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 <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>
25 #include <media/v4l2-cci.h>
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-subdev.h>
30 #define OV2680_XVCLK_VALUE 24000000
32 #define OV2680_CHIP_ID 0x2680
34 #define OV2680_REG_STREAM_CTRL CCI_REG8(0x0100)
35 #define OV2680_REG_SOFT_RESET CCI_REG8(0x0103)
37 #define OV2680_REG_CHIP_ID CCI_REG16(0x300a)
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)
47 #define OV2680_REG_ISP_CTRL00 CCI_REG8(0x5080)
49 #define OV2680_FRAME_RATE 30
51 #define OV2680_WIDTH_MAX 1600
52 #define OV2680_HEIGHT_MAX 1200
54 #define OV2680_DEFAULT_WIDTH 800
55 #define OV2680_DEFAULT_HEIGHT 600
58 OV2680_MODE_QUXGA_800_600,
59 OV2680_MODE_720P_1280_720,
60 OV2680_MODE_UXGA_1600_1200,
64 static const char * const ov2680_supply_name[] = {
70 #define OV2680_NUM_SUPPLIES ARRAY_SIZE(ov2680_supply_name)
72 struct ov2680_mode_info {
74 enum ov2680_mode_id id;
77 const struct reg_sequence *reg_data;
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;
92 struct regmap *regmap;
93 struct v4l2_subdev sd;
98 struct regulator_bulk_data supplies[OV2680_NUM_SUPPLIES];
100 struct gpio_desc *pwdn_gpio;
101 struct mutex lock; /* protect members */
105 struct ov2680_ctrls ctrls;
106 struct v4l2_mbus_framefmt fmt;
107 struct v4l2_fract frame_interval;
109 const struct ov2680_mode_info *current_mode;
112 static const char * const test_pattern_menu[] = {
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,
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},
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},
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}
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),
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)},
169 static struct ov2680_dev *to_ov2680_dev(struct v4l2_subdev *sd)
171 return container_of(sd, struct ov2680_dev, sd);
174 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
176 return &container_of(ctrl->handler, struct ov2680_dev,
180 static void ov2680_power_up(struct ov2680_dev *sensor)
182 if (!sensor->pwdn_gpio)
185 gpiod_set_value(sensor->pwdn_gpio, 0);
186 usleep_range(5000, 10000);
189 static void ov2680_power_down(struct ov2680_dev *sensor)
191 if (!sensor->pwdn_gpio)
194 gpiod_set_value(sensor->pwdn_gpio, 1);
195 usleep_range(5000, 10000);
198 static void ov2680_set_bayer_order(struct ov2680_dev *sensor,
199 struct v4l2_mbus_framefmt *fmt)
203 if (sensor->ctrls.vflip && sensor->ctrls.vflip->val)
206 if (sensor->ctrls.hflip && sensor->ctrls.hflip->val)
209 fmt->code = ov2680_hv_flip_bayer_order[hv_flip];
212 static void ov2680_fill_format(struct ov2680_dev *sensor,
213 struct v4l2_mbus_framefmt *fmt,
214 unsigned int width, unsigned int height)
216 memset(fmt, 0, sizeof(*fmt));
218 fmt->height = height;
219 fmt->field = V4L2_FIELD_NONE;
220 fmt->colorspace = V4L2_COLORSPACE_SRGB;
221 ov2680_set_bayer_order(sensor, fmt);
224 static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val)
228 if (sensor->is_streaming)
231 ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT1,
232 BIT(2), val ? BIT(2) : 0, NULL);
236 ov2680_set_bayer_order(sensor, &sensor->fmt);
240 static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val)
244 if (sensor->is_streaming)
247 ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT2,
248 BIT(2), val ? BIT(2) : 0, NULL);
252 ov2680_set_bayer_order(sensor, &sensor->fmt);
256 static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value)
261 return cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
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);
272 static int ov2680_gain_set(struct ov2680_dev *sensor, u32 gain)
274 return cci_write(sensor->regmap, OV2680_REG_GAIN_PK, gain, NULL);
277 static int ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp)
279 return cci_write(sensor->regmap, OV2680_REG_EXPOSURE_PK, exp << 4,
283 static int ov2680_stream_enable(struct ov2680_dev *sensor)
287 ret = regmap_multi_reg_write(sensor->regmap,
288 ov2680_mode_init_data.reg_data,
289 ov2680_mode_init_data.reg_data_size);
293 ret = regmap_multi_reg_write(sensor->regmap,
294 sensor->current_mode->reg_data,
295 sensor->current_mode->reg_data_size);
299 /* Restore value of all ctrls */
300 ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
304 return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 1, NULL);
307 static int ov2680_stream_disable(struct ov2680_dev *sensor)
309 return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 0, NULL);
312 static int ov2680_power_off(struct ov2680_dev *sensor)
314 clk_disable_unprepare(sensor->xvclk);
315 ov2680_power_down(sensor);
316 regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
320 static int ov2680_power_on(struct ov2680_dev *sensor)
324 ret = regulator_bulk_enable(OV2680_NUM_SUPPLIES, sensor->supplies);
326 dev_err(sensor->dev, "failed to enable regulators: %d\n", ret);
330 if (!sensor->pwdn_gpio) {
331 ret = cci_write(sensor->regmap, OV2680_REG_SOFT_RESET, 0x01,
334 dev_err(sensor->dev, "sensor soft reset failed\n");
335 goto err_disable_regulators;
337 usleep_range(1000, 2000);
339 ov2680_power_down(sensor);
340 ov2680_power_up(sensor);
343 ret = clk_prepare_enable(sensor->xvclk);
345 goto err_disable_regulators;
349 err_disable_regulators:
350 regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
354 static int ov2680_s_g_frame_interval(struct v4l2_subdev *sd,
355 struct v4l2_subdev_frame_interval *fi)
357 struct ov2680_dev *sensor = to_ov2680_dev(sd);
359 mutex_lock(&sensor->lock);
360 fi->interval = sensor->frame_interval;
361 mutex_unlock(&sensor->lock);
366 static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
368 struct ov2680_dev *sensor = to_ov2680_dev(sd);
371 mutex_lock(&sensor->lock);
373 if (sensor->is_streaming == !!enable)
377 ret = pm_runtime_resume_and_get(sensor->sd.dev);
381 ret = ov2680_stream_enable(sensor);
383 pm_runtime_put(sensor->sd.dev);
387 ret = ov2680_stream_disable(sensor);
388 pm_runtime_put(sensor->sd.dev);
391 sensor->is_streaming = !!enable;
394 mutex_unlock(&sensor->lock);
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)
403 struct ov2680_dev *sensor = to_ov2680_dev(sd);
405 if (code->pad != 0 || code->index != 0)
408 code->code = sensor->fmt.code;
413 static int ov2680_get_fmt(struct v4l2_subdev *sd,
414 struct v4l2_subdev_state *sd_state,
415 struct v4l2_subdev_format *format)
417 struct ov2680_dev *sensor = to_ov2680_dev(sd);
418 struct v4l2_mbus_framefmt *fmt = NULL;
420 if (format->pad != 0)
423 mutex_lock(&sensor->lock);
425 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
426 fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state,
432 format->format = *fmt;
434 mutex_unlock(&sensor->lock);
439 static int ov2680_set_fmt(struct v4l2_subdev *sd,
440 struct v4l2_subdev_state *sd_state,
441 struct v4l2_subdev_format *format)
443 struct ov2680_dev *sensor = to_ov2680_dev(sd);
444 struct v4l2_mbus_framefmt *try_fmt;
445 const struct ov2680_mode_info *mode;
448 if (format->pad != 0)
451 mode = v4l2_find_nearest_size(ov2680_mode_data,
452 ARRAY_SIZE(ov2680_mode_data),
454 format->format.width,
455 format->format.height);
459 ov2680_fill_format(sensor, &format->format, mode->width, mode->height);
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;
467 mutex_lock(&sensor->lock);
469 if (sensor->is_streaming) {
474 sensor->current_mode = mode;
475 sensor->fmt = format->format;
478 mutex_unlock(&sensor->lock);
483 static int ov2680_init_cfg(struct v4l2_subdev *sd,
484 struct v4l2_subdev_state *sd_state)
486 struct ov2680_dev *sensor = to_ov2680_dev(sd);
488 ov2680_fill_format(sensor, &sd_state->pads[0].try_fmt,
489 OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
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)
497 int index = fse->index;
499 if (index >= OV2680_MODE_MAX || index < 0)
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;
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)
514 struct v4l2_fract tpf;
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)
521 tpf.denominator = OV2680_FRAME_RATE;
529 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
531 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
532 struct ov2680_dev *sensor = to_ov2680_dev(sd);
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);
543 ret = ov2680_gain_set(sensor, ctrl->val);
545 case V4L2_CID_EXPOSURE:
546 ret = ov2680_exposure_set(sensor, ctrl->val);
549 ret = ov2680_set_vflip(sensor, ctrl->val);
552 ret = ov2680_set_hflip(sensor, ctrl->val);
554 case V4L2_CID_TEST_PATTERN:
555 ret = ov2680_test_pattern_set(sensor, ctrl->val);
562 pm_runtime_put(sensor->sd.dev);
566 static const struct v4l2_ctrl_ops ov2680_ctrl_ops = {
567 .s_ctrl = ov2680_s_ctrl,
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,
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,
585 static const struct v4l2_subdev_ops ov2680_subdev_ops = {
586 .video = &ov2680_video_ops,
587 .pad = &ov2680_pad_ops,
590 static int ov2680_mode_init(struct ov2680_dev *sensor)
592 const struct ov2680_mode_info *init_mode;
594 /* set initial mode */
595 ov2680_fill_format(sensor, &sensor->fmt,
596 OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
598 sensor->frame_interval.denominator = OV2680_FRAME_RATE;
599 sensor->frame_interval.numerator = 1;
601 init_mode = &ov2680_mode_init_data;
603 sensor->current_mode = init_mode;
608 static int ov2680_v4l2_register(struct ov2680_dev *sensor)
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;
616 v4l2_i2c_subdev_init(&sensor->sd, client, &ov2680_subdev_ops);
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;
622 ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
626 v4l2_ctrl_handler_init(hdl, 5);
628 hdl->lock = &sensor->lock;
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);
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);
638 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
641 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 2047, 1, 0);
648 ctrls->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
649 ctrls->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
651 sensor->sd.ctrl_handler = hdl;
653 ret = v4l2_async_register_subdev(&sensor->sd);
660 media_entity_cleanup(&sensor->sd.entity);
661 v4l2_ctrl_handler_free(hdl);
666 static int ov2680_get_regulators(struct ov2680_dev *sensor)
670 for (i = 0; i < OV2680_NUM_SUPPLIES; i++)
671 sensor->supplies[i].supply = ov2680_supply_name[i];
673 return devm_regulator_bulk_get(sensor->dev,
674 OV2680_NUM_SUPPLIES, sensor->supplies);
677 static int ov2680_check_id(struct ov2680_dev *sensor)
682 ret = cci_read(sensor->regmap, OV2680_REG_CHIP_ID, &chip_id, NULL);
684 dev_err(sensor->dev, "failed to read chip id\n");
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);
697 static int ov2680_parse_dt(struct ov2680_dev *sensor)
699 struct device *dev = sensor->dev;
700 struct gpio_desc *gpio;
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.
709 gpio = devm_gpiod_get_optional(dev, "powerdown", GPIOD_OUT_HIGH);
711 gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
713 ret = PTR_ERR_OR_ZERO(gpio);
715 dev_dbg(dev, "error while getting reset gpio: %d\n", ret);
719 sensor->pwdn_gpio = gpio;
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);
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);
737 static int ov2680_probe(struct i2c_client *client)
739 struct device *dev = &client->dev;
740 struct ov2680_dev *sensor;
743 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
747 sensor->dev = &client->dev;
749 sensor->regmap = devm_cci_regmap_init_i2c(client, 16);
750 if (IS_ERR(sensor->regmap))
751 return PTR_ERR(sensor->regmap);
753 ret = ov2680_parse_dt(sensor);
757 ret = ov2680_mode_init(sensor);
761 ret = ov2680_get_regulators(sensor);
763 dev_err(dev, "failed to get regulators\n");
767 mutex_init(&sensor->lock);
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.
773 ret = ov2680_power_on(sensor);
777 ret = ov2680_check_id(sensor);
781 pm_runtime_set_active(&client->dev);
782 pm_runtime_get_noresume(&client->dev);
783 pm_runtime_enable(&client->dev);
785 ret = ov2680_v4l2_register(sensor);
789 pm_runtime_set_autosuspend_delay(&client->dev, 1000);
790 pm_runtime_use_autosuspend(&client->dev);
791 pm_runtime_put_autosuspend(&client->dev);
793 dev_info(dev, "ov2680 init correctly\n");
798 pm_runtime_disable(&client->dev);
799 pm_runtime_put_noidle(&client->dev);
801 ov2680_power_off(sensor);
803 dev_err(dev, "ov2680 init fail: %d\n", ret);
804 mutex_destroy(&sensor->lock);
809 static void ov2680_remove(struct i2c_client *client)
811 struct v4l2_subdev *sd = i2c_get_clientdata(client);
812 struct ov2680_dev *sensor = to_ov2680_dev(sd);
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);
820 * Disable runtime PM. In case runtime PM is disabled in the kernel,
821 * make sure to turn power off manually.
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);
829 static int ov2680_suspend(struct device *dev)
831 struct v4l2_subdev *sd = dev_get_drvdata(dev);
832 struct ov2680_dev *sensor = to_ov2680_dev(sd);
834 if (sensor->is_streaming)
835 ov2680_stream_disable(sensor);
837 return ov2680_power_off(sensor);
840 static int ov2680_resume(struct device *dev)
842 struct v4l2_subdev *sd = dev_get_drvdata(dev);
843 struct ov2680_dev *sensor = to_ov2680_dev(sd);
846 ret = ov2680_power_on(sensor);
850 if (sensor->is_streaming) {
851 ret = ov2680_stream_enable(sensor);
859 ov2680_stream_disable(sensor);
860 sensor->is_streaming = false;
865 static DEFINE_RUNTIME_DEV_PM_OPS(ov2680_pm_ops, ov2680_suspend, ov2680_resume,
868 static const struct of_device_id ov2680_dt_ids[] = {
869 { .compatible = "ovti,ov2680" },
872 MODULE_DEVICE_TABLE(of, ov2680_dt_ids);
874 static struct i2c_driver ov2680_i2c_driver = {
877 .pm = pm_sleep_ptr(&ov2680_pm_ops),
878 .of_match_table = ov2680_dt_ids,
880 .probe = ov2680_probe,
881 .remove = ov2680_remove,
883 module_i2c_driver(ov2680_i2c_driver);
885 MODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>");
886 MODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver");
887 MODULE_LICENSE("GPL v2");