File: /usr/src/linux/drivers/media/video/i2c-old.c

1     /*
2      *	Generic i2c interface for linux
3      *
4      *	(c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de>
5      *
6      */
7     
8     #include <linux/config.h>
9     #include <linux/module.h>
10     #include <linux/kernel.h>
11     #include <linux/errno.h>
12     #include <linux/types.h>
13     #include <linux/string.h>
14     #include <linux/delay.h>
15     #include <linux/locks.h>
16     #include <linux/sched.h>
17     #include <linux/slab.h>
18     #include <linux/i2c-old.h>
19     
20     #define REGPRINT(x)   if (verbose)   (x)
21     #define I2C_DEBUG(x)  if (i2c_debug) (x)
22     
23     static int scan      = 0;
24     static int verbose   = 0;
25     static int i2c_debug = 0;
26     
27     #if LINUX_VERSION_CODE >= 0x020117
28     MODULE_PARM(scan,"i");
29     MODULE_PARM(verbose,"i");
30     MODULE_PARM(i2c_debug,"i");
31     #endif
32     
33     /* ----------------------------------------------------------------------- */
34     
35     static struct i2c_bus    *busses[I2C_BUS_MAX];
36     static struct i2c_driver *drivers[I2C_DRIVER_MAX];
37     static int bus_count = 0, driver_count = 0;
38     
39     #ifdef CONFIG_VIDEO_BUZ
40     extern int saa7111_init(void);
41     extern int saa7185_init(void);
42     #endif
43     #ifdef CONFIG_VIDEO_LML33
44     extern int bt819_init(void);
45     extern int bt856_init(void);
46     #endif
47     
48     int i2c_init(void)
49     {
50     	printk(KERN_INFO "i2c: initialized%s\n",
51     		scan ? " (i2c bus scan enabled)" : "");
52     	/* anything to do here ? */
53     #ifdef CONFIG_VIDEO_BUZ
54     	saa7111_init();
55     	saa7185_init();
56     #endif
57     #ifdef CONFIG_VIDEO_LML33
58     	bt819_init();
59     	bt856_init();
60     #endif
61     	return 0;
62     }
63     
64     /* ----------------------------------------------------------------------- */
65     
66     static void i2c_attach_device(struct i2c_bus *bus, struct i2c_driver *driver)
67     {
68     	struct i2c_device *device;
69     	int i,j,ack=1;
70     	unsigned char addr;
71     	LOCK_FLAGS;
72         
73     	/* probe for device */
74     	LOCK_I2C_BUS(bus);
75     	for (addr = driver->addr_l; addr <= driver->addr_h; addr += 2) 
76     	{
77     		i2c_start(bus);
78     		ack = i2c_sendbyte(bus,addr,0);
79     		i2c_stop(bus);
80     		if (!ack)
81     			break;
82     	}
83     	UNLOCK_I2C_BUS(bus);
84     	if (ack)
85     		return;
86     
87     	/* got answer */
88     	for (i = 0; i < I2C_DEVICE_MAX; i++)
89     		if (NULL == driver->devices[i])
90     			break;
91     	if (I2C_DEVICE_MAX == i)
92     		return;
93     
94     	for (j = 0; j < I2C_DEVICE_MAX; j++)
95     		if (NULL == bus->devices[j])
96     			break;
97     	if (I2C_DEVICE_MAX == j)
98     		return;
99     
100     	if (NULL == (device = kmalloc(sizeof(struct i2c_device),GFP_KERNEL)))
101     		return;
102     	device->bus = bus;
103     	device->driver = driver;
104     	device->addr = addr;
105     
106     	/* Attach */
107     	
108     	if (driver->attach(device)!=0) 
109     	{
110     		kfree(device);
111     		return;
112     	}
113     	driver->devices[i] = device;
114     	driver->devcount++;
115     	bus->devices[j] = device;
116     	bus->devcount++;
117     
118     	if (bus->attach_inform)
119     		bus->attach_inform(bus,driver->id);
120     	REGPRINT(printk("i2c: device attached: %s (addr=0x%02x, bus=%s, driver=%s)\n",device->name,addr,bus->name,driver->name));
121     }
122     
123     static void i2c_detach_device(struct i2c_device *device)
124     {
125     	int i;
126     
127     	if (device->bus->detach_inform)
128     		device->bus->detach_inform(device->bus,device->driver->id);
129     	device->driver->detach(device);
130     
131     	for (i = 0; i < I2C_DEVICE_MAX; i++)
132     		if (device == device->driver->devices[i])
133     			break;
134     	if (I2C_DEVICE_MAX == i) 
135     	{
136     		printk(KERN_WARNING "i2c: detach_device #1: device not found: %s\n",
137     			device->name);
138     		return;
139     	}
140     	device->driver->devices[i] = NULL;
141     	device->driver->devcount--;
142     
143     	for (i = 0; i < I2C_DEVICE_MAX; i++)
144     		if (device == device->bus->devices[i])
145     			break;
146     	if (I2C_DEVICE_MAX == i) 
147     	{
148     		printk(KERN_WARNING "i2c: detach_device #2: device not found: %s\n",
149     		       device->name);
150     		return;
151     	}
152     	device->bus->devices[i] = NULL;
153     	device->bus->devcount--;
154     
155     	REGPRINT(printk("i2c: device detached: %s (addr=0x%02x, bus=%s, driver=%s)\n",device->name,device->addr,device->bus->name,device->driver->name));
156     	kfree(device);
157     }
158     
159     /* ----------------------------------------------------------------------- */
160     
161     int i2c_register_bus(struct i2c_bus *bus)
162     {
163     	int i,ack;
164     	LOCK_FLAGS;
165     
166     	memset(bus->devices,0,sizeof(bus->devices));
167     	bus->devcount = 0;
168     
169     	for (i = 0; i < I2C_BUS_MAX; i++)
170     		if (NULL == busses[i])
171     			break;
172     	if (I2C_BUS_MAX == i)
173     		return -ENOMEM;
174     
175     	busses[i] = bus;
176     	bus_count++;
177     	REGPRINT(printk("i2c: bus registered: %s\n",bus->name));
178     	
179     	MOD_INC_USE_COUNT;
180     
181     	if (scan) 
182     	{
183     		/* scan whole i2c bus */
184     		LOCK_I2C_BUS(bus);
185     		for (i = 0; i < 256; i+=2) 
186     		{
187     			i2c_start(bus);
188     			ack = i2c_sendbyte(bus,i,0);
189     			i2c_stop(bus);
190     			if (!ack) 
191     			{
192     				printk(KERN_INFO "i2c: scanning bus %s: found device at addr=0x%02x\n",
193     					bus->name,i);
194     			}
195     		}
196     		UNLOCK_I2C_BUS(bus);
197     	}
198     
199     	/* probe available drivers */
200     	for (i = 0; i < I2C_DRIVER_MAX; i++)
201     		if (drivers[i])
202     			i2c_attach_device(bus,drivers[i]);
203     	return 0;
204     }
205     
206     int i2c_unregister_bus(struct i2c_bus *bus)
207     {
208     	int i;
209     
210     	/* detach devices */
211     	for (i = 0; i < I2C_DEVICE_MAX; i++)
212     		if (bus->devices[i])
213     			i2c_detach_device(bus->devices[i]);
214     
215     	for (i = 0; i < I2C_BUS_MAX; i++)
216     		if (bus == busses[i])
217     			break;
218     	if (I2C_BUS_MAX == i) 
219     	{
220     		printk(KERN_WARNING "i2c: unregister_bus #1: bus not found: %s\n",
221     			bus->name);
222     		return -ENODEV;
223     	}
224     	
225     	MOD_DEC_USE_COUNT;
226     	
227     	busses[i] = NULL;
228     	bus_count--;
229     	REGPRINT(printk("i2c: bus unregistered: %s\n",bus->name));
230     
231     	return 0;    
232     }
233     
234     /* ----------------------------------------------------------------------- */
235     
236     int i2c_register_driver(struct i2c_driver *driver)
237     {
238     	int i;
239     
240     	memset(driver->devices,0,sizeof(driver->devices));
241     	driver->devcount = 0;
242     
243     	for (i = 0; i < I2C_DRIVER_MAX; i++)
244     		if (NULL == drivers[i])
245     			break;
246     	if (I2C_DRIVER_MAX == i)
247     		return -ENOMEM;
248     
249     	drivers[i] = driver;
250     	driver_count++;
251     	
252     	MOD_INC_USE_COUNT;
253     	
254     	REGPRINT(printk("i2c: driver registered: %s\n",driver->name));
255     
256     	/* Probe available busses */
257     	for (i = 0; i < I2C_BUS_MAX; i++)
258     		if (busses[i])
259     			i2c_attach_device(busses[i],driver);
260     
261     	return 0;
262     }
263     
264     int i2c_unregister_driver(struct i2c_driver *driver)
265     {
266     	int i;
267     
268     	/* detach devices */
269     	for (i = 0; i < I2C_DEVICE_MAX; i++)
270     		if (driver->devices[i])
271     			i2c_detach_device(driver->devices[i]);
272     
273     	for (i = 0; i < I2C_DRIVER_MAX; i++)
274     		if (driver == drivers[i])
275     			break;
276     	if (I2C_DRIVER_MAX == i) 
277     	{
278     		printk(KERN_WARNING "i2c: unregister_driver: driver not found: %s\n",
279     			driver->name);
280     		return -ENODEV;
281     	}
282     
283     	MOD_DEC_USE_COUNT;
284     	
285     	drivers[i] = NULL;
286     	driver_count--;
287     	REGPRINT(printk("i2c: driver unregistered: %s\n",driver->name));
288     
289     	return 0;
290     }
291     
292     /* ----------------------------------------------------------------------- */
293     
294     int i2c_control_device(struct i2c_bus *bus, int id,
295     		       unsigned int cmd, void *arg)
296     {
297     	int i;
298     
299     	for (i = 0; i < I2C_DEVICE_MAX; i++)
300     		if (bus->devices[i] && bus->devices[i]->driver->id == id)
301     			break;
302     	if (i == I2C_DEVICE_MAX)
303     		return -ENODEV;
304     	if (NULL == bus->devices[i]->driver->command)
305     		return -ENODEV;
306     	return bus->devices[i]->driver->command(bus->devices[i],cmd,arg);
307     }
308     
309     /* ----------------------------------------------------------------------- */
310     
311     #define I2C_SET(bus,ctrl,data)  (bus->i2c_setlines(bus,ctrl,data))
312     #define I2C_GET(bus)            (bus->i2c_getdataline(bus))
313     
314     void i2c_start(struct i2c_bus *bus)
315     {
316     	I2C_SET(bus,0,1);
317     	I2C_SET(bus,1,1);
318     	I2C_SET(bus,1,0);
319     	I2C_SET(bus,0,0);
320     	I2C_DEBUG(printk("%s: < ",bus->name));
321     }
322     
323     void i2c_stop(struct i2c_bus *bus)
324     {
325     	I2C_SET(bus,0,0);
326     	I2C_SET(bus,1,0);
327     	I2C_SET(bus,1,1);
328     	I2C_DEBUG(printk(">\n"));
329     }
330     
331     void i2c_one(struct i2c_bus *bus)
332     {
333     	I2C_SET(bus,0,1);
334     	I2C_SET(bus,1,1);
335     	I2C_SET(bus,0,1);
336     }
337     
338     void i2c_zero(struct i2c_bus *bus)
339     {
340     	I2C_SET(bus,0,0);
341     	I2C_SET(bus,1,0);
342     	I2C_SET(bus,0,0);
343     }
344     
345     int i2c_ack(struct i2c_bus *bus)
346     {
347     	int ack;
348         
349     	I2C_SET(bus,0,1);
350     	I2C_SET(bus,1,1);
351     	ack = I2C_GET(bus);
352     	I2C_SET(bus,0,1);
353     	return ack;
354     }
355     
356     int i2c_sendbyte(struct i2c_bus *bus,unsigned char data,int wait_for_ack)
357     {
358     	int i, ack;
359         
360     	I2C_SET(bus,0,0);
361     	for (i=7; i>=0; i--)
362     		(data&(1<<i)) ? i2c_one(bus) : i2c_zero(bus);
363     	if (wait_for_ack)
364     		udelay(wait_for_ack);
365     	ack=i2c_ack(bus);
366     	I2C_DEBUG(printk("%02x%c ",(int)data,ack?'-':'+'));
367     	return ack;
368     }
369     
370     unsigned char i2c_readbyte(struct i2c_bus *bus,int last)
371     {
372     	int i;
373     	unsigned char data=0;
374         
375     	I2C_SET(bus,0,1);
376     	for (i=7; i>=0; i--) 
377     	{
378     		I2C_SET(bus,1,1);
379     		if (I2C_GET(bus))
380     			data |= (1<<i);
381     		I2C_SET(bus,0,1);
382     	}
383     	last ? i2c_one(bus) : i2c_zero(bus);
384     	I2C_DEBUG(printk("=%02x%c ",(int)data,last?'-':'+'));
385     	return data;
386     }
387     
388     /* ----------------------------------------------------------------------- */
389     
390     int i2c_read(struct i2c_bus *bus, unsigned char addr)
391     {
392     	int ret;
393         
394     	if (bus->i2c_read)
395     		return bus->i2c_read(bus, addr);
396     
397     	i2c_start(bus);
398     	i2c_sendbyte(bus,addr,0);
399     	ret = i2c_readbyte(bus,1);
400     	i2c_stop(bus);
401     	return ret;
402     }
403     
404     int i2c_write(struct i2c_bus *bus, unsigned char addr,
405     	      unsigned char data1, unsigned char data2, int both)
406     {
407     	int ack;
408     
409     	if (bus->i2c_write)
410     		return bus->i2c_write(bus, addr, data1, data2, both);
411     
412     	i2c_start(bus);
413     	i2c_sendbyte(bus,addr,0);
414     	ack = i2c_sendbyte(bus,data1,0);
415     	if (both)
416     		ack = i2c_sendbyte(bus,data2,0);
417     	i2c_stop(bus);
418     	return ack ? -1 : 0 ;
419     }
420     
421     /* ----------------------------------------------------------------------- */
422     
423     #ifdef MODULE
424     
425     #if LINUX_VERSION_CODE >= 0x020100
426     EXPORT_SYMBOL(i2c_register_bus);
427     EXPORT_SYMBOL(i2c_unregister_bus);
428     EXPORT_SYMBOL(i2c_register_driver);
429     EXPORT_SYMBOL(i2c_unregister_driver);
430     EXPORT_SYMBOL(i2c_control_device);
431     EXPORT_SYMBOL(i2c_start);
432     EXPORT_SYMBOL(i2c_stop);
433     EXPORT_SYMBOL(i2c_one);
434     EXPORT_SYMBOL(i2c_zero);
435     EXPORT_SYMBOL(i2c_ack);
436     EXPORT_SYMBOL(i2c_sendbyte);
437     EXPORT_SYMBOL(i2c_readbyte);
438     EXPORT_SYMBOL(i2c_read);
439     EXPORT_SYMBOL(i2c_write);
440     #endif
441     
442     int init_module(void)
443     {
444     	return i2c_init();
445     }
446     
447     void cleanup_module(void)
448     {
449     }
450     #endif
451