2 * saa7111 - Philips SAA7111A 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/types.h>
32 #include <linux/ioctl.h>
33 #include <asm/uaccess.h>
34 #include <linux/i2c.h>
35 #include <linux/i2c-id.h>
36 #include <linux/videodev.h>
37 #include <linux/video_decoder.h>
38 #include <media/v4l2-common.h>
39 #include <media/v4l2-i2c-drv-legacy.h>
41 MODULE_DESCRIPTION("Philips SAA7111 video decoder driver");
42 MODULE_AUTHOR("Dave Perks");
43 MODULE_LICENSE("GPL");
46 module_param(debug, int, 0644);
47 MODULE_PARM_DESC(debug, "Debug level (0-1)");
49 /* ----------------------------------------------------------------------- */
51 #define SAA7111_NR_REG 0x18
54 unsigned char reg[SAA7111_NR_REG];
61 /* ----------------------------------------------------------------------- */
63 static inline int saa7111_write(struct i2c_client *client, u8 reg, u8 value)
65 struct saa7111 *decoder = i2c_get_clientdata(client);
67 decoder->reg[reg] = value;
68 return i2c_smbus_write_byte_data(client, reg, value);
71 static inline void saa7111_write_if_changed(struct i2c_client *client, u8 reg, u8 value)
73 struct saa7111 *decoder = i2c_get_clientdata(client);
75 if (decoder->reg[reg] != value) {
76 decoder->reg[reg] = value;
77 i2c_smbus_write_byte_data(client, reg, value);
81 static int saa7111_write_block(struct i2c_client *client, const u8 *data, unsigned int len)
86 /* the saa7111 has an autoincrement function, use it if
87 * the adapter understands raw I2C */
88 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
89 /* do raw I2C, not smbus compatible */
90 struct saa7111 *decoder = i2c_get_clientdata(client);
96 block_data[block_len++] = reg = data[0];
98 block_data[block_len++] =
99 decoder->reg[reg++] = data[1];
102 } while (len >= 2 && data[0] == reg && block_len < 32);
103 ret = i2c_master_send(client, block_data, block_len);
108 /* do some slow I2C emulation kind of thing */
111 ret = saa7111_write(client, reg, *data++);
121 static int saa7111_init_decoder(struct i2c_client *client,
122 struct video_decoder_init *init)
124 return saa7111_write_block(client, init->data, init->len);
127 static inline int saa7111_read(struct i2c_client *client, u8 reg)
129 return i2c_smbus_read_byte_data(client, reg);
132 /* ----------------------------------------------------------------------- */
134 static const unsigned char saa7111_i2c_init[] = {
135 0x00, 0x00, /* 00 - ID byte */
136 0x01, 0x00, /* 01 - reserved */
139 0x02, 0xd0, /* 02 - FUSE=3, GUDL=2, MODE=0 */
140 0x03, 0x23, /* 03 - HLNRS=0, VBSL=1, WPOFF=0,
141 * HOLDG=0, GAFIX=0, GAI1=256, GAI2=256 */
142 0x04, 0x00, /* 04 - GAI1=256 */
143 0x05, 0x00, /* 05 - GAI2=256 */
146 0x06, 0xf3, /* 06 - HSB at 13(50Hz) / 17(60Hz)
147 * pixels after end of last line */
148 /*0x07, 0x13, * 07 - HSS at 113(50Hz) / 117(60Hz) pixels
149 * after end of last line */
150 0x07, 0xe8, /* 07 - HSS seems to be needed to
151 * work with NTSC, too */
152 0x08, 0xc8, /* 08 - AUFD=1, FSEL=1, EXFIL=0,
153 * VTRC=1, HPLL=0, VNOI=0 */
154 0x09, 0x01, /* 09 - BYPS=0, PREF=0, BPSS=0,
155 * VBLB=0, UPTCV=0, APER=1 */
156 0x0a, 0x80, /* 0a - BRIG=128 */
157 0x0b, 0x47, /* 0b - CONT=1.109 */
158 0x0c, 0x40, /* 0c - SATN=1.0 */
159 0x0d, 0x00, /* 0d - HUE=0 */
160 0x0e, 0x01, /* 0e - CDTO=0, CSTD=0, DCCF=0,
162 0x0f, 0x00, /* 0f - reserved */
163 0x10, 0x48, /* 10 - OFTS=1, HDEL=0, VRLN=1, YDEL=0 */
164 0x11, 0x1c, /* 11 - GPSW=0, CM99=0, FECO=0, COMPO=1,
165 * OEYC=1, OEHV=1, VIPB=0, COLO=0 */
166 0x12, 0x00, /* 12 - output control 2 */
167 0x13, 0x00, /* 13 - output control 3 */
168 0x14, 0x00, /* 14 - reserved */
169 0x15, 0x00, /* 15 - VBI */
170 0x16, 0x00, /* 16 - VBI */
171 0x17, 0x00, /* 17 - VBI */
174 static int saa7111_command(struct i2c_client *client, unsigned cmd, void *arg)
176 struct saa7111 *decoder = i2c_get_clientdata(client);
183 struct video_decoder_init *init = arg;
184 struct video_decoder_init vdi;
187 return saa7111_init_decoder(client, init);
188 vdi.data = saa7111_i2c_init;
189 vdi.len = sizeof(saa7111_i2c_init);
190 return saa7111_init_decoder(client, &vdi);
197 for (i = 0; i < SAA7111_NR_REG; i += 16) {
200 v4l_info(client, "%03x", i);
201 for (j = 0; j < 16 && i + j < SAA7111_NR_REG; ++j) {
202 printk(KERN_CONT " %02x",
203 saa7111_read(client, i + j));
205 printk(KERN_CONT "\n");
210 case DECODER_GET_CAPABILITIES:
212 struct video_decoder_capability *cap = arg;
214 cap->flags = VIDEO_DECODER_PAL |
216 VIDEO_DECODER_SECAM |
224 case DECODER_GET_STATUS:
230 status = saa7111_read(client, 0x1f);
231 v4l_dbg(1, debug, client, "status: 0x%02x\n", status);
233 if ((status & (1 << 6)) == 0) {
234 res |= DECODER_STATUS_GOOD;
236 switch (decoder->norm) {
237 case VIDEO_MODE_NTSC:
238 res |= DECODER_STATUS_NTSC;
241 res |= DECODER_STATUS_PAL;
243 case VIDEO_MODE_SECAM:
244 res |= DECODER_STATUS_SECAM;
247 case VIDEO_MODE_AUTO:
248 if ((status & (1 << 5)) != 0) {
249 res |= DECODER_STATUS_NTSC;
251 res |= DECODER_STATUS_PAL;
255 if ((status & (1 << 0)) != 0) {
256 res |= DECODER_STATUS_COLOR;
262 case DECODER_SET_GPIO:
266 saa7111_write(client, 0x11,
267 (decoder->reg[0x11] | 0x80));
269 saa7111_write(client, 0x11,
270 (decoder->reg[0x11] & 0x7f));
275 case DECODER_SET_VBI_BYPASS:
279 saa7111_write(client, 0x13,
280 (decoder->reg[0x13] & 0xf0) | 0x0a);
282 saa7111_write(client, 0x13,
283 (decoder->reg[0x13] & 0xf0));
288 case DECODER_SET_NORM:
294 case VIDEO_MODE_NTSC:
295 saa7111_write(client, 0x08,
296 (decoder->reg[0x08] & 0x3f) | 0x40);
297 saa7111_write(client, 0x0e,
298 (decoder->reg[0x0e] & 0x8f));
302 saa7111_write(client, 0x08,
303 (decoder->reg[0x08] & 0x3f) | 0x00);
304 saa7111_write(client, 0x0e,
305 (decoder->reg[0x0e] & 0x8f));
308 case VIDEO_MODE_SECAM:
309 saa7111_write(client, 0x08,
310 (decoder->reg[0x08] & 0x3f) | 0x00);
311 saa7111_write(client, 0x0e,
312 (decoder->reg[0x0e] & 0x8f) | 0x50);
315 case VIDEO_MODE_AUTO:
316 saa7111_write(client, 0x08,
317 (decoder->reg[0x08] & 0x3f) | 0x80);
318 saa7111_write(client, 0x0e,
319 (decoder->reg[0x0e] & 0x8f));
326 decoder->norm = *iarg;
330 case DECODER_SET_INPUT:
334 if (*iarg < 0 || *iarg > 7) {
338 if (decoder->input != *iarg) {
339 decoder->input = *iarg;
341 saa7111_write(client, 0x02,
343 reg[0x02] & 0xf8) | decoder->input);
344 /* bypass chrominance trap for modes 4..7 */
345 saa7111_write(client, 0x09,
347 reg[0x09] & 0x7f) | ((decoder->
355 case DECODER_SET_OUTPUT:
359 /* not much choice of outputs */
366 case DECODER_ENABLE_OUTPUT:
369 int enable = (*iarg != 0);
371 if (decoder->enable != enable) {
372 decoder->enable = enable;
374 /* RJ: If output should be disabled (for
375 * playing videos), we also need a open PLL.
376 * The input is set to 0 (where no input
377 * source is connected), although this
380 * If output should be enabled, we have to
384 if (decoder->enable) {
385 saa7111_write(client, 0x02,
389 saa7111_write(client, 0x08,
390 (decoder->reg[0x08] & 0xfb));
391 saa7111_write(client, 0x11,
393 reg[0x11] & 0xf3) | 0x0c);
395 saa7111_write(client, 0x02,
396 (decoder->reg[0x02] & 0xf8));
397 saa7111_write(client, 0x08,
399 reg[0x08] & 0xfb) | 0x04);
400 saa7111_write(client, 0x11,
401 (decoder->reg[0x11] & 0xf3));
407 case DECODER_SET_PICTURE:
409 struct video_picture *pic = arg;
411 /* We want 0 to 255 we get 0-65535 */
412 saa7111_write_if_changed(client, 0x0a, pic->brightness >> 8);
413 /* We want 0 to 127 we get 0-65535 */
414 saa7111_write(client, 0x0b, pic->contrast >> 9);
415 /* We want 0 to 127 we get 0-65535 */
416 saa7111_write(client, 0x0c, pic->colour >> 9);
417 /* We want -128 to 127 we get 0-65535 */
418 saa7111_write(client, 0x0d, (pic->hue - 32768) >> 8);
429 /* ----------------------------------------------------------------------- */
431 static unsigned short normal_i2c[] = { 0x48 >> 1, I2C_CLIENT_END };
435 static int saa7111_probe(struct i2c_client *client,
436 const struct i2c_device_id *id)
439 struct saa7111 *decoder;
440 struct video_decoder_init vdi;
442 /* Check if the adapter supports the needed features */
443 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
446 v4l_info(client, "chip found @ 0x%x (%s)\n",
447 client->addr << 1, client->adapter->name);
449 decoder = kzalloc(sizeof(struct saa7111), GFP_KERNEL);
450 if (decoder == NULL) {
454 decoder->norm = VIDEO_MODE_NTSC;
457 i2c_set_clientdata(client, decoder);
459 vdi.data = saa7111_i2c_init;
460 vdi.len = sizeof(saa7111_i2c_init);
461 i = saa7111_init_decoder(client, &vdi);
463 v4l_dbg(1, debug, client, "init status %d\n", i);
465 v4l_dbg(1, debug, client, "revision %x\n",
466 saa7111_read(client, 0x00) >> 4);
471 static int saa7111_remove(struct i2c_client *client)
473 kfree(i2c_get_clientdata(client));
477 /* ----------------------------------------------------------------------- */
479 static const struct i2c_device_id saa7111_id[] = {
480 { "saa7111_old", 0 }, /* "saa7111" maps to the saa7115 driver */
483 MODULE_DEVICE_TABLE(i2c, saa7111_id);
485 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
487 .driverid = I2C_DRIVERID_SAA7111A,
488 .command = saa7111_command,
489 .probe = saa7111_probe,
490 .remove = saa7111_remove,
491 .id_table = saa7111_id,