2 * adv7175 - adv7175a video encoder driver version 0.0.3
4 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
5 * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
6 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
7 * - some corrections for Pinnacle Systems Inc. DC10plus card.
9 * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
10 * - moved over to linux>=2.4.x i2c protocol (9/9/2002)
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/delay.h>
30 #include <linux/errno.h>
32 #include <linux/kernel.h>
33 #include <linux/major.h>
34 #include <linux/slab.h>
36 #include <linux/pci.h>
37 #include <linux/signal.h>
39 #include <asm/pgtable.h>
41 #include <linux/sched.h>
42 #include <asm/segment.h>
43 #include <linux/types.h>
45 #include <linux/videodev.h>
46 #include <asm/uaccess.h>
48 MODULE_DESCRIPTION("Analog Devices ADV7175 video encoder driver");
49 MODULE_AUTHOR("Dave Perks");
50 MODULE_LICENSE("GPL");
52 #include <linux/i2c.h>
53 #include <linux/i2c-dev.h>
55 #define I2C_NAME(s) (s)->name
57 #include <linux/video_encoder.h>
60 module_param(debug, int, 0);
61 MODULE_PARM_DESC(debug, "Debug level (0-1)");
63 #define dprintk(num, format, args...) \
66 printk(format, ##args); \
69 /* ----------------------------------------------------------------------- */
72 unsigned char reg[128];
83 #define I2C_ADV7175 0xd4
84 #define I2C_ADV7176 0x54
86 static char adv7175_name[] = "adv7175";
87 static char adv7176_name[] = "adv7176";
89 static char *inputs[] = { "pass_through", "play_back", "color_bar" };
90 static char *norms[] = { "PAL", "NTSC", "SECAM->PAL (may not work!)" };
92 /* ----------------------------------------------------------------------- */
95 adv7175_write (struct i2c_client *client,
99 struct adv7175 *encoder = i2c_get_clientdata(client);
101 encoder->reg[reg] = value;
102 return i2c_smbus_write_byte_data(client, reg, value);
106 adv7175_read (struct i2c_client *client,
109 return i2c_smbus_read_byte_data(client, reg);
113 adv7175_write_block (struct i2c_client *client,
120 /* the adv7175 has an autoincrement function, use it if
121 * the adapter understands raw I2C */
122 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
123 /* do raw I2C, not smbus compatible */
124 struct adv7175 *encoder = i2c_get_clientdata(client);
128 msg.addr = client->addr;
131 msg.buf = (char *) block_data;
133 block_data[msg.len++] = reg = data[0];
135 block_data[msg.len++] =
136 encoder->reg[reg++] = data[1];
139 } while (len >= 2 && data[0] == reg &&
141 if ((ret = i2c_transfer(client->adapter,
146 /* do some slow I2C emulation kind of thing */
149 if ((ret = adv7175_write(client, reg,
160 set_subcarrier_freq (struct i2c_client *client,
163 /* for some reason pass_through NTSC needs
164 * a different sub-carrier freq to remain stable. */
166 adv7175_write(client, 0x02, 0x00);
168 adv7175_write(client, 0x02, 0x55);
170 adv7175_write(client, 0x03, 0x55);
171 adv7175_write(client, 0x04, 0x55);
172 adv7175_write(client, 0x05, 0x25);
177 dump (struct i2c_client *client)
179 struct adv7175 *encoder = i2c_get_clientdata(client);
182 printk(KERN_INFO "%s: registry dump\n", I2C_NAME(client));
183 for (i = 0; i < 182 / 8; i++) {
184 printk("%s: 0x%02x -", I2C_NAME(client), i * 8);
185 for (j = 0; j < 8; j++) {
186 printk(" 0x%02x", encoder->reg[i * 8 + j]);
193 /* ----------------------------------------------------------------------- */
194 // Output filter: S-Video Composite
196 #define MR050 0x11 //0x09
197 #define MR060 0x14 //0x0c
199 //---------------------------------------------------------------------------
207 static const unsigned char init_common[] = {
209 0x00, MR050, /* MR0, PAL enabled */
210 0x01, 0x00, /* MR1 */
211 0x02, 0x0c, /* subc. freq. */
212 0x03, 0x8c, /* subc. freq. */
213 0x04, 0x79, /* subc. freq. */
214 0x05, 0x26, /* subc. freq. */
215 0x06, 0x40, /* subc. phase */
217 0x07, TR0MODE, /* TR0, 16bit */
222 0x0c, TR1CAPT, /* TR1 */
223 0x0d, 0x4f, /* MR2 */
230 static const unsigned char init_pal[] = {
231 0x00, MR050, /* MR0, PAL enabled */
232 0x01, 0x00, /* MR1 */
233 0x02, 0x0c, /* subc. freq. */
234 0x03, 0x8c, /* subc. freq. */
235 0x04, 0x79, /* subc. freq. */
236 0x05, 0x26, /* subc. freq. */
237 0x06, 0x40, /* subc. phase */
240 static const unsigned char init_ntsc[] = {
241 0x00, MR060, /* MR0, NTSC enabled */
242 0x01, 0x00, /* MR1 */
243 0x02, 0x55, /* subc. freq. */
244 0x03, 0x55, /* subc. freq. */
245 0x04, 0x55, /* subc. freq. */
246 0x05, 0x25, /* subc. freq. */
247 0x06, 0x1a, /* subc. phase */
251 adv7175_command (struct i2c_client *client,
255 struct adv7175 *encoder = i2c_get_clientdata(client);
260 /* This is just for testing!!! */
261 adv7175_write_block(client, init_common,
262 sizeof(init_common));
263 adv7175_write(client, 0x07, TR0MODE | TR0RST);
264 adv7175_write(client, 0x07, TR0MODE);
267 case ENCODER_GET_CAPABILITIES:
269 struct video_encoder_capability *cap = arg;
271 cap->flags = VIDEO_ENCODER_PAL |
273 VIDEO_ENCODER_SECAM; /* well, hacky */
279 case ENCODER_SET_NORM:
281 int iarg = *(int *) arg;
285 case VIDEO_MODE_NTSC:
286 adv7175_write_block(client, init_ntsc,
288 if (encoder->input == 0)
289 adv7175_write(client, 0x0d, 0x4f); // Enable genlock
290 adv7175_write(client, 0x07, TR0MODE | TR0RST);
291 adv7175_write(client, 0x07, TR0MODE);
295 adv7175_write_block(client, init_pal,
297 if (encoder->input == 0)
298 adv7175_write(client, 0x0d, 0x4f); // Enable genlock
299 adv7175_write(client, 0x07, TR0MODE | TR0RST);
300 adv7175_write(client, 0x07, TR0MODE);
303 case VIDEO_MODE_SECAM: // WARNING! ADV7176 does not support SECAM.
304 /* This is an attempt to convert
305 * SECAM->PAL (typically it does not work
306 * due to genlock: when decoder is in SECAM
307 * and encoder in in PAL the subcarrier can
308 * not be syncronized with horizontal
310 adv7175_write_block(client, init_pal,
312 if (encoder->input == 0)
313 adv7175_write(client, 0x0d, 0x49); // Disable genlock
314 adv7175_write(client, 0x07, TR0MODE | TR0RST);
315 adv7175_write(client, 0x07, TR0MODE);
318 dprintk(1, KERN_ERR "%s: illegal norm: %d\n",
319 I2C_NAME(client), iarg);
323 dprintk(1, KERN_INFO "%s: switched to %s\n", I2C_NAME(client),
325 encoder->norm = iarg;
329 case ENCODER_SET_INPUT:
331 int iarg = *(int *) arg;
333 /* RJ: *iarg = 0: input is from SAA7110
334 *iarg = 1: input is from ZR36060
335 *iarg = 2: color bar */
340 adv7175_write(client, 0x01, 0x00);
342 if (encoder->norm == VIDEO_MODE_NTSC)
343 set_subcarrier_freq(client, 1);
345 adv7175_write(client, 0x0c, TR1CAPT); /* TR1 */
346 if (encoder->norm == VIDEO_MODE_SECAM)
347 adv7175_write(client, 0x0d, 0x49); // Disable genlock
349 adv7175_write(client, 0x0d, 0x4f); // Enable genlock
350 adv7175_write(client, 0x07, TR0MODE | TR0RST);
351 adv7175_write(client, 0x07, TR0MODE);
356 adv7175_write(client, 0x01, 0x00);
358 if (encoder->norm == VIDEO_MODE_NTSC)
359 set_subcarrier_freq(client, 0);
361 adv7175_write(client, 0x0c, TR1PLAY); /* TR1 */
362 adv7175_write(client, 0x0d, 0x49);
363 adv7175_write(client, 0x07, TR0MODE | TR0RST);
364 adv7175_write(client, 0x07, TR0MODE);
369 adv7175_write(client, 0x01, 0x80);
371 if (encoder->norm == VIDEO_MODE_NTSC)
372 set_subcarrier_freq(client, 0);
374 adv7175_write(client, 0x0d, 0x49);
375 adv7175_write(client, 0x07, TR0MODE | TR0RST);
376 adv7175_write(client, 0x07, TR0MODE);
381 dprintk(1, KERN_ERR "%s: illegal input: %d\n",
382 I2C_NAME(client), iarg);
386 dprintk(1, KERN_INFO "%s: switched to %s\n", I2C_NAME(client),
388 encoder->input = iarg;
392 case ENCODER_SET_OUTPUT:
396 /* not much choice of outputs */
403 case ENCODER_ENABLE_OUTPUT:
407 encoder->enable = !!*iarg;
426 /* ----------------------------------------------------------------------- */
430 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
432 static unsigned short normal_i2c[] =
433 { I2C_ADV7175 >> 1, (I2C_ADV7175 >> 1) + 1,
434 I2C_ADV7176 >> 1, (I2C_ADV7176 >> 1) + 1,
438 static unsigned short ignore = I2C_CLIENT_END;
440 static struct i2c_client_address_data addr_data = {
441 .normal_i2c = normal_i2c,
447 static struct i2c_driver i2c_driver_adv7175;
450 adv7175_detect_client (struct i2c_adapter *adapter,
455 struct i2c_client *client;
456 struct adv7175 *encoder;
461 "adv7175.c: detecting adv7175 client on address 0x%x\n",
464 /* Check if the adapter supports the needed features */
465 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
468 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
471 memset(client, 0, sizeof(struct i2c_client));
472 client->addr = address;
473 client->adapter = adapter;
474 client->driver = &i2c_driver_adv7175;
475 client->flags = I2C_CLIENT_ALLOW_USE;
476 if ((client->addr == I2C_ADV7175 >> 1) ||
477 (client->addr == (I2C_ADV7175 >> 1) + 1)) {
478 dname = adv7175_name;
479 } else if ((client->addr == I2C_ADV7176 >> 1) ||
480 (client->addr == (I2C_ADV7176 >> 1) + 1)) {
481 dname = adv7176_name;
483 /* We should never get here!!! */
487 strlcpy(I2C_NAME(client), dname, sizeof(I2C_NAME(client)));
489 encoder = kmalloc(sizeof(struct adv7175), GFP_KERNEL);
490 if (encoder == NULL) {
494 memset(encoder, 0, sizeof(struct adv7175));
495 encoder->norm = VIDEO_MODE_PAL;
498 i2c_set_clientdata(client, encoder);
500 i = i2c_attach_client(client);
507 i = adv7175_write_block(client, init_common, sizeof(init_common));
509 i = adv7175_write(client, 0x07, TR0MODE | TR0RST);
510 i = adv7175_write(client, 0x07, TR0MODE);
511 i = adv7175_read(client, 0x12);
512 dprintk(1, KERN_INFO "%s_attach: rev. %d at 0x%x\n",
513 I2C_NAME(client), i & 1, client->addr << 1);
516 dprintk(1, KERN_ERR "%s_attach: init error 0x%x\n",
517 I2C_NAME(client), i);
524 adv7175_attach_adapter (struct i2c_adapter *adapter)
528 "adv7175.c: starting probe for adapter %s (0x%x)\n",
529 I2C_NAME(adapter), adapter->id);
530 return i2c_probe(adapter, &addr_data, &adv7175_detect_client);
534 adv7175_detach_client (struct i2c_client *client)
536 struct adv7175 *encoder = i2c_get_clientdata(client);
539 err = i2c_detach_client(client);
550 /* ----------------------------------------------------------------------- */
552 static struct i2c_driver i2c_driver_adv7175 = {
553 .owner = THIS_MODULE,
554 .name = "adv7175", /* name */
556 .id = I2C_DRIVERID_ADV7175,
557 .flags = I2C_DF_NOTIFY,
559 .attach_adapter = adv7175_attach_adapter,
560 .detach_client = adv7175_detach_client,
561 .command = adv7175_command,
567 return i2c_add_driver(&i2c_driver_adv7175);
573 i2c_del_driver(&i2c_driver_adv7175);
576 module_init(adv7175_init);
577 module_exit(adv7175_exit);