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_SYSTEM_CTRL0 0x3008
38 #define OV5645_SYSTEM_CTRL0_START 0x02
39 #define OV5645_SYSTEM_CTRL0_STOP 0x42
40 #define OV5645_CHIP_ID_HIGH 0x300a
41 #define OV5645_CHIP_ID_HIGH_BYTE 0x56
42 #define OV5645_CHIP_ID_LOW 0x300b
43 #define OV5645_CHIP_ID_LOW_BYTE 0x45
44 #define OV5645_IO_MIPI_CTRL00 0x300e
45 #define OV5645_PAD_OUTPUT00 0x3019
46 #define OV5645_AWB_MANUAL_CONTROL 0x3406
47 #define OV5645_AWB_MANUAL_ENABLE BIT(0)
48 #define OV5645_AEC_PK_MANUAL 0x3503
49 #define OV5645_AEC_MANUAL_ENABLE BIT(0)
50 #define OV5645_AGC_MANUAL_ENABLE BIT(1)
51 #define OV5645_TIMING_TC_REG20 0x3820
52 #define OV5645_SENSOR_VFLIP BIT(1)
53 #define OV5645_ISP_VFLIP BIT(2)
54 #define OV5645_TIMING_TC_REG21 0x3821
55 #define OV5645_SENSOR_MIRROR BIT(1)
56 #define OV5645_MIPI_CTRL00 0x4800
57 #define OV5645_PRE_ISP_TEST_SETTING_1 0x503d
58 #define OV5645_TEST_PATTERN_MASK 0x3
59 #define OV5645_SET_TEST_PATTERN(x) ((x) & OV5645_TEST_PATTERN_MASK)
60 #define OV5645_TEST_PATTERN_ENABLE BIT(7)
61 #define OV5645_SDE_SAT_U 0x5583
62 #define OV5645_SDE_SAT_V 0x5584
64 /* regulator supplies */
65 static const char * const ov5645_supply_name[] = {
66 "vdddo", /* Digital I/O (1.8V) supply */
67 "vdda", /* Analog (2.8V) supply */
68 "vddd", /* Digital Core (1.5V) supply */
71 #define OV5645_NUM_SUPPLIES ARRAY_SIZE(ov5645_supply_name)
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_bulk_data supplies[OV5645_NUM_SUPPLIES];
99 const struct ov5645_mode_info *current_mode;
101 struct v4l2_ctrl_handler ctrls;
102 struct v4l2_ctrl *pixel_clock;
103 struct v4l2_ctrl *link_freq;
105 /* Cached register values */
110 struct mutex power_lock; /* lock to protect power state */
113 struct gpio_desc *enable_gpio;
114 struct gpio_desc *rst_gpio;
117 static inline struct ov5645 *to_ov5645(struct v4l2_subdev *sd)
119 return container_of(sd, struct ov5645, sd);
122 static const struct reg_value ov5645_global_init_setting[] = {
359 { OV5645_IO_MIPI_CTRL00, 0x40 },
360 { OV5645_MIPI_CTRL00, 0x24 },
361 { OV5645_PAD_OUTPUT00, 0x70 }
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 = 112000000,
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 = 168000000,
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 = 168000000,
540 .link_freq = 1 /* an index in link_freq[] */
544 static int ov5645_write_reg(struct ov5645 *ov5645, u16 reg, u8 val)
549 regbuf[0] = reg >> 8;
550 regbuf[1] = reg & 0xff;
553 ret = i2c_master_send(ov5645->i2c_client, regbuf, 3);
555 dev_err(ov5645->dev, "%s: write reg error %d: reg=%x, val=%x\n",
556 __func__, ret, reg, val);
563 static int ov5645_read_reg(struct ov5645 *ov5645, u16 reg, u8 *val)
568 regbuf[0] = reg >> 8;
569 regbuf[1] = reg & 0xff;
571 ret = i2c_master_send(ov5645->i2c_client, regbuf, 2);
573 dev_err(ov5645->dev, "%s: write reg error %d: reg=%x\n",
578 ret = i2c_master_recv(ov5645->i2c_client, val, 1);
580 dev_err(ov5645->dev, "%s: read reg error %d: reg=%x\n",
588 static int ov5645_set_aec_mode(struct ov5645 *ov5645, u32 mode)
590 u8 val = ov5645->aec_pk_manual;
593 if (mode == V4L2_EXPOSURE_AUTO)
594 val &= ~OV5645_AEC_MANUAL_ENABLE;
595 else /* V4L2_EXPOSURE_MANUAL */
596 val |= OV5645_AEC_MANUAL_ENABLE;
598 ret = ov5645_write_reg(ov5645, OV5645_AEC_PK_MANUAL, val);
600 ov5645->aec_pk_manual = val;
605 static int ov5645_set_agc_mode(struct ov5645 *ov5645, u32 enable)
607 u8 val = ov5645->aec_pk_manual;
611 val &= ~OV5645_AGC_MANUAL_ENABLE;
613 val |= OV5645_AGC_MANUAL_ENABLE;
615 ret = ov5645_write_reg(ov5645, OV5645_AEC_PK_MANUAL, val);
617 ov5645->aec_pk_manual = val;
622 static int ov5645_set_register_array(struct ov5645 *ov5645,
623 const struct reg_value *settings,
624 unsigned int num_settings)
629 for (i = 0; i < num_settings; ++i, ++settings) {
630 ret = ov5645_write_reg(ov5645, settings->reg, settings->val);
638 static int ov5645_set_power_on(struct ov5645 *ov5645)
642 ret = regulator_bulk_enable(OV5645_NUM_SUPPLIES, ov5645->supplies);
646 ret = clk_prepare_enable(ov5645->xclk);
648 dev_err(ov5645->dev, "clk prepare enable failed\n");
649 regulator_bulk_disable(OV5645_NUM_SUPPLIES, ov5645->supplies);
653 usleep_range(5000, 15000);
654 gpiod_set_value_cansleep(ov5645->enable_gpio, 1);
656 usleep_range(1000, 2000);
657 gpiod_set_value_cansleep(ov5645->rst_gpio, 0);
664 static void ov5645_set_power_off(struct ov5645 *ov5645)
666 gpiod_set_value_cansleep(ov5645->rst_gpio, 1);
667 gpiod_set_value_cansleep(ov5645->enable_gpio, 0);
668 clk_disable_unprepare(ov5645->xclk);
669 regulator_bulk_disable(OV5645_NUM_SUPPLIES, ov5645->supplies);
672 static int ov5645_s_power(struct v4l2_subdev *sd, int on)
674 struct ov5645 *ov5645 = to_ov5645(sd);
677 mutex_lock(&ov5645->power_lock);
679 /* If the power count is modified from 0 to != 0 or from != 0 to 0,
680 * update the power state.
682 if (ov5645->power_count == !on) {
684 ret = ov5645_set_power_on(ov5645);
688 ret = ov5645_set_register_array(ov5645,
689 ov5645_global_init_setting,
690 ARRAY_SIZE(ov5645_global_init_setting));
693 "could not set init registers\n");
694 ov5645_set_power_off(ov5645);
698 usleep_range(500, 1000);
700 ov5645_write_reg(ov5645, OV5645_IO_MIPI_CTRL00, 0x58);
701 ov5645_set_power_off(ov5645);
705 /* Update the power count. */
706 ov5645->power_count += on ? 1 : -1;
707 WARN_ON(ov5645->power_count < 0);
710 mutex_unlock(&ov5645->power_lock);
715 static int ov5645_set_saturation(struct ov5645 *ov5645, s32 value)
717 u32 reg_value = (value * 0x10) + 0x40;
720 ret = ov5645_write_reg(ov5645, OV5645_SDE_SAT_U, reg_value);
724 return ov5645_write_reg(ov5645, OV5645_SDE_SAT_V, reg_value);
727 static int ov5645_set_hflip(struct ov5645 *ov5645, s32 value)
729 u8 val = ov5645->timing_tc_reg21;
733 val &= ~(OV5645_SENSOR_MIRROR);
735 val |= (OV5645_SENSOR_MIRROR);
737 ret = ov5645_write_reg(ov5645, OV5645_TIMING_TC_REG21, val);
739 ov5645->timing_tc_reg21 = val;
744 static int ov5645_set_vflip(struct ov5645 *ov5645, s32 value)
746 u8 val = ov5645->timing_tc_reg20;
750 val |= (OV5645_SENSOR_VFLIP | OV5645_ISP_VFLIP);
752 val &= ~(OV5645_SENSOR_VFLIP | OV5645_ISP_VFLIP);
754 ret = ov5645_write_reg(ov5645, OV5645_TIMING_TC_REG20, val);
756 ov5645->timing_tc_reg20 = val;
761 static int ov5645_set_test_pattern(struct ov5645 *ov5645, s32 value)
766 val = OV5645_SET_TEST_PATTERN(value - 1);
767 val |= OV5645_TEST_PATTERN_ENABLE;
770 return ov5645_write_reg(ov5645, OV5645_PRE_ISP_TEST_SETTING_1, val);
773 static const char * const ov5645_test_pattern_menu[] = {
775 "Vertical Color Bars",
776 "Pseudo-Random Data",
781 static int ov5645_set_awb(struct ov5645 *ov5645, s32 enable_auto)
786 val = OV5645_AWB_MANUAL_ENABLE;
788 return ov5645_write_reg(ov5645, OV5645_AWB_MANUAL_CONTROL, val);
791 static int ov5645_s_ctrl(struct v4l2_ctrl *ctrl)
793 struct ov5645 *ov5645 = container_of(ctrl->handler,
794 struct ov5645, ctrls);
797 mutex_lock(&ov5645->power_lock);
798 if (!ov5645->power_count) {
799 mutex_unlock(&ov5645->power_lock);
804 case V4L2_CID_SATURATION:
805 ret = ov5645_set_saturation(ov5645, ctrl->val);
807 case V4L2_CID_AUTO_WHITE_BALANCE:
808 ret = ov5645_set_awb(ov5645, ctrl->val);
810 case V4L2_CID_AUTOGAIN:
811 ret = ov5645_set_agc_mode(ov5645, ctrl->val);
813 case V4L2_CID_EXPOSURE_AUTO:
814 ret = ov5645_set_aec_mode(ov5645, ctrl->val);
816 case V4L2_CID_TEST_PATTERN:
817 ret = ov5645_set_test_pattern(ov5645, ctrl->val);
820 ret = ov5645_set_hflip(ov5645, ctrl->val);
823 ret = ov5645_set_vflip(ov5645, ctrl->val);
830 mutex_unlock(&ov5645->power_lock);
835 static const struct v4l2_ctrl_ops ov5645_ctrl_ops = {
836 .s_ctrl = ov5645_s_ctrl,
839 static int ov5645_enum_mbus_code(struct v4l2_subdev *sd,
840 struct v4l2_subdev_pad_config *cfg,
841 struct v4l2_subdev_mbus_code_enum *code)
846 code->code = MEDIA_BUS_FMT_UYVY8_2X8;
851 static int ov5645_enum_frame_size(struct v4l2_subdev *subdev,
852 struct v4l2_subdev_pad_config *cfg,
853 struct v4l2_subdev_frame_size_enum *fse)
855 if (fse->code != MEDIA_BUS_FMT_UYVY8_2X8)
858 if (fse->index >= ARRAY_SIZE(ov5645_mode_info_data))
861 fse->min_width = ov5645_mode_info_data[fse->index].width;
862 fse->max_width = ov5645_mode_info_data[fse->index].width;
863 fse->min_height = ov5645_mode_info_data[fse->index].height;
864 fse->max_height = ov5645_mode_info_data[fse->index].height;
869 static struct v4l2_mbus_framefmt *
870 __ov5645_get_pad_format(struct ov5645 *ov5645,
871 struct v4l2_subdev_pad_config *cfg,
873 enum v4l2_subdev_format_whence which)
876 case V4L2_SUBDEV_FORMAT_TRY:
877 return v4l2_subdev_get_try_format(&ov5645->sd, cfg, pad);
878 case V4L2_SUBDEV_FORMAT_ACTIVE:
885 static int ov5645_get_format(struct v4l2_subdev *sd,
886 struct v4l2_subdev_pad_config *cfg,
887 struct v4l2_subdev_format *format)
889 struct ov5645 *ov5645 = to_ov5645(sd);
891 format->format = *__ov5645_get_pad_format(ov5645, cfg, format->pad,
896 static struct v4l2_rect *
897 __ov5645_get_pad_crop(struct ov5645 *ov5645, struct v4l2_subdev_pad_config *cfg,
898 unsigned int pad, enum v4l2_subdev_format_whence which)
901 case V4L2_SUBDEV_FORMAT_TRY:
902 return v4l2_subdev_get_try_crop(&ov5645->sd, cfg, pad);
903 case V4L2_SUBDEV_FORMAT_ACTIVE:
904 return &ov5645->crop;
910 static int ov5645_set_format(struct v4l2_subdev *sd,
911 struct v4l2_subdev_pad_config *cfg,
912 struct v4l2_subdev_format *format)
914 struct ov5645 *ov5645 = to_ov5645(sd);
915 struct v4l2_mbus_framefmt *__format;
916 struct v4l2_rect *__crop;
917 const struct ov5645_mode_info *new_mode;
920 __crop = __ov5645_get_pad_crop(ov5645, cfg, format->pad,
923 new_mode = v4l2_find_nearest_size(ov5645_mode_info_data,
924 ARRAY_SIZE(ov5645_mode_info_data),
926 format->format.width, format->format.height);
928 __crop->width = new_mode->width;
929 __crop->height = new_mode->height;
931 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
932 ret = v4l2_ctrl_s_ctrl_int64(ov5645->pixel_clock,
933 new_mode->pixel_clock);
937 ret = v4l2_ctrl_s_ctrl(ov5645->link_freq,
938 new_mode->link_freq);
942 ov5645->current_mode = new_mode;
945 __format = __ov5645_get_pad_format(ov5645, cfg, format->pad,
947 __format->width = __crop->width;
948 __format->height = __crop->height;
949 __format->code = MEDIA_BUS_FMT_UYVY8_2X8;
950 __format->field = V4L2_FIELD_NONE;
951 __format->colorspace = V4L2_COLORSPACE_SRGB;
953 format->format = *__format;
958 static int ov5645_entity_init_cfg(struct v4l2_subdev *subdev,
959 struct v4l2_subdev_pad_config *cfg)
961 struct v4l2_subdev_format fmt = { 0 };
963 fmt.which = cfg ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
964 fmt.format.width = 1920;
965 fmt.format.height = 1080;
967 ov5645_set_format(subdev, cfg, &fmt);
972 static int ov5645_get_selection(struct v4l2_subdev *sd,
973 struct v4l2_subdev_pad_config *cfg,
974 struct v4l2_subdev_selection *sel)
976 struct ov5645 *ov5645 = to_ov5645(sd);
978 if (sel->target != V4L2_SEL_TGT_CROP)
981 sel->r = *__ov5645_get_pad_crop(ov5645, cfg, sel->pad,
986 static int ov5645_s_stream(struct v4l2_subdev *subdev, int enable)
988 struct ov5645 *ov5645 = to_ov5645(subdev);
992 ret = ov5645_set_register_array(ov5645,
993 ov5645->current_mode->data,
994 ov5645->current_mode->data_size);
996 dev_err(ov5645->dev, "could not set mode %dx%d\n",
997 ov5645->current_mode->width,
998 ov5645->current_mode->height);
1001 ret = v4l2_ctrl_handler_setup(&ov5645->ctrls);
1003 dev_err(ov5645->dev, "could not sync v4l2 controls\n");
1007 ret = ov5645_write_reg(ov5645, OV5645_IO_MIPI_CTRL00, 0x45);
1011 ret = ov5645_write_reg(ov5645, OV5645_SYSTEM_CTRL0,
1012 OV5645_SYSTEM_CTRL0_START);
1016 ret = ov5645_write_reg(ov5645, OV5645_IO_MIPI_CTRL00, 0x40);
1020 ret = ov5645_write_reg(ov5645, OV5645_SYSTEM_CTRL0,
1021 OV5645_SYSTEM_CTRL0_STOP);
1029 static const struct v4l2_subdev_core_ops ov5645_core_ops = {
1030 .s_power = ov5645_s_power,
1033 static const struct v4l2_subdev_video_ops ov5645_video_ops = {
1034 .s_stream = ov5645_s_stream,
1037 static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
1038 .init_cfg = ov5645_entity_init_cfg,
1039 .enum_mbus_code = ov5645_enum_mbus_code,
1040 .enum_frame_size = ov5645_enum_frame_size,
1041 .get_fmt = ov5645_get_format,
1042 .set_fmt = ov5645_set_format,
1043 .get_selection = ov5645_get_selection,
1046 static const struct v4l2_subdev_ops ov5645_subdev_ops = {
1047 .core = &ov5645_core_ops,
1048 .video = &ov5645_video_ops,
1049 .pad = &ov5645_subdev_pad_ops,
1052 static int ov5645_probe(struct i2c_client *client)
1054 struct device *dev = &client->dev;
1055 struct device_node *endpoint;
1056 struct ov5645 *ov5645;
1057 u8 chip_id_high, chip_id_low;
1062 ov5645 = devm_kzalloc(dev, sizeof(struct ov5645), GFP_KERNEL);
1066 ov5645->i2c_client = client;
1069 endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
1071 dev_err(dev, "endpoint node not found\n");
1075 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint),
1078 of_node_put(endpoint);
1081 dev_err(dev, "parsing endpoint node failed\n");
1085 if (ov5645->ep.bus_type != V4L2_MBUS_CSI2_DPHY) {
1086 dev_err(dev, "invalid bus type, must be CSI2\n");
1090 /* get system clock (xclk) */
1091 ov5645->xclk = devm_clk_get(dev, "xclk");
1092 if (IS_ERR(ov5645->xclk)) {
1093 dev_err(dev, "could not get xclk");
1094 return PTR_ERR(ov5645->xclk);
1097 ret = of_property_read_u32(dev->of_node, "clock-frequency", &xclk_freq);
1099 dev_err(dev, "could not get xclk frequency\n");
1103 /* external clock must be 24MHz, allow 1% tolerance */
1104 if (xclk_freq < 23760000 || xclk_freq > 24240000) {
1105 dev_err(dev, "external clock frequency %u is not supported\n",
1110 ret = clk_set_rate(ov5645->xclk, xclk_freq);
1112 dev_err(dev, "could not set xclk frequency\n");
1116 for (i = 0; i < OV5645_NUM_SUPPLIES; i++)
1117 ov5645->supplies[i].supply = ov5645_supply_name[i];
1119 ret = devm_regulator_bulk_get(dev, OV5645_NUM_SUPPLIES,
1124 ov5645->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_HIGH);
1125 if (IS_ERR(ov5645->enable_gpio)) {
1126 dev_err(dev, "cannot get enable gpio\n");
1127 return PTR_ERR(ov5645->enable_gpio);
1130 ov5645->rst_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
1131 if (IS_ERR(ov5645->rst_gpio)) {
1132 dev_err(dev, "cannot get reset gpio\n");
1133 return PTR_ERR(ov5645->rst_gpio);
1136 mutex_init(&ov5645->power_lock);
1138 v4l2_ctrl_handler_init(&ov5645->ctrls, 9);
1139 v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
1140 V4L2_CID_SATURATION, -4, 4, 1, 0);
1141 v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
1142 V4L2_CID_HFLIP, 0, 1, 1, 0);
1143 v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
1144 V4L2_CID_VFLIP, 0, 1, 1, 0);
1145 v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
1146 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1147 v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
1148 V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
1149 v4l2_ctrl_new_std_menu(&ov5645->ctrls, &ov5645_ctrl_ops,
1150 V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL,
1151 0, V4L2_EXPOSURE_AUTO);
1152 v4l2_ctrl_new_std_menu_items(&ov5645->ctrls, &ov5645_ctrl_ops,
1153 V4L2_CID_TEST_PATTERN,
1154 ARRAY_SIZE(ov5645_test_pattern_menu) - 1,
1155 0, 0, ov5645_test_pattern_menu);
1156 ov5645->pixel_clock = v4l2_ctrl_new_std(&ov5645->ctrls,
1158 V4L2_CID_PIXEL_RATE,
1160 ov5645->link_freq = v4l2_ctrl_new_int_menu(&ov5645->ctrls,
1163 ARRAY_SIZE(link_freq) - 1,
1165 if (ov5645->link_freq)
1166 ov5645->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1168 ov5645->sd.ctrl_handler = &ov5645->ctrls;
1170 if (ov5645->ctrls.error) {
1171 dev_err(dev, "%s: control initialization error %d\n",
1172 __func__, ov5645->ctrls.error);
1173 ret = ov5645->ctrls.error;
1177 v4l2_i2c_subdev_init(&ov5645->sd, client, &ov5645_subdev_ops);
1178 ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1179 ov5645->pad.flags = MEDIA_PAD_FL_SOURCE;
1180 ov5645->sd.dev = &client->dev;
1181 ov5645->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1183 ret = media_entity_pads_init(&ov5645->sd.entity, 1, &ov5645->pad);
1185 dev_err(dev, "could not register media entity\n");
1189 ret = ov5645_s_power(&ov5645->sd, true);
1191 dev_err(dev, "could not power up OV5645\n");
1195 ret = ov5645_read_reg(ov5645, OV5645_CHIP_ID_HIGH, &chip_id_high);
1196 if (ret < 0 || chip_id_high != OV5645_CHIP_ID_HIGH_BYTE) {
1197 dev_err(dev, "could not read ID high\n");
1201 ret = ov5645_read_reg(ov5645, OV5645_CHIP_ID_LOW, &chip_id_low);
1202 if (ret < 0 || chip_id_low != OV5645_CHIP_ID_LOW_BYTE) {
1203 dev_err(dev, "could not read ID low\n");
1208 dev_info(dev, "OV5645 detected at address 0x%02x\n", client->addr);
1210 ret = ov5645_read_reg(ov5645, OV5645_AEC_PK_MANUAL,
1211 &ov5645->aec_pk_manual);
1213 dev_err(dev, "could not read AEC/AGC mode\n");
1218 ret = ov5645_read_reg(ov5645, OV5645_TIMING_TC_REG20,
1219 &ov5645->timing_tc_reg20);
1221 dev_err(dev, "could not read vflip value\n");
1226 ret = ov5645_read_reg(ov5645, OV5645_TIMING_TC_REG21,
1227 &ov5645->timing_tc_reg21);
1229 dev_err(dev, "could not read hflip value\n");
1234 ov5645_s_power(&ov5645->sd, false);
1236 ret = v4l2_async_register_subdev(&ov5645->sd);
1238 dev_err(dev, "could not register v4l2 device\n");
1242 ov5645_entity_init_cfg(&ov5645->sd, NULL);
1247 ov5645_s_power(&ov5645->sd, false);
1249 media_entity_cleanup(&ov5645->sd.entity);
1251 v4l2_ctrl_handler_free(&ov5645->ctrls);
1252 mutex_destroy(&ov5645->power_lock);
1257 static int ov5645_remove(struct i2c_client *client)
1259 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1260 struct ov5645 *ov5645 = to_ov5645(sd);
1262 v4l2_async_unregister_subdev(&ov5645->sd);
1263 media_entity_cleanup(&ov5645->sd.entity);
1264 v4l2_ctrl_handler_free(&ov5645->ctrls);
1265 mutex_destroy(&ov5645->power_lock);
1270 static const struct i2c_device_id ov5645_id[] = {
1274 MODULE_DEVICE_TABLE(i2c, ov5645_id);
1276 static const struct of_device_id ov5645_of_match[] = {
1277 { .compatible = "ovti,ov5645" },
1280 MODULE_DEVICE_TABLE(of, ov5645_of_match);
1282 static struct i2c_driver ov5645_i2c_driver = {
1284 .of_match_table = of_match_ptr(ov5645_of_match),
1287 .probe_new = ov5645_probe,
1288 .remove = ov5645_remove,
1289 .id_table = ov5645_id,
1292 module_i2c_driver(ov5645_i2c_driver);
1294 MODULE_DESCRIPTION("Omnivision OV5645 Camera Driver");
1295 MODULE_AUTHOR("Todor Tomov <todor.tomov@linaro.org>");
1296 MODULE_LICENSE("GPL v2");