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_CHIP_ID 0x2680
32 #define OV2680_REG_STREAM_CTRL CCI_REG8(0x0100)
33 #define OV2680_REG_SOFT_RESET CCI_REG8(0x0103)
35 #define OV2680_REG_CHIP_ID CCI_REG16(0x300a)
36 #define OV2680_REG_PLL_MULTIPLIER CCI_REG16(0x3081)
38 #define OV2680_REG_EXPOSURE_PK CCI_REG24(0x3500)
39 #define OV2680_REG_R_MANUAL CCI_REG8(0x3503)
40 #define OV2680_REG_GAIN_PK CCI_REG16(0x350a)
41 #define OV2680_REG_TIMING_HTS CCI_REG16(0x380c)
42 #define OV2680_REG_TIMING_VTS CCI_REG16(0x380e)
43 #define OV2680_REG_FORMAT1 CCI_REG8(0x3820)
44 #define OV2680_REG_FORMAT2 CCI_REG8(0x3821)
46 #define OV2680_REG_ISP_CTRL00 CCI_REG8(0x5080)
48 #define OV2680_FRAME_RATE 30
50 #define OV2680_WIDTH_MAX 1600
51 #define OV2680_HEIGHT_MAX 1200
53 #define OV2680_DEFAULT_WIDTH 800
54 #define OV2680_DEFAULT_HEIGHT 600
57 OV2680_MODE_QUXGA_800_600,
58 OV2680_MODE_720P_1280_720,
59 OV2680_MODE_UXGA_1600_1200,
63 static const char * const ov2680_supply_name[] = {
69 #define OV2680_NUM_SUPPLIES ARRAY_SIZE(ov2680_supply_name)
76 static const unsigned long ov2680_xvclk_freqs[] = {
77 [OV2680_19_2_MHZ] = 19200000,
78 [OV2680_24_MHZ] = 24000000,
81 static const u8 ov2680_pll_multipliers[] = {
82 [OV2680_19_2_MHZ] = 69,
86 struct ov2680_mode_info {
88 enum ov2680_mode_id id;
91 const struct reg_sequence *reg_data;
96 struct v4l2_ctrl_handler handler;
97 struct v4l2_ctrl *exposure;
98 struct v4l2_ctrl *gain;
99 struct v4l2_ctrl *hflip;
100 struct v4l2_ctrl *vflip;
101 struct v4l2_ctrl *test_pattern;
105 struct v4l2_mbus_framefmt fmt;
106 struct v4l2_fract frame_interval;
111 struct regmap *regmap;
112 struct v4l2_subdev sd;
114 struct media_pad pad;
118 struct regulator_bulk_data supplies[OV2680_NUM_SUPPLIES];
120 struct gpio_desc *pwdn_gpio;
121 struct mutex lock; /* protect members */
125 struct ov2680_ctrls ctrls;
126 struct ov2680_mode mode;
128 const struct ov2680_mode_info *current_mode;
131 static const char * const test_pattern_menu[] = {
139 static const int ov2680_hv_flip_bayer_order[] = {
140 MEDIA_BUS_FMT_SBGGR10_1X10,
141 MEDIA_BUS_FMT_SGRBG10_1X10,
142 MEDIA_BUS_FMT_SGBRG10_1X10,
143 MEDIA_BUS_FMT_SRGGB10_1X10,
146 static const struct reg_sequence ov2680_setting_30fps_QUXGA_800_600[] = {
147 /* Set PLL SP DIV to 1 for binning mode */
150 /* Sensor control register 0x0a to 0x23 for binning mode */
153 /* Set X and Y output size to 800x600 */
159 /* Set HTS + VTS to 1708x644 */
165 /* Set ISP WIN X and Y start to 4x4 */
169 /* Set X INC and Y INC for binning */
173 /* Initialize FORMAT1 to default/reset value (vflip disabled) */
176 /* Set black level compensation range to 0 - 3 (default 0 - 11) */
180 /* Set MIPI pclk period to 0x1e (default/reset is 0x18) */
183 /* Initialize exposure to 0x4ee (overridden by the ctrl, drop this */
187 /* R MANUAL set exposure and gain to manual (hw does not do auto) */
191 static const struct reg_sequence ov2680_setting_30fps_720P_1280_720[] = {
192 /* Set PLL SP DIV to 0 for not binning mode */
195 /* Set X and Y output size to 1280x720 */
201 /* Set HTS + VTS to 1704x1294 */
207 /* Set ISP WIN X and Y start to 8x6 */
211 /* Set X INC and Y INC for non binning */
215 /* Initialize FORMAT1 to default/reset value (vflip disabled) */
218 /* Set backlight compensation range start to 0 */
222 static const struct reg_sequence ov2680_setting_30fps_UXGA_1600_1200[] = {
223 /* Set PLL SP DIV to 0 for not binning mode */
226 /* Initialize exposure to 0x4ee (overridden by the ctrl, drop this */
230 /* Set X and Y output size to 1600x1200 */
236 /* Set HTS + VTS to 1704x1294 */
242 /* Set ISP WIN X and Y start to 0x0 */
246 /* Set X INC and Y INC for non binning */
250 /* Initialize FORMAT1 to default/reset value (vflip disabled) */
253 /* Set backlight compensation range start to 0 */
256 /* Set MIPI pclk period to default/reset value of 0x18 */
260 static const struct ov2680_mode_info ov2680_mode_init_data = {
261 "mode_quxga_800_600", OV2680_MODE_QUXGA_800_600, 800, 600,
262 ov2680_setting_30fps_QUXGA_800_600,
263 ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600),
266 static const struct ov2680_mode_info ov2680_mode_data[OV2680_MODE_MAX] = {
267 {"mode_quxga_800_600", OV2680_MODE_QUXGA_800_600,
268 800, 600, ov2680_setting_30fps_QUXGA_800_600,
269 ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600)},
270 {"mode_720p_1280_720", OV2680_MODE_720P_1280_720,
271 1280, 720, ov2680_setting_30fps_720P_1280_720,
272 ARRAY_SIZE(ov2680_setting_30fps_720P_1280_720)},
273 {"mode_uxga_1600_1200", OV2680_MODE_UXGA_1600_1200,
274 1600, 1200, ov2680_setting_30fps_UXGA_1600_1200,
275 ARRAY_SIZE(ov2680_setting_30fps_UXGA_1600_1200)},
278 static struct ov2680_dev *to_ov2680_dev(struct v4l2_subdev *sd)
280 return container_of(sd, struct ov2680_dev, sd);
283 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
285 return &container_of(ctrl->handler, struct ov2680_dev,
289 static void ov2680_power_up(struct ov2680_dev *sensor)
291 if (!sensor->pwdn_gpio)
294 gpiod_set_value(sensor->pwdn_gpio, 0);
295 usleep_range(5000, 10000);
298 static void ov2680_power_down(struct ov2680_dev *sensor)
300 if (!sensor->pwdn_gpio)
303 gpiod_set_value(sensor->pwdn_gpio, 1);
304 usleep_range(5000, 10000);
307 static void ov2680_set_bayer_order(struct ov2680_dev *sensor,
308 struct v4l2_mbus_framefmt *fmt)
312 if (sensor->ctrls.vflip && sensor->ctrls.vflip->val)
315 if (sensor->ctrls.hflip && sensor->ctrls.hflip->val)
318 fmt->code = ov2680_hv_flip_bayer_order[hv_flip];
321 static void ov2680_fill_format(struct ov2680_dev *sensor,
322 struct v4l2_mbus_framefmt *fmt,
323 unsigned int width, unsigned int height)
325 memset(fmt, 0, sizeof(*fmt));
327 fmt->height = height;
328 fmt->field = V4L2_FIELD_NONE;
329 fmt->colorspace = V4L2_COLORSPACE_SRGB;
330 ov2680_set_bayer_order(sensor, fmt);
333 static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val)
337 if (sensor->is_streaming)
340 ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT1,
341 BIT(2), val ? BIT(2) : 0, NULL);
345 ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
349 static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val)
353 if (sensor->is_streaming)
356 ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT2,
357 BIT(2), val ? BIT(2) : 0, NULL);
361 ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
365 static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value)
370 return cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
373 cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
374 0x03, value - 1, &ret);
375 cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
376 BIT(7), BIT(7), &ret);
381 static int ov2680_gain_set(struct ov2680_dev *sensor, u32 gain)
383 return cci_write(sensor->regmap, OV2680_REG_GAIN_PK, gain, NULL);
386 static int ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp)
388 return cci_write(sensor->regmap, OV2680_REG_EXPOSURE_PK, exp << 4,
392 static int ov2680_stream_enable(struct ov2680_dev *sensor)
396 ret = cci_write(sensor->regmap, OV2680_REG_PLL_MULTIPLIER,
397 sensor->pll_mult, NULL);
401 ret = regmap_multi_reg_write(sensor->regmap,
402 ov2680_mode_init_data.reg_data,
403 ov2680_mode_init_data.reg_data_size);
407 ret = regmap_multi_reg_write(sensor->regmap,
408 sensor->current_mode->reg_data,
409 sensor->current_mode->reg_data_size);
413 /* Restore value of all ctrls */
414 ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
418 return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 1, NULL);
421 static int ov2680_stream_disable(struct ov2680_dev *sensor)
423 return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 0, NULL);
426 static int ov2680_power_off(struct ov2680_dev *sensor)
428 clk_disable_unprepare(sensor->xvclk);
429 ov2680_power_down(sensor);
430 regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
434 static int ov2680_power_on(struct ov2680_dev *sensor)
438 ret = regulator_bulk_enable(OV2680_NUM_SUPPLIES, sensor->supplies);
440 dev_err(sensor->dev, "failed to enable regulators: %d\n", ret);
444 if (!sensor->pwdn_gpio) {
445 ret = cci_write(sensor->regmap, OV2680_REG_SOFT_RESET, 0x01,
448 dev_err(sensor->dev, "sensor soft reset failed\n");
449 goto err_disable_regulators;
451 usleep_range(1000, 2000);
453 ov2680_power_down(sensor);
454 ov2680_power_up(sensor);
457 ret = clk_prepare_enable(sensor->xvclk);
459 goto err_disable_regulators;
463 err_disable_regulators:
464 regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
468 static int ov2680_s_g_frame_interval(struct v4l2_subdev *sd,
469 struct v4l2_subdev_frame_interval *fi)
471 struct ov2680_dev *sensor = to_ov2680_dev(sd);
473 mutex_lock(&sensor->lock);
474 fi->interval = sensor->mode.frame_interval;
475 mutex_unlock(&sensor->lock);
480 static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
482 struct ov2680_dev *sensor = to_ov2680_dev(sd);
485 mutex_lock(&sensor->lock);
487 if (sensor->is_streaming == !!enable)
491 ret = pm_runtime_resume_and_get(sensor->sd.dev);
495 ret = ov2680_stream_enable(sensor);
497 pm_runtime_put(sensor->sd.dev);
501 ret = ov2680_stream_disable(sensor);
502 pm_runtime_put(sensor->sd.dev);
505 sensor->is_streaming = !!enable;
508 mutex_unlock(&sensor->lock);
513 static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
514 struct v4l2_subdev_state *sd_state,
515 struct v4l2_subdev_mbus_code_enum *code)
517 struct ov2680_dev *sensor = to_ov2680_dev(sd);
519 if (code->pad != 0 || code->index != 0)
522 code->code = sensor->mode.fmt.code;
527 static int ov2680_get_fmt(struct v4l2_subdev *sd,
528 struct v4l2_subdev_state *sd_state,
529 struct v4l2_subdev_format *format)
531 struct ov2680_dev *sensor = to_ov2680_dev(sd);
532 struct v4l2_mbus_framefmt *fmt = NULL;
534 if (format->pad != 0)
537 mutex_lock(&sensor->lock);
539 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
540 fmt = v4l2_subdev_get_try_format(&sensor->sd, sd_state,
543 fmt = &sensor->mode.fmt;
546 format->format = *fmt;
548 mutex_unlock(&sensor->lock);
553 static int ov2680_set_fmt(struct v4l2_subdev *sd,
554 struct v4l2_subdev_state *sd_state,
555 struct v4l2_subdev_format *format)
557 struct ov2680_dev *sensor = to_ov2680_dev(sd);
558 struct v4l2_mbus_framefmt *try_fmt;
559 const struct ov2680_mode_info *mode;
562 if (format->pad != 0)
565 mode = v4l2_find_nearest_size(ov2680_mode_data,
566 ARRAY_SIZE(ov2680_mode_data),
568 format->format.width,
569 format->format.height);
573 ov2680_fill_format(sensor, &format->format, mode->width, mode->height);
575 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
576 try_fmt = v4l2_subdev_get_try_format(sd, sd_state, 0);
577 *try_fmt = format->format;
581 mutex_lock(&sensor->lock);
583 if (sensor->is_streaming) {
588 sensor->current_mode = mode;
589 sensor->mode.fmt = format->format;
592 mutex_unlock(&sensor->lock);
597 static int ov2680_init_cfg(struct v4l2_subdev *sd,
598 struct v4l2_subdev_state *sd_state)
600 struct ov2680_dev *sensor = to_ov2680_dev(sd);
602 ov2680_fill_format(sensor, &sd_state->pads[0].try_fmt,
603 OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
607 static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
608 struct v4l2_subdev_state *sd_state,
609 struct v4l2_subdev_frame_size_enum *fse)
611 int index = fse->index;
613 if (index >= OV2680_MODE_MAX || index < 0)
616 fse->min_width = ov2680_mode_data[index].width;
617 fse->min_height = ov2680_mode_data[index].height;
618 fse->max_width = ov2680_mode_data[index].width;
619 fse->max_height = ov2680_mode_data[index].height;
624 static bool ov2680_valid_frame_size(struct v4l2_subdev_frame_interval_enum *fie)
628 for (i = 0; i < OV2680_MODE_MAX; i++) {
629 if (fie->width == ov2680_mode_data[i].width &&
630 fie->height == ov2680_mode_data[i].height)
637 static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
638 struct v4l2_subdev_state *sd_state,
639 struct v4l2_subdev_frame_interval_enum *fie)
641 struct ov2680_dev *sensor = to_ov2680_dev(sd);
643 /* Only 1 framerate */
644 if (fie->index || !ov2680_valid_frame_size(fie))
647 fie->interval = sensor->mode.frame_interval;
652 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
654 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
655 struct ov2680_dev *sensor = to_ov2680_dev(sd);
658 /* Only apply changes to the controls if the device is powered up */
659 if (!pm_runtime_get_if_in_use(sensor->sd.dev)) {
660 ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
666 ret = ov2680_gain_set(sensor, ctrl->val);
668 case V4L2_CID_EXPOSURE:
669 ret = ov2680_exposure_set(sensor, ctrl->val);
672 ret = ov2680_set_vflip(sensor, ctrl->val);
675 ret = ov2680_set_hflip(sensor, ctrl->val);
677 case V4L2_CID_TEST_PATTERN:
678 ret = ov2680_test_pattern_set(sensor, ctrl->val);
685 pm_runtime_put(sensor->sd.dev);
689 static const struct v4l2_ctrl_ops ov2680_ctrl_ops = {
690 .s_ctrl = ov2680_s_ctrl,
693 static const struct v4l2_subdev_video_ops ov2680_video_ops = {
694 .g_frame_interval = ov2680_s_g_frame_interval,
695 .s_frame_interval = ov2680_s_g_frame_interval,
696 .s_stream = ov2680_s_stream,
699 static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
700 .init_cfg = ov2680_init_cfg,
701 .enum_mbus_code = ov2680_enum_mbus_code,
702 .get_fmt = ov2680_get_fmt,
703 .set_fmt = ov2680_set_fmt,
704 .enum_frame_size = ov2680_enum_frame_size,
705 .enum_frame_interval = ov2680_enum_frame_interval,
708 static const struct v4l2_subdev_ops ov2680_subdev_ops = {
709 .video = &ov2680_video_ops,
710 .pad = &ov2680_pad_ops,
713 static int ov2680_mode_init(struct ov2680_dev *sensor)
715 const struct ov2680_mode_info *init_mode;
717 /* set initial mode */
718 ov2680_fill_format(sensor, &sensor->mode.fmt,
719 OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
721 sensor->mode.frame_interval.denominator = OV2680_FRAME_RATE;
722 sensor->mode.frame_interval.numerator = 1;
724 init_mode = &ov2680_mode_init_data;
726 sensor->current_mode = init_mode;
731 static int ov2680_v4l2_register(struct ov2680_dev *sensor)
733 struct i2c_client *client = to_i2c_client(sensor->dev);
734 const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops;
735 struct ov2680_ctrls *ctrls = &sensor->ctrls;
736 struct v4l2_ctrl_handler *hdl = &ctrls->handler;
739 v4l2_i2c_subdev_init(&sensor->sd, client, &ov2680_subdev_ops);
741 sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
742 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
743 sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
745 ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
749 v4l2_ctrl_handler_init(hdl, 5);
751 hdl->lock = &sensor->lock;
753 ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
754 ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
756 ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(hdl,
757 &ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN,
758 ARRAY_SIZE(test_pattern_menu) - 1,
759 0, 0, test_pattern_menu);
761 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
764 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 0, 2047, 1, 0);
771 ctrls->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
772 ctrls->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
774 sensor->sd.ctrl_handler = hdl;
776 ret = v4l2_async_register_subdev(&sensor->sd);
783 media_entity_cleanup(&sensor->sd.entity);
784 v4l2_ctrl_handler_free(hdl);
789 static int ov2680_get_regulators(struct ov2680_dev *sensor)
793 for (i = 0; i < OV2680_NUM_SUPPLIES; i++)
794 sensor->supplies[i].supply = ov2680_supply_name[i];
796 return devm_regulator_bulk_get(sensor->dev,
797 OV2680_NUM_SUPPLIES, sensor->supplies);
800 static int ov2680_check_id(struct ov2680_dev *sensor)
805 ret = cci_read(sensor->regmap, OV2680_REG_CHIP_ID, &chip_id, NULL);
807 dev_err(sensor->dev, "failed to read chip id\n");
811 if (chip_id != OV2680_CHIP_ID) {
812 dev_err(sensor->dev, "chip id: 0x%04llx does not match expected 0x%04x\n",
813 chip_id, OV2680_CHIP_ID);
820 static int ov2680_parse_dt(struct ov2680_dev *sensor)
822 struct device *dev = sensor->dev;
823 struct fwnode_handle *ep_fwnode;
824 struct gpio_desc *gpio;
825 unsigned int rate = 0;
829 * Sometimes the fwnode graph is initialized by the bridge driver.
830 * Bridge drivers doing this may also add GPIO mappings, wait for this.
832 ep_fwnode = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
834 return dev_err_probe(dev, -EPROBE_DEFER,
835 "waiting for fwnode graph endpoint\n");
837 fwnode_handle_put(ep_fwnode);
840 * The pin we want is named XSHUTDN in the datasheet. Linux sensor
841 * drivers have standardized on using "powerdown" as con-id name
842 * for powerdown or shutdown pins. Older DTB files use "reset",
843 * so fallback to that if there is no "powerdown" pin.
845 gpio = devm_gpiod_get_optional(dev, "powerdown", GPIOD_OUT_HIGH);
847 gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
849 ret = PTR_ERR_OR_ZERO(gpio);
851 dev_dbg(dev, "error while getting reset gpio: %d\n", ret);
855 sensor->pwdn_gpio = gpio;
857 sensor->xvclk = devm_clk_get_optional(dev, "xvclk");
858 if (IS_ERR(sensor->xvclk)) {
859 dev_err(dev, "xvclk clock missing or invalid\n");
860 return PTR_ERR(sensor->xvclk);
864 * We could have either a 24MHz or 19.2MHz clock rate from either DT or
865 * ACPI... but we also need to support the weird IPU3 case which will
866 * have an external clock AND a clock-frequency property. Check for the
867 * clock-frequency property and if found, set that rate if we managed
868 * to acquire a clock. This should cover the ACPI case. If the system
869 * uses devicetree then the configured rate should already be set, so
870 * we can just read it.
872 ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency",
874 if (ret && !sensor->xvclk)
875 return dev_err_probe(dev, ret, "invalid clock config\n");
877 if (!ret && sensor->xvclk) {
878 ret = clk_set_rate(sensor->xvclk, rate);
880 return dev_err_probe(dev, ret,
881 "failed to set clock rate\n");
884 sensor->xvclk_freq = rate ?: clk_get_rate(sensor->xvclk);
886 for (i = 0; i < ARRAY_SIZE(ov2680_xvclk_freqs); i++) {
887 if (sensor->xvclk_freq == ov2680_xvclk_freqs[i])
891 if (i == ARRAY_SIZE(ov2680_xvclk_freqs))
892 return dev_err_probe(dev, -EINVAL,
893 "unsupported xvclk frequency %d Hz\n",
896 sensor->pll_mult = ov2680_pll_multipliers[i];
901 static int ov2680_probe(struct i2c_client *client)
903 struct device *dev = &client->dev;
904 struct ov2680_dev *sensor;
907 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
911 sensor->dev = &client->dev;
913 sensor->regmap = devm_cci_regmap_init_i2c(client, 16);
914 if (IS_ERR(sensor->regmap))
915 return PTR_ERR(sensor->regmap);
917 ret = ov2680_parse_dt(sensor);
921 ret = ov2680_mode_init(sensor);
925 ret = ov2680_get_regulators(sensor);
927 dev_err(dev, "failed to get regulators\n");
931 mutex_init(&sensor->lock);
934 * Power up and verify the chip now, so that if runtime pm is
935 * disabled the chip is left on and streaming will work.
937 ret = ov2680_power_on(sensor);
941 ret = ov2680_check_id(sensor);
945 pm_runtime_set_active(&client->dev);
946 pm_runtime_get_noresume(&client->dev);
947 pm_runtime_enable(&client->dev);
949 ret = ov2680_v4l2_register(sensor);
953 pm_runtime_set_autosuspend_delay(&client->dev, 1000);
954 pm_runtime_use_autosuspend(&client->dev);
955 pm_runtime_put_autosuspend(&client->dev);
957 dev_info(dev, "ov2680 init correctly\n");
962 pm_runtime_disable(&client->dev);
963 pm_runtime_put_noidle(&client->dev);
965 ov2680_power_off(sensor);
967 dev_err(dev, "ov2680 init fail: %d\n", ret);
968 mutex_destroy(&sensor->lock);
973 static void ov2680_remove(struct i2c_client *client)
975 struct v4l2_subdev *sd = i2c_get_clientdata(client);
976 struct ov2680_dev *sensor = to_ov2680_dev(sd);
978 v4l2_async_unregister_subdev(&sensor->sd);
979 mutex_destroy(&sensor->lock);
980 media_entity_cleanup(&sensor->sd.entity);
981 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
984 * Disable runtime PM. In case runtime PM is disabled in the kernel,
985 * make sure to turn power off manually.
987 pm_runtime_disable(&client->dev);
988 if (!pm_runtime_status_suspended(&client->dev))
989 ov2680_power_off(sensor);
990 pm_runtime_set_suspended(&client->dev);
993 static int ov2680_suspend(struct device *dev)
995 struct v4l2_subdev *sd = dev_get_drvdata(dev);
996 struct ov2680_dev *sensor = to_ov2680_dev(sd);
998 if (sensor->is_streaming)
999 ov2680_stream_disable(sensor);
1001 return ov2680_power_off(sensor);
1004 static int ov2680_resume(struct device *dev)
1006 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1007 struct ov2680_dev *sensor = to_ov2680_dev(sd);
1010 ret = ov2680_power_on(sensor);
1012 goto stream_disable;
1014 if (sensor->is_streaming) {
1015 ret = ov2680_stream_enable(sensor);
1017 goto stream_disable;
1023 ov2680_stream_disable(sensor);
1024 sensor->is_streaming = false;
1029 static DEFINE_RUNTIME_DEV_PM_OPS(ov2680_pm_ops, ov2680_suspend, ov2680_resume,
1032 static const struct of_device_id ov2680_dt_ids[] = {
1033 { .compatible = "ovti,ov2680" },
1036 MODULE_DEVICE_TABLE(of, ov2680_dt_ids);
1038 static const struct acpi_device_id ov2680_acpi_ids[] = {
1042 MODULE_DEVICE_TABLE(acpi, ov2680_acpi_ids);
1044 static struct i2c_driver ov2680_i2c_driver = {
1047 .pm = pm_sleep_ptr(&ov2680_pm_ops),
1048 .of_match_table = ov2680_dt_ids,
1049 .acpi_match_table = ov2680_acpi_ids,
1051 .probe = ov2680_probe,
1052 .remove = ov2680_remove,
1054 module_i2c_driver(ov2680_i2c_driver);
1056 MODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>");
1057 MODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver");
1058 MODULE_LICENSE("GPL v2");