76f97d053e45b5bb098f5cbe2581e7ea4d8bbbf0
[platform/kernel/linux-starfive.git] / drivers / media / i2c / ov2680.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Omnivision OV2680 CMOS Image Sensor driver
4  *
5  * Copyright (C) 2018 Linaro Ltd
6  *
7  * Based on OV5640 Sensor Driver
8  * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
9  * Copyright (C) 2014-2017 Mentor Graphics Inc.
10  *
11  */
12
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/err.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/i2c.h>
18 #include <linux/init.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/module.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/regmap.h>
23 #include <linux/regulator/consumer.h>
24
25 #include <media/v4l2-cci.h>
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-subdev.h>
29
30 #define OV2680_CHIP_ID                          0x2680
31
32 #define OV2680_REG_STREAM_CTRL                  CCI_REG8(0x0100)
33 #define OV2680_REG_SOFT_RESET                   CCI_REG8(0x0103)
34
35 #define OV2680_REG_CHIP_ID                      CCI_REG16(0x300a)
36 #define OV2680_REG_PLL_MULTIPLIER               CCI_REG16(0x3081)
37
38 #define OV2680_REG_EXPOSURE_PK                  CCI_REG24(0x3500)
39 #define OV2680_REG_R_MANUAL                     CCI_REG8(0x3503)
40 #define OV2680_REG_GAIN_PK                      CCI_REG16(0x350a)
41 #define OV2680_REG_TIMING_HTS                   CCI_REG16(0x380c)
42 #define OV2680_REG_TIMING_VTS                   CCI_REG16(0x380e)
43 #define OV2680_REG_FORMAT1                      CCI_REG8(0x3820)
44 #define OV2680_REG_FORMAT2                      CCI_REG8(0x3821)
45
46 #define OV2680_REG_ISP_CTRL00                   CCI_REG8(0x5080)
47
48 #define OV2680_FRAME_RATE                       30
49
50 #define OV2680_WIDTH_MAX                        1600
51 #define OV2680_HEIGHT_MAX                       1200
52
53 #define OV2680_DEFAULT_WIDTH                    800
54 #define OV2680_DEFAULT_HEIGHT                   600
55
56 enum ov2680_mode_id {
57         OV2680_MODE_QUXGA_800_600,
58         OV2680_MODE_720P_1280_720,
59         OV2680_MODE_UXGA_1600_1200,
60         OV2680_MODE_MAX,
61 };
62
63 static const char * const ov2680_supply_name[] = {
64         "DOVDD",
65         "DVDD",
66         "AVDD",
67 };
68
69 #define OV2680_NUM_SUPPLIES ARRAY_SIZE(ov2680_supply_name)
70
71 enum {
72         OV2680_19_2_MHZ,
73         OV2680_24_MHZ,
74 };
75
76 static const unsigned long ov2680_xvclk_freqs[] = {
77         [OV2680_19_2_MHZ] = 19200000,
78         [OV2680_24_MHZ] = 24000000,
79 };
80
81 static const u8 ov2680_pll_multipliers[] = {
82         [OV2680_19_2_MHZ] = 69,
83         [OV2680_24_MHZ] = 55,
84 };
85
86 struct ov2680_mode_info {
87         const char *name;
88         enum ov2680_mode_id id;
89         u32 width;
90         u32 height;
91         const struct reg_sequence *reg_data;
92         u32 reg_data_size;
93 };
94
95 struct ov2680_ctrls {
96         struct v4l2_ctrl_handler handler;
97         struct v4l2_ctrl *exposure;
98         struct v4l2_ctrl *gain;
99         struct v4l2_ctrl *hflip;
100         struct v4l2_ctrl *vflip;
101         struct v4l2_ctrl *test_pattern;
102 };
103
104 struct ov2680_mode {
105         struct v4l2_mbus_framefmt       fmt;
106         struct v4l2_fract               frame_interval;
107 };
108
109 struct ov2680_dev {
110         struct device                   *dev;
111         struct regmap                   *regmap;
112         struct v4l2_subdev              sd;
113
114         struct media_pad                pad;
115         struct clk                      *xvclk;
116         u32                             xvclk_freq;
117         u8                              pll_mult;
118         struct regulator_bulk_data      supplies[OV2680_NUM_SUPPLIES];
119
120         struct gpio_desc                *pwdn_gpio;
121         struct mutex                    lock; /* protect members */
122
123         bool                            is_streaming;
124
125         struct ov2680_ctrls             ctrls;
126         struct ov2680_mode              mode;
127
128         const struct ov2680_mode_info   *current_mode;
129 };
130
131 static const char * const test_pattern_menu[] = {
132         "Disabled",
133         "Color Bars",
134         "Random Data",
135         "Square",
136         "Black Image",
137 };
138
139 static const int ov2680_hv_flip_bayer_order[] = {
140         MEDIA_BUS_FMT_SBGGR10_1X10,
141         MEDIA_BUS_FMT_SGRBG10_1X10,
142         MEDIA_BUS_FMT_SGBRG10_1X10,
143         MEDIA_BUS_FMT_SRGGB10_1X10,
144 };
145
146 static const struct reg_sequence ov2680_setting_30fps_QUXGA_800_600[] = {
147         /* Set PLL SP DIV to 1 for binning mode */
148         {0x3086, 0x01},
149
150         /* Sensor control register 0x0a to 0x23 for binning mode */
151         {0x370a, 0x23},
152
153         /* Set X and Y output size to 800x600 */
154         {0x3808, 0x03},
155         {0x3809, 0x20},
156         {0x380a, 0x02},
157         {0x380b, 0x58},
158
159         /* Set HTS + VTS to 1708x644 */
160         {0x380c, 0x06},
161         {0x380d, 0xac},
162         {0x380e, 0x02},
163         {0x380f, 0x84},
164
165         /* Set ISP WIN X and Y start to 4x4 */
166         {0x3811, 0x04},
167         {0x3813, 0x04},
168
169         /* Set X INC and Y INC for binning */
170         {0x3814, 0x31},
171         {0x3815, 0x31},
172
173         /* Initialize FORMAT1 to default/reset value (vflip disabled) */
174         {0x3820, 0xc0},
175
176         /* Set black level compensation range to 0 - 3 (default 0 - 11) */
177         {0x4008, 0x00},
178         {0x4009, 0x03},
179
180         /* Set MIPI pclk period to 0x1e (default/reset is 0x18) */
181         {0x4837, 0x1e},
182
183         /* Initialize exposure to 0x4ee (overridden by the ctrl, drop this */
184         {0x3501, 0x4e},
185         {0x3502, 0xe0},
186
187         /* R MANUAL set exposure and gain to manual (hw does not do auto) */
188         {0x3503, 0x03},
189 };
190
191 static const struct reg_sequence ov2680_setting_30fps_720P_1280_720[] = {
192         /* Set PLL SP DIV to 0 for not binning mode */
193         {0x3086, 0x00},
194
195         /* Set X and Y output size to 1280x720 */
196         {0x3808, 0x05},
197         {0x3809, 0x00},
198         {0x380a, 0x02},
199         {0x380b, 0xd0},
200
201         /* Set HTS + VTS to 1704x1294 */
202         {0x380c, 0x06},
203         {0x380d, 0xa8},
204         {0x380e, 0x05},
205         {0x380f, 0x0e},
206
207         /* Set ISP WIN X and Y start to 8x6 */
208         {0x3811, 0x08},
209         {0x3813, 0x06},
210
211         /* Set X INC and Y INC for non binning */
212         {0x3814, 0x11},
213         {0x3815, 0x11},
214
215         /* Initialize FORMAT1 to default/reset value (vflip disabled) */
216         {0x3820, 0xc0},
217
218         /* Set backlight compensation range start to 0 */
219         {0x4008, 0x00},
220 };
221
222 static const struct reg_sequence ov2680_setting_30fps_UXGA_1600_1200[] = {
223         /* Set PLL SP DIV to 0 for not binning mode */
224         {0x3086, 0x00},
225
226         /* Initialize exposure to 0x4ee (overridden by the ctrl, drop this */
227         {0x3501, 0x4e},
228         {0x3502, 0xe0},
229
230         /* Set X and Y output size to 1600x1200 */
231         {0x3808, 0x06},
232         {0x3809, 0x40},
233         {0x380a, 0x04},
234         {0x380b, 0xb0},
235
236         /* Set HTS + VTS to 1704x1294 */
237         {0x380c, 0x06},
238         {0x380d, 0xa8},
239         {0x380e, 0x05},
240         {0x380f, 0x0e},
241
242         /* Set ISP WIN X and Y start to 0x0 */
243         {0x3811, 0x00},
244         {0x3813, 0x00},
245
246         /* Set X INC and Y INC for non binning */
247         {0x3814, 0x11},
248         {0x3815, 0x11},
249
250         /* Initialize FORMAT1 to default/reset value (vflip disabled) */
251         {0x3820, 0xc0},
252
253         /* Set backlight compensation range start to 0 */
254         {0x4008, 0x00},
255
256         /* Set MIPI pclk period to default/reset value of 0x18 */
257         {0x4837, 0x18}
258 };
259
260 static const struct ov2680_mode_info ov2680_mode_init_data = {
261         "mode_quxga_800_600", OV2680_MODE_QUXGA_800_600, 800, 600,
262         ov2680_setting_30fps_QUXGA_800_600,
263         ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600),
264 };
265
266 static const struct ov2680_mode_info ov2680_mode_data[OV2680_MODE_MAX] = {
267         {"mode_quxga_800_600", OV2680_MODE_QUXGA_800_600,
268          800, 600, ov2680_setting_30fps_QUXGA_800_600,
269          ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600)},
270         {"mode_720p_1280_720", OV2680_MODE_720P_1280_720,
271          1280, 720, ov2680_setting_30fps_720P_1280_720,
272          ARRAY_SIZE(ov2680_setting_30fps_720P_1280_720)},
273         {"mode_uxga_1600_1200", OV2680_MODE_UXGA_1600_1200,
274          1600, 1200, ov2680_setting_30fps_UXGA_1600_1200,
275          ARRAY_SIZE(ov2680_setting_30fps_UXGA_1600_1200)},
276 };
277
278 static struct ov2680_dev *to_ov2680_dev(struct v4l2_subdev *sd)
279 {
280         return container_of(sd, struct ov2680_dev, sd);
281 }
282
283 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
284 {
285         return &container_of(ctrl->handler, struct ov2680_dev,
286                              ctrls.handler)->sd;
287 }
288
289 static void ov2680_power_up(struct ov2680_dev *sensor)
290 {
291         if (!sensor->pwdn_gpio)
292                 return;
293
294         gpiod_set_value(sensor->pwdn_gpio, 0);
295         usleep_range(5000, 10000);
296 }
297
298 static void ov2680_power_down(struct ov2680_dev *sensor)
299 {
300         if (!sensor->pwdn_gpio)
301                 return;
302
303         gpiod_set_value(sensor->pwdn_gpio, 1);
304         usleep_range(5000, 10000);
305 }
306
307 static void ov2680_set_bayer_order(struct ov2680_dev *sensor,
308                                    struct v4l2_mbus_framefmt *fmt)
309 {
310         int hv_flip = 0;
311
312         if (sensor->ctrls.vflip && sensor->ctrls.vflip->val)
313                 hv_flip += 1;
314
315         if (sensor->ctrls.hflip && sensor->ctrls.hflip->val)
316                 hv_flip += 2;
317
318         fmt->code = ov2680_hv_flip_bayer_order[hv_flip];
319 }
320
321 static void ov2680_fill_format(struct ov2680_dev *sensor,
322                                struct v4l2_mbus_framefmt *fmt,
323                                unsigned int width, unsigned int height)
324 {
325         memset(fmt, 0, sizeof(*fmt));
326         fmt->width = width;
327         fmt->height = height;
328         fmt->field = V4L2_FIELD_NONE;
329         fmt->colorspace = V4L2_COLORSPACE_SRGB;
330         ov2680_set_bayer_order(sensor, fmt);
331 }
332
333 static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val)
334 {
335         int ret;
336
337         if (sensor->is_streaming)
338                 return -EBUSY;
339
340         ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT1,
341                               BIT(2), val ? BIT(2) : 0, NULL);
342         if (ret < 0)
343                 return ret;
344
345         ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
346         return 0;
347 }
348
349 static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val)
350 {
351         int ret;
352
353         if (sensor->is_streaming)
354                 return -EBUSY;
355
356         ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT2,
357                               BIT(2), val ? BIT(2) : 0, NULL);
358         if (ret < 0)
359                 return ret;
360
361         ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
362         return 0;
363 }
364
365 static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value)
366 {
367         int ret = 0;
368
369         if (!value)
370                 return cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
371                                        BIT(7), 0, NULL);
372
373         cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
374                         0x03, value - 1, &ret);
375         cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
376                         BIT(7), BIT(7), &ret);
377
378         return ret;
379 }
380
381 static int ov2680_gain_set(struct ov2680_dev *sensor, u32 gain)
382 {
383         return cci_write(sensor->regmap, OV2680_REG_GAIN_PK, gain, NULL);
384 }
385
386 static int ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp)
387 {
388         return cci_write(sensor->regmap, OV2680_REG_EXPOSURE_PK, exp << 4,
389                          NULL);
390 }
391
392 static int ov2680_stream_enable(struct ov2680_dev *sensor)
393 {
394         int ret;
395
396         ret = cci_write(sensor->regmap, OV2680_REG_PLL_MULTIPLIER,
397                         sensor->pll_mult, NULL);
398         if (ret < 0)
399                 return ret;
400
401         ret = regmap_multi_reg_write(sensor->regmap,
402                                      ov2680_mode_init_data.reg_data,
403                                      ov2680_mode_init_data.reg_data_size);
404         if (ret < 0)
405                 return ret;
406
407         ret = regmap_multi_reg_write(sensor->regmap,
408                                      sensor->current_mode->reg_data,
409                                      sensor->current_mode->reg_data_size);
410         if (ret < 0)
411                 return ret;
412
413         /* Restore value of all ctrls */
414         ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
415         if (ret < 0)
416                 return ret;
417
418         return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 1, NULL);
419 }
420
421 static int ov2680_stream_disable(struct ov2680_dev *sensor)
422 {
423         return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 0, NULL);
424 }
425
426 static int ov2680_power_off(struct ov2680_dev *sensor)
427 {
428         clk_disable_unprepare(sensor->xvclk);
429         ov2680_power_down(sensor);
430         regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
431         return 0;
432 }
433
434 static int ov2680_power_on(struct ov2680_dev *sensor)
435 {
436         int ret;
437
438         ret = regulator_bulk_enable(OV2680_NUM_SUPPLIES, sensor->supplies);
439         if (ret < 0) {
440                 dev_err(sensor->dev, "failed to enable regulators: %d\n", ret);
441                 return ret;
442         }
443
444         if (!sensor->pwdn_gpio) {
445                 ret = cci_write(sensor->regmap, OV2680_REG_SOFT_RESET, 0x01,
446                                 NULL);
447                 if (ret != 0) {
448                         dev_err(sensor->dev, "sensor soft reset failed\n");
449                         goto err_disable_regulators;
450                 }
451                 usleep_range(1000, 2000);
452         } else {
453                 ov2680_power_down(sensor);
454                 ov2680_power_up(sensor);
455         }
456
457         ret = clk_prepare_enable(sensor->xvclk);
458         if (ret < 0)
459                 goto err_disable_regulators;
460
461         return 0;
462
463 err_disable_regulators:
464         regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
465         return ret;
466 }
467
468 static int ov2680_s_g_frame_interval(struct v4l2_subdev *sd,
469                                      struct v4l2_subdev_frame_interval *fi)
470 {
471         struct ov2680_dev *sensor = to_ov2680_dev(sd);
472
473         mutex_lock(&sensor->lock);
474         fi->interval = sensor->mode.frame_interval;
475         mutex_unlock(&sensor->lock);
476
477         return 0;
478 }
479
480 static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
481 {
482         struct ov2680_dev *sensor = to_ov2680_dev(sd);
483         int ret = 0;
484
485         mutex_lock(&sensor->lock);
486
487         if (sensor->is_streaming == !!enable)
488                 goto unlock;
489
490         if (enable) {
491                 ret = pm_runtime_resume_and_get(sensor->sd.dev);
492                 if (ret < 0)
493                         goto unlock;
494
495                 ret = ov2680_stream_enable(sensor);
496                 if (ret < 0) {
497                         pm_runtime_put(sensor->sd.dev);
498                         goto unlock;
499                 }
500         } else {
501                 ret = ov2680_stream_disable(sensor);
502                 pm_runtime_put(sensor->sd.dev);
503         }
504
505         sensor->is_streaming = !!enable;
506
507 unlock:
508         mutex_unlock(&sensor->lock);
509
510         return ret;
511 }
512
513 static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
514                                  struct v4l2_subdev_state *sd_state,
515                                  struct v4l2_subdev_mbus_code_enum *code)
516 {
517         struct ov2680_dev *sensor = to_ov2680_dev(sd);
518
519         if (code->pad != 0 || code->index != 0)
520                 return -EINVAL;
521
522         code->code = sensor->mode.fmt.code;
523
524         return 0;
525 }
526
527 static int ov2680_get_fmt(struct v4l2_subdev *sd,
528                           struct v4l2_subdev_state *sd_state,
529                           struct v4l2_subdev_format *format)
530 {
531         struct ov2680_dev *sensor = to_ov2680_dev(sd);
532         struct v4l2_mbus_framefmt *fmt = NULL;
533
534         if (format->pad != 0)
535                 return -EINVAL;
536
537         mutex_lock(&sensor->lock);
538
539         if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
540                 fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state,
541                                                  format->pad);
542         } else {
543                 fmt = &sensor->mode.fmt;
544         }
545
546         format->format = *fmt;
547
548         mutex_unlock(&sensor->lock);
549
550         return 0;
551 }
552
553 static int ov2680_set_fmt(struct v4l2_subdev *sd,
554                           struct v4l2_subdev_state *sd_state,
555                           struct v4l2_subdev_format *format)
556 {
557         struct ov2680_dev *sensor = to_ov2680_dev(sd);
558         struct v4l2_mbus_framefmt *try_fmt;
559         const struct ov2680_mode_info *mode;
560         int ret = 0;
561
562         if (format->pad != 0)
563                 return -EINVAL;
564
565         mode = v4l2_find_nearest_size(ov2680_mode_data,
566                                       ARRAY_SIZE(ov2680_mode_data),
567                                       width, height,
568                                       format->format.width,
569                                       format->format.height);
570         if (!mode)
571                 return -EINVAL;
572
573         ov2680_fill_format(sensor, &format->format, mode->width, mode->height);
574
575         if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
576                 try_fmt = v4l2_subdev_get_try_format(sd, sd_state, 0);
577                 *try_fmt = format->format;
578                 return 0;
579         }
580
581         mutex_lock(&sensor->lock);
582
583         if (sensor->is_streaming) {
584                 ret = -EBUSY;
585                 goto unlock;
586         }
587
588         sensor->current_mode = mode;
589         sensor->mode.fmt = format->format;
590
591 unlock:
592         mutex_unlock(&sensor->lock);
593
594         return ret;
595 }
596
597 static int ov2680_init_cfg(struct v4l2_subdev *sd,
598                            struct v4l2_subdev_state *sd_state)
599 {
600         struct ov2680_dev *sensor = to_ov2680_dev(sd);
601
602         ov2680_fill_format(sensor, &sd_state->pads[0].try_fmt,
603                            OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
604         return 0;
605 }
606
607 static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
608                                   struct v4l2_subdev_state *sd_state,
609                                   struct v4l2_subdev_frame_size_enum *fse)
610 {
611         int index = fse->index;
612
613         if (index >= OV2680_MODE_MAX || index < 0)
614                 return -EINVAL;
615
616         fse->min_width = ov2680_mode_data[index].width;
617         fse->min_height = ov2680_mode_data[index].height;
618         fse->max_width = ov2680_mode_data[index].width;
619         fse->max_height = ov2680_mode_data[index].height;
620
621         return 0;
622 }
623
624 static bool ov2680_valid_frame_size(struct v4l2_subdev_frame_interval_enum *fie)
625 {
626         int i;
627
628         for (i = 0; i < OV2680_MODE_MAX; i++) {
629                 if (fie->width == ov2680_mode_data[i].width &&
630                     fie->height == ov2680_mode_data[i].height)
631                         return true;
632         }
633
634         return false;
635 }
636
637 static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
638                               struct v4l2_subdev_state *sd_state,
639                               struct v4l2_subdev_frame_interval_enum *fie)
640 {
641         struct ov2680_dev *sensor = to_ov2680_dev(sd);
642
643         /* Only 1 framerate */
644         if (fie->index || !ov2680_valid_frame_size(fie))
645                 return -EINVAL;
646
647         fie->interval = sensor->mode.frame_interval;
648
649         return 0;
650 }
651
652 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
653 {
654         struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
655         struct ov2680_dev *sensor = to_ov2680_dev(sd);
656         int ret;
657
658         /* Only apply changes to the controls if the device is powered up */
659         if (!pm_runtime_get_if_in_use(sensor->sd.dev)) {
660                 ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
661                 return 0;
662         }
663
664         switch (ctrl->id) {
665         case V4L2_CID_GAIN:
666                 ret = ov2680_gain_set(sensor, ctrl->val);
667                 break;
668         case V4L2_CID_EXPOSURE:
669                 ret = ov2680_exposure_set(sensor, ctrl->val);
670                 break;
671         case V4L2_CID_VFLIP:
672                 ret = ov2680_set_vflip(sensor, ctrl->val);
673                 break;
674         case V4L2_CID_HFLIP:
675                 ret = ov2680_set_hflip(sensor, ctrl->val);
676                 break;
677         case V4L2_CID_TEST_PATTERN:
678                 ret = ov2680_test_pattern_set(sensor, ctrl->val);
679                 break;
680         default:
681                 ret = -EINVAL;
682                 break;
683         }
684
685         pm_runtime_put(sensor->sd.dev);
686         return ret;
687 }
688
689 static const struct v4l2_ctrl_ops ov2680_ctrl_ops = {
690         .s_ctrl = ov2680_s_ctrl,
691 };
692
693 static const struct v4l2_subdev_video_ops ov2680_video_ops = {
694         .g_frame_interval       = ov2680_s_g_frame_interval,
695         .s_frame_interval       = ov2680_s_g_frame_interval,
696         .s_stream               = ov2680_s_stream,
697 };
698
699 static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
700         .init_cfg               = ov2680_init_cfg,
701         .enum_mbus_code         = ov2680_enum_mbus_code,
702         .get_fmt                = ov2680_get_fmt,
703         .set_fmt                = ov2680_set_fmt,
704         .enum_frame_size        = ov2680_enum_frame_size,
705         .enum_frame_interval    = ov2680_enum_frame_interval,
706 };
707
708 static const struct v4l2_subdev_ops ov2680_subdev_ops = {
709         .video  = &ov2680_video_ops,
710         .pad    = &ov2680_pad_ops,
711 };
712
713 static int ov2680_mode_init(struct ov2680_dev *sensor)
714 {
715         const struct ov2680_mode_info *init_mode;
716
717         /* set initial mode */
718         ov2680_fill_format(sensor, &sensor->mode.fmt,
719                            OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
720
721         sensor->mode.frame_interval.denominator = OV2680_FRAME_RATE;
722         sensor->mode.frame_interval.numerator = 1;
723
724         init_mode = &ov2680_mode_init_data;
725
726         sensor->current_mode = init_mode;
727
728         return 0;
729 }
730
731 static int ov2680_v4l2_register(struct ov2680_dev *sensor)
732 {
733         struct i2c_client *client = to_i2c_client(sensor->dev);
734         const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops;
735         struct ov2680_ctrls *ctrls = &sensor->ctrls;
736         struct v4l2_ctrl_handler *hdl = &ctrls->handler;
737         int ret = 0;
738
739         v4l2_i2c_subdev_init(&sensor->sd, client, &ov2680_subdev_ops);
740
741         sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
742         sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
743         sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
744
745         ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
746         if (ret < 0)
747                 return ret;
748
749         v4l2_ctrl_handler_init(hdl, 5);
750
751         hdl->lock = &sensor->lock;
752
753         ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
754         ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
755
756         ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(hdl,
757                                         &ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN,
758                                         ARRAY_SIZE(test_pattern_menu) - 1,
759                                         0, 0, test_pattern_menu);
760
761         ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
762                                             0, 32767, 1, 0);
763
764         ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 2047, 1, 0);
765
766         if (hdl->error) {
767                 ret = hdl->error;
768                 goto cleanup_entity;
769         }
770
771         ctrls->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
772         ctrls->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
773
774         sensor->sd.ctrl_handler = hdl;
775
776         ret = v4l2_async_register_subdev(&sensor->sd);
777         if (ret < 0)
778                 goto cleanup_entity;
779
780         return 0;
781
782 cleanup_entity:
783         media_entity_cleanup(&sensor->sd.entity);
784         v4l2_ctrl_handler_free(hdl);
785
786         return ret;
787 }
788
789 static int ov2680_get_regulators(struct ov2680_dev *sensor)
790 {
791         int i;
792
793         for (i = 0; i < OV2680_NUM_SUPPLIES; i++)
794                 sensor->supplies[i].supply = ov2680_supply_name[i];
795
796         return devm_regulator_bulk_get(sensor->dev,
797                                        OV2680_NUM_SUPPLIES, sensor->supplies);
798 }
799
800 static int ov2680_check_id(struct ov2680_dev *sensor)
801 {
802         u64 chip_id;
803         int ret;
804
805         ret = cci_read(sensor->regmap, OV2680_REG_CHIP_ID, &chip_id, NULL);
806         if (ret < 0) {
807                 dev_err(sensor->dev, "failed to read chip id\n");
808                 return -ENODEV;
809         }
810
811         if (chip_id != OV2680_CHIP_ID) {
812                 dev_err(sensor->dev, "chip id: 0x%04llx does not match expected 0x%04x\n",
813                         chip_id, OV2680_CHIP_ID);
814                 return -ENODEV;
815         }
816
817         return 0;
818 }
819
820 static int ov2680_parse_dt(struct ov2680_dev *sensor)
821 {
822         struct device *dev = sensor->dev;
823         struct fwnode_handle *ep_fwnode;
824         struct gpio_desc *gpio;
825         unsigned int rate = 0;
826         int i, ret;
827
828         /*
829          * Sometimes the fwnode graph is initialized by the bridge driver.
830          * Bridge drivers doing this may also add GPIO mappings, wait for this.
831          */
832         ep_fwnode = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
833         if (!ep_fwnode)
834                 return dev_err_probe(dev, -EPROBE_DEFER,
835                                      "waiting for fwnode graph endpoint\n");
836
837         fwnode_handle_put(ep_fwnode);
838
839         /*
840          * The pin we want is named XSHUTDN in the datasheet. Linux sensor
841          * drivers have standardized on using "powerdown" as con-id name
842          * for powerdown or shutdown pins. Older DTB files use "reset",
843          * so fallback to that if there is no "powerdown" pin.
844          */
845         gpio = devm_gpiod_get_optional(dev, "powerdown", GPIOD_OUT_HIGH);
846         if (!gpio)
847                 gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
848
849         ret = PTR_ERR_OR_ZERO(gpio);
850         if (ret < 0) {
851                 dev_dbg(dev, "error while getting reset gpio: %d\n", ret);
852                 return ret;
853         }
854
855         sensor->pwdn_gpio = gpio;
856
857         sensor->xvclk = devm_clk_get_optional(dev, "xvclk");
858         if (IS_ERR(sensor->xvclk)) {
859                 dev_err(dev, "xvclk clock missing or invalid\n");
860                 return PTR_ERR(sensor->xvclk);
861         }
862
863         /*
864          * We could have either a 24MHz or 19.2MHz clock rate from either DT or
865          * ACPI... but we also need to support the weird IPU3 case which will
866          * have an external clock AND a clock-frequency property. Check for the
867          * clock-frequency property and if found, set that rate if we managed
868          * to acquire a clock. This should cover the ACPI case. If the system
869          * uses devicetree then the configured rate should already be set, so
870          * we can just read it.
871          */
872         ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency",
873                                        &rate);
874         if (ret && !sensor->xvclk)
875                 return dev_err_probe(dev, ret, "invalid clock config\n");
876
877         if (!ret && sensor->xvclk) {
878                 ret = clk_set_rate(sensor->xvclk, rate);
879                 if (ret)
880                         return dev_err_probe(dev, ret,
881                                              "failed to set clock rate\n");
882         }
883
884         sensor->xvclk_freq = rate ?: clk_get_rate(sensor->xvclk);
885
886         for (i = 0; i < ARRAY_SIZE(ov2680_xvclk_freqs); i++) {
887                 if (sensor->xvclk_freq == ov2680_xvclk_freqs[i])
888                         break;
889         }
890
891         if (i == ARRAY_SIZE(ov2680_xvclk_freqs))
892                 return dev_err_probe(dev, -EINVAL,
893                                      "unsupported xvclk frequency %d Hz\n",
894                                      sensor->xvclk_freq);
895
896         sensor->pll_mult = ov2680_pll_multipliers[i];
897
898         return 0;
899 }
900
901 static int ov2680_probe(struct i2c_client *client)
902 {
903         struct device *dev = &client->dev;
904         struct ov2680_dev *sensor;
905         int ret;
906
907         sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
908         if (!sensor)
909                 return -ENOMEM;
910
911         sensor->dev = &client->dev;
912
913         sensor->regmap = devm_cci_regmap_init_i2c(client, 16);
914         if (IS_ERR(sensor->regmap))
915                 return PTR_ERR(sensor->regmap);
916
917         ret = ov2680_parse_dt(sensor);
918         if (ret < 0)
919                 return ret;
920
921         ret = ov2680_mode_init(sensor);
922         if (ret < 0)
923                 return ret;
924
925         ret = ov2680_get_regulators(sensor);
926         if (ret < 0) {
927                 dev_err(dev, "failed to get regulators\n");
928                 return ret;
929         }
930
931         mutex_init(&sensor->lock);
932
933         /*
934          * Power up and verify the chip now, so that if runtime pm is
935          * disabled the chip is left on and streaming will work.
936          */
937         ret = ov2680_power_on(sensor);
938         if (ret < 0)
939                 goto lock_destroy;
940
941         ret = ov2680_check_id(sensor);
942         if (ret < 0)
943                 goto err_powerdown;
944
945         pm_runtime_set_active(&client->dev);
946         pm_runtime_get_noresume(&client->dev);
947         pm_runtime_enable(&client->dev);
948
949         ret = ov2680_v4l2_register(sensor);
950         if (ret < 0)
951                 goto err_pm_runtime;
952
953         pm_runtime_set_autosuspend_delay(&client->dev, 1000);
954         pm_runtime_use_autosuspend(&client->dev);
955         pm_runtime_put_autosuspend(&client->dev);
956
957         dev_info(dev, "ov2680 init correctly\n");
958
959         return 0;
960
961 err_pm_runtime:
962         pm_runtime_disable(&client->dev);
963         pm_runtime_put_noidle(&client->dev);
964 err_powerdown:
965         ov2680_power_off(sensor);
966 lock_destroy:
967         dev_err(dev, "ov2680 init fail: %d\n", ret);
968         mutex_destroy(&sensor->lock);
969
970         return ret;
971 }
972
973 static void ov2680_remove(struct i2c_client *client)
974 {
975         struct v4l2_subdev *sd = i2c_get_clientdata(client);
976         struct ov2680_dev *sensor = to_ov2680_dev(sd);
977
978         v4l2_async_unregister_subdev(&sensor->sd);
979         mutex_destroy(&sensor->lock);
980         media_entity_cleanup(&sensor->sd.entity);
981         v4l2_ctrl_handler_free(&sensor->ctrls.handler);
982
983         /*
984          * Disable runtime PM. In case runtime PM is disabled in the kernel,
985          * make sure to turn power off manually.
986          */
987         pm_runtime_disable(&client->dev);
988         if (!pm_runtime_status_suspended(&client->dev))
989                 ov2680_power_off(sensor);
990         pm_runtime_set_suspended(&client->dev);
991 }
992
993 static int ov2680_suspend(struct device *dev)
994 {
995         struct v4l2_subdev *sd = dev_get_drvdata(dev);
996         struct ov2680_dev *sensor = to_ov2680_dev(sd);
997
998         if (sensor->is_streaming)
999                 ov2680_stream_disable(sensor);
1000
1001         return ov2680_power_off(sensor);
1002 }
1003
1004 static int ov2680_resume(struct device *dev)
1005 {
1006         struct v4l2_subdev *sd = dev_get_drvdata(dev);
1007         struct ov2680_dev *sensor = to_ov2680_dev(sd);
1008         int ret;
1009
1010         ret = ov2680_power_on(sensor);
1011         if (ret < 0)
1012                 goto stream_disable;
1013
1014         if (sensor->is_streaming) {
1015                 ret = ov2680_stream_enable(sensor);
1016                 if (ret < 0)
1017                         goto stream_disable;
1018         }
1019
1020         return 0;
1021
1022 stream_disable:
1023         ov2680_stream_disable(sensor);
1024         sensor->is_streaming = false;
1025
1026         return ret;
1027 }
1028
1029 static DEFINE_RUNTIME_DEV_PM_OPS(ov2680_pm_ops, ov2680_suspend, ov2680_resume,
1030                                  NULL);
1031
1032 static const struct of_device_id ov2680_dt_ids[] = {
1033         { .compatible = "ovti,ov2680" },
1034         { /* sentinel */ },
1035 };
1036 MODULE_DEVICE_TABLE(of, ov2680_dt_ids);
1037
1038 static const struct acpi_device_id ov2680_acpi_ids[] = {
1039         { "OVTI2680" },
1040         { /* sentinel */ }
1041 };
1042 MODULE_DEVICE_TABLE(acpi, ov2680_acpi_ids);
1043
1044 static struct i2c_driver ov2680_i2c_driver = {
1045         .driver = {
1046                 .name  = "ov2680",
1047                 .pm = pm_sleep_ptr(&ov2680_pm_ops),
1048                 .of_match_table = ov2680_dt_ids,
1049                 .acpi_match_table = ov2680_acpi_ids,
1050         },
1051         .probe          = ov2680_probe,
1052         .remove         = ov2680_remove,
1053 };
1054 module_i2c_driver(ov2680_i2c_driver);
1055
1056 MODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>");
1057 MODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver");
1058 MODULE_LICENSE("GPL v2");