diff options
-rw-r--r-- | drivers/of/Kconfig | 6 | ||||
-rw-r--r-- | drivers/of/Makefile | 1 | ||||
-rw-r--r-- | drivers/of/i2c.c | 115 | ||||
-rw-r--r-- | include/linux/of_i2c.h | 24 |
4 files changed, 146 insertions, 0 deletions
diff --git a/drivers/of/Kconfig b/drivers/of/Kconfig index 3354ad766a49..7c305317f372 100644 --- a/drivers/of/Kconfig +++ b/drivers/of/Kconfig | |||
@@ -7,3 +7,9 @@ config OF_GPIO | |||
7 | depends on OF && PPC_OF && HAVE_GPIO_LIB | 7 | depends on OF && PPC_OF && HAVE_GPIO_LIB |
8 | help | 8 | help |
9 | OpenFirmware GPIO accessors | 9 | OpenFirmware GPIO accessors |
10 | |||
11 | config OF_I2C | ||
12 | def_bool y | ||
13 | depends on OF && I2C | ||
14 | help | ||
15 | OpenFirmware I2C accessors | ||
diff --git a/drivers/of/Makefile b/drivers/of/Makefile index 5a61f70b4027..a07b95362c53 100644 --- a/drivers/of/Makefile +++ b/drivers/of/Makefile | |||
@@ -1,3 +1,4 @@ | |||
1 | obj-y = base.o | 1 | obj-y = base.o |
2 | obj-$(CONFIG_OF_DEVICE) += device.o platform.o | 2 | obj-$(CONFIG_OF_DEVICE) += device.o platform.o |
3 | obj-$(CONFIG_OF_GPIO) += gpio.o | 3 | obj-$(CONFIG_OF_GPIO) += gpio.o |
4 | obj-$(CONFIG_OF_I2C) += i2c.o | ||
diff --git a/drivers/of/i2c.c b/drivers/of/i2c.c new file mode 100644 index 000000000000..631689171159 --- /dev/null +++ b/drivers/of/i2c.c | |||
@@ -0,0 +1,115 @@ | |||
1 | /* | ||
2 | * OF helpers for the I2C API | ||
3 | * | ||
4 | * Copyright (c) 2008 Jochen Friedrich <jochen@scram.de> | ||
5 | * | ||
6 | * Based on a previous patch from Jon Smirl <jonsmirl@gmail.com> | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or modify | ||
9 | * it under the terms of the GNU General Public License as published by | ||
10 | * the Free Software Foundation; either version 2 of the License, or | ||
11 | * (at your option) any later version. | ||
12 | */ | ||
13 | |||
14 | #include <linux/i2c.h> | ||
15 | #include <linux/of.h> | ||
16 | |||
17 | struct i2c_driver_device { | ||
18 | char *of_device; | ||
19 | char *i2c_type; | ||
20 | }; | ||
21 | |||
22 | static struct i2c_driver_device i2c_devices[] = { | ||
23 | { "dallas,ds1374", "rtc-ds1374" }, | ||
24 | }; | ||
25 | |||
26 | static int of_find_i2c_driver(struct device_node *node, | ||
27 | struct i2c_board_info *info) | ||
28 | { | ||
29 | int i, cplen; | ||
30 | const char *compatible; | ||
31 | const char *p; | ||
32 | |||
33 | /* 1. search for exception list entry */ | ||
34 | for (i = 0; i < ARRAY_SIZE(i2c_devices); i++) { | ||
35 | if (!of_device_is_compatible(node, i2c_devices[i].of_device)) | ||
36 | continue; | ||
37 | if (strlcpy(info->type, i2c_devices[i].i2c_type, | ||
38 | I2C_NAME_SIZE) >= I2C_NAME_SIZE) | ||
39 | return -ENOMEM; | ||
40 | |||
41 | return 0; | ||
42 | } | ||
43 | |||
44 | compatible = of_get_property(node, "compatible", &cplen); | ||
45 | if (!compatible) | ||
46 | return -ENODEV; | ||
47 | |||
48 | /* 2. search for linux,<i2c-type> entry */ | ||
49 | p = compatible; | ||
50 | while (cplen > 0) { | ||
51 | if (!strncmp(p, "linux,", 6)) { | ||
52 | p += 6; | ||
53 | if (strlcpy(info->type, p, | ||
54 | I2C_NAME_SIZE) >= I2C_NAME_SIZE) | ||
55 | return -ENOMEM; | ||
56 | return 0; | ||
57 | } | ||
58 | |||
59 | i = strlen(p) + 1; | ||
60 | p += i; | ||
61 | cplen -= i; | ||
62 | } | ||
63 | |||
64 | /* 3. take fist compatible entry and strip manufacturer */ | ||
65 | p = strchr(compatible, ','); | ||
66 | if (!p) | ||
67 | return -ENODEV; | ||
68 | p++; | ||
69 | if (strlcpy(info->type, p, I2C_NAME_SIZE) >= I2C_NAME_SIZE) | ||
70 | return -ENOMEM; | ||
71 | return 0; | ||
72 | } | ||
73 | |||
74 | void of_register_i2c_devices(struct i2c_adapter *adap, | ||
75 | struct device_node *adap_node) | ||
76 | { | ||
77 | void *result; | ||
78 | struct device_node *node; | ||
79 | |||
80 | for_each_child_of_node(adap_node, node) { | ||
81 | struct i2c_board_info info = {}; | ||
82 | const u32 *addr; | ||
83 | int len; | ||
84 | |||
85 | addr = of_get_property(node, "reg", &len); | ||
86 | if (!addr || len < sizeof(int) || *addr > (1 << 10) - 1) { | ||
87 | printk(KERN_ERR | ||
88 | "of-i2c: invalid i2c device entry\n"); | ||
89 | continue; | ||
90 | } | ||
91 | |||
92 | info.irq = irq_of_parse_and_map(node, 0); | ||
93 | if (info.irq == NO_IRQ) | ||
94 | info.irq = -1; | ||
95 | |||
96 | if (of_find_i2c_driver(node, &info) < 0) { | ||
97 | irq_dispose_mapping(info.irq); | ||
98 | continue; | ||
99 | } | ||
100 | |||
101 | info.addr = *addr; | ||
102 | |||
103 | request_module(info.type); | ||
104 | |||
105 | result = i2c_new_device(adap, &info); | ||
106 | if (result == NULL) { | ||
107 | printk(KERN_ERR | ||
108 | "of-i2c: Failed to load driver for %s\n", | ||
109 | info.type); | ||
110 | irq_dispose_mapping(info.irq); | ||
111 | continue; | ||
112 | } | ||
113 | } | ||
114 | } | ||
115 | EXPORT_SYMBOL(of_register_i2c_devices); | ||
diff --git a/include/linux/of_i2c.h b/include/linux/of_i2c.h new file mode 100644 index 000000000000..2e5a96732042 --- /dev/null +++ b/include/linux/of_i2c.h | |||
@@ -0,0 +1,24 @@ | |||
1 | /* | ||
2 | * Generic I2C API implementation for PowerPC. | ||
3 | * | ||
4 | * Copyright (c) 2008 Jochen Friedrich <jochen@scram.de> | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation; either version 2 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | */ | ||
11 | |||
12 | #ifndef __LINUX_OF_I2C_H | ||
13 | #define __LINUX_OF_I2C_H | ||
14 | |||
15 | #include <linux/i2c.h> | ||
16 | |||
17 | #ifdef CONFIG_OF_I2C | ||
18 | |||
19 | void of_register_i2c_devices(struct i2c_adapter *adap, | ||
20 | struct device_node *adap_node); | ||
21 | |||
22 | #endif /* CONFIG_OF_I2C */ | ||
23 | |||
24 | #endif /* __LINUX_OF_I2C_H */ | ||