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 <asm/unaligned.h>
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/err.h>
17 #include <linux/i2c.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/regulator/consumer.h>
23
24 #include <media/v4l2-common.h>
25 #include <media/v4l2-ctrls.h>
26 #include <media/v4l2-subdev.h>
27
28 #define OV2680_XVCLK_VALUE 24000000
29
30 #define OV2680_CHIP_ID 0x2680
31
32 #define OV2680_REG_STREAM_CTRL 0x0100
33 #define OV2680_REG_SOFT_RESET 0x0103
34
35 #define OV2680_REG_CHIP_ID_HIGH 0x300a
36 #define OV2680_REG_CHIP_ID_LOW 0x300b
37
38 #define OV2680_REG_R_MANUAL 0x3503
39 #define OV2680_REG_GAIN_PK 0x350a
40 #define OV2680_REG_EXPOSURE_PK_HIGH 0x3500
41 #define OV2680_REG_TIMING_HTS 0x380c
42 #define OV2680_REG_TIMING_VTS 0x380e
43 #define OV2680_REG_FORMAT1 0x3820
44 #define OV2680_REG_FORMAT2 0x3821
45
46 #define OV2680_REG_ISP_CTRL00 0x5080
47
48 #define OV2680_FRAME_RATE 30
49
50 #define OV2680_REG_VALUE_8BIT 1
51 #define OV2680_REG_VALUE_16BIT 2
52 #define OV2680_REG_VALUE_24BIT 3
53
54 #define OV2680_WIDTH_MAX 1600
55 #define OV2680_HEIGHT_MAX 1200
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 struct reg_value {
65 u16 reg_addr;
66 u8 val;
67 };
68
69 static const char * const ov2680_supply_name[] = {
70 "DOVDD",
71 "DVDD",
72 "AVDD",
73 };
74
75 #define OV2680_NUM_SUPPLIES ARRAY_SIZE(ov2680_supply_name)
76
77 struct ov2680_mode_info {
78 const char *name;
79 enum ov2680_mode_id id;
80 u32 width;
81 u32 height;
82 const struct reg_value *reg_data;
83 u32 reg_data_size;
84 };
85
86 struct ov2680_ctrls {
87 struct v4l2_ctrl_handler handler;
88 struct v4l2_ctrl *exposure;
89 struct v4l2_ctrl *gain;
90 struct v4l2_ctrl *hflip;
91 struct v4l2_ctrl *vflip;
92 struct v4l2_ctrl *test_pattern;
93 };
94
95 struct ov2680_dev {
96 struct i2c_client *i2c_client;
97 struct v4l2_subdev sd;
98
99 struct media_pad pad;
100 struct clk *xvclk;
101 u32 xvclk_freq;
102 struct regulator_bulk_data supplies[OV2680_NUM_SUPPLIES];
103
104 struct gpio_desc *reset_gpio;
105 struct mutex lock; /* protect members */
106
107 bool mode_pending_changes;
108 bool is_enabled;
109 bool is_streaming;
110
111 struct ov2680_ctrls ctrls;
112 struct v4l2_mbus_framefmt fmt;
113 struct v4l2_fract frame_interval;
114
115 const struct ov2680_mode_info *current_mode;
116 };
117
118 static const char * const test_pattern_menu[] = {
119 "Disabled",
120 "Color Bars",
121 "Random Data",
122 "Square",
123 "Black Image",
124 };
125
126 static const int ov2680_hv_flip_bayer_order[] = {
127 MEDIA_BUS_FMT_SBGGR10_1X10,
128 MEDIA_BUS_FMT_SGRBG10_1X10,
129 MEDIA_BUS_FMT_SGBRG10_1X10,
130 MEDIA_BUS_FMT_SRGGB10_1X10,
131 };
132
133 static const struct reg_value ov2680_setting_30fps_QUXGA_800_600[] = {
134 {0x3086, 0x01}, {0x370a, 0x23}, {0x3808, 0x03}, {0x3809, 0x20},
135 {0x380a, 0x02}, {0x380b, 0x58}, {0x380c, 0x06}, {0x380d, 0xac},
136 {0x380e, 0x02}, {0x380f, 0x84}, {0x3811, 0x04}, {0x3813, 0x04},
137 {0x3814, 0x31}, {0x3815, 0x31}, {0x3820, 0xc0}, {0x4008, 0x00},
138 {0x4009, 0x03}, {0x4837, 0x1e}, {0x3501, 0x4e}, {0x3502, 0xe0},
139 {0x3503, 0x03},
140 };
141
142 static const struct reg_value ov2680_setting_30fps_720P_1280_720[] = {
143 {0x3086, 0x00}, {0x3808, 0x05}, {0x3809, 0x00}, {0x380a, 0x02},
144 {0x380b, 0xd0}, {0x380c, 0x06}, {0x380d, 0xa8}, {0x380e, 0x05},
145 {0x380f, 0x0e}, {0x3811, 0x08}, {0x3813, 0x06}, {0x3814, 0x11},
146 {0x3815, 0x11}, {0x3820, 0xc0}, {0x4008, 0x00},
147 };
148
149 static const struct reg_value ov2680_setting_30fps_UXGA_1600_1200[] = {
150 {0x3086, 0x00}, {0x3501, 0x4e}, {0x3502, 0xe0}, {0x3808, 0x06},
151 {0x3809, 0x40}, {0x380a, 0x04}, {0x380b, 0xb0}, {0x380c, 0x06},
152 {0x380d, 0xa8}, {0x380e, 0x05}, {0x380f, 0x0e}, {0x3811, 0x00},
153 {0x3813, 0x00}, {0x3814, 0x11}, {0x3815, 0x11}, {0x3820, 0xc0},
154 {0x4008, 0x00}, {0x4837, 0x18}
155 };
156
157 static const struct ov2680_mode_info ov2680_mode_init_data = {
158 "mode_quxga_800_600", OV2680_MODE_QUXGA_800_600, 800, 600,
159 ov2680_setting_30fps_QUXGA_800_600,
160 ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600),
161 };
162
163 static const struct ov2680_mode_info ov2680_mode_data[OV2680_MODE_MAX] = {
164 {"mode_quxga_800_600", OV2680_MODE_QUXGA_800_600,
165 800, 600, ov2680_setting_30fps_QUXGA_800_600,
166 ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600)},
167 {"mode_720p_1280_720", OV2680_MODE_720P_1280_720,
168 1280, 720, ov2680_setting_30fps_720P_1280_720,
169 ARRAY_SIZE(ov2680_setting_30fps_720P_1280_720)},
170 {"mode_uxga_1600_1200", OV2680_MODE_UXGA_1600_1200,
171 1600, 1200, ov2680_setting_30fps_UXGA_1600_1200,
172 ARRAY_SIZE(ov2680_setting_30fps_UXGA_1600_1200)},
173 };
174
to_ov2680_dev(struct v4l2_subdev *sd)175 static struct ov2680_dev *to_ov2680_dev(struct v4l2_subdev *sd)
176 {
177 return container_of(sd, struct ov2680_dev, sd);
178 }
179
ov2680_to_dev(struct ov2680_dev *sensor)180 static struct device *ov2680_to_dev(struct ov2680_dev *sensor)
181 {
182 return &sensor->i2c_client->dev;
183 }
184
ctrl_to_sd(struct v4l2_ctrl *ctrl)185 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
186 {
187 return &container_of(ctrl->handler, struct ov2680_dev,
188 ctrls.handler)->sd;
189 }
190
__ov2680_write_reg(struct ov2680_dev *sensor, u16 reg, unsigned int len, u32 val)191 static int __ov2680_write_reg(struct ov2680_dev *sensor, u16 reg,
192 unsigned int len, u32 val)
193 {
194 struct i2c_client *client = sensor->i2c_client;
195 u8 buf[6];
196 int ret;
197
198 if (len > 4)
199 return -EINVAL;
200
201 put_unaligned_be16(reg, buf);
202 put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
203 ret = i2c_master_send(client, buf, len + 2);
204 if (ret != len + 2) {
205 dev_err(&client->dev, "write error: reg=0x%4x: %d\n", reg, ret);
206 return -EIO;
207 }
208
209 return 0;
210 }
211
212 #define ov2680_write_reg(s, r, v) \
213 __ov2680_write_reg(s, r, OV2680_REG_VALUE_8BIT, v)
214
215 #define ov2680_write_reg16(s, r, v) \
216 __ov2680_write_reg(s, r, OV2680_REG_VALUE_16BIT, v)
217
218 #define ov2680_write_reg24(s, r, v) \
219 __ov2680_write_reg(s, r, OV2680_REG_VALUE_24BIT, v)
220
__ov2680_read_reg(struct ov2680_dev *sensor, u16 reg, unsigned int len, u32 *val)221 static int __ov2680_read_reg(struct ov2680_dev *sensor, u16 reg,
222 unsigned int len, u32 *val)
223 {
224 struct i2c_client *client = sensor->i2c_client;
225 struct i2c_msg msgs[2];
226 u8 addr_buf[2] = { reg >> 8, reg & 0xff };
227 u8 data_buf[4] = { 0, };
228 int ret;
229
230 if (len > 4)
231 return -EINVAL;
232
233 msgs[0].addr = client->addr;
234 msgs[0].flags = 0;
235 msgs[0].len = ARRAY_SIZE(addr_buf);
236 msgs[0].buf = addr_buf;
237
238 msgs[1].addr = client->addr;
239 msgs[1].flags = I2C_M_RD;
240 msgs[1].len = len;
241 msgs[1].buf = &data_buf[4 - len];
242
243 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
244 if (ret != ARRAY_SIZE(msgs)) {
245 dev_err(&client->dev, "read error: reg=0x%4x: %d\n", reg, ret);
246 return -EIO;
247 }
248
249 *val = get_unaligned_be32(data_buf);
250
251 return 0;
252 }
253
254 #define ov2680_read_reg(s, r, v) \
255 __ov2680_read_reg(s, r, OV2680_REG_VALUE_8BIT, v)
256
257 #define ov2680_read_reg16(s, r, v) \
258 __ov2680_read_reg(s, r, OV2680_REG_VALUE_16BIT, v)
259
260 #define ov2680_read_reg24(s, r, v) \
261 __ov2680_read_reg(s, r, OV2680_REG_VALUE_24BIT, v)
262
ov2680_mod_reg(struct ov2680_dev *sensor, u16 reg, u8 mask, u8 val)263 static int ov2680_mod_reg(struct ov2680_dev *sensor, u16 reg, u8 mask, u8 val)
264 {
265 u32 readval;
266 int ret;
267
268 ret = ov2680_read_reg(sensor, reg, &readval);
269 if (ret < 0)
270 return ret;
271
272 readval &= ~mask;
273 val &= mask;
274 val |= readval;
275
276 return ov2680_write_reg(sensor, reg, val);
277 }
278
ov2680_load_regs(struct ov2680_dev *sensor, const struct ov2680_mode_info *mode)279 static int ov2680_load_regs(struct ov2680_dev *sensor,
280 const struct ov2680_mode_info *mode)
281 {
282 const struct reg_value *regs = mode->reg_data;
283 unsigned int i;
284 int ret = 0;
285 u16 reg_addr;
286 u8 val;
287
288 for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
289 reg_addr = regs->reg_addr;
290 val = regs->val;
291
292 ret = ov2680_write_reg(sensor, reg_addr, val);
293 if (ret)
294 break;
295 }
296
297 return ret;
298 }
299
ov2680_power_up(struct ov2680_dev *sensor)300 static void ov2680_power_up(struct ov2680_dev *sensor)
301 {
302 if (!sensor->reset_gpio)
303 return;
304
305 gpiod_set_value(sensor->reset_gpio, 0);
306 usleep_range(5000, 10000);
307 }
308
ov2680_power_down(struct ov2680_dev *sensor)309 static void ov2680_power_down(struct ov2680_dev *sensor)
310 {
311 if (!sensor->reset_gpio)
312 return;
313
314 gpiod_set_value(sensor->reset_gpio, 1);
315 usleep_range(5000, 10000);
316 }
317
ov2680_set_bayer_order(struct ov2680_dev *sensor)318 static void ov2680_set_bayer_order(struct ov2680_dev *sensor)
319 {
320 int hv_flip = 0;
321
322 if (sensor->ctrls.vflip && sensor->ctrls.vflip->val)
323 hv_flip += 1;
324
325 if (sensor->ctrls.hflip && sensor->ctrls.hflip->val)
326 hv_flip += 2;
327
328 sensor->fmt.code = ov2680_hv_flip_bayer_order[hv_flip];
329 }
330
ov2680_set_vflip(struct ov2680_dev *sensor, s32 val)331 static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val)
332 {
333 int ret;
334
335 if (sensor->is_streaming)
336 return -EBUSY;
337
338 ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1,
339 BIT(2), val ? BIT(2) : 0);
340 if (ret < 0)
341 return ret;
342
343 ov2680_set_bayer_order(sensor);
344 return 0;
345 }
346
ov2680_set_hflip(struct ov2680_dev *sensor, s32 val)347 static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val)
348 {
349 int ret;
350
351 if (sensor->is_streaming)
352 return -EBUSY;
353
354 ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2,
355 BIT(2), val ? BIT(2) : 0);
356 if (ret < 0)
357 return ret;
358
359 ov2680_set_bayer_order(sensor);
360 return 0;
361 }
362
ov2680_test_pattern_set(struct ov2680_dev *sensor, int value)363 static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value)
364 {
365 int ret;
366
367 if (!value)
368 return ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, BIT(7), 0);
369
370 ret = ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, 0x03, value - 1);
371 if (ret < 0)
372 return ret;
373
374 ret = ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, BIT(7), BIT(7));
375 if (ret < 0)
376 return ret;
377
378 return 0;
379 }
380
ov2680_gain_set(struct ov2680_dev *sensor, u32 gain)381 static int ov2680_gain_set(struct ov2680_dev *sensor, u32 gain)
382 {
383 return ov2680_write_reg16(sensor, OV2680_REG_GAIN_PK, gain);
384 }
385
ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp)386 static int ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp)
387 {
388 return ov2680_write_reg24(sensor, OV2680_REG_EXPOSURE_PK_HIGH,
389 exp << 4);
390 }
391
ov2680_stream_enable(struct ov2680_dev *sensor)392 static int ov2680_stream_enable(struct ov2680_dev *sensor)
393 {
394 return ov2680_write_reg(sensor, OV2680_REG_STREAM_CTRL, 1);
395 }
396
ov2680_stream_disable(struct ov2680_dev *sensor)397 static int ov2680_stream_disable(struct ov2680_dev *sensor)
398 {
399 return ov2680_write_reg(sensor, OV2680_REG_STREAM_CTRL, 0);
400 }
401
ov2680_mode_set(struct ov2680_dev *sensor)402 static int ov2680_mode_set(struct ov2680_dev *sensor)
403 {
404 int ret;
405
406 ret = ov2680_load_regs(sensor, sensor->current_mode);
407 if (ret < 0)
408 return ret;
409
410 /* Restore value of all ctrls */
411 ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
412 if (ret < 0)
413 return ret;
414
415 sensor->mode_pending_changes = false;
416
417 return 0;
418 }
419
ov2680_mode_restore(struct ov2680_dev *sensor)420 static int ov2680_mode_restore(struct ov2680_dev *sensor)
421 {
422 int ret;
423
424 ret = ov2680_load_regs(sensor, &ov2680_mode_init_data);
425 if (ret < 0)
426 return ret;
427
428 return ov2680_mode_set(sensor);
429 }
430
ov2680_power_off(struct ov2680_dev *sensor)431 static int ov2680_power_off(struct ov2680_dev *sensor)
432 {
433 if (!sensor->is_enabled)
434 return 0;
435
436 clk_disable_unprepare(sensor->xvclk);
437 ov2680_power_down(sensor);
438 regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
439 sensor->is_enabled = false;
440
441 return 0;
442 }
443
ov2680_power_on(struct ov2680_dev *sensor)444 static int ov2680_power_on(struct ov2680_dev *sensor)
445 {
446 struct device *dev = ov2680_to_dev(sensor);
447 int ret;
448
449 if (sensor->is_enabled)
450 return 0;
451
452 ret = regulator_bulk_enable(OV2680_NUM_SUPPLIES, sensor->supplies);
453 if (ret < 0) {
454 dev_err(dev, "failed to enable regulators: %d\n", ret);
455 return ret;
456 }
457
458 if (!sensor->reset_gpio) {
459 ret = ov2680_write_reg(sensor, OV2680_REG_SOFT_RESET, 0x01);
460 if (ret != 0) {
461 dev_err(dev, "sensor soft reset failed\n");
462 goto err_disable_regulators;
463 }
464 usleep_range(1000, 2000);
465 } else {
466 ov2680_power_down(sensor);
467 ov2680_power_up(sensor);
468 }
469
470 ret = clk_prepare_enable(sensor->xvclk);
471 if (ret < 0)
472 goto err_disable_regulators;
473
474 sensor->is_enabled = true;
475
476 /* Set clock lane into LP-11 state */
477 ov2680_stream_enable(sensor);
478 usleep_range(1000, 2000);
479 ov2680_stream_disable(sensor);
480
481 return 0;
482
483 err_disable_regulators:
484 regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
485 return ret;
486 }
487
ov2680_s_power(struct v4l2_subdev *sd, int on)488 static int ov2680_s_power(struct v4l2_subdev *sd, int on)
489 {
490 struct ov2680_dev *sensor = to_ov2680_dev(sd);
491 int ret = 0;
492
493 mutex_lock(&sensor->lock);
494
495 if (on)
496 ret = ov2680_power_on(sensor);
497 else
498 ret = ov2680_power_off(sensor);
499
500 if (on && ret == 0)
501 ret = ov2680_mode_restore(sensor);
502
503 mutex_unlock(&sensor->lock);
504
505 return ret;
506 }
507
ov2680_s_g_frame_interval(struct v4l2_subdev *sd, struct v4l2_subdev_frame_interval *fi)508 static int ov2680_s_g_frame_interval(struct v4l2_subdev *sd,
509 struct v4l2_subdev_frame_interval *fi)
510 {
511 struct ov2680_dev *sensor = to_ov2680_dev(sd);
512
513 mutex_lock(&sensor->lock);
514 fi->interval = sensor->frame_interval;
515 mutex_unlock(&sensor->lock);
516
517 return 0;
518 }
519
ov2680_s_stream(struct v4l2_subdev *sd, int enable)520 static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
521 {
522 struct ov2680_dev *sensor = to_ov2680_dev(sd);
523 int ret = 0;
524
525 mutex_lock(&sensor->lock);
526
527 if (sensor->is_streaming == !!enable)
528 goto unlock;
529
530 if (enable && sensor->mode_pending_changes) {
531 ret = ov2680_mode_set(sensor);
532 if (ret < 0)
533 goto unlock;
534 }
535
536 if (enable)
537 ret = ov2680_stream_enable(sensor);
538 else
539 ret = ov2680_stream_disable(sensor);
540
541 sensor->is_streaming = !!enable;
542
543 unlock:
544 mutex_unlock(&sensor->lock);
545
546 return ret;
547 }
548
ov2680_enum_mbus_code(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg, struct v4l2_subdev_mbus_code_enum *code)549 static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
550 struct v4l2_subdev_pad_config *cfg,
551 struct v4l2_subdev_mbus_code_enum *code)
552 {
553 struct ov2680_dev *sensor = to_ov2680_dev(sd);
554
555 if (code->pad != 0 || code->index != 0)
556 return -EINVAL;
557
558 code->code = sensor->fmt.code;
559
560 return 0;
561 }
562
ov2680_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg, struct v4l2_subdev_format *format)563 static int ov2680_get_fmt(struct v4l2_subdev *sd,
564 struct v4l2_subdev_pad_config *cfg,
565 struct v4l2_subdev_format *format)
566 {
567 struct ov2680_dev *sensor = to_ov2680_dev(sd);
568 struct v4l2_mbus_framefmt *fmt = NULL;
569 int ret = 0;
570
571 if (format->pad != 0)
572 return -EINVAL;
573
574 mutex_lock(&sensor->lock);
575
576 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
577 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
578 fmt = v4l2_subdev_get_try_format(&sensor->sd, cfg, format->pad);
579 #else
580 ret = -EINVAL;
581 #endif
582 } else {
583 fmt = &sensor->fmt;
584 }
585
586 if (fmt)
587 format->format = *fmt;
588
589 mutex_unlock(&sensor->lock);
590
591 return ret;
592 }
593
ov2680_set_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg, struct v4l2_subdev_format *format)594 static int ov2680_set_fmt(struct v4l2_subdev *sd,
595 struct v4l2_subdev_pad_config *cfg,
596 struct v4l2_subdev_format *format)
597 {
598 struct ov2680_dev *sensor = to_ov2680_dev(sd);
599 struct v4l2_mbus_framefmt *fmt = &format->format;
600 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
601 struct v4l2_mbus_framefmt *try_fmt;
602 #endif
603 const struct ov2680_mode_info *mode;
604 int ret = 0;
605
606 if (format->pad != 0)
607 return -EINVAL;
608
609 mutex_lock(&sensor->lock);
610
611 if (sensor->is_streaming) {
612 ret = -EBUSY;
613 goto unlock;
614 }
615
616 mode = v4l2_find_nearest_size(ov2680_mode_data,
617 ARRAY_SIZE(ov2680_mode_data), width,
618 height, fmt->width, fmt->height);
619 if (!mode) {
620 ret = -EINVAL;
621 goto unlock;
622 }
623
624 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
625 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
626 try_fmt = v4l2_subdev_get_try_format(sd, cfg, 0);
627 format->format = *try_fmt;
628 #endif
629 goto unlock;
630 }
631
632 fmt->width = mode->width;
633 fmt->height = mode->height;
634 fmt->code = sensor->fmt.code;
635 fmt->colorspace = sensor->fmt.colorspace;
636
637 sensor->current_mode = mode;
638 sensor->fmt = format->format;
639 sensor->mode_pending_changes = true;
640
641 unlock:
642 mutex_unlock(&sensor->lock);
643
644 return ret;
645 }
646
ov2680_init_cfg(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg)647 static int ov2680_init_cfg(struct v4l2_subdev *sd,
648 struct v4l2_subdev_pad_config *cfg)
649 {
650 struct v4l2_subdev_format fmt = {
651 .which = cfg ? V4L2_SUBDEV_FORMAT_TRY
652 : V4L2_SUBDEV_FORMAT_ACTIVE,
653 .format = {
654 .width = 800,
655 .height = 600,
656 }
657 };
658
659 return ov2680_set_fmt(sd, cfg, &fmt);
660 }
661
ov2680_enum_frame_size(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg, struct v4l2_subdev_frame_size_enum *fse)662 static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
663 struct v4l2_subdev_pad_config *cfg,
664 struct v4l2_subdev_frame_size_enum *fse)
665 {
666 int index = fse->index;
667
668 if (index >= OV2680_MODE_MAX || index < 0)
669 return -EINVAL;
670
671 fse->min_width = ov2680_mode_data[index].width;
672 fse->min_height = ov2680_mode_data[index].height;
673 fse->max_width = ov2680_mode_data[index].width;
674 fse->max_height = ov2680_mode_data[index].height;
675
676 return 0;
677 }
678
ov2680_enum_frame_interval(struct v4l2_subdev *sd, struct v4l2_subdev_pad_config *cfg, struct v4l2_subdev_frame_interval_enum *fie)679 static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
680 struct v4l2_subdev_pad_config *cfg,
681 struct v4l2_subdev_frame_interval_enum *fie)
682 {
683 struct v4l2_fract tpf;
684
685 if (fie->index >= OV2680_MODE_MAX || fie->width > OV2680_WIDTH_MAX ||
686 fie->height > OV2680_HEIGHT_MAX ||
687 fie->which > V4L2_SUBDEV_FORMAT_ACTIVE)
688 return -EINVAL;
689
690 tpf.denominator = OV2680_FRAME_RATE;
691 tpf.numerator = 1;
692
693 fie->interval = tpf;
694
695 return 0;
696 }
697
ov2680_s_ctrl(struct v4l2_ctrl *ctrl)698 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
699 {
700 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
701 struct ov2680_dev *sensor = to_ov2680_dev(sd);
702
703 if (!sensor->is_enabled)
704 return 0;
705
706 switch (ctrl->id) {
707 case V4L2_CID_GAIN:
708 return ov2680_gain_set(sensor, ctrl->val);
709 case V4L2_CID_EXPOSURE:
710 return ov2680_exposure_set(sensor, ctrl->val);
711 case V4L2_CID_VFLIP:
712 return ov2680_set_vflip(sensor, ctrl->val);
713 case V4L2_CID_HFLIP:
714 return ov2680_set_hflip(sensor, ctrl->val);
715 case V4L2_CID_TEST_PATTERN:
716 return ov2680_test_pattern_set(sensor, ctrl->val);
717 default:
718 break;
719 }
720
721 return -EINVAL;
722 }
723
724 static const struct v4l2_ctrl_ops ov2680_ctrl_ops = {
725 .s_ctrl = ov2680_s_ctrl,
726 };
727
728 static const struct v4l2_subdev_core_ops ov2680_core_ops = {
729 .s_power = ov2680_s_power,
730 };
731
732 static const struct v4l2_subdev_video_ops ov2680_video_ops = {
733 .g_frame_interval = ov2680_s_g_frame_interval,
734 .s_frame_interval = ov2680_s_g_frame_interval,
735 .s_stream = ov2680_s_stream,
736 };
737
738 static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
739 .init_cfg = ov2680_init_cfg,
740 .enum_mbus_code = ov2680_enum_mbus_code,
741 .get_fmt = ov2680_get_fmt,
742 .set_fmt = ov2680_set_fmt,
743 .enum_frame_size = ov2680_enum_frame_size,
744 .enum_frame_interval = ov2680_enum_frame_interval,
745 };
746
747 static const struct v4l2_subdev_ops ov2680_subdev_ops = {
748 .core = &ov2680_core_ops,
749 .video = &ov2680_video_ops,
750 .pad = &ov2680_pad_ops,
751 };
752
ov2680_mode_init(struct ov2680_dev *sensor)753 static int ov2680_mode_init(struct ov2680_dev *sensor)
754 {
755 const struct ov2680_mode_info *init_mode;
756
757 /* set initial mode */
758 sensor->fmt.code = MEDIA_BUS_FMT_SBGGR10_1X10;
759 sensor->fmt.width = 800;
760 sensor->fmt.height = 600;
761 sensor->fmt.field = V4L2_FIELD_NONE;
762 sensor->fmt.colorspace = V4L2_COLORSPACE_SRGB;
763
764 sensor->frame_interval.denominator = OV2680_FRAME_RATE;
765 sensor->frame_interval.numerator = 1;
766
767 init_mode = &ov2680_mode_init_data;
768
769 sensor->current_mode = init_mode;
770
771 sensor->mode_pending_changes = true;
772
773 return 0;
774 }
775
ov2680_v4l2_register(struct ov2680_dev *sensor)776 static int ov2680_v4l2_register(struct ov2680_dev *sensor)
777 {
778 const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops;
779 struct ov2680_ctrls *ctrls = &sensor->ctrls;
780 struct v4l2_ctrl_handler *hdl = &ctrls->handler;
781 int ret = 0;
782
783 v4l2_i2c_subdev_init(&sensor->sd, sensor->i2c_client,
784 &ov2680_subdev_ops);
785
786 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
787 sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
788 #endif
789 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
790 sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
791
792 ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
793 if (ret < 0)
794 return ret;
795
796 v4l2_ctrl_handler_init(hdl, 5);
797
798 hdl->lock = &sensor->lock;
799
800 ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
801 ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
802
803 ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(hdl,
804 &ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN,
805 ARRAY_SIZE(test_pattern_menu) - 1,
806 0, 0, test_pattern_menu);
807
808 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
809 0, 32767, 1, 0);
810
811 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 2047, 1, 0);
812
813 if (hdl->error) {
814 ret = hdl->error;
815 goto cleanup_entity;
816 }
817
818 ctrls->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
819 ctrls->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
820
821 sensor->sd.ctrl_handler = hdl;
822
823 ret = v4l2_async_register_subdev(&sensor->sd);
824 if (ret < 0)
825 goto cleanup_entity;
826
827 return 0;
828
829 cleanup_entity:
830 media_entity_cleanup(&sensor->sd.entity);
831 v4l2_ctrl_handler_free(hdl);
832
833 return ret;
834 }
835
ov2680_get_regulators(struct ov2680_dev *sensor)836 static int ov2680_get_regulators(struct ov2680_dev *sensor)
837 {
838 int i;
839
840 for (i = 0; i < OV2680_NUM_SUPPLIES; i++)
841 sensor->supplies[i].supply = ov2680_supply_name[i];
842
843 return devm_regulator_bulk_get(&sensor->i2c_client->dev,
844 OV2680_NUM_SUPPLIES,
845 sensor->supplies);
846 }
847
ov2680_check_id(struct ov2680_dev *sensor)848 static int ov2680_check_id(struct ov2680_dev *sensor)
849 {
850 struct device *dev = ov2680_to_dev(sensor);
851 u32 chip_id;
852 int ret;
853
854 ov2680_power_on(sensor);
855
856 ret = ov2680_read_reg16(sensor, OV2680_REG_CHIP_ID_HIGH, &chip_id);
857 if (ret < 0) {
858 dev_err(dev, "failed to read chip id high\n");
859 return -ENODEV;
860 }
861
862 if (chip_id != OV2680_CHIP_ID) {
863 dev_err(dev, "chip id: 0x%04x does not match expected 0x%04x\n",
864 chip_id, OV2680_CHIP_ID);
865 return -ENODEV;
866 }
867
868 return 0;
869 }
870
ov2680_parse_dt(struct ov2680_dev *sensor)871 static int ov2680_parse_dt(struct ov2680_dev *sensor)
872 {
873 struct device *dev = ov2680_to_dev(sensor);
874 int ret;
875
876 sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
877 GPIOD_OUT_HIGH);
878 ret = PTR_ERR_OR_ZERO(sensor->reset_gpio);
879 if (ret < 0) {
880 dev_dbg(dev, "error while getting reset gpio: %d\n", ret);
881 return ret;
882 }
883
884 sensor->xvclk = devm_clk_get(dev, "xvclk");
885 if (IS_ERR(sensor->xvclk)) {
886 dev_err(dev, "xvclk clock missing or invalid\n");
887 return PTR_ERR(sensor->xvclk);
888 }
889
890 sensor->xvclk_freq = clk_get_rate(sensor->xvclk);
891 if (sensor->xvclk_freq != OV2680_XVCLK_VALUE) {
892 dev_err(dev, "wrong xvclk frequency %d HZ, expected: %d Hz\n",
893 sensor->xvclk_freq, OV2680_XVCLK_VALUE);
894 return -EINVAL;
895 }
896
897 return 0;
898 }
899
ov2680_probe(struct i2c_client *client)900 static int ov2680_probe(struct i2c_client *client)
901 {
902 struct device *dev = &client->dev;
903 struct ov2680_dev *sensor;
904 int ret;
905
906 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
907 if (!sensor)
908 return -ENOMEM;
909
910 sensor->i2c_client = client;
911
912 ret = ov2680_parse_dt(sensor);
913 if (ret < 0)
914 return -EINVAL;
915
916 ret = ov2680_mode_init(sensor);
917 if (ret < 0)
918 return ret;
919
920 ret = ov2680_get_regulators(sensor);
921 if (ret < 0) {
922 dev_err(dev, "failed to get regulators\n");
923 return ret;
924 }
925
926 mutex_init(&sensor->lock);
927
928 ret = ov2680_check_id(sensor);
929 if (ret < 0)
930 goto lock_destroy;
931
932 ret = ov2680_v4l2_register(sensor);
933 if (ret < 0)
934 goto lock_destroy;
935
936 dev_info(dev, "ov2680 init correctly\n");
937
938 return 0;
939
940 lock_destroy:
941 dev_err(dev, "ov2680 init fail: %d\n", ret);
942 mutex_destroy(&sensor->lock);
943
944 return ret;
945 }
946
ov2680_remove(struct i2c_client *client)947 static int ov2680_remove(struct i2c_client *client)
948 {
949 struct v4l2_subdev *sd = i2c_get_clientdata(client);
950 struct ov2680_dev *sensor = to_ov2680_dev(sd);
951
952 v4l2_async_unregister_subdev(&sensor->sd);
953 mutex_destroy(&sensor->lock);
954 media_entity_cleanup(&sensor->sd.entity);
955 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
956
957 return 0;
958 }
959
ov2680_suspend(struct device *dev)960 static int __maybe_unused ov2680_suspend(struct device *dev)
961 {
962 struct i2c_client *client = to_i2c_client(dev);
963 struct v4l2_subdev *sd = i2c_get_clientdata(client);
964 struct ov2680_dev *sensor = to_ov2680_dev(sd);
965
966 if (sensor->is_streaming)
967 ov2680_stream_disable(sensor);
968
969 return 0;
970 }
971
ov2680_resume(struct device *dev)972 static int __maybe_unused ov2680_resume(struct device *dev)
973 {
974 struct i2c_client *client = to_i2c_client(dev);
975 struct v4l2_subdev *sd = i2c_get_clientdata(client);
976 struct ov2680_dev *sensor = to_ov2680_dev(sd);
977 int ret;
978
979 if (sensor->is_streaming) {
980 ret = ov2680_stream_enable(sensor);
981 if (ret < 0)
982 goto stream_disable;
983 }
984
985 return 0;
986
987 stream_disable:
988 ov2680_stream_disable(sensor);
989 sensor->is_streaming = false;
990
991 return ret;
992 }
993
994 static const struct dev_pm_ops ov2680_pm_ops = {
995 SET_SYSTEM_SLEEP_PM_OPS(ov2680_suspend, ov2680_resume)
996 };
997
998 static const struct of_device_id ov2680_dt_ids[] = {
999 { .compatible = "ovti,ov2680" },
1000 { /* sentinel */ },
1001 };
1002 MODULE_DEVICE_TABLE(of, ov2680_dt_ids);
1003
1004 static struct i2c_driver ov2680_i2c_driver = {
1005 .driver = {
1006 .name = "ov2680",
1007 .pm = &ov2680_pm_ops,
1008 .of_match_table = of_match_ptr(ov2680_dt_ids),
1009 },
1010 .probe_new = ov2680_probe,
1011 .remove = ov2680_remove,
1012 };
1013 module_i2c_driver(ov2680_i2c_driver);
1014
1015 MODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>");
1016 MODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver");
1017 MODULE_LICENSE("GPL v2");
1018