blob: 8837726cc1650950dc8a82f9b9d50a248d5dfd2c [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
103/**
Masahiro Yamada0cbfe5c2016-08-19 18:26:54 +0900104 * pinconfig_post_bind() - post binding for PINCONFIG uclass
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900105 * Recursively bind its children as pinconfig devices.
106 *
107 * @dev: pinconfig device
108 * @return: 0 on success, or negative error code on failure
109 */
110static int pinconfig_post_bind(struct udevice *dev)
111{
Simon Glass9cefa472015-12-29 05:22:52 -0700112 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900113 const char *name;
Simon Glasse6dd8da2017-05-18 20:09:07 -0600114 ofnode node;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900115 int ret;
116
Simon Glassf1d50f72020-12-19 10:40:13 -0700117 if (!dev_has_ofnode(dev))
Urja Rannikkocb484fc2019-03-22 19:14:33 +0000118 return 0;
119
Simon Glasse6dd8da2017-05-18 20:09:07 -0600120 dev_for_each_subnode(node, dev) {
Lukasz Majewski6fdf2722019-01-09 23:05:02 +0100121 if (pre_reloc_only &&
122 !ofnode_pre_reloc(node))
Simon Glass9cefa472015-12-29 05:22:52 -0700123 continue;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900124 /*
125 * If this node has "compatible" property, this is not
126 * a pin configuration node, but a normal device. skip.
127 */
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900128 ofnode_get_property(node, "compatible", &ret);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900129 if (ret >= 0)
130 continue;
Patrick Delaunay1408ab32019-02-25 13:39:56 +0100131 /* If this node has "gpio-controller" property, skip */
132 if (ofnode_read_bool(node, "gpio-controller"))
133 continue;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900134
135 if (ret != -FDT_ERR_NOTFOUND)
136 return ret;
137
Simon Glasse6dd8da2017-05-18 20:09:07 -0600138 name = ofnode_get_name(node);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900139 if (!name)
140 return -EINVAL;
141 ret = device_bind_driver_to_node(dev, "pinconfig", name,
Simon Glasse6dd8da2017-05-18 20:09:07 -0600142 node, NULL);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900143 if (ret)
144 return ret;
145 }
146
147 return 0;
148}
149
Michael Walled08a9892023-01-18 13:12:23 +0100150#if CONFIG_IS_ENABLED(PINCTRL_FULL)
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900151UCLASS_DRIVER(pinconfig) = {
152 .id = UCLASS_PINCONFIG,
Patrick Delaunay2c7526a2019-10-21 15:02:40 +0200153#if CONFIG_IS_ENABLED(PINCONF_RECURSIVE)
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900154 .post_bind = pinconfig_post_bind,
Patrick Delaunaybcdb1042019-08-02 14:48:00 +0200155#endif
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900156 .name = "pinconfig",
157};
158
159U_BOOT_DRIVER(pinconfig_generic) = {
160 .name = "pinconfig",
161 .id = UCLASS_PINCONFIG,
162};
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900163#endif
164
Marek Vasut87510962019-04-21 23:57:23 +0200165static int
166pinctrl_gpio_get_pinctrl_and_offset(struct udevice *dev, unsigned offset,
167 struct udevice **pctldev,
168 unsigned int *pin_selector)
169{
170 struct ofnode_phandle_args args;
171 unsigned gpio_offset, pfc_base, pfc_pins;
172 int ret;
173
174 ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
175 0, &args);
176 if (ret) {
177 dev_dbg(dev, "%s: dev_read_phandle_with_args: err=%d\n",
178 __func__, ret);
179 return ret;
180 }
181
182 ret = uclass_get_device_by_ofnode(UCLASS_PINCTRL,
183 args.node, pctldev);
184 if (ret) {
185 dev_dbg(dev,
186 "%s: uclass_get_device_by_of_offset failed: err=%d\n",
187 __func__, ret);
188 return ret;
189 }
190
191 gpio_offset = args.args[0];
192 pfc_base = args.args[1];
193 pfc_pins = args.args[2];
194
195 if (offset < gpio_offset || offset > gpio_offset + pfc_pins) {
196 dev_dbg(dev,
197 "%s: GPIO can not be mapped to pincontrol pin\n",
198 __func__);
199 return -EINVAL;
200 }
201
202 offset -= gpio_offset;
203 offset += pfc_base;
204 *pin_selector = offset;
205
206 return 0;
207}
208
209/**
210 * pinctrl_gpio_request() - request a single pin to be used as GPIO
211 *
212 * @dev: GPIO peripheral device
213 * @offset: the GPIO pin offset from the GPIO controller
Pali Rohár69cee502022-07-25 13:56:11 +0200214 * @label: the GPIO pin label
Marek Vasut87510962019-04-21 23:57:23 +0200215 * @return: 0 on success, or negative error code on failure
216 */
Pali Rohár69cee502022-07-25 13:56:11 +0200217int pinctrl_gpio_request(struct udevice *dev, unsigned offset, const char *label)
Marek Vasut87510962019-04-21 23:57:23 +0200218{
219 const struct pinctrl_ops *ops;
220 struct udevice *pctldev;
221 unsigned int pin_selector;
222 int ret;
223
224 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
225 &pctldev, &pin_selector);
226 if (ret)
227 return ret;
228
229 ops = pinctrl_get_ops(pctldev);
Simon Glass33bf7b82021-03-25 10:26:11 +1300230 assert(ops);
231 if (!ops->gpio_request_enable)
232 return -ENOSYS;
Marek Vasut87510962019-04-21 23:57:23 +0200233
234 return ops->gpio_request_enable(pctldev, pin_selector);
235}
236
237/**
238 * pinctrl_gpio_free() - free a single pin used as GPIO
239 *
240 * @dev: GPIO peripheral device
241 * @offset: the GPIO pin offset from the GPIO controller
242 * @return: 0 on success, or negative error code on failure
243 */
244int pinctrl_gpio_free(struct udevice *dev, unsigned offset)
245{
246 const struct pinctrl_ops *ops;
247 struct udevice *pctldev;
248 unsigned int pin_selector;
249 int ret;
250
251 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
252 &pctldev, &pin_selector);
253 if (ret)
254 return ret;
255
256 ops = pinctrl_get_ops(pctldev);
Simon Glass33bf7b82021-03-25 10:26:11 +1300257 assert(ops);
258 if (!ops->gpio_disable_free)
259 return -ENOSYS;
Marek Vasut87510962019-04-21 23:57:23 +0200260
261 return ops->gpio_disable_free(pctldev, pin_selector);
262}
263
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900264/**
265 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
266 *
267 * @dev: peripheral device
268 * @return: 0 on success, or negative error code on failure
269 */
270static int pinctrl_select_state_simple(struct udevice *dev)
271{
272 struct udevice *pctldev;
273 struct pinctrl_ops *ops;
274 int ret;
275
276 /*
Patrice Chotard74110632019-02-25 13:39:55 +0100277 * For most system, there is only one pincontroller device. But in
278 * case of multiple pincontroller devices, probe the one with sequence
279 * number 0 (defined by alias) to avoid race condition.
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900280 */
Patrice Chotard74110632019-02-25 13:39:55 +0100281 ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev);
282 if (ret)
283 /* if not found, get the first one */
284 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900285 if (ret)
286 return ret;
287
288 ops = pinctrl_get_ops(pctldev);
289 if (!ops->set_state_simple) {
290 dev_dbg(dev, "set_state_simple op missing\n");
291 return -ENOSYS;
292 }
293
294 return ops->set_state_simple(pctldev, dev);
295}
296
297int pinctrl_select_state(struct udevice *dev, const char *statename)
298{
299 /*
Kever Yang16ed9712018-04-18 17:54:04 +0800300 * Some device which is logical like mmc.blk, do not have
301 * a valid ofnode.
302 */
Simon Glass07c17772020-12-19 10:40:12 -0700303 if (!dev_has_ofnode(dev))
Kever Yang16ed9712018-04-18 17:54:04 +0800304 return 0;
305 /*
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900306 * Try full-implemented pinctrl first.
307 * If it fails or is not implemented, try simple one.
308 */
Michael Walleac09e6b2023-01-18 13:12:22 +0100309 if (CONFIG_IS_ENABLED(PINCTRL_FULL))
310 return pinctrl_select_state_full(dev, statename);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900311
Michael Walleac09e6b2023-01-18 13:12:22 +0100312 return pinctrl_select_state_simple(dev);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900313}
314
Simon Glassac2ee652015-08-30 16:55:13 -0600315int pinctrl_request(struct udevice *dev, int func, int flags)
316{
317 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
318
319 if (!ops->request)
320 return -ENOSYS;
321
322 return ops->request(dev, func, flags);
323}
324
325int pinctrl_request_noflags(struct udevice *dev, int func)
326{
327 return pinctrl_request(dev, func, 0);
328}
329
330int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
331{
332 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
333
334 if (!ops->get_periph_id)
335 return -ENOSYS;
336
337 return ops->get_periph_id(dev, periph);
338}
339
Simon Glass2d4fa3c2016-01-21 19:43:56 -0700340int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
341{
342 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
343
344 if (!ops->get_gpio_mux)
345 return -ENOSYS;
346
347 return ops->get_gpio_mux(dev, banknum, index);
348}
349
Patrice Chotardfbf80822018-10-24 14:10:14 +0200350int pinctrl_get_pins_count(struct udevice *dev)
351{
352 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
353
354 if (!ops->get_pins_count)
355 return -ENOSYS;
356
357 return ops->get_pins_count(dev);
358}
359
360int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
361 int size)
362{
363 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
364
365 if (!ops->get_pin_name)
366 return -ENOSYS;
367
368 snprintf(buf, size, ops->get_pin_name(dev, selector));
369
370 return 0;
371}
372
Patrice Chotard74ba3f22018-10-24 14:10:13 +0200373int pinctrl_get_pin_muxing(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_muxing)
379 return -ENOSYS;
380
381 return ops->get_pin_muxing(dev, selector, buf, size);
382}
383
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900384/**
Michael Walle6f772ea2023-01-18 13:12:24 +0100385 * pinctrl_post_bind() - post binding for PINCTRL uclass
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900386 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
387 *
388 * @dev: pinctrl device
389 * @return: 0 on success, or negative error code on failure
390 */
Patrick Delaunayd23bcf12019-08-02 14:48:00 +0200391static int __maybe_unused pinctrl_post_bind(struct udevice *dev)
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900392{
393 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
394
395 if (!ops) {
396 dev_dbg(dev, "ops is not set. Do not bind.\n");
397 return -EINVAL;
398 }
399
400 /*
Michael Walled08a9892023-01-18 13:12:23 +0100401 * If the pinctrl driver has the full implementation, its child nodes
402 * should be bound so that peripheral devices can easily search in
403 * parent devices during later DT-parsing.
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900404 */
Michael Walled08a9892023-01-18 13:12:23 +0100405 if (CONFIG_IS_ENABLED(PINCTRL_FULL))
Masahiro Yamada7778b032015-09-06 01:44:50 +0900406 return pinconfig_post_bind(dev);
407
408 return 0;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900409}
410
411UCLASS_DRIVER(pinctrl) = {
412 .id = UCLASS_PINCTRL,
Simon Glass92882652021-08-07 07:24:04 -0600413#if CONFIG_IS_ENABLED(OF_REAL)
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900414 .post_bind = pinctrl_post_bind,
Simon Glassdefcc632020-12-23 08:11:13 -0700415#endif
Thomas Abraham87adc642016-04-23 22:18:07 +0530416 .flags = DM_UC_FLAG_SEQ_ALIAS,
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900417 .name = "pinctrl",
418};