blob: 0b1eb7fab4a454d6227364455c2c9c8e3637b7ae [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>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +09007#include <linux/libfdt.h>
Masahiro Yamadad90a5a32015-08-27 12:44:29 +09008#include <linux/err.h>
9#include <linux/list.h>
Simon Glass9d922452017-05-17 17:18:03 -060010#include <dm.h>
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090011#include <dm/lists.h>
12#include <dm/pinctrl.h>
Heiko Stübner27326c72017-02-18 19:46:21 +010013#include <dm/util.h>
Kever Yang1e656ad2018-02-09 10:56:24 +080014#include <dm/of_access.h>
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090015
16DECLARE_GLOBAL_DATA_PTR;
17
Simon Glass52db39a2016-01-21 19:43:26 -070018int 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
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090030#if CONFIG_IS_ENABLED(PINCTRL_FULL)
31/**
32 * pinctrl_config_one() - apply pinctrl settings for a single node
33 *
34 * @config: pin configuration node
35 * @return: 0 on success, or negative error code on failure
36 */
37static int pinctrl_config_one(struct udevice *config)
38{
39 struct udevice *pctldev;
40 const struct pinctrl_ops *ops;
41
42 pctldev = config;
43 for (;;) {
44 pctldev = dev_get_parent(pctldev);
45 if (!pctldev) {
46 dev_err(config, "could not find pctldev\n");
47 return -EINVAL;
48 }
49 if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
50 break;
51 }
52
53 ops = pinctrl_get_ops(pctldev);
54 return ops->set_state(pctldev, config);
55}
56
57/**
58 * pinctrl_select_state_full() - full implementation of pinctrl_select_state
59 *
60 * @dev: peripheral device
61 * @statename: state name, like "default"
62 * @return: 0 on success, or negative error code on failure
63 */
64static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
65{
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090066 char propname[32]; /* long enough */
67 const fdt32_t *list;
68 uint32_t phandle;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090069 struct udevice *config;
70 int state, size, i, ret;
71
Kever Yang1e656ad2018-02-09 10:56:24 +080072 state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090073 if (state < 0) {
74 char *end;
75 /*
76 * If statename is not found in "pinctrl-names",
77 * assume statename is just the integer state ID.
78 */
79 state = simple_strtoul(statename, &end, 10);
80 if (*end)
81 return -EINVAL;
82 }
83
84 snprintf(propname, sizeof(propname), "pinctrl-%d", state);
Kever Yang1e656ad2018-02-09 10:56:24 +080085 list = dev_read_prop(dev, propname, &size);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090086 if (!list)
87 return -EINVAL;
88
89 size /= sizeof(*list);
90 for (i = 0; i < size; i++) {
91 phandle = fdt32_to_cpu(*list++);
Kever Yang1e656ad2018-02-09 10:56:24 +080092 ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
93 &config);
Michael Trimarchi36a90ed2019-09-17 22:06:03 +020094 if (ret) {
95 dev_warn(dev, "%s: uclass_get_device_by_phandle_id: err=%d\n",
96 __func__, ret);
97 continue;
98 }
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090099
100 ret = pinctrl_config_one(config);
Michael Trimarchi36a90ed2019-09-17 22:06:03 +0200101 if (ret) {
102 dev_warn(dev, "%s: pinctrl_config_one: err=%d\n",
103 __func__, ret);
104 continue;
105 }
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900106 }
107
108 return 0;
109}
110
111/**
Masahiro Yamadaf8357062016-08-19 18:26:54 +0900112 * pinconfig_post_bind() - post binding for PINCONFIG uclass
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900113 * Recursively bind its children as pinconfig devices.
114 *
115 * @dev: pinconfig device
116 * @return: 0 on success, or negative error code on failure
117 */
118static int pinconfig_post_bind(struct udevice *dev)
119{
Simon Glass5589a812015-12-29 05:22:52 -0700120 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900121 const char *name;
Simon Glass45a26862017-05-18 20:09:07 -0600122 ofnode node;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900123 int ret;
124
Urja Rannikko63402832019-03-22 19:14:33 +0000125 if (!dev_of_valid(dev))
126 return 0;
127
Simon Glass45a26862017-05-18 20:09:07 -0600128 dev_for_each_subnode(node, dev) {
Lukasz Majewskib6a62382019-01-09 23:05:02 +0100129 if (pre_reloc_only &&
130 !ofnode_pre_reloc(node))
Simon Glass5589a812015-12-29 05:22:52 -0700131 continue;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900132 /*
133 * If this node has "compatible" property, this is not
134 * a pin configuration node, but a normal device. skip.
135 */
Masahiro Yamada61e51ba2017-06-22 16:54:05 +0900136 ofnode_get_property(node, "compatible", &ret);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900137 if (ret >= 0)
138 continue;
Patrick Delaunayd39e2bd2019-02-25 13:39:56 +0100139 /* If this node has "gpio-controller" property, skip */
140 if (ofnode_read_bool(node, "gpio-controller"))
141 continue;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900142
143 if (ret != -FDT_ERR_NOTFOUND)
144 return ret;
145
Simon Glass45a26862017-05-18 20:09:07 -0600146 name = ofnode_get_name(node);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900147 if (!name)
148 return -EINVAL;
149 ret = device_bind_driver_to_node(dev, "pinconfig", name,
Simon Glass45a26862017-05-18 20:09:07 -0600150 node, NULL);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900151 if (ret)
152 return ret;
153 }
154
155 return 0;
156}
157
158UCLASS_DRIVER(pinconfig) = {
159 .id = UCLASS_PINCONFIG,
Patrick Delaunayc20851b2019-08-02 14:48:00 +0200160#if CONFIG_IS_ENABLED(PINCONFIG_RECURSIVE)
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900161 .post_bind = pinconfig_post_bind,
Patrick Delaunayc20851b2019-08-02 14:48:00 +0200162#endif
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900163 .name = "pinconfig",
164};
165
166U_BOOT_DRIVER(pinconfig_generic) = {
167 .name = "pinconfig",
168 .id = UCLASS_PINCONFIG,
169};
170
171#else
172static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
173{
174 return -ENODEV;
175}
176
177static int pinconfig_post_bind(struct udevice *dev)
178{
Masahiro Yamada8a5f6122015-09-06 01:44:50 +0900179 return 0;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900180}
181#endif
182
Marek Vasutae59d7c2019-04-21 23:57:23 +0200183static int
184pinctrl_gpio_get_pinctrl_and_offset(struct udevice *dev, unsigned offset,
185 struct udevice **pctldev,
186 unsigned int *pin_selector)
187{
188 struct ofnode_phandle_args args;
189 unsigned gpio_offset, pfc_base, pfc_pins;
190 int ret;
191
192 ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
193 0, &args);
194 if (ret) {
195 dev_dbg(dev, "%s: dev_read_phandle_with_args: err=%d\n",
196 __func__, ret);
197 return ret;
198 }
199
200 ret = uclass_get_device_by_ofnode(UCLASS_PINCTRL,
201 args.node, pctldev);
202 if (ret) {
203 dev_dbg(dev,
204 "%s: uclass_get_device_by_of_offset failed: err=%d\n",
205 __func__, ret);
206 return ret;
207 }
208
209 gpio_offset = args.args[0];
210 pfc_base = args.args[1];
211 pfc_pins = args.args[2];
212
213 if (offset < gpio_offset || offset > gpio_offset + pfc_pins) {
214 dev_dbg(dev,
215 "%s: GPIO can not be mapped to pincontrol pin\n",
216 __func__);
217 return -EINVAL;
218 }
219
220 offset -= gpio_offset;
221 offset += pfc_base;
222 *pin_selector = offset;
223
224 return 0;
225}
226
227/**
228 * pinctrl_gpio_request() - request a single pin to be used as GPIO
229 *
230 * @dev: GPIO peripheral device
231 * @offset: the GPIO pin offset from the GPIO controller
232 * @return: 0 on success, or negative error code on failure
233 */
234int pinctrl_gpio_request(struct udevice *dev, unsigned offset)
235{
236 const struct pinctrl_ops *ops;
237 struct udevice *pctldev;
238 unsigned int pin_selector;
239 int ret;
240
241 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
242 &pctldev, &pin_selector);
243 if (ret)
244 return ret;
245
246 ops = pinctrl_get_ops(pctldev);
247 if (!ops || !ops->gpio_request_enable)
248 return -ENOTSUPP;
249
250 return ops->gpio_request_enable(pctldev, pin_selector);
251}
252
253/**
254 * pinctrl_gpio_free() - free a single pin used as GPIO
255 *
256 * @dev: GPIO peripheral device
257 * @offset: the GPIO pin offset from the GPIO controller
258 * @return: 0 on success, or negative error code on failure
259 */
260int pinctrl_gpio_free(struct udevice *dev, unsigned offset)
261{
262 const struct pinctrl_ops *ops;
263 struct udevice *pctldev;
264 unsigned int pin_selector;
265 int ret;
266
267 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
268 &pctldev, &pin_selector);
269 if (ret)
270 return ret;
271
272 ops = pinctrl_get_ops(pctldev);
273 if (!ops || !ops->gpio_disable_free)
274 return -ENOTSUPP;
275
276 return ops->gpio_disable_free(pctldev, pin_selector);
277}
278
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900279/**
280 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
281 *
282 * @dev: peripheral device
283 * @return: 0 on success, or negative error code on failure
284 */
285static int pinctrl_select_state_simple(struct udevice *dev)
286{
287 struct udevice *pctldev;
288 struct pinctrl_ops *ops;
289 int ret;
290
291 /*
Patrice Chotarddce406e2019-02-25 13:39:55 +0100292 * For most system, there is only one pincontroller device. But in
293 * case of multiple pincontroller devices, probe the one with sequence
294 * number 0 (defined by alias) to avoid race condition.
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900295 */
Patrice Chotarddce406e2019-02-25 13:39:55 +0100296 ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev);
297 if (ret)
298 /* if not found, get the first one */
299 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900300 if (ret)
301 return ret;
302
303 ops = pinctrl_get_ops(pctldev);
304 if (!ops->set_state_simple) {
305 dev_dbg(dev, "set_state_simple op missing\n");
306 return -ENOSYS;
307 }
308
309 return ops->set_state_simple(pctldev, dev);
310}
311
312int pinctrl_select_state(struct udevice *dev, const char *statename)
313{
314 /*
Kever Yangf717b4c2018-04-18 17:54:04 +0800315 * Some device which is logical like mmc.blk, do not have
316 * a valid ofnode.
317 */
318 if (!ofnode_valid(dev->node))
319 return 0;
320 /*
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900321 * Try full-implemented pinctrl first.
322 * If it fails or is not implemented, try simple one.
323 */
324 if (pinctrl_select_state_full(dev, statename))
325 return pinctrl_select_state_simple(dev);
326
327 return 0;
328}
329
Simon Glassc5acf4a2015-08-30 16:55:13 -0600330int pinctrl_request(struct udevice *dev, int func, int flags)
331{
332 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
333
334 if (!ops->request)
335 return -ENOSYS;
336
337 return ops->request(dev, func, flags);
338}
339
340int pinctrl_request_noflags(struct udevice *dev, int func)
341{
342 return pinctrl_request(dev, func, 0);
343}
344
345int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
346{
347 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
348
349 if (!ops->get_periph_id)
350 return -ENOSYS;
351
352 return ops->get_periph_id(dev, periph);
353}
354
Simon Glass77eaa192016-01-21 19:43:56 -0700355int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
356{
357 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
358
359 if (!ops->get_gpio_mux)
360 return -ENOSYS;
361
362 return ops->get_gpio_mux(dev, banknum, index);
363}
364
Patrice Chotard8bbb5b22018-10-24 14:10:14 +0200365int pinctrl_get_pins_count(struct udevice *dev)
366{
367 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
368
369 if (!ops->get_pins_count)
370 return -ENOSYS;
371
372 return ops->get_pins_count(dev);
373}
374
375int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
376 int size)
377{
378 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
379
380 if (!ops->get_pin_name)
381 return -ENOSYS;
382
383 snprintf(buf, size, ops->get_pin_name(dev, selector));
384
385 return 0;
386}
387
Patrice Chotardf55a0c02018-10-24 14:10:13 +0200388int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf,
389 int size)
390{
391 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
392
393 if (!ops->get_pin_muxing)
394 return -ENOSYS;
395
396 return ops->get_pin_muxing(dev, selector, buf, size);
397}
398
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900399/**
Masahiro Yamadaf8357062016-08-19 18:26:54 +0900400 * pinconfig_post_bind() - post binding for PINCTRL uclass
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900401 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
402 *
403 * @dev: pinctrl device
404 * @return: 0 on success, or negative error code on failure
405 */
Patrick Delaunaye878b532019-08-02 14:48:00 +0200406static int __maybe_unused pinctrl_post_bind(struct udevice *dev)
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900407{
408 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
409
410 if (!ops) {
411 dev_dbg(dev, "ops is not set. Do not bind.\n");
412 return -EINVAL;
413 }
414
415 /*
Masahiro Yamada8a5f6122015-09-06 01:44:50 +0900416 * If set_state callback is set, we assume this pinctrl driver is the
417 * full implementation. In this case, its child nodes should be bound
418 * so that peripheral devices can easily search in parent devices
419 * during later DT-parsing.
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900420 */
Masahiro Yamada8a5f6122015-09-06 01:44:50 +0900421 if (ops->set_state)
422 return pinconfig_post_bind(dev);
423
424 return 0;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900425}
426
427UCLASS_DRIVER(pinctrl) = {
428 .id = UCLASS_PINCTRL,
Patrick Delaunaye878b532019-08-02 14:48:00 +0200429#if CONFIG_IS_ENABLED(PINCONF_RECURSIVE)
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900430 .post_bind = pinctrl_post_bind,
Patrick Delaunaye878b532019-08-02 14:48:00 +0200431#endif
Thomas Abrahamac985272016-04-23 22:18:07 +0530432 .flags = DM_UC_FLAG_SEQ_ALIAS,
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900433 .name = "pinctrl",
434};