Home | History | Annotate | Line # | Download | only in dev
ite8181.c revision 1.13
      1 /*	$NetBSD: ite8181.c,v 1.13 2001/07/17 01:41:38 toshii Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000,2001 SATO Kazumi
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  *
     28  */
     29 
     30 #include <sys/param.h>
     31 #include <sys/kernel.h>
     32 #include <sys/device.h>
     33 #include <sys/systm.h>
     34 #include <sys/boot_flag.h>
     35 #include <sys/buf.h>
     36 
     37 #include <uvm/uvm_extern.h>
     38 
     39 #include <dev/wscons/wsconsio.h>
     40 
     41 #include <machine/bootinfo.h>
     42 #include <machine/bus.h>
     43 #include <machine/autoconf.h>
     44 #include <machine/config_hook.h>
     45 #include <machine/platid.h>
     46 #include <machine/platid_mask.h>
     47 
     48 #include <hpcmips/dev/ite8181reg.h>
     49 #include <hpcmips/dev/ite8181var.h>
     50 #include "bivideo.h"
     51 #if NBIVIDEO > 0
     52 #include <dev/hpc/bivideovar.h>
     53 #endif
     54 #include <dev/hpc/hpccmapvar.h>
     55 
     56 #define ITE8181DEBUG
     57 #ifdef ITE8181DEBUG
     58 #ifndef ITE8181DEBUG_CONF
     59 #define ITE8181DEBUG_CONF 0
     60 #endif
     61 int	ite8181_debug = ITE8181DEBUG_CONF;
     62 #define	DPRINTF(arg)     if (ite8181_debug) printf arg
     63 #define	DPRINTFN(n, arg) if (ite8181_debug > (n)) printf arg
     64 #define	VPRINTF(arg)     if (bootverbose || ite8181_debug) printf arg
     65 #define	VPRINTFN(n, arg) if (bootverbose || ite8181_debug > (n)) printf arg
     66 #else
     67 #define	DPRINTF(arg)
     68 #define DPRINTFN(n, arg)
     69 #define	VPRINTF(arg)     if (bootverbose) printf arg
     70 #define	VPRINTFN(n, arg) if (bootverbose) printf arg
     71 #endif
     72 
     73 #ifndef ITE8181_LCD_CONTROL_ENABLE
     74 int ite8181_lcd_control_disable = 1;
     75 #else /* ITE8181_LCD_CONTROL_ENABLE */
     76 int ite8181_lcd_control_disable = 0;
     77 #endif /* ITE8181_LCD_CONTROL_ENABLE */
     78 
     79 #define ITE8181_WINCE_CMAP
     80 
     81 /*
     82  * XXX:
     83  * IBM WorkPad z50 power unit has too weak power.
     84  * So we must wait too many times to access some device
     85  * after LCD panel and BackLight on.
     86  * Currently delay is not enough ??? FIXME
     87  */
     88 #ifndef ITE8181_LCD_ON_SELF_DELAY
     89 #define ITE8181_LCD_ON_SELF_DELAY 1000
     90 #endif /* ITE8181_LCD_ON__SELF_DELAY */
     91 #ifndef ITE8181_LCD_ON_DELAY
     92 #define ITE8181_LCD_ON_DELAY 2000
     93 #endif /* ITE8181_LCD_ON_DELAY */
     94 int ite8181_lcd_on_self_delay = ITE8181_LCD_ON_SELF_DELAY; /* msec */
     95 int ite8181_lcd_on_delay = ITE8181_LCD_ON_DELAY; /* msec */
     96 
     97 #define MSEC	1000
     98 /*
     99  * function prototypes
    100  */
    101 static void	ite8181_config_write_4 __P((bus_space_tag_t, bus_space_handle_t, int, int));
    102 static int	ite8181_config_read_4 __P((bus_space_tag_t, bus_space_handle_t, int));
    103 
    104 static void	ite8181_gui_write_4 __P((struct ite8181_softc *, int, int));
    105 static int	ite8181_gui_read_4 __P((struct ite8181_softc *, int));
    106 
    107 static void	ite8181_gui_write_1 __P((struct ite8181_softc *, int, int));
    108 static int	ite8181_gui_read_1 __P((struct ite8181_softc *, int));
    109 
    110 static void	ite8181_graphics_write_1 __P((struct ite8181_softc *, int, int));
    111 static int	ite8181_graphics_read_1 __P((struct ite8181_softc *, int));
    112 
    113 static void	ite8181_ema_write_1 __P((struct ite8181_softc *, int, int));
    114 static int	ite8181_ema_read_1 __P((struct ite8181_softc *, int));
    115 
    116 static void	ite8181_power __P((int, void *));
    117 static int	ite8181_hardpower __P((void *, int, long, void *));
    118 static int	ite8181_fbinit __P((struct hpcfb_fbconf *));
    119 static int	ite8181_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
    120 static paddr_t	ite8181_mmap __P((void *, off_t offset, int));
    121 static void	ite8181_erase_cursor __P((struct ite8181_softc *));
    122 static int	ite8181_lcd_power __P((struct ite8181_softc *, int));
    123 
    124 static void	ite8181_update_powerstate __P((struct ite8181_softc *, int));
    125 void	ite8181_init_backlight __P((struct ite8181_softc *, int));
    126 void	ite8181_init_brightness __P((struct ite8181_softc *, int));
    127 void	ite8181_init_contrast __P((struct ite8181_softc *, int));
    128 void	ite8181_set_brightness __P((struct ite8181_softc *, int));
    129 void	ite8181_set_contrast __P((struct ite8181_softc *, int));
    130 
    131 /*
    132  * static variables
    133  */
    134 struct hpcfb_accessops ite8181_ha = {
    135 	ite8181_ioctl, ite8181_mmap
    136 };
    137 
    138 inline int
    139 ite8181_config_read_4(iot, ioh, byteoffset)
    140 	bus_space_tag_t iot;
    141 	bus_space_handle_t ioh;
    142 	int byteoffset;
    143 {
    144 	return bus_space_read_4(iot, ioh, ITE8181_CONF_OFFSET + byteoffset);
    145 }
    146 
    147 inline void
    148 ite8181_config_write_4(iot, ioh, byteoffset, data)
    149 	bus_space_tag_t iot;
    150 	bus_space_handle_t ioh;
    151 	int byteoffset;
    152 	int data;
    153 {
    154 	bus_space_write_4(iot, ioh, ITE8181_CONF_OFFSET + byteoffset, data);
    155 }
    156 
    157 inline int
    158 ite8181_gui_read_4(sc, byteoffset)
    159 	struct ite8181_softc *sc;
    160 	int byteoffset;
    161 {
    162 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    163 			sc->sc_gba + byteoffset);
    164 }
    165 
    166 inline void
    167 ite8181_gui_write_4(sc, byteoffset, data)
    168 	struct ite8181_softc *sc;
    169 	int byteoffset;
    170 	int data;
    171 {
    172 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, sc->sc_gba + byteoffset, data);
    173 }
    174 
    175 inline int
    176 ite8181_gui_read_1(sc, byteoffset)
    177 	struct ite8181_softc *sc;
    178 	int byteoffset;
    179 {
    180 	return bus_space_read_1(sc->sc_iot, sc->sc_ioh,
    181 			sc->sc_gba + byteoffset);
    182 }
    183 
    184 inline void
    185 ite8181_gui_write_1(sc, byteoffset, data)
    186 	struct ite8181_softc *sc;
    187 	int byteoffset;
    188 	int data;
    189 {
    190 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, sc->sc_gba + byteoffset, data);
    191 }
    192 
    193 inline int
    194 ite8181_graphics_read_1(sc, byteoffset)
    195 	struct ite8181_softc *sc;
    196 	int byteoffset;
    197 {
    198 	return bus_space_read_1(sc->sc_iot, sc->sc_ioh,
    199 			sc->sc_sba + byteoffset);
    200 }
    201 
    202 inline void
    203 ite8181_graphics_write_1(sc, byteoffset, data)
    204 	struct ite8181_softc *sc;
    205 	int byteoffset;
    206 	int data;
    207 {
    208 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, sc->sc_sba + byteoffset, data);
    209 }
    210 
    211 inline int
    212 ite8181_ema_read_1(sc, byteoffset)
    213 	struct ite8181_softc *sc;
    214 	int byteoffset;
    215 {
    216 	ite8181_graphics_write_1(sc, ITE8181_EMA_EXAX, byteoffset);
    217 	return ite8181_graphics_read_1(sc, ITE8181_EMA_EXADATA);
    218 }
    219 
    220 inline void
    221 ite8181_ema_write_1(sc, byteoffset, data)
    222 	struct ite8181_softc *sc;
    223 	int byteoffset;
    224 	int data;
    225 {
    226 	ite8181_graphics_write_1(sc, ITE8181_EMA_EXAX, byteoffset);
    227 	ite8181_graphics_write_1(sc, ITE8181_EMA_EXADATA, data);
    228 }
    229 
    230 int
    231 ite8181_probe(iot, ioh)
    232 	bus_space_tag_t iot;
    233 	bus_space_handle_t ioh;
    234 {
    235 	unsigned long regval;
    236 
    237 #if NBIVIDEO > 0
    238 	if (bivideo_dont_attach)	/* some video driver already attached */
    239 		return (0);
    240 #endif /* NBIVIDEO > 0 */
    241 
    242 	regval = ite8181_config_read_4(iot, ioh, ITE8181_ID);
    243 	VPRINTF(("ite8181_probe: vendor id=%04lx product id=%04lx\n",
    244 		 regval & 0xffff, (regval >> 16) & 0xffff));
    245 	if (regval != ((ITE8181_PRODUCT_ID << 16) | ITE8181_VENDER_ID))
    246 		return (0);
    247 
    248 	return (1);
    249 }
    250 
    251 void
    252 ite8181_attach(sc)
    253 	struct ite8181_softc *sc;
    254 {
    255 	unsigned long regval;
    256 	struct hpcfb_attach_args ha;
    257 	int console = (bootinfo->bi_cnuse & BI_CNUSE_SERIAL) ? 0 : 1;
    258 
    259 	printf(": ");
    260 	if (ite8181_fbinit(&sc->sc_fbconf) != 0) {
    261 		/* just return so that hpcfb will not be attached */
    262 		return;
    263 	}
    264 
    265 	regval = ite8181_config_read_4(sc->sc_iot, sc->sc_ioh, ITE8181_CLASS);
    266 	printf("ITE8181 Rev.%02lx", regval & ITE8181_REV_MASK);
    267 	if (console) {
    268 		printf(", console");
    269 	}
    270 	printf("\n");
    271 	printf("%s: framebuffer address: 0x%08lx\n",
    272 		sc->sc_dev.dv_xname, (u_long)bootinfo->fb_addr);
    273 	if (ite8181_lcd_control_disable)
    274 		printf("%s: ite8181 lcd coontrol is DISABLED.\n",
    275 			sc->sc_dev.dv_xname);
    276 
    277 	/* set base offsets */
    278 	sc->sc_mba = ite8181_config_read_4(sc->sc_iot, sc->sc_ioh, ITE8181_MBA);
    279 	DPRINTFN(1, ("ite8181: Memory base offset %08x\n", sc->sc_mba));
    280 	sc->sc_gba = ite8181_config_read_4(sc->sc_iot, sc->sc_ioh, ITE8181_GBA);
    281 	DPRINTFN(1, ("ite8181: GUI base offset %08x\n", sc->sc_gba));
    282 	sc->sc_sba = ite8181_config_read_4(sc->sc_iot, sc->sc_ioh, ITE8181_SBA);
    283 	DPRINTFN(1, ("ite8181: Graphics base offset %08x\n", sc->sc_sba));
    284 
    285 	/* assume lcd is on */
    286 	sc->sc_lcd = 1;
    287 	/* erase wince cursor */
    288 	ite8181_erase_cursor(sc);
    289 
    290 	/* Add a power hook to power saving */
    291 	sc->sc_powerhook = powerhook_establish(ite8181_power, sc);
    292 	if (sc->sc_powerhook == NULL)
    293 		printf("%s: WARNING: unable to establish power hook\n",
    294 			sc->sc_dev.dv_xname);
    295 
    296 	/* Add a hard power hook to power saving */
    297 	sc->sc_hardpowerhook = config_hook(CONFIG_HOOK_PMEVENT,
    298 					   CONFIG_HOOK_PMEVENT_HARDPOWER,
    299 					   CONFIG_HOOK_SHARE,
    300 					   ite8181_hardpower, sc);
    301 	if (sc->sc_hardpowerhook == NULL)
    302 		printf("%s: WARNING: unable to establish hard power hook\n",
    303 			sc->sc_dev.dv_xname);
    304 
    305 	/* initialize backlight brightness and lcd contrast */
    306 	sc->sc_lcd_inited = 0;
    307 	ite8181_init_brightness(sc, 1);
    308 	ite8181_init_contrast(sc, 1);
    309 	ite8181_init_backlight(sc, 1);
    310 
    311 	if (console && hpcfb_cnattach(&sc->sc_fbconf) != 0) {
    312 		panic("ite8181_attach: can't init fb console");
    313 	}
    314 
    315 	ha.ha_console = console;
    316 	ha.ha_accessops = &ite8181_ha;
    317 	ha.ha_accessctx = sc;
    318 	ha.ha_curfbconf = 0;
    319 	ha.ha_nfbconf = 1;
    320 	ha.ha_fbconflist = &sc->sc_fbconf;
    321 	ha.ha_curdspconf = 0;
    322 	ha.ha_ndspconf = 1;
    323 	ha.ha_dspconflist = &sc->sc_dspconf;
    324 
    325 	config_found(&sc->sc_dev, &ha, hpcfbprint);
    326 
    327 #if NBIVIDEO > 0
    328 	/*
    329 	 * bivideo is no longer need
    330 	 */
    331 	bivideo_dont_attach = 1;
    332 #endif /* NBIVIDEO > 0 */
    333 }
    334 
    335 int ite8181_lcd_power(sc, on)
    336 	struct ite8181_softc *sc;
    337 	int on;
    338 {
    339 	int lcd_p;
    340 	int lcd_s;
    341 	int lcd_seq;
    342 	int loop = 10;
    343 
    344 	if (ite8181_lcd_control_disable) {
    345 		VPRINTF(("ite8171_lcd_control_disable!: %s\n", on?"on":"off"));
    346 		return 0;
    347 	}
    348 
    349 	if (sc->sc_lcd != on) {
    350 		ite8181_ema_write_1(sc, ITE8181_EMA_ENABLEEMA,
    351 			ITE8181_EMA_ENABLEPASS);
    352 		lcd_p = ite8181_ema_read_1(sc, ITE8181_EMA_LCDPOWER);
    353 		lcd_s = ite8181_ema_read_1(sc, ITE8181_EMA_LCDPOWERSTAT);
    354 		lcd_seq = ite8181_ema_read_1(sc, ITE8181_EMA_LCDPOWERSEQ);
    355 		DPRINTFN(1,("ite8181_lcd_power(%d)< p=%x, s=%x, seq=%x\n",
    356 			on,
    357 			lcd_p, lcd_s, lcd_seq));
    358 		if (on) {
    359 			sc->sc_lcd = 1;
    360 			lcd_seq |= (ITE8181_PUP0|ITE8181_PUP1|ITE8181_PUP2);
    361 			ite8181_ema_write_1(sc, ITE8181_EMA_LCDPOWERSEQ, lcd_seq);
    362 			lcd_p &= ~ITE8181_LCDSTANDBY;
    363 			ite8181_ema_write_1(sc, ITE8181_EMA_LCDPOWER, lcd_p);
    364 			/*
    365 			 * XXX:
    366 			 * IBM WorkPad z50 power unit has too weak power.
    367 			 * So we must wait too many times to access self device
    368 			 * after LCD panel and BackLight on.
    369 			 * Currently delay is not enough ??? FIXME
    370 			 */
    371 			delay(ite8181_lcd_on_self_delay*MSEC);
    372 			while (loop--) {
    373 				lcd_p = ite8181_ema_read_1(sc, ITE8181_EMA_LCDPOWER);
    374 				lcd_s = ite8181_ema_read_1(sc, ITE8181_EMA_LCDPOWERSTAT);
    375 				lcd_seq = ite8181_ema_read_1(sc, ITE8181_EMA_LCDPOWERSEQ);
    376 				DPRINTFN(1,("ite8181_lcd_power(%d)%d| p=%x, s=%x, seq=%x\n",
    377 					on, loop,
    378 					lcd_p, lcd_s, lcd_seq));
    379 				/* XXX the states which are not described in manual.*/
    380 				if (!(lcd_s&ITE8181_LCDPSTANDBY) &&
    381 					!(lcd_s&ITE8181_LCDPUP) &&
    382 					(lcd_s&ITE8181_LCDPON))
    383 					break;
    384 				delay(100);
    385 			}
    386 			lcd_s |= ITE8181_PPTOBEON;
    387 			ite8181_ema_write_1(sc, ITE8181_EMA_LCDPOWERSTAT, lcd_s);
    388 		} else {
    389 			sc->sc_lcd = 0;
    390 			lcd_p |= ITE8181_LCDSTANDBY;
    391 			ite8181_ema_write_1(sc, ITE8181_EMA_LCDPOWER, lcd_p);
    392 			while (loop--) {
    393 				lcd_p = ite8181_ema_read_1(sc, ITE8181_EMA_LCDPOWER);
    394 				lcd_s = ite8181_ema_read_1(sc, ITE8181_EMA_LCDPOWERSTAT);
    395 				lcd_seq = ite8181_ema_read_1(sc, ITE8181_EMA_LCDPOWERSEQ);
    396 				DPRINTFN(1,("ite8181_lcd_power(%d)%d| p=%x, s=%x, seq=%x\n",
    397 					on, loop,
    398 					lcd_p, lcd_s, lcd_seq));
    399 				/* XXX the states which are not described in manual.*/
    400 				if ((lcd_s&ITE8181_LCDPSTANDBY) &&
    401 					!(lcd_s&ITE8181_LCDPDOWN) &&
    402 					!(lcd_s&ITE8181_LCDPON))
    403 					break;
    404 				delay(100);
    405 			}
    406 			lcd_s &= ~ITE8181_PPTOBEON;
    407 			ite8181_ema_write_1(sc, ITE8181_EMA_LCDPOWERSTAT, lcd_s);
    408 		}
    409 		DPRINTFN(1,("ite8181_lcd_power(%d)> p=%x, s=%x, seq=%x\n",
    410 			on,
    411 			ite8181_ema_read_1(sc, ITE8181_EMA_LCDPOWER),
    412 			ite8181_ema_read_1(sc, ITE8181_EMA_LCDPOWERSTAT),
    413 			ite8181_ema_read_1(sc, ITE8181_EMA_LCDPOWERSEQ)));
    414 		ite8181_ema_write_1(sc, ITE8181_EMA_ENABLEEMA,
    415 					ITE8181_EMA_DISABLEPASS);
    416 	}
    417 	return 0;
    418 }
    419 
    420 static void
    421 ite8181_erase_cursor(sc)
    422 	struct ite8181_softc *sc;
    423 {
    424 	ite8181_gui_write_1(sc, ITE8181_GUI_C1C, 0); /* Cursor 1 Control Reg. */
    425 	/* other ? */
    426 }
    427 
    428 static void
    429 ite8181_update_powerstate(sc, updates)
    430 	struct ite8181_softc *sc;
    431 	int updates;
    432 {
    433 	if (updates & PWRSTAT_LCD)
    434 		config_hook_call(CONFIG_HOOK_POWERCONTROL,
    435 		    CONFIG_HOOK_POWERCONTROL_LCD,
    436 		    (void*)!(sc->sc_powerstate &
    437 				(PWRSTAT_VIDEOOFF|PWRSTAT_SUSPEND)));
    438 
    439 	if (updates & PWRSTAT_BACKLIGHT)
    440 		config_hook_call(CONFIG_HOOK_POWERCONTROL,
    441 		    CONFIG_HOOK_POWERCONTROL_LCDLIGHT,
    442 		    (void*)(!(sc->sc_powerstate &
    443 				(PWRSTAT_VIDEOOFF|PWRSTAT_SUSPEND)) &&
    444 			     (sc->sc_powerstate & PWRSTAT_BACKLIGHT)));
    445 }
    446 
    447 static void
    448 ite8181_power(why, arg)
    449 	int why;
    450 	void *arg;
    451 {
    452         struct ite8181_softc *sc = arg;
    453 
    454 	switch (why) {
    455 	case PWR_STANDBY:
    456 		sc->sc_powerstate |= PWRSTAT_SUSPEND;
    457 		ite8181_update_powerstate(sc, PWRSTAT_ALL);
    458 		break;
    459 	case PWR_SUSPEND:
    460 		sc->sc_powerstate |= PWRSTAT_SUSPEND;
    461 		ite8181_update_powerstate(sc, PWRSTAT_ALL);
    462 		break;
    463 	case PWR_RESUME:
    464 		sc->sc_powerstate &= ~PWRSTAT_SUSPEND;
    465 		ite8181_update_powerstate(sc, PWRSTAT_ALL);
    466 		break;
    467 	}
    468 }
    469 
    470 static int
    471 ite8181_hardpower(ctx, type, id, msg)
    472 	void *ctx;
    473 	int type;
    474 	long id;
    475 	void *msg;
    476 {
    477 	struct ite8181_softc *sc = ctx;
    478 	int why = (int)msg;
    479 
    480 	switch (why) {
    481 	case PWR_STANDBY:
    482 		/* ite8181_lcd_power(sc, 0); */
    483 		delay(MSEC);
    484 		break;
    485 	case PWR_SUSPEND:
    486 		ite8181_lcd_power(sc, 0);
    487 		delay(MSEC);
    488 		break;
    489 	case PWR_RESUME:
    490 		delay(MSEC);
    491 		ite8181_lcd_power(sc, 1);
    492 		/*
    493 		 * XXX:
    494 		 * IBM WorkPad z50 power unit has too weak power.
    495 		 * So we must wait too many times to access other devices
    496 		 * after LCD panel and BackLight on.
    497 		 */
    498 		delay(ite8181_lcd_on_delay*MSEC);
    499 		break;
    500 	}
    501 
    502 	/*
    503 	 * you should wait until the
    504 	 * power state transit sequence will end.
    505 	 */
    506 
    507 	return (0);
    508 }
    509 
    510 static int
    511 ite8181_fbinit(fb)
    512 	struct hpcfb_fbconf *fb;
    513 {
    514 
    515 	/*
    516 	 * get fb settings from bootinfo
    517 	 */
    518 	if (bootinfo == NULL ||
    519 	    bootinfo->fb_addr == 0 ||
    520 	    bootinfo->fb_line_bytes == 0 ||
    521 	    bootinfo->fb_width == 0 ||
    522 	    bootinfo->fb_height == 0) {
    523 		printf("no frame buffer information.\n");
    524 		return (-1);
    525 	}
    526 
    527 	/* zero fill */
    528 	bzero(fb, sizeof(*fb));
    529 
    530 	fb->hf_conf_index	= 0;	/* configuration index		*/
    531 	fb->hf_nconfs		= 1;   	/* how many configurations	*/
    532 	strcpy(fb->hf_name, "built-in video");
    533 					/* frame buffer name		*/
    534 	strcpy(fb->hf_conf_name, "default");
    535 					/* configuration name		*/
    536 	fb->hf_height		= bootinfo->fb_height;
    537 	fb->hf_width		= bootinfo->fb_width;
    538 	fb->hf_baseaddr		= (u_long)bootinfo->fb_addr;
    539 	fb->hf_offset		= (u_long)bootinfo->fb_addr -
    540 				     mips_ptob(mips_btop(bootinfo->fb_addr));
    541 					/* frame buffer start offset   	*/
    542 	fb->hf_bytes_per_line	= bootinfo->fb_line_bytes;
    543 	fb->hf_nplanes		= 1;
    544 	fb->hf_bytes_per_plane	= bootinfo->fb_height *
    545 					bootinfo->fb_line_bytes;
    546 
    547 	fb->hf_access_flags |= HPCFB_ACCESS_BYTE;
    548 	fb->hf_access_flags |= HPCFB_ACCESS_WORD;
    549 	fb->hf_access_flags |= HPCFB_ACCESS_DWORD;
    550 
    551 	switch (bootinfo->fb_type) {
    552 		/*
    553 		 * gray scale
    554 		 */
    555 	case BIFB_D2_M2L_3:
    556 	case BIFB_D2_M2L_3x2:
    557 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
    558 		/* fall through */
    559 	case BIFB_D2_M2L_0:
    560 	case BIFB_D2_M2L_0x2:
    561 		fb->hf_class = HPCFB_CLASS_GRAYSCALE;
    562 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
    563 		fb->hf_pack_width = 8;
    564 		fb->hf_pixels_per_pack = 4;
    565 		fb->hf_pixel_width = 2;
    566 		fb->hf_class_data_length = sizeof(struct hf_gray_tag);
    567 		fb->hf_u.hf_gray.hf_flags = 0;	/* reserved for future use */
    568 		break;
    569 
    570 	case BIFB_D4_M2L_F:
    571 	case BIFB_D4_M2L_Fx2:
    572 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
    573 		/* fall through */
    574 	case BIFB_D4_M2L_0:
    575 	case BIFB_D4_M2L_0x2:
    576 		fb->hf_class = HPCFB_CLASS_GRAYSCALE;
    577 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
    578 		fb->hf_pack_width = 8;
    579 		fb->hf_pixels_per_pack = 2;
    580 		fb->hf_pixel_width = 4;
    581 		fb->hf_class_data_length = sizeof(struct hf_gray_tag);
    582 		fb->hf_u.hf_gray.hf_flags = 0;	/* reserved for future use */
    583 		break;
    584 
    585 		/*
    586 		 * indexed color
    587 		 */
    588 	case BIFB_D8_FF:
    589 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
    590 		/* fall through */
    591 	case BIFB_D8_00:
    592 		fb->hf_class = HPCFB_CLASS_INDEXCOLOR;
    593 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
    594 		fb->hf_pack_width = 8;
    595 		fb->hf_pixels_per_pack = 1;
    596 		fb->hf_pixel_width = 8;
    597 		fb->hf_class_data_length = sizeof(struct hf_indexed_tag);
    598 		fb->hf_u.hf_indexed.hf_flags = 0; /* reserved for future use */
    599 		break;
    600 
    601 		/*
    602 		 * RGB color
    603 		 */
    604 	case BIFB_D16_FFFF:
    605 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
    606 		/* fall through */
    607 	case BIFB_D16_0000:
    608 		fb->hf_class = HPCFB_CLASS_RGBCOLOR;
    609 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
    610 #if BYTE_ORDER == BIG_ENDIAN /* XXXX */
    611 		fb->hf_swap_flags = HPCFB_SWAP_BYTE;
    612 #endif
    613 		fb->hf_pack_width = 16;
    614 		fb->hf_pixels_per_pack = 1;
    615 		fb->hf_pixel_width = 16;
    616 
    617 		fb->hf_class_data_length = sizeof(struct hf_rgb_tag);
    618 		fb->hf_u.hf_rgb.hf_flags = 0;	/* reserved for future use */
    619 
    620 		fb->hf_u.hf_rgb.hf_red_width = 5;
    621 		fb->hf_u.hf_rgb.hf_red_shift = 11;
    622 		fb->hf_u.hf_rgb.hf_green_width = 6;
    623 		fb->hf_u.hf_rgb.hf_green_shift = 5;
    624 		fb->hf_u.hf_rgb.hf_blue_width = 5;
    625 		fb->hf_u.hf_rgb.hf_blue_shift = 0;
    626 		fb->hf_u.hf_rgb.hf_alpha_width = 0;
    627 		fb->hf_u.hf_rgb.hf_alpha_shift = 0;
    628 		break;
    629 
    630 	default:
    631 		printf("unknown type (=%d).\n", bootinfo->fb_type);
    632 		return (-1);
    633 		break;
    634 	}
    635 
    636 	return (0); /* no error */
    637 }
    638 
    639 int
    640 ite8181_ioctl(v, cmd, data, flag, p)
    641 	void *v;
    642 	u_long cmd;
    643 	caddr_t data;
    644 	int flag;
    645 	struct proc *p;
    646 {
    647 	struct ite8181_softc *sc = (struct ite8181_softc *)v;
    648 	struct hpcfb_fbconf *fbconf;
    649 	struct hpcfb_dspconf *dspconf;
    650 	struct wsdisplay_cmap *cmap;
    651 	struct wsdisplay_param *dispparam;
    652 
    653 	switch (cmd) {
    654 	case WSDISPLAYIO_GETCMAP:
    655 		cmap = (struct wsdisplay_cmap*)data;
    656 
    657 		if (sc->sc_fbconf.hf_class != HPCFB_CLASS_INDEXCOLOR ||
    658 		    sc->sc_fbconf.hf_pack_width != 8 ||
    659 		    256 <= cmap->index ||
    660 		    256 < (cmap->index + cmap->count))
    661 			return (EINVAL);
    662 
    663 		if (!uvm_useracc(cmap->red, cmap->count, B_WRITE) ||
    664 		    !uvm_useracc(cmap->green, cmap->count, B_WRITE) ||
    665 		    !uvm_useracc(cmap->blue, cmap->count, B_WRITE))
    666 			return (EFAULT);
    667 
    668 #ifdef ITE8181_WINCE_CMAP
    669 		copyout(&bivideo_cmap_r[cmap->index], cmap->red, cmap->count);
    670 		copyout(&bivideo_cmap_g[cmap->index], cmap->green,cmap->count);
    671 		copyout(&bivideo_cmap_b[cmap->index], cmap->blue, cmap->count);
    672 		return (0);
    673 #else /* ITE8181_WINCE_CMAP */
    674 		return EINVAL;
    675 #endif /* ITE8181_WINCE_CMAP */
    676 
    677 	case WSDISPLAYIO_PUTCMAP:
    678 		/*
    679 		 * This driver can't set color map.
    680 		 */
    681 		return (EINVAL);
    682 
    683 	case WSDISPLAYIO_SVIDEO:
    684 		if (*(int *)data == WSDISPLAYIO_VIDEO_OFF)
    685 			sc->sc_powerstate |= PWRSTAT_VIDEOOFF;
    686 		else
    687 			sc->sc_powerstate &= ~PWRSTAT_VIDEOOFF;
    688 		ite8181_update_powerstate(sc, PWRSTAT_ALL);
    689 		return 0;
    690 
    691 	case WSDISPLAYIO_GVIDEO:
    692 		*(int *)data = (sc->sc_powerstate&PWRSTAT_VIDEOOFF) ?
    693 				WSDISPLAYIO_VIDEO_OFF:WSDISPLAYIO_VIDEO_ON;
    694 		return 0;
    695 
    696 
    697 	case WSDISPLAYIO_GETPARAM:
    698 		dispparam = (struct wsdisplay_param*)data;
    699 		switch (dispparam->param) {
    700 		case WSDISPLAYIO_PARAM_BACKLIGHT:
    701 			VPRINTF(("ite8181_ioctl: GET:BACKLIGHT\n"));
    702 			ite8181_init_brightness(sc, 0);
    703 			ite8181_init_backlight(sc, 0);
    704 			VPRINTF(("ite8181_ioctl: GET:(real)BACKLIGHT %d\n",
    705 				 (sc->sc_powerstate&PWRSTAT_BACKLIGHT)? 1: 0));
    706 			dispparam->min = 0;
    707 			dispparam->max = 1;
    708 			if (sc->sc_max_brightness > 0)
    709 				dispparam->curval = sc->sc_brightness > 0? 1: 0;
    710 			else
    711 				dispparam->curval =
    712 				    (sc->sc_powerstate&PWRSTAT_BACKLIGHT) ? 1: 0;
    713 			VPRINTF(("ite8181_ioctl: GET:BACKLIGHT:%d(%s)\n",
    714 				dispparam->curval,
    715 				sc->sc_max_brightness > 0? "brightness": "light"));
    716 			return 0;
    717 			break;
    718 		case WSDISPLAYIO_PARAM_CONTRAST:
    719 			VPRINTF(("ite8181_ioctl: GET:CONTRAST\n"));
    720 			ite8181_init_contrast(sc, 0);
    721 			if (sc->sc_max_contrast > 0) {
    722 				dispparam->min = 0;
    723 				dispparam->max = sc->sc_max_contrast;
    724 				dispparam->curval = sc->sc_contrast;
    725 				VPRINTF(("ite8181_ioctl: GET:CONTRAST max=%d, current=%d\n", sc->sc_max_contrast, sc->sc_contrast));
    726 				return 0;
    727 			} else {
    728 				VPRINTF(("ite8181_ioctl: GET:CONTRAST EINVAL\n"));
    729 				return (EINVAL);
    730 			}
    731 			break;
    732 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
    733 			VPRINTF(("ite8181_ioctl: GET:BRIGHTNESS\n"));
    734 			ite8181_init_brightness(sc, 0);
    735 			if (sc->sc_max_brightness > 0) {
    736 				dispparam->min = 0;
    737 				dispparam->max = sc->sc_max_brightness;
    738 				dispparam->curval = sc->sc_brightness;
    739 				VPRINTF(("ite8181_ioctl: GET:BRIGHTNESS max=%d, current=%d\n", sc->sc_max_brightness, sc->sc_brightness));
    740 				return 0;
    741 			} else {
    742 				VPRINTF(("ite8181_ioctl: GET:BRIGHTNESS EINVAL\n"));
    743 				return (EINVAL);
    744 			}
    745 			return (EINVAL);
    746 		default:
    747 			return (EINVAL);
    748 		}
    749 		return (0);
    750 
    751 	case WSDISPLAYIO_SETPARAM:
    752 		dispparam = (struct wsdisplay_param*)data;
    753 		switch (dispparam->param) {
    754 		case WSDISPLAYIO_PARAM_BACKLIGHT:
    755 			VPRINTF(("ite8181_ioctl: SET:BACKLIGHT\n"));
    756 			if (dispparam->curval < 0 ||
    757 			    1 < dispparam->curval)
    758 				return (EINVAL);
    759 			ite8181_init_brightness(sc, 0);
    760 			VPRINTF(("ite8181_ioctl: SET:max brightness=%d\n", sc->sc_max_brightness));
    761 			if (sc->sc_max_brightness > 0) { /* dimmer */
    762 				if (dispparam->curval == 0){
    763 					sc->sc_brightness_save = sc->sc_brightness;
    764 					ite8181_set_brightness(sc, 0);	/* min */
    765 				} else {
    766 					if (sc->sc_brightness_save == 0)
    767 						sc->sc_brightness_save = sc->sc_max_brightness;
    768 					ite8181_set_brightness(sc, sc->sc_brightness_save);
    769 				}
    770 				VPRINTF(("ite8181_ioctl: SET:BACKLIGHT:brightness=%d\n", sc->sc_brightness));
    771 			} else { /* off */
    772 				if (dispparam->curval == 0)
    773 					sc->sc_powerstate &= ~PWRSTAT_BACKLIGHT;
    774 				else
    775 					sc->sc_powerstate |= PWRSTAT_BACKLIGHT;
    776 				VPRINTF(("ite8181_ioctl: SET:BACKLIGHT:powerstate %d\n",
    777 						(sc->sc_powerstate & PWRSTAT_BACKLIGHT)?1:0));
    778 				ite8181_update_powerstate(sc, PWRSTAT_BACKLIGHT);
    779 				VPRINTF(("ite8181_ioctl: SET:BACKLIGHT:%d\n",
    780 					(sc->sc_powerstate & PWRSTAT_BACKLIGHT)?1:0));
    781 			}
    782 			return 0;
    783 			break;
    784 		case WSDISPLAYIO_PARAM_CONTRAST:
    785 			VPRINTF(("ite8181_ioctl: SET:CONTRAST\n"));
    786 			ite8181_init_contrast(sc, 0);
    787 			if (dispparam->curval < 0 ||
    788 			    sc->sc_max_contrast < dispparam->curval)
    789 				return (EINVAL);
    790 			if (sc->sc_max_contrast > 0) {
    791 				int org = sc->sc_contrast;
    792 				ite8181_set_contrast(sc, dispparam->curval);
    793 				VPRINTF(("ite8181_ioctl: SET:CONTRAST org=%d, current=%d\n", org, sc->sc_contrast));
    794 				return 0;
    795 			} else {
    796 				VPRINTF(("ite8181_ioctl: SET:CONTRAST EINVAL\n"));
    797 				return (EINVAL);
    798 			}
    799 			break;
    800 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
    801 			VPRINTF(("ite8181_ioctl: SET:BRIGHTNESS\n"));
    802 			ite8181_init_brightness(sc, 0);
    803 			if (dispparam->curval < 0 ||
    804 			    sc->sc_max_brightness < dispparam->curval)
    805 				return (EINVAL);
    806 			if (sc->sc_max_brightness > 0) {
    807 				int org = sc->sc_brightness;
    808 				ite8181_set_brightness(sc, dispparam->curval);
    809 				VPRINTF(("ite8181_ioctl: SET:BRIGHTNESS org=%d, current=%d\n", org, sc->sc_brightness));
    810 				return 0;
    811 			} else {
    812 				VPRINTF(("ite8181_ioctl: SET:BRIGHTNESS EINVAL\n"));
    813 				return (EINVAL);
    814 			}
    815 			break;
    816 		default:
    817 			return (EINVAL);
    818 		}
    819 		return (0);
    820 
    821 	case HPCFBIO_GCONF:
    822 		fbconf = (struct hpcfb_fbconf *)data;
    823 		if (fbconf->hf_conf_index != 0 &&
    824 		    fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
    825 			return (EINVAL);
    826 		}
    827 		*fbconf = sc->sc_fbconf;	/* structure assignment */
    828 		return (0);
    829 	case HPCFBIO_SCONF:
    830 		fbconf = (struct hpcfb_fbconf *)data;
    831 		if (fbconf->hf_conf_index != 0 &&
    832 		    fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
    833 			return (EINVAL);
    834 		}
    835 		/*
    836 		 * nothing to do because we have only one configration
    837 		 */
    838 		return (0);
    839 	case HPCFBIO_GDSPCONF:
    840 		dspconf = (struct hpcfb_dspconf *)data;
    841 		if ((dspconf->hd_unit_index != 0 &&
    842 		     dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
    843 		    (dspconf->hd_conf_index != 0 &&
    844 		     dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
    845 			return (EINVAL);
    846 		}
    847 		*dspconf = sc->sc_dspconf;	/* structure assignment */
    848 		return (0);
    849 	case HPCFBIO_SDSPCONF:
    850 		dspconf = (struct hpcfb_dspconf *)data;
    851 		if ((dspconf->hd_unit_index != 0 &&
    852 		     dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
    853 		    (dspconf->hd_conf_index != 0 &&
    854 		     dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
    855 			return (EINVAL);
    856 		}
    857 		/*
    858 		 * nothing to do
    859 		 * because we have only one unit and one configration
    860 		 */
    861 		return (0);
    862 	case HPCFBIO_GOP:
    863 	case HPCFBIO_SOP:
    864 		/*
    865 		 * curently not implemented...
    866 		 */
    867 		return (EINVAL);
    868 	}
    869 
    870 	return (ENOTTY);
    871 }
    872 
    873 paddr_t
    874 ite8181_mmap(ctx, offset, prot)
    875 	void *ctx;
    876 	off_t offset;
    877 	int prot;
    878 {
    879 	struct ite8181_softc *sc = (struct ite8181_softc *)ctx;
    880 
    881 	if (offset < 0 ||
    882 	    (sc->sc_fbconf.hf_bytes_per_plane +
    883 		sc->sc_fbconf.hf_offset) <  offset)
    884 		return -1;
    885 
    886 	return mips_btop((u_long)bootinfo->fb_addr + offset);
    887 }
    888 
    889 
    890 void
    891 ite8181_init_backlight(sc, inattach)
    892 	struct ite8181_softc *sc;
    893 	int inattach;
    894 {
    895 	int val = -1;
    896 
    897 	if (sc->sc_lcd_inited&BACKLIGHT_INITED)
    898 		return;
    899 
    900 	if (config_hook_call(CONFIG_HOOK_GET,
    901 	     CONFIG_HOOK_POWER_LCDLIGHT, &val) != -1) {
    902 		/* we can get real light state */
    903 		VPRINTF(("ite8181_init_backlight: real backlight=%d\n", val));
    904 		if (val == 0)
    905 			sc->sc_powerstate &= ~PWRSTAT_BACKLIGHT;
    906 		else
    907 			sc->sc_powerstate |= PWRSTAT_BACKLIGHT;
    908 		sc->sc_lcd_inited |= BACKLIGHT_INITED;
    909 	} else if (inattach) {
    910 		/*
    911 		   we cannot get real light state in attach time
    912 		   because light device not yet attached.
    913 		   we will retry in !inattach.
    914 		   temporary assume light is on.
    915 		 */
    916 		sc->sc_powerstate |= PWRSTAT_BACKLIGHT;
    917 	} else {
    918 		/* we cannot get real light state, so work by myself state */
    919 		sc->sc_lcd_inited |= BACKLIGHT_INITED;
    920 	}
    921 }
    922 
    923 void
    924 ite8181_init_brightness(sc, inattach)
    925 	struct ite8181_softc *sc;
    926 	int inattach;
    927 {
    928 	int val = -1;
    929 
    930 	if (sc->sc_lcd_inited&BRIGHTNESS_INITED)
    931 		return;
    932 
    933 	VPRINTF(("ite8181_init_brightness\n"));
    934 	if (config_hook_call(CONFIG_HOOK_GET,
    935 	     CONFIG_HOOK_BRIGHTNESS_MAX, &val) != -1) {
    936 		/* we can get real brightness max */
    937 		VPRINTF(("ite8181_init_brightness: real brightness max=%d\n", val));
    938 		sc->sc_max_brightness = val;
    939 		val = -1;
    940 		if (config_hook_call(CONFIG_HOOK_GET,
    941 		     CONFIG_HOOK_BRIGHTNESS, &val) != -1) {
    942 			/* we can get real brightness */
    943 			VPRINTF(("ite8181_init_brightness: real brightness=%d\n", val));
    944 			sc->sc_brightness_save = sc->sc_brightness = val;
    945 		} else {
    946 			sc->sc_brightness_save =
    947 			sc->sc_brightness = sc->sc_max_brightness;
    948 		}
    949 		sc->sc_lcd_inited |= BRIGHTNESS_INITED;
    950 	} else if (inattach) {
    951 		/*
    952 		   we cannot get real brightness in attach time
    953 		   because brightness device not yet attached.
    954 		   we will retry in !inattach.
    955 		 */
    956 		sc->sc_max_brightness = -1;
    957 		sc->sc_brightness = -1;
    958 		sc->sc_brightness_save = -1;
    959 	} else {
    960 		/* we cannot get real brightness */
    961 		sc->sc_lcd_inited |= BRIGHTNESS_INITED;
    962 	}
    963 
    964 	return;
    965 }
    966 
    967 void
    968 ite8181_init_contrast(sc, inattach)
    969 	struct ite8181_softc *sc;
    970 	int inattach;
    971 {
    972 	int val = -1;
    973 
    974 	if (sc->sc_lcd_inited&CONTRAST_INITED)
    975 		return;
    976 
    977 	VPRINTF(("ite8181_init_contrast\n"));
    978 	if (config_hook_call(CONFIG_HOOK_GET,
    979 	     CONFIG_HOOK_CONTRAST_MAX, &val) != -1) {
    980 		/* we can get real contrast max */
    981 		VPRINTF(("ite8181_init_contrast: real contrast max=%d\n", val));
    982 		sc->sc_max_contrast = val;
    983 		val = -1;
    984 		if (config_hook_call(CONFIG_HOOK_GET,
    985 		     CONFIG_HOOK_CONTRAST, &val) != -1) {
    986 			/* we can get real contrast */
    987 			VPRINTF(("ite8181_init_contrast: real contrast=%d\n", val));
    988 			sc->sc_contrast = val;
    989 		} else {
    990 			sc->sc_contrast = sc->sc_max_contrast;
    991 		}
    992 		sc->sc_lcd_inited |= CONTRAST_INITED;
    993 	} else if (inattach) {
    994 		/*
    995 		   we cannot get real contrast in attach time
    996 		   because contrast device not yet attached.
    997 		   we will retry in !inattach.
    998 		 */
    999 		sc->sc_max_contrast = -1;
   1000 		sc->sc_contrast = -1;
   1001 	} else {
   1002 		/* we cannot get real contrast */
   1003 		sc->sc_lcd_inited |= CONTRAST_INITED;
   1004 	}
   1005 
   1006 	return;
   1007 }
   1008 
   1009 
   1010 void
   1011 ite8181_set_brightness(sc, val)
   1012 	struct ite8181_softc *sc;
   1013 	int val;
   1014 {
   1015 	sc->sc_brightness = val;
   1016 
   1017 	config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_BRIGHTNESS, &val);
   1018 	if (config_hook_call(CONFIG_HOOK_GET,
   1019 	     CONFIG_HOOK_BRIGHTNESS, &val) != -1) {
   1020 		sc->sc_brightness = val;
   1021 	}
   1022 }
   1023 
   1024 void
   1025 ite8181_set_contrast(sc, val)
   1026 	struct ite8181_softc *sc;
   1027 	int val;
   1028 {
   1029 	sc->sc_contrast = val;
   1030 
   1031 	config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_CONTRAST, &val);
   1032 	if (config_hook_call(CONFIG_HOOK_GET,
   1033 	     CONFIG_HOOK_CONTRAST, &val) != -1) {
   1034 		sc->sc_contrast = val;
   1035 	}
   1036 }
   1037