2 * adv7180.c Analog Devices ADV7180 video decoder driver
3 * Copyright (c) 2009 Intel Corporation
4 * Copyright (C) 2013 Cogent Embedded, Inc.
5 * Copyright (C) 2013 Renesas Solutions Corp.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/errno.h>
24 #include <linux/kernel.h>
25 #include <linux/interrupt.h>
26 #include <linux/i2c.h>
27 #include <linux/slab.h>
28 #include <media/v4l2-ioctl.h>
29 #include <linux/videodev2.h>
30 #include <media/v4l2-device.h>
31 #include <media/v4l2-ctrls.h>
32 #include <linux/mutex.h>
34 #define ADV7180_INPUT_CONTROL_REG 0x00
35 #define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM 0x00
36 #define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM_PED 0x10
37 #define ADV7180_INPUT_CONTROL_AD_PAL_N_NTSC_J_SECAM 0x20
38 #define ADV7180_INPUT_CONTROL_AD_PAL_N_NTSC_M_SECAM 0x30
39 #define ADV7180_INPUT_CONTROL_NTSC_J 0x40
40 #define ADV7180_INPUT_CONTROL_NTSC_M 0x50
41 #define ADV7180_INPUT_CONTROL_PAL60 0x60
42 #define ADV7180_INPUT_CONTROL_NTSC_443 0x70
43 #define ADV7180_INPUT_CONTROL_PAL_BG 0x80
44 #define ADV7180_INPUT_CONTROL_PAL_N 0x90
45 #define ADV7180_INPUT_CONTROL_PAL_M 0xa0
46 #define ADV7180_INPUT_CONTROL_PAL_M_PED 0xb0
47 #define ADV7180_INPUT_CONTROL_PAL_COMB_N 0xc0
48 #define ADV7180_INPUT_CONTROL_PAL_COMB_N_PED 0xd0
49 #define ADV7180_INPUT_CONTROL_PAL_SECAM 0xe0
50 #define ADV7180_INPUT_CONTROL_PAL_SECAM_PED 0xf0
51 #define ADV7180_INPUT_CONTROL_INSEL_MASK 0x0f
53 #define ADV7180_EXTENDED_OUTPUT_CONTROL_REG 0x04
54 #define ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS 0xC5
56 #define ADV7180_AUTODETECT_ENABLE_REG 0x07
57 #define ADV7180_AUTODETECT_DEFAULT 0x7f
59 #define ADV7180_CON_REG 0x08 /*Unsigned */
60 #define ADV7180_CON_MIN 0
61 #define ADV7180_CON_DEF 128
62 #define ADV7180_CON_MAX 255
64 #define ADV7180_BRI_REG 0x0a /*Signed */
65 #define ADV7180_BRI_MIN -128
66 #define ADV7180_BRI_DEF 0
67 #define ADV7180_BRI_MAX 127
69 #define ADV7180_HUE_REG 0x0b /*Signed, inverted */
70 #define ADV7180_HUE_MIN -127
71 #define ADV7180_HUE_DEF 0
72 #define ADV7180_HUE_MAX 128
74 #define ADV7180_ADI_CTRL_REG 0x0e
75 #define ADV7180_ADI_CTRL_IRQ_SPACE 0x20
77 #define ADV7180_PWR_MAN_REG 0x0f
78 #define ADV7180_PWR_MAN_ON 0x04
79 #define ADV7180_PWR_MAN_OFF 0x24
80 #define ADV7180_PWR_MAN_RES 0x80
82 #define ADV7180_STATUS1_REG 0x10
83 #define ADV7180_STATUS1_IN_LOCK 0x01
84 #define ADV7180_STATUS1_AUTOD_MASK 0x70
85 #define ADV7180_STATUS1_AUTOD_NTSM_M_J 0x00
86 #define ADV7180_STATUS1_AUTOD_NTSC_4_43 0x10
87 #define ADV7180_STATUS1_AUTOD_PAL_M 0x20
88 #define ADV7180_STATUS1_AUTOD_PAL_60 0x30
89 #define ADV7180_STATUS1_AUTOD_PAL_B_G 0x40
90 #define ADV7180_STATUS1_AUTOD_SECAM 0x50
91 #define ADV7180_STATUS1_AUTOD_PAL_COMB 0x60
92 #define ADV7180_STATUS1_AUTOD_SECAM_525 0x70
94 #define ADV7180_IDENT_REG 0x11
95 #define ADV7180_ID_7180 0x18
97 #define ADV7180_ICONF1_ADI 0x40
98 #define ADV7180_ICONF1_ACTIVE_LOW 0x01
99 #define ADV7180_ICONF1_PSYNC_ONLY 0x10
100 #define ADV7180_ICONF1_ACTIVE_TO_CLR 0xC0
102 #define ADV7180_SD_SAT_CB_REG 0xe3 /*Unsigned */
103 #define ADV7180_SD_SAT_CR_REG 0xe4 /*Unsigned */
104 #define ADV7180_SAT_MIN 0
105 #define ADV7180_SAT_DEF 128
106 #define ADV7180_SAT_MAX 255
108 #define ADV7180_IRQ1_LOCK 0x01
109 #define ADV7180_IRQ1_UNLOCK 0x02
110 #define ADV7180_ISR1_ADI 0x42
111 #define ADV7180_ICR1_ADI 0x43
112 #define ADV7180_IMR1_ADI 0x44
113 #define ADV7180_IMR2_ADI 0x48
114 #define ADV7180_IRQ3_AD_CHANGE 0x08
115 #define ADV7180_ISR3_ADI 0x4A
116 #define ADV7180_ICR3_ADI 0x4B
117 #define ADV7180_IMR3_ADI 0x4C
118 #define ADV7180_IMR4_ADI 0x50
120 #define ADV7180_NTSC_V_BIT_END_REG 0xE6
121 #define ADV7180_NTSC_V_BIT_END_MANUAL_NVEND 0x4F
123 struct adv7180_state {
124 struct v4l2_ctrl_handler ctrl_hdl;
125 struct v4l2_subdev sd;
126 struct mutex mutex; /* mutual excl. when accessing chip */
128 v4l2_std_id curr_norm;
133 #define to_adv7180_sd(_ctrl) (&container_of(_ctrl->handler, \
134 struct adv7180_state, \
137 static v4l2_std_id adv7180_std_to_v4l2(u8 status1)
139 /* in case V4L2_IN_ST_NO_SIGNAL */
140 if (!(status1 & ADV7180_STATUS1_IN_LOCK))
141 return V4L2_STD_UNKNOWN;
143 switch (status1 & ADV7180_STATUS1_AUTOD_MASK) {
144 case ADV7180_STATUS1_AUTOD_NTSM_M_J:
145 return V4L2_STD_NTSC;
146 case ADV7180_STATUS1_AUTOD_NTSC_4_43:
147 return V4L2_STD_NTSC_443;
148 case ADV7180_STATUS1_AUTOD_PAL_M:
149 return V4L2_STD_PAL_M;
150 case ADV7180_STATUS1_AUTOD_PAL_60:
151 return V4L2_STD_PAL_60;
152 case ADV7180_STATUS1_AUTOD_PAL_B_G:
154 case ADV7180_STATUS1_AUTOD_SECAM:
155 return V4L2_STD_SECAM;
156 case ADV7180_STATUS1_AUTOD_PAL_COMB:
157 return V4L2_STD_PAL_Nc | V4L2_STD_PAL_N;
158 case ADV7180_STATUS1_AUTOD_SECAM_525:
159 return V4L2_STD_SECAM;
161 return V4L2_STD_UNKNOWN;
165 static int v4l2_std_to_adv7180(v4l2_std_id std)
167 if (std == V4L2_STD_PAL_60)
168 return ADV7180_INPUT_CONTROL_PAL60;
169 if (std == V4L2_STD_NTSC_443)
170 return ADV7180_INPUT_CONTROL_NTSC_443;
171 if (std == V4L2_STD_PAL_N)
172 return ADV7180_INPUT_CONTROL_PAL_N;
173 if (std == V4L2_STD_PAL_M)
174 return ADV7180_INPUT_CONTROL_PAL_M;
175 if (std == V4L2_STD_PAL_Nc)
176 return ADV7180_INPUT_CONTROL_PAL_COMB_N;
178 if (std & V4L2_STD_PAL)
179 return ADV7180_INPUT_CONTROL_PAL_BG;
180 if (std & V4L2_STD_NTSC)
181 return ADV7180_INPUT_CONTROL_NTSC_M;
182 if (std & V4L2_STD_SECAM)
183 return ADV7180_INPUT_CONTROL_PAL_SECAM;
188 static u32 adv7180_status_to_v4l2(u8 status1)
190 if (!(status1 & ADV7180_STATUS1_IN_LOCK))
191 return V4L2_IN_ST_NO_SIGNAL;
196 static int __adv7180_status(struct i2c_client *client, u32 *status,
199 int status1 = i2c_smbus_read_byte_data(client, ADV7180_STATUS1_REG);
205 *status = adv7180_status_to_v4l2(status1);
207 *std = adv7180_std_to_v4l2(status1);
212 static inline struct adv7180_state *to_state(struct v4l2_subdev *sd)
214 return container_of(sd, struct adv7180_state, sd);
217 static int adv7180_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
219 struct adv7180_state *state = to_state(sd);
220 int err = mutex_lock_interruptible(&state->mutex);
224 /* when we are interrupt driven we know the state */
225 if (!state->autodetect || state->irq > 0)
226 *std = state->curr_norm;
228 err = __adv7180_status(v4l2_get_subdevdata(sd), NULL, std);
230 mutex_unlock(&state->mutex);
234 static int adv7180_s_routing(struct v4l2_subdev *sd, u32 input,
235 u32 output, u32 config)
237 struct adv7180_state *state = to_state(sd);
238 int ret = mutex_lock_interruptible(&state->mutex);
239 struct i2c_client *client = v4l2_get_subdevdata(sd);
244 /* We cannot discriminate between LQFP and 40-pin LFCSP, so accept
245 * all inputs and let the card driver take care of validation
247 if ((input & ADV7180_INPUT_CONTROL_INSEL_MASK) != input)
250 ret = i2c_smbus_read_byte_data(client, ADV7180_INPUT_CONTROL_REG);
255 ret &= ~ADV7180_INPUT_CONTROL_INSEL_MASK;
256 ret = i2c_smbus_write_byte_data(client,
257 ADV7180_INPUT_CONTROL_REG, ret | input);
258 state->input = input;
260 mutex_unlock(&state->mutex);
264 static int adv7180_g_input_status(struct v4l2_subdev *sd, u32 *status)
266 struct adv7180_state *state = to_state(sd);
267 int ret = mutex_lock_interruptible(&state->mutex);
271 ret = __adv7180_status(v4l2_get_subdevdata(sd), status, NULL);
272 mutex_unlock(&state->mutex);
276 static int adv7180_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
278 struct adv7180_state *state = to_state(sd);
279 struct i2c_client *client = v4l2_get_subdevdata(sd);
280 int ret = mutex_lock_interruptible(&state->mutex);
284 /* all standards -> autodetect */
285 if (std == V4L2_STD_ALL) {
287 i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG,
288 ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM
293 __adv7180_status(client, NULL, &state->curr_norm);
294 state->autodetect = true;
296 ret = v4l2_std_to_adv7180(std);
300 ret = i2c_smbus_write_byte_data(client,
301 ADV7180_INPUT_CONTROL_REG,
306 state->curr_norm = std;
307 state->autodetect = false;
311 mutex_unlock(&state->mutex);
315 static int adv7180_set_power(struct adv7180_state *state,
316 struct i2c_client *client, bool on)
321 val = ADV7180_PWR_MAN_ON;
323 val = ADV7180_PWR_MAN_OFF;
325 return i2c_smbus_write_byte_data(client, ADV7180_PWR_MAN_REG, val);
328 static int adv7180_s_power(struct v4l2_subdev *sd, int on)
330 struct adv7180_state *state = to_state(sd);
331 struct i2c_client *client = v4l2_get_subdevdata(sd);
334 ret = mutex_lock_interruptible(&state->mutex);
338 ret = adv7180_set_power(state, client, on);
342 mutex_unlock(&state->mutex);
346 static int adv7180_s_ctrl(struct v4l2_ctrl *ctrl)
348 struct v4l2_subdev *sd = to_adv7180_sd(ctrl);
349 struct adv7180_state *state = to_state(sd);
350 struct i2c_client *client = v4l2_get_subdevdata(sd);
351 int ret = mutex_lock_interruptible(&state->mutex);
358 case V4L2_CID_BRIGHTNESS:
359 ret = i2c_smbus_write_byte_data(client, ADV7180_BRI_REG, val);
362 /*Hue is inverted according to HSL chart */
363 ret = i2c_smbus_write_byte_data(client, ADV7180_HUE_REG, -val);
365 case V4L2_CID_CONTRAST:
366 ret = i2c_smbus_write_byte_data(client, ADV7180_CON_REG, val);
368 case V4L2_CID_SATURATION:
370 *This could be V4L2_CID_BLUE_BALANCE/V4L2_CID_RED_BALANCE
371 *Let's not confuse the user, everybody understands saturation
373 ret = i2c_smbus_write_byte_data(client, ADV7180_SD_SAT_CB_REG,
377 ret = i2c_smbus_write_byte_data(client, ADV7180_SD_SAT_CR_REG,
384 mutex_unlock(&state->mutex);
388 static const struct v4l2_ctrl_ops adv7180_ctrl_ops = {
389 .s_ctrl = adv7180_s_ctrl,
392 static int adv7180_init_controls(struct adv7180_state *state)
394 v4l2_ctrl_handler_init(&state->ctrl_hdl, 4);
396 v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
397 V4L2_CID_BRIGHTNESS, ADV7180_BRI_MIN,
398 ADV7180_BRI_MAX, 1, ADV7180_BRI_DEF);
399 v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
400 V4L2_CID_CONTRAST, ADV7180_CON_MIN,
401 ADV7180_CON_MAX, 1, ADV7180_CON_DEF);
402 v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
403 V4L2_CID_SATURATION, ADV7180_SAT_MIN,
404 ADV7180_SAT_MAX, 1, ADV7180_SAT_DEF);
405 v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
406 V4L2_CID_HUE, ADV7180_HUE_MIN,
407 ADV7180_HUE_MAX, 1, ADV7180_HUE_DEF);
408 state->sd.ctrl_handler = &state->ctrl_hdl;
409 if (state->ctrl_hdl.error) {
410 int err = state->ctrl_hdl.error;
412 v4l2_ctrl_handler_free(&state->ctrl_hdl);
415 v4l2_ctrl_handler_setup(&state->ctrl_hdl);
419 static void adv7180_exit_controls(struct adv7180_state *state)
421 v4l2_ctrl_handler_free(&state->ctrl_hdl);
424 static int adv7180_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned int index,
425 enum v4l2_mbus_pixelcode *code)
430 *code = V4L2_MBUS_FMT_YUYV8_2X8;
435 static int adv7180_mbus_fmt(struct v4l2_subdev *sd,
436 struct v4l2_mbus_framefmt *fmt)
438 struct adv7180_state *state = to_state(sd);
440 fmt->code = V4L2_MBUS_FMT_YUYV8_2X8;
441 fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
442 fmt->field = V4L2_FIELD_INTERLACED;
444 fmt->height = state->curr_norm & V4L2_STD_525_60 ? 480 : 576;
449 static int adv7180_g_mbus_config(struct v4l2_subdev *sd,
450 struct v4l2_mbus_config *cfg)
453 * The ADV7180 sensor supports BT.601/656 output modes.
454 * The BT.656 is default and not yet configurable by s/w.
456 cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_PCLK_SAMPLE_RISING |
457 V4L2_MBUS_DATA_ACTIVE_HIGH;
458 cfg->type = V4L2_MBUS_BT656;
463 static const struct v4l2_subdev_video_ops adv7180_video_ops = {
464 .querystd = adv7180_querystd,
465 .g_input_status = adv7180_g_input_status,
466 .s_routing = adv7180_s_routing,
467 .enum_mbus_fmt = adv7180_enum_mbus_fmt,
468 .try_mbus_fmt = adv7180_mbus_fmt,
469 .g_mbus_fmt = adv7180_mbus_fmt,
470 .s_mbus_fmt = adv7180_mbus_fmt,
471 .g_mbus_config = adv7180_g_mbus_config,
474 static const struct v4l2_subdev_core_ops adv7180_core_ops = {
475 .s_std = adv7180_s_std,
476 .s_power = adv7180_s_power,
479 static const struct v4l2_subdev_ops adv7180_ops = {
480 .core = &adv7180_core_ops,
481 .video = &adv7180_video_ops,
484 static irqreturn_t adv7180_irq(int irq, void *devid)
486 struct adv7180_state *state = devid;
487 struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
490 mutex_lock(&state->mutex);
491 i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
492 ADV7180_ADI_CTRL_IRQ_SPACE);
493 isr3 = i2c_smbus_read_byte_data(client, ADV7180_ISR3_ADI);
495 i2c_smbus_write_byte_data(client, ADV7180_ICR3_ADI, isr3);
496 i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG, 0);
498 if (isr3 & ADV7180_IRQ3_AD_CHANGE && state->autodetect)
499 __adv7180_status(client, NULL, &state->curr_norm);
500 mutex_unlock(&state->mutex);
505 static int init_device(struct i2c_client *client, struct adv7180_state *state)
509 /* Initialize adv7180 */
510 /* Enable autodetection */
511 if (state->autodetect) {
513 i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG,
514 ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM
520 i2c_smbus_write_byte_data(client,
521 ADV7180_AUTODETECT_ENABLE_REG,
522 ADV7180_AUTODETECT_DEFAULT);
526 ret = v4l2_std_to_adv7180(state->curr_norm);
531 i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG,
537 /* ITU-R BT.656-4 compatible */
538 ret = i2c_smbus_write_byte_data(client,
539 ADV7180_EXTENDED_OUTPUT_CONTROL_REG,
540 ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS);
544 /* Manually set V bit end position in NTSC mode */
545 ret = i2c_smbus_write_byte_data(client,
546 ADV7180_NTSC_V_BIT_END_REG,
547 ADV7180_NTSC_V_BIT_END_MANUAL_NVEND);
551 /* read current norm */
552 __adv7180_status(client, NULL, &state->curr_norm);
554 /* register for interrupts */
555 if (state->irq > 0) {
556 ret = request_threaded_irq(state->irq, NULL, adv7180_irq,
557 IRQF_ONESHOT, KBUILD_MODNAME, state);
561 ret = i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
562 ADV7180_ADI_CTRL_IRQ_SPACE);
566 /* config the Interrupt pin to be active low */
567 ret = i2c_smbus_write_byte_data(client, ADV7180_ICONF1_ADI,
568 ADV7180_ICONF1_ACTIVE_LOW |
569 ADV7180_ICONF1_PSYNC_ONLY);
573 ret = i2c_smbus_write_byte_data(client, ADV7180_IMR1_ADI, 0);
577 ret = i2c_smbus_write_byte_data(client, ADV7180_IMR2_ADI, 0);
581 /* enable AD change interrupts interrupts */
582 ret = i2c_smbus_write_byte_data(client, ADV7180_IMR3_ADI,
583 ADV7180_IRQ3_AD_CHANGE);
587 ret = i2c_smbus_write_byte_data(client, ADV7180_IMR4_ADI, 0);
591 ret = i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
600 free_irq(state->irq, state);
604 static int adv7180_probe(struct i2c_client *client,
605 const struct i2c_device_id *id)
607 struct adv7180_state *state;
608 struct v4l2_subdev *sd;
611 /* Check if the adapter supports the needed features */
612 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
615 v4l_info(client, "chip found @ 0x%02x (%s)\n",
616 client->addr, client->adapter->name);
618 state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
624 state->irq = client->irq;
625 mutex_init(&state->mutex);
626 state->autodetect = true;
627 state->powered = true;
630 v4l2_i2c_subdev_init(sd, client, &adv7180_ops);
632 ret = adv7180_init_controls(state);
634 goto err_unreg_subdev;
635 ret = init_device(client, state);
639 ret = v4l2_async_register_subdev(sd);
647 free_irq(client->irq, state);
649 adv7180_exit_controls(state);
651 mutex_destroy(&state->mutex);
656 static int adv7180_remove(struct i2c_client *client)
658 struct v4l2_subdev *sd = i2c_get_clientdata(client);
659 struct adv7180_state *state = to_state(sd);
661 v4l2_async_unregister_subdev(sd);
664 free_irq(client->irq, state);
666 v4l2_device_unregister_subdev(sd);
667 adv7180_exit_controls(state);
668 mutex_destroy(&state->mutex);
672 static const struct i2c_device_id adv7180_id[] = {
677 #ifdef CONFIG_PM_SLEEP
678 static int adv7180_suspend(struct device *dev)
680 struct i2c_client *client = to_i2c_client(dev);
681 struct v4l2_subdev *sd = i2c_get_clientdata(client);
682 struct adv7180_state *state = to_state(sd);
684 return adv7180_set_power(state, client, false);
687 static int adv7180_resume(struct device *dev)
689 struct i2c_client *client = to_i2c_client(dev);
690 struct v4l2_subdev *sd = i2c_get_clientdata(client);
691 struct adv7180_state *state = to_state(sd);
694 if (state->powered) {
695 ret = adv7180_set_power(state, client, true);
699 ret = init_device(client, state);
705 static SIMPLE_DEV_PM_OPS(adv7180_pm_ops, adv7180_suspend, adv7180_resume);
706 #define ADV7180_PM_OPS (&adv7180_pm_ops)
709 #define ADV7180_PM_OPS NULL
712 MODULE_DEVICE_TABLE(i2c, adv7180_id);
714 static struct i2c_driver adv7180_driver = {
716 .owner = THIS_MODULE,
717 .name = KBUILD_MODNAME,
718 .pm = ADV7180_PM_OPS,
720 .probe = adv7180_probe,
721 .remove = adv7180_remove,
722 .id_table = adv7180_id,
725 module_i2c_driver(adv7180_driver);
727 MODULE_DESCRIPTION("Analog Devices ADV7180 video decoder driver");
728 MODULE_AUTHOR("Mocean Laboratories");
729 MODULE_LICENSE("GPL v2");