xref: /netbsd-src/sys/arch/hpcmips/dev/mq200.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: mq200.c,v 1.23 2003/11/13 03:09:28 chs Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000, 2001 TAKEMURA Shin
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  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: mq200.c,v 1.23 2003/11/13 03:09:28 chs Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/device.h>
38 #include <sys/systm.h>
39 #include <sys/reboot.h>
40 
41 #include <uvm/uvm_extern.h>
42 
43 #include <dev/wscons/wsconsio.h>
44 
45 #include <machine/bootinfo.h>
46 #include <machine/bus.h>
47 #include <machine/autoconf.h>
48 #include <machine/config_hook.h>
49 #include <machine/platid.h>
50 #include <machine/platid_mask.h>
51 
52 #include "opt_mq200.h"
53 #include <hpcmips/dev/mq200reg.h>
54 #include <hpcmips/dev/mq200var.h>
55 #include <hpcmips/dev/mq200priv.h>
56 
57 #include "bivideo.h"
58 #if NBIVIDEO > 0
59 #include <dev/hpc/bivideovar.h>
60 #endif
61 
62 /*
63  * function prototypes
64  */
65 static void	mq200_power(int, void *);
66 static int	mq200_hardpower(void *, int, long, void *);
67 static int	mq200_fbinit(struct hpcfb_fbconf *);
68 static int	mq200_ioctl(void *, u_long, caddr_t, int, struct proc *);
69 static paddr_t	mq200_mmap(void *, off_t offset, int);
70 static void	mq200_update_powerstate(struct mq200_softc *, int);
71 void	mq200_init_backlight(struct mq200_softc *, int);
72 void	mq200_init_brightness(struct mq200_softc *, int);
73 void	mq200_init_contrast(struct mq200_softc *, int);
74 void	mq200_set_brightness(struct mq200_softc *, int);
75 void	mq200_set_contrast(struct mq200_softc *, int);
76 
77 /*
78  * static variables
79  */
80 struct hpcfb_accessops mq200_ha = {
81 	mq200_ioctl, mq200_mmap
82 };
83 
84 #ifdef MQ200_DEBUG
85 int mq200_debug = MQ200DEBUG_CONF;
86 #endif
87 
88 int
89 mq200_probe(bus_space_tag_t iot, bus_space_handle_t ioh)
90 {
91 	unsigned long regval;
92 
93 #if NBIVIDEO > 0
94 	if (bivideo_dont_attach) /* some video driver already attached */
95 		return (0);
96 #endif /* NBIVIDEO > 0 */
97 
98 	regval = bus_space_read_4(iot, ioh, MQ200_PC00R);
99 	VPRINTF("probe: vendor id=%04lx product id=%04lx\n",
100 	    regval & 0xffff, (regval >> 16) & 0xffff);
101 	if (regval != ((MQ200_PRODUCT_ID << 16) | MQ200_VENDOR_ID))
102 		return (0);
103 
104 	return (1);
105 }
106 
107 void
108 mq200_attach(struct mq200_softc *sc)
109 {
110 	unsigned long regval;
111 	struct hpcfb_attach_args ha;
112 	int console = (bootinfo->bi_cnuse & BI_CNUSE_SERIAL) ? 0 : 1;
113 
114 	printf(": ");
115 	if (mq200_fbinit(&sc->sc_fbconf) != 0) {
116 		/* just return so that hpcfb will not be attached */
117 		return;
118 	}
119 
120 	sc->sc_fbconf.hf_baseaddr = (u_long)bootinfo->fb_addr;
121 	sc->sc_fbconf.hf_offset	= (u_long)sc->sc_fbconf.hf_baseaddr -
122 	    MIPS_PHYS_TO_KSEG1(mips_ptob(mips_btop(sc->sc_baseaddr)));
123 	DPRINTF("hf_baseaddr=%lx\n", sc->sc_fbconf.hf_baseaddr);
124 	DPRINTF("hf_offset=%lx\n", sc->sc_fbconf.hf_offset);
125 
126 	regval = mq200_read(sc, MQ200_PC08R);
127 	printf("MQ200 Rev.%02lx video controller", regval & 0xff);
128 	if (console) {
129 		printf(", console");
130 	}
131 	printf("\n");
132         printf("%s: framebuffer address: 0x%08lx\n",
133 	    sc->sc_dev.dv_xname, (u_long)bootinfo->fb_addr);
134 
135 	/*
136 	 * setup registers
137 	 */
138 	sc->sc_flags = 0;
139 	sc->sc_baseclock = 12288;	/* 12.288 MHz */
140 #ifdef MQ200_DEBUG
141 	if (bootverbose) {
142 		/* dump current setting	*/
143 		mq200_dump_all(sc);
144 		mq200_dump_pll(sc);
145 	}
146 #endif
147 	mq200_setup_regctx(sc);
148 	mq200_mdsetup(sc);
149 	if (sc->sc_md) {
150 		int mode;
151 
152 		switch (sc->sc_fbconf.hf_pixel_width) {
153 		case  1:	mode = MQ200_GCC_1BPP;		break;
154 		case  2:	mode = MQ200_GCC_2BPP;		break;
155 		case  4:	mode = MQ200_GCC_4BPP;		break;
156 		case  8:	mode = MQ200_GCC_8BPP;		break;
157 		case 16:	mode = MQ200_GCC_16BPP_DIRECT;	break;
158 		default:
159 			printf("%s: %dbpp isn't supported\n",
160 			    sc->sc_dev.dv_xname, sc->sc_fbconf.hf_pixel_width);
161 			return;
162 		}
163 
164 		if (sc->sc_md->md_flags & MQ200_MD_HAVEFP) {
165 			sc->sc_flags |= MQ200_SC_GC2_ENABLE;	/* FP	*/
166 		}
167 #if MQ200_USECRT
168 		if (sc->sc_md->md_flags & MQ200_MD_HAVECRT) {
169 			int i;
170 			sc->sc_flags |= MQ200_SC_GC1_ENABLE;	/* CRT	*/
171 			for (i = 0; i < mq200_crt_nparams; i++) {
172 				sc->sc_crt = &mq200_crt_params[i];
173 				if (sc->sc_md->md_fp_width <=
174 				    mq200_crt_params[i].width &&
175 				    sc->sc_md->md_fp_height <=
176 				    mq200_crt_params[i].height)
177 					break;
178 			}
179 		}
180 #endif
181 		mq200_setup(sc);
182 
183 		if (sc->sc_flags & MQ200_SC_GC2_ENABLE)	/* FP	*/
184 			mq200_win_enable(sc, MQ200_GC2, mode,
185 			    sc->sc_fbconf.hf_baseaddr,
186 			    sc->sc_fbconf.hf_width, sc->sc_fbconf.hf_height,
187 			    sc->sc_fbconf.hf_bytes_per_plane);
188 		if (sc->sc_flags & MQ200_SC_GC1_ENABLE)	/* CRT	*/
189 			mq200_win_enable(sc, MQ200_GC1, mode,
190 			    sc->sc_fbconf.hf_baseaddr,
191 			    sc->sc_fbconf.hf_width, sc->sc_fbconf.hf_height,
192 			    sc->sc_fbconf.hf_bytes_per_plane);
193 	}
194 #ifdef MQ200_DEBUG
195 	if (sc->sc_md == NULL || bootverbose) {
196 		mq200_dump_pll(sc);
197 	}
198 #endif
199 
200 	/* Add a power hook to power saving */
201 	sc->sc_mq200pwstate = MQ200_POWERSTATE_D0;
202 	sc->sc_powerhook = powerhook_establish(mq200_power, sc);
203 	if (sc->sc_powerhook == NULL)
204 		printf("%s: WARNING: unable to establish power hook\n",
205 		    sc->sc_dev.dv_xname);
206 
207 	/* Add a hard power hook to power saving */
208 	sc->sc_hardpowerhook = config_hook(CONFIG_HOOK_PMEVENT,
209 	    CONFIG_HOOK_PMEVENT_HARDPOWER,
210 	    CONFIG_HOOK_SHARE,
211 	    mq200_hardpower, sc);
212 	if (sc->sc_hardpowerhook == NULL)
213 		printf("%s: WARNING: unable to establish hard power hook\n",
214 		    sc->sc_dev.dv_xname);
215 
216 	/* initialize backlight brightness and lcd contrast */
217 	sc->sc_lcd_inited = 0;
218 	mq200_init_brightness(sc, 1);
219 	mq200_init_contrast(sc, 1);
220 	mq200_init_backlight(sc, 1);
221 
222 	if (console && hpcfb_cnattach(&sc->sc_fbconf) != 0) {
223 		panic("mq200_attach: can't init fb console");
224 	}
225 
226 	ha.ha_console = console;
227 	ha.ha_accessops = &mq200_ha;
228 	ha.ha_accessctx = sc;
229 	ha.ha_curfbconf = 0;
230 	ha.ha_nfbconf = 1;
231 	ha.ha_fbconflist = &sc->sc_fbconf;
232 	ha.ha_curdspconf = 0;
233 	ha.ha_ndspconf = 1;
234 	ha.ha_dspconflist = &sc->sc_dspconf;
235 
236 	config_found(&sc->sc_dev, &ha, hpcfbprint);
237 
238 #if NBIVIDEO > 0
239 	/*
240 	 * bivideo is no longer need
241 	 */
242 	bivideo_dont_attach = 1;
243 #endif /* NBIVIDEO > 0 */
244 }
245 
246 static void
247 mq200_update_powerstate(struct mq200_softc *sc, int updates)
248 {
249 
250 	if (updates & PWRSTAT_LCD)
251 		config_hook_call(CONFIG_HOOK_POWERCONTROL,
252 		    CONFIG_HOOK_POWERCONTROL_LCD,
253 		    (void*)!(sc->sc_powerstate &
254 			(PWRSTAT_VIDEOOFF|PWRSTAT_SUSPEND)));
255 
256 	if (updates & PWRSTAT_BACKLIGHT)
257 		config_hook_call(CONFIG_HOOK_POWERCONTROL,
258 		    CONFIG_HOOK_POWERCONTROL_LCDLIGHT,
259 		    (void*)(!(sc->sc_powerstate &
260 			(PWRSTAT_VIDEOOFF|PWRSTAT_SUSPEND)) &&
261 			(sc->sc_powerstate & PWRSTAT_BACKLIGHT)));
262 }
263 
264 static void
265 mq200_power(int why, void *arg)
266 {
267 	struct mq200_softc *sc = arg;
268 
269 	switch (why) {
270 	case PWR_SUSPEND:
271 		sc->sc_powerstate |= PWRSTAT_SUSPEND;
272 		mq200_update_powerstate(sc, PWRSTAT_ALL);
273 		break;
274 	case PWR_STANDBY:
275 		sc->sc_powerstate |= PWRSTAT_SUSPEND;
276 		mq200_update_powerstate(sc, PWRSTAT_ALL);
277 		break;
278 	case PWR_RESUME:
279 		sc->sc_powerstate &= ~PWRSTAT_SUSPEND;
280 		mq200_update_powerstate(sc, PWRSTAT_ALL);
281 		break;
282 	}
283 }
284 
285 static int
286 mq200_hardpower(void *ctx, int type, long id, void *msg)
287 {
288 	struct mq200_softc *sc = ctx;
289 	int why = (int)msg;
290 
291 	switch (why) {
292 	case PWR_SUSPEND:
293 		sc->sc_mq200pwstate = MQ200_POWERSTATE_D2;
294 		break;
295 	case PWR_STANDBY:
296 		sc->sc_mq200pwstate = MQ200_POWERSTATE_D3;
297 		break;
298 	case PWR_RESUME:
299 		sc->sc_mq200pwstate = MQ200_POWERSTATE_D0;
300 		break;
301 	}
302 
303 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
304 	    MQ200_PMCSR, sc->sc_mq200pwstate);
305 
306 	/*
307 	 * you should wait until the
308 	 * power state transit sequence will end.
309 	 */
310 	{
311 		unsigned long tmp;
312 		do {
313 			tmp = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
314 			    MQ200_PMCSR);
315 		} while ((tmp & 0x3) != (sc->sc_mq200pwstate & 0x3));
316 		delay(100000); /* XXX */
317 	}
318 
319 	return (0);
320 }
321 
322 
323 static int
324 mq200_fbinit(struct hpcfb_fbconf *fb)
325 {
326 
327 	/*
328 	 * get fb settings from bootinfo
329 	 */
330 	if (bootinfo == NULL ||
331 	    bootinfo->fb_addr == 0 ||
332 	    bootinfo->fb_line_bytes == 0 ||
333 	    bootinfo->fb_width == 0 ||
334 	    bootinfo->fb_height == 0) {
335 		printf("no frame buffer information.\n");
336 		return (-1);
337 	}
338 
339 	/* zero fill */
340 	bzero(fb, sizeof(*fb));
341 
342 	fb->hf_conf_index	= 0;	/* configuration index		*/
343 	fb->hf_nconfs		= 1;   	/* how many configurations	*/
344 	strcpy(fb->hf_name, "built-in video");
345 					/* frame buffer name		*/
346 	strcpy(fb->hf_conf_name, "default");
347 					/* configuration name		*/
348 	fb->hf_height		= bootinfo->fb_height;
349 	fb->hf_width		= bootinfo->fb_width;
350 	fb->hf_baseaddr		= mips_ptob(mips_btop(bootinfo->fb_addr));
351 	fb->hf_offset		= (u_long)bootinfo->fb_addr - fb->hf_baseaddr;
352 					/* frame buffer start offset   	*/
353 	fb->hf_bytes_per_line	= bootinfo->fb_line_bytes;
354 	fb->hf_nplanes		= 1;
355 	fb->hf_bytes_per_plane	= bootinfo->fb_height *
356 	    bootinfo->fb_line_bytes;
357 
358 	fb->hf_access_flags |= HPCFB_ACCESS_BYTE;
359 	fb->hf_access_flags |= HPCFB_ACCESS_WORD;
360 	fb->hf_access_flags |= HPCFB_ACCESS_DWORD;
361 
362 	switch (bootinfo->fb_type) {
363 		/*
364 		 * monochrome
365 		 */
366 	case BIFB_D1_M2L_1:
367 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
368 		/* fall through */
369 	case BIFB_D1_M2L_0:
370 		fb->hf_class = HPCFB_CLASS_GRAYSCALE;
371 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
372 		fb->hf_pack_width = 8;
373 		fb->hf_pixels_per_pack = 8;
374 		fb->hf_pixel_width = 1;
375 		fb->hf_class_data_length = sizeof(struct hf_gray_tag);
376 		fb->hf_u.hf_gray.hf_flags = 0;	/* reserved for future use */
377 		break;
378 
379 		/*
380 		 * gray scale
381 		 */
382 	case BIFB_D2_M2L_3:
383 	case BIFB_D2_M2L_3x2:
384 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
385 		/* fall through */
386 	case BIFB_D2_M2L_0:
387 	case BIFB_D2_M2L_0x2:
388 		fb->hf_class = HPCFB_CLASS_GRAYSCALE;
389 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
390 		fb->hf_pack_width = 8;
391 		fb->hf_pixels_per_pack = 4;
392 		fb->hf_pixel_width = 2;
393 		fb->hf_class_data_length = sizeof(struct hf_gray_tag);
394 		fb->hf_u.hf_gray.hf_flags = 0;	/* reserved for future use */
395 		break;
396 
397 	case BIFB_D4_M2L_F:
398 	case BIFB_D4_M2L_Fx2:
399 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
400 		/* fall through */
401 	case BIFB_D4_M2L_0:
402 	case BIFB_D4_M2L_0x2:
403 		fb->hf_class = HPCFB_CLASS_GRAYSCALE;
404 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
405 		fb->hf_pack_width = 8;
406 		fb->hf_pixels_per_pack = 2;
407 		fb->hf_pixel_width = 4;
408 		fb->hf_class_data_length = sizeof(struct hf_gray_tag);
409 		fb->hf_u.hf_gray.hf_flags = 0;	/* reserved for future use */
410 		break;
411 
412 		/*
413 		 * indexed color
414 		 */
415 	case BIFB_D8_FF:
416 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
417 		/* fall through */
418 	case BIFB_D8_00:
419 		fb->hf_class = HPCFB_CLASS_INDEXCOLOR;
420 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
421 		fb->hf_pack_width = 8;
422 		fb->hf_pixels_per_pack = 1;
423 		fb->hf_pixel_width = 8;
424 		fb->hf_class_data_length = sizeof(struct hf_indexed_tag);
425 		fb->hf_u.hf_indexed.hf_flags = 0; /* reserved for future use */
426 		break;
427 
428 		/*
429 		 * RGB color
430 		 */
431 	case BIFB_D16_FFFF:
432 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
433 		/* fall through */
434 	case BIFB_D16_0000:
435 		fb->hf_class = HPCFB_CLASS_RGBCOLOR;
436 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
437 		fb->hf_order_flags = HPCFB_REVORDER_BYTE;
438 		fb->hf_pack_width = 16;
439 		fb->hf_pixels_per_pack = 1;
440 		fb->hf_pixel_width = 16;
441 
442 		fb->hf_class_data_length = sizeof(struct hf_rgb_tag);
443 		fb->hf_u.hf_rgb.hf_flags = 0;	/* reserved for future use */
444 
445 		fb->hf_u.hf_rgb.hf_red_width = 5;
446 		fb->hf_u.hf_rgb.hf_red_shift = 11;
447 		fb->hf_u.hf_rgb.hf_green_width = 6;
448 		fb->hf_u.hf_rgb.hf_green_shift = 5;
449 		fb->hf_u.hf_rgb.hf_blue_width = 5;
450 		fb->hf_u.hf_rgb.hf_blue_shift = 0;
451 		fb->hf_u.hf_rgb.hf_alpha_width = 0;
452 		fb->hf_u.hf_rgb.hf_alpha_shift = 0;
453 		break;
454 
455 	default:
456 		printf("unknown type (=%d).\n", bootinfo->fb_type);
457 		return (-1);
458 		break;
459 	}
460 
461 	return (0); /* no error */
462 }
463 
464 int
465 mq200_ioctl(v, cmd, data, flag, p)
466 	void *v;
467 	u_long cmd;
468 	caddr_t data;
469 	int flag;
470 	struct proc *p;
471 {
472 	struct mq200_softc *sc = (struct mq200_softc *)v;
473 	struct hpcfb_fbconf *fbconf;
474 	struct hpcfb_dspconf *dspconf;
475 	struct wsdisplay_cmap *cmap;
476 	struct wsdisplay_param *dispparam;
477 
478 	switch (cmd) {
479 	case WSDISPLAYIO_GETCMAP:
480 		cmap = (struct wsdisplay_cmap *)data;
481 
482 		if (sc->sc_fbconf.hf_class != HPCFB_CLASS_INDEXCOLOR ||
483 		    sc->sc_fbconf.hf_pack_width != 8 ||
484 		    256 <= cmap->index ||
485 		    256 - cmap->index < cmap->count)
486 			return (EINVAL);
487 
488 		/*
489 		 * This driver can't get color map.
490 		 */
491 		return (EINVAL);
492 
493 	case WSDISPLAYIO_PUTCMAP:
494 		/*
495 		 * This driver can't set color map.
496 		 */
497 		return (EINVAL);
498 
499 	case WSDISPLAYIO_SVIDEO:
500 		if (*(int *)data == WSDISPLAYIO_VIDEO_OFF)
501 			sc->sc_powerstate |= PWRSTAT_VIDEOOFF;
502 		else
503 			sc->sc_powerstate &= ~PWRSTAT_VIDEOOFF;
504 		mq200_update_powerstate(sc, PWRSTAT_ALL);
505 		return 0;
506 
507 	case WSDISPLAYIO_GVIDEO:
508 		*(int *)data = (sc->sc_powerstate&PWRSTAT_VIDEOOFF) ?
509 		    WSDISPLAYIO_VIDEO_OFF:WSDISPLAYIO_VIDEO_ON;
510 		return 0;
511 
512 	case WSDISPLAYIO_GETPARAM:
513 		dispparam = (struct wsdisplay_param*)data;
514 		switch (dispparam->param) {
515 		case WSDISPLAYIO_PARAM_BACKLIGHT:
516 			VPRINTF("ioctl: GET:BACKLIGHT\n");
517 			mq200_init_brightness(sc, 0);
518 			mq200_init_backlight(sc, 0);
519 			VPRINTF("ioctl: GET:(real)BACKLIGHT %d\n",
520 			    (sc->sc_powerstate&PWRSTAT_BACKLIGHT)? 1: 0);
521 			dispparam->min = 0;
522 			dispparam->max = 1;
523 			if (sc->sc_max_brightness > 0)
524 				dispparam->curval = sc->sc_brightness > 0
525 				    ? 1: 0;
526 			else
527 				dispparam->curval =
528 				    (sc->sc_powerstate&PWRSTAT_BACKLIGHT)
529 				    ? 1: 0;
530 			VPRINTF("ioctl: GET:BACKLIGHT:%d(%s)\n",
531 			    dispparam->curval,
532 			    sc->sc_max_brightness > 0? "brightness": "light");
533 			return 0;
534 			break;
535 		case WSDISPLAYIO_PARAM_CONTRAST:
536 			VPRINTF("ioctl: GET:CONTRAST\n");
537 			mq200_init_contrast(sc, 0);
538 			if (sc->sc_max_contrast > 0) {
539 				dispparam->min = 0;
540 				dispparam->max = sc->sc_max_contrast;
541 				dispparam->curval = sc->sc_contrast;
542 				VPRINTF("ioctl: GET:CONTRAST"
543 				    " max=%d, current=%d\n",
544 				    sc->sc_max_contrast, sc->sc_contrast);
545 				return 0;
546 			} else {
547 				VPRINTF("ioctl: GET:CONTRAST EINVAL\n");
548 				return (EINVAL);
549 			}
550 			break;
551 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
552 			VPRINTF("ioctl: GET:BRIGHTNESS\n");
553 			mq200_init_brightness(sc, 0);
554 			if (sc->sc_max_brightness > 0) {
555 				dispparam->min = 0;
556 				dispparam->max = sc->sc_max_brightness;
557 				dispparam->curval = sc->sc_brightness;
558 				VPRINTF("ioctl: GET:BRIGHTNESS"
559 				    " max=%d, current=%d\n",
560 				    sc->sc_max_brightness, sc->sc_brightness);
561 				return 0;
562 			} else {
563 				VPRINTF("ioctl: GET:BRIGHTNESS EINVAL\n");
564 				return (EINVAL);
565 			}
566 			return (EINVAL);
567 		default:
568 			return (EINVAL);
569 		}
570 		return (0);
571 
572 	case WSDISPLAYIO_SETPARAM:
573 		dispparam = (struct wsdisplay_param*)data;
574 		switch (dispparam->param) {
575 		case WSDISPLAYIO_PARAM_BACKLIGHT:
576 			VPRINTF("ioctl: SET:BACKLIGHT\n");
577 			if (dispparam->curval < 0 ||
578 			    1 < dispparam->curval)
579 				return (EINVAL);
580 			mq200_init_brightness(sc, 0);
581 			VPRINTF("ioctl: SET:max brightness=%d\n",
582 			    sc->sc_max_brightness);
583 			if (sc->sc_max_brightness > 0) { /* dimmer */
584 				if (dispparam->curval == 0){
585 					sc->sc_brightness_save =
586 					    sc->sc_brightness;
587 					mq200_set_brightness(sc, 0); /* min */
588 				} else {
589 					if (sc->sc_brightness_save == 0)
590 						sc->sc_brightness_save =
591 						    sc->sc_max_brightness;
592 					mq200_set_brightness(sc,
593 					    sc->sc_brightness_save);
594 				}
595 				VPRINTF("ioctl: SET:BACKLIGHT:"
596 				    " brightness=%d\n", sc->sc_brightness);
597 			} else { /* off */
598 				if (dispparam->curval == 0)
599 					sc->sc_powerstate &= ~PWRSTAT_BACKLIGHT;
600 				else
601 					sc->sc_powerstate |= PWRSTAT_BACKLIGHT;
602 				VPRINTF("ioctl: SET:BACKLIGHT:"
603 				    " powerstate %d\n",
604 				    (sc->sc_powerstate & PWRSTAT_BACKLIGHT)
605 				    ? 1 : 0);
606 				mq200_update_powerstate(sc, PWRSTAT_BACKLIGHT);
607 				VPRINTF("ioctl: SET:BACKLIGHT:%d\n",
608 				    (sc->sc_powerstate & PWRSTAT_BACKLIGHT)
609 				    ? 1 : 0);
610 			}
611 			return 0;
612 			break;
613 		case WSDISPLAYIO_PARAM_CONTRAST:
614 			VPRINTF("ioctl: SET:CONTRAST\n");
615 			mq200_init_contrast(sc, 0);
616 			if (dispparam->curval < 0 ||
617 			    sc->sc_max_contrast < dispparam->curval)
618 				return (EINVAL);
619 			if (sc->sc_max_contrast > 0) {
620 				int org = sc->sc_contrast;
621 				mq200_set_contrast(sc, dispparam->curval);
622 				VPRINTF("ioctl: SET:CONTRAST"
623 				    " org=%d, current=%d\n", org,
624 				    sc->sc_contrast);
625 				VPRINTF("ioctl: SETPARAM:"
626 				    " CONTRAST org=%d, current=%d\n", org,
627 				    sc->sc_contrast);
628 				return 0;
629 			} else {
630 				VPRINTF("ioctl: SET:CONTRAST EINVAL\n");
631 				return (EINVAL);
632 			}
633 			break;
634 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
635 			VPRINTF("ioctl: SET:BRIGHTNESS\n");
636 			mq200_init_brightness(sc, 0);
637 			if (dispparam->curval < 0 ||
638 			    sc->sc_max_brightness < dispparam->curval)
639 				return (EINVAL);
640 			if (sc->sc_max_brightness > 0) {
641 				int org = sc->sc_brightness;
642 				mq200_set_brightness(sc, dispparam->curval);
643 				VPRINTF("ioctl: SET:BRIGHTNESS"
644 				    " org=%d, current=%d\n", org,
645 				    sc->sc_brightness);
646 				return 0;
647 			} else {
648 				VPRINTF("ioctl: SET:BRIGHTNESS EINVAL\n");
649 				return (EINVAL);
650 			}
651 			break;
652 		default:
653 			return (EINVAL);
654 		}
655 		return (0);
656 
657 	case HPCFBIO_GCONF:
658 		fbconf = (struct hpcfb_fbconf *)data;
659 		if (fbconf->hf_conf_index != 0 &&
660 		    fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
661 			return (EINVAL);
662 		}
663 		*fbconf = sc->sc_fbconf;	/* structure assignment */
664 		return (0);
665 	case HPCFBIO_SCONF:
666 		fbconf = (struct hpcfb_fbconf *)data;
667 		if (fbconf->hf_conf_index != 0 &&
668 		    fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
669 			return (EINVAL);
670 		}
671 		/*
672 		 * nothing to do because we have only one configration
673 		 */
674 		return (0);
675 	case HPCFBIO_GDSPCONF:
676 		dspconf = (struct hpcfb_dspconf *)data;
677 		if ((dspconf->hd_unit_index != 0 &&
678 		    dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
679 		    (dspconf->hd_conf_index != 0 &&
680 			dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
681 			return (EINVAL);
682 		}
683 		*dspconf = sc->sc_dspconf;	/* structure assignment */
684 		return (0);
685 	case HPCFBIO_SDSPCONF:
686 		dspconf = (struct hpcfb_dspconf *)data;
687 		if ((dspconf->hd_unit_index != 0 &&
688 		    dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
689 		    (dspconf->hd_conf_index != 0 &&
690 			dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
691 			return (EINVAL);
692 		}
693 		/*
694 		 * nothing to do
695 		 * because we have only one unit and one configration
696 		 */
697 		return (0);
698 	case HPCFBIO_GOP:
699 	case HPCFBIO_SOP:
700 		/*
701 		 * curently not implemented...
702 		 */
703 		return (EINVAL);
704 	}
705 
706 	return (EPASSTHROUGH);
707 }
708 
709 paddr_t
710 mq200_mmap(void *ctx, off_t offset, int prot)
711 {
712 	struct mq200_softc *sc = (struct mq200_softc *)ctx;
713 
714 	if (offset < 0 || MQ200_MAPSIZE <= offset)
715 		return -1;
716 
717 	return mips_btop(sc->sc_baseaddr + offset);
718 }
719 
720 
721 void
722 mq200_init_backlight(struct mq200_softc *sc, int inattach)
723 {
724 	int val = -1;
725 
726 	if (sc->sc_lcd_inited&BACKLIGHT_INITED)
727 		return;
728 
729 	if (config_hook_call(CONFIG_HOOK_GET,
730 	    CONFIG_HOOK_POWER_LCDLIGHT, &val) != -1) {
731 		/* we can get real light state */
732 		VPRINTF("init_backlight: real backlight=%d\n", val);
733 		if (val == 0)
734 			sc->sc_powerstate &= ~PWRSTAT_BACKLIGHT;
735 		else
736 			sc->sc_powerstate |= PWRSTAT_BACKLIGHT;
737 		sc->sc_lcd_inited |= BACKLIGHT_INITED;
738 	} else if (inattach) {
739 		/*
740 		   we cannot get real light state in attach time
741 		   because light device not yet attached.
742 		   we will retry in !inattach.
743 		   temporary assume light is on.
744 		*/
745 		sc->sc_powerstate |= PWRSTAT_BACKLIGHT;
746 	} else {
747 		/* we cannot get real light state, so work by myself state */
748 		sc->sc_lcd_inited |= BACKLIGHT_INITED;
749 	}
750 }
751 
752 void
753 mq200_init_brightness(struct mq200_softc *sc, int inattach)
754 {
755 	int val = -1;
756 
757 	if (sc->sc_lcd_inited&BRIGHTNESS_INITED)
758 		return;
759 
760 	VPRINTF("init_brightness\n");
761 	if (config_hook_call(CONFIG_HOOK_GET,
762 	    CONFIG_HOOK_BRIGHTNESS_MAX, &val) != -1) {
763 		/* we can get real brightness max */
764 		VPRINTF("init_brightness: real brightness max=%d\n", val);
765 		sc->sc_max_brightness = val;
766 		val = -1;
767 		if (config_hook_call(CONFIG_HOOK_GET,
768 		    CONFIG_HOOK_BRIGHTNESS, &val) != -1) {
769 			/* we can get real brightness */
770 			VPRINTF("init_brightness: real brightness=%d\n", val);
771 			sc->sc_brightness_save = sc->sc_brightness = val;
772 		} else {
773 			sc->sc_brightness_save =
774 			    sc->sc_brightness = sc->sc_max_brightness;
775 		}
776 		sc->sc_lcd_inited |= BRIGHTNESS_INITED;
777 	} else if (inattach) {
778 		/*
779 		   we cannot get real brightness in attach time
780 		   because brightness device not yet attached.
781 		   we will retry in !inattach.
782 		*/
783 		sc->sc_max_brightness = -1;
784 		sc->sc_brightness = -1;
785 		sc->sc_brightness_save = -1;
786 	} else {
787 		/* we cannot get real brightness */
788 		sc->sc_lcd_inited |= BRIGHTNESS_INITED;
789 	}
790 
791 	return;
792 }
793 
794 
795 void
796 mq200_init_contrast(struct mq200_softc *sc, int inattach)
797 {
798 	int val = -1;
799 
800 	if (sc->sc_lcd_inited&CONTRAST_INITED)
801 		return;
802 
803 	VPRINTF("init_contrast\n");
804 	if (config_hook_call(CONFIG_HOOK_GET,
805 	    CONFIG_HOOK_CONTRAST_MAX, &val) != -1) {
806 		/* we can get real contrast max */
807 		VPRINTF("init_contrast: real contrast max=%d\n", val);
808 		sc->sc_max_contrast = val;
809 		val = -1;
810 		if (config_hook_call(CONFIG_HOOK_GET,
811 		    CONFIG_HOOK_CONTRAST, &val) != -1) {
812 			/* we can get real contrast */
813 			VPRINTF("init_contrast: real contrast=%d\n", val);
814 			sc->sc_contrast = val;
815 		} else {
816 			sc->sc_contrast = sc->sc_max_contrast;
817 		}
818 		sc->sc_lcd_inited |= CONTRAST_INITED;
819 	} else if (inattach) {
820 		/*
821 		   we cannot get real contrast in attach time
822 		   because contrast device not yet attached.
823 		   we will retry in !inattach.
824 		*/
825 		sc->sc_max_contrast = -1;
826 		sc->sc_contrast = -1;
827 	} else {
828 		/* we cannot get real contrast */
829 		sc->sc_lcd_inited |= CONTRAST_INITED;
830 	}
831 
832 	return;
833 }
834 
835 
836 void
837 mq200_set_brightness(struct mq200_softc *sc, int val)
838 {
839 	sc->sc_brightness = val;
840 
841 	config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_BRIGHTNESS, &val);
842 	if (config_hook_call(CONFIG_HOOK_GET,
843 	    CONFIG_HOOK_BRIGHTNESS, &val) != -1) {
844 		sc->sc_brightness = val;
845 	}
846 }
847 
848 void
849 mq200_set_contrast(struct mq200_softc *sc, int val)
850 {
851 	sc->sc_contrast = val;
852 
853 	config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_CONTRAST, &val);
854 	if (config_hook_call(CONFIG_HOOK_GET,
855 	    CONFIG_HOOK_CONTRAST, &val) != -1) {
856 		sc->sc_contrast = val;
857 	}
858 }
859