blob: 20cad9fc99ef5061c26e6d19fc3b54d4f540fa3e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * saa7110 - Philips SAA7110(A) video decoder driver
3 *
4 * Copyright (C) 1998 Pauline Middelink <middelin@polyware.nl>
5 *
6 * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
7 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
8 * - some corrections for Pinnacle Systems Inc. DC10plus card.
9 *
10 * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
11 * - moved over to linux>=2.4.x i2c protocol (1/1/2003)
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/types.h>
31#include <linux/delay.h>
32#include <linux/slab.h>
33#include <linux/wait.h>
34#include <asm/io.h>
35#include <asm/uaccess.h>
36
37MODULE_DESCRIPTION("Philips SAA7110 video decoder driver");
38MODULE_AUTHOR("Pauline Middelink");
39MODULE_LICENSE("GPL");
40
41#include <linux/i2c.h>
42#include <linux/i2c-dev.h>
43
44#define I2C_NAME(s) (s)->name
45
46#include <linux/videodev.h>
47#include <linux/video_decoder.h>
48
49static int debug = 0;
50module_param(debug, int, 0);
51MODULE_PARM_DESC(debug, "Debug level (0-1)");
52
53#define dprintk(num, format, args...) \
54 do { \
55 if (debug >= num) \
56 printk(format, ##args); \
57 } while (0)
58
59#define SAA7110_MAX_INPUT 9 /* 6 CVBS, 3 SVHS */
60#define SAA7110_MAX_OUTPUT 0 /* its a decoder only */
61
62#define I2C_SAA7110 0x9C /* or 0x9E */
63
64#define SAA7110_NR_REG 0x35
65
66struct saa7110 {
67 u8 reg[SAA7110_NR_REG];
68
69 int norm;
70 int input;
71 int enable;
72 int bright;
73 int contrast;
74 int hue;
75 int sat;
76
77 wait_queue_head_t wq;
78};
79
80/* ----------------------------------------------------------------------- */
81/* I2C support functions */
82/* ----------------------------------------------------------------------- */
83
84static int
85saa7110_write (struct i2c_client *client,
86 u8 reg,
87 u8 value)
88{
89 struct saa7110 *decoder = i2c_get_clientdata(client);
90
91 decoder->reg[reg] = value;
92 return i2c_smbus_write_byte_data(client, reg, value);
93}
94
95static int
96saa7110_write_block (struct i2c_client *client,
97 const u8 *data,
98 unsigned int len)
99{
100 int ret = -1;
101 u8 reg = *data; /* first register to write to */
102
103 /* Sanity check */
104 if (reg + (len - 1) > SAA7110_NR_REG)
105 return ret;
106
107 /* the saa7110 has an autoincrement function, use it if
108 * the adapter understands raw I2C */
109 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
110 struct saa7110 *decoder = i2c_get_clientdata(client);
111 struct i2c_msg msg;
112
113 msg.len = len;
114 msg.buf = (char *) data;
115 msg.addr = client->addr;
116 msg.flags = 0;
117 ret = i2c_transfer(client->adapter, &msg, 1);
118
119 /* Cache the written data */
120 memcpy(decoder->reg + reg, data + 1, len - 1);
121 } else {
122 for (++data, --len; len; len--) {
123 if ((ret = saa7110_write(client, reg++,
124 *data++)) < 0)
125 break;
126 }
127 }
128
129 return ret;
130}
131
132static inline int
133saa7110_read (struct i2c_client *client)
134{
135 return i2c_smbus_read_byte(client);
136}
137
138/* ----------------------------------------------------------------------- */
139/* SAA7110 functions */
140/* ----------------------------------------------------------------------- */
141
142#define FRESP_06H_COMPST 0x03 //0x13
143#define FRESP_06H_SVIDEO 0x83 //0xC0
144
145
146static int
147saa7110_selmux (struct i2c_client *client,
148 int chan)
149{
150 static const unsigned char modes[9][8] = {
151 /* mode 0 */
152 {FRESP_06H_COMPST, 0xD9, 0x17, 0x40, 0x03,
153 0x44, 0x75, 0x16},
154 /* mode 1 */
155 {FRESP_06H_COMPST, 0xD8, 0x17, 0x40, 0x03,
156 0x44, 0x75, 0x16},
157 /* mode 2 */
158 {FRESP_06H_COMPST, 0xBA, 0x07, 0x91, 0x03,
159 0x60, 0xB5, 0x05},
160 /* mode 3 */
161 {FRESP_06H_COMPST, 0xB8, 0x07, 0x91, 0x03,
162 0x60, 0xB5, 0x05},
163 /* mode 4 */
164 {FRESP_06H_COMPST, 0x7C, 0x07, 0xD2, 0x83,
165 0x60, 0xB5, 0x03},
166 /* mode 5 */
167 {FRESP_06H_COMPST, 0x78, 0x07, 0xD2, 0x83,
168 0x60, 0xB5, 0x03},
169 /* mode 6 */
170 {FRESP_06H_SVIDEO, 0x59, 0x17, 0x42, 0xA3,
171 0x44, 0x75, 0x12},
172 /* mode 7 */
173 {FRESP_06H_SVIDEO, 0x9A, 0x17, 0xB1, 0x13,
174 0x60, 0xB5, 0x14},
175 /* mode 8 */
176 {FRESP_06H_SVIDEO, 0x3C, 0x27, 0xC1, 0x23,
177 0x44, 0x75, 0x21}
178 };
179 struct saa7110 *decoder = i2c_get_clientdata(client);
180 const unsigned char *ptr = modes[chan];
181
182 saa7110_write(client, 0x06, ptr[0]); /* Luminance control */
183 saa7110_write(client, 0x20, ptr[1]); /* Analog Control #1 */
184 saa7110_write(client, 0x21, ptr[2]); /* Analog Control #2 */
185 saa7110_write(client, 0x22, ptr[3]); /* Mixer Control #1 */
186 saa7110_write(client, 0x2C, ptr[4]); /* Mixer Control #2 */
187 saa7110_write(client, 0x30, ptr[5]); /* ADCs gain control */
188 saa7110_write(client, 0x31, ptr[6]); /* Mixer Control #3 */
189 saa7110_write(client, 0x21, ptr[7]); /* Analog Control #2 */
190 decoder->input = chan;
191
192 return 0;
193}
194
195static const unsigned char initseq[1 + SAA7110_NR_REG] = {
196 0, 0x4C, 0x3C, 0x0D, 0xEF, 0xBD, 0xF2, 0x03, 0x00,
197 /* 0x08 */ 0xF8, 0xF8, 0x60, 0x60, 0x00, 0x86, 0x18, 0x90,
198 /* 0x10 */ 0x00, 0x59, 0x40, 0x46, 0x42, 0x1A, 0xFF, 0xDA,
199 /* 0x18 */ 0xF2, 0x8B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
200 /* 0x20 */ 0xD9, 0x16, 0x40, 0x41, 0x80, 0x41, 0x80, 0x4F,
201 /* 0x28 */ 0xFE, 0x01, 0xCF, 0x0F, 0x03, 0x01, 0x03, 0x0C,
202 /* 0x30 */ 0x44, 0x71, 0x02, 0x8C, 0x02
203};
204
205static int
206determine_norm (struct i2c_client *client)
207{
208 DEFINE_WAIT(wait);
209 struct saa7110 *decoder = i2c_get_clientdata(client);
210 int status;
211
212 /* mode changed, start automatic detection */
213 saa7110_write_block(client, initseq, sizeof(initseq));
214 saa7110_selmux(client, decoder->input);
215 prepare_to_wait(&decoder->wq, &wait, TASK_UNINTERRUPTIBLE);
216 schedule_timeout(HZ/4);
217 finish_wait(&decoder->wq, &wait);
218 status = saa7110_read(client);
219 if (status & 0x40) {
220 dprintk(1, KERN_INFO "%s: status=0x%02x (no signal)\n",
221 I2C_NAME(client), status);
222 return decoder->norm; // no change
223 }
224 if ((status & 3) == 0) {
225 saa7110_write(client, 0x06, 0x83);
226 if (status & 0x20) {
227 dprintk(1,
228 KERN_INFO
229 "%s: status=0x%02x (NTSC/no color)\n",
230 I2C_NAME(client), status);
231 //saa7110_write(client,0x2E,0x81);
232 return VIDEO_MODE_NTSC;
233 }
234 dprintk(1, KERN_INFO "%s: status=0x%02x (PAL/no color)\n",
235 I2C_NAME(client), status);
236 //saa7110_write(client,0x2E,0x9A);
237 return VIDEO_MODE_PAL;
238 }
239 //saa7110_write(client,0x06,0x03);
240 if (status & 0x20) { /* 60Hz */
241 dprintk(1, KERN_INFO "%s: status=0x%02x (NTSC)\n",
242 I2C_NAME(client), status);
243 saa7110_write(client, 0x0D, 0x86);
244 saa7110_write(client, 0x0F, 0x50);
245 saa7110_write(client, 0x11, 0x2C);
246 //saa7110_write(client,0x2E,0x81);
247 return VIDEO_MODE_NTSC;
248 }
249
250 /* 50Hz -> PAL/SECAM */
251 saa7110_write(client, 0x0D, 0x86);
252 saa7110_write(client, 0x0F, 0x10);
253 saa7110_write(client, 0x11, 0x59);
254 //saa7110_write(client,0x2E,0x9A);
255
256 prepare_to_wait(&decoder->wq, &wait, TASK_UNINTERRUPTIBLE);
257 schedule_timeout(HZ/4);
258 finish_wait(&decoder->wq, &wait);
259
260 status = saa7110_read(client);
261 if ((status & 0x03) == 0x01) {
262 dprintk(1, KERN_INFO "%s: status=0x%02x (SECAM)\n",
263 I2C_NAME(client), status);
264 saa7110_write(client, 0x0D, 0x87);
265 return VIDEO_MODE_SECAM;
266 }
267 dprintk(1, KERN_INFO "%s: status=0x%02x (PAL)\n", I2C_NAME(client),
268 status);
269 return VIDEO_MODE_PAL;
270}
271
272static int
273saa7110_command (struct i2c_client *client,
274 unsigned int cmd,
275 void *arg)
276{
277 struct saa7110 *decoder = i2c_get_clientdata(client);
278 int v;
279
280 switch (cmd) {
281 case 0:
282 //saa7110_write_block(client, initseq, sizeof(initseq));
283 break;
284
285 case DECODER_GET_CAPABILITIES:
286 {
287 struct video_decoder_capability *dc = arg;
288
289 dc->flags =
290 VIDEO_DECODER_PAL | VIDEO_DECODER_NTSC |
291 VIDEO_DECODER_SECAM | VIDEO_DECODER_AUTO;
292 dc->inputs = SAA7110_MAX_INPUT;
293 dc->outputs = SAA7110_MAX_OUTPUT;
294 }
295 break;
296
297 case DECODER_GET_STATUS:
298 {
299 int status;
300 int res = 0;
301
302 status = saa7110_read(client);
303 dprintk(1, KERN_INFO "%s: status=0x%02x norm=%d\n",
304 I2C_NAME(client), status, decoder->norm);
305 if (!(status & 0x40))
306 res |= DECODER_STATUS_GOOD;
307 if (status & 0x03)
308 res |= DECODER_STATUS_COLOR;
309
310 switch (decoder->norm) {
311 case VIDEO_MODE_NTSC:
312 res |= DECODER_STATUS_NTSC;
313 break;
314 case VIDEO_MODE_PAL:
315 res |= DECODER_STATUS_PAL;
316 break;
317 case VIDEO_MODE_SECAM:
318 res |= DECODER_STATUS_SECAM;
319 break;
320 }
321 *(int *) arg = res;
322 }
323 break;
324
325 case DECODER_SET_NORM:
326 v = *(int *) arg;
327 if (decoder->norm != v) {
328 decoder->norm = v;
329 //saa7110_write(client, 0x06, 0x03);
330 switch (v) {
331 case VIDEO_MODE_NTSC:
332 saa7110_write(client, 0x0D, 0x86);
333 saa7110_write(client, 0x0F, 0x50);
334 saa7110_write(client, 0x11, 0x2C);
335 //saa7110_write(client, 0x2E, 0x81);
336 dprintk(1,
337 KERN_INFO "%s: switched to NTSC\n",
338 I2C_NAME(client));
339 break;
340 case VIDEO_MODE_PAL:
341 saa7110_write(client, 0x0D, 0x86);
342 saa7110_write(client, 0x0F, 0x10);
343 saa7110_write(client, 0x11, 0x59);
344 //saa7110_write(client, 0x2E, 0x9A);
345 dprintk(1,
346 KERN_INFO "%s: switched to PAL\n",
347 I2C_NAME(client));
348 break;
349 case VIDEO_MODE_SECAM:
350 saa7110_write(client, 0x0D, 0x87);
351 saa7110_write(client, 0x0F, 0x10);
352 saa7110_write(client, 0x11, 0x59);
353 //saa7110_write(client, 0x2E, 0x9A);
354 dprintk(1,
355 KERN_INFO
356 "%s: switched to SECAM\n",
357 I2C_NAME(client));
358 break;
359 case VIDEO_MODE_AUTO:
360 dprintk(1,
361 KERN_INFO
362 "%s: TV standard detection...\n",
363 I2C_NAME(client));
364 decoder->norm = determine_norm(client);
365 *(int *) arg = decoder->norm;
366 break;
367 default:
368 return -EPERM;
369 }
370 }
371 break;
372
373 case DECODER_SET_INPUT:
374 v = *(int *) arg;
375 if (v < 0 || v > SAA7110_MAX_INPUT) {
376 dprintk(1,
377 KERN_INFO "%s: input=%d not available\n",
378 I2C_NAME(client), v);
379 return -EINVAL;
380 }
381 if (decoder->input != v) {
382 saa7110_selmux(client, v);
383 dprintk(1, KERN_INFO "%s: switched to input=%d\n",
384 I2C_NAME(client), v);
385 }
386 break;
387
388 case DECODER_SET_OUTPUT:
389 v = *(int *) arg;
390 /* not much choice of outputs */
391 if (v != 0)
392 return -EINVAL;
393 break;
394
395 case DECODER_ENABLE_OUTPUT:
396 v = *(int *) arg;
397 if (decoder->enable != v) {
398 decoder->enable = v;
399 saa7110_write(client, 0x0E, v ? 0x18 : 0x80);
400 dprintk(1, KERN_INFO "%s: YUV %s\n", I2C_NAME(client),
401 v ? "on" : "off");
402 }
403 break;
404
405 case DECODER_SET_PICTURE:
406 {
407 struct video_picture *pic = arg;
408
409 if (decoder->bright != pic->brightness) {
410 /* We want 0 to 255 we get 0-65535 */
411 decoder->bright = pic->brightness;
412 saa7110_write(client, 0x19, decoder->bright >> 8);
413 }
414 if (decoder->contrast != pic->contrast) {
415 /* We want 0 to 127 we get 0-65535 */
416 decoder->contrast = pic->contrast;
417 saa7110_write(client, 0x13,
418 decoder->contrast >> 9);
419 }
420 if (decoder->sat != pic->colour) {
421 /* We want 0 to 127 we get 0-65535 */
422 decoder->sat = pic->colour;
423 saa7110_write(client, 0x12, decoder->sat >> 9);
424 }
425 if (decoder->hue != pic->hue) {
426 /* We want -128 to 127 we get 0-65535 */
427 decoder->hue = pic->hue;
428 saa7110_write(client, 0x07,
429 (decoder->hue >> 8) - 128);
430 }
431 }
432 break;
433
434 case DECODER_DUMP:
Jean Delvare62015732006-03-22 03:48:30 -0300435 for (v = 0; v < SAA7110_NR_REG; v += 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 int j;
Jean Delvare62015732006-03-22 03:48:30 -0300437 dprintk(1, KERN_DEBUG "%s: %02x:", I2C_NAME(client),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 v);
Jean Delvare62015732006-03-22 03:48:30 -0300439 for (j = 0; j < 16 && v + j < SAA7110_NR_REG; j++)
440 dprintk(1, " %02x", decoder->reg[v + j]);
441 dprintk(1, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443 break;
444
445 default:
446 dprintk(1, KERN_INFO "unknown saa7110_command??(%d)\n",
447 cmd);
448 return -EINVAL;
449 }
450 return 0;
451}
452
453/* ----------------------------------------------------------------------- */
454
455/*
456 * Generic i2c probe
457 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
458 */
459static unsigned short normal_i2c[] = {
460 I2C_SAA7110 >> 1,
461 (I2C_SAA7110 >> 1) + 1,
462 I2C_CLIENT_END
463};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Jean Delvare68cc9d02005-04-02 20:04:41 +0200465static unsigned short ignore = I2C_CLIENT_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467static struct i2c_client_address_data addr_data = {
468 .normal_i2c = normal_i2c,
Jean Delvare68cc9d02005-04-02 20:04:41 +0200469 .probe = &ignore,
470 .ignore = &ignore,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471};
472
473static struct i2c_driver i2c_driver_saa7110;
474
475static int
476saa7110_detect_client (struct i2c_adapter *adapter,
477 int address,
478 int kind)
479{
480 struct i2c_client *client;
481 struct saa7110 *decoder;
482 int rv;
483
484 dprintk(1,
485 KERN_INFO
486 "saa7110.c: detecting saa7110 client on address 0x%x\n",
487 address << 1);
488
489 /* Check if the adapter supports the needed features */
490 if (!i2c_check_functionality
491 (adapter,
492 I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
493 return 0;
494
Panagiotis Issaris74081872006-01-11 19:40:56 -0200495 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (client == 0)
497 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 client->addr = address;
499 client->adapter = adapter;
500 client->driver = &i2c_driver_saa7110;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 strlcpy(I2C_NAME(client), "saa7110", sizeof(I2C_NAME(client)));
502
Panagiotis Issaris74081872006-01-11 19:40:56 -0200503 decoder = kzalloc(sizeof(struct saa7110), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (decoder == 0) {
505 kfree(client);
506 return -ENOMEM;
507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 decoder->norm = VIDEO_MODE_PAL;
509 decoder->input = 0;
510 decoder->enable = 1;
511 decoder->bright = 32768;
512 decoder->contrast = 32768;
513 decoder->hue = 32768;
514 decoder->sat = 32768;
515 init_waitqueue_head(&decoder->wq);
516 i2c_set_clientdata(client, decoder);
517
518 rv = i2c_attach_client(client);
519 if (rv) {
520 kfree(client);
521 kfree(decoder);
522 return rv;
523 }
524
525 rv = saa7110_write_block(client, initseq, sizeof(initseq));
526 if (rv < 0)
527 dprintk(1, KERN_ERR "%s_attach: init status %d\n",
528 I2C_NAME(client), rv);
529 else {
530 int ver, status;
531 saa7110_write(client, 0x21, 0x10);
532 saa7110_write(client, 0x0e, 0x18);
533 saa7110_write(client, 0x0D, 0x04);
534 ver = saa7110_read(client);
535 saa7110_write(client, 0x0D, 0x06);
536 //mdelay(150);
537 status = saa7110_read(client);
538 dprintk(1,
539 KERN_INFO
540 "%s_attach: SAA7110A version %x at 0x%02x, status=0x%02x\n",
541 I2C_NAME(client), ver, client->addr << 1, status);
542 saa7110_write(client, 0x0D, 0x86);
543 saa7110_write(client, 0x0F, 0x10);
544 saa7110_write(client, 0x11, 0x59);
545 //saa7110_write(client, 0x2E, 0x9A);
546 }
547
548 //saa7110_selmux(client,0);
549 //determine_norm(client);
550 /* setup and implicit mode 0 select has been performed */
551
552 return 0;
553}
554
555static int
556saa7110_attach_adapter (struct i2c_adapter *adapter)
557{
558 dprintk(1,
559 KERN_INFO
560 "saa7110.c: starting probe for adapter %s (0x%x)\n",
561 I2C_NAME(adapter), adapter->id);
562 return i2c_probe(adapter, &addr_data, &saa7110_detect_client);
563}
564
565static int
566saa7110_detach_client (struct i2c_client *client)
567{
568 struct saa7110 *decoder = i2c_get_clientdata(client);
569 int err;
570
571 err = i2c_detach_client(client);
572 if (err) {
573 return err;
574 }
575
576 kfree(decoder);
577 kfree(client);
578
579 return 0;
580}
581
582/* ----------------------------------------------------------------------- */
583
584static struct i2c_driver i2c_driver_saa7110 = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100585 .driver = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100586 .name = "saa7110",
587 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 .id = I2C_DRIVERID_SAA7110,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 .attach_adapter = saa7110_attach_adapter,
592 .detach_client = saa7110_detach_client,
593 .command = saa7110_command,
594};
595
596static int __init
597saa7110_init (void)
598{
599 return i2c_add_driver(&i2c_driver_saa7110);
600}
601
602static void __exit
603saa7110_exit (void)
604{
605 i2c_del_driver(&i2c_driver_saa7110);
606}
607
608module_init(saa7110_init);
609module_exit(saa7110_exit);