smiapp-core.c 83 KB

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