1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2017 Microchip Corporation.
5 #include <linux/delay.h>
6 #include <linux/gpio/consumer.h>
8 #include <linux/module.h>
9 #include <linux/pm_runtime.h>
10 #include <linux/regmap.h>
11 #include <media/v4l2-ctrls.h>
12 #include <media/v4l2-event.h>
13 #include <media/v4l2-image-sizes.h>
14 #include <media/v4l2-subdev.h>
16 #define REG_OUTSIZE_LSB 0x34
18 /* OV7740 register tables */
19 #define REG_GAIN 0x00 /* Gain lower 8 bits (rest in vref) */
20 #define REG_BGAIN 0x01 /* blue gain */
21 #define REG_RGAIN 0x02 /* red gain */
22 #define REG_GGAIN 0x03 /* green gain */
23 #define REG_REG04 0x04 /* analog setting, don't change*/
24 #define REG_BAVG 0x05 /* b channel average */
25 #define REG_GAVG 0x06 /* g channel average */
26 #define REG_RAVG 0x07 /* r channel average */
28 #define REG_REG0C 0x0C /* filp enable */
29 #define REG0C_IMG_FLIP 0x80
30 #define REG0C_IMG_MIRROR 0x40
32 #define REG_REG0E 0x0E /* blc line */
33 #define REG_HAEC 0x0F /* auto exposure cntrl */
34 #define REG_AEC 0x10 /* auto exposure cntrl */
36 #define REG_CLK 0x11 /* Clock control */
37 #define REG_REG55 0x55 /* Clock PLL DIV/PreDiv */
39 #define REG_REG12 0x12
41 #define REG_REG13 0x13 /* auto/manual AGC, AEC, Write Balance*/
42 #define REG13_AEC_EN 0x01
43 #define REG13_AGC_EN 0x04
45 #define REG_REG14 0x14
46 #define REG_CTRL15 0x15
47 #define REG15_GAIN_MSB 0x03
49 #define REG_REG16 0x16
51 #define REG_MIDH 0x1C /* manufacture id byte */
52 #define REG_MIDL 0x1D /* manufacture id byre */
53 #define REG_PIDH 0x0A /* Product ID MSB */
54 #define REG_PIDL 0x0B /* Product ID LSB */
56 #define REG_84 0x84 /* lots of stuff */
57 #define REG_REG38 0x38 /* sub-addr */
59 #define REG_AHSTART 0x17 /* Horiz start high bits */
60 #define REG_AHSIZE 0x18
61 #define REG_AVSTART 0x19 /* Vert start high bits */
62 #define REG_AVSIZE 0x1A
63 #define REG_PSHFT 0x1b /* Pixel delay after HREF */
65 #define REG_HOUTSIZE 0x31
66 #define REG_VOUTSIZE 0x32
67 #define REG_HVSIZEOFF 0x33
68 #define REG_REG34 0x34 /* DSP output size H/V LSB*/
70 #define REG_ISP_CTRL00 0x80
71 #define ISPCTRL00_AWB_EN 0x10
72 #define ISPCTRL00_AWB_GAIN_EN 0x04
74 #define REG_YGAIN 0xE2 /* ygain for contrast control */
76 #define REG_YBRIGHT 0xE3
77 #define REG_SGNSET 0xE4
78 #define SGNSET_YBRIGHT_MASK 0x08
85 struct v4l2_subdev subdev;
87 struct v4l2_mbus_framefmt format;
88 const struct ov7740_pixfmt *fmt; /* Current format */
89 const struct ov7740_framesize *frmsize;
90 struct regmap *regmap;
92 struct v4l2_ctrl_handler ctrl_handler;
95 struct v4l2_ctrl *auto_gain;
96 struct v4l2_ctrl *gain;
99 struct v4l2_ctrl *auto_wb;
100 struct v4l2_ctrl *blue_balance;
101 struct v4l2_ctrl *red_balance;
104 struct v4l2_ctrl *hflip;
105 struct v4l2_ctrl *vflip;
108 /* exposure cluster */
109 struct v4l2_ctrl *auto_exposure;
110 struct v4l2_ctrl *exposure;
113 /* saturation/hue cluster */
114 struct v4l2_ctrl *saturation;
115 struct v4l2_ctrl *hue;
117 struct v4l2_ctrl *brightness;
118 struct v4l2_ctrl *contrast;
120 struct mutex mutex; /* To serialize asynchronus callbacks */
122 struct gpio_desc *resetb_gpio;
123 struct gpio_desc *pwdn_gpio;
126 struct ov7740_pixfmt {
128 enum v4l2_colorspace colorspace;
129 const struct reg_sequence *regs;
133 struct ov7740_framesize {
136 const struct reg_sequence *regs;
140 static const struct reg_sequence ov7740_vga[] = {
156 {REG_HOUTSIZE, 0xa0},
157 {REG_VOUTSIZE, 0xf0},
159 {REG_OUTSIZE_LSB, 0x0},
257 static const struct ov7740_framesize ov7740_framesizes[] = {
260 .height = VGA_HEIGHT,
262 .reg_num = ARRAY_SIZE(ov7740_vga),
266 #ifdef CONFIG_VIDEO_ADV_DEBUG
267 static int ov7740_get_register(struct v4l2_subdev *sd,
268 struct v4l2_dbg_register *reg)
270 struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
271 struct regmap *regmap = ov7740->regmap;
272 unsigned int val = 0;
275 ret = regmap_read(regmap, reg->reg & 0xff, &val);
282 static int ov7740_set_register(struct v4l2_subdev *sd,
283 const struct v4l2_dbg_register *reg)
285 struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
286 struct regmap *regmap = ov7740->regmap;
288 regmap_write(regmap, reg->reg & 0xff, reg->val & 0xff);
294 static int ov7740_set_power(struct ov7740 *ov7740, int on)
299 ret = clk_prepare_enable(ov7740->xvclk);
303 if (ov7740->pwdn_gpio)
304 gpiod_direction_output(ov7740->pwdn_gpio, 0);
306 if (ov7740->resetb_gpio) {
307 gpiod_set_value(ov7740->resetb_gpio, 1);
308 usleep_range(500, 1000);
309 gpiod_set_value(ov7740->resetb_gpio, 0);
310 usleep_range(3000, 5000);
313 clk_disable_unprepare(ov7740->xvclk);
315 if (ov7740->pwdn_gpio)
316 gpiod_direction_output(ov7740->pwdn_gpio, 0);
322 static const struct v4l2_subdev_core_ops ov7740_subdev_core_ops = {
323 .log_status = v4l2_ctrl_subdev_log_status,
324 #ifdef CONFIG_VIDEO_ADV_DEBUG
325 .g_register = ov7740_get_register,
326 .s_register = ov7740_set_register,
328 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
329 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
332 static int ov7740_set_white_balance(struct ov7740 *ov7740, int awb)
334 struct regmap *regmap = ov7740->regmap;
338 ret = regmap_read(regmap, REG_ISP_CTRL00, &value);
341 value |= (ISPCTRL00_AWB_EN | ISPCTRL00_AWB_GAIN_EN);
343 value &= ~(ISPCTRL00_AWB_EN | ISPCTRL00_AWB_GAIN_EN);
344 ret = regmap_write(regmap, REG_ISP_CTRL00, value);
350 ret = regmap_write(regmap, REG_BGAIN,
351 ov7740->blue_balance->val);
355 ret = regmap_write(regmap, REG_RGAIN, ov7740->red_balance->val);
363 static int ov7740_set_saturation(struct regmap *regmap, int value)
367 ret = regmap_write(regmap, REG_USAT, (unsigned char)value);
371 return regmap_write(regmap, REG_VSAT, (unsigned char)value);
374 static int ov7740_set_gain(struct regmap *regmap, int value)
378 ret = regmap_write(regmap, REG_GAIN, value & 0xff);
382 ret = regmap_update_bits(regmap, REG_CTRL15,
383 REG15_GAIN_MSB, (value >> 8) & 0x3);
385 ret = regmap_update_bits(regmap, REG_REG13, REG13_AGC_EN, 0);
390 static int ov7740_set_autogain(struct regmap *regmap, int value)
395 ret = regmap_read(regmap, REG_REG13, ®);
401 reg &= ~REG13_AGC_EN;
402 return regmap_write(regmap, REG_REG13, reg);
405 static int ov7740_set_brightness(struct regmap *regmap, int value)
407 /* Turn off AEC/AGC */
408 regmap_update_bits(regmap, REG_REG13, REG13_AEC_EN, 0);
409 regmap_update_bits(regmap, REG_REG13, REG13_AGC_EN, 0);
412 regmap_write(regmap, REG_YBRIGHT, (unsigned char)value);
413 regmap_update_bits(regmap, REG_SGNSET, SGNSET_YBRIGHT_MASK, 0);
415 regmap_write(regmap, REG_YBRIGHT, (unsigned char)(-value));
416 regmap_update_bits(regmap, REG_SGNSET, SGNSET_YBRIGHT_MASK, 1);
422 static int ov7740_set_contrast(struct regmap *regmap, int value)
424 return regmap_write(regmap, REG_YGAIN, (unsigned char)value);
427 static int ov7740_get_gain(struct ov7740 *ov7740, struct v4l2_ctrl *ctrl)
429 struct regmap *regmap = ov7740->regmap;
430 unsigned int value0, value1;
436 ret = regmap_read(regmap, REG_GAIN, &value0);
439 ret = regmap_read(regmap, REG_CTRL15, &value1);
443 ov7740->gain->val = (value1 << 8) | (value0 & 0xff);
448 static int ov7740_get_exp(struct ov7740 *ov7740, struct v4l2_ctrl *ctrl)
450 struct regmap *regmap = ov7740->regmap;
451 unsigned int value0, value1;
454 if (ctrl->val == V4L2_EXPOSURE_MANUAL)
457 ret = regmap_read(regmap, REG_AEC, &value0);
460 ret = regmap_read(regmap, REG_HAEC, &value1);
464 ov7740->exposure->val = (value1 << 8) | (value0 & 0xff);
469 static int ov7740_set_exp(struct regmap *regmap, int value)
473 /* Turn off AEC/AGC */
474 ret = regmap_update_bits(regmap, REG_REG13,
475 REG13_AEC_EN | REG13_AGC_EN, 0);
479 ret = regmap_write(regmap, REG_AEC, (unsigned char)value);
483 return regmap_write(regmap, REG_HAEC, (unsigned char)(value >> 8));
486 static int ov7740_set_autoexp(struct regmap *regmap,
487 enum v4l2_exposure_auto_type value)
492 ret = regmap_read(regmap, REG_REG13, ®);
494 if (value == V4L2_EXPOSURE_AUTO)
495 reg |= (REG13_AEC_EN | REG13_AGC_EN);
497 reg &= ~(REG13_AEC_EN | REG13_AGC_EN);
498 ret = regmap_write(regmap, REG_REG13, reg);
505 static int ov7740_get_volatile_ctrl(struct v4l2_ctrl *ctrl)
507 struct ov7740 *ov7740 = container_of(ctrl->handler,
508 struct ov7740, ctrl_handler);
512 case V4L2_CID_AUTOGAIN:
513 ret = ov7740_get_gain(ov7740, ctrl);
515 case V4L2_CID_EXPOSURE_AUTO:
516 ret = ov7740_get_exp(ov7740, ctrl);
525 static int ov7740_set_ctrl(struct v4l2_ctrl *ctrl)
527 struct ov7740 *ov7740 = container_of(ctrl->handler,
528 struct ov7740, ctrl_handler);
529 struct i2c_client *client = v4l2_get_subdevdata(&ov7740->subdev);
530 struct regmap *regmap = ov7740->regmap;
534 if (!pm_runtime_get_if_in_use(&client->dev))
538 case V4L2_CID_AUTO_WHITE_BALANCE:
539 ret = ov7740_set_white_balance(ov7740, ctrl->val);
541 case V4L2_CID_SATURATION:
542 ret = ov7740_set_saturation(regmap, ctrl->val);
544 case V4L2_CID_BRIGHTNESS:
545 ret = ov7740_set_brightness(regmap, ctrl->val);
547 case V4L2_CID_CONTRAST:
548 ret = ov7740_set_contrast(regmap, ctrl->val);
551 val = ctrl->val ? REG0C_IMG_FLIP : 0x00;
552 ret = regmap_update_bits(regmap, REG_REG0C,
553 REG0C_IMG_FLIP, val);
556 val = ctrl->val ? REG0C_IMG_MIRROR : 0x00;
557 ret = regmap_update_bits(regmap, REG_REG0C,
558 REG0C_IMG_MIRROR, val);
560 case V4L2_CID_AUTOGAIN:
562 ret = ov7740_set_gain(regmap, ov7740->gain->val);
564 ret = ov7740_set_autogain(regmap, ctrl->val);
567 case V4L2_CID_EXPOSURE_AUTO:
568 if (ctrl->val == V4L2_EXPOSURE_MANUAL)
569 ret = ov7740_set_exp(regmap, ov7740->exposure->val);
571 ret = ov7740_set_autoexp(regmap, ctrl->val);
578 pm_runtime_put(&client->dev);
583 static const struct v4l2_ctrl_ops ov7740_ctrl_ops = {
584 .g_volatile_ctrl = ov7740_get_volatile_ctrl,
585 .s_ctrl = ov7740_set_ctrl,
588 static int ov7740_start_streaming(struct ov7740 *ov7740)
593 ret = regmap_multi_reg_write(ov7740->regmap,
595 ov7740->fmt->reg_num);
600 if (ov7740->frmsize) {
601 ret = regmap_multi_reg_write(ov7740->regmap,
602 ov7740->frmsize->regs,
603 ov7740->frmsize->reg_num);
608 return __v4l2_ctrl_handler_setup(ov7740->subdev.ctrl_handler);
611 static int ov7740_set_stream(struct v4l2_subdev *sd, int enable)
613 struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
614 struct i2c_client *client = v4l2_get_subdevdata(sd);
617 mutex_lock(&ov7740->mutex);
620 ret = pm_runtime_resume_and_get(&client->dev);
624 ret = ov7740_start_streaming(ov7740);
628 pm_runtime_put(&client->dev);
631 mutex_unlock(&ov7740->mutex);
635 pm_runtime_put(&client->dev);
637 mutex_unlock(&ov7740->mutex);
641 static const struct v4l2_subdev_video_ops ov7740_subdev_video_ops = {
642 .s_stream = ov7740_set_stream,
645 static const struct reg_sequence ov7740_format_yuyv[] = {
652 static const struct reg_sequence ov7740_format_bggr8[] = {
658 static const struct ov7740_pixfmt ov7740_formats[] = {
660 .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
661 .colorspace = V4L2_COLORSPACE_SRGB,
662 .regs = ov7740_format_yuyv,
663 .reg_num = ARRAY_SIZE(ov7740_format_yuyv),
666 .mbus_code = MEDIA_BUS_FMT_SBGGR8_1X8,
667 .colorspace = V4L2_COLORSPACE_SRGB,
668 .regs = ov7740_format_bggr8,
669 .reg_num = ARRAY_SIZE(ov7740_format_bggr8),
672 #define N_OV7740_FMTS ARRAY_SIZE(ov7740_formats)
674 static int ov7740_enum_mbus_code(struct v4l2_subdev *sd,
675 struct v4l2_subdev_state *sd_state,
676 struct v4l2_subdev_mbus_code_enum *code)
678 if (code->pad || code->index >= N_OV7740_FMTS)
681 code->code = ov7740_formats[code->index].mbus_code;
686 static int ov7740_enum_frame_interval(struct v4l2_subdev *sd,
687 struct v4l2_subdev_state *sd_state,
688 struct v4l2_subdev_frame_interval_enum *fie)
696 if ((fie->width != VGA_WIDTH) || (fie->height != VGA_HEIGHT))
699 fie->interval.numerator = 1;
700 fie->interval.denominator = 60;
705 static int ov7740_enum_frame_size(struct v4l2_subdev *sd,
706 struct v4l2_subdev_state *sd_state,
707 struct v4l2_subdev_frame_size_enum *fse)
715 fse->min_width = fse->max_width = VGA_WIDTH;
716 fse->min_height = fse->max_height = VGA_HEIGHT;
721 static int ov7740_try_fmt_internal(struct v4l2_subdev *sd,
722 struct v4l2_mbus_framefmt *fmt,
723 const struct ov7740_pixfmt **ret_fmt,
724 const struct ov7740_framesize **ret_frmsize)
726 struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
727 const struct ov7740_framesize *fsize = &ov7740_framesizes[0];
730 for (index = 0; index < N_OV7740_FMTS; index++) {
731 if (ov7740_formats[index].mbus_code == fmt->code)
734 if (index >= N_OV7740_FMTS) {
735 /* default to first format */
737 fmt->code = ov7740_formats[0].mbus_code;
740 *ret_fmt = ov7740_formats + index;
742 for (i = 0; i < ARRAY_SIZE(ov7740_framesizes); i++) {
743 if ((fsize->width >= fmt->width) &&
744 (fsize->height >= fmt->height)) {
745 fmt->width = fsize->width;
746 fmt->height = fsize->height;
752 if (i >= ARRAY_SIZE(ov7740_framesizes)) {
753 fsize = &ov7740_framesizes[0];
754 fmt->width = fsize->width;
755 fmt->height = fsize->height;
757 if (ret_frmsize != NULL)
758 *ret_frmsize = fsize;
760 fmt->field = V4L2_FIELD_NONE;
761 fmt->colorspace = ov7740_formats[index].colorspace;
763 ov7740->format = *fmt;
768 static int ov7740_set_fmt(struct v4l2_subdev *sd,
769 struct v4l2_subdev_state *sd_state,
770 struct v4l2_subdev_format *format)
772 struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
773 const struct ov7740_pixfmt *ovfmt;
774 const struct ov7740_framesize *fsize;
775 struct v4l2_mbus_framefmt *mbus_fmt;
778 mutex_lock(&ov7740->mutex);
784 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
785 ret = ov7740_try_fmt_internal(sd, &format->format, NULL, NULL);
789 mbus_fmt = v4l2_subdev_state_get_format(sd_state, format->pad);
790 *mbus_fmt = format->format;
791 mutex_unlock(&ov7740->mutex);
795 ret = ov7740_try_fmt_internal(sd, &format->format, &ovfmt, &fsize);
800 ov7740->frmsize = fsize;
802 mutex_unlock(&ov7740->mutex);
806 mutex_unlock(&ov7740->mutex);
810 static int ov7740_get_fmt(struct v4l2_subdev *sd,
811 struct v4l2_subdev_state *sd_state,
812 struct v4l2_subdev_format *format)
814 struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
815 struct v4l2_mbus_framefmt *mbus_fmt;
817 mutex_lock(&ov7740->mutex);
818 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
819 mbus_fmt = v4l2_subdev_state_get_format(sd_state, 0);
820 format->format = *mbus_fmt;
822 format->format = ov7740->format;
824 mutex_unlock(&ov7740->mutex);
829 static int ov7740_get_frame_interval(struct v4l2_subdev *sd,
830 struct v4l2_subdev_state *sd_state,
831 struct v4l2_subdev_frame_interval *ival)
833 struct v4l2_fract *tpf = &ival->interval;
836 tpf->denominator = 60;
841 static const struct v4l2_subdev_pad_ops ov7740_subdev_pad_ops = {
842 .enum_frame_interval = ov7740_enum_frame_interval,
843 .enum_frame_size = ov7740_enum_frame_size,
844 .enum_mbus_code = ov7740_enum_mbus_code,
845 .get_fmt = ov7740_get_fmt,
846 .set_fmt = ov7740_set_fmt,
847 .get_frame_interval = ov7740_get_frame_interval,
848 .set_frame_interval = ov7740_get_frame_interval,
851 static const struct v4l2_subdev_ops ov7740_subdev_ops = {
852 .core = &ov7740_subdev_core_ops,
853 .video = &ov7740_subdev_video_ops,
854 .pad = &ov7740_subdev_pad_ops,
857 static void ov7740_get_default_format(struct v4l2_subdev *sd,
858 struct v4l2_mbus_framefmt *format)
860 struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
862 format->width = ov7740->frmsize->width;
863 format->height = ov7740->frmsize->height;
864 format->colorspace = ov7740->fmt->colorspace;
865 format->code = ov7740->fmt->mbus_code;
866 format->field = V4L2_FIELD_NONE;
869 static int ov7740_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
871 struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
872 struct v4l2_mbus_framefmt *format =
873 v4l2_subdev_state_get_format(fh->state, 0);
875 mutex_lock(&ov7740->mutex);
876 ov7740_get_default_format(sd, format);
877 mutex_unlock(&ov7740->mutex);
882 static const struct v4l2_subdev_internal_ops ov7740_subdev_internal_ops = {
886 static int ov7740_probe_dt(struct i2c_client *client,
887 struct ov7740 *ov7740)
889 ov7740->resetb_gpio = devm_gpiod_get_optional(&client->dev, "reset",
891 if (IS_ERR(ov7740->resetb_gpio)) {
892 dev_info(&client->dev, "can't get %s GPIO\n", "reset");
893 return PTR_ERR(ov7740->resetb_gpio);
896 ov7740->pwdn_gpio = devm_gpiod_get_optional(&client->dev, "powerdown",
898 if (IS_ERR(ov7740->pwdn_gpio)) {
899 dev_info(&client->dev, "can't get %s GPIO\n", "powerdown");
900 return PTR_ERR(ov7740->pwdn_gpio);
906 static int ov7740_detect(struct ov7740 *ov7740)
908 struct regmap *regmap = ov7740->regmap;
909 unsigned int midh, midl, pidh, pidl;
912 ret = regmap_read(regmap, REG_MIDH, &midh);
918 ret = regmap_read(regmap, REG_MIDL, &midl);
924 ret = regmap_read(regmap, REG_PIDH, &pidh);
930 ret = regmap_read(regmap, REG_PIDL, &pidl);
933 if ((pidl != 0x40) && (pidl != 0x41) && (pidl != 0x42))
939 static int ov7740_init_controls(struct ov7740 *ov7740)
941 struct i2c_client *client = v4l2_get_subdevdata(&ov7740->subdev);
942 struct v4l2_ctrl_handler *ctrl_hdlr = &ov7740->ctrl_handler;
945 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 12);
949 ctrl_hdlr->lock = &ov7740->mutex;
950 ov7740->auto_wb = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
951 V4L2_CID_AUTO_WHITE_BALANCE,
953 ov7740->blue_balance = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
954 V4L2_CID_BLUE_BALANCE,
956 ov7740->red_balance = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
957 V4L2_CID_RED_BALANCE,
960 ov7740->brightness = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
963 ov7740->contrast = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
966 ov7740->saturation = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
967 V4L2_CID_SATURATION, 0, 256, 1, 0x80);
968 ov7740->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
969 V4L2_CID_HFLIP, 0, 1, 1, 0);
970 ov7740->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
971 V4L2_CID_VFLIP, 0, 1, 1, 0);
973 ov7740->gain = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
974 V4L2_CID_GAIN, 0, 1023, 1, 500);
976 ov7740->auto_gain = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
977 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
979 ov7740->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &ov7740_ctrl_ops,
980 V4L2_CID_EXPOSURE, 0, 65535, 1, 500);
982 ov7740->auto_exposure = v4l2_ctrl_new_std_menu(ctrl_hdlr,
984 V4L2_CID_EXPOSURE_AUTO,
985 V4L2_EXPOSURE_MANUAL, 0,
988 v4l2_ctrl_auto_cluster(3, &ov7740->auto_wb, 0, false);
989 v4l2_ctrl_auto_cluster(2, &ov7740->auto_gain, 0, true);
990 v4l2_ctrl_auto_cluster(2, &ov7740->auto_exposure,
991 V4L2_EXPOSURE_MANUAL, true);
993 if (ctrl_hdlr->error) {
994 ret = ctrl_hdlr->error;
995 dev_err(&client->dev, "controls initialisation failed (%d)\n",
1000 ret = v4l2_ctrl_handler_setup(ctrl_hdlr);
1002 dev_err(&client->dev, "%s control init failed (%d)\n",
1007 ov7740->subdev.ctrl_handler = ctrl_hdlr;
1011 v4l2_ctrl_handler_free(ctrl_hdlr);
1012 mutex_destroy(&ov7740->mutex);
1016 static void ov7740_free_controls(struct ov7740 *ov7740)
1018 v4l2_ctrl_handler_free(ov7740->subdev.ctrl_handler);
1019 mutex_destroy(&ov7740->mutex);
1022 #define OV7740_MAX_REGISTER 0xff
1023 static const struct regmap_config ov7740_regmap_config = {
1026 .max_register = OV7740_MAX_REGISTER,
1029 static int ov7740_probe(struct i2c_client *client)
1031 struct ov7740 *ov7740;
1032 struct v4l2_subdev *sd;
1035 ov7740 = devm_kzalloc(&client->dev, sizeof(*ov7740), GFP_KERNEL);
1039 ov7740->xvclk = devm_clk_get(&client->dev, "xvclk");
1040 if (IS_ERR(ov7740->xvclk)) {
1041 ret = PTR_ERR(ov7740->xvclk);
1042 dev_err(&client->dev,
1043 "OV7740: fail to get xvclk: %d\n", ret);
1047 ret = ov7740_probe_dt(client, ov7740);
1051 ov7740->regmap = devm_regmap_init_sccb(client, &ov7740_regmap_config);
1052 if (IS_ERR(ov7740->regmap)) {
1053 ret = PTR_ERR(ov7740->regmap);
1054 dev_err(&client->dev, "Failed to allocate register map: %d\n",
1059 sd = &ov7740->subdev;
1060 v4l2_i2c_subdev_init(sd, client, &ov7740_subdev_ops);
1062 sd->internal_ops = &ov7740_subdev_internal_ops;
1063 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
1065 ov7740->pad.flags = MEDIA_PAD_FL_SOURCE;
1066 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1067 ret = media_entity_pads_init(&sd->entity, 1, &ov7740->pad);
1071 ret = ov7740_set_power(ov7740, 1);
1075 pm_runtime_set_active(&client->dev);
1076 pm_runtime_enable(&client->dev);
1078 ret = ov7740_detect(ov7740);
1082 mutex_init(&ov7740->mutex);
1084 ret = ov7740_init_controls(ov7740);
1086 goto error_init_controls;
1088 v4l_info(client, "chip found @ 0x%02x (%s)\n",
1089 client->addr << 1, client->adapter->name);
1091 ov7740->fmt = &ov7740_formats[0];
1092 ov7740->frmsize = &ov7740_framesizes[0];
1094 ov7740_get_default_format(sd, &ov7740->format);
1096 ret = v4l2_async_register_subdev(sd);
1098 goto error_async_register;
1100 pm_runtime_idle(&client->dev);
1104 error_async_register:
1105 v4l2_ctrl_handler_free(ov7740->subdev.ctrl_handler);
1106 error_init_controls:
1107 ov7740_free_controls(ov7740);
1109 pm_runtime_disable(&client->dev);
1110 pm_runtime_set_suspended(&client->dev);
1111 ov7740_set_power(ov7740, 0);
1112 media_entity_cleanup(&ov7740->subdev.entity);
1117 static void ov7740_remove(struct i2c_client *client)
1119 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1120 struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
1122 mutex_destroy(&ov7740->mutex);
1123 v4l2_ctrl_handler_free(ov7740->subdev.ctrl_handler);
1124 media_entity_cleanup(&ov7740->subdev.entity);
1125 v4l2_async_unregister_subdev(sd);
1126 ov7740_free_controls(ov7740);
1128 pm_runtime_get_sync(&client->dev);
1129 pm_runtime_disable(&client->dev);
1130 pm_runtime_set_suspended(&client->dev);
1131 pm_runtime_put_noidle(&client->dev);
1133 ov7740_set_power(ov7740, 0);
1136 static int __maybe_unused ov7740_runtime_suspend(struct device *dev)
1138 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1139 struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
1141 ov7740_set_power(ov7740, 0);
1146 static int __maybe_unused ov7740_runtime_resume(struct device *dev)
1148 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1149 struct ov7740 *ov7740 = container_of(sd, struct ov7740, subdev);
1151 return ov7740_set_power(ov7740, 1);
1154 static const struct i2c_device_id ov7740_id[] = {
1158 MODULE_DEVICE_TABLE(i2c, ov7740_id);
1160 static const struct dev_pm_ops ov7740_pm_ops = {
1161 SET_RUNTIME_PM_OPS(ov7740_runtime_suspend, ov7740_runtime_resume, NULL)
1164 static const struct of_device_id ov7740_of_match[] = {
1165 {.compatible = "ovti,ov7740", },
1168 MODULE_DEVICE_TABLE(of, ov7740_of_match);
1170 static struct i2c_driver ov7740_i2c_driver = {
1173 .pm = &ov7740_pm_ops,
1174 .of_match_table = ov7740_of_match,
1176 .probe = ov7740_probe,
1177 .remove = ov7740_remove,
1178 .id_table = ov7740_id,
1180 module_i2c_driver(ov7740_i2c_driver);
1182 MODULE_DESCRIPTION("The V4L2 driver for Omnivision 7740 sensor");
1184 MODULE_LICENSE("GPL v2");