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