j720ssp.c revision 1.10 1 /* $NetBSD: j720ssp.c,v 1.10 2002/09/18 19:54:47 manu Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*-
40 * Copyright (c) 1990 The Regents of the University of California.
41 * All rights reserved.
42 *
43 * This code is derived from software contributed to Berkeley by
44 * William Jolitz and Don Ahn.
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. All advertising materials mentioning features or use of this software
55 * must display the following acknowledgement:
56 * This product includes software developed by the University of
57 * California, Berkeley and its contributors.
58 * 4. Neither the name of the University nor the names of its contributors
59 * may be used to endorse or promote products derived from this software
60 * without specific prior written permission.
61 *
62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 * SUCH DAMAGE.
73 *
74 * @(#)pccons.c 5.11 (Berkeley) 5/21/91
75 */
76
77 #include "apm.h"
78
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/device.h>
82 #include <sys/kernel.h>
83 #include <sys/malloc.h>
84 #include <sys/ioctl.h>
85 #include <sys/kthread.h>
86 #include <sys/lock.h>
87
88 #include <machine/bus.h>
89 #include <machine/config_hook.h>
90 #include <machine/bootinfo.h>
91 #include <machine/apmvar.h>
92
93 #include <hpcarm/dev/sed1356var.h>
94
95 #include <arm/sa11x0/sa11x0_var.h>
96 #include <arm/sa11x0/sa11x0_gpioreg.h>
97 #include <arm/sa11x0/sa11x0_ppcreg.h>
98 #include <arm/sa11x0/sa11x0_sspreg.h>
99
100 #include <dev/wscons/wsconsio.h>
101 #include <dev/wscons/wskbdvar.h>
102 #include <dev/wscons/wsksymdef.h>
103 #include <dev/wscons/wsksymvar.h>
104 #include <dev/wscons/wsmousevar.h>
105 #include <dev/hpc/tpcalibvar.h>
106
107 extern const struct wscons_keydesc j720kbd_keydesctab[];
108
109 struct j720ssp_softc {
110 struct device sc_dev;
111
112 bus_space_tag_t sc_iot;
113 bus_space_handle_t sc_gpioh;
114 bus_space_handle_t sc_ssph;
115
116 struct device *sc_wskbddev;
117 struct device *sc_wsmousedev;
118 struct tpcalib_softc sc_tpcalib;
119
120 void *sc_kbdsi;
121 void *sc_tpsi;
122 int sc_enabled;
123
124 struct proc *sc_ssp_kthread;
125 int sc_ssp_status;
126 struct simplelock sc_ssp_status_lock;
127 };
128 /* Values for struct softc's sc_ssp_status */
129 #define J720_SSP_STATUS_NONE 0
130 #define J720_SSP_STATUS_TP 1
131 #define J720_SSP_STATUS_KBD 2
132
133 void j720ssp_create_kthread __P((void *));
134 void j720ssp_kthread __P((void *));
135 int j720kbd_intr __P((void *));
136 int j720tp_intr __P((void *));
137 void j720kbd_poll __P((void *));
138 int j720tp_poll __P((void *));
139
140 int j720lcdparam __P((void *, int, long, void *));
141 static void j720kbd_read __P((struct j720ssp_softc *, char *));
142 static int j720ssp_readwrite __P((struct j720ssp_softc *, int,
143 int, int *, int));
144
145 int j720sspprobe __P((struct device *, struct cfdata *, void *));
146 void j720sspattach __P((struct device *, struct device *, void *));
147
148 int j720kbd_submatch __P((struct device *, struct cfdata *, void *));
149 int j720tp_submatch __P((struct device *, struct cfdata *, void *));
150 int apm_submatch __P((struct device *, struct cfdata *, void *));
151
152 int j720kbd_enable __P((void *, int));
153 void j720kbd_set_leds __P((void *, int));
154 int j720kbd_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
155
156 int hpcarm_apm_getpower __P((struct apm_power_info *, void *));
157
158 struct cfattach j720ssp_ca = {
159 sizeof(struct j720ssp_softc), j720sspprobe, j720sspattach,
160 };
161
162 const struct wskbd_accessops j720kbd_accessops = {
163 j720kbd_enable,
164 j720kbd_set_leds,
165 j720kbd_ioctl,
166 };
167
168 void j720kbd_cngetc __P((void *, u_int *, int *));
169 void j720kbd_cnpollc __P((void *, int));
170 void j720kbd_cnbell __P((void *, u_int, u_int, u_int));
171
172 const struct wskbd_consops j720kbd_consops = {
173 j720kbd_cngetc,
174 j720kbd_cnpollc,
175 j720kbd_cnbell,
176 };
177
178 const struct wskbd_mapdata j720kbd_keymapdata = {
179 j720kbd_keydesctab,
180 #ifdef J720KBD_LAYOUT
181 J720KBD_LAYOUT,
182 #else
183 KB_US,
184 #endif
185 };
186
187 static int j720tp_enable __P((void *));
188 static int j720tp_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
189 static void j720tp_disable __P((void *));
190
191 const struct wsmouse_accessops j720tp_accessops = {
192 j720tp_enable,
193 j720tp_ioctl,
194 j720tp_disable,
195 };
196
197 static int j720ssp_powerstate = 1;
198
199 static struct j720ssp_softc j720kbdcons_sc;
200 static int j720kbdcons_initstate = 0;
201
202 #define DEBUG
203 #ifdef DEBUG
204 int j720sspwaitcnt;
205 int j720sspwaittime;
206 extern int gettick();
207 #endif
208
209 #define BIT_INVERT(x) do { \
210 (x) = ((((x) & 0xf0) >> 4) | (((x) & 0x0f) << 4)); \
211 (x) = ((((x) & 0xcc) >> 2) | (((x) & 0x33) << 2)); \
212 (x) = ((((x) & 0xaa) >> 1) | (((x) & 0x55) << 1)); \
213 } while(0)
214
215
216 int
217 j720sspprobe(parent, cf, aux)
218 struct device *parent;
219 struct cfdata *cf;
220 void *aux;
221 {
222 return (1);
223 }
224
225 void
226 j720sspattach(parent, self, aux)
227 struct device *parent;
228 struct device *self;
229 void *aux;
230 {
231 struct j720ssp_softc *sc = (void *)self;
232 struct sa11x0_softc *psc = (void *)parent;
233 struct sa11x0_attach_args *sa = aux;
234 struct wskbddev_attach_args kbd_args;
235 struct wsmousedev_attach_args mouse_args;
236 struct apm_attach_args apm_args;
237
238 printf("\n");
239
240 sc->sc_iot = psc->sc_iot;
241 sc->sc_gpioh = psc->sc_gpioh;
242 if (bus_space_map(sc->sc_iot, sa->sa_addr, sa->sa_size, 0,
243 &sc->sc_ssph)) {
244 printf("%s: unable to map SSP registers\n",
245 sc->sc_dev.dv_xname);
246 return;
247 }
248
249 sc->sc_ssp_status = J720_SSP_STATUS_NONE;
250 simple_lock_init(&sc->sc_ssp_status_lock);
251 kthread_create(j720ssp_create_kthread, sc);
252
253 sc->sc_enabled = 0;
254
255 kbd_args.console = 0;
256
257 kbd_args.keymap = &j720kbd_keymapdata;
258
259 kbd_args.accessops = &j720kbd_accessops;
260 kbd_args.accesscookie = sc;
261
262 /* Do console initialization */
263 if (! (bootinfo->bi_cnuse & BI_CNUSE_SERIAL)) {
264 j720kbdcons_sc = *sc;
265 kbd_args.console = 1;
266
267 wskbd_cnattach(&j720kbd_consops, NULL, &j720kbd_keymapdata);
268 j720kbdcons_initstate = 1;
269 }
270
271 /*
272 * Attach the wskbd, saving a handle to it.
273 * XXX XXX XXX
274 */
275 sc->sc_wskbddev = config_found_sm(self, &kbd_args, wskbddevprint,
276 j720kbd_submatch);
277
278 #ifdef DEBUG
279 /* Zero the stat counters */
280 j720sspwaitcnt = 0;
281 j720sspwaittime = 0;
282 #endif
283
284 if (j720kbdcons_initstate == 1)
285 j720kbd_enable(sc, 1);
286
287 mouse_args.accessops = &j720tp_accessops;
288 mouse_args.accesscookie = sc;
289
290 sc->sc_wsmousedev = config_found_sm(self, &mouse_args,
291 wsmousedevprint, j720tp_submatch);
292 tpcalib_init(&sc->sc_tpcalib);
293
294 /* XXX fill in "default" calibrate param */
295 {
296 static const struct wsmouse_calibcoords j720_default_calib = {
297 0, 0, 639, 239,
298 4,
299 { { 988, 80, 0, 0 },
300 { 88, 84, 639, 0 },
301 { 988, 927, 0, 239 },
302 { 88, 940, 639, 239 } } };
303 tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS,
304 (caddr_t)&j720_default_calib, 0, 0);
305 }
306
307 j720tp_disable(sc);
308
309 /* Setup touchpad interrupt */
310 sa11x0_intr_establish(0, 9, 1, IPL_BIO, j720tp_intr, sc);
311
312 /* LCD control is on the same bus */
313 config_hook(CONFIG_HOOK_SET, CONFIG_HOOK_BRIGHTNESS,
314 CONFIG_HOOK_SHARE, j720lcdparam, sc);
315 config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_BRIGHTNESS,
316 CONFIG_HOOK_SHARE, j720lcdparam, sc);
317 config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_BRIGHTNESS_MAX,
318 CONFIG_HOOK_SHARE, j720lcdparam, sc);
319
320 config_hook(CONFIG_HOOK_SET, CONFIG_HOOK_CONTRAST,
321 CONFIG_HOOK_SHARE, j720lcdparam, sc);
322 config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_CONTRAST,
323 CONFIG_HOOK_SHARE, j720lcdparam, sc);
324 config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_CONTRAST_MAX,
325 CONFIG_HOOK_SHARE, j720lcdparam, sc);
326
327 #if NAPM > 0
328 /* attach APM emulation */
329 apm_args.aaa_magic = APM_ATTACH_ARGS_MAGIC; /* magic number */
330 (void)config_found_sm(self, &apm_args, NULL, apm_submatch);
331 #endif
332
333 return;
334 }
335
336 void
337 j720ssp_create_kthread(arg)
338 void *arg;
339 {
340 struct j720ssp_softc *sc = arg;
341
342 if (kthread_create1(j720ssp_kthread, sc,
343 &sc->sc_ssp_kthread, "j720ssp"))
344 panic("j720ssp_create_kthread");
345
346 return;
347 }
348
349 void
350 j720ssp_kthread(arg)
351 void *arg;
352 {
353 struct j720ssp_softc *sc = arg;
354 int ssp_status;
355
356 while (1) {
357 if (ssp_status & J720_SSP_STATUS_TP)
358 tsleep(&sc->sc_ssp_kthread, PRIBIO, "j720ssp", hz / 25);
359 else
360 tsleep(&sc->sc_ssp_kthread, PRIBIO, "j720ssp", 0);
361
362 simple_lock(&sc->sc_ssp_status_lock);
363 ssp_status = sc->sc_ssp_status;
364 sc->sc_ssp_status &= ~J720_SSP_STATUS_KBD;
365 simple_unlock(&sc->sc_ssp_status_lock);
366
367 if (ssp_status & J720_SSP_STATUS_KBD)
368 j720kbd_poll(sc);
369
370 if (ssp_status & J720_SSP_STATUS_TP) {
371 if (j720tp_poll(sc) == 0) {
372 simple_lock(&sc->sc_ssp_status_lock);
373 sc->sc_ssp_status &= ~J720_SSP_STATUS_TP;
374 simple_unlock(&sc->sc_ssp_status_lock);
375 }
376 }
377 }
378
379 /* NOTREACHED */
380 }
381
382
383 int
384 j720kbd_submatch(parent, cf, aux)
385 struct device *parent;
386 struct cfdata *cf;
387 void *aux;
388 {
389 if (strcmp(cf->cf_driver->cd_name, "wskbd") == 0)
390 return (1);
391 return (0);
392 }
393
394 int
395 j720tp_submatch(parent, cf, aux)
396 struct device *parent;
397 struct cfdata *cf;
398 void *aux;
399 {
400 if (strcmp(cf->cf_driver->cd_name, "wsmouse") == 0)
401 return (1);
402 return (0);
403 }
404
405 int
406 apm_submatch(parent, cf, aux)
407 struct device *parent;
408 struct cfdata *cf;
409 void *aux;
410 {
411 if (strcmp(cf->cf_driver->cd_name, "apm") == 0)
412 return (1);
413 return (0);
414 }
415
416 int
417 j720kbd_enable(v, on)
418 void *v;
419 int on;
420 {
421 struct j720ssp_softc *sc = v;
422
423 if (! sc->sc_enabled) {
424 sc->sc_enabled = 1;
425
426 sa11x0_intr_establish(0, 0, 1, IPL_BIO, j720kbd_intr, sc);
427 }
428 /* XXX */
429 return (0);
430 }
431
432 void
433 j720kbd_set_leds(v, on)
434 void *v;
435 int on;
436 {
437 /* XXX */
438 return;
439 }
440
441 int
442 j720kbd_ioctl(v, cmd, data, flag, p)
443 void *v;
444 u_long cmd;
445 caddr_t data;
446 int flag;
447 struct proc *p;
448 {
449 switch (cmd) {
450 case WSKBDIO_GTYPE:
451 *(int *)data = WSKBD_TYPE_HPC_KBD;
452 return 0;
453 }
454
455 return (EPASSTHROUGH);
456 }
457
458 int
459 j720kbd_intr(arg)
460 void *arg;
461 {
462 struct j720ssp_softc *sc = arg;
463
464 bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_EDR, 1);
465
466 simple_lock(&sc->sc_ssp_status_lock);
467 sc->sc_ssp_status |= J720_SSP_STATUS_KBD;
468 simple_unlock(&sc->sc_ssp_status_lock);
469
470 wakeup(&sc->sc_ssp_kthread);
471
472 return (1);
473 }
474
475 int
476 j720tp_intr(arg)
477 void *arg;
478 {
479 struct j720ssp_softc *sc = arg;
480
481 bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_EDR, 1 << 9);
482
483 simple_lock(&sc->sc_ssp_status_lock);
484 sc->sc_ssp_status |= J720_SSP_STATUS_TP;
485 simple_unlock(&sc->sc_ssp_status_lock);
486
487 j720tp_disable(sc);
488 wakeup(&sc->sc_ssp_kthread);
489
490 return (1);
491 }
492
493 void
494 j720kbd_poll(arg)
495 void *arg;
496 {
497 struct j720ssp_softc *sc = arg;
498 int s, type, value;
499 char buf[9], *p;
500
501 j720kbd_read(sc, buf);
502
503 for(p = buf; *p; p++) {
504 type = *p & 0x80 ? WSCONS_EVENT_KEY_UP :
505 WSCONS_EVENT_KEY_DOWN;
506 value = *p & 0x7f;
507 s = spltty();
508 wskbd_input(sc->sc_wskbddev, type, value);
509 splx(s);
510 if (type == WSCONS_EVENT_KEY_DOWN &&
511 value == 0x7f) {
512 j720ssp_powerstate = ! j720ssp_powerstate;
513 config_hook_call(CONFIG_HOOK_POWERCONTROL,
514 CONFIG_HOOK_POWERCONTROL_LCDLIGHT,
515 (void *)j720ssp_powerstate);
516 }
517 }
518
519 return;
520 }
521
522 void
523 j720kbd_read(sc, buf)
524 struct j720ssp_softc *sc;
525 char *buf;
526 {
527 int data, count;
528 #ifdef DEBUG
529 u_int32_t oscr;
530
531 oscr = gettick();
532 #endif
533 bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PCR, 0x2000000);
534
535 /* send scan keycode command */
536 if (j720ssp_readwrite(sc, 1, 0x900, &data, 100) < 0 ||
537 data != 0x88)
538 goto out;
539
540 /* read numbers of scancode available */
541 if (j720ssp_readwrite(sc, 0, 0x8800, &data, 100) < 0)
542 goto out;
543 BIT_INVERT(data);
544 count = data;
545
546 for(; count; count--) {
547 if (j720ssp_readwrite(sc, 0, 0x8800, &data, 100) < 0)
548 goto out;
549 BIT_INVERT(data);
550 *buf++ = data;
551 }
552 *buf = 0;
553 bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PSR, 0x2000000);
554
555 #ifdef DEBUG
556 oscr = (u_int32_t)gettick() - oscr;
557 j720sspwaitcnt++;
558 j720sspwaittime += oscr;
559 #endif
560
561 return;
562
563 out:
564 *buf = 0;
565 bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PSR, 0x2000000);
566
567 /* reset SSP */
568 bus_space_write_4(sc->sc_iot, sc->sc_ssph, SASSP_CR0, 0x307);
569 delay(100);
570 bus_space_write_4(sc->sc_iot, sc->sc_ssph, SASSP_CR0, 0x387);
571
572 printf("j720kbd_read: error %x\n", data);
573 return;
574 }
575
576 int
577 j720tp_poll(arg)
578 void *arg;
579 {
580 struct j720ssp_softc *sc = arg;
581 int buf[8], data, i, x, y;
582
583 /*
584 * If touch panel is not touched anymore,
585 * stop polling and re-enable interrupt
586 */
587 if (bus_space_read_4(sc->sc_iot,
588 sc->sc_gpioh, SAGPIO_PLR) & (1 << 9)) {
589 wsmouse_input(sc->sc_wsmousedev, 0, 0, 0, 0, 0);
590 j720tp_enable(sc);
591 return 0;
592 }
593
594 bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PCR, 0x2000000);
595
596 /* send read touchpanel command */
597 if (j720ssp_readwrite(sc, 1, 0x500, &data, 100) < 0 ||
598 data != 0x88)
599 goto out;
600
601 for(i = 0; i < 8; i++) {
602 if (j720ssp_readwrite(sc, 0, 0x8800, &data, 100) < 0)
603 goto out;
604 BIT_INVERT(data);
605 buf[i] = data;
606 }
607
608 bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PSR, 0x2000000);
609
610 buf[6] <<= 8;
611 buf[7] <<= 8;
612 for(i = 0; i < 3; i++) {
613 buf[i] |= buf[6] & 0x300;
614 buf[6] >>= 2;
615 buf[i + 3] |= buf[7] & 0x300;
616 buf[7] >>= 2;
617 }
618 #if 0
619 printf("j720tp_poll: %d %d %d %d %d %d\n", buf[0], buf[1], buf[2],
620 buf[3], buf[4], buf[5]);
621 #endif
622
623 /* XXX buf[1], buf[2], ... should also be used */
624 tpcalib_trans(&sc->sc_tpcalib, buf[1], buf[4], &x, &y);
625 wsmouse_input(sc->sc_wsmousedev, 1, x, y, 0,
626 WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
627
628 return 1;
629
630 out:
631 *buf = 0;
632 bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PSR, 0x2000000);
633
634 /* reset SSP */
635 bus_space_write_4(sc->sc_iot, sc->sc_ssph, SASSP_CR0, 0x307);
636 delay(100);
637 bus_space_write_4(sc->sc_iot, sc->sc_ssph, SASSP_CR0, 0x387);
638 printf("j720tp_poll: error %x\n", data);
639
640 return 0;
641 }
642
643 static int
644 j720tp_enable(arg)
645 void *arg;
646 {
647 struct j720ssp_softc *sc = arg;
648 int er, s;
649
650 s = splhigh();
651 er = bus_space_read_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_FER);
652 er |= 1 << 9;
653 bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_FER, er);
654 splx(s);
655
656 return (0);
657 }
658
659 static void
660 j720tp_disable(arg)
661 void *arg;
662 {
663 struct j720ssp_softc *sc = arg;
664 int er, s;
665
666 s = splhigh();
667 er = bus_space_read_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_FER);
668 er &= ~(1 << 9);
669 bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_FER, er);
670 splx(s);
671 }
672
673 static int
674 j720tp_ioctl(arg, cmd, data, flag, p)
675 void *arg;
676 u_long cmd;
677 caddr_t data;
678 int flag;
679 struct proc *p;
680 {
681 struct j720ssp_softc *sc = arg;
682
683 switch (cmd) {
684 case WSMOUSEIO_GTYPE:
685 *(u_int *)data = WSMOUSE_TYPE_TPANEL;
686 return (0);
687
688 case WSMOUSEIO_SCALIBCOORDS:
689 case WSMOUSEIO_GCALIBCOORDS:
690 return tpcalib_ioctl(&sc->sc_tpcalib, cmd, data, flag, p);
691
692 default:
693 return (EPASSTHROUGH);
694 }
695 }
696
697 int
698 j720lcdparam(ctx, type, id, msg)
699 void *ctx;
700 int type;
701 long id;
702 void *msg;
703 {
704 struct j720ssp_softc *sc = ctx;
705 int i, s;
706 u_int32_t data[2], len;
707
708 switch (type) {
709 case CONFIG_HOOK_GET:
710 switch (id) {
711 case CONFIG_HOOK_BRIGHTNESS_MAX:
712 case CONFIG_HOOK_CONTRAST_MAX:
713 *(int *)msg = 255;
714 return 1;
715 case CONFIG_HOOK_BRIGHTNESS:
716 data[0] = 0x6b00;
717 data[1] = 0x8800;
718 len = 2;
719 break;
720 case CONFIG_HOOK_CONTRAST:
721 data[0] = 0x2b00;
722 data[1] = 0x8800;
723 len = 2;
724 break;
725 default:
726 return 0;
727 }
728 break;
729
730 case CONFIG_HOOK_SET:
731 switch (id) {
732 case CONFIG_HOOK_BRIGHTNESS:
733 if (*(int *)msg >= 0) {
734 data[0] = 0xcb00;
735 data[1] = *(int *)msg;
736 BIT_INVERT(data[1]);
737 data[1] <<= 8;
738 len = 2;
739 } else {
740 /* XXX hack */
741 data[0] = 0xfb00;
742 len = 1;
743 }
744 break;
745 case CONFIG_HOOK_CONTRAST:
746 data[0] = 0x8b00;
747 data[1] = *(int *)msg;
748 BIT_INVERT(data[1]);
749 data[1] <<= 8;
750 len = 2;
751 break;
752 default:
753 return 0;
754 }
755 }
756
757 s = splbio();
758 bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PCR, 0x2000000);
759
760 for (i = 0; i < len; i++) {
761 if (j720ssp_readwrite(sc, 1, data[i], &data[i], 500) < 0)
762 goto out;
763 }
764 bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PSR, 0x2000000);
765 splx(s);
766
767 if (type == CONFIG_HOOK_SET)
768 return 1;
769
770 BIT_INVERT(data[1]);
771 *(int *)msg = data[1];
772
773 return 1;
774
775 out:
776 bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PSR, 0x2000000);
777
778 /* reset SSP */
779 bus_space_write_4(sc->sc_iot, sc->sc_ssph, SASSP_CR0, 0x307);
780 delay(100);
781 bus_space_write_4(sc->sc_iot, sc->sc_ssph, SASSP_CR0, 0x387);
782 splx(s);
783 return 0;
784 }
785
786 static int
787 j720ssp_readwrite(sc, drainfifo, in, out, wait)
788 struct j720ssp_softc *sc;
789 int drainfifo;
790 int in;
791 int *out;
792 int wait;
793 {
794 int timo;
795
796 timo = 100000;
797 while(bus_space_read_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PLR) & 0x400)
798 if (--timo == 0) {
799 printf("timo0\n");
800 return -1;
801 }
802 if (drainfifo) {
803 while(bus_space_read_4(sc->sc_iot, sc->sc_ssph, SASSP_SR) &
804 SR_RNE)
805 bus_space_read_4(sc->sc_iot, sc->sc_ssph, SASSP_DR);
806 #if 1
807 delay(wait);
808 #endif
809 }
810
811 bus_space_write_4(sc->sc_iot, sc->sc_ssph, SASSP_DR, in);
812
813 delay(wait);
814 timo = 100000;
815 while(! (bus_space_read_4(sc->sc_iot, sc->sc_ssph, SASSP_SR) & SR_RNE))
816 if (--timo == 0) {
817 printf("timo1\n");
818 return -1;
819 }
820
821 *out = bus_space_read_4(sc->sc_iot, sc->sc_ssph, SASSP_DR);
822
823 return 0;
824 }
825
826 #if 0
827 int
828 j720kbd_cnattach(void)
829 {
830 /* XXX defer initialization till j720sspattach */
831
832 return (0);
833 }
834 #endif
835
836 /* ARGSUSED */
837 void
838 j720kbd_cngetc(v, type, data)
839 void *v;
840 u_int *type;
841 int *data;
842 {
843 char buf[9];
844
845 if (j720kbdcons_initstate < 1)
846 return;
847
848 for (;;) {
849 j720kbd_read(&j720kbdcons_sc, buf);
850
851 if (buf[0] != 0) {
852 /* XXX we are discarding buffer contents */
853 *type = buf[0] & 0x80 ? WSCONS_EVENT_KEY_UP :
854 WSCONS_EVENT_KEY_DOWN;
855 *data = buf[0] & 0x7f;
856 return;
857 }
858 }
859 }
860
861 void
862 j720kbd_cnpollc(v, on)
863 void *v;
864 int on;
865 {
866 #if 0
867 /* XXX */
868 struct j720kbd_internal *t = v;
869
870 pckbc_set_poll(t->t_kbctag, t->t_kbcslot, on);
871 #endif
872 }
873
874 void
875 j720kbd_cnbell(v, pitch, period, volume)
876 void *v;
877 u_int pitch;
878 u_int period;
879 u_int volume;
880 {
881 }
882
883 int
884 j720lcdpower(ctx, type, id, msg)
885 void *ctx;
886 int type;
887 long id;
888 void *msg;
889 {
890 struct sed1356_softc *sc = ctx;
891 struct sa11x0_softc *psc = sc->sc_parent;
892 int val;
893 u_int32_t reg;
894
895 if (type != CONFIG_HOOK_POWERCONTROL ||
896 id != CONFIG_HOOK_POWERCONTROL_LCDLIGHT)
897 return 0;
898
899 sed1356_init_brightness(sc, 0);
900 sed1356_init_contrast(sc, 0);
901
902 if (msg) {
903 bus_space_write_1(sc->sc_iot, sc->sc_regh, 0x1f0, 0);
904
905 reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
906 reg |= 0x1;
907 bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
908 delay(50000);
909
910 val = sc->sc_contrast;
911 config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_CONTRAST, &val);
912 delay(100000);
913
914 reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
915 reg |= 0x4;
916 bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
917
918 val = sc->sc_brightness;
919 config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_BRIGHTNESS, &val);
920
921 reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
922 reg |= 0x2;
923 bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
924 } else {
925 reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
926 reg &= ~0x2;
927 bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
928 reg &= ~0x4;
929 bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
930 delay(100000);
931
932 val = -2;
933 config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_BRIGHTNESS, &val);
934
935 bus_space_write_1(sc->sc_iot, sc->sc_regh, 0x1f0, 1);
936
937 delay(100000);
938 reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
939 reg &= ~0x1;
940 bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
941 }
942 return 1;
943 }
944
945 int
946 hpcarm_apm_getpower(api, parent)
947 struct apm_power_info *api;
948 void *parent;
949 {
950 int data, pmdata[3], i;
951 struct j720ssp_softc *sc = (struct j720ssp_softc *)parent;
952
953 bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PCR, 0x2000000);
954
955 if (j720ssp_readwrite(sc, 1, 0x300, &data, 100) < 0 || data != 0x88)
956 goto out;
957
958 for (i = 0; i < 3; i++) {
959 if (j720ssp_readwrite(sc, 0, 0x8800, &pmdata[i], 500) < 0)
960 goto out;
961 BIT_INVERT(pmdata[i]);
962 #if 0
963 printf("pmdata%d = %d ", i, pmdata[i]);
964 #endif
965 }
966 printf("\n");
967
968 bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PSR, 0x2000000);
969
970 bzero(api, sizeof(struct apm_power_info));
971 api->ac_state = APM_AC_UNKNOWN;
972
973 /*
974 * pmdata[0] is the main battery level
975 * pmdata[1] is the backup battery level
976 * pmdata[2] tells which battery is present
977 */
978 switch(pmdata[2]) {
979 case 14: /* backup battery present */
980 case 2: /* backup battery absent */
981 api->battery_state = APM_BATT_CHARGING;
982 api->minutes_left = (pmdata[0] * 840) / 170;
983 api->battery_life = (pmdata[0] * 100) / 170;
984 api->nbattery = 1;
985 break;
986 case 15: /* backup battery present */
987 case 3: /* backup battery absent */
988 api->battery_state = APM_BATT_ABSENT;
989 api->battery_life = 0;
990 api->nbattery = 0;
991 break;
992 default:
993 api->battery_state = APM_BATT_UNKNOWN;
994 break;
995 }
996
997 return 0;
998
999 out:
1000 bus_space_write_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PSR, 0x2000000);
1001
1002 /* reset SSP */
1003 bus_space_write_4(sc->sc_iot, sc->sc_ssph, SASSP_CR0, 0x307);
1004 delay(100);
1005 bus_space_write_4(sc->sc_iot, sc->sc_ssph, SASSP_CR0, 0x387);
1006
1007 printf("hpcarm_apm_getpower: error %x\n", data);
1008 return EIO;
1009 }
1010