blob: 4e218f22b214653aaad19696be4303eef2120ae0 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/types.h>
43
44#include <linux/videodev.h>
45#include <asm/uaccess.h>
46
47MODULE_DESCRIPTION("Analog Devices ADV7175 video encoder driver");
48MODULE_AUTHOR("Dave Perks");
49MODULE_LICENSE("GPL");
50
51#include <linux/i2c.h>
52#include <linux/i2c-dev.h>
53
54#define I2C_NAME(s) (s)->name
55
56#include <linux/video_encoder.h>
57
58static int debug = 0;
59module_param(debug, int, 0);
60MODULE_PARM_DESC(debug, "Debug level (0-1)");
61
62#define dprintk(num, format, args...) \
63 do { \
64 if (debug >= num) \
65 printk(format, ##args); \
66 } while (0)
67
68/* ----------------------------------------------------------------------- */
69
70struct adv7175 {
71 unsigned char reg[128];
72
73 int norm;
74 int input;
75 int enable;
76 int bright;
77 int contrast;
78 int hue;
79 int sat;
80};
81
82#define I2C_ADV7175 0xd4
83#define I2C_ADV7176 0x54
84
85static char adv7175_name[] = "adv7175";
86static char adv7176_name[] = "adv7176";
87
88static char *inputs[] = { "pass_through", "play_back", "color_bar" };
89static char *norms[] = { "PAL", "NTSC", "SECAM->PAL (may not work!)" };
90
91/* ----------------------------------------------------------------------- */
92
93static inline int
94adv7175_write (struct i2c_client *client,
95 u8 reg,
96 u8 value)
97{
98 struct adv7175 *encoder = i2c_get_clientdata(client);
99
100 encoder->reg[reg] = value;
101 return i2c_smbus_write_byte_data(client, reg, value);
102}
103
104static inline int
105adv7175_read (struct i2c_client *client,
106 u8 reg)
107{
108 return i2c_smbus_read_byte_data(client, reg);
109}
110
111static int
112adv7175_write_block (struct i2c_client *client,
113 const u8 *data,
114 unsigned int len)
115{
116 int ret = -1;
117 u8 reg;
118
119 /* the adv7175 has an autoincrement function, use it if
120 * the adapter understands raw I2C */
121 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
122 /* do raw I2C, not smbus compatible */
123 struct adv7175 *encoder = i2c_get_clientdata(client);
124 struct i2c_msg msg;
125 u8 block_data[32];
126
127 msg.addr = client->addr;
128 msg.flags = 0;
129 while (len >= 2) {
130 msg.buf = (char *) block_data;
131 msg.len = 0;
132 block_data[msg.len++] = reg = data[0];
133 do {
134 block_data[msg.len++] =
135 encoder->reg[reg++] = data[1];
136 len -= 2;
137 data += 2;
138 } while (len >= 2 && data[0] == reg &&
139 msg.len < 32);
140 if ((ret = i2c_transfer(client->adapter,
141 &msg, 1)) < 0)
142 break;
143 }
144 } else {
145 /* do some slow I2C emulation kind of thing */
146 while (len >= 2) {
147 reg = *data++;
148 if ((ret = adv7175_write(client, reg,
149 *data++)) < 0)
150 break;
151 len -= 2;
152 }
153 }
154
155 return ret;
156}
157
158static void
159set_subcarrier_freq (struct i2c_client *client,
160 int pass_through)
161{
162 /* for some reason pass_through NTSC needs
163 * a different sub-carrier freq to remain stable. */
164 if(pass_through)
165 adv7175_write(client, 0x02, 0x00);
166 else
167 adv7175_write(client, 0x02, 0x55);
168
169 adv7175_write(client, 0x03, 0x55);
170 adv7175_write(client, 0x04, 0x55);
171 adv7175_write(client, 0x05, 0x25);
172}
173
174#ifdef ENCODER_DUMP
175static void
176dump (struct i2c_client *client)
177{
178 struct adv7175 *encoder = i2c_get_clientdata(client);
179 int i, j;
180
181 printk(KERN_INFO "%s: registry dump\n", I2C_NAME(client));
182 for (i = 0; i < 182 / 8; i++) {
183 printk("%s: 0x%02x -", I2C_NAME(client), i * 8);
184 for (j = 0; j < 8; j++) {
185 printk(" 0x%02x", encoder->reg[i * 8 + j]);
186 }
187 printk("\n");
188 }
189}
190#endif
191
192/* ----------------------------------------------------------------------- */
193// Output filter: S-Video Composite
194
195#define MR050 0x11 //0x09
196#define MR060 0x14 //0x0c
197
198//---------------------------------------------------------------------------
199
200#define TR0MODE 0x46
201#define TR0RST 0x80
202
203#define TR1CAPT 0x80
204#define TR1PLAY 0x00
205
206static const unsigned char init_common[] = {
207
208 0x00, MR050, /* MR0, PAL enabled */
209 0x01, 0x00, /* MR1 */
210 0x02, 0x0c, /* subc. freq. */
211 0x03, 0x8c, /* subc. freq. */
212 0x04, 0x79, /* subc. freq. */
213 0x05, 0x26, /* subc. freq. */
214 0x06, 0x40, /* subc. phase */
215
216 0x07, TR0MODE, /* TR0, 16bit */
217 0x08, 0x21, /* */
218 0x09, 0x00, /* */
219 0x0a, 0x00, /* */
220 0x0b, 0x00, /* */
221 0x0c, TR1CAPT, /* TR1 */
222 0x0d, 0x4f, /* MR2 */
223 0x0e, 0x00, /* */
224 0x0f, 0x00, /* */
225 0x10, 0x00, /* */
226 0x11, 0x00, /* */
227};
228
229static const unsigned char init_pal[] = {
230 0x00, MR050, /* MR0, PAL enabled */
231 0x01, 0x00, /* MR1 */
232 0x02, 0x0c, /* subc. freq. */
233 0x03, 0x8c, /* subc. freq. */
234 0x04, 0x79, /* subc. freq. */
235 0x05, 0x26, /* subc. freq. */
236 0x06, 0x40, /* subc. phase */
237};
238
239static const unsigned char init_ntsc[] = {
240 0x00, MR060, /* MR0, NTSC enabled */
241 0x01, 0x00, /* MR1 */
242 0x02, 0x55, /* subc. freq. */
243 0x03, 0x55, /* subc. freq. */
244 0x04, 0x55, /* subc. freq. */
245 0x05, 0x25, /* subc. freq. */
246 0x06, 0x1a, /* subc. phase */
247};
248
249static int
250adv7175_command (struct i2c_client *client,
251 unsigned int cmd,
252 void *arg)
253{
254 struct adv7175 *encoder = i2c_get_clientdata(client);
255
256 switch (cmd) {
257
258 case 0:
259 /* This is just for testing!!! */
260 adv7175_write_block(client, init_common,
261 sizeof(init_common));
262 adv7175_write(client, 0x07, TR0MODE | TR0RST);
263 adv7175_write(client, 0x07, TR0MODE);
264 break;
265
266 case ENCODER_GET_CAPABILITIES:
267 {
268 struct video_encoder_capability *cap = arg;
269
270 cap->flags = VIDEO_ENCODER_PAL |
271 VIDEO_ENCODER_NTSC |
272 VIDEO_ENCODER_SECAM; /* well, hacky */
273 cap->inputs = 2;
274 cap->outputs = 1;
275 }
276 break;
277
278 case ENCODER_SET_NORM:
279 {
280 int iarg = *(int *) arg;
281
282 switch (iarg) {
283
284 case VIDEO_MODE_NTSC:
285 adv7175_write_block(client, init_ntsc,
286 sizeof(init_ntsc));
287 if (encoder->input == 0)
288 adv7175_write(client, 0x0d, 0x4f); // Enable genlock
289 adv7175_write(client, 0x07, TR0MODE | TR0RST);
290 adv7175_write(client, 0x07, TR0MODE);
291 break;
292
293 case VIDEO_MODE_PAL:
294 adv7175_write_block(client, init_pal,
295 sizeof(init_pal));
296 if (encoder->input == 0)
297 adv7175_write(client, 0x0d, 0x4f); // Enable genlock
298 adv7175_write(client, 0x07, TR0MODE | TR0RST);
299 adv7175_write(client, 0x07, TR0MODE);
300 break;
301
302 case VIDEO_MODE_SECAM: // WARNING! ADV7176 does not support SECAM.
303 /* This is an attempt to convert
304 * SECAM->PAL (typically it does not work
305 * due to genlock: when decoder is in SECAM
306 * and encoder in in PAL the subcarrier can
307 * not be syncronized with horizontal
308 * quency) */
309 adv7175_write_block(client, init_pal,
310 sizeof(init_pal));
311 if (encoder->input == 0)
312 adv7175_write(client, 0x0d, 0x49); // Disable genlock
313 adv7175_write(client, 0x07, TR0MODE | TR0RST);
314 adv7175_write(client, 0x07, TR0MODE);
315 break;
316 default:
317 dprintk(1, KERN_ERR "%s: illegal norm: %d\n",
318 I2C_NAME(client), iarg);
319 return -EINVAL;
320
321 }
322 dprintk(1, KERN_INFO "%s: switched to %s\n", I2C_NAME(client),
323 norms[iarg]);
324 encoder->norm = iarg;
325 }
326 break;
327
328 case ENCODER_SET_INPUT:
329 {
330 int iarg = *(int *) arg;
331
332 /* RJ: *iarg = 0: input is from SAA7110
333 *iarg = 1: input is from ZR36060
334 *iarg = 2: color bar */
335
336 switch (iarg) {
337
338 case 0:
339 adv7175_write(client, 0x01, 0x00);
340
341 if (encoder->norm == VIDEO_MODE_NTSC)
342 set_subcarrier_freq(client, 1);
343
344 adv7175_write(client, 0x0c, TR1CAPT); /* TR1 */
345 if (encoder->norm == VIDEO_MODE_SECAM)
346 adv7175_write(client, 0x0d, 0x49); // Disable genlock
347 else
348 adv7175_write(client, 0x0d, 0x4f); // Enable genlock
349 adv7175_write(client, 0x07, TR0MODE | TR0RST);
350 adv7175_write(client, 0x07, TR0MODE);
351 //udelay(10);
352 break;
353
354 case 1:
355 adv7175_write(client, 0x01, 0x00);
356
357 if (encoder->norm == VIDEO_MODE_NTSC)
358 set_subcarrier_freq(client, 0);
359
360 adv7175_write(client, 0x0c, TR1PLAY); /* TR1 */
361 adv7175_write(client, 0x0d, 0x49);
362 adv7175_write(client, 0x07, TR0MODE | TR0RST);
363 adv7175_write(client, 0x07, TR0MODE);
364 //udelay(10);
365 break;
366
367 case 2:
368 adv7175_write(client, 0x01, 0x80);
369
370 if (encoder->norm == VIDEO_MODE_NTSC)
371 set_subcarrier_freq(client, 0);
372
373 adv7175_write(client, 0x0d, 0x49);
374 adv7175_write(client, 0x07, TR0MODE | TR0RST);
375 adv7175_write(client, 0x07, TR0MODE);
376 //udelay(10);
377 break;
378
379 default:
380 dprintk(1, KERN_ERR "%s: illegal input: %d\n",
381 I2C_NAME(client), iarg);
382 return -EINVAL;
383
384 }
385 dprintk(1, KERN_INFO "%s: switched to %s\n", I2C_NAME(client),
386 inputs[iarg]);
387 encoder->input = iarg;
388 }
389 break;
390
391 case ENCODER_SET_OUTPUT:
392 {
393 int *iarg = arg;
394
395 /* not much choice of outputs */
396 if (*iarg != 0) {
397 return -EINVAL;
398 }
399 }
400 break;
401
402 case ENCODER_ENABLE_OUTPUT:
403 {
404 int *iarg = arg;
405
406 encoder->enable = !!*iarg;
407 }
408 break;
409
410#ifdef ENCODER_DUMP
411 case ENCODER_DUMP:
412 {
413 dump(client);
414 }
415 break;
416#endif
417
418 default:
419 return -EINVAL;
420 }
421
422 return 0;
423}
424
425/* ----------------------------------------------------------------------- */
426
427/*
428 * Generic i2c probe
429 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
430 */
431static unsigned short normal_i2c[] =
432 { I2C_ADV7175 >> 1, (I2C_ADV7175 >> 1) + 1,
433 I2C_ADV7176 >> 1, (I2C_ADV7176 >> 1) + 1,
434 I2C_CLIENT_END
435};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Jean Delvare68cc9d02005-04-02 20:04:41 +0200437static unsigned short ignore = I2C_CLIENT_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439static struct i2c_client_address_data addr_data = {
440 .normal_i2c = normal_i2c,
Jean Delvare68cc9d02005-04-02 20:04:41 +0200441 .probe = &ignore,
442 .ignore = &ignore,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443};
444
445static struct i2c_driver i2c_driver_adv7175;
446
447static int
448adv7175_detect_client (struct i2c_adapter *adapter,
449 int address,
450 int kind)
451{
452 int i;
453 struct i2c_client *client;
454 struct adv7175 *encoder;
455 char *dname;
456
457 dprintk(1,
458 KERN_INFO
459 "adv7175.c: detecting adv7175 client on address 0x%x\n",
460 address << 1);
461
462 /* Check if the adapter supports the needed features */
463 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
464 return 0;
465
Panagiotis Issaris74081872006-01-11 19:40:56 -0200466 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (client == 0)
468 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 client->addr = address;
470 client->adapter = adapter;
471 client->driver = &i2c_driver_adv7175;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if ((client->addr == I2C_ADV7175 >> 1) ||
473 (client->addr == (I2C_ADV7175 >> 1) + 1)) {
474 dname = adv7175_name;
475 } else if ((client->addr == I2C_ADV7176 >> 1) ||
476 (client->addr == (I2C_ADV7176 >> 1) + 1)) {
477 dname = adv7176_name;
478 } else {
479 /* We should never get here!!! */
480 kfree(client);
481 return 0;
482 }
483 strlcpy(I2C_NAME(client), dname, sizeof(I2C_NAME(client)));
484
Panagiotis Issaris74081872006-01-11 19:40:56 -0200485 encoder = kzalloc(sizeof(struct adv7175), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (encoder == NULL) {
487 kfree(client);
488 return -ENOMEM;
489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 encoder->norm = VIDEO_MODE_PAL;
491 encoder->input = 0;
492 encoder->enable = 1;
493 i2c_set_clientdata(client, encoder);
494
495 i = i2c_attach_client(client);
496 if (i) {
497 kfree(client);
498 kfree(encoder);
499 return i;
500 }
501
502 i = adv7175_write_block(client, init_common, sizeof(init_common));
503 if (i >= 0) {
504 i = adv7175_write(client, 0x07, TR0MODE | TR0RST);
505 i = adv7175_write(client, 0x07, TR0MODE);
506 i = adv7175_read(client, 0x12);
507 dprintk(1, KERN_INFO "%s_attach: rev. %d at 0x%x\n",
508 I2C_NAME(client), i & 1, client->addr << 1);
509 }
510 if (i < 0) {
511 dprintk(1, KERN_ERR "%s_attach: init error 0x%x\n",
512 I2C_NAME(client), i);
513 }
514
515 return 0;
516}
517
518static int
519adv7175_attach_adapter (struct i2c_adapter *adapter)
520{
521 dprintk(1,
522 KERN_INFO
523 "adv7175.c: starting probe for adapter %s (0x%x)\n",
524 I2C_NAME(adapter), adapter->id);
525 return i2c_probe(adapter, &addr_data, &adv7175_detect_client);
526}
527
528static int
529adv7175_detach_client (struct i2c_client *client)
530{
531 struct adv7175 *encoder = i2c_get_clientdata(client);
532 int err;
533
534 err = i2c_detach_client(client);
535 if (err) {
536 return err;
537 }
538
539 kfree(encoder);
540 kfree(client);
541
542 return 0;
543}
544
545/* ----------------------------------------------------------------------- */
546
547static struct i2c_driver i2c_driver_adv7175 = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100548 .driver = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100549 .name = "adv7175", /* name */
550 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 .id = I2C_DRIVERID_ADV7175,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 .attach_adapter = adv7175_attach_adapter,
555 .detach_client = adv7175_detach_client,
556 .command = adv7175_command,
557};
558
559static int __init
560adv7175_init (void)
561{
562 return i2c_add_driver(&i2c_driver_adv7175);
563}
564
565static void __exit
566adv7175_exit (void)
567{
568 i2c_del_driver(&i2c_driver_adv7175);
569}
570
571module_init(adv7175_init);
572module_exit(adv7175_exit);