xref: /openbsd-src/sys/dev/fdt/gpiobl.c (revision 56d4042b3888883b0d09babf274e25c5b920d312)
1*56d4042bStobhe /*	$OpenBSD: gpiobl.c,v 1.3 2023/01/03 10:59:00 tobhe Exp $	*/
2b0ed8e09Stobhe /*
3b0ed8e09Stobhe  * Copyright (c) 2022 Tobias Heider <tobhe@openbsd.org>
4b0ed8e09Stobhe  *
5b0ed8e09Stobhe  * Permission to use, copy, modify, and distribute this software for any
6b0ed8e09Stobhe  * purpose with or without fee is hereby granted, provided that the above
7b0ed8e09Stobhe  * copyright notice and this permission notice appear in all copies.
8b0ed8e09Stobhe  *
9b0ed8e09Stobhe  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10b0ed8e09Stobhe  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11b0ed8e09Stobhe  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12b0ed8e09Stobhe  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13b0ed8e09Stobhe  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14b0ed8e09Stobhe  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15b0ed8e09Stobhe  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16b0ed8e09Stobhe  */
17b0ed8e09Stobhe 
18b0ed8e09Stobhe #include <sys/param.h>
19b0ed8e09Stobhe #include <sys/systm.h>
20b0ed8e09Stobhe #include <sys/device.h>
21b0ed8e09Stobhe #include <sys/malloc.h>
22b0ed8e09Stobhe #include <sys/task.h>
23b0ed8e09Stobhe 
24b0ed8e09Stobhe #include <machine/fdt.h>
25b0ed8e09Stobhe #include <machine/bus.h>
26b0ed8e09Stobhe 
27b0ed8e09Stobhe #include <dev/ofw/openfirm.h>
28b0ed8e09Stobhe #include <dev/ofw/ofw_gpio.h>
29b0ed8e09Stobhe #include <dev/ofw/ofw_misc.h>
30b0ed8e09Stobhe 
31b0ed8e09Stobhe #include <dev/wscons/wsconsio.h>
32b0ed8e09Stobhe #include <dev/wscons/wsdisplayvar.h>
33b0ed8e09Stobhe 
34f2edca47Stobhe extern void (*simplefb_burn_hook)(u_int);
35f2edca47Stobhe 
36b0ed8e09Stobhe struct gpiobl_softc {
37b0ed8e09Stobhe 	struct device		sc_dev;
38b0ed8e09Stobhe 	int			sc_on;
39b0ed8e09Stobhe 	uint32_t		sc_gpio[3];
40b0ed8e09Stobhe 	struct task		sc_task;
41b0ed8e09Stobhe };
42b0ed8e09Stobhe 
43b0ed8e09Stobhe struct gpiobl_softc *sc_gpiobl;
44b0ed8e09Stobhe 
45b0ed8e09Stobhe int	gpiobl_match(struct device *, void *, void *);
46b0ed8e09Stobhe void	gpiobl_attach(struct device *, struct device *, void *);
47*56d4042bStobhe int	gpiobl_activate(struct device *, int);
48b0ed8e09Stobhe 
49b0ed8e09Stobhe const struct cfattach gpiobl_ca = {
50*56d4042bStobhe 	sizeof(struct gpiobl_softc), gpiobl_match, gpiobl_attach, NULL,
51*56d4042bStobhe 	gpiobl_activate
52b0ed8e09Stobhe };
53b0ed8e09Stobhe 
54b0ed8e09Stobhe struct cfdriver gpiobl_cd = {
55b0ed8e09Stobhe 	NULL, "gpiobl", DV_DULL
56b0ed8e09Stobhe };
57b0ed8e09Stobhe 
58f2edca47Stobhe void gpiobl_set(u_int);
59b0ed8e09Stobhe void gpiobl_task(void *);
60b0ed8e09Stobhe 
61b0ed8e09Stobhe int
gpiobl_match(struct device * parent,void * match,void * aux)62b0ed8e09Stobhe gpiobl_match(struct device *parent, void *match, void *aux)
63b0ed8e09Stobhe {
64b0ed8e09Stobhe 	struct fdt_attach_args *faa = aux;
65b0ed8e09Stobhe 
66b0ed8e09Stobhe 	return OF_is_compatible(faa->fa_node, "gpio-backlight");
67b0ed8e09Stobhe }
68b0ed8e09Stobhe 
69b0ed8e09Stobhe 
70b0ed8e09Stobhe void
gpiobl_attach(struct device * parent,struct device * self,void * aux)71b0ed8e09Stobhe gpiobl_attach(struct device *parent, struct device *self, void *aux)
72b0ed8e09Stobhe {
73b0ed8e09Stobhe 	struct gpiobl_softc *sc = (struct gpiobl_softc *)self;
74b0ed8e09Stobhe 	struct fdt_attach_args *faa = aux;
75b0ed8e09Stobhe 	size_t len;
76b0ed8e09Stobhe 
77b0ed8e09Stobhe 	len = OF_getproplen(faa->fa_node, "gpios");
78b0ed8e09Stobhe 	if (len <= 0)
79b0ed8e09Stobhe 		return;
80b0ed8e09Stobhe 	OF_getpropintarray(faa->fa_node, "gpios", sc->sc_gpio, len);
81b0ed8e09Stobhe 	gpio_controller_config_pin(sc->sc_gpio, GPIO_CONFIG_OUTPUT);
82b0ed8e09Stobhe 
83b0ed8e09Stobhe 	sc->sc_on = OF_getpropbool(faa->fa_node, "default-on");
84b0ed8e09Stobhe 	sc_gpiobl = sc;
85b0ed8e09Stobhe 
86b0ed8e09Stobhe 	task_set(&sc->sc_task, gpiobl_task, sc);
87f2edca47Stobhe 
88f2edca47Stobhe 	simplefb_burn_hook = gpiobl_set;
89f2edca47Stobhe 
90b0ed8e09Stobhe 	printf("\n");
91b0ed8e09Stobhe }
92b0ed8e09Stobhe 
93*56d4042bStobhe int
gpiobl_activate(struct device * self,int act)94*56d4042bStobhe gpiobl_activate(struct device *self, int act)
95*56d4042bStobhe {
96*56d4042bStobhe 	struct gpiobl_softc *sc = (struct gpiobl_softc *)self;
97*56d4042bStobhe 
98*56d4042bStobhe 	switch (act) {
99*56d4042bStobhe 	case DVACT_QUIESCE:
100*56d4042bStobhe 		gpio_controller_set_pin(&sc->sc_gpio[0], 0);
101*56d4042bStobhe 		break;
102*56d4042bStobhe 	case DVACT_WAKEUP:
103*56d4042bStobhe 		gpio_controller_set_pin(&sc->sc_gpio[0], sc->sc_on);
104*56d4042bStobhe 		break;
105*56d4042bStobhe 	}
106*56d4042bStobhe 
107*56d4042bStobhe 	return 0;
108*56d4042bStobhe }
109*56d4042bStobhe 
110b0ed8e09Stobhe void
gpiobl_set(u_int on)111f2edca47Stobhe gpiobl_set(u_int on)
112f2edca47Stobhe {
113f2edca47Stobhe 	struct gpiobl_softc *sc = (struct gpiobl_softc *)sc_gpiobl;
114f2edca47Stobhe 	sc->sc_on = on;
115f2edca47Stobhe 	task_add(systq, &sc->sc_task);
116f2edca47Stobhe }
117f2edca47Stobhe 
118f2edca47Stobhe void
gpiobl_task(void * args)119b0ed8e09Stobhe gpiobl_task(void *args)
120b0ed8e09Stobhe {
121b0ed8e09Stobhe 	struct gpiobl_softc *sc = args;
122b0ed8e09Stobhe 	gpio_controller_set_pin(&sc->sc_gpio[0], sc->sc_on);
123b0ed8e09Stobhe }
124