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