gpio-uclass.c 32.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
Simon Glass's avatar
Simon Glass committed
2
3
4
5
/*
 * Copyright (c) 2013 Google, Inc
 */

6
7
#define LOG_CATEGORY	UCLASS_GPIO

Simon Glass's avatar
Simon Glass committed
8
9
#include <common.h>
#include <dm.h>
10
#include <log.h>
11
12
#include <dm/devres.h>
#include <dm/device_compat.h>
Heiko Schocher's avatar
Heiko Schocher committed
13
14
15
#include <dm/device-internal.h>
#include <dm/lists.h>
#include <dm/uclass-internal.h>
16
#include <dt-bindings/gpio/gpio.h>
Simon Glass's avatar
Simon Glass committed
17
#include <errno.h>
18
#include <fdtdec.h>
19
#include <malloc.h>
20
#include <acpi/acpi_device.h>
21
#include <asm/global_data.h>
Simon Glass's avatar
Simon Glass committed
22
#include <asm/gpio.h>
23
#include <dm/device_compat.h>
24
#include <linux/bug.h>
Simon Glass's avatar
Simon Glass committed
25
#include <linux/ctype.h>
26
#include <linux/delay.h>
Simon Glass's avatar
Simon Glass committed
27

28
29
DECLARE_GLOBAL_DATA_PTR;

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
 * gpio_desc_init() - Initialize the GPIO descriptor
 *
 * @desc:	GPIO descriptor to initialize
 * @dev:	GPIO device
 * @offset:	Offset of device GPIO
 */
static void gpio_desc_init(struct gpio_desc *desc,
			   struct udevice *dev,
			   uint offset)
{
	desc->dev = dev;
	desc->offset = offset;
	desc->flags = 0;
}

Simon Glass's avatar
Simon Glass committed
46
47
48
49
50
51
/**
 * gpio_to_device() - Convert global GPIO number to device, number
 *
 * Convert the GPIO number to an entry in the list of GPIOs
 * or GPIO blocks registered with the GPIO controller. Returns
 * entry on success, NULL on error.
52
53
54
55
 *
 * @gpio:	The numeric representation of the GPIO
 * @desc:	Returns description (desc->flags will always be 0)
 * @return 0 if found, -ENOENT if not found
Simon Glass's avatar
Simon Glass committed
56
 */
57
static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
Simon Glass's avatar
Simon Glass committed
58
59
{
	struct gpio_dev_priv *uc_priv;
60
	struct udevice *dev;
Simon Glass's avatar
Simon Glass committed
61
62
63
64
65
	int ret;

	for (ret = uclass_first_device(UCLASS_GPIO, &dev);
	     dev;
	     ret = uclass_next_device(&dev)) {
66
		uc_priv = dev_get_uclass_priv(dev);
Simon Glass's avatar
Simon Glass committed
67
68
		if (gpio >= uc_priv->gpio_base &&
		    gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
69
			gpio_desc_init(desc, dev, gpio - uc_priv->gpio_base);
Simon Glass's avatar
Simon Glass committed
70
71
72
73
74
			return 0;
		}
	}

	/* No such GPIO */
75
	return ret ? ret : -ENOENT;
Simon Glass's avatar
Simon Glass committed
76
77
}

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#if CONFIG_IS_ENABLED(DM_GPIO_LOOKUP_LABEL)
/**
 * dm_gpio_lookup_label() - look for name in gpio device
 *
 * search in uc_priv, if there is a gpio with labelname same
 * as name.
 *
 * @name:	name which is searched
 * @uc_priv:	gpio_dev_priv pointer.
 * @offset:	gpio offset within the device
 * @return:	0 if found, -ENOENT if not.
 */
static int dm_gpio_lookup_label(const char *name,
				struct gpio_dev_priv *uc_priv, ulong *offset)
{
	int len;
	int i;

	*offset = -1;
	len = strlen(name);
	for (i = 0; i < uc_priv->gpio_count; i++) {
		if (!uc_priv->name[i])
			continue;
		if (!strncmp(name, uc_priv->name[i], len)) {
			*offset = i;
			return 0;
		}
	}
	return -ENOENT;
}
#else
static int
dm_gpio_lookup_label(const char *name, struct gpio_dev_priv *uc_priv,
		     ulong *offset)
{
	return -ENOENT;
}
#endif

117
int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
Simon Glass's avatar
Simon Glass committed
118
{
Simon Glass's avatar
Simon Glass committed
119
	struct gpio_dev_priv *uc_priv = NULL;
120
	struct udevice *dev;
Simon Glass's avatar
Simon Glass committed
121
122
	ulong offset;
	int numeric;
Simon Glass's avatar
Simon Glass committed
123
124
	int ret;

Simon Glass's avatar
Simon Glass committed
125
	numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
Simon Glass's avatar
Simon Glass committed
126
127
128
129
130
	for (ret = uclass_first_device(UCLASS_GPIO, &dev);
	     dev;
	     ret = uclass_next_device(&dev)) {
		int len;

131
		uc_priv = dev_get_uclass_priv(dev);
Simon Glass's avatar
Simon Glass committed
132
133
134
		if (numeric != -1) {
			offset = numeric - uc_priv->gpio_base;
			/* Allow GPIOs to be numbered from 0 */
135
			if (offset < uc_priv->gpio_count)
Simon Glass's avatar
Simon Glass committed
136
137
138
				break;
		}

Simon Glass's avatar
Simon Glass committed
139
140
		len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;

141
		if (!strncasecmp(name, uc_priv->bank_name, len)) {
Simon Glass's avatar
Simon Glass committed
142
143
			if (!strict_strtoul(name + len, 10, &offset))
				break;
Simon Glass's avatar
Simon Glass committed
144
		}
145
146
147
148
149
150
151

		/*
		 * if we did not found a gpio through its bank
		 * name, we search for a valid gpio label.
		 */
		if (!dm_gpio_lookup_label(name, uc_priv, &offset))
			break;
Simon Glass's avatar
Simon Glass committed
152
153
	}

Simon Glass's avatar
Simon Glass committed
154
155
156
	if (!dev)
		return ret ? ret : -EINVAL;

157
	gpio_desc_init(desc, dev, offset);
158
159
160
161
162
163
164
165
166
167

	return 0;
}

