net: Fix the rollback test in dev_change_name()
[linux-2.6/mini2440.git] / drivers / i2c / i2c-core.c
blob529d82ce5a4c51607bac74f6de49af3bd3ef9af9
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>
22 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
23 Jean Delvare <khali@linux-fr.org> */
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/slab.h>
29 #include <linux/i2c.h>
30 #include <linux/init.h>
31 #include <linux/idr.h>
32 #include <linux/mutex.h>
33 #include <linux/completion.h>
34 #include <linux/hardirq.h>
35 #include <linux/irqflags.h>
36 #include <linux/rwsem.h>
37 #include <asm/uaccess.h>
39 #include "i2c-core.h"
42 /* core_lock protects i2c_adapter_idr, userspace_devices, and guarantees
43 that device detection, deletion of detected devices, and attach_adapter
44 and detach_adapter calls are serialized */
45 static DEFINE_MUTEX(core_lock);
46 static DEFINE_IDR(i2c_adapter_idr);
47 static LIST_HEAD(userspace_devices);
49 static int i2c_check_addr(struct i2c_adapter *adapter, int addr);
50 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
52 /* ------------------------------------------------------------------------- */
54 static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
55 const struct i2c_client *client)
57 while (id->name[0]) {
58 if (strcmp(client->name, id->name) == 0)
59 return id;
60 id++;
62 return NULL;
65 static int i2c_device_match(struct device *dev, struct device_driver *drv)
67 struct i2c_client *client = to_i2c_client(dev);
68 struct i2c_driver *driver = to_i2c_driver(drv);
70 /* match on an id table if there is one */
71 if (driver->id_table)
72 return i2c_match_id(driver->id_table, client) != NULL;
74 return 0;
77 #ifdef CONFIG_HOTPLUG
79 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
80 static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
82 struct i2c_client *client = to_i2c_client(dev);
84 if (add_uevent_var(env, "MODALIAS=%s%s",
85 I2C_MODULE_PREFIX, client->name))
86 return -ENOMEM;
87 dev_dbg(dev, "uevent\n");
88 return 0;
91 #else
92 #define i2c_device_uevent NULL
93 #endif /* CONFIG_HOTPLUG */
95 static int i2c_device_probe(struct device *dev)
97 struct i2c_client *client = to_i2c_client(dev);
98 struct i2c_driver *driver = to_i2c_driver(dev->driver);
99 int status;
101 if (!driver->probe || !driver->id_table)
102 return -ENODEV;
103 client->driver = driver;
104 if (!device_can_wakeup(&client->dev))
105 device_init_wakeup(&client->dev,
106 client->flags & I2C_CLIENT_WAKE);
107 dev_dbg(dev, "probe\n");
109 status = driver->probe(client, i2c_match_id(driver->id_table, client));
110 if (status)
111 client->driver = NULL;
112 return status;
115 static int i2c_device_remove(struct device *dev)
117 struct i2c_client *client = to_i2c_client(dev);
118 struct i2c_driver *driver;
119 int status;
121 if (!dev->driver)
122 return 0;
124 driver = to_i2c_driver(dev->driver);
125 if (driver->remove) {
126 dev_dbg(dev, "remove\n");
127 status = driver->remove(client);
128 } else {
129 dev->driver = NULL;
130 status = 0;
132 if (status == 0)
133 client->driver = NULL;
134 return status;
137 static void i2c_device_shutdown(struct device *dev)
139 struct i2c_driver *driver;
141 if (!dev->driver)
142 return;
143 driver = to_i2c_driver(dev->driver);
144 if (driver->shutdown)
145 driver->shutdown(to_i2c_client(dev));
148 static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
150 struct i2c_driver *driver;
152 if (!dev->driver)
153 return 0;
154 driver = to_i2c_driver(dev->driver);
155 if (!driver->suspend)
156 return 0;
157 return driver->suspend(to_i2c_client(dev), mesg);
160 static int i2c_device_resume(struct device *dev)
162 struct i2c_driver *driver;
164 if (!dev->driver)
165 return 0;
166 driver = to_i2c_driver(dev->driver);
167 if (!driver->resume)
168 return 0;
169 return driver->resume(to_i2c_client(dev));
172 static void i2c_client_dev_release(struct device *dev)
174 kfree(to_i2c_client(dev));
177 static ssize_t
178 show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
180 struct i2c_client *client = to_i2c_client(dev);
181 return sprintf(buf, "%s\n", client->name);
184 static ssize_t
185 show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
187 struct i2c_client *client = to_i2c_client(dev);
188 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
191 static struct device_attribute i2c_dev_attrs[] = {
192 __ATTR(name, S_IRUGO, show_client_name, NULL),
193 /* modalias helps coldplug: modprobe $(cat .../modalias) */
194 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
195 { },
198 struct bus_type i2c_bus_type = {
199 .name = "i2c",
200 .dev_attrs = i2c_dev_attrs,
201 .match = i2c_device_match,
202 .uevent = i2c_device_uevent,
203 .probe = i2c_device_probe,
204 .remove = i2c_device_remove,
205 .shutdown = i2c_device_shutdown,
206 .suspend = i2c_device_suspend,
207 .resume = i2c_device_resume,
209 EXPORT_SYMBOL_GPL(i2c_bus_type);
213 * i2c_verify_client - return parameter as i2c_client, or NULL
214 * @dev: device, probably from some driver model iterator
216 * When traversing the driver model tree, perhaps using driver model
217 * iterators like @device_for_each_child(), you can't assume very much
218 * about the nodes you find. Use this function to avoid oopses caused
219 * by wrongly treating some non-I2C device as an i2c_client.
221 struct i2c_client *i2c_verify_client(struct device *dev)
223 return (dev->bus == &i2c_bus_type)
224 ? to_i2c_client(dev)
225 : NULL;
227 EXPORT_SYMBOL(i2c_verify_client);
231 * i2c_new_device - instantiate an i2c device
232 * @adap: the adapter managing the device
233 * @info: describes one I2C device; bus_num is ignored
234 * Context: can sleep
236 * Create an i2c device. Binding is handled through driver model
237 * probe()/remove() methods. A driver may be bound to this device when we
238 * return from this function, or any later moment (e.g. maybe hotplugging will
239 * load the driver module). This call is not appropriate for use by mainboard
240 * initialization logic, which usually runs during an arch_initcall() long
241 * before any i2c_adapter could exist.
243 * This returns the new i2c client, which may be saved for later use with
244 * i2c_unregister_device(); or NULL to indicate an error.
246 struct i2c_client *
247 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
249 struct i2c_client *client;
250 int status;
252 client = kzalloc(sizeof *client, GFP_KERNEL);
253 if (!client)
254 return NULL;
256 client->adapter = adap;
258 client->dev.platform_data = info->platform_data;
260 if (info->archdata)
261 client->dev.archdata = *info->archdata;
263 client->flags = info->flags;
264 client->addr = info->addr;
265 client->irq = info->irq;
267 strlcpy(client->name, info->type, sizeof(client->name));
269 /* Check for address business */
270 status = i2c_check_addr(adap, client->addr);
271 if (status)
272 goto out_err;
274 client->dev.parent = &client->adapter->dev;
275 client->dev.bus = &i2c_bus_type;
276 client->dev.release = i2c_client_dev_release;
278 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
279 client->addr);
280 status = device_register(&client->dev);
281 if (status)
282 goto out_err;
284 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
285 client->name, dev_name(&client->dev));
287 return client;
289 out_err:
290 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
291 "(%d)\n", client->name, client->addr, status);
292 kfree(client);
293 return NULL;
295 EXPORT_SYMBOL_GPL(i2c_new_device);
299 * i2c_unregister_device - reverse effect of i2c_new_device()
300 * @client: value returned from i2c_new_device()
301 * Context: can sleep
303 void i2c_unregister_device(struct i2c_client *client)
305 device_unregister(&client->dev);
307 EXPORT_SYMBOL_GPL(i2c_unregister_device);
310 static const struct i2c_device_id dummy_id[] = {
311 { "dummy", 0 },
312 { },
315 static int dummy_probe(struct i2c_client *client,
316 const struct i2c_device_id *id)
318 return 0;
321 static int dummy_remove(struct i2c_client *client)
323 return 0;
326 static struct i2c_driver dummy_driver = {
327 .driver.name = "dummy",
328 .probe = dummy_probe,
329 .remove = dummy_remove,
330 .id_table = dummy_id,
334 * i2c_new_dummy - return a new i2c device bound to a dummy driver
335 * @adapter: the adapter managing the device
336 * @address: seven bit address to be used
337 * Context: can sleep
339 * This returns an I2C client bound to the "dummy" driver, intended for use
340 * with devices that consume multiple addresses. Examples of such chips
341 * include various EEPROMS (like 24c04 and 24c08 models).
343 * These dummy devices have two main uses. First, most I2C and SMBus calls
344 * except i2c_transfer() need a client handle; the dummy will be that handle.
345 * And second, this prevents the specified address from being bound to a
346 * different driver.
348 * This returns the new i2c client, which should be saved for later use with
349 * i2c_unregister_device(); or NULL to indicate an error.
351 struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
353 struct i2c_board_info info = {
354 I2C_BOARD_INFO("dummy", address),
357 return i2c_new_device(adapter, &info);
359 EXPORT_SYMBOL_GPL(i2c_new_dummy);
361 /* ------------------------------------------------------------------------- */
363 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
365 static void i2c_adapter_dev_release(struct device *dev)
367 struct i2c_adapter *adap = to_i2c_adapter(dev);
368 complete(&adap->dev_released);
371 static ssize_t
372 show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
374 struct i2c_adapter *adap = to_i2c_adapter(dev);
375 return sprintf(buf, "%s\n", adap->name);
379 * Let users instantiate I2C devices through sysfs. This can be used when
380 * platform initialization code doesn't contain the proper data for
381 * whatever reason. Also useful for drivers that do device detection and
382 * detection fails, either because the device uses an unexpected address,
383 * or this is a compatible device with different ID register values.
385 * Parameter checking may look overzealous, but we really don't want
386 * the user to provide incorrect parameters.
388 static ssize_t
389 i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
390 const char *buf, size_t count)
392 struct i2c_adapter *adap = to_i2c_adapter(dev);
393 struct i2c_board_info info;
394 struct i2c_client *client;
395 char *blank, end;
396 int res;
398 dev_warn(dev, "The new_device interface is still experimental "
399 "and may change in a near future\n");
400 memset(&info, 0, sizeof(struct i2c_board_info));
402 blank = strchr(buf, ' ');
403 if (!blank) {
404 dev_err(dev, "%s: Missing parameters\n", "new_device");
405 return -EINVAL;
407 if (blank - buf > I2C_NAME_SIZE - 1) {
408 dev_err(dev, "%s: Invalid device name\n", "new_device");
409 return -EINVAL;
411 memcpy(info.type, buf, blank - buf);
413 /* Parse remaining parameters, reject extra parameters */
414 res = sscanf(++blank, "%hi%c", &info.addr, &end);
415 if (res < 1) {
416 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
417 return -EINVAL;
419 if (res > 1 && end != '\n') {
420 dev_err(dev, "%s: Extra parameters\n", "new_device");
421 return -EINVAL;
424 if (info.addr < 0x03 || info.addr > 0x77) {
425 dev_err(dev, "%s: Invalid I2C address 0x%hx\n", "new_device",
426 info.addr);
427 return -EINVAL;
430 client = i2c_new_device(adap, &info);
431 if (!client)
432 return -EEXIST;
434 /* Keep track of the added device */
435 mutex_lock(&core_lock);
436 list_add_tail(&client->detected, &userspace_devices);
437 mutex_unlock(&core_lock);
438 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
439 info.type, info.addr);
441 return count;
445 * And of course let the users delete the devices they instantiated, if
446 * they got it wrong. This interface can only be used to delete devices
447 * instantiated by i2c_sysfs_new_device above. This guarantees that we
448 * don't delete devices to which some kernel code still has references.
450 * Parameter checking may look overzealous, but we really don't want
451 * the user to delete the wrong device.
453 static ssize_t
454 i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
455 const char *buf, size_t count)
457 struct i2c_adapter *adap = to_i2c_adapter(dev);
458 struct i2c_client *client, *next;
459 unsigned short addr;
460 char end;
461 int res;
463 /* Parse parameters, reject extra parameters */
464 res = sscanf(buf, "%hi%c", &addr, &end);
465 if (res < 1) {
466 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
467 return -EINVAL;
469 if (res > 1 && end != '\n') {
470 dev_err(dev, "%s: Extra parameters\n", "delete_device");
471 return -EINVAL;
474 /* Make sure the device was added through sysfs */
475 res = -ENOENT;
476 mutex_lock(&core_lock);
477 list_for_each_entry_safe(client, next, &userspace_devices, detected) {
478 if (client->addr == addr && client->adapter == adap) {
479 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
480 "delete_device", client->name, client->addr);
482 list_del(&client->detected);
483 i2c_unregister_device(client);
484 res = count;
485 break;
488 mutex_unlock(&core_lock);
490 if (res < 0)
491 dev_err(dev, "%s: Can't find device in list\n",
492 "delete_device");
493 return res;
496 static struct device_attribute i2c_adapter_attrs[] = {
497 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
498 __ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device),
499 __ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device),
500 { },
503 static struct class i2c_adapter_class = {
504 .owner = THIS_MODULE,
505 .name = "i2c-adapter",
506 .dev_attrs = i2c_adapter_attrs,
509 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
511 struct i2c_devinfo *devinfo;
513 down_read(&__i2c_board_lock);
514 list_for_each_entry(devinfo, &__i2c_board_list, list) {
515 if (devinfo->busnum == adapter->nr
516 && !i2c_new_device(adapter,
517 &devinfo->board_info))
518 dev_err(&adapter->dev,
519 "Can't create device at 0x%02x\n",
520 devinfo->board_info.addr);
522 up_read(&__i2c_board_lock);
525 static int i2c_do_add_adapter(struct device_driver *d, void *data)
527 struct i2c_driver *driver = to_i2c_driver(d);
528 struct i2c_adapter *adap = data;
530 /* Detect supported devices on that bus, and instantiate them */
531 i2c_detect(adap, driver);
533 /* Let legacy drivers scan this bus for matching devices */
534 if (driver->attach_adapter) {
535 /* We ignore the return code; if it fails, too bad */
536 driver->attach_adapter(adap);
538 return 0;
541 static int i2c_register_adapter(struct i2c_adapter *adap)
543 int res = 0, dummy;
545 /* Can't register until after driver model init */
546 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
547 res = -EAGAIN;
548 goto out_list;
551 mutex_init(&adap->bus_lock);
553 /* Set default timeout to 1 second if not already set */
554 if (adap->timeout == 0)
555 adap->timeout = HZ;
557 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
558 adap->dev.release = &i2c_adapter_dev_release;
559 adap->dev.class = &i2c_adapter_class;
560 res = device_register(&adap->dev);
561 if (res)
562 goto out_list;
564 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
566 /* create pre-declared device nodes */
567 if (adap->nr < __i2c_first_dynamic_bus_num)
568 i2c_scan_static_board_info(adap);
570 /* Notify drivers */
571 mutex_lock(&core_lock);
572 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
573 i2c_do_add_adapter);
574 mutex_unlock(&core_lock);
576 return 0;
578 out_list:
579 mutex_lock(&core_lock);
580 idr_remove(&i2c_adapter_idr, adap->nr);
581 mutex_unlock(&core_lock);
582 return res;
586 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
587 * @adapter: the adapter to add
588 * Context: can sleep
590 * This routine is used to declare an I2C adapter when its bus number
591 * doesn't matter. Examples: for I2C adapters dynamically added by
592 * USB links or PCI plugin cards.
594 * When this returns zero, a new bus number was allocated and stored
595 * in adap->nr, and the specified adapter became available for clients.
596 * Otherwise, a negative errno value is returned.
598 int i2c_add_adapter(struct i2c_adapter *adapter)
600 int id, res = 0;
602 retry:
603 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
604 return -ENOMEM;
606 mutex_lock(&core_lock);
607 /* "above" here means "above or equal to", sigh */
608 res = idr_get_new_above(&i2c_adapter_idr, adapter,
609 __i2c_first_dynamic_bus_num, &id);
610 mutex_unlock(&core_lock);
612 if (res < 0) {
613 if (res == -EAGAIN)
614 goto retry;
615 return res;
618 adapter->nr = id;
619 return i2c_register_adapter(adapter);
621 EXPORT_SYMBOL(i2c_add_adapter);
624 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
625 * @adap: the adapter to register (with adap->nr initialized)
626 * Context: can sleep
628 * This routine is used to declare an I2C adapter when its bus number
629 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
630 * or otherwise built in to the system's mainboard, and where i2c_board_info
631 * is used to properly configure I2C devices.
633 * If no devices have pre-been declared for this bus, then be sure to
634 * register the adapter before any dynamically allocated ones. Otherwise
635 * the required bus ID may not be available.
637 * When this returns zero, the specified adapter became available for
638 * clients using the bus number provided in adap->nr. Also, the table
639 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
640 * and the appropriate driver model device nodes are created. Otherwise, a
641 * negative errno value is returned.
643 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
645 int id;
646 int status;
648 if (adap->nr & ~MAX_ID_MASK)
649 return -EINVAL;
651 retry:
652 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
653 return -ENOMEM;
655 mutex_lock(&core_lock);
656 /* "above" here means "above or equal to", sigh;
657 * we need the "equal to" result to force the result
659 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
660 if (status == 0 && id != adap->nr) {
661 status = -EBUSY;
662 idr_remove(&i2c_adapter_idr, id);
664 mutex_unlock(&core_lock);
665 if (status == -EAGAIN)
666 goto retry;
668 if (status == 0)
669 status = i2c_register_adapter(adap);
670 return status;
672 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
674 static int i2c_do_del_adapter(struct device_driver *d, void *data)
676 struct i2c_driver *driver = to_i2c_driver(d);
677 struct i2c_adapter *adapter = data;
678 struct i2c_client *client, *_n;
679 int res;
681 /* Remove the devices we created ourselves as the result of hardware
682 * probing (using a driver's detect method) */
683 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
684 if (client->adapter == adapter) {
685 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
686 client->name, client->addr);
687 list_del(&client->detected);
688 i2c_unregister_device(client);
692 if (!driver->detach_adapter)
693 return 0;
694 res = driver->detach_adapter(adapter);
695 if (res)
696 dev_err(&adapter->dev, "detach_adapter failed (%d) "
697 "for driver [%s]\n", res, driver->driver.name);
698 return res;
701 static int __unregister_client(struct device *dev, void *dummy)
703 struct i2c_client *client = i2c_verify_client(dev);
704 if (client)
705 i2c_unregister_device(client);
706 return 0;
710 * i2c_del_adapter - unregister I2C adapter
711 * @adap: the adapter being unregistered
712 * Context: can sleep
714 * This unregisters an I2C adapter which was previously registered
715 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
717 int i2c_del_adapter(struct i2c_adapter *adap)
719 int res = 0;
720 struct i2c_adapter *found;
721 struct i2c_client *client, *next;
723 /* First make sure that this adapter was ever added */
724 mutex_lock(&core_lock);
725 found = idr_find(&i2c_adapter_idr, adap->nr);
726 mutex_unlock(&core_lock);
727 if (found != adap) {
728 pr_debug("i2c-core: attempting to delete unregistered "
729 "adapter [%s]\n", adap->name);
730 return -EINVAL;
733 /* Tell drivers about this removal */
734 mutex_lock(&core_lock);
735 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
736 i2c_do_del_adapter);
737 mutex_unlock(&core_lock);
738 if (res)
739 return res;
741 /* Remove devices instantiated from sysfs */
742 list_for_each_entry_safe(client, next, &userspace_devices, detected) {
743 if (client->adapter == adap) {
744 dev_dbg(&adap->dev, "Removing %s at 0x%x\n",
745 client->name, client->addr);
746 list_del(&client->detected);
747 i2c_unregister_device(client);
751 /* Detach any active clients. This can't fail, thus we do not
752 checking the returned value. */
753 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
755 /* clean up the sysfs representation */
756 init_completion(&adap->dev_released);
757 device_unregister(&adap->dev);
759 /* wait for sysfs to drop all references */
760 wait_for_completion(&adap->dev_released);
762 /* free bus id */
763 mutex_lock(&core_lock);
764 idr_remove(&i2c_adapter_idr, adap->nr);
765 mutex_unlock(&core_lock);
767 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
769 /* Clear the device structure in case this adapter is ever going to be
770 added again */
771 memset(&adap->dev, 0, sizeof(adap->dev));
773 return 0;
775 EXPORT_SYMBOL(i2c_del_adapter);
778 /* ------------------------------------------------------------------------- */
780 static int __attach_adapter(struct device *dev, void *data)
782 struct i2c_adapter *adapter = to_i2c_adapter(dev);
783 struct i2c_driver *driver = data;
785 i2c_detect(adapter, driver);
787 /* Legacy drivers scan i2c busses directly */
788 if (driver->attach_adapter)
789 driver->attach_adapter(adapter);
791 return 0;
795 * An i2c_driver is used with one or more i2c_client (device) nodes to access
796 * i2c slave chips, on a bus instance associated with some i2c_adapter.
799 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
801 int res;
803 /* Can't register until after driver model init */
804 if (unlikely(WARN_ON(!i2c_bus_type.p)))
805 return -EAGAIN;
807 /* add the driver to the list of i2c drivers in the driver core */
808 driver->driver.owner = owner;
809 driver->driver.bus = &i2c_bus_type;
811 /* When registration returns, the driver core
812 * will have called probe() for all matching-but-unbound devices.
814 res = driver_register(&driver->driver);
815 if (res)
816 return res;
818 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
820 INIT_LIST_HEAD(&driver->clients);
821 /* Walk the adapters that are already present */
822 mutex_lock(&core_lock);
823 class_for_each_device(&i2c_adapter_class, NULL, driver,
824 __attach_adapter);
825 mutex_unlock(&core_lock);
827 return 0;
829 EXPORT_SYMBOL(i2c_register_driver);
831 static int __detach_adapter(struct device *dev, void *data)
833 struct i2c_adapter *adapter = to_i2c_adapter(dev);
834 struct i2c_driver *driver = data;
835 struct i2c_client *client, *_n;
837 /* Remove the devices we created ourselves as the result of hardware
838 * probing (using a driver's detect method) */
839 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
840 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
841 client->name, client->addr);
842 list_del(&client->detected);
843 i2c_unregister_device(client);
846 if (driver->detach_adapter) {
847 if (driver->detach_adapter(adapter))
848 dev_err(&adapter->dev,
849 "detach_adapter failed for driver [%s]\n",
850 driver->driver.name);
853 return 0;
857 * i2c_del_driver - unregister I2C driver
858 * @driver: the driver being unregistered
859 * Context: can sleep
861 void i2c_del_driver(struct i2c_driver *driver)
863 mutex_lock(&core_lock);
864 class_for_each_device(&i2c_adapter_class, NULL, driver,
865 __detach_adapter);
866 mutex_unlock(&core_lock);
868 driver_unregister(&driver->driver);
869 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
871 EXPORT_SYMBOL(i2c_del_driver);
873 /* ------------------------------------------------------------------------- */
875 static int __i2c_check_addr(struct device *dev, void *addrp)
877 struct i2c_client *client = i2c_verify_client(dev);
878 int addr = *(int *)addrp;
880 if (client && client->addr == addr)
881 return -EBUSY;
882 return 0;
885 static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
887 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
891 * i2c_use_client - increments the reference count of the i2c client structure
892 * @client: the client being referenced
894 * Each live reference to a client should be refcounted. The driver model does
895 * that automatically as part of driver binding, so that most drivers don't
896 * need to do this explicitly: they hold a reference until they're unbound
897 * from the device.
899 * A pointer to the client with the incremented reference counter is returned.
901 struct i2c_client *i2c_use_client(struct i2c_client *client)
903 if (client && get_device(&client->dev))
904 return client;
905 return NULL;
907 EXPORT_SYMBOL(i2c_use_client);
910 * i2c_release_client - release a use of the i2c client structure
911 * @client: the client being no longer referenced
913 * Must be called when a user of a client is finished with it.
915 void i2c_release_client(struct i2c_client *client)
917 if (client)
918 put_device(&client->dev);
920 EXPORT_SYMBOL(i2c_release_client);
922 struct i2c_cmd_arg {
923 unsigned cmd;
924 void *arg;
927 static int i2c_cmd(struct device *dev, void *_arg)
929 struct i2c_client *client = i2c_verify_client(dev);
930 struct i2c_cmd_arg *arg = _arg;
932 if (client && client->driver && client->driver->command)
933 client->driver->command(client, arg->cmd, arg->arg);
934 return 0;
937 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
939 struct i2c_cmd_arg cmd_arg;
941 cmd_arg.cmd = cmd;
942 cmd_arg.arg = arg;
943 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
945 EXPORT_SYMBOL(i2c_clients_command);
947 static int __init i2c_init(void)
949 int retval;
951 retval = bus_register(&i2c_bus_type);
952 if (retval)
953 return retval;
954 retval = class_register(&i2c_adapter_class);
955 if (retval)
956 goto bus_err;
957 retval = i2c_add_driver(&dummy_driver);
958 if (retval)
959 goto class_err;
960 return 0;
962 class_err:
963 class_unregister(&i2c_adapter_class);
964 bus_err:
965 bus_unregister(&i2c_bus_type);
966 return retval;
969 static void __exit i2c_exit(void)
971 i2c_del_driver(&dummy_driver);
972 class_unregister(&i2c_adapter_class);
973 bus_unregister(&i2c_bus_type);
976 /* We must initialize early, because some subsystems register i2c drivers
977 * in subsys_initcall() code, but are linked (and initialized) before i2c.
979 postcore_initcall(i2c_init);
980 module_exit(i2c_exit);
982 /* ----------------------------------------------------
983 * the functional interface to the i2c busses.
984 * ----------------------------------------------------
988 * i2c_transfer - execute a single or combined I2C message
989 * @adap: Handle to I2C bus
990 * @msgs: One or more messages to execute before STOP is issued to
991 * terminate the operation; each message begins with a START.
992 * @num: Number of messages to be executed.
994 * Returns negative errno, else the number of messages executed.
996 * Note that there is no requirement that each message be sent to
997 * the same slave address, although that is the most common model.
999 int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1001 unsigned long orig_jiffies;
1002 int ret, try;
1004 /* REVISIT the fault reporting model here is weak:
1006 * - When we get an error after receiving N bytes from a slave,
1007 * there is no way to report "N".
1009 * - When we get a NAK after transmitting N bytes to a slave,
1010 * there is no way to report "N" ... or to let the master
1011 * continue executing the rest of this combined message, if
1012 * that's the appropriate response.
1014 * - When for example "num" is two and we successfully complete
1015 * the first message but get an error part way through the
1016 * second, it's unclear whether that should be reported as
1017 * one (discarding status on the second message) or errno
1018 * (discarding status on the first one).
1021 if (adap->algo->master_xfer) {
1022 #ifdef DEBUG
1023 for (ret = 0; ret < num; ret++) {
1024 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
1025 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1026 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1027 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
1029 #endif
1031 if (in_atomic() || irqs_disabled()) {
1032 ret = mutex_trylock(&adap->bus_lock);
1033 if (!ret)
1034 /* I2C activity is ongoing. */
1035 return -EAGAIN;
1036 } else {
1037 mutex_lock_nested(&adap->bus_lock, adap->level);
1040 /* Retry automatically on arbitration loss */
1041 orig_jiffies = jiffies;
1042 for (ret = 0, try = 0; try <= adap->retries; try++) {
1043 ret = adap->algo->master_xfer(adap, msgs, num);
1044 if (ret != -EAGAIN)
1045 break;
1046 if (time_after(jiffies, orig_jiffies + adap->timeout))
1047 break;
1049 mutex_unlock(&adap->bus_lock);
1051 return ret;
1052 } else {
1053 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
1054 return -EOPNOTSUPP;
1057 EXPORT_SYMBOL(i2c_transfer);
1060 * i2c_master_send - issue a single I2C message in master transmit mode
1061 * @client: Handle to slave device
1062 * @buf: Data that will be written to the slave
1063 * @count: How many bytes to write
1065 * Returns negative errno, or else the number of bytes written.
1067 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
1069 int ret;
1070 struct i2c_adapter *adap=client->adapter;
1071 struct i2c_msg msg;
1073 msg.addr = client->addr;
1074 msg.flags = client->flags & I2C_M_TEN;
1075 msg.len = count;
1076 msg.buf = (char *)buf;
1078 ret = i2c_transfer(adap, &msg, 1);
1080 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1081 transmitted, else error code. */
1082 return (ret == 1) ? count : ret;
1084 EXPORT_SYMBOL(i2c_master_send);
1087 * i2c_master_recv - issue a single I2C message in master receive mode
1088 * @client: Handle to slave device
1089 * @buf: Where to store data read from slave
1090 * @count: How many bytes to read
1092 * Returns negative errno, or else the number of bytes read.
1094 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1096 struct i2c_adapter *adap=client->adapter;
1097 struct i2c_msg msg;
1098 int ret;
1100 msg.addr = client->addr;
1101 msg.flags = client->flags & I2C_M_TEN;
1102 msg.flags |= I2C_M_RD;
1103 msg.len = count;
1104 msg.buf = buf;
1106 ret = i2c_transfer(adap, &msg, 1);
1108 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1109 transmitted, else error code. */
1110 return (ret == 1) ? count : ret;
1112 EXPORT_SYMBOL(i2c_master_recv);
1114 /* ----------------------------------------------------
1115 * the i2c address scanning function
1116 * Will not work for 10-bit addresses!
1117 * ----------------------------------------------------
1120 static int i2c_detect_address(struct i2c_client *temp_client, int kind,
1121 struct i2c_driver *driver)
1123 struct i2c_board_info info;
1124 struct i2c_adapter *adapter = temp_client->adapter;
1125 int addr = temp_client->addr;
1126 int err;
1128 /* Make sure the address is valid */
1129 if (addr < 0x03 || addr > 0x77) {
1130 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1131 addr);
1132 return -EINVAL;
1135 /* Skip if already in use */
1136 if (i2c_check_addr(adapter, addr))
1137 return 0;
1139 /* Make sure there is something at this address, unless forced */
1140 if (kind < 0) {
1141 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1142 I2C_SMBUS_QUICK, NULL) < 0)
1143 return 0;
1145 /* prevent 24RF08 corruption */
1146 if ((addr & ~0x0f) == 0x50)
1147 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1148 I2C_SMBUS_QUICK, NULL);
1151 /* Finally call the custom detection function */
1152 memset(&info, 0, sizeof(struct i2c_board_info));
1153 info.addr = addr;
1154 err = driver->detect(temp_client, kind, &info);
1155 if (err) {
1156 /* -ENODEV is returned if the detection fails. We catch it
1157 here as this isn't an error. */
1158 return err == -ENODEV ? 0 : err;
1161 /* Consistency check */
1162 if (info.type[0] == '\0') {
1163 dev_err(&adapter->dev, "%s detection function provided "
1164 "no name for 0x%x\n", driver->driver.name,
1165 addr);
1166 } else {
1167 struct i2c_client *client;
1169 /* Detection succeeded, instantiate the device */
1170 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1171 info.type, info.addr);
1172 client = i2c_new_device(adapter, &info);
1173 if (client)
1174 list_add_tail(&client->detected, &driver->clients);
1175 else
1176 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1177 info.type, info.addr);
1179 return 0;
1182 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1184 const struct i2c_client_address_data *address_data;
1185 struct i2c_client *temp_client;
1186 int i, err = 0;
1187 int adap_id = i2c_adapter_id(adapter);
1189 address_data = driver->address_data;
1190 if (!driver->detect || !address_data)
1191 return 0;
1193 /* Set up a temporary client to help detect callback */
1194 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1195 if (!temp_client)
1196 return -ENOMEM;
1197 temp_client->adapter = adapter;
1199 /* Force entries are done first, and are not affected by ignore
1200 entries */
1201 if (address_data->forces) {
1202 const unsigned short * const *forces = address_data->forces;
1203 int kind;
1205 for (kind = 0; forces[kind]; kind++) {
1206 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1207 i += 2) {
1208 if (forces[kind][i] == adap_id
1209 || forces[kind][i] == ANY_I2C_BUS) {
1210 dev_dbg(&adapter->dev, "found force "
1211 "parameter for adapter %d, "
1212 "addr 0x%02x, kind %d\n",
1213 adap_id, forces[kind][i + 1],
1214 kind);
1215 temp_client->addr = forces[kind][i + 1];
1216 err = i2c_detect_address(temp_client,
1217 kind, driver);
1218 if (err)
1219 goto exit_free;
1225 /* Stop here if the classes do not match */
1226 if (!(adapter->class & driver->class))
1227 goto exit_free;
1229 /* Stop here if we can't use SMBUS_QUICK */
1230 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1231 if (address_data->probe[0] == I2C_CLIENT_END
1232 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1233 goto exit_free;
1235 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1236 "can't probe for chips\n");
1237 err = -EOPNOTSUPP;
1238 goto exit_free;
1241 /* Probe entries are done second, and are not affected by ignore
1242 entries either */
1243 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1244 if (address_data->probe[i] == adap_id
1245 || address_data->probe[i] == ANY_I2C_BUS) {
1246 dev_dbg(&adapter->dev, "found probe parameter for "
1247 "adapter %d, addr 0x%02x\n", adap_id,
1248 address_data->probe[i + 1]);
1249 temp_client->addr = address_data->probe[i + 1];
1250 err = i2c_detect_address(temp_client, -1, driver);
1251 if (err)
1252 goto exit_free;
1256 /* Normal entries are done last, unless shadowed by an ignore entry */
1257 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1258 int j, ignore;
1260 ignore = 0;
1261 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1262 j += 2) {
1263 if ((address_data->ignore[j] == adap_id ||
1264 address_data->ignore[j] == ANY_I2C_BUS)
1265 && address_data->ignore[j + 1]
1266 == address_data->normal_i2c[i]) {
1267 dev_dbg(&adapter->dev, "found ignore "
1268 "parameter for adapter %d, "
1269 "addr 0x%02x\n", adap_id,
1270 address_data->ignore[j + 1]);
1271 ignore = 1;
1272 break;
1275 if (ignore)
1276 continue;
1278 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1279 "addr 0x%02x\n", adap_id,
1280 address_data->normal_i2c[i]);
1281 temp_client->addr = address_data->normal_i2c[i];
1282 err = i2c_detect_address(temp_client, -1, driver);
1283 if (err)
1284 goto exit_free;
1287 exit_free:
1288 kfree(temp_client);
1289 return err;
1292 struct i2c_client *
1293 i2c_new_probed_device(struct i2c_adapter *adap,
1294 struct i2c_board_info *info,
1295 unsigned short const *addr_list)
1297 int i;
1299 /* Stop here if the bus doesn't support probing */
1300 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1301 dev_err(&adap->dev, "Probing not supported\n");
1302 return NULL;
1305 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1306 /* Check address validity */
1307 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1308 dev_warn(&adap->dev, "Invalid 7-bit address "
1309 "0x%02x\n", addr_list[i]);
1310 continue;
1313 /* Check address availability */
1314 if (i2c_check_addr(adap, addr_list[i])) {
1315 dev_dbg(&adap->dev, "Address 0x%02x already in "
1316 "use, not probing\n", addr_list[i]);
1317 continue;
1320 /* Test address responsiveness
1321 The default probe method is a quick write, but it is known
1322 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1323 and could also irreversibly write-protect some EEPROMs, so
1324 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1325 read instead. Also, some bus drivers don't implement
1326 quick write, so we fallback to a byte read it that case
1327 too. */
1328 if ((addr_list[i] & ~0x07) == 0x30
1329 || (addr_list[i] & ~0x0f) == 0x50
1330 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1331 union i2c_smbus_data data;
1333 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1334 I2C_SMBUS_READ, 0,
1335 I2C_SMBUS_BYTE, &data) >= 0)
1336 break;
1337 } else {
1338 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1339 I2C_SMBUS_WRITE, 0,
1340 I2C_SMBUS_QUICK, NULL) >= 0)
1341 break;
1345 if (addr_list[i] == I2C_CLIENT_END) {
1346 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1347 return NULL;
1350 info->addr = addr_list[i];
1351 return i2c_new_device(adap, info);
1353 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1355 struct i2c_adapter* i2c_get_adapter(int id)
1357 struct i2c_adapter *adapter;
1359 mutex_lock(&core_lock);
1360 adapter = idr_find(&i2c_adapter_idr, id);
1361 if (adapter && !try_module_get(adapter->owner))
1362 adapter = NULL;
1364 mutex_unlock(&core_lock);
1365 return adapter;
1367 EXPORT_SYMBOL(i2c_get_adapter);
1369 void i2c_put_adapter(struct i2c_adapter *adap)
1371 module_put(adap->owner);
1373 EXPORT_SYMBOL(i2c_put_adapter);
1375 /* The SMBus parts */
1377 #define POLY (0x1070U << 3)
1378 static u8 crc8(u16 data)
1380 int i;
1382 for(i = 0; i < 8; i++) {
1383 if (data & 0x8000)
1384 data = data ^ POLY;
1385 data = data << 1;
1387 return (u8)(data >> 8);
1390 /* Incremental CRC8 over count bytes in the array pointed to by p */
1391 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1393 int i;
1395 for(i = 0; i < count; i++)
1396 crc = crc8((crc ^ p[i]) << 8);
1397 return crc;
1400 /* Assume a 7-bit address, which is reasonable for SMBus */
1401 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1403 /* The address will be sent first */
1404 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1405 pec = i2c_smbus_pec(pec, &addr, 1);
1407 /* The data buffer follows */
1408 return i2c_smbus_pec(pec, msg->buf, msg->len);
1411 /* Used for write only transactions */
1412 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1414 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1415 msg->len++;
1418 /* Return <0 on CRC error
1419 If there was a write before this read (most cases) we need to take the
1420 partial CRC from the write part into account.
1421 Note that this function does modify the message (we need to decrease the
1422 message length to hide the CRC byte from the caller). */
1423 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1425 u8 rpec = msg->buf[--msg->len];
1426 cpec = i2c_smbus_msg_pec(cpec, msg);
1428 if (rpec != cpec) {
1429 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1430 rpec, cpec);
1431 return -EBADMSG;
1433 return 0;
1437 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1438 * @client: Handle to slave device
1440 * This executes the SMBus "receive byte" protocol, returning negative errno
1441 * else the byte received from the device.
1443 s32 i2c_smbus_read_byte(struct i2c_client *client)
1445 union i2c_smbus_data data;
1446 int status;
1448 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1449 I2C_SMBUS_READ, 0,
1450 I2C_SMBUS_BYTE, &data);
1451 return (status < 0) ? status : data.byte;
1453 EXPORT_SYMBOL(i2c_smbus_read_byte);
1456 * i2c_smbus_write_byte - SMBus "send byte" protocol
1457 * @client: Handle to slave device
1458 * @value: Byte to be sent
1460 * This executes the SMBus "send byte" protocol, returning negative errno
1461 * else zero on success.
1463 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1465 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1466 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1468 EXPORT_SYMBOL(i2c_smbus_write_byte);
1471 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1472 * @client: Handle to slave device
1473 * @command: Byte interpreted by slave
1475 * This executes the SMBus "read byte" protocol, returning negative errno
1476 * else a data byte received from the device.
1478 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1480 union i2c_smbus_data data;
1481 int status;
1483 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1484 I2C_SMBUS_READ, command,
1485 I2C_SMBUS_BYTE_DATA, &data);
1486 return (status < 0) ? status : data.byte;
1488 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1491 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1492 * @client: Handle to slave device
1493 * @command: Byte interpreted by slave
1494 * @value: Byte being written
1496 * This executes the SMBus "write byte" protocol, returning negative errno
1497 * else zero on success.
1499 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1501 union i2c_smbus_data data;
1502 data.byte = value;
1503 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1504 I2C_SMBUS_WRITE,command,
1505 I2C_SMBUS_BYTE_DATA,&data);
1507 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1510 * i2c_smbus_read_word_data - SMBus "read word" protocol
1511 * @client: Handle to slave device
1512 * @command: Byte interpreted by slave
1514 * This executes the SMBus "read word" protocol, returning negative errno
1515 * else a 16-bit unsigned "word" received from the device.
1517 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1519 union i2c_smbus_data data;
1520 int status;
1522 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1523 I2C_SMBUS_READ, command,
1524 I2C_SMBUS_WORD_DATA, &data);
1525 return (status < 0) ? status : data.word;
1527 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1530 * i2c_smbus_write_word_data - SMBus "write word" protocol
1531 * @client: Handle to slave device
1532 * @command: Byte interpreted by slave
1533 * @value: 16-bit "word" being written
1535 * This executes the SMBus "write word" protocol, returning negative errno
1536 * else zero on success.
1538 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1540 union i2c_smbus_data data;
1541 data.word = value;
1542 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1543 I2C_SMBUS_WRITE,command,
1544 I2C_SMBUS_WORD_DATA,&data);
1546 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1549 * i2c_smbus_process_call - SMBus "process call" protocol
1550 * @client: Handle to slave device
1551 * @command: Byte interpreted by slave
1552 * @value: 16-bit "word" being written
1554 * This executes the SMBus "process call" protocol, returning negative errno
1555 * else a 16-bit unsigned "word" received from the device.
1557 s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1559 union i2c_smbus_data data;
1560 int status;
1561 data.word = value;
1563 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1564 I2C_SMBUS_WRITE, command,
1565 I2C_SMBUS_PROC_CALL, &data);
1566 return (status < 0) ? status : data.word;
1568 EXPORT_SYMBOL(i2c_smbus_process_call);
1571 * i2c_smbus_read_block_data - SMBus "block read" protocol
1572 * @client: Handle to slave device
1573 * @command: Byte interpreted by slave
1574 * @values: Byte array into which data will be read; big enough to hold
1575 * the data returned by the slave. SMBus allows at most 32 bytes.
1577 * This executes the SMBus "block read" protocol, returning negative errno
1578 * else the number of data bytes in the slave's response.
1580 * Note that using this function requires that the client's adapter support
1581 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1582 * support this; its emulation through I2C messaging relies on a specific
1583 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1585 s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1586 u8 *values)
1588 union i2c_smbus_data data;
1589 int status;
1591 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1592 I2C_SMBUS_READ, command,
1593 I2C_SMBUS_BLOCK_DATA, &data);
1594 if (status)
1595 return status;
1597 memcpy(values, &data.block[1], data.block[0]);
1598 return data.block[0];
1600 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1603 * i2c_smbus_write_block_data - SMBus "block write" protocol
1604 * @client: Handle to slave device
1605 * @command: Byte interpreted by slave
1606 * @length: Size of data block; SMBus allows at most 32 bytes
1607 * @values: Byte array which will be written.
1609 * This executes the SMBus "block write" protocol, returning negative errno
1610 * else zero on success.
1612 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1613 u8 length, const u8 *values)
1615 union i2c_smbus_data data;
1617 if (length > I2C_SMBUS_BLOCK_MAX)
1618 length = I2C_SMBUS_BLOCK_MAX;
1619 data.block[0] = length;
1620 memcpy(&data.block[1], values, length);
1621 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1622 I2C_SMBUS_WRITE,command,
1623 I2C_SMBUS_BLOCK_DATA,&data);
1625 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1627 /* Returns the number of read bytes */
1628 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1629 u8 length, u8 *values)
1631 union i2c_smbus_data data;
1632 int status;
1634 if (length > I2C_SMBUS_BLOCK_MAX)
1635 length = I2C_SMBUS_BLOCK_MAX;
1636 data.block[0] = length;
1637 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1638 I2C_SMBUS_READ, command,
1639 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1640 if (status < 0)
1641 return status;
1643 memcpy(values, &data.block[1], data.block[0]);
1644 return data.block[0];
1646 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1648 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1649 u8 length, const u8 *values)
1651 union i2c_smbus_data data;
1653 if (length > I2C_SMBUS_BLOCK_MAX)
1654 length = I2C_SMBUS_BLOCK_MAX;
1655 data.block[0] = length;
1656 memcpy(data.block + 1, values, length);
1657 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1658 I2C_SMBUS_WRITE, command,
1659 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1661 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1663 /* Simulate a SMBus command using the i2c protocol
1664 No checking of parameters is done! */
1665 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1666 unsigned short flags,
1667 char read_write, u8 command, int size,
1668 union i2c_smbus_data * data)
1670 /* So we need to generate a series of msgs. In the case of writing, we
1671 need to use only one message; when reading, we need two. We initialize
1672 most things with sane defaults, to keep the code below somewhat
1673 simpler. */
1674 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1675 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1676 int num = read_write == I2C_SMBUS_READ?2:1;
1677 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1678 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1680 int i;
1681 u8 partial_pec = 0;
1682 int status;
1684 msgbuf0[0] = command;
1685 switch(size) {
1686 case I2C_SMBUS_QUICK:
1687 msg[0].len = 0;
1688 /* Special case: The read/write field is used as data */
1689 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1690 I2C_M_RD : 0);
1691 num = 1;
1692 break;
1693 case I2C_SMBUS_BYTE:
1694 if (read_write == I2C_SMBUS_READ) {
1695 /* Special case: only a read! */
1696 msg[0].flags = I2C_M_RD | flags;
1697 num = 1;
1699 break;
1700 case I2C_SMBUS_BYTE_DATA:
1701 if (read_write == I2C_SMBUS_READ)
1702 msg[1].len = 1;
1703 else {
1704 msg[0].len = 2;
1705 msgbuf0[1] = data->byte;
1707 break;
1708 case I2C_SMBUS_WORD_DATA:
1709 if (read_write == I2C_SMBUS_READ)
1710 msg[1].len = 2;
1711 else {
1712 msg[0].len=3;
1713 msgbuf0[1] = data->word & 0xff;
1714 msgbuf0[2] = data->word >> 8;
1716 break;
1717 case I2C_SMBUS_PROC_CALL:
1718 num = 2; /* Special case */
1719 read_write = I2C_SMBUS_READ;
1720 msg[0].len = 3;
1721 msg[1].len = 2;
1722 msgbuf0[1] = data->word & 0xff;
1723 msgbuf0[2] = data->word >> 8;
1724 break;
1725 case I2C_SMBUS_BLOCK_DATA:
1726 if (read_write == I2C_SMBUS_READ) {
1727 msg[1].flags |= I2C_M_RECV_LEN;
1728 msg[1].len = 1; /* block length will be added by
1729 the underlying bus driver */
1730 } else {
1731 msg[0].len = data->block[0] + 2;
1732 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1733 dev_err(&adapter->dev,
1734 "Invalid block write size %d\n",
1735 data->block[0]);
1736 return -EINVAL;
1738 for (i = 1; i < msg[0].len; i++)
1739 msgbuf0[i] = data->block[i-1];
1741 break;
1742 case I2C_SMBUS_BLOCK_PROC_CALL:
1743 num = 2; /* Another special case */
1744 read_write = I2C_SMBUS_READ;
1745 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1746 dev_err(&adapter->dev,
1747 "Invalid block write size %d\n",
1748 data->block[0]);
1749 return -EINVAL;
1751 msg[0].len = data->block[0] + 2;
1752 for (i = 1; i < msg[0].len; i++)
1753 msgbuf0[i] = data->block[i-1];
1754 msg[1].flags |= I2C_M_RECV_LEN;
1755 msg[1].len = 1; /* block length will be added by
1756 the underlying bus driver */
1757 break;
1758 case I2C_SMBUS_I2C_BLOCK_DATA:
1759 if (read_write == I2C_SMBUS_READ) {
1760 msg[1].len = data->block[0];
1761 } else {
1762 msg[0].len = data->block[0] + 1;
1763 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1764 dev_err(&adapter->dev,
1765 "Invalid block write size %d\n",
1766 data->block[0]);
1767 return -EINVAL;
1769 for (i = 1; i <= data->block[0]; i++)
1770 msgbuf0[i] = data->block[i];
1772 break;
1773 default:
1774 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1775 return -EOPNOTSUPP;
1778 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1779 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1780 if (i) {
1781 /* Compute PEC if first message is a write */
1782 if (!(msg[0].flags & I2C_M_RD)) {
1783 if (num == 1) /* Write only */
1784 i2c_smbus_add_pec(&msg[0]);
1785 else /* Write followed by read */
1786 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1788 /* Ask for PEC if last message is a read */
1789 if (msg[num-1].flags & I2C_M_RD)
1790 msg[num-1].len++;
1793 status = i2c_transfer(adapter, msg, num);
1794 if (status < 0)
1795 return status;
1797 /* Check PEC if last message is a read */
1798 if (i && (msg[num-1].flags & I2C_M_RD)) {
1799 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1800 if (status < 0)
1801 return status;
1804 if (read_write == I2C_SMBUS_READ)
1805 switch(size) {
1806 case I2C_SMBUS_BYTE:
1807 data->byte = msgbuf0[0];
1808 break;
1809 case I2C_SMBUS_BYTE_DATA:
1810 data->byte = msgbuf1[0];
1811 break;
1812 case I2C_SMBUS_WORD_DATA:
1813 case I2C_SMBUS_PROC_CALL:
1814 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1815 break;
1816 case I2C_SMBUS_I2C_BLOCK_DATA:
1817 for (i = 0; i < data->block[0]; i++)
1818 data->block[i+1] = msgbuf1[i];
1819 break;
1820 case I2C_SMBUS_BLOCK_DATA:
1821 case I2C_SMBUS_BLOCK_PROC_CALL:
1822 for (i = 0; i < msgbuf1[0] + 1; i++)
1823 data->block[i] = msgbuf1[i];
1824 break;
1826 return 0;
1830 * i2c_smbus_xfer - execute SMBus protocol operations
1831 * @adapter: Handle to I2C bus
1832 * @addr: Address of SMBus slave on that bus
1833 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1834 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1835 * @command: Byte interpreted by slave, for protocols which use such bytes
1836 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1837 * @data: Data to be read or written
1839 * This executes an SMBus protocol operation, and returns a negative
1840 * errno code else zero on success.
1842 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
1843 char read_write, u8 command, int protocol,
1844 union i2c_smbus_data *data)
1846 unsigned long orig_jiffies;
1847 int try;
1848 s32 res;
1850 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1852 if (adapter->algo->smbus_xfer) {
1853 mutex_lock(&adapter->bus_lock);
1855 /* Retry automatically on arbitration loss */
1856 orig_jiffies = jiffies;
1857 for (res = 0, try = 0; try <= adapter->retries; try++) {
1858 res = adapter->algo->smbus_xfer(adapter, addr, flags,
1859 read_write, command,
1860 protocol, data);
1861 if (res != -EAGAIN)
1862 break;
1863 if (time_after(jiffies,
1864 orig_jiffies + adapter->timeout))
1865 break;
1867 mutex_unlock(&adapter->bus_lock);
1868 } else
1869 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1870 command, protocol, data);
1872 return res;
1874 EXPORT_SYMBOL(i2c_smbus_xfer);
1876 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1877 MODULE_DESCRIPTION("I2C-Bus main module");
1878 MODULE_LICENSE("GPL");