bc0ca2927370bdb0cacbbc78cd03c1c8099dc4fe
[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_XVCLK_VALUE                      24000000
31
32 #define OV2680_CHIP_ID                          0x2680
33
34 #define OV2680_REG_STREAM_CTRL                  CCI_REG8(0x0100)
35 #define OV2680_REG_SOFT_RESET                   CCI_REG8(0x0103)
36
37 #define OV2680_REG_CHIP_ID                      CCI_REG16(0x300a)
38
39 #define OV2680_REG_EXPOSURE_PK                  CCI_REG24(0x3500)
40 #define OV2680_REG_R_MANUAL                     CCI_REG8(0x3503)
41 #define OV2680_REG_GAIN_PK                      CCI_REG16(0x350a)
42 #define OV2680_REG_TIMING_HTS                   CCI_REG16(0x380c)
43 #define OV2680_REG_TIMING_VTS                   CCI_REG16(0x380e)
44 #define OV2680_REG_FORMAT1                      CCI_REG8(0x3820)
45 #define OV2680_REG_FORMAT2                      CCI_REG8(0x3821)
46
47 #define OV2680_REG_ISP_CTRL00                   CCI_REG8(0x5080)
48
49 #define OV2680_FRAME_RATE                       30
50
51 #define OV2680_WIDTH_MAX                        1600
52 #define OV2680_HEIGHT_MAX                       1200
53
54 #define OV2680_DEFAULT_WIDTH                    800
55 #define OV2680_DEFAULT_HEIGHT                   600
56
57 enum ov2680_mode_id {
58         OV2680_MODE_QUXGA_800_600,
59         OV2680_MODE_720P_1280_720,
60         OV2680_MODE_UXGA_1600_1200,
61         OV2680_MODE_MAX,
62 };
63
64 static const char * const ov2680_supply_name[] = {
65         "DOVDD",
66         "DVDD",
67         "AVDD",
68 };
69
70 #define OV2680_NUM_SUPPLIES ARRAY_SIZE(ov2680_supply_name)
71
72 struct ov2680_mode_info {
73         const char *name;
74         enum ov2680_mode_id id;
75         u32 width;
76         u32 height;
77         const struct reg_sequence *reg_data;
78         u32 reg_data_size;
79 };
80
81 struct ov2680_ctrls {
82         struct v4l2_ctrl_handler handler;
83         struct v4l2_ctrl *exposure;
84         struct v4l2_ctrl *gain;
85         struct v4l2_ctrl *hflip;
86         struct v4l2_ctrl *vflip;
87         struct v4l2_ctrl *test_pattern;
88 };
89
90 struct ov2680_dev {
91         struct device                   *dev;
92         struct regmap                   *regmap;
93         struct v4l2_subdev              sd;
94
95         struct media_pad                pad;
96         struct clk                      *xvclk;
97         u32                             xvclk_freq;
98         struct regulator_bulk_data      supplies[OV2680_NUM_SUPPLIES];
99
100         struct gpio_desc                *reset_gpio;
101         struct mutex                    lock; /* protect members */
102
103         bool                            is_enabled;
104         bool                            is_streaming;
105
106         struct ov2680_ctrls             ctrls;
107         struct v4l2_mbus_framefmt       fmt;
108         struct v4l2_fract               frame_interval;
109
110         const struct ov2680_mode_info   *current_mode;
111 };
112
113 static const char * const test_pattern_menu[] = {
114         "Disabled",
115         "Color Bars",
116         "Random Data",
117         "Square",
118         "Black Image",
119 };
120
121 static const int ov2680_hv_flip_bayer_order[] = {
122         MEDIA_BUS_FMT_SBGGR10_1X10,
123         MEDIA_BUS_FMT_SGRBG10_1X10,
124         MEDIA_BUS_FMT_SGBRG10_1X10,
125         MEDIA_BUS_FMT_SRGGB10_1X10,
126 };
127
128 static const struct reg_sequence ov2680_setting_30fps_QUXGA_800_600[] = {
129         {0x3086, 0x01}, {0x370a, 0x23}, {0x3808, 0x03}, {0x3809, 0x20},
130         {0x380a, 0x02}, {0x380b, 0x58}, {0x380c, 0x06}, {0x380d, 0xac},
131         {0x380e, 0x02}, {0x380f, 0x84}, {0x3811, 0x04}, {0x3813, 0x04},
132         {0x3814, 0x31}, {0x3815, 0x31}, {0x3820, 0xc0}, {0x4008, 0x00},
133         {0x4009, 0x03}, {0x4837, 0x1e}, {0x3501, 0x4e}, {0x3502, 0xe0},
134         {0x3503, 0x03},
135 };
136
137 static const struct reg_sequence ov2680_setting_30fps_720P_1280_720[] = {
138         {0x3086, 0x00}, {0x3808, 0x05}, {0x3809, 0x00}, {0x380a, 0x02},
139         {0x380b, 0xd0}, {0x380c, 0x06}, {0x380d, 0xa8}, {0x380e, 0x05},
140         {0x380f, 0x0e}, {0x3811, 0x08}, {0x3813, 0x06}, {0x3814, 0x11},
141         {0x3815, 0x11}, {0x3820, 0xc0}, {0x4008, 0x00},
142 };
143
144 static const struct reg_sequence ov2680_setting_30fps_UXGA_1600_1200[] = {
145         {0x3086, 0x00}, {0x3501, 0x4e}, {0x3502, 0xe0}, {0x3808, 0x06},
146         {0x3809, 0x40}, {0x380a, 0x04}, {0x380b, 0xb0}, {0x380c, 0x06},
147         {0x380d, 0xa8}, {0x380e, 0x05}, {0x380f, 0x0e}, {0x3811, 0x00},
148         {0x3813, 0x00}, {0x3814, 0x11}, {0x3815, 0x11}, {0x3820, 0xc0},
149         {0x4008, 0x00}, {0x4837, 0x18}
150 };
151
152 static const struct ov2680_mode_info ov2680_mode_init_data = {
153         "mode_quxga_800_600", OV2680_MODE_QUXGA_800_600, 800, 600,
154         ov2680_setting_30fps_QUXGA_800_600,
155         ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600),
156 };
157
158 static const struct ov2680_mode_info ov2680_mode_data[OV2680_MODE_MAX] = {
159         {"mode_quxga_800_600", OV2680_MODE_QUXGA_800_600,
160          800, 600, ov2680_setting_30fps_QUXGA_800_600,
161          ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600)},
162         {"mode_720p_1280_720", OV2680_MODE_720P_1280_720,
163          1280, 720, ov2680_setting_30fps_720P_1280_720,
164          ARRAY_SIZE(ov2680_setting_30fps_720P_1280_720)},
165         {"mode_uxga_1600_1200", OV2680_MODE_UXGA_1600_1200,
166          1600, 1200, ov2680_setting_30fps_UXGA_1600_1200,
167          ARRAY_SIZE(ov2680_setting_30fps_UXGA_1600_1200)},
168 };
169
170 static struct ov2680_dev *to_ov2680_dev(struct v4l2_subdev *sd)
171 {
172         return container_of(sd, struct ov2680_dev, sd);
173 }
174
175 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
176 {
177         return &container_of(ctrl->handler, struct ov2680_dev,
178                              ctrls.handler)->sd;
179 }
180
181 static void ov2680_power_up(struct ov2680_dev *sensor)
182 {
183         if (!sensor->reset_gpio)
184                 return;
185
186         gpiod_set_value(sensor->reset_gpio, 0);
187         usleep_range(5000, 10000);
188 }
189
190 static void ov2680_power_down(struct ov2680_dev *sensor)
191 {
192         if (!sensor->reset_gpio)
193                 return;
194
195         gpiod_set_value(sensor->reset_gpio, 1);
196         usleep_range(5000, 10000);
197 }
198
199 static void ov2680_set_bayer_order(struct ov2680_dev *sensor,
200                                    struct v4l2_mbus_framefmt *fmt)
201 {
202         int hv_flip = 0;
203
204         if (sensor->ctrls.vflip && sensor->ctrls.vflip->val)
205                 hv_flip += 1;
206
207         if (sensor->ctrls.hflip && sensor->ctrls.hflip->val)
208                 hv_flip += 2;
209
210         fmt->code = ov2680_hv_flip_bayer_order[hv_flip];
211 }
212
213 static void ov2680_fill_format(struct ov2680_dev *sensor,
214                                struct v4l2_mbus_framefmt *fmt,
215                                unsigned int width, unsigned int height)
216 {
217         memset(fmt, 0, sizeof(*fmt));
218         fmt->width = width;
219         fmt->height = height;
220         fmt->field = V4L2_FIELD_NONE;
221         fmt->colorspace = V4L2_COLORSPACE_SRGB;
222         ov2680_set_bayer_order(sensor, fmt);
223 }
224
225 static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val)
226 {
227         int ret;
228
229         if (sensor->is_streaming)
230                 return -EBUSY;
231
232         ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT1,
233                               BIT(2), val ? BIT(2) : 0, NULL);
234         if (ret < 0)
235                 return ret;
236
237         ov2680_set_bayer_order(sensor, &sensor->fmt);
238         return 0;
239 }
240
241 static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val)
242 {
243         int ret;
244
245         if (sensor->is_streaming)
246                 return -EBUSY;
247
248         ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT2,
249                               BIT(2), val ? BIT(2) : 0, NULL);
250         if (ret < 0)
251                 return ret;
252
253         ov2680_set_bayer_order(sensor, &sensor->fmt);
254         return 0;
255 }
256
257 static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value)
258 {
259         int ret = 0;
260
261         if (!value)
262                 return cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
263                                        BIT(7), 0, NULL);
264
265         cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
266                         0x03, value - 1, &ret);
267         cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
268                         BIT(7), BIT(7), &ret);
269
270         return ret;
271 }
272
273 static int ov2680_gain_set(struct ov2680_dev *sensor, u32 gain)
274 {
275         return cci_write(sensor->regmap, OV2680_REG_GAIN_PK, gain, NULL);
276 }
277
278 static int ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp)
279 {
280         return cci_write(sensor->regmap, OV2680_REG_EXPOSURE_PK, exp << 4,
281                          NULL);
282 }
283
284 static int ov2680_stream_enable(struct ov2680_dev *sensor)
285 {
286         int ret;
287
288         ret = regmap_multi_reg_write(sensor->regmap,
289                                      ov2680_mode_init_data.reg_data,
290                                      ov2680_mode_init_data.reg_data_size);
291         if (ret < 0)
292                 return ret;
293
294         ret = regmap_multi_reg_write(sensor->regmap,
295                                      sensor->current_mode->reg_data,
296                                      sensor->current_mode->reg_data_size);
297         if (ret < 0)
298                 return ret;
299
300         /* Restore value of all ctrls */
301         ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
302         if (ret < 0)
303                 return ret;
304
305         return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 1, NULL);
306 }
307
308 static int ov2680_stream_disable(struct ov2680_dev *sensor)
309 {
310         return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 0, NULL);
311 }
312
313 static int ov2680_power_off(struct ov2680_dev *sensor)
314 {
315         if (!sensor->is_enabled)
316                 return 0;
317
318         clk_disable_unprepare(sensor->xvclk);
319         ov2680_power_down(sensor);
320         regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
321         sensor->is_enabled = false;
322
323         return 0;
324 }
325
326 static int ov2680_power_on(struct ov2680_dev *sensor)
327 {
328         int ret;
329
330         if (sensor->is_enabled)
331                 return 0;
332
333         ret = regulator_bulk_enable(OV2680_NUM_SUPPLIES, sensor->supplies);
334         if (ret < 0) {
335                 dev_err(sensor->dev, "failed to enable regulators: %d\n", ret);
336                 return ret;
337         }
338
339         if (!sensor->reset_gpio) {
340                 ret = cci_write(sensor->regmap, OV2680_REG_SOFT_RESET, 0x01,
341                                 NULL);
342                 if (ret != 0) {
343                         dev_err(sensor->dev, "sensor soft reset failed\n");
344                         goto err_disable_regulators;
345                 }
346                 usleep_range(1000, 2000);
347         } else {
348                 ov2680_power_down(sensor);
349                 ov2680_power_up(sensor);
350         }
351
352         ret = clk_prepare_enable(sensor->xvclk);
353         if (ret < 0)
354                 goto err_disable_regulators;
355
356         sensor->is_enabled = true;
357
358         return 0;
359
360 err_disable_regulators:
361         regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
362         return ret;
363 }
364
365 static int ov2680_s_g_frame_interval(struct v4l2_subdev *sd,
366                                      struct v4l2_subdev_frame_interval *fi)
367 {
368         struct ov2680_dev *sensor = to_ov2680_dev(sd);
369
370         mutex_lock(&sensor->lock);
371         fi->interval = sensor->frame_interval;
372         mutex_unlock(&sensor->lock);
373
374         return 0;
375 }
376
377 static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
378 {
379         struct ov2680_dev *sensor = to_ov2680_dev(sd);
380         int ret = 0;
381
382         mutex_lock(&sensor->lock);
383
384         if (sensor->is_streaming == !!enable)
385                 goto unlock;
386
387         if (enable) {
388                 ret = pm_runtime_resume_and_get(sensor->sd.dev);
389                 if (ret < 0)
390                         goto unlock;
391
392                 ret = ov2680_stream_enable(sensor);
393                 if (ret < 0) {
394                         pm_runtime_put(sensor->sd.dev);
395                         goto unlock;
396                 }
397         } else {
398                 ret = ov2680_stream_disable(sensor);
399                 pm_runtime_put(sensor->sd.dev);
400         }
401
402         sensor->is_streaming = !!enable;
403
404 unlock:
405         mutex_unlock(&sensor->lock);
406
407         return ret;
408 }
409
410 static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
411                                  struct v4l2_subdev_state *sd_state,
412                                  struct v4l2_subdev_mbus_code_enum *code)
413 {
414         struct ov2680_dev *sensor = to_ov2680_dev(sd);
415
416         if (code->pad != 0 || code->index != 0)
417                 return -EINVAL;
418
419         code->code = sensor->fmt.code;
420
421         return 0;
422 }
423
424 static int ov2680_get_fmt(struct v4l2_subdev *sd,
425                           struct v4l2_subdev_state *sd_state,
426                           struct v4l2_subdev_format *format)
427 {
428         struct ov2680_dev *sensor = to_ov2680_dev(sd);
429         struct v4l2_mbus_framefmt *fmt = NULL;
430
431         if (format->pad != 0)
432                 return -EINVAL;
433
434         mutex_lock(&sensor->lock);
435
436         if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
437                 fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state,
438                                                  format->pad);
439         } else {
440                 fmt = &sensor->fmt;
441         }
442
443         format->format = *fmt;
444
445         mutex_unlock(&sensor->lock);
446
447         return 0;
448 }
449
450 static int ov2680_set_fmt(struct v4l2_subdev *sd,
451                           struct v4l2_subdev_state *sd_state,
452                           struct v4l2_subdev_format *format)
453 {
454         struct ov2680_dev *sensor = to_ov2680_dev(sd);
455         struct v4l2_mbus_framefmt *try_fmt;
456         const struct ov2680_mode_info *mode;
457         int ret = 0;
458
459         if (format->pad != 0)
460                 return -EINVAL;
461
462         mode = v4l2_find_nearest_size(ov2680_mode_data,
463                                       ARRAY_SIZE(ov2680_mode_data),
464                                       width, height,
465                                       format->format.width,
466                                       format->format.height);
467         if (!mode)
468                 return -EINVAL;
469
470         ov2680_fill_format(sensor, &format->format, mode->width, mode->height);
471
472         if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
473                 try_fmt = v4l2_subdev_get_try_format(sd, sd_state, 0);
474                 *try_fmt = format->format;
475                 return 0;
476         }
477
478         mutex_lock(&sensor->lock);
479
480         if (sensor->is_streaming) {
481                 ret = -EBUSY;
482                 goto unlock;
483         }
484
485         sensor->current_mode = mode;
486         sensor->fmt = format->format;
487
488 unlock:
489         mutex_unlock(&sensor->lock);
490
491         return ret;
492 }
493
494 static int ov2680_init_cfg(struct v4l2_subdev *sd,
495                            struct v4l2_subdev_state *sd_state)
496 {
497         struct ov2680_dev *sensor = to_ov2680_dev(sd);
498
499         ov2680_fill_format(sensor, &sd_state->pads[0].try_fmt,
500                            OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
501         return 0;
502 }
503
504 static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
505                                   struct v4l2_subdev_state *sd_state,
506                                   struct v4l2_subdev_frame_size_enum *fse)
507 {
508         int index = fse->index;
509
510         if (index >= OV2680_MODE_MAX || index < 0)
511                 return -EINVAL;
512
513         fse->min_width = ov2680_mode_data[index].width;
514         fse->min_height = ov2680_mode_data[index].height;
515         fse->max_width = ov2680_mode_data[index].width;
516         fse->max_height = ov2680_mode_data[index].height;
517
518         return 0;
519 }
520
521 static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
522                               struct v4l2_subdev_state *sd_state,
523                               struct v4l2_subdev_frame_interval_enum *fie)
524 {
525         struct v4l2_fract tpf;
526
527         if (fie->index >= OV2680_MODE_MAX || fie->width > OV2680_WIDTH_MAX ||
528             fie->height > OV2680_HEIGHT_MAX ||
529             fie->which > V4L2_SUBDEV_FORMAT_ACTIVE)
530                 return -EINVAL;
531
532         tpf.denominator = OV2680_FRAME_RATE;
533         tpf.numerator = 1;
534
535         fie->interval = tpf;
536
537         return 0;
538 }
539
540 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
541 {
542         struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
543         struct ov2680_dev *sensor = to_ov2680_dev(sd);
544
545         if (!sensor->is_enabled)
546                 return 0;
547
548         switch (ctrl->id) {
549         case V4L2_CID_GAIN:
550                 return ov2680_gain_set(sensor, ctrl->val);
551         case V4L2_CID_EXPOSURE:
552                 return ov2680_exposure_set(sensor, ctrl->val);
553         case V4L2_CID_VFLIP:
554                 return ov2680_set_vflip(sensor, ctrl->val);
555         case V4L2_CID_HFLIP:
556                 return ov2680_set_hflip(sensor, ctrl->val);
557         case V4L2_CID_TEST_PATTERN:
558                 return ov2680_test_pattern_set(sensor, ctrl->val);
559         default:
560                 break;
561         }
562
563         return -EINVAL;
564 }
565
566 static const struct v4l2_ctrl_ops ov2680_ctrl_ops = {
567         .s_ctrl = ov2680_s_ctrl,
568 };
569
570 static const struct v4l2_subdev_video_ops ov2680_video_ops = {
571         .g_frame_interval       = ov2680_s_g_frame_interval,
572         .s_frame_interval       = ov2680_s_g_frame_interval,
573         .s_stream               = ov2680_s_stream,
574 };
575
576 static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
577         .init_cfg               = ov2680_init_cfg,
578         .enum_mbus_code         = ov2680_enum_mbus_code,
579         .get_fmt                = ov2680_get_fmt,
580         .set_fmt                = ov2680_set_fmt,
581         .enum_frame_size        = ov2680_enum_frame_size,
582         .enum_frame_interval    = ov2680_enum_frame_interval,
583 };
584
585 static const struct v4l2_subdev_ops ov2680_subdev_ops = {
586         .video  = &ov2680_video_ops,
587         .pad    = &ov2680_pad_ops,
588 };
589
590 static int ov2680_mode_init(struct ov2680_dev *sensor)
591 {
592         const struct ov2680_mode_info *init_mode;
593
594         /* set initial mode */
595         ov2680_fill_format(sensor, &sensor->fmt,
596                            OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
597
598         sensor->frame_interval.denominator = OV2680_FRAME_RATE;
599         sensor->frame_interval.numerator = 1;
600
601         init_mode = &ov2680_mode_init_data;
602
603         sensor->current_mode = init_mode;
604
605         return 0;
606 }
607
608 static int ov2680_v4l2_register(struct ov2680_dev *sensor)
609 {
610         struct i2c_client *client = to_i2c_client(sensor->dev);
611         const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops;
612         struct ov2680_ctrls *ctrls = &sensor->ctrls;
613         struct v4l2_ctrl_handler *hdl = &ctrls->handler;
614         int ret = 0;
615
616         v4l2_i2c_subdev_init(&sensor->sd, client, &ov2680_subdev_ops);
617
618         sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
619         sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
620         sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
621
622         ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
623         if (ret < 0)
624                 return ret;
625
626         v4l2_ctrl_handler_init(hdl, 5);
627
628         hdl->lock = &sensor->lock;
629
630         ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
631         ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
632
633         ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(hdl,
634                                         &ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN,
635                                         ARRAY_SIZE(test_pattern_menu) - 1,
636                                         0, 0, test_pattern_menu);
637
638         ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
639                                             0, 32767, 1, 0);
640
641         ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 2047, 1, 0);
642
643         if (hdl->error) {
644                 ret = hdl->error;
645                 goto cleanup_entity;
646         }
647
648         ctrls->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
649         ctrls->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
650
651         sensor->sd.ctrl_handler = hdl;
652
653         ret = v4l2_async_register_subdev(&sensor->sd);
654         if (ret < 0)
655                 goto cleanup_entity;
656
657         return 0;
658
659 cleanup_entity:
660         media_entity_cleanup(&sensor->sd.entity);
661         v4l2_ctrl_handler_free(hdl);
662
663         return ret;
664 }
665
666 static int ov2680_get_regulators(struct ov2680_dev *sensor)
667 {
668         int i;
669
670         for (i = 0; i < OV2680_NUM_SUPPLIES; i++)
671                 sensor->supplies[i].supply = ov2680_supply_name[i];
672
673         return devm_regulator_bulk_get(sensor->dev,
674                                        OV2680_NUM_SUPPLIES, sensor->supplies);
675 }
676
677 static int ov2680_check_id(struct ov2680_dev *sensor)
678 {
679         u64 chip_id;
680         int ret;
681
682         ret = cci_read(sensor->regmap, OV2680_REG_CHIP_ID, &chip_id, NULL);
683         if (ret < 0) {
684                 dev_err(sensor->dev, "failed to read chip id\n");
685                 return -ENODEV;
686         }
687
688         if (chip_id != OV2680_CHIP_ID) {
689                 dev_err(sensor->dev, "chip id: 0x%04llx does not match expected 0x%04x\n",
690                         chip_id, OV2680_CHIP_ID);
691                 return -ENODEV;
692         }
693
694         return 0;
695 }
696
697 static int ov2680_parse_dt(struct ov2680_dev *sensor)
698 {
699         struct device *dev = sensor->dev;
700         int ret;
701
702         sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
703                                                      GPIOD_OUT_HIGH);
704         ret = PTR_ERR_OR_ZERO(sensor->reset_gpio);
705         if (ret < 0) {
706                 dev_dbg(dev, "error while getting reset gpio: %d\n", ret);
707                 return ret;
708         }
709
710         sensor->xvclk = devm_clk_get(dev, "xvclk");
711         if (IS_ERR(sensor->xvclk)) {
712                 dev_err(dev, "xvclk clock missing or invalid\n");
713                 return PTR_ERR(sensor->xvclk);
714         }
715
716         sensor->xvclk_freq = clk_get_rate(sensor->xvclk);
717         if (sensor->xvclk_freq != OV2680_XVCLK_VALUE) {
718                 dev_err(dev, "wrong xvclk frequency %d HZ, expected: %d Hz\n",
719                         sensor->xvclk_freq, OV2680_XVCLK_VALUE);
720                 return -EINVAL;
721         }
722
723         return 0;
724 }
725
726 static int ov2680_probe(struct i2c_client *client)
727 {
728         struct device *dev = &client->dev;
729         struct ov2680_dev *sensor;
730         int ret;
731
732         sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
733         if (!sensor)
734                 return -ENOMEM;
735
736         sensor->dev = &client->dev;
737
738         sensor->regmap = devm_cci_regmap_init_i2c(client, 16);
739         if (IS_ERR(sensor->regmap))
740                 return PTR_ERR(sensor->regmap);
741
742         ret = ov2680_parse_dt(sensor);
743         if (ret < 0)
744                 return -EINVAL;
745
746         ret = ov2680_mode_init(sensor);
747         if (ret < 0)
748                 return ret;
749
750         ret = ov2680_get_regulators(sensor);
751         if (ret < 0) {
752                 dev_err(dev, "failed to get regulators\n");
753                 return ret;
754         }
755
756         mutex_init(&sensor->lock);
757
758         /*
759          * Power up and verify the chip now, so that if runtime pm is
760          * disabled the chip is left on and streaming will work.
761          */
762         ret = ov2680_power_on(sensor);
763         if (ret < 0)
764                 goto lock_destroy;
765
766         ret = ov2680_check_id(sensor);
767         if (ret < 0)
768                 goto err_powerdown;
769
770         pm_runtime_set_active(&client->dev);
771         pm_runtime_get_noresume(&client->dev);
772         pm_runtime_enable(&client->dev);
773
774         ret = ov2680_v4l2_register(sensor);
775         if (ret < 0)
776                 goto err_pm_runtime;
777
778         pm_runtime_set_autosuspend_delay(&client->dev, 1000);
779         pm_runtime_use_autosuspend(&client->dev);
780         pm_runtime_put_autosuspend(&client->dev);
781
782         dev_info(dev, "ov2680 init correctly\n");
783
784         return 0;
785
786 err_pm_runtime:
787         pm_runtime_disable(&client->dev);
788         pm_runtime_put_noidle(&client->dev);
789 err_powerdown:
790         ov2680_power_off(sensor);
791 lock_destroy:
792         dev_err(dev, "ov2680 init fail: %d\n", ret);
793         mutex_destroy(&sensor->lock);
794
795         return ret;
796 }
797
798 static void ov2680_remove(struct i2c_client *client)
799 {
800         struct v4l2_subdev *sd = i2c_get_clientdata(client);
801         struct ov2680_dev *sensor = to_ov2680_dev(sd);
802
803         v4l2_async_unregister_subdev(&sensor->sd);
804         mutex_destroy(&sensor->lock);
805         media_entity_cleanup(&sensor->sd.entity);
806         v4l2_ctrl_handler_free(&sensor->ctrls.handler);
807
808         /*
809          * Disable runtime PM. In case runtime PM is disabled in the kernel,
810          * make sure to turn power off manually.
811          */
812         pm_runtime_disable(&client->dev);
813         if (!pm_runtime_status_suspended(&client->dev))
814                 ov2680_power_off(sensor);
815         pm_runtime_set_suspended(&client->dev);
816 }
817
818 static int ov2680_suspend(struct device *dev)
819 {
820         struct v4l2_subdev *sd = dev_get_drvdata(dev);
821         struct ov2680_dev *sensor = to_ov2680_dev(sd);
822
823         if (sensor->is_streaming)
824                 ov2680_stream_disable(sensor);
825
826         return ov2680_power_off(sensor);
827 }
828
829 static int ov2680_resume(struct device *dev)
830 {
831         struct v4l2_subdev *sd = dev_get_drvdata(dev);
832         struct ov2680_dev *sensor = to_ov2680_dev(sd);
833         int ret;
834
835         ret = ov2680_power_on(sensor);
836         if (ret < 0)
837                 goto stream_disable;
838
839         if (sensor->is_streaming) {
840                 ret = ov2680_stream_enable(sensor);
841                 if (ret < 0)
842                         goto stream_disable;
843         }
844
845         return 0;
846
847 stream_disable:
848         ov2680_stream_disable(sensor);
849         sensor->is_streaming = false;
850
851         return ret;
852 }
853
854 static DEFINE_RUNTIME_DEV_PM_OPS(ov2680_pm_ops, ov2680_suspend, ov2680_resume,
855                                  NULL);
856
857 static const struct of_device_id ov2680_dt_ids[] = {
858         { .compatible = "ovti,ov2680" },
859         { /* sentinel */ },
860 };
861 MODULE_DEVICE_TABLE(of, ov2680_dt_ids);
862
863 static struct i2c_driver ov2680_i2c_driver = {
864         .driver = {
865                 .name  = "ov2680",
866                 .pm = pm_sleep_ptr(&ov2680_pm_ops),
867                 .of_match_table = ov2680_dt_ids,
868         },
869         .probe          = ov2680_probe,
870         .remove         = ov2680_remove,
871 };
872 module_i2c_driver(ov2680_i2c_driver);
873
874 MODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>");
875 MODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver");
876 MODULE_LICENSE("GPL v2");