blob: d9c76898a9692d40b7cd4eecf7cb2edf9e93a209 [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
Simon Glass9bc15642020-02-03 07:36:16 -07008#include <malloc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06009#include <asm/global_data.h>
Simon Glass9bc15642020-02-03 07:36:16 -070010#include <dm/device_compat.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090011#include <linux/libfdt.h>
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090012#include <linux/err.h>
13#include <linux/list.h>
Simon Glass11c89f32017-05-17 17:18:03 -060014#include <dm.h>
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090015#include <dm/lists.h>
16#include <dm/pinctrl.h>
Heiko Stübner9a2cdca2017-02-18 19:46:21 +010017#include <dm/util.h>
Kever Yanga5897622018-02-09 10:56:24 +080018#include <dm/of_access.h>
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090019
20DECLARE_GLOBAL_DATA_PTR;
21
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090022/**
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 Yamadaf8efa632015-08-27 12:44:29 +090057 char propname[32]; /* long enough */
58 const fdt32_t *list;
59 uint32_t phandle;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090060 struct udevice *config;
61 int state, size, i, ret;
62
Kever Yanga5897622018-02-09 10:56:24 +080063 state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
Masahiro Yamadaf8efa632015-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 */
Simon Glassff9b9032021-07-24 09:03:30 -060070 state = dectoul(statename, &end);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090071 if (*end)
Michael Walleac09e6b2023-01-18 13:12:22 +010072 return -ENOSYS;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090073 }
74
75 snprintf(propname, sizeof(propname), "pinctrl-%d", state);
Kever Yanga5897622018-02-09 10:56:24 +080076 list = dev_read_prop(dev, propname, &size);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090077 if (!list)
Michael Walleac09e6b2023-01-18 13:12:22 +010078 return -ENOSYS;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090079
80 size /= sizeof(*list);
81 for (i = 0; i < size; i++) {
82 phandle = fdt32_to_cpu(*list++);
Kever Yanga5897622018-02-09 10:56:24 +080083 ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
84 &config);
Michael Trimarchi990a4c52019-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 Yamadaf8efa632015-08-27 12:44:29 +090090
91 ret = pinctrl_config_one(config);
Michael Trimarchi990a4c52019-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 Yamadaf8efa632015-08-27 12:44:29 +090097 }
98
99 return 0;
100}
101
Jonas Karlman285b7ae2024-02-17 12:08:40 +0000102static bool ofnode_pre_reloc_recursive(ofnode parent)
103{
104 ofnode child;
105
106 if (ofnode_pre_reloc(parent))
107 return true;
108
109 if (CONFIG_IS_ENABLED(PINCONF_RECURSIVE)) {
110 ofnode_for_each_subnode(child, parent)
111 if (ofnode_pre_reloc_recursive(child))
112 return true;
113 }
114
115 return false;
116}
117
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900118/**
Masahiro Yamada0cbfe5c2016-08-19 18:26:54 +0900119 * pinconfig_post_bind() - post binding for PINCONFIG uclass
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900120 * Recursively bind its children as pinconfig devices.
121 *
122 * @dev: pinconfig device
123 * @return: 0 on success, or negative error code on failure
124 */
125static int pinconfig_post_bind(struct udevice *dev)
126{
Simon Glass9cefa472015-12-29 05:22:52 -0700127 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900128 const char *name;
Simon Glasse6dd8da2017-05-18 20:09:07 -0600129 ofnode node;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900130 int ret;
131
Simon Glassf1d50f72020-12-19 10:40:13 -0700132 if (!dev_has_ofnode(dev))
Urja Rannikkocb484fc2019-03-22 19:14:33 +0000133 return 0;
134
Simon Glasse6dd8da2017-05-18 20:09:07 -0600135 dev_for_each_subnode(node, dev) {
Lukasz Majewski6fdf2722019-01-09 23:05:02 +0100136 if (pre_reloc_only &&
Jonas Karlman285b7ae2024-02-17 12:08:40 +0000137 !ofnode_pre_reloc_recursive(node))
Simon Glass9cefa472015-12-29 05:22:52 -0700138 continue;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900139 /*
140 * If this node has "compatible" property, this is not
141 * a pin configuration node, but a normal device. skip.
142 */
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900143 ofnode_get_property(node, "compatible", &ret);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900144 if (ret >= 0)
145 continue;
Patrick Delaunay1408ab32019-02-25 13:39:56 +0100146 /* If this node has "gpio-controller" property, skip */
147 if (ofnode_read_bool(node, "gpio-controller"))
148 continue;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900149
150 if (ret != -FDT_ERR_NOTFOUND)
151 return ret;
152
Simon Glasse6dd8da2017-05-18 20:09:07 -0600153 name = ofnode_get_name(node);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900154 if (!name)
155 return -EINVAL;
156 ret = device_bind_driver_to_node(dev, "pinconfig", name,
Simon Glasse6dd8da2017-05-18 20:09:07 -0600157 node, NULL);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900158 if (ret)
159 return ret;
160 }
161
162 return 0;
163}
164
Michael Walled08a9892023-01-18 13:12:23 +0100165#if CONFIG_IS_ENABLED(PINCTRL_FULL)
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900166UCLASS_DRIVER(pinconfig) = {
167 .id = UCLASS_PINCONFIG,
Patrick Delaunay2c7526a2019-10-21 15:02:40 +0200168#if CONFIG_IS_ENABLED(PINCONF_RECURSIVE)
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900169 .post_bind = pinconfig_post_bind,
Patrick Delaunaybcdb1042019-08-02 14:48:00 +0200170#endif
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900171 .name = "pinconfig",
172};
173
174U_BOOT_DRIVER(pinconfig_generic) = {
175 .name = "pinconfig",
176 .id = UCLASS_PINCONFIG,
177};
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900178#endif
179
Marek Vasut87510962019-04-21 23:57:23 +0200180static int
181pinctrl_gpio_get_pinctrl_and_offset(struct udevice *dev, unsigned offset,
182 struct udevice **pctldev,
183 unsigned int *pin_selector)
184{
185 struct ofnode_phandle_args args;
186 unsigned gpio_offset, pfc_base, pfc_pins;
Quanyang Wangd7efc562023-03-16 14:11:46 +0800187 int ret = 0;
188 int i = 0;
Marek Vasut87510962019-04-21 23:57:23 +0200189
Quanyang Wangd7efc562023-03-16 14:11:46 +0800190 while (ret == 0) {
191 ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
192 i++, &args);
193 if (ret) {
194 dev_dbg(dev, "%s: dev_read_phandle_with_args: err=%d\n",
195 __func__, ret);
196 return ret;
197 }
Marek Vasut87510962019-04-21 23:57:23 +0200198
Quanyang Wangd7efc562023-03-16 14:11:46 +0800199 ret = uclass_get_device_by_ofnode(UCLASS_PINCTRL,
200 args.node, pctldev);
201 if (ret) {
202 dev_dbg(dev,
203 "%s: uclass_get_device_by_of_offset failed: err=%d\n",
204 __func__, ret);
205 return ret;
206 }
Marek Vasut87510962019-04-21 23:57:23 +0200207
Quanyang Wangd7efc562023-03-16 14:11:46 +0800208 gpio_offset = args.args[0];
209 pfc_base = args.args[1];
210 pfc_pins = args.args[2];
Marek Vasut87510962019-04-21 23:57:23 +0200211
Jonas Karlman523a31c2024-05-10 19:35:51 +0000212 if (offset >= gpio_offset && offset < gpio_offset + pfc_pins)
Quanyang Wangd7efc562023-03-16 14:11:46 +0800213 break;
Marek Vasut87510962019-04-21 23:57:23 +0200214 }
215
216 offset -= gpio_offset;
217 offset += pfc_base;
218 *pin_selector = offset;
219
220 return 0;
221}
222
223/**
224 * pinctrl_gpio_request() - request a single pin to be used as GPIO
225 *
226 * @dev: GPIO peripheral device
227 * @offset: the GPIO pin offset from the GPIO controller
Pali Rohár69cee502022-07-25 13:56:11 +0200228 * @label: the GPIO pin label
Marek Vasut87510962019-04-21 23:57:23 +0200229 * @return: 0 on success, or negative error code on failure
230 */
Pali Rohár69cee502022-07-25 13:56:11 +0200231int pinctrl_gpio_request(struct udevice *dev, unsigned offset, const char *label)
Marek Vasut87510962019-04-21 23:57:23 +0200232{
233 const struct pinctrl_ops *ops;
234 struct udevice *pctldev;
235 unsigned int pin_selector;
236 int ret;
237
238 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
239 &pctldev, &pin_selector);
240 if (ret)
241 return ret;
242
243 ops = pinctrl_get_ops(pctldev);
Simon Glass33bf7b82021-03-25 10:26:11 +1300244 assert(ops);
245 if (!ops->gpio_request_enable)
246 return -ENOSYS;
Marek Vasut87510962019-04-21 23:57:23 +0200247
248 return ops->gpio_request_enable(pctldev, pin_selector);
249}
250
251/**
252 * pinctrl_gpio_free() - free a single pin used as GPIO
253 *
254 * @dev: GPIO peripheral device
255 * @offset: the GPIO pin offset from the GPIO controller
256 * @return: 0 on success, or negative error code on failure
257 */
258int pinctrl_gpio_free(struct udevice *dev, unsigned offset)
259{
260 const struct pinctrl_ops *ops;
261 struct udevice *pctldev;
262 unsigned int pin_selector;
263 int ret;
264
265 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
266 &pctldev, &pin_selector);
267 if (ret)
268 return ret;
269
270 ops = pinctrl_get_ops(pctldev);
Simon Glass33bf7b82021-03-25 10:26:11 +1300271 assert(ops);
272 if (!ops->gpio_disable_free)
273 return -ENOSYS;
Marek Vasut87510962019-04-21 23:57:23 +0200274
275 return ops->gpio_disable_free(pctldev, pin_selector);
276}
277
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900278/**
279 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
280 *
281 * @dev: peripheral device
282 * @return: 0 on success, or negative error code on failure
283 */
284static int pinctrl_select_state_simple(struct udevice *dev)
285{
286 struct udevice *pctldev;
287 struct pinctrl_ops *ops;
288 int ret;
289
290 /*
Patrice Chotard74110632019-02-25 13:39:55 +0100291 * For most system, there is only one pincontroller device. But in
292 * case of multiple pincontroller devices, probe the one with sequence
293 * number 0 (defined by alias) to avoid race condition.
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900294 */
Patrice Chotard74110632019-02-25 13:39:55 +0100295 ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev);
296 if (ret)
297 /* if not found, get the first one */
298 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900299 if (ret)
300 return ret;
301
302 ops = pinctrl_get_ops(pctldev);
303 if (!ops->set_state_simple) {
304 dev_dbg(dev, "set_state_simple op missing\n");
305 return -ENOSYS;
306 }
307
308 return ops->set_state_simple(pctldev, dev);
309}
310
311int pinctrl_select_state(struct udevice *dev, const char *statename)
312{
313 /*
Kever Yang16ed9712018-04-18 17:54:04 +0800314 * Some device which is logical like mmc.blk, do not have
315 * a valid ofnode.
316 */
Simon Glass07c17772020-12-19 10:40:12 -0700317 if (!dev_has_ofnode(dev))
Kever Yang16ed9712018-04-18 17:54:04 +0800318 return 0;
319 /*
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900320 * Try full-implemented pinctrl first.
321 * If it fails or is not implemented, try simple one.
322 */
Michael Walleac09e6b2023-01-18 13:12:22 +0100323 if (CONFIG_IS_ENABLED(PINCTRL_FULL))
324 return pinctrl_select_state_full(dev, statename);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900325
Michael Walleac09e6b2023-01-18 13:12:22 +0100326 return pinctrl_select_state_simple(dev);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900327}
328
Simon Glassac2ee652015-08-30 16:55:13 -0600329int pinctrl_request(struct udevice *dev, int func, int flags)
330{
331 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
332
333 if (!ops->request)
334 return -ENOSYS;
335
336 return ops->request(dev, func, flags);
337}
338
339int pinctrl_request_noflags(struct udevice *dev, int func)
340{
341 return pinctrl_request(dev, func, 0);
342}
343
344int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
345{
346 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
347
348 if (!ops->get_periph_id)
349 return -ENOSYS;
350
351 return ops->get_periph_id(dev, periph);
352}
353
Simon Glass2d4fa3c2016-01-21 19:43:56 -0700354int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
355{
356 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
357
358 if (!ops->get_gpio_mux)
359 return -ENOSYS;
360
361 return ops->get_gpio_mux(dev, banknum, index);
362}
363
Patrice Chotardfbf80822018-10-24 14:10:14 +0200364int pinctrl_get_pins_count(struct udevice *dev)
365{
366 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
367
368 if (!ops->get_pins_count)
369 return -ENOSYS;
370
371 return ops->get_pins_count(dev);
372}
373
374int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
375 int size)
376{
377 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
378
379 if (!ops->get_pin_name)
380 return -ENOSYS;
381
382 snprintf(buf, size, ops->get_pin_name(dev, selector));
383
384 return 0;
385}
386
Patrice Chotard74ba3f22018-10-24 14:10:13 +0200387int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf,
388 int size)
389{
390 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
391
392 if (!ops->get_pin_muxing)
393 return -ENOSYS;
394
395 return ops->get_pin_muxing(dev, selector, buf, size);
396}
397
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900398/**
Michael Walle6f772ea2023-01-18 13:12:24 +0100399 * pinctrl_post_bind() - post binding for PINCTRL uclass
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900400 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
401 *
402 * @dev: pinctrl device
403 * @return: 0 on success, or negative error code on failure
404 */
Patrick Delaunayd23bcf12019-08-02 14:48:00 +0200405static int __maybe_unused pinctrl_post_bind(struct udevice *dev)
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900406{
407 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
408
409 if (!ops) {
410 dev_dbg(dev, "ops is not set. Do not bind.\n");
411 return -EINVAL;
412 }
413
414 /*
Michael Walled08a9892023-01-18 13:12:23 +0100415 * If the pinctrl driver has the full implementation, its child nodes
416 * should be bound so that peripheral devices can easily search in
417 * parent devices during later DT-parsing.
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900418 */
Michael Walled08a9892023-01-18 13:12:23 +0100419 if (CONFIG_IS_ENABLED(PINCTRL_FULL))
Masahiro Yamada7778b032015-09-06 01:44:50 +0900420 return pinconfig_post_bind(dev);
421
422 return 0;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900423}
424
425UCLASS_DRIVER(pinctrl) = {
426 .id = UCLASS_PINCTRL,
Simon Glass92882652021-08-07 07:24:04 -0600427#if CONFIG_IS_ENABLED(OF_REAL)
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900428 .post_bind = pinctrl_post_bind,
Simon Glassdefcc632020-12-23 08:11:13 -0700429#endif
Thomas Abraham87adc642016-04-23 22:18:07 +0530430 .flags = DM_UC_FLAG_SEQ_ALIAS,
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900431 .name = "pinctrl",
432};