aboutsummaryrefslogtreecommitdiffstats
path: root/bttv/experimental/i2c.c
blob: 2bd52a7d159d02106a1a62a952fd6fc3beca5aa1 (plain)
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
/* ------------------------------------------------------------------------- */
/* i2c.c - a device driver for the iic-bus interface			     */
/* ------------------------------------------------------------------------- */
/*   Copyright (C) 1995-97 Simon G. Vogl

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.		     */
/* ------------------------------------------------------------------------- */
#define RCSID "$Id: i2c.c,v 1.1 1998/05/25 12:08:09 i2c Exp i2c $"
/* ------------------------------------------------------------------------- */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/malloc.h>

#include "i2c.h"
 
/* ----- global defines ---------------------------------------------------- */
#define DEB(x)		/* should be reasonable open, close &c. 	*/
#define DEB2(x) 	/* low level debugging - very slow 		*/
#define DEBE(x)	x	/* error messages 				*/
#define DEBI(x) 	/* ioctl and its arguments 			*/

/* exclusive access to the bus */
#define I2C_LOCK(adap) spin_lock_irqsave(&adap->lock,adap->lockflags)
#define I2C_UNLOCK(adap) spin_unlock_irqrestore(&adap->lock,adap->lockflags)

/* ----- global variables -------------------------------------------------- */

/**** algorithm list */
struct i2c_algorithm *algorithms[I2C_ALGO_MAX];
static int algo_count;

/**** algorithm list */
struct i2c_adapter *adapters[I2C_ADAP_MAX];
static int adap_count;

/**** drivers list */
struct i2c_driver *drivers[I2C_DRIVER_MAX];
static int driver_count;

/**** clients list */
struct i2c_client *clients[I2C_CLIENT_MAX];
static int client_count;

/* ---------------------------------------------------    
 * registering functions 
 * --------------------------------------------------- 
 */

/* -----
 * Algorithms - used to access groups of similar hw adapters or
 * specific interfaces like the PCF8584 et al.
 */
int i2c_register_algorithm(struct i2c_algorithm *algo)
{
	int i;

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

	algorithms[i] = algo;
	algo_count++;
	MOD_INC_USE_COUNT;
	DEB(printk("i2c: algorithm %s registered.\n",algo->name));
	return 0;	
}


int i2c_unregister_algorithm(struct i2c_algorithm *algo)
{
	int i;

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

	algorithms[i] = NULL;
	algo_count--;
	MOD_DEC_USE_COUNT;
	DEB(printk("i2c: algorithm unregistered: %s\n",algo->name));
	return 0;    
}


/* -----
 * i2c_register_adapter is called from within the algorithm layer,
 * when a new hw adapter registers. A new device is register to be
 * available for clients.
 */
int i2c_register_adapter(struct i2c_adapter *adap)
{
	int i;

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

	adapters[i] = adap;
	adap_count++;
/*	MOD_INC_USE_COUNT;*/

	/* init data types */

	adap->lock = (spinlock_t)SPIN_LOCK_UNLOCKED;


	/* inform drivers of new adapters */
	for (i=0;i<I2C_DRIVER_MAX;i++)
		if (drivers[i]!=NULL && drivers[i]->flags&DF_NOTIFY)
			drivers[i]->attach_adapter(adap);

	DEB(printk("i2c: adapter %s registered.\n",adap->name));
	return 0;	
}

int i2c_unregister_adapter(struct i2c_adapter *adap)
{
	int i;

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

	adapters[i] = NULL;
	adap_count--;

/*	MOD_DEC_USE_COUNT;*/

	/* inform drivers */
	for (i=0;i<I2C_DRIVER_MAX;i++)
		if (drivers[i]!=NULL && drivers[i]->flags&DF_NOTIFY)
			drivers[i]->detach_adapter(adap);

	DEB(printk("i2c: adapter unregistered: %s\n",adap->name));
	return 0;    
}

/* -----
 * What follows is the "upwards" interface: commands for talking to clients,
 * which implement the functions to access the physical information of the
 * chips.
 */

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

	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;

	if ( driver->flags&DF_NOTIFY )
	for (i=0;i<I2C_ADAP_MAX;i++)
		if (adapters[i]!=NULL)
			driver->attach_adapter(adapters[i]);

	DEB(printk("i2c: driver %s registered.\n",driver->name));
	return 0;
}

