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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
/* $OpenBSD: ofw_regulator.c,v 1.10 2019/01/02 18:50:15 patrick Exp $ */
/*
* Copyright (c) 2016 Mark Kettenis
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_gpio.h>
#include <dev/ofw/ofw_pinctrl.h>
#include <dev/ofw/ofw_regulator.h>
LIST_HEAD(, regulator_device) regulator_devices =
LIST_HEAD_INITIALIZER(regulator_devices);
uint32_t regulator_gpio_get_voltage(int);
int regulator_gpio_set_voltage(int, uint32_t);
void
regulator_register(struct regulator_device *rd)
{
rd->rd_min = OF_getpropint(rd->rd_node, "regulator-min-microvolt", 0);
rd->rd_max = OF_getpropint(rd->rd_node, "regulator-max-microvolt", ~0);
KASSERT(rd->rd_min <= rd->rd_max);
rd->rd_ramp_delay =
OF_getpropint(rd->rd_node, "regulator-ramp-delay", 0);
if (rd->rd_get_voltage && rd->rd_set_voltage) {
uint32_t voltage = rd->rd_get_voltage(rd->rd_cookie);
if (voltage < rd->rd_min)
rd->rd_set_voltage(rd->rd_cookie, rd->rd_min);
if (voltage > rd->rd_max)
rd->rd_set_voltage(rd->rd_cookie, rd->rd_max);
}
rd->rd_phandle = OF_getpropint(rd->rd_node, "phandle", 0);
if (rd->rd_phandle == 0)
return;
LIST_INSERT_HEAD(®ulator_devices, rd, rd_list);
}
int
regulator_fixed_set(int node, int enable)
{
uint32_t *gpio;
uint32_t startup_delay;
int active;
int len;
pinctrl_byname(node, "default");
if (OF_getproplen(node, "enable-active-high") == 0)
active = 1;
else
active = 0;
/* The "gpio" property is optional. */
len = OF_getproplen(node, "gpio");
if (len < 0)
return 0;
gpio = malloc(len, M_TEMP, M_WAITOK);
OF_getpropintarray(node, "gpio", gpio, len);
gpio_controller_config_pin(gpio, GPIO_CONFIG_OUTPUT);
if (enable)
gpio_controller_set_pin(gpio, active);
else
gpio_controller_set_pin(gpio, !active);
free(gpio, M_TEMP, len);
startup_delay = OF_getpropint(node, "startup-delay-us", 0);
if (enable && startup_delay > 0)
delay(startup_delay);
return 0;
}
int
regulator_set(uint32_t phandle, int enable)
{
struct regulator_device *rd;
int node;
node = OF_getnodebyphandle(phandle);
if (node == 0)
return ENODEV;
/* Don't mess around with regulators that are always on. */
if (OF_getproplen(node, "regulator-always-on") == 0)
return 0;
LIST_FOREACH(rd, ®ulator_devices, rd_list) {
if (rd->rd_phandle == phandle)
break;
}
if (rd && rd->rd_enable)
return rd->rd_enable(rd->rd_cookie, enable);
if (OF_is_compatible(node, "regulator-fixed"))
return regulator_fixed_set(node, enable);
return ENODEV;
}
int
regulator_enable(uint32_t phandle)
{
return regulator_set(phandle, 1);
}
int
regulator_disable(uint32_t phandle)
{
return regulator_set(phandle, 0);
}
uint32_t
regulator_get_voltage(uint32_t phandle)
{
struct regulator_device *rd;
int node;
LIST_FOREACH(rd, ®ulator_devices, rd_list) {
if (rd->rd_phandle == phandle)
break;
}
if (rd && rd->rd_get_voltage)
return rd->rd_get_voltage(rd->rd_cookie);
node = OF_getnodebyphandle(phandle);
if (node == 0)
return 0;
if (OF_is_compatible(node, "regulator-fixed"))
return OF_getpropint(node, "regulator-min-microvolt", 0);
if (OF_is_compatible(node, "regulator-gpio"))
return regulator_gpio_get_voltage(node);
return 0;
}
int
regulator_set_voltage(uint32_t phandle, uint32_t voltage)
{
struct regulator_device *rd;
uint32_t old, delta;
int error, node;
LIST_FOREACH(rd, ®ulator_devices, rd_list) {
if (rd->rd_phandle == phandle)
break;
}
/* Check limits. */
if (rd && (voltage < rd->rd_min || voltage > rd->rd_max))
return EINVAL;
if (rd && rd->rd_set_voltage) {
old = rd->rd_get_voltage(rd->rd_cookie);
error = rd->rd_set_voltage(rd->rd_cookie, voltage);
if (voltage > old && rd->rd_ramp_delay > 0) {
delta = voltage - old;
delay(howmany(delta, rd->rd_ramp_delay));
}
return error;
}
node = OF_getnodebyphandle(phandle);
if (node == 0)
return ENODEV;
if (OF_is_compatible(node, "regulator-fixed") &&
OF_getpropint(node, "regulator-min-microvolt", 0) == voltage)
return 0;
if (OF_is_compatible(node, "regulator-gpio"))
return regulator_gpio_set_voltage(node, voltage);
return ENODEV;
}
uint32_t
regulator_gpio_get_voltage(int node)
{
uint32_t *gpio, *gpios, *states;
uint32_t idx, voltage;
size_t glen, slen;
int i;
pinctrl_byname(node, "default");
if ((glen = OF_getproplen(node, "gpios")) <= 0)
return EINVAL;
if ((slen = OF_getproplen(node, "states")) <= 0)
return EINVAL;
if (slen % (2 * sizeof(uint32_t)) != 0)
return EINVAL;
gpios = malloc(glen, M_TEMP, M_WAITOK);
states = malloc(slen, M_TEMP, M_WAITOK);
OF_getpropintarray(node, "gpios", gpios, glen);
OF_getpropintarray(node, "states", states, slen);
i = 0;
idx = 0;
gpio = gpios;
while (gpio && gpio < gpios + (glen / sizeof(uint32_t))) {
idx |= (1 << i);
gpio = gpio_controller_next_pin(gpio);
i++;
}
voltage = 0;
for (i = 0; i < slen / (2 * sizeof(uint32_t)); i++) {
if (states[2 * i + 1] == idx) {
voltage = states[2 * i];
break;
}
}
if (i >= slen / (2 * sizeof(uint32_t)))
return 0;
free(gpios, M_TEMP, glen);
free(states, M_TEMP, slen);
return voltage;
}
int
regulator_gpio_set_voltage(int node, uint32_t voltage)
{
uint32_t *gpio, *gpios, *states;
size_t glen, slen;
uint32_t min, max;
uint32_t idx;
int i;
pinctrl_byname(node, "default");
/* Check limits. */
min = OF_getpropint(node, "regulator-min-microvolt", 0);
max = OF_getpropint(node, "regulator-max-microvolt", 0);
if (voltage < min || voltage > max)
return EINVAL;
if ((glen = OF_getproplen(node, "gpios")) <= 0)
return EINVAL;
if ((slen = OF_getproplen(node, "states")) <= 0)
return EINVAL;
if (slen % (2 * sizeof(uint32_t)) != 0)
return EINVAL;
gpios = malloc(glen, M_TEMP, M_WAITOK);
states = malloc(slen, M_TEMP, M_WAITOK);
OF_getpropintarray(node, "gpios", gpios, glen);
OF_getpropintarray(node, "states", states, slen);
idx = 0;
for (i = 0; i < slen / (2 * sizeof(uint32_t)); i++) {
if (states[2 * i] < min || states[2 * i] > max)
continue;
if (states[2 * i] == voltage) {
idx = states[2 * i + 1];
break;
}
}
if (i >= slen / (2 * sizeof(uint32_t)))
return EINVAL;
i = 0;
gpio = gpios;
while (gpio && gpio < gpios + (glen / sizeof(uint32_t))) {
gpio_controller_set_pin(gpio, !!(idx & (1 << i)));
gpio = gpio_controller_next_pin(gpio);
i++;
}
free(gpios, M_TEMP, glen);
free(states, M_TEMP, slen);
return 0;
}
|