imx335.c 26 KB

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