1 // SPDX-License-Identifier: GPL-2.0
3 * Omnivision OV2311 1600x1300 global shutter image sensor driver
4 * Copyright (C) 2022, Raspberry Pi (Trading) Ltd
6 * This driver is based on the OV9281 driver.
7 * Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd.
8 * Register configuration from
9 * https://github.com/ArduCAM/ArduCAM_USB_Camera_Shield/tree/master/Config/USB3.0_UC-425_Rev.C%2BUC-628_Rev.B/OV2311
10 * with additional exposure and gain register information from
11 * https://github.com/renesas-rcar/linux-bsp/tree/0cf6e36f5bf49e1c2aab87139ec5b588623c56f8/drivers/media/i2c/imagers
14 #include <linux/clk.h>
15 #include <linux/device.h>
16 #include <linux/delay.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/i2c.h>
19 #include <linux/module.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/regulator/consumer.h>
22 #include <media/media-entity.h>
23 #include <media/v4l2-async.h>
24 #include <media/v4l2-ctrls.h>
25 #include <media/v4l2-event.h>
26 #include <media/v4l2-fwnode.h>
27 #include <media/v4l2-subdev.h>
29 #define OV2311_LINK_FREQ 400000000
30 #define OV2311_LANES 2
32 /* pixel rate = link frequency * 2 * lanes / BITS_PER_SAMPLE */
33 #define OV2311_PIXEL_RATE_10BIT (OV2311_LINK_FREQ * 2 * \
35 #define OV2311_PIXEL_RATE_8BIT (OV2311_LINK_FREQ * 2 * \
37 #define OV2311_XVCLK_FREQ 24000000
39 #define CHIP_ID 0x2311
40 #define OV2311_REG_CHIP_ID 0x300a
42 #define OV2311_REG_CTRL_MODE 0x0100
43 #define OV2311_MODE_SW_STANDBY 0x0
44 #define OV2311_MODE_STREAMING BIT(0)
46 #define OV2311_REG_V_FLIP 0x3820
47 #define OV2311_REG_H_FLIP 0x3821
48 #define OV2311_FLIP_BIT BIT(2)
50 #define OV2311_REG_EXPOSURE 0x3501
51 #define OV2311_EXPOSURE_MIN 4
52 #define OV2311_EXPOSURE_STEP 1
53 #define OV2311_VTS_MAX 0xffff
55 #define OV2311_REG_GAIN_H 0x3508
56 #define OV2311_REG_GAIN_L 0x3509
57 #define OV2311_GAIN_H_MASK 0x07
58 #define OV2311_GAIN_H_SHIFT 8
59 #define OV2311_GAIN_L_MASK 0xff
60 #define OV2311_GAIN_MIN 0x100
61 #define OV2311_GAIN_MAX 0x780
62 #define OV2311_GAIN_STEP 1
63 #define OV2311_GAIN_DEFAULT OV2311_GAIN_MIN
65 #define OV2311_REG_TEST_PATTERN 0x5e00
66 #define OV2311_TEST_PATTERN_ENABLE 0x80
67 #define OV2311_TEST_PATTERN_DISABLE 0x0
69 #define OV2311_REG_VTS 0x380e
72 * OV2311 native and active pixel array size.
73 * Datasheet not available to confirm these values. renesas-rcar linux-bsp tree
76 #define OV2311_NATIVE_WIDTH 1616U
77 #define OV2311_NATIVE_HEIGHT 1316U
78 #define OV2311_PIXEL_ARRAY_LEFT 8U
79 #define OV2311_PIXEL_ARRAY_TOP 8U
80 #define OV2311_PIXEL_ARRAY_WIDTH 1600U
81 #define OV2311_PIXEL_ARRAY_HEIGHT 1300U
83 #define REG_NULL 0xFFFF
85 #define OV2311_REG_VALUE_08BIT 1
86 #define OV2311_REG_VALUE_16BIT 2
87 #define OV2311_REG_VALUE_24BIT 3
89 #define OV2311_NAME "ov2311"
91 static const char * const ov2311_supply_names[] = {
92 "avdd", /* Analog power */
93 "dovdd", /* Digital I/O power */
94 "dvdd", /* Digital core power */
97 #define OV2311_NUM_SUPPLIES ARRAY_SIZE(ov2311_supply_names)
110 struct v4l2_rect crop;
111 const struct regval *reg_list;
115 struct i2c_client *client;
117 struct gpio_desc *reset_gpio;
118 struct gpio_desc *pwdn_gpio;
119 struct regulator_bulk_data supplies[OV2311_NUM_SUPPLIES];
121 struct v4l2_subdev subdev;
122 struct media_pad pad;
123 struct v4l2_ctrl_handler ctrl_handler;
124 struct v4l2_ctrl *exposure;
125 struct v4l2_ctrl *hblank;
126 struct v4l2_ctrl *vblank;
127 struct v4l2_ctrl *pixel_rate;
129 * Mutex for serialized access:
130 * Protect sensor module set pad format and start/stop streaming safely.
134 /* Streaming on/off */
137 const struct ov2311_mode *cur_mode;
141 #define to_ov2311(sd) container_of(sd, struct ov2311, subdev)
145 * max_framerate 60fps for 10 bit, 74.6fps for 8 bit.
147 static const struct regval ov2311_common_regs[] = {
287 static const struct regval ov2311_1600x1300_regs[] = {
301 static const struct regval ov2311_1600x1080_regs[] = {
321 static const struct regval op_10bit[] = {
327 static const struct regval op_8bit[] = {
333 static const struct ov2311_mode supported_modes[] = {
338 .hts_def = (0x0388 * 2),/* Registers 0x380c / 0x380d * 2 */
339 .vts_def = 0x5c2, /* Registers 0x380e / 0x380f
343 .left = OV2311_PIXEL_ARRAY_LEFT,
344 .top = OV2311_PIXEL_ARRAY_TOP,
348 .reg_list = ov2311_1600x1300_regs,
354 .hts_def = (0x0388 * 2),/* Registers 0x380c / 0x380d * 2 */
355 .vts_def = 0x5c2, /* Registers 0x380e / 0x380f
359 .left = OV2311_PIXEL_ARRAY_LEFT,
360 .top = 110 + OV2311_PIXEL_ARRAY_TOP,
364 .reg_list = ov2311_1600x1080_regs,
368 static const s64 link_freq_menu_items[] = {
372 static const char * const ov2311_test_pattern_menu[] = {
374 "Vertical Color Bar Type 1",
375 "Vertical Color Bar Type 2",
376 "Vertical Color Bar Type 3",
377 "Vertical Color Bar Type 4"
380 /* Write registers up to 4 at a time */
381 static int ov2311_write_reg(struct i2c_client *client, u16 reg,
395 val_be = cpu_to_be32(val);
396 val_p = (u8 *)&val_be;
401 buf[buf_i++] = val_p[val_i++];
403 if (i2c_master_send(client, buf, len + 2) != len + 2)
409 static int ov2311_write_array(struct i2c_client *client,
410 const struct regval *regs)
415 for (i = 0; ret == 0 && regs[i].addr != REG_NULL; i++)
416 ret = ov2311_write_reg(client, regs[i].addr,
417 OV2311_REG_VALUE_08BIT, regs[i].val);
422 /* Read registers up to 4 at a time */
423 static int ov2311_read_reg(struct i2c_client *client, u16 reg, unsigned int len,
426 struct i2c_msg msgs[2];
429 __be16 reg_addr_be = cpu_to_be16(reg);
435 data_be_p = (u8 *)&data_be;
436 /* Write register address */
437 msgs[0].addr = client->addr;
440 msgs[0].buf = (u8 *)®_addr_be;
442 /* Read data from register */
443 msgs[1].addr = client->addr;
444 msgs[1].flags = I2C_M_RD;
446 msgs[1].buf = &data_be_p[4 - len];
448 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
449 if (ret != ARRAY_SIZE(msgs))
452 *val = be32_to_cpu(data_be);
457 static int ov2311_set_fmt(struct v4l2_subdev *sd,
458 struct v4l2_subdev_state *sd_state,
459 struct v4l2_subdev_format *fmt)
461 struct ov2311 *ov2311 = to_ov2311(sd);
462 const struct ov2311_mode *mode;
463 s64 h_blank, vblank_def, pixel_rate;
465 mutex_lock(&ov2311->mutex);
467 mode = v4l2_find_nearest_size(supported_modes,
468 ARRAY_SIZE(supported_modes),
472 if (fmt->format.code != MEDIA_BUS_FMT_Y8_1X8)
473 fmt->format.code = MEDIA_BUS_FMT_Y10_1X10;
474 fmt->format.width = mode->width;
475 fmt->format.height = mode->height;
476 fmt->format.field = V4L2_FIELD_NONE;
477 fmt->format.colorspace = V4L2_COLORSPACE_RAW;
478 fmt->format.ycbcr_enc =
479 V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->format.colorspace);
480 fmt->format.quantization =
481 V4L2_MAP_QUANTIZATION_DEFAULT(true, fmt->format.colorspace,
482 fmt->format.ycbcr_enc);
483 fmt->format.xfer_func =
484 V4L2_MAP_XFER_FUNC_DEFAULT(fmt->format.colorspace);
486 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
487 *v4l2_subdev_get_try_format(sd, sd_state, fmt->pad) =
490 ov2311->cur_mode = mode;
491 ov2311->code = fmt->format.code;
492 h_blank = mode->hts_def - mode->width;
493 __v4l2_ctrl_modify_range(ov2311->hblank, h_blank,
494 h_blank, 1, h_blank);
495 __v4l2_ctrl_s_ctrl(ov2311->hblank, h_blank);
496 vblank_def = mode->vts_def - mode->height;
497 __v4l2_ctrl_modify_range(ov2311->vblank, vblank_def,
498 OV2311_VTS_MAX - mode->height,
500 __v4l2_ctrl_s_ctrl(ov2311->vblank, vblank_def);
502 pixel_rate = (fmt->format.code == MEDIA_BUS_FMT_Y10_1X10) ?
503 OV2311_PIXEL_RATE_10BIT : OV2311_PIXEL_RATE_8BIT;
504 __v4l2_ctrl_modify_range(ov2311->pixel_rate, pixel_rate,
505 pixel_rate, 1, pixel_rate);
508 mutex_unlock(&ov2311->mutex);
513 static int ov2311_get_fmt(struct v4l2_subdev *sd,
514 struct v4l2_subdev_state *sd_state,
515 struct v4l2_subdev_format *fmt)
517 struct ov2311 *ov2311 = to_ov2311(sd);
518 const struct ov2311_mode *mode = ov2311->cur_mode;
520 mutex_lock(&ov2311->mutex);
521 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
522 fmt->format = *v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
524 fmt->format.width = mode->width;
525 fmt->format.height = mode->height;
526 fmt->format.code = ov2311->code;
527 fmt->format.field = V4L2_FIELD_NONE;
528 fmt->format.colorspace = V4L2_COLORSPACE_SRGB;
529 fmt->format.ycbcr_enc =
530 V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->format.colorspace);
531 fmt->format.quantization =
532 V4L2_MAP_QUANTIZATION_DEFAULT(true,
533 fmt->format.colorspace,
534 fmt->format.ycbcr_enc);
535 fmt->format.xfer_func =
536 V4L2_MAP_XFER_FUNC_DEFAULT(fmt->format.colorspace);
538 mutex_unlock(&ov2311->mutex);
543 static int ov2311_enum_mbus_code(struct v4l2_subdev *sd,
544 struct v4l2_subdev_state *sd_state,
545 struct v4l2_subdev_mbus_code_enum *code)
547 switch (code->index) {
551 code->code = MEDIA_BUS_FMT_Y10_1X10;
554 code->code = MEDIA_BUS_FMT_Y8_1X8;
561 static int ov2311_enum_frame_sizes(struct v4l2_subdev *sd,
562 struct v4l2_subdev_state *sd_state,
563 struct v4l2_subdev_frame_size_enum *fse)
565 if (fse->index >= ARRAY_SIZE(supported_modes))
568 if (fse->code != MEDIA_BUS_FMT_Y10_1X10 &&
569 fse->code != MEDIA_BUS_FMT_Y8_1X8)
572 fse->min_width = supported_modes[fse->index].width;
573 fse->max_width = supported_modes[fse->index].width;
574 fse->max_height = supported_modes[fse->index].height;
575 fse->min_height = supported_modes[fse->index].height;
580 static int ov2311_enable_test_pattern(struct ov2311 *ov2311, u32 pattern)
585 val = (pattern - 1) | OV2311_TEST_PATTERN_ENABLE;
587 val = OV2311_TEST_PATTERN_DISABLE;
589 return ov2311_write_reg(ov2311->client, OV2311_REG_TEST_PATTERN,
590 OV2311_REG_VALUE_08BIT, val);
593 static const struct v4l2_rect *
594 __ov2311_get_pad_crop(struct ov2311 *ov2311, struct v4l2_subdev_state *sd_state,
595 unsigned int pad, enum v4l2_subdev_format_whence which)
598 case V4L2_SUBDEV_FORMAT_TRY:
599 return v4l2_subdev_get_try_crop(&ov2311->subdev, sd_state, pad);
600 case V4L2_SUBDEV_FORMAT_ACTIVE:
601 return &ov2311->cur_mode->crop;
607 static int ov2311_get_selection(struct v4l2_subdev *sd,
608 struct v4l2_subdev_state *sd_state,
609 struct v4l2_subdev_selection *sel)
611 switch (sel->target) {
612 case V4L2_SEL_TGT_CROP: {
613 struct ov2311 *ov2311 = to_ov2311(sd);
615 mutex_lock(&ov2311->mutex);
616 sel->r = *__ov2311_get_pad_crop(ov2311, sd_state, sel->pad,
618 mutex_unlock(&ov2311->mutex);
623 case V4L2_SEL_TGT_NATIVE_SIZE:
626 sel->r.width = OV2311_NATIVE_WIDTH;
627 sel->r.height = OV2311_NATIVE_HEIGHT;
631 case V4L2_SEL_TGT_CROP_DEFAULT:
632 case V4L2_SEL_TGT_CROP_BOUNDS:
633 sel->r.top = OV2311_PIXEL_ARRAY_TOP;
634 sel->r.left = OV2311_PIXEL_ARRAY_LEFT;
635 sel->r.width = OV2311_PIXEL_ARRAY_WIDTH;
636 sel->r.height = OV2311_PIXEL_ARRAY_HEIGHT;
644 static int ov2311_start_stream(struct ov2311 *ov2311)
648 ret = ov2311_write_array(ov2311->client, ov2311_common_regs);
652 ret = ov2311_write_array(ov2311->client, ov2311->cur_mode->reg_list);
656 if (ov2311->code == MEDIA_BUS_FMT_Y10_1X10)
657 ret = ov2311_write_array(ov2311->client, op_10bit);
659 ret = ov2311_write_array(ov2311->client, op_8bit);
663 /* In case these controls are set before streaming */
664 mutex_unlock(&ov2311->mutex);
665 ret = v4l2_ctrl_handler_setup(&ov2311->ctrl_handler);
666 mutex_lock(&ov2311->mutex);
670 return ov2311_write_reg(ov2311->client, OV2311_REG_CTRL_MODE,
671 OV2311_REG_VALUE_08BIT, OV2311_MODE_STREAMING);
674 static int ov2311_stop_stream(struct ov2311 *ov2311)
676 return ov2311_write_reg(ov2311->client, OV2311_REG_CTRL_MODE,
677 OV2311_REG_VALUE_08BIT, OV2311_MODE_SW_STANDBY);
680 static int ov2311_s_stream(struct v4l2_subdev *sd, int enable)
682 struct ov2311 *ov2311 = to_ov2311(sd);
683 struct i2c_client *client = ov2311->client;
686 mutex_lock(&ov2311->mutex);
687 if (ov2311->streaming == enable) {
688 mutex_unlock(&ov2311->mutex);
693 ret = pm_runtime_resume_and_get(&client->dev);
695 goto unlock_and_return;
697 ret = ov2311_start_stream(ov2311);
699 v4l2_err(sd, "start stream failed while write regs\n");
700 pm_runtime_put(&client->dev);
701 goto unlock_and_return;
704 ov2311_stop_stream(ov2311);
705 pm_runtime_put(&client->dev);
708 ov2311->streaming = enable;
711 mutex_unlock(&ov2311->mutex);
716 static int ov2311_power_on(struct device *dev)
718 struct v4l2_subdev *sd = dev_get_drvdata(dev);
719 struct ov2311 *ov2311 = to_ov2311(sd);
722 ret = clk_set_rate(ov2311->xvclk, OV2311_XVCLK_FREQ);
724 dev_warn(dev, "Failed to set xvclk rate (24MHz)\n");
725 if (clk_get_rate(ov2311->xvclk) != OV2311_XVCLK_FREQ)
726 dev_warn(dev, "xvclk mismatched, modes are based on 24MHz - rate is %lu\n",
727 clk_get_rate(ov2311->xvclk));
729 ret = clk_prepare_enable(ov2311->xvclk);
731 dev_err(dev, "Failed to enable xvclk\n");
735 gpiod_set_value_cansleep(ov2311->reset_gpio, 0);
737 ret = regulator_bulk_enable(OV2311_NUM_SUPPLIES, ov2311->supplies);
739 dev_err(dev, "Failed to enable regulators\n");
743 gpiod_set_value_cansleep(ov2311->reset_gpio, 1);
745 usleep_range(500, 1000);
746 gpiod_set_value_cansleep(ov2311->pwdn_gpio, 1);
748 usleep_range(1000, 2000);
753 clk_disable_unprepare(ov2311->xvclk);
758 static int ov2311_power_off(struct device *dev)
760 struct v4l2_subdev *sd = dev_get_drvdata(dev);
761 struct ov2311 *ov2311 = to_ov2311(sd);
763 gpiod_set_value_cansleep(ov2311->pwdn_gpio, 0);
764 clk_disable_unprepare(ov2311->xvclk);
765 gpiod_set_value_cansleep(ov2311->reset_gpio, 0);
766 regulator_bulk_disable(OV2311_NUM_SUPPLIES, ov2311->supplies);
771 static int ov2311_runtime_resume(struct device *dev)
773 struct v4l2_subdev *sd = dev_get_drvdata(dev);
774 struct ov2311 *ov2311 = to_ov2311(sd);
777 if (ov2311->streaming) {
778 ret = ov2311_start_stream(ov2311);
785 ov2311_stop_stream(ov2311);
786 ov2311->streaming = 0;
790 static int ov2311_runtime_suspend(struct device *dev)
792 struct v4l2_subdev *sd = dev_get_drvdata(dev);
793 struct ov2311 *ov2311 = to_ov2311(sd);
795 if (ov2311->streaming)
796 ov2311_stop_stream(ov2311);
801 static int ov2311_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
803 struct ov2311 *ov2311 = to_ov2311(sd);
804 struct v4l2_mbus_framefmt *try_fmt =
805 v4l2_subdev_get_try_format(sd, fh->state, 0);
806 const struct ov2311_mode *def_mode = &supported_modes[0];
808 mutex_lock(&ov2311->mutex);
809 /* Initialize try_fmt */
810 try_fmt->width = def_mode->width;
811 try_fmt->height = def_mode->height;
812 try_fmt->code = MEDIA_BUS_FMT_Y10_1X10;
813 try_fmt->field = V4L2_FIELD_NONE;
814 try_fmt->colorspace = V4L2_COLORSPACE_RAW;
815 try_fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(try_fmt->colorspace);
816 try_fmt->quantization =
817 V4L2_MAP_QUANTIZATION_DEFAULT(true, try_fmt->colorspace,
819 try_fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(try_fmt->colorspace);
821 mutex_unlock(&ov2311->mutex);
822 /* No crop or compose */
827 static const struct dev_pm_ops ov2311_pm_ops = {
828 SET_RUNTIME_PM_OPS(ov2311_runtime_suspend, ov2311_runtime_resume, NULL)
829 SET_RUNTIME_PM_OPS(ov2311_power_off, ov2311_power_on, NULL)
832 static const struct v4l2_subdev_internal_ops ov2311_internal_ops = {
836 static const struct v4l2_subdev_core_ops ov2311_core_ops = {
837 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
838 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
841 static const struct v4l2_subdev_video_ops ov2311_video_ops = {
842 .s_stream = ov2311_s_stream,
845 static const struct v4l2_subdev_pad_ops ov2311_pad_ops = {
846 .enum_mbus_code = ov2311_enum_mbus_code,
847 .enum_frame_size = ov2311_enum_frame_sizes,
848 .get_fmt = ov2311_get_fmt,
849 .set_fmt = ov2311_set_fmt,
850 .get_selection = ov2311_get_selection,
853 static const struct v4l2_subdev_ops ov2311_subdev_ops = {
854 .core = &ov2311_core_ops,
855 .video = &ov2311_video_ops,
856 .pad = &ov2311_pad_ops,
859 static int ov2311_set_ctrl(struct v4l2_ctrl *ctrl)
861 struct ov2311 *ov2311 = container_of(ctrl->handler,
862 struct ov2311, ctrl_handler);
863 struct i2c_client *client = ov2311->client;
867 /* Propagate change of current control to all related controls */
869 case V4L2_CID_VBLANK:
870 /* Update max exposure while meeting expected vblanking */
871 max = ov2311->cur_mode->height + ctrl->val - 4;
872 __v4l2_ctrl_modify_range(ov2311->exposure,
873 ov2311->exposure->minimum, max,
874 ov2311->exposure->step,
875 ov2311->exposure->default_value);
879 if (pm_runtime_get(&client->dev) <= 0)
883 case V4L2_CID_EXPOSURE:
884 ret = ov2311_write_reg(ov2311->client, OV2311_REG_EXPOSURE,
885 OV2311_REG_VALUE_16BIT, ctrl->val);
887 case V4L2_CID_ANALOGUE_GAIN:
888 ret = ov2311_write_reg(ov2311->client, OV2311_REG_GAIN_H,
889 OV2311_REG_VALUE_08BIT,
890 (ctrl->val >> OV2311_GAIN_H_SHIFT) &
892 ret |= ov2311_write_reg(ov2311->client, OV2311_REG_GAIN_L,
893 OV2311_REG_VALUE_08BIT,
894 ctrl->val & OV2311_GAIN_L_MASK);
896 case V4L2_CID_VBLANK:
897 ret = ov2311_write_reg(ov2311->client, OV2311_REG_VTS,
898 OV2311_REG_VALUE_16BIT,
899 ctrl->val + ov2311->cur_mode->height);
901 case V4L2_CID_TEST_PATTERN:
902 ret = ov2311_enable_test_pattern(ov2311, ctrl->val);
905 ret = ov2311_write_reg(ov2311->client, OV2311_REG_H_FLIP,
906 OV2311_REG_VALUE_08BIT,
907 ctrl->val ? OV2311_FLIP_BIT : 0);
910 ret = ov2311_write_reg(ov2311->client, OV2311_REG_V_FLIP,
911 OV2311_REG_VALUE_08BIT,
912 ctrl->val ? OV2311_FLIP_BIT : 0);
915 dev_warn(&client->dev, "%s Unhandled id:0x%x, val:0x%x\n",
916 __func__, ctrl->id, ctrl->val);
920 pm_runtime_put(&client->dev);
925 static const struct v4l2_ctrl_ops ov2311_ctrl_ops = {
926 .s_ctrl = ov2311_set_ctrl,
929 static int ov2311_initialize_controls(struct ov2311 *ov2311)
931 struct v4l2_fwnode_device_properties props;
932 const struct ov2311_mode *mode;
933 struct v4l2_ctrl_handler *handler;
934 struct v4l2_ctrl *ctrl;
935 s64 exposure_max, vblank_def;
939 handler = &ov2311->ctrl_handler;
940 mode = ov2311->cur_mode;
941 ret = v4l2_ctrl_handler_init(handler, 11);
944 handler->lock = &ov2311->mutex;
946 ctrl = v4l2_ctrl_new_int_menu(handler, NULL, V4L2_CID_LINK_FREQ,
947 0, 0, link_freq_menu_items);
949 ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
951 ov2311->pixel_rate = v4l2_ctrl_new_std(handler, NULL,
953 OV2311_PIXEL_RATE_10BIT,
954 OV2311_PIXEL_RATE_10BIT, 1,
955 OV2311_PIXEL_RATE_10BIT);
957 h_blank = mode->hts_def - mode->width;
958 ov2311->hblank = v4l2_ctrl_new_std(handler, NULL, V4L2_CID_HBLANK,
959 h_blank, h_blank, 1, h_blank);
961 ov2311->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
963 vblank_def = mode->vts_def - mode->height;
964 ov2311->vblank = v4l2_ctrl_new_std(handler, &ov2311_ctrl_ops,
965 V4L2_CID_VBLANK, vblank_def,
966 OV2311_VTS_MAX - mode->height, 1,
969 exposure_max = mode->vts_def - 4;
970 ov2311->exposure = v4l2_ctrl_new_std(handler, &ov2311_ctrl_ops,
972 OV2311_EXPOSURE_MIN, exposure_max,
973 OV2311_EXPOSURE_STEP,
976 v4l2_ctrl_new_std(handler, &ov2311_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
977 OV2311_GAIN_MIN, OV2311_GAIN_MAX, OV2311_GAIN_STEP,
978 OV2311_GAIN_DEFAULT);
980 v4l2_ctrl_new_std_menu_items(handler, &ov2311_ctrl_ops,
981 V4L2_CID_TEST_PATTERN,
982 ARRAY_SIZE(ov2311_test_pattern_menu) - 1,
983 0, 0, ov2311_test_pattern_menu);
985 v4l2_ctrl_new_std(handler, &ov2311_ctrl_ops,
986 V4L2_CID_HFLIP, 0, 1, 1, 0);
988 v4l2_ctrl_new_std(handler, &ov2311_ctrl_ops,
989 V4L2_CID_VFLIP, 0, 1, 1, 0);
991 ret = v4l2_fwnode_device_parse(&ov2311->client->dev, &props);
993 goto err_free_handler;
995 ret = v4l2_ctrl_new_fwnode_properties(handler, &ov2311_ctrl_ops,
998 goto err_free_handler;
1000 if (handler->error) {
1001 ret = handler->error;
1002 dev_err(&ov2311->client->dev,
1003 "Failed to init controls(%d)\n", ret);
1004 goto err_free_handler;
1007 ov2311->subdev.ctrl_handler = handler;
1012 v4l2_ctrl_handler_free(handler);
1017 static int ov2311_check_sensor_id(struct ov2311 *ov2311,
1018 struct i2c_client *client)
1020 struct device *dev = &ov2311->client->dev;
1024 ret = ov2311_read_reg(client, OV2311_REG_CHIP_ID + 1,
1025 OV2311_REG_VALUE_08BIT, &id);
1027 ret = ov2311_read_reg(client, OV2311_REG_CHIP_ID,
1028 OV2311_REG_VALUE_08BIT, &id_msb);
1029 id |= (id_msb << 8);
1030 if (ret || id != CHIP_ID) {
1031 dev_err(dev, "Unexpected sensor id(%04x), ret(%d)\n", id, ret);
1035 dev_info(dev, "Detected OV%06x sensor\n", CHIP_ID);
1040 static int ov2311_configure_regulators(struct ov2311 *ov2311)
1044 for (i = 0; i < OV2311_NUM_SUPPLIES; i++)
1045 ov2311->supplies[i].supply = ov2311_supply_names[i];
1047 return devm_regulator_bulk_get(&ov2311->client->dev,
1048 OV2311_NUM_SUPPLIES,
1052 static int ov2311_probe(struct i2c_client *client,
1053 const struct i2c_device_id *id)
1055 struct device *dev = &client->dev;
1056 struct ov2311 *ov2311;
1057 struct v4l2_subdev *sd;
1060 ov2311 = devm_kzalloc(dev, sizeof(*ov2311), GFP_KERNEL);
1064 ov2311->client = client;
1065 ov2311->cur_mode = &supported_modes[0];
1067 ov2311->xvclk = devm_clk_get(dev, "xvclk");
1068 if (IS_ERR(ov2311->xvclk)) {
1069 dev_err(dev, "Failed to get xvclk\n");
1073 ov2311->reset_gpio = devm_gpiod_get_optional(dev, "reset",
1075 if (IS_ERR(ov2311->reset_gpio))
1076 dev_warn(dev, "Failed to get reset-gpios\n");
1078 ov2311->pwdn_gpio = devm_gpiod_get_optional(dev, "pwdn", GPIOD_OUT_LOW);
1079 if (IS_ERR(ov2311->pwdn_gpio))
1080 dev_warn(dev, "Failed to get pwdn-gpios\n");
1082 ret = ov2311_configure_regulators(ov2311);
1084 dev_err(dev, "Failed to get power regulators\n");
1088 mutex_init(&ov2311->mutex);
1090 sd = &ov2311->subdev;
1091 v4l2_i2c_subdev_init(sd, client, &ov2311_subdev_ops);
1092 ret = ov2311_initialize_controls(ov2311);
1094 goto err_destroy_mutex;
1096 ret = ov2311_power_on(&client->dev);
1098 goto err_free_handler;
1100 ret = ov2311_check_sensor_id(ov2311, client);
1104 sd->internal_ops = &ov2311_internal_ops;
1105 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1107 ov2311->pad.flags = MEDIA_PAD_FL_SOURCE;
1108 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1109 ret = media_entity_pads_init(&sd->entity, 1, &ov2311->pad);
1113 ret = v4l2_async_register_subdev(sd);
1115 dev_err(dev, "v4l2 async register subdev failed\n");
1116 goto err_clean_entity;
1119 pm_runtime_set_active(dev);
1120 pm_runtime_enable(dev);
1121 pm_runtime_idle(dev);
1126 media_entity_cleanup(&sd->entity);
1128 ov2311_power_off(&client->dev);
1130 v4l2_ctrl_handler_free(&ov2311->ctrl_handler);
1132 mutex_destroy(&ov2311->mutex);
1137 static int ov2311_remove(struct i2c_client *client)
1139 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1140 struct ov2311 *ov2311 = to_ov2311(sd);
1142 v4l2_async_unregister_subdev(sd);
1143 media_entity_cleanup(&sd->entity);
1144 v4l2_ctrl_handler_free(&ov2311->ctrl_handler);
1145 mutex_destroy(&ov2311->mutex);
1147 pm_runtime_disable(&client->dev);
1148 if (!pm_runtime_status_suspended(&client->dev))
1149 ov2311_power_off(&client->dev);
1150 pm_runtime_set_suspended(&client->dev);
1155 static const struct of_device_id ov2311_of_match[] = {
1156 { .compatible = "ovti,ov2311" },
1159 MODULE_DEVICE_TABLE(of, ov2311_of_match);
1161 static const struct i2c_device_id ov2311_match_id[] = {
1162 { "ovti,ov2311", 0 },
1166 static struct i2c_driver ov2311_i2c_driver = {
1168 .name = OV2311_NAME,
1169 .pm = &ov2311_pm_ops,
1170 .of_match_table = of_match_ptr(ov2311_of_match),
1172 .probe = &ov2311_probe,
1173 .remove = &ov2311_remove,
1174 .id_table = ov2311_match_id,
1177 module_i2c_driver(ov2311_i2c_driver);
1179 MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com");
1180 MODULE_DESCRIPTION("OmniVision OV2311 sensor driver");
1181 MODULE_LICENSE("GPL v2");