dm: pinctrl: Skip gpio-controller node in pinconfig_post_bind()
[oweals/u-boot.git] / drivers / pinctrl / pinctrl-uclass.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015  Masahiro Yamada <yamada.masahiro@socionext.com>
4  */
5
6 #include <common.h>
7 #include <linux/libfdt.h>
8 #include <linux/err.h>
9 #include <linux/list.h>
10 #include <dm.h>
11 #include <dm/lists.h>
12 #include <dm/pinctrl.h>
13 #include <dm/util.h>
14 #include <dm/of_access.h>
15
16 DECLARE_GLOBAL_DATA_PTR;
17
18 int pinctrl_decode_pin_config(const void *blob, int node)
19 {
20         int flags = 0;
21
22         if (fdtdec_get_bool(blob, node, "bias-pull-up"))
23                 flags |= 1 << PIN_CONFIG_BIAS_PULL_UP;
24         else if (fdtdec_get_bool(blob, node, "bias-pull-down"))
25                 flags |= 1 << PIN_CONFIG_BIAS_PULL_DOWN;
26
27         return flags;
28 }
29
30 #if CONFIG_IS_ENABLED(PINCTRL_FULL)
31 /**
32  * pinctrl_config_one() - apply pinctrl settings for a single node
33  *
34  * @config: pin configuration node
35  * @return: 0 on success, or negative error code on failure
36  */
37 static int pinctrl_config_one(struct udevice *config)
38 {
39         struct udevice *pctldev;
40         const struct pinctrl_ops *ops;
41
42         pctldev = config;
43         for (;;) {
44                 pctldev = dev_get_parent(pctldev);
45                 if (!pctldev) {
46                         dev_err(config, "could not find pctldev\n");
47                         return -EINVAL;
48                 }
49                 if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
50                         break;
51         }
52
53         ops = pinctrl_get_ops(pctldev);
54         return ops->set_state(pctldev, config);
55 }
56
57 /**
58  * pinctrl_select_state_full() - full implementation of pinctrl_select_state
59  *
60  * @dev: peripheral device
61  * @statename: state name, like "default"
62  * @return: 0 on success, or negative error code on failure
63  */
64 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
65 {
66         char propname[32]; /* long enough */
67         const fdt32_t *list;
68         uint32_t phandle;
69         struct udevice *config;
70         int state, size, i, ret;
71
72         state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
73         if (state < 0) {
74                 char *end;
75                 /*
76                  * If statename is not found in "pinctrl-names",
77                  * assume statename is just the integer state ID.
78                  */
79                 state = simple_strtoul(statename, &end, 10);
80                 if (*end)
81                         return -EINVAL;
82         }
83
84         snprintf(propname, sizeof(propname), "pinctrl-%d", state);
85         list = dev_read_prop(dev, propname, &size);
86         if (!list)
87                 return -EINVAL;
88
89         size /= sizeof(*list);
90         for (i = 0; i < size; i++) {
91                 phandle = fdt32_to_cpu(*list++);
92                 ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
93                                                       &config);
94                 if (ret)
95                         return ret;
96
97                 ret = pinctrl_config_one(config);
98                 if (ret)
99                         return ret;
100         }
101
102         return 0;
103 }
104
105 /**
106  * pinconfig_post_bind() - post binding for PINCONFIG uclass
107  * Recursively bind its children as pinconfig devices.
108  *
109  * @dev: pinconfig device
110  * @return: 0 on success, or negative error code on failure
111  */
112 static int pinconfig_post_bind(struct udevice *dev)
113 {
114         bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
115         const char *name;
116         ofnode node;
117         int ret;
118
119         dev_for_each_subnode(node, dev) {
120                 if (pre_reloc_only &&
121                     !ofnode_pre_reloc(node))
122                         continue;
123                 /*
124                  * If this node has "compatible" property, this is not
125                  * a pin configuration node, but a normal device. skip.
126                  */
127                 ofnode_get_property(node, "compatible", &ret);
128                 if (ret >= 0)
129                         continue;
130                 /* If this node has "gpio-controller" property, skip */
131                 if (ofnode_read_bool(node, "gpio-controller"))
132                         continue;
133
134                 if (ret != -FDT_ERR_NOTFOUND)
135                         return ret;
136
137                 name = ofnode_get_name(node);
138                 if (!name)
139                         return -EINVAL;
140                 ret = device_bind_driver_to_node(dev, "pinconfig", name,
141                                                  node, NULL);
142                 if (ret)
143                         return ret;
144         }
145
146         return 0;
147 }
148
149 UCLASS_DRIVER(pinconfig) = {
150         .id = UCLASS_PINCONFIG,
151         .post_bind = pinconfig_post_bind,
152         .name = "pinconfig",
153 };
154
155 U_BOOT_DRIVER(pinconfig_generic) = {
156         .name = "pinconfig",
157         .id = UCLASS_PINCONFIG,
158 };
159
160 #else
161 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
162 {
163         return -ENODEV;
164 }
165
166 static int pinconfig_post_bind(struct udevice *dev)
167 {
168         return 0;
169 }
170 #endif
171
172 /**
173  * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
174  *
175  * @dev: peripheral device
176  * @return: 0 on success, or negative error code on failure
177  */
178 static int pinctrl_select_state_simple(struct udevice *dev)
179 {
180         struct udevice *pctldev;
181         struct pinctrl_ops *ops;
182         int ret;
183
184         /*
185          * For most system, there is only one pincontroller device. But in
186          * case of multiple pincontroller devices, probe the one with sequence
187          * number 0 (defined by alias) to avoid race condition.
188          */
189         ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev);
190         if (ret)
191                 /* if not found, get the first one */
192                 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
193         if (ret)
194                 return ret;
195
196         ops = pinctrl_get_ops(pctldev);
197         if (!ops->set_state_simple) {
198                 dev_dbg(dev, "set_state_simple op missing\n");
199                 return -ENOSYS;
200         }
201
202         return ops->set_state_simple(pctldev, dev);
203 }
204
205 int pinctrl_select_state(struct udevice *dev, const char *statename)
206 {
207         /*
208          * Some device which is logical like mmc.blk, do not have
209          * a valid ofnode.
210          */
211         if (!ofnode_valid(dev->node))
212                 return 0;
213         /*
214          * Try full-implemented pinctrl first.
215          * If it fails or is not implemented, try simple one.
216          */
217         if (pinctrl_select_state_full(dev, statename))
218                 return pinctrl_select_state_simple(dev);
219
220         return 0;
221 }
222
223 int pinctrl_request(struct udevice *dev, int func, int flags)
224 {
225         struct pinctrl_ops *ops = pinctrl_get_ops(dev);
226
227         if (!ops->request)
228                 return -ENOSYS;
229
230         return ops->request(dev, func, flags);
231 }
232
233 int pinctrl_request_noflags(struct udevice *dev, int func)
234 {
235         return pinctrl_request(dev, func, 0);
236 }
237
238 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
239 {
240         struct pinctrl_ops *ops = pinctrl_get_ops(dev);
241
242         if (!ops->get_periph_id)
243                 return -ENOSYS;
244
245         return ops->get_periph_id(dev, periph);
246 }
247
248 int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
249 {
250         struct pinctrl_ops *ops = pinctrl_get_ops(dev);
251
252         if (!ops->get_gpio_mux)
253                 return -ENOSYS;
254
255         return ops->get_gpio_mux(dev, banknum, index);
256 }
257
258 int pinctrl_get_pins_count(struct udevice *dev)
259 {
260         struct pinctrl_ops *ops = pinctrl_get_ops(dev);
261
262         if (!ops->get_pins_count)
263                 return -ENOSYS;
264
265         return ops->get_pins_count(dev);
266 }
267
268 int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
269                          int size)
270 {
271         struct pinctrl_ops *ops = pinctrl_get_ops(dev);
272
273         if (!ops->get_pin_name)
274                 return -ENOSYS;
275
276         snprintf(buf, size, ops->get_pin_name(dev, selector));
277
278         return 0;
279 }
280
281 int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf,
282                            int size)
283 {
284         struct pinctrl_ops *ops = pinctrl_get_ops(dev);
285
286         if (!ops->get_pin_muxing)
287                 return -ENOSYS;
288
289         return ops->get_pin_muxing(dev, selector, buf, size);
290 }
291
292 /**
293  * pinconfig_post_bind() - post binding for PINCTRL uclass
294  * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
295  *
296  * @dev: pinctrl device
297  * @return: 0 on success, or negative error code on failure
298  */
299 static int pinctrl_post_bind(struct udevice *dev)
300 {
301         const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
302
303         if (!ops) {
304                 dev_dbg(dev, "ops is not set.  Do not bind.\n");
305                 return -EINVAL;
306         }
307
308         /*
309          * If set_state callback is set, we assume this pinctrl driver is the
310          * full implementation.  In this case, its child nodes should be bound
311          * so that peripheral devices can easily search in parent devices
312          * during later DT-parsing.
313          */
314         if (ops->set_state)
315                 return pinconfig_post_bind(dev);
316
317         return 0;
318 }
319
320 UCLASS_DRIVER(pinctrl) = {
321         .id = UCLASS_PINCTRL,
322         .post_bind = pinctrl_post_bind,
323         .flags = DM_UC_FLAG_SEQ_ALIAS,
324         .name = "pinctrl",
325 };