1 /* i2c-core.c - a device driver for the iic-bus interface */
2 /* ------------------------------------------------------------------------- */
3 /* Copyright (C) 1995-99 Simon G. Vogl
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* ------------------------------------------------------------------------- */
20 /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
21 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl> */
23 /* $Id: i2c-core.c,v 1.1.1.1 2005/04/11 02:50:21 jack Exp $ */
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/slab.h>
29 #include <linux/proc_fs.h>
30 #include <linux/config.h>
32 #include <linux/i2c.h>
34 /* ----- compatibility stuff ----------------------------------------------- */
36 #include <linux/version.h>
37 #include <linux/init.h>
39 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,1)
40 #define init_MUTEX(s) do { *(s) = MUTEX; } while(0)
43 #include <asm/uaccess.h>
45 /* ----- global defines ---------------------------------------------------- */
47 /* exclusive access to the bus */
48 #define I2C_LOCK(adap) down(&adap->lock)
49 #define I2C_UNLOCK(adap) up(&adap->lock)
51 #define ADAP_LOCK() down(&adap_lock)
52 #define ADAP_UNLOCK() up(&adap_lock)
54 #define DRV_LOCK() down(&driver_lock)
55 #define DRV_UNLOCK() up(&driver_lock)
57 #define DEB(x) if (i2c_debug>=1) x;
58 #define DEB2(x) if (i2c_debug>=2) x;
60 /* ----- global variables -------------------------------------------------- */
62 /**** lock for writing to global variables: the adapter & driver list */
63 struct semaphore adap_lock;
64 struct semaphore driver_lock;
67 static struct i2c_adapter *adapters[I2C_ADAP_MAX];
68 static int adap_count;
71 static struct i2c_driver *drivers[I2C_DRIVER_MAX];
72 static int driver_count;
75 static int i2c_debug=1;
77 /* ---------------------------------------------------
78 * /proc entry declarations
79 *----------------------------------------------------
84 static int i2cproc_init(void);
85 static int i2cproc_cleanup(void);
87 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,3,27))
88 static void monitor_bus_i2c(struct inode *inode, int fill);
89 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,58)) */
91 static ssize_t i2cproc_bus_read(struct file * file, char * buf,size_t count,
93 static int read_bus_i2c(char *buf, char **start, off_t offset, int len,
94 int *eof , void *private);
96 /* To implement the dynamic /proc/bus/i2c-? files, we need our own
97 implementation of the read hook */
98 static struct file_operations i2cproc_operations = {
99 read: i2cproc_bus_read,
102 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,48))
103 static struct inode_operations i2cproc_inode_operations = {
108 static int i2cproc_initialized = 0;
110 #else /* undef CONFIG_PROC_FS */
112 #define i2cproc_init() 0
113 #define i2cproc_cleanup() 0
115 #endif /* CONFIG_PROC_FS */
118 /* ---------------------------------------------------
119 * registering functions
120 * ---------------------------------------------------
124 * i2c_add_adapter is called from within the algorithm layer,
125 * when a new hw adapter registers. A new device is register to be
126 * available for clients.
128 int i2c_add_adapter(struct i2c_adapter *adap)
133 for (i = 0; i < I2C_ADAP_MAX; i++)
134 if (NULL == adapters[i])
136 if (I2C_ADAP_MAX == i) {
138 " i2c-core.o: register_adapter(%s) - enlarge I2C_ADAP_MAX.\n",
148 /* init data types */
149 init_MUTEX(&adap->lock);
151 #ifdef CONFIG_PROC_FS
153 if (i2cproc_initialized) {
155 struct proc_dir_entry *proc_entry;
157 sprintf(name,"i2c-%d", i);
159 proc_entry = create_proc_entry(name,0,proc_bus);
161 printk("i2c-core.o: Could not create /proc/bus/%s\n",
167 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,48))
168 proc_entry->proc_fops = &i2cproc_operations;
170 proc_entry->ops = &i2cproc_inode_operations;
172 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,27))
173 proc_entry->owner = THIS_MODULE;
175 proc_entry->fill_inode = &monitor_bus_i2c;
176 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2,1,58)) */
177 adap->inode = proc_entry->low_ino;
180 #endif /* def CONFIG_PROC_FS */
182 /* inform drivers of new adapters */
184 for (j=0;j<I2C_DRIVER_MAX;j++)
185 if (drivers[j]!=NULL &&
186 (drivers[j]->flags&(I2C_DF_NOTIFY|I2C_DF_DUMMY)))
187 /* We ignore the return code; if it fails, too bad */
188 drivers[j]->attach_adapter(adap);
191 DEB(printk("i2c-core.o: adapter %s registered as adapter %d.\n",
207 int i2c_del_adapter(struct i2c_adapter *adap)
213 for (i = 0; i < I2C_ADAP_MAX; i++)
214 if (adap == adapters[i])
216 if (I2C_ADAP_MAX == i) {
217 printk( "i2c-core.o: unregister_adapter adap [%s] not found.\n",
223 /* DUMMY drivers do not register their clients, so we have to
224 * use a trick here: we call driver->attach_adapter to
225 * *detach* it! Of course, each dummy driver should know about
226 * this or hell will break loose...
229 for (j = 0; j < I2C_DRIVER_MAX; j++)
230 if (drivers[j] && (drivers[j]->flags & I2C_DF_DUMMY))
231 if ((res = drivers[j]->attach_adapter(adap))) {
232 printk("i2c-core.o: can't detach adapter %s "
233 "while detaching driver %s: driver not "
234 "detached!",adap->name,drivers[j]->name);
240 /* detach any active clients. This must be done first, because
241 * it can fail; in which case we give upp. */
242 for (j=0;j<I2C_CLIENT_MAX;j++) {
243 struct i2c_client *client = adap->clients[j];
245 /* detaching devices is unconditional of the set notify
246 * flag, as _all_ clients that reside on the adapter
247 * must be deleted, as this would cause invalid states.
249 if ((res=client->driver->detach_client(client))) {
250 printk("i2c-core.o: adapter %s not "
251 "unregistered, because client at "
252 "address %02x can't be detached. ",
253 adap->name, client->addr);
257 #ifdef CONFIG_PROC_FS
258 if (i2cproc_initialized) {
260 sprintf(name,"i2c-%d", i);
261 remove_proc_entry(name,proc_bus);
263 #endif /* def CONFIG_PROC_FS */
269 DEB(printk("i2c-core.o: adapter unregistered: %s\n",adap->name));
282 * What follows is the "upwards" interface: commands for talking to clients,
283 * which implement the functions to access the physical information of the
287 int i2c_add_driver(struct i2c_driver *driver)
291 for (i = 0; i < I2C_DRIVER_MAX; i++)
292 if (NULL == drivers[i])
294 if (I2C_DRIVER_MAX == i) {
296 " i2c-core.o: register_driver(%s) "
297 "- enlarge I2C_DRIVER_MAX.\n",
306 DRV_UNLOCK(); /* driver was successfully added */
308 DEB(printk("i2c-core.o: driver %s registered.\n",driver->name));
312 /* now look for instances of driver on our adapters
314 if (driver->flags& (I2C_DF_NOTIFY|I2C_DF_DUMMY)) {
315 for (i=0;i<I2C_ADAP_MAX;i++)
316 if (adapters[i]!=NULL)
318 driver->attach_adapter(adapters[i]);
324 int i2c_del_driver(struct i2c_driver *driver)
329 for (i = 0; i < I2C_DRIVER_MAX; i++)
330 if (driver == drivers[i])
332 if (I2C_DRIVER_MAX == i) {
333 printk(KERN_WARNING " i2c-core.o: unregister_driver: "
339 /* Have a look at each adapter, if clients of this driver are still
340 * attached. If so, detach them to be able to kill the driver
343 DEB2(printk("i2c-core.o: unregister_driver - looking for clients.\n"));
344 /* removing clients does not depend on the notify flag, else
345 * invalid operation might (will!) result, when using stale client
348 ADAP_LOCK(); /* should be moved inside the if statement... */
349 for (k=0;k<I2C_ADAP_MAX;k++) {
350 struct i2c_adapter *adap = adapters[k];
351 if (adap == NULL) /* skip empty entries. */
353 DEB2(printk("i2c-core.o: examining adapter %s:\n",
355 if (driver->flags & I2C_DF_DUMMY) {
356 /* DUMMY drivers do not register their clients, so we have to
357 * use a trick here: we call driver->attach_adapter to
358 * *detach* it! Of course, each dummy driver should know about
359 * this or hell will break loose...
361 if ((res = driver->attach_adapter(adap))) {
362 printk("i2c-core.o: while unregistering "
363 "dummy driver %s, adapter %s could "
364 "not be detached properly; driver "
365 "not unloaded!",driver->name,
371 for (j=0;j<I2C_CLIENT_MAX;j++) {
372 struct i2c_client *client = adap->clients[j];
373 if (client != NULL &&
374 client->driver == driver) {
375 DEB2(printk("i2c-core.o: "
376 "detaching client %s:\n",
379 detach_client(client)))
381 printk("i2c-core.o: while "
382 "unregistering driver "
383 "`%s', the client at "
385 "adapter `%s' could not"
386 "be detached; driver"
403 DEB(printk("i2c-core.o: driver unregistered: %s\n",driver->name));
407 int i2c_check_addr (struct i2c_adapter *adapter, int addr)
410 for (i = 0; i < I2C_CLIENT_MAX ; i++)
411 if (adapter->clients[i] && (adapter->clients[i]->addr == addr))
416 int i2c_attach_client(struct i2c_client *client)
418 struct i2c_adapter *adapter = client->adapter;
421 if (i2c_check_addr(client->adapter,client->addr))
424 for (i = 0; i < I2C_CLIENT_MAX; i++)
425 if (NULL == adapter->clients[i])
427 if (I2C_CLIENT_MAX == i) {
429 " i2c-core.o: attach_client(%s) - enlarge I2C_CLIENT_MAX.\n",
434 adapter->clients[i] = client;
435 adapter->client_count++;
437 if (adapter->client_register)
438 if (adapter->client_register(client))
439 printk("i2c-core.o: warning: client_register seems "
440 "to have failed for client %02x at adapter %s\n",
441 client->addr,adapter->name);
442 DEB(printk("i2c-core.o: client [%s] registered to adapter [%s](pos. %d).\n",
443 client->name, adapter->name,i));
445 if(client->flags & I2C_CLIENT_ALLOW_USE)
446 client->usage_count = 0;
452 int i2c_detach_client(struct i2c_client *client)
454 struct i2c_adapter *adapter = client->adapter;
457 for (i = 0; i < I2C_CLIENT_MAX; i++)
458 if (client == adapter->clients[i])
460 if (I2C_CLIENT_MAX == i) {
461 printk(KERN_WARNING " i2c-core.o: unregister_client "
467 if( (client->flags & I2C_CLIENT_ALLOW_USE) &&
468 (client->usage_count>0))
471 if (adapter->client_unregister != NULL)
472 if ((res = adapter->client_unregister(client))) {
473 printk("i2c-core.o: client_unregister [%s] failed, "
474 "client not detached",client->name);
478 adapter->clients[i] = NULL;
479 adapter->client_count--;
481 DEB(printk("i2c-core.o: client [%s] unregistered.\n",client->name));
485 void i2c_inc_use_client(struct i2c_client *client)
488 if (client->driver->inc_use != NULL)
489 client->driver->inc_use(client);
491 if (client->adapter->inc_use != NULL)
492 client->adapter->inc_use(client->adapter);
495 void i2c_dec_use_client(struct i2c_client *client)
498 if (client->driver->dec_use != NULL)
499 client->driver->dec_use(client);
501 if (client->adapter->dec_use != NULL)
502 client->adapter->dec_use(client->adapter);
505 struct i2c_client *i2c_get_client(int driver_id, int adapter_id,
506 struct i2c_client *prev)
510 /* Will iterate through the list of clients in each adapter of adapters-list
511 in search for a client that matches the search criteria. driver_id or
512 adapter_id are ignored if set to 0. If both are ignored this returns
513 first client found. */
517 /* set starting point */
521 return (struct i2c_client *) -EINVAL;
523 for(j=0; j < I2C_ADAP_MAX; j++)
524 if(prev->adapter == adapters[j])
527 /* invalid starting point? */
528 if (I2C_ADAP_MAX == j) {
529 printk(KERN_WARNING " i2c-core.o: get_client adapter for client:[%s] not found\n",
531 return (struct i2c_client *) -ENODEV;
534 for(i=0; i < I2C_CLIENT_MAX; i++)
535 if(prev == adapters[j]->clients[i])
538 /* invalid starting point? */
539 if (I2C_CLIENT_MAX == i) {
540 printk(KERN_WARNING " i2c-core.o: get_client client:[%s] not found\n",
542 return (struct i2c_client *) -ENODEV;
545 i++; /* start from one after prev */
548 for(; j < I2C_ADAP_MAX; j++)
553 if(adapter_id && (adapters[j]->id != adapter_id))
556 for(; i < I2C_CLIENT_MAX; i++)
558 if(!adapters[j]->clients[i])
561 if(driver_id && (adapters[j]->clients[i]->driver->id != driver_id))
563 if(adapters[j]->clients[i]->flags & I2C_CLIENT_ALLOW_USE)
564 return adapters[j]->clients[i];
572 int i2c_use_client(struct i2c_client *client)
574 if(client->flags & I2C_CLIENT_ALLOW_USE) {
575 if (client->flags & I2C_CLIENT_ALLOW_MULTIPLE_USE)
576 client->usage_count++;
578 if(client->usage_count > 0)
581 client->usage_count++;
585 i2c_inc_use_client(client);
590 int i2c_release_client(struct i2c_client *client)
592 if(client->flags & I2C_CLIENT_ALLOW_USE) {
593 if(client->usage_count>0)
594 client->usage_count--;
597 printk(KERN_WARNING " i2c-core.o: dec_use_client used one too many times\n");
602 i2c_dec_use_client(client);
607 /* ----------------------------------------------------
608 * The /proc functions
609 * ----------------------------------------------------
612 #ifdef CONFIG_PROC_FS
614 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,3,27))
615 /* Monitor access to /proc/bus/i2c*; make unloading i2c-proc impossible
616 if some process still uses it or some file in it */
617 void monitor_bus_i2c(struct inode *inode, int fill)
624 #endif /* (LINUX_VERSION_CODE <= KERNEL_VERSION(2,3,37)) */
626 /* This function generates the output for /proc/bus/i2c */
627 int read_bus_i2c(char *buf, char **start, off_t offset, int len, int *eof,
632 /* Note that it is safe to write a `little' beyond len. Yes, really. */
633 for (i = 0; (i < I2C_ADAP_MAX) && (nr < len); i++)
635 nr += sprintf(buf+nr, "i2c-%d\t", i);
636 if (adapters[i]->algo->smbus_xfer) {
637 if (adapters[i]->algo->master_xfer)
638 nr += sprintf(buf+nr,"smbus/i2c");
640 nr += sprintf(buf+nr,"smbus ");
641 } else if (adapters[i]->algo->master_xfer)
642 nr += sprintf(buf+nr,"i2c ");
644 nr += sprintf(buf+nr,"dummy ");
645 nr += sprintf(buf+nr,"\t%-32s\t%-32s\n",
647 adapters[i]->algo->name);
652 /* This function generates the output for /proc/bus/i2c-? */
653 ssize_t i2cproc_bus_read(struct file * file, char * buf,size_t count,
656 struct inode * inode = file->f_dentry->d_inode;
658 struct i2c_client *client;
659 int i,j,k,order_nr,len=0;
661 int order[I2C_CLIENT_MAX];
665 len_total = file->f_pos + count;
666 /* Too bad if this gets longer (unlikely) */
667 if (len_total > 4000)
669 for (i = 0; i < I2C_ADAP_MAX; i++)
670 if (adapters[i]->inode == inode->i_ino) {
671 /* We need a bit of slack in the kernel buffer; this makes the
673 if (! (kbuf = kmalloc(count + 80,GFP_KERNEL)))
675 /* Order will hold the indexes of the clients
678 for (j = 0; j < I2C_CLIENT_MAX; j++) {
679 if ((client = adapters[i]->clients[j]) &&
680 (client->driver->id != I2C_DRIVERID_I2CDEV)) {
683 adapters[i]->clients[order[k-1]]->
686 order[k] = order[k-1];
693 for (j = 0; (j < order_nr) && (len < len_total); j++) {
694 client = adapters[i]->clients[order[j]];
695 len += sprintf(kbuf+len,"%02x\t%-32s\t%-32s\n",
698 client->driver->name);
700 len = len - file->f_pos;
705 if (copy_to_user (buf,kbuf+file->f_pos, len)) {
716 int i2cproc_init(void)
719 struct proc_dir_entry *proc_bus_i2c;
721 i2cproc_initialized = 0;
724 printk("i2c-core.o: /proc/bus/ does not exist");
728 proc_bus_i2c = create_proc_entry("i2c",0,proc_bus);
730 printk("i2c-core.o: Could not create /proc/bus/i2c");
734 proc_bus_i2c->read_proc = &read_bus_i2c;
735 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,27))
736 proc_bus_i2c->owner = THIS_MODULE;
738 proc_bus_i2c->fill_inode = &monitor_bus_i2c;
739 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,27)) */
740 i2cproc_initialized += 2;
744 int i2cproc_cleanup(void)
747 if (i2cproc_initialized >= 1) {
748 remove_proc_entry("i2c",proc_bus);
749 i2cproc_initialized -= 2;
755 #endif /* def CONFIG_PROC_FS */
757 /* ----------------------------------------------------
758 * the functional interface to the i2c busses.
759 * ----------------------------------------------------
762 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg msgs[],int num)
766 if (adap->algo->master_xfer) {
767 DEB2(printk("i2c-core.o: master_xfer: %s with %d msgs.\n",
771 ret = adap->algo->master_xfer(adap,msgs,num);
776 printk("i2c-core.o: I2C adapter %04x: I2C level transfers not supported\n",
782 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
785 struct i2c_adapter *adap=client->adapter;
788 if (client->adapter->algo->master_xfer) {
789 msg.addr = client->addr;
790 msg.flags = client->flags & I2C_M_TEN;
792 (const char *)msg.buf = buf;
794 DEB2(printk("i2c-core.o: master_send: writing %d bytes on %s.\n",
795 count,client->adapter->name));
798 ret = adap->algo->master_xfer(adap,&msg,1);
801 /* if everything went ok (i.e. 1 msg transmitted), return #bytes
802 * transmitted, else error code.
804 return (ret == 1 )? count : ret;
806 printk("i2c-core.o: I2C adapter %04x: I2C level transfers not supported\n",
807 client->adapter->id);
812 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
814 struct i2c_adapter *adap=client->adapter;
817 if (client->adapter->algo->master_xfer) {
818 msg.addr = client->addr;
819 msg.flags = client->flags & I2C_M_TEN;
820 msg.flags |= I2C_M_RD;
824 DEB2(printk("i2c-core.o: master_recv: reading %d bytes on %s.\n",
825 count,client->adapter->name));
828 ret = adap->algo->master_xfer(adap,&msg,1);
831 DEB2(printk("i2c-core.o: master_recv: return:%d (count:%d, addr:0x%02x)\n",
832 ret, count, client->addr));
834 /* if everything went ok (i.e. 1 msg transmitted), return #bytes
835 * transmitted, else error code.
837 return (ret == 1 )? count : ret;
839 printk("i2c-core.o: I2C adapter %04x: I2C level transfers not supported\n",
840 client->adapter->id);
846 int i2c_control(struct i2c_client *client,
847 unsigned int cmd, unsigned long arg)
850 struct i2c_adapter *adap = client->adapter;
852 DEB2(printk("i2c-core.o: i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg));
861 if (adap->algo->algo_control!=NULL)
862 ret = adap->algo->algo_control(adap,cmd,arg);
867 /* ----------------------------------------------------
868 * the i2c address scanning function
869 * Will not work for 10-bit addresses!
870 * ----------------------------------------------------
872 int i2c_probe(struct i2c_adapter *adapter,
873 struct i2c_client_address_data *address_data,
874 i2c_client_found_addr_proc *found_proc)
876 int addr,i,found,err;
877 int adap_id = i2c_adapter_id(adapter);
879 /* Forget it if we can't probe using SMBUS_QUICK */
880 if (! i2c_check_functionality(adapter,I2C_FUNC_SMBUS_QUICK))
883 for (addr = 0x00; addr <= 0x7f; addr++) {
885 /* Skip if already in use */
886 if (i2c_check_addr(adapter,addr))
889 /* If it is in one of the force entries, we don't do any detection
893 for (i = 0; !found && (address_data->force[i] != I2C_CLIENT_END); i += 3) {
894 if (((adap_id == address_data->force[i]) ||
895 (address_data->force[i] == ANY_I2C_BUS)) &&
896 (addr == address_data->force[i+1])) {
897 DEB2(printk("i2c-core.o: found force parameter for adapter %d, addr %04x\n",
899 if ((err = found_proc(adapter,addr,0,0)))
907 /* If this address is in one of the ignores, we can forget about
910 !found && (address_data->ignore[i] != I2C_CLIENT_END);
912 if (((adap_id == address_data->ignore[i]) ||
913 ((address_data->ignore[i] == ANY_I2C_BUS))) &&
914 (addr == address_data->ignore[i+1])) {
915 DEB2(printk("i2c-core.o: found ignore parameter for adapter %d, "
916 "addr %04x\n", adap_id ,addr));
921 !found && (address_data->ignore_range[i] != I2C_CLIENT_END);
923 if (((adap_id == address_data->ignore_range[i]) ||
924 ((address_data->ignore_range[i]==ANY_I2C_BUS))) &&
925 (addr >= address_data->ignore_range[i+1]) &&
926 (addr <= address_data->ignore_range[i+2])) {
927 DEB2(printk("i2c-core.o: found ignore_range parameter for adapter %d, "
928 "addr %04x\n", adap_id,addr));
935 /* Now, we will do a detection, but only if it is in the normal or
938 !found && (address_data->normal_i2c[i] != I2C_CLIENT_END);
940 if (addr == address_data->normal_i2c[i]) {
942 DEB2(printk("i2c-core.o: found normal i2c entry for adapter %d, "
943 "addr %02x", adap_id,addr));
948 !found && (address_data->normal_i2c_range[i] != I2C_CLIENT_END);
950 if ((addr >= address_data->normal_i2c_range[i]) &&
951 (addr <= address_data->normal_i2c_range[i+1])) {
953 DEB2(printk("i2c-core.o: found normal i2c_range entry for adapter %d, "
954 "addr %04x\n", adap_id,addr));
959 !found && (address_data->probe[i] != I2C_CLIENT_END);
961 if (((adap_id == address_data->probe[i]) ||
962 ((address_data->probe[i] == ANY_I2C_BUS))) &&
963 (addr == address_data->probe[i+1])) {
965 DEB2(printk("i2c-core.o: found probe parameter for adapter %d, "
966 "addr %04x\n", adap_id,addr));
970 !found && (address_data->probe_range[i] != I2C_CLIENT_END);
972 if (((adap_id == address_data->probe_range[i]) ||
973 (address_data->probe_range[i] == ANY_I2C_BUS)) &&
974 (addr >= address_data->probe_range[i+1]) &&
975 (addr <= address_data->probe_range[i+2])) {
977 DEB2(printk("i2c-core.o: found probe_range parameter for adapter %d, "
978 "addr %04x\n", adap_id,addr));
984 /* OK, so we really should examine this address. First check
985 whether there is some client here at all! */
986 if (i2c_smbus_xfer(adapter,addr,0,0,0,I2C_SMBUS_QUICK,NULL) >= 0)
987 if ((err = found_proc(adapter,addr,0,-1)))
994 * return id number for a specific adapter
996 int i2c_adapter_id(struct i2c_adapter *adap)
999 for (i = 0; i < I2C_ADAP_MAX; i++)
1000 if (adap == adapters[i])
1005 /* The SMBus parts */
1007 extern s32 i2c_smbus_write_quick(struct i2c_client * client, u8 value)
1009 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1010 value,0,I2C_SMBUS_QUICK,NULL);
1013 extern s32 i2c_smbus_read_byte(struct i2c_client * client)
1015 union i2c_smbus_data data;
1016 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1017 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
1020 return 0x0FF & data.byte;
1023 extern s32 i2c_smbus_write_byte(struct i2c_client * client, u8 value)
1025 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1026 I2C_SMBUS_WRITE,value, I2C_SMBUS_BYTE,NULL);
1029 extern s32 i2c_smbus_read_byte_data(struct i2c_client * client, u8 command)
1031 union i2c_smbus_data data;
1032 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1033 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
1036 return 0x0FF & data.byte;
1039 extern s32 i2c_smbus_write_byte_data(struct i2c_client * client, u8 command,
1042 union i2c_smbus_data data;
1044 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1045 I2C_SMBUS_WRITE,command,
1046 I2C_SMBUS_BYTE_DATA,&data);
1049 extern s32 i2c_smbus_read_word_data(struct i2c_client * client, u8 command)
1051 union i2c_smbus_data data;
1052 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1053 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1056 return 0x0FFFF & data.word;
1059 extern s32 i2c_smbus_write_word_data(struct i2c_client * client,
1060 u8 command, u16 value)
1062 union i2c_smbus_data data;
1064 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1065 I2C_SMBUS_WRITE,command,
1066 I2C_SMBUS_WORD_DATA,&data);
1069 extern s32 i2c_smbus_process_call(struct i2c_client * client,
1070 u8 command, u16 value)
1072 union i2c_smbus_data data;
1074 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1075 I2C_SMBUS_WRITE,command,
1076 I2C_SMBUS_PROC_CALL, &data))
1079 return 0x0FFFF & data.word;
1082 /* Returns the number of read bytes */
1083 extern s32 i2c_smbus_read_block_data(struct i2c_client * client,
1084 u8 command, u8 *values)
1086 union i2c_smbus_data data;
1088 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1089 I2C_SMBUS_READ,command,
1090 I2C_SMBUS_BLOCK_DATA,&data))
1093 for (i = 1; i <= data.block[0]; i++)
1094 values[i-1] = data.block[i];
1095 return data.block[0];
1099 extern s32 i2c_smbus_write_block_data(struct i2c_client * client,
1100 u8 command, u8 length, u8 *values)
1102 union i2c_smbus_data data;
1106 for (i = 1; i <= length; i++)
1107 data.block[i] = values[i-1];
1108 data.block[0] = length;
1109 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1110 I2C_SMBUS_WRITE,command,
1111 I2C_SMBUS_BLOCK_DATA,&data);
1114 extern s32 i2c_smbus_write_i2c_block_data(struct i2c_client * client,
1115 u8 command, u8 length, u8 *values)
1117 union i2c_smbus_data data;
1121 for (i = 1; i <= length; i++)
1122 data.block[i] = values[i-1];
1123 data.block[0] = length;
1124 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1125 I2C_SMBUS_WRITE,command,
1126 I2C_SMBUS_I2C_BLOCK_DATA,&data);
1129 /* Simulate a SMBus command using the i2c protocol
1130 No checking of parameters is done! */
1131 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1132 unsigned short flags,
1133 char read_write, u8 command, int size,
1134 union i2c_smbus_data * data)
1136 /* So we need to generate a series of msgs. In the case of writing, we
1137 need to use only one message; when reading, we need two. We initialize
1138 most things with sane defaults, to keep the code below somewhat
1140 unsigned char msgbuf0[34];
1141 unsigned char msgbuf1[34];
1142 int num = read_write == I2C_SMBUS_READ?2:1;
1143 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1144 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1148 msgbuf0[0] = command;
1150 case I2C_SMBUS_QUICK:
1152 /* Special case: The read/write field is used as data */
1153 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1156 case I2C_SMBUS_BYTE:
1157 if (read_write == I2C_SMBUS_READ) {
1158 /* Special case: only a read! */
1159 msg[0].flags = I2C_M_RD | flags;
1163 case I2C_SMBUS_BYTE_DATA:
1164 if (read_write == I2C_SMBUS_READ)
1168 msgbuf0[1] = data->byte;
1171 case I2C_SMBUS_WORD_DATA:
1172 if (read_write == I2C_SMBUS_READ)
1176 msgbuf0[1] = data->word & 0xff;
1177 msgbuf0[2] = (data->word >> 8) & 0xff;
1180 case I2C_SMBUS_PROC_CALL:
1181 num = 2; /* Special case */
1184 msgbuf0[1] = data->word & 0xff;
1185 msgbuf0[2] = (data->word >> 8) & 0xff;
1187 case I2C_SMBUS_BLOCK_DATA:
1188 if (read_write == I2C_SMBUS_READ) {
1189 printk("i2c-core.o: Block read not supported under "
1190 "I2C emulation!\n");
1193 msg[0].len = data->block[0] + 2;
1194 if (msg[0].len > 34) {
1195 printk("i2c-core.o: smbus_access called with "
1196 "invalid block write size (%d)\n",
1200 for (i = 1; i <= msg[0].len; i++)
1201 msgbuf0[i] = data->block[i-1];
1205 printk("i2c-core.o: smbus_access called with invalid size (%d)\n",
1210 if (i2c_transfer(adapter, msg, num) < 0)
1213 if (read_write == I2C_SMBUS_READ)
1215 case I2C_SMBUS_BYTE:
1216 data->byte = msgbuf0[0];
1218 case I2C_SMBUS_BYTE_DATA:
1219 data->byte = msgbuf1[0];
1221 case I2C_SMBUS_WORD_DATA:
1222 case I2C_SMBUS_PROC_CALL:
1223 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1230 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1231 char read_write, u8 command, int size,
1232 union i2c_smbus_data * data)
1235 flags = flags & I2C_M_TEN;
1236 if (adapter->algo->smbus_xfer) {
1238 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1240 I2C_UNLOCK(adapter);
1242 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1248 /* You should always define `functionality'; the 'else' is just for
1249 backward compatibility. */
1250 u32 i2c_get_functionality (struct i2c_adapter *adap)
1252 if (adap->algo->functionality)
1253 return adap->algo->functionality(adap);
1258 int i2c_check_functionality (struct i2c_adapter *adap, u32 func)
1260 u32 adap_func = i2c_get_functionality (adap);
1261 return (func & adap_func) == func;
1265 static int __init i2c_init(void)
1267 printk("i2c-core.o: i2c core module\n");
1268 memset(adapters,0,sizeof(adapters));
1269 memset(drivers,0,sizeof(drivers));
1273 init_MUTEX(&adap_lock);
1274 init_MUTEX(&driver_lock);
1282 #ifdef CONFIG_I2C_CHARDEV
1283 extern int i2c_dev_init(void);
1285 #ifdef CONFIG_I2C_ALGOBIT
1286 extern int i2c_algo_bit_init(void);
1288 #ifdef CONFIG_I2C_PHILIPSPAR
1289 extern int i2c_bitlp_init(void);
1291 #ifdef CONFIG_I2C_ELV
1292 extern int i2c_bitelv_init(void);
1294 #ifdef CONFIG_I2C_VELLEMAN
1295 extern int i2c_bitvelle_init(void);
1297 #ifdef CONFIG_I2C_BITVIA
1298 extern int i2c_bitvia_init(void);
1301 #ifdef CONFIG_I2C_ALGOPCF
1302 extern int i2c_algo_pcf_init(void);
1304 #ifdef CONFIG_I2C_ELEKTOR
1305 extern int i2c_pcfisa_init(void);
1308 #ifdef CONFIG_I2C_ALGO8XX
1309 extern int i2c_algo_8xx_init(void);
1311 #ifdef CONFIG_I2C_RPXLITE
1312 extern int i2c_rpx_init(void);
1314 #ifdef CONFIG_I2C_PROC
1315 extern int sensors_init(void);
1318 /* This is needed for automatic patch generation: sensors code starts here */
1319 /* This is needed for automatic patch generation: sensors code ends here */
1321 int __init i2c_init_all(void)
1323 /* --------------------- global ----- */
1326 #ifdef CONFIG_I2C_CHARDEV
1329 /* --------------------- bit -------- */
1330 #ifdef CONFIG_I2C_ALGOBIT
1331 i2c_algo_bit_init();
1333 #ifdef CONFIG_I2C_PHILIPSPAR
1336 #ifdef CONFIG_I2C_ELV
1339 #ifdef CONFIG_I2C_VELLEMAN
1340 i2c_bitvelle_init();
1343 /* --------------------- pcf -------- */
1344 #ifdef CONFIG_I2C_ALGOPCF
1345 i2c_algo_pcf_init();
1347 #ifdef CONFIG_I2C_ELEKTOR
1351 /* --------------------- 8xx -------- */
1352 #ifdef CONFIG_I2C_ALGO8XX
1353 i2c_algo_8xx_init();
1355 #ifdef CONFIG_I2C_RPXLITE
1359 /* -------------- proc interface ---- */
1360 #ifdef CONFIG_I2C_PROC
1363 /* This is needed for automatic patch generation: sensors code starts here */
1364 /* This is needed for automatic patch generation: sensors code ends here */
1373 EXPORT_SYMBOL(i2c_add_adapter);
1374 EXPORT_SYMBOL(i2c_del_adapter);
1375 EXPORT_SYMBOL(i2c_add_driver);
1376 EXPORT_SYMBOL(i2c_del_driver);
1377 EXPORT_SYMBOL(i2c_attach_client);
1378 EXPORT_SYMBOL(i2c_detach_client);
1379 EXPORT_SYMBOL(i2c_inc_use_client);
1380 EXPORT_SYMBOL(i2c_dec_use_client);
1381 EXPORT_SYMBOL(i2c_get_client);
1382 EXPORT_SYMBOL(i2c_use_client);
1383 EXPORT_SYMBOL(i2c_release_client);
1384 EXPORT_SYMBOL(i2c_check_addr);
1387 EXPORT_SYMBOL(i2c_master_send);
1388 EXPORT_SYMBOL(i2c_master_recv);
1389 EXPORT_SYMBOL(i2c_control);
1390 EXPORT_SYMBOL(i2c_transfer);
1391 EXPORT_SYMBOL(i2c_adapter_id);
1392 EXPORT_SYMBOL(i2c_probe);
1394 EXPORT_SYMBOL(i2c_smbus_xfer);
1395 EXPORT_SYMBOL(i2c_smbus_write_quick);
1396 EXPORT_SYMBOL(i2c_smbus_read_byte);
1397 EXPORT_SYMBOL(i2c_smbus_write_byte);
1398 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1399 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1400 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1401 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1402 EXPORT_SYMBOL(i2c_smbus_process_call);
1403 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1404 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1406 EXPORT_SYMBOL(i2c_get_functionality);
1407 EXPORT_SYMBOL(i2c_check_functionality);
1410 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1411 MODULE_DESCRIPTION("I2C-Bus main module");
1412 MODULE_PARM(i2c_debug, "i");
1413 MODULE_PARM_DESC(i2c_debug,"debug level");
1414 MODULE_LICENSE("GPL");
1416 int init_module(void)
1421 void cleanup_module(void)