gpiolib-of.c 7.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * OF helpers for the GPIO API
 *
 * Copyright (c) 2007-2008  MontaVista Software, Inc.
 *
 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

G
Grant Likely 已提交
14
#include <linux/device.h>
15
#include <linux/errno.h>
G
Grant Likely 已提交
16
#include <linux/module.h>
17
#include <linux/io.h>
18
#include <linux/gpio.h>
19
#include <linux/of.h>
G
Grant Likely 已提交
20
#include <linux/of_address.h>
21
#include <linux/of_gpio.h>
22
#include <linux/pinctrl/pinctrl.h>
G
Grant Likely 已提交
23
#include <linux/slab.h>
24

25
/* Private data structure for of_gpiochip_find_and_xlate */
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
struct gg_data {
	enum of_gpio_flags *flags;
	struct of_phandle_args gpiospec;

	int out_gpio;
};

/* Private function for resolving node pointer to gpio_chip */
static int of_gpiochip_find_and_xlate(struct gpio_chip *gc, void *data)
{
	struct gg_data *gg_data = data;
	int ret;

	if ((gc->of_node != gg_data->gpiospec.np) ||
	    (gc->of_gpio_n_cells != gg_data->gpiospec.args_count) ||
	    (!gc->of_xlate))
		return false;

	ret = gc->of_xlate(gc, &gg_data->gpiospec, gg_data->flags);
	if (ret < 0)
		return false;

	gg_data->out_gpio = ret + gc->base;
	return true;
}

52
/**
53
 * of_get_named_gpio_flags() - Get a GPIO number and flags to use with GPIO API
54
 * @np:		device node to get GPIO from
55
 * @propname:	property name containing gpio specifier(s)
56
 * @index:	index of the GPIO
57
 * @flags:	a flags pointer to fill in
58 59
 *
 * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
60 61
 * value on the error condition. If @flags is not NULL the function also fills
 * in flags for the GPIO.
62
 */
63 64
int of_get_named_gpio_flags(struct device_node *np, const char *propname,
                           int index, enum of_gpio_flags *flags)
65
{
66 67 68 69
	/* Return -EPROBE_DEFER to support probe() functions to be called
	 * later when the GPIO actually becomes available
	 */
	struct gg_data gg_data = { .flags = flags, .out_gpio = -EPROBE_DEFER };
70
	int ret;
71 72 73 74

	/* .of_xlate might decide to not fill in the flags, so clear it. */
	if (flags)
		*flags = 0;
75

76
	ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index,
77
					 &gg_data.gpiospec);
78 79
	if (ret) {
		pr_debug("%s: can't parse gpios property\n", __func__);
80
		return ret;
81 82
	}

83
	gpiochip_find(&gg_data, of_gpiochip_find_and_xlate);
84

85
	of_node_put(gg_data.gpiospec.np);
86
	pr_debug("%s exited with status %d\n", __func__, gg_data.out_gpio);
87
	return gg_data.out_gpio;
88
}
89
EXPORT_SYMBOL(of_get_named_gpio_flags);
90

91
/**
92
 * of_gpio_named_count - Count GPIOs for a device
93
 * @np:		device node to count GPIOs for
94
 * @propname:	property name containing gpio specifier(s)
95 96 97 98 99 100 101 102 103 104 105 106 107
 *
 * The function returns the count of GPIOs specified for a node.
 *
 * Note that the empty GPIO specifiers counts too. For example,
 *
 * gpios = <0
 *          &pio1 1 2
 *          0
 *          &pio2 3 4>;
 *
 * defines four GPIOs (so this function will return 4), two of which
 * are not specified.
 */
108
unsigned int of_gpio_named_count(struct device_node *np, const char* propname)
109 110 111 112 113 114
{
	unsigned int cnt = 0;

	do {
		int ret;

115
		ret = of_parse_phandle_with_args(np, propname, "#gpio-cells",
116
						 cnt, NULL);
117 118 119 120 121 122 123
		/* A hole in the gpios = <> counts anyway. */
		if (ret < 0 && ret != -EEXIST)
			break;
	} while (++cnt);

	return cnt;
}
124
EXPORT_SYMBOL(of_gpio_named_count);
125

126
/**
127
 * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
128
 * @gc:		pointer to the gpio_chip structure
129 130
 * @np:		device node of the GPIO chip
 * @gpio_spec:	gpio specifier as found in the device tree
131
 * @flags:	a flags pointer to fill in
132 133 134 135 136
 *
 * This is simple translation function, suitable for the most 1:1 mapped
 * gpio chips. This function performs only one sanity check: whether gpio
 * is less than ngpios (that is specified in the gpio_chip).
 */
137 138
int of_gpio_simple_xlate(struct gpio_chip *gc,
			 const struct of_phandle_args *gpiospec, u32 *flags)
