Commit | Line | Data |
---|---|---|
27038c3e | 1 | // SPDX-License-Identifier: GPL-2.0+ |
863fbf49 AV |
2 | /* |
3 | * OF helpers for the GPIO API | |
4 | * | |
5 | * Copyright (c) 2007-2008 MontaVista Software, Inc. | |
6 | * | |
7 | * Author: Anton Vorontsov <avorontsov@ru.mvista.com> | |
863fbf49 AV |
8 | */ |
9 | ||
2e13cba8 | 10 | #include <linux/device.h> |
bea4dbee | 11 | #include <linux/err.h> |
863fbf49 | 12 | #include <linux/errno.h> |
2e13cba8 | 13 | #include <linux/module.h> |
863fbf49 | 14 | #include <linux/io.h> |
af8b6375 | 15 | #include <linux/gpio/consumer.h> |
863fbf49 | 16 | #include <linux/of.h> |
2e13cba8 | 17 | #include <linux/of_address.h> |
863fbf49 | 18 | #include <linux/of_gpio.h> |
f23f1516 | 19 | #include <linux/pinctrl/pinctrl.h> |
2e13cba8 | 20 | #include <linux/slab.h> |
f625d460 | 21 | #include <linux/gpio/machine.h> |
863fbf49 | 22 | |
1bd6b601 | 23 | #include "gpiolib.h" |
af8b6375 | 24 | |
c7e9d398 | 25 | static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data) |
3d0f7cf0 | 26 | { |
c7e9d398 MY |
27 | struct of_phandle_args *gpiospec = data; |
28 | ||
29 | return chip->gpiodev->dev.of_node == gpiospec->np && | |
d49b48f0 | 30 | chip->of_xlate && |
c7e9d398 | 31 | chip->of_xlate(chip, gpiospec, NULL) >= 0; |
762c2e46 | 32 | } |
3d0f7cf0 | 33 | |
c7e9d398 MY |
34 | static struct gpio_chip *of_find_gpiochip_by_xlate( |
35 | struct of_phandle_args *gpiospec) | |
762c2e46 | 36 | { |
c7e9d398 | 37 | return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate); |
3d0f7cf0 | 38 | } |
3d0f7cf0 | 39 | |
99468c1a MY |
40 | static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip, |
41 | struct of_phandle_args *gpiospec, | |
42 | enum of_gpio_flags *flags) | |
3d0f7cf0 | 43 | { |
3d0f7cf0 GL |
44 | int ret; |
45 | ||
99468c1a MY |
46 | if (chip->of_gpio_n_cells != gpiospec->args_count) |
47 | return ERR_PTR(-EINVAL); | |
48 | ||
49 | ret = chip->of_xlate(chip, gpiospec, flags); | |
50 | if (ret < 0) | |
51 | return ERR_PTR(ret); | |
52 | ||
53 | return gpiochip_get_desc(chip, ret); | |
3d0f7cf0 GL |
54 | } |
55 | ||
a603a2b8 | 56 | static void of_gpio_flags_quirks(struct device_node *np, |
89a5e15b | 57 | const char *propname, |
6953c57a LW |
58 | enum of_gpio_flags *flags, |
59 | int index) | |
a603a2b8 | 60 | { |
81c85ec1 LW |
61 | /* |
62 | * Handle MMC "cd-inverted" and "wp-inverted" semantics. | |
63 | */ | |
64 | if (IS_ENABLED(CONFIG_MMC)) { | |
89a5e15b LW |
65 | /* |
66 | * Active low is the default according to the | |
67 | * SDHCI specification and the device tree | |
68 | * bindings. However the code in the current | |
69 | * kernel was written such that the phandle | |
70 | * flags were always respected, and "cd-inverted" | |
71 | * would invert the flag from the device phandle. | |
72 | */ | |
73 | if (!strcmp(propname, "cd-gpios")) { | |
74 | if (of_property_read_bool(np, "cd-inverted")) | |
75 | *flags ^= OF_GPIO_ACTIVE_LOW; | |
81c85ec1 | 76 | } |
89a5e15b LW |
77 | if (!strcmp(propname, "wp-gpios")) { |
78 | if (of_property_read_bool(np, "wp-inverted")) | |
79 | *flags ^= OF_GPIO_ACTIVE_LOW; | |
81c85ec1 LW |
80 | } |
81 | } | |
a603a2b8 LW |
82 | /* |
83 | * Some GPIO fixed regulator quirks. | |
84 | * Note that active low is the default. | |
85 | */ | |
86 | if (IS_ENABLED(CONFIG_REGULATOR) && | |
906402a4 LW |
87 | (of_device_is_compatible(np, "regulator-fixed") || |
88 | of_device_is_compatible(np, "reg-fixed-voltage") || | |
a603a2b8 LW |
89 | of_device_is_compatible(np, "regulator-gpio"))) { |
90 | /* | |
91 | * The regulator GPIO handles are specified such that the | |
92 | * presence or absence of "enable-active-high" solely controls | |
93 | * the polarity of the GPIO line. Any phandle flags must | |
94 | * be actively ignored. | |
95 | */ | |
96 | if (*flags & OF_GPIO_ACTIVE_LOW) { | |
97 | pr_warn("%s GPIO handle specifies active low - ignored\n", | |
98 | of_node_full_name(np)); | |
99 | *flags &= ~OF_GPIO_ACTIVE_LOW; | |
100 | } | |
101 | if (!of_property_read_bool(np, "enable-active-high")) | |
102 | *flags |= OF_GPIO_ACTIVE_LOW; | |
103 | } | |
104 | /* | |
105 | * Legacy open drain handling for fixed voltage regulators. | |
106 | */ | |
107 | if (IS_ENABLED(CONFIG_REGULATOR) && | |
108 | of_device_is_compatible(np, "reg-fixed-voltage") && | |
109 | of_property_read_bool(np, "gpio-open-drain")) { | |
110 | *flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN); | |
111 | pr_info("%s uses legacy open drain flag - update the DTS if you can\n", | |
112 | of_node_full_name(np)); | |
113 | } | |
6953c57a LW |
114 | |
115 | /* | |
116 | * Legacy handling of SPI active high chip select. If we have a | |
117 | * property named "cs-gpios" we need to inspect the child node | |
118 | * to determine if the flags should have inverted semantics. | |
119 | */ | |
120 | if (IS_ENABLED(CONFIG_SPI_MASTER) && | |
121 | of_property_read_bool(np, "cs-gpios")) { | |
122 | struct device_node *child; | |
123 | u32 cs; | |
124 | int ret; | |
125 | ||
126 | for_each_child_of_node(np, child) { | |
127 | ret = of_property_read_u32(child, "reg", &cs); | |
128 | if (!ret) | |
129 | continue; | |
130 | if (cs == index) { | |
131 | /* | |
132 | * SPI children have active low chip selects | |
133 | * by default. This can be specified negatively | |
134 | * by just omitting "spi-cs-high" in the | |
135 | * device node, or actively by tagging on | |
136 | * GPIO_ACTIVE_LOW as flag in the device | |
137 | * tree. If the line is simultaneously | |
138 | * tagged as active low in the device tree | |
139 | * and has the "spi-cs-high" set, we get a | |
140 | * conflict and the "spi-cs-high" flag will | |
141 | * take precedence. | |
142 | */ | |
143 | if (of_property_read_bool(np, "spi-cs-high")) { | |
144 | if (*flags & OF_GPIO_ACTIVE_LOW) { | |
145 | pr_warn("%s GPIO handle specifies active low - ignored\n", | |
146 | of_node_full_name(np)); | |
147 | *flags &= ~OF_GPIO_ACTIVE_LOW; | |
148 | } | |
149 | } else { | |
150 | if (!(*flags & OF_GPIO_ACTIVE_LOW)) | |
151 | pr_info("%s enforce active low on chipselect handle\n", | |
152 | of_node_full_name(np)); | |
153 | *flags |= OF_GPIO_ACTIVE_LOW; | |
154 | } | |
155 | break; | |
156 | } | |
157 | } | |
158 | } | |
a603a2b8 LW |
159 | } |
160 | ||
863fbf49 | 161 | /** |
af8b6375 | 162 | * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API |
863fbf49 | 163 | * @np: device node to get GPIO from |
a6b09191 | 164 | * @propname: property name containing gpio specifier(s) |
863fbf49 | 165 | * @index: index of the GPIO |
b908b53d | 166 | * @flags: a flags pointer to fill in |
863fbf49 | 167 | * |
af8b6375 | 168 | * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno |
b908b53d AV |
169 | * value on the error condition. If @flags is not NULL the function also fills |
170 | * in flags for the GPIO. | |
863fbf49 | 171 | */ |
af8b6375 AC |
172 | struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np, |
173 | const char *propname, int index, enum of_gpio_flags *flags) | |
863fbf49 | 174 | { |
762c2e46 MY |
175 | struct of_phandle_args gpiospec; |
176 | struct gpio_chip *chip; | |
177 | struct gpio_desc *desc; | |
64b60e09 | 178 | int ret; |
3d0f7cf0 | 179 | |
c11e6f0f SB |
180 | ret = of_parse_phandle_with_args_map(np, propname, "gpio", index, |
181 | &gpiospec); | |
64b60e09 | 182 | if (ret) { |
7eb6ce2f RH |
183 | pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n", |
184 | __func__, propname, np, index); | |
af8b6375 | 185 | return ERR_PTR(ret); |
863fbf49 AV |
186 | } |
187 | ||
c7e9d398 | 188 | chip = of_find_gpiochip_by_xlate(&gpiospec); |
762c2e46 MY |
189 | if (!chip) { |
190 | desc = ERR_PTR(-EPROBE_DEFER); | |
191 | goto out; | |
192 | } | |
762c2e46 | 193 | |
99468c1a | 194 | desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags); |
762c2e46 MY |
195 | if (IS_ERR(desc)) |
196 | goto out; | |
863fbf49 | 197 | |
605f2d34 | 198 | if (flags) |
89a5e15b | 199 | of_gpio_flags_quirks(np, propname, flags, index); |
a603a2b8 | 200 | |
7eb6ce2f RH |
201 | pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n", |
202 | __func__, propname, np, index, | |
762c2e46 MY |
203 | PTR_ERR_OR_ZERO(desc)); |
204 | ||
205 | out: | |
206 | of_node_put(gpiospec.np); | |
207 | ||
208 | return desc; | |
863fbf49 | 209 | } |
863fbf49 | 210 | |
f01d9075 AC |
211 | int of_get_named_gpio_flags(struct device_node *np, const char *list_name, |
212 | int index, enum of_gpio_flags *flags) | |
213 | { | |
214 | struct gpio_desc *desc; | |
215 | ||
216 | desc = of_get_named_gpiod_flags(np, list_name, index, flags); | |
217 | ||
218 | if (IS_ERR(desc)) | |
219 | return PTR_ERR(desc); | |
220 | else | |
221 | return desc_to_gpio(desc); | |
222 | } | |
223 | EXPORT_SYMBOL(of_get_named_gpio_flags); | |
224 | ||
c8582339 LW |
225 | /* |
226 | * The SPI GPIO bindings happened before we managed to establish that GPIO | |
227 | * properties should be named "foo-gpios" so we have this special kludge for | |
228 | * them. | |
229 | */ | |
230 | static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id, | |
231 | enum of_gpio_flags *of_flags) | |
232 | { | |
233 | char prop_name[32]; /* 32 is max size of property name */ | |
234 | struct device_node *np = dev->of_node; | |
235 | struct gpio_desc *desc; | |
236 | ||
237 | /* | |
238 | * Hopefully the compiler stubs the rest of the function if this | |
239 | * is false. | |
240 | */ | |
241 | if (!IS_ENABLED(CONFIG_SPI_MASTER)) | |
242 | return ERR_PTR(-ENOENT); | |
243 | ||
244 | /* Allow this specifically for "spi-gpio" devices */ | |
245 | if (!of_device_is_compatible(np, "spi-gpio") || !con_id) | |
246 | return ERR_PTR(-ENOENT); | |
247 | ||
248 | /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */ | |
249 | snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id); | |
250 | ||
251 | desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags); | |
252 | return desc; | |
253 | } | |
254 | ||
6a537d48 LW |
255 | /* |
256 | * Some regulator bindings happened before we managed to establish that GPIO | |
257 | * properties should be named "foo-gpios" so we have this special kludge for | |
258 | * them. | |
259 | */ | |
260 | static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id, | |
261 | enum of_gpio_flags *of_flags) | |
262 | { | |
263 | /* These are the connection IDs we accept as legacy GPIO phandles */ | |
264 | const char *whitelist[] = { | |
265 | "wlf,ldoena", /* Arizona */ | |
266 | "wlf,ldo1ena", /* WM8994 */ | |
267 | "wlf,ldo2ena", /* WM8994 */ | |
268 | }; | |
269 | struct device_node *np = dev->of_node; | |
270 | struct gpio_desc *desc; | |
271 | int i; | |
272 | ||
273 | if (!IS_ENABLED(CONFIG_REGULATOR)) | |
274 | return ERR_PTR(-ENOENT); | |
275 | ||
276 | if (!con_id) | |
277 | return ERR_PTR(-ENOENT); | |
278 | ||
4b21f94a AS |
279 | i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id); |
280 | if (i < 0) | |
6a537d48 LW |
281 | return ERR_PTR(-ENOENT); |
282 | ||
283 | desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags); | |
284 | return desc; | |
285 | } | |
286 | ||
ea713bc4 LW |
287 | struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id, |
288 | unsigned int idx, | |
289 | enum gpio_lookup_flags *flags) | |
290 | { | |
291 | char prop_name[32]; /* 32 is max size of property name */ | |
292 | enum of_gpio_flags of_flags; | |
293 | struct gpio_desc *desc; | |
294 | unsigned int i; | |
295 | ||
c8582339 | 296 | /* Try GPIO property "foo-gpios" and "foo-gpio" */ |
ea713bc4 LW |
297 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { |
298 | if (con_id) | |
299 | snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id, | |
300 | gpio_suffixes[i]); | |
301 | else | |
302 | snprintf(prop_name, sizeof(prop_name), "%s", | |
303 | gpio_suffixes[i]); | |
304 | ||
305 | desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx, | |
306 | &of_flags); | |
6662ae6a MR |
307 | /* |
308 | * -EPROBE_DEFER in our case means that we found a | |
309 | * valid GPIO property, but no controller has been | |
310 | * registered so far. | |
311 | * | |
312 | * This means we don't need to look any further for | |
313 | * alternate name conventions, and we should really | |
314 | * preserve the return code for our user to be able to | |
315 | * retry probing later. | |
316 | */ | |
317 | if (IS_ERR(desc) && PTR_ERR(desc) == -EPROBE_DEFER) | |
318 | return desc; | |
319 | ||
ea713bc4 LW |
320 | if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT)) |
321 | break; | |
322 | } | |
323 | ||
c8582339 LW |
324 | /* Special handling for SPI GPIOs if used */ |
325 | if (IS_ERR(desc)) | |
326 | desc = of_find_spi_gpio(dev, con_id, &of_flags); | |
327 | ||
6a537d48 | 328 | /* Special handling for regulator GPIOs if used */ |
ce27fb2c | 329 | if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER) |
6a537d48 LW |
330 | desc = of_find_regulator_gpio(dev, con_id, &of_flags); |
331 | ||
ea713bc4 LW |
332 | if (IS_ERR(desc)) |
333 | return desc; | |
334 | ||
335 | if (of_flags & OF_GPIO_ACTIVE_LOW) | |
336 | *flags |= GPIO_ACTIVE_LOW; | |
337 | ||
338 | if (of_flags & OF_GPIO_SINGLE_ENDED) { | |
4c0facdd | 339 | if (of_flags & OF_GPIO_OPEN_DRAIN) |
ea713bc4 LW |
340 | *flags |= GPIO_OPEN_DRAIN; |
341 | else | |
342 | *flags |= GPIO_OPEN_SOURCE; | |
343 | } | |
344 | ||
e10f72bf AJ |
345 | if (of_flags & OF_GPIO_TRANSITORY) |
346 | *flags |= GPIO_TRANSITORY; | |
05f479bf | 347 | |
ea713bc4 LW |
348 | return desc; |
349 | } | |
350 | ||
f625d460 | 351 | /** |
fd7337fd | 352 | * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API |
f625d460 | 353 | * @np: device node to get GPIO from |
be715343 | 354 | * @chip: GPIO chip whose hog is parsed |
a79fead5 | 355 | * @idx: Index of the GPIO to parse |
f625d460 BP |
356 | * @name: GPIO line name |
357 | * @lflags: gpio_lookup_flags - returned from of_find_gpio() or | |
fd7337fd | 358 | * of_parse_own_gpio() |
f625d460 BP |
359 | * @dflags: gpiod_flags - optional GPIO initialization flags |
360 | * | |
361 | * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno | |
362 | * value on the error condition. | |
363 | */ | |
fd7337fd | 364 | static struct gpio_desc *of_parse_own_gpio(struct device_node *np, |
be715343 | 365 | struct gpio_chip *chip, |
a79fead5 | 366 | unsigned int idx, const char **name, |
fd7337fd MP |
367 | enum gpio_lookup_flags *lflags, |
368 | enum gpiod_flags *dflags) | |
f625d460 BP |
369 | { |
370 | struct device_node *chip_np; | |
371 | enum of_gpio_flags xlate_flags; | |
be715343 MY |
372 | struct of_phandle_args gpiospec; |
373 | struct gpio_desc *desc; | |
a79fead5 | 374 | unsigned int i; |
f625d460 | 375 | u32 tmp; |
3f9547e1 | 376 | int ret; |
f625d460 | 377 | |
be715343 | 378 | chip_np = chip->of_node; |
f625d460 BP |
379 | if (!chip_np) |
380 | return ERR_PTR(-EINVAL); | |
381 | ||
382 | xlate_flags = 0; | |
383 | *lflags = 0; | |
384 | *dflags = 0; | |
385 | ||
386 | ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp); | |
387 | if (ret) | |
388 | return ERR_PTR(ret); | |
389 | ||
be715343 MY |
390 | gpiospec.np = chip_np; |
391 | gpiospec.args_count = tmp; | |
f625d460 | 392 | |
a79fead5 GU |
393 | for (i = 0; i < tmp; i++) { |
394 | ret = of_property_read_u32_index(np, "gpios", idx * tmp + i, | |
395 | &gpiospec.args[i]); | |
396 | if (ret) | |
397 | return ERR_PTR(ret); | |
398 | } | |
f625d460 | 399 | |
99468c1a | 400 | desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags); |
be715343 MY |
401 | if (IS_ERR(desc)) |
402 | return desc; | |
f625d460 BP |
403 | |
404 | if (xlate_flags & OF_GPIO_ACTIVE_LOW) | |
405 | *lflags |= GPIO_ACTIVE_LOW; | |
e10f72bf AJ |
406 | if (xlate_flags & OF_GPIO_TRANSITORY) |
407 | *lflags |= GPIO_TRANSITORY; | |
f625d460 BP |
408 | |
409 | if (of_property_read_bool(np, "input")) | |
410 | *dflags |= GPIOD_IN; | |
411 | else if (of_property_read_bool(np, "output-low")) | |
412 | *dflags |= GPIOD_OUT_LOW; | |
413 | else if (of_property_read_bool(np, "output-high")) | |
414 | *dflags |= GPIOD_OUT_HIGH; | |
415 | else { | |
62cdcb6c RH |
416 | pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n", |
417 | desc_to_gpio(desc), np); | |
f625d460 BP |
418 | return ERR_PTR(-EINVAL); |
419 | } | |
420 | ||
421 | if (name && of_property_read_string(np, "line-name", name)) | |
422 | *name = np->name; | |
423 | ||
be715343 | 424 | return desc; |
f625d460 BP |
425 | } |
426 | ||
427 | /** | |
fd7337fd | 428 | * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions |
f625d460 BP |
429 | * @chip: gpio chip to act on |
430 | * | |
431 | * This is only used by of_gpiochip_add to request/set GPIO initial | |
432 | * configuration. | |
ead066e6 | 433 | * It returns error if it fails otherwise 0 on success. |
f625d460 | 434 | */ |
dfbd379b | 435 | static int of_gpiochip_scan_gpios(struct gpio_chip *chip) |
f625d460 BP |
436 | { |
437 | struct gpio_desc *desc = NULL; | |
438 | struct device_node *np; | |
439 | const char *name; | |
440 | enum gpio_lookup_flags lflags; | |
441 | enum gpiod_flags dflags; | |
a79fead5 | 442 | unsigned int i; |
dfbd379b | 443 | int ret; |
f625d460 | 444 | |
d1279d94 | 445 | for_each_available_child_of_node(chip->of_node, np) { |
f625d460 BP |
446 | if (!of_property_read_bool(np, "gpio-hog")) |
447 | continue; | |
448 | ||
a79fead5 GU |
449 | for (i = 0;; i++) { |
450 | desc = of_parse_own_gpio(np, chip, i, &name, &lflags, | |
451 | &dflags); | |
452 | if (IS_ERR(desc)) | |
453 | break; | |
454 | ||
455 | ret = gpiod_hog(desc, name, lflags, dflags); | |
456 | if (ret < 0) { | |
457 | of_node_put(np); | |
458 | return ret; | |
459 | } | |
09e258af | 460 | } |
f625d460 | 461 | } |
dfbd379b LD |
462 | |
463 | return 0; | |
f625d460 BP |
464 | } |
465 | ||
863fbf49 | 466 | /** |
67049c50 | 467 | * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags |
a19e3da5 | 468 | * @gc: pointer to the gpio_chip structure |
67049c50 | 469 | * @gpiospec: GPIO specifier as found in the device tree |
b908b53d | 470 | * @flags: a flags pointer to fill in |
863fbf49 AV |
471 | * |
472 | * This is simple translation function, suitable for the most 1:1 mapped | |
67049c50 | 473 | * GPIO chips. This function performs only one sanity check: whether GPIO |
863fbf49 AV |
474 | * is less than ngpios (that is specified in the gpio_chip). |
475 | */ | |
15c9a0ac GL |
476 | int of_gpio_simple_xlate(struct gpio_chip *gc, |
477 | const struct of_phandle_args *gpiospec, u32 *flags) | |
863fbf49 | 478 | { |
b908b53d AV |
479 | /* |
480 | * We're discouraging gpio_cells < 2, since that way you'll have to | |
20a8a968 | 481 | * write your own xlate function (that will have to retrieve the GPIO |
b908b53d AV |
482 | * number and the flags from a single gpio cell -- this is possible, |
483 | * but not recommended). | |
484 | */ | |
a19e3da5 | 485 | if (gc->of_gpio_n_cells < 2) { |
b908b53d AV |
486 | WARN_ON(1); |
487 | return -EINVAL; | |
488 | } | |
489 | ||
15c9a0ac GL |
490 | if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells)) |
491 | return -EINVAL; | |
492 | ||
6270d830 | 493 | if (gpiospec->args[0] >= gc->ngpio) |
863fbf49 AV |
494 | return -EINVAL; |
495 | ||
b908b53d | 496 | if (flags) |
15c9a0ac | 497 | *flags = gpiospec->args[1]; |
b908b53d | 498 | |
15c9a0ac | 499 | return gpiospec->args[0]; |
863fbf49 | 500 | } |
3038bbdf | 501 | EXPORT_SYMBOL(of_gpio_simple_xlate); |
863fbf49 | 502 | |
863fbf49 | 503 | /** |
3208b0f0 | 504 | * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank) |
863fbf49 AV |
505 | * @np: device node of the GPIO chip |
506 | * @mm_gc: pointer to the of_mm_gpio_chip allocated structure | |
3208b0f0 | 507 | * @data: driver data to store in the struct gpio_chip |
863fbf49 AV |
508 | * |
509 | * To use this function you should allocate and fill mm_gc with: | |
510 | * | |
511 | * 1) In the gpio_chip structure: | |
512 | * - all the callbacks | |
a19e3da5 AV |
513 | * - of_gpio_n_cells |
514 | * - of_xlate callback (optional) | |
863fbf49 AV |
515 | * |
516 | * 3) In the of_mm_gpio_chip structure: | |
517 | * - save_regs callback (optional) | |
518 | * | |
519 | * If succeeded, this function will map bank's memory and will | |
520 | * do all necessary work for you. Then you'll able to use .regs | |
521 | * to manage GPIOs from the callbacks. | |
522 | */ | |
3208b0f0 LW |
523 | int of_mm_gpiochip_add_data(struct device_node *np, |
524 | struct of_mm_gpio_chip *mm_gc, | |
525 | void *data) | |
863fbf49 AV |
526 | { |
527 | int ret = -ENOMEM; | |
a19e3da5 | 528 | struct gpio_chip *gc = &mm_gc->gc; |
863fbf49 | 529 | |
7eb6ce2f | 530 | gc->label = kasprintf(GFP_KERNEL, "%pOF", np); |
863fbf49 AV |
531 | if (!gc->label) |
532 | goto err0; | |
533 | ||
534 | mm_gc->regs = of_iomap(np, 0); | |
535 | if (!mm_gc->regs) | |
536 | goto err1; | |
537 | ||
21451155 | 538 | gc->base = -1; |
863fbf49 | 539 | |
863fbf49 AV |
540 | if (mm_gc->save_regs) |
541 | mm_gc->save_regs(mm_gc); | |
542 | ||
a19e3da5 | 543 | mm_gc->gc.of_node = np; |
863fbf49 | 544 | |
3208b0f0 | 545 | ret = gpiochip_add_data(gc, data); |
863fbf49 AV |
546 | if (ret) |
547 | goto err2; | |
548 | ||
863fbf49 AV |
549 | return 0; |
550 | err2: | |
863fbf49 AV |
551 | iounmap(mm_gc->regs); |
552 | err1: | |
553 | kfree(gc->label); | |
554 | err0: | |
7eb6ce2f | 555 | pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret); |
863fbf49 AV |
556 | return ret; |
557 | } | |
3208b0f0 | 558 | EXPORT_SYMBOL(of_mm_gpiochip_add_data); |
594fa265 | 559 | |
d621e8ba RRD |
560 | /** |
561 | * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank) | |
562 | * @mm_gc: pointer to the of_mm_gpio_chip allocated structure | |
563 | */ | |
564 | void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc) | |
565 | { | |
566 | struct gpio_chip *gc = &mm_gc->gc; | |
567 | ||
568 | if (!mm_gc) | |
569 | return; | |
570 | ||
571 | gpiochip_remove(gc); | |
572 | iounmap(mm_gc->regs); | |
573 | kfree(gc->label); | |
574 | } | |
575 | EXPORT_SYMBOL(of_mm_gpiochip_remove); | |
576 | ||
726cb3ba SB |
577 | static void of_gpiochip_init_valid_mask(struct gpio_chip *chip) |
578 | { | |
579 | int len, i; | |
580 | u32 start, count; | |
581 | struct device_node *np = chip->of_node; | |
582 | ||
583 | len = of_property_count_u32_elems(np, "gpio-reserved-ranges"); | |
584 | if (len < 0 || len % 2 != 0) | |
585 | return; | |
586 | ||
587 | for (i = 0; i < len; i += 2) { | |
588 | of_property_read_u32_index(np, "gpio-reserved-ranges", | |
589 | i, &start); | |
590 | of_property_read_u32_index(np, "gpio-reserved-ranges", | |
591 | i + 1, &count); | |
592 | if (start >= chip->ngpio || start + count >= chip->ngpio) | |
593 | continue; | |
594 | ||
595 | bitmap_clear(chip->valid_mask, start, count); | |
596 | } | |
597 | }; | |
598 | ||
f23f1516 | 599 | #ifdef CONFIG_PINCTRL |
28355f81 | 600 | static int of_gpiochip_add_pin_range(struct gpio_chip *chip) |
f23f1516 SH |
601 | { |
602 | struct device_node *np = chip->of_node; | |
f23f1516 | 603 | struct of_phandle_args pinspec; |
1e63d7b9 | 604 | struct pinctrl_dev *pctldev; |
f23f1516 | 605 | int index = 0, ret; |
586a87e6 CR |
606 | const char *name; |
607 | static const char group_names_propname[] = "gpio-ranges-group-names"; | |
608 | struct property *group_names; | |
f23f1516 SH |
609 | |
610 | if (!np) | |
28355f81 | 611 | return 0; |
f23f1516 | 612 | |
586a87e6 CR |
613 | group_names = of_find_property(np, group_names_propname, NULL); |
614 | ||
ad4e1a7c | 615 | for (;; index++) { |
d9fe0039 SW |
616 | ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, |
617 | index, &pinspec); | |
f23f1516 SH |
618 | if (ret) |
619 | break; | |
620 | ||
1e63d7b9 | 621 | pctldev = of_pinctrl_get(pinspec.np); |
602cf638 | 622 | of_node_put(pinspec.np); |
1e63d7b9 | 623 | if (!pctldev) |
28355f81 | 624 | return -EPROBE_DEFER; |
f23f1516 | 625 | |
586a87e6 CR |
626 | if (pinspec.args[2]) { |
627 | if (group_names) { | |
72858602 | 628 | of_property_read_string_index(np, |
586a87e6 CR |
629 | group_names_propname, |
630 | index, &name); | |
631 | if (strlen(name)) { | |
7eb6ce2f RH |
632 | pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n", |
633 | np); | |
586a87e6 CR |
634 | break; |
635 | } | |
636 | } | |
637 | /* npins != 0: linear range */ | |
638 | ret = gpiochip_add_pin_range(chip, | |
639 | pinctrl_dev_get_devname(pctldev), | |
640 | pinspec.args[0], | |
641 | pinspec.args[1], | |
642 | pinspec.args[2]); | |
643 | if (ret) | |
28355f81 | 644 | return ret; |
586a87e6 CR |
645 | } else { |
646 | /* npins == 0: special range */ | |
647 | if (pinspec.args[1]) { | |
7eb6ce2f RH |
648 | pr_err("%pOF: Illegal gpio-range format.\n", |
649 | np); | |
586a87e6 CR |
650 | break; |
651 | } | |
652 | ||
653 | if (!group_names) { | |
7eb6ce2f RH |
654 | pr_err("%pOF: GPIO group range requested but no %s property.\n", |
655 | np, group_names_propname); | |
586a87e6 CR |
656 | break; |
657 | } | |
658 | ||
659 | ret = of_property_read_string_index(np, | |
660 | group_names_propname, | |
661 | index, &name); | |
662 | if (ret) | |
663 | break; | |
664 | ||
665 | if (!strlen(name)) { | |
7eb6ce2f RH |
666 | pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n", |
667 | np); | |
586a87e6 CR |
668 | break; |
669 | } | |
670 | ||
671 | ret = gpiochip_add_pingroup_range(chip, pctldev, | |
672 | pinspec.args[0], name); | |
673 | if (ret) | |
28355f81 | 674 | return ret; |
586a87e6 | 675 | } |
ad4e1a7c | 676 | } |
28355f81 TV |
677 | |
678 | return 0; | |
f23f1516 SH |
679 | } |
680 | ||
f23f1516 | 681 | #else |
28355f81 | 682 | static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; } |
f23f1516 SH |
683 | #endif |
684 | ||
28355f81 | 685 | int of_gpiochip_add(struct gpio_chip *chip) |
391c970c | 686 | { |
28355f81 TV |
687 | int status; |
688 | ||
391c970c | 689 | if (!chip->of_node) |
28355f81 | 690 | return 0; |
391c970c AV |
691 | |
692 | if (!chip->of_xlate) { | |
693 | chip->of_gpio_n_cells = 2; | |
694 | chip->of_xlate = of_gpio_simple_xlate; | |
695 | } | |
696 | ||
1020dfd1 MY |
697 | if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS) |
698 | return -EINVAL; | |
699 | ||
726cb3ba SB |
700 | of_gpiochip_init_valid_mask(chip); |
701 | ||
28355f81 TV |
702 | status = of_gpiochip_add_pin_range(chip); |
703 | if (status) | |
704 | return status; | |
705 | ||
fd9c5531 LW |
706 | /* If the chip defines names itself, these take precedence */ |
707 | if (!chip->names) | |
82270335 CL |
708 | devprop_gpiochip_set_names(chip, |
709 | of_fwnode_handle(chip->of_node)); | |
fd9c5531 | 710 | |
391c970c | 711 | of_node_get(chip->of_node); |
f625d460 | 712 | |
dfbd379b | 713 | return of_gpiochip_scan_gpios(chip); |
391c970c AV |
714 | } |
715 | ||
716 | void of_gpiochip_remove(struct gpio_chip *chip) | |
717 | { | |
e93fa3f2 | 718 | gpiochip_remove_pin_ranges(chip); |
8a691550 | 719 | of_node_put(chip->of_node); |
391c970c | 720 | } |