int gpio_lookup_name(const char *name, struct udevice **devp,
		     unsigned int *offsetp, unsigned int *gpiop)
{
	struct gpio_desc desc;
	int ret;

Simon Glass's avatar
Simon Glass committed
168
	if (devp)
169
170
171
172
173
174
175
		*devp = NULL;
	ret = dm_gpio_lookup_name(name, &desc);
	if (ret)
		return ret;

	if (devp)
		*devp = desc.dev;
Simon Glass's avatar
Simon Glass committed
176
	if (offsetp)
177
178
179
180
181
182
		*offsetp = desc.offset;
	if (gpiop) {
		struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);

		*gpiop = uc_priv->gpio_base + desc.offset;
	}
Simon Glass's avatar
Simon Glass committed
183
184

	return 0;
Simon Glass's avatar
Simon Glass committed
185
186
}

187
188
int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
			  struct ofnode_phandle_args *args)
189
190
191
192
193
194
195
196
197
{
	if (args->args_count < 1)
		return -EINVAL;

	desc->offset = args->args[0];

	if (args->args_count < 2)
		return 0;

198
	desc->flags = 0;
199
	if (args->args[1] & GPIO_ACTIVE_LOW)
200
		desc->flags |= GPIOD_ACTIVE_LOW;
201

202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
	/*
	 * need to test 2 bits for gpio output binding:
	 * OPEN_DRAIN (0x6) = SINGLE_ENDED (0x2) | LINE_OPEN_DRAIN (0x4)
	 * OPEN_SOURCE (0x2) = SINGLE_ENDED (0x2) | LINE_OPEN_SOURCE (0x0)
	 */
	if (args->args[1] & GPIO_SINGLE_ENDED) {
		if (args->args[1] & GPIO_LINE_OPEN_DRAIN)
			desc->flags |= GPIOD_OPEN_DRAIN;
		else
			desc->flags |= GPIOD_OPEN_SOURCE;
	}

	if (args->args[1] & GPIO_PULL_UP)
		desc->flags |= GPIOD_PULL_UP;

	if (args->args[1] & GPIO_PULL_DOWN)
		desc->flags |= GPIOD_PULL_DOWN;

220
221
222
	return 0;
}

223
static int gpio_find_and_xlate(struct gpio_desc *desc,
224
			       struct ofnode_phandle_args *args)
225
{
226
	const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
227

228
229
	if (ops->xlate)
		return ops->xlate(desc->dev, desc, args);
230
	else
231
		return gpio_xlate_offs_flags(desc->dev, desc, args);
232
233
}

234
#if defined(CONFIG_GPIO_HOG)
Heiko Schocher's avatar
Heiko Schocher committed
235
236
237
238
239
240
241
242
243
244
245

struct gpio_hog_priv {
	struct gpio_desc gpiod;
};

struct gpio_hog_data {
	int gpiod_flags;
	int value;
	u32 val[2];
};

246
static int gpio_hog_of_to_plat(struct udevice *dev)
Heiko Schocher's avatar
Heiko Schocher committed
247
{
248
	struct gpio_hog_data *plat = dev_get_plat(dev);
Heiko Schocher's avatar
Heiko Schocher committed
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
	const char *nodename;
	int ret;

	plat->value = 0;
	if (dev_read_bool(dev, "input")) {
		plat->gpiod_flags = GPIOD_IS_IN;
	} else if (dev_read_bool(dev, "output-high")) {
		plat->value = 1;
		plat->gpiod_flags = GPIOD_IS_OUT;
	} else if (dev_read_bool(dev, "output-low")) {
		plat->gpiod_flags = GPIOD_IS_OUT;
	} else {
		printf("%s: missing gpio-hog state.\n", __func__);
		return -EINVAL;
	}
	ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
	if (ret) {
		printf("%s: wrong gpios property, 2 values needed %d\n",
		       __func__, ret);
		return ret;
	}
	nodename = dev_read_string(dev, "line-name");
271
272
	if (nodename)
		device_set_name(dev, nodename);
Heiko Schocher's avatar
Heiko Schocher committed
273
274
275
276
277
278

	return 0;
}

static int gpio_hog_probe(struct udevice *dev)
{
279
	struct gpio_hog_data *plat = dev_get_plat(dev);
Heiko Schocher's avatar
Heiko Schocher committed
280
281
282
283
284
285
286
287
288
289
290
	struct gpio_hog_priv *priv = dev_get_priv(dev);
	int ret;

	ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
				     plat->val[0], plat->gpiod_flags,
				     plat->val[1], &priv->gpiod);
	if (ret < 0) {
		debug("%s: node %s could not get gpio.\n", __func__,
		      dev->name);
		return ret;
	}
291
292
293
294
295
296
297
298
299

	if (plat->gpiod_flags == GPIOD_IS_OUT) {
		ret = dm_gpio_set_value(&priv->gpiod, plat->value);
		if (ret < 0) {
			debug("%s: node %s could not set gpio.\n", __func__,
			      dev->name);
			return ret;
		}
	}
Heiko Schocher's avatar
Heiko Schocher committed
300
301
302
303
304
305
306
307

	return 0;
}

