/src/barebox/drivers/of/of_gpio.c
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-2.0-only |
2 | | |
3 | | #include <common.h> |
4 | | #include <errno.h> |
5 | | #include <of.h> |
6 | | #include <of_gpio.h> |
7 | | #include <gpio.h> |
8 | | |
9 | | static void of_gpio_flags_quirks(struct device_node *np, |
10 | | const char *propname, |
11 | | enum of_gpio_flags *flags, |
12 | | int index) |
13 | 0 | { |
14 | | /* |
15 | | * Some GPIO fixed regulator quirks. |
16 | | * Note that active low is the default. |
17 | | */ |
18 | 0 | if (IS_ENABLED(CONFIG_REGULATOR) && |
19 | 0 | (of_device_is_compatible(np, "regulator-fixed") || |
20 | 0 | of_device_is_compatible(np, "reg-fixed-voltage") || |
21 | 0 | (!(strcmp(propname, "enable-gpio") && |
22 | 0 | strcmp(propname, "enable-gpios")) && |
23 | 0 | of_device_is_compatible(np, "regulator-gpio")))) { |
24 | 0 | bool active_low = !of_property_read_bool(np, |
25 | 0 | "enable-active-high"); |
26 | | /* |
27 | | * The regulator GPIO handles are specified such that the |
28 | | * presence or absence of "enable-active-high" solely controls |
29 | | * the polarity of the GPIO line. Any phandle flags must |
30 | | * be actively ignored. |
31 | | */ |
32 | 0 | if ((*flags & OF_GPIO_ACTIVE_LOW) && !active_low) { |
33 | 0 | pr_warn("%pOF GPIO handle specifies active low - ignored\n", |
34 | 0 | np); |
35 | 0 | *flags &= ~OF_GPIO_ACTIVE_LOW; |
36 | 0 | } |
37 | 0 | if (active_low) |
38 | 0 | *flags |= OF_GPIO_ACTIVE_LOW; |
39 | 0 | } |
40 | | |
41 | | /* Legacy handling of stmmac's active-low PHY reset line */ |
42 | 0 | if (IS_ENABLED(CONFIG_DRIVER_NET_DESIGNWARE_EQOS) && |
43 | 0 | !strcmp(propname, "snps,reset-gpio") && |
44 | 0 | of_property_read_bool(np, "snps,reset-active-low")) |
45 | 0 | *flags |= OF_GPIO_ACTIVE_LOW; |
46 | |
|
47 | 0 | } |
48 | | |
49 | | static struct gpio_chip *of_find_gpiochip_by_xlate( |
50 | | struct of_phandle_args *gpiospec) |
51 | 0 | { |
52 | 0 | struct gpio_chip *chip; |
53 | 0 | struct device *dev; |
54 | |
|
55 | 0 | dev = of_find_device_by_node(gpiospec->np); |
56 | 0 | if (!dev) { |
57 | 0 | pr_debug("%s: unable to find device of node %pOF\n", |
58 | 0 | __func__, gpiospec->np); |
59 | 0 | return NULL; |
60 | 0 | } |
61 | | |
62 | 0 | chip = gpio_get_chip_by_dev(dev); |
63 | 0 | if (!chip) { |
64 | 0 | pr_debug("%s: unable to find gpiochip\n", __func__); |
65 | 0 | return NULL; |
66 | 0 | } |
67 | | |
68 | 0 | if (!chip->ops->of_xlate || |
69 | 0 | chip->ops->of_xlate(chip, gpiospec, NULL) < 0) { |
70 | 0 | pr_err("%s: failed to execute of_xlate\n", __func__); |
71 | 0 | return NULL; |
72 | 0 | } |
73 | | |
74 | 0 | return chip; |
75 | 0 | } |
76 | | |
77 | | static int of_xlate_and_get_gpiod_flags(struct gpio_chip *chip, |
78 | | struct of_phandle_args *gpiospec, |
79 | | enum of_gpio_flags *flags) |
80 | 0 | { |
81 | 0 | if (chip->of_gpio_n_cells != gpiospec->args_count) |
82 | 0 | return -EINVAL; |
83 | | |
84 | 0 | return chip->ops->of_xlate(chip, gpiospec, flags); |
85 | 0 | } |
86 | | |
87 | | /** |
88 | | * of_get_named_gpio_flags() - Get a GPIO number and flags to use with GPIO API |
89 | | * @np: device node to get GPIO from |
90 | | * @propname: property name containing gpio specifier(s) |
91 | | * @index: index of the GPIO |
92 | | * @flags: a flags pointer to fill in |
93 | | * |
94 | | * Returns GPIO number to use with GPIO API, or one of the errno value on the |
95 | | * error condition. If @flags is not NULL the function also fills in flags for |
96 | | * the GPIO. |
97 | | */ |
98 | | int of_get_named_gpio_flags(struct device_node *np, const char *propname, |
99 | | int index, enum of_gpio_flags *flags) |
100 | 0 | { |
101 | 0 | struct of_phandle_args gpiospec; |
102 | 0 | struct gpio_chip *chip; |
103 | 0 | int ret; |
104 | |
|
105 | 0 | ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", |
106 | 0 | index, &gpiospec); |
107 | 0 | if (ret) { |
108 | 0 | pr_debug("%s: cannot parse %s property: %d\n", |
109 | 0 | __func__, propname, ret); |
110 | 0 | return ret; |
111 | 0 | } |
112 | | |
113 | 0 | chip = of_find_gpiochip_by_xlate(&gpiospec); |
114 | 0 | if (!chip) { |
115 | 0 | ret = -EPROBE_DEFER; |
116 | 0 | goto out; |
117 | 0 | } |
118 | | |
119 | 0 | ret = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags); |
120 | 0 | if (ret < 0) { |
121 | 0 | pr_err("%s: unable to get gpio num of device %s: %d\n", |
122 | 0 | __func__, dev_name(chip->dev), ret); |
123 | 0 | goto out; |
124 | 0 | } |
125 | | |
126 | 0 | if (flags) |
127 | 0 | of_gpio_flags_quirks(np, propname, flags, index); |
128 | |
|
129 | 0 | out: |
130 | 0 | of_node_put(gpiospec.np); |
131 | |
|
132 | 0 | return ret; |
133 | 0 | } |
134 | | EXPORT_SYMBOL(of_get_named_gpio_flags); |