summaryrefslogtreecommitdiff
path: root/lib/utils/gpio/gpio.c
blob: fb30c0f7b16ad8e2208429d4f09f36a71fcdc2e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
/*
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2021 Western Digital Corporation or its affiliates.
 *
 * Authors:
 *   Anup Patel <anup.patel@wdc.com>
 */

#include <sbi/sbi_error.h>
#include <sbi_utils/gpio/gpio.h>

#define GPIO_CHIP_MAX		16

static struct gpio_chip *gc_array[GPIO_CHIP_MAX];

struct gpio_chip *gpio_chip_find(unsigned int id)
{
	unsigned int i;
	struct gpio_chip *ret = NULL;

	for (i = 0; i < GPIO_CHIP_MAX; i++) {
		if (gc_array[i] && gc_array[i]->id == id) {
			ret = gc_array[i];
			break;
		}
	}

	return ret;
}

int gpio_chip_add(struct gpio_chip *gc)
{
	int i, ret = SBI_ENOSPC;

	if (!gc)
		return SBI_EINVAL;
	if (gpio_chip_find(gc->id))
		return SBI_EALREADY;

	for (i = 0; i < GPIO_CHIP_MAX; i++) {
		if (!gc_array[i]) {
			gc_array[i] = gc;
			ret = 0;
			break;
		}
	}

	return ret;
}

void gpio_chip_remove(struct gpio_chip *gc)
{
	int i;

	if (!gc)
		return;

	for (i = 0; i < GPIO_CHIP_MAX; i++) {
		if (gc_array[i] == gc) {
			gc_array[i] = NULL;
			break;
		}
	}
}

int gpio_get_direction(struct gpio_pin *gp)
{
	if (!gp || !gp->chip || (gp->chip->ngpio <= gp->offset))
		return SBI_EINVAL;
	if (!gp->chip->get_direction)
		return SBI_ENOSYS;

	return gp->chip->get_direction(gp);
}

int gpio_direction_input(struct gpio_pin *gp)
{
	if (!gp || !gp->chip || (gp->chip->ngpio <= gp->offset))
		return SBI_EINVAL;
	if (!gp->chip->direction_input)
		return SBI_ENOSYS;

	return gp->chip->direction_input(gp);
}

int gpio_direction_output(struct gpio_pin *gp, int value)
{
	if (!gp || !gp->chip || (gp->chip->ngpio <= gp->offset))
		return SBI_EINVAL;
	if (!gp->chip->direction_output)
		return SBI_ENOSYS;

	return gp->chip->direction_output(gp, value);
}

int gpio_get(struct gpio_pin *gp)
{
	if (!gp || !gp->chip || (gp->chip->ngpio <= gp->offset))
		return SBI_EINVAL;
	if (!gp->chip->get)
		return SBI_ENOSYS;

	return gp->chip->get(gp);
}

int gpio_set(struct gpio_pin *gp, int value)
{
	if (!gp || !gp->chip || (gp->chip->ngpio <= gp->offset))
		return SBI_EINVAL;
	if (!gp->chip->set)
		return SBI_ENOSYS;

	gp->chip->set(gp, value);
	return 0;
}