xref: /openbmc/linux/drivers/media/i2c/imx334.c (revision 745b7908)
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  * @hblank: Horizontal blanking in lines
83  * @vblank: Vertical blanking in lines
84  * @vblank_min: Minimal vertical blanking in lines
85  * @vblank_max: Maximum vertical blanking in lines
86  * @pclk: Sensor pixel clock
87  * @link_freq_idx: Link frequency index
88  * @reg_list: Register list for sensor mode
89  */
90 struct imx334_mode {
91 	u32 width;
92 	u32 height;
93 	u32 hblank;
94 	u32 vblank;
95 	u32 vblank_min;
96 	u32 vblank_max;
97 	u64 pclk;
98 	u32 link_freq_idx;
99 	struct imx334_reg_list reg_list;
100 };
101 
102 /**
103  * struct imx334 - imx334 sensor device structure
104  * @dev: Pointer to generic device
105  * @client: Pointer to i2c client
106  * @sd: V4L2 sub-device
107  * @pad: Media pad. Only one pad supported
108  * @reset_gpio: Sensor reset gpio
109  * @inclk: Sensor input clock
110  * @ctrl_handler: V4L2 control handler
111  * @link_freq_ctrl: Pointer to link frequency control
112  * @pclk_ctrl: Pointer to pixel clock control
113  * @hblank_ctrl: Pointer to horizontal blanking control
114  * @vblank_ctrl: Pointer to vertical blanking control
115  * @exp_ctrl: Pointer to exposure control
116  * @again_ctrl: Pointer to analog gain control
117  * @vblank: Vertical blanking in lines
118  * @cur_mode: Pointer to current selected sensor mode
119  * @mutex: Mutex for serializing sensor controls
120  * @cur_code: current selected format code
121  * @streaming: Flag indicating streaming state
122  */
123 struct imx334 {
124 	struct device *dev;
125 	struct i2c_client *client;
126 	struct v4l2_subdev sd;
127 	struct media_pad pad;
128 	struct gpio_desc *reset_gpio;
129 	struct clk *inclk;
130 	struct v4l2_ctrl_handler ctrl_handler;
131 	struct v4l2_ctrl *link_freq_ctrl;
132 	struct v4l2_ctrl *pclk_ctrl;
133 	struct v4l2_ctrl *hblank_ctrl;
134 	struct v4l2_ctrl *vblank_ctrl;
135 	struct {
136 		struct v4l2_ctrl *exp_ctrl;
137 		struct v4l2_ctrl *again_ctrl;
138 	};
139 	u32 vblank;
140 	const struct imx334_mode *cur_mode;
141 	struct mutex mutex;
142 	u32 cur_code;
143 	bool streaming;
144 };
145 
146 static const s64 link_freq[] = {
147 	IMX334_LINK_FREQ,
148 };
149 
150 /* Sensor mode registers for 1920x1080@30fps */
151 static const struct imx334_reg mode_1920x1080_regs[] = {
152 	{0x3000, 0x01},
153 	{0x3018, 0x04},
154 	{0x3030, 0xca},
155 	{0x3031, 0x08},
156 	{0x3032, 0x00},
157 	{0x3034, 0x4c},
158 	{0x3035, 0x04},
159 	{0x302c, 0xf0},
160 	{0x302d, 0x03},
161 	{0x302e, 0x80},
162 	{0x302f, 0x07},
163 	{0x3074, 0xcc},
164 	{0x3075, 0x02},
165 	{0x308e, 0xcd},
166 	{0x308f, 0x02},
167 	{0x3076, 0x38},
168 	{0x3077, 0x04},
169 	{0x3090, 0x38},
170 	{0x3091, 0x04},
171 	{0x3308, 0x38},
172 	{0x3309, 0x04},
173 	{0x30C6, 0x00},
174 	{0x30c7, 0x00},
175 	{0x30ce, 0x00},
176 	{0x30cf, 0x00},
177 	{0x30d8, 0x18},
178 	{0x30d9, 0x0a},
179 	{0x304c, 0x00},
180 	{0x304e, 0x00},
181 	{0x304f, 0x00},
182 	{0x3050, 0x00},
183 	{0x30b6, 0x00},
184 	{0x30b7, 0x00},
185 	{0x3116, 0x08},
186 	{0x3117, 0x00},
187 	{0x31a0, 0x20},
188 	{0x31a1, 0x0f},
189 	{0x300c, 0x3b},
190 	{0x300d, 0x29},
191 	{0x314c, 0x29},
192 	{0x314d, 0x01},
193 	{0x315a, 0x06},
194 	{0x3168, 0xa0},
195 	{0x316a, 0x7e},
196 	{0x319e, 0x02},
197 	{0x3199, 0x00},
198 	{0x319d, 0x00},
199 	{0x31dd, 0x03},
200 	{0x3300, 0x00},
201 	{0x341c, 0xff},
202 	{0x341d, 0x01},
203 	{0x3a01, 0x03},
204 	{0x3a18, 0x7f},
205 	{0x3a19, 0x00},
206 	{0x3a1a, 0x37},
207 	{0x3a1b, 0x00},
208 	{0x3a1c, 0x37},
209 	{0x3a1d, 0x00},
210 	{0x3a1e, 0xf7},
211 	{0x3a1f, 0x00},
212 	{0x3a20, 0x3f},
213 	{0x3a21, 0x00},
214 	{0x3a20, 0x6f},
215 	{0x3a21, 0x00},
216 	{0x3a20, 0x3f},
217 	{0x3a21, 0x00},
218 	{0x3a20, 0x5f},
219 	{0x3a21, 0x00},
220 	{0x3a20, 0x2f},
221 	{0x3a21, 0x00},
222 	{0x3078, 0x02},
223 	{0x3079, 0x00},
224 	{0x307a, 0x00},
225 	{0x307b, 0x00},
226 	{0x3080, 0x02},
227 	{0x3081, 0x00},
228 	{0x3082, 0x00},
229 	{0x3083, 0x00},
230 	{0x3088, 0x02},
231 	{0x3094, 0x00},
232 	{0x3095, 0x00},
233 	{0x3096, 0x00},
234 	{0x309b, 0x02},
235 	{0x309c, 0x00},
236 	{0x309d, 0x00},
237 	{0x309e, 0x00},
238 	{0x30a4, 0x00},
239 	{0x30a5, 0x00},
240 	{0x3288, 0x21},
241 	{0x328a, 0x02},
242 	{0x3414, 0x05},
243 	{0x3416, 0x18},
244 	{0x35Ac, 0x0e},
245 	{0x3648, 0x01},
246 	{0x364a, 0x04},
247 	{0x364c, 0x04},
248 	{0x3678, 0x01},
249 	{0x367c, 0x31},
250 	{0x367e, 0x31},
251 	{0x3708, 0x02},
252 	{0x3714, 0x01},
253 	{0x3715, 0x02},
254 	{0x3716, 0x02},
255 	{0x3717, 0x02},
256 	{0x371c, 0x3d},
257 	{0x371d, 0x3f},
258 	{0x372c, 0x00},
259 	{0x372d, 0x00},
260 	{0x372e, 0x46},
261 	{0x372f, 0x00},
262 	{0x3730, 0x89},
263 	{0x3731, 0x00},
264 	{0x3732, 0x08},
265 	{0x3733, 0x01},
266 	{0x3734, 0xfe},
267 	{0x3735, 0x05},
268 	{0x375d, 0x00},
269 	{0x375e, 0x00},
270 	{0x375f, 0x61},
271 	{0x3760, 0x06},
272 	{0x3768, 0x1b},
273 	{0x3769, 0x1b},
274 	{0x376a, 0x1a},
275 	{0x376b, 0x19},
276 	{0x376c, 0x18},
277 	{0x376d, 0x14},
278 	{0x376e, 0x0f},
279 	{0x3776, 0x00},
280 	{0x3777, 0x00},
281 	{0x3778, 0x46},
282 	{0x3779, 0x00},
283 	{0x377a, 0x08},
284 	{0x377b, 0x01},
285 	{0x377c, 0x45},
286 	{0x377d, 0x01},
287 	{0x377e, 0x23},
288 	{0x377f, 0x02},
289 	{0x3780, 0xd9},
290 	{0x3781, 0x03},
291 	{0x3782, 0xf5},
292 	{0x3783, 0x06},
293 	{0x3784, 0xa5},
294 	{0x3788, 0x0f},
295 	{0x378a, 0xd9},
296 	{0x378b, 0x03},
297 	{0x378c, 0xeb},
298 	{0x378d, 0x05},
299 	{0x378e, 0x87},
300 	{0x378f, 0x06},
301 	{0x3790, 0xf5},
302 	{0x3792, 0x43},
303 	{0x3794, 0x7a},
304 	{0x3796, 0xa1},
305 	{0x37b0, 0x37},
306 	{0x3e04, 0x0e},
307 	{0x30e8, 0x50},
308 	{0x30e9, 0x00},
309 	{0x3e04, 0x0e},
310 	{0x3002, 0x00},
311 };
312 
313 /* Sensor mode registers for 3840x2160@30fps */
314 static const struct imx334_reg mode_3840x2160_regs[] = {
315 	{0x3000, 0x01},
316 	{0x3002, 0x00},
317 	{0x3018, 0x04},
318 	{0x37b0, 0x36},
319 	{0x304c, 0x00},
320 	{0x300c, 0x3b},
321 	{0x300d, 0x2a},
322 	{0x3034, 0x26},
323 	{0x3035, 0x02},
324 	{0x314c, 0x29},
325 	{0x314d, 0x01},
326 	{0x315a, 0x02},
327 	{0x3168, 0xa0},
328 	{0x316a, 0x7e},
329 	{0x3288, 0x21},
330 	{0x328a, 0x02},
331 	{0x302c, 0x3c},
332 	{0x302d, 0x00},
333 	{0x302e, 0x00},
334 	{0x302f, 0x0f},
335 	{0x3076, 0x70},
336 	{0x3077, 0x08},
337 	{0x3090, 0x70},
338 	{0x3091, 0x08},
339 	{0x30d8, 0x20},
340 	{0x30d9, 0x12},
341 	{0x3308, 0x70},
342 	{0x3309, 0x08},
343 	{0x3414, 0x05},
344 	{0x3416, 0x18},
345 	{0x35ac, 0x0e},
346 	{0x3648, 0x01},
347 	{0x364a, 0x04},
348 	{0x364c, 0x04},
349 	{0x3678, 0x01},
350 	{0x367c, 0x31},
351 	{0x367e, 0x31},
352 	{0x3708, 0x02},
353 	{0x3714, 0x01},
354 	{0x3715, 0x02},
355 	{0x3716, 0x02},
356 	{0x3717, 0x02},
357 	{0x371c, 0x3d},
358 	{0x371d, 0x3f},
359 	{0x372c, 0x00},
360 	{0x372d, 0x00},
361 	{0x372e, 0x46},
362 	{0x372f, 0x00},
363 	{0x3730, 0x89},
364 	{0x3731, 0x00},
365 	{0x3732, 0x08},
366 	{0x3733, 0x01},
367 	{0x3734, 0xfe},
368 	{0x3735, 0x05},
369 	{0x375d, 0x00},
370 	{0x375e, 0x00},
371 	{0x375f, 0x61},
372 	{0x3760, 0x06},
373 	{0x3768, 0x1b},
374 	{0x3769, 0x1b},
375 	{0x376a, 0x1a},
376 	{0x376b, 0x19},
377 	{0x376c, 0x18},
378 	{0x376d, 0x14},
379 	{0x376e, 0x0f},
380 	{0x3776, 0x00},
381 	{0x3777, 0x00},
382 	{0x3778, 0x46},
383 	{0x3779, 0x00},
384 	{0x377a, 0x08},
385 	{0x377b, 0x01},
386 	{0x377c, 0x45},
387 	{0x377d, 0x01},
388 	{0x377e, 0x23},
389 	{0x377f, 0x02},
390 	{0x3780, 0xd9},
391 	{0x3781, 0x03},
392 	{0x3782, 0xf5},
393 	{0x3783, 0x06},
394 	{0x3784, 0xa5},
395 	{0x3788, 0x0f},
396 	{0x378a, 0xd9},
397 	{0x378b, 0x03},
398 	{0x378c, 0xeb},
399 	{0x378d, 0x05},
400 	{0x378e, 0x87},
401 	{0x378f, 0x06},
402 	{0x3790, 0xf5},
403 	{0x3792, 0x43},
404 	{0x3794, 0x7a},
405 	{0x3796, 0xa1},
406 	{0x3e04, 0x0e},
407 	{0x319e, 0x00},
408 	{0x3a00, 0x01},
409 	{0x3a18, 0xbf},
410 	{0x3a19, 0x00},
411 	{0x3a1a, 0x67},
412 	{0x3a1b, 0x00},
413 	{0x3a1c, 0x6f},
414 	{0x3a1d, 0x00},
415 	{0x3a1e, 0xd7},
416 	{0x3a1f, 0x01},
417 	{0x3a20, 0x6f},
418 	{0x3a21, 0x00},
419 	{0x3a22, 0xcf},
420 	{0x3a23, 0x00},
421 	{0x3a24, 0x6f},
422 	{0x3a25, 0x00},
423 	{0x3a26, 0xb7},
424 	{0x3a27, 0x00},
425 	{0x3a28, 0x5f},
426 	{0x3a29, 0x00},
427 };
428 
429 static const struct imx334_reg raw10_framefmt_regs[] = {
430 	{0x3050, 0x00},
431 	{0x319d, 0x00},
432 	{0x341c, 0xff},
433 	{0x341d, 0x01},
434 };
435 
436 static const struct imx334_reg raw12_framefmt_regs[] = {
437 	{0x3050, 0x01},
438 	{0x319d, 0x01},
439 	{0x341c, 0x47},
440 	{0x341d, 0x00},
441 };
442 
443 static const u32 imx334_mbus_codes[] = {
444 	MEDIA_BUS_FMT_SRGGB12_1X12,
445 	MEDIA_BUS_FMT_SRGGB10_1X10,
446 };
447 
448 /* Supported sensor mode configurations */
449 static const struct imx334_mode supported_modes[] = {
450 	{
451 		.width = 3840,
452 		.height = 2160,
453 		.hblank = 560,
454 		.vblank = 2340,
455 		.vblank_min = 90,
456 		.vblank_max = 132840,
457 		.pclk = 594000000,
458 		.link_freq_idx = 0,
459 		.reg_list = {
460 			.num_of_regs = ARRAY_SIZE(mode_3840x2160_regs),
461 			.regs = mode_3840x2160_regs,
462 		},
463 	}, {
464 		.width = 1920,
465 		.height = 1080,
466 		.hblank = 2480,
467 		.vblank = 1170,
468 		.vblank_min = 45,
469 		.vblank_max = 132840,
470 		.pclk = 297000000,
471 		.link_freq_idx = 0,
472 		.reg_list = {
473 			.num_of_regs = ARRAY_SIZE(mode_1920x1080_regs),
474 			.regs = mode_1920x1080_regs,
475 		},
476 	},
477 };
478 
479 /**
480  * to_imx334() - imv334 V4L2 sub-device to imx334 device.
481  * @subdev: pointer to imx334 V4L2 sub-device
482  *
483  * Return: pointer to imx334 device
484  */
485 static inline struct imx334 *to_imx334(struct v4l2_subdev *subdev)
486 {
487 	return container_of(subdev, struct imx334, sd);
488 }
489 
490 /**
491  * imx334_read_reg() - Read registers.
492  * @imx334: pointer to imx334 device
493  * @reg: register address
494  * @len: length of bytes to read. Max supported bytes is 4
495  * @val: pointer to register value to be filled.
496  *
497  * Big endian register addresses with little endian values.
498  *
499  * Return: 0 if successful, error code otherwise.
500  */
501 static int imx334_read_reg(struct imx334 *imx334, u16 reg, u32 len, u32 *val)
502 {
503 	struct i2c_client *client = v4l2_get_subdevdata(&imx334->sd);
504 	struct i2c_msg msgs[2] = {0};
505 	u8 addr_buf[2] = {0};
506 	u8 data_buf[4] = {0};
507 	int ret;
508 
509 	if (WARN_ON(len > 4))
510 		return -EINVAL;
511 
512 	put_unaligned_be16(reg, addr_buf);
513 
514 	/* Write register address */
515 	msgs[0].addr = client->addr;
516 	msgs[0].flags = 0;
517 	msgs[0].len = ARRAY_SIZE(addr_buf);
518 	msgs[0].buf = addr_buf;
519 
520 	/* Read data from register */
521 	msgs[1].addr = client->addr;
522 	msgs[1].flags = I2C_M_RD;
523 	msgs[1].len = len;
524 	msgs[1].buf = data_buf;
525 
526 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
527 	if (ret != ARRAY_SIZE(msgs))
528 		return -EIO;
529 
530 	*val = get_unaligned_le32(data_buf);
531 
532 	return 0;
533 }
534 
535 /**
536  * imx334_write_reg() - Write register
537  * @imx334: pointer to imx334 device
538  * @reg: register address
539  * @len: length of bytes. Max supported bytes is 4
540  * @val: register value
541  *
542  * Big endian register addresses with little endian values.
543  *
544  * Return: 0 if successful, error code otherwise.
545  */
546 static int imx334_write_reg(struct imx334 *imx334, u16 reg, u32 len, u32 val)
547 {
548 	struct i2c_client *client = v4l2_get_subdevdata(&imx334->sd);
549 	u8 buf[6] = {0};
550 
551 	if (WARN_ON(len > 4))
552 		return -EINVAL;
553 
554 	put_unaligned_be16(reg, buf);
555 	put_unaligned_le32(val, buf + 2);
556 	if (i2c_master_send(client, buf, len + 2) != len + 2)
557 		return -EIO;
558 
559 	return 0;
560 }
561 
562 /**
563  * imx334_write_regs() - Write a list of registers
564  * @imx334: pointer to imx334 device
565  * @regs: list of registers to be written
566  * @len: length of registers array
567  *
568  * Return: 0 if successful, error code otherwise.
569  */
570 static int imx334_write_regs(struct imx334 *imx334,
571 			     const struct imx334_reg *regs, u32 len)
572 {
573 	unsigned int i;
574 	int ret;
575 
576 	for (i = 0; i < len; i++) {
577 		ret = imx334_write_reg(imx334, regs[i].address, 1, regs[i].val);
578 		if (ret)
579 			return ret;
580 	}
581 
582 	return 0;
583 }
584 
585 /**
586  * imx334_update_controls() - Update control ranges based on streaming mode
587  * @imx334: pointer to imx334 device
588  * @mode: pointer to imx334_mode sensor mode
589  *
590  * Return: 0 if successful, error code otherwise.
591  */
592 static int imx334_update_controls(struct imx334 *imx334,
593 				  const struct imx334_mode *mode)
594 {
595 	int ret;
596 
597 	ret = __v4l2_ctrl_s_ctrl(imx334->link_freq_ctrl, mode->link_freq_idx);
598 	if (ret)
599 		return ret;
600 
601 	ret = __v4l2_ctrl_modify_range(imx334->hblank_ctrl, mode->hblank,
602 				       mode->hblank, 1, mode->hblank);
603 	if (ret)
604 		return ret;
605 
606 	return __v4l2_ctrl_modify_range(imx334->vblank_ctrl, mode->vblank_min,
607 					mode->vblank_max, 1, mode->vblank);
608 }
609 
610 /**
611  * imx334_update_exp_gain() - Set updated exposure and gain
612  * @imx334: pointer to imx334 device
613  * @exposure: updated exposure value
614  * @gain: updated analog gain value
615  *
616  * Return: 0 if successful, error code otherwise.
617  */
618 static int imx334_update_exp_gain(struct imx334 *imx334, u32 exposure, u32 gain)
619 {
620 	u32 lpfr, shutter;
621 	int ret;
622 
623 	lpfr = imx334->vblank + imx334->cur_mode->height;
624 	shutter = lpfr - exposure;
625 
626 	dev_dbg(imx334->dev, "Set long exp %u analog gain %u sh0 %u lpfr %u",
627 		exposure, gain, shutter, lpfr);
628 
629 	ret = imx334_write_reg(imx334, IMX334_REG_HOLD, 1, 1);
630 	if (ret)
631 		return ret;
632 
633 	ret = imx334_write_reg(imx334, IMX334_REG_LPFR, 3, lpfr);
634 	if (ret)
635 		goto error_release_group_hold;
636 
637 	ret = imx334_write_reg(imx334, IMX334_REG_SHUTTER, 3, shutter);
638 	if (ret)
639 		goto error_release_group_hold;
640 
641 	ret = imx334_write_reg(imx334, IMX334_REG_AGAIN, 1, gain);
642 
643 error_release_group_hold:
644 	imx334_write_reg(imx334, IMX334_REG_HOLD, 1, 0);
645 
646 	return ret;
647 }
648 
649 /**
650  * imx334_set_ctrl() - Set subdevice control
651  * @ctrl: pointer to v4l2_ctrl structure
652  *
653  * Supported controls:
654  * - V4L2_CID_VBLANK
655  * - cluster controls:
656  *   - V4L2_CID_ANALOGUE_GAIN
657  *   - V4L2_CID_EXPOSURE
658  *
659  * Return: 0 if successful, error code otherwise.
660  */
661 static int imx334_set_ctrl(struct v4l2_ctrl *ctrl)
662 {
663 	struct imx334 *imx334 =
664 		container_of(ctrl->handler, struct imx334, ctrl_handler);
665 	u32 analog_gain;
666 	u32 exposure;
667 	int ret;
668 
669 	switch (ctrl->id) {
670 	case V4L2_CID_VBLANK:
671 		imx334->vblank = imx334->vblank_ctrl->val;
672 
673 		dev_dbg(imx334->dev, "Received vblank %u, new lpfr %u",
674 			imx334->vblank,
675 			imx334->vblank + imx334->cur_mode->height);
676 
677 		ret = __v4l2_ctrl_modify_range(imx334->exp_ctrl,
678 					       IMX334_EXPOSURE_MIN,
679 					       imx334->vblank +
680 					       imx334->cur_mode->height -
681 					       IMX334_EXPOSURE_OFFSET,
682 					       1, IMX334_EXPOSURE_DEFAULT);
683 		break;
684 	case V4L2_CID_EXPOSURE:
685 
686 		/* Set controls only if sensor is in power on state */
687 		if (!pm_runtime_get_if_in_use(imx334->dev))
688 			return 0;
689 
690 		exposure = ctrl->val;
691 		analog_gain = imx334->again_ctrl->val;
692 
693 		dev_dbg(imx334->dev, "Received exp %u analog gain %u",
694 			exposure, analog_gain);
695 
696 		ret = imx334_update_exp_gain(imx334, exposure, analog_gain);
697 
698 		pm_runtime_put(imx334->dev);
699 
700 		break;
701 	case V4L2_CID_HBLANK:
702 		ret = 0;
703 		break;
704 	default:
705 		dev_err(imx334->dev, "Invalid control %d", ctrl->id);
706 		ret = -EINVAL;
707 	}
708 
709 	return ret;
710 }
711 
712 /* V4l2 subdevice control ops*/
713 static const struct v4l2_ctrl_ops imx334_ctrl_ops = {
714 	.s_ctrl = imx334_set_ctrl,
715 };
716 
717 static int imx334_get_format_code(struct imx334 *imx334, u32 code)
718 {
719 	unsigned int i;
720 
721 	for (i = 0; i < ARRAY_SIZE(imx334_mbus_codes); i++) {
722 		if (imx334_mbus_codes[i] == code)
723 			return imx334_mbus_codes[i];
724 	}
725 
726 	return imx334_mbus_codes[0];
727 }
728 
729 /**
730  * imx334_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes
731  * @sd: pointer to imx334 V4L2 sub-device structure
732  * @sd_state: V4L2 sub-device state
733  * @code: V4L2 sub-device code enumeration need to be filled
734  *
735  * Return: 0 if successful, error code otherwise.
736  */
737 static int imx334_enum_mbus_code(struct v4l2_subdev *sd,
738 				 struct v4l2_subdev_state *sd_state,
739 				 struct v4l2_subdev_mbus_code_enum *code)
740 {
741 	if (code->index >= ARRAY_SIZE(imx334_mbus_codes))
742 		return -EINVAL;
743 
744 	code->code = imx334_mbus_codes[code->index];
745 
746 	return 0;
747 }
748 
749 /**
750  * imx334_enum_frame_size() - Enumerate V4L2 sub-device frame sizes
751  * @sd: pointer to imx334 V4L2 sub-device structure
752  * @sd_state: V4L2 sub-device state
753  * @fsize: V4L2 sub-device size enumeration need to be filled
754  *
755  * Return: 0 if successful, error code otherwise.
756  */
757 static int imx334_enum_frame_size(struct v4l2_subdev *sd,
758 				  struct v4l2_subdev_state *sd_state,
759 				  struct v4l2_subdev_frame_size_enum *fsize)
760 {
761 	struct imx334 *imx334 = to_imx334(sd);
762 	u32 code;
763 
764 	if (fsize->index >= ARRAY_SIZE(supported_modes))
765 		return -EINVAL;
766 
767 	code = imx334_get_format_code(imx334, fsize->code);
768 
769 	if (fsize->code != code)
770 		return -EINVAL;
771 
772 	fsize->min_width = supported_modes[fsize->index].width;
773 	fsize->max_width = fsize->min_width;
774 	fsize->min_height = supported_modes[fsize->index].height;
775 	fsize->max_height = fsize->min_height;
776 
777 	return 0;
778 }
779 
780 /**
781  * imx334_fill_pad_format() - Fill subdevice pad format
782  *                            from selected sensor mode
783  * @imx334: pointer to imx334 device
784  * @mode: pointer to imx334_mode sensor mode
785  * @fmt: V4L2 sub-device format need to be filled
786  */
787 static void imx334_fill_pad_format(struct imx334 *imx334,
788 				   const struct imx334_mode *mode,
789 				   struct v4l2_subdev_format *fmt)
790 {
791 	fmt->format.width = mode->width;
792 	fmt->format.height = mode->height;
793 	fmt->format.field = V4L2_FIELD_NONE;
794 	fmt->format.colorspace = V4L2_COLORSPACE_RAW;
795 	fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
796 	fmt->format.quantization = V4L2_QUANTIZATION_DEFAULT;
797 	fmt->format.xfer_func = V4L2_XFER_FUNC_NONE;
798 }
799 
800 /**
801  * imx334_get_pad_format() - Get subdevice pad format
802  * @sd: pointer to imx334 V4L2 sub-device structure
803  * @sd_state: V4L2 sub-device state
804  * @fmt: V4L2 sub-device format need to be set
805  *
806  * Return: 0 if successful, error code otherwise.
807  */
808 static int imx334_get_pad_format(struct v4l2_subdev *sd,
809 				 struct v4l2_subdev_state *sd_state,
810 				 struct v4l2_subdev_format *fmt)
811 {
812 	struct imx334 *imx334 = to_imx334(sd);
813 
814 	mutex_lock(&imx334->mutex);
815 
816 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
817 		struct v4l2_mbus_framefmt *framefmt;
818 
819 		framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
820 		fmt->format = *framefmt;
821 	} else {
822 		fmt->format.code = imx334->cur_code;
823 		imx334_fill_pad_format(imx334, imx334->cur_mode, fmt);
824 	}
825 
826 	mutex_unlock(&imx334->mutex);
827 
828 	return 0;
829 }
830 
831 /**
832  * imx334_set_pad_format() - Set subdevice pad format
833  * @sd: pointer to imx334 V4L2 sub-device structure
834  * @sd_state: V4L2 sub-device state
835  * @fmt: V4L2 sub-device format need to be set
836  *
837  * Return: 0 if successful, error code otherwise.
838  */
839 static int imx334_set_pad_format(struct v4l2_subdev *sd,
840 				 struct v4l2_subdev_state *sd_state,
841 				 struct v4l2_subdev_format *fmt)
842 {
843 	struct imx334 *imx334 = to_imx334(sd);
844 	const struct imx334_mode *mode;
845 	int ret = 0;
846 
847 	mutex_lock(&imx334->mutex);
848 
849 	mode = v4l2_find_nearest_size(supported_modes,
850 				      ARRAY_SIZE(supported_modes),
851 				      width, height,
852 				      fmt->format.width, fmt->format.height);
853 
854 	imx334_fill_pad_format(imx334, mode, fmt);
855 	fmt->format.code = imx334_get_format_code(imx334, fmt->format.code);
856 
857 	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
858 		struct v4l2_mbus_framefmt *framefmt;
859 
860 		framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
861 		*framefmt = fmt->format;
862 	} else if (imx334->cur_mode != mode || imx334->cur_code != fmt->format.code) {
863 		imx334->cur_code = fmt->format.code;
864 		ret = imx334_update_controls(imx334, mode);
865 		if (!ret)
866 			imx334->cur_mode = mode;
867 	}
868 
869 	mutex_unlock(&imx334->mutex);
870 
871 	return ret;
872 }
873 
874 /**
875  * imx334_init_pad_cfg() - Initialize sub-device pad configuration
876  * @sd: pointer to imx334 V4L2 sub-device structure
877  * @sd_state: V4L2 sub-device state
878  *
879  * Return: 0 if successful, error code otherwise.
880  */
881 static int imx334_init_pad_cfg(struct v4l2_subdev *sd,
882 			       struct v4l2_subdev_state *sd_state)
883 {
884 	struct imx334 *imx334 = to_imx334(sd);
885 	struct v4l2_subdev_format fmt = { 0 };
886 
887 	fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
888 	imx334_fill_pad_format(imx334, &supported_modes[0], &fmt);
889 
890 	return imx334_set_pad_format(sd, sd_state, &fmt);
891 }
892 
893 static int imx334_set_framefmt(struct imx334 *imx334)
894 {
895 	switch (imx334->cur_code) {
896 	case MEDIA_BUS_FMT_SRGGB10_1X10:
897 		return imx334_write_regs(imx334, raw10_framefmt_regs,
898 					 ARRAY_SIZE(raw10_framefmt_regs));
899 
900 	case MEDIA_BUS_FMT_SRGGB12_1X12:
901 		return imx334_write_regs(imx334, raw12_framefmt_regs,
902 					 ARRAY_SIZE(raw12_framefmt_regs));
903 	}
904 
905 	return -EINVAL;
906 }
907 
908 /**
909  * imx334_start_streaming() - Start sensor stream
910  * @imx334: pointer to imx334 device
911  *
912  * Return: 0 if successful, error code otherwise.
913  */
914 static int imx334_start_streaming(struct imx334 *imx334)
915 {
916 	const struct imx334_reg_list *reg_list;
917 	int ret;
918 
919 	/* Write sensor mode registers */
920 	reg_list = &imx334->cur_mode->reg_list;
921 	ret = imx334_write_regs(imx334, reg_list->regs,
922 				reg_list->num_of_regs);
923 	if (ret) {
924 		dev_err(imx334->dev, "fail to write initial registers");
925 		return ret;
926 	}
927 
928 	ret = imx334_set_framefmt(imx334);
929 	if (ret) {
930 		dev_err(imx334->dev, "%s failed to set frame format: %d\n",
931 			__func__, ret);
932 		return ret;
933 	}
934 
935 	/* Setup handler will write actual exposure and gain */
936 	ret =  __v4l2_ctrl_handler_setup(imx334->sd.ctrl_handler);
937 	if (ret) {
938 		dev_err(imx334->dev, "fail to setup handler");
939 		return ret;
940 	}
941 
942 	/* Start streaming */
943 	ret = imx334_write_reg(imx334, IMX334_REG_MODE_SELECT,
944 			       1, IMX334_MODE_STREAMING);
945 	if (ret) {
946 		dev_err(imx334->dev, "fail to start streaming");
947 		return ret;
948 	}
949 
950 	return 0;
951 }
952 
953 /**
954  * imx334_stop_streaming() - Stop sensor stream
955  * @imx334: pointer to imx334 device
956  *
957  * Return: 0 if successful, error code otherwise.
958  */
959 static int imx334_stop_streaming(struct imx334 *imx334)
960 {
961 	return imx334_write_reg(imx334, IMX334_REG_MODE_SELECT,
962 				1, IMX334_MODE_STANDBY);
963 }
964 
965 /**
966  * imx334_set_stream() - Enable sensor streaming
967  * @sd: pointer to imx334 subdevice
968  * @enable: set to enable sensor streaming
969  *
970  * Return: 0 if successful, error code otherwise.
971  */
972 static int imx334_set_stream(struct v4l2_subdev *sd, int enable)
973 {
974 	struct imx334 *imx334 = to_imx334(sd);
975 	int ret;
976 
977 	mutex_lock(&imx334->mutex);
978 
979 	if (imx334->streaming == enable) {
980 		mutex_unlock(&imx334->mutex);
981 		return 0;
982 	}
983 
984 	if (enable) {
985 		ret = pm_runtime_resume_and_get(imx334->dev);
986 		if (ret < 0)
987 			goto error_unlock;
988 
989 		ret = imx334_start_streaming(imx334);
990 		if (ret)
991 			goto error_power_off;
992 	} else {
993 		imx334_stop_streaming(imx334);
994 		pm_runtime_put(imx334->dev);
995 	}
996 
997 	imx334->streaming = enable;
998 
999 	mutex_unlock(&imx334->mutex);
1000 
1001 	return 0;
1002 
1003 error_power_off:
1004 	pm_runtime_put(imx334->dev);
1005 error_unlock:
1006 	mutex_unlock(&imx334->mutex);
1007 
1008 	return ret;
1009 }
1010 
1011 /**
1012  * imx334_detect() - Detect imx334 sensor
1013  * @imx334: pointer to imx334 device
1014  *
1015  * Return: 0 if successful, -EIO if sensor id does not match
1016  */
1017 static int imx334_detect(struct imx334 *imx334)
1018 {
1019 	int ret;
1020 	u32 val;
1021 
1022 	ret = imx334_read_reg(imx334, IMX334_REG_ID, 2, &val);
1023 	if (ret)
1024 		return ret;
1025 
1026 	if (val != IMX334_ID) {
1027 		dev_err(imx334->dev, "chip id mismatch: %x!=%x",
1028 			IMX334_ID, val);
1029 		return -ENXIO;
1030 	}
1031 
1032 	return 0;
1033 }
1034 
1035 /**
1036  * imx334_parse_hw_config() - Parse HW configuration and check if supported
1037  * @imx334: pointer to imx334 device
1038  *
1039  * Return: 0 if successful, error code otherwise.
1040  */
1041 static int imx334_parse_hw_config(struct imx334 *imx334)
1042 {
1043 	struct fwnode_handle *fwnode = dev_fwnode(imx334->dev);
1044 	struct v4l2_fwnode_endpoint bus_cfg = {
1045 		.bus_type = V4L2_MBUS_CSI2_DPHY
1046 	};
1047 	struct fwnode_handle *ep;
1048 	unsigned long rate;
1049 	int ret;
1050 	int i;
1051 
1052 	if (!fwnode)
1053 		return -ENXIO;
1054 
1055 	/* Request optional reset pin */
1056 	imx334->reset_gpio = devm_gpiod_get_optional(imx334->dev, "reset",
1057 						     GPIOD_OUT_LOW);
1058 	if (IS_ERR(imx334->reset_gpio)) {
1059 		dev_err(imx334->dev, "failed to get reset gpio %ld",
1060 			PTR_ERR(imx334->reset_gpio));
1061 		return PTR_ERR(imx334->reset_gpio);
1062 	}
1063 
1064 	/* Get sensor input clock */
1065 	imx334->inclk = devm_clk_get(imx334->dev, NULL);
1066 	if (IS_ERR(imx334->inclk)) {
1067 		dev_err(imx334->dev, "could not get inclk");
1068 		return PTR_ERR(imx334->inclk);
1069 	}
1070 
1071 	rate = clk_get_rate(imx334->inclk);
1072 	if (rate != IMX334_INCLK_RATE) {
1073 		dev_err(imx334->dev, "inclk frequency mismatch");
1074 		return -EINVAL;
1075 	}
1076 
1077 	ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
1078 	if (!ep)
1079 		return -ENXIO;
1080 
1081 	ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
1082 	fwnode_handle_put(ep);
1083 	if (ret)
1084 		return ret;
1085 
1086 	if (bus_cfg.bus.mipi_csi2.num_data_lanes != IMX334_NUM_DATA_LANES) {
1087 		dev_err(imx334->dev,
1088 			"number of CSI2 data lanes %d is not supported",
1089 			bus_cfg.bus.mipi_csi2.num_data_lanes);
1090 		ret = -EINVAL;
1091 		goto done_endpoint_free;
1092 	}
1093 
1094 	if (!bus_cfg.nr_of_link_frequencies) {
1095 		dev_err(imx334->dev, "no link frequencies defined");
1096 		ret = -EINVAL;
1097 		goto done_endpoint_free;
1098 	}
1099 
1100 	for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
1101 		if (bus_cfg.link_frequencies[i] == IMX334_LINK_FREQ)
1102 			goto done_endpoint_free;
1103 
1104 	ret = -EINVAL;
1105 
1106 done_endpoint_free:
1107 	v4l2_fwnode_endpoint_free(&bus_cfg);
1108 
1109 	return ret;
1110 }
1111 
1112 /* V4l2 subdevice ops */
1113 static const struct v4l2_subdev_video_ops imx334_video_ops = {
1114 	.s_stream = imx334_set_stream,
1115 };
1116 
1117 static const struct v4l2_subdev_pad_ops imx334_pad_ops = {
1118 	.init_cfg = imx334_init_pad_cfg,
1119 	.enum_mbus_code = imx334_enum_mbus_code,
1120 	.enum_frame_size = imx334_enum_frame_size,
1121 	.get_fmt = imx334_get_pad_format,
1122 	.set_fmt = imx334_set_pad_format,
1123 };
1124 
1125 static const struct v4l2_subdev_ops imx334_subdev_ops = {
1126 	.video = &imx334_video_ops,
1127 	.pad = &imx334_pad_ops,
1128 };
1129 
1130 /**
1131  * imx334_power_on() - Sensor power on sequence
1132  * @dev: pointer to i2c device
1133  *
1134  * Return: 0 if successful, error code otherwise.
1135  */
1136 static int imx334_power_on(struct device *dev)
1137 {
1138 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
1139 	struct imx334 *imx334 = to_imx334(sd);
1140 	int ret;
1141 
1142 	gpiod_set_value_cansleep(imx334->reset_gpio, 1);
1143 
1144 	ret = clk_prepare_enable(imx334->inclk);
1145 	if (ret) {
1146 		dev_err(imx334->dev, "fail to enable inclk");
1147 		goto error_reset;
1148 	}
1149 
1150 	usleep_range(18000, 20000);
1151 
1152 	return 0;
1153 
1154 error_reset:
1155 	gpiod_set_value_cansleep(imx334->reset_gpio, 0);
1156 
1157 	return ret;
1158 }
1159 
1160 /**
1161  * imx334_power_off() - Sensor power off sequence
1162  * @dev: pointer to i2c device
1163  *
1164  * Return: 0 if successful, error code otherwise.
1165  */
1166 static int imx334_power_off(struct device *dev)
1167 {
1168 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
1169 	struct imx334 *imx334 = to_imx334(sd);
1170 
1171 	gpiod_set_value_cansleep(imx334->reset_gpio, 0);
1172 
1173 	clk_disable_unprepare(imx334->inclk);
1174 
1175 	return 0;
1176 }
1177 
1178 /**
1179  * imx334_init_controls() - Initialize sensor subdevice controls
1180  * @imx334: pointer to imx334 device
1181  *
1182  * Return: 0 if successful, error code otherwise.
1183  */
1184 static int imx334_init_controls(struct imx334 *imx334)
1185 {
1186 	struct v4l2_ctrl_handler *ctrl_hdlr = &imx334->ctrl_handler;
1187 	const struct imx334_mode *mode = imx334->cur_mode;
1188 	u32 lpfr;
1189 	int ret;
1190 
1191 	ret = v4l2_ctrl_handler_init(ctrl_hdlr, 6);
1192 	if (ret)
1193 		return ret;
1194 
1195 	/* Serialize controls with sensor device */
1196 	ctrl_hdlr->lock = &imx334->mutex;
1197 
1198 	/* Initialize exposure and gain */
1199 	lpfr = mode->vblank + mode->height;
1200 	imx334->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1201 					     &imx334_ctrl_ops,
1202 					     V4L2_CID_EXPOSURE,
1203 					     IMX334_EXPOSURE_MIN,
1204 					     lpfr - IMX334_EXPOSURE_OFFSET,
1205 					     IMX334_EXPOSURE_STEP,
1206 					     IMX334_EXPOSURE_DEFAULT);
1207 
1208 	imx334->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1209 					       &imx334_ctrl_ops,
1210 					       V4L2_CID_ANALOGUE_GAIN,
1211 					       IMX334_AGAIN_MIN,
1212 					       IMX334_AGAIN_MAX,
1213 					       IMX334_AGAIN_STEP,
1214 					       IMX334_AGAIN_DEFAULT);
1215 
1216 	v4l2_ctrl_cluster(2, &imx334->exp_ctrl);
1217 
1218 	imx334->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1219 						&imx334_ctrl_ops,
1220 						V4L2_CID_VBLANK,
1221 						mode->vblank_min,
1222 						mode->vblank_max,
1223 						1, mode->vblank);
1224 
1225 	/* Read only controls */
1226 	imx334->pclk_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1227 					      &imx334_ctrl_ops,
1228 					      V4L2_CID_PIXEL_RATE,
1229 					      mode->pclk, mode->pclk,
1230 					      1, mode->pclk);
1231 
1232 	imx334->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr,
1233 							&imx334_ctrl_ops,
1234 							V4L2_CID_LINK_FREQ,
1235 							ARRAY_SIZE(link_freq) -
1236 							1,
1237 							mode->link_freq_idx,
1238 							link_freq);
1239 	if (imx334->link_freq_ctrl)
1240 		imx334->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1241 
1242 	imx334->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1243 						&imx334_ctrl_ops,
1244 						V4L2_CID_HBLANK,
1245 						IMX334_REG_MIN,
1246 						IMX334_REG_MAX,
1247 						1, mode->hblank);
1248 	if (imx334->hblank_ctrl)
1249 		imx334->hblank_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1250 
1251 	if (ctrl_hdlr->error) {
1252 		dev_err(imx334->dev, "control init failed: %d",
1253 			ctrl_hdlr->error);
1254 		v4l2_ctrl_handler_free(ctrl_hdlr);
1255 		return ctrl_hdlr->error;
1256 	}
1257 
1258 	imx334->sd.ctrl_handler = ctrl_hdlr;
1259 
1260 	return 0;
1261 }
1262 
1263 /**
1264  * imx334_probe() - I2C client device binding
1265  * @client: pointer to i2c client device
1266  *
1267  * Return: 0 if successful, error code otherwise.
1268  */
1269 static int imx334_probe(struct i2c_client *client)
1270 {
1271 	struct imx334 *imx334;
1272 	int ret;
1273 
1274 	imx334 = devm_kzalloc(&client->dev, sizeof(*imx334), GFP_KERNEL);
1275 	if (!imx334)
1276 		return -ENOMEM;
1277 
1278 	imx334->dev = &client->dev;
1279 
1280 	/* Initialize subdev */
1281 	v4l2_i2c_subdev_init(&imx334->sd, client, &imx334_subdev_ops);
1282 
1283 	ret = imx334_parse_hw_config(imx334);
1284 	if (ret) {
1285 		dev_err(imx334->dev, "HW configuration is not supported");
1286 		return ret;
1287 	}
1288 
1289 	mutex_init(&imx334->mutex);
1290 
1291 	ret = imx334_power_on(imx334->dev);
1292 	if (ret) {
1293 		dev_err(imx334->dev, "failed to power-on the sensor");
1294 		goto error_mutex_destroy;
1295 	}
1296 
1297 	/* Check module identity */
1298 	ret = imx334_detect(imx334);
1299 	if (ret) {
1300 		dev_err(imx334->dev, "failed to find sensor: %d", ret);
1301 		goto error_power_off;
1302 	}
1303 
1304 	/* Set default mode to max resolution */
1305 	imx334->cur_mode = &supported_modes[0];
1306 	imx334->cur_code = imx334_mbus_codes[0];
1307 	imx334->vblank = imx334->cur_mode->vblank;
1308 
1309 	ret = imx334_init_controls(imx334);
1310 	if (ret) {
1311 		dev_err(imx334->dev, "failed to init controls: %d", ret);
1312 		goto error_power_off;
1313 	}
1314 
1315 	/* Initialize subdev */
1316 	imx334->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1317 	imx334->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1318 
1319 	/* Initialize source pad */
1320 	imx334->pad.flags = MEDIA_PAD_FL_SOURCE;
1321 	ret = media_entity_pads_init(&imx334->sd.entity, 1, &imx334->pad);
1322 	if (ret) {
1323 		dev_err(imx334->dev, "failed to init entity pads: %d", ret);
1324 		goto error_handler_free;
1325 	}
1326 
1327 	ret = v4l2_async_register_subdev_sensor(&imx334->sd);
1328 	if (ret < 0) {
1329 		dev_err(imx334->dev,
1330 			"failed to register async subdev: %d", ret);
1331 		goto error_media_entity;
1332 	}
1333 
1334 	pm_runtime_set_active(imx334->dev);
1335 	pm_runtime_enable(imx334->dev);
1336 	pm_runtime_idle(imx334->dev);
1337 
1338 	return 0;
1339 
1340 error_media_entity:
1341 	media_entity_cleanup(&imx334->sd.entity);
1342 error_handler_free:
1343 	v4l2_ctrl_handler_free(imx334->sd.ctrl_handler);
1344 error_power_off:
1345 	imx334_power_off(imx334->dev);
1346 error_mutex_destroy:
1347 	mutex_destroy(&imx334->mutex);
1348 
1349 	return ret;
1350 }
1351 
1352 /**
1353  * imx334_remove() - I2C client device unbinding
1354  * @client: pointer to I2C client device
1355  *
1356  * Return: 0 if successful, error code otherwise.
1357  */
1358 static void imx334_remove(struct i2c_client *client)
1359 {
1360 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1361 	struct imx334 *imx334 = to_imx334(sd);
1362 
1363 	v4l2_async_unregister_subdev(sd);
1364 	media_entity_cleanup(&sd->entity);
1365 	v4l2_ctrl_handler_free(sd->ctrl_handler);
1366 
1367 	pm_runtime_disable(&client->dev);
1368 	pm_runtime_suspended(&client->dev);
1369 
1370 	mutex_destroy(&imx334->mutex);
1371 }
1372 
1373 static const struct dev_pm_ops imx334_pm_ops = {
1374 	SET_RUNTIME_PM_OPS(imx334_power_off, imx334_power_on, NULL)
1375 };
1376 
1377 static const struct of_device_id imx334_of_match[] = {
1378 	{ .compatible = "sony,imx334" },
1379 	{ }
1380 };
1381 
1382 MODULE_DEVICE_TABLE(of, imx334_of_match);
1383 
1384 static struct i2c_driver imx334_driver = {
1385 	.probe_new = imx334_probe,
1386 	.remove = imx334_remove,
1387 	.driver = {
1388 		.name = "imx334",
1389 		.pm = &imx334_pm_ops,
1390 		.of_match_table = imx334_of_match,
1391 	},
1392 };
1393 
1394 module_i2c_driver(imx334_driver);
1395 
1396 MODULE_DESCRIPTION("Sony imx334 sensor driver");
1397 MODULE_LICENSE("GPL");
1398