Blame view

drivers/of/gpio.c 5.78 KB
863fbf496   Anton Vorontsov   [POWERPC] OF help...
1
2
3
4
5
6
7
8
9
10
11
12
  /*
   * 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.
   */
2e13cba8d   Grant Likely   of/gpio: fix of_g...
13
  #include <linux/device.h>
863fbf496   Anton Vorontsov   [POWERPC] OF help...
14
  #include <linux/errno.h>
2e13cba8d   Grant Likely   of/gpio: fix of_g...
15
  #include <linux/module.h>
863fbf496   Anton Vorontsov   [POWERPC] OF help...
16
17
  #include <linux/io.h>
  #include <linux/of.h>
2e13cba8d   Grant Likely   of/gpio: fix of_g...
18
  #include <linux/of_address.h>
863fbf496   Anton Vorontsov   [POWERPC] OF help...
19
  #include <linux/of_gpio.h>
2e13cba8d   Grant Likely   of/gpio: fix of_g...
20
  #include <linux/slab.h>
863fbf496   Anton Vorontsov   [POWERPC] OF help...
21
22
  
  /**
a6b091914   John Bonesio   of/gpio: Add new ...
23
   * of_get_named_gpio_flags() - Get a GPIO number and flags to use with GPIO API
863fbf496   Anton Vorontsov   [POWERPC] OF help...
24
   * @np:		device node to get GPIO from
a6b091914   John Bonesio   of/gpio: Add new ...
25
   * @propname:	property name containing gpio specifier(s)
863fbf496   Anton Vorontsov   [POWERPC] OF help...
26
   * @index:	index of the GPIO
b908b53d5   Anton Vorontsov   of/gpio: Implemen...
27
   * @flags:	a flags pointer to fill in
863fbf496   Anton Vorontsov   [POWERPC] OF help...
28
29
   *
   * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
b908b53d5   Anton Vorontsov   of/gpio: Implemen...
30
31
   * value on the error condition. If @flags is not NULL the function also fills
   * in flags for the GPIO.
863fbf496   Anton Vorontsov   [POWERPC] OF help...
32
   */
a6b091914   John Bonesio   of/gpio: Add new ...
33
34
  int of_get_named_gpio_flags(struct device_node *np, const char *propname,
                             int index, enum of_gpio_flags *flags)
863fbf496   Anton Vorontsov   [POWERPC] OF help...
35
  {
64b60e096   Anton Vorontsov   of: Add new helpe...
36
  	int ret;
a19e3da5b   Anton Vorontsov   of/gpio: Kill of_...
37
  	struct gpio_chip *gc;
15c9a0acc   Grant Likely   of: create of_pha...
38
  	struct of_phandle_args gpiospec;
863fbf496   Anton Vorontsov   [POWERPC] OF help...
39

15c9a0acc   Grant Likely   of: create of_pha...
40
41
  	ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index,
  					 &gpiospec);