int gpio_hog_probe_all(void)
{
	struct udevice *dev;
	int ret;
308
	int retval = 0;
Heiko Schocher's avatar
Heiko Schocher committed
309
310
311
312

	for (uclass_first_device(UCLASS_NOP, &dev);
	     dev;
	     uclass_find_next_device(&dev)) {
313
		if (dev->driver == DM_DRIVER_GET(gpio_hog)) {
Heiko Schocher's avatar
Heiko Schocher committed
314
			ret = device_probe(dev);
315
316
317
318
319
			if (ret) {
				printf("Failed to probe device %s err: %d\n",
				       dev->name, ret);
				retval = ret;
			}
Heiko Schocher's avatar
Heiko Schocher committed
320
321
322
		}
	}

323
	return retval;
Heiko Schocher's avatar
Heiko Schocher committed
324
325
}

326
int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher's avatar
Heiko Schocher committed
327
328
329
{
	struct udevice *dev;

330
	*desc = NULL;
Heiko Schocher's avatar
Heiko Schocher committed
331
332
333
334
	gpio_hog_probe_all();
	if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
		struct gpio_hog_priv *priv = dev_get_priv(dev);

335
336
		*desc = &priv->gpiod;
		return 0;
Heiko Schocher's avatar
Heiko Schocher committed
337
338
	}

339
	return -ENODEV;
Heiko Schocher's avatar
Heiko Schocher committed
340
341
342
343
344
}

U_BOOT_DRIVER(gpio_hog) = {
	.name	= "gpio_hog",
	.id	= UCLASS_NOP,
345
	.of_to_plat = gpio_hog_of_to_plat,
Heiko Schocher's avatar
Heiko Schocher committed
346
	.probe = gpio_hog_probe,
347
	.priv_auto	= sizeof(struct gpio_hog_priv),
348
	.plat_auto	= sizeof(struct gpio_hog_data),
Heiko Schocher's avatar
Heiko Schocher committed
349
350
};
#else
351
int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher's avatar
Heiko Schocher committed
352
{
353
	return 0;
Heiko Schocher's avatar
Heiko Schocher committed
354
355
356
}
#endif

357
int dm_gpio_request(struct gpio_desc *desc, const char *label)
358
{
359
	const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
360
361
362
363
364
	struct udevice *dev = desc->dev;
	struct gpio_dev_priv *uc_priv;
	char *str;
	int ret;

365
	uc_priv = dev_get_uclass_priv(dev);
366
367
368
369
370
	if (uc_priv->name[desc->offset])
		return -EBUSY;
	str = strdup(label);
	if (!str)
		return -ENOMEM;
371
372
	if (ops->request) {
		ret = ops->request(dev, desc->offset, label);
373
374
375
376
377
378
379
380
381
382
		if (ret) {
			free(str);
			return ret;
		}
	}
	uc_priv->name[desc->offset] = str;

	return 0;
}

383
384
static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
{
385
#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
386
387
388
389
390
391
392
	va_list args;
	char buf[40];

	va_start(args, fmt);
	vscnprintf(buf, sizeof(buf), fmt, args);
	va_end(args);
	return dm_gpio_request(desc, buf);
393
394
395
#else
	return dm_gpio_request(desc, fmt);
#endif
396
397
}

Simon Glass's avatar
Simon Glass committed
398
399
400
401
402
/**
 * gpio_request() - [COMPAT] Request GPIO
 * gpio:	GPIO number
 * label:	Name for the requested GPIO
 *
403
404
405
 * The label is copied and allocated so the caller does not need to keep
 * the pointer around.
 *
Simon Glass's avatar
Simon Glass committed
406
407
408
409
410
411
 * This function implements the API that's compatible with current
 * GPIO API used in U-Boot. The request is forwarded to particular
 * GPIO driver. Returns 0 on success, negative value on error.
 */
int gpio_request(unsigned gpio, const char *label)
{
412
	struct gpio_desc desc;
Simon Glass's avatar
Simon Glass committed
413
414
	int ret;

415
	ret = gpio_to_device(gpio, &desc);
Simon Glass's avatar
Simon Glass committed
416
417
418
	if (ret)
		return ret;

419
	return dm_gpio_request(&desc, label);
Simon Glass's avatar
Simon Glass committed
420
421
}

422
423
424
425
426
427
428
429
430
431
432
433
/**
 * gpio_requestf() - [COMPAT] Request GPIO
 * @gpio:	GPIO number
 * @fmt:	Format string for the requested GPIO
 * @...:	Arguments for the printf() format string
 *
 * This function implements the API that's compatible with current
 * GPIO API used in U-Boot. The request is forwarded to particular
 * GPIO driver. Returns 0 on success, negative value on error.
 */
int gpio_requestf(unsigned gpio, const char *fmt, ...)
{
434
#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
435
436
437
438
439
440
441
	va_list args;
	char buf[40];

	va_start(args, fmt);
	vscnprintf(buf, sizeof(buf), fmt, args);
	va_end(args);
	return gpio_request(gpio, buf);
442
443
444
#else
	return gpio_request(gpio, fmt);
#endif
445
446
}

447
int _dm_gpio_free(struct udevice *dev, uint offset)
Simon Glass's avatar
Simon Glass committed
448
{
449
	const struct dm_gpio_ops *ops = gpio_get_ops(dev);
450
	struct gpio_dev_priv *uc_priv;
Simon Glass's avatar
Simon Glass committed
451
452
	int ret;

453
	uc_priv = dev_get_uclass_priv(dev);
454
455
	if (!uc_priv->name[offset])
		return -ENXIO;
456
457
	if (ops->rfree) {
		ret = ops->rfree(dev, offset);
458
459
460
461
462
463
464
465
466
467
		if (ret)
			return ret;
	}

	free(uc_priv->name[offset]);
	uc_priv->name[offset] = NULL;

	return 0;
}

468
469
470
471
472
473
474
475
476
/**
 * gpio_free() - [COMPAT] Relinquish GPIO
 * gpio:	GPIO number
 *
 * This function implements the API that's compatible with current
 * GPIO API used in U-Boot. The request is forwarded to particular
 * GPIO driver. Returns 0 on success, negative value on error.
 */
