1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2020 Intel Corporation.
4 #include <linux/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/nvmem-provider.h>
13 #include <linux/regmap.h>
14 #include <media/v4l2-ctrls.h>
15 #include <media/v4l2-device.h>
16 #include <media/v4l2-fwnode.h>
18 #define OV2740_LINK_FREQ_360MHZ 360000000ULL
19 #define OV2740_LINK_FREQ_180MHZ 180000000ULL
20 #define OV2740_SCLK 72000000LL
21 #define OV2740_MCLK 19200000
22 #define OV2740_DATA_LANES 2
23 #define OV2740_RGB_DEPTH 10
25 #define OV2740_REG_CHIP_ID 0x300a
26 #define OV2740_CHIP_ID 0x2740
28 #define OV2740_REG_MODE_SELECT 0x0100
29 #define OV2740_MODE_STANDBY 0x00
30 #define OV2740_MODE_STREAMING 0x01
32 /* vertical-timings from sensor */
33 #define OV2740_REG_VTS 0x380e
35 /* horizontal-timings from sensor */
36 #define OV2740_REG_HTS 0x380c
38 /* Exposure controls from sensor */
39 #define OV2740_REG_EXPOSURE 0x3500
40 #define OV2740_EXPOSURE_MIN 4
41 #define OV2740_EXPOSURE_MAX_MARGIN 8
42 #define OV2740_EXPOSURE_STEP 1
44 /* Analog gain controls from sensor */
45 #define OV2740_REG_ANALOG_GAIN 0x3508
46 #define OV2740_ANAL_GAIN_MIN 128
47 #define OV2740_ANAL_GAIN_MAX 1983
48 #define OV2740_ANAL_GAIN_STEP 1
50 /* Digital gain controls from sensor */
51 #define OV2740_REG_MWB_R_GAIN 0x500a
52 #define OV2740_REG_MWB_G_GAIN 0x500c
53 #define OV2740_REG_MWB_B_GAIN 0x500e
54 #define OV2740_DGTL_GAIN_MIN 1024
55 #define OV2740_DGTL_GAIN_MAX 4095
56 #define OV2740_DGTL_GAIN_STEP 1
57 #define OV2740_DGTL_GAIN_DEFAULT 1024
59 /* Test Pattern Control */
60 #define OV2740_REG_TEST_PATTERN 0x5040
61 #define OV2740_TEST_PATTERN_ENABLE BIT(7)
62 #define OV2740_TEST_PATTERN_BAR_SHIFT 2
65 #define OV2740_REG_GROUP_ACCESS 0x3208
66 #define OV2740_GROUP_HOLD_START 0x0
67 #define OV2740_GROUP_HOLD_END 0x10
68 #define OV2740_GROUP_HOLD_LAUNCH 0xa0
71 #define OV2740_REG_ISP_CTRL00 0x5000
73 #define OV2740_REG_ISP_CTRL01 0x5001
74 /* Customer Addresses: 0x7010 - 0x710F */
75 #define CUSTOMER_USE_OTP_SIZE 0x100
76 /* OTP registers from sensor */
77 #define OV2740_REG_OTP_CUSTOMER 0x7010
80 struct nvmem_device
*nvmem
;
81 struct regmap
*regmap
;
86 OV2740_LINK_FREQ_360MHZ_INDEX
,
87 OV2740_LINK_FREQ_180MHZ_INDEX
,
95 struct ov2740_reg_list
{
97 const struct ov2740_reg
*regs
;
100 struct ov2740_link_freq_config
{
101 const struct ov2740_reg_list reg_list
;
105 /* Frame width in pixels */
108 /* Frame height in pixels */
111 /* Horizontal timining size */
114 /* Default vertical timining size */
117 /* Min vertical timining size */
120 /* Max vertical timining size */
123 /* Link frequency needed for this resolution */
126 /* Sensor register settings for this resolution */
127 const struct ov2740_reg_list reg_list
;
130 static const struct ov2740_reg mipi_data_rate_720mbps
[] = {
138 static const struct ov2740_reg mipi_data_rate_360mbps
[] = {
148 static const struct ov2740_reg mode_1932x1092_regs_360mhz
[] = {
301 static const struct ov2740_reg mode_1932x1092_regs_180mhz
[] = {
303 {0x3018, 0x32}, /* 0x32 for 2 lanes, 0x12 for 1 lane */
429 {0x5000, 0x73}, /* 0x7f enable DPC */
451 {0x4003, 0x40}, /* set Black level to 0x40 */
454 static const char * const ov2740_test_pattern_menu
[] = {
457 "Top-Bottom Darker Color Bar",
458 "Right-Left Darker Color Bar",
459 "Bottom-Top Darker Color Bar",
462 static const s64 link_freq_menu_items
[] = {
463 OV2740_LINK_FREQ_360MHZ
,
464 OV2740_LINK_FREQ_180MHZ
,
467 static const struct ov2740_link_freq_config link_freq_configs
[] = {
468 [OV2740_LINK_FREQ_360MHZ_INDEX
] = {
470 .num_of_regs
= ARRAY_SIZE(mipi_data_rate_720mbps
),
471 .regs
= mipi_data_rate_720mbps
,
474 [OV2740_LINK_FREQ_180MHZ_INDEX
] = {
476 .num_of_regs
= ARRAY_SIZE(mipi_data_rate_360mbps
),
477 .regs
= mipi_data_rate_360mbps
,
482 static const struct ov2740_mode supported_modes_360mhz
[] = {
491 .num_of_regs
= ARRAY_SIZE(mode_1932x1092_regs_360mhz
),
492 .regs
= mode_1932x1092_regs_360mhz
,
494 .link_freq_index
= OV2740_LINK_FREQ_360MHZ_INDEX
,
498 static const struct ov2740_mode supported_modes_180mhz
[] = {
507 .num_of_regs
= ARRAY_SIZE(mode_1932x1092_regs_180mhz
),
508 .regs
= mode_1932x1092_regs_180mhz
,
510 .link_freq_index
= OV2740_LINK_FREQ_180MHZ_INDEX
,
515 struct v4l2_subdev sd
;
516 struct media_pad pad
;
517 struct v4l2_ctrl_handler ctrl_handler
;
520 struct v4l2_ctrl
*link_freq
;
521 struct v4l2_ctrl
*pixel_rate
;
522 struct v4l2_ctrl
*vblank
;
523 struct v4l2_ctrl
*hblank
;
524 struct v4l2_ctrl
*exposure
;
527 struct gpio_desc
*reset_gpio
;
531 const struct ov2740_mode
*cur_mode
;
533 /* NVM data information */
534 struct nvm_data
*nvm
;
536 /* Supported modes */
537 const struct ov2740_mode
*supported_modes
;
538 int supported_modes_count
;
540 /* True if the device has been identified */
544 static inline struct ov2740
*to_ov2740(struct v4l2_subdev
*subdev
)
546 return container_of(subdev
, struct ov2740
, sd
);
549 static u64
to_pixel_rate(u32 f_index
)
551 u64 pixel_rate
= link_freq_menu_items
[f_index
] * 2 * OV2740_DATA_LANES
;
553 do_div(pixel_rate
, OV2740_RGB_DEPTH
);
558 static int ov2740_read_reg(struct ov2740
*ov2740
, u16 reg
, u16 len
, u32
*val
)
560 struct i2c_client
*client
= v4l2_get_subdevdata(&ov2740
->sd
);
561 struct i2c_msg msgs
[2];
563 u8 data_buf
[4] = {0};
566 if (len
> sizeof(data_buf
))
569 put_unaligned_be16(reg
, addr_buf
);
570 msgs
[0].addr
= client
->addr
;
572 msgs
[0].len
= sizeof(addr_buf
);
573 msgs
[0].buf
= addr_buf
;
574 msgs
[1].addr
= client
->addr
;
575 msgs
[1].flags
= I2C_M_RD
;
577 msgs
[1].buf
= &data_buf
[sizeof(data_buf
) - len
];
579 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
580 if (ret
!= ARRAY_SIZE(msgs
))
581 return ret
< 0 ? ret
: -EIO
;
583 *val
= get_unaligned_be32(data_buf
);
588 static int ov2740_write_reg(struct ov2740
*ov2740
, u16 reg
, u16 len
, u32 val
)
590 struct i2c_client
*client
= v4l2_get_subdevdata(&ov2740
->sd
);
597 put_unaligned_be16(reg
, buf
);
598 put_unaligned_be32(val
<< 8 * (4 - len
), buf
+ 2);
600 ret
= i2c_master_send(client
, buf
, len
+ 2);
602 return ret
< 0 ? ret
: -EIO
;
607 static int ov2740_write_reg_list(struct ov2740
*ov2740
,
608 const struct ov2740_reg_list
*r_list
)
610 struct i2c_client
*client
= v4l2_get_subdevdata(&ov2740
->sd
);
614 for (i
= 0; i
< r_list
->num_of_regs
; i
++) {
615 ret
= ov2740_write_reg(ov2740
, r_list
->regs
[i
].address
, 1,
616 r_list
->regs
[i
].val
);
618 dev_err_ratelimited(&client
->dev
,
619 "write reg 0x%4.4x return err = %d\n",
620 r_list
->regs
[i
].address
, ret
);
628 static int ov2740_identify_module(struct ov2740
*ov2740
)
630 struct i2c_client
*client
= v4l2_get_subdevdata(&ov2740
->sd
);
634 if (ov2740
->identified
)
637 ret
= ov2740_read_reg(ov2740
, OV2740_REG_CHIP_ID
, 3, &val
);
641 if (val
!= OV2740_CHIP_ID
) {
642 dev_err(&client
->dev
, "chip id mismatch: %x != %x\n",
643 OV2740_CHIP_ID
, val
);
647 ov2740
->identified
= true;
652 static int ov2740_update_digital_gain(struct ov2740
*ov2740
, u32 d_gain
)
656 ret
= ov2740_write_reg(ov2740
, OV2740_REG_GROUP_ACCESS
, 1,
657 OV2740_GROUP_HOLD_START
);
661 ret
= ov2740_write_reg(ov2740
, OV2740_REG_MWB_R_GAIN
, 2, d_gain
);
665 ret
= ov2740_write_reg(ov2740
, OV2740_REG_MWB_G_GAIN
, 2, d_gain
);
669 ret
= ov2740_write_reg(ov2740
, OV2740_REG_MWB_B_GAIN
, 2, d_gain
);
673 ret
= ov2740_write_reg(ov2740
, OV2740_REG_GROUP_ACCESS
, 1,
674 OV2740_GROUP_HOLD_END
);
678 ret
= ov2740_write_reg(ov2740
, OV2740_REG_GROUP_ACCESS
, 1,
679 OV2740_GROUP_HOLD_LAUNCH
);
683 static int ov2740_test_pattern(struct ov2740
*ov2740
, u32 pattern
)
686 pattern
= (pattern
- 1) << OV2740_TEST_PATTERN_BAR_SHIFT
|
687 OV2740_TEST_PATTERN_ENABLE
;
689 return ov2740_write_reg(ov2740
, OV2740_REG_TEST_PATTERN
, 1, pattern
);
692 static int ov2740_set_ctrl(struct v4l2_ctrl
*ctrl
)
694 struct ov2740
*ov2740
= container_of(ctrl
->handler
,
695 struct ov2740
, ctrl_handler
);
696 struct i2c_client
*client
= v4l2_get_subdevdata(&ov2740
->sd
);
700 /* Propagate change of current control to all related controls */
701 if (ctrl
->id
== V4L2_CID_VBLANK
) {
702 /* Update max exposure while meeting expected vblanking */
703 exposure_max
= ov2740
->cur_mode
->height
+ ctrl
->val
-
704 OV2740_EXPOSURE_MAX_MARGIN
;
705 __v4l2_ctrl_modify_range(ov2740
->exposure
,
706 ov2740
->exposure
->minimum
,
707 exposure_max
, ov2740
->exposure
->step
,
711 /* V4L2 controls values will be applied only when power is already up */
712 if (!pm_runtime_get_if_in_use(&client
->dev
))
716 case V4L2_CID_ANALOGUE_GAIN
:
717 ret
= ov2740_write_reg(ov2740
, OV2740_REG_ANALOG_GAIN
, 2,
721 case V4L2_CID_DIGITAL_GAIN
:
722 ret
= ov2740_update_digital_gain(ov2740
, ctrl
->val
);
725 case V4L2_CID_EXPOSURE
:
726 /* 4 least significant bits of expsoure are fractional part */
727 ret
= ov2740_write_reg(ov2740
, OV2740_REG_EXPOSURE
, 3,
731 case V4L2_CID_VBLANK
:
732 ret
= ov2740_write_reg(ov2740
, OV2740_REG_VTS
, 2,
733 ov2740
->cur_mode
->height
+ ctrl
->val
);
736 case V4L2_CID_TEST_PATTERN
:
737 ret
= ov2740_test_pattern(ov2740
, ctrl
->val
);
745 pm_runtime_put(&client
->dev
);
750 static const struct v4l2_ctrl_ops ov2740_ctrl_ops
= {
751 .s_ctrl
= ov2740_set_ctrl
,
754 static int ov2740_init_controls(struct ov2740
*ov2740
)
756 struct v4l2_ctrl_handler
*ctrl_hdlr
;
757 const struct ov2740_mode
*cur_mode
;
758 s64 exposure_max
, h_blank
, pixel_rate
;
759 u32 vblank_min
, vblank_max
, vblank_default
;
763 ctrl_hdlr
= &ov2740
->ctrl_handler
;
764 ret
= v4l2_ctrl_handler_init(ctrl_hdlr
, 8);
768 cur_mode
= ov2740
->cur_mode
;
769 size
= ARRAY_SIZE(link_freq_menu_items
);
772 v4l2_ctrl_new_int_menu(ctrl_hdlr
, &ov2740_ctrl_ops
,
773 V4L2_CID_LINK_FREQ
, size
- 1,
774 ov2740
->supported_modes
->link_freq_index
,
775 link_freq_menu_items
);
776 if (ov2740
->link_freq
)
777 ov2740
->link_freq
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
779 pixel_rate
= to_pixel_rate(ov2740
->supported_modes
->link_freq_index
);
780 ov2740
->pixel_rate
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov2740_ctrl_ops
,
781 V4L2_CID_PIXEL_RATE
, 0,
782 pixel_rate
, 1, pixel_rate
);
784 vblank_min
= cur_mode
->vts_min
- cur_mode
->height
;
785 vblank_max
= cur_mode
->vts_max
- cur_mode
->height
;
786 vblank_default
= cur_mode
->vts_def
- cur_mode
->height
;
787 ov2740
->vblank
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov2740_ctrl_ops
,
788 V4L2_CID_VBLANK
, vblank_min
,
789 vblank_max
, 1, vblank_default
);
791 h_blank
= cur_mode
->hts
- cur_mode
->width
;
792 ov2740
->hblank
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov2740_ctrl_ops
,
793 V4L2_CID_HBLANK
, h_blank
, h_blank
, 1,
796 ov2740
->hblank
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
798 v4l2_ctrl_new_std(ctrl_hdlr
, &ov2740_ctrl_ops
, V4L2_CID_ANALOGUE_GAIN
,
799 OV2740_ANAL_GAIN_MIN
, OV2740_ANAL_GAIN_MAX
,
800 OV2740_ANAL_GAIN_STEP
, OV2740_ANAL_GAIN_MIN
);
801 v4l2_ctrl_new_std(ctrl_hdlr
, &ov2740_ctrl_ops
, V4L2_CID_DIGITAL_GAIN
,
802 OV2740_DGTL_GAIN_MIN
, OV2740_DGTL_GAIN_MAX
,
803 OV2740_DGTL_GAIN_STEP
, OV2740_DGTL_GAIN_DEFAULT
);
804 exposure_max
= cur_mode
->vts_def
- OV2740_EXPOSURE_MAX_MARGIN
;
805 ov2740
->exposure
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov2740_ctrl_ops
,
807 OV2740_EXPOSURE_MIN
, exposure_max
,
808 OV2740_EXPOSURE_STEP
,
810 v4l2_ctrl_new_std_menu_items(ctrl_hdlr
, &ov2740_ctrl_ops
,
811 V4L2_CID_TEST_PATTERN
,
812 ARRAY_SIZE(ov2740_test_pattern_menu
) - 1,
813 0, 0, ov2740_test_pattern_menu
);
814 if (ctrl_hdlr
->error
) {
815 v4l2_ctrl_handler_free(ctrl_hdlr
);
816 return ctrl_hdlr
->error
;
819 ov2740
->sd
.ctrl_handler
= ctrl_hdlr
;
824 static void ov2740_update_pad_format(const struct ov2740_mode
*mode
,
825 struct v4l2_mbus_framefmt
*fmt
)
827 fmt
->width
= mode
->width
;
828 fmt
->height
= mode
->height
;
829 fmt
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
830 fmt
->field
= V4L2_FIELD_NONE
;
833 static int ov2740_load_otp_data(struct nvm_data
*nvm
)
835 struct device
*dev
= regmap_get_device(nvm
->regmap
);
836 struct ov2740
*ov2740
= to_ov2740(dev_get_drvdata(dev
));
844 nvm
->nvm_buffer
= kzalloc(CUSTOMER_USE_OTP_SIZE
, GFP_KERNEL
);
845 if (!nvm
->nvm_buffer
)
848 ret
= ov2740_read_reg(ov2740
, OV2740_REG_ISP_CTRL00
, 1, &isp_ctrl00
);
850 dev_err(dev
, "failed to read ISP CTRL00\n");
854 ret
= ov2740_read_reg(ov2740
, OV2740_REG_ISP_CTRL01
, 1, &isp_ctrl01
);
856 dev_err(dev
, "failed to read ISP CTRL01\n");
860 /* Clear bit 5 of ISP CTRL00 */
861 ret
= ov2740_write_reg(ov2740
, OV2740_REG_ISP_CTRL00
, 1,
862 isp_ctrl00
& ~BIT(5));
864 dev_err(dev
, "failed to set ISP CTRL00\n");
868 /* Clear bit 7 of ISP CTRL01 */
869 ret
= ov2740_write_reg(ov2740
, OV2740_REG_ISP_CTRL01
, 1,
870 isp_ctrl01
& ~BIT(7));
872 dev_err(dev
, "failed to set ISP CTRL01\n");
876 ret
= ov2740_write_reg(ov2740
, OV2740_REG_MODE_SELECT
, 1,
877 OV2740_MODE_STREAMING
);
879 dev_err(dev
, "failed to set streaming mode\n");
884 * Users are not allowed to access OTP-related registers and memory
885 * during the 20 ms period after streaming starts (0x100 = 0x01).
889 ret
= regmap_bulk_read(nvm
->regmap
, OV2740_REG_OTP_CUSTOMER
,
890 nvm
->nvm_buffer
, CUSTOMER_USE_OTP_SIZE
);
892 dev_err(dev
, "failed to read OTP data, ret %d\n", ret
);
896 ret
= ov2740_write_reg(ov2740
, OV2740_REG_MODE_SELECT
, 1,
897 OV2740_MODE_STANDBY
);
899 dev_err(dev
, "failed to set streaming mode\n");
903 ret
= ov2740_write_reg(ov2740
, OV2740_REG_ISP_CTRL01
, 1, isp_ctrl01
);
905 dev_err(dev
, "failed to set ISP CTRL01\n");
909 ret
= ov2740_write_reg(ov2740
, OV2740_REG_ISP_CTRL00
, 1, isp_ctrl00
);
911 dev_err(dev
, "failed to set ISP CTRL00\n");
917 kfree(nvm
->nvm_buffer
);
918 nvm
->nvm_buffer
= NULL
;
923 static int ov2740_start_streaming(struct ov2740
*ov2740
)
925 struct i2c_client
*client
= v4l2_get_subdevdata(&ov2740
->sd
);
926 const struct ov2740_reg_list
*reg_list
;
930 ret
= ov2740_identify_module(ov2740
);
935 ov2740_load_otp_data(ov2740
->nvm
);
937 /* Reset the sensor */
938 ret
= ov2740_write_reg(ov2740
, 0x0103, 1, 0x01);
940 dev_err(&client
->dev
, "failed to reset\n");
944 usleep_range(10000, 15000);
946 link_freq_index
= ov2740
->cur_mode
->link_freq_index
;
947 reg_list
= &link_freq_configs
[link_freq_index
].reg_list
;
948 ret
= ov2740_write_reg_list(ov2740
, reg_list
);
950 dev_err(&client
->dev
, "failed to set plls\n");
954 reg_list
= &ov2740
->cur_mode
->reg_list
;
955 ret
= ov2740_write_reg_list(ov2740
, reg_list
);
957 dev_err(&client
->dev
, "failed to set mode\n");
961 ret
= __v4l2_ctrl_handler_setup(ov2740
->sd
.ctrl_handler
);
965 ret
= ov2740_write_reg(ov2740
, OV2740_REG_MODE_SELECT
, 1,
966 OV2740_MODE_STREAMING
);
968 dev_err(&client
->dev
, "failed to start streaming\n");
973 static void ov2740_stop_streaming(struct ov2740
*ov2740
)
975 struct i2c_client
*client
= v4l2_get_subdevdata(&ov2740
->sd
);
977 if (ov2740_write_reg(ov2740
, OV2740_REG_MODE_SELECT
, 1,
978 OV2740_MODE_STANDBY
))
979 dev_err(&client
->dev
, "failed to stop streaming\n");
982 static int ov2740_set_stream(struct v4l2_subdev
*sd
, int enable
)
984 struct ov2740
*ov2740
= to_ov2740(sd
);
985 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
986 struct v4l2_subdev_state
*sd_state
;
989 sd_state
= v4l2_subdev_lock_and_get_active_state(&ov2740
->sd
);
992 ret
= pm_runtime_resume_and_get(&client
->dev
);
996 ret
= ov2740_start_streaming(ov2740
);
999 ov2740_stop_streaming(ov2740
);
1000 pm_runtime_put(&client
->dev
);
1003 ov2740_stop_streaming(ov2740
);
1004 pm_runtime_put(&client
->dev
);
1008 v4l2_subdev_unlock_state(sd_state
);
1013 static int ov2740_set_format(struct v4l2_subdev
*sd
,
1014 struct v4l2_subdev_state
*sd_state
,
1015 struct v4l2_subdev_format
*fmt
)
1017 struct ov2740
*ov2740
= to_ov2740(sd
);
1018 const struct ov2740_mode
*mode
;
1019 s32 vblank_def
, h_blank
;
1021 mode
= v4l2_find_nearest_size(ov2740
->supported_modes
,
1022 ov2740
->supported_modes_count
,
1024 fmt
->format
.width
, fmt
->format
.height
);
1026 ov2740_update_pad_format(mode
, &fmt
->format
);
1027 *v4l2_subdev_state_get_format(sd_state
, fmt
->pad
) = fmt
->format
;
1029 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
)
1032 ov2740
->cur_mode
= mode
;
1033 __v4l2_ctrl_s_ctrl(ov2740
->link_freq
, mode
->link_freq_index
);
1034 __v4l2_ctrl_s_ctrl_int64(ov2740
->pixel_rate
,
1035 to_pixel_rate(mode
->link_freq_index
));
1037 /* Update limits and set FPS to default */
1038 vblank_def
= mode
->vts_def
- mode
->height
;
1039 __v4l2_ctrl_modify_range(ov2740
->vblank
,
1040 mode
->vts_min
- mode
->height
,
1041 mode
->vts_max
- mode
->height
, 1, vblank_def
);
1042 __v4l2_ctrl_s_ctrl(ov2740
->vblank
, vblank_def
);
1043 h_blank
= mode
->hts
- mode
->width
;
1044 __v4l2_ctrl_modify_range(ov2740
->hblank
, h_blank
, h_blank
, 1, h_blank
);
1049 static int ov2740_enum_mbus_code(struct v4l2_subdev
*sd
,
1050 struct v4l2_subdev_state
*sd_state
,
1051 struct v4l2_subdev_mbus_code_enum
*code
)
1053 if (code
->index
> 0)
1056 code
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
1061 static int ov2740_enum_frame_size(struct v4l2_subdev
*sd
,
1062 struct v4l2_subdev_state
*sd_state
,
1063 struct v4l2_subdev_frame_size_enum
*fse
)
1065 struct ov2740
*ov2740
= to_ov2740(sd
);
1066 const struct ov2740_mode
*supported_modes
= ov2740
->supported_modes
;
1068 if (fse
->index
>= ov2740
->supported_modes_count
)
1071 if (fse
->code
!= MEDIA_BUS_FMT_SGRBG10_1X10
)
1074 fse
->min_width
= supported_modes
[fse
->index
].width
;
1075 fse
->max_width
= fse
->min_width
;
1076 fse
->min_height
= supported_modes
[fse
->index
].height
;
1077 fse
->max_height
= fse
->min_height
;
1082 static int ov2740_init_state(struct v4l2_subdev
*sd
,
1083 struct v4l2_subdev_state
*sd_state
)
1085 struct ov2740
*ov2740
= to_ov2740(sd
);
1087 ov2740_update_pad_format(&ov2740
->supported_modes
[0],
1088 v4l2_subdev_state_get_format(sd_state
, 0));
1092 static const struct v4l2_subdev_video_ops ov2740_video_ops
= {
1093 .s_stream
= ov2740_set_stream
,
1096 static const struct v4l2_subdev_pad_ops ov2740_pad_ops
= {
1097 .get_fmt
= v4l2_subdev_get_fmt
,
1098 .set_fmt
= ov2740_set_format
,
1099 .enum_mbus_code
= ov2740_enum_mbus_code
,
1100 .enum_frame_size
= ov2740_enum_frame_size
,
1103 static const struct v4l2_subdev_ops ov2740_subdev_ops
= {
1104 .video
= &ov2740_video_ops
,
1105 .pad
= &ov2740_pad_ops
,
1108 static const struct v4l2_subdev_internal_ops ov2740_internal_ops
= {
1109 .init_state
= ov2740_init_state
,
1112 static const struct media_entity_operations ov2740_subdev_entity_ops
= {
1113 .link_validate
= v4l2_subdev_link_validate
,
1116 static int ov2740_check_hwcfg(struct device
*dev
)
1118 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
1119 struct ov2740
*ov2740
= to_ov2740(sd
);
1120 struct fwnode_handle
*ep
;
1121 struct fwnode_handle
*fwnode
= dev_fwnode(dev
);
1122 struct v4l2_fwnode_endpoint bus_cfg
= {
1123 .bus_type
= V4L2_MBUS_CSI2_DPHY
1130 * Sometimes the fwnode graph is initialized by the bridge driver,
1133 ep
= fwnode_graph_get_next_endpoint(fwnode
, NULL
);
1135 return dev_err_probe(dev
, -EPROBE_DEFER
,
1136 "waiting for fwnode graph endpoint\n");
1138 ret
= fwnode_property_read_u32(fwnode
, "clock-frequency", &mclk
);
1140 fwnode_handle_put(ep
);
1141 return dev_err_probe(dev
, ret
,
1142 "reading clock-frequency property\n");
1145 if (mclk
!= OV2740_MCLK
) {
1146 fwnode_handle_put(ep
);
1147 return dev_err_probe(dev
, -EINVAL
,
1148 "external clock %d is not supported\n",
1152 ret
= v4l2_fwnode_endpoint_alloc_parse(ep
, &bus_cfg
);
1153 fwnode_handle_put(ep
);
1155 return dev_err_probe(dev
, ret
, "parsing endpoint failed\n");
1157 if (bus_cfg
.bus
.mipi_csi2
.num_data_lanes
!= OV2740_DATA_LANES
) {
1158 ret
= dev_err_probe(dev
, -EINVAL
,
1159 "number of CSI2 data lanes %d is not supported\n",
1160 bus_cfg
.bus
.mipi_csi2
.num_data_lanes
);
1161 goto check_hwcfg_error
;
1164 if (!bus_cfg
.nr_of_link_frequencies
) {
1165 ret
= dev_err_probe(dev
, -EINVAL
, "no link frequencies defined\n");
1166 goto check_hwcfg_error
;
1169 for (i
= 0; i
< ARRAY_SIZE(link_freq_menu_items
); i
++) {
1170 for (j
= 0; j
< bus_cfg
.nr_of_link_frequencies
; j
++) {
1171 if (link_freq_menu_items
[i
] ==
1172 bus_cfg
.link_frequencies
[j
])
1176 if (j
== bus_cfg
.nr_of_link_frequencies
)
1180 case OV2740_LINK_FREQ_360MHZ_INDEX
:
1181 ov2740
->supported_modes
= supported_modes_360mhz
;
1182 ov2740
->supported_modes_count
=
1183 ARRAY_SIZE(supported_modes_360mhz
);
1185 case OV2740_LINK_FREQ_180MHZ_INDEX
:
1186 ov2740
->supported_modes
= supported_modes_180mhz
;
1187 ov2740
->supported_modes_count
=
1188 ARRAY_SIZE(supported_modes_180mhz
);
1192 break; /* Prefer modes from first available link-freq */
1195 if (!ov2740
->supported_modes
)
1196 ret
= dev_err_probe(dev
, -EINVAL
,
1197 "no supported link frequencies\n");
1200 v4l2_fwnode_endpoint_free(&bus_cfg
);
1205 static void ov2740_remove(struct i2c_client
*client
)
1207 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1209 v4l2_async_unregister_subdev(sd
);
1210 media_entity_cleanup(&sd
->entity
);
1211 v4l2_subdev_cleanup(sd
);
1212 v4l2_ctrl_handler_free(sd
->ctrl_handler
);
1213 pm_runtime_disable(&client
->dev
);
1216 static int ov2740_nvmem_read(void *priv
, unsigned int off
, void *val
,
1219 struct nvm_data
*nvm
= priv
;
1220 struct device
*dev
= regmap_get_device(nvm
->regmap
);
1221 struct ov2740
*ov2740
= to_ov2740(dev_get_drvdata(dev
));
1222 struct v4l2_subdev_state
*sd_state
;
1225 /* Serialise sensor access */
1226 sd_state
= v4l2_subdev_lock_and_get_active_state(&ov2740
->sd
);
1228 if (nvm
->nvm_buffer
) {
1229 memcpy(val
, nvm
->nvm_buffer
+ off
, count
);
1233 ret
= pm_runtime_resume_and_get(dev
);
1238 ret
= ov2740_load_otp_data(nvm
);
1240 memcpy(val
, nvm
->nvm_buffer
+ off
, count
);
1242 pm_runtime_put(dev
);
1244 v4l2_subdev_unlock_state(sd_state
);
1248 static int ov2740_register_nvmem(struct i2c_client
*client
,
1249 struct ov2740
*ov2740
)
1251 struct nvm_data
*nvm
;
1252 struct regmap_config regmap_config
= { };
1253 struct nvmem_config nvmem_config
= { };
1254 struct regmap
*regmap
;
1255 struct device
*dev
= &client
->dev
;
1257 nvm
= devm_kzalloc(dev
, sizeof(*nvm
), GFP_KERNEL
);
1261 regmap_config
.val_bits
= 8;
1262 regmap_config
.reg_bits
= 16;
1263 regmap_config
.disable_locking
= true;
1264 regmap
= devm_regmap_init_i2c(client
, ®map_config
);
1266 return PTR_ERR(regmap
);
1268 nvm
->regmap
= regmap
;
1270 nvmem_config
.name
= dev_name(dev
);
1271 nvmem_config
.dev
= dev
;
1272 nvmem_config
.read_only
= true;
1273 nvmem_config
.root_only
= true;
1274 nvmem_config
.owner
= THIS_MODULE
;
1275 nvmem_config
.compat
= true;
1276 nvmem_config
.base_dev
= dev
;
1277 nvmem_config
.reg_read
= ov2740_nvmem_read
;
1278 nvmem_config
.reg_write
= NULL
;
1279 nvmem_config
.priv
= nvm
;
1280 nvmem_config
.stride
= 1;
1281 nvmem_config
.word_size
= 1;
1282 nvmem_config
.size
= CUSTOMER_USE_OTP_SIZE
;
1284 nvm
->nvmem
= devm_nvmem_register(dev
, &nvmem_config
);
1285 if (IS_ERR(nvm
->nvmem
))
1286 return PTR_ERR(nvm
->nvmem
);
1292 static int ov2740_suspend(struct device
*dev
)
1294 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
1295 struct ov2740
*ov2740
= to_ov2740(sd
);
1297 gpiod_set_value_cansleep(ov2740
->reset_gpio
, 1);
1298 clk_disable_unprepare(ov2740
->clk
);
1302 static int ov2740_resume(struct device
*dev
)
1304 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
1305 struct ov2740
*ov2740
= to_ov2740(sd
);
1308 ret
= clk_prepare_enable(ov2740
->clk
);
1312 gpiod_set_value_cansleep(ov2740
->reset_gpio
, 0);
1318 static int ov2740_probe(struct i2c_client
*client
)
1320 struct device
*dev
= &client
->dev
;
1321 struct ov2740
*ov2740
;
1325 ov2740
= devm_kzalloc(&client
->dev
, sizeof(*ov2740
), GFP_KERNEL
);
1329 v4l2_i2c_subdev_init(&ov2740
->sd
, client
, &ov2740_subdev_ops
);
1330 ov2740
->sd
.internal_ops
= &ov2740_internal_ops
;
1332 ret
= ov2740_check_hwcfg(dev
);
1336 ov2740
->reset_gpio
= devm_gpiod_get_optional(dev
, "reset", GPIOD_OUT_HIGH
);
1337 if (IS_ERR(ov2740
->reset_gpio
)) {
1338 return dev_err_probe(dev
, PTR_ERR(ov2740
->reset_gpio
),
1339 "failed to get reset GPIO\n");
1340 } else if (ov2740
->reset_gpio
) {
1342 * Ensure reset is asserted for at least 20 ms before
1343 * ov2740_resume() deasserts it.
1348 ov2740
->clk
= devm_clk_get_optional(dev
, "clk");
1349 if (IS_ERR(ov2740
->clk
))
1350 return dev_err_probe(dev
, PTR_ERR(ov2740
->clk
),
1351 "failed to get clock\n");
1353 full_power
= acpi_dev_state_d0(&client
->dev
);
1355 /* ACPI does not always clear the reset GPIO / enable the clock */
1356 ret
= ov2740_resume(dev
);
1358 return dev_err_probe(dev
, ret
, "failed to power on sensor\n");
1360 ret
= ov2740_identify_module(ov2740
);
1362 dev_err_probe(dev
, ret
, "failed to find sensor\n");
1363 goto probe_error_power_off
;
1367 ov2740
->cur_mode
= &ov2740
->supported_modes
[0];
1368 ret
= ov2740_init_controls(ov2740
);
1370 dev_err_probe(dev
, ret
, "failed to init controls\n");
1371 goto probe_error_v4l2_ctrl_handler_free
;
1374 ov2740
->sd
.state_lock
= ov2740
->ctrl_handler
.lock
;
1375 ov2740
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1376 ov2740
->sd
.entity
.ops
= &ov2740_subdev_entity_ops
;
1377 ov2740
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1378 ov2740
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1379 ret
= media_entity_pads_init(&ov2740
->sd
.entity
, 1, &ov2740
->pad
);
1381 dev_err_probe(dev
, ret
, "failed to init entity pads\n");
1382 goto probe_error_v4l2_ctrl_handler_free
;
1385 ret
= v4l2_subdev_init_finalize(&ov2740
->sd
);
1387 goto probe_error_media_entity_cleanup
;
1389 /* Set the device's state to active if it's in D0 state. */
1391 pm_runtime_set_active(&client
->dev
);
1392 pm_runtime_enable(&client
->dev
);
1393 pm_runtime_idle(&client
->dev
);
1395 ret
= v4l2_async_register_subdev_sensor(&ov2740
->sd
);
1397 dev_err_probe(dev
, ret
, "failed to register V4L2 subdev\n");
1398 goto probe_error_v4l2_subdev_cleanup
;
1401 ret
= ov2740_register_nvmem(client
, ov2740
);
1403 dev_warn(&client
->dev
, "register nvmem failed, ret %d\n", ret
);
1407 probe_error_v4l2_subdev_cleanup
:
1408 v4l2_subdev_cleanup(&ov2740
->sd
);
1410 probe_error_media_entity_cleanup
:
1411 media_entity_cleanup(&ov2740
->sd
.entity
);
1412 pm_runtime_disable(&client
->dev
);
1413 pm_runtime_set_suspended(&client
->dev
);
1415 probe_error_v4l2_ctrl_handler_free
:
1416 v4l2_ctrl_handler_free(ov2740
->sd
.ctrl_handler
);
1418 probe_error_power_off
:
1420 ov2740_suspend(dev
);
1425 static DEFINE_RUNTIME_DEV_PM_OPS(ov2740_pm_ops
, ov2740_suspend
, ov2740_resume
,
1428 static const struct acpi_device_id ov2740_acpi_ids
[] = {
1433 MODULE_DEVICE_TABLE(acpi
, ov2740_acpi_ids
);
1435 static struct i2c_driver ov2740_i2c_driver
= {
1438 .acpi_match_table
= ov2740_acpi_ids
,
1439 .pm
= pm_sleep_ptr(&ov2740_pm_ops
),
1441 .probe
= ov2740_probe
,
1442 .remove
= ov2740_remove
,
1443 .flags
= I2C_DRV_ACPI_WAIVE_D0_PROBE
,
1446 module_i2c_driver(ov2740_i2c_driver
);
1448 MODULE_AUTHOR("Qiu, Tianshu <tian.shu.qiu@intel.com>");
1449 MODULE_AUTHOR("Shawn Tu");
1450 MODULE_AUTHOR("Bingbu Cao <bingbu.cao@intel.com>");
1451 MODULE_DESCRIPTION("OmniVision OV2740 sensor driver");
1452 MODULE_LICENSE("GPL v2");