cf84701a6a5ae5a176844b0912813e8f54f17941
[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                *pwdn_gpio;
101         struct mutex                    lock; /* protect members */
102
103         bool                            is_streaming;
104
105         struct ov2680_ctrls             ctrls;
106         struct v4l2_mbus_framefmt       fmt;
107         struct v4l2_fract               frame_interval;
108
109         const struct ov2680_mode_info   *current_mode;
110 };
111
112 static const char * const test_pattern_menu[] = {
113         "Disabled",
114         "Color Bars",
115         "Random Data",
116         "Square",
117         "Black Image",
118 };
119
120 static const int ov2680_hv_flip_bayer_order[] = {
121         MEDIA_BUS_FMT_SBGGR10_1X10,
122         MEDIA_BUS_FMT_SGRBG10_1X10,
123         MEDIA_BUS_FMT_SGBRG10_1X10,
124         MEDIA_BUS_FMT_SRGGB10_1X10,
125 };
126
127 static const struct reg_sequence ov2680_setting_30fps_QUXGA_800_600[] = {
128         {0x3086, 0x01}, {0x370a, 0x23}, {0x3808, 0x03}, {0x3809, 0x20},
129         {0x380a, 0x02}, {0x380b, 0x58}, {0x380c, 0x06}, {0x380d, 0xac},
130         {0x380e, 0x02}, {0x380f, 0x84}, {0x3811, 0x04}, {0x3813, 0x04},
131         {0x3814, 0x31}, {0x3815, 0x31}, {0x3820, 0xc0}, {0x4008, 0x00},
132         {0x4009, 0x03}, {0x4837, 0x1e}, {0x3501, 0x4e}, {0x3502, 0xe0},
133         {0x3503, 0x03},
134 };
135
136 static const struct reg_sequence ov2680_setting_30fps_720P_1280_720[] = {
137         {0x3086, 0x00}, {0x3808, 0x05}, {0x3809, 0x00}, {0x380a, 0x02},
138         {0x380b, 0xd0}, {0x380c, 0x06}, {0x380d, 0xa8}, {0x380e, 0x05},
139         {0x380f, 0x0e}, {0x3811, 0x08}, {0x3813, 0x06}, {0x3814, 0x11},
140         {0x3815, 0x11}, {0x3820, 0xc0}, {0x4008, 0x00},
141 };
142
143 static const struct reg_sequence ov2680_setting_30fps_UXGA_1600_1200[] = {
144         {0x3086, 0x00}, {0x3501, 0x4e}, {0x3502, 0xe0}, {0x3808, 0x06},
145         {0x3809, 0x40}, {0x380a, 0x04}, {0x380b, 0xb0}, {0x380c, 0x06},
146         {0x380d, 0xa8}, {0x380e, 0x05}, {0x380f, 0x0e}, {0x3811, 0x00},
147         {0x3813, 0x00}, {0x3814, 0x11}, {0x3815, 0x11}, {0x3820, 0xc0},
148         {0x4008, 0x00}, {0x4837, 0x18}
149 };
150
151 static const struct ov2680_mode_info ov2680_mode_init_data = {
152         "mode_quxga_800_600", OV2680_MODE_QUXGA_800_600, 800, 600,
153         ov2680_setting_30fps_QUXGA_800_600,
154         ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600),
155 };
156
157 static const struct ov2680_mode_info ov2680_mode_data[OV2680_MODE_MAX] = {
158         {"mode_quxga_800_600", OV2680_MODE_QUXGA_800_600,
159          800, 600, ov2680_setting_30fps_QUXGA_800_600,
160          ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600)},
161         {"mode_720p_1280_720", OV2680_MODE_720P_1280_720,
162          1280, 720, ov2680_setting_30fps_720P_1280_720,
163          ARRAY_SIZE(ov2680_setting_30fps_720P_1280_720)},
164         {"mode_uxga_1600_1200", OV2680_MODE_UXGA_1600_1200,
165          1600, 1200, ov2680_setting_30fps_UXGA_1600_1200,
166          ARRAY_SIZE(ov2680_setting_30fps_UXGA_1600_1200)},
167 };
168
169 static struct ov2680_dev *to_ov2680_dev(struct v4l2_subdev *sd)
170 {
171         return container_of(sd, struct ov2680_dev, sd);
172 }
173
174 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
175 {
176         return &container_of(ctrl->handler, struct ov2680_dev,
177                              ctrls.handler)->sd;
178 }
179
180 static void ov2680_power_up(struct ov2680_dev *sensor)
181 {
182         if (!sensor->pwdn_gpio)
183                 return;
184
185         gpiod_set_value(sensor->pwdn_gpio, 0);
186         usleep_range(5000, 10000);
187 }
188
189 static void ov2680_power_down(struct ov2680_dev *sensor)
190 {
191         if (!sensor->pwdn_gpio)
192                 return;
193
194         gpiod_set_value(sensor->pwdn_gpio, 1);
195         usleep_range(5000, 10000);
196 }
197
198 static void ov2680_set_bayer_order(struct ov2680_dev *sensor,
199                                    struct v4l2_mbus_framefmt *fmt)
200 {
201         int hv_flip = 0;
202
203         if (sensor->ctrls.vflip && sensor->ctrls.vflip->val)
204                 hv_flip += 1;
205
206         if (sensor->ctrls.hflip && sensor->ctrls.hflip->val)
207                 hv_flip += 2;
208
209         fmt->code = ov2680_hv_flip_bayer_order[hv_flip];
210 }
211
212 static void ov2680_fill_format(struct ov2680_dev *sensor,
213                                struct v4l2_mbus_framefmt *fmt,
214                                unsigned int width, unsigned int height)
215 {
216         memset(fmt, 0, sizeof(*fmt));
217         fmt->width = width;
218         fmt->height = height;
219         fmt->field = V4L2_FIELD_NONE;
220         fmt->colorspace = V4L2_COLORSPACE_SRGB;
221         ov2680_set_bayer_order(sensor, fmt);
222 }
223
224 static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val)
225 {
226         int ret;
227
228         if (sensor->is_streaming)
229                 return -EBUSY;
230
231         ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT1,
232                               BIT(2), val ? BIT(2) : 0, NULL);
233         if (ret < 0)
234                 return ret;
235
236         ov2680_set_bayer_order(sensor, &sensor->fmt);
237         return 0;
238 }
239
240 static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val)
241 {
242         int ret;
243
244         if (sensor->is_streaming)
245                 return -EBUSY;
246
247         ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT2,
248                               BIT(2), val ? BIT(2) : 0, NULL);
249         if (ret < 0)
250                 return ret;
251
252         ov2680_set_bayer_order(sensor, &sensor->fmt);
253         return 0;
254 }
255
256 static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value)
257 {
258         int ret = 0;
259
260         if (!value)
261                 return cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
262                                        BIT(7), 0, NULL);
263
264         cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
265                         0x03, value - 1, &ret);
266         cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
267                         BIT(7), BIT(7), &ret);
268
269         return ret;
270 }
271
272 static int ov2680_gain_set(struct ov2680_dev *sensor, u32 gain)
273 {
274         return cci_write(sensor->regmap, OV2680_REG_GAIN_PK, gain, NULL);
275 }
276
277 static int ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp)
278 {
279         return cci_write(sensor->regmap, OV2680_REG_EXPOSURE_PK, exp << 4,
280                          NULL);
281 }
282
283 static int ov2680_stream_enable(struct ov2680_dev *sensor)
284 {
285         int ret;
286
287         ret = regmap_multi_reg_write(sensor->regmap,
288                                      ov2680_mode_init_data.reg_data,
289                                      ov2680_mode_init_data.reg_data_size);
290         if (ret < 0)
291                 return ret;
292
293         ret = regmap_multi_reg_write(sensor->regmap,
294                                      sensor->current_mode->reg_data,
295                                      sensor->current_mode->reg_data_size);
296         if (ret < 0)
297                 return ret;
298
299         /* Restore value of all ctrls */
300         ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
301         if (ret < 0)
302                 return ret;
303
304         return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 1, NULL);
305 }
306
307 static int ov2680_stream_disable(struct ov2680_dev *sensor)
308 {
309         return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 0, NULL);
310 }
311
312 static int ov2680_power_off(struct ov2680_dev *sensor)
313 {
314         clk_disable_unprepare(sensor->xvclk);
315         ov2680_power_down(sensor);
316         regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
317         return 0;
318 }
319
320 static int ov2680_power_on(struct ov2680_dev *sensor)
321 {
322         int ret;
323
324         ret = regulator_bulk_enable(OV2680_NUM_SUPPLIES, sensor->supplies);
325         if (ret < 0) {
326                 dev_err(sensor->dev, "failed to enable regulators: %d\n", ret);
327                 return ret;
328         }
329
330         if (!sensor->pwdn_gpio) {
331                 ret = cci_write(sensor->regmap, OV2680_REG_SOFT_RESET, 0x01,
332                                 NULL);
333                 if (ret != 0) {
334                         dev_err(sensor->dev, "sensor soft reset failed\n");
335                         goto err_disable_regulators;
336                 }
337                 usleep_range(1000, 2000);
338         } else {
339                 ov2680_power_down(sensor);
340                 ov2680_power_up(sensor);
341         }
342
343         ret = clk_prepare_enable(sensor->xvclk);
344         if (ret < 0)
345                 goto err_disable_regulators;
346
347         return 0;
348
349 err_disable_regulators:
350         regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
351         return ret;
352 }
353
354 static int ov2680_s_g_frame_interval(struct v4l2_subdev *sd,
355                                      struct v4l2_subdev_frame_interval *fi)
356 {
357         struct ov2680_dev *sensor = to_ov2680_dev(sd);
358
359         mutex_lock(&sensor->lock);
360         fi->interval = sensor->frame_interval;
361         mutex_unlock(&sensor->lock);
362
363         return 0;
364 }
365
366 static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
367 {
368         struct ov2680_dev *sensor = to_ov2680_dev(sd);
369         int ret = 0;
370
371         mutex_lock(&sensor->lock);
372
373         if (sensor->is_streaming == !!enable)
374                 goto unlock;
375
376         if (enable) {
377                 ret = pm_runtime_resume_and_get(sensor->sd.dev);
378                 if (ret < 0)
379                         goto unlock;
380
381                 ret = ov2680_stream_enable(sensor);
382                 if (ret < 0) {
383                         pm_runtime_put(sensor->sd.dev);
384                         goto unlock;
385                 }
386         } else {
387                 ret = ov2680_stream_disable(sensor);
388                 pm_runtime_put(sensor->sd.dev);
389         }
390
391         sensor->is_streaming = !!enable;
392
393 unlock:
394         mutex_unlock(&sensor->lock);
395
396         return ret;
397 }
398
399 static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
400                                  struct v4l2_subdev_state *sd_state,
401                                  struct v4l2_subdev_mbus_code_enum *code)
402 {
403         struct ov2680_dev *sensor = to_ov2680_dev(sd);
404
405         if (code->pad != 0 || code->index != 0)
406                 return -EINVAL;
407
408         code->code = sensor->fmt.code;
409
410         return 0;
411 }
412
413 static int ov2680_get_fmt(struct v4l2_subdev *sd,
414                           struct v4l2_subdev_state *sd_state,
415                           struct v4l2_subdev_format *format)
416 {
417         struct ov2680_dev *sensor = to_ov2680_dev(sd);
418         struct v4l2_mbus_framefmt *fmt = NULL;
419
420         if (format->pad != 0)
421                 return -EINVAL;
422
423         mutex_lock(&sensor->lock);
424
425         if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
426                 fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state,
427                                                  format->pad);
428         } else {
429                 fmt = &sensor->fmt;
430         }
431
432         format->format = *fmt;
433
434         mutex_unlock(&sensor->lock);
435
436         return 0;
437 }
438
439 static int ov2680_set_fmt(struct v4l2_subdev *sd,
440                           struct v4l2_subdev_state *sd_state,
441                           struct v4l2_subdev_format *format)
442 {
443         struct ov2680_dev *sensor = to_ov2680_dev(sd);
444         struct v4l2_mbus_framefmt *try_fmt;
445         const struct ov2680_mode_info *mode;
446         int ret = 0;
447
448         if (format->pad != 0)
449                 return -EINVAL;
450
451         mode = v4l2_find_nearest_size(ov2680_mode_data,
452                                       ARRAY_SIZE(ov2680_mode_data),
453                                       width, height,
454                                       format->format.width,
455                                       format->format.height);
456         if (!mode)
457                 return -EINVAL;
458
459         ov2680_fill_format(sensor, &format->format, mode->width, mode->height);
460
461         if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
462                 try_fmt = v4l2_subdev_get_try_format(sd, sd_state, 0);
463                 *try_fmt = format->format;
464                 return 0;
465         }
466
467         mutex_lock(&sensor->lock);
468
469         if (sensor->is_streaming) {
470                 ret = -EBUSY;
471                 goto unlock;
472         }
473
474         sensor->current_mode = mode;
475         sensor->fmt = format->format;
476
477 unlock:
478         mutex_unlock(&sensor->lock);
479
480         return ret;
481 }
482
483 static int ov2680_init_cfg(struct v4l2_subdev *sd,
484                            struct v4l2_subdev_state *sd_state)
485 {
486         struct ov2680_dev *sensor = to_ov2680_dev(sd);
487
488         ov2680_fill_format(sensor, &sd_state->pads[0].try_fmt,
489                            OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
490         return 0;
491 }
492
493 static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
494                                   struct v4l2_subdev_state *sd_state,
495                                   struct v4l2_subdev_frame_size_enum *fse)
496 {
497         int index = fse->index;
498
499         if (index >= OV2680_MODE_MAX || index < 0)
500                 return -EINVAL;
501
502         fse->min_width = ov2680_mode_data[index].width;
503         fse->min_height = ov2680_mode_data[index].height;
504         fse->max_width = ov2680_mode_data[index].width;
505         fse->max_height = ov2680_mode_data[index].height;
506
507         return 0;
508 }
509
510 static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
511                               struct v4l2_subdev_state *sd_state,
512                               struct v4l2_subdev_frame_interval_enum *fie)
513 {
514         struct v4l2_fract tpf;
515
516         if (fie->index >= OV2680_MODE_MAX || fie->width > OV2680_WIDTH_MAX ||
517             fie->height > OV2680_HEIGHT_MAX ||
518             fie->which > V4L2_SUBDEV_FORMAT_ACTIVE)
519                 return -EINVAL;
520
521         tpf.denominator = OV2680_FRAME_RATE;
522         tpf.numerator = 1;
523
524         fie->interval = tpf;
525
526         return 0;
527 }
528
529 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
530 {
531         struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
532         struct ov2680_dev *sensor = to_ov2680_dev(sd);
533         int ret;
534
535         /* Only apply changes to the controls if the device is powered up */
536         if (!pm_runtime_get_if_in_use(sensor->sd.dev)) {
537                 ov2680_set_bayer_order(sensor, &sensor->fmt);
538                 return 0;
539         }
540
541         switch (ctrl->id) {
542         case V4L2_CID_GAIN:
543                 ret = ov2680_gain_set(sensor, ctrl->val);
544                 break;
545         case V4L2_CID_EXPOSURE:
546                 ret = ov2680_exposure_set(sensor, ctrl->val);
547                 break;
548         case V4L2_CID_VFLIP:
549                 ret = ov2680_set_vflip(sensor, ctrl->val);
550                 break;
551         case V4L2_CID_HFLIP:
552                 ret = ov2680_set_hflip(sensor, ctrl->val);
553                 break;
554         case V4L2_CID_TEST_PATTERN:
555                 ret = ov2680_test_pattern_set(sensor, ctrl->val);
556                 break;
557         default:
558                 ret = -EINVAL;
559                 break;
560         }
561
562         pm_runtime_put(sensor->sd.dev);
563         return ret;
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         struct gpio_desc *gpio;
701         int ret;
702
703         /*
704          * The pin we want is named XSHUTDN in the datasheet. Linux sensor
705          * drivers have standardized on using "powerdown" as con-id name
706          * for powerdown or shutdown pins. Older DTB files use "reset",
707          * so fallback to that if there is no "powerdown" pin.
708          */
709         gpio = devm_gpiod_get_optional(dev, "powerdown", GPIOD_OUT_HIGH);
710         if (!gpio)
711                 gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
712
713         ret = PTR_ERR_OR_ZERO(gpio);
714         if (ret < 0) {
715                 dev_dbg(dev, "error while getting reset gpio: %d\n", ret);
716                 return ret;
717         }
718
719         sensor->pwdn_gpio = gpio;
720
721         sensor->xvclk = devm_clk_get(dev, "xvclk");
722         if (IS_ERR(sensor->xvclk)) {
723                 dev_err(dev, "xvclk clock missing or invalid\n");
724                 return PTR_ERR(sensor->xvclk);
725         }
726
727         sensor->xvclk_freq = clk_get_rate(sensor->xvclk);
728         if (sensor->xvclk_freq != OV2680_XVCLK_VALUE) {
729                 dev_err(dev, "wrong xvclk frequency %d HZ, expected: %d Hz\n",
730                         sensor->xvclk_freq, OV2680_XVCLK_VALUE);
731                 return -EINVAL;
732         }
733
734         return 0;
735 }
736
737 static int ov2680_probe(struct i2c_client *client)
738 {
739         struct device *dev = &client->dev;
740         struct ov2680_dev *sensor;
741         int ret;
742
743         sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
744         if (!sensor)
745                 return -ENOMEM;
746
747         sensor->dev = &client->dev;
748
749         sensor->regmap = devm_cci_regmap_init_i2c(client, 16);
750         if (IS_ERR(sensor->regmap))
751                 return PTR_ERR(sensor->regmap);
752
753         ret = ov2680_parse_dt(sensor);
754         if (ret < 0)
755                 return -EINVAL;
756
757         ret = ov2680_mode_init(sensor);
758         if (ret < 0)
759                 return ret;
760
761         ret = ov2680_get_regulators(sensor);
762         if (ret < 0) {
763                 dev_err(dev, "failed to get regulators\n");
764                 return ret;
765         }
766
767         mutex_init(&sensor->lock);
768
769         /*
770          * Power up and verify the chip now, so that if runtime pm is
771          * disabled the chip is left on and streaming will work.
772          */
773         ret = ov2680_power_on(sensor);
774         if (ret < 0)
775                 goto lock_destroy;
776
777         ret = ov2680_check_id(sensor);
778         if (ret < 0)
779                 goto err_powerdown;
780
781         pm_runtime_set_active(&client->dev);
782         pm_runtime_get_noresume(&client->dev);
783         pm_runtime_enable(&client->dev);
784
785         ret = ov2680_v4l2_register(sensor);
786         if (ret < 0)
787                 goto err_pm_runtime;
788
789         pm_runtime_set_autosuspend_delay(&client->dev, 1000);
790         pm_runtime_use_autosuspend(&client->dev);
791         pm_runtime_put_autosuspend(&client->dev);
792
793         dev_info(dev, "ov2680 init correctly\n");
794
795         return 0;
796
797 err_pm_runtime:
798         pm_runtime_disable(&client->dev);
799         pm_runtime_put_noidle(&client->dev);
800 err_powerdown:
801         ov2680_power_off(sensor);
802 lock_destroy:
803         dev_err(dev, "ov2680 init fail: %d\n", ret);
804         mutex_destroy(&sensor->lock);
805
806         return ret;
807 }
808
809 static void ov2680_remove(struct i2c_client *client)
810 {
811         struct v4l2_subdev *sd = i2c_get_clientdata(client);
812         struct ov2680_dev *sensor = to_ov2680_dev(sd);
813
814         v4l2_async_unregister_subdev(&sensor->sd);
815         mutex_destroy(&sensor->lock);
816         media_entity_cleanup(&sensor->sd.entity);
817         v4l2_ctrl_handler_free(&sensor->ctrls.handler);
818
819         /*
820          * Disable runtime PM. In case runtime PM is disabled in the kernel,
821          * make sure to turn power off manually.
822          */
823         pm_runtime_disable(&client->dev);
824         if (!pm_runtime_status_suspended(&client->dev))
825                 ov2680_power_off(sensor);
826         pm_runtime_set_suspended(&client->dev);
827 }
828
829 static int ov2680_suspend(struct device *dev)
830 {
831         struct v4l2_subdev *sd = dev_get_drvdata(dev);
832         struct ov2680_dev *sensor = to_ov2680_dev(sd);
833
834         if (sensor->is_streaming)
835                 ov2680_stream_disable(sensor);
836
837         return ov2680_power_off(sensor);
838 }
839
840 static int ov2680_resume(struct device *dev)
841 {
842         struct v4l2_subdev *sd = dev_get_drvdata(dev);
843         struct ov2680_dev *sensor = to_ov2680_dev(sd);
844         int ret;
845
846         ret = ov2680_power_on(sensor);
847         if (ret < 0)
848                 goto stream_disable;
849
850         if (sensor->is_streaming) {
851                 ret = ov2680_stream_enable(sensor);
852                 if (ret < 0)
853                         goto stream_disable;
854         }
855
856         return 0;
857
858 stream_disable:
859         ov2680_stream_disable(sensor);
860         sensor->is_streaming = false;
861
862         return ret;
863 }
864
865 static DEFINE_RUNTIME_DEV_PM_OPS(ov2680_pm_ops, ov2680_suspend, ov2680_resume,
866                                  NULL);
867
868 static const struct of_device_id ov2680_dt_ids[] = {
869         { .compatible = "ovti,ov2680" },
870         { /* sentinel */ },
871 };
872 MODULE_DEVICE_TABLE(of, ov2680_dt_ids);
873
874 static struct i2c_driver ov2680_i2c_driver = {
875         .driver = {
876                 .name  = "ov2680",
877                 .pm = pm_sleep_ptr(&ov2680_pm_ops),
878                 .of_match_table = ov2680_dt_ids,
879         },
880         .probe          = ov2680_probe,
881         .remove         = ov2680_remove,
882 };
883 module_i2c_driver(ov2680_i2c_driver);
884
885 MODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>");
886 MODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver");
887 MODULE_LICENSE("GPL v2");