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
57 #define OV2680_DEFAULT_WIDTH 800
58 #define OV2680_DEFAULT_HEIGHT 600
61 OV2680_MODE_QUXGA_800_600,
62 OV2680_MODE_720P_1280_720,
63 OV2680_MODE_UXGA_1600_1200,
72 static const char * const ov2680_supply_name[] = {
78 #define OV2680_NUM_SUPPLIES ARRAY_SIZE(ov2680_supply_name)
80 struct ov2680_mode_info {
82 enum ov2680_mode_id id;
85 const struct reg_value *reg_data;
90 struct v4l2_ctrl_handler handler;
91 struct v4l2_ctrl *exposure;
92 struct v4l2_ctrl *gain;
93 struct v4l2_ctrl *hflip;
94 struct v4l2_ctrl *vflip;
95 struct v4l2_ctrl *test_pattern;
99 struct i2c_client *i2c_client;
100 struct v4l2_subdev sd;
102 struct media_pad pad;
105 struct regulator_bulk_data supplies[OV2680_NUM_SUPPLIES];
107 struct gpio_desc *reset_gpio;
108 struct mutex lock; /* protect members */
110 bool mode_pending_changes;
114 struct ov2680_ctrls ctrls;
115 struct v4l2_mbus_framefmt fmt;
116 struct v4l2_fract frame_interval;
118 const struct ov2680_mode_info *current_mode;
121 static const char * const test_pattern_menu[] = {
129 static const int ov2680_hv_flip_bayer_order[] = {
130 MEDIA_BUS_FMT_SBGGR10_1X10,
131 MEDIA_BUS_FMT_SGRBG10_1X10,
132 MEDIA_BUS_FMT_SGBRG10_1X10,
133 MEDIA_BUS_FMT_SRGGB10_1X10,
136 static const struct reg_value ov2680_setting_30fps_QUXGA_800_600[] = {
137 {0x3086, 0x01}, {0x370a, 0x23}, {0x3808, 0x03}, {0x3809, 0x20},
138 {0x380a, 0x02}, {0x380b, 0x58}, {0x380c, 0x06}, {0x380d, 0xac},
139 {0x380e, 0x02}, {0x380f, 0x84}, {0x3811, 0x04}, {0x3813, 0x04},
140 {0x3814, 0x31}, {0x3815, 0x31}, {0x3820, 0xc0}, {0x4008, 0x00},
141 {0x4009, 0x03}, {0x4837, 0x1e}, {0x3501, 0x4e}, {0x3502, 0xe0},
145 static const struct reg_value ov2680_setting_30fps_720P_1280_720[] = {
146 {0x3086, 0x00}, {0x3808, 0x05}, {0x3809, 0x00}, {0x380a, 0x02},
147 {0x380b, 0xd0}, {0x380c, 0x06}, {0x380d, 0xa8}, {0x380e, 0x05},
148 {0x380f, 0x0e}, {0x3811, 0x08}, {0x3813, 0x06}, {0x3814, 0x11},
149 {0x3815, 0x11}, {0x3820, 0xc0}, {0x4008, 0x00},
152 static const struct reg_value ov2680_setting_30fps_UXGA_1600_1200[] = {
153 {0x3086, 0x00}, {0x3501, 0x4e}, {0x3502, 0xe0}, {0x3808, 0x06},
154 {0x3809, 0x40}, {0x380a, 0x04}, {0x380b, 0xb0}, {0x380c, 0x06},
155 {0x380d, 0xa8}, {0x380e, 0x05}, {0x380f, 0x0e}, {0x3811, 0x00},
156 {0x3813, 0x00}, {0x3814, 0x11}, {0x3815, 0x11}, {0x3820, 0xc0},
157 {0x4008, 0x00}, {0x4837, 0x18}
160 static const struct ov2680_mode_info ov2680_mode_init_data = {
161 "mode_quxga_800_600", OV2680_MODE_QUXGA_800_600, 800, 600,
162 ov2680_setting_30fps_QUXGA_800_600,
163 ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600),
166 static const struct ov2680_mode_info ov2680_mode_data[OV2680_MODE_MAX] = {
167 {"mode_quxga_800_600", OV2680_MODE_QUXGA_800_600,
168 800, 600, ov2680_setting_30fps_QUXGA_800_600,
169 ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600)},
170 {"mode_720p_1280_720", OV2680_MODE_720P_1280_720,
171 1280, 720, ov2680_setting_30fps_720P_1280_720,
172 ARRAY_SIZE(ov2680_setting_30fps_720P_1280_720)},
173 {"mode_uxga_1600_1200", OV2680_MODE_UXGA_1600_1200,
174 1600, 1200, ov2680_setting_30fps_UXGA_1600_1200,
175 ARRAY_SIZE(ov2680_setting_30fps_UXGA_1600_1200)},
178 static struct ov2680_dev *to_ov2680_dev(struct v4l2_subdev *sd)
180 return container_of(sd, struct ov2680_dev, sd);
183 static struct device *ov2680_to_dev(struct ov2680_dev *sensor)
185 return &sensor->i2c_client->dev;
188 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
190 return &container_of(ctrl->handler, struct ov2680_dev,
194 static int __ov2680_write_reg(struct ov2680_dev *sensor, u16 reg,
195 unsigned int len, u32 val)
197 struct i2c_client *client = sensor->i2c_client;
204 put_unaligned_be16(reg, buf);
205 put_unaligned_be32(val << (8 * (4 - len)), buf + 2);
206 ret = i2c_master_send(client, buf, len + 2);
207 if (ret != len + 2) {
208 dev_err(&client->dev, "write error: reg=0x%4x: %d\n", reg, ret);
215 #define ov2680_write_reg(s, r, v) \
216 __ov2680_write_reg(s, r, OV2680_REG_VALUE_8BIT, v)
218 #define ov2680_write_reg16(s, r, v) \
219 __ov2680_write_reg(s, r, OV2680_REG_VALUE_16BIT, v)
221 #define ov2680_write_reg24(s, r, v) \
222 __ov2680_write_reg(s, r, OV2680_REG_VALUE_24BIT, v)
224 static int __ov2680_read_reg(struct ov2680_dev *sensor, u16 reg,
225 unsigned int len, u32 *val)
227 struct i2c_client *client = sensor->i2c_client;
228 struct i2c_msg msgs[2];
229 u8 addr_buf[2] = { reg >> 8, reg & 0xff };
230 u8 data_buf[4] = { 0, };
236 msgs[0].addr = client->addr;
238 msgs[0].len = ARRAY_SIZE(addr_buf);
239 msgs[0].buf = addr_buf;
241 msgs[1].addr = client->addr;
242 msgs[1].flags = I2C_M_RD;
244 msgs[1].buf = &data_buf[4 - len];
246 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
247 if (ret != ARRAY_SIZE(msgs)) {
248 dev_err(&client->dev, "read error: reg=0x%4x: %d\n", reg, ret);
252 *val = get_unaligned_be32(data_buf);
257 #define ov2680_read_reg(s, r, v) \
258 __ov2680_read_reg(s, r, OV2680_REG_VALUE_8BIT, v)
260 #define ov2680_read_reg16(s, r, v) \
261 __ov2680_read_reg(s, r, OV2680_REG_VALUE_16BIT, v)
263 #define ov2680_read_reg24(s, r, v) \
264 __ov2680_read_reg(s, r, OV2680_REG_VALUE_24BIT, v)
266 static int ov2680_mod_reg(struct ov2680_dev *sensor, u16 reg, u8 mask, u8 val)
271 ret = ov2680_read_reg(sensor, reg, &readval);
279 return ov2680_write_reg(sensor, reg, val);
282 static int ov2680_load_regs(struct ov2680_dev *sensor,
283 const struct ov2680_mode_info *mode)
285 const struct reg_value *regs = mode->reg_data;
291 for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
292 reg_addr = regs->reg_addr;
295 ret = ov2680_write_reg(sensor, reg_addr, val);
303 static void ov2680_power_up(struct ov2680_dev *sensor)
305 if (!sensor->reset_gpio)
308 gpiod_set_value(sensor->reset_gpio, 0);
309 usleep_range(5000, 10000);
312 static void ov2680_power_down(struct ov2680_dev *sensor)
314 if (!sensor->reset_gpio)
317 gpiod_set_value(sensor->reset_gpio, 1);
318 usleep_range(5000, 10000);
321 static void ov2680_set_bayer_order(struct ov2680_dev *sensor,
322 struct v4l2_mbus_framefmt *fmt)
326 if (sensor->ctrls.vflip && sensor->ctrls.vflip->val)
329 if (sensor->ctrls.hflip && sensor->ctrls.hflip->val)
332 fmt->code = ov2680_hv_flip_bayer_order[hv_flip];
335 static void ov2680_fill_format(struct ov2680_dev *sensor,
336 struct v4l2_mbus_framefmt *fmt,
337 unsigned int width, unsigned int height)
339 memset(fmt, 0, sizeof(*fmt));
341 fmt->height = height;
342 fmt->field = V4L2_FIELD_NONE;
343 fmt->colorspace = V4L2_COLORSPACE_SRGB;
344 ov2680_set_bayer_order(sensor, fmt);
347 static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val)
351 if (sensor->is_streaming)
354 ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT1,
355 BIT(2), val ? BIT(2) : 0);
359 ov2680_set_bayer_order(sensor, &sensor->fmt);
363 static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val)
367 if (sensor->is_streaming)
370 ret = ov2680_mod_reg(sensor, OV2680_REG_FORMAT2,
371 BIT(2), val ? BIT(2) : 0);
375 ov2680_set_bayer_order(sensor, &sensor->fmt);
379 static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value)
384 return ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, BIT(7), 0);
386 ret = ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, 0x03, value - 1);
390 ret = ov2680_mod_reg(sensor, OV2680_REG_ISP_CTRL00, BIT(7), BIT(7));
397 static int ov2680_gain_set(struct ov2680_dev *sensor, u32 gain)
399 return ov2680_write_reg16(sensor, OV2680_REG_GAIN_PK, gain);
402 static int ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp)
404 return ov2680_write_reg24(sensor, OV2680_REG_EXPOSURE_PK_HIGH,
408 static int ov2680_stream_enable(struct ov2680_dev *sensor)
410 return ov2680_write_reg(sensor, OV2680_REG_STREAM_CTRL, 1);
413 static int ov2680_stream_disable(struct ov2680_dev *sensor)
415 return ov2680_write_reg(sensor, OV2680_REG_STREAM_CTRL, 0);
418 static int ov2680_mode_set(struct ov2680_dev *sensor)
422 ret = ov2680_load_regs(sensor, sensor->current_mode);
426 /* Restore value of all ctrls */
427 ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
431 sensor->mode_pending_changes = false;
436 static int ov2680_mode_restore(struct ov2680_dev *sensor)
440 ret = ov2680_load_regs(sensor, &ov2680_mode_init_data);
444 return ov2680_mode_set(sensor);
447 static int ov2680_power_off(struct ov2680_dev *sensor)
449 if (!sensor->is_enabled)
452 clk_disable_unprepare(sensor->xvclk);
453 ov2680_power_down(sensor);
454 regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
455 sensor->is_enabled = false;
460 static int ov2680_power_on(struct ov2680_dev *sensor)
462 struct device *dev = ov2680_to_dev(sensor);
465 if (sensor->is_enabled)
468 ret = regulator_bulk_enable(OV2680_NUM_SUPPLIES, sensor->supplies);
470 dev_err(dev, "failed to enable regulators: %d\n", ret);
474 if (!sensor->reset_gpio) {
475 ret = ov2680_write_reg(sensor, OV2680_REG_SOFT_RESET, 0x01);
477 dev_err(dev, "sensor soft reset failed\n");
478 goto err_disable_regulators;
480 usleep_range(1000, 2000);
482 ov2680_power_down(sensor);
483 ov2680_power_up(sensor);
486 ret = clk_prepare_enable(sensor->xvclk);
488 goto err_disable_regulators;
490 sensor->is_enabled = true;
492 /* Set clock lane into LP-11 state */
493 ov2680_stream_enable(sensor);
494 usleep_range(1000, 2000);
495 ov2680_stream_disable(sensor);
499 err_disable_regulators:
500 regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
504 static int ov2680_s_power(struct v4l2_subdev *sd, int on)
506 struct ov2680_dev *sensor = to_ov2680_dev(sd);
509 mutex_lock(&sensor->lock);
512 ret = ov2680_power_on(sensor);
514 ret = ov2680_power_off(sensor);
517 ret = ov2680_mode_restore(sensor);
519 mutex_unlock(&sensor->lock);
524 static int ov2680_s_g_frame_interval(struct v4l2_subdev *sd,
525 struct v4l2_subdev_frame_interval *fi)
527 struct ov2680_dev *sensor = to_ov2680_dev(sd);
529 mutex_lock(&sensor->lock);
530 fi->interval = sensor->frame_interval;
531 mutex_unlock(&sensor->lock);
536 static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
538 struct ov2680_dev *sensor = to_ov2680_dev(sd);
541 mutex_lock(&sensor->lock);
543 if (sensor->is_streaming == !!enable)
546 if (enable && sensor->mode_pending_changes) {
547 ret = ov2680_mode_set(sensor);
553 ret = ov2680_stream_enable(sensor);
555 ret = ov2680_stream_disable(sensor);
557 sensor->is_streaming = !!enable;
560 mutex_unlock(&sensor->lock);
565 static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
566 struct v4l2_subdev_state *sd_state,
567 struct v4l2_subdev_mbus_code_enum *code)
569 struct ov2680_dev *sensor = to_ov2680_dev(sd);
571 if (code->pad != 0 || code->index != 0)
574 code->code = sensor->fmt.code;
579 static int ov2680_get_fmt(struct v4l2_subdev *sd,
580 struct v4l2_subdev_state *sd_state,
581 struct v4l2_subdev_format *format)
583 struct ov2680_dev *sensor = to_ov2680_dev(sd);
584 struct v4l2_mbus_framefmt *fmt = NULL;
586 if (format->pad != 0)
589 mutex_lock(&sensor->lock);
591 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
592 fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state,
598 format->format = *fmt;
600 mutex_unlock(&sensor->lock);
605 static int ov2680_set_fmt(struct v4l2_subdev *sd,
606 struct v4l2_subdev_state *sd_state,
607 struct v4l2_subdev_format *format)
609 struct ov2680_dev *sensor = to_ov2680_dev(sd);
610 struct v4l2_mbus_framefmt *try_fmt;
611 const struct ov2680_mode_info *mode;
614 if (format->pad != 0)
617 mode = v4l2_find_nearest_size(ov2680_mode_data,
618 ARRAY_SIZE(ov2680_mode_data),
620 format->format.width,
621 format->format.height);
625 ov2680_fill_format(sensor, &format->format, mode->width, mode->height);
627 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
628 try_fmt = v4l2_subdev_get_try_format(sd, sd_state, 0);
629 *try_fmt = format->format;
633 mutex_lock(&sensor->lock);
635 if (sensor->is_streaming) {
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_state *sd_state)
653 struct ov2680_dev *sensor = to_ov2680_dev(sd);
655 ov2680_fill_format(sensor, &sd_state->pads[0].try_fmt,
656 OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
660 static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
661 struct v4l2_subdev_state *sd_state,
662 struct v4l2_subdev_frame_size_enum *fse)
664 int index = fse->index;
666 if (index >= OV2680_MODE_MAX || index < 0)
669 fse->min_width = ov2680_mode_data[index].width;
670 fse->min_height = ov2680_mode_data[index].height;
671 fse->max_width = ov2680_mode_data[index].width;
672 fse->max_height = ov2680_mode_data[index].height;
677 static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
678 struct v4l2_subdev_state *sd_state,
679 struct v4l2_subdev_frame_interval_enum *fie)
681 struct v4l2_fract tpf;
683 if (fie->index >= OV2680_MODE_MAX || fie->width > OV2680_WIDTH_MAX ||
684 fie->height > OV2680_HEIGHT_MAX ||
685 fie->which > V4L2_SUBDEV_FORMAT_ACTIVE)
688 tpf.denominator = OV2680_FRAME_RATE;
696 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
698 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
699 struct ov2680_dev *sensor = to_ov2680_dev(sd);
701 if (!sensor->is_enabled)
706 return ov2680_gain_set(sensor, ctrl->val);
707 case V4L2_CID_EXPOSURE:
708 return ov2680_exposure_set(sensor, ctrl->val);
710 return ov2680_set_vflip(sensor, ctrl->val);
712 return ov2680_set_hflip(sensor, ctrl->val);
713 case V4L2_CID_TEST_PATTERN:
714 return ov2680_test_pattern_set(sensor, ctrl->val);
722 static const struct v4l2_ctrl_ops ov2680_ctrl_ops = {
723 .s_ctrl = ov2680_s_ctrl,
726 static const struct v4l2_subdev_core_ops ov2680_core_ops = {
727 .s_power = ov2680_s_power,
730 static const struct v4l2_subdev_video_ops ov2680_video_ops = {
731 .g_frame_interval = ov2680_s_g_frame_interval,
732 .s_frame_interval = ov2680_s_g_frame_interval,
733 .s_stream = ov2680_s_stream,
736 static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
737 .init_cfg = ov2680_init_cfg,
738 .enum_mbus_code = ov2680_enum_mbus_code,
739 .get_fmt = ov2680_get_fmt,
740 .set_fmt = ov2680_set_fmt,
741 .enum_frame_size = ov2680_enum_frame_size,
742 .enum_frame_interval = ov2680_enum_frame_interval,
745 static const struct v4l2_subdev_ops ov2680_subdev_ops = {
746 .core = &ov2680_core_ops,
747 .video = &ov2680_video_ops,
748 .pad = &ov2680_pad_ops,
751 static int ov2680_mode_init(struct ov2680_dev *sensor)
753 const struct ov2680_mode_info *init_mode;
755 /* set initial mode */
756 ov2680_fill_format(sensor, &sensor->fmt,
757 OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
759 sensor->frame_interval.denominator = OV2680_FRAME_RATE;
760 sensor->frame_interval.numerator = 1;
762 init_mode = &ov2680_mode_init_data;
764 sensor->current_mode = init_mode;
766 sensor->mode_pending_changes = true;
771 static int ov2680_v4l2_register(struct ov2680_dev *sensor)
773 const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops;
774 struct ov2680_ctrls *ctrls = &sensor->ctrls;
775 struct v4l2_ctrl_handler *hdl = &ctrls->handler;
778 v4l2_i2c_subdev_init(&sensor->sd, sensor->i2c_client,
781 sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
782 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
783 sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
785 ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
789 v4l2_ctrl_handler_init(hdl, 5);
791 hdl->lock = &sensor->lock;
793 ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
794 ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
796 ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(hdl,
797 &ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN,
798 ARRAY_SIZE(test_pattern_menu) - 1,
799 0, 0, test_pattern_menu);
801 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
804 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 2047, 1, 0);
811 ctrls->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
812 ctrls->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
814 sensor->sd.ctrl_handler = hdl;
816 ret = v4l2_async_register_subdev(&sensor->sd);
823 media_entity_cleanup(&sensor->sd.entity);
824 v4l2_ctrl_handler_free(hdl);
829 static int ov2680_get_regulators(struct ov2680_dev *sensor)
833 for (i = 0; i < OV2680_NUM_SUPPLIES; i++)
834 sensor->supplies[i].supply = ov2680_supply_name[i];
836 return devm_regulator_bulk_get(&sensor->i2c_client->dev,
841 static int ov2680_check_id(struct ov2680_dev *sensor)
843 struct device *dev = ov2680_to_dev(sensor);
847 ov2680_power_on(sensor);
849 ret = ov2680_read_reg16(sensor, OV2680_REG_CHIP_ID_HIGH, &chip_id);
851 dev_err(dev, "failed to read chip id high\n");
855 if (chip_id != OV2680_CHIP_ID) {
856 dev_err(dev, "chip id: 0x%04x does not match expected 0x%04x\n",
857 chip_id, OV2680_CHIP_ID);
864 static int ov2680_parse_dt(struct ov2680_dev *sensor)
866 struct device *dev = ov2680_to_dev(sensor);
869 sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
871 ret = PTR_ERR_OR_ZERO(sensor->reset_gpio);
873 dev_dbg(dev, "error while getting reset gpio: %d\n", ret);
877 sensor->xvclk = devm_clk_get(dev, "xvclk");
878 if (IS_ERR(sensor->xvclk)) {
879 dev_err(dev, "xvclk clock missing or invalid\n");
880 return PTR_ERR(sensor->xvclk);
883 sensor->xvclk_freq = clk_get_rate(sensor->xvclk);
884 if (sensor->xvclk_freq != OV2680_XVCLK_VALUE) {
885 dev_err(dev, "wrong xvclk frequency %d HZ, expected: %d Hz\n",
886 sensor->xvclk_freq, OV2680_XVCLK_VALUE);
893 static int ov2680_probe(struct i2c_client *client)
895 struct device *dev = &client->dev;
896 struct ov2680_dev *sensor;
899 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
903 sensor->i2c_client = client;
905 ret = ov2680_parse_dt(sensor);
909 ret = ov2680_mode_init(sensor);
913 ret = ov2680_get_regulators(sensor);
915 dev_err(dev, "failed to get regulators\n");
919 mutex_init(&sensor->lock);
921 ret = ov2680_check_id(sensor);
925 ret = ov2680_v4l2_register(sensor);
929 dev_info(dev, "ov2680 init correctly\n");
934 dev_err(dev, "ov2680 init fail: %d\n", ret);
935 mutex_destroy(&sensor->lock);
940 static void ov2680_remove(struct i2c_client *client)
942 struct v4l2_subdev *sd = i2c_get_clientdata(client);
943 struct ov2680_dev *sensor = to_ov2680_dev(sd);
945 v4l2_async_unregister_subdev(&sensor->sd);
946 mutex_destroy(&sensor->lock);
947 media_entity_cleanup(&sensor->sd.entity);
948 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
951 static int __maybe_unused ov2680_suspend(struct device *dev)
953 struct v4l2_subdev *sd = dev_get_drvdata(dev);
954 struct ov2680_dev *sensor = to_ov2680_dev(sd);
956 if (sensor->is_streaming)
957 ov2680_stream_disable(sensor);
962 static int __maybe_unused ov2680_resume(struct device *dev)
964 struct v4l2_subdev *sd = dev_get_drvdata(dev);
965 struct ov2680_dev *sensor = to_ov2680_dev(sd);
968 if (sensor->is_streaming) {
969 ret = ov2680_stream_enable(sensor);
977 ov2680_stream_disable(sensor);
978 sensor->is_streaming = false;
983 static const struct dev_pm_ops ov2680_pm_ops = {
984 SET_SYSTEM_SLEEP_PM_OPS(ov2680_suspend, ov2680_resume)
987 static const struct of_device_id ov2680_dt_ids[] = {
988 { .compatible = "ovti,ov2680" },
991 MODULE_DEVICE_TABLE(of, ov2680_dt_ids);
993 static struct i2c_driver ov2680_i2c_driver = {
996 .pm = &ov2680_pm_ops,
997 .of_match_table = of_match_ptr(ov2680_dt_ids),
999 .probe_new = ov2680_probe,
1000 .remove = ov2680_remove,
1002 module_i2c_driver(ov2680_i2c_driver);
1004 MODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>");
1005 MODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver");
1006 MODULE_LICENSE("GPL v2");