blob: dfe60b6dadb29b3867f269d13c381bb229734ff0 [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
Patrick Delaunayb953ec22021-04-27 11:02:19 +02006#define LOG_CATEGORY UCLASS_PINCTRL
7
Masahiro Yamadad90a5a32015-08-27 12:44:29 +09008#include <common.h>
Simon Glass336d4612020-02-03 07:36:16 -07009#include <malloc.h>
Simon Glass401d1c42020-10-30 21:38:53 -060010#include <asm/global_data.h>
Simon Glass336d4612020-02-03 07:36:16 -070011#include <dm/device_compat.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090012#include <linux/libfdt.h>
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090013#include <linux/err.h>
14#include <linux/list.h>
Simon Glass9d922452017-05-17 17:18:03 -060015#include <dm.h>
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090016#include <dm/lists.h>
17#include <dm/pinctrl.h>
Heiko Stübner27326c72017-02-18 19:46:21 +010018#include <dm/util.h>
Kever Yang1e656ad2018-02-09 10:56:24 +080019#include <dm/of_access.h>
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090020
21DECLARE_GLOBAL_DATA_PTR;
22
23#if CONFIG_IS_ENABLED(PINCTRL_FULL)
24/**
25 * pinctrl_config_one() - apply pinctrl settings for a single node
26 *
27 * @config: pin configuration node
28 * @return: 0 on success, or negative error code on failure
29 */
30static int pinctrl_config_one(struct udevice *config)
31{
32 struct udevice *pctldev;
33 const struct pinctrl_ops *ops;
34
35 pctldev = config;
36 for (;;) {
37 pctldev = dev_get_parent(pctldev);
38 if (!pctldev) {
39 dev_err(config, "could not find pctldev\n");
40 return -EINVAL;
41 }
42 if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
43 break;
44 }
45
46 ops = pinctrl_get_ops(pctldev);
47 return ops->set_state(pctldev, config);
48}
49
50/**
51 * pinctrl_select_state_full() - full implementation of pinctrl_select_state
52 *
53 * @dev: peripheral device
54 * @statename: state name, like "default"
55 * @return: 0 on success, or negative error code on failure
56 */
57static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
58{
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090059 char propname[32]; /* long enough */
60 const fdt32_t *list;
61 uint32_t phandle;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090062 struct udevice *config;
63 int state, size, i, ret;
64
Kever Yang1e656ad2018-02-09 10:56:24 +080065 state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090066 if (state < 0) {
67 char *end;
68 /*
69 * If statename is not found in "pinctrl-names",
70 * assume statename is just the integer state ID.
71 */
Simon Glass0b1284e2021-07-24 09:03:30 -060072 state = dectoul(statename, &end);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090073 if (*end)
74 return -EINVAL;
75 }
76
77 snprintf(propname, sizeof(propname), "pinctrl-%d", state);
Kever Yang1e656ad2018-02-09 10:56:24 +080078 list = dev_read_prop(dev, propname, &size);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090079 if (!list)
80 return -EINVAL;
81
82 size /= sizeof(*list);
83 for (i = 0; i < size; i++) {
84 phandle = fdt32_to_cpu(*list++);
Kever Yang1e656ad2018-02-09 10:56:24 +080085 ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
86 &config);
Michael Trimarchi36a90ed2019-09-17 22:06:03 +020087 if (ret) {
88 dev_warn(dev, "%s: uclass_get_device_by_phandle_id: err=%d\n",
89 __func__, ret);
90 continue;
91 }
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090092
93 ret = pinctrl_config_one(config);
Michael Trimarchi36a90ed2019-09-17 22:06:03 +020094 if (ret) {
95 dev_warn(dev, "%s: pinctrl_config_one: err=%d\n",
96 __func__, ret);
97 continue;
98 }
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090099 }
100
101 return 0;
102}
103
104/**
Masahiro Yamadaf8357062016-08-19 18:26:54 +0900105 * pinconfig_post_bind() - post binding for PINCONFIG uclass
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900106 * Recursively bind its children as pinconfig devices.
107 *
108 * @dev: pinconfig device
109 * @return: 0 on success, or negative error code on failure
110 */
111static int pinconfig_post_bind(struct udevice *dev)
112{
Simon Glass5589a812015-12-29 05:22:52 -0700113 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900114 const char *name;
Simon Glass45a26862017-05-18 20:09:07 -0600115 ofnode node;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900116 int ret;
117
Simon Glass7d14ee42020-12-19 10:40:13 -0700118 if (!dev_has_ofnode(dev))
Urja Rannikko63402832019-03-22 19:14:33 +0000119 return 0;
120
Simon Glass45a26862017-05-18 20:09:07 -0600121 dev_for_each_subnode(node, dev) {
Lukasz Majewskib6a62382019-01-09 23:05:02 +0100122 if (pre_reloc_only &&
123 !ofnode_pre_reloc(node))
Simon Glass5589a812015-12-29 05:22:52 -0700124 continue;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900125 /*
126 * If this node has "compatible" property, this is not
127 * a pin configuration node, but a normal device. skip.
128 */
Masahiro Yamada61e51ba2017-06-22 16:54:05 +0900129 ofnode_get_property(node, "compatible", &ret);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900130 if (ret >= 0)
131 continue;
Patrick Delaunayd39e2bd2019-02-25 13:39:56 +0100132 /* If this node has "gpio-controller" property, skip */
133 if (ofnode_read_bool(node, "gpio-controller"))
134 continue;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900135
136 if (ret != -FDT_ERR_NOTFOUND)
137 return ret;
138
Simon Glass45a26862017-05-18 20:09:07 -0600139 name = ofnode_get_name(node);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900140 if (!name)
141 return -EINVAL;
142 ret = device_bind_driver_to_node(dev, "pinconfig", name,
Simon Glass45a26862017-05-18 20:09:07 -0600143 node, NULL);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900144 if (ret)
145 return ret;
146 }
147
148 return 0;
149}
150
151UCLASS_DRIVER(pinconfig) = {
152 .id = UCLASS_PINCONFIG,
Patrick Delaunay44510da2019-10-21 15:02:40 +0200153#if CONFIG_IS_ENABLED(PINCONF_RECURSIVE)
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900154 .post_bind = pinconfig_post_bind,
Patrick Delaunayc20851b2019-08-02 14:48:00 +0200155#endif
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900156 .name = "pinconfig",
157};
158
159U_BOOT_DRIVER(pinconfig_generic) = {
160 .name = "pinconfig",
161 .id = UCLASS_PINCONFIG,
162};
163
164#else
165static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
166{
167 return -ENODEV;
168}
169
170static int pinconfig_post_bind(struct udevice *dev)
171{
Masahiro Yamada8a5f6122015-09-06 01:44:50 +0900172 return 0;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900173}
174#endif
175
Marek Vasutae59d7c2019-04-21 23:57:23 +0200176static int
177pinctrl_gpio_get_pinctrl_and_offset(struct udevice *dev, unsigned offset,
178 struct udevice **pctldev,
179 unsigned int *pin_selector)
180{
181 struct ofnode_phandle_args args;
182 unsigned gpio_offset, pfc_base, pfc_pins;
183 int ret;
184
185 ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
186 0, &args);
187 if (ret) {
188 dev_dbg(dev, "%s: dev_read_phandle_with_args: err=%d\n",
189 __func__, ret);
190 return ret;
191 }
192
193 ret = uclass_get_device_by_ofnode(UCLASS_PINCTRL,
194 args.node, pctldev);
195 if (ret) {
196 dev_dbg(dev,
197 "%s: uclass_get_device_by_of_offset failed: err=%d\n",
198 __func__, ret);
199 return ret;
200 }
201
202 gpio_offset = args.args[0];
203 pfc_base = args.args[1];
204 pfc_pins = args.args[2];
205
206 if (offset < gpio_offset || offset > gpio_offset + pfc_pins) {
207 dev_dbg(dev,
208 "%s: GPIO can not be mapped to pincontrol pin\n",
209 __func__);
210 return -EINVAL;
211 }
212
213 offset -= gpio_offset;
214 offset += pfc_base;
215 *pin_selector = offset;
216
217 return 0;
218}
219
220/**
221 * pinctrl_gpio_request() - request a single pin to be used as GPIO
222 *
223 * @dev: GPIO peripheral device
224 * @offset: the GPIO pin offset from the GPIO controller
225 * @return: 0 on success, or negative error code on failure
226 */
227int pinctrl_gpio_request(struct udevice *dev, unsigned offset)
228{
229 const struct pinctrl_ops *ops;
230 struct udevice *pctldev;
231 unsigned int pin_selector;
232 int ret;
233
234 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
235 &pctldev, &pin_selector);
236 if (ret)
237 return ret;
238
239 ops = pinctrl_get_ops(pctldev);
Simon Glassbddac452021-03-25 10:26:11 +1300240 assert(ops);
241 if (!ops->gpio_request_enable)
242 return -ENOSYS;
Marek Vasutae59d7c2019-04-21 23:57:23 +0200243
244 return ops->gpio_request_enable(pctldev, pin_selector);
245}
246
247/**
248 * pinctrl_gpio_free() - free a single pin used as GPIO
249 *
250 * @dev: GPIO peripheral device
251 * @offset: the GPIO pin offset from the GPIO controller
252 * @return: 0 on success, or negative error code on failure
253 */
254int pinctrl_gpio_free(struct udevice *dev, unsigned offset)
255{
256 const struct pinctrl_ops *ops;
257 struct udevice *pctldev;
258 unsigned int pin_selector;
259 int ret;
260
261 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
262 &pctldev, &pin_selector);
263 if (ret)
264 return ret;
265
266 ops = pinctrl_get_ops(pctldev);
Simon Glassbddac452021-03-25 10:26:11 +1300267 assert(ops);
268 if (!ops->gpio_disable_free)
269 return -ENOSYS;
Marek Vasutae59d7c2019-04-21 23:57:23 +0200270
271 return ops->gpio_disable_free(pctldev, pin_selector);
272}
273
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900274/**
275 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
276 *
277 * @dev: peripheral device
278 * @return: 0 on success, or negative error code on failure
279 */
280static int pinctrl_select_state_simple(struct udevice *dev)
281{
282 struct udevice *pctldev;
283 struct pinctrl_ops *ops;
284 int ret;
285
286 /*
Patrice Chotarddce406e2019-02-25 13:39:55 +0100287 * For most system, there is only one pincontroller device. But in
288 * case of multiple pincontroller devices, probe the one with sequence
289 * number 0 (defined by alias) to avoid race condition.
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900290 */
Patrice Chotarddce406e2019-02-25 13:39:55 +0100291 ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev);
292 if (ret)
293 /* if not found, get the first one */
294 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900295 if (ret)
296 return ret;
297
298 ops = pinctrl_get_ops(pctldev);
299 if (!ops->set_state_simple) {
300 dev_dbg(dev, "set_state_simple op missing\n");
301 return -ENOSYS;
302 }
303
304 return ops->set_state_simple(pctldev, dev);
305}
306
307int pinctrl_select_state(struct udevice *dev, const char *statename)
308{
309 /*
Kever Yangf717b4c2018-04-18 17:54:04 +0800310 * Some device which is logical like mmc.blk, do not have
311 * a valid ofnode.
312 */
Simon Glassc23405f2020-12-19 10:40:12 -0700313 if (!dev_has_ofnode(dev))
Kever Yangf717b4c2018-04-18 17:54:04 +0800314 return 0;
315 /*
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900316 * Try full-implemented pinctrl first.
317 * If it fails or is not implemented, try simple one.
318 */
319 if (pinctrl_select_state_full(dev, statename))
320 return pinctrl_select_state_simple(dev);
321
322 return 0;
323}
324
Simon Glassc5acf4a2015-08-30 16:55:13 -0600325int pinctrl_request(struct udevice *dev, int func, int flags)
326{
327 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
328
329 if (!ops->request)
330 return -ENOSYS;
331
332 return ops->request(dev, func, flags);
333}
334
335int pinctrl_request_noflags(struct udevice *dev, int func)
336{
337 return pinctrl_request(dev, func, 0);
338}
339
340int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
341{
342 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
343
344 if (!ops->get_periph_id)
345 return -ENOSYS;
346
347 return ops->get_periph_id(dev, periph);
348}
349
Simon Glass77eaa192016-01-21 19:43:56 -0700350int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
351{
352 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
353
354 if (!ops->get_gpio_mux)
355 return -ENOSYS;
356
357 return ops->get_gpio_mux(dev, banknum, index);
358}
359
Patrice Chotard8bbb5b22018-10-24 14:10:14 +0200360int pinctrl_get_pins_count(struct udevice *dev)
361{
362 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
363
364 if (!ops->get_pins_count)
365 return -ENOSYS;
366
367 return ops->get_pins_count(dev);
368}
369
370int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
371 int size)
372{
373 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
374
375 if (!ops->get_pin_name)
376 return -ENOSYS;
377
378 snprintf(buf, size, ops->get_pin_name(dev, selector));
379
380 return 0;
381}
382
Patrice Chotardf55a0c02018-10-24 14:10:13 +0200383int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf,
384 int size)
385{
386 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
387
388 if (!ops->get_pin_muxing)
389 return -ENOSYS;
390
391 return ops->get_pin_muxing(dev, selector, buf, size);
392}
393
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900394/**
Masahiro Yamadaf8357062016-08-19 18:26:54 +0900395 * pinconfig_post_bind() - post binding for PINCTRL uclass
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900396 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
397 *
398 * @dev: pinctrl device
399 * @return: 0 on success, or negative error code on failure
400 */
Patrick Delaunaye878b532019-08-02 14:48:00 +0200401static int __maybe_unused pinctrl_post_bind(struct udevice *dev)
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900402{
403 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
404
405 if (!ops) {
406 dev_dbg(dev, "ops is not set. Do not bind.\n");
407 return -EINVAL;
408 }
409
410 /*
Masahiro Yamada8a5f6122015-09-06 01:44:50 +0900411 * If set_state callback is set, we assume this pinctrl driver is the
412 * full implementation. In this case, its child nodes should be bound
413 * so that peripheral devices can easily search in parent devices
414 * during later DT-parsing.
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900415 */
Masahiro Yamada8a5f6122015-09-06 01:44:50 +0900416 if (ops->set_state)
417 return pinconfig_post_bind(dev);
418
419 return 0;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900420}
421
422UCLASS_DRIVER(pinctrl) = {
423 .id = UCLASS_PINCTRL,
Simon Glassc8fbf302020-12-23 08:11:13 -0700424#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900425 .post_bind = pinctrl_post_bind,
Simon Glassc8fbf302020-12-23 08:11:13 -0700426#endif
Thomas Abrahamac985272016-04-23 22:18:07 +0530427 .flags = DM_UC_FLAG_SEQ_ALIAS,
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900428 .name = "pinctrl",
429};