smiapp-core.c 78 KB

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