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