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
17 #include <linux/bitops.h>
18 #include <linux/clk.h>
19 #include <linux/delay.h>
20 #include <linux/device.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/i2c.h>
23 #include <linux/init.h>
24 #include <linux/module.h>
26 #include <linux/of_graph.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/slab.h>
30 #include <linux/types.h>
31 #include <media/v4l2-ctrls.h>
32 #include <media/v4l2-fwnode.h>
33 #include <media/v4l2-subdev.h>
35 #define OV5645_SYSTEM_CTRL0 0x3008
36 #define OV5645_SYSTEM_CTRL0_START 0x02
37 #define OV5645_SYSTEM_CTRL0_STOP 0x42
38 #define OV5645_CHIP_ID_HIGH 0x300a
39 #define OV5645_CHIP_ID_HIGH_BYTE 0x56
40 #define OV5645_CHIP_ID_LOW 0x300b
41 #define OV5645_CHIP_ID_LOW_BYTE 0x45
42 #define OV5645_IO_MIPI_CTRL00 0x300e
43 #define OV5645_PAD_OUTPUT00 0x3019
44 #define OV5645_AWB_MANUAL_CONTROL 0x3406
45 #define OV5645_AWB_MANUAL_ENABLE BIT(0)
46 #define OV5645_AEC_PK_MANUAL 0x3503
47 #define OV5645_AEC_MANUAL_ENABLE BIT(0)
48 #define OV5645_AGC_MANUAL_ENABLE BIT(1)
49 #define OV5645_TIMING_TC_REG20 0x3820
50 #define OV5645_SENSOR_VFLIP BIT(1)
51 #define OV5645_ISP_VFLIP BIT(2)
52 #define OV5645_TIMING_TC_REG21 0x3821
53 #define OV5645_SENSOR_MIRROR BIT(1)
54 #define OV5645_MIPI_CTRL00 0x4800
55 #define OV5645_PRE_ISP_TEST_SETTING_1 0x503d
56 #define OV5645_TEST_PATTERN_MASK 0x3
57 #define OV5645_SET_TEST_PATTERN(x) ((x) & OV5645_TEST_PATTERN_MASK)
58 #define OV5645_TEST_PATTERN_ENABLE BIT(7)
59 #define OV5645_SDE_SAT_U 0x5583
60 #define OV5645_SDE_SAT_V 0x5584
62 /* regulator supplies */
63 static const char * const ov5645_supply_name
[] = {
64 "vdddo", /* Digital I/O (1.8V) supply */
65 "vdda", /* Analog (2.8V) supply */
66 "vddd", /* Digital Core (1.5V) supply */
69 #define OV5645_NUM_SUPPLIES ARRAY_SIZE(ov5645_supply_name)
76 struct ov5645_mode_info
{
79 const struct reg_value
*data
;
86 struct i2c_client
*i2c_client
;
88 struct v4l2_subdev sd
;
90 struct v4l2_fwnode_endpoint ep
;
91 struct v4l2_rect crop
;
94 struct regulator_bulk_data supplies
[OV5645_NUM_SUPPLIES
];
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 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
[] = {
352 { OV5645_IO_MIPI_CTRL00
, 0x40 },
353 { OV5645_MIPI_CTRL00
, 0x24 },
354 { OV5645_PAD_OUTPUT00
, 0x70 }
357 static const struct reg_value ov5645_setting_sxga
[] = {
405 static const struct reg_value ov5645_setting_1080p
[] = {
455 static const struct reg_value ov5645_setting_full
[] = {
505 static const s64 link_freq
[] = {
510 static const struct ov5645_mode_info ov5645_mode_info_data
[] = {
514 .data
= ov5645_setting_sxga
,
515 .data_size
= ARRAY_SIZE(ov5645_setting_sxga
),
516 .pixel_clock
= 112000000,
517 .link_freq
= 0 /* an index in link_freq[] */
522 .data
= ov5645_setting_1080p
,
523 .data_size
= ARRAY_SIZE(ov5645_setting_1080p
),
524 .pixel_clock
= 168000000,
525 .link_freq
= 1 /* an index in link_freq[] */
530 .data
= ov5645_setting_full
,
531 .data_size
= ARRAY_SIZE(ov5645_setting_full
),
532 .pixel_clock
= 168000000,
533 .link_freq
= 1 /* an index in link_freq[] */
537 static int ov5645_write_reg(struct ov5645
*ov5645
, u16 reg
, u8 val
)
542 regbuf
[0] = reg
>> 8;
543 regbuf
[1] = reg
& 0xff;
546 ret
= i2c_master_send(ov5645
->i2c_client
, regbuf
, 3);
548 dev_err(ov5645
->dev
, "%s: write reg error %d: reg=%x, val=%x\n",
549 __func__
, ret
, reg
, val
);
556 static int ov5645_read_reg(struct ov5645
*ov5645
, u16 reg
, u8
*val
)
561 regbuf
[0] = reg
>> 8;
562 regbuf
[1] = reg
& 0xff;
564 ret
= i2c_master_send(ov5645
->i2c_client
, regbuf
, 2);
566 dev_err(ov5645
->dev
, "%s: write reg error %d: reg=%x\n",
571 ret
= i2c_master_recv(ov5645
->i2c_client
, val
, 1);
573 dev_err(ov5645
->dev
, "%s: read reg error %d: reg=%x\n",
581 static int ov5645_set_aec_mode(struct ov5645
*ov5645
, u32 mode
)
583 u8 val
= ov5645
->aec_pk_manual
;
586 if (mode
== V4L2_EXPOSURE_AUTO
)
587 val
&= ~OV5645_AEC_MANUAL_ENABLE
;
588 else /* V4L2_EXPOSURE_MANUAL */
589 val
|= OV5645_AEC_MANUAL_ENABLE
;
591 ret
= ov5645_write_reg(ov5645
, OV5645_AEC_PK_MANUAL
, val
);
593 ov5645
->aec_pk_manual
= val
;
598 static int ov5645_set_agc_mode(struct ov5645
*ov5645
, u32 enable
)
600 u8 val
= ov5645
->aec_pk_manual
;
604 val
&= ~OV5645_AGC_MANUAL_ENABLE
;
606 val
|= OV5645_AGC_MANUAL_ENABLE
;
608 ret
= ov5645_write_reg(ov5645
, OV5645_AEC_PK_MANUAL
, val
);
610 ov5645
->aec_pk_manual
= val
;
615 static int ov5645_set_register_array(struct ov5645
*ov5645
,
616 const struct reg_value
*settings
,
617 unsigned int num_settings
)
622 for (i
= 0; i
< num_settings
; ++i
, ++settings
) {
623 ret
= ov5645_write_reg(ov5645
, settings
->reg
, settings
->val
);
627 if (settings
->reg
== OV5645_SYSTEM_CTRL0
&&
628 settings
->val
== OV5645_SYSTEM_CTRL0_START
)
629 usleep_range(1000, 2000);
635 static void __ov5645_set_power_off(struct device
*dev
)
637 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
638 struct ov5645
*ov5645
= to_ov5645(sd
);
640 ov5645_write_reg(ov5645
, OV5645_IO_MIPI_CTRL00
, 0x58);
641 gpiod_set_value_cansleep(ov5645
->rst_gpio
, 1);
642 gpiod_set_value_cansleep(ov5645
->enable_gpio
, 0);
643 regulator_bulk_disable(OV5645_NUM_SUPPLIES
, ov5645
->supplies
);
646 static int ov5645_set_power_off(struct device
*dev
)
648 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
649 struct ov5645
*ov5645
= to_ov5645(sd
);
651 __ov5645_set_power_off(dev
);
652 clk_disable_unprepare(ov5645
->xclk
);
657 static int ov5645_set_power_on(struct device
*dev
)
659 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
660 struct ov5645
*ov5645
= to_ov5645(sd
);
663 ret
= regulator_bulk_enable(OV5645_NUM_SUPPLIES
, ov5645
->supplies
);
667 ret
= clk_prepare_enable(ov5645
->xclk
);
669 dev_err(ov5645
->dev
, "clk prepare enable failed\n");
670 regulator_bulk_disable(OV5645_NUM_SUPPLIES
, ov5645
->supplies
);
674 usleep_range(5000, 15000);
675 gpiod_set_value_cansleep(ov5645
->enable_gpio
, 1);
677 usleep_range(1000, 2000);
678 gpiod_set_value_cansleep(ov5645
->rst_gpio
, 0);
682 ret
= ov5645_set_register_array(ov5645
, ov5645_global_init_setting
,
683 ARRAY_SIZE(ov5645_global_init_setting
));
685 dev_err(ov5645
->dev
, "could not set init registers\n");
689 usleep_range(500, 1000);
694 __ov5645_set_power_off(dev
);
695 clk_disable_unprepare(ov5645
->xclk
);
699 static int ov5645_set_saturation(struct ov5645
*ov5645
, s32 value
)
701 u32 reg_value
= (value
* 0x10) + 0x40;
704 ret
= ov5645_write_reg(ov5645
, OV5645_SDE_SAT_U
, reg_value
);
708 return ov5645_write_reg(ov5645
, OV5645_SDE_SAT_V
, reg_value
);
711 static int ov5645_set_hflip(struct ov5645
*ov5645
, s32 value
)
713 u8 val
= ov5645
->timing_tc_reg21
;
717 val
&= ~(OV5645_SENSOR_MIRROR
);
719 val
|= (OV5645_SENSOR_MIRROR
);
721 ret
= ov5645_write_reg(ov5645
, OV5645_TIMING_TC_REG21
, val
);
723 ov5645
->timing_tc_reg21
= val
;
728 static int ov5645_set_vflip(struct ov5645
*ov5645
, s32 value
)
730 u8 val
= ov5645
->timing_tc_reg20
;
734 val
|= (OV5645_SENSOR_VFLIP
| OV5645_ISP_VFLIP
);
736 val
&= ~(OV5645_SENSOR_VFLIP
| OV5645_ISP_VFLIP
);
738 ret
= ov5645_write_reg(ov5645
, OV5645_TIMING_TC_REG20
, val
);
740 ov5645
->timing_tc_reg20
= val
;
745 static int ov5645_set_test_pattern(struct ov5645
*ov5645
, s32 value
)
750 val
= OV5645_SET_TEST_PATTERN(value
- 1);
751 val
|= OV5645_TEST_PATTERN_ENABLE
;
754 return ov5645_write_reg(ov5645
, OV5645_PRE_ISP_TEST_SETTING_1
, val
);
757 static const char * const ov5645_test_pattern_menu
[] = {
759 "Vertical Color Bars",
760 "Pseudo-Random Data",
765 static int ov5645_set_awb(struct ov5645
*ov5645
, s32 enable_auto
)
770 val
= OV5645_AWB_MANUAL_ENABLE
;
772 return ov5645_write_reg(ov5645
, OV5645_AWB_MANUAL_CONTROL
, val
);
775 static int ov5645_s_ctrl(struct v4l2_ctrl
*ctrl
)
777 struct ov5645
*ov5645
= container_of(ctrl
->handler
,
778 struct ov5645
, ctrls
);
781 if (!pm_runtime_get_if_in_use(ov5645
->dev
))
785 case V4L2_CID_SATURATION
:
786 ret
= ov5645_set_saturation(ov5645
, ctrl
->val
);
788 case V4L2_CID_AUTO_WHITE_BALANCE
:
789 ret
= ov5645_set_awb(ov5645
, ctrl
->val
);
791 case V4L2_CID_AUTOGAIN
:
792 ret
= ov5645_set_agc_mode(ov5645
, ctrl
->val
);
794 case V4L2_CID_EXPOSURE_AUTO
:
795 ret
= ov5645_set_aec_mode(ov5645
, ctrl
->val
);
797 case V4L2_CID_TEST_PATTERN
:
798 ret
= ov5645_set_test_pattern(ov5645
, ctrl
->val
);
801 ret
= ov5645_set_hflip(ov5645
, ctrl
->val
);
804 ret
= ov5645_set_vflip(ov5645
, ctrl
->val
);
811 pm_runtime_mark_last_busy(ov5645
->dev
);
812 pm_runtime_put_autosuspend(ov5645
->dev
);
817 static const struct v4l2_ctrl_ops ov5645_ctrl_ops
= {
818 .s_ctrl
= ov5645_s_ctrl
,
821 static int ov5645_enum_mbus_code(struct v4l2_subdev
*sd
,
822 struct v4l2_subdev_state
*sd_state
,
823 struct v4l2_subdev_mbus_code_enum
*code
)
828 code
->code
= MEDIA_BUS_FMT_UYVY8_1X16
;
833 static int ov5645_enum_frame_size(struct v4l2_subdev
*subdev
,
834 struct v4l2_subdev_state
*sd_state
,
835 struct v4l2_subdev_frame_size_enum
*fse
)
837 if (fse
->code
!= MEDIA_BUS_FMT_UYVY8_1X16
)
840 if (fse
->index
>= ARRAY_SIZE(ov5645_mode_info_data
))
843 fse
->min_width
= ov5645_mode_info_data
[fse
->index
].width
;
844 fse
->max_width
= ov5645_mode_info_data
[fse
->index
].width
;
845 fse
->min_height
= ov5645_mode_info_data
[fse
->index
].height
;
846 fse
->max_height
= ov5645_mode_info_data
[fse
->index
].height
;
851 static int ov5645_set_format(struct v4l2_subdev
*sd
,
852 struct v4l2_subdev_state
*sd_state
,
853 struct v4l2_subdev_format
*format
)
855 struct ov5645
*ov5645
= to_ov5645(sd
);
856 struct v4l2_mbus_framefmt
*__format
;
857 struct v4l2_rect
*__crop
;
858 const struct ov5645_mode_info
*new_mode
;
861 __crop
= v4l2_subdev_state_get_crop(sd_state
, 0);
862 new_mode
= v4l2_find_nearest_size(ov5645_mode_info_data
,
863 ARRAY_SIZE(ov5645_mode_info_data
),
864 width
, height
, format
->format
.width
,
865 format
->format
.height
);
867 __crop
->width
= new_mode
->width
;
868 __crop
->height
= new_mode
->height
;
870 if (format
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
) {
871 ret
= __v4l2_ctrl_s_ctrl_int64(ov5645
->pixel_clock
,
872 new_mode
->pixel_clock
);
876 ret
= __v4l2_ctrl_s_ctrl(ov5645
->link_freq
,
877 new_mode
->link_freq
);
881 ov5645
->current_mode
= new_mode
;
884 __format
= v4l2_subdev_state_get_format(sd_state
, 0);
885 __format
->width
= __crop
->width
;
886 __format
->height
= __crop
->height
;
887 __format
->code
= MEDIA_BUS_FMT_UYVY8_1X16
;
888 __format
->field
= V4L2_FIELD_NONE
;
889 __format
->colorspace
= V4L2_COLORSPACE_SRGB
;
891 format
->format
= *__format
;
896 static int ov5645_init_state(struct v4l2_subdev
*subdev
,
897 struct v4l2_subdev_state
*sd_state
)
899 struct v4l2_subdev_format fmt
= {
900 .which
= V4L2_SUBDEV_FORMAT_TRY
,
903 .code
= MEDIA_BUS_FMT_UYVY8_1X16
,
904 .width
= ov5645_mode_info_data
[1].width
,
905 .height
= ov5645_mode_info_data
[1].height
,
909 ov5645_set_format(subdev
, sd_state
, &fmt
);
914 static int ov5645_get_selection(struct v4l2_subdev
*sd
,
915 struct v4l2_subdev_state
*sd_state
,
916 struct v4l2_subdev_selection
*sel
)
918 if (sel
->target
!= V4L2_SEL_TGT_CROP
)
921 sel
->r
= *v4l2_subdev_state_get_crop(sd_state
, 0);
925 static int ov5645_enable_streams(struct v4l2_subdev
*sd
,
926 struct v4l2_subdev_state
*state
, u32 pad
,
929 struct ov5645
*ov5645
= to_ov5645(sd
);
932 ret
= pm_runtime_resume_and_get(ov5645
->dev
);
936 ret
= ov5645_set_register_array(ov5645
,
937 ov5645
->current_mode
->data
,
938 ov5645
->current_mode
->data_size
);
940 dev_err(ov5645
->dev
, "could not set mode %dx%d\n",
941 ov5645
->current_mode
->width
,
942 ov5645
->current_mode
->height
);
945 ret
= __v4l2_ctrl_handler_setup(&ov5645
->ctrls
);
947 dev_err(ov5645
->dev
, "could not sync v4l2 controls\n");
951 ret
= ov5645_write_reg(ov5645
, OV5645_IO_MIPI_CTRL00
, 0x45);
955 ret
= ov5645_write_reg(ov5645
, OV5645_SYSTEM_CTRL0
,
956 OV5645_SYSTEM_CTRL0_START
);
963 pm_runtime_put_sync(ov5645
->dev
);
967 static int ov5645_disable_streams(struct v4l2_subdev
*sd
,
968 struct v4l2_subdev_state
*state
, u32 pad
,
971 struct ov5645
*ov5645
= to_ov5645(sd
);
974 ret
= ov5645_write_reg(ov5645
, OV5645_IO_MIPI_CTRL00
, 0x40);
978 ret
= ov5645_write_reg(ov5645
, OV5645_SYSTEM_CTRL0
,
979 OV5645_SYSTEM_CTRL0_STOP
);
982 pm_runtime_mark_last_busy(ov5645
->dev
);
983 pm_runtime_put_autosuspend(ov5645
->dev
);
988 static const struct v4l2_subdev_video_ops ov5645_video_ops
= {
989 .s_stream
= v4l2_subdev_s_stream_helper
,
992 static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops
= {
993 .enum_mbus_code
= ov5645_enum_mbus_code
,
994 .enum_frame_size
= ov5645_enum_frame_size
,
995 .get_fmt
= v4l2_subdev_get_fmt
,
996 .set_fmt
= ov5645_set_format
,
997 .get_selection
= ov5645_get_selection
,
998 .enable_streams
= ov5645_enable_streams
,
999 .disable_streams
= ov5645_disable_streams
,
1002 static const struct v4l2_subdev_ops ov5645_subdev_ops
= {
1003 .video
= &ov5645_video_ops
,
1004 .pad
= &ov5645_subdev_pad_ops
,
1007 static const struct v4l2_subdev_internal_ops ov5645_internal_ops
= {
1008 .init_state
= ov5645_init_state
,
1011 static int ov5645_probe(struct i2c_client
*client
)
1013 struct device
*dev
= &client
->dev
;
1014 struct device_node
*endpoint
;
1015 struct ov5645
*ov5645
;
1016 u8 chip_id_high
, chip_id_low
;
1021 ov5645
= devm_kzalloc(dev
, sizeof(struct ov5645
), GFP_KERNEL
);
1025 ov5645
->i2c_client
= client
;
1028 endpoint
= of_graph_get_endpoint_by_regs(dev
->of_node
, 0, -1);
1030 return dev_err_probe(dev
, -EINVAL
,
1031 "endpoint node not found\n");
1033 ret
= v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint
),
1036 of_node_put(endpoint
);
1039 return dev_err_probe(dev
, ret
,
1040 "parsing endpoint node failed\n");
1042 if (ov5645
->ep
.bus_type
!= V4L2_MBUS_CSI2_DPHY
)
1043 return dev_err_probe(dev
, -EINVAL
,
1044 "invalid bus type, must be CSI2\n");
1046 /* get system clock (xclk) */
1047 ov5645
->xclk
= devm_clk_get(dev
, NULL
);
1048 if (IS_ERR(ov5645
->xclk
))
1049 return dev_err_probe(dev
, PTR_ERR(ov5645
->xclk
),
1050 "could not get xclk");
1052 ret
= of_property_read_u32(dev
->of_node
, "clock-frequency", &xclk_freq
);
1054 return dev_err_probe(dev
, ret
,
1055 "could not get xclk frequency\n");
1057 /* external clock must be 24MHz, allow 1% tolerance */
1058 if (xclk_freq
< 23760000 || xclk_freq
> 24240000)
1059 return dev_err_probe(dev
, -EINVAL
,
1060 "unsupported xclk frequency %u\n",
1063 ret
= clk_set_rate(ov5645
->xclk
, xclk_freq
);
1065 return dev_err_probe(dev
, ret
,
1066 "could not set xclk frequency\n");
1068 for (i
= 0; i
< OV5645_NUM_SUPPLIES
; i
++)
1069 ov5645
->supplies
[i
].supply
= ov5645_supply_name
[i
];
1071 ret
= devm_regulator_bulk_get(dev
, OV5645_NUM_SUPPLIES
,
1076 ov5645
->enable_gpio
= devm_gpiod_get(dev
, "enable", GPIOD_OUT_HIGH
);
1077 if (IS_ERR(ov5645
->enable_gpio
))
1078 return dev_err_probe(dev
, PTR_ERR(ov5645
->enable_gpio
),
1079 "cannot get enable gpio\n");
1081 ov5645
->rst_gpio
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_HIGH
);
1082 if (IS_ERR(ov5645
->rst_gpio
))
1083 return dev_err_probe(dev
, PTR_ERR(ov5645
->rst_gpio
),
1084 "cannot get reset gpio\n");
1086 v4l2_ctrl_handler_init(&ov5645
->ctrls
, 9);
1087 v4l2_ctrl_new_std(&ov5645
->ctrls
, &ov5645_ctrl_ops
,
1088 V4L2_CID_SATURATION
, -4, 4, 1, 0);
1089 v4l2_ctrl_new_std(&ov5645
->ctrls
, &ov5645_ctrl_ops
,
1090 V4L2_CID_HFLIP
, 0, 1, 1, 0);
1091 v4l2_ctrl_new_std(&ov5645
->ctrls
, &ov5645_ctrl_ops
,
1092 V4L2_CID_VFLIP
, 0, 1, 1, 0);
1093 v4l2_ctrl_new_std(&ov5645
->ctrls
, &ov5645_ctrl_ops
,
1094 V4L2_CID_AUTOGAIN
, 0, 1, 1, 1);
1095 v4l2_ctrl_new_std(&ov5645
->ctrls
, &ov5645_ctrl_ops
,
1096 V4L2_CID_AUTO_WHITE_BALANCE
, 0, 1, 1, 1);
1097 v4l2_ctrl_new_std_menu(&ov5645
->ctrls
, &ov5645_ctrl_ops
,
1098 V4L2_CID_EXPOSURE_AUTO
, V4L2_EXPOSURE_MANUAL
,
1099 0, V4L2_EXPOSURE_AUTO
);
1100 v4l2_ctrl_new_std_menu_items(&ov5645
->ctrls
, &ov5645_ctrl_ops
,
1101 V4L2_CID_TEST_PATTERN
,
1102 ARRAY_SIZE(ov5645_test_pattern_menu
) - 1,
1103 0, 0, ov5645_test_pattern_menu
);
1104 ov5645
->pixel_clock
= v4l2_ctrl_new_std(&ov5645
->ctrls
,
1106 V4L2_CID_PIXEL_RATE
,
1108 ov5645
->link_freq
= v4l2_ctrl_new_int_menu(&ov5645
->ctrls
,
1111 ARRAY_SIZE(link_freq
) - 1,
1113 if (ov5645
->link_freq
)
1114 ov5645
->link_freq
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
1116 ov5645
->sd
.ctrl_handler
= &ov5645
->ctrls
;
1118 if (ov5645
->ctrls
.error
) {
1119 ret
= ov5645
->ctrls
.error
;
1120 dev_err_probe(dev
, ret
, "failed to add controls\n");
1124 v4l2_i2c_subdev_init(&ov5645
->sd
, client
, &ov5645_subdev_ops
);
1125 ov5645
->sd
.internal_ops
= &ov5645_internal_ops
;
1126 ov5645
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1127 ov5645
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1128 ov5645
->sd
.dev
= dev
;
1129 ov5645
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1131 ret
= media_entity_pads_init(&ov5645
->sd
.entity
, 1, &ov5645
->pad
);
1133 dev_err_probe(dev
, ret
, "could not register media entity\n");
1137 ret
= ov5645_set_power_on(dev
);
1141 ret
= ov5645_read_reg(ov5645
, OV5645_CHIP_ID_HIGH
, &chip_id_high
);
1142 if (ret
< 0 || chip_id_high
!= OV5645_CHIP_ID_HIGH_BYTE
) {
1144 dev_err_probe(dev
, ret
, "could not read ID high\n");
1147 ret
= ov5645_read_reg(ov5645
, OV5645_CHIP_ID_LOW
, &chip_id_low
);
1148 if (ret
< 0 || chip_id_low
!= OV5645_CHIP_ID_LOW_BYTE
) {
1150 dev_err_probe(dev
, ret
, "could not read ID low\n");
1154 dev_info(dev
, "OV5645 detected at address 0x%02x\n", client
->addr
);
1156 ret
= ov5645_read_reg(ov5645
, OV5645_AEC_PK_MANUAL
,
1157 &ov5645
->aec_pk_manual
);
1160 dev_err_probe(dev
, ret
, "could not read AEC/AGC mode\n");
1164 ret
= ov5645_read_reg(ov5645
, OV5645_TIMING_TC_REG20
,
1165 &ov5645
->timing_tc_reg20
);
1168 dev_err_probe(dev
, ret
, "could not read vflip value\n");
1172 ret
= ov5645_read_reg(ov5645
, OV5645_TIMING_TC_REG21
,
1173 &ov5645
->timing_tc_reg21
);
1176 dev_err_probe(dev
, ret
, "could not read hflip value\n");
1180 ov5645
->sd
.state_lock
= ov5645
->ctrls
.lock
;
1181 ret
= v4l2_subdev_init_finalize(&ov5645
->sd
);
1183 dev_err_probe(dev
, ret
, "subdev init error\n");
1187 pm_runtime_set_active(dev
);
1188 pm_runtime_get_noresume(dev
);
1189 pm_runtime_enable(dev
);
1191 ret
= v4l2_async_register_subdev_sensor(&ov5645
->sd
);
1193 dev_err_probe(dev
, ret
, "could not register v4l2 device\n");
1194 goto err_pm_runtime
;
1197 pm_runtime_set_autosuspend_delay(dev
, 1000);
1198 pm_runtime_use_autosuspend(dev
);
1199 pm_runtime_mark_last_busy(dev
);
1200 pm_runtime_put_autosuspend(dev
);
1205 pm_runtime_disable(dev
);
1206 pm_runtime_put_noidle(dev
);
1207 v4l2_subdev_cleanup(&ov5645
->sd
);
1209 ov5645_set_power_off(dev
);
1211 media_entity_cleanup(&ov5645
->sd
.entity
);
1213 v4l2_ctrl_handler_free(&ov5645
->ctrls
);
1218 static void ov5645_remove(struct i2c_client
*client
)
1220 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1221 struct ov5645
*ov5645
= to_ov5645(sd
);
1223 v4l2_async_unregister_subdev(&ov5645
->sd
);
1224 v4l2_subdev_cleanup(sd
);
1225 media_entity_cleanup(&ov5645
->sd
.entity
);
1226 v4l2_ctrl_handler_free(&ov5645
->ctrls
);
1227 pm_runtime_disable(ov5645
->dev
);
1228 if (!pm_runtime_status_suspended(ov5645
->dev
))
1229 ov5645_set_power_off(ov5645
->dev
);
1230 pm_runtime_set_suspended(ov5645
->dev
);
1233 static const struct i2c_device_id ov5645_id
[] = {
1237 MODULE_DEVICE_TABLE(i2c
, ov5645_id
);
1239 static const struct of_device_id ov5645_of_match
[] = {
1240 { .compatible
= "ovti,ov5645" },
1243 MODULE_DEVICE_TABLE(of
, ov5645_of_match
);
1245 static const struct dev_pm_ops ov5645_pm_ops
= {
1246 SET_RUNTIME_PM_OPS(ov5645_set_power_off
, ov5645_set_power_on
, NULL
)
1249 static struct i2c_driver ov5645_i2c_driver
= {
1251 .of_match_table
= ov5645_of_match
,
1253 .pm
= &ov5645_pm_ops
,
1255 .probe
= ov5645_probe
,
1256 .remove
= ov5645_remove
,
1257 .id_table
= ov5645_id
,
1260 module_i2c_driver(ov5645_i2c_driver
);
1262 MODULE_DESCRIPTION("Omnivision OV5645 Camera Driver");
1263 MODULE_AUTHOR("Todor Tomov <todor.tomov@linaro.org>");
1264 MODULE_LICENSE("GPL v2");