smiapp-core.c 77 KB

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