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