1/*
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/signal.h>
41#include <asm/io.h>
42#include <asm/pgtable.h>
43#include <asm/page.h>
44#include <linux/types.h>
45
46#include <linux/videodev.h>
47#include <asm/uaccess.h>
48
49MODULE_DESCRIPTION("Analog Devices ADV7170 video encoder driver");
50MODULE_AUTHOR("Maxim Yevtyushkin");
51MODULE_LICENSE("GPL");
52
53#include <linux/i2c.h>
54
55#define I2C_NAME(x) (x)->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 adv7170 {
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_ADV7170        0xd4
84#define   I2C_ADV7171        0x54
85
86static char adv7170_name[] = "adv7170";
87static char adv7171_name[] = "adv7171";
88
89static char *inputs[] = { "pass_through", "play_back" };
90static char *norms[] = { "PAL", "NTSC" };
91
92/* ----------------------------------------------------------------------- */
93
94static inline int
95adv7170_write (struct i2c_client *client,
96	       u8                 reg,
97	       u8                 value)
98{
99	struct adv7170 *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
106adv7170_read (struct i2c_client *client,
107	      u8                 reg)
108{
109	return i2c_smbus_read_byte_data(client, reg);
110}
111
112static int
113adv7170_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 adv7170 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 adv7170 *encoder = i2c_get_clientdata(client);
125		u8 block_data[32];
126		int block_len;
127
128		while (len >= 2) {
129			block_len = 0;
130			block_data[block_len++] = reg = data[0];
131			do {
132				block_data[block_len++] =
133				    encoder->reg[reg++] = data[1];
134				len -= 2;
135				data += 2;
136			} while (len >= 2 && data[0] == reg &&
137				 block_len < 32);
138			if ((ret = i2c_master_send(client, block_data,
139						   block_len)) < 0)
140				break;
141		}
142	} else {
143		/* do some slow I2C emulation kind of thing */
144		while (len >= 2) {
145			reg = *data++;
146			if ((ret = adv7170_write(client, reg,
147						 *data++)) < 0)
148				break;
149			len -= 2;
150		}
151	}
152
153	return ret;
154}
155
156/* ----------------------------------------------------------------------- */
157// Output filter:  S-Video  Composite
158
159#define MR050       0x11	//0x09
160#define MR060       0x14	//0x0c
161
162//---------------------------------------------------------------------------
163
164#define TR0MODE     0x4c
165#define TR0RST	    0x80
166
167#define TR1CAPT	    0x00
168#define TR1PLAY	    0x00
169
170
171static const unsigned char init_NTSC[] = {
172	0x00, 0x10,		// MR0
173	0x01, 0x20,		// MR1
174	0x02, 0x0e,		// MR2 RTC control: bits 2 and 1
175	0x03, 0x80,		// MR3
176	0x04, 0x30,		// MR4
177	0x05, 0x00,		// Reserved
178	0x06, 0x00,		// Reserved
179	0x07, TR0MODE,		// TM0
180	0x08, TR1CAPT,		// TM1
181	0x09, 0x16,		// Fsc0
182	0x0a, 0x7c,		// Fsc1
183	0x0b, 0xf0,		// Fsc2
184	0x0c, 0x21,		// Fsc3
185	0x0d, 0x00,		// Subcarrier Phase
186	0x0e, 0x00,		// Closed Capt. Ext 0
187	0x0f, 0x00,		// Closed Capt. Ext 1
188	0x10, 0x00,		// Closed Capt. 0
189	0x11, 0x00,		// Closed Capt. 1
190	0x12, 0x00,		// Pedestal Ctl 0
191	0x13, 0x00,		// Pedestal Ctl 1
192	0x14, 0x00,		// Pedestal Ctl 2
193	0x15, 0x00,		// Pedestal Ctl 3
194	0x16, 0x00,		// CGMS_WSS_0
195	0x17, 0x00,		// CGMS_WSS_1
196	0x18, 0x00,		// CGMS_WSS_2
197	0x19, 0x00,		// Teletext Ctl
198};
199
200static const unsigned char init_PAL[] = {
201	0x00, 0x71,		// MR0
202	0x01, 0x20,		// MR1
203	0x02, 0x0e,		// MR2 RTC control: bits 2 and 1
204	0x03, 0x80,		// MR3
205	0x04, 0x30,		// MR4
206	0x05, 0x00,		// Reserved
207	0x06, 0x00,		// Reserved
208	0x07, TR0MODE,		// TM0
209	0x08, TR1CAPT,		// TM1
210	0x09, 0xcb,		// Fsc0
211	0x0a, 0x8a,		// Fsc1
212	0x0b, 0x09,		// Fsc2
213	0x0c, 0x2a,		// Fsc3
214	0x0d, 0x00,		// Subcarrier Phase
215	0x0e, 0x00,		// Closed Capt. Ext 0
216	0x0f, 0x00,		// Closed Capt. Ext 1
217	0x10, 0x00,		// Closed Capt. 0
218	0x11, 0x00,		// Closed Capt. 1
219	0x12, 0x00,		// Pedestal Ctl 0
220	0x13, 0x00,		// Pedestal Ctl 1
221	0x14, 0x00,		// Pedestal Ctl 2
222	0x15, 0x00,		// Pedestal Ctl 3
223	0x16, 0x00,		// CGMS_WSS_0
224	0x17, 0x00,		// CGMS_WSS_1
225	0x18, 0x00,		// CGMS_WSS_2
226	0x19, 0x00,		// Teletext Ctl
227};
228
229
230static int
231adv7170_command (struct i2c_client *client,
232		 unsigned int       cmd,
233		 void *             arg)
234{
235	struct adv7170 *encoder = i2c_get_clientdata(client);
236
237	switch (cmd) {
238
239	case 0:
240		break;
241
242	case ENCODER_GET_CAPABILITIES:
243	{
244		struct video_encoder_capability *cap = arg;
245
246		cap->flags = VIDEO_ENCODER_PAL |
247			     VIDEO_ENCODER_NTSC;
248		cap->inputs = 2;
249		cap->outputs = 1;
250	}
251		break;
252
253	case ENCODER_SET_NORM:
254	{
255		int iarg = *(int *) arg;
256
257		dprintk(1, KERN_DEBUG "%s_command: set norm %d",
258			I2C_NAME(client), iarg);
259
260		switch (iarg) {
261
262		case VIDEO_MODE_NTSC:
263			adv7170_write_block(client, init_NTSC,
264					    sizeof(init_NTSC));
265			if (encoder->input == 0)
266				adv7170_write(client, 0x02, 0x0e);	// Enable genlock
267			adv7170_write(client, 0x07, TR0MODE | TR0RST);
268			adv7170_write(client, 0x07, TR0MODE);
269			break;
270
271		case VIDEO_MODE_PAL:
272			adv7170_write_block(client, init_PAL,
273					    sizeof(init_PAL));
274			if (encoder->input == 0)
275				adv7170_write(client, 0x02, 0x0e);	// Enable genlock
276			adv7170_write(client, 0x07, TR0MODE | TR0RST);
277			adv7170_write(client, 0x07, TR0MODE);
278			break;
279
280		default:
281			dprintk(1, KERN_ERR "%s: illegal norm: %d\n",
282			       I2C_NAME(client), iarg);
283			return -EINVAL;
284
285		}
286		dprintk(1, KERN_DEBUG "%s: switched to %s\n", I2C_NAME(client),
287			norms[iarg]);
288		encoder->norm = iarg;
289	}
290		break;
291
292	case ENCODER_SET_INPUT:
293	{
294		int iarg = *(int *) arg;
295
296		/* RJ: *iarg = 0: input is from decoder
297		 *iarg = 1: input is from ZR36060
298		 *iarg = 2: color bar */
299
300		dprintk(1, KERN_DEBUG "%s_command: set input from %s\n",
301			I2C_NAME(client),
302			iarg == 0 ? "decoder" : "ZR36060");
303
304		switch (iarg) {
305
306		case 0:
307			adv7170_write(client, 0x01, 0x20);
308			adv7170_write(client, 0x08, TR1CAPT);	/* TR1 */
309			adv7170_write(client, 0x02, 0x0e);	// Enable genlock
310			adv7170_write(client, 0x07, TR0MODE | TR0RST);
311			adv7170_write(client, 0x07, TR0MODE);
312			//udelay(10);
313			break;
314
315		case 1:
316			adv7170_write(client, 0x01, 0x00);
317			adv7170_write(client, 0x08, TR1PLAY);	/* TR1 */
318			adv7170_write(client, 0x02, 0x08);
319			adv7170_write(client, 0x07, TR0MODE | TR0RST);
320			adv7170_write(client, 0x07, TR0MODE);
321			//udelay(10);
322			break;
323
324		default:
325			dprintk(1, KERN_ERR "%s: illegal input: %d\n",
326				I2C_NAME(client), iarg);
327			return -EINVAL;
328
329		}
330		dprintk(1, KERN_DEBUG "%s: switched to %s\n", I2C_NAME(client),
331			inputs[iarg]);
332		encoder->input = iarg;
333	}
334		break;
335
336	case ENCODER_SET_OUTPUT:
337	{
338		int *iarg = arg;
339
340		/* not much choice of outputs */
341		if (*iarg != 0) {
342			return -EINVAL;
343		}
344	}
345		break;
346
347	case ENCODER_ENABLE_OUTPUT:
348	{
349		int *iarg = arg;
350
351		encoder->enable = !!*iarg;
352	}
353		break;
354
355	default:
356		return -EINVAL;
357	}
358
359	return 0;
360}
361
362/* ----------------------------------------------------------------------- */
363
364/*
365 * Generic i2c probe
366 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
367 */
368static unsigned short normal_i2c[] =
369    { I2C_ADV7170 >> 1, (I2C_ADV7170 >> 1) + 1,
370	I2C_ADV7171 >> 1, (I2C_ADV7171 >> 1) + 1,
371	I2C_CLIENT_END
372};
373
374static unsigned short ignore = I2C_CLIENT_END;
375
376static struct i2c_client_address_data addr_data = {
377	.normal_i2c		= normal_i2c,
378	.probe			= &ignore,
379	.ignore			= &ignore,
380};
381
382static struct i2c_driver i2c_driver_adv7170;
383
384static int
385adv7170_detect_client (struct i2c_adapter *adapter,
386		       int                 address,
387		       int                 kind)
388{
389	int i;
390	struct i2c_client *client;
391	struct adv7170 *encoder;
392	char *dname;
393
394	dprintk(1,
395		KERN_INFO
396		"adv7170.c: detecting adv7170 client on address 0x%x\n",
397		address << 1);
398
399	/* Check if the adapter supports the needed features */
400	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
401		return 0;
402
403	client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
404	if (client == 0)
405		return -ENOMEM;
406	client->addr = address;
407	client->adapter = adapter;
408	client->driver = &i2c_driver_adv7170;
409	if ((client->addr == I2C_ADV7170 >> 1) ||
410	    (client->addr == (I2C_ADV7170 >> 1) + 1)) {
411		dname = adv7170_name;
412	} else if ((client->addr == I2C_ADV7171 >> 1) ||
413		   (client->addr == (I2C_ADV7171 >> 1) + 1)) {
414		dname = adv7171_name;
415	} else {
416		/* We should never get here!!! */
417		kfree(client);
418		return 0;
419	}
420	strlcpy(I2C_NAME(client), dname, sizeof(I2C_NAME(client)));
421
422	encoder = kzalloc(sizeof(struct adv7170), GFP_KERNEL);
423	if (encoder == NULL) {
424		kfree(client);
425		return -ENOMEM;
426	}
427	encoder->norm = VIDEO_MODE_NTSC;
428	encoder->input = 0;
429	encoder->enable = 1;
430	i2c_set_clientdata(client, encoder);
431
432	i = i2c_attach_client(client);
433	if (i) {
434		kfree(client);
435		kfree(encoder);
436		return i;
437	}
438
439	i = adv7170_write_block(client, init_NTSC, sizeof(init_NTSC));
440	if (i >= 0) {
441		i = adv7170_write(client, 0x07, TR0MODE | TR0RST);
442		i = adv7170_write(client, 0x07, TR0MODE);
443		i = adv7170_read(client, 0x12);
444		dprintk(1, KERN_INFO "%s_attach: rev. %d at 0x%02x\n",
445			I2C_NAME(client), i & 1, client->addr << 1);
446	}
447	if (i < 0) {
448		dprintk(1, KERN_ERR "%s_attach: init error 0x%x\n",
449		       I2C_NAME(client), i);
450	}
451
452	return 0;
453}
454
455static int
456adv7170_attach_adapter (struct i2c_adapter *adapter)
457{
458	dprintk(1,
459		KERN_INFO
460		"adv7170.c: starting probe for adapter %s (0x%x)\n",
461		I2C_NAME(adapter), adapter->id);
462	return i2c_probe(adapter, &addr_data, &adv7170_detect_client);
463}
464
465static int
466adv7170_detach_client (struct i2c_client *client)
467{
468	struct adv7170 *encoder = i2c_get_clientdata(client);
469	int err;
470
471	err = i2c_detach_client(client);
472	if (err) {
473		return err;
474	}
475
476	kfree(encoder);
477	kfree(client);
478
479	return 0;
480}
481
482/* ----------------------------------------------------------------------- */
483
484static struct i2c_driver i2c_driver_adv7170 = {
485	.driver = {
486		.name = "adv7170",	/* name */
487	},
488
489	.id = I2C_DRIVERID_ADV7170,
490
491	.attach_adapter = adv7170_attach_adapter,
492	.detach_client = adv7170_detach_client,
493	.command = adv7170_command,
494};
495
496static int __init
497adv7170_init (void)
498{
499	return i2c_add_driver(&i2c_driver_adv7170);
500}
501
502static void __exit
503adv7170_exit (void)
504{
505	i2c_del_driver(&i2c_driver_adv7170);
506}
507
508module_init(adv7170_init);
509module_exit(adv7170_exit);
510