Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
 *	Generic i2c interface for linux
 *
 *	(c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de>
 *
 */

#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/delay.h>
#include <linux/locks.h>
#include <linux/sched.h>
#include <linux/malloc.h>
#include <linux/i2c-old.h>

#define REGPRINT(x)   if (verbose)   (x)
#define I2C_DEBUG(x)  if (i2c_debug) (x)

static int scan      = 0;
static int verbose   = 0;
static int i2c_debug = 0;

#if LINUX_VERSION_CODE >= 0x020117
MODULE_PARM(scan,"i");
MODULE_PARM(verbose,"i");
MODULE_PARM(i2c_debug,"i");
#endif

/* ----------------------------------------------------------------------- */

static struct i2c_bus    *busses[I2C_BUS_MAX];
static struct i2c_driver *drivers[I2C_DRIVER_MAX];
static int bus_count = 0, driver_count = 0;

#ifdef CONFIG_VIDEO_BT848
extern int i2c_tuner_init(void);
extern int msp3400c_init(void);
#endif
#ifdef CONFIG_VIDEO_BUZ
extern int saa7111_init(void);
extern int saa7185_init(void);
#endif
#ifdef CONFIG_VIDEO_LML33
extern int bt819_init(void);
extern int bt856_init(void);
#endif

int i2c_init(void)
{
	printk(KERN_INFO "i2c: initialized%s\n",
		scan ? " (i2c bus scan enabled)" : "");
	/* anything to do here ? */
#ifdef CONFIG_VIDEO_BT848
	i2c_tuner_init();
	msp3400c_init();
#endif	
#ifdef CONFIG_VIDEO_BUZ
	saa7111_init();
	saa7185_init();
#endif
#ifdef CONFIG_VIDEO_LML33
	bt819_init();
	bt856_init();
#endif
	return 0;
}

/* ----------------------------------------------------------------------- */

static void i2c_attach_device(struct i2c_bus *bus, struct i2c_driver *driver)
{
	struct i2c_device *device;
	int i,j,ack=1;
	unsigned char addr;
	LOCK_FLAGS;
    
	/* probe for device */
	LOCK_I2C_BUS(bus);
	for (addr = driver->addr_l; addr <= driver->addr_h; addr += 2) 
	{
		i2c_start(bus);
		ack = i2c_sendbyte(bus,addr,0);
		i2c_stop(bus);
		if (!ack)
			break;
	}
	UNLOCK_I2C_BUS(bus);
	if (ack)
		return;

	/* got answer */
	for (i = 0; i < I2C_DEVICE_MAX; i++)
		if (NULL == driver->devices[i])
			break;
	if (I2C_DEVICE_MAX == i)
		return;

	for (j = 0; j < I2C_DEVICE_MAX; j++)
		if (NULL == bus->devices[j])
			break;
	if (I2C_DEVICE_MAX == j)
		return;

	if (NULL == (device = kmalloc(sizeof(struct i2c_device),GFP_KERNEL)))
		return;
	device->bus = bus;
	device->driver = driver;
	device->addr = addr;

	/* Attach */
	
	if (driver->attach(device)!=0) 
	{
		kfree(device);
		return;
	}
	driver->devices[i] = device;
	driver->devcount++;
	bus->devices[j] = device;
	bus->devcount++;

	if (bus->attach_inform)
		bus->attach_inform(bus,driver->id);
	REGPRINT(printk("i2c: device attached: %s (addr=0x%02x, bus=%s, driver=%s)\n",device->name,addr,bus->name,driver->name));
}

static void i2c_detach_device(struct i2c_device *device)
{
	int i;

	if (device->bus->detach_inform)
		device->bus->detach_inform(device->bus,device->driver->id);
	device->driver->detach(device);

	for (i = 0; i < I2C_DEVICE_MAX; i++)
		if (device == device->driver->devices[i])
			break;
	if (I2C_DEVICE_MAX == i) 
	{
		printk(KERN_WARNING "i2c: detach_device #1: device not found: %s\n",
			device->name);
		return;
	}
	device->driver->devices[i] = NULL;
	device->driver->devcount--;

	for (i = 0; i < I2C_DEVICE_MAX; i++)
		if (device == device->bus->devices[i])
			break;
	if (I2C_DEVICE_MAX == i) 
	{
		printk(KERN_WARNING "i2c: detach_device #2: device not found: %s\n",
		       device->name);
		return;
	}
	device->bus->devices[i] = NULL;
	device->bus->devcount--;

	REGPRINT(printk("i2c: device detached: %s (addr=0x%02x, bus=%s, driver=%s)\n",device->name,device->addr,device->bus->name,device->driver->name));
	kfree(device);
}

/* ----------------------------------------------------------------------- */

int i2c_register_bus(struct i2c_bus *bus)
{
	int i,ack;
	LOCK_FLAGS;

	memset(bus->devices,0,sizeof(bus->devices));
	bus->devcount = 0;

	for (i = 0; i < I2C_BUS_MAX; i++)
		if (NULL == busses[i])
			break;
	if (I2C_BUS_MAX == i)
		return -ENOMEM;

	busses[i] = bus;
	bus_count++;
	REGPRINT(printk("i2c: bus registered: %s\n",bus->name));
	
	MOD_INC_USE_COUNT;

	if (scan) 
	{
		/* scan whole i2c bus */
		LOCK_I2C_BUS(bus);
		for (i = 0; i < 256; i+=2) 
		{
			i2c_start(bus);
			ack = i2c_sendbyte(bus,i,0);
			i2c_stop(bus);
			if (!ack) 
			{
				printk(KERN_INFO "i2c: scanning bus %s: found device at addr=0x%02x\n",
					bus->name,i);
			}
		}
		UNLOCK_I2C_BUS(bus);
	}

	/* probe available drivers */
	for (i = 0; i < I2C_DRIVER_MAX; i++)
		if (drivers[i])
			i2c_attach_device(bus,drivers[i]);
	return 0;
}

int i2c_unregister_bus(struct i2c_bus *bus)
{
	int i;

	/* detach devices */
	for (i = 0; i < I2C_DEVICE_MAX; i++)
		if (bus->devices[i])
			i2c_detach_device(bus->devices[i]);

	for (i = 0; i < I2C_BUS_MAX; i++)
		if (bus == busses[i])
			break;
	if (I2C_BUS_MAX == i) 
	{
		printk(KERN_WARNING "i2c: unregister_bus #1: bus not found: %s\n",
			bus->name);
		return -ENODEV;
	}
	
	MOD_DEC_USE_COUNT;
	
	busses[i] = NULL;
	bus_count--;
	REGPRINT(printk("i2c: bus unregistered: %s\n",bus->name));

	return 0;    
}

/* ----------------------------------------------------------------------- */

int i2c_register_driver(struct i2c_driver *driver)
{
	int i;

	memset(driver->devices,0,sizeof(driver->devices));
	driver->devcount = 0;

	for (i = 0; i < I2C_DRIVER_MAX; i++)
		if (NULL == drivers[i])
			break;
	if (I2C_DRIVER_MAX == i)
		return -ENOMEM;

	drivers[i] = driver;
	driver_count++;
	
	MOD_INC_USE_COUNT;
	
	REGPRINT(printk("i2c: driver registered: %s\n",driver->name));

	/* Probe available busses */
	for (i = 0; i < I2C_BUS_MAX; i++)
		if (busses[i])
			i2c_attach_device(busses[i],driver);

	return 0;
}

int i2c_unregister_driver(struct i2c_driver *driver)
{
	int i;

	/* detach devices */
	for (i = 0; i < I2C_DEVICE_MAX; i++)
		if (driver->devices[i])
			i2c_detach_device(driver->devices[i]);

	for (i = 0; i < I2C_DRIVER_MAX; i++)
		if (driver == drivers[i])
			break;
	if (I2C_DRIVER_MAX == i) 
	{
		printk(KERN_WARNING "i2c: unregister_driver: driver not found: %s\n",
			driver->name);
		return -ENODEV;
	}

	MOD_DEC_USE_COUNT;
	
	drivers[i] = NULL;
	driver_count--;
	REGPRINT(printk("i2c: driver unregistered: %s\n",driver->name));

	return 0;
}

/* ----------------------------------------------------------------------- */

int i2c_control_device(struct i2c_bus *bus, int id,
		       unsigned int cmd, void *arg)
{
	int i;

	for (i = 0; i < I2C_DEVICE_MAX; i++)
		if (bus->devices[i] && bus->devices[i]->driver->id == id)
			break;
	if (i == I2C_DEVICE_MAX)
		return -ENODEV;
	if (NULL == bus->devices[i]->driver->command)
		return -ENODEV;
	return bus->devices[i]->driver->command(bus->devices[i],cmd,arg);
}

/* ----------------------------------------------------------------------- */

#define I2C_SET(bus,ctrl,data)  (bus->i2c_setlines(bus,ctrl,data))
#define I2C_GET(bus)            (bus->i2c_getdataline(bus))

void i2c_start(struct i2c_bus *bus)
{
	I2C_SET(bus,0,1);
	I2C_SET(bus,1,1);
	I2C_SET(bus,1,0);
	I2C_SET(bus,0,0);
	I2C_DEBUG(printk("%s: < ",bus->name));
}

void i2c_stop(struct i2c_bus *bus)
{
	I2C_SET(bus,0,0);
	I2C_SET(bus,1,0);
	I2C_SET(bus,1,1);
	I2C_DEBUG(printk(">\n"));
}

void i2c_one(struct i2c_bus *bus)
{
	I2C_SET(bus,0,1);
	I2C_SET(bus,1,1);
	I2C_SET(bus,0,1);
}

void i2c_zero(struct i2c_bus *bus)
{
	I2C_SET(bus,0,0);
	I2C_SET(bus,1,0);
	I2C_SET(bus,0,0);
}

int i2c_ack(struct i2c_bus *bus)
{
	int ack;
    
	I2C_SET(bus,0,1);
	I2C_SET(bus,1,1);
	ack = I2C_GET(bus);
	I2C_SET(bus,0,1);
	return ack;
}

int i2c_sendbyte(struct i2c_bus *bus,unsigned char data,int wait_for_ack)
{
	int i, ack;
    
	I2C_SET(bus,0,0);
	for (i=7; i>=0; i--)
		(data&(1<<i)) ? i2c_one(bus) : i2c_zero(bus);
	if (wait_for_ack)
		udelay(wait_for_ack);
	ack=i2c_ack(bus);
	I2C_DEBUG(printk("%02x%c ",(int)data,ack?'-':'+'));
	return ack;
}

unsigned char i2c_readbyte(struct i2c_bus *bus,int last)
{
	int i;
	unsigned char data=0;
    
	I2C_SET(bus,0,1);
	for (i=7; i>=0; i--) 
	{
		I2C_SET(bus,1,1);
		if (I2C_GET(bus))
			data |= (1<<i);
		I2C_SET(bus,0,1);
	}
	last ? i2c_one(bus) : i2c_zero(bus);
	I2C_DEBUG(printk("=%02x%c ",(int)data,last?'-':'+'));
	return data;
}

/* ----------------------------------------------------------------------- */

int i2c_read(struct i2c_bus *bus, unsigned char addr)
{
	int ret;
    
	if (bus->i2c_read)
		return bus->i2c_read(bus, addr);

	i2c_start(bus);
	i2c_sendbyte(bus,addr,0);
	ret = i2c_readbyte(bus,1);
	i2c_stop(bus);
	return ret;
}

int i2c_write(struct i2c_bus *bus, unsigned char addr,
	      unsigned char data1, unsigned char data2, int both)
{
	int ack;

	if (bus->i2c_write)
		return bus->i2c_write(bus, addr, data1, data2, both);

	i2c_start(bus);
	i2c_sendbyte(bus,addr,0);
	ack = i2c_sendbyte(bus,data1,0);
	if (both)
		ack = i2c_sendbyte(bus,data2,0);
	i2c_stop(bus);
	return ack ? -1 : 0 ;
}

/* ----------------------------------------------------------------------- */

#ifdef MODULE

#if LINUX_VERSION_CODE >= 0x020100
EXPORT_SYMBOL(i2c_register_bus);
EXPORT_SYMBOL(i2c_unregister_bus);
EXPORT_SYMBOL(i2c_register_driver);
EXPORT_SYMBOL(i2c_unregister_driver);
EXPORT_SYMBOL(i2c_control_device);
EXPORT_SYMBOL(i2c_start);
EXPORT_SYMBOL(i2c_stop);
EXPORT_SYMBOL(i2c_one);
EXPORT_SYMBOL(i2c_zero);
EXPORT_SYMBOL(i2c_ack);
EXPORT_SYMBOL(i2c_sendbyte);
EXPORT_SYMBOL(i2c_readbyte);
EXPORT_SYMBOL(i2c_read);
EXPORT_SYMBOL(i2c_write);
#endif

int init_module(void)
{
	return i2c_init();
}

void cleanup_module(void)
{
}
#endif