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