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_IO_MIPI_CTRL00 0x300e
49 #define OV5645_PAD_OUTPUT00 0x3019
50 #define OV5645_AWB_MANUAL_CONTROL 0x3406
51 #define OV5645_AWB_MANUAL_ENABLE BIT(0)
52 #define OV5645_AEC_PK_MANUAL 0x3503
53 #define OV5645_AEC_MANUAL_ENABLE BIT(0)
54 #define OV5645_AGC_MANUAL_ENABLE BIT(1)
55 #define OV5645_TIMING_TC_REG20 0x3820
56 #define OV5645_SENSOR_VFLIP BIT(1)
57 #define OV5645_ISP_VFLIP BIT(2)
58 #define OV5645_TIMING_TC_REG21 0x3821
59 #define OV5645_SENSOR_MIRROR BIT(1)
60 #define OV5645_MIPI_CTRL00 0x4800
61 #define OV5645_PRE_ISP_TEST_SETTING_1 0x503d
62 #define OV5645_TEST_PATTERN_MASK 0x3
63 #define OV5645_SET_TEST_PATTERN(x) ((x) & OV5645_TEST_PATTERN_MASK)
64 #define OV5645_TEST_PATTERN_ENABLE BIT(7)
65 #define OV5645_SDE_SAT_U 0x5583
66 #define OV5645_SDE_SAT_V 0x5584
73 struct ov5645_mode_info {
76 const struct reg_value *data;
83 struct i2c_client *i2c_client;
85 struct v4l2_subdev sd;
87 struct v4l2_fwnode_endpoint ep;
88 struct v4l2_mbus_framefmt fmt;
89 struct v4l2_rect crop;
92 struct regulator *io_regulator;
93 struct regulator *core_regulator;
94 struct regulator *analog_regulator;
96 const struct ov5645_mode_info *current_mode;
98 struct v4l2_ctrl_handler ctrls;
99 struct v4l2_ctrl *pixel_clock;
100 struct v4l2_ctrl *link_freq;
102 /* Cached register values */
107 struct mutex power_lock; /* lock to protect power state */
110 struct gpio_desc *enable_gpio;
111 struct gpio_desc *rst_gpio;
114 static inline struct ov5645 *to_ov5645(struct v4l2_subdev *sd)
116 return container_of(sd, struct ov5645, sd);
119 static const struct reg_value ov5645_global_init_setting[] = {
356 { OV5645_IO_MIPI_CTRL00, 0x40 },
357 { OV5645_MIPI_CTRL00, 0x24 },
358 { OV5645_PAD_OUTPUT00, 0x70 }
361 static const struct reg_value ov5645_setting_sxga[] = {
409 static const struct reg_value ov5645_setting_1080p[] = {
459 static const struct reg_value ov5645_setting_full[] = {
509 static const s64 link_freq[] = {
514 static const struct ov5645_mode_info ov5645_mode_info_data[] = {
518 .data = ov5645_setting_sxga,
519 .data_size = ARRAY_SIZE(ov5645_setting_sxga),
520 .pixel_clock = 112000000,
521 .link_freq = 0 /* an index in link_freq[] */
526 .data = ov5645_setting_1080p,
527 .data_size = ARRAY_SIZE(ov5645_setting_1080p),
528 .pixel_clock = 168000000,
529 .link_freq = 1 /* an index in link_freq[] */
534 .data = ov5645_setting_full,
535 .data_size = ARRAY_SIZE(ov5645_setting_full),
536 .pixel_clock = 168000000,
537 .link_freq = 1 /* an index in link_freq[] */
541 static int ov5645_regulators_enable(struct ov5645 *ov5645)
545 ret = regulator_enable(ov5645->io_regulator);
547 dev_err(ov5645->dev, "set io voltage failed\n");
551 ret = regulator_enable(ov5645->analog_regulator);
553 dev_err(ov5645->dev, "set analog voltage failed\n");
557 ret = regulator_enable(ov5645->core_regulator);
559 dev_err(ov5645->dev, "set core voltage failed\n");
560 goto err_disable_analog;
566 regulator_disable(ov5645->analog_regulator);
568 regulator_disable(ov5645->io_regulator);
573 static void ov5645_regulators_disable(struct ov5645 *ov5645)
577 ret = regulator_disable(ov5645->core_regulator);
579 dev_err(ov5645->dev, "core regulator disable failed\n");
581 ret = regulator_disable(ov5645->analog_regulator);
583 dev_err(ov5645->dev, "analog regulator disable failed\n");
585 ret = regulator_disable(ov5645->io_regulator);
587 dev_err(ov5645->dev, "io regulator disable failed\n");
590 static int ov5645_write_reg(struct ov5645 *ov5645, u16 reg, u8 val)
595 regbuf[0] = reg >> 8;
596 regbuf[1] = reg & 0xff;
599 ret = i2c_master_send(ov5645->i2c_client, regbuf, 3);
601 dev_err(ov5645->dev, "%s: write reg error %d: reg=%x, val=%x\n",
602 __func__, ret, reg, val);
609 static int ov5645_read_reg(struct ov5645 *ov5645, u16 reg, u8 *val)
614 regbuf[0] = reg >> 8;
615 regbuf[1] = reg & 0xff;
617 ret = i2c_master_send(ov5645->i2c_client, regbuf, 2);
619 dev_err(ov5645->dev, "%s: write reg error %d: reg=%x\n",
624 ret = i2c_master_recv(ov5645->i2c_client, val, 1);
626 dev_err(ov5645->dev, "%s: read reg error %d: reg=%x\n",
634 static int ov5645_set_aec_mode(struct ov5645 *ov5645, u32 mode)
636 u8 val = ov5645->aec_pk_manual;
639 if (mode == V4L2_EXPOSURE_AUTO)
640 val &= ~OV5645_AEC_MANUAL_ENABLE;
641 else /* V4L2_EXPOSURE_MANUAL */
642 val |= OV5645_AEC_MANUAL_ENABLE;
644 ret = ov5645_write_reg(ov5645, OV5645_AEC_PK_MANUAL, val);
646 ov5645->aec_pk_manual = val;
651 static int ov5645_set_agc_mode(struct ov5645 *ov5645, u32 enable)
653 u8 val = ov5645->aec_pk_manual;
657 val &= ~OV5645_AGC_MANUAL_ENABLE;
659 val |= OV5645_AGC_MANUAL_ENABLE;
661 ret = ov5645_write_reg(ov5645, OV5645_AEC_PK_MANUAL, val);
663 ov5645->aec_pk_manual = val;
668 static int ov5645_set_register_array(struct ov5645 *ov5645,
669 const struct reg_value *settings,
670 unsigned int num_settings)
675 for (i = 0; i < num_settings; ++i, ++settings) {
676 ret = ov5645_write_reg(ov5645, settings->reg, settings->val);
684 static int ov5645_set_power_on(struct ov5645 *ov5645)
688 ret = ov5645_regulators_enable(ov5645);
693 ret = clk_prepare_enable(ov5645->xclk);
695 dev_err(ov5645->dev, "clk prepare enable failed\n");
696 ov5645_regulators_disable(ov5645);
700 usleep_range(5000, 15000);
701 gpiod_set_value_cansleep(ov5645->enable_gpio, 1);
703 usleep_range(1000, 2000);
704 gpiod_set_value_cansleep(ov5645->rst_gpio, 0);
711 static void ov5645_set_power_off(struct ov5645 *ov5645)
713 gpiod_set_value_cansleep(ov5645->rst_gpio, 1);
714 gpiod_set_value_cansleep(ov5645->enable_gpio, 0);
715 clk_disable_unprepare(ov5645->xclk);
716 ov5645_regulators_disable(ov5645);
719 static int ov5645_s_power(struct v4l2_subdev *sd, int on)
721 struct ov5645 *ov5645 = to_ov5645(sd);
724 mutex_lock(&ov5645->power_lock);
726 /* If the power count is modified from 0 to != 0 or from != 0 to 0,
727 * update the power state.
729 if (ov5645->power_count == !on) {
731 ret = ov5645_set_power_on(ov5645);
735 ret = ov5645_set_register_array(ov5645,
736 ov5645_global_init_setting,
737 ARRAY_SIZE(ov5645_global_init_setting));
740 "could not set init registers\n");
741 ov5645_set_power_off(ov5645);
745 usleep_range(500, 1000);
747 ov5645_write_reg(ov5645, OV5645_IO_MIPI_CTRL00, 0x58);
748 ov5645_set_power_off(ov5645);
752 /* Update the power count. */
753 ov5645->power_count += on ? 1 : -1;
754 WARN_ON(ov5645->power_count < 0);
757 mutex_unlock(&ov5645->power_lock);
762 static int ov5645_set_saturation(struct ov5645 *ov5645, s32 value)
764 u32 reg_value = (value * 0x10) + 0x40;
767 ret = ov5645_write_reg(ov5645, OV5645_SDE_SAT_U, reg_value);
771 return ov5645_write_reg(ov5645, OV5645_SDE_SAT_V, reg_value);
774 static int ov5645_set_hflip(struct ov5645 *ov5645, s32 value)
776 u8 val = ov5645->timing_tc_reg21;
780 val &= ~(OV5645_SENSOR_MIRROR);
782 val |= (OV5645_SENSOR_MIRROR);
784 ret = ov5645_write_reg(ov5645, OV5645_TIMING_TC_REG21, val);
786 ov5645->timing_tc_reg21 = val;
791 static int ov5645_set_vflip(struct ov5645 *ov5645, s32 value)
793 u8 val = ov5645->timing_tc_reg20;
797 val |= (OV5645_SENSOR_VFLIP | OV5645_ISP_VFLIP);
799 val &= ~(OV5645_SENSOR_VFLIP | OV5645_ISP_VFLIP);
801 ret = ov5645_write_reg(ov5645, OV5645_TIMING_TC_REG20, val);
803 ov5645->timing_tc_reg20 = val;
808 static int ov5645_set_test_pattern(struct ov5645 *ov5645, s32 value)
813 val = OV5645_SET_TEST_PATTERN(value - 1);
814 val |= OV5645_TEST_PATTERN_ENABLE;
817 return ov5645_write_reg(ov5645, OV5645_PRE_ISP_TEST_SETTING_1, val);
820 static const char * const ov5645_test_pattern_menu[] = {
822 "Vertical Color Bars",
823 "Pseudo-Random Data",
828 static int ov5645_set_awb(struct ov5645 *ov5645, s32 enable_auto)
833 val = OV5645_AWB_MANUAL_ENABLE;
835 return ov5645_write_reg(ov5645, OV5645_AWB_MANUAL_CONTROL, val);
838 static int ov5645_s_ctrl(struct v4l2_ctrl *ctrl)
840 struct ov5645 *ov5645 = container_of(ctrl->handler,
841 struct ov5645, ctrls);
844 mutex_lock(&ov5645->power_lock);
845 if (!ov5645->power_count) {
846 mutex_unlock(&ov5645->power_lock);
851 case V4L2_CID_SATURATION:
852 ret = ov5645_set_saturation(ov5645, ctrl->val);
854 case V4L2_CID_AUTO_WHITE_BALANCE:
855 ret = ov5645_set_awb(ov5645, ctrl->val);
857 case V4L2_CID_AUTOGAIN:
858 ret = ov5645_set_agc_mode(ov5645, ctrl->val);
860 case V4L2_CID_EXPOSURE_AUTO:
861 ret = ov5645_set_aec_mode(ov5645, ctrl->val);
863 case V4L2_CID_TEST_PATTERN:
864 ret = ov5645_set_test_pattern(ov5645, ctrl->val);
867 ret = ov5645_set_hflip(ov5645, ctrl->val);
870 ret = ov5645_set_vflip(ov5645, ctrl->val);
877 mutex_unlock(&ov5645->power_lock);
882 static const struct v4l2_ctrl_ops ov5645_ctrl_ops = {
883 .s_ctrl = ov5645_s_ctrl,
886 static int ov5645_enum_mbus_code(struct v4l2_subdev *sd,
887 struct v4l2_subdev_pad_config *cfg,
888 struct v4l2_subdev_mbus_code_enum *code)
893 code->code = MEDIA_BUS_FMT_UYVY8_2X8;
898 static int ov5645_enum_frame_size(struct v4l2_subdev *subdev,
899 struct v4l2_subdev_pad_config *cfg,
900 struct v4l2_subdev_frame_size_enum *fse)
902 if (fse->code != MEDIA_BUS_FMT_UYVY8_2X8)
905 if (fse->index >= ARRAY_SIZE(ov5645_mode_info_data))
908 fse->min_width = ov5645_mode_info_data[fse->index].width;
909 fse->max_width = ov5645_mode_info_data[fse->index].width;
910 fse->min_height = ov5645_mode_info_data[fse->index].height;
911 fse->max_height = ov5645_mode_info_data[fse->index].height;
916 static struct v4l2_mbus_framefmt *
917 __ov5645_get_pad_format(struct ov5645 *ov5645,
918 struct v4l2_subdev_pad_config *cfg,
920 enum v4l2_subdev_format_whence which)
923 case V4L2_SUBDEV_FORMAT_TRY:
924 return v4l2_subdev_get_try_format(&ov5645->sd, cfg, pad);
925 case V4L2_SUBDEV_FORMAT_ACTIVE:
932 static int ov5645_get_format(struct v4l2_subdev *sd,
933 struct v4l2_subdev_pad_config *cfg,
934 struct v4l2_subdev_format *format)
936 struct ov5645 *ov5645 = to_ov5645(sd);
938 format->format = *__ov5645_get_pad_format(ov5645, cfg, format->pad,
943 static struct v4l2_rect *
944 __ov5645_get_pad_crop(struct ov5645 *ov5645, struct v4l2_subdev_pad_config *cfg,
945 unsigned int pad, enum v4l2_subdev_format_whence which)
948 case V4L2_SUBDEV_FORMAT_TRY:
949 return v4l2_subdev_get_try_crop(&ov5645->sd, cfg, pad);
950 case V4L2_SUBDEV_FORMAT_ACTIVE:
951 return &ov5645->crop;
957 static int ov5645_set_format(struct v4l2_subdev *sd,
958 struct v4l2_subdev_pad_config *cfg,
959 struct v4l2_subdev_format *format)
961 struct ov5645 *ov5645 = to_ov5645(sd);
962 struct v4l2_mbus_framefmt *__format;
963 struct v4l2_rect *__crop;
964 const struct ov5645_mode_info *new_mode;
967 __crop = __ov5645_get_pad_crop(ov5645, cfg, format->pad,
970 new_mode = v4l2_find_nearest_size(ov5645_mode_info_data,
971 ARRAY_SIZE(ov5645_mode_info_data),
973 format->format.width, format->format.height);
975 __crop->width = new_mode->width;
976 __crop->height = new_mode->height;
978 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
979 ret = v4l2_ctrl_s_ctrl_int64(ov5645->pixel_clock,
980 new_mode->pixel_clock);
984 ret = v4l2_ctrl_s_ctrl(ov5645->link_freq,
985 new_mode->link_freq);
989 ov5645->current_mode = new_mode;
992 __format = __ov5645_get_pad_format(ov5645, cfg, format->pad,
994 __format->width = __crop->width;
995 __format->height = __crop->height;
996 __format->code = MEDIA_BUS_FMT_UYVY8_2X8;
997 __format->field = V4L2_FIELD_NONE;
998 __format->colorspace = V4L2_COLORSPACE_SRGB;
1000 format->format = *__format;
1005 static int ov5645_entity_init_cfg(struct v4l2_subdev *subdev,
1006 struct v4l2_subdev_pad_config *cfg)
1008 struct v4l2_subdev_format fmt = { 0 };
1010 fmt.which = cfg ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
1011 fmt.format.width = 1920;
1012 fmt.format.height = 1080;
1014 ov5645_set_format(subdev, cfg, &fmt);
1019 static int ov5645_get_selection(struct v4l2_subdev *sd,
1020 struct v4l2_subdev_pad_config *cfg,
1021 struct v4l2_subdev_selection *sel)
1023 struct ov5645 *ov5645 = to_ov5645(sd);
1025 if (sel->target != V4L2_SEL_TGT_CROP)
1028 sel->r = *__ov5645_get_pad_crop(ov5645, cfg, sel->pad,
1033 static int ov5645_s_stream(struct v4l2_subdev *subdev, int enable)
1035 struct ov5645 *ov5645 = to_ov5645(subdev);
1039 ret = ov5645_set_register_array(ov5645,
1040 ov5645->current_mode->data,
1041 ov5645->current_mode->data_size);
1043 dev_err(ov5645->dev, "could not set mode %dx%d\n",
1044 ov5645->current_mode->width,
1045 ov5645->current_mode->height);
1048 ret = v4l2_ctrl_handler_setup(&ov5645->ctrls);
1050 dev_err(ov5645->dev, "could not sync v4l2 controls\n");
1054 ret = ov5645_write_reg(ov5645, OV5645_IO_MIPI_CTRL00, 0x45);
1058 ret = ov5645_write_reg(ov5645, OV5645_SYSTEM_CTRL0,
1059 OV5645_SYSTEM_CTRL0_START);
1063 ret = ov5645_write_reg(ov5645, OV5645_IO_MIPI_CTRL00, 0x40);
1067 ret = ov5645_write_reg(ov5645, OV5645_SYSTEM_CTRL0,
1068 OV5645_SYSTEM_CTRL0_STOP);
1076 static const struct v4l2_subdev_core_ops ov5645_core_ops = {
1077 .s_power = ov5645_s_power,
1080 static const struct v4l2_subdev_video_ops ov5645_video_ops = {
1081 .s_stream = ov5645_s_stream,
1084 static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
1085 .init_cfg = ov5645_entity_init_cfg,
1086 .enum_mbus_code = ov5645_enum_mbus_code,
1087 .enum_frame_size = ov5645_enum_frame_size,
1088 .get_fmt = ov5645_get_format,
1089 .set_fmt = ov5645_set_format,
1090 .get_selection = ov5645_get_selection,
1093 static const struct v4l2_subdev_ops ov5645_subdev_ops = {
1094 .core = &ov5645_core_ops,
1095 .video = &ov5645_video_ops,
1096 .pad = &ov5645_subdev_pad_ops,
1099 static int ov5645_probe(struct i2c_client *client,
1100 const struct i2c_device_id *id)
1102 struct device *dev = &client->dev;
1103 struct device_node *endpoint;
1104 struct ov5645 *ov5645;
1105 u8 chip_id_high, chip_id_low;
1109 ov5645 = devm_kzalloc(dev, sizeof(struct ov5645), GFP_KERNEL);
1113 ov5645->i2c_client = client;
1116 endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
1118 dev_err(dev, "endpoint node not found\n");
1122 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint),
1125 of_node_put(endpoint);
1128 dev_err(dev, "parsing endpoint node failed\n");
1132 if (ov5645->ep.bus_type != V4L2_MBUS_CSI2_DPHY) {
1133 dev_err(dev, "invalid bus type, must be CSI2\n");
1137 /* get system clock (xclk) */
1138 ov5645->xclk = devm_clk_get(dev, "xclk");
1139 if (IS_ERR(ov5645->xclk)) {
1140 dev_err(dev, "could not get xclk");
1141 return PTR_ERR(ov5645->xclk);
1144 ret = of_property_read_u32(dev->of_node, "clock-frequency", &xclk_freq);
1146 dev_err(dev, "could not get xclk frequency\n");
1150 /* external clock must be 24MHz, allow 1% tolerance */
1151 if (xclk_freq < 23760000 || xclk_freq > 24240000) {
1152 dev_err(dev, "external clock frequency %u is not supported\n",
1157 ret = clk_set_rate(ov5645->xclk, xclk_freq);
1159 dev_err(dev, "could not set xclk frequency\n");
1163 ov5645->io_regulator = devm_regulator_get(dev, "vdddo");
1164 if (IS_ERR(ov5645->io_regulator)) {
1165 dev_err(dev, "cannot get io regulator\n");
1166 return PTR_ERR(ov5645->io_regulator);
1169 ret = regulator_set_voltage(ov5645->io_regulator,
1170 OV5645_VOLTAGE_DIGITAL_IO,
1171 OV5645_VOLTAGE_DIGITAL_IO);
1173 dev_err(dev, "cannot set io voltage\n");
1177 ov5645->core_regulator = devm_regulator_get(dev, "vddd");
1178 if (IS_ERR(ov5645->core_regulator)) {
1179 dev_err(dev, "cannot get core regulator\n");
1180 return PTR_ERR(ov5645->core_regulator);
1183 ret = regulator_set_voltage(ov5645->core_regulator,
1184 OV5645_VOLTAGE_DIGITAL_CORE,
1185 OV5645_VOLTAGE_DIGITAL_CORE);
1187 dev_err(dev, "cannot set core voltage\n");
1191 ov5645->analog_regulator = devm_regulator_get(dev, "vdda");
1192 if (IS_ERR(ov5645->analog_regulator)) {
1193 dev_err(dev, "cannot get analog regulator\n");
1194 return PTR_ERR(ov5645->analog_regulator);
1197 ret = regulator_set_voltage(ov5645->analog_regulator,
1198 OV5645_VOLTAGE_ANALOG,
1199 OV5645_VOLTAGE_ANALOG);
1201 dev_err(dev, "cannot set analog voltage\n");
1205 ov5645->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_HIGH);
1206 if (IS_ERR(ov5645->enable_gpio)) {
1207 dev_err(dev, "cannot get enable gpio\n");
1208 return PTR_ERR(ov5645->enable_gpio);
1211 ov5645->rst_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
1212 if (IS_ERR(ov5645->rst_gpio)) {
1213 dev_err(dev, "cannot get reset gpio\n");
1214 return PTR_ERR(ov5645->rst_gpio);
1217 mutex_init(&ov5645->power_lock);
1219 v4l2_ctrl_handler_init(&ov5645->ctrls, 9);
1220 v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
1221 V4L2_CID_SATURATION, -4, 4, 1, 0);
1222 v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
1223 V4L2_CID_HFLIP, 0, 1, 1, 0);
1224 v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
1225 V4L2_CID_VFLIP, 0, 1, 1, 0);
1226 v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
1227 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1228 v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
1229 V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
1230 v4l2_ctrl_new_std_menu(&ov5645->ctrls, &ov5645_ctrl_ops,
1231 V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL,
1232 0, V4L2_EXPOSURE_AUTO);
1233 v4l2_ctrl_new_std_menu_items(&ov5645->ctrls, &ov5645_ctrl_ops,
1234 V4L2_CID_TEST_PATTERN,
1235 ARRAY_SIZE(ov5645_test_pattern_menu) - 1,
1236 0, 0, ov5645_test_pattern_menu);
1237 ov5645->pixel_clock = v4l2_ctrl_new_std(&ov5645->ctrls,
1239 V4L2_CID_PIXEL_RATE,
1241 ov5645->link_freq = v4l2_ctrl_new_int_menu(&ov5645->ctrls,
1244 ARRAY_SIZE(link_freq) - 1,
1246 if (ov5645->link_freq)
1247 ov5645->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1249 ov5645->sd.ctrl_handler = &ov5645->ctrls;
1251 if (ov5645->ctrls.error) {
1252 dev_err(dev, "%s: control initialization error %d\n",
1253 __func__, ov5645->ctrls.error);
1254 ret = ov5645->ctrls.error;
1258 v4l2_i2c_subdev_init(&ov5645->sd, client, &ov5645_subdev_ops);
1259 ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1260 ov5645->pad.flags = MEDIA_PAD_FL_SOURCE;
1261 ov5645->sd.dev = &client->dev;
1262 ov5645->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1264 ret = media_entity_pads_init(&ov5645->sd.entity, 1, &ov5645->pad);
1266 dev_err(dev, "could not register media entity\n");
1270 ret = ov5645_s_power(&ov5645->sd, true);
1272 dev_err(dev, "could not power up OV5645\n");
1276 ret = ov5645_read_reg(ov5645, OV5645_CHIP_ID_HIGH, &chip_id_high);
1277 if (ret < 0 || chip_id_high != OV5645_CHIP_ID_HIGH_BYTE) {
1278 dev_err(dev, "could not read ID high\n");
1282 ret = ov5645_read_reg(ov5645, OV5645_CHIP_ID_LOW, &chip_id_low);
1283 if (ret < 0 || chip_id_low != OV5645_CHIP_ID_LOW_BYTE) {
1284 dev_err(dev, "could not read ID low\n");
1289 dev_info(dev, "OV5645 detected at address 0x%02x\n", client->addr);
1291 ret = ov5645_read_reg(ov5645, OV5645_AEC_PK_MANUAL,
1292 &ov5645->aec_pk_manual);
1294 dev_err(dev, "could not read AEC/AGC mode\n");
1299 ret = ov5645_read_reg(ov5645, OV5645_TIMING_TC_REG20,
1300 &ov5645->timing_tc_reg20);
1302 dev_err(dev, "could not read vflip value\n");
1307 ret = ov5645_read_reg(ov5645, OV5645_TIMING_TC_REG21,
1308 &ov5645->timing_tc_reg21);
1310 dev_err(dev, "could not read hflip value\n");
1315 ov5645_s_power(&ov5645->sd, false);
1317 ret = v4l2_async_register_subdev(&ov5645->sd);
1319 dev_err(dev, "could not register v4l2 device\n");
1323 ov5645_entity_init_cfg(&ov5645->sd, NULL);
1328 ov5645_s_power(&ov5645->sd, false);
1330 media_entity_cleanup(&ov5645->sd.entity);
1332 v4l2_ctrl_handler_free(&ov5645->ctrls);
1333 mutex_destroy(&ov5645->power_lock);
1338 static int ov5645_remove(struct i2c_client *client)
1340 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1341 struct ov5645 *ov5645 = to_ov5645(sd);
1343 v4l2_async_unregister_subdev(&ov5645->sd);
1344 media_entity_cleanup(&ov5645->sd.entity);
1345 v4l2_ctrl_handler_free(&ov5645->ctrls);
1346 mutex_destroy(&ov5645->power_lock);
1351 static const struct i2c_device_id ov5645_id[] = {
1355 MODULE_DEVICE_TABLE(i2c, ov5645_id);
1357 static const struct of_device_id ov5645_of_match[] = {
1358 { .compatible = "ovti,ov5645" },
1361 MODULE_DEVICE_TABLE(of, ov5645_of_match);
1363 static struct i2c_driver ov5645_i2c_driver = {
1365 .of_match_table = of_match_ptr(ov5645_of_match),
1368 .probe = ov5645_probe,
1369 .remove = ov5645_remove,
1370 .id_table = ov5645_id,
1373 module_i2c_driver(ov5645_i2c_driver);
1375 MODULE_DESCRIPTION("Omnivision OV5645 Camera Driver");
1376 MODULE_AUTHOR("Todor Tomov <todor.tomov@linaro.org>");
1377 MODULE_LICENSE("GPL v2");