smiapp-core.c 85 KB

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