int gpio_free(unsigned gpio)
477
{
478
479
480
481
482
483
484
485
486
	struct gpio_desc desc;
	int ret;

	ret = gpio_to_device(gpio, &desc);
	if (ret)
		return ret;

	return _dm_gpio_free(desc.dev, desc.offset);
}
487

Simon Glass's avatar
Simon Glass committed
488
static int check_reserved(const struct gpio_desc *desc, const char *func)
489
{
490
491
492
493
	struct gpio_dev_priv *uc_priv;

	if (!dm_gpio_is_valid(desc))
		return -ENOENT;
494

495
	uc_priv = dev_get_uclass_priv(desc->dev);
496
	if (!uc_priv->name[desc->offset]) {
497
		printf("%s: %s: error: gpio %s%d not reserved\n",
498
499
500
		       desc->dev->name, func,
		       uc_priv->bank_name ? uc_priv->bank_name : "",
		       desc->offset);
501
502
503
504
		return -EBUSY;
	}

	return 0;
Simon Glass's avatar
Simon Glass committed
505
506
507
508
509
510
511
512
513
514
515
516
}

/**
 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
 * gpio:	GPIO number
 *
 * This function implements the API that's compatible with current
 * GPIO API used in U-Boot. The request is forwarded to particular
 * GPIO driver. Returns 0 on success, negative value on error.
 */
int gpio_direction_input(unsigned gpio)
{
517
	struct gpio_desc desc;
Simon Glass's avatar
Simon Glass committed
518
519
	int ret;

520
	ret = gpio_to_device(gpio, &desc);
Simon Glass's avatar
Simon Glass committed
521
522
523
	if (ret)
		return ret;

524
	return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, GPIOD_IS_IN);
Simon Glass's avatar
Simon Glass committed
525
526
527
528
529
530
531
532
533
534
535
536
537
}

/**
 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
 * gpio:	GPIO number
 * value:	Logical value to be set on the GPIO pin
 *
 * This function implements the API that's compatible with current
 * GPIO API used in U-Boot. The request is forwarded to particular
 * GPIO driver. Returns 0 on success, negative value on error.
 */
int gpio_direction_output(unsigned gpio, int value)
{
538
	struct gpio_desc desc;
539
	ulong flags;
Simon Glass's avatar
Simon Glass committed
540
541
	int ret;

542
543
544
545
	ret = gpio_to_device(gpio, &desc);
	if (ret)
		return ret;

546
547
548
549
	flags = GPIOD_IS_OUT;
	if (value)
		flags |= GPIOD_IS_OUT_ACTIVE;
	return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, flags);
550
551
}

552
static int _gpio_get_value(const struct gpio_desc *desc)
553
{
554
	const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
555
	int value;
556

557
	value = ops->get_value(desc->dev, desc->offset);
558
559
560
561
562
563

	return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
}

int dm_gpio_get_value(const struct gpio_desc *desc)
{
564
565
566
	int ret;

	ret = check_reserved(desc, "get_value");
Simon Glass's avatar
Simon Glass committed
567
568
569
	if (ret)
		return ret;

570
	return _gpio_get_value(desc);
571
572
}

Simon Glass's avatar
Simon Glass committed
573
int dm_gpio_set_value(const struct gpio_desc *desc, int value)
574
{
575
	const struct dm_gpio_ops *ops;
576
577
578
579
580
581
582
583
	int ret;

	ret = check_reserved(desc, "set_value");
	if (ret)
		return ret;

	if (desc->flags & GPIOD_ACTIVE_LOW)
		value = !value;
584

585
586
587
588
589
590
591
592
593
594
595
596
	/* GPIOD_ are directly managed by driver in set_flags */
	ops = gpio_get_ops(desc->dev);
	if (ops->set_flags) {
		ulong flags = desc->flags;

		if (value)
			flags |= GPIOD_IS_OUT_ACTIVE;
		else
			flags &= ~GPIOD_IS_OUT_ACTIVE;
		return ops->set_flags(desc->dev, desc->offset, flags);
	}

597
598
599
600
601
602
	/*
	 * Emulate open drain by not actively driving the line high or
	 * Emulate open source by not actively driving the line low
	 */
	if ((desc->flags & GPIOD_OPEN_DRAIN && value) ||
	    (desc->flags & GPIOD_OPEN_SOURCE && !value))
603
		return ops->direction_input(desc->dev, desc->offset);
604
605
	else if (desc->flags & GPIOD_OPEN_DRAIN ||
		 desc->flags & GPIOD_OPEN_SOURCE)
606
607
608
609
610
		return ops->direction_output(desc->dev, desc->offset, value);

	ret = ops->set_value(desc->dev, desc->offset, value);
	if (ret)
		return ret;
611

612
613
614
	return 0;
}

615
616
617
618
619
620
/* check dir flags invalid configuration */
static int check_dir_flags(ulong flags)
{
	if ((flags & GPIOD_IS_OUT) && (flags & GPIOD_IS_IN)) {
		log_debug("%s: flags 0x%lx has GPIOD_IS_OUT and GPIOD_IS_IN\n",
			  __func__, flags);
621
622
623
624
625
626
627
628
629
630
631
632
		return -EINVAL;
	}

	if ((flags & GPIOD_PULL_UP) && (flags & GPIOD_PULL_DOWN)) {
		log_debug("%s: flags 0x%lx has GPIOD_PULL_UP and GPIOD_PULL_DOWN\n",
			  __func__, flags);
		return -EINVAL;
	}

	if ((flags & GPIOD_OPEN_DRAIN) && (flags & GPIOD_OPEN_SOURCE)) {
		log_debug("%s: flags 0x%lx has GPIOD_OPEN_DRAIN and GPIOD_OPEN_SOURCE\n",
			  __func__, flags);
633
634
635
636
637
638
		return -EINVAL;
	}

	return 0;
}

