1 /* i2c-core.c - a device driver for the iic-bus interface */
2 /* ------------------------------------------------------------------------- */
3 /* Copyright (C) 1995-99 Simon G. Vogl
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* ------------------------------------------------------------------------- */
20 /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
21 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
22 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
23 Jean Delvare <khali@linux-fr.org> */
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/slab.h>
29 #include <linux/i2c.h>
30 #include <linux/init.h>
31 #include <linux/idr.h>
32 #include <linux/mutex.h>
33 #include <linux/completion.h>
34 #include <linux/hardirq.h>
35 #include <linux/irqflags.h>
36 #include <linux/rwsem.h>
37 #include <asm/uaccess.h>
42 /* core_lock protects i2c_adapter_idr, userspace_devices, and guarantees
43 that device detection, deletion of detected devices, and attach_adapter
44 and detach_adapter calls are serialized */
45 static DEFINE_MUTEX(core_lock);
46 static DEFINE_IDR(i2c_adapter_idr);
47 static LIST_HEAD(userspace_devices);
49 static struct device_type i2c_client_type;
50 static int i2c_check_addr(struct i2c_adapter *adapter, int addr);
51 static int i2c_check_addr_ex(struct i2c_adapter *adapter, int addr);
52 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
54 /* ------------------------------------------------------------------------- */
55 #ifdef CONFIG_I2C_DEV_RK29
56 extern struct completion i2c_dev_complete;
57 extern void i2c_dev_dump_start(struct i2c_adapter *adap, struct i2c_msg *msgs, int num);
58 extern void i2c_dev_dump_stop(struct i2c_adapter *adap, struct i2c_msg *msgs, int num, int ret);
60 static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
61 const struct i2c_client *client)
64 if (strcmp(client->name, id->name) == 0)
71 static int i2c_device_match(struct device *dev, struct device_driver *drv)
73 struct i2c_client *client = i2c_verify_client(dev);
74 struct i2c_driver *driver;
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;
129 static int i2c_device_remove(struct device *dev)
131 struct i2c_client *client = i2c_verify_client(dev);
132 struct i2c_driver *driver;
135 if (!client || !dev->driver)
138 driver = to_i2c_driver(dev->driver);
139 if (driver->remove) {
140 dev_dbg(dev, "remove\n");
141 status = driver->remove(client);
147 client->driver = NULL;
151 static void i2c_device_shutdown(struct device *dev)
153 struct i2c_client *client = i2c_verify_client(dev);
154 struct i2c_driver *driver;
156 if (!client || !dev->driver)
158 driver = to_i2c_driver(dev->driver);
159 if (driver->shutdown)
160 driver->shutdown(client);
163 static int i2c_device_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_device_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 void i2c_client_dev_release(struct device *dev)
191 kfree(to_i2c_client(dev));
195 show_name(struct device *dev, struct device_attribute *attr, char *buf)
197 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
198 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
202 show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
204 struct i2c_client *client = to_i2c_client(dev);
205 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
208 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
209 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
211 static struct attribute *i2c_dev_attrs[] = {
213 /* modalias helps coldplug: modprobe $(cat .../modalias) */
214 &dev_attr_modalias.attr,
218 static struct attribute_group i2c_dev_attr_group = {
219 .attrs = i2c_dev_attrs,
222 static const struct attribute_group *i2c_dev_attr_groups[] = {
227 struct bus_type i2c_bus_type = {
229 .match = i2c_device_match,
230 .probe = i2c_device_probe,
231 .remove = i2c_device_remove,
232 .shutdown = i2c_device_shutdown,
233 .suspend = i2c_device_suspend,
234 .resume = i2c_device_resume,
236 EXPORT_SYMBOL_GPL(i2c_bus_type);
238 static struct device_type i2c_client_type = {
239 .groups = i2c_dev_attr_groups,
240 .uevent = i2c_device_uevent,
241 .release = i2c_client_dev_release,
246 * i2c_verify_client - return parameter as i2c_client, or NULL
247 * @dev: device, probably from some driver model iterator
249 * When traversing the driver model tree, perhaps using driver model
250 * iterators like @device_for_each_child(), you can't assume very much
251 * about the nodes you find. Use this function to avoid oopses caused
252 * by wrongly treating some non-I2C device as an i2c_client.
254 struct i2c_client *i2c_verify_client(struct device *dev)
256 return (dev->type == &i2c_client_type)
260 EXPORT_SYMBOL(i2c_verify_client);
264 * i2c_new_device - instantiate an i2c device
265 * @adap: the adapter managing the device
266 * @info: describes one I2C device; bus_num is ignored
269 * Create an i2c device. Binding is handled through driver model
270 * probe()/remove() methods. A driver may be bound to this device when we
271 * return from this function, or any later moment (e.g. maybe hotplugging will
272 * load the driver module). This call is not appropriate for use by mainboard
273 * initialization logic, which usually runs during an arch_initcall() long
274 * before any i2c_adapter could exist.
276 * This returns the new i2c client, which may be saved for later use with
277 * i2c_unregister_device(); or NULL to indicate an error.
280 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
282 struct i2c_client *client;
285 client = kzalloc(sizeof *client, GFP_KERNEL);
289 client->adapter = adap;
291 client->dev.platform_data = info->platform_data;
294 client->dev.archdata = *info->archdata;
296 client->flags = info->flags;
297 client->addr = info->addr;
298 client->irq = info->irq;
299 client->udelay = info->udelay; // add by kfx
301 strlcpy(client->name, info->type, sizeof(client->name));
303 /* Check for address business */
305 status = i2c_check_addr(adap, client->addr);
309 /* ddl@rock-chips.com : Devices which have some i2c addr can work in same i2c bus,
310 if devices havn't work at the same time.*/
311 status = i2c_check_addr_ex(adap, client->addr);
313 dev_err(&adap->dev, "%d i2c clients have been registered at 0x%02x",
314 status, client->addr);
317 client->dev.parent = &client->adapter->dev;
318 client->dev.bus = &i2c_bus_type;
319 client->dev.type = &i2c_client_type;
321 /* ddl@rock-chips.com : Devices which have some i2c addr can work in same i2c bus,
322 if devices havn't work at the same time.*/
324 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
328 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
331 dev_set_name(&client->dev, "%d-%04x-%01x", i2c_adapter_id(adap),
332 client->addr,status);
335 status = device_register(&client->dev);
339 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
340 client->name, dev_name(&client->dev));
345 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
346 "(%d)\n", client->name, client->addr, status);
350 EXPORT_SYMBOL_GPL(i2c_new_device);
354 * i2c_unregister_device - reverse effect of i2c_new_device()
355 * @client: value returned from i2c_new_device()
358 void i2c_unregister_device(struct i2c_client *client)
360 device_unregister(&client->dev);
362 EXPORT_SYMBOL_GPL(i2c_unregister_device);
365 static const struct i2c_device_id dummy_id[] = {
370 static int dummy_probe(struct i2c_client *client,
371 const struct i2c_device_id *id)
376 static int dummy_remove(struct i2c_client *client)
381 static struct i2c_driver dummy_driver = {
382 .driver.name = "dummy",
383 .probe = dummy_probe,
384 .remove = dummy_remove,
385 .id_table = dummy_id,
389 * i2c_new_dummy - return a new i2c device bound to a dummy driver
390 * @adapter: the adapter managing the device
391 * @address: seven bit address to be used
394 * This returns an I2C client bound to the "dummy" driver, intended for use
395 * with devices that consume multiple addresses. Examples of such chips
396 * include various EEPROMS (like 24c04 and 24c08 models).
398 * These dummy devices have two main uses. First, most I2C and SMBus calls
399 * except i2c_transfer() need a client handle; the dummy will be that handle.
400 * And second, this prevents the specified address from being bound to a
403 * This returns the new i2c client, which should be saved for later use with
404 * i2c_unregister_device(); or NULL to indicate an error.
406 struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
408 struct i2c_board_info info = {
409 I2C_BOARD_INFO("dummy", address),
412 return i2c_new_device(adapter, &info);
414 EXPORT_SYMBOL_GPL(i2c_new_dummy);
416 /* ------------------------------------------------------------------------- */
418 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
420 static void i2c_adapter_dev_release(struct device *dev)
422 struct i2c_adapter *adap = to_i2c_adapter(dev);
423 complete(&adap->dev_released);
427 * Let users instantiate I2C devices through sysfs. This can be used when
428 * platform initialization code doesn't contain the proper data for
429 * whatever reason. Also useful for drivers that do device detection and
430 * detection fails, either because the device uses an unexpected address,
431 * or this is a compatible device with different ID register values.
433 * Parameter checking may look overzealous, but we really don't want
434 * the user to provide incorrect parameters.
437 i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
438 const char *buf, size_t count)
440 struct i2c_adapter *adap = to_i2c_adapter(dev);
441 struct i2c_board_info info;
442 struct i2c_client *client;
446 dev_warn(dev, "The new_device interface is still experimental "
447 "and may change in a near future\n");
448 memset(&info, 0, sizeof(struct i2c_board_info));
450 blank = strchr(buf, ' ');
452 dev_err(dev, "%s: Missing parameters\n", "new_device");
455 if (blank - buf > I2C_NAME_SIZE - 1) {
456 dev_err(dev, "%s: Invalid device name\n", "new_device");
459 memcpy(info.type, buf, blank - buf);
461 /* Parse remaining parameters, reject extra parameters */
462 res = sscanf(++blank, "%hi%c", &info.addr, &end);
464 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
467 if (res > 1 && end != '\n') {
468 dev_err(dev, "%s: Extra parameters\n", "new_device");
472 if (info.addr < 0x03 || info.addr > 0x77) {
473 dev_err(dev, "%s: Invalid I2C address 0x%hx\n", "new_device",
478 client = i2c_new_device(adap, &info);
482 /* Keep track of the added device */
483 mutex_lock(&core_lock);
484 list_add_tail(&client->detected, &userspace_devices);
485 mutex_unlock(&core_lock);
486 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
487 info.type, info.addr);
493 * And of course let the users delete the devices they instantiated, if
494 * they got it wrong. This interface can only be used to delete devices
495 * instantiated by i2c_sysfs_new_device above. This guarantees that we
496 * don't delete devices to which some kernel code still has references.
498 * Parameter checking may look overzealous, but we really don't want
499 * the user to delete the wrong device.
502 i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
503 const char *buf, size_t count)
505 struct i2c_adapter *adap = to_i2c_adapter(dev);
506 struct i2c_client *client, *next;
511 /* Parse parameters, reject extra parameters */
512 res = sscanf(buf, "%hi%c", &addr, &end);
514 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
517 if (res > 1 && end != '\n') {
518 dev_err(dev, "%s: Extra parameters\n", "delete_device");
522 /* Make sure the device was added through sysfs */
524 mutex_lock(&core_lock);
525 list_for_each_entry_safe(client, next, &userspace_devices, detected) {
526 if (client->addr == addr && client->adapter == adap) {
527 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
528 "delete_device", client->name, client->addr);
530 list_del(&client->detected);
531 i2c_unregister_device(client);
536 mutex_unlock(&core_lock);
539 dev_err(dev, "%s: Can't find device in list\n",
544 static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
545 static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
547 static struct attribute *i2c_adapter_attrs[] = {
549 &dev_attr_new_device.attr,
550 &dev_attr_delete_device.attr,
554 static struct attribute_group i2c_adapter_attr_group = {
555 .attrs = i2c_adapter_attrs,
558 static const struct attribute_group *i2c_adapter_attr_groups[] = {
559 &i2c_adapter_attr_group,
563 static struct device_type i2c_adapter_type = {
564 .groups = i2c_adapter_attr_groups,
565 .release = i2c_adapter_dev_release,
568 #ifdef CONFIG_I2C_COMPAT
569 static struct class_compat *i2c_adapter_compat_class;
572 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
574 struct i2c_devinfo *devinfo;
576 down_read(&__i2c_board_lock);
577 list_for_each_entry(devinfo, &__i2c_board_list, list) {
578 if (devinfo->busnum == adapter->nr
579 && !i2c_new_device(adapter,
580 &devinfo->board_info))
581 dev_err(&adapter->dev,
582 "Can't create device at 0x%02x\n",
583 devinfo->board_info.addr);
585 up_read(&__i2c_board_lock);
588 static int i2c_do_add_adapter(struct device_driver *d, void *data)
590 struct i2c_driver *driver = to_i2c_driver(d);
591 struct i2c_adapter *adap = data;
593 /* Detect supported devices on that bus, and instantiate them */
594 i2c_detect(adap, driver);
596 /* Let legacy drivers scan this bus for matching devices */
597 if (driver->attach_adapter) {
598 /* We ignore the return code; if it fails, too bad */
599 driver->attach_adapter(adap);
604 static int i2c_register_adapter(struct i2c_adapter *adap)
608 /* Can't register until after driver model init */
609 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
614 mutex_init(&adap->bus_lock);
616 /* Set default timeout to 1 second if not already set */
617 if (adap->timeout == 0)
620 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
621 adap->dev.bus = &i2c_bus_type;
622 adap->dev.type = &i2c_adapter_type;
623 res = device_register(&adap->dev);
627 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
629 #ifdef CONFIG_I2C_COMPAT
630 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
634 "Failed to create compatibility class link\n");
637 /* create pre-declared device nodes */
638 if (adap->nr < __i2c_first_dynamic_bus_num)
639 i2c_scan_static_board_info(adap);
642 mutex_lock(&core_lock);
643 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
645 mutex_unlock(&core_lock);
650 mutex_lock(&core_lock);
651 idr_remove(&i2c_adapter_idr, adap->nr);
652 mutex_unlock(&core_lock);
657 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
658 * @adapter: the adapter to add
661 * This routine is used to declare an I2C adapter when its bus number
662 * doesn't matter. Examples: for I2C adapters dynamically added by
663 * USB links or PCI plugin cards.
665 * When this returns zero, a new bus number was allocated and stored
666 * in adap->nr, and the specified adapter became available for clients.
667 * Otherwise, a negative errno value is returned.
669 int i2c_add_adapter(struct i2c_adapter *adapter)
674 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
677 mutex_lock(&core_lock);
678 /* "above" here means "above or equal to", sigh */
679 res = idr_get_new_above(&i2c_adapter_idr, adapter,
680 __i2c_first_dynamic_bus_num, &id);
681 mutex_unlock(&core_lock);
690 return i2c_register_adapter(adapter);
692 EXPORT_SYMBOL(i2c_add_adapter);
695 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
696 * @adap: the adapter to register (with adap->nr initialized)
699 * This routine is used to declare an I2C adapter when its bus number
700 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
701 * or otherwise built in to the system's mainboard, and where i2c_board_info
702 * is used to properly configure I2C devices.
704 * If no devices have pre-been declared for this bus, then be sure to
705 * register the adapter before any dynamically allocated ones. Otherwise
706 * the required bus ID may not be available.
708 * When this returns zero, the specified adapter became available for
709 * clients using the bus number provided in adap->nr. Also, the table
710 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
711 * and the appropriate driver model device nodes are created. Otherwise, a
712 * negative errno value is returned.
714 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
719 if (adap->nr & ~MAX_ID_MASK)
723 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
726 mutex_lock(&core_lock);
727 /* "above" here means "above or equal to", sigh;
728 * we need the "equal to" result to force the result
730 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
731 if (status == 0 && id != adap->nr) {
733 idr_remove(&i2c_adapter_idr, id);
735 mutex_unlock(&core_lock);
736 if (status == -EAGAIN)
740 status = i2c_register_adapter(adap);
743 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
745 static int i2c_do_del_adapter(struct device_driver *d, void *data)
747 struct i2c_driver *driver = to_i2c_driver(d);
748 struct i2c_adapter *adapter = data;
749 struct i2c_client *client, *_n;
752 /* Remove the devices we created ourselves as the result of hardware
753 * probing (using a driver's detect method) */
754 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
755 if (client->adapter == adapter) {
756 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
757 client->name, client->addr);
758 list_del(&client->detected);
759 i2c_unregister_device(client);
763 if (!driver->detach_adapter)
765 res = driver->detach_adapter(adapter);
767 dev_err(&adapter->dev, "detach_adapter failed (%d) "
768 "for driver [%s]\n", res, driver->driver.name);
772 static int __unregister_client(struct device *dev, void *dummy)
774 struct i2c_client *client = i2c_verify_client(dev);
776 i2c_unregister_device(client);
781 * i2c_del_adapter - unregister I2C adapter
782 * @adap: the adapter being unregistered
785 * This unregisters an I2C adapter which was previously registered
786 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
788 int i2c_del_adapter(struct i2c_adapter *adap)
791 struct i2c_adapter *found;
792 struct i2c_client *client, *next;
794 /* First make sure that this adapter was ever added */
795 mutex_lock(&core_lock);
796 found = idr_find(&i2c_adapter_idr, adap->nr);
797 mutex_unlock(&core_lock);
799 pr_debug("i2c-core: attempting to delete unregistered "
800 "adapter [%s]\n", adap->name);
804 /* Tell drivers about this removal */
805 mutex_lock(&core_lock);
806 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
808 mutex_unlock(&core_lock);
812 /* Remove devices instantiated from sysfs */
813 list_for_each_entry_safe(client, next, &userspace_devices, detected) {
814 if (client->adapter == adap) {
815 dev_dbg(&adap->dev, "Removing %s at 0x%x\n",
816 client->name, client->addr);
817 list_del(&client->detected);
818 i2c_unregister_device(client);
822 /* Detach any active clients. This can't fail, thus we do not
823 checking the returned value. */
824 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
826 #ifdef CONFIG_I2C_COMPAT
827 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
831 /* device name is gone after device_unregister */
832 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
834 /* clean up the sysfs representation */
835 init_completion(&adap->dev_released);
836 device_unregister(&adap->dev);
838 /* wait for sysfs to drop all references */
839 wait_for_completion(&adap->dev_released);
842 mutex_lock(&core_lock);
843 idr_remove(&i2c_adapter_idr, adap->nr);
844 mutex_unlock(&core_lock);
846 /* Clear the device structure in case this adapter is ever going to be
848 memset(&adap->dev, 0, sizeof(adap->dev));
852 EXPORT_SYMBOL(i2c_del_adapter);
855 /* ------------------------------------------------------------------------- */
857 static int __attach_adapter(struct device *dev, void *data)
859 struct i2c_adapter *adapter;
860 struct i2c_driver *driver = data;
862 if (dev->type != &i2c_adapter_type)
864 adapter = to_i2c_adapter(dev);
866 i2c_detect(adapter, driver);
868 /* Legacy drivers scan i2c busses directly */
869 if (driver->attach_adapter)
870 driver->attach_adapter(adapter);
876 * An i2c_driver is used with one or more i2c_client (device) nodes to access
877 * i2c slave chips, on a bus instance associated with some i2c_adapter.
880 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
884 /* Can't register until after driver model init */
885 if (unlikely(WARN_ON(!i2c_bus_type.p)))
888 /* add the driver to the list of i2c drivers in the driver core */
889 driver->driver.owner = owner;
890 driver->driver.bus = &i2c_bus_type;
892 /* When registration returns, the driver core
893 * will have called probe() for all matching-but-unbound devices.
895 res = driver_register(&driver->driver);
899 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
901 INIT_LIST_HEAD(&driver->clients);
902 /* Walk the adapters that are already present */
903 mutex_lock(&core_lock);
904 bus_for_each_dev(&i2c_bus_type, NULL, driver, __attach_adapter);
905 mutex_unlock(&core_lock);
909 EXPORT_SYMBOL(i2c_register_driver);
911 static int __detach_adapter(struct device *dev, void *data)
913 struct i2c_adapter *adapter;
914 struct i2c_driver *driver = data;
915 struct i2c_client *client, *_n;
917 if (dev->type != &i2c_adapter_type)
919 adapter = to_i2c_adapter(dev);
921 /* Remove the devices we created ourselves as the result of hardware
922 * probing (using a driver's detect method) */
923 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
924 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
925 client->name, client->addr);
926 list_del(&client->detected);
927 i2c_unregister_device(client);
930 if (driver->detach_adapter) {
931 if (driver->detach_adapter(adapter))
932 dev_err(&adapter->dev,
933 "detach_adapter failed for driver [%s]\n",
934 driver->driver.name);
941 * i2c_del_driver - unregister I2C driver
942 * @driver: the driver being unregistered
945 void i2c_del_driver(struct i2c_driver *driver)
947 mutex_lock(&core_lock);
948 bus_for_each_dev(&i2c_bus_type, NULL, driver, __detach_adapter);
949 mutex_unlock(&core_lock);
951 driver_unregister(&driver->driver);
952 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
954 EXPORT_SYMBOL(i2c_del_driver);
956 /* ------------------------------------------------------------------------- */
958 static int __i2c_check_addr(struct device *dev, void *addrp)
960 struct i2c_client *client = i2c_verify_client(dev);
961 int addr = *(int *)addrp;
963 if (client && client->addr == addr)
968 static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
970 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
972 /* ddl@rock-chips.com : Devices which have some i2c addr can work in same i2c bus,
973 if devices havn't work at the same time.*/
979 static int __i2c_check_addr_ex(struct device *dev, void *addrp)
981 struct i2c_client *client = i2c_verify_client(dev);
982 struct i2c_addr_cnt *addrinfo = (struct i2c_addr_cnt *)addrp;
983 int addr = addrinfo->addr;
985 if (client && client->addr == addr) {
990 static int i2c_check_addr_ex(struct i2c_adapter *adapter, int addr)
992 struct i2c_addr_cnt addrinfo;
994 addrinfo.addr = addr;
996 device_for_each_child(&adapter->dev, &addrinfo, __i2c_check_addr_ex);
1001 * i2c_use_client - increments the reference count of the i2c client structure
1002 * @client: the client being referenced
1004 * Each live reference to a client should be refcounted. The driver model does
1005 * that automatically as part of driver binding, so that most drivers don't
1006 * need to do this explicitly: they hold a reference until they're unbound
1009 * A pointer to the client with the incremented reference counter is returned.
1011 struct i2c_client *i2c_use_client(struct i2c_client *client)
1013 if (client && get_device(&client->dev))
1017 EXPORT_SYMBOL(i2c_use_client);
1020 * i2c_release_client - release a use of the i2c client structure
1021 * @client: the client being no longer referenced
1023 * Must be called when a user of a client is finished with it.
1025 void i2c_release_client(struct i2c_client *client)
1028 put_device(&client->dev);
1030 EXPORT_SYMBOL(i2c_release_client);
1032 struct i2c_cmd_arg {
1037 static int i2c_cmd(struct device *dev, void *_arg)
1039 struct i2c_client *client = i2c_verify_client(dev);
1040 struct i2c_cmd_arg *arg = _arg;
1042 if (client && client->driver && client->driver->command)
1043 client->driver->command(client, arg->cmd, arg->arg);
1047 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1049 struct i2c_cmd_arg cmd_arg;
1053 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
1055 EXPORT_SYMBOL(i2c_clients_command);
1057 static int __init i2c_init(void)
1061 retval = bus_register(&i2c_bus_type);
1064 #ifdef CONFIG_I2C_COMPAT
1065 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1066 if (!i2c_adapter_compat_class) {
1071 retval = i2c_add_driver(&dummy_driver);
1074 #ifdef CONFIG_I2C_DEV_RK29
1075 init_completion(&i2c_dev_complete);
1081 #ifdef CONFIG_I2C_COMPAT
1082 class_compat_unregister(i2c_adapter_compat_class);
1085 bus_unregister(&i2c_bus_type);
1089 static void __exit i2c_exit(void)
1091 i2c_del_driver(&dummy_driver);
1092 #ifdef CONFIG_I2C_COMPAT
1093 class_compat_unregister(i2c_adapter_compat_class);
1095 bus_unregister(&i2c_bus_type);
1098 /* We must initialize early, because some subsystems register i2c drivers
1099 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1101 postcore_initcall(i2c_init);
1102 module_exit(i2c_exit);
1104 /* ----------------------------------------------------
1105 * the functional interface to the i2c busses.
1106 * ----------------------------------------------------
1110 * i2c_transfer - execute a single or combined I2C message
1111 * @adap: Handle to I2C bus
1112 * @msgs: One or more messages to execute before STOP is issued to
1113 * terminate the operation; each message begins with a START.
1114 * @num: Number of messages to be executed.
1116 * Returns negative errno, else the number of messages executed.
1118 * Note that there is no requirement that each message be sent to
1119 * the same slave address, although that is the most common model.
1121 int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1123 unsigned long orig_jiffies;
1126 /* REVISIT the fault reporting model here is weak:
1128 * - When we get an error after receiving N bytes from a slave,
1129 * there is no way to report "N".
1131 * - When we get a NAK after transmitting N bytes to a slave,
1132 * there is no way to report "N" ... or to let the master
1133 * continue executing the rest of this combined message, if
1134 * that's the appropriate response.
1136 * - When for example "num" is two and we successfully complete
1137 * the first message but get an error part way through the
1138 * second, it's unclear whether that should be reported as
1139 * one (discarding status on the second message) or errno
1140 * (discarding status on the first one).
1143 if (adap->algo->master_xfer) {
1145 for (ret = 0; ret < num; ret++) {
1146 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
1147 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1148 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1149 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
1152 #if defined (CONFIG_I2C_RK2818) || defined(CONFIG_I2C_RK29)
1153 if (!(i2c_suspended(adap)) && (in_atomic() || irqs_disabled())) {
1155 if (in_atomic() || irqs_disabled()) {
1158 ret = mutex_trylock(&adap->bus_lock);
1160 /* I2C activity is ongoing. */
1163 mutex_lock_nested(&adap->bus_lock, adap->level);
1166 /* Retry automatically on arbitration loss */
1167 orig_jiffies = jiffies;
1168 #ifdef CONFIG_I2C_DEV_RK29
1169 i2c_dev_dump_start(adap, msgs, num);
1171 for (ret = 0, try = 0; try <= adap->retries; try++) {
1172 ret = adap->algo->master_xfer(adap, msgs, num);
1175 if (time_after(jiffies, orig_jiffies + adap->timeout))
1178 #ifdef CONFIG_I2C_DEV_RK29
1179 i2c_dev_dump_stop(adap, msgs, num ,ret);
1181 mutex_unlock(&adap->bus_lock);
1185 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
1189 EXPORT_SYMBOL(i2c_transfer);
1190 #if defined (CONFIG_I2C_RK2818) || defined(CONFIG_I2C_RK29)
1191 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
1194 struct i2c_adapter *adap=client->adapter;
1197 msg.addr = client->addr;
1198 msg.flags = client->flags;
1200 msg.buf = (char *)buf;
1201 msg.scl_rate = 100 * 1000;
1202 msg.udelay = client->udelay;
1204 ret = i2c_transfer(adap, &msg, 1);
1205 return (ret == 1) ? count : ret;
1207 EXPORT_SYMBOL(i2c_master_send);
1209 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1211 struct i2c_adapter *adap=client->adapter;
1215 msg.addr = client->addr;
1216 msg.flags = client->flags | I2C_M_RD;
1218 msg.buf = (char *)buf;
1219 msg.scl_rate = 400 * 1000;
1220 msg.udelay = client->udelay;
1222 ret = i2c_transfer(adap, &msg, 1);
1224 return (ret == 1) ? count : ret;
1226 EXPORT_SYMBOL(i2c_master_recv);
1228 int i2c_master_normal_send(struct i2c_client *client,const char *buf ,int count, int scl_rate)
1231 struct i2c_adapter *adap=client->adapter;
1234 msg.addr = client->addr;
1235 msg.flags = client->flags;
1237 msg.buf = (char *)buf;
1238 msg.scl_rate = scl_rate;
1239 msg.udelay = client->udelay;
1241 ret = i2c_transfer(adap, &msg, 1);
1242 return (ret == 1) ? count : ret;
1244 EXPORT_SYMBOL(i2c_master_normal_send);
1246 int i2c_master_normal_recv(struct i2c_client *client, char *buf ,int count, int scl_rate)
1248 struct i2c_adapter *adap=client->adapter;
1252 msg.addr = client->addr;
1253 msg.flags = client->flags | I2C_M_RD;
1255 msg.buf = (char *)buf;
1256 msg.scl_rate = scl_rate;
1257 msg.udelay = client->udelay;
1259 ret = i2c_transfer(adap, &msg, 1);
1261 return (ret == 1) ? count : ret;
1263 EXPORT_SYMBOL(i2c_master_normal_recv);
1265 int i2c_master_reg8_send(struct i2c_client *client, const char reg, const char *buf, int count, int scl_rate)
1267 struct i2c_adapter *adap=client->adapter;
1270 char *tx_buf = (char *)kmalloc(count + 1, GFP_KERNEL);
1274 memcpy(tx_buf+1, buf, count);
1276 msg.addr = client->addr;
1277 msg.flags = client->flags;
1278 msg.len = count + 1;
1279 msg.buf = (char *)tx_buf;
1280 msg.scl_rate = scl_rate;
1281 msg.udelay = client->udelay;
1283 ret = i2c_transfer(adap, &msg, 1);
1285 return (ret == 1) ? count : ret;
1288 EXPORT_SYMBOL(i2c_master_reg8_send);
1290 int i2c_master_reg8_recv(struct i2c_client *client, const char reg, char *buf, int count, int scl_rate)
1292 struct i2c_adapter *adap=client->adapter;
1293 struct i2c_msg msgs[2];
1297 msgs[0].addr = client->addr;
1298 msgs[0].flags = client->flags;
1300 msgs[0].buf = ®_buf;
1301 msgs[0].scl_rate = scl_rate;
1302 msgs[0].udelay = client->udelay;
1304 msgs[1].addr = client->addr;
1305 msgs[1].flags = client->flags | I2C_M_RD;
1306 msgs[1].len = count;
1307 msgs[1].buf = (char *)buf;
1308 msgs[1].scl_rate = scl_rate;
1309 msgs[1].udelay = client->udelay;
1311 ret = i2c_transfer(adap, msgs, 2);
1313 return (ret == 2)? count : ret;
1316 EXPORT_SYMBOL(i2c_master_reg8_recv);
1318 int i2c_master_reg8_direct_send(struct i2c_client *client, const char reg, const char *buf, int count, int scl_rate)
1320 return i2c_master_reg8_send(client, reg, buf, count, scl_rate);
1322 EXPORT_SYMBOL(i2c_master_reg8_direct_send);
1324 int i2c_master_reg8_direct_recv(struct i2c_client *client, const char reg, char *buf, int count, int scl_rate)
1326 struct i2c_adapter *adap=client->adapter;
1329 char tx_buf[count+1];
1332 msg.addr = client->addr;
1333 msg.flags = client->flags | I2C_M_REG8_DIRECT | I2C_M_RD;
1334 msg.len = count + 1;
1336 msg.scl_rate = scl_rate;
1337 msg.udelay = client->udelay;
1339 ret = i2c_transfer(adap, &msg, 1);
1340 memcpy(buf, tx_buf + 1, count);
1341 return (ret == 1) ? count : ret;
1343 EXPORT_SYMBOL(i2c_master_reg8_direct_recv);
1345 int i2c_master_reg16_send(struct i2c_client *client, const short regs, const short *buf, int count, int scl_rate)
1347 struct i2c_adapter *adap=client->adapter;
1350 char *tx_buf = (char *)kmalloc(2 * (count + 1), GFP_KERNEL);
1353 memcpy(tx_buf, ®s, 2);
1354 memcpy(tx_buf+2, (char *)buf, count * 2);
1356 msg.addr = client->addr;
1357 msg.flags = client->flags;
1358 msg.len = 2 * (count + 1);
1359 msg.buf = (char *)tx_buf;
1360 msg.scl_rate = scl_rate;
1361 msg.udelay = client->udelay;
1363 ret = i2c_transfer(adap, &msg, 1);
1365 return (ret == 1) ? count : ret;
1367 EXPORT_SYMBOL(i2c_master_reg16_send);
1369 int i2c_master_reg16_recv(struct i2c_client *client, const short regs, short *buf, int count, int scl_rate)
1371 struct i2c_adapter *adap=client->adapter;
1372 struct i2c_msg msgs[2];
1376 memcpy(reg_buf, ®s, 2);
1378 msgs[0].addr = client->addr;
1379 msgs[0].flags = client->flags;
1381 msgs[0].buf = reg_buf;
1382 msgs[0].scl_rate = scl_rate;
1383 msgs[0].udelay = client->udelay;
1385 msgs[1].addr = client->addr;
1386 msgs[1].flags = client->flags | I2C_M_RD;
1387 msgs[1].len = count * 2;
1388 msgs[1].buf = (char *)buf;
1389 msgs[1].scl_rate = scl_rate;
1390 msgs[1].udelay = client->udelay;
1392 ret = i2c_transfer(adap, msgs, 2);
1394 return (ret == 2)? count : ret;
1396 EXPORT_SYMBOL(i2c_master_reg16_recv);
1400 * i2c_master_send - issue a single I2C message in master transmit mode
1401 * @client: Handle to slave device
1402 * @buf: Data that will be written to the slave
1403 * @count: How many bytes to write
1405 * Returns negative errno, or else the number of bytes written.
1407 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
1410 struct i2c_adapter *adap=client->adapter;
1413 msg.addr = client->addr;
1414 msg.flags = client->flags & I2C_M_TEN;
1416 msg.buf = (char *)buf;
1418 ret = i2c_transfer(adap, &msg, 1);
1420 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1421 transmitted, else error code. */
1422 return (ret == 1) ? count : ret;
1424 EXPORT_SYMBOL(i2c_master_send);
1427 * i2c_master_recv - issue a single I2C message in master receive mode
1428 * @client: Handle to slave device
1429 * @buf: Where to store data read from slave
1430 * @count: How many bytes to read
1432 * Returns negative errno, or else the number of bytes read.
1434 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1436 struct i2c_adapter *adap=client->adapter;
1440 msg.addr = client->addr;
1441 msg.flags = client->flags & I2C_M_TEN;
1442 msg.flags |= I2C_M_RD;
1446 ret = i2c_transfer(adap, &msg, 1);
1448 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1449 transmitted, else error code. */
1450 return (ret == 1) ? count : ret;
1452 EXPORT_SYMBOL(i2c_master_recv);
1454 /* ----------------------------------------------------
1455 * the i2c address scanning function
1456 * Will not work for 10-bit addresses!
1457 * ----------------------------------------------------
1460 static int i2c_detect_address(struct i2c_client *temp_client, int kind,
1461 struct i2c_driver *driver)
1463 struct i2c_board_info info;
1464 struct i2c_adapter *adapter = temp_client->adapter;
1465 int addr = temp_client->addr;
1468 /* Make sure the address is valid */
1469 if (addr < 0x03 || addr > 0x77) {
1470 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1475 /* Skip if already in use */
1476 if (i2c_check_addr(adapter, addr))
1479 /* Make sure there is something at this address, unless forced */
1481 if (addr == 0x73 && (adapter->class & I2C_CLASS_HWMON)) {
1482 /* Special probe for FSC hwmon chips */
1483 union i2c_smbus_data dummy;
1485 if (i2c_smbus_xfer(adapter, addr, 0, I2C_SMBUS_READ, 0,
1486 I2C_SMBUS_BYTE_DATA, &dummy) < 0)
1489 if (i2c_smbus_xfer(adapter, addr, 0, I2C_SMBUS_WRITE, 0,
1490 I2C_SMBUS_QUICK, NULL) < 0)
1493 /* Prevent 24RF08 corruption */
1494 if ((addr & ~0x0f) == 0x50)
1495 i2c_smbus_xfer(adapter, addr, 0,
1497 I2C_SMBUS_QUICK, NULL);
1501 /* Finally call the custom detection function */
1502 memset(&info, 0, sizeof(struct i2c_board_info));
1504 err = driver->detect(temp_client, kind, &info);
1506 /* -ENODEV is returned if the detection fails. We catch it
1507 here as this isn't an error. */
1508 return err == -ENODEV ? 0 : err;
1511 /* Consistency check */
1512 if (info.type[0] == '\0') {
1513 dev_err(&adapter->dev, "%s detection function provided "
1514 "no name for 0x%x\n", driver->driver.name,
1517 struct i2c_client *client;
1519 /* Detection succeeded, instantiate the device */
1520 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1521 info.type, info.addr);
1522 client = i2c_new_device(adapter, &info);
1524 list_add_tail(&client->detected, &driver->clients);
1526 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1527 info.type, info.addr);
1532 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1534 const struct i2c_client_address_data *address_data;
1535 struct i2c_client *temp_client;
1537 int adap_id = i2c_adapter_id(adapter);
1539 address_data = driver->address_data;
1540 if (!driver->detect || !address_data)
1543 /* Set up a temporary client to help detect callback */
1544 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1547 temp_client->adapter = adapter;
1549 /* Force entries are done first, and are not affected by ignore
1551 if (address_data->forces) {
1552 const unsigned short * const *forces = address_data->forces;
1555 for (kind = 0; forces[kind]; kind++) {
1556 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1558 if (forces[kind][i] == adap_id
1559 || forces[kind][i] == ANY_I2C_BUS) {
1560 dev_dbg(&adapter->dev, "found force "
1561 "parameter for adapter %d, "
1562 "addr 0x%02x, kind %d\n",
1563 adap_id, forces[kind][i + 1],
1565 temp_client->addr = forces[kind][i + 1];
1566 err = i2c_detect_address(temp_client,
1575 /* Stop here if the classes do not match */
1576 if (!(adapter->class & driver->class))
1579 /* Stop here if we can't use SMBUS_QUICK */
1580 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1581 if (address_data->probe[0] == I2C_CLIENT_END
1582 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1585 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1586 "can't probe for chips\n");
1591 /* Probe entries are done second, and are not affected by ignore
1593 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1594 if (address_data->probe[i] == adap_id
1595 || address_data->probe[i] == ANY_I2C_BUS) {
1596 dev_dbg(&adapter->dev, "found probe parameter for "
1597 "adapter %d, addr 0x%02x\n", adap_id,
1598 address_data->probe[i + 1]);
1599 temp_client->addr = address_data->probe[i + 1];
1600 err = i2c_detect_address(temp_client, -1, driver);
1606 /* Normal entries are done last, unless shadowed by an ignore entry */
1607 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1611 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1613 if ((address_data->ignore[j] == adap_id ||
1614 address_data->ignore[j] == ANY_I2C_BUS)
1615 && address_data->ignore[j + 1]
1616 == address_data->normal_i2c[i]) {
1617 dev_dbg(&adapter->dev, "found ignore "
1618 "parameter for adapter %d, "
1619 "addr 0x%02x\n", adap_id,
1620 address_data->ignore[j + 1]);
1628 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1629 "addr 0x%02x\n", adap_id,
1630 address_data->normal_i2c[i]);
1631 temp_client->addr = address_data->normal_i2c[i];
1632 err = i2c_detect_address(temp_client, -1, driver);
1643 i2c_new_probed_device(struct i2c_adapter *adap,
1644 struct i2c_board_info *info,
1645 unsigned short const *addr_list)
1649 /* Stop here if the bus doesn't support probing */
1650 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1651 dev_err(&adap->dev, "Probing not supported\n");
1655 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1656 /* Check address validity */
1657 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1658 dev_warn(&adap->dev, "Invalid 7-bit address "
1659 "0x%02x\n", addr_list[i]);
1663 /* Check address availability */
1664 if (i2c_check_addr(adap, addr_list[i])) {
1665 dev_dbg(&adap->dev, "Address 0x%02x already in "
1666 "use, not probing\n", addr_list[i]);
1670 /* Test address responsiveness
1671 The default probe method is a quick write, but it is known
1672 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1673 and could also irreversibly write-protect some EEPROMs, so
1674 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1675 read instead. Also, some bus drivers don't implement
1676 quick write, so we fallback to a byte read it that case
1678 if ((addr_list[i] & ~0x07) == 0x30
1679 || (addr_list[i] & ~0x0f) == 0x50
1680 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1681 union i2c_smbus_data data;
1683 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1685 I2C_SMBUS_BYTE, &data) >= 0)
1688 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1690 I2C_SMBUS_QUICK, NULL) >= 0)
1695 if (addr_list[i] == I2C_CLIENT_END) {
1696 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1700 info->addr = addr_list[i];
1701 return i2c_new_device(adap, info);
1703 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1705 struct i2c_adapter* i2c_get_adapter(int id)
1707 struct i2c_adapter *adapter;
1709 mutex_lock(&core_lock);
1710 adapter = idr_find(&i2c_adapter_idr, id);
1711 if (adapter && !try_module_get(adapter->owner))
1714 mutex_unlock(&core_lock);
1717 EXPORT_SYMBOL(i2c_get_adapter);
1719 void i2c_put_adapter(struct i2c_adapter *adap)
1721 module_put(adap->owner);
1723 EXPORT_SYMBOL(i2c_put_adapter);
1725 /* The SMBus parts */
1727 #define POLY (0x1070U << 3)
1728 static u8 crc8(u16 data)
1732 for(i = 0; i < 8; i++) {
1737 return (u8)(data >> 8);
1740 /* Incremental CRC8 over count bytes in the array pointed to by p */
1741 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1745 for(i = 0; i < count; i++)
1746 crc = crc8((crc ^ p[i]) << 8);
1750 /* Assume a 7-bit address, which is reasonable for SMBus */
1751 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1753 /* The address will be sent first */
1754 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1755 pec = i2c_smbus_pec(pec, &addr, 1);
1757 /* The data buffer follows */
1758 return i2c_smbus_pec(pec, msg->buf, msg->len);
1761 /* Used for write only transactions */
1762 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1764 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1768 /* Return <0 on CRC error
1769 If there was a write before this read (most cases) we need to take the
1770 partial CRC from the write part into account.
1771 Note that this function does modify the message (we need to decrease the
1772 message length to hide the CRC byte from the caller). */
1773 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1775 u8 rpec = msg->buf[--msg->len];
1776 cpec = i2c_smbus_msg_pec(cpec, msg);
1779 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1787 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1788 * @client: Handle to slave device
1790 * This executes the SMBus "receive byte" protocol, returning negative errno
1791 * else the byte received from the device.
1793 s32 i2c_smbus_read_byte(struct i2c_client *client)
1795 union i2c_smbus_data data;
1798 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1800 I2C_SMBUS_BYTE, &data);
1801 return (status < 0) ? status : data.byte;
1803 EXPORT_SYMBOL(i2c_smbus_read_byte);
1806 * i2c_smbus_write_byte - SMBus "send byte" protocol
1807 * @client: Handle to slave device
1808 * @value: Byte to be sent
1810 * This executes the SMBus "send byte" protocol, returning negative errno
1811 * else zero on success.
1813 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1815 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1816 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1818 EXPORT_SYMBOL(i2c_smbus_write_byte);
1821 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1822 * @client: Handle to slave device
1823 * @command: Byte interpreted by slave
1825 * This executes the SMBus "read byte" protocol, returning negative errno
1826 * else a data byte received from the device.
1828 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1830 union i2c_smbus_data data;
1833 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1834 I2C_SMBUS_READ, command,
1835 I2C_SMBUS_BYTE_DATA, &data);
1836 return (status < 0) ? status : data.byte;
1838 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1841 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1842 * @client: Handle to slave device
1843 * @command: Byte interpreted by slave
1844 * @value: Byte being written
1846 * This executes the SMBus "write byte" protocol, returning negative errno
1847 * else zero on success.
1849 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1851 union i2c_smbus_data data;
1853 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1854 I2C_SMBUS_WRITE,command,
1855 I2C_SMBUS_BYTE_DATA,&data);
1857 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1860 * i2c_smbus_read_word_data - SMBus "read word" protocol
1861 * @client: Handle to slave device
1862 * @command: Byte interpreted by slave
1864 * This executes the SMBus "read word" protocol, returning negative errno
1865 * else a 16-bit unsigned "word" received from the device.
1867 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1869 union i2c_smbus_data data;
1872 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1873 I2C_SMBUS_READ, command,
1874 I2C_SMBUS_WORD_DATA, &data);
1875 return (status < 0) ? status : data.word;
1877 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1880 * i2c_smbus_write_word_data - SMBus "write word" protocol
1881 * @client: Handle to slave device
1882 * @command: Byte interpreted by slave
1883 * @value: 16-bit "word" being written
1885 * This executes the SMBus "write word" protocol, returning negative errno
1886 * else zero on success.
1888 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1890 union i2c_smbus_data data;
1892 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1893 I2C_SMBUS_WRITE,command,
1894 I2C_SMBUS_WORD_DATA,&data);
1896 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1899 * i2c_smbus_process_call - SMBus "process call" protocol
1900 * @client: Handle to slave device
1901 * @command: Byte interpreted by slave
1902 * @value: 16-bit "word" being written
1904 * This executes the SMBus "process call" protocol, returning negative errno
1905 * else a 16-bit unsigned "word" received from the device.
1907 s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1909 union i2c_smbus_data data;
1913 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1914 I2C_SMBUS_WRITE, command,
1915 I2C_SMBUS_PROC_CALL, &data);
1916 return (status < 0) ? status : data.word;
1918 EXPORT_SYMBOL(i2c_smbus_process_call);
1921 * i2c_smbus_read_block_data - SMBus "block read" protocol
1922 * @client: Handle to slave device
1923 * @command: Byte interpreted by slave
1924 * @values: Byte array into which data will be read; big enough to hold
1925 * the data returned by the slave. SMBus allows at most 32 bytes.
1927 * This executes the SMBus "block read" protocol, returning negative errno
1928 * else the number of data bytes in the slave's response.
1930 * Note that using this function requires that the client's adapter support
1931 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1932 * support this; its emulation through I2C messaging relies on a specific
1933 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1935 s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1938 union i2c_smbus_data data;
1941 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1942 I2C_SMBUS_READ, command,
1943 I2C_SMBUS_BLOCK_DATA, &data);
1947 memcpy(values, &data.block[1], data.block[0]);
1948 return data.block[0];
1950 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1953 * i2c_smbus_write_block_data - SMBus "block write" protocol
1954 * @client: Handle to slave device
1955 * @command: Byte interpreted by slave
1956 * @length: Size of data block; SMBus allows at most 32 bytes
1957 * @values: Byte array which will be written.
1959 * This executes the SMBus "block write" protocol, returning negative errno
1960 * else zero on success.
1962 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1963 u8 length, const u8 *values)
1965 union i2c_smbus_data data;
1967 if (length > I2C_SMBUS_BLOCK_MAX)
1968 length = I2C_SMBUS_BLOCK_MAX;
1969 data.block[0] = length;
1970 memcpy(&data.block[1], values, length);
1971 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1972 I2C_SMBUS_WRITE,command,
1973 I2C_SMBUS_BLOCK_DATA,&data);
1975 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1977 /* Returns the number of read bytes */
1978 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1979 u8 length, u8 *values)
1981 union i2c_smbus_data data;
1984 if (length > I2C_SMBUS_BLOCK_MAX)
1985 length = I2C_SMBUS_BLOCK_MAX;
1986 data.block[0] = length;
1987 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1988 I2C_SMBUS_READ, command,
1989 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1993 memcpy(values, &data.block[1], data.block[0]);
1994 return data.block[0];
1996 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1998 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1999 u8 length, const u8 *values)
2001 union i2c_smbus_data data;
2003 if (length > I2C_SMBUS_BLOCK_MAX)
2004 length = I2C_SMBUS_BLOCK_MAX;
2005 data.block[0] = length;
2006 memcpy(data.block + 1, values, length);
2007 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2008 I2C_SMBUS_WRITE, command,
2009 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2011 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
2013 /* Simulate a SMBus command using the i2c protocol
2014 No checking of parameters is done! */
2015 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
2016 unsigned short flags,
2017 char read_write, u8 command, int size,
2018 union i2c_smbus_data * data)
2020 /* So we need to generate a series of msgs. In the case of writing, we
2021 need to use only one message; when reading, we need two. We initialize
2022 most things with sane defaults, to keep the code below somewhat
2024 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2025 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
2026 int num = read_write == I2C_SMBUS_READ?2:1;
2027 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 ,100000, 0, 0},
2028 { addr, flags | I2C_M_RD, 0, msgbuf1 ,100000, 0, 0}
2034 msgbuf0[0] = command;
2036 case I2C_SMBUS_QUICK:
2038 /* Special case: The read/write field is used as data */
2039 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2043 case I2C_SMBUS_BYTE:
2044 if (read_write == I2C_SMBUS_READ) {
2045 /* Special case: only a read! */
2046 msg[0].flags = I2C_M_RD | flags;
2050 case I2C_SMBUS_BYTE_DATA:
2051 if (read_write == I2C_SMBUS_READ)
2055 msgbuf0[1] = data->byte;
2058 case I2C_SMBUS_WORD_DATA:
2059 if (read_write == I2C_SMBUS_READ)
2063 msgbuf0[1] = data->word & 0xff;
2064 msgbuf0[2] = data->word >> 8;
2067 case I2C_SMBUS_PROC_CALL:
2068 num = 2; /* Special case */
2069 read_write = I2C_SMBUS_READ;
2072 msgbuf0[1] = data->word & 0xff;
2073 msgbuf0[2] = data->word >> 8;
2075 case I2C_SMBUS_BLOCK_DATA:
2076 if (read_write == I2C_SMBUS_READ) {
2077 msg[1].flags |= I2C_M_RECV_LEN;
2078 msg[1].len = 1; /* block length will be added by
2079 the underlying bus driver */
2081 msg[0].len = data->block[0] + 2;
2082 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
2083 dev_err(&adapter->dev,
2084 "Invalid block write size %d\n",
2088 for (i = 1; i < msg[0].len; i++)
2089 msgbuf0[i] = data->block[i-1];
2092 case I2C_SMBUS_BLOCK_PROC_CALL:
2093 num = 2; /* Another special case */
2094 read_write = I2C_SMBUS_READ;
2095 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
2096 dev_err(&adapter->dev,
2097 "Invalid block write size %d\n",
2101 msg[0].len = data->block[0] + 2;
2102 for (i = 1; i < msg[0].len; i++)
2103 msgbuf0[i] = data->block[i-1];
2104 msg[1].flags |= I2C_M_RECV_LEN;
2105 msg[1].len = 1; /* block length will be added by
2106 the underlying bus driver */
2108 case I2C_SMBUS_I2C_BLOCK_DATA:
2109 if (read_write == I2C_SMBUS_READ) {
2110 msg[1].len = data->block[0];
2112 msg[0].len = data->block[0] + 1;
2113 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
2114 dev_err(&adapter->dev,
2115 "Invalid block write size %d\n",
2119 for (i = 1; i <= data->block[0]; i++)
2120 msgbuf0[i] = data->block[i];
2124 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2128 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2129 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2131 /* Compute PEC if first message is a write */
2132 if (!(msg[0].flags & I2C_M_RD)) {
2133 if (num == 1) /* Write only */
2134 i2c_smbus_add_pec(&msg[0]);
2135 else /* Write followed by read */
2136 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2138 /* Ask for PEC if last message is a read */
2139 if (msg[num-1].flags & I2C_M_RD)
2143 status = i2c_transfer(adapter, msg, num);
2147 /* Check PEC if last message is a read */
2148 if (i && (msg[num-1].flags & I2C_M_RD)) {
2149 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2154 if (read_write == I2C_SMBUS_READ)
2156 case I2C_SMBUS_BYTE:
2157 data->byte = msgbuf0[0];
2159 case I2C_SMBUS_BYTE_DATA:
2160 data->byte = msgbuf1[0];
2162 case I2C_SMBUS_WORD_DATA:
2163 case I2C_SMBUS_PROC_CALL:
2164 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2166 case I2C_SMBUS_I2C_BLOCK_DATA:
2167 for (i = 0; i < data->block[0]; i++)
2168 data->block[i+1] = msgbuf1[i];
2170 case I2C_SMBUS_BLOCK_DATA:
2171 case I2C_SMBUS_BLOCK_PROC_CALL:
2172 for (i = 0; i < msgbuf1[0] + 1; i++)
2173 data->block[i] = msgbuf1[i];
2180 * i2c_smbus_xfer - execute SMBus protocol operations
2181 * @adapter: Handle to I2C bus
2182 * @addr: Address of SMBus slave on that bus
2183 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2184 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2185 * @command: Byte interpreted by slave, for protocols which use such bytes
2186 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2187 * @data: Data to be read or written
2189 * This executes an SMBus protocol operation, and returns a negative
2190 * errno code else zero on success.
2192 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
2193 char read_write, u8 command, int protocol,
2194 union i2c_smbus_data *data)
2196 unsigned long orig_jiffies;
2200 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
2202 if (adapter->algo->smbus_xfer) {
2203 mutex_lock(&adapter->bus_lock);
2205 /* Retry automatically on arbitration loss */
2206 orig_jiffies = jiffies;
2207 for (res = 0, try = 0; try <= adapter->retries; try++) {
2208 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2209 read_write, command,
2213 if (time_after(jiffies,
2214 orig_jiffies + adapter->timeout))
2217 mutex_unlock(&adapter->bus_lock);
2219 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
2220 command, protocol, data);
2224 EXPORT_SYMBOL(i2c_smbus_xfer);
2226 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2227 MODULE_DESCRIPTION("I2C-Bus main module");
2228 MODULE_LICENSE("GPL");