blob: d18bf9097dc9785b37bf62350ab175b39470caf7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * adv7170 - adv7170, adv7171 video encoder driver version 0.0.1
3 *
4 * Copyright (C) 2002 Maxim Yevtyushkin <max@linuxmedialabs.com>
5 *
6 * Based on adv7176 driver by:
7 *
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.
12 *
13 * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
14 * - moved over to linux>=2.4.x i2c protocol (1/1/2003)
15 *
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.
20 *
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.
25 *
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.
29 */
30
31#include <linux/module.h>
32#include <linux/init.h>
33#include <linux/delay.h>
34#include <linux/errno.h>
35#include <linux/fs.h>
36#include <linux/kernel.h>
37#include <linux/major.h>
38#include <linux/slab.h>
39#include <linux/mm.h>
40#include <linux/pci.h>
41#include <linux/signal.h>
42#include <asm/io.h>
43#include <asm/pgtable.h>
44#include <asm/page.h>
45#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/types.h>
47
48#include <linux/videodev.h>
49#include <asm/uaccess.h>
50
51MODULE_DESCRIPTION("Analog Devices ADV7170 video encoder driver");
52MODULE_AUTHOR("Maxim Yevtyushkin");
53MODULE_LICENSE("GPL");
54
55#include <linux/i2c.h>
56#include <linux/i2c-dev.h>
57
58#define I2C_NAME(x) (x)->name
59
60#include <linux/video_encoder.h>
61
62static int debug = 0;
63module_param(debug, int, 0);
64MODULE_PARM_DESC(debug, "Debug level (0-1)");
65
66#define dprintk(num, format, args...) \
67 do { \
68 if (debug >= num) \
69 printk(format, ##args); \
70 } while (0)
71
72/* ----------------------------------------------------------------------- */
73
74struct adv7170 {
75 unsigned char reg[128];
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_ADV7170 0xd4
87#define I2C_ADV7171 0x54
88
89static char adv7170_name[] = "adv7170";
90static char adv7171_name[] = "adv7171";
91
92static char *inputs[] = { "pass_through", "play_back" };
93static char *norms[] = { "PAL", "NTSC" };
94
95/* ----------------------------------------------------------------------- */
96
97static inline int
98adv7170_write (struct i2c_client *client,
99 u8 reg,
100 u8 value)
101{
102 struct adv7170 *encoder = i2c_get_clientdata(client);
103
104 encoder->reg[reg] = value;
105 return i2c_smbus_write_byte_data(client, reg, value);
106}
107
108static inline int
109adv7170_read (struct i2c_client *client,
110 u8 reg)
111{
112 return i2c_smbus_read_byte_data(client, reg);
113}
114
115static int
116adv7170_write_block (struct i2c_client *client,
117 const u8 *data,
118 unsigned int len)
119{
120 int ret = -1;
121 u8 reg;
122
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 u8 block_data[32];
Jean Delvare9aa45e32006-03-22 03:48:35 -0300129 int block_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 while (len >= 2) {
Jean Delvare9aa45e32006-03-22 03:48:35 -0300132 block_len = 0;
133 block_data[block_len++] = reg = data[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 do {
Jean Delvare9aa45e32006-03-22 03:48:35 -0300135 block_data[block_len++] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 encoder->reg[reg++] = data[1];
137 len -= 2;
138 data += 2;
139 } while (len >= 2 && data[0] == reg &&
Jean Delvare9aa45e32006-03-22 03:48:35 -0300140 block_len < 32);
141 if ((ret = i2c_master_send(client, block_data,
142 block_len)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 break;
144 }
145 } else {
146 /* do some slow I2C emulation kind of thing */
147 while (len >= 2) {
148 reg = *data++;
149 if ((ret = adv7170_write(client, reg,
150 *data++)) < 0)
151 break;
152 len -= 2;
153 }
154 }
155
156 return ret;
157}
158
159/* ----------------------------------------------------------------------- */
160// Output filter: S-Video Composite
161
162#define MR050 0x11 //0x09
163#define MR060 0x14 //0x0c
164
165//---------------------------------------------------------------------------
166
167#define TR0MODE 0x4c
168#define TR0RST 0x80
169
170#define TR1CAPT 0x00
171#define TR1PLAY 0x00
172
173
174static const unsigned char init_NTSC[] = {
175 0x00, 0x10, // MR0
176 0x01, 0x20, // MR1
177 0x02, 0x0e, // MR2 RTC control: bits 2 and 1
178 0x03, 0x80, // MR3
179 0x04, 0x30, // MR4
180 0x05, 0x00, // Reserved
181 0x06, 0x00, // Reserved
182 0x07, TR0MODE, // TM0
183 0x08, TR1CAPT, // TM1
184 0x09, 0x16, // Fsc0
185 0x0a, 0x7c, // Fsc1
186 0x0b, 0xf0, // Fsc2
187 0x0c, 0x21, // Fsc3
188 0x0d, 0x00, // Subcarrier Phase
189 0x0e, 0x00, // Closed Capt. Ext 0
190 0x0f, 0x00, // Closed Capt. Ext 1
191 0x10, 0x00, // Closed Capt. 0
192 0x11, 0x00, // Closed Capt. 1
193 0x12, 0x00, // Pedestal Ctl 0
194 0x13, 0x00, // Pedestal Ctl 1
195 0x14, 0x00, // Pedestal Ctl 2
196 0x15, 0x00, // Pedestal Ctl 3
197 0x16, 0x00, // CGMS_WSS_0
198 0x17, 0x00, // CGMS_WSS_1
199 0x18, 0x00, // CGMS_WSS_2
200 0x19, 0x00, // Teletext Ctl
201};
202
203static const unsigned char init_PAL[] = {
204 0x00, 0x71, // MR0
205 0x01, 0x20, // MR1
206 0x02, 0x0e, // MR2 RTC control: bits 2 and 1
207 0x03, 0x80, // MR3
208 0x04, 0x30, // MR4
209 0x05, 0x00, // Reserved
210 0x06, 0x00, // Reserved
211 0x07, TR0MODE, // TM0
212 0x08, TR1CAPT, // TM1
213 0x09, 0xcb, // Fsc0
214 0x0a, 0x8a, // Fsc1
215 0x0b, 0x09, // Fsc2
216 0x0c, 0x2a, // Fsc3
217 0x0d, 0x00, // Subcarrier Phase
218 0x0e, 0x00, // Closed Capt. Ext 0
219 0x0f, 0x00, // Closed Capt. Ext 1
220 0x10, 0x00, // Closed Capt. 0
221 0x11, 0x00, // Closed Capt. 1
222 0x12, 0x00, // Pedestal Ctl 0
223 0x13, 0x00, // Pedestal Ctl 1
224 0x14, 0x00, // Pedestal Ctl 2
225 0x15, 0x00, // Pedestal Ctl 3
226 0x16, 0x00, // CGMS_WSS_0
227 0x17, 0x00, // CGMS_WSS_1
228 0x18, 0x00, // CGMS_WSS_2
229 0x19, 0x00, // Teletext Ctl
230};
231
232
233static int
234adv7170_command (struct i2c_client *client,
235 unsigned int cmd,
236 void * arg)
237{
238 struct adv7170 *encoder = i2c_get_clientdata(client);
239
240 switch (cmd) {
241
242 case 0:
243#if 0
244 /* This is just for testing!!! */
245 adv7170_write_block(client, init_common,
246 sizeof(init_common));
247 adv7170_write(client, 0x07, TR0MODE | TR0RST);
248 adv7170_write(client, 0x07, TR0MODE);
249#endif
250 break;
251
252 case ENCODER_GET_CAPABILITIES:
253 {
254 struct video_encoder_capability *cap = arg;
255
256 cap->flags = VIDEO_ENCODER_PAL |
257 VIDEO_ENCODER_NTSC;
258 cap->inputs = 2;
259 cap->outputs = 1;
260 }
261 break;
262
263 case ENCODER_SET_NORM:
264 {
265 int iarg = *(int *) arg;
266
267 dprintk(1, KERN_DEBUG "%s_command: set norm %d",
268 I2C_NAME(client), iarg);
269
270 switch (iarg) {
271
272 case VIDEO_MODE_NTSC:
273 adv7170_write_block(client, init_NTSC,
274 sizeof(init_NTSC));
275 if (encoder->input == 0)
276 adv7170_write(client, 0x02, 0x0e); // Enable genlock
277 adv7170_write(client, 0x07, TR0MODE | TR0RST);
278 adv7170_write(client, 0x07, TR0MODE);
279 break;
280
281 case VIDEO_MODE_PAL:
282 adv7170_write_block(client, init_PAL,
283 sizeof(init_PAL));
284 if (encoder->input == 0)
285 adv7170_write(client, 0x02, 0x0e); // Enable genlock
286 adv7170_write(client, 0x07, TR0MODE | TR0RST);
287 adv7170_write(client, 0x07, TR0MODE);
288 break;
289
290 default:
291 dprintk(1, KERN_ERR "%s: illegal norm: %d\n",
292 I2C_NAME(client), iarg);
293 return -EINVAL;
294
295 }
296 dprintk(1, KERN_DEBUG "%s: switched to %s\n", I2C_NAME(client),
297 norms[iarg]);
298 encoder->norm = iarg;
299 }
300 break;
301
302 case ENCODER_SET_INPUT:
303 {
304 int iarg = *(int *) arg;
305
306 /* RJ: *iarg = 0: input is from decoder
307 *iarg = 1: input is from ZR36060
308 *iarg = 2: color bar */
309
310 dprintk(1, KERN_DEBUG "%s_command: set input from %s\n",
311 I2C_NAME(client),
312 iarg == 0 ? "decoder" : "ZR36060");
313
314 switch (iarg) {
315
316 case 0:
317 adv7170_write(client, 0x01, 0x20);
318 adv7170_write(client, 0x08, TR1CAPT); /* TR1 */
319 adv7170_write(client, 0x02, 0x0e); // Enable genlock
320 adv7170_write(client, 0x07, TR0MODE | TR0RST);
321 adv7170_write(client, 0x07, TR0MODE);
322 //udelay(10);
323 break;
324
325 case 1:
326 adv7170_write(client, 0x01, 0x00);
327 adv7170_write(client, 0x08, TR1PLAY); /* TR1 */
328 adv7170_write(client, 0x02, 0x08);
329 adv7170_write(client, 0x07, TR0MODE | TR0RST);
330 adv7170_write(client, 0x07, TR0MODE);
331 //udelay(10);
332 break;
333
334 default:
335 dprintk(1, KERN_ERR "%s: illegal input: %d\n",
336 I2C_NAME(client), iarg);
337 return -EINVAL;
338
339 }
340 dprintk(1, KERN_DEBUG "%s: switched to %s\n", I2C_NAME(client),
341 inputs[iarg]);
342 encoder->input = iarg;
343 }
344 break;
345
346 case ENCODER_SET_OUTPUT:
347 {
348 int *iarg = arg;
349
350 /* not much choice of outputs */
351 if (*iarg != 0) {
352 return -EINVAL;
353 }
354 }
355 break;
356
357 case ENCODER_ENABLE_OUTPUT:
358 {
359 int *iarg = arg;
360
361 encoder->enable = !!*iarg;
362 }
363 break;
364
365 default:
366 return -EINVAL;
367 }
368
369 return 0;
370}
371
372/* ----------------------------------------------------------------------- */
373
374/*
375 * Generic i2c probe
376 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
377 */
378static unsigned short normal_i2c[] =
379 { I2C_ADV7170 >> 1, (I2C_ADV7170 >> 1) + 1,
380 I2C_ADV7171 >> 1, (I2C_ADV7171 >> 1) + 1,
381 I2C_CLIENT_END
382};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Jean Delvare68cc9d02005-04-02 20:04:41 +0200384static unsigned short ignore = I2C_CLIENT_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386static struct i2c_client_address_data addr_data = {
387 .normal_i2c = normal_i2c,
Jean Delvare68cc9d02005-04-02 20:04:41 +0200388 .probe = &ignore,
389 .ignore = &ignore,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390};
391
392static struct i2c_driver i2c_driver_adv7170;
393
394static int
395adv7170_detect_client (struct i2c_adapter *adapter,
396 int address,
397 int kind)
398{
399 int i;
400 struct i2c_client *client;
401 struct adv7170 *encoder;
402 char *dname;
403
404 dprintk(1,
405 KERN_INFO
406 "adv7170.c: detecting adv7170 client on address 0x%x\n",
407 address << 1);
408
409 /* Check if the adapter supports the needed features */
410 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
411 return 0;
412
Panagiotis Issaris74081872006-01-11 19:40:56 -0200413 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (client == 0)
415 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 client->addr = address;
417 client->adapter = adapter;
418 client->driver = &i2c_driver_adv7170;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if ((client->addr == I2C_ADV7170 >> 1) ||
420 (client->addr == (I2C_ADV7170 >> 1) + 1)) {
421 dname = adv7170_name;
422 } else if ((client->addr == I2C_ADV7171 >> 1) ||
423 (client->addr == (I2C_ADV7171 >> 1) + 1)) {
424 dname = adv7171_name;
425 } else {
426 /* We should never get here!!! */
427 kfree(client);
428 return 0;
429 }
430 strlcpy(I2C_NAME(client), dname, sizeof(I2C_NAME(client)));
431
Panagiotis Issaris74081872006-01-11 19:40:56 -0200432 encoder = kzalloc(sizeof(struct adv7170), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (encoder == NULL) {
434 kfree(client);
435 return -ENOMEM;
436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 encoder->norm = VIDEO_MODE_NTSC;
438 encoder->input = 0;
439 encoder->enable = 1;
440 i2c_set_clientdata(client, encoder);
441
442 i = i2c_attach_client(client);
443 if (i) {
444 kfree(client);
445 kfree(encoder);
446 return i;
447 }
448
449 i = adv7170_write_block(client, init_NTSC, sizeof(init_NTSC));
450 if (i >= 0) {
451 i = adv7170_write(client, 0x07, TR0MODE | TR0RST);
452 i = adv7170_write(client, 0x07, TR0MODE);
453 i = adv7170_read(client, 0x12);
454 dprintk(1, KERN_INFO "%s_attach: rev. %d at 0x%02x\n",
455 I2C_NAME(client), i & 1, client->addr << 1);
456 }
457 if (i < 0) {
458 dprintk(1, KERN_ERR "%s_attach: init error 0x%x\n",
459 I2C_NAME(client), i);
460 }
461
462 return 0;
463}
464
465static int
466adv7170_attach_adapter (struct i2c_adapter *adapter)
467{
468 dprintk(1,
469 KERN_INFO
470 "adv7170.c: starting probe for adapter %s (0x%x)\n",
471 I2C_NAME(adapter), adapter->id);
472 return i2c_probe(adapter, &addr_data, &adv7170_detect_client);
473}
474
475static int
476adv7170_detach_client (struct i2c_client *client)
477{
478 struct adv7170 *encoder = i2c_get_clientdata(client);
479 int err;
480
481 err = i2c_detach_client(client);
482 if (err) {
483 return err;
484 }
485
486 kfree(encoder);
487 kfree(client);
488
489 return 0;
490}
491
492/* ----------------------------------------------------------------------- */
493
494static struct i2c_driver i2c_driver_adv7170 = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100495 .driver = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100496 .name = "adv7170", /* name */
497 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 .id = I2C_DRIVERID_ADV7170,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 .attach_adapter = adv7170_attach_adapter,
502 .detach_client = adv7170_detach_client,
503 .command = adv7170_command,
504};
505
506static int __init
507adv7170_init (void)
508{
509 return i2c_add_driver(&i2c_driver_adv7170);
510}
511
512static void __exit
513adv7170_exit (void)
514{
515 i2c_del_driver(&i2c_driver_adv7170);
516}
517
518module_init(adv7170_init);
519module_exit(adv7170_exit);