smiapp-core.c 84 KB

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