2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 Justin Hibbits
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/module.h>
36 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
40 #include <machine/bus.h>
42 #include <dev/ofw/openfirm.h>
43 #include <dev/pci/pcivar.h>
45 #ifndef PCI_VENDOR_ID_ATI
46 #define PCI_VENDOR_ID_ATI 0x1002
49 /* From the xf86-video-ati driver's radeon_reg.h */
50 #define RADEON_LVDS_GEN_CNTL 0x02d0
51 #define RADEON_LVDS_ON (1 << 0)
52 #define RADEON_LVDS_DISPLAY_DIS (1 << 1)
53 #define RADEON_LVDS_PANEL_TYPE (1 << 2)
54 #define RADEON_LVDS_PANEL_FORMAT (1 << 3)
55 #define RADEON_LVDS_RST_FM (1 << 6)
56 #define RADEON_LVDS_EN (1 << 7)
57 #define RADEON_LVDS_BL_MOD_LEVEL_SHIFT 8
58 #define RADEON_LVDS_BL_MOD_LEVEL_MASK (0xff << 8)
59 #define RADEON_LVDS_BL_MOD_EN (1 << 16)
60 #define RADEON_LVDS_DIGON (1 << 18)
61 #define RADEON_LVDS_BLON (1 << 19)
62 #define RADEON_LVDS_PLL_CNTL 0x02d4
63 #define RADEON_LVDS_PLL_EN (1 << 16)
64 #define RADEON_LVDS_PLL_RESET (1 << 17)
65 #define RADEON_PIXCLKS_CNTL 0x002d
66 #define RADEON_PIXCLK_LVDS_ALWAYS_ONb (1 << 14)
67 #define RADEON_DISP_PWR_MAN 0x0d08
68 #define RADEON_AUTO_PWRUP_EN (1 << 26)
69 #define RADEON_CLOCK_CNTL_DATA 0x000c
70 #define RADEON_CLOCK_CNTL_INDEX 0x0008
71 #define RADEON_PLL_WR_EN (1 << 7)
72 #define RADEON_CRTC_GEN_CNTL 0x0050
75 struct resource *sc_memr;
79 static void atibl_identify(driver_t *driver, device_t parent);
80 static int atibl_probe(device_t dev);
81 static int atibl_attach(device_t dev);
82 static int atibl_setlevel(struct atibl_softc *sc, int newlevel);
83 static int atibl_getlevel(struct atibl_softc *sc);
84 static int atibl_resume(device_t dev);
85 static int atibl_suspend(device_t dev);
86 static int atibl_sysctl(SYSCTL_HANDLER_ARGS);
88 static device_method_t atibl_methods[] = {
89 /* Device interface */
90 DEVMETHOD(device_identify, atibl_identify),
91 DEVMETHOD(device_probe, atibl_probe),
92 DEVMETHOD(device_attach, atibl_attach),
93 DEVMETHOD(device_suspend, atibl_suspend),
94 DEVMETHOD(device_resume, atibl_resume),
98 static driver_t atibl_driver = {
101 sizeof(struct atibl_softc)
104 DRIVER_MODULE(atibl, vgapci, atibl_driver, 0, 0);
107 atibl_identify(driver_t *driver, device_t parent)
109 if (OF_finddevice("mac-io/backlight") == -1)
111 if (device_find_child(parent, "backlight", -1) == NULL)
112 device_add_child(parent, "backlight", -1);
116 atibl_probe(device_t dev)
121 handle = OF_finddevice("mac-io/backlight");
126 if (OF_getprop(handle, "backlight-control", &control, sizeof(control)) < 0)
129 if (strcmp(control, "ati") != 0 &&
130 (strcmp(control, "mnca") != 0 ||
131 pci_get_vendor(device_get_parent(dev)) != 0x1002))
134 device_set_desc(dev, "PowerBook backlight for ATI graphics");
140 atibl_attach(device_t dev)
142 struct atibl_softc *sc;
143 struct sysctl_ctx_list *ctx;
144 struct sysctl_oid *tree;
147 sc = device_get_softc(dev);
149 rid = 0x18; /* BAR[2], for the MMIO register */
150 sc->sc_memr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
151 RF_ACTIVE | RF_SHAREABLE);
152 if (sc->sc_memr == NULL) {
153 device_printf(dev, "Could not alloc mem resource!\n");
157 ctx = device_get_sysctl_ctx(dev);
158 tree = device_get_sysctl_tree(dev);
160 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
161 "level", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
162 atibl_sysctl, "I", "Backlight level (0-100)");
167 static uint32_t __inline
168 atibl_pll_rreg(struct atibl_softc *sc, uint32_t reg)
170 uint32_t data, save, tmp;
172 bus_write_1(sc->sc_memr, RADEON_CLOCK_CNTL_INDEX, (reg & 0x3f));
173 (void)bus_read_4(sc->sc_memr, RADEON_CLOCK_CNTL_DATA);
174 (void)bus_read_4(sc->sc_memr, RADEON_CRTC_GEN_CNTL);
176 data = bus_read_4(sc->sc_memr, RADEON_CLOCK_CNTL_DATA);
178 /* Only necessary on R300, but won't hurt others. */
179 save = bus_read_4(sc->sc_memr, RADEON_CLOCK_CNTL_INDEX);
180 tmp = save & (~0x3f | RADEON_PLL_WR_EN);
181 bus_write_4(sc->sc_memr, RADEON_CLOCK_CNTL_INDEX, tmp);
182 tmp = bus_read_4(sc->sc_memr, RADEON_CLOCK_CNTL_DATA);
183 bus_write_4(sc->sc_memr, RADEON_CLOCK_CNTL_INDEX, save);
189 atibl_pll_wreg(struct atibl_softc *sc, uint32_t reg, uint32_t val)
193 bus_write_1(sc->sc_memr, RADEON_CLOCK_CNTL_INDEX,
194 ((reg & 0x3f) | RADEON_PLL_WR_EN));
195 (void)bus_read_4(sc->sc_memr, RADEON_CLOCK_CNTL_DATA);
196 (void)bus_read_4(sc->sc_memr, RADEON_CRTC_GEN_CNTL);
198 bus_write_4(sc->sc_memr, RADEON_CLOCK_CNTL_DATA, val);
201 /* Only necessary on R300, but won't hurt others. */
202 save = bus_read_4(sc->sc_memr, RADEON_CLOCK_CNTL_INDEX);
203 tmp = save & (~0x3f | RADEON_PLL_WR_EN);
204 bus_write_4(sc->sc_memr, RADEON_CLOCK_CNTL_INDEX, tmp);
205 tmp = bus_read_4(sc->sc_memr, RADEON_CLOCK_CNTL_DATA);
206 bus_write_4(sc->sc_memr, RADEON_CLOCK_CNTL_INDEX, save);
210 atibl_setlevel(struct atibl_softc *sc, int newlevel)
212 uint32_t lvds_gen_cntl;
213 uint32_t lvds_pll_cntl;
214 uint32_t pixclks_cntl;
215 uint32_t disp_pwr_reg;
223 lvds_gen_cntl = bus_read_4(sc->sc_memr, RADEON_LVDS_GEN_CNTL);
226 newlevel = (newlevel * 5) / 2 + 5;
227 disp_pwr_reg = bus_read_4(sc->sc_memr, RADEON_DISP_PWR_MAN);
228 disp_pwr_reg |= RADEON_AUTO_PWRUP_EN;
229 bus_write_4(sc->sc_memr, RADEON_DISP_PWR_MAN, disp_pwr_reg);
230 lvds_pll_cntl = bus_read_4(sc->sc_memr, RADEON_LVDS_PLL_CNTL);
231 lvds_pll_cntl |= RADEON_LVDS_PLL_EN;
232 bus_write_4(sc->sc_memr, RADEON_LVDS_PLL_CNTL, lvds_pll_cntl);
233 lvds_pll_cntl &= ~RADEON_LVDS_PLL_RESET;
234 bus_write_4(sc->sc_memr, RADEON_LVDS_PLL_CNTL, lvds_pll_cntl);
237 lvds_gen_cntl &= ~(RADEON_LVDS_DISPLAY_DIS |
238 RADEON_LVDS_BL_MOD_LEVEL_MASK);
239 lvds_gen_cntl |= RADEON_LVDS_ON | RADEON_LVDS_EN |
240 RADEON_LVDS_DIGON | RADEON_LVDS_BLON;
241 lvds_gen_cntl |= (newlevel << RADEON_LVDS_BL_MOD_LEVEL_SHIFT) &
242 RADEON_LVDS_BL_MOD_LEVEL_MASK;
243 lvds_gen_cntl |= RADEON_LVDS_BL_MOD_EN;
245 bus_write_4(sc->sc_memr, RADEON_LVDS_GEN_CNTL, lvds_gen_cntl);
247 pixclks_cntl = atibl_pll_rreg(sc, RADEON_PIXCLKS_CNTL);
248 atibl_pll_wreg(sc, RADEON_PIXCLKS_CNTL,
249 pixclks_cntl & ~RADEON_PIXCLK_LVDS_ALWAYS_ONb);
250 lvds_gen_cntl |= RADEON_LVDS_DISPLAY_DIS;
251 lvds_gen_cntl &= ~(RADEON_LVDS_BL_MOD_EN | RADEON_LVDS_BL_MOD_LEVEL_MASK);
252 bus_write_4(sc->sc_memr, RADEON_LVDS_GEN_CNTL, lvds_gen_cntl);
253 lvds_gen_cntl &= ~(RADEON_LVDS_ON | RADEON_LVDS_EN);
255 bus_write_4(sc->sc_memr, RADEON_LVDS_GEN_CNTL, lvds_gen_cntl);
257 atibl_pll_wreg(sc, RADEON_PIXCLKS_CNTL, pixclks_cntl);
265 atibl_getlevel(struct atibl_softc *sc)
267 uint32_t lvds_gen_cntl;
270 lvds_gen_cntl = bus_read_4(sc->sc_memr, RADEON_LVDS_GEN_CNTL);
272 level = ((lvds_gen_cntl & RADEON_LVDS_BL_MOD_LEVEL_MASK) >>
273 RADEON_LVDS_BL_MOD_LEVEL_SHIFT);
275 level = ((level - 5) * 2) / 5;
281 atibl_suspend(device_t dev)
283 struct atibl_softc *sc;
285 sc = device_get_softc(dev);
287 sc->sc_level = atibl_getlevel(sc);
288 atibl_setlevel(sc, 0);
294 atibl_resume(device_t dev)
296 struct atibl_softc *sc;
298 sc = device_get_softc(dev);
300 atibl_setlevel(sc, sc->sc_level);
306 atibl_sysctl(SYSCTL_HANDLER_ARGS)
308 struct atibl_softc *sc;
313 newlevel = atibl_getlevel(sc);
315 error = sysctl_handle_int(oidp, &newlevel, 0, req);
317 if (error || !req->newptr)
320 return (atibl_setlevel(sc, newlevel));