2 * adv7183.c Analog Devices ADV7183 video decoder driver
4 * Copyright (c) 2011 Analog Devices Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <linux/delay.h>
21 #include <linux/errno.h>
22 #include <linux/gpio.h>
23 #include <linux/i2c.h>
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/types.h>
28 #include <linux/videodev2.h>
30 #include <media/adv7183.h>
31 #include <media/v4l2-chip-ident.h>
32 #include <media/v4l2-ctrls.h>
33 #include <media/v4l2-device.h>
35 #include "adv7183_regs.h"
38 struct v4l2_subdev sd;
39 struct v4l2_ctrl_handler hdl;
41 v4l2_std_id std; /* Current set standard */
46 struct v4l2_mbus_framefmt fmt;
49 /* EXAMPLES USING 27 MHz CLOCK
50 * Mode 1 CVBS Input (Composite Video on AIN5)
51 * All standards are supported through autodetect, 8-bit, 4:2:2, ITU-R BT.656 output on P15 to P8.
53 static const unsigned char adv7183_init_regs[] = {
54 ADV7183_IN_CTRL, 0x04, /* CVBS input on AIN5 */
55 ADV7183_DIGI_CLAMP_CTRL_1, 0x00, /* Slow down digital clamps */
56 ADV7183_SHAP_FILT_CTRL, 0x41, /* Set CSFM to SH1 */
57 ADV7183_ADC_CTRL, 0x16, /* Power down ADC 1 and ADC 2 */
58 ADV7183_CTI_DNR_CTRL_4, 0x04, /* Set DNR threshold to 4 for flat response */
59 /* ADI recommended programming sequence */
60 ADV7183_ADI_CTRL, 0x80,
61 ADV7183_CTI_DNR_CTRL_4, 0x20,
70 ADV7183_SD_SATURATION_CR, 0x3E,
71 ADV7183_PAL_V_END, 0x3E,
72 ADV7183_PAL_F_TOGGLE, 0x0F,
73 ADV7183_ADI_CTRL, 0x00,
76 static inline struct adv7183 *to_adv7183(struct v4l2_subdev *sd)
78 return container_of(sd, struct adv7183, sd);
80 static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
82 return &container_of(ctrl->handler, struct adv7183, hdl)->sd;
85 static inline int adv7183_read(struct v4l2_subdev *sd, unsigned char reg)
87 struct i2c_client *client = v4l2_get_subdevdata(sd);
89 return i2c_smbus_read_byte_data(client, reg);
92 static inline int adv7183_write(struct v4l2_subdev *sd, unsigned char reg,
95 struct i2c_client *client = v4l2_get_subdevdata(sd);
97 return i2c_smbus_write_byte_data(client, reg, value);
100 static int adv7183_writeregs(struct v4l2_subdev *sd,
101 const unsigned char *regs, unsigned int num)
103 unsigned char reg, data;
104 unsigned int cnt = 0;
107 v4l2_err(sd, "invalid regs array\n");
116 adv7183_write(sd, reg, data);
121 static int adv7183_log_status(struct v4l2_subdev *sd)
123 struct adv7183 *decoder = to_adv7183(sd);
125 v4l2_info(sd, "adv7183: Input control = 0x%02x\n",
126 adv7183_read(sd, ADV7183_IN_CTRL));
127 v4l2_info(sd, "adv7183: Video selection = 0x%02x\n",
128 adv7183_read(sd, ADV7183_VD_SEL));
129 v4l2_info(sd, "adv7183: Output control = 0x%02x\n",
130 adv7183_read(sd, ADV7183_OUT_CTRL));
131 v4l2_info(sd, "adv7183: Extended output control = 0x%02x\n",
132 adv7183_read(sd, ADV7183_EXT_OUT_CTRL));
133 v4l2_info(sd, "adv7183: Autodetect enable = 0x%02x\n",
134 adv7183_read(sd, ADV7183_AUTO_DET_EN));
135 v4l2_info(sd, "adv7183: Contrast = 0x%02x\n",
136 adv7183_read(sd, ADV7183_CONTRAST));
137 v4l2_info(sd, "adv7183: Brightness = 0x%02x\n",
138 adv7183_read(sd, ADV7183_BRIGHTNESS));
139 v4l2_info(sd, "adv7183: Hue = 0x%02x\n",
140 adv7183_read(sd, ADV7183_HUE));
141 v4l2_info(sd, "adv7183: Default value Y = 0x%02x\n",
142 adv7183_read(sd, ADV7183_DEF_Y));
143 v4l2_info(sd, "adv7183: Default value C = 0x%02x\n",
144 adv7183_read(sd, ADV7183_DEF_C));
145 v4l2_info(sd, "adv7183: ADI control = 0x%02x\n",
146 adv7183_read(sd, ADV7183_ADI_CTRL));
147 v4l2_info(sd, "adv7183: Power Management = 0x%02x\n",
148 adv7183_read(sd, ADV7183_POW_MANAGE));
149 v4l2_info(sd, "adv7183: Status 1 2 and 3 = 0x%02x 0x%02x 0x%02x\n",
150 adv7183_read(sd, ADV7183_STATUS_1),
151 adv7183_read(sd, ADV7183_STATUS_2),
152 adv7183_read(sd, ADV7183_STATUS_3));
153 v4l2_info(sd, "adv7183: Ident = 0x%02x\n",
154 adv7183_read(sd, ADV7183_IDENT));
155 v4l2_info(sd, "adv7183: Analog clamp control = 0x%02x\n",
156 adv7183_read(sd, ADV7183_ANAL_CLAMP_CTRL));
157 v4l2_info(sd, "adv7183: Digital clamp control 1 = 0x%02x\n",
158 adv7183_read(sd, ADV7183_DIGI_CLAMP_CTRL_1));
159 v4l2_info(sd, "adv7183: Shaping filter control 1 and 2 = 0x%02x 0x%02x\n",
160 adv7183_read(sd, ADV7183_SHAP_FILT_CTRL),
161 adv7183_read(sd, ADV7183_SHAP_FILT_CTRL_2));
162 v4l2_info(sd, "adv7183: Comb filter control = 0x%02x\n",
163 adv7183_read(sd, ADV7183_COMB_FILT_CTRL));
164 v4l2_info(sd, "adv7183: ADI control 2 = 0x%02x\n",
165 adv7183_read(sd, ADV7183_ADI_CTRL_2));
166 v4l2_info(sd, "adv7183: Pixel delay control = 0x%02x\n",
167 adv7183_read(sd, ADV7183_PIX_DELAY_CTRL));
168 v4l2_info(sd, "adv7183: Misc gain control = 0x%02x\n",
169 adv7183_read(sd, ADV7183_MISC_GAIN_CTRL));
170 v4l2_info(sd, "adv7183: AGC mode control = 0x%02x\n",
171 adv7183_read(sd, ADV7183_AGC_MODE_CTRL));
172 v4l2_info(sd, "adv7183: Chroma gain control 1 and 2 = 0x%02x 0x%02x\n",
173 adv7183_read(sd, ADV7183_CHRO_GAIN_CTRL_1),
174 adv7183_read(sd, ADV7183_CHRO_GAIN_CTRL_2));
175 v4l2_info(sd, "adv7183: Luma gain control 1 and 2 = 0x%02x 0x%02x\n",
176 adv7183_read(sd, ADV7183_LUMA_GAIN_CTRL_1),
177 adv7183_read(sd, ADV7183_LUMA_GAIN_CTRL_2));
178 v4l2_info(sd, "adv7183: Vsync field control 1 2 and 3 = 0x%02x 0x%02x 0x%02x\n",
179 adv7183_read(sd, ADV7183_VS_FIELD_CTRL_1),
180 adv7183_read(sd, ADV7183_VS_FIELD_CTRL_2),
181 adv7183_read(sd, ADV7183_VS_FIELD_CTRL_3));
182 v4l2_info(sd, "adv7183: Hsync positon control 1 2 and 3 = 0x%02x 0x%02x 0x%02x\n",
183 adv7183_read(sd, ADV7183_HS_POS_CTRL_1),
184 adv7183_read(sd, ADV7183_HS_POS_CTRL_2),
185 adv7183_read(sd, ADV7183_HS_POS_CTRL_3));
186 v4l2_info(sd, "adv7183: Polarity = 0x%02x\n",
187 adv7183_read(sd, ADV7183_POLARITY));
188 v4l2_info(sd, "adv7183: ADC control = 0x%02x\n",
189 adv7183_read(sd, ADV7183_ADC_CTRL));
190 v4l2_info(sd, "adv7183: SD offset Cb and Cr = 0x%02x 0x%02x\n",
191 adv7183_read(sd, ADV7183_SD_OFFSET_CB),
192 adv7183_read(sd, ADV7183_SD_OFFSET_CR));
193 v4l2_info(sd, "adv7183: SD saturation Cb and Cr = 0x%02x 0x%02x\n",
194 adv7183_read(sd, ADV7183_SD_SATURATION_CB),
195 adv7183_read(sd, ADV7183_SD_SATURATION_CR));
196 v4l2_info(sd, "adv7183: Drive strength = 0x%02x\n",
197 adv7183_read(sd, ADV7183_DRIVE_STR));
198 v4l2_ctrl_handler_log_status(&decoder->hdl, sd->name);
202 static int adv7183_g_std(struct v4l2_subdev *sd, v4l2_std_id *std)
204 struct adv7183 *decoder = to_adv7183(sd);
210 static int adv7183_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
212 struct adv7183 *decoder = to_adv7183(sd);
215 reg = adv7183_read(sd, ADV7183_IN_CTRL) & 0xF;
216 if (std == V4L2_STD_PAL_60)
218 else if (std == V4L2_STD_NTSC_443)
220 else if (std == V4L2_STD_PAL_N)
222 else if (std == V4L2_STD_PAL_M)
224 else if (std == V4L2_STD_PAL_Nc)
226 else if (std & V4L2_STD_PAL)
228 else if (std & V4L2_STD_NTSC)
230 else if (std & V4L2_STD_SECAM)
234 adv7183_write(sd, ADV7183_IN_CTRL, reg);
241 static int adv7183_reset(struct v4l2_subdev *sd, u32 val)
245 reg = adv7183_read(sd, ADV7183_POW_MANAGE) | 0x80;
246 adv7183_write(sd, ADV7183_POW_MANAGE, reg);
247 /* wait 5ms before any further i2c writes are performed */
248 usleep_range(5000, 10000);
252 static int adv7183_s_routing(struct v4l2_subdev *sd,
253 u32 input, u32 output, u32 config)
255 struct adv7183 *decoder = to_adv7183(sd);
258 if ((input > ADV7183_COMPONENT1) || (output > ADV7183_16BIT_OUT))
261 if (input != decoder->input) {
262 decoder->input = input;
263 reg = adv7183_read(sd, ADV7183_IN_CTRL) & 0xF0;
265 case ADV7183_COMPOSITE1:
268 case ADV7183_COMPOSITE2:
271 case ADV7183_COMPOSITE3:
274 case ADV7183_COMPOSITE4:
277 case ADV7183_COMPOSITE5:
280 case ADV7183_COMPOSITE6:
283 case ADV7183_COMPOSITE7:
286 case ADV7183_COMPOSITE8:
289 case ADV7183_COMPOSITE9:
292 case ADV7183_COMPOSITE10:
295 case ADV7183_SVIDEO0:
298 case ADV7183_SVIDEO1:
301 case ADV7183_SVIDEO2:
304 case ADV7183_COMPONENT0:
307 case ADV7183_COMPONENT1:
313 adv7183_write(sd, ADV7183_IN_CTRL, reg);
316 if (output != decoder->output) {
317 decoder->output = output;
318 reg = adv7183_read(sd, ADV7183_OUT_CTRL) & 0xC0;
320 case ADV7183_16BIT_OUT:
327 adv7183_write(sd, ADV7183_OUT_CTRL, reg);
333 static int adv7183_s_ctrl(struct v4l2_ctrl *ctrl)
335 struct v4l2_subdev *sd = to_sd(ctrl);
339 case V4L2_CID_BRIGHTNESS:
342 adv7183_write(sd, ADV7183_BRIGHTNESS, val);
344 case V4L2_CID_CONTRAST:
345 adv7183_write(sd, ADV7183_CONTRAST, val);
347 case V4L2_CID_SATURATION:
348 adv7183_write(sd, ADV7183_SD_SATURATION_CB, val >> 8);
349 adv7183_write(sd, ADV7183_SD_SATURATION_CR, (val & 0xFF));
352 adv7183_write(sd, ADV7183_SD_OFFSET_CB, val >> 8);
353 adv7183_write(sd, ADV7183_SD_OFFSET_CR, (val & 0xFF));
362 static int adv7183_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
364 struct adv7183 *decoder = to_adv7183(sd);
367 /* enable autodetection block */
368 reg = adv7183_read(sd, ADV7183_IN_CTRL) & 0xF;
369 adv7183_write(sd, ADV7183_IN_CTRL, reg);
371 /* wait autodetection switch */
374 /* get autodetection result */
375 reg = adv7183_read(sd, ADV7183_STATUS_1);
376 switch ((reg >> 0x4) & 0x7) {
378 *std = V4L2_STD_NTSC;
381 *std = V4L2_STD_NTSC_443;
384 *std = V4L2_STD_PAL_M;
387 *std = V4L2_STD_PAL_60;
393 *std = V4L2_STD_SECAM;
396 *std = V4L2_STD_PAL_Nc;
399 *std = V4L2_STD_SECAM;
402 *std = V4L2_STD_UNKNOWN;
406 /* after std detection, write back user set std */
407 adv7183_s_std(sd, decoder->std);
411 static int adv7183_g_input_status(struct v4l2_subdev *sd, u32 *status)
415 *status = V4L2_IN_ST_NO_SIGNAL;
416 reg = adv7183_read(sd, ADV7183_STATUS_1);
424 static int adv7183_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned index,
425 enum v4l2_mbus_pixelcode *code)
430 *code = V4L2_MBUS_FMT_UYVY8_2X8;
434 static int adv7183_try_mbus_fmt(struct v4l2_subdev *sd,
435 struct v4l2_mbus_framefmt *fmt)
437 struct adv7183 *decoder = to_adv7183(sd);
439 fmt->code = V4L2_MBUS_FMT_UYVY8_2X8;
440 fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
441 if (decoder->std & V4L2_STD_525_60) {
442 fmt->field = V4L2_FIELD_SEQ_TB;
446 fmt->field = V4L2_FIELD_SEQ_BT;
453 static int adv7183_s_mbus_fmt(struct v4l2_subdev *sd,
454 struct v4l2_mbus_framefmt *fmt)
456 struct adv7183 *decoder = to_adv7183(sd);
458 adv7183_try_mbus_fmt(sd, fmt);
463 static int adv7183_g_mbus_fmt(struct v4l2_subdev *sd,
464 struct v4l2_mbus_framefmt *fmt)
466 struct adv7183 *decoder = to_adv7183(sd);
472 static int adv7183_s_stream(struct v4l2_subdev *sd, int enable)
474 struct adv7183 *decoder = to_adv7183(sd);
477 gpio_direction_output(decoder->oe_pin, 0);
479 gpio_direction_output(decoder->oe_pin, 1);
484 static int adv7183_g_chip_ident(struct v4l2_subdev *sd,
485 struct v4l2_dbg_chip_ident *chip)
488 struct i2c_client *client = v4l2_get_subdevdata(sd);
490 /* 0x11 for adv7183, 0x13 for adv7183b */
491 rev = adv7183_read(sd, ADV7183_IDENT);
493 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7183, rev);
496 #ifdef CONFIG_VIDEO_ADV_DEBUG
497 static int adv7183_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
499 struct i2c_client *client = v4l2_get_subdevdata(sd);
501 if (!v4l2_chip_match_i2c_client(client, ®->match))
503 if (!capable(CAP_SYS_ADMIN))
505 reg->val = adv7183_read(sd, reg->reg & 0xff);
510 static int adv7183_s_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
512 struct i2c_client *client = v4l2_get_subdevdata(sd);
514 if (!v4l2_chip_match_i2c_client(client, ®->match))
516 if (!capable(CAP_SYS_ADMIN))
518 adv7183_write(sd, reg->reg & 0xff, reg->val & 0xff);
523 static const struct v4l2_ctrl_ops adv7183_ctrl_ops = {
524 .s_ctrl = adv7183_s_ctrl,
527 static const struct v4l2_subdev_core_ops adv7183_core_ops = {
528 .log_status = adv7183_log_status,
529 .g_std = adv7183_g_std,
530 .s_std = adv7183_s_std,
531 .reset = adv7183_reset,
532 .g_chip_ident = adv7183_g_chip_ident,
533 #ifdef CONFIG_VIDEO_ADV_DEBUG
534 .g_register = adv7183_g_register,
535 .s_register = adv7183_s_register,
539 static const struct v4l2_subdev_video_ops adv7183_video_ops = {
540 .s_routing = adv7183_s_routing,
541 .querystd = adv7183_querystd,
542 .g_input_status = adv7183_g_input_status,
543 .enum_mbus_fmt = adv7183_enum_mbus_fmt,
544 .try_mbus_fmt = adv7183_try_mbus_fmt,
545 .s_mbus_fmt = adv7183_s_mbus_fmt,
546 .g_mbus_fmt = adv7183_g_mbus_fmt,
547 .s_stream = adv7183_s_stream,
550 static const struct v4l2_subdev_ops adv7183_ops = {
551 .core = &adv7183_core_ops,
552 .video = &adv7183_video_ops,
555 static int adv7183_probe(struct i2c_client *client,
556 const struct i2c_device_id *id)
558 struct adv7183 *decoder;
559 struct v4l2_subdev *sd;
560 struct v4l2_ctrl_handler *hdl;
562 struct v4l2_mbus_framefmt fmt;
563 const unsigned *pin_array;
565 /* Check if the adapter supports the needed features */
566 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
569 v4l_info(client, "chip found @ 0x%02x (%s)\n",
570 client->addr << 1, client->adapter->name);
572 pin_array = client->dev.platform_data;
573 if (pin_array == NULL)
576 decoder = kzalloc(sizeof(struct adv7183), GFP_KERNEL);
580 decoder->reset_pin = pin_array[0];
581 decoder->oe_pin = pin_array[1];
583 if (gpio_request(decoder->reset_pin, "ADV7183 Reset")) {
584 v4l_err(client, "failed to request GPIO %d\n", decoder->reset_pin);
586 goto err_free_decoder;
589 if (gpio_request(decoder->oe_pin, "ADV7183 Output Enable")) {
590 v4l_err(client, "failed to request GPIO %d\n", decoder->oe_pin);
596 v4l2_i2c_subdev_init(sd, client, &adv7183_ops);
599 v4l2_ctrl_handler_init(hdl, 4);
600 v4l2_ctrl_new_std(hdl, &adv7183_ctrl_ops,
601 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
602 v4l2_ctrl_new_std(hdl, &adv7183_ctrl_ops,
603 V4L2_CID_CONTRAST, 0, 0xFF, 1, 0x80);
604 v4l2_ctrl_new_std(hdl, &adv7183_ctrl_ops,
605 V4L2_CID_SATURATION, 0, 0xFFFF, 1, 0x8080);
606 v4l2_ctrl_new_std(hdl, &adv7183_ctrl_ops,
607 V4L2_CID_HUE, 0, 0xFFFF, 1, 0x8080);
608 /* hook the control handler into the driver */
609 sd->ctrl_handler = hdl;
613 v4l2_ctrl_handler_free(hdl);
617 /* v4l2 doesn't support an autodetect standard, pick PAL as default */
618 decoder->std = V4L2_STD_PAL;
619 decoder->input = ADV7183_COMPOSITE4;
620 decoder->output = ADV7183_8BIT_OUT;
622 gpio_direction_output(decoder->oe_pin, 1);
624 gpio_direction_output(decoder->reset_pin, 0);
625 /* reset pulse width at least 5ms */
627 gpio_direction_output(decoder->reset_pin, 1);
628 /* wait 5ms before any further i2c writes are performed */
631 adv7183_writeregs(sd, adv7183_init_regs, ARRAY_SIZE(adv7183_init_regs));
632 adv7183_s_std(sd, decoder->std);
635 adv7183_s_mbus_fmt(sd, &fmt);
637 /* initialize the hardware to the default control values */
638 ret = v4l2_ctrl_handler_setup(hdl);
640 v4l2_ctrl_handler_free(hdl);
646 gpio_free(decoder->oe_pin);
648 gpio_free(decoder->reset_pin);
654 static int adv7183_remove(struct i2c_client *client)
656 struct v4l2_subdev *sd = i2c_get_clientdata(client);
657 struct adv7183 *decoder = to_adv7183(sd);
659 v4l2_device_unregister_subdev(sd);
660 v4l2_ctrl_handler_free(sd->ctrl_handler);
661 gpio_free(decoder->oe_pin);
662 gpio_free(decoder->reset_pin);
667 static const struct i2c_device_id adv7183_id[] = {
672 MODULE_DEVICE_TABLE(i2c, adv7183_id);
674 static struct i2c_driver adv7183_driver = {
676 .owner = THIS_MODULE,
679 .probe = adv7183_probe,
680 .remove = __devexit_p(adv7183_remove),
681 .id_table = adv7183_id,
684 static __init int adv7183_init(void)
686 return i2c_add_driver(&adv7183_driver);
689 static __exit void adv7183_exit(void)
691 i2c_del_driver(&adv7183_driver);
694 module_init(adv7183_init);
695 module_exit(adv7183_exit);
697 MODULE_DESCRIPTION("Analog Devices ADV7183 video decoder driver");
698 MODULE_AUTHOR("Scott Jiang <Scott.Jiang.Linux@gmail.com>");
699 MODULE_LICENSE("GPL v2");