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