blob: fe2ba5021a786cc901e21b2454e3a6612243da48 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamadaf8efa632015-08-27 12:44:29 +09002/*
3 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamadaf8efa632015-08-27 12:44:29 +09004 */
5
Patrick Delaunay81313352021-04-27 11:02:19 +02006#define LOG_CATEGORY UCLASS_PINCTRL
7
Masahiro Yamadaf8efa632015-08-27 12:44:29 +09008#include <common.h>
Simon Glass9bc15642020-02-03 07:36:16 -07009#include <malloc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060010#include <asm/global_data.h>
Simon Glass9bc15642020-02-03 07:36:16 -070011#include <dm/device_compat.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090012#include <linux/libfdt.h>
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090013#include <linux/err.h>
14#include <linux/list.h>
Simon Glass11c89f32017-05-17 17:18:03 -060015#include <dm.h>
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090016#include <dm/lists.h>
17#include <dm/pinctrl.h>
Heiko Stübner9a2cdca2017-02-18 19:46:21 +010018#include <dm/util.h>
Kever Yanga5897622018-02-09 10:56:24 +080019#include <dm/of_access.h>
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090020
21DECLARE_GLOBAL_DATA_PTR;
22
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090023/**
24 * pinctrl_config_one() - apply pinctrl settings for a single node
25 *
26 * @config: pin configuration node
27 * @return: 0 on success, or negative error code on failure
28 */
29static int pinctrl_config_one(struct udevice *config)
30{
31 struct udevice *pctldev;
32 const struct pinctrl_ops *ops;
33
34 pctldev = config;
35 for (;;) {
36 pctldev = dev_get_parent(pctldev);
37 if (!pctldev) {
38 dev_err(config, "could not find pctldev\n");
39 return -EINVAL;
40 }
41 if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
42 break;
43 }
44
45 ops = pinctrl_get_ops(pctldev);
46 return ops->set_state(pctldev, config);
47}
48
49/**
50 * pinctrl_select_state_full() - full implementation of pinctrl_select_state
51 *
52 * @dev: peripheral device
53 * @statename: state name, like "default"
54 * @return: 0 on success, or negative error code on failure
55 */
56static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
57{
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090058 char propname[32]; /* long enough */
59 const fdt32_t *list;
60 uint32_t phandle;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090061 struct udevice *config;
62 int state, size, i, ret;
63
Kever Yanga5897622018-02-09 10:56:24 +080064 state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090065 if (state < 0) {
66 char *end;
67 /*
68 * If statename is not found in "pinctrl-names",
69 * assume statename is just the integer state ID.
70 */
Simon Glassff9b9032021-07-24 09:03:30 -060071 state = dectoul(statename, &end);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090072 if (*end)
Michael Walleac09e6b2023-01-18 13:12:22 +010073 return -ENOSYS;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090074 }
75
76 snprintf(propname, sizeof(propname), "pinctrl-%d", state);
Kever Yanga5897622018-02-09 10:56:24 +080077 list = dev_read_prop(dev, propname, &size);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090078 if (!list)
Michael Walleac09e6b2023-01-18 13:12:22 +010079 return -ENOSYS;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090080
81 size /= sizeof(*list);
82 for (i = 0; i < size; i++) {
83 phandle = fdt32_to_cpu(*list++);
Kever Yanga5897622018-02-09 10:56:24 +080084 ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
85 &config);
Michael Trimarchi990a4c52019-09-17 22:06:03 +020086 if (ret) {
87 dev_warn(dev, "%s: uclass_get_device_by_phandle_id: err=%d\n",
88 __func__, ret);
89 continue;
90 }
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090091
92 ret = pinctrl_config_one(config);
Michael Trimarchi990a4c52019-09-17 22:06:03 +020093 if (ret) {
94 dev_warn(dev, "%s: pinctrl_config_one: err=%d\n",
95 __func__, ret);
96 continue;
97 }
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090098 }
99
100 return 0;
101}
102
Jonas Karlman285b7ae2024-02-17 12:08:40 +0000103static bool ofnode_pre_reloc_recursive(ofnode parent)
104{
105 ofnode child;
106
107 if (ofnode_pre_reloc(parent))
108 return true;
109
110 if (CONFIG_IS_ENABLED(PINCONF_RECURSIVE)) {
111 ofnode_for_each_subnode(child, parent)
112 if (ofnode_pre_reloc_recursive(child))
113 return true;
114 }
115
116 return false;
117}
118
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900119/**
Masahiro Yamada0cbfe5c2016-08-19 18:26:54 +0900120 * pinconfig_post_bind() - post binding for PINCONFIG uclass
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900121 * Recursively bind its children as pinconfig devices.
122 *
123 * @dev: pinconfig device
124 * @return: 0 on success, or negative error code on failure
125 */
126static int pinconfig_post_bind(struct udevice *dev)
127{
Simon Glass9cefa472015-12-29 05:22:52 -0700128 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900129 const char *name;
Simon Glasse6dd8da2017-05-18 20:09:07 -0600130 ofnode node;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900131 int ret;
132
Simon Glassf1d50f72020-12-19 10:40:13 -0700133 if (!dev_has_ofnode(dev))
Urja Rannikkocb484fc2019-03-22 19:14:33 +0000134 return 0;
135
Simon Glasse6dd8da2017-05-18 20:09:07 -0600136 dev_for_each_subnode(node, dev) {
Lukasz Majewski6fdf2722019-01-09 23:05:02 +0100137 if (pre_reloc_only &&
Jonas Karlman285b7ae2024-02-17 12:08:40 +0000138 !ofnode_pre_reloc_recursive(node))
Simon Glass9cefa472015-12-29 05:22:52 -0700139 continue;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900140 /*
141 * If this node has "compatible" property, this is not
142 * a pin configuration node, but a normal device. skip.
143 */
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900144 ofnode_get_property(node, "compatible", &ret);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900145 if (ret >= 0)
146 continue;
Patrick Delaunay1408ab32019-02-25 13:39:56 +0100147 /* If this node has "gpio-controller" property, skip */
148 if (ofnode_read_bool(node, "gpio-controller"))
149 continue;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900150
151 if (ret != -FDT_ERR_NOTFOUND)
152 return ret;
153
Simon Glasse6dd8da2017-05-18 20:09:07 -0600154 name = ofnode_get_name(node);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900155 if (!name)
156 return -EINVAL;
157 ret = device_bind_driver_to_node(dev, "pinconfig", name,
Simon Glasse6dd8da2017-05-18 20:09:07 -0600158 node, NULL);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900159 if (ret)
160 return ret;
161 }
162
163 return 0;
164}
165
Michael Walled08a9892023-01-18 13:12:23 +0100166#if CONFIG_IS_ENABLED(PINCTRL_FULL)
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900167UCLASS_DRIVER(pinconfig) = {
168 .id = UCLASS_PINCONFIG,
Patrick Delaunay2c7526a2019-10-21 15:02:40 +0200169#if CONFIG_IS_ENABLED(PINCONF_RECURSIVE)
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900170 .post_bind = pinconfig_post_bind,
Patrick Delaunaybcdb1042019-08-02 14:48:00 +0200171#endif
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900172 .name = "pinconfig",
173};
174
175U_BOOT_DRIVER(pinconfig_generic) = {
176 .name = "pinconfig",
177 .id = UCLASS_PINCONFIG,
178};
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900179#endif
180
Marek Vasut87510962019-04-21 23:57:23 +0200181static int
182pinctrl_gpio_get_pinctrl_and_offset(struct udevice *dev, unsigned offset,
183 struct udevice **pctldev,
184 unsigned int *pin_selector)
185{
186 struct ofnode_phandle_args args;
187 unsigned gpio_offset, pfc_base, pfc_pins;
Quanyang Wangd7efc562023-03-16 14:11:46 +0800188 int ret = 0;
189 int i = 0;
Marek Vasut87510962019-04-21 23:57:23 +0200190
Quanyang Wangd7efc562023-03-16 14:11:46 +0800191 while (ret == 0) {
192 ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
193 i++, &args);
194 if (ret) {
195 dev_dbg(dev, "%s: dev_read_phandle_with_args: err=%d\n",
196 __func__, ret);
197 return ret;
198 }
Marek Vasut87510962019-04-21 23:57:23 +0200199
Quanyang Wangd7efc562023-03-16 14:11:46 +0800200 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 }
Marek Vasut87510962019-04-21 23:57:23 +0200208
Quanyang Wangd7efc562023-03-16 14:11:46 +0800209 gpio_offset = args.args[0];
210 pfc_base = args.args[1];
211 pfc_pins = args.args[2];
Marek Vasut87510962019-04-21 23:57:23 +0200212
Quanyang Wangd7efc562023-03-16 14:11:46 +0800213 if (offset >= gpio_offset && offset <= gpio_offset + pfc_pins)
214 break;
Marek Vasut87510962019-04-21 23:57:23 +0200215 }
216
217 offset -= gpio_offset;
218 offset += pfc_base;
219 *pin_selector = offset;
220
221 return 0;
222}
223
224/**
225 * pinctrl_gpio_request() - request a single pin to be used as GPIO
226 *
227 * @dev: GPIO peripheral device
228 * @offset: the GPIO pin offset from the GPIO controller
Pali Rohár69cee502022-07-25 13:56:11 +0200229 * @label: the GPIO pin label
Marek Vasut87510962019-04-21 23:57:23 +0200230 * @return: 0 on success, or negative error code on failure
231 */
Pali Rohár69cee502022-07-25 13:56:11 +0200232int pinctrl_gpio_request(struct udevice *dev, unsigned offset, const char *label)
Marek Vasut87510962019-04-21 23:57:23 +0200233{
234 const struct pinctrl_ops *ops;
235 struct udevice *pctldev;
236 unsigned int pin_selector;
237 int ret;
238
239 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
240 &pctldev, &pin_selector);
241 if (ret)
242 return ret;
243
244 ops = pinctrl_get_ops(pctldev);
Simon Glass33bf7b82021-03-25 10:26:11 +1300245 assert(ops);
246 if (!ops->gpio_request_enable)
247 return -ENOSYS;
Marek Vasut87510962019-04-21 23:57:23 +0200248
249 return ops->gpio_request_enable(pctldev, pin_selector);
250}
251
252/**
253 * pinctrl_gpio_free() - free a single pin used as GPIO
254 *
255 * @dev: GPIO peripheral device
256 * @offset: the GPIO pin offset from the GPIO controller
257 * @return: 0 on success, or negative error code on failure
258 */
259int pinctrl_gpio_free(struct udevice *dev, unsigned offset)
260{
261 const struct pinctrl_ops *ops;
262 struct udevice *pctldev;
263 unsigned int pin_selector;
264 int ret;
265
266 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
267 &pctldev, &pin_selector);
268 if (ret)
269 return ret;
270
271 ops = pinctrl_get_ops(pctldev);
Simon Glass33bf7b82021-03-25 10:26:11 +1300272 assert(ops);
273 if (!ops->gpio_disable_free)
274 return -ENOSYS;
Marek Vasut87510962019-04-21 23:57:23 +0200275
276 return ops->gpio_disable_free(pctldev, pin_selector);
277}
278
Masahiro Yamadaf8efa632015-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 Chotard74110632019-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 Yamadaf8efa632015-08-27 12:44:29 +0900295 */
Patrice Chotard74110632019-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 Yamadaf8efa632015-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 Yang16ed9712018-04-18 17:54:04 +0800315 * Some device which is logical like mmc.blk, do not have
316 * a valid ofnode.
317 */
Simon Glass07c17772020-12-19 10:40:12 -0700318 if (!dev_has_ofnode(dev))
Kever Yang16ed9712018-04-18 17:54:04 +0800319 return 0;
320 /*
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900321 * Try full-implemented pinctrl first.
322 * If it fails or is not implemented, try simple one.
323 */
Michael Walleac09e6b2023-01-18 13:12:22 +0100324 if (CONFIG_IS_ENABLED(PINCTRL_FULL))
325 return pinctrl_select_state_full(dev, statename);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900326
Michael Walleac09e6b2023-01-18 13:12:22 +0100327 return pinctrl_select_state_simple(dev);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900328}
329
Simon Glassac2ee652015-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 Glass2d4fa3c2016-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 Chotardfbf80822018-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 Chotard74ba3f22018-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 Yamadaf8efa632015-08-27 12:44:29 +0900399/**
Michael Walle6f772ea2023-01-18 13:12:24 +0100400 * pinctrl_post_bind() - post binding for PINCTRL uclass
Masahiro Yamadaf8efa632015-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 Delaunayd23bcf12019-08-02 14:48:00 +0200406static int __maybe_unused pinctrl_post_bind(struct udevice *dev)
Masahiro Yamadaf8efa632015-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 /*
Michael Walled08a9892023-01-18 13:12:23 +0100416 * If the pinctrl driver has the full implementation, its child nodes
417 * should be bound so that peripheral devices can easily search in
418 * parent devices during later DT-parsing.
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900419 */
Michael Walled08a9892023-01-18 13:12:23 +0100420 if (CONFIG_IS_ENABLED(PINCTRL_FULL))
Masahiro Yamada7778b032015-09-06 01:44:50 +0900421 return pinconfig_post_bind(dev);
422
423 return 0;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900424}
425
426UCLASS_DRIVER(pinctrl) = {
427 .id = UCLASS_PINCTRL,
Simon Glass92882652021-08-07 07:24:04 -0600428#if CONFIG_IS_ENABLED(OF_REAL)
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900429 .post_bind = pinctrl_post_bind,
Simon Glassdefcc632020-12-23 08:11:13 -0700430#endif
Thomas Abraham87adc642016-04-23 22:18:07 +0530431 .flags = DM_UC_FLAG_SEQ_ALIAS,
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900432 .name = "pinctrl",
433};