blob: 85f3403f74040d1b4d29d798ff35ee3fab396c7c [file] [log] [blame]
Dwaine Garden340622c2005-11-08 21:37:34 -08001/*
2 * saa7111 - Philips SAA7113A 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>
45#include <asm/segment.h>
46#include <linux/types.h>
47#include <asm/uaccess.h>
48#include <linux/videodev.h>
49
50MODULE_DESCRIPTION("Philips SAA7113 video decoder driver");
51MODULE_AUTHOR("Dave Perks, Jose Ignacio Gijon, Joerg Heckenbach, Mark McClelland, Dwaine Garden");
52MODULE_LICENSE("GPL");
53
54#include <linux/i2c.h>
55#include <linux/i2c-dev.h>
56
57#define I2C_NAME(s) (s)->name
58
59#include <linux/video_decoder.h>
60
61static int debug = 0;
62MODULE_PARM(debug, "i");
63MODULE_PARM_DESC(debug, " Set the default Debug level. Default: 0 (Off) - (0-1)");
64
65
66#define dprintk(num, format, args...) \
67 do { \
68 if (debug >= num) \
69 printk(format , ##args); \
70 } while (0)
71
72/* ----------------------------------------------------------------------- */
73
74struct saa7113 {
75 unsigned char reg[32];
76
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_SAA7113 0x4A
Markus Rechberger791b4032005-11-08 21:38:08 -080087#define I2C_SAA7114 0x42
Dwaine Garden340622c2005-11-08 21:37:34 -080088
89/* ----------------------------------------------------------------------- */
90
91static inline int
92saa7113_write (struct i2c_client *client,
93 u8 reg,
94 u8 value)
95{
96 struct saa7113 *decoder = i2c_get_clientdata(client);
97
98 decoder->reg[reg] = value;
99 return i2c_smbus_write_byte_data(client, reg, value);
100}
101
102static int
103saa7113_write_block (struct i2c_client *client,
104 const u8 *data,
105 unsigned int len)
106{
107 int ret = -1;
108 u8 reg;
109
110 /* the saa7113 has an autoincrement function, use it if
111 * the adapter understands raw I2C */
112 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
113 /* do raw I2C, not smbus compatible */
114 struct saa7113 *decoder = i2c_get_clientdata(client);
115 struct i2c_msg msg;
116 u8 block_data[32];
117
118 msg.addr = client->addr;
119 msg.flags = 0;
120 while (len >= 2) {
121 msg.buf = (char *) block_data;
122 msg.len = 0;
123 block_data[msg.len++] = reg = data[0];
124 do {
125 block_data[msg.len++] =
126 decoder->reg[reg++] = data[1];
127 len -= 2;
128 data += 2;
129 } while (len >= 2 && data[0] == reg &&
130 msg.len < 32);
131 if ((ret = i2c_transfer(client->adapter,
132 &msg, 1)) < 0)
133 break;
134 }
135 } else {
136 /* do some slow I2C emulation kind of thing */
137 while (len >= 2) {
138 reg = *data++;
139 if ((ret = saa7113_write(client, reg,
140 *data++)) < 0)
141 break;
142 len -= 2;
143 }
144 }
145
146 return ret;
147}
148
149static int
150saa7113_init_decoder (struct i2c_client *client,
151 struct video_decoder_init *init)
152{
153 return saa7113_write_block(client, init->data, init->len);
154}
155
156static inline int
157saa7113_read (struct i2c_client *client,
158 u8 reg)
159{
160 return i2c_smbus_read_byte_data(client, reg);
161}
162
163/* ----------------------------------------------------------------------- */
164
165static const unsigned char saa7113_i2c_init[] = {
166 0x00, 0x00, /* PH7113_CHIP_VERSION 00 - ID byte */
167 0x01, 0x08, /* PH7113_INCREMENT_DELAY - (1) (1) (1) (1) IDEL3 IDEL2 IDELL1 IDEL0 */
168 0x02, 0xc0, /* PH7113_ANALOG_INPUT_CONTR_1 - FUSE1 FUSE0 GUDL1 GUDL0 MODE3 MODE2 MODE1 MODE0 */
169 0x03, 0x23, /* PH7113_ANALOG_INPUT_CONTR_2 - (1) HLNRS VBSL WPOFF HOLDG GAFIX GAI28 GAI18 */
170 0x04, 0x00, /* PH7113_ANALOG_INPUT_CONTR_3 - GAI17 GAI16 GAI15 GAI14 GAI13 GAI12 GAI11 GAI10 */
171 0x05, 0x00, /* PH7113_ANALOG_INPUT_CONTR_4 - GAI27 GAI26 GAI25 GAI24 GAI23 GAI22 GAI21 GAI20 */
172 0x06, 0xeb, /* PH7113_HORIZONTAL_SYNC_START - HSB7 HSB6 HSB5 HSB4 HSB3 HSB2 HSB1 HSB0 */
173 0x07, 0xe0, /* PH7113_HORIZONTAL_SYNC_STOP - HSS7 HSS6 HSS5 HSS4 HSS3 HSS2 HSS1 HSS0 */
174 0x08, 0x88, /* PH7113_SYNC_CONTROL - AUFD FSEL FOET HTC1 HTC0 HPLL VNOI1 VNOI0 */
175 0x09, 0x00, /* PH7113_LUMINANCE_CONTROL - BYPS PREF BPSS1 BPSS0 VBLB UPTCV APER1 APER0 */
176 0x0a, 0x80, /* PH7113_LUMINANCE_BRIGHTNESS - BRIG7 BRIG6 BRIG5 BRIG4 BRIG3 BRIG2 BRIG1 BRIG0 */
177 0x0b, 0x47, /* PH7113_LUMINANCE_CONTRAST - CONT7 CONT6 CONT5 CONT4 CONT3 CONT2 CONT1 CONT0 */
178 0x0c, 0x40, /* PH7113_CHROMA_SATURATION - SATN7 SATN6 SATN5 SATN4 SATN3 SATN2 SATN1 SATN0 */
179 0x0d, 0x00, /* PH7113_CHROMA_HUE_CONTROL - HUEC7 HUEC6 HUEC5 HUEC4 HUEC3 HUEC2 HUEC1 HUEC0 */
180 0x0e, 0x01, /* PH7113_CHROMA_CONTROL - CDTO CSTD2 CSTD1 CSTD0 DCCF FCTC CHBW1 CHBW0 */
181 0x0f, 0xaa, /* PH7113_CHROMA_GAIN_CONTROL - ACGC CGAIN6 CGAIN5 CGAIN4 CGAIN3 CGAIN2 CGAIN1 CGAIN0 */
182 0x10, 0x00, /* PH7113_FORMAT_DELAY_CONTROL - OFTS1 OFTS0 HDEL1 HDEL0 VRLN YDEL2 YDEL1 YDEL0 */
183 0x11, 0x1C, /* PH7113_OUTPUT_CONTROL_1 - GPSW1 CM99 GPSW0 HLSEL OEYC OERT VIPB COLO */
184 0x12, 0x01, /* PH7113_OUTPUT_CONTROL_2 - RTSE13 RTSE12 RTSE11 RTSE10 RTSE03 RTSE02 RTSE01 RTSE00 */
185 0x13, 0x00, /* PH7113_OUTPUT_CONTROL_3 - ADLSB (1) (1) OLDSB FIDP (1) AOSL1 AOSL0 */
186 0x14, 0x00, /* RESERVED 14 - (1) (1) (1) (1) (1) (1) (1) (1) */
187 0x15, 0x00, /* PH7113_V_GATE1_START - VSTA7 VSTA6 VSTA5 VSTA4 VSTA3 VSTA2 VSTA1 VSTA0 */
188 0x16, 0x00, /* PH7113_V_GATE1_STOP - VSTO7 VSTO6 VSTO5 VSTO4 VSTO3 VSTO2 VSTO1 VSTO0 */
189 0x17, 0x00, /* PH7113_V_GATE1_MSB - (1) (1) (1) (1) (1) (1) VSTO8 VSTA8 */
190};
191
192static int
193saa7113_command (struct i2c_client *client,
194 unsigned int cmd,
195 void *arg)
196{
197 struct saa7113 *decoder = i2c_get_clientdata(client);
198
199 switch (cmd) {
200
201 case 0:
202 case DECODER_INIT:
203 {
204 struct video_decoder_init *init = arg;
205 if (NULL != init)
206 return saa7113_init_decoder(client, init);
207 else {
208 struct video_decoder_init vdi;
209 vdi.data = saa7113_i2c_init;
210 vdi.len = sizeof(saa7113_i2c_init);
211 return saa7113_init_decoder(client, &vdi);
212 }
213 }
214
215 case DECODER_DUMP:
216 {
217 int i;
218
219 for (i = 0; i < 32; i += 16) {
220 int j;
221
222 printk(KERN_DEBUG "%s: %03x", I2C_NAME(client), i);
223 for (j = 0; j < 16; ++j) {
224 printk(" %02x",
225 saa7113_read(client, i + j));
226 }
227 printk("\n");
228 }
229 }
230 break;
231
232 case DECODER_GET_CAPABILITIES:
233 {
234 struct video_decoder_capability *cap = arg;
235
236 cap->flags = VIDEO_DECODER_PAL |
237 VIDEO_DECODER_NTSC |
238 VIDEO_DECODER_SECAM |
239 VIDEO_DECODER_AUTO |
240 VIDEO_DECODER_CCIR;
241 cap->inputs = 8;
242 cap->outputs = 1;
243 }
244 break;
245
246 case DECODER_GET_STATUS:
247 {
248 int *iarg = arg;
249 int status;
250 int res;
251
252 status = saa7113_read(client, 0x1f);
253 dprintk(1, KERN_DEBUG "%s status: 0x%02x\n", I2C_NAME(client),
254 status);
255 res = 0;
256 if ((status & (1 << 6)) == 0) {
257 res |= DECODER_STATUS_GOOD;
258 }
259 switch (decoder->norm) {
260 case VIDEO_MODE_NTSC:
261 res |= DECODER_STATUS_NTSC;
262 break;
263 case VIDEO_MODE_PAL:
264 res |= DECODER_STATUS_PAL;
265 break;
266 case VIDEO_MODE_SECAM:
267 res |= DECODER_STATUS_SECAM;
268 break;
269 default:
270 case VIDEO_MODE_AUTO:
271 if ((status & (1 << 5)) != 0) {
272 res |= DECODER_STATUS_NTSC;
273 } else {
274 res |= DECODER_STATUS_PAL;
275 }
276 break;
277 }
278 if ((status & (1 << 0)) != 0) {
279 res |= DECODER_STATUS_COLOR;
280 }
281 *iarg = res;
282 }
283 break;
284
285 case DECODER_SET_GPIO:
286 {
287 int *iarg = arg;
288 if (0 != *iarg) {
289 saa7113_write(client, 0x11,
290 (decoder->reg[0x11] | 0x80));
291 } else {
292 saa7113_write(client, 0x11,
293 (decoder->reg[0x11] & 0x7f));
294 }
295 break;
296 }
297
298 case DECODER_SET_VBI_BYPASS:
299 {
300 int *iarg = arg;
301 if (0 != *iarg) {
302 saa7113_write(client, 0x13,
303 (decoder->reg[0x13] & 0xf0) | 0x0a);
304 } else {
305 saa7113_write(client, 0x13,
306 (decoder->reg[0x13] & 0xf0));
307 }
308 break;
309 }
310
311 case DECODER_SET_NORM:
312 {
313 int *iarg = arg;
314
315 switch (*iarg) {
316
317 case VIDEO_MODE_NTSC:
318 saa7113_write(client, 0x08,
319 (decoder->reg[0x08] & 0x3f) | 0x40);
320 saa7113_write(client, 0x0e,
321 (decoder->reg[0x0e] & 0x8f));
322 break;
323
324 case VIDEO_MODE_PAL:
325 saa7113_write(client, 0x08,
326 (decoder->reg[0x08] & 0x3f) | 0x00);
327 saa7113_write(client, 0x0e,
328 (decoder->reg[0x0e] & 0x8f));
329 break;
330
331 case VIDEO_MODE_SECAM:
332 saa7113_write(client, 0x08,
333 (decoder->reg[0x0e] & 0x3f) | 0x00);
334 saa7113_write(client, 0x0e,
335 (decoder->reg[0x0e] & 0x8f) | 0x50);
336 break;
337
338 case VIDEO_MODE_AUTO:
339 saa7113_write(client, 0x08,
340 (decoder->reg[0x08] & 0x3f) | 0x80);
341 saa7113_write(client, 0x0e,
342 (decoder->reg[0x0e] & 0x8f));
343 break;
344
345 default:
346 return -EINVAL;
347
348 }
349 decoder->norm = *iarg;
350 }
351 break;
352
353 case DECODER_SET_INPUT:
354 {
355 int *iarg = arg;
356 if (*iarg < 0 || *iarg > 9) {
357 return -EINVAL;
358 }
359 if (decoder->input != *iarg) {
360 decoder->input = *iarg;
361 /* select mode */
362 saa7113_write(client, 0x02,
363 (decoder->reg[0x02] & 0xf0) | decoder->input);
364 /* bypass chrominance trap for modes 4..7 */
365 saa7113_write(client, 0x09,
366 (decoder->reg[0x09] & 0x7f) | ((decoder->input > 3) ? 0x80 : 0));
367 }
368 }
369 break;
370
371 case DECODER_SET_OUTPUT:
372 {
373 int *iarg = arg;
374
375 /* not much choice of outputs */
376 if (*iarg != 0) {
377 return -EINVAL;
378 }
379 }
380 break;
381
382 case DECODER_ENABLE_OUTPUT:
383 {
384 int *iarg = arg;
385 int enable = (*iarg != 0);
386
387 if (decoder->enable != enable) {
388 decoder->enable = enable;
389
390 /* RJ: If output should be disabled (for
391 * playing videos), we also need a open PLL.
392 * The input is set to 0 (where no input
393 * source is connected), although this
394 * is not necessary.
395 *
396 * If output should be enabled, we have to
397 * reverse the above.
398 */
399
400 if (decoder->enable) {
401 saa7113_write(client, 0x02,
402 (decoder->
403 reg[0x02] & 0xf8) |
404 decoder->input);
405 saa7113_write(client, 0x08,
406 (decoder->reg[0x08] & 0xfb));
407 saa7113_write(client, 0x11,
408 (decoder->
409 reg[0x11] & 0xf3) | 0x0c);
410 } else {
411 saa7113_write(client, 0x02,
412 (decoder->reg[0x02] & 0xf8));
413 saa7113_write(client, 0x08,
414 (decoder->
415 reg[0x08] & 0xfb) | 0x04);
416 saa7113_write(client, 0x11,
417 (decoder->reg[0x11] & 0xf3));
418 }
419 }
420 }
421 break;
422
423 case DECODER_SET_PICTURE:
424 {
425 struct video_picture *pic = arg;
426
427 if (decoder->bright != pic->brightness) {
428 /* We want 0 to 255 we get 0-65535 */
429 decoder->bright = pic->brightness;
430 saa7113_write(client, 0x0a, decoder->bright >> 8);
431 }
432 if (decoder->contrast != pic->contrast) {
433 /* We want 0 to 127 we get 0-65535 */
434 decoder->contrast = pic->contrast;
435 saa7113_write(client, 0x0b,
436 decoder->contrast >> 9);
437 }
438 if (decoder->sat != pic->colour) {
439 /* We want 0 to 127 we get 0-65535 */
440 decoder->sat = pic->colour;
441 saa7113_write(client, 0x0c, decoder->sat >> 9);
442 }
443 if (decoder->hue != pic->hue) {
444 /* We want -128 to 127 we get 0-65535 */
445 decoder->hue = pic->hue;
446 saa7113_write(client, 0x0d,
447 (decoder->hue - 32768) >> 8);
448 }
449 }
450 break;
451
452 default:
453 return -EINVAL;
454 }
455
456 return 0;
457}
458
459/* ----------------------------------------------------------------------- */
460
461/*
462 * Generic i2c probe
463 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
464 */
465
466/* standard i2c insmod options */
467static unsigned short normal_i2c[] = {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800468 I2C_SAA7113>>1, /* saa7113 */
Markus Rechberger791b4032005-11-08 21:38:08 -0800469 I2C_SAA7114>>1, /* saa7114 */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800470 I2C_CLIENT_END
Dwaine Garden340622c2005-11-08 21:37:34 -0800471};
472
473I2C_CLIENT_INSMOD;
474
475
476static struct i2c_driver i2c_driver_saa7113;
477
478static int
479saa7113_detect_client (struct i2c_adapter *adapter,
480 int address,
481 int kind)
482{
483 int i;
484 struct i2c_client *client;
485 struct saa7113 *decoder;
486 struct video_decoder_init vdi;
487
488 dprintk(1,
489 KERN_INFO
490 "saa7113.c: detecting saa7113 client on address 0x%x\n",
491 address << 1);
492
493 /* Check if the adapter supports the needed features */
494 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
495 return 0;
496
497 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
498 if (client == 0)
499 return -ENOMEM;
500 memset(client, 0, sizeof(struct i2c_client));
501 client->addr = address;
502 client->adapter = adapter;
503 client->driver = &i2c_driver_saa7113;
504 client->flags = I2C_CLIENT_ALLOW_USE;
505 strlcpy(I2C_NAME(client), "saa7113", sizeof(I2C_NAME(client)));
506 decoder = kmalloc(sizeof(struct saa7113), GFP_KERNEL);
507 if (decoder == NULL) {
508 kfree(client);
509 return -ENOMEM;
510 }
511 memset(decoder, 0, sizeof(struct saa7113));
512 decoder->norm = VIDEO_MODE_NTSC;
513 decoder->input = 0;
514 decoder->enable = 1;
515 decoder->bright = 32768;
516 decoder->contrast = 32768;
517 decoder->hue = 32768;
518 decoder->sat = 32768;
519 i2c_set_clientdata(client, decoder);
520
521 i = i2c_attach_client(client);
522 if (i) {
523 kfree(client);
524 kfree(decoder);
525 return i;
526 }
527
528 vdi.data = saa7113_i2c_init;
529 vdi.len = sizeof(saa7113_i2c_init);
530 i = saa7113_init_decoder(client, &vdi);
531 if (i < 0) {
532 dprintk(1, KERN_ERR "%s_attach error: init status %d\n",
533 I2C_NAME(client), i);
534 } else {
535 dprintk(1,
536 KERN_INFO
537 "%s_attach: chip version %x at address 0x%x\n",
538 I2C_NAME(client), saa7113_read(client, 0x00) >> 4,
539 client->addr << 1);
540 }
541
542 return 0;
543}
544
545static int
546saa7113_attach_adapter (struct i2c_adapter *adapter)
547{
548 dprintk(1,
549 KERN_INFO
550 "saa7113.c: starting probe for adapter %s (0x%x)\n",
551 I2C_NAME(adapter), adapter->id);
552 return i2c_probe(adapter, &addr_data, &saa7113_detect_client);
553}
554
555static int
556saa7113_detach_client (struct i2c_client *client)
557{
558 struct saa7113 *decoder = i2c_get_clientdata(client);
559 int err;
560
561 err = i2c_detach_client(client);
562 if (err) {
563 return err;
564 }
565
566 kfree(decoder);
567 kfree(client);
568
569 return 0;
570}
571
572/* ----------------------------------------------------------------------- */
573
574static struct i2c_driver i2c_driver_saa7113 = {
575 .owner = THIS_MODULE,
576 .name = "saa7113",
577
578 .id = I2C_DRIVERID_SAA7113,
579 .flags = I2C_DF_NOTIFY,
580
581 .attach_adapter = saa7113_attach_adapter,
582 .detach_client = saa7113_detach_client,
583 .command = saa7113_command,
584};
585
586static int __init
587saa7113_init (void)
588{
589 return i2c_add_driver(&i2c_driver_saa7113);
590}
591
592static void __exit
593saa7113_exit (void)
594{
595 i2c_del_driver(&i2c_driver_saa7113);
596}
597
598module_init(saa7113_init);
599module_exit(saa7113_exit);