smiapp-core.c 83 KB

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