imx274.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * imx274.c - IMX274 CMOS Image Sensor driver
  4. *
  5. * Copyright (C) 2017, Leopard Imaging, Inc.
  6. *
  7. * Leon Luo <leonl@leopardimaging.com>
  8. * Edwin Zou <edwinz@leopardimaging.com>
  9. * Luca Ceresoli <luca@lucaceresoli.net>
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/gpio/consumer.h>
  14. #include <linux/i2c.h>
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/regmap.h>
  20. #include <linux/regulator/consumer.h>
  21. #include <linux/slab.h>
  22. #include <linux/v4l2-mediabus.h>
  23. #include <linux/videodev2.h>
  24. #include <media/v4l2-ctrls.h>
  25. #include <media/v4l2-device.h>
  26. #include <media/v4l2-fwnode.h>
  27. #include <media/v4l2-subdev.h>
  28. /*
  29. * See "SHR, SVR Setting" in datasheet
  30. */
  31. #define IMX274_DEFAULT_FRAME_LENGTH (4550)
  32. #define IMX274_MAX_FRAME_LENGTH (0x000fffff)
  33. /*
  34. * See "Frame Rate Adjustment" in datasheet
  35. */
  36. #define IMX274_PIXCLK_CONST1 (72000000)
  37. #define IMX274_PIXCLK_CONST2 (1000000)
  38. /*
  39. * The input gain is shifted by IMX274_GAIN_SHIFT to get
  40. * decimal number. The real gain is
  41. * (float)input_gain_value / (1 << IMX274_GAIN_SHIFT)
  42. */
  43. #define IMX274_GAIN_SHIFT (8)
  44. #define IMX274_GAIN_SHIFT_MASK ((1 << IMX274_GAIN_SHIFT) - 1)
  45. /*
  46. * See "Analog Gain" and "Digital Gain" in datasheet
  47. * min gain is 1X
  48. * max gain is calculated based on IMX274_GAIN_REG_MAX
  49. */
  50. #define IMX274_GAIN_REG_MAX (1957)
  51. #define IMX274_MIN_GAIN (0x01 << IMX274_GAIN_SHIFT)
  52. #define IMX274_MAX_ANALOG_GAIN ((2048 << IMX274_GAIN_SHIFT)\
  53. / (2048 - IMX274_GAIN_REG_MAX))
  54. #define IMX274_MAX_DIGITAL_GAIN (8)
  55. #define IMX274_DEF_GAIN (20 << IMX274_GAIN_SHIFT)
  56. #define IMX274_GAIN_CONST (2048) /* for gain formula */
  57. /*
  58. * 1 line time in us = (HMAX / 72), minimal is 4 lines
  59. */
  60. #define IMX274_MIN_EXPOSURE_TIME (4 * 260 / 72)
  61. #define IMX274_MAX_WIDTH (3840)
  62. #define IMX274_MAX_HEIGHT (2160)
  63. #define IMX274_MAX_FRAME_RATE (120)
  64. #define IMX274_MIN_FRAME_RATE (5)
  65. #define IMX274_DEF_FRAME_RATE (60)
  66. /*
  67. * register SHR is limited to (SVR value + 1) x VMAX value - 4
  68. */
  69. #define IMX274_SHR_LIMIT_CONST (4)
  70. /*
  71. * Min and max sensor reset delay (microseconds)
  72. */
  73. #define IMX274_RESET_DELAY1 (2000)
  74. #define IMX274_RESET_DELAY2 (2200)
  75. /*
  76. * shift and mask constants
  77. */
  78. #define IMX274_SHIFT_8_BITS (8)
  79. #define IMX274_SHIFT_16_BITS (16)
  80. #define IMX274_MASK_LSB_2_BITS (0x03)
  81. #define IMX274_MASK_LSB_3_BITS (0x07)
  82. #define IMX274_MASK_LSB_4_BITS (0x0f)
  83. #define IMX274_MASK_LSB_8_BITS (0x00ff)
  84. #define DRIVER_NAME "IMX274"
  85. /*
  86. * IMX274 register definitions
  87. */
  88. #define IMX274_SHR_REG_MSB 0x300D /* SHR */
  89. #define IMX274_SHR_REG_LSB 0x300C /* SHR */
  90. #define IMX274_SVR_REG_MSB 0x300F /* SVR */
  91. #define IMX274_SVR_REG_LSB 0x300E /* SVR */
  92. #define IMX274_HTRIM_EN_REG 0x3037
  93. #define IMX274_HTRIM_START_REG_LSB 0x3038
  94. #define IMX274_HTRIM_START_REG_MSB 0x3039
  95. #define IMX274_HTRIM_END_REG_LSB 0x303A
  96. #define IMX274_HTRIM_END_REG_MSB 0x303B
  97. #define IMX274_VWIDCUTEN_REG 0x30DD
  98. #define IMX274_VWIDCUT_REG_LSB 0x30DE
  99. #define IMX274_VWIDCUT_REG_MSB 0x30DF
  100. #define IMX274_VWINPOS_REG_LSB 0x30E0
  101. #define IMX274_VWINPOS_REG_MSB 0x30E1
  102. #define IMX274_WRITE_VSIZE_REG_LSB 0x3130
  103. #define IMX274_WRITE_VSIZE_REG_MSB 0x3131
  104. #define IMX274_Y_OUT_SIZE_REG_LSB 0x3132
  105. #define IMX274_Y_OUT_SIZE_REG_MSB 0x3133
  106. #define IMX274_VMAX_REG_1 0x30FA /* VMAX, MSB */
  107. #define IMX274_VMAX_REG_2 0x30F9 /* VMAX */
  108. #define IMX274_VMAX_REG_3 0x30F8 /* VMAX, LSB */
  109. #define IMX274_HMAX_REG_MSB 0x30F7 /* HMAX */
  110. #define IMX274_HMAX_REG_LSB 0x30F6 /* HMAX */
  111. #define IMX274_ANALOG_GAIN_ADDR_LSB 0x300A /* ANALOG GAIN LSB */
  112. #define IMX274_ANALOG_GAIN_ADDR_MSB 0x300B /* ANALOG GAIN MSB */
  113. #define IMX274_DIGITAL_GAIN_REG 0x3012 /* Digital Gain */
  114. #define IMX274_VFLIP_REG 0x301A /* VERTICAL FLIP */
  115. #define IMX274_TEST_PATTERN_REG 0x303D /* TEST PATTERN */
  116. #define IMX274_STANDBY_REG 0x3000 /* STANDBY */
  117. #define IMX274_TABLE_WAIT_MS 0
  118. #define IMX274_TABLE_END 1
  119. /* regulator supplies */
  120. static const char * const imx274_supply_names[] = {
  121. "vddl", /* IF (1.2V) supply */
  122. "vdig", /* Digital Core (1.8V) supply */
  123. "vana", /* Analog (2.8V) supply */
  124. };
  125. #define IMX274_NUM_SUPPLIES ARRAY_SIZE(imx274_supply_names)
  126. /*
  127. * imx274 I2C operation related structure
  128. */
  129. struct reg_8 {
  130. u16 addr;
  131. u8 val;
  132. };
  133. static const struct regmap_config imx274_regmap_config = {
  134. .reg_bits = 16,
  135. .val_bits = 8,
  136. .cache_type = REGCACHE_MAPLE,
  137. };
  138. /*
  139. * Parameters for each imx274 readout mode.
  140. *
  141. * These are the values to configure the sensor in one of the
  142. * implemented modes.
  143. *
  144. * @init_regs: registers to initialize the mode
  145. * @wbin_ratio: width downscale factor (e.g. 3 for 1280; 3 = 3840/1280)
  146. * @hbin_ratio: height downscale factor (e.g. 3 for 720; 3 = 2160/720)
  147. * @min_frame_len: Minimum frame length for each mode (see "Frame Rate
  148. * Adjustment (CSI-2)" in the datasheet)
  149. * @min_SHR: Minimum SHR register value (see "Shutter Setting (CSI-2)" in the
  150. * datasheet)
  151. * @max_fps: Maximum frames per second
  152. * @nocpiop: Number of clocks per internal offset period (see "Integration Time
  153. * in Each Readout Drive Mode (CSI-2)" in the datasheet)
  154. */
  155. struct imx274_mode {
  156. const struct reg_8 *init_regs;
  157. u8 wbin_ratio;
  158. u8 hbin_ratio;
  159. int min_frame_len;
  160. int min_SHR;
  161. int max_fps;
  162. int nocpiop;
  163. };
  164. /*
  165. * imx274 test pattern related structure
  166. */
  167. enum {
  168. TEST_PATTERN_DISABLED = 0,
  169. TEST_PATTERN_ALL_000H,
  170. TEST_PATTERN_ALL_FFFH,
  171. TEST_PATTERN_ALL_555H,
  172. TEST_PATTERN_ALL_AAAH,
  173. TEST_PATTERN_VSP_5AH, /* VERTICAL STRIPE PATTERN 555H/AAAH */
  174. TEST_PATTERN_VSP_A5H, /* VERTICAL STRIPE PATTERN AAAH/555H */
  175. TEST_PATTERN_VSP_05H, /* VERTICAL STRIPE PATTERN 000H/555H */
  176. TEST_PATTERN_VSP_50H, /* VERTICAL STRIPE PATTERN 555H/000H */
  177. TEST_PATTERN_VSP_0FH, /* VERTICAL STRIPE PATTERN 000H/FFFH */
  178. TEST_PATTERN_VSP_F0H, /* VERTICAL STRIPE PATTERN FFFH/000H */
  179. TEST_PATTERN_H_COLOR_BARS,
  180. TEST_PATTERN_V_COLOR_BARS,
  181. };
  182. static const char * const tp_qmenu[] = {
  183. "Disabled",
  184. "All 000h Pattern",
  185. "All FFFh Pattern",
  186. "All 555h Pattern",
  187. "All AAAh Pattern",
  188. "Vertical Stripe (555h / AAAh)",
  189. "Vertical Stripe (AAAh / 555h)",
  190. "Vertical Stripe (000h / 555h)",
  191. "Vertical Stripe (555h / 000h)",
  192. "Vertical Stripe (000h / FFFh)",
  193. "Vertical Stripe (FFFh / 000h)",
  194. "Vertical Color Bars",
  195. "Horizontal Color Bars",
  196. };
  197. /*
  198. * All-pixel scan mode (10-bit)
  199. * imx274 mode1(refer to datasheet) register configuration with
  200. * 3840x2160 resolution, raw10 data and mipi four lane output
  201. */
  202. static const struct reg_8 imx274_mode1_3840x2160_raw10[] = {
  203. {0x3004, 0x01},
  204. {0x3005, 0x01},
  205. {0x3006, 0x00},
  206. {0x3007, 0xa2},
  207. {0x3018, 0xA2}, /* output XVS, HVS */
  208. {0x306B, 0x05},
  209. {0x30E2, 0x01},
  210. {0x30EE, 0x01},
  211. {0x3342, 0x0A},
  212. {0x3343, 0x00},
  213. {0x3344, 0x16},
  214. {0x3345, 0x00},
  215. {0x33A6, 0x01},
  216. {0x3528, 0x0E},
  217. {0x3554, 0x1F},
  218. {0x3555, 0x01},
  219. {0x3556, 0x01},
  220. {0x3557, 0x01},
  221. {0x3558, 0x01},
  222. {0x3559, 0x00},
  223. {0x355A, 0x00},
  224. {0x35BA, 0x0E},
  225. {0x366A, 0x1B},
  226. {0x366B, 0x1A},
  227. {0x366C, 0x19},
  228. {0x366D, 0x17},
  229. {0x3A41, 0x08},
  230. {IMX274_TABLE_END, 0x00}
  231. };
  232. /*
  233. * Horizontal/vertical 2/2-line binning
  234. * (Horizontal and vertical weightedbinning, 10-bit)
  235. * imx274 mode3(refer to datasheet) register configuration with
  236. * 1920x1080 resolution, raw10 data and mipi four lane output
  237. */
  238. static const struct reg_8 imx274_mode3_1920x1080_raw10[] = {
  239. {0x3004, 0x02},
  240. {0x3005, 0x21},
  241. {0x3006, 0x00},
  242. {0x3007, 0xb1},
  243. {0x3018, 0xA2}, /* output XVS, HVS */
  244. {0x306B, 0x05},
  245. {0x30E2, 0x02},
  246. {0x30EE, 0x01},
  247. {0x3342, 0x0A},
  248. {0x3343, 0x00},
  249. {0x3344, 0x1A},
  250. {0x3345, 0x00},
  251. {0x33A6, 0x01},
  252. {0x3528, 0x0E},
  253. {0x3554, 0x00},
  254. {0x3555, 0x01},
  255. {0x3556, 0x01},
  256. {0x3557, 0x01},
  257. {0x3558, 0x01},
  258. {0x3559, 0x00},
  259. {0x355A, 0x00},
  260. {0x35BA, 0x0E},
  261. {0x366A, 0x1B},
  262. {0x366B, 0x1A},
  263. {0x366C, 0x19},
  264. {0x366D, 0x17},
  265. {0x3A41, 0x08},
  266. {IMX274_TABLE_END, 0x00}
  267. };
  268. /*
  269. * Vertical 2/3 subsampling binning horizontal 3 binning
  270. * imx274 mode5(refer to datasheet) register configuration with
  271. * 1280x720 resolution, raw10 data and mipi four lane output
  272. */
  273. static const struct reg_8 imx274_mode5_1280x720_raw10[] = {
  274. {0x3004, 0x03},
  275. {0x3005, 0x31},
  276. {0x3006, 0x00},
  277. {0x3007, 0xa9},
  278. {0x3018, 0xA2}, /* output XVS, HVS */
  279. {0x306B, 0x05},
  280. {0x30E2, 0x03},
  281. {0x30EE, 0x01},
  282. {0x3342, 0x0A},
  283. {0x3343, 0x00},
  284. {0x3344, 0x1B},
  285. {0x3345, 0x00},
  286. {0x33A6, 0x01},
  287. {0x3528, 0x0E},
  288. {0x3554, 0x00},
  289. {0x3555, 0x01},
  290. {0x3556, 0x01},
  291. {0x3557, 0x01},
  292. {0x3558, 0x01},
  293. {0x3559, 0x00},
  294. {0x355A, 0x00},
  295. {0x35BA, 0x0E},
  296. {0x366A, 0x1B},
  297. {0x366B, 0x19},
  298. {0x366C, 0x17},
  299. {0x366D, 0x17},
  300. {0x3A41, 0x04},
  301. {IMX274_TABLE_END, 0x00}
  302. };
  303. /*
  304. * Vertical 2/8 subsampling horizontal 3 binning
  305. * imx274 mode6(refer to datasheet) register configuration with
  306. * 1280x540 resolution, raw10 data and mipi four lane output
  307. */
  308. static const struct reg_8 imx274_mode6_1280x540_raw10[] = {
  309. {0x3004, 0x04}, /* mode setting */
  310. {0x3005, 0x31},
  311. {0x3006, 0x00},
  312. {0x3007, 0x02}, /* mode setting */
  313. {0x3018, 0xA2}, /* output XVS, HVS */
  314. {0x306B, 0x05},
  315. {0x30E2, 0x04}, /* mode setting */
  316. {0x30EE, 0x01},
  317. {0x3342, 0x0A},
  318. {0x3343, 0x00},
  319. {0x3344, 0x16},
  320. {0x3345, 0x00},
  321. {0x33A6, 0x01},
  322. {0x3528, 0x0E},
  323. {0x3554, 0x1F},
  324. {0x3555, 0x01},
  325. {0x3556, 0x01},
  326. {0x3557, 0x01},
  327. {0x3558, 0x01},
  328. {0x3559, 0x00},
  329. {0x355A, 0x00},
  330. {0x35BA, 0x0E},
  331. {0x366A, 0x1B},
  332. {0x366B, 0x1A},
  333. {0x366C, 0x19},
  334. {0x366D, 0x17},
  335. {0x3A41, 0x04},
  336. {IMX274_TABLE_END, 0x00}
  337. };
  338. /*
  339. * imx274 first step register configuration for
  340. * starting stream
  341. */
  342. static const struct reg_8 imx274_start_1[] = {
  343. {IMX274_STANDBY_REG, 0x12},
  344. /* PLRD: clock settings */
  345. {0x3120, 0xF0},
  346. {0x3121, 0x00},
  347. {0x3122, 0x02},
  348. {0x3129, 0x9C},
  349. {0x312A, 0x02},
  350. {0x312D, 0x02},
  351. {0x310B, 0x00},
  352. /* PLSTMG */
  353. {0x304C, 0x00}, /* PLSTMG01 */
  354. {0x304D, 0x03},
  355. {0x331C, 0x1A},
  356. {0x331D, 0x00},
  357. {0x3502, 0x02},
  358. {0x3529, 0x0E},
  359. {0x352A, 0x0E},
  360. {0x352B, 0x0E},
  361. {0x3538, 0x0E},
  362. {0x3539, 0x0E},
  363. {0x3553, 0x00},
  364. {0x357D, 0x05},
  365. {0x357F, 0x05},
  366. {0x3581, 0x04},
  367. {0x3583, 0x76},
  368. {0x3587, 0x01},
  369. {0x35BB, 0x0E},
  370. {0x35BC, 0x0E},
  371. {0x35BD, 0x0E},
  372. {0x35BE, 0x0E},
  373. {0x35BF, 0x0E},
  374. {0x366E, 0x00},
  375. {0x366F, 0x00},
  376. {0x3670, 0x00},
  377. {0x3671, 0x00},
  378. /* PSMIPI */
  379. {0x3304, 0x32}, /* PSMIPI1 */
  380. {0x3305, 0x00},
  381. {0x3306, 0x32},
  382. {0x3307, 0x00},
  383. {0x3590, 0x32},
  384. {0x3591, 0x00},
  385. {0x3686, 0x32},
  386. {0x3687, 0x00},
  387. {IMX274_TABLE_END, 0x00}
  388. };
  389. /*
  390. * imx274 second step register configuration for
  391. * starting stream
  392. */
  393. static const struct reg_8 imx274_start_2[] = {
  394. {IMX274_STANDBY_REG, 0x00},
  395. {0x303E, 0x02}, /* SYS_MODE = 2 */
  396. {IMX274_TABLE_END, 0x00}
  397. };
  398. /*
  399. * imx274 third step register configuration for
  400. * starting stream
  401. */
  402. static const struct reg_8 imx274_start_3[] = {
  403. {0x30F4, 0x00},
  404. {0x3018, 0xA2}, /* XHS VHS OUTPUT */
  405. {IMX274_TABLE_END, 0x00}
  406. };
  407. /*
  408. * imx274 register configuration for stopping stream
  409. */
  410. static const struct reg_8 imx274_stop[] = {
  411. {IMX274_STANDBY_REG, 0x01},
  412. {IMX274_TABLE_END, 0x00}
  413. };
  414. /*
  415. * imx274 disable test pattern register configuration
  416. */
  417. static const struct reg_8 imx274_tp_disabled[] = {
  418. {0x303C, 0x00},
  419. {0x377F, 0x00},
  420. {0x3781, 0x00},
  421. {0x370B, 0x00},
  422. {IMX274_TABLE_END, 0x00}
  423. };
  424. /*
  425. * imx274 test pattern register configuration
  426. * reg 0x303D defines the test pattern modes
  427. */
  428. static const struct reg_8 imx274_tp_regs[] = {
  429. {0x303C, 0x11},
  430. {0x370E, 0x01},
  431. {0x377F, 0x01},
  432. {0x3781, 0x01},
  433. {0x370B, 0x11},
  434. {IMX274_TABLE_END, 0x00}
  435. };
  436. /* nocpiop happens to be the same number for the implemented modes */
  437. static const struct imx274_mode imx274_modes[] = {
  438. {
  439. /* mode 1, 4K */
  440. .wbin_ratio = 1, /* 3840 */
  441. .hbin_ratio = 1, /* 2160 */
  442. .init_regs = imx274_mode1_3840x2160_raw10,
  443. .min_frame_len = 4550,
  444. .min_SHR = 12,
  445. .max_fps = 60,
  446. .nocpiop = 112,
  447. },
  448. {
  449. /* mode 3, 1080p */
  450. .wbin_ratio = 2, /* 1920 */
  451. .hbin_ratio = 2, /* 1080 */
  452. .init_regs = imx274_mode3_1920x1080_raw10,
  453. .min_frame_len = 2310,
  454. .min_SHR = 8,
  455. .max_fps = 120,
  456. .nocpiop = 112,
  457. },
  458. {
  459. /* mode 5, 720p */
  460. .wbin_ratio = 3, /* 1280 */
  461. .hbin_ratio = 3, /* 720 */
  462. .init_regs = imx274_mode5_1280x720_raw10,
  463. .min_frame_len = 2310,
  464. .min_SHR = 8,
  465. .max_fps = 120,
  466. .nocpiop = 112,
  467. },
  468. {
  469. /* mode 6, 540p */
  470. .wbin_ratio = 3, /* 1280 */
  471. .hbin_ratio = 4, /* 540 */
  472. .init_regs = imx274_mode6_1280x540_raw10,
  473. .min_frame_len = 2310,
  474. .min_SHR = 4,
  475. .max_fps = 120,
  476. .nocpiop = 112,
  477. },
  478. };
  479. /*
  480. * struct imx274_ctrls - imx274 ctrl structure
  481. * @handler: V4L2 ctrl handler structure
  482. * @exposure: Pointer to expsure ctrl structure
  483. * @gain: Pointer to gain ctrl structure
  484. * @vflip: Pointer to vflip ctrl structure
  485. * @test_pattern: Pointer to test pattern ctrl structure
  486. */
  487. struct imx274_ctrls {
  488. struct v4l2_ctrl_handler handler;
  489. struct v4l2_ctrl *exposure;
  490. struct v4l2_ctrl *gain;
  491. struct v4l2_ctrl *vflip;
  492. struct v4l2_ctrl *test_pattern;
  493. };
  494. /*
  495. * struct stim274 - imx274 device structure
  496. * @sd: V4L2 subdevice structure
  497. * @pad: Media pad structure
  498. * @client: Pointer to I2C client
  499. * @ctrls: imx274 control structure
  500. * @crop: rect to be captured
  501. * @compose: compose rect, i.e. output resolution
  502. * @format: V4L2 media bus frame format structure
  503. * (width and height are in sync with the compose rect)
  504. * @frame_rate: V4L2 frame rate structure
  505. * @regmap: Pointer to regmap structure
  506. * @reset_gpio: Pointer to reset gpio
  507. * @supplies: List of analog and digital supply regulators
  508. * @inck: Pointer to sensor input clock
  509. * @lock: Mutex structure
  510. * @mode: Parameters for the selected readout mode
  511. */
  512. struct stimx274 {
  513. struct v4l2_subdev sd;
  514. struct media_pad pad;
  515. struct i2c_client *client;
  516. struct imx274_ctrls ctrls;
  517. struct v4l2_rect crop;
  518. struct v4l2_mbus_framefmt format;
  519. struct v4l2_fract frame_interval;
  520. struct regmap *regmap;
  521. struct gpio_desc *reset_gpio;
  522. struct regulator_bulk_data supplies[IMX274_NUM_SUPPLIES];
  523. struct clk *inck;
  524. struct mutex lock; /* mutex lock for operations */
  525. const struct imx274_mode *mode;
  526. };
  527. #define IMX274_ROUND(dim, step, flags) \
  528. ((flags) & V4L2_SEL_FLAG_GE \
  529. ? roundup((dim), (step)) \
  530. : ((flags) & V4L2_SEL_FLAG_LE \
  531. ? rounddown((dim), (step)) \
  532. : rounddown((dim) + (step) / 2, (step))))
  533. /*
  534. * Function declaration
  535. */
  536. static int imx274_set_gain(struct stimx274 *priv, struct v4l2_ctrl *ctrl);
  537. static int imx274_set_exposure(struct stimx274 *priv, int val);
  538. static int imx274_set_vflip(struct stimx274 *priv, int val);
  539. static int imx274_set_test_pattern(struct stimx274 *priv, int val);
  540. static int __imx274_set_frame_interval(struct stimx274 *priv,
  541. struct v4l2_fract frame_interval);
  542. static inline void msleep_range(unsigned int delay_base)
  543. {
  544. usleep_range(delay_base * 1000, delay_base * 1000 + 500);
  545. }
  546. /*
  547. * v4l2_ctrl and v4l2_subdev related operations
  548. */
  549. static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
  550. {
  551. return &container_of(ctrl->handler,
  552. struct stimx274, ctrls.handler)->sd;
  553. }
  554. static inline struct stimx274 *to_imx274(struct v4l2_subdev *sd)
  555. {
  556. return container_of(sd, struct stimx274, sd);
  557. }
  558. /*
  559. * Writing a register table
  560. *
  561. * @priv: Pointer to device
  562. * @table: Table containing register values (with optional delays)
  563. *
  564. * This is used to write register table into sensor's reg map.
  565. *
  566. * Return: 0 on success, errors otherwise
  567. */
  568. static int imx274_write_table(struct stimx274 *priv, const struct reg_8 table[])
  569. {
  570. struct regmap *regmap = priv->regmap;
  571. int err = 0;
  572. const struct reg_8 *next;
  573. u8 val;
  574. int range_start = -1;
  575. int range_count = 0;
  576. u8 range_vals[16];
  577. int max_range_vals = ARRAY_SIZE(range_vals);
  578. for (next = table;; next++) {
  579. if ((next->addr != range_start + range_count) ||
  580. (next->addr == IMX274_TABLE_END) ||
  581. (next->addr == IMX274_TABLE_WAIT_MS) ||
  582. (range_count == max_range_vals)) {
  583. if (range_count == 1)
  584. err = regmap_write(regmap,
  585. range_start, range_vals[0]);
  586. else if (range_count > 1)
  587. err = regmap_bulk_write(regmap, range_start,
  588. &range_vals[0],
  589. range_count);
  590. else
  591. err = 0;
  592. if (err)
  593. return err;
  594. range_start = -1;
  595. range_count = 0;
  596. /* Handle special address values */
  597. if (next->addr == IMX274_TABLE_END)
  598. break;
  599. if (next->addr == IMX274_TABLE_WAIT_MS) {
  600. msleep_range(next->val);
  601. continue;
  602. }
  603. }
  604. val = next->val;
  605. if (range_start == -1)
  606. range_start = next->addr;
  607. range_vals[range_count++] = val;
  608. }
  609. return 0;
  610. }
  611. static inline int imx274_write_reg(struct stimx274 *priv, u16 addr, u8 val)
  612. {
  613. int err;
  614. err = regmap_write(priv->regmap, addr, val);
  615. if (err)
  616. dev_err(&priv->client->dev,
  617. "%s : i2c write failed, %x = %x\n", __func__,
  618. addr, val);
  619. else
  620. dev_dbg(&priv->client->dev,
  621. "%s : addr 0x%x, val=0x%x\n", __func__,
  622. addr, val);
  623. return err;
  624. }
  625. /**
  626. * imx274_read_mbreg - Read a multibyte register.
  627. *
  628. * Uses a bulk read where possible.
  629. *
  630. * @priv: Pointer to device structure
  631. * @addr: Address of the LSB register. Other registers must be
  632. * consecutive, least-to-most significant.
  633. * @val: Pointer to store the register value (cpu endianness)
  634. * @nbytes: Number of bytes to read (range: [1..3]).
  635. * Other bytes are zet to 0.
  636. *
  637. * Return: 0 on success, errors otherwise
  638. */
  639. static int imx274_read_mbreg(struct stimx274 *priv, u16 addr, u32 *val,
  640. size_t nbytes)
  641. {
  642. __le32 val_le = 0;
  643. int err;
  644. err = regmap_bulk_read(priv->regmap, addr, &val_le, nbytes);
  645. if (err) {
  646. dev_err(&priv->client->dev,
  647. "%s : i2c bulk read failed, %x (%zu bytes)\n",
  648. __func__, addr, nbytes);
  649. } else {
  650. *val = le32_to_cpu(val_le);
  651. dev_dbg(&priv->client->dev,
  652. "%s : addr 0x%x, val=0x%x (%zu bytes)\n",
  653. __func__, addr, *val, nbytes);
  654. }
  655. return err;
  656. }
  657. /**
  658. * imx274_write_mbreg - Write a multibyte register.
  659. *
  660. * Uses a bulk write where possible.
  661. *
  662. * @priv: Pointer to device structure
  663. * @addr: Address of the LSB register. Other registers must be
  664. * consecutive, least-to-most significant.
  665. * @val: Value to be written to the register (cpu endianness)
  666. * @nbytes: Number of bytes to write (range: [1..3])
  667. */
  668. static int imx274_write_mbreg(struct stimx274 *priv, u16 addr, u32 val,
  669. size_t nbytes)
  670. {
  671. __le32 val_le = cpu_to_le32(val);
  672. int err;
  673. err = regmap_bulk_write(priv->regmap, addr, &val_le, nbytes);
  674. if (err)
  675. dev_err(&priv->client->dev,
  676. "%s : i2c bulk write failed, %x = %x (%zu bytes)\n",
  677. __func__, addr, val, nbytes);
  678. else
  679. dev_dbg(&priv->client->dev,
  680. "%s : addr 0x%x, val=0x%x (%zu bytes)\n",
  681. __func__, addr, val, nbytes);
  682. return err;
  683. }
  684. /*
  685. * Set mode registers to start stream.
  686. * @priv: Pointer to device structure
  687. *
  688. * Return: 0 on success, errors otherwise
  689. */
  690. static int imx274_mode_regs(struct stimx274 *priv)
  691. {
  692. int err = 0;
  693. err = imx274_write_table(priv, imx274_start_1);
  694. if (err)
  695. return err;
  696. err = imx274_write_table(priv, priv->mode->init_regs);
  697. return err;
  698. }
  699. /*
  700. * imx274_start_stream - Function for starting stream per mode index
  701. * @priv: Pointer to device structure
  702. *
  703. * Return: 0 on success, errors otherwise
  704. */
  705. static int imx274_start_stream(struct stimx274 *priv)
  706. {
  707. int err = 0;
  708. err = __v4l2_ctrl_handler_setup(&priv->ctrls.handler);
  709. if (err) {
  710. dev_err(&priv->client->dev, "Error %d setup controls\n", err);
  711. return err;
  712. }
  713. /*
  714. * Refer to "Standby Cancel Sequence when using CSI-2" in
  715. * imx274 datasheet, it should wait 10ms or more here.
  716. * give it 1 extra ms for margin
  717. */
  718. msleep_range(11);
  719. err = imx274_write_table(priv, imx274_start_2);
  720. if (err)
  721. return err;
  722. /*
  723. * Refer to "Standby Cancel Sequence when using CSI-2" in
  724. * imx274 datasheet, it should wait 7ms or more here.
  725. * give it 1 extra ms for margin
  726. */
  727. msleep_range(8);
  728. err = imx274_write_table(priv, imx274_start_3);
  729. if (err)
  730. return err;
  731. return 0;
  732. }
  733. /*
  734. * imx274_reset - Function called to reset the sensor
  735. * @priv: Pointer to device structure
  736. * @rst: Input value for determining the sensor's end state after reset
  737. *
  738. * Set the senor in reset and then
  739. * if rst = 0, keep it in reset;
  740. * if rst = 1, bring it out of reset.
  741. *
  742. * Note: Misinterpretation of reset assertion - do not re-use this code.
  743. * XCLR pin is using incorrect (for reset signal) logical level.
  744. */
  745. static void imx274_reset(struct stimx274 *priv, int rst)
  746. {
  747. gpiod_set_value_cansleep(priv->reset_gpio, 0);
  748. usleep_range(IMX274_RESET_DELAY1, IMX274_RESET_DELAY2);
  749. gpiod_set_value_cansleep(priv->reset_gpio, !!rst);
  750. usleep_range(IMX274_RESET_DELAY1, IMX274_RESET_DELAY2);
  751. }
  752. static int imx274_power_on(struct device *dev)
  753. {
  754. struct i2c_client *client = to_i2c_client(dev);
  755. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  756. struct stimx274 *imx274 = to_imx274(sd);
  757. int ret;
  758. /* keep sensor in reset before power on */
  759. imx274_reset(imx274, 0);
  760. ret = clk_prepare_enable(imx274->inck);
  761. if (ret) {
  762. dev_err(&imx274->client->dev,
  763. "Failed to enable input clock: %d\n", ret);
  764. return ret;
  765. }
  766. ret = regulator_bulk_enable(IMX274_NUM_SUPPLIES, imx274->supplies);
  767. if (ret) {
  768. dev_err(&imx274->client->dev,
  769. "Failed to enable regulators: %d\n", ret);
  770. goto fail_reg;
  771. }
  772. udelay(2);
  773. imx274_reset(imx274, 1);
  774. return 0;
  775. fail_reg:
  776. clk_disable_unprepare(imx274->inck);
  777. return ret;
  778. }
  779. static int imx274_power_off(struct device *dev)
  780. {
  781. struct i2c_client *client = to_i2c_client(dev);
  782. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  783. struct stimx274 *imx274 = to_imx274(sd);
  784. imx274_reset(imx274, 0);
  785. regulator_bulk_disable(IMX274_NUM_SUPPLIES, imx274->supplies);
  786. clk_disable_unprepare(imx274->inck);
  787. return 0;
  788. }
  789. static int imx274_regulators_get(struct device *dev, struct stimx274 *imx274)
  790. {
  791. unsigned int i;
  792. for (i = 0; i < IMX274_NUM_SUPPLIES; i++)
  793. imx274->supplies[i].supply = imx274_supply_names[i];
  794. return devm_regulator_bulk_get(dev, IMX274_NUM_SUPPLIES,
  795. imx274->supplies);
  796. }
  797. /**
  798. * imx274_s_ctrl - This is used to set the imx274 V4L2 controls
  799. * @ctrl: V4L2 control to be set
  800. *
  801. * This function is used to set the V4L2 controls for the imx274 sensor.
  802. *
  803. * Return: 0 on success, errors otherwise
  804. */
  805. static int imx274_s_ctrl(struct v4l2_ctrl *ctrl)
  806. {
  807. struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
  808. struct stimx274 *imx274 = to_imx274(sd);
  809. int ret = -EINVAL;
  810. if (!pm_runtime_get_if_in_use(&imx274->client->dev))
  811. return 0;
  812. dev_dbg(&imx274->client->dev,
  813. "%s : s_ctrl: %s, value: %d\n", __func__,
  814. ctrl->name, ctrl->val);
  815. switch (ctrl->id) {
  816. case V4L2_CID_EXPOSURE:
  817. dev_dbg(&imx274->client->dev,
  818. "%s : set V4L2_CID_EXPOSURE\n", __func__);
  819. ret = imx274_set_exposure(imx274, ctrl->val);
  820. break;
  821. case V4L2_CID_GAIN:
  822. dev_dbg(&imx274->client->dev,
  823. "%s : set V4L2_CID_GAIN\n", __func__);
  824. ret = imx274_set_gain(imx274, ctrl);
  825. break;
  826. case V4L2_CID_VFLIP:
  827. dev_dbg(&imx274->client->dev,
  828. "%s : set V4L2_CID_VFLIP\n", __func__);
  829. ret = imx274_set_vflip(imx274, ctrl->val);
  830. break;
  831. case V4L2_CID_TEST_PATTERN:
  832. dev_dbg(&imx274->client->dev,
  833. "%s : set V4L2_CID_TEST_PATTERN\n", __func__);
  834. ret = imx274_set_test_pattern(imx274, ctrl->val);
  835. break;
  836. }
  837. pm_runtime_put(&imx274->client->dev);
  838. return ret;
  839. }
  840. static int imx274_binning_goodness(struct stimx274 *imx274,
  841. int w, int ask_w,
  842. int h, int ask_h, u32 flags)
  843. {
  844. struct device *dev = &imx274->client->dev;
  845. const int goodness = 100000;
  846. int val = 0;
  847. if (flags & V4L2_SEL_FLAG_GE) {
  848. if (w < ask_w)
  849. val -= goodness;
  850. if (h < ask_h)
  851. val -= goodness;
  852. }
  853. if (flags & V4L2_SEL_FLAG_LE) {
  854. if (w > ask_w)
  855. val -= goodness;
  856. if (h > ask_h)
  857. val -= goodness;
  858. }
  859. val -= abs(w - ask_w);
  860. val -= abs(h - ask_h);
  861. dev_dbg(dev, "%s: ask %dx%d, size %dx%d, goodness %d\n",
  862. __func__, ask_w, ask_h, w, h, val);
  863. return val;
  864. }
  865. /**
  866. * __imx274_change_compose - Helper function to change binning and set both
  867. * compose and format.
  868. *
  869. * We have two entry points to change binning: set_fmt and
  870. * set_selection(COMPOSE). Both have to compute the new output size
  871. * and set it in both the compose rect and the frame format size. We
  872. * also need to do the same things after setting cropping to restore
  873. * 1:1 binning.
  874. *
  875. * This function contains the common code for these three cases, it
  876. * has many arguments in order to accommodate the needs of all of
  877. * them.
  878. *
  879. * Must be called with imx274->lock locked.
  880. *
  881. * @imx274: The device object
  882. * @sd_state: The subdev state we are editing for TRY requests
  883. * @which: V4L2_SUBDEV_FORMAT_ACTIVE or V4L2_SUBDEV_FORMAT_TRY from the caller
  884. * @width: Input-output parameter: set to the desired width before
  885. * the call, contains the chosen value after returning successfully
  886. * @height: Input-output parameter for height (see @width)
  887. * @flags: Selection flags from struct v4l2_subdev_selection, or 0 if not
  888. * available (when called from set_fmt)
  889. */
  890. static int __imx274_change_compose(struct stimx274 *imx274,
  891. struct v4l2_subdev_state *sd_state,
  892. u32 which,
  893. u32 *width,
  894. u32 *height,
  895. u32 flags)
  896. {
  897. struct device *dev = &imx274->client->dev;
  898. const struct v4l2_rect *cur_crop;
  899. struct v4l2_mbus_framefmt *tgt_fmt;
  900. unsigned int i;
  901. const struct imx274_mode *best_mode = &imx274_modes[0];
  902. int best_goodness = INT_MIN;
  903. if (which == V4L2_SUBDEV_FORMAT_TRY) {
  904. cur_crop = v4l2_subdev_state_get_crop(sd_state, 0);
  905. tgt_fmt = v4l2_subdev_state_get_format(sd_state, 0);
  906. } else {
  907. cur_crop = &imx274->crop;
  908. tgt_fmt = &imx274->format;
  909. }
  910. for (i = 0; i < ARRAY_SIZE(imx274_modes); i++) {
  911. u8 wratio = imx274_modes[i].wbin_ratio;
  912. u8 hratio = imx274_modes[i].hbin_ratio;
  913. int goodness = imx274_binning_goodness(
  914. imx274,
  915. cur_crop->width / wratio, *width,
  916. cur_crop->height / hratio, *height,
  917. flags);
  918. if (goodness >= best_goodness) {
  919. best_goodness = goodness;
  920. best_mode = &imx274_modes[i];
  921. }
  922. }
  923. *width = cur_crop->width / best_mode->wbin_ratio;
  924. *height = cur_crop->height / best_mode->hbin_ratio;
  925. if (which == V4L2_SUBDEV_FORMAT_ACTIVE)
  926. imx274->mode = best_mode;
  927. dev_dbg(dev, "%s: selected %ux%u binning\n",
  928. __func__, best_mode->wbin_ratio, best_mode->hbin_ratio);
  929. tgt_fmt->width = *width;
  930. tgt_fmt->height = *height;
  931. tgt_fmt->field = V4L2_FIELD_NONE;
  932. return 0;
  933. }
  934. /**
  935. * imx274_get_fmt - Get the pad format
  936. * @sd: Pointer to V4L2 Sub device structure
  937. * @sd_state: Pointer to sub device state structure
  938. * @fmt: Pointer to pad level media bus format
  939. *
  940. * This function is used to get the pad format information.
  941. *
  942. * Return: 0 on success
  943. */
  944. static int imx274_get_fmt(struct v4l2_subdev *sd,
  945. struct v4l2_subdev_state *sd_state,
  946. struct v4l2_subdev_format *fmt)
  947. {
  948. struct stimx274 *imx274 = to_imx274(sd);
  949. mutex_lock(&imx274->lock);
  950. fmt->format = imx274->format;
  951. mutex_unlock(&imx274->lock);
  952. return 0;
  953. }
  954. /**
  955. * imx274_set_fmt - This is used to set the pad format
  956. * @sd: Pointer to V4L2 Sub device structure
  957. * @sd_state: Pointer to sub device state information structure
  958. * @format: Pointer to pad level media bus format
  959. *
  960. * This function is used to set the pad format.
  961. *
  962. * Return: 0 on success
  963. */
  964. static int imx274_set_fmt(struct v4l2_subdev *sd,
  965. struct v4l2_subdev_state *sd_state,
  966. struct v4l2_subdev_format *format)
  967. {
  968. struct v4l2_mbus_framefmt *fmt = &format->format;
  969. struct stimx274 *imx274 = to_imx274(sd);
  970. int err = 0;
  971. mutex_lock(&imx274->lock);
  972. err = __imx274_change_compose(imx274, sd_state, format->which,
  973. &fmt->width, &fmt->height, 0);
  974. if (err)
  975. goto out;
  976. /*
  977. * __imx274_change_compose already set width and height in the
  978. * applicable format, but we need to keep all other format
  979. * values, so do a full copy here
  980. */
  981. fmt->field = V4L2_FIELD_NONE;
  982. if (format->which == V4L2_SUBDEV_FORMAT_TRY)
  983. *v4l2_subdev_state_get_format(sd_state, 0) = *fmt;
  984. else
  985. imx274->format = *fmt;
  986. out:
  987. mutex_unlock(&imx274->lock);
  988. return err;
  989. }
  990. static int imx274_get_selection(struct v4l2_subdev *sd,
  991. struct v4l2_subdev_state *sd_state,
  992. struct v4l2_subdev_selection *sel)
  993. {
  994. struct stimx274 *imx274 = to_imx274(sd);
  995. const struct v4l2_rect *src_crop;
  996. const struct v4l2_mbus_framefmt *src_fmt;
  997. int ret = 0;
  998. if (sel->pad != 0)
  999. return -EINVAL;
  1000. if (sel->target == V4L2_SEL_TGT_CROP_BOUNDS) {
  1001. sel->r.left = 0;
  1002. sel->r.top = 0;
  1003. sel->r.width = IMX274_MAX_WIDTH;
  1004. sel->r.height = IMX274_MAX_HEIGHT;
  1005. return 0;
  1006. }
  1007. if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
  1008. src_crop = v4l2_subdev_state_get_crop(sd_state, 0);
  1009. src_fmt = v4l2_subdev_state_get_format(sd_state, 0);
  1010. } else {
  1011. src_crop = &imx274->crop;
  1012. src_fmt = &imx274->format;
  1013. }
  1014. mutex_lock(&imx274->lock);
  1015. switch (sel->target) {
  1016. case V4L2_SEL_TGT_CROP:
  1017. sel->r = *src_crop;
  1018. break;
  1019. case V4L2_SEL_TGT_COMPOSE_BOUNDS:
  1020. sel->r.top = 0;
  1021. sel->r.left = 0;
  1022. sel->r.width = src_crop->width;
  1023. sel->r.height = src_crop->height;
  1024. break;
  1025. case V4L2_SEL_TGT_COMPOSE:
  1026. sel->r.top = 0;
  1027. sel->r.left = 0;
  1028. sel->r.width = src_fmt->width;
  1029. sel->r.height = src_fmt->height;
  1030. break;
  1031. default:
  1032. ret = -EINVAL;
  1033. }
  1034. mutex_unlock(&imx274->lock);
  1035. return ret;
  1036. }
  1037. static int imx274_set_selection_crop(struct stimx274 *imx274,
  1038. struct v4l2_subdev_state *sd_state,
  1039. struct v4l2_subdev_selection *sel)
  1040. {
  1041. struct v4l2_rect *tgt_crop;
  1042. struct v4l2_rect new_crop;
  1043. bool size_changed;
  1044. /*
  1045. * h_step could be 12 or 24 depending on the binning. But we
  1046. * won't know the binning until we choose the mode later in
  1047. * __imx274_change_compose(). Thus let's be safe and use the
  1048. * most conservative value in all cases.
  1049. */
  1050. const u32 h_step = 24;
  1051. new_crop.width = min_t(u32,
  1052. IMX274_ROUND(sel->r.width, h_step, sel->flags),
  1053. IMX274_MAX_WIDTH);
  1054. /* Constraint: HTRIMMING_END - HTRIMMING_START >= 144 */
  1055. if (new_crop.width < 144)
  1056. new_crop.width = 144;
  1057. new_crop.left = min_t(u32,
  1058. IMX274_ROUND(sel->r.left, h_step, 0),
  1059. IMX274_MAX_WIDTH - new_crop.width);
  1060. new_crop.height = min_t(u32,
  1061. IMX274_ROUND(sel->r.height, 2, sel->flags),
  1062. IMX274_MAX_HEIGHT);
  1063. new_crop.top = min_t(u32, IMX274_ROUND(sel->r.top, 2, 0),
  1064. IMX274_MAX_HEIGHT - new_crop.height);
  1065. sel->r = new_crop;
  1066. if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
  1067. tgt_crop = v4l2_subdev_state_get_crop(sd_state, 0);
  1068. else
  1069. tgt_crop = &imx274->crop;
  1070. mutex_lock(&imx274->lock);
  1071. size_changed = (new_crop.width != tgt_crop->width ||
  1072. new_crop.height != tgt_crop->height);
  1073. /* __imx274_change_compose needs the new size in *tgt_crop */
  1074. *tgt_crop = new_crop;
  1075. /* if crop size changed then reset the output image size */
  1076. if (size_changed)
  1077. __imx274_change_compose(imx274, sd_state, sel->which,
  1078. &new_crop.width, &new_crop.height,
  1079. sel->flags);
  1080. mutex_unlock(&imx274->lock);
  1081. return 0;
  1082. }
  1083. static int imx274_set_selection(struct v4l2_subdev *sd,
  1084. struct v4l2_subdev_state *sd_state,
  1085. struct v4l2_subdev_selection *sel)
  1086. {
  1087. struct stimx274 *imx274 = to_imx274(sd);
  1088. if (sel->pad != 0)
  1089. return -EINVAL;
  1090. if (sel->target == V4L2_SEL_TGT_CROP)
  1091. return imx274_set_selection_crop(imx274, sd_state, sel);
  1092. if (sel->target == V4L2_SEL_TGT_COMPOSE) {
  1093. int err;
  1094. mutex_lock(&imx274->lock);
  1095. err = __imx274_change_compose(imx274, sd_state, sel->which,
  1096. &sel->r.width, &sel->r.height,
  1097. sel->flags);
  1098. mutex_unlock(&imx274->lock);
  1099. /*
  1100. * __imx274_change_compose already set width and
  1101. * height in set->r, we still need to set top-left
  1102. */
  1103. if (!err) {
  1104. sel->r.top = 0;
  1105. sel->r.left = 0;
  1106. }
  1107. return err;
  1108. }
  1109. return -EINVAL;
  1110. }
  1111. static int imx274_apply_trimming(struct stimx274 *imx274)
  1112. {
  1113. u32 h_start;
  1114. u32 h_end;
  1115. u32 hmax;
  1116. u32 v_cut;
  1117. s32 v_pos;
  1118. u32 write_v_size;
  1119. u32 y_out_size;
  1120. int err;
  1121. h_start = imx274->crop.left + 12;
  1122. h_end = h_start + imx274->crop.width;
  1123. /* Use the minimum allowed value of HMAX */
  1124. /* Note: except in mode 1, (width / 16 + 23) is always < hmax_min */
  1125. /* Note: 260 is the minimum HMAX in all implemented modes */
  1126. hmax = max_t(u32, 260, (imx274->crop.width) / 16 + 23);
  1127. /* invert v_pos if VFLIP */
  1128. v_pos = imx274->ctrls.vflip->cur.val ?
  1129. (-imx274->crop.top / 2) : (imx274->crop.top / 2);
  1130. v_cut = (IMX274_MAX_HEIGHT - imx274->crop.height) / 2;
  1131. write_v_size = imx274->crop.height + 22;
  1132. y_out_size = imx274->crop.height;
  1133. err = imx274_write_mbreg(imx274, IMX274_HMAX_REG_LSB, hmax, 2);
  1134. if (!err)
  1135. err = imx274_write_mbreg(imx274, IMX274_HTRIM_EN_REG, 1, 1);
  1136. if (!err)
  1137. err = imx274_write_mbreg(imx274, IMX274_HTRIM_START_REG_LSB,
  1138. h_start, 2);
  1139. if (!err)
  1140. err = imx274_write_mbreg(imx274, IMX274_HTRIM_END_REG_LSB,
  1141. h_end, 2);
  1142. if (!err)
  1143. err = imx274_write_mbreg(imx274, IMX274_VWIDCUTEN_REG, 1, 1);
  1144. if (!err)
  1145. err = imx274_write_mbreg(imx274, IMX274_VWIDCUT_REG_LSB,
  1146. v_cut, 2);
  1147. if (!err)
  1148. err = imx274_write_mbreg(imx274, IMX274_VWINPOS_REG_LSB,
  1149. v_pos, 2);
  1150. if (!err)
  1151. err = imx274_write_mbreg(imx274, IMX274_WRITE_VSIZE_REG_LSB,
  1152. write_v_size, 2);
  1153. if (!err)
  1154. err = imx274_write_mbreg(imx274, IMX274_Y_OUT_SIZE_REG_LSB,
  1155. y_out_size, 2);
  1156. return err;
  1157. }
  1158. static int imx274_get_frame_interval(struct v4l2_subdev *sd,
  1159. struct v4l2_subdev_state *sd_state,
  1160. struct v4l2_subdev_frame_interval *fi)
  1161. {
  1162. struct stimx274 *imx274 = to_imx274(sd);
  1163. /*
  1164. * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
  1165. * subdev active state API.
  1166. */
  1167. if (fi->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  1168. return -EINVAL;
  1169. fi->interval = imx274->frame_interval;
  1170. dev_dbg(&imx274->client->dev, "%s frame rate = %d / %d\n",
  1171. __func__, imx274->frame_interval.numerator,
  1172. imx274->frame_interval.denominator);
  1173. return 0;
  1174. }
  1175. static int imx274_set_frame_interval(struct v4l2_subdev *sd,
  1176. struct v4l2_subdev_state *sd_state,
  1177. struct v4l2_subdev_frame_interval *fi)
  1178. {
  1179. struct stimx274 *imx274 = to_imx274(sd);
  1180. struct v4l2_ctrl *ctrl = imx274->ctrls.exposure;
  1181. int min, max, def;
  1182. int ret;
  1183. /*
  1184. * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
  1185. * subdev active state API.
  1186. */
  1187. if (fi->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  1188. return -EINVAL;
  1189. ret = pm_runtime_resume_and_get(&imx274->client->dev);
  1190. if (ret < 0)
  1191. return ret;
  1192. mutex_lock(&imx274->lock);
  1193. ret = __imx274_set_frame_interval(imx274, fi->interval);
  1194. if (!ret) {
  1195. fi->interval = imx274->frame_interval;
  1196. /*
  1197. * exposure time range is decided by frame interval
  1198. * need to update it after frame interval changes
  1199. */
  1200. min = IMX274_MIN_EXPOSURE_TIME;
  1201. max = fi->interval.numerator * 1000000
  1202. / fi->interval.denominator;
  1203. def = max;
  1204. ret = __v4l2_ctrl_modify_range(ctrl, min, max, 1, def);
  1205. if (ret) {
  1206. dev_err(&imx274->client->dev,
  1207. "Exposure ctrl range update failed\n");
  1208. goto unlock;
  1209. }
  1210. /* update exposure time accordingly */
  1211. imx274_set_exposure(imx274, ctrl->val);
  1212. dev_dbg(&imx274->client->dev, "set frame interval to %uus\n",
  1213. fi->interval.numerator * 1000000
  1214. / fi->interval.denominator);
  1215. }
  1216. unlock:
  1217. mutex_unlock(&imx274->lock);
  1218. pm_runtime_put(&imx274->client->dev);
  1219. return ret;
  1220. }
  1221. /**
  1222. * imx274_load_default - load default control values
  1223. * @priv: Pointer to device structure
  1224. *
  1225. * Return: 0 on success, errors otherwise
  1226. */
  1227. static void imx274_load_default(struct stimx274 *priv)
  1228. {
  1229. /* load default control values */
  1230. priv->frame_interval.numerator = 1;
  1231. priv->frame_interval.denominator = IMX274_DEF_FRAME_RATE;
  1232. priv->ctrls.exposure->val = 1000000 / IMX274_DEF_FRAME_RATE;
  1233. priv->ctrls.gain->val = IMX274_DEF_GAIN;
  1234. priv->ctrls.vflip->val = 0;
  1235. priv->ctrls.test_pattern->val = TEST_PATTERN_DISABLED;
  1236. }
  1237. /**
  1238. * imx274_s_stream - It is used to start/stop the streaming.
  1239. * @sd: V4L2 Sub device
  1240. * @on: Flag (True / False)
  1241. *
  1242. * This function controls the start or stop of streaming for the
  1243. * imx274 sensor.
  1244. *
  1245. * Return: 0 on success, errors otherwise
  1246. */
  1247. static int imx274_s_stream(struct v4l2_subdev *sd, int on)
  1248. {
  1249. struct stimx274 *imx274 = to_imx274(sd);
  1250. int ret = 0;
  1251. dev_dbg(&imx274->client->dev, "%s : %s, mode index = %td\n", __func__,
  1252. on ? "Stream Start" : "Stream Stop",
  1253. imx274->mode - &imx274_modes[0]);
  1254. mutex_lock(&imx274->lock);
  1255. if (on) {
  1256. ret = pm_runtime_resume_and_get(&imx274->client->dev);
  1257. if (ret < 0) {
  1258. mutex_unlock(&imx274->lock);
  1259. return ret;
  1260. }
  1261. /* load mode registers */
  1262. ret = imx274_mode_regs(imx274);
  1263. if (ret)
  1264. goto fail;
  1265. ret = imx274_apply_trimming(imx274);
  1266. if (ret)
  1267. goto fail;
  1268. /*
  1269. * update frame rate & exposure. if the last mode is different,
  1270. * HMAX could be changed. As the result, frame rate & exposure
  1271. * are changed.
  1272. * gain is not affected.
  1273. */
  1274. ret = __imx274_set_frame_interval(imx274,
  1275. imx274->frame_interval);
  1276. if (ret)
  1277. goto fail;
  1278. /* start stream */
  1279. ret = imx274_start_stream(imx274);
  1280. if (ret)
  1281. goto fail;
  1282. } else {
  1283. /* stop stream */
  1284. ret = imx274_write_table(imx274, imx274_stop);
  1285. if (ret)
  1286. goto fail;
  1287. pm_runtime_put(&imx274->client->dev);
  1288. }
  1289. mutex_unlock(&imx274->lock);
  1290. dev_dbg(&imx274->client->dev, "%s : Done\n", __func__);
  1291. return 0;
  1292. fail:
  1293. pm_runtime_put(&imx274->client->dev);
  1294. mutex_unlock(&imx274->lock);
  1295. dev_err(&imx274->client->dev, "s_stream failed\n");
  1296. return ret;
  1297. }
  1298. /*
  1299. * imx274_get_frame_length - Function for obtaining current frame length
  1300. * @priv: Pointer to device structure
  1301. * @val: Pointer to obtained value
  1302. *
  1303. * frame_length = vmax x (svr + 1), in unit of hmax.
  1304. *
  1305. * Return: 0 on success
  1306. */
  1307. static int imx274_get_frame_length(struct stimx274 *priv, u32 *val)
  1308. {
  1309. int err;
  1310. u32 svr;
  1311. u32 vmax;
  1312. err = imx274_read_mbreg(priv, IMX274_SVR_REG_LSB, &svr, 2);
  1313. if (err)
  1314. goto fail;
  1315. err = imx274_read_mbreg(priv, IMX274_VMAX_REG_3, &vmax, 3);
  1316. if (err)
  1317. goto fail;
  1318. *val = vmax * (svr + 1);
  1319. return 0;
  1320. fail:
  1321. dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
  1322. return err;
  1323. }
  1324. static int imx274_clamp_coarse_time(struct stimx274 *priv, u32 *val,
  1325. u32 *frame_length)
  1326. {
  1327. int err;
  1328. err = imx274_get_frame_length(priv, frame_length);
  1329. if (err)
  1330. return err;
  1331. if (*frame_length < priv->mode->min_frame_len)
  1332. *frame_length = priv->mode->min_frame_len;
  1333. *val = *frame_length - *val; /* convert to raw shr */
  1334. if (*val > *frame_length - IMX274_SHR_LIMIT_CONST)
  1335. *val = *frame_length - IMX274_SHR_LIMIT_CONST;
  1336. else if (*val < priv->mode->min_SHR)
  1337. *val = priv->mode->min_SHR;
  1338. return 0;
  1339. }
  1340. /*
  1341. * imx274_set_digital gain - Function called when setting digital gain
  1342. * @priv: Pointer to device structure
  1343. * @dgain: Value of digital gain.
  1344. *
  1345. * Digital gain has only 4 steps: 1x, 2x, 4x, and 8x
  1346. *
  1347. * Return: 0 on success
  1348. */
  1349. static int imx274_set_digital_gain(struct stimx274 *priv, u32 dgain)
  1350. {
  1351. u8 reg_val;
  1352. reg_val = ffs(dgain);
  1353. if (reg_val)
  1354. reg_val--;
  1355. reg_val = clamp(reg_val, (u8)0, (u8)3);
  1356. return imx274_write_reg(priv, IMX274_DIGITAL_GAIN_REG,
  1357. reg_val & IMX274_MASK_LSB_4_BITS);
  1358. }
  1359. /*
  1360. * imx274_set_gain - Function called when setting gain
  1361. * @priv: Pointer to device structure
  1362. * @val: Value of gain. the real value = val << IMX274_GAIN_SHIFT;
  1363. * @ctrl: v4l2 control pointer
  1364. *
  1365. * Set the gain based on input value.
  1366. * The caller should hold the mutex lock imx274->lock if necessary
  1367. *
  1368. * Return: 0 on success
  1369. */
  1370. static int imx274_set_gain(struct stimx274 *priv, struct v4l2_ctrl *ctrl)
  1371. {
  1372. int err;
  1373. u32 gain, analog_gain, digital_gain, gain_reg;
  1374. gain = (u32)(ctrl->val);
  1375. dev_dbg(&priv->client->dev,
  1376. "%s : input gain = %d.%d\n", __func__,
  1377. gain >> IMX274_GAIN_SHIFT,
  1378. ((gain & IMX274_GAIN_SHIFT_MASK) * 100) >> IMX274_GAIN_SHIFT);
  1379. if (gain > IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN)
  1380. gain = IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN;
  1381. else if (gain < IMX274_MIN_GAIN)
  1382. gain = IMX274_MIN_GAIN;
  1383. if (gain <= IMX274_MAX_ANALOG_GAIN)
  1384. digital_gain = 1;
  1385. else if (gain <= IMX274_MAX_ANALOG_GAIN * 2)
  1386. digital_gain = 2;
  1387. else if (gain <= IMX274_MAX_ANALOG_GAIN * 4)
  1388. digital_gain = 4;
  1389. else
  1390. digital_gain = IMX274_MAX_DIGITAL_GAIN;
  1391. analog_gain = gain / digital_gain;
  1392. dev_dbg(&priv->client->dev,
  1393. "%s : digital gain = %d, analog gain = %d.%d\n",
  1394. __func__, digital_gain, analog_gain >> IMX274_GAIN_SHIFT,
  1395. ((analog_gain & IMX274_GAIN_SHIFT_MASK) * 100)
  1396. >> IMX274_GAIN_SHIFT);
  1397. err = imx274_set_digital_gain(priv, digital_gain);
  1398. if (err)
  1399. goto fail;
  1400. /* convert to register value, refer to imx274 datasheet */
  1401. gain_reg = (u32)IMX274_GAIN_CONST -
  1402. (IMX274_GAIN_CONST << IMX274_GAIN_SHIFT) / analog_gain;
  1403. if (gain_reg > IMX274_GAIN_REG_MAX)
  1404. gain_reg = IMX274_GAIN_REG_MAX;
  1405. err = imx274_write_mbreg(priv, IMX274_ANALOG_GAIN_ADDR_LSB, gain_reg,
  1406. 2);
  1407. if (err)
  1408. goto fail;
  1409. if (IMX274_GAIN_CONST - gain_reg == 0) {
  1410. err = -EINVAL;
  1411. goto fail;
  1412. }
  1413. /* convert register value back to gain value */
  1414. ctrl->val = (IMX274_GAIN_CONST << IMX274_GAIN_SHIFT)
  1415. / (IMX274_GAIN_CONST - gain_reg) * digital_gain;
  1416. dev_dbg(&priv->client->dev,
  1417. "%s : GAIN control success, gain_reg = %d, new gain = %d\n",
  1418. __func__, gain_reg, ctrl->val);
  1419. return 0;
  1420. fail:
  1421. dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
  1422. return err;
  1423. }
  1424. /*
  1425. * imx274_set_coarse_time - Function called when setting SHR value
  1426. * @priv: Pointer to device structure
  1427. * @val: Value for exposure time in number of line_length, or [HMAX]
  1428. *
  1429. * Set SHR value based on input value.
  1430. *
  1431. * Return: 0 on success
  1432. */
  1433. static int imx274_set_coarse_time(struct stimx274 *priv, u32 *val)
  1434. {
  1435. int err;
  1436. u32 coarse_time, frame_length;
  1437. coarse_time = *val;
  1438. /* convert exposure_time to appropriate SHR value */
  1439. err = imx274_clamp_coarse_time(priv, &coarse_time, &frame_length);
  1440. if (err)
  1441. goto fail;
  1442. err = imx274_write_mbreg(priv, IMX274_SHR_REG_LSB, coarse_time, 2);
  1443. if (err)
  1444. goto fail;
  1445. *val = frame_length - coarse_time;
  1446. return 0;
  1447. fail:
  1448. dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
  1449. return err;
  1450. }
  1451. /*
  1452. * imx274_set_exposure - Function called when setting exposure time
  1453. * @priv: Pointer to device structure
  1454. * @val: Variable for exposure time, in the unit of micro-second
  1455. *
  1456. * Set exposure time based on input value.
  1457. * The caller should hold the mutex lock imx274->lock if necessary
  1458. *
  1459. * Return: 0 on success
  1460. */
  1461. static int imx274_set_exposure(struct stimx274 *priv, int val)
  1462. {
  1463. int err;
  1464. u32 hmax;
  1465. u32 coarse_time; /* exposure time in unit of line (HMAX)*/
  1466. dev_dbg(&priv->client->dev,
  1467. "%s : EXPOSURE control input = %d\n", __func__, val);
  1468. /* step 1: convert input exposure_time (val) into number of 1[HMAX] */
  1469. err = imx274_read_mbreg(priv, IMX274_HMAX_REG_LSB, &hmax, 2);
  1470. if (err)
  1471. goto fail;
  1472. if (hmax == 0) {
  1473. err = -EINVAL;
  1474. goto fail;
  1475. }
  1476. coarse_time = (IMX274_PIXCLK_CONST1 / IMX274_PIXCLK_CONST2 * val
  1477. - priv->mode->nocpiop) / hmax;
  1478. /* step 2: convert exposure_time into SHR value */
  1479. /* set SHR */
  1480. err = imx274_set_coarse_time(priv, &coarse_time);
  1481. if (err)
  1482. goto fail;
  1483. priv->ctrls.exposure->val =
  1484. (coarse_time * hmax + priv->mode->nocpiop)
  1485. / (IMX274_PIXCLK_CONST1 / IMX274_PIXCLK_CONST2);
  1486. dev_dbg(&priv->client->dev,
  1487. "%s : EXPOSURE control success\n", __func__);
  1488. return 0;
  1489. fail:
  1490. dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
  1491. return err;
  1492. }
  1493. /*
  1494. * imx274_set_vflip - Function called when setting vertical flip
  1495. * @priv: Pointer to device structure
  1496. * @val: Value for vflip setting
  1497. *
  1498. * Set vertical flip based on input value.
  1499. * val = 0: normal, no vertical flip
  1500. * val = 1: vertical flip enabled
  1501. * The caller should hold the mutex lock imx274->lock if necessary
  1502. *
  1503. * Return: 0 on success
  1504. */
  1505. static int imx274_set_vflip(struct stimx274 *priv, int val)
  1506. {
  1507. int err;
  1508. err = imx274_write_reg(priv, IMX274_VFLIP_REG, val);
  1509. if (err) {
  1510. dev_err(&priv->client->dev, "VFLIP control error\n");
  1511. return err;
  1512. }
  1513. dev_dbg(&priv->client->dev,
  1514. "%s : VFLIP control success\n", __func__);
  1515. return 0;
  1516. }
  1517. /*
  1518. * imx274_set_test_pattern - Function called when setting test pattern
  1519. * @priv: Pointer to device structure
  1520. * @val: Variable for test pattern
  1521. *
  1522. * Set to different test patterns based on input value.
  1523. *
  1524. * Return: 0 on success
  1525. */
  1526. static int imx274_set_test_pattern(struct stimx274 *priv, int val)
  1527. {
  1528. int err = 0;
  1529. if (val == TEST_PATTERN_DISABLED) {
  1530. err = imx274_write_table(priv, imx274_tp_disabled);
  1531. } else if (val <= TEST_PATTERN_V_COLOR_BARS) {
  1532. err = imx274_write_reg(priv, IMX274_TEST_PATTERN_REG, val - 1);
  1533. if (!err)
  1534. err = imx274_write_table(priv, imx274_tp_regs);
  1535. } else {
  1536. err = -EINVAL;
  1537. }
  1538. if (!err)
  1539. dev_dbg(&priv->client->dev,
  1540. "%s : TEST PATTERN control success\n", __func__);
  1541. else
  1542. dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
  1543. return err;
  1544. }
  1545. /*
  1546. * imx274_set_frame_length - Function called when setting frame length
  1547. * @priv: Pointer to device structure
  1548. * @val: Variable for frame length (= VMAX, i.e. vertical drive period length)
  1549. *
  1550. * Set frame length based on input value.
  1551. *
  1552. * Return: 0 on success
  1553. */
  1554. static int imx274_set_frame_length(struct stimx274 *priv, u32 val)
  1555. {
  1556. int err;
  1557. u32 frame_length;
  1558. dev_dbg(&priv->client->dev, "%s : input length = %d\n",
  1559. __func__, val);
  1560. frame_length = (u32)val;
  1561. err = imx274_write_mbreg(priv, IMX274_VMAX_REG_3, frame_length, 3);
  1562. if (err)
  1563. goto fail;
  1564. return 0;
  1565. fail:
  1566. dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
  1567. return err;
  1568. }
  1569. /*
  1570. * __imx274_set_frame_interval - Function called when setting frame interval
  1571. * @priv: Pointer to device structure
  1572. * @frame_interval: Variable for frame interval
  1573. *
  1574. * Change frame interval by updating VMAX value
  1575. * The caller should hold the mutex lock imx274->lock if necessary
  1576. *
  1577. * Return: 0 on success
  1578. */
  1579. static int __imx274_set_frame_interval(struct stimx274 *priv,
  1580. struct v4l2_fract frame_interval)
  1581. {
  1582. int err;
  1583. u32 frame_length, req_frame_rate;
  1584. u32 svr;
  1585. u32 hmax;
  1586. dev_dbg(&priv->client->dev, "%s: input frame interval = %d / %d",
  1587. __func__, frame_interval.numerator,
  1588. frame_interval.denominator);
  1589. if (frame_interval.numerator == 0 || frame_interval.denominator == 0) {
  1590. frame_interval.denominator = IMX274_DEF_FRAME_RATE;
  1591. frame_interval.numerator = 1;
  1592. }
  1593. req_frame_rate = (u32)(frame_interval.denominator
  1594. / frame_interval.numerator);
  1595. /* boundary check */
  1596. if (req_frame_rate > priv->mode->max_fps) {
  1597. frame_interval.numerator = 1;
  1598. frame_interval.denominator = priv->mode->max_fps;
  1599. } else if (req_frame_rate < IMX274_MIN_FRAME_RATE) {
  1600. frame_interval.numerator = 1;
  1601. frame_interval.denominator = IMX274_MIN_FRAME_RATE;
  1602. }
  1603. /*
  1604. * VMAX = 1/frame_rate x 72M / (SVR+1) / HMAX
  1605. * frame_length (i.e. VMAX) = (frame_interval) x 72M /(SVR+1) / HMAX
  1606. */
  1607. err = imx274_read_mbreg(priv, IMX274_SVR_REG_LSB, &svr, 2);
  1608. if (err)
  1609. goto fail;
  1610. dev_dbg(&priv->client->dev,
  1611. "%s : register SVR = %d\n", __func__, svr);
  1612. err = imx274_read_mbreg(priv, IMX274_HMAX_REG_LSB, &hmax, 2);
  1613. if (err)
  1614. goto fail;
  1615. dev_dbg(&priv->client->dev,
  1616. "%s : register HMAX = %d\n", __func__, hmax);
  1617. if (hmax == 0 || frame_interval.denominator == 0) {
  1618. err = -EINVAL;
  1619. goto fail;
  1620. }
  1621. frame_length = IMX274_PIXCLK_CONST1 / (svr + 1) / hmax
  1622. * frame_interval.numerator
  1623. / frame_interval.denominator;
  1624. err = imx274_set_frame_length(priv, frame_length);
  1625. if (err)
  1626. goto fail;
  1627. priv->frame_interval = frame_interval;
  1628. return 0;
  1629. fail:
  1630. dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
  1631. return err;
  1632. }
  1633. static int imx274_enum_mbus_code(struct v4l2_subdev *sd,
  1634. struct v4l2_subdev_state *sd_state,
  1635. struct v4l2_subdev_mbus_code_enum *code)
  1636. {
  1637. if (code->index > 0)
  1638. return -EINVAL;
  1639. /* only supported format in the driver is Raw 10 bits SRGGB */
  1640. code->code = MEDIA_BUS_FMT_SRGGB10_1X10;
  1641. return 0;
  1642. }
  1643. static const struct v4l2_subdev_pad_ops imx274_pad_ops = {
  1644. .enum_mbus_code = imx274_enum_mbus_code,
  1645. .get_fmt = imx274_get_fmt,
  1646. .set_fmt = imx274_set_fmt,
  1647. .get_selection = imx274_get_selection,
  1648. .set_selection = imx274_set_selection,
  1649. .get_frame_interval = imx274_get_frame_interval,
  1650. .set_frame_interval = imx274_set_frame_interval,
  1651. };
  1652. static const struct v4l2_subdev_video_ops imx274_video_ops = {
  1653. .s_stream = imx274_s_stream,
  1654. };
  1655. static const struct v4l2_subdev_ops imx274_subdev_ops = {
  1656. .pad = &imx274_pad_ops,
  1657. .video = &imx274_video_ops,
  1658. };
  1659. static const struct v4l2_ctrl_ops imx274_ctrl_ops = {
  1660. .s_ctrl = imx274_s_ctrl,
  1661. };
  1662. static const struct of_device_id imx274_of_id_table[] = {
  1663. { .compatible = "sony,imx274" },
  1664. { }
  1665. };
  1666. MODULE_DEVICE_TABLE(of, imx274_of_id_table);
  1667. static const struct i2c_device_id imx274_id[] = {
  1668. { "IMX274" },
  1669. { }
  1670. };
  1671. MODULE_DEVICE_TABLE(i2c, imx274_id);
  1672. static int imx274_fwnode_parse(struct device *dev)
  1673. {
  1674. struct fwnode_handle *endpoint;
  1675. /* Only CSI2 is supported */
  1676. struct v4l2_fwnode_endpoint ep = {
  1677. .bus_type = V4L2_MBUS_CSI2_DPHY
  1678. };
  1679. int ret;
  1680. endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
  1681. if (!endpoint) {
  1682. dev_err(dev, "Endpoint node not found\n");
  1683. return -EINVAL;
  1684. }
  1685. ret = v4l2_fwnode_endpoint_parse(endpoint, &ep);
  1686. fwnode_handle_put(endpoint);
  1687. if (ret == -ENXIO) {
  1688. dev_err(dev, "Unsupported bus type, should be CSI2\n");
  1689. return ret;
  1690. } else if (ret) {
  1691. dev_err(dev, "Parsing endpoint node failed %d\n", ret);
  1692. return ret;
  1693. }
  1694. /* Check number of data lanes, only 4 lanes supported */
  1695. if (ep.bus.mipi_csi2.num_data_lanes != 4) {
  1696. dev_err(dev, "Invalid data lanes: %d\n",
  1697. ep.bus.mipi_csi2.num_data_lanes);
  1698. return -EINVAL;
  1699. }
  1700. return 0;
  1701. }
  1702. static int imx274_probe(struct i2c_client *client)
  1703. {
  1704. struct v4l2_subdev *sd;
  1705. struct stimx274 *imx274;
  1706. struct device *dev = &client->dev;
  1707. int ret;
  1708. /* initialize imx274 */
  1709. imx274 = devm_kzalloc(dev, sizeof(*imx274), GFP_KERNEL);
  1710. if (!imx274)
  1711. return -ENOMEM;
  1712. mutex_init(&imx274->lock);
  1713. ret = imx274_fwnode_parse(dev);
  1714. if (ret)
  1715. return ret;
  1716. imx274->inck = devm_clk_get_optional(dev, "inck");
  1717. if (IS_ERR(imx274->inck))
  1718. return PTR_ERR(imx274->inck);
  1719. ret = imx274_regulators_get(dev, imx274);
  1720. if (ret) {
  1721. dev_err(dev, "Failed to get power regulators, err: %d\n", ret);
  1722. return ret;
  1723. }
  1724. /* initialize format */
  1725. imx274->mode = &imx274_modes[0];
  1726. imx274->crop.width = IMX274_MAX_WIDTH;
  1727. imx274->crop.height = IMX274_MAX_HEIGHT;
  1728. imx274->format.width = imx274->crop.width / imx274->mode->wbin_ratio;
  1729. imx274->format.height = imx274->crop.height / imx274->mode->hbin_ratio;
  1730. imx274->format.field = V4L2_FIELD_NONE;
  1731. imx274->format.code = MEDIA_BUS_FMT_SRGGB10_1X10;
  1732. imx274->format.colorspace = V4L2_COLORSPACE_SRGB;
  1733. imx274->frame_interval.numerator = 1;
  1734. imx274->frame_interval.denominator = IMX274_DEF_FRAME_RATE;
  1735. /* initialize regmap */
  1736. imx274->regmap = devm_regmap_init_i2c(client, &imx274_regmap_config);
  1737. if (IS_ERR(imx274->regmap)) {
  1738. dev_err(dev, "regmap init failed: %pe\n", imx274->regmap);
  1739. ret = -ENODEV;
  1740. goto err_regmap;
  1741. }
  1742. /* initialize subdevice */
  1743. imx274->client = client;
  1744. sd = &imx274->sd;
  1745. v4l2_i2c_subdev_init(sd, client, &imx274_subdev_ops);
  1746. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
  1747. /* initialize subdev media pad */
  1748. imx274->pad.flags = MEDIA_PAD_FL_SOURCE;
  1749. sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1750. ret = media_entity_pads_init(&sd->entity, 1, &imx274->pad);
  1751. if (ret < 0) {
  1752. dev_err(dev,
  1753. "%s : media entity init Failed %d\n", __func__, ret);
  1754. goto err_regmap;
  1755. }
  1756. /* initialize sensor reset gpio */
  1757. imx274->reset_gpio = devm_gpiod_get_optional(dev, "reset",
  1758. GPIOD_OUT_HIGH);
  1759. if (IS_ERR(imx274->reset_gpio)) {
  1760. ret = dev_err_probe(dev, PTR_ERR(imx274->reset_gpio),
  1761. "Reset GPIO not setup in DT\n");
  1762. goto err_me;
  1763. }
  1764. /* power on the sensor */
  1765. ret = imx274_power_on(dev);
  1766. if (ret < 0) {
  1767. dev_err(dev, "%s : imx274 power on failed\n", __func__);
  1768. goto err_me;
  1769. }
  1770. /* initialize controls */
  1771. ret = v4l2_ctrl_handler_init(&imx274->ctrls.handler, 4);
  1772. if (ret < 0) {
  1773. dev_err(dev, "%s : ctrl handler init Failed\n", __func__);
  1774. goto err_power_off;
  1775. }
  1776. imx274->ctrls.handler.lock = &imx274->lock;
  1777. /* add new controls */
  1778. imx274->ctrls.test_pattern = v4l2_ctrl_new_std_menu_items(
  1779. &imx274->ctrls.handler, &imx274_ctrl_ops,
  1780. V4L2_CID_TEST_PATTERN,
  1781. ARRAY_SIZE(tp_qmenu) - 1, 0, 0, tp_qmenu);
  1782. imx274->ctrls.gain = v4l2_ctrl_new_std(
  1783. &imx274->ctrls.handler,
  1784. &imx274_ctrl_ops,
  1785. V4L2_CID_GAIN, IMX274_MIN_GAIN,
  1786. IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN, 1,
  1787. IMX274_DEF_GAIN);
  1788. imx274->ctrls.exposure = v4l2_ctrl_new_std(
  1789. &imx274->ctrls.handler,
  1790. &imx274_ctrl_ops,
  1791. V4L2_CID_EXPOSURE, IMX274_MIN_EXPOSURE_TIME,
  1792. 1000000 / IMX274_DEF_FRAME_RATE, 1,
  1793. IMX274_MIN_EXPOSURE_TIME);
  1794. imx274->ctrls.vflip = v4l2_ctrl_new_std(
  1795. &imx274->ctrls.handler,
  1796. &imx274_ctrl_ops,
  1797. V4L2_CID_VFLIP, 0, 1, 1, 0);
  1798. imx274->sd.ctrl_handler = &imx274->ctrls.handler;
  1799. if (imx274->ctrls.handler.error) {
  1800. ret = imx274->ctrls.handler.error;
  1801. goto err_ctrls;
  1802. }
  1803. /* load default control values */
  1804. imx274_load_default(imx274);
  1805. /* register subdevice */
  1806. ret = v4l2_async_register_subdev(sd);
  1807. if (ret < 0) {
  1808. dev_err(dev, "%s : v4l2_async_register_subdev failed %d\n",
  1809. __func__, ret);
  1810. goto err_ctrls;
  1811. }
  1812. pm_runtime_set_active(dev);
  1813. pm_runtime_enable(dev);
  1814. pm_runtime_idle(dev);
  1815. dev_info(dev, "imx274 : imx274 probe success !\n");
  1816. return 0;
  1817. err_ctrls:
  1818. v4l2_ctrl_handler_free(&imx274->ctrls.handler);
  1819. err_power_off:
  1820. imx274_power_off(dev);
  1821. err_me:
  1822. media_entity_cleanup(&sd->entity);
  1823. err_regmap:
  1824. mutex_destroy(&imx274->lock);
  1825. return ret;
  1826. }
  1827. static void imx274_remove(struct i2c_client *client)
  1828. {
  1829. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1830. struct stimx274 *imx274 = to_imx274(sd);
  1831. pm_runtime_disable(&client->dev);
  1832. if (!pm_runtime_status_suspended(&client->dev))
  1833. imx274_power_off(&client->dev);
  1834. pm_runtime_set_suspended(&client->dev);
  1835. v4l2_async_unregister_subdev(sd);
  1836. v4l2_ctrl_handler_free(&imx274->ctrls.handler);
  1837. media_entity_cleanup(&sd->entity);
  1838. mutex_destroy(&imx274->lock);
  1839. }
  1840. static const struct dev_pm_ops imx274_pm_ops = {
  1841. SET_RUNTIME_PM_OPS(imx274_power_off, imx274_power_on, NULL)
  1842. };
  1843. static struct i2c_driver imx274_i2c_driver = {
  1844. .driver = {
  1845. .name = DRIVER_NAME,
  1846. .pm = &imx274_pm_ops,
  1847. .of_match_table = imx274_of_id_table,
  1848. },
  1849. .probe = imx274_probe,
  1850. .remove = imx274_remove,
  1851. .id_table = imx274_id,
  1852. };
  1853. module_i2c_driver(imx274_i2c_driver);
  1854. MODULE_AUTHOR("Leon Luo <leonl@leopardimaging.com>");
  1855. MODULE_DESCRIPTION("IMX274 CMOS Image Sensor driver");
  1856. MODULE_LICENSE("GPL v2");