smiapp-core.c 77 KB

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