Home | History | Annotate | Line # | Download | only in acpi
asus_acpi.c revision 1.17
      1 /* $NetBSD: asus_acpi.c,v 1.17 2010/02/24 22:37:56 dyoung 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.17 2010/02/24 22:37:56 dyoung 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 	int32_t			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, const pmf_qual_t *);
    101 static bool	asus_resume(device_t, const 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 	ACPI_STATUS rv;
    190 	int i;
    191 
    192 	rv = AcpiRemoveNotifyHandler(sc->sc_node->ad_handle,
    193 	    ACPI_ALL_NOTIFY, asus_notify_handler);
    194 
    195 	if (ACPI_FAILURE(rv))
    196 		return EBUSY;
    197 
    198 	if (sc->sc_smpsw_valid)
    199 		for (i = 0; i < ASUS_PSW_LAST; i++)
    200 			sysmon_pswitch_unregister(&sc->sc_smpsw[i]);
    201 
    202 	if (sc->sc_sme)
    203 		sysmon_envsys_unregister(sc->sc_sme);
    204 	if (sc->sc_log)
    205 		sysctl_teardown(&sc->sc_log);
    206 	pmf_device_deregister(self);
    207 
    208 	return 0;
    209 }
    210 
    211 static void
    212 asus_notify_handler(ACPI_HANDLE hdl, UINT32 notify, void *opaque)
    213 {
    214 	struct asus_softc *sc = opaque;
    215 
    216 	if (notify >= ASUS_NOTIFY_BrightnessLow &&
    217 	    notify <= ASUS_NOTIFY_BrightnessHigh) {
    218 		aprint_debug_dev(sc->sc_dev, "brightness %d percent\n",
    219 		    (notify & 0xf) * 100 / 0xf);
    220 		return;
    221 	}
    222 
    223 	switch (notify) {
    224 	case ASUS_NOTIFY_WirelessSwitch:	/* handled by AML */
    225 	case ASUS_NOTIFY_WindowSwitch:		/* XXXJDM what is this? */
    226 		break;
    227 	case ASUS_NOTIFY_DisplayCycle:
    228 		if (sc->sc_smpsw_valid == false)
    229 			break;
    230 		sysmon_pswitch_event(&sc->sc_smpsw[ASUS_PSW_DISPLAY_CYCLE],
    231 		    PSWITCH_EVENT_PRESSED);
    232 		break;
    233 	case ASUS_NOTIFY_VolumeMute:
    234 		pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_TOGGLE);
    235 		break;
    236 	case ASUS_NOTIFY_VolumeDown:
    237 		pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_DOWN);
    238 		break;
    239 	case ASUS_NOTIFY_VolumeUp:
    240 		pmf_event_inject(NULL, PMFE_AUDIO_VOLUME_UP);
    241 		break;
    242 	default:
    243 		aprint_debug_dev(sc->sc_dev, "unknown event 0x%02x\n", notify);
    244 		break;
    245 	}
    246 }
    247 
    248 static void
    249 asus_init(device_t self)
    250 {
    251 	struct asus_softc *sc = device_private(self);
    252 	ACPI_INTEGER cfv;
    253 	ACPI_STATUS rv;
    254 
    255 	/* Disable ASL display switching. */
    256 	rv = acpi_eval_set_integer(sc->sc_node->ad_handle, "INIT", 0x40);
    257 
    258 	if (ACPI_FAILURE(rv))
    259 		aprint_error_dev(self, "couldn't evaluate INIT: %s\n",
    260 		    AcpiFormatException(rv));
    261 
    262 	rv = acpi_eval_integer(sc->sc_node->ad_handle, ASUS_METHOD_CFVG, &cfv);
    263 
    264 	if (ACPI_FAILURE(rv))
    265 		return;
    266 
    267 	sc->sc_cfvnum = (cfv >> 8) & 0xff;
    268 }
    269 
    270 static bool
    271 asus_suspend(device_t self, const pmf_qual_t *qual)
    272 {
    273 	struct asus_softc *sc = device_private(self);
    274 	ACPI_INTEGER val = 0;
    275 	ACPI_STATUS rv;
    276 
    277 	/* Capture display brightness. */
    278 	rv = acpi_eval_integer(sc->sc_node->ad_handle, ASUS_METHOD_PBLG, &val);
    279 
    280 	if (ACPI_FAILURE(rv) || val > INT32_MAX)
    281 		sc->sc_brightness = -1;
    282 	else
    283 		sc->sc_brightness = val;
    284 
    285 	return true;
    286 }
    287 
    288 static bool
    289 asus_resume(device_t self, const pmf_qual_t *qual)
    290 {
    291 	struct asus_softc *sc = device_private(self);
    292 	ACPI_STATUS rv;
    293 
    294 	asus_init(self);
    295 
    296 	if (sc->sc_brightness < 0)
    297 		return true;
    298 
    299 	/* Restore previous display brightness. */
    300 	rv = acpi_eval_set_integer(sc->sc_node->ad_handle, ASUS_METHOD_PBLS,
    301 	    sc->sc_brightness);
    302 
    303 	if (ACPI_FAILURE(rv))
    304 		aprint_error_dev(self, "couldn't evaluate PBLS: %s\n",
    305 		    AcpiFormatException(rv));
    306 
    307 	return true;
    308 }
    309 
    310 static int
    311 asus_sysctl_verify(SYSCTLFN_ARGS)
    312 {
    313 	struct sysctlnode node;
    314 	struct asus_softc *sc;
    315 	ACPI_INTEGER cfv;
    316 	ACPI_STATUS rv;
    317 	int err, tmp;
    318 
    319 	node = *rnode;
    320 	sc = rnode->sysctl_data;
    321 	if (node.sysctl_num == sc->sc_cfv_mib) {
    322 		rv = acpi_eval_integer(sc->sc_node->ad_handle,
    323 		    ASUS_METHOD_CFVG, &cfv);
    324 		if (ACPI_FAILURE(rv))
    325 			return ENXIO;
    326 		tmp = cfv & 0xff;
    327 		node.sysctl_data = &tmp;
    328 		err = sysctl_lookup(SYSCTLFN_CALL(&node));
    329 		if (err || newp == NULL)
    330 			return err;
    331 
    332 		if (tmp < 0 || tmp >= sc->sc_cfvnum)
    333 			return EINVAL;
    334 
    335 		rv = acpi_eval_set_integer(sc->sc_node->ad_handle,
    336 		    ASUS_METHOD_CFVS, tmp);
    337 
    338 		if (ACPI_FAILURE(rv))
    339 			return ENXIO;
    340 	}
    341 
    342 	return 0;
    343 }
    344 
    345 static void
    346 asus_sysctl_setup(struct asus_softc *sc)
    347 {
    348 	const struct sysctlnode *node, *node_cfv, *node_ncfv;
    349 	int err, node_mib;
    350 
    351 	if (sc->sc_cfvnum == 0)
    352 		return;
    353 
    354 	err = sysctl_createv(&sc->sc_log, 0, NULL, NULL, 0,
    355 	    CTLTYPE_NODE, "hw", NULL, NULL, 0, NULL, 0, CTL_HW, CTL_EOL);
    356 	if (err)
    357 		goto sysctl_err;
    358 	err = sysctl_createv(&sc->sc_log, 0, NULL, &node, 0,
    359 	    CTLTYPE_NODE, device_xname(sc->sc_dev), NULL, NULL, 0,
    360 	    NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
    361 	if (err)
    362 		goto sysctl_err;
    363 	node_mib = node->sysctl_num;
    364 	err = sysctl_createv(&sc->sc_log, 0, NULL, &node_ncfv,
    365 	    CTLFLAG_READONLY, CTLTYPE_INT, "ncfv",
    366 	    SYSCTL_DESCR("Number of CPU frequency/voltage modes"),
    367 	    NULL, 0, &sc->sc_cfvnum, 0,
    368 	    CTL_HW, node_mib, CTL_CREATE, CTL_EOL);
    369 	if (err)
    370 		goto sysctl_err;
    371 	sc->sc_cfvnum_mib = node_ncfv->sysctl_num;
    372 	err = sysctl_createv(&sc->sc_log, 0, NULL, &node_cfv,
    373 	    CTLFLAG_READWRITE, CTLTYPE_INT, "cfv",
    374 	    SYSCTL_DESCR("Current CPU frequency/voltage mode"),
    375 	    asus_sysctl_verify, 0, sc, 0,
    376 	    CTL_HW, node_mib, CTL_CREATE, CTL_EOL);
    377 	if (err)
    378 		goto sysctl_err;
    379 	sc->sc_cfv_mib = node_cfv->sysctl_num;
    380 
    381 	return;
    382 sysctl_err:
    383 	aprint_error_dev(sc->sc_dev, "failed to add sysctl nodes. (%d)\n", err);
    384 }
    385 
    386 static void
    387 asus_sensors_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    388 {
    389 	struct asus_softc *sc = sme->sme_cookie;
    390 	uint32_t rpm;
    391 
    392 	switch (edata->sensor) {
    393 	case ASUS_SENSOR_FAN:
    394 		if (asus_get_fan_speed(sc, &rpm)) {
    395 			edata->value_cur = rpm;
    396 			edata->state = ENVSYS_SVALID;
    397 		} else
    398 			edata->state = ENVSYS_SINVALID;
    399 		break;
    400 	}
    401 }
    402 
    403 static bool
    404 asus_get_fan_speed(struct asus_softc *sc, uint32_t *speed)
    405 {
    406 	ACPI_INTEGER rpmh, rpml;
    407 	ACPI_STATUS rv;
    408 
    409 	rv = acpi_eval_integer(sc->sc_node->ad_handle,
    410 	    ASUS_EC_METHOD_FAN_RPMH, &rpmh);
    411 	if (ACPI_FAILURE(rv))
    412 		return false;
    413 	rv = acpi_eval_integer(sc->sc_node->ad_handle,
    414 	    ASUS_EC_METHOD_FAN_RPML, &rpml);
    415 	if (ACPI_FAILURE(rv))
    416 		return false;
    417 
    418 	if (speed)
    419 		*speed = (rpmh << 8) | rpml;
    420 	return true;
    421 }
    422