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