smiapp-core.c 83 KB

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