xref: /freebsd/sys/powerpc/powermac/nvbl.c (revision ba3c1f5972d7b90feb6e6da47905ff2757e0fe57)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2012 Justin Hibbits
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/systm.h>
35 #include <sys/module.h>
36 #include <sys/kernel.h>
37 #include <sys/rman.h>
38 #include <sys/sysctl.h>
39 
40 #include <machine/bus.h>
41 
42 #include <dev/ofw/openfirm.h>
43 #include <dev/pci/pcivar.h>
44 
45 #define PCI_VENDOR_ID_NVIDIA	0x10de
46 
47 #define NVIDIA_BRIGHT_MIN     (0x0ec)
48 #define NVIDIA_BRIGHT_MAX     (0x538)
49 #define NVIDIA_BRIGHT_SCALE   ((NVIDIA_BRIGHT_MAX - NVIDIA_BRIGHT_MIN)/100)
50 /* nVidia's MMIO registers are at PCI BAR[0] */
51 #define NVIDIA_MMIO_PMC       (0x0)
52 #define  NVIDIA_PMC_OFF         (NVIDIA_MMIO_PMC + 0x10f0)
53 #define   NVIDIA_PMC_BL_SHIFT    (16)
54 #define   NVIDIA_PMC_BL_EN       (1U << 31)
55 
56 struct nvbl_softc {
57 	device_t	 dev;
58 	struct resource *sc_memr;
59 };
60 
61 static void nvbl_identify(driver_t *driver, device_t parent);
62 static int nvbl_probe(device_t dev);
63 static int nvbl_attach(device_t dev);
64 static int nvbl_setlevel(struct nvbl_softc *sc, int newlevel);
65 static int nvbl_getlevel(struct nvbl_softc *sc);
66 static int nvbl_sysctl(SYSCTL_HANDLER_ARGS);
67 
68 static device_method_t nvbl_methods[] = {
69 	/* Device interface */
70 	DEVMETHOD(device_identify, nvbl_identify),
71 	DEVMETHOD(device_probe, nvbl_probe),
72 	DEVMETHOD(device_attach, nvbl_attach),
73 	{0, 0},
74 };
75 
76 static driver_t	nvbl_driver = {
77 	"backlight",
78 	nvbl_methods,
79 	sizeof(struct nvbl_softc)
80 };
81 
82 DRIVER_MODULE(nvbl, vgapci, nvbl_driver, 0, 0);
83 
84 static void
85 nvbl_identify(driver_t *driver, device_t parent)
86 {
87 	if (OF_finddevice("mac-io/backlight") == -1)
88 		return;
89 	if (device_find_child(parent, "backlight", -1) == NULL)
90 		device_add_child(parent, "backlight", -1);
91 }
92 
93 static int
94 nvbl_probe(device_t dev)
95 {
96 	char		control[8];
97 	phandle_t	handle;
98 
99 	handle = OF_finddevice("mac-io/backlight");
100 
101 	if (handle == -1)
102 		return (ENXIO);
103 
104 	if (OF_getprop(handle, "backlight-control", &control, sizeof(control)) < 0)
105 		return (ENXIO);
106 
107 	if ((strcmp(control, "mnca") != 0) ||
108 	    pci_get_vendor(device_get_parent(dev)) != PCI_VENDOR_ID_NVIDIA)
109 		return (ENXIO);
110 
111 	device_set_desc(dev, "PowerBook backlight for nVidia graphics");
112 
113 	return (0);
114 }
115 
116 static int
117 nvbl_attach(device_t dev)
118 {
119 	struct nvbl_softc	*sc;
120 	struct sysctl_ctx_list *ctx;
121 	struct sysctl_oid *tree;
122 	int			 rid;
123 
124 	sc = device_get_softc(dev);
125 
126 	rid = 0x10;	/* BAR[0], for the MMIO register */
127 	sc->sc_memr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
128 			RF_ACTIVE | RF_SHAREABLE);
129 	if (sc->sc_memr == NULL) {
130 		device_printf(dev, "Could not alloc mem resource!\n");
131 		return (ENXIO);
132 	}
133 
134 	/* Turn on big-endian mode */
135 	if (!(bus_read_stream_4(sc->sc_memr, NVIDIA_MMIO_PMC + 4) & 0x01000001)) {
136 		bus_write_stream_4(sc->sc_memr, NVIDIA_MMIO_PMC + 4, 0x01000001);
137 		mb();
138 	}
139 
140 	ctx = device_get_sysctl_ctx(dev);
141 	tree = device_get_sysctl_tree(dev);
142 
143 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
144 	    "level", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
145 	    nvbl_sysctl, "I", "Backlight level (0-100)");
146 
147 	return (0);
148 }
149 
150 static int
151 nvbl_setlevel(struct nvbl_softc *sc, int newlevel)
152 {
153 	uint32_t pmc_reg;
154 
155 	if (newlevel > 100)
156 		newlevel = 100;
157 
158 	if (newlevel < 0)
159 		newlevel = 0;
160 
161 	if (newlevel > 0)
162 		newlevel = (newlevel * NVIDIA_BRIGHT_SCALE) + NVIDIA_BRIGHT_MIN;
163 
164 	pmc_reg = bus_read_stream_4(sc->sc_memr, NVIDIA_PMC_OFF) & 0xffff;
165 	pmc_reg |= NVIDIA_PMC_BL_EN | (newlevel << NVIDIA_PMC_BL_SHIFT);
166 	bus_write_stream_4(sc->sc_memr, NVIDIA_PMC_OFF, pmc_reg);
167 
168 	return (0);
169 }
170 
171 static int
172 nvbl_getlevel(struct nvbl_softc *sc)
173 {
174 	uint16_t level;
175 
176 	level = bus_read_stream_2(sc->sc_memr, NVIDIA_PMC_OFF) & 0x7fff;
177 
178 	if (level  < NVIDIA_BRIGHT_MIN)
179 		return 0;
180 
181 	level = (level - NVIDIA_BRIGHT_MIN) / NVIDIA_BRIGHT_SCALE;
182 
183 	return (level);
184 }
185 
186 static int
187 nvbl_sysctl(SYSCTL_HANDLER_ARGS)
188 {
189 	struct nvbl_softc *sc;
190 	int newlevel, error;
191 
192 	sc = arg1;
193 
194 	newlevel = nvbl_getlevel(sc);
195 
196 	error = sysctl_handle_int(oidp, &newlevel, 0, req);
197 
198 	if (error || !req->newptr)
199 		return (error);
200 
201 	return (nvbl_setlevel(sc, newlevel));
202 }
203