1 // SPDX-License-Identifier: GPL-2.0
3 * A V4L2 driver for OmniVision OV5647 cameras.
5 * Based on Samsung S5K6AAFX SXGA 1/6" 1.3M CMOS Image Sensor driver
6 * Copyright (C) 2011 Sylwester Nawrocki <s.nawrocki@samsung.com>
8 * Based on Omnivision OV7670 Camera Driver
9 * Copyright (C) 2006-7 Jonathan Corbet <corbet@lwn.net>
11 * Copyright (C) 2016, Synopsys, Inc.
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/i2c.h>
18 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/of_graph.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/slab.h>
24 #include <linux/videodev2.h>
25 #include <media/v4l2-ctrls.h>
26 #include <media/v4l2-device.h>
27 #include <media/v4l2-event.h>
28 #include <media/v4l2-fwnode.h>
29 #include <media/v4l2-image-sizes.h>
30 #include <media/v4l2-mediabus.h>
33 * From the datasheet, "20ms after PWDN goes low or 20ms after RESETB goes
34 * high if reset is inserted after PWDN goes high, host can access sensor's
35 * SCCB to initialize sensor."
37 #define PWDN_ACTIVE_DELAY_MS 20
39 #define MIPI_CTRL00_CLOCK_LANE_GATE BIT(5)
40 #define MIPI_CTRL00_LINE_SYNC_ENABLE BIT(4)
41 #define MIPI_CTRL00_BUS_IDLE BIT(2)
42 #define MIPI_CTRL00_CLOCK_LANE_DISABLE BIT(0)
44 #define OV5647_SW_STANDBY 0x0100
45 #define OV5647_SW_RESET 0x0103
46 #define OV5647_REG_CHIPID_H 0x300a
47 #define OV5647_REG_CHIPID_L 0x300b
48 #define OV5640_REG_PAD_OUT 0x300d
49 #define OV5647_REG_EXP_HI 0x3500
50 #define OV5647_REG_EXP_MID 0x3501
51 #define OV5647_REG_EXP_LO 0x3502
52 #define OV5647_REG_AEC_AGC 0x3503
53 #define OV5647_REG_GAIN_HI 0x350a
54 #define OV5647_REG_GAIN_LO 0x350b
55 #define OV5647_REG_VTS_HI 0x380e
56 #define OV5647_REG_VTS_LO 0x380f
57 #define OV5647_REG_FRAME_OFF_NUMBER 0x4202
58 #define OV5647_REG_MIPI_CTRL00 0x4800
59 #define OV5647_REG_MIPI_CTRL14 0x4814
60 #define OV5647_REG_AWB 0x5001
61 #define OV5647_REG_ISPCTRL3D 0x503d
63 #define REG_TERM 0xfffe
65 #define REG_DLY 0xffff
67 /* OV5647 native and active pixel array size */
68 #define OV5647_NATIVE_WIDTH 2624U
69 #define OV5647_NATIVE_HEIGHT 1956U
71 #define OV5647_PIXEL_ARRAY_LEFT 16U
72 #define OV5647_PIXEL_ARRAY_TOP 16U
73 #define OV5647_PIXEL_ARRAY_WIDTH 2592U
74 #define OV5647_PIXEL_ARRAY_HEIGHT 1944U
76 #define OV5647_VBLANK_MIN 4
77 #define OV5647_VTS_MAX 32767
79 #define OV5647_EXPOSURE_MIN 4
80 #define OV5647_EXPOSURE_STEP 1
81 #define OV5647_EXPOSURE_DEFAULT 1000
82 #define OV5647_EXPOSURE_MAX 65535
90 struct v4l2_mbus_framefmt format
;
91 struct v4l2_rect crop
;
95 const struct regval_list
*reg_list
;
96 unsigned int num_regs
;
100 struct v4l2_subdev sd
;
101 struct media_pad pad
;
104 struct gpio_desc
*pwdn
;
106 struct v4l2_ctrl_handler ctrls
;
107 const struct ov5647_mode
*mode
;
108 struct v4l2_ctrl
*pixel_rate
;
109 struct v4l2_ctrl
*hblank
;
110 struct v4l2_ctrl
*vblank
;
111 struct v4l2_ctrl
*exposure
;
114 static inline struct ov5647
*to_sensor(struct v4l2_subdev
*sd
)
116 return container_of(sd
, struct ov5647
, sd
);
119 static const char * const ov5647_test_pattern_menu
[] = {
126 static const u8 ov5647_test_pattern_val
[] = {
128 0x80, /* Color Bars */
129 0x82, /* Color Squares */
130 0x81, /* Random Data */
133 static const struct regval_list sensor_oe_disable_regs
[] = {
139 static const struct regval_list sensor_oe_enable_regs
[] = {
145 static struct regval_list ov5647_2592x1944_10bpp
[] = {
234 static struct regval_list ov5647_1080p30_10bpp
[] = {
323 static struct regval_list ov5647_2x2binned_10bpp
[] = {
416 static struct regval_list ov5647_640x480_10bpp
[] = {
506 static const struct ov5647_mode ov5647_modes
[] = {
507 /* 2592x1944 full resolution full FOV 10-bit mode. */
510 .code
= MEDIA_BUS_FMT_SBGGR10_1X10
,
511 .colorspace
= V4L2_COLORSPACE_SRGB
,
512 .field
= V4L2_FIELD_NONE
,
517 .left
= OV5647_PIXEL_ARRAY_LEFT
,
518 .top
= OV5647_PIXEL_ARRAY_TOP
,
522 .pixel_rate
= 87500000,
525 .reg_list
= ov5647_2592x1944_10bpp
,
526 .num_regs
= ARRAY_SIZE(ov5647_2592x1944_10bpp
)
528 /* 1080p30 10-bit mode. Full resolution centre-cropped down to 1080p. */
531 .code
= MEDIA_BUS_FMT_SBGGR10_1X10
,
532 .colorspace
= V4L2_COLORSPACE_SRGB
,
533 .field
= V4L2_FIELD_NONE
,
538 .left
= 348 + OV5647_PIXEL_ARRAY_LEFT
,
539 .top
= 434 + OV5647_PIXEL_ARRAY_TOP
,
543 .pixel_rate
= 81666700,
546 .reg_list
= ov5647_1080p30_10bpp
,
547 .num_regs
= ARRAY_SIZE(ov5647_1080p30_10bpp
)
549 /* 2x2 binned full FOV 10-bit mode. */
552 .code
= MEDIA_BUS_FMT_SBGGR10_1X10
,
553 .colorspace
= V4L2_COLORSPACE_SRGB
,
554 .field
= V4L2_FIELD_NONE
,
559 .left
= OV5647_PIXEL_ARRAY_LEFT
,
560 .top
= OV5647_PIXEL_ARRAY_TOP
,
564 .pixel_rate
= 81666700,
567 .reg_list
= ov5647_2x2binned_10bpp
,
568 .num_regs
= ARRAY_SIZE(ov5647_2x2binned_10bpp
)
570 /* 10-bit VGA full FOV 60fps. 2x2 binned and subsampled down to VGA. */
573 .code
= MEDIA_BUS_FMT_SBGGR10_1X10
,
574 .colorspace
= V4L2_COLORSPACE_SRGB
,
575 .field
= V4L2_FIELD_NONE
,
580 .left
= 16 + OV5647_PIXEL_ARRAY_LEFT
,
581 .top
= OV5647_PIXEL_ARRAY_TOP
,
585 .pixel_rate
= 55000000,
588 .reg_list
= ov5647_640x480_10bpp
,
589 .num_regs
= ARRAY_SIZE(ov5647_640x480_10bpp
)
593 /* Default sensor mode is 2x2 binned 640x480 SBGGR10_1X10. */
594 #define OV5647_DEFAULT_MODE (&ov5647_modes[3])
595 #define OV5647_DEFAULT_FORMAT (ov5647_modes[3].format)
597 static int ov5647_write16(struct v4l2_subdev
*sd
, u16 reg
, u16 val
)
599 unsigned char data
[4] = { reg
>> 8, reg
& 0xff, val
>> 8, val
& 0xff};
600 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
603 ret
= i2c_master_send(client
, data
, 4);
605 dev_dbg(&client
->dev
, "%s: i2c write error, reg: %x\n",
613 static int ov5647_write(struct v4l2_subdev
*sd
, u16 reg
, u8 val
)
615 unsigned char data
[3] = { reg
>> 8, reg
& 0xff, val
};
616 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
619 ret
= i2c_master_send(client
, data
, 3);
621 dev_dbg(&client
->dev
, "%s: i2c write error, reg: %x\n",
629 static int ov5647_read(struct v4l2_subdev
*sd
, u16 reg
, u8
*val
)
631 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
632 u8 buf
[2] = { reg
>> 8, reg
& 0xff };
633 struct i2c_msg msg
[2];
636 msg
[0].addr
= client
->addr
;
637 msg
[0].flags
= client
->flags
;
639 msg
[0].len
= sizeof(buf
);
641 msg
[1].addr
= client
->addr
;
642 msg
[1].flags
= client
->flags
| I2C_M_RD
;
646 ret
= i2c_transfer(client
->adapter
, msg
, 2);
648 dev_err(&client
->dev
, "%s: i2c read error, reg: %x = %d\n",
650 return ret
>= 0 ? -EINVAL
: ret
;
658 static int ov5647_write_array(struct v4l2_subdev
*sd
,
659 const struct regval_list
*regs
, int array_size
)
663 for (i
= 0; i
< array_size
; i
++) {
664 ret
= ov5647_write(sd
, regs
[i
].addr
, regs
[i
].data
);
672 static int ov5647_set_virtual_channel(struct v4l2_subdev
*sd
, int channel
)
677 ret
= ov5647_read(sd
, OV5647_REG_MIPI_CTRL14
, &channel_id
);
681 channel_id
&= ~(3 << 6);
683 return ov5647_write(sd
, OV5647_REG_MIPI_CTRL14
,
684 channel_id
| (channel
<< 6));
687 static int ov5647_set_mode(struct v4l2_subdev
*sd
)
689 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
690 struct ov5647
*sensor
= to_sensor(sd
);
694 ret
= ov5647_read(sd
, OV5647_SW_STANDBY
, &rdval
);
698 ret
= ov5647_write_array(sd
, sensor
->mode
->reg_list
,
699 sensor
->mode
->num_regs
);
701 dev_err(&client
->dev
, "write sensor default regs error\n");
705 ret
= ov5647_set_virtual_channel(sd
, 0);
709 ret
= ov5647_read(sd
, OV5647_SW_STANDBY
, &resetval
);
713 if (!(resetval
& 0x01)) {
714 dev_err(&client
->dev
, "Device was in SW standby");
715 ret
= ov5647_write(sd
, OV5647_SW_STANDBY
, 0x01);
723 static int ov5647_stream_on(struct v4l2_subdev
*sd
)
725 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
726 struct ov5647
*sensor
= to_sensor(sd
);
727 u8 val
= MIPI_CTRL00_BUS_IDLE
;
730 ret
= ov5647_set_mode(sd
);
732 dev_err(&client
->dev
, "Failed to program sensor mode: %d\n", ret
);
736 /* Apply customized values from user when stream starts. */
737 ret
= __v4l2_ctrl_handler_setup(sd
->ctrl_handler
);
741 if (sensor
->clock_ncont
)
742 val
|= MIPI_CTRL00_CLOCK_LANE_GATE
|
743 MIPI_CTRL00_LINE_SYNC_ENABLE
;
745 ret
= ov5647_write(sd
, OV5647_REG_MIPI_CTRL00
, val
);
749 ret
= ov5647_write(sd
, OV5647_REG_FRAME_OFF_NUMBER
, 0x00);
753 return ov5647_write(sd
, OV5640_REG_PAD_OUT
, 0x00);
756 static int ov5647_stream_off(struct v4l2_subdev
*sd
)
760 ret
= ov5647_write(sd
, OV5647_REG_MIPI_CTRL00
,
761 MIPI_CTRL00_CLOCK_LANE_GATE
| MIPI_CTRL00_BUS_IDLE
|
762 MIPI_CTRL00_CLOCK_LANE_DISABLE
);
766 ret
= ov5647_write(sd
, OV5647_REG_FRAME_OFF_NUMBER
, 0x0f);
770 return ov5647_write(sd
, OV5640_REG_PAD_OUT
, 0x01);
773 static int ov5647_power_on(struct device
*dev
)
775 struct ov5647
*sensor
= dev_get_drvdata(dev
);
778 dev_dbg(dev
, "OV5647 power on\n");
781 gpiod_set_value_cansleep(sensor
->pwdn
, 0);
782 msleep(PWDN_ACTIVE_DELAY_MS
);
785 ret
= clk_prepare_enable(sensor
->xclk
);
787 dev_err(dev
, "clk prepare enable failed\n");
791 ret
= ov5647_write_array(&sensor
->sd
, sensor_oe_enable_regs
,
792 ARRAY_SIZE(sensor_oe_enable_regs
));
794 dev_err(dev
, "write sensor_oe_enable_regs error\n");
795 goto error_clk_disable
;
798 /* Stream off to coax lanes into LP-11 state. */
799 ret
= ov5647_stream_off(&sensor
->sd
);
801 dev_err(dev
, "camera not available, check power\n");
802 goto error_clk_disable
;
808 clk_disable_unprepare(sensor
->xclk
);
810 gpiod_set_value_cansleep(sensor
->pwdn
, 1);
815 static int ov5647_power_off(struct device
*dev
)
817 struct ov5647
*sensor
= dev_get_drvdata(dev
);
821 dev_dbg(dev
, "OV5647 power off\n");
823 ret
= ov5647_write_array(&sensor
->sd
, sensor_oe_disable_regs
,
824 ARRAY_SIZE(sensor_oe_disable_regs
));
826 dev_dbg(dev
, "disable oe failed\n");
828 /* Enter software standby */
829 ret
= ov5647_read(&sensor
->sd
, OV5647_SW_STANDBY
, &rdval
);
831 dev_dbg(dev
, "software standby failed\n");
834 ret
= ov5647_write(&sensor
->sd
, OV5647_SW_STANDBY
, rdval
);
836 dev_dbg(dev
, "software standby failed\n");
838 clk_disable_unprepare(sensor
->xclk
);
839 gpiod_set_value_cansleep(sensor
->pwdn
, 1);
844 #ifdef CONFIG_VIDEO_ADV_DEBUG
845 static int ov5647_sensor_get_register(struct v4l2_subdev
*sd
,
846 struct v4l2_dbg_register
*reg
)
851 ret
= ov5647_read(sd
, reg
->reg
& 0xff, &val
);
861 static int ov5647_sensor_set_register(struct v4l2_subdev
*sd
,
862 const struct v4l2_dbg_register
*reg
)
864 return ov5647_write(sd
, reg
->reg
& 0xff, reg
->val
& 0xff);
868 /* Subdev core operations registration */
869 static const struct v4l2_subdev_core_ops ov5647_subdev_core_ops
= {
870 .subscribe_event
= v4l2_ctrl_subdev_subscribe_event
,
871 .unsubscribe_event
= v4l2_event_subdev_unsubscribe
,
872 #ifdef CONFIG_VIDEO_ADV_DEBUG
873 .g_register
= ov5647_sensor_get_register
,
874 .s_register
= ov5647_sensor_set_register
,
878 static const struct v4l2_rect
*
879 __ov5647_get_pad_crop(struct ov5647
*ov5647
,
880 struct v4l2_subdev_state
*sd_state
,
881 unsigned int pad
, enum v4l2_subdev_format_whence which
)
884 case V4L2_SUBDEV_FORMAT_TRY
:
885 return v4l2_subdev_state_get_crop(sd_state
, pad
);
886 case V4L2_SUBDEV_FORMAT_ACTIVE
:
887 return &ov5647
->mode
->crop
;
893 static int ov5647_s_stream(struct v4l2_subdev
*sd
, int enable
)
895 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
896 struct ov5647
*sensor
= to_sensor(sd
);
899 mutex_lock(&sensor
->lock
);
902 ret
= pm_runtime_resume_and_get(&client
->dev
);
906 ret
= ov5647_stream_on(sd
);
908 dev_err(&client
->dev
, "stream start failed: %d\n", ret
);
912 ret
= ov5647_stream_off(sd
);
914 dev_err(&client
->dev
, "stream stop failed: %d\n", ret
);
917 pm_runtime_put(&client
->dev
);
920 mutex_unlock(&sensor
->lock
);
925 pm_runtime_put(&client
->dev
);
927 mutex_unlock(&sensor
->lock
);
932 static const struct v4l2_subdev_video_ops ov5647_subdev_video_ops
= {
933 .s_stream
= ov5647_s_stream
,
936 static int ov5647_enum_mbus_code(struct v4l2_subdev
*sd
,
937 struct v4l2_subdev_state
*sd_state
,
938 struct v4l2_subdev_mbus_code_enum
*code
)
943 code
->code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
948 static int ov5647_enum_frame_size(struct v4l2_subdev
*sd
,
949 struct v4l2_subdev_state
*sd_state
,
950 struct v4l2_subdev_frame_size_enum
*fse
)
952 const struct v4l2_mbus_framefmt
*fmt
;
954 if (fse
->code
!= MEDIA_BUS_FMT_SBGGR10_1X10
||
955 fse
->index
>= ARRAY_SIZE(ov5647_modes
))
958 fmt
= &ov5647_modes
[fse
->index
].format
;
959 fse
->min_width
= fmt
->width
;
960 fse
->max_width
= fmt
->width
;
961 fse
->min_height
= fmt
->height
;
962 fse
->max_height
= fmt
->height
;
967 static int ov5647_get_pad_fmt(struct v4l2_subdev
*sd
,
968 struct v4l2_subdev_state
*sd_state
,
969 struct v4l2_subdev_format
*format
)
971 struct v4l2_mbus_framefmt
*fmt
= &format
->format
;
972 const struct v4l2_mbus_framefmt
*sensor_format
;
973 struct ov5647
*sensor
= to_sensor(sd
);
975 mutex_lock(&sensor
->lock
);
976 switch (format
->which
) {
977 case V4L2_SUBDEV_FORMAT_TRY
:
978 sensor_format
= v4l2_subdev_state_get_format(sd_state
,
982 sensor_format
= &sensor
->mode
->format
;
986 *fmt
= *sensor_format
;
987 mutex_unlock(&sensor
->lock
);
992 static int ov5647_set_pad_fmt(struct v4l2_subdev
*sd
,
993 struct v4l2_subdev_state
*sd_state
,
994 struct v4l2_subdev_format
*format
)
996 struct v4l2_mbus_framefmt
*fmt
= &format
->format
;
997 struct ov5647
*sensor
= to_sensor(sd
);
998 const struct ov5647_mode
*mode
;
1000 mode
= v4l2_find_nearest_size(ov5647_modes
, ARRAY_SIZE(ov5647_modes
),
1001 format
.width
, format
.height
,
1002 fmt
->width
, fmt
->height
);
1004 /* Update the sensor mode and apply at it at streamon time. */
1005 mutex_lock(&sensor
->lock
);
1006 if (format
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1007 *v4l2_subdev_state_get_format(sd_state
, format
->pad
) = mode
->format
;
1009 int exposure_max
, exposure_def
;
1012 sensor
->mode
= mode
;
1013 __v4l2_ctrl_modify_range(sensor
->pixel_rate
, mode
->pixel_rate
,
1014 mode
->pixel_rate
, 1, mode
->pixel_rate
);
1016 hblank
= mode
->hts
- mode
->format
.width
;
1017 __v4l2_ctrl_modify_range(sensor
->hblank
, hblank
, hblank
, 1,
1020 vblank
= mode
->vts
- mode
->format
.height
;
1021 __v4l2_ctrl_modify_range(sensor
->vblank
, OV5647_VBLANK_MIN
,
1022 OV5647_VTS_MAX
- mode
->format
.height
,
1024 __v4l2_ctrl_s_ctrl(sensor
->vblank
, vblank
);
1026 exposure_max
= mode
->vts
- 4;
1027 exposure_def
= min(exposure_max
, OV5647_EXPOSURE_DEFAULT
);
1028 __v4l2_ctrl_modify_range(sensor
->exposure
,
1029 sensor
->exposure
->minimum
,
1030 exposure_max
, sensor
->exposure
->step
,
1033 *fmt
= mode
->format
;
1034 mutex_unlock(&sensor
->lock
);
1039 static int ov5647_get_selection(struct v4l2_subdev
*sd
,
1040 struct v4l2_subdev_state
*sd_state
,
1041 struct v4l2_subdev_selection
*sel
)
1043 switch (sel
->target
) {
1044 case V4L2_SEL_TGT_CROP
: {
1045 struct ov5647
*sensor
= to_sensor(sd
);
1047 mutex_lock(&sensor
->lock
);
1048 sel
->r
= *__ov5647_get_pad_crop(sensor
, sd_state
, sel
->pad
,
1050 mutex_unlock(&sensor
->lock
);
1055 case V4L2_SEL_TGT_NATIVE_SIZE
:
1058 sel
->r
.width
= OV5647_NATIVE_WIDTH
;
1059 sel
->r
.height
= OV5647_NATIVE_HEIGHT
;
1063 case V4L2_SEL_TGT_CROP_DEFAULT
:
1064 case V4L2_SEL_TGT_CROP_BOUNDS
:
1065 sel
->r
.top
= OV5647_PIXEL_ARRAY_TOP
;
1066 sel
->r
.left
= OV5647_PIXEL_ARRAY_LEFT
;
1067 sel
->r
.width
= OV5647_PIXEL_ARRAY_WIDTH
;
1068 sel
->r
.height
= OV5647_PIXEL_ARRAY_HEIGHT
;
1076 static const struct v4l2_subdev_pad_ops ov5647_subdev_pad_ops
= {
1077 .enum_mbus_code
= ov5647_enum_mbus_code
,
1078 .enum_frame_size
= ov5647_enum_frame_size
,
1079 .set_fmt
= ov5647_set_pad_fmt
,
1080 .get_fmt
= ov5647_get_pad_fmt
,
1081 .get_selection
= ov5647_get_selection
,
1084 static const struct v4l2_subdev_ops ov5647_subdev_ops
= {
1085 .core
= &ov5647_subdev_core_ops
,
1086 .video
= &ov5647_subdev_video_ops
,
1087 .pad
= &ov5647_subdev_pad_ops
,
1090 static int ov5647_detect(struct v4l2_subdev
*sd
)
1092 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1096 ret
= ov5647_write(sd
, OV5647_SW_RESET
, 0x01);
1100 ret
= ov5647_read(sd
, OV5647_REG_CHIPID_H
, &read
);
1105 dev_err(&client
->dev
, "ID High expected 0x56 got %x", read
);
1109 ret
= ov5647_read(sd
, OV5647_REG_CHIPID_L
, &read
);
1114 dev_err(&client
->dev
, "ID Low expected 0x47 got %x", read
);
1118 return ov5647_write(sd
, OV5647_SW_RESET
, 0x00);
1121 static int ov5647_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
1123 struct v4l2_mbus_framefmt
*format
=
1124 v4l2_subdev_state_get_format(fh
->state
, 0);
1125 struct v4l2_rect
*crop
= v4l2_subdev_state_get_crop(fh
->state
, 0);
1127 crop
->left
= OV5647_PIXEL_ARRAY_LEFT
;
1128 crop
->top
= OV5647_PIXEL_ARRAY_TOP
;
1129 crop
->width
= OV5647_PIXEL_ARRAY_WIDTH
;
1130 crop
->height
= OV5647_PIXEL_ARRAY_HEIGHT
;
1132 *format
= OV5647_DEFAULT_FORMAT
;
1137 static const struct v4l2_subdev_internal_ops ov5647_subdev_internal_ops
= {
1138 .open
= ov5647_open
,
1141 static int ov5647_s_auto_white_balance(struct v4l2_subdev
*sd
, u32 val
)
1143 return ov5647_write(sd
, OV5647_REG_AWB
, val
? 1 : 0);
1146 static int ov5647_s_autogain(struct v4l2_subdev
*sd
, u32 val
)
1151 /* Non-zero turns on AGC by clearing bit 1.*/
1152 ret
= ov5647_read(sd
, OV5647_REG_AEC_AGC
, ®
);
1156 return ov5647_write(sd
, OV5647_REG_AEC_AGC
, val
? reg
& ~BIT(1)
1160 static int ov5647_s_exposure_auto(struct v4l2_subdev
*sd
, u32 val
)
1166 * Everything except V4L2_EXPOSURE_MANUAL turns on AEC by
1169 ret
= ov5647_read(sd
, OV5647_REG_AEC_AGC
, ®
);
1173 return ov5647_write(sd
, OV5647_REG_AEC_AGC
,
1174 val
== V4L2_EXPOSURE_MANUAL
? reg
| BIT(0)
1178 static int ov5647_s_analogue_gain(struct v4l2_subdev
*sd
, u32 val
)
1182 /* 10 bits of gain, 2 in the high register. */
1183 ret
= ov5647_write(sd
, OV5647_REG_GAIN_HI
, (val
>> 8) & 3);
1187 return ov5647_write(sd
, OV5647_REG_GAIN_LO
, val
& 0xff);
1190 static int ov5647_s_exposure(struct v4l2_subdev
*sd
, u32 val
)
1195 * Sensor has 20 bits, but the bottom 4 bits are fractions of a line
1196 * which we leave as zero (and don't receive in "val").
1198 ret
= ov5647_write(sd
, OV5647_REG_EXP_HI
, (val
>> 12) & 0xf);
1202 ret
= ov5647_write(sd
, OV5647_REG_EXP_MID
, (val
>> 4) & 0xff);
1206 return ov5647_write(sd
, OV5647_REG_EXP_LO
, (val
& 0xf) << 4);
1209 static int ov5647_s_ctrl(struct v4l2_ctrl
*ctrl
)
1211 struct ov5647
*sensor
= container_of(ctrl
->handler
,
1212 struct ov5647
, ctrls
);
1213 struct v4l2_subdev
*sd
= &sensor
->sd
;
1214 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1218 /* v4l2_ctrl_lock() locks our own mutex */
1220 if (ctrl
->id
== V4L2_CID_VBLANK
) {
1221 int exposure_max
, exposure_def
;
1223 /* Update max exposure while meeting expected vblanking */
1224 exposure_max
= sensor
->mode
->format
.height
+ ctrl
->val
- 4;
1225 exposure_def
= min(exposure_max
, OV5647_EXPOSURE_DEFAULT
);
1226 __v4l2_ctrl_modify_range(sensor
->exposure
,
1227 sensor
->exposure
->minimum
,
1228 exposure_max
, sensor
->exposure
->step
,
1233 * If the device is not powered up do not apply any controls
1234 * to H/W at this time. Instead the controls will be restored
1235 * at s_stream(1) time.
1237 if (pm_runtime_get_if_in_use(&client
->dev
) == 0)
1241 case V4L2_CID_AUTO_WHITE_BALANCE
:
1242 ret
= ov5647_s_auto_white_balance(sd
, ctrl
->val
);
1244 case V4L2_CID_AUTOGAIN
:
1245 ret
= ov5647_s_autogain(sd
, ctrl
->val
);
1247 case V4L2_CID_EXPOSURE_AUTO
:
1248 ret
= ov5647_s_exposure_auto(sd
, ctrl
->val
);
1250 case V4L2_CID_ANALOGUE_GAIN
:
1251 ret
= ov5647_s_analogue_gain(sd
, ctrl
->val
);
1253 case V4L2_CID_EXPOSURE
:
1254 ret
= ov5647_s_exposure(sd
, ctrl
->val
);
1256 case V4L2_CID_VBLANK
:
1257 ret
= ov5647_write16(sd
, OV5647_REG_VTS_HI
,
1258 sensor
->mode
->format
.height
+ ctrl
->val
);
1260 case V4L2_CID_TEST_PATTERN
:
1261 ret
= ov5647_write(sd
, OV5647_REG_ISPCTRL3D
,
1262 ov5647_test_pattern_val
[ctrl
->val
]);
1265 /* Read-only, but we adjust it based on mode. */
1266 case V4L2_CID_PIXEL_RATE
:
1267 case V4L2_CID_HBLANK
:
1268 /* Read-only, but we adjust it based on mode. */
1272 dev_info(&client
->dev
,
1273 "Control (id:0x%x, val:0x%x) not supported\n",
1274 ctrl
->id
, ctrl
->val
);
1278 pm_runtime_put(&client
->dev
);
1283 static const struct v4l2_ctrl_ops ov5647_ctrl_ops
= {
1284 .s_ctrl
= ov5647_s_ctrl
,
1287 static int ov5647_init_controls(struct ov5647
*sensor
)
1289 struct i2c_client
*client
= v4l2_get_subdevdata(&sensor
->sd
);
1290 int hblank
, exposure_max
, exposure_def
;
1292 v4l2_ctrl_handler_init(&sensor
->ctrls
, 9);
1294 v4l2_ctrl_new_std(&sensor
->ctrls
, &ov5647_ctrl_ops
,
1295 V4L2_CID_AUTOGAIN
, 0, 1, 1, 0);
1297 v4l2_ctrl_new_std(&sensor
->ctrls
, &ov5647_ctrl_ops
,
1298 V4L2_CID_AUTO_WHITE_BALANCE
, 0, 1, 1, 0);
1300 v4l2_ctrl_new_std_menu(&sensor
->ctrls
, &ov5647_ctrl_ops
,
1301 V4L2_CID_EXPOSURE_AUTO
, V4L2_EXPOSURE_MANUAL
,
1302 0, V4L2_EXPOSURE_MANUAL
);
1304 exposure_max
= sensor
->mode
->vts
- 4;
1305 exposure_def
= min(exposure_max
, OV5647_EXPOSURE_DEFAULT
);
1306 sensor
->exposure
= v4l2_ctrl_new_std(&sensor
->ctrls
, &ov5647_ctrl_ops
,
1308 OV5647_EXPOSURE_MIN
,
1309 exposure_max
, OV5647_EXPOSURE_STEP
,
1312 /* min: 16 = 1.0x; max (10 bits); default: 32 = 2.0x. */
1313 v4l2_ctrl_new_std(&sensor
->ctrls
, &ov5647_ctrl_ops
,
1314 V4L2_CID_ANALOGUE_GAIN
, 16, 1023, 1, 32);
1316 /* By default, PIXEL_RATE is read only, but it does change per mode */
1317 sensor
->pixel_rate
= v4l2_ctrl_new_std(&sensor
->ctrls
, &ov5647_ctrl_ops
,
1318 V4L2_CID_PIXEL_RATE
,
1319 sensor
->mode
->pixel_rate
,
1320 sensor
->mode
->pixel_rate
, 1,
1321 sensor
->mode
->pixel_rate
);
1323 /* By default, HBLANK is read only, but it does change per mode. */
1324 hblank
= sensor
->mode
->hts
- sensor
->mode
->format
.width
;
1325 sensor
->hblank
= v4l2_ctrl_new_std(&sensor
->ctrls
, &ov5647_ctrl_ops
,
1326 V4L2_CID_HBLANK
, hblank
, hblank
, 1,
1329 sensor
->vblank
= v4l2_ctrl_new_std(&sensor
->ctrls
, &ov5647_ctrl_ops
,
1330 V4L2_CID_VBLANK
, OV5647_VBLANK_MIN
,
1332 sensor
->mode
->format
.height
, 1,
1334 sensor
->mode
->format
.height
);
1336 v4l2_ctrl_new_std_menu_items(&sensor
->ctrls
, &ov5647_ctrl_ops
,
1337 V4L2_CID_TEST_PATTERN
,
1338 ARRAY_SIZE(ov5647_test_pattern_menu
) - 1,
1339 0, 0, ov5647_test_pattern_menu
);
1341 if (sensor
->ctrls
.error
)
1344 sensor
->pixel_rate
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1345 sensor
->hblank
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1346 sensor
->sd
.ctrl_handler
= &sensor
->ctrls
;
1351 dev_err(&client
->dev
, "%s Controls initialization failed (%d)\n",
1352 __func__
, sensor
->ctrls
.error
);
1353 v4l2_ctrl_handler_free(&sensor
->ctrls
);
1355 return sensor
->ctrls
.error
;
1358 static int ov5647_parse_dt(struct ov5647
*sensor
, struct device_node
*np
)
1360 struct v4l2_fwnode_endpoint bus_cfg
= {
1361 .bus_type
= V4L2_MBUS_CSI2_DPHY
,
1363 struct device_node
*ep
__free(device_node
) =
1364 of_graph_get_endpoint_by_regs(np
, 0, -1);
1370 ret
= v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep
), &bus_cfg
);
1374 sensor
->clock_ncont
= bus_cfg
.bus
.mipi_csi2
.flags
&
1375 V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK
;
1380 static int ov5647_probe(struct i2c_client
*client
)
1382 struct device_node
*np
= client
->dev
.of_node
;
1383 struct device
*dev
= &client
->dev
;
1384 struct ov5647
*sensor
;
1385 struct v4l2_subdev
*sd
;
1389 sensor
= devm_kzalloc(dev
, sizeof(*sensor
), GFP_KERNEL
);
1393 if (IS_ENABLED(CONFIG_OF
) && np
) {
1394 ret
= ov5647_parse_dt(sensor
, np
);
1396 dev_err(dev
, "DT parsing error: %d\n", ret
);
1401 sensor
->xclk
= devm_clk_get(dev
, NULL
);
1402 if (IS_ERR(sensor
->xclk
)) {
1403 dev_err(dev
, "could not get xclk");
1404 return PTR_ERR(sensor
->xclk
);
1407 xclk_freq
= clk_get_rate(sensor
->xclk
);
1408 if (xclk_freq
!= 25000000) {
1409 dev_err(dev
, "Unsupported clock frequency: %u\n", xclk_freq
);
1413 /* Request the power down GPIO asserted. */
1414 sensor
->pwdn
= devm_gpiod_get_optional(dev
, "pwdn", GPIOD_OUT_HIGH
);
1415 if (IS_ERR(sensor
->pwdn
)) {
1416 dev_err(dev
, "Failed to get 'pwdn' gpio\n");
1420 mutex_init(&sensor
->lock
);
1422 sensor
->mode
= OV5647_DEFAULT_MODE
;
1424 ret
= ov5647_init_controls(sensor
);
1429 v4l2_i2c_subdev_init(sd
, client
, &ov5647_subdev_ops
);
1430 sd
->internal_ops
= &ov5647_subdev_internal_ops
;
1431 sd
->flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
| V4L2_SUBDEV_FL_HAS_EVENTS
;
1433 sensor
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1434 sd
->entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1435 ret
= media_entity_pads_init(&sd
->entity
, 1, &sensor
->pad
);
1437 goto ctrl_handler_free
;
1439 ret
= ov5647_power_on(dev
);
1441 goto entity_cleanup
;
1443 ret
= ov5647_detect(sd
);
1447 ret
= v4l2_async_register_subdev(sd
);
1451 /* Enable runtime PM and turn off the device */
1452 pm_runtime_set_active(dev
);
1453 pm_runtime_enable(dev
);
1454 pm_runtime_idle(dev
);
1456 dev_dbg(dev
, "OmniVision OV5647 camera driver probed\n");
1461 ov5647_power_off(dev
);
1463 media_entity_cleanup(&sd
->entity
);
1465 v4l2_ctrl_handler_free(&sensor
->ctrls
);
1467 mutex_destroy(&sensor
->lock
);
1472 static void ov5647_remove(struct i2c_client
*client
)
1474 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1475 struct ov5647
*sensor
= to_sensor(sd
);
1477 v4l2_async_unregister_subdev(&sensor
->sd
);
1478 media_entity_cleanup(&sensor
->sd
.entity
);
1479 v4l2_ctrl_handler_free(&sensor
->ctrls
);
1480 v4l2_device_unregister_subdev(sd
);
1481 pm_runtime_disable(&client
->dev
);
1482 mutex_destroy(&sensor
->lock
);
1485 static const struct dev_pm_ops ov5647_pm_ops
= {
1486 SET_RUNTIME_PM_OPS(ov5647_power_off
, ov5647_power_on
, NULL
)
1489 static const struct i2c_device_id ov5647_id
[] = {
1493 MODULE_DEVICE_TABLE(i2c
, ov5647_id
);
1495 #if IS_ENABLED(CONFIG_OF)
1496 static const struct of_device_id ov5647_of_match
[] = {
1497 { .compatible
= "ovti,ov5647" },
1500 MODULE_DEVICE_TABLE(of
, ov5647_of_match
);
1503 static struct i2c_driver ov5647_driver
= {
1505 .of_match_table
= of_match_ptr(ov5647_of_match
),
1507 .pm
= &ov5647_pm_ops
,
1509 .probe
= ov5647_probe
,
1510 .remove
= ov5647_remove
,
1511 .id_table
= ov5647_id
,
1514 module_i2c_driver(ov5647_driver
);
1516 MODULE_AUTHOR("Ramiro Oliveira <roliveir@synopsys.com>");
1517 MODULE_DESCRIPTION("A low-level driver for OmniVision ov5647 sensors");
1518 MODULE_LICENSE("GPL v2");