639
640
641
642
643
644
645
646
647
648
649
/**
 * _dm_gpio_set_flags() - Send flags to the driver
 *
 * This uses the best available method to send the given flags to the driver.
 * Note that if flags & GPIOD_ACTIVE_LOW, the driver sees the opposite value
 * of GPIOD_IS_OUT_ACTIVE.
 *
 * @desc:	GPIO description
 * @flags:	flags value to set
 * @return 0 if OK, -ve on error
 */
650
static int _dm_gpio_set_flags(struct gpio_desc *desc, ulong flags)
651
652
{
	struct udevice *dev = desc->dev;
653
	const struct dm_gpio_ops *ops = gpio_get_ops(dev);
654
	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
655
	int ret = 0;
656

657
658
659
660
661
662
663
664
665
666
667
	ret = check_dir_flags(flags);
	if (ret) {
		dev_dbg(dev,
			"%s error: set_dir_flags for gpio %s%d has invalid dir flags 0x%lx\n",
			desc->dev->name,
			uc_priv->bank_name ? uc_priv->bank_name : "",
			desc->offset, flags);

		return ret;
	}

668
669
670
671
672
	/* If active low, invert the output state */
	if ((flags & (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW)) ==
		(GPIOD_IS_OUT | GPIOD_ACTIVE_LOW))
		flags ^= GPIOD_IS_OUT_ACTIVE;

673
674
675
	/* GPIOD_ are directly managed by driver in set_flags */
	if (ops->set_flags) {
		ret = ops->set_flags(dev, desc->offset, flags);
676
677
	} else {
		if (flags & GPIOD_IS_OUT) {
678
679
680
			bool value = flags & GPIOD_IS_OUT_ACTIVE;

			ret = ops->direction_output(dev, desc->offset, value);
681
682
683
		} else if (flags & GPIOD_IS_IN) {
			ret = ops->direction_input(dev, desc->offset);
		}
684
	}
685
686
687
688

	return ret;
}

689
int dm_gpio_clrset_flags(struct gpio_desc *desc, ulong clr, ulong set)
690
{
691
	ulong flags;
692
693
694
	int ret;

	ret = check_reserved(desc, "set_dir_flags");
695
696
697
	if (ret)
		return ret;

698
699
	flags = (desc->flags & ~clr) | set;

700
	ret = _dm_gpio_set_flags(desc, flags);
701
702
	if (ret)
		return ret;
703

704
705
706
707
708
709
710
711
712
713
	/* save the flags also in descriptor */
	desc->flags = flags;

	return 0;
}

int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
{
	/* combine the requested flags (for IN/OUT) and the descriptor flags */
	return dm_gpio_clrset_flags(desc, GPIOD_MASK_DIR, flags);
714
715
}

716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
int dm_gpios_clrset_flags(struct gpio_desc *desc, int count, ulong clr,
			  ulong set)
{
	int ret;
	int i;

	for (i = 0; i < count; i++) {
		ret = dm_gpio_clrset_flags(&desc[i], clr, set);
		if (ret)
			return log_ret(ret);
	}

	return 0;
}

731
int dm_gpio_get_flags(struct gpio_desc *desc, ulong *flagsp)
732
{
733
734
	struct udevice *dev = desc->dev;
	int ret, value;
735
	const struct dm_gpio_ops *ops = gpio_get_ops(dev);
736
	ulong flags;
737

738
	ret = check_reserved(desc, "get_flags");
739
740
741
	if (ret)
		return ret;

742
	/* GPIOD_ are directly provided by driver except GPIOD_ACTIVE_LOW */
743
744
	if (ops->get_flags) {
		ret = ops->get_flags(dev, desc->offset, &flags);
745
746
		if (ret)
			return ret;
747

748
		/* GPIOD_ACTIVE_LOW is saved in desc->flags */
749
		value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
750
751
		if (desc->flags & GPIOD_ACTIVE_LOW)
			value = !value;
752
753
		flags &= ~(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
		flags |= (desc->flags & GPIOD_ACTIVE_LOW);
754
		if (value)
755
			flags |= GPIOD_IS_OUT_ACTIVE;
756
	} else {
757
		flags = desc->flags;
758
		/* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
759
		flags &= ~GPIOD_IS_OUT_ACTIVE;
760
		if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
761
			flags |= GPIOD_IS_OUT_ACTIVE;
762
	}
763
	*flagsp = flags;
764
765
766
767

	return 0;
}

Simon Glass's avatar
Simon Glass committed
768
769
770
771
772
773
774
775
776
777
778
779
780
/**
 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
 * gpio:	GPIO number
 *
 * This function implements the API that's compatible with current
 * GPIO API used in U-Boot. The request is forwarded to particular
 * GPIO driver. Returns the value of the GPIO pin, or negative value
 * on error.
 */
int gpio_get_value(unsigned gpio)
{
	int ret;

781
782
783
	struct gpio_desc desc;

	ret = gpio_to_device(gpio, &desc);
Simon Glass's avatar
Simon Glass committed
784
785
	if (ret)
		return ret;
786
	return dm_gpio_get_value(&desc);
Simon Glass's avatar
Simon Glass committed
787
788
789
790
791
792
793
794
795
796
797
798
799
}

/**
 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
 * gpio:	GPIO number
 * value:	Logical value to be set on the GPIO pin.
 *
 * This function implements the API that's compatible with current
 * GPIO API used in U-Boot. The request is forwarded to particular
 * GPIO driver. Returns 0 on success, negative value on error.
 */
int gpio_set_value(unsigned gpio, int value)
{
800
	struct gpio_desc desc;
Simon Glass's avatar
Simon Glass committed
801
802
	int ret;

803
	ret = gpio_to_device(gpio, &desc);
Simon Glass's avatar
Simon Glass committed
804
805
	if (ret)
		return ret;
806
	return dm_gpio_set_value(&desc, value);
Simon Glass's avatar
Simon Glass committed
807
808
}

809
const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
Simon Glass's avatar
Simon Glass committed
810
811
812
813
{
	struct gpio_dev_priv *priv;

	/* Must be called on an active device */
814
	priv = dev_get_uclass_priv(dev);
Simon Glass's avatar
Simon Glass committed
815
816
817
818
819
820
	assert(priv);

	*bit_count = priv->gpio_count;
	return priv->bank_name;
}

821
822
823
824
825
826
827
828
static const char * const gpio_function[GPIOF_COUNT] = {
	"input",
	"output",
	"unused",
	"unknown",
	"func",
};

829
830
static int get_function(struct udevice *dev, int offset, bool skip_unused,
			const char **namep)
831
{
832
	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
833
	const struct dm_gpio_ops *ops = gpio_get_ops(dev);
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867

	BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
	if (!device_active(dev))
		return -ENODEV;
	if (offset < 0 || offset >= uc_priv->gpio_count)
		return -EINVAL;
	if (namep)
		*namep = uc_priv->name[offset];
	if (skip_unused && !uc_priv->name[offset])
		return GPIOF_UNUSED;
	if (ops->get_function) {
		int ret;

		ret = ops->get_function(dev, offset);
		if (ret < 0)
			return ret;
		if (ret >= ARRAY_SIZE(gpio_function))
			return -ENODATA;
		return ret;
	}

	return GPIOF_UNKNOWN;
}

int gpio_get_function(struct udevice *dev, int offset, const char **namep)
{
	return get_function(dev, offset, true, namep);
}

int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
{
	return get_function(dev, offset, false, namep);
}

868
869
int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
{
870
	const struct dm_gpio_ops *ops = gpio_get_ops(dev);
871
872
873
874
875
876
877
878
879
	struct gpio_dev_priv *priv;
	char *str = buf;
	int func;
	int ret;
	int len;

	BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));

	*buf = 0;
880
	priv = dev_get_uclass_priv(dev);
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
	ret = gpio_get_raw_function(dev, offset, NULL);
	if (ret < 0)
		return ret;
	func = ret;
	len = snprintf(str, buffsize, "%s%d: %s",
		       priv->bank_name ? priv->bank_name : "",
		       offset, gpio_function[func]);
	if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
	    func == GPIOF_UNUSED) {
		const char *label;
		bool used;

		ret = ops->get_value(dev, offset);
		if (ret < 0)
			return ret;
		used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
		snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
			 ret,
			 used ? 'x' : ' ',
			 used ? " " : "",
			 label ? label : "");
	}

	return 0;
}

