xref: /openbmc/linux/drivers/media/i2c/imx412.c (revision 80d0624d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Sony imx412 Camera Sensor Driver
4  *
5  * Copyright (C) 2021 Intel Corporation
6  */
7 #include <asm/unaligned.h>
8 
9 #include <linux/clk.h>
10 #include <linux/delay.h>
11 #include <linux/i2c.h>
12 #include <linux/module.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/regulator/consumer.h>
15 
16 #include <media/v4l2-ctrls.h>
17 #include <media/v4l2-fwnode.h>
18 #include <media/v4l2-subdev.h>
19 
20 /* Streaming Mode */
21 #define IMX412_REG_MODE_SELECT	0x0100
22 #define IMX412_MODE_STANDBY	0x00
23 #define IMX412_MODE_STREAMING	0x01
24 
25 /* Lines per frame */
26 #define IMX412_REG_LPFR		0x0340
27 
28 /* Chip ID */
29 #define IMX412_REG_ID		0x0016
30 #define IMX412_ID		0x577
31 
32 /* Exposure control */
33 #define IMX412_REG_EXPOSURE_CIT	0x0202
34 #define IMX412_EXPOSURE_MIN	8
35 #define IMX412_EXPOSURE_OFFSET	22
36 #define IMX412_EXPOSURE_STEP	1
37 #define IMX412_EXPOSURE_DEFAULT	0x0648
38 
39 /* Analog gain control */
40 #define IMX412_REG_AGAIN	0x0204
41 #define IMX412_AGAIN_MIN	0
42 #define IMX412_AGAIN_MAX	978
43 #define IMX412_AGAIN_STEP	1
44 #define IMX412_AGAIN_DEFAULT	0
45 
46 /* Group hold register */
47 #define IMX412_REG_HOLD		0x0104
48 
49 /* Input clock rate */
50 #define IMX412_INCLK_RATE	24000000
51 
52 /* CSI2 HW configuration */
53 #define IMX412_LINK_FREQ	600000000
54 #define IMX412_NUM_DATA_LANES	4
55 
56 #define IMX412_REG_MIN		0x00
57 #define IMX412_REG_MAX		0xffff
58 
59 /**
60  * struct imx412_reg - imx412 sensor register
61  * @address: Register address
62  * @val: Register value
63  */
64 struct imx412_reg {
65 	u16 address;
66 	u8 val;
67 };
68 
69 /**
70  * struct imx412_reg_list - imx412 sensor register list
71  * @num_of_regs: Number of registers in the list
72  * @regs: Pointer to register list
73  */
74 struct imx412_reg_list {
75 	u32 num_of_regs;
76 	const struct imx412_reg *regs;
77 };
78 
79 /**
80  * struct imx412_mode - imx412 sensor mode structure
81  * @width: Frame width
82  * @height: Frame height
83  * @code: Format code
84  * @hblank: Horizontal blanking in lines
85  * @vblank: Vertical blanking in lines
86  * @vblank_min: Minimum vertical blanking in lines
87  * @vblank_max: Maximum vertical blanking in lines
88  * @pclk: Sensor pixel clock
89  * @link_freq_idx: Link frequency index
90  * @reg_list: Register list for sensor mode
91  */
92 struct imx412_mode {
93 	u32 width;
94 	u32 height;
95 	u32 code;
96 	u32 hblank;
97 	u32 vblank;
98 	u32 vblank_min;
99 	u32 vblank_max;
100 	u64 pclk;
101 	u32 link_freq_idx;
102 	struct imx412_reg_list reg_list;
103 };
104 
105 static const char * const imx412_supply_names[] = {
106 	"dovdd",	/* Digital I/O power */
107 	"avdd",		/* Analog power */
108 	"dvdd",		/* Digital core power */
109 };
110 
111 /**
112  * struct imx412 - imx412 sensor device structure
113  * @dev: Pointer to generic device
114  * @client: Pointer to i2c client
115  * @sd: V4L2 sub-device
116  * @pad: Media pad. Only one pad supported
117  * @reset_gpio: Sensor reset gpio
118  * @inclk: Sensor input clock
119  * @supplies: Regulator supplies
120  * @ctrl_handler: V4L2 control handler
121  * @link_freq_ctrl: Pointer to link frequency control
122  * @pclk_ctrl: Pointer to pixel clock control
123  * @hblank_ctrl: Pointer to horizontal blanking control
124  * @vblank_ctrl: Pointer to vertical blanking control
125  * @exp_ctrl: Pointer to exposure control
126  * @again_ctrl: Pointer to analog gain control
127  * @vblank: Vertical blanking in lines
128  * @cur_mode: Pointer to current selected sensor mode
129  * @mutex: Mutex for serializing sensor controls
130  * @streaming: Flag indicating streaming state
131  */
132 struct imx412 {
133 	struct device *dev;
134 	struct i2c_client *client;
135 	struct v4l2_subdev sd;
136 	struct media_pad pad;
137 	struct gpio_desc *reset_gpio;
138 	struct clk *inclk;
139 	struct regulator_bulk_data supplies[ARRAY_SIZE(imx412_supply_names)];
140 	struct v4l2_ctrl_handler ctrl_handler;
141 	struct v4l2_ctrl *link_freq_ctrl;
142 	struct v4l2_ctrl *pclk_ctrl;
143 	struct v4l2_ctrl *hblank_ctrl;
144 	struct v4l2_ctrl *vblank_ctrl;
145 	struct {
146 		struct v4l2_ctrl *exp_ctrl;
147 		struct v4l2_ctrl *again_ctrl;
148 	};
149 	u32 vblank;
150 	const struct imx412_mode *cur_mode;
151 	struct mutex mutex;
152 	bool streaming;
153 };
154 
155 static const s64 link_freq[] = {
156 	IMX412_LINK_FREQ,
157 };
158 
159 /* Sensor mode registers */
160 static const struct imx412_reg mode_4056x3040_regs[] = {
161 	{0x0136, 0x18},
162 	{0x0137, 0x00},
163 	{0x3c7e, 0x08},
164 	{0x3c7f, 0x02},
165 	{0x38a8, 0x1f},
166 	{0x38a9, 0xff},
167 	{0x38aa, 0x1f},
168 	{0x38ab, 0xff},
169 	{0x55d4, 0x00},
170 	{0x55d5, 0x00},
171 	{0x55d6, 0x07},
172 	{0x55d7, 0xff},
173 	{0x55e8, 0x07},
174 	{0x55e9, 0xff},
175 	{0x55ea, 0x00},
176 	{0x55eb, 0x00},
177 	{0x575c, 0x07},
178 	{0x575d, 0xff},
179 	{0x575e, 0x00},
180 	{0x575f, 0x00},
181 	{0x5764, 0x00},
182 	{0x5765, 0x00},
183 	{0x5766, 0x07},
184 	{0x5767, 0xff},
185 	{0x5974, 0x04},
186 	{0x5975, 0x01},
187 	{0x5f10, 0x09},
188 	{0x5f11, 0x92},
189 	{0x5f12, 0x32},
190 	{0x5f13, 0x72},
191 	{0x5f14, 0x16},
192 	{0x5f15, 0xba},
193 	{0x5f17, 0x13},
194 	{0x5f18, 0x24},
195 	{0x5f19, 0x60},
196 	{0x5f1a, 0xe3},
197 	{0x5f1b, 0xad},
198 	{0x5f1c, 0x74},
199 	{0x5f2d, 0x25},
200 	{0x5f5c, 0xd0},
201 	{0x6a22, 0x00},
202 	{0x6a23, 0x1d},
203 	{0x7ba8, 0x00},
204 	{0x7ba9, 0x00},
205 	{0x886b, 0x00},
206 	{0x9002, 0x0a},
207 	{0x9004, 0x1a},
208 	{0x9214, 0x93},
209 	{0x9215, 0x69},
210 	{0x9216, 0x93},
211 	{0x9217, 0x6b},
212 	{0x9218, 0x93},
213 	{0x9219, 0x6d},
214 	{0x921a, 0x57},
215 	{0x921b, 0x58},
216 	{0x921c, 0x57},
217 	{0x921d, 0x59},
218 	{0x921e, 0x57},
219 	{0x921f, 0x5a},
220 	{0x9220, 0x57},
221 	{0x9221, 0x5b},
222 	{0x9222, 0x93},
223 	{0x9223, 0x02},
224 	{0x9224, 0x93},
225 	{0x9225, 0x03},
226 	{0x9226, 0x93},
227 	{0x9227, 0x04},
228 	{0x9228, 0x93},
229 	{0x9229, 0x05},
230 	{0x922a, 0x98},
231 	{0x922b, 0x21},
232 	{0x922c, 0xb2},
233 	{0x922d, 0xdb},
234 	{0x922e, 0xb2},
235 	{0x922f, 0xdc},
236 	{0x9230, 0xb2},
237 	{0x9231, 0xdd},
238 	{0x9232, 0xe2},
239 	{0x9233, 0xe1},
240 	{0x9234, 0xb2},
241 	{0x9235, 0xe2},
242 	{0x9236, 0xb2},
243 	{0x9237, 0xe3},
244 	{0x9238, 0xb7},
245 	{0x9239, 0xb9},
246 	{0x923a, 0xb7},
247 	{0x923b, 0xbb},
248 	{0x923c, 0xb7},
249 	{0x923d, 0xbc},
250 	{0x923e, 0xb7},
251 	{0x923f, 0xc5},
252 	{0x9240, 0xb7},
253 	{0x9241, 0xc7},
254 	{0x9242, 0xb7},
255 	{0x9243, 0xc9},
256 	{0x9244, 0x98},
257 	{0x9245, 0x56},
258 	{0x9246, 0x98},
259 	{0x9247, 0x55},
260 	{0x9380, 0x00},
261 	{0x9381, 0x62},
262 	{0x9382, 0x00},
263 	{0x9383, 0x56},
264 	{0x9384, 0x00},
265 	{0x9385, 0x52},
266 	{0x9388, 0x00},
267 	{0x9389, 0x55},
268 	{0x938a, 0x00},
269 	{0x938b, 0x55},
270 	{0x938c, 0x00},
271 	{0x938d, 0x41},
272 	{0x5078, 0x01},
273 	{0x0112, 0x0a},
274 	{0x0113, 0x0a},
275 	{0x0114, 0x03},
276 	{0x0342, 0x11},
277 	{0x0343, 0xa0},
278 	{0x0340, 0x0d},
279 	{0x0341, 0xda},
280 	{0x3210, 0x00},
281 	{0x0344, 0x00},
282 	{0x0345, 0x00},
283 	{0x0346, 0x00},
284 	{0x0347, 0x00},
285 	{0x0348, 0x0f},
286 	{0x0349, 0xd7},
287 	{0x034a, 0x0b},
288 	{0x034b, 0xdf},
289 	{0x00e3, 0x00},
290 	{0x00e4, 0x00},
291 	{0x00e5, 0x01},
292 	{0x00fc, 0x0a},
293 	{0x00fd, 0x0a},
294 	{0x00fe, 0x0a},
295 	{0x00ff, 0x0a},
296 	{0xe013, 0x00},
297 	{0x0220, 0x00},
298 	{0x0221, 0x11},
299 	{0x0381, 0x01},
300 	{0x0383, 0x01},
301 	{0x0385, 0x01},
302 	{0x0387, 0x01},
303 	{0x0900, 0x00},
304 	{0x0901, 0x11},
305 	{0x0902, 0x00},
306 	{0x3140, 0x02},
307 	{0x3241, 0x11},
308 	{0x3250, 0x03},
309 	{0x3e10, 0x00},
310 	{0x3e11, 0x00},
311 	{0x3f0d, 0x00},
312 	{0x3f42, 0x00},
313 	{0x3f43, 0x00},
314 	{0x0401, 0x00},
315 	{0x0404, 0x00},
316 	{0x0405, 0x10},
317 	{0x0408, 0x00},
318 	{0x0409, 0x00},
319 	{0x040a, 0x00},
320 	{0x040b, 0x00},
321 	{0x040c, 0x0f},
322 	{0x040d, 0xd8},
323 	{0x040e, 0x0b},
324 	{0x040f, 0xe0},
325 	{0x034c, 0x0f},
326 	{0x034d, 0xd8},
327 	{0x034e, 0x0b},
328 	{0x034f, 0xe0},
329 	{0x0301, 0x05},
330 	{0x0303, 0x02},
331 	{0x0305, 0x04},
332 	{0x0306, 0x00},
333 	{0x0307, 0xc8},
334 	{0x0309, 0x0a},
335 	{0x030b, 0x01},
336 	{0x030d, 0x02},
337 	{0x030e, 0x01},
338 	{0x030f, 0x5e},
339 	{0x0310, 0x00},
340 	{0x0820, 0x12},
341 	{0x0821, 0xc0},
342 	{0x0822, 0x00},
343 	{0x0823, 0x00},
344 	{0x3e20, 0x01},
345 	{0x3e37, 0x00},
346 	{0x3f50, 0x00},
347 	{0x3f56, 0x00},
348 	{0x3f57, 0xe2},
349 	{0x3c0a, 0x5a},
350 	{0x3c0b, 0x55},
351 	{0x3c0c, 0x28},
352 	{0x3c0d, 0x07},
353 	{0x3c0e, 0xff},
354 	{0x3c0f, 0x00},
355 	{0x3c10, 0x00},
356 	{0x3c11, 0x02},
357 	{0x3c12, 0x00},
358 	{0x3c13, 0x03},
359 	{0x3c14, 0x00},
360 	{0x3c15, 0x00},
361 	{0x3c16, 0x0c},
362 	{0x3c17, 0x0c},
363 	{0x3c18, 0x0c},
364 	{0x3c19, 0x0a},
365 	{0x3c1a, 0x0a},
366 	{0x3c1b, 0x0a},
367 	{0x3c1c, 0x00},
368 	{0x3c1d, 0x00},
369 	{0x3c1e, 0x00},
370 	{0x3c1f, 0x00},
371 	{0x3c20, 0x00},
372 	{0x3c21, 0x00},
373 	{0x3c22, 0x3f},
374 	{0x3c23, 0x0a},
375 	{0x3e35, 0x01},
376 	{0x3f4a, 0x03},
377 	{0x3f4b, 0xbf},
378 	{0x3f26, 0x00},
379 	{0x0202, 0x0d},
380 	{0x0203, 0xc4},
381 	{0x0204, 0x00},
382 	{0x0205, 0x00},
383 	{0x020e, 0x01},
384 	{0x020f, 0x00},
385 	{0x0210, 0x01},
386 	{0x0211, 0x00},
387 	{0x0212, 0x01},
388 	{0x0213, 0x00},
389 	{0x0214, 0x01},
390 	{0x0215, 0x00},
391 	{0xbcf1, 0x00},
392 };
393 
394 /* Supported sensor mode configurations */
395 static const struct imx412_mode supported_mode = {
396 	.width = 4056,
397 	.height = 3040,
398 	.hblank = 456,
399 	.vblank = 506,
400 	.vblank_min = 506,
401 	.vblank_max = 32420,
402 	.pclk = 480000000,
403 	.link_freq_idx = 0,
404 	.code = MEDIA_BUS_FMT_SRGGB10_1X10,
405 	.reg_list = {
406 		.num_of_regs = ARRAY_SIZE(mode_4056x3040_regs),
407 		.regs = mode_4056x3040_regs,
408 	},
409 };
410 
411 /**
412  * to_imx412() - imx412 V4L2 sub-device to imx412 device.
413  * @subdev: pointer to imx412 V4L2 sub-device
414  *
415  * Return: pointer to imx412 device
416  */
417 static inline struct imx412 *to_imx412(struct v4l2_subdev *subdev)
418 {
419 	return container_of(subdev, struct imx412, sd);
420 }
421 
422 /**
423  * imx412_read_reg() - Read registers.
424  * @imx412: pointer to imx412 device
425  * @reg: register address
426  * @len: length of bytes to read. Max supported bytes is 4
427  * @val: pointer to register value to be filled.
428  *
429  * Return: 0 if successful, error code otherwise.
430  */
431 static int imx412_read_reg(struct imx412 *imx412, u16 reg, u32 len, u32 *val)
432 {
433 	struct i2c_client *client = v4l2_get_subdevdata(&imx412->sd);
434 	struct i2c_msg msgs[2] = {0};
435 	u8 addr_buf[2] = {0};
436 	u8 data_buf[4] = {0};
437 	int ret;
438 
439 	if (WARN_ON(len > 4))
440 		return -EINVAL;
441 
442 	put_unaligned_be16(reg, addr_buf);
443 
444 	/* Write register address */
445 	msgs[0].addr = client->addr;
446 	msgs[0].flags = 0;
447 	msgs[0].len = ARRAY_SIZE(addr_buf);
448 	msgs[0].buf = addr_buf;
449 
450 	/* Read data from register */
451 	msgs[1].addr = client->addr;
452 	msgs[1].flags = I2C_M_RD;
453 	msgs[1].len = len;
454 	msgs[1].buf = &data_buf[4 - len];
455 
456 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
457 	if (ret != ARRAY_SIZE(msgs))
458 		return -EIO;
459 
460 	*val = get_unaligned_be32(data_buf);
461 
462 	return 0;
463 }
464 
465 /**
466  * imx412_write_reg() - Write register
467  * @imx412: pointer to imx412 device
468  * @reg: register address
469  * @len: length of bytes. Max supported bytes is 4
470  * @val: register value
471  *
472  * Return: 0 if successful, error code otherwise.
473  */
474 static int imx412_write_reg(struct imx412 *imx412, u16 reg, u32 len, u32 val)
475 {
476 	struct i2c_client *client = v4l2_get_subdevdata(&imx412->sd);
477 	u8 buf[6] = {0};
478 
479 	if (WARN_ON(len > 4))
480 		return -EINVAL;
481 
482 	put_unaligned_be16(reg, buf);
483 	put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
484 	if (i2c_master_send(client, buf, len + 2) != len + 2)
485 		return -EIO;
486 
487 	return 0;
488 }
489 
490 /**
491  * imx412_write_regs() - Write a list of registers
492  * @imx412: pointer to imx412 device
493  * @regs: list of registers to be written
494  * @len: length of registers array
495  *
496  * Return: 0 if successful, error code otherwise.
497  */
498 static int imx412_write_regs(struct imx412 *imx412,
499 			     const struct imx412_reg *regs, u32 len)
500 {
501 	unsigned int i;
502 	int ret;
503 
504 	for (i = 0; i < len; i++) {
505 		ret = imx412_write_reg(imx412, regs[i].address, 1, regs[i].val);
506 		if (ret)
507 			return ret;
508 	}
509 
510 	return 0;
511 }
512 
513 /**
514  * imx412_update_controls() - Update control ranges based on streaming mode
515  * @imx412: pointer to imx412 device
516  * @mode: pointer to imx412_mode sensor mode
517  *
518  * Return: 0 if successful, error code otherwise.
519  */
520 static int imx412_update_controls(struct imx412 *imx412,
521 				  const struct imx412_mode *mode)
522 {
523 	int ret;
524 
525 	ret = __v4l2_ctrl_s_ctrl(imx412->link_freq_ctrl, mode->link_freq_idx);
526 	if (ret)
527 		return ret;
528 
529 	ret = __v4l2_ctrl_s_ctrl(imx412->hblank_ctrl, mode->hblank);
530 	if (ret)
531 		return ret;
532 
533 	return __v4l2_ctrl_modify_range(imx412->vblank_ctrl, mode->vblank_min,
534 					mode->vblank_max, 1, mode->vblank);
535 }
536 
537 /**
538  * imx412_update_exp_gain() - Set updated exposure and gain
539  * @imx412: pointer to imx412 device
540  * @exposure: updated exposure value
541  * @gain: updated analog gain value
542  *
543  * Return: 0 if successful, error code otherwise.
544  */
545 static int imx412_update_exp_gain(struct imx412 *imx412, u32 exposure, u32 gain)
546 {
547 	u32 lpfr;
548 	int ret;
549 
550 	lpfr = imx412->vblank + imx412->cur_mode->height;
551 
552 	dev_dbg(imx412->dev, "Set exp %u, analog gain %u, lpfr %u",
553 		exposure, gain, lpfr);
554 
555 	ret = imx412_write_reg(imx412, IMX412_REG_HOLD, 1, 1);
556 	if (ret)
557 		return ret;
558 
559 	ret = imx412_write_reg(imx412, IMX412_REG_LPFR, 2, lpfr);
560 	if (ret)
561 		goto error_release_group_hold;
562 
563 	ret = imx412_write_reg(imx412, IMX412_REG_EXPOSURE_CIT, 2, exposure);
564 	if (ret)
565 		goto error_release_group_hold;
566 
567 	ret = imx412_write_reg(imx412, IMX412_REG_AGAIN, 2, gain);
568 
569 error_release_group_hold:
570 	imx412_write_reg(imx412, IMX412_REG_HOLD, 1, 0);
571 
572 	return ret;
573 }
574 
575 /**
576  * imx412_set_ctrl() - Set subdevice control
577  * @ctrl: pointer to v4l2_ctrl structure
578  *
579  * Supported controls:
580  * - V4L2_CID_VBLANK
581  * - cluster controls:
582  *   - V4L2_CID_ANALOGUE_GAIN
583  *   - V4L2_CID_EXPOSURE
584  *
585  * Return: 0 if successful, error code otherwise.
586  */
587 static int imx412_set_ctrl(struct v4l2_ctrl *ctrl)
588 {
589 	struct imx412 *imx412 =
590 		container_of(ctrl->handler, struct imx412, ctrl_handler);
591 	u32 analog_gain;
592 	u32 exposure;
593 	int ret;
594 
595 	switch (ctrl->id) {
596 	case V4L2_CID_VBLANK:
597 		imx412->vblank = imx412->vblank_ctrl->val;
598 
599 		dev_dbg(imx412->dev, "Received vblank %u, new lpfr %u",
600 			imx412->vblank,
601 			imx412->vblank + imx412->cur_mode->height);
602 
603 		ret = __v4l2_ctrl_modify_range(imx412->exp_ctrl,
604 					       IMX412_EXPOSURE_MIN,
605 					       imx412->vblank +
606 					       imx412->cur_mode->height -
607 					       IMX412_EXPOSURE_OFFSET,
608 					       1, IMX412_EXPOSURE_DEFAULT);
609 		break;
610 	case V4L2_CID_EXPOSURE:
611 		/* Set controls only if sensor is in power on state */
612 		if (!pm_runtime_get_if_in_use(imx412->dev))
613 			return 0;
614 
615 		exposure = ctrl->val;
616 		analog_gain = imx412->again_ctrl->val;
617 
618 		dev_dbg(imx412->dev, "Received exp %u, analog gain %u",
619 			exposure, analog_gain);
620 
621 		ret = imx412_update_exp_gain(imx412, exposure, analog_gain);
622 
623 		pm_runtime_put(imx412->dev);
624 
625 		break;
626 	default:
627 		dev_err(imx412->dev, "Invalid control %d", ctrl->id);
628 		ret = -EINVAL;
629 	}
630 
631 	return ret;
632 }
633 
634 /* V4l2 subdevice control ops*/
635 static const struct v4l2_ctrl_ops imx412_ctrl_ops = {
636 	.s_ctrl = imx412_set_ctrl,
637 };
638 
639 /**
640  * imx412_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes
641  * @sd: pointer to imx412 V4L2 sub-device structure
642  * @sd_state: V4L2 sub-device configuration
643  * @code: V4L2 sub-device code enumeration need to be filled
644  *
645  * Return: 0 if successful, error code otherwise.
646  */
647 static int imx412_enum_mbus_code(struct v4l2_subdev *sd,
648 				 struct v4l2_subdev_state *sd_state,
649 				 struct v4l2_subdev_mbus_code_enum *code)
650 {
651 	if (code->index > 0)
652 		return -EINVAL;
653 
654 	code->code = supported_mode.code;
655 
656 	return 0;
657 }
658 
659 /**
660  * imx412_enum_frame_size() - Enumerate V4L2 sub-device frame sizes
661  * @sd: pointer to imx412 V4L2 sub-device structure
662  * @sd_state: V4L2 sub-device configuration
663  * @fsize: V4L2 sub-device size enumeration need to be filled
664  *
665  * Return: 0 if successful, error code otherwise.
666  */
667 static int imx412_enum_frame_size(struct v4l2_subdev *sd,
668 				  struct v4l2_subdev_state *sd_state,
669 				  struct v4l2_subdev_frame_size_enum *fsize)
670 {
671 	if (fsize->index > 0)
672 		return -EINVAL;
673 
674 	if (fsize->code != supported_mode.code)
675 		return -EINVAL;
676 
677 	fsize->min_width = supported_mode.width;
678 	fsize->max_width = fsize->min_width;
679 	fsize->min_height = supported_mode.height;
680 	fsize->max_height = fsize->min_height;
681 
682 	return 0;
683 }
684 
685 /**
686  * imx412_fill_pad_format() - Fill subdevice pad format
687  *                            from selected sensor mode
688  * @imx412: pointer to imx412 device
689  * @mode: pointer to imx412_mode sensor mode
690  * @fmt: V4L2 sub-device format need to be filled
691  */
692 static void imx412_fill_pad_format(struct imx412 *imx412,
693 				   const struct imx412_mode *mode,
694 				   struct v4l2_subdev_format *fmt)
695 {
696 	fmt->format.width = mode->width;
697 	fmt->format.height = mode->height;
698 	fmt->format.code = mode->code;
699 	fmt->format.field = V4L2_FIELD_NONE;
700 	fmt->format.colorspace = V4L2_COLORSPACE_RAW;
701 	fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
702 	fmt->format.quantization = V4L2_QUANTIZATION_DEFAULT;
703 	fmt->format.xfer_func = V4L2_XFER_FUNC_NONE;
704 }
705 
706 /**
707  * imx412_get_pad_format() - Get subdevice pad format
708  * @sd: pointer to imx412 V4L2 sub-device structure
709  * @sd_state: V4L2 sub-device configuration
710  * @fmt: V4L2 sub-device format need to be set
711  *
712  * Return: 0 if successful, error code otherwise.
713  */
714 static int imx412_get_pad_format(struct v4l2_subdev *sd,
715 				 struct v4l2_subdev_state *sd_state,
716 				 struct v4l2_subdev_format *fmt)
717 {
718 	struct imx412 *imx412 = to_imx412(sd);
719 
720 	mutex_lock(&imx412->mutex);
721 
722 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
723 		struct v4l2_mbus_framefmt *framefmt;
724 
725 		framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
726 		fmt->format = *framefmt;
727 	} else {
728 		imx412_fill_pad_format(imx412, imx412->cur_mode, fmt);
729 	}
730 
731 	mutex_unlock(&imx412->mutex);
732 
733 	return 0;
734 }
735 
736 /**
737  * imx412_set_pad_format() - Set subdevice pad format
738  * @sd: pointer to imx412 V4L2 sub-device structure
739  * @sd_state: V4L2 sub-device configuration
740  * @fmt: V4L2 sub-device format need to be set
741  *
742  * Return: 0 if successful, error code otherwise.
743  */
744 static int imx412_set_pad_format(struct v4l2_subdev *sd,
745 				 struct v4l2_subdev_state *sd_state,
746 				 struct v4l2_subdev_format *fmt)
747 {
748 	struct imx412 *imx412 = to_imx412(sd);
749 	const struct imx412_mode *mode;
750 	int ret = 0;
751 
752 	mutex_lock(&imx412->mutex);
753 
754 	mode = &supported_mode;
755 	imx412_fill_pad_format(imx412, mode, fmt);
756 
757 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
758 		struct v4l2_mbus_framefmt *framefmt;
759 
760 		framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
761 		*framefmt = fmt->format;
762 	} else {
763 		ret = imx412_update_controls(imx412, mode);
764 		if (!ret)
765 			imx412->cur_mode = mode;
766 	}
767 
768 	mutex_unlock(&imx412->mutex);
769 
770 	return ret;
771 }
772 
773 /**
774  * imx412_init_pad_cfg() - Initialize sub-device pad configuration
775  * @sd: pointer to imx412 V4L2 sub-device structure
776  * @sd_state: V4L2 sub-device configuration
777  *
778  * Return: 0 if successful, error code otherwise.
779  */
780 static int imx412_init_pad_cfg(struct v4l2_subdev *sd,
781 			       struct v4l2_subdev_state *sd_state)
782 {
783 	struct imx412 *imx412 = to_imx412(sd);
784 	struct v4l2_subdev_format fmt = { 0 };
785 
786 	fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
787 	imx412_fill_pad_format(imx412, &supported_mode, &fmt);
788 
789 	return imx412_set_pad_format(sd, sd_state, &fmt);
790 }
791 
792 /**
793  * imx412_start_streaming() - Start sensor stream
794  * @imx412: pointer to imx412 device
795  *
796  * Return: 0 if successful, error code otherwise.
797  */
798 static int imx412_start_streaming(struct imx412 *imx412)
799 {
800 	const struct imx412_reg_list *reg_list;
801 	int ret;
802 
803 	/* Write sensor mode registers */
804 	reg_list = &imx412->cur_mode->reg_list;
805 	ret = imx412_write_regs(imx412, reg_list->regs,
806 				reg_list->num_of_regs);
807 	if (ret) {
808 		dev_err(imx412->dev, "fail to write initial registers");
809 		return ret;
810 	}
811 
812 	/* Setup handler will write actual exposure and gain */
813 	ret =  __v4l2_ctrl_handler_setup(imx412->sd.ctrl_handler);
814 	if (ret) {
815 		dev_err(imx412->dev, "fail to setup handler");
816 		return ret;
817 	}
818 
819 	/* Delay is required before streaming*/
820 	usleep_range(7400, 8000);
821 
822 	/* Start streaming */
823 	ret = imx412_write_reg(imx412, IMX412_REG_MODE_SELECT,
824 			       1, IMX412_MODE_STREAMING);
825 	if (ret) {
826 		dev_err(imx412->dev, "fail to start streaming");
827 		return ret;
828 	}
829 
830 	return 0;
831 }
832 
833 /**
834  * imx412_stop_streaming() - Stop sensor stream
835  * @imx412: pointer to imx412 device
836  *
837  * Return: 0 if successful, error code otherwise.
838  */
839 static int imx412_stop_streaming(struct imx412 *imx412)
840 {
841 	return imx412_write_reg(imx412, IMX412_REG_MODE_SELECT,
842 				1, IMX412_MODE_STANDBY);
843 }
844 
845 /**
846  * imx412_set_stream() - Enable sensor streaming
847  * @sd: pointer to imx412 subdevice
848  * @enable: set to enable sensor streaming
849  *
850  * Return: 0 if successful, error code otherwise.
851  */
852 static int imx412_set_stream(struct v4l2_subdev *sd, int enable)
853 {
854 	struct imx412 *imx412 = to_imx412(sd);
855 	int ret;
856 
857 	mutex_lock(&imx412->mutex);
858 
859 	if (imx412->streaming == enable) {
860 		mutex_unlock(&imx412->mutex);
861 		return 0;
862 	}
863 
864 	if (enable) {
865 		ret = pm_runtime_resume_and_get(imx412->dev);
866 		if (ret)
867 			goto error_unlock;
868 
869 		ret = imx412_start_streaming(imx412);
870 		if (ret)
871 			goto error_power_off;
872 	} else {
873 		imx412_stop_streaming(imx412);
874 		pm_runtime_put(imx412->dev);
875 	}
876 
877 	imx412->streaming = enable;
878 
879 	mutex_unlock(&imx412->mutex);
880 
881 	return 0;
882 
883 error_power_off:
884 	pm_runtime_put(imx412->dev);
885 error_unlock:
886 	mutex_unlock(&imx412->mutex);
887 
888 	return ret;
889 }
890 
891 /**
892  * imx412_detect() - Detect imx412 sensor
893  * @imx412: pointer to imx412 device
894  *
895  * Return: 0 if successful, -EIO if sensor id does not match
896  */
897 static int imx412_detect(struct imx412 *imx412)
898 {
899 	int ret;
900 	u32 val;
901 
902 	ret = imx412_read_reg(imx412, IMX412_REG_ID, 2, &val);
903 	if (ret)
904 		return ret;
905 
906 	if (val != IMX412_ID) {
907 		dev_err(imx412->dev, "chip id mismatch: %x!=%x",
908 			IMX412_ID, val);
909 		return -ENXIO;
910 	}
911 
912 	return 0;
913 }
914 
915 /**
916  * imx412_parse_hw_config() - Parse HW configuration and check if supported
917  * @imx412: pointer to imx412 device
918  *
919  * Return: 0 if successful, error code otherwise.
920  */
921 static int imx412_parse_hw_config(struct imx412 *imx412)
922 {
923 	struct fwnode_handle *fwnode = dev_fwnode(imx412->dev);
924 	struct v4l2_fwnode_endpoint bus_cfg = {
925 		.bus_type = V4L2_MBUS_CSI2_DPHY
926 	};
927 	struct fwnode_handle *ep;
928 	unsigned long rate;
929 	unsigned int i;
930 	int ret;
931 
932 	if (!fwnode)
933 		return -ENXIO;
934 
935 	/* Request optional reset pin */
936 	imx412->reset_gpio = devm_gpiod_get_optional(imx412->dev, "reset",
937 						     GPIOD_OUT_LOW);
938 	if (IS_ERR(imx412->reset_gpio)) {
939 		dev_err(imx412->dev, "failed to get reset gpio %ld",
940 			PTR_ERR(imx412->reset_gpio));
941 		return PTR_ERR(imx412->reset_gpio);
942 	}
943 
944 	/* Get sensor input clock */
945 	imx412->inclk = devm_clk_get(imx412->dev, NULL);
946 	if (IS_ERR(imx412->inclk)) {
947 		dev_err(imx412->dev, "could not get inclk");
948 		return PTR_ERR(imx412->inclk);
949 	}
950 
951 	rate = clk_get_rate(imx412->inclk);
952 	if (rate != IMX412_INCLK_RATE) {
953 		dev_err(imx412->dev, "inclk frequency mismatch");
954 		return -EINVAL;
955 	}
956 
957 	/* Get optional DT defined regulators */
958 	for (i = 0; i < ARRAY_SIZE(imx412_supply_names); i++)
959 		imx412->supplies[i].supply = imx412_supply_names[i];
960 
961 	ret = devm_regulator_bulk_get(imx412->dev,
962 				      ARRAY_SIZE(imx412_supply_names),
963 				      imx412->supplies);
964 	if (ret)
965 		return ret;
966 
967 	ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
968 	if (!ep)
969 		return -ENXIO;
970 
971 	ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
972 	fwnode_handle_put(ep);
973 	if (ret)
974 		return ret;
975 
976 	if (bus_cfg.bus.mipi_csi2.num_data_lanes != IMX412_NUM_DATA_LANES) {
977 		dev_err(imx412->dev,
978 			"number of CSI2 data lanes %d is not supported",
979 			bus_cfg.bus.mipi_csi2.num_data_lanes);
980 		ret = -EINVAL;
981 		goto done_endpoint_free;
982 	}
983 
984 	if (!bus_cfg.nr_of_link_frequencies) {
985 		dev_err(imx412->dev, "no link frequencies defined");
986 		ret = -EINVAL;
987 		goto done_endpoint_free;
988 	}
989 
990 	for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
991 		if (bus_cfg.link_frequencies[i] == IMX412_LINK_FREQ)
992 			goto done_endpoint_free;
993 
994 	ret = -EINVAL;
995 
996 done_endpoint_free:
997 	v4l2_fwnode_endpoint_free(&bus_cfg);
998 
999 	return ret;
1000 }
1001 
1002 /* V4l2 subdevice ops */
1003 static const struct v4l2_subdev_video_ops imx412_video_ops = {
1004 	.s_stream = imx412_set_stream,
1005 };
1006 
1007 static const struct v4l2_subdev_pad_ops imx412_pad_ops = {
1008 	.init_cfg = imx412_init_pad_cfg,
1009 	.enum_mbus_code = imx412_enum_mbus_code,
1010 	.enum_frame_size = imx412_enum_frame_size,
1011 	.get_fmt = imx412_get_pad_format,
1012 	.set_fmt = imx412_set_pad_format,
1013 };
1014 
1015 static const struct v4l2_subdev_ops imx412_subdev_ops = {
1016 	.video = &imx412_video_ops,
1017 	.pad = &imx412_pad_ops,
1018 };
1019 
1020 /**
1021  * imx412_power_on() - Sensor power on sequence
1022  * @dev: pointer to i2c device
1023  *
1024  * Return: 0 if successful, error code otherwise.
1025  */
1026 static int imx412_power_on(struct device *dev)
1027 {
1028 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
1029 	struct imx412 *imx412 = to_imx412(sd);
1030 	int ret;
1031 
1032 	ret = regulator_bulk_enable(ARRAY_SIZE(imx412_supply_names),
1033 				    imx412->supplies);
1034 	if (ret < 0) {
1035 		dev_err(dev, "failed to enable regulators\n");
1036 		return ret;
1037 	}
1038 
1039 	gpiod_set_value_cansleep(imx412->reset_gpio, 0);
1040 
1041 	ret = clk_prepare_enable(imx412->inclk);
1042 	if (ret) {
1043 		dev_err(imx412->dev, "fail to enable inclk");
1044 		goto error_reset;
1045 	}
1046 
1047 	usleep_range(1000, 1200);
1048 
1049 	return 0;
1050 
1051 error_reset:
1052 	gpiod_set_value_cansleep(imx412->reset_gpio, 1);
1053 	regulator_bulk_disable(ARRAY_SIZE(imx412_supply_names),
1054 			       imx412->supplies);
1055 
1056 	return ret;
1057 }
1058 
1059 /**
1060  * imx412_power_off() - Sensor power off sequence
1061  * @dev: pointer to i2c device
1062  *
1063  * Return: 0 if successful, error code otherwise.
1064  */
1065 static int imx412_power_off(struct device *dev)
1066 {
1067 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
1068 	struct imx412 *imx412 = to_imx412(sd);
1069 
1070 	clk_disable_unprepare(imx412->inclk);
1071 
1072 	gpiod_set_value_cansleep(imx412->reset_gpio, 1);
1073 
1074 	regulator_bulk_disable(ARRAY_SIZE(imx412_supply_names),
1075 			       imx412->supplies);
1076 
1077 	return 0;
1078 }
1079 
1080 /**
1081  * imx412_init_controls() - Initialize sensor subdevice controls
1082  * @imx412: pointer to imx412 device
1083  *
1084  * Return: 0 if successful, error code otherwise.
1085  */
1086 static int imx412_init_controls(struct imx412 *imx412)
1087 {
1088 	struct v4l2_ctrl_handler *ctrl_hdlr = &imx412->ctrl_handler;
1089 	const struct imx412_mode *mode = imx412->cur_mode;
1090 	u32 lpfr;
1091 	int ret;
1092 
1093 	ret = v4l2_ctrl_handler_init(ctrl_hdlr, 6);
1094 	if (ret)
1095 		return ret;
1096 
1097 	/* Serialize controls with sensor device */
1098 	ctrl_hdlr->lock = &imx412->mutex;
1099 
1100 	/* Initialize exposure and gain */
1101 	lpfr = mode->vblank + mode->height;
1102 	imx412->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1103 					     &imx412_ctrl_ops,
1104 					     V4L2_CID_EXPOSURE,
1105 					     IMX412_EXPOSURE_MIN,
1106 					     lpfr - IMX412_EXPOSURE_OFFSET,
1107 					     IMX412_EXPOSURE_STEP,
1108 					     IMX412_EXPOSURE_DEFAULT);
1109 
1110 	imx412->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1111 					       &imx412_ctrl_ops,
1112 					       V4L2_CID_ANALOGUE_GAIN,
1113 					       IMX412_AGAIN_MIN,
1114 					       IMX412_AGAIN_MAX,
1115 					       IMX412_AGAIN_STEP,
1116 					       IMX412_AGAIN_DEFAULT);
1117 
1118 	v4l2_ctrl_cluster(2, &imx412->exp_ctrl);
1119 
1120 	imx412->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1121 						&imx412_ctrl_ops,
1122 						V4L2_CID_VBLANK,
1123 						mode->vblank_min,
1124 						mode->vblank_max,
1125 						1, mode->vblank);
1126 
1127 	/* Read only controls */
1128 	imx412->pclk_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1129 					      &imx412_ctrl_ops,
1130 					      V4L2_CID_PIXEL_RATE,
1131 					      mode->pclk, mode->pclk,
1132 					      1, mode->pclk);
1133 
1134 	imx412->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr,
1135 							&imx412_ctrl_ops,
1136 							V4L2_CID_LINK_FREQ,
1137 							ARRAY_SIZE(link_freq) -
1138 							1,
1139 							mode->link_freq_idx,
1140 							link_freq);
1141 	if (imx412->link_freq_ctrl)
1142 		imx412->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1143 
1144 	imx412->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1145 						&imx412_ctrl_ops,
1146 						V4L2_CID_HBLANK,
1147 						IMX412_REG_MIN,
1148 						IMX412_REG_MAX,
1149 						1, mode->hblank);
1150 	if (imx412->hblank_ctrl)
1151 		imx412->hblank_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1152 
1153 	if (ctrl_hdlr->error) {
1154 		dev_err(imx412->dev, "control init failed: %d",
1155 			ctrl_hdlr->error);
1156 		v4l2_ctrl_handler_free(ctrl_hdlr);
1157 		return ctrl_hdlr->error;
1158 	}
1159 
1160 	imx412->sd.ctrl_handler = ctrl_hdlr;
1161 
1162 	return 0;
1163 }
1164 
1165 /**
1166  * imx412_probe() - I2C client device binding
1167  * @client: pointer to i2c client device
1168  *
1169  * Return: 0 if successful, error code otherwise.
1170  */
1171 static int imx412_probe(struct i2c_client *client)
1172 {
1173 	struct imx412 *imx412;
1174 	const char *name;
1175 	int ret;
1176 
1177 	imx412 = devm_kzalloc(&client->dev, sizeof(*imx412), GFP_KERNEL);
1178 	if (!imx412)
1179 		return -ENOMEM;
1180 
1181 	imx412->dev = &client->dev;
1182 	name = device_get_match_data(&client->dev);
1183 	if (!name)
1184 		return -ENODEV;
1185 
1186 	/* Initialize subdev */
1187 	v4l2_i2c_subdev_init(&imx412->sd, client, &imx412_subdev_ops);
1188 
1189 	ret = imx412_parse_hw_config(imx412);
1190 	if (ret) {
1191 		dev_err(imx412->dev, "HW configuration is not supported");
1192 		return ret;
1193 	}
1194 
1195 	mutex_init(&imx412->mutex);
1196 
1197 	ret = imx412_power_on(imx412->dev);
1198 	if (ret) {
1199 		dev_err(imx412->dev, "failed to power-on the sensor");
1200 		goto error_mutex_destroy;
1201 	}
1202 
1203 	/* Check module identity */
1204 	ret = imx412_detect(imx412);
1205 	if (ret) {
1206 		dev_err(imx412->dev, "failed to find sensor: %d", ret);
1207 		goto error_power_off;
1208 	}
1209 
1210 	/* Set default mode to max resolution */
1211 	imx412->cur_mode = &supported_mode;
1212 	imx412->vblank = imx412->cur_mode->vblank;
1213 
1214 	ret = imx412_init_controls(imx412);
1215 	if (ret) {
1216 		dev_err(imx412->dev, "failed to init controls: %d", ret);
1217 		goto error_power_off;
1218 	}
1219 
1220 	/* Initialize subdev */
1221 	imx412->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1222 	imx412->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1223 
1224 	v4l2_i2c_subdev_set_name(&imx412->sd, client, name, NULL);
1225 
1226 	/* Initialize source pad */
1227 	imx412->pad.flags = MEDIA_PAD_FL_SOURCE;
1228 	ret = media_entity_pads_init(&imx412->sd.entity, 1, &imx412->pad);
1229 	if (ret) {
1230 		dev_err(imx412->dev, "failed to init entity pads: %d", ret);
1231 		goto error_handler_free;
1232 	}
1233 
1234 	ret = v4l2_async_register_subdev_sensor(&imx412->sd);
1235 	if (ret < 0) {
1236 		dev_err(imx412->dev,
1237 			"failed to register async subdev: %d", ret);
1238 		goto error_media_entity;
1239 	}
1240 
1241 	pm_runtime_set_active(imx412->dev);
1242 	pm_runtime_enable(imx412->dev);
1243 	pm_runtime_idle(imx412->dev);
1244 
1245 	return 0;
1246 
1247 error_media_entity:
1248 	media_entity_cleanup(&imx412->sd.entity);
1249 error_handler_free:
1250 	v4l2_ctrl_handler_free(imx412->sd.ctrl_handler);
1251 error_power_off:
1252 	imx412_power_off(imx412->dev);
1253 error_mutex_destroy:
1254 	mutex_destroy(&imx412->mutex);
1255 
1256 	return ret;
1257 }
1258 
1259 /**
1260  * imx412_remove() - I2C client device unbinding
1261  * @client: pointer to I2C client device
1262  *
1263  * Return: 0 if successful, error code otherwise.
1264  */
1265 static void imx412_remove(struct i2c_client *client)
1266 {
1267 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1268 	struct imx412 *imx412 = to_imx412(sd);
1269 
1270 	v4l2_async_unregister_subdev(sd);
1271 	media_entity_cleanup(&sd->entity);
1272 	v4l2_ctrl_handler_free(sd->ctrl_handler);
1273 
1274 	pm_runtime_disable(&client->dev);
1275 	if (!pm_runtime_status_suspended(&client->dev))
1276 		imx412_power_off(&client->dev);
1277 	pm_runtime_set_suspended(&client->dev);
1278 
1279 	mutex_destroy(&imx412->mutex);
1280 }
1281 
1282 static const struct dev_pm_ops imx412_pm_ops = {
1283 	SET_RUNTIME_PM_OPS(imx412_power_off, imx412_power_on, NULL)
1284 };
1285 
1286 static const struct of_device_id imx412_of_match[] = {
1287 	{ .compatible = "sony,imx412", .data = "imx412" },
1288 	{ .compatible = "sony,imx577", .data = "imx577" },
1289 	{ }
1290 };
1291 
1292 MODULE_DEVICE_TABLE(of, imx412_of_match);
1293 
1294 static struct i2c_driver imx412_driver = {
1295 	.probe = imx412_probe,
1296 	.remove = imx412_remove,
1297 	.driver = {
1298 		.name = "imx412",
1299 		.pm = &imx412_pm_ops,
1300 		.of_match_table = imx412_of_match,
1301 	},
1302 };
1303 
1304 module_i2c_driver(imx412_driver);
1305 
1306 MODULE_DESCRIPTION("Sony imx412 sensor driver");
1307 MODULE_LICENSE("GPL");
1308