smiapp-core.c 77 KB

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