blob: f01bc77a5761275013fef13e6d3e5476dc93aded [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>
Masahiro Yamada75f82d02018-03-05 01:20:11 +09007#include <linux/libfdt.h>
Masahiro Yamadaf8efa632015-08-27 12:44:29 +09008#include <linux/err.h>
9#include <linux/list.h>
Simon Glass11c89f32017-05-17 17:18:03 -060010#include <dm.h>
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090011#include <dm/lists.h>
12#include <dm/pinctrl.h>
Heiko Stübner9a2cdca2017-02-18 19:46:21 +010013#include <dm/util.h>
Kever Yanga5897622018-02-09 10:56:24 +080014#include <dm/of_access.h>
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090015
16DECLARE_GLOBAL_DATA_PTR;
17
Simon Glassa0984472016-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 Yamadaf8efa632015-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 Yamadaf8efa632015-08-27 12:44:29 +090066 char propname[32]; /* long enough */
67 const fdt32_t *list;
68 uint32_t phandle;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090069 struct udevice *config;
70 int state, size, i, ret;
71
Kever Yanga5897622018-02-09 10:56:24 +080072 state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
Masahiro Yamadaf8efa632015-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 Yanga5897622018-02-09 10:56:24 +080085 list = dev_read_prop(dev, propname, &size);
Masahiro Yamadaf8efa632015-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 Yanga5897622018-02-09 10:56:24 +080092 ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
93 &config);
Masahiro Yamadaf8efa632015-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 Yamada0cbfe5c2016-08-19 18:26:54 +0900106 * pinconfig_post_bind() - post binding for PINCONFIG uclass
Masahiro Yamadaf8efa632015-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 Glass9cefa472015-12-29 05:22:52 -0700114 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900115 const char *name;
Simon Glasse6dd8da2017-05-18 20:09:07 -0600116 ofnode node;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900117 int ret;
118
Urja Rannikkocb484fc2019-03-22 19:14:33 +0000119 if (!dev_of_valid(dev))
120 return 0;
121
Simon Glasse6dd8da2017-05-18 20:09:07 -0600122 dev_for_each_subnode(node, dev) {
Lukasz Majewski6fdf2722019-01-09 23:05:02 +0100123 if (pre_reloc_only &&
124 !ofnode_pre_reloc(node))
Simon Glass9cefa472015-12-29 05:22:52 -0700125 continue;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900126 /*
127 * If this node has "compatible" property, this is not
128 * a pin configuration node, but a normal device. skip.
129 */
Masahiro Yamada9cf85cb2017-06-22 16:54:05 +0900130 ofnode_get_property(node, "compatible", &ret);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900131 if (ret >= 0)
132 continue;
Patrick Delaunay1408ab32019-02-25 13:39:56 +0100133 /* If this node has "gpio-controller" property, skip */
134 if (ofnode_read_bool(node, "gpio-controller"))
135 continue;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900136
137 if (ret != -FDT_ERR_NOTFOUND)
138 return ret;
139
Simon Glasse6dd8da2017-05-18 20:09:07 -0600140 name = ofnode_get_name(node);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900141 if (!name)
142 return -EINVAL;
143 ret = device_bind_driver_to_node(dev, "pinconfig", name,
Simon Glasse6dd8da2017-05-18 20:09:07 -0600144 node, NULL);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900145 if (ret)
146 return ret;
147 }
148
149 return 0;
150}
151
152UCLASS_DRIVER(pinconfig) = {
153 .id = UCLASS_PINCONFIG,
154 .post_bind = pinconfig_post_bind,
155 .name = "pinconfig",
156};
157
158U_BOOT_DRIVER(pinconfig_generic) = {
159 .name = "pinconfig",
160 .id = UCLASS_PINCONFIG,
161};
162
163#else
164static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
165{
166 return -ENODEV;
167}
168
169static int pinconfig_post_bind(struct udevice *dev)
170{
Masahiro Yamada7778b032015-09-06 01:44:50 +0900171 return 0;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900172}
173#endif
174
175/**
176 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
177 *
178 * @dev: peripheral device
179 * @return: 0 on success, or negative error code on failure
180 */
181static int pinctrl_select_state_simple(struct udevice *dev)
182{
183 struct udevice *pctldev;
184 struct pinctrl_ops *ops;
185 int ret;
186
187 /*
Patrice Chotard74110632019-02-25 13:39:55 +0100188 * For most system, there is only one pincontroller device. But in
189 * case of multiple pincontroller devices, probe the one with sequence
190 * number 0 (defined by alias) to avoid race condition.
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900191 */
Patrice Chotard74110632019-02-25 13:39:55 +0100192 ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev);
193 if (ret)
194 /* if not found, get the first one */
195 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900196 if (ret)
197 return ret;
198
199 ops = pinctrl_get_ops(pctldev);
200 if (!ops->set_state_simple) {
201 dev_dbg(dev, "set_state_simple op missing\n");
202 return -ENOSYS;
203 }
204
205 return ops->set_state_simple(pctldev, dev);
206}
207
208int pinctrl_select_state(struct udevice *dev, const char *statename)
209{
210 /*
Kever Yang16ed9712018-04-18 17:54:04 +0800211 * Some device which is logical like mmc.blk, do not have
212 * a valid ofnode.
213 */
214 if (!ofnode_valid(dev->node))
215 return 0;
216 /*
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900217 * Try full-implemented pinctrl first.
218 * If it fails or is not implemented, try simple one.
219 */
220 if (pinctrl_select_state_full(dev, statename))
221 return pinctrl_select_state_simple(dev);
222
223 return 0;
224}
225
Simon Glassac2ee652015-08-30 16:55:13 -0600226int pinctrl_request(struct udevice *dev, int func, int flags)
227{
228 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
229
230 if (!ops->request)
231 return -ENOSYS;
232
233 return ops->request(dev, func, flags);
234}
235
236int pinctrl_request_noflags(struct udevice *dev, int func)
237{
238 return pinctrl_request(dev, func, 0);
239}
240
241int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
242{
243 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
244
245 if (!ops->get_periph_id)
246 return -ENOSYS;
247
248 return ops->get_periph_id(dev, periph);
249}
250
Simon Glass2d4fa3c2016-01-21 19:43:56 -0700251int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
252{
253 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
254
255 if (!ops->get_gpio_mux)
256 return -ENOSYS;
257
258 return ops->get_gpio_mux(dev, banknum, index);
259}
260
Patrice Chotardfbf80822018-10-24 14:10:14 +0200261int pinctrl_get_pins_count(struct udevice *dev)
262{
263 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
264
265 if (!ops->get_pins_count)
266 return -ENOSYS;
267
268 return ops->get_pins_count(dev);
269}
270
271int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
272 int size)
273{
274 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
275
276 if (!ops->get_pin_name)
277 return -ENOSYS;
278
279 snprintf(buf, size, ops->get_pin_name(dev, selector));
280
281 return 0;
282}
283
Patrice Chotard74ba3f22018-10-24 14:10:13 +0200284int pinctrl_get_pin_muxing(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_muxing)
290 return -ENOSYS;
291
292 return ops->get_pin_muxing(dev, selector, buf, size);
293}
294
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900295/**
Masahiro Yamada0cbfe5c2016-08-19 18:26:54 +0900296 * pinconfig_post_bind() - post binding for PINCTRL uclass
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900297 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
298 *
299 * @dev: pinctrl device
300 * @return: 0 on success, or negative error code on failure
301 */
302static int pinctrl_post_bind(struct udevice *dev)
303{
304 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
305
306 if (!ops) {
307 dev_dbg(dev, "ops is not set. Do not bind.\n");
308 return -EINVAL;
309 }
310
311 /*
Masahiro Yamada7778b032015-09-06 01:44:50 +0900312 * If set_state callback is set, we assume this pinctrl driver is the
313 * full implementation. In this case, its child nodes should be bound
314 * so that peripheral devices can easily search in parent devices
315 * during later DT-parsing.
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900316 */
Masahiro Yamada7778b032015-09-06 01:44:50 +0900317 if (ops->set_state)
318 return pinconfig_post_bind(dev);
319
320 return 0;
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900321}
322
323UCLASS_DRIVER(pinctrl) = {
324 .id = UCLASS_PINCTRL,
325 .post_bind = pinctrl_post_bind,
Thomas Abraham87adc642016-04-23 22:18:07 +0530326 .flags = DM_UC_FLAG_SEQ_ALIAS,
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900327 .name = "pinctrl",
328};