]> git.ipfire.org Git - people/ms/u-boot.git/blame - drivers/pinctrl/pinctrl-uclass.c
dm: core: Replace of_offset with accessor
[people/ms/u-boot.git] / drivers / pinctrl / pinctrl-uclass.c
CommitLineData
d90a5a30
MY
1/*
2 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
8#include <libfdt.h>
9#include <linux/err.h>
10#include <linux/list.h>
11#include <dm/device.h>
12#include <dm/lists.h>
13#include <dm/pinctrl.h>
14#include <dm/uclass.h>
15
16DECLARE_GLOBAL_DATA_PTR;
17
52db39a2
SG
18int 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
d90a5a30
MY
30#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{
66 const void *fdt = gd->fdt_blob;
e160f7d4 67 int node = dev_of_offset(dev);
d90a5a30
MY
68 char propname[32]; /* long enough */
69 const fdt32_t *list;
70 uint32_t phandle;
71 int config_node;
72 struct udevice *config;
73 int state, size, i, ret;
74
b02e4044 75 state = fdt_stringlist_search(fdt, node, "pinctrl-names", statename);
d90a5a30
MY
76 if (state < 0) {
77 char *end;
78 /*
79 * If statename is not found in "pinctrl-names",
80 * assume statename is just the integer state ID.
81 */
82 state = simple_strtoul(statename, &end, 10);
83 if (*end)
84 return -EINVAL;
85 }
86
87 snprintf(propname, sizeof(propname), "pinctrl-%d", state);
88 list = fdt_getprop(fdt, node, propname, &size);
89 if (!list)
90 return -EINVAL;
91
92 size /= sizeof(*list);
93 for (i = 0; i < size; i++) {
94 phandle = fdt32_to_cpu(*list++);
95
96 config_node = fdt_node_offset_by_phandle(fdt, phandle);
97 if (config_node < 0) {
98 dev_err(dev, "prop %s index %d invalid phandle\n",
99 propname, i);
100 return -EINVAL;
101 }
102 ret = uclass_get_device_by_of_offset(UCLASS_PINCONFIG,
103 config_node, &config);
104 if (ret)
105 return ret;
106
107 ret = pinctrl_config_one(config);
108 if (ret)
109 return ret;
110 }
111
112 return 0;
113}
114
115/**
f835706c 116 * pinconfig_post_bind() - post binding for PINCONFIG uclass
d90a5a30
MY
117 * Recursively bind its children as pinconfig devices.
118 *
119 * @dev: pinconfig device
120 * @return: 0 on success, or negative error code on failure
121 */
122static int pinconfig_post_bind(struct udevice *dev)
123{
124 const void *fdt = gd->fdt_blob;
e160f7d4 125 int offset = dev_of_offset(dev);
5589a818 126 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
d90a5a30
MY
127 const char *name;
128 int ret;
129
130 for (offset = fdt_first_subnode(fdt, offset);
131 offset > 0;
132 offset = fdt_next_subnode(fdt, offset)) {
5589a818
SG
133 if (pre_reloc_only &&
134 !fdt_getprop(fdt, offset, "u-boot,dm-pre-reloc", NULL))
135 continue;
d90a5a30
MY
136 /*
137 * If this node has "compatible" property, this is not
138 * a pin configuration node, but a normal device. skip.
139 */
140 fdt_get_property(fdt, offset, "compatible", &ret);
141 if (ret >= 0)
142 continue;
143
144 if (ret != -FDT_ERR_NOTFOUND)
145 return ret;
146
147 name = fdt_get_name(fdt, offset, NULL);
148 if (!name)
149 return -EINVAL;
150 ret = device_bind_driver_to_node(dev, "pinconfig", name,
151 offset, NULL);
152 if (ret)
153 return ret;
154 }
155
156 return 0;
157}
158
159UCLASS_DRIVER(pinconfig) = {
160 .id = UCLASS_PINCONFIG,
161 .post_bind = pinconfig_post_bind,
162 .name = "pinconfig",
163};
164
165U_BOOT_DRIVER(pinconfig_generic) = {
166 .name = "pinconfig",
167 .id = UCLASS_PINCONFIG,
168};
169
170#else
171static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
172{
173 return -ENODEV;
174}
175
176static int pinconfig_post_bind(struct udevice *dev)
177{
8a5f6129 178 return 0;
d90a5a30
MY
179}
180#endif
181
182/**
183 * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
184 *
185 * @dev: peripheral device
186 * @return: 0 on success, or negative error code on failure
187 */
188static int pinctrl_select_state_simple(struct udevice *dev)
189{
190 struct udevice *pctldev;
191 struct pinctrl_ops *ops;
192 int ret;
193
194 /*
195 * For simplicity, assume the first device of PINCTRL uclass
196 * is the correct one. This is most likely OK as there is
197 * usually only one pinctrl device on the system.
198 */
199 ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
200 if (ret)
201 return ret;
202
203 ops = pinctrl_get_ops(pctldev);
204 if (!ops->set_state_simple) {
205 dev_dbg(dev, "set_state_simple op missing\n");
206 return -ENOSYS;
207 }
208
209 return ops->set_state_simple(pctldev, dev);
210}
211
212int pinctrl_select_state(struct udevice *dev, const char *statename)
213{
214 /*
215 * Try full-implemented pinctrl first.
216 * If it fails or is not implemented, try simple one.
217 */
218 if (pinctrl_select_state_full(dev, statename))
219 return pinctrl_select_state_simple(dev);
220
221 return 0;
222}
223
c5acf4a2
SG
224int pinctrl_request(struct udevice *dev, int func, int flags)
225{
226 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
227
228 if (!ops->request)
229 return -ENOSYS;
230
231 return ops->request(dev, func, flags);
232}
233
234int pinctrl_request_noflags(struct udevice *dev, int func)
235{
236 return pinctrl_request(dev, func, 0);
237}
238
239int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
240{
241 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
242
243 if (!ops->get_periph_id)
244 return -ENOSYS;
245
246 return ops->get_periph_id(dev, periph);
247}
248
77eaa19e
SG
249int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
250{
251 struct pinctrl_ops *ops = pinctrl_get_ops(dev);
252
253 if (!ops->get_gpio_mux)
254 return -ENOSYS;
255
256 return ops->get_gpio_mux(dev, banknum, index);
257}
258
d90a5a30 259/**
f835706c 260 * pinconfig_post_bind() - post binding for PINCTRL uclass
d90a5a30
MY
261 * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
262 *
263 * @dev: pinctrl device
264 * @return: 0 on success, or negative error code on failure
265 */
266static int pinctrl_post_bind(struct udevice *dev)
267{
268 const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
269
270 if (!ops) {
271 dev_dbg(dev, "ops is not set. Do not bind.\n");
272 return -EINVAL;
273 }
274
275 /*
8a5f6129
MY
276 * If set_state callback is set, we assume this pinctrl driver is the
277 * full implementation. In this case, its child nodes should be bound
278 * so that peripheral devices can easily search in parent devices
279 * during later DT-parsing.
d90a5a30 280 */
8a5f6129
MY
281 if (ops->set_state)
282 return pinconfig_post_bind(dev);
283
284 return 0;
d90a5a30
MY
285}
286
287UCLASS_DRIVER(pinctrl) = {
288 .id = UCLASS_PINCTRL,
289 .post_bind = pinctrl_post_bind,
ac985273 290 .flags = DM_UC_FLAG_SEQ_ALIAS,
d90a5a30
MY
291 .name = "pinctrl",
292};