dm: pinctrl: introduce PINCONF_RECURSIVE option
[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         if (!dev_of_valid(dev))
120                 return 0;
121
122         dev_for_each_subnode(node, dev) {
123                 if (pre_reloc_only &&
124                     !ofnode_pre_reloc(node))
125                         continue;
126                 /*
127                  * If this node has "compatible" property, this is not
128                  * a pin configuration node, but a normal device. skip.
129                  */
130                 ofnode_get_property(node, "compatible", &ret);
131                 if (ret >= 0)
132                         continue;
133                 /* If this node has "gpio-controller" property, skip */
134                 if (ofnode_read_bool(node, "gpio-controller"))
135                         continue;
136
137                 if (ret != -FDT_ERR_NOTFOUND)
138                         return ret;
139
140                 name = ofnode_get_name(node);
141                 if (!name)
142                         return -EINVAL;
143                 ret = device_bind_driver_to_node(dev, "pinconfig", name,
144                                                  node, NULL);
145                 if (ret)
146                         return ret;
147         }
148
149         return 0;
150 }
151
152 UCLASS_DRIVER(pinconfig) = {
153         .id = UCLASS_PINCONFIG,
154 #if CONFIG_IS_ENABLED(PINCONFIG_RECURSIVE)
155         .post_bind = pinconfig_post_bind,
156 #endif
157         .name = "pinconfig",
158 };
159
160 U_BOOT_DRIVER(pinconfig_generic) = {
161         .name = "pinconfig",
162         .id = UCLASS_PINCONFIG,
163 };
164
165 #else
166 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
167 {
168         return -ENODEV;
169 }
170
171 static int pinconfig_post_bind(struct udevice *dev)
172 {
173         return 0;
174 }
175 #endif
176
177 static int
178 pinctrl_gpio_get_pinctrl_and_offset(struct udevice *dev, unsigned offset,
179                                     struct udevice **pctldev,
180                                     unsigned int *pin_selector)
181 {
182         struct ofnode_phandle_args args;
183         unsigned gpio_offset, pfc_base, pfc_pins;
184         int ret;
185
186         ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
187                                          0, &args);
188         if (ret) {
189                 dev_dbg(dev, "%s: dev_read_phandle_with_args: err=%d\n",
190                         __func__, ret);
191                 return ret;
192         }
193
194         ret = uclass_get_device_by_ofnode(UCLASS_PINCTRL,
195                                           args.node, pctldev);
196         if (ret) {
197                 dev_dbg(dev,
198                         "%s: uclass_get_device_by_of_offset failed: err=%d\n",
199                         __func__, ret);
200                 return ret;
201         }
202
203         gpio_offset = args.args[0];
204         pfc_base = args.args[1];
205         pfc_pins = args.args[2];
206
207         if (offset < gpio_offset || offset > gpio_offset + pfc_pins) {
208                 dev_dbg(dev,
209                         "%s: GPIO can not be mapped to pincontrol pin\n",
210                         __func__);
211                 return -EINVAL;
212         }
213
214         offset -= gpio_offset;
215         offset += pfc_base;
216         *pin_selector = offset;
217
218         return 0;
219 }
220
221 /**
222  * pinctrl_gpio_request() - request a single pin to be used as GPIO
223  *
224  * @dev: GPIO peripheral device
225  * @offset: the GPIO pin offset from the GPIO controller
226  * @return: 0 on success, or negative error code on failure
227  */
228 int pinctrl_gpio_request(struct udevice *dev, unsigned offset)
229 {
230         const struct pinctrl_ops *ops;
231         struct udevice *pctldev;
232         unsigned int pin_selector;
233         int ret;
234
235         ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
236                                                   &pctldev, &pin_selector);
237         if (ret)
238                 return ret;
239
240         ops = pinctrl_get_ops(pctldev);
241         if (!ops || !ops->gpio_request_enable)
242                 return -ENOTSUPP;
243
244         return ops->gpio_request_enable(pctldev, pin_selector);
245 }
246
247 /**
248  * pinctrl_gpio_free() - free a single pin used as GPIO
249  *
250  * @dev: GPIO peripheral device
251  * @offset: the GPIO pin offset from the GPIO controller
252  * @return: 0 on success, or negative error code on failure
253  */
254 int pinctrl_gpio_free(struct udevice *dev, unsigned offset)
255 {
256         const struct pinctrl_ops *ops;
257         struct udevice *pctldev;
258         unsigned int pin_selector;
259         int ret;
260
261         ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
262                                                   &pctldev, &pin_selector);
263         if (ret)
264                 return ret;
265
266         ops = pinctrl_get_ops(pctldev);
267         if (!ops || !ops->gpio_disable_free)
268                 return -ENOTSUPP;
269
270         return ops->gpio_disable_free(pctldev, pin_selector);
271 }
272
273 /**
274  * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
275  *
276  * @dev: peripheral device
277  * @return: 0 on success, or negative error code on failure
278  */
279 static int pinctrl_select_state_simple(struct udevice *dev)
280 {
281         struct udevice *pctldev;
282         struct pinctrl_ops *ops;
283         int ret;
284
285         /*
286          * For most system, there is only one pincontroller device. But in
287          * case of multiple pincontroller devices, probe the one with sequence
288          * number 0 (defined by alias) to avoid race condition.
289          */
290         ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev);
291         if (ret)
292                 /* if not found, get the first one */
293                 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
294         if (ret)
295                 return ret;
296
297         ops = pinctrl_get_ops(pctldev);
298         if (!ops->set_state_simple) {
299                 dev_dbg(dev, "set_state_simple op missing\n");
300                 return -ENOSYS;
301         }
302
303         return ops->set_state_simple(pctldev, dev);
304 }
305
306 int pinctrl_select_state(struct udevice *dev, const char *statename)
307 {
308         /*
309          * Some device which is logical like mmc.blk, do not have
310          * a valid ofnode.
311          */
312         if (!ofnode_valid(dev->node))
313                 return 0;
314         /*
315          * Try full-implemented pinctrl first.
316          * If it fails or is not implemented, try simple one.
317          */
318         if (pinctrl_select_state_full(dev, statename))
319                 return pinctrl_select_state_simple(dev);
320
321         return 0;
322 }
323
324 int pinctrl_request(struct udevice *dev, int func, int flags)
325 {
326         struct pinctrl_ops *ops = pinctrl_get_ops(dev);
327
328         if (!ops->request)
329                 return -ENOSYS;
330
331         return ops->request(dev, func, flags);
332 }
333
334 int pinctrl_request_noflags(struct udevice *dev, int func)
335 {
336         return pinctrl_request(dev, func, 0);
337 }
338
339 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
340 {
341         struct pinctrl_ops *ops = pinctrl_get_ops(dev);
342
343         if (!ops->get_periph_id)
344                 return -ENOSYS;
345
346         return ops->get_periph_id(dev, periph);
347 }
348
349 int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
350 {
351         struct pinctrl_ops *ops = pinctrl_get_ops(dev);
352
353         if (!ops->get_gpio_mux)
354                 return -ENOSYS;
355
356         return ops->get_gpio_mux(dev, banknum, index);
357 }
358
359 int pinctrl_get_pins_count(struct udevice *dev)
360 {
361         struct pinctrl_ops *ops = pinctrl_get_ops(dev);
362
363         if (!ops->get_pins_count)
364                 return -ENOSYS;
365
366         return ops->get_pins_count(dev);
367 }
368
369 int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
370                          int size)
371 {
372         struct pinctrl_ops *ops = pinctrl_get_ops(dev);
373
374         if (!ops->get_pin_name)
375                 return -ENOSYS;
376
377         snprintf(buf, size, ops->get_pin_name(dev, selector));
378
379         return 0;
380 }
381
382 int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf,
383                            int size)
384 {
385         struct pinctrl_ops *ops = pinctrl_get_ops(dev);
386
387         if (!ops->get_pin_muxing)
388                 return -ENOSYS;
389
390         return ops->get_pin_muxing(dev, selector, buf, size);
391 }
392
393 /**
394  * pinconfig_post_bind() - post binding for PINCTRL uclass
395  * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
396  *
397  * @dev: pinctrl device
398  * @return: 0 on success, or negative error code on failure
399  */
400 static int pinctrl_post_bind(struct udevice *dev)
401 {
402         const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
403
404         if (!ops) {
405                 dev_dbg(dev, "ops is not set.  Do not bind.\n");
406                 return -EINVAL;
407         }
408
409         /*
410          * If set_state callback is set, we assume this pinctrl driver is the
411          * full implementation.  In this case, its child nodes should be bound
412          * so that peripheral devices can easily search in parent devices
413          * during later DT-parsing.
414          */
415         if (ops->set_state)
416                 return pinconfig_post_bind(dev);
417
418         return 0;
419 }
420
421 UCLASS_DRIVER(pinctrl) = {
422         .id = UCLASS_PINCTRL,
423         .post_bind = pinctrl_post_bind,
424         .flags = DM_UC_FLAG_SEQ_ALIAS,
425         .name = "pinctrl",
426 };