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_IO_MIPI_CTRL00 0x300e
57 #define OV5645_PAD_OUTPUT00 0x3019
58 #define OV5645_AWB_MANUAL_CONTROL 0x3406
59 #define OV5645_AWB_MANUAL_ENABLE BIT(0)
60 #define OV5645_AEC_PK_MANUAL 0x3503
61 #define OV5645_AEC_MANUAL_ENABLE BIT(0)
62 #define OV5645_AGC_MANUAL_ENABLE BIT(1)
63 #define OV5645_TIMING_TC_REG20 0x3820
64 #define OV5645_SENSOR_VFLIP BIT(1)
65 #define OV5645_ISP_VFLIP BIT(2)
66 #define OV5645_TIMING_TC_REG21 0x3821
67 #define OV5645_SENSOR_MIRROR BIT(1)
68 #define OV5645_MIPI_CTRL00 0x4800
69 #define OV5645_PRE_ISP_TEST_SETTING_1 0x503d
70 #define OV5645_TEST_PATTERN_MASK 0x3
71 #define OV5645_SET_TEST_PATTERN(x) ((x) & OV5645_TEST_PATTERN_MASK)
72 #define OV5645_TEST_PATTERN_ENABLE BIT(7)
73 #define OV5645_SDE_SAT_U 0x5583
74 #define OV5645_SDE_SAT_V 0x5584
81 struct ov5645_mode_info {
84 const struct reg_value *data;
91 struct i2c_client *i2c_client;
93 struct v4l2_subdev sd;
95 struct v4l2_fwnode_endpoint ep;
96 struct v4l2_mbus_framefmt fmt;
97 struct v4l2_rect crop;
100 struct regulator *io_regulator;
101 struct regulator *core_regulator;
102 struct regulator *analog_regulator;
104 const struct ov5645_mode_info *current_mode;
106 struct v4l2_ctrl_handler ctrls;
107 struct v4l2_ctrl *pixel_clock;
108 struct v4l2_ctrl *link_freq;
110 /* Cached register values */
115 struct mutex power_lock; /* lock to protect power state */
118 struct gpio_desc *enable_gpio;
119 struct gpio_desc *rst_gpio;
122 static inline struct ov5645 *to_ov5645(struct v4l2_subdev *sd)
124 return container_of(sd, struct ov5645, sd);
127 static const struct reg_value ov5645_global_init_setting[] = {
364 { OV5645_IO_MIPI_CTRL00, 0x40 },
365 { OV5645_MIPI_CTRL00, 0x24 },
366 { OV5645_PAD_OUTPUT00, 0x70 }
369 static const struct reg_value ov5645_setting_sxga[] = {
417 static const struct reg_value ov5645_setting_1080p[] = {
467 static const struct reg_value ov5645_setting_full[] = {
517 static const s64 link_freq[] = {
522 static const struct ov5645_mode_info ov5645_mode_info_data[] = {
526 .data = ov5645_setting_sxga,
527 .data_size = ARRAY_SIZE(ov5645_setting_sxga),
528 .pixel_clock = 112000000,
529 .link_freq = 0 /* an index in link_freq[] */
534 .data = ov5645_setting_1080p,
535 .data_size = ARRAY_SIZE(ov5645_setting_1080p),
536 .pixel_clock = 168000000,
537 .link_freq = 1 /* an index in link_freq[] */
542 .data = ov5645_setting_full,
543 .data_size = ARRAY_SIZE(ov5645_setting_full),
544 .pixel_clock = 168000000,
545 .link_freq = 1 /* an index in link_freq[] */
549 static int ov5645_regulators_enable(struct ov5645 *ov5645)
553 ret = regulator_enable(ov5645->io_regulator);
555 dev_err(ov5645->dev, "set io voltage failed\n");
559 ret = regulator_enable(ov5645->analog_regulator);
561 dev_err(ov5645->dev, "set analog voltage failed\n");
565 ret = regulator_enable(ov5645->core_regulator);
567 dev_err(ov5645->dev, "set core voltage failed\n");
568 goto err_disable_analog;
574 regulator_disable(ov5645->analog_regulator);
576 regulator_disable(ov5645->io_regulator);
581 static void ov5645_regulators_disable(struct ov5645 *ov5645)
585 ret = regulator_disable(ov5645->core_regulator);
587 dev_err(ov5645->dev, "core regulator disable failed\n");
589 ret = regulator_disable(ov5645->analog_regulator);
591 dev_err(ov5645->dev, "analog regulator disable failed\n");
593 ret = regulator_disable(ov5645->io_regulator);
595 dev_err(ov5645->dev, "io regulator disable failed\n");
598 static int ov5645_write_reg(struct ov5645 *ov5645, u16 reg, u8 val)
603 regbuf[0] = reg >> 8;
604 regbuf[1] = reg & 0xff;
607 ret = i2c_master_send(ov5645->i2c_client, regbuf, 3);
609 dev_err(ov5645->dev, "%s: write reg error %d: reg=%x, val=%x\n",
610 __func__, ret, reg, val);
615 static int ov5645_read_reg(struct ov5645 *ov5645, u16 reg, u8 *val)
620 regbuf[0] = reg >> 8;
621 regbuf[1] = reg & 0xff;
623 ret = i2c_master_send(ov5645->i2c_client, regbuf, 2);
625 dev_err(ov5645->dev, "%s: write reg error %d: reg=%x\n",
630 ret = i2c_master_recv(ov5645->i2c_client, val, 1);
632 dev_err(ov5645->dev, "%s: read reg error %d: reg=%x\n",
640 static int ov5645_set_aec_mode(struct ov5645 *ov5645, u32 mode)
642 u8 val = ov5645->aec_pk_manual;
645 if (mode == V4L2_EXPOSURE_AUTO)
646 val &= ~OV5645_AEC_MANUAL_ENABLE;
647 else /* V4L2_EXPOSURE_MANUAL */
648 val |= OV5645_AEC_MANUAL_ENABLE;
650 ret = ov5645_write_reg(ov5645, OV5645_AEC_PK_MANUAL, val);
652 ov5645->aec_pk_manual = val;
657 static int ov5645_set_agc_mode(struct ov5645 *ov5645, u32 enable)
659 u8 val = ov5645->aec_pk_manual;
663 val &= ~OV5645_AGC_MANUAL_ENABLE;
665 val |= OV5645_AGC_MANUAL_ENABLE;
667 ret = ov5645_write_reg(ov5645, OV5645_AEC_PK_MANUAL, val);
669 ov5645->aec_pk_manual = val;
674 static int ov5645_set_register_array(struct ov5645 *ov5645,
675 const struct reg_value *settings,
676 unsigned int num_settings)
681 for (i = 0; i < num_settings; ++i, ++settings) {
682 ret = ov5645_write_reg(ov5645, settings->reg, settings->val);
690 static int ov5645_set_power_on(struct ov5645 *ov5645)
694 ret = ov5645_regulators_enable(ov5645);
699 ret = clk_prepare_enable(ov5645->xclk);
701 dev_err(ov5645->dev, "clk prepare enable failed\n");
702 ov5645_regulators_disable(ov5645);
706 usleep_range(5000, 15000);
707 gpiod_set_value_cansleep(ov5645->enable_gpio, 1);
709 usleep_range(1000, 2000);
710 gpiod_set_value_cansleep(ov5645->rst_gpio, 0);
717 static void ov5645_set_power_off(struct ov5645 *ov5645)
719 gpiod_set_value_cansleep(ov5645->rst_gpio, 1);
720 gpiod_set_value_cansleep(ov5645->enable_gpio, 0);
721 clk_disable_unprepare(ov5645->xclk);
722 ov5645_regulators_disable(ov5645);
725 static int ov5645_s_power(struct v4l2_subdev *sd, int on)
727 struct ov5645 *ov5645 = to_ov5645(sd);
730 mutex_lock(&ov5645->power_lock);
732 /* If the power count is modified from 0 to != 0 or from != 0 to 0,
733 * update the power state.
735 if (ov5645->power_count == !on) {
737 ret = ov5645_set_power_on(ov5645);
741 ret = ov5645_set_register_array(ov5645,
742 ov5645_global_init_setting,
743 ARRAY_SIZE(ov5645_global_init_setting));
746 "could not set init registers\n");
747 ov5645_set_power_off(ov5645);
751 usleep_range(500, 1000);
753 ov5645_write_reg(ov5645, OV5645_IO_MIPI_CTRL00, 0x58);
754 ov5645_set_power_off(ov5645);
758 /* Update the power count. */
759 ov5645->power_count += on ? 1 : -1;
760 WARN_ON(ov5645->power_count < 0);
763 mutex_unlock(&ov5645->power_lock);
768 static int ov5645_set_saturation(struct ov5645 *ov5645, s32 value)
770 u32 reg_value = (value * 0x10) + 0x40;
773 ret = ov5645_write_reg(ov5645, OV5645_SDE_SAT_U, reg_value);
777 return ov5645_write_reg(ov5645, OV5645_SDE_SAT_V, reg_value);
780 static int ov5645_set_hflip(struct ov5645 *ov5645, s32 value)
782 u8 val = ov5645->timing_tc_reg21;
786 val &= ~(OV5645_SENSOR_MIRROR);
788 val |= (OV5645_SENSOR_MIRROR);
790 ret = ov5645_write_reg(ov5645, OV5645_TIMING_TC_REG21, val);
792 ov5645->timing_tc_reg21 = val;
797 static int ov5645_set_vflip(struct ov5645 *ov5645, s32 value)
799 u8 val = ov5645->timing_tc_reg20;
803 val |= (OV5645_SENSOR_VFLIP | OV5645_ISP_VFLIP);
805 val &= ~(OV5645_SENSOR_VFLIP | OV5645_ISP_VFLIP);
807 ret = ov5645_write_reg(ov5645, OV5645_TIMING_TC_REG20, val);
809 ov5645->timing_tc_reg20 = val;
814 static int ov5645_set_test_pattern(struct ov5645 *ov5645, s32 value)
819 val = OV5645_SET_TEST_PATTERN(value - 1);
820 val |= OV5645_TEST_PATTERN_ENABLE;
823 return ov5645_write_reg(ov5645, OV5645_PRE_ISP_TEST_SETTING_1, val);
826 static const char * const ov5645_test_pattern_menu[] = {
828 "Vertical Color Bars",
829 "Pseudo-Random Data",
834 static int ov5645_set_awb(struct ov5645 *ov5645, s32 enable_auto)
839 val = OV5645_AWB_MANUAL_ENABLE;
841 return ov5645_write_reg(ov5645, OV5645_AWB_MANUAL_CONTROL, val);
844 static int ov5645_s_ctrl(struct v4l2_ctrl *ctrl)
846 struct ov5645 *ov5645 = container_of(ctrl->handler,
847 struct ov5645, ctrls);
850 mutex_lock(&ov5645->power_lock);
851 if (!ov5645->power_count) {
852 mutex_unlock(&ov5645->power_lock);
857 case V4L2_CID_SATURATION:
858 ret = ov5645_set_saturation(ov5645, ctrl->val);
860 case V4L2_CID_AUTO_WHITE_BALANCE:
861 ret = ov5645_set_awb(ov5645, ctrl->val);
863 case V4L2_CID_AUTOGAIN:
864 ret = ov5645_set_agc_mode(ov5645, ctrl->val);
866 case V4L2_CID_EXPOSURE_AUTO:
867 ret = ov5645_set_aec_mode(ov5645, ctrl->val);
869 case V4L2_CID_TEST_PATTERN:
870 ret = ov5645_set_test_pattern(ov5645, ctrl->val);
873 ret = ov5645_set_hflip(ov5645, ctrl->val);
876 ret = ov5645_set_vflip(ov5645, ctrl->val);
883 mutex_unlock(&ov5645->power_lock);
888 static struct v4l2_ctrl_ops ov5645_ctrl_ops = {
889 .s_ctrl = ov5645_s_ctrl,
892 static int ov5645_enum_mbus_code(struct v4l2_subdev *sd,
893 struct v4l2_subdev_pad_config *cfg,
894 struct v4l2_subdev_mbus_code_enum *code)
899 code->code = MEDIA_BUS_FMT_UYVY8_2X8;
904 static int ov5645_enum_frame_size(struct v4l2_subdev *subdev,
905 struct v4l2_subdev_pad_config *cfg,
906 struct v4l2_subdev_frame_size_enum *fse)
908 if (fse->code != MEDIA_BUS_FMT_UYVY8_2X8)
911 if (fse->index >= ARRAY_SIZE(ov5645_mode_info_data))
914 fse->min_width = ov5645_mode_info_data[fse->index].width;
915 fse->max_width = ov5645_mode_info_data[fse->index].width;
916 fse->min_height = ov5645_mode_info_data[fse->index].height;
917 fse->max_height = ov5645_mode_info_data[fse->index].height;
922 static struct v4l2_mbus_framefmt *
923 __ov5645_get_pad_format(struct ov5645 *ov5645,
924 struct v4l2_subdev_pad_config *cfg,
926 enum v4l2_subdev_format_whence which)
929 case V4L2_SUBDEV_FORMAT_TRY:
930 return v4l2_subdev_get_try_format(&ov5645->sd, cfg, pad);
931 case V4L2_SUBDEV_FORMAT_ACTIVE:
938 static int ov5645_get_format(struct v4l2_subdev *sd,
939 struct v4l2_subdev_pad_config *cfg,
940 struct v4l2_subdev_format *format)
942 struct ov5645 *ov5645 = to_ov5645(sd);
944 format->format = *__ov5645_get_pad_format(ov5645, cfg, format->pad,
949 static struct v4l2_rect *
950 __ov5645_get_pad_crop(struct ov5645 *ov5645, struct v4l2_subdev_pad_config *cfg,
951 unsigned int pad, enum v4l2_subdev_format_whence which)
954 case V4L2_SUBDEV_FORMAT_TRY:
955 return v4l2_subdev_get_try_crop(&ov5645->sd, cfg, pad);
956 case V4L2_SUBDEV_FORMAT_ACTIVE:
957 return &ov5645->crop;
963 static const struct ov5645_mode_info *
964 ov5645_find_nearest_mode(unsigned int width, unsigned int height)
968 for (i = ARRAY_SIZE(ov5645_mode_info_data) - 1; i >= 0; i--) {
969 if (ov5645_mode_info_data[i].width <= width &&
970 ov5645_mode_info_data[i].height <= height)
977 return &ov5645_mode_info_data[i];
980 static int ov5645_set_format(struct v4l2_subdev *sd,
981 struct v4l2_subdev_pad_config *cfg,
982 struct v4l2_subdev_format *format)
984 struct ov5645 *ov5645 = to_ov5645(sd);
985 struct v4l2_mbus_framefmt *__format;
986 struct v4l2_rect *__crop;
987 const struct ov5645_mode_info *new_mode;
990 __crop = __ov5645_get_pad_crop(ov5645, cfg, format->pad,
993 new_mode = ov5645_find_nearest_mode(format->format.width,
994 format->format.height);
995 __crop->width = new_mode->width;
996 __crop->height = new_mode->height;
998 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
999 ret = v4l2_ctrl_s_ctrl_int64(ov5645->pixel_clock,
1000 new_mode->pixel_clock);
1004 ret = v4l2_ctrl_s_ctrl(ov5645->link_freq,
1005 new_mode->link_freq);
1009 ov5645->current_mode = new_mode;
1012 __format = __ov5645_get_pad_format(ov5645, cfg, format->pad,
1014 __format->width = __crop->width;
1015 __format->height = __crop->height;
1016 __format->code = MEDIA_BUS_FMT_UYVY8_2X8;
1017 __format->field = V4L2_FIELD_NONE;
1018 __format->colorspace = V4L2_COLORSPACE_SRGB;
1020 format->format = *__format;
1025 static int ov5645_entity_init_cfg(struct v4l2_subdev *subdev,
1026 struct v4l2_subdev_pad_config *cfg)
1028 struct v4l2_subdev_format fmt = { 0 };
1030 fmt.which = cfg ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
1031 fmt.format.width = 1920;
1032 fmt.format.height = 1080;
1034 ov5645_set_format(subdev, cfg, &fmt);
1039 static int ov5645_get_selection(struct v4l2_subdev *sd,
1040 struct v4l2_subdev_pad_config *cfg,
1041 struct v4l2_subdev_selection *sel)
1043 struct ov5645 *ov5645 = to_ov5645(sd);
1045 if (sel->target != V4L2_SEL_TGT_CROP)
1048 sel->r = *__ov5645_get_pad_crop(ov5645, cfg, sel->pad,
1053 static int ov5645_s_stream(struct v4l2_subdev *subdev, int enable)
1055 struct ov5645 *ov5645 = to_ov5645(subdev);
1059 ret = ov5645_set_register_array(ov5645,
1060 ov5645->current_mode->data,
1061 ov5645->current_mode->data_size);
1063 dev_err(ov5645->dev, "could not set mode %dx%d\n",
1064 ov5645->current_mode->width,
1065 ov5645->current_mode->height);
1068 ret = v4l2_ctrl_handler_setup(&ov5645->ctrls);
1070 dev_err(ov5645->dev, "could not sync v4l2 controls\n");
1074 ret = ov5645_write_reg(ov5645, OV5645_IO_MIPI_CTRL00, 0x45);
1078 ret = ov5645_write_reg(ov5645, OV5645_SYSTEM_CTRL0,
1079 OV5645_SYSTEM_CTRL0_START);
1083 ret = ov5645_write_reg(ov5645, OV5645_IO_MIPI_CTRL00, 0x40);
1087 ret = ov5645_write_reg(ov5645, OV5645_SYSTEM_CTRL0,
1088 OV5645_SYSTEM_CTRL0_STOP);
1096 static const struct v4l2_subdev_core_ops ov5645_core_ops = {
1097 .s_power = ov5645_s_power,
1100 static const struct v4l2_subdev_video_ops ov5645_video_ops = {
1101 .s_stream = ov5645_s_stream,
1104 static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
1105 .init_cfg = ov5645_entity_init_cfg,
1106 .enum_mbus_code = ov5645_enum_mbus_code,
1107 .enum_frame_size = ov5645_enum_frame_size,
1108 .get_fmt = ov5645_get_format,
1109 .set_fmt = ov5645_set_format,
1110 .get_selection = ov5645_get_selection,
1113 static const struct v4l2_subdev_ops ov5645_subdev_ops = {
1114 .core = &ov5645_core_ops,
1115 .video = &ov5645_video_ops,
1116 .pad = &ov5645_subdev_pad_ops,
1119 static int ov5645_probe(struct i2c_client *client,
1120 const struct i2c_device_id *id)
1122 struct device *dev = &client->dev;
1123 struct device_node *endpoint;
1124 struct ov5645 *ov5645;
1125 u8 chip_id_high, chip_id_low;
1129 ov5645 = devm_kzalloc(dev, sizeof(struct ov5645), GFP_KERNEL);
1133 ov5645->i2c_client = client;
1136 endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
1138 dev_err(dev, "endpoint node not found\n");
1142 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint),
1145 of_node_put(endpoint);
1148 dev_err(dev, "parsing endpoint node failed\n");
1152 if (ov5645->ep.bus_type != V4L2_MBUS_CSI2) {
1153 dev_err(dev, "invalid bus type, must be CSI2\n");
1157 /* get system clock (xclk) */
1158 ov5645->xclk = devm_clk_get(dev, "xclk");
1159 if (IS_ERR(ov5645->xclk)) {
1160 dev_err(dev, "could not get xclk");
1161 return PTR_ERR(ov5645->xclk);
1164 ret = of_property_read_u32(dev->of_node, "clock-frequency", &xclk_freq);
1166 dev_err(dev, "could not get xclk frequency\n");
1170 /* external clock must be 24MHz, allow 1% tolerance */
1171 if (xclk_freq < 23760000 || xclk_freq > 24240000) {
1172 dev_err(dev, "external clock frequency %u is not supported\n",
1177 ret = clk_set_rate(ov5645->xclk, xclk_freq);
1179 dev_err(dev, "could not set xclk frequency\n");
1183 ov5645->io_regulator = devm_regulator_get(dev, "vdddo");
1184 if (IS_ERR(ov5645->io_regulator)) {
1185 dev_err(dev, "cannot get io regulator\n");
1186 return PTR_ERR(ov5645->io_regulator);
1189 ret = regulator_set_voltage(ov5645->io_regulator,
1190 OV5645_VOLTAGE_DIGITAL_IO,
1191 OV5645_VOLTAGE_DIGITAL_IO);
1193 dev_err(dev, "cannot set io voltage\n");
1197 ov5645->core_regulator = devm_regulator_get(dev, "vddd");
1198 if (IS_ERR(ov5645->core_regulator)) {
1199 dev_err(dev, "cannot get core regulator\n");
1200 return PTR_ERR(ov5645->core_regulator);
1203 ret = regulator_set_voltage(ov5645->core_regulator,
1204 OV5645_VOLTAGE_DIGITAL_CORE,
1205 OV5645_VOLTAGE_DIGITAL_CORE);
1207 dev_err(dev, "cannot set core voltage\n");
1211 ov5645->analog_regulator = devm_regulator_get(dev, "vdda");
1212 if (IS_ERR(ov5645->analog_regulator)) {
1213 dev_err(dev, "cannot get analog regulator\n");
1214 return PTR_ERR(ov5645->analog_regulator);
1217 ret = regulator_set_voltage(ov5645->analog_regulator,
1218 OV5645_VOLTAGE_ANALOG,
1219 OV5645_VOLTAGE_ANALOG);
1221 dev_err(dev, "cannot set analog voltage\n");
1225 ov5645->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_HIGH);
1226 if (IS_ERR(ov5645->enable_gpio)) {
1227 dev_err(dev, "cannot get enable gpio\n");
1228 return PTR_ERR(ov5645->enable_gpio);
1231 ov5645->rst_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
1232 if (IS_ERR(ov5645->rst_gpio)) {
1233 dev_err(dev, "cannot get reset gpio\n");
1234 return PTR_ERR(ov5645->rst_gpio);
1237 mutex_init(&ov5645->power_lock);
1239 v4l2_ctrl_handler_init(&ov5645->ctrls, 9);
1240 v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
1241 V4L2_CID_SATURATION, -4, 4, 1, 0);
1242 v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
1243 V4L2_CID_HFLIP, 0, 1, 1, 0);
1244 v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
1245 V4L2_CID_VFLIP, 0, 1, 1, 0);
1246 v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
1247 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1248 v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
1249 V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
1250 v4l2_ctrl_new_std_menu(&ov5645->ctrls, &ov5645_ctrl_ops,
1251 V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL,
1252 0, V4L2_EXPOSURE_AUTO);
1253 v4l2_ctrl_new_std_menu_items(&ov5645->ctrls, &ov5645_ctrl_ops,
1254 V4L2_CID_TEST_PATTERN,
1255 ARRAY_SIZE(ov5645_test_pattern_menu) - 1,
1256 0, 0, ov5645_test_pattern_menu);
1257 ov5645->pixel_clock = v4l2_ctrl_new_std(&ov5645->ctrls,
1259 V4L2_CID_PIXEL_RATE,
1261 ov5645->link_freq = v4l2_ctrl_new_int_menu(&ov5645->ctrls,
1264 ARRAY_SIZE(link_freq) - 1,
1266 if (ov5645->link_freq)
1267 ov5645->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1269 ov5645->sd.ctrl_handler = &ov5645->ctrls;
1271 if (ov5645->ctrls.error) {
1272 dev_err(dev, "%s: control initialization error %d\n",
1273 __func__, ov5645->ctrls.error);
1274 ret = ov5645->ctrls.error;
1278 v4l2_i2c_subdev_init(&ov5645->sd, client, &ov5645_subdev_ops);
1279 ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1280 ov5645->pad.flags = MEDIA_PAD_FL_SOURCE;
1281 ov5645->sd.dev = &client->dev;
1282 ov5645->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1284 ret = media_entity_pads_init(&ov5645->sd.entity, 1, &ov5645->pad);
1286 dev_err(dev, "could not register media entity\n");
1290 ret = ov5645_s_power(&ov5645->sd, true);
1292 dev_err(dev, "could not power up OV5645\n");
1296 ret = ov5645_read_reg(ov5645, OV5645_CHIP_ID_HIGH, &chip_id_high);
1297 if (ret < 0 || chip_id_high != OV5645_CHIP_ID_HIGH_BYTE) {
1298 dev_err(dev, "could not read ID high\n");
1302 ret = ov5645_read_reg(ov5645, OV5645_CHIP_ID_LOW, &chip_id_low);
1303 if (ret < 0 || chip_id_low != OV5645_CHIP_ID_LOW_BYTE) {
1304 dev_err(dev, "could not read ID low\n");
1309 dev_info(dev, "OV5645 detected at address 0x%02x\n", client->addr);
1311 ret = ov5645_read_reg(ov5645, OV5645_AEC_PK_MANUAL,
1312 &ov5645->aec_pk_manual);
1314 dev_err(dev, "could not read AEC/AGC mode\n");
1319 ret = ov5645_read_reg(ov5645, OV5645_TIMING_TC_REG20,
1320 &ov5645->timing_tc_reg20);
1322 dev_err(dev, "could not read vflip value\n");
1327 ret = ov5645_read_reg(ov5645, OV5645_TIMING_TC_REG21,
1328 &ov5645->timing_tc_reg21);
1330 dev_err(dev, "could not read hflip value\n");
1335 ov5645_s_power(&ov5645->sd, false);
1337 ret = v4l2_async_register_subdev(&ov5645->sd);
1339 dev_err(dev, "could not register v4l2 device\n");
1343 ov5645_entity_init_cfg(&ov5645->sd, NULL);
1348 ov5645_s_power(&ov5645->sd, false);
1350 media_entity_cleanup(&ov5645->sd.entity);
1352 v4l2_ctrl_handler_free(&ov5645->ctrls);
1353 mutex_destroy(&ov5645->power_lock);
1358 static int ov5645_remove(struct i2c_client *client)
1360 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1361 struct ov5645 *ov5645 = to_ov5645(sd);
1363 v4l2_async_unregister_subdev(&ov5645->sd);
1364 media_entity_cleanup(&ov5645->sd.entity);
1365 v4l2_ctrl_handler_free(&ov5645->ctrls);
1366 mutex_destroy(&ov5645->power_lock);
1371 static const struct i2c_device_id ov5645_id[] = {
1375 MODULE_DEVICE_TABLE(i2c, ov5645_id);
1377 static const struct of_device_id ov5645_of_match[] = {
1378 { .compatible = "ovti,ov5645" },
1381 MODULE_DEVICE_TABLE(of, ov5645_of_match);
1383 static struct i2c_driver ov5645_i2c_driver = {
1385 .of_match_table = of_match_ptr(ov5645_of_match),
1388 .probe = ov5645_probe,
1389 .remove = ov5645_remove,
1390 .id_table = ov5645_id,
1393 module_i2c_driver(ov5645_i2c_driver);
1395 MODULE_DESCRIPTION("Omnivision OV5645 Camera Driver");
1396 MODULE_AUTHOR("Todor Tomov <todor.tomov@linaro.org>");
1397 MODULE_LICENSE("GPL v2");