1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
/* $OpenBSD: abl.c,v 1.4 2022/01/09 05:42:37 jsg Exp $ */
/*
* Copyright (c) 2020 Marcus Glocker <mglocker@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Driver for controlling the screen backlight brightness on Apple machines.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <dev/acpi/acpivar.h>
#include <dev/acpi/acpidev.h>
#include <dev/acpi/amltypes.h>
#include <dev/acpi/dsdt.h>
#include <dev/pci/pcidevs.h>
#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wsdisplayvar.h>
#ifdef ABL_DEBUG
#define DPRINTF(x) printf x
#else
#define DPRINTF(x)
#endif
#define ABL_IO_BASE_INTEL 0xb2
#define ABL_IO_BASE_NVIDIA 0x52e
#define ABL_IO_SIZE 2
#define ABL_IO_LO 0x00
#define ABL_IO_HI 0x01
#define ABL_BRIGHTNESS_MIN 0
#define ABL_BRIGHTNESS_MAX 15
struct abl_softc {
struct device sc_dev;
struct acpi_softc *sc_acpi;
struct aml_node *sc_devnode;
bus_space_tag_t sc_bt;
bus_space_handle_t sc_bh;
bus_addr_t sc_io_base;
uint8_t sc_brightness;
};
int abl_match(struct device *, void *, void *);
void abl_attach(struct device *, struct device *, void *);
int abl_get_brightness(struct abl_softc *);
int abl_set_brightness(struct abl_softc *, uint8_t);
/* Hooks for wsconsole brightness control. */
int abl_get_param(struct wsdisplay_param *);
int abl_set_param(struct wsdisplay_param *);
struct cfattach abl_ca = {
sizeof(struct abl_softc), abl_match, abl_attach, NULL, NULL
};
struct cfdriver abl_cd = {
NULL, "abl", DV_DULL
};
const char *abl_hids[] = {
"APP0002", NULL
};
int
abl_match(struct device *parent, void *match, void *aux)
{
struct acpi_attach_args *aa = aux;
struct cfdata *cf = match;
return acpi_matchhids(aa, abl_hids, cf->cf_driver->cd_name);
}
void
abl_attach(struct device *parent, struct device *self, void *aux)
{
struct abl_softc *sc = (struct abl_softc *)self;
struct acpi_attach_args *aaa = aux;
struct aml_value res;
int64_t sta;
int reg;
pci_chipset_tag_t pc;
pcitag_t tag;
sc->sc_acpi = (struct acpi_softc *)parent;
sc->sc_devnode = aaa->aaa_node;
printf(": %s", sc->sc_devnode->name);
sta = acpi_getsta(sc->sc_acpi, sc->sc_devnode);
if ((sta & (STA_PRESENT | STA_ENABLED | STA_DEV_OK)) !=
(STA_PRESENT | STA_ENABLED | STA_DEV_OK)) {
printf(": not enabled\n");
return;
}
if (!(aml_evalname(sc->sc_acpi, sc->sc_devnode, "_CID", 0, NULL, &res)))
printf(" (%s)", res.v_string);
/* Backlight on non-iMacs is already handled differently. */
if (strncmp(hw_prod, "iMac", 4)) {
printf("\n");
return;
}
/*
* We need to check on what type of PCI controller we're running on to
* access the right I/O space.
*/
pc = pci_lookup_segment(0);
tag = pci_make_tag(pc, 0, 0, 0);
reg = pci_conf_read(pc, tag, PCI_ID_REG);
switch (PCI_VENDOR(reg)) {
case PCI_VENDOR_INTEL:
sc->sc_io_base = ABL_IO_BASE_INTEL;
break;
case PCI_VENDOR_NVIDIA:
sc->sc_io_base = ABL_IO_BASE_NVIDIA;
break;
default:
printf(": pci controller not supported\n");
return;
}
/*
* Map I/O space. This driver uses the ACPI SMI command port.
* This port has already been claimed by the generic ACPI code
* so we need to work around that here by calling _bus_space_map().
*/
sc->sc_bt = aaa->aaa_iot;
if (_bus_space_map(sc->sc_bt, sc->sc_io_base, ABL_IO_SIZE, 0,
&sc->sc_bh)) {
printf(": can't map register\n");
return;
}
/* Read the current brightness value initially. */
sc->sc_brightness = abl_get_brightness(sc);
/* Map wsconsole hook functions. */
ws_get_param = abl_get_param;
ws_set_param = abl_set_param;
printf("\n");
}
int
abl_get_brightness(struct abl_softc *sc)
{
uint8_t val;
bus_space_write_1(sc->sc_bt, sc->sc_bh, ABL_IO_HI, 0x03);
bus_space_write_1(sc->sc_bt, sc->sc_bh, ABL_IO_LO, 0xbf);
val = bus_space_read_1(sc->sc_bt, sc->sc_bh, ABL_IO_HI);
return (val >> 4);
}
int
abl_set_brightness(struct abl_softc *sc, uint8_t val)
{
bus_space_write_1(sc->sc_bt, sc->sc_bh, ABL_IO_HI, 0x04 | (val << 4));
bus_space_write_1(sc->sc_bt, sc->sc_bh, ABL_IO_LO, 0xbf);
return 0;
}
int
abl_get_param(struct wsdisplay_param *dp)
{
struct abl_softc *sc = abl_cd.cd_devs[0];
if (sc == NULL)
return -1;
switch (dp->param) {
case WSDISPLAYIO_PARAM_BRIGHTNESS:
DPRINTF(("abl_get_param: sc->sc_brightness = %d\n",
sc->sc_brightness));
dp->min = ABL_BRIGHTNESS_MIN;
dp->max = ABL_BRIGHTNESS_MAX;
dp->curval = sc->sc_brightness;
return 0;
default:
return -1;
}
}
int
abl_set_param(struct wsdisplay_param *dp)
{
struct abl_softc *sc = abl_cd.cd_devs[0];
if (sc == NULL)
return -1;
switch (dp->param) {
case WSDISPLAYIO_PARAM_BRIGHTNESS:
DPRINTF(("abl_set_param: curval = %d\n", dp->curval));
if (dp->curval < ABL_BRIGHTNESS_MIN)
dp->curval = 0;
if (dp->curval > ABL_BRIGHTNESS_MAX)
dp->curval = ABL_BRIGHTNESS_MAX;
abl_set_brightness(sc, dp->curval);
sc->sc_brightness = dp->curval;
return 0;
default:
return -1;
}
}
|