OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [media/] [video/] [i2c-old.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.