xref: /rk3399_rockchip-uboot/drivers/gpio/gpio-uclass.c (revision 0dac4d51f50e9252dbc00075cf65eeba57017926)
1 /*
2  * Copyright (c) 2013 Google, Inc
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <errno.h>
10 #include <fdtdec.h>
11 #include <malloc.h>
12 #include <asm/gpio.h>
13 #include <linux/ctype.h>
14 
15 /**
16  * gpio_to_device() - Convert global GPIO number to device, number
17  *
18  * Convert the GPIO number to an entry in the list of GPIOs
19  * or GPIO blocks registered with the GPIO controller. Returns
20  * entry on success, NULL on error.
21  *
22  * @gpio:	The numeric representation of the GPIO
23  * @desc:	Returns description (desc->flags will always be 0)
24  * @return 0 if found, -ENOENT if not found
25  */
26 static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
27 {
28 	struct gpio_dev_priv *uc_priv;
29 	struct udevice *dev;
30 	int ret;
31 
32 	for (ret = uclass_first_device(UCLASS_GPIO, &dev);
33 	     dev;
34 	     ret = uclass_next_device(&dev)) {
35 		uc_priv = dev->uclass_priv;
36 		if (gpio >= uc_priv->gpio_base &&
37 		    gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
38 			desc->dev = dev;
39 			desc->offset = gpio - uc_priv->gpio_base;
40 			desc->flags = 0;
41 			return 0;
42 		}
43 	}
44 
45 	/* No such GPIO */
46 	return ret ? ret : -ENOENT;
47 }
48 
49 int gpio_lookup_name(const char *name, struct udevice **devp,
50 		     unsigned int *offsetp, unsigned int *gpiop)
51 {
52 	struct gpio_dev_priv *uc_priv = NULL;
53 	struct udevice *dev;
54 	ulong offset;
55 	int numeric;
56 	int ret;
57 
58 	if (devp)
59 		*devp = NULL;
60 	numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
61 	for (ret = uclass_first_device(UCLASS_GPIO, &dev);
62 	     dev;
63 	     ret = uclass_next_device(&dev)) {
64 		int len;
65 
66 		uc_priv = dev->uclass_priv;
67 		if (numeric != -1) {
68 			offset = numeric - uc_priv->gpio_base;
69 			/* Allow GPIOs to be numbered from 0 */
70 			if (offset >= 0 && offset < uc_priv->gpio_count)
71 				break;
72 		}
73 
74 		len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
75 
76 		if (!strncasecmp(name, uc_priv->bank_name, len)) {
77 			if (!strict_strtoul(name + len, 10, &offset))
78 				break;
79 		}
80 	}
81 
82 	if (!dev)
83 		return ret ? ret : -EINVAL;
84 
85 	if (devp)
86 		*devp = dev;
87 	if (offsetp)
88 		*offsetp = offset;
89 	if (gpiop)
90 		*gpiop = uc_priv->gpio_base + offset;
91 
92 	return 0;
93 }
94 
95 int gpio_find_and_xlate(struct gpio_desc *desc,
96 			struct fdtdec_phandle_args *args)
97 {
98 	struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
99 
100 	/* Use the first argument as the offset by default */
101 	if (args->args_count > 0)
102 		desc->offset = args->args[0];
103 	else
104 		desc->offset = -1;
105 	desc->flags = 0;
106 
107 	return ops->xlate ? ops->xlate(desc->dev, desc, args) : 0;
108 }
109 
110 static int dm_gpio_request(struct gpio_desc *desc, const char *label)
111 {
112 	struct udevice *dev = desc->dev;
113 	struct gpio_dev_priv *uc_priv;
114 	char *str;
115 	int ret;
116 
117 	uc_priv = dev->uclass_priv;
118 	if (uc_priv->name[desc->offset])
119 		return -EBUSY;
120 	str = strdup(label);
121 	if (!str)
122 		return -ENOMEM;
123 	if (gpio_get_ops(dev)->request) {
124 		ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
125 		if (ret) {
126 			free(str);
127 			return ret;
128 		}
129 	}
130 	uc_priv->name[desc->offset] = str;
131 
132 	return 0;
133 }
134 
135 /**
136  * gpio_request() - [COMPAT] Request GPIO
137  * gpio:	GPIO number
138  * label:	Name for the requested GPIO
139  *
140  * The label is copied and allocated so the caller does not need to keep
141  * the pointer around.
142  *
143  * This function implements the API that's compatible with current
144  * GPIO API used in U-Boot. The request is forwarded to particular
145  * GPIO driver. Returns 0 on success, negative value on error.
146  */
147 int gpio_request(unsigned gpio, const char *label)
148 {
149 	struct gpio_desc desc;
150 	int ret;
151 
152 	ret = gpio_to_device(gpio, &desc);
153 	if (ret)
154 		return ret;
155 
156 	return dm_gpio_request(&desc, label);
157 }
158 
159 /**
160  * gpio_requestf() - [COMPAT] Request GPIO
161  * @gpio:	GPIO number
162  * @fmt:	Format string for the requested GPIO
163  * @...:	Arguments for the printf() format string
164  *
165  * This function implements the API that's compatible with current
166  * GPIO API used in U-Boot. The request is forwarded to particular
167  * GPIO driver. Returns 0 on success, negative value on error.
168  */
169 int gpio_requestf(unsigned gpio, const char *fmt, ...)
170 {
171 	va_list args;
172 	char buf[40];
173 
174 	va_start(args, fmt);
175 	vscnprintf(buf, sizeof(buf), fmt, args);
176 	va_end(args);
177 	return gpio_request(gpio, buf);
178 }
179 
180 int _dm_gpio_free(struct udevice *dev, uint offset)
181 {
182 	struct gpio_dev_priv *uc_priv;
183 	int ret;
184 
185 	uc_priv = dev->uclass_priv;
186 	if (!uc_priv->name[offset])
187 		return -ENXIO;
188 	if (gpio_get_ops(dev)->free) {
189 		ret = gpio_get_ops(dev)->free(dev, offset);
190 		if (ret)
191 			return ret;
192 	}
193 
194 	free(uc_priv->name[offset]);
195 	uc_priv->name[offset] = NULL;
196 
197 	return 0;
198 }
199 
200 /**
201  * gpio_free() - [COMPAT] Relinquish GPIO
202  * gpio:	GPIO number
203  *
204  * This function implements the API that's compatible with current
205  * GPIO API used in U-Boot. The request is forwarded to particular
206  * GPIO driver. Returns 0 on success, negative value on error.
207  */
208 int gpio_free(unsigned gpio)
209 {
210 	struct gpio_desc desc;
211 	int ret;
212 
213 	ret = gpio_to_device(gpio, &desc);
214 	if (ret)
215 		return ret;
216 
217 	return _dm_gpio_free(desc.dev, desc.offset);
218 }
219 
220 static int check_reserved(struct gpio_desc *desc, const char *func)
221 {
222 	struct gpio_dev_priv *uc_priv = desc->dev->uclass_priv;
223 
224 	if (!uc_priv->name[desc->offset]) {
225 		printf("%s: %s: error: gpio %s%d not reserved\n",
226 		       desc->dev->name, func,
227 		       uc_priv->bank_name ? uc_priv->bank_name : "",
228 		       desc->offset);
229 		return -EBUSY;
230 	}
231 
232 	return 0;
233 }
234 
235 /**
236  * gpio_direction_input() - [COMPAT] Set GPIO direction to input
237  * gpio:	GPIO number
238  *
239  * This function implements the API that's compatible with current
240  * GPIO API used in U-Boot. The request is forwarded to particular
241  * GPIO driver. Returns 0 on success, negative value on error.
242  */
243 int gpio_direction_input(unsigned gpio)
244 {
245 	struct gpio_desc desc;
246 	int ret;
247 
248 	ret = gpio_to_device(gpio, &desc);
249 	if (ret)
250 		return ret;
251 	ret = check_reserved(&desc, "dir_input");
252 	if (ret)
253 		return ret;
254 
255 	return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
256 }
257 
258 /**
259  * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
260  * gpio:	GPIO number
261  * value:	Logical value to be set on the GPIO pin
262  *
263  * This function implements the API that's compatible with current
264  * GPIO API used in U-Boot. The request is forwarded to particular
265  * GPIO driver. Returns 0 on success, negative value on error.
266  */
267 int gpio_direction_output(unsigned gpio, int value)
268 {
269 	struct gpio_desc desc;
270 	int ret;
271 
272 	ret = gpio_to_device(gpio, &desc);
273 	if (ret)
274 		return ret;
275 	ret = check_reserved(&desc, "dir_output");
276 	if (ret)
277 		return ret;
278 
279 	return gpio_get_ops(desc.dev)->direction_output(desc.dev,
280 							desc.offset, value);
281 }
282 
283 int dm_gpio_get_value(struct gpio_desc *desc)
284 {
285 	int value;
286 	int ret;
287 
288 	ret = check_reserved(desc, "get_value");
289 	if (ret)
290 		return ret;
291 
292 	value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
293 
294 	return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
295 }
296 
297 int dm_gpio_set_value(struct gpio_desc *desc, int value)
298 {
299 	int ret;
300 
301 	ret = check_reserved(desc, "set_value");
302 	if (ret)
303 		return ret;
304 
305 	if (desc->flags & GPIOD_ACTIVE_LOW)
306 		value = !value;
307 	gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
308 	return 0;
309 }
310 
311 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
312 {
313 	struct udevice *dev = desc->dev;
314 	struct dm_gpio_ops *ops = gpio_get_ops(dev);
315 	int ret;
316 
317 	ret = check_reserved(desc, "set_dir");
318 	if (ret)
319 		return ret;
320 
321 	if (flags & GPIOD_IS_OUT) {
322 		int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
323 
324 		if (flags & GPIOD_ACTIVE_LOW)
325 			value = !value;
326 		ret = ops->direction_output(dev, desc->offset, value);
327 	} else  if (flags & GPIOD_IS_IN) {
328 		ret = ops->direction_input(dev, desc->offset);
329 	}
330 	if (ret)
331 		return ret;
332 	/*
333 	 * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
334 	 * futures
335 	 */
336 	desc->flags = flags;
337 
338 	return 0;
339 }
340 
341 int dm_gpio_set_dir(struct gpio_desc *desc)
342 {
343 	return dm_gpio_set_dir_flags(desc, desc->flags);
344 }
345 
346 /**
347  * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
348  * gpio:	GPIO number
349  *
350  * This function implements the API that's compatible with current
351  * GPIO API used in U-Boot. The request is forwarded to particular
352  * GPIO driver. Returns the value of the GPIO pin, or negative value
353  * on error.
354  */
355 int gpio_get_value(unsigned gpio)
356 {
357 	int ret;
358 
359 	struct gpio_desc desc;
360 
361 	ret = gpio_to_device(gpio, &desc);
362 	if (ret)
363 		return ret;
364 	return dm_gpio_get_value(&desc);
365 }
366 
367 /**
368  * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
369  * gpio:	GPIO number
370  * value:	Logical value to be set on the GPIO pin.
371  *
372  * This function implements the API that's compatible with current
373  * GPIO API used in U-Boot. The request is forwarded to particular
374  * GPIO driver. Returns 0 on success, negative value on error.
375  */
376 int gpio_set_value(unsigned gpio, int value)
377 {
378 	struct gpio_desc desc;
379 	int ret;
380 
381 	ret = gpio_to_device(gpio, &desc);
382 	if (ret)
383 		return ret;
384 	return dm_gpio_set_value(&desc, value);
385 }
386 
387 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
388 {
389 	struct gpio_dev_priv *priv;
390 
391 	/* Must be called on an active device */
392 	priv = dev->uclass_priv;
393 	assert(priv);
394 
395 	*bit_count = priv->gpio_count;
396 	return priv->bank_name;
397 }
398 
399 static const char * const gpio_function[GPIOF_COUNT] = {
400 	"input",
401 	"output",
402 	"unused",
403 	"unknown",
404 	"func",
405 };
406 
407 int get_function(struct udevice *dev, int offset, bool skip_unused,
408 		 const char **namep)
409 {
410 	struct gpio_dev_priv *uc_priv = dev->uclass_priv;
411 	struct dm_gpio_ops *ops = gpio_get_ops(dev);
412 
413 	BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
414 	if (!device_active(dev))
415 		return -ENODEV;
416 	if (offset < 0 || offset >= uc_priv->gpio_count)
417 		return -EINVAL;
418 	if (namep)
419 		*namep = uc_priv->name[offset];
420 	if (skip_unused && !uc_priv->name[offset])
421 		return GPIOF_UNUSED;
422 	if (ops->get_function) {
423 		int ret;
424 
425 		ret = ops->get_function(dev, offset);
426 		if (ret < 0)
427 			return ret;
428 		if (ret >= ARRAY_SIZE(gpio_function))
429 			return -ENODATA;
430 		return ret;
431 	}
432 
433 	return GPIOF_UNKNOWN;
434 }
435 
436 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
437 {
438 	return get_function(dev, offset, true, namep);
439 }
440 
441 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
442 {
443 	return get_function(dev, offset, false, namep);
444 }
445 
446 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
447 {
448 	struct dm_gpio_ops *ops = gpio_get_ops(dev);
449 	struct gpio_dev_priv *priv;
450 	char *str = buf;
451 	int func;
452 	int ret;
453 	int len;
454 
455 	BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
456 
457 	*buf = 0;
458 	priv = dev->uclass_priv;
459 	ret = gpio_get_raw_function(dev, offset, NULL);
460 	if (ret < 0)
461 		return ret;
462 	func = ret;
463 	len = snprintf(str, buffsize, "%s%d: %s",
464 		       priv->bank_name ? priv->bank_name : "",
465 		       offset, gpio_function[func]);
466 	if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
467 	    func == GPIOF_UNUSED) {
468 		const char *label;
469 		bool used;
470 
471 		ret = ops->get_value(dev, offset);
472 		if (ret < 0)
473 			return ret;
474 		used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
475 		snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
476 			 ret,
477 			 used ? 'x' : ' ',
478 			 used ? " " : "",
479 			 label ? label : "");
480 	}
481 
482 	return 0;
483 }
484 
485 /*
486  * get a number comprised of multiple GPIO values. gpio_num_array points to
487  * the array of gpio pin numbers to scan, terminated by -1.
488  */
489 unsigned gpio_get_values_as_int(const int *gpio_num_array)
490 {
491 	int gpio;
492 	unsigned bitmask = 1;
493 	unsigned vector = 0;
494 
495 	while (bitmask &&
496 	       ((gpio = *gpio_num_array++) != -1)) {
497 		if (gpio_get_value(gpio))
498 			vector |= bitmask;
499 		bitmask <<= 1;
500 	}
501 	return vector;
502 }
503 
504 /* We need to renumber the GPIOs when any driver is probed/removed */
505 static int gpio_renumber(struct udevice *removed_dev)
506 {
507 	struct gpio_dev_priv *uc_priv;
508 	struct udevice *dev;
509 	struct uclass *uc;
510 	unsigned base;
511 	int ret;
512 
513 	ret = uclass_get(UCLASS_GPIO, &uc);
514 	if (ret)
515 		return ret;
516 
517 	/* Ensure that we have a base for each bank */
518 	base = 0;
519 	uclass_foreach_dev(dev, uc) {
520 		if (device_active(dev) && dev != removed_dev) {
521 			uc_priv = dev->uclass_priv;
522 			uc_priv->gpio_base = base;
523 			base += uc_priv->gpio_count;
524 		}
525 	}
526 
527 	return 0;
528 }
529 
530 static int gpio_post_probe(struct udevice *dev)
531 {
532 	struct gpio_dev_priv *uc_priv = dev->uclass_priv;
533 
534 	uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
535 	if (!uc_priv->name)
536 		return -ENOMEM;
537 
538 	return gpio_renumber(NULL);
539 }
540 
541 static int gpio_pre_remove(struct udevice *dev)
542 {
543 	struct gpio_dev_priv *uc_priv = dev->uclass_priv;
544 	int i;
545 
546 	for (i = 0; i < uc_priv->gpio_count; i++) {
547 		if (uc_priv->name[i])
548 			free(uc_priv->name[i]);
549 	}
550 	free(uc_priv->name);
551 
552 	return gpio_renumber(dev);
553 }
554 
555 UCLASS_DRIVER(gpio) = {
556 	.id		= UCLASS_GPIO,
557 	.name		= "gpio",
558 	.post_probe	= gpio_post_probe,
559 	.pre_remove	= gpio_pre_remove,
560 	.per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),
561 };
562