1 // SPDX-License-Identifier: GPL-2.0
3 * Omnivision OV2680 CMOS Image Sensor driver
5 * Copyright (C) 2018 Linaro Ltd
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.
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>
24 #include <media/v4l2-common.h>
25 #include <media/v4l2-ctrls.h>
26 #include <media/v4l2-subdev.h>
28 #define OV2680_XVCLK_VALUE 24000000
30 #define OV2680_CHIP_ID 0x2680
32 #define OV2680_REG_STREAM_CTRL 0x0100
33 #define OV2680_REG_SOFT_RESET 0x0103
35 #define OV2680_REG_CHIP_ID_HIGH 0x300a
36 #define OV2680_REG_CHIP_ID_LOW 0x300b
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
46 #define OV2680_REG_ISP_CTRL00 0x5080
48 #define OV2680_FRAME_RATE 30
50 #define OV2680_REG_VALUE_8BIT 1
51 #define OV2680_REG_VALUE_16BIT 2
52 #define OV2680_REG_VALUE_24BIT 3
54 #define OV2680_WIDTH_MAX 1600
55 #define OV2680_HEIGHT_MAX 1200
58 OV2680_MODE_QUXGA_800_600,
59 OV2680_MODE_720P_1280_720,
60 OV2680_MODE_UXGA_1600_1200,
69 static const char * const ov2680_supply_name[] = {
75 #define OV2680_NUM_SUPPLIES ARRAY_SIZE(ov2680_supply_name)
77 struct ov2680_mode_info {
79 enum ov2680_mode_id id;
82 const struct reg_value *reg_data;
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;
96 struct i2c_client *i2c_client;
97 struct v4l2_subdev sd;
102 struct regulator_bulk_data supplies[OV2680_NUM_SUPPLIES];
104 struct gpio_desc *reset_gpio;
105 struct mutex lock; /* protect members */
107 bool mode_pending_changes;
111 struct ov2680_ctrls ctrls;
112 struct v4l2_mbus_framefmt fmt;
113 struct v4l2_fract frame_interval;
115 const struct ov2680_mode_info *current_mode;
118 static const char * const test_pattern_menu[] = {
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,
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},
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},
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}
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),
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)},
175 static struct ov2680_dev *to_ov2680_dev(struct v4l2_subdev *sd)
177 return container_of(sd, struct ov2680_dev, sd);
180 static struct device *ov2680_to_dev(struct ov2680_dev *sensor)
182 return &sensor->i2c_client->dev;
185 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
187 return &container_of(ctrl->handler, struct ov2680_dev,
191 static int __ov2680_write_reg(struct ov2680_dev *sensor, u16 reg,
192 unsigned int len, u32 val)
194 struct i2c_client *client = sensor->i2c_client;
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);
212 #define ov2680_write_reg(s, r, v) \
213 __ov2680_write_reg(s, r, OV2680_REG_VALUE_8BIT, v)
215 #define ov2680_write_reg16(s, r, v) \
216 __ov2680_write_reg(s, r, OV2680_REG_VALUE_16BIT, v)
218 #define ov2680_write_reg24(s, r, v) \
219 __ov2680_write_reg(s, r, OV2680_REG_VALUE_24BIT, v)
221 static int __ov2680_read_reg(struct ov2680_dev *sensor, u16 reg,
222 unsigned int len, u32 *val)
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, };
233 msgs[0].addr = client->addr;
235 msgs[0].len = ARRAY_SIZE(addr_buf);
236 msgs[0].buf = addr_buf;
238 msgs[1].addr = client->addr;
239 msgs[1].flags = I2C_M_RD;
241 msgs[1].buf = &data_buf[4 - len];
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);
249 *val = get_unaligned_be32(data_buf);
254 #define ov2680_read_reg(s, r, v) \
255 __ov2680_read_reg(s, r, OV2680_REG_VALUE_8BIT, v)
257 #define ov2680_read_reg16(s, r, v) \
258 __ov2680_read_reg(s, r, OV2680_REG_VALUE_16BIT, v)
260 #define ov2680_read_reg24(s, r, v) \
261 __ov2680_read_reg(s, r, OV2680_REG_VALUE_24BIT, v)
263 static int ov2680_mod_reg(struct ov2680_dev *sensor, u16 reg, u8 mask, u8 val)
268 ret = ov2680_read_reg(sensor, reg, &readval);
276 return ov2680_write_reg(sensor, reg, val);
279 static int ov2680_load_regs(struct ov2680_dev *sensor,
280 const struct ov2680_mode_info *mode)
282 const struct reg_value *regs = mode->reg_data;
288 for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
289 reg_addr = regs->reg_addr;
292 ret = ov2680_write_reg(sensor, reg_addr, val);
300 static void ov2680_power_up(struct ov2680_dev *sensor)
302 if (!sensor->reset_gpio)
305 gpiod_set_value(sensor->reset_gpio, 0);
306 usleep_range(5000, 10000);
309 static void ov2680_power_down(struct ov2680_dev *sensor)
311 if (!sensor->reset_gpio)
314 gpiod_set_value(sensor->reset_gpio, 1);
315 usleep_range(5000, 10000);
318 static void ov2680_set_bayer_order(struct ov2680_dev *sensor)
322 if (sensor->ctrls.vflip && sensor->ctrls.vflip->val)
325 if (sensor->ctrls.hflip && sensor->ctrls.hflip->val)
328 sensor->fmt.code = ov2680_hv_flip_bayer_order[hv_flip];
331 static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val)
335 if (sensor->is_streaming)
338 ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1,
339 BIT(2), val ? BIT(2) : 0);
343 ov2680_set_bayer_order(sensor);
347 static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val)
351 if (sensor->is_streaming)
354 ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2,
355 BIT(2), val ? BIT(2) : 0);
359 ov2680_set_bayer_order(sensor);
363 static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value)
368 return ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, BIT(7), 0);
370 ret = ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, 0x03, value - 1);
374 ret = ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, BIT(7), BIT(7));
381 static int ov2680_gain_set(struct ov2680_dev *sensor, u32 gain)
383 return ov2680_write_reg16(sensor, OV2680_REG_GAIN_PK, gain);
386 static int ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp)
388 return ov2680_write_reg24(sensor, OV2680_REG_EXPOSURE_PK_HIGH,
392 static int ov2680_stream_enable(struct ov2680_dev *sensor)
394 return ov2680_write_reg(sensor, OV2680_REG_STREAM_CTRL, 1);
397 static int ov2680_stream_disable(struct ov2680_dev *sensor)
399 return ov2680_write_reg(sensor, OV2680_REG_STREAM_CTRL, 0);
402 static int ov2680_mode_set(struct ov2680_dev *sensor)
406 ret = ov2680_load_regs(sensor, sensor->current_mode);
410 /* Restore value of all ctrls */
411 ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
415 sensor->mode_pending_changes = false;
420 static int ov2680_mode_restore(struct ov2680_dev *sensor)
424 ret = ov2680_load_regs(sensor, &ov2680_mode_init_data);
428 return ov2680_mode_set(sensor);
431 static int ov2680_power_off(struct ov2680_dev *sensor)
433 if (!sensor->is_enabled)
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;
444 static int ov2680_power_on(struct ov2680_dev *sensor)
446 struct device *dev = ov2680_to_dev(sensor);
449 if (sensor->is_enabled)
452 ret = regulator_bulk_enable(OV2680_NUM_SUPPLIES, sensor->supplies);
454 dev_err(dev, "failed to enable regulators: %d\n", ret);
458 if (!sensor->reset_gpio) {
459 ret = ov2680_write_reg(sensor, OV2680_REG_SOFT_RESET, 0x01);
461 dev_err(dev, "sensor soft reset failed\n");
462 goto err_disable_regulators;
464 usleep_range(1000, 2000);
466 ov2680_power_down(sensor);
467 ov2680_power_up(sensor);
470 ret = clk_prepare_enable(sensor->xvclk);
472 goto err_disable_regulators;
474 sensor->is_enabled = true;
476 /* Set clock lane into LP-11 state */
477 ov2680_stream_enable(sensor);
478 usleep_range(1000, 2000);
479 ov2680_stream_disable(sensor);
483 err_disable_regulators:
484 regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
488 static int ov2680_s_power(struct v4l2_subdev *sd, int on)
490 struct ov2680_dev *sensor = to_ov2680_dev(sd);
493 mutex_lock(&sensor->lock);
496 ret = ov2680_power_on(sensor);
498 ret = ov2680_power_off(sensor);
501 ret = ov2680_mode_restore(sensor);
503 mutex_unlock(&sensor->lock);
508 static int ov2680_s_g_frame_interval(struct v4l2_subdev *sd,
509 struct v4l2_subdev_frame_interval *fi)
511 struct ov2680_dev *sensor = to_ov2680_dev(sd);
513 mutex_lock(&sensor->lock);
514 fi->interval = sensor->frame_interval;
515 mutex_unlock(&sensor->lock);
520 static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
522 struct ov2680_dev *sensor = to_ov2680_dev(sd);
525 mutex_lock(&sensor->lock);
527 if (sensor->is_streaming == !!enable)
530 if (enable && sensor->mode_pending_changes) {
531 ret = ov2680_mode_set(sensor);
537 ret = ov2680_stream_enable(sensor);
539 ret = ov2680_stream_disable(sensor);
541 sensor->is_streaming = !!enable;
544 mutex_unlock(&sensor->lock);
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)
553 struct ov2680_dev *sensor = to_ov2680_dev(sd);
555 if (code->pad != 0 || code->index != 0)
558 code->code = sensor->fmt.code;
563 static int ov2680_get_fmt(struct v4l2_subdev *sd,
564 struct v4l2_subdev_pad_config *cfg,
565 struct v4l2_subdev_format *format)
567 struct ov2680_dev *sensor = to_ov2680_dev(sd);
568 struct v4l2_mbus_framefmt *fmt = NULL;
571 if (format->pad != 0)
574 mutex_lock(&sensor->lock);
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);
587 format->format = *fmt;
589 mutex_unlock(&sensor->lock);
594 static int ov2680_set_fmt(struct v4l2_subdev *sd,
595 struct v4l2_subdev_pad_config *cfg,
596 struct v4l2_subdev_format *format)
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;
603 const struct ov2680_mode_info *mode;
606 if (format->pad != 0)
609 mutex_lock(&sensor->lock);
611 if (sensor->is_streaming) {
616 mode = v4l2_find_nearest_size(ov2680_mode_data,
617 ARRAY_SIZE(ov2680_mode_data), width,
618 height, fmt->width, fmt->height);
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;
635 fmt->width = mode->width;
636 fmt->height = mode->height;
637 fmt->code = sensor->fmt.code;
638 fmt->colorspace = sensor->fmt.colorspace;
640 sensor->current_mode = mode;
641 sensor->fmt = format->format;
642 sensor->mode_pending_changes = true;
645 mutex_unlock(&sensor->lock);
650 static int ov2680_init_cfg(struct v4l2_subdev *sd,
651 struct v4l2_subdev_pad_config *cfg)
653 struct v4l2_subdev_format fmt = {
654 .which = cfg ? V4L2_SUBDEV_FORMAT_TRY
655 : V4L2_SUBDEV_FORMAT_ACTIVE,
662 return ov2680_set_fmt(sd, cfg, &fmt);
665 static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
666 struct v4l2_subdev_pad_config *cfg,
667 struct v4l2_subdev_frame_size_enum *fse)
669 int index = fse->index;
671 if (index >= OV2680_MODE_MAX || index < 0)
674 fse->min_width = ov2680_mode_data[index].width;
675 fse->min_height = ov2680_mode_data[index].height;
676 fse->max_width = ov2680_mode_data[index].width;
677 fse->max_height = ov2680_mode_data[index].height;
682 static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
683 struct v4l2_subdev_pad_config *cfg,
684 struct v4l2_subdev_frame_interval_enum *fie)
686 struct v4l2_fract tpf;
688 if (fie->index >= OV2680_MODE_MAX || fie->width > OV2680_WIDTH_MAX ||
689 fie->height > OV2680_HEIGHT_MAX ||
690 fie->which > V4L2_SUBDEV_FORMAT_ACTIVE)
693 tpf.denominator = OV2680_FRAME_RATE;
701 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
703 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
704 struct ov2680_dev *sensor = to_ov2680_dev(sd);
706 if (!sensor->is_enabled)
711 return ov2680_gain_set(sensor, ctrl->val);
712 case V4L2_CID_EXPOSURE:
713 return ov2680_exposure_set(sensor, ctrl->val);
715 return ov2680_set_vflip(sensor, ctrl->val);
717 return ov2680_set_hflip(sensor, ctrl->val);
718 case V4L2_CID_TEST_PATTERN:
719 return ov2680_test_pattern_set(sensor, ctrl->val);
727 static const struct v4l2_ctrl_ops ov2680_ctrl_ops = {
728 .s_ctrl = ov2680_s_ctrl,
731 static const struct v4l2_subdev_core_ops ov2680_core_ops = {
732 .s_power = ov2680_s_power,
735 static const struct v4l2_subdev_video_ops ov2680_video_ops = {
736 .g_frame_interval = ov2680_s_g_frame_interval,
737 .s_frame_interval = ov2680_s_g_frame_interval,
738 .s_stream = ov2680_s_stream,
741 static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
742 .init_cfg = ov2680_init_cfg,
743 .enum_mbus_code = ov2680_enum_mbus_code,
744 .get_fmt = ov2680_get_fmt,
745 .set_fmt = ov2680_set_fmt,
746 .enum_frame_size = ov2680_enum_frame_size,
747 .enum_frame_interval = ov2680_enum_frame_interval,
750 static const struct v4l2_subdev_ops ov2680_subdev_ops = {
751 .core = &ov2680_core_ops,
752 .video = &ov2680_video_ops,
753 .pad = &ov2680_pad_ops,
756 static int ov2680_mode_init(struct ov2680_dev *sensor)
758 const struct ov2680_mode_info *init_mode;
760 /* set initial mode */
761 sensor->fmt.code = MEDIA_BUS_FMT_SBGGR10_1X10;
762 sensor->fmt.width = 800;
763 sensor->fmt.height = 600;
764 sensor->fmt.field = V4L2_FIELD_NONE;
765 sensor->fmt.colorspace = V4L2_COLORSPACE_SRGB;
767 sensor->frame_interval.denominator = OV2680_FRAME_RATE;
768 sensor->frame_interval.numerator = 1;
770 init_mode = &ov2680_mode_init_data;
772 sensor->current_mode = init_mode;
774 sensor->mode_pending_changes = true;
779 static int ov2680_v4l2_init(struct ov2680_dev *sensor)
781 const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops;
782 struct ov2680_ctrls *ctrls = &sensor->ctrls;
783 struct v4l2_ctrl_handler *hdl = &ctrls->handler;
786 v4l2_i2c_subdev_init(&sensor->sd, sensor->i2c_client,
789 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
790 sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
792 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
793 sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
795 ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
799 v4l2_ctrl_handler_init(hdl, 5);
801 hdl->lock = &sensor->lock;
803 ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
804 ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
806 ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(hdl,
807 &ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN,
808 ARRAY_SIZE(test_pattern_menu) - 1,
809 0, 0, test_pattern_menu);
811 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
814 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 2047, 1, 0);
821 ctrls->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
822 ctrls->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
824 sensor->sd.ctrl_handler = hdl;
826 ret = v4l2_async_register_subdev(&sensor->sd);
833 media_entity_cleanup(&sensor->sd.entity);
834 v4l2_ctrl_handler_free(hdl);
839 static int ov2680_get_regulators(struct ov2680_dev *sensor)
843 for (i = 0; i < OV2680_NUM_SUPPLIES; i++)
844 sensor->supplies[i].supply = ov2680_supply_name[i];
846 return devm_regulator_bulk_get(&sensor->i2c_client->dev,
851 static int ov2680_check_id(struct ov2680_dev *sensor)
853 struct device *dev = ov2680_to_dev(sensor);
857 ov2680_power_on(sensor);
859 ret = ov2680_read_reg16(sensor, OV2680_REG_CHIP_ID_HIGH, &chip_id);
861 dev_err(dev, "failed to read chip id high\n");
865 if (chip_id != OV2680_CHIP_ID) {
866 dev_err(dev, "chip id: 0x%04x does not match expected 0x%04x\n",
867 chip_id, OV2680_CHIP_ID);
874 static int ov2860_parse_dt(struct ov2680_dev *sensor)
876 struct device *dev = ov2680_to_dev(sensor);
879 sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
881 ret = PTR_ERR_OR_ZERO(sensor->reset_gpio);
883 dev_dbg(dev, "error while getting reset gpio: %d\n", ret);
887 sensor->xvclk = devm_clk_get(dev, "xvclk");
888 if (IS_ERR(sensor->xvclk)) {
889 dev_err(dev, "xvclk clock missing or invalid\n");
890 return PTR_ERR(sensor->xvclk);
893 sensor->xvclk_freq = clk_get_rate(sensor->xvclk);
894 if (sensor->xvclk_freq != OV2680_XVCLK_VALUE) {
895 dev_err(dev, "wrong xvclk frequency %d HZ, expected: %d Hz\n",
896 sensor->xvclk_freq, OV2680_XVCLK_VALUE);
903 static int ov2680_probe(struct i2c_client *client)
905 struct device *dev = &client->dev;
906 struct ov2680_dev *sensor;
909 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
913 sensor->i2c_client = client;
915 ret = ov2860_parse_dt(sensor);
919 ret = ov2680_mode_init(sensor);
923 ret = ov2680_get_regulators(sensor);
925 dev_err(dev, "failed to get regulators\n");
929 mutex_init(&sensor->lock);
931 ret = ov2680_check_id(sensor);
935 ret = ov2680_v4l2_init(sensor);
939 dev_info(dev, "ov2680 init correctly\n");
944 dev_err(dev, "ov2680 init fail: %d\n", ret);
945 mutex_destroy(&sensor->lock);
950 static int ov2680_remove(struct i2c_client *client)
952 struct v4l2_subdev *sd = i2c_get_clientdata(client);
953 struct ov2680_dev *sensor = to_ov2680_dev(sd);
955 v4l2_async_unregister_subdev(&sensor->sd);
956 mutex_destroy(&sensor->lock);
957 media_entity_cleanup(&sensor->sd.entity);
958 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
963 static int __maybe_unused ov2680_suspend(struct device *dev)
965 struct i2c_client *client = to_i2c_client(dev);
966 struct v4l2_subdev *sd = i2c_get_clientdata(client);
967 struct ov2680_dev *sensor = to_ov2680_dev(sd);
969 if (sensor->is_streaming)
970 ov2680_stream_disable(sensor);
975 static int __maybe_unused ov2680_resume(struct device *dev)
977 struct i2c_client *client = to_i2c_client(dev);
978 struct v4l2_subdev *sd = i2c_get_clientdata(client);
979 struct ov2680_dev *sensor = to_ov2680_dev(sd);
982 if (sensor->is_streaming) {
983 ret = ov2680_stream_enable(sensor);
991 ov2680_stream_disable(sensor);
992 sensor->is_streaming = false;
997 static const struct dev_pm_ops ov2680_pm_ops = {
998 SET_SYSTEM_SLEEP_PM_OPS(ov2680_suspend, ov2680_resume)
1001 static const struct of_device_id ov2680_dt_ids[] = {
1002 { .compatible = "ovti,ov2680" },
1005 MODULE_DEVICE_TABLE(of, ov2680_dt_ids);
1007 static struct i2c_driver ov2680_i2c_driver = {
1010 .pm = &ov2680_pm_ops,
1011 .of_match_table = of_match_ptr(ov2680_dt_ids),
1013 .probe_new = ov2680_probe,
1014 .remove = ov2680_remove,
1016 module_i2c_driver(ov2680_i2c_driver);
1018 MODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>");
1019 MODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver");
1020 MODULE_LICENSE("GPL v2");