pwmregulator.c revision 1.2.2.2 1 1.2.2.2 thorpej /* $NetBSD: pwmregulator.c,v 1.2.2.2 2021/01/03 16:34:57 thorpej Exp $ */
2 1.2.2.2 thorpej
3 1.2.2.2 thorpej /*
4 1.2.2.2 thorpej * Copyright (c) 2020 Ryo Shimizu <ryo (at) nerv.org>
5 1.2.2.2 thorpej * All rights reserved.
6 1.2.2.2 thorpej *
7 1.2.2.2 thorpej * Redistribution and use in source and binary forms, with or without
8 1.2.2.2 thorpej * modification, are permitted provided that the following conditions
9 1.2.2.2 thorpej * are met:
10 1.2.2.2 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.2.2.2 thorpej * notice, this list of conditions and the following disclaimer.
12 1.2.2.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.2.2 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.2.2.2 thorpej * documentation and/or other materials provided with the distribution.
15 1.2.2.2 thorpej *
16 1.2.2.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17 1.2.2.2 thorpej * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 1.2.2.2 thorpej * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.2.2.2 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 1.2.2.2 thorpej * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 1.2.2.2 thorpej * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 1.2.2.2 thorpej * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.2.2.2 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 1.2.2.2 thorpej * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 1.2.2.2 thorpej * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.2.2.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
27 1.2.2.2 thorpej */
28 1.2.2.2 thorpej
29 1.2.2.2 thorpej #include <sys/cdefs.h>
30 1.2.2.2 thorpej __KERNEL_RCSID(0, "$NetBSD: pwmregulator.c,v 1.2.2.2 2021/01/03 16:34:57 thorpej Exp $");
31 1.2.2.2 thorpej
32 1.2.2.2 thorpej #include <sys/param.h>
33 1.2.2.2 thorpej #include <sys/systm.h>
34 1.2.2.2 thorpej #include <sys/device.h>
35 1.2.2.2 thorpej #include <sys/kmem.h>
36 1.2.2.2 thorpej
37 1.2.2.2 thorpej #include <dev/fdt/fdtvar.h>
38 1.2.2.2 thorpej #include <dev/pwm/pwmvar.h>
39 1.2.2.2 thorpej
40 1.2.2.2 thorpej static int pwmregulator_match(device_t, cfdata_t, void *);
41 1.2.2.2 thorpej static void pwmregulator_attach(device_t, device_t, void *);
42 1.2.2.2 thorpej
43 1.2.2.2 thorpej /* fdtbus_regulator_controller_func callback */
44 1.2.2.2 thorpej static int pwmregulator_acquire(device_t);
45 1.2.2.2 thorpej static void pwmregulator_release(device_t);
46 1.2.2.2 thorpej static int pwmregulator_enable(device_t, bool);
47 1.2.2.2 thorpej static int pwmregulator_set_voltage(device_t, u_int, u_int);
48 1.2.2.2 thorpej static int pwmregulator_get_voltage(device_t, u_int *);
49 1.2.2.2 thorpej
50 1.2.2.2 thorpej static const struct fdtbus_regulator_controller_func pwmregulator_funcs = {
51 1.2.2.2 thorpej .acquire = pwmregulator_acquire,
52 1.2.2.2 thorpej .release = pwmregulator_release,
53 1.2.2.2 thorpej .enable = pwmregulator_enable,
54 1.2.2.2 thorpej .set_voltage = pwmregulator_set_voltage,
55 1.2.2.2 thorpej .get_voltage = pwmregulator_get_voltage
56 1.2.2.2 thorpej };
57 1.2.2.2 thorpej
58 1.2.2.2 thorpej struct voltage_duty {
59 1.2.2.2 thorpej uint32_t microvolt;
60 1.2.2.2 thorpej uint32_t duty; /* percentage; 0-100 */
61 1.2.2.2 thorpej };
62 1.2.2.2 thorpej
63 1.2.2.2 thorpej struct pwmregulator_softc {
64 1.2.2.2 thorpej device_t sc_dev;
65 1.2.2.2 thorpej pwm_tag_t sc_pwm;
66 1.2.2.2 thorpej struct fdtbus_gpio_pin *sc_pin;
67 1.2.2.2 thorpej struct voltage_duty *sc_voltage_table;
68 1.2.2.2 thorpej int sc_voltage_table_num;
69 1.2.2.2 thorpej int sc_phandle;
70 1.2.2.2 thorpej uint32_t sc_microvolt_min;
71 1.2.2.2 thorpej uint32_t sc_microvolt_max;
72 1.2.2.2 thorpej uint32_t sc_dutycycle_unit;
73 1.2.2.2 thorpej uint32_t sc_dutycycle_range[2];
74 1.2.2.2 thorpej bool sc_always_on;
75 1.2.2.2 thorpej bool sc_boot_on;
76 1.2.2.2 thorpej };
77 1.2.2.2 thorpej
78 1.2.2.2 thorpej CFATTACH_DECL_NEW(pregulator, sizeof(struct pwmregulator_softc),
79 1.2.2.2 thorpej pwmregulator_match, pwmregulator_attach, NULL, NULL);
80 1.2.2.2 thorpej
81 1.2.2.2 thorpej static const char * const compatible[] = {
82 1.2.2.2 thorpej "pwm-regulator",
83 1.2.2.2 thorpej NULL
84 1.2.2.2 thorpej };
85 1.2.2.2 thorpej
86 1.2.2.2 thorpej static int
87 1.2.2.2 thorpej pwmregulator_match(device_t parent, cfdata_t cf, void *aux)
88 1.2.2.2 thorpej {
89 1.2.2.2 thorpej const struct fdt_attach_args *faa = aux;
90 1.2.2.2 thorpej
91 1.2.2.2 thorpej return of_match_compatible(faa->faa_phandle, compatible);
92 1.2.2.2 thorpej }
93 1.2.2.2 thorpej
94 1.2.2.2 thorpej static void
95 1.2.2.2 thorpej pwmregulator_attach(device_t parent, device_t self, void *aux)
96 1.2.2.2 thorpej {
97 1.2.2.2 thorpej struct pwmregulator_softc * const sc = device_private(self);
98 1.2.2.2 thorpej const struct fdt_attach_args *faa = aux;
99 1.2.2.2 thorpej const int phandle = faa->faa_phandle;
100 1.2.2.2 thorpej int len;
101 1.2.2.2 thorpej char *name;
102 1.2.2.2 thorpej
103 1.2.2.2 thorpej sc->sc_dev = self;
104 1.2.2.2 thorpej sc->sc_phandle = phandle;
105 1.2.2.2 thorpej
106 1.2.2.2 thorpej aprint_naive("\n");
107 1.2.2.2 thorpej len = OF_getproplen(phandle, "regulator-name");
108 1.2.2.2 thorpej if (len > 0) {
109 1.2.2.2 thorpej name = kmem_zalloc(len, KM_SLEEP);
110 1.2.2.2 thorpej if (OF_getprop(phandle, "regulator-name", name, len) == len)
111 1.2.2.2 thorpej aprint_normal(": %s\n", name);
112 1.2.2.2 thorpej else
113 1.2.2.2 thorpej aprint_normal("\n");
114 1.2.2.2 thorpej kmem_free(name, len);
115 1.2.2.2 thorpej } else {
116 1.2.2.2 thorpej aprint_normal("\n");
117 1.2.2.2 thorpej }
118 1.2.2.2 thorpej
119 1.2.2.2 thorpej if (of_getprop_uint32(phandle, "regulator-min-microvolt",
120 1.2.2.2 thorpej &sc->sc_microvolt_min) != 0) {
121 1.2.2.2 thorpej aprint_error_dev(sc->sc_dev,
122 1.2.2.2 thorpej "missing regulator-min-microvolt properties\n");
123 1.2.2.2 thorpej return;
124 1.2.2.2 thorpej }
125 1.2.2.2 thorpej if (of_getprop_uint32(phandle, "regulator-max-microvolt",
126 1.2.2.2 thorpej &sc->sc_microvolt_max) != 0) {
127 1.2.2.2 thorpej aprint_error_dev(sc->sc_dev,
128 1.2.2.2 thorpej "missing regulator-max-microvolt properties\n");
129 1.2.2.2 thorpej return;
130 1.2.2.2 thorpej }
131 1.2.2.2 thorpej
132 1.2.2.2 thorpej if (of_getprop_uint32(phandle, "pwm-dutycycle-unit",
133 1.2.2.2 thorpej &sc->sc_dutycycle_unit) != 0)
134 1.2.2.2 thorpej sc->sc_dutycycle_unit = 100;
135 1.2.2.2 thorpej
136 1.2.2.2 thorpej if (of_getprop_uint32_array(phandle, "pwm-dutycycle-range",
137 1.2.2.2 thorpej sc->sc_dutycycle_range, 2) != 0) {
138 1.2.2.2 thorpej sc->sc_dutycycle_range[0] = 0;
139 1.2.2.2 thorpej sc->sc_dutycycle_range[1] = 100;
140 1.2.2.2 thorpej }
141 1.2.2.2 thorpej
142 1.2.2.2 thorpej len = OF_getproplen(phandle, "voltage-table");
143 1.2.2.2 thorpej if (len > 0) {
144 1.2.2.2 thorpej struct voltage_duty *voltage_table = kmem_zalloc(len, KM_SLEEP);
145 1.2.2.2 thorpej if (of_getprop_uint32_array(phandle, "voltage-table",
146 1.2.2.2 thorpej (uint32_t *)voltage_table, len / sizeof(uint32_t)) == 0) {
147 1.2.2.2 thorpej sc->sc_voltage_table = voltage_table;
148 1.2.2.2 thorpej sc->sc_voltage_table_num =
149 1.2.2.2 thorpej len / sizeof(struct voltage_duty);
150 1.2.2.2 thorpej #ifdef PWMREGULATOR_DEBUG
151 1.2.2.2 thorpej for (int i = 0; i < sc->sc_voltage_table_num; i++) {
152 1.2.2.2 thorpej aprint_debug_dev(sc->sc_dev,
153 1.2.2.2 thorpej "VoltageTable[%d]: %uuV = Duty:%u%%\n", i,
154 1.2.2.2 thorpej voltage_table[i].voltage,
155 1.2.2.2 thorpej voltage_table[i].duty);
156 1.2.2.2 thorpej }
157 1.2.2.2 thorpej #endif
158 1.2.2.2 thorpej /*
159 1.2.2.2 thorpej * if voltage-table is provided, the duty in the table
160 1.2.2.2 thorpej * represents a percentage, i.e. 0-100%, so
161 1.2.2.2 thorpej * dutycycle_unit is 100.
162 1.2.2.2 thorpej */
163 1.2.2.2 thorpej sc->sc_dutycycle_unit = 100;
164 1.2.2.2 thorpej } else {
165 1.2.2.2 thorpej kmem_free(sc->sc_voltage_table, len);
166 1.2.2.2 thorpej }
167 1.2.2.2 thorpej }
168 1.2.2.2 thorpej #ifdef PWMREGULATOR_DEBUG
169 1.2.2.2 thorpej if (sc->sc_voltage_table_num == 0) {
170 1.2.2.2 thorpej aprint_debug_dev(sc->sc_dev, "Duty:%u%%=%uuV, Duty:%u%%=%uuV\n",
171 1.2.2.2 thorpej sc->sc_dutycycle_range[0], sc->sc_microvolt_min,
172 1.2.2.2 thorpej sc->sc_dutycycle_range[1], sc->sc_microvolt_max);
173 1.2.2.2 thorpej }
174 1.2.2.2 thorpej #endif
175 1.2.2.2 thorpej
176 1.2.2.2 thorpej sc->sc_always_on = of_getprop_bool(phandle, "regulator-always-on");
177 1.2.2.2 thorpej sc->sc_boot_on = of_getprop_bool(phandle, "regulator-boot-on");
178 1.2.2.2 thorpej
179 1.2.2.2 thorpej fdtbus_register_regulator_controller(self, phandle,
180 1.2.2.2 thorpej &pwmregulator_funcs);
181 1.2.2.2 thorpej
182 1.2.2.2 thorpej /*
183 1.2.2.2 thorpej * If the regulator is flagged as always on or enabled at boot,
184 1.2.2.2 thorpej * ensure that it is enabled
185 1.2.2.2 thorpej */
186 1.2.2.2 thorpej if (sc->sc_always_on || sc->sc_boot_on)
187 1.2.2.2 thorpej pwmregulator_enable(self, true);
188 1.2.2.2 thorpej }
189 1.2.2.2 thorpej
190 1.2.2.2 thorpej static int
191 1.2.2.2 thorpej pwmregulator_acquire(device_t dev)
192 1.2.2.2 thorpej {
193 1.2.2.2 thorpej struct pwmregulator_softc * const sc = device_private(dev);
194 1.2.2.2 thorpej
195 1.2.2.2 thorpej /* "enable-gpios" is optional */
196 1.2.2.2 thorpej sc->sc_pin = fdtbus_gpio_acquire(sc->sc_phandle, "enable-gpios",
197 1.2.2.2 thorpej GPIO_PIN_OUTPUT);
198 1.2.2.2 thorpej
199 1.2.2.2 thorpej sc->sc_pwm = fdtbus_pwm_acquire(sc->sc_phandle, "pwms");
200 1.2.2.2 thorpej if (sc->sc_pwm == NULL)
201 1.2.2.2 thorpej return ENXIO;
202 1.2.2.2 thorpej
203 1.2.2.2 thorpej return 0;
204 1.2.2.2 thorpej }
205 1.2.2.2 thorpej
206 1.2.2.2 thorpej static void
207 1.2.2.2 thorpej pwmregulator_release(device_t dev)
208 1.2.2.2 thorpej {
209 1.2.2.2 thorpej struct pwmregulator_softc * const sc = device_private(dev);
210 1.2.2.2 thorpej
211 1.2.2.2 thorpej if (sc->sc_pin != NULL) {
212 1.2.2.2 thorpej fdtbus_gpio_write(sc->sc_pin, 0);
213 1.2.2.2 thorpej fdtbus_gpio_release(sc->sc_pin);
214 1.2.2.2 thorpej }
215 1.2.2.2 thorpej
216 1.2.2.2 thorpej sc->sc_pwm = NULL;
217 1.2.2.2 thorpej }
218 1.2.2.2 thorpej
219 1.2.2.2 thorpej static int
220 1.2.2.2 thorpej pwmregulator_enable(device_t dev, bool enable)
221 1.2.2.2 thorpej {
222 1.2.2.2 thorpej struct pwmregulator_softc * const sc = device_private(dev);
223 1.2.2.2 thorpej int error;
224 1.2.2.2 thorpej
225 1.2.2.2 thorpej if (sc->sc_pwm == NULL)
226 1.2.2.2 thorpej return ENXIO;
227 1.2.2.2 thorpej
228 1.2.2.2 thorpej if (enable) {
229 1.2.2.2 thorpej if (sc->sc_pin != NULL)
230 1.2.2.2 thorpej fdtbus_gpio_write(sc->sc_pin, 1);
231 1.2.2.2 thorpej error = pwm_enable(sc->sc_pwm);
232 1.2.2.2 thorpej } else {
233 1.2.2.2 thorpej error = pwm_disable(sc->sc_pwm);
234 1.2.2.2 thorpej if (sc->sc_pin != NULL)
235 1.2.2.2 thorpej fdtbus_gpio_write(sc->sc_pin, 0);
236 1.2.2.2 thorpej }
237 1.2.2.2 thorpej
238 1.2.2.2 thorpej return error;
239 1.2.2.2 thorpej }
240 1.2.2.2 thorpej
241 1.2.2.2 thorpej static int
242 1.2.2.2 thorpej pwmregulator_set_voltage(device_t dev, u_int min_uvolt, u_int max_uvolt)
243 1.2.2.2 thorpej {
244 1.2.2.2 thorpej struct pwmregulator_softc * const sc = device_private(dev);
245 1.2.2.2 thorpej struct pwm_config conf;
246 1.2.2.2 thorpej int duty, d0, d1, v0, v1, uv, rc;
247 1.2.2.2 thorpej
248 1.2.2.2 thorpej if (sc->sc_pwm == NULL)
249 1.2.2.2 thorpej return ENXIO;
250 1.2.2.2 thorpej
251 1.2.2.2 thorpej rc = pwm_get_config(sc->sc_pwm, &conf);
252 1.2.2.2 thorpej if (rc != 0) {
253 1.2.2.2 thorpej device_printf(dev, "%s: couldn't get pwm config, error=%d\n",
254 1.2.2.2 thorpej __func__, rc);
255 1.2.2.2 thorpej return rc;
256 1.2.2.2 thorpej }
257 1.2.2.2 thorpej
258 1.2.2.2 thorpej uv = (min_uvolt + max_uvolt) / 2;
259 1.2.2.2 thorpej
260 1.2.2.2 thorpej if (sc->sc_voltage_table_num > 0) {
261 1.2.2.2 thorpej /* find the nearest duty from voltage-table */
262 1.2.2.2 thorpej int i, bestidx = 0;
263 1.2.2.2 thorpej for (i = 1; i < sc->sc_voltage_table_num; i++) {
264 1.2.2.2 thorpej if (abs(sc->sc_voltage_table[i].microvolt - uv) <
265 1.2.2.2 thorpej abs(sc->sc_voltage_table[bestidx].microvolt - uv))
266 1.2.2.2 thorpej bestidx = i;
267 1.2.2.2 thorpej }
268 1.2.2.2 thorpej duty = sc->sc_voltage_table[bestidx].duty;
269 1.2.2.2 thorpej } else {
270 1.2.2.2 thorpej /* calculate duty from voltage */
271 1.2.2.2 thorpej v0 = sc->sc_microvolt_min;
272 1.2.2.2 thorpej v1 = sc->sc_microvolt_max;
273 1.2.2.2 thorpej d0 = sc->sc_dutycycle_range[0];
274 1.2.2.2 thorpej d1 = sc->sc_dutycycle_range[1];
275 1.2.2.2 thorpej duty = (uv - v0) * (d1 - d0) / (v1 - v0) + d0;
276 1.2.2.2 thorpej }
277 1.2.2.2 thorpej
278 1.2.2.2 thorpej conf.duty_cycle = duty * conf.period / sc->sc_dutycycle_unit;
279 1.2.2.2 thorpej
280 1.2.2.2 thorpej rc = pwm_set_config(sc->sc_pwm, &conf);
281 1.2.2.2 thorpej if (rc != 0)
282 1.2.2.2 thorpej device_printf(dev, "couldn't set pwm config, error=%d\n", rc);
283 1.2.2.2 thorpej return rc;
284 1.2.2.2 thorpej }
285 1.2.2.2 thorpej
286 1.2.2.2 thorpej static int
287 1.2.2.2 thorpej pwmregulator_get_voltage(device_t dev, u_int *puvolt)
288 1.2.2.2 thorpej {
289 1.2.2.2 thorpej struct pwmregulator_softc * const sc = device_private(dev);
290 1.2.2.2 thorpej struct pwm_config conf;
291 1.2.2.2 thorpej int duty, d0, d1, v0, v1, uv, rc;
292 1.2.2.2 thorpej
293 1.2.2.2 thorpej if (sc->sc_pwm == NULL)
294 1.2.2.2 thorpej return ENXIO;
295 1.2.2.2 thorpej
296 1.2.2.2 thorpej rc = pwm_get_config(sc->sc_pwm, &conf);
297 1.2.2.2 thorpej if (rc != 0) {
298 1.2.2.2 thorpej device_printf(dev, "%s: couldn't get pwm config, error=%d\n",
299 1.2.2.2 thorpej __func__, rc);
300 1.2.2.2 thorpej return rc;
301 1.2.2.2 thorpej }
302 1.2.2.2 thorpej
303 1.2.2.2 thorpej duty = conf.duty_cycle * sc->sc_dutycycle_unit / conf.period;
304 1.2.2.2 thorpej
305 1.2.2.2 thorpej if (sc->sc_voltage_table_num > 0) {
306 1.2.2.2 thorpej /* find the nearest voltage from voltage-table */
307 1.2.2.2 thorpej int i, bestidx = 0;
308 1.2.2.2 thorpej for (i = 1; i < sc->sc_voltage_table_num; i++) {
309 1.2.2.2 thorpej if (abs(sc->sc_voltage_table[i].duty - duty) <
310 1.2.2.2 thorpej abs(sc->sc_voltage_table[bestidx].duty - duty))
311 1.2.2.2 thorpej bestidx = i;
312 1.2.2.2 thorpej }
313 1.2.2.2 thorpej uv = sc->sc_voltage_table[bestidx].microvolt;
314 1.2.2.2 thorpej } else {
315 1.2.2.2 thorpej /* calculate voltage from duty */
316 1.2.2.2 thorpej d0 = sc->sc_dutycycle_range[0];
317 1.2.2.2 thorpej d1 = sc->sc_dutycycle_range[1];
318 1.2.2.2 thorpej v0 = sc->sc_microvolt_min;
319 1.2.2.2 thorpej v1 = sc->sc_microvolt_max;
320 1.2.2.2 thorpej uv = (duty - d0) * (v1 - v0) / (d1 - d0) + v0;
321 1.2.2.2 thorpej }
322 1.2.2.2 thorpej
323 1.2.2.2 thorpej *puvolt = uv;
324 1.2.2.2 thorpej return 0;
325 1.2.2.2 thorpej }
326