Merge tag 'v5.15.57' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / drivers / media / i2c / ov2311.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Omnivision OV2311 1600x1300 global shutter image sensor driver
4  * Copyright (C) 2022, Raspberry Pi (Trading) Ltd
5  *
6  * This driver is based on the OV9281 driver.
7  * Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd.
8  * Register configuration from
9  * https://github.com/ArduCAM/ArduCAM_USB_Camera_Shield/tree/master/Config/USB3.0_UC-425_Rev.C%2BUC-628_Rev.B/OV2311
10  * with additional exposure and gain register information from
11  * https://github.com/renesas-rcar/linux-bsp/tree/0cf6e36f5bf49e1c2aab87139ec5b588623c56f8/drivers/media/i2c/imagers
12  */
13
14 #include <linux/clk.h>
15 #include <linux/device.h>
16 #include <linux/delay.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/i2c.h>
19 #include <linux/module.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/regulator/consumer.h>
22 #include <media/media-entity.h>
23 #include <media/v4l2-async.h>
24 #include <media/v4l2-ctrls.h>
25 #include <media/v4l2-event.h>
26 #include <media/v4l2-fwnode.h>
27 #include <media/v4l2-subdev.h>
28
29 #define OV2311_LINK_FREQ                400000000
30 #define OV2311_LANES                    2
31
32 /* pixel rate = link frequency * 2 * lanes / BITS_PER_SAMPLE */
33 #define OV2311_PIXEL_RATE_10BIT         (OV2311_LINK_FREQ * 2 * \
34                                          OV2311_LANES / 10)
35 #define OV2311_PIXEL_RATE_8BIT          (OV2311_LINK_FREQ * 2 * \
36                                          OV2311_LANES / 8)
37 #define OV2311_XVCLK_FREQ               24000000
38
39 #define CHIP_ID                         0x2311
40 #define OV2311_REG_CHIP_ID              0x300a
41
42 #define OV2311_REG_CTRL_MODE            0x0100
43 #define OV2311_MODE_SW_STANDBY          0x0
44 #define OV2311_MODE_STREAMING           BIT(0)
45
46 #define OV2311_REG_V_FLIP               0x3820
47 #define OV2311_REG_H_FLIP               0x3821
48 #define OV2311_FLIP_BIT                 BIT(2)
49
50 #define OV2311_REG_EXPOSURE             0x3501
51 #define OV2311_EXPOSURE_MIN             4
52 #define OV2311_EXPOSURE_STEP            1
53 #define OV2311_VTS_MAX                  0xffff
54
55 #define OV2311_REG_GAIN_H               0x3508
56 #define OV2311_REG_GAIN_L               0x3509
57 #define OV2311_GAIN_H_MASK              0x07
58 #define OV2311_GAIN_H_SHIFT             8
59 #define OV2311_GAIN_L_MASK              0xff
60 #define OV2311_GAIN_MIN                 0x100
61 #define OV2311_GAIN_MAX                 0x780
62 #define OV2311_GAIN_STEP                1
63 #define OV2311_GAIN_DEFAULT             OV2311_GAIN_MIN
64
65 #define OV2311_REG_TEST_PATTERN         0x5e00
66 #define OV2311_TEST_PATTERN_ENABLE      0x80
67 #define OV2311_TEST_PATTERN_DISABLE     0x0
68
69 #define OV2311_REG_VTS                  0x380e
70
71 /*
72  * OV2311 native and active pixel array size.
73  * Datasheet not available to confirm these values. renesas-rcar linux-bsp tree
74  * has these values.
75  */
76 #define OV2311_NATIVE_WIDTH             1616U
77 #define OV2311_NATIVE_HEIGHT            1316U
78 #define OV2311_PIXEL_ARRAY_LEFT         8U
79 #define OV2311_PIXEL_ARRAY_TOP          8U
80 #define OV2311_PIXEL_ARRAY_WIDTH        1600U
81 #define OV2311_PIXEL_ARRAY_HEIGHT       1300U
82
83 #define REG_NULL                        0xFFFF
84
85 #define OV2311_REG_VALUE_08BIT          1
86 #define OV2311_REG_VALUE_16BIT          2
87 #define OV2311_REG_VALUE_24BIT          3
88
89 #define OV2311_NAME                     "ov2311"
90
91 static const char * const ov2311_supply_names[] = {
92         "avdd",         /* Analog power */
93         "dovdd",        /* Digital I/O power */
94         "dvdd",         /* Digital core power */
95 };
96
97 #define OV2311_NUM_SUPPLIES ARRAY_SIZE(ov2311_supply_names)
98
99 struct regval {
100         u16 addr;
101         u8 val;
102 };
103
104 struct ov2311_mode {
105         u32 width;
106         u32 height;
107         u32 hts_def;
108         u32 vts_def;
109         u32 exp_def;
110         struct v4l2_rect crop;
111         const struct regval *reg_list;
112 };
113
114 struct ov2311 {
115         struct i2c_client       *client;
116         struct clk              *xvclk;
117         struct gpio_desc        *reset_gpio;
118         struct gpio_desc        *pwdn_gpio;
119         struct regulator_bulk_data supplies[OV2311_NUM_SUPPLIES];
120
121         struct v4l2_subdev      subdev;
122         struct media_pad        pad;
123         struct v4l2_ctrl_handler ctrl_handler;
124         struct v4l2_ctrl        *exposure;
125         struct v4l2_ctrl        *hblank;
126         struct v4l2_ctrl        *vblank;
127         struct v4l2_ctrl        *pixel_rate;
128         /*
129          * Mutex for serialized access:
130          * Protect sensor module set pad format and start/stop streaming safely.
131          */
132         struct mutex            mutex;
133
134         /* Streaming on/off */
135         bool streaming;
136
137         const struct ov2311_mode *cur_mode;
138         u32                     code;
139 };
140
141 #define to_ov2311(sd) container_of(sd, struct ov2311, subdev)
142
143 /*
144  * Xclk 24Mhz
145  * max_framerate 60fps for 10 bit, 74.6fps for 8 bit.
146  */
147 static const struct regval ov2311_common_regs[] = {
148         { 0x0103, 0x01 },
149         { 0x0100, 0x00 },
150         { 0x0300, 0x01 },
151         { 0x0302, 0x32 },
152         { 0x0303, 0x00 },
153         { 0x0304, 0x03 },
154         { 0x0305, 0x02 },
155         { 0x0306, 0x01 },
156         { 0x030e, 0x04 },
157         { 0x3001, 0x02 },
158         { 0x3004, 0x00 },
159         { 0x3005, 0x00 },
160         { 0x3006, 0x00 },
161         { 0x3011, 0x0d },
162         { 0x3014, 0x04 },
163         { 0x301c, 0xf0 },
164         { 0x3020, 0x00 },
165         { 0x302c, 0x00 },
166         { 0x302d, 0x12 },
167         { 0x302e, 0x4c },
168         { 0x302f, 0x8c },
169         { 0x3030, 0x10 },
170         { 0x303f, 0x03 },
171         { 0x3103, 0x00 },
172         { 0x3106, 0x08 },
173         { 0x31ff, 0x01 },
174         { 0x3501, 0x05 },
175         { 0x3502, 0xba },
176         { 0x3506, 0x00 },
177         { 0x3507, 0x00 },
178         { 0x3620, 0x67 },
179         { 0x3633, 0x78 },
180         { 0x3666, 0x00 },
181         { 0x3670, 0x68 },
182         { 0x3674, 0x10 },
183         { 0x3675, 0x00 },
184         { 0x3680, 0x84 },
185         { 0x36a2, 0x04 },
186         { 0x36a3, 0x80 },
187         { 0x36b0, 0x00 },
188         { 0x3700, 0x35 },
189         { 0x3704, 0x59 },
190         { 0x3712, 0x00 },
191         { 0x3713, 0x02 },
192         { 0x379b, 0x01 },
193         { 0x379c, 0x10 },
194         { 0x3800, 0x00 },
195         { 0x3801, 0x00 },
196         { 0x3804, 0x06 },
197         { 0x3805, 0x4f },
198         { 0x3810, 0x00 },
199         { 0x3811, 0x08 },
200         { 0x3812, 0x00 },
201         { 0x3813, 0x08 },
202         { 0x3814, 0x11 },
203         { 0x3815, 0x11 },
204         { 0x3816, 0x00 },
205         { 0x3817, 0x00 },
206         { 0x3818, 0x04 },
207         { 0x3819, 0x00 },
208         { 0x382b, 0x5a },
209         { 0x382c, 0x09 },
210         { 0x382d, 0x9a },
211         { 0x3882, 0x02 },
212         { 0x3883, 0x6c },
213         { 0x3885, 0x07 },
214         { 0x389d, 0x03 },
215         { 0x38a6, 0x00 },
216         { 0x38a7, 0x01 },
217         { 0x38b3, 0x07 },
218         { 0x38b1, 0x00 },
219         { 0x38e5, 0x02 },
220         { 0x38e7, 0x00 },
221         { 0x38e8, 0x00 },
222         { 0x3910, 0xff },
223         { 0x3911, 0xff },
224         { 0x3912, 0x08 },
225         { 0x3913, 0x00 },
226         { 0x3914, 0x00 },
227         { 0x3915, 0x00 },
228         { 0x391c, 0x00 },
229         { 0x3920, 0xa5 },
230         { 0x3921, 0x00 },
231         { 0x3922, 0x00 },
232         { 0x3923, 0x00 },
233         { 0x3924, 0x05 },
234         { 0x3925, 0x00 },
235         { 0x3926, 0x00 },
236         { 0x3927, 0x00 },
237         { 0x3928, 0x1a },
238         { 0x392d, 0x05 },
239         { 0x392e, 0xf2 },
240         { 0x392f, 0x40 },
241         { 0x4001, 0x00 },
242         { 0x4003, 0x40 },
243         { 0x4008, 0x12 },
244         { 0x4009, 0x1b },
245         { 0x400c, 0x0c },
246         { 0x400d, 0x13 },
247         { 0x4010, 0xf0 },
248         { 0x4011, 0x00 },
249         { 0x4016, 0x00 },
250         { 0x4017, 0x04 },
251         { 0x4042, 0x11 },
252         { 0x4043, 0x70 },
253         { 0x4045, 0x00 },
254         { 0x4409, 0x5f },
255         { 0x450b, 0x00 },
256         { 0x4600, 0x00 },
257         { 0x4601, 0xa0 },
258         { 0x4708, 0x09 },
259         { 0x470c, 0x81 },
260         { 0x4710, 0x06 },
261         { 0x4711, 0x00 },
262         { 0x4800, 0x00 },
263         { 0x481f, 0x30 },
264         { 0x4837, 0x14 },
265         { 0x4f00, 0x00 },
266         { 0x4f07, 0x00 },
267         { 0x4f08, 0x03 },
268         { 0x4f09, 0x08 },
269         { 0x4f0c, 0x06 },
270         { 0x4f0d, 0x02 },
271         { 0x4f10, 0x00 },
272         { 0x4f11, 0x00 },
273         { 0x4f12, 0x07 },
274         { 0x4f13, 0xe2 },
275         { 0x5000, 0x9f },
276         { 0x5001, 0x20 },
277         { 0x5026, 0x00 },
278         { 0x5c00, 0x00 },
279         { 0x5c01, 0x2c },
280         { 0x5c02, 0x00 },
281         { 0x5c03, 0x7f },
282         { 0x5e00, 0x00 },
283         { 0x5e01, 0x41 },
284         {REG_NULL, 0x00},
285 };
286
287 static const struct regval ov2311_1600x1300_regs[] = {
288         { 0x3802, 0x00 },
289         { 0x3803, 0x00 },
290         { 0x3806, 0x05 },
291         { 0x3807, 0x23 },
292         { 0x3808, 0x06 },
293         { 0x3809, 0x40 },
294         { 0x380a, 0x05 },
295         { 0x380b, 0x14 },
296         { 0x380c, 0x03 },
297         { 0x380d, 0x88 },
298         {REG_NULL, 0x00},
299 };
300
301 static const struct regval ov2311_1600x1080_regs[] = {
302         { 0x3802, 0x00 },
303         { 0x3803, 0x6e },
304         { 0x3806, 0x04 },
305         { 0x3807, 0xae },
306         { 0x3808, 0x06 },
307         { 0x3809, 0x40 },
308         { 0x380a, 0x04 },
309         { 0x380b, 0x38 },
310         { 0x380c, 0x03 },
311         { 0x380d, 0x88 },
312
313         { 0x5d01, 0x00 },
314         { 0x5d02, 0x04 },
315         { 0x5d03, 0x00 },
316         { 0x5d04, 0x04 },
317         { 0x5d05, 0x00 },
318         {REG_NULL, 0x00},
319 };
320
321 static const struct regval op_10bit[] = {
322         { 0x030d, 0x5a },
323         { 0x3662, 0x65 },
324         {REG_NULL, 0x00},
325 };
326
327 static const struct regval op_8bit[] = {
328         { 0x030d, 0x70 },
329         { 0x3662, 0x67 },
330         {REG_NULL, 0x00},
331 };
332
333 static const struct ov2311_mode supported_modes[] = {
334         {
335                 .width = 1600,
336                 .height = 1300,
337                 .exp_def = 0x0320,
338                 .hts_def = (0x0388 * 2),/* Registers 0x380c / 0x380d  * 2 */
339                 .vts_def = 0x5c2,       /* Registers 0x380e / 0x380f
340                                          * 60fps for 10bpp
341                                          */
342                 .crop = {
343                         .left = OV2311_PIXEL_ARRAY_LEFT,
344                         .top = OV2311_PIXEL_ARRAY_TOP,
345                         .width = 1600,
346                         .height = 1300
347                 },
348                 .reg_list = ov2311_1600x1300_regs,
349         },
350         {
351                 .width = 1600,
352                 .height = 1080,
353                 .exp_def = 0x0320,
354                 .hts_def = (0x0388 * 2),/* Registers 0x380c / 0x380d  * 2 */
355                 .vts_def = 0x5c2,       /* Registers 0x380e / 0x380f
356                                          * 60fps for 10bpp
357                                          */
358                 .crop = {
359                         .left = OV2311_PIXEL_ARRAY_LEFT,
360                         .top = 110 + OV2311_PIXEL_ARRAY_TOP,
361                         .width = 1600,
362                         .height = 1080
363                 },
364                 .reg_list = ov2311_1600x1080_regs,
365         },
366 };
367
368 static const s64 link_freq_menu_items[] = {
369         OV2311_LINK_FREQ
370 };
371
372 static const char * const ov2311_test_pattern_menu[] = {
373         "Disabled",
374         "Vertical Color Bar Type 1",
375         "Vertical Color Bar Type 2",
376         "Vertical Color Bar Type 3",
377         "Vertical Color Bar Type 4"
378 };
379
380 /* Write registers up to 4 at a time */
381 static int ov2311_write_reg(struct i2c_client *client, u16 reg,
382                             u32 len, u32 val)
383 {
384         u32 buf_i, val_i;
385         u8 buf[6];
386         u8 *val_p;
387         __be32 val_be;
388
389         if (len > 4)
390                 return -EINVAL;
391
392         buf[0] = reg >> 8;
393         buf[1] = reg & 0xff;
394
395         val_be = cpu_to_be32(val);
396         val_p = (u8 *)&val_be;
397         buf_i = 2;
398         val_i = 4 - len;
399
400         while (val_i < 4)
401                 buf[buf_i++] = val_p[val_i++];
402
403         if (i2c_master_send(client, buf, len + 2) != len + 2)
404                 return -EIO;
405
406         return 0;
407 }
408
409 static int ov2311_write_array(struct i2c_client *client,
410                               const struct regval *regs)
411 {
412         u32 i;
413         int ret = 0;
414
415         for (i = 0; ret == 0 && regs[i].addr != REG_NULL; i++)
416                 ret = ov2311_write_reg(client, regs[i].addr,
417                                        OV2311_REG_VALUE_08BIT, regs[i].val);
418
419         return ret;
420 }
421
422 /* Read registers up to 4 at a time */
423 static int ov2311_read_reg(struct i2c_client *client, u16 reg, unsigned int len,
424                            u32 *val)
425 {
426         struct i2c_msg msgs[2];
427         u8 *data_be_p;
428         __be32 data_be = 0;
429         __be16 reg_addr_be = cpu_to_be16(reg);
430         int ret;
431
432         if (len > 4 || !len)
433                 return -EINVAL;
434
435         data_be_p = (u8 *)&data_be;
436         /* Write register address */
437         msgs[0].addr = client->addr;
438         msgs[0].flags = 0;
439         msgs[0].len = 2;
440         msgs[0].buf = (u8 *)&reg_addr_be;
441
442         /* Read data from register */
443         msgs[1].addr = client->addr;
444         msgs[1].flags = I2C_M_RD;
445         msgs[1].len = len;
446         msgs[1].buf = &data_be_p[4 - len];
447
448         ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
449         if (ret != ARRAY_SIZE(msgs))
450                 return -EIO;
451
452         *val = be32_to_cpu(data_be);
453
454         return 0;
455 }
456
457 static int ov2311_set_fmt(struct v4l2_subdev *sd,
458                           struct v4l2_subdev_state *sd_state,
459                           struct v4l2_subdev_format *fmt)
460 {
461         struct ov2311 *ov2311 = to_ov2311(sd);
462         const struct ov2311_mode *mode;
463         s64 h_blank, vblank_def, pixel_rate;
464
465         mutex_lock(&ov2311->mutex);
466
467         mode = v4l2_find_nearest_size(supported_modes,
468                                       ARRAY_SIZE(supported_modes),
469                                       width, height,
470                                       fmt->format.width,
471                                       fmt->format.height);
472         if (fmt->format.code != MEDIA_BUS_FMT_Y8_1X8)
473                 fmt->format.code = MEDIA_BUS_FMT_Y10_1X10;
474         fmt->format.width = mode->width;
475         fmt->format.height = mode->height;
476         fmt->format.field = V4L2_FIELD_NONE;
477         fmt->format.colorspace = V4L2_COLORSPACE_RAW;
478         fmt->format.ycbcr_enc =
479                         V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->format.colorspace);
480         fmt->format.quantization =
481                 V4L2_MAP_QUANTIZATION_DEFAULT(true, fmt->format.colorspace,
482                                               fmt->format.ycbcr_enc);
483         fmt->format.xfer_func =
484                 V4L2_MAP_XFER_FUNC_DEFAULT(fmt->format.colorspace);
485
486         if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
487                 *v4l2_subdev_get_try_format(sd, sd_state, fmt->pad) =
488                                                                 fmt->format;
489         } else {
490                 ov2311->cur_mode = mode;
491                 ov2311->code = fmt->format.code;
492                 h_blank = mode->hts_def - mode->width;
493                 __v4l2_ctrl_modify_range(ov2311->hblank, h_blank,
494                                          h_blank, 1, h_blank);
495                 __v4l2_ctrl_s_ctrl(ov2311->hblank, h_blank);
496                 vblank_def = mode->vts_def - mode->height;
497                 __v4l2_ctrl_modify_range(ov2311->vblank, vblank_def,
498                                          OV2311_VTS_MAX - mode->height,
499                                          1, vblank_def);
500                 __v4l2_ctrl_s_ctrl(ov2311->vblank, vblank_def);
501
502                 pixel_rate = (fmt->format.code == MEDIA_BUS_FMT_Y10_1X10) ?
503                         OV2311_PIXEL_RATE_10BIT : OV2311_PIXEL_RATE_8BIT;
504                 __v4l2_ctrl_modify_range(ov2311->pixel_rate, pixel_rate,
505                                          pixel_rate, 1, pixel_rate);
506         }
507
508         mutex_unlock(&ov2311->mutex);
509
510         return 0;
511 }
512
513 static int ov2311_get_fmt(struct v4l2_subdev *sd,
514                           struct v4l2_subdev_state *sd_state,
515                           struct v4l2_subdev_format *fmt)
516 {
517         struct ov2311 *ov2311 = to_ov2311(sd);
518         const struct ov2311_mode *mode = ov2311->cur_mode;
519
520         mutex_lock(&ov2311->mutex);
521         if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
522                 fmt->format = *v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
523         } else {
524                 fmt->format.width = mode->width;
525                 fmt->format.height = mode->height;
526                 fmt->format.code = ov2311->code;
527                 fmt->format.field = V4L2_FIELD_NONE;
528                 fmt->format.colorspace = V4L2_COLORSPACE_SRGB;
529                 fmt->format.ycbcr_enc =
530                         V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->format.colorspace);
531                 fmt->format.quantization =
532                         V4L2_MAP_QUANTIZATION_DEFAULT(true,
533                                                       fmt->format.colorspace,
534                                                       fmt->format.ycbcr_enc);
535                 fmt->format.xfer_func =
536                         V4L2_MAP_XFER_FUNC_DEFAULT(fmt->format.colorspace);
537         }
538         mutex_unlock(&ov2311->mutex);
539
540         return 0;
541 }
542
543 static int ov2311_enum_mbus_code(struct v4l2_subdev *sd,
544                                  struct v4l2_subdev_state *sd_state,
545                                  struct v4l2_subdev_mbus_code_enum *code)
546 {
547         switch (code->index) {
548         default:
549                 return -EINVAL;
550         case 0:
551                 code->code = MEDIA_BUS_FMT_Y10_1X10;
552                 break;
553         case 1:
554                 code->code = MEDIA_BUS_FMT_Y8_1X8;
555                 break;
556         }
557
558         return 0;
559 }
560
561 static int ov2311_enum_frame_sizes(struct v4l2_subdev *sd,
562                                    struct v4l2_subdev_state *sd_state,
563                                    struct v4l2_subdev_frame_size_enum *fse)
564 {
565         if (fse->index >= ARRAY_SIZE(supported_modes))
566                 return -EINVAL;
567
568         if (fse->code != MEDIA_BUS_FMT_Y10_1X10 &&
569             fse->code != MEDIA_BUS_FMT_Y8_1X8)
570                 return -EINVAL;
571
572         fse->min_width  = supported_modes[fse->index].width;
573         fse->max_width  = supported_modes[fse->index].width;
574         fse->max_height = supported_modes[fse->index].height;
575         fse->min_height = supported_modes[fse->index].height;
576
577         return 0;
578 }
579
580 static int ov2311_enable_test_pattern(struct ov2311 *ov2311, u32 pattern)
581 {
582         u32 val;
583
584         if (pattern)
585                 val = (pattern - 1) | OV2311_TEST_PATTERN_ENABLE;
586         else
587                 val = OV2311_TEST_PATTERN_DISABLE;
588
589         return ov2311_write_reg(ov2311->client, OV2311_REG_TEST_PATTERN,
590                                 OV2311_REG_VALUE_08BIT, val);
591 }
592
593 static const struct v4l2_rect *
594 __ov2311_get_pad_crop(struct ov2311 *ov2311, struct v4l2_subdev_state *sd_state,
595                       unsigned int pad, enum v4l2_subdev_format_whence which)
596 {
597         switch (which) {
598         case V4L2_SUBDEV_FORMAT_TRY:
599                 return v4l2_subdev_get_try_crop(&ov2311->subdev, sd_state, pad);
600         case V4L2_SUBDEV_FORMAT_ACTIVE:
601                 return &ov2311->cur_mode->crop;
602         }
603
604         return NULL;
605 }
606
607 static int ov2311_get_selection(struct v4l2_subdev *sd,
608                                 struct v4l2_subdev_state *sd_state,
609                                 struct v4l2_subdev_selection *sel)
610 {
611         switch (sel->target) {
612         case V4L2_SEL_TGT_CROP: {
613                 struct ov2311 *ov2311 = to_ov2311(sd);
614
615                 mutex_lock(&ov2311->mutex);
616                 sel->r = *__ov2311_get_pad_crop(ov2311, sd_state, sel->pad,
617                                                 sel->which);
618                 mutex_unlock(&ov2311->mutex);
619
620                 return 0;
621         }
622
623         case V4L2_SEL_TGT_NATIVE_SIZE:
624                 sel->r.top = 0;
625                 sel->r.left = 0;
626                 sel->r.width = OV2311_NATIVE_WIDTH;
627                 sel->r.height = OV2311_NATIVE_HEIGHT;
628
629                 return 0;
630
631         case V4L2_SEL_TGT_CROP_DEFAULT:
632         case V4L2_SEL_TGT_CROP_BOUNDS:
633                 sel->r.top = OV2311_PIXEL_ARRAY_TOP;
634                 sel->r.left = OV2311_PIXEL_ARRAY_LEFT;
635                 sel->r.width = OV2311_PIXEL_ARRAY_WIDTH;
636                 sel->r.height = OV2311_PIXEL_ARRAY_HEIGHT;
637
638                 return 0;
639         }
640
641         return -EINVAL;
642 }
643
644 static int ov2311_start_stream(struct ov2311 *ov2311)
645 {
646         int ret;
647
648         ret = ov2311_write_array(ov2311->client, ov2311_common_regs);
649         if (ret)
650                 return ret;
651
652         ret = ov2311_write_array(ov2311->client, ov2311->cur_mode->reg_list);
653         if (ret)
654                 return ret;
655
656         if (ov2311->code == MEDIA_BUS_FMT_Y10_1X10)
657                 ret = ov2311_write_array(ov2311->client, op_10bit);
658         else
659                 ret = ov2311_write_array(ov2311->client, op_8bit);
660         if (ret)
661                 return ret;
662
663         /* In case these controls are set before streaming */
664         mutex_unlock(&ov2311->mutex);
665         ret = v4l2_ctrl_handler_setup(&ov2311->ctrl_handler);
666         mutex_lock(&ov2311->mutex);
667         if (ret)
668                 return ret;
669
670         return ov2311_write_reg(ov2311->client, OV2311_REG_CTRL_MODE,
671                                 OV2311_REG_VALUE_08BIT, OV2311_MODE_STREAMING);
672 }
673
674 static int ov2311_stop_stream(struct ov2311 *ov2311)
675 {
676         return ov2311_write_reg(ov2311->client, OV2311_REG_CTRL_MODE,
677                                 OV2311_REG_VALUE_08BIT, OV2311_MODE_SW_STANDBY);
678 }
679
680 static int ov2311_s_stream(struct v4l2_subdev *sd, int enable)
681 {
682         struct ov2311 *ov2311 = to_ov2311(sd);
683         struct i2c_client *client = ov2311->client;
684         int ret = 0;
685
686         mutex_lock(&ov2311->mutex);
687         if (ov2311->streaming == enable) {
688                 mutex_unlock(&ov2311->mutex);
689                 return 0;
690         }
691
692         if (enable) {
693                 ret = pm_runtime_resume_and_get(&client->dev);
694                 if (ret < 0)
695                         goto unlock_and_return;
696
697                 ret = ov2311_start_stream(ov2311);
698                 if (ret) {
699                         v4l2_err(sd, "start stream failed while write regs\n");
700                         pm_runtime_put(&client->dev);
701                         goto unlock_and_return;
702                 }
703         } else {
704                 ov2311_stop_stream(ov2311);
705                 pm_runtime_put(&client->dev);
706         }
707
708         ov2311->streaming = enable;
709
710 unlock_and_return:
711         mutex_unlock(&ov2311->mutex);
712
713         return ret;
714 }
715
716 static int ov2311_power_on(struct device *dev)
717 {
718         struct v4l2_subdev *sd = dev_get_drvdata(dev);
719         struct ov2311 *ov2311 = to_ov2311(sd);
720         int ret;
721
722         ret = clk_set_rate(ov2311->xvclk, OV2311_XVCLK_FREQ);
723         if (ret < 0)
724                 dev_warn(dev, "Failed to set xvclk rate (24MHz)\n");
725         if (clk_get_rate(ov2311->xvclk) != OV2311_XVCLK_FREQ)
726                 dev_warn(dev, "xvclk mismatched, modes are based on 24MHz - rate is %lu\n",
727                          clk_get_rate(ov2311->xvclk));
728
729         ret = clk_prepare_enable(ov2311->xvclk);
730         if (ret < 0) {
731                 dev_err(dev, "Failed to enable xvclk\n");
732                 return ret;
733         }
734
735         gpiod_set_value_cansleep(ov2311->reset_gpio, 0);
736
737         ret = regulator_bulk_enable(OV2311_NUM_SUPPLIES, ov2311->supplies);
738         if (ret < 0) {
739                 dev_err(dev, "Failed to enable regulators\n");
740                 goto disable_clk;
741         }
742
743         gpiod_set_value_cansleep(ov2311->reset_gpio, 1);
744
745         usleep_range(500, 1000);
746         gpiod_set_value_cansleep(ov2311->pwdn_gpio, 1);
747
748         usleep_range(1000, 2000);
749
750         return 0;
751
752 disable_clk:
753         clk_disable_unprepare(ov2311->xvclk);
754
755         return ret;
756 }
757
758 static int ov2311_power_off(struct device *dev)
759 {
760         struct v4l2_subdev *sd = dev_get_drvdata(dev);
761         struct ov2311 *ov2311 = to_ov2311(sd);
762
763         gpiod_set_value_cansleep(ov2311->pwdn_gpio, 0);
764         clk_disable_unprepare(ov2311->xvclk);
765         gpiod_set_value_cansleep(ov2311->reset_gpio, 0);
766         regulator_bulk_disable(OV2311_NUM_SUPPLIES, ov2311->supplies);
767
768         return 0;
769 }
770
771 static int ov2311_runtime_resume(struct device *dev)
772 {
773         struct v4l2_subdev *sd = dev_get_drvdata(dev);
774         struct ov2311 *ov2311 = to_ov2311(sd);
775         int ret;
776
777         if (ov2311->streaming) {
778                 ret = ov2311_start_stream(ov2311);
779                 if (ret)
780                         goto error;
781         }
782         return 0;
783
784 error:
785         ov2311_stop_stream(ov2311);
786         ov2311->streaming = 0;
787         return ret;
788 }
789
790 static int ov2311_runtime_suspend(struct device *dev)
791 {
792         struct v4l2_subdev *sd = dev_get_drvdata(dev);
793         struct ov2311 *ov2311 = to_ov2311(sd);
794
795         if (ov2311->streaming)
796                 ov2311_stop_stream(ov2311);
797
798         return 0;
799 }
800
801 static int ov2311_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
802 {
803         struct ov2311 *ov2311 = to_ov2311(sd);
804         struct v4l2_mbus_framefmt *try_fmt =
805                                 v4l2_subdev_get_try_format(sd, fh->state, 0);
806         const struct ov2311_mode *def_mode = &supported_modes[0];
807
808         mutex_lock(&ov2311->mutex);
809         /* Initialize try_fmt */
810         try_fmt->width = def_mode->width;
811         try_fmt->height = def_mode->height;
812         try_fmt->code = MEDIA_BUS_FMT_Y10_1X10;
813         try_fmt->field = V4L2_FIELD_NONE;
814         try_fmt->colorspace = V4L2_COLORSPACE_RAW;
815         try_fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(try_fmt->colorspace);
816         try_fmt->quantization =
817                 V4L2_MAP_QUANTIZATION_DEFAULT(true, try_fmt->colorspace,
818                                               try_fmt->ycbcr_enc);
819         try_fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(try_fmt->colorspace);
820
821         mutex_unlock(&ov2311->mutex);
822         /* No crop or compose */
823
824         return 0;
825 }
826
827 static const struct dev_pm_ops ov2311_pm_ops = {
828         SET_RUNTIME_PM_OPS(ov2311_runtime_suspend, ov2311_runtime_resume, NULL)
829         SET_RUNTIME_PM_OPS(ov2311_power_off, ov2311_power_on, NULL)
830 };
831
832 static const struct v4l2_subdev_internal_ops ov2311_internal_ops = {
833         .open = ov2311_open,
834 };
835
836 static const struct v4l2_subdev_core_ops ov2311_core_ops = {
837         .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
838         .unsubscribe_event = v4l2_event_subdev_unsubscribe,
839 };
840
841 static const struct v4l2_subdev_video_ops ov2311_video_ops = {
842         .s_stream = ov2311_s_stream,
843 };
844
845 static const struct v4l2_subdev_pad_ops ov2311_pad_ops = {
846         .enum_mbus_code = ov2311_enum_mbus_code,
847         .enum_frame_size = ov2311_enum_frame_sizes,
848         .get_fmt = ov2311_get_fmt,
849         .set_fmt = ov2311_set_fmt,
850         .get_selection = ov2311_get_selection,
851 };
852
853 static const struct v4l2_subdev_ops ov2311_subdev_ops = {
854         .core   = &ov2311_core_ops,
855         .video  = &ov2311_video_ops,
856         .pad    = &ov2311_pad_ops,
857 };
858
859 static int ov2311_set_ctrl(struct v4l2_ctrl *ctrl)
860 {
861         struct ov2311 *ov2311 = container_of(ctrl->handler,
862                                              struct ov2311, ctrl_handler);
863         struct i2c_client *client = ov2311->client;
864         s64 max;
865         int ret = 0;
866
867         /* Propagate change of current control to all related controls */
868         switch (ctrl->id) {
869         case V4L2_CID_VBLANK:
870                 /* Update max exposure while meeting expected vblanking */
871                 max = ov2311->cur_mode->height + ctrl->val - 4;
872                 __v4l2_ctrl_modify_range(ov2311->exposure,
873                                          ov2311->exposure->minimum, max,
874                                          ov2311->exposure->step,
875                                          ov2311->exposure->default_value);
876                 break;
877         }
878
879         if (pm_runtime_get(&client->dev) <= 0)
880                 return 0;
881
882         switch (ctrl->id) {
883         case V4L2_CID_EXPOSURE:
884                 ret = ov2311_write_reg(ov2311->client, OV2311_REG_EXPOSURE,
885                                        OV2311_REG_VALUE_16BIT, ctrl->val);
886                 break;
887         case V4L2_CID_ANALOGUE_GAIN:
888                 ret = ov2311_write_reg(ov2311->client, OV2311_REG_GAIN_H,
889                                        OV2311_REG_VALUE_08BIT,
890                                        (ctrl->val >> OV2311_GAIN_H_SHIFT) &
891                                                         OV2311_GAIN_H_MASK);
892                 ret |= ov2311_write_reg(ov2311->client, OV2311_REG_GAIN_L,
893                                        OV2311_REG_VALUE_08BIT,
894                                        ctrl->val & OV2311_GAIN_L_MASK);
895                 break;
896         case V4L2_CID_VBLANK:
897                 ret = ov2311_write_reg(ov2311->client, OV2311_REG_VTS,
898                                        OV2311_REG_VALUE_16BIT,
899                                        ctrl->val + ov2311->cur_mode->height);
900                 break;
901         case V4L2_CID_TEST_PATTERN:
902                 ret = ov2311_enable_test_pattern(ov2311, ctrl->val);
903                 break;
904         case V4L2_CID_HFLIP:
905                 ret = ov2311_write_reg(ov2311->client, OV2311_REG_H_FLIP,
906                                        OV2311_REG_VALUE_08BIT,
907                                        ctrl->val ? OV2311_FLIP_BIT : 0);
908                 break;
909         case V4L2_CID_VFLIP:
910                 ret = ov2311_write_reg(ov2311->client, OV2311_REG_V_FLIP,
911                                        OV2311_REG_VALUE_08BIT,
912                                        ctrl->val ? OV2311_FLIP_BIT : 0);
913                 break;
914         default:
915                 dev_warn(&client->dev, "%s Unhandled id:0x%x, val:0x%x\n",
916                          __func__, ctrl->id, ctrl->val);
917                 break;
918         }
919
920         pm_runtime_put(&client->dev);
921
922         return ret;
923 }
924
925 static const struct v4l2_ctrl_ops ov2311_ctrl_ops = {
926         .s_ctrl = ov2311_set_ctrl,
927 };
928
929 static int ov2311_initialize_controls(struct ov2311 *ov2311)
930 {
931         struct v4l2_fwnode_device_properties props;
932         const struct ov2311_mode *mode;
933         struct v4l2_ctrl_handler *handler;
934         struct v4l2_ctrl *ctrl;
935         s64 exposure_max, vblank_def;
936         u32 h_blank;
937         int ret;
938
939         handler = &ov2311->ctrl_handler;
940         mode = ov2311->cur_mode;
941         ret = v4l2_ctrl_handler_init(handler, 11);
942         if (ret)
943                 return ret;
944         handler->lock = &ov2311->mutex;
945
946         ctrl = v4l2_ctrl_new_int_menu(handler, NULL, V4L2_CID_LINK_FREQ,
947                                       0, 0, link_freq_menu_items);
948         if (ctrl)
949                 ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
950
951         ov2311->pixel_rate = v4l2_ctrl_new_std(handler, NULL,
952                                                V4L2_CID_PIXEL_RATE,
953                                                OV2311_PIXEL_RATE_10BIT,
954                                                OV2311_PIXEL_RATE_10BIT, 1,
955                                                OV2311_PIXEL_RATE_10BIT);
956
957         h_blank = mode->hts_def - mode->width;
958         ov2311->hblank = v4l2_ctrl_new_std(handler, NULL, V4L2_CID_HBLANK,
959                                            h_blank, h_blank, 1, h_blank);
960         if (ov2311->hblank)
961                 ov2311->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
962
963         vblank_def = mode->vts_def - mode->height;
964         ov2311->vblank = v4l2_ctrl_new_std(handler, &ov2311_ctrl_ops,
965                                            V4L2_CID_VBLANK, vblank_def,
966                                            OV2311_VTS_MAX - mode->height, 1,
967                                            vblank_def);
968
969         exposure_max = mode->vts_def - 4;
970         ov2311->exposure = v4l2_ctrl_new_std(handler, &ov2311_ctrl_ops,
971                                              V4L2_CID_EXPOSURE,
972                                              OV2311_EXPOSURE_MIN, exposure_max,
973                                              OV2311_EXPOSURE_STEP,
974                                              mode->exp_def);
975
976         v4l2_ctrl_new_std(handler, &ov2311_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
977                           OV2311_GAIN_MIN, OV2311_GAIN_MAX, OV2311_GAIN_STEP,
978                           OV2311_GAIN_DEFAULT);
979
980         v4l2_ctrl_new_std_menu_items(handler, &ov2311_ctrl_ops,
981                                      V4L2_CID_TEST_PATTERN,
982                                      ARRAY_SIZE(ov2311_test_pattern_menu) - 1,
983                                      0, 0, ov2311_test_pattern_menu);
984
985         v4l2_ctrl_new_std(handler, &ov2311_ctrl_ops,
986                           V4L2_CID_HFLIP, 0, 1, 1, 0);
987
988         v4l2_ctrl_new_std(handler, &ov2311_ctrl_ops,
989                           V4L2_CID_VFLIP, 0, 1, 1, 0);
990
991         ret = v4l2_fwnode_device_parse(&ov2311->client->dev, &props);
992         if (ret)
993                 goto err_free_handler;
994
995         ret = v4l2_ctrl_new_fwnode_properties(handler, &ov2311_ctrl_ops,
996                                               &props);
997         if (ret)
998                 goto err_free_handler;
999
1000         if (handler->error) {
1001                 ret = handler->error;
1002                 dev_err(&ov2311->client->dev,
1003                         "Failed to init controls(%d)\n", ret);
1004                 goto err_free_handler;
1005         }
1006
1007         ov2311->subdev.ctrl_handler = handler;
1008
1009         return 0;
1010
1011 err_free_handler:
1012         v4l2_ctrl_handler_free(handler);
1013
1014         return ret;
1015 }
1016
1017 static int ov2311_check_sensor_id(struct ov2311 *ov2311,
1018                                   struct i2c_client *client)
1019 {
1020         struct device *dev = &ov2311->client->dev;
1021         u32 id = 0, id_msb;
1022         int ret;
1023
1024         ret = ov2311_read_reg(client, OV2311_REG_CHIP_ID + 1,
1025                               OV2311_REG_VALUE_08BIT, &id);
1026         if (!ret)
1027                 ret = ov2311_read_reg(client, OV2311_REG_CHIP_ID,
1028                                       OV2311_REG_VALUE_08BIT, &id_msb);
1029         id |= (id_msb << 8);
1030         if (ret || id != CHIP_ID) {
1031                 dev_err(dev, "Unexpected sensor id(%04x), ret(%d)\n", id, ret);
1032                 return -ENODEV;
1033         }
1034
1035         dev_info(dev, "Detected OV%06x sensor\n", CHIP_ID);
1036
1037         return 0;
1038 }
1039
1040 static int ov2311_configure_regulators(struct ov2311 *ov2311)
1041 {
1042         unsigned int i;
1043
1044         for (i = 0; i < OV2311_NUM_SUPPLIES; i++)
1045                 ov2311->supplies[i].supply = ov2311_supply_names[i];
1046
1047         return devm_regulator_bulk_get(&ov2311->client->dev,
1048                                        OV2311_NUM_SUPPLIES,
1049                                        ov2311->supplies);
1050 }
1051
1052 static int ov2311_probe(struct i2c_client *client,
1053                         const struct i2c_device_id *id)
1054 {
1055         struct device *dev = &client->dev;
1056         struct ov2311 *ov2311;
1057         struct v4l2_subdev *sd;
1058         int ret;
1059
1060         ov2311 = devm_kzalloc(dev, sizeof(*ov2311), GFP_KERNEL);
1061         if (!ov2311)
1062                 return -ENOMEM;
1063
1064         ov2311->client = client;
1065         ov2311->cur_mode = &supported_modes[0];
1066
1067         ov2311->xvclk = devm_clk_get(dev, "xvclk");
1068         if (IS_ERR(ov2311->xvclk)) {
1069                 dev_err(dev, "Failed to get xvclk\n");
1070                 return -EINVAL;
1071         }
1072
1073         ov2311->reset_gpio = devm_gpiod_get_optional(dev, "reset",
1074                                                      GPIOD_OUT_LOW);
1075         if (IS_ERR(ov2311->reset_gpio))
1076                 dev_warn(dev, "Failed to get reset-gpios\n");
1077
1078         ov2311->pwdn_gpio = devm_gpiod_get_optional(dev, "pwdn", GPIOD_OUT_LOW);
1079         if (IS_ERR(ov2311->pwdn_gpio))
1080                 dev_warn(dev, "Failed to get pwdn-gpios\n");
1081
1082         ret = ov2311_configure_regulators(ov2311);
1083         if (ret) {
1084                 dev_err(dev, "Failed to get power regulators\n");
1085                 return ret;
1086         }
1087
1088         mutex_init(&ov2311->mutex);
1089
1090         sd = &ov2311->subdev;
1091         v4l2_i2c_subdev_init(sd, client, &ov2311_subdev_ops);
1092         ret = ov2311_initialize_controls(ov2311);
1093         if (ret)
1094                 goto err_destroy_mutex;
1095
1096         ret = ov2311_power_on(&client->dev);
1097         if (ret)
1098                 goto err_free_handler;
1099
1100         ret = ov2311_check_sensor_id(ov2311, client);
1101         if (ret)
1102                 goto err_power_off;
1103
1104         sd->internal_ops = &ov2311_internal_ops;
1105         sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1106
1107         ov2311->pad.flags = MEDIA_PAD_FL_SOURCE;
1108         sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1109         ret = media_entity_pads_init(&sd->entity, 1, &ov2311->pad);
1110         if (ret < 0)
1111                 goto err_power_off;
1112
1113         ret = v4l2_async_register_subdev(sd);
1114         if (ret) {
1115                 dev_err(dev, "v4l2 async register subdev failed\n");
1116                 goto err_clean_entity;
1117         }
1118
1119         pm_runtime_set_active(dev);
1120         pm_runtime_enable(dev);
1121         pm_runtime_idle(dev);
1122
1123         return 0;
1124
1125 err_clean_entity:
1126         media_entity_cleanup(&sd->entity);
1127 err_power_off:
1128         ov2311_power_off(&client->dev);
1129 err_free_handler:
1130         v4l2_ctrl_handler_free(&ov2311->ctrl_handler);
1131 err_destroy_mutex:
1132         mutex_destroy(&ov2311->mutex);
1133
1134         return ret;
1135 }
1136
1137 static int ov2311_remove(struct i2c_client *client)
1138 {
1139         struct v4l2_subdev *sd = i2c_get_clientdata(client);
1140         struct ov2311 *ov2311 = to_ov2311(sd);
1141
1142         v4l2_async_unregister_subdev(sd);
1143         media_entity_cleanup(&sd->entity);
1144         v4l2_ctrl_handler_free(&ov2311->ctrl_handler);
1145         mutex_destroy(&ov2311->mutex);
1146
1147         pm_runtime_disable(&client->dev);
1148         if (!pm_runtime_status_suspended(&client->dev))
1149                 ov2311_power_off(&client->dev);
1150         pm_runtime_set_suspended(&client->dev);
1151
1152         return 0;
1153 }
1154
1155 static const struct of_device_id ov2311_of_match[] = {
1156         { .compatible = "ovti,ov2311" },
1157         {},
1158 };
1159 MODULE_DEVICE_TABLE(of, ov2311_of_match);
1160
1161 static const struct i2c_device_id ov2311_match_id[] = {
1162         { "ovti,ov2311", 0 },
1163         { },
1164 };
1165
1166 static struct i2c_driver ov2311_i2c_driver = {
1167         .driver = {
1168                 .name = OV2311_NAME,
1169                 .pm = &ov2311_pm_ops,
1170                 .of_match_table = of_match_ptr(ov2311_of_match),
1171         },
1172         .probe          = &ov2311_probe,
1173         .remove         = &ov2311_remove,
1174         .id_table       = ov2311_match_id,
1175 };
1176
1177 module_i2c_driver(ov2311_i2c_driver);
1178
1179 MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com");
1180 MODULE_DESCRIPTION("OmniVision OV2311 sensor driver");
1181 MODULE_LICENSE("GPL v2");