smiapp-core.c 77 KB

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