smiapp-core.c 75 KB

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