139
{
140 141 142 143 144 145
	/*
	 * We're discouraging gpio_cells < 2, since that way you'll have to
	 * write your own xlate function (that will have to retrive the GPIO
	 * number and the flags from a single gpio cell -- this is possible,
	 * but not recommended).
	 */
146
	if (gc->of_gpio_n_cells < 2) {
147 148 149 150
		WARN_ON(1);
		return -EINVAL;
	}

151 152 153
	if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
		return -EINVAL;

154
	if (gpiospec->args[0] >= gc->ngpio)
155 156
		return -EINVAL;

157
	if (flags)
158
		*flags = gpiospec->args[1];
159

160
	return gpiospec->args[0];
161
}
162
EXPORT_SYMBOL(of_gpio_simple_xlate);
163 164 165 166 167 168 169 170 171 172

/**
 * of_mm_gpiochip_add - Add memory mapped GPIO chip (bank)
 * @np:		device node of the GPIO chip
 * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
 *
 * To use this function you should allocate and fill mm_gc with:
 *
 * 1) In the gpio_chip structure:
 *    - all the callbacks
173 174
 *    - of_gpio_n_cells
 *    - of_xlate callback (optional)
175 176 177 178 179 180 181 182 183 184 185 186
 *
 * 3) In the of_mm_gpio_chip structure:
 *    - save_regs callback (optional)
 *
 * If succeeded, this function will map bank's memory and will
 * do all necessary work for you. Then you'll able to use .regs
 * to manage GPIOs from the callbacks.
 */
int of_mm_gpiochip_add(struct device_node *np,
		       struct of_mm_gpio_chip *mm_gc)
{
	int ret = -ENOMEM;
187
	struct gpio_chip *gc = &mm_gc->gc;
188 189 190 191 192 193 194 195 196

	gc->label = kstrdup(np->full_name, GFP_KERNEL);
	if (!gc->label)
		goto err0;

	mm_gc->regs = of_iomap(np, 0);
	if (!mm_gc->regs)
		goto err1;

197
	gc->base = -1;
198 199 200 201

	if (mm_gc->save_regs)
		mm_gc->save_regs(mm_gc);

202
	mm_gc->gc.of_node = np;
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

	ret = gpiochip_add(gc);
	if (ret)
		goto err2;

	return 0;
err2:
	iounmap(mm_gc->regs);
err1:
	kfree(gc->label);
err0:
	pr_err("%s: GPIO chip registration failed with status %d\n",
	       np->full_name, ret);
	return ret;
}
EXPORT_SYMBOL(of_mm_gpiochip_add);
219

220
#ifdef CONFIG_PINCTRL
221
static void of_gpiochip_add_pin_range(struct gpio_chip *chip)
222 223 224
{
	struct device_node *np = chip->of_node;
	struct of_phandle_args pinspec;
225
	struct pinctrl_dev *pctldev;
226 227 228 229 230 231 232 233 234 235 236
	int index = 0, ret;

	if (!np)
		return;

	do {
		ret = of_parse_phandle_with_args(np, "gpio-ranges",
				"#gpio-range-cells", index, &pinspec);
		if (ret)
			break;

237 238
		pctldev = of_pinctrl_get(pinspec.np);
		if (!pctldev)
239 240
			break;

241 242 243 244 245 246 247 248 249 250 251
		/*
		 * This assumes that the n GPIO pins are consecutive in the
		 * GPIO number space, and that the pins are also consecutive
		 * in their local number space. Currently it is not possible
		 * to add different ranges for one and the same GPIO chip,
		 * as the code assumes that we have one consecutive range
		 * on both, mapping 1-to-1.
		 *
		 * TODO: make the OF bindings handle multiple sparse ranges
		 * on the same GPIO chip.
		 */
252 253
		ret = gpiochip_add_pin_range(chip,
					     pinctrl_dev_get_name(pctldev),
254
					     0, /* offset in gpiochip */
255 256
					     pinspec.args[0],
					     pinspec.args[1]);
257

258 259
		if (ret)
			break;
260 261 262 263 264

	} while (index++);
}

#else
265
static void of_gpiochip_add_pin_range(struct gpio_chip *chip) {}
266 267
#endif

268 269 270 271 272 273 274 275 276 277 278 279 280
void of_gpiochip_add(struct gpio_chip *chip)
{
	if ((!chip->of_node) && (chip->dev))
		chip->of_node = chip->dev->of_node;

	if (!chip->of_node)
		return;

	if (!chip->of_xlate) {
		chip->of_gpio_n_cells = 2;
		chip->of_xlate = of_gpio_simple_xlate;
	}

281
	of_gpiochip_add_pin_range(chip);
282 283 284 285 286
	of_node_get(chip->of_node);
}

void of_gpiochip_remove(struct gpio_chip *chip)
{
287
	gpiochip_remove_pin_ranges(chip);
288

289 290 291
	if (chip->of_node)
		of_node_put(chip->of_node);
}