1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 Intel Corporation.
4 #include <asm/unaligned.h>
5 #include <linux/acpi.h>
6 #include <linux/delay.h>
8 #include <linux/module.h>
9 #include <linux/pm_runtime.h>
10 #include <media/v4l2-ctrls.h>
11 #include <media/v4l2-device.h>
12 #include <media/v4l2-fwnode.h>
14 #define OV5675_REG_VALUE_08BIT 1
15 #define OV5675_REG_VALUE_16BIT 2
16 #define OV5675_REG_VALUE_24BIT 3
18 #define OV5675_LINK_FREQ_450MHZ 450000000ULL
19 #define OV5675_SCLK 90000000LL
20 #define OV5675_MCLK 19200000
21 #define OV5675_DATA_LANES 2
22 #define OV5675_RGB_DEPTH 10
24 #define OV5675_REG_CHIP_ID 0x300a
25 #define OV5675_CHIP_ID 0x5675
27 #define OV5675_REG_MODE_SELECT 0x0100
28 #define OV5675_MODE_STANDBY 0x00
29 #define OV5675_MODE_STREAMING 0x01
31 /* vertical-timings from sensor */
32 #define OV5675_REG_VTS 0x380e
33 #define OV5675_VTS_30FPS 0x07e4
34 #define OV5675_VTS_30FPS_MIN 0x07e4
35 #define OV5675_VTS_MAX 0x7fff
37 /* horizontal-timings from sensor */
38 #define OV5675_REG_HTS 0x380c
40 /* Exposure controls from sensor */
41 #define OV5675_REG_EXPOSURE 0x3500
42 #define OV5675_EXPOSURE_MIN 4
43 #define OV5675_EXPOSURE_MAX_MARGIN 4
44 #define OV5675_EXPOSURE_STEP 1
46 /* Analog gain controls from sensor */
47 #define OV5675_REG_ANALOG_GAIN 0x3508
48 #define OV5675_ANAL_GAIN_MIN 128
49 #define OV5675_ANAL_GAIN_MAX 2047
50 #define OV5675_ANAL_GAIN_STEP 1
52 /* Digital gain controls from sensor */
53 #define OV5675_REG_MWB_R_GAIN 0x5019
54 #define OV5675_REG_MWB_G_GAIN 0x501b
55 #define OV5675_REG_MWB_B_GAIN 0x501d
56 #define OV5675_DGTL_GAIN_MIN 0
57 #define OV5675_DGTL_GAIN_MAX 4095
58 #define OV5675_DGTL_GAIN_STEP 1
59 #define OV5675_DGTL_GAIN_DEFAULT 1024
61 /* Test Pattern Control */
62 #define OV5675_REG_TEST_PATTERN 0x4503
63 #define OV5675_TEST_PATTERN_ENABLE BIT(7)
64 #define OV5675_TEST_PATTERN_BAR_SHIFT 2
66 /* Flip Mirror Controls from sensor */
67 #define OV5675_REG_FORMAT1 0x3820
68 #define OV5675_REG_FORMAT2 0x373d
70 #define to_ov5675(_sd) container_of(_sd, struct ov5675, sd)
73 OV5675_LINK_FREQ_900MBPS
,
81 struct ov5675_reg_list
{
83 const struct ov5675_reg
*regs
;
86 struct ov5675_link_freq_config
{
87 const struct ov5675_reg_list reg_list
;
91 /* Frame width in pixels */
94 /* Frame height in pixels */
97 /* Horizontal timining size */
100 /* Default vertical timining size */
103 /* Min vertical timining size */
106 /* Link frequency needed for this resolution */
109 /* Sensor register settings for this resolution */
110 const struct ov5675_reg_list reg_list
;
113 static const struct ov5675_reg mipi_data_rate_900mbps
[] = {
122 static const struct ov5675_reg mode_2592x1944_regs
[] = {
275 static const struct ov5675_reg mode_1296x972_regs
[] = {
428 static const char * const ov5675_test_pattern_menu
[] = {
430 "Standard Color Bar",
431 "Top-Bottom Darker Color Bar",
432 "Right-Left Darker Color Bar",
433 "Bottom-Top Darker Color Bar"
436 static const s64 link_freq_menu_items
[] = {
437 OV5675_LINK_FREQ_450MHZ
,
440 static const struct ov5675_link_freq_config link_freq_configs
[] = {
441 [OV5675_LINK_FREQ_900MBPS
] = {
443 .num_of_regs
= ARRAY_SIZE(mipi_data_rate_900mbps
),
444 .regs
= mipi_data_rate_900mbps
,
449 static const struct ov5675_mode supported_modes
[] = {
454 .vts_def
= OV5675_VTS_30FPS
,
455 .vts_min
= OV5675_VTS_30FPS_MIN
,
457 .num_of_regs
= ARRAY_SIZE(mode_2592x1944_regs
),
458 .regs
= mode_2592x1944_regs
,
460 .link_freq_index
= OV5675_LINK_FREQ_900MBPS
,
466 .vts_def
= OV5675_VTS_30FPS
,
467 .vts_min
= OV5675_VTS_30FPS_MIN
,
469 .num_of_regs
= ARRAY_SIZE(mode_1296x972_regs
),
470 .regs
= mode_1296x972_regs
,
472 .link_freq_index
= OV5675_LINK_FREQ_900MBPS
,
477 struct v4l2_subdev sd
;
478 struct media_pad pad
;
479 struct v4l2_ctrl_handler ctrl_handler
;
482 struct v4l2_ctrl
*link_freq
;
483 struct v4l2_ctrl
*pixel_rate
;
484 struct v4l2_ctrl
*vblank
;
485 struct v4l2_ctrl
*hblank
;
486 struct v4l2_ctrl
*exposure
;
489 const struct ov5675_mode
*cur_mode
;
491 /* To serialize asynchronus callbacks */
494 /* Streaming on/off */
498 static u64
to_pixel_rate(u32 f_index
)
500 u64 pixel_rate
= link_freq_menu_items
[f_index
] * 2 * OV5675_DATA_LANES
;
502 do_div(pixel_rate
, OV5675_RGB_DEPTH
);
507 static u64
to_pixels_per_line(u32 hts
, u32 f_index
)
509 u64 ppl
= hts
* to_pixel_rate(f_index
);
511 do_div(ppl
, OV5675_SCLK
);
516 static int ov5675_read_reg(struct ov5675
*ov5675
, u16 reg
, u16 len
, u32
*val
)
518 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5675
->sd
);
519 struct i2c_msg msgs
[2];
521 u8 data_buf
[4] = {0};
527 put_unaligned_be16(reg
, addr_buf
);
528 msgs
[0].addr
= client
->addr
;
530 msgs
[0].len
= sizeof(addr_buf
);
531 msgs
[0].buf
= addr_buf
;
532 msgs
[1].addr
= client
->addr
;
533 msgs
[1].flags
= I2C_M_RD
;
535 msgs
[1].buf
= &data_buf
[4 - len
];
537 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
538 if (ret
!= ARRAY_SIZE(msgs
))
541 *val
= get_unaligned_be32(data_buf
);
546 static int ov5675_write_reg(struct ov5675
*ov5675
, u16 reg
, u16 len
, u32 val
)
548 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5675
->sd
);
554 put_unaligned_be16(reg
, buf
);
555 put_unaligned_be32(val
<< 8 * (4 - len
), buf
+ 2);
556 if (i2c_master_send(client
, buf
, len
+ 2) != len
+ 2)
562 static int ov5675_write_reg_list(struct ov5675
*ov5675
,
563 const struct ov5675_reg_list
*r_list
)
565 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5675
->sd
);
569 for (i
= 0; i
< r_list
->num_of_regs
; i
++) {
570 ret
= ov5675_write_reg(ov5675
, r_list
->regs
[i
].address
, 1,
571 r_list
->regs
[i
].val
);
573 dev_err_ratelimited(&client
->dev
,
574 "failed to write reg 0x%4.4x. error = %d",
575 r_list
->regs
[i
].address
, ret
);
583 static int ov5675_update_digital_gain(struct ov5675
*ov5675
, u32 d_gain
)
587 ret
= ov5675_write_reg(ov5675
, OV5675_REG_MWB_R_GAIN
,
588 OV5675_REG_VALUE_16BIT
, d_gain
);
592 ret
= ov5675_write_reg(ov5675
, OV5675_REG_MWB_G_GAIN
,
593 OV5675_REG_VALUE_16BIT
, d_gain
);
597 return ov5675_write_reg(ov5675
, OV5675_REG_MWB_B_GAIN
,
598 OV5675_REG_VALUE_16BIT
, d_gain
);
601 static int ov5675_test_pattern(struct ov5675
*ov5675
, u32 pattern
)
604 pattern
= (pattern
- 1) << OV5675_TEST_PATTERN_BAR_SHIFT
|
605 OV5675_TEST_PATTERN_ENABLE
;
607 return ov5675_write_reg(ov5675
, OV5675_REG_TEST_PATTERN
,
608 OV5675_REG_VALUE_08BIT
, pattern
);
612 * OV5675 supports keeping the pixel order by mirror and flip function
613 * The Bayer order isn't affected by the flip controls
615 static int ov5675_set_ctrl_hflip(struct ov5675
*ov5675
, u32 ctrl_val
)
620 ret
= ov5675_read_reg(ov5675
, OV5675_REG_FORMAT1
,
621 OV5675_REG_VALUE_08BIT
, &val
);
625 return ov5675_write_reg(ov5675
, OV5675_REG_FORMAT1
,
626 OV5675_REG_VALUE_08BIT
,
627 ctrl_val
? val
& ~BIT(3) : val
);
630 static int ov5675_set_ctrl_vflip(struct ov5675
*ov5675
, u8 ctrl_val
)
635 ret
= ov5675_read_reg(ov5675
, OV5675_REG_FORMAT1
,
636 OV5675_REG_VALUE_08BIT
, &val
);
640 ret
= ov5675_write_reg(ov5675
, OV5675_REG_FORMAT1
,
641 OV5675_REG_VALUE_08BIT
,
642 ctrl_val
? val
| BIT(4) | BIT(5) : val
);
647 ret
= ov5675_read_reg(ov5675
, OV5675_REG_FORMAT2
,
648 OV5675_REG_VALUE_08BIT
, &val
);
653 return ov5675_write_reg(ov5675
, OV5675_REG_FORMAT2
,
654 OV5675_REG_VALUE_08BIT
,
655 ctrl_val
? val
| BIT(1) : val
);
658 static int ov5675_set_ctrl(struct v4l2_ctrl
*ctrl
)
660 struct ov5675
*ov5675
= container_of(ctrl
->handler
,
661 struct ov5675
, ctrl_handler
);
662 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5675
->sd
);
666 /* Propagate change of current control to all related controls */
667 if (ctrl
->id
== V4L2_CID_VBLANK
) {
668 /* Update max exposure while meeting expected vblanking */
669 exposure_max
= ov5675
->cur_mode
->height
+ ctrl
->val
-
670 OV5675_EXPOSURE_MAX_MARGIN
;
671 __v4l2_ctrl_modify_range(ov5675
->exposure
,
672 ov5675
->exposure
->minimum
,
673 exposure_max
, ov5675
->exposure
->step
,
677 /* V4L2 controls values will be applied only when power is already up */
678 if (!pm_runtime_get_if_in_use(&client
->dev
))
682 case V4L2_CID_ANALOGUE_GAIN
:
683 ret
= ov5675_write_reg(ov5675
, OV5675_REG_ANALOG_GAIN
,
684 OV5675_REG_VALUE_16BIT
, ctrl
->val
);
687 case V4L2_CID_DIGITAL_GAIN
:
688 ret
= ov5675_update_digital_gain(ov5675
, ctrl
->val
);
691 case V4L2_CID_EXPOSURE
:
692 /* 4 least significant bits of expsoure are fractional part
694 * for ov5675, the unit of exposure is differnt from other
695 * OmniVision sensors, its exposure value is twice of the
696 * register value, the exposure should be divided by 2 before
697 * set register, e.g. val << 3.
699 ret
= ov5675_write_reg(ov5675
, OV5675_REG_EXPOSURE
,
700 OV5675_REG_VALUE_24BIT
, ctrl
->val
<< 3);
703 case V4L2_CID_VBLANK
:
704 ret
= ov5675_write_reg(ov5675
, OV5675_REG_VTS
,
705 OV5675_REG_VALUE_16BIT
,
706 ov5675
->cur_mode
->height
+ ctrl
->val
+
710 case V4L2_CID_TEST_PATTERN
:
711 ret
= ov5675_test_pattern(ov5675
, ctrl
->val
);
715 ov5675_set_ctrl_hflip(ov5675
, ctrl
->val
);
719 ov5675_set_ctrl_vflip(ov5675
, ctrl
->val
);
727 pm_runtime_put(&client
->dev
);
732 static const struct v4l2_ctrl_ops ov5675_ctrl_ops
= {
733 .s_ctrl
= ov5675_set_ctrl
,
736 static int ov5675_init_controls(struct ov5675
*ov5675
)
738 struct v4l2_ctrl_handler
*ctrl_hdlr
;
739 s64 exposure_max
, h_blank
;
742 ctrl_hdlr
= &ov5675
->ctrl_handler
;
743 ret
= v4l2_ctrl_handler_init(ctrl_hdlr
, 8);
747 ctrl_hdlr
->lock
= &ov5675
->mutex
;
748 ov5675
->link_freq
= v4l2_ctrl_new_int_menu(ctrl_hdlr
, &ov5675_ctrl_ops
,
750 ARRAY_SIZE(link_freq_menu_items
) - 1,
751 0, link_freq_menu_items
);
752 if (ov5675
->link_freq
)
753 ov5675
->link_freq
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
755 ov5675
->pixel_rate
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov5675_ctrl_ops
,
756 V4L2_CID_PIXEL_RATE
, 0,
757 to_pixel_rate(OV5675_LINK_FREQ_900MBPS
),
759 to_pixel_rate(OV5675_LINK_FREQ_900MBPS
));
760 ov5675
->vblank
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov5675_ctrl_ops
,
762 ov5675
->cur_mode
->vts_min
- ov5675
->cur_mode
->height
,
763 OV5675_VTS_MAX
- ov5675
->cur_mode
->height
, 1,
764 ov5675
->cur_mode
->vts_def
- ov5675
->cur_mode
->height
);
765 h_blank
= to_pixels_per_line(ov5675
->cur_mode
->hts
,
766 ov5675
->cur_mode
->link_freq_index
) - ov5675
->cur_mode
->width
;
767 ov5675
->hblank
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov5675_ctrl_ops
,
768 V4L2_CID_HBLANK
, h_blank
, h_blank
, 1,
771 ov5675
->hblank
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
773 v4l2_ctrl_new_std(ctrl_hdlr
, &ov5675_ctrl_ops
, V4L2_CID_ANALOGUE_GAIN
,
774 OV5675_ANAL_GAIN_MIN
, OV5675_ANAL_GAIN_MAX
,
775 OV5675_ANAL_GAIN_STEP
, OV5675_ANAL_GAIN_MIN
);
776 v4l2_ctrl_new_std(ctrl_hdlr
, &ov5675_ctrl_ops
, V4L2_CID_DIGITAL_GAIN
,
777 OV5675_DGTL_GAIN_MIN
, OV5675_DGTL_GAIN_MAX
,
778 OV5675_DGTL_GAIN_STEP
, OV5675_DGTL_GAIN_DEFAULT
);
779 exposure_max
= (ov5675
->cur_mode
->vts_def
- OV5675_EXPOSURE_MAX_MARGIN
);
780 ov5675
->exposure
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov5675_ctrl_ops
,
782 OV5675_EXPOSURE_MIN
, exposure_max
,
783 OV5675_EXPOSURE_STEP
,
785 v4l2_ctrl_new_std_menu_items(ctrl_hdlr
, &ov5675_ctrl_ops
,
786 V4L2_CID_TEST_PATTERN
,
787 ARRAY_SIZE(ov5675_test_pattern_menu
) - 1,
788 0, 0, ov5675_test_pattern_menu
);
789 v4l2_ctrl_new_std(ctrl_hdlr
, &ov5675_ctrl_ops
,
790 V4L2_CID_HFLIP
, 0, 1, 1, 0);
791 v4l2_ctrl_new_std(ctrl_hdlr
, &ov5675_ctrl_ops
,
792 V4L2_CID_VFLIP
, 0, 1, 1, 0);
794 if (ctrl_hdlr
->error
)
795 return ctrl_hdlr
->error
;
797 ov5675
->sd
.ctrl_handler
= ctrl_hdlr
;
802 static void ov5675_update_pad_format(const struct ov5675_mode
*mode
,
803 struct v4l2_mbus_framefmt
*fmt
)
805 fmt
->width
= mode
->width
;
806 fmt
->height
= mode
->height
;
807 fmt
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
808 fmt
->field
= V4L2_FIELD_NONE
;
811 static int ov5675_start_streaming(struct ov5675
*ov5675
)
813 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5675
->sd
);
814 const struct ov5675_reg_list
*reg_list
;
815 int link_freq_index
, ret
;
817 link_freq_index
= ov5675
->cur_mode
->link_freq_index
;
818 reg_list
= &link_freq_configs
[link_freq_index
].reg_list
;
819 ret
= ov5675_write_reg_list(ov5675
, reg_list
);
821 dev_err(&client
->dev
, "failed to set plls");
825 reg_list
= &ov5675
->cur_mode
->reg_list
;
826 ret
= ov5675_write_reg_list(ov5675
, reg_list
);
828 dev_err(&client
->dev
, "failed to set mode");
832 ret
= __v4l2_ctrl_handler_setup(ov5675
->sd
.ctrl_handler
);
836 ret
= ov5675_write_reg(ov5675
, OV5675_REG_MODE_SELECT
,
837 OV5675_REG_VALUE_08BIT
, OV5675_MODE_STREAMING
);
839 dev_err(&client
->dev
, "failed to set stream");
846 static void ov5675_stop_streaming(struct ov5675
*ov5675
)
848 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5675
->sd
);
850 if (ov5675_write_reg(ov5675
, OV5675_REG_MODE_SELECT
,
851 OV5675_REG_VALUE_08BIT
, OV5675_MODE_STANDBY
))
852 dev_err(&client
->dev
, "failed to set stream");
855 static int ov5675_set_stream(struct v4l2_subdev
*sd
, int enable
)
857 struct ov5675
*ov5675
= to_ov5675(sd
);
858 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
861 if (ov5675
->streaming
== enable
)
864 mutex_lock(&ov5675
->mutex
);
866 ret
= pm_runtime_get_sync(&client
->dev
);
868 pm_runtime_put_noidle(&client
->dev
);
869 mutex_unlock(&ov5675
->mutex
);
873 ret
= ov5675_start_streaming(ov5675
);
876 ov5675_stop_streaming(ov5675
);
877 pm_runtime_put(&client
->dev
);
880 ov5675_stop_streaming(ov5675
);
881 pm_runtime_put(&client
->dev
);
884 ov5675
->streaming
= enable
;
885 mutex_unlock(&ov5675
->mutex
);
890 static int __maybe_unused
ov5675_suspend(struct device
*dev
)
892 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
893 struct ov5675
*ov5675
= to_ov5675(sd
);
895 mutex_lock(&ov5675
->mutex
);
896 if (ov5675
->streaming
)
897 ov5675_stop_streaming(ov5675
);
899 mutex_unlock(&ov5675
->mutex
);
904 static int __maybe_unused
ov5675_resume(struct device
*dev
)
906 struct v4l2_subdev
*sd
= dev_get_drvdata(dev
);
907 struct ov5675
*ov5675
= to_ov5675(sd
);
910 mutex_lock(&ov5675
->mutex
);
911 if (ov5675
->streaming
) {
912 ret
= ov5675_start_streaming(ov5675
);
914 ov5675
->streaming
= false;
915 ov5675_stop_streaming(ov5675
);
916 mutex_unlock(&ov5675
->mutex
);
921 mutex_unlock(&ov5675
->mutex
);
926 static int ov5675_set_format(struct v4l2_subdev
*sd
,
927 struct v4l2_subdev_pad_config
*cfg
,
928 struct v4l2_subdev_format
*fmt
)
930 struct ov5675
*ov5675
= to_ov5675(sd
);
931 const struct ov5675_mode
*mode
;
932 s32 vblank_def
, h_blank
;
934 mode
= v4l2_find_nearest_size(supported_modes
,
935 ARRAY_SIZE(supported_modes
), width
,
936 height
, fmt
->format
.width
,
939 mutex_lock(&ov5675
->mutex
);
940 ov5675_update_pad_format(mode
, &fmt
->format
);
941 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
942 *v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
) = fmt
->format
;
944 ov5675
->cur_mode
= mode
;
945 __v4l2_ctrl_s_ctrl(ov5675
->link_freq
, mode
->link_freq_index
);
946 __v4l2_ctrl_s_ctrl_int64(ov5675
->pixel_rate
,
947 to_pixel_rate(mode
->link_freq_index
));
949 /* Update limits and set FPS to default */
950 vblank_def
= mode
->vts_def
- mode
->height
;
951 __v4l2_ctrl_modify_range(ov5675
->vblank
,
952 mode
->vts_min
- mode
->height
,
953 OV5675_VTS_MAX
- mode
->height
, 1,
955 __v4l2_ctrl_s_ctrl(ov5675
->vblank
, vblank_def
);
956 h_blank
= to_pixels_per_line(mode
->hts
, mode
->link_freq_index
) -
958 __v4l2_ctrl_modify_range(ov5675
->hblank
, h_blank
, h_blank
, 1,
962 mutex_unlock(&ov5675
->mutex
);
967 static int ov5675_get_format(struct v4l2_subdev
*sd
,
968 struct v4l2_subdev_pad_config
*cfg
,
969 struct v4l2_subdev_format
*fmt
)
971 struct ov5675
*ov5675
= to_ov5675(sd
);
973 mutex_lock(&ov5675
->mutex
);
974 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
)
975 fmt
->format
= *v4l2_subdev_get_try_format(&ov5675
->sd
, cfg
,
978 ov5675_update_pad_format(ov5675
->cur_mode
, &fmt
->format
);
980 mutex_unlock(&ov5675
->mutex
);
985 static int ov5675_enum_mbus_code(struct v4l2_subdev
*sd
,
986 struct v4l2_subdev_pad_config
*cfg
,
987 struct v4l2_subdev_mbus_code_enum
*code
)
992 code
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
997 static int ov5675_enum_frame_size(struct v4l2_subdev
*sd
,
998 struct v4l2_subdev_pad_config
*cfg
,
999 struct v4l2_subdev_frame_size_enum
*fse
)
1001 if (fse
->index
>= ARRAY_SIZE(supported_modes
))
1004 if (fse
->code
!= MEDIA_BUS_FMT_SGRBG10_1X10
)
1007 fse
->min_width
= supported_modes
[fse
->index
].width
;
1008 fse
->max_width
= fse
->min_width
;
1009 fse
->min_height
= supported_modes
[fse
->index
].height
;
1010 fse
->max_height
= fse
->min_height
;
1015 static int ov5675_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
1017 struct ov5675
*ov5675
= to_ov5675(sd
);
1019 mutex_lock(&ov5675
->mutex
);
1020 ov5675_update_pad_format(&supported_modes
[0],
1021 v4l2_subdev_get_try_format(sd
, fh
->pad
, 0));
1022 mutex_unlock(&ov5675
->mutex
);
1027 static const struct v4l2_subdev_video_ops ov5675_video_ops
= {
1028 .s_stream
= ov5675_set_stream
,
1031 static const struct v4l2_subdev_pad_ops ov5675_pad_ops
= {
1032 .set_fmt
= ov5675_set_format
,
1033 .get_fmt
= ov5675_get_format
,
1034 .enum_mbus_code
= ov5675_enum_mbus_code
,
1035 .enum_frame_size
= ov5675_enum_frame_size
,
1038 static const struct v4l2_subdev_ops ov5675_subdev_ops
= {
1039 .video
= &ov5675_video_ops
,
1040 .pad
= &ov5675_pad_ops
,
1043 static const struct media_entity_operations ov5675_subdev_entity_ops
= {
1044 .link_validate
= v4l2_subdev_link_validate
,
1047 static const struct v4l2_subdev_internal_ops ov5675_internal_ops
= {
1048 .open
= ov5675_open
,
1051 static int ov5675_identify_module(struct ov5675
*ov5675
)
1053 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5675
->sd
);
1057 ret
= ov5675_read_reg(ov5675
, OV5675_REG_CHIP_ID
,
1058 OV5675_REG_VALUE_24BIT
, &val
);
1062 if (val
!= OV5675_CHIP_ID
) {
1063 dev_err(&client
->dev
, "chip id mismatch: %x!=%x",
1064 OV5675_CHIP_ID
, val
);
1071 static int ov5675_check_hwcfg(struct device
*dev
)
1073 struct fwnode_handle
*ep
;
1074 struct fwnode_handle
*fwnode
= dev_fwnode(dev
);
1075 struct v4l2_fwnode_endpoint bus_cfg
= {
1076 .bus_type
= V4L2_MBUS_CSI2_DPHY
1085 ret
= fwnode_property_read_u32(fwnode
, "clock-frequency", &mclk
);
1088 dev_err(dev
, "can't get clock frequency");
1092 if (mclk
!= OV5675_MCLK
) {
1093 dev_err(dev
, "external clock %d is not supported", mclk
);
1097 ep
= fwnode_graph_get_next_endpoint(fwnode
, NULL
);
1101 ret
= v4l2_fwnode_endpoint_alloc_parse(ep
, &bus_cfg
);
1102 fwnode_handle_put(ep
);
1106 if (bus_cfg
.bus
.mipi_csi2
.num_data_lanes
!= OV5675_DATA_LANES
) {
1107 dev_err(dev
, "number of CSI2 data lanes %d is not supported",
1108 bus_cfg
.bus
.mipi_csi2
.num_data_lanes
);
1110 goto check_hwcfg_error
;
1113 if (!bus_cfg
.nr_of_link_frequencies
) {
1114 dev_err(dev
, "no link frequencies defined");
1116 goto check_hwcfg_error
;
1119 for (i
= 0; i
< ARRAY_SIZE(link_freq_menu_items
); i
++) {
1120 for (j
= 0; j
< bus_cfg
.nr_of_link_frequencies
; j
++) {
1121 if (link_freq_menu_items
[i
] ==
1122 bus_cfg
.link_frequencies
[j
])
1126 if (j
== bus_cfg
.nr_of_link_frequencies
) {
1127 dev_err(dev
, "no link frequency %lld supported",
1128 link_freq_menu_items
[i
]);
1130 goto check_hwcfg_error
;
1135 v4l2_fwnode_endpoint_free(&bus_cfg
);
1140 static int ov5675_remove(struct i2c_client
*client
)
1142 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1143 struct ov5675
*ov5675
= to_ov5675(sd
);
1145 v4l2_async_unregister_subdev(sd
);
1146 media_entity_cleanup(&sd
->entity
);
1147 v4l2_ctrl_handler_free(sd
->ctrl_handler
);
1148 pm_runtime_disable(&client
->dev
);
1149 mutex_destroy(&ov5675
->mutex
);
1154 static int ov5675_probe(struct i2c_client
*client
)
1156 struct ov5675
*ov5675
;
1159 ret
= ov5675_check_hwcfg(&client
->dev
);
1161 dev_err(&client
->dev
, "failed to check HW configuration: %d",
1166 ov5675
= devm_kzalloc(&client
->dev
, sizeof(*ov5675
), GFP_KERNEL
);
1170 v4l2_i2c_subdev_init(&ov5675
->sd
, client
, &ov5675_subdev_ops
);
1171 ret
= ov5675_identify_module(ov5675
);
1173 dev_err(&client
->dev
, "failed to find sensor: %d", ret
);
1177 mutex_init(&ov5675
->mutex
);
1178 ov5675
->cur_mode
= &supported_modes
[0];
1179 ret
= ov5675_init_controls(ov5675
);
1181 dev_err(&client
->dev
, "failed to init controls: %d", ret
);
1182 goto probe_error_v4l2_ctrl_handler_free
;
1185 ov5675
->sd
.internal_ops
= &ov5675_internal_ops
;
1186 ov5675
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1187 ov5675
->sd
.entity
.ops
= &ov5675_subdev_entity_ops
;
1188 ov5675
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1189 ov5675
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1190 ret
= media_entity_pads_init(&ov5675
->sd
.entity
, 1, &ov5675
->pad
);
1192 dev_err(&client
->dev
, "failed to init entity pads: %d", ret
);
1193 goto probe_error_v4l2_ctrl_handler_free
;
1196 ret
= v4l2_async_register_subdev_sensor_common(&ov5675
->sd
);
1198 dev_err(&client
->dev
, "failed to register V4L2 subdev: %d",
1200 goto probe_error_media_entity_cleanup
;
1204 * Device is already turned on by i2c-core with ACPI domain PM.
1205 * Enable runtime PM and turn off the device.
1207 pm_runtime_set_active(&client
->dev
);
1208 pm_runtime_enable(&client
->dev
);
1209 pm_runtime_idle(&client
->dev
);
1213 probe_error_media_entity_cleanup
:
1214 media_entity_cleanup(&ov5675
->sd
.entity
);
1216 probe_error_v4l2_ctrl_handler_free
:
1217 v4l2_ctrl_handler_free(ov5675
->sd
.ctrl_handler
);
1218 mutex_destroy(&ov5675
->mutex
);
1223 static const struct dev_pm_ops ov5675_pm_ops
= {
1224 SET_SYSTEM_SLEEP_PM_OPS(ov5675_suspend
, ov5675_resume
)
1228 static const struct acpi_device_id ov5675_acpi_ids
[] = {
1233 MODULE_DEVICE_TABLE(acpi
, ov5675_acpi_ids
);
1236 static struct i2c_driver ov5675_i2c_driver
= {
1239 .pm
= &ov5675_pm_ops
,
1240 .acpi_match_table
= ACPI_PTR(ov5675_acpi_ids
),
1242 .probe_new
= ov5675_probe
,
1243 .remove
= ov5675_remove
,
1246 module_i2c_driver(ov5675_i2c_driver
);
1248 MODULE_AUTHOR("Shawn Tu <shawnx.tu@intel.com>");
1249 MODULE_DESCRIPTION("OmniVision OV5675 sensor driver");
1250 MODULE_LICENSE("GPL v2");