138d94a4bSEmmanuel Vadot /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
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
2838d94a4bSEmmanuel Vadot #include <sys/param.h>
2938d94a4bSEmmanuel Vadot #include <sys/systm.h>
3038d94a4bSEmmanuel Vadot #include <sys/bio.h>
3138d94a4bSEmmanuel Vadot #include <sys/bus.h>
3238d94a4bSEmmanuel Vadot #include <sys/conf.h>
3338d94a4bSEmmanuel Vadot #include <sys/endian.h>
3438d94a4bSEmmanuel Vadot #include <sys/fcntl.h>
3538d94a4bSEmmanuel Vadot #include <sys/ioccom.h>
3638d94a4bSEmmanuel Vadot #include <sys/kernel.h>
3738d94a4bSEmmanuel Vadot #include <sys/kthread.h>
3838d94a4bSEmmanuel Vadot #include <sys/lock.h>
3938d94a4bSEmmanuel Vadot #include <sys/malloc.h>
4038d94a4bSEmmanuel Vadot #include <sys/module.h>
4138d94a4bSEmmanuel Vadot #include <sys/mutex.h>
4238d94a4bSEmmanuel Vadot #include <sys/priv.h>
4338d94a4bSEmmanuel Vadot #include <sys/slicer.h>
4438d94a4bSEmmanuel Vadot #include <sys/sysctl.h>
4538d94a4bSEmmanuel Vadot #include <sys/time.h>
4638d94a4bSEmmanuel Vadot
4738d94a4bSEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
4838d94a4bSEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h>
4938d94a4bSEmmanuel Vadot
50*b2f0caf1SEmmanuel Vadot #include <dev/regulator/regulator.h>
5138d94a4bSEmmanuel Vadot
5238d94a4bSEmmanuel Vadot #include <dev/backlight/backlight.h>
5338d94a4bSEmmanuel Vadot
5438d94a4bSEmmanuel Vadot #include <dev/pwm/ofw_pwm.h>
5538d94a4bSEmmanuel Vadot
5638d94a4bSEmmanuel Vadot #include "backlight_if.h"
5738d94a4bSEmmanuel Vadot #include "pwmbus_if.h"
5838d94a4bSEmmanuel Vadot
5938d94a4bSEmmanuel Vadot struct pwm_backlight_softc {
6038d94a4bSEmmanuel Vadot device_t pwmdev;
6138d94a4bSEmmanuel Vadot struct cdev *cdev;
6238d94a4bSEmmanuel Vadot
6338d94a4bSEmmanuel Vadot pwm_channel_t channel;
6438d94a4bSEmmanuel Vadot uint32_t *levels;
6538d94a4bSEmmanuel Vadot ssize_t nlevels;
6638d94a4bSEmmanuel Vadot int default_level;
6738d94a4bSEmmanuel Vadot ssize_t current_level;
6838d94a4bSEmmanuel Vadot
6938d94a4bSEmmanuel Vadot regulator_t power_supply;
7038d94a4bSEmmanuel Vadot uint64_t period;
7138d94a4bSEmmanuel Vadot uint64_t duty;
7238d94a4bSEmmanuel Vadot bool enabled;
7338d94a4bSEmmanuel Vadot };
7438d94a4bSEmmanuel Vadot
7538d94a4bSEmmanuel Vadot static int pwm_backlight_find_level_per_percent(struct pwm_backlight_softc *sc, int percent);
7638d94a4bSEmmanuel Vadot
7738d94a4bSEmmanuel Vadot static struct ofw_compat_data compat_data[] = {
7838d94a4bSEmmanuel Vadot { "pwm-backlight", 1 },
7938d94a4bSEmmanuel Vadot { NULL, 0 }
8038d94a4bSEmmanuel Vadot };
8138d94a4bSEmmanuel Vadot
8238d94a4bSEmmanuel Vadot static int
pwm_backlight_probe(device_t dev)8338d94a4bSEmmanuel Vadot pwm_backlight_probe(device_t dev)
8438d94a4bSEmmanuel Vadot {
8538d94a4bSEmmanuel Vadot
8638d94a4bSEmmanuel Vadot if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
8738d94a4bSEmmanuel Vadot return (ENXIO);
8838d94a4bSEmmanuel Vadot
8938d94a4bSEmmanuel Vadot device_set_desc(dev, "PWM Backlight");
9038d94a4bSEmmanuel Vadot return (BUS_PROBE_DEFAULT);
9138d94a4bSEmmanuel Vadot }
9238d94a4bSEmmanuel Vadot
9338d94a4bSEmmanuel Vadot static int
pwm_backlight_attach(device_t dev)9438d94a4bSEmmanuel Vadot pwm_backlight_attach(device_t dev)
9538d94a4bSEmmanuel Vadot {
9638d94a4bSEmmanuel Vadot struct pwm_backlight_softc *sc;
9738d94a4bSEmmanuel Vadot phandle_t node;
9838d94a4bSEmmanuel Vadot int rv;
9938d94a4bSEmmanuel Vadot
10038d94a4bSEmmanuel Vadot sc = device_get_softc(dev);
10138d94a4bSEmmanuel Vadot node = ofw_bus_get_node(dev);
10238d94a4bSEmmanuel Vadot
10338d94a4bSEmmanuel Vadot rv = pwm_get_by_ofw_propidx(dev, node, "pwms", 0, &sc->channel);
10438d94a4bSEmmanuel Vadot if (rv != 0) {
10538d94a4bSEmmanuel Vadot device_printf(dev, "Cannot map pwm channel %d\n", rv);
10638d94a4bSEmmanuel Vadot return (ENXIO);
10738d94a4bSEmmanuel Vadot }
10838d94a4bSEmmanuel Vadot
10938d94a4bSEmmanuel Vadot if (regulator_get_by_ofw_property(dev, 0, "power-supply",
11038d94a4bSEmmanuel Vadot &sc->power_supply) != 0) {
11138d94a4bSEmmanuel Vadot device_printf(dev, "No power-supply property\n");
11238d94a4bSEmmanuel Vadot return (ENXIO);
11338d94a4bSEmmanuel Vadot }
11438d94a4bSEmmanuel Vadot
11538d94a4bSEmmanuel Vadot if (OF_hasprop(node, "brightness-levels")) {
11638d94a4bSEmmanuel Vadot sc->nlevels = OF_getencprop_alloc(node, "brightness-levels",
11738d94a4bSEmmanuel Vadot (void **)&sc->levels);
11838d94a4bSEmmanuel Vadot if (sc->nlevels <= 0) {
11938d94a4bSEmmanuel Vadot device_printf(dev, "Cannot parse brightness levels\n");
12038d94a4bSEmmanuel Vadot return (ENXIO);
12138d94a4bSEmmanuel Vadot }
12238d94a4bSEmmanuel Vadot sc->nlevels /= sizeof(uint32_t);
12338d94a4bSEmmanuel Vadot
12438d94a4bSEmmanuel Vadot if (OF_getencprop(node, "default-brightness-level",
12538d94a4bSEmmanuel Vadot &sc->default_level, sizeof(uint32_t)) <= 0) {
12638d94a4bSEmmanuel Vadot device_printf(dev, "No default-brightness-level while brightness-levels is specified\n");
12738d94a4bSEmmanuel Vadot return (ENXIO);
12838d94a4bSEmmanuel Vadot } else {
12938d94a4bSEmmanuel Vadot if (sc->default_level > sc->nlevels) {
13038d94a4bSEmmanuel Vadot device_printf(dev, "default-brightness-level isn't present in brightness-levels range\n");
13138d94a4bSEmmanuel Vadot return (ENXIO);
13238d94a4bSEmmanuel Vadot }
13338d94a4bSEmmanuel Vadot sc->channel->duty = sc->channel->period * sc->levels[sc->default_level] / 100;
13438d94a4bSEmmanuel Vadot }
13538d94a4bSEmmanuel Vadot
13638d94a4bSEmmanuel Vadot if (bootverbose) {
13738d94a4bSEmmanuel Vadot device_printf(dev, "Number of levels: %zd\n", sc->nlevels);
138b4866825SEmmanuel Vadot device_printf(dev, "Configured period time: %ju\n", (uintmax_t)sc->channel->period);
139b4866825SEmmanuel Vadot device_printf(dev, "Default duty cycle: %ju\n", (uintmax_t)sc->channel->duty);
14038d94a4bSEmmanuel Vadot }
14138d94a4bSEmmanuel Vadot } else {
14238d94a4bSEmmanuel Vadot /* Get the current backlight level */
14338d94a4bSEmmanuel Vadot PWMBUS_CHANNEL_GET_CONFIG(sc->channel->dev,
14438d94a4bSEmmanuel Vadot sc->channel->channel,
14538d94a4bSEmmanuel Vadot (unsigned int *)&sc->channel->period,
14638d94a4bSEmmanuel Vadot (unsigned int *)&sc->channel->duty);
14738d94a4bSEmmanuel Vadot if (sc->channel->duty > sc->channel->period)
14838d94a4bSEmmanuel Vadot sc->channel->duty = sc->channel->period;
14938d94a4bSEmmanuel Vadot if (bootverbose) {
150b4866825SEmmanuel Vadot device_printf(dev, "Configured period time: %ju\n", (uintmax_t)sc->channel->period);
151b4866825SEmmanuel Vadot device_printf(dev, "Default duty cycle: %ju\n", (uintmax_t)sc->channel->duty);
15238d94a4bSEmmanuel Vadot }
15338d94a4bSEmmanuel Vadot }
15438d94a4bSEmmanuel Vadot
15538d94a4bSEmmanuel Vadot regulator_enable(sc->power_supply);
15638d94a4bSEmmanuel Vadot sc->channel->enabled = true;
15738d94a4bSEmmanuel Vadot PWMBUS_CHANNEL_CONFIG(sc->channel->dev, sc->channel->channel,
15838d94a4bSEmmanuel Vadot sc->channel->period, sc->channel->duty);
15938d94a4bSEmmanuel Vadot PWMBUS_CHANNEL_ENABLE(sc->channel->dev, sc->channel->channel,
16038d94a4bSEmmanuel Vadot sc->channel->enabled);
16138d94a4bSEmmanuel Vadot
16238d94a4bSEmmanuel Vadot sc->current_level = pwm_backlight_find_level_per_percent(sc,
16338d94a4bSEmmanuel Vadot sc->channel->period / sc->channel->duty);
16438d94a4bSEmmanuel Vadot sc->cdev = backlight_register("pwm_backlight", dev);
16538d94a4bSEmmanuel Vadot if (sc->cdev == NULL)
16638d94a4bSEmmanuel Vadot device_printf(dev, "Cannot register as a backlight\n");
16738d94a4bSEmmanuel Vadot
16838d94a4bSEmmanuel Vadot return (0);
16938d94a4bSEmmanuel Vadot }
17038d94a4bSEmmanuel Vadot
17138d94a4bSEmmanuel Vadot static int
pwm_backlight_detach(device_t dev)17238d94a4bSEmmanuel Vadot pwm_backlight_detach(device_t dev)
17338d94a4bSEmmanuel Vadot {
17438d94a4bSEmmanuel Vadot struct pwm_backlight_softc *sc;
17538d94a4bSEmmanuel Vadot
17638d94a4bSEmmanuel Vadot sc = device_get_softc(dev);
17738d94a4bSEmmanuel Vadot if (sc->nlevels > 0)
17838d94a4bSEmmanuel Vadot OF_prop_free(sc->levels);
17938d94a4bSEmmanuel Vadot regulator_disable(sc->power_supply);
18038d94a4bSEmmanuel Vadot backlight_destroy(sc->cdev);
18138d94a4bSEmmanuel Vadot return (0);
18238d94a4bSEmmanuel Vadot }
18338d94a4bSEmmanuel Vadot
18438d94a4bSEmmanuel Vadot static int
pwm_backlight_find_level_per_percent(struct pwm_backlight_softc * sc,int percent)18538d94a4bSEmmanuel Vadot pwm_backlight_find_level_per_percent(struct pwm_backlight_softc *sc, int percent)
18638d94a4bSEmmanuel Vadot {
18738d94a4bSEmmanuel Vadot int i;
18838d94a4bSEmmanuel Vadot int diff;
18938d94a4bSEmmanuel Vadot
19038d94a4bSEmmanuel Vadot if (percent < 0 || percent > 100)
19138d94a4bSEmmanuel Vadot return (-1);
19238d94a4bSEmmanuel Vadot
19338d94a4bSEmmanuel Vadot for (i = 0, diff = 0; i < sc->nlevels; i++) {
19438d94a4bSEmmanuel Vadot if (sc->levels[i] == percent)
19538d94a4bSEmmanuel Vadot return (i);
19638d94a4bSEmmanuel Vadot else if (sc->levels[i] < percent)
19738d94a4bSEmmanuel Vadot diff = percent - sc->levels[i];
19838d94a4bSEmmanuel Vadot else {
19938d94a4bSEmmanuel Vadot if (diff < abs((percent - sc->levels[i])))
20038d94a4bSEmmanuel Vadot return (i - 1);
20138d94a4bSEmmanuel Vadot else
20238d94a4bSEmmanuel Vadot return (i);
20338d94a4bSEmmanuel Vadot }
20438d94a4bSEmmanuel Vadot }
20538d94a4bSEmmanuel Vadot
20638d94a4bSEmmanuel Vadot return (-1);
20738d94a4bSEmmanuel Vadot }
20838d94a4bSEmmanuel Vadot
20938d94a4bSEmmanuel Vadot static int
pwm_backlight_update_status(device_t dev,struct backlight_props * props)21038d94a4bSEmmanuel Vadot pwm_backlight_update_status(device_t dev, struct backlight_props *props)
21138d94a4bSEmmanuel Vadot {
21238d94a4bSEmmanuel Vadot struct pwm_backlight_softc *sc;
21338d94a4bSEmmanuel Vadot int reg_status;
21438d94a4bSEmmanuel Vadot int error;
21538d94a4bSEmmanuel Vadot
21638d94a4bSEmmanuel Vadot sc = device_get_softc(dev);
21738d94a4bSEmmanuel Vadot
21838d94a4bSEmmanuel Vadot if (sc->nlevels != 0) {
21938d94a4bSEmmanuel Vadot error = pwm_backlight_find_level_per_percent(sc,
22038d94a4bSEmmanuel Vadot props->brightness);
22138d94a4bSEmmanuel Vadot if (error < 0)
22238d94a4bSEmmanuel Vadot return (ERANGE);
22338d94a4bSEmmanuel Vadot sc->current_level = error;
22438d94a4bSEmmanuel Vadot sc->channel->duty = sc->channel->period *
22538d94a4bSEmmanuel Vadot sc->levels[sc->current_level] / 100;
22638d94a4bSEmmanuel Vadot } else {
22738d94a4bSEmmanuel Vadot if (props->brightness > 100 || props->brightness < 0)
22838d94a4bSEmmanuel Vadot return (ERANGE);
22938d94a4bSEmmanuel Vadot sc->channel->duty = sc->channel->period *
23038d94a4bSEmmanuel Vadot props->brightness / 100;
23138d94a4bSEmmanuel Vadot }
23238d94a4bSEmmanuel Vadot sc->channel->enabled = true;
23338d94a4bSEmmanuel Vadot PWMBUS_CHANNEL_CONFIG(sc->channel->dev, sc->channel->channel,
23438d94a4bSEmmanuel Vadot sc->channel->period, sc->channel->duty);
23538d94a4bSEmmanuel Vadot PWMBUS_CHANNEL_ENABLE(sc->channel->dev, sc->channel->channel,
23638d94a4bSEmmanuel Vadot sc->channel->enabled);
23738d94a4bSEmmanuel Vadot error = regulator_status(sc->power_supply, ®_status);
23838d94a4bSEmmanuel Vadot if (error != 0)
23938d94a4bSEmmanuel Vadot device_printf(dev,
24038d94a4bSEmmanuel Vadot "Cannot get power-supply status: %d\n", error);
24138d94a4bSEmmanuel Vadot else {
24238d94a4bSEmmanuel Vadot if (props->brightness > 0) {
24338d94a4bSEmmanuel Vadot if (reg_status != REGULATOR_STATUS_ENABLED)
24438d94a4bSEmmanuel Vadot regulator_enable(sc->power_supply);
24538d94a4bSEmmanuel Vadot } else {
24638d94a4bSEmmanuel Vadot if (reg_status == REGULATOR_STATUS_ENABLED)
24738d94a4bSEmmanuel Vadot regulator_disable(sc->power_supply);
24838d94a4bSEmmanuel Vadot }
24938d94a4bSEmmanuel Vadot }
25038d94a4bSEmmanuel Vadot
25138d94a4bSEmmanuel Vadot return (0);
25238d94a4bSEmmanuel Vadot }
25338d94a4bSEmmanuel Vadot
25438d94a4bSEmmanuel Vadot static int
pwm_backlight_get_status(device_t dev,struct backlight_props * props)25538d94a4bSEmmanuel Vadot pwm_backlight_get_status(device_t dev, struct backlight_props *props)
25638d94a4bSEmmanuel Vadot {
25738d94a4bSEmmanuel Vadot struct pwm_backlight_softc *sc;
25838d94a4bSEmmanuel Vadot int i;
25938d94a4bSEmmanuel Vadot
26038d94a4bSEmmanuel Vadot sc = device_get_softc(dev);
26138d94a4bSEmmanuel Vadot
26238d94a4bSEmmanuel Vadot if (sc->nlevels != 0) {
26338d94a4bSEmmanuel Vadot props->brightness = sc->levels[sc->current_level];
26438d94a4bSEmmanuel Vadot props->nlevels = sc->nlevels;
26538d94a4bSEmmanuel Vadot for (i = 0; i < sc->nlevels; i++)
26638d94a4bSEmmanuel Vadot props->levels[i] = sc->levels[i];
26738d94a4bSEmmanuel Vadot } else {
26838d94a4bSEmmanuel Vadot props->brightness = sc->channel->duty * 100 / sc->channel->period;
26938d94a4bSEmmanuel Vadot props->nlevels = 0;
27038d94a4bSEmmanuel Vadot }
27138d94a4bSEmmanuel Vadot return (0);
27238d94a4bSEmmanuel Vadot }
27338d94a4bSEmmanuel Vadot
27438d94a4bSEmmanuel Vadot static int
pwm_backlight_get_info(device_t dev,struct backlight_info * info)27538d94a4bSEmmanuel Vadot pwm_backlight_get_info(device_t dev, struct backlight_info *info)
27638d94a4bSEmmanuel Vadot {
27738d94a4bSEmmanuel Vadot
27838d94a4bSEmmanuel Vadot info->type = BACKLIGHT_TYPE_PANEL;
27938d94a4bSEmmanuel Vadot strlcpy(info->name, "pwm-backlight", BACKLIGHTMAXNAMELENGTH);
28038d94a4bSEmmanuel Vadot return (0);
28138d94a4bSEmmanuel Vadot }
28238d94a4bSEmmanuel Vadot
28338d94a4bSEmmanuel Vadot static device_method_t pwm_backlight_methods[] = {
28438d94a4bSEmmanuel Vadot /* device_if */
28538d94a4bSEmmanuel Vadot DEVMETHOD(device_probe, pwm_backlight_probe),
28638d94a4bSEmmanuel Vadot DEVMETHOD(device_attach, pwm_backlight_attach),
28738d94a4bSEmmanuel Vadot DEVMETHOD(device_detach, pwm_backlight_detach),
28838d94a4bSEmmanuel Vadot
28938d94a4bSEmmanuel Vadot /* backlight interface */
29038d94a4bSEmmanuel Vadot DEVMETHOD(backlight_update_status, pwm_backlight_update_status),
29138d94a4bSEmmanuel Vadot DEVMETHOD(backlight_get_status, pwm_backlight_get_status),
29238d94a4bSEmmanuel Vadot DEVMETHOD(backlight_get_info, pwm_backlight_get_info),
29338d94a4bSEmmanuel Vadot DEVMETHOD_END
29438d94a4bSEmmanuel Vadot };
29538d94a4bSEmmanuel Vadot
29638d94a4bSEmmanuel Vadot driver_t pwm_backlight_driver = {
29738d94a4bSEmmanuel Vadot "pwm_backlight",
29838d94a4bSEmmanuel Vadot pwm_backlight_methods,
29938d94a4bSEmmanuel Vadot sizeof(struct pwm_backlight_softc),
30038d94a4bSEmmanuel Vadot };
30138d94a4bSEmmanuel Vadot
302024d9473SJohn Baldwin DRIVER_MODULE(pwm_backlight, simplebus, pwm_backlight_driver, 0, 0);
30343d4dfacSBrett Mastbergen MODULE_DEPEND(pwm_backlight, backlight, 1, 1, 1);
30438d94a4bSEmmanuel Vadot OFWBUS_PNP_INFO(compat_data);
305