Home | History | Annotate | Line # | Download | only in dev
mq200.c revision 1.2.2.6
      1 /*	$NetBSD: mq200.c,v 1.2.2.6 2001/03/12 13:28:37 bouyer Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 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/param.h>
     33 #include <sys/kernel.h>
     34 #include <sys/device.h>
     35 #include <sys/systm.h>
     36 #include <sys/reboot.h>
     37 
     38 #include <uvm/uvm_extern.h>
     39 
     40 #include <dev/wscons/wsconsio.h>
     41 
     42 #include <machine/bootinfo.h>
     43 #include <machine/bus.h>
     44 #include <machine/autoconf.h>
     45 #include <machine/config_hook.h>
     46 #include <machine/platid.h>
     47 #include <machine/platid_mask.h>
     48 
     49 #include <hpcmips/dev/mq200reg.h>
     50 #include <hpcmips/dev/mq200var.h>
     51 #include "bivideo.h"
     52 #if NBIVIDEO > 0
     53 #include <dev/hpc/bivideovar.h>
     54 #endif
     55 
     56 #define MQ200DEBUG
     57 #ifdef MQ200DEBUG
     58 #ifndef MQ200DEBUG_CONF
     59 #define MQ200DEBUG_CONF 0
     60 #endif
     61 int	mq200_debug = MQ200DEBUG_CONF;
     62 #define	DPRINTF(arg)     do { if (mq200_debug) printf arg; } while(0);
     63 #define	DPRINTFN(n, arg) do { if (mq200_debug > (n)) printf arg; } while (0);
     64 #define	VPRINTF(arg)     do { if (bootverbose || mq200_debug) printf arg; } while(0);
     65 #define	VPRINTFN(n, arg) do { if (bootverbose || mq200_debug > (n)) printf arg; } while (0);
     66 #else
     67 #define	DPRINTF(arg)     do { } while (0);
     68 #define DPRINTFN(n, arg) do { } while (0);
     69 #define	VPRINTF(arg)     do { if (bootverbose) printf arg; } while(0);
     70 #define	VPRINTFN(n, arg) do { if (bootverbose) printf arg; } while (0);
     71 #endif
     72 
     73 /*
     74  * function prototypes
     75  */
     76 static void	mq200_power __P((int, void *));
     77 static int	mq200_hardpower __P((void *, int, long, void *));
     78 static int	mq200_fbinit __P((struct hpcfb_fbconf *));
     79 static int	mq200_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
     80 static paddr_t	mq200_mmap __P((void *, off_t offset, int));
     81 static void	mq200_update_powerstate __P((struct mq200_softc *, int));
     82 void	mq200_get_backlight __P((struct mq200_softc *));
     83 void	mq200_init_brightness __P((struct mq200_softc *));
     84 void	mq200_init_contrast __P((struct mq200_softc *));
     85 void	mq200_set_brightness __P((struct mq200_softc *, int));
     86 void	mq200_set_contrast __P((struct mq200_softc *, int));
     87 
     88 /*
     89  * static variables
     90  */
     91 struct hpcfb_accessops mq200_ha = {
     92 	mq200_ioctl, mq200_mmap
     93 };
     94 
     95 int
     96 mq200_probe(iot, ioh)
     97 	bus_space_tag_t iot;
     98 	bus_space_handle_t ioh;
     99 {
    100 	unsigned long regval;
    101 
    102 #if NBIVIDEO > 0
    103 	if (bivideo_dont_attach) /* some video driver already attached */
    104 		return (0);
    105 #endif /* NBIVIDEO > 0 */
    106 
    107 	regval = bus_space_read_4(iot, ioh, MQ200_PC00R);
    108 	VPRINTF(("mq200 probe: vendor id=%04lx product id=%04lx\n",
    109 		 regval & 0xffff, (regval >> 16) & 0xffff));
    110 	if (regval != ((MQ200_PRODUCT_ID << 16) | MQ200_VENDOR_ID))
    111 		return (0);
    112 
    113 	return (1);
    114 }
    115 
    116 void
    117 mq200_attach(sc)
    118 	struct mq200_softc *sc;
    119 {
    120 	unsigned long regval;
    121 	struct hpcfb_attach_args ha;
    122 	int console = (bootinfo->bi_cnuse & BI_CNUSE_SERIAL) ? 0 : 1;
    123 
    124 	printf(": ");
    125 	if (mq200_fbinit(&sc->sc_fbconf) != 0) {
    126 		/* just return so that hpcfb will not be attached */
    127 		return;
    128 	}
    129 
    130 	sc->sc_fbconf.hf_baseaddr = (u_long)bootinfo->fb_addr;
    131 	sc->sc_fbconf.hf_offset	= (u_long)sc->sc_fbconf.hf_baseaddr -
    132 	    MIPS_PHYS_TO_KSEG1(mips_ptob(mips_btop(sc->sc_baseaddr)));
    133 	DPRINTF(("hf_baseaddr=%lx\n", sc->sc_fbconf.hf_baseaddr));
    134 	DPRINTF(("hf_offset=%lx\n", sc->sc_fbconf.hf_offset));
    135 
    136 	regval = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MQ200_PC08R);
    137 	printf("MQ200 Rev.%02lx video controller", regval & 0xff);
    138 	if (console) {
    139 		printf(", console");
    140 	}
    141 	printf("\n");
    142         printf("%s: framebuffer address: 0x%08lx\n",
    143 		sc->sc_dev.dv_xname, (u_long)bootinfo->fb_addr);
    144 
    145 	/* Add a power hook to power saving */
    146 	sc->sc_mq200pwstate = MQ200_POWERSTATE_D0;
    147 	sc->sc_powerhook = powerhook_establish(mq200_power, sc);
    148 	if (sc->sc_powerhook == NULL)
    149 		printf("%s: WARNING: unable to establish power hook\n",
    150 			sc->sc_dev.dv_xname);
    151 
    152 	/* Add a hard power hook to power saving */
    153 	sc->sc_hardpowerhook = config_hook(CONFIG_HOOK_PMEVENT,
    154 					   CONFIG_HOOK_PMEVENT_HARDPOWER,
    155 					   CONFIG_HOOK_SHARE,
    156 					   mq200_hardpower, sc);
    157 	if (sc->sc_hardpowerhook == NULL)
    158 		printf("%s: WARNING: unable to establish hard power hook\n",
    159 			sc->sc_dev.dv_xname);
    160 
    161 	/* initialize backlight brightness and lcd contrast */
    162 	sc->sc_brightness = sc->sc_contrast =
    163 	sc->sc_max_brightness = sc->sc_max_contrast = -1;
    164 	mq200_init_brightness(sc);
    165 	mq200_init_contrast(sc);
    166 	mq200_get_backlight(sc);
    167 
    168 	if (console && hpcfb_cnattach(&sc->sc_fbconf) != 0) {
    169 		panic("mq200_attach: can't init fb console");
    170 	}
    171 
    172 	ha.ha_console = console;
    173 	ha.ha_accessops = &mq200_ha;
    174 	ha.ha_accessctx = sc;
    175 	ha.ha_curfbconf = 0;
    176 	ha.ha_nfbconf = 1;
    177 	ha.ha_fbconflist = &sc->sc_fbconf;
    178 	ha.ha_curdspconf = 0;
    179 	ha.ha_ndspconf = 1;
    180 	ha.ha_dspconflist = &sc->sc_dspconf;
    181 
    182 	config_found(&sc->sc_dev, &ha, hpcfbprint);
    183 
    184 #if NBIVIDEO > 0
    185 	/*
    186 	 * bivideo is no longer need
    187 	 */
    188 	bivideo_dont_attach = 1;
    189 #endif /* NBIVIDEO > 0 */
    190 }
    191 
    192 static void
    193 mq200_update_powerstate(sc, updates)
    194 	struct mq200_softc *sc;
    195 	int updates;
    196 {
    197 
    198 	if (updates & PWRSTAT_LCD)
    199 		config_hook_call(CONFIG_HOOK_POWERCONTROL,
    200 		    CONFIG_HOOK_POWERCONTROL_LCD,
    201 		    (void*)!(sc->sc_powerstate &
    202 				(PWRSTAT_VIDEOOFF|PWRSTAT_SUSPEND)));
    203 
    204 	if (updates & PWRSTAT_BACKLIGHT)
    205 		config_hook_call(CONFIG_HOOK_POWERCONTROL,
    206 		    CONFIG_HOOK_POWERCONTROL_LCDLIGHT,
    207 		    (void*)(!(sc->sc_powerstate &
    208 				(PWRSTAT_VIDEOOFF|PWRSTAT_SUSPEND)) &&
    209 			     (sc->sc_powerstate & PWRSTAT_BACKLIGHT)));
    210 }
    211 
    212 static void
    213 mq200_power(why, arg)
    214 	int why;
    215 	void *arg;
    216 {
    217 	struct mq200_softc *sc = arg;
    218 
    219 	switch (why) {
    220 	case PWR_SUSPEND:
    221 		sc->sc_powerstate |= PWRSTAT_SUSPEND;
    222 		mq200_update_powerstate(sc, PWRSTAT_ALL);
    223 		break;
    224 	case PWR_STANDBY:
    225 		sc->sc_powerstate |= PWRSTAT_SUSPEND;
    226 		mq200_update_powerstate(sc, PWRSTAT_ALL);
    227 		break;
    228 	case PWR_RESUME:
    229 		sc->sc_powerstate &= ~PWRSTAT_SUSPEND;
    230 		mq200_update_powerstate(sc, PWRSTAT_ALL);
    231 		break;
    232 	}
    233 }
    234 
    235 static int
    236 mq200_hardpower(ctx, type, id, msg)
    237 	void *ctx;
    238 	int type;
    239 	long id;
    240 	void *msg;
    241 {
    242 	struct mq200_softc *sc = ctx;
    243 	int why = (int)msg;
    244 
    245 	switch (why) {
    246 	case PWR_SUSPEND:
    247 		sc->sc_mq200pwstate = MQ200_POWERSTATE_D2;
    248 		break;
    249 	case PWR_STANDBY:
    250 		sc->sc_mq200pwstate = MQ200_POWERSTATE_D3;
    251 		break;
    252 	case PWR_RESUME:
    253 		sc->sc_mq200pwstate = MQ200_POWERSTATE_D0;
    254 		break;
    255 	}
    256 
    257 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    258 			  MQ200_PMCSR, sc->sc_mq200pwstate);
    259 
    260 	/*
    261 	 * you should wait until the
    262 	 * power state transit sequence will end.
    263 	 */
    264 	{
    265 		unsigned long tmp;
    266 		do {
    267 			tmp = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    268 					       MQ200_PMCSR);
    269 		} while ((tmp & 0x3) != (sc->sc_mq200pwstate & 0x3));
    270 		delay(100000); /* XXX */
    271 	}
    272 
    273 	return (0);
    274 }
    275 
    276 
    277 static int
    278 mq200_fbinit(fb)
    279 	struct hpcfb_fbconf *fb;
    280 {
    281 
    282 	/*
    283 	 * get fb settings from bootinfo
    284 	 */
    285 	if (bootinfo == NULL ||
    286 	    bootinfo->fb_addr == 0 ||
    287 	    bootinfo->fb_line_bytes == 0 ||
    288 	    bootinfo->fb_width == 0 ||
    289 	    bootinfo->fb_height == 0) {
    290 		printf("no frame buffer infomation.\n");
    291 		return (-1);
    292 	}
    293 
    294 	/* zero fill */
    295 	bzero(fb, sizeof(*fb));
    296 
    297 	fb->hf_conf_index	= 0;	/* configuration index		*/
    298 	fb->hf_nconfs		= 1;   	/* how many configurations	*/
    299 	strcpy(fb->hf_name, "built-in video");
    300 					/* frame buffer name		*/
    301 	strcpy(fb->hf_conf_name, "default");
    302 					/* configuration name		*/
    303 	fb->hf_height		= bootinfo->fb_height;
    304 	fb->hf_width		= bootinfo->fb_width;
    305 	fb->hf_baseaddr		= mips_ptob(mips_btop(bootinfo->fb_addr));
    306 	fb->hf_offset		= (u_long)bootinfo->fb_addr - fb->hf_baseaddr;
    307 					/* frame buffer start offset   	*/
    308 	fb->hf_bytes_per_line	= bootinfo->fb_line_bytes;
    309 	fb->hf_nplanes		= 1;
    310 	fb->hf_bytes_per_plane	= bootinfo->fb_height *
    311 					bootinfo->fb_line_bytes;
    312 
    313 	fb->hf_access_flags |= HPCFB_ACCESS_BYTE;
    314 	fb->hf_access_flags |= HPCFB_ACCESS_WORD;
    315 	fb->hf_access_flags |= HPCFB_ACCESS_DWORD;
    316 
    317 	switch (bootinfo->fb_type) {
    318 		/*
    319 		 * gray scale
    320 		 */
    321 	case BIFB_D2_M2L_3:
    322 	case BIFB_D2_M2L_3x2:
    323 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
    324 		/* fall through */
    325 	case BIFB_D2_M2L_0:
    326 	case BIFB_D2_M2L_0x2:
    327 		fb->hf_class = HPCFB_CLASS_GRAYSCALE;
    328 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
    329 		fb->hf_pack_width = 8;
    330 		fb->hf_pixels_per_pack = 4;
    331 		fb->hf_pixel_width = 2;
    332 		fb->hf_class_data_length = sizeof(struct hf_gray_tag);
    333 		fb->hf_u.hf_gray.hf_flags = 0;	/* reserved for future use */
    334 		break;
    335 
    336 	case BIFB_D4_M2L_F:
    337 	case BIFB_D4_M2L_Fx2:
    338 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
    339 		/* fall through */
    340 	case BIFB_D4_M2L_0:
    341 	case BIFB_D4_M2L_0x2:
    342 		fb->hf_class = HPCFB_CLASS_GRAYSCALE;
    343 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
    344 		fb->hf_pack_width = 8;
    345 		fb->hf_pixels_per_pack = 2;
    346 		fb->hf_pixel_width = 4;
    347 		fb->hf_class_data_length = sizeof(struct hf_gray_tag);
    348 		fb->hf_u.hf_gray.hf_flags = 0;	/* reserved for future use */
    349 		break;
    350 
    351 		/*
    352 		 * indexed color
    353 		 */
    354 	case BIFB_D8_FF:
    355 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
    356 		/* fall through */
    357 	case BIFB_D8_00:
    358 		fb->hf_class = HPCFB_CLASS_INDEXCOLOR;
    359 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
    360 		fb->hf_pack_width = 8;
    361 		fb->hf_pixels_per_pack = 1;
    362 		fb->hf_pixel_width = 8;
    363 		fb->hf_class_data_length = sizeof(struct hf_indexed_tag);
    364 		fb->hf_u.hf_indexed.hf_flags = 0; /* reserved for future use */
    365 		break;
    366 
    367 		/*
    368 		 * RGB color
    369 		 */
    370 	case BIFB_D16_FFFF:
    371 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
    372 		/* fall through */
    373 	case BIFB_D16_0000:
    374 		fb->hf_class = HPCFB_CLASS_RGBCOLOR;
    375 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
    376 #if BYTE_ORDER == LITTLE_ENDIAN
    377 		fb->hf_swap_flags = HPCFB_SWAP_BYTE;
    378 #endif
    379 		fb->hf_pack_width = 16;
    380 		fb->hf_pixels_per_pack = 1;
    381 		fb->hf_pixel_width = 16;
    382 
    383 		fb->hf_class_data_length = sizeof(struct hf_rgb_tag);
    384 		fb->hf_u.hf_rgb.hf_flags = 0;	/* reserved for future use */
    385 
    386 		fb->hf_u.hf_rgb.hf_red_width = 5;
    387 		fb->hf_u.hf_rgb.hf_red_shift = 11;
    388 		fb->hf_u.hf_rgb.hf_green_width = 6;
    389 		fb->hf_u.hf_rgb.hf_green_shift = 5;
    390 		fb->hf_u.hf_rgb.hf_blue_width = 5;
    391 		fb->hf_u.hf_rgb.hf_blue_shift = 0;
    392 		fb->hf_u.hf_rgb.hf_alpha_width = 0;
    393 		fb->hf_u.hf_rgb.hf_alpha_shift = 0;
    394 		break;
    395 
    396 	default:
    397 		printf("unknown type (=%d).\n", bootinfo->fb_type);
    398 		return (-1);
    399 		break;
    400 	}
    401 
    402 	return (0); /* no error */
    403 }
    404 
    405 int
    406 mq200_ioctl(v, cmd, data, flag, p)
    407 	void *v;
    408 	u_long cmd;
    409 	caddr_t data;
    410 	int flag;
    411 	struct proc *p;
    412 {
    413 	struct mq200_softc *sc = (struct mq200_softc *)v;
    414 	struct hpcfb_fbconf *fbconf;
    415 	struct hpcfb_dspconf *dspconf;
    416 	struct wsdisplay_cmap *cmap;
    417 	struct wsdisplay_param *dispparam;
    418 
    419 	switch (cmd) {
    420 	case WSDISPLAYIO_GETCMAP:
    421 		cmap = (struct wsdisplay_cmap*)data;
    422 
    423 		if (sc->sc_fbconf.hf_class != HPCFB_CLASS_INDEXCOLOR ||
    424 		    sc->sc_fbconf.hf_pack_width != 8 ||
    425 		    256 <= cmap->index ||
    426 		    256 < (cmap->index + cmap->count))
    427 			return (EINVAL);
    428 
    429 #if 0
    430 		if (!uvm_useracc(cmap->red, cmap->count, B_WRITE) ||
    431 		    !uvm_useracc(cmap->green, cmap->count, B_WRITE) ||
    432 		    !uvm_useracc(cmap->blue, cmap->count, B_WRITE))
    433 			return (EFAULT);
    434 
    435 		copyout(&bivideo_cmap_r[cmap->index], cmap->red, cmap->count);
    436 		copyout(&bivideo_cmap_g[cmap->index], cmap->green,cmap->count);
    437 		copyout(&bivideo_cmap_b[cmap->index], cmap->blue, cmap->count);
    438 #endif
    439 
    440 		return (0);
    441 
    442 	case WSDISPLAYIO_PUTCMAP:
    443 		/*
    444 		 * This driver can't set color map.
    445 		 */
    446 		return (EINVAL);
    447 
    448 	case WSDISPLAYIO_SVIDEO:
    449 		if (*(int *)data == WSDISPLAYIO_VIDEO_OFF)
    450 			sc->sc_powerstate |= PWRSTAT_VIDEOOFF;
    451 		else
    452 			sc->sc_powerstate &= ~PWRSTAT_VIDEOOFF;
    453 		mq200_update_powerstate(sc, PWRSTAT_ALL);
    454 		return 0;
    455 
    456 	case WSDISPLAYIO_GVIDEO:
    457 		*(int *)data = (sc->sc_powerstate&PWRSTAT_VIDEOOFF) ?
    458 				WSDISPLAYIO_VIDEO_OFF:WSDISPLAYIO_VIDEO_ON;
    459 		return 0;
    460 
    461 	case WSDISPLAYIO_GETPARAM:
    462 		dispparam = (struct wsdisplay_param*)data;
    463 		switch (dispparam->param) {
    464 		case WSDISPLAYIO_PARAM_BACKLIGHT:
    465 			VPRINTF(("mq200_ioctl: GETPARAM:BACKLIGHT call\n"));
    466 			if (sc->sc_max_brightness == -1)
    467 				mq200_init_brightness(sc);
    468 			mq200_get_backlight(sc);
    469 			dispparam->min = 0;
    470 			dispparam->max = 1;
    471 			if (sc->sc_max_brightness > 0)
    472 				dispparam->curval = sc->sc_brightness > 0? 1: 0;
    473 			else
    474 				dispparam->curval =
    475 				    (sc->sc_powerstate&PWRSTAT_BACKLIGHT) ? 1 : 0;
    476 			VPRINTF(("mq200_ioctl: GETPARAM:BACKLIGHT:%d\n",
    477 				dispparam->curval));
    478 			return 0;
    479 			break;
    480 		case WSDISPLAYIO_PARAM_CONTRAST:
    481 			VPRINTF(("mq200_ioctl: GETPARAM:CONTRAST call\n"));
    482 			if (sc->sc_max_contrast == -1)
    483 				mq200_init_contrast(sc);
    484 			if (sc->sc_max_contrast > 0) {
    485 				dispparam->min = 0;
    486 				dispparam->max = sc->sc_max_contrast;
    487 				dispparam->curval = sc->sc_contrast;
    488 				VPRINTF(("mq200_ioctl: GETPARAM:CONTRAST max=%d, current=%d\n", sc->sc_max_contrast, sc->sc_contrast));
    489 				return 0;
    490 			} else {
    491 				VPRINTF(("mq200_ioctl: GETPARAM:CONTRAST ret\n"));
    492 				return (EINVAL);
    493 			}
    494 			break;
    495 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
    496 			VPRINTF(("mq200_ioctl: GETPARAM:BRIGHTNESS call\n"));
    497 			if (sc->sc_max_brightness == -1)
    498 				mq200_init_brightness(sc);
    499 			if (sc->sc_max_brightness > 0) {
    500 				dispparam->min = 0;
    501 				dispparam->max = sc->sc_max_brightness;
    502 				dispparam->curval = sc->sc_brightness;
    503 				VPRINTF(("mq200_ioctl: GETPARAM:BRIGHTNESS max=%d, current=%d\n", sc->sc_max_brightness, sc->sc_brightness));
    504 				return 0;
    505 			} else {
    506 				VPRINTF(("mq200_ioctl: GETPARAM:BRIGHTNESS ret\n"));
    507 				return (EINVAL);
    508 			}
    509 			return (EINVAL);
    510 		default:
    511 			return (EINVAL);
    512 		}
    513 		return (0);
    514 
    515 	case WSDISPLAYIO_SETPARAM:
    516 		dispparam = (struct wsdisplay_param*)data;
    517 		switch (dispparam->param) {
    518 		case WSDISPLAYIO_PARAM_BACKLIGHT:
    519 			VPRINTF(("mq200_ioctl: SETPARAM:BACKLIGHT call\n"));
    520 			if (dispparam->curval < 0 ||
    521 			    1 < dispparam->curval)
    522 				return (EINVAL);
    523 			if (sc->sc_max_brightness == -1)
    524 				mq200_init_brightness(sc);
    525 			VPRINTF(("mq200_ioctl: SETPARAM:max brightness=%d\n", sc->sc_max_brightness));
    526 			if (sc->sc_max_brightness > 0) { /* dimmer */
    527 				if (dispparam->curval == 0){
    528 					sc->sc_brightness_save = sc->sc_brightness;
    529 					mq200_set_brightness(sc, 0);	/* min */
    530 				} else {
    531 					if (sc->sc_brightness_save == 0)
    532 						sc->sc_brightness_save = sc->sc_max_brightness;
    533 					mq200_set_brightness(sc, sc->sc_brightness_save);
    534 				}
    535 				VPRINTF(("mq200_ioctl: SETPARAM:BACKLIGHT: brightness=%d\n", sc->sc_brightness));
    536 			} else { /* off */
    537 				if (dispparam->curval == 0)
    538 					sc->sc_powerstate &= ~PWRSTAT_BACKLIGHT;
    539 				else
    540 					sc->sc_powerstate |= PWRSTAT_BACKLIGHT;
    541 				VPRINTF(("mq200_ioctl: SETPARAM:BACKLIGHT: powerstate %d\n",
    542 						(sc->sc_powerstate & PWRSTAT_BACKLIGHT)?1:0));
    543 				mq200_update_powerstate(sc, PWRSTAT_BACKLIGHT);
    544 				VPRINTF(("mq200_ioctl: SETPARAM:BACKLIGHT:%d\n",
    545 					(sc->sc_powerstate & PWRSTAT_BACKLIGHT)?1:0));
    546 			}
    547 			return 0;
    548 			break;
    549 		case WSDISPLAYIO_PARAM_CONTRAST:
    550 			VPRINTF(("mq200_ioctl: SETPARAM:CONTRAST call\n"));
    551 			if (sc->sc_max_contrast == -1)
    552 				mq200_init_contrast(sc);
    553 			if (dispparam->curval < 0 ||
    554 			    sc->sc_max_contrast < dispparam->curval)
    555 				return (EINVAL);
    556 			if (sc->sc_max_contrast > 0) {
    557 				int org = sc->sc_contrast;
    558 				mq200_set_contrast(sc, dispparam->curval);
    559 				VPRINTF(("mq200_ioctl: SETPARAM:CONTRAST org=%d, current=%d\n", org, sc->sc_contrast));
    560 				return 0;
    561 			} else {
    562 				VPRINTF(("mq200_ioctl: SETPARAM:CONTRAST ret\n"));
    563 				return (EINVAL);
    564 			}
    565 			break;
    566 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
    567 			VPRINTF(("mq200_ioctl: SETPARAM:BRIGHTNESS call\n"));
    568 			if (sc->sc_max_brightness == -1)
    569 				mq200_init_brightness(sc);
    570 			if (dispparam->curval < 0 ||
    571 			    sc->sc_max_brightness < dispparam->curval)
    572 				return (EINVAL);
    573 			if (sc->sc_max_brightness > 0) {
    574 				int org = sc->sc_brightness;
    575 				mq200_set_brightness(sc, dispparam->curval);
    576 				VPRINTF(("mq200_ioctl: SETPARAM:BRIGHTNESS org=%d, current=%d\n", org, sc->sc_brightness));
    577 				return 0;
    578 			} else {
    579 				VPRINTF(("mq200_ioctl: SETPARAM:BRIGHTNESS ret\n"));
    580 				return (EINVAL);
    581 			}
    582 			break;
    583 		default:
    584 			return (EINVAL);
    585 		}
    586 		return (0);
    587 
    588 	case HPCFBIO_GCONF:
    589 		fbconf = (struct hpcfb_fbconf *)data;
    590 		if (fbconf->hf_conf_index != 0 &&
    591 		    fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
    592 			return (EINVAL);
    593 		}
    594 		*fbconf = sc->sc_fbconf;	/* structure assignment */
    595 		return (0);
    596 	case HPCFBIO_SCONF:
    597 		fbconf = (struct hpcfb_fbconf *)data;
    598 		if (fbconf->hf_conf_index != 0 &&
    599 		    fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
    600 			return (EINVAL);
    601 		}
    602 		/*
    603 		 * nothing to do because we have only one configration
    604 		 */
    605 		return (0);
    606 	case HPCFBIO_GDSPCONF:
    607 		dspconf = (struct hpcfb_dspconf *)data;
    608 		if ((dspconf->hd_unit_index != 0 &&
    609 		     dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
    610 		    (dspconf->hd_conf_index != 0 &&
    611 		     dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
    612 			return (EINVAL);
    613 		}
    614 		*dspconf = sc->sc_dspconf;	/* structure assignment */
    615 		return (0);
    616 	case HPCFBIO_SDSPCONF:
    617 		dspconf = (struct hpcfb_dspconf *)data;
    618 		if ((dspconf->hd_unit_index != 0 &&
    619 		     dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
    620 		    (dspconf->hd_conf_index != 0 &&
    621 		     dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
    622 			return (EINVAL);
    623 		}
    624 		/*
    625 		 * nothing to do
    626 		 * because we have only one unit and one configration
    627 		 */
    628 		return (0);
    629 	case HPCFBIO_GOP:
    630 	case HPCFBIO_SOP:
    631 		/*
    632 		 * curently not implemented...
    633 		 */
    634 		return (EINVAL);
    635 	}
    636 
    637 	return (ENOTTY);
    638 }
    639 
    640 paddr_t
    641 mq200_mmap(ctx, offset, prot)
    642 	void *ctx;
    643 	off_t offset;
    644 	int prot;
    645 {
    646 	struct mq200_softc *sc = (struct mq200_softc *)ctx;
    647 
    648 	if (offset < 0 || MQ200_MAPSIZE <= offset)
    649 		return -1;
    650 
    651 	return mips_btop(sc->sc_baseaddr + offset);
    652 }
    653 
    654 
    655 void
    656 mq200_get_backlight(sc)
    657 	struct mq200_softc *sc;
    658 {
    659 	int val = -1;
    660 
    661 	if (config_hook_call(CONFIG_HOOK_GET,
    662 	     CONFIG_HOOK_POWER_LCDLIGHT, &val) != -1) {
    663 		if (val == 0)
    664 			sc->sc_powerstate &= ~PWRSTAT_BACKLIGHT;
    665 		else
    666 			sc->sc_powerstate |= PWRSTAT_BACKLIGHT;
    667 	} else /* assume backlight is on */
    668 		sc->sc_powerstate |= PWRSTAT_BACKLIGHT;
    669 }
    670 
    671 void
    672 mq200_init_brightness(sc)
    673 	struct mq200_softc *sc;
    674 {
    675 	int val = -1;
    676 
    677 	VPRINTF(("mq200_init_brightness\n"));
    678 	if (config_hook_call(CONFIG_HOOK_GET,
    679 	     CONFIG_HOOK_BRIGHTNESS, &val) != -1) {
    680 		sc->sc_brightness_save = sc->sc_brightness = val;
    681 	}
    682 	val = -1;
    683 	if (config_hook_call(CONFIG_HOOK_GET,
    684 	     CONFIG_HOOK_BRIGHTNESS_MAX, &val) != -1) {
    685 		sc->sc_max_brightness = val;
    686 	}
    687 	return;
    688 }
    689 
    690 
    691 void
    692 mq200_init_contrast(sc)
    693 	struct mq200_softc *sc;
    694 {
    695 	int val = -1;
    696 
    697 	VPRINTF(("mq200_init_contrast\n"));
    698 	if (config_hook_call(CONFIG_HOOK_GET,
    699 	     CONFIG_HOOK_CONTRAST, &val) != -1) {
    700 		sc->sc_contrast = val;
    701 	}
    702 	val = -1;
    703 	if (config_hook_call(CONFIG_HOOK_GET,
    704 	     CONFIG_HOOK_CONTRAST_MAX, &val) != -1) {
    705 		sc->sc_max_contrast = val;
    706 	}
    707 	return;
    708 }
    709 
    710 void
    711 mq200_set_brightness(sc, val)
    712 	struct mq200_softc *sc;
    713 	int val;
    714 {
    715 	sc->sc_brightness = val;
    716 
    717 	config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_BRIGHTNESS, &val);
    718 	if (config_hook_call(CONFIG_HOOK_GET,
    719 	     CONFIG_HOOK_BRIGHTNESS, &val) != -1) {
    720 		sc->sc_brightness = val;
    721 	}
    722 }
    723 
    724 void
    725 mq200_set_contrast(sc, val)
    726 	struct mq200_softc *sc;
    727 	int val;
    728 {
    729 	sc->sc_contrast = val;
    730 
    731 	config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_CONTRAST, &val);
    732 	if (config_hook_call(CONFIG_HOOK_GET,
    733 	     CONFIG_HOOK_CONTRAST, &val) != -1) {
    734 		sc->sc_contrast = val;
    735 	}
    736 }
    737