xref: /netbsd-src/sys/arch/hpcmips/dev/ite8181.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: ite8181.c,v 1.14 2001/07/22 09:56:41 takemura 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 		fb->hf_order_flags = HPCFB_REVORDER_BYTE;
611 		fb->hf_pack_width = 16;
612 		fb->hf_pixels_per_pack = 1;
613 		fb->hf_pixel_width = 16;
614 
615 		fb->hf_class_data_length = sizeof(struct hf_rgb_tag);
616 		fb->hf_u.hf_rgb.hf_flags = 0;	/* reserved for future use */
617 
618 		fb->hf_u.hf_rgb.hf_red_width = 5;
619 		fb->hf_u.hf_rgb.hf_red_shift = 11;
620 		fb->hf_u.hf_rgb.hf_green_width = 6;
621 		fb->hf_u.hf_rgb.hf_green_shift = 5;
622 		fb->hf_u.hf_rgb.hf_blue_width = 5;
623 		fb->hf_u.hf_rgb.hf_blue_shift = 0;
624 		fb->hf_u.hf_rgb.hf_alpha_width = 0;
625 		fb->hf_u.hf_rgb.hf_alpha_shift = 0;
626 		break;
627 
628 	default:
629 		printf("unknown type (=%d).\n", bootinfo->fb_type);
630 		return (-1);
631 		break;
632 	}
633 
634 	return (0); /* no error */
635 }
636 
637 int
638 ite8181_ioctl(v, cmd, data, flag, p)
639 	void *v;
640 	u_long cmd;
641 	caddr_t data;
642 	int flag;
643 	struct proc *p;
644 {
645 	struct ite8181_softc *sc = (struct ite8181_softc *)v;
646 	struct hpcfb_fbconf *fbconf;
647 	struct hpcfb_dspconf *dspconf;
648 	struct wsdisplay_cmap *cmap;
649 	struct wsdisplay_param *dispparam;
650 
651 	switch (cmd) {
652 	case WSDISPLAYIO_GETCMAP:
653 		cmap = (struct wsdisplay_cmap*)data;
654 
655 		if (sc->sc_fbconf.hf_class != HPCFB_CLASS_INDEXCOLOR ||
656 		    sc->sc_fbconf.hf_pack_width != 8 ||
657 		    256 <= cmap->index ||
658 		    256 < (cmap->index + cmap->count))
659 			return (EINVAL);
660 
661 		if (!uvm_useracc(cmap->red, cmap->count, B_WRITE) ||
662 		    !uvm_useracc(cmap->green, cmap->count, B_WRITE) ||
663 		    !uvm_useracc(cmap->blue, cmap->count, B_WRITE))
664 			return (EFAULT);
665 
666 #ifdef ITE8181_WINCE_CMAP
667 		copyout(&bivideo_cmap_r[cmap->index], cmap->red, cmap->count);
668 		copyout(&bivideo_cmap_g[cmap->index], cmap->green,cmap->count);
669 		copyout(&bivideo_cmap_b[cmap->index], cmap->blue, cmap->count);
670 		return (0);
671 #else /* ITE8181_WINCE_CMAP */
672 		return EINVAL;
673 #endif /* ITE8181_WINCE_CMAP */
674 
675 	case WSDISPLAYIO_PUTCMAP:
676 		/*
677 		 * This driver can't set color map.
678 		 */
679 		return (EINVAL);
680 
681 	case WSDISPLAYIO_SVIDEO:
682 		if (*(int *)data == WSDISPLAYIO_VIDEO_OFF)
683 			sc->sc_powerstate |= PWRSTAT_VIDEOOFF;
684 		else
685 			sc->sc_powerstate &= ~PWRSTAT_VIDEOOFF;
686 		ite8181_update_powerstate(sc, PWRSTAT_ALL);
687 		return 0;
688 
689 	case WSDISPLAYIO_GVIDEO:
690 		*(int *)data = (sc->sc_powerstate&PWRSTAT_VIDEOOFF) ?
691 				WSDISPLAYIO_VIDEO_OFF:WSDISPLAYIO_VIDEO_ON;
692 		return 0;
693 
694 
695 	case WSDISPLAYIO_GETPARAM:
696 		dispparam = (struct wsdisplay_param*)data;
697 		switch (dispparam->param) {
698 		case WSDISPLAYIO_PARAM_BACKLIGHT:
699 			VPRINTF(("ite8181_ioctl: GET:BACKLIGHT\n"));
700 			ite8181_init_brightness(sc, 0);
701 			ite8181_init_backlight(sc, 0);
702 			VPRINTF(("ite8181_ioctl: GET:(real)BACKLIGHT %d\n",
703 				 (sc->sc_powerstate&PWRSTAT_BACKLIGHT)? 1: 0));
704 			dispparam->min = 0;
705 			dispparam->max = 1;
706 			if (sc->sc_max_brightness > 0)
707 				dispparam->curval = sc->sc_brightness > 0? 1: 0;
708 			else
709 				dispparam->curval =
710 				    (sc->sc_powerstate&PWRSTAT_BACKLIGHT) ? 1: 0;
711 			VPRINTF(("ite8181_ioctl: GET:BACKLIGHT:%d(%s)\n",
712 				dispparam->curval,
713 				sc->sc_max_brightness > 0? "brightness": "light"));
714 			return 0;
715 			break;
716 		case WSDISPLAYIO_PARAM_CONTRAST:
717 			VPRINTF(("ite8181_ioctl: GET:CONTRAST\n"));
718 			ite8181_init_contrast(sc, 0);
719 			if (sc->sc_max_contrast > 0) {
720 				dispparam->min = 0;
721 				dispparam->max = sc->sc_max_contrast;
722 				dispparam->curval = sc->sc_contrast;
723 				VPRINTF(("ite8181_ioctl: GET:CONTRAST max=%d, current=%d\n", sc->sc_max_contrast, sc->sc_contrast));
724 				return 0;
725 			} else {
726 				VPRINTF(("ite8181_ioctl: GET:CONTRAST EINVAL\n"));
727 				return (EINVAL);
728 			}
729 			break;
730 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
731 			VPRINTF(("ite8181_ioctl: GET:BRIGHTNESS\n"));
732 			ite8181_init_brightness(sc, 0);
733 			if (sc->sc_max_brightness > 0) {
734 				dispparam->min = 0;
735 				dispparam->max = sc->sc_max_brightness;
736 				dispparam->curval = sc->sc_brightness;
737 				VPRINTF(("ite8181_ioctl: GET:BRIGHTNESS max=%d, current=%d\n", sc->sc_max_brightness, sc->sc_brightness));
738 				return 0;
739 			} else {
740 				VPRINTF(("ite8181_ioctl: GET:BRIGHTNESS EINVAL\n"));
741 				return (EINVAL);
742 			}
743 			return (EINVAL);
744 		default:
745 			return (EINVAL);
746 		}
747 		return (0);
748 
749 	case WSDISPLAYIO_SETPARAM:
750 		dispparam = (struct wsdisplay_param*)data;
751 		switch (dispparam->param) {
752 		case WSDISPLAYIO_PARAM_BACKLIGHT:
753 			VPRINTF(("ite8181_ioctl: SET:BACKLIGHT\n"));
754 			if (dispparam->curval < 0 ||
755 			    1 < dispparam->curval)
756 				return (EINVAL);
757 			ite8181_init_brightness(sc, 0);
758 			VPRINTF(("ite8181_ioctl: SET:max brightness=%d\n", sc->sc_max_brightness));
759 			if (sc->sc_max_brightness > 0) { /* dimmer */
760 				if (dispparam->curval == 0){
761 					sc->sc_brightness_save = sc->sc_brightness;
762 					ite8181_set_brightness(sc, 0);	/* min */
763 				} else {
764 					if (sc->sc_brightness_save == 0)
765 						sc->sc_brightness_save = sc->sc_max_brightness;
766 					ite8181_set_brightness(sc, sc->sc_brightness_save);
767 				}
768 				VPRINTF(("ite8181_ioctl: SET:BACKLIGHT:brightness=%d\n", sc->sc_brightness));
769 			} else { /* off */
770 				if (dispparam->curval == 0)
771 					sc->sc_powerstate &= ~PWRSTAT_BACKLIGHT;
772 				else
773 					sc->sc_powerstate |= PWRSTAT_BACKLIGHT;
774 				VPRINTF(("ite8181_ioctl: SET:BACKLIGHT:powerstate %d\n",
775 						(sc->sc_powerstate & PWRSTAT_BACKLIGHT)?1:0));
776 				ite8181_update_powerstate(sc, PWRSTAT_BACKLIGHT);
777 				VPRINTF(("ite8181_ioctl: SET:BACKLIGHT:%d\n",
778 					(sc->sc_powerstate & PWRSTAT_BACKLIGHT)?1:0));
779 			}
780 			return 0;
781 			break;
782 		case WSDISPLAYIO_PARAM_CONTRAST:
783 			VPRINTF(("ite8181_ioctl: SET:CONTRAST\n"));
784 			ite8181_init_contrast(sc, 0);
785 			if (dispparam->curval < 0 ||
786 			    sc->sc_max_contrast < dispparam->curval)
787 				return (EINVAL);
788 			if (sc->sc_max_contrast > 0) {
789 				int org = sc->sc_contrast;
790 				ite8181_set_contrast(sc, dispparam->curval);
791 				VPRINTF(("ite8181_ioctl: SET:CONTRAST org=%d, current=%d\n", org, sc->sc_contrast));
792 				return 0;
793 			} else {
794 				VPRINTF(("ite8181_ioctl: SET:CONTRAST EINVAL\n"));
795 				return (EINVAL);
796 			}
797 			break;
798 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
799 			VPRINTF(("ite8181_ioctl: SET:BRIGHTNESS\n"));
800 			ite8181_init_brightness(sc, 0);
801 			if (dispparam->curval < 0 ||
802 			    sc->sc_max_brightness < dispparam->curval)
803 				return (EINVAL);
804 			if (sc->sc_max_brightness > 0) {
805 				int org = sc->sc_brightness;
806 				ite8181_set_brightness(sc, dispparam->curval);
807 				VPRINTF(("ite8181_ioctl: SET:BRIGHTNESS org=%d, current=%d\n", org, sc->sc_brightness));
808 				return 0;
809 			} else {
810 				VPRINTF(("ite8181_ioctl: SET:BRIGHTNESS EINVAL\n"));
811 				return (EINVAL);
812 			}
813 			break;
814 		default:
815 			return (EINVAL);
816 		}
817 		return (0);
818 
819 	case HPCFBIO_GCONF:
820 		fbconf = (struct hpcfb_fbconf *)data;
821 		if (fbconf->hf_conf_index != 0 &&
822 		    fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
823 			return (EINVAL);
824 		}
825 		*fbconf = sc->sc_fbconf;	/* structure assignment */
826 		return (0);
827 	case HPCFBIO_SCONF:
828 		fbconf = (struct hpcfb_fbconf *)data;
829 		if (fbconf->hf_conf_index != 0 &&
830 		    fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
831 			return (EINVAL);
832 		}
833 		/*
834 		 * nothing to do because we have only one configration
835 		 */
836 		return (0);
837 	case HPCFBIO_GDSPCONF:
838 		dspconf = (struct hpcfb_dspconf *)data;
839 		if ((dspconf->hd_unit_index != 0 &&
840 		     dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
841 		    (dspconf->hd_conf_index != 0 &&
842 		     dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
843 			return (EINVAL);
844 		}
845 		*dspconf = sc->sc_dspconf;	/* structure assignment */
846 		return (0);
847 	case HPCFBIO_SDSPCONF:
848 		dspconf = (struct hpcfb_dspconf *)data;
849 		if ((dspconf->hd_unit_index != 0 &&
850 		     dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
851 		    (dspconf->hd_conf_index != 0 &&
852 		     dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
853 			return (EINVAL);
854 		}
855 		/*
856 		 * nothing to do
857 		 * because we have only one unit and one configration
858 		 */
859 		return (0);
860 	case HPCFBIO_GOP:
861 	case HPCFBIO_SOP:
862 		/*
863 		 * curently not implemented...
864 		 */
865 		return (EINVAL);
866 	}
867 
868 	return (ENOTTY);
869 }
870 
871 paddr_t
872 ite8181_mmap(ctx, offset, prot)
873 	void *ctx;
874 	off_t offset;
875 	int prot;
876 {
877 	struct ite8181_softc *sc = (struct ite8181_softc *)ctx;
878 
879 	if (offset < 0 ||
880 	    (sc->sc_fbconf.hf_bytes_per_plane +
881 		sc->sc_fbconf.hf_offset) <  offset)
882 		return -1;
883 
884 	return mips_btop((u_long)bootinfo->fb_addr + offset);
885 }
886 
887 
888 void
889 ite8181_init_backlight(sc, inattach)
890 	struct ite8181_softc *sc;
891 	int inattach;
892 {
893 	int val = -1;
894 
895 	if (sc->sc_lcd_inited&BACKLIGHT_INITED)
896 		return;
897 
898 	if (config_hook_call(CONFIG_HOOK_GET,
899 	     CONFIG_HOOK_POWER_LCDLIGHT, &val) != -1) {
900 		/* we can get real light state */
901 		VPRINTF(("ite8181_init_backlight: real backlight=%d\n", val));
902 		if (val == 0)
903 			sc->sc_powerstate &= ~PWRSTAT_BACKLIGHT;
904 		else
905 			sc->sc_powerstate |= PWRSTAT_BACKLIGHT;
906 		sc->sc_lcd_inited |= BACKLIGHT_INITED;
907 	} else if (inattach) {
908 		/*
909 		   we cannot get real light state in attach time
910 		   because light device not yet attached.
911 		   we will retry in !inattach.
912 		   temporary assume light is on.
913 		 */
914 		sc->sc_powerstate |= PWRSTAT_BACKLIGHT;
915 	} else {
916 		/* we cannot get real light state, so work by myself state */
917 		sc->sc_lcd_inited |= BACKLIGHT_INITED;
918 	}
919 }
920 
921 void
922 ite8181_init_brightness(sc, inattach)
923 	struct ite8181_softc *sc;
924 	int inattach;
925 {
926 	int val = -1;
927 
928 	if (sc->sc_lcd_inited&BRIGHTNESS_INITED)
929 		return;
930 
931 	VPRINTF(("ite8181_init_brightness\n"));
932 	if (config_hook_call(CONFIG_HOOK_GET,
933 	     CONFIG_HOOK_BRIGHTNESS_MAX, &val) != -1) {
934 		/* we can get real brightness max */
935 		VPRINTF(("ite8181_init_brightness: real brightness max=%d\n", val));
936 		sc->sc_max_brightness = val;
937 		val = -1;
938 		if (config_hook_call(CONFIG_HOOK_GET,
939 		     CONFIG_HOOK_BRIGHTNESS, &val) != -1) {
940 			/* we can get real brightness */
941 			VPRINTF(("ite8181_init_brightness: real brightness=%d\n", val));
942 			sc->sc_brightness_save = sc->sc_brightness = val;
943 		} else {
944 			sc->sc_brightness_save =
945 			sc->sc_brightness = sc->sc_max_brightness;
946 		}
947 		sc->sc_lcd_inited |= BRIGHTNESS_INITED;
948 	} else if (inattach) {
949 		/*
950 		   we cannot get real brightness in attach time
951 		   because brightness device not yet attached.
952 		   we will retry in !inattach.
953 		 */
954 		sc->sc_max_brightness = -1;
955 		sc->sc_brightness = -1;
956 		sc->sc_brightness_save = -1;
957 	} else {
958 		/* we cannot get real brightness */
959 		sc->sc_lcd_inited |= BRIGHTNESS_INITED;
960 	}
961 
962 	return;
963 }
964 
965 void
966 ite8181_init_contrast(sc, inattach)
967 	struct ite8181_softc *sc;
968 	int inattach;
969 {
970 	int val = -1;
971 
972 	if (sc->sc_lcd_inited&CONTRAST_INITED)
973 		return;
974 
975 	VPRINTF(("ite8181_init_contrast\n"));
976 	if (config_hook_call(CONFIG_HOOK_GET,
977 	     CONFIG_HOOK_CONTRAST_MAX, &val) != -1) {
978 		/* we can get real contrast max */
979 		VPRINTF(("ite8181_init_contrast: real contrast max=%d\n", val));
980 		sc->sc_max_contrast = val;
981 		val = -1;
982 		if (config_hook_call(CONFIG_HOOK_GET,
983 		     CONFIG_HOOK_CONTRAST, &val) != -1) {
984 			/* we can get real contrast */
985 			VPRINTF(("ite8181_init_contrast: real contrast=%d\n", val));
986 			sc->sc_contrast = val;
987 		} else {
988 			sc->sc_contrast = sc->sc_max_contrast;
989 		}
990 		sc->sc_lcd_inited |= CONTRAST_INITED;
991 	} else if (inattach) {
992 		/*
993 		   we cannot get real contrast in attach time
994 		   because contrast device not yet attached.
995 		   we will retry in !inattach.
996 		 */
997 		sc->sc_max_contrast = -1;
998 		sc->sc_contrast = -1;
999 	} else {
1000 		/* we cannot get real contrast */
1001 		sc->sc_lcd_inited |= CONTRAST_INITED;
1002 	}
1003 
1004 	return;
1005 }
1006 
1007 
1008 void
1009 ite8181_set_brightness(sc, val)
1010 	struct ite8181_softc *sc;
1011 	int val;
1012 {
1013 	sc->sc_brightness = val;
1014 
1015 	config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_BRIGHTNESS, &val);
1016 	if (config_hook_call(CONFIG_HOOK_GET,
1017 	     CONFIG_HOOK_BRIGHTNESS, &val) != -1) {
1018 		sc->sc_brightness = val;
1019 	}
1020 }
1021 
1022 void
1023 ite8181_set_contrast(sc, val)
1024 	struct ite8181_softc *sc;
1025 	int val;
1026 {
1027 	sc->sc_contrast = val;
1028 
1029 	config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_CONTRAST, &val);
1030 	if (config_hook_call(CONFIG_HOOK_GET,
1031 	     CONFIG_HOOK_CONTRAST, &val) != -1) {
1032 		sc->sc_contrast = val;
1033 	}
1034 }
1035