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_DIGITAL_GAIN 0x350a
54 #define OV5675_REG_MWB_R_GAIN 0x5019
55 #define OV5675_REG_MWB_G_GAIN 0x501b
56 #define OV5675_REG_MWB_B_GAIN 0x501d
57 #define OV5675_DGTL_GAIN_MIN 1024
58 #define OV5675_DGTL_GAIN_MAX 4095
59 #define OV5675_DGTL_GAIN_STEP 1
60 #define OV5675_DGTL_GAIN_DEFAULT 1024
63 #define OV5675_REG_GROUP_ACCESS 0x3208
64 #define OV5675_GROUP_HOLD_START 0x0
65 #define OV5675_GROUP_HOLD_END 0x10
66 #define OV5675_GROUP_HOLD_LAUNCH 0xa0
68 /* Test Pattern Control */
69 #define OV5675_REG_TEST_PATTERN 0x4503
70 #define OV5675_TEST_PATTERN_ENABLE BIT(7)
71 #define OV5675_TEST_PATTERN_BAR_SHIFT 2
73 /* Flip Mirror Controls from sensor */
74 #define OV5675_REG_FORMAT1 0x3820
75 #define OV5675_REG_FORMAT2 0x373d
77 #define to_ov5675(_sd) container_of(_sd, struct ov5675, sd)
80 OV5675_LINK_FREQ_900MBPS,
88 struct ov5675_reg_list {
90 const struct ov5675_reg *regs;
93 struct ov5675_link_freq_config {
94 const struct ov5675_reg_list reg_list;
98 /* Frame width in pixels */
101 /* Frame height in pixels */
104 /* Horizontal timining size */
107 /* Default vertical timining size */
110 /* Min vertical timining size */
113 /* Link frequency needed for this resolution */
116 /* Sensor register settings for this resolution */
117 const struct ov5675_reg_list reg_list;
120 static const struct ov5675_reg mipi_data_rate_900mbps[] = {
129 static const struct ov5675_reg mode_2592x1944_regs[] = {
282 static const struct ov5675_reg mode_1296x972_regs[] = {
435 static const char * const ov5675_test_pattern_menu[] = {
437 "Standard Color Bar",
438 "Top-Bottom Darker Color Bar",
439 "Right-Left Darker Color Bar",
440 "Bottom-Top Darker Color Bar"
443 static const s64 link_freq_menu_items[] = {
444 OV5675_LINK_FREQ_450MHZ,
447 static const struct ov5675_link_freq_config link_freq_configs[] = {
448 [OV5675_LINK_FREQ_900MBPS] = {
450 .num_of_regs = ARRAY_SIZE(mipi_data_rate_900mbps),
451 .regs = mipi_data_rate_900mbps,
456 static const struct ov5675_mode supported_modes[] = {
461 .vts_def = OV5675_VTS_30FPS,
462 .vts_min = OV5675_VTS_30FPS_MIN,
464 .num_of_regs = ARRAY_SIZE(mode_2592x1944_regs),
465 .regs = mode_2592x1944_regs,
467 .link_freq_index = OV5675_LINK_FREQ_900MBPS,
473 .vts_def = OV5675_VTS_30FPS,
474 .vts_min = OV5675_VTS_30FPS_MIN,
476 .num_of_regs = ARRAY_SIZE(mode_1296x972_regs),
477 .regs = mode_1296x972_regs,
479 .link_freq_index = OV5675_LINK_FREQ_900MBPS,
484 struct v4l2_subdev sd;
485 struct media_pad pad;
486 struct v4l2_ctrl_handler ctrl_handler;
489 struct v4l2_ctrl *link_freq;
490 struct v4l2_ctrl *pixel_rate;
491 struct v4l2_ctrl *vblank;
492 struct v4l2_ctrl *hblank;
493 struct v4l2_ctrl *exposure;
496 const struct ov5675_mode *cur_mode;
498 /* To serialize asynchronus callbacks */
501 /* Streaming on/off */
504 /* True if the device has been identified */
508 static u64 to_pixel_rate(u32 f_index)
510 u64 pixel_rate = link_freq_menu_items[f_index] * 2 * OV5675_DATA_LANES;
512 do_div(pixel_rate, OV5675_RGB_DEPTH);
517 static u64 to_pixels_per_line(u32 hts, u32 f_index)
519 u64 ppl = hts * to_pixel_rate(f_index);
521 do_div(ppl, OV5675_SCLK);
526 static int ov5675_read_reg(struct ov5675 *ov5675, u16 reg, u16 len, u32 *val)
528 struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd);
529 struct i2c_msg msgs[2];
531 u8 data_buf[4] = {0};
537 put_unaligned_be16(reg, addr_buf);
538 msgs[0].addr = client->addr;
540 msgs[0].len = sizeof(addr_buf);
541 msgs[0].buf = addr_buf;
542 msgs[1].addr = client->addr;
543 msgs[1].flags = I2C_M_RD;
545 msgs[1].buf = &data_buf[4 - len];
547 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
548 if (ret != ARRAY_SIZE(msgs))
551 *val = get_unaligned_be32(data_buf);
556 static int ov5675_write_reg(struct ov5675 *ov5675, u16 reg, u16 len, u32 val)
558 struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd);
564 put_unaligned_be16(reg, buf);
565 put_unaligned_be32(val << 8 * (4 - len), buf + 2);
566 if (i2c_master_send(client, buf, len + 2) != len + 2)
572 static int ov5675_write_reg_list(struct ov5675 *ov5675,
573 const struct ov5675_reg_list *r_list)
575 struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd);
579 for (i = 0; i < r_list->num_of_regs; i++) {
580 ret = ov5675_write_reg(ov5675, r_list->regs[i].address, 1,
581 r_list->regs[i].val);
583 dev_err_ratelimited(&client->dev,
584 "failed to write reg 0x%4.4x. error = %d",
585 r_list->regs[i].address, ret);
593 static int ov5675_update_digital_gain(struct ov5675 *ov5675, u32 d_gain)
597 ret = ov5675_write_reg(ov5675, OV5675_REG_GROUP_ACCESS,
598 OV5675_REG_VALUE_08BIT,
599 OV5675_GROUP_HOLD_START);
603 ret = ov5675_write_reg(ov5675, OV5675_REG_MWB_R_GAIN,
604 OV5675_REG_VALUE_16BIT, d_gain);
608 ret = ov5675_write_reg(ov5675, OV5675_REG_MWB_G_GAIN,
609 OV5675_REG_VALUE_16BIT, d_gain);
613 ret = ov5675_write_reg(ov5675, OV5675_REG_MWB_B_GAIN,
614 OV5675_REG_VALUE_16BIT, d_gain);
618 ret = ov5675_write_reg(ov5675, OV5675_REG_GROUP_ACCESS,
619 OV5675_REG_VALUE_08BIT,
620 OV5675_GROUP_HOLD_END);
624 ret = ov5675_write_reg(ov5675, OV5675_REG_GROUP_ACCESS,
625 OV5675_REG_VALUE_08BIT,
626 OV5675_GROUP_HOLD_LAUNCH);
630 static int ov5675_test_pattern(struct ov5675 *ov5675, u32 pattern)
633 pattern = (pattern - 1) << OV5675_TEST_PATTERN_BAR_SHIFT |
634 OV5675_TEST_PATTERN_ENABLE;
636 return ov5675_write_reg(ov5675, OV5675_REG_TEST_PATTERN,
637 OV5675_REG_VALUE_08BIT, pattern);
641 * OV5675 supports keeping the pixel order by mirror and flip function
642 * The Bayer order isn't affected by the flip controls
644 static int ov5675_set_ctrl_hflip(struct ov5675 *ov5675, u32 ctrl_val)
649 ret = ov5675_read_reg(ov5675, OV5675_REG_FORMAT1,
650 OV5675_REG_VALUE_08BIT, &val);
654 return ov5675_write_reg(ov5675, OV5675_REG_FORMAT1,
655 OV5675_REG_VALUE_08BIT,
656 ctrl_val ? val & ~BIT(3) : val | BIT(3));
659 static int ov5675_set_ctrl_vflip(struct ov5675 *ov5675, u8 ctrl_val)
664 ret = ov5675_read_reg(ov5675, OV5675_REG_FORMAT1,
665 OV5675_REG_VALUE_08BIT, &val);
669 ret = ov5675_write_reg(ov5675, OV5675_REG_FORMAT1,
670 OV5675_REG_VALUE_08BIT,
671 ctrl_val ? val | BIT(4) | BIT(5) : val & ~BIT(4) & ~BIT(5));
676 ret = ov5675_read_reg(ov5675, OV5675_REG_FORMAT2,
677 OV5675_REG_VALUE_08BIT, &val);
682 return ov5675_write_reg(ov5675, OV5675_REG_FORMAT2,
683 OV5675_REG_VALUE_08BIT,
684 ctrl_val ? val | BIT(1) : val & ~BIT(1));
687 static int ov5675_set_ctrl(struct v4l2_ctrl *ctrl)
689 struct ov5675 *ov5675 = container_of(ctrl->handler,
690 struct ov5675, ctrl_handler);
691 struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd);
695 /* Propagate change of current control to all related controls */
696 if (ctrl->id == V4L2_CID_VBLANK) {
697 /* Update max exposure while meeting expected vblanking */
698 exposure_max = ov5675->cur_mode->height + ctrl->val -
699 OV5675_EXPOSURE_MAX_MARGIN;
700 __v4l2_ctrl_modify_range(ov5675->exposure,
701 ov5675->exposure->minimum,
702 exposure_max, ov5675->exposure->step,
706 /* V4L2 controls values will be applied only when power is already up */
707 if (!pm_runtime_get_if_in_use(&client->dev))
711 case V4L2_CID_ANALOGUE_GAIN:
712 ret = ov5675_write_reg(ov5675, OV5675_REG_ANALOG_GAIN,
713 OV5675_REG_VALUE_16BIT, ctrl->val);
716 case V4L2_CID_DIGITAL_GAIN:
717 ret = ov5675_update_digital_gain(ov5675, ctrl->val);
720 case V4L2_CID_EXPOSURE:
721 /* 4 least significant bits of expsoure are fractional part
723 * for ov5675, the unit of exposure is differnt from other
724 * OmniVision sensors, its exposure value is twice of the
725 * register value, the exposure should be divided by 2 before
726 * set register, e.g. val << 3.
728 ret = ov5675_write_reg(ov5675, OV5675_REG_EXPOSURE,
729 OV5675_REG_VALUE_24BIT, ctrl->val << 3);
732 case V4L2_CID_VBLANK:
733 ret = ov5675_write_reg(ov5675, OV5675_REG_VTS,
734 OV5675_REG_VALUE_16BIT,
735 ov5675->cur_mode->height + ctrl->val +
739 case V4L2_CID_TEST_PATTERN:
740 ret = ov5675_test_pattern(ov5675, ctrl->val);
744 ov5675_set_ctrl_hflip(ov5675, ctrl->val);
748 ov5675_set_ctrl_vflip(ov5675, ctrl->val);
756 pm_runtime_put(&client->dev);
761 static const struct v4l2_ctrl_ops ov5675_ctrl_ops = {
762 .s_ctrl = ov5675_set_ctrl,
765 static int ov5675_init_controls(struct ov5675 *ov5675)
767 struct v4l2_ctrl_handler *ctrl_hdlr;
768 s64 exposure_max, h_blank;
771 ctrl_hdlr = &ov5675->ctrl_handler;
772 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 8);
776 ctrl_hdlr->lock = &ov5675->mutex;
777 ov5675->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr, &ov5675_ctrl_ops,
779 ARRAY_SIZE(link_freq_menu_items) - 1,
780 0, link_freq_menu_items);
781 if (ov5675->link_freq)
782 ov5675->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
784 ov5675->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops,
785 V4L2_CID_PIXEL_RATE, 0,
786 to_pixel_rate(OV5675_LINK_FREQ_900MBPS),
788 to_pixel_rate(OV5675_LINK_FREQ_900MBPS));
789 ov5675->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops,
791 ov5675->cur_mode->vts_min - ov5675->cur_mode->height,
792 OV5675_VTS_MAX - ov5675->cur_mode->height, 1,
793 ov5675->cur_mode->vts_def - ov5675->cur_mode->height);
794 h_blank = to_pixels_per_line(ov5675->cur_mode->hts,
795 ov5675->cur_mode->link_freq_index) - ov5675->cur_mode->width;
796 ov5675->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops,
797 V4L2_CID_HBLANK, h_blank, h_blank, 1,
800 ov5675->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
802 v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
803 OV5675_ANAL_GAIN_MIN, OV5675_ANAL_GAIN_MAX,
804 OV5675_ANAL_GAIN_STEP, OV5675_ANAL_GAIN_MIN);
805 v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
806 OV5675_DGTL_GAIN_MIN, OV5675_DGTL_GAIN_MAX,
807 OV5675_DGTL_GAIN_STEP, OV5675_DGTL_GAIN_DEFAULT);
808 exposure_max = (ov5675->cur_mode->vts_def - OV5675_EXPOSURE_MAX_MARGIN);
809 ov5675->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops,
811 OV5675_EXPOSURE_MIN, exposure_max,
812 OV5675_EXPOSURE_STEP,
814 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &ov5675_ctrl_ops,
815 V4L2_CID_TEST_PATTERN,
816 ARRAY_SIZE(ov5675_test_pattern_menu) - 1,
817 0, 0, ov5675_test_pattern_menu);
818 v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops,
819 V4L2_CID_HFLIP, 0, 1, 1, 0);
820 v4l2_ctrl_new_std(ctrl_hdlr, &ov5675_ctrl_ops,
821 V4L2_CID_VFLIP, 0, 1, 1, 0);
823 if (ctrl_hdlr->error) {
824 v4l2_ctrl_handler_free(ctrl_hdlr);
825 return ctrl_hdlr->error;
828 ov5675->sd.ctrl_handler = ctrl_hdlr;
833 static void ov5675_update_pad_format(const struct ov5675_mode *mode,
834 struct v4l2_mbus_framefmt *fmt)
836 fmt->width = mode->width;
837 fmt->height = mode->height;
838 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
839 fmt->field = V4L2_FIELD_NONE;
842 static int ov5675_identify_module(struct ov5675 *ov5675)
844 struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd);
848 if (ov5675->identified)
851 ret = ov5675_read_reg(ov5675, OV5675_REG_CHIP_ID,
852 OV5675_REG_VALUE_24BIT, &val);
856 if (val != OV5675_CHIP_ID) {
857 dev_err(&client->dev, "chip id mismatch: %x!=%x",
858 OV5675_CHIP_ID, val);
862 ov5675->identified = true;
867 static int ov5675_start_streaming(struct ov5675 *ov5675)
869 struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd);
870 const struct ov5675_reg_list *reg_list;
871 int link_freq_index, ret;
873 ret = ov5675_identify_module(ov5675);
877 link_freq_index = ov5675->cur_mode->link_freq_index;
878 reg_list = &link_freq_configs[link_freq_index].reg_list;
879 ret = ov5675_write_reg_list(ov5675, reg_list);
881 dev_err(&client->dev, "failed to set plls");
885 reg_list = &ov5675->cur_mode->reg_list;
886 ret = ov5675_write_reg_list(ov5675, reg_list);
888 dev_err(&client->dev, "failed to set mode");
892 ret = __v4l2_ctrl_handler_setup(ov5675->sd.ctrl_handler);
896 ret = ov5675_write_reg(ov5675, OV5675_REG_MODE_SELECT,
897 OV5675_REG_VALUE_08BIT, OV5675_MODE_STREAMING);
899 dev_err(&client->dev, "failed to set stream");
906 static void ov5675_stop_streaming(struct ov5675 *ov5675)
908 struct i2c_client *client = v4l2_get_subdevdata(&ov5675->sd);
910 if (ov5675_write_reg(ov5675, OV5675_REG_MODE_SELECT,
911 OV5675_REG_VALUE_08BIT, OV5675_MODE_STANDBY))
912 dev_err(&client->dev, "failed to set stream");
915 static int ov5675_set_stream(struct v4l2_subdev *sd, int enable)
917 struct ov5675 *ov5675 = to_ov5675(sd);
918 struct i2c_client *client = v4l2_get_subdevdata(sd);
921 if (ov5675->streaming == enable)
924 mutex_lock(&ov5675->mutex);
926 ret = pm_runtime_resume_and_get(&client->dev);
928 mutex_unlock(&ov5675->mutex);
932 ret = ov5675_start_streaming(ov5675);
935 ov5675_stop_streaming(ov5675);
936 pm_runtime_put(&client->dev);
939 ov5675_stop_streaming(ov5675);
940 pm_runtime_put(&client->dev);
943 ov5675->streaming = enable;
944 mutex_unlock(&ov5675->mutex);
949 static int __maybe_unused ov5675_suspend(struct device *dev)
951 struct v4l2_subdev *sd = dev_get_drvdata(dev);
952 struct ov5675 *ov5675 = to_ov5675(sd);
954 mutex_lock(&ov5675->mutex);
955 if (ov5675->streaming)
956 ov5675_stop_streaming(ov5675);
958 mutex_unlock(&ov5675->mutex);
963 static int __maybe_unused ov5675_resume(struct device *dev)
965 struct v4l2_subdev *sd = dev_get_drvdata(dev);
966 struct ov5675 *ov5675 = to_ov5675(sd);
969 mutex_lock(&ov5675->mutex);
970 if (ov5675->streaming) {
971 ret = ov5675_start_streaming(ov5675);
973 ov5675->streaming = false;
974 ov5675_stop_streaming(ov5675);
975 mutex_unlock(&ov5675->mutex);
980 mutex_unlock(&ov5675->mutex);
985 static int ov5675_set_format(struct v4l2_subdev *sd,
986 struct v4l2_subdev_state *sd_state,
987 struct v4l2_subdev_format *fmt)
989 struct ov5675 *ov5675 = to_ov5675(sd);
990 const struct ov5675_mode *mode;
991 s32 vblank_def, h_blank;
993 mode = v4l2_find_nearest_size(supported_modes,
994 ARRAY_SIZE(supported_modes), width,
995 height, fmt->format.width,
998 mutex_lock(&ov5675->mutex);
999 ov5675_update_pad_format(mode, &fmt->format);
1000 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1001 *v4l2_subdev_get_try_format(sd, sd_state, fmt->pad) = fmt->format;
1003 ov5675->cur_mode = mode;
1004 __v4l2_ctrl_s_ctrl(ov5675->link_freq, mode->link_freq_index);
1005 __v4l2_ctrl_s_ctrl_int64(ov5675->pixel_rate,
1006 to_pixel_rate(mode->link_freq_index));
1008 /* Update limits and set FPS to default */
1009 vblank_def = mode->vts_def - mode->height;
1010 __v4l2_ctrl_modify_range(ov5675->vblank,
1011 mode->vts_min - mode->height,
1012 OV5675_VTS_MAX - mode->height, 1,
1014 __v4l2_ctrl_s_ctrl(ov5675->vblank, vblank_def);
1015 h_blank = to_pixels_per_line(mode->hts, mode->link_freq_index) -
1017 __v4l2_ctrl_modify_range(ov5675->hblank, h_blank, h_blank, 1,
1021 mutex_unlock(&ov5675->mutex);
1026 static int ov5675_get_format(struct v4l2_subdev *sd,
1027 struct v4l2_subdev_state *sd_state,
1028 struct v4l2_subdev_format *fmt)
1030 struct ov5675 *ov5675 = to_ov5675(sd);
1032 mutex_lock(&ov5675->mutex);
1033 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
1034 fmt->format = *v4l2_subdev_get_try_format(&ov5675->sd,
1038 ov5675_update_pad_format(ov5675->cur_mode, &fmt->format);
1040 mutex_unlock(&ov5675->mutex);
1045 static int ov5675_enum_mbus_code(struct v4l2_subdev *sd,
1046 struct v4l2_subdev_state *sd_state,
1047 struct v4l2_subdev_mbus_code_enum *code)
1049 if (code->index > 0)
1052 code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1057 static int ov5675_enum_frame_size(struct v4l2_subdev *sd,
1058 struct v4l2_subdev_state *sd_state,
1059 struct v4l2_subdev_frame_size_enum *fse)
1061 if (fse->index >= ARRAY_SIZE(supported_modes))
1064 if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
1067 fse->min_width = supported_modes[fse->index].width;
1068 fse->max_width = fse->min_width;
1069 fse->min_height = supported_modes[fse->index].height;
1070 fse->max_height = fse->min_height;
1075 static int ov5675_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1077 struct ov5675 *ov5675 = to_ov5675(sd);
1079 mutex_lock(&ov5675->mutex);
1080 ov5675_update_pad_format(&supported_modes[0],
1081 v4l2_subdev_get_try_format(sd, fh->state, 0));
1082 mutex_unlock(&ov5675->mutex);
1087 static const struct v4l2_subdev_video_ops ov5675_video_ops = {
1088 .s_stream = ov5675_set_stream,
1091 static const struct v4l2_subdev_pad_ops ov5675_pad_ops = {
1092 .set_fmt = ov5675_set_format,
1093 .get_fmt = ov5675_get_format,
1094 .enum_mbus_code = ov5675_enum_mbus_code,
1095 .enum_frame_size = ov5675_enum_frame_size,
1098 static const struct v4l2_subdev_ops ov5675_subdev_ops = {
1099 .video = &ov5675_video_ops,
1100 .pad = &ov5675_pad_ops,
1103 static const struct media_entity_operations ov5675_subdev_entity_ops = {
1104 .link_validate = v4l2_subdev_link_validate,
1107 static const struct v4l2_subdev_internal_ops ov5675_internal_ops = {
1108 .open = ov5675_open,
1111 static int ov5675_check_hwcfg(struct device *dev)
1113 struct fwnode_handle *ep;
1114 struct fwnode_handle *fwnode = dev_fwnode(dev);
1115 struct v4l2_fwnode_endpoint bus_cfg = {
1116 .bus_type = V4L2_MBUS_CSI2_DPHY
1125 ret = fwnode_property_read_u32(fwnode, "clock-frequency", &mclk);
1128 dev_err(dev, "can't get clock frequency");
1132 if (mclk != OV5675_MCLK) {
1133 dev_err(dev, "external clock %d is not supported", mclk);
1137 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
1141 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
1142 fwnode_handle_put(ep);
1146 if (bus_cfg.bus.mipi_csi2.num_data_lanes != OV5675_DATA_LANES) {
1147 dev_err(dev, "number of CSI2 data lanes %d is not supported",
1148 bus_cfg.bus.mipi_csi2.num_data_lanes);
1150 goto check_hwcfg_error;
1153 if (!bus_cfg.nr_of_link_frequencies) {
1154 dev_err(dev, "no link frequencies defined");
1156 goto check_hwcfg_error;
1159 for (i = 0; i < ARRAY_SIZE(link_freq_menu_items); i++) {
1160 for (j = 0; j < bus_cfg.nr_of_link_frequencies; j++) {
1161 if (link_freq_menu_items[i] ==
1162 bus_cfg.link_frequencies[j])
1166 if (j == bus_cfg.nr_of_link_frequencies) {
1167 dev_err(dev, "no link frequency %lld supported",
1168 link_freq_menu_items[i]);
1170 goto check_hwcfg_error;
1175 v4l2_fwnode_endpoint_free(&bus_cfg);
1180 static void ov5675_remove(struct i2c_client *client)
1182 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1183 struct ov5675 *ov5675 = to_ov5675(sd);
1185 v4l2_async_unregister_subdev(sd);
1186 media_entity_cleanup(&sd->entity);
1187 v4l2_ctrl_handler_free(sd->ctrl_handler);
1188 pm_runtime_disable(&client->dev);
1189 mutex_destroy(&ov5675->mutex);
1192 static int ov5675_probe(struct i2c_client *client)
1194 struct ov5675 *ov5675;
1198 ret = ov5675_check_hwcfg(&client->dev);
1200 dev_err(&client->dev, "failed to check HW configuration: %d",
1205 ov5675 = devm_kzalloc(&client->dev, sizeof(*ov5675), GFP_KERNEL);
1209 v4l2_i2c_subdev_init(&ov5675->sd, client, &ov5675_subdev_ops);
1211 full_power = acpi_dev_state_d0(&client->dev);
1213 ret = ov5675_identify_module(ov5675);
1215 dev_err(&client->dev, "failed to find sensor: %d", ret);
1220 mutex_init(&ov5675->mutex);
1221 ov5675->cur_mode = &supported_modes[0];
1222 ret = ov5675_init_controls(ov5675);
1224 dev_err(&client->dev, "failed to init controls: %d", ret);
1225 goto probe_error_v4l2_ctrl_handler_free;
1228 ov5675->sd.internal_ops = &ov5675_internal_ops;
1229 ov5675->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1230 ov5675->sd.entity.ops = &ov5675_subdev_entity_ops;
1231 ov5675->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1232 ov5675->pad.flags = MEDIA_PAD_FL_SOURCE;
1233 ret = media_entity_pads_init(&ov5675->sd.entity, 1, &ov5675->pad);
1235 dev_err(&client->dev, "failed to init entity pads: %d", ret);
1236 goto probe_error_v4l2_ctrl_handler_free;
1239 ret = v4l2_async_register_subdev_sensor(&ov5675->sd);
1241 dev_err(&client->dev, "failed to register V4L2 subdev: %d",
1243 goto probe_error_media_entity_cleanup;
1247 * Device is already turned on by i2c-core with ACPI domain PM.
1248 * Enable runtime PM and turn off the device.
1251 /* Set the device's state to active if it's in D0 state. */
1253 pm_runtime_set_active(&client->dev);
1254 pm_runtime_enable(&client->dev);
1255 pm_runtime_idle(&client->dev);
1259 probe_error_media_entity_cleanup:
1260 media_entity_cleanup(&ov5675->sd.entity);
1262 probe_error_v4l2_ctrl_handler_free:
1263 v4l2_ctrl_handler_free(ov5675->sd.ctrl_handler);
1264 mutex_destroy(&ov5675->mutex);
1269 static const struct dev_pm_ops ov5675_pm_ops = {
1270 SET_SYSTEM_SLEEP_PM_OPS(ov5675_suspend, ov5675_resume)
1274 static const struct acpi_device_id ov5675_acpi_ids[] = {
1279 MODULE_DEVICE_TABLE(acpi, ov5675_acpi_ids);
1282 static struct i2c_driver ov5675_i2c_driver = {
1285 .pm = &ov5675_pm_ops,
1286 .acpi_match_table = ACPI_PTR(ov5675_acpi_ids),
1288 .probe_new = ov5675_probe,
1289 .remove = ov5675_remove,
1290 .flags = I2C_DRV_ACPI_WAIVE_D0_PROBE,
1293 module_i2c_driver(ov5675_i2c_driver);
1295 MODULE_AUTHOR("Shawn Tu <shawnx.tu@intel.com>");
1296 MODULE_DESCRIPTION("OmniVision OV5675 sensor driver");
1297 MODULE_LICENSE("GPL v2");