blob: 686fd4746205385c23c8b6b53731e37df5de7287 [file] [log] [blame]
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * saa7111 - Philips SAA7111A video decoder driver version 0.0.3
3 *
4 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
5 *
6 * Slight changes for video timing and attachment output by
7 * Wolfgang Scherr <scherr@net4you.net>
8 *
9 * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
10 * - moved over to linux>=2.4.x i2c protocol (1/1/2003)
11 *
12 * Changes by Michael Hunold <michael@mihu.de>
13 * - implemented DECODER_SET_GPIO, DECODER_INIT, DECODER_SET_VBI_BYPASS
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 */
29
30#include <linux/module.h>
31#include <linux/init.h>
32#include <linux/delay.h>
33#include <linux/errno.h>
34#include <linux/fs.h>
35#include <linux/kernel.h>
36#include <linux/major.h>
37#include <linux/slab.h>
38#include <linux/mm.h>
39#include <linux/pci.h>
40#include <linux/signal.h>
41#include <asm/io.h>
42#include <asm/pgtable.h>
43#include <asm/page.h>
44#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/types.h>
46
47#include <linux/videodev.h>
48#include <asm/uaccess.h>
49
50MODULE_DESCRIPTION("Philips SAA7111 video decoder driver");
51MODULE_AUTHOR("Dave Perks");
52MODULE_LICENSE("GPL");
53
54#include <linux/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#define I2C_NAME(s) (s)->name
57
58#include <linux/video_decoder.h>
59
60static int debug = 0;
61module_param(debug, int, 0644);
62MODULE_PARM_DESC(debug, "Debug level (0-1)");
63
64#define dprintk(num, format, args...) \
65 do { \
66 if (debug >= num) \
67 printk(format, ##args); \
68 } while (0)
69
70/* ----------------------------------------------------------------------- */
71
Jean Delvare6eb5d9c2006-03-22 03:48:32 -030072#define SAA7111_NR_REG 0x18
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074struct saa7111 {
Jean Delvare6eb5d9c2006-03-22 03:48:32 -030075 unsigned char reg[SAA7111_NR_REG];
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 int norm;
78 int input;
79 int enable;
80 int bright;
81 int contrast;
82 int hue;
83 int sat;
84};
85
86#define I2C_SAA7111 0x48
87
88/* ----------------------------------------------------------------------- */
89
90static inline int
91saa7111_write (struct i2c_client *client,
92 u8 reg,
93 u8 value)
94{
95 struct saa7111 *decoder = i2c_get_clientdata(client);
96
97 decoder->reg[reg] = value;
98 return i2c_smbus_write_byte_data(client, reg, value);
99}
100
101static int
102saa7111_write_block (struct i2c_client *client,
103 const u8 *data,
104 unsigned int len)
105{
106 int ret = -1;
107 u8 reg;
108
109 /* the saa7111 has an autoincrement function, use it if
110 * the adapter understands raw I2C */
111 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
112 /* do raw I2C, not smbus compatible */
113 struct saa7111 *decoder = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 u8 block_data[32];
Jean Delvare9aa45e32006-03-22 03:48:35 -0300115 int block_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 while (len >= 2) {
Jean Delvare9aa45e32006-03-22 03:48:35 -0300118 block_len = 0;
119 block_data[block_len++] = reg = data[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 do {
Jean Delvare9aa45e32006-03-22 03:48:35 -0300121 block_data[block_len++] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 decoder->reg[reg++] = data[1];
123 len -= 2;
124 data += 2;
125 } while (len >= 2 && data[0] == reg &&
Jean Delvare9aa45e32006-03-22 03:48:35 -0300126 block_len < 32);
127 if ((ret = i2c_master_send(client, block_data,
128 block_len)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 break;
130 }
131 } else {
132 /* do some slow I2C emulation kind of thing */
133 while (len >= 2) {
134 reg = *data++;
135 if ((ret = saa7111_write(client, reg,
136 *data++)) < 0)
137 break;
138 len -= 2;
139 }
140 }
141
142 return ret;
143}
144
145static int
146saa7111_init_decoder (struct i2c_client *client,
147 struct video_decoder_init *init)
148{
149 return saa7111_write_block(client, init->data, init->len);
150}
151
152static inline int
153saa7111_read (struct i2c_client *client,
154 u8 reg)
155{
156 return i2c_smbus_read_byte_data(client, reg);
157}
158
159/* ----------------------------------------------------------------------- */
160
161static const unsigned char saa7111_i2c_init[] = {
162 0x00, 0x00, /* 00 - ID byte */
163 0x01, 0x00, /* 01 - reserved */
164
165 /*front end */
166 0x02, 0xd0, /* 02 - FUSE=3, GUDL=2, MODE=0 */
167 0x03, 0x23, /* 03 - HLNRS=0, VBSL=1, WPOFF=0,
168 * HOLDG=0, GAFIX=0, GAI1=256, GAI2=256 */
169 0x04, 0x00, /* 04 - GAI1=256 */
170 0x05, 0x00, /* 05 - GAI2=256 */
171
172 /* decoder */
173 0x06, 0xf3, /* 06 - HSB at 13(50Hz) / 17(60Hz)
174 * pixels after end of last line */
175 /*0x07, 0x13, * 07 - HSS at 113(50Hz) / 117(60Hz) pixels
176 * after end of last line */
177 0x07, 0xe8, /* 07 - HSS seems to be needed to
178 * work with NTSC, too */
179 0x08, 0xc8, /* 08 - AUFD=1, FSEL=1, EXFIL=0,
180 * VTRC=1, HPLL=0, VNOI=0 */
181 0x09, 0x01, /* 09 - BYPS=0, PREF=0, BPSS=0,
182 * VBLB=0, UPTCV=0, APER=1 */
183 0x0a, 0x80, /* 0a - BRIG=128 */
184 0x0b, 0x47, /* 0b - CONT=1.109 */
185 0x0c, 0x40, /* 0c - SATN=1.0 */
186 0x0d, 0x00, /* 0d - HUE=0 */
187 0x0e, 0x01, /* 0e - CDTO=0, CSTD=0, DCCF=0,
188 * FCTC=0, CHBW=1 */
189 0x0f, 0x00, /* 0f - reserved */
190 0x10, 0x48, /* 10 - OFTS=1, HDEL=0, VRLN=1, YDEL=0 */
191 0x11, 0x1c, /* 11 - GPSW=0, CM99=0, FECO=0, COMPO=1,
192 * OEYC=1, OEHV=1, VIPB=0, COLO=0 */
193 0x12, 0x00, /* 12 - output control 2 */
194 0x13, 0x00, /* 13 - output control 3 */
195 0x14, 0x00, /* 14 - reserved */
196 0x15, 0x00, /* 15 - VBI */
197 0x16, 0x00, /* 16 - VBI */
198 0x17, 0x00, /* 17 - VBI */
199};
200
201static int
202saa7111_command (struct i2c_client *client,
203 unsigned int cmd,
204 void *arg)
205{
206 struct saa7111 *decoder = i2c_get_clientdata(client);
207
208 switch (cmd) {
209
210 case 0:
Martin Samuelsson58a0b842006-03-22 03:48:38 -0300211 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 case DECODER_INIT:
213 {
214 struct video_decoder_init *init = arg;
215 if (NULL != init)
216 return saa7111_init_decoder(client, init);
217 else {
218 struct video_decoder_init vdi;
219 vdi.data = saa7111_i2c_init;
220 vdi.len = sizeof(saa7111_i2c_init);
221 return saa7111_init_decoder(client, &vdi);
222 }
223 }
224
225 case DECODER_DUMP:
226 {
227 int i;
228
Jean Delvare6eb5d9c2006-03-22 03:48:32 -0300229 for (i = 0; i < SAA7111_NR_REG; i += 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 int j;
231
232 printk(KERN_DEBUG "%s: %03x", I2C_NAME(client), i);
Jean Delvare6eb5d9c2006-03-22 03:48:32 -0300233 for (j = 0; j < 16 && i + j < SAA7111_NR_REG; ++j) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 printk(" %02x",
235 saa7111_read(client, i + j));
236 }
237 printk("\n");
238 }
239 }
240 break;
241
242 case DECODER_GET_CAPABILITIES:
243 {
244 struct video_decoder_capability *cap = arg;
245
246 cap->flags = VIDEO_DECODER_PAL |
247 VIDEO_DECODER_NTSC |
248 VIDEO_DECODER_SECAM |
249 VIDEO_DECODER_AUTO |
250 VIDEO_DECODER_CCIR;
251 cap->inputs = 8;
252 cap->outputs = 1;
253 }
254 break;
255
256 case DECODER_GET_STATUS:
257 {
258 int *iarg = arg;
259 int status;
260 int res;
261
262 status = saa7111_read(client, 0x1f);
263 dprintk(1, KERN_DEBUG "%s status: 0x%02x\n", I2C_NAME(client),
264 status);
265 res = 0;
266 if ((status & (1 << 6)) == 0) {
267 res |= DECODER_STATUS_GOOD;
268 }
269 switch (decoder->norm) {
270 case VIDEO_MODE_NTSC:
271 res |= DECODER_STATUS_NTSC;
272 break;
273 case VIDEO_MODE_PAL:
274 res |= DECODER_STATUS_PAL;
275 break;
276 case VIDEO_MODE_SECAM:
277 res |= DECODER_STATUS_SECAM;
278 break;
279 default:
280 case VIDEO_MODE_AUTO:
281 if ((status & (1 << 5)) != 0) {
282 res |= DECODER_STATUS_NTSC;
283 } else {
284 res |= DECODER_STATUS_PAL;
285 }
286 break;
287 }
288 if ((status & (1 << 0)) != 0) {
289 res |= DECODER_STATUS_COLOR;
290 }
291 *iarg = res;
292 }
293 break;
294
295 case DECODER_SET_GPIO:
296 {
297 int *iarg = arg;
298 if (0 != *iarg) {
299 saa7111_write(client, 0x11,
300 (decoder->reg[0x11] | 0x80));
301 } else {
302 saa7111_write(client, 0x11,
303 (decoder->reg[0x11] & 0x7f));
304 }
305 break;
306 }
307
308 case DECODER_SET_VBI_BYPASS:
309 {
310 int *iarg = arg;
311 if (0 != *iarg) {
312 saa7111_write(client, 0x13,
313 (decoder->reg[0x13] & 0xf0) | 0x0a);
314 } else {
315 saa7111_write(client, 0x13,
316 (decoder->reg[0x13] & 0xf0));
317 }
318 break;
319 }
320
321 case DECODER_SET_NORM:
322 {
323 int *iarg = arg;
324
325 switch (*iarg) {
326
327 case VIDEO_MODE_NTSC:
328 saa7111_write(client, 0x08,
329 (decoder->reg[0x08] & 0x3f) | 0x40);
330 saa7111_write(client, 0x0e,
331 (decoder->reg[0x0e] & 0x8f));
332 break;
333
334 case VIDEO_MODE_PAL:
335 saa7111_write(client, 0x08,
336 (decoder->reg[0x08] & 0x3f) | 0x00);
337 saa7111_write(client, 0x0e,
338 (decoder->reg[0x0e] & 0x8f));
339 break;
340
341 case VIDEO_MODE_SECAM:
342 saa7111_write(client, 0x08,
343 (decoder->reg[0x08] & 0x3f) | 0x00);
344 saa7111_write(client, 0x0e,
345 (decoder->reg[0x0e] & 0x8f) | 0x50);
346 break;
347
348 case VIDEO_MODE_AUTO:
349 saa7111_write(client, 0x08,
350 (decoder->reg[0x08] & 0x3f) | 0x80);
351 saa7111_write(client, 0x0e,
352 (decoder->reg[0x0e] & 0x8f));
353 break;
354
355 default:
356 return -EINVAL;
357
358 }
359 decoder->norm = *iarg;
360 }
361 break;
362
363 case DECODER_SET_INPUT:
364 {
365 int *iarg = arg;
366
367 if (*iarg < 0 || *iarg > 7) {
368 return -EINVAL;
369 }
370
371 if (decoder->input != *iarg) {
372 decoder->input = *iarg;
373 /* select mode */
374 saa7111_write(client, 0x02,
375 (decoder->
376 reg[0x02] & 0xf8) | decoder->input);
377 /* bypass chrominance trap for modes 4..7 */
378 saa7111_write(client, 0x09,
379 (decoder->
380 reg[0x09] & 0x7f) | ((decoder->
381 input >
382 3) ? 0x80 :
383 0));
384 }
385 }
386 break;
387
388 case DECODER_SET_OUTPUT:
389 {
390 int *iarg = arg;
391
392 /* not much choice of outputs */
393 if (*iarg != 0) {
394 return -EINVAL;
395 }
396 }
397 break;
398
399 case DECODER_ENABLE_OUTPUT:
400 {
401 int *iarg = arg;
402 int enable = (*iarg != 0);
403
404 if (decoder->enable != enable) {
405 decoder->enable = enable;
406
407 /* RJ: If output should be disabled (for
408 * playing videos), we also need a open PLL.
409 * The input is set to 0 (where no input
410 * source is connected), although this
411 * is not necessary.
412 *
413 * If output should be enabled, we have to
414 * reverse the above.
415 */
416
417 if (decoder->enable) {
418 saa7111_write(client, 0x02,
419 (decoder->
420 reg[0x02] & 0xf8) |
421 decoder->input);
422 saa7111_write(client, 0x08,
423 (decoder->reg[0x08] & 0xfb));
424 saa7111_write(client, 0x11,
425 (decoder->
426 reg[0x11] & 0xf3) | 0x0c);
427 } else {
428 saa7111_write(client, 0x02,
429 (decoder->reg[0x02] & 0xf8));
430 saa7111_write(client, 0x08,
431 (decoder->
432 reg[0x08] & 0xfb) | 0x04);
433 saa7111_write(client, 0x11,
434 (decoder->reg[0x11] & 0xf3));
435 }
436 }
437 }
438 break;
439
440 case DECODER_SET_PICTURE:
441 {
442 struct video_picture *pic = arg;
443
444 if (decoder->bright != pic->brightness) {
445 /* We want 0 to 255 we get 0-65535 */
446 decoder->bright = pic->brightness;
447 saa7111_write(client, 0x0a, decoder->bright >> 8);
448 }
449 if (decoder->contrast != pic->contrast) {
450 /* We want 0 to 127 we get 0-65535 */
451 decoder->contrast = pic->contrast;
452 saa7111_write(client, 0x0b,
453 decoder->contrast >> 9);
454 }
455 if (decoder->sat != pic->colour) {
456 /* We want 0 to 127 we get 0-65535 */
457 decoder->sat = pic->colour;
458 saa7111_write(client, 0x0c, decoder->sat >> 9);
459 }
460 if (decoder->hue != pic->hue) {
461 /* We want -128 to 127 we get 0-65535 */
462 decoder->hue = pic->hue;
463 saa7111_write(client, 0x0d,
464 (decoder->hue - 32768) >> 8);
465 }
466 }
467 break;
468
469 default:
470 return -EINVAL;
471 }
472
473 return 0;
474}
475
476/* ----------------------------------------------------------------------- */
477
478/*
479 * Generic i2c probe
480 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
481 */
482static unsigned short normal_i2c[] = { I2C_SAA7111 >> 1, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Jean Delvare68cc9d02005-04-02 20:04:41 +0200484static unsigned short ignore = I2C_CLIENT_END;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486static struct i2c_client_address_data addr_data = {
487 .normal_i2c = normal_i2c,
Jean Delvare68cc9d02005-04-02 20:04:41 +0200488 .probe = &ignore,
489 .ignore = &ignore,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490};
491
492static struct i2c_driver i2c_driver_saa7111;
493
494static int
495saa7111_detect_client (struct i2c_adapter *adapter,
496 int address,
497 int kind)
498{
499 int i;
500 struct i2c_client *client;
501 struct saa7111 *decoder;
502 struct video_decoder_init vdi;
503
504 dprintk(1,
505 KERN_INFO
506 "saa7111.c: detecting saa7111 client on address 0x%x\n",
507 address << 1);
508
509 /* Check if the adapter supports the needed features */
510 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
511 return 0;
512
Panagiotis Issaris74081872006-01-11 19:40:56 -0200513 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (client == 0)
515 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 client->addr = address;
517 client->adapter = adapter;
518 client->driver = &i2c_driver_saa7111;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 strlcpy(I2C_NAME(client), "saa7111", sizeof(I2C_NAME(client)));
520
Panagiotis Issaris74081872006-01-11 19:40:56 -0200521 decoder = kzalloc(sizeof(struct saa7111), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (decoder == NULL) {
523 kfree(client);
524 return -ENOMEM;
525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 decoder->norm = VIDEO_MODE_NTSC;
527 decoder->input = 0;
528 decoder->enable = 1;
529 decoder->bright = 32768;
530 decoder->contrast = 32768;
531 decoder->hue = 32768;
532 decoder->sat = 32768;
533 i2c_set_clientdata(client, decoder);
534
535 i = i2c_attach_client(client);
536 if (i) {
537 kfree(client);
538 kfree(decoder);
539 return i;
540 }
541
542 vdi.data = saa7111_i2c_init;
543 vdi.len = sizeof(saa7111_i2c_init);
544 i = saa7111_init_decoder(client, &vdi);
545 if (i < 0) {
546 dprintk(1, KERN_ERR "%s_attach error: init status %d\n",
547 I2C_NAME(client), i);
548 } else {
549 dprintk(1,
550 KERN_INFO
551 "%s_attach: chip version %x at address 0x%x\n",
552 I2C_NAME(client), saa7111_read(client, 0x00) >> 4,
553 client->addr << 1);
554 }
555
556 return 0;
557}
558
559static int
560saa7111_attach_adapter (struct i2c_adapter *adapter)
561{
562 dprintk(1,
563 KERN_INFO
564 "saa7111.c: starting probe for adapter %s (0x%x)\n",
565 I2C_NAME(adapter), adapter->id);
566 return i2c_probe(adapter, &addr_data, &saa7111_detect_client);
567}
568
569static int
570saa7111_detach_client (struct i2c_client *client)
571{
572 struct saa7111 *decoder = i2c_get_clientdata(client);
573 int err;
574
575 err = i2c_detach_client(client);
576 if (err) {
577 return err;
578 }
579
580 kfree(decoder);
581 kfree(client);
582
583 return 0;
584}
585
586/* ----------------------------------------------------------------------- */
587
588static struct i2c_driver i2c_driver_saa7111 = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100589 .driver = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100590 .name = "saa7111",
591 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 .id = I2C_DRIVERID_SAA7111A,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 .attach_adapter = saa7111_attach_adapter,
596 .detach_client = saa7111_detach_client,
597 .command = saa7111_command,
598};
599
600static int __init
601saa7111_init (void)
602{
603 return i2c_add_driver(&i2c_driver_saa7111);
604}
605
606static void __exit
607saa7111_exit (void)
608{
609 i2c_del_driver(&i2c_driver_saa7111);
610}
611
612module_init(saa7111_init);
613module_exit(saa7111_exit);