2 * saa7111 - Philips SAA7113A video decoder driver version 0.0.3
4 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
6 * Slight changes for video timing and attachment output by
7 * Wolfgang Scherr <scherr@net4you.net>
9 * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
10 * - moved over to linux>=2.4.x i2c protocol (1/1/2003)
12 * Changes by Michael Hunold <michael@mihu.de>
13 * - implemented DECODER_SET_GPIO, DECODER_INIT, DECODER_SET_VBI_BYPASS
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.
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.
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.
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
33 #include <linux/errno.h>
35 #include <linux/kernel.h>
36 #include <linux/major.h>
37 #include <linux/slab.h>
39 #include <linux/pci.h>
40 #include <linux/signal.h>
42 #include <asm/pgtable.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>
50 MODULE_DESCRIPTION("Philips SAA7113 video decoder driver");
51 MODULE_AUTHOR("Dave Perks, Jose Ignacio Gijon, Joerg Heckenbach, Mark McClelland, Dwaine Garden");
52 MODULE_LICENSE("GPL");
54 #include <linux/i2c.h>
55 #include <linux/i2c-dev.h>
57 #define I2C_NAME(s) (s)->name
59 #include <linux/video_decoder.h>
62 MODULE_PARM(debug, "i");
63 MODULE_PARM_DESC(debug, " Set the default Debug level. Default: 0 (Off) - (0-1)");
66 #define dprintk(num, format, args...) \
69 printk(format , ##args); \
72 /* ----------------------------------------------------------------------- */
75 unsigned char reg[32];
86 #define I2C_SAA7113 0x4A
87 #define I2C_SAA7114 0x42
89 /* ----------------------------------------------------------------------- */
92 saa7113_write (struct i2c_client *client,
96 struct saa7113 *decoder = i2c_get_clientdata(client);
98 decoder->reg[reg] = value;
99 return i2c_smbus_write_byte_data(client, reg, value);
103 saa7113_write_block (struct i2c_client *client,
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);
118 msg.addr = client->addr;
121 msg.buf = (char *) block_data;
123 block_data[msg.len++] = reg = data[0];
125 block_data[msg.len++] =
126 decoder->reg[reg++] = data[1];
129 } while (len >= 2 && data[0] == reg &&
131 if ((ret = i2c_transfer(client->adapter,
136 /* do some slow I2C emulation kind of thing */
139 if ((ret = saa7113_write(client, reg,
150 saa7113_init_decoder (struct i2c_client *client,
151 struct video_decoder_init *init)
153 return saa7113_write_block(client, init->data, init->len);
157 saa7113_read (struct i2c_client *client,
160 return i2c_smbus_read_byte_data(client, reg);
163 /* ----------------------------------------------------------------------- */
165 static 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 */
193 saa7113_command (struct i2c_client *client,
197 struct saa7113 *decoder = i2c_get_clientdata(client);
204 struct video_decoder_init *init = arg;
206 return saa7113_init_decoder(client, init);
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);
219 for (i = 0; i < 32; i += 16) {
222 printk(KERN_DEBUG "%s: %03x", I2C_NAME(client), i);
223 for (j = 0; j < 16; ++j) {
225 saa7113_read(client, i + j));
232 case DECODER_GET_CAPABILITIES:
234 struct video_decoder_capability *cap = arg;
236 cap->flags = VIDEO_DECODER_PAL |
238 VIDEO_DECODER_SECAM |
246 case DECODER_GET_STATUS:
252 status = saa7113_read(client, 0x1f);
253 dprintk(1, KERN_DEBUG "%s status: 0x%02x\n", I2C_NAME(client),
256 if ((status & (1 << 6)) == 0) {
257 res |= DECODER_STATUS_GOOD;
259 switch (decoder->norm) {
260 case VIDEO_MODE_NTSC:
261 res |= DECODER_STATUS_NTSC;
264 res |= DECODER_STATUS_PAL;
266 case VIDEO_MODE_SECAM:
267 res |= DECODER_STATUS_SECAM;
270 case VIDEO_MODE_AUTO:
271 if ((status & (1 << 5)) != 0) {
272 res |= DECODER_STATUS_NTSC;
274 res |= DECODER_STATUS_PAL;
278 if ((status & (1 << 0)) != 0) {
279 res |= DECODER_STATUS_COLOR;
285 case DECODER_SET_GPIO:
289 saa7113_write(client, 0x11,
290 (decoder->reg[0x11] | 0x80));
292 saa7113_write(client, 0x11,
293 (decoder->reg[0x11] & 0x7f));
298 case DECODER_SET_VBI_BYPASS:
302 saa7113_write(client, 0x13,
303 (decoder->reg[0x13] & 0xf0) | 0x0a);
305 saa7113_write(client, 0x13,
306 (decoder->reg[0x13] & 0xf0));
311 case DECODER_SET_NORM:
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));
325 saa7113_write(client, 0x08,
326 (decoder->reg[0x08] & 0x3f) | 0x00);
327 saa7113_write(client, 0x0e,
328 (decoder->reg[0x0e] & 0x8f));
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);
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));
349 decoder->norm = *iarg;
353 case DECODER_SET_INPUT:
356 if (*iarg < 0 || *iarg > 9) {
359 if (decoder->input != *iarg) {
360 decoder->input = *iarg;
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));
371 case DECODER_SET_OUTPUT:
375 /* not much choice of outputs */
382 case DECODER_ENABLE_OUTPUT:
385 int enable = (*iarg != 0);
387 if (decoder->enable != enable) {
388 decoder->enable = enable;
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
396 * If output should be enabled, we have to
400 if (decoder->enable) {
401 saa7113_write(client, 0x02,
405 saa7113_write(client, 0x08,
406 (decoder->reg[0x08] & 0xfb));
407 saa7113_write(client, 0x11,
409 reg[0x11] & 0xf3) | 0x0c);
411 saa7113_write(client, 0x02,
412 (decoder->reg[0x02] & 0xf8));
413 saa7113_write(client, 0x08,
415 reg[0x08] & 0xfb) | 0x04);
416 saa7113_write(client, 0x11,
417 (decoder->reg[0x11] & 0xf3));
423 case DECODER_SET_PICTURE:
425 struct video_picture *pic = arg;
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);
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);
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);
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);
459 /* ----------------------------------------------------------------------- */
463 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
466 /* standard i2c insmod options */
467 static unsigned short normal_i2c[] = {
468 I2C_SAA7113>>1, /* saa7113 */
469 I2C_SAA7114>>1, /* saa7114 */
476 static struct i2c_driver i2c_driver_saa7113;
479 saa7113_detect_client (struct i2c_adapter *adapter,
484 struct i2c_client *client;
485 struct saa7113 *decoder;
486 struct video_decoder_init vdi;
490 "saa7113.c: detecting saa7113 client on address 0x%x\n",
493 /* Check if the adapter supports the needed features */
494 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
497 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
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) {
511 memset(decoder, 0, sizeof(struct saa7113));
512 decoder->norm = VIDEO_MODE_NTSC;
515 decoder->bright = 32768;
516 decoder->contrast = 32768;
517 decoder->hue = 32768;
518 decoder->sat = 32768;
519 i2c_set_clientdata(client, decoder);
521 i = i2c_attach_client(client);
528 vdi.data = saa7113_i2c_init;
529 vdi.len = sizeof(saa7113_i2c_init);
530 i = saa7113_init_decoder(client, &vdi);
532 dprintk(1, KERN_ERR "%s_attach error: init status %d\n",
533 I2C_NAME(client), i);
537 "%s_attach: chip version %x at address 0x%x\n",
538 I2C_NAME(client), saa7113_read(client, 0x00) >> 4,
546 saa7113_attach_adapter (struct i2c_adapter *adapter)
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);
556 saa7113_detach_client (struct i2c_client *client)
558 struct saa7113 *decoder = i2c_get_clientdata(client);
561 err = i2c_detach_client(client);
572 /* ----------------------------------------------------------------------- */
574 static struct i2c_driver i2c_driver_saa7113 = {
575 .owner = THIS_MODULE,
578 .id = I2C_DRIVERID_SAA7113,
579 .flags = I2C_DF_NOTIFY,
581 .attach_adapter = saa7113_attach_adapter,
582 .detach_client = saa7113_detach_client,
583 .command = saa7113_command,
589 return i2c_add_driver(&i2c_driver_saa7113);
595 i2c_del_driver(&i2c_driver_saa7113);
598 module_init(saa7113_init);
599 module_exit(saa7113_exit);