1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2020 Emmanuel Vadot <manu@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/bio.h> 32 #include <sys/bus.h> 33 #include <sys/conf.h> 34 #include <sys/endian.h> 35 #include <sys/fcntl.h> 36 #include <sys/ioccom.h> 37 #include <sys/kernel.h> 38 #include <sys/kthread.h> 39 #include <sys/lock.h> 40 #include <sys/malloc.h> 41 #include <sys/module.h> 42 #include <sys/mutex.h> 43 #include <sys/priv.h> 44 #include <sys/slicer.h> 45 #include <sys/sysctl.h> 46 #include <sys/time.h> 47 48 #include <dev/ofw/ofw_bus.h> 49 #include <dev/ofw/ofw_bus_subr.h> 50 51 #include <dev/extres/regulator/regulator.h> 52 53 #include <dev/backlight/backlight.h> 54 55 #include <dev/pwm/ofw_pwm.h> 56 57 #include "backlight_if.h" 58 #include "pwmbus_if.h" 59 60 struct pwm_backlight_softc { 61 device_t pwmdev; 62 struct cdev *cdev; 63 64 pwm_channel_t channel; 65 uint32_t *levels; 66 ssize_t nlevels; 67 int default_level; 68 ssize_t current_level; 69 70 regulator_t power_supply; 71 uint64_t period; 72 uint64_t duty; 73 bool enabled; 74 }; 75 76 static int pwm_backlight_find_level_per_percent(struct pwm_backlight_softc *sc, int percent); 77 78 static struct ofw_compat_data compat_data[] = { 79 { "pwm-backlight", 1 }, 80 { NULL, 0 } 81 }; 82 83 static int 84 pwm_backlight_probe(device_t dev) 85 { 86 87 if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data) 88 return (ENXIO); 89 90 device_set_desc(dev, "PWM Backlight"); 91 return (BUS_PROBE_DEFAULT); 92 } 93 94 static int 95 pwm_backlight_attach(device_t dev) 96 { 97 struct pwm_backlight_softc *sc; 98 phandle_t node; 99 int rv; 100 101 sc = device_get_softc(dev); 102 node = ofw_bus_get_node(dev); 103 104 rv = pwm_get_by_ofw_propidx(dev, node, "pwms", 0, &sc->channel); 105 if (rv != 0) { 106 device_printf(dev, "Cannot map pwm channel %d\n", rv); 107 return (ENXIO); 108 } 109 110 if (regulator_get_by_ofw_property(dev, 0, "power-supply", 111 &sc->power_supply) != 0) { 112 device_printf(dev, "No power-supply property\n"); 113 return (ENXIO); 114 } 115 116 if (OF_hasprop(node, "brightness-levels")) { 117 sc->nlevels = OF_getencprop_alloc(node, "brightness-levels", 118 (void **)&sc->levels); 119 if (sc->nlevels <= 0) { 120 device_printf(dev, "Cannot parse brightness levels\n"); 121 return (ENXIO); 122 } 123 sc->nlevels /= sizeof(uint32_t); 124 125 if (OF_getencprop(node, "default-brightness-level", 126 &sc->default_level, sizeof(uint32_t)) <= 0) { 127 device_printf(dev, "No default-brightness-level while brightness-levels is specified\n"); 128 return (ENXIO); 129 } else { 130 if (sc->default_level > sc->nlevels) { 131 device_printf(dev, "default-brightness-level isn't present in brightness-levels range\n"); 132 return (ENXIO); 133 } 134 sc->channel->duty = sc->channel->period * sc->levels[sc->default_level] / 100; 135 } 136 137 if (bootverbose) { 138 device_printf(dev, "Number of levels: %zd\n", sc->nlevels); 139 device_printf(dev, "Configured period time: %ju\n", (uintmax_t)sc->channel->period); 140 device_printf(dev, "Default duty cycle: %ju\n", (uintmax_t)sc->channel->duty); 141 } 142 } else { 143 /* Get the current backlight level */ 144 PWMBUS_CHANNEL_GET_CONFIG(sc->channel->dev, 145 sc->channel->channel, 146 (unsigned int *)&sc->channel->period, 147 (unsigned int *)&sc->channel->duty); 148 if (sc->channel->duty > sc->channel->period) 149 sc->channel->duty = sc->channel->period; 150 if (bootverbose) { 151 device_printf(dev, "Configured period time: %ju\n", (uintmax_t)sc->channel->period); 152 device_printf(dev, "Default duty cycle: %ju\n", (uintmax_t)sc->channel->duty); 153 } 154 } 155 156 regulator_enable(sc->power_supply); 157 sc->channel->enabled = true; 158 PWMBUS_CHANNEL_CONFIG(sc->channel->dev, sc->channel->channel, 159 sc->channel->period, sc->channel->duty); 160 PWMBUS_CHANNEL_ENABLE(sc->channel->dev, sc->channel->channel, 161 sc->channel->enabled); 162 163 sc->current_level = pwm_backlight_find_level_per_percent(sc, 164 sc->channel->period / sc->channel->duty); 165 sc->cdev = backlight_register("pwm_backlight", dev); 166 if (sc->cdev == NULL) 167 device_printf(dev, "Cannot register as a backlight\n"); 168 169 return (0); 170 } 171 172 static int 173 pwm_backlight_detach(device_t dev) 174 { 175 struct pwm_backlight_softc *sc; 176 177 sc = device_get_softc(dev); 178 if (sc->nlevels > 0) 179 OF_prop_free(sc->levels); 180 regulator_disable(sc->power_supply); 181 backlight_destroy(sc->cdev); 182 return (0); 183 } 184 185 static int 186 pwm_backlight_find_level_per_percent(struct pwm_backlight_softc *sc, int percent) 187 { 188 int i; 189 int diff; 190 191 if (percent < 0 || percent > 100) 192 return (-1); 193 194 for (i = 0, diff = 0; i < sc->nlevels; i++) { 195 if (sc->levels[i] == percent) 196 return (i); 197 else if (sc->levels[i] < percent) 198 diff = percent - sc->levels[i]; 199 else { 200 if (diff < abs((percent - sc->levels[i]))) 201 return (i - 1); 202 else 203 return (i); 204 } 205 } 206 207 return (-1); 208 } 209 210 static int 211 pwm_backlight_update_status(device_t dev, struct backlight_props *props) 212 { 213 struct pwm_backlight_softc *sc; 214 int reg_status; 215 int error; 216 217 sc = device_get_softc(dev); 218 219 if (sc->nlevels != 0) { 220 error = pwm_backlight_find_level_per_percent(sc, 221 props->brightness); 222 if (error < 0) 223 return (ERANGE); 224 sc->current_level = error; 225 sc->channel->duty = sc->channel->period * 226 sc->levels[sc->current_level] / 100; 227 } else { 228 if (props->brightness > 100 || props->brightness < 0) 229 return (ERANGE); 230 sc->channel->duty = sc->channel->period * 231 props->brightness / 100; 232 } 233 sc->channel->enabled = true; 234 PWMBUS_CHANNEL_CONFIG(sc->channel->dev, sc->channel->channel, 235 sc->channel->period, sc->channel->duty); 236 PWMBUS_CHANNEL_ENABLE(sc->channel->dev, sc->channel->channel, 237 sc->channel->enabled); 238 error = regulator_status(sc->power_supply, ®_status); 239 if (error != 0) 240 device_printf(dev, 241 "Cannot get power-supply status: %d\n", error); 242 else { 243 if (props->brightness > 0) { 244 if (reg_status != REGULATOR_STATUS_ENABLED) 245 regulator_enable(sc->power_supply); 246 } else { 247 if (reg_status == REGULATOR_STATUS_ENABLED) 248 regulator_disable(sc->power_supply); 249 } 250 } 251 252 return (0); 253 } 254 255 static int 256 pwm_backlight_get_status(device_t dev, struct backlight_props *props) 257 { 258 struct pwm_backlight_softc *sc; 259 int i; 260 261 sc = device_get_softc(dev); 262 263 if (sc->nlevels != 0) { 264 props->brightness = sc->levels[sc->current_level]; 265 props->nlevels = sc->nlevels; 266 for (i = 0; i < sc->nlevels; i++) 267 props->levels[i] = sc->levels[i]; 268 } else { 269 props->brightness = sc->channel->duty * 100 / sc->channel->period; 270 props->nlevels = 0; 271 } 272 return (0); 273 } 274 275 static int 276 pwm_backlight_get_info(device_t dev, struct backlight_info *info) 277 { 278 279 info->type = BACKLIGHT_TYPE_PANEL; 280 strlcpy(info->name, "pwm-backlight", BACKLIGHTMAXNAMELENGTH); 281 return (0); 282 } 283 284 static device_method_t pwm_backlight_methods[] = { 285 /* device_if */ 286 DEVMETHOD(device_probe, pwm_backlight_probe), 287 DEVMETHOD(device_attach, pwm_backlight_attach), 288 DEVMETHOD(device_detach, pwm_backlight_detach), 289 290 /* backlight interface */ 291 DEVMETHOD(backlight_update_status, pwm_backlight_update_status), 292 DEVMETHOD(backlight_get_status, pwm_backlight_get_status), 293 DEVMETHOD(backlight_get_info, pwm_backlight_get_info), 294 DEVMETHOD_END 295 }; 296 297 driver_t pwm_backlight_driver = { 298 "pwm_backlight", 299 pwm_backlight_methods, 300 sizeof(struct pwm_backlight_softc), 301 }; 302 303 DRIVER_MODULE(pwm_backlight, simplebus, pwm_backlight_driver, 0, 0); 304 MODULE_DEPEND(pwm_backlight, backlight, 1, 1, 1); 305 OFWBUS_PNP_INFO(compat_data); 306