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