imx334.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Sony imx334 sensor driver
  4. *
  5. * Copyright (C) 2021 Intel Corporation
  6. */
  7. #include <asm/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-ctrls.h>
  14. #include <media/v4l2-fwnode.h>
  15. #include <media/v4l2-subdev.h>
  16. /* Streaming Mode */
  17. #define IMX334_REG_MODE_SELECT 0x3000
  18. #define IMX334_MODE_STANDBY 0x01
  19. #define IMX334_MODE_STREAMING 0x00
  20. /* Lines per frame */
  21. #define IMX334_REG_LPFR 0x3030
  22. /* Chip ID */
  23. #define IMX334_REG_ID 0x3044
  24. #define IMX334_ID 0x1e
  25. /* Exposure control */
  26. #define IMX334_REG_SHUTTER 0x3058
  27. #define IMX334_EXPOSURE_MIN 1
  28. #define IMX334_EXPOSURE_OFFSET 5
  29. #define IMX334_EXPOSURE_STEP 1
  30. #define IMX334_EXPOSURE_DEFAULT 0x0648
  31. /* Analog gain control */
  32. #define IMX334_REG_AGAIN 0x30e8
  33. #define IMX334_AGAIN_MIN 0
  34. #define IMX334_AGAIN_MAX 240
  35. #define IMX334_AGAIN_STEP 1
  36. #define IMX334_AGAIN_DEFAULT 0
  37. /* Group hold register */
  38. #define IMX334_REG_HOLD 0x3001
  39. /* Input clock rate */
  40. #define IMX334_INCLK_RATE 24000000
  41. /* CSI2 HW configuration */
  42. #define IMX334_LINK_FREQ 891000000
  43. #define IMX334_NUM_DATA_LANES 4
  44. #define IMX334_REG_MIN 0x00
  45. #define IMX334_REG_MAX 0xfffff
  46. /**
  47. * struct imx334_reg - imx334 sensor register
  48. * @address: Register address
  49. * @val: Register value
  50. */
  51. struct imx334_reg {
  52. u16 address;
  53. u8 val;
  54. };
  55. /**
  56. * struct imx334_reg_list - imx334 sensor register list
  57. * @num_of_regs: Number of registers in the list
  58. * @regs: Pointer to register list
  59. */
  60. struct imx334_reg_list {
  61. u32 num_of_regs;
  62. const struct imx334_reg *regs;
  63. };
  64. /**
  65. * struct imx334_mode - imx334 sensor mode structure
  66. * @width: Frame width
  67. * @height: Frame height
  68. * @code: Format code
  69. * @hblank: Horizontal blanking in lines
  70. * @vblank: Vertical blanking in lines
  71. * @vblank_min: Minimal vertical blanking in lines
  72. * @vblank_max: Maximum vertical blanking in lines
  73. * @pclk: Sensor pixel clock
  74. * @link_freq_idx: Link frequency index
  75. * @reg_list: Register list for sensor mode
  76. */
  77. struct imx334_mode {
  78. u32 width;
  79. u32 height;
  80. u32 code;
  81. u32 hblank;
  82. u32 vblank;
  83. u32 vblank_min;
  84. u32 vblank_max;
  85. u64 pclk;
  86. u32 link_freq_idx;
  87. struct imx334_reg_list reg_list;
  88. };
  89. /**
  90. * struct imx334 - imx334 sensor device structure
  91. * @dev: Pointer to generic device
  92. * @client: Pointer to i2c client
  93. * @sd: V4L2 sub-device
  94. * @pad: Media pad. Only one pad supported
  95. * @reset_gpio: Sensor reset gpio
  96. * @inclk: Sensor input clock
  97. * @ctrl_handler: V4L2 control handler
  98. * @link_freq_ctrl: Pointer to link frequency control
  99. * @pclk_ctrl: Pointer to pixel clock control
  100. * @hblank_ctrl: Pointer to horizontal blanking control
  101. * @vblank_ctrl: Pointer to vertical blanking control
  102. * @exp_ctrl: Pointer to exposure control
  103. * @again_ctrl: Pointer to analog gain control
  104. * @vblank: Vertical blanking in lines
  105. * @cur_mode: Pointer to current selected sensor mode
  106. * @mutex: Mutex for serializing sensor controls
  107. * @streaming: Flag indicating streaming state
  108. */
  109. struct imx334 {
  110. struct device *dev;
  111. struct i2c_client *client;
  112. struct v4l2_subdev sd;
  113. struct media_pad pad;
  114. struct gpio_desc *reset_gpio;
  115. struct clk *inclk;
  116. struct v4l2_ctrl_handler ctrl_handler;
  117. struct v4l2_ctrl *link_freq_ctrl;
  118. struct v4l2_ctrl *pclk_ctrl;
  119. struct v4l2_ctrl *hblank_ctrl;
  120. struct v4l2_ctrl *vblank_ctrl;
  121. struct {
  122. struct v4l2_ctrl *exp_ctrl;
  123. struct v4l2_ctrl *again_ctrl;
  124. };
  125. u32 vblank;
  126. const struct imx334_mode *cur_mode;
  127. struct mutex mutex;
  128. bool streaming;
  129. };
  130. static const s64 link_freq[] = {
  131. IMX334_LINK_FREQ,
  132. };
  133. /* Sensor mode registers */
  134. static const struct imx334_reg mode_3840x2160_regs[] = {
  135. {0x3000, 0x01},
  136. {0x3002, 0x00},
  137. {0x3018, 0x04},
  138. {0x37b0, 0x36},
  139. {0x304c, 0x00},
  140. {0x300c, 0x3b},
  141. {0x300d, 0x2a},
  142. {0x3034, 0x26},
  143. {0x3035, 0x02},
  144. {0x314c, 0x29},
  145. {0x314d, 0x01},
  146. {0x315a, 0x02},
  147. {0x3168, 0xa0},
  148. {0x316a, 0x7e},
  149. {0x3288, 0x21},
  150. {0x328a, 0x02},
  151. {0x302c, 0x3c},
  152. {0x302e, 0x00},
  153. {0x302f, 0x0f},
  154. {0x3076, 0x70},
  155. {0x3077, 0x08},
  156. {0x3090, 0x70},
  157. {0x3091, 0x08},
  158. {0x30d8, 0x20},
  159. {0x30d9, 0x12},
  160. {0x3308, 0x70},
  161. {0x3309, 0x08},
  162. {0x3414, 0x05},
  163. {0x3416, 0x18},
  164. {0x35ac, 0x0e},
  165. {0x3648, 0x01},
  166. {0x364a, 0x04},
  167. {0x364c, 0x04},
  168. {0x3678, 0x01},
  169. {0x367c, 0x31},
  170. {0x367e, 0x31},
  171. {0x3708, 0x02},
  172. {0x3714, 0x01},
  173. {0x3715, 0x02},
  174. {0x3716, 0x02},
  175. {0x3717, 0x02},
  176. {0x371c, 0x3d},
  177. {0x371d, 0x3f},
  178. {0x372c, 0x00},
  179. {0x372d, 0x00},
  180. {0x372e, 0x46},
  181. {0x372f, 0x00},
  182. {0x3730, 0x89},
  183. {0x3731, 0x00},
  184. {0x3732, 0x08},
  185. {0x3733, 0x01},
  186. {0x3734, 0xfe},
  187. {0x3735, 0x05},
  188. {0x375d, 0x00},
  189. {0x375e, 0x00},
  190. {0x375f, 0x61},
  191. {0x3760, 0x06},
  192. {0x3768, 0x1b},
  193. {0x3769, 0x1b},
  194. {0x376a, 0x1a},
  195. {0x376b, 0x19},
  196. {0x376c, 0x18},
  197. {0x376d, 0x14},
  198. {0x376e, 0x0f},
  199. {0x3776, 0x00},
  200. {0x3777, 0x00},
  201. {0x3778, 0x46},
  202. {0x3779, 0x00},
  203. {0x377a, 0x08},
  204. {0x377b, 0x01},
  205. {0x377c, 0x45},
  206. {0x377d, 0x01},
  207. {0x377e, 0x23},
  208. {0x377f, 0x02},
  209. {0x3780, 0xd9},
  210. {0x3781, 0x03},
  211. {0x3782, 0xf5},
  212. {0x3783, 0x06},
  213. {0x3784, 0xa5},
  214. {0x3788, 0x0f},
  215. {0x378a, 0xd9},
  216. {0x378b, 0x03},
  217. {0x378c, 0xeb},
  218. {0x378d, 0x05},
  219. {0x378e, 0x87},
  220. {0x378f, 0x06},
  221. {0x3790, 0xf5},
  222. {0x3792, 0x43},
  223. {0x3794, 0x7a},
  224. {0x3796, 0xa1},
  225. {0x3e04, 0x0e},
  226. {0x3a00, 0x01},
  227. };
  228. /* Supported sensor mode configurations */
  229. static const struct imx334_mode supported_mode = {
  230. .width = 3840,
  231. .height = 2160,
  232. .hblank = 560,
  233. .vblank = 2340,
  234. .vblank_min = 90,
  235. .vblank_max = 132840,
  236. .pclk = 594000000,
  237. .link_freq_idx = 0,
  238. .code = MEDIA_BUS_FMT_SRGGB12_1X12,
  239. .reg_list = {
  240. .num_of_regs = ARRAY_SIZE(mode_3840x2160_regs),
  241. .regs = mode_3840x2160_regs,
  242. },
  243. };
  244. /**
  245. * to_imx334() - imv334 V4L2 sub-device to imx334 device.
  246. * @subdev: pointer to imx334 V4L2 sub-device
  247. *
  248. * Return: pointer to imx334 device
  249. */
  250. static inline struct imx334 *to_imx334(struct v4l2_subdev *subdev)
  251. {
  252. return container_of(subdev, struct imx334, sd);
  253. }
  254. /**
  255. * imx334_read_reg() - Read registers.
  256. * @imx334: pointer to imx334 device
  257. * @reg: register address
  258. * @len: length of bytes to read. Max supported bytes is 4
  259. * @val: pointer to register value to be filled.
  260. *
  261. * Big endian register addresses with little endian values.
  262. *
  263. * Return: 0 if successful, error code otherwise.
  264. */
  265. static int imx334_read_reg(struct imx334 *imx334, u16 reg, u32 len, u32 *val)
  266. {
  267. struct i2c_client *client = v4l2_get_subdevdata(&imx334->sd);
  268. struct i2c_msg msgs[2] = {0};
  269. u8 addr_buf[2] = {0};
  270. u8 data_buf[4] = {0};
  271. int ret;
  272. if (WARN_ON(len > 4))
  273. return -EINVAL;
  274. put_unaligned_be16(reg, addr_buf);
  275. /* Write register address */
  276. msgs[0].addr = client->addr;
  277. msgs[0].flags = 0;
  278. msgs[0].len = ARRAY_SIZE(addr_buf);
  279. msgs[0].buf = addr_buf;
  280. /* Read data from register */
  281. msgs[1].addr = client->addr;
  282. msgs[1].flags = I2C_M_RD;
  283. msgs[1].len = len;
  284. msgs[1].buf = data_buf;
  285. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  286. if (ret != ARRAY_SIZE(msgs))
  287. return -EIO;
  288. *val = get_unaligned_le32(data_buf);
  289. return 0;
  290. }
  291. /**
  292. * imx334_write_reg() - Write register
  293. * @imx334: pointer to imx334 device
  294. * @reg: register address
  295. * @len: length of bytes. Max supported bytes is 4
  296. * @val: register value
  297. *
  298. * Big endian register addresses with little endian values.
  299. *
  300. * Return: 0 if successful, error code otherwise.
  301. */
  302. static int imx334_write_reg(struct imx334 *imx334, u16 reg, u32 len, u32 val)
  303. {
  304. struct i2c_client *client = v4l2_get_subdevdata(&imx334->sd);
  305. u8 buf[6] = {0};
  306. if (WARN_ON(len > 4))
  307. return -EINVAL;
  308. put_unaligned_be16(reg, buf);
  309. put_unaligned_le32(val, buf + 2);
  310. if (i2c_master_send(client, buf, len + 2) != len + 2)
  311. return -EIO;
  312. return 0;
  313. }
  314. /**
  315. * imx334_write_regs() - Write a list of registers
  316. * @imx334: pointer to imx334 device
  317. * @regs: list of registers to be written
  318. * @len: length of registers array
  319. *
  320. * Return: 0 if successful, error code otherwise.
  321. */
  322. static int imx334_write_regs(struct imx334 *imx334,
  323. const struct imx334_reg *regs, u32 len)
  324. {
  325. unsigned int i;
  326. int ret;
  327. for (i = 0; i < len; i++) {
  328. ret = imx334_write_reg(imx334, regs[i].address, 1, regs[i].val);
  329. if (ret)
  330. return ret;
  331. }
  332. return 0;
  333. }
  334. /**
  335. * imx334_update_controls() - Update control ranges based on streaming mode
  336. * @imx334: pointer to imx334 device
  337. * @mode: pointer to imx334_mode sensor mode
  338. *
  339. * Return: 0 if successful, error code otherwise.
  340. */
  341. static int imx334_update_controls(struct imx334 *imx334,
  342. const struct imx334_mode *mode)
  343. {
  344. int ret;
  345. ret = __v4l2_ctrl_s_ctrl(imx334->link_freq_ctrl, mode->link_freq_idx);
  346. if (ret)
  347. return ret;
  348. ret = __v4l2_ctrl_s_ctrl(imx334->hblank_ctrl, mode->hblank);
  349. if (ret)
  350. return ret;
  351. return __v4l2_ctrl_modify_range(imx334->vblank_ctrl, mode->vblank_min,
  352. mode->vblank_max, 1, mode->vblank);
  353. }
  354. /**
  355. * imx334_update_exp_gain() - Set updated exposure and gain
  356. * @imx334: pointer to imx334 device
  357. * @exposure: updated exposure value
  358. * @gain: updated analog gain value
  359. *
  360. * Return: 0 if successful, error code otherwise.
  361. */
  362. static int imx334_update_exp_gain(struct imx334 *imx334, u32 exposure, u32 gain)
  363. {
  364. u32 lpfr, shutter;
  365. int ret;
  366. lpfr = imx334->vblank + imx334->cur_mode->height;
  367. shutter = lpfr - exposure;
  368. dev_dbg(imx334->dev, "Set long exp %u analog gain %u sh0 %u lpfr %u",
  369. exposure, gain, shutter, lpfr);
  370. ret = imx334_write_reg(imx334, IMX334_REG_HOLD, 1, 1);
  371. if (ret)
  372. return ret;
  373. ret = imx334_write_reg(imx334, IMX334_REG_LPFR, 3, lpfr);
  374. if (ret)
  375. goto error_release_group_hold;
  376. ret = imx334_write_reg(imx334, IMX334_REG_SHUTTER, 3, shutter);
  377. if (ret)
  378. goto error_release_group_hold;
  379. ret = imx334_write_reg(imx334, IMX334_REG_AGAIN, 1, gain);
  380. error_release_group_hold:
  381. imx334_write_reg(imx334, IMX334_REG_HOLD, 1, 0);
  382. return ret;
  383. }
  384. /**
  385. * imx334_set_ctrl() - Set subdevice control
  386. * @ctrl: pointer to v4l2_ctrl structure
  387. *
  388. * Supported controls:
  389. * - V4L2_CID_VBLANK
  390. * - cluster controls:
  391. * - V4L2_CID_ANALOGUE_GAIN
  392. * - V4L2_CID_EXPOSURE
  393. *
  394. * Return: 0 if successful, error code otherwise.
  395. */
  396. static int imx334_set_ctrl(struct v4l2_ctrl *ctrl)
  397. {
  398. struct imx334 *imx334 =
  399. container_of(ctrl->handler, struct imx334, ctrl_handler);
  400. u32 analog_gain;
  401. u32 exposure;
  402. int ret;
  403. switch (ctrl->id) {
  404. case V4L2_CID_VBLANK:
  405. imx334->vblank = imx334->vblank_ctrl->val;
  406. dev_dbg(imx334->dev, "Received vblank %u, new lpfr %u",
  407. imx334->vblank,
  408. imx334->vblank + imx334->cur_mode->height);
  409. ret = __v4l2_ctrl_modify_range(imx334->exp_ctrl,
  410. IMX334_EXPOSURE_MIN,
  411. imx334->vblank +
  412. imx334->cur_mode->height -
  413. IMX334_EXPOSURE_OFFSET,
  414. 1, IMX334_EXPOSURE_DEFAULT);
  415. break;
  416. case V4L2_CID_EXPOSURE:
  417. /* Set controls only if sensor is in power on state */
  418. if (!pm_runtime_get_if_in_use(imx334->dev))
  419. return 0;
  420. exposure = ctrl->val;
  421. analog_gain = imx334->again_ctrl->val;
  422. dev_dbg(imx334->dev, "Received exp %u analog gain %u",
  423. exposure, analog_gain);
  424. ret = imx334_update_exp_gain(imx334, exposure, analog_gain);
  425. pm_runtime_put(imx334->dev);
  426. break;
  427. default:
  428. dev_err(imx334->dev, "Invalid control %d", ctrl->id);
  429. ret = -EINVAL;
  430. }
  431. return ret;
  432. }
  433. /* V4l2 subdevice control ops*/
  434. static const struct v4l2_ctrl_ops imx334_ctrl_ops = {
  435. .s_ctrl = imx334_set_ctrl,
  436. };
  437. /**
  438. * imx334_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes
  439. * @sd: pointer to imx334 V4L2 sub-device structure
  440. * @sd_state: V4L2 sub-device state
  441. * @code: V4L2 sub-device code enumeration need to be filled
  442. *
  443. * Return: 0 if successful, error code otherwise.
  444. */
  445. static int imx334_enum_mbus_code(struct v4l2_subdev *sd,
  446. struct v4l2_subdev_state *sd_state,
  447. struct v4l2_subdev_mbus_code_enum *code)
  448. {
  449. if (code->index > 0)
  450. return -EINVAL;
  451. code->code = supported_mode.code;
  452. return 0;
  453. }
  454. /**
  455. * imx334_enum_frame_size() - Enumerate V4L2 sub-device frame sizes
  456. * @sd: pointer to imx334 V4L2 sub-device structure
  457. * @sd_state: V4L2 sub-device state
  458. * @fsize: V4L2 sub-device size enumeration need to be filled
  459. *
  460. * Return: 0 if successful, error code otherwise.
  461. */
  462. static int imx334_enum_frame_size(struct v4l2_subdev *sd,
  463. struct v4l2_subdev_state *sd_state,
  464. struct v4l2_subdev_frame_size_enum *fsize)
  465. {
  466. if (fsize->index > 0)
  467. return -EINVAL;
  468. if (fsize->code != supported_mode.code)
  469. return -EINVAL;
  470. fsize->min_width = supported_mode.width;
  471. fsize->max_width = fsize->min_width;
  472. fsize->min_height = supported_mode.height;
  473. fsize->max_height = fsize->min_height;
  474. return 0;
  475. }
  476. /**
  477. * imx334_fill_pad_format() - Fill subdevice pad format
  478. * from selected sensor mode
  479. * @imx334: pointer to imx334 device
  480. * @mode: pointer to imx334_mode sensor mode
  481. * @fmt: V4L2 sub-device format need to be filled
  482. */
  483. static void imx334_fill_pad_format(struct imx334 *imx334,
  484. const struct imx334_mode *mode,
  485. struct v4l2_subdev_format *fmt)
  486. {
  487. fmt->format.width = mode->width;
  488. fmt->format.height = mode->height;
  489. fmt->format.code = mode->code;
  490. fmt->format.field = V4L2_FIELD_NONE;
  491. fmt->format.colorspace = V4L2_COLORSPACE_RAW;
  492. fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
  493. fmt->format.quantization = V4L2_QUANTIZATION_DEFAULT;
  494. fmt->format.xfer_func = V4L2_XFER_FUNC_NONE;
  495. }
  496. /**
  497. * imx334_get_pad_format() - Get subdevice pad format
  498. * @sd: pointer to imx334 V4L2 sub-device structure
  499. * @sd_state: V4L2 sub-device state
  500. * @fmt: V4L2 sub-device format need to be set
  501. *
  502. * Return: 0 if successful, error code otherwise.
  503. */
  504. static int imx334_get_pad_format(struct v4l2_subdev *sd,
  505. struct v4l2_subdev_state *sd_state,
  506. struct v4l2_subdev_format *fmt)
  507. {
  508. struct imx334 *imx334 = to_imx334(sd);
  509. mutex_lock(&imx334->mutex);
  510. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  511. struct v4l2_mbus_framefmt *framefmt;
  512. framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
  513. fmt->format = *framefmt;
  514. } else {
  515. imx334_fill_pad_format(imx334, imx334->cur_mode, fmt);
  516. }
  517. mutex_unlock(&imx334->mutex);
  518. return 0;
  519. }
  520. /**
  521. * imx334_set_pad_format() - Set subdevice pad format
  522. * @sd: pointer to imx334 V4L2 sub-device structure
  523. * @sd_state: V4L2 sub-device state
  524. * @fmt: V4L2 sub-device format need to be set
  525. *
  526. * Return: 0 if successful, error code otherwise.
  527. */
  528. static int imx334_set_pad_format(struct v4l2_subdev *sd,
  529. struct v4l2_subdev_state *sd_state,
  530. struct v4l2_subdev_format *fmt)
  531. {
  532. struct imx334 *imx334 = to_imx334(sd);
  533. const struct imx334_mode *mode;
  534. int ret = 0;
  535. mutex_lock(&imx334->mutex);
  536. mode = &supported_mode;
  537. imx334_fill_pad_format(imx334, mode, fmt);
  538. if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
  539. struct v4l2_mbus_framefmt *framefmt;
  540. framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
  541. *framefmt = fmt->format;
  542. } else {
  543. ret = imx334_update_controls(imx334, mode);
  544. if (!ret)
  545. imx334->cur_mode = mode;
  546. }
  547. mutex_unlock(&imx334->mutex);
  548. return ret;
  549. }
  550. /**
  551. * imx334_init_pad_cfg() - Initialize sub-device pad configuration
  552. * @sd: pointer to imx334 V4L2 sub-device structure
  553. * @sd_state: V4L2 sub-device state
  554. *
  555. * Return: 0 if successful, error code otherwise.
  556. */
  557. static int imx334_init_pad_cfg(struct v4l2_subdev *sd,
  558. struct v4l2_subdev_state *sd_state)
  559. {
  560. struct imx334 *imx334 = to_imx334(sd);
  561. struct v4l2_subdev_format fmt = { 0 };
  562. fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
  563. imx334_fill_pad_format(imx334, &supported_mode, &fmt);
  564. return imx334_set_pad_format(sd, sd_state, &fmt);
  565. }
  566. /**
  567. * imx334_start_streaming() - Start sensor stream
  568. * @imx334: pointer to imx334 device
  569. *
  570. * Return: 0 if successful, error code otherwise.
  571. */
  572. static int imx334_start_streaming(struct imx334 *imx334)
  573. {
  574. const struct imx334_reg_list *reg_list;
  575. int ret;
  576. /* Write sensor mode registers */
  577. reg_list = &imx334->cur_mode->reg_list;
  578. ret = imx334_write_regs(imx334, reg_list->regs,
  579. reg_list->num_of_regs);
  580. if (ret) {
  581. dev_err(imx334->dev, "fail to write initial registers");
  582. return ret;
  583. }
  584. /* Setup handler will write actual exposure and gain */
  585. ret = __v4l2_ctrl_handler_setup(imx334->sd.ctrl_handler);
  586. if (ret) {
  587. dev_err(imx334->dev, "fail to setup handler");
  588. return ret;
  589. }
  590. /* Start streaming */
  591. ret = imx334_write_reg(imx334, IMX334_REG_MODE_SELECT,
  592. 1, IMX334_MODE_STREAMING);
  593. if (ret) {
  594. dev_err(imx334->dev, "fail to start streaming");
  595. return ret;
  596. }
  597. return 0;
  598. }
  599. /**
  600. * imx334_stop_streaming() - Stop sensor stream
  601. * @imx334: pointer to imx334 device
  602. *
  603. * Return: 0 if successful, error code otherwise.
  604. */
  605. static int imx334_stop_streaming(struct imx334 *imx334)
  606. {
  607. return imx334_write_reg(imx334, IMX334_REG_MODE_SELECT,
  608. 1, IMX334_MODE_STANDBY);
  609. }
  610. /**
  611. * imx334_set_stream() - Enable sensor streaming
  612. * @sd: pointer to imx334 subdevice
  613. * @enable: set to enable sensor streaming
  614. *
  615. * Return: 0 if successful, error code otherwise.
  616. */
  617. static int imx334_set_stream(struct v4l2_subdev *sd, int enable)
  618. {
  619. struct imx334 *imx334 = to_imx334(sd);
  620. int ret;
  621. mutex_lock(&imx334->mutex);
  622. if (imx334->streaming == enable) {
  623. mutex_unlock(&imx334->mutex);
  624. return 0;
  625. }
  626. if (enable) {
  627. ret = pm_runtime_resume_and_get(imx334->dev);
  628. if (ret < 0)
  629. goto error_unlock;
  630. ret = imx334_start_streaming(imx334);
  631. if (ret)
  632. goto error_power_off;
  633. } else {
  634. imx334_stop_streaming(imx334);
  635. pm_runtime_put(imx334->dev);
  636. }
  637. imx334->streaming = enable;
  638. mutex_unlock(&imx334->mutex);
  639. return 0;
  640. error_power_off:
  641. pm_runtime_put(imx334->dev);
  642. error_unlock:
  643. mutex_unlock(&imx334->mutex);
  644. return ret;
  645. }
  646. /**
  647. * imx334_detect() - Detect imx334 sensor
  648. * @imx334: pointer to imx334 device
  649. *
  650. * Return: 0 if successful, -EIO if sensor id does not match
  651. */
  652. static int imx334_detect(struct imx334 *imx334)
  653. {
  654. int ret;
  655. u32 val;
  656. ret = imx334_read_reg(imx334, IMX334_REG_ID, 2, &val);
  657. if (ret)
  658. return ret;
  659. if (val != IMX334_ID) {
  660. dev_err(imx334->dev, "chip id mismatch: %x!=%x",
  661. IMX334_ID, val);
  662. return -ENXIO;
  663. }
  664. return 0;
  665. }
  666. /**
  667. * imx334_parse_hw_config() - Parse HW configuration and check if supported
  668. * @imx334: pointer to imx334 device
  669. *
  670. * Return: 0 if successful, error code otherwise.
  671. */
  672. static int imx334_parse_hw_config(struct imx334 *imx334)
  673. {
  674. struct fwnode_handle *fwnode = dev_fwnode(imx334->dev);
  675. struct v4l2_fwnode_endpoint bus_cfg = {
  676. .bus_type = V4L2_MBUS_CSI2_DPHY
  677. };
  678. struct fwnode_handle *ep;
  679. unsigned long rate;
  680. int ret;
  681. int i;
  682. if (!fwnode)
  683. return -ENXIO;
  684. /* Request optional reset pin */
  685. imx334->reset_gpio = devm_gpiod_get_optional(imx334->dev, "reset",
  686. GPIOD_OUT_LOW);
  687. if (IS_ERR(imx334->reset_gpio)) {
  688. dev_err(imx334->dev, "failed to get reset gpio %ld",
  689. PTR_ERR(imx334->reset_gpio));
  690. return PTR_ERR(imx334->reset_gpio);
  691. }
  692. /* Get sensor input clock */
  693. imx334->inclk = devm_clk_get(imx334->dev, NULL);
  694. if (IS_ERR(imx334->inclk)) {
  695. dev_err(imx334->dev, "could not get inclk");
  696. return PTR_ERR(imx334->inclk);
  697. }
  698. rate = clk_get_rate(imx334->inclk);
  699. if (rate != IMX334_INCLK_RATE) {
  700. dev_err(imx334->dev, "inclk frequency mismatch");
  701. return -EINVAL;
  702. }
  703. ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
  704. if (!ep)
  705. return -ENXIO;
  706. ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
  707. fwnode_handle_put(ep);
  708. if (ret)
  709. return ret;
  710. if (bus_cfg.bus.mipi_csi2.num_data_lanes != IMX334_NUM_DATA_LANES) {
  711. dev_err(imx334->dev,
  712. "number of CSI2 data lanes %d is not supported",
  713. bus_cfg.bus.mipi_csi2.num_data_lanes);
  714. ret = -EINVAL;
  715. goto done_endpoint_free;
  716. }
  717. if (!bus_cfg.nr_of_link_frequencies) {
  718. dev_err(imx334->dev, "no link frequencies defined");
  719. ret = -EINVAL;
  720. goto done_endpoint_free;
  721. }
  722. for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
  723. if (bus_cfg.link_frequencies[i] == IMX334_LINK_FREQ)
  724. goto done_endpoint_free;
  725. ret = -EINVAL;
  726. done_endpoint_free:
  727. v4l2_fwnode_endpoint_free(&bus_cfg);
  728. return ret;
  729. }
  730. /* V4l2 subdevice ops */
  731. static const struct v4l2_subdev_video_ops imx334_video_ops = {
  732. .s_stream = imx334_set_stream,
  733. };
  734. static const struct v4l2_subdev_pad_ops imx334_pad_ops = {
  735. .init_cfg = imx334_init_pad_cfg,
  736. .enum_mbus_code = imx334_enum_mbus_code,
  737. .enum_frame_size = imx334_enum_frame_size,
  738. .get_fmt = imx334_get_pad_format,
  739. .set_fmt = imx334_set_pad_format,
  740. };
  741. static const struct v4l2_subdev_ops imx334_subdev_ops = {
  742. .video = &imx334_video_ops,
  743. .pad = &imx334_pad_ops,
  744. };
  745. /**
  746. * imx334_power_on() - Sensor power on sequence
  747. * @dev: pointer to i2c device
  748. *
  749. * Return: 0 if successful, error code otherwise.
  750. */
  751. static int imx334_power_on(struct device *dev)
  752. {
  753. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  754. struct imx334 *imx334 = to_imx334(sd);
  755. int ret;
  756. gpiod_set_value_cansleep(imx334->reset_gpio, 1);
  757. ret = clk_prepare_enable(imx334->inclk);
  758. if (ret) {
  759. dev_err(imx334->dev, "fail to enable inclk");
  760. goto error_reset;
  761. }
  762. usleep_range(18000, 20000);
  763. return 0;
  764. error_reset:
  765. gpiod_set_value_cansleep(imx334->reset_gpio, 0);
  766. return ret;
  767. }
  768. /**
  769. * imx334_power_off() - Sensor power off sequence
  770. * @dev: pointer to i2c device
  771. *
  772. * Return: 0 if successful, error code otherwise.
  773. */
  774. static int imx334_power_off(struct device *dev)
  775. {
  776. struct v4l2_subdev *sd = dev_get_drvdata(dev);
  777. struct imx334 *imx334 = to_imx334(sd);
  778. gpiod_set_value_cansleep(imx334->reset_gpio, 0);
  779. clk_disable_unprepare(imx334->inclk);
  780. return 0;
  781. }
  782. /**
  783. * imx334_init_controls() - Initialize sensor subdevice controls
  784. * @imx334: pointer to imx334 device
  785. *
  786. * Return: 0 if successful, error code otherwise.
  787. */
  788. static int imx334_init_controls(struct imx334 *imx334)
  789. {
  790. struct v4l2_ctrl_handler *ctrl_hdlr = &imx334->ctrl_handler;
  791. const struct imx334_mode *mode = imx334->cur_mode;
  792. u32 lpfr;
  793. int ret;
  794. ret = v4l2_ctrl_handler_init(ctrl_hdlr, 6);
  795. if (ret)
  796. return ret;
  797. /* Serialize controls with sensor device */
  798. ctrl_hdlr->lock = &imx334->mutex;
  799. /* Initialize exposure and gain */
  800. lpfr = mode->vblank + mode->height;
  801. imx334->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  802. &imx334_ctrl_ops,
  803. V4L2_CID_EXPOSURE,
  804. IMX334_EXPOSURE_MIN,
  805. lpfr - IMX334_EXPOSURE_OFFSET,
  806. IMX334_EXPOSURE_STEP,
  807. IMX334_EXPOSURE_DEFAULT);
  808. imx334->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  809. &imx334_ctrl_ops,
  810. V4L2_CID_ANALOGUE_GAIN,
  811. IMX334_AGAIN_MIN,
  812. IMX334_AGAIN_MAX,
  813. IMX334_AGAIN_STEP,
  814. IMX334_AGAIN_DEFAULT);
  815. v4l2_ctrl_cluster(2, &imx334->exp_ctrl);
  816. imx334->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  817. &imx334_ctrl_ops,
  818. V4L2_CID_VBLANK,
  819. mode->vblank_min,
  820. mode->vblank_max,
  821. 1, mode->vblank);
  822. /* Read only controls */
  823. imx334->pclk_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  824. &imx334_ctrl_ops,
  825. V4L2_CID_PIXEL_RATE,
  826. mode->pclk, mode->pclk,
  827. 1, mode->pclk);
  828. imx334->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr,
  829. &imx334_ctrl_ops,
  830. V4L2_CID_LINK_FREQ,
  831. ARRAY_SIZE(link_freq) -
  832. 1,
  833. mode->link_freq_idx,
  834. link_freq);
  835. if (imx334->link_freq_ctrl)
  836. imx334->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  837. imx334->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
  838. &imx334_ctrl_ops,
  839. V4L2_CID_HBLANK,
  840. IMX334_REG_MIN,
  841. IMX334_REG_MAX,
  842. 1, mode->hblank);
  843. if (imx334->hblank_ctrl)
  844. imx334->hblank_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
  845. if (ctrl_hdlr->error) {
  846. dev_err(imx334->dev, "control init failed: %d",
  847. ctrl_hdlr->error);
  848. v4l2_ctrl_handler_free(ctrl_hdlr);
  849. return ctrl_hdlr->error;
  850. }
  851. imx334->sd.ctrl_handler = ctrl_hdlr;
  852. return 0;
  853. }
  854. /**
  855. * imx334_probe() - I2C client device binding
  856. * @client: pointer to i2c client device
  857. *
  858. * Return: 0 if successful, error code otherwise.
  859. */
  860. static int imx334_probe(struct i2c_client *client)
  861. {
  862. struct imx334 *imx334;
  863. int ret;
  864. imx334 = devm_kzalloc(&client->dev, sizeof(*imx334), GFP_KERNEL);
  865. if (!imx334)
  866. return -ENOMEM;
  867. imx334->dev = &client->dev;
  868. /* Initialize subdev */
  869. v4l2_i2c_subdev_init(&imx334->sd, client, &imx334_subdev_ops);
  870. ret = imx334_parse_hw_config(imx334);
  871. if (ret) {
  872. dev_err(imx334->dev, "HW configuration is not supported");
  873. return ret;
  874. }
  875. mutex_init(&imx334->mutex);
  876. ret = imx334_power_on(imx334->dev);
  877. if (ret) {
  878. dev_err(imx334->dev, "failed to power-on the sensor");
  879. goto error_mutex_destroy;
  880. }
  881. /* Check module identity */
  882. ret = imx334_detect(imx334);
  883. if (ret) {
  884. dev_err(imx334->dev, "failed to find sensor: %d", ret);
  885. goto error_power_off;
  886. }
  887. /* Set default mode to max resolution */
  888. imx334->cur_mode = &supported_mode;
  889. imx334->vblank = imx334->cur_mode->vblank;
  890. ret = imx334_init_controls(imx334);
  891. if (ret) {
  892. dev_err(imx334->dev, "failed to init controls: %d", ret);
  893. goto error_power_off;
  894. }
  895. /* Initialize subdev */
  896. imx334->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  897. imx334->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  898. /* Initialize source pad */
  899. imx334->pad.flags = MEDIA_PAD_FL_SOURCE;
  900. ret = media_entity_pads_init(&imx334->sd.entity, 1, &imx334->pad);
  901. if (ret) {
  902. dev_err(imx334->dev, "failed to init entity pads: %d", ret);
  903. goto error_handler_free;
  904. }
  905. ret = v4l2_async_register_subdev_sensor(&imx334->sd);
  906. if (ret < 0) {
  907. dev_err(imx334->dev,
  908. "failed to register async subdev: %d", ret);
  909. goto error_media_entity;
  910. }
  911. pm_runtime_set_active(imx334->dev);
  912. pm_runtime_enable(imx334->dev);
  913. pm_runtime_idle(imx334->dev);
  914. return 0;
  915. error_media_entity:
  916. media_entity_cleanup(&imx334->sd.entity);
  917. error_handler_free:
  918. v4l2_ctrl_handler_free(imx334->sd.ctrl_handler);
  919. error_power_off:
  920. imx334_power_off(imx334->dev);
  921. error_mutex_destroy:
  922. mutex_destroy(&imx334->mutex);
  923. return ret;
  924. }
  925. /**
  926. * imx334_remove() - I2C client device unbinding
  927. * @client: pointer to I2C client device
  928. *
  929. * Return: 0 if successful, error code otherwise.
  930. */
  931. static void imx334_remove(struct i2c_client *client)
  932. {
  933. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  934. struct imx334 *imx334 = to_imx334(sd);
  935. v4l2_async_unregister_subdev(sd);
  936. media_entity_cleanup(&sd->entity);
  937. v4l2_ctrl_handler_free(sd->ctrl_handler);
  938. pm_runtime_disable(&client->dev);
  939. pm_runtime_suspended(&client->dev);
  940. mutex_destroy(&imx334->mutex);
  941. }
  942. static const struct dev_pm_ops imx334_pm_ops = {
  943. SET_RUNTIME_PM_OPS(imx334_power_off, imx334_power_on, NULL)
  944. };
  945. static const struct of_device_id imx334_of_match[] = {
  946. { .compatible = "sony,imx334" },
  947. { }
  948. };
  949. MODULE_DEVICE_TABLE(of, imx334_of_match);
  950. static struct i2c_driver imx334_driver = {
  951. .probe_new = imx334_probe,
  952. .remove = imx334_remove,
  953. .driver = {
  954. .name = "imx334",
  955. .pm = &imx334_pm_ops,
  956. .of_match_table = imx334_of_match,
  957. },
  958. };
  959. module_i2c_driver(imx334_driver);
  960. MODULE_DESCRIPTION("Sony imx334 sensor driver");
  961. MODULE_LICENSE("GPL");