aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/of
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/of')
-rw-r--r--drivers/of/Kconfig12
-rw-r--r--drivers/of/Makefile2
-rw-r--r--drivers/of/base.c26
-rw-r--r--drivers/of/gpio.c242
-rw-r--r--drivers/of/of_i2c.c115
5 files changed, 397 insertions, 0 deletions
diff --git a/drivers/of/Kconfig b/drivers/of/Kconfig
index c03072b12f42..3a7a11a75fb4 100644
--- a/drivers/of/Kconfig
+++ b/drivers/of/Kconfig
@@ -1,3 +1,15 @@
1config OF_DEVICE 1config OF_DEVICE
2 def_bool y 2 def_bool y
3 depends on OF && (SPARC || PPC_OF) 3 depends on OF && (SPARC || PPC_OF)
4
5config OF_GPIO
6 def_bool y
7 depends on OF && PPC_OF && HAVE_GPIO_LIB
8 help
9 OpenFirmware GPIO accessors
10
11config OF_I2C
12 def_tristate I2C
13 depends on PPC_OF && I2C
14 help
15 OpenFirmware I2C accessors
diff --git a/drivers/of/Makefile b/drivers/of/Makefile
index ab9be5d5255b..548772e871fd 100644
--- a/drivers/of/Makefile
+++ b/drivers/of/Makefile
@@ -1,2 +1,4 @@
1obj-y = base.o 1obj-y = base.o
2obj-$(CONFIG_OF_DEVICE) += device.o platform.o 2obj-$(CONFIG_OF_DEVICE) += device.o platform.o
3obj-$(CONFIG_OF_GPIO) += gpio.o
4obj-$(CONFIG_OF_I2C) += of_i2c.o
diff --git a/drivers/of/base.c b/drivers/of/base.c
index 80c9deca5f35..9bd7c4a31253 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -117,6 +117,32 @@ int of_device_is_compatible(const struct device_node *device,
117EXPORT_SYMBOL(of_device_is_compatible); 117EXPORT_SYMBOL(of_device_is_compatible);
118 118
119/** 119/**
120 * of_device_is_available - check if a device is available for use
121 *
122 * @device: Node to check for availability
123 *
124 * Returns 1 if the status property is absent or set to "okay" or "ok",
125 * 0 otherwise
126 */
127int of_device_is_available(const struct device_node *device)
128{
129 const char *status;
130 int statlen;
131
132 status = of_get_property(device, "status", &statlen);
133 if (status == NULL)
134 return 1;
135
136 if (statlen > 0) {
137 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
138 return 1;
139 }
140
141 return 0;
142}
143EXPORT_SYMBOL(of_device_is_available);
144
145/**
120 * of_get_parent - Get a node's parent if any 146 * of_get_parent - Get a node's parent if any
121 * @node: Node to get parent 147 * @node: Node to get parent
122 * 148 *
diff --git a/drivers/of/gpio.c b/drivers/of/gpio.c
new file mode 100644
index 000000000000..000681e98f2c
--- /dev/null
+++ b/drivers/of/gpio.c
@@ -0,0 +1,242 @@
1/*
2 * OF helpers for the GPIO API
3 *
4 * Copyright (c) 2007-2008 MontaVista Software, Inc.
5 *
6 * Author: Anton Vorontsov <avorontsov@ru.mvista.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/kernel.h>
15#include <linux/errno.h>
16#include <linux/io.h>
17#include <linux/of.h>
18#include <linux/of_gpio.h>
19#include <asm/prom.h>
20
21/**
22 * of_get_gpio - Get a GPIO number from the device tree to use with GPIO API
23 * @np: device node to get GPIO from
24 * @index: index of the GPIO
25 *
26 * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
27 * value on the error condition.
28 */
29int of_get_gpio(struct device_node *np, int index)
30{
31 int ret = -EINVAL;
32 struct device_node *gc;
33 struct of_gpio_chip *of_gc = NULL;
34 int size;
35 const u32 *gpios;
36 u32 nr_cells;
37 int i;
38 const void *gpio_spec;
39 const u32 *gpio_cells;
40 int gpio_index = 0;
41
42 gpios = of_get_property(np, "gpios", &size);
43 if (!gpios) {
44 ret = -ENOENT;
45 goto err0;
46 }
47 nr_cells = size / sizeof(u32);
48
49 for (i = 0; i < nr_cells; gpio_index++) {
50 const phandle *gpio_phandle;
51
52 gpio_phandle = gpios + i;
53 gpio_spec = gpio_phandle + 1;
54
55 /* one cell hole in the gpios = <>; */
56 if (!*gpio_phandle) {
57 if (gpio_index == index)
58 return -ENOENT;
59 i++;
60 continue;
61 }
62
63 gc = of_find_node_by_phandle(*gpio_phandle);
64 if (!gc) {
65 pr_debug("%s: could not find phandle for gpios\n",
66 np->full_name);
67 goto err0;
68 }
69
70 of_gc = gc->data;
71 if (!of_gc) {
72 pr_debug("%s: gpio controller %s isn't registered\n",
73 np->full_name, gc->full_name);
74 goto err1;
75 }
76
77 gpio_cells = of_get_property(gc, "#gpio-cells", &size);
78 if (!gpio_cells || size != sizeof(*gpio_cells) ||
79 *gpio_cells != of_gc->gpio_cells) {
80 pr_debug("%s: wrong #gpio-cells for %s\n",
81 np->full_name, gc->full_name);
82 goto err1;
83 }
84
85 /* Next phandle is at phandle cells + #gpio-cells */
86 i += sizeof(*gpio_phandle) / sizeof(u32) + *gpio_cells;
87 if (i >= nr_cells + 1) {
88 pr_debug("%s: insufficient gpio-spec length\n",
89 np->full_name);
90 goto err1;
91 }
92
93 if (gpio_index == index)
94 break;
95
96 of_gc = NULL;
97 of_node_put(gc);
98 }
99
100 if (!of_gc) {
101 ret = -ENOENT;
102 goto err0;
103 }
104
105 ret = of_gc->xlate(of_gc, np, gpio_spec);
106 if (ret < 0)
107 goto err1;
108
109 ret += of_gc->gc.base;
110err1:
111 of_node_put(gc);
112err0:
113 pr_debug("%s exited with status %d\n", __func__, ret);
114 return ret;
115}
116EXPORT_SYMBOL(of_get_gpio);
117
118/**
119 * of_gpio_simple_xlate - translate gpio_spec to the GPIO number
120 * @of_gc: pointer to the of_gpio_chip structure
121 * @np: device node of the GPIO chip
122 * @gpio_spec: gpio specifier as found in the device tree
123 *
124 * This is simple translation function, suitable for the most 1:1 mapped
125 * gpio chips. This function performs only one sanity check: whether gpio
126 * is less than ngpios (that is specified in the gpio_chip).
127 */
128int of_gpio_simple_xlate(struct of_gpio_chip *of_gc, struct device_node *np,
129 const void *gpio_spec)
130{
131 const u32 *gpio = gpio_spec;
132
133 if (*gpio > of_gc->gc.ngpio)
134 return -EINVAL;
135
136 return *gpio;
137}
138EXPORT_SYMBOL(of_gpio_simple_xlate);
139
140/* Should be sufficient for now, later we'll use dynamic bases. */
141#if defined(CONFIG_PPC32) || defined(CONFIG_SPARC32)
142#define GPIOS_PER_CHIP 32
143#else
144#define GPIOS_PER_CHIP 64
145#endif
146
147static int of_get_gpiochip_base(struct device_node *np)
148{
149 struct device_node *gc = NULL;
150 int gpiochip_base = 0;
151
152 while ((gc = of_find_all_nodes(gc))) {
153 if (!of_get_property(gc, "gpio-controller", NULL))
154 continue;
155
156 if (gc != np) {
157 gpiochip_base += GPIOS_PER_CHIP;
158 continue;
159 }
160
161 of_node_put(gc);
162
163 if (gpiochip_base >= ARCH_NR_GPIOS)
164 return -ENOSPC;
165
166 return gpiochip_base;
167 }
168
169 return -ENOENT;
170}
171
172/**
173 * of_mm_gpiochip_add - Add memory mapped GPIO chip (bank)
174 * @np: device node of the GPIO chip
175 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
176 *
177 * To use this function you should allocate and fill mm_gc with:
178 *
179 * 1) In the gpio_chip structure:
180 * - all the callbacks
181 *
182 * 2) In the of_gpio_chip structure:
183 * - gpio_cells
184 * - xlate callback (optional)
185 *
186 * 3) In the of_mm_gpio_chip structure:
187 * - save_regs callback (optional)
188 *
189 * If succeeded, this function will map bank's memory and will
190 * do all necessary work for you. Then you'll able to use .regs
191 * to manage GPIOs from the callbacks.
192 */
193int of_mm_gpiochip_add(struct device_node *np,
194 struct of_mm_gpio_chip *mm_gc)
195{
196 int ret = -ENOMEM;
197 struct of_gpio_chip *of_gc = &mm_gc->of_gc;
198 struct gpio_chip *gc = &of_gc->gc;
199
200 gc->label = kstrdup(np->full_name, GFP_KERNEL);
201 if (!gc->label)
202 goto err0;
203
204 mm_gc->regs = of_iomap(np, 0);
205 if (!mm_gc->regs)
206 goto err1;
207
208 gc->base = of_get_gpiochip_base(np);
209 if (gc->base < 0) {
210 ret = gc->base;
211 goto err1;
212 }
213
214 if (!of_gc->xlate)
215 of_gc->xlate = of_gpio_simple_xlate;
216
217 if (mm_gc->save_regs)
218 mm_gc->save_regs(mm_gc);
219
220 np->data = of_gc;
221
222 ret = gpiochip_add(gc);
223 if (ret)
224 goto err2;
225
226 /* We don't want to lose the node and its ->data */
227 of_node_get(np);
228
229 pr_debug("%s: registered as generic GPIO chip, base is %d\n",
230 np->full_name, gc->base);
231 return 0;
232err2:
233 np->data = NULL;
234 iounmap(mm_gc->regs);
235err1:
236 kfree(gc->label);
237err0:
238 pr_err("%s: GPIO chip registration failed with status %d\n",
239 np->full_name, ret);
240 return ret;
241}
242EXPORT_SYMBOL(of_mm_gpiochip_add);
diff --git a/drivers/of/of_i2c.c b/drivers/of/of_i2c.c
new file mode 100644
index 000000000000..631689171159
--- /dev/null
+++ b/drivers/of/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
17struct i2c_driver_device {
18 char *of_device;
19 char *i2c_type;
20};
21
22static struct i2c_driver_device i2c_devices[] = {
23 { "dallas,ds1374", "rtc-ds1374" },
24};
25
26static 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
74void 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}
115EXPORT_SYMBOL(of_register_i2c_devices);