Home | History | Annotate | Line # | Download | only in dev
      1 /*	$NetBSD: j720pwr.c,v 1.7 2021/08/07 16:18:53 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002, 2006 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Emmanuel Dreyfus and Peter Postma.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /* Jornada 720 power management. */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: j720pwr.c,v 1.7 2021/08/07 16:18:53 thorpej Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/kernel.h>
     40 #include <sys/proc.h>
     41 #include <sys/device.h>
     42 
     43 #include <dev/apm/apmbios.h>
     44 
     45 #include <machine/config_hook.h>
     46 #include <machine/platid.h>
     47 #include <machine/platid_mask.h>
     48 
     49 #include <arm/sa11x0/sa11x0_var.h>
     50 #include <arm/sa11x0/sa11x0_gpioreg.h>
     51 #include <arm/sa11x0/sa11x0_ppcreg.h>
     52 #include <arm/sa11x0/sa11x0_sspreg.h>
     53 
     54 #include <hpcarm/dev/j720sspvar.h>
     55 
     56 #ifdef DEBUG
     57 #define DPRINTF(arg)	aprint_normal arg
     58 #else
     59 #define DPRINTF(arg)	/* nothing */
     60 #endif
     61 
     62 #define arraysize(ary)	(sizeof(ary) / sizeof(ary[0]))
     63 
     64 struct j720pwr_softc {
     65 	device_t		sc_dev;
     66 
     67 	struct j720ssp_softc	*sc_ssp;
     68 
     69 	volatile int		sc_state;
     70 #define J720PWR_POWEROFF	0x01
     71 #define J720PWR_SLEEPING	0x02
     72 };
     73 
     74 static int	j720pwr_match(device_t, cfdata_t, void *);
     75 static void	j720pwr_attach(device_t, device_t, void *);
     76 
     77 static void	j720pwr_sleep(void *);
     78 static int	j720pwr_suspend_hook(void *, int, long, void *);
     79 static int	j720pwr_event_hook(void *, int, long, void *);
     80 static int	j720pwr_apm_getpower_hook(void *, int, long, void *);
     81 static int	j720pwr_get_battery(struct j720pwr_softc *);
     82 static int	j720pwr_get_ac_status(struct j720pwr_softc *);
     83 static int	j720pwr_get_charge_status(struct j720pwr_softc *);
     84 
     85 static const struct {
     86 	int	percent;
     87 	int	value;
     88 	int	state;
     89 } battery_table[] = {
     90 	{ 100,	670,	APM_BATT_FLAG_HIGH	},
     91 	{  90,	660,	APM_BATT_FLAG_HIGH	},
     92 	{  80,	650,	APM_BATT_FLAG_HIGH	},
     93 	{  70,	640,	APM_BATT_FLAG_HIGH	},
     94 	{  60,	630,	APM_BATT_FLAG_HIGH	},
     95 	{  50,	620,	APM_BATT_FLAG_HIGH	},
     96 	{  40,	605,	APM_BATT_FLAG_LOW	},
     97 	{  30,	580,	APM_BATT_FLAG_LOW	},
     98 	{  20,	545,	APM_BATT_FLAG_LOW	},
     99 	{  10,	500,	APM_BATT_FLAG_CRITICAL	},
    100 	{   0,  430,	APM_BATT_FLAG_CRITICAL	},
    101 };
    102 
    103 CFATTACH_DECL_NEW(j720pwr, sizeof(struct j720pwr_softc),
    104     j720pwr_match, j720pwr_attach, NULL, NULL);
    105 
    106 
    107 static int
    108 j720pwr_match(device_t parent, cfdata_t cf, void *aux)
    109 {
    110 
    111 	if (!platid_match(&platid, &platid_mask_MACH_HP_JORNADA_7XX))
    112 		return 0;
    113 	if (strcmp(cf->cf_name, "j720pwr") != 0)
    114 		return 0;
    115 
    116 	return 1;
    117 }
    118 
    119 static void
    120 j720pwr_attach(device_t parent, device_t self, void *aux)
    121 {
    122 	struct j720pwr_softc *sc = device_private(self);
    123 	extern void (*__sleep_func)(void *);
    124 	extern void *__sleep_ctx;
    125 
    126 	aprint_normal("\n");
    127 
    128 	sc->sc_dev = self;
    129 	sc->sc_ssp = device_private(parent);
    130 	sc->sc_state = 0;
    131 
    132 	/* Register apm sleep function. */
    133 	__sleep_func = j720pwr_sleep;
    134 	__sleep_ctx = sc;
    135 
    136 	/* Battery status query hook. */
    137 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_BATTERYVAL,
    138 		    CONFIG_HOOK_EXCLUSIVE, j720pwr_apm_getpower_hook, sc);
    139 
    140 	/* Battery charge status query hook. */
    141 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_CHARGE,
    142 		    CONFIG_HOOK_EXCLUSIVE, j720pwr_apm_getpower_hook, sc);
    143 
    144 	/* AC status query hook. */
    145 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_ACADAPTER,
    146 		    CONFIG_HOOK_EXCLUSIVE, j720pwr_apm_getpower_hook, sc);
    147 
    148 	/* Suspend/resume button hook. */
    149 	config_hook(CONFIG_HOOK_BUTTONEVENT,
    150 		    CONFIG_HOOK_BUTTONEVENT_POWER,
    151 		    CONFIG_HOOK_SHARE, j720pwr_suspend_hook, sc);
    152 
    153 	/* Receive suspend/resume events. */
    154 	config_hook(CONFIG_HOOK_PMEVENT,
    155 		    CONFIG_HOOK_PMEVENT_HARDPOWER,
    156 		    CONFIG_HOOK_SHARE, j720pwr_event_hook, sc);
    157 
    158 	/* Attach hpcapm. */
    159 	config_found(self, NULL, NULL, CFARGS_NONE);
    160 }
    161 
    162 static void
    163 j720pwr_sleep(void *ctx)
    164 {
    165 	struct j720pwr_softc *sc = ctx;
    166 	struct j720ssp_softc *ssp = sc->sc_ssp;
    167 	uint32_t oldfer;
    168 
    169 	/* Disable falling-edge detect on all GPIO ports, except keyboard. */
    170 	oldfer = bus_space_read_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_FER);
    171 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_FER, 1 << 0);
    172 
    173 	while (sc->sc_state & J720PWR_POWEROFF) {
    174 		/*
    175 		 * Just sleep here until the poweroff bit gets unset.
    176 		 * We need to wait here because when machine_sleep() returns
    177 		 * hpcapm(4) assumes that we are "resuming".
    178 		 */
    179 		(void)tsleep(&sc->sc_state, PWAIT, "j720slp", 0);
    180 	}
    181 
    182 	/* Restore previous FER value. */
    183 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_FER, oldfer);
    184 }
    185 
    186 static int
    187 j720pwr_suspend_hook(void *ctx, int type, long id, void *msg)
    188 {
    189 	struct j720pwr_softc *sc = ctx;
    190 
    191 	if (type != CONFIG_HOOK_BUTTONEVENT ||
    192 	    id != CONFIG_HOOK_BUTTONEVENT_POWER)
    193 		return EINVAL;
    194 
    195 	if ((sc->sc_state & (J720PWR_POWEROFF | J720PWR_SLEEPING)) == 0) {
    196 		sc->sc_state |= J720PWR_POWEROFF;
    197 	} else if ((sc->sc_state & (J720PWR_POWEROFF | J720PWR_SLEEPING)) ==
    198 	    (J720PWR_POWEROFF | J720PWR_SLEEPING)) {
    199 		sc->sc_state &= ~J720PWR_POWEROFF;
    200 		wakeup(&sc->sc_state);
    201 	} else {
    202 		DPRINTF(("j720pwr_suspend_hook: busy\n"));
    203 		return EBUSY;
    204 	}
    205 
    206 	config_hook_call(CONFIG_HOOK_PMEVENT,
    207 		         CONFIG_HOOK_PMEVENT_SUSPENDREQ, NULL);
    208 
    209 	return 0;
    210 }
    211 
    212 static int
    213 j720pwr_event_hook(void *ctx, int type, long id, void *msg)
    214 {
    215 	struct j720pwr_softc *sc = ctx;
    216 	int event = (int)msg;
    217 
    218 	if (type != CONFIG_HOOK_PMEVENT ||
    219 	    id != CONFIG_HOOK_PMEVENT_HARDPOWER)
    220 		return EINVAL;
    221 
    222 	switch (event) {
    223 	case PWR_SUSPEND:
    224 		sc->sc_state |= (J720PWR_SLEEPING | J720PWR_POWEROFF);
    225 		break;
    226 	case PWR_RESUME:
    227 		sc->sc_state &= ~(J720PWR_SLEEPING | J720PWR_POWEROFF);
    228 		break;
    229 	default:
    230 		return EINVAL;
    231 	}
    232 
    233 	return 0;
    234 }
    235 
    236 static int
    237 j720pwr_apm_getpower_hook(void *ctx, int type, long id, void *msg)
    238 {
    239 	int * const pval = msg;
    240 	int val, tmp, i, state = 0;
    241 
    242 	if (type != CONFIG_HOOK_GET)
    243 		return EINVAL;
    244 
    245 	switch (id) {
    246 	case CONFIG_HOOK_BATTERYVAL:
    247 		val = j720pwr_get_battery(ctx);
    248 
    249 		if (val != -1) {
    250 			for (i = 0; i < arraysize(battery_table); i++)
    251 				if (val > battery_table[i].value)
    252 					break;
    253 			if (i == 0) {
    254 				/* Battery charge status is at maximum. */
    255 				*pval = 100;
    256 			} else {
    257 				/*
    258 				 * Use linear interpolation to calculate
    259 				 * the estimated charge status.
    260 				 */
    261 				tmp = ((val - battery_table[i].value) * 100) /
    262 				    (battery_table[i - 1].value -
    263 				     battery_table[i].value);
    264 				*pval = battery_table[i].percent +
    265 				    ((battery_table[i - 1].percent -
    266 				      battery_table[i].percent) * tmp) / 100;
    267 			}
    268 		} else {
    269 			/* Battery is absent. */
    270 			*pval = 0;
    271 		}
    272 
    273 		return 0;
    274 
    275 	case CONFIG_HOOK_CHARGE:
    276 		val = j720pwr_get_battery(ctx);
    277 
    278 		if (val != -1) {
    279 			for (i = 1; i < arraysize(battery_table); i++) {
    280 				if (val > battery_table[i].value) {
    281 					state = battery_table[i - 1].state;
    282 					break;
    283 				}
    284 			}
    285 
    286 			if (j720pwr_get_charge_status(ctx) == 0)
    287 				state |= APM_BATT_FLAG_CHARGING;
    288 		} else {
    289 			state = APM_BATT_FLAG_NO_SYSTEM_BATTERY;
    290 		}
    291 
    292 		*pval = state;
    293 		return 0;
    294 
    295 	case CONFIG_HOOK_ACADAPTER:
    296 		*pval = j720pwr_get_ac_status(ctx) ? APM_AC_OFF : APM_AC_ON;
    297 
    298 		return 0;
    299 	}
    300 
    301 	return EINVAL;
    302 }
    303 
    304 static int
    305 j720pwr_get_battery(struct j720pwr_softc *sc)
    306 {
    307 	struct j720ssp_softc *ssp = sc->sc_ssp;
    308 	int data, i, pmdata[3];
    309 
    310 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PCR, 0x2000000);
    311 
    312 	if (j720ssp_readwrite(ssp, 1, 0xc0, &data, 500) < 0 || data != 0x11) {
    313 		DPRINTF(("j720pwr_get_battery: no dummy received\n"));
    314 		goto out;
    315 	}
    316 
    317 	for (i = 0; i < 3; i++) {
    318 		if (j720ssp_readwrite(ssp, 0, 0x11, &pmdata[i], 100) < 0)
    319 			goto out;
    320 	}
    321 
    322 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000);
    323 
    324 	pmdata[0] |= (pmdata[2] & 0x3) << 8;	/* Main battery. */
    325 	pmdata[1] |= (pmdata[2] & 0xc) << 6;	/* Backup battery (unused). */
    326 
    327 	DPRINTF(("j720pwr_get_battery: data[0]=%d data[1]=%d data[2]=%d\n",
    328 	    pmdata[0], pmdata[1], pmdata[2]));
    329 
    330 	/* If bit 0 and 1 are both set, the main battery is absent. */
    331 	if ((pmdata[2] & 3) == 3)
    332 		return -1;
    333 
    334 	return pmdata[0];
    335 
    336 out:
    337 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000);
    338 
    339 	/* reset SSP */
    340 	bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x307);
    341 	delay(100);
    342 	bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x387);
    343 
    344 	DPRINTF(("j720pwr_get_battery: error %x\n", data));
    345 	return 0;
    346 }
    347 
    348 static int
    349 j720pwr_get_ac_status(struct j720pwr_softc *sc)
    350 {
    351 	struct j720ssp_softc *ssp = sc->sc_ssp;
    352 	uint32_t status;
    353 
    354 	status = bus_space_read_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PLR);
    355 
    356 	return status & (1 << 4);
    357 }
    358 
    359 static int
    360 j720pwr_get_charge_status(struct j720pwr_softc *sc)
    361 {
    362 	struct j720ssp_softc *ssp = sc->sc_ssp;
    363 	uint32_t status;
    364 
    365 	status = bus_space_read_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PLR);
    366 
    367 	return status & (1 << 26);
    368 }
    369