• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015  Masahiro Yamada <yamada.masahiro@socionext.com>
4  */
5 
6 #include <common.h>
7 #include <linux/libfdt.h>
8 #include <linux/err.h>
9 #include <linux/list.h>
10 #include <dm.h>
11 #include <dm/lists.h>
12 #include <dm/pinctrl.h>
13 #include <dm/util.h>
14 #include <dm/of_access.h>
15 
16 DECLARE_GLOBAL_DATA_PTR;
17 
18 #if CONFIG_IS_ENABLED(PINCTRL_FULL)
19 /**
20  * pinctrl_config_one() - apply pinctrl settings for a single node
21  *
22  * @config: pin configuration node
23  * @return: 0 on success, or negative error code on failure
24  */
pinctrl_config_one(struct udevice * config)25 static int pinctrl_config_one(struct udevice *config)
26 {
27 	struct udevice *pctldev;
28 	const struct pinctrl_ops *ops;
29 
30 	pctldev = config;
31 	for (;;) {
32 		pctldev = dev_get_parent(pctldev);
33 		if (!pctldev) {
34 			dev_err(config, "could not find pctldev\n");
35 			return -EINVAL;
36 		}
37 		if (pctldev->uclass->uc_drv->id == UCLASS_PINCTRL)
38 			break;
39 	}
40 
41 	ops = pinctrl_get_ops(pctldev);
42 	return ops->set_state(pctldev, config);
43 }
44 
45 /**
46  * pinctrl_select_state_full() - full implementation of pinctrl_select_state
47  *
48  * @dev: peripheral device
49  * @statename: state name, like "default"
50  * @return: 0 on success, or negative error code on failure
51  */
pinctrl_select_state_full(struct udevice * dev,const char * statename)52 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
53 {
54 	char propname[32]; /* long enough */
55 	const fdt32_t *list;
56 	uint32_t phandle;
57 	struct udevice *config;
58 	int state, size, i, ret;
59 
60 	state = dev_read_stringlist_search(dev, "pinctrl-names", statename);
61 	if (state < 0) {
62 		char *end;
63 		/*
64 		 * If statename is not found in "pinctrl-names",
65 		 * assume statename is just the integer state ID.
66 		 */
67 		state = simple_strtoul(statename, &end, 10);
68 		if (*end)
69 			return -EINVAL;
70 	}
71 
72 	snprintf(propname, sizeof(propname), "pinctrl-%d", state);
73 	list = dev_read_prop(dev, propname, &size);
74 	if (!list)
75 		return -EINVAL;
76 
77 	size /= sizeof(*list);
78 	for (i = 0; i < size; i++) {
79 		phandle = fdt32_to_cpu(*list++);
80 		ret = uclass_get_device_by_phandle_id(UCLASS_PINCONFIG, phandle,
81 						      &config);
82 		if (ret) {
83 			dev_warn(dev, "%s: uclass_get_device_by_phandle_id: err=%d\n",
84 				__func__, ret);
85 			continue;
86 		}
87 
88 		ret = pinctrl_config_one(config);
89 		if (ret) {
90 			dev_warn(dev, "%s: pinctrl_config_one: err=%d\n",
91 				__func__, ret);
92 			continue;
93 		}
94 	}
95 
96 	return 0;
97 }
98 
99 /**
100  * pinconfig_post_bind() - post binding for PINCONFIG uclass
101  * Recursively bind its children as pinconfig devices.
102  *
103  * @dev: pinconfig device
104  * @return: 0 on success, or negative error code on failure
105  */
pinconfig_post_bind(struct udevice * dev)106 static int pinconfig_post_bind(struct udevice *dev)
107 {
108 	bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
109 	const char *name;
110 	ofnode node;
111 	int ret;
112 
113 	if (!dev_of_valid(dev))
114 		return 0;
115 
116 	dev_for_each_subnode(node, dev) {
117 		if (pre_reloc_only &&
118 		    !ofnode_pre_reloc(node))
119 			continue;
120 		/*
121 		 * If this node has "compatible" property, this is not
122 		 * a pin configuration node, but a normal device. skip.
123 		 */
124 		ofnode_get_property(node, "compatible", &ret);
125 		if (ret >= 0)
126 			continue;
127 		/* If this node has "gpio-controller" property, skip */
128 		if (ofnode_read_bool(node, "gpio-controller"))
129 			continue;
130 
131 		if (ret != -FDT_ERR_NOTFOUND)
132 			return ret;
133 
134 		name = ofnode_get_name(node);
135 		if (!name)
136 			return -EINVAL;
137 		ret = device_bind_driver_to_node(dev, "pinconfig", name,
138 						 node, NULL);
139 		if (ret)
140 			return ret;
141 	}
142 
143 	return 0;
144 }
145 
146 UCLASS_DRIVER(pinconfig) = {
147 	.id = UCLASS_PINCONFIG,
148 #if CONFIG_IS_ENABLED(PINCONF_RECURSIVE)
149 	.post_bind = pinconfig_post_bind,
150 #endif
151 	.name = "pinconfig",
152 };
153 
154 U_BOOT_DRIVER(pinconfig_generic) = {
155 	.name = "pinconfig",
156 	.id = UCLASS_PINCONFIG,
157 };
158 
159 #else
pinctrl_select_state_full(struct udevice * dev,const char * statename)160 static int pinctrl_select_state_full(struct udevice *dev, const char *statename)
161 {
162 	return -ENODEV;
163 }
164 
pinconfig_post_bind(struct udevice * dev)165 static int pinconfig_post_bind(struct udevice *dev)
166 {
167 	return 0;
168 }
169 #endif
170 
171 static int
pinctrl_gpio_get_pinctrl_and_offset(struct udevice * dev,unsigned offset,struct udevice ** pctldev,unsigned int * pin_selector)172 pinctrl_gpio_get_pinctrl_and_offset(struct udevice *dev, unsigned offset,
173 				    struct udevice **pctldev,
174 				    unsigned int *pin_selector)
175 {
176 	struct ofnode_phandle_args args;
177 	unsigned gpio_offset, pfc_base, pfc_pins;
178 	int ret;
179 
180 	ret = dev_read_phandle_with_args(dev, "gpio-ranges", NULL, 3,
181 					 0, &args);
182 	if (ret) {
183 		dev_dbg(dev, "%s: dev_read_phandle_with_args: err=%d\n",
184 			__func__, ret);
185 		return ret;
186 	}
187 
188 	ret = uclass_get_device_by_ofnode(UCLASS_PINCTRL,
189 					  args.node, pctldev);
190 	if (ret) {
191 		dev_dbg(dev,
192 			"%s: uclass_get_device_by_of_offset failed: err=%d\n",
193 			__func__, ret);
194 		return ret;
195 	}
196 
197 	gpio_offset = args.args[0];
198 	pfc_base = args.args[1];
199 	pfc_pins = args.args[2];
200 
201 	if (offset < gpio_offset || offset > gpio_offset + pfc_pins) {
202 		dev_dbg(dev,
203 			"%s: GPIO can not be mapped to pincontrol pin\n",
204 			__func__);
205 		return -EINVAL;
206 	}
207 
208 	offset -= gpio_offset;
209 	offset += pfc_base;
210 	*pin_selector = offset;
211 
212 	return 0;
213 }
214 
215 /**
216  * pinctrl_gpio_request() - request a single pin to be used as GPIO
217  *
218  * @dev: GPIO peripheral device
219  * @offset: the GPIO pin offset from the GPIO controller
220  * @return: 0 on success, or negative error code on failure
221  */
pinctrl_gpio_request(struct udevice * dev,unsigned offset)222 int pinctrl_gpio_request(struct udevice *dev, unsigned offset)
223 {
224 	const struct pinctrl_ops *ops;
225 	struct udevice *pctldev;
226 	unsigned int pin_selector;
227 	int ret;
228 
229 	ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
230 						  &pctldev, &pin_selector);
231 	if (ret)
232 		return ret;
233 
234 	ops = pinctrl_get_ops(pctldev);
235 	if (!ops || !ops->gpio_request_enable)
236 		return -ENOTSUPP;
237 
238 	return ops->gpio_request_enable(pctldev, pin_selector);
239 }
240 
241 /**
242  * pinctrl_gpio_free() - free a single pin used as GPIO
243  *
244  * @dev: GPIO peripheral device
245  * @offset: the GPIO pin offset from the GPIO controller
246  * @return: 0 on success, or negative error code on failure
247  */
pinctrl_gpio_free(struct udevice * dev,unsigned offset)248 int pinctrl_gpio_free(struct udevice *dev, unsigned offset)
249 {
250 	const struct pinctrl_ops *ops;
251 	struct udevice *pctldev;
252 	unsigned int pin_selector;
253 	int ret;
254 
255 	ret = pinctrl_gpio_get_pinctrl_and_offset(dev, offset,
256 						  &pctldev, &pin_selector);
257 	if (ret)
258 		return ret;
259 
260 	ops = pinctrl_get_ops(pctldev);
261 	if (!ops || !ops->gpio_disable_free)
262 		return -ENOTSUPP;
263 
264 	return ops->gpio_disable_free(pctldev, pin_selector);
265 }
266 
267 /**
268  * pinctrl_select_state_simple() - simple implementation of pinctrl_select_state
269  *
270  * @dev: peripheral device
271  * @return: 0 on success, or negative error code on failure
272  */
pinctrl_select_state_simple(struct udevice * dev)273 static int pinctrl_select_state_simple(struct udevice *dev)
274 {
275 	struct udevice *pctldev;
276 	struct pinctrl_ops *ops;
277 	int ret;
278 
279 	/*
280 	 * For most system, there is only one pincontroller device. But in
281 	 * case of multiple pincontroller devices, probe the one with sequence
282 	 * number 0 (defined by alias) to avoid race condition.
283 	 */
284 	ret = uclass_get_device_by_seq(UCLASS_PINCTRL, 0, &pctldev);
285 	if (ret)
286 		/* if not found, get the first one */
287 		ret = uclass_get_device(UCLASS_PINCTRL, 0, &pctldev);
288 	if (ret)
289 		return ret;
290 
291 	ops = pinctrl_get_ops(pctldev);
292 	if (!ops->set_state_simple) {
293 		dev_dbg(dev, "set_state_simple op missing\n");
294 		return -ENOSYS;
295 	}
296 
297 	return ops->set_state_simple(pctldev, dev);
298 }
299 
pinctrl_select_state(struct udevice * dev,const char * statename)300 int pinctrl_select_state(struct udevice *dev, const char *statename)
301 {
302 	/*
303 	 * Some device which is logical like mmc.blk, do not have
304 	 * a valid ofnode.
305 	 */
306 	if (!ofnode_valid(dev->node))
307 		return 0;
308 	/*
309 	 * Try full-implemented pinctrl first.
310 	 * If it fails or is not implemented, try simple one.
311 	 */
312 	if (pinctrl_select_state_full(dev, statename))
313 		return pinctrl_select_state_simple(dev);
314 
315 	return 0;
316 }
317 
pinctrl_request(struct udevice * dev,int func,int flags)318 int pinctrl_request(struct udevice *dev, int func, int flags)
319 {
320 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
321 
322 	if (!ops->request)
323 		return -ENOSYS;
324 
325 	return ops->request(dev, func, flags);
326 }
327 
pinctrl_request_noflags(struct udevice * dev,int func)328 int pinctrl_request_noflags(struct udevice *dev, int func)
329 {
330 	return pinctrl_request(dev, func, 0);
331 }
332 
pinctrl_get_periph_id(struct udevice * dev,struct udevice * periph)333 int pinctrl_get_periph_id(struct udevice *dev, struct udevice *periph)
334 {
335 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
336 
337 	if (!ops->get_periph_id)
338 		return -ENOSYS;
339 
340 	return ops->get_periph_id(dev, periph);
341 }
342 
pinctrl_get_gpio_mux(struct udevice * dev,int banknum,int index)343 int pinctrl_get_gpio_mux(struct udevice *dev, int banknum, int index)
344 {
345 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
346 
347 	if (!ops->get_gpio_mux)
348 		return -ENOSYS;
349 
350 	return ops->get_gpio_mux(dev, banknum, index);
351 }
352 
pinctrl_get_pins_count(struct udevice * dev)353 int pinctrl_get_pins_count(struct udevice *dev)
354 {
355 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
356 
357 	if (!ops->get_pins_count)
358 		return -ENOSYS;
359 
360 	return ops->get_pins_count(dev);
361 }
362 
pinctrl_get_pin_name(struct udevice * dev,int selector,char * buf,int size)363 int pinctrl_get_pin_name(struct udevice *dev, int selector, char *buf,
364 			 int size)
365 {
366 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
367 
368 	if (!ops->get_pin_name)
369 		return -ENOSYS;
370 
371 	snprintf(buf, size, ops->get_pin_name(dev, selector));
372 
373 	return 0;
374 }
375 
pinctrl_get_pin_muxing(struct udevice * dev,int selector,char * buf,int size)376 int pinctrl_get_pin_muxing(struct udevice *dev, int selector, char *buf,
377 			   int size)
378 {
379 	struct pinctrl_ops *ops = pinctrl_get_ops(dev);
380 
381 	if (!ops->get_pin_muxing)
382 		return -ENOSYS;
383 
384 	return ops->get_pin_muxing(dev, selector, buf, size);
385 }
386 
387 /**
388  * pinconfig_post_bind() - post binding for PINCTRL uclass
389  * Recursively bind child nodes as pinconfig devices in case of full pinctrl.
390  *
391  * @dev: pinctrl device
392  * @return: 0 on success, or negative error code on failure
393  */
pinctrl_post_bind(struct udevice * dev)394 static int __maybe_unused pinctrl_post_bind(struct udevice *dev)
395 {
396 	const struct pinctrl_ops *ops = pinctrl_get_ops(dev);
397 
398 	if (!ops) {
399 		dev_dbg(dev, "ops is not set.  Do not bind.\n");
400 		return -EINVAL;
401 	}
402 
403 	/*
404 	 * If set_state callback is set, we assume this pinctrl driver is the
405 	 * full implementation.  In this case, its child nodes should be bound
406 	 * so that peripheral devices can easily search in parent devices
407 	 * during later DT-parsing.
408 	 */
409 	if (ops->set_state)
410 		return pinconfig_post_bind(dev);
411 
412 	return 0;
413 }
414 
415 UCLASS_DRIVER(pinctrl) = {
416 	.id = UCLASS_PINCTRL,
417 	.post_bind = pinctrl_post_bind,
418 	.flags = DM_UC_FLAG_SEQ_ALIAS,
419 	.name = "pinctrl",
420 };
421