Home | History | Annotate | Line # | Download | only in dev
scoop.c revision 1.2
      1 /*	$NetBSD: scoop.c,v 1.2 2006/12/17 16:07:11 peter 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.2 2006/12/17 16:07:11 peter 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 	struct device		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(struct device *, struct cfdata *, void *);
     51 static void	scoopattach(struct device *, struct device *, void *);
     52 
     53 CFATTACH_DECL(scoop, sizeof(struct scoop_softc),
     54 	scoopmatch, scoopattach, NULL, NULL);
     55 
     56 #if 0
     57 static int	scoop_gpio_pin_read(struct scoop_softc *sc, int);
     58 #endif
     59 static void	scoop_gpio_pin_write(struct scoop_softc *sc, int, int);
     60 static void	scoop_gpio_pin_ctl(struct scoop_softc *sc, int, int);
     61 
     62 static int
     63 scoopmatch(struct device *parent, struct cfdata *cf, void *aux)
     64 {
     65 
     66 	/*
     67 	 * Only C3000-like models are known to have two SCOOPs.
     68 	 */
     69         if (ZAURUS_ISC3000)
     70 		return (cf->cf_unit < 2);
     71 	return (cf->cf_unit == 0);
     72 }
     73 
     74 static void
     75 scoopattach(struct device *parent, struct device *self, void *aux)
     76 {
     77 	struct pxaip_attach_args *pxa = (struct pxaip_attach_args *)aux;
     78 	struct scoop_softc *sc = (struct scoop_softc *)self;
     79 	bus_addr_t addr;
     80 	bus_size_t size;
     81 
     82 	sc->sc_iot = pxa->pxa_iot;
     83 
     84 	if (pxa->pxa_addr != -1)
     85 		addr = pxa->pxa_addr;
     86 	else if (sc->sc_dev.dv_unit == 0)
     87 		addr = C3000_SCOOP0_BASE;
     88 	else
     89 		addr = C3000_SCOOP1_BASE;
     90 
     91 	size = pxa->pxa_size < SCOOP_SIZE ? SCOOP_SIZE : pxa->pxa_size;
     92 
     93 	if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh) != 0) {
     94 		printf(": failed to map %s\n", sc->sc_dev.dv_xname);
     95 		return;
     96 	}
     97 
     98 	if (ZAURUS_ISC3000 && sc->sc_dev.dv_unit == 1) {
     99 		scoop_gpio_pin_ctl(sc, SCOOP1_AKIN_PULLUP, GPIO_PIN_OUTPUT);
    100 		scoop_gpio_pin_write(sc, SCOOP1_AKIN_PULLUP, GPIO_PIN_LOW);
    101 	} else if (!ZAURUS_ISC3000) {
    102 		scoop_gpio_pin_ctl(sc, SCOOP0_AKIN_PULLUP, GPIO_PIN_OUTPUT);
    103 		scoop_gpio_pin_write(sc, SCOOP0_AKIN_PULLUP, GPIO_PIN_LOW);
    104 	}
    105 
    106 	printf(": PCMCIA/GPIO controller\n");
    107 }
    108 
    109 #if 0
    110 static int
    111 scoop_gpio_pin_read(struct scoop_softc *sc, int pin)
    112 {
    113 	uint16_t bit = (1 << pin);
    114 	uint16_t rv;
    115 
    116 	rv = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPWR);
    117 	return (rv & bit) ? 1 : 0;
    118 }
    119 #endif
    120 
    121 static void
    122 scoop_gpio_pin_write(struct scoop_softc *sc, int pin, int level)
    123 {
    124 	uint16_t bit = (1 << pin);
    125 	uint16_t rv;
    126 
    127 	rv = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPWR);
    128 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPWR,
    129 	    (level == GPIO_PIN_LOW) ? (rv & ~bit) : (rv | bit));
    130 }
    131 
    132 static void
    133 scoop_gpio_pin_ctl(struct scoop_softc *sc, int pin, int flags)
    134 {
    135 	uint16_t bit = (1 << pin);
    136 	uint16_t rv;
    137 
    138 	rv = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPCR);
    139 	switch (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
    140 	case GPIO_PIN_INPUT:
    141 		rv &= ~bit;
    142 		break;
    143 	case GPIO_PIN_OUTPUT:
    144 		rv |= bit;
    145 		break;
    146 	}
    147 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPCR, rv);
    148 }
    149 
    150 /*
    151  * Turn the LCD background light and contrast signal on or off.
    152  */
    153 void
    154 scoop_set_backlight(int on, int cont)
    155 {
    156 
    157 	if (scoop_cd.cd_ndevs > 1 && scoop_cd.cd_devs[1] != NULL) {
    158 		/* C3000 */
    159 		scoop_gpio_pin_write(scoop_cd.cd_devs[1],
    160 		    SCOOP1_BACKLIGHT_CONT, !cont);
    161 		scoop_gpio_pin_write(scoop_cd.cd_devs[1],
    162 		    SCOOP1_BACKLIGHT_ON, on);
    163 	}
    164 #if 0
    165 	else if (scoop_cd.cd_ndevs > 0 && scoop_cd.cd_devs[0] != NULL) {
    166 		scoop_gpio_pin_write(scoop_cd.cd_devs[0],
    167 		    SCOOP0_BACKLIGHT_CONT, cont);
    168 	}
    169 #endif
    170 }
    171 
    172 /*
    173  * Turn the infrared LED on or off (must be on while transmitting).
    174  */
    175 void
    176 scoop_set_irled(int on)
    177 {
    178 
    179 	if (scoop_cd.cd_ndevs > 1 && scoop_cd.cd_devs[1] != NULL) {
    180 		/* IR_ON is inverted */
    181 		scoop_gpio_pin_write(scoop_cd.cd_devs[1],
    182 		    SCOOP1_IR_ON, !on);
    183 	}
    184 }
    185 
    186 /*
    187  * Turn the green and orange LEDs on or off.  If the orange LED is on,
    188  * then it is wired to indicate if A/C is connected.  The green LED has
    189  * no such predefined function.
    190  */
    191 void
    192 scoop_led_set(int led, int on)
    193 {
    194 
    195 	if (scoop_cd.cd_ndevs > 0 && scoop_cd.cd_devs[0] != NULL) {
    196 		if ((led & SCOOP_LED_GREEN) != 0) {
    197 			scoop_gpio_pin_write(scoop_cd.cd_devs[0],
    198 			    SCOOP0_LED_GREEN, on);
    199 		}
    200 		if (scoop_cd.cd_ndevs > 1 && (led & SCOOP_LED_ORANGE) != 0) {
    201 			scoop_gpio_pin_write(scoop_cd.cd_devs[0],
    202 			    SCOOP0_LED_ORANGE_C3000, on);
    203 		}
    204 	}
    205 }
    206 
    207 /*
    208  * Enable or disable the headphone output connection.
    209  */
    210 void
    211 scoop_set_headphone(int on)
    212 {
    213 
    214 	if (scoop_cd.cd_ndevs < 1 || scoop_cd.cd_devs[0] == NULL)
    215 		return;
    216 
    217 	scoop_gpio_pin_ctl(scoop_cd.cd_devs[0], SCOOP0_MUTE_L,
    218 	    GPIO_PIN_OUTPUT);
    219 	scoop_gpio_pin_ctl(scoop_cd.cd_devs[0], SCOOP0_MUTE_R,
    220 	    GPIO_PIN_OUTPUT);
    221 
    222 	if (on) {
    223 		scoop_gpio_pin_write(scoop_cd.cd_devs[0], SCOOP0_MUTE_L,
    224 		    GPIO_PIN_HIGH);
    225 		scoop_gpio_pin_write(scoop_cd.cd_devs[0], SCOOP0_MUTE_R,
    226 		    GPIO_PIN_HIGH);
    227 	} else {
    228 		scoop_gpio_pin_write(scoop_cd.cd_devs[0], SCOOP0_MUTE_L,
    229 		    GPIO_PIN_LOW);
    230 		scoop_gpio_pin_write(scoop_cd.cd_devs[0], SCOOP0_MUTE_R,
    231 		    GPIO_PIN_LOW);
    232 	}
    233 }
    234 
    235 /*
    236  * Turn on pullup resistor while not reading the remote control.
    237  */
    238 void
    239 scoop_akin_pullup(int enable)
    240 {
    241 
    242 	if (scoop_cd.cd_ndevs > 1 && scoop_cd.cd_devs[1] != NULL) {
    243 		scoop_gpio_pin_write(scoop_cd.cd_devs[1],
    244 		    SCOOP1_AKIN_PULLUP, enable);
    245 	} else {
    246 		scoop_gpio_pin_write(scoop_cd.cd_devs[0],
    247 		    SCOOP0_AKIN_PULLUP, enable);
    248 	}
    249 }
    250 
    251 void
    252 scoop_battery_temp_adc(int enable)
    253 {
    254 
    255 	if (scoop_cd.cd_ndevs > 0 && scoop_cd.cd_devs[0] != NULL) {
    256 		scoop_gpio_pin_write(scoop_cd.cd_devs[0],
    257 		    SCOOP0_ADC_TEMP_ON_C3000, enable);
    258 	}
    259 }
    260 
    261 void
    262 scoop_charge_battery(int enable, int voltage_high)
    263 {
    264 
    265 	if (scoop_cd.cd_ndevs > 0 && scoop_cd.cd_devs[0] != NULL) {
    266 		scoop_gpio_pin_write(scoop_cd.cd_devs[0],
    267 		    SCOOP0_JK_B_C3000, voltage_high);
    268 		scoop_gpio_pin_write(scoop_cd.cd_devs[0],
    269 		    SCOOP0_CHARGE_OFF_C3000, !enable);
    270 	}
    271 }
    272 
    273 void
    274 scoop_discharge_battery(int enable)
    275 {
    276 
    277 	if (scoop_cd.cd_ndevs > 0 && scoop_cd.cd_devs[0] != NULL) {
    278 		scoop_gpio_pin_write(scoop_cd.cd_devs[0],
    279 		    SCOOP0_JK_A_C3000, enable);
    280 	}
    281 }
    282 
    283 void
    284 scoop_set_sd_power(int enable)
    285 {
    286 	struct scoop_softc *sc;
    287 	bus_space_tag_t iot;
    288 	bus_space_handle_t ioh;
    289 	uint16_t v;
    290 
    291 	if (scoop_cd.cd_ndevs > 1 && scoop_cd.cd_devs[1] != NULL) {
    292 		sc = scoop_cd.cd_devs[0];
    293 		iot = sc->sc_iot;
    294 		ioh = sc->sc_ioh;
    295 
    296 		if (enable) {
    297 			v = bus_space_read_2(iot, ioh, SCOOP_GPWR);
    298 			v |= (1 << SCOOP0_CF_POWER_C3000);
    299 			bus_space_write_2(iot, ioh, SCOOP_GPWR, v);
    300 
    301 			v = bus_space_read_2(iot, ioh, SCOOP_CPR);
    302 			v |= (1 << SCP_CPR_SD);
    303 			bus_space_write_2(iot, ioh, SCOOP_CPR, v);
    304 		} else {
    305 			v = bus_space_read_2(iot, ioh, SCOOP_CPR);
    306 			v &= ~(1 << SCP_CPR_SD);
    307 			bus_space_write_2(iot, ioh, SCOOP_CPR, v);
    308 #if 0	/* XXX */
    309 			v = bus_space_read_2(iot, ioh, SCOOP_GPWR);
    310 			v &= ~(1 << SCOOP0_CF_POWER_C3000);
    311 			bus_space_write_2(iot, ioh, SCOOP_GPWR, v);
    312 #endif	/* XXX */
    313 		}
    314 	}
    315 }
    316 
    317 void
    318 scoop_check_mcr(void)
    319 {
    320 	struct scoop_softc *sc;
    321 	uint16_t v;
    322 
    323 	/* C3000 */
    324 	if (scoop_cd.cd_ndevs > 1 && scoop_cd.cd_devs[1] != NULL) {
    325 		sc = scoop_cd.cd_devs[0];
    326 		v = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SCOOP_MCR);
    327 		if ((v & 0x100) == 0) {
    328 			bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCOOP_MCR,
    329 			    0x0101);
    330 		}
    331 
    332 		sc = scoop_cd.cd_devs[1];
    333 		v = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SCOOP_MCR);
    334 		if ((v & 0x100) == 0) {
    335 			bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCOOP_MCR,
    336 			    0x0101);
    337 		}
    338 	}
    339 }
    340 
    341 void
    342 scoop_suspend(void)
    343 {
    344 	struct scoop_softc *sc;
    345 	uint32_t rv;
    346 
    347 	if (scoop_cd.cd_ndevs > 0 && scoop_cd.cd_devs[0] != NULL) {
    348 		sc = scoop_cd.cd_devs[0];
    349 		sc->sc_gpwr = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
    350 		    SCOOP_GPWR);
    351 		/* C3000 */
    352 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPWR,
    353 		    sc->sc_gpwr & ~((1<<SCOOP0_MUTE_L) | (1<<SCOOP0_MUTE_R) |
    354 		    (1<<SCOOP0_JK_A_C3000) | (1<<SCOOP0_ADC_TEMP_ON_C3000) |
    355 		    (1<<SCOOP0_LED_GREEN)));
    356 	}
    357 
    358 	/* C3000 */
    359 	if (scoop_cd.cd_ndevs > 1 && scoop_cd.cd_devs[1] != NULL) {
    360 		sc = scoop_cd.cd_devs[1];
    361 		sc->sc_gpwr = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
    362 		    SCOOP_GPWR);
    363 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPWR,
    364 		    sc->sc_gpwr & ~((1<<SCOOP1_RESERVED_4) |
    365 		    (1<<SCOOP1_RESERVED_5) | (1<<SCOOP1_RESERVED_6) |
    366 		    (1<<SCOOP1_BACKLIGHT_CONT) | (1<<SCOOP1_BACKLIGHT_ON) |
    367 		    (1<<SCOOP1_MIC_BIAS)));
    368 		rv = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPWR);
    369 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPWR,
    370 		    rv | ((1<<SCOOP1_IR_ON) | (1<<SCOOP1_RESERVED_3)));
    371 	}
    372 }
    373 
    374 void
    375 scoop_resume(void)
    376 {
    377 	struct scoop_softc *sc;
    378 
    379 	if (scoop_cd.cd_ndevs > 0 && scoop_cd.cd_devs[0] != NULL) {
    380 		sc = scoop_cd.cd_devs[0];
    381 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPWR,
    382 		    sc->sc_gpwr);
    383 	}
    384 
    385 	if (scoop_cd.cd_ndevs > 1 && scoop_cd.cd_devs[1] != NULL) {
    386 		sc = scoop_cd.cd_devs[1];
    387 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPWR,
    388 		    sc->sc_gpwr);
    389 	}
    390 }
    391