asus_acpi.c revision 1.14 1 /* $NetBSD: asus_acpi.c,v 1.14 2010/01/29 11:57:37 jruoho Exp $ */
2
3 /*-
4 * Copyright (c) 2007, 2008, 2009 Jared D. McNeill <jmcneill (at) invisible.ca>
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: asus_acpi.c,v 1.14 2010/01/29 11:57:37 jruoho Exp $");
31
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/malloc.h>
35 #include <sys/buf.h>
36 #include <sys/callout.h>
37 #include <sys/kernel.h>
38 #include <sys/device.h>
39 #include <sys/sysctl.h>
40
41 #include <dev/acpi/acpivar.h>
42 #include <dev/acpi/acpireg.h>
43
44 #define _COMPONENT ACPI_RESOURCE_COMPONENT
45 ACPI_MODULE_NAME ("asus_acpi")
46
47 struct asus_softc {
48 device_t sc_dev;
49 struct acpi_devnode *sc_node;
50
51 #define ASUS_PSW_DISPLAY_CYCLE 0
52 #define ASUS_PSW_LAST 1
53 struct sysmon_pswitch sc_smpsw[ASUS_PSW_LAST];
54 bool sc_smpsw_valid;
55
56 struct sysmon_envsys *sc_sme;
57 #define ASUS_SENSOR_FAN 0
58 #define ASUS_SENSOR_LAST 1
59 envsys_data_t sc_sensor[ASUS_SENSOR_LAST];
60
61 ACPI_INTEGER sc_brightness;
62 ACPI_INTEGER sc_cfvnum;
63
64 struct sysctllog *sc_log;
65 int sc_cfv_mib;
66 int sc_cfvnum_mib;
67 };
68
69 #define ASUS_NOTIFY_WirelessSwitch 0x10
70 #define ASUS_NOTIFY_BrightnessLow 0x20
71 #define ASUS_NOTIFY_BrightnessHigh 0x2f
72 #define ASUS_NOTIFY_DisplayCycle 0x30
73 #define ASUS_NOTIFY_WindowSwitch 0x12 /* XXXJDM ?? */
74 #define ASUS_NOTIFY_VolumeMute 0x13
75 #define ASUS_NOTIFY_VolumeDown 0x14
76 #define ASUS_NOTIFY_VolumeUp 0x15
77
78 #define ASUS_METHOD_SDSP "SDSP"
79 #define ASUS_SDSP_LCD 0x01
80 #define ASUS_SDSP_CRT 0x02
81 #define ASUS_SDSP_TV 0x04
82 #define ASUS_SDSP_DVI 0x08
83 #define ASUS_SDSP_ALL \
84 (ASUS_SDSP_LCD | ASUS_SDSP_CRT | ASUS_SDSP_TV | ASUS_SDSP_DVI)
85 #define ASUS_METHOD_PBLG "PBLG"
86 #define ASUS_METHOD_PBLS "PBLS"
87 #define ASUS_METHOD_CFVS "CFVS"
88 #define ASUS_METHOD_CFVG "CFVG"
89
90 #define ASUS_EC_METHOD_FAN_RPMH "\\_SB.PCI0.SBRG.EC0.SC05"
91 #define ASUS_EC_METHOD_FAN_RPML "\\_SB.PCI0.SBRG.EC0.SC06"
92
93 static int asus_match(device_t, cfdata_t, void *);
94 static void asus_attach(device_t, device_t, void *);
95 static int asus_detach(device_t, int);
96
97 static void asus_notify_handler(ACPI_HANDLE, UINT32, void *);
98
99 static void asus_init(device_t);
100 static bool asus_suspend(device_t, pmf_qual_t);
101 static bool asus_resume(device_t, pmf_qual_t);
102
103 static void asus_sysctl_setup(struct asus_softc *);
104
105 static void asus_sensors_refresh(struct sysmon_envsys *, envsys_data_t *);
106 static bool asus_get_fan_speed(struct asus_softc *, uint32_t *);
107
108 CFATTACH_DECL_NEW(asus, sizeof(struct asus_softc),
109 asus_match, asus_attach, asus_detach, NULL);
110
111 static const char * const asus_ids[] = {
112 "ASUS010",
113 NULL
114 };
115
116 static int
117 asus_match(device_t parent, cfdata_t match, void *opaque)
118 {
119 struct acpi_attach_args *aa = opaque;
120
121 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
122 return 0;
123
124 return acpi_match_hid(aa->aa_node->ad_devinfo, asus_ids);
125 }
126
127 static void
128 asus_attach(device_t parent, device_t self, void *opaque)
129 {
130 struct asus_softc *sc = device_private(self);
131 struct acpi_attach_args *aa = opaque;
132 ACPI_STATUS rv;
133
134 sc->sc_node = aa->aa_node;
135 sc->sc_dev = self;
136
137 aprint_naive("\n");
138 aprint_normal("\n");
139
140 asus_init(self);
141 asus_sysctl_setup(sc);
142
143 sc->sc_smpsw_valid = true;
144 sc->sc_smpsw[ASUS_PSW_DISPLAY_CYCLE].smpsw_name =
145 PSWITCH_HK_DISPLAY_CYCLE;
146 sc->sc_smpsw[ASUS_PSW_DISPLAY_CYCLE].smpsw_type =
147 PSWITCH_TYPE_HOTKEY;
148 if (sysmon_pswitch_register(&sc->sc_smpsw[ASUS_PSW_DISPLAY_CYCLE])) {
149 aprint_error_dev(self, "couldn't register with sysmon\n");
150 sc->sc_smpsw_valid = false;
151 }
152
153 if (asus_get_fan_speed(sc, NULL) == false)
154 goto nosensors;
155
156 sc->sc_sme = sysmon_envsys_create();
157
158 strcpy(sc->sc_sensor[ASUS_SENSOR_FAN].desc, "fan");
159 sc->sc_sensor[ASUS_SENSOR_FAN].units = ENVSYS_SFANRPM;
160 sysmon_envsys_sensor_attach(sc->sc_sme,
161 &sc->sc_sensor[ASUS_SENSOR_FAN]);
162
163 sc->sc_sme->sme_name = device_xname(self);
164 sc->sc_sme->sme_cookie = sc;
165 sc->sc_sme->sme_refresh = asus_sensors_refresh;
166 sc->sc_sme->sme_flags = SME_POLL_ONLY;
167
168 if (sysmon_envsys_register(sc->sc_sme)) {
169 aprint_error_dev(self, "couldn't register with envsys\n");
170 sysmon_envsys_destroy(sc->sc_sme);
171 sc->sc_sme = NULL;
172 }
173 nosensors:
174
175 rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle, ACPI_ALL_NOTIFY,
176 asus_notify_handler, sc);
177 if (ACPI_FAILURE(rv))
178 aprint_error_dev(self, "couldn't install notify handler: %s\n",
179 AcpiFormatException(rv));
180
181 if (!pmf_device_register(self, asus_suspend, asus_resume))
182 aprint_error_dev(self, "couldn't establish power handler\n");
183 }
184
185 static int
186 asus_detach(device_t self, int flags)
187 {
188 struct asus_softc *sc = device_private(self);
189 int i;
190
191 if (sc->sc_smpsw_valid)
192 for (i = 0; i < ASUS_PSW_LAST; i++)
193 sysmon_pswitch_unregister(&sc->sc_smpsw[i]);
194
195 if (sc->sc_sme)
196 sysmon_envsys_unregister(sc->sc_sme);
197 if (sc->sc_log)
198 sysctl_teardown(&sc->sc_log);
199 pmf_device_deregister(self);
200
201 return 0;
202 }
203
204 static void
205 asus_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque)
206 {
207 struct asus_softc *sc = opaque;
208
209 if (notify >= ASUS_NOTIFY_BrightnessLow &&
210 notify <= ASUS_NOTIFY_BrightnessHigh) {
211 aprint_debug_dev(sc->sc_dev, "brightness %d percent\n",
212 (notify & 0xf) * 100 / 0xf);
213 return;
214 }
215
216 switch (notify) {
217 case ASUS_NOTIFY_WirelessSwitch: /* handled by AML */
218 case ASUS_NOTIFY_WindowSwitch: /* XXXJDM what is this? */
219 break;
220 case ASUS_NOTIFY_DisplayCycle:
221 if (sc->sc_smpsw_valid == false)
222 break;
223 sysmon_pswitch_event(&sc->sc_smpsw[ASUS_PSW_DISPLAY_CYCLE],
224 PSWITCH_EVENT_PRESSED);
225 break;
226 case ASUS_NOTIFY_VolumeMute:
227 pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_TOGGLE);
228 break;
229 case ASUS_NOTIFY_VolumeDown:
230 pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_DOWN);
231 break;
232 case ASUS_NOTIFY_VolumeUp:
233 pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_UP);
234 break;
235 default:
236 aprint_debug_dev(sc->sc_dev, "unknown event 0x%02x\n", notify);
237 break;
238 }
239 }
240
241 static void
242 asus_init(device_t self)
243 {
244 struct asus_softc *sc = device_private(self);
245 ACPI_INTEGER cfv;
246 ACPI_STATUS rv;
247
248 /* Disable ASL display switching. */
249 rv = acpi_eval_set_integer(sc->sc_node->ad_handle, "INIT", 0x40);
250
251 if (ACPI_FAILURE(rv))
252 aprint_error_dev(self, "couldn't evaluate INIT: %s\n",
253 AcpiFormatException(rv));
254
255 rv = acpi_eval_integer(sc->sc_node->ad_handle, ASUS_METHOD_CFVG, &cfv);
256
257 if (ACPI_FAILURE(rv))
258 return;
259
260 sc->sc_cfvnum = (cfv >> 8) & 0xff;
261 }
262
263 static bool
264 asus_suspend(device_t self, pmf_qual_t qual)
265 {
266 struct asus_softc *sc = device_private(self);
267 ACPI_STATUS rv;
268
269 /* capture display brightness when we're sleeping */
270 rv = acpi_eval_integer(sc->sc_node->ad_handle, ASUS_METHOD_PBLG,
271 &sc->sc_brightness);
272 if (ACPI_FAILURE(rv))
273 aprint_error_dev(sc->sc_dev, "couldn't evaluate PBLG: %s\n",
274 AcpiFormatException(rv));
275
276 return true;
277 }
278
279 static bool
280 asus_resume(device_t self, pmf_qual_t qual)
281 {
282 struct asus_softc *sc = device_private(self);
283 ACPI_STATUS rv;
284
285 asus_init(self);
286
287 /* Restore previous display brightness. */
288 rv = acpi_eval_set_integer(sc->sc_node->ad_handle, ASUS_METHOD_PBLS,
289 sc->sc_brightness);
290
291 if (ACPI_FAILURE(rv))
292 aprint_error_dev(self, "couldn't evaluate PBLS: %s\n",
293 AcpiFormatException(rv));
294
295 return true;
296 }
297
298 static int
299 asus_sysctl_verify(SYSCTLFN_ARGS)
300 {
301 struct sysctlnode node;
302 struct asus_softc *sc;
303 ACPI_INTEGER cfv;
304 ACPI_STATUS rv;
305 int err, tmp;
306
307 node = *rnode;
308 sc = rnode->sysctl_data;
309 if (node.sysctl_num == sc->sc_cfv_mib) {
310 rv = acpi_eval_integer(sc->sc_node->ad_handle,
311 ASUS_METHOD_CFVG, &cfv);
312 if (ACPI_FAILURE(rv))
313 return ENXIO;
314 tmp = cfv & 0xff;
315 node.sysctl_data = &tmp;
316 err = sysctl_lookup(SYSCTLFN_CALL(&node));
317 if (err || newp == NULL)
318 return err;
319
320 if (tmp < 0 || tmp >= sc->sc_cfvnum)
321 return EINVAL;
322
323 rv = acpi_eval_set_integer(sc->sc_node->ad_handle,
324 ASUS_METHOD_CFVS, tmp);
325
326 if (ACPI_FAILURE(rv))
327 return ENXIO;
328 }
329
330 return 0;
331 }
332
333 static void
334 asus_sysctl_setup(struct asus_softc *sc)
335 {
336 const struct sysctlnode *node, *node_cfv, *node_ncfv;
337 int err, node_mib;
338
339 if (sc->sc_cfvnum == 0)
340 return;
341
342 err = sysctl_createv(&sc->sc_log, 0, NULL, NULL, 0,
343 CTLTYPE_NODE, "hw", NULL, NULL, 0, NULL, 0, CTL_HW, CTL_EOL);
344 if (err)
345 goto sysctl_err;
346 err = sysctl_createv(&sc->sc_log, 0, NULL, &node, 0,
347 CTLTYPE_NODE, device_xname(sc->sc_dev), NULL, NULL, 0,
348 NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
349 if (err)
350 goto sysctl_err;
351 node_mib = node->sysctl_num;
352 err = sysctl_createv(&sc->sc_log, 0, NULL, &node_ncfv,
353 CTLFLAG_READONLY, CTLTYPE_INT, "ncfv",
354 SYSCTL_DESCR("Number of CPU frequency/voltage modes"),
355 NULL, 0, &sc->sc_cfvnum, 0,
356 CTL_HW, node_mib, CTL_CREATE, CTL_EOL);
357 if (err)
358 goto sysctl_err;
359 sc->sc_cfvnum_mib = node_ncfv->sysctl_num;
360 err = sysctl_createv(&sc->sc_log, 0, NULL, &node_cfv,
361 CTLFLAG_READWRITE, CTLTYPE_INT, "cfv",
362 SYSCTL_DESCR("Current CPU frequency/voltage mode"),
363 asus_sysctl_verify, 0, sc, 0,
364 CTL_HW, node_mib, CTL_CREATE, CTL_EOL);
365 if (err)
366 goto sysctl_err;
367 sc->sc_cfv_mib = node_cfv->sysctl_num;
368
369 return;
370 sysctl_err:
371 aprint_error_dev(sc->sc_dev, "failed to add sysctl nodes. (%d)\n", err);
372 }
373
374 static void
375 asus_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
376 {
377 struct asus_softc *sc = sme->sme_cookie;
378 uint32_t rpm;
379
380 switch (edata->sensor) {
381 case ASUS_SENSOR_FAN:
382 if (asus_get_fan_speed(sc, &rpm)) {
383 edata->value_cur = rpm;
384 edata->state = ENVSYS_SVALID;
385 } else
386 edata->state = ENVSYS_SINVALID;
387 break;
388 }
389 }
390
391 static bool
392 asus_get_fan_speed(struct asus_softc *sc, uint32_t *speed)
393 {
394 ACPI_INTEGER rpmh, rpml;
395 ACPI_STATUS rv;
396
397 rv = acpi_eval_integer(sc->sc_node->ad_handle,
398 ASUS_EC_METHOD_FAN_RPMH, &rpmh);
399 if (ACPI_FAILURE(rv))
400 return false;
401 rv = acpi_eval_integer(sc->sc_node->ad_handle,
402 ASUS_EC_METHOD_FAN_RPML, &rpml);
403 if (ACPI_FAILURE(rv))
404 return false;
405
406 if (speed)
407 *speed = (rpmh << 8) | rpml;
408 return true;
409 }
410