Home | History | Annotate | Line # | Download | only in dev
scoop.c revision 1.6
      1 /*	$NetBSD: scoop.c,v 1.6 2009/01/29 12:28:15 nonaka Exp $	*/
      2 /*	$OpenBSD: zaurus_scoop.c,v 1.12 2005/11/17 05:26:31 uwe Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 2005 Uwe Stuehler <uwe (at) bsdx.de>
      6  *
      7  * Permission to use, copy, modify, and distribute this software for any
      8  * purpose with or without fee is hereby granted, provided that the above
      9  * copyright notice and this permission notice appear in all copies.
     10  *
     11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  */
     19 
     20 #include <sys/cdefs.h>
     21 __KERNEL_RCSID(0, "$NetBSD: scoop.c,v 1.6 2009/01/29 12:28:15 nonaka Exp $");
     22 
     23 #include <sys/param.h>
     24 #include <sys/systm.h>
     25 #include <sys/device.h>
     26 #include <sys/conf.h>
     27 #include <sys/gpio.h>
     28 
     29 #include <machine/bus.h>
     30 
     31 #include <arm/xscale/pxa2x0var.h>
     32 
     33 #include <zaurus/zaurus/zaurus_reg.h>
     34 #include <zaurus/zaurus/zaurus_var.h>
     35 
     36 #include <zaurus/dev/scoopreg.h>
     37 #include <zaurus/dev/scoopvar.h>
     38 
     39 #include "ioconf.h"
     40 
     41 struct scoop_softc {
     42 	device_t		sc_dev;
     43 
     44 	bus_space_tag_t		sc_iot;
     45 	bus_space_handle_t	sc_ioh;
     46 
     47 	uint16_t		sc_gpwr;	/* GPIO state before suspend */
     48 };
     49 
     50 static int	scoopmatch(device_t, cfdata_t, void *);
     51 static void	scoopattach(device_t, device_t, void *);
     52 
     53 CFATTACH_DECL_NEW(scoop, sizeof(struct scoop_softc),
     54     scoopmatch, scoopattach, NULL, NULL);
     55 
     56 #if 0
     57 static int	scoop_gpio_pin_read(struct scoop_softc *, int);
     58 #endif
     59 static void	scoop_gpio_pin_write(struct scoop_softc *, int, int);
     60 static void	scoop_gpio_pin_ctl(struct scoop_softc *, int, int);
     61 
     62 enum scoop_card {
     63 	SD_CARD,
     64 	CF_CARD		/* socket 0 (external) */
     65 };
     66 
     67 static void	scoop0_set_card_power(enum scoop_card card, int new_cpr);
     68 
     69 static int
     70 scoopmatch(device_t parent, cfdata_t cf, void *aux)
     71 {
     72 
     73 	/*
     74 	 * Only C3000-like models are known to have two SCOOPs.
     75 	 */
     76         if (ZAURUS_ISC3000)
     77 		return (cf->cf_unit < 2);
     78 	return (cf->cf_unit == 0);
     79 }
     80 
     81 static void
     82 scoopattach(device_t parent, device_t self, void *aux)
     83 {
     84 	struct scoop_softc *sc = device_private(self);
     85 	struct pxaip_attach_args *pxa = (struct pxaip_attach_args *)aux;
     86 	bus_addr_t addr;
     87 	bus_size_t size;
     88 
     89 	sc->sc_dev = self;
     90 	sc->sc_iot = pxa->pxa_iot;
     91 
     92 	aprint_normal(": PCMCIA/GPIO controller\n");
     93 	aprint_naive("\n");
     94 
     95 	if (pxa->pxa_addr != -1)
     96 		addr = pxa->pxa_addr;
     97 	else if (sc->sc_dev->dv_unit == 0)
     98 		addr = C3000_SCOOP0_BASE;
     99 	else
    100 		addr = C3000_SCOOP1_BASE;
    101 
    102 	size = pxa->pxa_size < SCOOP_SIZE ? SCOOP_SIZE : pxa->pxa_size;
    103 
    104 	if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh) != 0) {
    105 		aprint_error_dev(sc->sc_dev, "couldn't map registers\n");
    106 		return;
    107 	}
    108 
    109 	if (ZAURUS_ISC3000 && sc->sc_dev->dv_unit == 1) {
    110 		scoop_gpio_pin_ctl(sc, SCOOP1_AKIN_PULLUP, GPIO_PIN_OUTPUT);
    111 		scoop_gpio_pin_write(sc, SCOOP1_AKIN_PULLUP, GPIO_PIN_LOW);
    112 	} else if (!ZAURUS_ISC3000) {
    113 		scoop_gpio_pin_ctl(sc, SCOOP0_AKIN_PULLUP, GPIO_PIN_OUTPUT);
    114 		scoop_gpio_pin_write(sc, SCOOP0_AKIN_PULLUP, GPIO_PIN_LOW);
    115 	}
    116 
    117 }
    118 
    119 #if 0
    120 static int
    121 scoop_gpio_pin_read(struct scoop_softc *sc, int pin)
    122 {
    123 	uint16_t bit = (1 << pin);
    124 	uint16_t rv;
    125 
    126 	rv = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPWR);
    127 	return (rv & bit) ? 1 : 0;
    128 }
    129 #endif
    130 
    131 static void
    132 scoop_gpio_pin_write(struct scoop_softc *sc, int pin, int level)
    133 {
    134 	uint16_t bit = (1 << pin);
    135 	uint16_t rv;
    136 
    137 	rv = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPWR);
    138 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPWR,
    139 	    (level == GPIO_PIN_LOW) ? (rv & ~bit) : (rv | bit));
    140 }
    141 
    142 static void
    143 scoop_gpio_pin_ctl(struct scoop_softc *sc, int pin, int flags)
    144 {
    145 	uint16_t bit = (1 << pin);
    146 	uint16_t rv;
    147 
    148 	rv = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPCR);
    149 	switch (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
    150 	case GPIO_PIN_INPUT:
    151 		rv &= ~bit;
    152 		break;
    153 	case GPIO_PIN_OUTPUT:
    154 		rv |= bit;
    155 		break;
    156 	}
    157 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPCR, rv);
    158 }
    159 
    160 /*
    161  * Turn the LCD background light and contrast signal on or off.
    162  */
    163 void
    164 scoop_set_backlight(int on, int cont)
    165 {
    166 	struct scoop_softc *sc;
    167 #if 0
    168 	struct scoop_softc *sc0;
    169 
    170 	sc0 = device_lookup_private(&scoop_cd, 0);
    171 #endif
    172 
    173 	sc = device_lookup_private(&scoop_cd, 1);
    174 	if (sc != NULL) {
    175 		/* C3000 */
    176 		scoop_gpio_pin_write(sc,
    177 		    SCOOP1_BACKLIGHT_CONT, !cont);
    178 		scoop_gpio_pin_write(sc,
    179 		    SCOOP1_BACKLIGHT_ON, on);
    180 	}
    181 #if 0
    182 	else if (sc0 != NULL) {
    183 		scoop_gpio_pin_write(sc0,
    184 		    SCOOP0_BACKLIGHT_CONT, cont);
    185 	}
    186 #endif
    187 }
    188 
    189 /*
    190  * Turn the infrared LED on or off (must be on while transmitting).
    191  */
    192 void
    193 scoop_set_irled(int on)
    194 {
    195 	struct scoop_softc *sc;
    196 
    197 	sc = device_lookup_private(&scoop_cd, 1);
    198 	if (sc != NULL) {
    199 		/* IR_ON is inverted */
    200 		scoop_gpio_pin_write(sc,
    201 		    SCOOP1_IR_ON, !on);
    202 	}
    203 }
    204 
    205 /*
    206  * Turn the green and orange LEDs on or off.  If the orange LED is on,
    207  * then it is wired to indicate if A/C is connected.  The green LED has
    208  * no such predefined function.
    209  */
    210 void
    211 scoop_led_set(int led, int on)
    212 {
    213 	struct scoop_softc *sc;
    214 
    215 	sc = device_lookup_private(&scoop_cd, 0);
    216 	if (sc != NULL) {
    217 		if ((led & SCOOP_LED_GREEN) != 0) {
    218 			scoop_gpio_pin_write(sc,
    219 			    SCOOP0_LED_GREEN, on);
    220 		}
    221 		if (scoop_cd.cd_ndevs > 1 && (led & SCOOP_LED_ORANGE) != 0) {
    222 			scoop_gpio_pin_write(sc,
    223 			    SCOOP0_LED_ORANGE_C3000, on);
    224 		}
    225 	}
    226 }
    227 
    228 /*
    229  * Enable or disable the headphone output connection.
    230  */
    231 void
    232 scoop_set_headphone(int on)
    233 {
    234 	struct scoop_softc *sc;
    235 
    236 	sc = device_lookup_private(&scoop_cd, 0);
    237 	if (sc == NULL)
    238 		return;
    239 
    240 	scoop_gpio_pin_ctl(sc, SCOOP0_MUTE_L,
    241 	    GPIO_PIN_OUTPUT);
    242 	scoop_gpio_pin_ctl(sc, SCOOP0_MUTE_R,
    243 	    GPIO_PIN_OUTPUT);
    244 
    245 	if (on) {
    246 		scoop_gpio_pin_write(sc, SCOOP0_MUTE_L,
    247 		    GPIO_PIN_HIGH);
    248 		scoop_gpio_pin_write(sc, SCOOP0_MUTE_R,
    249 		    GPIO_PIN_HIGH);
    250 	} else {
    251 		scoop_gpio_pin_write(sc, SCOOP0_MUTE_L,
    252 		    GPIO_PIN_LOW);
    253 		scoop_gpio_pin_write(sc, SCOOP0_MUTE_R,
    254 		    GPIO_PIN_LOW);
    255 	}
    256 }
    257 
    258 /*
    259  * Turn on pullup resistor while not reading the remote control.
    260  */
    261 void
    262 scoop_akin_pullup(int enable)
    263 {
    264 	struct scoop_softc *sc0;
    265 	struct scoop_softc *sc1;
    266 
    267 	sc0 = device_lookup_private(&scoop_cd, 0);
    268 	sc1 = device_lookup_private(&scoop_cd, 1);
    269 
    270 	if (sc1 != NULL) {
    271 		scoop_gpio_pin_write(sc1,
    272 		    SCOOP1_AKIN_PULLUP, enable);
    273 	} else if (sc0 != NULL) {
    274 		scoop_gpio_pin_write(sc0,
    275 		    SCOOP0_AKIN_PULLUP, enable);
    276 	}
    277 }
    278 
    279 void
    280 scoop_battery_temp_adc(int enable)
    281 {
    282 	struct scoop_softc *sc;
    283 
    284 	sc = device_lookup_private(&scoop_cd, 0);
    285 
    286 	if (sc != NULL) {
    287 		scoop_gpio_pin_write(sc,
    288 		    SCOOP0_ADC_TEMP_ON_C3000, enable);
    289 	}
    290 }
    291 
    292 void
    293 scoop_charge_battery(int enable, int voltage_high)
    294 {
    295 	struct scoop_softc *sc;
    296 
    297 	sc = device_lookup_private(&scoop_cd, 0);
    298 
    299 	if (sc != NULL) {
    300 		scoop_gpio_pin_write(sc,
    301 		    SCOOP0_JK_B_C3000, voltage_high);
    302 		scoop_gpio_pin_write(sc,
    303 		    SCOOP0_CHARGE_OFF_C3000, !enable);
    304 	}
    305 }
    306 
    307 void
    308 scoop_discharge_battery(int enable)
    309 {
    310 	struct scoop_softc *sc;
    311 
    312 	sc = device_lookup_private(&scoop_cd, 0);
    313 
    314 	if (sc != NULL) {
    315 		scoop_gpio_pin_write(sc,
    316 		    SCOOP0_JK_A_C3000, enable);
    317 	}
    318 }
    319 
    320 /*
    321  * Enable or disable 3.3V power to the SD/MMC card slot.
    322  */
    323 void
    324 scoop_set_sdmmc_power(int on)
    325 {
    326 
    327 	scoop0_set_card_power(SD_CARD, on ? SCP_CPR_SD_3V : SCP_CPR_OFF);
    328 }
    329 
    330 /*
    331  * The Card Power Register of the first SCOOP unit controls the power
    332  * for the first CompactFlash slot and the SD/MMC card slot as well.
    333  */
    334 void
    335 scoop0_set_card_power(enum scoop_card card, int new_cpr)
    336 {
    337 	struct scoop_softc *sc;
    338 	bus_space_tag_t iot;
    339 	bus_space_handle_t ioh;
    340 	uint16_t cpr;
    341 
    342 	sc = device_lookup_private(&scoop_cd, 0);
    343 	if (sc == NULL)
    344 		return;
    345 
    346 	iot = sc->sc_iot;
    347 	ioh = sc->sc_ioh;
    348 
    349 	cpr = bus_space_read_2(iot, ioh, SCOOP_CPR);
    350 	if (new_cpr & SCP_CPR_VOLTAGE_MSK) {
    351 		if (card == CF_CARD)
    352 			cpr |= SCP_CPR_5V;
    353 		else if (card == SD_CARD)
    354 			cpr |= SCP_CPR_SD_3V;
    355 
    356 		scoop_gpio_pin_write(sc, SCOOP0_CF_POWER_C3000, 1);
    357 		if (!ISSET(cpr, SCP_CPR_5V) && !ISSET(cpr, SCP_CPR_SD_3V))
    358 			delay(5000);
    359 		bus_space_write_2(iot, ioh, SCOOP_CPR, cpr | new_cpr);
    360 	} else {
    361 		if (card == CF_CARD)
    362 			cpr &= ~SCP_CPR_5V;
    363 		else if (card == SD_CARD)
    364 			cpr &= ~SCP_CPR_SD_3V;
    365 
    366 		if (!ISSET(cpr, SCP_CPR_5V) && !ISSET(cpr, SCP_CPR_SD_3V)) {
    367 			bus_space_write_2(iot, ioh, SCOOP_CPR, SCP_CPR_OFF);
    368 			delay(1000);
    369 			scoop_gpio_pin_write(sc, SCOOP0_CF_POWER_C3000, 0);
    370 		} else
    371 			bus_space_write_2(iot, ioh, SCOOP_CPR, cpr | new_cpr);
    372 	}
    373 }
    374 
    375 void
    376 scoop_check_mcr(void)
    377 {
    378 	struct scoop_softc *sc0, *sc1, *sc;
    379 	uint16_t v;
    380 
    381 	sc0 = device_lookup_private(&scoop_cd, 0);
    382 	sc1 = device_lookup_private(&scoop_cd, 1);
    383 
    384 	/* C3000 */
    385 	if (sc1 != NULL) {
    386 		sc = sc0;
    387 		v = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SCOOP_MCR);
    388 		if ((v & 0x100) == 0) {
    389 			bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCOOP_MCR,
    390 			    0x0101);
    391 		}
    392 
    393 		sc = sc1;
    394 		v = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SCOOP_MCR);
    395 		if ((v & 0x100) == 0) {
    396 			bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCOOP_MCR,
    397 			    0x0101);
    398 		}
    399 	}
    400 }
    401 
    402 void
    403 scoop_suspend(void)
    404 {
    405 	struct scoop_softc *sc, *sc0, *sc1;
    406 	uint32_t rv;
    407 
    408 	sc0 = device_lookup_private(&scoop_cd, 0);
    409 	sc1 = device_lookup_private(&scoop_cd, 1);
    410 
    411 	if (sc0 != NULL) {
    412 		sc = sc0;
    413 		sc->sc_gpwr = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
    414 		    SCOOP_GPWR);
    415 		/* C3000 */
    416 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPWR,
    417 		    sc->sc_gpwr & ~((1<<SCOOP0_MUTE_L) | (1<<SCOOP0_MUTE_R) |
    418 		    (1<<SCOOP0_JK_A_C3000) | (1<<SCOOP0_ADC_TEMP_ON_C3000) |
    419 		    (1<<SCOOP0_LED_GREEN)));
    420 	}
    421 
    422 	/* C3000 */
    423 	if (sc1 != NULL) {
    424 		sc = sc1;
    425 		sc->sc_gpwr = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
    426 		    SCOOP_GPWR);
    427 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPWR,
    428 		    sc->sc_gpwr & ~((1<<SCOOP1_RESERVED_4) |
    429 		    (1<<SCOOP1_RESERVED_5) | (1<<SCOOP1_RESERVED_6) |
    430 		    (1<<SCOOP1_BACKLIGHT_CONT) | (1<<SCOOP1_BACKLIGHT_ON) |
    431 		    (1<<SCOOP1_MIC_BIAS)));
    432 		rv = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPWR);
    433 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPWR,
    434 		    rv | ((1<<SCOOP1_IR_ON) | (1<<SCOOP1_RESERVED_3)));
    435 	}
    436 }
    437 
    438 void
    439 scoop_resume(void)
    440 {
    441 	struct scoop_softc *sc, *sc0, *sc1;
    442 
    443 	sc0 = device_lookup_private(&scoop_cd, 0);
    444 	sc1 = device_lookup_private(&scoop_cd, 1);
    445 
    446 	if (sc0 != NULL) {
    447 		sc = sc0;
    448 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPWR,
    449 		    sc->sc_gpwr);
    450 	}
    451 
    452 	if (sc1 != NULL) {
    453 		sc = sc1;
    454 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPWR,
    455 		    sc->sc_gpwr);
    456 	}
    457 }
    458