Home | History | Annotate | Line # | Download | only in dev
smartbat.c revision 1.9
      1  1.9  macallan /*	$NetBSD: smartbat.c,v 1.9 2012/09/05 21:23:31 macallan Exp $ */
      2  1.1  macallan 
      3  1.1  macallan /*-
      4  1.1  macallan  * Copyright (c) 2007 Michael Lorenz
      5  1.3  macallan  *               2008 Magnus Henoch
      6  1.1  macallan  * All rights reserved.
      7  1.1  macallan  *
      8  1.1  macallan  * Redistribution and use in source and binary forms, with or without
      9  1.1  macallan  * modification, are permitted provided that the following conditions
     10  1.1  macallan  * are met:
     11  1.1  macallan  * 1. Redistributions of source code must retain the above copyright
     12  1.1  macallan  *    notice, this list of conditions and the following disclaimer.
     13  1.1  macallan  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  macallan  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  macallan  *    documentation and/or other materials provided with the distribution.
     16  1.1  macallan  *
     17  1.1  macallan  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  1.1  macallan  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  1.1  macallan  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  1.1  macallan  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  1.1  macallan  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  1.1  macallan  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  1.1  macallan  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  1.1  macallan  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  1.1  macallan  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  1.1  macallan  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  1.1  macallan  * POSSIBILITY OF SUCH DAMAGE.
     28  1.1  macallan  */
     29  1.1  macallan 
     30  1.1  macallan #include <sys/cdefs.h>
     31  1.9  macallan __KERNEL_RCSID(0, "$NetBSD: smartbat.c,v 1.9 2012/09/05 21:23:31 macallan Exp $");
     32  1.1  macallan 
     33  1.1  macallan #include <sys/param.h>
     34  1.1  macallan #include <sys/systm.h>
     35  1.1  macallan #include <sys/kernel.h>
     36  1.1  macallan #include <sys/device.h>
     37  1.1  macallan #include <sys/proc.h>
     38  1.1  macallan 
     39  1.1  macallan #include <dev/sysmon/sysmonvar.h>
     40  1.1  macallan #include <dev/sysmon/sysmon_taskq.h>
     41  1.1  macallan 
     42  1.1  macallan #include <macppc/dev/pmuvar.h>
     43  1.1  macallan #include <macppc/dev/batteryvar.h>
     44  1.6    dyoung #include <sys/bus.h>
     45  1.1  macallan #include "opt_battery.h"
     46  1.1  macallan 
     47  1.1  macallan #ifdef SMARTBAT_DEBUG
     48  1.1  macallan #define DPRINTF printf
     49  1.9  macallan #define static /* static */
     50  1.1  macallan #else
     51  1.1  macallan #define DPRINTF while (0) printf
     52  1.1  macallan #endif
     53  1.1  macallan 
     54  1.3  macallan #define BAT_AC_PRESENT		0
     55  1.9  macallan 
     56  1.9  macallan #define BAT_PRESENT		0
     57  1.9  macallan #define BAT_VOLTAGE		1
     58  1.9  macallan #define BAT_CURRENT		2
     59  1.9  macallan #define BAT_MAX_CHARGE		3
     60  1.9  macallan #define BAT_CHARGE		4
     61  1.9  macallan #define BAT_CHARGING		5
     62  1.9  macallan #define BAT_FULL		6
     63  1.9  macallan #define BAT_NSENSORS		7  /* number of sensors */
     64  1.1  macallan 
     65  1.1  macallan struct smartbat_softc {
     66  1.8  macallan 	device_t sc_dev;
     67  1.1  macallan 	struct pmu_ops *sc_pmu_ops;
     68  1.1  macallan 	int sc_num;
     69  1.1  macallan 
     70  1.1  macallan 	/* envsys stuff */
     71  1.9  macallan 	struct sysmon_envsys *sc_bat_sme;
     72  1.9  macallan 	envsys_data_t sc_bat_sensor[BAT_NSENSORS];
     73  1.9  macallan 	struct sysmon_envsys *sc_ac_sme;
     74  1.9  macallan 	envsys_data_t sc_ac_sensor[1];
     75  1.3  macallan 	struct sysmon_pswitch sc_sm_acpower;
     76  1.9  macallan 	int sc_have_ac;
     77  1.3  macallan 
     78  1.3  macallan 	/* battery status */
     79  1.3  macallan 	int sc_flags;
     80  1.3  macallan 	int sc_oflags;
     81  1.3  macallan 	int sc_voltage;
     82  1.3  macallan 	int sc_charge;
     83  1.3  macallan 	int sc_max_charge;
     84  1.3  macallan 	int sc_draw;
     85  1.3  macallan 	int sc_time;
     86  1.3  macallan 	uint32_t sc_timestamp;
     87  1.1  macallan };
     88  1.1  macallan 
     89  1.5      matt static void smartbat_attach(device_t, device_t, void *);
     90  1.5      matt static int smartbat_match(device_t, cfdata_t, void *);
     91  1.3  macallan static void smartbat_setup_envsys(struct smartbat_softc *);
     92  1.3  macallan static void smartbat_refresh(struct sysmon_envsys *, envsys_data_t *);
     93  1.9  macallan static void smartbat_refresh_ac(struct sysmon_envsys *, envsys_data_t *);
     94  1.3  macallan static void smartbat_poll(void *);
     95  1.3  macallan static int smartbat_update(struct smartbat_softc *, int);
     96  1.1  macallan 
     97  1.8  macallan CFATTACH_DECL_NEW(smartbat, sizeof(struct smartbat_softc),
     98  1.1  macallan     smartbat_match, smartbat_attach, NULL, NULL);
     99  1.1  macallan 
    100  1.1  macallan static int
    101  1.5      matt smartbat_match(device_t parent, cfdata_t cf, void *aux)
    102  1.1  macallan {
    103  1.1  macallan 	struct battery_attach_args *baa = aux;
    104  1.1  macallan 
    105  1.1  macallan 	if (baa->baa_type == BATTERY_TYPE_SMART)
    106  1.1  macallan 		return 1;
    107  1.1  macallan 
    108  1.1  macallan 	return 0;
    109  1.1  macallan }
    110  1.1  macallan 
    111  1.1  macallan static void
    112  1.5      matt smartbat_attach(device_t parent, device_t self, void *aux)
    113  1.1  macallan {
    114  1.1  macallan 	struct battery_attach_args *baa = aux;
    115  1.5      matt 	struct smartbat_softc *sc = device_private(self);
    116  1.1  macallan 
    117  1.8  macallan 	sc->sc_dev = self;
    118  1.1  macallan 	sc->sc_pmu_ops = baa->baa_pmu_ops;
    119  1.1  macallan 	sc->sc_num = baa->baa_num;
    120  1.1  macallan 
    121  1.9  macallan 	/*
    122  1.9  macallan 	 * we can have more than one instance but only the first one needs
    123  1.9  macallan 	 * to report AC status
    124  1.9  macallan 	 */
    125  1.9  macallan 	sc->sc_have_ac = (device_unit(self) == 0);
    126  1.1  macallan 	printf(" addr %d: smart battery\n", sc->sc_num);
    127  1.1  macallan 
    128  1.3  macallan 	smartbat_update(sc, 1);
    129  1.3  macallan 	/* trigger a status update */
    130  1.3  macallan 	sc->sc_oflags = ~sc->sc_flags;
    131  1.3  macallan 
    132  1.3  macallan 	smartbat_setup_envsys(sc);
    133  1.9  macallan 	sc->sc_pmu_ops->register_callback(sc->sc_pmu_ops->cookie,
    134  1.9  macallan 	    smartbat_poll, sc);
    135  1.3  macallan 
    136  1.9  macallan 	if (sc->sc_have_ac) {
    137  1.9  macallan 		memset(&sc->sc_sm_acpower, 0, sizeof(struct sysmon_pswitch));
    138  1.9  macallan 		sc->sc_sm_acpower.smpsw_name = "AC Power";
    139  1.9  macallan 		sc->sc_sm_acpower.smpsw_type = PSWITCH_TYPE_ACADAPTER;
    140  1.9  macallan 		if (sysmon_pswitch_register(&sc->sc_sm_acpower) != 0)
    141  1.9  macallan 			printf("%s: unable to register AC power status with " \
    142  1.9  macallan 			       "sysmon\n",
    143  1.9  macallan 			    device_xname(sc->sc_dev));
    144  1.9  macallan 	}
    145  1.3  macallan }
    146  1.3  macallan 
    147  1.3  macallan 
    148  1.3  macallan static void
    149  1.3  macallan smartbat_setup_envsys(struct smartbat_softc *sc)
    150  1.3  macallan {
    151  1.3  macallan 	int i;
    152  1.3  macallan 
    153  1.9  macallan 	if (sc->sc_have_ac) {
    154  1.9  macallan 
    155  1.9  macallan #define INITDATA(index, unit, string)					\
    156  1.9  macallan 	sc->sc_ac_sensor[index].units = unit;     			\
    157  1.9  macallan 	sc->sc_ac_sensor[index].state = ENVSYS_SINVALID;		\
    158  1.9  macallan 	snprintf(sc->sc_ac_sensor[index].desc,				\
    159  1.9  macallan 	    sizeof(sc->sc_ac_sensor[index].desc), "%s", string);
    160  1.9  macallan 
    161  1.9  macallan 		INITDATA(BAT_AC_PRESENT, ENVSYS_INDICATOR, "connected");
    162  1.9  macallan #undef INITDATA
    163  1.9  macallan 
    164  1.9  macallan 		sc->sc_ac_sme = sysmon_envsys_create();
    165  1.9  macallan 
    166  1.9  macallan 		if (sysmon_envsys_sensor_attach(sc->sc_ac_sme,
    167  1.9  macallan 					&sc->sc_ac_sensor[0])) {
    168  1.9  macallan 			sysmon_envsys_destroy(sc->sc_ac_sme);
    169  1.9  macallan 			return;
    170  1.9  macallan 		}
    171  1.9  macallan 
    172  1.9  macallan 		sc->sc_ac_sme->sme_name = "AC Adaptor";
    173  1.9  macallan 		sc->sc_ac_sme->sme_cookie = sc;
    174  1.9  macallan 		sc->sc_ac_sme->sme_refresh = smartbat_refresh_ac;
    175  1.9  macallan 		sc->sc_ac_sme->sme_class = SME_CLASS_ACADAPTER;
    176  1.9  macallan 
    177  1.9  macallan 		if (sysmon_envsys_register(sc->sc_ac_sme)) {
    178  1.9  macallan 			aprint_error("%s: unable to register AC with sysmon\n",
    179  1.9  macallan 			    device_xname(sc->sc_dev));
    180  1.9  macallan 			sysmon_envsys_destroy(sc->sc_ac_sme);
    181  1.9  macallan 		}
    182  1.9  macallan 	}
    183  1.9  macallan 
    184  1.9  macallan 	sc->sc_bat_sme = sysmon_envsys_create();
    185  1.9  macallan 
    186  1.9  macallan #define INITDATA(index, unit, string)					\
    187  1.9  macallan 	sc->sc_bat_sensor[index].units = unit;     			\
    188  1.9  macallan 	sc->sc_bat_sensor[index].state = ENVSYS_SINVALID;		\
    189  1.9  macallan 	snprintf(sc->sc_bat_sensor[index].desc,				\
    190  1.9  macallan 	    sizeof(sc->sc_bat_sensor[index].desc), "%s", string);
    191  1.3  macallan 
    192  1.3  macallan 	INITDATA(BAT_PRESENT, ENVSYS_INDICATOR, "Battery present");
    193  1.3  macallan 	INITDATA(BAT_VOLTAGE, ENVSYS_SVOLTS_DC, "Battery voltage");
    194  1.3  macallan 	INITDATA(BAT_CURRENT, ENVSYS_SAMPS, "Battery current");
    195  1.3  macallan 	INITDATA(BAT_MAX_CHARGE, ENVSYS_SAMPHOUR, "Battery design cap");
    196  1.3  macallan 	INITDATA(BAT_CHARGE, ENVSYS_SAMPHOUR, "Battery charge");
    197  1.3  macallan 	INITDATA(BAT_CHARGING, ENVSYS_BATTERY_CHARGE, "Battery charging");
    198  1.9  macallan #if 0
    199  1.9  macallan 	INITDATA(BAT_CHARGE_STATE, ENVSYS_BATTERY_CAPACITY,
    200  1.9  macallan 	    "Battery charge state");
    201  1.9  macallan #endif
    202  1.3  macallan 	INITDATA(BAT_FULL, ENVSYS_INDICATOR, "Battery full");
    203  1.3  macallan #undef INITDATA
    204  1.3  macallan 	for (i = 0; i < BAT_NSENSORS; i++) {
    205  1.9  macallan 		sc->sc_bat_sensor[i].flags = ENVSYS_FMONNOTSUPP;
    206  1.9  macallan 		if (sysmon_envsys_sensor_attach(sc->sc_bat_sme,
    207  1.9  macallan 						&sc->sc_bat_sensor[i])) {
    208  1.9  macallan 			sysmon_envsys_destroy(sc->sc_bat_sme);
    209  1.3  macallan 			return;
    210  1.3  macallan 		}
    211  1.3  macallan 	}
    212  1.3  macallan 
    213  1.9  macallan 	sc->sc_bat_sme->sme_name = device_xname(sc->sc_dev);
    214  1.9  macallan 	sc->sc_bat_sme->sme_cookie = sc;
    215  1.9  macallan 	sc->sc_bat_sme->sme_refresh = smartbat_refresh;
    216  1.9  macallan 	sc->sc_bat_sme->sme_class = SME_CLASS_BATTERY;
    217  1.3  macallan 
    218  1.9  macallan 	if (sysmon_envsys_register(sc->sc_bat_sme)) {
    219  1.3  macallan 		aprint_error("%s: unable to register with sysmon\n",
    220  1.8  macallan 		    device_xname(sc->sc_dev));
    221  1.9  macallan 		sysmon_envsys_destroy(sc->sc_bat_sme);
    222  1.3  macallan 	}
    223  1.3  macallan }
    224  1.3  macallan 
    225  1.3  macallan static void
    226  1.3  macallan smartbat_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    227  1.3  macallan {
    228  1.3  macallan 	struct smartbat_softc *sc = sme->sme_cookie;
    229  1.4  macallan 	int which = edata->sensor, present;
    230  1.3  macallan 
    231  1.3  macallan 	smartbat_update(sc, 0);
    232  1.4  macallan 	present = (sc->sc_flags & PMU_PWR_BATT_PRESENT) != 0;
    233  1.3  macallan 
    234  1.4  macallan 	if (present) {
    235  1.4  macallan 		switch (which) {
    236  1.4  macallan 		case BAT_PRESENT:
    237  1.4  macallan 			edata->value_cur = present;
    238  1.4  macallan 			break;
    239  1.4  macallan 		case BAT_VOLTAGE:
    240  1.4  macallan 			edata->value_cur = sc->sc_voltage * 1000;
    241  1.4  macallan 			break;
    242  1.4  macallan 		case BAT_CURRENT:
    243  1.4  macallan 			edata->value_cur = sc->sc_draw * 1000;
    244  1.4  macallan 			break;
    245  1.4  macallan 		case BAT_MAX_CHARGE:
    246  1.4  macallan 			edata->value_cur = sc->sc_max_charge * 1000;
    247  1.4  macallan 			break;
    248  1.4  macallan 		case BAT_CHARGE:
    249  1.4  macallan 			edata->value_cur = sc->sc_charge * 1000;
    250  1.4  macallan 			break;
    251  1.4  macallan 		case BAT_CHARGING:
    252  1.4  macallan 			if ((sc->sc_flags & PMU_PWR_BATT_CHARGING) &&
    253  1.4  macallan 			    (sc->sc_flags & PMU_PWR_AC_PRESENT))
    254  1.4  macallan 				edata->value_cur = 1;
    255  1.4  macallan 			else
    256  1.4  macallan 				edata->value_cur = 0;
    257  1.4  macallan 
    258  1.4  macallan 			break;
    259  1.4  macallan 		case BAT_FULL:
    260  1.4  macallan 			edata->value_cur = (sc->sc_flags & PMU_PWR_BATT_FULL);
    261  1.4  macallan 			break;
    262  1.4  macallan 		}
    263  1.4  macallan 		edata->state = ENVSYS_SVALID;
    264  1.4  macallan 	} else {
    265  1.4  macallan 		/* battery isn't there */
    266  1.4  macallan 		switch (which) {
    267  1.4  macallan 		case BAT_PRESENT:
    268  1.4  macallan 			edata->value_cur = present;
    269  1.4  macallan 			edata->state = ENVSYS_SVALID;
    270  1.4  macallan 			break;
    271  1.4  macallan 		default:
    272  1.4  macallan 			edata->state = ENVSYS_SINVALID;
    273  1.3  macallan 			edata->value_cur = 0;
    274  1.4  macallan 		}
    275  1.3  macallan 	}
    276  1.3  macallan }
    277  1.3  macallan 
    278  1.9  macallan static void
    279  1.9  macallan smartbat_refresh_ac(struct sysmon_envsys *sme, envsys_data_t *edata)
    280  1.9  macallan {
    281  1.9  macallan 	struct smartbat_softc *sc = sme->sme_cookie;
    282  1.9  macallan 	int which = edata->sensor;
    283  1.9  macallan 
    284  1.9  macallan 	smartbat_update(sc, 0);
    285  1.9  macallan 	switch (which) {
    286  1.9  macallan 		case BAT_AC_PRESENT:
    287  1.9  macallan 			edata->value_cur = (sc->sc_flags & PMU_PWR_AC_PRESENT);
    288  1.9  macallan 			edata->state = ENVSYS_SVALID;
    289  1.9  macallan 			break;
    290  1.9  macallan 	}
    291  1.9  macallan }
    292  1.9  macallan 
    293  1.3  macallan /*
    294  1.3  macallan  * Thanks to Paul Mackerras and Fabio Riccardi's Linux implementation
    295  1.3  macallan  * for a clear description of the PMU results.
    296  1.3  macallan  */
    297  1.3  macallan static int
    298  1.3  macallan smartbat_update(struct smartbat_softc *sc, int out)
    299  1.3  macallan {
    300  1.3  macallan 	int len;
    301  1.3  macallan 	uint8_t buf[16];
    302  1.3  macallan 	uint8_t battery_number;
    303  1.3  macallan 
    304  1.3  macallan 	if (sc->sc_timestamp == time_second)
    305  1.3  macallan 		return 0;
    306  1.3  macallan 	sc->sc_timestamp = time_second;
    307  1.3  macallan 
    308  1.3  macallan 	/* sc_num starts from 0, but we need to start from 1 */
    309  1.3  macallan 	battery_number = sc->sc_num + 1;
    310  1.3  macallan 	len = sc->sc_pmu_ops->do_command(sc->sc_pmu_ops->cookie,
    311  1.3  macallan 					 PMU_SMART_BATTERY_STATE,
    312  1.3  macallan 					 1, &battery_number,
    313  1.3  macallan 					 16, buf);
    314  1.3  macallan 
    315  1.3  macallan 	if (len < 0) {
    316  1.9  macallan 		DPRINTF("%s: couldn't get battery data\n",
    317  1.9  macallan 		    device_xname(sc->sc_dev));
    318  1.3  macallan 		/* XXX: the return value is never checked */
    319  1.3  macallan 		return -1;
    320  1.3  macallan 	}
    321  1.3  macallan 
    322  1.3  macallan 	/* Now, buf[0] is the command number, which we already know.
    323  1.3  macallan 	   That's why all indexes are off by one compared to
    324  1.3  macallan 	   pm_battery_info_smart in pm_direct.c.
    325  1.3  macallan 	*/
    326  1.3  macallan 	sc->sc_flags = buf[2];
    327  1.3  macallan 
    328  1.3  macallan         /* XXX: are these all valid for smart batteries? */
    329  1.3  macallan 	if (out) {
    330  1.3  macallan 		printf(" flags: %x", buf[2]);
    331  1.3  macallan 		if (buf[2] & PMU_PWR_AC_PRESENT)
    332  1.3  macallan 			printf(" AC");
    333  1.3  macallan 		if (buf[2] & PMU_PWR_BATT_CHARGING)
    334  1.3  macallan 			printf(" charging");
    335  1.3  macallan 		if (buf[2] & PMU_PWR_BATT_PRESENT)
    336  1.3  macallan 			printf(" present");
    337  1.3  macallan 		if (buf[2] & PMU_PWR_BATT_FULL)
    338  1.3  macallan 			printf(" full");
    339  1.3  macallan 		printf("\n");
    340  1.3  macallan 	}
    341  1.3  macallan 
    342  1.3  macallan 	switch(buf[1]) {
    343  1.3  macallan 	case 3:
    344  1.3  macallan 	case 4:
    345  1.3  macallan 		sc->sc_charge = buf[3];
    346  1.3  macallan 		sc->sc_max_charge = buf[4];
    347  1.3  macallan 		sc->sc_draw = *((signed char *)&buf[5]);
    348  1.3  macallan 		sc->sc_voltage = buf[6];
    349  1.3  macallan 		break;
    350  1.3  macallan 	case 5:
    351  1.3  macallan 		sc->sc_charge = ((buf[3] << 8) | (buf[4]));
    352  1.3  macallan 		sc->sc_max_charge = ((buf[5] << 8) | (buf[6]));
    353  1.3  macallan 		sc->sc_draw = *((signed short *)&buf[7]);
    354  1.3  macallan 		sc->sc_voltage = ((buf[9] << 8) | (buf[8]));
    355  1.3  macallan 		break;
    356  1.3  macallan 	default:
    357  1.3  macallan 		/* XXX - Error condition */
    358  1.9  macallan 		DPRINTF("%s: why is buf[1] %x?\n", device_xname(sc->sc_dev),
    359  1.9  macallan 		   buf[1]);
    360  1.3  macallan 		sc->sc_charge = 0;
    361  1.3  macallan 		sc->sc_max_charge = 0;
    362  1.3  macallan 		sc->sc_draw = 0;
    363  1.3  macallan 		sc->sc_voltage = 0;
    364  1.3  macallan 		break;
    365  1.3  macallan 	}
    366  1.3  macallan 
    367  1.3  macallan 	return 1;
    368  1.3  macallan }
    369  1.3  macallan 
    370  1.3  macallan static void
    371  1.3  macallan smartbat_poll(void *cookie)
    372  1.3  macallan {
    373  1.3  macallan 	struct smartbat_softc *sc = cookie;
    374  1.3  macallan 
    375  1.3  macallan 	smartbat_update(sc, 0);
    376  1.3  macallan 	if ((sc->sc_flags & PMU_PWR_AC_PRESENT) == sc->sc_oflags)
    377  1.3  macallan 		return;
    378  1.3  macallan 
    379  1.3  macallan 	sc->sc_oflags = sc->sc_flags & PMU_PWR_AC_PRESENT;
    380  1.3  macallan 
    381  1.3  macallan 	sysmon_pswitch_event(&sc->sc_sm_acpower,
    382  1.3  macallan 	    sc->sc_oflags ? PSWITCH_EVENT_PRESSED :
    383  1.3  macallan 	    PSWITCH_EVENT_RELEASED);
    384  1.1  macallan }
    385