2 * adv7170 - adv7170, adv7171 video encoder driver version 0.0.1
4 * Copyright (C) 2002 Maxim Yevtyushkin <max@linuxmedialabs.com>
6 * Based on adv7176 driver by:
8 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
9 * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
10 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
11 * - some corrections for Pinnacle Systems Inc. DC10plus card.
13 * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
14 * - moved over to linux>=2.4.x i2c protocol (1/1/2003)
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/delay.h>
34 #include <linux/errno.h>
36 #include <linux/kernel.h>
37 #include <linux/major.h>
38 #include <linux/slab.h>
40 #include <linux/pci.h>
41 #include <linux/signal.h>
43 #include <asm/pgtable.h>
45 #include <linux/sched.h>
46 #include <linux/types.h>
48 #include <linux/videodev.h>
49 #include <asm/uaccess.h>
51 MODULE_DESCRIPTION("Analog Devices ADV7170 video encoder driver");
52 MODULE_AUTHOR("Maxim Yevtyushkin");
53 MODULE_LICENSE("GPL");
55 #include <linux/i2c.h>
56 #include <linux/i2c-dev.h>
58 #define I2C_NAME(x) (x)->name
60 #include <linux/video_encoder.h>
63 module_param(debug, int, 0);
64 MODULE_PARM_DESC(debug, "Debug level (0-1)");
66 #define dprintk(num, format, args...) \
69 printk(format, ##args); \
72 /* ----------------------------------------------------------------------- */
75 unsigned char reg[128];
86 #define I2C_ADV7170 0xd4
87 #define I2C_ADV7171 0x54
89 static char adv7170_name[] = "adv7170";
90 static char adv7171_name[] = "adv7171";
92 static char *inputs[] = { "pass_through", "play_back" };
93 static char *norms[] = { "PAL", "NTSC" };
95 /* ----------------------------------------------------------------------- */
98 adv7170_write (struct i2c_client *client,
102 struct adv7170 *encoder = i2c_get_clientdata(client);
104 encoder->reg[reg] = value;
105 return i2c_smbus_write_byte_data(client, reg, value);
109 adv7170_read (struct i2c_client *client,
112 return i2c_smbus_read_byte_data(client, reg);
116 adv7170_write_block (struct i2c_client *client,
123 /* the adv7170 has an autoincrement function, use it if
124 * the adapter understands raw I2C */
125 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
126 /* do raw I2C, not smbus compatible */
127 struct adv7170 *encoder = i2c_get_clientdata(client);
131 msg.addr = client->addr;
134 msg.buf = (char *) block_data;
136 block_data[msg.len++] = reg = data[0];
138 block_data[msg.len++] =
139 encoder->reg[reg++] = data[1];
142 } while (len >= 2 && data[0] == reg &&
144 if ((ret = i2c_transfer(client->adapter,
149 /* do some slow I2C emulation kind of thing */
152 if ((ret = adv7170_write(client, reg,
162 /* ----------------------------------------------------------------------- */
163 // Output filter: S-Video Composite
165 #define MR050 0x11 //0x09
166 #define MR060 0x14 //0x0c
168 //---------------------------------------------------------------------------
177 static const unsigned char init_NTSC[] = {
180 0x02, 0x0e, // MR2 RTC control: bits 2 and 1
183 0x05, 0x00, // Reserved
184 0x06, 0x00, // Reserved
185 0x07, TR0MODE, // TM0
186 0x08, TR1CAPT, // TM1
191 0x0d, 0x00, // Subcarrier Phase
192 0x0e, 0x00, // Closed Capt. Ext 0
193 0x0f, 0x00, // Closed Capt. Ext 1
194 0x10, 0x00, // Closed Capt. 0
195 0x11, 0x00, // Closed Capt. 1
196 0x12, 0x00, // Pedestal Ctl 0
197 0x13, 0x00, // Pedestal Ctl 1
198 0x14, 0x00, // Pedestal Ctl 2
199 0x15, 0x00, // Pedestal Ctl 3
200 0x16, 0x00, // CGMS_WSS_0
201 0x17, 0x00, // CGMS_WSS_1
202 0x18, 0x00, // CGMS_WSS_2
203 0x19, 0x00, // Teletext Ctl
206 static const unsigned char init_PAL[] = {
209 0x02, 0x0e, // MR2 RTC control: bits 2 and 1
212 0x05, 0x00, // Reserved
213 0x06, 0x00, // Reserved
214 0x07, TR0MODE, // TM0
215 0x08, TR1CAPT, // TM1
220 0x0d, 0x00, // Subcarrier Phase
221 0x0e, 0x00, // Closed Capt. Ext 0
222 0x0f, 0x00, // Closed Capt. Ext 1
223 0x10, 0x00, // Closed Capt. 0
224 0x11, 0x00, // Closed Capt. 1
225 0x12, 0x00, // Pedestal Ctl 0
226 0x13, 0x00, // Pedestal Ctl 1
227 0x14, 0x00, // Pedestal Ctl 2
228 0x15, 0x00, // Pedestal Ctl 3
229 0x16, 0x00, // CGMS_WSS_0
230 0x17, 0x00, // CGMS_WSS_1
231 0x18, 0x00, // CGMS_WSS_2
232 0x19, 0x00, // Teletext Ctl
237 adv7170_command (struct i2c_client *client,
241 struct adv7170 *encoder = i2c_get_clientdata(client);
247 /* This is just for testing!!! */
248 adv7170_write_block(client, init_common,
249 sizeof(init_common));
250 adv7170_write(client, 0x07, TR0MODE | TR0RST);
251 adv7170_write(client, 0x07, TR0MODE);
255 case ENCODER_GET_CAPABILITIES:
257 struct video_encoder_capability *cap = arg;
259 cap->flags = VIDEO_ENCODER_PAL |
266 case ENCODER_SET_NORM:
268 int iarg = *(int *) arg;
270 dprintk(1, KERN_DEBUG "%s_command: set norm %d",
271 I2C_NAME(client), iarg);
275 case VIDEO_MODE_NTSC:
276 adv7170_write_block(client, init_NTSC,
278 if (encoder->input == 0)
279 adv7170_write(client, 0x02, 0x0e); // Enable genlock
280 adv7170_write(client, 0x07, TR0MODE | TR0RST);
281 adv7170_write(client, 0x07, TR0MODE);
285 adv7170_write_block(client, init_PAL,
287 if (encoder->input == 0)
288 adv7170_write(client, 0x02, 0x0e); // Enable genlock
289 adv7170_write(client, 0x07, TR0MODE | TR0RST);
290 adv7170_write(client, 0x07, TR0MODE);
294 dprintk(1, KERN_ERR "%s: illegal norm: %d\n",
295 I2C_NAME(client), iarg);
299 dprintk(1, KERN_DEBUG "%s: switched to %s\n", I2C_NAME(client),
301 encoder->norm = iarg;
305 case ENCODER_SET_INPUT:
307 int iarg = *(int *) arg;
309 /* RJ: *iarg = 0: input is from decoder
310 *iarg = 1: input is from ZR36060
311 *iarg = 2: color bar */
313 dprintk(1, KERN_DEBUG "%s_command: set input from %s\n",
315 iarg == 0 ? "decoder" : "ZR36060");
320 adv7170_write(client, 0x01, 0x20);
321 adv7170_write(client, 0x08, TR1CAPT); /* TR1 */
322 adv7170_write(client, 0x02, 0x0e); // Enable genlock
323 adv7170_write(client, 0x07, TR0MODE | TR0RST);
324 adv7170_write(client, 0x07, TR0MODE);
329 adv7170_write(client, 0x01, 0x00);
330 adv7170_write(client, 0x08, TR1PLAY); /* TR1 */
331 adv7170_write(client, 0x02, 0x08);
332 adv7170_write(client, 0x07, TR0MODE | TR0RST);
333 adv7170_write(client, 0x07, TR0MODE);
338 dprintk(1, KERN_ERR "%s: illegal input: %d\n",
339 I2C_NAME(client), iarg);
343 dprintk(1, KERN_DEBUG "%s: switched to %s\n", I2C_NAME(client),
345 encoder->input = iarg;
349 case ENCODER_SET_OUTPUT:
353 /* not much choice of outputs */
360 case ENCODER_ENABLE_OUTPUT:
364 encoder->enable = !!*iarg;
375 /* ----------------------------------------------------------------------- */
379 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
381 static unsigned short normal_i2c[] =
382 { I2C_ADV7170 >> 1, (I2C_ADV7170 >> 1) + 1,
383 I2C_ADV7171 >> 1, (I2C_ADV7171 >> 1) + 1,
387 static unsigned short ignore = I2C_CLIENT_END;
389 static struct i2c_client_address_data addr_data = {
390 .normal_i2c = normal_i2c,
395 static struct i2c_driver i2c_driver_adv7170;
398 adv7170_detect_client (struct i2c_adapter *adapter,
403 struct i2c_client *client;
404 struct adv7170 *encoder;
409 "adv7170.c: detecting adv7170 client on address 0x%x\n",
412 /* Check if the adapter supports the needed features */
413 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
416 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
419 memset(client, 0, sizeof(struct i2c_client));
420 client->addr = address;
421 client->adapter = adapter;
422 client->driver = &i2c_driver_adv7170;
423 client->flags = I2C_CLIENT_ALLOW_USE;
424 if ((client->addr == I2C_ADV7170 >> 1) ||
425 (client->addr == (I2C_ADV7170 >> 1) + 1)) {
426 dname = adv7170_name;
427 } else if ((client->addr == I2C_ADV7171 >> 1) ||
428 (client->addr == (I2C_ADV7171 >> 1) + 1)) {
429 dname = adv7171_name;
431 /* We should never get here!!! */
435 strlcpy(I2C_NAME(client), dname, sizeof(I2C_NAME(client)));
437 encoder = kmalloc(sizeof(struct adv7170), GFP_KERNEL);
438 if (encoder == NULL) {
442 memset(encoder, 0, sizeof(struct adv7170));
443 encoder->norm = VIDEO_MODE_NTSC;
446 i2c_set_clientdata(client, encoder);
448 i = i2c_attach_client(client);
455 i = adv7170_write_block(client, init_NTSC, sizeof(init_NTSC));
457 i = adv7170_write(client, 0x07, TR0MODE | TR0RST);
458 i = adv7170_write(client, 0x07, TR0MODE);
459 i = adv7170_read(client, 0x12);
460 dprintk(1, KERN_INFO "%s_attach: rev. %d at 0x%02x\n",
461 I2C_NAME(client), i & 1, client->addr << 1);
464 dprintk(1, KERN_ERR "%s_attach: init error 0x%x\n",
465 I2C_NAME(client), i);
472 adv7170_attach_adapter (struct i2c_adapter *adapter)
476 "adv7170.c: starting probe for adapter %s (0x%x)\n",
477 I2C_NAME(adapter), adapter->id);
478 return i2c_probe(adapter, &addr_data, &adv7170_detect_client);
482 adv7170_detach_client (struct i2c_client *client)
484 struct adv7170 *encoder = i2c_get_clientdata(client);
487 err = i2c_detach_client(client);
498 /* ----------------------------------------------------------------------- */
500 static struct i2c_driver i2c_driver_adv7170 = {
501 .owner = THIS_MODULE,
502 .name = "adv7170", /* name */
504 .id = I2C_DRIVERID_ADV7170,
505 .flags = I2C_DF_NOTIFY,
507 .attach_adapter = adv7170_attach_adapter,
508 .detach_client = adv7170_detach_client,
509 .command = adv7170_command,
515 return i2c_add_driver(&i2c_driver_adv7170);
521 i2c_del_driver(&i2c_driver_adv7170);
524 module_init(adv7170_init);
525 module_exit(adv7170_exit);