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/of_i2c.h>
34 #include <linux/of_device.h>
35 #include <linux/completion.h>
36 #include <linux/hardirq.h>
37 #include <linux/irqflags.h>
38 #include <linux/rwsem.h>
39 #include <linux/pm_runtime.h>
40 #include <asm/uaccess.h>
45 /* core_lock protects i2c_adapter_idr, and guarantees
46 that device detection, deletion of detected devices, and attach_adapter
47 and detach_adapter calls are serialized */
48 static DEFINE_MUTEX(core_lock);
49 static DEFINE_IDR(i2c_adapter_idr);
51 static struct device_type i2c_client_type;
52 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
54 /* ------------------------------------------------------------------------- */
56 static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
57 const struct i2c_client *client)
60 if (strcmp(client->name, id->name) == 0)
67 static int i2c_device_match(struct device *dev, struct device_driver *drv)
69 struct i2c_client *client = i2c_verify_client(dev);
70 struct i2c_driver *driver;
75 /* Attempt an OF style match */
76 if (of_driver_match_device(dev, drv))
79 driver = to_i2c_driver(drv);
80 /* match on an id table if there is one */
82 return i2c_match_id(driver->id_table, client) != NULL;
89 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
90 static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
92 struct i2c_client *client = to_i2c_client(dev);
94 if (add_uevent_var(env, "MODALIAS=%s%s",
95 I2C_MODULE_PREFIX, client->name))
97 dev_dbg(dev, "uevent\n");
102 #define i2c_device_uevent NULL
103 #endif /* CONFIG_HOTPLUG */
105 static int i2c_device_probe(struct device *dev)
107 struct i2c_client *client = i2c_verify_client(dev);
108 struct i2c_driver *driver;
114 driver = to_i2c_driver(dev->driver);
115 if (!driver->probe || !driver->id_table)
117 client->driver = driver;
118 if (!device_can_wakeup(&client->dev))
119 device_init_wakeup(&client->dev,
120 client->flags & I2C_CLIENT_WAKE);
121 dev_dbg(dev, "probe\n");
123 status = driver->probe(client, i2c_match_id(driver->id_table, client));
125 client->driver = NULL;
126 i2c_set_clientdata(client, NULL);
131 static int i2c_device_remove(struct device *dev)
133 struct i2c_client *client = i2c_verify_client(dev);
134 struct i2c_driver *driver;
137 if (!client || !dev->driver)
140 driver = to_i2c_driver(dev->driver);
141 if (driver->remove) {
142 dev_dbg(dev, "remove\n");
143 status = driver->remove(client);
149 client->driver = NULL;
150 i2c_set_clientdata(client, NULL);
155 static void i2c_device_shutdown(struct device *dev)
157 struct i2c_client *client = i2c_verify_client(dev);
158 struct i2c_driver *driver;
160 if (!client || !dev->driver)
162 driver = to_i2c_driver(dev->driver);
163 if (driver->shutdown)
164 driver->shutdown(client);
167 #ifdef CONFIG_PM_SLEEP
168 static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
170 struct i2c_client *client = i2c_verify_client(dev);
171 struct i2c_driver *driver;
173 if (!client || !dev->driver)
175 driver = to_i2c_driver(dev->driver);
176 if (!driver->suspend)
178 return driver->suspend(client, mesg);
181 static int i2c_legacy_resume(struct device *dev)
183 struct i2c_client *client = i2c_verify_client(dev);
184 struct i2c_driver *driver;
186 if (!client || !dev->driver)
188 driver = to_i2c_driver(dev->driver);
191 return driver->resume(client);
194 static int i2c_device_pm_suspend(struct device *dev)
196 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
198 if (pm_runtime_suspended(dev))
202 return pm->suspend ? pm->suspend(dev) : 0;
204 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
207 static int i2c_device_pm_resume(struct device *dev)
209 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
213 ret = pm->resume ? pm->resume(dev) : 0;
215 ret = i2c_legacy_resume(dev);
218 pm_runtime_disable(dev);
219 pm_runtime_set_active(dev);
220 pm_runtime_enable(dev);
226 static int i2c_device_pm_freeze(struct device *dev)
228 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
230 if (pm_runtime_suspended(dev))
234 return pm->freeze ? pm->freeze(dev) : 0;
236 return i2c_legacy_suspend(dev, PMSG_FREEZE);
239 static int i2c_device_pm_thaw(struct device *dev)
241 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
243 if (pm_runtime_suspended(dev))
247 return pm->thaw ? pm->thaw(dev) : 0;
249 return i2c_legacy_resume(dev);
252 static int i2c_device_pm_poweroff(struct device *dev)
254 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
256 if (pm_runtime_suspended(dev))
260 return pm->poweroff ? pm->poweroff(dev) : 0;
262 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
265 static int i2c_device_pm_restore(struct device *dev)
267 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
271 ret = pm->restore ? pm->restore(dev) : 0;
273 ret = i2c_legacy_resume(dev);
276 pm_runtime_disable(dev);
277 pm_runtime_set_active(dev);
278 pm_runtime_enable(dev);
283 #else /* !CONFIG_PM_SLEEP */
284 #define i2c_device_pm_suspend NULL
285 #define i2c_device_pm_resume NULL
286 #define i2c_device_pm_freeze NULL
287 #define i2c_device_pm_thaw NULL
288 #define i2c_device_pm_poweroff NULL
289 #define i2c_device_pm_restore NULL
290 #endif /* !CONFIG_PM_SLEEP */
292 static void i2c_client_dev_release(struct device *dev)
294 kfree(to_i2c_client(dev));
298 show_name(struct device *dev, struct device_attribute *attr, char *buf)
300 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
301 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
305 show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
307 struct i2c_client *client = to_i2c_client(dev);
308 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
311 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
312 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
314 static struct attribute *i2c_dev_attrs[] = {
316 /* modalias helps coldplug: modprobe $(cat .../modalias) */
317 &dev_attr_modalias.attr,
321 static struct attribute_group i2c_dev_attr_group = {
322 .attrs = i2c_dev_attrs,
325 static const struct attribute_group *i2c_dev_attr_groups[] = {
330 static const struct dev_pm_ops i2c_device_pm_ops = {
331 .suspend = i2c_device_pm_suspend,
332 .resume = i2c_device_pm_resume,
333 .freeze = i2c_device_pm_freeze,
334 .thaw = i2c_device_pm_thaw,
335 .poweroff = i2c_device_pm_poweroff,
336 .restore = i2c_device_pm_restore,
338 pm_generic_runtime_suspend,
339 pm_generic_runtime_resume,
340 pm_generic_runtime_idle
344 struct bus_type i2c_bus_type = {
346 .match = i2c_device_match,
347 .probe = i2c_device_probe,
348 .remove = i2c_device_remove,
349 .shutdown = i2c_device_shutdown,
350 .pm = &i2c_device_pm_ops,
352 EXPORT_SYMBOL_GPL(i2c_bus_type);
354 static struct device_type i2c_client_type = {
355 .groups = i2c_dev_attr_groups,
356 .uevent = i2c_device_uevent,
357 .release = i2c_client_dev_release,
362 * i2c_verify_client - return parameter as i2c_client, or NULL
363 * @dev: device, probably from some driver model iterator
365 * When traversing the driver model tree, perhaps using driver model
366 * iterators like @device_for_each_child(), you can't assume very much
367 * about the nodes you find. Use this function to avoid oopses caused
368 * by wrongly treating some non-I2C device as an i2c_client.
370 struct i2c_client *i2c_verify_client(struct device *dev)
372 return (dev->type == &i2c_client_type)
376 EXPORT_SYMBOL(i2c_verify_client);
379 /* This is a permissive address validity check, I2C address map constraints
380 * are purposedly not enforced, except for the general call address. */
381 static int i2c_check_client_addr_validity(const struct i2c_client *client)
383 if (client->flags & I2C_CLIENT_TEN) {
384 /* 10-bit address, all values are valid */
385 if (client->addr > 0x3ff)
388 /* 7-bit address, reject the general call address */
389 if (client->addr == 0x00 || client->addr > 0x7f)
395 /* And this is a strict address validity check, used when probing. If a
396 * device uses a reserved address, then it shouldn't be probed. 7-bit
397 * addressing is assumed, 10-bit address devices are rare and should be
398 * explicitly enumerated. */
399 static int i2c_check_addr_validity(unsigned short addr)
402 * Reserved addresses per I2C specification:
403 * 0x00 General call address / START byte
405 * 0x02 Reserved for different bus format
406 * 0x03 Reserved for future purposes
407 * 0x04-0x07 Hs-mode master code
408 * 0x78-0x7b 10-bit slave addressing
409 * 0x7c-0x7f Reserved for future purposes
411 if (addr < 0x08 || addr > 0x77)
416 static int __i2c_check_addr_busy(struct device *dev, void *addrp)
418 struct i2c_client *client = i2c_verify_client(dev);
419 int addr = *(int *)addrp;
421 if (client && client->addr == addr)
426 static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
428 return device_for_each_child(&adapter->dev, &addr,
429 __i2c_check_addr_busy);
433 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
434 * @adapter: Target I2C bus segment
436 void i2c_lock_adapter(struct i2c_adapter *adapter)
438 rt_mutex_lock(&adapter->bus_lock);
440 EXPORT_SYMBOL_GPL(i2c_lock_adapter);
443 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
444 * @adapter: Target I2C bus segment
446 static int i2c_trylock_adapter(struct i2c_adapter *adapter)
448 return rt_mutex_trylock(&adapter->bus_lock);
452 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
453 * @adapter: Target I2C bus segment
455 void i2c_unlock_adapter(struct i2c_adapter *adapter)
457 rt_mutex_unlock(&adapter->bus_lock);
459 EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
462 * i2c_new_device - instantiate an i2c device
463 * @adap: the adapter managing the device
464 * @info: describes one I2C device; bus_num is ignored
467 * Create an i2c device. Binding is handled through driver model
468 * probe()/remove() methods. A driver may be bound to this device when we
469 * return from this function, or any later moment (e.g. maybe hotplugging will
470 * load the driver module). This call is not appropriate for use by mainboard
471 * initialization logic, which usually runs during an arch_initcall() long
472 * before any i2c_adapter could exist.
474 * This returns the new i2c client, which may be saved for later use with
475 * i2c_unregister_device(); or NULL to indicate an error.
478 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
480 struct i2c_client *client;
483 client = kzalloc(sizeof *client, GFP_KERNEL);
487 client->adapter = adap;
489 client->dev.platform_data = info->platform_data;
492 client->dev.archdata = *info->archdata;
494 client->flags = info->flags;
495 client->addr = info->addr;
496 client->irq = info->irq;
498 strlcpy(client->name, info->type, sizeof(client->name));
500 /* Check for address validity */
501 status = i2c_check_client_addr_validity(client);
503 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
504 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
508 /* Check for address business */
509 status = i2c_check_addr_busy(adap, client->addr);
513 client->dev.parent = &client->adapter->dev;
514 client->dev.bus = &i2c_bus_type;
515 client->dev.type = &i2c_client_type;
517 client->dev.of_node = info->of_node;
520 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
522 status = device_register(&client->dev);
526 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
527 client->name, dev_name(&client->dev));
532 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
533 "(%d)\n", client->name, client->addr, status);
538 EXPORT_SYMBOL_GPL(i2c_new_device);
542 * i2c_unregister_device - reverse effect of i2c_new_device()
543 * @client: value returned from i2c_new_device()
546 void i2c_unregister_device(struct i2c_client *client)
548 device_unregister(&client->dev);
550 EXPORT_SYMBOL_GPL(i2c_unregister_device);
553 static const struct i2c_device_id dummy_id[] = {
558 static int dummy_probe(struct i2c_client *client,
559 const struct i2c_device_id *id)
564 static int dummy_remove(struct i2c_client *client)
569 static struct i2c_driver dummy_driver = {
570 .driver.name = "dummy",
571 .probe = dummy_probe,
572 .remove = dummy_remove,
573 .id_table = dummy_id,
577 * i2c_new_dummy - return a new i2c device bound to a dummy driver
578 * @adapter: the adapter managing the device
579 * @address: seven bit address to be used
582 * This returns an I2C client bound to the "dummy" driver, intended for use
583 * with devices that consume multiple addresses. Examples of such chips
584 * include various EEPROMS (like 24c04 and 24c08 models).
586 * These dummy devices have two main uses. First, most I2C and SMBus calls
587 * except i2c_transfer() need a client handle; the dummy will be that handle.
588 * And second, this prevents the specified address from being bound to a
591 * This returns the new i2c client, which should be saved for later use with
592 * i2c_unregister_device(); or NULL to indicate an error.
594 struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
596 struct i2c_board_info info = {
597 I2C_BOARD_INFO("dummy", address),
600 return i2c_new_device(adapter, &info);
602 EXPORT_SYMBOL_GPL(i2c_new_dummy);
604 /* ------------------------------------------------------------------------- */
606 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
608 static void i2c_adapter_dev_release(struct device *dev)
610 struct i2c_adapter *adap = to_i2c_adapter(dev);
611 complete(&adap->dev_released);
615 * Let users instantiate I2C devices through sysfs. This can be used when
616 * platform initialization code doesn't contain the proper data for
617 * whatever reason. Also useful for drivers that do device detection and
618 * detection fails, either because the device uses an unexpected address,
619 * or this is a compatible device with different ID register values.
621 * Parameter checking may look overzealous, but we really don't want
622 * the user to provide incorrect parameters.
625 i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
626 const char *buf, size_t count)
628 struct i2c_adapter *adap = to_i2c_adapter(dev);
629 struct i2c_board_info info;
630 struct i2c_client *client;
634 dev_warn(dev, "The new_device interface is still experimental "
635 "and may change in a near future\n");
636 memset(&info, 0, sizeof(struct i2c_board_info));
638 blank = strchr(buf, ' ');
640 dev_err(dev, "%s: Missing parameters\n", "new_device");
643 if (blank - buf > I2C_NAME_SIZE - 1) {
644 dev_err(dev, "%s: Invalid device name\n", "new_device");
647 memcpy(info.type, buf, blank - buf);
649 /* Parse remaining parameters, reject extra parameters */
650 res = sscanf(++blank, "%hi%c", &info.addr, &end);
652 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
655 if (res > 1 && end != '\n') {
656 dev_err(dev, "%s: Extra parameters\n", "new_device");
660 client = i2c_new_device(adap, &info);
664 /* Keep track of the added device */
665 i2c_lock_adapter(adap);
666 list_add_tail(&client->detected, &adap->userspace_clients);
667 i2c_unlock_adapter(adap);
668 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
669 info.type, info.addr);
675 * And of course let the users delete the devices they instantiated, if
676 * they got it wrong. This interface can only be used to delete devices
677 * instantiated by i2c_sysfs_new_device above. This guarantees that we
678 * don't delete devices to which some kernel code still has references.
680 * Parameter checking may look overzealous, but we really don't want
681 * the user to delete the wrong device.
684 i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
685 const char *buf, size_t count)
687 struct i2c_adapter *adap = to_i2c_adapter(dev);
688 struct i2c_client *client, *next;
693 /* Parse parameters, reject extra parameters */
694 res = sscanf(buf, "%hi%c", &addr, &end);
696 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
699 if (res > 1 && end != '\n') {
700 dev_err(dev, "%s: Extra parameters\n", "delete_device");
704 /* Make sure the device was added through sysfs */
706 i2c_lock_adapter(adap);
707 list_for_each_entry_safe(client, next, &adap->userspace_clients,
709 if (client->addr == addr) {
710 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
711 "delete_device", client->name, client->addr);
713 list_del(&client->detected);
714 i2c_unregister_device(client);
719 i2c_unlock_adapter(adap);
722 dev_err(dev, "%s: Can't find device in list\n",
727 static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
728 static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
730 static struct attribute *i2c_adapter_attrs[] = {
732 &dev_attr_new_device.attr,
733 &dev_attr_delete_device.attr,
737 static struct attribute_group i2c_adapter_attr_group = {
738 .attrs = i2c_adapter_attrs,
741 static const struct attribute_group *i2c_adapter_attr_groups[] = {
742 &i2c_adapter_attr_group,
746 static struct device_type i2c_adapter_type = {
747 .groups = i2c_adapter_attr_groups,
748 .release = i2c_adapter_dev_release,
751 #ifdef CONFIG_I2C_COMPAT
752 static struct class_compat *i2c_adapter_compat_class;
755 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
757 struct i2c_devinfo *devinfo;
759 down_read(&__i2c_board_lock);
760 list_for_each_entry(devinfo, &__i2c_board_list, list) {
761 if (devinfo->busnum == adapter->nr
762 && !i2c_new_device(adapter,
763 &devinfo->board_info))
764 dev_err(&adapter->dev,
765 "Can't create device at 0x%02x\n",
766 devinfo->board_info.addr);
768 up_read(&__i2c_board_lock);
771 static int i2c_do_add_adapter(struct i2c_driver *driver,
772 struct i2c_adapter *adap)
774 /* Detect supported devices on that bus, and instantiate them */
775 i2c_detect(adap, driver);
777 /* Let legacy drivers scan this bus for matching devices */
778 if (driver->attach_adapter) {
779 /* We ignore the return code; if it fails, too bad */
780 driver->attach_adapter(adap);
785 static int __process_new_adapter(struct device_driver *d, void *data)
787 return i2c_do_add_adapter(to_i2c_driver(d), data);
790 static int i2c_register_adapter(struct i2c_adapter *adap)
794 /* Can't register until after driver model init */
795 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
800 rt_mutex_init(&adap->bus_lock);
801 INIT_LIST_HEAD(&adap->userspace_clients);
803 /* Set default timeout to 1 second if not already set */
804 if (adap->timeout == 0)
807 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
808 adap->dev.bus = &i2c_bus_type;
809 adap->dev.type = &i2c_adapter_type;
810 res = device_register(&adap->dev);
814 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
816 #ifdef CONFIG_I2C_COMPAT
817 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
821 "Failed to create compatibility class link\n");
824 /* create pre-declared device nodes */
825 if (adap->nr < __i2c_first_dynamic_bus_num)
826 i2c_scan_static_board_info(adap);
828 /* Register devices from the device tree */
829 of_i2c_register_devices(adap);
832 mutex_lock(&core_lock);
833 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
834 mutex_unlock(&core_lock);
839 mutex_lock(&core_lock);
840 idr_remove(&i2c_adapter_idr, adap->nr);
841 mutex_unlock(&core_lock);
846 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
847 * @adapter: the adapter to add
850 * This routine is used to declare an I2C adapter when its bus number
851 * doesn't matter. Examples: for I2C adapters dynamically added by
852 * USB links or PCI plugin cards.
854 * When this returns zero, a new bus number was allocated and stored
855 * in adap->nr, and the specified adapter became available for clients.
856 * Otherwise, a negative errno value is returned.
858 int i2c_add_adapter(struct i2c_adapter *adapter)
863 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
866 mutex_lock(&core_lock);
867 /* "above" here means "above or equal to", sigh */
868 res = idr_get_new_above(&i2c_adapter_idr, adapter,
869 __i2c_first_dynamic_bus_num, &id);
870 mutex_unlock(&core_lock);
879 return i2c_register_adapter(adapter);
881 EXPORT_SYMBOL(i2c_add_adapter);
884 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
885 * @adap: the adapter to register (with adap->nr initialized)
888 * This routine is used to declare an I2C adapter when its bus number
889 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
890 * or otherwise built in to the system's mainboard, and where i2c_board_info
891 * is used to properly configure I2C devices.
893 * If no devices have pre-been declared for this bus, then be sure to
894 * register the adapter before any dynamically allocated ones. Otherwise
895 * the required bus ID may not be available.
897 * When this returns zero, the specified adapter became available for
898 * clients using the bus number provided in adap->nr. Also, the table
899 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
900 * and the appropriate driver model device nodes are created. Otherwise, a
901 * negative errno value is returned.
903 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
908 if (adap->nr & ~MAX_ID_MASK)
912 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
915 mutex_lock(&core_lock);
916 /* "above" here means "above or equal to", sigh;
917 * we need the "equal to" result to force the result
919 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
920 if (status == 0 && id != adap->nr) {
922 idr_remove(&i2c_adapter_idr, id);
924 mutex_unlock(&core_lock);
925 if (status == -EAGAIN)
929 status = i2c_register_adapter(adap);
932 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
934 static int i2c_do_del_adapter(struct i2c_driver *driver,
935 struct i2c_adapter *adapter)
937 struct i2c_client *client, *_n;
940 /* Remove the devices we created ourselves as the result of hardware
941 * probing (using a driver's detect method) */
942 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
943 if (client->adapter == adapter) {
944 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
945 client->name, client->addr);
946 list_del(&client->detected);
947 i2c_unregister_device(client);
951 if (!driver->detach_adapter)
953 res = driver->detach_adapter(adapter);
955 dev_err(&adapter->dev, "detach_adapter failed (%d) "
956 "for driver [%s]\n", res, driver->driver.name);
960 static int __unregister_client(struct device *dev, void *dummy)
962 struct i2c_client *client = i2c_verify_client(dev);
964 i2c_unregister_device(client);
968 static int __process_removed_adapter(struct device_driver *d, void *data)
970 return i2c_do_del_adapter(to_i2c_driver(d), data);
974 * i2c_del_adapter - unregister I2C adapter
975 * @adap: the adapter being unregistered
978 * This unregisters an I2C adapter which was previously registered
979 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
981 int i2c_del_adapter(struct i2c_adapter *adap)
984 struct i2c_adapter *found;
985 struct i2c_client *client, *next;
987 /* First make sure that this adapter was ever added */
988 mutex_lock(&core_lock);
989 found = idr_find(&i2c_adapter_idr, adap->nr);
990 mutex_unlock(&core_lock);
992 pr_debug("i2c-core: attempting to delete unregistered "
993 "adapter [%s]\n", adap->name);
997 /* Tell drivers about this removal */
998 mutex_lock(&core_lock);
999 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
1000 __process_removed_adapter);
1001 mutex_unlock(&core_lock);
1005 /* Remove devices instantiated from sysfs */
1006 i2c_lock_adapter(adap);
1007 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1009 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1011 list_del(&client->detected);
1012 i2c_unregister_device(client);
1014 i2c_unlock_adapter(adap);
1016 /* Detach any active clients. This can't fail, thus we do not
1017 checking the returned value. */
1018 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
1020 #ifdef CONFIG_I2C_COMPAT
1021 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1025 /* device name is gone after device_unregister */
1026 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1028 /* clean up the sysfs representation */
1029 init_completion(&adap->dev_released);
1030 device_unregister(&adap->dev);
1032 /* wait for sysfs to drop all references */
1033 wait_for_completion(&adap->dev_released);
1036 mutex_lock(&core_lock);
1037 idr_remove(&i2c_adapter_idr, adap->nr);
1038 mutex_unlock(&core_lock);
1040 /* Clear the device structure in case this adapter is ever going to be
1042 memset(&adap->dev, 0, sizeof(adap->dev));
1046 EXPORT_SYMBOL(i2c_del_adapter);
1049 /* ------------------------------------------------------------------------- */
1051 static int __process_new_driver(struct device *dev, void *data)
1053 if (dev->type != &i2c_adapter_type)
1055 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
1059 * An i2c_driver is used with one or more i2c_client (device) nodes to access
1060 * i2c slave chips, on a bus instance associated with some i2c_adapter.
1063 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
1067 /* Can't register until after driver model init */
1068 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1071 /* add the driver to the list of i2c drivers in the driver core */
1072 driver->driver.owner = owner;
1073 driver->driver.bus = &i2c_bus_type;
1075 /* When registration returns, the driver core
1076 * will have called probe() for all matching-but-unbound devices.
1078 res = driver_register(&driver->driver);
1082 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
1084 INIT_LIST_HEAD(&driver->clients);
1085 /* Walk the adapters that are already present */
1086 mutex_lock(&core_lock);
1087 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver);
1088 mutex_unlock(&core_lock);
1092 EXPORT_SYMBOL(i2c_register_driver);
1094 static int __process_removed_driver(struct device *dev, void *data)
1096 if (dev->type != &i2c_adapter_type)
1098 return i2c_do_del_adapter(data, to_i2c_adapter(dev));
1102 * i2c_del_driver - unregister I2C driver
1103 * @driver: the driver being unregistered
1104 * Context: can sleep
1106 void i2c_del_driver(struct i2c_driver *driver)
1108 mutex_lock(&core_lock);
1109 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_removed_driver);
1110 mutex_unlock(&core_lock);
1112 driver_unregister(&driver->driver);
1113 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
1115 EXPORT_SYMBOL(i2c_del_driver);
1117 /* ------------------------------------------------------------------------- */
1120 * i2c_use_client - increments the reference count of the i2c client structure
1121 * @client: the client being referenced
1123 * Each live reference to a client should be refcounted. The driver model does
1124 * that automatically as part of driver binding, so that most drivers don't
1125 * need to do this explicitly: they hold a reference until they're unbound
1128 * A pointer to the client with the incremented reference counter is returned.
1130 struct i2c_client *i2c_use_client(struct i2c_client *client)
1132 if (client && get_device(&client->dev))
1136 EXPORT_SYMBOL(i2c_use_client);
1139 * i2c_release_client - release a use of the i2c client structure
1140 * @client: the client being no longer referenced
1142 * Must be called when a user of a client is finished with it.
1144 void i2c_release_client(struct i2c_client *client)
1147 put_device(&client->dev);
1149 EXPORT_SYMBOL(i2c_release_client);
1151 struct i2c_cmd_arg {
1156 static int i2c_cmd(struct device *dev, void *_arg)
1158 struct i2c_client *client = i2c_verify_client(dev);
1159 struct i2c_cmd_arg *arg = _arg;
1161 if (client && client->driver && client->driver->command)
1162 client->driver->command(client, arg->cmd, arg->arg);
1166 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1168 struct i2c_cmd_arg cmd_arg;
1172 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
1174 EXPORT_SYMBOL(i2c_clients_command);
1176 static int __init i2c_init(void)
1180 retval = bus_register(&i2c_bus_type);
1183 #ifdef CONFIG_I2C_COMPAT
1184 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1185 if (!i2c_adapter_compat_class) {
1190 retval = i2c_add_driver(&dummy_driver);
1196 #ifdef CONFIG_I2C_COMPAT
1197 class_compat_unregister(i2c_adapter_compat_class);
1200 bus_unregister(&i2c_bus_type);
1204 static void __exit i2c_exit(void)
1206 i2c_del_driver(&dummy_driver);
1207 #ifdef CONFIG_I2C_COMPAT
1208 class_compat_unregister(i2c_adapter_compat_class);
1210 bus_unregister(&i2c_bus_type);
1213 /* We must initialize early, because some subsystems register i2c drivers
1214 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1216 postcore_initcall(i2c_init);
1217 module_exit(i2c_exit);
1219 /* ----------------------------------------------------
1220 * the functional interface to the i2c busses.
1221 * ----------------------------------------------------
1225 * i2c_transfer - execute a single or combined I2C message
1226 * @adap: Handle to I2C bus
1227 * @msgs: One or more messages to execute before STOP is issued to
1228 * terminate the operation; each message begins with a START.
1229 * @num: Number of messages to be executed.
1231 * Returns negative errno, else the number of messages executed.
1233 * Note that there is no requirement that each message be sent to
1234 * the same slave address, although that is the most common model.
1236 int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1238 unsigned long orig_jiffies;
1241 /* REVISIT the fault reporting model here is weak:
1243 * - When we get an error after receiving N bytes from a slave,
1244 * there is no way to report "N".
1246 * - When we get a NAK after transmitting N bytes to a slave,
1247 * there is no way to report "N" ... or to let the master
1248 * continue executing the rest of this combined message, if
1249 * that's the appropriate response.
1251 * - When for example "num" is two and we successfully complete
1252 * the first message but get an error part way through the
1253 * second, it's unclear whether that should be reported as
1254 * one (discarding status on the second message) or errno
1255 * (discarding status on the first one).
1258 if (adap->algo->master_xfer) {
1260 for (ret = 0; ret < num; ret++) {
1261 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
1262 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1263 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1264 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
1268 if (in_atomic() || irqs_disabled()) {
1269 ret = i2c_trylock_adapter(adap);
1271 /* I2C activity is ongoing. */
1274 i2c_lock_adapter(adap);
1277 /* Retry automatically on arbitration loss */
1278 orig_jiffies = jiffies;
1279 for (ret = 0, try = 0; try <= adap->retries; try++) {
1280 ret = adap->algo->master_xfer(adap, msgs, num);
1283 if (time_after(jiffies, orig_jiffies + adap->timeout))
1286 i2c_unlock_adapter(adap);
1290 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
1294 EXPORT_SYMBOL(i2c_transfer);
1297 * i2c_master_send - issue a single I2C message in master transmit mode
1298 * @client: Handle to slave device
1299 * @buf: Data that will be written to the slave
1300 * @count: How many bytes to write, must be less than 64k since msg.len is u16
1302 * Returns negative errno, or else the number of bytes written.
1304 int i2c_master_send(struct i2c_client *client, const char *buf, int count)
1307 struct i2c_adapter *adap = client->adapter;
1310 msg.addr = client->addr;
1311 msg.flags = client->flags & I2C_M_TEN;
1313 msg.buf = (char *)buf;
1315 ret = i2c_transfer(adap, &msg, 1);
1317 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1318 transmitted, else error code. */
1319 return (ret == 1) ? count : ret;
1321 EXPORT_SYMBOL(i2c_master_send);
1324 * i2c_master_recv - issue a single I2C message in master receive mode
1325 * @client: Handle to slave device
1326 * @buf: Where to store data read from slave
1327 * @count: How many bytes to read, must be less than 64k since msg.len is u16
1329 * Returns negative errno, or else the number of bytes read.
1331 int i2c_master_recv(struct i2c_client *client, char *buf, int count)
1333 struct i2c_adapter *adap = client->adapter;
1337 msg.addr = client->addr;
1338 msg.flags = client->flags & I2C_M_TEN;
1339 msg.flags |= I2C_M_RD;
1343 ret = i2c_transfer(adap, &msg, 1);
1345 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1346 transmitted, else error code. */
1347 return (ret == 1) ? count : ret;
1349 EXPORT_SYMBOL(i2c_master_recv);
1351 /* ----------------------------------------------------
1352 * the i2c address scanning function
1353 * Will not work for 10-bit addresses!
1354 * ----------------------------------------------------
1358 * Legacy default probe function, mostly relevant for SMBus. The default
1359 * probe method is a quick write, but it is known to corrupt the 24RF08
1360 * EEPROMs due to a state machine bug, and could also irreversibly
1361 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1362 * we use a short byte read instead. Also, some bus drivers don't implement
1363 * quick write, so we fallback to a byte read in that case too.
1364 * On x86, there is another special case for FSC hardware monitoring chips,
1365 * which want regular byte reads (address 0x73.) Fortunately, these are the
1366 * only known chips using this I2C address on PC hardware.
1367 * Returns 1 if probe succeeded, 0 if not.
1369 static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1372 union i2c_smbus_data dummy;
1375 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1376 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1377 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1378 I2C_SMBUS_BYTE_DATA, &dummy);
1381 if ((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50
1382 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
1383 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1384 I2C_SMBUS_BYTE, &dummy);
1386 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1387 I2C_SMBUS_QUICK, NULL);
1392 static int i2c_detect_address(struct i2c_client *temp_client,
1393 struct i2c_driver *driver)
1395 struct i2c_board_info info;
1396 struct i2c_adapter *adapter = temp_client->adapter;
1397 int addr = temp_client->addr;
1400 /* Make sure the address is valid */
1401 err = i2c_check_addr_validity(addr);
1403 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1408 /* Skip if already in use */
1409 if (i2c_check_addr_busy(adapter, addr))
1412 /* Make sure there is something at this address */
1413 if (!i2c_default_probe(adapter, addr))
1416 /* Finally call the custom detection function */
1417 memset(&info, 0, sizeof(struct i2c_board_info));
1419 err = driver->detect(temp_client, &info);
1421 /* -ENODEV is returned if the detection fails. We catch it
1422 here as this isn't an error. */
1423 return err == -ENODEV ? 0 : err;
1426 /* Consistency check */
1427 if (info.type[0] == '\0') {
1428 dev_err(&adapter->dev, "%s detection function provided "
1429 "no name for 0x%x\n", driver->driver.name,
1432 struct i2c_client *client;
1434 /* Detection succeeded, instantiate the device */
1435 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1436 info.type, info.addr);
1437 client = i2c_new_device(adapter, &info);
1439 list_add_tail(&client->detected, &driver->clients);
1441 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1442 info.type, info.addr);
1447 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1449 const unsigned short *address_list;
1450 struct i2c_client *temp_client;
1452 int adap_id = i2c_adapter_id(adapter);
1454 address_list = driver->address_list;
1455 if (!driver->detect || !address_list)
1458 /* Set up a temporary client to help detect callback */
1459 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1462 temp_client->adapter = adapter;
1464 /* Stop here if the classes do not match */
1465 if (!(adapter->class & driver->class))
1468 /* Stop here if the bus doesn't support probing */
1469 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE)) {
1470 if (address_list[0] == I2C_CLIENT_END)
1473 dev_warn(&adapter->dev, "Probing not supported\n");
1478 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
1479 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1480 "addr 0x%02x\n", adap_id, address_list[i]);
1481 temp_client->addr = address_list[i];
1482 err = i2c_detect_address(temp_client, driver);
1492 int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
1494 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1495 I2C_SMBUS_QUICK, NULL) >= 0;
1497 EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
1500 i2c_new_probed_device(struct i2c_adapter *adap,
1501 struct i2c_board_info *info,
1502 unsigned short const *addr_list,
1503 int (*probe)(struct i2c_adapter *, unsigned short addr))
1508 /* Stop here if the bus doesn't support probing */
1509 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1510 dev_err(&adap->dev, "Probing not supported\n");
1513 probe = i2c_default_probe;
1516 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1517 /* Check address validity */
1518 if (i2c_check_addr_validity(addr_list[i]) < 0) {
1519 dev_warn(&adap->dev, "Invalid 7-bit address "
1520 "0x%02x\n", addr_list[i]);
1524 /* Check address availability */
1525 if (i2c_check_addr_busy(adap, addr_list[i])) {
1526 dev_dbg(&adap->dev, "Address 0x%02x already in "
1527 "use, not probing\n", addr_list[i]);
1531 /* Test address responsiveness */
1532 if (probe(adap, addr_list[i]))
1536 if (addr_list[i] == I2C_CLIENT_END) {
1537 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1541 info->addr = addr_list[i];
1542 return i2c_new_device(adap, info);
1544 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1546 struct i2c_adapter *i2c_get_adapter(int id)
1548 struct i2c_adapter *adapter;
1550 mutex_lock(&core_lock);
1551 adapter = idr_find(&i2c_adapter_idr, id);
1552 if (adapter && !try_module_get(adapter->owner))
1555 mutex_unlock(&core_lock);
1558 EXPORT_SYMBOL(i2c_get_adapter);
1560 void i2c_put_adapter(struct i2c_adapter *adap)
1562 module_put(adap->owner);
1564 EXPORT_SYMBOL(i2c_put_adapter);
1566 /* The SMBus parts */
1568 #define POLY (0x1070U << 3)
1569 static u8 crc8(u16 data)
1573 for (i = 0; i < 8; i++) {
1578 return (u8)(data >> 8);
1581 /* Incremental CRC8 over count bytes in the array pointed to by p */
1582 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1586 for (i = 0; i < count; i++)
1587 crc = crc8((crc ^ p[i]) << 8);
1591 /* Assume a 7-bit address, which is reasonable for SMBus */
1592 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1594 /* The address will be sent first */
1595 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1596 pec = i2c_smbus_pec(pec, &addr, 1);
1598 /* The data buffer follows */
1599 return i2c_smbus_pec(pec, msg->buf, msg->len);
1602 /* Used for write only transactions */
1603 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1605 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1609 /* Return <0 on CRC error
1610 If there was a write before this read (most cases) we need to take the
1611 partial CRC from the write part into account.
1612 Note that this function does modify the message (we need to decrease the
1613 message length to hide the CRC byte from the caller). */
1614 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1616 u8 rpec = msg->buf[--msg->len];
1617 cpec = i2c_smbus_msg_pec(cpec, msg);
1620 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1628 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1629 * @client: Handle to slave device
1631 * This executes the SMBus "receive byte" protocol, returning negative errno
1632 * else the byte received from the device.
1634 s32 i2c_smbus_read_byte(struct i2c_client *client)
1636 union i2c_smbus_data data;
1639 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1641 I2C_SMBUS_BYTE, &data);
1642 return (status < 0) ? status : data.byte;
1644 EXPORT_SYMBOL(i2c_smbus_read_byte);
1647 * i2c_smbus_write_byte - SMBus "send byte" protocol
1648 * @client: Handle to slave device
1649 * @value: Byte to be sent
1651 * This executes the SMBus "send byte" protocol, returning negative errno
1652 * else zero on success.
1654 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1656 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1657 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1659 EXPORT_SYMBOL(i2c_smbus_write_byte);
1662 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1663 * @client: Handle to slave device
1664 * @command: Byte interpreted by slave
1666 * This executes the SMBus "read byte" protocol, returning negative errno
1667 * else a data byte received from the device.
1669 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1671 union i2c_smbus_data data;
1674 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1675 I2C_SMBUS_READ, command,
1676 I2C_SMBUS_BYTE_DATA, &data);
1677 return (status < 0) ? status : data.byte;
1679 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1682 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1683 * @client: Handle to slave device
1684 * @command: Byte interpreted by slave
1685 * @value: Byte being written
1687 * This executes the SMBus "write byte" protocol, returning negative errno
1688 * else zero on success.
1690 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1692 union i2c_smbus_data data;
1694 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1695 I2C_SMBUS_WRITE, command,
1696 I2C_SMBUS_BYTE_DATA, &data);
1698 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1701 * i2c_smbus_read_word_data - SMBus "read word" protocol
1702 * @client: Handle to slave device
1703 * @command: Byte interpreted by slave
1705 * This executes the SMBus "read word" protocol, returning negative errno
1706 * else a 16-bit unsigned "word" received from the device.
1708 s32 i2c_smbus_read_word_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_WORD_DATA, &data);
1716 return (status < 0) ? status : data.word;
1718 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1721 * i2c_smbus_write_word_data - SMBus "write word" protocol
1722 * @client: Handle to slave device
1723 * @command: Byte interpreted by slave
1724 * @value: 16-bit "word" being written
1726 * This executes the SMBus "write word" protocol, returning negative errno
1727 * else zero on success.
1729 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1731 union i2c_smbus_data data;
1733 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1734 I2C_SMBUS_WRITE, command,
1735 I2C_SMBUS_WORD_DATA, &data);
1737 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1740 * i2c_smbus_process_call - SMBus "process call" protocol
1741 * @client: Handle to slave device
1742 * @command: Byte interpreted by slave
1743 * @value: 16-bit "word" being written
1745 * This executes the SMBus "process call" protocol, returning negative errno
1746 * else a 16-bit unsigned "word" received from the device.
1748 s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1750 union i2c_smbus_data data;
1754 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1755 I2C_SMBUS_WRITE, command,
1756 I2C_SMBUS_PROC_CALL, &data);
1757 return (status < 0) ? status : data.word;
1759 EXPORT_SYMBOL(i2c_smbus_process_call);
1762 * i2c_smbus_read_block_data - SMBus "block read" protocol
1763 * @client: Handle to slave device
1764 * @command: Byte interpreted by slave
1765 * @values: Byte array into which data will be read; big enough to hold
1766 * the data returned by the slave. SMBus allows at most 32 bytes.
1768 * This executes the SMBus "block read" protocol, returning negative errno
1769 * else the number of data bytes in the slave's response.
1771 * Note that using this function requires that the client's adapter support
1772 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1773 * support this; its emulation through I2C messaging relies on a specific
1774 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1776 s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1779 union i2c_smbus_data data;
1782 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1783 I2C_SMBUS_READ, command,
1784 I2C_SMBUS_BLOCK_DATA, &data);
1788 memcpy(values, &data.block[1], data.block[0]);
1789 return data.block[0];
1791 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1794 * i2c_smbus_write_block_data - SMBus "block write" protocol
1795 * @client: Handle to slave device
1796 * @command: Byte interpreted by slave
1797 * @length: Size of data block; SMBus allows at most 32 bytes
1798 * @values: Byte array which will be written.
1800 * This executes the SMBus "block write" protocol, returning negative errno
1801 * else zero on success.
1803 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1804 u8 length, const u8 *values)
1806 union i2c_smbus_data data;
1808 if (length > I2C_SMBUS_BLOCK_MAX)
1809 length = I2C_SMBUS_BLOCK_MAX;
1810 data.block[0] = length;
1811 memcpy(&data.block[1], values, length);
1812 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1813 I2C_SMBUS_WRITE, command,
1814 I2C_SMBUS_BLOCK_DATA, &data);
1816 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1818 /* Returns the number of read bytes */
1819 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1820 u8 length, u8 *values)
1822 union i2c_smbus_data data;
1825 if (length > I2C_SMBUS_BLOCK_MAX)
1826 length = I2C_SMBUS_BLOCK_MAX;
1827 data.block[0] = length;
1828 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1829 I2C_SMBUS_READ, command,
1830 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1834 memcpy(values, &data.block[1], data.block[0]);
1835 return data.block[0];
1837 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1839 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1840 u8 length, const u8 *values)
1842 union i2c_smbus_data data;
1844 if (length > I2C_SMBUS_BLOCK_MAX)
1845 length = I2C_SMBUS_BLOCK_MAX;
1846 data.block[0] = length;
1847 memcpy(data.block + 1, values, length);
1848 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1849 I2C_SMBUS_WRITE, command,
1850 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1852 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1854 /* Simulate a SMBus command using the i2c protocol
1855 No checking of parameters is done! */
1856 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
1857 unsigned short flags,
1858 char read_write, u8 command, int size,
1859 union i2c_smbus_data *data)
1861 /* So we need to generate a series of msgs. In the case of writing, we
1862 need to use only one message; when reading, we need two. We initialize
1863 most things with sane defaults, to keep the code below somewhat
1865 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1866 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1867 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
1868 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1869 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1875 msgbuf0[0] = command;
1877 case I2C_SMBUS_QUICK:
1879 /* Special case: The read/write field is used as data */
1880 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1884 case I2C_SMBUS_BYTE:
1885 if (read_write == I2C_SMBUS_READ) {
1886 /* Special case: only a read! */
1887 msg[0].flags = I2C_M_RD | flags;
1891 case I2C_SMBUS_BYTE_DATA:
1892 if (read_write == I2C_SMBUS_READ)
1896 msgbuf0[1] = data->byte;
1899 case I2C_SMBUS_WORD_DATA:
1900 if (read_write == I2C_SMBUS_READ)
1904 msgbuf0[1] = data->word & 0xff;
1905 msgbuf0[2] = data->word >> 8;
1908 case I2C_SMBUS_PROC_CALL:
1909 num = 2; /* Special case */
1910 read_write = I2C_SMBUS_READ;
1913 msgbuf0[1] = data->word & 0xff;
1914 msgbuf0[2] = data->word >> 8;
1916 case I2C_SMBUS_BLOCK_DATA:
1917 if (read_write == I2C_SMBUS_READ) {
1918 msg[1].flags |= I2C_M_RECV_LEN;
1919 msg[1].len = 1; /* block length will be added by
1920 the underlying bus driver */
1922 msg[0].len = data->block[0] + 2;
1923 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1924 dev_err(&adapter->dev,
1925 "Invalid block write size %d\n",
1929 for (i = 1; i < msg[0].len; i++)
1930 msgbuf0[i] = data->block[i-1];
1933 case I2C_SMBUS_BLOCK_PROC_CALL:
1934 num = 2; /* Another special case */
1935 read_write = I2C_SMBUS_READ;
1936 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1937 dev_err(&adapter->dev,
1938 "Invalid block write size %d\n",
1942 msg[0].len = data->block[0] + 2;
1943 for (i = 1; i < msg[0].len; i++)
1944 msgbuf0[i] = data->block[i-1];
1945 msg[1].flags |= I2C_M_RECV_LEN;
1946 msg[1].len = 1; /* block length will be added by
1947 the underlying bus driver */
1949 case I2C_SMBUS_I2C_BLOCK_DATA:
1950 if (read_write == I2C_SMBUS_READ) {
1951 msg[1].len = data->block[0];
1953 msg[0].len = data->block[0] + 1;
1954 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1955 dev_err(&adapter->dev,
1956 "Invalid block write size %d\n",
1960 for (i = 1; i <= data->block[0]; i++)
1961 msgbuf0[i] = data->block[i];
1965 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1969 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1970 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1972 /* Compute PEC if first message is a write */
1973 if (!(msg[0].flags & I2C_M_RD)) {
1974 if (num == 1) /* Write only */
1975 i2c_smbus_add_pec(&msg[0]);
1976 else /* Write followed by read */
1977 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1979 /* Ask for PEC if last message is a read */
1980 if (msg[num-1].flags & I2C_M_RD)
1984 status = i2c_transfer(adapter, msg, num);
1988 /* Check PEC if last message is a read */
1989 if (i && (msg[num-1].flags & I2C_M_RD)) {
1990 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1995 if (read_write == I2C_SMBUS_READ)
1997 case I2C_SMBUS_BYTE:
1998 data->byte = msgbuf0[0];
2000 case I2C_SMBUS_BYTE_DATA:
2001 data->byte = msgbuf1[0];
2003 case I2C_SMBUS_WORD_DATA:
2004 case I2C_SMBUS_PROC_CALL:
2005 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2007 case I2C_SMBUS_I2C_BLOCK_DATA:
2008 for (i = 0; i < data->block[0]; i++)
2009 data->block[i+1] = msgbuf1[i];
2011 case I2C_SMBUS_BLOCK_DATA:
2012 case I2C_SMBUS_BLOCK_PROC_CALL:
2013 for (i = 0; i < msgbuf1[0] + 1; i++)
2014 data->block[i] = msgbuf1[i];
2021 * i2c_smbus_xfer - execute SMBus protocol operations
2022 * @adapter: Handle to I2C bus
2023 * @addr: Address of SMBus slave on that bus
2024 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2025 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2026 * @command: Byte interpreted by slave, for protocols which use such bytes
2027 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2028 * @data: Data to be read or written
2030 * This executes an SMBus protocol operation, and returns a negative
2031 * errno code else zero on success.
2033 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
2034 char read_write, u8 command, int protocol,
2035 union i2c_smbus_data *data)
2037 unsigned long orig_jiffies;
2041 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
2043 if (adapter->algo->smbus_xfer) {
2044 i2c_lock_adapter(adapter);
2046 /* Retry automatically on arbitration loss */
2047 orig_jiffies = jiffies;
2048 for (res = 0, try = 0; try <= adapter->retries; try++) {
2049 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2050 read_write, command,
2054 if (time_after(jiffies,
2055 orig_jiffies + adapter->timeout))
2058 i2c_unlock_adapter(adapter);
2060 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2061 command, protocol, data);
2065 EXPORT_SYMBOL(i2c_smbus_xfer);
2067 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2068 MODULE_DESCRIPTION("I2C-Bus main module");
2069 MODULE_LICENSE("GPL");