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