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