907
908
909
#if CONFIG_IS_ENABLED(ACPIGEN)
int gpio_get_acpi(const struct gpio_desc *desc, struct acpi_gpio *gpio)
{
910
	const struct dm_gpio_ops *ops;
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927

	memset(gpio, '\0', sizeof(*gpio));
	if (!dm_gpio_is_valid(desc)) {
		/* Indicate that the GPIO is not valid */
		gpio->pin_count = 0;
		gpio->pins[0] = 0;
		return -EINVAL;
	}

	ops = gpio_get_ops(desc->dev);
	if (!ops->get_acpi)
		return -ENOSYS;

	return ops->get_acpi(desc, gpio);
}
#endif

928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
{
	int i, ret;
	int gpio;

	for (i = 0; i < 32; i++) {
		gpio = gpio_num_array[i];
		if (gpio == -1)
			break;
		ret = gpio_requestf(gpio, fmt, i);
		if (ret)
			goto err;
		ret = gpio_direction_input(gpio);
		if (ret) {
			gpio_free(gpio);
			goto err;
		}
	}

	return 0;
err:
	for (i--; i >= 0; i--)
		gpio_free(gpio_num_array[i]);

	return ret;
}

955
956
957
958
/*
 * get a number comprised of multiple GPIO values. gpio_num_array points to
 * the array of gpio pin numbers to scan, terminated by -1.
 */
959
int gpio_get_values_as_int(const int *gpio_list)
960
961
962
963
{
	int gpio;
	unsigned bitmask = 1;
	unsigned vector = 0;
964
	int ret;
965
966

	while (bitmask &&
967
968
969
970
971
	       ((gpio = *gpio_list++) != -1)) {
		ret = gpio_get_value(gpio);
		if (ret < 0)
			return ret;
		else if (ret)
972
973
974
			vector |= bitmask;
		bitmask <<= 1;
	}
975

976
977
978
	return vector;
}

Simon Glass's avatar
Simon Glass committed
979
int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
{
	unsigned bitmask = 1;
	unsigned vector = 0;
	int ret, i;

	for (i = 0; i < count; i++) {
		ret = dm_gpio_get_value(&desc_list[i]);
		if (ret < 0)
			return ret;
		else if (ret)
			vector |= bitmask;
		bitmask <<= 1;
	}

	return vector;
}

997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
int dm_gpio_get_values_as_int_base3(struct gpio_desc *desc_list,
				    int count)
{
	static const char tristate[] = "01z";
	enum {
		PULLUP,
		PULLDOWN,

		NUM_OPTIONS,
	};
	int vals[NUM_OPTIONS];
	uint mask;
	uint vector = 0;
	int ret, i;

	/*
	 * Limit to 19 digits which should be plenty. This avoids overflow of a
	 * 32-bit int
	 */
	assert(count < 20);

	for (i = 0; i < NUM_OPTIONS; i++) {
		uint flags = GPIOD_IS_IN;

		flags |= (i == PULLDOWN) ? GPIOD_PULL_DOWN : GPIOD_PULL_UP;
		ret = dm_gpios_clrset_flags(desc_list, count, GPIOD_MASK_PULL,
					    flags);
		if (ret)
			return log_msg_ret("pu", ret);

		/* Give the lines time to settle */
		udelay(10);

		ret = dm_gpio_get_values_as_int(desc_list, count);
		if (ret < 0)
			return log_msg_ret("get1", ret);
		vals[i] = ret;
	}

	log_debug("values: %x %x, count = %d\n", vals[0], vals[1], count);
	for (i = count - 1, mask = 1 << i; i >= 0; i--, mask >>= 1) {
		uint pd = vals[PULLDOWN] & mask ? 1 : 0;
		uint pu = vals[PULLUP] & mask ? 1 : 0;
		uint digit;

		/*
		 * Get value with internal pulldown active. If this is 1 then
		 * there is a stronger external pullup, which we call 1. If not
		 * then call it 0.
		 *
		 * If the values differ then the pin is floating so we call
		 * this a 2.
		 */
		if (pu == pd)
			digit = pd;
		else
			digit = 2;
		log_debug("%c ", tristate[digit]);
		vector = 3 * vector + digit;
	}
	log_debug("vector=%d\n", vector);

	return vector;
}

