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