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