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-fwnode.h>
29 #include <media/v4l2-subdev.h>
31 #define OV2680_CHIP_ID 0x2680
33 #define OV2680_REG_STREAM_CTRL CCI_REG8(0x0100)
34 #define OV2680_REG_SOFT_RESET CCI_REG8(0x0103)
36 #define OV2680_REG_CHIP_ID CCI_REG16(0x300a)
37 #define OV2680_REG_SC_CMMN_SUB_ID CCI_REG8(0x302a)
38 #define OV2680_REG_PLL_MULTIPLIER CCI_REG16(0x3081)
40 #define OV2680_REG_EXPOSURE_PK CCI_REG24(0x3500)
41 #define OV2680_REG_R_MANUAL CCI_REG8(0x3503)
42 #define OV2680_REG_GAIN_PK CCI_REG16(0x350a)
44 #define OV2680_REG_SENSOR_CTRL_0A CCI_REG8(0x370a)
46 #define OV2680_REG_HORIZONTAL_START CCI_REG16(0x3800)
47 #define OV2680_REG_VERTICAL_START CCI_REG16(0x3802)
48 #define OV2680_REG_HORIZONTAL_END CCI_REG16(0x3804)
49 #define OV2680_REG_VERTICAL_END CCI_REG16(0x3806)
50 #define OV2680_REG_HORIZONTAL_OUTPUT_SIZE CCI_REG16(0x3808)
51 #define OV2680_REG_VERTICAL_OUTPUT_SIZE CCI_REG16(0x380a)
52 #define OV2680_REG_TIMING_HTS CCI_REG16(0x380c)
53 #define OV2680_REG_TIMING_VTS CCI_REG16(0x380e)
54 #define OV2680_REG_ISP_X_WIN CCI_REG16(0x3810)
55 #define OV2680_REG_ISP_Y_WIN CCI_REG16(0x3812)
56 #define OV2680_REG_X_INC CCI_REG8(0x3814)
57 #define OV2680_REG_Y_INC CCI_REG8(0x3815)
58 #define OV2680_REG_FORMAT1 CCI_REG8(0x3820)
59 #define OV2680_REG_FORMAT2 CCI_REG8(0x3821)
61 #define OV2680_REG_ISP_CTRL00 CCI_REG8(0x5080)
63 #define OV2680_REG_X_WIN CCI_REG16(0x5704)
64 #define OV2680_REG_Y_WIN CCI_REG16(0x5706)
66 #define OV2680_FRAME_RATE 30
68 #define OV2680_NATIVE_WIDTH 1616
69 #define OV2680_NATIVE_HEIGHT 1216
70 #define OV2680_NATIVE_START_LEFT 0
71 #define OV2680_NATIVE_START_TOP 0
72 #define OV2680_ACTIVE_WIDTH 1600
73 #define OV2680_ACTIVE_HEIGHT 1200
74 #define OV2680_ACTIVE_START_LEFT 8
75 #define OV2680_ACTIVE_START_TOP 8
76 #define OV2680_MIN_CROP_WIDTH 2
77 #define OV2680_MIN_CROP_HEIGHT 2
78 #define OV2680_MIN_VBLANK 4
79 #define OV2680_MAX_VBLANK 0xffff
81 /* Fixed pre-div of 1/2 */
82 #define OV2680_PLL_PREDIV0 2
84 /* Pre-div configurable through reg 0x3080, left at its default of 0x02 : 1/2 */
85 #define OV2680_PLL_PREDIV 2
87 /* 66MHz pixel clock: 66MHz / 1704 * 1294 = 30fps */
88 #define OV2680_PIXELS_PER_LINE 1704
89 #define OV2680_LINES_PER_FRAME_30FPS 1294
91 /* Max exposure time is VTS - 8 */
92 #define OV2680_INTEGRATION_TIME_MARGIN 8
94 #define OV2680_DEFAULT_WIDTH 800
95 #define OV2680_DEFAULT_HEIGHT 600
97 /* For enum_frame_size() full-size + binned-/quarter-size */
98 #define OV2680_FRAME_SIZES 2
100 static const char * const ov2680_supply_name
[] = {
106 #define OV2680_NUM_SUPPLIES ARRAY_SIZE(ov2680_supply_name)
113 static const unsigned long ov2680_xvclk_freqs
[] = {
114 [OV2680_19_2_MHZ
] = 19200000,
115 [OV2680_24_MHZ
] = 24000000,
118 static const u8 ov2680_pll_multipliers
[] = {
119 [OV2680_19_2_MHZ
] = 69,
120 [OV2680_24_MHZ
] = 55,
123 struct ov2680_ctrls
{
124 struct v4l2_ctrl_handler handler
;
125 struct v4l2_ctrl
*exposure
;
126 struct v4l2_ctrl
*gain
;
127 struct v4l2_ctrl
*hflip
;
128 struct v4l2_ctrl
*vflip
;
129 struct v4l2_ctrl
*test_pattern
;
130 struct v4l2_ctrl
*link_freq
;
131 struct v4l2_ctrl
*pixel_rate
;
132 struct v4l2_ctrl
*vblank
;
133 struct v4l2_ctrl
*hblank
;
137 struct v4l2_rect crop
;
138 struct v4l2_mbus_framefmt fmt
;
139 struct v4l2_fract frame_interval
;
151 struct regmap
*regmap
;
152 struct v4l2_subdev sd
;
154 struct media_pad pad
;
160 struct regulator_bulk_data supplies
[OV2680_NUM_SUPPLIES
];
162 struct gpio_desc
*pwdn_gpio
;
163 struct mutex lock
; /* protect members */
167 struct ov2680_ctrls ctrls
;
168 struct ov2680_mode mode
;
171 static const struct v4l2_rect ov2680_default_crop
= {
172 .left
= OV2680_ACTIVE_START_LEFT
,
173 .top
= OV2680_ACTIVE_START_TOP
,
174 .width
= OV2680_ACTIVE_WIDTH
,
175 .height
= OV2680_ACTIVE_HEIGHT
,
178 static const char * const test_pattern_menu
[] = {
186 static const int ov2680_hv_flip_bayer_order
[] = {
187 MEDIA_BUS_FMT_SBGGR10_1X10
,
188 MEDIA_BUS_FMT_SGRBG10_1X10
,
189 MEDIA_BUS_FMT_SGBRG10_1X10
,
190 MEDIA_BUS_FMT_SRGGB10_1X10
,
193 static const struct reg_sequence ov2680_global_setting
[] = {
194 /* MIPI PHY, 0x10 -> 0x1c enable bp_c_hs_en_lat and bp_d_hs_en_lat */
197 /* R MANUAL set exposure and gain to manual (hw does not do auto) */
200 /* Analog control register tweaks */
201 {0x3603, 0x39}, /* Reset value 0x99 */
202 {0x3604, 0x24}, /* Reset value 0x74 */
203 {0x3621, 0x37}, /* Reset value 0x44 */
205 /* Sensor control register tweaks */
206 {0x3701, 0x64}, /* Reset value 0x61 */
207 {0x3705, 0x3c}, /* Reset value 0x21 */
208 {0x370c, 0x50}, /* Reset value 0x10 */
209 {0x370d, 0xc0}, /* Reset value 0x00 */
210 {0x3718, 0x88}, /* Reset value 0x80 */
213 {0x3781, 0x80}, /* Reset value 0x00 */
214 {0x3784, 0x0c}, /* Reset value 0x00, based on OV2680_R1A_AM10.ovt */
215 {0x3789, 0x60}, /* Reset value 0x50 */
217 /* BLC CTRL00 0x01 -> 0x81 set avg_weight to 8 */
220 /* Set black level compensation range to 0 - 3 (default 0 - 11) */
224 /* VFIFO R2 0x00 -> 0x02 set Frame reset enable */
227 /* MIPI ctrl CLK PREPARE MIN change from 0x26 (38) -> 0x36 (54) */
230 /* MIPI ctrl CLK LPX P MIN change from 0x32 (50) -> 0x36 (54) */
233 /* R ISP CTRL2 0x20 -> 0x30, set sof_sel bit */
237 * Window CONTROL 0x00 -> 0x01, enable manual window control,
238 * this is necessary for full size flip and mirror support.
243 * DPC CTRL0 0x14 -> 0x3e, set enable_tail, enable_3x3_cluster
244 * and enable_general_tail bits based OV2680_R1A_AM10.ovt.
248 /* DPC MORE CONNECTION CASE THRE 0x0c (12) -> 0x02 (2) */
251 /* DPC GAIN LIST1 0x0f (15) -> 0x08 (8) */
254 /* DPC GAIN LIST2 0x3f (63) -> 0x0c (12) */
257 /* DPC THRE RATIO 0x04 (4) -> 0x00 (0) */
261 static struct ov2680_dev
*to_ov2680_dev(struct v4l2_subdev
*sd
)
263 return container_of(sd
, struct ov2680_dev
, sd
);
266 static inline struct v4l2_subdev
*ctrl_to_sd(struct v4l2_ctrl
*ctrl
)
268 return &container_of(ctrl
->handler
, struct ov2680_dev
,
272 static void ov2680_power_up(struct ov2680_dev
*sensor
)
274 if (!sensor
->pwdn_gpio
)
277 gpiod_set_value(sensor
->pwdn_gpio
, 0);
278 usleep_range(5000, 10000);
281 static void ov2680_power_down(struct ov2680_dev
*sensor
)
283 if (!sensor
->pwdn_gpio
)
286 gpiod_set_value(sensor
->pwdn_gpio
, 1);
287 usleep_range(5000, 10000);
290 static void ov2680_set_bayer_order(struct ov2680_dev
*sensor
,
291 struct v4l2_mbus_framefmt
*fmt
)
295 if (sensor
->ctrls
.vflip
&& sensor
->ctrls
.vflip
->val
)
298 if (sensor
->ctrls
.hflip
&& sensor
->ctrls
.hflip
->val
)
301 fmt
->code
= ov2680_hv_flip_bayer_order
[hv_flip
];
304 static struct v4l2_mbus_framefmt
*
305 __ov2680_get_pad_format(struct ov2680_dev
*sensor
,
306 struct v4l2_subdev_state
*state
,
308 enum v4l2_subdev_format_whence which
)
310 if (which
== V4L2_SUBDEV_FORMAT_TRY
)
311 return v4l2_subdev_state_get_format(state
, pad
);
313 return &sensor
->mode
.fmt
;
316 static struct v4l2_rect
*
317 __ov2680_get_pad_crop(struct ov2680_dev
*sensor
,
318 struct v4l2_subdev_state
*state
,
320 enum v4l2_subdev_format_whence which
)
322 if (which
== V4L2_SUBDEV_FORMAT_TRY
)
323 return v4l2_subdev_state_get_crop(state
, pad
);
325 return &sensor
->mode
.crop
;
328 static void ov2680_fill_format(struct ov2680_dev
*sensor
,
329 struct v4l2_mbus_framefmt
*fmt
,
330 unsigned int width
, unsigned int height
)
332 memset(fmt
, 0, sizeof(*fmt
));
334 fmt
->height
= height
;
335 fmt
->field
= V4L2_FIELD_NONE
;
336 fmt
->colorspace
= V4L2_COLORSPACE_SRGB
;
337 ov2680_set_bayer_order(sensor
, fmt
);
340 static void ov2680_calc_mode(struct ov2680_dev
*sensor
)
342 int width
= sensor
->mode
.fmt
.width
;
343 int height
= sensor
->mode
.fmt
.height
;
344 int orig_width
= width
;
345 int orig_height
= height
;
347 if (width
<= (sensor
->mode
.crop
.width
/ 2) &&
348 height
<= (sensor
->mode
.crop
.height
/ 2)) {
349 sensor
->mode
.binning
= true;
353 sensor
->mode
.binning
= false;
356 sensor
->mode
.h_start
= (sensor
->mode
.crop
.left
+
357 (sensor
->mode
.crop
.width
- width
) / 2) & ~1;
358 sensor
->mode
.v_start
= (sensor
->mode
.crop
.top
+
359 (sensor
->mode
.crop
.height
- height
) / 2) & ~1;
361 min(sensor
->mode
.h_start
+ width
- 1, OV2680_NATIVE_WIDTH
- 1);
363 min(sensor
->mode
.v_start
+ height
- 1, OV2680_NATIVE_HEIGHT
- 1);
364 sensor
->mode
.h_output_size
= orig_width
;
365 sensor
->mode
.v_output_size
= orig_height
;
368 static int ov2680_set_mode(struct ov2680_dev
*sensor
)
370 u8 sensor_ctrl_0a
, inc
, fmt1
, fmt2
;
373 if (sensor
->mode
.binning
) {
374 sensor_ctrl_0a
= 0x23;
379 sensor_ctrl_0a
= 0x21;
385 cci_write(sensor
->regmap
, OV2680_REG_SENSOR_CTRL_0A
,
386 sensor_ctrl_0a
, &ret
);
387 cci_write(sensor
->regmap
, OV2680_REG_HORIZONTAL_START
,
388 sensor
->mode
.h_start
, &ret
);
389 cci_write(sensor
->regmap
, OV2680_REG_VERTICAL_START
,
390 sensor
->mode
.v_start
, &ret
);
391 cci_write(sensor
->regmap
, OV2680_REG_HORIZONTAL_END
,
392 sensor
->mode
.h_end
, &ret
);
393 cci_write(sensor
->regmap
, OV2680_REG_VERTICAL_END
,
394 sensor
->mode
.v_end
, &ret
);
395 cci_write(sensor
->regmap
, OV2680_REG_HORIZONTAL_OUTPUT_SIZE
,
396 sensor
->mode
.h_output_size
, &ret
);
397 cci_write(sensor
->regmap
, OV2680_REG_VERTICAL_OUTPUT_SIZE
,
398 sensor
->mode
.v_output_size
, &ret
);
399 cci_write(sensor
->regmap
, OV2680_REG_TIMING_HTS
,
400 OV2680_PIXELS_PER_LINE
, &ret
);
401 /* VTS gets set by the vblank ctrl */
402 cci_write(sensor
->regmap
, OV2680_REG_ISP_X_WIN
, 0, &ret
);
403 cci_write(sensor
->regmap
, OV2680_REG_ISP_Y_WIN
, 0, &ret
);
404 cci_write(sensor
->regmap
, OV2680_REG_X_INC
, inc
, &ret
);
405 cci_write(sensor
->regmap
, OV2680_REG_Y_INC
, inc
, &ret
);
406 cci_write(sensor
->regmap
, OV2680_REG_X_WIN
,
407 sensor
->mode
.h_output_size
, &ret
);
408 cci_write(sensor
->regmap
, OV2680_REG_Y_WIN
,
409 sensor
->mode
.v_output_size
, &ret
);
410 cci_write(sensor
->regmap
, OV2680_REG_FORMAT1
, fmt1
, &ret
);
411 cci_write(sensor
->regmap
, OV2680_REG_FORMAT2
, fmt2
, &ret
);
416 static int ov2680_set_vflip(struct ov2680_dev
*sensor
, s32 val
)
420 if (sensor
->is_streaming
)
423 ret
= cci_update_bits(sensor
->regmap
, OV2680_REG_FORMAT1
,
424 BIT(2), val
? BIT(2) : 0, NULL
);
428 ov2680_set_bayer_order(sensor
, &sensor
->mode
.fmt
);
432 static int ov2680_set_hflip(struct ov2680_dev
*sensor
, s32 val
)
436 if (sensor
->is_streaming
)
439 ret
= cci_update_bits(sensor
->regmap
, OV2680_REG_FORMAT2
,
440 BIT(2), val
? BIT(2) : 0, NULL
);
444 ov2680_set_bayer_order(sensor
, &sensor
->mode
.fmt
);
448 static int ov2680_test_pattern_set(struct ov2680_dev
*sensor
, int value
)
453 return cci_update_bits(sensor
->regmap
, OV2680_REG_ISP_CTRL00
,
456 cci_update_bits(sensor
->regmap
, OV2680_REG_ISP_CTRL00
,
457 0x03, value
- 1, &ret
);
458 cci_update_bits(sensor
->regmap
, OV2680_REG_ISP_CTRL00
,
459 BIT(7), BIT(7), &ret
);
464 static int ov2680_gain_set(struct ov2680_dev
*sensor
, u32 gain
)
466 return cci_write(sensor
->regmap
, OV2680_REG_GAIN_PK
, gain
, NULL
);
469 static int ov2680_exposure_set(struct ov2680_dev
*sensor
, u32 exp
)
471 return cci_write(sensor
->regmap
, OV2680_REG_EXPOSURE_PK
, exp
<< 4,
475 static int ov2680_exposure_update_range(struct ov2680_dev
*sensor
)
477 int exp_max
= sensor
->mode
.fmt
.height
+ sensor
->ctrls
.vblank
->val
-
478 OV2680_INTEGRATION_TIME_MARGIN
;
480 return __v4l2_ctrl_modify_range(sensor
->ctrls
.exposure
, 0, exp_max
,
484 static int ov2680_stream_enable(struct ov2680_dev
*sensor
)
488 ret
= cci_write(sensor
->regmap
, OV2680_REG_PLL_MULTIPLIER
,
489 sensor
->pll_mult
, NULL
);
493 ret
= regmap_multi_reg_write(sensor
->regmap
,
494 ov2680_global_setting
,
495 ARRAY_SIZE(ov2680_global_setting
));
499 ret
= ov2680_set_mode(sensor
);
503 /* Restore value of all ctrls */
504 ret
= __v4l2_ctrl_handler_setup(&sensor
->ctrls
.handler
);
508 return cci_write(sensor
->regmap
, OV2680_REG_STREAM_CTRL
, 1, NULL
);
511 static int ov2680_stream_disable(struct ov2680_dev
*sensor
)
513 return cci_write(sensor
->regmap
, OV2680_REG_STREAM_CTRL
, 0, NULL
);
516 static int ov2680_power_off(struct ov2680_dev
*sensor
)
518 clk_disable_unprepare(sensor
->xvclk
);
519 ov2680_power_down(sensor
);
520 regulator_bulk_disable(OV2680_NUM_SUPPLIES
, sensor
->supplies
);
524 static int ov2680_power_on(struct ov2680_dev
*sensor
)
528 ret
= regulator_bulk_enable(OV2680_NUM_SUPPLIES
, sensor
->supplies
);
530 dev_err(sensor
->dev
, "failed to enable regulators: %d\n", ret
);
534 if (!sensor
->pwdn_gpio
) {
535 ret
= cci_write(sensor
->regmap
, OV2680_REG_SOFT_RESET
, 0x01,
538 dev_err(sensor
->dev
, "sensor soft reset failed\n");
539 goto err_disable_regulators
;
541 usleep_range(1000, 2000);
543 ov2680_power_down(sensor
);
544 ov2680_power_up(sensor
);
547 ret
= clk_prepare_enable(sensor
->xvclk
);
549 goto err_disable_regulators
;
553 err_disable_regulators
:
554 regulator_bulk_disable(OV2680_NUM_SUPPLIES
, sensor
->supplies
);
558 static int ov2680_get_frame_interval(struct v4l2_subdev
*sd
,
559 struct v4l2_subdev_state
*sd_state
,
560 struct v4l2_subdev_frame_interval
*fi
)
562 struct ov2680_dev
*sensor
= to_ov2680_dev(sd
);
565 * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
566 * subdev active state API.
568 if (fi
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
571 mutex_lock(&sensor
->lock
);
572 fi
->interval
= sensor
->mode
.frame_interval
;
573 mutex_unlock(&sensor
->lock
);
578 static int ov2680_s_stream(struct v4l2_subdev
*sd
, int enable
)
580 struct ov2680_dev
*sensor
= to_ov2680_dev(sd
);
583 mutex_lock(&sensor
->lock
);
585 if (sensor
->is_streaming
== !!enable
)
589 ret
= pm_runtime_resume_and_get(sensor
->sd
.dev
);
593 ret
= ov2680_stream_enable(sensor
);
595 pm_runtime_put(sensor
->sd
.dev
);
599 ret
= ov2680_stream_disable(sensor
);
600 pm_runtime_put(sensor
->sd
.dev
);
603 sensor
->is_streaming
= !!enable
;
606 mutex_unlock(&sensor
->lock
);
611 static int ov2680_enum_mbus_code(struct v4l2_subdev
*sd
,
612 struct v4l2_subdev_state
*sd_state
,
613 struct v4l2_subdev_mbus_code_enum
*code
)
615 struct ov2680_dev
*sensor
= to_ov2680_dev(sd
);
617 if (code
->index
!= 0)
620 code
->code
= sensor
->mode
.fmt
.code
;
625 static int ov2680_get_fmt(struct v4l2_subdev
*sd
,
626 struct v4l2_subdev_state
*sd_state
,
627 struct v4l2_subdev_format
*format
)
629 struct ov2680_dev
*sensor
= to_ov2680_dev(sd
);
630 struct v4l2_mbus_framefmt
*fmt
;
632 fmt
= __ov2680_get_pad_format(sensor
, sd_state
, format
->pad
,
635 mutex_lock(&sensor
->lock
);
636 format
->format
= *fmt
;
637 mutex_unlock(&sensor
->lock
);
642 static int ov2680_set_fmt(struct v4l2_subdev
*sd
,
643 struct v4l2_subdev_state
*sd_state
,
644 struct v4l2_subdev_format
*format
)
646 struct ov2680_dev
*sensor
= to_ov2680_dev(sd
);
647 struct v4l2_mbus_framefmt
*try_fmt
;
648 const struct v4l2_rect
*crop
;
649 unsigned int width
, height
;
650 int def
, max
, ret
= 0;
652 crop
= __ov2680_get_pad_crop(sensor
, sd_state
, format
->pad
,
655 /* Limit set_fmt max size to crop width / height */
656 width
= clamp_val(ALIGN(format
->format
.width
, 2),
657 OV2680_MIN_CROP_WIDTH
, crop
->width
);
658 height
= clamp_val(ALIGN(format
->format
.height
, 2),
659 OV2680_MIN_CROP_HEIGHT
, crop
->height
);
661 ov2680_fill_format(sensor
, &format
->format
, width
, height
);
663 if (format
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
664 try_fmt
= v4l2_subdev_state_get_format(sd_state
, 0);
665 *try_fmt
= format
->format
;
669 mutex_lock(&sensor
->lock
);
671 if (sensor
->is_streaming
) {
676 sensor
->mode
.fmt
= format
->format
;
677 ov2680_calc_mode(sensor
);
679 /* vblank range is height dependent adjust and reset to default */
680 max
= OV2680_MAX_VBLANK
- height
;
681 def
= OV2680_LINES_PER_FRAME_30FPS
- height
;
682 ret
= __v4l2_ctrl_modify_range(sensor
->ctrls
.vblank
, OV2680_MIN_VBLANK
,
687 ret
= __v4l2_ctrl_s_ctrl(sensor
->ctrls
.vblank
, def
);
691 /* exposure range depends on vts which may have changed */
692 ret
= ov2680_exposure_update_range(sensor
);
696 /* adjust hblank value for new width */
697 def
= OV2680_PIXELS_PER_LINE
- width
;
698 ret
= __v4l2_ctrl_modify_range(sensor
->ctrls
.hblank
, def
, def
, 1, def
);
701 mutex_unlock(&sensor
->lock
);
706 static int ov2680_get_selection(struct v4l2_subdev
*sd
,
707 struct v4l2_subdev_state
*state
,
708 struct v4l2_subdev_selection
*sel
)
710 struct ov2680_dev
*sensor
= to_ov2680_dev(sd
);
712 switch (sel
->target
) {
713 case V4L2_SEL_TGT_CROP
:
714 mutex_lock(&sensor
->lock
);
715 sel
->r
= *__ov2680_get_pad_crop(sensor
, state
, sel
->pad
,
717 mutex_unlock(&sensor
->lock
);
719 case V4L2_SEL_TGT_NATIVE_SIZE
:
720 case V4L2_SEL_TGT_CROP_BOUNDS
:
723 sel
->r
.width
= OV2680_NATIVE_WIDTH
;
724 sel
->r
.height
= OV2680_NATIVE_HEIGHT
;
726 case V4L2_SEL_TGT_CROP_DEFAULT
:
727 sel
->r
= ov2680_default_crop
;
736 static int ov2680_set_selection(struct v4l2_subdev
*sd
,
737 struct v4l2_subdev_state
*state
,
738 struct v4l2_subdev_selection
*sel
)
740 struct ov2680_dev
*sensor
= to_ov2680_dev(sd
);
741 struct v4l2_mbus_framefmt
*format
;
742 struct v4l2_rect
*crop
;
743 struct v4l2_rect rect
;
745 if (sel
->target
!= V4L2_SEL_TGT_CROP
)
749 * Clamp the boundaries of the crop rectangle to the size of the sensor
750 * pixel array. Align to multiples of 2 to ensure Bayer pattern isn't
753 rect
.left
= clamp_val(ALIGN(sel
->r
.left
, 2),
754 OV2680_NATIVE_START_LEFT
, OV2680_NATIVE_WIDTH
);
755 rect
.top
= clamp_val(ALIGN(sel
->r
.top
, 2),
756 OV2680_NATIVE_START_TOP
, OV2680_NATIVE_HEIGHT
);
757 rect
.width
= clamp_val(ALIGN(sel
->r
.width
, 2),
758 OV2680_MIN_CROP_WIDTH
, OV2680_NATIVE_WIDTH
);
759 rect
.height
= clamp_val(ALIGN(sel
->r
.height
, 2),
760 OV2680_MIN_CROP_HEIGHT
, OV2680_NATIVE_HEIGHT
);
762 /* Make sure the crop rectangle isn't outside the bounds of the array */
763 rect
.width
= min_t(unsigned int, rect
.width
,
764 OV2680_NATIVE_WIDTH
- rect
.left
);
765 rect
.height
= min_t(unsigned int, rect
.height
,
766 OV2680_NATIVE_HEIGHT
- rect
.top
);
768 crop
= __ov2680_get_pad_crop(sensor
, state
, sel
->pad
, sel
->which
);
770 mutex_lock(&sensor
->lock
);
771 if (rect
.width
!= crop
->width
|| rect
.height
!= crop
->height
) {
773 * Reset the output image size if the crop rectangle size has
776 format
= __ov2680_get_pad_format(sensor
, state
, sel
->pad
,
778 format
->width
= rect
.width
;
779 format
->height
= rect
.height
;
783 mutex_unlock(&sensor
->lock
);
790 static int ov2680_init_state(struct v4l2_subdev
*sd
,
791 struct v4l2_subdev_state
*sd_state
)
793 struct ov2680_dev
*sensor
= to_ov2680_dev(sd
);
795 *v4l2_subdev_state_get_crop(sd_state
, 0) = ov2680_default_crop
;
797 ov2680_fill_format(sensor
, v4l2_subdev_state_get_format(sd_state
, 0),
798 OV2680_DEFAULT_WIDTH
, OV2680_DEFAULT_HEIGHT
);
802 static int ov2680_enum_frame_size(struct v4l2_subdev
*sd
,
803 struct v4l2_subdev_state
*sd_state
,
804 struct v4l2_subdev_frame_size_enum
*fse
)
806 struct ov2680_dev
*sensor
= to_ov2680_dev(sd
);
807 struct v4l2_rect
*crop
;
809 if (fse
->index
>= OV2680_FRAME_SIZES
)
812 crop
= __ov2680_get_pad_crop(sensor
, sd_state
, fse
->pad
, fse
->which
);
816 fse
->min_width
= crop
->width
/ (fse
->index
+ 1);
817 fse
->min_height
= crop
->height
/ (fse
->index
+ 1);
818 fse
->max_width
= fse
->min_width
;
819 fse
->max_height
= fse
->min_height
;
824 static bool ov2680_valid_frame_size(struct v4l2_subdev
*sd
,
825 struct v4l2_subdev_state
*sd_state
,
826 struct v4l2_subdev_frame_interval_enum
*fie
)
828 struct v4l2_subdev_frame_size_enum fse
= {
834 for (i
= 0; i
< OV2680_FRAME_SIZES
; i
++) {
837 if (ov2680_enum_frame_size(sd
, sd_state
, &fse
))
840 if (fie
->width
== fse
.min_width
&&
841 fie
->height
== fse
.min_height
)
848 static int ov2680_enum_frame_interval(struct v4l2_subdev
*sd
,
849 struct v4l2_subdev_state
*sd_state
,
850 struct v4l2_subdev_frame_interval_enum
*fie
)
852 struct ov2680_dev
*sensor
= to_ov2680_dev(sd
);
854 /* Only 1 framerate */
855 if (fie
->index
|| !ov2680_valid_frame_size(sd
, sd_state
, fie
))
858 fie
->interval
= sensor
->mode
.frame_interval
;
863 static int ov2680_s_ctrl(struct v4l2_ctrl
*ctrl
)
865 struct v4l2_subdev
*sd
= ctrl_to_sd(ctrl
);
866 struct ov2680_dev
*sensor
= to_ov2680_dev(sd
);
869 /* Update exposure range on vblank changes */
870 if (ctrl
->id
== V4L2_CID_VBLANK
) {
871 ret
= ov2680_exposure_update_range(sensor
);
876 /* Only apply changes to the controls if the device is powered up */
877 if (!pm_runtime_get_if_in_use(sensor
->sd
.dev
)) {
878 ov2680_set_bayer_order(sensor
, &sensor
->mode
.fmt
);
883 case V4L2_CID_ANALOGUE_GAIN
:
884 ret
= ov2680_gain_set(sensor
, ctrl
->val
);
886 case V4L2_CID_EXPOSURE
:
887 ret
= ov2680_exposure_set(sensor
, ctrl
->val
);
890 ret
= ov2680_set_vflip(sensor
, ctrl
->val
);
893 ret
= ov2680_set_hflip(sensor
, ctrl
->val
);
895 case V4L2_CID_TEST_PATTERN
:
896 ret
= ov2680_test_pattern_set(sensor
, ctrl
->val
);
898 case V4L2_CID_VBLANK
:
899 ret
= cci_write(sensor
->regmap
, OV2680_REG_TIMING_VTS
,
900 sensor
->mode
.fmt
.height
+ ctrl
->val
, NULL
);
907 pm_runtime_put(sensor
->sd
.dev
);
911 static const struct v4l2_ctrl_ops ov2680_ctrl_ops
= {
912 .s_ctrl
= ov2680_s_ctrl
,
915 static const struct v4l2_subdev_video_ops ov2680_video_ops
= {
916 .s_stream
= ov2680_s_stream
,
919 static const struct v4l2_subdev_pad_ops ov2680_pad_ops
= {
920 .enum_mbus_code
= ov2680_enum_mbus_code
,
921 .enum_frame_size
= ov2680_enum_frame_size
,
922 .enum_frame_interval
= ov2680_enum_frame_interval
,
923 .get_fmt
= ov2680_get_fmt
,
924 .set_fmt
= ov2680_set_fmt
,
925 .get_selection
= ov2680_get_selection
,
926 .set_selection
= ov2680_set_selection
,
927 .get_frame_interval
= ov2680_get_frame_interval
,
928 .set_frame_interval
= ov2680_get_frame_interval
,
931 static const struct v4l2_subdev_ops ov2680_subdev_ops
= {
932 .video
= &ov2680_video_ops
,
933 .pad
= &ov2680_pad_ops
,
936 static const struct v4l2_subdev_internal_ops ov2680_internal_ops
= {
937 .init_state
= ov2680_init_state
,
940 static int ov2680_mode_init(struct ov2680_dev
*sensor
)
942 /* set initial mode */
943 sensor
->mode
.crop
= ov2680_default_crop
;
944 ov2680_fill_format(sensor
, &sensor
->mode
.fmt
,
945 OV2680_DEFAULT_WIDTH
, OV2680_DEFAULT_HEIGHT
);
946 ov2680_calc_mode(sensor
);
948 sensor
->mode
.frame_interval
.denominator
= OV2680_FRAME_RATE
;
949 sensor
->mode
.frame_interval
.numerator
= 1;
954 static int ov2680_v4l2_register(struct ov2680_dev
*sensor
)
956 struct i2c_client
*client
= to_i2c_client(sensor
->dev
);
957 const struct v4l2_ctrl_ops
*ops
= &ov2680_ctrl_ops
;
958 struct ov2680_ctrls
*ctrls
= &sensor
->ctrls
;
959 struct v4l2_ctrl_handler
*hdl
= &ctrls
->handler
;
960 struct v4l2_fwnode_device_properties props
;
961 int def
, max
, ret
= 0;
963 v4l2_i2c_subdev_init(&sensor
->sd
, client
, &ov2680_subdev_ops
);
964 sensor
->sd
.internal_ops
= &ov2680_internal_ops
;
966 sensor
->sd
.flags
= V4L2_SUBDEV_FL_HAS_DEVNODE
;
967 sensor
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
968 sensor
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
970 ret
= media_entity_pads_init(&sensor
->sd
.entity
, 1, &sensor
->pad
);
974 v4l2_ctrl_handler_init(hdl
, 11);
976 hdl
->lock
= &sensor
->lock
;
978 ctrls
->vflip
= v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_VFLIP
, 0, 1, 1, 0);
979 ctrls
->hflip
= v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_HFLIP
, 0, 1, 1, 0);
981 ctrls
->test_pattern
= v4l2_ctrl_new_std_menu_items(hdl
,
982 &ov2680_ctrl_ops
, V4L2_CID_TEST_PATTERN
,
983 ARRAY_SIZE(test_pattern_menu
) - 1,
984 0, 0, test_pattern_menu
);
986 max
= OV2680_LINES_PER_FRAME_30FPS
- OV2680_INTEGRATION_TIME_MARGIN
;
987 ctrls
->exposure
= v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_EXPOSURE
,
990 ctrls
->gain
= v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_ANALOGUE_GAIN
,
993 ctrls
->link_freq
= v4l2_ctrl_new_int_menu(hdl
, NULL
, V4L2_CID_LINK_FREQ
,
994 0, 0, sensor
->link_freq
);
995 ctrls
->pixel_rate
= v4l2_ctrl_new_std(hdl
, NULL
, V4L2_CID_PIXEL_RATE
,
996 0, sensor
->pixel_rate
,
997 1, sensor
->pixel_rate
);
999 max
= OV2680_MAX_VBLANK
- OV2680_DEFAULT_HEIGHT
;
1000 def
= OV2680_LINES_PER_FRAME_30FPS
- OV2680_DEFAULT_HEIGHT
;
1001 ctrls
->vblank
= v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_VBLANK
,
1002 OV2680_MIN_VBLANK
, max
, 1, def
);
1004 def
= OV2680_PIXELS_PER_LINE
- OV2680_DEFAULT_WIDTH
;
1005 ctrls
->hblank
= v4l2_ctrl_new_std(hdl
, ops
, V4L2_CID_HBLANK
,
1008 ret
= v4l2_fwnode_device_parse(sensor
->dev
, &props
);
1010 goto cleanup_entity
;
1012 v4l2_ctrl_new_fwnode_properties(hdl
, ops
, &props
);
1016 goto cleanup_entity
;
1019 ctrls
->vflip
->flags
|= V4L2_CTRL_FLAG_MODIFY_LAYOUT
;
1020 ctrls
->hflip
->flags
|= V4L2_CTRL_FLAG_MODIFY_LAYOUT
;
1021 ctrls
->link_freq
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1022 ctrls
->hblank
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1024 sensor
->sd
.ctrl_handler
= hdl
;
1026 ret
= v4l2_async_register_subdev(&sensor
->sd
);
1028 goto cleanup_entity
;
1033 media_entity_cleanup(&sensor
->sd
.entity
);
1034 v4l2_ctrl_handler_free(hdl
);
1039 static int ov2680_get_regulators(struct ov2680_dev
*sensor
)
1043 for (i
= 0; i
< OV2680_NUM_SUPPLIES
; i
++)
1044 sensor
->supplies
[i
].supply
= ov2680_supply_name
[i
];
1046 return devm_regulator_bulk_get(sensor
->dev
,
1047 OV2680_NUM_SUPPLIES
, sensor
->supplies
);
1050 static int ov2680_check_id(struct ov2680_dev
*sensor
)
1055 cci_read(sensor
->regmap
, OV2680_REG_CHIP_ID
, &chip_id
, &ret
);
1056 cci_read(sensor
->regmap
, OV2680_REG_SC_CMMN_SUB_ID
, &rev
, &ret
);
1058 dev_err(sensor
->dev
, "failed to read chip id\n");
1062 if (chip_id
!= OV2680_CHIP_ID
) {
1063 dev_err(sensor
->dev
, "chip id: 0x%04llx does not match expected 0x%04x\n",
1064 chip_id
, OV2680_CHIP_ID
);
1068 dev_info(sensor
->dev
, "sensor_revision id = 0x%llx, rev= %lld\n",
1069 chip_id
, rev
& 0x0f);
1074 static int ov2680_parse_dt(struct ov2680_dev
*sensor
)
1076 struct v4l2_fwnode_endpoint bus_cfg
= {
1077 .bus_type
= V4L2_MBUS_CSI2_DPHY
,
1079 struct device
*dev
= sensor
->dev
;
1080 struct fwnode_handle
*ep_fwnode
;
1081 struct gpio_desc
*gpio
;
1082 unsigned int rate
= 0;
1086 * Sometimes the fwnode graph is initialized by the bridge driver.
1087 * Bridge drivers doing this may also add GPIO mappings, wait for this.
1089 ep_fwnode
= fwnode_graph_get_next_endpoint(dev_fwnode(dev
), NULL
);
1091 return dev_err_probe(dev
, -EPROBE_DEFER
,
1092 "waiting for fwnode graph endpoint\n");
1094 ret
= v4l2_fwnode_endpoint_alloc_parse(ep_fwnode
, &bus_cfg
);
1095 fwnode_handle_put(ep_fwnode
);
1100 * The pin we want is named XSHUTDN in the datasheet. Linux sensor
1101 * drivers have standardized on using "powerdown" as con-id name
1102 * for powerdown or shutdown pins. Older DTB files use "reset",
1103 * so fallback to that if there is no "powerdown" pin.
1105 gpio
= devm_gpiod_get_optional(dev
, "powerdown", GPIOD_OUT_HIGH
);
1107 gpio
= devm_gpiod_get_optional(dev
, "reset", GPIOD_OUT_HIGH
);
1109 ret
= PTR_ERR_OR_ZERO(gpio
);
1111 dev_dbg(dev
, "error while getting reset gpio: %d\n", ret
);
1112 goto out_free_bus_cfg
;
1115 sensor
->pwdn_gpio
= gpio
;
1117 sensor
->xvclk
= devm_clk_get_optional(dev
, "xvclk");
1118 if (IS_ERR(sensor
->xvclk
)) {
1119 ret
= dev_err_probe(dev
, PTR_ERR(sensor
->xvclk
),
1120 "xvclk clock missing or invalid\n");
1121 goto out_free_bus_cfg
;
1125 * We could have either a 24MHz or 19.2MHz clock rate from either DT or
1126 * ACPI... but we also need to support the weird IPU3 case which will
1127 * have an external clock AND a clock-frequency property. Check for the
1128 * clock-frequency property and if found, set that rate if we managed
1129 * to acquire a clock. This should cover the ACPI case. If the system
1130 * uses devicetree then the configured rate should already be set, so
1131 * we can just read it.
1133 ret
= fwnode_property_read_u32(dev_fwnode(dev
), "clock-frequency",
1135 if (ret
&& !sensor
->xvclk
) {
1136 dev_err_probe(dev
, ret
, "invalid clock config\n");
1137 goto out_free_bus_cfg
;
1140 if (!ret
&& sensor
->xvclk
) {
1141 ret
= clk_set_rate(sensor
->xvclk
, rate
);
1143 dev_err_probe(dev
, ret
, "failed to set clock rate\n");
1144 goto out_free_bus_cfg
;
1148 sensor
->xvclk_freq
= rate
?: clk_get_rate(sensor
->xvclk
);
1150 for (i
= 0; i
< ARRAY_SIZE(ov2680_xvclk_freqs
); i
++) {
1151 if (sensor
->xvclk_freq
== ov2680_xvclk_freqs
[i
])
1155 if (i
== ARRAY_SIZE(ov2680_xvclk_freqs
)) {
1156 ret
= dev_err_probe(dev
, -EINVAL
,
1157 "unsupported xvclk frequency %d Hz\n",
1158 sensor
->xvclk_freq
);
1159 goto out_free_bus_cfg
;
1162 sensor
->pll_mult
= ov2680_pll_multipliers
[i
];
1164 sensor
->link_freq
[0] = sensor
->xvclk_freq
/ OV2680_PLL_PREDIV0
/
1165 OV2680_PLL_PREDIV
* sensor
->pll_mult
;
1167 /* CSI-2 is double data rate, bus-format is 10 bpp */
1168 sensor
->pixel_rate
= sensor
->link_freq
[0] * 2;
1169 do_div(sensor
->pixel_rate
, 10);
1171 if (!bus_cfg
.nr_of_link_frequencies
) {
1172 dev_warn(dev
, "Consider passing 'link-frequencies' in DT\n");
1173 goto skip_link_freq_validation
;
1176 for (i
= 0; i
< bus_cfg
.nr_of_link_frequencies
; i
++)
1177 if (bus_cfg
.link_frequencies
[i
] == sensor
->link_freq
[0])
1180 if (bus_cfg
.nr_of_link_frequencies
== i
) {
1181 ret
= dev_err_probe(dev
, -EINVAL
,
1182 "supported link freq %lld not found\n",
1183 sensor
->link_freq
[0]);
1184 goto out_free_bus_cfg
;
1187 skip_link_freq_validation
:
1190 v4l2_fwnode_endpoint_free(&bus_cfg
);
1194 static int ov2680_probe(struct i2c_client
*client
)
1196 struct device
*dev
= &client
->dev
;
1197 struct ov2680_dev
*sensor
;
1200 sensor
= devm_kzalloc(dev
, sizeof(*sensor
), GFP_KERNEL
);
1204 sensor
->dev
= &client
->dev
;
1206 sensor
->regmap
= devm_cci_regmap_init_i2c(client
, 16);
1207 if (IS_ERR(sensor
->regmap
))
1208 return PTR_ERR(sensor
->regmap
);
1210 ret
= ov2680_parse_dt(sensor
);
1214 ret
= ov2680_mode_init(sensor
);
1218 ret
= ov2680_get_regulators(sensor
);
1220 dev_err(dev
, "failed to get regulators\n");
1224 mutex_init(&sensor
->lock
);
1227 * Power up and verify the chip now, so that if runtime pm is
1228 * disabled the chip is left on and streaming will work.
1230 ret
= ov2680_power_on(sensor
);
1234 ret
= ov2680_check_id(sensor
);
1238 pm_runtime_set_active(&client
->dev
);
1239 pm_runtime_get_noresume(&client
->dev
);
1240 pm_runtime_enable(&client
->dev
);
1242 ret
= ov2680_v4l2_register(sensor
);
1244 goto err_pm_runtime
;
1246 pm_runtime_set_autosuspend_delay(&client
->dev
, 1000);
1247 pm_runtime_use_autosuspend(&client
->dev
);
1248 pm_runtime_put_autosuspend(&client
->dev
);
1253 pm_runtime_disable(&client
->dev
);
1254 pm_runtime_put_noidle(&client
->dev
);
1256 ov2680_power_off(sensor
);
1258 dev_err(dev
, "ov2680 init fail: %d\n", ret
);
1259 mutex_destroy(&sensor
->lock
);
1264 static void ov2680_remove(struct i2c_client
*client
)
1266 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1267 struct ov2680_dev
*sensor
= to_ov2680_dev(sd
);
1269 v4l2_async_unregister_subdev(&sensor
->sd
);
1270 mutex_destroy(&sensor
->lock
);
1271 media_entity_cleanup(&sensor
->sd
.entity
);
1272 v4l2_ctrl_handler_free(&sensor
->ctrls
.handler
);
1275 * Disable runtime PM. In case runtime PM is disabled in the kernel,
1276 * make sure to turn power off manually.
1278 pm_runtime_disable(&client
->dev
);
1279 if (!pm_runtime_status_suspended(&client
->dev
))
1280 ov2680_power_off(sensor
);
1281 pm_runtime_set_suspended(&client
->dev
);
1284 static int ov2680_suspend(struct device
*dev
)
1286 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
1287 struct ov2680_dev
*sensor
= to_ov2680_dev(sd
);
1289 if (sensor
->is_streaming
)
1290 ov2680_stream_disable(sensor
);
1292 return ov2680_power_off(sensor
);
1295 static int ov2680_resume(struct device
*dev
)
1297 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
1298 struct ov2680_dev
*sensor
= to_ov2680_dev(sd
);
1301 ret
= ov2680_power_on(sensor
);
1303 goto stream_disable
;
1305 if (sensor
->is_streaming
) {
1306 ret
= ov2680_stream_enable(sensor
);
1308 goto stream_disable
;
1314 ov2680_stream_disable(sensor
);
1315 sensor
->is_streaming
= false;
1320 static DEFINE_RUNTIME_DEV_PM_OPS(ov2680_pm_ops
, ov2680_suspend
, ov2680_resume
,
1323 static const struct of_device_id ov2680_dt_ids
[] = {
1324 { .compatible
= "ovti,ov2680" },
1327 MODULE_DEVICE_TABLE(of
, ov2680_dt_ids
);
1329 static const struct acpi_device_id ov2680_acpi_ids
[] = {
1333 MODULE_DEVICE_TABLE(acpi
, ov2680_acpi_ids
);
1335 static struct i2c_driver ov2680_i2c_driver
= {
1338 .pm
= pm_sleep_ptr(&ov2680_pm_ops
),
1339 .of_match_table
= ov2680_dt_ids
,
1340 .acpi_match_table
= ov2680_acpi_ids
,
1342 .probe
= ov2680_probe
,
1343 .remove
= ov2680_remove
,
1345 module_i2c_driver(ov2680_i2c_driver
);
1347 MODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>");
1348 MODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver");
1349 MODULE_LICENSE("GPL v2");