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