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)
391 * i2c_new_device - instantiate an i2c device
392 * @adap: the adapter managing the device
393 * @info: describes one I2C device; bus_num is ignored
396 * Create an i2c device. Binding is handled through driver model
397 * probe()/remove() methods. A driver may be bound to this device when we
398 * return from this function, or any later moment (e.g. maybe hotplugging will
399 * load the driver module). This call is not appropriate for use by mainboard
400 * initialization logic, which usually runs during an arch_initcall() long
401 * before any i2c_adapter could exist.
403 * This returns the new i2c client, which may be saved for later use with
404 * i2c_unregister_device(); or NULL to indicate an error.
407 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
409 struct i2c_client *client;
412 client = kzalloc(sizeof *client, GFP_KERNEL);
416 client->adapter = adap;
418 client->dev.platform_data = info->platform_data;
421 client->dev.archdata = *info->archdata;
423 client->flags = info->flags;
424 client->addr = info->addr;
425 client->irq = info->irq;
427 strlcpy(client->name, info->type, sizeof(client->name));
429 /* Check for address validity */
430 status = i2c_check_client_addr_validity(client);
432 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
433 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
437 /* Check for address business */
438 status = i2c_check_addr(adap, client->addr);
442 client->dev.parent = &client->adapter->dev;
443 client->dev.bus = &i2c_bus_type;
444 client->dev.type = &i2c_client_type;
446 client->dev.of_node = info->of_node;
449 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
451 status = device_register(&client->dev);
455 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
456 client->name, dev_name(&client->dev));
461 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
462 "(%d)\n", client->name, client->addr, status);
467 EXPORT_SYMBOL_GPL(i2c_new_device);
471 * i2c_unregister_device - reverse effect of i2c_new_device()
472 * @client: value returned from i2c_new_device()
475 void i2c_unregister_device(struct i2c_client *client)
477 device_unregister(&client->dev);
479 EXPORT_SYMBOL_GPL(i2c_unregister_device);
482 static const struct i2c_device_id dummy_id[] = {
487 static int dummy_probe(struct i2c_client *client,
488 const struct i2c_device_id *id)
493 static int dummy_remove(struct i2c_client *client)
498 static struct i2c_driver dummy_driver = {
499 .driver.name = "dummy",
500 .probe = dummy_probe,
501 .remove = dummy_remove,
502 .id_table = dummy_id,
506 * i2c_new_dummy - return a new i2c device bound to a dummy driver
507 * @adapter: the adapter managing the device
508 * @address: seven bit address to be used
511 * This returns an I2C client bound to the "dummy" driver, intended for use
512 * with devices that consume multiple addresses. Examples of such chips
513 * include various EEPROMS (like 24c04 and 24c08 models).
515 * These dummy devices have two main uses. First, most I2C and SMBus calls
516 * except i2c_transfer() need a client handle; the dummy will be that handle.
517 * And second, this prevents the specified address from being bound to a
520 * This returns the new i2c client, which should be saved for later use with
521 * i2c_unregister_device(); or NULL to indicate an error.
523 struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
525 struct i2c_board_info info = {
526 I2C_BOARD_INFO("dummy", address),
529 return i2c_new_device(adapter, &info);
531 EXPORT_SYMBOL_GPL(i2c_new_dummy);
533 /* ------------------------------------------------------------------------- */
535 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
537 static void i2c_adapter_dev_release(struct device *dev)
539 struct i2c_adapter *adap = to_i2c_adapter(dev);
540 complete(&adap->dev_released);
544 * Let users instantiate I2C devices through sysfs. This can be used when
545 * platform initialization code doesn't contain the proper data for
546 * whatever reason. Also useful for drivers that do device detection and
547 * detection fails, either because the device uses an unexpected address,
548 * or this is a compatible device with different ID register values.
550 * Parameter checking may look overzealous, but we really don't want
551 * the user to provide incorrect parameters.
554 i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
555 const char *buf, size_t count)
557 struct i2c_adapter *adap = to_i2c_adapter(dev);
558 struct i2c_board_info info;
559 struct i2c_client *client;
563 dev_warn(dev, "The new_device interface is still experimental "
564 "and may change in a near future\n");
565 memset(&info, 0, sizeof(struct i2c_board_info));
567 blank = strchr(buf, ' ');
569 dev_err(dev, "%s: Missing parameters\n", "new_device");
572 if (blank - buf > I2C_NAME_SIZE - 1) {
573 dev_err(dev, "%s: Invalid device name\n", "new_device");
576 memcpy(info.type, buf, blank - buf);
578 /* Parse remaining parameters, reject extra parameters */
579 res = sscanf(++blank, "%hi%c", &info.addr, &end);
581 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
584 if (res > 1 && end != '\n') {
585 dev_err(dev, "%s: Extra parameters\n", "new_device");
589 client = i2c_new_device(adap, &info);
593 /* Keep track of the added device */
594 i2c_lock_adapter(adap);
595 list_add_tail(&client->detected, &adap->userspace_clients);
596 i2c_unlock_adapter(adap);
597 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
598 info.type, info.addr);
604 * And of course let the users delete the devices they instantiated, if
605 * they got it wrong. This interface can only be used to delete devices
606 * instantiated by i2c_sysfs_new_device above. This guarantees that we
607 * don't delete devices to which some kernel code still has references.
609 * Parameter checking may look overzealous, but we really don't want
610 * the user to delete the wrong device.
613 i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
614 const char *buf, size_t count)
616 struct i2c_adapter *adap = to_i2c_adapter(dev);
617 struct i2c_client *client, *next;
622 /* Parse parameters, reject extra parameters */
623 res = sscanf(buf, "%hi%c", &addr, &end);
625 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
628 if (res > 1 && end != '\n') {
629 dev_err(dev, "%s: Extra parameters\n", "delete_device");
633 /* Make sure the device was added through sysfs */
635 i2c_lock_adapter(adap);
636 list_for_each_entry_safe(client, next, &adap->userspace_clients,
638 if (client->addr == addr) {
639 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
640 "delete_device", client->name, client->addr);
642 list_del(&client->detected);
643 i2c_unregister_device(client);
648 i2c_unlock_adapter(adap);
651 dev_err(dev, "%s: Can't find device in list\n",
656 static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
657 static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
659 static struct attribute *i2c_adapter_attrs[] = {
661 &dev_attr_new_device.attr,
662 &dev_attr_delete_device.attr,
666 static struct attribute_group i2c_adapter_attr_group = {
667 .attrs = i2c_adapter_attrs,
670 static const struct attribute_group *i2c_adapter_attr_groups[] = {
671 &i2c_adapter_attr_group,
675 static struct device_type i2c_adapter_type = {
676 .groups = i2c_adapter_attr_groups,
677 .release = i2c_adapter_dev_release,
680 #ifdef CONFIG_I2C_COMPAT
681 static struct class_compat *i2c_adapter_compat_class;
684 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
686 struct i2c_devinfo *devinfo;
688 down_read(&__i2c_board_lock);
689 list_for_each_entry(devinfo, &__i2c_board_list, list) {
690 if (devinfo->busnum == adapter->nr
691 && !i2c_new_device(adapter,
692 &devinfo->board_info))
693 dev_err(&adapter->dev,
694 "Can't create device at 0x%02x\n",
695 devinfo->board_info.addr);
697 up_read(&__i2c_board_lock);
700 static int i2c_do_add_adapter(struct i2c_driver *driver,
701 struct i2c_adapter *adap)
703 /* Detect supported devices on that bus, and instantiate them */
704 i2c_detect(adap, driver);
706 /* Let legacy drivers scan this bus for matching devices */
707 if (driver->attach_adapter) {
708 /* We ignore the return code; if it fails, too bad */
709 driver->attach_adapter(adap);
714 static int __process_new_adapter(struct device_driver *d, void *data)
716 return i2c_do_add_adapter(to_i2c_driver(d), data);
719 static int i2c_register_adapter(struct i2c_adapter *adap)
723 /* Can't register until after driver model init */
724 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
729 rt_mutex_init(&adap->bus_lock);
730 INIT_LIST_HEAD(&adap->userspace_clients);
732 /* Set default timeout to 1 second if not already set */
733 if (adap->timeout == 0)
736 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
737 adap->dev.bus = &i2c_bus_type;
738 adap->dev.type = &i2c_adapter_type;
739 res = device_register(&adap->dev);
743 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
745 #ifdef CONFIG_I2C_COMPAT
746 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
750 "Failed to create compatibility class link\n");
753 /* create pre-declared device nodes */
754 if (adap->nr < __i2c_first_dynamic_bus_num)
755 i2c_scan_static_board_info(adap);
758 mutex_lock(&core_lock);
759 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
760 __process_new_adapter);
761 mutex_unlock(&core_lock);
766 mutex_lock(&core_lock);
767 idr_remove(&i2c_adapter_idr, adap->nr);
768 mutex_unlock(&core_lock);
773 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
774 * @adapter: the adapter to add
777 * This routine is used to declare an I2C adapter when its bus number
778 * doesn't matter. Examples: for I2C adapters dynamically added by
779 * USB links or PCI plugin cards.
781 * When this returns zero, a new bus number was allocated and stored
782 * in adap->nr, and the specified adapter became available for clients.
783 * Otherwise, a negative errno value is returned.
785 int i2c_add_adapter(struct i2c_adapter *adapter)
790 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
793 mutex_lock(&core_lock);
794 /* "above" here means "above or equal to", sigh */
795 res = idr_get_new_above(&i2c_adapter_idr, adapter,
796 __i2c_first_dynamic_bus_num, &id);
797 mutex_unlock(&core_lock);
806 return i2c_register_adapter(adapter);
808 EXPORT_SYMBOL(i2c_add_adapter);
811 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
812 * @adap: the adapter to register (with adap->nr initialized)
815 * This routine is used to declare an I2C adapter when its bus number
816 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
817 * or otherwise built in to the system's mainboard, and where i2c_board_info
818 * is used to properly configure I2C devices.
820 * If no devices have pre-been declared for this bus, then be sure to
821 * register the adapter before any dynamically allocated ones. Otherwise
822 * the required bus ID may not be available.
824 * When this returns zero, the specified adapter became available for
825 * clients using the bus number provided in adap->nr. Also, the table
826 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
827 * and the appropriate driver model device nodes are created. Otherwise, a
828 * negative errno value is returned.
830 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
835 if (adap->nr & ~MAX_ID_MASK)
839 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
842 mutex_lock(&core_lock);
843 /* "above" here means "above or equal to", sigh;
844 * we need the "equal to" result to force the result
846 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
847 if (status == 0 && id != adap->nr) {
849 idr_remove(&i2c_adapter_idr, id);
851 mutex_unlock(&core_lock);
852 if (status == -EAGAIN)
856 status = i2c_register_adapter(adap);
859 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
861 static int i2c_do_del_adapter(struct i2c_driver *driver,
862 struct i2c_adapter *adapter)
864 struct i2c_client *client, *_n;
867 /* Remove the devices we created ourselves as the result of hardware
868 * probing (using a driver's detect method) */
869 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
870 if (client->adapter == adapter) {
871 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
872 client->name, client->addr);
873 list_del(&client->detected);
874 i2c_unregister_device(client);
878 if (!driver->detach_adapter)
880 res = driver->detach_adapter(adapter);
882 dev_err(&adapter->dev, "detach_adapter failed (%d) "
883 "for driver [%s]\n", res, driver->driver.name);
887 static int __unregister_client(struct device *dev, void *dummy)
889 struct i2c_client *client = i2c_verify_client(dev);
891 i2c_unregister_device(client);
895 static int __process_removed_adapter(struct device_driver *d, void *data)
897 return i2c_do_del_adapter(to_i2c_driver(d), data);
901 * i2c_del_adapter - unregister I2C adapter
902 * @adap: the adapter being unregistered
905 * This unregisters an I2C adapter which was previously registered
906 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
908 int i2c_del_adapter(struct i2c_adapter *adap)
911 struct i2c_adapter *found;
912 struct i2c_client *client, *next;
914 /* First make sure that this adapter was ever added */
915 mutex_lock(&core_lock);
916 found = idr_find(&i2c_adapter_idr, adap->nr);
917 mutex_unlock(&core_lock);
919 pr_debug("i2c-core: attempting to delete unregistered "
920 "adapter [%s]\n", adap->name);
924 /* Tell drivers about this removal */
925 mutex_lock(&core_lock);
926 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
927 __process_removed_adapter);
928 mutex_unlock(&core_lock);
932 /* Remove devices instantiated from sysfs */
933 i2c_lock_adapter(adap);
934 list_for_each_entry_safe(client, next, &adap->userspace_clients,
936 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
938 list_del(&client->detected);
939 i2c_unregister_device(client);
941 i2c_unlock_adapter(adap);
943 /* Detach any active clients. This can't fail, thus we do not
944 checking the returned value. */
945 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
947 #ifdef CONFIG_I2C_COMPAT
948 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
952 /* device name is gone after device_unregister */
953 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
955 /* clean up the sysfs representation */
956 init_completion(&adap->dev_released);
957 device_unregister(&adap->dev);
959 /* wait for sysfs to drop all references */
960 wait_for_completion(&adap->dev_released);
963 mutex_lock(&core_lock);
964 idr_remove(&i2c_adapter_idr, adap->nr);
965 mutex_unlock(&core_lock);
967 /* Clear the device structure in case this adapter is ever going to be
969 memset(&adap->dev, 0, sizeof(adap->dev));
973 EXPORT_SYMBOL(i2c_del_adapter);
976 /* ------------------------------------------------------------------------- */
978 static int __process_new_driver(struct device *dev, void *data)
980 if (dev->type != &i2c_adapter_type)
982 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
986 * An i2c_driver is used with one or more i2c_client (device) nodes to access
987 * i2c slave chips, on a bus instance associated with some i2c_adapter.
990 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
994 /* Can't register until after driver model init */
995 if (unlikely(WARN_ON(!i2c_bus_type.p)))
998 /* add the driver to the list of i2c drivers in the driver core */
999 driver->driver.owner = owner;
1000 driver->driver.bus = &i2c_bus_type;
1002 /* When registration returns, the driver core
1003 * will have called probe() for all matching-but-unbound devices.
1005 res = driver_register(&driver->driver);
1009 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
1011 INIT_LIST_HEAD(&driver->clients);
1012 /* Walk the adapters that are already present */
1013 mutex_lock(&core_lock);
1014 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver);
1015 mutex_unlock(&core_lock);
1019 EXPORT_SYMBOL(i2c_register_driver);
1021 static int __process_removed_driver(struct device *dev, void *data)
1023 if (dev->type != &i2c_adapter_type)
1025 return i2c_do_del_adapter(data, to_i2c_adapter(dev));
1029 * i2c_del_driver - unregister I2C driver
1030 * @driver: the driver being unregistered
1031 * Context: can sleep
1033 void i2c_del_driver(struct i2c_driver *driver)
1035 mutex_lock(&core_lock);
1036 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_removed_driver);
1037 mutex_unlock(&core_lock);
1039 driver_unregister(&driver->driver);
1040 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
1042 EXPORT_SYMBOL(i2c_del_driver);
1044 /* ------------------------------------------------------------------------- */
1046 static int __i2c_check_addr(struct device *dev, void *addrp)
1048 struct i2c_client *client = i2c_verify_client(dev);
1049 int addr = *(int *)addrp;
1051 if (client && client->addr == addr)
1056 static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
1058 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
1062 * i2c_use_client - increments the reference count of the i2c client structure
1063 * @client: the client being referenced
1065 * Each live reference to a client should be refcounted. The driver model does
1066 * that automatically as part of driver binding, so that most drivers don't
1067 * need to do this explicitly: they hold a reference until they're unbound
1070 * A pointer to the client with the incremented reference counter is returned.
1072 struct i2c_client *i2c_use_client(struct i2c_client *client)
1074 if (client && get_device(&client->dev))
1078 EXPORT_SYMBOL(i2c_use_client);
1081 * i2c_release_client - release a use of the i2c client structure
1082 * @client: the client being no longer referenced
1084 * Must be called when a user of a client is finished with it.
1086 void i2c_release_client(struct i2c_client *client)
1089 put_device(&client->dev);
1091 EXPORT_SYMBOL(i2c_release_client);
1093 struct i2c_cmd_arg {
1098 static int i2c_cmd(struct device *dev, void *_arg)
1100 struct i2c_client *client = i2c_verify_client(dev);
1101 struct i2c_cmd_arg *arg = _arg;
1103 if (client && client->driver && client->driver->command)
1104 client->driver->command(client, arg->cmd, arg->arg);
1108 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1110 struct i2c_cmd_arg cmd_arg;
1114 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
1116 EXPORT_SYMBOL(i2c_clients_command);
1118 static int __init i2c_init(void)
1122 retval = bus_register(&i2c_bus_type);
1125 #ifdef CONFIG_I2C_COMPAT
1126 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1127 if (!i2c_adapter_compat_class) {
1132 retval = i2c_add_driver(&dummy_driver);
1138 #ifdef CONFIG_I2C_COMPAT
1139 class_compat_unregister(i2c_adapter_compat_class);
1142 bus_unregister(&i2c_bus_type);
1146 static void __exit i2c_exit(void)
1148 i2c_del_driver(&dummy_driver);
1149 #ifdef CONFIG_I2C_COMPAT
1150 class_compat_unregister(i2c_adapter_compat_class);
1152 bus_unregister(&i2c_bus_type);
1155 /* We must initialize early, because some subsystems register i2c drivers
1156 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1158 postcore_initcall(i2c_init);
1159 module_exit(i2c_exit);
1161 /* ----------------------------------------------------
1162 * the functional interface to the i2c busses.
1163 * ----------------------------------------------------
1167 * i2c_transfer - execute a single or combined I2C message
1168 * @adap: Handle to I2C bus
1169 * @msgs: One or more messages to execute before STOP is issued to
1170 * terminate the operation; each message begins with a START.
1171 * @num: Number of messages to be executed.
1173 * Returns negative errno, else the number of messages executed.
1175 * Note that there is no requirement that each message be sent to
1176 * the same slave address, although that is the most common model.
1178 int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1180 unsigned long orig_jiffies;
1183 /* REVISIT the fault reporting model here is weak:
1185 * - When we get an error after receiving N bytes from a slave,
1186 * there is no way to report "N".
1188 * - When we get a NAK after transmitting N bytes to a slave,
1189 * there is no way to report "N" ... or to let the master
1190 * continue executing the rest of this combined message, if
1191 * that's the appropriate response.
1193 * - When for example "num" is two and we successfully complete
1194 * the first message but get an error part way through the
1195 * second, it's unclear whether that should be reported as
1196 * one (discarding status on the second message) or errno
1197 * (discarding status on the first one).
1200 if (adap->algo->master_xfer) {
1202 for (ret = 0; ret < num; ret++) {
1203 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
1204 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1205 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1206 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
1210 if (in_atomic() || irqs_disabled()) {
1211 ret = rt_mutex_trylock(&adap->bus_lock);
1213 /* I2C activity is ongoing. */
1216 rt_mutex_lock(&adap->bus_lock);
1219 /* Retry automatically on arbitration loss */
1220 orig_jiffies = jiffies;
1221 for (ret = 0, try = 0; try <= adap->retries; try++) {
1222 ret = adap->algo->master_xfer(adap, msgs, num);
1225 if (time_after(jiffies, orig_jiffies + adap->timeout))
1228 rt_mutex_unlock(&adap->bus_lock);
1232 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
1236 EXPORT_SYMBOL(i2c_transfer);
1239 * i2c_master_send - issue a single I2C message in master transmit mode
1240 * @client: Handle to slave device
1241 * @buf: Data that will be written to the slave
1242 * @count: How many bytes to write, must be less than 64k since msg.len is u16
1244 * Returns negative errno, or else the number of bytes written.
1246 int i2c_master_send(struct i2c_client *client, const char *buf, int count)
1249 struct i2c_adapter *adap = client->adapter;
1252 msg.addr = client->addr;
1253 msg.flags = client->flags & I2C_M_TEN;
1255 msg.buf = (char *)buf;
1257 ret = i2c_transfer(adap, &msg, 1);
1259 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1260 transmitted, else error code. */
1261 return (ret == 1) ? count : ret;
1263 EXPORT_SYMBOL(i2c_master_send);
1266 * i2c_master_recv - issue a single I2C message in master receive mode
1267 * @client: Handle to slave device
1268 * @buf: Where to store data read from slave
1269 * @count: How many bytes to read, must be less than 64k since msg.len is u16
1271 * Returns negative errno, or else the number of bytes read.
1273 int i2c_master_recv(struct i2c_client *client, char *buf, int count)
1275 struct i2c_adapter *adap = client->adapter;
1279 msg.addr = client->addr;
1280 msg.flags = client->flags & I2C_M_TEN;
1281 msg.flags |= I2C_M_RD;
1285 ret = i2c_transfer(adap, &msg, 1);
1287 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1288 transmitted, else error code. */
1289 return (ret == 1) ? count : ret;
1291 EXPORT_SYMBOL(i2c_master_recv);
1293 /* ----------------------------------------------------
1294 * the i2c address scanning function
1295 * Will not work for 10-bit addresses!
1296 * ----------------------------------------------------
1300 * Legacy default probe function, mostly relevant for SMBus. The default
1301 * probe method is a quick write, but it is known to corrupt the 24RF08
1302 * EEPROMs due to a state machine bug, and could also irreversibly
1303 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1304 * we use a short byte read instead. Also, some bus drivers don't implement
1305 * quick write, so we fallback to a byte read in that case too.
1306 * On x86, there is another special case for FSC hardware monitoring chips,
1307 * which want regular byte reads (address 0x73.) Fortunately, these are the
1308 * only known chips using this I2C address on PC hardware.
1309 * Returns 1 if probe succeeded, 0 if not.
1311 static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1314 union i2c_smbus_data dummy;
1317 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1318 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1319 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1320 I2C_SMBUS_BYTE_DATA, &dummy);
1323 if ((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50
1324 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
1325 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1326 I2C_SMBUS_BYTE, &dummy);
1328 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1329 I2C_SMBUS_QUICK, NULL);
1334 static int i2c_detect_address(struct i2c_client *temp_client,
1335 struct i2c_driver *driver)
1337 struct i2c_board_info info;
1338 struct i2c_adapter *adapter = temp_client->adapter;
1339 int addr = temp_client->addr;
1342 /* Make sure the address is valid */
1343 if (addr < 0x03 || addr > 0x77) {
1344 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1349 /* Skip if already in use */
1350 if (i2c_check_addr(adapter, addr))
1353 /* Make sure there is something at this address */
1354 if (!i2c_default_probe(adapter, addr))
1357 /* Finally call the custom detection function */
1358 memset(&info, 0, sizeof(struct i2c_board_info));
1360 err = driver->detect(temp_client, &info);
1362 /* -ENODEV is returned if the detection fails. We catch it
1363 here as this isn't an error. */
1364 return err == -ENODEV ? 0 : err;
1367 /* Consistency check */
1368 if (info.type[0] == '\0') {
1369 dev_err(&adapter->dev, "%s detection function provided "
1370 "no name for 0x%x\n", driver->driver.name,
1373 struct i2c_client *client;
1375 /* Detection succeeded, instantiate the device */
1376 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1377 info.type, info.addr);
1378 client = i2c_new_device(adapter, &info);
1380 list_add_tail(&client->detected, &driver->clients);
1382 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1383 info.type, info.addr);
1388 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1390 const unsigned short *address_list;
1391 struct i2c_client *temp_client;
1393 int adap_id = i2c_adapter_id(adapter);
1395 address_list = driver->address_list;
1396 if (!driver->detect || !address_list)
1399 /* Set up a temporary client to help detect callback */
1400 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1403 temp_client->adapter = adapter;
1405 /* Stop here if the classes do not match */
1406 if (!(adapter->class & driver->class))
1409 /* Stop here if we can't use SMBUS_QUICK */
1410 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1411 if (address_list[0] == I2C_CLIENT_END)
1414 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1415 "can't probe for chips\n");
1420 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
1421 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1422 "addr 0x%02x\n", adap_id, address_list[i]);
1423 temp_client->addr = address_list[i];
1424 err = i2c_detect_address(temp_client, driver);
1435 i2c_new_probed_device(struct i2c_adapter *adap,
1436 struct i2c_board_info *info,
1437 unsigned short const *addr_list)
1441 /* Stop here if the bus doesn't support probing */
1442 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1443 dev_err(&adap->dev, "Probing not supported\n");
1447 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1448 /* Check address validity */
1449 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1450 dev_warn(&adap->dev, "Invalid 7-bit address "
1451 "0x%02x\n", addr_list[i]);
1455 /* Check address availability */
1456 if (i2c_check_addr(adap, addr_list[i])) {
1457 dev_dbg(&adap->dev, "Address 0x%02x already in "
1458 "use, not probing\n", addr_list[i]);
1462 /* Test address responsiveness */
1463 if (i2c_default_probe(adap, addr_list[i]))
1467 if (addr_list[i] == I2C_CLIENT_END) {
1468 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1472 info->addr = addr_list[i];
1473 return i2c_new_device(adap, info);
1475 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1477 struct i2c_adapter *i2c_get_adapter(int id)
1479 struct i2c_adapter *adapter;
1481 mutex_lock(&core_lock);
1482 adapter = idr_find(&i2c_adapter_idr, id);
1483 if (adapter && !try_module_get(adapter->owner))
1486 mutex_unlock(&core_lock);
1489 EXPORT_SYMBOL(i2c_get_adapter);
1491 void i2c_put_adapter(struct i2c_adapter *adap)
1493 module_put(adap->owner);
1495 EXPORT_SYMBOL(i2c_put_adapter);
1497 /* The SMBus parts */
1499 #define POLY (0x1070U << 3)
1500 static u8 crc8(u16 data)
1504 for (i = 0; i < 8; i++) {
1509 return (u8)(data >> 8);
1512 /* Incremental CRC8 over count bytes in the array pointed to by p */
1513 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1517 for (i = 0; i < count; i++)
1518 crc = crc8((crc ^ p[i]) << 8);
1522 /* Assume a 7-bit address, which is reasonable for SMBus */
1523 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1525 /* The address will be sent first */
1526 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1527 pec = i2c_smbus_pec(pec, &addr, 1);
1529 /* The data buffer follows */
1530 return i2c_smbus_pec(pec, msg->buf, msg->len);
1533 /* Used for write only transactions */
1534 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1536 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1540 /* Return <0 on CRC error
1541 If there was a write before this read (most cases) we need to take the
1542 partial CRC from the write part into account.
1543 Note that this function does modify the message (we need to decrease the
1544 message length to hide the CRC byte from the caller). */
1545 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1547 u8 rpec = msg->buf[--msg->len];
1548 cpec = i2c_smbus_msg_pec(cpec, msg);
1551 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1559 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1560 * @client: Handle to slave device
1562 * This executes the SMBus "receive byte" protocol, returning negative errno
1563 * else the byte received from the device.
1565 s32 i2c_smbus_read_byte(struct i2c_client *client)
1567 union i2c_smbus_data data;
1570 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1572 I2C_SMBUS_BYTE, &data);
1573 return (status < 0) ? status : data.byte;
1575 EXPORT_SYMBOL(i2c_smbus_read_byte);
1578 * i2c_smbus_write_byte - SMBus "send byte" protocol
1579 * @client: Handle to slave device
1580 * @value: Byte to be sent
1582 * This executes the SMBus "send byte" protocol, returning negative errno
1583 * else zero on success.
1585 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1587 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1588 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1590 EXPORT_SYMBOL(i2c_smbus_write_byte);
1593 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1594 * @client: Handle to slave device
1595 * @command: Byte interpreted by slave
1597 * This executes the SMBus "read byte" protocol, returning negative errno
1598 * else a data byte received from the device.
1600 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1602 union i2c_smbus_data data;
1605 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1606 I2C_SMBUS_READ, command,
1607 I2C_SMBUS_BYTE_DATA, &data);
1608 return (status < 0) ? status : data.byte;
1610 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1613 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1614 * @client: Handle to slave device
1615 * @command: Byte interpreted by slave
1616 * @value: Byte being written
1618 * This executes the SMBus "write byte" protocol, returning negative errno
1619 * else zero on success.
1621 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1623 union i2c_smbus_data data;
1625 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1626 I2C_SMBUS_WRITE, command,
1627 I2C_SMBUS_BYTE_DATA, &data);
1629 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1632 * i2c_smbus_read_word_data - SMBus "read word" protocol
1633 * @client: Handle to slave device
1634 * @command: Byte interpreted by slave
1636 * This executes the SMBus "read word" protocol, returning negative errno
1637 * else a 16-bit unsigned "word" received from the device.
1639 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1641 union i2c_smbus_data data;
1644 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1645 I2C_SMBUS_READ, command,
1646 I2C_SMBUS_WORD_DATA, &data);
1647 return (status < 0) ? status : data.word;
1649 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1652 * i2c_smbus_write_word_data - SMBus "write word" protocol
1653 * @client: Handle to slave device
1654 * @command: Byte interpreted by slave
1655 * @value: 16-bit "word" being written
1657 * This executes the SMBus "write word" protocol, returning negative errno
1658 * else zero on success.
1660 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1662 union i2c_smbus_data data;
1664 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1665 I2C_SMBUS_WRITE, command,
1666 I2C_SMBUS_WORD_DATA, &data);
1668 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1671 * i2c_smbus_process_call - SMBus "process call" protocol
1672 * @client: Handle to slave device
1673 * @command: Byte interpreted by slave
1674 * @value: 16-bit "word" being written
1676 * This executes the SMBus "process call" protocol, returning negative errno
1677 * else a 16-bit unsigned "word" received from the device.
1679 s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1681 union i2c_smbus_data data;
1685 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1686 I2C_SMBUS_WRITE, command,
1687 I2C_SMBUS_PROC_CALL, &data);
1688 return (status < 0) ? status : data.word;
1690 EXPORT_SYMBOL(i2c_smbus_process_call);
1693 * i2c_smbus_read_block_data - SMBus "block read" protocol
1694 * @client: Handle to slave device
1695 * @command: Byte interpreted by slave
1696 * @values: Byte array into which data will be read; big enough to hold
1697 * the data returned by the slave. SMBus allows at most 32 bytes.
1699 * This executes the SMBus "block read" protocol, returning negative errno
1700 * else the number of data bytes in the slave's response.
1702 * Note that using this function requires that the client's adapter support
1703 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1704 * support this; its emulation through I2C messaging relies on a specific
1705 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1707 s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1710 union i2c_smbus_data data;
1713 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1714 I2C_SMBUS_READ, command,
1715 I2C_SMBUS_BLOCK_DATA, &data);
1719 memcpy(values, &data.block[1], data.block[0]);
1720 return data.block[0];
1722 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1725 * i2c_smbus_write_block_data - SMBus "block write" protocol
1726 * @client: Handle to slave device
1727 * @command: Byte interpreted by slave
1728 * @length: Size of data block; SMBus allows at most 32 bytes
1729 * @values: Byte array which will be written.
1731 * This executes the SMBus "block write" protocol, returning negative errno
1732 * else zero on success.
1734 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1735 u8 length, const u8 *values)
1737 union i2c_smbus_data data;
1739 if (length > I2C_SMBUS_BLOCK_MAX)
1740 length = I2C_SMBUS_BLOCK_MAX;
1741 data.block[0] = length;
1742 memcpy(&data.block[1], values, length);
1743 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1744 I2C_SMBUS_WRITE, command,
1745 I2C_SMBUS_BLOCK_DATA, &data);
1747 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1749 /* Returns the number of read bytes */
1750 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1751 u8 length, u8 *values)
1753 union i2c_smbus_data data;
1756 if (length > I2C_SMBUS_BLOCK_MAX)
1757 length = I2C_SMBUS_BLOCK_MAX;
1758 data.block[0] = length;
1759 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1760 I2C_SMBUS_READ, command,
1761 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1765 memcpy(values, &data.block[1], data.block[0]);
1766 return data.block[0];
1768 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1770 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1771 u8 length, const u8 *values)
1773 union i2c_smbus_data data;
1775 if (length > I2C_SMBUS_BLOCK_MAX)
1776 length = I2C_SMBUS_BLOCK_MAX;
1777 data.block[0] = length;
1778 memcpy(data.block + 1, values, length);
1779 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1780 I2C_SMBUS_WRITE, command,
1781 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1783 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1785 /* Simulate a SMBus command using the i2c protocol
1786 No checking of parameters is done! */
1787 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
1788 unsigned short flags,
1789 char read_write, u8 command, int size,
1790 union i2c_smbus_data *data)
1792 /* So we need to generate a series of msgs. In the case of writing, we
1793 need to use only one message; when reading, we need two. We initialize
1794 most things with sane defaults, to keep the code below somewhat
1796 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1797 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1798 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
1799 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1800 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1806 msgbuf0[0] = command;
1808 case I2C_SMBUS_QUICK:
1810 /* Special case: The read/write field is used as data */
1811 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1815 case I2C_SMBUS_BYTE:
1816 if (read_write == I2C_SMBUS_READ) {
1817 /* Special case: only a read! */
1818 msg[0].flags = I2C_M_RD | flags;
1822 case I2C_SMBUS_BYTE_DATA:
1823 if (read_write == I2C_SMBUS_READ)
1827 msgbuf0[1] = data->byte;
1830 case I2C_SMBUS_WORD_DATA:
1831 if (read_write == I2C_SMBUS_READ)
1835 msgbuf0[1] = data->word & 0xff;
1836 msgbuf0[2] = data->word >> 8;
1839 case I2C_SMBUS_PROC_CALL:
1840 num = 2; /* Special case */
1841 read_write = I2C_SMBUS_READ;
1844 msgbuf0[1] = data->word & 0xff;
1845 msgbuf0[2] = data->word >> 8;
1847 case I2C_SMBUS_BLOCK_DATA:
1848 if (read_write == I2C_SMBUS_READ) {
1849 msg[1].flags |= I2C_M_RECV_LEN;
1850 msg[1].len = 1; /* block length will be added by
1851 the underlying bus driver */
1853 msg[0].len = data->block[0] + 2;
1854 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1855 dev_err(&adapter->dev,
1856 "Invalid block write size %d\n",
1860 for (i = 1; i < msg[0].len; i++)
1861 msgbuf0[i] = data->block[i-1];
1864 case I2C_SMBUS_BLOCK_PROC_CALL:
1865 num = 2; /* Another special case */
1866 read_write = I2C_SMBUS_READ;
1867 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1868 dev_err(&adapter->dev,
1869 "Invalid block write size %d\n",
1873 msg[0].len = data->block[0] + 2;
1874 for (i = 1; i < msg[0].len; i++)
1875 msgbuf0[i] = data->block[i-1];
1876 msg[1].flags |= I2C_M_RECV_LEN;
1877 msg[1].len = 1; /* block length will be added by
1878 the underlying bus driver */
1880 case I2C_SMBUS_I2C_BLOCK_DATA:
1881 if (read_write == I2C_SMBUS_READ) {
1882 msg[1].len = data->block[0];
1884 msg[0].len = data->block[0] + 1;
1885 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1886 dev_err(&adapter->dev,
1887 "Invalid block write size %d\n",
1891 for (i = 1; i <= data->block[0]; i++)
1892 msgbuf0[i] = data->block[i];
1896 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1900 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1901 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1903 /* Compute PEC if first message is a write */
1904 if (!(msg[0].flags & I2C_M_RD)) {
1905 if (num == 1) /* Write only */
1906 i2c_smbus_add_pec(&msg[0]);
1907 else /* Write followed by read */
1908 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1910 /* Ask for PEC if last message is a read */
1911 if (msg[num-1].flags & I2C_M_RD)
1915 status = i2c_transfer(adapter, msg, num);
1919 /* Check PEC if last message is a read */
1920 if (i && (msg[num-1].flags & I2C_M_RD)) {
1921 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1926 if (read_write == I2C_SMBUS_READ)
1928 case I2C_SMBUS_BYTE:
1929 data->byte = msgbuf0[0];
1931 case I2C_SMBUS_BYTE_DATA:
1932 data->byte = msgbuf1[0];
1934 case I2C_SMBUS_WORD_DATA:
1935 case I2C_SMBUS_PROC_CALL:
1936 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1938 case I2C_SMBUS_I2C_BLOCK_DATA:
1939 for (i = 0; i < data->block[0]; i++)
1940 data->block[i+1] = msgbuf1[i];
1942 case I2C_SMBUS_BLOCK_DATA:
1943 case I2C_SMBUS_BLOCK_PROC_CALL:
1944 for (i = 0; i < msgbuf1[0] + 1; i++)
1945 data->block[i] = msgbuf1[i];
1952 * i2c_smbus_xfer - execute SMBus protocol operations
1953 * @adapter: Handle to I2C bus
1954 * @addr: Address of SMBus slave on that bus
1955 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1956 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1957 * @command: Byte interpreted by slave, for protocols which use such bytes
1958 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1959 * @data: Data to be read or written
1961 * This executes an SMBus protocol operation, and returns a negative
1962 * errno code else zero on success.
1964 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
1965 char read_write, u8 command, int protocol,
1966 union i2c_smbus_data *data)
1968 unsigned long orig_jiffies;
1972 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1974 if (adapter->algo->smbus_xfer) {
1975 rt_mutex_lock(&adapter->bus_lock);
1977 /* Retry automatically on arbitration loss */
1978 orig_jiffies = jiffies;
1979 for (res = 0, try = 0; try <= adapter->retries; try++) {
1980 res = adapter->algo->smbus_xfer(adapter, addr, flags,
1981 read_write, command,
1985 if (time_after(jiffies,
1986 orig_jiffies + adapter->timeout))
1989 rt_mutex_unlock(&adapter->bus_lock);
1991 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
1992 command, protocol, data);
1996 EXPORT_SYMBOL(i2c_smbus_xfer);
1998 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1999 MODULE_DESCRIPTION("I2C-Bus main module");
2000 MODULE_LICENSE("GPL");