xref: /netbsd-src/sys/arch/zaurus/dev/scoop.c (revision d25ffa98a4bfca1fe272f3c182496ec9934faac7)
1 /*	$NetBSD: scoop.c,v 1.8 2011/06/19 16:20:09 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@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.8 2011/06/19 16:20:09 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_ISC860) {
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 #if 0
119 static int
120 scoop_gpio_pin_read(struct scoop_softc *sc, int pin)
121 {
122 	uint16_t bit = (1 << pin);
123 	uint16_t rv;
124 
125 	rv = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPWR);
126 	return (rv & bit) ? 1 : 0;
127 }
128 #endif
129 
130 static void
131 scoop_gpio_pin_write(struct scoop_softc *sc, int pin, int level)
132 {
133 	uint16_t bit = (1 << pin);
134 	uint16_t rv;
135 
136 	rv = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPWR);
137 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPWR,
138 	    (level == GPIO_PIN_LOW) ? (rv & ~bit) : (rv | bit));
139 }
140 
141 static void
142 scoop_gpio_pin_ctl(struct scoop_softc *sc, int pin, int flags)
143 {
144 	uint16_t bit = (1 << pin);
145 	uint16_t rv;
146 
147 	rv = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPCR);
148 	switch (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
149 	case GPIO_PIN_INPUT:
150 		rv &= ~bit;
151 		break;
152 	case GPIO_PIN_OUTPUT:
153 		rv |= bit;
154 		break;
155 	}
156 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPCR, rv);
157 }
158 
159 /*
160  * Turn the LCD background light and contrast signal on or off.
161  */
162 void
163 scoop_set_backlight(int on, int cont)
164 {
165 	struct scoop_softc *sc;
166 #if 0
167 	struct scoop_softc *sc0;
168 
169 	sc0 = device_lookup_private(&scoop_cd, 0);
170 #endif
171 
172 	sc = device_lookup_private(&scoop_cd, 1);
173 	if (sc != NULL) {
174 		/* C3000 */
175 		scoop_gpio_pin_write(sc, SCOOP1_BACKLIGHT_CONT, !cont);
176 		scoop_gpio_pin_write(sc, SCOOP1_BACKLIGHT_ON, on);
177 	}
178 #if 0
179 	else if (sc0 != NULL) {
180 		scoop_gpio_pin_write(sc0,
181 		    SCOOP0_BACKLIGHT_CONT, cont);
182 	}
183 #endif
184 }
185 
186 /*
187  * Turn the infrared LED on or off (must be on while transmitting).
188  */
189 void
190 scoop_set_irled(int on)
191 {
192 	struct scoop_softc *sc;
193 
194 	sc = device_lookup_private(&scoop_cd, 1);
195 	if (sc != NULL) {
196 		/* IR_ON is inverted */
197 		scoop_gpio_pin_write(sc, SCOOP1_IR_ON, !on);
198 	}
199 }
200 
201 /*
202  * Turn the green and orange LEDs on or off.  If the orange LED is on,
203  * then it is wired to indicate if A/C is connected.  The green LED has
204  * no such predefined function.
205  */
206 void
207 scoop_led_set(int led, int on)
208 {
209 	struct scoop_softc *sc;
210 
211 	sc = device_lookup_private(&scoop_cd, 0);
212 	if (sc != NULL) {
213 		if ((led & SCOOP_LED_GREEN) != 0) {
214 			scoop_gpio_pin_write(sc, SCOOP0_LED_GREEN, on);
215 		}
216 		if (scoop_cd.cd_ndevs > 1 && (led & SCOOP_LED_ORANGE) != 0) {
217 			scoop_gpio_pin_write(sc, SCOOP0_LED_ORANGE_C3000, on);
218 		}
219 	}
220 }
221 
222 /*
223  * Enable or disable the headphone output connection.
224  */
225 void
226 scoop_set_headphone(int on)
227 {
228 	struct scoop_softc *sc;
229 
230 	sc = device_lookup_private(&scoop_cd, 0);
231 	if (sc == NULL)
232 		return;
233 
234 	scoop_gpio_pin_ctl(sc, SCOOP0_MUTE_L, GPIO_PIN_OUTPUT);
235 	scoop_gpio_pin_ctl(sc, SCOOP0_MUTE_R, GPIO_PIN_OUTPUT);
236 
237 	if (on) {
238 		scoop_gpio_pin_write(sc, SCOOP0_MUTE_L, GPIO_PIN_HIGH);
239 		scoop_gpio_pin_write(sc, SCOOP0_MUTE_R, GPIO_PIN_HIGH);
240 	} else {
241 		scoop_gpio_pin_write(sc, SCOOP0_MUTE_L, GPIO_PIN_LOW);
242 		scoop_gpio_pin_write(sc, SCOOP0_MUTE_R, GPIO_PIN_LOW);
243 	}
244 }
245 
246 /*
247  * Enable or disable the mic bias
248  */
249 void
250 scoop_set_mic_bias(int onoff)
251 {
252 	struct scoop_softc *sc1;
253 
254 	sc1 = device_lookup_private(&scoop_cd, 1);
255 	if (sc1 != NULL)
256 		scoop_gpio_pin_write(sc1, SCOOP1_MIC_BIAS, onoff);
257 }
258 
259 /*
260  * Turn on pullup resistor while not reading the remote control.
261  */
262 void
263 scoop_akin_pullup(int enable)
264 {
265 	struct scoop_softc *sc0;
266 	struct scoop_softc *sc1;
267 
268 	sc0 = device_lookup_private(&scoop_cd, 0);
269 	sc1 = device_lookup_private(&scoop_cd, 1);
270 
271 	if (sc1 != NULL) {
272 		scoop_gpio_pin_write(sc1, SCOOP1_AKIN_PULLUP, enable);
273 	} else if (sc0 != NULL) {
274 		scoop_gpio_pin_write(sc0, SCOOP0_AKIN_PULLUP, enable);
275 	}
276 }
277 
278 void
279 scoop_battery_temp_adc(int enable)
280 {
281 	struct scoop_softc *sc;
282 
283 	sc = device_lookup_private(&scoop_cd, 0);
284 
285 	if (sc != NULL) {
286 		scoop_gpio_pin_write(sc, SCOOP0_ADC_TEMP_ON_C3000, enable);
287 	}
288 }
289 
290 void
291 scoop_charge_battery(int enable, int voltage_high)
292 {
293 	struct scoop_softc *sc;
294 
295 	sc = device_lookup_private(&scoop_cd, 0);
296 
297 	if (sc != NULL) {
298 		scoop_gpio_pin_write(sc, SCOOP0_JK_B_C3000, voltage_high);
299 		scoop_gpio_pin_write(sc, SCOOP0_CHARGE_OFF_C3000, !enable);
300 	}
301 }
302 
303 void
304 scoop_discharge_battery(int enable)
305 {
306 	struct scoop_softc *sc;
307 
308 	sc = device_lookup_private(&scoop_cd, 0);
309 
310 	if (sc != NULL) {
311 		scoop_gpio_pin_write(sc, SCOOP0_JK_A_C3000, enable);
312 	}
313 }
314 
315 /*
316  * Enable or disable 3.3V power to the SD/MMC card slot.
317  */
318 void
319 scoop_set_sdmmc_power(int on)
320 {
321 
322 	scoop0_set_card_power(SD_CARD, on ? SCP_CPR_SD_3V : SCP_CPR_OFF);
323 }
324 
325 /*
326  * The Card Power Register of the first SCOOP unit controls the power
327  * for the first CompactFlash slot and the SD/MMC card slot as well.
328  */
329 void
330 scoop0_set_card_power(enum scoop_card card, int new_cpr)
331 {
332 	struct scoop_softc *sc;
333 	bus_space_tag_t iot;
334 	bus_space_handle_t ioh;
335 	uint16_t cpr;
336 
337 	sc = device_lookup_private(&scoop_cd, 0);
338 	if (sc == NULL)
339 		return;
340 
341 	iot = sc->sc_iot;
342 	ioh = sc->sc_ioh;
343 
344 	cpr = bus_space_read_2(iot, ioh, SCOOP_CPR);
345 	if (new_cpr & SCP_CPR_VOLTAGE_MSK) {
346 		if (card == CF_CARD)
347 			cpr |= SCP_CPR_5V;
348 		else if (card == SD_CARD)
349 			cpr |= SCP_CPR_SD_3V;
350 
351 		scoop_gpio_pin_write(sc, SCOOP0_CF_POWER_C3000, 1);
352 		if (!ISSET(cpr, SCP_CPR_5V) && !ISSET(cpr, SCP_CPR_SD_3V))
353 			delay(5000);
354 		bus_space_write_2(iot, ioh, SCOOP_CPR, cpr | new_cpr);
355 	} else {
356 		if (card == CF_CARD)
357 			cpr &= ~SCP_CPR_5V;
358 		else if (card == SD_CARD)
359 			cpr &= ~SCP_CPR_SD_3V;
360 
361 		if (!ISSET(cpr, SCP_CPR_5V) && !ISSET(cpr, SCP_CPR_SD_3V)) {
362 			bus_space_write_2(iot, ioh, SCOOP_CPR, SCP_CPR_OFF);
363 			delay(1000);
364 			scoop_gpio_pin_write(sc, SCOOP0_CF_POWER_C3000, 0);
365 		} else
366 			bus_space_write_2(iot, ioh, SCOOP_CPR, cpr | new_cpr);
367 	}
368 }
369 
370 void
371 scoop_check_mcr(void)
372 {
373 	struct scoop_softc *sc0, *sc1, *sc;
374 	uint16_t v;
375 
376 	sc0 = device_lookup_private(&scoop_cd, 0);
377 	sc1 = device_lookup_private(&scoop_cd, 1);
378 
379 	/* C3000 */
380 	if (sc1 != NULL) {
381 		sc = sc0;
382 		v = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SCOOP_MCR);
383 		if ((v & 0x100) == 0) {
384 			bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCOOP_MCR,
385 			    0x0101);
386 		}
387 
388 		sc = sc1;
389 		v = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SCOOP_MCR);
390 		if ((v & 0x100) == 0) {
391 			bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCOOP_MCR,
392 			    0x0101);
393 		}
394 	}
395 }
396 
397 void
398 scoop_suspend(void)
399 {
400 	struct scoop_softc *sc, *sc0, *sc1;
401 	uint32_t rv;
402 
403 	sc0 = device_lookup_private(&scoop_cd, 0);
404 	sc1 = device_lookup_private(&scoop_cd, 1);
405 
406 	if (sc0 != NULL) {
407 		sc = sc0;
408 		sc->sc_gpwr = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
409 		    SCOOP_GPWR);
410 		/* C3000 */
411 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPWR,
412 		    sc->sc_gpwr & ~((1<<SCOOP0_MUTE_L) | (1<<SCOOP0_MUTE_R) |
413 		    (1<<SCOOP0_JK_A_C3000) | (1<<SCOOP0_ADC_TEMP_ON_C3000) |
414 		    (1<<SCOOP0_LED_GREEN)));
415 	}
416 
417 	/* C3000 */
418 	if (sc1 != NULL) {
419 		sc = sc1;
420 		sc->sc_gpwr = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
421 		    SCOOP_GPWR);
422 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPWR,
423 		    sc->sc_gpwr & ~((1<<SCOOP1_RESERVED_4) |
424 		    (1<<SCOOP1_RESERVED_5) | (1<<SCOOP1_RESERVED_6) |
425 		    (1<<SCOOP1_BACKLIGHT_CONT) | (1<<SCOOP1_BACKLIGHT_ON) |
426 		    (1<<SCOOP1_MIC_BIAS)));
427 		rv = bus_space_read_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPWR);
428 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPWR,
429 		    rv | ((1<<SCOOP1_IR_ON) | (1<<SCOOP1_RESERVED_3)));
430 	}
431 }
432 
433 void
434 scoop_resume(void)
435 {
436 	struct scoop_softc *sc, *sc0, *sc1;
437 
438 	sc0 = device_lookup_private(&scoop_cd, 0);
439 	sc1 = device_lookup_private(&scoop_cd, 1);
440 
441 	if (sc0 != NULL) {
442 		sc = sc0;
443 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPWR,
444 		    sc->sc_gpwr);
445 	}
446 
447 	if (sc1 != NULL) {
448 		sc = sc1;
449 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, SCOOP_GPWR,
450 		    sc->sc_gpwr);
451 	}
452 }
453