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.64 2001/08/13 01:35:56 mds 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/init.h>
38 #include <asm/uaccess.h>
40 /* ----- global defines ---------------------------------------------------- */
42 /* exclusive access to the bus */
43 #define I2C_LOCK(adap) down(&adap->lock)
44 #define I2C_UNLOCK(adap) up(&adap->lock)
46 #define ADAP_LOCK() down(&adap_lock)
47 #define ADAP_UNLOCK() up(&adap_lock)
49 #define DRV_LOCK() down(&driver_lock)
50 #define DRV_UNLOCK() up(&driver_lock)
52 #define DEB(x) if (i2c_debug>=1) x;
53 #define DEB2(x) if (i2c_debug>=2) x;
55 /* ----- global variables -------------------------------------------------- */
57 /**** lock for writing to global variables: the adapter & driver list */
58 struct semaphore adap_lock;
59 struct semaphore driver_lock;
62 static struct i2c_adapter *adapters[I2C_ADAP_MAX];
63 static int adap_count;
66 static struct i2c_driver *drivers[I2C_DRIVER_MAX];
67 static int driver_count;
70 static int i2c_debug=1;
72 /* ---------------------------------------------------
73 * /proc entry declarations
74 *----------------------------------------------------
79 static int i2cproc_init(void);
80 static int i2cproc_cleanup(void);
82 static ssize_t i2cproc_bus_read(struct file * file, char * buf,size_t count,
84 static int read_bus_i2c(char *buf, char **start, off_t offset, int len,
85 int *eof , void *private);
87 /* To implement the dynamic /proc/bus/i2c-? files, we need our own
88 implementation of the read hook */
89 static struct file_operations i2cproc_operations = {
90 .read = i2cproc_bus_read,
93 static int i2cproc_initialized = 0;
95 #else /* undef CONFIG_PROC_FS */
97 #define i2cproc_init() 0
98 #define i2cproc_cleanup() 0
100 #endif /* CONFIG_PROC_FS */
103 /* ---------------------------------------------------
104 * registering functions
105 * ---------------------------------------------------
109 * i2c_add_adapter is called from within the algorithm layer,
110 * when a new hw adapter registers. A new device is register to be
111 * available for clients.
113 int i2c_add_adapter(struct i2c_adapter *adap)
118 for (i = 0; i < I2C_ADAP_MAX; i++)
119 if (NULL == adapters[i])
121 if (I2C_ADAP_MAX == i) {
123 " i2c-core.o: register_adapter(%s) - enlarge I2C_ADAP_MAX.\n",
133 /* init data types */
134 init_MUTEX(&adap->lock);
136 #ifdef CONFIG_PROC_FS
138 if (i2cproc_initialized) {
140 struct proc_dir_entry *proc_entry;
142 sprintf(name,"i2c-%d", i);
144 proc_entry = create_proc_entry(name,0,proc_bus);
146 printk("i2c-core.o: Could not create /proc/bus/%s\n",
152 proc_entry->proc_fops = &i2cproc_operations;
153 proc_entry->owner = THIS_MODULE;
154 adap->inode = proc_entry->low_ino;
157 #endif /* def CONFIG_PROC_FS */
159 /* inform drivers of new adapters */
161 for (j=0;j<I2C_DRIVER_MAX;j++)
162 if (drivers[j]!=NULL &&
163 (drivers[j]->flags&(I2C_DF_NOTIFY|I2C_DF_DUMMY)))
164 /* We ignore the return code; if it fails, too bad */
165 drivers[j]->attach_adapter(adap);
168 DEB(printk(KERN_DEBUG "i2c-core.o: adapter %s registered as adapter %d.\n",
184 int i2c_del_adapter(struct i2c_adapter *adap)
190 for (i = 0; i < I2C_ADAP_MAX; i++)
191 if (adap == adapters[i])
193 if (I2C_ADAP_MAX == i) {
194 printk(KERN_WARNING "i2c-core.o: unregister_adapter adap [%s] not found.\n",
200 /* DUMMY drivers do not register their clients, so we have to
201 * use a trick here: we call driver->attach_adapter to
202 * *detach* it! Of course, each dummy driver should know about
203 * this or hell will break loose...
206 for (j = 0; j < I2C_DRIVER_MAX; j++)
207 if (drivers[j] && (drivers[j]->flags & I2C_DF_DUMMY))
208 if ((res = drivers[j]->attach_adapter(adap))) {
209 printk(KERN_WARNING "i2c-core.o: can't detach adapter %s "
210 "while detaching driver %s: driver not "
211 "detached!\n", adap->name, drivers[j]->name);
217 /* detach any active clients. This must be done first, because
218 * it can fail; in which case we give up. */
219 for (j=0;j<I2C_CLIENT_MAX;j++) {
220 struct i2c_client *client = adap->clients[j];
222 /* detaching devices is unconditional of the set notify
223 * flag, as _all_ clients that reside on the adapter
224 * must be deleted, as this would cause invalid states.
226 if ((res=client->driver->detach_client(client))) {
227 printk(KERN_ERR "i2c-core.o: adapter %s not "
228 "unregistered, because client at "
229 "address %02x can't be detached\n",
230 adap->name, client->addr);
235 #ifdef CONFIG_PROC_FS
236 if (i2cproc_initialized) {
238 sprintf(name,"i2c-%d", i);
239 remove_proc_entry(name,proc_bus);
241 #endif /* def CONFIG_PROC_FS */
247 DEB(printk(KERN_DEBUG "i2c-core.o: adapter unregistered: %s\n",adap->name));
260 * What follows is the "upwards" interface: commands for talking to clients,
261 * which implement the functions to access the physical information of the
265 int i2c_add_driver(struct i2c_driver *driver)
269 for (i = 0; i < I2C_DRIVER_MAX; i++)
270 if (NULL == drivers[i])
272 if (I2C_DRIVER_MAX == i) {
274 " i2c-core.o: register_driver(%s) "
275 "- enlarge I2C_DRIVER_MAX.\n",
284 DRV_UNLOCK(); /* driver was successfully added */
286 DEB(printk(KERN_DEBUG "i2c-core.o: driver %s registered.\n",driver->name));
290 /* now look for instances of driver on our adapters
292 if (driver->flags& (I2C_DF_NOTIFY|I2C_DF_DUMMY)) {
293 for (i=0;i<I2C_ADAP_MAX;i++)
294 if (adapters[i]!=NULL)
296 driver->attach_adapter(adapters[i]);
302 int i2c_del_driver(struct i2c_driver *driver)
307 for (i = 0; i < I2C_DRIVER_MAX; i++)
308 if (driver == drivers[i])
310 if (I2C_DRIVER_MAX == i) {
311 printk(KERN_WARNING " i2c-core.o: unregister_driver: "
317 /* Have a look at each adapter, if clients of this driver are still
318 * attached. If so, detach them to be able to kill the driver
321 DEB2(printk(KERN_DEBUG "i2c-core.o: unregister_driver - looking for clients.\n"));
322 /* removing clients does not depend on the notify flag, else
323 * invalid operation might (will!) result, when using stale client
326 ADAP_LOCK(); /* should be moved inside the if statement... */
327 for (k=0;k<I2C_ADAP_MAX;k++) {
328 struct i2c_adapter *adap = adapters[k];
329 if (adap == NULL) /* skip empty entries. */
331 DEB2(printk(KERN_DEBUG "i2c-core.o: examining adapter %s:\n",
333 if (driver->flags & I2C_DF_DUMMY) {
334 /* DUMMY drivers do not register their clients, so we have to
335 * use a trick here: we call driver->attach_adapter to
336 * *detach* it! Of course, each dummy driver should know about
337 * this or hell will break loose...
339 if ((res = driver->attach_adapter(adap))) {
340 printk(KERN_WARNING "i2c-core.o: while unregistering "
341 "dummy driver %s, adapter %s could "
342 "not be detached properly; driver "
343 "not unloaded!\n", driver->name,
349 for (j=0;j<I2C_CLIENT_MAX;j++) {
350 struct i2c_client *client = adap->clients[j];
351 if (client != NULL &&
352 client->driver == driver) {
353 DEB2(printk(KERN_DEBUG "i2c-core.o: "
354 "detaching client %s:\n",
357 detach_client(client)))
359 printk(KERN_ERR "i2c-core.o: while "
360 "unregistering driver "
361 "`%s', the client at "
363 "adapter `%s' could not "
364 "be detached; driver "
381 DEB(printk(KERN_DEBUG "i2c-core.o: driver unregistered: %s\n",driver->name));
385 int i2c_check_addr (struct i2c_adapter *adapter, int addr)
388 for (i = 0; i < I2C_CLIENT_MAX ; i++)
389 if (adapter->clients[i] && (adapter->clients[i]->addr == addr))
394 int i2c_attach_client(struct i2c_client *client)
396 struct i2c_adapter *adapter = client->adapter;
399 if (i2c_check_addr(client->adapter,client->addr))
402 for (i = 0; i < I2C_CLIENT_MAX; i++)
403 if (NULL == adapter->clients[i])
405 if (I2C_CLIENT_MAX == i) {
407 " i2c-core.o: attach_client(%s) - enlarge I2C_CLIENT_MAX.\n",
412 adapter->clients[i] = client;
413 adapter->client_count++;
415 if (adapter->client_register)
416 if (adapter->client_register(client))
417 printk(KERN_DEBUG "i2c-core.o: warning: client_register seems "
418 "to have failed for client %02x at adapter %s\n",
419 client->addr,adapter->name);
420 DEB(printk(KERN_DEBUG "i2c-core.o: client [%s] registered to adapter [%s](pos. %d).\n",
421 client->name, adapter->name,i));
423 if(client->flags & I2C_CLIENT_ALLOW_USE)
424 client->usage_count = 0;
430 int i2c_detach_client(struct i2c_client *client)
432 struct i2c_adapter *adapter = client->adapter;
435 for (i = 0; i < I2C_CLIENT_MAX; i++)
436 if (client == adapter->clients[i])
438 if (I2C_CLIENT_MAX == i) {
439 printk(KERN_WARNING " i2c-core.o: unregister_client "
445 if( (client->flags & I2C_CLIENT_ALLOW_USE) &&
446 (client->usage_count>0))
449 if (adapter->client_unregister != NULL)
450 if ((res = adapter->client_unregister(client))) {
451 printk(KERN_ERR "i2c-core.o: client_unregister [%s] failed, "
452 "client not detached\n", client->name);
456 adapter->clients[i] = NULL;
457 adapter->client_count--;
459 DEB(printk(KERN_DEBUG "i2c-core.o: client [%s] unregistered.\n",client->name));
463 void i2c_inc_use_client(struct i2c_client *client)
465 if (client->driver->inc_use != NULL)
466 client->driver->inc_use(client);
467 if (client->adapter->inc_use != NULL)
468 client->adapter->inc_use(client->adapter);
471 void i2c_dec_use_client(struct i2c_client *client)
473 if (client->driver->dec_use != NULL)
474 client->driver->dec_use(client);
475 if (client->adapter->dec_use != NULL)
476 client->adapter->dec_use(client->adapter);
479 struct i2c_client *i2c_get_client(int driver_id, int adapter_id,
480 struct i2c_client *prev)
484 /* Will iterate through the list of clients in each adapter of adapters-list
485 in search for a client that matches the search criteria. driver_id or
486 adapter_id are ignored if set to 0. If both are ignored this returns
487 first client found. */
491 /* set starting point */
495 return (struct i2c_client *) -EINVAL;
497 for(j=0; j < I2C_ADAP_MAX; j++)
498 if(prev->adapter == adapters[j])
501 /* invalid starting point? */
502 if (I2C_ADAP_MAX == j) {
503 printk(KERN_WARNING " i2c-core.o: get_client adapter for client:[%s] not found\n",
505 return (struct i2c_client *) -ENODEV;
508 for(i=0; i < I2C_CLIENT_MAX; i++)
509 if(prev == adapters[j]->clients[i])
512 /* invalid starting point? */
513 if (I2C_CLIENT_MAX == i) {
514 printk(KERN_WARNING " i2c-core.o: get_client client:[%s] not found\n",
516 return (struct i2c_client *) -ENODEV;
519 i++; /* start from one after prev */
522 for(; j < I2C_ADAP_MAX; j++)
527 if(adapter_id && (adapters[j]->id != adapter_id))
530 for(; i < I2C_CLIENT_MAX; i++)
532 if(!adapters[j]->clients[i])
535 if(driver_id && (adapters[j]->clients[i]->driver->id != driver_id))
537 if(adapters[j]->clients[i]->flags & I2C_CLIENT_ALLOW_USE)
538 return adapters[j]->clients[i];
546 int i2c_use_client(struct i2c_client *client)
548 if (client->flags & I2C_CLIENT_ALLOW_USE) {
549 if (client->flags & I2C_CLIENT_ALLOW_MULTIPLE_USE)
550 client->usage_count++;
551 else if (client->usage_count > 0)
554 client->usage_count++;
557 i2c_inc_use_client(client);
562 int i2c_release_client(struct i2c_client *client)
564 if(client->flags & I2C_CLIENT_ALLOW_USE) {
565 if(client->usage_count>0)
566 client->usage_count--;
569 printk(KERN_WARNING " i2c-core.o: dec_use_client used one too many times\n");
574 i2c_dec_use_client(client);
579 /* ----------------------------------------------------
580 * The /proc functions
581 * ----------------------------------------------------
584 #ifdef CONFIG_PROC_FS
586 /* This function generates the output for /proc/bus/i2c */
587 int read_bus_i2c(char *buf, char **start, off_t offset, int len, int *eof,
592 /* Note that it is safe to write a `little' beyond len. Yes, really. */
593 for (i = 0; (i < I2C_ADAP_MAX) && (nr < len); i++)
595 nr += sprintf(buf+nr, "i2c-%d\t", i);
596 if (adapters[i]->algo->smbus_xfer) {
597 if (adapters[i]->algo->master_xfer)
598 nr += sprintf(buf+nr,"smbus/i2c");
600 nr += sprintf(buf+nr,"smbus ");
601 } else if (adapters[i]->algo->master_xfer)
602 nr += sprintf(buf+nr,"i2c ");
604 nr += sprintf(buf+nr,"dummy ");
605 nr += sprintf(buf+nr,"\t%-32s\t%-32s\n",
607 adapters[i]->algo->name);
612 /* This function generates the output for /proc/bus/i2c-? */
613 ssize_t i2cproc_bus_read(struct file * file, char * buf,size_t count,
616 struct inode * inode = file->f_dentry->d_inode;
618 struct i2c_client *client;
619 int i,j,k,order_nr,len=0;
621 int order[I2C_CLIENT_MAX];
625 len_total = file->f_pos + count;
626 /* Too bad if this gets longer (unlikely) */
627 if (len_total > 4000)
629 for (i = 0; i < I2C_ADAP_MAX; i++)
630 if (adapters[i]->inode == inode->i_ino) {
631 /* We need a bit of slack in the kernel buffer; this makes the
633 if (! (kbuf = kmalloc(count + 80,GFP_KERNEL)))
635 /* Order will hold the indexes of the clients
638 for (j = 0; j < I2C_CLIENT_MAX; j++) {
639 if ((client = adapters[i]->clients[j]) &&
640 (client->driver->id != I2C_DRIVERID_I2CDEV)) {
643 adapters[i]->clients[order[k-1]]->
646 order[k] = order[k-1];
653 for (j = 0; (j < order_nr) && (len < len_total); j++) {
654 client = adapters[i]->clients[order[j]];
655 len += sprintf(kbuf+len,"%02x\t%-32s\t%-32s\n",
658 client->driver->name);
660 len = len - file->f_pos;
665 if (copy_to_user (buf,kbuf+file->f_pos, len)) {
676 int i2cproc_init(void)
679 struct proc_dir_entry *proc_bus_i2c;
681 i2cproc_initialized = 0;
684 printk("i2c-core.o: /proc/bus/ does not exist");
688 proc_bus_i2c = create_proc_entry("i2c",0,proc_bus);
690 printk(KERN_ERR "i2c-core.o: Could not create /proc/bus/i2c");
694 proc_bus_i2c->read_proc = &read_bus_i2c;
695 proc_bus_i2c->owner = THIS_MODULE;
696 i2cproc_initialized += 2;
700 int i2cproc_cleanup(void)
703 if (i2cproc_initialized >= 1) {
704 remove_proc_entry("i2c",proc_bus);
705 i2cproc_initialized -= 2;
710 #endif /* def CONFIG_PROC_FS */
712 /* ----------------------------------------------------
713 * the functional interface to the i2c busses.
714 * ----------------------------------------------------
717 int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs,int num)
721 if (adap->algo->master_xfer) {
722 DEB2(printk(KERN_DEBUG "i2c-core.o: master_xfer: %s with %d msgs.\n",
726 ret = adap->algo->master_xfer(adap,msgs,num);
731 printk(KERN_ERR "i2c-core.o: I2C adapter %04x: I2C level transfers not supported\n",
737 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
740 struct i2c_adapter *adap=client->adapter;
743 if (client->adapter->algo->master_xfer) {
744 msg.addr = client->addr;
745 msg.flags = client->flags & I2C_M_TEN;
747 msg.buf = (char *)buf;
749 DEB2(printk(KERN_DEBUG "i2c-core.o: master_send: writing %d bytes on %s.\n",
750 count,client->adapter->name));
753 ret = adap->algo->master_xfer(adap,&msg,1);
756 /* if everything went ok (i.e. 1 msg transmitted), return #bytes
757 * transmitted, else error code.
759 return (ret == 1 )? count : ret;
761 printk(KERN_ERR "i2c-core.o: I2C adapter %04x: I2C level transfers not supported\n",
762 client->adapter->id);
767 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
769 struct i2c_adapter *adap=client->adapter;
772 if (client->adapter->algo->master_xfer) {
773 msg.addr = client->addr;
774 msg.flags = client->flags & I2C_M_TEN;
775 msg.flags |= I2C_M_RD;
779 DEB2(printk(KERN_DEBUG "i2c-core.o: master_recv: reading %d bytes on %s.\n",
780 count,client->adapter->name));
783 ret = adap->algo->master_xfer(adap,&msg,1);
786 DEB2(printk(KERN_DEBUG "i2c-core.o: master_recv: return:%d (count:%d, addr:0x%02x)\n",
787 ret, count, client->addr));
789 /* if everything went ok (i.e. 1 msg transmitted), return #bytes
790 * transmitted, else error code.
792 return (ret == 1 )? count : ret;
794 printk(KERN_ERR "i2c-core.o: I2C adapter %04x: I2C level transfers not supported\n",
795 client->adapter->id);
801 int i2c_control(struct i2c_client *client,
802 unsigned int cmd, unsigned long arg)
805 struct i2c_adapter *adap = client->adapter;
807 DEB2(printk(KERN_DEBUG "i2c-core.o: i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg));
816 if (adap->algo->algo_control!=NULL)
817 ret = adap->algo->algo_control(adap,cmd,arg);
822 /* ----------------------------------------------------
823 * the i2c address scanning function
824 * Will not work for 10-bit addresses!
825 * ----------------------------------------------------
827 int i2c_probe(struct i2c_adapter *adapter,
828 struct i2c_client_address_data *address_data,
829 i2c_client_found_addr_proc *found_proc)
831 int addr,i,found,err;
832 int adap_id = i2c_adapter_id(adapter);
834 /* Forget it if we can't probe using SMBUS_QUICK */
835 if (! i2c_check_functionality(adapter,I2C_FUNC_SMBUS_QUICK))
838 for (addr = 0x00; addr <= 0x7f; addr++) {
840 /* Skip if already in use */
841 if (i2c_check_addr(adapter,addr))
844 /* If it is in one of the force entries, we don't do any detection
848 for (i = 0; !found && (address_data->force[i] != I2C_CLIENT_END); i += 2) {
849 if (((adap_id == address_data->force[i]) ||
850 (address_data->force[i] == ANY_I2C_BUS)) &&
851 (addr == address_data->force[i+1])) {
852 DEB2(printk(KERN_DEBUG "i2c-core.o: found force parameter for adapter %d, addr %04x\n",
854 if ((err = found_proc(adapter,addr,0,0)))
862 /* If this address is in one of the ignores, we can forget about
865 !found && (address_data->ignore[i] != I2C_CLIENT_END);
867 if (((adap_id == address_data->ignore[i]) ||
868 ((address_data->ignore[i] == ANY_I2C_BUS))) &&
869 (addr == address_data->ignore[i+1])) {
870 DEB2(printk(KERN_DEBUG "i2c-core.o: found ignore parameter for adapter %d, "
871 "addr %04x\n", adap_id ,addr));
876 !found && (address_data->ignore_range[i] != I2C_CLIENT_END);
878 if (((adap_id == address_data->ignore_range[i]) ||
879 ((address_data->ignore_range[i]==ANY_I2C_BUS))) &&
880 (addr >= address_data->ignore_range[i+1]) &&
881 (addr <= address_data->ignore_range[i+2])) {
882 DEB2(printk(KERN_DEBUG "i2c-core.o: found ignore_range parameter for adapter %d, "
883 "addr %04x\n", adap_id,addr));
890 /* Now, we will do a detection, but only if it is in the normal or
893 !found && (address_data->normal_i2c[i] != I2C_CLIENT_END);
895 if (addr == address_data->normal_i2c[i]) {
897 DEB2(printk(KERN_DEBUG "i2c-core.o: found normal i2c entry for adapter %d, "
898 "addr %02x\n", adap_id, addr));
903 !found && (address_data->normal_i2c_range[i] != I2C_CLIENT_END);
905 if ((addr >= address_data->normal_i2c_range[i]) &&
906 (addr <= address_data->normal_i2c_range[i+1])) {
908 DEB2(printk(KERN_DEBUG "i2c-core.o: found normal i2c_range entry for adapter %d, "
909 "addr %04x\n", adap_id,addr));
914 !found && (address_data->probe[i] != I2C_CLIENT_END);
916 if (((adap_id == address_data->probe[i]) ||
917 ((address_data->probe[i] == ANY_I2C_BUS))) &&
918 (addr == address_data->probe[i+1])) {
920 DEB2(printk(KERN_DEBUG "i2c-core.o: found probe parameter for adapter %d, "
921 "addr %04x\n", adap_id,addr));
925 !found && (address_data->probe_range[i] != I2C_CLIENT_END);
927 if (((adap_id == address_data->probe_range[i]) ||
928 (address_data->probe_range[i] == ANY_I2C_BUS)) &&
929 (addr >= address_data->probe_range[i+1]) &&
930 (addr <= address_data->probe_range[i+2])) {
932 DEB2(printk(KERN_DEBUG "i2c-core.o: found probe_range parameter for adapter %d, "
933 "addr %04x\n", adap_id,addr));
939 /* OK, so we really should examine this address. First check
940 whether there is some client here at all! */
941 if (i2c_smbus_xfer(adapter,addr,0,0,0,I2C_SMBUS_QUICK,NULL) >= 0)
942 if ((err = found_proc(adapter,addr,0,-1)))
949 * return id number for a specific adapter
951 int i2c_adapter_id(struct i2c_adapter *adap)
954 for (i = 0; i < I2C_ADAP_MAX; i++)
955 if (adap == adapters[i])
960 /* The SMBus parts */
962 extern s32 i2c_smbus_write_quick(struct i2c_client * client, u8 value)
964 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
965 value,0,I2C_SMBUS_QUICK,NULL);
968 extern s32 i2c_smbus_read_byte(struct i2c_client * client)
970 union i2c_smbus_data data;
971 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
972 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
978 extern s32 i2c_smbus_write_byte(struct i2c_client * client, u8 value)
980 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
981 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
984 extern s32 i2c_smbus_read_byte_data(struct i2c_client * client, u8 command)
986 union i2c_smbus_data data;
987 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
988 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
994 extern s32 i2c_smbus_write_byte_data(struct i2c_client * client, u8 command,
997 union i2c_smbus_data data;
999 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1000 I2C_SMBUS_WRITE,command,
1001 I2C_SMBUS_BYTE_DATA,&data);
1004 extern s32 i2c_smbus_read_word_data(struct i2c_client * client, u8 command)
1006 union i2c_smbus_data data;
1007 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1008 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1014 extern s32 i2c_smbus_write_word_data(struct i2c_client * client,
1015 u8 command, u16 value)
1017 union i2c_smbus_data data;
1019 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1020 I2C_SMBUS_WRITE,command,
1021 I2C_SMBUS_WORD_DATA,&data);
1024 extern s32 i2c_smbus_process_call(struct i2c_client * client,
1025 u8 command, u16 value)
1027 union i2c_smbus_data data;
1029 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1030 I2C_SMBUS_WRITE,command,
1031 I2C_SMBUS_PROC_CALL, &data))
1037 /* Returns the number of read bytes */
1038 extern s32 i2c_smbus_read_block_data(struct i2c_client * client,
1039 u8 command, u8 *values)
1041 union i2c_smbus_data data;
1043 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1044 I2C_SMBUS_READ,command,
1045 I2C_SMBUS_BLOCK_DATA,&data))
1048 for (i = 1; i <= data.block[0]; i++)
1049 values[i-1] = data.block[i];
1050 return data.block[0];
1054 extern s32 i2c_smbus_write_block_data(struct i2c_client * client,
1055 u8 command, u8 length, u8 *values)
1057 union i2c_smbus_data data;
1059 if (length > I2C_SMBUS_BLOCK_MAX)
1060 length = I2C_SMBUS_BLOCK_MAX;
1061 for (i = 1; i <= length; i++)
1062 data.block[i] = values[i-1];
1063 data.block[0] = length;
1064 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1065 I2C_SMBUS_WRITE,command,
1066 I2C_SMBUS_BLOCK_DATA,&data);
1069 extern s32 i2c_smbus_write_i2c_block_data(struct i2c_client * client,
1070 u8 command, u8 length, u8 *values)
1072 union i2c_smbus_data data;
1074 if (length > I2C_SMBUS_I2C_BLOCK_MAX)
1075 length = I2C_SMBUS_I2C_BLOCK_MAX;
1076 for (i = 1; i <= length; i++)
1077 data.block[i] = values[i-1];
1078 data.block[0] = length;
1079 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1080 I2C_SMBUS_WRITE,command,
1081 I2C_SMBUS_I2C_BLOCK_DATA,&data);
1084 /* Simulate a SMBus command using the i2c protocol
1085 No checking of parameters is done! */
1086 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1087 unsigned short flags,
1088 char read_write, u8 command, int size,
1089 union i2c_smbus_data * data)
1091 /* So we need to generate a series of msgs. In the case of writing, we
1092 need to use only one message; when reading, we need two. We initialize
1093 most things with sane defaults, to keep the code below somewhat
1095 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+2];
1096 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1097 int num = read_write == I2C_SMBUS_READ?2:1;
1098 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1099 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1103 msgbuf0[0] = command;
1105 case I2C_SMBUS_QUICK:
1107 /* Special case: The read/write field is used as data */
1108 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1111 case I2C_SMBUS_BYTE:
1112 if (read_write == I2C_SMBUS_READ) {
1113 /* Special case: only a read! */
1114 msg[0].flags = I2C_M_RD | flags;
1118 case I2C_SMBUS_BYTE_DATA:
1119 if (read_write == I2C_SMBUS_READ)
1123 msgbuf0[1] = data->byte;
1126 case I2C_SMBUS_WORD_DATA:
1127 if (read_write == I2C_SMBUS_READ)
1131 msgbuf0[1] = data->word & 0xff;
1132 msgbuf0[2] = data->word >> 8;
1135 case I2C_SMBUS_PROC_CALL:
1136 num = 2; /* Special case */
1139 msgbuf0[1] = data->word & 0xff;
1140 msgbuf0[2] = data->word >> 8;
1142 case I2C_SMBUS_BLOCK_DATA:
1143 if (read_write == I2C_SMBUS_READ) {
1144 printk(KERN_ERR "i2c-core.o: Block read not supported "
1145 "under I2C emulation!\n");
1148 msg[0].len = data->block[0] + 2;
1149 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1150 printk(KERN_ERR "i2c-core.o: smbus_access called with "
1151 "invalid block write size (%d)\n",
1155 for (i = 1; i <= msg[0].len; i++)
1156 msgbuf0[i] = data->block[i-1];
1160 printk(KERN_ERR "i2c-core.o: smbus_access called with invalid size (%d)\n",
1165 if (i2c_transfer(adapter, msg, num) < 0)
1168 if (read_write == I2C_SMBUS_READ)
1170 case I2C_SMBUS_BYTE:
1171 data->byte = msgbuf0[0];
1173 case I2C_SMBUS_BYTE_DATA:
1174 data->byte = msgbuf1[0];
1176 case I2C_SMBUS_WORD_DATA:
1177 case I2C_SMBUS_PROC_CALL:
1178 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1185 s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1186 char read_write, u8 command, int size,
1187 union i2c_smbus_data * data)
1191 flags = flags & I2C_M_TEN;
1193 if (adapter->algo->smbus_xfer) {
1195 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1197 I2C_UNLOCK(adapter);
1199 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1206 /* You should always define `functionality'; the 'else' is just for
1207 backward compatibility. */
1208 u32 i2c_get_functionality (struct i2c_adapter *adap)
1210 if (adap->algo->functionality)
1211 return adap->algo->functionality(adap);
1216 int i2c_check_functionality (struct i2c_adapter *adap, u32 func)
1218 u32 adap_func = i2c_get_functionality (adap);
1219 return (func & adap_func) == func;
1223 static int __init i2c_init(void)
1225 printk(KERN_INFO "i2c-core.o: i2c core module version %s (%s)\n", I2C_VERSION, I2C_DATE);
1226 memset(adapters,0,sizeof(adapters));
1227 memset(drivers,0,sizeof(drivers));
1231 init_MUTEX(&adap_lock);
1232 init_MUTEX(&driver_lock);
1240 #ifdef CONFIG_I2C_CHARDEV
1241 extern int i2c_dev_init(void);
1243 #ifdef CONFIG_I2C_ALGOBIT
1244 extern int i2c_algo_bit_init(void);
1246 #ifdef CONFIG_I2C_PHILIPSPAR
1247 extern int i2c_bitlp_init(void);
1249 #ifdef CONFIG_I2C_ELV
1250 extern int i2c_bitelv_init(void);
1252 #ifdef CONFIG_I2C_VELLEMAN
1253 extern int i2c_bitvelle_init(void);
1255 #ifdef CONFIG_I2C_BITVIA
1256 extern int i2c_bitvia_init(void);
1259 #ifdef CONFIG_I2C_ALGOPCF
1260 extern int i2c_algo_pcf_init(void);
1262 #ifdef CONFIG_I2C_ELEKTOR
1263 extern int i2c_pcfisa_init(void);
1266 #ifdef CONFIG_I2C_ALGO8XX
1267 extern int i2c_algo_8xx_init(void);
1269 #ifdef CONFIG_I2C_RPXLITE
1270 extern int i2c_rpx_init(void);
1273 #ifdef CONFIG_I2C_ALGO_SIBYTE
1274 extern int i2c_algo_sibyte_init(void);
1275 extern int i2c_sibyte_init(void);
1277 #ifdef CONFIG_I2C_MAX1617
1278 extern int i2c_max1617_init(void);
1281 #ifdef CONFIG_I2C_PROC
1282 extern int sensors_init(void);
1285 /* This is needed for automatic patch generation: sensors code starts here */
1286 /* This is needed for automatic patch generation: sensors code ends here */
1288 int __init i2c_init_all(void)
1290 /* --------------------- global ----- */
1293 #ifdef CONFIG_I2C_CHARDEV
1296 /* --------------------- bit -------- */
1297 #ifdef CONFIG_I2C_ALGOBIT
1298 i2c_algo_bit_init();
1300 #ifdef CONFIG_I2C_PHILIPSPAR
1303 #ifdef CONFIG_I2C_ELV
1306 #ifdef CONFIG_I2C_VELLEMAN
1307 i2c_bitvelle_init();
1310 /* --------------------- pcf -------- */
1311 #ifdef CONFIG_I2C_ALGOPCF
1312 i2c_algo_pcf_init();
1314 #ifdef CONFIG_I2C_ELEKTOR
1318 /* --------------------- 8xx -------- */
1319 #ifdef CONFIG_I2C_ALGO8XX
1320 i2c_algo_8xx_init();
1322 #ifdef CONFIG_I2C_RPXLITE
1326 /* --------------------- SiByte -------- */
1327 #ifdef CONFIG_I2C_ALGO_SIBYTE
1328 i2c_algo_sibyte_init();
1331 #ifdef CONFIG_I2C_MAX1617
1335 /* -------------- proc interface ---- */
1336 #ifdef CONFIG_I2C_PROC
1339 /* This is needed for automatic patch generation: sensors code starts here */
1340 /* This is needed for automatic patch generation: sensors code ends here */
1349 EXPORT_SYMBOL(i2c_add_adapter);
1350 EXPORT_SYMBOL(i2c_del_adapter);
1351 EXPORT_SYMBOL(i2c_add_driver);
1352 EXPORT_SYMBOL(i2c_del_driver);
1353 EXPORT_SYMBOL(i2c_attach_client);
1354 EXPORT_SYMBOL(i2c_detach_client);
1355 EXPORT_SYMBOL(i2c_inc_use_client);
1356 EXPORT_SYMBOL(i2c_dec_use_client);
1357 EXPORT_SYMBOL(i2c_get_client);
1358 EXPORT_SYMBOL(i2c_use_client);
1359 EXPORT_SYMBOL(i2c_release_client);
1360 EXPORT_SYMBOL(i2c_check_addr);
1363 EXPORT_SYMBOL(i2c_master_send);
1364 EXPORT_SYMBOL(i2c_master_recv);
1365 EXPORT_SYMBOL(i2c_control);
1366 EXPORT_SYMBOL(i2c_transfer);
1367 EXPORT_SYMBOL(i2c_adapter_id);
1368 EXPORT_SYMBOL(i2c_probe);
1370 EXPORT_SYMBOL(i2c_smbus_xfer);
1371 EXPORT_SYMBOL(i2c_smbus_write_quick);
1372 EXPORT_SYMBOL(i2c_smbus_read_byte);
1373 EXPORT_SYMBOL(i2c_smbus_write_byte);
1374 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1375 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1376 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1377 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1378 EXPORT_SYMBOL(i2c_smbus_process_call);
1379 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1380 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1382 EXPORT_SYMBOL(i2c_get_functionality);
1383 EXPORT_SYMBOL(i2c_check_functionality);
1386 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1387 MODULE_DESCRIPTION("I2C-Bus main module");
1388 MODULE_LICENSE("GPL");
1390 MODULE_PARM(i2c_debug, "i");
1391 MODULE_PARM_DESC(i2c_debug,"debug level");
1393 int init_module(void)
1398 void cleanup_module(void)