smiapp-core.c 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934
  1. /*
  2. * drivers/media/i2c/smiapp/smiapp-core.c
  3. *
  4. * Generic driver for SMIA/SMIA++ compliant camera modules
  5. *
  6. * Copyright (C) 2010--2012 Nokia Corporation
  7. * Contact: Sakari Ailus <sakari.ailus@iki.fi>
  8. *
  9. * Based on smiapp driver by Vimarsh Zutshi
  10. * Based on jt8ev1.c by Vimarsh Zutshi
  11. * Based on smia-sensor.c by Tuukka Toivonen <tuukkat76@gmail.com>
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * version 2 as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful, but
  18. * WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  25. * 02110-1301 USA
  26. *
  27. */
  28. #include <linux/clk.h>
  29. #include <linux/delay.h>
  30. #include <linux/device.h>
  31. #include <linux/gpio.h>
  32. #include <linux/module.h>
  33. #include <linux/regulator/consumer.h>
  34. #include <linux/slab.h>
  35. #include <linux/smiapp.h>
  36. #include <linux/v4l2-mediabus.h>
  37. #include <media/v4l2-device.h>
  38. #include "smiapp.h"
  39. #define SMIAPP_ALIGN_DIM(dim, flags) \
  40. ((flags) & V4L2_SEL_FLAG_GE \
  41. ? ALIGN((dim), 2) \
  42. : (dim) & ~1)
  43. /*
  44. * smiapp_module_idents - supported camera modules
  45. */
  46. static const struct smiapp_module_ident smiapp_module_idents[] = {
  47. SMIAPP_IDENT_L(0x01, 0x022b, -1, "vs6555"),
  48. SMIAPP_IDENT_L(0x01, 0x022e, -1, "vw6558"),
  49. SMIAPP_IDENT_L(0x07, 0x7698, -1, "ovm7698"),
  50. SMIAPP_IDENT_L(0x0b, 0x4242, -1, "smiapp-003"),
  51. SMIAPP_IDENT_L(0x0c, 0x208a, -1, "tcm8330md"),
  52. SMIAPP_IDENT_LQ(0x0c, 0x2134, -1, "tcm8500md", &smiapp_tcm8500md_quirk),
  53. SMIAPP_IDENT_L(0x0c, 0x213e, -1, "et8en2"),
  54. SMIAPP_IDENT_L(0x0c, 0x2184, -1, "tcm8580md"),
  55. SMIAPP_IDENT_LQ(0x0c, 0x560f, -1, "jt8ew9", &smiapp_jt8ew9_quirk),
  56. SMIAPP_IDENT_LQ(0x10, 0x4141, -1, "jt8ev1", &smiapp_jt8ev1_quirk),
  57. SMIAPP_IDENT_LQ(0x10, 0x4241, -1, "imx125es", &smiapp_imx125es_quirk),
  58. };
  59. /*
  60. *
  61. * Dynamic Capability Identification
  62. *
  63. */
  64. static int smiapp_read_frame_fmt(struct smiapp_sensor *sensor)
  65. {
  66. struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
  67. u32 fmt_model_type, fmt_model_subtype, ncol_desc, nrow_desc;
  68. unsigned int i;
  69. int rval;
  70. int line_count = 0;
  71. int embedded_start = -1, embedded_end = -1;
  72. int image_start = 0;
  73. rval = smiapp_read(sensor, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_TYPE,
  74. &fmt_model_type);
  75. if (rval)
  76. return rval;
  77. rval = smiapp_read(sensor, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_SUBTYPE,
  78. &fmt_model_subtype);
  79. if (rval)
  80. return rval;
  81. ncol_desc = (fmt_model_subtype
  82. & SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NCOLS_MASK)
  83. >> SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NCOLS_SHIFT;
  84. nrow_desc = fmt_model_subtype
  85. & SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NROWS_MASK;
  86. dev_dbg(&client->dev, "format_model_type %s\n",
  87. fmt_model_type == SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE
  88. ? "2 byte" :
  89. fmt_model_type == SMIAPP_FRAME_FORMAT_MODEL_TYPE_4BYTE
  90. ? "4 byte" : "is simply bad");
  91. for (i = 0; i < ncol_desc + nrow_desc; i++) {
  92. u32 desc;
  93. u32 pixelcode;
  94. u32 pixels;
  95. char *which;
  96. char *what;
  97. if (fmt_model_type == SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE) {
  98. rval = smiapp_read(
  99. sensor,
  100. SMIAPP_REG_U16_FRAME_FORMAT_DESCRIPTOR_2(i),
  101. &desc);
  102. if (rval)
  103. return rval;
  104. pixelcode =
  105. (desc
  106. & SMIAPP_FRAME_FORMAT_DESC_2_PIXELCODE_MASK)
  107. >> SMIAPP_FRAME_FORMAT_DESC_2_PIXELCODE_SHIFT;
  108. pixels = desc & SMIAPP_FRAME_FORMAT_DESC_2_PIXELS_MASK;
  109. } else if (fmt_model_type
  110. == SMIAPP_FRAME_FORMAT_MODEL_TYPE_4BYTE) {
  111. rval = smiapp_read(
  112. sensor,
  113. SMIAPP_REG_U32_FRAME_FORMAT_DESCRIPTOR_4(i),
  114. &desc);
  115. if (rval)
  116. return rval;
  117. pixelcode =
  118. (desc
  119. & SMIAPP_FRAME_FORMAT_DESC_4_PIXELCODE_MASK)
  120. >> SMIAPP_FRAME_FORMAT_DESC_4_PIXELCODE_SHIFT;
  121. pixels = desc & SMIAPP_FRAME_FORMAT_DESC_4_PIXELS_MASK;
  122. } else {
  123. dev_dbg(&client->dev,
  124. "invalid frame format model type %d\n",
  125. fmt_model_type);
  126. return -EINVAL;
  127. }
  128. if (i < ncol_desc)
  129. which = "columns";
  130. else
  131. which = "rows";
  132. switch (pixelcode) {
  133. case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED:
  134. what = "embedded";
  135. break;
  136. case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DUMMY:
  137. what = "dummy";
  138. break;
  139. case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_BLACK:
  140. what = "black";
  141. break;
  142. case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DARK:
  143. what = "dark";
  144. break;
  145. case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE:
  146. what = "visible";
  147. break;
  148. default:
  149. what = "invalid";
  150. dev_dbg(&client->dev, "pixelcode %d\n", pixelcode);
  151. break;
  152. }
  153. dev_dbg(&client->dev, "%s pixels: %d %s\n",
  154. what, pixels, which);
  155. if (i < ncol_desc)
  156. continue;
  157. /* Handle row descriptors */
  158. if (pixelcode
  159. == SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED) {
  160. embedded_start = line_count;
  161. } else {
  162. if (pixelcode == SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE
  163. || pixels >= sensor->limits[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES] / 2)
  164. image_start = line_count;
  165. if (embedded_start != -1 && embedded_end == -1)
  166. embedded_end = line_count;
  167. }
  168. line_count += pixels;
  169. }
  170. if (embedded_start == -1 || embedded_end == -1) {
  171. embedded_start = 0;
  172. embedded_end = 0;
  173. }
  174. dev_dbg(&client->dev, "embedded data from lines %d to %d\n",
  175. embedded_start, embedded_end);
  176. dev_dbg(&client->dev, "image data starts at line %d\n", image_start);
  177. return 0;
  178. }
  179. static int smiapp_pll_configure(struct smiapp_sensor *sensor)
  180. {
  181. struct smiapp_pll *pll = &sensor->pll;
  182. int rval;
  183. rval = smiapp_write(
  184. sensor, SMIAPP_REG_U16_VT_PIX_CLK_DIV, pll->vt_pix_clk_div);
  185. if (rval < 0)
  186. return rval;
  187. rval = smiapp_write(
  188. sensor, SMIAPP_REG_U16_VT_SYS_CLK_DIV, pll->vt_sys_clk_div);
  189. if (rval < 0)
  190. return rval;
  191. rval = smiapp_write(
  192. sensor, SMIAPP_REG_U16_PRE_PLL_CLK_DIV, pll->pre_pll_clk_div);
  193. if (rval < 0)
  194. return rval;
  195. rval = smiapp_write(
  196. sensor, SMIAPP_REG_U16_PLL_MULTIPLIER, pll->pll_multiplier);
  197. if (rval < 0)
  198. return rval;
  199. /* Lane op clock ratio does not apply here. */
  200. rval = smiapp_write(
  201. sensor, SMIAPP_REG_U32_REQUESTED_LINK_BIT_RATE_MBPS,
  202. DIV_ROUND_UP(pll->op_sys_clk_freq_hz, 1000000 / 256 / 256));
  203. if (rval < 0 || sensor->minfo.smiapp_profile == SMIAPP_PROFILE_0)
  204. return rval;
  205. rval = smiapp_write(
  206. sensor, SMIAPP_REG_U16_OP_PIX_CLK_DIV, pll->op_pix_clk_div);
  207. if (rval < 0)
  208. return rval;
  209. return smiapp_write(
  210. sensor, SMIAPP_REG_U16_OP_SYS_CLK_DIV, pll->op_sys_clk_div);
  211. }
  212. static int smiapp_pll_update(struct smiapp_sensor *sensor)
  213. {
  214. struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
  215. struct smiapp_pll_limits lim = {
  216. .min_pre_pll_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_PRE_PLL_CLK_DIV],
  217. .max_pre_pll_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_PRE_PLL_CLK_DIV],
  218. .min_pll_ip_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_PLL_IP_FREQ_HZ],
  219. .max_pll_ip_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_PLL_IP_FREQ_HZ],
  220. .min_pll_multiplier = sensor->limits[SMIAPP_LIMIT_MIN_PLL_MULTIPLIER],
  221. .max_pll_multiplier = sensor->limits[SMIAPP_LIMIT_MAX_PLL_MULTIPLIER],
  222. .min_pll_op_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_PLL_OP_FREQ_HZ],
  223. .max_pll_op_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_PLL_OP_FREQ_HZ],
  224. .op.min_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV],
  225. .op.max_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV],
  226. .op.min_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV],
  227. .op.max_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV],
  228. .op.min_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_FREQ_HZ],
  229. .op.max_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_FREQ_HZ],
  230. .op.min_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_FREQ_HZ],
  231. .op.max_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_FREQ_HZ],
  232. .vt.min_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_VT_SYS_CLK_DIV],
  233. .vt.max_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_VT_SYS_CLK_DIV],
  234. .vt.min_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_VT_PIX_CLK_DIV],
  235. .vt.max_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_VT_PIX_CLK_DIV],
  236. .vt.min_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_VT_SYS_CLK_FREQ_HZ],
  237. .vt.max_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_VT_SYS_CLK_FREQ_HZ],
  238. .vt.min_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_VT_PIX_CLK_FREQ_HZ],
  239. .vt.max_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_VT_PIX_CLK_FREQ_HZ],
  240. .min_line_length_pck_bin = sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN],
  241. .min_line_length_pck = sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK],
  242. };
  243. struct smiapp_pll *pll = &sensor->pll;
  244. int rval;
  245. if (sensor->minfo.smiapp_profile == SMIAPP_PROFILE_0) {
  246. /*
  247. * Fill in operational clock divisors limits from the
  248. * video timing ones. On profile 0 sensors the
  249. * requirements regarding them are essentially the
  250. * same as on VT ones.
  251. */
  252. lim.op = lim.vt;
  253. }
  254. pll->binning_horizontal = sensor->binning_horizontal;
  255. pll->binning_vertical = sensor->binning_vertical;
  256. pll->link_freq =
  257. sensor->link_freq->qmenu_int[sensor->link_freq->val];
  258. pll->scale_m = sensor->scale_m;
  259. pll->bits_per_pixel = sensor->csi_format->compressed;
  260. rval = smiapp_pll_calculate(&client->dev, &lim, pll);
  261. if (rval < 0)
  262. return rval;
  263. __v4l2_ctrl_s_ctrl_int64(sensor->pixel_rate_parray,
  264. pll->vt_pix_clk_freq_hz);
  265. __v4l2_ctrl_s_ctrl_int64(sensor->pixel_rate_csi, pll->pixel_rate_csi);
  266. return 0;
  267. }
  268. /*
  269. *
  270. * V4L2 Controls handling
  271. *
  272. */
  273. static void __smiapp_update_exposure_limits(struct smiapp_sensor *sensor)
  274. {
  275. struct v4l2_ctrl *ctrl = sensor->exposure;
  276. int max;
  277. max = sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
  278. + sensor->vblank->val
  279. - sensor->limits[SMIAPP_LIMIT_COARSE_INTEGRATION_TIME_MAX_MARGIN];
  280. __v4l2_ctrl_modify_range(ctrl, ctrl->minimum, max, ctrl->step, max);
  281. }
  282. /*
  283. * Order matters.
  284. *
  285. * 1. Bits-per-pixel, descending.
  286. * 2. Bits-per-pixel compressed, descending.
  287. * 3. Pixel order, same as in pixel_order_str. Formats for all four pixel
  288. * orders must be defined.
  289. */
  290. static const struct smiapp_csi_data_format smiapp_csi_data_formats[] = {
  291. { V4L2_MBUS_FMT_SGRBG12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_GRBG, },
  292. { V4L2_MBUS_FMT_SRGGB12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_RGGB, },
  293. { V4L2_MBUS_FMT_SBGGR12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_BGGR, },
  294. { V4L2_MBUS_FMT_SGBRG12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_GBRG, },
  295. { V4L2_MBUS_FMT_SGRBG10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_GRBG, },
  296. { V4L2_MBUS_FMT_SRGGB10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_RGGB, },
  297. { V4L2_MBUS_FMT_SBGGR10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_BGGR, },
  298. { V4L2_MBUS_FMT_SGBRG10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_GBRG, },
  299. { V4L2_MBUS_FMT_SGRBG10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_GRBG, },
  300. { V4L2_MBUS_FMT_SRGGB10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_RGGB, },
  301. { V4L2_MBUS_FMT_SBGGR10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_BGGR, },
  302. { V4L2_MBUS_FMT_SGBRG10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_GBRG, },
  303. { V4L2_MBUS_FMT_SGRBG8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_GRBG, },
  304. { V4L2_MBUS_FMT_SRGGB8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_RGGB, },
  305. { V4L2_MBUS_FMT_SBGGR8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_BGGR, },
  306. { V4L2_MBUS_FMT_SGBRG8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_GBRG, },
  307. };
  308. const char *pixel_order_str[] = { "GRBG", "RGGB", "BGGR", "GBRG" };
  309. #define to_csi_format_idx(fmt) (((unsigned long)(fmt) \
  310. - (unsigned long)smiapp_csi_data_formats) \
  311. / sizeof(*smiapp_csi_data_formats))
  312. static u32 smiapp_pixel_order(struct smiapp_sensor *sensor)
  313. {
  314. struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
  315. int flip = 0;
  316. if (sensor->hflip) {
  317. if (sensor->hflip->val)
  318. flip |= SMIAPP_IMAGE_ORIENTATION_HFLIP;
  319. if (sensor->vflip->val)
  320. flip |= SMIAPP_IMAGE_ORIENTATION_VFLIP;
  321. }
  322. flip ^= sensor->hvflip_inv_mask;
  323. dev_dbg(&client->dev, "flip %d\n", flip);
  324. return sensor->default_pixel_order ^ flip;
  325. }
  326. static void smiapp_update_mbus_formats(struct smiapp_sensor *sensor)
  327. {
  328. struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
  329. unsigned int csi_format_idx =
  330. to_csi_format_idx(sensor->csi_format) & ~3;
  331. unsigned int internal_csi_format_idx =
  332. to_csi_format_idx(sensor->internal_csi_format) & ~3;
  333. unsigned int pixel_order = smiapp_pixel_order(sensor);
  334. sensor->mbus_frame_fmts =
  335. sensor->default_mbus_frame_fmts << pixel_order;
  336. sensor->csi_format =
  337. &smiapp_csi_data_formats[csi_format_idx + pixel_order];
  338. sensor->internal_csi_format =
  339. &smiapp_csi_data_formats[internal_csi_format_idx
  340. + pixel_order];
  341. BUG_ON(max(internal_csi_format_idx, csi_format_idx) + pixel_order
  342. >= ARRAY_SIZE(smiapp_csi_data_formats));
  343. dev_dbg(&client->dev, "new pixel order %s\n",
  344. pixel_order_str[pixel_order]);
  345. }
  346. static const char * const smiapp_test_patterns[] = {
  347. "Disabled",
  348. "Solid Colour",
  349. "Eight Vertical Colour Bars",
  350. "Colour Bars With Fade to Grey",
  351. "Pseudorandom Sequence (PN9)",
  352. };
  353. static int smiapp_set_ctrl(struct v4l2_ctrl *ctrl)
  354. {
  355. struct smiapp_sensor *sensor =
  356. container_of(ctrl->handler, struct smiapp_subdev, ctrl_handler)
  357. ->sensor;
  358. u32 orient = 0;
  359. int exposure;
  360. int rval;
  361. switch (ctrl->id) {
  362. case V4L2_CID_ANALOGUE_GAIN:
  363. return smiapp_write(
  364. sensor,
  365. SMIAPP_REG_U16_ANALOGUE_GAIN_CODE_GLOBAL, ctrl->val);
  366. case V4L2_CID_EXPOSURE:
  367. return smiapp_write(
  368. sensor,
  369. SMIAPP_REG_U16_COARSE_INTEGRATION_TIME, ctrl->val);
  370. case V4L2_CID_HFLIP:
  371. case V4L2_CID_VFLIP:
  372. if (sensor->streaming)
  373. return -EBUSY;
  374. if (sensor->hflip->val)
  375. orient |= SMIAPP_IMAGE_ORIENTATION_HFLIP;
  376. if (sensor->vflip->val)
  377. orient |= SMIAPP_IMAGE_ORIENTATION_VFLIP;
  378. orient ^= sensor->hvflip_inv_mask;
  379. rval = smiapp_write(sensor,
  380. SMIAPP_REG_U8_IMAGE_ORIENTATION,
  381. orient);
  382. if (rval < 0)
  383. return rval;
  384. smiapp_update_mbus_formats(sensor);
  385. return 0;
  386. case V4L2_CID_VBLANK:
  387. exposure = sensor->exposure->val;
  388. __smiapp_update_exposure_limits(sensor);
  389. if (exposure > sensor->exposure->maximum) {
  390. sensor->exposure->val =
  391. sensor->exposure->maximum;
  392. rval = smiapp_set_ctrl(
  393. sensor->exposure);
  394. if (rval < 0)
  395. return rval;
  396. }
  397. return smiapp_write(
  398. sensor, SMIAPP_REG_U16_FRAME_LENGTH_LINES,
  399. sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
  400. + ctrl->val);
  401. case V4L2_CID_HBLANK:
  402. return smiapp_write(
  403. sensor, SMIAPP_REG_U16_LINE_LENGTH_PCK,
  404. sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width
  405. + ctrl->val);
  406. case V4L2_CID_LINK_FREQ:
  407. if (sensor->streaming)
  408. return -EBUSY;
  409. return smiapp_pll_update(sensor);
  410. case V4L2_CID_TEST_PATTERN: {
  411. unsigned int i;
  412. for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++)
  413. v4l2_ctrl_activate(
  414. sensor->test_data[i],
  415. ctrl->val ==
  416. V4L2_SMIAPP_TEST_PATTERN_MODE_SOLID_COLOUR);
  417. return smiapp_write(
  418. sensor, SMIAPP_REG_U16_TEST_PATTERN_MODE, ctrl->val);
  419. }
  420. case V4L2_CID_TEST_PATTERN_RED:
  421. return smiapp_write(
  422. sensor, SMIAPP_REG_U16_TEST_DATA_RED, ctrl->val);
  423. case V4L2_CID_TEST_PATTERN_GREENR:
  424. return smiapp_write(
  425. sensor, SMIAPP_REG_U16_TEST_DATA_GREENR, ctrl->val);
  426. case V4L2_CID_TEST_PATTERN_BLUE:
  427. return smiapp_write(
  428. sensor, SMIAPP_REG_U16_TEST_DATA_BLUE, ctrl->val);
  429. case V4L2_CID_TEST_PATTERN_GREENB:
  430. return smiapp_write(
  431. sensor, SMIAPP_REG_U16_TEST_DATA_GREENB, ctrl->val);
  432. case V4L2_CID_PIXEL_RATE:
  433. /* For v4l2_ctrl_s_ctrl_int64() used internally. */
  434. return 0;
  435. default:
  436. return -EINVAL;
  437. }
  438. }
  439. static const struct v4l2_ctrl_ops smiapp_ctrl_ops = {
  440. .s_ctrl = smiapp_set_ctrl,
  441. };
  442. static int smiapp_init_controls(struct smiapp_sensor *sensor)
  443. {
  444. struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
  445. unsigned int max, i;
  446. int rval;
  447. rval = v4l2_ctrl_handler_init(&sensor->pixel_array->ctrl_handler, 12);
  448. if (rval)
  449. return rval;
  450. sensor->pixel_array->ctrl_handler.lock = &sensor->mutex;
  451. sensor->analog_gain = v4l2_ctrl_new_std(
  452. &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
  453. V4L2_CID_ANALOGUE_GAIN,
  454. sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN],
  455. sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MAX],
  456. max(sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_STEP], 1U),
  457. sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN]);
  458. /* Exposure limits will be updated soon, use just something here. */
  459. sensor->exposure = v4l2_ctrl_new_std(
  460. &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
  461. V4L2_CID_EXPOSURE, 0, 0, 1, 0);
  462. sensor->hflip = v4l2_ctrl_new_std(
  463. &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
  464. V4L2_CID_HFLIP, 0, 1, 1, 0);
  465. sensor->vflip = v4l2_ctrl_new_std(
  466. &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
  467. V4L2_CID_VFLIP, 0, 1, 1, 0);
  468. sensor->vblank = v4l2_ctrl_new_std(
  469. &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
  470. V4L2_CID_VBLANK, 0, 1, 1, 0);
  471. if (sensor->vblank)
  472. sensor->vblank->flags |= V4L2_CTRL_FLAG_UPDATE;
  473. sensor->hblank = v4l2_ctrl_new_std(
  474. &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
  475. V4L2_CID_HBLANK, 0, 1, 1, 0);
  476. if (sensor->hblank)
  477. sensor->hblank->flags |= V4L2_CTRL_FLAG_UPDATE;
  478. sensor->pixel_rate_parray = v4l2_ctrl_new_std(
  479. &sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
  480. V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1);
  481. v4l2_ctrl_new_std_menu_items(&sensor->pixel_array->ctrl_handler,
  482. &smiapp_ctrl_ops, V4L2_CID_TEST_PATTERN,
  483. ARRAY_SIZE(smiapp_test_patterns) - 1,
  484. 0, 0, smiapp_test_patterns);
  485. for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++) {
  486. int max_value = (1 << sensor->csi_format->width) - 1;
  487. sensor->test_data[i] =
  488. v4l2_ctrl_new_std(
  489. &sensor->pixel_array->ctrl_handler,
  490. &smiapp_ctrl_ops, V4L2_CID_TEST_PATTERN_RED + i,
  491. 0, max_value, 1, max_value);
  492. }
  493. if (sensor->pixel_array->ctrl_handler.error) {
  494. dev_err(&client->dev,
  495. "pixel array controls initialization failed (%d)\n",
  496. sensor->pixel_array->ctrl_handler.error);
  497. rval = sensor->pixel_array->ctrl_handler.error;
  498. goto error;
  499. }
  500. sensor->pixel_array->sd.ctrl_handler =
  501. &sensor->pixel_array->ctrl_handler;
  502. v4l2_ctrl_cluster(2, &sensor->hflip);
  503. rval = v4l2_ctrl_handler_init(&sensor->src->ctrl_handler, 0);
  504. if (rval)
  505. goto error;
  506. sensor->src->ctrl_handler.lock = &sensor->mutex;
  507. for (max = 0; sensor->platform_data->op_sys_clock[max + 1]; max++);
  508. sensor->link_freq = v4l2_ctrl_new_int_menu(
  509. &sensor->src->ctrl_handler, &smiapp_ctrl_ops,
  510. V4L2_CID_LINK_FREQ, max, 0,
  511. sensor->platform_data->op_sys_clock);
  512. sensor->pixel_rate_csi = v4l2_ctrl_new_std(
  513. &sensor->src->ctrl_handler, &smiapp_ctrl_ops,
  514. V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1);
  515. if (sensor->src->ctrl_handler.error) {
  516. dev_err(&client->dev,
  517. "src controls initialization failed (%d)\n",
  518. sensor->src->ctrl_handler.error);
  519. rval = sensor->src->ctrl_handler.error;
  520. goto error;
  521. }
  522. sensor->src->sd.ctrl_handler =
  523. &sensor->src->ctrl_handler;
  524. return 0;
  525. error:
  526. v4l2_ctrl_handler_free(&sensor->pixel_array->ctrl_handler);
  527. v4l2_ctrl_handler_free(&sensor->src->ctrl_handler);
  528. return rval;
  529. }
  530. static void smiapp_free_controls(struct smiapp_sensor *sensor)
  531. {
  532. unsigned int i;
  533. for (i = 0; i < sensor->ssds_used; i++)
  534. v4l2_ctrl_handler_free(&sensor->ssds[i].ctrl_handler);
  535. }
  536. static int smiapp_get_limits(struct smiapp_sensor *sensor, int const *limit,
  537. unsigned int n)
  538. {
  539. struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
  540. unsigned int i;
  541. u32 val;
  542. int rval;
  543. for (i = 0; i < n; i++) {
  544. rval = smiapp_read(
  545. sensor, smiapp_reg_limits[limit[i]].addr, &val);
  546. if (rval)
  547. return rval;
  548. sensor->limits[limit[i]] = val;
  549. dev_dbg(&client->dev, "0x%8.8x \"%s\" = %u, 0x%x\n",
  550. smiapp_reg_limits[limit[i]].addr,
  551. smiapp_reg_limits[limit[i]].what, val, val);
  552. }
  553. return 0;
  554. }
  555. static int smiapp_get_all_limits(struct smiapp_sensor *sensor)
  556. {
  557. unsigned int i;
  558. int rval;
  559. for (i = 0; i < SMIAPP_LIMIT_LAST; i++) {
  560. rval = smiapp_get_limits(sensor, &i, 1);
  561. if (rval < 0)
  562. return rval;
  563. }
  564. if (sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] == 0)
  565. smiapp_replace_limit(sensor, SMIAPP_LIMIT_SCALER_N_MIN, 16);
  566. return 0;
  567. }
  568. static int smiapp_get_limits_binning(struct smiapp_sensor *sensor)
  569. {
  570. struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
  571. static u32 const limits[] = {
  572. SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN,
  573. SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN,
  574. SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN,
  575. SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN,
  576. SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN,
  577. SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN_BIN,
  578. SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN_BIN,
  579. };
  580. static u32 const limits_replace[] = {
  581. SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES,
  582. SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES,
  583. SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK,
  584. SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK,
  585. SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK,
  586. SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN,
  587. SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN,
  588. };
  589. unsigned int i;
  590. int rval;
  591. if (sensor->limits[SMIAPP_LIMIT_BINNING_CAPABILITY] ==
  592. SMIAPP_BINNING_CAPABILITY_NO) {
  593. for (i = 0; i < ARRAY_SIZE(limits); i++)
  594. sensor->limits[limits[i]] =
  595. sensor->limits[limits_replace[i]];
  596. return 0;
  597. }
  598. rval = smiapp_get_limits(sensor, limits, ARRAY_SIZE(limits));
  599. if (rval < 0)
  600. return rval;
  601. /*
  602. * Sanity check whether the binning limits are valid. If not,
  603. * use the non-binning ones.
  604. */
  605. if (sensor->limits[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN]
  606. && sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN]
  607. && sensor->limits[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN])
  608. return 0;
  609. for (i = 0; i < ARRAY_SIZE(limits); i++) {
  610. dev_dbg(&client->dev,
  611. "replace limit 0x%8.8x \"%s\" = %d, 0x%x\n",
  612. smiapp_reg_limits[limits[i]].addr,
  613. smiapp_reg_limits[limits[i]].what,
  614. sensor->limits[limits_replace[i]],
  615. sensor->limits[limits_replace[i]]);
  616. sensor->limits[limits[i]] =
  617. sensor->limits[limits_replace[i]];
  618. }
  619. return 0;
  620. }
  621. static int smiapp_get_mbus_formats(struct smiapp_sensor *sensor)
  622. {
  623. struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
  624. unsigned int type, n;
  625. unsigned int i, pixel_order;
  626. int rval;
  627. rval = smiapp_read(
  628. sensor, SMIAPP_REG_U8_DATA_FORMAT_MODEL_TYPE, &type);
  629. if (rval)
  630. return rval;
  631. dev_dbg(&client->dev, "data_format_model_type %d\n", type);
  632. rval = smiapp_read(sensor, SMIAPP_REG_U8_PIXEL_ORDER,
  633. &pixel_order);
  634. if (rval)
  635. return rval;
  636. if (pixel_order >= ARRAY_SIZE(pixel_order_str)) {
  637. dev_dbg(&client->dev, "bad pixel order %d\n", pixel_order);
  638. return -EINVAL;
  639. }
  640. dev_dbg(&client->dev, "pixel order %d (%s)\n", pixel_order,
  641. pixel_order_str[pixel_order]);
  642. switch (type) {
  643. case SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL:
  644. n = SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL_N;
  645. break;
  646. case SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED:
  647. n = SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED_N;
  648. break;
  649. default:
  650. return -EINVAL;
  651. }
  652. sensor->default_pixel_order = pixel_order;
  653. sensor->mbus_frame_fmts = 0;
  654. for (i = 0; i < n; i++) {
  655. unsigned int fmt, j;
  656. rval = smiapp_read(
  657. sensor,
  658. SMIAPP_REG_U16_DATA_FORMAT_DESCRIPTOR(i), &fmt);
  659. if (rval)
  660. return rval;
  661. dev_dbg(&client->dev, "%u: bpp %u, compressed %u\n",
  662. i, fmt >> 8, (u8)fmt);
  663. for (j = 0; j < ARRAY_SIZE(smiapp_csi_data_formats); j++) {
  664. const struct smiapp_csi_data_format *f =
  665. &smiapp_csi_data_formats[j];
  666. if (f->pixel_order != SMIAPP_PIXEL_ORDER_GRBG)
  667. continue;
  668. if (f->width != fmt >> 8 || f->compressed != (u8)fmt)
  669. continue;
  670. dev_dbg(&client->dev, "jolly good! %d\n", j);
  671. sensor->default_mbus_frame_fmts |= 1 << j;
  672. if (!sensor->csi_format
  673. || f->width > sensor->csi_format->width
  674. || (f->width == sensor->csi_format->width
  675. && f->compressed
  676. > sensor->csi_format->compressed)) {
  677. sensor->csi_format = f;
  678. sensor->internal_csi_format = f;
  679. }
  680. }
  681. }
  682. if (!sensor->csi_format) {
  683. dev_err(&client->dev, "no supported mbus code found\n");
  684. return -EINVAL;
  685. }
  686. smiapp_update_mbus_formats(sensor);
  687. return 0;
  688. }
  689. static void smiapp_update_blanking(struct smiapp_sensor *sensor)
  690. {
  691. struct v4l2_ctrl *vblank = sensor->vblank;
  692. struct v4l2_ctrl *hblank = sensor->hblank;
  693. int min, max;
  694. min = max_t(int,
  695. sensor->limits[SMIAPP_LIMIT_MIN_FRAME_BLANKING_LINES],
  696. sensor->limits[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN] -
  697. sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height);
  698. max = sensor->limits[SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN] -
  699. sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height;
  700. __v4l2_ctrl_modify_range(vblank, min, max, vblank->step, min);
  701. min = max_t(int,
  702. sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN] -
  703. sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width,
  704. sensor->limits[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN]);
  705. max = sensor->limits[SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN] -
  706. sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width;
  707. __v4l2_ctrl_modify_range(hblank, min, max, hblank->step, min);
  708. __smiapp_update_exposure_limits(sensor);
  709. }
  710. static int smiapp_update_mode(struct smiapp_sensor *sensor)
  711. {
  712. struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
  713. unsigned int binning_mode;
  714. int rval;
  715. dev_dbg(&client->dev, "frame size: %dx%d\n",
  716. sensor->src->crop[SMIAPP_PAD_SRC].width,
  717. sensor->src->crop[SMIAPP_PAD_SRC].height);
  718. dev_dbg(&client->dev, "csi format width: %d\n",
  719. sensor->csi_format->width);
  720. /* Binning has to be set up here; it affects limits */
  721. if (sensor->binning_horizontal == 1 &&
  722. sensor->binning_vertical == 1) {
  723. binning_mode = 0;
  724. } else {
  725. u8 binning_type =
  726. (sensor->binning_horizontal << 4)
  727. | sensor->binning_vertical;
  728. rval = smiapp_write(
  729. sensor, SMIAPP_REG_U8_BINNING_TYPE, binning_type);
  730. if (rval < 0)
  731. return rval;
  732. binning_mode = 1;
  733. }
  734. rval = smiapp_write(sensor, SMIAPP_REG_U8_BINNING_MODE, binning_mode);
  735. if (rval < 0)
  736. return rval;
  737. /* Get updated limits due to binning */
  738. rval = smiapp_get_limits_binning(sensor);
  739. if (rval < 0)
  740. return rval;
  741. rval = smiapp_pll_update(sensor);
  742. if (rval < 0)
  743. return rval;
  744. /* Output from pixel array, including blanking */
  745. smiapp_update_blanking(sensor);
  746. dev_dbg(&client->dev, "vblank\t\t%d\n", sensor->vblank->val);
  747. dev_dbg(&client->dev, "hblank\t\t%d\n", sensor->hblank->val);
  748. dev_dbg(&client->dev, "real timeperframe\t100/%d\n",
  749. sensor->pll.vt_pix_clk_freq_hz /
  750. ((sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width
  751. + sensor->hblank->val) *
  752. (sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
  753. + sensor->vblank->val) / 100));
  754. return 0;
  755. }
  756. /*
  757. *
  758. * SMIA++ NVM handling
  759. *
  760. */
  761. static int smiapp_read_nvm(struct smiapp_sensor *sensor,
  762. unsigned char *nvm)
  763. {
  764. u32 i, s, p, np, v;
  765. int rval = 0, rval2;
  766. np = sensor->nvm_size / SMIAPP_NVM_PAGE_SIZE;
  767. for (p = 0; p < np; p++) {
  768. rval = smiapp_write(
  769. sensor,
  770. SMIAPP_REG_U8_DATA_TRANSFER_IF_1_PAGE_SELECT, p);
  771. if (rval)
  772. goto out;
  773. rval = smiapp_write(sensor,
  774. SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL,
  775. SMIAPP_DATA_TRANSFER_IF_1_CTRL_EN |
  776. SMIAPP_DATA_TRANSFER_IF_1_CTRL_RD_EN);
  777. if (rval)
  778. goto out;
  779. for (i = 0; i < 1000; i++) {
  780. rval = smiapp_read(
  781. sensor,
  782. SMIAPP_REG_U8_DATA_TRANSFER_IF_1_STATUS, &s);
  783. if (rval)
  784. goto out;
  785. if (s & SMIAPP_DATA_TRANSFER_IF_1_STATUS_RD_READY)
  786. break;
  787. if (--i == 0) {
  788. rval = -ETIMEDOUT;
  789. goto out;
  790. }
  791. }
  792. for (i = 0; i < SMIAPP_NVM_PAGE_SIZE; i++) {
  793. rval = smiapp_read(
  794. sensor,
  795. SMIAPP_REG_U8_DATA_TRANSFER_IF_1_DATA_0 + i,
  796. &v);
  797. if (rval)
  798. goto out;
  799. *nvm++ = v;
  800. }
  801. }
  802. out:
  803. rval2 = smiapp_write(sensor, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL, 0);
  804. if (rval < 0)
  805. return rval;
  806. else
  807. return rval2;
  808. }
  809. /*
  810. *
  811. * SMIA++ CCI address control
  812. *
  813. */
  814. static int smiapp_change_cci_addr(struct smiapp_sensor *sensor)
  815. {
  816. struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
  817. int rval;
  818. u32 val;
  819. client->addr = sensor->platform_data->i2c_addr_dfl;
  820. rval = smiapp_write(sensor,
  821. SMIAPP_REG_U8_CCI_ADDRESS_CONTROL,
  822. sensor->platform_data->i2c_addr_alt << 1);
  823. if (rval)
  824. return rval;
  825. client->addr = sensor->platform_data->i2c_addr_alt;
  826. /* verify addr change went ok */
  827. rval = smiapp_read(sensor, SMIAPP_REG_U8_CCI_ADDRESS_CONTROL, &val);
  828. if (rval)
  829. return rval;
  830. if (val != sensor->platform_data->i2c_addr_alt << 1)
  831. return -ENODEV;
  832. return 0;
  833. }
  834. /*
  835. *
  836. * SMIA++ Mode Control
  837. *
  838. */
  839. static int smiapp_setup_flash_strobe(struct smiapp_sensor *sensor)
  840. {
  841. struct smiapp_flash_strobe_parms *strobe_setup;
  842. unsigned int ext_freq = sensor->platform_data->ext_clk;
  843. u32 tmp;
  844. u32 strobe_adjustment;
  845. u32 strobe_width_high_rs;
  846. int rval;
  847. strobe_setup = sensor->platform_data->strobe_setup;
  848. /*
  849. * How to calculate registers related to strobe length. Please
  850. * do not change, or if you do at least know what you're
  851. * doing. :-)
  852. *
  853. * Sakari Ailus <sakari.ailus@iki.fi> 2010-10-25
  854. *
  855. * flash_strobe_length [us] / 10^6 = (tFlash_strobe_width_ctrl
  856. * / EXTCLK freq [Hz]) * flash_strobe_adjustment
  857. *
  858. * tFlash_strobe_width_ctrl E N, [1 - 0xffff]
  859. * flash_strobe_adjustment E N, [1 - 0xff]
  860. *
  861. * The formula above is written as below to keep it on one
  862. * line:
  863. *
  864. * l / 10^6 = w / e * a
  865. *
  866. * Let's mark w * a by x:
  867. *
  868. * x = w * a
  869. *
  870. * Thus, we get:
  871. *
  872. * x = l * e / 10^6
  873. *
  874. * The strobe width must be at least as long as requested,
  875. * thus rounding upwards is needed.
  876. *
  877. * x = (l * e + 10^6 - 1) / 10^6
  878. * -----------------------------
  879. *
  880. * Maximum possible accuracy is wanted at all times. Thus keep
  881. * a as small as possible.
  882. *
  883. * Calculate a, assuming maximum w, with rounding upwards:
  884. *
  885. * a = (x + (2^16 - 1) - 1) / (2^16 - 1)
  886. * -------------------------------------
  887. *
  888. * Thus, we also get w, with that a, with rounding upwards:
  889. *
  890. * w = (x + a - 1) / a
  891. * -------------------
  892. *
  893. * To get limits:
  894. *
  895. * x E [1, (2^16 - 1) * (2^8 - 1)]
  896. *
  897. * Substituting maximum x to the original formula (with rounding),
  898. * the maximum l is thus
  899. *
  900. * (2^16 - 1) * (2^8 - 1) * 10^6 = l * e + 10^6 - 1
  901. *
  902. * l = (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / e
  903. * --------------------------------------------------
  904. *
  905. * flash_strobe_length must be clamped between 1 and
  906. * (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / EXTCLK freq.
  907. *
  908. * Then,
  909. *
  910. * flash_strobe_adjustment = ((flash_strobe_length *
  911. * EXTCLK freq + 10^6 - 1) / 10^6 + (2^16 - 1) - 1) / (2^16 - 1)
  912. *
  913. * tFlash_strobe_width_ctrl = ((flash_strobe_length *
  914. * EXTCLK freq + 10^6 - 1) / 10^6 +
  915. * flash_strobe_adjustment - 1) / flash_strobe_adjustment
  916. */
  917. tmp = div_u64(1000000ULL * ((1 << 16) - 1) * ((1 << 8) - 1) -
  918. 1000000 + 1, ext_freq);
  919. strobe_setup->strobe_width_high_us =
  920. clamp_t(u32, strobe_setup->strobe_width_high_us, 1, tmp);
  921. tmp = div_u64(((u64)strobe_setup->strobe_width_high_us * (u64)ext_freq +
  922. 1000000 - 1), 1000000ULL);
  923. strobe_adjustment = (tmp + (1 << 16) - 1 - 1) / ((1 << 16) - 1);
  924. strobe_width_high_rs = (tmp + strobe_adjustment - 1) /
  925. strobe_adjustment;
  926. rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_MODE_RS,
  927. strobe_setup->mode);
  928. if (rval < 0)
  929. goto out;
  930. rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_STROBE_ADJUSTMENT,
  931. strobe_adjustment);
  932. if (rval < 0)
  933. goto out;
  934. rval = smiapp_write(
  935. sensor, SMIAPP_REG_U16_TFLASH_STROBE_WIDTH_HIGH_RS_CTRL,
  936. strobe_width_high_rs);
  937. if (rval < 0)
  938. goto out;
  939. rval = smiapp_write(sensor, SMIAPP_REG_U16_TFLASH_STROBE_DELAY_RS_CTRL,
  940. strobe_setup->strobe_delay);
  941. if (rval < 0)
  942. goto out;
  943. rval = smiapp_write(sensor, SMIAPP_REG_U16_FLASH_STROBE_START_POINT,
  944. strobe_setup->stobe_start_point);
  945. if (rval < 0)
  946. goto out;
  947. rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_TRIGGER_RS,
  948. strobe_setup->trigger);
  949. out:
  950. sensor->platform_data->strobe_setup->trigger = 0;
  951. return rval;
  952. }
  953. /* -----------------------------------------------------------------------------
  954. * Power management
  955. */
  956. static int smiapp_power_on(struct smiapp_sensor *sensor)
  957. {
  958. struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
  959. unsigned int sleep;
  960. int rval;
  961. rval = regulator_enable(sensor->vana);
  962. if (rval) {
  963. dev_err(&client->dev, "failed to enable vana regulator\n");
  964. return rval;
  965. }
  966. usleep_range(1000, 1000);
  967. if (sensor->platform_data->set_xclk)
  968. rval = sensor->platform_data->set_xclk(
  969. &sensor->src->sd, sensor->platform_data->ext_clk);
  970. else
  971. rval = clk_prepare_enable(sensor->ext_clk);
  972. if (rval < 0) {
  973. dev_dbg(&client->dev, "failed to enable xclk\n");
  974. goto out_xclk_fail;
  975. }
  976. usleep_range(1000, 1000);
  977. if (gpio_is_valid(sensor->platform_data->xshutdown))
  978. gpio_set_value(sensor->platform_data->xshutdown, 1);
  979. sleep = SMIAPP_RESET_DELAY(sensor->platform_data->ext_clk);
  980. usleep_range(sleep, sleep);
  981. /*
  982. * Failures to respond to the address change command have been noticed.
  983. * Those failures seem to be caused by the sensor requiring a longer
  984. * boot time than advertised. An additional 10ms delay seems to work
  985. * around the issue, but the SMIA++ I2C write retry hack makes the delay
  986. * unnecessary. The failures need to be investigated to find a proper
  987. * fix, and a delay will likely need to be added here if the I2C write
  988. * retry hack is reverted before the root cause of the boot time issue
  989. * is found.
  990. */
  991. if (sensor->platform_data->i2c_addr_alt) {
  992. rval = smiapp_change_cci_addr(sensor);
  993. if (rval) {
  994. dev_err(&client->dev, "cci address change error\n");
  995. goto out_cci_addr_fail;
  996. }
  997. }
  998. rval = smiapp_write(sensor, SMIAPP_REG_U8_SOFTWARE_RESET,
  999. SMIAPP_SOFTWARE_RESET);
  1000. if (rval < 0) {
  1001. dev_err(&client->dev, "software reset failed\n");
  1002. goto out_cci_addr_fail;
  1003. }
  1004. if (sensor->platform_data->i2c_addr_alt) {
  1005. rval = smiapp_change_cci_addr(sensor);
  1006. if (rval) {
  1007. dev_err(&client->dev, "cci address change error\n");
  1008. goto out_cci_addr_fail;
  1009. }
  1010. }
  1011. rval = smiapp_write(sensor, SMIAPP_REG_U16_COMPRESSION_MODE,
  1012. SMIAPP_COMPRESSION_MODE_SIMPLE_PREDICTOR);
  1013. if (rval) {
  1014. dev_err(&client->dev, "compression mode set failed\n");
  1015. goto out_cci_addr_fail;
  1016. }
  1017. rval = smiapp_write(
  1018. sensor, SMIAPP_REG_U16_EXTCLK_FREQUENCY_MHZ,
  1019. sensor->platform_data->ext_clk / (1000000 / (1 << 8)));
  1020. if (rval) {
  1021. dev_err(&client->dev, "extclk frequency set failed\n");
  1022. goto out_cci_addr_fail;
  1023. }
  1024. rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_LANE_MODE,
  1025. sensor->platform_data->lanes - 1);
  1026. if (rval) {
  1027. dev_err(&client->dev, "csi lane mode set failed\n");
  1028. goto out_cci_addr_fail;
  1029. }
  1030. rval = smiapp_write(sensor, SMIAPP_REG_U8_FAST_STANDBY_CTRL,
  1031. SMIAPP_FAST_STANDBY_CTRL_IMMEDIATE);
  1032. if (rval) {
  1033. dev_err(&client->dev, "fast standby set failed\n");
  1034. goto out_cci_addr_fail;
  1035. }
  1036. rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_SIGNALLING_MODE,
  1037. sensor->platform_data->csi_signalling_mode);
  1038. if (rval) {
  1039. dev_err(&client->dev, "csi signalling mode set failed\n");
  1040. goto out_cci_addr_fail;
  1041. }
  1042. /* DPHY control done by sensor based on requested link rate */
  1043. rval = smiapp_write(sensor, SMIAPP_REG_U8_DPHY_CTRL,
  1044. SMIAPP_DPHY_CTRL_UI);
  1045. if (rval < 0)
  1046. return rval;
  1047. rval = smiapp_call_quirk(sensor, post_poweron);
  1048. if (rval) {
  1049. dev_err(&client->dev, "post_poweron quirks failed\n");
  1050. goto out_cci_addr_fail;
  1051. }
  1052. /* Are we still initialising...? If yes, return here. */
  1053. if (!sensor->pixel_array)
  1054. return 0;
  1055. rval = v4l2_ctrl_handler_setup(
  1056. &sensor->pixel_array->ctrl_handler);
  1057. if (rval)
  1058. goto out_cci_addr_fail;
  1059. rval = v4l2_ctrl_handler_setup(&sensor->src->ctrl_handler);
  1060. if (rval)
  1061. goto out_cci_addr_fail;
  1062. mutex_lock(&sensor->mutex);
  1063. rval = smiapp_update_mode(sensor);
  1064. mutex_unlock(&sensor->mutex);
  1065. if (rval < 0)
  1066. goto out_cci_addr_fail;
  1067. return 0;
  1068. out_cci_addr_fail:
  1069. if (gpio_is_valid(sensor->platform_data->xshutdown))
  1070. gpio_set_value(sensor->platform_data->xshutdown, 0);
  1071. if (sensor->platform_data->set_xclk)
  1072. sensor->platform_data->set_xclk(&sensor->src->sd, 0);
  1073. else
  1074. clk_disable_unprepare(sensor->ext_clk);
  1075. out_xclk_fail:
  1076. regulator_disable(sensor->vana);
  1077. return rval;
  1078. }
  1079. static void smiapp_power_off(struct smiapp_sensor *sensor)
  1080. {
  1081. /*
  1082. * Currently power/clock to lens are enable/disabled separately
  1083. * but they are essentially the same signals. So if the sensor is
  1084. * powered off while the lens is powered on the sensor does not
  1085. * really see a power off and next time the cci address change
  1086. * will fail. So do a soft reset explicitly here.
  1087. */
  1088. if (sensor->platform_data->i2c_addr_alt)
  1089. smiapp_write(sensor,
  1090. SMIAPP_REG_U8_SOFTWARE_RESET,
  1091. SMIAPP_SOFTWARE_RESET);
  1092. if (gpio_is_valid(sensor->platform_data->xshutdown))
  1093. gpio_set_value(sensor->platform_data->xshutdown, 0);
  1094. if (sensor->platform_data->set_xclk)
  1095. sensor->platform_data->set_xclk(&sensor->src->sd, 0);
  1096. else
  1097. clk_disable_unprepare(sensor->ext_clk);
  1098. usleep_range(5000, 5000);
  1099. regulator_disable(sensor->vana);
  1100. sensor->streaming = false;
  1101. }
  1102. static int smiapp_set_power(struct v4l2_subdev *subdev, int on)
  1103. {
  1104. struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
  1105. int ret = 0;
  1106. mutex_lock(&sensor->power_mutex);
  1107. if (on && !sensor->power_count) {
  1108. /* Power on and perform initialisation. */
  1109. ret = smiapp_power_on(sensor);
  1110. if (ret < 0)
  1111. goto out;
  1112. } else if (!on && sensor->power_count == 1) {
  1113. smiapp_power_off(sensor);
  1114. }
  1115. /* Update the power count. */
  1116. sensor->power_count += on ? 1 : -1;
  1117. WARN_ON(sensor->power_count < 0);
  1118. out:
  1119. mutex_unlock(&sensor->power_mutex);
  1120. return ret;
  1121. }
  1122. /* -----------------------------------------------------------------------------
  1123. * Video stream management
  1124. */
  1125. static int smiapp_start_streaming(struct smiapp_sensor *sensor)
  1126. {
  1127. struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
  1128. int rval;
  1129. mutex_lock(&sensor->mutex);
  1130. rval = smiapp_write(sensor, SMIAPP_REG_U16_CSI_DATA_FORMAT,
  1131. (sensor->csi_format->width << 8) |
  1132. sensor->csi_format->compressed);
  1133. if (rval)
  1134. goto out;
  1135. rval = smiapp_pll_configure(sensor);
  1136. if (rval)
  1137. goto out;
  1138. /* Analog crop start coordinates */
  1139. rval = smiapp_write(sensor, SMIAPP_REG_U16_X_ADDR_START,
  1140. sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left);
  1141. if (rval < 0)
  1142. goto out;
  1143. rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_ADDR_START,
  1144. sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top);
  1145. if (rval < 0)
  1146. goto out;
  1147. /* Analog crop end coordinates */
  1148. rval = smiapp_write(
  1149. sensor, SMIAPP_REG_U16_X_ADDR_END,
  1150. sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left
  1151. + sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width - 1);
  1152. if (rval < 0)
  1153. goto out;
  1154. rval = smiapp_write(
  1155. sensor, SMIAPP_REG_U16_Y_ADDR_END,
  1156. sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top
  1157. + sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height - 1);
  1158. if (rval < 0)
  1159. goto out;
  1160. /*
  1161. * Output from pixel array, including blanking, is set using
  1162. * controls below. No need to set here.
  1163. */
  1164. /* Digital crop */
  1165. if (sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
  1166. == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
  1167. rval = smiapp_write(
  1168. sensor, SMIAPP_REG_U16_DIGITAL_CROP_X_OFFSET,
  1169. sensor->scaler->crop[SMIAPP_PAD_SINK].left);
  1170. if (rval < 0)
  1171. goto out;
  1172. rval = smiapp_write(
  1173. sensor, SMIAPP_REG_U16_DIGITAL_CROP_Y_OFFSET,
  1174. sensor->scaler->crop[SMIAPP_PAD_SINK].top);
  1175. if (rval < 0)
  1176. goto out;
  1177. rval = smiapp_write(
  1178. sensor, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_WIDTH,
  1179. sensor->scaler->crop[SMIAPP_PAD_SINK].width);
  1180. if (rval < 0)
  1181. goto out;
  1182. rval = smiapp_write(
  1183. sensor, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_HEIGHT,
  1184. sensor->scaler->crop[SMIAPP_PAD_SINK].height);
  1185. if (rval < 0)
  1186. goto out;
  1187. }
  1188. /* Scaling */
  1189. if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
  1190. != SMIAPP_SCALING_CAPABILITY_NONE) {
  1191. rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALING_MODE,
  1192. sensor->scaling_mode);
  1193. if (rval < 0)
  1194. goto out;
  1195. rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALE_M,
  1196. sensor->scale_m);
  1197. if (rval < 0)
  1198. goto out;
  1199. }
  1200. /* Output size from sensor */
  1201. rval = smiapp_write(sensor, SMIAPP_REG_U16_X_OUTPUT_SIZE,
  1202. sensor->src->crop[SMIAPP_PAD_SRC].width);
  1203. if (rval < 0)
  1204. goto out;
  1205. rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_OUTPUT_SIZE,
  1206. sensor->src->crop[SMIAPP_PAD_SRC].height);
  1207. if (rval < 0)
  1208. goto out;
  1209. if ((sensor->flash_capability &
  1210. (SMIAPP_FLASH_MODE_CAPABILITY_SINGLE_STROBE |
  1211. SMIAPP_FLASH_MODE_CAPABILITY_MULTIPLE_STROBE)) &&
  1212. sensor->platform_data->strobe_setup != NULL &&
  1213. sensor->platform_data->strobe_setup->trigger != 0) {
  1214. rval = smiapp_setup_flash_strobe(sensor);
  1215. if (rval)
  1216. goto out;
  1217. }
  1218. rval = smiapp_call_quirk(sensor, pre_streamon);
  1219. if (rval) {
  1220. dev_err(&client->dev, "pre_streamon quirks failed\n");
  1221. goto out;
  1222. }
  1223. rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT,
  1224. SMIAPP_MODE_SELECT_STREAMING);
  1225. out:
  1226. mutex_unlock(&sensor->mutex);
  1227. return rval;
  1228. }
  1229. static int smiapp_stop_streaming(struct smiapp_sensor *sensor)
  1230. {
  1231. struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
  1232. int rval;
  1233. mutex_lock(&sensor->mutex);
  1234. rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT,
  1235. SMIAPP_MODE_SELECT_SOFTWARE_STANDBY);
  1236. if (rval)
  1237. goto out;
  1238. rval = smiapp_call_quirk(sensor, post_streamoff);
  1239. if (rval)
  1240. dev_err(&client->dev, "post_streamoff quirks failed\n");
  1241. out:
  1242. mutex_unlock(&sensor->mutex);
  1243. return rval;
  1244. }
  1245. /* -----------------------------------------------------------------------------
  1246. * V4L2 subdev video operations
  1247. */
  1248. static int smiapp_set_stream(struct v4l2_subdev *subdev, int enable)
  1249. {
  1250. struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
  1251. int rval;
  1252. if (sensor->streaming == enable)
  1253. return 0;
  1254. if (enable) {
  1255. sensor->streaming = true;
  1256. rval = smiapp_start_streaming(sensor);
  1257. if (rval < 0)
  1258. sensor->streaming = false;
  1259. } else {
  1260. rval = smiapp_stop_streaming(sensor);
  1261. sensor->streaming = false;
  1262. }
  1263. return rval;
  1264. }
  1265. static int smiapp_enum_mbus_code(struct v4l2_subdev *subdev,
  1266. struct v4l2_subdev_fh *fh,
  1267. struct v4l2_subdev_mbus_code_enum *code)
  1268. {
  1269. struct i2c_client *client = v4l2_get_subdevdata(subdev);
  1270. struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
  1271. unsigned int i;
  1272. int idx = -1;
  1273. int rval = -EINVAL;
  1274. mutex_lock(&sensor->mutex);
  1275. dev_err(&client->dev, "subdev %s, pad %d, index %d\n",
  1276. subdev->name, code->pad, code->index);
  1277. if (subdev != &sensor->src->sd || code->pad != SMIAPP_PAD_SRC) {
  1278. if (code->index)
  1279. goto out;
  1280. code->code = sensor->internal_csi_format->code;
  1281. rval = 0;
  1282. goto out;
  1283. }
  1284. for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
  1285. if (sensor->mbus_frame_fmts & (1 << i))
  1286. idx++;
  1287. if (idx == code->index) {
  1288. code->code = smiapp_csi_data_formats[i].code;
  1289. dev_err(&client->dev, "found index %d, i %d, code %x\n",
  1290. code->index, i, code->code);
  1291. rval = 0;
  1292. break;
  1293. }
  1294. }
  1295. out:
  1296. mutex_unlock(&sensor->mutex);
  1297. return rval;
  1298. }
  1299. static u32 __smiapp_get_mbus_code(struct v4l2_subdev *subdev,
  1300. unsigned int pad)
  1301. {
  1302. struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
  1303. if (subdev == &sensor->src->sd && pad == SMIAPP_PAD_SRC)
  1304. return sensor->csi_format->code;
  1305. else
  1306. return sensor->internal_csi_format->code;
  1307. }
  1308. static int __smiapp_get_format(struct v4l2_subdev *subdev,
  1309. struct v4l2_subdev_fh *fh,
  1310. struct v4l2_subdev_format *fmt)
  1311. {
  1312. struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
  1313. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  1314. fmt->format = *v4l2_subdev_get_try_format(fh, fmt->pad);
  1315. } else {
  1316. struct v4l2_rect *r;
  1317. if (fmt->pad == ssd->source_pad)
  1318. r = &ssd->crop[ssd->source_pad];
  1319. else
  1320. r = &ssd->sink_fmt;
  1321. fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad);
  1322. fmt->format.width = r->width;
  1323. fmt->format.height = r->height;
  1324. fmt->format.field = V4L2_FIELD_NONE;
  1325. }
  1326. return 0;
  1327. }
  1328. static int smiapp_get_format(struct v4l2_subdev *subdev,
  1329. struct v4l2_subdev_fh *fh,
  1330. struct v4l2_subdev_format *fmt)
  1331. {
  1332. struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
  1333. int rval;
  1334. mutex_lock(&sensor->mutex);
  1335. rval = __smiapp_get_format(subdev, fh, fmt);
  1336. mutex_unlock(&sensor->mutex);
  1337. return rval;
  1338. }
  1339. static void smiapp_get_crop_compose(struct v4l2_subdev *subdev,
  1340. struct v4l2_subdev_fh *fh,
  1341. struct v4l2_rect **crops,
  1342. struct v4l2_rect **comps, int which)
  1343. {
  1344. struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
  1345. unsigned int i;
  1346. if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  1347. if (crops)
  1348. for (i = 0; i < subdev->entity.num_pads; i++)
  1349. crops[i] = &ssd->crop[i];
  1350. if (comps)
  1351. *comps = &ssd->compose;
  1352. } else {
  1353. if (crops) {
  1354. for (i = 0; i < subdev->entity.num_pads; i++) {
  1355. crops[i] = v4l2_subdev_get_try_crop(fh, i);
  1356. BUG_ON(!crops[i]);
  1357. }
  1358. }
  1359. if (comps) {
  1360. *comps = v4l2_subdev_get_try_compose(fh,
  1361. SMIAPP_PAD_SINK);
  1362. BUG_ON(!*comps);
  1363. }
  1364. }
  1365. }
  1366. /* Changes require propagation only on sink pad. */
  1367. static void smiapp_propagate(struct v4l2_subdev *subdev,
  1368. struct v4l2_subdev_fh *fh, int which,
  1369. int target)
  1370. {
  1371. struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
  1372. struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
  1373. struct v4l2_rect *comp, *crops[SMIAPP_PADS];
  1374. smiapp_get_crop_compose(subdev, fh, crops, &comp, which);
  1375. switch (target) {
  1376. case V4L2_SEL_TGT_CROP:
  1377. comp->width = crops[SMIAPP_PAD_SINK]->width;
  1378. comp->height = crops[SMIAPP_PAD_SINK]->height;
  1379. if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  1380. if (ssd == sensor->scaler) {
  1381. sensor->scale_m =
  1382. sensor->limits[
  1383. SMIAPP_LIMIT_SCALER_N_MIN];
  1384. sensor->scaling_mode =
  1385. SMIAPP_SCALING_MODE_NONE;
  1386. } else if (ssd == sensor->binner) {
  1387. sensor->binning_horizontal = 1;
  1388. sensor->binning_vertical = 1;
  1389. }
  1390. }
  1391. /* Fall through */
  1392. case V4L2_SEL_TGT_COMPOSE:
  1393. *crops[SMIAPP_PAD_SRC] = *comp;
  1394. break;
  1395. default:
  1396. BUG();
  1397. }
  1398. }
  1399. static const struct smiapp_csi_data_format
  1400. *smiapp_validate_csi_data_format(struct smiapp_sensor *sensor, u32 code)
  1401. {
  1402. const struct smiapp_csi_data_format *csi_format = sensor->csi_format;
  1403. unsigned int i;
  1404. for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
  1405. if (sensor->mbus_frame_fmts & (1 << i)
  1406. && smiapp_csi_data_formats[i].code == code)
  1407. return &smiapp_csi_data_formats[i];
  1408. }
  1409. return csi_format;
  1410. }
  1411. static int smiapp_set_format(struct v4l2_subdev *subdev,
  1412. struct v4l2_subdev_fh *fh,
  1413. struct v4l2_subdev_format *fmt)
  1414. {
  1415. struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
  1416. struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
  1417. struct v4l2_rect *crops[SMIAPP_PADS];
  1418. mutex_lock(&sensor->mutex);
  1419. /*
  1420. * Media bus code is changeable on src subdev's source pad. On
  1421. * other source pads we just get format here.
  1422. */
  1423. if (fmt->pad == ssd->source_pad) {
  1424. u32 code = fmt->format.code;
  1425. int rval = __smiapp_get_format(subdev, fh, fmt);
  1426. bool range_changed = false;
  1427. unsigned int i;
  1428. if (!rval && subdev == &sensor->src->sd) {
  1429. const struct smiapp_csi_data_format *csi_format =
  1430. smiapp_validate_csi_data_format(sensor, code);
  1431. if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  1432. if (csi_format->width !=
  1433. sensor->csi_format->width)
  1434. range_changed = true;
  1435. sensor->csi_format = csi_format;
  1436. }
  1437. fmt->format.code = csi_format->code;
  1438. }
  1439. mutex_unlock(&sensor->mutex);
  1440. if (rval || !range_changed)
  1441. return rval;
  1442. for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++)
  1443. v4l2_ctrl_modify_range(
  1444. sensor->test_data[i],
  1445. 0, (1 << sensor->csi_format->width) - 1, 1, 0);
  1446. return 0;
  1447. }
  1448. /* Sink pad. Width and height are changeable here. */
  1449. fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad);
  1450. fmt->format.width &= ~1;
  1451. fmt->format.height &= ~1;
  1452. fmt->format.field = V4L2_FIELD_NONE;
  1453. fmt->format.width =
  1454. clamp(fmt->format.width,
  1455. sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE],
  1456. sensor->limits[SMIAPP_LIMIT_MAX_X_OUTPUT_SIZE]);
  1457. fmt->format.height =
  1458. clamp(fmt->format.height,
  1459. sensor->limits[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE],
  1460. sensor->limits[SMIAPP_LIMIT_MAX_Y_OUTPUT_SIZE]);
  1461. smiapp_get_crop_compose(subdev, fh, crops, NULL, fmt->which);
  1462. crops[ssd->sink_pad]->left = 0;
  1463. crops[ssd->sink_pad]->top = 0;
  1464. crops[ssd->sink_pad]->width = fmt->format.width;
  1465. crops[ssd->sink_pad]->height = fmt->format.height;
  1466. if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  1467. ssd->sink_fmt = *crops[ssd->sink_pad];
  1468. smiapp_propagate(subdev, fh, fmt->which,
  1469. V4L2_SEL_TGT_CROP);
  1470. mutex_unlock(&sensor->mutex);
  1471. return 0;
  1472. }
  1473. /*
  1474. * Calculate goodness of scaled image size compared to expected image
  1475. * size and flags provided.
  1476. */
  1477. #define SCALING_GOODNESS 100000
  1478. #define SCALING_GOODNESS_EXTREME 100000000
  1479. static int scaling_goodness(struct v4l2_subdev *subdev, int w, int ask_w,
  1480. int h, int ask_h, u32 flags)
  1481. {
  1482. struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
  1483. struct i2c_client *client = v4l2_get_subdevdata(subdev);
  1484. int val = 0;
  1485. w &= ~1;
  1486. ask_w &= ~1;
  1487. h &= ~1;
  1488. ask_h &= ~1;
  1489. if (flags & V4L2_SEL_FLAG_GE) {
  1490. if (w < ask_w)
  1491. val -= SCALING_GOODNESS;
  1492. if (h < ask_h)
  1493. val -= SCALING_GOODNESS;
  1494. }
  1495. if (flags & V4L2_SEL_FLAG_LE) {
  1496. if (w > ask_w)
  1497. val -= SCALING_GOODNESS;
  1498. if (h > ask_h)
  1499. val -= SCALING_GOODNESS;
  1500. }
  1501. val -= abs(w - ask_w);
  1502. val -= abs(h - ask_h);
  1503. if (w < sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE])
  1504. val -= SCALING_GOODNESS_EXTREME;
  1505. dev_dbg(&client->dev, "w %d ask_w %d h %d ask_h %d goodness %d\n",
  1506. w, ask_h, h, ask_h, val);
  1507. return val;
  1508. }
  1509. static void smiapp_set_compose_binner(struct v4l2_subdev *subdev,
  1510. struct v4l2_subdev_fh *fh,
  1511. struct v4l2_subdev_selection *sel,
  1512. struct v4l2_rect **crops,
  1513. struct v4l2_rect *comp)
  1514. {
  1515. struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
  1516. unsigned int i;
  1517. unsigned int binh = 1, binv = 1;
  1518. int best = scaling_goodness(
  1519. subdev,
  1520. crops[SMIAPP_PAD_SINK]->width, sel->r.width,
  1521. crops[SMIAPP_PAD_SINK]->height, sel->r.height, sel->flags);
  1522. for (i = 0; i < sensor->nbinning_subtypes; i++) {
  1523. int this = scaling_goodness(
  1524. subdev,
  1525. crops[SMIAPP_PAD_SINK]->width
  1526. / sensor->binning_subtypes[i].horizontal,
  1527. sel->r.width,
  1528. crops[SMIAPP_PAD_SINK]->height
  1529. / sensor->binning_subtypes[i].vertical,
  1530. sel->r.height, sel->flags);
  1531. if (this > best) {
  1532. binh = sensor->binning_subtypes[i].horizontal;
  1533. binv = sensor->binning_subtypes[i].vertical;
  1534. best = this;
  1535. }
  1536. }
  1537. if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  1538. sensor->binning_vertical = binv;
  1539. sensor->binning_horizontal = binh;
  1540. }
  1541. sel->r.width = (crops[SMIAPP_PAD_SINK]->width / binh) & ~1;
  1542. sel->r.height = (crops[SMIAPP_PAD_SINK]->height / binv) & ~1;
  1543. }
  1544. /*
  1545. * Calculate best scaling ratio and mode for given output resolution.
  1546. *
  1547. * Try all of these: horizontal ratio, vertical ratio and smallest
  1548. * size possible (horizontally).
  1549. *
  1550. * Also try whether horizontal scaler or full scaler gives a better
  1551. * result.
  1552. */
  1553. static void smiapp_set_compose_scaler(struct v4l2_subdev *subdev,
  1554. struct v4l2_subdev_fh *fh,
  1555. struct v4l2_subdev_selection *sel,
  1556. struct v4l2_rect **crops,
  1557. struct v4l2_rect *comp)
  1558. {
  1559. struct i2c_client *client = v4l2_get_subdevdata(subdev);
  1560. struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
  1561. u32 min, max, a, b, max_m;
  1562. u32 scale_m = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
  1563. int mode = SMIAPP_SCALING_MODE_HORIZONTAL;
  1564. u32 try[4];
  1565. u32 ntry = 0;
  1566. unsigned int i;
  1567. int best = INT_MIN;
  1568. sel->r.width = min_t(unsigned int, sel->r.width,
  1569. crops[SMIAPP_PAD_SINK]->width);
  1570. sel->r.height = min_t(unsigned int, sel->r.height,
  1571. crops[SMIAPP_PAD_SINK]->height);
  1572. a = crops[SMIAPP_PAD_SINK]->width
  1573. * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] / sel->r.width;
  1574. b = crops[SMIAPP_PAD_SINK]->height
  1575. * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] / sel->r.height;
  1576. max_m = crops[SMIAPP_PAD_SINK]->width
  1577. * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN]
  1578. / sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE];
  1579. a = clamp(a, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN],
  1580. sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX]);
  1581. b = clamp(b, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN],
  1582. sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX]);
  1583. max_m = clamp(max_m, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN],
  1584. sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX]);
  1585. dev_dbg(&client->dev, "scaling: a %d b %d max_m %d\n", a, b, max_m);
  1586. min = min(max_m, min(a, b));
  1587. max = min(max_m, max(a, b));
  1588. try[ntry] = min;
  1589. ntry++;
  1590. if (min != max) {
  1591. try[ntry] = max;
  1592. ntry++;
  1593. }
  1594. if (max != max_m) {
  1595. try[ntry] = min + 1;
  1596. ntry++;
  1597. if (min != max) {
  1598. try[ntry] = max + 1;
  1599. ntry++;
  1600. }
  1601. }
  1602. for (i = 0; i < ntry; i++) {
  1603. int this = scaling_goodness(
  1604. subdev,
  1605. crops[SMIAPP_PAD_SINK]->width
  1606. / try[i]
  1607. * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
  1608. sel->r.width,
  1609. crops[SMIAPP_PAD_SINK]->height,
  1610. sel->r.height,
  1611. sel->flags);
  1612. dev_dbg(&client->dev, "trying factor %d (%d)\n", try[i], i);
  1613. if (this > best) {
  1614. scale_m = try[i];
  1615. mode = SMIAPP_SCALING_MODE_HORIZONTAL;
  1616. best = this;
  1617. }
  1618. if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
  1619. == SMIAPP_SCALING_CAPABILITY_HORIZONTAL)
  1620. continue;
  1621. this = scaling_goodness(
  1622. subdev, crops[SMIAPP_PAD_SINK]->width
  1623. / try[i]
  1624. * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
  1625. sel->r.width,
  1626. crops[SMIAPP_PAD_SINK]->height
  1627. / try[i]
  1628. * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
  1629. sel->r.height,
  1630. sel->flags);
  1631. if (this > best) {
  1632. scale_m = try[i];
  1633. mode = SMIAPP_SCALING_MODE_BOTH;
  1634. best = this;
  1635. }
  1636. }
  1637. sel->r.width =
  1638. (crops[SMIAPP_PAD_SINK]->width
  1639. / scale_m
  1640. * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN]) & ~1;
  1641. if (mode == SMIAPP_SCALING_MODE_BOTH)
  1642. sel->r.height =
  1643. (crops[SMIAPP_PAD_SINK]->height
  1644. / scale_m
  1645. * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN])
  1646. & ~1;
  1647. else
  1648. sel->r.height = crops[SMIAPP_PAD_SINK]->height;
  1649. if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  1650. sensor->scale_m = scale_m;
  1651. sensor->scaling_mode = mode;
  1652. }
  1653. }
  1654. /* We're only called on source pads. This function sets scaling. */
  1655. static int smiapp_set_compose(struct v4l2_subdev *subdev,
  1656. struct v4l2_subdev_fh *fh,
  1657. struct v4l2_subdev_selection *sel)
  1658. {
  1659. struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
  1660. struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
  1661. struct v4l2_rect *comp, *crops[SMIAPP_PADS];
  1662. smiapp_get_crop_compose(subdev, fh, crops, &comp, sel->which);
  1663. sel->r.top = 0;
  1664. sel->r.left = 0;
  1665. if (ssd == sensor->binner)
  1666. smiapp_set_compose_binner(subdev, fh, sel, crops, comp);
  1667. else
  1668. smiapp_set_compose_scaler(subdev, fh, sel, crops, comp);
  1669. *comp = sel->r;
  1670. smiapp_propagate(subdev, fh, sel->which,
  1671. V4L2_SEL_TGT_COMPOSE);
  1672. if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  1673. return smiapp_update_mode(sensor);
  1674. return 0;
  1675. }
  1676. static int __smiapp_sel_supported(struct v4l2_subdev *subdev,
  1677. struct v4l2_subdev_selection *sel)
  1678. {
  1679. struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
  1680. struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
  1681. /* We only implement crop in three places. */
  1682. switch (sel->target) {
  1683. case V4L2_SEL_TGT_CROP:
  1684. case V4L2_SEL_TGT_CROP_BOUNDS:
  1685. if (ssd == sensor->pixel_array
  1686. && sel->pad == SMIAPP_PA_PAD_SRC)
  1687. return 0;
  1688. if (ssd == sensor->src
  1689. && sel->pad == SMIAPP_PAD_SRC)
  1690. return 0;
  1691. if (ssd == sensor->scaler
  1692. && sel->pad == SMIAPP_PAD_SINK
  1693. && sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
  1694. == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP)
  1695. return 0;
  1696. return -EINVAL;
  1697. case V4L2_SEL_TGT_COMPOSE:
  1698. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  1699. if (sel->pad == ssd->source_pad)
  1700. return -EINVAL;
  1701. if (ssd == sensor->binner)
  1702. return 0;
  1703. if (ssd == sensor->scaler
  1704. && sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
  1705. != SMIAPP_SCALING_CAPABILITY_NONE)
  1706. return 0;
  1707. /* Fall through */
  1708. default:
  1709. return -EINVAL;
  1710. }
  1711. }
  1712. static int smiapp_set_crop(struct v4l2_subdev *subdev,
  1713. struct v4l2_subdev_fh *fh,
  1714. struct v4l2_subdev_selection *sel)
  1715. {
  1716. struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
  1717. struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
  1718. struct v4l2_rect *src_size, *crops[SMIAPP_PADS];
  1719. struct v4l2_rect _r;
  1720. smiapp_get_crop_compose(subdev, fh, crops, NULL, sel->which);
  1721. if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  1722. if (sel->pad == ssd->sink_pad)
  1723. src_size = &ssd->sink_fmt;
  1724. else
  1725. src_size = &ssd->compose;
  1726. } else {
  1727. if (sel->pad == ssd->sink_pad) {
  1728. _r.left = 0;
  1729. _r.top = 0;
  1730. _r.width = v4l2_subdev_get_try_format(fh, sel->pad)
  1731. ->width;
  1732. _r.height = v4l2_subdev_get_try_format(fh, sel->pad)
  1733. ->height;
  1734. src_size = &_r;
  1735. } else {
  1736. src_size =
  1737. v4l2_subdev_get_try_compose(
  1738. fh, ssd->sink_pad);
  1739. }
  1740. }
  1741. if (ssd == sensor->src && sel->pad == SMIAPP_PAD_SRC) {
  1742. sel->r.left = 0;
  1743. sel->r.top = 0;
  1744. }
  1745. sel->r.width = min(sel->r.width, src_size->width);
  1746. sel->r.height = min(sel->r.height, src_size->height);
  1747. sel->r.left = min_t(int, sel->r.left, src_size->width - sel->r.width);
  1748. sel->r.top = min_t(int, sel->r.top, src_size->height - sel->r.height);
  1749. *crops[sel->pad] = sel->r;
  1750. if (ssd != sensor->pixel_array && sel->pad == SMIAPP_PAD_SINK)
  1751. smiapp_propagate(subdev, fh, sel->which,
  1752. V4L2_SEL_TGT_CROP);
  1753. return 0;
  1754. }
  1755. static int __smiapp_get_selection(struct v4l2_subdev *subdev,
  1756. struct v4l2_subdev_fh *fh,
  1757. struct v4l2_subdev_selection *sel)
  1758. {
  1759. struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
  1760. struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
  1761. struct v4l2_rect *comp, *crops[SMIAPP_PADS];
  1762. struct v4l2_rect sink_fmt;
  1763. int ret;
  1764. ret = __smiapp_sel_supported(subdev, sel);
  1765. if (ret)
  1766. return ret;
  1767. smiapp_get_crop_compose(subdev, fh, crops, &comp, sel->which);
  1768. if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
  1769. sink_fmt = ssd->sink_fmt;
  1770. } else {
  1771. struct v4l2_mbus_framefmt *fmt =
  1772. v4l2_subdev_get_try_format(fh, ssd->sink_pad);
  1773. sink_fmt.left = 0;
  1774. sink_fmt.top = 0;
  1775. sink_fmt.width = fmt->width;
  1776. sink_fmt.height = fmt->height;
  1777. }
  1778. switch (sel->target) {
  1779. case V4L2_SEL_TGT_CROP_BOUNDS:
  1780. if (ssd == sensor->pixel_array) {
  1781. sel->r.width =
  1782. sensor->limits[SMIAPP_LIMIT_X_ADDR_MAX] + 1;
  1783. sel->r.height =
  1784. sensor->limits[SMIAPP_LIMIT_Y_ADDR_MAX] + 1;
  1785. } else if (sel->pad == ssd->sink_pad) {
  1786. sel->r = sink_fmt;
  1787. } else {
  1788. sel->r = *comp;
  1789. }
  1790. break;
  1791. case V4L2_SEL_TGT_CROP:
  1792. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  1793. sel->r = *crops[sel->pad];
  1794. break;
  1795. case V4L2_SEL_TGT_COMPOSE:
  1796. sel->r = *comp;
  1797. break;
  1798. }
  1799. return 0;
  1800. }
  1801. static int smiapp_get_selection(struct v4l2_subdev *subdev,
  1802. struct v4l2_subdev_fh *fh,
  1803. struct v4l2_subdev_selection *sel)
  1804. {
  1805. struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
  1806. int rval;
  1807. mutex_lock(&sensor->mutex);
  1808. rval = __smiapp_get_selection(subdev, fh, sel);
  1809. mutex_unlock(&sensor->mutex);
  1810. return rval;
  1811. }
  1812. static int smiapp_set_selection(struct v4l2_subdev *subdev,
  1813. struct v4l2_subdev_fh *fh,
  1814. struct v4l2_subdev_selection *sel)
  1815. {
  1816. struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
  1817. int ret;
  1818. ret = __smiapp_sel_supported(subdev, sel);
  1819. if (ret)
  1820. return ret;
  1821. mutex_lock(&sensor->mutex);
  1822. sel->r.left = max(0, sel->r.left & ~1);
  1823. sel->r.top = max(0, sel->r.top & ~1);
  1824. sel->r.width = SMIAPP_ALIGN_DIM(sel->r.width, sel->flags);
  1825. sel->r.height = SMIAPP_ALIGN_DIM(sel->r.height, sel->flags);
  1826. sel->r.width = max_t(unsigned int,
  1827. sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE],
  1828. sel->r.width);
  1829. sel->r.height = max_t(unsigned int,
  1830. sensor->limits[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE],
  1831. sel->r.height);
  1832. switch (sel->target) {
  1833. case V4L2_SEL_TGT_CROP:
  1834. ret = smiapp_set_crop(subdev, fh, sel);
  1835. break;
  1836. case V4L2_SEL_TGT_COMPOSE:
  1837. ret = smiapp_set_compose(subdev, fh, sel);
  1838. break;
  1839. default:
  1840. BUG();
  1841. }
  1842. mutex_unlock(&sensor->mutex);
  1843. return ret;
  1844. }
  1845. static int smiapp_get_skip_frames(struct v4l2_subdev *subdev, u32 *frames)
  1846. {
  1847. struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
  1848. *frames = sensor->frame_skip;
  1849. return 0;
  1850. }
  1851. /* -----------------------------------------------------------------------------
  1852. * sysfs attributes
  1853. */
  1854. static ssize_t
  1855. smiapp_sysfs_nvm_read(struct device *dev, struct device_attribute *attr,
  1856. char *buf)
  1857. {
  1858. struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
  1859. struct i2c_client *client = v4l2_get_subdevdata(subdev);
  1860. struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
  1861. unsigned int nbytes;
  1862. if (!sensor->dev_init_done)
  1863. return -EBUSY;
  1864. if (!sensor->nvm_size) {
  1865. /* NVM not read yet - read it now */
  1866. sensor->nvm_size = sensor->platform_data->nvm_size;
  1867. if (smiapp_set_power(subdev, 1) < 0)
  1868. return -ENODEV;
  1869. if (smiapp_read_nvm(sensor, sensor->nvm)) {
  1870. dev_err(&client->dev, "nvm read failed\n");
  1871. return -ENODEV;
  1872. }
  1873. smiapp_set_power(subdev, 0);
  1874. }
  1875. /*
  1876. * NVM is still way below a PAGE_SIZE, so we can safely
  1877. * assume this for now.
  1878. */
  1879. nbytes = min_t(unsigned int, sensor->nvm_size, PAGE_SIZE);
  1880. memcpy(buf, sensor->nvm, nbytes);
  1881. return nbytes;
  1882. }
  1883. static DEVICE_ATTR(nvm, S_IRUGO, smiapp_sysfs_nvm_read, NULL);
  1884. static ssize_t
  1885. smiapp_sysfs_ident_read(struct device *dev, struct device_attribute *attr,
  1886. char *buf)
  1887. {
  1888. struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
  1889. struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
  1890. struct smiapp_module_info *minfo = &sensor->minfo;
  1891. return snprintf(buf, PAGE_SIZE, "%2.2x%4.4x%2.2x\n",
  1892. minfo->manufacturer_id, minfo->model_id,
  1893. minfo->revision_number_major) + 1;
  1894. }
  1895. static DEVICE_ATTR(ident, S_IRUGO, smiapp_sysfs_ident_read, NULL);
  1896. /* -----------------------------------------------------------------------------
  1897. * V4L2 subdev core operations
  1898. */
  1899. static int smiapp_identify_module(struct v4l2_subdev *subdev)
  1900. {
  1901. struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
  1902. struct i2c_client *client = v4l2_get_subdevdata(subdev);
  1903. struct smiapp_module_info *minfo = &sensor->minfo;
  1904. unsigned int i;
  1905. int rval = 0;
  1906. minfo->name = SMIAPP_NAME;
  1907. /* Module info */
  1908. rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MANUFACTURER_ID,
  1909. &minfo->manufacturer_id);
  1910. if (!rval)
  1911. rval = smiapp_read_8only(sensor, SMIAPP_REG_U16_MODEL_ID,
  1912. &minfo->model_id);
  1913. if (!rval)
  1914. rval = smiapp_read_8only(sensor,
  1915. SMIAPP_REG_U8_REVISION_NUMBER_MAJOR,
  1916. &minfo->revision_number_major);
  1917. if (!rval)
  1918. rval = smiapp_read_8only(sensor,
  1919. SMIAPP_REG_U8_REVISION_NUMBER_MINOR,
  1920. &minfo->revision_number_minor);
  1921. if (!rval)
  1922. rval = smiapp_read_8only(sensor,
  1923. SMIAPP_REG_U8_MODULE_DATE_YEAR,
  1924. &minfo->module_year);
  1925. if (!rval)
  1926. rval = smiapp_read_8only(sensor,
  1927. SMIAPP_REG_U8_MODULE_DATE_MONTH,
  1928. &minfo->module_month);
  1929. if (!rval)
  1930. rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MODULE_DATE_DAY,
  1931. &minfo->module_day);
  1932. /* Sensor info */
  1933. if (!rval)
  1934. rval = smiapp_read_8only(sensor,
  1935. SMIAPP_REG_U8_SENSOR_MANUFACTURER_ID,
  1936. &minfo->sensor_manufacturer_id);
  1937. if (!rval)
  1938. rval = smiapp_read_8only(sensor,
  1939. SMIAPP_REG_U16_SENSOR_MODEL_ID,
  1940. &minfo->sensor_model_id);
  1941. if (!rval)
  1942. rval = smiapp_read_8only(sensor,
  1943. SMIAPP_REG_U8_SENSOR_REVISION_NUMBER,
  1944. &minfo->sensor_revision_number);
  1945. if (!rval)
  1946. rval = smiapp_read_8only(sensor,
  1947. SMIAPP_REG_U8_SENSOR_FIRMWARE_VERSION,
  1948. &minfo->sensor_firmware_version);
  1949. /* SMIA */
  1950. if (!rval)
  1951. rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIA_VERSION,
  1952. &minfo->smia_version);
  1953. if (!rval)
  1954. rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIAPP_VERSION,
  1955. &minfo->smiapp_version);
  1956. if (rval) {
  1957. dev_err(&client->dev, "sensor detection failed\n");
  1958. return -ENODEV;
  1959. }
  1960. dev_dbg(&client->dev, "module 0x%2.2x-0x%4.4x\n",
  1961. minfo->manufacturer_id, minfo->model_id);
  1962. dev_dbg(&client->dev,
  1963. "module revision 0x%2.2x-0x%2.2x date %2.2d-%2.2d-%2.2d\n",
  1964. minfo->revision_number_major, minfo->revision_number_minor,
  1965. minfo->module_year, minfo->module_month, minfo->module_day);
  1966. dev_dbg(&client->dev, "sensor 0x%2.2x-0x%4.4x\n",
  1967. minfo->sensor_manufacturer_id, minfo->sensor_model_id);
  1968. dev_dbg(&client->dev,
  1969. "sensor revision 0x%2.2x firmware version 0x%2.2x\n",
  1970. minfo->sensor_revision_number, minfo->sensor_firmware_version);
  1971. dev_dbg(&client->dev, "smia version %2.2d smiapp version %2.2d\n",
  1972. minfo->smia_version, minfo->smiapp_version);
  1973. /*
  1974. * Some modules have bad data in the lvalues below. Hope the
  1975. * rvalues have better stuff. The lvalues are module
  1976. * parameters whereas the rvalues are sensor parameters.
  1977. */
  1978. if (!minfo->manufacturer_id && !minfo->model_id) {
  1979. minfo->manufacturer_id = minfo->sensor_manufacturer_id;
  1980. minfo->model_id = minfo->sensor_model_id;
  1981. minfo->revision_number_major = minfo->sensor_revision_number;
  1982. }
  1983. for (i = 0; i < ARRAY_SIZE(smiapp_module_idents); i++) {
  1984. if (smiapp_module_idents[i].manufacturer_id
  1985. != minfo->manufacturer_id)
  1986. continue;
  1987. if (smiapp_module_idents[i].model_id != minfo->model_id)
  1988. continue;
  1989. if (smiapp_module_idents[i].flags
  1990. & SMIAPP_MODULE_IDENT_FLAG_REV_LE) {
  1991. if (smiapp_module_idents[i].revision_number_major
  1992. < minfo->revision_number_major)
  1993. continue;
  1994. } else {
  1995. if (smiapp_module_idents[i].revision_number_major
  1996. != minfo->revision_number_major)
  1997. continue;
  1998. }
  1999. minfo->name = smiapp_module_idents[i].name;
  2000. minfo->quirk = smiapp_module_idents[i].quirk;
  2001. break;
  2002. }
  2003. if (i >= ARRAY_SIZE(smiapp_module_idents))
  2004. dev_warn(&client->dev,
  2005. "no quirks for this module; let's hope it's fully compliant\n");
  2006. dev_dbg(&client->dev, "the sensor is called %s, ident %2.2x%4.4x%2.2x\n",
  2007. minfo->name, minfo->manufacturer_id, minfo->model_id,
  2008. minfo->revision_number_major);
  2009. strlcpy(subdev->name, sensor->minfo.name, sizeof(subdev->name));
  2010. return 0;
  2011. }
  2012. static const struct v4l2_subdev_ops smiapp_ops;
  2013. static const struct v4l2_subdev_internal_ops smiapp_internal_ops;
  2014. static const struct media_entity_operations smiapp_entity_ops;
  2015. static int smiapp_registered(struct v4l2_subdev *subdev)
  2016. {
  2017. struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
  2018. struct i2c_client *client = v4l2_get_subdevdata(subdev);
  2019. struct smiapp_pll *pll = &sensor->pll;
  2020. struct smiapp_subdev *last = NULL;
  2021. u32 tmp;
  2022. unsigned int i;
  2023. int rval;
  2024. sensor->vana = devm_regulator_get(&client->dev, "vana");
  2025. if (IS_ERR(sensor->vana)) {
  2026. dev_err(&client->dev, "could not get regulator for vana\n");
  2027. return PTR_ERR(sensor->vana);
  2028. }
  2029. if (!sensor->platform_data->set_xclk) {
  2030. sensor->ext_clk = devm_clk_get(&client->dev, "ext_clk");
  2031. if (IS_ERR(sensor->ext_clk)) {
  2032. dev_err(&client->dev, "could not get clock\n");
  2033. return PTR_ERR(sensor->ext_clk);
  2034. }
  2035. rval = clk_set_rate(sensor->ext_clk,
  2036. sensor->platform_data->ext_clk);
  2037. if (rval < 0) {
  2038. dev_err(&client->dev,
  2039. "unable to set clock freq to %u\n",
  2040. sensor->platform_data->ext_clk);
  2041. return rval;
  2042. }
  2043. }
  2044. if (gpio_is_valid(sensor->platform_data->xshutdown)) {
  2045. rval = devm_gpio_request_one(
  2046. &client->dev, sensor->platform_data->xshutdown, 0,
  2047. "SMIA++ xshutdown");
  2048. if (rval < 0) {
  2049. dev_err(&client->dev,
  2050. "unable to acquire reset gpio %d\n",
  2051. sensor->platform_data->xshutdown);
  2052. return rval;
  2053. }
  2054. }
  2055. rval = smiapp_power_on(sensor);
  2056. if (rval)
  2057. return -ENODEV;
  2058. rval = smiapp_identify_module(subdev);
  2059. if (rval) {
  2060. rval = -ENODEV;
  2061. goto out_power_off;
  2062. }
  2063. rval = smiapp_get_all_limits(sensor);
  2064. if (rval) {
  2065. rval = -ENODEV;
  2066. goto out_power_off;
  2067. }
  2068. /*
  2069. * Handle Sensor Module orientation on the board.
  2070. *
  2071. * The application of H-FLIP and V-FLIP on the sensor is modified by
  2072. * the sensor orientation on the board.
  2073. *
  2074. * For SMIAPP_BOARD_SENSOR_ORIENT_180 the default behaviour is to set
  2075. * both H-FLIP and V-FLIP for normal operation which also implies
  2076. * that a set/unset operation for user space HFLIP and VFLIP v4l2
  2077. * controls will need to be internally inverted.
  2078. *
  2079. * Rotation also changes the bayer pattern.
  2080. */
  2081. if (sensor->platform_data->module_board_orient ==
  2082. SMIAPP_MODULE_BOARD_ORIENT_180)
  2083. sensor->hvflip_inv_mask = SMIAPP_IMAGE_ORIENTATION_HFLIP |
  2084. SMIAPP_IMAGE_ORIENTATION_VFLIP;
  2085. rval = smiapp_call_quirk(sensor, limits);
  2086. if (rval) {
  2087. dev_err(&client->dev, "limits quirks failed\n");
  2088. goto out_power_off;
  2089. }
  2090. rval = smiapp_get_mbus_formats(sensor);
  2091. if (rval) {
  2092. rval = -ENODEV;
  2093. goto out_power_off;
  2094. }
  2095. if (sensor->limits[SMIAPP_LIMIT_BINNING_CAPABILITY]) {
  2096. u32 val;
  2097. rval = smiapp_read(sensor,
  2098. SMIAPP_REG_U8_BINNING_SUBTYPES, &val);
  2099. if (rval < 0) {
  2100. rval = -ENODEV;
  2101. goto out_power_off;
  2102. }
  2103. sensor->nbinning_subtypes = min_t(u8, val,
  2104. SMIAPP_BINNING_SUBTYPES);
  2105. for (i = 0; i < sensor->nbinning_subtypes; i++) {
  2106. rval = smiapp_read(
  2107. sensor, SMIAPP_REG_U8_BINNING_TYPE_n(i), &val);
  2108. if (rval < 0) {
  2109. rval = -ENODEV;
  2110. goto out_power_off;
  2111. }
  2112. sensor->binning_subtypes[i] =
  2113. *(struct smiapp_binning_subtype *)&val;
  2114. dev_dbg(&client->dev, "binning %xx%x\n",
  2115. sensor->binning_subtypes[i].horizontal,
  2116. sensor->binning_subtypes[i].vertical);
  2117. }
  2118. }
  2119. sensor->binning_horizontal = 1;
  2120. sensor->binning_vertical = 1;
  2121. if (device_create_file(&client->dev, &dev_attr_ident) != 0) {
  2122. dev_err(&client->dev, "sysfs ident entry creation failed\n");
  2123. rval = -ENOENT;
  2124. goto out_power_off;
  2125. }
  2126. /* SMIA++ NVM initialization - it will be read from the sensor
  2127. * when it is first requested by userspace.
  2128. */
  2129. if (sensor->minfo.smiapp_version && sensor->platform_data->nvm_size) {
  2130. sensor->nvm = devm_kzalloc(&client->dev,
  2131. sensor->platform_data->nvm_size, GFP_KERNEL);
  2132. if (sensor->nvm == NULL) {
  2133. dev_err(&client->dev, "nvm buf allocation failed\n");
  2134. rval = -ENOMEM;
  2135. goto out_ident_release;
  2136. }
  2137. if (device_create_file(&client->dev, &dev_attr_nvm) != 0) {
  2138. dev_err(&client->dev, "sysfs nvm entry failed\n");
  2139. rval = -EBUSY;
  2140. goto out_ident_release;
  2141. }
  2142. }
  2143. /* We consider this as profile 0 sensor if any of these are zero. */
  2144. if (!sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV] ||
  2145. !sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV] ||
  2146. !sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV] ||
  2147. !sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV]) {
  2148. sensor->minfo.smiapp_profile = SMIAPP_PROFILE_0;
  2149. } else if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
  2150. != SMIAPP_SCALING_CAPABILITY_NONE) {
  2151. if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
  2152. == SMIAPP_SCALING_CAPABILITY_HORIZONTAL)
  2153. sensor->minfo.smiapp_profile = SMIAPP_PROFILE_1;
  2154. else
  2155. sensor->minfo.smiapp_profile = SMIAPP_PROFILE_2;
  2156. sensor->scaler = &sensor->ssds[sensor->ssds_used];
  2157. sensor->ssds_used++;
  2158. } else if (sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
  2159. == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
  2160. sensor->scaler = &sensor->ssds[sensor->ssds_used];
  2161. sensor->ssds_used++;
  2162. }
  2163. sensor->binner = &sensor->ssds[sensor->ssds_used];
  2164. sensor->ssds_used++;
  2165. sensor->pixel_array = &sensor->ssds[sensor->ssds_used];
  2166. sensor->ssds_used++;
  2167. sensor->scale_m = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
  2168. for (i = 0; i < SMIAPP_SUBDEVS; i++) {
  2169. struct {
  2170. struct smiapp_subdev *ssd;
  2171. char *name;
  2172. } const __this[] = {
  2173. { sensor->scaler, "scaler", },
  2174. { sensor->binner, "binner", },
  2175. { sensor->pixel_array, "pixel array", },
  2176. }, *_this = &__this[i];
  2177. struct smiapp_subdev *this = _this->ssd;
  2178. if (!this)
  2179. continue;
  2180. if (this != sensor->src)
  2181. v4l2_subdev_init(&this->sd, &smiapp_ops);
  2182. this->sensor = sensor;
  2183. if (this == sensor->pixel_array) {
  2184. this->npads = 1;
  2185. } else {
  2186. this->npads = 2;
  2187. this->source_pad = 1;
  2188. }
  2189. snprintf(this->sd.name,
  2190. sizeof(this->sd.name), "%s %s %d-%4.4x",
  2191. sensor->minfo.name, _this->name,
  2192. i2c_adapter_id(client->adapter), client->addr);
  2193. this->sink_fmt.width =
  2194. sensor->limits[SMIAPP_LIMIT_X_ADDR_MAX] + 1;
  2195. this->sink_fmt.height =
  2196. sensor->limits[SMIAPP_LIMIT_Y_ADDR_MAX] + 1;
  2197. this->compose.width = this->sink_fmt.width;
  2198. this->compose.height = this->sink_fmt.height;
  2199. this->crop[this->source_pad] = this->compose;
  2200. this->pads[this->source_pad].flags = MEDIA_PAD_FL_SOURCE;
  2201. if (this != sensor->pixel_array) {
  2202. this->crop[this->sink_pad] = this->compose;
  2203. this->pads[this->sink_pad].flags = MEDIA_PAD_FL_SINK;
  2204. }
  2205. this->sd.entity.ops = &smiapp_entity_ops;
  2206. if (last == NULL) {
  2207. last = this;
  2208. continue;
  2209. }
  2210. this->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  2211. this->sd.internal_ops = &smiapp_internal_ops;
  2212. this->sd.owner = THIS_MODULE;
  2213. v4l2_set_subdevdata(&this->sd, client);
  2214. rval = media_entity_init(&this->sd.entity,
  2215. this->npads, this->pads, 0);
  2216. if (rval) {
  2217. dev_err(&client->dev,
  2218. "media_entity_init failed\n");
  2219. goto out_nvm_release;
  2220. }
  2221. rval = media_entity_create_link(&this->sd.entity,
  2222. this->source_pad,
  2223. &last->sd.entity,
  2224. last->sink_pad,
  2225. MEDIA_LNK_FL_ENABLED |
  2226. MEDIA_LNK_FL_IMMUTABLE);
  2227. if (rval) {
  2228. dev_err(&client->dev,
  2229. "media_entity_create_link failed\n");
  2230. goto out_nvm_release;
  2231. }
  2232. rval = v4l2_device_register_subdev(sensor->src->sd.v4l2_dev,
  2233. &this->sd);
  2234. if (rval) {
  2235. dev_err(&client->dev,
  2236. "v4l2_device_register_subdev failed\n");
  2237. goto out_nvm_release;
  2238. }
  2239. last = this;
  2240. }
  2241. dev_dbg(&client->dev, "profile %d\n", sensor->minfo.smiapp_profile);
  2242. sensor->pixel_array->sd.entity.type = MEDIA_ENT_T_V4L2_SUBDEV_SENSOR;
  2243. /* final steps */
  2244. smiapp_read_frame_fmt(sensor);
  2245. rval = smiapp_init_controls(sensor);
  2246. if (rval < 0)
  2247. goto out_nvm_release;
  2248. /* prepare PLL configuration input values */
  2249. pll->bus_type = SMIAPP_PLL_BUS_TYPE_CSI2;
  2250. pll->csi2.lanes = sensor->platform_data->lanes;
  2251. pll->ext_clk_freq_hz = sensor->platform_data->ext_clk;
  2252. pll->flags = smiapp_call_quirk(sensor, pll_flags);
  2253. /* Profile 0 sensors have no separate OP clock branch. */
  2254. if (sensor->minfo.smiapp_profile == SMIAPP_PROFILE_0)
  2255. pll->flags |= SMIAPP_PLL_FLAG_NO_OP_CLOCKS;
  2256. pll->scale_n = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
  2257. rval = smiapp_update_mode(sensor);
  2258. if (rval) {
  2259. dev_err(&client->dev, "update mode failed\n");
  2260. goto out_nvm_release;
  2261. }
  2262. sensor->streaming = false;
  2263. sensor->dev_init_done = true;
  2264. /* check flash capability */
  2265. rval = smiapp_read(sensor, SMIAPP_REG_U8_FLASH_MODE_CAPABILITY, &tmp);
  2266. sensor->flash_capability = tmp;
  2267. if (rval)
  2268. goto out_nvm_release;
  2269. smiapp_power_off(sensor);
  2270. return 0;
  2271. out_nvm_release:
  2272. device_remove_file(&client->dev, &dev_attr_nvm);
  2273. out_ident_release:
  2274. device_remove_file(&client->dev, &dev_attr_ident);
  2275. out_power_off:
  2276. smiapp_power_off(sensor);
  2277. return rval;
  2278. }
  2279. static int smiapp_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  2280. {
  2281. struct smiapp_subdev *ssd = to_smiapp_subdev(sd);
  2282. struct smiapp_sensor *sensor = ssd->sensor;
  2283. u32 mbus_code =
  2284. smiapp_csi_data_formats[smiapp_pixel_order(sensor)].code;
  2285. unsigned int i;
  2286. mutex_lock(&sensor->mutex);
  2287. for (i = 0; i < ssd->npads; i++) {
  2288. struct v4l2_mbus_framefmt *try_fmt =
  2289. v4l2_subdev_get_try_format(fh, i);
  2290. struct v4l2_rect *try_crop = v4l2_subdev_get_try_crop(fh, i);
  2291. struct v4l2_rect *try_comp;
  2292. try_fmt->width = sensor->limits[SMIAPP_LIMIT_X_ADDR_MAX] + 1;
  2293. try_fmt->height = sensor->limits[SMIAPP_LIMIT_Y_ADDR_MAX] + 1;
  2294. try_fmt->code = mbus_code;
  2295. try_fmt->field = V4L2_FIELD_NONE;
  2296. try_crop->top = 0;
  2297. try_crop->left = 0;
  2298. try_crop->width = try_fmt->width;
  2299. try_crop->height = try_fmt->height;
  2300. if (ssd != sensor->pixel_array)
  2301. continue;
  2302. try_comp = v4l2_subdev_get_try_compose(fh, i);
  2303. *try_comp = *try_crop;
  2304. }
  2305. mutex_unlock(&sensor->mutex);
  2306. return smiapp_set_power(sd, 1);
  2307. }
  2308. static int smiapp_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
  2309. {
  2310. return smiapp_set_power(sd, 0);
  2311. }
  2312. static const struct v4l2_subdev_video_ops smiapp_video_ops = {
  2313. .s_stream = smiapp_set_stream,
  2314. };
  2315. static const struct v4l2_subdev_core_ops smiapp_core_ops = {
  2316. .s_power = smiapp_set_power,
  2317. };
  2318. static const struct v4l2_subdev_pad_ops smiapp_pad_ops = {
  2319. .enum_mbus_code = smiapp_enum_mbus_code,
  2320. .get_fmt = smiapp_get_format,
  2321. .set_fmt = smiapp_set_format,
  2322. .get_selection = smiapp_get_selection,
  2323. .set_selection = smiapp_set_selection,
  2324. };
  2325. static const struct v4l2_subdev_sensor_ops smiapp_sensor_ops = {
  2326. .g_skip_frames = smiapp_get_skip_frames,
  2327. };
  2328. static const struct v4l2_subdev_ops smiapp_ops = {
  2329. .core = &smiapp_core_ops,
  2330. .video = &smiapp_video_ops,
  2331. .pad = &smiapp_pad_ops,
  2332. .sensor = &smiapp_sensor_ops,
  2333. };
  2334. static const struct media_entity_operations smiapp_entity_ops = {
  2335. .link_validate = v4l2_subdev_link_validate,
  2336. };
  2337. static const struct v4l2_subdev_internal_ops smiapp_internal_src_ops = {
  2338. .registered = smiapp_registered,
  2339. .open = smiapp_open,
  2340. .close = smiapp_close,
  2341. };
  2342. static const struct v4l2_subdev_internal_ops smiapp_internal_ops = {
  2343. .open = smiapp_open,
  2344. .close = smiapp_close,
  2345. };
  2346. /* -----------------------------------------------------------------------------
  2347. * I2C Driver
  2348. */
  2349. #ifdef CONFIG_PM
  2350. static int smiapp_suspend(struct device *dev)
  2351. {
  2352. struct i2c_client *client = to_i2c_client(dev);
  2353. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  2354. struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
  2355. bool streaming;
  2356. BUG_ON(mutex_is_locked(&sensor->mutex));
  2357. if (sensor->power_count == 0)
  2358. return 0;
  2359. if (sensor->streaming)
  2360. smiapp_stop_streaming(sensor);
  2361. streaming = sensor->streaming;
  2362. smiapp_power_off(sensor);
  2363. /* save state for resume */
  2364. sensor->streaming = streaming;
  2365. return 0;
  2366. }
  2367. static int smiapp_resume(struct device *dev)
  2368. {
  2369. struct i2c_client *client = to_i2c_client(dev);
  2370. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  2371. struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
  2372. int rval;
  2373. if (sensor->power_count == 0)
  2374. return 0;
  2375. rval = smiapp_power_on(sensor);
  2376. if (rval)
  2377. return rval;
  2378. if (sensor->streaming)
  2379. rval = smiapp_start_streaming(sensor);
  2380. return rval;
  2381. }
  2382. #else
  2383. #define smiapp_suspend NULL
  2384. #define smiapp_resume NULL
  2385. #endif /* CONFIG_PM */
  2386. static int smiapp_probe(struct i2c_client *client,
  2387. const struct i2c_device_id *devid)
  2388. {
  2389. struct smiapp_sensor *sensor;
  2390. if (client->dev.platform_data == NULL)
  2391. return -ENODEV;
  2392. sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL);
  2393. if (sensor == NULL)
  2394. return -ENOMEM;
  2395. sensor->platform_data = client->dev.platform_data;
  2396. mutex_init(&sensor->mutex);
  2397. mutex_init(&sensor->power_mutex);
  2398. sensor->src = &sensor->ssds[sensor->ssds_used];
  2399. v4l2_i2c_subdev_init(&sensor->src->sd, client, &smiapp_ops);
  2400. sensor->src->sd.internal_ops = &smiapp_internal_src_ops;
  2401. sensor->src->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  2402. sensor->src->sensor = sensor;
  2403. sensor->src->pads[0].flags = MEDIA_PAD_FL_SOURCE;
  2404. return media_entity_init(&sensor->src->sd.entity, 2,
  2405. sensor->src->pads, 0);
  2406. }
  2407. static int smiapp_remove(struct i2c_client *client)
  2408. {
  2409. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  2410. struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
  2411. unsigned int i;
  2412. if (sensor->power_count) {
  2413. if (gpio_is_valid(sensor->platform_data->xshutdown))
  2414. gpio_set_value(sensor->platform_data->xshutdown, 0);
  2415. if (sensor->platform_data->set_xclk)
  2416. sensor->platform_data->set_xclk(&sensor->src->sd, 0);
  2417. else
  2418. clk_disable_unprepare(sensor->ext_clk);
  2419. sensor->power_count = 0;
  2420. }
  2421. device_remove_file(&client->dev, &dev_attr_ident);
  2422. if (sensor->nvm)
  2423. device_remove_file(&client->dev, &dev_attr_nvm);
  2424. for (i = 0; i < sensor->ssds_used; i++) {
  2425. v4l2_device_unregister_subdev(&sensor->ssds[i].sd);
  2426. media_entity_cleanup(&sensor->ssds[i].sd.entity);
  2427. }
  2428. smiapp_free_controls(sensor);
  2429. return 0;
  2430. }
  2431. static const struct i2c_device_id smiapp_id_table[] = {
  2432. { SMIAPP_NAME, 0 },
  2433. { },
  2434. };
  2435. MODULE_DEVICE_TABLE(i2c, smiapp_id_table);
  2436. static const struct dev_pm_ops smiapp_pm_ops = {
  2437. .suspend = smiapp_suspend,
  2438. .resume = smiapp_resume,
  2439. };
  2440. static struct i2c_driver smiapp_i2c_driver = {
  2441. .driver = {
  2442. .name = SMIAPP_NAME,
  2443. .pm = &smiapp_pm_ops,
  2444. },
  2445. .probe = smiapp_probe,
  2446. .remove = smiapp_remove,
  2447. .id_table = smiapp_id_table,
  2448. };
  2449. module_i2c_driver(smiapp_i2c_driver);
  2450. MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>");
  2451. MODULE_DESCRIPTION("Generic SMIA/SMIA++ camera module driver");
  2452. MODULE_LICENSE("GPL");