blob: 4e6cef8ae762f4a3382dc29910109b84514c4fc0 [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
Simon Glass52db39a2016-01-21 19:43:26 -070018int pinctrl_decode_pin_config(const void *blob, int node)
19{
20 int flags = 0;
21
22 if (fdtdec_get_bool(blob, node, "bias-pull-up"))
23 flags |= 1 << PIN_CONFIG_BIAS_PULL_UP;
24 else if (fdtdec_get_bool(blob, node, "bias-pull-down"))
25 flags |= 1 << PIN_CONFIG_BIAS_PULL_DOWN;
26
27 return flags;
28}
29
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090030#if CONFIG_IS_ENABLED(PINCTRL_FULL)
31/**
32 * pinctrl_config_one() - apply pinctrl settings for a single node
33 *
34 * @config: pin configuration node
35 * @return: 0 on success, or negative error code on failure
36 */
37static int pinctrl_config_one(struct udevice *config)
38{
39 struct udevice *pctldev;
40 const struct pinctrl_ops *ops;
41
42 pctldev = config;
43 for (;;) {
44 pctldev = dev_get_parent(pctldev);
45 if (!pctldev) {
46 dev_err(config, "could not find pctldev\n");
47 return -EINVAL;
48 }
49 if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
50 break;
51 }
52
53 ops = pinctrl_get_ops(pctldev);
54 return ops->set_state(pctldev, config);
55}
56
57/**
58 * pinctrl_select_state_full() - full implementation of pinctrl_select_state
59 *
60 * @dev: peripheral device
61 * @statename: state name, like "default"
62 * @return: 0 on success, or negative error code on failure
63 */
64static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
65{
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090066 char propname[32]; /* long enough */
67 const fdt32_t *list;
68 uint32_t phandle;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090069 struct udevice *config;
70 int state, size, i, ret;
71
Kever Yang1e656ad2018-02-09 10:56:24 +080072 state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090073 if (state < 0) {
74 char *end;
75 /*
76 * If statename is not found in "pinctrl-names",
77 * assume statename is just the integer state ID.
78 */
79 state = simple_strtoul(statename, &end, 10);
80 if (*end)
81 return -EINVAL;
82 }
83
84 snprintf(propname, sizeof(propname), "pinctrl-%d", state);
Kever Yang1e656ad2018-02-09 10:56:24 +080085 list = dev_read_prop(dev, propname, &size);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090086 if (!list)
87 return -EINVAL;
88
89 size /= sizeof(*list);
90 for (i = 0; i < size; i++) {
91 phandle = fdt32_to_cpu(*list++);
Kever Yang1e656ad2018-02-09 10:56:24 +080092 ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
93 &config);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090094 if (ret)
95 return ret;
96
97 ret = pinctrl_config_one(config);
98 if (ret)
99 return ret;
100 }
101
102 return 0;
103}
104
105/**
Masahiro Yamadaf8357062016-08-19 18:26:54 +0900106 * pinconfig_post_bind() - post binding for PINCONFIG uclass
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900107 * Recursively bind its children as pinconfig devices.
108 *
109 * @dev: pinconfig device
110 * @return: 0 on success, or negative error code on failure
111 */
112static int pinconfig_post_bind(struct udevice *dev)
113{
Simon Glass5589a812015-12-29 05:22:52 -0700114 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900115 const char *name;
Simon Glass45a26862017-05-18 20:09:07 -0600116 ofnode node;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900117 int ret;
118
Simon Glass45a26862017-05-18 20:09:07 -0600119 dev_for_each_subnode(node, dev) {
Lukasz Majewskib6a62382019-01-09 23:05:02 +0100120 if (pre_reloc_only &&
121 !ofnode_pre_reloc(node))
Simon Glass5589a812015-12-29 05:22:52 -0700122 continue;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900123 /*
124 * If this node has "compatible" property, this is not
125 * a pin configuration node, but a normal device. skip.
126 */
Masahiro Yamada61e51ba2017-06-22 16:54:05 +0900127 ofnode_get_property(node, "compatible", &ret);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900128 if (ret >= 0)
129 continue;
130
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,
148 .post_bind = pinconfig_post_bind,
149 .name = "pinconfig",
150};
151
152U_BOOT_DRIVER(pinconfig_generic) = {
153 .name = "pinconfig",
154 .id = UCLASS_PINCONFIG,
155};
156
157#else
158static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
159{
160 return -ENODEV;
161}
162
163static int pinconfig_post_bind(struct udevice *dev)
164{
Masahiro Yamada8a5f6122015-09-06 01:44:50 +0900165 return 0;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900166}
167#endif
168
169/**
170 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
171 *
172 * @dev: peripheral device
173 * @return: 0 on success, or negative error code on failure
174 */
175static int pinctrl_select_state_simple(struct udevice *dev)
176{
177 struct udevice *pctldev;
178 struct pinctrl_ops *ops;
179 int ret;
180
181 /*
Patrice Chotarddce406e2019-02-25 13:39:55 +0100182 * For most system, there is only one pincontroller device. But in
183 * case of multiple pincontroller devices, probe the one with sequence
184 * number 0 (defined by alias) to avoid race condition.
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900185 */
Patrice Chotarddce406e2019-02-25 13:39:55 +0100186 ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev);
187 if (ret)
188 /* if not found, get the first one */
189 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900190 if (ret)
191 return ret;
192
193 ops = pinctrl_get_ops(pctldev);
194 if (!ops->set_state_simple) {
195 dev_dbg(dev, "set_state_simple op missing\n");
196 return -ENOSYS;
197 }
198
199 return ops->set_state_simple(pctldev, dev);
200}
201
202int pinctrl_select_state(struct udevice *dev, const char *statename)
203{
204 /*
Kever Yangf717b4c2018-04-18 17:54:04 +0800205 * Some device which is logical like mmc.blk, do not have
206 * a valid ofnode.
207 */
208 if (!ofnode_valid(dev->node))
209 return 0;
210 /*
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900211 * Try full-implemented pinctrl first.
212 * If it fails or is not implemented, try simple one.
213 */
214 if (pinctrl_select_state_full(dev, statename))
215 return pinctrl_select_state_simple(dev);
216
217 return 0;
218}
219
Simon Glassc5acf4a2015-08-30 16:55:13 -0600220int pinctrl_request(struct udevice *dev, int func, int flags)
221{
222 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
223
224 if (!ops->request)
225 return -ENOSYS;
226
227 return ops->request(dev, func, flags);
228}
229
230int pinctrl_request_noflags(struct udevice *dev, int func)
231{
232 return pinctrl_request(dev, func, 0);
233}
234
235int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
236{
237 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
238
239 if (!ops->get_periph_id)
240 return -ENOSYS;
241
242 return ops->get_periph_id(dev, periph);
243}
244
Simon Glass77eaa192016-01-21 19:43:56 -0700245int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
246{
247 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
248
249 if (!ops->get_gpio_mux)
250 return -ENOSYS;
251
252 return ops->get_gpio_mux(dev, banknum, index);
253}
254
Patrice Chotard8bbb5b22018-10-24 14:10:14 +0200255int pinctrl_get_pins_count(struct udevice *dev)
256{
257 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
258
259 if (!ops->get_pins_count)
260 return -ENOSYS;
261
262 return ops->get_pins_count(dev);
263}
264
265int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
266 int size)
267{
268 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
269
270 if (!ops->get_pin_name)
271 return -ENOSYS;
272
273 snprintf(buf, size, ops->get_pin_name(dev, selector));
274
275 return 0;
276}
277
Patrice Chotardf55a0c02018-10-24 14:10:13 +0200278int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf,
279 int size)
280{
281 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
282
283 if (!ops->get_pin_muxing)
284 return -ENOSYS;
285
286 return ops->get_pin_muxing(dev, selector, buf, size);
287}
288
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900289/**
Masahiro Yamadaf8357062016-08-19 18:26:54 +0900290 * pinconfig_post_bind() - post binding for PINCTRL uclass
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900291 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
292 *
293 * @dev: pinctrl device
294 * @return: 0 on success, or negative error code on failure
295 */
296static int pinctrl_post_bind(struct udevice *dev)
297{
298 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
299
300 if (!ops) {
301 dev_dbg(dev, "ops is not set. Do not bind.\n");
302 return -EINVAL;
303 }
304
305 /*
Masahiro Yamada8a5f6122015-09-06 01:44:50 +0900306 * If set_state callback is set, we assume this pinctrl driver is the
307 * full implementation. In this case, its child nodes should be bound
308 * so that peripheral devices can easily search in parent devices
309 * during later DT-parsing.
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900310 */
Masahiro Yamada8a5f6122015-09-06 01:44:50 +0900311 if (ops->set_state)
312 return pinconfig_post_bind(dev);
313
314 return 0;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900315}
316
317UCLASS_DRIVER(pinctrl) = {
318 .id = UCLASS_PINCTRL,
319 .post_bind = pinctrl_post_bind,
Thomas Abrahamac985272016-04-23 22:18:07 +0530320 .flags = DM_UC_FLAG_SEQ_ALIAS,
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900321 .name = "pinctrl",
322};