smiapp-core.c 84 KB

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