1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 Intel Corporation.
4 #include <asm/unaligned.h>
5 #include <linux/acpi.h>
7 #include <linux/delay.h>
8 #include <linux/gpio/consumer.h>
10 #include <linux/module.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/regulator/consumer.h>
13 #include <media/v4l2-ctrls.h>
14 #include <media/v4l2-device.h>
15 #include <media/v4l2-fwnode.h>
17 #define OV8856_REG_VALUE_08BIT 1
18 #define OV8856_REG_VALUE_16BIT 2
19 #define OV8856_REG_VALUE_24BIT 3
21 #define OV8856_LINK_FREQ_360MHZ 360000000ULL
22 #define OV8856_LINK_FREQ_180MHZ 180000000ULL
23 #define OV8856_SCLK 144000000ULL
24 #define OV8856_XVCLK_19_2 19200000
25 #define OV8856_DATA_LANES 4
26 #define OV8856_RGB_DEPTH 10
28 #define OV8856_REG_CHIP_ID 0x300a
29 #define OV8856_CHIP_ID 0x00885a
31 #define OV8856_REG_MODE_SELECT 0x0100
32 #define OV8856_MODE_STANDBY 0x00
33 #define OV8856_MODE_STREAMING 0x01
35 /* module revisions */
36 #define OV8856_2A_MODULE 0x01
37 #define OV8856_1B_MODULE 0x02
39 /* the OTP read-out buffer is at 0x7000 and 0xf is the offset
40 * of the byte in the OTP that means the module revision
42 #define OV8856_MODULE_REVISION 0x700f
43 #define OV8856_OTP_MODE_CTRL 0x3d84
44 #define OV8856_OTP_LOAD_CTRL 0x3d81
45 #define OV8856_OTP_MODE_AUTO 0x00
46 #define OV8856_OTP_LOAD_CTRL_ENABLE BIT(0)
48 /* vertical-timings from sensor */
49 #define OV8856_REG_VTS 0x380e
50 #define OV8856_VTS_MAX 0x7fff
52 /* horizontal-timings from sensor */
53 #define OV8856_REG_HTS 0x380c
55 /* Exposure controls from sensor */
56 #define OV8856_REG_EXPOSURE 0x3500
57 #define OV8856_EXPOSURE_MIN 6
58 #define OV8856_EXPOSURE_MAX_MARGIN 6
59 #define OV8856_EXPOSURE_STEP 1
61 /* Analog gain controls from sensor */
62 #define OV8856_REG_ANALOG_GAIN 0x3508
63 #define OV8856_ANAL_GAIN_MIN 128
64 #define OV8856_ANAL_GAIN_MAX 2047
65 #define OV8856_ANAL_GAIN_STEP 1
67 /* Digital gain controls from sensor */
68 #define OV8856_REG_MWB_R_GAIN 0x5019
69 #define OV8856_REG_MWB_G_GAIN 0x501b
70 #define OV8856_REG_MWB_B_GAIN 0x501d
71 #define OV8856_DGTL_GAIN_MIN 0
72 #define OV8856_DGTL_GAIN_MAX 4095
73 #define OV8856_DGTL_GAIN_STEP 1
74 #define OV8856_DGTL_GAIN_DEFAULT 1024
76 /* Test Pattern Control */
77 #define OV8856_REG_TEST_PATTERN 0x5e00
78 #define OV8856_TEST_PATTERN_ENABLE BIT(7)
79 #define OV8856_TEST_PATTERN_BAR_SHIFT 2
81 #define to_ov8856(_sd) container_of(_sd, struct ov8856, sd)
83 static const char * const ov8856_supply_names
[] = {
84 "dovdd", /* Digital I/O power */
85 "avdd", /* Analog power */
86 "dvdd", /* Digital core power */
90 OV8856_LINK_FREQ_720MBPS
,
91 OV8856_LINK_FREQ_360MBPS
,
99 struct ov8856_reg_list
{
101 const struct ov8856_reg
*regs
;
104 struct ov8856_link_freq_config
{
105 const struct ov8856_reg_list reg_list
;
109 /* Frame width in pixels */
112 /* Frame height in pixels */
115 /* Horizontal timining size */
118 /* Default vertical timining size */
121 /* Min vertical timining size */
124 /* Link frequency needed for this resolution */
127 /* Sensor register settings for this resolution */
128 const struct ov8856_reg_list reg_list
;
131 static const struct ov8856_reg mipi_data_rate_720mbps
[] = {
141 static const struct ov8856_reg mipi_data_rate_360mbps
[] = {
151 static const struct ov8856_reg mode_3280x2464_regs
[] = {
341 static const struct ov8856_reg mode_1640x1232_regs
[] = {
531 static const char * const ov8856_test_pattern_menu
[] = {
533 "Standard Color Bar",
534 "Top-Bottom Darker Color Bar",
535 "Right-Left Darker Color Bar",
536 "Bottom-Top Darker Color Bar"
539 static const s64 link_freq_menu_items
[] = {
540 OV8856_LINK_FREQ_360MHZ
,
541 OV8856_LINK_FREQ_180MHZ
544 static const struct ov8856_link_freq_config link_freq_configs
[] = {
545 [OV8856_LINK_FREQ_720MBPS
] = {
547 .num_of_regs
= ARRAY_SIZE(mipi_data_rate_720mbps
),
548 .regs
= mipi_data_rate_720mbps
,
551 [OV8856_LINK_FREQ_360MBPS
] = {
553 .num_of_regs
= ARRAY_SIZE(mipi_data_rate_360mbps
),
554 .regs
= mipi_data_rate_360mbps
,
559 static const struct ov8856_mode supported_modes
[] = {
567 .num_of_regs
= ARRAY_SIZE(mode_3280x2464_regs
),
568 .regs
= mode_3280x2464_regs
,
570 .link_freq_index
= OV8856_LINK_FREQ_720MBPS
,
579 .num_of_regs
= ARRAY_SIZE(mode_1640x1232_regs
),
580 .regs
= mode_1640x1232_regs
,
582 .link_freq_index
= OV8856_LINK_FREQ_360MBPS
,
587 struct v4l2_subdev sd
;
588 struct media_pad pad
;
589 struct v4l2_ctrl_handler ctrl_handler
;
592 struct gpio_desc
*reset_gpio
;
593 struct regulator_bulk_data supplies
[ARRAY_SIZE(ov8856_supply_names
)];
596 struct v4l2_ctrl
*link_freq
;
597 struct v4l2_ctrl
*pixel_rate
;
598 struct v4l2_ctrl
*vblank
;
599 struct v4l2_ctrl
*hblank
;
600 struct v4l2_ctrl
*exposure
;
603 const struct ov8856_mode
*cur_mode
;
605 /* To serialize asynchronus callbacks */
608 /* Streaming on/off */
612 static u64
to_pixel_rate(u32 f_index
)
614 u64 pixel_rate
= link_freq_menu_items
[f_index
] * 2 * OV8856_DATA_LANES
;
616 do_div(pixel_rate
, OV8856_RGB_DEPTH
);
621 static u64
to_pixels_per_line(u32 hts
, u32 f_index
)
623 u64 ppl
= hts
* to_pixel_rate(f_index
);
625 do_div(ppl
, OV8856_SCLK
);
630 static int ov8856_read_reg(struct ov8856
*ov8856
, u16 reg
, u16 len
, u32
*val
)
632 struct i2c_client
*client
= v4l2_get_subdevdata(&ov8856
->sd
);
633 struct i2c_msg msgs
[2];
635 u8 data_buf
[4] = {0};
641 put_unaligned_be16(reg
, addr_buf
);
642 msgs
[0].addr
= client
->addr
;
644 msgs
[0].len
= sizeof(addr_buf
);
645 msgs
[0].buf
= addr_buf
;
646 msgs
[1].addr
= client
->addr
;
647 msgs
[1].flags
= I2C_M_RD
;
649 msgs
[1].buf
= &data_buf
[4 - len
];
651 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
652 if (ret
!= ARRAY_SIZE(msgs
))
655 *val
= get_unaligned_be32(data_buf
);
660 static int ov8856_write_reg(struct ov8856
*ov8856
, u16 reg
, u16 len
, u32 val
)
662 struct i2c_client
*client
= v4l2_get_subdevdata(&ov8856
->sd
);
668 put_unaligned_be16(reg
, buf
);
669 put_unaligned_be32(val
<< 8 * (4 - len
), buf
+ 2);
670 if (i2c_master_send(client
, buf
, len
+ 2) != len
+ 2)
676 static int ov8856_write_reg_list(struct ov8856
*ov8856
,
677 const struct ov8856_reg_list
*r_list
)
679 struct i2c_client
*client
= v4l2_get_subdevdata(&ov8856
->sd
);
683 for (i
= 0; i
< r_list
->num_of_regs
; i
++) {
684 ret
= ov8856_write_reg(ov8856
, r_list
->regs
[i
].address
, 1,
685 r_list
->regs
[i
].val
);
687 dev_err_ratelimited(&client
->dev
,
688 "failed to write reg 0x%4.4x. error = %d",
689 r_list
->regs
[i
].address
, ret
);
697 static int ov8856_update_digital_gain(struct ov8856
*ov8856
, u32 d_gain
)
701 ret
= ov8856_write_reg(ov8856
, OV8856_REG_MWB_R_GAIN
,
702 OV8856_REG_VALUE_16BIT
, d_gain
);
706 ret
= ov8856_write_reg(ov8856
, OV8856_REG_MWB_G_GAIN
,
707 OV8856_REG_VALUE_16BIT
, d_gain
);
711 return ov8856_write_reg(ov8856
, OV8856_REG_MWB_B_GAIN
,
712 OV8856_REG_VALUE_16BIT
, d_gain
);
715 static int ov8856_test_pattern(struct ov8856
*ov8856
, u32 pattern
)
718 pattern
= (pattern
- 1) << OV8856_TEST_PATTERN_BAR_SHIFT
|
719 OV8856_TEST_PATTERN_ENABLE
;
721 return ov8856_write_reg(ov8856
, OV8856_REG_TEST_PATTERN
,
722 OV8856_REG_VALUE_08BIT
, pattern
);
725 static int ov8856_set_ctrl(struct v4l2_ctrl
*ctrl
)
727 struct ov8856
*ov8856
= container_of(ctrl
->handler
,
728 struct ov8856
, ctrl_handler
);
729 struct i2c_client
*client
= v4l2_get_subdevdata(&ov8856
->sd
);
733 /* Propagate change of current control to all related controls */
734 if (ctrl
->id
== V4L2_CID_VBLANK
) {
735 /* Update max exposure while meeting expected vblanking */
736 exposure_max
= ov8856
->cur_mode
->height
+ ctrl
->val
-
737 OV8856_EXPOSURE_MAX_MARGIN
;
738 __v4l2_ctrl_modify_range(ov8856
->exposure
,
739 ov8856
->exposure
->minimum
,
740 exposure_max
, ov8856
->exposure
->step
,
744 /* V4L2 controls values will be applied only when power is already up */
745 if (!pm_runtime_get_if_in_use(&client
->dev
))
749 case V4L2_CID_ANALOGUE_GAIN
:
750 ret
= ov8856_write_reg(ov8856
, OV8856_REG_ANALOG_GAIN
,
751 OV8856_REG_VALUE_16BIT
, ctrl
->val
);
754 case V4L2_CID_DIGITAL_GAIN
:
755 ret
= ov8856_update_digital_gain(ov8856
, ctrl
->val
);
758 case V4L2_CID_EXPOSURE
:
759 /* 4 least significant bits of expsoure are fractional part */
760 ret
= ov8856_write_reg(ov8856
, OV8856_REG_EXPOSURE
,
761 OV8856_REG_VALUE_24BIT
, ctrl
->val
<< 4);
764 case V4L2_CID_VBLANK
:
765 ret
= ov8856_write_reg(ov8856
, OV8856_REG_VTS
,
766 OV8856_REG_VALUE_16BIT
,
767 ov8856
->cur_mode
->height
+ ctrl
->val
);
770 case V4L2_CID_TEST_PATTERN
:
771 ret
= ov8856_test_pattern(ov8856
, ctrl
->val
);
779 pm_runtime_put(&client
->dev
);
784 static const struct v4l2_ctrl_ops ov8856_ctrl_ops
= {
785 .s_ctrl
= ov8856_set_ctrl
,
788 static int ov8856_init_controls(struct ov8856
*ov8856
)
790 struct v4l2_ctrl_handler
*ctrl_hdlr
;
791 s64 exposure_max
, h_blank
;
794 ctrl_hdlr
= &ov8856
->ctrl_handler
;
795 ret
= v4l2_ctrl_handler_init(ctrl_hdlr
, 8);
799 ctrl_hdlr
->lock
= &ov8856
->mutex
;
800 ov8856
->link_freq
= v4l2_ctrl_new_int_menu(ctrl_hdlr
, &ov8856_ctrl_ops
,
802 ARRAY_SIZE(link_freq_menu_items
) - 1,
803 0, link_freq_menu_items
);
804 if (ov8856
->link_freq
)
805 ov8856
->link_freq
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
807 ov8856
->pixel_rate
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov8856_ctrl_ops
,
808 V4L2_CID_PIXEL_RATE
, 0,
809 to_pixel_rate(OV8856_LINK_FREQ_720MBPS
),
811 to_pixel_rate(OV8856_LINK_FREQ_720MBPS
));
812 ov8856
->vblank
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov8856_ctrl_ops
,
814 ov8856
->cur_mode
->vts_min
- ov8856
->cur_mode
->height
,
815 OV8856_VTS_MAX
- ov8856
->cur_mode
->height
, 1,
816 ov8856
->cur_mode
->vts_def
- ov8856
->cur_mode
->height
);
817 h_blank
= to_pixels_per_line(ov8856
->cur_mode
->hts
,
818 ov8856
->cur_mode
->link_freq_index
) - ov8856
->cur_mode
->width
;
819 ov8856
->hblank
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov8856_ctrl_ops
,
820 V4L2_CID_HBLANK
, h_blank
, h_blank
, 1,
823 ov8856
->hblank
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
825 v4l2_ctrl_new_std(ctrl_hdlr
, &ov8856_ctrl_ops
, V4L2_CID_ANALOGUE_GAIN
,
826 OV8856_ANAL_GAIN_MIN
, OV8856_ANAL_GAIN_MAX
,
827 OV8856_ANAL_GAIN_STEP
, OV8856_ANAL_GAIN_MIN
);
828 v4l2_ctrl_new_std(ctrl_hdlr
, &ov8856_ctrl_ops
, V4L2_CID_DIGITAL_GAIN
,
829 OV8856_DGTL_GAIN_MIN
, OV8856_DGTL_GAIN_MAX
,
830 OV8856_DGTL_GAIN_STEP
, OV8856_DGTL_GAIN_DEFAULT
);
831 exposure_max
= ov8856
->cur_mode
->vts_def
- OV8856_EXPOSURE_MAX_MARGIN
;
832 ov8856
->exposure
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov8856_ctrl_ops
,
834 OV8856_EXPOSURE_MIN
, exposure_max
,
835 OV8856_EXPOSURE_STEP
,
837 v4l2_ctrl_new_std_menu_items(ctrl_hdlr
, &ov8856_ctrl_ops
,
838 V4L2_CID_TEST_PATTERN
,
839 ARRAY_SIZE(ov8856_test_pattern_menu
) - 1,
840 0, 0, ov8856_test_pattern_menu
);
841 if (ctrl_hdlr
->error
)
842 return ctrl_hdlr
->error
;
844 ov8856
->sd
.ctrl_handler
= ctrl_hdlr
;
849 static void ov8856_update_pad_format(const struct ov8856_mode
*mode
,
850 struct v4l2_mbus_framefmt
*fmt
)
852 fmt
->width
= mode
->width
;
853 fmt
->height
= mode
->height
;
854 fmt
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
855 fmt
->field
= V4L2_FIELD_NONE
;
858 static int ov8856_start_streaming(struct ov8856
*ov8856
)
860 struct i2c_client
*client
= v4l2_get_subdevdata(&ov8856
->sd
);
861 const struct ov8856_reg_list
*reg_list
;
862 int link_freq_index
, ret
;
864 link_freq_index
= ov8856
->cur_mode
->link_freq_index
;
865 reg_list
= &link_freq_configs
[link_freq_index
].reg_list
;
866 ret
= ov8856_write_reg_list(ov8856
, reg_list
);
868 dev_err(&client
->dev
, "failed to set plls");
872 reg_list
= &ov8856
->cur_mode
->reg_list
;
873 ret
= ov8856_write_reg_list(ov8856
, reg_list
);
875 dev_err(&client
->dev
, "failed to set mode");
879 ret
= __v4l2_ctrl_handler_setup(ov8856
->sd
.ctrl_handler
);
883 ret
= ov8856_write_reg(ov8856
, OV8856_REG_MODE_SELECT
,
884 OV8856_REG_VALUE_08BIT
, OV8856_MODE_STREAMING
);
886 dev_err(&client
->dev
, "failed to set stream");
893 static void ov8856_stop_streaming(struct ov8856
*ov8856
)
895 struct i2c_client
*client
= v4l2_get_subdevdata(&ov8856
->sd
);
897 if (ov8856_write_reg(ov8856
, OV8856_REG_MODE_SELECT
,
898 OV8856_REG_VALUE_08BIT
, OV8856_MODE_STANDBY
))
899 dev_err(&client
->dev
, "failed to set stream");
902 static int ov8856_set_stream(struct v4l2_subdev
*sd
, int enable
)
904 struct ov8856
*ov8856
= to_ov8856(sd
);
905 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
908 if (ov8856
->streaming
== enable
)
911 mutex_lock(&ov8856
->mutex
);
913 ret
= pm_runtime_get_sync(&client
->dev
);
915 pm_runtime_put_noidle(&client
->dev
);
916 mutex_unlock(&ov8856
->mutex
);
920 ret
= ov8856_start_streaming(ov8856
);
923 ov8856_stop_streaming(ov8856
);
924 pm_runtime_put(&client
->dev
);
927 ov8856_stop_streaming(ov8856
);
928 pm_runtime_put(&client
->dev
);
931 ov8856
->streaming
= enable
;
932 mutex_unlock(&ov8856
->mutex
);
937 static int __ov8856_power_on(struct ov8856
*ov8856
)
939 struct i2c_client
*client
= v4l2_get_subdevdata(&ov8856
->sd
);
942 if (is_acpi_node(dev_fwnode(&client
->dev
)))
945 ret
= clk_prepare_enable(ov8856
->xvclk
);
947 dev_err(&client
->dev
, "failed to enable xvclk\n");
951 if (ov8856
->reset_gpio
) {
952 gpiod_set_value_cansleep(ov8856
->reset_gpio
, 1);
953 usleep_range(1000, 2000);
956 ret
= regulator_bulk_enable(ARRAY_SIZE(ov8856_supply_names
),
959 dev_err(&client
->dev
, "failed to enable regulators\n");
963 gpiod_set_value_cansleep(ov8856
->reset_gpio
, 0);
964 usleep_range(1500, 1800);
969 gpiod_set_value_cansleep(ov8856
->reset_gpio
, 1);
970 clk_disable_unprepare(ov8856
->xvclk
);
975 static void __ov8856_power_off(struct ov8856
*ov8856
)
977 struct i2c_client
*client
= v4l2_get_subdevdata(&ov8856
->sd
);
979 if (is_acpi_node(dev_fwnode(&client
->dev
)))
982 gpiod_set_value_cansleep(ov8856
->reset_gpio
, 1);
983 regulator_bulk_disable(ARRAY_SIZE(ov8856_supply_names
),
985 clk_disable_unprepare(ov8856
->xvclk
);
988 static int __maybe_unused
ov8856_suspend(struct device
*dev
)
990 struct i2c_client
*client
= to_i2c_client(dev
);
991 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
992 struct ov8856
*ov8856
= to_ov8856(sd
);
994 mutex_lock(&ov8856
->mutex
);
995 if (ov8856
->streaming
)
996 ov8856_stop_streaming(ov8856
);
998 __ov8856_power_off(ov8856
);
999 mutex_unlock(&ov8856
->mutex
);
1004 static int __maybe_unused
ov8856_resume(struct device
*dev
)
1006 struct i2c_client
*client
= to_i2c_client(dev
);
1007 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1008 struct ov8856
*ov8856
= to_ov8856(sd
);
1011 mutex_lock(&ov8856
->mutex
);
1013 __ov8856_power_on(ov8856
);
1014 if (ov8856
->streaming
) {
1015 ret
= ov8856_start_streaming(ov8856
);
1017 ov8856
->streaming
= false;
1018 ov8856_stop_streaming(ov8856
);
1019 mutex_unlock(&ov8856
->mutex
);
1024 mutex_unlock(&ov8856
->mutex
);
1029 static int ov8856_set_format(struct v4l2_subdev
*sd
,
1030 struct v4l2_subdev_pad_config
*cfg
,
1031 struct v4l2_subdev_format
*fmt
)
1033 struct ov8856
*ov8856
= to_ov8856(sd
);
1034 const struct ov8856_mode
*mode
;
1035 s32 vblank_def
, h_blank
;
1037 mode
= v4l2_find_nearest_size(supported_modes
,
1038 ARRAY_SIZE(supported_modes
), width
,
1039 height
, fmt
->format
.width
,
1040 fmt
->format
.height
);
1042 mutex_lock(&ov8856
->mutex
);
1043 ov8856_update_pad_format(mode
, &fmt
->format
);
1044 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1045 *v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
) = fmt
->format
;
1047 ov8856
->cur_mode
= mode
;
1048 __v4l2_ctrl_s_ctrl(ov8856
->link_freq
, mode
->link_freq_index
);
1049 __v4l2_ctrl_s_ctrl_int64(ov8856
->pixel_rate
,
1050 to_pixel_rate(mode
->link_freq_index
));
1052 /* Update limits and set FPS to default */
1053 vblank_def
= mode
->vts_def
- mode
->height
;
1054 __v4l2_ctrl_modify_range(ov8856
->vblank
,
1055 mode
->vts_min
- mode
->height
,
1056 OV8856_VTS_MAX
- mode
->height
, 1,
1058 __v4l2_ctrl_s_ctrl(ov8856
->vblank
, vblank_def
);
1059 h_blank
= to_pixels_per_line(mode
->hts
, mode
->link_freq_index
) -
1061 __v4l2_ctrl_modify_range(ov8856
->hblank
, h_blank
, h_blank
, 1,
1065 mutex_unlock(&ov8856
->mutex
);
1070 static int ov8856_get_format(struct v4l2_subdev
*sd
,
1071 struct v4l2_subdev_pad_config
*cfg
,
1072 struct v4l2_subdev_format
*fmt
)
1074 struct ov8856
*ov8856
= to_ov8856(sd
);
1076 mutex_lock(&ov8856
->mutex
);
1077 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
)
1078 fmt
->format
= *v4l2_subdev_get_try_format(&ov8856
->sd
, cfg
,
1081 ov8856_update_pad_format(ov8856
->cur_mode
, &fmt
->format
);
1083 mutex_unlock(&ov8856
->mutex
);
1088 static int ov8856_enum_mbus_code(struct v4l2_subdev
*sd
,
1089 struct v4l2_subdev_pad_config
*cfg
,
1090 struct v4l2_subdev_mbus_code_enum
*code
)
1092 /* Only one bayer order GRBG is supported */
1093 if (code
->index
> 0)
1096 code
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
1101 static int ov8856_enum_frame_size(struct v4l2_subdev
*sd
,
1102 struct v4l2_subdev_pad_config
*cfg
,
1103 struct v4l2_subdev_frame_size_enum
*fse
)
1105 if (fse
->index
>= ARRAY_SIZE(supported_modes
))
1108 if (fse
->code
!= MEDIA_BUS_FMT_SGRBG10_1X10
)
1111 fse
->min_width
= supported_modes
[fse
->index
].width
;
1112 fse
->max_width
= fse
->min_width
;
1113 fse
->min_height
= supported_modes
[fse
->index
].height
;
1114 fse
->max_height
= fse
->min_height
;
1119 static int ov8856_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
1121 struct ov8856
*ov8856
= to_ov8856(sd
);
1123 mutex_lock(&ov8856
->mutex
);
1124 ov8856_update_pad_format(&supported_modes
[0],
1125 v4l2_subdev_get_try_format(sd
, fh
->pad
, 0));
1126 mutex_unlock(&ov8856
->mutex
);
1131 static const struct v4l2_subdev_video_ops ov8856_video_ops
= {
1132 .s_stream
= ov8856_set_stream
,
1135 static const struct v4l2_subdev_pad_ops ov8856_pad_ops
= {
1136 .set_fmt
= ov8856_set_format
,
1137 .get_fmt
= ov8856_get_format
,
1138 .enum_mbus_code
= ov8856_enum_mbus_code
,
1139 .enum_frame_size
= ov8856_enum_frame_size
,
1142 static const struct v4l2_subdev_ops ov8856_subdev_ops
= {
1143 .video
= &ov8856_video_ops
,
1144 .pad
= &ov8856_pad_ops
,
1147 static const struct media_entity_operations ov8856_subdev_entity_ops
= {
1148 .link_validate
= v4l2_subdev_link_validate
,
1151 static const struct v4l2_subdev_internal_ops ov8856_internal_ops
= {
1152 .open
= ov8856_open
,
1155 static int ov8856_identify_module(struct ov8856
*ov8856
)
1157 struct i2c_client
*client
= v4l2_get_subdevdata(&ov8856
->sd
);
1161 ret
= ov8856_read_reg(ov8856
, OV8856_REG_CHIP_ID
,
1162 OV8856_REG_VALUE_24BIT
, &val
);
1166 if (val
!= OV8856_CHIP_ID
) {
1167 dev_err(&client
->dev
, "chip id mismatch: %x!=%x",
1168 OV8856_CHIP_ID
, val
);
1172 ret
= ov8856_write_reg(ov8856
, OV8856_REG_MODE_SELECT
,
1173 OV8856_REG_VALUE_08BIT
, OV8856_MODE_STREAMING
);
1177 ret
= ov8856_write_reg(ov8856
, OV8856_OTP_MODE_CTRL
,
1178 OV8856_REG_VALUE_08BIT
, OV8856_OTP_MODE_AUTO
);
1180 dev_err(&client
->dev
, "failed to set otp mode");
1184 ret
= ov8856_write_reg(ov8856
, OV8856_OTP_LOAD_CTRL
,
1185 OV8856_REG_VALUE_08BIT
,
1186 OV8856_OTP_LOAD_CTRL_ENABLE
);
1188 dev_err(&client
->dev
, "failed to enable load control");
1192 ret
= ov8856_read_reg(ov8856
, OV8856_MODULE_REVISION
,
1193 OV8856_REG_VALUE_08BIT
, &val
);
1195 dev_err(&client
->dev
, "failed to read module revision");
1199 dev_info(&client
->dev
, "OV8856 revision %x (%s) at address 0x%02x\n",
1201 val
== OV8856_2A_MODULE
? "2A" :
1202 val
== OV8856_1B_MODULE
? "1B" : "unknown revision",
1205 ret
= ov8856_write_reg(ov8856
, OV8856_REG_MODE_SELECT
,
1206 OV8856_REG_VALUE_08BIT
, OV8856_MODE_STANDBY
);
1208 dev_err(&client
->dev
, "failed to exit streaming mode");
1215 static int ov8856_get_hwcfg(struct ov8856
*ov8856
, struct device
*dev
)
1217 struct fwnode_handle
*ep
;
1218 struct fwnode_handle
*fwnode
= dev_fwnode(dev
);
1219 struct v4l2_fwnode_endpoint bus_cfg
= {
1220 .bus_type
= V4L2_MBUS_CSI2_DPHY
1229 ret
= fwnode_property_read_u32(fwnode
, "clock-frequency", &xvclk_rate
);
1233 if (!is_acpi_node(fwnode
)) {
1234 ov8856
->xvclk
= devm_clk_get(dev
, "xvclk");
1235 if (IS_ERR(ov8856
->xvclk
)) {
1236 dev_err(dev
, "could not get xvclk clock (%pe)\n",
1238 return PTR_ERR(ov8856
->xvclk
);
1241 clk_set_rate(ov8856
->xvclk
, xvclk_rate
);
1242 xvclk_rate
= clk_get_rate(ov8856
->xvclk
);
1245 if (xvclk_rate
!= OV8856_XVCLK_19_2
)
1246 dev_warn(dev
, "external clock rate %u is unsupported",
1249 ov8856
->reset_gpio
= devm_gpiod_get_optional(dev
, "reset",
1251 if (IS_ERR(ov8856
->reset_gpio
))
1252 return PTR_ERR(ov8856
->reset_gpio
);
1254 for (i
= 0; i
< ARRAY_SIZE(ov8856_supply_names
); i
++)
1255 ov8856
->supplies
[i
].supply
= ov8856_supply_names
[i
];
1257 ret
= devm_regulator_bulk_get(dev
, ARRAY_SIZE(ov8856_supply_names
),
1262 ep
= fwnode_graph_get_next_endpoint(fwnode
, NULL
);
1266 ret
= v4l2_fwnode_endpoint_alloc_parse(ep
, &bus_cfg
);
1267 fwnode_handle_put(ep
);
1271 if (bus_cfg
.bus
.mipi_csi2
.num_data_lanes
!= OV8856_DATA_LANES
) {
1272 dev_err(dev
, "number of CSI2 data lanes %d is not supported",
1273 bus_cfg
.bus
.mipi_csi2
.num_data_lanes
);
1275 goto check_hwcfg_error
;
1278 if (!bus_cfg
.nr_of_link_frequencies
) {
1279 dev_err(dev
, "no link frequencies defined");
1281 goto check_hwcfg_error
;
1284 for (i
= 0; i
< ARRAY_SIZE(link_freq_menu_items
); i
++) {
1285 for (j
= 0; j
< bus_cfg
.nr_of_link_frequencies
; j
++) {
1286 if (link_freq_menu_items
[i
] ==
1287 bus_cfg
.link_frequencies
[j
])
1291 if (j
== bus_cfg
.nr_of_link_frequencies
) {
1292 dev_err(dev
, "no link frequency %lld supported",
1293 link_freq_menu_items
[i
]);
1295 goto check_hwcfg_error
;
1300 v4l2_fwnode_endpoint_free(&bus_cfg
);
1305 static int ov8856_remove(struct i2c_client
*client
)
1307 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1308 struct ov8856
*ov8856
= to_ov8856(sd
);
1310 v4l2_async_unregister_subdev(sd
);
1311 media_entity_cleanup(&sd
->entity
);
1312 v4l2_ctrl_handler_free(sd
->ctrl_handler
);
1313 pm_runtime_disable(&client
->dev
);
1314 mutex_destroy(&ov8856
->mutex
);
1316 __ov8856_power_off(ov8856
);
1321 static int ov8856_probe(struct i2c_client
*client
)
1323 struct ov8856
*ov8856
;
1326 ov8856
= devm_kzalloc(&client
->dev
, sizeof(*ov8856
), GFP_KERNEL
);
1330 ret
= ov8856_get_hwcfg(ov8856
, &client
->dev
);
1332 dev_err(&client
->dev
, "failed to get HW configuration: %d",
1337 v4l2_i2c_subdev_init(&ov8856
->sd
, client
, &ov8856_subdev_ops
);
1339 ret
= __ov8856_power_on(ov8856
);
1341 dev_err(&client
->dev
, "failed to power on\n");
1345 ret
= ov8856_identify_module(ov8856
);
1347 dev_err(&client
->dev
, "failed to find sensor: %d", ret
);
1348 goto probe_power_off
;
1351 mutex_init(&ov8856
->mutex
);
1352 ov8856
->cur_mode
= &supported_modes
[0];
1353 ret
= ov8856_init_controls(ov8856
);
1355 dev_err(&client
->dev
, "failed to init controls: %d", ret
);
1356 goto probe_error_v4l2_ctrl_handler_free
;
1359 ov8856
->sd
.internal_ops
= &ov8856_internal_ops
;
1360 ov8856
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1361 ov8856
->sd
.entity
.ops
= &ov8856_subdev_entity_ops
;
1362 ov8856
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1363 ov8856
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1364 ret
= media_entity_pads_init(&ov8856
->sd
.entity
, 1, &ov8856
->pad
);
1366 dev_err(&client
->dev
, "failed to init entity pads: %d", ret
);
1367 goto probe_error_v4l2_ctrl_handler_free
;
1370 ret
= v4l2_async_register_subdev_sensor_common(&ov8856
->sd
);
1372 dev_err(&client
->dev
, "failed to register V4L2 subdev: %d",
1374 goto probe_error_media_entity_cleanup
;
1378 * Device is already turned on by i2c-core with ACPI domain PM.
1379 * Enable runtime PM and turn off the device.
1381 pm_runtime_set_active(&client
->dev
);
1382 pm_runtime_enable(&client
->dev
);
1383 pm_runtime_idle(&client
->dev
);
1387 probe_error_media_entity_cleanup
:
1388 media_entity_cleanup(&ov8856
->sd
.entity
);
1390 probe_error_v4l2_ctrl_handler_free
:
1391 v4l2_ctrl_handler_free(ov8856
->sd
.ctrl_handler
);
1392 mutex_destroy(&ov8856
->mutex
);
1395 __ov8856_power_off(ov8856
);
1400 static const struct dev_pm_ops ov8856_pm_ops
= {
1401 SET_SYSTEM_SLEEP_PM_OPS(ov8856_suspend
, ov8856_resume
)
1405 static const struct acpi_device_id ov8856_acpi_ids
[] = {
1410 MODULE_DEVICE_TABLE(acpi
, ov8856_acpi_ids
);
1413 static const struct of_device_id ov8856_of_match
[] = {
1414 { .compatible
= "ovti,ov8856" },
1417 MODULE_DEVICE_TABLE(of
, ov8856_of_match
);
1419 static struct i2c_driver ov8856_i2c_driver
= {
1422 .pm
= &ov8856_pm_ops
,
1423 .acpi_match_table
= ACPI_PTR(ov8856_acpi_ids
),
1424 .of_match_table
= ov8856_of_match
,
1426 .probe_new
= ov8856_probe
,
1427 .remove
= ov8856_remove
,
1430 module_i2c_driver(ov8856_i2c_driver
);
1432 MODULE_AUTHOR("Ben Kao <ben.kao@intel.com>");
1433 MODULE_DESCRIPTION("OmniVision OV8856 sensor driver");
1434 MODULE_LICENSE("GPL v2");