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
) / 2;
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 /* 3 least significant bits of expsoure are fractional part */
693 ret
= ov5675_write_reg(ov5675
, OV5675_REG_EXPOSURE
,
694 OV5675_REG_VALUE_24BIT
, ctrl
->val
<< 3);
697 case V4L2_CID_VBLANK
:
698 ret
= ov5675_write_reg(ov5675
, OV5675_REG_VTS
,
699 OV5675_REG_VALUE_16BIT
,
700 ov5675
->cur_mode
->height
+ ctrl
->val
+
704 case V4L2_CID_TEST_PATTERN
:
705 ret
= ov5675_test_pattern(ov5675
, ctrl
->val
);
709 ov5675_set_ctrl_hflip(ov5675
, ctrl
->val
);
713 ov5675_set_ctrl_vflip(ov5675
, ctrl
->val
);
721 pm_runtime_put(&client
->dev
);
726 static const struct v4l2_ctrl_ops ov5675_ctrl_ops
= {
727 .s_ctrl
= ov5675_set_ctrl
,
730 static int ov5675_init_controls(struct ov5675
*ov5675
)
732 struct v4l2_ctrl_handler
*ctrl_hdlr
;
733 s64 exposure_max
, h_blank
;
736 ctrl_hdlr
= &ov5675
->ctrl_handler
;
737 ret
= v4l2_ctrl_handler_init(ctrl_hdlr
, 8);
741 ctrl_hdlr
->lock
= &ov5675
->mutex
;
742 ov5675
->link_freq
= v4l2_ctrl_new_int_menu(ctrl_hdlr
, &ov5675_ctrl_ops
,
744 ARRAY_SIZE(link_freq_menu_items
) - 1,
745 0, link_freq_menu_items
);
746 if (ov5675
->link_freq
)
747 ov5675
->link_freq
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
749 ov5675
->pixel_rate
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov5675_ctrl_ops
,
750 V4L2_CID_PIXEL_RATE
, 0,
751 to_pixel_rate(OV5675_LINK_FREQ_900MBPS
),
753 to_pixel_rate(OV5675_LINK_FREQ_900MBPS
));
754 ov5675
->vblank
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov5675_ctrl_ops
,
756 ov5675
->cur_mode
->vts_min
- ov5675
->cur_mode
->height
,
757 OV5675_VTS_MAX
- ov5675
->cur_mode
->height
, 1,
758 ov5675
->cur_mode
->vts_def
- ov5675
->cur_mode
->height
);
759 h_blank
= to_pixels_per_line(ov5675
->cur_mode
->hts
,
760 ov5675
->cur_mode
->link_freq_index
) - ov5675
->cur_mode
->width
;
761 ov5675
->hblank
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov5675_ctrl_ops
,
762 V4L2_CID_HBLANK
, h_blank
, h_blank
, 1,
765 ov5675
->hblank
->flags
|= V4L2_CTRL_FLAG_READ_ONLY
;
767 v4l2_ctrl_new_std(ctrl_hdlr
, &ov5675_ctrl_ops
, V4L2_CID_ANALOGUE_GAIN
,
768 OV5675_ANAL_GAIN_MIN
, OV5675_ANAL_GAIN_MAX
,
769 OV5675_ANAL_GAIN_STEP
, OV5675_ANAL_GAIN_MIN
);
770 v4l2_ctrl_new_std(ctrl_hdlr
, &ov5675_ctrl_ops
, V4L2_CID_DIGITAL_GAIN
,
771 OV5675_DGTL_GAIN_MIN
, OV5675_DGTL_GAIN_MAX
,
772 OV5675_DGTL_GAIN_STEP
, OV5675_DGTL_GAIN_DEFAULT
);
773 exposure_max
= (ov5675
->cur_mode
->vts_def
-
774 OV5675_EXPOSURE_MAX_MARGIN
) / 2;
775 ov5675
->exposure
= v4l2_ctrl_new_std(ctrl_hdlr
, &ov5675_ctrl_ops
,
777 OV5675_EXPOSURE_MIN
, exposure_max
,
778 OV5675_EXPOSURE_STEP
,
780 v4l2_ctrl_new_std_menu_items(ctrl_hdlr
, &ov5675_ctrl_ops
,
781 V4L2_CID_TEST_PATTERN
,
782 ARRAY_SIZE(ov5675_test_pattern_menu
) - 1,
783 0, 0, ov5675_test_pattern_menu
);
784 v4l2_ctrl_new_std(ctrl_hdlr
, &ov5675_ctrl_ops
,
785 V4L2_CID_HFLIP
, 0, 1, 1, 0);
786 v4l2_ctrl_new_std(ctrl_hdlr
, &ov5675_ctrl_ops
,
787 V4L2_CID_VFLIP
, 0, 1, 1, 0);
789 if (ctrl_hdlr
->error
)
790 return ctrl_hdlr
->error
;
792 ov5675
->sd
.ctrl_handler
= ctrl_hdlr
;
797 static void ov5675_update_pad_format(const struct ov5675_mode
*mode
,
798 struct v4l2_mbus_framefmt
*fmt
)
800 fmt
->width
= mode
->width
;
801 fmt
->height
= mode
->height
;
802 fmt
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
803 fmt
->field
= V4L2_FIELD_NONE
;
806 static int ov5675_start_streaming(struct ov5675
*ov5675
)
808 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5675
->sd
);
809 const struct ov5675_reg_list
*reg_list
;
810 int link_freq_index
, ret
;
812 link_freq_index
= ov5675
->cur_mode
->link_freq_index
;
813 reg_list
= &link_freq_configs
[link_freq_index
].reg_list
;
814 ret
= ov5675_write_reg_list(ov5675
, reg_list
);
816 dev_err(&client
->dev
, "failed to set plls");
820 reg_list
= &ov5675
->cur_mode
->reg_list
;
821 ret
= ov5675_write_reg_list(ov5675
, reg_list
);
823 dev_err(&client
->dev
, "failed to set mode");
827 ret
= __v4l2_ctrl_handler_setup(ov5675
->sd
.ctrl_handler
);
831 ret
= ov5675_write_reg(ov5675
, OV5675_REG_MODE_SELECT
,
832 OV5675_REG_VALUE_08BIT
, OV5675_MODE_STREAMING
);
834 dev_err(&client
->dev
, "failed to set stream");
841 static void ov5675_stop_streaming(struct ov5675
*ov5675
)
843 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5675
->sd
);
845 if (ov5675_write_reg(ov5675
, OV5675_REG_MODE_SELECT
,
846 OV5675_REG_VALUE_08BIT
, OV5675_MODE_STANDBY
))
847 dev_err(&client
->dev
, "failed to set stream");
850 static int ov5675_set_stream(struct v4l2_subdev
*sd
, int enable
)
852 struct ov5675
*ov5675
= to_ov5675(sd
);
853 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
856 if (ov5675
->streaming
== enable
)
859 mutex_lock(&ov5675
->mutex
);
861 ret
= pm_runtime_get_sync(&client
->dev
);
863 pm_runtime_put_noidle(&client
->dev
);
864 mutex_unlock(&ov5675
->mutex
);
868 ret
= ov5675_start_streaming(ov5675
);
871 ov5675_stop_streaming(ov5675
);
872 pm_runtime_put(&client
->dev
);
875 ov5675_stop_streaming(ov5675
);
876 pm_runtime_put(&client
->dev
);
879 ov5675
->streaming
= enable
;
880 mutex_unlock(&ov5675
->mutex
);
885 static int __maybe_unused
ov5675_suspend(struct device
*dev
)
887 struct i2c_client
*client
= to_i2c_client(dev
);
888 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
889 struct ov5675
*ov5675
= to_ov5675(sd
);
891 mutex_lock(&ov5675
->mutex
);
892 if (ov5675
->streaming
)
893 ov5675_stop_streaming(ov5675
);
895 mutex_unlock(&ov5675
->mutex
);
900 static int __maybe_unused
ov5675_resume(struct device
*dev
)
902 struct i2c_client
*client
= to_i2c_client(dev
);
903 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
904 struct ov5675
*ov5675
= to_ov5675(sd
);
907 mutex_lock(&ov5675
->mutex
);
908 if (ov5675
->streaming
) {
909 ret
= ov5675_start_streaming(ov5675
);
911 ov5675
->streaming
= false;
912 ov5675_stop_streaming(ov5675
);
913 mutex_unlock(&ov5675
->mutex
);
918 mutex_unlock(&ov5675
->mutex
);
923 static int ov5675_set_format(struct v4l2_subdev
*sd
,
924 struct v4l2_subdev_pad_config
*cfg
,
925 struct v4l2_subdev_format
*fmt
)
927 struct ov5675
*ov5675
= to_ov5675(sd
);
928 const struct ov5675_mode
*mode
;
929 s32 vblank_def
, h_blank
;
931 mode
= v4l2_find_nearest_size(supported_modes
,
932 ARRAY_SIZE(supported_modes
), width
,
933 height
, fmt
->format
.width
,
936 mutex_lock(&ov5675
->mutex
);
937 ov5675_update_pad_format(mode
, &fmt
->format
);
938 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
939 *v4l2_subdev_get_try_format(sd
, cfg
, fmt
->pad
) = fmt
->format
;
941 ov5675
->cur_mode
= mode
;
942 __v4l2_ctrl_s_ctrl(ov5675
->link_freq
, mode
->link_freq_index
);
943 __v4l2_ctrl_s_ctrl_int64(ov5675
->pixel_rate
,
944 to_pixel_rate(mode
->link_freq_index
));
946 /* Update limits and set FPS to default */
947 vblank_def
= mode
->vts_def
- mode
->height
;
948 __v4l2_ctrl_modify_range(ov5675
->vblank
,
949 mode
->vts_min
- mode
->height
,
950 OV5675_VTS_MAX
- mode
->height
, 1,
952 __v4l2_ctrl_s_ctrl(ov5675
->vblank
, vblank_def
);
953 h_blank
= to_pixels_per_line(mode
->hts
, mode
->link_freq_index
) -
955 __v4l2_ctrl_modify_range(ov5675
->hblank
, h_blank
, h_blank
, 1,
959 mutex_unlock(&ov5675
->mutex
);
964 static int ov5675_get_format(struct v4l2_subdev
*sd
,
965 struct v4l2_subdev_pad_config
*cfg
,
966 struct v4l2_subdev_format
*fmt
)
968 struct ov5675
*ov5675
= to_ov5675(sd
);
970 mutex_lock(&ov5675
->mutex
);
971 if (fmt
->which
== V4L2_SUBDEV_FORMAT_TRY
)
972 fmt
->format
= *v4l2_subdev_get_try_format(&ov5675
->sd
, cfg
,
975 ov5675_update_pad_format(ov5675
->cur_mode
, &fmt
->format
);
977 mutex_unlock(&ov5675
->mutex
);
982 static int ov5675_enum_mbus_code(struct v4l2_subdev
*sd
,
983 struct v4l2_subdev_pad_config
*cfg
,
984 struct v4l2_subdev_mbus_code_enum
*code
)
989 code
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
994 static int ov5675_enum_frame_size(struct v4l2_subdev
*sd
,
995 struct v4l2_subdev_pad_config
*cfg
,
996 struct v4l2_subdev_frame_size_enum
*fse
)
998 if (fse
->index
>= ARRAY_SIZE(supported_modes
))
1001 if (fse
->code
!= MEDIA_BUS_FMT_SGRBG10_1X10
)
1004 fse
->min_width
= supported_modes
[fse
->index
].width
;
1005 fse
->max_width
= fse
->min_width
;
1006 fse
->min_height
= supported_modes
[fse
->index
].height
;
1007 fse
->max_height
= fse
->min_height
;
1012 static int ov5675_open(struct v4l2_subdev
*sd
, struct v4l2_subdev_fh
*fh
)
1014 struct ov5675
*ov5675
= to_ov5675(sd
);
1016 mutex_lock(&ov5675
->mutex
);
1017 ov5675_update_pad_format(&supported_modes
[0],
1018 v4l2_subdev_get_try_format(sd
, fh
->pad
, 0));
1019 mutex_unlock(&ov5675
->mutex
);
1024 static const struct v4l2_subdev_video_ops ov5675_video_ops
= {
1025 .s_stream
= ov5675_set_stream
,
1028 static const struct v4l2_subdev_pad_ops ov5675_pad_ops
= {
1029 .set_fmt
= ov5675_set_format
,
1030 .get_fmt
= ov5675_get_format
,
1031 .enum_mbus_code
= ov5675_enum_mbus_code
,
1032 .enum_frame_size
= ov5675_enum_frame_size
,
1035 static const struct v4l2_subdev_ops ov5675_subdev_ops
= {
1036 .video
= &ov5675_video_ops
,
1037 .pad
= &ov5675_pad_ops
,
1040 static const struct media_entity_operations ov5675_subdev_entity_ops
= {
1041 .link_validate
= v4l2_subdev_link_validate
,
1044 static const struct v4l2_subdev_internal_ops ov5675_internal_ops
= {
1045 .open
= ov5675_open
,
1048 static int ov5675_identify_module(struct ov5675
*ov5675
)
1050 struct i2c_client
*client
= v4l2_get_subdevdata(&ov5675
->sd
);
1054 ret
= ov5675_read_reg(ov5675
, OV5675_REG_CHIP_ID
,
1055 OV5675_REG_VALUE_24BIT
, &val
);
1059 if (val
!= OV5675_CHIP_ID
) {
1060 dev_err(&client
->dev
, "chip id mismatch: %x!=%x",
1061 OV5675_CHIP_ID
, val
);
1068 static int ov5675_check_hwcfg(struct device
*dev
)
1070 struct fwnode_handle
*ep
;
1071 struct fwnode_handle
*fwnode
= dev_fwnode(dev
);
1072 struct v4l2_fwnode_endpoint bus_cfg
= {
1073 .bus_type
= V4L2_MBUS_CSI2_DPHY
1082 ret
= fwnode_property_read_u32(fwnode
, "clock-frequency", &mclk
);
1085 dev_err(dev
, "can't get clock frequency");
1089 if (mclk
!= OV5675_MCLK
) {
1090 dev_err(dev
, "external clock %d is not supported", mclk
);
1094 ep
= fwnode_graph_get_next_endpoint(fwnode
, NULL
);
1098 ret
= v4l2_fwnode_endpoint_alloc_parse(ep
, &bus_cfg
);
1099 fwnode_handle_put(ep
);
1103 if (bus_cfg
.bus
.mipi_csi2
.num_data_lanes
!= OV5675_DATA_LANES
) {
1104 dev_err(dev
, "number of CSI2 data lanes %d is not supported",
1105 bus_cfg
.bus
.mipi_csi2
.num_data_lanes
);
1107 goto check_hwcfg_error
;
1110 if (!bus_cfg
.nr_of_link_frequencies
) {
1111 dev_err(dev
, "no link frequencies defined");
1113 goto check_hwcfg_error
;
1116 for (i
= 0; i
< ARRAY_SIZE(link_freq_menu_items
); i
++) {
1117 for (j
= 0; j
< bus_cfg
.nr_of_link_frequencies
; j
++) {
1118 if (link_freq_menu_items
[i
] ==
1119 bus_cfg
.link_frequencies
[j
])
1123 if (j
== bus_cfg
.nr_of_link_frequencies
) {
1124 dev_err(dev
, "no link frequency %lld supported",
1125 link_freq_menu_items
[i
]);
1127 goto check_hwcfg_error
;
1132 v4l2_fwnode_endpoint_free(&bus_cfg
);
1137 static int ov5675_remove(struct i2c_client
*client
)
1139 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1140 struct ov5675
*ov5675
= to_ov5675(sd
);
1142 v4l2_async_unregister_subdev(sd
);
1143 media_entity_cleanup(&sd
->entity
);
1144 v4l2_ctrl_handler_free(sd
->ctrl_handler
);
1145 pm_runtime_disable(&client
->dev
);
1146 mutex_destroy(&ov5675
->mutex
);
1151 static int ov5675_probe(struct i2c_client
*client
)
1153 struct ov5675
*ov5675
;
1156 ret
= ov5675_check_hwcfg(&client
->dev
);
1158 dev_err(&client
->dev
, "failed to check HW configuration: %d",
1163 ov5675
= devm_kzalloc(&client
->dev
, sizeof(*ov5675
), GFP_KERNEL
);
1167 v4l2_i2c_subdev_init(&ov5675
->sd
, client
, &ov5675_subdev_ops
);
1168 ret
= ov5675_identify_module(ov5675
);
1170 dev_err(&client
->dev
, "failed to find sensor: %d", ret
);
1174 mutex_init(&ov5675
->mutex
);
1175 ov5675
->cur_mode
= &supported_modes
[0];
1176 ret
= ov5675_init_controls(ov5675
);
1178 dev_err(&client
->dev
, "failed to init controls: %d", ret
);
1179 goto probe_error_v4l2_ctrl_handler_free
;
1182 ov5675
->sd
.internal_ops
= &ov5675_internal_ops
;
1183 ov5675
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1184 ov5675
->sd
.entity
.ops
= &ov5675_subdev_entity_ops
;
1185 ov5675
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1186 ov5675
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1187 ret
= media_entity_pads_init(&ov5675
->sd
.entity
, 1, &ov5675
->pad
);
1189 dev_err(&client
->dev
, "failed to init entity pads: %d", ret
);
1190 goto probe_error_v4l2_ctrl_handler_free
;
1193 ret
= v4l2_async_register_subdev_sensor_common(&ov5675
->sd
);
1195 dev_err(&client
->dev
, "failed to register V4L2 subdev: %d",
1197 goto probe_error_media_entity_cleanup
;
1201 * Device is already turned on by i2c-core with ACPI domain PM.
1202 * Enable runtime PM and turn off the device.
1204 pm_runtime_set_active(&client
->dev
);
1205 pm_runtime_enable(&client
->dev
);
1206 pm_runtime_idle(&client
->dev
);
1210 probe_error_media_entity_cleanup
:
1211 media_entity_cleanup(&ov5675
->sd
.entity
);
1213 probe_error_v4l2_ctrl_handler_free
:
1214 v4l2_ctrl_handler_free(ov5675
->sd
.ctrl_handler
);
1215 mutex_destroy(&ov5675
->mutex
);
1220 static const struct dev_pm_ops ov5675_pm_ops
= {
1221 SET_SYSTEM_SLEEP_PM_OPS(ov5675_suspend
, ov5675_resume
)
1225 static const struct acpi_device_id ov5675_acpi_ids
[] = {
1230 MODULE_DEVICE_TABLE(acpi
, ov5675_acpi_ids
);
1233 static struct i2c_driver ov5675_i2c_driver
= {
1236 .pm
= &ov5675_pm_ops
,
1237 .acpi_match_table
= ACPI_PTR(ov5675_acpi_ids
),
1239 .probe_new
= ov5675_probe
,
1240 .remove
= ov5675_remove
,
1243 module_i2c_driver(ov5675_i2c_driver
);
1245 MODULE_AUTHOR("Shawn Tu <shawnx.tu@intel.com>");
1246 MODULE_DESCRIPTION("OmniVision OV5675 sensor driver");
1247 MODULE_LICENSE("GPL v2");