bonding: deliver netdev event for fail-over under the active-backup mode
[linux-2.6/kvm.git] / drivers / of / gpio.c
blob000681e98f2c2d8ef6df123ab8026cc0caca751f
1 /*
2 * OF helpers for the GPIO API
4 * Copyright (c) 2007-2008 MontaVista Software, Inc.
6 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
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.
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>
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
26 * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
27 * value on the error condition.
29 int of_get_gpio(struct device_node *np, int index)
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;
42 gpios = of_get_property(np, "gpios", &size);
43 if (!gpios) {
44 ret = -ENOENT;
45 goto err0;
47 nr_cells = size / sizeof(u32);
49 for (i = 0; i < nr_cells; gpio_index++) {
50 const phandle *gpio_phandle;
52 gpio_phandle = gpios + i;
53 gpio_spec = gpio_phandle + 1;
55 /* one cell hole in the gpios = <>; */
56 if (!*gpio_phandle) {
57 if (gpio_index == index)
58 return -ENOENT;
59 i++;
60 continue;
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;
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;
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;
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;
93 if (gpio_index == index)
94 break;
96 of_gc = NULL;
97 of_node_put(gc);
100 if (!of_gc) {
101 ret = -ENOENT;
102 goto err0;
105 ret = of_gc->xlate(of_gc, np, gpio_spec);
106 if (ret < 0)
107 goto err1;
109 ret += of_gc->gc.base;
110 err1:
111 of_node_put(gc);
112 err0:
113 pr_debug("%s exited with status %d\n", __func__, ret);
114 return ret;
116 EXPORT_SYMBOL(of_get_gpio);
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
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).
128 int of_gpio_simple_xlate(struct of_gpio_chip *of_gc, struct device_node *np,
129 const void *gpio_spec)
131 const u32 *gpio = gpio_spec;
133 if (*gpio > of_gc->gc.ngpio)
134 return -EINVAL;
136 return *gpio;
138 EXPORT_SYMBOL(of_gpio_simple_xlate);
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
147 static int of_get_gpiochip_base(struct device_node *np)
149 struct device_node *gc = NULL;
150 int gpiochip_base = 0;
152 while ((gc = of_find_all_nodes(gc))) {
153 if (!of_get_property(gc, "gpio-controller", NULL))
154 continue;
156 if (gc != np) {
157 gpiochip_base += GPIOS_PER_CHIP;
158 continue;
161 of_node_put(gc);
163 if (gpiochip_base >= ARCH_NR_GPIOS)
164 return -ENOSPC;
166 return gpiochip_base;
169 return -ENOENT;
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
177 * To use this function you should allocate and fill mm_gc with:
179 * 1) In the gpio_chip structure:
180 * - all the callbacks
182 * 2) In the of_gpio_chip structure:
183 * - gpio_cells
184 * - xlate callback (optional)
186 * 3) In the of_mm_gpio_chip structure:
187 * - save_regs callback (optional)
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.
193 int of_mm_gpiochip_add(struct device_node *np,
194 struct of_mm_gpio_chip *mm_gc)
196 int ret = -ENOMEM;
197 struct of_gpio_chip *of_gc = &mm_gc->of_gc;
198 struct gpio_chip *gc = &of_gc->gc;
200 gc->label = kstrdup(np->full_name, GFP_KERNEL);
201 if (!gc->label)
202 goto err0;
204 mm_gc->regs = of_iomap(np, 0);
205 if (!mm_gc->regs)
206 goto err1;
208 gc->base = of_get_gpiochip_base(np);
209 if (gc->base < 0) {
210 ret = gc->base;
211 goto err1;
214 if (!of_gc->xlate)
215 of_gc->xlate = of_gpio_simple_xlate;
217 if (mm_gc->save_regs)
218 mm_gc->save_regs(mm_gc);
220 np->data = of_gc;
222 ret = gpiochip_add(gc);
223 if (ret)
224 goto err2;
226 /* We don't want to lose the node and its ->data */
227 of_node_get(np);
229 pr_debug("%s: registered as generic GPIO chip, base is %d\n",
230 np->full_name, gc->base);
231 return 0;
232 err2:
233 np->data = NULL;
234 iounmap(mm_gc->regs);
235 err1:
236 kfree(gc->label);
237 err0:
238 pr_err("%s: GPIO chip registration failed with status %d\n",
239 np->full_name, ret);
240 return ret;
242 EXPORT_SYMBOL(of_mm_gpiochip_add);