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