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
;
89 struct v4l2_ctrl
*auto_exp
;
90 struct v4l2_ctrl
*exposure
;
93 struct v4l2_ctrl
*auto_gain
;
94 struct v4l2_ctrl
*gain
;
97 struct v4l2_ctrl
*hflip
;
98 struct v4l2_ctrl
*vflip
;
99 struct v4l2_ctrl
*test_pattern
;
103 struct i2c_client
*i2c_client
;
104 struct v4l2_subdev sd
;
106 struct media_pad pad
;
109 struct regulator_bulk_data supplies
[OV2680_NUM_SUPPLIES
];
111 struct gpio_desc
*reset_gpio
;
112 struct mutex lock
; /* protect members */
114 bool mode_pending_changes
;
118 struct ov2680_ctrls ctrls
;
119 struct v4l2_mbus_framefmt fmt
;
120 struct v4l2_fract frame_interval
;
122 const struct ov2680_mode_info
*current_mode
;
125 static const char * const test_pattern_menu
[] = {
133 static const int ov2680_hv_flip_bayer_order
[] = {
134 MEDIA_BUS_FMT_SBGGR10_1X10
,
135 MEDIA_BUS_FMT_SGRBG10_1X10
,
136 MEDIA_BUS_FMT_SGBRG10_1X10
,
137 MEDIA_BUS_FMT_SRGGB10_1X10
,
140 static const struct reg_value ov2680_setting_30fps_QUXGA_800_600
[] = {
141 {0x3086, 0x01}, {0x370a, 0x23}, {0x3808, 0x03}, {0x3809, 0x20},
142 {0x380a, 0x02}, {0x380b, 0x58}, {0x380c, 0x06}, {0x380d, 0xac},
143 {0x380e, 0x02}, {0x380f, 0x84}, {0x3811, 0x04}, {0x3813, 0x04},
144 {0x3814, 0x31}, {0x3815, 0x31}, {0x3820, 0xc0}, {0x4008, 0x00},
145 {0x4009, 0x03}, {0x4837, 0x1e}, {0x3501, 0x4e}, {0x3502, 0xe0},
148 static const struct reg_value ov2680_setting_30fps_720P_1280_720
[] = {
149 {0x3086, 0x00}, {0x3808, 0x05}, {0x3809, 0x00}, {0x380a, 0x02},
150 {0x380b, 0xd0}, {0x380c, 0x06}, {0x380d, 0xa8}, {0x380e, 0x05},
151 {0x380f, 0x0e}, {0x3811, 0x08}, {0x3813, 0x06}, {0x3814, 0x11},
152 {0x3815, 0x11}, {0x3820, 0xc0}, {0x4008, 0x00},
155 static const struct reg_value ov2680_setting_30fps_UXGA_1600_1200
[] = {
156 {0x3086, 0x00}, {0x3501, 0x4e}, {0x3502, 0xe0}, {0x3808, 0x06},
157 {0x3809, 0x40}, {0x380a, 0x04}, {0x380b, 0xb0}, {0x380c, 0x06},
158 {0x380d, 0xa8}, {0x380e, 0x05}, {0x380f, 0x0e}, {0x3811, 0x00},
159 {0x3813, 0x00}, {0x3814, 0x11}, {0x3815, 0x11}, {0x3820, 0xc0},
160 {0x4008, 0x00}, {0x4837, 0x18}
163 static const struct ov2680_mode_info ov2680_mode_init_data
= {
164 "mode_quxga_800_600", OV2680_MODE_QUXGA_800_600
, 800, 600,
165 ov2680_setting_30fps_QUXGA_800_600
,
166 ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600
),
169 static const struct ov2680_mode_info ov2680_mode_data
[OV2680_MODE_MAX
] = {
170 {"mode_quxga_800_600", OV2680_MODE_QUXGA_800_600
,
171 800, 600, ov2680_setting_30fps_QUXGA_800_600
,
172 ARRAY_SIZE(ov2680_setting_30fps_QUXGA_800_600
)},
173 {"mode_720p_1280_720", OV2680_MODE_720P_1280_720
,
174 1280, 720, ov2680_setting_30fps_720P_1280_720
,
175 ARRAY_SIZE(ov2680_setting_30fps_720P_1280_720
)},
176 {"mode_uxga_1600_1200", OV2680_MODE_UXGA_1600_1200
,
177 1600, 1200, ov2680_setting_30fps_UXGA_1600_1200
,
178 ARRAY_SIZE(ov2680_setting_30fps_UXGA_1600_1200
)},
181 static struct ov2680_dev
*to_ov2680_dev(struct v4l2_subdev
*sd
)
183 return container_of(sd
, struct ov2680_dev
, sd
);
186 static struct device
*ov2680_to_dev(struct ov2680_dev
*sensor
)
188 return &sensor
->i2c_client
->dev
;
191 static inline struct v4l2_subdev
*ctrl_to_sd(struct v4l2_ctrl
*ctrl
)
193 return &container_of(ctrl
->handler
, struct ov2680_dev
,
197 static int __ov2680_write_reg(struct ov2680_dev
*sensor
, u16 reg
,
198 unsigned int len
, u32 val
)
200 struct i2c_client
*client
= sensor
->i2c_client
;
207 put_unaligned_be16(reg
, buf
);
208 put_unaligned_be32(val
<< (8 * (4 - len
)), buf
+ 2);
209 ret
= i2c_master_send(client
, buf
, len
+ 2);
210 if (ret
!= len
+ 2) {
211 dev_err(&client
->dev
, "write error: reg=0x%4x: %d\n", reg
, ret
);
218 #define ov2680_write_reg(s, r, v) \
219 __ov2680_write_reg(s, r, OV2680_REG_VALUE_8BIT, v)
221 #define ov2680_write_reg16(s, r, v) \
222 __ov2680_write_reg(s, r, OV2680_REG_VALUE_16BIT, v)
224 #define ov2680_write_reg24(s, r, v) \
225 __ov2680_write_reg(s, r, OV2680_REG_VALUE_24BIT, v)
227 static int __ov2680_read_reg(struct ov2680_dev
*sensor
, u16 reg
,
228 unsigned int len
, u32
*val
)
230 struct i2c_client
*client
= sensor
->i2c_client
;
231 struct i2c_msg msgs
[2];
232 u8 addr_buf
[2] = { reg
>> 8, reg
& 0xff };
233 u8 data_buf
[4] = { 0, };
239 msgs
[0].addr
= client
->addr
;
241 msgs
[0].len
= ARRAY_SIZE(addr_buf
);
242 msgs
[0].buf
= addr_buf
;
244 msgs
[1].addr
= client
->addr
;
245 msgs
[1].flags
= I2C_M_RD
;
247 msgs
[1].buf
= &data_buf
[4 - len
];
249 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
250 if (ret
!= ARRAY_SIZE(msgs
)) {
251 dev_err(&client
->dev
, "read error: reg=0x%4x: %d\n", reg
, ret
);
255 *val
= get_unaligned_be32(data_buf
);
260 #define ov2680_read_reg(s, r, v) \
261 __ov2680_read_reg(s, r, OV2680_REG_VALUE_8BIT, v)
263 #define ov2680_read_reg16(s, r, v) \
264 __ov2680_read_reg(s, r, OV2680_REG_VALUE_16BIT, v)
266 #define ov2680_read_reg24(s, r, v) \
267 __ov2680_read_reg(s, r, OV2680_REG_VALUE_24BIT, v)
269 static int ov2680_mod_reg(struct ov2680_dev
*sensor
, u16 reg
, u8 mask
, u8 val
)
274 ret
= ov2680_read_reg(sensor
, reg
, &readval
);
282 return ov2680_write_reg(sensor
, reg
, val
);
285 static int ov2680_load_regs(struct ov2680_dev
*sensor
,
286 const struct ov2680_mode_info
*mode
)
288 const struct reg_value
*regs
= mode
->reg_data
;
294 for (i
= 0; i
< mode
->reg_data_size
; ++i
, ++regs
) {
295 reg_addr
= regs
->reg_addr
;
298 ret
= ov2680_write_reg(sensor
, reg_addr
, val
);
306 static void ov2680_power_up(struct ov2680_dev
*sensor
)
308 if (!sensor
->reset_gpio
)
311 gpiod_set_value(sensor
->reset_gpio
, 0);
312 usleep_range(5000, 10000);
315 static void ov2680_power_down(struct ov2680_dev
*sensor
)
317 if (!sensor
->reset_gpio
)
320 gpiod_set_value(sensor
->reset_gpio
, 1);
321 usleep_range(5000, 10000);
324 static int ov2680_bayer_order(struct ov2680_dev
*sensor
)
331 ret
= ov2680_read_reg(sensor
, OV2680_REG_FORMAT1
, &format1
);
335 ret
= ov2680_read_reg(sensor
, OV2680_REG_FORMAT2
, &format2
);
339 hv_flip
= (format2
& BIT(2) << 1) | (format1
& BIT(2));
341 sensor
->fmt
.code
= ov2680_hv_flip_bayer_order
[hv_flip
];
346 static int ov2680_vflip_enable(struct ov2680_dev
*sensor
)
350 ret
= ov2680_mod_reg(sensor
, OV2680_REG_FORMAT1
, BIT(2), BIT(2));
354 return ov2680_bayer_order(sensor
);
357 static int ov2680_vflip_disable(struct ov2680_dev
*sensor
)
361 ret
= ov2680_mod_reg(sensor
, OV2680_REG_FORMAT1
, BIT(2), BIT(0));
365 return ov2680_bayer_order(sensor
);
368 static int ov2680_hflip_enable(struct ov2680_dev
*sensor
)
372 ret
= ov2680_mod_reg(sensor
, OV2680_REG_FORMAT2
, BIT(2), BIT(2));
376 return ov2680_bayer_order(sensor
);
379 static int ov2680_hflip_disable(struct ov2680_dev
*sensor
)
383 ret
= ov2680_mod_reg(sensor
, OV2680_REG_FORMAT2
, BIT(2), BIT(0));
387 return ov2680_bayer_order(sensor
);
390 static int ov2680_test_pattern_set(struct ov2680_dev
*sensor
, int value
)
395 return ov2680_mod_reg(sensor
, OV2680_REG_ISP_CTRL00
, BIT(7), 0);
397 ret
= ov2680_mod_reg(sensor
, OV2680_REG_ISP_CTRL00
, 0x03, value
- 1);
401 ret
= ov2680_mod_reg(sensor
, OV2680_REG_ISP_CTRL00
, BIT(7), BIT(7));
408 static int ov2680_gain_set(struct ov2680_dev
*sensor
, bool auto_gain
)
410 struct ov2680_ctrls
*ctrls
= &sensor
->ctrls
;
414 ret
= ov2680_mod_reg(sensor
, OV2680_REG_R_MANUAL
, BIT(1),
415 auto_gain
? 0 : BIT(1));
419 if (auto_gain
|| !ctrls
->gain
->is_new
)
422 gain
= ctrls
->gain
->val
;
424 ret
= ov2680_write_reg16(sensor
, OV2680_REG_GAIN_PK
, gain
);
429 static int ov2680_gain_get(struct ov2680_dev
*sensor
)
434 ret
= ov2680_read_reg16(sensor
, OV2680_REG_GAIN_PK
, &gain
);
441 static int ov2680_exposure_set(struct ov2680_dev
*sensor
, bool auto_exp
)
443 struct ov2680_ctrls
*ctrls
= &sensor
->ctrls
;
447 ret
= ov2680_mod_reg(sensor
, OV2680_REG_R_MANUAL
, BIT(0),
448 auto_exp
? 0 : BIT(0));
452 if (auto_exp
|| !ctrls
->exposure
->is_new
)
455 exp
= (u32
)ctrls
->exposure
->val
;
458 return ov2680_write_reg24(sensor
, OV2680_REG_EXPOSURE_PK_HIGH
, exp
);
461 static int ov2680_exposure_get(struct ov2680_dev
*sensor
)
466 ret
= ov2680_read_reg24(sensor
, OV2680_REG_EXPOSURE_PK_HIGH
, &exp
);
473 static int ov2680_stream_enable(struct ov2680_dev
*sensor
)
475 return ov2680_write_reg(sensor
, OV2680_REG_STREAM_CTRL
, 1);
478 static int ov2680_stream_disable(struct ov2680_dev
*sensor
)
480 return ov2680_write_reg(sensor
, OV2680_REG_STREAM_CTRL
, 0);
483 static int ov2680_mode_set(struct ov2680_dev
*sensor
)
485 struct ov2680_ctrls
*ctrls
= &sensor
->ctrls
;
488 ret
= ov2680_gain_set(sensor
, false);
492 ret
= ov2680_exposure_set(sensor
, false);
496 ret
= ov2680_load_regs(sensor
, sensor
->current_mode
);
500 if (ctrls
->auto_gain
->val
) {
501 ret
= ov2680_gain_set(sensor
, true);
506 if (ctrls
->auto_exp
->val
== V4L2_EXPOSURE_AUTO
) {
507 ret
= ov2680_exposure_set(sensor
, true);
512 sensor
->mode_pending_changes
= false;
517 static int ov2680_mode_restore(struct ov2680_dev
*sensor
)
521 ret
= ov2680_load_regs(sensor
, &ov2680_mode_init_data
);
525 return ov2680_mode_set(sensor
);
528 static int ov2680_power_off(struct ov2680_dev
*sensor
)
530 if (!sensor
->is_enabled
)
533 clk_disable_unprepare(sensor
->xvclk
);
534 ov2680_power_down(sensor
);
535 regulator_bulk_disable(OV2680_NUM_SUPPLIES
, sensor
->supplies
);
536 sensor
->is_enabled
= false;
541 static int ov2680_power_on(struct ov2680_dev
*sensor
)
543 struct device
*dev
= ov2680_to_dev(sensor
);
546 if (sensor
->is_enabled
)
549 ret
= regulator_bulk_enable(OV2680_NUM_SUPPLIES
, sensor
->supplies
);
551 dev_err(dev
, "failed to enable regulators: %d\n", ret
);
555 if (!sensor
->reset_gpio
) {
556 ret
= ov2680_write_reg(sensor
, OV2680_REG_SOFT_RESET
, 0x01);
558 dev_err(dev
, "sensor soft reset failed\n");
561 usleep_range(1000, 2000);
563 ov2680_power_down(sensor
);
564 ov2680_power_up(sensor
);
567 ret
= clk_prepare_enable(sensor
->xvclk
);
571 sensor
->is_enabled
= true;
573 /* Set clock lane into LP-11 state */
574 ov2680_stream_enable(sensor
);
575 usleep_range(1000, 2000);
576 ov2680_stream_disable(sensor
);
581 static int ov2680_s_power(struct v4l2_subdev
*sd
, int on
)
583 struct ov2680_dev
*sensor
= to_ov2680_dev(sd
);
586 mutex_lock(&sensor
->lock
);
589 ret
= ov2680_power_on(sensor
);
591 ret
= ov2680_power_off(sensor
);
593 mutex_unlock(&sensor
->lock
);
595 if (on
&& ret
== 0) {
596 ret
= v4l2_ctrl_handler_setup(&sensor
->ctrls
.handler
);
600 ret
= ov2680_mode_restore(sensor
);
606 static int ov2680_s_g_frame_interval(struct v4l2_subdev
*sd
,
607 struct v4l2_subdev_frame_interval
*fi
)
609 struct ov2680_dev
*sensor
= to_ov2680_dev(sd
);
611 mutex_lock(&sensor
->lock
);
612 fi
->interval
= sensor
->frame_interval
;
613 mutex_unlock(&sensor
->lock
);
618 static int ov2680_s_stream(struct v4l2_subdev
*sd
, int enable
)
620 struct ov2680_dev
*sensor
= to_ov2680_dev(sd
);
623 mutex_lock(&sensor
->lock
);
625 if (sensor
->is_streaming
== !!enable
)
628 if (enable
&& sensor
->mode_pending_changes
) {
629 ret
= ov2680_mode_set(sensor
);
635 ret
= ov2680_stream_enable(sensor
);
637 ret
= ov2680_stream_disable(sensor
);
639 sensor
->is_streaming
= !!enable
;
642 mutex_unlock(&sensor
->lock
);
647 static int ov2680_enum_mbus_code(struct v4l2_subdev
*sd
,
648 struct v4l2_subdev_pad_config
*cfg
,
649 struct v4l2_subdev_mbus_code_enum
*code
)
651 struct ov2680_dev
*sensor
= to_ov2680_dev(sd
);
653 if (code
->pad
!= 0 || code
->index
!= 0)
656 code
->code
= sensor
->fmt
.code
;
661 static int ov2680_get_fmt(struct v4l2_subdev
*sd
,
662 struct v4l2_subdev_pad_config
*cfg
,
663 struct v4l2_subdev_format
*format
)
665 struct ov2680_dev
*sensor
= to_ov2680_dev(sd
);
666 struct v4l2_mbus_framefmt
*fmt
= NULL
;
669 if (format
->pad
!= 0)
672 mutex_lock(&sensor
->lock
);
674 if (format
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
675 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
676 fmt
= v4l2_subdev_get_try_format(&sensor
->sd
, cfg
, format
->pad
);
685 format
->format
= *fmt
;
687 mutex_unlock(&sensor
->lock
);
692 static int ov2680_set_fmt(struct v4l2_subdev
*sd
,
693 struct v4l2_subdev_pad_config
*cfg
,
694 struct v4l2_subdev_format
*format
)
696 struct ov2680_dev
*sensor
= to_ov2680_dev(sd
);
697 struct v4l2_mbus_framefmt
*fmt
= &format
->format
;
698 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
699 struct v4l2_mbus_framefmt
*try_fmt
;
701 const struct ov2680_mode_info
*mode
;
704 if (format
->pad
!= 0)
707 mutex_lock(&sensor
->lock
);
709 if (sensor
->is_streaming
) {
714 mode
= v4l2_find_nearest_size(ov2680_mode_data
,
715 ARRAY_SIZE(ov2680_mode_data
), width
,
716 height
, fmt
->width
, fmt
->height
);
722 if (format
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
723 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
724 try_fmt
= v4l2_subdev_get_try_format(sd
, cfg
, 0);
725 format
->format
= *try_fmt
;
730 fmt
->width
= mode
->width
;
731 fmt
->height
= mode
->height
;
732 fmt
->code
= sensor
->fmt
.code
;
733 fmt
->colorspace
= sensor
->fmt
.colorspace
;
735 sensor
->current_mode
= mode
;
736 sensor
->fmt
= format
->format
;
737 sensor
->mode_pending_changes
= true;
740 mutex_unlock(&sensor
->lock
);
745 static int ov2680_init_cfg(struct v4l2_subdev
*sd
,
746 struct v4l2_subdev_pad_config
*cfg
)
748 struct v4l2_subdev_format fmt
= {
749 .which
= cfg
? V4L2_SUBDEV_FORMAT_TRY
750 : V4L2_SUBDEV_FORMAT_ACTIVE
,
757 return ov2680_set_fmt(sd
, cfg
, &fmt
);
760 static int ov2680_enum_frame_size(struct v4l2_subdev
*sd
,
761 struct v4l2_subdev_pad_config
*cfg
,
762 struct v4l2_subdev_frame_size_enum
*fse
)
764 int index
= fse
->index
;
766 if (index
>= OV2680_MODE_MAX
|| index
< 0)
769 fse
->min_width
= ov2680_mode_data
[index
].width
;
770 fse
->min_height
= ov2680_mode_data
[index
].height
;
771 fse
->max_width
= ov2680_mode_data
[index
].width
;
772 fse
->max_height
= ov2680_mode_data
[index
].height
;
777 static int ov2680_enum_frame_interval(struct v4l2_subdev
*sd
,
778 struct v4l2_subdev_pad_config
*cfg
,
779 struct v4l2_subdev_frame_interval_enum
*fie
)
781 struct v4l2_fract tpf
;
783 if (fie
->index
>= OV2680_MODE_MAX
|| fie
->width
> OV2680_WIDTH_MAX
||
784 fie
->height
> OV2680_HEIGHT_MAX
||
785 fie
->which
> V4L2_SUBDEV_FORMAT_ACTIVE
)
788 tpf
.denominator
= OV2680_FRAME_RATE
;
796 static int ov2680_g_volatile_ctrl(struct v4l2_ctrl
*ctrl
)
798 struct v4l2_subdev
*sd
= ctrl_to_sd(ctrl
);
799 struct ov2680_dev
*sensor
= to_ov2680_dev(sd
);
800 struct ov2680_ctrls
*ctrls
= &sensor
->ctrls
;
803 if (!sensor
->is_enabled
)
808 val
= ov2680_gain_get(sensor
);
811 ctrls
->gain
->val
= val
;
813 case V4L2_CID_EXPOSURE
:
814 val
= ov2680_exposure_get(sensor
);
817 ctrls
->exposure
->val
= val
;
824 static int ov2680_s_ctrl(struct v4l2_ctrl
*ctrl
)
826 struct v4l2_subdev
*sd
= ctrl_to_sd(ctrl
);
827 struct ov2680_dev
*sensor
= to_ov2680_dev(sd
);
828 struct ov2680_ctrls
*ctrls
= &sensor
->ctrls
;
830 if (!sensor
->is_enabled
)
834 case V4L2_CID_AUTOGAIN
:
835 return ov2680_gain_set(sensor
, !!ctrl
->val
);
837 return ov2680_gain_set(sensor
, !!ctrls
->auto_gain
->val
);
838 case V4L2_CID_EXPOSURE_AUTO
:
839 return ov2680_exposure_set(sensor
, !!ctrl
->val
);
840 case V4L2_CID_EXPOSURE
:
841 return ov2680_exposure_set(sensor
, !!ctrls
->auto_exp
->val
);
843 if (sensor
->is_streaming
)
846 return ov2680_vflip_enable(sensor
);
848 return ov2680_vflip_disable(sensor
);
850 if (sensor
->is_streaming
)
853 return ov2680_hflip_enable(sensor
);
855 return ov2680_hflip_disable(sensor
);
856 case V4L2_CID_TEST_PATTERN
:
857 return ov2680_test_pattern_set(sensor
, ctrl
->val
);
865 static const struct v4l2_ctrl_ops ov2680_ctrl_ops
= {
866 .g_volatile_ctrl
= ov2680_g_volatile_ctrl
,
867 .s_ctrl
= ov2680_s_ctrl
,
870 static const struct v4l2_subdev_core_ops ov2680_core_ops
= {
871 .s_power
= ov2680_s_power
,
874 static const struct v4l2_subdev_video_ops ov2680_video_ops
= {
875 .g_frame_interval
= ov2680_s_g_frame_interval
,
876 .s_frame_interval
= ov2680_s_g_frame_interval
,
877 .s_stream
= ov2680_s_stream
,
880 static const struct v4l2_subdev_pad_ops ov2680_pad_ops
= {
881 .init_cfg
= ov2680_init_cfg
,
882 .enum_mbus_code
= ov2680_enum_mbus_code
,
883 .get_fmt
= ov2680_get_fmt
,
884 .set_fmt
= ov2680_set_fmt
,
885 .enum_frame_size
= ov2680_enum_frame_size
,
886 .enum_frame_interval
= ov2680_enum_frame_interval
,
889 static const struct v4l2_subdev_ops ov2680_subdev_ops
= {
890 .core
= &ov2680_core_ops
,
891 .video
= &ov2680_video_ops
,
892 .pad
= &ov2680_pad_ops
,
895 static int ov2680_mode_init(struct ov2680_dev
*sensor
)
897 const struct ov2680_mode_info
*init_mode
;
899 /* set initial mode */
900 sensor
->fmt
.code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
901 sensor
->fmt
.width
= 800;
902 sensor
->fmt
.height
= 600;
903 sensor
->fmt
.field
= V4L2_FIELD_NONE
;
904 sensor
->fmt
.colorspace
= V4L2_COLORSPACE_SRGB
;
906 sensor
->frame_interval
.denominator
= OV2680_FRAME_RATE
;
907 sensor
->frame_interval
.numerator
= 1;
909 init_mode
= &ov2680_mode_init_data
;
911 sensor
->current_mode
= init_mode
;
913 sensor
->mode_pending_changes
= true;
918 static int ov2680_v4l2_register(struct ov2680_dev
*sensor
)
920 const struct v4l2_ctrl_ops
*ops
= &ov2680_ctrl_ops
;
921 struct ov2680_ctrls
*ctrls
= &sensor
->ctrls
;
922 struct v4l2_ctrl_handler
*hdl
= &ctrls
->handler
;
925 v4l2_i2c_subdev_init(&sensor
->sd
, sensor
->i2c_client
,
928 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
929 sensor
->sd
.flags
= V4L2_SUBDEV_FL_HAS_DEVNODE
;
931 sensor
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
932 sensor
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
934 ret
= media_entity_pads_init(&sensor
->sd
.entity
, 1, &sensor
->pad
);
938 v4l2_ctrl_handler_init(hdl
, 7);
940 hdl
->lock
= &sensor
->lock
;
942 ctrls
->vflip
= v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_VFLIP
, 0, 1, 1, 0);
943 ctrls
->hflip
= v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_HFLIP
, 0, 1, 1, 0);
945 ctrls
->test_pattern
= v4l2_ctrl_new_std_menu_items(hdl
,
946 &ov2680_ctrl_ops
, V4L2_CID_TEST_PATTERN
,
947 ARRAY_SIZE(test_pattern_menu
) - 1,
948 0, 0, test_pattern_menu
);
950 ctrls
->auto_exp
= v4l2_ctrl_new_std_menu(hdl
, ops
,
951 V4L2_CID_EXPOSURE_AUTO
,
952 V4L2_EXPOSURE_MANUAL
, 0,
955 ctrls
->exposure
= v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_EXPOSURE
,
958 ctrls
->auto_gain
= v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_AUTOGAIN
,
960 ctrls
->gain
= v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_GAIN
, 0, 2047, 1, 0);
967 ctrls
->gain
->flags
|= V4L2_CTRL_FLAG_VOLATILE
;
968 ctrls
->exposure
->flags
|= V4L2_CTRL_FLAG_VOLATILE
;
970 v4l2_ctrl_auto_cluster(2, &ctrls
->auto_gain
, 0, true);
971 v4l2_ctrl_auto_cluster(2, &ctrls
->auto_exp
, 1, true);
973 sensor
->sd
.ctrl_handler
= hdl
;
975 ret
= v4l2_async_register_subdev(&sensor
->sd
);
982 media_entity_cleanup(&sensor
->sd
.entity
);
983 v4l2_ctrl_handler_free(hdl
);
988 static int ov2680_get_regulators(struct ov2680_dev
*sensor
)
992 for (i
= 0; i
< OV2680_NUM_SUPPLIES
; i
++)
993 sensor
->supplies
[i
].supply
= ov2680_supply_name
[i
];
995 return devm_regulator_bulk_get(&sensor
->i2c_client
->dev
,
1000 static int ov2680_check_id(struct ov2680_dev
*sensor
)
1002 struct device
*dev
= ov2680_to_dev(sensor
);
1006 ov2680_power_on(sensor
);
1008 ret
= ov2680_read_reg16(sensor
, OV2680_REG_CHIP_ID_HIGH
, &chip_id
);
1010 dev_err(dev
, "failed to read chip id high\n");
1014 if (chip_id
!= OV2680_CHIP_ID
) {
1015 dev_err(dev
, "chip id: 0x%04x does not match expected 0x%04x\n",
1016 chip_id
, OV2680_CHIP_ID
);
1023 static int ov2680_parse_dt(struct ov2680_dev
*sensor
)
1025 struct device
*dev
= ov2680_to_dev(sensor
);
1028 sensor
->reset_gpio
= devm_gpiod_get_optional(dev
, "reset",
1030 ret
= PTR_ERR_OR_ZERO(sensor
->reset_gpio
);
1032 dev_dbg(dev
, "error while getting reset gpio: %d\n", ret
);
1036 sensor
->xvclk
= devm_clk_get(dev
, "xvclk");
1037 if (IS_ERR(sensor
->xvclk
)) {
1038 dev_err(dev
, "xvclk clock missing or invalid\n");
1039 return PTR_ERR(sensor
->xvclk
);
1042 sensor
->xvclk_freq
= clk_get_rate(sensor
->xvclk
);
1043 if (sensor
->xvclk_freq
!= OV2680_XVCLK_VALUE
) {
1044 dev_err(dev
, "wrong xvclk frequency %d HZ, expected: %d Hz\n",
1045 sensor
->xvclk_freq
, OV2680_XVCLK_VALUE
);
1052 static int ov2680_probe(struct i2c_client
*client
)
1054 struct device
*dev
= &client
->dev
;
1055 struct ov2680_dev
*sensor
;
1058 sensor
= devm_kzalloc(dev
, sizeof(*sensor
), GFP_KERNEL
);
1062 sensor
->i2c_client
= client
;
1064 ret
= ov2680_parse_dt(sensor
);
1068 ret
= ov2680_mode_init(sensor
);
1072 ret
= ov2680_get_regulators(sensor
);
1074 dev_err(dev
, "failed to get regulators\n");
1078 mutex_init(&sensor
->lock
);
1080 ret
= ov2680_check_id(sensor
);
1084 ret
= ov2680_v4l2_register(sensor
);
1088 dev_info(dev
, "ov2680 init correctly\n");
1093 dev_err(dev
, "ov2680 init fail: %d\n", ret
);
1094 mutex_destroy(&sensor
->lock
);
1099 static int ov2680_remove(struct i2c_client
*client
)
1101 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1102 struct ov2680_dev
*sensor
= to_ov2680_dev(sd
);
1104 v4l2_async_unregister_subdev(&sensor
->sd
);
1105 mutex_destroy(&sensor
->lock
);
1106 media_entity_cleanup(&sensor
->sd
.entity
);
1107 v4l2_ctrl_handler_free(&sensor
->ctrls
.handler
);
1112 static int __maybe_unused
ov2680_suspend(struct device
*dev
)
1114 struct i2c_client
*client
= to_i2c_client(dev
);
1115 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1116 struct ov2680_dev
*sensor
= to_ov2680_dev(sd
);
1118 if (sensor
->is_streaming
)
1119 ov2680_stream_disable(sensor
);
1124 static int __maybe_unused
ov2680_resume(struct device
*dev
)
1126 struct i2c_client
*client
= to_i2c_client(dev
);
1127 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1128 struct ov2680_dev
*sensor
= to_ov2680_dev(sd
);
1131 if (sensor
->is_streaming
) {
1132 ret
= ov2680_stream_enable(sensor
);
1134 goto stream_disable
;
1140 ov2680_stream_disable(sensor
);
1141 sensor
->is_streaming
= false;
1146 static const struct dev_pm_ops ov2680_pm_ops
= {
1147 SET_SYSTEM_SLEEP_PM_OPS(ov2680_suspend
, ov2680_resume
)
1150 static const struct of_device_id ov2680_dt_ids
[] = {
1151 { .compatible
= "ovti,ov2680" },
1154 MODULE_DEVICE_TABLE(of
, ov2680_dt_ids
);
1156 static struct i2c_driver ov2680_i2c_driver
= {
1159 .pm
= &ov2680_pm_ops
,
1160 .of_match_table
= of_match_ptr(ov2680_dt_ids
),
1162 .probe_new
= ov2680_probe
,
1163 .remove
= ov2680_remove
,
1165 module_i2c_driver(ov2680_i2c_driver
);
1167 MODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>");
1168 MODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver");
1169 MODULE_LICENSE("GPL v2");