summaryrefslogtreecommitdiffstats
path: root/drivers/gpio/gpio.c
blob: ed9f9236eb4383311374415c88a2435e925c460f (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <common.h>
#include <gpio.h>
#include <errno.h>
#include <malloc.h>

static LIST_HEAD(chip_list);

struct gpio_info {
	struct gpio_chip *chip;
	bool requested;
	char *label;
};

static struct gpio_info gpio_desc[ARCH_NR_GPIOS];

static int gpio_ensure_requested(struct gpio_info *gi, int gpio)
{
	if (gi->requested)
		return 0;

	return gpio_request(gpio, "gpio");
}

int gpio_request(unsigned gpio, const char *label)
{
	struct gpio_info *gi = &gpio_desc[gpio];
	struct gpio_chip *chip = gi->chip;
	int ret;

	if (!gpio_is_valid(gpio))
		return -EINVAL;
	if (!chip)
		return -EINVAL;
	if (gi->requested)
		return -EBUSY;
	if (chip->ops->request) {
		ret = chip->ops->request(chip, gpio - chip->base);
		if (ret)
			return ret;
	}

	gi->requested = true;
	gi->label = xstrdup(label);

	return 0;
}

void gpio_free(unsigned gpio)
{
	struct gpio_info *gi = &gpio_desc[gpio];
	struct gpio_chip *chip = gi->chip;

	if (!gpio_is_valid(gpio))
		return;
	if (!chip)
		return;
	if (!gi->requested)
		return;
	if (chip->ops->free)
		chip->ops->free(chip, gpio - chip->base);

	gi->requested = false;
	free(gi->label);
}

void gpio_set_value(unsigned gpio, int value)
{
	struct gpio_info *gi = &gpio_desc[gpio];
	struct gpio_chip *chip = gi->chip;

	if (!gpio_is_valid(gpio))
		return;
	if (!chip)
		return;
	if (gpio_ensure_requested(gi, gpio))
		return;
	if (!chip->ops->set)
		return;
	chip->ops->set(chip, gpio - chip->base, value);
}
EXPORT_SYMBOL(gpio_set_value);

int gpio_get_value(unsigned gpio)
{
	struct gpio_info *gi = &gpio_desc[gpio];
	struct gpio_chip *chip = gi->chip;
	int ret;

	if (!gpio_is_valid(gpio))
		return -EINVAL;
	if (!chip)
		return -ENODEV;
	ret = gpio_ensure_requested(gi, gpio);
	if (ret)
		return ret;
	if (!chip->ops->get)
		return -ENOSYS;
	return chip->ops->get(chip, gpio - chip->base);
}
EXPORT_SYMBOL(gpio_get_value);

int gpio_direction_output(unsigned gpio, int value)
{
	struct gpio_info *gi = &gpio_desc[gpio];
	struct gpio_chip *chip = gi->chip;
	int ret;

	if (!gpio_is_valid(gpio))
		return -EINVAL;
	if (!chip)
		return -ENODEV;
	ret = gpio_ensure_requested(gi, gpio);
	if (ret)
		return ret;
	if (!chip->ops->direction_output)
		return -ENOSYS;
	return chip->ops->direction_output(chip, gpio - chip->base, value);
}
EXPORT_SYMBOL(gpio_direction_output);

int gpio_direction_input(unsigned gpio)
{
	struct gpio_info *gi = &gpio_desc[gpio];
	struct gpio_chip *chip = gi->chip;
	int ret;

	if (!gpio_is_valid(gpio))
		return -EINVAL;
	if (!chip)
		return -ENODEV;
	ret = gpio_ensure_requested(gi, gpio);
	if (ret)
		return ret;
	if (!chip->ops->direction_input)
		return -ENOSYS;
	return chip->ops->direction_input(chip, gpio - chip->base);
}
EXPORT_SYMBOL(gpio_direction_input);

static int gpiochip_find_base(int start, int ngpio)
{
	int i;
	int spare = 0;
	int base = -ENOSPC;

	if (start < 0)
		start = 0;

	for (i = start; i < ARCH_NR_GPIOS; i++) {
		struct gpio_chip *chip = gpio_desc[i].chip;

		if (!chip) {
			spare++;
			if (spare == ngpio) {
				base = i + 1 - ngpio;
				break;
			}
		} else {
			spare = 0;
			i += chip->ngpio - 1;
		}
	}

	if (gpio_is_valid(base))
		debug("%s: found new base at %d\n", __func__, base);
	return base;
}

int gpiochip_add(struct gpio_chip *chip)
{
	int base, i;

	base = gpiochip_find_base(chip->base, chip->ngpio);
	if (base < 0)
		return base;

	if (chip->base >= 0 && chip->base != base)
		return -EBUSY;

	chip->base = base;

	list_add_tail(&chip->list, &chip_list);

	for (i = chip->base; i < chip->base + chip->ngpio; i++)
		gpio_desc[i].chip = chip;

	return 0;
}

int gpio_get_num(struct device_d *dev, int gpio)
{
	struct gpio_chip *chip;

	list_for_each_entry(chip, &chip_list, list) {
		if (chip->dev == dev)
			return chip->base + gpio;
	}

	return -ENODEV;
}