pwmregulator.c revision 1.1 1 1.1 ryo /* $NetBSD: pwmregulator.c,v 1.1 2020/12/31 15:12:33 ryo Exp $ */
2 1.1 ryo
3 1.1 ryo /*
4 1.1 ryo * Copyright (c) 2020 Ryo Shimizu <ryo (at) nerv.org>
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.1 ryo __KERNEL_RCSID(0, "$NetBSD: pwmregulator.c,v 1.1 2020/12/31 15:12:33 ryo 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.1 ryo struct voltage_duty *sc_voltage_table;
67 1.1 ryo int sc_voltage_table_num;
68 1.1 ryo int sc_phandle;
69 1.1 ryo uint32_t sc_microvolt_min;
70 1.1 ryo uint32_t sc_microvolt_max;
71 1.1 ryo uint32_t sc_dutycycle_unit;
72 1.1 ryo uint32_t sc_dutycycle_range[2];
73 1.1 ryo bool sc_always_on;
74 1.1 ryo bool sc_boot_on;
75 1.1 ryo };
76 1.1 ryo
77 1.1 ryo CFATTACH_DECL_NEW(pregulator, sizeof(struct pwmregulator_softc),
78 1.1 ryo pwmregulator_match, pwmregulator_attach, NULL, NULL);
79 1.1 ryo
80 1.1 ryo static const char * const compatible[] = {
81 1.1 ryo "pwm-regulator",
82 1.1 ryo NULL
83 1.1 ryo };
84 1.1 ryo
85 1.1 ryo static int
86 1.1 ryo pwmregulator_match(device_t parent, cfdata_t cf, void *aux)
87 1.1 ryo {
88 1.1 ryo const struct fdt_attach_args *faa = aux;
89 1.1 ryo
90 1.1 ryo return of_match_compatible(faa->faa_phandle, compatible);
91 1.1 ryo }
92 1.1 ryo
93 1.1 ryo static void
94 1.1 ryo pwmregulator_attach(device_t parent, device_t self, void *aux)
95 1.1 ryo {
96 1.1 ryo struct pwmregulator_softc * const sc = device_private(self);
97 1.1 ryo const struct fdt_attach_args *faa = aux;
98 1.1 ryo const int phandle = faa->faa_phandle;
99 1.1 ryo int len;
100 1.1 ryo char *name;
101 1.1 ryo
102 1.1 ryo sc->sc_dev = self;
103 1.1 ryo sc->sc_phandle = phandle;
104 1.1 ryo
105 1.1 ryo aprint_naive("\n");
106 1.1 ryo len = OF_getproplen(phandle, "regulator-name");
107 1.1 ryo if (len > 0) {
108 1.1 ryo name = kmem_zalloc(len, KM_SLEEP);
109 1.1 ryo if (OF_getprop(phandle, "regulator-name", name, len) == len)
110 1.1 ryo aprint_normal(": %s\n", name);
111 1.1 ryo else
112 1.1 ryo aprint_normal("\n");
113 1.1 ryo kmem_free(name, len);
114 1.1 ryo } else {
115 1.1 ryo aprint_normal("\n");
116 1.1 ryo }
117 1.1 ryo
118 1.1 ryo if (of_getprop_uint32(phandle, "regulator-min-microvolt",
119 1.1 ryo &sc->sc_microvolt_min) != 0) {
120 1.1 ryo aprint_error_dev(sc->sc_dev,
121 1.1 ryo "missing regulator-min-microvolt properties\n");
122 1.1 ryo return;
123 1.1 ryo }
124 1.1 ryo if (of_getprop_uint32(phandle, "regulator-max-microvolt",
125 1.1 ryo &sc->sc_microvolt_max) != 0) {
126 1.1 ryo aprint_error_dev(sc->sc_dev,
127 1.1 ryo "missing regulator-max-microvolt properties\n");
128 1.1 ryo return;
129 1.1 ryo }
130 1.1 ryo
131 1.1 ryo if (of_getprop_uint32(phandle, "pwm-dutycycle-unit",
132 1.1 ryo &sc->sc_dutycycle_unit) != 0)
133 1.1 ryo sc->sc_dutycycle_unit = 100;
134 1.1 ryo
135 1.1 ryo len = of_getprop_uint32_array(phandle, "pwm-dutycycle-range",
136 1.1 ryo sc->sc_dutycycle_range, 2);
137 1.1 ryo if (of_getprop_uint32_array(phandle, "pwm-dutycycle-range",
138 1.1 ryo sc->sc_dutycycle_range, 2) != 0) {
139 1.1 ryo sc->sc_dutycycle_range[0] = 0;
140 1.1 ryo sc->sc_dutycycle_range[1] = 100;
141 1.1 ryo }
142 1.1 ryo
143 1.1 ryo len = OF_getproplen(phandle, "voltage-table");
144 1.1 ryo if (len > 0) {
145 1.1 ryo struct voltage_duty *voltage_table = kmem_zalloc(len, KM_SLEEP);
146 1.1 ryo if (of_getprop_uint32_array(phandle, "voltage-table",
147 1.1 ryo (uint32_t *)voltage_table, len / sizeof(uint32_t)) == 0) {
148 1.1 ryo sc->sc_voltage_table = voltage_table;
149 1.1 ryo sc->sc_voltage_table_num =
150 1.1 ryo len / sizeof(struct voltage_duty);
151 1.1 ryo #ifdef PWMREGULATOR_DEBUG
152 1.1 ryo for (int i = 0; i < sc->sc_voltage_table_num; i++) {
153 1.1 ryo aprint_debug_dev(sc->sc_dev,
154 1.1 ryo "VoltageTable[%d]: %uuV = Duty:%u%%\n", i,
155 1.1 ryo voltage_table[i].voltage,
156 1.1 ryo voltage_table[i].duty);
157 1.1 ryo }
158 1.1 ryo #endif
159 1.1 ryo /*
160 1.1 ryo * if voltage-table is provided, the duty in the table
161 1.1 ryo * represents a percentage, i.e. 0-100%, so
162 1.1 ryo * dutycycle_unit is 100.
163 1.1 ryo */
164 1.1 ryo sc->sc_dutycycle_unit = 100;
165 1.1 ryo } else {
166 1.1 ryo kmem_free(sc->sc_voltage_table, len);
167 1.1 ryo }
168 1.1 ryo }
169 1.1 ryo #ifdef PWMREGULATOR_DEBUG
170 1.1 ryo if (sc->sc_voltage_table_num == 0) {
171 1.1 ryo aprint_debug_dev(sc->sc_dev, "Duty:%u%%=%uuV, Duty:%u%%=%uuV\n",
172 1.1 ryo sc->sc_dutycycle_range[0], sc->sc_microvolt_min,
173 1.1 ryo sc->sc_dutycycle_range[1], sc->sc_microvolt_max);
174 1.1 ryo }
175 1.1 ryo #endif
176 1.1 ryo
177 1.1 ryo sc->sc_always_on = of_getprop_bool(phandle, "regulator-always-on");
178 1.1 ryo sc->sc_boot_on = of_getprop_bool(phandle, "regulator-boot-on");
179 1.1 ryo
180 1.1 ryo fdtbus_register_regulator_controller(self, phandle,
181 1.1 ryo &pwmregulator_funcs);
182 1.1 ryo
183 1.1 ryo /*
184 1.1 ryo * If the regulator is flagged as always on or enabled at boot,
185 1.1 ryo * ensure that it is enabled
186 1.1 ryo */
187 1.1 ryo if (sc->sc_always_on || sc->sc_boot_on)
188 1.1 ryo pwmregulator_enable(self, true);
189 1.1 ryo }
190 1.1 ryo
191 1.1 ryo static int
192 1.1 ryo pwmregulator_acquire(device_t dev)
193 1.1 ryo {
194 1.1 ryo struct pwmregulator_softc * const sc = device_private(dev);
195 1.1 ryo
196 1.1 ryo sc->sc_pwm = fdtbus_pwm_acquire(sc->sc_phandle, "pwms");
197 1.1 ryo if (sc->sc_pwm == NULL)
198 1.1 ryo return ENXIO;
199 1.1 ryo
200 1.1 ryo return 0;
201 1.1 ryo }
202 1.1 ryo
203 1.1 ryo static void
204 1.1 ryo pwmregulator_release(device_t dev)
205 1.1 ryo {
206 1.1 ryo struct pwmregulator_softc * const sc = device_private(dev);
207 1.1 ryo
208 1.1 ryo sc->sc_pwm = NULL;
209 1.1 ryo }
210 1.1 ryo
211 1.1 ryo static int
212 1.1 ryo pwmregulator_enable(device_t dev, bool enable)
213 1.1 ryo {
214 1.1 ryo struct pwmregulator_softc * const sc = device_private(dev);
215 1.1 ryo
216 1.1 ryo if (sc->sc_pwm == NULL)
217 1.1 ryo return ENXIO;
218 1.1 ryo
219 1.1 ryo if (enable)
220 1.1 ryo return pwm_enable(sc->sc_pwm);
221 1.1 ryo else
222 1.1 ryo return pwm_disable(sc->sc_pwm);
223 1.1 ryo }
224 1.1 ryo
225 1.1 ryo static int
226 1.1 ryo pwmregulator_set_voltage(device_t dev, u_int min_uvolt, u_int max_uvolt)
227 1.1 ryo {
228 1.1 ryo struct pwmregulator_softc * const sc = device_private(dev);
229 1.1 ryo struct pwm_config conf;
230 1.1 ryo int duty, d0, d1, v0, v1, uv, rc;
231 1.1 ryo
232 1.1 ryo if (sc->sc_pwm == NULL)
233 1.1 ryo return ENXIO;
234 1.1 ryo
235 1.1 ryo rc = pwm_get_config(sc->sc_pwm, &conf);
236 1.1 ryo if (rc != 0) {
237 1.1 ryo device_printf(dev, "%s: couldn't get pwm config, error=%d\n",
238 1.1 ryo __func__, rc);
239 1.1 ryo return rc;
240 1.1 ryo }
241 1.1 ryo
242 1.1 ryo uv = (min_uvolt + max_uvolt) / 2;
243 1.1 ryo
244 1.1 ryo if (sc->sc_voltage_table_num > 0) {
245 1.1 ryo /* find the nearest duty from voltage-table */
246 1.1 ryo int i, bestidx = 0;
247 1.1 ryo for (i = 1; i < sc->sc_voltage_table_num; i++) {
248 1.1 ryo if (abs(sc->sc_voltage_table[i].microvolt - uv) <
249 1.1 ryo abs(sc->sc_voltage_table[bestidx].microvolt - uv))
250 1.1 ryo bestidx = i;
251 1.1 ryo }
252 1.1 ryo duty = sc->sc_voltage_table[bestidx].duty;
253 1.1 ryo } else {
254 1.1 ryo /* calculate duty from voltage */
255 1.1 ryo v0 = sc->sc_microvolt_min;
256 1.1 ryo v1 = sc->sc_microvolt_max;
257 1.1 ryo d0 = sc->sc_dutycycle_range[0];
258 1.1 ryo d1 = sc->sc_dutycycle_range[1];
259 1.1 ryo duty = (uv - v0) * (d1 - d0) / (v1 - v0) + d0;
260 1.1 ryo }
261 1.1 ryo
262 1.1 ryo conf.duty_cycle = duty * conf.period / sc->sc_dutycycle_unit;
263 1.1 ryo
264 1.1 ryo rc = pwm_set_config(sc->sc_pwm, &conf);
265 1.1 ryo if (rc != 0)
266 1.1 ryo device_printf(dev, "couldn't set pwm config, error=%d\n", rc);
267 1.1 ryo return rc;
268 1.1 ryo }
269 1.1 ryo
270 1.1 ryo static int
271 1.1 ryo pwmregulator_get_voltage(device_t dev, u_int *puvolt)
272 1.1 ryo {
273 1.1 ryo struct pwmregulator_softc * const sc = device_private(dev);
274 1.1 ryo struct pwm_config conf;
275 1.1 ryo int duty, d0, d1, v0, v1, uv, rc;
276 1.1 ryo
277 1.1 ryo if (sc->sc_pwm == NULL)
278 1.1 ryo return ENXIO;
279 1.1 ryo
280 1.1 ryo rc = pwm_get_config(sc->sc_pwm, &conf);
281 1.1 ryo if (rc != 0) {
282 1.1 ryo device_printf(dev, "%s: couldn't get pwm config, error=%d\n",
283 1.1 ryo __func__, rc);
284 1.1 ryo return rc;
285 1.1 ryo }
286 1.1 ryo
287 1.1 ryo duty = conf.duty_cycle * sc->sc_dutycycle_unit / conf.period;
288 1.1 ryo
289 1.1 ryo if (sc->sc_voltage_table_num > 0) {
290 1.1 ryo /* find the nearest voltage from voltage-table */
291 1.1 ryo int i, bestidx = 0;
292 1.1 ryo for (i = 1; i < sc->sc_voltage_table_num; i++) {
293 1.1 ryo if (abs(sc->sc_voltage_table[i].duty - duty) <
294 1.1 ryo abs(sc->sc_voltage_table[bestidx].duty - duty))
295 1.1 ryo bestidx = i;
296 1.1 ryo }
297 1.1 ryo uv = sc->sc_voltage_table[bestidx].microvolt;
298 1.1 ryo } else {
299 1.1 ryo /* calculate voltage from duty */
300 1.1 ryo d0 = sc->sc_dutycycle_range[0];
301 1.1 ryo d1 = sc->sc_dutycycle_range[1];
302 1.1 ryo v0 = sc->sc_microvolt_min;
303 1.1 ryo v1 = sc->sc_microvolt_max;
304 1.1 ryo uv = (duty - d0) * (v1 - v0) / (d1 - d0) + v0;
305 1.1 ryo }
306 1.1 ryo
307 1.1 ryo *puvolt = uv;
308 1.1 ryo return 0;
309 1.1 ryo }
310