imx334.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Sony imx334 sensor driver
  4. *
  5. * Copyright (C) 2021 Intel Corporation
  6. */
  7. #include <linux/unaligned.h>
  8. #include <linux/clk.h>
  9. #include <linux/delay.h>
  10. #include <linux/i2c.h>
  11. #include <linux/module.h>
  12. #include <linux/pm_runtime.h>
  13. #include <media/v4l2-cci.h>
  14. #include <media/v4l2-ctrls.h>
  15. #include <media/v4l2-fwnode.h>
  16. #include <media/v4l2-subdev.h>
  17. /* Streaming Mode */
  18. #define IMX334_REG_MODE_SELECT CCI_REG8(0x3000)
  19. #define IMX334_MODE_STANDBY 0x01
  20. #define IMX334_MODE_STREAMING 0x00
  21. /* Lines per frame */
  22. #define IMX334_REG_VMAX CCI_REG24_LE(0x3030)
  23. #define IMX334_REG_HMAX CCI_REG16_LE(0x3034)
  24. #define IMX334_REG_OPB_SIZE_V CCI_REG8(0x304c)
  25. #define IMX334_REG_ADBIT CCI_REG8(0x3050)
  26. #define IMX334_REG_MDBIT CCI_REG8(0x319d)
  27. #define IMX334_REG_ADBIT1 CCI_REG16_LE(0x341c)
  28. #define IMX334_REG_Y_OUT_SIZE CCI_REG16_LE(0x3308)
  29. #define IMX334_REG_XVS_XHS_OUTSEL CCI_REG8(0x31a0)
  30. #define IMX334_REG_XVS_XHS_DRV CCI_REG8(0x31a1)
  31. /* Chip ID */
  32. #define IMX334_REG_ID CCI_REG8(0x3044)
  33. #define IMX334_ID 0x1e
  34. /* Exposure control */
  35. #define IMX334_REG_SHUTTER CCI_REG24_LE(0x3058)
  36. #define IMX334_EXPOSURE_MIN 1
  37. #define IMX334_EXPOSURE_OFFSET 5
  38. #define IMX334_EXPOSURE_STEP 1
  39. #define IMX334_EXPOSURE_DEFAULT 0x0648
  40. #define IMX334_REG_LANEMODE CCI_REG8(0x3a01)
  41. #define IMX334_CSI_4_LANE_MODE 3
  42. #define IMX334_CSI_8_LANE_MODE 7
  43. /* Window cropping Settings */
  44. #define IMX334_REG_AREA3_ST_ADR_1 CCI_REG16_LE(0x3074)
  45. #define IMX334_REG_AREA3_ST_ADR_2 CCI_REG16_LE(0x308e)
  46. #define IMX334_REG_UNREAD_PARAM5 CCI_REG16_LE(0x30b6)
  47. #define IMX334_REG_AREA3_WIDTH_1 CCI_REG16_LE(0x3076)
  48. #define IMX334_REG_AREA3_WIDTH_2 CCI_REG16_LE(0x3090)
  49. #define IMX334_REG_BLACK_OFSET_ADR CCI_REG16_LE(0x30c6)
  50. #define IMX334_REG_UNRD_LINE_MAX CCI_REG16_LE(0x30ce)
  51. #define IMX334_REG_UNREAD_ED_ADR CCI_REG16_LE(0x30d8)
  52. #define IMX334_REG_UNREAD_PARAM6 CCI_REG16_LE(0x3116)
  53. #define IMX334_REG_VREVERSE CCI_REG8(0x304f)
  54. #define IMX334_REG_HREVERSE CCI_REG8(0x304e)
  55. /* Binning Settings */
  56. #define IMX334_REG_HADD_VADD CCI_REG8(0x3199)
  57. #define IMX334_REG_VALID_EXPAND CCI_REG8(0x31dd)
  58. #define IMX334_REG_TCYCLE CCI_REG8(0x3300)
  59. /* Analog gain control */
  60. #define IMX334_REG_AGAIN CCI_REG16_LE(0x30e8)
  61. #define IMX334_AGAIN_MIN 0
  62. #define IMX334_AGAIN_MAX 240
  63. #define IMX334_AGAIN_STEP 1
  64. #define IMX334_AGAIN_DEFAULT 0
  65. /* Group hold register */
  66. #define IMX334_REG_HOLD CCI_REG8(0x3001)
  67. #define IMX334_REG_MASTER_MODE CCI_REG8(0x3002)
  68. #define IMX334_REG_WINMODE CCI_REG8(0x3018)
  69. #define IMX334_REG_HTRIMMING_START CCI_REG16_LE(0x302c)
  70. #define IMX334_REG_HNUM CCI_REG16_LE(0x302e)
  71. /* Input clock rate */
  72. #define IMX334_INCLK_RATE 24000000
  73. /* INCK Setting Register */
  74. #define IMX334_REG_BCWAIT_TIME CCI_REG8(0x300c)
  75. #define IMX334_REG_CPWAIT_TIME CCI_REG8(0x300d)
  76. #define IMX334_REG_INCKSEL1 CCI_REG16_LE(0x314c)
  77. #define IMX334_REG_INCKSEL2 CCI_REG8(0x315a)
  78. #define IMX334_REG_INCKSEL3 CCI_REG8(0x3168)
  79. #define IMX334_REG_INCKSEL4 CCI_REG8(0x316a)
  80. #define IMX334_REG_SYS_MODE CCI_REG8(0x319e)
  81. #define IMX334_REG_TCLKPOST CCI_REG16_LE(0x3a18)
  82. #define IMX334_REG_TCLKPREPARE CCI_REG16_LE(0x3a1a)
  83. #define IMX334_REG_TCLKTRAIL CCI_REG16_LE(0x3a1c)
  84. #define IMX334_REG_TCLKZERO CCI_REG16_LE(0x3a1e)
  85. #define IMX334_REG_THSPREPARE CCI_REG16_LE(0x3a20)
  86. #define IMX334_REG_THSZERO CCI_REG16_LE(0x3a22)
  87. #define IMX334_REG_THSTRAIL CCI_REG16_LE(0x3a24)
  88. #define IMX334_REG_THSEXIT CCI_REG16_LE(0x3a26)
  89. #define IMX334_REG_TPLX CCI_REG16_LE(0x3a28)
  90. /* CSI2 HW configuration */
  91. #define IMX334_LINK_FREQ_891M 891000000
  92. #define IMX334_LINK_FREQ_445M 445500000
  93. #define IMX334_NUM_DATA_LANES 4
  94. #define IMX334_REG_MIN 0x00
  95. #define IMX334_REG_MAX 0xfffff
  96. /* Test Pattern Control */
  97. #define IMX334_REG_TP CCI_REG8(0x329e)
  98. #define IMX334_TP_COLOR_HBARS 0xa
  99. #define IMX334_TP_COLOR_VBARS 0xb
  100. #define IMX334_TP_BLACK 0x0
  101. #define IMX334_TP_WHITE 0x1
  102. #define IMX334_TP_BLACK_GREY 0xc
  103. #define IMX334_TPG_EN_DOUT CCI_REG8(0x329c)
  104. #define IMX334_TP_ENABLE 0x1
  105. #define IMX334_TP_DISABLE 0x0
  106. #define IMX334_TPG_COLORW CCI_REG8(0x32a0)
  107. #define IMX334_TPG_COLORW_120P 0x13
  108. #define IMX334_TP_CLK_EN CCI_REG8(0x3148)
  109. #define IMX334_TP_CLK_EN_VAL 0x10
  110. #define IMX334_TP_CLK_DIS_VAL 0x0
  111. #define IMX334_DIG_CLP_MODE CCI_REG8(0x3280)
  112. /**
  113. * struct imx334_reg_list - imx334 sensor register list
  114. * @num_of_regs: Number of registers in the list
  115. * @regs: Pointer to register list
  116. */
  117. struct imx334_reg_list {
  118. u32 num_of_regs;
  119. const struct cci_reg_sequence *regs;
  120. };
  121. /**
  122. * struct imx334_mode - imx334 sensor mode structure
  123. * @width: Frame width
  124. * @height: Frame height
  125. * @hblank: Horizontal blanking in lines
  126. * @vblank: Vertical blanking in lines
  127. * @vblank_min: Minimal vertical blanking in lines
  128. * @vblank_max: Maximum vertical blanking in lines
  129. * @pclk: Sensor pixel clock
  130. * @link_freq_idx: Link frequency index
  131. * @reg_list: Register list for sensor mode
  132. */
  133. struct imx334_mode {
  134. u32 width;
  135. u32 height;
  136. u32 hblank;
  137. u32 vblank;
  138. u32 vblank_min;
  139. u32 vblank_max;
  140. u64 pclk;
  141. u32 link_freq_idx;
  142. struct imx334_reg_list reg_list;
  143. };
  144. /**
  145. * struct imx334 - imx334 sensor device structure
  146. * @dev: Pointer to generic device
  147. * @cci: CCI register map
  148. * @client: Pointer to i2c client
  149. * @sd: V4L2 sub-device
  150. * @pad: Media pad. Only one pad supported
  151. * @reset_gpio: Sensor reset gpio
  152. * @inclk: Sensor input clock
  153. * @ctrl_handler: V4L2 control handler
  154. * @link_freq_ctrl: Pointer to link frequency control
  155. * @pclk_ctrl: Pointer to pixel clock control
  156. * @hblank_ctrl: Pointer to horizontal blanking control
  157. * @vblank_ctrl: Pointer to vertical blanking control
  158. * @exp_ctrl: Pointer to exposure control
  159. * @again_ctrl: Pointer to analog gain control
  160. * @vblank: Vertical blanking in lines
  161. * @cur_mode: Pointer to current selected sensor mode
  162. * @link_freq_bitmap: Menu bitmap for link_freq_ctrl
  163. * @cur_code: current selected format code
  164. */
  165. struct imx334 {
  166. struct device *dev;
  167. struct regmap *cci;
  168. struct i2c_client *client;
  169. struct v4l2_subdev sd;
  170. struct media_pad pad;
  171. struct gpio_desc *reset_gpio;
  172. struct clk *inclk;
  173. struct v4l2_ctrl_handler ctrl_handler;
  174. struct v4l2_ctrl *link_freq_ctrl;
  175. struct v4l2_ctrl *pclk_ctrl;
  176. struct v4l2_ctrl *hblank_ctrl;
  177. struct v4l2_ctrl *vblank_ctrl;
  178. struct {
  179. struct v4l2_ctrl *exp_ctrl;
  180. struct v4l2_ctrl *again_ctrl;
  181. };
  182. u32 vblank;
  183. const struct imx334_mode *cur_mode;
  184. unsigned long link_freq_bitmap;
  185. u32 cur_code;
  186. };
  187. static const s64 link_freq[] = {
  188. IMX334_LINK_FREQ_891M,
  189. IMX334_LINK_FREQ_445M,
  190. };
  191. /* Sensor common mode registers values */
  192. static const struct cci_reg_sequence common_mode_regs[] = {
  193. { IMX334_REG_MODE_SELECT, IMX334_MODE_STANDBY },
  194. { IMX334_REG_WINMODE, 0x04 },
  195. { IMX334_REG_VMAX, 0x0008ca },
  196. { IMX334_REG_HMAX, 0x044c },
  197. { IMX334_REG_BLACK_OFSET_ADR, 0x0000 },
  198. { IMX334_REG_UNRD_LINE_MAX, 0x0000 },
  199. { IMX334_REG_OPB_SIZE_V, 0x00 },
  200. { IMX334_REG_HREVERSE, 0x00 },
  201. { IMX334_REG_VREVERSE, 0x00 },
  202. { IMX334_REG_UNREAD_PARAM5, 0x0000 },
  203. { IMX334_REG_UNREAD_PARAM6, 0x0008 },
  204. { IMX334_REG_XVS_XHS_OUTSEL, 0x20 },
  205. { IMX334_REG_XVS_XHS_DRV, 0x0f },
  206. { IMX334_REG_BCWAIT_TIME, 0x3b },
  207. { IMX334_REG_CPWAIT_TIME, 0x2a },
  208. { IMX334_REG_INCKSEL1, 0x0129 },
  209. { IMX334_REG_INCKSEL2, 0x06 },
  210. { IMX334_REG_INCKSEL3, 0xa0 },
  211. { IMX334_REG_INCKSEL4, 0x7e },
  212. { IMX334_REG_SYS_MODE, 0x02 },
  213. { IMX334_REG_HADD_VADD, 0x00 },
  214. { IMX334_REG_VALID_EXPAND, 0x03 },
  215. { IMX334_REG_TCYCLE, 0x00 },
  216. { IMX334_REG_TCLKPOST, 0x007f },
  217. { IMX334_REG_TCLKPREPARE, 0x0037 },
  218. { IMX334_REG_TCLKTRAIL, 0x0037 },
  219. { IMX334_REG_TCLKZERO, 0xf7 },
  220. { IMX334_REG_THSPREPARE, 0x002f },
  221. { CCI_REG8(0x3078), 0x02 },
  222. { CCI_REG8(0x3079), 0x00 },
  223. { CCI_REG8(0x307a), 0x00 },
  224. { CCI_REG8(0x307b), 0x00 },
  225. { CCI_REG8(0x3080), 0x02 },
  226. { CCI_REG8(0x3081), 0x00 },
  227. { CCI_REG8(0x3082), 0x00 },
  228. { CCI_REG8(0x3083), 0x00 },
  229. { CCI_REG8(0x3088), 0x02 },
  230. { CCI_REG8(0x3094), 0x00 },
  231. { CCI_REG8(0x3095), 0x00 },
  232. { CCI_REG8(0x3096), 0x00 },
  233. { CCI_REG8(0x309b), 0x02 },
  234. { CCI_REG8(0x309c), 0x00 },
  235. { CCI_REG8(0x309d), 0x00 },
  236. { CCI_REG8(0x309e), 0x00 },
  237. { CCI_REG8(0x30a4), 0x00 },
  238. { CCI_REG8(0x30a5), 0x00 },
  239. { CCI_REG8(0x3288), 0x21 },
  240. { CCI_REG8(0x328a), 0x02 },
  241. { CCI_REG8(0x3414), 0x05 },
  242. { CCI_REG8(0x3416), 0x18 },
  243. { CCI_REG8(0x35Ac), 0x0e },
  244. { CCI_REG8(0x3648), 0x01 },
  245. { CCI_REG8(0x364a), 0x04 },
  246. { CCI_REG8(0x364c), 0x04 },
  247. { CCI_REG8(0x3678), 0x01 },
  248. { CCI_REG8(0x367c), 0x31 },
  249. { CCI_REG8(0x367e), 0x31 },
  250. { CCI_REG8(0x3708), 0x02 },
  251. { CCI_REG8(0x3714), 0x01 },
  252. { CCI_REG8(0x3715), 0x02 },
  253. { CCI_REG8(0x3716), 0x02 },
  254. { CCI_REG8(0x3717), 0x02 },
  255. { CCI_REG8(0x371c), 0x3d },
  256. { CCI_REG8(0x371d), 0x3f },
  257. { CCI_REG8(0x372c), 0x00 },
  258. { CCI_REG8(0x372d), 0x00 },
  259. { CCI_REG8(0x372e), 0x46 },
  260. { CCI_REG8(0x372f), 0x00 },
  261. { CCI_REG8(0x3730), 0x89 },
  262. { CCI_REG8(0x3731), 0x00 },
  263. { CCI_REG8(0x3732), 0x08 },
  264. { CCI_REG8(0x3733), 0x01 },
  265. { CCI_REG8(0x3734), 0xfe },
  266. { CCI_REG8(0x3735), 0x05 },
  267. { CCI_REG8(0x375d), 0x00 },
  268. { CCI_REG8(0x375e), 0x00 },
  269. { CCI_REG8(0x375f), 0x61 },
  270. { CCI_REG8(0x3760), 0x06 },
  271. { CCI_REG8(0x3768), 0x1b },
  272. { CCI_REG8(0x3769), 0x1b },
  273. { CCI_REG8(0x376a), 0x1a },
  274. { CCI_REG8(0x376b), 0x19 },
  275. { CCI_REG8(0x376c), 0x18 },
  276. { CCI_REG8(0x376d), 0x14 },
  277. { CCI_REG8(0x376e), 0x0f },
  278. { CCI_REG8(0x3776), 0x00 },
  279. { CCI_REG8(0x3777), 0x00 },
  280. { CCI_REG8(0x3778), 0x46 },
  281. { CCI_REG8(0x3779), 0x00 },
  282. { CCI_REG8(0x377a), 0x08 },
  283. { CCI_REG8(0x377b), 0x01 },
  284. { CCI_REG8(0x377c), 0x45 },
  285. { CCI_REG8(0x377d), 0x01 },
  286. { CCI_REG8(0x377e), 0x23 },
  287. { CCI_REG8(0x377f), 0x02 },
  288. { CCI_REG8(0x3780), 0xd9 },
  289. { CCI_REG8(0x3781), 0x03 },
  290. { CCI_REG8(0x3782), 0xf5 },
  291. { CCI_REG8(0x3783), 0x06 },
  292. { CCI_REG8(0x3784), 0xa5 },
  293. { CCI_REG8(0x3788), 0x0f },
  294. { CCI_REG8(0x378a), 0xd9 },
  295. { CCI_REG8(0x378b), 0x03 },
  296. { CCI_REG8(0x378c), 0xeb },
  297. { CCI_REG8(0x378d), 0x05 },
  298. { CCI_REG8(0x378e), 0x87 },
  299. { CCI_REG8(0x378f), 0x06 },
  300. { CCI_REG8(0x3790), 0xf5 },
  301. { CCI_REG8(0x3792), 0x43 },
  302. { CCI_REG8(0x3794), 0x7a },
  303. { CCI_REG8(0x3796), 0xa1 },
  304. { CCI_REG8(0x37b0), 0x37 },
  305. { CCI_REG8(0x3e04), 0x0e },
  306. { IMX334_REG_AGAIN, 0x0050 },
  307. { IMX334_REG_MASTER_MODE, 0x00 },
  308. };
  309. /* Sensor mode registers for 640x480@30fps */
  310. static const struct cci_reg_sequence mode_640x480_regs[] = {
  311. { IMX334_REG_HTRIMMING_START, 0x0670 },
  312. { IMX334_REG_HNUM, 0x0280 },
  313. { IMX334_REG_AREA3_ST_ADR_1, 0x0748 },
  314. { IMX334_REG_AREA3_ST_ADR_2, 0x0749 },
  315. { IMX334_REG_AREA3_WIDTH_1, 0x01e0 },
  316. { IMX334_REG_AREA3_WIDTH_2, 0x01e0 },
  317. { IMX334_REG_Y_OUT_SIZE, 0x01e0 },
  318. { IMX334_REG_UNREAD_ED_ADR, 0x0b30 },
  319. };
  320. /* Sensor mode registers for 1280x720@30fps */
  321. static const struct cci_reg_sequence mode_1280x720_regs[] = {
  322. { IMX334_REG_HTRIMMING_START, 0x0530 },
  323. { IMX334_REG_HNUM, 0x0500 },
  324. { IMX334_REG_AREA3_ST_ADR_1, 0x0384 },
  325. { IMX334_REG_AREA3_ST_ADR_2, 0x0385 },
  326. { IMX334_REG_AREA3_WIDTH_1, 0x02d0 },
  327. { IMX334_REG_AREA3_WIDTH_2, 0x02d0 },
  328. { IMX334_REG_Y_OUT_SIZE, 0x02d0 },
  329. { IMX334_REG_UNREAD_ED_ADR, 0x0b30 },
  330. };
  331. /* Sensor mode registers for 1920x1080@30fps */
  332. static const struct cci_reg_sequence mode_1920x1080_regs[] = {
  333. { IMX334_REG_HTRIMMING_START, 0x03f0 },
  334. { IMX334_REG_HNUM, 0x0780 },
  335. { IMX334_REG_AREA3_ST_ADR_1, 0x02cc },
  336. { IMX334_REG_AREA3_ST_ADR_2, 0x02cd },
  337. { IMX334_REG_AREA3_WIDTH_1, 0x0438 },
  338. { IMX334_REG_AREA3_WIDTH_2, 0x0438 },
  339. { IMX334_REG_Y_OUT_SIZE, 0x0438 },
  340. { IMX334_REG_UNREAD_ED_ADR, 0x0a18 },
  341. };
  342. /* Sensor mode registers for 3840x2160@30fps */
  343. static const struct cci_reg_sequence mode_3840x2160_regs[] = {
  344. { IMX334_REG_HMAX, 0x0226 },
  345. { IMX334_REG_INCKSEL2, 0x02 },
  346. { IMX334_REG_HTRIMMING_START, 0x003c },
  347. { IMX334_REG_HNUM, 0x0f00 },
  348. { IMX334_REG_AREA3_ST_ADR_1, 0x00b0 },
  349. { IMX334_REG_AREA3_ST_ADR_2, 0x00b1 },
  350. { IMX334_REG_UNREAD_ED_ADR, 0x1220 },
  351. { IMX334_REG_AREA3_WIDTH_1, 0x0870 },
  352. { IMX334_REG_AREA3_WIDTH_2, 0x0870 },
  353. { IMX334_REG_Y_OUT_SIZE, 0x0870 },
  354. { IMX334_REG_SYS_MODE, 0x0100 },
  355. { IMX334_REG_TCLKPOST, 0x00bf },
  356. { IMX334_REG_TCLKPREPARE, 0x0067 },
  357. { IMX334_REG_TCLKTRAIL, 0x006f },
  358. { IMX334_REG_TCLKZERO, 0x1d7 },
  359. { IMX334_REG_THSPREPARE, 0x006f },
  360. { IMX334_REG_THSZERO, 0x00cf },
  361. { IMX334_REG_THSTRAIL, 0x006f },
  362. { IMX334_REG_THSEXIT, 0x00b7 },
  363. { IMX334_REG_TPLX, 0x005f },
  364. };
  365. static const char * const imx334_test_pattern_menu[] = {
  366. "Disabled",
  367. "Vertical Color Bars",
  368. "Horizontal Color Bars",
  369. "Black and Grey Bars",
  370. "Black Color",
  371. "White Color",
  372. };
  373. static const int imx334_test_pattern_val[] = {
  374. IMX334_TP_DISABLE,
  375. IMX334_TP_COLOR_HBARS,
  376. IMX334_TP_COLOR_VBARS,
  377. IMX334_TP_BLACK_GREY,
  378. IMX334_TP_BLACK,
  379. IMX334_TP_WHITE,
  380. };
  381. static const struct cci_reg_sequence raw10_framefmt_regs[] = {
  382. { IMX334_REG_ADBIT, 0x00 },
  383. { IMX334_REG_MDBIT, 0x00 },
  384. { IMX334_REG_ADBIT1, 0x01ff },
  385. };
  386. static const struct cci_reg_sequence raw12_framefmt_regs[] = {
  387. { IMX334_REG_ADBIT, 0x01 },
  388. { IMX334_REG_MDBIT, 0x01 },
  389. { IMX334_REG_ADBIT1, 0x0047 },
  390. };
  391. static const u32 imx334_mbus_codes[] = {
  392. MEDIA_BUS_FMT_SRGGB12_1X12,
  393. MEDIA_BUS_FMT_SRGGB10_1X10,
  394. };
  395. /* Supported sensor mode configurations */
  396. static const struct imx334_mode supported_modes[] = {
  397. {
  398. .width = 3840,
  399. .height = 2160,
  400. .hblank = 560,
  401. .vblank = 2340,
  402. .vblank_min = 90,
  403. .vblank_max = 132840,
  404. .pclk = 594000000,
  405. .link_freq_idx = 0,
  406. .reg_list = {
  407. .num_of_regs = ARRAY_SIZE(mode_3840x2160_regs),
  408. .regs = mode_3840x2160_regs,
  409. },
  410. }, {
  411. .width = 1920,
  412. .height = 1080,
  413. .hblank = 2480,
  414. .vblank = 1170,
  415. .vblank_min = 45,
  416. .vblank_max = 132840,
  417. .pclk = 297000000,
  418. .link_freq_idx = 1,
  419. .reg_list = {
  420. .num_of_regs = ARRAY_SIZE(mode_1920x1080_regs),
  421. .regs = mode_1920x1080_regs,
  422. },
  423. }, {
  424. .width = 1280,
  425. .height = 720,
  426. .hblank = 2480,
  427. .vblank = 1170,
  428. .vblank_min = 45,
  429. .vblank_max = 132840,
  430. .pclk = 297000000,
  431. .link_freq_idx = 1,
  432. .reg_list = {
  433. .num_of_regs = ARRAY_SIZE(mode_1280x720_regs),
  434. .regs = mode_1280x720_regs,
  435. },
  436. }, {
  437. .width = 640,
  438. .height = 480,
  439. .hblank = 2480,
  440. .vblank = 1170,
  441. .vblank_min = 45,
  442. .vblank_max = 132840,
  443. .pclk = 297000000,
  444. .link_freq_idx = 1,
  445. .reg_list = {
  446. .num_of_regs = ARRAY_SIZE(mode_640x480_regs),
  447. .regs = mode_640x480_regs,
  448. },
  449. },
  450. };
  451. /**
  452. * to_imx334() - imv334 V4L2 sub-device to imx334 device.
  453. * @subdev: pointer to imx334 V4L2 sub-device
  454. *
  455. * Return: pointer to imx334 device
  456. */
  457. static inline struct imx334 *to_imx334(struct v4l2_subdev *subdev)
  458. {
  459. return container_of(subdev, struct imx334, sd);
  460. }
  461. /**
  462. * imx334_update_controls() - Update control ranges based on streaming mode
  463. * @imx334: pointer to imx334 device
  464. * @mode: pointer to imx334_mode sensor mode
  465. *
  466. * Return: 0 if successful, error code otherwise.
  467. */
  468. static int imx334_update_controls(struct imx334 *imx334,
  469. const struct imx334_mode *mode)
  470. {
  471. int ret;
  472. ret = __v4l2_ctrl_s_ctrl(imx334->link_freq_ctrl, mode->link_freq_idx);
  473. if (ret)
  474. return ret;
  475. ret = __v4l2_ctrl_modify_range(imx334->pclk_ctrl, mode->pclk,
  476. mode->pclk, 1, mode->pclk);
  477. if (ret)
  478. return ret;
  479. ret = __v4l2_ctrl_modify_range(imx334->hblank_ctrl, mode->hblank,
  480. mode->hblank, 1, mode->hblank);
  481. if (ret)
  482. return ret;
  483. ret = __v4l2_ctrl_modify_range(imx334->vblank_ctrl, mode->vblank_min,
  484. mode->vblank_max, 1, mode->vblank);
  485. if (ret)
  486. return ret;
  487. return __v4l2_ctrl_s_ctrl(imx334->vblank_ctrl, mode->vblank);
  488. }
  489. /**
  490. * imx334_update_exp_gain() - Set updated exposure and gain
  491. * @imx334: pointer to imx334 device
  492. * @exposure: updated exposure value
  493. * @gain: updated analog gain value
  494. *
  495. * Return: 0 if successful, error code otherwise.
  496. */
  497. static int imx334_update_exp_gain(struct imx334 *imx334, u32 exposure, u32 gain)
  498. {
  499. u32 lpfr, shutter;
  500. int ret_hold;
  501. int ret = 0;
  502. lpfr = imx334->vblank + imx334->cur_mode->height;
  503. shutter = lpfr - exposure;
  504. dev_dbg(imx334->dev, "Set long exp %u analog gain %u sh0 %u lpfr %u\n",
  505. exposure, gain, shutter, lpfr);
  506. cci_write(imx334->cci, IMX334_REG_HOLD, 1, &ret);
  507. cci_write(imx334->cci, IMX334_REG_VMAX, lpfr, &ret);
  508. cci_write(imx334->cci, IMX334_REG_SHUTTER, shutter, &ret);
  509. cci_write(imx334->cci, IMX334_REG_AGAIN, gain, &ret);
  510. ret_hold = cci_write(imx334->cci, IMX334_REG_HOLD, 0, NULL);
  511. if (ret_hold)
  512. return ret_hold;
  513. return ret;
  514. }
  515. /**
  516. * imx334_set_ctrl() - Set subdevice control
  517. * @ctrl: pointer to v4l2_ctrl structure
  518. *
  519. * Supported controls:
  520. * - V4L2_CID_VBLANK
  521. * - cluster controls:
  522. * - V4L2_CID_ANALOGUE_GAIN
  523. * - V4L2_CID_EXPOSURE
  524. *
  525. * Return: 0 if successful, error code otherwise.
  526. */
  527. static int imx334_set_ctrl(struct v4l2_ctrl *ctrl)
  528. {
  529. struct imx334 *imx334 =
  530. container_of(ctrl->handler, struct imx334, ctrl_handler);
  531. u32 analog_gain;
  532. u32 exposure;
  533. int ret;
  534. if (ctrl->id == V4L2_CID_VBLANK) {
  535. imx334->vblank = imx334->vblank_ctrl->val;
  536. dev_dbg(imx334->dev, "Received vblank %u, new lpfr %u\n",
  537. imx334->vblank,
  538. imx334->vblank + imx334->cur_mode->height);
  539. ret = __v4l2_ctrl_modify_range(imx334->exp_ctrl,
  540. IMX334_EXPOSURE_MIN,
  541. imx334->vblank +
  542. imx334->cur_mode->height -
  543. IMX334_EXPOSURE_OFFSET,
  544. 1, IMX334_EXPOSURE_DEFAULT);
  545. if (ret)
  546. return ret;
  547. }
  548. /* Set controls only if sensor is in power on state */
  549. if (!pm_runtime_get_if_in_use(imx334->dev))
  550. return 0;
  551. switch (ctrl->id) {
  552. case V4L2_CID_VBLANK:
  553. exposure = imx334->exp_ctrl->val;
  554. analog_gain = imx334->again_ctrl->val;
  555. ret = imx334_update_exp_gain(imx334, exposure, analog_gain);
  556. break;
  557. case V4L2_CID_EXPOSURE:
  558. exposure = ctrl->val;
  559. analog_gain = imx334->again_ctrl->val;
  560. dev_dbg(imx334->dev, "Received exp %u analog gain %u\n",
  561. exposure, analog_gain);
  562. ret = imx334_update_exp_gain(imx334, exposure, analog_gain);
  563. break;
  564. case V4L2_CID_PIXEL_RATE:
  565. case V4L2_CID_LINK_FREQ:
  566. case V4L2_CID_HBLANK:
  567. ret = 0;
  568. break;
  569. case V4L2_CID_TEST_PATTERN:
  570. if (ctrl->val) {
  571. cci_write(imx334->cci, IMX334_TP_CLK_EN,
  572. IMX334_TP_CLK_EN_VAL, NULL);
  573. cci_write(imx334->cci, IMX334_DIG_CLP_MODE, 0x0, NULL);
  574. cci_write(imx334->cci, IMX334_TPG_COLORW,
  575. IMX334_TPG_COLORW_120P, NULL);
  576. cci_write(imx334->cci, IMX334_REG_TP,
  577. imx334_test_pattern_val[ctrl->val], NULL);
  578. cci_write(imx334->cci, IMX334_TPG_EN_DOUT,
  579. IMX334_TP_ENABLE, NULL);
  580. } else {
  581. cci_write(imx334->cci, IMX334_DIG_CLP_MODE, 0x1, NULL);
  582. cci_write(imx334->cci, IMX334_TP_CLK_EN,
  583. IMX334_TP_CLK_DIS_VAL, NULL);
  584. cci_write(imx334->cci, IMX334_TPG_EN_DOUT,
  585. IMX334_TP_DISABLE, NULL);
  586. }
  587. ret = 0;
  588. break;
  589. default:
  590. dev_err(imx334->dev, "Invalid control %d\n", ctrl->id);
  591. ret = -EINVAL;
  592. }
  593. pm_runtime_put(imx334->dev);
  594. return ret;
  595. }
  596. /* V4l2 subdevice control ops*/
  597. static const struct v4l2_ctrl_ops imx334_ctrl_ops = {
  598. .s_ctrl = imx334_set_ctrl,
  599. };
  600. static int imx334_get_format_code(struct imx334 *imx334, u32 code)
  601. {
  602. unsigned int i;
  603. for (i = 0; i < ARRAY_SIZE(imx334_mbus_codes); i++) {
  604. if (imx334_mbus_codes[i] == code)
  605. return imx334_mbus_codes[i];
  606. }
  607. return imx334_mbus_codes[0];
  608. }
  609. /**
  610. * imx334_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes
  611. * @sd: pointer to imx334 V4L2 sub-device structure
  612. * @sd_state: V4L2 sub-device state
  613. * @code: V4L2 sub-device code enumeration need to be filled
  614. *
  615. * Return: 0 if successful, error code otherwise.
  616. */
  617. static int imx334_enum_mbus_code(struct v4l2_subdev *sd,
  618. struct v4l2_subdev_state *sd_state,
  619. struct v4l2_subdev_mbus_code_enum *code)
  620. {
  621. if (code->index >= ARRAY_SIZE(imx334_mbus_codes))
  622. return -EINVAL;
  623. code->code = imx334_mbus_codes[code->index];
  624. return 0;
  625. }
  626. /**
  627. * imx334_enum_frame_size() - Enumerate V4L2 sub-device frame sizes
  628. * @sd: pointer to imx334 V4L2 sub-device structure
  629. * @sd_state: V4L2 sub-device state
  630. * @fsize: V4L2 sub-device size enumeration need to be filled
  631. *
  632. * Return: 0 if successful, error code otherwise.
  633. */
  634. static int imx334_enum_frame_size(struct v4l2_subdev *sd,
  635. struct v4l2_subdev_state *sd_state,
  636. struct v4l2_subdev_frame_size_enum *fsize)
  637. {
  638. struct imx334 *imx334 = to_imx334(sd);
  639. u32 code;
  640. if (fsize->index >= ARRAY_SIZE(supported_modes))
  641. return -EINVAL;
  642. code = imx334_get_format_code(imx334, fsize->code);
  643. if (fsize->code != code)
  644. return -EINVAL;
  645. fsize->min_width = supported_modes[fsize->index].width;
  646. fsize->max_width = fsize->min_width;
  647. fsize->min_height = supported_modes[fsize->index].height;
  648. fsize->max_height = fsize->min_height;
  649. return 0;
  650. }
  651. /**
  652. * imx334_fill_pad_format() - Fill subdevice pad format
  653. * from selected sensor mode
  654. * @imx334: pointer to imx334 device
  655. * @mode: pointer to imx334_mode sensor mode
  656. * @fmt: V4L2 sub-device format need to be filled
  657. */
  658. static void imx334_fill_pad_format(struct imx334 *imx334,
  659. const struct imx334_mode *mode,
  660. struct v4l2_subdev_format *fmt)
  661. {
  662. fmt->format.width = mode->width;
  663. fmt->format.height = mode->height;
  664. fmt->format.field = V4L2_FIELD_NONE;
  665. fmt->format.colorspace = V4L2_COLORSPACE_RAW;
  666. fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  667. fmt->format.quantization = V4L2_QUANTIZATION_DEFAULT;
  668. fmt->format.xfer_func = V4L2_XFER_FUNC_NONE;
  669. }
  670. /**
  671. * imx334_get_pad_format() - Get subdevice pad format
  672. * @sd: pointer to imx334 V4L2 sub-device structure
  673. * @sd_state: V4L2 sub-device state
  674. * @fmt: V4L2 sub-device format need to be set
  675. *
  676. * Return: 0 if successful, error code otherwise.
  677. */
  678. static int imx334_get_pad_format(struct v4l2_subdev *sd,
  679. struct v4l2_subdev_state *sd_state,
  680. struct v4l2_subdev_format *fmt)
  681. {
  682. struct imx334 *imx334 = to_imx334(sd);
  683. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  684. struct v4l2_mbus_framefmt *framefmt;
  685. framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad);
  686. fmt->format = *framefmt;
  687. } else {
  688. fmt->format.code = imx334->cur_code;
  689. imx334_fill_pad_format(imx334, imx334->cur_mode, fmt);
  690. }
  691. return 0;
  692. }
  693. /**
  694. * imx334_set_pad_format() - Set subdevice pad format
  695. * @sd: pointer to imx334 V4L2 sub-device structure
  696. * @sd_state: V4L2 sub-device state
  697. * @fmt: V4L2 sub-device format need to be set
  698. *
  699. * Return: 0 if successful, error code otherwise.
  700. */
  701. static int imx334_set_pad_format(struct v4l2_subdev *sd,
  702. struct v4l2_subdev_state *sd_state,
  703. struct v4l2_subdev_format *fmt)
  704. {
  705. struct imx334 *imx334 = to_imx334(sd);
  706. const struct imx334_mode *mode;
  707. int ret = 0;
  708. mode = v4l2_find_nearest_size(supported_modes,
  709. ARRAY_SIZE(supported_modes),
  710. width, height,
  711. fmt->format.width, fmt->format.height);
  712. imx334_fill_pad_format(imx334, mode, fmt);
  713. fmt->format.code = imx334_get_format_code(imx334, fmt->format.code);
  714. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  715. struct v4l2_mbus_framefmt *framefmt;
  716. framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad);
  717. *framefmt = fmt->format;
  718. } else if (imx334->cur_mode != mode || imx334->cur_code != fmt->format.code) {
  719. imx334->cur_code = fmt->format.code;
  720. ret = imx334_update_controls(imx334, mode);
  721. if (!ret)
  722. imx334->cur_mode = mode;
  723. }
  724. return ret;
  725. }
  726. /**
  727. * imx334_init_state() - Initialize sub-device state
  728. * @sd: pointer to imx334 V4L2 sub-device structure
  729. * @sd_state: V4L2 sub-device state
  730. *
  731. * Return: 0 if successful, error code otherwise.
  732. */
  733. static int imx334_init_state(struct v4l2_subdev *sd,
  734. struct v4l2_subdev_state *sd_state)
  735. {
  736. struct imx334 *imx334 = to_imx334(sd);
  737. struct v4l2_subdev_format fmt = { 0 };
  738. fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
  739. imx334_fill_pad_format(imx334, imx334->cur_mode, &fmt);
  740. __v4l2_ctrl_modify_range(imx334->link_freq_ctrl, 0,
  741. __fls(imx334->link_freq_bitmap),
  742. ~(imx334->link_freq_bitmap),
  743. __ffs(imx334->link_freq_bitmap));
  744. return imx334_set_pad_format(sd, sd_state, &fmt);
  745. }
  746. static int imx334_set_framefmt(struct imx334 *imx334)
  747. {
  748. switch (imx334->cur_code) {
  749. case MEDIA_BUS_FMT_SRGGB10_1X10:
  750. return cci_multi_reg_write(imx334->cci, raw10_framefmt_regs,
  751. ARRAY_SIZE(raw10_framefmt_regs), NULL);
  752. case MEDIA_BUS_FMT_SRGGB12_1X12:
  753. return cci_multi_reg_write(imx334->cci, raw12_framefmt_regs,
  754. ARRAY_SIZE(raw12_framefmt_regs), NULL);
  755. }
  756. return -EINVAL;
  757. }
  758. /**
  759. * imx334_enable_streams() - Enable specified streams for the sensor
  760. * @sd: pointer to the V4L2 subdevice
  761. * @state: pointer to the subdevice state
  762. * @pad: pad number for which streams are enabled
  763. * @streams_mask: bitmask specifying the streams to enable
  764. *
  765. * Return: 0 if successful, error code otherwise.
  766. */
  767. static int imx334_enable_streams(struct v4l2_subdev *sd,
  768. struct v4l2_subdev_state *state, u32 pad,
  769. u64 streams_mask)
  770. {
  771. struct imx334 *imx334 = to_imx334(sd);
  772. const struct imx334_reg_list *reg_list;
  773. int ret;
  774. ret = pm_runtime_resume_and_get(imx334->dev);
  775. if (ret < 0)
  776. return ret;
  777. ret = cci_multi_reg_write(imx334->cci, common_mode_regs,
  778. ARRAY_SIZE(common_mode_regs), NULL);
  779. if (ret) {
  780. dev_err(imx334->dev, "fail to write common registers\n");
  781. goto err_rpm_put;
  782. }
  783. /* Write sensor mode registers */
  784. reg_list = &imx334->cur_mode->reg_list;
  785. ret = cci_multi_reg_write(imx334->cci, reg_list->regs,
  786. reg_list->num_of_regs, NULL);
  787. if (ret) {
  788. dev_err(imx334->dev, "fail to write initial registers\n");
  789. goto err_rpm_put;
  790. }
  791. ret = cci_write(imx334->cci, IMX334_REG_LANEMODE,
  792. IMX334_CSI_4_LANE_MODE, NULL);
  793. if (ret) {
  794. dev_err(imx334->dev, "failed to configure lanes\n");
  795. goto err_rpm_put;
  796. }
  797. ret = imx334_set_framefmt(imx334);
  798. if (ret) {
  799. dev_err(imx334->dev, "%s failed to set frame format: %d\n",
  800. __func__, ret);
  801. goto err_rpm_put;
  802. }
  803. /* Setup handler will write actual exposure and gain */
  804. ret = __v4l2_ctrl_handler_setup(imx334->sd.ctrl_handler);
  805. if (ret) {
  806. dev_err(imx334->dev, "fail to setup handler\n");
  807. goto err_rpm_put;
  808. }
  809. /* Start streaming */
  810. ret = cci_write(imx334->cci, IMX334_REG_MODE_SELECT,
  811. IMX334_MODE_STREAMING, NULL);
  812. if (ret) {
  813. dev_err(imx334->dev, "fail to start streaming\n");
  814. goto err_rpm_put;
  815. }
  816. return 0;
  817. err_rpm_put:
  818. pm_runtime_put(imx334->dev);
  819. return ret;
  820. }
  821. /**
  822. * imx334_disable_streams() - Enable specified streams for the sensor
  823. * @sd: pointer to the V4L2 subdevice
  824. * @state: pointer to the subdevice state
  825. * @pad: pad number for which streams are disabled
  826. * @streams_mask: bitmask specifying the streams to disable
  827. *
  828. * Return: 0 if successful, error code otherwise.
  829. */
  830. static int imx334_disable_streams(struct v4l2_subdev *sd,
  831. struct v4l2_subdev_state *state, u32 pad,
  832. u64 streams_mask)
  833. {
  834. struct imx334 *imx334 = to_imx334(sd);
  835. int ret;
  836. ret = cci_write(imx334->cci, IMX334_REG_MODE_SELECT,
  837. IMX334_MODE_STANDBY, NULL);
  838. if (ret)
  839. dev_err(imx334->dev, "%s failed to stop stream\n", __func__);
  840. pm_runtime_put(imx334->dev);
  841. return ret;
  842. }
  843. /**
  844. * imx334_detect() - Detect imx334 sensor
  845. * @imx334: pointer to imx334 device
  846. *
  847. * Return: 0 if successful, -EIO if sensor id does not match
  848. */
  849. static int imx334_detect(struct imx334 *imx334)
  850. {
  851. int ret;
  852. u64 val;
  853. ret = cci_read(imx334->cci, IMX334_REG_ID, &val, NULL);
  854. if (ret)
  855. return ret;
  856. if (val != IMX334_ID) {
  857. dev_err(imx334->dev, "chip id mismatch: %x!=%llx\n",
  858. IMX334_ID, val);
  859. return -ENXIO;
  860. }
  861. return 0;
  862. }
  863. /**
  864. * imx334_parse_hw_config() - Parse HW configuration and check if supported
  865. * @imx334: pointer to imx334 device
  866. *
  867. * Return: 0 if successful, error code otherwise.
  868. */
  869. static int imx334_parse_hw_config(struct imx334 *imx334)
  870. {
  871. struct fwnode_handle *fwnode = dev_fwnode(imx334->dev);
  872. struct v4l2_fwnode_endpoint bus_cfg = {
  873. .bus_type = V4L2_MBUS_CSI2_DPHY
  874. };
  875. struct fwnode_handle *ep;
  876. unsigned long rate;
  877. int ret;
  878. if (!fwnode)
  879. return -ENXIO;
  880. /* Request optional reset pin */
  881. imx334->reset_gpio = devm_gpiod_get_optional(imx334->dev, "reset",
  882. GPIOD_OUT_LOW);
  883. if (IS_ERR(imx334->reset_gpio))
  884. return dev_err_probe(imx334->dev, PTR_ERR(imx334->reset_gpio),
  885. "failed to get reset gpio\n");
  886. /* Get sensor input clock */
  887. imx334->inclk = devm_v4l2_sensor_clk_get(imx334->dev, NULL);
  888. if (IS_ERR(imx334->inclk))
  889. return dev_err_probe(imx334->dev, PTR_ERR(imx334->inclk),
  890. "could not get inclk\n");
  891. rate = clk_get_rate(imx334->inclk);
  892. if (rate != IMX334_INCLK_RATE)
  893. return dev_err_probe(imx334->dev, -EINVAL,
  894. "inclk frequency mismatch\n");
  895. ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
  896. if (!ep)
  897. return -ENXIO;
  898. ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
  899. fwnode_handle_put(ep);
  900. if (ret)
  901. return ret;
  902. if (bus_cfg.bus.mipi_csi2.num_data_lanes != IMX334_NUM_DATA_LANES) {
  903. dev_err(imx334->dev,
  904. "number of CSI2 data lanes %d is not supported\n",
  905. bus_cfg.bus.mipi_csi2.num_data_lanes);
  906. ret = -EINVAL;
  907. goto done_endpoint_free;
  908. }
  909. ret = v4l2_link_freq_to_bitmap(imx334->dev, bus_cfg.link_frequencies,
  910. bus_cfg.nr_of_link_frequencies,
  911. link_freq, ARRAY_SIZE(link_freq),
  912. &imx334->link_freq_bitmap);
  913. done_endpoint_free:
  914. v4l2_fwnode_endpoint_free(&bus_cfg);
  915. return ret;
  916. }
  917. /* V4l2 subdevice ops */
  918. static const struct v4l2_subdev_video_ops imx334_video_ops = {
  919. .s_stream = v4l2_subdev_s_stream_helper,
  920. };
  921. static const struct v4l2_subdev_pad_ops imx334_pad_ops = {
  922. .enum_mbus_code = imx334_enum_mbus_code,
  923. .enum_frame_size = imx334_enum_frame_size,
  924. .get_fmt = imx334_get_pad_format,
  925. .set_fmt = imx334_set_pad_format,
  926. .enable_streams = imx334_enable_streams,
  927. .disable_streams = imx334_disable_streams,
  928. };
  929. static const struct v4l2_subdev_ops imx334_subdev_ops = {
  930. .video = &imx334_video_ops,
  931. .pad = &imx334_pad_ops,
  932. };
  933. static const struct v4l2_subdev_internal_ops imx334_internal_ops = {
  934. .init_state = imx334_init_state,
  935. };
  936. /**
  937. * imx334_power_on() - Sensor power on sequence
  938. * @dev: pointer to i2c device
  939. *
  940. * Return: 0 if successful, error code otherwise.
  941. */
  942. static int imx334_power_on(struct device *dev)
  943. {
  944. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  945. struct imx334 *imx334 = to_imx334(sd);
  946. int ret;
  947. /*
  948. * Note: Misinterpretation of reset assertion - do not re-use this code.
  949. * XCLR pin is using incorrect (for reset signal) logical level.
  950. */
  951. gpiod_set_value_cansleep(imx334->reset_gpio, 1);
  952. ret = clk_prepare_enable(imx334->inclk);
  953. if (ret) {
  954. dev_err(imx334->dev, "fail to enable inclk\n");
  955. goto error_reset;
  956. }
  957. usleep_range(18000, 20000);
  958. return 0;
  959. error_reset:
  960. gpiod_set_value_cansleep(imx334->reset_gpio, 0);
  961. return ret;
  962. }
  963. /**
  964. * imx334_power_off() - Sensor power off sequence
  965. * @dev: pointer to i2c device
  966. *
  967. * Return: 0 if successful, error code otherwise.
  968. */
  969. static int imx334_power_off(struct device *dev)
  970. {
  971. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  972. struct imx334 *imx334 = to_imx334(sd);
  973. gpiod_set_value_cansleep(imx334->reset_gpio, 0);
  974. clk_disable_unprepare(imx334->inclk);
  975. return 0;
  976. }
  977. /**
  978. * imx334_init_controls() - Initialize sensor subdevice controls
  979. * @imx334: pointer to imx334 device
  980. *
  981. * Return: 0 if successful, error code otherwise.
  982. */
  983. static int imx334_init_controls(struct imx334 *imx334)
  984. {
  985. struct v4l2_ctrl_handler *ctrl_hdlr = &imx334->ctrl_handler;
  986. const struct imx334_mode *mode = imx334->cur_mode;
  987. u32 lpfr;
  988. int ret;
  989. ret = v4l2_ctrl_handler_init(ctrl_hdlr, 7);
  990. if (ret)
  991. return ret;
  992. /* Initialize exposure and gain */
  993. lpfr = mode->vblank + mode->height;
  994. imx334->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  995. &imx334_ctrl_ops,
  996. V4L2_CID_EXPOSURE,
  997. IMX334_EXPOSURE_MIN,
  998. lpfr - IMX334_EXPOSURE_OFFSET,
  999. IMX334_EXPOSURE_STEP,
  1000. IMX334_EXPOSURE_DEFAULT);
  1001. imx334->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  1002. &imx334_ctrl_ops,
  1003. V4L2_CID_ANALOGUE_GAIN,
  1004. IMX334_AGAIN_MIN,
  1005. IMX334_AGAIN_MAX,
  1006. IMX334_AGAIN_STEP,
  1007. IMX334_AGAIN_DEFAULT);
  1008. v4l2_ctrl_cluster(2, &imx334->exp_ctrl);
  1009. imx334->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  1010. &imx334_ctrl_ops,
  1011. V4L2_CID_VBLANK,
  1012. mode->vblank_min,
  1013. mode->vblank_max,
  1014. 1, mode->vblank);
  1015. /* Read only controls */
  1016. imx334->pclk_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  1017. &imx334_ctrl_ops,
  1018. V4L2_CID_PIXEL_RATE,
  1019. mode->pclk, mode->pclk,
  1020. 1, mode->pclk);
  1021. imx334->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr,
  1022. &imx334_ctrl_ops,
  1023. V4L2_CID_LINK_FREQ,
  1024. __fls(imx334->link_freq_bitmap),
  1025. __ffs(imx334->link_freq_bitmap),
  1026. link_freq);
  1027. if (imx334->link_freq_ctrl)
  1028. imx334->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1029. imx334->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  1030. &imx334_ctrl_ops,
  1031. V4L2_CID_HBLANK,
  1032. IMX334_REG_MIN,
  1033. IMX334_REG_MAX,
  1034. 1, mode->hblank);
  1035. if (imx334->hblank_ctrl)
  1036. imx334->hblank_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  1037. v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx334_ctrl_ops,
  1038. V4L2_CID_TEST_PATTERN,
  1039. ARRAY_SIZE(imx334_test_pattern_menu) - 1,
  1040. 0, 0, imx334_test_pattern_menu);
  1041. if (ctrl_hdlr->error) {
  1042. dev_err(imx334->dev, "control init failed: %d",
  1043. ctrl_hdlr->error);
  1044. v4l2_ctrl_handler_free(ctrl_hdlr);
  1045. return ctrl_hdlr->error;
  1046. }
  1047. imx334->sd.ctrl_handler = ctrl_hdlr;
  1048. return 0;
  1049. }
  1050. /**
  1051. * imx334_probe() - I2C client device binding
  1052. * @client: pointer to i2c client device
  1053. *
  1054. * Return: 0 if successful, error code otherwise.
  1055. */
  1056. static int imx334_probe(struct i2c_client *client)
  1057. {
  1058. struct imx334 *imx334;
  1059. int ret;
  1060. imx334 = devm_kzalloc(&client->dev, sizeof(*imx334), GFP_KERNEL);
  1061. if (!imx334)
  1062. return -ENOMEM;
  1063. imx334->dev = &client->dev;
  1064. imx334->cci = devm_cci_regmap_init_i2c(client, 16);
  1065. if (IS_ERR(imx334->cci)) {
  1066. dev_err(imx334->dev, "Unable to initialize I2C\n");
  1067. return -ENODEV;
  1068. }
  1069. /* Initialize subdev */
  1070. v4l2_i2c_subdev_init(&imx334->sd, client, &imx334_subdev_ops);
  1071. imx334->sd.internal_ops = &imx334_internal_ops;
  1072. ret = imx334_parse_hw_config(imx334);
  1073. if (ret)
  1074. return dev_err_probe(imx334->dev, ret,
  1075. "HW configuration is not supported\n");
  1076. ret = imx334_power_on(imx334->dev);
  1077. if (ret) {
  1078. dev_err_probe(imx334->dev, ret, "failed to power-on the sensor\n");
  1079. return ret;
  1080. }
  1081. /* Check module identity */
  1082. ret = imx334_detect(imx334);
  1083. if (ret) {
  1084. dev_err(imx334->dev, "failed to find sensor: %d\n", ret);
  1085. goto error_power_off;
  1086. }
  1087. /* Set default mode to max resolution */
  1088. imx334->cur_mode = &supported_modes[__ffs(imx334->link_freq_bitmap)];
  1089. imx334->cur_code = imx334_mbus_codes[0];
  1090. imx334->vblank = imx334->cur_mode->vblank;
  1091. ret = imx334_init_controls(imx334);
  1092. if (ret) {
  1093. dev_err(imx334->dev, "failed to init controls: %d\n", ret);
  1094. goto error_power_off;
  1095. }
  1096. /* Initialize subdev */
  1097. imx334->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1098. imx334->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1099. /* Initialize source pad */
  1100. imx334->pad.flags = MEDIA_PAD_FL_SOURCE;
  1101. ret = media_entity_pads_init(&imx334->sd.entity, 1, &imx334->pad);
  1102. if (ret) {
  1103. dev_err(imx334->dev, "failed to init entity pads: %d\n", ret);
  1104. goto error_handler_free;
  1105. }
  1106. imx334->sd.state_lock = imx334->ctrl_handler.lock;
  1107. ret = v4l2_subdev_init_finalize(&imx334->sd);
  1108. if (ret < 0) {
  1109. dev_err(imx334->dev, "subdev init error: %d\n", ret);
  1110. goto error_media_entity;
  1111. }
  1112. pm_runtime_set_active(imx334->dev);
  1113. pm_runtime_enable(imx334->dev);
  1114. ret = v4l2_async_register_subdev_sensor(&imx334->sd);
  1115. if (ret < 0) {
  1116. dev_err(imx334->dev,
  1117. "failed to register async subdev: %d\n", ret);
  1118. goto error_subdev_cleanup;
  1119. }
  1120. pm_runtime_idle(imx334->dev);
  1121. return 0;
  1122. error_subdev_cleanup:
  1123. v4l2_subdev_cleanup(&imx334->sd);
  1124. pm_runtime_disable(imx334->dev);
  1125. pm_runtime_set_suspended(imx334->dev);
  1126. error_media_entity:
  1127. media_entity_cleanup(&imx334->sd.entity);
  1128. error_handler_free:
  1129. v4l2_ctrl_handler_free(imx334->sd.ctrl_handler);
  1130. error_power_off:
  1131. imx334_power_off(imx334->dev);
  1132. return ret;
  1133. }
  1134. /**
  1135. * imx334_remove() - I2C client device unbinding
  1136. * @client: pointer to I2C client device
  1137. *
  1138. * Return: 0 if successful, error code otherwise.
  1139. */
  1140. static void imx334_remove(struct i2c_client *client)
  1141. {
  1142. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1143. v4l2_async_unregister_subdev(sd);
  1144. v4l2_subdev_cleanup(sd);
  1145. media_entity_cleanup(&sd->entity);
  1146. v4l2_ctrl_handler_free(sd->ctrl_handler);
  1147. pm_runtime_disable(&client->dev);
  1148. if (!pm_runtime_status_suspended(&client->dev)) {
  1149. imx334_power_off(&client->dev);
  1150. pm_runtime_set_suspended(&client->dev);
  1151. }
  1152. }
  1153. static const struct dev_pm_ops imx334_pm_ops = {
  1154. SET_RUNTIME_PM_OPS(imx334_power_off, imx334_power_on, NULL)
  1155. };
  1156. static const struct of_device_id imx334_of_match[] = {
  1157. { .compatible = "sony,imx334" },
  1158. { }
  1159. };
  1160. MODULE_DEVICE_TABLE(of, imx334_of_match);
  1161. static struct i2c_driver imx334_driver = {
  1162. .probe = imx334_probe,
  1163. .remove = imx334_remove,
  1164. .driver = {
  1165. .name = "imx334",
  1166. .pm = &imx334_pm_ops,
  1167. .of_match_table = imx334_of_match,
  1168. },
  1169. };
  1170. module_i2c_driver(imx334_driver);
  1171. MODULE_DESCRIPTION("Sony imx334 sensor driver");
  1172. MODULE_LICENSE("GPL");