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/i2c-id.h>
26 #include <linux/slab.h>
27 #include <media/v4l2-ioctl.h>
28 #include <linux/videodev2.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-chip-ident.h>
31 #include <linux/mutex.h>
33 #define DRIVER_NAME "adv7180"
35 #define ADV7180_INPUT_CONTROL_REG 0x00
36 #define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM 0x00
37 #define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM_PED 0x10
38 #define ADV7180_INPUT_CONTROL_AD_PAL_N_NTSC_J_SECAM 0x20
39 #define ADV7180_INPUT_CONTROL_AD_PAL_N_NTSC_M_SECAM 0x30
40 #define ADV7180_INPUT_CONTROL_NTSC_J 0x40
41 #define ADV7180_INPUT_CONTROL_NTSC_M 0x50
42 #define ADV7180_INPUT_CONTROL_PAL60 0x60
43 #define ADV7180_INPUT_CONTROL_NTSC_443 0x70
44 #define ADV7180_INPUT_CONTROL_PAL_BG 0x80
45 #define ADV7180_INPUT_CONTROL_PAL_N 0x90
46 #define ADV7180_INPUT_CONTROL_PAL_M 0xa0
47 #define ADV7180_INPUT_CONTROL_PAL_M_PED 0xb0
48 #define ADV7180_INPUT_CONTROL_PAL_COMB_N 0xc0
49 #define ADV7180_INPUT_CONTROL_PAL_COMB_N_PED 0xd0
50 #define ADV7180_INPUT_CONTROL_PAL_SECAM 0xe0
51 #define ADV7180_INPUT_CONTROL_PAL_SECAM_PED 0xf0
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_ADI_CTRL_REG 0x0e
60 #define ADV7180_ADI_CTRL_IRQ_SPACE 0x20
62 #define ADV7180_STATUS1_REG 0x10
63 #define ADV7180_STATUS1_IN_LOCK 0x01
64 #define ADV7180_STATUS1_AUTOD_MASK 0x70
65 #define ADV7180_STATUS1_AUTOD_NTSM_M_J 0x00
66 #define ADV7180_STATUS1_AUTOD_NTSC_4_43 0x10
67 #define ADV7180_STATUS1_AUTOD_PAL_M 0x20
68 #define ADV7180_STATUS1_AUTOD_PAL_60 0x30
69 #define ADV7180_STATUS1_AUTOD_PAL_B_G 0x40
70 #define ADV7180_STATUS1_AUTOD_SECAM 0x50
71 #define ADV7180_STATUS1_AUTOD_PAL_COMB 0x60
72 #define ADV7180_STATUS1_AUTOD_SECAM_525 0x70
74 #define ADV7180_IDENT_REG 0x11
75 #define ADV7180_ID_7180 0x18
77 #define ADV7180_ICONF1_ADI 0x40
78 #define ADV7180_ICONF1_ACTIVE_LOW 0x01
79 #define ADV7180_ICONF1_PSYNC_ONLY 0x10
80 #define ADV7180_ICONF1_ACTIVE_TO_CLR 0xC0
82 #define ADV7180_IRQ1_LOCK 0x01
83 #define ADV7180_IRQ1_UNLOCK 0x02
84 #define ADV7180_ISR1_ADI 0x42
85 #define ADV7180_ICR1_ADI 0x43
86 #define ADV7180_IMR1_ADI 0x44
87 #define ADV7180_IMR2_ADI 0x48
88 #define ADV7180_IRQ3_AD_CHANGE 0x08
89 #define ADV7180_ISR3_ADI 0x4A
90 #define ADV7180_ICR3_ADI 0x4B
91 #define ADV7180_IMR3_ADI 0x4C
92 #define ADV7180_IMR4_ADI 0x50
94 struct adv7180_state
{
95 struct v4l2_subdev sd
;
96 struct work_struct work
;
97 struct mutex mutex
; /* mutual excl. when accessing chip */
99 v4l2_std_id curr_norm
;
103 static v4l2_std_id
adv7180_std_to_v4l2(u8 status1
)
105 switch (status1
& ADV7180_STATUS1_AUTOD_MASK
) {
106 case ADV7180_STATUS1_AUTOD_NTSM_M_J
:
107 return V4L2_STD_NTSC
;
108 case ADV7180_STATUS1_AUTOD_NTSC_4_43
:
109 return V4L2_STD_NTSC_443
;
110 case ADV7180_STATUS1_AUTOD_PAL_M
:
111 return V4L2_STD_PAL_M
;
112 case ADV7180_STATUS1_AUTOD_PAL_60
:
113 return V4L2_STD_PAL_60
;
114 case ADV7180_STATUS1_AUTOD_PAL_B_G
:
116 case ADV7180_STATUS1_AUTOD_SECAM
:
117 return V4L2_STD_SECAM
;
118 case ADV7180_STATUS1_AUTOD_PAL_COMB
:
119 return V4L2_STD_PAL_Nc
| V4L2_STD_PAL_N
;
120 case ADV7180_STATUS1_AUTOD_SECAM_525
:
121 return V4L2_STD_SECAM
;
123 return V4L2_STD_UNKNOWN
;
127 static int v4l2_std_to_adv7180(v4l2_std_id std
)
129 if (std
== V4L2_STD_PAL_60
)
130 return ADV7180_INPUT_CONTROL_PAL60
;
131 if (std
== V4L2_STD_NTSC_443
)
132 return ADV7180_INPUT_CONTROL_NTSC_443
;
133 if (std
== V4L2_STD_PAL_N
)
134 return ADV7180_INPUT_CONTROL_PAL_N
;
135 if (std
== V4L2_STD_PAL_M
)
136 return ADV7180_INPUT_CONTROL_PAL_M
;
137 if (std
== V4L2_STD_PAL_Nc
)
138 return ADV7180_INPUT_CONTROL_PAL_COMB_N
;
140 if (std
& V4L2_STD_PAL
)
141 return ADV7180_INPUT_CONTROL_PAL_BG
;
142 if (std
& V4L2_STD_NTSC
)
143 return ADV7180_INPUT_CONTROL_NTSC_M
;
144 if (std
& V4L2_STD_SECAM
)
145 return ADV7180_INPUT_CONTROL_PAL_SECAM
;
150 static u32
adv7180_status_to_v4l2(u8 status1
)
152 if (!(status1
& ADV7180_STATUS1_IN_LOCK
))
153 return V4L2_IN_ST_NO_SIGNAL
;
158 static int __adv7180_status(struct i2c_client
*client
, u32
*status
,
161 int status1
= i2c_smbus_read_byte_data(client
, ADV7180_STATUS1_REG
);
167 *status
= adv7180_status_to_v4l2(status1
);
169 *std
= adv7180_std_to_v4l2(status1
);
174 static inline struct adv7180_state
*to_state(struct v4l2_subdev
*sd
)
176 return container_of(sd
, struct adv7180_state
, sd
);
179 static int adv7180_querystd(struct v4l2_subdev
*sd
, v4l2_std_id
*std
)
181 struct adv7180_state
*state
= to_state(sd
);
182 int err
= mutex_lock_interruptible(&state
->mutex
);
186 /* when we are interrupt driven we know the state */
187 if (!state
->autodetect
|| state
->irq
> 0)
188 *std
= state
->curr_norm
;
190 err
= __adv7180_status(v4l2_get_subdevdata(sd
), NULL
, std
);
192 mutex_unlock(&state
->mutex
);
196 static int adv7180_g_input_status(struct v4l2_subdev
*sd
, u32
*status
)
198 struct adv7180_state
*state
= to_state(sd
);
199 int ret
= mutex_lock_interruptible(&state
->mutex
);
203 ret
= __adv7180_status(v4l2_get_subdevdata(sd
), status
, NULL
);
204 mutex_unlock(&state
->mutex
);
208 static int adv7180_g_chip_ident(struct v4l2_subdev
*sd
,
209 struct v4l2_dbg_chip_ident
*chip
)
211 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
213 return v4l2_chip_ident_i2c_client(client
, chip
, V4L2_IDENT_ADV7180
, 0);
216 static int adv7180_s_std(struct v4l2_subdev
*sd
, v4l2_std_id std
)
218 struct adv7180_state
*state
= to_state(sd
);
219 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
220 int ret
= mutex_lock_interruptible(&state
->mutex
);
224 /* all standards -> autodetect */
225 if (std
== V4L2_STD_ALL
) {
226 ret
= i2c_smbus_write_byte_data(client
,
227 ADV7180_INPUT_CONTROL_REG
,
228 ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM
);
232 __adv7180_status(client
, NULL
, &state
->curr_norm
);
233 state
->autodetect
= true;
235 ret
= v4l2_std_to_adv7180(std
);
239 ret
= i2c_smbus_write_byte_data(client
,
240 ADV7180_INPUT_CONTROL_REG
, ret
);
244 state
->curr_norm
= std
;
245 state
->autodetect
= false;
249 mutex_unlock(&state
->mutex
);
253 static const struct v4l2_subdev_video_ops adv7180_video_ops
= {
254 .querystd
= adv7180_querystd
,
255 .g_input_status
= adv7180_g_input_status
,
258 static const struct v4l2_subdev_core_ops adv7180_core_ops
= {
259 .g_chip_ident
= adv7180_g_chip_ident
,
260 .s_std
= adv7180_s_std
,
263 static const struct v4l2_subdev_ops adv7180_ops
= {
264 .core
= &adv7180_core_ops
,
265 .video
= &adv7180_video_ops
,
268 static void adv7180_work(struct work_struct
*work
)
270 struct adv7180_state
*state
= container_of(work
, struct adv7180_state
,
272 struct i2c_client
*client
= v4l2_get_subdevdata(&state
->sd
);
275 mutex_lock(&state
->mutex
);
276 i2c_smbus_write_byte_data(client
, ADV7180_ADI_CTRL_REG
,
277 ADV7180_ADI_CTRL_IRQ_SPACE
);
278 isr3
= i2c_smbus_read_byte_data(client
, ADV7180_ISR3_ADI
);
280 i2c_smbus_write_byte_data(client
, ADV7180_ICR3_ADI
, isr3
);
281 i2c_smbus_write_byte_data(client
, ADV7180_ADI_CTRL_REG
, 0);
283 if (isr3
& ADV7180_IRQ3_AD_CHANGE
&& state
->autodetect
)
284 __adv7180_status(client
, NULL
, &state
->curr_norm
);
285 mutex_unlock(&state
->mutex
);
287 enable_irq(state
->irq
);
290 static irqreturn_t
adv7180_irq(int irq
, void *devid
)
292 struct adv7180_state
*state
= devid
;
294 schedule_work(&state
->work
);
296 disable_irq_nosync(state
->irq
);
303 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
306 static __devinit
int adv7180_probe(struct i2c_client
*client
,
307 const struct i2c_device_id
*id
)
309 struct adv7180_state
*state
;
310 struct v4l2_subdev
*sd
;
313 /* Check if the adapter supports the needed features */
314 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
317 v4l_info(client
, "chip found @ 0x%02x (%s)\n",
318 client
->addr
<< 1, client
->adapter
->name
);
320 state
= kzalloc(sizeof(struct adv7180_state
), GFP_KERNEL
);
326 state
->irq
= client
->irq
;
327 INIT_WORK(&state
->work
, adv7180_work
);
328 mutex_init(&state
->mutex
);
329 state
->autodetect
= true;
331 v4l2_i2c_subdev_init(sd
, client
, &adv7180_ops
);
333 /* Initialize adv7180 */
334 /* Enable autodetection */
335 ret
= i2c_smbus_write_byte_data(client
, ADV7180_INPUT_CONTROL_REG
,
336 ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM
);
338 goto err_unreg_subdev
;
340 ret
= i2c_smbus_write_byte_data(client
, ADV7180_AUTODETECT_ENABLE_REG
,
341 ADV7180_AUTODETECT_DEFAULT
);
343 goto err_unreg_subdev
;
345 /* ITU-R BT.656-4 compatible */
346 ret
= i2c_smbus_write_byte_data(client
,
347 ADV7180_EXTENDED_OUTPUT_CONTROL_REG
,
348 ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS
);
350 goto err_unreg_subdev
;
352 /* read current norm */
353 __adv7180_status(client
, NULL
, &state
->curr_norm
);
355 /* register for interrupts */
356 if (state
->irq
> 0) {
357 ret
= request_irq(state
->irq
, adv7180_irq
, 0, DRIVER_NAME
,
360 goto err_unreg_subdev
;
362 ret
= i2c_smbus_write_byte_data(client
, ADV7180_ADI_CTRL_REG
,
363 ADV7180_ADI_CTRL_IRQ_SPACE
);
365 goto err_unreg_subdev
;
367 /* config the Interrupt pin to be active low */
368 ret
= i2c_smbus_write_byte_data(client
, ADV7180_ICONF1_ADI
,
369 ADV7180_ICONF1_ACTIVE_LOW
| ADV7180_ICONF1_PSYNC_ONLY
);
371 goto err_unreg_subdev
;
373 ret
= i2c_smbus_write_byte_data(client
, ADV7180_IMR1_ADI
, 0);
375 goto err_unreg_subdev
;
377 ret
= i2c_smbus_write_byte_data(client
, ADV7180_IMR2_ADI
, 0);
379 goto err_unreg_subdev
;
381 /* enable AD change interrupts interrupts */
382 ret
= i2c_smbus_write_byte_data(client
, ADV7180_IMR3_ADI
,
383 ADV7180_IRQ3_AD_CHANGE
);
385 goto err_unreg_subdev
;
387 ret
= i2c_smbus_write_byte_data(client
, ADV7180_IMR4_ADI
, 0);
389 goto err_unreg_subdev
;
391 ret
= i2c_smbus_write_byte_data(client
, ADV7180_ADI_CTRL_REG
,
394 goto err_unreg_subdev
;
400 mutex_destroy(&state
->mutex
);
401 v4l2_device_unregister_subdev(sd
);
404 printk(KERN_ERR DRIVER_NAME
": Failed to probe: %d\n", ret
);
408 static __devexit
int adv7180_remove(struct i2c_client
*client
)
410 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
411 struct adv7180_state
*state
= to_state(sd
);
413 if (state
->irq
> 0) {
414 free_irq(client
->irq
, state
);
415 if (cancel_work_sync(&state
->work
)) {
417 * Work was pending, therefore we need to enable
418 * IRQ here to balance the disable_irq() done in the
421 enable_irq(state
->irq
);
425 mutex_destroy(&state
->mutex
);
426 v4l2_device_unregister_subdev(sd
);
431 static const struct i2c_device_id adv7180_id
[] = {
436 MODULE_DEVICE_TABLE(i2c
, adv7180_id
);
438 static struct i2c_driver adv7180_driver
= {
440 .owner
= THIS_MODULE
,
443 .probe
= adv7180_probe
,
444 .remove
= __devexit_p(adv7180_remove
),
445 .id_table
= adv7180_id
,
448 static __init
int adv7180_init(void)
450 return i2c_add_driver(&adv7180_driver
);
453 static __exit
void adv7180_exit(void)
455 i2c_del_driver(&adv7180_driver
);
458 module_init(adv7180_init
);
459 module_exit(adv7180_exit
);
461 MODULE_DESCRIPTION("Analog Devices ADV7180 video decoder driver");
462 MODULE_AUTHOR("Mocean Laboratories");
463 MODULE_LICENSE("GPL v2");