aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2010-08-12 13:06:29 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2010-08-12 13:06:29 -0400
commit1c00650c233c404fadf9347329214d55283bd17a (patch)
tree9de7cda2fe2e54411cc24ff3befd758c9cb9cd74 /drivers/i2c
parent26df0766a73a859bb93dc58e747c5028557a23fd (diff)
parent7f528135da9704d67db1f727162024b078e1cd8f (diff)
Merge branch 'i2c-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jdelvare/staging
* 'i2c-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jdelvare/staging: i2c: I2C bus multiplexer driver pca954x i2c: Multiplexed I2C bus core support i2c: Use a separate mutex for userspace client lists i2c: Make i2c_default_probe self-sufficient i2c: Drop dummy variable i2c: Move adapter locking helpers to i2c-core V4L/DVB: Use custom I2C probing function mechanism i2c: Add support for custom probe function i2c-dev: Use memdup_user i2c-dev: Remove unnecessary kmalloc casts
Diffstat (limited to 'drivers/i2c')
-rw-r--r--drivers/i2c/Kconfig13
-rw-r--r--drivers/i2c/Makefile3
-rw-r--r--drivers/i2c/i2c-core.c158
-rw-r--r--drivers/i2c/i2c-dev.c66
-rw-r--r--drivers/i2c/i2c-mux.c165
-rw-r--r--drivers/i2c/muxes/Kconfig18
-rw-r--r--drivers/i2c/muxes/Makefile8
-rw-r--r--drivers/i2c/muxes/pca954x.c301
8 files changed, 671 insertions, 61 deletions
diff --git a/drivers/i2c/Kconfig b/drivers/i2c/Kconfig
index d06083fdffbb..30f06e956bfb 100644
--- a/drivers/i2c/Kconfig
+++ b/drivers/i2c/Kconfig
@@ -47,6 +47,19 @@ config I2C_CHARDEV
47 This support is also available as a module. If so, the module 47 This support is also available as a module. If so, the module
48 will be called i2c-dev. 48 will be called i2c-dev.
49 49
50config I2C_MUX
51 tristate "I2C bus multiplexing support"
52 depends on EXPERIMENTAL
53 help
54 Say Y here if you want the I2C core to support the ability to
55 handle multiplexed I2C bus topologies, by presenting each
56 multiplexed segment as a I2C adapter.
57
58 This support is also available as a module. If so, the module
59 will be called i2c-mux.
60
61source drivers/i2c/muxes/Kconfig
62
50config I2C_HELPER_AUTO 63config I2C_HELPER_AUTO
51 bool "Autoselect pertinent helper modules" 64 bool "Autoselect pertinent helper modules"
52 default y 65 default y
diff --git a/drivers/i2c/Makefile b/drivers/i2c/Makefile
index a7d9b4be9bb3..c00fd66388f5 100644
--- a/drivers/i2c/Makefile
+++ b/drivers/i2c/Makefile
@@ -6,7 +6,8 @@ obj-$(CONFIG_I2C_BOARDINFO) += i2c-boardinfo.o
6obj-$(CONFIG_I2C) += i2c-core.o 6obj-$(CONFIG_I2C) += i2c-core.o
7obj-$(CONFIG_I2C_SMBUS) += i2c-smbus.o 7obj-$(CONFIG_I2C_SMBUS) += i2c-smbus.o
8obj-$(CONFIG_I2C_CHARDEV) += i2c-dev.o 8obj-$(CONFIG_I2C_CHARDEV) += i2c-dev.o
9obj-y += algos/ busses/ 9obj-$(CONFIG_I2C_MUX) += i2c-mux.o
10obj-y += algos/ busses/ muxes/
10 11
11ifeq ($(CONFIG_I2C_DEBUG_CORE),y) 12ifeq ($(CONFIG_I2C_DEBUG_CORE),y)
12EXTRA_CFLAGS += -DDEBUG 13EXTRA_CFLAGS += -DDEBUG
diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index df937df845eb..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>
@@ -423,11 +425,87 @@ static int __i2c_check_addr_busy(struct device *dev, void *addrp)
423 return 0; 425 return 0;
424} 426}
425 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
426static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr) 457static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
427{ 458{
428 return device_for_each_child(&adapter->dev, &addr, 459 int result = 0;
429 __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);
430} 507}
508EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
431 509
432/** 510/**
433 * i2c_new_device - instantiate an i2c device 511 * i2c_new_device - instantiate an i2c device
@@ -633,9 +711,9 @@ i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
633 return -EINVAL; 711 return -EINVAL;
634 712
635 /* Keep track of the added device */ 713 /* Keep track of the added device */
636 i2c_lock_adapter(adap); 714 mutex_lock(&adap->userspace_clients_lock);
637 list_add_tail(&client->detected, &adap->userspace_clients); 715 list_add_tail(&client->detected, &adap->userspace_clients);
638 i2c_unlock_adapter(adap); 716 mutex_unlock(&adap->userspace_clients_lock);
639 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",
640 info.type, info.addr); 718 info.type, info.addr);
641 719
@@ -674,7 +752,7 @@ i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
674 752
675 /* Make sure the device was added through sysfs */ 753 /* Make sure the device was added through sysfs */
676 res = -ENOENT; 754 res = -ENOENT;
677 i2c_lock_adapter(adap); 755 mutex_lock(&adap->userspace_clients_lock);
678 list_for_each_entry_safe(client, next, &adap->userspace_clients, 756 list_for_each_entry_safe(client, next, &adap->userspace_clients,
679 detected) { 757 detected) {
680 if (client->addr == addr) { 758 if (client->addr == addr) {
@@ -687,7 +765,7 @@ i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
687 break; 765 break;
688 } 766 }
689 } 767 }
690 i2c_unlock_adapter(adap); 768 mutex_unlock(&adap->userspace_clients_lock);
691 769
692 if (res < 0) 770 if (res < 0)
693 dev_err(dev, "%s: Can't find device in list\n", 771 dev_err(dev, "%s: Can't find device in list\n",
@@ -714,10 +792,11 @@ static const struct attribute_group *i2c_adapter_attr_groups[] = {
714 NULL 792 NULL
715}; 793};
716 794
717static struct device_type i2c_adapter_type = { 795struct device_type i2c_adapter_type = {
718 .groups = i2c_adapter_attr_groups, 796 .groups = i2c_adapter_attr_groups,
719 .release = i2c_adapter_dev_release, 797 .release = i2c_adapter_dev_release,
720}; 798};
799EXPORT_SYMBOL_GPL(i2c_adapter_type);
721 800
722#ifdef CONFIG_I2C_COMPAT 801#ifdef CONFIG_I2C_COMPAT
723static struct class_compat *i2c_adapter_compat_class; 802static struct class_compat *i2c_adapter_compat_class;
@@ -760,7 +839,7 @@ static int __process_new_adapter(struct device_driver *d, void *data)
760 839
761static int i2c_register_adapter(struct i2c_adapter *adap) 840static int i2c_register_adapter(struct i2c_adapter *adap)
762{ 841{
763 int res = 0, dummy; 842 int res = 0;
764 843
765 /* Can't register until after driver model init */ 844 /* Can't register until after driver model init */
766 if (unlikely(WARN_ON(!i2c_bus_type.p))) { 845 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
@@ -769,6 +848,7 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
769 } 848 }
770 849
771 rt_mutex_init(&adap->bus_lock); 850 rt_mutex_init(&adap->bus_lock);
851 mutex_init(&adap->userspace_clients_lock);
772 INIT_LIST_HEAD(&adap->userspace_clients); 852 INIT_LIST_HEAD(&adap->userspace_clients);
773 853
774 /* Set default timeout to 1 second if not already set */ 854 /* Set default timeout to 1 second if not already set */
@@ -801,8 +881,7 @@ static int i2c_register_adapter(struct i2c_adapter *adap)
801 881
802 /* Notify drivers */ 882 /* Notify drivers */
803 mutex_lock(&core_lock); 883 mutex_lock(&core_lock);
804 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap, 884 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
805 __process_new_adapter);
806 mutex_unlock(&core_lock); 885 mutex_unlock(&core_lock);
807 886
808 return 0; 887 return 0;
@@ -975,7 +1054,7 @@ int i2c_del_adapter(struct i2c_adapter *adap)
975 return res; 1054 return res;
976 1055
977 /* Remove devices instantiated from sysfs */ 1056 /* Remove devices instantiated from sysfs */
978 i2c_lock_adapter(adap); 1057 mutex_lock(&adap->userspace_clients_lock);
979 list_for_each_entry_safe(client, next, &adap->userspace_clients, 1058 list_for_each_entry_safe(client, next, &adap->userspace_clients,
980 detected) { 1059 detected) {
981 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,
@@ -983,7 +1062,7 @@ int i2c_del_adapter(struct i2c_adapter *adap)
983 list_del(&client->detected); 1062 list_del(&client->detected);
984 i2c_unregister_device(client); 1063 i2c_unregister_device(client);
985 } 1064 }
986 i2c_unlock_adapter(adap); 1065 mutex_unlock(&adap->userspace_clients_lock);
987 1066
988 /* 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
989 checking the returned value. */ 1068 checking the returned value. */
@@ -1238,12 +1317,12 @@ int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1238#endif 1317#endif
1239 1318
1240 if (in_atomic() || irqs_disabled()) { 1319 if (in_atomic() || irqs_disabled()) {
1241 ret = rt_mutex_trylock(&adap->bus_lock); 1320 ret = i2c_trylock_adapter(adap);
1242 if (!ret) 1321 if (!ret)
1243 /* I2C activity is ongoing. */ 1322 /* I2C activity is ongoing. */
1244 return -EAGAIN; 1323 return -EAGAIN;
1245 } else { 1324 } else {
1246 rt_mutex_lock(&adap->bus_lock); 1325 i2c_lock_adapter(adap);
1247 } 1326 }
1248 1327
1249 /* Retry automatically on arbitration loss */ 1328 /* Retry automatically on arbitration loss */
@@ -1255,7 +1334,7 @@ int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1255 if (time_after(jiffies, orig_jiffies + adap->timeout)) 1334 if (time_after(jiffies, orig_jiffies + adap->timeout))
1256 break; 1335 break;
1257 } 1336 }
1258 rt_mutex_unlock(&adap->bus_lock); 1337 i2c_unlock_adapter(adap);
1259 1338
1260 return ret; 1339 return ret;
1261 } else { 1340 } else {
@@ -1350,13 +1429,17 @@ static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1350 I2C_SMBUS_BYTE_DATA, &dummy); 1429 I2C_SMBUS_BYTE_DATA, &dummy);
1351 else 1430 else
1352#endif 1431#endif
1353 if ((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50 1432 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
1354 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) 1433 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
1355 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1356 I2C_SMBUS_BYTE, &dummy);
1357 else
1358 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0, 1434 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1359 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 }
1360 1443
1361 return err >= 0; 1444 return err >= 0;
1362} 1445}
@@ -1437,16 +1520,6 @@ static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1437 if (!(adapter->class & driver->class)) 1520 if (!(adapter->class & driver->class))
1438 goto exit_free; 1521 goto exit_free;
1439 1522
1440 /* Stop here if the bus doesn't support probing */
1441 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE)) {
1442 if (address_list[0] == I2C_CLIENT_END)
1443 goto exit_free;
1444
1445 dev_warn(&adapter->dev, "Probing not supported\n");
1446 err = -EOPNOTSUPP;
1447 goto exit_free;
1448 }
1449
1450 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) { 1523 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
1451 dev_dbg(&adapter->dev, "found normal entry for adapter %d, " 1524 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1452 "addr 0x%02x\n", adap_id, address_list[i]); 1525 "addr 0x%02x\n", adap_id, address_list[i]);
@@ -1461,18 +1534,23 @@ static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1461 return err; 1534 return err;
1462} 1535}
1463 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
1464struct i2c_client * 1544struct i2c_client *
1465i2c_new_probed_device(struct i2c_adapter *adap, 1545i2c_new_probed_device(struct i2c_adapter *adap,
1466 struct i2c_board_info *info, 1546 struct i2c_board_info *info,
1467 unsigned short const *addr_list) 1547 unsigned short const *addr_list,
1548 int (*probe)(struct i2c_adapter *, unsigned short addr))
1468{ 1549{
1469 int i; 1550 int i;
1470 1551
1471 /* Stop here if the bus doesn't support probing */ 1552 if (!probe)
1472 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) { 1553 probe = i2c_default_probe;
1473 dev_err(&adap->dev, "Probing not supported\n");
1474 return NULL;
1475 }
1476 1554
1477 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) { 1555 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1478 /* Check address validity */ 1556 /* Check address validity */
@@ -1490,7 +1568,7 @@ i2c_new_probed_device(struct i2c_adapter *adap,
1490 } 1568 }
1491 1569
1492 /* Test address responsiveness */ 1570 /* Test address responsiveness */
1493 if (i2c_default_probe(adap, addr_list[i])) 1571 if (probe(adap, addr_list[i]))
1494 break; 1572 break;
1495 } 1573 }
1496 1574
@@ -2002,7 +2080,7 @@ s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
2002 flags &= I2C_M_TEN | I2C_CLIENT_PEC; 2080 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
2003 2081
2004 if (adapter->algo->smbus_xfer) { 2082 if (adapter->algo->smbus_xfer) {
2005 rt_mutex_lock(&adapter->bus_lock); 2083 i2c_lock_adapter(adapter);
2006 2084
2007 /* Retry automatically on arbitration loss */ 2085 /* Retry automatically on arbitration loss */
2008 orig_jiffies = jiffies; 2086 orig_jiffies = jiffies;
@@ -2016,7 +2094,7 @@ s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
2016 orig_jiffies + adapter->timeout)) 2094 orig_jiffies + adapter->timeout))
2017 break; 2095 break;
2018 } 2096 }
2019 rt_mutex_unlock(&adapter->bus_lock); 2097 i2c_unlock_adapter(adapter);
2020 } else 2098 } else
2021 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write, 2099 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2022 command, protocol, data); 2100 command, protocol, data);
diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c
index e0694e4d86c7..5f3a52d517c3 100644
--- a/drivers/i2c/i2c-dev.c
+++ b/drivers/i2c/i2c-dev.c
@@ -167,13 +167,9 @@ static ssize_t i2cdev_write(struct file *file, const char __user *buf,
167 if (count > 8192) 167 if (count > 8192)
168 count = 8192; 168 count = 8192;
169 169
170 tmp = kmalloc(count, GFP_KERNEL); 170 tmp = memdup_user(buf, count);
171 if (tmp == NULL) 171 if (IS_ERR(tmp))
172 return -ENOMEM; 172 return PTR_ERR(tmp);
173 if (copy_from_user(tmp, buf, count)) {
174 kfree(tmp);
175 return -EFAULT;
176 }
177 173
178 pr_debug("i2c-dev: i2c-%d writing %zu bytes.\n", 174 pr_debug("i2c-dev: i2c-%d writing %zu bytes.\n",
179 iminor(file->f_path.dentry->d_inode), count); 175 iminor(file->f_path.dentry->d_inode), count);
@@ -193,12 +189,50 @@ static int i2cdev_check(struct device *dev, void *addrp)
193 return dev->driver ? -EBUSY : 0; 189 return dev->driver ? -EBUSY : 0;
194} 190}
195 191
192/* walk up mux tree */
193static int i2cdev_check_mux_parents(struct i2c_adapter *adapter, int addr)
194{
195 int result;
196
197 result = device_for_each_child(&adapter->dev, &addr, i2cdev_check);
198
199 if (!result && i2c_parent_is_i2c_adapter(adapter))
200 result = i2cdev_check_mux_parents(
201 to_i2c_adapter(adapter->dev.parent), addr);
202
203 return result;
204}
205
206/* recurse down mux tree */
207static int i2cdev_check_mux_children(struct device *dev, void *addrp)
208{
209 int result;
210
211 if (dev->type == &i2c_adapter_type)
212 result = device_for_each_child(dev, addrp,
213 i2cdev_check_mux_children);
214 else
215 result = i2cdev_check(dev, addrp);
216
217 return result;
218}
219
196/* This address checking function differs from the one in i2c-core 220/* This address checking function differs from the one in i2c-core
197 in that it considers an address with a registered device, but no 221 in that it considers an address with a registered device, but no
198 driver bound to it, as NOT busy. */ 222 driver bound to it, as NOT busy. */
199static int i2cdev_check_addr(struct i2c_adapter *adapter, unsigned int addr) 223static int i2cdev_check_addr(struct i2c_adapter *adapter, unsigned int addr)
200{ 224{
201 return device_for_each_child(&adapter->dev, &addr, i2cdev_check); 225 int result = 0;
226
227 if (i2c_parent_is_i2c_adapter(adapter))
228 result = i2cdev_check_mux_parents(
229 to_i2c_adapter(adapter->dev.parent), addr);
230
231 if (!result)
232 result = device_for_each_child(&adapter->dev, &addr,
233 i2cdev_check_mux_children);
234
235 return result;
202} 236}
203 237
204static noinline int i2cdev_ioctl_rdrw(struct i2c_client *client, 238static noinline int i2cdev_ioctl_rdrw(struct i2c_client *client,
@@ -219,9 +253,7 @@ static noinline int i2cdev_ioctl_rdrw(struct i2c_client *client,
219 if (rdwr_arg.nmsgs > I2C_RDRW_IOCTL_MAX_MSGS) 253 if (rdwr_arg.nmsgs > I2C_RDRW_IOCTL_MAX_MSGS)
220 return -EINVAL; 254 return -EINVAL;
221 255
222 rdwr_pa = (struct i2c_msg *) 256 rdwr_pa = kmalloc(rdwr_arg.nmsgs * sizeof(struct i2c_msg), GFP_KERNEL);
223 kmalloc(rdwr_arg.nmsgs * sizeof(struct i2c_msg),
224 GFP_KERNEL);
225 if (!rdwr_pa) 257 if (!rdwr_pa)
226 return -ENOMEM; 258 return -ENOMEM;
227 259
@@ -247,15 +279,9 @@ static noinline int i2cdev_ioctl_rdrw(struct i2c_client *client,
247 break; 279 break;
248 } 280 }
249 data_ptrs[i] = (u8 __user *)rdwr_pa[i].buf; 281 data_ptrs[i] = (u8 __user *)rdwr_pa[i].buf;
250 rdwr_pa[i].buf = kmalloc(rdwr_pa[i].len, GFP_KERNEL); 282 rdwr_pa[i].buf = memdup_user(data_ptrs[i], rdwr_pa[i].len);
251 if (rdwr_pa[i].buf == NULL) { 283 if (IS_ERR(rdwr_pa[i].buf)) {
252 res = -ENOMEM; 284 res = PTR_ERR(rdwr_pa[i].buf);
253 break;
254 }
255 if (copy_from_user(rdwr_pa[i].buf, data_ptrs[i],
256 rdwr_pa[i].len)) {
257 ++i; /* Needs to be kfreed too */
258 res = -EFAULT;
259 break; 285 break;
260 } 286 }
261 } 287 }
diff --git a/drivers/i2c/i2c-mux.c b/drivers/i2c/i2c-mux.c
new file mode 100644
index 000000000000..d32a4843fc3a
--- /dev/null
+++ b/drivers/i2c/i2c-mux.c
@@ -0,0 +1,165 @@
1/*
2 * Multiplexed I2C bus driver.
3 *
4 * Copyright (c) 2008-2009 Rodolfo Giometti <giometti@linux.it>
5 * Copyright (c) 2008-2009 Eurotech S.p.A. <info@eurotech.it>
6 * Copyright (c) 2009-2010 NSN GmbH & Co KG <michael.lawnick.ext@nsn.com>
7 *
8 * Simplifies access to complex multiplexed I2C bus topologies, by presenting
9 * each multiplexed bus segment as an additional I2C adapter.
10 * Supports multi-level mux'ing (mux behind a mux).
11 *
12 * Based on:
13 * i2c-virt.c from Kumar Gala <galak@kernel.crashing.org>
14 * i2c-virtual.c from Ken Harrenstien, Copyright (c) 2004 Google, Inc.
15 * i2c-virtual.c from Brian Kuschak <bkuschak@yahoo.com>
16 *
17 * This file is licensed under the terms of the GNU General Public
18 * License version 2. This program is licensed "as is" without any
19 * warranty of any kind, whether express or implied.
20 */
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/slab.h>
25#include <linux/i2c.h>
26#include <linux/i2c-mux.h>
27
28/* multiplexer per channel data */
29struct i2c_mux_priv {
30 struct i2c_adapter adap;
31 struct i2c_algorithm algo;
32
33 struct i2c_adapter *parent;
34 void *mux_dev; /* the mux chip/device */
35 u32 chan_id; /* the channel id */
36
37 int (*select)(struct i2c_adapter *, void *mux_dev, u32 chan_id);
38 int (*deselect)(struct i2c_adapter *, void *mux_dev, u32 chan_id);
39};
40
41static int i2c_mux_master_xfer(struct i2c_adapter *adap,
42 struct i2c_msg msgs[], int num)
43{
44 struct i2c_mux_priv *priv = adap->algo_data;
45 struct i2c_adapter *parent = priv->parent;
46 int ret;
47
48 /* Switch to the right mux port and perform the transfer. */
49
50 ret = priv->select(parent, priv->mux_dev, priv->chan_id);
51 if (ret >= 0)
52 ret = parent->algo->master_xfer(parent, msgs, num);
53 if (priv->deselect)
54 priv->deselect(parent, priv->mux_dev, priv->chan_id);
55
56 return ret;
57}
58
59static int i2c_mux_smbus_xfer(struct i2c_adapter *adap,
60 u16 addr, unsigned short flags,
61 char read_write, u8 command,
62 int size, union i2c_smbus_data *data)
63{
64 struct i2c_mux_priv *priv = adap->algo_data;
65 struct i2c_adapter *parent = priv->parent;
66 int ret;
67
68 /* Select the right mux port and perform the transfer. */
69
70 ret = priv->select(parent, priv->mux_dev, priv->chan_id);
71 if (ret >= 0)
72 ret = parent->algo->smbus_xfer(parent, addr, flags,
73 read_write, command, size, data);
74 if (priv->deselect)
75 priv->deselect(parent, priv->mux_dev, priv->chan_id);
76
77 return ret;
78}
79
80/* Return the parent's functionality */
81static u32 i2c_mux_functionality(struct i2c_adapter *adap)
82{
83 struct i2c_mux_priv *priv = adap->algo_data;
84 struct i2c_adapter *parent = priv->parent;
85
86 return parent->algo->functionality(parent);
87}
88
89struct i2c_adapter *i2c_add_mux_adapter(struct i2c_adapter *parent,
90 void *mux_dev, u32 force_nr, u32 chan_id,
91 int (*select) (struct i2c_adapter *,
92 void *, u32),
93 int (*deselect) (struct i2c_adapter *,
94 void *, u32))
95{
96 struct i2c_mux_priv *priv;
97 int ret;
98
99 priv = kzalloc(sizeof(struct i2c_mux_priv), GFP_KERNEL);
100 if (!priv)
101 return NULL;
102
103 /* Set up private adapter data */
104 priv->parent = parent;
105 priv->mux_dev = mux_dev;
106 priv->chan_id = chan_id;
107 priv->select = select;
108 priv->deselect = deselect;
109
110 /* Need to do algo dynamically because we don't know ahead
111 * of time what sort of physical adapter we'll be dealing with.
112 */
113 if (parent->algo->master_xfer)
114 priv->algo.master_xfer = i2c_mux_master_xfer;
115 if (parent->algo->smbus_xfer)
116 priv->algo.smbus_xfer = i2c_mux_smbus_xfer;
117 priv->algo.functionality = i2c_mux_functionality;
118
119 /* Now fill out new adapter structure */
120 snprintf(priv->adap.name, sizeof(priv->adap.name),
121 "i2c-%d-mux (chan_id %d)", i2c_adapter_id(parent), chan_id);
122 priv->adap.owner = THIS_MODULE;
123 priv->adap.id = parent->id;
124 priv->adap.algo = &priv->algo;
125 priv->adap.algo_data = priv;
126 priv->adap.dev.parent = &parent->dev;
127
128 if (force_nr) {
129 priv->adap.nr = force_nr;
130 ret = i2c_add_numbered_adapter(&priv->adap);
131 } else {
132 ret = i2c_add_adapter(&priv->adap);
133 }
134 if (ret < 0) {
135 dev_err(&parent->dev,
136 "failed to add mux-adapter (error=%d)\n",
137 ret);
138 kfree(priv);
139 return NULL;
140 }
141
142 dev_info(&parent->dev, "Added multiplexed i2c bus %d\n",
143 i2c_adapter_id(&priv->adap));
144
145 return &priv->adap;
146}
147EXPORT_SYMBOL_GPL(i2c_add_mux_adapter);
148
149int i2c_del_mux_adapter(struct i2c_adapter *adap)
150{
151 struct i2c_mux_priv *priv = adap->algo_data;
152 int ret;
153
154 ret = i2c_del_adapter(adap);
155 if (ret < 0)
156 return ret;
157 kfree(priv);
158
159 return 0;
160}
161EXPORT_SYMBOL_GPL(i2c_del_mux_adapter);
162
163MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
164MODULE_DESCRIPTION("I2C driver for multiplexed I2C busses");
165MODULE_LICENSE("GPL v2");
diff --git a/drivers/i2c/muxes/Kconfig b/drivers/i2c/muxes/Kconfig
new file mode 100644
index 000000000000..4c9a99c4fcb0
--- /dev/null
+++ b/drivers/i2c/muxes/Kconfig
@@ -0,0 +1,18 @@
1#
2# Multiplexer I2C chip drivers configuration
3#
4
5menu "Multiplexer I2C Chip support"
6 depends on I2C_MUX
7
8config I2C_MUX_PCA954x
9 tristate "Philips PCA954x I2C Mux/switches"
10 depends on EXPERIMENTAL
11 help
12 If you say yes here you get support for the Philips PCA954x
13 I2C mux/switch devices.
14
15 This driver can also be built as a module. If so, the module
16 will be called pca954x.
17
18endmenu
diff --git a/drivers/i2c/muxes/Makefile b/drivers/i2c/muxes/Makefile
new file mode 100644
index 000000000000..bd83b5274815
--- /dev/null
+++ b/drivers/i2c/muxes/Makefile
@@ -0,0 +1,8 @@
1#
2# Makefile for multiplexer I2C chip drivers.
3
4obj-$(CONFIG_I2C_MUX_PCA954x) += pca954x.o
5
6ifeq ($(CONFIG_I2C_DEBUG_BUS),y)
7EXTRA_CFLAGS += -DDEBUG
8endif
diff --git a/drivers/i2c/muxes/pca954x.c b/drivers/i2c/muxes/pca954x.c
new file mode 100644
index 000000000000..6f9accf3189d
--- /dev/null
+++ b/drivers/i2c/muxes/pca954x.c
@@ -0,0 +1,301 @@
1/*
2 * I2C multiplexer
3 *
4 * Copyright (c) 2008-2009 Rodolfo Giometti <giometti@linux.it>
5 * Copyright (c) 2008-2009 Eurotech S.p.A. <info@eurotech.it>
6 *
7 * This module supports the PCA954x series of I2C multiplexer/switch chips
8 * made by Philips Semiconductors.
9 * This includes the:
10 * PCA9540, PCA9542, PCA9543, PCA9544, PCA9545, PCA9546, PCA9547
11 * and PCA9548.
12 *
13 * These chips are all controlled via the I2C bus itself, and all have a
14 * single 8-bit register. The upstream "parent" bus fans out to two,
15 * four, or eight downstream busses or channels; which of these
16 * are selected is determined by the chip type and register contents. A
17 * mux can select only one sub-bus at a time; a switch can select any
18 * combination simultaneously.
19 *
20 * Based on:
21 * pca954x.c from Kumar Gala <galak@kernel.crashing.org>
22 * Copyright (C) 2006
23 *
24 * Based on:
25 * pca954x.c from Ken Harrenstien
26 * Copyright (C) 2004 Google, Inc. (Ken Harrenstien)
27 *
28 * Based on:
29 * i2c-virtual_cb.c from Brian Kuschak <bkuschak@yahoo.com>
30 * and
31 * pca9540.c from Jean Delvare <khali@linux-fr.org>.
32 *
33 * This file is licensed under the terms of the GNU General Public
34 * License version 2. This program is licensed "as is" without any
35 * warranty of any kind, whether express or implied.
36 */
37
38#include <linux/module.h>
39#include <linux/init.h>
40#include <linux/slab.h>
41#include <linux/device.h>
42#include <linux/i2c.h>
43#include <linux/i2c-mux.h>
44
45#include <linux/i2c/pca954x.h>
46
47#define PCA954X_MAX_NCHANS 8
48
49enum pca_type {
50 pca_9540,
51 pca_9542,
52 pca_9543,
53 pca_9544,
54 pca_9545,
55 pca_9546,
56 pca_9547,
57 pca_9548,
58};
59
60struct pca954x {
61 enum pca_type type;
62 struct i2c_adapter *virt_adaps[PCA954X_MAX_NCHANS];
63
64 u8 last_chan; /* last register value */
65};
66
67struct chip_desc {
68 u8 nchans;
69 u8 enable; /* used for muxes only */
70 enum muxtype {
71 pca954x_ismux = 0,
72 pca954x_isswi
73 } muxtype;
74};
75
76/* Provide specs for the PCA954x types we know about */
77static const struct chip_desc chips[] = {
78 [pca_9540] = {
79 .nchans = 2,
80 .enable = 0x4,
81 .muxtype = pca954x_ismux,
82 },
83 [pca_9543] = {
84 .nchans = 2,
85 .muxtype = pca954x_isswi,
86 },
87 [pca_9544] = {
88 .nchans = 4,
89 .enable = 0x4,
90 .muxtype = pca954x_ismux,
91 },
92 [pca_9545] = {
93 .nchans = 4,
94 .muxtype = pca954x_isswi,
95 },
96 [pca_9547] = {
97 .nchans = 8,
98 .enable = 0x8,
99 .muxtype = pca954x_ismux,
100 },
101 [pca_9548] = {
102 .nchans = 8,
103 .muxtype = pca954x_isswi,
104 },
105};
106
107static const struct i2c_device_id pca954x_id[] = {
108 { "pca9540", pca_9540 },
109 { "pca9542", pca_9540 },
110 { "pca9543", pca_9543 },
111 { "pca9544", pca_9544 },
112 { "pca9545", pca_9545 },
113 { "pca9546", pca_9545 },
114 { "pca9547", pca_9547 },
115 { "pca9548", pca_9548 },
116 { }
117};
118MODULE_DEVICE_TABLE(i2c, pca954x_id);
119
120/* Write to mux register. Don't use i2c_transfer()/i2c_smbus_xfer()
121 for this as they will try to lock adapter a second time */
122static int pca954x_reg_write(struct i2c_adapter *adap,
123 struct i2c_client *client, u8 val)
124{
125 int ret = -ENODEV;
126
127 if (adap->algo->master_xfer) {
128 struct i2c_msg msg;
129 char buf[1];
130
131 msg.addr = client->addr;
132 msg.flags = 0;
133 msg.len = 1;
134 buf[0] = val;
135 msg.buf = buf;
136 ret = adap->algo->master_xfer(adap, &msg, 1);
137 } else {
138 union i2c_smbus_data data;
139 ret = adap->algo->smbus_xfer(adap, client->addr,
140 client->flags,
141 I2C_SMBUS_WRITE,
142 val, I2C_SMBUS_BYTE, &data);
143 }
144
145 return ret;
146}
147
148static int pca954x_select_chan(struct i2c_adapter *adap,
149 void *client, u32 chan)
150{
151 struct pca954x *data = i2c_get_clientdata(client);
152 const struct chip_desc *chip = &chips[data->type];
153 u8 regval;
154 int ret = 0;
155
156 /* we make switches look like muxes, not sure how to be smarter */
157 if (chip->muxtype == pca954x_ismux)
158 regval = chan | chip->enable;
159 else
160 regval = 1 << chan;
161
162 /* Only select the channel if its different from the last channel */
163 if (data->last_chan != regval) {
164 ret = pca954x_reg_write(adap, client, regval);
165 data->last_chan = regval;
166 }
167
168 return ret;
169}
170
171static int pca954x_deselect_mux(struct i2c_adapter *adap,
172 void *client, u32 chan)
173{
174 struct pca954x *data = i2c_get_clientdata(client);
175
176 /* Deselect active channel */
177 data->last_chan = 0;
178 return pca954x_reg_write(adap, client, data->last_chan);
179}
180
181/*
182 * I2C init/probing/exit functions
183 */
184static int __devinit pca954x_probe(struct i2c_client *client,
185 const struct i2c_device_id *id)
186{
187 struct i2c_adapter *adap = to_i2c_adapter(client->dev.parent);
188 struct pca954x_platform_data *pdata = client->dev.platform_data;
189 int num, force;
190 struct pca954x *data;
191 int ret = -ENODEV;
192
193 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE))
194 goto err;
195
196 data = kzalloc(sizeof(struct pca954x), GFP_KERNEL);
197 if (!data) {
198 ret = -ENOMEM;
199 goto err;
200 }
201
202 i2c_set_clientdata(client, data);
203
204 /* Read the mux register at addr to verify
205 * that the mux is in fact present.
206 */
207 if (i2c_smbus_read_byte(client) < 0) {
208 dev_warn(&client->dev, "probe failed\n");
209 goto exit_free;
210 }
211
212 data->type = id->driver_data;
213 data->last_chan = 0; /* force the first selection */
214
215 /* Now create an adapter for each channel */
216 for (num = 0; num < chips[data->type].nchans; num++) {
217 force = 0; /* dynamic adap number */
218 if (pdata) {
219 if (num < pdata->num_modes)
220 /* force static number */
221 force = pdata->modes[num].adap_id;
222 else
223 /* discard unconfigured channels */
224 break;
225 }
226
227 data->virt_adaps[num] =
228 i2c_add_mux_adapter(adap, client,
229 force, num, pca954x_select_chan,
230 (pdata && pdata->modes[num].deselect_on_exit)
231 ? pca954x_deselect_mux : NULL);
232
233 if (data->virt_adaps[num] == NULL) {
234 ret = -ENODEV;
235 dev_err(&client->dev,
236 "failed to register multiplexed adapter"
237 " %d as bus %d\n", num, force);
238 goto virt_reg_failed;
239 }
240 }
241
242 dev_info(&client->dev,
243 "registered %d multiplexed busses for I2C %s %s\n",
244 num, chips[data->type].muxtype == pca954x_ismux
245 ? "mux" : "switch", client->name);
246
247 return 0;
248
249virt_reg_failed:
250 for (num--; num >= 0; num--)
251 i2c_del_mux_adapter(data->virt_adaps[num]);
252exit_free:
253 kfree(data);
254err:
255 return ret;
256}
257
258static int __devexit pca954x_remove(struct i2c_client *client)
259{
260 struct pca954x *data = i2c_get_clientdata(client);
261 const struct chip_desc *chip = &chips[data->type];
262 int i, err;
263
264 for (i = 0; i < chip->nchans; ++i)
265 if (data->virt_adaps[i]) {
266 err = i2c_del_mux_adapter(data->virt_adaps[i]);
267 if (err)
268 return err;
269 data->virt_adaps[i] = NULL;
270 }
271
272 kfree(data);
273 return 0;
274}
275
276static struct i2c_driver pca954x_driver = {
277 .driver = {
278 .name = "pca954x",
279 .owner = THIS_MODULE,
280 },
281 .probe = pca954x_probe,
282 .remove = __devexit_p(pca954x_remove),
283 .id_table = pca954x_id,
284};
285
286static int __init pca954x_init(void)
287{
288 return i2c_add_driver(&pca954x_driver);
289}
290
291static void __exit pca954x_exit(void)
292{
293 i2c_del_driver(&pca954x_driver);
294}
295
296module_init(pca954x_init);
297module_exit(pca954x_exit);
298
299MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>");
300MODULE_DESCRIPTION("PCA954x I2C mux/switch driver");
301MODULE_LICENSE("GPL v2");