pinctrl: Avoid binding all pinconfig nodes before relocation
[oweals/u-boot.git] / drivers / pinctrl / pinctrl-uclass.c
1 /*
2  * Copyright (C) 2015  Masahiro Yamada <yamada.masahiro@socionext.com>
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6
7 #include <common.h>
8 #include <libfdt.h>
9 #include <linux/err.h>
10 #include <linux/list.h>
11 #include <dm/device.h>
12 #include <dm/lists.h>
13 #include <dm/pinctrl.h>
14 #include <dm/uclass.h>
15
16 DECLARE_GLOBAL_DATA_PTR;
17
18 #if CONFIG_IS_ENABLED(PINCTRL_FULL)
19 /**
20  * pinctrl_config_one() - apply pinctrl settings for a single node
21  *
22  * @config: pin configuration node
23  * @return: 0 on success, or negative error code on failure
24  */
25 static int pinctrl_config_one(struct udevice *config)
26 {
27         struct udevice *pctldev;
28         const struct pinctrl_ops *ops;
29
30         pctldev = config;
31         for (;;) {
32                 pctldev = dev_get_parent(pctldev);
33                 if (!pctldev) {
34                         dev_err(config, "could not find pctldev\n");
35                         return -EINVAL;
36                 }
37                 if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
38                         break;
39         }
40
41         ops = pinctrl_get_ops(pctldev);
42         return ops->set_state(pctldev, config);
43 }
44
45 /**
46  * pinctrl_select_state_full() - full implementation of pinctrl_select_state
47  *
48  * @dev: peripheral device
49  * @statename: state name, like "default"
50  * @return: 0 on success, or negative error code on failure
51  */
52 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
53 {
54         const void *fdt = gd->fdt_blob;
55         int node = dev->of_offset;
56         char propname[32]; /* long enough */
57         const fdt32_t *list;
58         uint32_t phandle;
59         int config_node;
60         struct udevice *config;
61         int state, size, i, ret;
62
63         state = fdt_find_string(fdt, node, "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 = fdt_getprop(fdt, node, 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
84                 config_node = fdt_node_offset_by_phandle(fdt, phandle);
85                 if (config_node < 0) {
86                         dev_err(dev, "prop %s index %d invalid phandle\n",
87                                 propname, i);
88                         return -EINVAL;
89                 }
90                 ret = uclass_get_device_by_of_offset(UCLASS_PINCONFIG,
91                                                      config_node, &config);
92                 if (ret)
93                         return ret;
94
95                 ret = pinctrl_config_one(config);
96                 if (ret)
97                         return ret;
98         }
99
100         return 0;
101 }
102
103 /**
104  * pinconfig_post-bind() - post binding for PINCONFIG uclass
105  * Recursively bind its children as pinconfig devices.
106  *
107  * @dev: pinconfig device
108  * @return: 0 on success, or negative error code on failure
109  */
110 static int pinconfig_post_bind(struct udevice *dev)
111 {
112         const void *fdt = gd->fdt_blob;
113         int offset = dev->of_offset;
114         bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
115         const char *name;
116         int ret;
117
118         for (offset = fdt_first_subnode(fdt, offset);
119              offset > 0;
120              offset = fdt_next_subnode(fdt, offset)) {
121                 if (pre_reloc_only &&
122                     !fdt_getprop(fdt, offset, "u-boot,dm-pre-reloc", NULL))
123                         continue;
124                 /*
125                  * If this node has "compatible" property, this is not
126                  * a pin configuration node, but a normal device. skip.
127                  */
128                 fdt_get_property(fdt, offset, "compatible", &ret);
129                 if (ret >= 0)
130                         continue;
131
132                 if (ret != -FDT_ERR_NOTFOUND)
133                         return ret;
134
135                 name = fdt_get_name(fdt, offset, NULL);
136                 if (!name)
137                         return -EINVAL;
138                 ret = device_bind_driver_to_node(dev, "pinconfig", name,
139                                                  offset, NULL);
140                 if (ret)
141                         return ret;
142         }
143
144         return 0;
145 }
146
147 UCLASS_DRIVER(pinconfig) = {
148         .id = UCLASS_PINCONFIG,
149         .post_bind = pinconfig_post_bind,
150         .name = "pinconfig",
151 };
152
153 U_BOOT_DRIVER(pinconfig_generic) = {
154         .name = "pinconfig",
155         .id = UCLASS_PINCONFIG,
156 };
157
158 #else
159 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
160 {
161         return -ENODEV;
162 }
163
164 static int pinconfig_post_bind(struct udevice *dev)
165 {
166         return 0;
167 }
168 #endif
169
170 /**
171  * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
172  *
173  * @dev: peripheral device
174  * @return: 0 on success, or negative error code on failure
175  */
176 static int pinctrl_select_state_simple(struct udevice *dev)
177 {
178         struct udevice *pctldev;
179         struct pinctrl_ops *ops;
180         int ret;
181
182         /*
183          * For simplicity, assume the first device of PINCTRL uclass
184          * is the correct one.  This is most likely OK as there is
185          * usually only one pinctrl device on the system.
186          */
187         ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
188         if (ret)
189                 return ret;
190
191         ops = pinctrl_get_ops(pctldev);
192         if (!ops->set_state_simple) {
193                 dev_dbg(dev, "set_state_simple op missing\n");
194                 return -ENOSYS;
195         }
196
197         return ops->set_state_simple(pctldev, dev);
198 }
199
200 int pinctrl_select_state(struct udevice *dev, const char *statename)
201 {
202         /*
203          * Try full-implemented pinctrl first.
204          * If it fails or is not implemented, try simple one.
205          */
206         if (pinctrl_select_state_full(dev, statename))
207                 return pinctrl_select_state_simple(dev);
208
209         return 0;
210 }
211
212 int pinctrl_request(struct udevice *dev, int func, int flags)
213 {
214         struct pinctrl_ops *ops = pinctrl_get_ops(dev);
215
216         if (!ops->request)
217                 return -ENOSYS;
218
219         return ops->request(dev, func, flags);
220 }
221
222 int pinctrl_request_noflags(struct udevice *dev, int func)
223 {
224         return pinctrl_request(dev, func, 0);
225 }
226
227 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
228 {
229         struct pinctrl_ops *ops = pinctrl_get_ops(dev);
230
231         if (!ops->get_periph_id)
232                 return -ENOSYS;
233
234         return ops->get_periph_id(dev, periph);
235 }
236
237 /**
238  * pinconfig_post-bind() - post binding for PINCTRL uclass
239  * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
240  *
241  * @dev: pinctrl device
242  * @return: 0 on success, or negative error code on failure
243  */
244 static int pinctrl_post_bind(struct udevice *dev)
245 {
246         const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
247
248         if (!ops) {
249                 dev_dbg(dev, "ops is not set.  Do not bind.\n");
250                 return -EINVAL;
251         }
252
253         /*
254          * If set_state callback is set, we assume this pinctrl driver is the
255          * full implementation.  In this case, its child nodes should be bound
256          * so that peripheral devices can easily search in parent devices
257          * during later DT-parsing.
258          */
259         if (ops->set_state)
260                 return pinconfig_post_bind(dev);
261
262         return 0;
263 }
264
265 UCLASS_DRIVER(pinctrl) = {
266         .id = UCLASS_PINCTRL,
267         .post_bind = pinctrl_post_bind,
268         .name = "pinctrl",
269 };