2 * Driver for the OV5645 camera sensor.
4 * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
5 * Copyright (C) 2015 By Tech Design S.L. All Rights Reserved.
6 * Copyright (C) 2012-2013 Freescale Semiconductor, Inc. All Rights Reserved.
9 * - the OV5645 driver from QC msm-3.10 kernel on codeaurora.org:
10 * https://us.codeaurora.org/cgit/quic/la/kernel/msm-3.10/tree/drivers/
11 * media/platform/msm/camera_v2/sensor/ov5645.c?h=LA.BR.1.2.4_rb1.41
12 * - the OV5640 driver posted on linux-media:
13 * https://www.mail-archive.com/linux-media%40vger.kernel.org/msg92671.html
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
28 #include <linux/bitops.h>
29 #include <linux/clk.h>
30 #include <linux/delay.h>
31 #include <linux/device.h>
32 #include <linux/gpio/consumer.h>
33 #include <linux/i2c.h>
34 #include <linux/init.h>
35 #include <linux/module.h>
37 #include <linux/of_graph.h>
38 #include <linux/regulator/consumer.h>
39 #include <linux/slab.h>
40 #include <linux/types.h>
41 #include <media/v4l2-ctrls.h>
42 #include <media/v4l2-fwnode.h>
43 #include <media/v4l2-subdev.h>
45 #define OV5645_VOLTAGE_ANALOG 2800000
46 #define OV5645_VOLTAGE_DIGITAL_CORE 1500000
47 #define OV5645_VOLTAGE_DIGITAL_IO 1800000
49 #define OV5645_SYSTEM_CTRL0 0x3008
50 #define OV5645_SYSTEM_CTRL0_START 0x02
51 #define OV5645_SYSTEM_CTRL0_STOP 0x42
52 #define OV5645_CHIP_ID_HIGH 0x300a
53 #define OV5645_CHIP_ID_HIGH_BYTE 0x56
54 #define OV5645_CHIP_ID_LOW 0x300b
55 #define OV5645_CHIP_ID_LOW_BYTE 0x45
56 #define OV5645_AWB_MANUAL_CONTROL 0x3406
57 #define OV5645_AWB_MANUAL_ENABLE BIT(0)
58 #define OV5645_AEC_PK_MANUAL 0x3503
59 #define OV5645_AEC_MANUAL_ENABLE BIT(0)
60 #define OV5645_AGC_MANUAL_ENABLE BIT(1)
61 #define OV5645_TIMING_TC_REG20 0x3820
62 #define OV5645_SENSOR_VFLIP BIT(1)
63 #define OV5645_ISP_VFLIP BIT(2)
64 #define OV5645_TIMING_TC_REG21 0x3821
65 #define OV5645_SENSOR_MIRROR BIT(1)
66 #define OV5645_PRE_ISP_TEST_SETTING_1 0x503d
67 #define OV5645_TEST_PATTERN_MASK 0x3
68 #define OV5645_SET_TEST_PATTERN(x) ((x) & OV5645_TEST_PATTERN_MASK)
69 #define OV5645_TEST_PATTERN_ENABLE BIT(7)
70 #define OV5645_SDE_SAT_U 0x5583
71 #define OV5645_SDE_SAT_V 0x5584
78 struct ov5645_mode_info
{
81 const struct reg_value
*data
;
88 struct i2c_client
*i2c_client
;
90 struct v4l2_subdev sd
;
92 struct v4l2_fwnode_endpoint ep
;
93 struct v4l2_mbus_framefmt fmt
;
94 struct v4l2_rect crop
;
97 struct regulator
*io_regulator
;
98 struct regulator
*core_regulator
;
99 struct regulator
*analog_regulator
;
101 const struct ov5645_mode_info
*current_mode
;
103 struct v4l2_ctrl_handler ctrls
;
104 struct v4l2_ctrl
*pixel_clock
;
105 struct v4l2_ctrl
*link_freq
;
107 /* Cached register values */
112 struct mutex power_lock
; /* lock to protect power state */
115 struct gpio_desc
*enable_gpio
;
116 struct gpio_desc
*rst_gpio
;
119 static inline struct ov5645
*to_ov5645(struct v4l2_subdev
*sd
)
121 return container_of(sd
, struct ov5645
, sd
);
124 static const struct reg_value ov5645_global_init_setting
[] = {
364 static const struct reg_value ov5645_setting_sxga
[] = {
412 static const struct reg_value ov5645_setting_1080p
[] = {
462 static const struct reg_value ov5645_setting_full
[] = {
512 static const s64 link_freq
[] = {
517 static const struct ov5645_mode_info ov5645_mode_info_data
[] = {
521 .data
= ov5645_setting_sxga
,
522 .data_size
= ARRAY_SIZE(ov5645_setting_sxga
),
523 .pixel_clock
= 111440000,
524 .link_freq
= 0 /* an index in link_freq[] */
529 .data
= ov5645_setting_1080p
,
530 .data_size
= ARRAY_SIZE(ov5645_setting_1080p
),
531 .pixel_clock
= 167160000,
532 .link_freq
= 1 /* an index in link_freq[] */
537 .data
= ov5645_setting_full
,
538 .data_size
= ARRAY_SIZE(ov5645_setting_full
),
539 .pixel_clock
= 167160000,
540 .link_freq
= 1 /* an index in link_freq[] */
544 static int ov5645_regulators_enable(struct ov5645
*ov5645
)
548 ret
= regulator_enable(ov5645
->io_regulator
);
550 dev_err(ov5645
->dev
, "set io voltage failed\n");
554 ret
= regulator_enable(ov5645
->analog_regulator
);
556 dev_err(ov5645
->dev
, "set analog voltage failed\n");
560 ret
= regulator_enable(ov5645
->core_regulator
);
562 dev_err(ov5645
->dev
, "set core voltage failed\n");
563 goto err_disable_analog
;
569 regulator_disable(ov5645
->analog_regulator
);
571 regulator_disable(ov5645
->io_regulator
);
576 static void ov5645_regulators_disable(struct ov5645
*ov5645
)
580 ret
= regulator_disable(ov5645
->core_regulator
);
582 dev_err(ov5645
->dev
, "core regulator disable failed\n");
584 ret
= regulator_disable(ov5645
->analog_regulator
);
586 dev_err(ov5645
->dev
, "analog regulator disable failed\n");
588 ret
= regulator_disable(ov5645
->io_regulator
);
590 dev_err(ov5645
->dev
, "io regulator disable failed\n");
593 static int ov5645_write_reg(struct ov5645
*ov5645
, u16 reg
, u8 val
)
598 regbuf
[0] = reg
>> 8;
599 regbuf
[1] = reg
& 0xff;
602 ret
= i2c_master_send(ov5645
->i2c_client
, regbuf
, 3);
604 dev_err(ov5645
->dev
, "%s: write reg error %d: reg=%x, val=%x\n",
605 __func__
, ret
, reg
, val
);
610 static int ov5645_read_reg(struct ov5645
*ov5645
, u16 reg
, u8
*val
)
615 regbuf
[0] = reg
>> 8;
616 regbuf
[1] = reg
& 0xff;
618 ret
= i2c_master_send(ov5645
->i2c_client
, regbuf
, 2);
620 dev_err(ov5645
->dev
, "%s: write reg error %d: reg=%x\n",
625 ret
= i2c_master_recv(ov5645
->i2c_client
, val
, 1);
627 dev_err(ov5645
->dev
, "%s: read reg error %d: reg=%x\n",
635 static int ov5645_set_aec_mode(struct ov5645
*ov5645
, u32 mode
)
637 u8 val
= ov5645
->aec_pk_manual
;
640 if (mode
== V4L2_EXPOSURE_AUTO
)
641 val
&= ~OV5645_AEC_MANUAL_ENABLE
;
642 else /* V4L2_EXPOSURE_MANUAL */
643 val
|= OV5645_AEC_MANUAL_ENABLE
;
645 ret
= ov5645_write_reg(ov5645
, OV5645_AEC_PK_MANUAL
, val
);
647 ov5645
->aec_pk_manual
= val
;
652 static int ov5645_set_agc_mode(struct ov5645
*ov5645
, u32 enable
)
654 u8 val
= ov5645
->aec_pk_manual
;
658 val
&= ~OV5645_AGC_MANUAL_ENABLE
;
660 val
|= OV5645_AGC_MANUAL_ENABLE
;
662 ret
= ov5645_write_reg(ov5645
, OV5645_AEC_PK_MANUAL
, val
);
664 ov5645
->aec_pk_manual
= val
;
669 static int ov5645_set_register_array(struct ov5645
*ov5645
,
670 const struct reg_value
*settings
,
671 unsigned int num_settings
)
676 for (i
= 0; i
< num_settings
; ++i
, ++settings
) {
677 ret
= ov5645_write_reg(ov5645
, settings
->reg
, settings
->val
);
685 static int ov5645_set_power_on(struct ov5645
*ov5645
)
689 ret
= ov5645_regulators_enable(ov5645
);
694 ret
= clk_prepare_enable(ov5645
->xclk
);
696 dev_err(ov5645
->dev
, "clk prepare enable failed\n");
697 ov5645_regulators_disable(ov5645
);
701 usleep_range(5000, 15000);
702 gpiod_set_value_cansleep(ov5645
->enable_gpio
, 1);
704 usleep_range(1000, 2000);
705 gpiod_set_value_cansleep(ov5645
->rst_gpio
, 0);
712 static void ov5645_set_power_off(struct ov5645
*ov5645
)
714 gpiod_set_value_cansleep(ov5645
->rst_gpio
, 1);
715 gpiod_set_value_cansleep(ov5645
->enable_gpio
, 0);
716 clk_disable_unprepare(ov5645
->xclk
);
717 ov5645_regulators_disable(ov5645
);
720 static int ov5645_s_power(struct v4l2_subdev
*sd
, int on
)
722 struct ov5645
*ov5645
= to_ov5645(sd
);
725 mutex_lock(&ov5645
->power_lock
);
727 /* If the power count is modified from 0 to != 0 or from != 0 to 0,
728 * update the power state.
730 if (ov5645
->power_count
== !on
) {
732 ret
= ov5645_set_power_on(ov5645
);
736 ret
= ov5645_set_register_array(ov5645
,
737 ov5645_global_init_setting
,
738 ARRAY_SIZE(ov5645_global_init_setting
));
741 "could not set init registers\n");
742 ov5645_set_power_off(ov5645
);
746 ret
= ov5645_write_reg(ov5645
, OV5645_SYSTEM_CTRL0
,
747 OV5645_SYSTEM_CTRL0_STOP
);
749 ov5645_set_power_off(ov5645
);
753 ov5645_set_power_off(ov5645
);
757 /* Update the power count. */
758 ov5645
->power_count
+= on
? 1 : -1;
759 WARN_ON(ov5645
->power_count
< 0);
762 mutex_unlock(&ov5645
->power_lock
);
767 static int ov5645_set_saturation(struct ov5645
*ov5645
, s32 value
)
769 u32 reg_value
= (value
* 0x10) + 0x40;
772 ret
= ov5645_write_reg(ov5645
, OV5645_SDE_SAT_U
, reg_value
);
776 return ov5645_write_reg(ov5645
, OV5645_SDE_SAT_V
, reg_value
);
779 static int ov5645_set_hflip(struct ov5645
*ov5645
, s32 value
)
781 u8 val
= ov5645
->timing_tc_reg21
;
785 val
&= ~(OV5645_SENSOR_MIRROR
);
787 val
|= (OV5645_SENSOR_MIRROR
);
789 ret
= ov5645_write_reg(ov5645
, OV5645_TIMING_TC_REG21
, val
);
791 ov5645
->timing_tc_reg21
= val
;
796 static int ov5645_set_vflip(struct ov5645
*ov5645
, s32 value
)
798 u8 val
= ov5645
->timing_tc_reg20
;
802 val
|= (OV5645_SENSOR_VFLIP
| OV5645_ISP_VFLIP
);
804 val
&= ~(OV5645_SENSOR_VFLIP
| OV5645_ISP_VFLIP
);
806 ret
= ov5645_write_reg(ov5645
, OV5645_TIMING_TC_REG20
, val
);
808 ov5645
->timing_tc_reg20
= val
;
813 static int ov5645_set_test_pattern(struct ov5645
*ov5645
, s32 value
)
818 val
= OV5645_SET_TEST_PATTERN(value
- 1);
819 val
|= OV5645_TEST_PATTERN_ENABLE
;
822 return ov5645_write_reg(ov5645
, OV5645_PRE_ISP_TEST_SETTING_1
, val
);
825 static const char * const ov5645_test_pattern_menu
[] = {
827 "Vertical Color Bars",
828 "Pseudo-Random Data",
833 static int ov5645_set_awb(struct ov5645
*ov5645
, s32 enable_auto
)
838 val
= OV5645_AWB_MANUAL_ENABLE
;
840 return ov5645_write_reg(ov5645
, OV5645_AWB_MANUAL_CONTROL
, val
);
843 static int ov5645_s_ctrl(struct v4l2_ctrl
*ctrl
)
845 struct ov5645
*ov5645
= container_of(ctrl
->handler
,
846 struct ov5645
, ctrls
);
849 mutex_lock(&ov5645
->power_lock
);
850 if (!ov5645
->power_count
) {
851 mutex_unlock(&ov5645
->power_lock
);
856 case V4L2_CID_SATURATION
:
857 ret
= ov5645_set_saturation(ov5645
, ctrl
->val
);
859 case V4L2_CID_AUTO_WHITE_BALANCE
:
860 ret
= ov5645_set_awb(ov5645
, ctrl
->val
);
862 case V4L2_CID_AUTOGAIN
:
863 ret
= ov5645_set_agc_mode(ov5645
, ctrl
->val
);
865 case V4L2_CID_EXPOSURE_AUTO
:
866 ret
= ov5645_set_aec_mode(ov5645
, ctrl
->val
);
868 case V4L2_CID_TEST_PATTERN
:
869 ret
= ov5645_set_test_pattern(ov5645
, ctrl
->val
);
872 ret
= ov5645_set_hflip(ov5645
, ctrl
->val
);
875 ret
= ov5645_set_vflip(ov5645
, ctrl
->val
);
882 mutex_unlock(&ov5645
->power_lock
);
887 static struct v4l2_ctrl_ops ov5645_ctrl_ops
= {
888 .s_ctrl
= ov5645_s_ctrl
,
891 static int ov5645_enum_mbus_code(struct v4l2_subdev
*sd
,
892 struct v4l2_subdev_pad_config
*cfg
,
893 struct v4l2_subdev_mbus_code_enum
*code
)
898 code
->code
= MEDIA_BUS_FMT_UYVY8_2X8
;
903 static int ov5645_enum_frame_size(struct v4l2_subdev
*subdev
,
904 struct v4l2_subdev_pad_config
*cfg
,
905 struct v4l2_subdev_frame_size_enum
*fse
)
907 if (fse
->code
!= MEDIA_BUS_FMT_UYVY8_2X8
)
910 if (fse
->index
>= ARRAY_SIZE(ov5645_mode_info_data
))
913 fse
->min_width
= ov5645_mode_info_data
[fse
->index
].width
;
914 fse
->max_width
= ov5645_mode_info_data
[fse
->index
].width
;
915 fse
->min_height
= ov5645_mode_info_data
[fse
->index
].height
;
916 fse
->max_height
= ov5645_mode_info_data
[fse
->index
].height
;
921 static struct v4l2_mbus_framefmt
*
922 __ov5645_get_pad_format(struct ov5645
*ov5645
,
923 struct v4l2_subdev_pad_config
*cfg
,
925 enum v4l2_subdev_format_whence which
)
928 case V4L2_SUBDEV_FORMAT_TRY
:
929 return v4l2_subdev_get_try_format(&ov5645
->sd
, cfg
, pad
);
930 case V4L2_SUBDEV_FORMAT_ACTIVE
:
937 static int ov5645_get_format(struct v4l2_subdev
*sd
,
938 struct v4l2_subdev_pad_config
*cfg
,
939 struct v4l2_subdev_format
*format
)
941 struct ov5645
*ov5645
= to_ov5645(sd
);
943 format
->format
= *__ov5645_get_pad_format(ov5645
, cfg
, format
->pad
,
948 static struct v4l2_rect
*
949 __ov5645_get_pad_crop(struct ov5645
*ov5645
, struct v4l2_subdev_pad_config
*cfg
,
950 unsigned int pad
, enum v4l2_subdev_format_whence which
)
953 case V4L2_SUBDEV_FORMAT_TRY
:
954 return v4l2_subdev_get_try_crop(&ov5645
->sd
, cfg
, pad
);
955 case V4L2_SUBDEV_FORMAT_ACTIVE
:
956 return &ov5645
->crop
;
962 static const struct ov5645_mode_info
*
963 ov5645_find_nearest_mode(unsigned int width
, unsigned int height
)
967 for (i
= ARRAY_SIZE(ov5645_mode_info_data
) - 1; i
>= 0; i
--) {
968 if (ov5645_mode_info_data
[i
].width
<= width
&&
969 ov5645_mode_info_data
[i
].height
<= height
)
976 return &ov5645_mode_info_data
[i
];
979 static int ov5645_set_format(struct v4l2_subdev
*sd
,
980 struct v4l2_subdev_pad_config
*cfg
,
981 struct v4l2_subdev_format
*format
)
983 struct ov5645
*ov5645
= to_ov5645(sd
);
984 struct v4l2_mbus_framefmt
*__format
;
985 struct v4l2_rect
*__crop
;
986 const struct ov5645_mode_info
*new_mode
;
989 __crop
= __ov5645_get_pad_crop(ov5645
, cfg
, format
->pad
,
992 new_mode
= ov5645_find_nearest_mode(format
->format
.width
,
993 format
->format
.height
);
994 __crop
->width
= new_mode
->width
;
995 __crop
->height
= new_mode
->height
;
997 if (format
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
998 ret
= v4l2_ctrl_s_ctrl_int64(ov5645
->pixel_clock
,
999 new_mode
->pixel_clock
);
1003 ret
= v4l2_ctrl_s_ctrl(ov5645
->link_freq
,
1004 new_mode
->link_freq
);
1008 ov5645
->current_mode
= new_mode
;
1011 __format
= __ov5645_get_pad_format(ov5645
, cfg
, format
->pad
,
1013 __format
->width
= __crop
->width
;
1014 __format
->height
= __crop
->height
;
1015 __format
->code
= MEDIA_BUS_FMT_UYVY8_2X8
;
1016 __format
->field
= V4L2_FIELD_NONE
;
1017 __format
->colorspace
= V4L2_COLORSPACE_SRGB
;
1019 format
->format
= *__format
;
1024 static int ov5645_entity_init_cfg(struct v4l2_subdev
*subdev
,
1025 struct v4l2_subdev_pad_config
*cfg
)
1027 struct v4l2_subdev_format fmt
= { 0 };
1029 fmt
.which
= cfg
? V4L2_SUBDEV_FORMAT_TRY
: V4L2_SUBDEV_FORMAT_ACTIVE
;
1030 fmt
.format
.width
= 1920;
1031 fmt
.format
.height
= 1080;
1033 ov5645_set_format(subdev
, cfg
, &fmt
);
1038 static int ov5645_get_selection(struct v4l2_subdev
*sd
,
1039 struct v4l2_subdev_pad_config
*cfg
,
1040 struct v4l2_subdev_selection
*sel
)
1042 struct ov5645
*ov5645
= to_ov5645(sd
);
1044 if (sel
->target
!= V4L2_SEL_TGT_CROP
)
1047 sel
->r
= *__ov5645_get_pad_crop(ov5645
, cfg
, sel
->pad
,
1052 static int ov5645_s_stream(struct v4l2_subdev
*subdev
, int enable
)
1054 struct ov5645
*ov5645
= to_ov5645(subdev
);
1058 ret
= ov5645_set_register_array(ov5645
,
1059 ov5645
->current_mode
->data
,
1060 ov5645
->current_mode
->data_size
);
1062 dev_err(ov5645
->dev
, "could not set mode %dx%d\n",
1063 ov5645
->current_mode
->width
,
1064 ov5645
->current_mode
->height
);
1067 ret
= v4l2_ctrl_handler_setup(&ov5645
->ctrls
);
1069 dev_err(ov5645
->dev
, "could not sync v4l2 controls\n");
1072 ret
= ov5645_write_reg(ov5645
, OV5645_SYSTEM_CTRL0
,
1073 OV5645_SYSTEM_CTRL0_START
);
1077 ret
= ov5645_write_reg(ov5645
, OV5645_SYSTEM_CTRL0
,
1078 OV5645_SYSTEM_CTRL0_STOP
);
1086 static const struct v4l2_subdev_core_ops ov5645_core_ops
= {
1087 .s_power
= ov5645_s_power
,
1090 static const struct v4l2_subdev_video_ops ov5645_video_ops
= {
1091 .s_stream
= ov5645_s_stream
,
1094 static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops
= {
1095 .init_cfg
= ov5645_entity_init_cfg
,
1096 .enum_mbus_code
= ov5645_enum_mbus_code
,
1097 .enum_frame_size
= ov5645_enum_frame_size
,
1098 .get_fmt
= ov5645_get_format
,
1099 .set_fmt
= ov5645_set_format
,
1100 .get_selection
= ov5645_get_selection
,
1103 static const struct v4l2_subdev_ops ov5645_subdev_ops
= {
1104 .core
= &ov5645_core_ops
,
1105 .video
= &ov5645_video_ops
,
1106 .pad
= &ov5645_subdev_pad_ops
,
1109 static int ov5645_probe(struct i2c_client
*client
,
1110 const struct i2c_device_id
*id
)
1112 struct device
*dev
= &client
->dev
;
1113 struct device_node
*endpoint
;
1114 struct ov5645
*ov5645
;
1115 u8 chip_id_high
, chip_id_low
;
1119 ov5645
= devm_kzalloc(dev
, sizeof(struct ov5645
), GFP_KERNEL
);
1123 ov5645
->i2c_client
= client
;
1126 endpoint
= of_graph_get_next_endpoint(dev
->of_node
, NULL
);
1128 dev_err(dev
, "endpoint node not found\n");
1132 ret
= v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint
),
1135 dev_err(dev
, "parsing endpoint node failed\n");
1139 of_node_put(endpoint
);
1141 if (ov5645
->ep
.bus_type
!= V4L2_MBUS_CSI2
) {
1142 dev_err(dev
, "invalid bus type, must be CSI2\n");
1146 /* get system clock (xclk) */
1147 ov5645
->xclk
= devm_clk_get(dev
, "xclk");
1148 if (IS_ERR(ov5645
->xclk
)) {
1149 dev_err(dev
, "could not get xclk");
1150 return PTR_ERR(ov5645
->xclk
);
1153 ret
= of_property_read_u32(dev
->of_node
, "clock-frequency", &xclk_freq
);
1155 dev_err(dev
, "could not get xclk frequency\n");
1159 if (xclk_freq
!= 23880000) {
1160 dev_err(dev
, "external clock frequency %u is not supported\n",
1165 ret
= clk_set_rate(ov5645
->xclk
, xclk_freq
);
1167 dev_err(dev
, "could not set xclk frequency\n");
1171 ov5645
->io_regulator
= devm_regulator_get(dev
, "vdddo");
1172 if (IS_ERR(ov5645
->io_regulator
)) {
1173 dev_err(dev
, "cannot get io regulator\n");
1174 return PTR_ERR(ov5645
->io_regulator
);
1177 ret
= regulator_set_voltage(ov5645
->io_regulator
,
1178 OV5645_VOLTAGE_DIGITAL_IO
,
1179 OV5645_VOLTAGE_DIGITAL_IO
);
1181 dev_err(dev
, "cannot set io voltage\n");
1185 ov5645
->core_regulator
= devm_regulator_get(dev
, "vddd");
1186 if (IS_ERR(ov5645
->core_regulator
)) {
1187 dev_err(dev
, "cannot get core regulator\n");
1188 return PTR_ERR(ov5645
->core_regulator
);
1191 ret
= regulator_set_voltage(ov5645
->core_regulator
,
1192 OV5645_VOLTAGE_DIGITAL_CORE
,
1193 OV5645_VOLTAGE_DIGITAL_CORE
);
1195 dev_err(dev
, "cannot set core voltage\n");
1199 ov5645
->analog_regulator
= devm_regulator_get(dev
, "vdda");
1200 if (IS_ERR(ov5645
->analog_regulator
)) {
1201 dev_err(dev
, "cannot get analog regulator\n");
1202 return PTR_ERR(ov5645
->analog_regulator
);
1205 ret
= regulator_set_voltage(ov5645
->analog_regulator
,
1206 OV5645_VOLTAGE_ANALOG
,
1207 OV5645_VOLTAGE_ANALOG
);
1209 dev_err(dev
, "cannot set analog voltage\n");
1213 ov5645
->enable_gpio
= devm_gpiod_get(dev
, "enable", GPIOD_OUT_HIGH
);
1214 if (IS_ERR(ov5645
->enable_gpio
)) {
1215 dev_err(dev
, "cannot get enable gpio\n");
1216 return PTR_ERR(ov5645
->enable_gpio
);
1219 ov5645
->rst_gpio
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_HIGH
);
1220 if (IS_ERR(ov5645
->rst_gpio
)) {
1221 dev_err(dev
, "cannot get reset gpio\n");
1222 return PTR_ERR(ov5645
->rst_gpio
);
1225 mutex_init(&ov5645
->power_lock
);
1227 v4l2_ctrl_handler_init(&ov5645
->ctrls
, 9);
1228 v4l2_ctrl_new_std(&ov5645
->ctrls
, &ov5645_ctrl_ops
,
1229 V4L2_CID_SATURATION
, -4, 4, 1, 0);
1230 v4l2_ctrl_new_std(&ov5645
->ctrls
, &ov5645_ctrl_ops
,
1231 V4L2_CID_HFLIP
, 0, 1, 1, 0);
1232 v4l2_ctrl_new_std(&ov5645
->ctrls
, &ov5645_ctrl_ops
,
1233 V4L2_CID_VFLIP
, 0, 1, 1, 0);
1234 v4l2_ctrl_new_std(&ov5645
->ctrls
, &ov5645_ctrl_ops
,
1235 V4L2_CID_AUTOGAIN
, 0, 1, 1, 1);
1236 v4l2_ctrl_new_std(&ov5645
->ctrls
, &ov5645_ctrl_ops
,
1237 V4L2_CID_AUTO_WHITE_BALANCE
, 0, 1, 1, 1);
1238 v4l2_ctrl_new_std_menu(&ov5645
->ctrls
, &ov5645_ctrl_ops
,
1239 V4L2_CID_EXPOSURE_AUTO
, V4L2_EXPOSURE_MANUAL
,
1240 0, V4L2_EXPOSURE_AUTO
);
1241 v4l2_ctrl_new_std_menu_items(&ov5645
->ctrls
, &ov5645_ctrl_ops
,
1242 V4L2_CID_TEST_PATTERN
,
1243 ARRAY_SIZE(ov5645_test_pattern_menu
) - 1,
1244 0, 0, ov5645_test_pattern_menu
);
1245 ov5645
->pixel_clock
= v4l2_ctrl_new_std(&ov5645
->ctrls
,
1247 V4L2_CID_PIXEL_RATE
,
1249 ov5645
->link_freq
= v4l2_ctrl_new_int_menu(&ov5645
->ctrls
,
1252 ARRAY_SIZE(link_freq
) - 1,
1254 if (ov5645
->link_freq
)
1255 ov5645
->link_freq
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1257 ov5645
->sd
.ctrl_handler
= &ov5645
->ctrls
;
1259 if (ov5645
->ctrls
.error
) {
1260 dev_err(dev
, "%s: control initialization error %d\n",
1261 __func__
, ov5645
->ctrls
.error
);
1262 ret
= ov5645
->ctrls
.error
;
1266 v4l2_i2c_subdev_init(&ov5645
->sd
, client
, &ov5645_subdev_ops
);
1267 ov5645
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1268 ov5645
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1269 ov5645
->sd
.dev
= &client
->dev
;
1270 ov5645
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1272 ret
= media_entity_pads_init(&ov5645
->sd
.entity
, 1, &ov5645
->pad
);
1274 dev_err(dev
, "could not register media entity\n");
1278 ret
= ov5645_s_power(&ov5645
->sd
, true);
1280 dev_err(dev
, "could not power up OV5645\n");
1284 ret
= ov5645_read_reg(ov5645
, OV5645_CHIP_ID_HIGH
, &chip_id_high
);
1285 if (ret
< 0 || chip_id_high
!= OV5645_CHIP_ID_HIGH_BYTE
) {
1286 dev_err(dev
, "could not read ID high\n");
1290 ret
= ov5645_read_reg(ov5645
, OV5645_CHIP_ID_LOW
, &chip_id_low
);
1291 if (ret
< 0 || chip_id_low
!= OV5645_CHIP_ID_LOW_BYTE
) {
1292 dev_err(dev
, "could not read ID low\n");
1297 dev_info(dev
, "OV5645 detected at address 0x%02x\n", client
->addr
);
1299 ret
= ov5645_read_reg(ov5645
, OV5645_AEC_PK_MANUAL
,
1300 &ov5645
->aec_pk_manual
);
1302 dev_err(dev
, "could not read AEC/AGC mode\n");
1307 ret
= ov5645_read_reg(ov5645
, OV5645_TIMING_TC_REG20
,
1308 &ov5645
->timing_tc_reg20
);
1310 dev_err(dev
, "could not read vflip value\n");
1315 ret
= ov5645_read_reg(ov5645
, OV5645_TIMING_TC_REG21
,
1316 &ov5645
->timing_tc_reg21
);
1318 dev_err(dev
, "could not read hflip value\n");
1323 ov5645_s_power(&ov5645
->sd
, false);
1325 ret
= v4l2_async_register_subdev(&ov5645
->sd
);
1327 dev_err(dev
, "could not register v4l2 device\n");
1331 ov5645_entity_init_cfg(&ov5645
->sd
, NULL
);
1336 ov5645_s_power(&ov5645
->sd
, false);
1338 media_entity_cleanup(&ov5645
->sd
.entity
);
1340 v4l2_ctrl_handler_free(&ov5645
->ctrls
);
1341 mutex_destroy(&ov5645
->power_lock
);
1346 static int ov5645_remove(struct i2c_client
*client
)
1348 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1349 struct ov5645
*ov5645
= to_ov5645(sd
);
1351 v4l2_async_unregister_subdev(&ov5645
->sd
);
1352 media_entity_cleanup(&ov5645
->sd
.entity
);
1353 v4l2_ctrl_handler_free(&ov5645
->ctrls
);
1354 mutex_destroy(&ov5645
->power_lock
);
1359 static const struct i2c_device_id ov5645_id
[] = {
1363 MODULE_DEVICE_TABLE(i2c
, ov5645_id
);
1365 static const struct of_device_id ov5645_of_match
[] = {
1366 { .compatible
= "ovti,ov5645" },
1369 MODULE_DEVICE_TABLE(of
, ov5645_of_match
);
1371 static struct i2c_driver ov5645_i2c_driver
= {
1373 .of_match_table
= of_match_ptr(ov5645_of_match
),
1376 .probe
= ov5645_probe
,
1377 .remove
= ov5645_remove
,
1378 .id_table
= ov5645_id
,
1381 module_i2c_driver(ov5645_i2c_driver
);
1383 MODULE_DESCRIPTION("Omnivision OV5645 Camera Driver");
1384 MODULE_AUTHOR("Todor Tomov <todor.tomov@linaro.org>");
1385 MODULE_LICENSE("GPL v2");