2 * adv7180.c Analog Devices ADV7180 video decoder driver
3 * Copyright (c) 2009 Intel Corporation
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/errno.h>
22 #include <linux/kernel.h>
23 #include <linux/interrupt.h>
24 #include <linux/i2c.h>
25 #include <linux/slab.h>
26 #include <media/v4l2-ioctl.h>
27 #include <linux/videodev2.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-ctrls.h>
30 #include <media/v4l2-chip-ident.h>
31 #include <linux/mutex.h>
33 #define ADV7180_INPUT_CONTROL_REG 0x00
34 #define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM 0x00
35 #define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM_PED 0x10
36 #define ADV7180_INPUT_CONTROL_AD_PAL_N_NTSC_J_SECAM 0x20
37 #define ADV7180_INPUT_CONTROL_AD_PAL_N_NTSC_M_SECAM 0x30
38 #define ADV7180_INPUT_CONTROL_NTSC_J 0x40
39 #define ADV7180_INPUT_CONTROL_NTSC_M 0x50
40 #define ADV7180_INPUT_CONTROL_PAL60 0x60
41 #define ADV7180_INPUT_CONTROL_NTSC_443 0x70
42 #define ADV7180_INPUT_CONTROL_PAL_BG 0x80
43 #define ADV7180_INPUT_CONTROL_PAL_N 0x90
44 #define ADV7180_INPUT_CONTROL_PAL_M 0xa0
45 #define ADV7180_INPUT_CONTROL_PAL_M_PED 0xb0
46 #define ADV7180_INPUT_CONTROL_PAL_COMB_N 0xc0
47 #define ADV7180_INPUT_CONTROL_PAL_COMB_N_PED 0xd0
48 #define ADV7180_INPUT_CONTROL_PAL_SECAM 0xe0
49 #define ADV7180_INPUT_CONTROL_PAL_SECAM_PED 0xf0
50 #define ADV7180_INPUT_CONTROL_INSEL_MASK 0x0f
52 #define ADV7180_EXTENDED_OUTPUT_CONTROL_REG 0x04
53 #define ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS 0xC5
55 #define ADV7180_AUTODETECT_ENABLE_REG 0x07
56 #define ADV7180_AUTODETECT_DEFAULT 0x7f
58 #define ADV7180_CON_REG 0x08 /*Unsigned */
59 #define ADV7180_CON_MIN 0
60 #define ADV7180_CON_DEF 128
61 #define ADV7180_CON_MAX 255
63 #define ADV7180_BRI_REG 0x0a /*Signed */
64 #define ADV7180_BRI_MIN -128
65 #define ADV7180_BRI_DEF 0
66 #define ADV7180_BRI_MAX 127
68 #define ADV7180_HUE_REG 0x0b /*Signed, inverted */
69 #define ADV7180_HUE_MIN -127
70 #define ADV7180_HUE_DEF 0
71 #define ADV7180_HUE_MAX 128
73 #define ADV7180_ADI_CTRL_REG 0x0e
74 #define ADV7180_ADI_CTRL_IRQ_SPACE 0x20
76 #define ADV7180_PWR_MAN_REG 0x0f
77 #define ADV7180_PWR_MAN_ON 0x04
78 #define ADV7180_PWR_MAN_OFF 0x24
79 #define ADV7180_PWR_MAN_RES 0x80
81 #define ADV7180_STATUS1_REG 0x10
82 #define ADV7180_STATUS1_IN_LOCK 0x01
83 #define ADV7180_STATUS1_AUTOD_MASK 0x70
84 #define ADV7180_STATUS1_AUTOD_NTSM_M_J 0x00
85 #define ADV7180_STATUS1_AUTOD_NTSC_4_43 0x10
86 #define ADV7180_STATUS1_AUTOD_PAL_M 0x20
87 #define ADV7180_STATUS1_AUTOD_PAL_60 0x30
88 #define ADV7180_STATUS1_AUTOD_PAL_B_G 0x40
89 #define ADV7180_STATUS1_AUTOD_SECAM 0x50
90 #define ADV7180_STATUS1_AUTOD_PAL_COMB 0x60
91 #define ADV7180_STATUS1_AUTOD_SECAM_525 0x70
93 #define ADV7180_IDENT_REG 0x11
94 #define ADV7180_ID_7180 0x18
96 #define ADV7180_ICONF1_ADI 0x40
97 #define ADV7180_ICONF1_ACTIVE_LOW 0x01
98 #define ADV7180_ICONF1_PSYNC_ONLY 0x10
99 #define ADV7180_ICONF1_ACTIVE_TO_CLR 0xC0
101 #define ADV7180_SD_SAT_CB_REG 0xe3 /*Unsigned */
102 #define ADV7180_SD_SAT_CR_REG 0xe4 /*Unsigned */
103 #define ADV7180_SAT_MIN 0
104 #define ADV7180_SAT_DEF 128
105 #define ADV7180_SAT_MAX 255
107 #define ADV7180_IRQ1_LOCK 0x01
108 #define ADV7180_IRQ1_UNLOCK 0x02
109 #define ADV7180_ISR1_ADI 0x42
110 #define ADV7180_ICR1_ADI 0x43
111 #define ADV7180_IMR1_ADI 0x44
112 #define ADV7180_IMR2_ADI 0x48
113 #define ADV7180_IRQ3_AD_CHANGE 0x08
114 #define ADV7180_ISR3_ADI 0x4A
115 #define ADV7180_ICR3_ADI 0x4B
116 #define ADV7180_IMR3_ADI 0x4C
117 #define ADV7180_IMR4_ADI 0x50
119 #define ADV7180_NTSC_V_BIT_END_REG 0xE6
120 #define ADV7180_NTSC_V_BIT_END_MANUAL_NVEND 0x4F
122 struct adv7180_state
{
123 struct v4l2_ctrl_handler ctrl_hdl
;
124 struct v4l2_subdev sd
;
125 struct work_struct work
;
126 struct mutex mutex
; /* mutual excl. when accessing chip */
128 v4l2_std_id curr_norm
;
132 #define to_adv7180_sd(_ctrl) (&container_of(_ctrl->handler, \
133 struct adv7180_state, \
136 static v4l2_std_id
adv7180_std_to_v4l2(u8 status1
)
138 /* in case V4L2_IN_ST_NO_SIGNAL */
139 if (!(status1
& ADV7180_STATUS1_IN_LOCK
))
140 return V4L2_STD_UNKNOWN
;
142 switch (status1
& ADV7180_STATUS1_AUTOD_MASK
) {
143 case ADV7180_STATUS1_AUTOD_NTSM_M_J
:
144 return V4L2_STD_NTSC
;
145 case ADV7180_STATUS1_AUTOD_NTSC_4_43
:
146 return V4L2_STD_NTSC_443
;
147 case ADV7180_STATUS1_AUTOD_PAL_M
:
148 return V4L2_STD_PAL_M
;
149 case ADV7180_STATUS1_AUTOD_PAL_60
:
150 return V4L2_STD_PAL_60
;
151 case ADV7180_STATUS1_AUTOD_PAL_B_G
:
153 case ADV7180_STATUS1_AUTOD_SECAM
:
154 return V4L2_STD_SECAM
;
155 case ADV7180_STATUS1_AUTOD_PAL_COMB
:
156 return V4L2_STD_PAL_Nc
| V4L2_STD_PAL_N
;
157 case ADV7180_STATUS1_AUTOD_SECAM_525
:
158 return V4L2_STD_SECAM
;
160 return V4L2_STD_UNKNOWN
;
164 static int v4l2_std_to_adv7180(v4l2_std_id std
)
166 if (std
== V4L2_STD_PAL_60
)
167 return ADV7180_INPUT_CONTROL_PAL60
;
168 if (std
== V4L2_STD_NTSC_443
)
169 return ADV7180_INPUT_CONTROL_NTSC_443
;
170 if (std
== V4L2_STD_PAL_N
)
171 return ADV7180_INPUT_CONTROL_PAL_N
;
172 if (std
== V4L2_STD_PAL_M
)
173 return ADV7180_INPUT_CONTROL_PAL_M
;
174 if (std
== V4L2_STD_PAL_Nc
)
175 return ADV7180_INPUT_CONTROL_PAL_COMB_N
;
177 if (std
& V4L2_STD_PAL
)
178 return ADV7180_INPUT_CONTROL_PAL_BG
;
179 if (std
& V4L2_STD_NTSC
)
180 return ADV7180_INPUT_CONTROL_NTSC_M
;
181 if (std
& V4L2_STD_SECAM
)
182 return ADV7180_INPUT_CONTROL_PAL_SECAM
;
187 static u32
adv7180_status_to_v4l2(u8 status1
)
189 if (!(status1
& ADV7180_STATUS1_IN_LOCK
))
190 return V4L2_IN_ST_NO_SIGNAL
;
195 static int __adv7180_status(struct i2c_client
*client
, u32
*status
,
198 int status1
= i2c_smbus_read_byte_data(client
, ADV7180_STATUS1_REG
);
204 *status
= adv7180_status_to_v4l2(status1
);
206 *std
= adv7180_std_to_v4l2(status1
);
211 static inline struct adv7180_state
*to_state(struct v4l2_subdev
*sd
)
213 return container_of(sd
, struct adv7180_state
, sd
);
216 static int adv7180_querystd(struct v4l2_subdev
*sd
, v4l2_std_id
*std
)
218 struct adv7180_state
*state
= to_state(sd
);
219 int err
= mutex_lock_interruptible(&state
->mutex
);
223 /* when we are interrupt driven we know the state */
224 if (!state
->autodetect
|| state
->irq
> 0)
225 *std
= state
->curr_norm
;
227 err
= __adv7180_status(v4l2_get_subdevdata(sd
), NULL
, std
);
229 mutex_unlock(&state
->mutex
);
233 static int adv7180_s_routing(struct v4l2_subdev
*sd
, u32 input
,
234 u32 output
, u32 config
)
236 struct adv7180_state
*state
= to_state(sd
);
237 int ret
= mutex_lock_interruptible(&state
->mutex
);
238 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
243 /* We cannot discriminate between LQFP and 40-pin LFCSP, so accept
244 * all inputs and let the card driver take care of validation
246 if ((input
& ADV7180_INPUT_CONTROL_INSEL_MASK
) != input
)
249 ret
= i2c_smbus_read_byte_data(client
, ADV7180_INPUT_CONTROL_REG
);
254 ret
&= ~ADV7180_INPUT_CONTROL_INSEL_MASK
;
255 ret
= i2c_smbus_write_byte_data(client
,
256 ADV7180_INPUT_CONTROL_REG
, ret
| input
);
257 state
->input
= input
;
259 mutex_unlock(&state
->mutex
);
263 static int adv7180_g_input_status(struct v4l2_subdev
*sd
, u32
*status
)
265 struct adv7180_state
*state
= to_state(sd
);
266 int ret
= mutex_lock_interruptible(&state
->mutex
);
270 ret
= __adv7180_status(v4l2_get_subdevdata(sd
), status
, NULL
);
271 mutex_unlock(&state
->mutex
);
275 static int adv7180_g_chip_ident(struct v4l2_subdev
*sd
,
276 struct v4l2_dbg_chip_ident
*chip
)
278 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
280 return v4l2_chip_ident_i2c_client(client
, chip
, V4L2_IDENT_ADV7180
, 0);
283 static int adv7180_s_std(struct v4l2_subdev
*sd
, v4l2_std_id std
)
285 struct adv7180_state
*state
= to_state(sd
);
286 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
287 int ret
= mutex_lock_interruptible(&state
->mutex
);
291 /* all standards -> autodetect */
292 if (std
== V4L2_STD_ALL
) {
294 i2c_smbus_write_byte_data(client
, ADV7180_INPUT_CONTROL_REG
,
295 ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM
300 __adv7180_status(client
, NULL
, &state
->curr_norm
);
301 state
->autodetect
= true;
303 ret
= v4l2_std_to_adv7180(std
);
307 ret
= i2c_smbus_write_byte_data(client
,
308 ADV7180_INPUT_CONTROL_REG
,
313 state
->curr_norm
= std
;
314 state
->autodetect
= false;
318 mutex_unlock(&state
->mutex
);
322 static int adv7180_s_ctrl(struct v4l2_ctrl
*ctrl
)
324 struct v4l2_subdev
*sd
= to_adv7180_sd(ctrl
);
325 struct adv7180_state
*state
= to_state(sd
);
326 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
327 int ret
= mutex_lock_interruptible(&state
->mutex
);
334 case V4L2_CID_BRIGHTNESS
:
335 ret
= i2c_smbus_write_byte_data(client
, ADV7180_BRI_REG
, val
);
338 /*Hue is inverted according to HSL chart */
339 ret
= i2c_smbus_write_byte_data(client
, ADV7180_HUE_REG
, -val
);
341 case V4L2_CID_CONTRAST
:
342 ret
= i2c_smbus_write_byte_data(client
, ADV7180_CON_REG
, val
);
344 case V4L2_CID_SATURATION
:
346 *This could be V4L2_CID_BLUE_BALANCE/V4L2_CID_RED_BALANCE
347 *Let's not confuse the user, everybody understands saturation
349 ret
= i2c_smbus_write_byte_data(client
, ADV7180_SD_SAT_CB_REG
,
353 ret
= i2c_smbus_write_byte_data(client
, ADV7180_SD_SAT_CR_REG
,
360 mutex_unlock(&state
->mutex
);
364 static const struct v4l2_ctrl_ops adv7180_ctrl_ops
= {
365 .s_ctrl
= adv7180_s_ctrl
,
368 static int adv7180_init_controls(struct adv7180_state
*state
)
370 v4l2_ctrl_handler_init(&state
->ctrl_hdl
, 4);
372 v4l2_ctrl_new_std(&state
->ctrl_hdl
, &adv7180_ctrl_ops
,
373 V4L2_CID_BRIGHTNESS
, ADV7180_BRI_MIN
,
374 ADV7180_BRI_MAX
, 1, ADV7180_BRI_DEF
);
375 v4l2_ctrl_new_std(&state
->ctrl_hdl
, &adv7180_ctrl_ops
,
376 V4L2_CID_CONTRAST
, ADV7180_CON_MIN
,
377 ADV7180_CON_MAX
, 1, ADV7180_CON_DEF
);
378 v4l2_ctrl_new_std(&state
->ctrl_hdl
, &adv7180_ctrl_ops
,
379 V4L2_CID_SATURATION
, ADV7180_SAT_MIN
,
380 ADV7180_SAT_MAX
, 1, ADV7180_SAT_DEF
);
381 v4l2_ctrl_new_std(&state
->ctrl_hdl
, &adv7180_ctrl_ops
,
382 V4L2_CID_HUE
, ADV7180_HUE_MIN
,
383 ADV7180_HUE_MAX
, 1, ADV7180_HUE_DEF
);
384 state
->sd
.ctrl_handler
= &state
->ctrl_hdl
;
385 if (state
->ctrl_hdl
.error
) {
386 int err
= state
->ctrl_hdl
.error
;
388 v4l2_ctrl_handler_free(&state
->ctrl_hdl
);
391 v4l2_ctrl_handler_setup(&state
->ctrl_hdl
);
395 static void adv7180_exit_controls(struct adv7180_state
*state
)
397 v4l2_ctrl_handler_free(&state
->ctrl_hdl
);
400 static const struct v4l2_subdev_video_ops adv7180_video_ops
= {
401 .querystd
= adv7180_querystd
,
402 .g_input_status
= adv7180_g_input_status
,
403 .s_routing
= adv7180_s_routing
,
406 static const struct v4l2_subdev_core_ops adv7180_core_ops
= {
407 .g_chip_ident
= adv7180_g_chip_ident
,
408 .s_std
= adv7180_s_std
,
411 static const struct v4l2_subdev_ops adv7180_ops
= {
412 .core
= &adv7180_core_ops
,
413 .video
= &adv7180_video_ops
,
416 static void adv7180_work(struct work_struct
*work
)
418 struct adv7180_state
*state
= container_of(work
, struct adv7180_state
,
420 struct i2c_client
*client
= v4l2_get_subdevdata(&state
->sd
);
423 mutex_lock(&state
->mutex
);
424 i2c_smbus_write_byte_data(client
, ADV7180_ADI_CTRL_REG
,
425 ADV7180_ADI_CTRL_IRQ_SPACE
);
426 isr3
= i2c_smbus_read_byte_data(client
, ADV7180_ISR3_ADI
);
428 i2c_smbus_write_byte_data(client
, ADV7180_ICR3_ADI
, isr3
);
429 i2c_smbus_write_byte_data(client
, ADV7180_ADI_CTRL_REG
, 0);
431 if (isr3
& ADV7180_IRQ3_AD_CHANGE
&& state
->autodetect
)
432 __adv7180_status(client
, NULL
, &state
->curr_norm
);
433 mutex_unlock(&state
->mutex
);
435 enable_irq(state
->irq
);
438 static irqreturn_t
adv7180_irq(int irq
, void *devid
)
440 struct adv7180_state
*state
= devid
;
442 schedule_work(&state
->work
);
444 disable_irq_nosync(state
->irq
);
449 static int init_device(struct i2c_client
*client
, struct adv7180_state
*state
)
453 /* Initialize adv7180 */
454 /* Enable autodetection */
455 if (state
->autodetect
) {
457 i2c_smbus_write_byte_data(client
, ADV7180_INPUT_CONTROL_REG
,
458 ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM
464 i2c_smbus_write_byte_data(client
,
465 ADV7180_AUTODETECT_ENABLE_REG
,
466 ADV7180_AUTODETECT_DEFAULT
);
470 ret
= v4l2_std_to_adv7180(state
->curr_norm
);
475 i2c_smbus_write_byte_data(client
, ADV7180_INPUT_CONTROL_REG
,
481 /* ITU-R BT.656-4 compatible */
482 ret
= i2c_smbus_write_byte_data(client
,
483 ADV7180_EXTENDED_OUTPUT_CONTROL_REG
,
484 ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS
);
488 /* Manually set V bit end position in NTSC mode */
489 ret
= i2c_smbus_write_byte_data(client
,
490 ADV7180_NTSC_V_BIT_END_REG
,
491 ADV7180_NTSC_V_BIT_END_MANUAL_NVEND
);
495 /* read current norm */
496 __adv7180_status(client
, NULL
, &state
->curr_norm
);
498 /* register for interrupts */
499 if (state
->irq
> 0) {
500 ret
= request_irq(state
->irq
, adv7180_irq
, 0, KBUILD_MODNAME
,
505 ret
= i2c_smbus_write_byte_data(client
, ADV7180_ADI_CTRL_REG
,
506 ADV7180_ADI_CTRL_IRQ_SPACE
);
510 /* config the Interrupt pin to be active low */
511 ret
= i2c_smbus_write_byte_data(client
, ADV7180_ICONF1_ADI
,
512 ADV7180_ICONF1_ACTIVE_LOW
|
513 ADV7180_ICONF1_PSYNC_ONLY
);
517 ret
= i2c_smbus_write_byte_data(client
, ADV7180_IMR1_ADI
, 0);
521 ret
= i2c_smbus_write_byte_data(client
, ADV7180_IMR2_ADI
, 0);
525 /* enable AD change interrupts interrupts */
526 ret
= i2c_smbus_write_byte_data(client
, ADV7180_IMR3_ADI
,
527 ADV7180_IRQ3_AD_CHANGE
);
531 ret
= i2c_smbus_write_byte_data(client
, ADV7180_IMR4_ADI
, 0);
535 ret
= i2c_smbus_write_byte_data(client
, ADV7180_ADI_CTRL_REG
,
544 static int adv7180_probe(struct i2c_client
*client
,
545 const struct i2c_device_id
*id
)
547 struct adv7180_state
*state
;
548 struct v4l2_subdev
*sd
;
551 /* Check if the adapter supports the needed features */
552 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
555 v4l_info(client
, "chip found @ 0x%02x (%s)\n",
556 client
->addr
, client
->adapter
->name
);
558 state
= kzalloc(sizeof(struct adv7180_state
), GFP_KERNEL
);
564 state
->irq
= client
->irq
;
565 INIT_WORK(&state
->work
, adv7180_work
);
566 mutex_init(&state
->mutex
);
567 state
->autodetect
= true;
570 v4l2_i2c_subdev_init(sd
, client
, &adv7180_ops
);
572 ret
= adv7180_init_controls(state
);
574 goto err_unreg_subdev
;
575 ret
= init_device(client
, state
);
581 adv7180_exit_controls(state
);
583 mutex_destroy(&state
->mutex
);
584 v4l2_device_unregister_subdev(sd
);
587 printk(KERN_ERR KBUILD_MODNAME
": Failed to probe: %d\n", ret
);
591 static int adv7180_remove(struct i2c_client
*client
)
593 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
594 struct adv7180_state
*state
= to_state(sd
);
596 if (state
->irq
> 0) {
597 free_irq(client
->irq
, state
);
598 if (cancel_work_sync(&state
->work
)) {
600 * Work was pending, therefore we need to enable
601 * IRQ here to balance the disable_irq() done in the
604 enable_irq(state
->irq
);
608 mutex_destroy(&state
->mutex
);
609 v4l2_device_unregister_subdev(sd
);
614 static const struct i2c_device_id adv7180_id
[] = {
620 static int adv7180_suspend(struct i2c_client
*client
, pm_message_t state
)
624 ret
= i2c_smbus_write_byte_data(client
, ADV7180_PWR_MAN_REG
,
625 ADV7180_PWR_MAN_OFF
);
631 static int adv7180_resume(struct i2c_client
*client
)
633 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
634 struct adv7180_state
*state
= to_state(sd
);
637 ret
= i2c_smbus_write_byte_data(client
, ADV7180_PWR_MAN_REG
,
641 ret
= init_device(client
, state
);
648 MODULE_DEVICE_TABLE(i2c
, adv7180_id
);
650 static struct i2c_driver adv7180_driver
= {
652 .owner
= THIS_MODULE
,
653 .name
= KBUILD_MODNAME
,
655 .probe
= adv7180_probe
,
656 .remove
= adv7180_remove
,
658 .suspend
= adv7180_suspend
,
659 .resume
= adv7180_resume
,
661 .id_table
= adv7180_id
,
664 module_i2c_driver(adv7180_driver
);
666 MODULE_DESCRIPTION("Analog Devices ADV7180 video decoder driver");
667 MODULE_AUTHOR("Mocean Laboratories");
668 MODULE_LICENSE("GPL v2");