1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for the OV5645 camera sensor.
5 * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
6 * Copyright (C) 2015 By Tech Design S.L. All Rights Reserved.
7 * Copyright (C) 2012-2013 Freescale Semiconductor, Inc. All Rights Reserved.
10 * - the OV5645 driver from QC msm-3.10 kernel on codeaurora.org:
11 * https://us.codeaurora.org/cgit/quic/la/kernel/msm-3.10/tree/drivers/
12 * media/platform/msm/camera_v2/sensor/ov5645.c?h=LA.BR.1.2.4_rb1.41
13 * - the OV5640 driver posted on linux-media:
14 * https://www.mail-archive.com/linux-media%40vger.kernel.org/msg92671.html
20 #include <linux/bitops.h>
21 #include <linux/clk.h>
22 #include <linux/delay.h>
23 #include <linux/device.h>
24 #include <linux/gpio/consumer.h>
25 #include <linux/i2c.h>
26 #include <linux/init.h>
27 #include <linux/module.h>
29 #include <linux/of_graph.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/slab.h>
32 #include <linux/types.h>
33 #include <media/v4l2-ctrls.h>
34 #include <media/v4l2-fwnode.h>
35 #include <media/v4l2-subdev.h>
37 #define OV5645_VOLTAGE_ANALOG 2800000
38 #define OV5645_VOLTAGE_DIGITAL_CORE 1500000
39 #define OV5645_VOLTAGE_DIGITAL_IO 1800000
41 #define OV5645_SYSTEM_CTRL0 0x3008
42 #define OV5645_SYSTEM_CTRL0_START 0x02
43 #define OV5645_SYSTEM_CTRL0_STOP 0x42
44 #define OV5645_CHIP_ID_HIGH 0x300a
45 #define OV5645_CHIP_ID_HIGH_BYTE 0x56
46 #define OV5645_CHIP_ID_LOW 0x300b
47 #define OV5645_CHIP_ID_LOW_BYTE 0x45
48 #define OV5645_AWB_MANUAL_CONTROL 0x3406
49 #define OV5645_AWB_MANUAL_ENABLE BIT(0)
50 #define OV5645_AEC_PK_MANUAL 0x3503
51 #define OV5645_AEC_MANUAL_ENABLE BIT(0)
52 #define OV5645_AGC_MANUAL_ENABLE BIT(1)
53 #define OV5645_TIMING_TC_REG20 0x3820
54 #define OV5645_SENSOR_VFLIP BIT(1)
55 #define OV5645_ISP_VFLIP BIT(2)
56 #define OV5645_TIMING_TC_REG21 0x3821
57 #define OV5645_SENSOR_MIRROR BIT(1)
58 #define OV5645_PRE_ISP_TEST_SETTING_1 0x503d
59 #define OV5645_TEST_PATTERN_MASK 0x3
60 #define OV5645_SET_TEST_PATTERN(x) ((x) & OV5645_TEST_PATTERN_MASK)
61 #define OV5645_TEST_PATTERN_ENABLE BIT(7)
62 #define OV5645_SDE_SAT_U 0x5583
63 #define OV5645_SDE_SAT_V 0x5584
70 struct ov5645_mode_info
{
73 const struct reg_value
*data
;
80 struct i2c_client
*i2c_client
;
82 struct v4l2_subdev sd
;
84 struct v4l2_fwnode_endpoint ep
;
85 struct v4l2_mbus_framefmt fmt
;
86 struct v4l2_rect crop
;
89 struct regulator
*io_regulator
;
90 struct regulator
*core_regulator
;
91 struct regulator
*analog_regulator
;
93 const struct ov5645_mode_info
*current_mode
;
95 struct v4l2_ctrl_handler ctrls
;
96 struct v4l2_ctrl
*pixel_clock
;
97 struct v4l2_ctrl
*link_freq
;
99 /* Cached register values */
104 struct mutex power_lock
; /* lock to protect power state */
107 struct gpio_desc
*enable_gpio
;
108 struct gpio_desc
*rst_gpio
;
111 static inline struct ov5645
*to_ov5645(struct v4l2_subdev
*sd
)
113 return container_of(sd
, struct ov5645
, sd
);
116 static const struct reg_value ov5645_global_init_setting
[] = {
356 static const struct reg_value ov5645_setting_sxga
[] = {
404 static const struct reg_value ov5645_setting_1080p
[] = {
454 static const struct reg_value ov5645_setting_full
[] = {
504 static const s64 link_freq
[] = {
509 static const struct ov5645_mode_info ov5645_mode_info_data
[] = {
513 .data
= ov5645_setting_sxga
,
514 .data_size
= ARRAY_SIZE(ov5645_setting_sxga
),
515 .pixel_clock
= 112000000,
516 .link_freq
= 0 /* an index in link_freq[] */
521 .data
= ov5645_setting_1080p
,
522 .data_size
= ARRAY_SIZE(ov5645_setting_1080p
),
523 .pixel_clock
= 168000000,
524 .link_freq
= 1 /* an index in link_freq[] */
529 .data
= ov5645_setting_full
,
530 .data_size
= ARRAY_SIZE(ov5645_setting_full
),
531 .pixel_clock
= 168000000,
532 .link_freq
= 1 /* an index in link_freq[] */
536 static int ov5645_regulators_enable(struct ov5645
*ov5645
)
540 ret
= regulator_enable(ov5645
->io_regulator
);
542 dev_err(ov5645
->dev
, "set io voltage failed\n");
546 ret
= regulator_enable(ov5645
->analog_regulator
);
548 dev_err(ov5645
->dev
, "set analog voltage failed\n");
552 ret
= regulator_enable(ov5645
->core_regulator
);
554 dev_err(ov5645
->dev
, "set core voltage failed\n");
555 goto err_disable_analog
;
561 regulator_disable(ov5645
->analog_regulator
);
563 regulator_disable(ov5645
->io_regulator
);
568 static void ov5645_regulators_disable(struct ov5645
*ov5645
)
572 ret
= regulator_disable(ov5645
->core_regulator
);
574 dev_err(ov5645
->dev
, "core regulator disable failed\n");
576 ret
= regulator_disable(ov5645
->analog_regulator
);
578 dev_err(ov5645
->dev
, "analog regulator disable failed\n");
580 ret
= regulator_disable(ov5645
->io_regulator
);
582 dev_err(ov5645
->dev
, "io regulator disable failed\n");
585 static int ov5645_write_reg(struct ov5645
*ov5645
, u16 reg
, u8 val
)
590 regbuf
[0] = reg
>> 8;
591 regbuf
[1] = reg
& 0xff;
594 ret
= i2c_master_send(ov5645
->i2c_client
, regbuf
, 3);
596 dev_err(ov5645
->dev
, "%s: write reg error %d: reg=%x, val=%x\n",
597 __func__
, ret
, reg
, val
);
604 static int ov5645_read_reg(struct ov5645
*ov5645
, u16 reg
, u8
*val
)
609 regbuf
[0] = reg
>> 8;
610 regbuf
[1] = reg
& 0xff;
612 ret
= i2c_master_send(ov5645
->i2c_client
, regbuf
, 2);
614 dev_err(ov5645
->dev
, "%s: write reg error %d: reg=%x\n",
619 ret
= i2c_master_recv(ov5645
->i2c_client
, val
, 1);
621 dev_err(ov5645
->dev
, "%s: read reg error %d: reg=%x\n",
629 static int ov5645_set_aec_mode(struct ov5645
*ov5645
, u32 mode
)
631 u8 val
= ov5645
->aec_pk_manual
;
634 if (mode
== V4L2_EXPOSURE_AUTO
)
635 val
&= ~OV5645_AEC_MANUAL_ENABLE
;
636 else /* V4L2_EXPOSURE_MANUAL */
637 val
|= OV5645_AEC_MANUAL_ENABLE
;
639 ret
= ov5645_write_reg(ov5645
, OV5645_AEC_PK_MANUAL
, val
);
641 ov5645
->aec_pk_manual
= val
;
646 static int ov5645_set_agc_mode(struct ov5645
*ov5645
, u32 enable
)
648 u8 val
= ov5645
->aec_pk_manual
;
652 val
&= ~OV5645_AGC_MANUAL_ENABLE
;
654 val
|= OV5645_AGC_MANUAL_ENABLE
;
656 ret
= ov5645_write_reg(ov5645
, OV5645_AEC_PK_MANUAL
, val
);
658 ov5645
->aec_pk_manual
= val
;
663 static int ov5645_set_register_array(struct ov5645
*ov5645
,
664 const struct reg_value
*settings
,
665 unsigned int num_settings
)
670 for (i
= 0; i
< num_settings
; ++i
, ++settings
) {
671 ret
= ov5645_write_reg(ov5645
, settings
->reg
, settings
->val
);
679 static int ov5645_set_power_on(struct ov5645
*ov5645
)
683 ret
= ov5645_regulators_enable(ov5645
);
688 ret
= clk_prepare_enable(ov5645
->xclk
);
690 dev_err(ov5645
->dev
, "clk prepare enable failed\n");
691 ov5645_regulators_disable(ov5645
);
695 usleep_range(5000, 15000);
696 gpiod_set_value_cansleep(ov5645
->enable_gpio
, 1);
698 usleep_range(1000, 2000);
699 gpiod_set_value_cansleep(ov5645
->rst_gpio
, 0);
706 static void ov5645_set_power_off(struct ov5645
*ov5645
)
708 gpiod_set_value_cansleep(ov5645
->rst_gpio
, 1);
709 gpiod_set_value_cansleep(ov5645
->enable_gpio
, 0);
710 clk_disable_unprepare(ov5645
->xclk
);
711 ov5645_regulators_disable(ov5645
);
714 static int ov5645_s_power(struct v4l2_subdev
*sd
, int on
)
716 struct ov5645
*ov5645
= to_ov5645(sd
);
719 mutex_lock(&ov5645
->power_lock
);
721 /* If the power count is modified from 0 to != 0 or from != 0 to 0,
722 * update the power state.
724 if (ov5645
->power_count
== !on
) {
726 ret
= ov5645_set_power_on(ov5645
);
730 ret
= ov5645_set_register_array(ov5645
,
731 ov5645_global_init_setting
,
732 ARRAY_SIZE(ov5645_global_init_setting
));
735 "could not set init registers\n");
736 ov5645_set_power_off(ov5645
);
740 ret
= ov5645_write_reg(ov5645
, OV5645_SYSTEM_CTRL0
,
741 OV5645_SYSTEM_CTRL0_STOP
);
743 ov5645_set_power_off(ov5645
);
747 ov5645_set_power_off(ov5645
);
751 /* Update the power count. */
752 ov5645
->power_count
+= on
? 1 : -1;
753 WARN_ON(ov5645
->power_count
< 0);
756 mutex_unlock(&ov5645
->power_lock
);
761 static int ov5645_set_saturation(struct ov5645
*ov5645
, s32 value
)
763 u32 reg_value
= (value
* 0x10) + 0x40;
766 ret
= ov5645_write_reg(ov5645
, OV5645_SDE_SAT_U
, reg_value
);
770 return ov5645_write_reg(ov5645
, OV5645_SDE_SAT_V
, reg_value
);
773 static int ov5645_set_hflip(struct ov5645
*ov5645
, s32 value
)
775 u8 val
= ov5645
->timing_tc_reg21
;
779 val
&= ~(OV5645_SENSOR_MIRROR
);
781 val
|= (OV5645_SENSOR_MIRROR
);
783 ret
= ov5645_write_reg(ov5645
, OV5645_TIMING_TC_REG21
, val
);
785 ov5645
->timing_tc_reg21
= val
;
790 static int ov5645_set_vflip(struct ov5645
*ov5645
, s32 value
)
792 u8 val
= ov5645
->timing_tc_reg20
;
796 val
|= (OV5645_SENSOR_VFLIP
| OV5645_ISP_VFLIP
);
798 val
&= ~(OV5645_SENSOR_VFLIP
| OV5645_ISP_VFLIP
);
800 ret
= ov5645_write_reg(ov5645
, OV5645_TIMING_TC_REG20
, val
);
802 ov5645
->timing_tc_reg20
= val
;
807 static int ov5645_set_test_pattern(struct ov5645
*ov5645
, s32 value
)
812 val
= OV5645_SET_TEST_PATTERN(value
- 1);
813 val
|= OV5645_TEST_PATTERN_ENABLE
;
816 return ov5645_write_reg(ov5645
, OV5645_PRE_ISP_TEST_SETTING_1
, val
);
819 static const char * const ov5645_test_pattern_menu
[] = {
821 "Vertical Color Bars",
822 "Pseudo-Random Data",
827 static int ov5645_set_awb(struct ov5645
*ov5645
, s32 enable_auto
)
832 val
= OV5645_AWB_MANUAL_ENABLE
;
834 return ov5645_write_reg(ov5645
, OV5645_AWB_MANUAL_CONTROL
, val
);
837 static int ov5645_s_ctrl(struct v4l2_ctrl
*ctrl
)
839 struct ov5645
*ov5645
= container_of(ctrl
->handler
,
840 struct ov5645
, ctrls
);
843 mutex_lock(&ov5645
->power_lock
);
844 if (!ov5645
->power_count
) {
845 mutex_unlock(&ov5645
->power_lock
);
850 case V4L2_CID_SATURATION
:
851 ret
= ov5645_set_saturation(ov5645
, ctrl
->val
);
853 case V4L2_CID_AUTO_WHITE_BALANCE
:
854 ret
= ov5645_set_awb(ov5645
, ctrl
->val
);
856 case V4L2_CID_AUTOGAIN
:
857 ret
= ov5645_set_agc_mode(ov5645
, ctrl
->val
);
859 case V4L2_CID_EXPOSURE_AUTO
:
860 ret
= ov5645_set_aec_mode(ov5645
, ctrl
->val
);
862 case V4L2_CID_TEST_PATTERN
:
863 ret
= ov5645_set_test_pattern(ov5645
, ctrl
->val
);
866 ret
= ov5645_set_hflip(ov5645
, ctrl
->val
);
869 ret
= ov5645_set_vflip(ov5645
, ctrl
->val
);
876 mutex_unlock(&ov5645
->power_lock
);
881 static const struct v4l2_ctrl_ops ov5645_ctrl_ops
= {
882 .s_ctrl
= ov5645_s_ctrl
,
885 static int ov5645_enum_mbus_code(struct v4l2_subdev
*sd
,
886 struct v4l2_subdev_pad_config
*cfg
,
887 struct v4l2_subdev_mbus_code_enum
*code
)
892 code
->code
= MEDIA_BUS_FMT_UYVY8_2X8
;
897 static int ov5645_enum_frame_size(struct v4l2_subdev
*subdev
,
898 struct v4l2_subdev_pad_config
*cfg
,
899 struct v4l2_subdev_frame_size_enum
*fse
)
901 if (fse
->code
!= MEDIA_BUS_FMT_UYVY8_2X8
)
904 if (fse
->index
>= ARRAY_SIZE(ov5645_mode_info_data
))
907 fse
->min_width
= ov5645_mode_info_data
[fse
->index
].width
;
908 fse
->max_width
= ov5645_mode_info_data
[fse
->index
].width
;
909 fse
->min_height
= ov5645_mode_info_data
[fse
->index
].height
;
910 fse
->max_height
= ov5645_mode_info_data
[fse
->index
].height
;
915 static struct v4l2_mbus_framefmt
*
916 __ov5645_get_pad_format(struct ov5645
*ov5645
,
917 struct v4l2_subdev_pad_config
*cfg
,
919 enum v4l2_subdev_format_whence which
)
922 case V4L2_SUBDEV_FORMAT_TRY
:
923 return v4l2_subdev_get_try_format(&ov5645
->sd
, cfg
, pad
);
924 case V4L2_SUBDEV_FORMAT_ACTIVE
:
931 static int ov5645_get_format(struct v4l2_subdev
*sd
,
932 struct v4l2_subdev_pad_config
*cfg
,
933 struct v4l2_subdev_format
*format
)
935 struct ov5645
*ov5645
= to_ov5645(sd
);
937 format
->format
= *__ov5645_get_pad_format(ov5645
, cfg
, format
->pad
,
942 static struct v4l2_rect
*
943 __ov5645_get_pad_crop(struct ov5645
*ov5645
, struct v4l2_subdev_pad_config
*cfg
,
944 unsigned int pad
, enum v4l2_subdev_format_whence which
)
947 case V4L2_SUBDEV_FORMAT_TRY
:
948 return v4l2_subdev_get_try_crop(&ov5645
->sd
, cfg
, pad
);
949 case V4L2_SUBDEV_FORMAT_ACTIVE
:
950 return &ov5645
->crop
;
956 static int ov5645_set_format(struct v4l2_subdev
*sd
,
957 struct v4l2_subdev_pad_config
*cfg
,
958 struct v4l2_subdev_format
*format
)
960 struct ov5645
*ov5645
= to_ov5645(sd
);
961 struct v4l2_mbus_framefmt
*__format
;
962 struct v4l2_rect
*__crop
;
963 const struct ov5645_mode_info
*new_mode
;
966 __crop
= __ov5645_get_pad_crop(ov5645
, cfg
, format
->pad
,
969 new_mode
= v4l2_find_nearest_size(ov5645_mode_info_data
,
970 ARRAY_SIZE(ov5645_mode_info_data
),
972 format
->format
.width
, format
->format
.height
);
974 __crop
->width
= new_mode
->width
;
975 __crop
->height
= new_mode
->height
;
977 if (format
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
978 ret
= v4l2_ctrl_s_ctrl_int64(ov5645
->pixel_clock
,
979 new_mode
->pixel_clock
);
983 ret
= v4l2_ctrl_s_ctrl(ov5645
->link_freq
,
984 new_mode
->link_freq
);
988 ov5645
->current_mode
= new_mode
;
991 __format
= __ov5645_get_pad_format(ov5645
, cfg
, format
->pad
,
993 __format
->width
= __crop
->width
;
994 __format
->height
= __crop
->height
;
995 __format
->code
= MEDIA_BUS_FMT_UYVY8_2X8
;
996 __format
->field
= V4L2_FIELD_NONE
;
997 __format
->colorspace
= V4L2_COLORSPACE_SRGB
;
999 format
->format
= *__format
;
1004 static int ov5645_entity_init_cfg(struct v4l2_subdev
*subdev
,
1005 struct v4l2_subdev_pad_config
*cfg
)
1007 struct v4l2_subdev_format fmt
= { 0 };
1009 fmt
.which
= cfg
? V4L2_SUBDEV_FORMAT_TRY
: V4L2_SUBDEV_FORMAT_ACTIVE
;
1010 fmt
.format
.width
= 1920;
1011 fmt
.format
.height
= 1080;
1013 ov5645_set_format(subdev
, cfg
, &fmt
);
1018 static int ov5645_get_selection(struct v4l2_subdev
*sd
,
1019 struct v4l2_subdev_pad_config
*cfg
,
1020 struct v4l2_subdev_selection
*sel
)
1022 struct ov5645
*ov5645
= to_ov5645(sd
);
1024 if (sel
->target
!= V4L2_SEL_TGT_CROP
)
1027 sel
->r
= *__ov5645_get_pad_crop(ov5645
, cfg
, sel
->pad
,
1032 static int ov5645_s_stream(struct v4l2_subdev
*subdev
, int enable
)
1034 struct ov5645
*ov5645
= to_ov5645(subdev
);
1038 ret
= ov5645_set_register_array(ov5645
,
1039 ov5645
->current_mode
->data
,
1040 ov5645
->current_mode
->data_size
);
1042 dev_err(ov5645
->dev
, "could not set mode %dx%d\n",
1043 ov5645
->current_mode
->width
,
1044 ov5645
->current_mode
->height
);
1047 ret
= v4l2_ctrl_handler_setup(&ov5645
->ctrls
);
1049 dev_err(ov5645
->dev
, "could not sync v4l2 controls\n");
1052 ret
= ov5645_write_reg(ov5645
, OV5645_SYSTEM_CTRL0
,
1053 OV5645_SYSTEM_CTRL0_START
);
1057 ret
= ov5645_write_reg(ov5645
, OV5645_SYSTEM_CTRL0
,
1058 OV5645_SYSTEM_CTRL0_STOP
);
1066 static const struct v4l2_subdev_core_ops ov5645_core_ops
= {
1067 .s_power
= ov5645_s_power
,
1070 static const struct v4l2_subdev_video_ops ov5645_video_ops
= {
1071 .s_stream
= ov5645_s_stream
,
1074 static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops
= {
1075 .init_cfg
= ov5645_entity_init_cfg
,
1076 .enum_mbus_code
= ov5645_enum_mbus_code
,
1077 .enum_frame_size
= ov5645_enum_frame_size
,
1078 .get_fmt
= ov5645_get_format
,
1079 .set_fmt
= ov5645_set_format
,
1080 .get_selection
= ov5645_get_selection
,
1083 static const struct v4l2_subdev_ops ov5645_subdev_ops
= {
1084 .core
= &ov5645_core_ops
,
1085 .video
= &ov5645_video_ops
,
1086 .pad
= &ov5645_subdev_pad_ops
,
1089 static int ov5645_probe(struct i2c_client
*client
,
1090 const struct i2c_device_id
*id
)
1092 struct device
*dev
= &client
->dev
;
1093 struct device_node
*endpoint
;
1094 struct ov5645
*ov5645
;
1095 u8 chip_id_high
, chip_id_low
;
1099 ov5645
= devm_kzalloc(dev
, sizeof(struct ov5645
), GFP_KERNEL
);
1103 ov5645
->i2c_client
= client
;
1106 endpoint
= of_graph_get_next_endpoint(dev
->of_node
, NULL
);
1108 dev_err(dev
, "endpoint node not found\n");
1112 ret
= v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint
),
1115 of_node_put(endpoint
);
1118 dev_err(dev
, "parsing endpoint node failed\n");
1122 if (ov5645
->ep
.bus_type
!= V4L2_MBUS_CSI2_DPHY
) {
1123 dev_err(dev
, "invalid bus type, must be CSI2\n");
1127 /* get system clock (xclk) */
1128 ov5645
->xclk
= devm_clk_get(dev
, "xclk");
1129 if (IS_ERR(ov5645
->xclk
)) {
1130 dev_err(dev
, "could not get xclk");
1131 return PTR_ERR(ov5645
->xclk
);
1134 ret
= of_property_read_u32(dev
->of_node
, "clock-frequency", &xclk_freq
);
1136 dev_err(dev
, "could not get xclk frequency\n");
1140 /* external clock must be 24MHz, allow 1% tolerance */
1141 if (xclk_freq
< 23760000 || xclk_freq
> 24240000) {
1142 dev_err(dev
, "external clock frequency %u is not supported\n",
1147 ret
= clk_set_rate(ov5645
->xclk
, xclk_freq
);
1149 dev_err(dev
, "could not set xclk frequency\n");
1153 ov5645
->io_regulator
= devm_regulator_get(dev
, "vdddo");
1154 if (IS_ERR(ov5645
->io_regulator
)) {
1155 dev_err(dev
, "cannot get io regulator\n");
1156 return PTR_ERR(ov5645
->io_regulator
);
1159 ret
= regulator_set_voltage(ov5645
->io_regulator
,
1160 OV5645_VOLTAGE_DIGITAL_IO
,
1161 OV5645_VOLTAGE_DIGITAL_IO
);
1163 dev_err(dev
, "cannot set io voltage\n");
1167 ov5645
->core_regulator
= devm_regulator_get(dev
, "vddd");
1168 if (IS_ERR(ov5645
->core_regulator
)) {
1169 dev_err(dev
, "cannot get core regulator\n");
1170 return PTR_ERR(ov5645
->core_regulator
);
1173 ret
= regulator_set_voltage(ov5645
->core_regulator
,
1174 OV5645_VOLTAGE_DIGITAL_CORE
,
1175 OV5645_VOLTAGE_DIGITAL_CORE
);
1177 dev_err(dev
, "cannot set core voltage\n");
1181 ov5645
->analog_regulator
= devm_regulator_get(dev
, "vdda");
1182 if (IS_ERR(ov5645
->analog_regulator
)) {
1183 dev_err(dev
, "cannot get analog regulator\n");
1184 return PTR_ERR(ov5645
->analog_regulator
);
1187 ret
= regulator_set_voltage(ov5645
->analog_regulator
,
1188 OV5645_VOLTAGE_ANALOG
,
1189 OV5645_VOLTAGE_ANALOG
);
1191 dev_err(dev
, "cannot set analog voltage\n");
1195 ov5645
->enable_gpio
= devm_gpiod_get(dev
, "enable", GPIOD_OUT_HIGH
);
1196 if (IS_ERR(ov5645
->enable_gpio
)) {
1197 dev_err(dev
, "cannot get enable gpio\n");
1198 return PTR_ERR(ov5645
->enable_gpio
);
1201 ov5645
->rst_gpio
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_HIGH
);
1202 if (IS_ERR(ov5645
->rst_gpio
)) {
1203 dev_err(dev
, "cannot get reset gpio\n");
1204 return PTR_ERR(ov5645
->rst_gpio
);
1207 mutex_init(&ov5645
->power_lock
);
1209 v4l2_ctrl_handler_init(&ov5645
->ctrls
, 9);
1210 v4l2_ctrl_new_std(&ov5645
->ctrls
, &ov5645_ctrl_ops
,
1211 V4L2_CID_SATURATION
, -4, 4, 1, 0);
1212 v4l2_ctrl_new_std(&ov5645
->ctrls
, &ov5645_ctrl_ops
,
1213 V4L2_CID_HFLIP
, 0, 1, 1, 0);
1214 v4l2_ctrl_new_std(&ov5645
->ctrls
, &ov5645_ctrl_ops
,
1215 V4L2_CID_VFLIP
, 0, 1, 1, 0);
1216 v4l2_ctrl_new_std(&ov5645
->ctrls
, &ov5645_ctrl_ops
,
1217 V4L2_CID_AUTOGAIN
, 0, 1, 1, 1);
1218 v4l2_ctrl_new_std(&ov5645
->ctrls
, &ov5645_ctrl_ops
,
1219 V4L2_CID_AUTO_WHITE_BALANCE
, 0, 1, 1, 1);
1220 v4l2_ctrl_new_std_menu(&ov5645
->ctrls
, &ov5645_ctrl_ops
,
1221 V4L2_CID_EXPOSURE_AUTO
, V4L2_EXPOSURE_MANUAL
,
1222 0, V4L2_EXPOSURE_AUTO
);
1223 v4l2_ctrl_new_std_menu_items(&ov5645
->ctrls
, &ov5645_ctrl_ops
,
1224 V4L2_CID_TEST_PATTERN
,
1225 ARRAY_SIZE(ov5645_test_pattern_menu
) - 1,
1226 0, 0, ov5645_test_pattern_menu
);
1227 ov5645
->pixel_clock
= v4l2_ctrl_new_std(&ov5645
->ctrls
,
1229 V4L2_CID_PIXEL_RATE
,
1231 ov5645
->link_freq
= v4l2_ctrl_new_int_menu(&ov5645
->ctrls
,
1234 ARRAY_SIZE(link_freq
) - 1,
1236 if (ov5645
->link_freq
)
1237 ov5645
->link_freq
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1239 ov5645
->sd
.ctrl_handler
= &ov5645
->ctrls
;
1241 if (ov5645
->ctrls
.error
) {
1242 dev_err(dev
, "%s: control initialization error %d\n",
1243 __func__
, ov5645
->ctrls
.error
);
1244 ret
= ov5645
->ctrls
.error
;
1248 v4l2_i2c_subdev_init(&ov5645
->sd
, client
, &ov5645_subdev_ops
);
1249 ov5645
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1250 ov5645
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1251 ov5645
->sd
.dev
= &client
->dev
;
1252 ov5645
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1254 ret
= media_entity_pads_init(&ov5645
->sd
.entity
, 1, &ov5645
->pad
);
1256 dev_err(dev
, "could not register media entity\n");
1260 ret
= ov5645_s_power(&ov5645
->sd
, true);
1262 dev_err(dev
, "could not power up OV5645\n");
1266 ret
= ov5645_read_reg(ov5645
, OV5645_CHIP_ID_HIGH
, &chip_id_high
);
1267 if (ret
< 0 || chip_id_high
!= OV5645_CHIP_ID_HIGH_BYTE
) {
1268 dev_err(dev
, "could not read ID high\n");
1272 ret
= ov5645_read_reg(ov5645
, OV5645_CHIP_ID_LOW
, &chip_id_low
);
1273 if (ret
< 0 || chip_id_low
!= OV5645_CHIP_ID_LOW_BYTE
) {
1274 dev_err(dev
, "could not read ID low\n");
1279 dev_info(dev
, "OV5645 detected at address 0x%02x\n", client
->addr
);
1281 ret
= ov5645_read_reg(ov5645
, OV5645_AEC_PK_MANUAL
,
1282 &ov5645
->aec_pk_manual
);
1284 dev_err(dev
, "could not read AEC/AGC mode\n");
1289 ret
= ov5645_read_reg(ov5645
, OV5645_TIMING_TC_REG20
,
1290 &ov5645
->timing_tc_reg20
);
1292 dev_err(dev
, "could not read vflip value\n");
1297 ret
= ov5645_read_reg(ov5645
, OV5645_TIMING_TC_REG21
,
1298 &ov5645
->timing_tc_reg21
);
1300 dev_err(dev
, "could not read hflip value\n");
1305 ov5645_s_power(&ov5645
->sd
, false);
1307 ret
= v4l2_async_register_subdev(&ov5645
->sd
);
1309 dev_err(dev
, "could not register v4l2 device\n");
1313 ov5645_entity_init_cfg(&ov5645
->sd
, NULL
);
1318 ov5645_s_power(&ov5645
->sd
, false);
1320 media_entity_cleanup(&ov5645
->sd
.entity
);
1322 v4l2_ctrl_handler_free(&ov5645
->ctrls
);
1323 mutex_destroy(&ov5645
->power_lock
);
1328 static int ov5645_remove(struct i2c_client
*client
)
1330 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1331 struct ov5645
*ov5645
= to_ov5645(sd
);
1333 v4l2_async_unregister_subdev(&ov5645
->sd
);
1334 media_entity_cleanup(&ov5645
->sd
.entity
);
1335 v4l2_ctrl_handler_free(&ov5645
->ctrls
);
1336 mutex_destroy(&ov5645
->power_lock
);
1341 static const struct i2c_device_id ov5645_id
[] = {
1345 MODULE_DEVICE_TABLE(i2c
, ov5645_id
);
1347 static const struct of_device_id ov5645_of_match
[] = {
1348 { .compatible
= "ovti,ov5645" },
1351 MODULE_DEVICE_TABLE(of
, ov5645_of_match
);
1353 static struct i2c_driver ov5645_i2c_driver
= {
1355 .of_match_table
= of_match_ptr(ov5645_of_match
),
1358 .probe
= ov5645_probe
,
1359 .remove
= ov5645_remove
,
1360 .id_table
= ov5645_id
,
1363 module_i2c_driver(ov5645_i2c_driver
);
1365 MODULE_DESCRIPTION("Omnivision OV5645 Camera Driver");
1366 MODULE_AUTHOR("Todor Tomov <todor.tomov@linaro.org>");
1367 MODULE_LICENSE("GPL v2");