int i2c_unregister_driver(struct i2c_driver *driver)
{
	int 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 %s not found\n",
			driver->name);
		return -ENODEV;
	}

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


int i2c_attach_client(struct i2c_client *client)
{
	struct i2c_adapter *adapter = client->adapter;
	struct i2c_algorithm *algo  = adapter->algo;
	int i;

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

	clients[i] = client;
	client_count++;
	if (algo->client_register != NULL) 
		algo->client_register(client);
	DEB(printk("i2c: client %s registered.\n",client->name));
	return 0;
}


int i2c_detach_client(struct i2c_client *client)
{
	struct i2c_adapter *adapter = client->adapter;
	struct i2c_algorithm *algo  = adapter->algo;
	int i;

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

	clients[i] = NULL;
	client_count--;
	if (algo->client_unregister != NULL) 
		algo->client_unregister(client);
	DEB(printk("i2c: client unregistered: %s\n",client->name));
	return 0;    
}

int i2c_init(void)
{
	/* clear algorithms */
	memset(algorithms,0,sizeof(algorithms));
	memset(adapters,0,sizeof(adapters));
	memset(clients,0,sizeof(clients));
	memset(drivers,0,sizeof(drivers));
	algo_count=0;
	adap_count=0;
	client_count=0;
	driver_count=0;
	
	printk("i2c module initialized.\n");
	return 0;
}


/* ----------------------------------------------------
 * the functional interface to the i2c busses.
 * ----------------------------------------------------
 */

int i2c_probe(struct i2c_adapter *adap, int low_addr, int hi_addr)
{
	struct i2c_client client;
	int i;

	memset(&client,0,sizeof(client));
	client.adapter=adap;
	I2C_LOCK(adap);
	for (i = low_addr; i <= hi_addr; i++) {
		client.addr=i;
		if (0 == adap->algo->master_send(&client,NULL,0))
			break;
	}
	I2C_UNLOCK(adap);
	return (i <= hi_addr) ? i : -1;
}

int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
{
	int ret;
	struct i2c_adapter *adap=client->adapter;
	DEB(printk("master_send: writing %d bytes on %s.\n",
		count,client->adapter->name));

	I2C_LOCK(adap);
	ret = adap->algo->master_send(client,buf,count);
	I2C_UNLOCK(adap);

	return ret;
}

int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
{
	int ret;
	struct i2c_adapter *adap=client->adapter;
	DEB(printk("master_recv: reading %d bytes on %s.\n",
		count,client->adapter->name));

	I2C_LOCK(adap);
	ret = adap->algo->master_recv(client,buf,count);
	I2C_UNLOCK(adap);
	DEB2(printk("master_recv: read %d bytes\n",ret));
	return ret;
}

int i2c_master_comb(struct i2c_client *client, char *readbuf,const char *writebuf, 
	int nread, int nwrite, int flags)
{
	int ret;
	struct i2c_adapter *adap=client->adapter;
	DEB(printk("master_comb: %s - %d in %d out on %s.\n",
		flags?"rd,wr":"wr,rd",nread,nwrite,
		client->adapter->name));

	I2C_LOCK(adap);
	ret = adap->algo->master_comb(client,readbuf,writebuf,
		nread,nwrite,flags);
	I2C_UNLOCK(adap);

	return ret;
}


int i2c_control(struct i2c_client *client,
	unsigned int cmd, unsigned long arg)
{
	int ret = 0;
	struct i2c_adapter *adap = client->adapter;

	DEBI(printk("i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg));
	switch ( cmd ) {
		case I2C_RETRIES:
			adap->retries = arg;
			break;
		case I2C_TIMEOUT:
			adap->timeout = arg;
			break;
		default:
			if (adap->algo->algo_control!=NULL)
				ret = adap->algo->algo_control(adap,cmd,arg);
	}
	return ret;
}


#ifdef MODULE
/*
EXPORT_SYMBOL(i2c_register_algorithm);
EXPORT_SYMBOL(i2c_unregister_algorithm);
*/

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

void cleanup_module(void) 
{
}
#endif

Privacy Policy