blob: 3425ed11b16479942a807234d3eda92a861535ef [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
6#include <common.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +09007#include <linux/libfdt.h>
Masahiro Yamadad90a5a32015-08-27 12:44:29 +09008#include <linux/err.h>
9#include <linux/list.h>
Simon Glass9d922452017-05-17 17:18:03 -060010#include <dm.h>
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090011#include <dm/lists.h>
12#include <dm/pinctrl.h>
Heiko Stübner27326c72017-02-18 19:46:21 +010013#include <dm/util.h>
Kever Yang1e656ad2018-02-09 10:56:24 +080014#include <dm/of_access.h>
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090015
16DECLARE_GLOBAL_DATA_PTR;
17
18#if CONFIG_IS_ENABLED(PINCTRL_FULL)
19/**
20 * pinctrl_config_one() - apply pinctrl settings for a single node
21 *
22 * @config: pin configuration node
23 * @return: 0 on success, or negative error code on failure
24 */
25static int pinctrl_config_one(struct udevice *config)
26{
27 struct udevice *pctldev;
28 const struct pinctrl_ops *ops;
29
30 pctldev = config;
31 for (;;) {
32 pctldev = dev_get_parent(pctldev);
33 if (!pctldev) {
34 dev_err(config, "could not find pctldev\n");
35 return -EINVAL;
36 }
37 if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
38 break;
39 }
40
41 ops = pinctrl_get_ops(pctldev);
42 return ops->set_state(pctldev, config);
43}
44
45/**
46 * pinctrl_select_state_full() - full implementation of pinctrl_select_state
47 *
48 * @dev: peripheral device
49 * @statename: state name, like "default"
50 * @return: 0 on success, or negative error code on failure
51 */
52static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
53{
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090054 char propname[32]; /* long enough */
55 const fdt32_t *list;
56 uint32_t phandle;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090057 struct udevice *config;
58 int state, size, i, ret;
59
Kever Yang1e656ad2018-02-09 10:56:24 +080060 state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090061 if (state < 0) {
62 char *end;
63 /*
64 * If statename is not found in "pinctrl-names",
65 * assume statename is just the integer state ID.
66 */
67 state = simple_strtoul(statename, &end, 10);
68 if (*end)
69 return -EINVAL;
70 }
71
72 snprintf(propname, sizeof(propname), "pinctrl-%d", state);
Kever Yang1e656ad2018-02-09 10:56:24 +080073 list = dev_read_prop(dev, propname, &size);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090074 if (!list)
75 return -EINVAL;
76
77 size /= sizeof(*list);
78 for (i = 0; i < size; i++) {
79 phandle = fdt32_to_cpu(*list++);
Kever Yang1e656ad2018-02-09 10:56:24 +080080 ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
81 &config);
Michael Trimarchi36a90ed2019-09-17 22:06:03 +020082 if (ret) {
83 dev_warn(dev, "%s: uclass_get_device_by_phandle_id: err=%d\n",
84 __func__, ret);
85 continue;
86 }
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090087
88 ret = pinctrl_config_one(config);
Michael Trimarchi36a90ed2019-09-17 22:06:03 +020089 if (ret) {
90 dev_warn(dev, "%s: pinctrl_config_one: err=%d\n",
91 __func__, ret);
92 continue;
93 }
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090094 }
95
96 return 0;
97}
98
99/**
Masahiro Yamadaf8357062016-08-19 18:26:54 +0900100 * pinconfig_post_bind() - post binding for PINCONFIG uclass
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900101 * Recursively bind its children as pinconfig devices.
102 *
103 * @dev: pinconfig device
104 * @return: 0 on success, or negative error code on failure
105 */
106static int pinconfig_post_bind(struct udevice *dev)
107{
Simon Glass5589a812015-12-29 05:22:52 -0700108 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900109 const char *name;
Simon Glass45a26862017-05-18 20:09:07 -0600110 ofnode node;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900111 int ret;
112
Urja Rannikko63402832019-03-22 19:14:33 +0000113 if (!dev_of_valid(dev))
114 return 0;
115
Simon Glass45a26862017-05-18 20:09:07 -0600116 dev_for_each_subnode(node, dev) {
Lukasz Majewskib6a62382019-01-09 23:05:02 +0100117 if (pre_reloc_only &&
118 !ofnode_pre_reloc(node))
Simon Glass5589a812015-12-29 05:22:52 -0700119 continue;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900120 /*
121 * If this node has "compatible" property, this is not
122 * a pin configuration node, but a normal device. skip.
123 */
Masahiro Yamada61e51ba2017-06-22 16:54:05 +0900124 ofnode_get_property(node, "compatible", &ret);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900125 if (ret >= 0)
126 continue;
Patrick Delaunayd39e2bd2019-02-25 13:39:56 +0100127 /* If this node has "gpio-controller" property, skip */
128 if (ofnode_read_bool(node, "gpio-controller"))
129 continue;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900130
131 if (ret != -FDT_ERR_NOTFOUND)
132 return ret;
133
Simon Glass45a26862017-05-18 20:09:07 -0600134 name = ofnode_get_name(node);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900135 if (!name)
136 return -EINVAL;
137 ret = device_bind_driver_to_node(dev, "pinconfig", name,
Simon Glass45a26862017-05-18 20:09:07 -0600138 node, NULL);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900139 if (ret)
140 return ret;
141 }
142
143 return 0;
144}
145
146UCLASS_DRIVER(pinconfig) = {
147 .id = UCLASS_PINCONFIG,
Patrick Delaunay44510da2019-10-21 15:02:40 +0200148#if CONFIG_IS_ENABLED(PINCONF_RECURSIVE)
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900149 .post_bind = pinconfig_post_bind,
Patrick Delaunayc20851b2019-08-02 14:48:00 +0200150#endif
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900151 .name = "pinconfig",
152};
153
154U_BOOT_DRIVER(pinconfig_generic) = {
155 .name = "pinconfig",
156 .id = UCLASS_PINCONFIG,
157};
158
159#else
160static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
161{
162 return -ENODEV;
163}
164
165static int pinconfig_post_bind(struct udevice *dev)
166{
Masahiro Yamada8a5f6122015-09-06 01:44:50 +0900167 return 0;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900168}
169#endif
170
Marek Vasutae59d7c2019-04-21 23:57:23 +0200171static int
172pinctrl_gpio_get_pinctrl_and_offset(struct udevice *dev, unsigned offset,
173 struct udevice **pctldev,
174 unsigned int *pin_selector)
175{
176 struct ofnode_phandle_args args;
177 unsigned gpio_offset, pfc_base, pfc_pins;
178 int ret;
179
180 ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
181 0, &args);
182 if (ret) {
183 dev_dbg(dev, "%s: dev_read_phandle_with_args: err=%d\n",
184 __func__, ret);
185 return ret;
186 }
187
188 ret = uclass_get_device_by_ofnode(UCLASS_PINCTRL,
189 args.node, pctldev);
190 if (ret) {
191 dev_dbg(dev,
192 "%s: uclass_get_device_by_of_offset failed: err=%d\n",
193 __func__, ret);
194 return ret;
195 }
196
197 gpio_offset = args.args[0];
198 pfc_base = args.args[1];
199 pfc_pins = args.args[2];
200
201 if (offset < gpio_offset || offset > gpio_offset + pfc_pins) {
202 dev_dbg(dev,
203 "%s: GPIO can not be mapped to pincontrol pin\n",
204 __func__);
205 return -EINVAL;
206 }
207
208 offset -= gpio_offset;
209 offset += pfc_base;
210 *pin_selector = offset;
211
212 return 0;
213}
214
215/**
216 * pinctrl_gpio_request() - request a single pin to be used as GPIO
217 *
218 * @dev: GPIO peripheral device
219 * @offset: the GPIO pin offset from the GPIO controller
220 * @return: 0 on success, or negative error code on failure
221 */
222int pinctrl_gpio_request(struct udevice *dev, unsigned offset)
223{
224 const struct pinctrl_ops *ops;
225 struct udevice *pctldev;
226 unsigned int pin_selector;
227 int ret;
228
229 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
230 &pctldev, &pin_selector);
231 if (ret)
232 return ret;
233
234 ops = pinctrl_get_ops(pctldev);
235 if (!ops || !ops->gpio_request_enable)
236 return -ENOTSUPP;
237
238 return ops->gpio_request_enable(pctldev, pin_selector);
239}
240
241/**
242 * pinctrl_gpio_free() - free a single pin used as GPIO
243 *
244 * @dev: GPIO peripheral device
245 * @offset: the GPIO pin offset from the GPIO controller
246 * @return: 0 on success, or negative error code on failure
247 */
248int pinctrl_gpio_free(struct udevice *dev, unsigned offset)
249{
250 const struct pinctrl_ops *ops;
251 struct udevice *pctldev;
252 unsigned int pin_selector;
253 int ret;
254
255 ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
256 &pctldev, &pin_selector);
257 if (ret)
258 return ret;
259
260 ops = pinctrl_get_ops(pctldev);
261 if (!ops || !ops->gpio_disable_free)
262 return -ENOTSUPP;
263
264 return ops->gpio_disable_free(pctldev, pin_selector);
265}
266
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900267/**
268 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
269 *
270 * @dev: peripheral device
271 * @return: 0 on success, or negative error code on failure
272 */
273static int pinctrl_select_state_simple(struct udevice *dev)
274{
275 struct udevice *pctldev;
276 struct pinctrl_ops *ops;
277 int ret;
278
279 /*
Patrice Chotarddce406e2019-02-25 13:39:55 +0100280 * For most system, there is only one pincontroller device. But in
281 * case of multiple pincontroller devices, probe the one with sequence
282 * number 0 (defined by alias) to avoid race condition.
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900283 */
Patrice Chotarddce406e2019-02-25 13:39:55 +0100284 ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev);
285 if (ret)
286 /* if not found, get the first one */
287 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900288 if (ret)
289 return ret;
290
291 ops = pinctrl_get_ops(pctldev);
292 if (!ops->set_state_simple) {
293 dev_dbg(dev, "set_state_simple op missing\n");
294 return -ENOSYS;
295 }
296
297 return ops->set_state_simple(pctldev, dev);
298}
299
300int pinctrl_select_state(struct udevice *dev, const char *statename)
301{
302 /*
Kever Yangf717b4c2018-04-18 17:54:04 +0800303 * Some device which is logical like mmc.blk, do not have
304 * a valid ofnode.
305 */
306 if (!ofnode_valid(dev->node))
307 return 0;
308 /*
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900309 * Try full-implemented pinctrl first.
310 * If it fails or is not implemented, try simple one.
311 */
312 if (pinctrl_select_state_full(dev, statename))
313 return pinctrl_select_state_simple(dev);
314
315 return 0;
316}
317
Simon Glassc5acf4a2015-08-30 16:55:13 -0600318int pinctrl_request(struct udevice *dev, int func, int flags)
319{
320 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
321
322 if (!ops->request)
323 return -ENOSYS;
324
325 return ops->request(dev, func, flags);
326}
327
328int pinctrl_request_noflags(struct udevice *dev, int func)
329{
330 return pinctrl_request(dev, func, 0);
331}
332
333int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
334{
335 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
336
337 if (!ops->get_periph_id)
338 return -ENOSYS;
339
340 return ops->get_periph_id(dev, periph);
341}
342
Simon Glass77eaa192016-01-21 19:43:56 -0700343int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
344{
345 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
346
347 if (!ops->get_gpio_mux)
348 return -ENOSYS;
349
350 return ops->get_gpio_mux(dev, banknum, index);
351}
352
Patrice Chotard8bbb5b22018-10-24 14:10:14 +0200353int pinctrl_get_pins_count(struct udevice *dev)
354{
355 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
356
357 if (!ops->get_pins_count)
358 return -ENOSYS;
359
360 return ops->get_pins_count(dev);
361}
362
363int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
364 int size)
365{
366 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
367
368 if (!ops->get_pin_name)
369 return -ENOSYS;
370
371 snprintf(buf, size, ops->get_pin_name(dev, selector));
372
373 return 0;
374}
375
Patrice Chotardf55a0c02018-10-24 14:10:13 +0200376int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf,
377 int size)
378{
379 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
380
381 if (!ops->get_pin_muxing)
382 return -ENOSYS;
383
384 return ops->get_pin_muxing(dev, selector, buf, size);
385}
386
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900387/**
Masahiro Yamadaf8357062016-08-19 18:26:54 +0900388 * pinconfig_post_bind() - post binding for PINCTRL uclass
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900389 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
390 *
391 * @dev: pinctrl device
392 * @return: 0 on success, or negative error code on failure
393 */
Patrick Delaunaye878b532019-08-02 14:48:00 +0200394static int __maybe_unused pinctrl_post_bind(struct udevice *dev)
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900395{
396 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
397
398 if (!ops) {
399 dev_dbg(dev, "ops is not set. Do not bind.\n");
400 return -EINVAL;
401 }
402
403 /*
Masahiro Yamada8a5f6122015-09-06 01:44:50 +0900404 * If set_state callback is set, we assume this pinctrl driver is the
405 * full implementation. In this case, its child nodes should be bound
406 * so that peripheral devices can easily search in parent devices
407 * during later DT-parsing.
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900408 */
Masahiro Yamada8a5f6122015-09-06 01:44:50 +0900409 if (ops->set_state)
410 return pinconfig_post_bind(dev);
411
412 return 0;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900413}
414
415UCLASS_DRIVER(pinctrl) = {
416 .id = UCLASS_PINCTRL,
417 .post_bind = pinctrl_post_bind,
Thomas Abrahamac985272016-04-23 22:18:07 +0530418 .flags = DM_UC_FLAG_SEQ_ALIAS,
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900419 .name = "pinctrl",
420};