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