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
|
/* $NetBSD: fdt_gpio.c,v 1.7 2020/12/23 04:07:34 thorpej Exp $ */
/*-
* Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: fdt_gpio.c,v 1.7 2020/12/23 04:07:34 thorpej Exp $");
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/kmem.h>
#include <sys/queue.h>
#include <libfdt.h>
#include <dev/fdt/fdtvar.h>
struct fdtbus_gpio_controller {
device_t gc_dev;
int gc_phandle;
const struct fdtbus_gpio_controller_func *gc_funcs;
LIST_ENTRY(fdtbus_gpio_controller) gc_next;
};
static LIST_HEAD(, fdtbus_gpio_controller) fdtbus_gpio_controllers =
LIST_HEAD_INITIALIZER(fdtbus_gpio_controllers);
int
fdtbus_register_gpio_controller(device_t dev, int phandle,
const struct fdtbus_gpio_controller_func *funcs)
{
struct fdtbus_gpio_controller *gc;
gc = kmem_alloc(sizeof(*gc), KM_SLEEP);
gc->gc_dev = dev;
gc->gc_phandle = phandle;
gc->gc_funcs = funcs;
LIST_INSERT_HEAD(&fdtbus_gpio_controllers, gc, gc_next);
return 0;
}
static struct fdtbus_gpio_controller *
fdtbus_get_gpio_controller(int phandle)
{
struct fdtbus_gpio_controller *gc;
LIST_FOREACH(gc, &fdtbus_gpio_controllers, gc_next) {
if (gc->gc_phandle == phandle)
return gc;
}
return NULL;
}
int
fdtbus_gpio_count(int phandle, const char *prop)
{
const uint32_t *gpios, *p;
u_int n, gpio_cells;
int len, resid;
gpios = fdtbus_get_prop(phandle, prop, &len);
if (gpios == NULL)
return 0;
p = gpios;
for (n = 0, resid = len; resid > 0; n++) {
const int gc_phandle =
fdtbus_get_phandle_from_native(be32toh(p[0]));
if (of_getprop_uint32(gc_phandle, "#gpio-cells", &gpio_cells))
break;
resid -= (gpio_cells + 1) * 4;
p += gpio_cells + 1;
}
return n;
}
struct fdtbus_gpio_pin *
fdtbus_gpio_acquire(int phandle, const char *prop, int flags)
{
return fdtbus_gpio_acquire_index(phandle, prop, 0, flags);
}
struct fdtbus_gpio_pin *
fdtbus_gpio_acquire_index(int phandle, const char *prop,
int index, int flags)
{
struct fdtbus_gpio_controller *gc;
struct fdtbus_gpio_pin *gp = NULL;
const uint32_t *gpios, *p;
u_int n, gpio_cells;
int len, resid;
gpios = fdtbus_get_prop(phandle, prop, &len);
if (gpios == NULL)
return NULL;
p = gpios;
for (n = 0, resid = len; resid > 0; n++) {
const int gc_phandle =
fdtbus_get_phandle_from_native(be32toh(p[0]));
if (of_getprop_uint32(gc_phandle, "#gpio-cells", &gpio_cells))
break;
if (n == index) {
gc = fdtbus_get_gpio_controller(gc_phandle);
if (gc == NULL)
return NULL;
gp = kmem_alloc(sizeof(*gp), KM_SLEEP);
gp->gp_gc = gc;
gp->gp_priv = gc->gc_funcs->acquire(gc->gc_dev,
&p[0], (gpio_cells + 1) * 4, flags);
if (gp->gp_priv == NULL) {
kmem_free(gp, sizeof(*gp));
return NULL;
}
break;
}
resid -= (gpio_cells + 1) * 4;
p += gpio_cells + 1;
}
return gp;
}
void
fdtbus_gpio_release(struct fdtbus_gpio_pin *gp)
{
struct fdtbus_gpio_controller *gc = gp->gp_gc;
gc->gc_funcs->release(gc->gc_dev, gp->gp_priv);
kmem_free(gp, sizeof(*gp));
}
int
fdtbus_gpio_read(struct fdtbus_gpio_pin *gp)
{
struct fdtbus_gpio_controller *gc = gp->gp_gc;
return gc->gc_funcs->read(gc->gc_dev, gp->gp_priv, false);
}
void
fdtbus_gpio_write(struct fdtbus_gpio_pin *gp, int val)
{
struct fdtbus_gpio_controller *gc = gp->gp_gc;
gc->gc_funcs->write(gc->gc_dev, gp->gp_priv, val, false);
}
int
fdtbus_gpio_read_raw(struct fdtbus_gpio_pin *gp)
{
struct fdtbus_gpio_controller *gc = gp->gp_gc;
return gc->gc_funcs->read(gc->gc_dev, gp->gp_priv, true);
}
void
fdtbus_gpio_write_raw(struct fdtbus_gpio_pin *gp, int val)
{
struct fdtbus_gpio_controller *gc = gp->gp_gc;
gc->gc_funcs->write(gc->gc_dev, gp->gp_priv, val, true);
}
|