blob: a0a326a142f0aeb2ed06c5f8c61702e8b1927733 [file] [log] [blame]
Masahiro Yamadad90a5a32015-08-27 12:44:29 +09001/*
2 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +09008#include <linux/libfdt.h>
Masahiro Yamadad90a5a32015-08-27 12:44:29 +09009#include <linux/err.h>
10#include <linux/list.h>
Simon Glass9d922452017-05-17 17:18:03 -060011#include <dm.h>
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090012#include <dm/lists.h>
13#include <dm/pinctrl.h>
Heiko Stübner27326c72017-02-18 19:46:21 +010014#include <dm/util.h>
Kever Yang1e656ad2018-02-09 10:56:24 +080015#include <dm/of_access.h>
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090016
17DECLARE_GLOBAL_DATA_PTR;
18
Simon Glass52db39a2016-01-21 19:43:26 -070019int pinctrl_decode_pin_config(const void *blob, int node)
20{
21 int flags = 0;
22
23 if (fdtdec_get_bool(blob, node, "bias-pull-up"))
24 flags |= 1 << PIN_CONFIG_BIAS_PULL_UP;
25 else if (fdtdec_get_bool(blob, node, "bias-pull-down"))
26 flags |= 1 << PIN_CONFIG_BIAS_PULL_DOWN;
27
28 return flags;
29}
30
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090031#if CONFIG_IS_ENABLED(PINCTRL_FULL)
32/**
33 * pinctrl_config_one() - apply pinctrl settings for a single node
34 *
35 * @config: pin configuration node
36 * @return: 0 on success, or negative error code on failure
37 */
38static int pinctrl_config_one(struct udevice *config)
39{
40 struct udevice *pctldev;
41 const struct pinctrl_ops *ops;
42
43 pctldev = config;
44 for (;;) {
45 pctldev = dev_get_parent(pctldev);
46 if (!pctldev) {
47 dev_err(config, "could not find pctldev\n");
48 return -EINVAL;
49 }
50 if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
51 break;
52 }
53
54 ops = pinctrl_get_ops(pctldev);
55 return ops->set_state(pctldev, config);
56}
57
58/**
59 * pinctrl_select_state_full() - full implementation of pinctrl_select_state
60 *
61 * @dev: peripheral device
62 * @statename: state name, like "default"
63 * @return: 0 on success, or negative error code on failure
64 */
65static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
66{
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090067 char propname[32]; /* long enough */
68 const fdt32_t *list;
69 uint32_t phandle;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090070 struct udevice *config;
71 int state, size, i, ret;
72
Kever Yang1e656ad2018-02-09 10:56:24 +080073 state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090074 if (state < 0) {
75 char *end;
76 /*
77 * If statename is not found in "pinctrl-names",
78 * assume statename is just the integer state ID.
79 */
80 state = simple_strtoul(statename, &end, 10);
81 if (*end)
82 return -EINVAL;
83 }
84
85 snprintf(propname, sizeof(propname), "pinctrl-%d", state);
Kever Yang1e656ad2018-02-09 10:56:24 +080086 list = dev_read_prop(dev, propname, &size);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090087 if (!list)
88 return -EINVAL;
89
90 size /= sizeof(*list);
91 for (i = 0; i < size; i++) {
92 phandle = fdt32_to_cpu(*list++);
Kever Yang1e656ad2018-02-09 10:56:24 +080093 ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
94 &config);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +090095 if (ret)
96 return ret;
97
98 ret = pinctrl_config_one(config);
99 if (ret)
100 return ret;
101 }
102
103 return 0;
104}
105
106/**
Masahiro Yamadaf8357062016-08-19 18:26:54 +0900107 * pinconfig_post_bind() - post binding for PINCONFIG uclass
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900108 * Recursively bind its children as pinconfig devices.
109 *
110 * @dev: pinconfig device
111 * @return: 0 on success, or negative error code on failure
112 */
113static int pinconfig_post_bind(struct udevice *dev)
114{
Simon Glass5589a812015-12-29 05:22:52 -0700115 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900116 const char *name;
Simon Glass45a26862017-05-18 20:09:07 -0600117 ofnode node;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900118 int ret;
119
Simon Glass45a26862017-05-18 20:09:07 -0600120 dev_for_each_subnode(node, dev) {
Simon Glass5589a812015-12-29 05:22:52 -0700121 if (pre_reloc_only &&
Simon Glass45a26862017-05-18 20:09:07 -0600122 !ofnode_pre_reloc(node))
Simon Glass5589a812015-12-29 05:22:52 -0700123 continue;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900124 /*
125 * If this node has "compatible" property, this is not
126 * a pin configuration node, but a normal device. skip.
127 */
Masahiro Yamada61e51ba2017-06-22 16:54:05 +0900128 ofnode_get_property(node, "compatible", &ret);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900129 if (ret >= 0)
130 continue;
131
132 if (ret != -FDT_ERR_NOTFOUND)
133 return ret;
134
Simon Glass45a26862017-05-18 20:09:07 -0600135 name = ofnode_get_name(node);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900136 if (!name)
137 return -EINVAL;
138 ret = device_bind_driver_to_node(dev, "pinconfig", name,
Simon Glass45a26862017-05-18 20:09:07 -0600139 node, NULL);
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900140 if (ret)
141 return ret;
142 }
143
144 return 0;
145}
146
147UCLASS_DRIVER(pinconfig) = {
148 .id = UCLASS_PINCONFIG,
149 .post_bind = pinconfig_post_bind,
150 .name = "pinconfig",
151};
152
153U_BOOT_DRIVER(pinconfig_generic) = {
154 .name = "pinconfig",
155 .id = UCLASS_PINCONFIG,
156};
157
158#else
159static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
160{
161 return -ENODEV;
162}
163
164static int pinconfig_post_bind(struct udevice *dev)
165{
Masahiro Yamada8a5f6122015-09-06 01:44:50 +0900166 return 0;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900167}
168#endif
169
170/**
171 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
172 *
173 * @dev: peripheral device
174 * @return: 0 on success, or negative error code on failure
175 */
176static int pinctrl_select_state_simple(struct udevice *dev)
177{
178 struct udevice *pctldev;
179 struct pinctrl_ops *ops;
180 int ret;
181
182 /*
183 * For simplicity, assume the first device of PINCTRL uclass
184 * is the correct one. This is most likely OK as there is
185 * usually only one pinctrl device on the system.
186 */
187 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
188 if (ret)
189 return ret;
190
191 ops = pinctrl_get_ops(pctldev);
192 if (!ops->set_state_simple) {
193 dev_dbg(dev, "set_state_simple op missing\n");
194 return -ENOSYS;
195 }
196
197 return ops->set_state_simple(pctldev, dev);
198}
199
200int pinctrl_select_state(struct udevice *dev, const char *statename)
201{
202 /*
203 * Try full-implemented pinctrl first.
204 * If it fails or is not implemented, try simple one.
205 */
206 if (pinctrl_select_state_full(dev, statename))
207 return pinctrl_select_state_simple(dev);
208
209 return 0;
210}
211
Simon Glassc5acf4a2015-08-30 16:55:13 -0600212int pinctrl_request(struct udevice *dev, int func, int flags)
213{
214 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
215
216 if (!ops->request)
217 return -ENOSYS;
218
219 return ops->request(dev, func, flags);
220}
221
222int pinctrl_request_noflags(struct udevice *dev, int func)
223{
224 return pinctrl_request(dev, func, 0);
225}
226
227int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
228{
229 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
230
231 if (!ops->get_periph_id)
232 return -ENOSYS;
233
234 return ops->get_periph_id(dev, periph);
235}
236
Simon Glass77eaa192016-01-21 19:43:56 -0700237int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
238{
239 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
240
241 if (!ops->get_gpio_mux)
242 return -ENOSYS;
243
244 return ops->get_gpio_mux(dev, banknum, index);
245}
246
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900247/**
Masahiro Yamadaf8357062016-08-19 18:26:54 +0900248 * pinconfig_post_bind() - post binding for PINCTRL uclass
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900249 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
250 *
251 * @dev: pinctrl device
252 * @return: 0 on success, or negative error code on failure
253 */
254static int pinctrl_post_bind(struct udevice *dev)
255{
256 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
257
258 if (!ops) {
259 dev_dbg(dev, "ops is not set. Do not bind.\n");
260 return -EINVAL;
261 }
262
263 /*
Masahiro Yamada8a5f6122015-09-06 01:44:50 +0900264 * If set_state callback is set, we assume this pinctrl driver is the
265 * full implementation. In this case, its child nodes should be bound
266 * so that peripheral devices can easily search in parent devices
267 * during later DT-parsing.
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900268 */
Masahiro Yamada8a5f6122015-09-06 01:44:50 +0900269 if (ops->set_state)
270 return pinconfig_post_bind(dev);
271
272 return 0;
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900273}
274
275UCLASS_DRIVER(pinctrl) = {
276 .id = UCLASS_PINCTRL,
277 .post_bind = pinctrl_post_bind,
Thomas Abrahamac985272016-04-23 22:18:07 +0530278 .flags = DM_UC_FLAG_SEQ_ALIAS,
Masahiro Yamadad90a5a32015-08-27 12:44:29 +0900279 .name = "pinctrl",
280};