aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGrant Likely <grant.likely@secretlab.ca>2010-06-08 09:48:18 -0400
committerGrant Likely <grant.likely@secretlab.ca>2010-07-05 18:14:52 -0400
commit9fd049927ccba1c1d0343239b82f28c4e07fb95d (patch)
treee7d86e42e2643e1376c102f00c0e76d14cf65f86
parent4f0ddcb020ef8afae65b4edb9aeb4a42ab74f4cf (diff)
of/i2c: Generalize OF support
This patch cleans up the i2c OF support code to make it selectable by all architectures and allow for automatic registration of i2c devices. Signed-off-by: Grant Likely <grant.likely@secretlab.ca>
-rw-r--r--drivers/i2c/busses/i2c-cpm.c3
-rw-r--r--drivers/i2c/busses/i2c-ibm_iic.c3
-rw-r--r--drivers/i2c/busses/i2c-mpc.c3
-rw-r--r--drivers/of/Kconfig2
-rw-r--r--drivers/of/of_i2c.c50
-rw-r--r--include/linux/of_i2c.h13
6 files changed, 46 insertions, 28 deletions
diff --git a/drivers/i2c/busses/i2c-cpm.c b/drivers/i2c/busses/i2c-cpm.c
index b02b4533651d..03ae62e69596 100644
--- a/drivers/i2c/busses/i2c-cpm.c
+++ b/drivers/i2c/busses/i2c-cpm.c
@@ -652,6 +652,7 @@ static int __devinit cpm_i2c_probe(struct of_device *ofdev,
652 cpm->adap = cpm_ops; 652 cpm->adap = cpm_ops;
653 i2c_set_adapdata(&cpm->adap, cpm); 653 i2c_set_adapdata(&cpm->adap, cpm);
654 cpm->adap.dev.parent = &ofdev->dev; 654 cpm->adap.dev.parent = &ofdev->dev;
655 cpm->adap.dev.of_node = of_node_get(ofdev->dev.of_node);
655 656
656 result = cpm_i2c_setup(cpm); 657 result = cpm_i2c_setup(cpm);
657 if (result) { 658 if (result) {
@@ -679,7 +680,7 @@ static int __devinit cpm_i2c_probe(struct of_device *ofdev,
679 /* 680 /*
680 * register OF I2C devices 681 * register OF I2C devices
681 */ 682 */
682 of_register_i2c_devices(&cpm->adap, ofdev->dev.of_node); 683 of_i2c_register_devices(&cpm->adap);
683 684
684 return 0; 685 return 0;
685out_shut: 686out_shut:
diff --git a/drivers/i2c/busses/i2c-ibm_iic.c b/drivers/i2c/busses/i2c-ibm_iic.c
index bf344135647a..d9641210dd3a 100644
--- a/drivers/i2c/busses/i2c-ibm_iic.c
+++ b/drivers/i2c/busses/i2c-ibm_iic.c
@@ -745,6 +745,7 @@ static int __devinit iic_probe(struct of_device *ofdev,
745 /* Register it with i2c layer */ 745 /* Register it with i2c layer */
746 adap = &dev->adap; 746 adap = &dev->adap;
747 adap->dev.parent = &ofdev->dev; 747 adap->dev.parent = &ofdev->dev;
748 adap->dev.of_node = of_node_get(np);
748 strlcpy(adap->name, "IBM IIC", sizeof(adap->name)); 749 strlcpy(adap->name, "IBM IIC", sizeof(adap->name));
749 i2c_set_adapdata(adap, dev); 750 i2c_set_adapdata(adap, dev);
750 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD; 751 adap->class = I2C_CLASS_HWMON | I2C_CLASS_SPD;
@@ -761,7 +762,7 @@ static int __devinit iic_probe(struct of_device *ofdev,
761 dev->fast_mode ? "fast (400 kHz)" : "standard (100 kHz)"); 762 dev->fast_mode ? "fast (400 kHz)" : "standard (100 kHz)");
762 763
763 /* Now register all the child nodes */ 764 /* Now register all the child nodes */
764 of_register_i2c_devices(adap, np); 765 of_i2c_register_devices(adap);
765 766
766 return 0; 767 return 0;
767 768
diff --git a/drivers/i2c/busses/i2c-mpc.c b/drivers/i2c/busses/i2c-mpc.c
index df00eb1f11f9..d2e26d290e7a 100644
--- a/drivers/i2c/busses/i2c-mpc.c
+++ b/drivers/i2c/busses/i2c-mpc.c
@@ -600,13 +600,14 @@ static int __devinit fsl_i2c_probe(struct of_device *op,
600 i2c->adap = mpc_ops; 600 i2c->adap = mpc_ops;
601 i2c_set_adapdata(&i2c->adap, i2c); 601 i2c_set_adapdata(&i2c->adap, i2c);
602 i2c->adap.dev.parent = &op->dev; 602 i2c->adap.dev.parent = &op->dev;
603 i2c->adap.dev.of_node = of_node_get(op->dev.of_node);
603 604
604 result = i2c_add_adapter(&i2c->adap); 605 result = i2c_add_adapter(&i2c->adap);
605 if (result < 0) { 606 if (result < 0) {
606 dev_err(i2c->dev, "failed to add adapter\n"); 607 dev_err(i2c->dev, "failed to add adapter\n");
607 goto fail_add; 608 goto fail_add;
608 } 609 }
609 of_register_i2c_devices(&i2c->adap, op->dev.of_node); 610 of_i2c_register_devices(&i2c->adap);
610 611
611 return result; 612 return result;
612 613
diff --git a/drivers/of/Kconfig b/drivers/of/Kconfig
index 097f42aebe90..80dd6318db68 100644
--- a/drivers/of/Kconfig
+++ b/drivers/of/Kconfig
@@ -26,7 +26,7 @@ config OF_GPIO
26 26
27config OF_I2C 27config OF_I2C
28 def_tristate I2C 28 def_tristate I2C
29 depends on (PPC_OF || MICROBLAZE) && I2C 29 depends on OF && !SPARC && I2C
30 help 30 help
31 OpenFirmware I2C accessors 31 OpenFirmware I2C accessors
32 32
diff --git a/drivers/of/of_i2c.c b/drivers/of/of_i2c.c
index ab6522c8e4fe..0a694debd226 100644
--- a/drivers/of/of_i2c.c
+++ b/drivers/of/of_i2c.c
@@ -14,57 +14,65 @@
14#include <linux/i2c.h> 14#include <linux/i2c.h>
15#include <linux/of.h> 15#include <linux/of.h>
16#include <linux/of_i2c.h> 16#include <linux/of_i2c.h>
17#include <linux/of_irq.h>
17#include <linux/module.h> 18#include <linux/module.h>
18 19
19void of_register_i2c_devices(struct i2c_adapter *adap, 20void of_i2c_register_devices(struct i2c_adapter *adap)
20 struct device_node *adap_node)
21{ 21{
22 void *result; 22 void *result;
23 struct device_node *node; 23 struct device_node *node;
24 24
25 for_each_child_of_node(adap_node, node) { 25 /* Only register child devices if the adapter has a node pointer set */
26 if (!adap->dev.of_node)
27 return;
28
29 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
30
31 for_each_child_of_node(adap->dev.of_node, node) {
26 struct i2c_board_info info = {}; 32 struct i2c_board_info info = {};
27 struct dev_archdata dev_ad = {}; 33 struct dev_archdata dev_ad = {};
28 const __be32 *addr; 34 const __be32 *addr;
29 int len; 35 int len;
30 36
31 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) 37 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
38
39 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
40 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
41 node->full_name);
32 continue; 42 continue;
43 }
33 44
34 addr = of_get_property(node, "reg", &len); 45 addr = of_get_property(node, "reg", &len);
35 if (!addr || len < sizeof(int) || *addr > (1 << 10) - 1) { 46 if (!addr || (len < sizeof(int))) {
36 printk(KERN_ERR 47 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
37 "of-i2c: invalid i2c device entry\n"); 48 node->full_name);
38 continue; 49 continue;
39 } 50 }
40 51
41 info.irq = irq_of_parse_and_map(node, 0);
42
43 info.addr = be32_to_cpup(addr); 52 info.addr = be32_to_cpup(addr);
53 if (info.addr > (1 << 10) - 1) {
54 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
55 info.addr, node->full_name);
56 continue;
57 }
44 58
45 info.of_node = node; 59 info.irq = irq_of_parse_and_map(node, 0);
60 info.of_node = of_node_get(node);
46 info.archdata = &dev_ad; 61 info.archdata = &dev_ad;
47 62
48 request_module("%s", info.type); 63 request_module("%s", info.type);
49 64
50 result = i2c_new_device(adap, &info); 65 result = i2c_new_device(adap, &info);
51 if (result == NULL) { 66 if (result == NULL) {
52 printk(KERN_ERR 67 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
53 "of-i2c: Failed to load driver for %s\n", 68 node->full_name);
54 info.type); 69 of_node_put(node);
55 irq_dispose_mapping(info.irq); 70 irq_dispose_mapping(info.irq);
56 continue; 71 continue;
57 } 72 }
58
59 /*
60 * Get the node to not lose the dev_archdata->of_node.
61 * Currently there is no way to put it back, as well as no
62 * of_unregister_i2c_devices() call.
63 */
64 of_node_get(node);
65 } 73 }
66} 74}
67EXPORT_SYMBOL(of_register_i2c_devices); 75EXPORT_SYMBOL(of_i2c_register_devices);
68 76
69static int of_dev_node_match(struct device *dev, void *data) 77static int of_dev_node_match(struct device *dev, void *data)
70{ 78{
diff --git a/include/linux/of_i2c.h b/include/linux/of_i2c.h
index 34974b5a76f7..0efe8d465f55 100644
--- a/include/linux/of_i2c.h
+++ b/include/linux/of_i2c.h
@@ -12,12 +12,19 @@
12#ifndef __LINUX_OF_I2C_H 12#ifndef __LINUX_OF_I2C_H
13#define __LINUX_OF_I2C_H 13#define __LINUX_OF_I2C_H
14 14
15#if defined(CONFIG_OF_I2C) || defined(CONFIG_OF_I2C_MODULE)
15#include <linux/i2c.h> 16#include <linux/i2c.h>
16 17
17void of_register_i2c_devices(struct i2c_adapter *adap, 18extern void of_i2c_register_devices(struct i2c_adapter *adap);
18 struct device_node *adap_node);
19 19
20/* must call put_device() when done with returned i2c_client device */ 20/* must call put_device() when done with returned i2c_client device */
21struct i2c_client *of_find_i2c_device_by_node(struct device_node *node); 21extern struct i2c_client *of_find_i2c_device_by_node(struct device_node *node);
22
23#else
24static inline void of_i2c_register_devices(struct i2c_adapter *adap)
25{
26 return;
27}
28#endif /* CONFIG_OF_I2C */
22 29
23#endif /* __LINUX_OF_I2C_H */ 30#endif /* __LINUX_OF_I2C_H */