Home | History | Annotate | Line # | Download | only in dev
sed_saip.c revision 1.17
      1 /*	$NetBSD: sed_saip.c,v 1.17 2006/03/04 15:23:14 peter Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999-2001
      5  *         Shin Takemura and PocketBSD Project. 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the PocketBSD project
     18  *	and its contributors.
     19  * 4. Neither the name of the project nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: sed_saip.c,v 1.17 2006/03/04 15:23:14 peter Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/device.h>
     43 #include <sys/buf.h>
     44 #include <sys/ioctl.h>
     45 #include <sys/reboot.h>
     46 
     47 #include <uvm/uvm_extern.h>
     48 
     49 #include <machine/bus.h>
     50 #include <machine/bootinfo.h>
     51 #include <machine/config_hook.h>
     52 #include <machine/platid.h>
     53 #include <machine/platid_mask.h>
     54 
     55 #include <dev/wscons/wsconsio.h>
     56 #include <dev/wscons/wsdisplayvar.h>
     57 
     58 #include <dev/rasops/rasops.h>
     59 
     60 #include <dev/hpc/hpcfbvar.h>
     61 #include <dev/hpc/hpcfbio.h>
     62 #include <dev/hpc/hpccmapvar.h>
     63 
     64 #include <arch/hpcarm/dev/sed1356var.h>
     65 
     66 #ifdef SED_DEBUG
     67 #define VPRINTF(arg)	do { if (bootverbose) printf arg; } while (0);
     68 #else
     69 #define VPRINTF(arg)	/* nothing */
     70 #endif
     71 
     72 /*
     73  *  function prototypes
     74  */
     75 int	sed1356match(struct device *, struct cfdata *, void *);
     76 void	sed1356attach(struct device *, struct device *, void *);
     77 int	sed1356_ioctl(void *, u_long, caddr_t, int, struct lwp *);
     78 paddr_t	sed1356_mmap(void *, off_t, int);
     79 
     80 extern	struct bus_space sa11x0_bs_tag;
     81 extern	int j720lcd_power(void *, int, long, void *);	/* XXX */
     82 
     83 static int sed1356_init(struct hpcfb_fbconf *);
     84 static void sed1356_power(int, void *);
     85 static void sed1356_update_powerstate(struct sed1356_softc *, int);
     86 void	sed1356_init_backlight(struct sed1356_softc *, int);
     87 void	sed1356_init_brightness(struct sed1356_softc *, int);
     88 void	sed1356_init_contrast(struct sed1356_softc *, int);
     89 void	sed1356_set_brightness(struct sed1356_softc *, int);
     90 void	sed1356_set_contrast(struct sed1356_softc *, int);
     91 
     92 #if defined __mips__ || defined __sh__ || defined __arm__
     93 #define __BTOP(x)		((paddr_t)(x) >> PGSHIFT)
     94 #define __PTOB(x)		((paddr_t)(x) << PGSHIFT)
     95 #else
     96 #error "define btop, ptob."
     97 #endif
     98 
     99 /*
    100  *  static variables
    101  */
    102 CFATTACH_DECL(sed, sizeof(struct sed1356_softc),
    103     sed1356match, sed1356attach, NULL, NULL);
    104 struct hpcfb_accessops sed1356_ha = {
    105 	sed1356_ioctl, sed1356_mmap
    106 };
    107 
    108 static int attach_flag = 0;
    109 
    110 /*
    111  *  function bodies
    112  */
    113 int
    114 sed1356match(struct device *parent, struct cfdata *match, void *aux)
    115 {
    116 
    117 	/* XXX check version register */
    118 	return 1;
    119 }
    120 
    121 void
    122 sed1356attach(struct device *parent, struct device *self, void *aux)
    123 {
    124 	struct sed1356_softc *sc = (struct sed1356_softc *)self;
    125 	struct hpcfb_attach_args ha;
    126 	int console = (bootinfo->bi_cnuse & BI_CNUSE_SERIAL) ? 0 : 1;
    127 
    128 	printf("\n");
    129 
    130 	if (attach_flag) {
    131 		panic("%s(%d): sed1356 attached twice", __FILE__, __LINE__);
    132 	}
    133 	attach_flag = 1;
    134 
    135 	if (sed1356_init(&sc->sc_fbconf) != 0) {
    136 		/* just return so that hpcfb will not be attached */
    137 		return;
    138 	}
    139 
    140 	sc->sc_iot = &sa11x0_bs_tag;
    141 	sc->sc_parent = (struct sa11x0_softc *)parent;
    142 	if (bus_space_map(sc->sc_iot, (bus_addr_t)bootinfo->fb_addr & ~0x3fffff,
    143 			  0x200, 0, &sc->sc_regh)) {
    144 		printf("%s: unable to map register\n", sc->sc_dev.dv_xname);
    145 		return;
    146 	}
    147 
    148 	printf("%s: Epson SED1356", sc->sc_dev.dv_xname);
    149 	if (console) {
    150 		printf(", console");
    151 	}
    152 	printf("\n");
    153 	printf("%s: framebuffer address: 0x%08lx\n",
    154 		sc->sc_dev.dv_xname, (u_long)bootinfo->fb_addr);
    155 
    156 	/* Add a suspend hook to power saving */
    157 	sc->sc_powerstate = 0;
    158 	sc->sc_powerhook = powerhook_establish(sed1356_power, sc);
    159 	if (sc->sc_powerhook == NULL)
    160 		printf("%s: WARNING: unable to establish power hook\n",
    161 			sc->sc_dev.dv_xname);
    162 
    163 	/* initialize backlight brightness and lcd contrast */
    164 	sc->sc_lcd_inited = 0;
    165 	sed1356_init_brightness(sc, 1);
    166 	sed1356_init_contrast(sc, 1);
    167 	sed1356_init_backlight(sc, 1);
    168 
    169 	if (console && hpcfb_cnattach(&sc->sc_fbconf) != 0)
    170 		panic("sed1356attach: cannot init fb console");
    171 
    172 	ha.ha_console = console;
    173 	ha.ha_accessops = &sed1356_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 	/* XXX */
    183 	if (platid_match(&platid, &platid_mask_MACH_HP_JORNADA_7XX)) {
    184 		config_hook(CONFIG_HOOK_POWERCONTROL,
    185 			    CONFIG_HOOK_POWERCONTROL_LCDLIGHT,
    186 			    CONFIG_HOOK_SHARE, j720lcd_power, sc);
    187 	}
    188 
    189 	config_found(self, &ha, hpcfbprint);
    190 }
    191 
    192 static int
    193 sed1356_init(struct hpcfb_fbconf *fb)
    194 {
    195 	/*
    196 	 * get fb settings from bootinfo
    197 	 */
    198 	if (bootinfo == NULL ||
    199 	    bootinfo->fb_addr == 0 ||
    200 	    bootinfo->fb_line_bytes == 0 ||
    201 	    bootinfo->fb_width == 0 ||
    202 	    bootinfo->fb_height == 0) {
    203 		printf("no frame buffer information.\n");
    204 		return (-1);
    205 	}
    206 
    207 	/* zero fill */
    208 	memset(fb, 0, sizeof(*fb));
    209 
    210 	fb->hf_conf_index	= 0;	/* configuration index		*/
    211 	fb->hf_nconfs		= 1;   	/* how many configurations	*/
    212 	strcpy(fb->hf_name, "built-in video");
    213 					/* frame buffer name		*/
    214 	strcpy(fb->hf_conf_name, "default");
    215 					/* configuration name		*/
    216 	fb->hf_height		= bootinfo->fb_height;
    217 	fb->hf_width		= bootinfo->fb_width;
    218 
    219 	if (bus_space_map(&sa11x0_bs_tag, (bus_addr_t)bootinfo->fb_addr,
    220 			   bootinfo->fb_height * bootinfo->fb_line_bytes,
    221 			   0, &fb->hf_baseaddr)) {
    222 		printf("unable to map framebuffer\n");
    223 		return (-1);
    224 	}
    225 	fb->hf_offset		= (u_long)bootinfo->fb_addr -
    226 				      __PTOB(__BTOP(bootinfo->fb_addr));
    227 					/* frame buffer start offset   	*/
    228 	fb->hf_bytes_per_line	= bootinfo->fb_line_bytes;
    229 	fb->hf_nplanes		= 1;
    230 	fb->hf_bytes_per_plane	= bootinfo->fb_height *
    231 					bootinfo->fb_line_bytes;
    232 
    233 	fb->hf_access_flags |= HPCFB_ACCESS_BYTE;
    234 	fb->hf_access_flags |= HPCFB_ACCESS_WORD;
    235 	fb->hf_access_flags |= HPCFB_ACCESS_DWORD;
    236 
    237 	switch (bootinfo->fb_type) {
    238 		/*
    239 		 * gray scale
    240 		 */
    241 	case BIFB_D4_M2L_F:
    242 	case BIFB_D4_M2L_Fx2:
    243 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
    244 		/* fall through */
    245 	case BIFB_D4_M2L_0:
    246 	case BIFB_D4_M2L_0x2:
    247 		fb->hf_class = HPCFB_CLASS_GRAYSCALE;
    248 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
    249 		fb->hf_pack_width = 8;
    250 		fb->hf_pixels_per_pack = 2;
    251 		fb->hf_pixel_width = 4;
    252 		fb->hf_class_data_length = sizeof(struct hf_gray_tag);
    253 		fb->hf_u.hf_gray.hf_flags = 0;	/* reserved for future use */
    254 		break;
    255 
    256 		/*
    257 		 * indexed color
    258 		 */
    259 	case BIFB_D8_FF:
    260 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
    261 		/* fall through */
    262 	case BIFB_D8_00:
    263 		fb->hf_class = HPCFB_CLASS_INDEXCOLOR;
    264 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
    265 		fb->hf_pack_width = 8;
    266 		fb->hf_pixels_per_pack = 1;
    267 		fb->hf_pixel_width = 8;
    268 		fb->hf_class_data_length = sizeof(struct hf_indexed_tag);
    269 		fb->hf_u.hf_indexed.hf_flags = 0; /* reserved for future use */
    270 		break;
    271 
    272 		/*
    273 		 * RGB color
    274 		 */
    275 	case BIFB_D16_FFFF:
    276 		fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
    277 		/* fall through */
    278 	case BIFB_D16_0000:
    279 		fb->hf_class = HPCFB_CLASS_RGBCOLOR;
    280 		fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
    281 		fb->hf_order_flags = HPCFB_REVORDER_BYTE;
    282 		fb->hf_pack_width = 16;
    283 		fb->hf_pixels_per_pack = 1;
    284 		fb->hf_pixel_width = 16;
    285 
    286 		fb->hf_class_data_length = sizeof(struct hf_rgb_tag);
    287 		fb->hf_u.hf_rgb.hf_flags = 0;	/* reserved for future use */
    288 
    289 		fb->hf_u.hf_rgb.hf_red_width = 5;
    290 		fb->hf_u.hf_rgb.hf_red_shift = 11;
    291 		fb->hf_u.hf_rgb.hf_green_width = 6;
    292 		fb->hf_u.hf_rgb.hf_green_shift = 5;
    293 		fb->hf_u.hf_rgb.hf_blue_width = 5;
    294 		fb->hf_u.hf_rgb.hf_blue_shift = 0;
    295 		fb->hf_u.hf_rgb.hf_alpha_width = 0;
    296 		fb->hf_u.hf_rgb.hf_alpha_shift = 0;
    297 		break;
    298 
    299 	default:
    300 		printf("unsupported type %d.\n", bootinfo->fb_type);
    301 		return (-1);
    302 		break;
    303 	}
    304 
    305 	return (0); /* no error */
    306 }
    307 
    308 static void
    309 sed1356_power(int why, void *arg)
    310 {
    311 	struct sed1356_softc *sc = arg;
    312 
    313 	switch (why) {
    314 	case PWR_SUSPEND:
    315 	case PWR_STANDBY:
    316 		sc->sc_powerstate |= PWRSTAT_SUSPEND;
    317 		sed1356_update_powerstate(sc, PWRSTAT_ALL);
    318 		break;
    319 	case PWR_RESUME:
    320 		sc->sc_powerstate &= ~PWRSTAT_SUSPEND;
    321 		sed1356_update_powerstate(sc, PWRSTAT_ALL);
    322 		break;
    323 	}
    324 }
    325 
    326 static void
    327 sed1356_update_powerstate(struct sed1356_softc *sc, int updates)
    328 {
    329 	if (updates & PWRSTAT_LCD)
    330 		config_hook_call(CONFIG_HOOK_POWERCONTROL,
    331 		    CONFIG_HOOK_POWERCONTROL_LCD,
    332 		    (void*)!(sc->sc_powerstate &
    333 				(PWRSTAT_VIDEOOFF|PWRSTAT_SUSPEND)));
    334 
    335 	if (updates & PWRSTAT_BACKLIGHT)
    336 		config_hook_call(CONFIG_HOOK_POWERCONTROL,
    337 		    CONFIG_HOOK_POWERCONTROL_LCDLIGHT,
    338 		    (void*)(!(sc->sc_powerstate &
    339 				(PWRSTAT_VIDEOOFF|PWRSTAT_SUSPEND)) &&
    340 			     (sc->sc_powerstate & PWRSTAT_BACKLIGHT)));
    341 }
    342 
    343 int
    344 sed1356_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct lwp *l)
    345 {
    346 	struct sed1356_softc *sc = (struct sed1356_softc *)v;
    347 	struct hpcfb_fbconf *fbconf;
    348 	struct hpcfb_dspconf *dspconf;
    349 	struct wsdisplay_param *dispparam;
    350 
    351 	switch (cmd) {
    352 	case WSDISPLAYIO_GETCMAP:
    353 	case WSDISPLAYIO_PUTCMAP:
    354 		/*
    355 		 * XXX should be able to handle color map in 4/8 bpp mode.
    356 		 */
    357 		return (EINVAL);
    358 
    359 	case WSDISPLAYIO_SVIDEO:
    360 		if (*(int *)data == WSDISPLAYIO_VIDEO_OFF)
    361 			sc->sc_powerstate |= PWRSTAT_VIDEOOFF;
    362 		else
    363 			sc->sc_powerstate &= ~PWRSTAT_VIDEOOFF;
    364 		sed1356_update_powerstate(sc, PWRSTAT_ALL);
    365 		return 0;
    366 
    367 	case WSDISPLAYIO_GVIDEO:
    368 		*(int *)data = (sc->sc_powerstate&PWRSTAT_VIDEOOFF) ?
    369 				WSDISPLAYIO_VIDEO_OFF:WSDISPLAYIO_VIDEO_ON;
    370 		return 0;
    371 
    372 
    373 	case WSDISPLAYIO_GETPARAM:
    374 		dispparam = (struct wsdisplay_param*)data;
    375 		switch (dispparam->param) {
    376 		case WSDISPLAYIO_PARAM_BACKLIGHT:
    377 			VPRINTF(("sed1356_ioctl: GET:BACKLIGHT\n"));
    378 			sed1356_init_brightness(sc, 0);
    379 			sed1356_init_backlight(sc, 0);
    380 			VPRINTF(("sed1356_ioctl: GET:(real)BACKLIGHT %d\n",
    381 				 (sc->sc_powerstate&PWRSTAT_BACKLIGHT)? 1: 0));
    382 			dispparam->min = 0;
    383 			dispparam->max = 1;
    384 			if (sc->sc_max_brightness > 0)
    385 				dispparam->curval = sc->sc_brightness > 0? 1: 0;
    386 			else
    387 				dispparam->curval =
    388 				    (sc->sc_powerstate&PWRSTAT_BACKLIGHT) ? 1: 0;
    389 			VPRINTF(("sed1356_ioctl: GET:BACKLIGHT:%d(%s)\n",
    390 				dispparam->curval,
    391 				sc->sc_max_brightness > 0? "brightness": "light"));
    392 			return 0;
    393 			break;
    394 		case WSDISPLAYIO_PARAM_CONTRAST:
    395 			VPRINTF(("sed1356_ioctl: GET:CONTRAST\n"));
    396 			sed1356_init_contrast(sc, 0);
    397 			if (sc->sc_max_contrast > 0) {
    398 				dispparam->min = 0;
    399 				dispparam->max = sc->sc_max_contrast;
    400 				dispparam->curval = sc->sc_contrast;
    401 				VPRINTF(("sed1356_ioctl: GET:CONTRAST max=%d, current=%d\n", sc->sc_max_contrast, sc->sc_contrast));
    402 				return 0;
    403 			} else {
    404 				VPRINTF(("sed1356_ioctl: GET:CONTRAST EINVAL\n"));
    405 				return (EINVAL);
    406 			}
    407 			break;
    408 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
    409 			VPRINTF(("sed1356_ioctl: GET:BRIGHTNESS\n"));
    410 			sed1356_init_brightness(sc, 0);
    411 			if (sc->sc_max_brightness > 0) {
    412 				dispparam->min = 0;
    413 				dispparam->max = sc->sc_max_brightness;
    414 				dispparam->curval = sc->sc_brightness;
    415 				VPRINTF(("sed1356_ioctl: GET:BRIGHTNESS max=%d, current=%d\n", sc->sc_max_brightness, sc->sc_brightness));
    416 				return 0;
    417 			} else {
    418 				VPRINTF(("sed1356_ioctl: GET:BRIGHTNESS EINVAL\n"));
    419 				return (EINVAL);
    420 			}
    421 			return (EINVAL);
    422 		default:
    423 			return (EINVAL);
    424 		}
    425 		return (0);
    426 
    427 	case WSDISPLAYIO_SETPARAM:
    428 		dispparam = (struct wsdisplay_param*)data;
    429 		switch (dispparam->param) {
    430 		case WSDISPLAYIO_PARAM_BACKLIGHT:
    431 			VPRINTF(("sed1356_ioctl: SET:BACKLIGHT\n"));
    432 			if (dispparam->curval < 0 ||
    433 			    1 < dispparam->curval)
    434 				return (EINVAL);
    435 			sed1356_init_brightness(sc, 0);
    436 			VPRINTF(("sed1356_ioctl: SET:max brightness=%d\n", sc->sc_max_brightness));
    437 			if (sc->sc_max_brightness > 0) { /* dimmer */
    438 				if (dispparam->curval == 0){
    439 					sc->sc_brightness_save = sc->sc_brightness;
    440 					sed1356_set_brightness(sc, 0);	/* min */
    441 				} else {
    442 					if (sc->sc_brightness_save == 0)
    443 						sc->sc_brightness_save = sc->sc_max_brightness;
    444 					sed1356_set_brightness(sc, sc->sc_brightness_save);
    445 				}
    446 				VPRINTF(("sed1356_ioctl: SET:BACKLIGHT:brightness=%d\n", sc->sc_brightness));
    447 			} else { /* off */
    448 				if (dispparam->curval == 0)
    449 					sc->sc_powerstate &= ~PWRSTAT_BACKLIGHT;
    450 				else
    451 					sc->sc_powerstate |= PWRSTAT_BACKLIGHT;
    452 				VPRINTF(("sed1356_ioctl: SET:BACKLIGHT:powerstate %d\n",
    453 						(sc->sc_powerstate & PWRSTAT_BACKLIGHT)?1:0));
    454 				sed1356_update_powerstate(sc, PWRSTAT_BACKLIGHT);
    455 				VPRINTF(("sed1356_ioctl: SET:BACKLIGHT:%d\n",
    456 					(sc->sc_powerstate & PWRSTAT_BACKLIGHT)?1:0));
    457 			}
    458 			return 0;
    459 			break;
    460 		case WSDISPLAYIO_PARAM_CONTRAST:
    461 			VPRINTF(("sed1356_ioctl: SET:CONTRAST\n"));
    462 			sed1356_init_contrast(sc, 0);
    463 			if (dispparam->curval < 0 ||
    464 			    sc->sc_max_contrast < dispparam->curval)
    465 				return (EINVAL);
    466 			if (sc->sc_max_contrast > 0) {
    467 #ifdef SED_DEBUG
    468 				int org = sc->sc_contrast;
    469 #endif
    470 				sed1356_set_contrast(sc, dispparam->curval);
    471 				VPRINTF(("sed1356_ioctl: SET:CONTRAST org=%d, current=%d\n", org, sc->sc_contrast));
    472 				return 0;
    473 			} else {
    474 				VPRINTF(("sed1356_ioctl: SET:CONTRAST EINVAL\n"));
    475 				return (EINVAL);
    476 			}
    477 			break;
    478 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
    479 			VPRINTF(("sed1356_ioctl: SET:BRIGHTNESS\n"));
    480 			sed1356_init_brightness(sc, 0);
    481 			if (dispparam->curval < 0 ||
    482 			    sc->sc_max_brightness < dispparam->curval)
    483 				return (EINVAL);
    484 			if (sc->sc_max_brightness > 0) {
    485 #ifdef SED_DEBUG
    486 				int org = sc->sc_brightness;
    487 #endif
    488 				sed1356_set_brightness(sc, dispparam->curval);
    489 				VPRINTF(("sed1356_ioctl: SET:BRIGHTNESS org=%d, current=%d\n", org, sc->sc_brightness));
    490 				return 0;
    491 			} else {
    492 				VPRINTF(("sed1356_ioctl: SET:BRIGHTNESS EINVAL\n"));
    493 				return (EINVAL);
    494 			}
    495 			break;
    496 		default:
    497 			return (EINVAL);
    498 		}
    499 		return (0);
    500 
    501 	case HPCFBIO_GCONF:
    502 		fbconf = (struct hpcfb_fbconf *)data;
    503 		if (fbconf->hf_conf_index != 0 &&
    504 		    fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
    505 			return (EINVAL);
    506 		}
    507 		*fbconf = sc->sc_fbconf;	/* structure assignment */
    508 		return (0);
    509 	case HPCFBIO_SCONF:
    510 		fbconf = (struct hpcfb_fbconf *)data;
    511 		if (fbconf->hf_conf_index != 0 &&
    512 		    fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
    513 			return (EINVAL);
    514 		}
    515 		/*
    516 		 * nothing to do because we have only one configuration
    517 		 */
    518 		return (0);
    519 	case HPCFBIO_GDSPCONF:
    520 		dspconf = (struct hpcfb_dspconf *)data;
    521 		if ((dspconf->hd_unit_index != 0 &&
    522 		     dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
    523 		    (dspconf->hd_conf_index != 0 &&
    524 		     dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
    525 			return (EINVAL);
    526 		}
    527 		*dspconf = sc->sc_dspconf;	/* structure assignment */
    528 		return (0);
    529 	case HPCFBIO_SDSPCONF:
    530 		dspconf = (struct hpcfb_dspconf *)data;
    531 		if ((dspconf->hd_unit_index != 0 &&
    532 		     dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
    533 		    (dspconf->hd_conf_index != 0 &&
    534 		     dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
    535 			return (EINVAL);
    536 		}
    537 		/*
    538 		 * nothing to do
    539 		 * because we have only one unit and one configuration
    540 		 */
    541 		return (0);
    542 	case HPCFBIO_GOP:
    543 	case HPCFBIO_SOP:
    544 		/*
    545 		 * curently not implemented...
    546 		 */
    547 		return (EINVAL);
    548 	}
    549 
    550 	return (EPASSTHROUGH);
    551 }
    552 
    553 paddr_t
    554 sed1356_mmap(void *ctx, off_t offset, int prot)
    555 {
    556 	struct sed1356_softc *sc = (struct sed1356_softc *)ctx;
    557 
    558 	if (offset < 0 ||
    559 	    (sc->sc_fbconf.hf_bytes_per_plane +
    560 		sc->sc_fbconf.hf_offset) <  offset)
    561 		return -1;
    562 
    563 	return __BTOP((u_long)bootinfo->fb_addr + offset);
    564 }
    565 
    566 
    567 void
    568 sed1356_init_backlight(struct sed1356_softc *sc, int inattach)
    569 {
    570 	int val = -1;
    571 
    572 	if (sc->sc_lcd_inited&BACKLIGHT_INITED)
    573 		return;
    574 
    575 	if (config_hook_call(CONFIG_HOOK_GET,
    576 	     CONFIG_HOOK_POWER_LCDLIGHT, &val) != -1) {
    577 		/* we can get real light state */
    578 		VPRINTF(("sed1356_init_backlight: real backlight=%d\n", val));
    579 		if (val == 0)
    580 			sc->sc_powerstate &= ~PWRSTAT_BACKLIGHT;
    581 		else
    582 			sc->sc_powerstate |= PWRSTAT_BACKLIGHT;
    583 		sc->sc_lcd_inited |= BACKLIGHT_INITED;
    584 	} else if (inattach) {
    585 		/*
    586 		   we cannot get real light state in attach time
    587 		   because light device not yet attached.
    588 		   we will retry in !inattach.
    589 		   temporary assume light is on.
    590 		 */
    591 		sc->sc_powerstate |= PWRSTAT_BACKLIGHT;
    592 	} else {
    593 		/* we cannot get real light state, so work by myself state */
    594 		sc->sc_lcd_inited |= BACKLIGHT_INITED;
    595 	}
    596 }
    597 
    598 void
    599 sed1356_init_brightness(struct sed1356_softc *sc, int inattach)
    600 {
    601 	int val = -1;
    602 
    603 	if (sc->sc_lcd_inited&BRIGHTNESS_INITED)
    604 		return;
    605 
    606 	VPRINTF(("sed1356_init_brightness\n"));
    607 	if (config_hook_call(CONFIG_HOOK_GET,
    608 	     CONFIG_HOOK_BRIGHTNESS_MAX, &val) != -1) {
    609 		/* we can get real brightness max */
    610 		VPRINTF(("sed1356_init_brightness: real brightness max=%d\n", val));
    611 		sc->sc_max_brightness = val;
    612 		val = -1;
    613 		if (config_hook_call(CONFIG_HOOK_GET,
    614 		     CONFIG_HOOK_BRIGHTNESS, &val) != -1) {
    615 			/* we can get real brightness */
    616 			VPRINTF(("sed1356_init_brightness: real brightness=%d\n", val));
    617 			sc->sc_brightness_save = sc->sc_brightness = val;
    618 		} else {
    619 			sc->sc_brightness_save =
    620 			sc->sc_brightness = sc->sc_max_brightness;
    621 		}
    622 		sc->sc_lcd_inited |= BRIGHTNESS_INITED;
    623 	} else if (inattach) {
    624 		/*
    625 		   we cannot get real brightness in attach time
    626 		   because brightness device not yet attached.
    627 		   we will retry in !inattach.
    628 		 */
    629 		sc->sc_max_brightness = -1;
    630 		sc->sc_brightness = -1;
    631 		sc->sc_brightness_save = -1;
    632 	} else {
    633 		/* we cannot get real brightness */
    634 		sc->sc_lcd_inited |= BRIGHTNESS_INITED;
    635 	}
    636 
    637 	return;
    638 }
    639 
    640 void
    641 sed1356_init_contrast(struct sed1356_softc *sc, int inattach)
    642 {
    643 	int val = -1;
    644 
    645 	if (sc->sc_lcd_inited&CONTRAST_INITED)
    646 		return;
    647 
    648 	VPRINTF(("sed1356_init_contrast\n"));
    649 	if (config_hook_call(CONFIG_HOOK_GET,
    650 	     CONFIG_HOOK_CONTRAST_MAX, &val) != -1) {
    651 		/* we can get real contrast max */
    652 		VPRINTF(("sed1356_init_contrast: real contrast max=%d\n", val));
    653 		sc->sc_max_contrast = val;
    654 		val = -1;
    655 		if (config_hook_call(CONFIG_HOOK_GET,
    656 		     CONFIG_HOOK_CONTRAST, &val) != -1) {
    657 			/* we can get real contrast */
    658 			VPRINTF(("sed1356_init_contrast: real contrast=%d\n", val));
    659 			sc->sc_contrast = val;
    660 		} else {
    661 			sc->sc_contrast = sc->sc_max_contrast;
    662 		}
    663 		sc->sc_lcd_inited |= CONTRAST_INITED;
    664 	} else if (inattach) {
    665 		/*
    666 		   we cannot get real contrast in attach time
    667 		   because contrast device not yet attached.
    668 		   we will retry in !inattach.
    669 		 */
    670 		sc->sc_max_contrast = -1;
    671 		sc->sc_contrast = -1;
    672 	} else {
    673 		/* we cannot get real contrast */
    674 		sc->sc_lcd_inited |= CONTRAST_INITED;
    675 	}
    676 
    677 	return;
    678 }
    679 
    680 void
    681 sed1356_set_brightness(struct sed1356_softc *sc, int val)
    682 {
    683 	sc->sc_brightness = val;
    684 
    685 	config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_BRIGHTNESS, &val);
    686 	if (config_hook_call(CONFIG_HOOK_GET,
    687 	     CONFIG_HOOK_BRIGHTNESS, &val) != -1) {
    688 		sc->sc_brightness = val;
    689 	}
    690 }
    691 
    692 void
    693 sed1356_set_contrast(struct sed1356_softc *sc, int val)
    694 {
    695 	sc->sc_contrast = val;
    696 
    697 	config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_CONTRAST, &val);
    698 	if (config_hook_call(CONFIG_HOOK_GET,
    699 	     CONFIG_HOOK_CONTRAST, &val) != -1) {
    700 		sc->sc_contrast = val;
    701 	}
    702 }
    703 
    704 void
    705 sed1356_toggle_lcdlight(void)
    706 {
    707 	extern struct cfdriver sed_cd;
    708 	struct sed1356_softc *sc = sed_cd.cd_devs[0];
    709 
    710 	if (sc->sc_powerstate & PWRSTAT_VIDEOOFF)
    711 		sc->sc_powerstate &= ~PWRSTAT_VIDEOOFF;
    712 	else
    713 		sc->sc_powerstate |= PWRSTAT_VIDEOOFF;
    714 
    715 	sed1356_update_powerstate(sc, PWRSTAT_ALL);
    716 }
    717