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 AV |
12 | #include <linux/errno.h> |
13 | #include <linux/io.h> | |
380c7ba3 | 14 | #include <linux/module.h> |
863fbf49 | 15 | #include <linux/of.h> |
2e13cba8 | 16 | #include <linux/of_address.h> |
863fbf49 | 17 | #include <linux/of_gpio.h> |
f23f1516 | 18 | #include <linux/pinctrl/pinctrl.h> |
2e13cba8 | 19 | #include <linux/slab.h> |
380c7ba3 AS |
20 | #include <linux/string.h> |
21 | ||
22 | #include <linux/gpio/consumer.h> | |
f625d460 | 23 | #include <linux/gpio/machine.h> |
863fbf49 | 24 | |
1bd6b601 | 25 | #include "gpiolib.h" |
f626d6df LW |
26 | #include "gpiolib-of.h" |
27 | ||
eed5a3bf AS |
28 | /* |
29 | * This is Linux-specific flags. By default controllers' and Linux' mapping | |
30 | * match, but GPIO controllers are free to translate their own flags to | |
31 | * Linux-specific in their .xlate callback. Though, 1:1 mapping is recommended. | |
32 | */ | |
33 | enum of_gpio_flags { | |
34 | OF_GPIO_ACTIVE_LOW = 0x1, | |
35 | OF_GPIO_SINGLE_ENDED = 0x2, | |
36 | OF_GPIO_OPEN_DRAIN = 0x4, | |
37 | OF_GPIO_TRANSITORY = 0x8, | |
38 | OF_GPIO_PULL_UP = 0x10, | |
39 | OF_GPIO_PULL_DOWN = 0x20, | |
40 | OF_GPIO_PULL_DISABLE = 0x40, | |
41 | }; | |
42 | ||
c7835652 DT |
43 | /** |
44 | * of_gpio_named_count() - Count GPIOs for a device | |
45 | * @np: device node to count GPIOs for | |
46 | * @propname: property name containing gpio specifier(s) | |
47 | * | |
48 | * The function returns the count of GPIOs specified for a node. | |
49 | * Note that the empty GPIO specifiers count too. Returns either | |
50 | * Number of gpios defined in property, | |
51 | * -EINVAL for an incorrectly formed gpios property, or | |
52 | * -ENOENT for a missing gpios property | |
53 | * | |
54 | * Example: | |
55 | * gpios = <0 | |
56 | * &gpio1 1 2 | |
57 | * 0 | |
58 | * &gpio2 3 4>; | |
59 | * | |
60 | * The above example defines four GPIOs, two of which are not specified. | |
61 | * This function will return '4' | |
62 | */ | |
63 | static int of_gpio_named_count(const struct device_node *np, | |
64 | const char *propname) | |
65 | { | |
66 | return of_count_phandle_with_args(np, propname, "#gpio-cells"); | |
67 | } | |
68 | ||
71b8f600 LW |
69 | /** |
70 | * of_gpio_spi_cs_get_count() - special GPIO counting for SPI | |
c5a66b97 LJ |
71 | * @dev: Consuming device |
72 | * @con_id: Function within the GPIO consumer | |
73 | * | |
71b8f600 | 74 | * Some elder GPIO controllers need special quirks. Currently we handle |
47267732 | 75 | * the Freescale and PPC GPIO controller with bindings that doesn't use the |
71b8f600 LW |
76 | * established "cs-gpios" for chip selects but instead rely on |
77 | * "gpios" for the chip select lines. If we detect this, we redirect | |
78 | * the counting of "cs-gpios" to count "gpios" transparent to the | |
79 | * driver. | |
80 | */ | |
a1f4c96b | 81 | static int of_gpio_spi_cs_get_count(struct device *dev, const char *con_id) |
71b8f600 LW |
82 | { |
83 | struct device_node *np = dev->of_node; | |
84 | ||
85 | if (!IS_ENABLED(CONFIG_SPI_MASTER)) | |
86 | return 0; | |
87 | if (!con_id || strcmp(con_id, "cs")) | |
88 | return 0; | |
89 | if (!of_device_is_compatible(np, "fsl,spi") && | |
47267732 LW |
90 | !of_device_is_compatible(np, "aeroflexgaisler,spictrl") && |
91 | !of_device_is_compatible(np, "ibm,ppc4xx-spi")) | |
71b8f600 LW |
92 | return 0; |
93 | return of_gpio_named_count(np, "gpios"); | |
94 | } | |
95 | ||
f626d6df LW |
96 | int of_gpio_get_count(struct device *dev, const char *con_id) |
97 | { | |
98 | int ret; | |
99 | char propname[32]; | |
100 | unsigned int i; | |
101 | ||
71b8f600 LW |
102 | ret = of_gpio_spi_cs_get_count(dev, con_id); |
103 | if (ret > 0) | |
104 | return ret; | |
105 | ||
f626d6df LW |
106 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { |
107 | if (con_id) | |
108 | snprintf(propname, sizeof(propname), "%s-%s", | |
109 | con_id, gpio_suffixes[i]); | |
110 | else | |
111 | snprintf(propname, sizeof(propname), "%s", | |
112 | gpio_suffixes[i]); | |
113 | ||
114 | ret = of_gpio_named_count(dev->of_node, propname); | |
115 | if (ret > 0) | |
116 | break; | |
117 | } | |
118 | return ret ? ret : -ENOENT; | |
119 | } | |
af8b6375 | 120 | |
c7e9d398 | 121 | static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data) |
3d0f7cf0 | 122 | { |
c7e9d398 MY |
123 | struct of_phandle_args *gpiospec = data; |
124 | ||
d59fdbc7 | 125 | return device_match_of_node(&chip->gpiodev->dev, gpiospec->np) && |
d49b48f0 | 126 | chip->of_xlate && |
c7e9d398 | 127 | chip->of_xlate(chip, gpiospec, NULL) >= 0; |
762c2e46 | 128 | } |
3d0f7cf0 | 129 | |
c7e9d398 MY |
130 | static struct gpio_chip *of_find_gpiochip_by_xlate( |
131 | struct of_phandle_args *gpiospec) | |
762c2e46 | 132 | { |
c7e9d398 | 133 | return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate); |
3d0f7cf0 | 134 | } |
3d0f7cf0 | 135 | |
99468c1a MY |
136 | static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip, |
137 | struct of_phandle_args *gpiospec, | |
138 | enum of_gpio_flags *flags) | |
3d0f7cf0 | 139 | { |
3d0f7cf0 GL |
140 | int ret; |
141 | ||
99468c1a MY |
142 | if (chip->of_gpio_n_cells != gpiospec->args_count) |
143 | return ERR_PTR(-EINVAL); | |
144 | ||
145 | ret = chip->of_xlate(chip, gpiospec, flags); | |
146 | if (ret < 0) | |
147 | return ERR_PTR(ret); | |
148 | ||
149 | return gpiochip_get_desc(chip, ret); | |
3d0f7cf0 GL |
150 | } |
151 | ||
e3186e36 DT |
152 | /* |
153 | * Overrides stated polarity of a gpio line and warns when there is a | |
154 | * discrepancy. | |
155 | */ | |
156 | static void of_gpio_quirk_polarity(const struct device_node *np, | |
157 | bool active_high, | |
158 | enum of_gpio_flags *flags) | |
159 | { | |
160 | if (active_high) { | |
161 | if (*flags & OF_GPIO_ACTIVE_LOW) { | |
162 | pr_warn("%s GPIO handle specifies active low - ignored\n", | |
163 | of_node_full_name(np)); | |
164 | *flags &= ~OF_GPIO_ACTIVE_LOW; | |
165 | } | |
166 | } else { | |
167 | if (!(*flags & OF_GPIO_ACTIVE_LOW)) | |
168 | pr_info("%s enforce active low on GPIO handle\n", | |
169 | of_node_full_name(np)); | |
170 | *flags |= OF_GPIO_ACTIVE_LOW; | |
171 | } | |
172 | } | |
173 | ||
99d18d42 DT |
174 | /* |
175 | * This quirk does static polarity overrides in cases where existing | |
176 | * DTS specified incorrect polarity. | |
177 | */ | |
178 | static void of_gpio_try_fixup_polarity(const struct device_node *np, | |
179 | const char *propname, | |
180 | enum of_gpio_flags *flags) | |
181 | { | |
182 | static const struct { | |
183 | const char *compatible; | |
184 | const char *propname; | |
185 | bool active_high; | |
186 | } gpios[] = { | |
187 | #if !IS_ENABLED(CONFIG_LCD_HX8357) | |
188 | /* | |
189 | * Himax LCD controllers used incorrectly named | |
190 | * "gpios-reset" property and also specified wrong | |
191 | * polarity. | |
192 | */ | |
193 | { "himax,hx8357", "gpios-reset", false }, | |
194 | { "himax,hx8369", "gpios-reset", false }, | |
195 | #endif | |
196 | }; | |
197 | unsigned int i; | |
198 | ||
199 | for (i = 0; i < ARRAY_SIZE(gpios); i++) { | |
200 | if (of_device_is_compatible(np, gpios[i].compatible) && | |
201 | !strcmp(propname, gpios[i].propname)) { | |
202 | of_gpio_quirk_polarity(np, gpios[i].active_high, flags); | |
203 | break; | |
204 | } | |
205 | } | |
206 | } | |
207 | ||
34cb9352 DT |
208 | static void of_gpio_set_polarity_by_property(const struct device_node *np, |
209 | const char *propname, | |
210 | enum of_gpio_flags *flags) | |
a603a2b8 | 211 | { |
98ac9e4f BG |
212 | const struct device_node *np_compat = np; |
213 | const struct device_node *np_propname = np; | |
34cb9352 DT |
214 | static const struct { |
215 | const char *compatible; | |
216 | const char *gpio_propname; | |
217 | const char *polarity_propname; | |
218 | } gpios[] = { | |
219 | #if IS_ENABLED(CONFIG_FEC) | |
220 | /* Freescale Fast Ethernet Controller */ | |
221 | { "fsl,imx25-fec", "phy-reset-gpios", "phy-reset-active-high" }, | |
222 | { "fsl,imx27-fec", "phy-reset-gpios", "phy-reset-active-high" }, | |
223 | { "fsl,imx28-fec", "phy-reset-gpios", "phy-reset-active-high" }, | |
224 | { "fsl,imx6q-fec", "phy-reset-gpios", "phy-reset-active-high" }, | |
225 | { "fsl,mvf600-fec", "phy-reset-gpios", "phy-reset-active-high" }, | |
226 | { "fsl,imx6sx-fec", "phy-reset-gpios", "phy-reset-active-high" }, | |
227 | { "fsl,imx6ul-fec", "phy-reset-gpios", "phy-reset-active-high" }, | |
228 | { "fsl,imx8mq-fec", "phy-reset-gpios", "phy-reset-active-high" }, | |
229 | { "fsl,imx8qm-fec", "phy-reset-gpios", "phy-reset-active-high" }, | |
230 | { "fsl,s32v234-fec", "phy-reset-gpios", "phy-reset-active-high" }, | |
231 | #endif | |
b8b80348 DT |
232 | #if IS_ENABLED(CONFIG_PCI_IMX6) |
233 | { "fsl,imx6q-pcie", "reset-gpio", "reset-gpio-active-high" }, | |
234 | { "fsl,imx6sx-pcie", "reset-gpio", "reset-gpio-active-high" }, | |
235 | { "fsl,imx6qp-pcie", "reset-gpio", "reset-gpio-active-high" }, | |
236 | { "fsl,imx7d-pcie", "reset-gpio", "reset-gpio-active-high" }, | |
237 | { "fsl,imx8mq-pcie", "reset-gpio", "reset-gpio-active-high" }, | |
238 | { "fsl,imx8mm-pcie", "reset-gpio", "reset-gpio-active-high" }, | |
239 | { "fsl,imx8mp-pcie", "reset-gpio", "reset-gpio-active-high" }, | |
240 | #endif | |
99d18d42 | 241 | |
a603a2b8 LW |
242 | /* |
243 | * The regulator GPIO handles are specified such that the | |
244 | * presence or absence of "enable-active-high" solely controls | |
245 | * the polarity of the GPIO line. Any phandle flags must | |
246 | * be actively ignored. | |
247 | */ | |
34cb9352 DT |
248 | #if IS_ENABLED(CONFIG_REGULATOR_FIXED_VOLTAGE) |
249 | { "regulator-fixed", "gpios", "enable-active-high" }, | |
250 | { "regulator-fixed", "gpio", "enable-active-high" }, | |
251 | { "reg-fixed-voltage", "gpios", "enable-active-high" }, | |
252 | { "reg-fixed-voltage", "gpio", "enable-active-high" }, | |
253 | #endif | |
254 | #if IS_ENABLED(CONFIG_REGULATOR_GPIO) | |
255 | { "regulator-gpio", "enable-gpio", "enable-active-high" }, | |
256 | { "regulator-gpio", "enable-gpios", "enable-active-high" }, | |
98ac9e4f BG |
257 | #endif |
258 | #if IS_ENABLED(CONFIG_MMC_ATMELMCI) | |
259 | { "atmel,hsmci", "cd-gpios", "cd-inverted" }, | |
34cb9352 DT |
260 | #endif |
261 | }; | |
262 | unsigned int i; | |
263 | bool active_high; | |
264 | ||
98ac9e4f BG |
265 | #if IS_ENABLED(CONFIG_MMC_ATMELMCI) |
266 | /* | |
267 | * The Atmel HSMCI has compatible property in the parent node and | |
268 | * gpio property in a child node | |
269 | */ | |
270 | if (of_device_is_compatible(np->parent, "atmel,hsmci")) { | |
271 | np_compat = np->parent; | |
272 | np_propname = np; | |
273 | } | |
274 | #endif | |
275 | ||
34cb9352 | 276 | for (i = 0; i < ARRAY_SIZE(gpios); i++) { |
98ac9e4f | 277 | if (of_device_is_compatible(np_compat, gpios[i].compatible) && |
34cb9352 | 278 | !strcmp(propname, gpios[i].gpio_propname)) { |
98ac9e4f | 279 | active_high = of_property_read_bool(np_propname, |
34cb9352 DT |
280 | gpios[i].polarity_propname); |
281 | of_gpio_quirk_polarity(np, active_high, flags); | |
282 | break; | |
283 | } | |
a603a2b8 | 284 | } |
34cb9352 DT |
285 | } |
286 | ||
287 | static void of_gpio_flags_quirks(const struct device_node *np, | |
288 | const char *propname, | |
289 | enum of_gpio_flags *flags, | |
290 | int index) | |
291 | { | |
292 | of_gpio_try_fixup_polarity(np, propname, flags); | |
293 | of_gpio_set_polarity_by_property(np, propname, flags); | |
294 | ||
a603a2b8 LW |
295 | /* |
296 | * Legacy open drain handling for fixed voltage regulators. | |
297 | */ | |
298 | if (IS_ENABLED(CONFIG_REGULATOR) && | |
299 | of_device_is_compatible(np, "reg-fixed-voltage") && | |
300 | of_property_read_bool(np, "gpio-open-drain")) { | |
301 | *flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN); | |
302 | pr_info("%s uses legacy open drain flag - update the DTS if you can\n", | |
303 | of_node_full_name(np)); | |
304 | } | |
6953c57a LW |
305 | |
306 | /* | |
307 | * Legacy handling of SPI active high chip select. If we have a | |
308 | * property named "cs-gpios" we need to inspect the child node | |
309 | * to determine if the flags should have inverted semantics. | |
310 | */ | |
da7f1349 | 311 | if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") && |
a71a81e7 | 312 | of_property_read_bool(np, "cs-gpios")) { |
6953c57a LW |
313 | struct device_node *child; |
314 | u32 cs; | |
315 | int ret; | |
316 | ||
317 | for_each_child_of_node(np, child) { | |
318 | ret = of_property_read_u32(child, "reg", &cs); | |
c1c04cea | 319 | if (ret) |
6953c57a LW |
320 | continue; |
321 | if (cs == index) { | |
322 | /* | |
323 | * SPI children have active low chip selects | |
324 | * by default. This can be specified negatively | |
325 | * by just omitting "spi-cs-high" in the | |
326 | * device node, or actively by tagging on | |
327 | * GPIO_ACTIVE_LOW as flag in the device | |
328 | * tree. If the line is simultaneously | |
329 | * tagged as active low in the device tree | |
330 | * and has the "spi-cs-high" set, we get a | |
331 | * conflict and the "spi-cs-high" flag will | |
332 | * take precedence. | |
333 | */ | |
e3186e36 DT |
334 | bool active_high = of_property_read_bool(child, |
335 | "spi-cs-high"); | |
336 | of_gpio_quirk_polarity(child, active_high, | |
337 | flags); | |
89fea04c | 338 | of_node_put(child); |
6953c57a LW |
339 | break; |
340 | } | |
341 | } | |
342 | } | |
edc1ef3f MB |
343 | |
344 | /* Legacy handling of stmmac's active-low PHY reset line */ | |
345 | if (IS_ENABLED(CONFIG_STMMAC_ETH) && | |
346 | !strcmp(propname, "snps,reset-gpio") && | |
347 | of_property_read_bool(np, "snps,reset-active-low")) | |
348 | *flags |= OF_GPIO_ACTIVE_LOW; | |
a603a2b8 LW |
349 | } |
350 | ||
863fbf49 | 351 | /** |
af8b6375 | 352 | * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API |
863fbf49 | 353 | * @np: device node to get GPIO from |
a6b09191 | 354 | * @propname: property name containing gpio specifier(s) |
863fbf49 | 355 | * @index: index of the GPIO |
b908b53d | 356 | * @flags: a flags pointer to fill in |
863fbf49 | 357 | * |
af8b6375 | 358 | * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno |
b908b53d AV |
359 | * value on the error condition. If @flags is not NULL the function also fills |
360 | * in flags for the GPIO. | |
863fbf49 | 361 | */ |
e6ae9a83 | 362 | static struct gpio_desc *of_get_named_gpiod_flags(const struct device_node *np, |
af8b6375 | 363 | const char *propname, int index, enum of_gpio_flags *flags) |
863fbf49 | 364 | { |
762c2e46 MY |
365 | struct of_phandle_args gpiospec; |
366 | struct gpio_chip *chip; | |
367 | struct gpio_desc *desc; | |
64b60e09 | 368 | int ret; |
3d0f7cf0 | 369 | |
c11e6f0f SB |
370 | ret = of_parse_phandle_with_args_map(np, propname, "gpio", index, |
371 | &gpiospec); | |
64b60e09 | 372 | if (ret) { |
7eb6ce2f RH |
373 | pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n", |
374 | __func__, propname, np, index); | |
af8b6375 | 375 | return ERR_PTR(ret); |
863fbf49 AV |
376 | } |
377 | ||
c7e9d398 | 378 | chip = of_find_gpiochip_by_xlate(&gpiospec); |
762c2e46 MY |
379 | if (!chip) { |
380 | desc = ERR_PTR(-EPROBE_DEFER); | |
381 | goto out; | |
382 | } | |
762c2e46 | 383 | |
99468c1a | 384 | desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags); |
762c2e46 MY |
385 | if (IS_ERR(desc)) |
386 | goto out; | |
863fbf49 | 387 | |
605f2d34 | 388 | if (flags) |
89a5e15b | 389 | of_gpio_flags_quirks(np, propname, flags, index); |
a603a2b8 | 390 | |
7eb6ce2f RH |
391 | pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n", |
392 | __func__, propname, np, index, | |
762c2e46 MY |
393 | PTR_ERR_OR_ZERO(desc)); |
394 | ||
395 | out: | |
396 | of_node_put(gpiospec.np); | |
397 | ||
398 | return desc; | |
863fbf49 | 399 | } |
863fbf49 | 400 | |
40fc56ee DT |
401 | /** |
402 | * of_get_named_gpio() - Get a GPIO number to use with GPIO API | |
403 | * @np: device node to get GPIO from | |
404 | * @propname: Name of property containing gpio specifier(s) | |
405 | * @index: index of the GPIO | |
406 | * | |
407 | * Returns GPIO number to use with Linux generic GPIO API, or one of the errno | |
408 | * value on the error condition. | |
409 | */ | |
410 | int of_get_named_gpio(const struct device_node *np, const char *propname, | |
411 | int index) | |
f01d9075 AC |
412 | { |
413 | struct gpio_desc *desc; | |
414 | ||
40fc56ee | 415 | desc = of_get_named_gpiod_flags(np, propname, index, NULL); |
f01d9075 AC |
416 | |
417 | if (IS_ERR(desc)) | |
418 | return PTR_ERR(desc); | |
419 | else | |
420 | return desc_to_gpio(desc); | |
421 | } | |
40fc56ee | 422 | EXPORT_SYMBOL_GPL(of_get_named_gpio); |
f01d9075 | 423 | |
d9e7f0e3 DT |
424 | /* Converts gpio_lookup_flags into bitmask of GPIO_* values */ |
425 | static unsigned long of_convert_gpio_flags(enum of_gpio_flags flags) | |
426 | { | |
427 | unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT; | |
428 | ||
429 | if (flags & OF_GPIO_ACTIVE_LOW) | |
430 | lflags |= GPIO_ACTIVE_LOW; | |
431 | ||
432 | if (flags & OF_GPIO_SINGLE_ENDED) { | |
433 | if (flags & OF_GPIO_OPEN_DRAIN) | |
434 | lflags |= GPIO_OPEN_DRAIN; | |
435 | else | |
436 | lflags |= GPIO_OPEN_SOURCE; | |
437 | } | |
438 | ||
439 | if (flags & OF_GPIO_TRANSITORY) | |
440 | lflags |= GPIO_TRANSITORY; | |
441 | ||
442 | if (flags & OF_GPIO_PULL_UP) | |
443 | lflags |= GPIO_PULL_UP; | |
444 | ||
445 | if (flags & OF_GPIO_PULL_DOWN) | |
446 | lflags |= GPIO_PULL_DOWN; | |
447 | ||
448 | if (flags & OF_GPIO_PULL_DISABLE) | |
449 | lflags |= GPIO_PULL_DISABLE; | |
450 | ||
451 | return lflags; | |
452 | } | |
453 | ||
b311c5cb | 454 | static struct gpio_desc *of_find_gpio_rename(struct device_node *np, |
e3023bf8 LW |
455 | const char *con_id, |
456 | unsigned int idx, | |
984914ec | 457 | enum of_gpio_flags *of_flags) |
e3023bf8 | 458 | { |
b311c5cb DT |
459 | static const struct of_rename_gpio { |
460 | const char *con_id; | |
461 | const char *legacy_id; /* NULL - same as con_id */ | |
462 | /* | |
463 | * Compatible string can be set to NULL in case where | |
464 | * matching to a particular compatible is not practical, | |
465 | * but it should only be done for gpio names that have | |
466 | * vendor prefix to reduce risk of false positives. | |
467 | * Addition of such entries is strongly discouraged. | |
468 | */ | |
469 | const char *compatible; | |
470 | } gpios[] = { | |
fbbbcd17 DT |
471 | #if !IS_ENABLED(CONFIG_LCD_HX8357) |
472 | /* Himax LCD controllers used "gpios-reset" */ | |
473 | { "reset", "gpios-reset", "himax,hx8357" }, | |
474 | { "reset", "gpios-reset", "himax,hx8369" }, | |
475 | #endif | |
b311c5cb DT |
476 | #if IS_ENABLED(CONFIG_MFD_ARIZONA) |
477 | { "wlf,reset", NULL, NULL }, | |
478 | #endif | |
eaf1a296 DT |
479 | #if IS_ENABLED(CONFIG_RTC_DRV_MOXART) |
480 | { "rtc-data", "gpio-rtc-data", "moxa,moxart-rtc" }, | |
481 | { "rtc-sclk", "gpio-rtc-sclk", "moxa,moxart-rtc" }, | |
482 | { "rtc-reset", "gpio-rtc-reset", "moxa,moxart-rtc" }, | |
483 | #endif | |
9c2cc717 DT |
484 | #if IS_ENABLED(CONFIG_NFC_MRVL_I2C) |
485 | { "reset", "reset-n-io", "marvell,nfc-i2c" }, | |
486 | #endif | |
487 | #if IS_ENABLED(CONFIG_NFC_MRVL_SPI) | |
488 | { "reset", "reset-n-io", "marvell,nfc-spi" }, | |
489 | #endif | |
490 | #if IS_ENABLED(CONFIG_NFC_MRVL_UART) | |
491 | { "reset", "reset-n-io", "marvell,nfc-uart" }, | |
492 | { "reset", "reset-n-io", "mrvl,nfc-uart" }, | |
493 | #endif | |
fbbbcd17 DT |
494 | #if !IS_ENABLED(CONFIG_PCI_LANTIQ) |
495 | /* MIPS Lantiq PCI */ | |
496 | { "reset", "gpios-reset", "lantiq,pci-xway" }, | |
497 | #endif | |
307c593b | 498 | |
b311c5cb DT |
499 | /* |
500 | * Some regulator bindings happened before we managed to | |
501 | * establish that GPIO properties should be named | |
502 | * "foo-gpios" so we have this special kludge for them. | |
503 | */ | |
307c593b | 504 | #if IS_ENABLED(CONFIG_REGULATOR_ARIZONA_LDO1) |
b311c5cb | 505 | { "wlf,ldoena", NULL, NULL }, /* Arizona */ |
307c593b DT |
506 | #endif |
507 | #if IS_ENABLED(CONFIG_REGULATOR_WM8994) | |
b311c5cb DT |
508 | { "wlf,ldo1ena", NULL, NULL }, /* WM8994 */ |
509 | { "wlf,ldo2ena", NULL, NULL }, /* WM8994 */ | |
510 | #endif | |
e3023bf8 | 511 | |
944004eb DT |
512 | #if IS_ENABLED(CONFIG_SND_SOC_CS42L56) |
513 | { "reset", "cirrus,gpio-nreset", "cirrus,cs42l56" }, | |
514 | #endif | |
fbbbcd17 DT |
515 | #if IS_ENABLED(CONFIG_SND_SOC_TLV320AIC3X) |
516 | { "reset", "gpio-reset", "ti,tlv320aic3x" }, | |
517 | { "reset", "gpio-reset", "ti,tlv320aic33" }, | |
518 | { "reset", "gpio-reset", "ti,tlv320aic3007" }, | |
519 | { "reset", "gpio-reset", "ti,tlv320aic3104" }, | |
520 | { "reset", "gpio-reset", "ti,tlv320aic3106" }, | |
521 | #endif | |
307c593b | 522 | #if IS_ENABLED(CONFIG_SPI_GPIO) |
b311c5cb DT |
523 | /* |
524 | * The SPI GPIO bindings happened before we managed to | |
525 | * establish that GPIO properties should be named | |
526 | * "foo-gpios" so we have this special kludge for them. | |
527 | */ | |
528 | { "miso", "gpio-miso", "spi-gpio" }, | |
529 | { "mosi", "gpio-mosi", "spi-gpio" }, | |
530 | { "sck", "gpio-sck", "spi-gpio" }, | |
307c593b | 531 | #endif |
e3023bf8 | 532 | |
b311c5cb DT |
533 | /* |
534 | * The old Freescale bindings use simply "gpios" as name | |
535 | * for the chip select lines rather than "cs-gpios" like | |
536 | * all other SPI hardware. Allow this specifically for | |
537 | * Freescale and PPC devices. | |
538 | */ | |
307c593b | 539 | #if IS_ENABLED(CONFIG_SPI_FSL_SPI) |
b311c5cb DT |
540 | { "cs", "gpios", "fsl,spi" }, |
541 | { "cs", "gpios", "aeroflexgaisler,spictrl" }, | |
307c593b DT |
542 | #endif |
543 | #if IS_ENABLED(CONFIG_SPI_PPC4xx) | |
b311c5cb DT |
544 | { "cs", "gpios", "ibm,ppc4xx-spi" }, |
545 | #endif | |
307c593b | 546 | |
b311c5cb DT |
547 | #if IS_ENABLED(CONFIG_TYPEC_FUSB302) |
548 | /* | |
549 | * Fairchild FUSB302 host is using undocumented "fcs,int_n" | |
550 | * property without the compulsory "-gpios" suffix. | |
551 | */ | |
552 | { "fcs,int_n", NULL, "fcs,fusb302" }, | |
553 | #endif | |
6a537d48 | 554 | }; |
b311c5cb DT |
555 | struct gpio_desc *desc; |
556 | const char *legacy_id; | |
557 | unsigned int i; | |
6a537d48 LW |
558 | |
559 | if (!con_id) | |
560 | return ERR_PTR(-ENOENT); | |
561 | ||
b311c5cb DT |
562 | for (i = 0; i < ARRAY_SIZE(gpios); i++) { |
563 | if (strcmp(con_id, gpios[i].con_id)) | |
564 | continue; | |
6a537d48 | 565 | |
b311c5cb DT |
566 | if (gpios[i].compatible && |
567 | !of_device_is_compatible(np, gpios[i].compatible)) | |
568 | continue; | |
11c43bb0 | 569 | |
b311c5cb DT |
570 | legacy_id = gpios[i].legacy_id ?: gpios[i].con_id; |
571 | desc = of_get_named_gpiod_flags(np, legacy_id, idx, of_flags); | |
572 | if (!gpiod_not_found(desc)) { | |
573 | pr_info("%s uses legacy gpio name '%s' instead of '%s-gpios'\n", | |
574 | of_node_full_name(np), legacy_id, con_id); | |
575 | return desc; | |
576 | } | |
577 | } | |
6e24826d | 578 | |
b311c5cb | 579 | return ERR_PTR(-ENOENT); |
6e24826d LW |
580 | } |
581 | ||
326c3753 DT |
582 | static struct gpio_desc *of_find_mt2701_gpio(struct device_node *np, |
583 | const char *con_id, | |
584 | unsigned int idx, | |
585 | enum of_gpio_flags *of_flags) | |
586 | { | |
587 | struct gpio_desc *desc; | |
588 | const char *legacy_id; | |
589 | ||
590 | if (!IS_ENABLED(CONFIG_SND_SOC_MT2701_CS42448)) | |
591 | return ERR_PTR(-ENOENT); | |
592 | ||
593 | if (!of_device_is_compatible(np, "mediatek,mt2701-cs42448-machine")) | |
594 | return ERR_PTR(-ENOENT); | |
595 | ||
596 | if (!con_id || strcmp(con_id, "i2s1-in-sel")) | |
597 | return ERR_PTR(-ENOENT); | |
598 | ||
599 | if (idx == 0) | |
600 | legacy_id = "i2s1-in-sel-gpio1"; | |
601 | else if (idx == 1) | |
602 | legacy_id = "i2s1-in-sel-gpio2"; | |
603 | else | |
604 | return ERR_PTR(-ENOENT); | |
605 | ||
606 | desc = of_get_named_gpiod_flags(np, legacy_id, 0, of_flags); | |
607 | if (!gpiod_not_found(desc)) | |
608 | pr_info("%s is using legacy gpio name '%s' instead of '%s-gpios'\n", | |
609 | of_node_full_name(np), legacy_id, con_id); | |
610 | ||
611 | return desc; | |
612 | } | |
613 | ||
a2b5e207 DT |
614 | typedef struct gpio_desc *(*of_find_gpio_quirk)(struct device_node *np, |
615 | const char *con_id, | |
616 | unsigned int idx, | |
617 | enum of_gpio_flags *of_flags); | |
618 | static const of_find_gpio_quirk of_find_gpio_quirks[] = { | |
b311c5cb | 619 | of_find_gpio_rename, |
326c3753 | 620 | of_find_mt2701_gpio, |
8b10ca2f | 621 | NULL |
a2b5e207 DT |
622 | }; |
623 | ||
07445ae1 | 624 | struct gpio_desc *of_find_gpio(struct device_node *np, const char *con_id, |
fed7026a | 625 | unsigned int idx, unsigned long *flags) |
ea713bc4 LW |
626 | { |
627 | char prop_name[32]; /* 32 is max size of property name */ | |
628 | enum of_gpio_flags of_flags; | |
a2b5e207 | 629 | const of_find_gpio_quirk *q; |
ea713bc4 LW |
630 | struct gpio_desc *desc; |
631 | unsigned int i; | |
632 | ||
c8582339 | 633 | /* Try GPIO property "foo-gpios" and "foo-gpio" */ |
ea713bc4 LW |
634 | for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) { |
635 | if (con_id) | |
636 | snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id, | |
637 | gpio_suffixes[i]); | |
638 | else | |
639 | snprintf(prop_name, sizeof(prop_name), "%s", | |
640 | gpio_suffixes[i]); | |
641 | ||
07445ae1 | 642 | desc = of_get_named_gpiod_flags(np, prop_name, idx, &of_flags); |
6662ae6a | 643 | |
7b58696d | 644 | if (!gpiod_not_found(desc)) |
ea713bc4 LW |
645 | break; |
646 | } | |
647 | ||
a2b5e207 DT |
648 | /* Properly named GPIO was not found, try workarounds */ |
649 | for (q = of_find_gpio_quirks; gpiod_not_found(desc) && *q; q++) | |
07445ae1 | 650 | desc = (*q)(np, con_id, idx, &of_flags); |
6e24826d | 651 | |
ea713bc4 LW |
652 | if (IS_ERR(desc)) |
653 | return desc; | |
654 | ||
d9e7f0e3 | 655 | *flags = of_convert_gpio_flags(of_flags); |
d449991c | 656 | |
ea713bc4 LW |
657 | return desc; |
658 | } | |
659 | ||
f625d460 | 660 | /** |
fd7337fd | 661 | * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API |
f625d460 | 662 | * @np: device node to get GPIO from |
be715343 | 663 | * @chip: GPIO chip whose hog is parsed |
a79fead5 | 664 | * @idx: Index of the GPIO to parse |
f625d460 | 665 | * @name: GPIO line name |
fed7026a AS |
666 | * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from |
667 | * of_find_gpio() or of_parse_own_gpio() | |
f625d460 BP |
668 | * @dflags: gpiod_flags - optional GPIO initialization flags |
669 | * | |
670 | * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno | |
671 | * value on the error condition. | |
672 | */ | |
fd7337fd | 673 | static struct gpio_desc *of_parse_own_gpio(struct device_node *np, |
be715343 | 674 | struct gpio_chip *chip, |
a79fead5 | 675 | unsigned int idx, const char **name, |
fed7026a | 676 | unsigned long *lflags, |
fd7337fd | 677 | enum gpiod_flags *dflags) |
f625d460 BP |
678 | { |
679 | struct device_node *chip_np; | |
680 | enum of_gpio_flags xlate_flags; | |
be715343 MY |
681 | struct of_phandle_args gpiospec; |
682 | struct gpio_desc *desc; | |
a79fead5 | 683 | unsigned int i; |
f625d460 | 684 | u32 tmp; |
3f9547e1 | 685 | int ret; |
f625d460 | 686 | |
70d0fc42 | 687 | chip_np = dev_of_node(&chip->gpiodev->dev); |
f625d460 BP |
688 | if (!chip_np) |
689 | return ERR_PTR(-EINVAL); | |
690 | ||
691 | xlate_flags = 0; | |
2d6c06f5 | 692 | *lflags = GPIO_LOOKUP_FLAGS_DEFAULT; |
40941954 | 693 | *dflags = GPIOD_ASIS; |
f625d460 BP |
694 | |
695 | ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp); | |
696 | if (ret) | |
697 | return ERR_PTR(ret); | |
698 | ||
be715343 MY |
699 | gpiospec.np = chip_np; |
700 | gpiospec.args_count = tmp; | |
f625d460 | 701 | |
a79fead5 GU |
702 | for (i = 0; i < tmp; i++) { |
703 | ret = of_property_read_u32_index(np, "gpios", idx * tmp + i, | |
704 | &gpiospec.args[i]); | |
705 | if (ret) | |
706 | return ERR_PTR(ret); | |
707 | } | |
f625d460 | 708 | |
99468c1a | 709 | desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags); |
be715343 MY |
710 | if (IS_ERR(desc)) |
711 | return desc; | |
f625d460 | 712 | |
d9e7f0e3 | 713 | *lflags = of_convert_gpio_flags(xlate_flags); |
f625d460 BP |
714 | |
715 | if (of_property_read_bool(np, "input")) | |
716 | *dflags |= GPIOD_IN; | |
717 | else if (of_property_read_bool(np, "output-low")) | |
718 | *dflags |= GPIOD_OUT_LOW; | |
719 | else if (of_property_read_bool(np, "output-high")) | |
720 | *dflags |= GPIOD_OUT_HIGH; | |
721 | else { | |
62cdcb6c RH |
722 | pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n", |
723 | desc_to_gpio(desc), np); | |
f625d460 BP |
724 | return ERR_PTR(-EINVAL); |
725 | } | |
726 | ||
727 | if (name && of_property_read_string(np, "line-name", name)) | |
728 | *name = np->name; | |
729 | ||
be715343 | 730 | return desc; |
f625d460 BP |
731 | } |
732 | ||
bc21077e GU |
733 | /** |
734 | * of_gpiochip_add_hog - Add all hogs in a hog device node | |
735 | * @chip: gpio chip to act on | |
736 | * @hog: device node describing the hogs | |
737 | * | |
738 | * Returns error if it fails otherwise 0 on success. | |
739 | */ | |
740 | static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog) | |
741 | { | |
742 | enum gpiod_flags dflags; | |
743 | struct gpio_desc *desc; | |
744 | unsigned long lflags; | |
745 | const char *name; | |
746 | unsigned int i; | |
747 | int ret; | |
748 | ||
749 | for (i = 0;; i++) { | |
750 | desc = of_parse_own_gpio(hog, chip, i, &name, &lflags, &dflags); | |
751 | if (IS_ERR(desc)) | |
752 | break; | |
753 | ||
754 | ret = gpiod_hog(desc, name, lflags, dflags); | |
755 | if (ret < 0) | |
756 | return ret; | |
63636d95 GU |
757 | |
758 | #ifdef CONFIG_OF_DYNAMIC | |
759 | desc->hog = hog; | |
760 | #endif | |
bc21077e GU |
761 | } |
762 | ||
763 | return 0; | |
764 | } | |
765 | ||
f625d460 | 766 | /** |
fd7337fd | 767 | * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions |
f625d460 BP |
768 | * @chip: gpio chip to act on |
769 | * | |
770 | * This is only used by of_gpiochip_add to request/set GPIO initial | |
771 | * configuration. | |
ead066e6 | 772 | * It returns error if it fails otherwise 0 on success. |
f625d460 | 773 | */ |
dfbd379b | 774 | static int of_gpiochip_scan_gpios(struct gpio_chip *chip) |
f625d460 | 775 | { |
f625d460 | 776 | struct device_node *np; |
dfbd379b | 777 | int ret; |
f625d460 | 778 | |
70d0fc42 | 779 | for_each_available_child_of_node(dev_of_node(&chip->gpiodev->dev), np) { |
f625d460 BP |
780 | if (!of_property_read_bool(np, "gpio-hog")) |
781 | continue; | |
782 | ||
bc21077e GU |
783 | ret = of_gpiochip_add_hog(chip, np); |
784 | if (ret < 0) { | |
785 | of_node_put(np); | |
786 | return ret; | |
09e258af | 787 | } |
63636d95 GU |
788 | |
789 | of_node_set_flag(np, OF_POPULATED); | |
f625d460 | 790 | } |
dfbd379b LD |
791 | |
792 | return 0; | |
f625d460 BP |
793 | } |
794 | ||
63636d95 GU |
795 | #ifdef CONFIG_OF_DYNAMIC |
796 | /** | |
797 | * of_gpiochip_remove_hog - Remove all hogs in a hog device node | |
798 | * @chip: gpio chip to act on | |
799 | * @hog: device node describing the hogs | |
800 | */ | |
801 | static void of_gpiochip_remove_hog(struct gpio_chip *chip, | |
802 | struct device_node *hog) | |
803 | { | |
80c78fbe | 804 | struct gpio_desc *desc; |
63636d95 | 805 | |
57017edd | 806 | for_each_gpio_desc_with_flag(chip, desc, FLAG_IS_HOGGED) |
80c78fbe AS |
807 | if (desc->hog == hog) |
808 | gpiochip_free_own_desc(desc); | |
63636d95 GU |
809 | } |
810 | ||
811 | static int of_gpiochip_match_node(struct gpio_chip *chip, void *data) | |
812 | { | |
597a8a88 | 813 | return device_match_of_node(&chip->gpiodev->dev, data); |
63636d95 GU |
814 | } |
815 | ||
816 | static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np) | |
817 | { | |
818 | return gpiochip_find(np, of_gpiochip_match_node); | |
819 | } | |
820 | ||
821 | static int of_gpio_notify(struct notifier_block *nb, unsigned long action, | |
822 | void *arg) | |
823 | { | |
824 | struct of_reconfig_data *rd = arg; | |
825 | struct gpio_chip *chip; | |
826 | int ret; | |
827 | ||
828 | /* | |
829 | * This only supports adding and removing complete gpio-hog nodes. | |
830 | * Modifying an existing gpio-hog node is not supported (except for | |
831 | * changing its "status" property, which is treated the same as | |
832 | * addition/removal). | |
833 | */ | |
834 | switch (of_reconfig_get_state_change(action, arg)) { | |
835 | case OF_RECONFIG_CHANGE_ADD: | |
836 | if (!of_property_read_bool(rd->dn, "gpio-hog")) | |
837 | return NOTIFY_OK; /* not for us */ | |
838 | ||
839 | if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) | |
840 | return NOTIFY_OK; | |
841 | ||
842 | chip = of_find_gpiochip_by_node(rd->dn->parent); | |
843 | if (chip == NULL) | |
844 | return NOTIFY_OK; /* not for us */ | |
845 | ||
846 | ret = of_gpiochip_add_hog(chip, rd->dn); | |
847 | if (ret < 0) { | |
848 | pr_err("%s: failed to add hogs for %pOF\n", __func__, | |
849 | rd->dn); | |
850 | of_node_clear_flag(rd->dn, OF_POPULATED); | |
851 | return notifier_from_errno(ret); | |
852 | } | |
853 | break; | |
854 | ||
855 | case OF_RECONFIG_CHANGE_REMOVE: | |
856 | if (!of_node_check_flag(rd->dn, OF_POPULATED)) | |
857 | return NOTIFY_OK; /* already depopulated */ | |
858 | ||
859 | chip = of_find_gpiochip_by_node(rd->dn->parent); | |
860 | if (chip == NULL) | |
861 | return NOTIFY_OK; /* not for us */ | |
862 | ||
863 | of_gpiochip_remove_hog(chip, rd->dn); | |
864 | of_node_clear_flag(rd->dn, OF_POPULATED); | |
865 | break; | |
866 | } | |
867 | ||
868 | return NOTIFY_OK; | |
869 | } | |
870 | ||
871 | struct notifier_block gpio_of_notifier = { | |
872 | .notifier_call = of_gpio_notify, | |
873 | }; | |
874 | #endif /* CONFIG_OF_DYNAMIC */ | |
875 | ||
863fbf49 | 876 | /** |
67049c50 | 877 | * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags |
a19e3da5 | 878 | * @gc: pointer to the gpio_chip structure |
67049c50 | 879 | * @gpiospec: GPIO specifier as found in the device tree |
b908b53d | 880 | * @flags: a flags pointer to fill in |
863fbf49 AV |
881 | * |
882 | * This is simple translation function, suitable for the most 1:1 mapped | |
67049c50 | 883 | * GPIO chips. This function performs only one sanity check: whether GPIO |
863fbf49 AV |
884 | * is less than ngpios (that is specified in the gpio_chip). |
885 | */ | |
b0c7e73b GU |
886 | static int of_gpio_simple_xlate(struct gpio_chip *gc, |
887 | const struct of_phandle_args *gpiospec, | |
888 | u32 *flags) | |
863fbf49 | 889 | { |
b908b53d AV |
890 | /* |
891 | * We're discouraging gpio_cells < 2, since that way you'll have to | |
20a8a968 | 892 | * write your own xlate function (that will have to retrieve the GPIO |
b908b53d AV |
893 | * number and the flags from a single gpio cell -- this is possible, |
894 | * but not recommended). | |
895 | */ | |
a19e3da5 | 896 | if (gc->of_gpio_n_cells < 2) { |
b908b53d AV |
897 | WARN_ON(1); |
898 | return -EINVAL; | |
899 | } | |
900 | ||
15c9a0ac GL |
901 | if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells)) |
902 | return -EINVAL; | |
903 | ||
6270d830 | 904 | if (gpiospec->args[0] >= gc->ngpio) |
863fbf49 AV |
905 | return -EINVAL; |
906 | ||
b908b53d | 907 | if (flags) |
15c9a0ac | 908 | *flags = gpiospec->args[1]; |
b908b53d | 909 | |
15c9a0ac | 910 | return gpiospec->args[0]; |
863fbf49 | 911 | } |
863fbf49 | 912 | |
a99cc668 AB |
913 | #if IS_ENABLED(CONFIG_OF_GPIO_MM_GPIOCHIP) |
914 | #include <linux/gpio/legacy-of-mm-gpiochip.h> | |
863fbf49 | 915 | /** |
3208b0f0 | 916 | * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank) |
863fbf49 AV |
917 | * @np: device node of the GPIO chip |
918 | * @mm_gc: pointer to the of_mm_gpio_chip allocated structure | |
3208b0f0 | 919 | * @data: driver data to store in the struct gpio_chip |
863fbf49 AV |
920 | * |
921 | * To use this function you should allocate and fill mm_gc with: | |
922 | * | |
923 | * 1) In the gpio_chip structure: | |
924 | * - all the callbacks | |
a19e3da5 AV |
925 | * - of_gpio_n_cells |
926 | * - of_xlate callback (optional) | |
863fbf49 AV |
927 | * |
928 | * 3) In the of_mm_gpio_chip structure: | |
929 | * - save_regs callback (optional) | |
930 | * | |
931 | * If succeeded, this function will map bank's memory and will | |
932 | * do all necessary work for you. Then you'll able to use .regs | |
933 | * to manage GPIOs from the callbacks. | |
934 | */ | |
3208b0f0 LW |
935 | int of_mm_gpiochip_add_data(struct device_node *np, |
936 | struct of_mm_gpio_chip *mm_gc, | |
937 | void *data) | |
863fbf49 AV |
938 | { |
939 | int ret = -ENOMEM; | |
a19e3da5 | 940 | struct gpio_chip *gc = &mm_gc->gc; |
863fbf49 | 941 | |
7eb6ce2f | 942 | gc->label = kasprintf(GFP_KERNEL, "%pOF", np); |
863fbf49 AV |
943 | if (!gc->label) |
944 | goto err0; | |
945 | ||
946 | mm_gc->regs = of_iomap(np, 0); | |
947 | if (!mm_gc->regs) | |
948 | goto err1; | |
949 | ||
21451155 | 950 | gc->base = -1; |
863fbf49 | 951 | |
863fbf49 AV |
952 | if (mm_gc->save_regs) |
953 | mm_gc->save_regs(mm_gc); | |
954 | ||
77289b2f AS |
955 | fwnode_handle_put(mm_gc->gc.fwnode); |
956 | mm_gc->gc.fwnode = fwnode_handle_get(of_fwnode_handle(np)); | |
863fbf49 | 957 | |
3208b0f0 | 958 | ret = gpiochip_add_data(gc, data); |
863fbf49 AV |
959 | if (ret) |
960 | goto err2; | |
961 | ||
863fbf49 AV |
962 | return 0; |
963 | err2: | |
5d07a692 | 964 | of_node_put(np); |
863fbf49 AV |
965 | iounmap(mm_gc->regs); |
966 | err1: | |
967 | kfree(gc->label); | |
968 | err0: | |
7eb6ce2f | 969 | pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret); |
863fbf49 AV |
970 | return ret; |
971 | } | |
6d662455 | 972 | EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data); |
594fa265 | 973 | |
d621e8ba RRD |
974 | /** |
975 | * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank) | |
976 | * @mm_gc: pointer to the of_mm_gpio_chip allocated structure | |
977 | */ | |
978 | void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc) | |
979 | { | |
980 | struct gpio_chip *gc = &mm_gc->gc; | |
981 | ||
d621e8ba RRD |
982 | gpiochip_remove(gc); |
983 | iounmap(mm_gc->regs); | |
984 | kfree(gc->label); | |
985 | } | |
6d662455 | 986 | EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove); |
a99cc668 | 987 | #endif |
d621e8ba | 988 | |
f23f1516 | 989 | #ifdef CONFIG_PINCTRL |
28355f81 | 990 | static int of_gpiochip_add_pin_range(struct gpio_chip *chip) |
f23f1516 | 991 | { |
f23f1516 | 992 | struct of_phandle_args pinspec; |
1e63d7b9 | 993 | struct pinctrl_dev *pctldev; |
70d0fc42 | 994 | struct device_node *np; |
f23f1516 | 995 | int index = 0, ret; |
586a87e6 CR |
996 | const char *name; |
997 | static const char group_names_propname[] = "gpio-ranges-group-names"; | |
998 | struct property *group_names; | |
f23f1516 | 999 | |
70d0fc42 | 1000 | np = dev_of_node(&chip->gpiodev->dev); |
f23f1516 | 1001 | if (!np) |
28355f81 | 1002 | return 0; |
f23f1516 | 1003 | |
586a87e6 CR |
1004 | group_names = of_find_property(np, group_names_propname, NULL); |
1005 | ||
ad4e1a7c | 1006 | for (;; index++) { |
d9fe0039 SW |
1007 | ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3, |
1008 | index, &pinspec); | |
f23f1516 SH |
1009 | if (ret) |
1010 | break; | |
1011 | ||
1e63d7b9 | 1012 | pctldev = of_pinctrl_get(pinspec.np); |
602cf638 | 1013 | of_node_put(pinspec.np); |
1e63d7b9 | 1014 | if (!pctldev) |
28355f81 | 1015 | return -EPROBE_DEFER; |
f23f1516 | 1016 | |
586a87e6 CR |
1017 | if (pinspec.args[2]) { |
1018 | if (group_names) { | |
72858602 | 1019 | of_property_read_string_index(np, |
586a87e6 CR |
1020 | group_names_propname, |
1021 | index, &name); | |
1022 | if (strlen(name)) { | |
7eb6ce2f RH |
1023 | pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n", |
1024 | np); | |
586a87e6 CR |
1025 | break; |
1026 | } | |
1027 | } | |
1028 | /* npins != 0: linear range */ | |
1029 | ret = gpiochip_add_pin_range(chip, | |
1030 | pinctrl_dev_get_devname(pctldev), | |
1031 | pinspec.args[0], | |
1032 | pinspec.args[1], | |
1033 | pinspec.args[2]); | |
1034 | if (ret) | |
28355f81 | 1035 | return ret; |
586a87e6 CR |
1036 | } else { |
1037 | /* npins == 0: special range */ | |
1038 | if (pinspec.args[1]) { | |
7eb6ce2f RH |
1039 | pr_err("%pOF: Illegal gpio-range format.\n", |
1040 | np); | |
586a87e6 CR |
1041 | break; |
1042 | } | |
1043 | ||
1044 | if (!group_names) { | |
7eb6ce2f RH |
1045 | pr_err("%pOF: GPIO group range requested but no %s property.\n", |
1046 | np, group_names_propname); | |
586a87e6 CR |
1047 | break; |
1048 | } | |
1049 | ||
1050 | ret = of_property_read_string_index(np, | |
1051 | group_names_propname, | |
1052 | index, &name); | |
1053 | if (ret) | |
1054 | break; | |
1055 | ||
1056 | if (!strlen(name)) { | |
7eb6ce2f RH |
1057 | pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n", |
1058 | np); | |
586a87e6 CR |
1059 | break; |
1060 | } | |
1061 | ||
1062 | ret = gpiochip_add_pingroup_range(chip, pctldev, | |
1063 | pinspec.args[0], name); | |
1064 | if (ret) | |
28355f81 | 1065 | return ret; |
586a87e6 | 1066 | } |
ad4e1a7c | 1067 | } |
28355f81 TV |
1068 | |
1069 | return 0; | |
f23f1516 SH |
1070 | } |
1071 | ||
f23f1516 | 1072 | #else |
28355f81 | 1073 | static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; } |
f23f1516 SH |
1074 | #endif |
1075 | ||
28355f81 | 1076 | int of_gpiochip_add(struct gpio_chip *chip) |
391c970c | 1077 | { |
8afe8255 | 1078 | struct device_node *np; |
f0d1ab05 | 1079 | int ret; |
28355f81 | 1080 | |
70d0fc42 | 1081 | np = dev_of_node(&chip->gpiodev->dev); |
8afe8255 | 1082 | if (!np) |
28355f81 | 1083 | return 0; |
391c970c AV |
1084 | |
1085 | if (!chip->of_xlate) { | |
1086 | chip->of_gpio_n_cells = 2; | |
1087 | chip->of_xlate = of_gpio_simple_xlate; | |
1088 | } | |
1089 | ||
1020dfd1 MY |
1090 | if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS) |
1091 | return -EINVAL; | |
1092 | ||
f0d1ab05 LW |
1093 | ret = of_gpiochip_add_pin_range(chip); |
1094 | if (ret) | |
1095 | return ret; | |
28355f81 | 1096 | |
59a4a351 | 1097 | of_node_get(np); |
f625d460 | 1098 | |
f0d1ab05 | 1099 | ret = of_gpiochip_scan_gpios(chip); |
2f4133bb | 1100 | if (ret) |
59a4a351 | 1101 | of_node_put(np); |
f7299d44 | 1102 | |
f0d1ab05 | 1103 | return ret; |
391c970c AV |
1104 | } |
1105 | ||
1106 | void of_gpiochip_remove(struct gpio_chip *chip) | |
1107 | { | |
59a4a351 | 1108 | of_node_put(dev_of_node(&chip->gpiodev->dev)); |
391c970c | 1109 | } |