aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c/i2c-core.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/i2c/i2c-core.c')
-rw-r--r--drivers/i2c/i2c-core.c167
1 files changed, 127 insertions, 40 deletions
diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index 0815e10da7c6..6649176de940 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -20,7 +20,9 @@
20/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>. 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> 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 22 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
23 Jean Delvare <khali@linux-fr.org> */ 23 Jean Delvare <khali@linux-fr.org>
24 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
25 Michael Lawnick <michael.lawnick.ext@nsn.com> */
24 26
25#include <linux/module.h> 27#include <linux/module.h>
26#include <linux/kernel.h> 28#include <linux/kernel.h>
@@ -30,6 +32,8 @@
30#include <linux/init.h> 32#include <linux/init.h>
31#include <linux/idr.h> 33#include <linux/idr.h>
32#include <linux/mutex.h> 34#include <linux/mutex.h>
35#include <linux/of_i2c.h>
36#include <linux/of_device.h>
33#include <linux/completion.h> 37#include <linux/completion.h>
34#include <linux/hardirq.h> 38#include <linux/hardirq.h>
35#include <linux/irqflags.h> 39#include <linux/irqflags.h>
@@ -70,6 +74,10 @@ static int i2c_device_match(struct device *dev, struct device_driver *drv)
70 if (!client) 74 if (!client)
71 return 0; 75 return 0;
72 76
77 /* Attempt an OF style match */
78 if (of_driver_match_device(dev, drv))
79 return 1;
80
73 driver = to_i2c_driver(drv); 81 driver = to_i2c_driver(drv);
74 /* match on an id table if there is one */ 82 /* match on an id table if there is one */
75 if (driver->id_table) 83 if (driver->id_table)
@@ -417,11 +425,87 @@ static int __i2c_check_addr_busy(struct device *dev, void *addrp)
417 return 0; 425 return 0;
418} 426}
419 427
428/* walk up mux tree */
429static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
430{
431 int result;
432
433 result = device_for_each_child(&adapter->dev, &addr,
434 __i2c_check_addr_busy);
435
436 if (!result && i2c_parent_is_i2c_adapter(adapter))
437 result = i2c_check_mux_parents(
438 to_i2c_adapter(adapter->dev.parent), addr);
439
440 return result;
441}
442
443/* recurse down mux tree */
444static int i2c_check_mux_children(struct device *dev, void *addrp)
445{
446 int result;
447
448 if (dev->type == &i2c_adapter_type)
449 result = device_for_each_child(dev, addrp,
450 i2c_check_mux_children);
451 else
452 result = __i2c_check_addr_busy(dev, addrp);
453
454 return result;
455}
456
420static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr) 457static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
421{ 458{
422 return device_for_each_child(&adapter->dev, &addr, 459 int result = 0;
423 __i2c_check_addr_busy); 460
461 if (i2c_parent_is_i2c_adapter(adapter))
462 result = i2c_check_mux_parents(
463 to_i2c_adapter(adapter->dev.parent), addr);
464
465 if (!result)
466 result = device_for_each_child(&adapter->dev, &addr,
467 i2c_check_mux_children);
468
469 return result;
470}
471
472/**
473 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
474 * @adapter: Target I2C bus segment
475 */
476void i2c_lock_adapter(struct i2c_adapter *adapter)
477{
478 if (i2c_parent_is_i2c_adapter(adapter))
479 i2c_lock_adapter(to_i2c_adapter(adapter->dev.parent));
480 else
481 rt_mutex_lock(&adapter->bus_lock);
482}
483EXPORT_SYMBOL_GPL(i2c_lock_adapter);
484
485/**
486 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
487 * @adapter: Target I2C bus segment
488 */
489static int i2c_trylock_adapter(struct i2c_adapter *adapter)
490{
491 if (i2c_parent_is_i2c_adapter(adapter))
492 return i2c_trylock_adapter(to_i2c_adapter(adapter->dev.parent));
493 else
494 return rt_mutex_trylock(&adapter->bus_lock);
495}
496
497/**
498 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
499 * @adapter: Target I2C bus segment
500 */
501void i2c_unlock_adapter(struct i2c_adapter *adapter)
502{
503 if (i2c_parent_is_i2c_adapter(adapter))
504 i2c_unlock_adapter(to_i2c_adapter(adapter->dev.parent));
505 else
506 rt_mutex_unlock(&adapter->bus_lock);
424} 507}
508EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
425 509
426/** 510/**
427 * i2c_new_device - instantiate an i2c device 511 * i2c_new_device - instantiate an i2c device
@@ -627,9 +711,9 @@ i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
627 return -EINVAL; 711 return -EINVAL;
628 712
629 /* Keep track of the added device */ 713 /* Keep track of the added device */
630 i2c_lock_adapter(adap); 714 mutex_lock(&adap->userspace_clients_lock);
631 list_add_tail(&client->detected, &adap->userspace_clients); 715 list_add_tail(&client->detected, &adap->userspace_clients);
632 i2c_unlock_adapter(adap); 716 mutex_unlock(&adap->userspace_clients_lock);
633 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device", 717 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
634 info.type, info.addr); 718 info.type, info.addr);
635 719
@@ -668,7 +752,7 @@ i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
668 752
669 /* Make sure the device was added through sysfs */ 753 /* Make sure the device was added through sysfs */
670 res = -ENOENT; 754 res = -ENOENT;
671 i2c_lock_adapter(adap); 755 mutex_lock(&adap->userspace_clients_lock);
672 list_for_each_entry_safe(client, next, &adap->userspace_clients, 756 list_for_each_entry_safe(client, next, &adap->userspace_clients,
673 detected) { 757 detected) {
674 if (client->addr == addr) { 758 if (client->addr == addr) {
@@ -681,7 +765,7 @@ i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
681 break; 765 break;
682 } 766 }
683 } 767 }
684 i2c_unlock_adapter(adap); 768 mutex_unlock(&adap->userspace_clients_lock);
685 769
686 if (res < 0) 770 if (res < 0)
687 dev_err(dev, "%s: Can't find device in list\n", 771 dev_err(dev, "%s: Can't find device in list\n",
@@ -708,10 +792,11 @@ static const struct attribute_group *i2c_adapter_attr_groups[] = {
708 NULL 792 NULL
709}; 793};
710 794
711static struct device_type i2c_adapter_type = { 795struct device_type i2c_adapter_type = {
712 .groups = i2c_adapter_attr_groups, 796 .groups = i2c_adapter_attr_groups,
713 .release = i2c_adapter_dev_release, 797 .release = i2c_adapter_dev_release,
714}; 798};
799EXPORT_SYMBOL_GPL(i2c_adapter_type);
715 800
716#ifdef CONFIG_I2C_COMPAT 801#ifdef CONFIG_I2C_COMPAT
717static struct class_compat *i2c_adapter_compat_class; 802static struct class_compat *i2c_adapter_compat_class;
@@ -754,7 +839,7 @@ static int __process_new_adapter(struct device_driver *d, void *data)
754 839
755static int i2c_register_adapter(struct i2c_adapter *adap) 840static int i2c_register_adapter(struct i2c_adapter *adap)
756{ 841{
757 int res = 0, dummy; 842 int res = 0;
758 843
759 /* Can't register until after driver model init */ 844 /* Can't register until after driver model init */
760 if (unlikely(WARN_ON(!i2c_bus_type.p))) { 845 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
@@ -763,6 +848,7 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
763 } 848 }
764 849
765 rt_mutex_init(&adap->bus_lock); 850 rt_mutex_init(&adap->bus_lock);
851 mutex_init(&adap->userspace_clients_lock);
766 INIT_LIST_HEAD(&adap->userspace_clients); 852 INIT_LIST_HEAD(&adap->userspace_clients);
767 853
768 /* Set default timeout to 1 second if not already set */ 854 /* Set default timeout to 1 second if not already set */
@@ -790,10 +876,12 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
790 if (adap->nr < __i2c_first_dynamic_bus_num) 876 if (adap->nr < __i2c_first_dynamic_bus_num)
791 i2c_scan_static_board_info(adap); 877 i2c_scan_static_board_info(adap);
792 878
879 /* Register devices from the device tree */
880 of_i2c_register_devices(adap);
881
793 /* Notify drivers */ 882 /* Notify drivers */
794 mutex_lock(&core_lock); 883 mutex_lock(&core_lock);
795 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap, 884 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
796 __process_new_adapter);
797 mutex_unlock(&core_lock); 885 mutex_unlock(&core_lock);
798 886
799 return 0; 887 return 0;
@@ -966,7 +1054,7 @@ int i2c_del_adapter(struct i2c_adapter *adap)
966 return res; 1054 return res;
967 1055
968 /* Remove devices instantiated from sysfs */ 1056 /* Remove devices instantiated from sysfs */
969 i2c_lock_adapter(adap); 1057 mutex_lock(&adap->userspace_clients_lock);
970 list_for_each_entry_safe(client, next, &adap->userspace_clients, 1058 list_for_each_entry_safe(client, next, &adap->userspace_clients,
971 detected) { 1059 detected) {
972 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name, 1060 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
@@ -974,7 +1062,7 @@ int i2c_del_adapter(struct i2c_adapter *adap)
974 list_del(&client->detected); 1062 list_del(&client->detected);
975 i2c_unregister_device(client); 1063 i2c_unregister_device(client);
976 } 1064 }
977 i2c_unlock_adapter(adap); 1065 mutex_unlock(&adap->userspace_clients_lock);
978 1066
979 /* Detach any active clients. This can't fail, thus we do not 1067 /* Detach any active clients. This can't fail, thus we do not
980 checking the returned value. */ 1068 checking the returned value. */
@@ -1229,12 +1317,12 @@ int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1229#endif 1317#endif
1230 1318
1231 if (in_atomic() || irqs_disabled()) { 1319 if (in_atomic() || irqs_disabled()) {
1232 ret = rt_mutex_trylock(&adap->bus_lock); 1320 ret = i2c_trylock_adapter(adap);
1233 if (!ret) 1321 if (!ret)
1234 /* I2C activity is ongoing. */ 1322 /* I2C activity is ongoing. */
1235 return -EAGAIN; 1323 return -EAGAIN;
1236 } else { 1324 } else {
1237 rt_mutex_lock(&adap->bus_lock); 1325 i2c_lock_adapter(adap);
1238 } 1326 }
1239 1327
1240 /* Retry automatically on arbitration loss */ 1328 /* Retry automatically on arbitration loss */
@@ -1246,7 +1334,7 @@ int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1246 if (time_after(jiffies, orig_jiffies + adap->timeout)) 1334 if (time_after(jiffies, orig_jiffies + adap->timeout))
1247 break; 1335 break;
1248 } 1336 }
1249 rt_mutex_unlock(&adap->bus_lock); 1337 i2c_unlock_adapter(adap);
1250 1338
1251 return ret; 1339 return ret;
1252 } else { 1340 } else {
@@ -1341,13 +1429,17 @@ static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1341 I2C_SMBUS_BYTE_DATA, &dummy); 1429 I2C_SMBUS_BYTE_DATA, &dummy);
1342 else 1430 else
1343#endif 1431#endif
1344 if ((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50 1432 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
1345 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) 1433 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
1346 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1347 I2C_SMBUS_BYTE, &dummy);
1348 else
1349 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0, 1434 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1350 I2C_SMBUS_QUICK, NULL); 1435 I2C_SMBUS_QUICK, NULL);
1436 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
1437 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1438 I2C_SMBUS_BYTE, &dummy);
1439 else {
1440 dev_warn(&adap->dev, "No suitable probing method supported\n");
1441 err = -EOPNOTSUPP;
1442 }
1351 1443
1352 return err >= 0; 1444 return err >= 0;
1353} 1445}
@@ -1428,16 +1520,6 @@ static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1428 if (!(adapter->class & driver->class)) 1520 if (!(adapter->class & driver->class))
1429 goto exit_free; 1521 goto exit_free;
1430 1522
1431 /* Stop here if the bus doesn't support probing */
1432 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE)) {
1433 if (address_list[0] == I2C_CLIENT_END)
1434 goto exit_free;
1435
1436 dev_warn(&adapter->dev, "Probing not supported\n");
1437 err = -EOPNOTSUPP;
1438 goto exit_free;
1439 }
1440
1441 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) { 1523 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
1442 dev_dbg(&adapter->dev, "found normal entry for adapter %d, " 1524 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1443 "addr 0x%02x\n", adap_id, address_list[i]); 1525 "addr 0x%02x\n", adap_id, address_list[i]);
@@ -1452,18 +1534,23 @@ static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1452 return err; 1534 return err;
1453} 1535}
1454 1536
1537int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
1538{
1539 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1540 I2C_SMBUS_QUICK, NULL) >= 0;
1541}
1542EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
1543
1455struct i2c_client * 1544struct i2c_client *
1456i2c_new_probed_device(struct i2c_adapter *adap, 1545i2c_new_probed_device(struct i2c_adapter *adap,
1457 struct i2c_board_info *info, 1546 struct i2c_board_info *info,
1458 unsigned short const *addr_list) 1547 unsigned short const *addr_list,
1548 int (*probe)(struct i2c_adapter *, unsigned short addr))
1459{ 1549{
1460 int i; 1550 int i;
1461 1551
1462 /* Stop here if the bus doesn't support probing */ 1552 if (!probe)
1463 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) { 1553 probe = i2c_default_probe;
1464 dev_err(&adap->dev, "Probing not supported\n");
1465 return NULL;
1466 }
1467 1554
1468 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) { 1555 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1469 /* Check address validity */ 1556 /* Check address validity */
@@ -1481,7 +1568,7 @@ i2c_new_probed_device(struct i2c_adapter *adap,
1481 } 1568 }
1482 1569
1483 /* Test address responsiveness */ 1570 /* Test address responsiveness */
1484 if (i2c_default_probe(adap, addr_list[i])) 1571 if (probe(adap, addr_list[i]))
1485 break; 1572 break;
1486 } 1573 }
1487 1574
@@ -1993,7 +2080,7 @@ s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
1993 flags &= I2C_M_TEN | I2C_CLIENT_PEC; 2080 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1994 2081
1995 if (adapter->algo->smbus_xfer) { 2082 if (adapter->algo->smbus_xfer) {
1996 rt_mutex_lock(&adapter->bus_lock); 2083 i2c_lock_adapter(adapter);
1997 2084
1998 /* Retry automatically on arbitration loss */ 2085 /* Retry automatically on arbitration loss */
1999 orig_jiffies = jiffies; 2086 orig_jiffies = jiffies;
@@ -2007,7 +2094,7 @@ s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
2007 orig_jiffies + adapter->timeout)) 2094 orig_jiffies + adapter->timeout))
2008 break; 2095 break;
2009 } 2096 }
2010 rt_mutex_unlock(&adapter->bus_lock); 2097 i2c_unlock_adapter(adapter);
2011 } else 2098 } else
2012 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write, 2099 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2013 command, protocol, data); 2100 command, protocol, data);