blob: d2138d06bcad976a931375c1fb0b9b09969d4baa [file] [log] [blame]
Richard Röjfors6789cb52009-09-18 21:17:20 -03001/*
2 * adv7180.c Analog Devices ADV7180 video decoder driver
3 * Copyright (c) 2009 Intel Corporation
4 *
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.
8 *
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.
13 *
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.
17 */
18
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Richard Röjfors6789cb52009-09-18 21:17:20 -030026#include <media/v4l2-ioctl.h>
27#include <linux/videodev2.h>
28#include <media/v4l2-device.h>
29#include <media/v4l2-chip-ident.h>
Richard Röjfors42752f72009-09-22 06:07:06 -030030#include <linux/mutex.h>
Richard Röjfors6789cb52009-09-18 21:17:20 -030031
32#define DRIVER_NAME "adv7180"
33
Richard Röjforsd3124292009-09-22 06:05:42 -030034#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
Richard Röjfors42752f72009-09-22 06:07:06 -030052#define ADV7180_EXTENDED_OUTPUT_CONTROL_REG 0x04
53#define ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS 0xC5
Richard Röjfors6789cb52009-09-18 21:17:20 -030054
Richard Röjfors42752f72009-09-22 06:07:06 -030055#define ADV7180_AUTODETECT_ENABLE_REG 0x07
56#define ADV7180_AUTODETECT_DEFAULT 0x7f
57
58#define ADV7180_ADI_CTRL_REG 0x0e
59#define ADV7180_ADI_CTRL_IRQ_SPACE 0x20
Richard Röjfors6789cb52009-09-18 21:17:20 -030060
Richard Röjforsd3124292009-09-22 06:05:42 -030061#define ADV7180_STATUS1_REG 0x10
62#define ADV7180_STATUS1_IN_LOCK 0x01
63#define ADV7180_STATUS1_AUTOD_MASK 0x70
Richard Röjfors6789cb52009-09-18 21:17:20 -030064#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
72
73#define ADV7180_IDENT_REG 0x11
74#define ADV7180_ID_7180 0x18
75
Richard Röjfors42752f72009-09-22 06:07:06 -030076#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
80
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
Richard Röjfors6789cb52009-09-18 21:17:20 -030092
93struct adv7180_state {
Richard Röjforsc277b602009-09-22 06:06:34 -030094 struct v4l2_subdev sd;
Richard Röjfors42752f72009-09-22 06:07:06 -030095 struct work_struct work;
96 struct mutex mutex; /* mutual excl. when accessing chip */
97 int irq;
Richard Röjforsc277b602009-09-22 06:06:34 -030098 v4l2_std_id curr_norm;
99 bool autodetect;
Richard Röjfors6789cb52009-09-18 21:17:20 -0300100};
101
Richard Röjforsd3124292009-09-22 06:05:42 -0300102static v4l2_std_id adv7180_std_to_v4l2(u8 status1)
Richard Röjfors6789cb52009-09-18 21:17:20 -0300103{
Richard Röjfors6789cb52009-09-18 21:17:20 -0300104 switch (status1 & ADV7180_STATUS1_AUTOD_MASK) {
105 case ADV7180_STATUS1_AUTOD_NTSM_M_J:
Richard Röjforsd3124292009-09-22 06:05:42 -0300106 return V4L2_STD_NTSC;
Richard Röjfors6789cb52009-09-18 21:17:20 -0300107 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;
123 }
124}
125
Richard Röjforsc277b602009-09-22 06:06:34 -0300126static int v4l2_std_to_adv7180(v4l2_std_id std)
127{
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;
138
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;
145
146 return -EINVAL;
147}
148
Richard Röjforsd3124292009-09-22 06:05:42 -0300149static u32 adv7180_status_to_v4l2(u8 status1)
150{
151 if (!(status1 & ADV7180_STATUS1_IN_LOCK))
152 return V4L2_IN_ST_NO_SIGNAL;
153
154 return 0;
155}
156
157static int __adv7180_status(struct i2c_client *client, u32 *status,
158 v4l2_std_id *std)
159{
160 int status1 = i2c_smbus_read_byte_data(client, ADV7180_STATUS1_REG);
161
162 if (status1 < 0)
163 return status1;
164
165 if (status)
166 *status = adv7180_status_to_v4l2(status1);
167 if (std)
168 *std = adv7180_std_to_v4l2(status1);
169
170 return 0;
171}
172
Richard Röjfors6789cb52009-09-18 21:17:20 -0300173static inline struct adv7180_state *to_state(struct v4l2_subdev *sd)
174{
175 return container_of(sd, struct adv7180_state, sd);
176}
177
178static int adv7180_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
179{
Richard Röjforsc277b602009-09-22 06:06:34 -0300180 struct adv7180_state *state = to_state(sd);
Richard Röjfors42752f72009-09-22 06:07:06 -0300181 int err = mutex_lock_interruptible(&state->mutex);
182 if (err)
183 return err;
Richard Röjforsc277b602009-09-22 06:06:34 -0300184
Richard Röjfors42752f72009-09-22 06:07:06 -0300185 /* when we are interrupt driven we know the state */
186 if (!state->autodetect || state->irq > 0)
Richard Röjforsc277b602009-09-22 06:06:34 -0300187 *std = state->curr_norm;
188 else
189 err = __adv7180_status(v4l2_get_subdevdata(sd), NULL, std);
190
Richard Röjfors42752f72009-09-22 06:07:06 -0300191 mutex_unlock(&state->mutex);
Richard Röjforsc277b602009-09-22 06:06:34 -0300192 return err;
Richard Röjforsd3124292009-09-22 06:05:42 -0300193}
Richard Röjfors6789cb52009-09-18 21:17:20 -0300194
Richard Röjforsd3124292009-09-22 06:05:42 -0300195static int adv7180_g_input_status(struct v4l2_subdev *sd, u32 *status)
196{
Richard Röjfors42752f72009-09-22 06:07:06 -0300197 struct adv7180_state *state = to_state(sd);
198 int ret = mutex_lock_interruptible(&state->mutex);
199 if (ret)
200 return ret;
201
202 ret = __adv7180_status(v4l2_get_subdevdata(sd), status, NULL);
203 mutex_unlock(&state->mutex);
204 return ret;
Richard Röjfors6789cb52009-09-18 21:17:20 -0300205}
206
207static int adv7180_g_chip_ident(struct v4l2_subdev *sd,
208 struct v4l2_dbg_chip_ident *chip)
209{
210 struct i2c_client *client = v4l2_get_subdevdata(sd);
211
212 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7180, 0);
213}
214
Richard Röjforsc277b602009-09-22 06:06:34 -0300215static int adv7180_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
216{
217 struct adv7180_state *state = to_state(sd);
218 struct i2c_client *client = v4l2_get_subdevdata(sd);
Richard Röjfors42752f72009-09-22 06:07:06 -0300219 int ret = mutex_lock_interruptible(&state->mutex);
220 if (ret)
221 return ret;
Richard Röjforsc277b602009-09-22 06:06:34 -0300222
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;
230
Richard Röjfors42752f72009-09-22 06:07:06 -0300231 __adv7180_status(client, NULL, &state->curr_norm);
Richard Röjforsc277b602009-09-22 06:06:34 -0300232 state->autodetect = true;
233 } else {
234 ret = v4l2_std_to_adv7180(std);
235 if (ret < 0)
236 goto out;
237
238 ret = i2c_smbus_write_byte_data(client,
239 ADV7180_INPUT_CONTROL_REG, ret);
240 if (ret < 0)
241 goto out;
242
243 state->curr_norm = std;
244 state->autodetect = false;
245 }
246 ret = 0;
247out:
Richard Röjfors42752f72009-09-22 06:07:06 -0300248 mutex_unlock(&state->mutex);
Richard Röjforsc277b602009-09-22 06:06:34 -0300249 return ret;
250}
251
Richard Röjfors6789cb52009-09-18 21:17:20 -0300252static const struct v4l2_subdev_video_ops adv7180_video_ops = {
253 .querystd = adv7180_querystd,
Richard Röjforsd3124292009-09-22 06:05:42 -0300254 .g_input_status = adv7180_g_input_status,
Richard Röjfors6789cb52009-09-18 21:17:20 -0300255};
256
257static const struct v4l2_subdev_core_ops adv7180_core_ops = {
258 .g_chip_ident = adv7180_g_chip_ident,
Richard Röjforsc277b602009-09-22 06:06:34 -0300259 .s_std = adv7180_s_std,
Richard Röjfors6789cb52009-09-18 21:17:20 -0300260};
261
262static const struct v4l2_subdev_ops adv7180_ops = {
263 .core = &adv7180_core_ops,
264 .video = &adv7180_video_ops,
265};
266
Richard Röjfors42752f72009-09-22 06:07:06 -0300267static void adv7180_work(struct work_struct *work)
268{
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;
273
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);
281
282 if (isr3 & ADV7180_IRQ3_AD_CHANGE && state->autodetect)
283 __adv7180_status(client, NULL, &state->curr_norm);
284 mutex_unlock(&state->mutex);
285
286 enable_irq(state->irq);
287}
288
289static irqreturn_t adv7180_irq(int irq, void *devid)
290{
291 struct adv7180_state *state = devid;
292
293 schedule_work(&state->work);
294
295 disable_irq_nosync(state->irq);
296
297 return IRQ_HANDLED;
298}
299
Richard Röjfors6789cb52009-09-18 21:17:20 -0300300/*
301 * Generic i2c probe
302 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
303 */
304
Richard Röjfors527aebf2009-09-22 06:07:34 -0300305static __devinit int adv7180_probe(struct i2c_client *client,
Richard Röjfors6789cb52009-09-18 21:17:20 -0300306 const struct i2c_device_id *id)
307{
308 struct adv7180_state *state;
309 struct v4l2_subdev *sd;
310 int ret;
311
312 /* Check if the adapter supports the needed features */
313 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
314 return -EIO;
315
316 v4l_info(client, "chip found @ 0x%02x (%s)\n",
317 client->addr << 1, client->adapter->name);
318
319 state = kzalloc(sizeof(struct adv7180_state), GFP_KERNEL);
Richard Röjfors42752f72009-09-22 06:07:06 -0300320 if (state == NULL) {
321 ret = -ENOMEM;
322 goto err;
323 }
324
325 state->irq = client->irq;
326 INIT_WORK(&state->work, adv7180_work);
327 mutex_init(&state->mutex);
Richard Röjforsc277b602009-09-22 06:06:34 -0300328 state->autodetect = true;
Richard Röjfors6789cb52009-09-18 21:17:20 -0300329 sd = &state->sd;
330 v4l2_i2c_subdev_init(sd, client, &adv7180_ops);
331
332 /* Initialize adv7180 */
Richard Röjfors42752f72009-09-22 06:07:06 -0300333 /* Enable autodetection */
Richard Röjfors6789cb52009-09-18 21:17:20 -0300334 ret = i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG,
Richard Röjforsd3124292009-09-22 06:05:42 -0300335 ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM);
Richard Röjfors42752f72009-09-22 06:07:06 -0300336 if (ret < 0)
337 goto err_unreg_subdev;
338
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;
343
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;
350
351 /* read current norm */
352 __adv7180_status(client, NULL, &state->curr_norm);
353
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;
360
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;
365
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;
371
372 ret = i2c_smbus_write_byte_data(client, ADV7180_IMR1_ADI, 0);
373 if (ret < 0)
374 goto err_unreg_subdev;
375
376 ret = i2c_smbus_write_byte_data(client, ADV7180_IMR2_ADI, 0);
377 if (ret < 0)
378 goto err_unreg_subdev;
379
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;
385
386 ret = i2c_smbus_write_byte_data(client, ADV7180_IMR4_ADI, 0);
387 if (ret < 0)
388 goto err_unreg_subdev;
389
390 ret = i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
391 0);
392 if (ret < 0)
393 goto err_unreg_subdev;
Richard Röjfors6789cb52009-09-18 21:17:20 -0300394 }
395
396 return 0;
Richard Röjfors42752f72009-09-22 06:07:06 -0300397
398err_unreg_subdev:
399 mutex_destroy(&state->mutex);
400 v4l2_device_unregister_subdev(sd);
401 kfree(state);
402err:
403 printk(KERN_ERR DRIVER_NAME ": Failed to probe: %d\n", ret);
404 return ret;
Richard Röjfors6789cb52009-09-18 21:17:20 -0300405}
406
Richard Röjfors527aebf2009-09-22 06:07:34 -0300407static __devexit int adv7180_remove(struct i2c_client *client)
Richard Röjfors6789cb52009-09-18 21:17:20 -0300408{
409 struct v4l2_subdev *sd = i2c_get_clientdata(client);
Richard Röjfors42752f72009-09-22 06:07:06 -0300410 struct adv7180_state *state = to_state(sd);
Richard Röjfors6789cb52009-09-18 21:17:20 -0300411
Richard Röjfors42752f72009-09-22 06:07:06 -0300412 if (state->irq > 0) {
413 free_irq(client->irq, state);
414 if (cancel_work_sync(&state->work)) {
415 /*
416 * Work was pending, therefore we need to enable
417 * IRQ here to balance the disable_irq() done in the
418 * interrupt handler.
419 */
420 enable_irq(state->irq);
421 }
422 }
423
424 mutex_destroy(&state->mutex);
Richard Röjfors6789cb52009-09-18 21:17:20 -0300425 v4l2_device_unregister_subdev(sd);
426 kfree(to_state(sd));
427 return 0;
428}
429
430static const struct i2c_device_id adv7180_id[] = {
431 {DRIVER_NAME, 0},
432 {},
433};
434
435MODULE_DEVICE_TABLE(i2c, adv7180_id);
436
437static struct i2c_driver adv7180_driver = {
438 .driver = {
439 .owner = THIS_MODULE,
440 .name = DRIVER_NAME,
441 },
442 .probe = adv7180_probe,
Richard Röjfors527aebf2009-09-22 06:07:34 -0300443 .remove = __devexit_p(adv7180_remove),
Richard Röjfors6789cb52009-09-18 21:17:20 -0300444 .id_table = adv7180_id,
445};
446
447static __init int adv7180_init(void)
448{
449 return i2c_add_driver(&adv7180_driver);
450}
451
452static __exit void adv7180_exit(void)
453{
454 i2c_del_driver(&adv7180_driver);
455}
456
457module_init(adv7180_init);
458module_exit(adv7180_exit);
459
460MODULE_DESCRIPTION("Analog Devices ADV7180 video decoder driver");
461MODULE_AUTHOR("Mocean Laboratories");
462MODULE_LICENSE("GPL v2");
463