media: rp1: fe: Fix pisp_fe_pad_set_fmt()
[platform/kernel/linux-rpi.git] / drivers / media / i2c / ov9282.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * OmniVision ov9282 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 #include <linux/regulator/consumer.h>
15
16 #include <media/v4l2-ctrls.h>
17 #include <media/v4l2-event.h>
18 #include <media/v4l2-fwnode.h>
19 #include <media/v4l2-subdev.h>
20
21 /* Streaming Mode */
22 #define OV9282_REG_MODE_SELECT  0x0100
23 #define OV9282_MODE_STANDBY     0x00
24 #define OV9282_MODE_STREAMING   0x01
25
26 #define OV9282_REG_PLL_CTRL_0D  0x030d
27 #define OV9282_PLL_CTRL_0D_RAW8         0x60
28 #define OV9282_PLL_CTRL_0D_RAW10        0x50
29
30 #define OV9282_REG_TIMING_HTS   0x380c
31 #define OV9282_TIMING_HTS_MAX   0x7fff
32
33 /* Lines per frame */
34 #define OV9282_REG_LPFR         0x380e
35
36 /* Chip ID */
37 #define OV9282_REG_ID           0x300a
38 #define OV9282_ID               0x9281
39
40 /* Exposure control */
41 #define OV9282_REG_EXPOSURE     0x3500
42 #define OV9282_EXPOSURE_MIN     1
43 #define OV9282_EXPOSURE_OFFSET  12
44 #define OV9282_EXPOSURE_STEP    1
45 #define OV9282_EXPOSURE_DEFAULT 0x0282
46
47 /* Analog gain control */
48 #define OV9282_REG_AGAIN        0x3509
49 #define OV9282_AGAIN_MIN        0x10
50 #define OV9282_AGAIN_MAX        0xff
51 #define OV9282_AGAIN_STEP       1
52 #define OV9282_AGAIN_DEFAULT    0x10
53
54 /* Group hold register */
55 #define OV9282_REG_HOLD         0x3308
56
57 #define OV9282_REG_ANA_CORE_2   0x3662
58 #define OV9282_ANA_CORE2_RAW8   0x07
59 #define OV9282_ANA_CORE2_RAW10  0x05
60
61 #define OV9282_REG_TIMING_FORMAT_1      0x3820
62 #define OV9282_REG_TIMING_FORMAT_2      0x3821
63 #define OV9282_FLIP_BIT                 BIT(2)
64
65 #define OV9282_REG_MIPI_CTRL00  0x4800
66 #define OV9282_GATED_CLOCK      BIT(5)
67
68 /* Input clock rate */
69 #define OV9282_INCLK_RATE       24000000
70
71 /* CSI2 HW configuration */
72 #define OV9282_LINK_FREQ        400000000
73 #define OV9282_NUM_DATA_LANES   2
74
75 /* Pixel rate */
76 #define OV9282_PIXEL_RATE_10BIT         (OV9282_LINK_FREQ * 2 * \
77                                          OV9282_NUM_DATA_LANES / 10)
78 #define OV9282_PIXEL_RATE_8BIT          (OV9282_LINK_FREQ * 2 * \
79                                          OV9282_NUM_DATA_LANES / 8)
80
81 /*
82  * OV9282 native and active pixel array size.
83  * 8 dummy rows/columns on each edge of a 1280x800 active array
84  */
85 #define OV9282_NATIVE_WIDTH             1296U
86 #define OV9282_NATIVE_HEIGHT            816U
87 #define OV9282_PIXEL_ARRAY_LEFT         8U
88 #define OV9282_PIXEL_ARRAY_TOP          8U
89 #define OV9282_PIXEL_ARRAY_WIDTH        1280U
90 #define OV9282_PIXEL_ARRAY_HEIGHT       800U
91
92 #define OV9282_REG_MIN          0x00
93 #define OV9282_REG_MAX          0xfffff
94
95 static const char * const ov9282_supply_names[] = {
96         "avdd",         /* Analog power */
97         "dovdd",        /* Digital I/O power */
98         "dvdd",         /* Digital core power */
99 };
100
101 #define OV9282_NUM_SUPPLIES ARRAY_SIZE(ov9282_supply_names)
102
103 /**
104  * struct ov9282_reg - ov9282 sensor register
105  * @address: Register address
106  * @val: Register value
107  */
108 struct ov9282_reg {
109         u16 address;
110         u8 val;
111 };
112
113 /**
114  * struct ov9282_reg_list - ov9282 sensor register list
115  * @num_of_regs: Number of registers in the list
116  * @regs: Pointer to register list
117  */
118 struct ov9282_reg_list {
119         u32 num_of_regs;
120         const struct ov9282_reg *regs;
121 };
122
123 /**
124  * struct ov9282_mode - ov9282 sensor mode structure
125  * @width: Frame width
126  * @height: Frame height
127  * @hblank_min: Minimum horizontal blanking in lines for non-continuous[0] and
128  *              continuous[1] clock modes
129  * @vblank: Vertical blanking in lines
130  * @vblank_min: Minimum vertical blanking in lines
131  * @vblank_max: Maximum vertical blanking in lines
132  * @link_freq_idx: Link frequency index
133  * @crop: on-sensor cropping for this mode
134  * @reg_list: Register list for sensor mode
135  */
136 struct ov9282_mode {
137         u32 width;
138         u32 height;
139         u32 hblank_min[2];
140         u32 vblank;
141         u32 vblank_min;
142         u32 vblank_max;
143         u32 link_freq_idx;
144         struct v4l2_rect crop;
145         struct ov9282_reg_list reg_list;
146 };
147
148 /**
149  * struct ov9282 - ov9282 sensor device structure
150  * @dev: Pointer to generic device
151  * @sd: V4L2 sub-device
152  * @pad: Media pad. Only one pad supported
153  * @reset_gpio: Sensor reset gpio
154  * @inclk: Sensor input clock
155  * @supplies: Regulator supplies for the sensor
156  * @ctrl_handler: V4L2 control handler
157  * @link_freq_ctrl: Pointer to link frequency control
158  * @hblank_ctrl: Pointer to horizontal blanking control
159  * @vblank_ctrl: Pointer to vertical blanking control
160  * @exp_ctrl: Pointer to exposure control
161  * @again_ctrl: Pointer to analog gain control
162  * @pixel_rate: Pointer to pixel rate control
163  * @vblank: Vertical blanking in lines
164  * @noncontinuous_clock: Selection of CSI2 noncontinuous clock mode
165  * @cur_mode: Pointer to current selected sensor mode
166  * @code: Mbus code currently selected
167  * @mutex: Mutex for serializing sensor controls
168  * @streaming: Flag indicating streaming state
169  */
170 struct ov9282 {
171         struct device *dev;
172         struct v4l2_subdev sd;
173         struct media_pad pad;
174         struct gpio_desc *reset_gpio;
175         struct clk *inclk;
176         struct regulator_bulk_data supplies[OV9282_NUM_SUPPLIES];
177         struct v4l2_ctrl_handler ctrl_handler;
178         struct v4l2_ctrl *link_freq_ctrl;
179         struct v4l2_ctrl *hblank_ctrl;
180         struct v4l2_ctrl *vblank_ctrl;
181         struct {
182                 struct v4l2_ctrl *exp_ctrl;
183                 struct v4l2_ctrl *again_ctrl;
184         };
185         struct v4l2_ctrl *pixel_rate;
186         u32 vblank;
187         bool noncontinuous_clock;
188         const struct ov9282_mode *cur_mode;
189         u32 code;
190         struct mutex mutex;
191         bool streaming;
192 };
193
194 static const s64 link_freq[] = {
195         OV9282_LINK_FREQ,
196 };
197
198 /*
199  * Common registers
200  *
201  * Note: Do NOT include a software reset (0x0103, 0x01) in any of these
202  * register arrays as some settings are written as part of ov9282_power_on,
203  * and the reset will clear them.
204  */
205 static const struct ov9282_reg common_regs[] = {
206         {0x0302, 0x32},
207         {0x030e, 0x02},
208         {0x3001, 0x00},
209         {0x3004, 0x00},
210         {0x3005, 0x00},
211         {0x3006, 0x04},
212         {0x3011, 0x0a},
213         {0x3013, 0x18},
214         {0x301c, 0xf0},
215         {0x3022, 0x01},
216         {0x3030, 0x10},
217         {0x3039, 0x32},
218         {0x303a, 0x00},
219         {0x3503, 0x08},
220         {0x3505, 0x8c},
221         {0x3507, 0x03},
222         {0x3508, 0x00},
223         {0x3610, 0x80},
224         {0x3611, 0xa0},
225         {0x3620, 0x6e},
226         {0x3632, 0x56},
227         {0x3633, 0x78},
228         {0x3666, 0x00},
229         {0x366f, 0x5a},
230         {0x3680, 0x84},
231         {0x3712, 0x80},
232         {0x372d, 0x22},
233         {0x3731, 0x80},
234         {0x3732, 0x30},
235         {0x377d, 0x22},
236         {0x3788, 0x02},
237         {0x3789, 0xa4},
238         {0x378a, 0x00},
239         {0x378b, 0x4a},
240         {0x3799, 0x20},
241         {0x3881, 0x42},
242         {0x38a8, 0x02},
243         {0x38a9, 0x80},
244         {0x38b1, 0x00},
245         {0x38c4, 0x00},
246         {0x38c5, 0xc0},
247         {0x38c6, 0x04},
248         {0x38c7, 0x80},
249         {0x3920, 0xff},
250         {0x4010, 0x40},
251         {0x4043, 0x40},
252         {0x4307, 0x30},
253         {0x4317, 0x00},
254         {0x4501, 0x00},
255         {0x450a, 0x08},
256         {0x4601, 0x04},
257         {0x470f, 0x00},
258         {0x4f07, 0x00},
259         {0x5000, 0x9f},
260         {0x5001, 0x00},
261         {0x5e00, 0x00},
262         {0x5d00, 0x07},
263         {0x5d01, 0x00},
264         {0x0101, 0x01},
265         {0x1000, 0x03},
266         {0x5a08, 0x84},
267 };
268
269 static struct ov9282_reg_list common_regs_list = {
270         .num_of_regs = ARRAY_SIZE(common_regs),
271         .regs = common_regs,
272 };
273
274 #define MODE_1280_800           0
275 #define MODE_1280_720           1
276 #define MODE_640_400            2
277
278 #define DEFAULT_MODE            MODE_1280_720
279
280 /* Sensor mode registers */
281 static const struct ov9282_reg mode_1280x800_regs[] = {
282         {0x3778, 0x00},
283         {0x3800, 0x00},
284         {0x3801, 0x00},
285         {0x3802, 0x00},
286         {0x3803, 0x00},
287         {0x3804, 0x05},
288         {0x3805, 0x0f},
289         {0x3806, 0x03},
290         {0x3807, 0x2f},
291         {0x3808, 0x05},
292         {0x3809, 0x00},
293         {0x380a, 0x03},
294         {0x380b, 0x20},
295         {0x3810, 0x00},
296         {0x3811, 0x08},
297         {0x3812, 0x00},
298         {0x3813, 0x08},
299         {0x3814, 0x11},
300         {0x3815, 0x11},
301         {0x3820, 0x40},
302         {0x3821, 0x00},
303         {0x4003, 0x40},
304         {0x4008, 0x04},
305         {0x4009, 0x0b},
306         {0x400c, 0x00},
307         {0x400d, 0x07},
308         {0x4507, 0x00},
309         {0x4509, 0x00},
310 };
311
312 static const struct ov9282_reg mode_1280x720_regs[] = {
313         {0x3778, 0x00},
314         {0x3800, 0x00},
315         {0x3801, 0x00},
316         {0x3802, 0x00},
317         {0x3803, 0x00},
318         {0x3804, 0x05},
319         {0x3805, 0x0f},
320         {0x3806, 0x02},
321         {0x3807, 0xdf},
322         {0x3808, 0x05},
323         {0x3809, 0x00},
324         {0x380a, 0x02},
325         {0x380b, 0xd0},
326         {0x3810, 0x00},
327         {0x3811, 0x08},
328         {0x3812, 0x00},
329         {0x3813, 0x08},
330         {0x3814, 0x11},
331         {0x3815, 0x11},
332         {0x3820, 0x3c},
333         {0x3821, 0x84},
334         {0x4003, 0x40},
335         {0x4008, 0x02},
336         {0x4009, 0x05},
337         {0x400c, 0x00},
338         {0x400d, 0x03},
339         {0x4507, 0x00},
340         {0x4509, 0x80},
341 };
342
343 static const struct ov9282_reg mode_640x400_regs[] = {
344         {0x3778, 0x10},
345         {0x3800, 0x00},
346         {0x3801, 0x00},
347         {0x3802, 0x00},
348         {0x3803, 0x00},
349         {0x3804, 0x05},
350         {0x3805, 0x0f},
351         {0x3806, 0x03},
352         {0x3807, 0x2f},
353         {0x3808, 0x02},
354         {0x3809, 0x80},
355         {0x380a, 0x01},
356         {0x380b, 0x90},
357         {0x3810, 0x00},
358         {0x3811, 0x04},
359         {0x3812, 0x00},
360         {0x3813, 0x04},
361         {0x3814, 0x31},
362         {0x3815, 0x22},
363         {0x3820, 0x60},
364         {0x3821, 0x01},
365         {0x4008, 0x02},
366         {0x4009, 0x05},
367         {0x400c, 0x00},
368         {0x400d, 0x03},
369         {0x4507, 0x03},
370         {0x4509, 0x80},
371 };
372
373 /* Supported sensor mode configurations */
374 static const struct ov9282_mode supported_modes[] = {
375         [MODE_1280_800] = {
376                 .width = 1280,
377                 .height = 800,
378                 .hblank_min = { 250, 176 },
379                 .vblank = 1022,
380                 .vblank_min = 110,
381                 .vblank_max = 51540,
382                 .link_freq_idx = 0,
383                 .crop = {
384                         .left = OV9282_PIXEL_ARRAY_LEFT,
385                         .top = OV9282_PIXEL_ARRAY_TOP,
386                         .width = 1280,
387                         .height = 800
388                 },
389                 .reg_list = {
390                         .num_of_regs = ARRAY_SIZE(mode_1280x800_regs),
391                         .regs = mode_1280x800_regs,
392                 },
393         },
394         [MODE_1280_720] = {
395                 .width = 1280,
396                 .height = 720,
397                 .hblank_min = { 250, 176 },
398                 .vblank = 1022,
399                 .vblank_min = 41,
400                 .vblank_max = 51540,
401                 .link_freq_idx = 0,
402                 .crop = {
403                         /*
404                          * Note that this mode takes the top 720 lines from the
405                          * 800 of the sensor. It does not take a middle crop.
406                          */
407                         .left = OV9282_PIXEL_ARRAY_LEFT,
408                         .top = OV9282_PIXEL_ARRAY_TOP,
409                         .width = 1280,
410                         .height = 720
411                 },
412                 .reg_list = {
413                         .num_of_regs = ARRAY_SIZE(mode_1280x720_regs),
414                         .regs = mode_1280x720_regs,
415                 },
416         },
417         [MODE_640_400] = {
418                 .width = 640,
419                 .height = 400,
420                 .hblank_min = { 890, 816 },
421                 .vblank = 1022,
422                 .vblank_min = 22,
423                 .vblank_max = 51540,
424                 .link_freq_idx = 0,
425                 .crop = {
426                         .left = OV9282_PIXEL_ARRAY_LEFT,
427                         .top = OV9282_PIXEL_ARRAY_TOP,
428                         .width = 1280,
429                         .height = 800
430                 },
431                 .reg_list = {
432                         .num_of_regs = ARRAY_SIZE(mode_640x400_regs),
433                         .regs = mode_640x400_regs,
434                 },
435         },
436 };
437
438 /**
439  * to_ov9282() - ov9282 V4L2 sub-device to ov9282 device.
440  * @subdev: pointer to ov9282 V4L2 sub-device
441  *
442  * Return: pointer to ov9282 device
443  */
444 static inline struct ov9282 *to_ov9282(struct v4l2_subdev *subdev)
445 {
446         return container_of(subdev, struct ov9282, sd);
447 }
448
449 /**
450  * ov9282_read_reg() - Read registers.
451  * @ov9282: pointer to ov9282 device
452  * @reg: register address
453  * @len: length of bytes to read. Max supported bytes is 4
454  * @val: pointer to register value to be filled.
455  *
456  * Return: 0 if successful, error code otherwise.
457  */
458 static int ov9282_read_reg(struct ov9282 *ov9282, u16 reg, u32 len, u32 *val)
459 {
460         struct i2c_client *client = v4l2_get_subdevdata(&ov9282->sd);
461         struct i2c_msg msgs[2] = {0};
462         u8 addr_buf[2] = {0};
463         u8 data_buf[4] = {0};
464         int ret;
465
466         if (WARN_ON(len > 4))
467                 return -EINVAL;
468
469         put_unaligned_be16(reg, addr_buf);
470
471         /* Write register address */
472         msgs[0].addr = client->addr;
473         msgs[0].flags = 0;
474         msgs[0].len = ARRAY_SIZE(addr_buf);
475         msgs[0].buf = addr_buf;
476
477         /* Read data from register */
478         msgs[1].addr = client->addr;
479         msgs[1].flags = I2C_M_RD;
480         msgs[1].len = len;
481         msgs[1].buf = &data_buf[4 - len];
482
483         ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
484         if (ret != ARRAY_SIZE(msgs))
485                 return -EIO;
486
487         *val = get_unaligned_be32(data_buf);
488
489         return 0;
490 }
491
492 /**
493  * ov9282_write_reg() - Write register
494  * @ov9282: pointer to ov9282 device
495  * @reg: register address
496  * @len: length of bytes. Max supported bytes is 4
497  * @val: register value
498  *
499  * Return: 0 if successful, error code otherwise.
500  */
501 static int ov9282_write_reg(struct ov9282 *ov9282, u16 reg, u32 len, u32 val)
502 {
503         struct i2c_client *client = v4l2_get_subdevdata(&ov9282->sd);
504         u8 buf[6] = {0};
505
506         if (WARN_ON(len > 4))
507                 return -EINVAL;
508
509         put_unaligned_be16(reg, buf);
510         put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
511         if (i2c_master_send(client, buf, len + 2) != len + 2)
512                 return -EIO;
513
514         return 0;
515 }
516
517 /**
518  * ov9282_write_regs() - Write a list of registers
519  * @ov9282: pointer to ov9282 device
520  * @regs: list of registers to be written
521  * @len: length of registers array
522  *
523  * Return: 0 if successful, error code otherwise.
524  */
525 static int ov9282_write_regs(struct ov9282 *ov9282,
526                              const struct ov9282_reg *regs, u32 len)
527 {
528         unsigned int i;
529         int ret;
530
531         for (i = 0; i < len; i++) {
532                 ret = ov9282_write_reg(ov9282, regs[i].address, 1, regs[i].val);
533                 if (ret)
534                         return ret;
535         }
536
537         return 0;
538 }
539
540 /**
541  * ov9282_update_controls() - Update control ranges based on streaming mode
542  * @ov9282: pointer to ov9282 device
543  * @mode: pointer to ov9282_mode sensor mode
544  * @fmt: pointer to the requested mode
545  *
546  * Return: 0 if successful, error code otherwise.
547  */
548 static int ov9282_update_controls(struct ov9282 *ov9282,
549                                   const struct ov9282_mode *mode,
550                                   const struct v4l2_subdev_format *fmt)
551 {
552         u32 hblank_min;
553         s64 pixel_rate;
554         int ret;
555
556         ret = __v4l2_ctrl_s_ctrl(ov9282->link_freq_ctrl, mode->link_freq_idx);
557         if (ret)
558                 return ret;
559
560         pixel_rate = (fmt->format.code == MEDIA_BUS_FMT_Y10_1X10) ?
561                 OV9282_PIXEL_RATE_10BIT : OV9282_PIXEL_RATE_8BIT;
562         ret = __v4l2_ctrl_modify_range(ov9282->pixel_rate, pixel_rate,
563                                        pixel_rate, 1, pixel_rate);
564         if (ret)
565                 return ret;
566
567         hblank_min = mode->hblank_min[ov9282->noncontinuous_clock ? 0 : 1];
568         ret =  __v4l2_ctrl_modify_range(ov9282->hblank_ctrl, hblank_min,
569                                         OV9282_TIMING_HTS_MAX - mode->width, 1,
570                                         hblank_min);
571         if (ret)
572                 return ret;
573
574         return __v4l2_ctrl_modify_range(ov9282->vblank_ctrl, mode->vblank_min,
575                                         mode->vblank_max, 1, mode->vblank);
576 }
577
578 /**
579  * ov9282_update_exp_gain() - Set updated exposure and gain
580  * @ov9282: pointer to ov9282 device
581  * @exposure: updated exposure value
582  * @gain: updated analog gain value
583  *
584  * Return: 0 if successful, error code otherwise.
585  */
586 static int ov9282_update_exp_gain(struct ov9282 *ov9282, u32 exposure, u32 gain)
587 {
588         int ret;
589
590         dev_dbg(ov9282->dev, "Set exp %u, analog gain %u",
591                 exposure, gain);
592
593         ret = ov9282_write_reg(ov9282, OV9282_REG_HOLD, 1, 1);
594         if (ret)
595                 return ret;
596
597         ret = ov9282_write_reg(ov9282, OV9282_REG_EXPOSURE, 3, exposure << 4);
598         if (ret)
599                 goto error_release_group_hold;
600
601         ret = ov9282_write_reg(ov9282, OV9282_REG_AGAIN, 1, gain);
602
603 error_release_group_hold:
604         ov9282_write_reg(ov9282, OV9282_REG_HOLD, 1, 0);
605
606         return ret;
607 }
608
609 static int ov9282_set_ctrl_hflip(struct ov9282 *ov9282, int value)
610 {
611         u32 current_val;
612         int ret = ov9282_read_reg(ov9282, OV9282_REG_TIMING_FORMAT_2, 1,
613                                   &current_val);
614         if (ret)
615                 return ret;
616
617         if (value)
618                 current_val |= OV9282_FLIP_BIT;
619         else
620                 current_val &= ~OV9282_FLIP_BIT;
621
622         return ov9282_write_reg(ov9282, OV9282_REG_TIMING_FORMAT_2, 1,
623                                 current_val);
624 }
625
626 static int ov9282_set_ctrl_vflip(struct ov9282 *ov9282, int value)
627 {
628         u32 current_val;
629         int ret = ov9282_read_reg(ov9282, OV9282_REG_TIMING_FORMAT_1, 1,
630                                   &current_val);
631         if (ret)
632                 return ret;
633
634         if (value)
635                 current_val |= OV9282_FLIP_BIT;
636         else
637                 current_val &= ~OV9282_FLIP_BIT;
638
639         return ov9282_write_reg(ov9282, OV9282_REG_TIMING_FORMAT_1, 1,
640                                 current_val);
641 }
642
643 /**
644  * ov9282_set_ctrl() - Set subdevice control
645  * @ctrl: pointer to v4l2_ctrl structure
646  *
647  * Supported controls:
648  * - V4L2_CID_VBLANK
649  * - cluster controls:
650  *   - V4L2_CID_ANALOGUE_GAIN
651  *   - V4L2_CID_EXPOSURE
652  *
653  * Return: 0 if successful, error code otherwise.
654  */
655 static int ov9282_set_ctrl(struct v4l2_ctrl *ctrl)
656 {
657         struct ov9282 *ov9282 =
658                 container_of(ctrl->handler, struct ov9282, ctrl_handler);
659         u32 analog_gain;
660         u32 exposure;
661         u32 lpfr;
662         int ret;
663
664         switch (ctrl->id) {
665         case V4L2_CID_VBLANK:
666                 ov9282->vblank = ov9282->vblank_ctrl->val;
667
668                 dev_dbg(ov9282->dev, "Received vblank %u, new lpfr %u",
669                         ov9282->vblank,
670                         ov9282->vblank + ov9282->cur_mode->height);
671
672                 ret = __v4l2_ctrl_modify_range(ov9282->exp_ctrl,
673                                                OV9282_EXPOSURE_MIN,
674                                                ov9282->vblank +
675                                                ov9282->cur_mode->height -
676                                                OV9282_EXPOSURE_OFFSET,
677                                                1, OV9282_EXPOSURE_DEFAULT);
678                 break;
679         }
680
681         /* Set controls only if sensor is in power on state */
682         if (!pm_runtime_get_if_in_use(ov9282->dev))
683                 return 0;
684
685         switch (ctrl->id) {
686         case V4L2_CID_EXPOSURE:
687                 exposure = ctrl->val;
688                 analog_gain = ov9282->again_ctrl->val;
689
690                 dev_dbg(ov9282->dev, "Received exp %u, analog gain %u",
691                         exposure, analog_gain);
692
693                 ret = ov9282_update_exp_gain(ov9282, exposure, analog_gain);
694                 break;
695         case V4L2_CID_VBLANK:
696                 lpfr = ov9282->vblank + ov9282->cur_mode->height;
697                 ret = ov9282_write_reg(ov9282, OV9282_REG_LPFR, 2, lpfr);
698                 break;
699         case V4L2_CID_HFLIP:
700                 ret = ov9282_set_ctrl_hflip(ov9282, ctrl->val);
701                 break;
702         case V4L2_CID_VFLIP:
703                 ret = ov9282_set_ctrl_vflip(ov9282, ctrl->val);
704                 break;
705         case V4L2_CID_HBLANK:
706                 ret = ov9282_write_reg(ov9282, OV9282_REG_TIMING_HTS, 2,
707                                        (ctrl->val + ov9282->cur_mode->width) >> 1);
708                 break;
709         default:
710                 dev_err(ov9282->dev, "Invalid control %d", ctrl->id);
711                 ret = -EINVAL;
712         }
713
714         pm_runtime_put(ov9282->dev);
715
716         return ret;
717 }
718
719 /* V4l2 subdevice control ops*/
720 static const struct v4l2_ctrl_ops ov9282_ctrl_ops = {
721         .s_ctrl = ov9282_set_ctrl,
722 };
723
724 /**
725  * ov9282_enum_mbus_code() - Enumerate V4L2 sub-device mbus codes
726  * @sd: pointer to ov9282 V4L2 sub-device structure
727  * @sd_state: V4L2 sub-device configuration
728  * @code: V4L2 sub-device code enumeration need to be filled
729  *
730  * Return: 0 if successful, error code otherwise.
731  */
732 static int ov9282_enum_mbus_code(struct v4l2_subdev *sd,
733                                  struct v4l2_subdev_state *sd_state,
734                                  struct v4l2_subdev_mbus_code_enum *code)
735 {
736         switch (code->index) {
737         case 0:
738                 code->code = MEDIA_BUS_FMT_Y10_1X10;
739                 break;
740         case 1:
741                 code->code = MEDIA_BUS_FMT_Y8_1X8;
742                 break;
743         default:
744                 return -EINVAL;
745         }
746
747         return 0;
748 }
749
750 /**
751  * ov9282_enum_frame_size() - Enumerate V4L2 sub-device frame sizes
752  * @sd: pointer to ov9282 V4L2 sub-device structure
753  * @sd_state: V4L2 sub-device configuration
754  * @fsize: V4L2 sub-device size enumeration need to be filled
755  *
756  * Return: 0 if successful, error code otherwise.
757  */
758 static int ov9282_enum_frame_size(struct v4l2_subdev *sd,
759                                   struct v4l2_subdev_state *sd_state,
760                                   struct v4l2_subdev_frame_size_enum *fsize)
761 {
762         if (fsize->index >= ARRAY_SIZE(supported_modes))
763                 return -EINVAL;
764
765         if (fsize->code != MEDIA_BUS_FMT_Y10_1X10 &&
766             fsize->code != MEDIA_BUS_FMT_Y8_1X8)
767                 return -EINVAL;
768
769         fsize->min_width = supported_modes[fsize->index].width;
770         fsize->max_width = fsize->min_width;
771         fsize->min_height = supported_modes[fsize->index].height;
772         fsize->max_height = fsize->min_height;
773
774         return 0;
775 }
776
777 /**
778  * ov9282_fill_pad_format() - Fill subdevice pad format
779  *                            from selected sensor mode
780  * @ov9282: pointer to ov9282 device
781  * @mode: pointer to ov9282_mode sensor mode
782  * @code: mbus code to be stored
783  * @fmt: V4L2 sub-device format need to be filled
784  */
785 static void ov9282_fill_pad_format(struct ov9282 *ov9282,
786                                    const struct ov9282_mode *mode,
787                                    u32 code,
788                                    struct v4l2_subdev_format *fmt)
789 {
790         fmt->format.width = mode->width;
791         fmt->format.height = mode->height;
792         fmt->format.code = code;
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  * ov9282_get_pad_format() - Get subdevice pad format
802  * @sd: pointer to ov9282 V4L2 sub-device structure
803  * @sd_state: V4L2 sub-device configuration
804  * @fmt: V4L2 sub-device format need to be set
805  *
806  * Return: 0 if successful, error code otherwise.
807  */
808 static int ov9282_get_pad_format(struct v4l2_subdev *sd,
809                                  struct v4l2_subdev_state *sd_state,
810                                  struct v4l2_subdev_format *fmt)
811 {
812         struct ov9282 *ov9282 = to_ov9282(sd);
813
814         mutex_lock(&ov9282->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                 ov9282_fill_pad_format(ov9282, ov9282->cur_mode, ov9282->code,
823                                        fmt);
824         }
825
826         mutex_unlock(&ov9282->mutex);
827
828         return 0;
829 }
830
831 /**
832  * ov9282_set_pad_format() - Set subdevice pad format
833  * @sd: pointer to ov9282 V4L2 sub-device structure
834  * @sd_state: V4L2 sub-device configuration
835  * @fmt: V4L2 sub-device format need to be set
836  *
837  * Return: 0 if successful, error code otherwise.
838  */
839 static int ov9282_set_pad_format(struct v4l2_subdev *sd,
840                                  struct v4l2_subdev_state *sd_state,
841                                  struct v4l2_subdev_format *fmt)
842 {
843         struct ov9282 *ov9282 = to_ov9282(sd);
844         const struct ov9282_mode *mode;
845         u32 code;
846         int ret = 0;
847
848         mutex_lock(&ov9282->mutex);
849
850         mode = v4l2_find_nearest_size(supported_modes,
851                                       ARRAY_SIZE(supported_modes),
852                                       width, height,
853                                       fmt->format.width,
854                                       fmt->format.height);
855         if (fmt->format.code == MEDIA_BUS_FMT_Y8_1X8)
856                 code = MEDIA_BUS_FMT_Y8_1X8;
857         else
858                 code = MEDIA_BUS_FMT_Y10_1X10;
859
860         ov9282_fill_pad_format(ov9282, mode, code, fmt);
861
862         if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
863                 struct v4l2_mbus_framefmt *framefmt;
864
865                 framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
866                 *framefmt = fmt->format;
867         } else {
868                 ret = ov9282_update_controls(ov9282, mode, fmt);
869                 if (!ret) {
870                         ov9282->cur_mode = mode;
871                         ov9282->code = code;
872                 }
873         }
874
875         mutex_unlock(&ov9282->mutex);
876
877         return ret;
878 }
879
880 /**
881  * ov9282_init_pad_cfg() - Initialize sub-device pad configuration
882  * @sd: pointer to ov9282 V4L2 sub-device structure
883  * @sd_state: V4L2 sub-device configuration
884  *
885  * Return: 0 if successful, error code otherwise.
886  */
887 static int ov9282_init_pad_cfg(struct v4l2_subdev *sd,
888                                struct v4l2_subdev_state *sd_state)
889 {
890         struct ov9282 *ov9282 = to_ov9282(sd);
891         struct v4l2_subdev_format fmt = { 0 };
892
893         fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
894         ov9282_fill_pad_format(ov9282, &supported_modes[DEFAULT_MODE],
895                                ov9282->code, &fmt);
896
897         return ov9282_set_pad_format(sd, sd_state, &fmt);
898 }
899
900 static const struct v4l2_rect *
901 __ov9282_get_pad_crop(struct ov9282 *ov9282,
902                       struct v4l2_subdev_state *sd_state,
903                       unsigned int pad, enum v4l2_subdev_format_whence which)
904 {
905         switch (which) {
906         case V4L2_SUBDEV_FORMAT_TRY:
907                 return v4l2_subdev_get_try_crop(&ov9282->sd, sd_state, pad);
908         case V4L2_SUBDEV_FORMAT_ACTIVE:
909                 return &ov9282->cur_mode->crop;
910         }
911
912         return NULL;
913 }
914
915 static int ov9282_get_selection(struct v4l2_subdev *sd,
916                                 struct v4l2_subdev_state *sd_state,
917                                 struct v4l2_subdev_selection *sel)
918 {
919         switch (sel->target) {
920         case V4L2_SEL_TGT_CROP: {
921                 struct ov9282 *ov9282 = to_ov9282(sd);
922
923                 mutex_lock(&ov9282->mutex);
924                 sel->r = *__ov9282_get_pad_crop(ov9282, sd_state, sel->pad,
925                                                 sel->which);
926                 mutex_unlock(&ov9282->mutex);
927
928                 return 0;
929         }
930
931         case V4L2_SEL_TGT_NATIVE_SIZE:
932                 sel->r.top = 0;
933                 sel->r.left = 0;
934                 sel->r.width = OV9282_NATIVE_WIDTH;
935                 sel->r.height = OV9282_NATIVE_HEIGHT;
936
937                 return 0;
938
939         case V4L2_SEL_TGT_CROP_DEFAULT:
940         case V4L2_SEL_TGT_CROP_BOUNDS:
941                 sel->r.top = OV9282_PIXEL_ARRAY_TOP;
942                 sel->r.left = OV9282_PIXEL_ARRAY_LEFT;
943                 sel->r.width = OV9282_PIXEL_ARRAY_WIDTH;
944                 sel->r.height = OV9282_PIXEL_ARRAY_HEIGHT;
945
946                 return 0;
947         }
948
949         return -EINVAL;
950 }
951
952 /**
953  * ov9282_start_streaming() - Start sensor stream
954  * @ov9282: pointer to ov9282 device
955  *
956  * Return: 0 if successful, error code otherwise.
957  */
958 static int ov9282_start_streaming(struct ov9282 *ov9282)
959 {
960         const struct ov9282_reg bitdepth_regs[2][2] = {
961                 {
962                         {OV9282_REG_PLL_CTRL_0D, OV9282_PLL_CTRL_0D_RAW10},
963                         {OV9282_REG_ANA_CORE_2, OV9282_ANA_CORE2_RAW10},
964                 }, {
965                         {OV9282_REG_PLL_CTRL_0D, OV9282_PLL_CTRL_0D_RAW8},
966                         {OV9282_REG_ANA_CORE_2, OV9282_ANA_CORE2_RAW8},
967                 }
968         };
969         const struct ov9282_reg_list *reg_list;
970         int bitdepth_index;
971         int ret;
972
973         /* Write common registers */
974         ret = ov9282_write_regs(ov9282, common_regs_list.regs,
975                                 common_regs_list.num_of_regs);
976         if (ret) {
977                 dev_err(ov9282->dev, "fail to write common registers");
978                 return ret;
979         }
980
981         bitdepth_index = ov9282->code == MEDIA_BUS_FMT_Y10_1X10 ? 0 : 1;
982         ret = ov9282_write_regs(ov9282, bitdepth_regs[bitdepth_index], 2);
983         if (ret) {
984                 dev_err(ov9282->dev, "fail to write bitdepth regs");
985                 return ret;
986         }
987
988         /* Write sensor mode registers */
989         reg_list = &ov9282->cur_mode->reg_list;
990         ret = ov9282_write_regs(ov9282, reg_list->regs, reg_list->num_of_regs);
991         if (ret) {
992                 dev_err(ov9282->dev, "fail to write initial registers");
993                 return ret;
994         }
995
996         /* Setup handler will write actual exposure and gain */
997         ret =  __v4l2_ctrl_handler_setup(ov9282->sd.ctrl_handler);
998         if (ret) {
999                 dev_err(ov9282->dev, "fail to setup handler");
1000                 return ret;
1001         }
1002
1003         /* Start streaming */
1004         ret = ov9282_write_reg(ov9282, OV9282_REG_MODE_SELECT,
1005                                1, OV9282_MODE_STREAMING);
1006         if (ret) {
1007                 dev_err(ov9282->dev, "fail to start streaming");
1008                 return ret;
1009         }
1010
1011         return 0;
1012 }
1013
1014 /**
1015  * ov9282_stop_streaming() - Stop sensor stream
1016  * @ov9282: pointer to ov9282 device
1017  *
1018  * Return: 0 if successful, error code otherwise.
1019  */
1020 static int ov9282_stop_streaming(struct ov9282 *ov9282)
1021 {
1022         return ov9282_write_reg(ov9282, OV9282_REG_MODE_SELECT,
1023                                 1, OV9282_MODE_STANDBY);
1024 }
1025
1026 /**
1027  * ov9282_set_stream() - Enable sensor streaming
1028  * @sd: pointer to ov9282 subdevice
1029  * @enable: set to enable sensor streaming
1030  *
1031  * Return: 0 if successful, error code otherwise.
1032  */
1033 static int ov9282_set_stream(struct v4l2_subdev *sd, int enable)
1034 {
1035         struct ov9282 *ov9282 = to_ov9282(sd);
1036         int ret;
1037
1038         mutex_lock(&ov9282->mutex);
1039
1040         if (ov9282->streaming == enable) {
1041                 mutex_unlock(&ov9282->mutex);
1042                 return 0;
1043         }
1044
1045         if (enable) {
1046                 ret = pm_runtime_resume_and_get(ov9282->dev);
1047                 if (ret)
1048                         goto error_unlock;
1049
1050                 ret = ov9282_start_streaming(ov9282);
1051                 if (ret)
1052                         goto error_power_off;
1053         } else {
1054                 ov9282_stop_streaming(ov9282);
1055                 pm_runtime_put(ov9282->dev);
1056         }
1057
1058         ov9282->streaming = enable;
1059
1060         mutex_unlock(&ov9282->mutex);
1061
1062         return 0;
1063
1064 error_power_off:
1065         pm_runtime_put(ov9282->dev);
1066 error_unlock:
1067         mutex_unlock(&ov9282->mutex);
1068
1069         return ret;
1070 }
1071
1072 /**
1073  * ov9282_detect() - Detect ov9282 sensor
1074  * @ov9282: pointer to ov9282 device
1075  *
1076  * Return: 0 if successful, -EIO if sensor id does not match
1077  */
1078 static int ov9282_detect(struct ov9282 *ov9282)
1079 {
1080         int ret;
1081         u32 val, msb;
1082
1083         ret = ov9282_read_reg(ov9282, OV9282_REG_ID + 1, 1, &val);
1084         if (ret)
1085                 return ret;
1086         ret = ov9282_read_reg(ov9282, OV9282_REG_ID, 1, &msb);
1087         if (ret)
1088                 return ret;
1089
1090         val |= (msb << 8);
1091         if (val != OV9282_ID) {
1092                 dev_err(ov9282->dev, "chip id mismatch: %x!=%x",
1093                         OV9282_ID, val);
1094                 return -ENXIO;
1095         }
1096
1097         return 0;
1098 }
1099
1100 static int ov9282_configure_regulators(struct ov9282 *ov9282)
1101 {
1102         unsigned int i;
1103
1104         for (i = 0; i < OV9282_NUM_SUPPLIES; i++)
1105                 ov9282->supplies[i].supply = ov9282_supply_names[i];
1106
1107         return devm_regulator_bulk_get(ov9282->dev,
1108                                        OV9282_NUM_SUPPLIES,
1109                                        ov9282->supplies);
1110 }
1111
1112 /**
1113  * ov9282_parse_hw_config() - Parse HW configuration and check if supported
1114  * @ov9282: pointer to ov9282 device
1115  *
1116  * Return: 0 if successful, error code otherwise.
1117  */
1118 static int ov9282_parse_hw_config(struct ov9282 *ov9282)
1119 {
1120         struct fwnode_handle *fwnode = dev_fwnode(ov9282->dev);
1121         struct v4l2_fwnode_endpoint bus_cfg = {
1122                 .bus_type = V4L2_MBUS_CSI2_DPHY
1123         };
1124         struct fwnode_handle *ep;
1125         unsigned long rate;
1126         unsigned int i;
1127         int ret;
1128
1129         if (!fwnode)
1130                 return -ENXIO;
1131
1132         /* Request optional reset pin */
1133         ov9282->reset_gpio = devm_gpiod_get_optional(ov9282->dev, "reset",
1134                                                      GPIOD_OUT_LOW);
1135         if (IS_ERR(ov9282->reset_gpio)) {
1136                 dev_err(ov9282->dev, "failed to get reset gpio %ld",
1137                         PTR_ERR(ov9282->reset_gpio));
1138                 return PTR_ERR(ov9282->reset_gpio);
1139         }
1140
1141         /* Get sensor input clock */
1142         ov9282->inclk = devm_clk_get(ov9282->dev, NULL);
1143         if (IS_ERR(ov9282->inclk)) {
1144                 dev_err(ov9282->dev, "could not get inclk");
1145                 return PTR_ERR(ov9282->inclk);
1146         }
1147
1148         ret = ov9282_configure_regulators(ov9282);
1149         if (ret)
1150                 return dev_err_probe(ov9282->dev, ret,
1151                                      "Failed to get power regulators\n");
1152
1153         rate = clk_get_rate(ov9282->inclk);
1154         if (rate != OV9282_INCLK_RATE) {
1155                 dev_err(ov9282->dev, "inclk frequency mismatch");
1156                 return -EINVAL;
1157         }
1158
1159         ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
1160         if (!ep)
1161                 return -ENXIO;
1162
1163         ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
1164         fwnode_handle_put(ep);
1165         if (ret)
1166                 return ret;
1167
1168         ov9282->noncontinuous_clock =
1169                 bus_cfg.bus.mipi_csi2.flags & V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
1170
1171         if (bus_cfg.bus.mipi_csi2.num_data_lanes != OV9282_NUM_DATA_LANES) {
1172                 dev_err(ov9282->dev,
1173                         "number of CSI2 data lanes %d is not supported",
1174                         bus_cfg.bus.mipi_csi2.num_data_lanes);
1175                 ret = -EINVAL;
1176                 goto done_endpoint_free;
1177         }
1178
1179         if (!bus_cfg.nr_of_link_frequencies) {
1180                 dev_err(ov9282->dev, "no link frequencies defined");
1181                 ret = -EINVAL;
1182                 goto done_endpoint_free;
1183         }
1184
1185         for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
1186                 if (bus_cfg.link_frequencies[i] == OV9282_LINK_FREQ)
1187                         goto done_endpoint_free;
1188
1189         ret = -EINVAL;
1190
1191 done_endpoint_free:
1192         v4l2_fwnode_endpoint_free(&bus_cfg);
1193
1194         return ret;
1195 }
1196
1197 /* V4l2 subdevice ops */
1198 static const struct v4l2_subdev_core_ops ov9282_core_ops = {
1199         .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
1200         .unsubscribe_event = v4l2_event_subdev_unsubscribe,
1201 };
1202
1203 static const struct v4l2_subdev_video_ops ov9282_video_ops = {
1204         .s_stream = ov9282_set_stream,
1205 };
1206
1207 static const struct v4l2_subdev_pad_ops ov9282_pad_ops = {
1208         .init_cfg = ov9282_init_pad_cfg,
1209         .enum_mbus_code = ov9282_enum_mbus_code,
1210         .enum_frame_size = ov9282_enum_frame_size,
1211         .get_fmt = ov9282_get_pad_format,
1212         .set_fmt = ov9282_set_pad_format,
1213         .get_selection = ov9282_get_selection,
1214 };
1215
1216 static const struct v4l2_subdev_ops ov9282_subdev_ops = {
1217         .core = &ov9282_core_ops,
1218         .video = &ov9282_video_ops,
1219         .pad = &ov9282_pad_ops,
1220 };
1221
1222 /**
1223  * ov9282_power_on() - Sensor power on sequence
1224  * @dev: pointer to i2c device
1225  *
1226  * Return: 0 if successful, error code otherwise.
1227  */
1228 static int ov9282_power_on(struct device *dev)
1229 {
1230         struct v4l2_subdev *sd = dev_get_drvdata(dev);
1231         struct ov9282 *ov9282 = to_ov9282(sd);
1232         int ret;
1233
1234         ret = regulator_bulk_enable(OV9282_NUM_SUPPLIES, ov9282->supplies);
1235         if (ret < 0) {
1236                 dev_err(dev, "Failed to enable regulators\n");
1237                 return ret;
1238         }
1239
1240         usleep_range(400, 600);
1241
1242         gpiod_set_value_cansleep(ov9282->reset_gpio, 1);
1243
1244         ret = clk_prepare_enable(ov9282->inclk);
1245         if (ret) {
1246                 dev_err(ov9282->dev, "fail to enable inclk");
1247                 goto error_reset;
1248         }
1249
1250         usleep_range(400, 600);
1251
1252         ret = ov9282_write_reg(ov9282, OV9282_REG_MIPI_CTRL00, 1,
1253                                ov9282->noncontinuous_clock ?
1254                                         OV9282_GATED_CLOCK : 0);
1255         if (ret) {
1256                 dev_err(ov9282->dev, "fail to write MIPI_CTRL00");
1257                 goto error_clk;
1258         }
1259
1260         return 0;
1261
1262 error_clk:
1263         clk_disable_unprepare(ov9282->inclk);
1264 error_reset:
1265         gpiod_set_value_cansleep(ov9282->reset_gpio, 0);
1266
1267         regulator_bulk_disable(OV9282_NUM_SUPPLIES, ov9282->supplies);
1268
1269         return ret;
1270 }
1271
1272 /**
1273  * ov9282_power_off() - Sensor power off sequence
1274  * @dev: pointer to i2c device
1275  *
1276  * Return: 0 if successful, error code otherwise.
1277  */
1278 static int ov9282_power_off(struct device *dev)
1279 {
1280         struct v4l2_subdev *sd = dev_get_drvdata(dev);
1281         struct ov9282 *ov9282 = to_ov9282(sd);
1282
1283         gpiod_set_value_cansleep(ov9282->reset_gpio, 0);
1284
1285         clk_disable_unprepare(ov9282->inclk);
1286
1287         regulator_bulk_disable(OV9282_NUM_SUPPLIES, ov9282->supplies);
1288
1289         return 0;
1290 }
1291
1292 /**
1293  * ov9282_init_controls() - Initialize sensor subdevice controls
1294  * @ov9282: pointer to ov9282 device
1295  *
1296  * Return: 0 if successful, error code otherwise.
1297  */
1298 static int ov9282_init_controls(struct ov9282 *ov9282)
1299 {
1300         struct v4l2_ctrl_handler *ctrl_hdlr = &ov9282->ctrl_handler;
1301         const struct ov9282_mode *mode = ov9282->cur_mode;
1302         struct v4l2_fwnode_device_properties props;
1303         u32 hblank_min;
1304         u32 lpfr;
1305         int ret;
1306
1307         ret = v4l2_ctrl_handler_init(ctrl_hdlr, 10);
1308         if (ret)
1309                 return ret;
1310
1311         /* Serialize controls with sensor device */
1312         ctrl_hdlr->lock = &ov9282->mutex;
1313
1314         /* Initialize exposure and gain */
1315         lpfr = mode->vblank + mode->height;
1316         ov9282->exp_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1317                                              &ov9282_ctrl_ops,
1318                                              V4L2_CID_EXPOSURE,
1319                                              OV9282_EXPOSURE_MIN,
1320                                              lpfr - OV9282_EXPOSURE_OFFSET,
1321                                              OV9282_EXPOSURE_STEP,
1322                                              OV9282_EXPOSURE_DEFAULT);
1323
1324         ov9282->again_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1325                                                &ov9282_ctrl_ops,
1326                                                V4L2_CID_ANALOGUE_GAIN,
1327                                                OV9282_AGAIN_MIN,
1328                                                OV9282_AGAIN_MAX,
1329                                                OV9282_AGAIN_STEP,
1330                                                OV9282_AGAIN_DEFAULT);
1331
1332         v4l2_ctrl_cluster(2, &ov9282->exp_ctrl);
1333
1334         ov9282->vblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1335                                                 &ov9282_ctrl_ops,
1336                                                 V4L2_CID_VBLANK,
1337                                                 mode->vblank_min,
1338                                                 mode->vblank_max,
1339                                                 1, mode->vblank);
1340
1341         v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops, V4L2_CID_VFLIP,
1342                           0, 1, 1, 1);
1343
1344         v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops, V4L2_CID_HFLIP,
1345                           0, 1, 1, 1);
1346
1347         /* Read only controls */
1348         ov9282->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops,
1349                                                V4L2_CID_PIXEL_RATE,
1350                                                OV9282_PIXEL_RATE_10BIT,
1351                                                OV9282_PIXEL_RATE_10BIT, 1,
1352                                                OV9282_PIXEL_RATE_10BIT);
1353
1354         ov9282->link_freq_ctrl = v4l2_ctrl_new_int_menu(ctrl_hdlr,
1355                                                         &ov9282_ctrl_ops,
1356                                                         V4L2_CID_LINK_FREQ,
1357                                                         ARRAY_SIZE(link_freq) -
1358                                                         1,
1359                                                         mode->link_freq_idx,
1360                                                         link_freq);
1361         if (ov9282->link_freq_ctrl)
1362                 ov9282->link_freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1363
1364         hblank_min = mode->hblank_min[ov9282->noncontinuous_clock ? 0 : 1];
1365         ov9282->hblank_ctrl = v4l2_ctrl_new_std(ctrl_hdlr,
1366                                                 &ov9282_ctrl_ops,
1367                                                 V4L2_CID_HBLANK,
1368                                                 hblank_min,
1369                                                 OV9282_TIMING_HTS_MAX - mode->width,
1370                                                 1, hblank_min);
1371
1372         ret = v4l2_fwnode_device_parse(ov9282->dev, &props);
1373         if (!ret) {
1374                 /* Failure sets ctrl_hdlr->error, which we check afterwards anyway */
1375                 v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &ov9282_ctrl_ops,
1376                                                 &props);
1377         }
1378
1379         if (ctrl_hdlr->error || ret) {
1380                 dev_err(ov9282->dev, "control init failed: %d",
1381                         ctrl_hdlr->error);
1382                 v4l2_ctrl_handler_free(ctrl_hdlr);
1383                 return ctrl_hdlr->error;
1384         }
1385
1386         ov9282->sd.ctrl_handler = ctrl_hdlr;
1387
1388         return 0;
1389 }
1390
1391 /**
1392  * ov9282_probe() - I2C client device binding
1393  * @client: pointer to i2c client device
1394  *
1395  * Return: 0 if successful, error code otherwise.
1396  */
1397 static int ov9282_probe(struct i2c_client *client)
1398 {
1399         struct ov9282 *ov9282;
1400         int ret;
1401
1402         ov9282 = devm_kzalloc(&client->dev, sizeof(*ov9282), GFP_KERNEL);
1403         if (!ov9282)
1404                 return -ENOMEM;
1405
1406         ov9282->dev = &client->dev;
1407
1408         /* Initialize subdev */
1409         v4l2_i2c_subdev_init(&ov9282->sd, client, &ov9282_subdev_ops);
1410         v4l2_i2c_subdev_set_name(&ov9282->sd, client,
1411                                  device_get_match_data(ov9282->dev), NULL);
1412
1413         ret = ov9282_parse_hw_config(ov9282);
1414         if (ret) {
1415                 dev_err(ov9282->dev, "HW configuration is not supported");
1416                 return ret;
1417         }
1418
1419         mutex_init(&ov9282->mutex);
1420
1421         ret = ov9282_power_on(ov9282->dev);
1422         if (ret) {
1423                 dev_err(ov9282->dev, "failed to power-on the sensor");
1424                 goto error_mutex_destroy;
1425         }
1426
1427         /* Check module identity */
1428         ret = ov9282_detect(ov9282);
1429         if (ret) {
1430                 dev_err(ov9282->dev, "failed to find sensor: %d", ret);
1431                 goto error_power_off;
1432         }
1433
1434         /* Set default mode to first mode */
1435         ov9282->cur_mode = &supported_modes[DEFAULT_MODE];
1436         ov9282->code = MEDIA_BUS_FMT_Y10_1X10;
1437         ov9282->vblank = ov9282->cur_mode->vblank;
1438
1439         ret = ov9282_init_controls(ov9282);
1440         if (ret) {
1441                 dev_err(ov9282->dev, "failed to init controls: %d", ret);
1442                 goto error_power_off;
1443         }
1444
1445         /* Initialize subdev */
1446         ov9282->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
1447                             V4L2_SUBDEV_FL_HAS_EVENTS;
1448         ov9282->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1449
1450         /* Initialize source pad */
1451         ov9282->pad.flags = MEDIA_PAD_FL_SOURCE;
1452         ret = media_entity_pads_init(&ov9282->sd.entity, 1, &ov9282->pad);
1453         if (ret) {
1454                 dev_err(ov9282->dev, "failed to init entity pads: %d", ret);
1455                 goto error_handler_free;
1456         }
1457
1458         ret = v4l2_async_register_subdev_sensor(&ov9282->sd);
1459         if (ret < 0) {
1460                 dev_err(ov9282->dev,
1461                         "failed to register async subdev: %d", ret);
1462                 goto error_media_entity;
1463         }
1464
1465         pm_runtime_set_active(ov9282->dev);
1466         pm_runtime_enable(ov9282->dev);
1467         pm_runtime_idle(ov9282->dev);
1468
1469         return 0;
1470
1471 error_media_entity:
1472         media_entity_cleanup(&ov9282->sd.entity);
1473 error_handler_free:
1474         v4l2_ctrl_handler_free(ov9282->sd.ctrl_handler);
1475 error_power_off:
1476         ov9282_power_off(ov9282->dev);
1477 error_mutex_destroy:
1478         mutex_destroy(&ov9282->mutex);
1479
1480         return ret;
1481 }
1482
1483 /**
1484  * ov9282_remove() - I2C client device unbinding
1485  * @client: pointer to I2C client device
1486  *
1487  * Return: 0 if successful, error code otherwise.
1488  */
1489 static void ov9282_remove(struct i2c_client *client)
1490 {
1491         struct v4l2_subdev *sd = i2c_get_clientdata(client);
1492         struct ov9282 *ov9282 = to_ov9282(sd);
1493
1494         v4l2_async_unregister_subdev(sd);
1495         media_entity_cleanup(&sd->entity);
1496         v4l2_ctrl_handler_free(sd->ctrl_handler);
1497
1498         pm_runtime_disable(&client->dev);
1499         if (!pm_runtime_status_suspended(&client->dev))
1500                 ov9282_power_off(&client->dev);
1501         pm_runtime_set_suspended(&client->dev);
1502
1503         mutex_destroy(&ov9282->mutex);
1504 }
1505
1506 static const struct dev_pm_ops ov9282_pm_ops = {
1507         SET_RUNTIME_PM_OPS(ov9282_power_off, ov9282_power_on, NULL)
1508 };
1509
1510 static const struct of_device_id ov9282_of_match[] = {
1511         { .compatible = "ovti,ov9281", .data = "ov9281" },
1512         { .compatible = "ovti,ov9282", .data = "ov9282" },
1513         { }
1514 };
1515
1516 MODULE_DEVICE_TABLE(of, ov9282_of_match);
1517
1518 static struct i2c_driver ov9282_driver = {
1519         .probe = ov9282_probe,
1520         .remove = ov9282_remove,
1521         .driver = {
1522                 .name = "ov9282",
1523                 .pm = &ov9282_pm_ops,
1524                 .of_match_table = ov9282_of_match,
1525         },
1526 };
1527
1528 module_i2c_driver(ov9282_driver);
1529
1530 MODULE_DESCRIPTION("OmniVision ov9282 sensor driver");
1531 MODULE_LICENSE("GPL");