1 | // SPDX-License-Identifier: GPL-2.0+ |
2 | /* |
3 | * Software Node helpers for the GPIO API |
4 | * |
5 | * Copyright 2022 Google LLC |
6 | */ |
7 | #include <linux/err.h> |
8 | #include <linux/errno.h> |
9 | #include <linux/kernel.h> |
10 | #include <linux/printk.h> |
11 | #include <linux/property.h> |
12 | #include <linux/string.h> |
13 | |
14 | #include <linux/gpio/consumer.h> |
15 | #include <linux/gpio/driver.h> |
16 | |
17 | #include "gpiolib.h" |
18 | #include "gpiolib-swnode.h" |
19 | |
20 | static void swnode_format_propname(const char *con_id, char *propname, |
21 | size_t max_size) |
22 | { |
23 | /* |
24 | * Note we do not need to try both -gpios and -gpio suffixes, |
25 | * as, unlike OF and ACPI, we can fix software nodes to conform |
26 | * to the proper binding. |
27 | */ |
28 | if (con_id) |
29 | snprintf(buf: propname, size: max_size, fmt: "%s-gpios" , con_id); |
30 | else |
31 | strscpy(propname, "gpios" , max_size); |
32 | } |
33 | |
34 | static struct gpio_device *swnode_get_gpio_device(struct fwnode_handle *fwnode) |
35 | { |
36 | const struct software_node *gdev_node; |
37 | struct gpio_device *gdev; |
38 | |
39 | gdev_node = to_software_node(fwnode); |
40 | if (!gdev_node || !gdev_node->name) |
41 | return ERR_PTR(error: -EINVAL); |
42 | |
43 | gdev = gpio_device_find_by_label(label: gdev_node->name); |
44 | return gdev ?: ERR_PTR(error: -EPROBE_DEFER); |
45 | } |
46 | |
47 | struct gpio_desc *swnode_find_gpio(struct fwnode_handle *fwnode, |
48 | const char *con_id, unsigned int idx, |
49 | unsigned long *flags) |
50 | { |
51 | const struct software_node *swnode; |
52 | struct fwnode_reference_args args; |
53 | struct gpio_desc *desc; |
54 | char propname[32]; /* 32 is max size of property name */ |
55 | int error; |
56 | |
57 | swnode = to_software_node(fwnode); |
58 | if (!swnode) |
59 | return ERR_PTR(error: -EINVAL); |
60 | |
61 | swnode_format_propname(con_id, propname, max_size: sizeof(propname)); |
62 | |
63 | /* |
64 | * We expect all swnode-described GPIOs have GPIO number and |
65 | * polarity arguments, hence nargs is set to 2. |
66 | */ |
67 | error = fwnode_property_get_reference_args(fwnode, prop: propname, NULL, nargs: 2, index: idx, args: &args); |
68 | if (error) { |
69 | pr_debug("%s: can't parse '%s' property of node '%pfwP[%d]'\n" , |
70 | __func__, propname, fwnode, idx); |
71 | return ERR_PTR(error); |
72 | } |
73 | |
74 | struct gpio_device *gdev __free(gpio_device_put) = |
75 | swnode_get_gpio_device(fwnode: args.fwnode); |
76 | fwnode_handle_put(fwnode: args.fwnode); |
77 | if (IS_ERR(ptr: gdev)) |
78 | return ERR_CAST(ptr: gdev); |
79 | |
80 | /* |
81 | * FIXME: The GPIO device reference is put at return but the descriptor |
82 | * is passed on. Find a proper solution. |
83 | */ |
84 | desc = gpio_device_get_desc(gdev, hwnum: args.args[0]); |
85 | *flags = args.args[1]; /* We expect native GPIO flags */ |
86 | |
87 | pr_debug("%s: parsed '%s' property of node '%pfwP[%d]' - status (%d)\n" , |
88 | __func__, propname, fwnode, idx, PTR_ERR_OR_ZERO(desc)); |
89 | |
90 | return desc; |
91 | } |
92 | |
93 | /** |
94 | * swnode_gpio_count - count the GPIOs associated with a device / function |
95 | * @fwnode: firmware node of the GPIO consumer, can be %NULL for |
96 | * system-global GPIOs |
97 | * @con_id: function within the GPIO consumer |
98 | * |
99 | * Return: |
100 | * The number of GPIOs associated with a device / function or %-ENOENT, |
101 | * if no GPIO has been assigned to the requested function. |
102 | */ |
103 | int swnode_gpio_count(const struct fwnode_handle *fwnode, const char *con_id) |
104 | { |
105 | struct fwnode_reference_args args; |
106 | char propname[32]; |
107 | int count; |
108 | |
109 | swnode_format_propname(con_id, propname, max_size: sizeof(propname)); |
110 | |
111 | /* |
112 | * This is not very efficient, but GPIO lists usually have only |
113 | * 1 or 2 entries. |
114 | */ |
115 | count = 0; |
116 | while (fwnode_property_get_reference_args(fwnode, prop: propname, NULL, nargs: 0, |
117 | index: count, args: &args) == 0) { |
118 | fwnode_handle_put(fwnode: args.fwnode); |
119 | count++; |
120 | } |
121 | |
122 | return count ?: -ENOENT; |
123 | } |
124 | |