imx274.c 50 KB

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