smiapp-core.c 77 KB

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