1 // SPDX-License-Identifier: GPL-2.0+
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>
8 */
9
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/errno.h>
13 #include <linux/module.h>
14 #include <linux/io.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/of.h>
17 #include <linux/of_address.h>
18 #include <linux/of_gpio.h>
19 #include <linux/pinctrl/pinctrl.h>
20 #include <linux/slab.h>
21 #include <linux/gpio/machine.h>
22
23 #include "gpiolib.h"
24 #include "gpiolib-of.h"
25
26 /**
27 * of_gpio_spi_cs_get_count() - special GPIO counting for SPI
28 * @dev: Consuming device
29 * @con_id: Function within the GPIO consumer
30 *
31 * Some elder GPIO controllers need special quirks. Currently we handle
32 * the Freescale and PPC GPIO controller with bindings that doesn't use the
33 * established "cs-gpios" for chip selects but instead rely on
34 * "gpios" for the chip select lines. If we detect this, we redirect
35 * the counting of "cs-gpios" to count "gpios" transparent to the
36 * driver.
37 */
of_gpio_spi_cs_get_count(struct device * dev,const char * con_id)38 static int of_gpio_spi_cs_get_count(struct device *dev, const char *con_id)
39 {
40 struct device_node *np = dev->of_node;
41
42 if (!IS_ENABLED(CONFIG_SPI_MASTER)) {
43 return 0;
44 }
45 if (!con_id || strcmp(con_id, "cs")) {
46 return 0;
47 }
48 if (!of_device_is_compatible(np, "fsl,spi") && !of_device_is_compatible(np, "aeroflexgaisler,spictrl") &&
49 !of_device_is_compatible(np, "ibm,ppc4xx-spi")) {
50 return 0;
51 }
52 return of_gpio_named_count(np, "gpios");
53 }
54
55 /*
56 * This is used by external users of of_gpio_count() from <linux/of_gpio.h>
57 *
58 * get rid of those external users by converting them to GPIO
59 * descriptors and let them all use gpiod_count()
60 */
of_gpio_get_count(struct device * dev,const char * con_id)61 int of_gpio_get_count(struct device *dev, const char *con_id)
62 {
63 int ret;
64 char propname[32];
65 unsigned int i;
66
67 ret = of_gpio_spi_cs_get_count(dev, con_id);
68 if (ret > 0) {
69 return ret;
70 }
71
72 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
73 if (con_id) {
74 snprintf(propname, sizeof(propname), "%s-%s", con_id, gpio_suffixes[i]);
75 } else {
76 snprintf(propname, sizeof(propname), "%s", gpio_suffixes[i]);
77 }
78
79 ret = of_gpio_named_count(dev->of_node, propname);
80 if (ret > 0) {
81 break;
82 }
83 }
84 return ret ? ret : -ENOENT;
85 }
86
of_gpiochip_match_node_and_xlate(struct gpio_chip * chip,void * data)87 static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
88 {
89 struct of_phandle_args *gpiospec = data;
90
91 return chip->gpiodev->dev.of_node == gpiospec->np && chip->of_xlate && chip->of_xlate(chip, gpiospec, NULL) >= 0;
92 }
93
of_find_gpiochip_by_xlate(struct of_phandle_args * gpiospec)94 static struct gpio_chip *of_find_gpiochip_by_xlate(struct of_phandle_args *gpiospec)
95 {
96 return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
97 }
98
of_xlate_and_get_gpiod_flags(struct gpio_chip * chip,struct of_phandle_args * gpiospec,enum of_gpio_flags * flags)99 static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip, struct of_phandle_args *gpiospec,
100 enum of_gpio_flags *flags)
101 {
102 int ret;
103
104 if (chip->of_gpio_n_cells != gpiospec->args_count) {
105 return ERR_PTR(-EINVAL);
106 }
107
108 ret = chip->of_xlate(chip, gpiospec, flags);
109 if (ret < 0) {
110 return ERR_PTR(ret);
111 }
112
113 return gpiochip_get_desc(chip, ret);
114 }
115
116 /**
117 * of_gpio_need_valid_mask() - figure out if the OF GPIO driver needs
118 * to set the .valid_mask
119 * @gc: the target gpio_chip
120 *
121 * Return: true if the valid mask needs to be set
122 */
of_gpio_need_valid_mask(const struct gpio_chip * gc)123 bool of_gpio_need_valid_mask(const struct gpio_chip *gc)
124 {
125 int size;
126 struct device_node *np = gc->of_node;
127
128 size = of_property_count_u32_elems(np, "gpio-reserved-ranges");
129 if (size > 0 && size % 2 == 0) {
130 return true;
131 }
132 return false;
133 }
134
of_gpio_flags_quirks(struct device_node * np,const char * propname,enum of_gpio_flags * flags,int index)135 static void of_gpio_flags_quirks(struct device_node *np, const char *propname, enum of_gpio_flags *flags, int index)
136 {
137 /*
138 * Some GPIO fixed regulator quirks.
139 * Note that active low is the default.
140 */
141 if (IS_ENABLED(CONFIG_REGULATOR) &&
142 (of_device_is_compatible(np, "regulator-fixed") || of_device_is_compatible(np, "reg-fixed-voltage") ||
143 (!(strcmp(propname, "enable-gpio") && strcmp(propname, "enable-gpios")) &&
144 of_device_is_compatible(np, "regulator-gpio")))) {
145 bool active_low = !of_property_read_bool(np, "enable-active-high");
146 /*
147 * The regulator GPIO handles are specified such that the
148 * presence or absence of "enable-active-high" solely controls
149 * the polarity of the GPIO line. Any phandle flags must
150 * be actively ignored.
151 */
152 if ((*flags & OF_GPIO_ACTIVE_LOW) && !active_low) {
153 pr_warn("%s GPIO handle specifies active low - ignored\n", of_node_full_name(np));
154 *flags &= ~OF_GPIO_ACTIVE_LOW;
155 }
156 if (active_low) {
157 *flags |= OF_GPIO_ACTIVE_LOW;
158 }
159 }
160 /*
161 * Legacy open drain handling for fixed voltage regulators.
162 */
163 if (IS_ENABLED(CONFIG_REGULATOR) && of_device_is_compatible(np, "reg-fixed-voltage") &&
164 of_property_read_bool(np, "gpio-open-drain")) {
165 *flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
166 pr_info("%s uses legacy open drain flag - update the DTS if you can\n", of_node_full_name(np));
167 }
168
169 /*
170 * Legacy handling of SPI active high chip select. If we have a
171 * property named "cs-gpios" we need to inspect the child node
172 * to determine if the flags should have inverted semantics.
173 */
174 if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") && of_property_read_bool(np, "cs-gpios")) {
175 struct device_node *child;
176 u32 cs;
177 int ret;
178
179 for_each_child_of_node(np, child)
180 {
181 ret = of_property_read_u32(child, "reg", &cs);
182 if (ret) {
183 continue;
184 }
185 if (cs == index) {
186 /*
187 * SPI children have active low chip selects
188 * by default. This can be specified negatively
189 * by just omitting "spi-cs-high" in the
190 * device node, or actively by tagging on
191 * GPIO_ACTIVE_LOW as flag in the device
192 * tree. If the line is simultaneously
193 * tagged as active low in the device tree
194 * and has the "spi-cs-high" set, we get a
195 * conflict and the "spi-cs-high" flag will
196 * take precedence.
197 */
198 if (of_property_read_bool(child, "spi-cs-high")) {
199 if (*flags & OF_GPIO_ACTIVE_LOW) {
200 pr_warn("%s GPIO handle specifies active low - ignored\n", of_node_full_name(child));
201 *flags &= ~OF_GPIO_ACTIVE_LOW;
202 }
203 } else {
204 if (!(*flags & OF_GPIO_ACTIVE_LOW)) {
205 pr_info("%s enforce active low on chipselect handle\n", of_node_full_name(child));
206 }
207 *flags |= OF_GPIO_ACTIVE_LOW;
208 }
209 of_node_put(child);
210 break;
211 }
212 }
213 }
214
215 /* Legacy handling of stmmac's active-low PHY reset line */
216 if (IS_ENABLED(CONFIG_STMMAC_ETH) && !strcmp(propname, "snps,reset-gpio") &&
217 of_property_read_bool(np, "snps,reset-active-low")) {
218 *flags |= OF_GPIO_ACTIVE_LOW;
219 }
220 }
221
222 /**
223 * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
224 * @np: device node to get GPIO from
225 * @propname: property name containing gpio specifier(s)
226 * @index: index of the GPIO
227 * @flags: a flags pointer to fill in
228 *
229 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
230 * value on the error condition. If @flags is not NULL the function also fills
231 * in flags for the GPIO.
232 */
of_get_named_gpiod_flags(struct device_node * np,const char * propname,int index,enum of_gpio_flags * flags)233 static struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np, const char *propname, int index,
234 enum of_gpio_flags *flags)
235 {
236 struct of_phandle_args gpiospec;
237 struct gpio_chip *chip;
238 struct gpio_desc *desc;
239 int ret;
240
241 ret = of_parse_phandle_with_args_map(np, propname, "gpio", index, &gpiospec);
242 if (ret) {
243 pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n", __func__, propname, np, index);
244 return ERR_PTR(ret);
245 }
246
247 chip = of_find_gpiochip_by_xlate(&gpiospec);
248 if (!chip) {
249 desc = ERR_PTR(-EPROBE_DEFER);
250 goto out;
251 }
252
253 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
254 if (IS_ERR(desc)) {
255 goto out;
256 }
257
258 if (flags) {
259 of_gpio_flags_quirks(np, propname, flags, index);
260 }
261
262 pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n", __func__, propname, np, index,
263 PTR_ERR_OR_ZERO(desc));
264
265 out:
266 of_node_put(gpiospec.np);
267
268 return desc;
269 }
270
of_get_named_gpio_flags(struct device_node * np,const char * list_name,int index,enum of_gpio_flags * flags)271 int of_get_named_gpio_flags(struct device_node *np, const char *list_name, int index, enum of_gpio_flags *flags)
272 {
273 struct gpio_desc *desc;
274
275 desc = of_get_named_gpiod_flags(np, list_name, index, flags);
276 if (IS_ERR(desc)) {
277 return PTR_ERR(desc);
278 } else {
279 return desc_to_gpio(desc);
280 }
281 }
282 EXPORT_SYMBOL_GPL(of_get_named_gpio_flags);
283
284 /**
285 * gpiod_get_from_of_node() - obtain a GPIO from an OF node
286 * @node: handle of the OF node
287 * @propname: name of the DT property representing the GPIO
288 * @index: index of the GPIO to obtain for the consumer
289 * @dflags: GPIO initialization flags
290 * @label: label to attach to the requested GPIO
291 *
292 * Returns:
293 * On successful request the GPIO pin is configured in accordance with
294 * provided @dflags.
295 *
296 * In case of error an ERR_PTR() is returned.
297 */
gpiod_get_from_of_node(struct device_node * node,const char * propname,int index,enum gpiod_flags dflags,const char * label)298 struct gpio_desc *gpiod_get_from_of_node(struct device_node *node, const char *propname, int index,
299 enum gpiod_flags dflags, const char *label)
300 {
301 unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
302 struct gpio_desc *desc;
303 enum of_gpio_flags flags;
304 bool active_low = false;
305 bool single_ended = false;
306 bool open_drain = false;
307 bool transitory = false;
308 int ret;
309
310 desc = of_get_named_gpiod_flags(node, propname, index, &flags);
311 if (!desc || IS_ERR(desc)) {
312 return desc;
313 }
314
315 active_low = flags & OF_GPIO_ACTIVE_LOW;
316 single_ended = flags & OF_GPIO_SINGLE_ENDED;
317 open_drain = flags & OF_GPIO_OPEN_DRAIN;
318 transitory = flags & OF_GPIO_TRANSITORY;
319
320 ret = gpiod_request(desc, label);
321 if (ret == -EBUSY && (dflags & GPIOD_FLAGS_BIT_NONEXCLUSIVE)) {
322 return desc;
323 }
324 if (ret) {
325 return ERR_PTR(ret);
326 }
327
328 if (active_low) {
329 lflags |= GPIO_ACTIVE_LOW;
330 }
331
332 if (single_ended) {
333 if (open_drain) {
334 lflags |= GPIO_OPEN_DRAIN;
335 } else {
336 lflags |= GPIO_OPEN_SOURCE;
337 }
338 }
339
340 if (transitory) {
341 lflags |= GPIO_TRANSITORY;
342 }
343
344 if (flags & OF_GPIO_PULL_UP) {
345 lflags |= GPIO_PULL_UP;
346 }
347
348 if (flags & OF_GPIO_PULL_DOWN) {
349 lflags |= GPIO_PULL_DOWN;
350 }
351
352 ret = gpiod_configure_flags(desc, propname, lflags, dflags);
353 if (ret < 0) {
354 gpiod_put(desc);
355 return ERR_PTR(ret);
356 }
357
358 return desc;
359 }
360 EXPORT_SYMBOL_GPL(gpiod_get_from_of_node);
361
362 /*
363 * The SPI GPIO bindings happened before we managed to establish that GPIO
364 * properties should be named "foo-gpios" so we have this special kludge for
365 * them.
366 */
of_find_spi_gpio(struct device * dev,const char * con_id,enum of_gpio_flags * of_flags)367 static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id, enum of_gpio_flags *of_flags)
368 {
369 char prop_name[32]; /* 32 is max size of property name */
370 struct device_node *np = dev->of_node;
371 struct gpio_desc *desc;
372 int ret = 0;
373 /*
374 * Hopefully the compiler stubs the rest of the function if this
375 * is false.
376 */
377 if (!IS_ENABLED(CONFIG_SPI_MASTER)) {
378 return ERR_PTR(-ENOENT);
379 }
380
381 /* Allow this specifically for "spi-gpio" devices */
382 if (!of_device_is_compatible(np, "spi-gpio") || !con_id) {
383 return ERR_PTR(-ENOENT);
384 }
385
386 /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
387 ret = snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id);
388
389 desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags);
390 return desc;
391 }
392
393 /*
394 * The old Freescale bindings use simply "gpios" as name for the chip select
395 * lines rather than "cs-gpios" like all other SPI hardware. Account for this
396 * with a special quirk.
397 */
of_find_spi_cs_gpio(struct device * dev,const char * con_id,unsigned int idx,unsigned long * flags)398 static struct gpio_desc *of_find_spi_cs_gpio(struct device *dev, const char *con_id, unsigned int idx,
399 unsigned long *flags)
400 {
401 struct device_node *np = dev->of_node;
402
403 if (!IS_ENABLED(CONFIG_SPI_MASTER)) {
404 return ERR_PTR(-ENOENT);
405 }
406
407 /* Allow this specifically for Freescale and PPC devices */
408 if (!of_device_is_compatible(np, "fsl,spi") && !of_device_is_compatible(np, "aeroflexgaisler,spictrl") &&
409 !of_device_is_compatible(np, "ibm,ppc4xx-spi")) {
410 return ERR_PTR(-ENOENT);
411 }
412 /* Allow only if asking for "cs-gpios" */
413 if (!con_id || strcmp(con_id, "cs")) {
414 return ERR_PTR(-ENOENT);
415 }
416
417 /*
418 * While all other SPI controllers use "cs-gpios" the Freescale
419 * uses just "gpios" so translate to that when "cs-gpios" is
420 * requested.
421 */
422 return of_find_gpio(dev, NULL, idx, flags);
423 }
424
425 /*
426 * Some regulator bindings happened before we managed to establish that GPIO
427 * properties should be named "foo-gpios" so we have this special kludge for
428 * them.
429 */
of_find_regulator_gpio(struct device * dev,const char * con_id,enum of_gpio_flags * of_flags)430 static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id, enum of_gpio_flags *of_flags)
431 {
432 /* These are the connection IDs we accept as legacy GPIO phandles */
433 const char *whitelist[] = {
434 "wlf,ldoena", /* Arizona */
435 "wlf,ldo1ena", /* WM8994 */
436 "wlf,ldo2ena", /* WM8994 */
437 };
438 struct device_node *np = dev->of_node;
439 struct gpio_desc *desc;
440 int i;
441
442 if (!IS_ENABLED(CONFIG_REGULATOR)) {
443 return ERR_PTR(-ENOENT);
444 }
445
446 if (!con_id) {
447 return ERR_PTR(-ENOENT);
448 }
449
450 i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id);
451 if (i < 0) {
452 return ERR_PTR(-ENOENT);
453 }
454
455 desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags);
456 return desc;
457 }
458
of_find_arizona_gpio(struct device * dev,const char * con_id,enum of_gpio_flags * of_flags)459 static struct gpio_desc *of_find_arizona_gpio(struct device *dev, const char *con_id, enum of_gpio_flags *of_flags)
460 {
461 if (!IS_ENABLED(CONFIG_MFD_ARIZONA)) {
462 return ERR_PTR(-ENOENT);
463 }
464
465 if (!con_id || strcmp(con_id, "wlf,reset")) {
466 return ERR_PTR(-ENOENT);
467 }
468
469 return of_get_named_gpiod_flags(dev->of_node, con_id, 0, of_flags);
470 }
471
of_find_usb_gpio(struct device * dev,const char * con_id,enum of_gpio_flags * of_flags)472 static struct gpio_desc *of_find_usb_gpio(struct device *dev, const char *con_id, enum of_gpio_flags *of_flags)
473 {
474 /*
475 * Currently this USB quirk is only for the Fairchild FUSB302 host which is using
476 * an undocumented DT GPIO line named "fcs,int_n" without the compulsory "-gpios"
477 * suffix.
478 */
479 if (!IS_ENABLED(CONFIG_TYPEC_FUSB302)) {
480 return ERR_PTR(-ENOENT);
481 }
482
483 if (!con_id || strcmp(con_id, "fcs,int_n")) {
484 return ERR_PTR(-ENOENT);
485 }
486
487 return of_get_named_gpiod_flags(dev->of_node, con_id, 0, of_flags);
488 }
489
of_find_gpio(struct device * dev,const char * con_id,unsigned int idx,unsigned long * flags)490 struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id, unsigned int idx, unsigned long *flags)
491 {
492 char prop_name[32]; /* 32 is max size of property name */
493 enum of_gpio_flags of_flags;
494 struct gpio_desc *desc;
495 unsigned int i;
496
497 /* Try GPIO property "foo-gpios" and "foo-gpio" */
498 for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
499 if (con_id) {
500 snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id, gpio_suffixes[i]);
501 } else {
502 snprintf(prop_name, sizeof(prop_name), "%s", gpio_suffixes[i]);
503 }
504
505 desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx, &of_flags);
506 if (!IS_ERR(desc) || PTR_ERR(desc) != -ENOENT) {
507 break;
508 }
509 }
510
511 if (PTR_ERR(desc) == -ENOENT) {
512 /* Special handling for SPI GPIOs if used */
513 desc = of_find_spi_gpio(dev, con_id, &of_flags);
514 }
515
516 if (PTR_ERR(desc) == -ENOENT) {
517 /* This quirk looks up flags and all */
518 desc = of_find_spi_cs_gpio(dev, con_id, idx, flags);
519 if (!IS_ERR(desc)) {
520 return desc;
521 }
522 }
523
524 if (PTR_ERR(desc) == -ENOENT) {
525 /* Special handling for regulator GPIOs if used */
526 desc = of_find_regulator_gpio(dev, con_id, &of_flags);
527 }
528
529 if (PTR_ERR(desc) == -ENOENT) {
530 desc = of_find_arizona_gpio(dev, con_id, &of_flags);
531 }
532
533 if (PTR_ERR(desc) == -ENOENT) {
534 desc = of_find_usb_gpio(dev, con_id, &of_flags);
535 }
536
537 if (IS_ERR(desc)) {
538 return desc;
539 }
540
541 if (of_flags & OF_GPIO_ACTIVE_LOW) {
542 *flags |= GPIO_ACTIVE_LOW;
543 }
544
545 if (of_flags & OF_GPIO_SINGLE_ENDED) {
546 if (of_flags & OF_GPIO_OPEN_DRAIN) {
547 *flags |= GPIO_OPEN_DRAIN;
548 } else {
549 *flags |= GPIO_OPEN_SOURCE;
550 }
551 }
552
553 if (of_flags & OF_GPIO_TRANSITORY) {
554 *flags |= GPIO_TRANSITORY;
555 }
556
557 if (of_flags & OF_GPIO_PULL_UP) {
558 *flags |= GPIO_PULL_UP;
559 }
560 if (of_flags & OF_GPIO_PULL_DOWN) {
561 *flags |= GPIO_PULL_DOWN;
562 }
563
564 return desc;
565 }
566
567 /**
568 * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
569 * @np: device node to get GPIO from
570 * @chip: GPIO chip whose hog is parsed
571 * @idx: Index of the GPIO to parse
572 * @name: GPIO line name
573 * @lflags: bitmask of gpio_lookup_flags GPIO_* values - returned from
574 * of_find_gpio() or of_parse_own_gpio()
575 * @dflags: gpiod_flags - optional GPIO initialization flags
576 *
577 * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
578 * value on the error condition.
579 */
of_parse_own_gpio(struct device_node * np,struct gpio_chip * chip,unsigned int idx,const char ** name,unsigned long * lflags,enum gpiod_flags * dflags)580 static struct gpio_desc *of_parse_own_gpio(struct device_node *np, struct gpio_chip *chip, unsigned int idx,
581 const char **name, unsigned long *lflags, enum gpiod_flags *dflags)
582 {
583 struct device_node *chip_np;
584 enum of_gpio_flags xlate_flags;
585 struct of_phandle_args gpiospec;
586 struct gpio_desc *desc;
587 unsigned int i;
588 u32 tmp;
589 int ret;
590
591 chip_np = chip->of_node;
592 if (!chip_np) {
593 return ERR_PTR(-EINVAL);
594 }
595
596 xlate_flags = 0;
597 *lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
598 *dflags = 0;
599
600 ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
601 if (ret) {
602 return ERR_PTR(ret);
603 }
604
605 gpiospec.np = chip_np;
606 gpiospec.args_count = tmp;
607
608 for (i = 0; i < tmp; i++) {
609 ret = of_property_read_u32_index(np, "gpios", idx * tmp + i, &gpiospec.args[i]);
610 if (ret) {
611 return ERR_PTR(ret);
612 }
613 }
614
615 desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
616 if (IS_ERR(desc)) {
617 return desc;
618 }
619
620 if (xlate_flags & OF_GPIO_ACTIVE_LOW) {
621 *lflags |= GPIO_ACTIVE_LOW;
622 }
623 if (xlate_flags & OF_GPIO_TRANSITORY) {
624 *lflags |= GPIO_TRANSITORY;
625 }
626 if (xlate_flags & OF_GPIO_PULL_UP) {
627 *lflags |= GPIO_PULL_UP;
628 }
629 if (xlate_flags & OF_GPIO_PULL_DOWN) {
630 *lflags |= GPIO_PULL_DOWN;
631 }
632
633 if (of_property_read_bool(np, "input")) {
634 *dflags |= GPIOD_IN;
635 } else if (of_property_read_bool(np, "output-low")) {
636 *dflags |= GPIOD_OUT_LOW;
637 } else if (of_property_read_bool(np, "output-high")) {
638 *dflags |= GPIOD_OUT_HIGH;
639 } else {
640 pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n", desc_to_gpio(desc), np);
641 return ERR_PTR(-EINVAL);
642 }
643
644 if (name && of_property_read_string(np, "line-name", name)) {
645 *name = np->name;
646 }
647
648 return desc;
649 }
650
651 /**
652 * of_gpiochip_add_hog - Add all hogs in a hog device node
653 * @chip: gpio chip to act on
654 * @hog: device node describing the hogs
655 *
656 * Returns error if it fails otherwise 0 on success.
657 */
of_gpiochip_add_hog(struct gpio_chip * chip,struct device_node * hog)658 static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog)
659 {
660 enum gpiod_flags dflags;
661 struct gpio_desc *desc;
662 unsigned long lflags;
663 const char *name;
664 unsigned int i;
665 int ret;
666
667 for (i = 0;; i++) {
668 desc = of_parse_own_gpio(hog, chip, i, &name, &lflags, &dflags);
669 if (IS_ERR(desc)) {
670 break;
671 }
672
673 ret = gpiod_hog(desc, name, lflags, dflags);
674 if (ret < 0) {
675 return ret;
676 }
677
678 #ifdef CONFIG_OF_DYNAMIC
679 desc->hog = hog;
680 #endif
681 }
682
683 return 0;
684 }
685
686 /**
687 * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
688 * @chip: gpio chip to act on
689 *
690 * This is only used by of_gpiochip_add to request/set GPIO initial
691 * configuration.
692 * It returns error if it fails otherwise 0 on success.
693 */
of_gpiochip_scan_gpios(struct gpio_chip * chip)694 static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
695 {
696 struct device_node *np;
697 int ret;
698
699 for_each_available_child_of_node(chip->of_node, np)
700 {
701 if (!of_property_read_bool(np, "gpio-hog")) {
702 continue;
703 }
704
705 ret = of_gpiochip_add_hog(chip, np);
706 if (ret < 0) {
707 of_node_put(np);
708 return ret;
709 }
710
711 of_node_set_flag(np, OF_POPULATED);
712 }
713
714 return 0;
715 }
716
717 #ifdef CONFIG_OF_DYNAMIC
718 /**
719 * of_gpiochip_remove_hog - Remove all hogs in a hog device node
720 * @chip: gpio chip to act on
721 * @hog: device node describing the hogs
722 */
of_gpiochip_remove_hog(struct gpio_chip * chip,struct device_node * hog)723 static void of_gpiochip_remove_hog(struct gpio_chip *chip, struct device_node *hog)
724 {
725 struct gpio_desc *descs = chip->gpiodev->descs;
726 unsigned int i;
727
728 for (i = 0; i < chip->ngpio; i++) {
729 if (test_bit(FLAG_IS_HOGGED, &descs[i].flags) && descs[i].hog == hog) {
730 gpiochip_free_own_desc(&descs[i]);
731 }
732 }
733 }
734
of_gpiochip_match_node(struct gpio_chip * chip,void * data)735 static int of_gpiochip_match_node(struct gpio_chip *chip, void *data)
736 {
737 return chip->gpiodev->dev.of_node == data;
738 }
739
of_find_gpiochip_by_node(struct device_node * np)740 static struct gpio_chip *of_find_gpiochip_by_node(struct device_node *np)
741 {
742 return gpiochip_find(np, of_gpiochip_match_node);
743 }
744
of_gpio_notify(struct notifier_block * nb,unsigned long action,void * arg)745 static int of_gpio_notify(struct notifier_block *nb, unsigned long action, void *arg)
746 {
747 struct of_reconfig_data *rd = arg;
748 struct gpio_chip *chip;
749 int ret;
750
751 /*
752 * This only supports adding and removing complete gpio-hog nodes.
753 * Modifying an existing gpio-hog node is not supported (except for
754 * changing its "status" property, which is treated the same as
755 * addition/removal).
756 */
757 switch (of_reconfig_get_state_change(action, arg)) {
758 case OF_RECONFIG_CHANGE_ADD:
759 if (!of_property_read_bool(rd->dn, "gpio-hog")) {
760 return NOTIFY_OK; /* not for us */
761 }
762
763 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
764 return NOTIFY_OK;
765 }
766
767 chip = of_find_gpiochip_by_node(rd->dn->parent);
768 if (chip == NULL) {
769 return NOTIFY_OK; /* not for us */
770 }
771
772 ret = of_gpiochip_add_hog(chip, rd->dn);
773 if (ret < 0) {
774 pr_err("%s: failed to add hogs for %pOF\n", __func__, rd->dn);
775 of_node_clear_flag(rd->dn, OF_POPULATED);
776 return notifier_from_errno(ret);
777 }
778 break;
779
780 case OF_RECONFIG_CHANGE_REMOVE:
781 if (!of_node_check_flag(rd->dn, OF_POPULATED)) {
782 return NOTIFY_OK; /* already depopulated */
783 }
784
785 chip = of_find_gpiochip_by_node(rd->dn->parent);
786 if (chip == NULL) {
787 return NOTIFY_OK; /* not for us */
788 }
789
790 of_gpiochip_remove_hog(chip, rd->dn);
791 of_node_clear_flag(rd->dn, OF_POPULATED);
792 break;
793 }
794
795 return NOTIFY_OK;
796 }
797
798 struct notifier_block gpio_of_notifier = {
799 .notifier_call = of_gpio_notify,
800 };
801 #endif /* CONFIG_OF_DYNAMIC */
802
803 /**
804 * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
805 * @gc: pointer to the gpio_chip structure
806 * @gpiospec: GPIO specifier as found in the device tree
807 * @flags: a flags pointer to fill in
808 *
809 * This is simple translation function, suitable for the most 1:1 mapped
810 * GPIO chips. This function performs only one sanity check: whether GPIO
811 * is less than ngpios (that is specified in the gpio_chip).
812 */
of_gpio_simple_xlate(struct gpio_chip * gc,const struct of_phandle_args * gpiospec,u32 * flags)813 static int of_gpio_simple_xlate(struct gpio_chip *gc, const struct of_phandle_args *gpiospec, u32 *flags)
814 {
815 /*
816 * We're discouraging gpio_cells < 2, since that way you'll have to
817 * write your own xlate function (that will have to retrieve the GPIO
818 * number and the flags from a single gpio cell -- this is possible,
819 * but not recommended).
820 */
821 if (gc->of_gpio_n_cells < 2) {
822 WARN_ON(1);
823 return -EINVAL;
824 }
825
826 if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells)) {
827 return -EINVAL;
828 }
829
830 if (gpiospec->args[0] >= gc->ngpio) {
831 return -EINVAL;
832 }
833
834 if (flags) {
835 *flags = gpiospec->args[1];
836 }
837
838 return gpiospec->args[0];
839 }
840
841 /**
842 * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
843 * @np: device node of the GPIO chip
844 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
845 * @data: driver data to store in the struct gpio_chip
846 *
847 * To use this function you should allocate and fill mm_gc with:
848 *
849 * 1) In the gpio_chip structure:
850 * - all the callbacks
851 * - of_gpio_n_cells
852 * - of_xlate callback (optional)
853 *
854 * 3) In the of_mm_gpio_chip structure:
855 * - save_regs callback (optional)
856 *
857 * If succeeded, this function will map bank's memory and will
858 * do all necessary work for you. Then you'll able to use .regs
859 * to manage GPIOs from the callbacks.
860 */
of_mm_gpiochip_add_data(struct device_node * np,struct of_mm_gpio_chip * mm_gc,void * data)861 int of_mm_gpiochip_add_data(struct device_node *np, struct of_mm_gpio_chip *mm_gc, void *data)
862 {
863 int ret = -ENOMEM;
864 struct gpio_chip *gc = &mm_gc->gc;
865 gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
866 if (!gc->label) {
867 goto err0;
868 }
869 mm_gc->regs = of_iomap(np, 0);
870 if (!mm_gc->regs) {
871 goto err1;
872 }
873 gc->base = -1;
874 if (mm_gc->save_regs) {
875 mm_gc->save_regs(mm_gc);
876 }
877 of_node_put(mm_gc->gc.of_node);
878 mm_gc->gc.of_node = of_node_get(np);
879 ret = gpiochip_add_data(gc, data);
880 if (ret) {
881 goto err2;
882 }
883
884 return 0;
885 err2:
886 of_node_put(np);
887 iounmap(mm_gc->regs);
888 err1:
889 kfree(gc->label);
890 err0:
891 pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
892 return ret;
893 }
894 EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data);
895
896 /**
897 * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
898 * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
899 */
of_mm_gpiochip_remove(struct of_mm_gpio_chip * mm_gc)900 void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
901 {
902 struct gpio_chip *gc = &mm_gc->gc;
903
904 if (!mm_gc) {
905 return;
906 }
907
908 gpiochip_remove(gc);
909 iounmap(mm_gc->regs);
910 kfree(gc->label);
911 }
912 EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove);
913
of_gpiochip_init_valid_mask(struct gpio_chip * chip)914 static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
915 {
916 int len, i;
917 u32 start, count;
918 struct device_node *np = chip->of_node;
919
920 len = of_property_count_u32_elems(np, "gpio-reserved-ranges");
921 if (len < 0 || len % 0x2 != 0) {
922 return;
923 }
924
925 for (i = 0; i < len; i += 0x2) {
926 of_property_read_u32_index(np, "gpio-reserved-ranges", i, &start);
927 of_property_read_u32_index(np, "gpio-reserved-ranges", i + 1, &count);
928 if (start >= chip->ngpio || start + count > chip->ngpio) {
929 continue;
930 }
931
932 bitmap_clear(chip->valid_mask, start, count);
933 }
934 };
935
936 #ifdef CONFIG_PINCTRL
of_gpiochip_add_pin_range(struct gpio_chip * chip)937 static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
938 {
939 struct of_phandle_args pinspec;
940 struct pinctrl_dev *pctldev;
941 struct device_node *np = chip->of_node;
942 int index = 0, ret;
943 struct property *group_names;
944 static const char group_names_propname[] = "gpio-ranges-group-names";
945 const char *name;
946
947 if (!np) {
948 return 0;
949 }
950
951 if (!of_property_read_bool(np, "gpio-ranges") &&
952 chip->of_gpio_ranges_fallback) {
953 return chip->of_gpio_ranges_fallback(chip, np);
954 }
955
956 group_names = of_find_property(np, group_names_propname, NULL);
957
958 for (;; index++) {
959 ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 0x3, index, &pinspec);
960 if (ret) {
961 break;
962 }
963
964 pctldev = of_pinctrl_get(pinspec.np);
965 of_node_put(pinspec.np);
966 if (!pctldev) {
967 return -EPROBE_DEFER;
968 }
969
970 if (pinspec.args[0x2]) {
971 if (group_names) {
972 of_property_read_string_index(np, group_names_propname, index, &name);
973 if (strlen(name)) {
974 pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n", np);
975 break;
976 }
977 }
978 /* npins != 0: linear range */
979 ret = gpiochip_add_pin_range(chip, pinctrl_dev_get_devname(pctldev), pinspec.args[0x0], pinspec.args[0x1],
980 pinspec.args[0x2]);
981 if (ret) {
982 return ret;
983 }
984 } else {
985 /* npins == 0: special range */
986 if (pinspec.args[1]) {
987 pr_err("%pOF: Illegal gpio-range format.\n", np);
988 break;
989 }
990
991 if (!group_names) {
992 pr_err("%pOF: GPIO group range requested but no %s property.\n", np, group_names_propname);
993 break;
994 }
995
996 ret = of_property_read_string_index(np, group_names_propname, index, &name);
997 if (ret) {
998 break;
999 }
1000
1001 if (!strlen(name)) {
1002 pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n", np);
1003 break;
1004 }
1005
1006 ret = gpiochip_add_pingroup_range(chip, pctldev, pinspec.args[0], name);
1007 if (ret) {
1008 return ret;
1009 }
1010 }
1011 }
1012
1013 return 0;
1014 }
1015
1016 #else
of_gpiochip_add_pin_range(struct gpio_chip * chip)1017 static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
1018 {
1019 return 0;
1020 }
1021 #endif
1022
of_gpiochip_add(struct gpio_chip * chip)1023 int of_gpiochip_add(struct gpio_chip *chip)
1024 {
1025 int ret;
1026
1027 if (!chip->of_node) {
1028 return 0;
1029 }
1030
1031 if (!chip->of_xlate) {
1032 chip->of_gpio_n_cells = 0x2;
1033 chip->of_xlate = of_gpio_simple_xlate;
1034 }
1035
1036 if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS) {
1037 return -EINVAL;
1038 }
1039
1040 of_gpiochip_init_valid_mask(chip);
1041
1042 ret = of_gpiochip_add_pin_range(chip);
1043 if (ret) {
1044 return ret;
1045 }
1046
1047 of_node_get(chip->of_node);
1048
1049 ret = of_gpiochip_scan_gpios(chip);
1050 if (ret) {
1051 of_node_put(chip->of_node);
1052 }
1053
1054 return ret;
1055 }
1056
of_gpiochip_remove(struct gpio_chip * chip)1057 void of_gpiochip_remove(struct gpio_chip *chip)
1058 {
1059 of_node_put(chip->of_node);
1060 }
1061
of_gpio_dev_init(struct gpio_chip * gc,struct gpio_device * gdev)1062 void of_gpio_dev_init(struct gpio_chip *gc, struct gpio_device *gdev)
1063 {
1064 /* If the gpiochip has an assigned OF node this takes precedence */
1065 if (gc->of_node) {
1066 gdev->dev.of_node = gc->of_node;
1067 } else {
1068 gc->of_node = gdev->dev.of_node;
1069 }
1070 if (gdev->dev.of_node) {
1071 gdev->dev.fwnode = of_fwnode_handle(gdev->dev.of_node);
1072 }
1073 }
1074