]> Git Repo - J-linux.git/blob - drivers/media/i2c/imx335.c
Merge tag 'kbuild-v6.9' of git://git.kernel.org/pub/scm/linux/kernel/git/masahiroy...
[J-linux.git] / drivers / media / i2c / imx335.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Sony imx335 Camera Sensor Driver
4  *
5  * Copyright (C) 2021 Intel Corporation
6  */
7 #include <asm/unaligned.h>
8
9 #include <linux/clk.h>
10 #include <linux/delay.h>
11 #include <linux/i2c.h>
12 #include <linux/module.h>
13 #include <linux/pm_runtime.h>
14
15 #include <media/v4l2-ctrls.h>
16 #include <media/v4l2-fwnode.h>
17 #include <media/v4l2-subdev.h>
18
19 /* Streaming Mode */
20 #define IMX335_REG_MODE_SELECT  0x3000
21 #define IMX335_MODE_STANDBY     0x01
22 #define IMX335_MODE_STREAMING   0x00
23
24 /* Lines per frame */
25 #define IMX335_REG_LPFR         0x3030
26
27 /* Chip ID */
28 #define IMX335_REG_ID           0x3912
29 #define IMX335_ID               0x00
30
31 /* Exposure control */
32 #define IMX335_REG_SHUTTER      0x3058
33 #define IMX335_EXPOSURE_MIN     1
34 #define IMX335_EXPOSURE_OFFSET  9
35 #define IMX335_EXPOSURE_STEP    1
36 #define IMX335_EXPOSURE_DEFAULT 0x0648
37
38 /* Analog gain control */
39 #define IMX335_REG_AGAIN        0x30e8
40 #define IMX335_AGAIN_MIN        0
41 #define IMX335_AGAIN_MAX        240
42 #define IMX335_AGAIN_STEP       1
43 #define IMX335_AGAIN_DEFAULT    0
44
45 /* Group hold register */
46 #define IMX335_REG_HOLD         0x3001
47
48 /* Test pattern generator */
49 #define IMX335_REG_TPG          0x329e
50 #define IMX335_TPG_ALL_000      0
51 #define IMX335_TPG_ALL_FFF      1
52 #define IMX335_TPG_ALL_555      2
53 #define IMX335_TPG_ALL_AAA      3
54 #define IMX335_TPG_TOG_555_AAA  4
55 #define IMX335_TPG_TOG_AAA_555  5
56 #define IMX335_TPG_TOG_000_555  6
57 #define IMX335_TPG_TOG_555_000  7
58 #define IMX335_TPG_TOG_000_FFF  8
59 #define IMX335_TPG_TOG_FFF_000  9
60 #define IMX335_TPG_H_COLOR_BARS 10
61 #define IMX335_TPG_V_COLOR_BARS 11
62
63 /* Input clock rate */
64 #define IMX335_INCLK_RATE       24000000
65
66 /* CSI2 HW configuration */
67 #define IMX335_LINK_FREQ_594MHz         594000000LL
68 #define IMX335_LINK_FREQ_445MHz         445500000LL
69
70 #define IMX335_NUM_DATA_LANES   4
71
72 #define IMX335_REG_MIN          0x00
73 #define IMX335_REG_MAX          0xfffff
74
75 /* IMX335 native and active pixel array size. */
76 #define IMX335_NATIVE_WIDTH             2616U
77 #define IMX335_NATIVE_HEIGHT            1964U
78 #define IMX335_PIXEL_ARRAY_LEFT         12U
79 #define IMX335_PIXEL_ARRAY_TOP          12U
80 #define IMX335_PIXEL_ARRAY_WIDTH        2592U
81 #define IMX335_PIXEL_ARRAY_HEIGHT       1944U
82
83 /**
84  * struct imx335_reg - imx335 sensor register
85  * @address: Register address
86  * @val: Register value
87  */
88 struct imx335_reg {
89         u16 address;
90         u8 val;
91 };
92
93 /**
94  * struct imx335_reg_list - imx335 sensor register list
95  * @num_of_regs: Number of registers in the list
96  * @regs: Pointer to register list
97  */
98 struct imx335_reg_list {
99         u32 num_of_regs;
100         const struct imx335_reg *regs;
101 };
102
103 static const char * const imx335_supply_name[] = {
104         "avdd", /* Analog (2.9V) supply */
105         "ovdd", /* Digital I/O (1.8V) supply */
106         "dvdd", /* Digital Core (1.2V) supply */
107 };
108
109 /**
110  * struct imx335_mode - imx335 sensor mode structure
111  * @width: Frame width
112  * @height: Frame height
113  * @code: Format code
114  * @hblank: Horizontal blanking in lines
115  * @vblank: Vertical blanking in lines
116  * @vblank_min: Minimum vertical blanking in lines
117  * @vblank_max: Maximum vertical blanking in lines
118  * @pclk: Sensor pixel clock
119  * @reg_list: Register list for sensor mode
120  */
121 struct imx335_mode {
122         u32 width;
123         u32 height;
124         u32 code;
125         u32 hblank;
126         u32 vblank;
127         u32 vblank_min;
128         u32 vblank_max;
129         u64 pclk;
130         struct imx335_reg_list reg_list;
131 };
132
133 /**
134  * struct imx335 - imx335 sensor device structure
135  * @dev: Pointer to generic device
136  * @client: Pointer to i2c client
137  * @sd: V4L2 sub-device
138  * @pad: Media pad. Only one pad supported
139  * @reset_gpio: Sensor reset gpio
140  * @supplies: Regulator supplies to handle power control
141  * @inclk: Sensor input clock
142  * @ctrl_handler: V4L2 control handler
143  * @link_freq_ctrl: Pointer to link frequency control
144  * @pclk_ctrl: Pointer to pixel clock control
145  * @hblank_ctrl: Pointer to horizontal blanking control
146  * @vblank_ctrl: Pointer to vertical blanking control
147  * @exp_ctrl: Pointer to exposure control
148  * @again_ctrl: Pointer to analog gain control
149  * @vblank: Vertical blanking in lines
150  * @cur_mode: Pointer to current selected sensor mode
151  * @mutex: Mutex for serializing sensor controls
152  * @link_freq_bitmap: Menu bitmap for link_freq_ctrl
153  * @cur_mbus_code: Currently selected media bus format code
154  */
155 struct imx335 {
156         struct device *dev;
157         struct i2c_client *client;
158         struct v4l2_subdev sd;
159         struct media_pad pad;
160         struct gpio_desc *reset_gpio;
161         struct regulator_bulk_data supplies[ARRAY_SIZE(imx335_supply_name)];
162
163         struct clk *inclk;
164         struct v4l2_ctrl_handler ctrl_handler;
165         struct v4l2_ctrl *link_freq_ctrl;
166         struct v4l2_ctrl *pclk_ctrl;
167         struct v4l2_ctrl *hblank_ctrl;
168         struct v4l2_ctrl *vblank_ctrl;
169         struct {
170                 struct v4l2_ctrl *exp_ctrl;
171                 struct v4l2_ctrl *again_ctrl;
172         };
173         u32 vblank;
174         const struct imx335_mode *cur_mode;
175         struct mutex mutex;
176         unsigned long link_freq_bitmap;
177         u32 cur_mbus_code;
178 };
179
180 static const char * const imx335_tpg_menu[] = {
181         "Disabled",
182         "All 000h",
183         "All FFFh",
184         "All 555h",
185         "All AAAh",
186         "Toggle 555/AAAh",
187         "Toggle AAA/555h",
188         "Toggle 000/555h",
189         "Toggle 555/000h",
190         "Toggle 000/FFFh",
191         "Toggle FFF/000h",
192         "Horizontal color bars",
193         "Vertical color bars",
194 };
195
196 static const int imx335_tpg_val[] = {
197         IMX335_TPG_ALL_000,
198         IMX335_TPG_ALL_000,
199         IMX335_TPG_ALL_FFF,
200         IMX335_TPG_ALL_555,
201         IMX335_TPG_ALL_AAA,
202         IMX335_TPG_TOG_555_AAA,
203         IMX335_TPG_TOG_AAA_555,
204         IMX335_TPG_TOG_000_555,
205         IMX335_TPG_TOG_555_000,
206         IMX335_TPG_TOG_000_FFF,
207         IMX335_TPG_TOG_FFF_000,
208         IMX335_TPG_H_COLOR_BARS,
209         IMX335_TPG_V_COLOR_BARS,
210 };
211
212 /* Sensor mode registers */
213 static const struct imx335_reg mode_2592x1940_regs[] = {
214         {0x3000, 0x01},
215         {0x3002, 0x00},
216         {0x3018, 0x04},
217         {0x302c, 0x3c},
218         {0x302e, 0x20},
219         {0x3056, 0x94},
220         {0x3074, 0xc8},
221         {0x3076, 0x28},
222         {0x304c, 0x00},
223         {0x31a1, 0x00},
224         {0x3288, 0x21},
225         {0x328a, 0x02},
226         {0x3414, 0x05},
227         {0x3416, 0x18},
228         {0x3648, 0x01},
229         {0x364a, 0x04},
230         {0x364c, 0x04},
231         {0x3678, 0x01},
232         {0x367c, 0x31},
233         {0x367e, 0x31},
234         {0x3706, 0x10},
235         {0x3708, 0x03},
236         {0x3714, 0x02},
237         {0x3715, 0x02},
238         {0x3716, 0x01},
239         {0x3717, 0x03},
240         {0x371c, 0x3d},
241         {0x371d, 0x3f},
242         {0x372c, 0x00},
243         {0x372d, 0x00},
244         {0x372e, 0x46},
245         {0x372f, 0x00},
246         {0x3730, 0x89},
247         {0x3731, 0x00},
248         {0x3732, 0x08},
249         {0x3733, 0x01},
250         {0x3734, 0xfe},
251         {0x3735, 0x05},
252         {0x3740, 0x02},
253         {0x375d, 0x00},
254         {0x375e, 0x00},
255         {0x375f, 0x11},
256         {0x3760, 0x01},
257         {0x3768, 0x1b},
258         {0x3769, 0x1b},
259         {0x376a, 0x1b},
260         {0x376b, 0x1b},
261         {0x376c, 0x1a},
262         {0x376d, 0x17},
263         {0x376e, 0x0f},
264         {0x3776, 0x00},
265         {0x3777, 0x00},
266         {0x3778, 0x46},
267         {0x3779, 0x00},
268         {0x377a, 0x89},
269         {0x377b, 0x00},
270         {0x377c, 0x08},
271         {0x377d, 0x01},
272         {0x377e, 0x23},
273         {0x377f, 0x02},
274         {0x3780, 0xd9},
275         {0x3781, 0x03},
276         {0x3782, 0xf5},
277         {0x3783, 0x06},
278         {0x3784, 0xa5},
279         {0x3788, 0x0f},
280         {0x378a, 0xd9},
281         {0x378b, 0x03},
282         {0x378c, 0xeb},
283         {0x378d, 0x05},
284         {0x378e, 0x87},
285         {0x378f, 0x06},
286         {0x3790, 0xf5},
287         {0x3792, 0x43},
288         {0x3794, 0x7a},
289         {0x3796, 0xa1},
290         {0x37b0, 0x36},
291         {0x3a00, 0x00},
292 };
293
294 static const struct imx335_reg raw10_framefmt_regs[] = {
295         {0x3050, 0x00},
296         {0x319d, 0x00},
297         {0x341c, 0xff},
298         {0x341d, 0x01},
299 };
300
301 static const struct imx335_reg raw12_framefmt_regs[] = {
302         {0x3050, 0x01},
303         {0x319d, 0x01},
304         {0x341c, 0x47},
305         {0x341d, 0x00},
306 };
307
308 static const struct imx335_reg mipi_data_rate_1188Mbps[] = {
309         {0x300c, 0x3b},
310         {0x300d, 0x2a},
311         {0x314c, 0xc6},
312         {0x314d, 0x00},
313         {0x315a, 0x02},
314         {0x3168, 0xa0},
315         {0x316a, 0x7e},
316         {0x319e, 0x01},
317         {0x3a18, 0x8f},
318         {0x3a1a, 0x4f},
319         {0x3a1c, 0x47},
320         {0x3a1e, 0x37},
321         {0x3a1f, 0x01},
322         {0x3a20, 0x4f},
323         {0x3a22, 0x87},
324         {0x3a24, 0x4f},
325         {0x3a26, 0x7f},
326         {0x3a28, 0x3f},
327 };
328
329 static const struct imx335_reg mipi_data_rate_891Mbps[] = {
330         {0x300c, 0x3b},
331         {0x300d, 0x2a},
332         {0x314c, 0x29},
333         {0x314d, 0x01},
334         {0x315a, 0x06},
335         {0x3168, 0xa0},
336         {0x316a, 0x7e},
337         {0x319e, 0x02},
338         {0x3a18, 0x7f},
339         {0x3a1a, 0x37},
340         {0x3a1c, 0x37},
341         {0x3a1e, 0xf7},
342         {0x3a20, 0x3f},
343         {0x3a22, 0x6f},
344         {0x3a24, 0x3f},
345         {0x3a26, 0x5f},
346         {0x3a28, 0x2f},
347 };
348
349 static const s64 link_freq[] = {
350         /* Corresponds to 1188Mbps data lane rate */
351         IMX335_LINK_FREQ_594MHz,
352         /* Corresponds to 891Mbps data lane rate */
353         IMX335_LINK_FREQ_445MHz,
354 };
355
356 static const struct imx335_reg_list link_freq_reglist[] = {
357         {
358                 .num_of_regs = ARRAY_SIZE(mipi_data_rate_1188Mbps),
359                 .regs = mipi_data_rate_1188Mbps,
360         },
361         {
362                 .num_of_regs = ARRAY_SIZE(mipi_data_rate_891Mbps),
363                 .regs = mipi_data_rate_891Mbps,
364         },
365 };
366
367 static const u32 imx335_mbus_codes[] = {
368         MEDIA_BUS_FMT_SRGGB12_1X12,
369         MEDIA_BUS_FMT_SRGGB10_1X10,
370 };
371
372 /* Supported sensor mode configurations */
373 static const struct imx335_mode supported_mode = {
374         .width = 2592,
375         .height = 1940,
376         .hblank = 342,
377         .vblank = 2560,
378         .vblank_min = 2560,
379         .vblank_max = 133060,
380         .pclk = 396000000,
381         .reg_list = {
382                 .num_of_regs = ARRAY_SIZE(mode_2592x1940_regs),
383                 .regs = mode_2592x1940_regs,
384         },
385 };
386
387 /**
388  * to_imx335() - imx335 V4L2 sub-device to imx335 device.
389  * @subdev: pointer to imx335 V4L2 sub-device
390  *
391  * Return: pointer to imx335 device
392  */
393 static inline struct imx335 *to_imx335(struct v4l2_subdev *subdev)
394 {
395         return container_of(subdev, struct imx335, sd);
396 }
397
398 /**
399  * imx335_read_reg() - Read registers.
400  * @imx335: pointer to imx335 device
401  * @reg: register address
402  * @len: length of bytes to read. Max supported bytes is 4
403  * @val: pointer to register value to be filled.
404  *
405  * Big endian register addresses with little endian values.
406  *
407  * Return: 0 if successful, error code otherwise.
408  */
409 static int imx335_read_reg(struct imx335 *imx335, u16 reg, u32 len, u32 *val)
410 {
411         struct i2c_client *client = v4l2_get_subdevdata(&imx335->sd);
412         struct i2c_msg msgs[2] = {0};
413         u8 addr_buf[2] = {0};
414         u8 data_buf[4] = {0};
415         int ret;
416
417         if (WARN_ON(len > 4))
418                 return -EINVAL;
419
420         put_unaligned_be16(reg, addr_buf);
421
422         /* Write register address */
423         msgs[0].addr = client->addr;
424         msgs[0].flags = 0;
425         msgs[0].len = ARRAY_SIZE(addr_buf);
426         msgs[0].buf = addr_buf;
427
428         /* Read data from register */
429         msgs[1].addr = client->addr;
430         msgs[1].flags = I2C_M_RD;
431         msgs[1].len = len;
432         msgs[1].buf = data_buf;
433
434         ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
435         if (ret != ARRAY_SIZE(msgs))
436                 return -EIO;
437
438         *val = get_unaligned_le32(data_buf);
439
440         return 0;
441 }
442
443 /**
444  * imx335_write_reg() - Write register
445  * @imx335: pointer to imx335 device
446  * @reg: register address
447  * @len: length of bytes. Max supported bytes is 4
448  * @val: register value
449  *
450  * Big endian register addresses with little endian values.
451  *
452  * Return: 0 if successful, error code otherwise.
453  */
454 static int imx335_write_reg(struct imx335 *imx335, u16 reg, u32 len, u32 val)
455 {
456         struct i2c_client *client = v4l2_get_subdevdata(&imx335->sd);
457         u8 buf[6] = {0};
458
459         if (WARN_ON(len > 4))
460                 return -EINVAL;
461
462         put_unaligned_be16(reg, buf);
463         put_unaligned_le32(val, buf + 2);
464         if (i2c_master_send(client, buf, len + 2) != len + 2)
465                 return -EIO;
466
467         return 0;
468 }
469
470 /**
471  * imx335_write_regs() - Write a list of registers
472  * @imx335: pointer to imx335 device
473  * @regs: list of registers to be written
474  * @len: length of registers array
475  *
476  * Return: 0 if successful. error code otherwise.
477  */
478 static int imx335_write_regs(struct imx335 *imx335,
479                              const struct imx335_reg *regs, u32 len)
480 {
481         unsigned int i;
482         int ret;
483
484         for (i = 0; i < len; i++) {
485                 ret = imx335_write_reg(imx335, regs[i].address, 1, regs[i].val);
486                 if (ret)
487                         return ret;
488         }
489
490         return 0;
491 }
492
493 /**
494  * imx335_update_controls() - Update control ranges based on streaming mode
495  * @imx335: pointer to imx335 device
496  * @mode: pointer to imx335_mode sensor mode
497  *
498  * Return: 0 if successful, error code otherwise.
499  */
500 static int imx335_update_controls(struct imx335 *imx335,
501                                   const struct imx335_mode *mode)
502 {
503         int ret;
504
505         ret = __v4l2_ctrl_s_ctrl(imx335->link_freq_ctrl,
506                                  __ffs(imx335->link_freq_bitmap));
507         if (ret)
508                 return ret;
509
510         ret = __v4l2_ctrl_s_ctrl(imx335->hblank_ctrl, mode->hblank);
511         if (ret)
512                 return ret;
513
514         return __v4l2_ctrl_modify_range(imx335->vblank_ctrl, mode->vblank_min,
515                                         mode->vblank_max, 1, mode->vblank);
516 }
517
518 /**
519  * imx335_update_exp_gain() - Set updated exposure and gain
520  * @imx335: pointer to imx335 device
521  * @exposure: updated exposure value
522  * @gain: updated analog gain value
523  *
524  * Return: 0 if successful, error code otherwise.
525  */
526 static int imx335_update_exp_gain(struct imx335 *imx335, u32 exposure, u32 gain)
527 {
528         u32 lpfr, shutter;
529         int ret;
530
531         lpfr = imx335->vblank + imx335->cur_mode->height;
532         shutter = lpfr - exposure;
533
534         dev_dbg(imx335->dev, "Set exp %u, analog gain %u, shutter %u, lpfr %u\n",
535                 exposure, gain, shutter, lpfr);
536
537         ret = imx335_write_reg(imx335, IMX335_REG_HOLD, 1, 1);
538         if (ret)
539                 return ret;
540
541         ret = imx335_write_reg(imx335, IMX335_REG_LPFR, 3, lpfr);
542         if (ret)
543                 goto error_release_group_hold;
544
545         ret = imx335_write_reg(imx335, IMX335_REG_SHUTTER, 3, shutter);
546         if (ret)
547                 goto error_release_group_hold;
548
549         ret = imx335_write_reg(imx335, IMX335_REG_AGAIN, 2, gain);
550
551 error_release_group_hold:
552         imx335_write_reg(imx335, IMX335_REG_HOLD, 1, 0);
553
554         return ret;
555 }
556
557 static int imx335_update_test_pattern(struct imx335 *imx335, u32 pattern_index)
558 {
559         int ret;
560
561         if (pattern_index >= ARRAY_SIZE(imx335_tpg_val))
562                 return -EINVAL;
563
564         if (pattern_index) {
565                 const struct imx335_reg tpg_enable_regs[] = {
566                         { 0x3148, 0x10 },
567                         { 0x3280, 0x00 },
568                         { 0x329c, 0x01 },
569                         { 0x32a0, 0x11 },
570                         { 0x3302, 0x00 },
571                         { 0x3303, 0x00 },
572                         { 0x336c, 0x00 },
573                 };
574
575                 ret = imx335_write_reg(imx335, IMX335_REG_TPG, 1,
576                                        imx335_tpg_val[pattern_index]);
577                 if (ret)
578                         return ret;
579
580                 ret = imx335_write_regs(imx335, tpg_enable_regs,
581                                         ARRAY_SIZE(tpg_enable_regs));
582         } else {
583                 const struct imx335_reg tpg_disable_regs[] = {
584                         { 0x3148, 0x00 },
585                         { 0x3280, 0x01 },
586                         { 0x329c, 0x00 },
587                         { 0x32a0, 0x10 },
588                         { 0x3302, 0x32 },
589                         { 0x3303, 0x00 },
590                         { 0x336c, 0x01 },
591                 };
592
593                 ret = imx335_write_regs(imx335, tpg_disable_regs,
594                                         ARRAY_SIZE(tpg_disable_regs));
595         }
596
597         return ret;
598 }
599
600 /**
601  * imx335_set_ctrl() - Set subdevice control
602  * @ctrl: pointer to v4l2_ctrl structure
603  *
604  * Supported controls:
605  * - V4L2_CID_VBLANK
606  * - cluster controls:
607  *   - V4L2_CID_ANALOGUE_GAIN
608  *   - V4L2_CID_EXPOSURE
609  *
610  * Return: 0 if successful, error code otherwise.
611  */
612 static int imx335_set_ctrl(struct v4l2_ctrl *ctrl)
613 {
614         struct imx335 *imx335 =
615                 container_of(ctrl->handler, struct imx335, ctrl_handler);
616         u32 analog_gain;
617         u32 exposure;
618         int ret;
619
620         /* Propagate change of current control to all related controls */
621         if (ctrl->id == V4L2_CID_VBLANK) {
622                 imx335->vblank = imx335->vblank_ctrl->val;
623
624                 dev_dbg(imx335->dev, "Received vblank %u, new lpfr %u\n",
625                         imx335->vblank,
626                         imx335->vblank + imx335->cur_mode->height);
627
628                 return __v4l2_ctrl_modify_range(imx335->exp_ctrl,
629                                                 IMX335_EXPOSURE_MIN,
630                                                 imx335->vblank +
631                                                 imx335->cur_mode->height -
632                                                 IMX335_EXPOSURE_OFFSET,
633                                                 1, IMX335_EXPOSURE_DEFAULT);
634         }
635
636         /*
637          * Applying V4L2 control value only happens
638          * when power is up for streaming.
639          */
640         if (pm_runtime_get_if_in_use(imx335->dev) == 0)
641                 return 0;
642
643         switch (ctrl->id) {
644         case V4L2_CID_EXPOSURE:
645                 exposure = ctrl->val;
646                 analog_gain = imx335->again_ctrl->val;
647
648                 dev_dbg(imx335->dev, "Received exp %u, analog gain %u\n",
649                         exposure, analog_gain);
650
651                 ret = imx335_update_exp_gain(imx335, exposure, analog_gain);
652
653                 break;
654         case V4L2_CID_TEST_PATTERN:
655                 ret = imx335_update_test_pattern(imx335, ctrl->val);
656
657                 break;
658         default:
659                 dev_err(imx335->dev, "Invalid control %d\n", ctrl->id);
660                 ret = -EINVAL;
661         }
662
663         pm_runtime_put(imx335->dev);
664
665         return ret;
666 }
667
668 /* V4l2 subdevice control ops*/
669 static const struct v4l2_ctrl_ops imx335_ctrl_ops = {
670         .s_ctrl = imx335_set_ctrl,
671 };
672
673 static int imx335_get_format_code(struct imx335 *imx335, u32 code)
674 {
675         unsigned int i;
676
677         for (i = 0; i < ARRAY_SIZE(imx335_mbus_codes); i++) {
678                 if (imx335_mbus_codes[i] == code)
679                         return imx335_mbus_codes[i];
680         }
681
682         return imx335_mbus_codes[0];
683 }
684
685 /**
686  * imx335_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes
687  * @sd: pointer to imx335 V4L2 sub-device structure
688  * @sd_state: V4L2 sub-device configuration
689  * @code: V4L2 sub-device code enumeration need to be filled
690  *
691  * Return: 0 if successful, error code otherwise.
692  */
693 static int imx335_enum_mbus_code(struct v4l2_subdev *sd,
694                                  struct v4l2_subdev_state *sd_state,
695                                  struct v4l2_subdev_mbus_code_enum *code)
696 {
697         if (code->index >= ARRAY_SIZE(imx335_mbus_codes))
698                 return -EINVAL;
699
700         code->code = imx335_mbus_codes[code->index];
701
702         return 0;
703 }
704
705 /**
706  * imx335_enum_frame_size() - Enumerate V4L2 sub-device frame sizes
707  * @sd: pointer to imx335 V4L2 sub-device structure
708  * @sd_state: V4L2 sub-device configuration
709  * @fsize: V4L2 sub-device size enumeration need to be filled
710  *
711  * Return: 0 if successful, error code otherwise.
712  */
713 static int imx335_enum_frame_size(struct v4l2_subdev *sd,
714                                   struct v4l2_subdev_state *sd_state,
715                                   struct v4l2_subdev_frame_size_enum *fsize)
716 {
717         struct imx335 *imx335 = to_imx335(sd);
718         u32 code;
719
720         if (fsize->index > ARRAY_SIZE(imx335_mbus_codes))
721                 return -EINVAL;
722
723         code = imx335_get_format_code(imx335, fsize->code);
724         if (fsize->code != code)
725                 return -EINVAL;
726
727         fsize->min_width = supported_mode.width;
728         fsize->max_width = fsize->min_width;
729         fsize->min_height = supported_mode.height;
730         fsize->max_height = fsize->min_height;
731
732         return 0;
733 }
734
735 /**
736  * imx335_fill_pad_format() - Fill subdevice pad format
737  *                            from selected sensor mode
738  * @imx335: pointer to imx335 device
739  * @mode: pointer to imx335_mode sensor mode
740  * @fmt: V4L2 sub-device format need to be filled
741  */
742 static void imx335_fill_pad_format(struct imx335 *imx335,
743                                    const struct imx335_mode *mode,
744                                    struct v4l2_subdev_format *fmt)
745 {
746         fmt->format.width = mode->width;
747         fmt->format.height = mode->height;
748         fmt->format.code = imx335->cur_mbus_code;
749         fmt->format.field = V4L2_FIELD_NONE;
750         fmt->format.colorspace = V4L2_COLORSPACE_RAW;
751         fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
752         fmt->format.quantization = V4L2_QUANTIZATION_DEFAULT;
753         fmt->format.xfer_func = V4L2_XFER_FUNC_NONE;
754 }
755
756 /**
757  * imx335_get_pad_format() - Get subdevice pad format
758  * @sd: pointer to imx335 V4L2 sub-device structure
759  * @sd_state: V4L2 sub-device configuration
760  * @fmt: V4L2 sub-device format need to be set
761  *
762  * Return: 0 if successful, error code otherwise.
763  */
764 static int imx335_get_pad_format(struct v4l2_subdev *sd,
765                                  struct v4l2_subdev_state *sd_state,
766                                  struct v4l2_subdev_format *fmt)
767 {
768         struct imx335 *imx335 = to_imx335(sd);
769
770         mutex_lock(&imx335->mutex);
771
772         if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
773                 struct v4l2_mbus_framefmt *framefmt;
774
775                 framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad);
776                 fmt->format = *framefmt;
777         } else {
778                 imx335_fill_pad_format(imx335, imx335->cur_mode, fmt);
779         }
780
781         mutex_unlock(&imx335->mutex);
782
783         return 0;
784 }
785
786 /**
787  * imx335_set_pad_format() - Set subdevice pad format
788  * @sd: pointer to imx335 V4L2 sub-device structure
789  * @sd_state: V4L2 sub-device configuration
790  * @fmt: V4L2 sub-device format need to be set
791  *
792  * Return: 0 if successful, error code otherwise.
793  */
794 static int imx335_set_pad_format(struct v4l2_subdev *sd,
795                                  struct v4l2_subdev_state *sd_state,
796                                  struct v4l2_subdev_format *fmt)
797 {
798         struct imx335 *imx335 = to_imx335(sd);
799         const struct imx335_mode *mode;
800         int i, ret = 0;
801
802         mutex_lock(&imx335->mutex);
803
804         mode = &supported_mode;
805         for (i = 0; i < ARRAY_SIZE(imx335_mbus_codes); i++) {
806                 if (imx335_mbus_codes[i] == fmt->format.code)
807                         imx335->cur_mbus_code = imx335_mbus_codes[i];
808         }
809
810         imx335_fill_pad_format(imx335, mode, fmt);
811
812         if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
813                 struct v4l2_mbus_framefmt *framefmt;
814
815                 framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad);
816                 *framefmt = fmt->format;
817         } else {
818                 ret = imx335_update_controls(imx335, mode);
819                 if (!ret)
820                         imx335->cur_mode = mode;
821         }
822
823         mutex_unlock(&imx335->mutex);
824
825         return ret;
826 }
827
828 /**
829  * imx335_init_state() - Initialize sub-device state
830  * @sd: pointer to imx335 V4L2 sub-device structure
831  * @sd_state: V4L2 sub-device configuration
832  *
833  * Return: 0 if successful, error code otherwise.
834  */
835 static int imx335_init_state(struct v4l2_subdev *sd,
836                              struct v4l2_subdev_state *sd_state)
837 {
838         struct imx335 *imx335 = to_imx335(sd);
839         struct v4l2_subdev_format fmt = { 0 };
840
841         fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
842         imx335_fill_pad_format(imx335, &supported_mode, &fmt);
843
844         mutex_lock(&imx335->mutex);
845         __v4l2_ctrl_modify_range(imx335->link_freq_ctrl, 0,
846                                  __fls(imx335->link_freq_bitmap),
847                                  ~(imx335->link_freq_bitmap),
848                                  __ffs(imx335->link_freq_bitmap));
849         mutex_unlock(&imx335->mutex);
850
851         return imx335_set_pad_format(sd, sd_state, &fmt);
852 }
853
854 /**
855  * imx335_get_selection() - Selection API
856  * @sd: pointer to imx335 V4L2 sub-device structure
857  * @sd_state: V4L2 sub-device configuration
858  * @sel: V4L2 selection info
859  *
860  * Return: 0 if successful, error code otherwise.
861  */
862 static int imx335_get_selection(struct v4l2_subdev *sd,
863                                 struct v4l2_subdev_state *sd_state,
864                                 struct v4l2_subdev_selection *sel)
865 {
866         switch (sel->target) {
867         case V4L2_SEL_TGT_NATIVE_SIZE:
868                 sel->r.top = 0;
869                 sel->r.left = 0;
870                 sel->r.width = IMX335_NATIVE_WIDTH;
871                 sel->r.height = IMX335_NATIVE_HEIGHT;
872
873                 return 0;
874
875         case V4L2_SEL_TGT_CROP:
876         case V4L2_SEL_TGT_CROP_DEFAULT:
877         case V4L2_SEL_TGT_CROP_BOUNDS:
878                 sel->r.top = IMX335_PIXEL_ARRAY_TOP;
879                 sel->r.left = IMX335_PIXEL_ARRAY_LEFT;
880                 sel->r.width = IMX335_PIXEL_ARRAY_WIDTH;
881                 sel->r.height = IMX335_PIXEL_ARRAY_HEIGHT;
882
883                 return 0;
884         }
885
886         return -EINVAL;
887 }
888
889 static int imx335_set_framefmt(struct imx335 *imx335)
890 {
891         switch (imx335->cur_mbus_code) {
892         case MEDIA_BUS_FMT_SRGGB10_1X10:
893                 return imx335_write_regs(imx335, raw10_framefmt_regs,
894                                          ARRAY_SIZE(raw10_framefmt_regs));
895
896         case MEDIA_BUS_FMT_SRGGB12_1X12:
897                 return imx335_write_regs(imx335, raw12_framefmt_regs,
898                                          ARRAY_SIZE(raw12_framefmt_regs));
899         }
900
901         return -EINVAL;
902 }
903
904 /**
905  * imx335_start_streaming() - Start sensor stream
906  * @imx335: pointer to imx335 device
907  *
908  * Return: 0 if successful, error code otherwise.
909  */
910 static int imx335_start_streaming(struct imx335 *imx335)
911 {
912         const struct imx335_reg_list *reg_list;
913         int ret;
914
915         /* Setup PLL */
916         reg_list = &link_freq_reglist[__ffs(imx335->link_freq_bitmap)];
917         ret = imx335_write_regs(imx335, reg_list->regs, reg_list->num_of_regs);
918         if (ret) {
919                 dev_err(imx335->dev, "%s failed to set plls\n", __func__);
920                 return ret;
921         }
922
923         /* Write sensor mode registers */
924         reg_list = &imx335->cur_mode->reg_list;
925         ret = imx335_write_regs(imx335, reg_list->regs,
926                                 reg_list->num_of_regs);
927         if (ret) {
928                 dev_err(imx335->dev, "fail to write initial registers\n");
929                 return ret;
930         }
931
932         ret = imx335_set_framefmt(imx335);
933         if (ret) {
934                 dev_err(imx335->dev, "%s failed to set frame format: %d\n",
935                         __func__, ret);
936                 return ret;
937         }
938
939         /* Setup handler will write actual exposure and gain */
940         ret =  __v4l2_ctrl_handler_setup(imx335->sd.ctrl_handler);
941         if (ret) {
942                 dev_err(imx335->dev, "fail to setup handler\n");
943                 return ret;
944         }
945
946         /* Start streaming */
947         ret = imx335_write_reg(imx335, IMX335_REG_MODE_SELECT,
948                                1, IMX335_MODE_STREAMING);
949         if (ret) {
950                 dev_err(imx335->dev, "fail to start streaming\n");
951                 return ret;
952         }
953
954         /* Initial regulator stabilization period */
955         usleep_range(18000, 20000);
956
957         return 0;
958 }
959
960 /**
961  * imx335_stop_streaming() - Stop sensor stream
962  * @imx335: pointer to imx335 device
963  *
964  * Return: 0 if successful, error code otherwise.
965  */
966 static int imx335_stop_streaming(struct imx335 *imx335)
967 {
968         return imx335_write_reg(imx335, IMX335_REG_MODE_SELECT,
969                                 1, IMX335_MODE_STANDBY);
970 }
971
972 /**
973  * imx335_set_stream() - Enable sensor streaming
974  * @sd: pointer to imx335 subdevice
975  * @enable: set to enable sensor streaming
976  *
977  * Return: 0 if successful, error code otherwise.
978  */
979 static int imx335_set_stream(struct v4l2_subdev *sd, int enable)
980 {
981         struct imx335 *imx335 = to_imx335(sd);
982         int ret;
983
984         mutex_lock(&imx335->mutex);
985
986         if (enable) {
987                 ret = pm_runtime_resume_and_get(imx335->dev);
988                 if (ret)
989                         goto error_unlock;
990
991                 ret = imx335_start_streaming(imx335);
992                 if (ret)
993                         goto error_power_off;
994         } else {
995                 imx335_stop_streaming(imx335);
996                 pm_runtime_put(imx335->dev);
997         }
998
999         mutex_unlock(&imx335->mutex);
1000
1001         return 0;
1002
1003 error_power_off:
1004         pm_runtime_put(imx335->dev);
1005 error_unlock:
1006         mutex_unlock(&imx335->mutex);
1007
1008         return ret;
1009 }
1010
1011 /**
1012  * imx335_detect() - Detect imx335 sensor
1013  * @imx335: pointer to imx335 device
1014  *
1015  * Return: 0 if successful, -EIO if sensor id does not match
1016  */
1017 static int imx335_detect(struct imx335 *imx335)
1018 {
1019         int ret;
1020         u32 val;
1021
1022         ret = imx335_read_reg(imx335, IMX335_REG_ID, 2, &val);
1023         if (ret)
1024                 return ret;
1025
1026         if (val != IMX335_ID) {
1027                 dev_err(imx335->dev, "chip id mismatch: %x!=%x\n",
1028                         IMX335_ID, val);
1029                 return -ENXIO;
1030         }
1031
1032         return 0;
1033 }
1034
1035 /**
1036  * imx335_parse_hw_config() - Parse HW configuration and check if supported
1037  * @imx335: pointer to imx335 device
1038  *
1039  * Return: 0 if successful, error code otherwise.
1040  */
1041 static int imx335_parse_hw_config(struct imx335 *imx335)
1042 {
1043         struct fwnode_handle *fwnode = dev_fwnode(imx335->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         unsigned int i;
1050         int ret;
1051
1052         if (!fwnode)
1053                 return -ENXIO;
1054
1055         /* Request optional reset pin */
1056         imx335->reset_gpio = devm_gpiod_get_optional(imx335->dev, "reset",
1057                                                      GPIOD_OUT_LOW);
1058         if (IS_ERR(imx335->reset_gpio)) {
1059                 dev_err(imx335->dev, "failed to get reset gpio %ld\n",
1060                         PTR_ERR(imx335->reset_gpio));
1061                 return PTR_ERR(imx335->reset_gpio);
1062         }
1063
1064         for (i = 0; i < ARRAY_SIZE(imx335_supply_name); i++)
1065                 imx335->supplies[i].supply = imx335_supply_name[i];
1066
1067         ret = devm_regulator_bulk_get(imx335->dev,
1068                                       ARRAY_SIZE(imx335_supply_name),
1069                                       imx335->supplies);
1070         if (ret) {
1071                 dev_err(imx335->dev, "Failed to get regulators\n");
1072                 return ret;
1073         }
1074
1075         /* Get sensor input clock */
1076         imx335->inclk = devm_clk_get(imx335->dev, NULL);
1077         if (IS_ERR(imx335->inclk)) {
1078                 dev_err(imx335->dev, "could not get inclk\n");
1079                 return PTR_ERR(imx335->inclk);
1080         }
1081
1082         rate = clk_get_rate(imx335->inclk);
1083         if (rate != IMX335_INCLK_RATE) {
1084                 dev_err(imx335->dev, "inclk frequency mismatch\n");
1085                 return -EINVAL;
1086         }
1087
1088         ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
1089         if (!ep) {
1090                 dev_err(imx335->dev, "Failed to get next endpoint\n");
1091                 return -ENXIO;
1092         }
1093
1094         ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
1095         fwnode_handle_put(ep);
1096         if (ret)
1097                 return ret;
1098
1099         if (bus_cfg.bus.mipi_csi2.num_data_lanes != IMX335_NUM_DATA_LANES) {
1100                 dev_err(imx335->dev,
1101                         "number of CSI2 data lanes %d is not supported\n",
1102                         bus_cfg.bus.mipi_csi2.num_data_lanes);
1103                 ret = -EINVAL;
1104                 goto done_endpoint_free;
1105         }
1106
1107         ret = v4l2_link_freq_to_bitmap(imx335->dev, bus_cfg.link_frequencies,
1108                                        bus_cfg.nr_of_link_frequencies,
1109                                        link_freq, ARRAY_SIZE(link_freq),
1110                                        &imx335->link_freq_bitmap);
1111
1112 done_endpoint_free:
1113         v4l2_fwnode_endpoint_free(&bus_cfg);
1114
1115         return ret;
1116 }
1117
1118 /* V4l2 subdevice ops */
1119 static const struct v4l2_subdev_video_ops imx335_video_ops = {
1120         .s_stream = imx335_set_stream,
1121 };
1122
1123 static const struct v4l2_subdev_pad_ops imx335_pad_ops = {
1124         .enum_mbus_code = imx335_enum_mbus_code,
1125         .enum_frame_size = imx335_enum_frame_size,
1126         .get_selection = imx335_get_selection,
1127         .set_selection = imx335_get_selection,
1128         .get_fmt = imx335_get_pad_format,
1129         .set_fmt = imx335_set_pad_format,
1130 };
1131
1132 static const struct v4l2_subdev_ops imx335_subdev_ops = {
1133         .video = &imx335_video_ops,
1134         .pad = &imx335_pad_ops,
1135 };
1136
1137 static const struct v4l2_subdev_internal_ops imx335_internal_ops = {
1138         .init_state = imx335_init_state,
1139 };
1140
1141 /**
1142  * imx335_power_on() - Sensor power on sequence
1143  * @dev: pointer to i2c device
1144  *
1145  * Return: 0 if successful, error code otherwise.
1146  */
1147 static int imx335_power_on(struct device *dev)
1148 {
1149         struct v4l2_subdev *sd = dev_get_drvdata(dev);
1150         struct imx335 *imx335 = to_imx335(sd);
1151         int ret;
1152
1153         ret = regulator_bulk_enable(ARRAY_SIZE(imx335_supply_name),
1154                                     imx335->supplies);
1155         if (ret) {
1156                 dev_err(dev, "%s: failed to enable regulators\n",
1157                         __func__);
1158                 return ret;
1159         }
1160
1161         usleep_range(500, 550); /* Tlow */
1162
1163         /* Set XCLR */
1164         gpiod_set_value_cansleep(imx335->reset_gpio, 1);
1165
1166         ret = clk_prepare_enable(imx335->inclk);
1167         if (ret) {
1168                 dev_err(imx335->dev, "fail to enable inclk\n");
1169                 goto error_reset;
1170         }
1171
1172         usleep_range(20, 22); /* T4 */
1173
1174         return 0;
1175
1176 error_reset:
1177         gpiod_set_value_cansleep(imx335->reset_gpio, 0);
1178         regulator_bulk_disable(ARRAY_SIZE(imx335_supply_name), imx335->supplies);
1179
1180         return ret;
1181 }
1182
1183 /**
1184  * imx335_power_off() - Sensor power off sequence
1185  * @dev: pointer to i2c device
1186  *
1187  * Return: 0 if successful, error code otherwise.
1188  */
1189 static int imx335_power_off(struct device *dev)
1190 {
1191         struct v4l2_subdev *sd = dev_get_drvdata(dev);
1192         struct imx335 *imx335 = to_imx335(sd);
1193
1194         gpiod_set_value_cansleep(imx335->reset_gpio, 0);
1195         clk_disable_unprepare(imx335->inclk);
1196         regulator_bulk_disable(ARRAY_SIZE(imx335_supply_name), imx335->supplies);
1197
1198         return 0;
1199 }
1200
1201 /**
1202  * imx335_init_controls() - Initialize sensor subdevice controls
1203  * @imx335: pointer to imx335 device
1204  *
1205  * Return: 0 if successful, error code otherwise.
1206  */
1207 static int imx335_init_controls(struct imx335 *imx335)
1208 {
1209         struct v4l2_ctrl_handler *ctrl_hdlr = &imx335->ctrl_handler;
1210         const struct imx335_mode *mode = imx335->cur_mode;
1211         u32 lpfr;
1212         int ret;
1213
1214         ret = v4l2_ctrl_handler_init(ctrl_hdlr, 7);
1215         if (ret)
1216                 return ret;
1217
1218         /* Serialize controls with sensor device */
1219         ctrl_hdlr->lock = &imx335->mutex;
1220
1221         /* Initialize exposure and gain */
1222         lpfr = mode->vblank + mode->height;
1223         imx335->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1224                                              &imx335_ctrl_ops,
1225                                              V4L2_CID_EXPOSURE,
1226                                              IMX335_EXPOSURE_MIN,
1227                                              lpfr - IMX335_EXPOSURE_OFFSET,
1228                                              IMX335_EXPOSURE_STEP,
1229                                              IMX335_EXPOSURE_DEFAULT);
1230
1231         imx335->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1232                                                &imx335_ctrl_ops,
1233                                                V4L2_CID_ANALOGUE_GAIN,
1234                                                IMX335_AGAIN_MIN,
1235                                                IMX335_AGAIN_MAX,
1236                                                IMX335_AGAIN_STEP,
1237                                                IMX335_AGAIN_DEFAULT);
1238
1239         v4l2_ctrl_cluster(2, &imx335->exp_ctrl);
1240
1241         imx335->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1242                                                 &imx335_ctrl_ops,
1243                                                 V4L2_CID_VBLANK,
1244                                                 mode->vblank_min,
1245                                                 mode->vblank_max,
1246                                                 1, mode->vblank);
1247
1248         v4l2_ctrl_new_std_menu_items(ctrl_hdlr,
1249                                      &imx335_ctrl_ops,
1250                                      V4L2_CID_TEST_PATTERN,
1251                                      ARRAY_SIZE(imx335_tpg_menu) - 1,
1252                                      0, 0, imx335_tpg_menu);
1253
1254         /* Read only controls */
1255         imx335->pclk_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1256                                               &imx335_ctrl_ops,
1257                                               V4L2_CID_PIXEL_RATE,
1258                                               mode->pclk, mode->pclk,
1259                                               1, mode->pclk);
1260
1261         imx335->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr,
1262                                                         &imx335_ctrl_ops,
1263                                                         V4L2_CID_LINK_FREQ,
1264                                                         __fls(imx335->link_freq_bitmap),
1265                                                         __ffs(imx335->link_freq_bitmap),
1266                                                         link_freq);
1267         if (imx335->link_freq_ctrl)
1268                 imx335->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1269
1270         imx335->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1271                                                 &imx335_ctrl_ops,
1272                                                 V4L2_CID_HBLANK,
1273                                                 mode->hblank,
1274                                                 mode->hblank,
1275                                                 1, mode->hblank);
1276         if (imx335->hblank_ctrl)
1277                 imx335->hblank_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1278
1279         if (ctrl_hdlr->error) {
1280                 dev_err(imx335->dev, "control init failed: %d\n",
1281                         ctrl_hdlr->error);
1282                 v4l2_ctrl_handler_free(ctrl_hdlr);
1283                 return ctrl_hdlr->error;
1284         }
1285
1286         imx335->sd.ctrl_handler = ctrl_hdlr;
1287
1288         return 0;
1289 }
1290
1291 /**
1292  * imx335_probe() - I2C client device binding
1293  * @client: pointer to i2c client device
1294  *
1295  * Return: 0 if successful, error code otherwise.
1296  */
1297 static int imx335_probe(struct i2c_client *client)
1298 {
1299         struct imx335 *imx335;
1300         int ret;
1301
1302         imx335 = devm_kzalloc(&client->dev, sizeof(*imx335), GFP_KERNEL);
1303         if (!imx335)
1304                 return -ENOMEM;
1305
1306         imx335->dev = &client->dev;
1307
1308         /* Initialize subdev */
1309         v4l2_i2c_subdev_init(&imx335->sd, client, &imx335_subdev_ops);
1310         imx335->sd.internal_ops = &imx335_internal_ops;
1311
1312         ret = imx335_parse_hw_config(imx335);
1313         if (ret) {
1314                 dev_err(imx335->dev, "HW configuration is not supported\n");
1315                 return ret;
1316         }
1317
1318         mutex_init(&imx335->mutex);
1319
1320         ret = imx335_power_on(imx335->dev);
1321         if (ret) {
1322                 dev_err(imx335->dev, "failed to power-on the sensor\n");
1323                 goto error_mutex_destroy;
1324         }
1325
1326         /* Check module identity */
1327         ret = imx335_detect(imx335);
1328         if (ret) {
1329                 dev_err(imx335->dev, "failed to find sensor: %d\n", ret);
1330                 goto error_power_off;
1331         }
1332
1333         /* Set default mode to max resolution */
1334         imx335->cur_mode = &supported_mode;
1335         imx335->cur_mbus_code = imx335_mbus_codes[0];
1336         imx335->vblank = imx335->cur_mode->vblank;
1337
1338         ret = imx335_init_controls(imx335);
1339         if (ret) {
1340                 dev_err(imx335->dev, "failed to init controls: %d\n", ret);
1341                 goto error_power_off;
1342         }
1343
1344         /* Initialize subdev */
1345         imx335->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1346         imx335->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1347
1348         /* Initialize source pad */
1349         imx335->pad.flags = MEDIA_PAD_FL_SOURCE;
1350         ret = media_entity_pads_init(&imx335->sd.entity, 1, &imx335->pad);
1351         if (ret) {
1352                 dev_err(imx335->dev, "failed to init entity pads: %d\n", ret);
1353                 goto error_handler_free;
1354         }
1355
1356         ret = v4l2_async_register_subdev_sensor(&imx335->sd);
1357         if (ret < 0) {
1358                 dev_err(imx335->dev,
1359                         "failed to register async subdev: %d\n", ret);
1360                 goto error_media_entity;
1361         }
1362
1363         pm_runtime_set_active(imx335->dev);
1364         pm_runtime_enable(imx335->dev);
1365         pm_runtime_idle(imx335->dev);
1366
1367         return 0;
1368
1369 error_media_entity:
1370         media_entity_cleanup(&imx335->sd.entity);
1371 error_handler_free:
1372         v4l2_ctrl_handler_free(imx335->sd.ctrl_handler);
1373 error_power_off:
1374         imx335_power_off(imx335->dev);
1375 error_mutex_destroy:
1376         mutex_destroy(&imx335->mutex);
1377
1378         return ret;
1379 }
1380
1381 /**
1382  * imx335_remove() - I2C client device unbinding
1383  * @client: pointer to I2C client device
1384  *
1385  * Return: 0 if successful, error code otherwise.
1386  */
1387 static void imx335_remove(struct i2c_client *client)
1388 {
1389         struct v4l2_subdev *sd = i2c_get_clientdata(client);
1390         struct imx335 *imx335 = to_imx335(sd);
1391
1392         v4l2_async_unregister_subdev(sd);
1393         media_entity_cleanup(&sd->entity);
1394         v4l2_ctrl_handler_free(sd->ctrl_handler);
1395
1396         pm_runtime_disable(&client->dev);
1397         if (!pm_runtime_status_suspended(&client->dev))
1398                 imx335_power_off(&client->dev);
1399         pm_runtime_set_suspended(&client->dev);
1400
1401         mutex_destroy(&imx335->mutex);
1402 }
1403
1404 static const struct dev_pm_ops imx335_pm_ops = {
1405         SET_RUNTIME_PM_OPS(imx335_power_off, imx335_power_on, NULL)
1406 };
1407
1408 static const struct of_device_id imx335_of_match[] = {
1409         { .compatible = "sony,imx335" },
1410         { }
1411 };
1412
1413 MODULE_DEVICE_TABLE(of, imx335_of_match);
1414
1415 static struct i2c_driver imx335_driver = {
1416         .probe = imx335_probe,
1417         .remove = imx335_remove,
1418         .driver = {
1419                 .name = "imx335",
1420                 .pm = &imx335_pm_ops,
1421                 .of_match_table = imx335_of_match,
1422         },
1423 };
1424
1425 module_i2c_driver(imx335_driver);
1426
1427 MODULE_DESCRIPTION("Sony imx335 sensor driver");
1428 MODULE_LICENSE("GPL");
This page took 0.110744 seconds and 4 git commands to generate.