xref: /rk3399_rockchip-uboot/drivers/pinctrl/pinctrl-uclass.c (revision 6b9191d0013c06e5b30cc43eb89b40f854b03cc7)
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 	if (!dev_of_valid(dev))
127 		return 0;
128 
129 	dev_for_each_subnode(node, dev) {
130 		if (pre_reloc_only &&
131 		    !ofnode_pre_reloc(node))
132 			continue;
133 		/*
134 		 * If this node has "compatible" property, this is not
135 		 * a pin configuration node, but a normal device. skip.
136 		 */
137 		ofnode_get_property(node, "compatible", &ret);
138 		if (ret >= 0)
139 			continue;
140 
141 		if (ret != -FDT_ERR_NOTFOUND)
142 			return ret;
143 
144 		name = ofnode_get_name(node);
145 		if (!name)
146 			return -EINVAL;
147 		ret = device_bind_driver_to_node(dev, "pinconfig", name,
148 						 node, NULL);
149 		if (ret)
150 			return ret;
151 	}
152 
153 	return 0;
154 }
155 
156 UCLASS_DRIVER(pinconfig) = {
157 	.id = UCLASS_PINCONFIG,
158 	.post_bind = pinconfig_post_bind,
159 	.name = "pinconfig",
160 };
161 
162 U_BOOT_DRIVER(pinconfig_generic) = {
163 	.name = "pinconfig",
164 	.id = UCLASS_PINCONFIG,
165 };
166 
167 #else
168 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
169 {
170 	return -ENODEV;
171 }
172 
173 static int pinconfig_post_bind(struct udevice *dev)
174 {
175 	return 0;
176 }
177 #endif
178 
179 /**
180  * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
181  *
182  * @dev: peripheral device
183  * @return: 0 on success, or negative error code on failure
184  */
185 static int pinctrl_select_state_simple(struct udevice *dev)
186 {
187 	struct udevice *pctldev;
188 	struct pinctrl_ops *ops;
189 	int ret;
190 
191 	/*
192 	 * For most system, there is only one pincontroller device. But in
193 	 * case of multiple pincontroller devices, probe the one with sequence
194 	 * number 0 (defined by alias) to avoid race condition.
195 	 */
196 	ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev);
197 	if (ret)
198 		/* if not found, get the first one */
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 
212 int pinctrl_select_state(struct udevice *dev, const char *statename)
213 {
214 	/*
215 	 * Some device which is logical like mmc.blk, do not have
216 	 * a valid ofnode.
217 	 */
218 	if (!ofnode_valid(dev->node))
219 		return 0;
220 	/*
221 	 * Try full-implemented pinctrl first.
222 	 * If it fails or is not implemented, try simple one.
223 	 */
224 	if (pinctrl_select_state_full(dev, statename))
225 		return pinctrl_select_state_simple(dev);
226 
227 	return 0;
228 }
229 
230 int pinctrl_request(struct udevice *dev, int func, int flags)
231 {
232 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
233 
234 	if (!ops->request)
235 		return -ENOSYS;
236 
237 	return ops->request(dev, func, flags);
238 }
239 
240 int pinctrl_request_noflags(struct udevice *dev, int func)
241 {
242 	return pinctrl_request(dev, func, 0);
243 }
244 
245 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
246 {
247 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
248 
249 	if (!ops->get_periph_id)
250 		return -ENOSYS;
251 
252 	return ops->get_periph_id(dev, periph);
253 }
254 
255 int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
256 {
257 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
258 
259 	if (!ops->get_gpio_mux)
260 		return -ENOSYS;
261 
262 	return ops->get_gpio_mux(dev, banknum, index);
263 }
264 
265 int pinctrl_get_pins_count(struct udevice *dev)
266 {
267 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
268 
269 	if (!ops->get_pins_count)
270 		return -ENOSYS;
271 
272 	return ops->get_pins_count(dev);
273 }
274 
275 int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
276 			 int size)
277 {
278 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
279 
280 	if (!ops->get_pin_name)
281 		return -ENOSYS;
282 
283 	snprintf(buf, size, ops->get_pin_name(dev, selector));
284 
285 	return 0;
286 }
287 
288 int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf,
289 			   int size)
290 {
291 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
292 
293 	if (!ops->get_pin_muxing)
294 		return -ENOSYS;
295 
296 	return ops->get_pin_muxing(dev, selector, buf, size);
297 }
298 
299 /**
300  * pinconfig_post_bind() - post binding for PINCTRL uclass
301  * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
302  *
303  * @dev: pinctrl device
304  * @return: 0 on success, or negative error code on failure
305  */
306 static int pinctrl_post_bind(struct udevice *dev)
307 {
308 	const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
309 
310 	if (!ops) {
311 		dev_dbg(dev, "ops is not set.  Do not bind.\n");
312 		return -EINVAL;
313 	}
314 
315 	/*
316 	 * If set_state callback is set, we assume this pinctrl driver is the
317 	 * full implementation.  In this case, its child nodes should be bound
318 	 * so that peripheral devices can easily search in parent devices
319 	 * during later DT-parsing.
320 	 */
321 	if (ops->set_state)
322 		return pinconfig_post_bind(dev);
323 
324 	return 0;
325 }
326 
327 UCLASS_DRIVER(pinctrl) = {
328 	.id = UCLASS_PINCTRL,
329 	.post_bind = pinctrl_post_bind,
330 	.flags = DM_UC_FLAG_SEQ_ALIAS,
331 	.name = "pinctrl",
332 };
333