blob: 0e3260afd1ee72afa4fa349114334a534e4cedf8 [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
Christoph Muellner5ff77682019-01-02 15:09:18 +010030/*
31 * TODO: this function is temporary for v2019.01.
32 * It should be renamed to pinctrl_decode_pin_config(),
33 * the original pinctrl_decode_pin_config() function should
34 * be removed and all callers of the original function should
35 * be migrated to use the new one.
36 */
37int pinctrl_decode_pin_config_dm(struct udevice *dev)
38{
39 int pinconfig = 0;
40
41 if (dev->uclass->uc_drv->id != UCLASS_PINCONFIG)
42 return -EINVAL;
43
44 if (dev_read_bool(dev, "bias-pull-up"))
45 pinconfig |= 1 << PIN_CONFIG_BIAS_PULL_UP;
46 else if (dev_read_bool(dev, "bias-pull-down"))
47 pinconfig |= 1 << PIN_CONFIG_BIAS_PULL_DOWN;
48
49 return pinconfig;
50}
51
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090052#if CONFIG_IS_ENABLED(PINCTRL_FULL)
53/**
54 * pinctrl_config_one() - apply pinctrl settings for a single node
55 *
56 * @config: pin configuration node
57 * @return: 0 on success, or negative error code on failure
58 */
59static int pinctrl_config_one(struct udevice *config)
60{
61 struct udevice *pctldev;
62 const struct pinctrl_ops *ops;
63
64 pctldev = config;
65 for (;;) {
66 pctldev = dev_get_parent(pctldev);
67 if (!pctldev) {
68 dev_err(config, "could not find pctldev\n");
69 return -EINVAL;
70 }
71 if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
72 break;
73 }
74
75 ops = pinctrl_get_ops(pctldev);
76 return ops->set_state(pctldev, config);
77}
78
79/**
80 * pinctrl_select_state_full() - full implementation of pinctrl_select_state
81 *
82 * @dev: peripheral device
83 * @statename: state name, like "default"
84 * @return: 0 on success, or negative error code on failure
85 */
86static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
87{
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090088 char propname[32]; /* long enough */
89 const fdt32_t *list;
90 uint32_t phandle;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090091 struct udevice *config;
92 int state, size, i, ret;
93
Kever Yang1e656ad2018-02-09 10:56:24 +080094 state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090095 if (state < 0) {
96 char *end;
97 /*
98 * If statename is not found in "pinctrl-names",
99 * assume statename is just the integer state ID.
100 */
101 state = simple_strtoul(statename, &end, 10);
102 if (*end)
103 return -EINVAL;
104 }
105
106 snprintf(propname, sizeof(propname), "pinctrl-%d", state);
Kever Yang1e656ad2018-02-09 10:56:24 +0800107 list = dev_read_prop(dev, propname, &size);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900108 if (!list)
109 return -EINVAL;
110
111 size /= sizeof(*list);
112 for (i = 0; i < size; i++) {
113 phandle = fdt32_to_cpu(*list++);
Kever Yang1e656ad2018-02-09 10:56:24 +0800114 ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
115 &config);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900116 if (ret)
117 return ret;
118
119 ret = pinctrl_config_one(config);
120 if (ret)
121 return ret;
122 }
123
124 return 0;
125}
126
127/**
Masahiro Yamadaf8357062016-08-19 18:26:54 +0900128 * pinconfig_post_bind() - post binding for PINCONFIG uclass
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900129 * Recursively bind its children as pinconfig devices.
130 *
131 * @dev: pinconfig device
132 * @return: 0 on success, or negative error code on failure
133 */
134static int pinconfig_post_bind(struct udevice *dev)
135{
Simon Glass5589a812015-12-29 05:22:52 -0700136 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900137 const char *name;
Simon Glass45a26862017-05-18 20:09:07 -0600138 ofnode node;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900139 int ret;
140
Simon Glass45a26862017-05-18 20:09:07 -0600141 dev_for_each_subnode(node, dev) {
Lukasz Majewskib6a62382019-01-09 23:05:02 +0100142 if (pre_reloc_only &&
143 !ofnode_pre_reloc(node))
Simon Glass5589a812015-12-29 05:22:52 -0700144 continue;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900145 /*
146 * If this node has "compatible" property, this is not
147 * a pin configuration node, but a normal device. skip.
148 */
Masahiro Yamada61e51ba2017-06-22 16:54:05 +0900149 ofnode_get_property(node, "compatible", &ret);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900150 if (ret >= 0)
151 continue;
152
153 if (ret != -FDT_ERR_NOTFOUND)
154 return ret;
155
Simon Glass45a26862017-05-18 20:09:07 -0600156 name = ofnode_get_name(node);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900157 if (!name)
158 return -EINVAL;
159 ret = device_bind_driver_to_node(dev, "pinconfig", name,
Simon Glass45a26862017-05-18 20:09:07 -0600160 node, NULL);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900161 if (ret)
162 return ret;
163 }
164
165 return 0;
166}
167
168UCLASS_DRIVER(pinconfig) = {
169 .id = UCLASS_PINCONFIG,
170 .post_bind = pinconfig_post_bind,
171 .name = "pinconfig",
172};
173
174U_BOOT_DRIVER(pinconfig_generic) = {
175 .name = "pinconfig",
176 .id = UCLASS_PINCONFIG,
177};
178
179#else
180static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
181{
182 return -ENODEV;
183}
184
185static int pinconfig_post_bind(struct udevice *dev)
186{
Masahiro Yamada8a5f6122015-09-06 01:44:50 +0900187 return 0;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900188}
189#endif
190
191/**
192 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
193 *
194 * @dev: peripheral device
195 * @return: 0 on success, or negative error code on failure
196 */
197static int pinctrl_select_state_simple(struct udevice *dev)
198{
199 struct udevice *pctldev;
200 struct pinctrl_ops *ops;
201 int ret;
202
203 /*
204 * For simplicity, assume the first device of PINCTRL uclass
205 * is the correct one. This is most likely OK as there is
206 * usually only one pinctrl device on the system.
207 */
208 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
209 if (ret)
210 return ret;
211
212 ops = pinctrl_get_ops(pctldev);
213 if (!ops->set_state_simple) {
214 dev_dbg(dev, "set_state_simple op missing\n");
215 return -ENOSYS;
216 }
217
218 return ops->set_state_simple(pctldev, dev);
219}
220
221int pinctrl_select_state(struct udevice *dev, const char *statename)
222{
223 /*
Kever Yangf717b4c2018-04-18 17:54:04 +0800224 * Some device which is logical like mmc.blk, do not have
225 * a valid ofnode.
226 */
227 if (!ofnode_valid(dev->node))
228 return 0;
229 /*
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900230 * Try full-implemented pinctrl first.
231 * If it fails or is not implemented, try simple one.
232 */
233 if (pinctrl_select_state_full(dev, statename))
234 return pinctrl_select_state_simple(dev);
235
236 return 0;
237}
238
Simon Glassc5acf4a2015-08-30 16:55:13 -0600239int pinctrl_request(struct udevice *dev, int func, int flags)
240{
241 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
242
243 if (!ops->request)
244 return -ENOSYS;
245
246 return ops->request(dev, func, flags);
247}
248
249int pinctrl_request_noflags(struct udevice *dev, int func)
250{
251 return pinctrl_request(dev, func, 0);
252}
253
254int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
255{
256 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
257
258 if (!ops->get_periph_id)
259 return -ENOSYS;
260
261 return ops->get_periph_id(dev, periph);
262}
263
Simon Glass77eaa192016-01-21 19:43:56 -0700264int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
265{
266 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
267
268 if (!ops->get_gpio_mux)
269 return -ENOSYS;
270
271 return ops->get_gpio_mux(dev, banknum, index);
272}
273
Patrice Chotard8bbb5b22018-10-24 14:10:14 +0200274int pinctrl_get_pins_count(struct udevice *dev)
275{
276 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
277
278 if (!ops->get_pins_count)
279 return -ENOSYS;
280
281 return ops->get_pins_count(dev);
282}
283
284int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
285 int size)
286{
287 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
288
289 if (!ops->get_pin_name)
290 return -ENOSYS;
291
292 snprintf(buf, size, ops->get_pin_name(dev, selector));
293
294 return 0;
295}
296
Patrice Chotardf55a0c02018-10-24 14:10:13 +0200297int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf,
298 int size)
299{
300 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
301
302 if (!ops->get_pin_muxing)
303 return -ENOSYS;
304
305 return ops->get_pin_muxing(dev, selector, buf, size);
306}
307
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900308/**
Masahiro Yamadaf8357062016-08-19 18:26:54 +0900309 * pinconfig_post_bind() - post binding for PINCTRL uclass
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900310 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
311 *
312 * @dev: pinctrl device
313 * @return: 0 on success, or negative error code on failure
314 */
315static int pinctrl_post_bind(struct udevice *dev)
316{
317 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
318
319 if (!ops) {
320 dev_dbg(dev, "ops is not set. Do not bind.\n");
321 return -EINVAL;
322 }
323
324 /*
Masahiro Yamada8a5f6122015-09-06 01:44:50 +0900325 * If set_state callback is set, we assume this pinctrl driver is the
326 * full implementation. In this case, its child nodes should be bound
327 * so that peripheral devices can easily search in parent devices
328 * during later DT-parsing.
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900329 */
Masahiro Yamada8a5f6122015-09-06 01:44:50 +0900330 if (ops->set_state)
331 return pinconfig_post_bind(dev);
332
333 return 0;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900334}
335
336UCLASS_DRIVER(pinctrl) = {
337 .id = UCLASS_PINCTRL,
338 .post_bind = pinctrl_post_bind,
Thomas Abrahamac985272016-04-23 22:18:07 +0530339 .flags = DM_UC_FLAG_SEQ_ALIAS,
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900340 .name = "pinctrl",
341};