blob: 4e474cbff735270965f18d840e7592f729a8b4cd [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
6#include <common.h>
Simon Glass9bc15642020-02-03 07:36:16 -07007#include <malloc.h>
8#include <dm/device_compat.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +09009#include <linux/libfdt.h>
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090010#include <linux/err.h>
11#include <linux/list.h>
Simon Glass11c89f32017-05-17 17:18:03 -060012#include <dm.h>
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090013#include <dm/lists.h>
14#include <dm/pinctrl.h>
Heiko Stübner9a2cdca2017-02-18 19:46:21 +010015#include <dm/util.h>
Kever Yanga5897622018-02-09 10:56:24 +080016#include <dm/of_access.h>
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090017
18DECLARE_GLOBAL_DATA_PTR;
19
20#if CONFIG_IS_ENABLED(PINCTRL_FULL)
21/**
22 * pinctrl_config_one() - apply pinctrl settings for a single node
23 *
24 * @config: pin configuration node
25 * @return: 0 on success, or negative error code on failure
26 */
27static int pinctrl_config_one(struct udevice *config)
28{
29 struct udevice *pctldev;
30 const struct pinctrl_ops *ops;
31
32 pctldev = config;
33 for (;;) {
34 pctldev = dev_get_parent(pctldev);
35 if (!pctldev) {
36 dev_err(config, "could not find pctldev\n");
37 return -EINVAL;
38 }
39 if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
40 break;
41 }
42
43 ops = pinctrl_get_ops(pctldev);
44 return ops->set_state(pctldev, config);
45}
46
47/**
48 * pinctrl_select_state_full() - full implementation of pinctrl_select_state
49 *
50 * @dev: peripheral device
51 * @statename: state name, like "default"
52 * @return: 0 on success, or negative error code on failure
53 */
54static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
55{
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090056 char propname[32]; /* long enough */
57 const fdt32_t *list;
58 uint32_t phandle;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090059 struct udevice *config;
60 int state, size, i, ret;
61
Kever Yanga5897622018-02-09 10:56:24 +080062 state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090063 if (state < 0) {
64 char *end;
65 /*
66 * If statename is not found in "pinctrl-names",
67 * assume statename is just the integer state ID.
68 */
69 state = simple_strtoul(statename, &end, 10);
70 if (*end)
71 return -EINVAL;
72 }
73
74 snprintf(propname, sizeof(propname), "pinctrl-%d", state);
Kever Yanga5897622018-02-09 10:56:24 +080075 list = dev_read_prop(dev, propname, &size);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090076 if (!list)
77 return -EINVAL;
78
79 size /= sizeof(*list);
80 for (i = 0; i < size; i++) {
81 phandle = fdt32_to_cpu(*list++);
Kever Yanga5897622018-02-09 10:56:24 +080082 ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
83 &config);
Michael Trimarchi990a4c52019-09-17 22:06:03 +020084 if (ret) {
85 dev_warn(dev, "%s: uclass_get_device_by_phandle_id: err=%d\n",
86 __func__, ret);
87 continue;
88 }
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090089
90 ret = pinctrl_config_one(config);
Michael Trimarchi990a4c52019-09-17 22:06:03 +020091 if (ret) {
92 dev_warn(dev, "%s: pinctrl_config_one: err=%d\n",
93 __func__, ret);
94 continue;
95 }
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090096 }
97
98 return 0;
99}
100
101/**
Masahiro Yamada0cbfe5c2016-08-19 18:26:54 +0900102 * pinconfig_post_bind() - post binding for PINCONFIG uclass
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900103 * Recursively bind its children as pinconfig devices.
104 *
105 * @dev: pinconfig device
106 * @return: 0 on success, or negative error code on failure
107 */
108static int pinconfig_post_bind(struct udevice *dev)
109{
Simon Glass9cefa472015-12-29 05:22:52 -0700110 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900111 const char *name;
Simon Glasse6dd8da2017-05-18 20:09:07 -0600112 ofnode node;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900113 int ret;
114
Urja Rannikkocb484fc2019-03-22 19:14:33 +0000115 if (!dev_of_valid(dev))
116 return 0;
117
Simon Glasse6dd8da2017-05-18 20:09:07 -0600118 dev_for_each_subnode(node, dev) {
Lukasz Majewski6fdf2722019-01-09 23:05:02 +0100119 if (pre_reloc_only &&
120 !ofnode_pre_reloc(node))
Simon Glass9cefa472015-12-29 05:22:52 -0700121 continue;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900122 /*
123 * If this node has "compatible" property, this is not
124 * a pin configuration node, but a normal device. skip.
125 */
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900126 ofnode_get_property(node, "compatible", &ret);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900127 if (ret >= 0)
128 continue;
Patrick Delaunay1408ab32019-02-25 13:39:56 +0100129 /* If this node has "gpio-controller" property, skip */
130 if (ofnode_read_bool(node, "gpio-controller"))
131 continue;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900132
133 if (ret != -FDT_ERR_NOTFOUND)
134 return ret;
135
Simon Glasse6dd8da2017-05-18 20:09:07 -0600136 name = ofnode_get_name(node);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900137 if (!name)
138 return -EINVAL;
139 ret = device_bind_driver_to_node(dev, "pinconfig", name,
Simon Glasse6dd8da2017-05-18 20:09:07 -0600140 node, NULL);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900141 if (ret)
142 return ret;
143 }
144
145 return 0;
146}
147
148UCLASS_DRIVER(pinconfig) = {
149 .id = UCLASS_PINCONFIG,
Patrick Delaunay2c7526a2019-10-21 15:02:40 +0200150#if CONFIG_IS_ENABLED(PINCONF_RECURSIVE)
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900151 .post_bind = pinconfig_post_bind,
Patrick Delaunaybcdb1042019-08-02 14:48:00 +0200152#endif
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900153 .name = "pinconfig",
154};
155
156U_BOOT_DRIVER(pinconfig_generic) = {
157 .name = "pinconfig",
158 .id = UCLASS_PINCONFIG,
159};
160
161#else
162static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
163{
164 return -ENODEV;
165}
166
167static int pinconfig_post_bind(struct udevice *dev)
168{
Masahiro Yamada7778b032015-09-06 01:44:50 +0900169 return 0;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900170}
171#endif
172
Marek Vasut87510962019-04-21 23:57:23 +0200173static int
174pinctrl_gpio_get_pinctrl_and_offset(struct udevice *dev, unsigned offset,
175 struct udevice **pctldev,
176 unsigned int *pin_selector)
177{
178 struct ofnode_phandle_args args;
179 unsigned gpio_offset, pfc_base, pfc_pins;
180 int ret;
181
182 ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
183 0, &args);
184 if (ret) {
185 dev_dbg(dev, "%s: dev_read_phandle_with_args: err=%d\n",
186 __func__, ret);
187 return ret;
188 }
189
190 ret = uclass_get_device_by_ofnode(UCLASS_PINCTRL,
191 args.node, pctldev);
192 if (ret) {
193 dev_dbg(dev,
194 "%s: uclass_get_device_by_of_offset failed: err=%d\n",
195 __func__, ret);
196 return ret;
197 }
198
199 gpio_offset = args.args[0];
200 pfc_base = args.args[1];
201 pfc_pins = args.args[2];
202
203 if (offset < gpio_offset || offset > gpio_offset + pfc_pins) {
204 dev_dbg(dev,
205 "%s: GPIO can not be mapped to pincontrol pin\n",
206 __func__);
207 return -EINVAL;
208 }
209
210 offset -= gpio_offset;
211 offset += pfc_base;
212 *pin_selector = offset;
213
214 return 0;
215}
216
217/**
218 * pinctrl_gpio_request() - request a single pin to be used as GPIO
219 *
220 * @dev: GPIO peripheral device
221 * @offset: the GPIO pin offset from the GPIO controller
222 * @return: 0 on success, or negative error code on failure
223 */
224int pinctrl_gpio_request(struct udevice *dev, unsigned offset)
225{
226 const struct pinctrl_ops *ops;
227 struct udevice *pctldev;
228 unsigned int pin_selector;
229 int ret;
230
231 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
232 &pctldev, &pin_selector);
233 if (ret)
234 return ret;
235
236 ops = pinctrl_get_ops(pctldev);
237 if (!ops || !ops->gpio_request_enable)
238 return -ENOTSUPP;
239
240 return ops->gpio_request_enable(pctldev, pin_selector);
241}
242
243/**
244 * pinctrl_gpio_free() - free a single pin used as GPIO
245 *
246 * @dev: GPIO peripheral device
247 * @offset: the GPIO pin offset from the GPIO controller
248 * @return: 0 on success, or negative error code on failure
249 */
250int pinctrl_gpio_free(struct udevice *dev, unsigned offset)
251{
252 const struct pinctrl_ops *ops;
253 struct udevice *pctldev;
254 unsigned int pin_selector;
255 int ret;
256
257 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
258 &pctldev, &pin_selector);
259 if (ret)
260 return ret;
261
262 ops = pinctrl_get_ops(pctldev);
263 if (!ops || !ops->gpio_disable_free)
264 return -ENOTSUPP;
265
266 return ops->gpio_disable_free(pctldev, pin_selector);
267}
268
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900269/**
270 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
271 *
272 * @dev: peripheral device
273 * @return: 0 on success, or negative error code on failure
274 */
275static int pinctrl_select_state_simple(struct udevice *dev)
276{
277 struct udevice *pctldev;
278 struct pinctrl_ops *ops;
279 int ret;
280
281 /*
Patrice Chotard74110632019-02-25 13:39:55 +0100282 * For most system, there is only one pincontroller device. But in
283 * case of multiple pincontroller devices, probe the one with sequence
284 * number 0 (defined by alias) to avoid race condition.
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900285 */
Patrice Chotard74110632019-02-25 13:39:55 +0100286 ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev);
287 if (ret)
288 /* if not found, get the first one */
289 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900290 if (ret)
291 return ret;
292
293 ops = pinctrl_get_ops(pctldev);
294 if (!ops->set_state_simple) {
295 dev_dbg(dev, "set_state_simple op missing\n");
296 return -ENOSYS;
297 }
298
299 return ops->set_state_simple(pctldev, dev);
300}
301
302int pinctrl_select_state(struct udevice *dev, const char *statename)
303{
304 /*
Kever Yang16ed9712018-04-18 17:54:04 +0800305 * Some device which is logical like mmc.blk, do not have
306 * a valid ofnode.
307 */
Simon Glassdefcc632020-12-23 08:11:13 -0700308 if (!dev_has_of_node(dev))
Kever Yang16ed9712018-04-18 17:54:04 +0800309 return 0;
310 /*
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900311 * Try full-implemented pinctrl first.
312 * If it fails or is not implemented, try simple one.
313 */
314 if (pinctrl_select_state_full(dev, statename))
315 return pinctrl_select_state_simple(dev);
316
317 return 0;
318}
319
Simon Glassac2ee652015-08-30 16:55:13 -0600320int pinctrl_request(struct udevice *dev, int func, int flags)
321{
322 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
323
324 if (!ops->request)
325 return -ENOSYS;
326
327 return ops->request(dev, func, flags);
328}
329
330int pinctrl_request_noflags(struct udevice *dev, int func)
331{
332 return pinctrl_request(dev, func, 0);
333}
334
335int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
336{
337 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
338
339 if (!ops->get_periph_id)
340 return -ENOSYS;
341
342 return ops->get_periph_id(dev, periph);
343}
344
Simon Glass2d4fa3c2016-01-21 19:43:56 -0700345int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
346{
347 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
348
349 if (!ops->get_gpio_mux)
350 return -ENOSYS;
351
352 return ops->get_gpio_mux(dev, banknum, index);
353}
354
Patrice Chotardfbf80822018-10-24 14:10:14 +0200355int pinctrl_get_pins_count(struct udevice *dev)
356{
357 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
358
359 if (!ops->get_pins_count)
360 return -ENOSYS;
361
362 return ops->get_pins_count(dev);
363}
364
365int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
366 int size)
367{
368 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
369
370 if (!ops->get_pin_name)
371 return -ENOSYS;
372
373 snprintf(buf, size, ops->get_pin_name(dev, selector));
374
375 return 0;
376}
377
Patrice Chotard74ba3f22018-10-24 14:10:13 +0200378int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf,
379 int size)
380{
381 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
382
383 if (!ops->get_pin_muxing)
384 return -ENOSYS;
385
386 return ops->get_pin_muxing(dev, selector, buf, size);
387}
388
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900389/**
Masahiro Yamada0cbfe5c2016-08-19 18:26:54 +0900390 * pinconfig_post_bind() - post binding for PINCTRL uclass
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900391 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
392 *
393 * @dev: pinctrl device
394 * @return: 0 on success, or negative error code on failure
395 */
Patrick Delaunayd23bcf12019-08-02 14:48:00 +0200396static int __maybe_unused pinctrl_post_bind(struct udevice *dev)
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900397{
398 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
399
400 if (!ops) {
401 dev_dbg(dev, "ops is not set. Do not bind.\n");
402 return -EINVAL;
403 }
404
405 /*
Masahiro Yamada7778b032015-09-06 01:44:50 +0900406 * If set_state callback is set, we assume this pinctrl driver is the
407 * full implementation. In this case, its child nodes should be bound
408 * so that peripheral devices can easily search in parent devices
409 * during later DT-parsing.
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900410 */
Masahiro Yamada7778b032015-09-06 01:44:50 +0900411 if (ops->set_state)
412 return pinconfig_post_bind(dev);
413
414 return 0;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900415}
416
417UCLASS_DRIVER(pinctrl) = {
418 .id = UCLASS_PINCTRL,
Simon Glassdefcc632020-12-23 08:11:13 -0700419#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900420 .post_bind = pinctrl_post_bind,
Simon Glassdefcc632020-12-23 08:11:13 -0700421#endif
Thomas Abraham87adc642016-04-23 22:18:07 +0530422 .flags = DM_UC_FLAG_SEQ_ALIAS,
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900423 .name = "pinctrl",
424};