Home | History | Annotate | Line # | Download | only in dev
zlcd.c revision 1.3
      1 /*	$NetBSD: zlcd.c,v 1.3 2006/12/18 15:30:56 nonaka Exp $	*/
      2 /*	$OpenBSD: zaurus_lcd.c,v 1.20 2006/06/02 20:50:14 miod Exp $	*/
      3 /* NetBSD: lubbock_lcd.c,v 1.1 2003/08/09 19:38:53 bsh Exp */
      4 
      5 /*
      6  * Copyright (c) 2002, 2003  Genetec Corporation.  All rights reserved.
      7  * Written by Hiroyuki Bessho for Genetec Corporation.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. The name of Genetec Corporation may not be used to endorse or
     18  *    promote products derived from this software without specific prior
     19  *    written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * LCD driver for Sharp Zaurus (based on the Intel Lubbock driver).
     36  *
     37  * Controlling LCD is almost completely done through PXA2X0's
     38  * integrated LCD controller.  Codes for it is arm/xscale/pxa2x0_lcd.c.
     39  *
     40  * Codes in this file provide platform specific things including:
     41  *   LCD on/off switch and backlight brightness
     42  *   LCD panel geometry
     43  */
     44 
     45 #include <sys/cdefs.h>
     46 __KERNEL_RCSID(0, "$NetBSD: zlcd.c,v 1.3 2006/12/18 15:30:56 nonaka Exp $");
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/conf.h>
     51 #include <sys/uio.h>
     52 #include <sys/malloc.h>
     53 
     54 #include <dev/cons.h>
     55 #include <dev/wscons/wsconsio.h>
     56 #include <dev/wscons/wsdisplayvar.h>
     57 #include <dev/wscons/wscons_callbacks.h>
     58 
     59 #include <machine/bus.h>
     60 #include <arm/xscale/pxa2x0var.h>
     61 #include <arm/xscale/pxa2x0reg.h>
     62 #include <arm/xscale/pxa2x0_lcd.h>
     63 
     64 #include <zaurus/dev/scoopvar.h>
     65 #include <zaurus/dev/zsspvar.h>
     66 #include <zaurus/zaurus/zaurus_var.h>
     67 
     68 /*
     69  * wsdisplay glue
     70  */
     71 static struct pxa2x0_wsscreen_descr lcd_std_screen = {
     72 	.c = {
     73 		.name = "std",
     74 		.textops = &pxa2x0_lcd_emulops,
     75 		.fontwidth = 8,
     76 		.fontheight = 16,
     77 		.capabilities = WSSCREEN_WSCOLORS,
     78 	},
     79 	.depth = 16,			/* bits per pixel */
     80 	.flags = 0/*RI_ROTATE_CW*/,	/* quarter clockwise rotation */
     81 };
     82 
     83 static const struct wsscreen_descr *lcd_scr_descr[] = {
     84 	&lcd_std_screen.c
     85 };
     86 
     87 static const struct wsscreen_list lcd_screen_list = {
     88 	.nscreens = __arraycount(lcd_scr_descr),
     89 	.screens = lcd_scr_descr,
     90 };
     91 
     92 static int	lcd_ioctl(void *, void *, u_long, caddr_t, int, struct lwp *);
     93 static int	lcd_param(struct pxa2x0_lcd_softc *, u_long,
     94 		    struct wsdisplay_param *);
     95 static int	lcd_show_screen(void *, void *, int,
     96 		    void (*)(void *, int, int), void *);
     97 
     98 struct wsdisplay_accessops lcd_accessops = {
     99 	lcd_ioctl,
    100 	pxa2x0_lcd_mmap,
    101 	pxa2x0_lcd_alloc_screen,
    102 	pxa2x0_lcd_free_screen,
    103 	lcd_show_screen,
    104 	NULL,
    105 	NULL,
    106 	NULL,
    107 };
    108 
    109 #define CURRENT_DISPLAY &sharp_zaurus_C3000
    110 
    111 const struct lcd_panel_geometry sharp_zaurus_C3000 =
    112 {
    113 	480,			/* Width */
    114 	640,			/* Height */
    115 	0,			/* No extra lines */
    116 
    117 	LCDPANEL_ACTIVE | LCDPANEL_VSP | LCDPANEL_HSP,
    118 	1,			/* clock divider */
    119 	0,			/* AC bias pin freq */
    120 
    121 	0x28,			/* horizontal sync pulse width */
    122 	0x2e,			/* BLW */
    123 	0x7d,			/* ELW */
    124 
    125 	2,			/* vertical sync pulse width */
    126 	1,			/* BFW */
    127 	0,			/* EFW */
    128 };
    129 
    130 struct sharp_lcd_backlight {
    131 	int	duty;		/* LZ9JG18 DAC value */
    132 	int	cont;		/* BACKLIGHT_CONT signal */
    133 	int	on;		/* BACKLIGHT_ON signal */
    134 };
    135 
    136 #define CURRENT_BACKLIGHT sharp_zaurus_C3000_bl
    137 
    138 const struct sharp_lcd_backlight sharp_zaurus_C3000_bl[] = {
    139 	{ 0x00, 0,  0 },	/* 0:     Off */
    140 	{ 0x00, 0,  1 },	/* 1:      0% */
    141 	{ 0x01, 0,  1 },	/* 2:     20% */
    142 	{ 0x07, 0,  1 },	/* 3:     40% */
    143 	{ 0x01, 1,  1 },	/* 4:     60% */
    144 	{ 0x07, 1,  1 },	/* 5:     80% */
    145 	{ 0x11, 1,  1 },	/* 6:    100% */
    146 	{  -1, -1, -1 },	/* 7: Invalid */
    147 };
    148 
    149 static int	lcd_match(struct device *, struct cfdata *, void *);
    150 static void	lcd_attach(struct device *, struct device *, void *);
    151 
    152 CFATTACH_DECL(zlcd, sizeof(struct pxa2x0_lcd_softc),
    153 	lcd_match, lcd_attach, NULL, NULL);
    154 
    155 void	lcd_cnattach(void);
    156 int	lcd_max_brightness(void);
    157 int	lcd_get_brightness(void);
    158 void	lcd_set_brightness(int);
    159 void	lcd_set_brightness_internal(int);
    160 int	lcd_get_backlight(void);
    161 void	lcd_set_backlight(int);
    162 void	lcd_blank(int);
    163 void	lcd_power(int, void *);
    164 
    165 static int
    166 lcd_match(struct device *parent, struct cfdata *cf, void *aux)
    167 {
    168 
    169 	return 1;
    170 }
    171 
    172 static void
    173 lcd_attach(struct device *parent, struct device *self, void *aux)
    174 {
    175 	struct pxa2x0_lcd_softc *sc = (struct pxa2x0_lcd_softc *)self;
    176 	struct wsemuldisplaydev_attach_args aa;
    177 
    178 	pxa2x0_lcd_attach_sub(sc, aux, CURRENT_DISPLAY);
    179 
    180 	aa.console = glass_console;
    181 	aa.scrdata = &lcd_screen_list;
    182 	aa.accessops = &lcd_accessops;
    183 	aa.accesscookie = sc;
    184 
    185 	(void) config_found(self, &aa, wsemuldisplaydevprint);
    186 
    187 	/* Start with approximately 40% of full brightness. */
    188 	lcd_set_brightness(3);
    189 
    190 	(void) powerhook_establish(sc->dev.dv_xname, lcd_power, sc);
    191 }
    192 
    193 void
    194 lcd_cnattach(void)
    195 {
    196 
    197 	pxa2x0_lcd_cnattach(&lcd_std_screen, CURRENT_DISPLAY);
    198 }
    199 
    200 /*
    201  * wsdisplay accessops overrides
    202  */
    203 static int
    204 lcd_ioctl(void *v, void *vs, u_long cmd, caddr_t data, int flag, struct lwp *l)
    205 {
    206 	struct pxa2x0_lcd_softc *sc = (struct pxa2x0_lcd_softc *)v;
    207 	int res = EINVAL;
    208 
    209 	switch (cmd) {
    210 	case WSDISPLAYIO_GETPARAM:
    211 	case WSDISPLAYIO_SETPARAM:
    212 		res = lcd_param(sc, cmd, (struct wsdisplay_param *)data);
    213 		break;
    214 	}
    215 
    216 	if (res == EINVAL)
    217 		res = pxa2x0_lcd_ioctl(v, vs, cmd, data, flag, l);
    218 	return res;
    219 }
    220 
    221 static int
    222 lcd_show_screen(void *v, void *cookie, int waitok,
    223     void (*cb_func)(void *, int, int), void *cb_arg)
    224 {
    225 	int error;
    226 
    227 	error = pxa2x0_lcd_show_screen(v, cookie, waitok, cb_func, cb_arg);
    228 	if (error)
    229 		return (error);
    230 
    231 	/* Turn on LCD */
    232 	lcd_set_brightness(lcd_get_brightness());
    233 
    234 	return 0;
    235 }
    236 
    237 /*
    238  * wsdisplay I/O controls
    239  */
    240 static int
    241 lcd_param(struct pxa2x0_lcd_softc *sc, u_long cmd,
    242     struct wsdisplay_param *dp)
    243 {
    244 	int res = EINVAL;
    245 
    246 	switch (dp->param) {
    247 	case WSDISPLAYIO_PARAM_BACKLIGHT:
    248 		if (cmd == WSDISPLAYIO_GETPARAM) {
    249 			dp->min = 0;
    250 			dp->max = 1;
    251 			dp->curval = lcd_get_backlight();
    252 			res = 0;
    253 		} else if (cmd == WSDISPLAYIO_SETPARAM) {
    254 			lcd_set_backlight(dp->curval);
    255 			res = 0;
    256 		}
    257 		break;
    258 
    259 	case WSDISPLAYIO_PARAM_CONTRAST:
    260 		/* unsupported */
    261 		res = ENOTTY;
    262 		break;
    263 
    264 	case WSDISPLAYIO_PARAM_BRIGHTNESS:
    265 		if (cmd == WSDISPLAYIO_GETPARAM) {
    266 			dp->min = 1;
    267 			dp->max = lcd_max_brightness();
    268 			dp->curval = lcd_get_brightness();
    269 			res = 0;
    270 		} else if (cmd == WSDISPLAYIO_SETPARAM) {
    271 			lcd_set_brightness(dp->curval);
    272 			res = 0;
    273 		}
    274 		break;
    275 	}
    276 
    277 	return res;
    278 }
    279 
    280 /*
    281  * LCD backlight
    282  */
    283 
    284 static	int lcdbrightnesscurval = 1;
    285 static	int lcdislit = 1;
    286 static	int lcdisblank = 0;
    287 
    288 int
    289 lcd_max_brightness(void)
    290 {
    291 	int i;
    292 
    293 	for (i = 0; CURRENT_BACKLIGHT[i].duty != -1; i++)
    294 		continue;
    295 	return i - 1;
    296 }
    297 
    298 int
    299 lcd_get_brightness(void)
    300 {
    301 
    302 	return lcdbrightnesscurval;
    303 }
    304 
    305 void
    306 lcd_set_brightness(int newval)
    307 {
    308 	int maxval;
    309 
    310 	maxval = lcd_max_brightness();
    311 	if (newval < 0)
    312 		newval = 0;
    313 	else if (newval > maxval)
    314 		newval = maxval;
    315 
    316 	if (lcd_get_backlight() && !lcdisblank)
    317 		lcd_set_brightness_internal(newval);
    318 
    319 	if (newval > 0)
    320 		lcdbrightnesscurval = newval;
    321 }
    322 
    323 void
    324 lcd_set_brightness_internal(int newval)
    325 {
    326 	static int curval = 1;
    327 	int i;
    328 
    329 	/*
    330 	 * It appears that the C3000 backlight can draw too much power if we
    331 	 * switch it from a low to a high brightness.  Increasing brightness
    332 	 * in steps avoids this issue.
    333 	 */
    334 	if (newval > curval) {
    335 		for (i = curval + 1; i <= newval; i++) {
    336 			(void)zssp_ic_send(ZSSP_IC_LZ9JG18,
    337 			    CURRENT_BACKLIGHT[i].duty);
    338 			scoop_set_backlight(CURRENT_BACKLIGHT[i].on,
    339 			    CURRENT_BACKLIGHT[i].cont);
    340 			delay(5000);
    341 		}
    342 	} else {
    343 		(void)zssp_ic_send(ZSSP_IC_LZ9JG18,
    344 		    CURRENT_BACKLIGHT[newval].duty);
    345 		scoop_set_backlight(CURRENT_BACKLIGHT[newval].on,
    346 		    CURRENT_BACKLIGHT[newval].cont);
    347 	}
    348 
    349 	curval = newval;
    350 }
    351 
    352 int
    353 lcd_get_backlight(void)
    354 {
    355 
    356 	return lcdislit;
    357 }
    358 
    359 void
    360 lcd_set_backlight(int onoff)
    361 {
    362 
    363 	if (!onoff) {
    364 		lcd_set_brightness(0);
    365 		lcdislit = 0;
    366 	} else {
    367 		lcdislit = 1;
    368 		lcd_set_brightness(lcd_get_brightness());
    369 	}
    370 }
    371 
    372 void
    373 lcd_blank(int blank)
    374 {
    375 
    376 	if (blank) {
    377 		lcd_set_brightness(0);
    378 		lcdisblank = 1;
    379 	} else {
    380 		lcdisblank = 0;
    381 		lcd_set_brightness(lcd_get_brightness());
    382 	}
    383 }
    384 
    385 void
    386 lcd_power(int why, void *v)
    387 {
    388 
    389 	switch (why) {
    390 	case PWR_SUSPEND:
    391 	case PWR_STANDBY:
    392 		lcd_set_brightness(0);
    393 		pxa2x0_lcd_power(why, v);
    394 		break;
    395 
    396 	case PWR_RESUME:
    397 		pxa2x0_lcd_power(why, v);
    398 		lcd_set_brightness(lcd_get_brightness());
    399 		break;
    400 	}
    401 }
    402