blob: 2d5fa44fcd4d7c49c5b57b2ade9f7a783cc9c305 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * adv7175 - adv7175a video encoder driver version 0.0.3
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.
8 *
9 * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
10 * - moved over to linux>=2.4.x i2c protocol (9/9/2002)
11 *
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.
16 *
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.
21 *
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.
25 */
26
27#include <linux/module.h>
28#include <linux/init.h>
29#include <linux/delay.h>
30#include <linux/errno.h>
31#include <linux/fs.h>
32#include <linux/kernel.h>
33#include <linux/major.h>
34#include <linux/slab.h>
35#include <linux/mm.h>
36#include <linux/pci.h>
37#include <linux/signal.h>
38#include <asm/io.h>
39#include <asm/pgtable.h>
40#include <asm/page.h>
41#include <linux/sched.h>
42#include <asm/segment.h>
43#include <linux/types.h>
44
45#include <linux/videodev.h>
46#include <asm/uaccess.h>
47
48MODULE_DESCRIPTION("Analog Devices ADV7175 video encoder driver");
49MODULE_AUTHOR("Dave Perks");
50MODULE_LICENSE("GPL");
51
52#include <linux/i2c.h>
53#include <linux/i2c-dev.h>
54
55#define I2C_NAME(s) (s)->name
56
57#include <linux/video_encoder.h>
58
59static int debug = 0;
60module_param(debug, int, 0);
61MODULE_PARM_DESC(debug, "Debug level (0-1)");
62
63#define dprintk(num, format, args...) \
64 do { \
65 if (debug >= num) \
66 printk(format, ##args); \
67 } while (0)
68
69/* ----------------------------------------------------------------------- */
70
71struct adv7175 {
72 unsigned char reg[128];
73
74 int norm;
75 int input;
76 int enable;
77 int bright;
78 int contrast;
79 int hue;
80 int sat;
81};
82
83#define I2C_ADV7175 0xd4
84#define I2C_ADV7176 0x54
85
86static char adv7175_name[] = "adv7175";
87static char adv7176_name[] = "adv7176";
88
89static char *inputs[] = { "pass_through", "play_back", "color_bar" };
90static char *norms[] = { "PAL", "NTSC", "SECAM->PAL (may not work!)" };
91
92/* ----------------------------------------------------------------------- */
93
94static inline int
95adv7175_write (struct i2c_client *client,
96 u8 reg,
97 u8 value)
98{
99 struct adv7175 *encoder = i2c_get_clientdata(client);
100
101 encoder->reg[reg] = value;
102 return i2c_smbus_write_byte_data(client, reg, value);
103}
104
105static inline int
106adv7175_read (struct i2c_client *client,
107 u8 reg)
108{
109 return i2c_smbus_read_byte_data(client, reg);
110}
111
112static int
113adv7175_write_block (struct i2c_client *client,
114 const u8 *data,
115 unsigned int len)
116{
117 int ret = -1;
118 u8 reg;
119
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);
125 struct i2c_msg msg;
126 u8 block_data[32];
127
128 msg.addr = client->addr;
129 msg.flags = 0;
130 while (len >= 2) {
131 msg.buf = (char *) block_data;
132 msg.len = 0;
133 block_data[msg.len++] = reg = data[0];
134 do {
135 block_data[msg.len++] =
136 encoder->reg[reg++] = data[1];
137 len -= 2;
138 data += 2;
139 } while (len >= 2 && data[0] == reg &&
140 msg.len < 32);
141 if ((ret = i2c_transfer(client->adapter,
142 &msg, 1)) < 0)
143 break;
144 }
145 } else {
146 /* do some slow I2C emulation kind of thing */
147 while (len >= 2) {
148 reg = *data++;
149 if ((ret = adv7175_write(client, reg,
150 *data++)) < 0)
151 break;
152 len -= 2;
153 }
154 }
155
156 return ret;
157}
158
159static void
160set_subcarrier_freq (struct i2c_client *client,
161 int pass_through)
162{
163 /* for some reason pass_through NTSC needs
164 * a different sub-carrier freq to remain stable. */
165 if(pass_through)
166 adv7175_write(client, 0x02, 0x00);
167 else
168 adv7175_write(client, 0x02, 0x55);
169
170 adv7175_write(client, 0x03, 0x55);
171 adv7175_write(client, 0x04, 0x55);
172 adv7175_write(client, 0x05, 0x25);
173}
174
175#ifdef ENCODER_DUMP
176static void
177dump (struct i2c_client *client)
178{
179 struct adv7175 *encoder = i2c_get_clientdata(client);
180 int i, j;
181
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]);
187 }
188 printk("\n");
189 }
190}
191#endif
192
193/* ----------------------------------------------------------------------- */
194// Output filter: S-Video Composite
195
196#define MR050 0x11 //0x09
197#define MR060 0x14 //0x0c
198
199//---------------------------------------------------------------------------
200
201#define TR0MODE 0x46
202#define TR0RST 0x80
203
204#define TR1CAPT 0x80
205#define TR1PLAY 0x00
206
207static const unsigned char init_common[] = {
208
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 */
216
217 0x07, TR0MODE, /* TR0, 16bit */
218 0x08, 0x21, /* */
219 0x09, 0x00, /* */
220 0x0a, 0x00, /* */
221 0x0b, 0x00, /* */
222 0x0c, TR1CAPT, /* TR1 */
223 0x0d, 0x4f, /* MR2 */
224 0x0e, 0x00, /* */
225 0x0f, 0x00, /* */
226 0x10, 0x00, /* */
227 0x11, 0x00, /* */
228};
229
230static 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 */
238};
239
240static 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 */
248};
249
250static int
251adv7175_command (struct i2c_client *client,
252 unsigned int cmd,
253 void *arg)
254{
255 struct adv7175 *encoder = i2c_get_clientdata(client);
256
257 switch (cmd) {
258
259 case 0:
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);
265 break;
266
267 case ENCODER_GET_CAPABILITIES:
268 {
269 struct video_encoder_capability *cap = arg;
270
271 cap->flags = VIDEO_ENCODER_PAL |
272 VIDEO_ENCODER_NTSC |
273 VIDEO_ENCODER_SECAM; /* well, hacky */
274 cap->inputs = 2;
275 cap->outputs = 1;
276 }
277 break;
278
279 case ENCODER_SET_NORM:
280 {
281 int iarg = *(int *) arg;
282
283 switch (iarg) {
284
285 case VIDEO_MODE_NTSC:
286 adv7175_write_block(client, init_ntsc,
287 sizeof(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);
292 break;
293
294 case VIDEO_MODE_PAL:
295 adv7175_write_block(client, init_pal,
296 sizeof(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);
301 break;
302
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
309 * quency) */
310 adv7175_write_block(client, init_pal,
311 sizeof(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);
316 break;
317 default:
318 dprintk(1, KERN_ERR "%s: illegal norm: %d\n",
319 I2C_NAME(client), iarg);
320 return -EINVAL;
321
322 }
323 dprintk(1, KERN_INFO "%s: switched to %s\n", I2C_NAME(client),
324 norms[iarg]);
325 encoder->norm = iarg;
326 }
327 break;
328
329 case ENCODER_SET_INPUT:
330 {
331 int iarg = *(int *) arg;
332
333 /* RJ: *iarg = 0: input is from SAA7110
334 *iarg = 1: input is from ZR36060
335 *iarg = 2: color bar */
336
337 switch (iarg) {
338
339 case 0:
340 adv7175_write(client, 0x01, 0x00);
341
342 if (encoder->norm == VIDEO_MODE_NTSC)
343 set_subcarrier_freq(client, 1);
344
345 adv7175_write(client, 0x0c, TR1CAPT); /* TR1 */
346 if (encoder->norm == VIDEO_MODE_SECAM)
347 adv7175_write(client, 0x0d, 0x49); // Disable genlock
348 else
349 adv7175_write(client, 0x0d, 0x4f); // Enable genlock
350 adv7175_write(client, 0x07, TR0MODE | TR0RST);
351 adv7175_write(client, 0x07, TR0MODE);
352 //udelay(10);
353 break;
354
355 case 1:
356 adv7175_write(client, 0x01, 0x00);
357
358 if (encoder->norm == VIDEO_MODE_NTSC)
359 set_subcarrier_freq(client, 0);
360
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);
365 //udelay(10);
366 break;
367
368 case 2:
369 adv7175_write(client, 0x01, 0x80);
370
371 if (encoder->norm == VIDEO_MODE_NTSC)
372 set_subcarrier_freq(client, 0);
373
374 adv7175_write(client, 0x0d, 0x49);
375 adv7175_write(client, 0x07, TR0MODE | TR0RST);
376 adv7175_write(client, 0x07, TR0MODE);
377 //udelay(10);
378 break;
379
380 default:
381 dprintk(1, KERN_ERR "%s: illegal input: %d\n",
382 I2C_NAME(client), iarg);
383 return -EINVAL;
384
385 }
386 dprintk(1, KERN_INFO "%s: switched to %s\n", I2C_NAME(client),
387 inputs[iarg]);
388 encoder->input = iarg;
389 }
390 break;
391
392 case ENCODER_SET_OUTPUT:
393 {
394 int *iarg = arg;
395
396 /* not much choice of outputs */
397 if (*iarg != 0) {
398 return -EINVAL;
399 }
400 }
401 break;
402
403 case ENCODER_ENABLE_OUTPUT:
404 {
405 int *iarg = arg;
406
407 encoder->enable = !!*iarg;
408 }
409 break;
410
411#ifdef ENCODER_DUMP
412 case ENCODER_DUMP:
413 {
414 dump(client);
415 }
416 break;
417#endif
418
419 default:
420 return -EINVAL;
421 }
422
423 return 0;
424}
425
426/* ----------------------------------------------------------------------- */
427
428/*
429 * Generic i2c probe
430 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
431 */
432static unsigned short normal_i2c[] =
433 { I2C_ADV7175 >> 1, (I2C_ADV7175 >> 1) + 1,
434 I2C_ADV7176 >> 1, (I2C_ADV7176 >> 1) + 1,
435 I2C_CLIENT_END
436};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438static unsigned short probe[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439static unsigned short ignore[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440static unsigned short force[2] = { I2C_CLIENT_END , I2C_CLIENT_END };
441
442static struct i2c_client_address_data addr_data = {
443 .normal_i2c = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 .probe = probe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 .ignore = ignore,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 .force = force
447};
448
449static struct i2c_driver i2c_driver_adv7175;
450
451static int
452adv7175_detect_client (struct i2c_adapter *adapter,
453 int address,
454 int kind)
455{
456 int i;
457 struct i2c_client *client;
458 struct adv7175 *encoder;
459 char *dname;
460
461 dprintk(1,
462 KERN_INFO
463 "adv7175.c: detecting adv7175 client on address 0x%x\n",
464 address << 1);
465
466 /* Check if the adapter supports the needed features */
467 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
468 return 0;
469
470 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
471 if (client == 0)
472 return -ENOMEM;
473 memset(client, 0, sizeof(struct i2c_client));
474 client->addr = address;
475 client->adapter = adapter;
476 client->driver = &i2c_driver_adv7175;
477 client->flags = I2C_CLIENT_ALLOW_USE;
478 if ((client->addr == I2C_ADV7175 >> 1) ||
479 (client->addr == (I2C_ADV7175 >> 1) + 1)) {
480 dname = adv7175_name;
481 } else if ((client->addr == I2C_ADV7176 >> 1) ||
482 (client->addr == (I2C_ADV7176 >> 1) + 1)) {
483 dname = adv7176_name;
484 } else {
485 /* We should never get here!!! */
486 kfree(client);
487 return 0;
488 }
489 strlcpy(I2C_NAME(client), dname, sizeof(I2C_NAME(client)));
490
491 encoder = kmalloc(sizeof(struct adv7175), GFP_KERNEL);
492 if (encoder == NULL) {
493 kfree(client);
494 return -ENOMEM;
495 }
496 memset(encoder, 0, sizeof(struct adv7175));
497 encoder->norm = VIDEO_MODE_PAL;
498 encoder->input = 0;
499 encoder->enable = 1;
500 i2c_set_clientdata(client, encoder);
501
502 i = i2c_attach_client(client);
503 if (i) {
504 kfree(client);
505 kfree(encoder);
506 return i;
507 }
508
509 i = adv7175_write_block(client, init_common, sizeof(init_common));
510 if (i >= 0) {
511 i = adv7175_write(client, 0x07, TR0MODE | TR0RST);
512 i = adv7175_write(client, 0x07, TR0MODE);
513 i = adv7175_read(client, 0x12);
514 dprintk(1, KERN_INFO "%s_attach: rev. %d at 0x%x\n",
515 I2C_NAME(client), i & 1, client->addr << 1);
516 }
517 if (i < 0) {
518 dprintk(1, KERN_ERR "%s_attach: init error 0x%x\n",
519 I2C_NAME(client), i);
520 }
521
522 return 0;
523}
524
525static int
526adv7175_attach_adapter (struct i2c_adapter *adapter)
527{
528 dprintk(1,
529 KERN_INFO
530 "adv7175.c: starting probe for adapter %s (0x%x)\n",
531 I2C_NAME(adapter), adapter->id);
532 return i2c_probe(adapter, &addr_data, &adv7175_detect_client);
533}
534
535static int
536adv7175_detach_client (struct i2c_client *client)
537{
538 struct adv7175 *encoder = i2c_get_clientdata(client);
539 int err;
540
541 err = i2c_detach_client(client);
542 if (err) {
543 return err;
544 }
545
546 kfree(encoder);
547 kfree(client);
548
549 return 0;
550}
551
552/* ----------------------------------------------------------------------- */
553
554static struct i2c_driver i2c_driver_adv7175 = {
555 .owner = THIS_MODULE,
556 .name = "adv7175", /* name */
557
558 .id = I2C_DRIVERID_ADV7175,
559 .flags = I2C_DF_NOTIFY,
560
561 .attach_adapter = adv7175_attach_adapter,
562 .detach_client = adv7175_detach_client,
563 .command = adv7175_command,
564};
565
566static int __init
567adv7175_init (void)
568{
569 return i2c_add_driver(&i2c_driver_adv7175);
570}
571
572static void __exit
573adv7175_exit (void)
574{
575 i2c_del_driver(&i2c_driver_adv7175);
576}
577
578module_init(adv7175_init);
579module_exit(adv7175_exit);