Commit | Line | Data |
---|---|---|
e7f9ff5d DT |
1 | // SPDX-License-Identifier: GPL-2.0+ |
2 | /* | |
3 | * Software Node helpers for the GPIO API | |
4 | * | |
5 | * Copyright 2022 Google LLC | |
6 | */ | |
9d50f95b CK |
7 | |
8 | #define pr_fmt(fmt) "gpiolib: swnode: " fmt | |
9 | ||
e7f9ff5d DT |
10 | #include <linux/err.h> |
11 | #include <linux/errno.h> | |
9d50f95b CK |
12 | #include <linux/export.h> |
13 | #include <linux/init.h> | |
e7f9ff5d DT |
14 | #include <linux/kernel.h> |
15 | #include <linux/printk.h> | |
16 | #include <linux/property.h> | |
17 | #include <linux/string.h> | |
18 | ||
380c7ba3 AS |
19 | #include <linux/gpio/consumer.h> |
20 | #include <linux/gpio/driver.h> | |
21 | ||
e7f9ff5d DT |
22 | #include "gpiolib.h" |
23 | #include "gpiolib-swnode.h" | |
24 | ||
9d50f95b CK |
25 | #define GPIOLIB_SWNODE_UNDEFINED_NAME "swnode-gpio-undefined" |
26 | ||
e7f9ff5d DT |
27 | static void swnode_format_propname(const char *con_id, char *propname, |
28 | size_t max_size) | |
29 | { | |
30 | /* | |
31 | * Note we do not need to try both -gpios and -gpio suffixes, | |
32 | * as, unlike OF and ACPI, we can fix software nodes to conform | |
33 | * to the proper binding. | |
34 | */ | |
35 | if (con_id) | |
36 | snprintf(propname, max_size, "%s-gpios", con_id); | |
37 | else | |
38 | strscpy(propname, "gpios", max_size); | |
39 | } | |
40 | ||
b7b56e64 | 41 | static struct gpio_device *swnode_get_gpio_device(struct fwnode_handle *fwnode) |
e7f9ff5d | 42 | { |
b7b56e64 BG |
43 | const struct software_node *gdev_node; |
44 | struct gpio_device *gdev; | |
e7f9ff5d | 45 | |
b7b56e64 BG |
46 | gdev_node = to_software_node(fwnode); |
47 | if (!gdev_node || !gdev_node->name) | |
e7f9ff5d DT |
48 | return ERR_PTR(-EINVAL); |
49 | ||
9d50f95b CK |
50 | /* |
51 | * Check for a special node that identifies undefined GPIOs, this is | |
52 | * primarily used as a key for internal chip selects in SPI bindings. | |
53 | */ | |
54 | if (IS_ENABLED(CONFIG_GPIO_SWNODE_UNDEFINED) && | |
55 | !strcmp(gdev_node->name, GPIOLIB_SWNODE_UNDEFINED_NAME)) | |
56 | return ERR_PTR(-ENOENT); | |
57 | ||
b7b56e64 BG |
58 | gdev = gpio_device_find_by_label(gdev_node->name); |
59 | return gdev ?: ERR_PTR(-EPROBE_DEFER); | |
e7f9ff5d DT |
60 | } |
61 | ||
62 | struct gpio_desc *swnode_find_gpio(struct fwnode_handle *fwnode, | |
63 | const char *con_id, unsigned int idx, | |
64 | unsigned long *flags) | |
65 | { | |
66 | const struct software_node *swnode; | |
67 | struct fwnode_reference_args args; | |
e7f9ff5d DT |
68 | struct gpio_desc *desc; |
69 | char propname[32]; /* 32 is max size of property name */ | |
70 | int error; | |
71 | ||
72 | swnode = to_software_node(fwnode); | |
73 | if (!swnode) | |
74 | return ERR_PTR(-EINVAL); | |
75 | ||
76 | swnode_format_propname(con_id, propname, sizeof(propname)); | |
77 | ||
78 | /* | |
79 | * We expect all swnode-described GPIOs have GPIO number and | |
80 | * polarity arguments, hence nargs is set to 2. | |
81 | */ | |
82 | error = fwnode_property_get_reference_args(fwnode, propname, NULL, 2, idx, &args); | |
83 | if (error) { | |
84 | pr_debug("%s: can't parse '%s' property of node '%pfwP[%d]'\n", | |
85 | __func__, propname, fwnode, idx); | |
86 | return ERR_PTR(error); | |
87 | } | |
88 | ||
b7b56e64 BG |
89 | struct gpio_device *gdev __free(gpio_device_put) = |
90 | swnode_get_gpio_device(args.fwnode); | |
e7f9ff5d | 91 | fwnode_handle_put(args.fwnode); |
b7b56e64 BG |
92 | if (IS_ERR(gdev)) |
93 | return ERR_CAST(gdev); | |
e7f9ff5d | 94 | |
b7b56e64 BG |
95 | /* |
96 | * FIXME: The GPIO device reference is put at return but the descriptor | |
97 | * is passed on. Find a proper solution. | |
98 | */ | |
99 | desc = gpio_device_get_desc(gdev, args.args[0]); | |
e7f9ff5d DT |
100 | *flags = args.args[1]; /* We expect native GPIO flags */ |
101 | ||
102 | pr_debug("%s: parsed '%s' property of node '%pfwP[%d]' - status (%d)\n", | |
103 | __func__, propname, fwnode, idx, PTR_ERR_OR_ZERO(desc)); | |
104 | ||
105 | return desc; | |
106 | } | |
107 | ||
108 | /** | |
109 | * swnode_gpio_count - count the GPIOs associated with a device / function | |
110 | * @fwnode: firmware node of the GPIO consumer, can be %NULL for | |
111 | * system-global GPIOs | |
112 | * @con_id: function within the GPIO consumer | |
113 | * | |
114 | * Return: | |
115 | * The number of GPIOs associated with a device / function or %-ENOENT, | |
116 | * if no GPIO has been assigned to the requested function. | |
117 | */ | |
118 | int swnode_gpio_count(const struct fwnode_handle *fwnode, const char *con_id) | |
119 | { | |
120 | struct fwnode_reference_args args; | |
121 | char propname[32]; | |
122 | int count; | |
123 | ||
124 | swnode_format_propname(con_id, propname, sizeof(propname)); | |
125 | ||
126 | /* | |
127 | * This is not very efficient, but GPIO lists usually have only | |
128 | * 1 or 2 entries. | |
129 | */ | |
130 | count = 0; | |
131 | while (fwnode_property_get_reference_args(fwnode, propname, NULL, 0, | |
132 | count, &args) == 0) { | |
133 | fwnode_handle_put(args.fwnode); | |
134 | count++; | |
135 | } | |
136 | ||
137 | return count ?: -ENOENT; | |
138 | } | |
9d50f95b CK |
139 | |
140 | #if IS_ENABLED(CONFIG_GPIO_SWNODE_UNDEFINED) | |
141 | /* | |
142 | * A special node that identifies undefined GPIOs, this is primarily used as | |
143 | * a key for internal chip selects in SPI bindings. | |
144 | */ | |
145 | const struct software_node swnode_gpio_undefined = { | |
146 | .name = GPIOLIB_SWNODE_UNDEFINED_NAME, | |
147 | }; | |
148 | EXPORT_SYMBOL_NS_GPL(swnode_gpio_undefined, GPIO_SWNODE); | |
149 | ||
150 | static int __init swnode_gpio_init(void) | |
151 | { | |
152 | int ret; | |
153 | ||
154 | ret = software_node_register(&swnode_gpio_undefined); | |
155 | if (ret < 0) | |
156 | pr_err("failed to register swnode: %d\n", ret); | |
157 | ||
158 | return ret; | |
159 | } | |
160 | subsys_initcall(swnode_gpio_init); | |
161 | ||
162 | static void __exit swnode_gpio_cleanup(void) | |
163 | { | |
164 | software_node_unregister(&swnode_gpio_undefined); | |
165 | } | |
166 | __exitcall(swnode_gpio_cleanup); | |
167 | #endif |