Merge tag 'xilinx-for-v2021.04-rc3' of https://gitlab.denx.de/u-boot/custodians/u...
[platform/kernel/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 <malloc.h>
8 #include <asm/global_data.h>
9 #include <dm/device_compat.h>
10 #include <linux/libfdt.h>
11 #include <linux/err.h>
12 #include <linux/list.h>
13 #include <dm.h>
14 #include <dm/lists.h>
15 #include <dm/pinctrl.h>
16 #include <dm/util.h>
17 #include <dm/of_access.h>
18
19 DECLARE_GLOBAL_DATA_PTR;
20
21 #if CONFIG_IS_ENABLED(PINCTRL_FULL)
22 /**
23  * pinctrl_config_one() - apply pinctrl settings for a single node
24  *
25  * @config: pin configuration node
26  * @return: 0 on success, or negative error code on failure
27  */
28 static int pinctrl_config_one(struct udevice *config)
29 {
30         struct udevice *pctldev;
31         const struct pinctrl_ops *ops;
32
33         pctldev = config;
34         for (;;) {
35                 pctldev = dev_get_parent(pctldev);
36                 if (!pctldev) {
37                         dev_err(config, "could not find pctldev\n");
38                         return -EINVAL;
39                 }
40                 if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
41                         break;
42         }
43
44         ops = pinctrl_get_ops(pctldev);
45         return ops->set_state(pctldev, config);
46 }
47
48 /**
49  * pinctrl_select_state_full() - full implementation of pinctrl_select_state
50  *
51  * @dev: peripheral device
52  * @statename: state name, like "default"
53  * @return: 0 on success, or negative error code on failure
54  */
55 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
56 {
57         char propname[32]; /* long enough */
58         const fdt32_t *list;
59         uint32_t phandle;
60         struct udevice *config;
61         int state, size, i, ret;
62
63         state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
64         if (state < 0) {
65                 char *end;
66                 /*
67                  * If statename is not found in "pinctrl-names",
68                  * assume statename is just the integer state ID.
69                  */
70                 state = simple_strtoul(statename, &end, 10);
71                 if (*end)
72                         return -EINVAL;
73         }
74
75         snprintf(propname, sizeof(propname), "pinctrl-%d", state);
76         list = dev_read_prop(dev, propname, &size);
77         if (!list)
78                 return -EINVAL;
79
80         size /= sizeof(*list);
81         for (i = 0; i < size; i++) {
82                 phandle = fdt32_to_cpu(*list++);
83                 ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
84                                                       &config);
85                 if (ret) {
86                         dev_warn(dev, "%s: uclass_get_device_by_phandle_id: err=%d\n",
87                                 __func__, ret);
88                         continue;
89                 }
90
91                 ret = pinctrl_config_one(config);
92                 if (ret) {
93                         dev_warn(dev, "%s: pinctrl_config_one: err=%d\n",
94                                 __func__, ret);
95                         continue;
96                 }
97         }
98
99         return 0;
100 }
101
102 /**
103  * pinconfig_post_bind() - post binding for PINCONFIG uclass
104  * Recursively bind its children as pinconfig devices.
105  *
106  * @dev: pinconfig device
107  * @return: 0 on success, or negative error code on failure
108  */
109 static int pinconfig_post_bind(struct udevice *dev)
110 {
111         bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
112         const char *name;
113         ofnode node;
114         int ret;
115
116         if (!dev_has_ofnode(dev))
117                 return 0;
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 #if CONFIG_IS_ENABLED(PINCONF_RECURSIVE)
152         .post_bind = pinconfig_post_bind,
153 #endif
154         .name = "pinconfig",
155 };
156
157 U_BOOT_DRIVER(pinconfig_generic) = {
158         .name = "pinconfig",
159         .id = UCLASS_PINCONFIG,
160 };
161
162 #else
163 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
164 {
165         return -ENODEV;
166 }
167
168 static int pinconfig_post_bind(struct udevice *dev)
169 {
170         return 0;
171 }
172 #endif
173
174 static int
175 pinctrl_gpio_get_pinctrl_and_offset(struct udevice *dev, unsigned offset,
176                                     struct udevice **pctldev,
177                                     unsigned int *pin_selector)
178 {
179         struct ofnode_phandle_args args;
180         unsigned gpio_offset, pfc_base, pfc_pins;
181         int ret;
182
183         ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
184                                          0, &args);
185         if (ret) {
186                 dev_dbg(dev, "%s: dev_read_phandle_with_args: err=%d\n",
187                         __func__, ret);
188                 return ret;
189         }
190
191         ret = uclass_get_device_by_ofnode(UCLASS_PINCTRL,
192                                           args.node, pctldev);
193         if (ret) {
194                 dev_dbg(dev,
195                         "%s: uclass_get_device_by_of_offset failed: err=%d\n",
196                         __func__, ret);
197                 return ret;
198         }
199
200         gpio_offset = args.args[0];
201         pfc_base = args.args[1];
202         pfc_pins = args.args[2];
203
204         if (offset < gpio_offset || offset > gpio_offset + pfc_pins) {
205                 dev_dbg(dev,
206                         "%s: GPIO can not be mapped to pincontrol pin\n",
207                         __func__);
208                 return -EINVAL;
209         }
210
211         offset -= gpio_offset;
212         offset += pfc_base;
213         *pin_selector = offset;
214
215         return 0;
216 }
217
218 /**
219  * pinctrl_gpio_request() - request a single pin to be used as GPIO
220  *
221  * @dev: GPIO peripheral device
222  * @offset: the GPIO pin offset from the GPIO controller
223  * @return: 0 on success, or negative error code on failure
224  */
225 int pinctrl_gpio_request(struct udevice *dev, unsigned offset)
226 {
227         const struct pinctrl_ops *ops;
228         struct udevice *pctldev;
229         unsigned int pin_selector;
230         int ret;
231
232         ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
233                                                   &pctldev, &pin_selector);
234         if (ret)
235                 return ret;
236
237         ops = pinctrl_get_ops(pctldev);
238         if (!ops || !ops->gpio_request_enable)
239                 return -ENOTSUPP;
240
241         return ops->gpio_request_enable(pctldev, pin_selector);
242 }
243
244 /**
245  * pinctrl_gpio_free() - free a single pin used as GPIO
246  *
247  * @dev: GPIO peripheral device
248  * @offset: the GPIO pin offset from the GPIO controller
249  * @return: 0 on success, or negative error code on failure
250  */
251 int pinctrl_gpio_free(struct udevice *dev, unsigned offset)
252 {
253         const struct pinctrl_ops *ops;
254         struct udevice *pctldev;
255         unsigned int pin_selector;
256         int ret;
257
258         ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
259                                                   &pctldev, &pin_selector);
260         if (ret)
261                 return ret;
262
263         ops = pinctrl_get_ops(pctldev);
264         if (!ops || !ops->gpio_disable_free)
265                 return -ENOTSUPP;
266
267         return ops->gpio_disable_free(pctldev, pin_selector);
268 }
269
270 /**
271  * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
272  *
273  * @dev: peripheral device
274  * @return: 0 on success, or negative error code on failure
275  */
276 static int pinctrl_select_state_simple(struct udevice *dev)
277 {
278         struct udevice *pctldev;
279         struct pinctrl_ops *ops;
280         int ret;
281
282         /*
283          * For most system, there is only one pincontroller device. But in
284          * case of multiple pincontroller devices, probe the one with sequence
285          * number 0 (defined by alias) to avoid race condition.
286          */
287         ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev);
288         if (ret)
289                 /* if not found, get the first one */
290                 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
291         if (ret)
292                 return ret;
293
294         ops = pinctrl_get_ops(pctldev);
295         if (!ops->set_state_simple) {
296                 dev_dbg(dev, "set_state_simple op missing\n");
297                 return -ENOSYS;
298         }
299
300         return ops->set_state_simple(pctldev, dev);
301 }
302
303 int pinctrl_select_state(struct udevice *dev, const char *statename)
304 {
305         /*
306          * Some device which is logical like mmc.blk, do not have
307          * a valid ofnode.
308          */
309         if (!dev_has_ofnode(dev))
310                 return 0;
311         /*
312          * Try full-implemented pinctrl first.
313          * If it fails or is not implemented, try simple one.
314          */
315         if (pinctrl_select_state_full(dev, statename))
316                 return pinctrl_select_state_simple(dev);
317
318         return 0;
319 }
320
321 int pinctrl_request(struct udevice *dev, int func, int flags)
322 {
323         struct pinctrl_ops *ops = pinctrl_get_ops(dev);
324
325         if (!ops->request)
326                 return -ENOSYS;
327
328         return ops->request(dev, func, flags);
329 }
330
331 int pinctrl_request_noflags(struct udevice *dev, int func)
332 {
333         return pinctrl_request(dev, func, 0);
334 }
335
336 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
337 {
338         struct pinctrl_ops *ops = pinctrl_get_ops(dev);
339
340         if (!ops->get_periph_id)
341                 return -ENOSYS;
342
343         return ops->get_periph_id(dev, periph);
344 }
345
346 int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
347 {
348         struct pinctrl_ops *ops = pinctrl_get_ops(dev);
349
350         if (!ops->get_gpio_mux)
351                 return -ENOSYS;
352
353         return ops->get_gpio_mux(dev, banknum, index);
354 }
355
356 int pinctrl_get_pins_count(struct udevice *dev)
357 {
358         struct pinctrl_ops *ops = pinctrl_get_ops(dev);
359
360         if (!ops->get_pins_count)
361                 return -ENOSYS;
362
363         return ops->get_pins_count(dev);
364 }
365
366 int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
367                          int size)
368 {
369         struct pinctrl_ops *ops = pinctrl_get_ops(dev);
370
371         if (!ops->get_pin_name)
372                 return -ENOSYS;
373
374         snprintf(buf, size, ops->get_pin_name(dev, selector));
375
376         return 0;
377 }
378
379 int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf,
380                            int size)
381 {
382         struct pinctrl_ops *ops = pinctrl_get_ops(dev);
383
384         if (!ops->get_pin_muxing)
385                 return -ENOSYS;
386
387         return ops->get_pin_muxing(dev, selector, buf, size);
388 }
389
390 /**
391  * pinconfig_post_bind() - post binding for PINCTRL uclass
392  * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
393  *
394  * @dev: pinctrl device
395  * @return: 0 on success, or negative error code on failure
396  */
397 static int __maybe_unused pinctrl_post_bind(struct udevice *dev)
398 {
399         const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
400
401         if (!ops) {
402                 dev_dbg(dev, "ops is not set.  Do not bind.\n");
403                 return -EINVAL;
404         }
405
406         /*
407          * If set_state callback is set, we assume this pinctrl driver is the
408          * full implementation.  In this case, its child nodes should be bound
409          * so that peripheral devices can easily search in parent devices
410          * during later DT-parsing.
411          */
412         if (ops->set_state)
413                 return pinconfig_post_bind(dev);
414
415         return 0;
416 }
417
418 UCLASS_DRIVER(pinctrl) = {
419         .id = UCLASS_PINCTRL,
420 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
421         .post_bind = pinctrl_post_bind,
422 #endif
423         .flags = DM_UC_FLAG_SEQ_ALIAS,
424         .name = "pinctrl",
425 };