smiapp-core.c 77 KB

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