1*e8baf380Sjmcneill /* $NetBSD: pwm_backlight.c,v 1.10 2021/11/07 17:14:09 jmcneill Exp $ */
239f9e242Sjmcneill
339f9e242Sjmcneill /*-
439f9e242Sjmcneill * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca>
539f9e242Sjmcneill * All rights reserved.
639f9e242Sjmcneill *
739f9e242Sjmcneill * Redistribution and use in source and binary forms, with or without
839f9e242Sjmcneill * modification, are permitted provided that the following conditions
939f9e242Sjmcneill * are met:
1039f9e242Sjmcneill * 1. Redistributions of source code must retain the above copyright
1139f9e242Sjmcneill * notice, this list of conditions and the following disclaimer.
1239f9e242Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
1339f9e242Sjmcneill * notice, this list of conditions and the following disclaimer in the
1439f9e242Sjmcneill * documentation and/or other materials provided with the distribution.
1539f9e242Sjmcneill *
1639f9e242Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1739f9e242Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1839f9e242Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1939f9e242Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2039f9e242Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2139f9e242Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2239f9e242Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2339f9e242Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2439f9e242Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2539f9e242Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2639f9e242Sjmcneill * SUCH DAMAGE.
2739f9e242Sjmcneill */
2839f9e242Sjmcneill
2939f9e242Sjmcneill #include <sys/cdefs.h>
30*e8baf380Sjmcneill __KERNEL_RCSID(0, "$NetBSD: pwm_backlight.c,v 1.10 2021/11/07 17:14:09 jmcneill Exp $");
3139f9e242Sjmcneill
3239f9e242Sjmcneill #include <sys/param.h>
3339f9e242Sjmcneill #include <sys/bus.h>
3439f9e242Sjmcneill #include <sys/device.h>
3539f9e242Sjmcneill #include <sys/systm.h>
3639f9e242Sjmcneill #include <sys/sysctl.h>
3739f9e242Sjmcneill #include <sys/kmem.h>
3839f9e242Sjmcneill #include <sys/gpio.h>
3939f9e242Sjmcneill
4039f9e242Sjmcneill #include <dev/pwm/pwmvar.h>
4139f9e242Sjmcneill
4239f9e242Sjmcneill #include <dev/fdt/fdtvar.h>
4339f9e242Sjmcneill
4439f9e242Sjmcneill struct pwm_backlight_softc {
4539f9e242Sjmcneill device_t sc_dev;
4639f9e242Sjmcneill pwm_tag_t sc_pwm;
4739f9e242Sjmcneill struct fdtbus_gpio_pin *sc_pin;
4839f9e242Sjmcneill
4939f9e242Sjmcneill u_int *sc_levels;
5039f9e242Sjmcneill u_int sc_nlevels;
51bb3a17a5Sjmcneill u_int sc_curlevel;
5239f9e242Sjmcneill
5339f9e242Sjmcneill char *sc_levelstr;
54eec5e924Sjmcneill
55eec5e924Sjmcneill bool sc_lid_state;
5639f9e242Sjmcneill };
5739f9e242Sjmcneill
5839f9e242Sjmcneill static int pwm_backlight_match(device_t, cfdata_t, void *);
5939f9e242Sjmcneill static void pwm_backlight_attach(device_t, device_t, void *);
6039f9e242Sjmcneill
6139f9e242Sjmcneill static void pwm_backlight_sysctl_init(struct pwm_backlight_softc *);
6239f9e242Sjmcneill static void pwm_backlight_pmf_init(struct pwm_backlight_softc *);
634ea65bdbSmrg static void pwm_backlight_set(struct pwm_backlight_softc *, u_int, bool);
6439f9e242Sjmcneill static u_int pwm_backlight_get(struct pwm_backlight_softc *);
6539f9e242Sjmcneill
666e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
676e54367aSthorpej { .compat = "pwm-backlight" },
686e54367aSthorpej DEVICE_COMPAT_EOL
6939f9e242Sjmcneill };
7039f9e242Sjmcneill
7139f9e242Sjmcneill CFATTACH_DECL_NEW(pwmbacklight, sizeof(struct pwm_backlight_softc),
7239f9e242Sjmcneill pwm_backlight_match, pwm_backlight_attach, NULL, NULL);
7339f9e242Sjmcneill
7439f9e242Sjmcneill static int
pwm_backlight_match(device_t parent,cfdata_t cf,void * aux)7539f9e242Sjmcneill pwm_backlight_match(device_t parent, cfdata_t cf, void *aux)
7639f9e242Sjmcneill {
7739f9e242Sjmcneill struct fdt_attach_args * const faa = aux;
7839f9e242Sjmcneill
796e54367aSthorpej return of_compatible_match(faa->faa_phandle, compat_data);
8039f9e242Sjmcneill }
8139f9e242Sjmcneill
8239f9e242Sjmcneill static void
pwm_backlight_attach(device_t parent,device_t self,void * aux)8339f9e242Sjmcneill pwm_backlight_attach(device_t parent, device_t self, void *aux)
8439f9e242Sjmcneill {
8539f9e242Sjmcneill struct pwm_backlight_softc * const sc = device_private(self);
8639f9e242Sjmcneill struct fdt_attach_args * const faa = aux;
8739f9e242Sjmcneill const int phandle = faa->faa_phandle;
8839f9e242Sjmcneill const u_int *levels;
89*e8baf380Sjmcneill u_int default_level = 0;
9039f9e242Sjmcneill u_int n;
9139f9e242Sjmcneill int len;
9239f9e242Sjmcneill
9339f9e242Sjmcneill sc->sc_dev = self;
9439f9e242Sjmcneill sc->sc_pwm = fdtbus_pwm_acquire(phandle, "pwms");
9539f9e242Sjmcneill if (sc->sc_pwm == NULL) {
9639f9e242Sjmcneill aprint_error(": couldn't acquire pwm\n");
9739f9e242Sjmcneill return;
9839f9e242Sjmcneill }
9939f9e242Sjmcneill if (of_hasprop(phandle, "enable-gpios")) {
10039f9e242Sjmcneill sc->sc_pin = fdtbus_gpio_acquire(phandle, "enable-gpios",
10139f9e242Sjmcneill GPIO_PIN_OUTPUT);
10239f9e242Sjmcneill if (!sc->sc_pin) {
10339f9e242Sjmcneill aprint_error(": couldn't acquire enable gpio\n");
10439f9e242Sjmcneill return;
10539f9e242Sjmcneill }
1063949b506Sjmcneill fdtbus_gpio_write(sc->sc_pin, 1);
10739f9e242Sjmcneill }
10839f9e242Sjmcneill
10939f9e242Sjmcneill levels = fdtbus_get_prop(phandle, "brightness-levels", &len);
110*e8baf380Sjmcneill if (levels != NULL) {
11139f9e242Sjmcneill sc->sc_nlevels = len / 4;
112*e8baf380Sjmcneill sc->sc_levels = kmem_alloc(len, KM_SLEEP);
11339f9e242Sjmcneill for (n = 0; n < sc->sc_nlevels; n++)
11439f9e242Sjmcneill sc->sc_levels[n] = be32toh(levels[n]);
115*e8baf380Sjmcneill } else {
116*e8baf380Sjmcneill sc->sc_nlevels = 256;
117*e8baf380Sjmcneill sc->sc_levels = kmem_alloc(sc->sc_nlevels * 4, KM_SLEEP);
118*e8baf380Sjmcneill for (n = 0; n < sc->sc_nlevels; n++)
119*e8baf380Sjmcneill sc->sc_levels[n] = n;
120*e8baf380Sjmcneill default_level = 200;
121*e8baf380Sjmcneill }
12239f9e242Sjmcneill
12339f9e242Sjmcneill aprint_naive("\n");
12439f9e242Sjmcneill aprint_normal(": PWM Backlight");
125d45d7be8Sjmcneill if (sc->sc_nlevels > 1) {
126d45d7be8Sjmcneill aprint_normal(", %u-%u (%u steps)",
127d45d7be8Sjmcneill sc->sc_levels[0], sc->sc_levels[sc->sc_nlevels - 1],
128d45d7be8Sjmcneill sc->sc_nlevels);
12939f9e242Sjmcneill }
13039f9e242Sjmcneill aprint_normal("\n");
13139f9e242Sjmcneill
132eec5e924Sjmcneill sc->sc_lid_state = true;
133eec5e924Sjmcneill
134*e8baf380Sjmcneill of_getprop_uint32(phandle, "default-brightness-level", &default_level);
135*e8baf380Sjmcneill if (default_level != 0) {
13639f9e242Sjmcneill /* set the default level now */
1374ea65bdbSmrg pwm_backlight_set(sc, default_level, true);
13839f9e242Sjmcneill }
13939f9e242Sjmcneill
140bb3a17a5Sjmcneill sc->sc_curlevel = pwm_backlight_get(sc);
141bb3a17a5Sjmcneill
14239f9e242Sjmcneill pwm_backlight_sysctl_init(sc);
14339f9e242Sjmcneill pwm_backlight_pmf_init(sc);
14439f9e242Sjmcneill }
14539f9e242Sjmcneill
14639f9e242Sjmcneill static void
pwm_backlight_set(struct pwm_backlight_softc * sc,u_int index,bool set_cur)1474ea65bdbSmrg pwm_backlight_set(struct pwm_backlight_softc *sc, u_int index, bool set_cur)
14839f9e242Sjmcneill {
14939f9e242Sjmcneill struct pwm_config conf;
15039f9e242Sjmcneill
15139f9e242Sjmcneill if (index >= sc->sc_nlevels)
15239f9e242Sjmcneill return;
15339f9e242Sjmcneill
1540ed92ee0Shkenken KASSERT(sc->sc_levels[sc->sc_nlevels - 1] != 0);
1550ed92ee0Shkenken aprint_debug_dev(sc->sc_dev, "set duty cycle to %u%%\n",
1560ed92ee0Shkenken (100 * sc->sc_levels[index]) / sc->sc_levels[sc->sc_nlevels - 1]);
15739f9e242Sjmcneill
15839f9e242Sjmcneill pwm_disable(sc->sc_pwm);
15939f9e242Sjmcneill pwm_get_config(sc->sc_pwm, &conf);
16039f9e242Sjmcneill conf.duty_cycle = (conf.period * sc->sc_levels[index]) / sc->sc_levels[sc->sc_nlevels - 1];
16139f9e242Sjmcneill pwm_set_config(sc->sc_pwm, &conf);
16239f9e242Sjmcneill pwm_enable(sc->sc_pwm);
1634ea65bdbSmrg
1644ea65bdbSmrg if (set_cur)
1654ea65bdbSmrg sc->sc_curlevel = index;
16639f9e242Sjmcneill }
16739f9e242Sjmcneill
16839f9e242Sjmcneill static u_int
pwm_backlight_get(struct pwm_backlight_softc * sc)16939f9e242Sjmcneill pwm_backlight_get(struct pwm_backlight_softc *sc)
17039f9e242Sjmcneill {
17139f9e242Sjmcneill struct pwm_config conf;
17239f9e242Sjmcneill u_int raw_val, n;
17339f9e242Sjmcneill
17439f9e242Sjmcneill pwm_get_config(sc->sc_pwm, &conf);
17539f9e242Sjmcneill
17639f9e242Sjmcneill raw_val = (conf.duty_cycle * sc->sc_levels[sc->sc_nlevels - 1]) / conf.period;
17739f9e242Sjmcneill
17839f9e242Sjmcneill /* Return the closest setting to the raw value */
17939f9e242Sjmcneill for (n = 0; n < sc->sc_nlevels; n++) {
18039f9e242Sjmcneill if (raw_val <= sc->sc_levels[n])
18139f9e242Sjmcneill break;
18239f9e242Sjmcneill }
18339f9e242Sjmcneill return n;
18439f9e242Sjmcneill }
18539f9e242Sjmcneill
18639f9e242Sjmcneill static int
pwm_backlight_sysctl_helper(SYSCTLFN_ARGS)18739f9e242Sjmcneill pwm_backlight_sysctl_helper(SYSCTLFN_ARGS)
18839f9e242Sjmcneill {
18939f9e242Sjmcneill struct pwm_backlight_softc * const sc = rnode->sysctl_data;
19039f9e242Sjmcneill struct sysctlnode node;
19139f9e242Sjmcneill int error;
19239f9e242Sjmcneill u_int level, n;
19339f9e242Sjmcneill
19439f9e242Sjmcneill node = *rnode;
19539f9e242Sjmcneill node.sysctl_data = &level;
19639f9e242Sjmcneill
19739f9e242Sjmcneill n = pwm_backlight_get(sc);
19839f9e242Sjmcneill level = sc->sc_levels[n];
19939f9e242Sjmcneill
20039f9e242Sjmcneill error = sysctl_lookup(SYSCTLFN_CALL(&node));
20139f9e242Sjmcneill if (error || newp == NULL)
20239f9e242Sjmcneill return error;
20339f9e242Sjmcneill
20439f9e242Sjmcneill for (n = 0; n < sc->sc_nlevels; n++) {
20539f9e242Sjmcneill if (sc->sc_levels[n] == level) {
2064ea65bdbSmrg pwm_backlight_set(sc, n, true);
20739f9e242Sjmcneill return 0;
20839f9e242Sjmcneill }
20939f9e242Sjmcneill }
21039f9e242Sjmcneill
21139f9e242Sjmcneill return EINVAL;
21239f9e242Sjmcneill }
21339f9e242Sjmcneill
21439f9e242Sjmcneill static void
pwm_backlight_sysctl_init(struct pwm_backlight_softc * sc)21539f9e242Sjmcneill pwm_backlight_sysctl_init(struct pwm_backlight_softc *sc)
21639f9e242Sjmcneill {
21739f9e242Sjmcneill const struct sysctlnode *node, *pwmnode;
21839f9e242Sjmcneill struct sysctllog *log = NULL;
21939f9e242Sjmcneill int error;
22039f9e242Sjmcneill u_int n;
22139f9e242Sjmcneill
22239f9e242Sjmcneill sc->sc_levelstr = kmem_zalloc(strlen("XXXXX ") * sc->sc_nlevels, KM_SLEEP);
22339f9e242Sjmcneill for (n = 0; n < sc->sc_nlevels; n++) {
22439f9e242Sjmcneill char buf[7];
22539f9e242Sjmcneill snprintf(buf, sizeof(buf), n ? " %u" : "%u", sc->sc_levels[n]);
22639f9e242Sjmcneill strcat(sc->sc_levelstr, buf);
22739f9e242Sjmcneill }
22839f9e242Sjmcneill
22939f9e242Sjmcneill error = sysctl_createv(&log, 0, NULL, &node,
23039f9e242Sjmcneill CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
23139f9e242Sjmcneill NULL, 0, NULL, 0, CTL_HW, CTL_EOL);
23239f9e242Sjmcneill if (error)
23339f9e242Sjmcneill goto failed;
23439f9e242Sjmcneill
23539f9e242Sjmcneill error = sysctl_createv(&log, 0, &node, &pwmnode,
23639f9e242Sjmcneill 0, CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
23739f9e242Sjmcneill NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
23839f9e242Sjmcneill if (error)
23939f9e242Sjmcneill goto failed;
24039f9e242Sjmcneill
24139f9e242Sjmcneill error = sysctl_createv(&log, 0, &pwmnode, NULL,
24239f9e242Sjmcneill 0, CTLTYPE_STRING, "levels", NULL,
2436c32e862Sjmcneill NULL, 0, sc->sc_levelstr, 0,
24439f9e242Sjmcneill CTL_CREATE, CTL_EOL);
24539f9e242Sjmcneill if (error)
24639f9e242Sjmcneill goto failed;
24739f9e242Sjmcneill
24839f9e242Sjmcneill error = sysctl_createv(&log, 0, &pwmnode, NULL,
24939f9e242Sjmcneill CTLFLAG_READWRITE, CTLTYPE_INT, "level", NULL,
25039f9e242Sjmcneill pwm_backlight_sysctl_helper, 0, (void *)sc, 0,
25139f9e242Sjmcneill CTL_CREATE, CTL_EOL);
25239f9e242Sjmcneill if (error)
25339f9e242Sjmcneill goto failed;
25439f9e242Sjmcneill
25539f9e242Sjmcneill return;
25639f9e242Sjmcneill
25739f9e242Sjmcneill failed:
25839f9e242Sjmcneill aprint_error_dev(sc->sc_dev, "couldn't create sysctl nodes: %d\n", error);
25939f9e242Sjmcneill sysctl_teardown(&log);
26039f9e242Sjmcneill }
26139f9e242Sjmcneill
26239f9e242Sjmcneill static void
pwm_backlight_enable(struct pwm_backlight_softc * sc,int enable)263bb3a17a5Sjmcneill pwm_backlight_enable(struct pwm_backlight_softc *sc, int enable)
264bb3a17a5Sjmcneill {
265bb3a17a5Sjmcneill if (sc->sc_pin)
266bb3a17a5Sjmcneill fdtbus_gpio_write(sc->sc_pin, enable);
267bb3a17a5Sjmcneill else
2684ea65bdbSmrg pwm_backlight_set(sc, enable ? sc->sc_curlevel : 0, false);
269bb3a17a5Sjmcneill }
270bb3a17a5Sjmcneill
271bb3a17a5Sjmcneill static void
pwm_backlight_display_on(device_t dev)27239f9e242Sjmcneill pwm_backlight_display_on(device_t dev)
27339f9e242Sjmcneill {
27439f9e242Sjmcneill struct pwm_backlight_softc * const sc = device_private(dev);
27539f9e242Sjmcneill
276bb3a17a5Sjmcneill if (sc->sc_lid_state)
277bb3a17a5Sjmcneill pwm_backlight_enable(sc, 1);
27839f9e242Sjmcneill }
27939f9e242Sjmcneill
28039f9e242Sjmcneill static void
pwm_backlight_display_off(device_t dev)28139f9e242Sjmcneill pwm_backlight_display_off(device_t dev)
28239f9e242Sjmcneill {
28339f9e242Sjmcneill struct pwm_backlight_softc * const sc = device_private(dev);
28439f9e242Sjmcneill
285bb3a17a5Sjmcneill pwm_backlight_enable(sc, 0);
28639f9e242Sjmcneill }
28739f9e242Sjmcneill
28839f9e242Sjmcneill static void
pwm_backlight_chassis_lid_open(device_t dev)289eec5e924Sjmcneill pwm_backlight_chassis_lid_open(device_t dev)
290eec5e924Sjmcneill {
291eec5e924Sjmcneill struct pwm_backlight_softc * const sc = device_private(dev);
292eec5e924Sjmcneill
293eec5e924Sjmcneill sc->sc_lid_state = true;
294eec5e924Sjmcneill
295bb3a17a5Sjmcneill pwm_backlight_enable(sc, 1);
296eec5e924Sjmcneill }
297eec5e924Sjmcneill
298eec5e924Sjmcneill static void
pwm_backlight_chassis_lid_close(device_t dev)299eec5e924Sjmcneill pwm_backlight_chassis_lid_close(device_t dev)
300eec5e924Sjmcneill {
301eec5e924Sjmcneill struct pwm_backlight_softc * const sc = device_private(dev);
302eec5e924Sjmcneill
303eec5e924Sjmcneill sc->sc_lid_state = false;
304eec5e924Sjmcneill
305bb3a17a5Sjmcneill pwm_backlight_enable(sc, 0);
306eec5e924Sjmcneill }
307eec5e924Sjmcneill
308eec5e924Sjmcneill static void
pwm_backlight_display_brightness_up(device_t dev)30939f9e242Sjmcneill pwm_backlight_display_brightness_up(device_t dev)
31039f9e242Sjmcneill {
31139f9e242Sjmcneill struct pwm_backlight_softc * const sc = device_private(dev);
31239f9e242Sjmcneill u_int n;
31339f9e242Sjmcneill
31439f9e242Sjmcneill n = pwm_backlight_get(sc);
31539f9e242Sjmcneill if (n < sc->sc_nlevels - 1)
3164ea65bdbSmrg pwm_backlight_set(sc, n + 1, true);
31739f9e242Sjmcneill }
31839f9e242Sjmcneill
31939f9e242Sjmcneill static void
pwm_backlight_display_brightness_down(device_t dev)32039f9e242Sjmcneill pwm_backlight_display_brightness_down(device_t dev)
32139f9e242Sjmcneill {
32239f9e242Sjmcneill struct pwm_backlight_softc * const sc = device_private(dev);
32339f9e242Sjmcneill u_int n;
32439f9e242Sjmcneill
32539f9e242Sjmcneill n = pwm_backlight_get(sc);
32639f9e242Sjmcneill if (n > 0)
3274ea65bdbSmrg pwm_backlight_set(sc, n - 1, true);
32839f9e242Sjmcneill }
32939f9e242Sjmcneill
33039f9e242Sjmcneill static void
pwm_backlight_pmf_init(struct pwm_backlight_softc * sc)33139f9e242Sjmcneill pwm_backlight_pmf_init(struct pwm_backlight_softc *sc)
33239f9e242Sjmcneill {
33339f9e242Sjmcneill pmf_event_register(sc->sc_dev, PMFE_DISPLAY_ON,
33439f9e242Sjmcneill pwm_backlight_display_on, true);
33539f9e242Sjmcneill pmf_event_register(sc->sc_dev, PMFE_DISPLAY_OFF,
33639f9e242Sjmcneill pwm_backlight_display_off, true);
337eec5e924Sjmcneill pmf_event_register(sc->sc_dev, PMFE_CHASSIS_LID_OPEN,
338eec5e924Sjmcneill pwm_backlight_chassis_lid_open, true);
339eec5e924Sjmcneill pmf_event_register(sc->sc_dev, PMFE_CHASSIS_LID_CLOSE,
340eec5e924Sjmcneill pwm_backlight_chassis_lid_close, true);
34139f9e242Sjmcneill pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_UP,
34239f9e242Sjmcneill pwm_backlight_display_brightness_up, true);
34339f9e242Sjmcneill pmf_event_register(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_DOWN,
34439f9e242Sjmcneill pwm_backlight_display_brightness_down, true);
34539f9e242Sjmcneill }
346