xref: /freebsd/sys/powerpc/powermac/atibl.c (revision 641a6cfb86023499caafe26a4d821a0b885cf00b)
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 /* From the xf86-video-ati driver's radeon_reg.h */
43 #define RADEON_LVDS_GEN_CNTL         0x02d0
44 #define  RADEON_LVDS_ON               (1   <<  0)
45 #define  RADEON_LVDS_DISPLAY_DIS      (1   <<  1)
46 #define  RADEON_LVDS_PANEL_TYPE       (1   <<  2)
47 #define  RADEON_LVDS_PANEL_FORMAT     (1   <<  3)
48 #define  RADEON_LVDS_RST_FM           (1   <<  6)
49 #define  RADEON_LVDS_EN               (1   <<  7)
50 #define  RADEON_LVDS_BL_MOD_LEVEL_SHIFT 8
51 #define  RADEON_LVDS_BL_MOD_LEVEL_MASK (0xff << 8)
52 #define  RADEON_LVDS_BL_MOD_EN        (1   << 16)
53 #define  RADEON_LVDS_DIGON            (1   << 18)
54 #define  RADEON_LVDS_BLON             (1   << 19)
55 
56 struct atibl_softc {
57 	device_t	 dev;
58 	struct resource *sc_memr;
59 };
60 
61 static void atibl_identify(driver_t *driver, device_t parent);
62 static int atibl_probe(device_t dev);
63 static int atibl_attach(device_t dev);
64 static int atibl_setlevel(struct atibl_softc *sc, int newlevel);
65 static int atibl_getlevel(struct atibl_softc *sc);
66 static int atibl_sysctl(SYSCTL_HANDLER_ARGS);
67 
68 static device_method_t atibl_methods[] = {
69 	/* Device interface */
70 	DEVMETHOD(device_identify, atibl_identify),
71 	DEVMETHOD(device_probe, atibl_probe),
72 	DEVMETHOD(device_attach, atibl_attach),
73 	{0, 0},
74 };
75 
76 static driver_t	atibl_driver = {
77 	"backlight",
78 	atibl_methods,
79 	sizeof(struct atibl_softc)
80 };
81 
82 static devclass_t atibl_devclass;
83 
84 DRIVER_MODULE(atibl, vgapci, atibl_driver, atibl_devclass, 0, 0);
85 
86 static void
87 atibl_identify(driver_t *driver, device_t parent)
88 {
89 	if (device_find_child(parent, "backlight", -1) == NULL)
90 		device_add_child(parent, "backlight", -1);
91 }
92 
93 static int
94 atibl_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 <= 0)
102 		return (ENXIO);
103 
104 	if (OF_getprop(handle, "backlight-control", &control, sizeof(control)) < 0)
105 		return (ENXIO);
106 
107 	if (strcmp(control, "ati") != 0)
108 		return (ENXIO);
109 
110 	device_set_desc(dev, "PowerBook backlight");
111 
112 	return (0);
113 }
114 
115 static int
116 atibl_attach(device_t dev)
117 {
118 	struct atibl_softc	*sc;
119 	struct sysctl_ctx_list *ctx;
120 	struct sysctl_oid *tree;
121 	int			 rid;
122 
123 	sc = device_get_softc(dev);
124 
125 	rid = 0x18;	/* BAR[2], for the MMIO register */
126 	sc->sc_memr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
127 			RF_ACTIVE | RF_SHAREABLE);
128 	if (sc->sc_memr == NULL) {
129 		device_printf(dev, "Could not alloc mem resource!\n");
130 		return (ENXIO);
131 	}
132 
133 	ctx = device_get_sysctl_ctx(dev);
134 	tree = device_get_sysctl_tree(dev);
135 
136 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
137 			"level", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
138 			atibl_sysctl, "I", "Backlight level (0-100)");
139 
140 	return (0);
141 }
142 
143 static int
144 atibl_setlevel(struct atibl_softc *sc, int newlevel)
145 {
146 	uint32_t lvds_gen_cntl;
147 
148 	if (newlevel > 100)
149 		newlevel = 100;
150 
151 	if (newlevel < 0)
152 		newlevel = 0;
153 
154 	newlevel = (newlevel * 5) / 2 + 5;
155 	lvds_gen_cntl = bus_read_4(sc->sc_memr, RADEON_LVDS_GEN_CNTL);
156 	lvds_gen_cntl |= RADEON_LVDS_BL_MOD_EN;
157 	lvds_gen_cntl &= ~RADEON_LVDS_BL_MOD_LEVEL_MASK;
158 	lvds_gen_cntl |= (newlevel << RADEON_LVDS_BL_MOD_LEVEL_SHIFT) &
159 		RADEON_LVDS_BL_MOD_LEVEL_MASK;
160 	bus_write_4(sc->sc_memr, RADEON_LVDS_GEN_CNTL, lvds_gen_cntl);
161 
162 	return (0);
163 }
164 
165 static int
166 atibl_getlevel(struct atibl_softc *sc)
167 {
168 	uint32_t	lvds_gen_cntl;
169 	int			level;
170 
171 	lvds_gen_cntl = bus_read_4(sc->sc_memr, RADEON_LVDS_GEN_CNTL);
172 
173 	level = ((lvds_gen_cntl & RADEON_LVDS_BL_MOD_LEVEL_MASK) >>
174 			RADEON_LVDS_BL_MOD_LEVEL_SHIFT);
175 	level = ((level - 5) * 2) / 5;
176 
177 	return (level);
178 }
179 
180 static int
181 atibl_sysctl(SYSCTL_HANDLER_ARGS)
182 {
183 	struct atibl_softc *sc;
184 	int newlevel, error;
185 
186 	sc = arg1;
187 
188 	newlevel = atibl_getlevel(sc);
189 
190 	error = sysctl_handle_int(oidp, &newlevel, 0, req);
191 
192 	if (error || !req->newptr)
193 		return (error);
194 
195 	return (atibl_setlevel(sc, newlevel));
196 }
197