1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
/**
 * gpio_request_tail: common work for requesting a gpio.
 *
 * ret:		return value from previous work in function which calls
 *		this function.
 *		This seems bogus (why calling this function instead not
 *		calling it and end caller function instead?).
 *		Because on error in caller function we want to set some
 *		default values in gpio desc and have a common error
 *		debug message, which provides this function.
 * nodename:	Name of node for which gpio gets requested
 *		used for gpio label name.
 * args:	pointer to output arguments structure
 * list_name:	Name of GPIO list
 *		used for gpio label name.
 * index:	gpio index in gpio list
 *		used for gpio label name.
 * desc:	pointer to gpio descriptor, filled from this
 *		function.
 * flags:	gpio flags to use.
 * add_index:	should index added to gpio label name
 * gpio_dev:	pointer to gpio device from which the gpio
 *		will be requested. If NULL try to get the
 *		gpio device with uclass_get_device_by_ofnode()
 *
 * return:	In error case this function sets default values in
 *		gpio descriptor, also emmits a debug message.
 *		On success it returns 0 else the error code from
 *		function calls, or the error code passed through
 *		ret to this function.
 *
 */
Heiko Schocher's avatar
Heiko Schocher committed
1094
static int gpio_request_tail(int ret, const char *nodename,
1095
1096
			     struct ofnode_phandle_args *args,
			     const char *list_name, int index,
Heiko Schocher's avatar
Heiko Schocher committed
1097
			     struct gpio_desc *desc, int flags,
1098
			     bool add_index, struct udevice *gpio_dev)
1099
{
1100
	gpio_desc_init(desc, gpio_dev, 0);
1101
	if (ret)
1102
1103
		goto err;

Heiko Schocher's avatar
Heiko Schocher committed
1104
1105
1106
1107
	if (!desc->dev) {
		ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
						  &desc->dev);
		if (ret) {
1108
1109
			debug("%s: uclass_get_device_by_ofnode failed\n",
			      __func__);
Heiko Schocher's avatar
Heiko Schocher committed
1110
1111
			goto err;
		}
1112
	}
1113
	ret = gpio_find_and_xlate(desc, args);
1114
1115
1116
1117
1118
	if (ret) {
		debug("%s: gpio_find_and_xlate failed\n", __func__);
		goto err;
	}
	ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
Heiko Schocher's avatar
Heiko Schocher committed
1119
			       nodename, list_name, index);
1120
1121
1122
1123
	if (ret) {
		debug("%s: dm_gpio_requestf failed\n", __func__);
		goto err;
	}
1124
1125
1126
1127

	/* Keep any direction flags provided by the devicetree */
	ret = dm_gpio_set_dir_flags(desc,
				    flags | (desc->flags & GPIOD_MASK_DIR));
1128
1129
1130
1131
1132
1133
1134
1135
	if (ret) {
		debug("%s: dm_gpio_set_dir failed\n", __func__);
		goto err;
	}

	return 0;
err:
	debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
Heiko Schocher's avatar
Heiko Schocher committed
1136
	      __func__, nodename, list_name, index, ret);
1137
1138
1139
	return ret;
}

1140
#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass's avatar
Simon Glass committed
1141
1142
1143
static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
				       int index, struct gpio_desc *desc,
				       int flags, bool add_index)
1144
1145
1146
1147
{
	struct ofnode_phandle_args args;
	int ret;

Simon Glass's avatar
Simon Glass committed
1148
1149
	ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
					     index, &args);
1150

Heiko Schocher's avatar
Heiko Schocher committed
1151
1152
	return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
				 index, desc, flags, add_index, NULL);
1153
1154
}

Simon Glass's avatar
Simon Glass committed
1155
int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
1156
1157
			       struct gpio_desc *desc, int flags)
{
Simon Glass's avatar
Simon Glass committed
1158
1159
	return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
					   index > 0);
1160
1161
}

Simon Glass's avatar
Simon Glass committed
1162
int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
1163
1164
			 struct gpio_desc *desc, int flags)
{
Simon Glass's avatar
Simon Glass committed
1165
	struct ofnode_phandle_args args;
Heiko Schocher's avatar
Heiko Schocher committed
1166
	ofnode node;
Simon Glass's avatar
Simon Glass committed
1167
1168
1169
1170
	int ret;

	ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
					 index, &args);
Heiko Schocher's avatar
Heiko Schocher committed
1171
1172
1173
	node = dev_ofnode(dev);
	return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
				 index, desc, flags, index > 0, NULL);
1174
1175
}

Simon Glass's avatar
Simon Glass committed
1176
int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
1177
1178
1179
1180
1181
1182
				    struct gpio_desc *desc, int max_count,
				    int flags)
{
	int count;
	int ret;

1183
	for (count = 0; count < max_count; count++) {
Simon Glass's avatar
Simon Glass committed
1184
		ret = _gpio_request_by_name_nodev(node, list_name, count,
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
						  &desc[count], flags, true);
		if (ret == -ENOENT)
			break;
		else if (ret)
			goto err;
	}

	/* We ran out of GPIOs in the list */
	return count;

err:
	gpio_free_list_nodev(desc, count - 1);

	return ret;
}

