smiapp-core.c 76 KB

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