Linux 2.6.34-rc3
[pohmelfs.git] / drivers / media / video / adv7180.c
blob0826f0dabc172a72ad2aaa70f931ac2b0b05463a
1 /*
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 <media/v4l2-ioctl.h>
27 #include <linux/videodev2.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-chip-ident.h>
30 #include <linux/mutex.h>
32 #define DRIVER_NAME "adv7180"
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
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_ADI_CTRL_REG 0x0e
59 #define ADV7180_ADI_CTRL_IRQ_SPACE 0x20
61 #define ADV7180_STATUS1_REG 0x10
62 #define ADV7180_STATUS1_IN_LOCK 0x01
63 #define ADV7180_STATUS1_AUTOD_MASK 0x70
64 #define ADV7180_STATUS1_AUTOD_NTSM_M_J 0x00
65 #define ADV7180_STATUS1_AUTOD_NTSC_4_43 0x10
66 #define ADV7180_STATUS1_AUTOD_PAL_M 0x20
67 #define ADV7180_STATUS1_AUTOD_PAL_60 0x30
68 #define ADV7180_STATUS1_AUTOD_PAL_B_G 0x40
69 #define ADV7180_STATUS1_AUTOD_SECAM 0x50
70 #define ADV7180_STATUS1_AUTOD_PAL_COMB 0x60
71 #define ADV7180_STATUS1_AUTOD_SECAM_525 0x70
73 #define ADV7180_IDENT_REG 0x11
74 #define ADV7180_ID_7180 0x18
76 #define ADV7180_ICONF1_ADI 0x40
77 #define ADV7180_ICONF1_ACTIVE_LOW 0x01
78 #define ADV7180_ICONF1_PSYNC_ONLY 0x10
79 #define ADV7180_ICONF1_ACTIVE_TO_CLR 0xC0
81 #define ADV7180_IRQ1_LOCK 0x01
82 #define ADV7180_IRQ1_UNLOCK 0x02
83 #define ADV7180_ISR1_ADI 0x42
84 #define ADV7180_ICR1_ADI 0x43
85 #define ADV7180_IMR1_ADI 0x44
86 #define ADV7180_IMR2_ADI 0x48
87 #define ADV7180_IRQ3_AD_CHANGE 0x08
88 #define ADV7180_ISR3_ADI 0x4A
89 #define ADV7180_ICR3_ADI 0x4B
90 #define ADV7180_IMR3_ADI 0x4C
91 #define ADV7180_IMR4_ADI 0x50
93 struct adv7180_state {
94 struct v4l2_subdev sd;
95 struct work_struct work;
96 struct mutex mutex; /* mutual excl. when accessing chip */
97 int irq;
98 v4l2_std_id curr_norm;
99 bool autodetect;
102 static v4l2_std_id adv7180_std_to_v4l2(u8 status1)
104 switch (status1 & ADV7180_STATUS1_AUTOD_MASK) {
105 case ADV7180_STATUS1_AUTOD_NTSM_M_J:
106 return V4L2_STD_NTSC;
107 case ADV7180_STATUS1_AUTOD_NTSC_4_43:
108 return V4L2_STD_NTSC_443;
109 case ADV7180_STATUS1_AUTOD_PAL_M:
110 return V4L2_STD_PAL_M;
111 case ADV7180_STATUS1_AUTOD_PAL_60:
112 return V4L2_STD_PAL_60;
113 case ADV7180_STATUS1_AUTOD_PAL_B_G:
114 return V4L2_STD_PAL;
115 case ADV7180_STATUS1_AUTOD_SECAM:
116 return V4L2_STD_SECAM;
117 case ADV7180_STATUS1_AUTOD_PAL_COMB:
118 return V4L2_STD_PAL_Nc | V4L2_STD_PAL_N;
119 case ADV7180_STATUS1_AUTOD_SECAM_525:
120 return V4L2_STD_SECAM;
121 default:
122 return V4L2_STD_UNKNOWN;
126 static int v4l2_std_to_adv7180(v4l2_std_id std)
128 if (std == V4L2_STD_PAL_60)
129 return ADV7180_INPUT_CONTROL_PAL60;
130 if (std == V4L2_STD_NTSC_443)
131 return ADV7180_INPUT_CONTROL_NTSC_443;
132 if (std == V4L2_STD_PAL_N)
133 return ADV7180_INPUT_CONTROL_PAL_N;
134 if (std == V4L2_STD_PAL_M)
135 return ADV7180_INPUT_CONTROL_PAL_M;
136 if (std == V4L2_STD_PAL_Nc)
137 return ADV7180_INPUT_CONTROL_PAL_COMB_N;
139 if (std & V4L2_STD_PAL)
140 return ADV7180_INPUT_CONTROL_PAL_BG;
141 if (std & V4L2_STD_NTSC)
142 return ADV7180_INPUT_CONTROL_NTSC_M;
143 if (std & V4L2_STD_SECAM)
144 return ADV7180_INPUT_CONTROL_PAL_SECAM;
146 return -EINVAL;
149 static u32 adv7180_status_to_v4l2(u8 status1)
151 if (!(status1 & ADV7180_STATUS1_IN_LOCK))
152 return V4L2_IN_ST_NO_SIGNAL;
154 return 0;
157 static int __adv7180_status(struct i2c_client *client, u32 *status,
158 v4l2_std_id *std)
160 int status1 = i2c_smbus_read_byte_data(client, ADV7180_STATUS1_REG);
162 if (status1 < 0)
163 return status1;
165 if (status)
166 *status = adv7180_status_to_v4l2(status1);
167 if (std)
168 *std = adv7180_std_to_v4l2(status1);
170 return 0;
173 static inline struct adv7180_state *to_state(struct v4l2_subdev *sd)
175 return container_of(sd, struct adv7180_state, sd);
178 static int adv7180_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
180 struct adv7180_state *state = to_state(sd);
181 int err = mutex_lock_interruptible(&state->mutex);
182 if (err)
183 return err;
185 /* when we are interrupt driven we know the state */
186 if (!state->autodetect || state->irq > 0)
187 *std = state->curr_norm;
188 else
189 err = __adv7180_status(v4l2_get_subdevdata(sd), NULL, std);
191 mutex_unlock(&state->mutex);
192 return err;
195 static int adv7180_g_input_status(struct v4l2_subdev *sd, u32 *status)
197 struct adv7180_state *state = to_state(sd);
198 int ret = mutex_lock_interruptible(&state->mutex);
199 if (ret)
200 return ret;
202 ret = __adv7180_status(v4l2_get_subdevdata(sd), status, NULL);
203 mutex_unlock(&state->mutex);
204 return ret;
207 static int adv7180_g_chip_ident(struct v4l2_subdev *sd,
208 struct v4l2_dbg_chip_ident *chip)
210 struct i2c_client *client = v4l2_get_subdevdata(sd);
212 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7180, 0);
215 static int adv7180_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
217 struct adv7180_state *state = to_state(sd);
218 struct i2c_client *client = v4l2_get_subdevdata(sd);
219 int ret = mutex_lock_interruptible(&state->mutex);
220 if (ret)
221 return ret;
223 /* all standards -> autodetect */
224 if (std == V4L2_STD_ALL) {
225 ret = i2c_smbus_write_byte_data(client,
226 ADV7180_INPUT_CONTROL_REG,
227 ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM);
228 if (ret < 0)
229 goto out;
231 __adv7180_status(client, NULL, &state->curr_norm);
232 state->autodetect = true;
233 } else {
234 ret = v4l2_std_to_adv7180(std);
235 if (ret < 0)
236 goto out;
238 ret = i2c_smbus_write_byte_data(client,
239 ADV7180_INPUT_CONTROL_REG, ret);
240 if (ret < 0)
241 goto out;
243 state->curr_norm = std;
244 state->autodetect = false;
246 ret = 0;
247 out:
248 mutex_unlock(&state->mutex);
249 return ret;
252 static const struct v4l2_subdev_video_ops adv7180_video_ops = {
253 .querystd = adv7180_querystd,
254 .g_input_status = adv7180_g_input_status,
257 static const struct v4l2_subdev_core_ops adv7180_core_ops = {
258 .g_chip_ident = adv7180_g_chip_ident,
259 .s_std = adv7180_s_std,
262 static const struct v4l2_subdev_ops adv7180_ops = {
263 .core = &adv7180_core_ops,
264 .video = &adv7180_video_ops,
267 static void adv7180_work(struct work_struct *work)
269 struct adv7180_state *state = container_of(work, struct adv7180_state,
270 work);
271 struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
272 u8 isr3;
274 mutex_lock(&state->mutex);
275 i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
276 ADV7180_ADI_CTRL_IRQ_SPACE);
277 isr3 = i2c_smbus_read_byte_data(client, ADV7180_ISR3_ADI);
278 /* clear */
279 i2c_smbus_write_byte_data(client, ADV7180_ICR3_ADI, isr3);
280 i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG, 0);
282 if (isr3 & ADV7180_IRQ3_AD_CHANGE && state->autodetect)
283 __adv7180_status(client, NULL, &state->curr_norm);
284 mutex_unlock(&state->mutex);
286 enable_irq(state->irq);
289 static irqreturn_t adv7180_irq(int irq, void *devid)
291 struct adv7180_state *state = devid;
293 schedule_work(&state->work);
295 disable_irq_nosync(state->irq);
297 return IRQ_HANDLED;
301 * Generic i2c probe
302 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
305 static __devinit int adv7180_probe(struct i2c_client *client,
306 const struct i2c_device_id *id)
308 struct adv7180_state *state;
309 struct v4l2_subdev *sd;
310 int ret;
312 /* Check if the adapter supports the needed features */
313 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
314 return -EIO;
316 v4l_info(client, "chip found @ 0x%02x (%s)\n",
317 client->addr << 1, client->adapter->name);
319 state = kzalloc(sizeof(struct adv7180_state), GFP_KERNEL);
320 if (state == NULL) {
321 ret = -ENOMEM;
322 goto err;
325 state->irq = client->irq;
326 INIT_WORK(&state->work, adv7180_work);
327 mutex_init(&state->mutex);
328 state->autodetect = true;
329 sd = &state->sd;
330 v4l2_i2c_subdev_init(sd, client, &adv7180_ops);
332 /* Initialize adv7180 */
333 /* Enable autodetection */
334 ret = i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG,
335 ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM);
336 if (ret < 0)
337 goto err_unreg_subdev;
339 ret = i2c_smbus_write_byte_data(client, ADV7180_AUTODETECT_ENABLE_REG,
340 ADV7180_AUTODETECT_DEFAULT);
341 if (ret < 0)
342 goto err_unreg_subdev;
344 /* ITU-R BT.656-4 compatible */
345 ret = i2c_smbus_write_byte_data(client,
346 ADV7180_EXTENDED_OUTPUT_CONTROL_REG,
347 ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS);
348 if (ret < 0)
349 goto err_unreg_subdev;
351 /* read current norm */
352 __adv7180_status(client, NULL, &state->curr_norm);
354 /* register for interrupts */
355 if (state->irq > 0) {
356 ret = request_irq(state->irq, adv7180_irq, 0, DRIVER_NAME,
357 state);
358 if (ret)
359 goto err_unreg_subdev;
361 ret = i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
362 ADV7180_ADI_CTRL_IRQ_SPACE);
363 if (ret < 0)
364 goto err_unreg_subdev;
366 /* config the Interrupt pin to be active low */
367 ret = i2c_smbus_write_byte_data(client, ADV7180_ICONF1_ADI,
368 ADV7180_ICONF1_ACTIVE_LOW | ADV7180_ICONF1_PSYNC_ONLY);
369 if (ret < 0)
370 goto err_unreg_subdev;
372 ret = i2c_smbus_write_byte_data(client, ADV7180_IMR1_ADI, 0);
373 if (ret < 0)
374 goto err_unreg_subdev;
376 ret = i2c_smbus_write_byte_data(client, ADV7180_IMR2_ADI, 0);
377 if (ret < 0)
378 goto err_unreg_subdev;
380 /* enable AD change interrupts interrupts */
381 ret = i2c_smbus_write_byte_data(client, ADV7180_IMR3_ADI,
382 ADV7180_IRQ3_AD_CHANGE);
383 if (ret < 0)
384 goto err_unreg_subdev;
386 ret = i2c_smbus_write_byte_data(client, ADV7180_IMR4_ADI, 0);
387 if (ret < 0)
388 goto err_unreg_subdev;
390 ret = i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
392 if (ret < 0)
393 goto err_unreg_subdev;
396 return 0;
398 err_unreg_subdev:
399 mutex_destroy(&state->mutex);
400 v4l2_device_unregister_subdev(sd);
401 kfree(state);
402 err:
403 printk(KERN_ERR DRIVER_NAME ": Failed to probe: %d\n", ret);
404 return ret;
407 static __devexit int adv7180_remove(struct i2c_client *client)
409 struct v4l2_subdev *sd = i2c_get_clientdata(client);
410 struct adv7180_state *state = to_state(sd);
412 if (state->irq > 0) {
413 free_irq(client->irq, state);
414 if (cancel_work_sync(&state->work)) {
416 * Work was pending, therefore we need to enable
417 * IRQ here to balance the disable_irq() done in the
418 * interrupt handler.
420 enable_irq(state->irq);
424 mutex_destroy(&state->mutex);
425 v4l2_device_unregister_subdev(sd);
426 kfree(to_state(sd));
427 return 0;
430 static const struct i2c_device_id adv7180_id[] = {
431 {DRIVER_NAME, 0},
435 MODULE_DEVICE_TABLE(i2c, adv7180_id);
437 static struct i2c_driver adv7180_driver = {
438 .driver = {
439 .owner = THIS_MODULE,
440 .name = DRIVER_NAME,
442 .probe = adv7180_probe,
443 .remove = __devexit_p(adv7180_remove),
444 .id_table = adv7180_id,
447 static __init int adv7180_init(void)
449 return i2c_add_driver(&adv7180_driver);
452 static __exit void adv7180_exit(void)
454 i2c_del_driver(&adv7180_driver);
457 module_init(adv7180_init);
458 module_exit(adv7180_exit);
460 MODULE_DESCRIPTION("Analog Devices ADV7180 video decoder driver");
461 MODULE_AUTHOR("Mocean Laboratories");
462 MODULE_LICENSE("GPL v2");