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