xref: /rk3399_rockchip-uboot/drivers/pinctrl/pinctrl-uclass.c (revision 5298c171dcfc929392b9d8701be72fe66e57df48)
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 <linux/libfdt.h>
9 #include <linux/err.h>
10 #include <linux/list.h>
11 #include <dm.h>
12 #include <dm/lists.h>
13 #include <dm/pinctrl.h>
14 #include <dm/util.h>
15 #include <dm/of_access.h>
16 
17 DECLARE_GLOBAL_DATA_PTR;
18 
19 int 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 
31 #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  */
38 static 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  */
65 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
66 {
67 	char propname[32]; /* long enough */
68 	const fdt32_t *list;
69 	uint32_t phandle;
70 	struct udevice *config;
71 	int state, size, i, ret;
72 
73 	state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
74 	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);
86 	list = dev_read_prop(dev, propname, &size);
87 	if (!list)
88 		return -EINVAL;
89 
90 	size /= sizeof(*list);
91 	for (i = 0; i < size; i++) {
92 		phandle = fdt32_to_cpu(*list++);
93 		ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
94 						      &config);
95 		if (ret) {
96 			dev_warn(dev, "%s: uclass_get_device_by_phandle_id: err=%d\n",
97 				__func__, ret);
98 			continue;
99 		}
100 
101 		ret = pinctrl_config_one(config);
102 		if (ret) {
103 			dev_warn(dev, "%s: pinctrl_config_one: err=%d\n",
104 				__func__, ret);
105 			continue;
106 		}
107 	}
108 
109 	return 0;
110 }
111 
112 /**
113  * pinconfig_post_bind() - post binding for PINCONFIG uclass
114  * Recursively bind its children as pinconfig devices.
115  *
116  * @dev: pinconfig device
117  * @return: 0 on success, or negative error code on failure
118  */
119 static int pinconfig_post_bind(struct udevice *dev)
120 {
121 	bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
122 	const char *name;
123 	ofnode node;
124 	int ret;
125 
126 	dev_for_each_subnode(node, dev) {
127 		if (pre_reloc_only &&
128 		    !ofnode_pre_reloc(node))
129 			continue;
130 		/*
131 		 * If this node has "compatible" property, this is not
132 		 * a pin configuration node, but a normal device. skip.
133 		 */
134 		ofnode_get_property(node, "compatible", &ret);
135 		if (ret >= 0)
136 			continue;
137 
138 		if (ret != -FDT_ERR_NOTFOUND)
139 			return ret;
140 
141 		name = ofnode_get_name(node);
142 		if (!name)
143 			return -EINVAL;
144 		ret = device_bind_driver_to_node(dev, "pinconfig", name,
145 						 node, NULL);
146 		if (ret)
147 			return ret;
148 	}
149 
150 	return 0;
151 }
152 
153 UCLASS_DRIVER(pinconfig) = {
154 	.id = UCLASS_PINCONFIG,
155 	.post_bind = pinconfig_post_bind,
156 	.name = "pinconfig",
157 };
158 
159 U_BOOT_DRIVER(pinconfig_generic) = {
160 	.name = "pinconfig",
161 	.id = UCLASS_PINCONFIG,
162 };
163 
164 #else
165 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
166 {
167 	return -ENODEV;
168 }
169 
170 static int pinconfig_post_bind(struct udevice *dev)
171 {
172 	return 0;
173 }
174 #endif
175 
176 /**
177  * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
178  *
179  * @dev: peripheral device
180  * @return: 0 on success, or negative error code on failure
181  */
182 static int pinctrl_select_state_simple(struct udevice *dev)
183 {
184 	struct udevice *pctldev;
185 	struct pinctrl_ops *ops;
186 	int ret;
187 
188 	/*
189 	 * For most system, there is only one pincontroller device. But in
190 	 * case of multiple pincontroller devices, probe the one with sequence
191 	 * number 0 (defined by alias) to avoid race condition.
192 	 */
193 	ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev);
194 	if (ret)
195 		/* if not found, get the first one */
196 		ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
197 	if (ret)
198 		return ret;
199 
200 	ops = pinctrl_get_ops(pctldev);
201 	if (!ops->set_state_simple) {
202 		dev_dbg(dev, "set_state_simple op missing\n");
203 		return -ENOSYS;
204 	}
205 
206 	return ops->set_state_simple(pctldev, dev);
207 }
208 
209 int pinctrl_select_state(struct udevice *dev, const char *statename)
210 {
211 	/*
212 	 * Some device which is logical like mmc.blk, do not have
213 	 * a valid ofnode.
214 	 */
215 	if (!ofnode_valid(dev->node))
216 		return 0;
217 	/*
218 	 * Try full-implemented pinctrl first.
219 	 * If it fails or is not implemented, try simple one.
220 	 */
221 	if (pinctrl_select_state_full(dev, statename))
222 		return pinctrl_select_state_simple(dev);
223 
224 	return 0;
225 }
226 
227 int pinctrl_request(struct udevice *dev, int func, int flags)
228 {
229 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
230 
231 	if (!ops->request)
232 		return -ENOSYS;
233 
234 	return ops->request(dev, func, flags);
235 }
236 
237 int pinctrl_request_noflags(struct udevice *dev, int func)
238 {
239 	return pinctrl_request(dev, func, 0);
240 }
241 
242 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
243 {
244 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
245 
246 	if (!ops->get_periph_id)
247 		return -ENOSYS;
248 
249 	return ops->get_periph_id(dev, periph);
250 }
251 
252 int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
253 {
254 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
255 
256 	if (!ops->get_gpio_mux)
257 		return -ENOSYS;
258 
259 	return ops->get_gpio_mux(dev, banknum, index);
260 }
261 
262 int pinctrl_get_pins_count(struct udevice *dev)
263 {
264 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
265 
266 	if (!ops->get_pins_count)
267 		return -ENOSYS;
268 
269 	return ops->get_pins_count(dev);
270 }
271 
272 /**
273  * pinconfig_post_bind() - post binding for PINCTRL uclass
274  * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
275  *
276  * @dev: pinctrl device
277  * @return: 0 on success, or negative error code on failure
278  */
279 static int pinctrl_post_bind(struct udevice *dev)
280 {
281 	const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
282 
283 	if (!ops) {
284 		dev_dbg(dev, "ops is not set.  Do not bind.\n");
285 		return -EINVAL;
286 	}
287 
288 	/*
289 	 * If set_state callback is set, we assume this pinctrl driver is the
290 	 * full implementation.  In this case, its child nodes should be bound
291 	 * so that peripheral devices can easily search in parent devices
292 	 * during later DT-parsing.
293 	 */
294 	if (ops->set_state)
295 		return pinconfig_post_bind(dev);
296 
297 	return 0;
298 }
299 
300 UCLASS_DRIVER(pinctrl) = {
301 	.id = UCLASS_PINCTRL,
302 	.post_bind = pinctrl_post_bind,
303 	.flags = DM_UC_FLAG_SEQ_ALIAS,
304 	.name = "pinctrl",
305 };
306