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 <linux/pm_runtime.h>
38 #include <asm/uaccess.h>
43 /* core_lock protects i2c_adapter_idr, and guarantees
44 that device detection, deletion of detected devices, and attach_adapter
45 and detach_adapter calls are serialized */
46 static DEFINE_MUTEX(core_lock);
47 static DEFINE_IDR(i2c_adapter_idr);
49 static struct device_type i2c_client_type;
50 static int i2c_check_addr(struct i2c_adapter *adapter, int addr);
51 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
53 /* ------------------------------------------------------------------------- */
55 static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
56 const struct i2c_client *client)
59 if (strcmp(client->name, id->name) == 0)
66 static int i2c_device_match(struct device *dev, struct device_driver *drv)
68 struct i2c_client *client = i2c_verify_client(dev);
69 struct i2c_driver *driver;
74 driver = to_i2c_driver(drv);
75 /* match on an id table if there is one */
77 return i2c_match_id(driver->id_table, client) != NULL;
84 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
85 static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
87 struct i2c_client *client = to_i2c_client(dev);
89 if (add_uevent_var(env, "MODALIAS=%s%s",
90 I2C_MODULE_PREFIX, client->name))
92 dev_dbg(dev, "uevent\n");
97 #define i2c_device_uevent NULL
98 #endif /* CONFIG_HOTPLUG */
100 static int i2c_device_probe(struct device *dev)
102 struct i2c_client *client = i2c_verify_client(dev);
103 struct i2c_driver *driver;
109 driver = to_i2c_driver(dev->driver);
110 if (!driver->probe || !driver->id_table)
112 client->driver = driver;
113 if (!device_can_wakeup(&client->dev))
114 device_init_wakeup(&client->dev,
115 client->flags & I2C_CLIENT_WAKE);
116 dev_dbg(dev, "probe\n");
118 status = driver->probe(client, i2c_match_id(driver->id_table, client));
120 client->driver = NULL;
121 i2c_set_clientdata(client, NULL);
126 static int i2c_device_remove(struct device *dev)
128 struct i2c_client *client = i2c_verify_client(dev);
129 struct i2c_driver *driver;
132 if (!client || !dev->driver)
135 driver = to_i2c_driver(dev->driver);
136 if (driver->remove) {
137 dev_dbg(dev, "remove\n");
138 status = driver->remove(client);
144 client->driver = NULL;
145 i2c_set_clientdata(client, NULL);
150 static void i2c_device_shutdown(struct device *dev)
152 struct i2c_client *client = i2c_verify_client(dev);
153 struct i2c_driver *driver;
155 if (!client || !dev->driver)
157 driver = to_i2c_driver(dev->driver);
158 if (driver->shutdown)
159 driver->shutdown(client);
162 #ifdef CONFIG_PM_SLEEP
163 static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
165 struct i2c_client *client = i2c_verify_client(dev);
166 struct i2c_driver *driver;
168 if (!client || !dev->driver)
170 driver = to_i2c_driver(dev->driver);
171 if (!driver->suspend)
173 return driver->suspend(client, mesg);
176 static int i2c_legacy_resume(struct device *dev)
178 struct i2c_client *client = i2c_verify_client(dev);
179 struct i2c_driver *driver;
181 if (!client || !dev->driver)
183 driver = to_i2c_driver(dev->driver);
186 return driver->resume(client);
189 static int i2c_device_pm_suspend(struct device *dev)
191 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
193 if (pm_runtime_suspended(dev))
197 return pm->suspend ? pm->suspend(dev) : 0;
199 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
202 static int i2c_device_pm_resume(struct device *dev)
204 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
208 ret = pm->resume ? pm->resume(dev) : 0;
210 ret = i2c_legacy_resume(dev);
213 pm_runtime_disable(dev);
214 pm_runtime_set_active(dev);
215 pm_runtime_enable(dev);
221 static int i2c_device_pm_freeze(struct device *dev)
223 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
225 if (pm_runtime_suspended(dev))
229 return pm->freeze ? pm->freeze(dev) : 0;
231 return i2c_legacy_suspend(dev, PMSG_FREEZE);
234 static int i2c_device_pm_thaw(struct device *dev)
236 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
238 if (pm_runtime_suspended(dev))
242 return pm->thaw ? pm->thaw(dev) : 0;
244 return i2c_legacy_resume(dev);
247 static int i2c_device_pm_poweroff(struct device *dev)
249 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
251 if (pm_runtime_suspended(dev))
255 return pm->poweroff ? pm->poweroff(dev) : 0;
257 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
260 static int i2c_device_pm_restore(struct device *dev)
262 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
266 ret = pm->restore ? pm->restore(dev) : 0;
268 ret = i2c_legacy_resume(dev);
271 pm_runtime_disable(dev);
272 pm_runtime_set_active(dev);
273 pm_runtime_enable(dev);
278 #else /* !CONFIG_PM_SLEEP */
279 #define i2c_device_pm_suspend NULL
280 #define i2c_device_pm_resume NULL
281 #define i2c_device_pm_freeze NULL
282 #define i2c_device_pm_thaw NULL
283 #define i2c_device_pm_poweroff NULL
284 #define i2c_device_pm_restore NULL
285 #endif /* !CONFIG_PM_SLEEP */
287 static void i2c_client_dev_release(struct device *dev)
289 kfree(to_i2c_client(dev));
293 show_name(struct device *dev, struct device_attribute *attr, char *buf)
295 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
296 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
300 show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
302 struct i2c_client *client = to_i2c_client(dev);
303 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
306 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
307 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
309 static struct attribute *i2c_dev_attrs[] = {
311 /* modalias helps coldplug: modprobe $(cat .../modalias) */
312 &dev_attr_modalias.attr,
316 static struct attribute_group i2c_dev_attr_group = {
317 .attrs = i2c_dev_attrs,
320 static const struct attribute_group *i2c_dev_attr_groups[] = {
325 static const struct dev_pm_ops i2c_device_pm_ops = {
326 .suspend = i2c_device_pm_suspend,
327 .resume = i2c_device_pm_resume,
328 .freeze = i2c_device_pm_freeze,
329 .thaw = i2c_device_pm_thaw,
330 .poweroff = i2c_device_pm_poweroff,
331 .restore = i2c_device_pm_restore,
333 pm_generic_runtime_suspend,
334 pm_generic_runtime_resume,
335 pm_generic_runtime_idle
339 struct bus_type i2c_bus_type = {
341 .match = i2c_device_match,
342 .probe = i2c_device_probe,
343 .remove = i2c_device_remove,
344 .shutdown = i2c_device_shutdown,
345 .pm = &i2c_device_pm_ops,
347 EXPORT_SYMBOL_GPL(i2c_bus_type);
349 static struct device_type i2c_client_type = {
350 .groups = i2c_dev_attr_groups,
351 .uevent = i2c_device_uevent,
352 .release = i2c_client_dev_release,
357 * i2c_verify_client - return parameter as i2c_client, or NULL
358 * @dev: device, probably from some driver model iterator
360 * When traversing the driver model tree, perhaps using driver model
361 * iterators like @device_for_each_child(), you can't assume very much
362 * about the nodes you find. Use this function to avoid oopses caused
363 * by wrongly treating some non-I2C device as an i2c_client.
365 struct i2c_client *i2c_verify_client(struct device *dev)
367 return (dev->type == &i2c_client_type)
371 EXPORT_SYMBOL(i2c_verify_client);
374 /* This is a permissive address validity check, I2C address map constraints
375 * are purposedly not enforced, except for the general call address. */
376 static int i2c_check_client_addr_validity(const struct i2c_client *client)
378 if (client->flags & I2C_CLIENT_TEN) {
379 /* 10-bit address, all values are valid */
380 if (client->addr > 0x3ff)
383 /* 7-bit address, reject the general call address */
384 if (client->addr == 0x00 || client->addr > 0x7f)
390 /* And this is a strict address validity check, used when probing. If a
391 * device uses a reserved address, then it shouldn't be probed. 7-bit
392 * addressing is assumed, 10-bit address devices are rare and should be
393 * explicitly enumerated. */
394 static int i2c_check_addr_validity(unsigned short addr)
397 * Reserved addresses per I2C specification:
398 * 0x00 General call address / START byte
400 * 0x02 Reserved for different bus format
401 * 0x03 Reserved for future purposes
402 * 0x04-0x07 Hs-mode master code
403 * 0x78-0x7b 10-bit slave addressing
404 * 0x7c-0x7f Reserved for future purposes
406 if (addr < 0x08 || addr > 0x77)
412 * i2c_new_device - instantiate an i2c device
413 * @adap: the adapter managing the device
414 * @info: describes one I2C device; bus_num is ignored
417 * Create an i2c device. Binding is handled through driver model
418 * probe()/remove() methods. A driver may be bound to this device when we
419 * return from this function, or any later moment (e.g. maybe hotplugging will
420 * load the driver module). This call is not appropriate for use by mainboard
421 * initialization logic, which usually runs during an arch_initcall() long
422 * before any i2c_adapter could exist.
424 * This returns the new i2c client, which may be saved for later use with
425 * i2c_unregister_device(); or NULL to indicate an error.
428 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
430 struct i2c_client *client;
433 client = kzalloc(sizeof *client, GFP_KERNEL);
437 client->adapter = adap;
439 client->dev.platform_data = info->platform_data;
442 client->dev.archdata = *info->archdata;
444 client->flags = info->flags;
445 client->addr = info->addr;
446 client->irq = info->irq;
448 strlcpy(client->name, info->type, sizeof(client->name));
450 /* Check for address validity */
451 status = i2c_check_client_addr_validity(client);
453 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
454 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
458 /* Check for address business */
459 status = i2c_check_addr(adap, client->addr);
463 client->dev.parent = &client->adapter->dev;
464 client->dev.bus = &i2c_bus_type;
465 client->dev.type = &i2c_client_type;
467 client->dev.of_node = info->of_node;
470 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
472 status = device_register(&client->dev);
476 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
477 client->name, dev_name(&client->dev));
482 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
483 "(%d)\n", client->name, client->addr, status);
488 EXPORT_SYMBOL_GPL(i2c_new_device);
492 * i2c_unregister_device - reverse effect of i2c_new_device()
493 * @client: value returned from i2c_new_device()
496 void i2c_unregister_device(struct i2c_client *client)
498 device_unregister(&client->dev);
500 EXPORT_SYMBOL_GPL(i2c_unregister_device);
503 static const struct i2c_device_id dummy_id[] = {
508 static int dummy_probe(struct i2c_client *client,
509 const struct i2c_device_id *id)
514 static int dummy_remove(struct i2c_client *client)
519 static struct i2c_driver dummy_driver = {
520 .driver.name = "dummy",
521 .probe = dummy_probe,
522 .remove = dummy_remove,
523 .id_table = dummy_id,
527 * i2c_new_dummy - return a new i2c device bound to a dummy driver
528 * @adapter: the adapter managing the device
529 * @address: seven bit address to be used
532 * This returns an I2C client bound to the "dummy" driver, intended for use
533 * with devices that consume multiple addresses. Examples of such chips
534 * include various EEPROMS (like 24c04 and 24c08 models).
536 * These dummy devices have two main uses. First, most I2C and SMBus calls
537 * except i2c_transfer() need a client handle; the dummy will be that handle.
538 * And second, this prevents the specified address from being bound to a
541 * This returns the new i2c client, which should be saved for later use with
542 * i2c_unregister_device(); or NULL to indicate an error.
544 struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
546 struct i2c_board_info info = {
547 I2C_BOARD_INFO("dummy", address),
550 return i2c_new_device(adapter, &info);
552 EXPORT_SYMBOL_GPL(i2c_new_dummy);
554 /* ------------------------------------------------------------------------- */
556 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
558 static void i2c_adapter_dev_release(struct device *dev)
560 struct i2c_adapter *adap = to_i2c_adapter(dev);
561 complete(&adap->dev_released);
565 * Let users instantiate I2C devices through sysfs. This can be used when
566 * platform initialization code doesn't contain the proper data for
567 * whatever reason. Also useful for drivers that do device detection and
568 * detection fails, either because the device uses an unexpected address,
569 * or this is a compatible device with different ID register values.
571 * Parameter checking may look overzealous, but we really don't want
572 * the user to provide incorrect parameters.
575 i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
576 const char *buf, size_t count)
578 struct i2c_adapter *adap = to_i2c_adapter(dev);
579 struct i2c_board_info info;
580 struct i2c_client *client;
584 dev_warn(dev, "The new_device interface is still experimental "
585 "and may change in a near future\n");
586 memset(&info, 0, sizeof(struct i2c_board_info));
588 blank = strchr(buf, ' ');
590 dev_err(dev, "%s: Missing parameters\n", "new_device");
593 if (blank - buf > I2C_NAME_SIZE - 1) {
594 dev_err(dev, "%s: Invalid device name\n", "new_device");
597 memcpy(info.type, buf, blank - buf);
599 /* Parse remaining parameters, reject extra parameters */
600 res = sscanf(++blank, "%hi%c", &info.addr, &end);
602 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
605 if (res > 1 && end != '\n') {
606 dev_err(dev, "%s: Extra parameters\n", "new_device");
610 client = i2c_new_device(adap, &info);
614 /* Keep track of the added device */
615 i2c_lock_adapter(adap);
616 list_add_tail(&client->detected, &adap->userspace_clients);
617 i2c_unlock_adapter(adap);
618 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
619 info.type, info.addr);
625 * And of course let the users delete the devices they instantiated, if
626 * they got it wrong. This interface can only be used to delete devices
627 * instantiated by i2c_sysfs_new_device above. This guarantees that we
628 * don't delete devices to which some kernel code still has references.
630 * Parameter checking may look overzealous, but we really don't want
631 * the user to delete the wrong device.
634 i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
635 const char *buf, size_t count)
637 struct i2c_adapter *adap = to_i2c_adapter(dev);
638 struct i2c_client *client, *next;
643 /* Parse parameters, reject extra parameters */
644 res = sscanf(buf, "%hi%c", &addr, &end);
646 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
649 if (res > 1 && end != '\n') {
650 dev_err(dev, "%s: Extra parameters\n", "delete_device");
654 /* Make sure the device was added through sysfs */
656 i2c_lock_adapter(adap);
657 list_for_each_entry_safe(client, next, &adap->userspace_clients,
659 if (client->addr == addr) {
660 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
661 "delete_device", client->name, client->addr);
663 list_del(&client->detected);
664 i2c_unregister_device(client);
669 i2c_unlock_adapter(adap);
672 dev_err(dev, "%s: Can't find device in list\n",
677 static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
678 static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
680 static struct attribute *i2c_adapter_attrs[] = {
682 &dev_attr_new_device.attr,
683 &dev_attr_delete_device.attr,
687 static struct attribute_group i2c_adapter_attr_group = {
688 .attrs = i2c_adapter_attrs,
691 static const struct attribute_group *i2c_adapter_attr_groups[] = {
692 &i2c_adapter_attr_group,
696 static struct device_type i2c_adapter_type = {
697 .groups = i2c_adapter_attr_groups,
698 .release = i2c_adapter_dev_release,
701 #ifdef CONFIG_I2C_COMPAT
702 static struct class_compat *i2c_adapter_compat_class;
705 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
707 struct i2c_devinfo *devinfo;
709 down_read(&__i2c_board_lock);
710 list_for_each_entry(devinfo, &__i2c_board_list, list) {
711 if (devinfo->busnum == adapter->nr
712 && !i2c_new_device(adapter,
713 &devinfo->board_info))
714 dev_err(&adapter->dev,
715 "Can't create device at 0x%02x\n",
716 devinfo->board_info.addr);
718 up_read(&__i2c_board_lock);
721 static int i2c_do_add_adapter(struct i2c_driver *driver,
722 struct i2c_adapter *adap)
724 /* Detect supported devices on that bus, and instantiate them */
725 i2c_detect(adap, driver);
727 /* Let legacy drivers scan this bus for matching devices */
728 if (driver->attach_adapter) {
729 /* We ignore the return code; if it fails, too bad */
730 driver->attach_adapter(adap);
735 static int __process_new_adapter(struct device_driver *d, void *data)
737 return i2c_do_add_adapter(to_i2c_driver(d), data);
740 static int i2c_register_adapter(struct i2c_adapter *adap)
744 /* Can't register until after driver model init */
745 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
750 rt_mutex_init(&adap->bus_lock);
751 INIT_LIST_HEAD(&adap->userspace_clients);
753 /* Set default timeout to 1 second if not already set */
754 if (adap->timeout == 0)
757 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
758 adap->dev.bus = &i2c_bus_type;
759 adap->dev.type = &i2c_adapter_type;
760 res = device_register(&adap->dev);
764 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
766 #ifdef CONFIG_I2C_COMPAT
767 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
771 "Failed to create compatibility class link\n");
774 /* create pre-declared device nodes */
775 if (adap->nr < __i2c_first_dynamic_bus_num)
776 i2c_scan_static_board_info(adap);
779 mutex_lock(&core_lock);
780 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
781 __process_new_adapter);
782 mutex_unlock(&core_lock);
787 mutex_lock(&core_lock);
788 idr_remove(&i2c_adapter_idr, adap->nr);
789 mutex_unlock(&core_lock);
794 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
795 * @adapter: the adapter to add
798 * This routine is used to declare an I2C adapter when its bus number
799 * doesn't matter. Examples: for I2C adapters dynamically added by
800 * USB links or PCI plugin cards.
802 * When this returns zero, a new bus number was allocated and stored
803 * in adap->nr, and the specified adapter became available for clients.
804 * Otherwise, a negative errno value is returned.
806 int i2c_add_adapter(struct i2c_adapter *adapter)
811 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
814 mutex_lock(&core_lock);
815 /* "above" here means "above or equal to", sigh */
816 res = idr_get_new_above(&i2c_adapter_idr, adapter,
817 __i2c_first_dynamic_bus_num, &id);
818 mutex_unlock(&core_lock);
827 return i2c_register_adapter(adapter);
829 EXPORT_SYMBOL(i2c_add_adapter);
832 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
833 * @adap: the adapter to register (with adap->nr initialized)
836 * This routine is used to declare an I2C adapter when its bus number
837 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
838 * or otherwise built in to the system's mainboard, and where i2c_board_info
839 * is used to properly configure I2C devices.
841 * If no devices have pre-been declared for this bus, then be sure to
842 * register the adapter before any dynamically allocated ones. Otherwise
843 * the required bus ID may not be available.
845 * When this returns zero, the specified adapter became available for
846 * clients using the bus number provided in adap->nr. Also, the table
847 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
848 * and the appropriate driver model device nodes are created. Otherwise, a
849 * negative errno value is returned.
851 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
856 if (adap->nr & ~MAX_ID_MASK)
860 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
863 mutex_lock(&core_lock);
864 /* "above" here means "above or equal to", sigh;
865 * we need the "equal to" result to force the result
867 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
868 if (status == 0 && id != adap->nr) {
870 idr_remove(&i2c_adapter_idr, id);
872 mutex_unlock(&core_lock);
873 if (status == -EAGAIN)
877 status = i2c_register_adapter(adap);
880 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
882 static int i2c_do_del_adapter(struct i2c_driver *driver,
883 struct i2c_adapter *adapter)
885 struct i2c_client *client, *_n;
888 /* Remove the devices we created ourselves as the result of hardware
889 * probing (using a driver's detect method) */
890 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
891 if (client->adapter == adapter) {
892 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
893 client->name, client->addr);
894 list_del(&client->detected);
895 i2c_unregister_device(client);
899 if (!driver->detach_adapter)
901 res = driver->detach_adapter(adapter);
903 dev_err(&adapter->dev, "detach_adapter failed (%d) "
904 "for driver [%s]\n", res, driver->driver.name);
908 static int __unregister_client(struct device *dev, void *dummy)
910 struct i2c_client *client = i2c_verify_client(dev);
912 i2c_unregister_device(client);
916 static int __process_removed_adapter(struct device_driver *d, void *data)
918 return i2c_do_del_adapter(to_i2c_driver(d), data);
922 * i2c_del_adapter - unregister I2C adapter
923 * @adap: the adapter being unregistered
926 * This unregisters an I2C adapter which was previously registered
927 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
929 int i2c_del_adapter(struct i2c_adapter *adap)
932 struct i2c_adapter *found;
933 struct i2c_client *client, *next;
935 /* First make sure that this adapter was ever added */
936 mutex_lock(&core_lock);
937 found = idr_find(&i2c_adapter_idr, adap->nr);
938 mutex_unlock(&core_lock);
940 pr_debug("i2c-core: attempting to delete unregistered "
941 "adapter [%s]\n", adap->name);
945 /* Tell drivers about this removal */
946 mutex_lock(&core_lock);
947 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
948 __process_removed_adapter);
949 mutex_unlock(&core_lock);
953 /* Remove devices instantiated from sysfs */
954 i2c_lock_adapter(adap);
955 list_for_each_entry_safe(client, next, &adap->userspace_clients,
957 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
959 list_del(&client->detected);
960 i2c_unregister_device(client);
962 i2c_unlock_adapter(adap);
964 /* Detach any active clients. This can't fail, thus we do not
965 checking the returned value. */
966 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
968 #ifdef CONFIG_I2C_COMPAT
969 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
973 /* device name is gone after device_unregister */
974 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
976 /* clean up the sysfs representation */
977 init_completion(&adap->dev_released);
978 device_unregister(&adap->dev);
980 /* wait for sysfs to drop all references */
981 wait_for_completion(&adap->dev_released);
984 mutex_lock(&core_lock);
985 idr_remove(&i2c_adapter_idr, adap->nr);
986 mutex_unlock(&core_lock);
988 /* Clear the device structure in case this adapter is ever going to be
990 memset(&adap->dev, 0, sizeof(adap->dev));
994 EXPORT_SYMBOL(i2c_del_adapter);
997 /* ------------------------------------------------------------------------- */
999 static int __process_new_driver(struct device *dev, void *data)
1001 if (dev->type != &i2c_adapter_type)
1003 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
1007 * An i2c_driver is used with one or more i2c_client (device) nodes to access
1008 * i2c slave chips, on a bus instance associated with some i2c_adapter.
1011 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
1015 /* Can't register until after driver model init */
1016 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1019 /* add the driver to the list of i2c drivers in the driver core */
1020 driver->driver.owner = owner;
1021 driver->driver.bus = &i2c_bus_type;
1023 /* When registration returns, the driver core
1024 * will have called probe() for all matching-but-unbound devices.
1026 res = driver_register(&driver->driver);
1030 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
1032 INIT_LIST_HEAD(&driver->clients);
1033 /* Walk the adapters that are already present */
1034 mutex_lock(&core_lock);
1035 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver);
1036 mutex_unlock(&core_lock);
1040 EXPORT_SYMBOL(i2c_register_driver);
1042 static int __process_removed_driver(struct device *dev, void *data)
1044 if (dev->type != &i2c_adapter_type)
1046 return i2c_do_del_adapter(data, to_i2c_adapter(dev));
1050 * i2c_del_driver - unregister I2C driver
1051 * @driver: the driver being unregistered
1052 * Context: can sleep
1054 void i2c_del_driver(struct i2c_driver *driver)
1056 mutex_lock(&core_lock);
1057 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_removed_driver);
1058 mutex_unlock(&core_lock);
1060 driver_unregister(&driver->driver);
1061 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
1063 EXPORT_SYMBOL(i2c_del_driver);
1065 /* ------------------------------------------------------------------------- */
1067 static int __i2c_check_addr(struct device *dev, void *addrp)
1069 struct i2c_client *client = i2c_verify_client(dev);
1070 int addr = *(int *)addrp;
1072 if (client && client->addr == addr)
1077 static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
1079 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
1083 * i2c_use_client - increments the reference count of the i2c client structure
1084 * @client: the client being referenced
1086 * Each live reference to a client should be refcounted. The driver model does
1087 * that automatically as part of driver binding, so that most drivers don't
1088 * need to do this explicitly: they hold a reference until they're unbound
1091 * A pointer to the client with the incremented reference counter is returned.
1093 struct i2c_client *i2c_use_client(struct i2c_client *client)
1095 if (client && get_device(&client->dev))
1099 EXPORT_SYMBOL(i2c_use_client);
1102 * i2c_release_client - release a use of the i2c client structure
1103 * @client: the client being no longer referenced
1105 * Must be called when a user of a client is finished with it.
1107 void i2c_release_client(struct i2c_client *client)
1110 put_device(&client->dev);
1112 EXPORT_SYMBOL(i2c_release_client);
1114 struct i2c_cmd_arg {
1119 static int i2c_cmd(struct device *dev, void *_arg)
1121 struct i2c_client *client = i2c_verify_client(dev);
1122 struct i2c_cmd_arg *arg = _arg;
1124 if (client && client->driver && client->driver->command)
1125 client->driver->command(client, arg->cmd, arg->arg);
1129 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1131 struct i2c_cmd_arg cmd_arg;
1135 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
1137 EXPORT_SYMBOL(i2c_clients_command);
1139 static int __init i2c_init(void)
1143 retval = bus_register(&i2c_bus_type);
1146 #ifdef CONFIG_I2C_COMPAT
1147 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1148 if (!i2c_adapter_compat_class) {
1153 retval = i2c_add_driver(&dummy_driver);
1159 #ifdef CONFIG_I2C_COMPAT
1160 class_compat_unregister(i2c_adapter_compat_class);
1163 bus_unregister(&i2c_bus_type);
1167 static void __exit i2c_exit(void)
1169 i2c_del_driver(&dummy_driver);
1170 #ifdef CONFIG_I2C_COMPAT
1171 class_compat_unregister(i2c_adapter_compat_class);
1173 bus_unregister(&i2c_bus_type);
1176 /* We must initialize early, because some subsystems register i2c drivers
1177 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1179 postcore_initcall(i2c_init);
1180 module_exit(i2c_exit);
1182 /* ----------------------------------------------------
1183 * the functional interface to the i2c busses.
1184 * ----------------------------------------------------
1188 * i2c_transfer - execute a single or combined I2C message
1189 * @adap: Handle to I2C bus
1190 * @msgs: One or more messages to execute before STOP is issued to
1191 * terminate the operation; each message begins with a START.
1192 * @num: Number of messages to be executed.
1194 * Returns negative errno, else the number of messages executed.
1196 * Note that there is no requirement that each message be sent to
1197 * the same slave address, although that is the most common model.
1199 int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1201 unsigned long orig_jiffies;
1204 /* REVISIT the fault reporting model here is weak:
1206 * - When we get an error after receiving N bytes from a slave,
1207 * there is no way to report "N".
1209 * - When we get a NAK after transmitting N bytes to a slave,
1210 * there is no way to report "N" ... or to let the master
1211 * continue executing the rest of this combined message, if
1212 * that's the appropriate response.
1214 * - When for example "num" is two and we successfully complete
1215 * the first message but get an error part way through the
1216 * second, it's unclear whether that should be reported as
1217 * one (discarding status on the second message) or errno
1218 * (discarding status on the first one).
1221 if (adap->algo->master_xfer) {
1223 for (ret = 0; ret < num; ret++) {
1224 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
1225 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1226 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1227 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
1231 if (in_atomic() || irqs_disabled()) {
1232 ret = rt_mutex_trylock(&adap->bus_lock);
1234 /* I2C activity is ongoing. */
1237 rt_mutex_lock(&adap->bus_lock);
1240 /* Retry automatically on arbitration loss */
1241 orig_jiffies = jiffies;
1242 for (ret = 0, try = 0; try <= adap->retries; try++) {
1243 ret = adap->algo->master_xfer(adap, msgs, num);
1246 if (time_after(jiffies, orig_jiffies + adap->timeout))
1249 rt_mutex_unlock(&adap->bus_lock);
1253 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
1257 EXPORT_SYMBOL(i2c_transfer);
1260 * i2c_master_send - issue a single I2C message in master transmit mode
1261 * @client: Handle to slave device
1262 * @buf: Data that will be written to the slave
1263 * @count: How many bytes to write, must be less than 64k since msg.len is u16
1265 * Returns negative errno, or else the number of bytes written.
1267 int i2c_master_send(struct i2c_client *client, const char *buf, int count)
1270 struct i2c_adapter *adap = client->adapter;
1273 msg.addr = client->addr;
1274 msg.flags = client->flags & I2C_M_TEN;
1276 msg.buf = (char *)buf;
1278 ret = i2c_transfer(adap, &msg, 1);
1280 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1281 transmitted, else error code. */
1282 return (ret == 1) ? count : ret;
1284 EXPORT_SYMBOL(i2c_master_send);
1287 * i2c_master_recv - issue a single I2C message in master receive mode
1288 * @client: Handle to slave device
1289 * @buf: Where to store data read from slave
1290 * @count: How many bytes to read, must be less than 64k since msg.len is u16
1292 * Returns negative errno, or else the number of bytes read.
1294 int i2c_master_recv(struct i2c_client *client, char *buf, int count)
1296 struct i2c_adapter *adap = client->adapter;
1300 msg.addr = client->addr;
1301 msg.flags = client->flags & I2C_M_TEN;
1302 msg.flags |= I2C_M_RD;
1306 ret = i2c_transfer(adap, &msg, 1);
1308 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1309 transmitted, else error code. */
1310 return (ret == 1) ? count : ret;
1312 EXPORT_SYMBOL(i2c_master_recv);
1314 /* ----------------------------------------------------
1315 * the i2c address scanning function
1316 * Will not work for 10-bit addresses!
1317 * ----------------------------------------------------
1321 * Legacy default probe function, mostly relevant for SMBus. The default
1322 * probe method is a quick write, but it is known to corrupt the 24RF08
1323 * EEPROMs due to a state machine bug, and could also irreversibly
1324 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1325 * we use a short byte read instead. Also, some bus drivers don't implement
1326 * quick write, so we fallback to a byte read in that case too.
1327 * On x86, there is another special case for FSC hardware monitoring chips,
1328 * which want regular byte reads (address 0x73.) Fortunately, these are the
1329 * only known chips using this I2C address on PC hardware.
1330 * Returns 1 if probe succeeded, 0 if not.
1332 static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1335 union i2c_smbus_data dummy;
1338 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1339 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1340 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1341 I2C_SMBUS_BYTE_DATA, &dummy);
1344 if ((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50
1345 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
1346 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1347 I2C_SMBUS_BYTE, &dummy);
1349 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1350 I2C_SMBUS_QUICK, NULL);
1355 static int i2c_detect_address(struct i2c_client *temp_client,
1356 struct i2c_driver *driver)
1358 struct i2c_board_info info;
1359 struct i2c_adapter *adapter = temp_client->adapter;
1360 int addr = temp_client->addr;
1363 /* Make sure the address is valid */
1364 err = i2c_check_addr_validity(addr);
1366 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1371 /* Skip if already in use */
1372 if (i2c_check_addr(adapter, addr))
1375 /* Make sure there is something at this address */
1376 if (!i2c_default_probe(adapter, addr))
1379 /* Finally call the custom detection function */
1380 memset(&info, 0, sizeof(struct i2c_board_info));
1382 err = driver->detect(temp_client, &info);
1384 /* -ENODEV is returned if the detection fails. We catch it
1385 here as this isn't an error. */
1386 return err == -ENODEV ? 0 : err;
1389 /* Consistency check */
1390 if (info.type[0] == '\0') {
1391 dev_err(&adapter->dev, "%s detection function provided "
1392 "no name for 0x%x\n", driver->driver.name,
1395 struct i2c_client *client;
1397 /* Detection succeeded, instantiate the device */
1398 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1399 info.type, info.addr);
1400 client = i2c_new_device(adapter, &info);
1402 list_add_tail(&client->detected, &driver->clients);
1404 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1405 info.type, info.addr);
1410 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1412 const unsigned short *address_list;
1413 struct i2c_client *temp_client;
1415 int adap_id = i2c_adapter_id(adapter);
1417 address_list = driver->address_list;
1418 if (!driver->detect || !address_list)
1421 /* Set up a temporary client to help detect callback */
1422 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1425 temp_client->adapter = adapter;
1427 /* Stop here if the classes do not match */
1428 if (!(adapter->class & driver->class))
1431 /* Stop here if we can't use SMBUS_QUICK */
1432 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1433 if (address_list[0] == I2C_CLIENT_END)
1436 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1437 "can't probe for chips\n");
1442 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
1443 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1444 "addr 0x%02x\n", adap_id, address_list[i]);
1445 temp_client->addr = address_list[i];
1446 err = i2c_detect_address(temp_client, driver);
1457 i2c_new_probed_device(struct i2c_adapter *adap,
1458 struct i2c_board_info *info,
1459 unsigned short const *addr_list)
1463 /* Stop here if the bus doesn't support probing */
1464 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1465 dev_err(&adap->dev, "Probing not supported\n");
1469 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1470 /* Check address validity */
1471 if (i2c_check_addr_validity(addr_list[i]) < 0) {
1472 dev_warn(&adap->dev, "Invalid 7-bit address "
1473 "0x%02x\n", addr_list[i]);
1477 /* Check address availability */
1478 if (i2c_check_addr(adap, addr_list[i])) {
1479 dev_dbg(&adap->dev, "Address 0x%02x already in "
1480 "use, not probing\n", addr_list[i]);
1484 /* Test address responsiveness */
1485 if (i2c_default_probe(adap, addr_list[i]))
1489 if (addr_list[i] == I2C_CLIENT_END) {
1490 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1494 info->addr = addr_list[i];
1495 return i2c_new_device(adap, info);
1497 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1499 struct i2c_adapter *i2c_get_adapter(int id)
1501 struct i2c_adapter *adapter;
1503 mutex_lock(&core_lock);
1504 adapter = idr_find(&i2c_adapter_idr, id);
1505 if (adapter && !try_module_get(adapter->owner))
1508 mutex_unlock(&core_lock);
1511 EXPORT_SYMBOL(i2c_get_adapter);
1513 void i2c_put_adapter(struct i2c_adapter *adap)
1515 module_put(adap->owner);
1517 EXPORT_SYMBOL(i2c_put_adapter);
1519 /* The SMBus parts */
1521 #define POLY (0x1070U << 3)
1522 static u8 crc8(u16 data)
1526 for (i = 0; i < 8; i++) {
1531 return (u8)(data >> 8);
1534 /* Incremental CRC8 over count bytes in the array pointed to by p */
1535 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1539 for (i = 0; i < count; i++)
1540 crc = crc8((crc ^ p[i]) << 8);
1544 /* Assume a 7-bit address, which is reasonable for SMBus */
1545 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1547 /* The address will be sent first */
1548 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1549 pec = i2c_smbus_pec(pec, &addr, 1);
1551 /* The data buffer follows */
1552 return i2c_smbus_pec(pec, msg->buf, msg->len);
1555 /* Used for write only transactions */
1556 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1558 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1562 /* Return <0 on CRC error
1563 If there was a write before this read (most cases) we need to take the
1564 partial CRC from the write part into account.
1565 Note that this function does modify the message (we need to decrease the
1566 message length to hide the CRC byte from the caller). */
1567 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1569 u8 rpec = msg->buf[--msg->len];
1570 cpec = i2c_smbus_msg_pec(cpec, msg);
1573 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1581 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1582 * @client: Handle to slave device
1584 * This executes the SMBus "receive byte" protocol, returning negative errno
1585 * else the byte received from the device.
1587 s32 i2c_smbus_read_byte(struct i2c_client *client)
1589 union i2c_smbus_data data;
1592 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1594 I2C_SMBUS_BYTE, &data);
1595 return (status < 0) ? status : data.byte;
1597 EXPORT_SYMBOL(i2c_smbus_read_byte);
1600 * i2c_smbus_write_byte - SMBus "send byte" protocol
1601 * @client: Handle to slave device
1602 * @value: Byte to be sent
1604 * This executes the SMBus "send byte" protocol, returning negative errno
1605 * else zero on success.
1607 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1609 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1610 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1612 EXPORT_SYMBOL(i2c_smbus_write_byte);
1615 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1616 * @client: Handle to slave device
1617 * @command: Byte interpreted by slave
1619 * This executes the SMBus "read byte" protocol, returning negative errno
1620 * else a data byte received from the device.
1622 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1624 union i2c_smbus_data data;
1627 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1628 I2C_SMBUS_READ, command,
1629 I2C_SMBUS_BYTE_DATA, &data);
1630 return (status < 0) ? status : data.byte;
1632 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1635 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1636 * @client: Handle to slave device
1637 * @command: Byte interpreted by slave
1638 * @value: Byte being written
1640 * This executes the SMBus "write byte" protocol, returning negative errno
1641 * else zero on success.
1643 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1645 union i2c_smbus_data data;
1647 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1648 I2C_SMBUS_WRITE, command,
1649 I2C_SMBUS_BYTE_DATA, &data);
1651 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1654 * i2c_smbus_read_word_data - SMBus "read word" protocol
1655 * @client: Handle to slave device
1656 * @command: Byte interpreted by slave
1658 * This executes the SMBus "read word" protocol, returning negative errno
1659 * else a 16-bit unsigned "word" received from the device.
1661 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1663 union i2c_smbus_data data;
1666 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1667 I2C_SMBUS_READ, command,
1668 I2C_SMBUS_WORD_DATA, &data);
1669 return (status < 0) ? status : data.word;
1671 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1674 * i2c_smbus_write_word_data - SMBus "write word" protocol
1675 * @client: Handle to slave device
1676 * @command: Byte interpreted by slave
1677 * @value: 16-bit "word" being written
1679 * This executes the SMBus "write word" protocol, returning negative errno
1680 * else zero on success.
1682 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1684 union i2c_smbus_data data;
1686 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1687 I2C_SMBUS_WRITE, command,
1688 I2C_SMBUS_WORD_DATA, &data);
1690 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1693 * i2c_smbus_process_call - SMBus "process call" protocol
1694 * @client: Handle to slave device
1695 * @command: Byte interpreted by slave
1696 * @value: 16-bit "word" being written
1698 * This executes the SMBus "process call" protocol, returning negative errno
1699 * else a 16-bit unsigned "word" received from the device.
1701 s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1703 union i2c_smbus_data data;
1707 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1708 I2C_SMBUS_WRITE, command,
1709 I2C_SMBUS_PROC_CALL, &data);
1710 return (status < 0) ? status : data.word;
1712 EXPORT_SYMBOL(i2c_smbus_process_call);
1715 * i2c_smbus_read_block_data - SMBus "block read" protocol
1716 * @client: Handle to slave device
1717 * @command: Byte interpreted by slave
1718 * @values: Byte array into which data will be read; big enough to hold
1719 * the data returned by the slave. SMBus allows at most 32 bytes.
1721 * This executes the SMBus "block read" protocol, returning negative errno
1722 * else the number of data bytes in the slave's response.
1724 * Note that using this function requires that the client's adapter support
1725 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1726 * support this; its emulation through I2C messaging relies on a specific
1727 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1729 s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1732 union i2c_smbus_data data;
1735 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1736 I2C_SMBUS_READ, command,
1737 I2C_SMBUS_BLOCK_DATA, &data);
1741 memcpy(values, &data.block[1], data.block[0]);
1742 return data.block[0];
1744 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1747 * i2c_smbus_write_block_data - SMBus "block write" protocol
1748 * @client: Handle to slave device
1749 * @command: Byte interpreted by slave
1750 * @length: Size of data block; SMBus allows at most 32 bytes
1751 * @values: Byte array which will be written.
1753 * This executes the SMBus "block write" protocol, returning negative errno
1754 * else zero on success.
1756 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1757 u8 length, const u8 *values)
1759 union i2c_smbus_data data;
1761 if (length > I2C_SMBUS_BLOCK_MAX)
1762 length = I2C_SMBUS_BLOCK_MAX;
1763 data.block[0] = length;
1764 memcpy(&data.block[1], values, length);
1765 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1766 I2C_SMBUS_WRITE, command,
1767 I2C_SMBUS_BLOCK_DATA, &data);
1769 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1771 /* Returns the number of read bytes */
1772 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1773 u8 length, u8 *values)
1775 union i2c_smbus_data data;
1778 if (length > I2C_SMBUS_BLOCK_MAX)
1779 length = I2C_SMBUS_BLOCK_MAX;
1780 data.block[0] = length;
1781 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1782 I2C_SMBUS_READ, command,
1783 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1787 memcpy(values, &data.block[1], data.block[0]);
1788 return data.block[0];
1790 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1792 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1793 u8 length, const u8 *values)
1795 union i2c_smbus_data data;
1797 if (length > I2C_SMBUS_BLOCK_MAX)
1798 length = I2C_SMBUS_BLOCK_MAX;
1799 data.block[0] = length;
1800 memcpy(data.block + 1, values, length);
1801 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1802 I2C_SMBUS_WRITE, command,
1803 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1805 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1807 /* Simulate a SMBus command using the i2c protocol
1808 No checking of parameters is done! */
1809 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
1810 unsigned short flags,
1811 char read_write, u8 command, int size,
1812 union i2c_smbus_data *data)
1814 /* So we need to generate a series of msgs. In the case of writing, we
1815 need to use only one message; when reading, we need two. We initialize
1816 most things with sane defaults, to keep the code below somewhat
1818 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1819 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1820 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
1821 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1822 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1828 msgbuf0[0] = command;
1830 case I2C_SMBUS_QUICK:
1832 /* Special case: The read/write field is used as data */
1833 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1837 case I2C_SMBUS_BYTE:
1838 if (read_write == I2C_SMBUS_READ) {
1839 /* Special case: only a read! */
1840 msg[0].flags = I2C_M_RD | flags;
1844 case I2C_SMBUS_BYTE_DATA:
1845 if (read_write == I2C_SMBUS_READ)
1849 msgbuf0[1] = data->byte;
1852 case I2C_SMBUS_WORD_DATA:
1853 if (read_write == I2C_SMBUS_READ)
1857 msgbuf0[1] = data->word & 0xff;
1858 msgbuf0[2] = data->word >> 8;
1861 case I2C_SMBUS_PROC_CALL:
1862 num = 2; /* Special case */
1863 read_write = I2C_SMBUS_READ;
1866 msgbuf0[1] = data->word & 0xff;
1867 msgbuf0[2] = data->word >> 8;
1869 case I2C_SMBUS_BLOCK_DATA:
1870 if (read_write == I2C_SMBUS_READ) {
1871 msg[1].flags |= I2C_M_RECV_LEN;
1872 msg[1].len = 1; /* block length will be added by
1873 the underlying bus driver */
1875 msg[0].len = data->block[0] + 2;
1876 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1877 dev_err(&adapter->dev,
1878 "Invalid block write size %d\n",
1882 for (i = 1; i < msg[0].len; i++)
1883 msgbuf0[i] = data->block[i-1];
1886 case I2C_SMBUS_BLOCK_PROC_CALL:
1887 num = 2; /* Another special case */
1888 read_write = I2C_SMBUS_READ;
1889 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1890 dev_err(&adapter->dev,
1891 "Invalid block write size %d\n",
1895 msg[0].len = data->block[0] + 2;
1896 for (i = 1; i < msg[0].len; i++)
1897 msgbuf0[i] = data->block[i-1];
1898 msg[1].flags |= I2C_M_RECV_LEN;
1899 msg[1].len = 1; /* block length will be added by
1900 the underlying bus driver */
1902 case I2C_SMBUS_I2C_BLOCK_DATA:
1903 if (read_write == I2C_SMBUS_READ) {
1904 msg[1].len = data->block[0];
1906 msg[0].len = data->block[0] + 1;
1907 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1908 dev_err(&adapter->dev,
1909 "Invalid block write size %d\n",
1913 for (i = 1; i <= data->block[0]; i++)
1914 msgbuf0[i] = data->block[i];
1918 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1922 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1923 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1925 /* Compute PEC if first message is a write */
1926 if (!(msg[0].flags & I2C_M_RD)) {
1927 if (num == 1) /* Write only */
1928 i2c_smbus_add_pec(&msg[0]);
1929 else /* Write followed by read */
1930 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1932 /* Ask for PEC if last message is a read */
1933 if (msg[num-1].flags & I2C_M_RD)
1937 status = i2c_transfer(adapter, msg, num);
1941 /* Check PEC if last message is a read */
1942 if (i && (msg[num-1].flags & I2C_M_RD)) {
1943 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1948 if (read_write == I2C_SMBUS_READ)
1950 case I2C_SMBUS_BYTE:
1951 data->byte = msgbuf0[0];
1953 case I2C_SMBUS_BYTE_DATA:
1954 data->byte = msgbuf1[0];
1956 case I2C_SMBUS_WORD_DATA:
1957 case I2C_SMBUS_PROC_CALL:
1958 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1960 case I2C_SMBUS_I2C_BLOCK_DATA:
1961 for (i = 0; i < data->block[0]; i++)
1962 data->block[i+1] = msgbuf1[i];
1964 case I2C_SMBUS_BLOCK_DATA:
1965 case I2C_SMBUS_BLOCK_PROC_CALL:
1966 for (i = 0; i < msgbuf1[0] + 1; i++)
1967 data->block[i] = msgbuf1[i];
1974 * i2c_smbus_xfer - execute SMBus protocol operations
1975 * @adapter: Handle to I2C bus
1976 * @addr: Address of SMBus slave on that bus
1977 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1978 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1979 * @command: Byte interpreted by slave, for protocols which use such bytes
1980 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1981 * @data: Data to be read or written
1983 * This executes an SMBus protocol operation, and returns a negative
1984 * errno code else zero on success.
1986 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
1987 char read_write, u8 command, int protocol,
1988 union i2c_smbus_data *data)
1990 unsigned long orig_jiffies;
1994 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1996 if (adapter->algo->smbus_xfer) {
1997 rt_mutex_lock(&adapter->bus_lock);
1999 /* Retry automatically on arbitration loss */
2000 orig_jiffies = jiffies;
2001 for (res = 0, try = 0; try <= adapter->retries; try++) {
2002 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2003 read_write, command,
2007 if (time_after(jiffies,
2008 orig_jiffies + adapter->timeout))
2011 rt_mutex_unlock(&adapter->bus_lock);
2013 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2014 command, protocol, data);
2018 EXPORT_SYMBOL(i2c_smbus_xfer);
2020 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2021 MODULE_DESCRIPTION("I2C-Bus main module");
2022 MODULE_LICENSE("GPL");