Home | History | Annotate | Line # | Download | only in acpi
asus_acpi.c revision 1.9
      1 /* $NetBSD: asus_acpi.c,v 1.9 2009/08/03 16:33:55 jmcneill 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.9 2009/08/03 16:33:55 jmcneill 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 
     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 	ACPI_INTEGER		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, void *);
     94 
     95 static void	asus_init(device_t);
     96 static bool	asus_suspend(device_t PMF_FN_PROTO);
     97 static bool	asus_resume(device_t PMF_FN_PROTO);
     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 
    163 	if (sysmon_envsys_register(sc->sc_sme)) {
    164 		aprint_error_dev(self, "couldn't register with envsys\n");
    165 		sysmon_envsys_destroy(sc->sc_sme);
    166 		sc->sc_sme = NULL;
    167 	}
    168 nosensors:
    169 
    170 	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle, ACPI_ALL_NOTIFY,
    171 	    asus_notify_handler, sc);
    172 	if (ACPI_FAILURE(rv))
    173 		aprint_error_dev(self, "couldn't install notify handler: %s\n",
    174 		    AcpiFormatException(rv));
    175 
    176 	if (!pmf_device_register(self, asus_suspend, asus_resume))
    177 		aprint_error_dev(self, "couldn't establish power handler\n");
    178 }
    179 
    180 static int
    181 asus_detach(device_t self, int flags)
    182 {
    183 	struct asus_softc *sc = device_private(self);
    184 	int i;
    185 
    186 	if (sc->sc_smpsw_valid)
    187 		for (i = 0; i < ASUS_PSW_LAST; i++)
    188 			sysmon_pswitch_unregister(&sc->sc_smpsw[i]);
    189 
    190 	if (sc->sc_sme)
    191 		sysmon_envsys_unregister(sc->sc_sme);
    192 	if (sc->sc_log)
    193 		sysctl_teardown(&sc->sc_log);
    194 	pmf_device_deregister(self);
    195 
    196 	return 0;
    197 }
    198 
    199 static void
    200 asus_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque)
    201 {
    202 	struct asus_softc *sc = opaque;
    203 
    204 	if (notify >= ASUS_NOTIFY_BrightnessLow &&
    205 	    notify <= ASUS_NOTIFY_BrightnessHigh) {
    206 		aprint_debug_dev(sc->sc_dev, "brightness %d percent\n",
    207 		    (notify & 0xf) * 100 / 0xf);
    208 		return;
    209 	}
    210 
    211 	switch (notify) {
    212 	case ASUS_NOTIFY_WirelessSwitch:	/* handled by AML */
    213 	case ASUS_NOTIFY_WindowSwitch:		/* XXXJDM what is this? */
    214 		break;
    215 	case ASUS_NOTIFY_DisplayCycle:
    216 		if (sc->sc_smpsw_valid == false)
    217 			break;
    218 		sysmon_pswitch_event(&sc->sc_smpsw[ASUS_PSW_DISPLAY_CYCLE],
    219 		    PSWITCH_EVENT_PRESSED);
    220 		break;
    221 	case ASUS_NOTIFY_VolumeMute:
    222 		pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_TOGGLE);
    223 		break;
    224 	case ASUS_NOTIFY_VolumeDown:
    225 		pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_DOWN);
    226 		break;
    227 	case ASUS_NOTIFY_VolumeUp:
    228 		pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_UP);
    229 		break;
    230 	default:
    231 		aprint_debug_dev(sc->sc_dev, "unknown event 0x%02x\n", notify);
    232 		break;
    233 	}
    234 }
    235 
    236 static void
    237 asus_init(device_t self)
    238 {
    239 	struct asus_softc *sc = device_private(self);
    240 	ACPI_STATUS rv;
    241 	ACPI_OBJECT param;
    242 	ACPI_OBJECT_LIST params;
    243 	ACPI_BUFFER ret;
    244 	ACPI_INTEGER cfv;
    245 
    246 	ret.Pointer = NULL;
    247 	ret.Length = ACPI_ALLOCATE_BUFFER;
    248 	param.Type = ACPI_TYPE_INTEGER;
    249 	param.Integer.Value = 0x40;	/* disable ASL display switching */
    250 	params.Pointer = &param;
    251 	params.Count = 1;
    252 
    253 	rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "INIT",
    254 	    &params, &ret);
    255 	if (ACPI_FAILURE(rv))
    256 		aprint_error_dev(self, "couldn't evaluate INIT: %s\n",
    257 		    AcpiFormatException(rv));
    258 
    259 	if (ret.Pointer)
    260 		AcpiOsFree(ret.Pointer);
    261 
    262 	rv = acpi_eval_integer(sc->sc_node->ad_handle, ASUS_METHOD_CFVG, &cfv);
    263 	if (ACPI_FAILURE(rv))
    264 		return;
    265 
    266 	sc->sc_cfvnum = (cfv >> 8) & 0xff;
    267 }
    268 
    269 static bool
    270 asus_suspend(device_t self PMF_FN_ARGS)
    271 {
    272 	struct asus_softc *sc = device_private(self);
    273 	ACPI_STATUS rv;
    274 
    275 	/* capture display brightness when we're sleeping */
    276 	rv = acpi_eval_integer(sc->sc_node->ad_handle, ASUS_METHOD_PBLG,
    277 	    &sc->sc_brightness);
    278 	if (ACPI_FAILURE(rv))
    279 		aprint_error_dev(sc->sc_dev, "couldn't evaluate PBLG: %s\n",
    280 		    AcpiFormatException(rv));
    281 
    282 	return true;
    283 }
    284 
    285 static bool
    286 asus_resume(device_t self PMF_FN_ARGS)
    287 {
    288 	struct asus_softc *sc = device_private(self);
    289 	ACPI_STATUS rv;
    290 	ACPI_OBJECT param;
    291 	ACPI_OBJECT_LIST params;
    292 	ACPI_BUFFER ret;
    293 
    294 	asus_init(self);
    295 
    296 	/* restore previous display brightness */
    297 	ret.Pointer = NULL;
    298 	ret.Length = ACPI_ALLOCATE_BUFFER;
    299 	param.Type = ACPI_TYPE_INTEGER;
    300 	param.Integer.Value = sc->sc_brightness;
    301 	params.Pointer = &param;
    302 	params.Count = 1;
    303 
    304 	rv = AcpiEvaluateObject(sc->sc_node->ad_handle, ASUS_METHOD_PBLS,
    305 	    &params, &ret);
    306 	if (ACPI_FAILURE(rv))
    307 		aprint_error_dev(self, "couldn't evaluate PBLS: %s\n",
    308 		    AcpiFormatException(rv));
    309 
    310 	return true;
    311 }
    312 
    313 static int
    314 asus_sysctl_verify(SYSCTLFN_ARGS)
    315 {
    316 	struct sysctlnode node;
    317 	struct asus_softc *sc;
    318 	ACPI_STATUS rv;
    319 	ACPI_INTEGER cfv;
    320 	ACPI_OBJECT param, retval;
    321 	ACPI_OBJECT_LIST params;
    322 	ACPI_BUFFER ret;
    323 	int err, tmp;
    324 
    325 	node = *rnode;
    326 	sc = rnode->sysctl_data;
    327 	if (node.sysctl_num == sc->sc_cfv_mib) {
    328 		rv = acpi_eval_integer(sc->sc_node->ad_handle,
    329 		    ASUS_METHOD_CFVG, &cfv);
    330 		if (ACPI_FAILURE(rv))
    331 			return ENXIO;
    332 		tmp = cfv & 0xff;
    333 		node.sysctl_data = &tmp;
    334 		err = sysctl_lookup(SYSCTLFN_CALL(&node));
    335 		if (err || newp == NULL)
    336 			return err;
    337 
    338 		if (tmp < 0 || tmp >= sc->sc_cfvnum)
    339 			return EINVAL;
    340 
    341 		ret.Pointer = &retval;
    342 		ret.Length = sizeof(retval);
    343 		param.Type = ACPI_TYPE_INTEGER;
    344 		param.Integer.Value = tmp;
    345 		params.Pointer = &param;
    346 		params.Count = 1;
    347 
    348 		rv = AcpiEvaluateObject(sc->sc_node->ad_handle,
    349 		    ASUS_METHOD_CFVS, &params, &ret);
    350 		if (ACPI_FAILURE(rv))
    351 			return ENXIO;
    352 	}
    353 
    354 	return 0;
    355 }
    356 
    357 static void
    358 asus_sysctl_setup(struct asus_softc *sc)
    359 {
    360 	const struct sysctlnode *node, *node_cfv, *node_ncfv;
    361 	int err, node_mib;
    362 
    363 	if (sc->sc_cfvnum == 0)
    364 		return;
    365 
    366 	err = sysctl_createv(&sc->sc_log, 0, NULL, NULL, 0,
    367 	    CTLTYPE_NODE, "hw", NULL, NULL, 0, NULL, 0, CTL_HW, CTL_EOL);
    368 	if (err)
    369 		goto sysctl_err;
    370 	err = sysctl_createv(&sc->sc_log, 0, NULL, &node, 0,
    371 	    CTLTYPE_NODE, device_xname(sc->sc_dev), NULL, NULL, 0,
    372 	    NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
    373 	if (err)
    374 		goto sysctl_err;
    375 	node_mib = node->sysctl_num;
    376 	err = sysctl_createv(&sc->sc_log, 0, NULL, &node_ncfv,
    377 	    CTLFLAG_READONLY, CTLTYPE_INT, "ncfv",
    378 	    SYSCTL_DESCR("Number of CPU frequency/voltage modes"),
    379 	    NULL, 0, &sc->sc_cfvnum, 0,
    380 	    CTL_HW, node_mib, CTL_CREATE, CTL_EOL);
    381 	if (err)
    382 		goto sysctl_err;
    383 	sc->sc_cfvnum_mib = node_ncfv->sysctl_num;
    384 	err = sysctl_createv(&sc->sc_log, 0, NULL, &node_cfv,
    385 	    CTLFLAG_READWRITE, CTLTYPE_INT, "cfv",
    386 	    SYSCTL_DESCR("Current CPU frequency/voltage mode"),
    387 	    asus_sysctl_verify, 0, sc, 0,
    388 	    CTL_HW, node_mib, CTL_CREATE, CTL_EOL);
    389 	if (err)
    390 		goto sysctl_err;
    391 	sc->sc_cfv_mib = node_cfv->sysctl_num;
    392 
    393 	return;
    394 sysctl_err:
    395 	aprint_error_dev(sc->sc_dev, "failed to add sysctl nodes. (%d)\n", err);
    396 }
    397 
    398 static void
    399 asus_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    400 {
    401 	struct asus_softc *sc = sme->sme_cookie;
    402 	uint32_t rpm;
    403 
    404 	switch (edata->sensor) {
    405 	case ASUS_SENSOR_FAN:
    406 		if (asus_get_fan_speed(sc, &rpm)) {
    407 			edata->value_cur = rpm;
    408 			edata->state = ENVSYS_SVALID;
    409 		} else
    410 			edata->state = ENVSYS_SINVALID;
    411 		break;
    412 	}
    413 }
    414 
    415 static bool
    416 asus_get_fan_speed(struct asus_softc *sc, uint32_t *speed)
    417 {
    418 	ACPI_INTEGER rpmh, rpml;
    419 	ACPI_STATUS rv;
    420 
    421 	rv = acpi_eval_integer(sc->sc_node->ad_handle,
    422 	    ASUS_EC_METHOD_FAN_RPMH, &rpmh);
    423 	if (ACPI_FAILURE(rv))
    424 		return false;
    425 	rv = acpi_eval_integer(sc->sc_node->ad_handle,
    426 	    ASUS_EC_METHOD_FAN_RPML, &rpml);
    427 	if (ACPI_FAILURE(rv))
    428 		return false;
    429 
    430 	if (speed)
    431 		*speed = (rpmh << 8) | rpml;
    432 	return true;
    433 }
    434