64b60e096   Anton Vorontsov   of: Add new helpe...
42
43
44
  	if (ret) {
  		pr_debug("%s: can't parse gpios property
  ", __func__);
863fbf496   Anton Vorontsov   [POWERPC] OF help...
45
46
  		goto err0;
  	}
863fbf496   Anton Vorontsov   [POWERPC] OF help...
47

15c9a0acc   Grant Likely   of: create of_pha...
48
  	gc = of_node_to_gpiochip(gpiospec.np);
a19e3da5b   Anton Vorontsov   of/gpio: Kill of_...
49
  	if (!gc) {
64b60e096   Anton Vorontsov   of: Add new helpe...
50
51
  		pr_debug("%s: gpio controller %s isn't registered
  ",
15c9a0acc   Grant Likely   of: create of_pha...
52
  			 np->full_name, gpiospec.np->full_name);
64b60e096   Anton Vorontsov   of: Add new helpe...
53
54
  		ret = -ENODEV;
  		goto err1;
863fbf496   Anton Vorontsov   [POWERPC] OF help...
55
  	}
15c9a0acc   Grant Likely   of: create of_pha...
56
  	if (gpiospec.args_count != gc->of_gpio_n_cells) {
64b60e096   Anton Vorontsov   of: Add new helpe...
57
58
  		pr_debug("%s: wrong #gpio-cells for %s
  ",
15c9a0acc   Grant Likely   of: create of_pha...
59
  			 np->full_name, gpiospec.np->full_name);
64b60e096   Anton Vorontsov   of: Add new helpe...
60
61
  		ret = -EINVAL;
  		goto err1;
863fbf496   Anton Vorontsov   [POWERPC] OF help...
62
  	}
b908b53d5   Anton Vorontsov   of/gpio: Implemen...
63
64
65
  	/* .xlate might decide to not fill in the flags, so clear it. */
  	if (flags)
  		*flags = 0;
15c9a0acc   Grant Likely   of: create of_pha...
66
  	ret = gc->of_xlate(gc, &gpiospec, flags);
863fbf496   Anton Vorontsov   [POWERPC] OF help...
67
68
  	if (ret < 0)
  		goto err1;
a19e3da5b   Anton Vorontsov   of/gpio: Kill of_...
69
  	ret += gc->base;
863fbf496   Anton Vorontsov   [POWERPC] OF help...
70
  err1:
15c9a0acc   Grant Likely   of: create of_pha...
71
  	of_node_put(gpiospec.np);
863fbf496   Anton Vorontsov   [POWERPC] OF help...
72
73
74
75
76
  err0:
  	pr_debug("%s exited with status %d
  ", __func__, ret);
  	return ret;
  }
a6b091914   John Bonesio   of/gpio: Add new ...
77
  EXPORT_SYMBOL(of_get_named_gpio_flags);
863fbf496   Anton Vorontsov   [POWERPC] OF help...
78
79
  
  /**
749820928   Anton Vorontsov   of/gpio: Implemen...
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
   * of_gpio_count - Count GPIOs for a device
   * @np:		device node to count GPIOs for
   *
   * 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.
   */
  unsigned int of_gpio_count(struct device_node *np)
  {
  	unsigned int cnt = 0;
  
  	do {
  		int ret;
15c9a0acc   Grant Likely   of: create of_pha...
101
102
  		ret = of_parse_phandle_with_args(np, "gpios", "#gpio-cells",
  						 cnt, NULL);
749820928   Anton Vorontsov   of/gpio: Implemen...
103
104
105
106
107
108
109
110
111
112
  		/* A hole in the gpios = <> counts anyway. */
  		if (ret < 0 && ret != -EEXIST)
  			break;
  	} while (++cnt);
  
  	return cnt;
  }
  EXPORT_SYMBOL(of_gpio_count);
  
  /**
b908b53d5   Anton Vorontsov   of/gpio: Implemen...
113
   * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
a19e3da5b   Anton Vorontsov   of/gpio: Kill of_...
114
   * @gc:		pointer to the gpio_chip structure
863fbf496   Anton Vorontsov   [POWERPC] OF help...
115
116
   * @np:		device node of the GPIO chip
   * @gpio_spec:	gpio specifier as found in the device tree
b908b53d5   Anton Vorontsov   of/gpio: Implemen...
117
   * @flags:	a flags pointer to fill in
863fbf496   Anton Vorontsov   [POWERPC] OF help...
118
119
120
121
122
   *
   * 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).
   */
15c9a0acc   Grant Likely   of: create of_pha...
123
124
  int of_gpio_simple_xlate(struct gpio_chip *gc,
  			 const struct of_phandle_args *gpiospec, u32 *flags)
863fbf496   Anton Vorontsov   [POWERPC] OF help...
125
  {
b908b53d5   Anton Vorontsov   of/gpio: Implemen...
126
127
128
129
130
131
  	/*
  	 * 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).
  	 */
a19e3da5b   Anton Vorontsov   of/gpio: Kill of_...
132
  	if (gc->of_gpio_n_cells < 2) {
b908b53d5   Anton Vorontsov   of/gpio: Implemen...
133
134
135
  		WARN_ON(1);
  		return -EINVAL;
  	}
15c9a0acc   Grant Likely   of: create of_pha...
136
137
138
139
  	if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
  		return -EINVAL;
  
  	if (gpiospec->args[0] > gc->ngpio)
863fbf496   Anton Vorontsov   [POWERPC] OF help...
140
  		return -EINVAL;
b908b53d5   Anton Vorontsov   of/gpio: Implemen...
141
  	if (flags)
15c9a0acc   Grant Likely   of: create of_pha...
142
  		*flags = gpiospec->args[1];
b908b53d5   Anton Vorontsov   of/gpio: Implemen...
143

15c9a0acc   Grant Likely   of: create of_pha...
144
  	return gpiospec->args[0];
863fbf496   Anton Vorontsov   [POWERPC] OF help...
145
  }
3038bbdf7   Jamie Iles   of/gpio: export o...
146
  EXPORT_SYMBOL(of_gpio_simple_xlate);
863fbf496   Anton Vorontsov   [POWERPC] OF help...
147

863fbf496   Anton Vorontsov   [POWERPC] OF help...
148
149
150
151
152
153
154
155
156
  /**
   * 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
a19e3da5b   Anton Vorontsov   of/gpio: Kill of_...
157
158
   *    - of_gpio_n_cells
   *    - of_xlate callback (optional)
863fbf496   Anton Vorontsov   [POWERPC] OF help...
159
160
161
162
163
164
165
166
167
168
169
170
   *
   * 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;
a19e3da5b   Anton Vorontsov   of/gpio: Kill of_...
171
  	struct gpio_chip *gc = &mm_gc->gc;
863fbf496   Anton Vorontsov   [POWERPC] OF help...
172
173
174
175
176
177
178
179
  
  	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;
21451155d   Anton Vorontsov   [POWERPC] of/gpio...
180
  	gc->base = -1;
863fbf496   Anton Vorontsov   [POWERPC] OF help...
181

863fbf496   Anton Vorontsov   [POWERPC] OF help...
182
183
  	if (mm_gc->save_regs)
  		mm_gc->save_regs(mm_gc);
a19e3da5b   Anton Vorontsov   of/gpio: Kill of_...
184
  	mm_gc->gc.of_node = np;
863fbf496   Anton Vorontsov   [POWERPC] OF help...
185
186
187
188
  
  	ret = gpiochip_add(gc);
  	if (ret)
  		goto err2;
863fbf496   Anton Vorontsov   [POWERPC] OF help...
189
190
  	return 0;
  err2:
863fbf496   Anton Vorontsov   [POWERPC] OF help...
191
192
193
194
195
196
197
198
199
200
  	iounmap(mm_gc->regs);
  err1:
  	kfree(gc->label);
  err0:
  	pr_err("%s: GPIO chip registration failed with status %d
  ",
  	       np->full_name, ret);
  	return ret;
  }
  EXPORT_SYMBOL(of_mm_gpiochip_add);
594fa265e   Grant Likely   of/gpio: stop usi...
201

391c970c0   Anton Vorontsov   of/gpio: add defa...
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
  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;
  	}
  
  	of_node_get(chip->of_node);
  }
  
  void of_gpiochip_remove(struct gpio_chip *chip)
  {
  	if (chip->of_node)
  		of_node_put(chip->of_node);
  }
594fa265e   Grant Likely   of/gpio: stop usi...
223
224
225
226
227
228
229
230
231
232
  /* Private function for resolving node pointer to gpio_chip */
  static int of_gpiochip_is_match(struct gpio_chip *chip, void *data)
  {
  	return chip->of_node == data;
  }
  
  struct gpio_chip *of_node_to_gpiochip(struct device_node *np)
  {
  	return gpiochip_find(np, of_gpiochip_is_match);
  }