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