1 // SPDX-License-Identifier: GPL-2.0-only
3 * adv7183.c Analog Devices ADV7183 video decoder driver
5 * Copyright (c) 2011 Analog Devices Inc.
8 #include <linux/delay.h>
9 #include <linux/errno.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/i2c.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/types.h>
16 #include <linux/videodev2.h>
18 #include <media/i2c/adv7183.h>
19 #include <media/v4l2-ctrls.h>
20 #include <media/v4l2-device.h>
22 #include "adv7183_regs.h"
25 struct v4l2_subdev sd
;
26 struct v4l2_ctrl_handler hdl
;
28 v4l2_std_id std
; /* Current set standard */
31 struct gpio_desc
*reset_pin
;
32 struct gpio_desc
*oe_pin
;
33 struct v4l2_mbus_framefmt fmt
;
36 /* EXAMPLES USING 27 MHz CLOCK
37 * Mode 1 CVBS Input (Composite Video on AIN5)
38 * All standards are supported through autodetect, 8-bit, 4:2:2, ITU-R BT.656 output on P15 to P8.
40 static const unsigned char adv7183_init_regs
[] = {
41 ADV7183_IN_CTRL
, 0x04, /* CVBS input on AIN5 */
42 ADV7183_DIGI_CLAMP_CTRL_1
, 0x00, /* Slow down digital clamps */
43 ADV7183_SHAP_FILT_CTRL
, 0x41, /* Set CSFM to SH1 */
44 ADV7183_ADC_CTRL
, 0x16, /* Power down ADC 1 and ADC 2 */
45 ADV7183_CTI_DNR_CTRL_4
, 0x04, /* Set DNR threshold to 4 for flat response */
46 /* ADI recommended programming sequence */
47 ADV7183_ADI_CTRL
, 0x80,
48 ADV7183_CTI_DNR_CTRL_4
, 0x20,
57 ADV7183_SD_SATURATION_CR
, 0x3E,
58 ADV7183_PAL_V_END
, 0x3E,
59 ADV7183_PAL_F_TOGGLE
, 0x0F,
60 ADV7183_ADI_CTRL
, 0x00,
63 static inline struct adv7183
*to_adv7183(struct v4l2_subdev
*sd
)
65 return container_of(sd
, struct adv7183
, sd
);
67 static inline struct v4l2_subdev
*to_sd(struct v4l2_ctrl
*ctrl
)
69 return &container_of(ctrl
->handler
, struct adv7183
, hdl
)->sd
;
72 static inline int adv7183_read(struct v4l2_subdev
*sd
, unsigned char reg
)
74 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
76 return i2c_smbus_read_byte_data(client
, reg
);
79 static inline int adv7183_write(struct v4l2_subdev
*sd
, unsigned char reg
,
82 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
84 return i2c_smbus_write_byte_data(client
, reg
, value
);
87 static int adv7183_writeregs(struct v4l2_subdev
*sd
,
88 const unsigned char *regs
, unsigned int num
)
90 unsigned char reg
, data
;
94 v4l2_err(sd
, "invalid regs array\n");
103 adv7183_write(sd
, reg
, data
);
108 static int adv7183_log_status(struct v4l2_subdev
*sd
)
110 struct adv7183
*decoder
= to_adv7183(sd
);
112 v4l2_info(sd
, "adv7183: Input control = 0x%02x\n",
113 adv7183_read(sd
, ADV7183_IN_CTRL
));
114 v4l2_info(sd
, "adv7183: Video selection = 0x%02x\n",
115 adv7183_read(sd
, ADV7183_VD_SEL
));
116 v4l2_info(sd
, "adv7183: Output control = 0x%02x\n",
117 adv7183_read(sd
, ADV7183_OUT_CTRL
));
118 v4l2_info(sd
, "adv7183: Extended output control = 0x%02x\n",
119 adv7183_read(sd
, ADV7183_EXT_OUT_CTRL
));
120 v4l2_info(sd
, "adv7183: Autodetect enable = 0x%02x\n",
121 adv7183_read(sd
, ADV7183_AUTO_DET_EN
));
122 v4l2_info(sd
, "adv7183: Contrast = 0x%02x\n",
123 adv7183_read(sd
, ADV7183_CONTRAST
));
124 v4l2_info(sd
, "adv7183: Brightness = 0x%02x\n",
125 adv7183_read(sd
, ADV7183_BRIGHTNESS
));
126 v4l2_info(sd
, "adv7183: Hue = 0x%02x\n",
127 adv7183_read(sd
, ADV7183_HUE
));
128 v4l2_info(sd
, "adv7183: Default value Y = 0x%02x\n",
129 adv7183_read(sd
, ADV7183_DEF_Y
));
130 v4l2_info(sd
, "adv7183: Default value C = 0x%02x\n",
131 adv7183_read(sd
, ADV7183_DEF_C
));
132 v4l2_info(sd
, "adv7183: ADI control = 0x%02x\n",
133 adv7183_read(sd
, ADV7183_ADI_CTRL
));
134 v4l2_info(sd
, "adv7183: Power Management = 0x%02x\n",
135 adv7183_read(sd
, ADV7183_POW_MANAGE
));
136 v4l2_info(sd
, "adv7183: Status 1 2 and 3 = 0x%02x 0x%02x 0x%02x\n",
137 adv7183_read(sd
, ADV7183_STATUS_1
),
138 adv7183_read(sd
, ADV7183_STATUS_2
),
139 adv7183_read(sd
, ADV7183_STATUS_3
));
140 v4l2_info(sd
, "adv7183: Ident = 0x%02x\n",
141 adv7183_read(sd
, ADV7183_IDENT
));
142 v4l2_info(sd
, "adv7183: Analog clamp control = 0x%02x\n",
143 adv7183_read(sd
, ADV7183_ANAL_CLAMP_CTRL
));
144 v4l2_info(sd
, "adv7183: Digital clamp control 1 = 0x%02x\n",
145 adv7183_read(sd
, ADV7183_DIGI_CLAMP_CTRL_1
));
146 v4l2_info(sd
, "adv7183: Shaping filter control 1 and 2 = 0x%02x 0x%02x\n",
147 adv7183_read(sd
, ADV7183_SHAP_FILT_CTRL
),
148 adv7183_read(sd
, ADV7183_SHAP_FILT_CTRL_2
));
149 v4l2_info(sd
, "adv7183: Comb filter control = 0x%02x\n",
150 adv7183_read(sd
, ADV7183_COMB_FILT_CTRL
));
151 v4l2_info(sd
, "adv7183: ADI control 2 = 0x%02x\n",
152 adv7183_read(sd
, ADV7183_ADI_CTRL_2
));
153 v4l2_info(sd
, "adv7183: Pixel delay control = 0x%02x\n",
154 adv7183_read(sd
, ADV7183_PIX_DELAY_CTRL
));
155 v4l2_info(sd
, "adv7183: Misc gain control = 0x%02x\n",
156 adv7183_read(sd
, ADV7183_MISC_GAIN_CTRL
));
157 v4l2_info(sd
, "adv7183: AGC mode control = 0x%02x\n",
158 adv7183_read(sd
, ADV7183_AGC_MODE_CTRL
));
159 v4l2_info(sd
, "adv7183: Chroma gain control 1 and 2 = 0x%02x 0x%02x\n",
160 adv7183_read(sd
, ADV7183_CHRO_GAIN_CTRL_1
),
161 adv7183_read(sd
, ADV7183_CHRO_GAIN_CTRL_2
));
162 v4l2_info(sd
, "adv7183: Luma gain control 1 and 2 = 0x%02x 0x%02x\n",
163 adv7183_read(sd
, ADV7183_LUMA_GAIN_CTRL_1
),
164 adv7183_read(sd
, ADV7183_LUMA_GAIN_CTRL_2
));
165 v4l2_info(sd
, "adv7183: Vsync field control 1 2 and 3 = 0x%02x 0x%02x 0x%02x\n",
166 adv7183_read(sd
, ADV7183_VS_FIELD_CTRL_1
),
167 adv7183_read(sd
, ADV7183_VS_FIELD_CTRL_2
),
168 adv7183_read(sd
, ADV7183_VS_FIELD_CTRL_3
));
169 v4l2_info(sd
, "adv7183: Hsync position control 1 2 and 3 = 0x%02x 0x%02x 0x%02x\n",
170 adv7183_read(sd
, ADV7183_HS_POS_CTRL_1
),
171 adv7183_read(sd
, ADV7183_HS_POS_CTRL_2
),
172 adv7183_read(sd
, ADV7183_HS_POS_CTRL_3
));
173 v4l2_info(sd
, "adv7183: Polarity = 0x%02x\n",
174 adv7183_read(sd
, ADV7183_POLARITY
));
175 v4l2_info(sd
, "adv7183: ADC control = 0x%02x\n",
176 adv7183_read(sd
, ADV7183_ADC_CTRL
));
177 v4l2_info(sd
, "adv7183: SD offset Cb and Cr = 0x%02x 0x%02x\n",
178 adv7183_read(sd
, ADV7183_SD_OFFSET_CB
),
179 adv7183_read(sd
, ADV7183_SD_OFFSET_CR
));
180 v4l2_info(sd
, "adv7183: SD saturation Cb and Cr = 0x%02x 0x%02x\n",
181 adv7183_read(sd
, ADV7183_SD_SATURATION_CB
),
182 adv7183_read(sd
, ADV7183_SD_SATURATION_CR
));
183 v4l2_info(sd
, "adv7183: Drive strength = 0x%02x\n",
184 adv7183_read(sd
, ADV7183_DRIVE_STR
));
185 v4l2_ctrl_handler_log_status(&decoder
->hdl
, sd
->name
);
189 static int adv7183_g_std(struct v4l2_subdev
*sd
, v4l2_std_id
*std
)
191 struct adv7183
*decoder
= to_adv7183(sd
);
197 static int adv7183_s_std(struct v4l2_subdev
*sd
, v4l2_std_id std
)
199 struct adv7183
*decoder
= to_adv7183(sd
);
202 reg
= adv7183_read(sd
, ADV7183_IN_CTRL
) & 0xF;
203 if (std
== V4L2_STD_PAL_60
)
205 else if (std
== V4L2_STD_NTSC_443
)
207 else if (std
== V4L2_STD_PAL_N
)
209 else if (std
== V4L2_STD_PAL_M
)
211 else if (std
== V4L2_STD_PAL_Nc
)
213 else if (std
& V4L2_STD_PAL
)
215 else if (std
& V4L2_STD_NTSC
)
217 else if (std
& V4L2_STD_SECAM
)
221 adv7183_write(sd
, ADV7183_IN_CTRL
, reg
);
228 static int adv7183_reset(struct v4l2_subdev
*sd
, u32 val
)
232 reg
= adv7183_read(sd
, ADV7183_POW_MANAGE
) | 0x80;
233 adv7183_write(sd
, ADV7183_POW_MANAGE
, reg
);
234 /* wait 5ms before any further i2c writes are performed */
235 usleep_range(5000, 10000);
239 static int adv7183_s_routing(struct v4l2_subdev
*sd
,
240 u32 input
, u32 output
, u32 config
)
242 struct adv7183
*decoder
= to_adv7183(sd
);
245 if ((input
> ADV7183_COMPONENT1
) || (output
> ADV7183_16BIT_OUT
))
248 if (input
!= decoder
->input
) {
249 decoder
->input
= input
;
250 reg
= adv7183_read(sd
, ADV7183_IN_CTRL
) & 0xF0;
252 case ADV7183_COMPOSITE1
:
255 case ADV7183_COMPOSITE2
:
258 case ADV7183_COMPOSITE3
:
261 case ADV7183_COMPOSITE4
:
264 case ADV7183_COMPOSITE5
:
267 case ADV7183_COMPOSITE6
:
270 case ADV7183_COMPOSITE7
:
273 case ADV7183_COMPOSITE8
:
276 case ADV7183_COMPOSITE9
:
279 case ADV7183_COMPOSITE10
:
282 case ADV7183_SVIDEO0
:
285 case ADV7183_SVIDEO1
:
288 case ADV7183_SVIDEO2
:
291 case ADV7183_COMPONENT0
:
294 case ADV7183_COMPONENT1
:
300 adv7183_write(sd
, ADV7183_IN_CTRL
, reg
);
303 if (output
!= decoder
->output
) {
304 decoder
->output
= output
;
305 reg
= adv7183_read(sd
, ADV7183_OUT_CTRL
) & 0xC0;
307 case ADV7183_16BIT_OUT
:
314 adv7183_write(sd
, ADV7183_OUT_CTRL
, reg
);
320 static int adv7183_s_ctrl(struct v4l2_ctrl
*ctrl
)
322 struct v4l2_subdev
*sd
= to_sd(ctrl
);
326 case V4L2_CID_BRIGHTNESS
:
329 adv7183_write(sd
, ADV7183_BRIGHTNESS
, val
);
331 case V4L2_CID_CONTRAST
:
332 adv7183_write(sd
, ADV7183_CONTRAST
, val
);
334 case V4L2_CID_SATURATION
:
335 adv7183_write(sd
, ADV7183_SD_SATURATION_CB
, val
>> 8);
336 adv7183_write(sd
, ADV7183_SD_SATURATION_CR
, (val
& 0xFF));
339 adv7183_write(sd
, ADV7183_SD_OFFSET_CB
, val
>> 8);
340 adv7183_write(sd
, ADV7183_SD_OFFSET_CR
, (val
& 0xFF));
349 static int adv7183_querystd(struct v4l2_subdev
*sd
, v4l2_std_id
*std
)
351 struct adv7183
*decoder
= to_adv7183(sd
);
354 /* enable autodetection block */
355 reg
= adv7183_read(sd
, ADV7183_IN_CTRL
) & 0xF;
356 adv7183_write(sd
, ADV7183_IN_CTRL
, reg
);
358 /* wait autodetection switch */
361 /* get autodetection result */
362 reg
= adv7183_read(sd
, ADV7183_STATUS_1
);
363 switch ((reg
>> 0x4) & 0x7) {
365 *std
&= V4L2_STD_NTSC
;
368 *std
&= V4L2_STD_NTSC_443
;
371 *std
&= V4L2_STD_PAL_M
;
374 *std
&= V4L2_STD_PAL_60
;
377 *std
&= V4L2_STD_PAL
;
380 *std
&= V4L2_STD_SECAM
;
383 *std
&= V4L2_STD_PAL_Nc
;
386 *std
&= V4L2_STD_SECAM
;
389 *std
= V4L2_STD_UNKNOWN
;
393 /* after std detection, write back user set std */
394 adv7183_s_std(sd
, decoder
->std
);
398 static int adv7183_g_input_status(struct v4l2_subdev
*sd
, u32
*status
)
402 *status
= V4L2_IN_ST_NO_SIGNAL
;
403 reg
= adv7183_read(sd
, ADV7183_STATUS_1
);
411 static int adv7183_enum_mbus_code(struct v4l2_subdev
*sd
,
412 struct v4l2_subdev_state
*sd_state
,
413 struct v4l2_subdev_mbus_code_enum
*code
)
415 if (code
->pad
|| code
->index
> 0)
418 code
->code
= MEDIA_BUS_FMT_UYVY8_2X8
;
422 static int adv7183_set_fmt(struct v4l2_subdev
*sd
,
423 struct v4l2_subdev_state
*sd_state
,
424 struct v4l2_subdev_format
*format
)
426 struct adv7183
*decoder
= to_adv7183(sd
);
427 struct v4l2_mbus_framefmt
*fmt
= &format
->format
;
432 fmt
->code
= MEDIA_BUS_FMT_UYVY8_2X8
;
433 fmt
->colorspace
= V4L2_COLORSPACE_SMPTE170M
;
434 if (decoder
->std
& V4L2_STD_525_60
) {
435 fmt
->field
= V4L2_FIELD_SEQ_TB
;
439 fmt
->field
= V4L2_FIELD_SEQ_BT
;
443 if (format
->which
== V4L2_SUBDEV_FORMAT_ACTIVE
)
448 static int adv7183_get_fmt(struct v4l2_subdev
*sd
,
449 struct v4l2_subdev_state
*sd_state
,
450 struct v4l2_subdev_format
*format
)
452 struct adv7183
*decoder
= to_adv7183(sd
);
457 format
->format
= decoder
->fmt
;
461 static int adv7183_s_stream(struct v4l2_subdev
*sd
, int enable
)
463 struct adv7183
*decoder
= to_adv7183(sd
);
466 gpiod_set_value(decoder
->oe_pin
, 1);
468 gpiod_set_value(decoder
->oe_pin
, 0);
473 #ifdef CONFIG_VIDEO_ADV_DEBUG
474 static int adv7183_g_register(struct v4l2_subdev
*sd
, struct v4l2_dbg_register
*reg
)
476 reg
->val
= adv7183_read(sd
, reg
->reg
& 0xff);
481 static int adv7183_s_register(struct v4l2_subdev
*sd
, const struct v4l2_dbg_register
*reg
)
483 adv7183_write(sd
, reg
->reg
& 0xff, reg
->val
& 0xff);
488 static const struct v4l2_ctrl_ops adv7183_ctrl_ops
= {
489 .s_ctrl
= adv7183_s_ctrl
,
492 static const struct v4l2_subdev_core_ops adv7183_core_ops
= {
493 .log_status
= adv7183_log_status
,
494 .reset
= adv7183_reset
,
495 #ifdef CONFIG_VIDEO_ADV_DEBUG
496 .g_register
= adv7183_g_register
,
497 .s_register
= adv7183_s_register
,
501 static const struct v4l2_subdev_video_ops adv7183_video_ops
= {
502 .g_std
= adv7183_g_std
,
503 .s_std
= adv7183_s_std
,
504 .s_routing
= adv7183_s_routing
,
505 .querystd
= adv7183_querystd
,
506 .g_input_status
= adv7183_g_input_status
,
507 .s_stream
= adv7183_s_stream
,
510 static const struct v4l2_subdev_pad_ops adv7183_pad_ops
= {
511 .enum_mbus_code
= adv7183_enum_mbus_code
,
512 .get_fmt
= adv7183_get_fmt
,
513 .set_fmt
= adv7183_set_fmt
,
516 static const struct v4l2_subdev_ops adv7183_ops
= {
517 .core
= &adv7183_core_ops
,
518 .video
= &adv7183_video_ops
,
519 .pad
= &adv7183_pad_ops
,
522 static int adv7183_probe(struct i2c_client
*client
)
524 struct adv7183
*decoder
;
525 struct v4l2_subdev
*sd
;
526 struct v4l2_ctrl_handler
*hdl
;
528 struct v4l2_subdev_format fmt
= {
529 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
532 /* Check if the adapter supports the needed features */
533 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
536 v4l_info(client
, "chip found @ 0x%02x (%s)\n",
537 client
->addr
<< 1, client
->adapter
->name
);
539 decoder
= devm_kzalloc(&client
->dev
, sizeof(*decoder
), GFP_KERNEL
);
544 * Requesting high will assert reset, the line should be
545 * flagged as active low in descriptor table or machine description.
547 decoder
->reset_pin
= devm_gpiod_get(&client
->dev
, "reset",
549 if (IS_ERR(decoder
->reset_pin
))
550 return PTR_ERR(decoder
->reset_pin
);
551 gpiod_set_consumer_name(decoder
->reset_pin
, "ADV7183 Reset");
553 * Requesting low will start with output disabled, the line should be
554 * flagged as active low in descriptor table or machine description.
556 decoder
->oe_pin
= devm_gpiod_get(&client
->dev
, "oe",
558 if (IS_ERR(decoder
->oe_pin
))
559 return PTR_ERR(decoder
->oe_pin
);
560 gpiod_set_consumer_name(decoder
->reset_pin
, "ADV7183 Output Enable");
563 v4l2_i2c_subdev_init(sd
, client
, &adv7183_ops
);
566 v4l2_ctrl_handler_init(hdl
, 4);
567 v4l2_ctrl_new_std(hdl
, &adv7183_ctrl_ops
,
568 V4L2_CID_BRIGHTNESS
, -128, 127, 1, 0);
569 v4l2_ctrl_new_std(hdl
, &adv7183_ctrl_ops
,
570 V4L2_CID_CONTRAST
, 0, 0xFF, 1, 0x80);
571 v4l2_ctrl_new_std(hdl
, &adv7183_ctrl_ops
,
572 V4L2_CID_SATURATION
, 0, 0xFFFF, 1, 0x8080);
573 v4l2_ctrl_new_std(hdl
, &adv7183_ctrl_ops
,
574 V4L2_CID_HUE
, 0, 0xFFFF, 1, 0x8080);
575 /* hook the control handler into the driver */
576 sd
->ctrl_handler
= hdl
;
580 v4l2_ctrl_handler_free(hdl
);
584 /* v4l2 doesn't support an autodetect standard, pick PAL as default */
585 decoder
->std
= V4L2_STD_PAL
;
586 decoder
->input
= ADV7183_COMPOSITE4
;
587 decoder
->output
= ADV7183_8BIT_OUT
;
590 /* reset pulse width at least 5ms */
592 /* De-assert reset line (descriptor tagged active low) */
593 gpiod_set_value(decoder
->reset_pin
, 0);
594 /* wait 5ms before any further i2c writes are performed */
597 adv7183_writeregs(sd
, adv7183_init_regs
, ARRAY_SIZE(adv7183_init_regs
));
598 adv7183_s_std(sd
, decoder
->std
);
599 fmt
.format
.width
= 720;
600 fmt
.format
.height
= 576;
601 adv7183_set_fmt(sd
, NULL
, &fmt
);
603 /* initialize the hardware to the default control values */
604 ret
= v4l2_ctrl_handler_setup(hdl
);
606 v4l2_ctrl_handler_free(hdl
);
613 static void adv7183_remove(struct i2c_client
*client
)
615 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
617 v4l2_device_unregister_subdev(sd
);
618 v4l2_ctrl_handler_free(sd
->ctrl_handler
);
621 static const struct i2c_device_id adv7183_id
[] = {
626 MODULE_DEVICE_TABLE(i2c
, adv7183_id
);
628 static struct i2c_driver adv7183_driver
= {
632 .probe
= adv7183_probe
,
633 .remove
= adv7183_remove
,
634 .id_table
= adv7183_id
,
637 module_i2c_driver(adv7183_driver
);
639 MODULE_DESCRIPTION("Analog Devices ADV7183 video decoder driver");
640 MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>");
641 MODULE_LICENSE("GPL v2");