int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
			      struct gpio_desc *desc, int max_count,
			      int flags)
{
	/*
	 * This isn't ideal since we don't use dev->name in the debug()
	 * calls in gpio_request_by_name(), but we can do this until
	 * gpio_request_list_by_name_nodev() can be dropped.
	 */
Simon Glass's avatar
Simon Glass committed
1210
1211
	return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
					       max_count, flags);
1212
1213
1214
1215
1216
1217
}

int gpio_get_list_count(struct udevice *dev, const char *list_name)
{
	int ret;

1218
1219
	ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0, -1,
					 NULL);
1220
1221
1222
1223
1224
1225
1226
	if (ret) {
		debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
		      __func__, dev->name, list_name, ret);
	}

	return ret;
}
1227
#endif /* OF_PLATDATA */
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250

int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
{
	/* For now, we don't do any checking of dev */
	return _dm_gpio_free(desc->dev, desc->offset);
}

int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
{
	int i;

	/* For now, we don't do any checking of dev */
	for (i = 0; i < count; i++)
		dm_gpio_free(dev, &desc[i]);

	return 0;
}

int gpio_free_list_nodev(struct gpio_desc *desc, int count)
{
	return gpio_free_list(NULL, desc, count);
}

Simon Glass's avatar
Simon Glass committed
1251
/* We need to renumber the GPIOs when any driver is probed/removed */
1252
static int gpio_renumber(struct udevice *removed_dev)
Simon Glass's avatar
Simon Glass committed
1253
1254
{
	struct gpio_dev_priv *uc_priv;
1255
	struct udevice *dev;
Simon Glass's avatar
Simon Glass committed
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
	struct uclass *uc;
	unsigned base;
	int ret;

	ret = uclass_get(UCLASS_GPIO, &uc);
	if (ret)
		return ret;

	/* Ensure that we have a base for each bank */
	base = 0;
	uclass_foreach_dev(dev, uc) {
1267
		if (device_active(dev) && dev != removed_dev) {
1268
			uc_priv = dev_get_uclass_priv(dev);
Simon Glass's avatar
Simon Glass committed
1269
1270
1271
1272
1273
1274
1275
1276
			uc_priv->gpio_base = base;
			base += uc_priv->gpio_count;
		}
	}

	return 0;
}

Simon Glass's avatar
Simon Glass committed
1277
int gpio_get_number(const struct gpio_desc *desc)
1278
1279
1280
1281
1282
1283
{
	struct udevice *dev = desc->dev;
	struct gpio_dev_priv *uc_priv;

	if (!dev)
		return -1;
1284
	uc_priv = dev_get_uclass_priv(dev);
1285
1286
1287
1288

	return uc_priv->gpio_base + desc->offset;
}

1289
static int gpio_post_probe(struct udevice *dev)
Simon Glass's avatar
Simon Glass committed
1290
{
1291
	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1292
1293
1294
1295
1296
1297

	uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
	if (!uc_priv->name)
		return -ENOMEM;

	return gpio_renumber(NULL);
Simon Glass's avatar
Simon Glass committed
1298
1299
}

1300
static int gpio_pre_remove(struct udevice *dev)
Simon Glass's avatar
Simon Glass committed
1301
{
1302
	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1303
1304
1305
1306
1307
1308
1309
1310
1311
	int i;

	for (i = 0; i < uc_priv->gpio_count; i++) {
		if (uc_priv->name[i])
			free(uc_priv->name[i]);
	}
	free(uc_priv->name);

	return gpio_renumber(dev);
Simon Glass's avatar
Simon Glass committed
1312
1313
}

Heiko Schocher's avatar
Heiko Schocher committed
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
int gpio_dev_request_index(struct udevice *dev, const char *nodename,
			   char *list_name, int index, int flags,
			   int dtflags, struct gpio_desc *desc)
{
	struct ofnode_phandle_args args;

	args.node =  ofnode_null();
	args.args_count = 2;
	args.args[0] = index;
	args.args[1] = dtflags;

	return gpio_request_tail(0, nodename, &args, list_name, index, desc,
				 flags, 0, dev);
}

1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
static void devm_gpiod_release(struct udevice *dev, void *res)
{
	dm_gpio_free(dev, res);
}

static int devm_gpiod_match(struct udevice *dev, void *res, void *data)
{
	return res == data;
}

struct gpio_desc *devm_gpiod_get_index(struct udevice *dev, const char *id,
				       unsigned int index, int flags)
{
	int rc;
	struct gpio_desc *desc;
	char *propname;
	static const char suffix[] = "-gpios";

	propname = malloc(strlen(id) + sizeof(suffix));
	if (!propname) {
		rc = -ENOMEM;
		goto end;
	}

	strcpy(propname, id);
	strcat(propname, suffix);

	desc = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc),
			    __GFP_ZERO);
	if (unlikely(!desc)) {
		rc = -ENOMEM;
		goto end;
	}

	rc = gpio_request_by_name(dev, propname, index, desc, flags);

end:
	if (propname)
		free(propname);

	if (rc)
		return ERR_PTR(rc);

	devres_add(dev, desc);

	return desc;
}

struct gpio_desc *devm_gpiod_get_index_optional(struct udevice *dev,
						const char *id,
						unsigned int index,
						int flags)
{
	struct gpio_desc *desc = devm_gpiod_get_index(dev, id, index, flags);

	if (IS_ERR(desc))
		return NULL;

	return desc;
}

void devm_gpiod_put(struct udevice *dev, struct gpio_desc *desc)
{
	int rc;

	rc = devres_release(dev, devm_gpiod_release, devm_gpiod_match, desc);
	WARN_ON(rc);
}