138d94a4bSEmmanuel Vadot /*- 238d94a4bSEmmanuel Vadot * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 338d94a4bSEmmanuel Vadot * 438d94a4bSEmmanuel Vadot * Copyright (c) 2020 Emmanuel Vadot <manu@FreeBSD.org> 538d94a4bSEmmanuel Vadot * 638d94a4bSEmmanuel Vadot * Redistribution and use in source and binary forms, with or without 738d94a4bSEmmanuel Vadot * modification, are permitted provided that the following conditions 838d94a4bSEmmanuel Vadot * are met: 938d94a4bSEmmanuel Vadot * 1. Redistributions of source code must retain the above copyright 1038d94a4bSEmmanuel Vadot * notice, this list of conditions and the following disclaimer. 1138d94a4bSEmmanuel Vadot * 2. Redistributions in binary form must reproduce the above copyright 1238d94a4bSEmmanuel Vadot * notice, this list of conditions and the following disclaimer in the 1338d94a4bSEmmanuel Vadot * documentation and/or other materials provided with the distribution. 1438d94a4bSEmmanuel Vadot * 1538d94a4bSEmmanuel Vadot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1638d94a4bSEmmanuel Vadot * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1738d94a4bSEmmanuel Vadot * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1838d94a4bSEmmanuel Vadot * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1938d94a4bSEmmanuel Vadot * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2038d94a4bSEmmanuel Vadot * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2138d94a4bSEmmanuel Vadot * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2238d94a4bSEmmanuel Vadot * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2338d94a4bSEmmanuel Vadot * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2438d94a4bSEmmanuel Vadot * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2538d94a4bSEmmanuel Vadot * SUCH DAMAGE. 2638d94a4bSEmmanuel Vadot * 2738d94a4bSEmmanuel Vadot * $FreeBSD$ 2838d94a4bSEmmanuel Vadot */ 2938d94a4bSEmmanuel Vadot 3038d94a4bSEmmanuel Vadot #include <sys/cdefs.h> 3138d94a4bSEmmanuel Vadot __FBSDID("$FreeBSD$"); 3238d94a4bSEmmanuel Vadot 3338d94a4bSEmmanuel Vadot 3438d94a4bSEmmanuel Vadot #include <sys/param.h> 3538d94a4bSEmmanuel Vadot #include <sys/systm.h> 3638d94a4bSEmmanuel Vadot #include <sys/bio.h> 3738d94a4bSEmmanuel Vadot #include <sys/bus.h> 3838d94a4bSEmmanuel Vadot #include <sys/conf.h> 3938d94a4bSEmmanuel Vadot #include <sys/endian.h> 4038d94a4bSEmmanuel Vadot #include <sys/fcntl.h> 4138d94a4bSEmmanuel Vadot #include <sys/ioccom.h> 4238d94a4bSEmmanuel Vadot #include <sys/kernel.h> 4338d94a4bSEmmanuel Vadot #include <sys/kthread.h> 4438d94a4bSEmmanuel Vadot #include <sys/lock.h> 4538d94a4bSEmmanuel Vadot #include <sys/malloc.h> 4638d94a4bSEmmanuel Vadot #include <sys/module.h> 4738d94a4bSEmmanuel Vadot #include <sys/mutex.h> 4838d94a4bSEmmanuel Vadot #include <sys/priv.h> 4938d94a4bSEmmanuel Vadot #include <sys/slicer.h> 5038d94a4bSEmmanuel Vadot #include <sys/sysctl.h> 5138d94a4bSEmmanuel Vadot #include <sys/time.h> 5238d94a4bSEmmanuel Vadot 5338d94a4bSEmmanuel Vadot #include <dev/ofw/ofw_bus.h> 5438d94a4bSEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h> 5538d94a4bSEmmanuel Vadot 5638d94a4bSEmmanuel Vadot #include <dev/extres/regulator/regulator.h> 5738d94a4bSEmmanuel Vadot 5838d94a4bSEmmanuel Vadot #include <dev/backlight/backlight.h> 5938d94a4bSEmmanuel Vadot 6038d94a4bSEmmanuel Vadot #include <dev/pwm/ofw_pwm.h> 6138d94a4bSEmmanuel Vadot 6238d94a4bSEmmanuel Vadot #include "backlight_if.h" 6338d94a4bSEmmanuel Vadot #include "pwmbus_if.h" 6438d94a4bSEmmanuel Vadot 6538d94a4bSEmmanuel Vadot struct pwm_backlight_softc { 6638d94a4bSEmmanuel Vadot device_t pwmdev; 6738d94a4bSEmmanuel Vadot struct cdev *cdev; 6838d94a4bSEmmanuel Vadot 6938d94a4bSEmmanuel Vadot pwm_channel_t channel; 7038d94a4bSEmmanuel Vadot uint32_t *levels; 7138d94a4bSEmmanuel Vadot ssize_t nlevels; 7238d94a4bSEmmanuel Vadot int default_level; 7338d94a4bSEmmanuel Vadot ssize_t current_level; 7438d94a4bSEmmanuel Vadot 7538d94a4bSEmmanuel Vadot regulator_t power_supply; 7638d94a4bSEmmanuel Vadot uint64_t period; 7738d94a4bSEmmanuel Vadot uint64_t duty; 7838d94a4bSEmmanuel Vadot bool enabled; 7938d94a4bSEmmanuel Vadot }; 8038d94a4bSEmmanuel Vadot 8138d94a4bSEmmanuel Vadot static int pwm_backlight_find_level_per_percent(struct pwm_backlight_softc *sc, int percent); 8238d94a4bSEmmanuel Vadot 8338d94a4bSEmmanuel Vadot static struct ofw_compat_data compat_data[] = { 8438d94a4bSEmmanuel Vadot { "pwm-backlight", 1 }, 8538d94a4bSEmmanuel Vadot { NULL, 0 } 8638d94a4bSEmmanuel Vadot }; 8738d94a4bSEmmanuel Vadot 8838d94a4bSEmmanuel Vadot static int 8938d94a4bSEmmanuel Vadot pwm_backlight_probe(device_t dev) 9038d94a4bSEmmanuel Vadot { 9138d94a4bSEmmanuel Vadot 9238d94a4bSEmmanuel Vadot if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data) 9338d94a4bSEmmanuel Vadot return (ENXIO); 9438d94a4bSEmmanuel Vadot 9538d94a4bSEmmanuel Vadot device_set_desc(dev, "PWM Backlight"); 9638d94a4bSEmmanuel Vadot return (BUS_PROBE_DEFAULT); 9738d94a4bSEmmanuel Vadot } 9838d94a4bSEmmanuel Vadot 9938d94a4bSEmmanuel Vadot static int 10038d94a4bSEmmanuel Vadot pwm_backlight_attach(device_t dev) 10138d94a4bSEmmanuel Vadot { 10238d94a4bSEmmanuel Vadot struct pwm_backlight_softc *sc; 10338d94a4bSEmmanuel Vadot phandle_t node; 10438d94a4bSEmmanuel Vadot int rv; 10538d94a4bSEmmanuel Vadot 10638d94a4bSEmmanuel Vadot sc = device_get_softc(dev); 10738d94a4bSEmmanuel Vadot node = ofw_bus_get_node(dev); 10838d94a4bSEmmanuel Vadot 10938d94a4bSEmmanuel Vadot rv = pwm_get_by_ofw_propidx(dev, node, "pwms", 0, &sc->channel); 11038d94a4bSEmmanuel Vadot if (rv != 0) { 11138d94a4bSEmmanuel Vadot device_printf(dev, "Cannot map pwm channel %d\n", rv); 11238d94a4bSEmmanuel Vadot return (ENXIO); 11338d94a4bSEmmanuel Vadot } 11438d94a4bSEmmanuel Vadot 11538d94a4bSEmmanuel Vadot if (regulator_get_by_ofw_property(dev, 0, "power-supply", 11638d94a4bSEmmanuel Vadot &sc->power_supply) != 0) { 11738d94a4bSEmmanuel Vadot device_printf(dev, "No power-supply property\n"); 11838d94a4bSEmmanuel Vadot return (ENXIO); 11938d94a4bSEmmanuel Vadot } 12038d94a4bSEmmanuel Vadot 12138d94a4bSEmmanuel Vadot if (OF_hasprop(node, "brightness-levels")) { 12238d94a4bSEmmanuel Vadot sc->nlevels = OF_getencprop_alloc(node, "brightness-levels", 12338d94a4bSEmmanuel Vadot (void **)&sc->levels); 12438d94a4bSEmmanuel Vadot if (sc->nlevels <= 0) { 12538d94a4bSEmmanuel Vadot device_printf(dev, "Cannot parse brightness levels\n"); 12638d94a4bSEmmanuel Vadot return (ENXIO); 12738d94a4bSEmmanuel Vadot } 12838d94a4bSEmmanuel Vadot sc->nlevels /= sizeof(uint32_t); 12938d94a4bSEmmanuel Vadot 13038d94a4bSEmmanuel Vadot if (OF_getencprop(node, "default-brightness-level", 13138d94a4bSEmmanuel Vadot &sc->default_level, sizeof(uint32_t)) <= 0) { 13238d94a4bSEmmanuel Vadot device_printf(dev, "No default-brightness-level while brightness-levels is specified\n"); 13338d94a4bSEmmanuel Vadot return (ENXIO); 13438d94a4bSEmmanuel Vadot } else { 13538d94a4bSEmmanuel Vadot if (sc->default_level > sc->nlevels) { 13638d94a4bSEmmanuel Vadot device_printf(dev, "default-brightness-level isn't present in brightness-levels range\n"); 13738d94a4bSEmmanuel Vadot return (ENXIO); 13838d94a4bSEmmanuel Vadot } 13938d94a4bSEmmanuel Vadot sc->channel->duty = sc->channel->period * sc->levels[sc->default_level] / 100; 14038d94a4bSEmmanuel Vadot } 14138d94a4bSEmmanuel Vadot 14238d94a4bSEmmanuel Vadot if (bootverbose) { 14338d94a4bSEmmanuel Vadot device_printf(dev, "Number of levels: %zd\n", sc->nlevels); 144b4866825SEmmanuel Vadot device_printf(dev, "Configured period time: %ju\n", (uintmax_t)sc->channel->period); 145b4866825SEmmanuel Vadot device_printf(dev, "Default duty cycle: %ju\n", (uintmax_t)sc->channel->duty); 14638d94a4bSEmmanuel Vadot } 14738d94a4bSEmmanuel Vadot } else { 14838d94a4bSEmmanuel Vadot /* Get the current backlight level */ 14938d94a4bSEmmanuel Vadot PWMBUS_CHANNEL_GET_CONFIG(sc->channel->dev, 15038d94a4bSEmmanuel Vadot sc->channel->channel, 15138d94a4bSEmmanuel Vadot (unsigned int *)&sc->channel->period, 15238d94a4bSEmmanuel Vadot (unsigned int *)&sc->channel->duty); 15338d94a4bSEmmanuel Vadot if (sc->channel->duty > sc->channel->period) 15438d94a4bSEmmanuel Vadot sc->channel->duty = sc->channel->period; 15538d94a4bSEmmanuel Vadot if (bootverbose) { 156b4866825SEmmanuel Vadot device_printf(dev, "Configured period time: %ju\n", (uintmax_t)sc->channel->period); 157b4866825SEmmanuel Vadot device_printf(dev, "Default duty cycle: %ju\n", (uintmax_t)sc->channel->duty); 15838d94a4bSEmmanuel Vadot } 15938d94a4bSEmmanuel Vadot } 16038d94a4bSEmmanuel Vadot 16138d94a4bSEmmanuel Vadot regulator_enable(sc->power_supply); 16238d94a4bSEmmanuel Vadot sc->channel->enabled = true; 16338d94a4bSEmmanuel Vadot PWMBUS_CHANNEL_CONFIG(sc->channel->dev, sc->channel->channel, 16438d94a4bSEmmanuel Vadot sc->channel->period, sc->channel->duty); 16538d94a4bSEmmanuel Vadot PWMBUS_CHANNEL_ENABLE(sc->channel->dev, sc->channel->channel, 16638d94a4bSEmmanuel Vadot sc->channel->enabled); 16738d94a4bSEmmanuel Vadot 16838d94a4bSEmmanuel Vadot sc->current_level = pwm_backlight_find_level_per_percent(sc, 16938d94a4bSEmmanuel Vadot sc->channel->period / sc->channel->duty); 17038d94a4bSEmmanuel Vadot sc->cdev = backlight_register("pwm_backlight", dev); 17138d94a4bSEmmanuel Vadot if (sc->cdev == NULL) 17238d94a4bSEmmanuel Vadot device_printf(dev, "Cannot register as a backlight\n"); 17338d94a4bSEmmanuel Vadot 17438d94a4bSEmmanuel Vadot return (0); 17538d94a4bSEmmanuel Vadot } 17638d94a4bSEmmanuel Vadot 17738d94a4bSEmmanuel Vadot static int 17838d94a4bSEmmanuel Vadot pwm_backlight_detach(device_t dev) 17938d94a4bSEmmanuel Vadot { 18038d94a4bSEmmanuel Vadot struct pwm_backlight_softc *sc; 18138d94a4bSEmmanuel Vadot 18238d94a4bSEmmanuel Vadot sc = device_get_softc(dev); 18338d94a4bSEmmanuel Vadot if (sc->nlevels > 0) 18438d94a4bSEmmanuel Vadot OF_prop_free(sc->levels); 18538d94a4bSEmmanuel Vadot regulator_disable(sc->power_supply); 18638d94a4bSEmmanuel Vadot backlight_destroy(sc->cdev); 18738d94a4bSEmmanuel Vadot return (0); 18838d94a4bSEmmanuel Vadot } 18938d94a4bSEmmanuel Vadot 19038d94a4bSEmmanuel Vadot static int 19138d94a4bSEmmanuel Vadot pwm_backlight_find_level_per_percent(struct pwm_backlight_softc *sc, int percent) 19238d94a4bSEmmanuel Vadot { 19338d94a4bSEmmanuel Vadot int i; 19438d94a4bSEmmanuel Vadot int diff; 19538d94a4bSEmmanuel Vadot 19638d94a4bSEmmanuel Vadot if (percent < 0 || percent > 100) 19738d94a4bSEmmanuel Vadot return (-1); 19838d94a4bSEmmanuel Vadot 19938d94a4bSEmmanuel Vadot for (i = 0, diff = 0; i < sc->nlevels; i++) { 20038d94a4bSEmmanuel Vadot if (sc->levels[i] == percent) 20138d94a4bSEmmanuel Vadot return (i); 20238d94a4bSEmmanuel Vadot else if (sc->levels[i] < percent) 20338d94a4bSEmmanuel Vadot diff = percent - sc->levels[i]; 20438d94a4bSEmmanuel Vadot else { 20538d94a4bSEmmanuel Vadot if (diff < abs((percent - sc->levels[i]))) 20638d94a4bSEmmanuel Vadot return (i - 1); 20738d94a4bSEmmanuel Vadot else 20838d94a4bSEmmanuel Vadot return (i); 20938d94a4bSEmmanuel Vadot } 21038d94a4bSEmmanuel Vadot } 21138d94a4bSEmmanuel Vadot 21238d94a4bSEmmanuel Vadot return (-1); 21338d94a4bSEmmanuel Vadot } 21438d94a4bSEmmanuel Vadot 21538d94a4bSEmmanuel Vadot static int 21638d94a4bSEmmanuel Vadot pwm_backlight_update_status(device_t dev, struct backlight_props *props) 21738d94a4bSEmmanuel Vadot { 21838d94a4bSEmmanuel Vadot struct pwm_backlight_softc *sc; 21938d94a4bSEmmanuel Vadot int reg_status; 22038d94a4bSEmmanuel Vadot int error; 22138d94a4bSEmmanuel Vadot 22238d94a4bSEmmanuel Vadot sc = device_get_softc(dev); 22338d94a4bSEmmanuel Vadot 22438d94a4bSEmmanuel Vadot if (sc->nlevels != 0) { 22538d94a4bSEmmanuel Vadot error = pwm_backlight_find_level_per_percent(sc, 22638d94a4bSEmmanuel Vadot props->brightness); 22738d94a4bSEmmanuel Vadot if (error < 0) 22838d94a4bSEmmanuel Vadot return (ERANGE); 22938d94a4bSEmmanuel Vadot sc->current_level = error; 23038d94a4bSEmmanuel Vadot sc->channel->duty = sc->channel->period * 23138d94a4bSEmmanuel Vadot sc->levels[sc->current_level] / 100; 23238d94a4bSEmmanuel Vadot } else { 23338d94a4bSEmmanuel Vadot if (props->brightness > 100 || props->brightness < 0) 23438d94a4bSEmmanuel Vadot return (ERANGE); 23538d94a4bSEmmanuel Vadot sc->channel->duty = sc->channel->period * 23638d94a4bSEmmanuel Vadot props->brightness / 100; 23738d94a4bSEmmanuel Vadot } 23838d94a4bSEmmanuel Vadot sc->channel->enabled = true; 23938d94a4bSEmmanuel Vadot PWMBUS_CHANNEL_CONFIG(sc->channel->dev, sc->channel->channel, 24038d94a4bSEmmanuel Vadot sc->channel->period, sc->channel->duty); 24138d94a4bSEmmanuel Vadot PWMBUS_CHANNEL_ENABLE(sc->channel->dev, sc->channel->channel, 24238d94a4bSEmmanuel Vadot sc->channel->enabled); 24338d94a4bSEmmanuel Vadot error = regulator_status(sc->power_supply, ®_status); 24438d94a4bSEmmanuel Vadot if (error != 0) 24538d94a4bSEmmanuel Vadot device_printf(dev, 24638d94a4bSEmmanuel Vadot "Cannot get power-supply status: %d\n", error); 24738d94a4bSEmmanuel Vadot else { 24838d94a4bSEmmanuel Vadot if (props->brightness > 0) { 24938d94a4bSEmmanuel Vadot if (reg_status != REGULATOR_STATUS_ENABLED) 25038d94a4bSEmmanuel Vadot regulator_enable(sc->power_supply); 25138d94a4bSEmmanuel Vadot } else { 25238d94a4bSEmmanuel Vadot if (reg_status == REGULATOR_STATUS_ENABLED) 25338d94a4bSEmmanuel Vadot regulator_disable(sc->power_supply); 25438d94a4bSEmmanuel Vadot } 25538d94a4bSEmmanuel Vadot } 25638d94a4bSEmmanuel Vadot 25738d94a4bSEmmanuel Vadot return (0); 25838d94a4bSEmmanuel Vadot } 25938d94a4bSEmmanuel Vadot 26038d94a4bSEmmanuel Vadot static int 26138d94a4bSEmmanuel Vadot pwm_backlight_get_status(device_t dev, struct backlight_props *props) 26238d94a4bSEmmanuel Vadot { 26338d94a4bSEmmanuel Vadot struct pwm_backlight_softc *sc; 26438d94a4bSEmmanuel Vadot int i; 26538d94a4bSEmmanuel Vadot 26638d94a4bSEmmanuel Vadot sc = device_get_softc(dev); 26738d94a4bSEmmanuel Vadot 26838d94a4bSEmmanuel Vadot if (sc->nlevels != 0) { 26938d94a4bSEmmanuel Vadot props->brightness = sc->levels[sc->current_level]; 27038d94a4bSEmmanuel Vadot props->nlevels = sc->nlevels; 27138d94a4bSEmmanuel Vadot for (i = 0; i < sc->nlevels; i++) 27238d94a4bSEmmanuel Vadot props->levels[i] = sc->levels[i]; 27338d94a4bSEmmanuel Vadot } else { 27438d94a4bSEmmanuel Vadot props->brightness = sc->channel->duty * 100 / sc->channel->period; 27538d94a4bSEmmanuel Vadot props->nlevels = 0; 27638d94a4bSEmmanuel Vadot } 27738d94a4bSEmmanuel Vadot return (0); 27838d94a4bSEmmanuel Vadot } 27938d94a4bSEmmanuel Vadot 28038d94a4bSEmmanuel Vadot static int 28138d94a4bSEmmanuel Vadot pwm_backlight_get_info(device_t dev, struct backlight_info *info) 28238d94a4bSEmmanuel Vadot { 28338d94a4bSEmmanuel Vadot 28438d94a4bSEmmanuel Vadot info->type = BACKLIGHT_TYPE_PANEL; 28538d94a4bSEmmanuel Vadot strlcpy(info->name, "pwm-backlight", BACKLIGHTMAXNAMELENGTH); 28638d94a4bSEmmanuel Vadot return (0); 28738d94a4bSEmmanuel Vadot } 28838d94a4bSEmmanuel Vadot 28938d94a4bSEmmanuel Vadot static device_method_t pwm_backlight_methods[] = { 29038d94a4bSEmmanuel Vadot /* device_if */ 29138d94a4bSEmmanuel Vadot DEVMETHOD(device_probe, pwm_backlight_probe), 29238d94a4bSEmmanuel Vadot DEVMETHOD(device_attach, pwm_backlight_attach), 29338d94a4bSEmmanuel Vadot DEVMETHOD(device_detach, pwm_backlight_detach), 29438d94a4bSEmmanuel Vadot 29538d94a4bSEmmanuel Vadot /* backlight interface */ 29638d94a4bSEmmanuel Vadot DEVMETHOD(backlight_update_status, pwm_backlight_update_status), 29738d94a4bSEmmanuel Vadot DEVMETHOD(backlight_get_status, pwm_backlight_get_status), 29838d94a4bSEmmanuel Vadot DEVMETHOD(backlight_get_info, pwm_backlight_get_info), 29938d94a4bSEmmanuel Vadot DEVMETHOD_END 30038d94a4bSEmmanuel Vadot }; 30138d94a4bSEmmanuel Vadot 30238d94a4bSEmmanuel Vadot driver_t pwm_backlight_driver = { 30338d94a4bSEmmanuel Vadot "pwm_backlight", 30438d94a4bSEmmanuel Vadot pwm_backlight_methods, 30538d94a4bSEmmanuel Vadot sizeof(struct pwm_backlight_softc), 30638d94a4bSEmmanuel Vadot }; 30738d94a4bSEmmanuel Vadot devclass_t pwm_backlight_devclass; 30838d94a4bSEmmanuel Vadot 30938d94a4bSEmmanuel Vadot DRIVER_MODULE(pwm_backlight, simplebus, pwm_backlight_driver, 31038d94a4bSEmmanuel Vadot pwm_backlight_devclass, 0, 0); 311*43d4dfacSBrett Mastbergen MODULE_DEPEND(pwm_backlight, backlight, 1, 1, 1); 31238d94a4bSEmmanuel Vadot OFWBUS_PNP_INFO(compat_data); 313