blob: 6e68e52c32cca4e083ddadba3acbe031d40673a0 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamadad90a5a32015-08-27 12:44:29 +09002/*
3 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamadad90a5a32015-08-27 12:44:29 +09004 */
5
6#include <common.h>
Simon Glass336d4612020-02-03 07:36:16 -07007#include <malloc.h>
Simon Glass401d1c42020-10-30 21:38:53 -06008#include <asm/global_data.h>
Simon Glass336d4612020-02-03 07:36:16 -07009#include <dm/device_compat.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090010#include <linux/libfdt.h>
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090011#include <linux/err.h>
12#include <linux/list.h>
Simon Glass9d922452017-05-17 17:18:03 -060013#include <dm.h>
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090014#include <dm/lists.h>
15#include <dm/pinctrl.h>
Heiko Stübner27326c72017-02-18 19:46:21 +010016#include <dm/util.h>
Kever Yang1e656ad2018-02-09 10:56:24 +080017#include <dm/of_access.h>
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090018
19DECLARE_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 */
28static 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 */
55static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
56{
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090057 char propname[32]; /* long enough */
58 const fdt32_t *list;
59 uint32_t phandle;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090060 struct udevice *config;
61 int state, size, i, ret;
62
Kever Yang1e656ad2018-02-09 10:56:24 +080063 state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090064 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);
Kever Yang1e656ad2018-02-09 10:56:24 +080076 list = dev_read_prop(dev, propname, &size);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090077 if (!list)
78 return -EINVAL;
79
80 size /= sizeof(*list);
81 for (i = 0; i < size; i++) {
82 phandle = fdt32_to_cpu(*list++);
Kever Yang1e656ad2018-02-09 10:56:24 +080083 ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
84 &config);
Michael Trimarchi36a90ed2019-09-17 22:06:03 +020085 if (ret) {
86 dev_warn(dev, "%s: uclass_get_device_by_phandle_id: err=%d\n",
87 __func__, ret);
88 continue;
89 }
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090090
91 ret = pinctrl_config_one(config);
Michael Trimarchi36a90ed2019-09-17 22:06:03 +020092 if (ret) {
93 dev_warn(dev, "%s: pinctrl_config_one: err=%d\n",
94 __func__, ret);
95 continue;
96 }
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090097 }
98
99 return 0;
100}
101
102/**
Masahiro Yamadaf8357062016-08-19 18:26:54 +0900103 * pinconfig_post_bind() - post binding for PINCONFIG uclass
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900104 * Recursively bind its children as pinconfig devices.
105 *
106 * @dev: pinconfig device
107 * @return: 0 on success, or negative error code on failure
108 */
109static int pinconfig_post_bind(struct udevice *dev)
110{
Simon Glass5589a812015-12-29 05:22:52 -0700111 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900112 const char *name;
Simon Glass45a26862017-05-18 20:09:07 -0600113 ofnode node;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900114 int ret;
115
Simon Glass7d14ee42020-12-19 10:40:13 -0700116 if (!dev_has_ofnode(dev))
Urja Rannikko63402832019-03-22 19:14:33 +0000117 return 0;
118
Simon Glass45a26862017-05-18 20:09:07 -0600119 dev_for_each_subnode(node, dev) {
Lukasz Majewskib6a62382019-01-09 23:05:02 +0100120 if (pre_reloc_only &&
121 !ofnode_pre_reloc(node))
Simon Glass5589a812015-12-29 05:22:52 -0700122 continue;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900123 /*
124 * If this node has "compatible" property, this is not
125 * a pin configuration node, but a normal device. skip.
126 */
Masahiro Yamada61e51ba2017-06-22 16:54:05 +0900127 ofnode_get_property(node, "compatible", &ret);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900128 if (ret >= 0)
129 continue;
Patrick Delaunayd39e2bd2019-02-25 13:39:56 +0100130 /* If this node has "gpio-controller" property, skip */
131 if (ofnode_read_bool(node, "gpio-controller"))
132 continue;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900133
134 if (ret != -FDT_ERR_NOTFOUND)
135 return ret;
136
Simon Glass45a26862017-05-18 20:09:07 -0600137 name = ofnode_get_name(node);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900138 if (!name)
139 return -EINVAL;
140 ret = device_bind_driver_to_node(dev, "pinconfig", name,
Simon Glass45a26862017-05-18 20:09:07 -0600141 node, NULL);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900142 if (ret)
143 return ret;
144 }
145
146 return 0;
147}
148
149UCLASS_DRIVER(pinconfig) = {
150 .id = UCLASS_PINCONFIG,
Patrick Delaunay44510da2019-10-21 15:02:40 +0200151#if CONFIG_IS_ENABLED(PINCONF_RECURSIVE)
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900152 .post_bind = pinconfig_post_bind,
Patrick Delaunayc20851b2019-08-02 14:48:00 +0200153#endif
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900154 .name = "pinconfig",
155};
156
157U_BOOT_DRIVER(pinconfig_generic) = {
158 .name = "pinconfig",
159 .id = UCLASS_PINCONFIG,
160};
161
162#else
163static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
164{
165 return -ENODEV;
166}
167
168static int pinconfig_post_bind(struct udevice *dev)
169{
Masahiro Yamada8a5f6122015-09-06 01:44:50 +0900170 return 0;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900171}
172#endif
173
Marek Vasutae59d7c2019-04-21 23:57:23 +0200174static int
175pinctrl_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 */
225int 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);
Simon Glassbddac452021-03-25 10:26:11 +1300238 assert(ops);
239 if (!ops->gpio_request_enable)
240 return -ENOSYS;
Marek Vasutae59d7c2019-04-21 23:57:23 +0200241
242 return ops->gpio_request_enable(pctldev, pin_selector);
243}
244
245/**
246 * pinctrl_gpio_free() - free a single pin used as GPIO
247 *
248 * @dev: GPIO peripheral device
249 * @offset: the GPIO pin offset from the GPIO controller
250 * @return: 0 on success, or negative error code on failure
251 */
252int pinctrl_gpio_free(struct udevice *dev, unsigned offset)
253{
254 const struct pinctrl_ops *ops;
255 struct udevice *pctldev;
256 unsigned int pin_selector;
257 int ret;
258
259 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
260 &pctldev, &pin_selector);
261 if (ret)
262 return ret;
263
264 ops = pinctrl_get_ops(pctldev);
Simon Glassbddac452021-03-25 10:26:11 +1300265 assert(ops);
266 if (!ops->gpio_disable_free)
267 return -ENOSYS;
Marek Vasutae59d7c2019-04-21 23:57:23 +0200268
269 return ops->gpio_disable_free(pctldev, pin_selector);
270}
271
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900272/**
273 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
274 *
275 * @dev: peripheral device
276 * @return: 0 on success, or negative error code on failure
277 */
278static int pinctrl_select_state_simple(struct udevice *dev)
279{
280 struct udevice *pctldev;
281 struct pinctrl_ops *ops;
282 int ret;
283
284 /*
Patrice Chotarddce406e2019-02-25 13:39:55 +0100285 * For most system, there is only one pincontroller device. But in
286 * case of multiple pincontroller devices, probe the one with sequence
287 * number 0 (defined by alias) to avoid race condition.
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900288 */
Patrice Chotarddce406e2019-02-25 13:39:55 +0100289 ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev);
290 if (ret)
291 /* if not found, get the first one */
292 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900293 if (ret)
294 return ret;
295
296 ops = pinctrl_get_ops(pctldev);
297 if (!ops->set_state_simple) {
298 dev_dbg(dev, "set_state_simple op missing\n");
299 return -ENOSYS;
300 }
301
302 return ops->set_state_simple(pctldev, dev);
303}
304
305int pinctrl_select_state(struct udevice *dev, const char *statename)
306{
307 /*
Kever Yangf717b4c2018-04-18 17:54:04 +0800308 * Some device which is logical like mmc.blk, do not have
309 * a valid ofnode.
310 */
Simon Glassc23405f2020-12-19 10:40:12 -0700311 if (!dev_has_ofnode(dev))
Kever Yangf717b4c2018-04-18 17:54:04 +0800312 return 0;
313 /*
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900314 * Try full-implemented pinctrl first.
315 * If it fails or is not implemented, try simple one.
316 */
317 if (pinctrl_select_state_full(dev, statename))
318 return pinctrl_select_state_simple(dev);
319
320 return 0;
321}
322
Simon Glassc5acf4a2015-08-30 16:55:13 -0600323int pinctrl_request(struct udevice *dev, int func, int flags)
324{
325 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
326
327 if (!ops->request)
328 return -ENOSYS;
329
330 return ops->request(dev, func, flags);
331}
332
333int pinctrl_request_noflags(struct udevice *dev, int func)
334{
335 return pinctrl_request(dev, func, 0);
336}
337
338int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
339{
340 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
341
342 if (!ops->get_periph_id)
343 return -ENOSYS;
344
345 return ops->get_periph_id(dev, periph);
346}
347
Simon Glass77eaa192016-01-21 19:43:56 -0700348int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
349{
350 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
351
352 if (!ops->get_gpio_mux)
353 return -ENOSYS;
354
355 return ops->get_gpio_mux(dev, banknum, index);
356}
357
Patrice Chotard8bbb5b22018-10-24 14:10:14 +0200358int pinctrl_get_pins_count(struct udevice *dev)
359{
360 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
361
362 if (!ops->get_pins_count)
363 return -ENOSYS;
364
365 return ops->get_pins_count(dev);
366}
367
368int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
369 int size)
370{
371 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
372
373 if (!ops->get_pin_name)
374 return -ENOSYS;
375
376 snprintf(buf, size, ops->get_pin_name(dev, selector));
377
378 return 0;
379}
380
Patrice Chotardf55a0c02018-10-24 14:10:13 +0200381int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf,
382 int size)
383{
384 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
385
386 if (!ops->get_pin_muxing)
387 return -ENOSYS;
388
389 return ops->get_pin_muxing(dev, selector, buf, size);
390}
391
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900392/**
Masahiro Yamadaf8357062016-08-19 18:26:54 +0900393 * pinconfig_post_bind() - post binding for PINCTRL uclass
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900394 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
395 *
396 * @dev: pinctrl device
397 * @return: 0 on success, or negative error code on failure
398 */
Patrick Delaunaye878b532019-08-02 14:48:00 +0200399static int __maybe_unused pinctrl_post_bind(struct udevice *dev)
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900400{
401 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
402
403 if (!ops) {
404 dev_dbg(dev, "ops is not set. Do not bind.\n");
405 return -EINVAL;
406 }
407
408 /*
Masahiro Yamada8a5f6122015-09-06 01:44:50 +0900409 * If set_state callback is set, we assume this pinctrl driver is the
410 * full implementation. In this case, its child nodes should be bound
411 * so that peripheral devices can easily search in parent devices
412 * during later DT-parsing.
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900413 */
Masahiro Yamada8a5f6122015-09-06 01:44:50 +0900414 if (ops->set_state)
415 return pinconfig_post_bind(dev);
416
417 return 0;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900418}
419
420UCLASS_DRIVER(pinctrl) = {
421 .id = UCLASS_PINCTRL,
Simon Glassc8fbf302020-12-23 08:11:13 -0700422#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900423 .post_bind = pinctrl_post_bind,
Simon Glassc8fbf302020-12-23 08:11:13 -0700424#endif
Thomas Abrahamac985272016-04-23 22:18:07 +0530425 .flags = DM_UC_FLAG_SEQ_ALIAS,
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900426 .name = "pinctrl",
427};