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