Home | History | Annotate | Line # | Download | only in dev
j720lcd.c revision 1.2
      1 /*	$NetBSD: j720lcd.c,v 1.2 2006/06/27 14:18:00 peter Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by IWAMOTO Toshihiro.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /* Jornada 720 LCD screen driver. */
     40 
     41 #include <sys/cdefs.h>
     42 __KERNEL_RCSID(0, "$NetBSD: j720lcd.c,v 1.2 2006/06/27 14:18:00 peter Exp $");
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/device.h>
     47 #include <sys/kernel.h>
     48 
     49 #include <machine/config_hook.h>
     50 #include <machine/platid.h>
     51 #include <machine/platid_mask.h>
     52 
     53 #include <arm/sa11x0/sa11x0_var.h>
     54 #include <arm/sa11x0/sa11x0_gpioreg.h>
     55 #include <arm/sa11x0/sa11x0_ppcreg.h>
     56 #include <arm/sa11x0/sa11x0_sspreg.h>
     57 
     58 #include <hpcarm/dev/j720sspvar.h>
     59 #include <hpcarm/dev/sed1356var.h>
     60 
     61 #ifdef DEBUG
     62 #define DPRINTF(arg)	printf arg
     63 #else
     64 #define DPRINTF(arg)	/* nothing */
     65 #endif
     66 
     67 struct j720lcd_softc {
     68 	struct device		sc_dev;
     69 
     70 	struct j720ssp_softc	*sc_ssp;
     71 };
     72 
     73 static int	j720lcd_match(struct device *, struct cfdata *, void *);
     74 static void	j720lcd_attach(struct device *, struct device *, void *);
     75 
     76 static int	j720lcd_param(void *, int, long, void *);
     77 int		j720lcd_power(void *, int, long, void *);
     78 
     79 CFATTACH_DECL(j720lcd, sizeof(struct j720lcd_softc),
     80     j720lcd_match, j720lcd_attach, NULL, NULL);
     81 
     82 
     83 static int
     84 j720lcd_match(struct device *parent, struct cfdata *cf, void *aux)
     85 {
     86 
     87 	if (!platid_match(&platid, &platid_mask_MACH_HP_JORNADA_7XX))
     88 		return 0;
     89 	if (strcmp(cf->cf_name, "j720lcd") != 0)
     90 		return 0;
     91 
     92 	return 1;
     93 }
     94 
     95 static void
     96 j720lcd_attach(struct device *parent, struct device *self, void *aux)
     97 {
     98 	struct j720lcd_softc *sc = (struct j720lcd_softc *)self;
     99 	int brightness, contrast;
    100 
    101 	sc->sc_ssp = (struct j720ssp_softc *)parent;
    102 
    103 	/* LCD brightness hooks. */
    104 	config_hook(CONFIG_HOOK_SET, CONFIG_HOOK_BRIGHTNESS,
    105 		    CONFIG_HOOK_SHARE, j720lcd_param, sc);
    106 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_BRIGHTNESS,
    107 		    CONFIG_HOOK_SHARE, j720lcd_param, sc);
    108 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_BRIGHTNESS_MAX,
    109 		    CONFIG_HOOK_SHARE, j720lcd_param, sc);
    110 
    111 	/* LCD contrast hooks. */
    112 	config_hook(CONFIG_HOOK_SET, CONFIG_HOOK_CONTRAST,
    113 		    CONFIG_HOOK_SHARE, j720lcd_param, sc);
    114 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_CONTRAST,
    115 		    CONFIG_HOOK_SHARE, j720lcd_param, sc);
    116 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_CONTRAST_MAX,
    117 		    CONFIG_HOOK_SHARE, j720lcd_param, sc);
    118 
    119 	/* LCD power hook. */
    120 #if 0
    121 	config_hook(CONFIG_HOOK_POWERCONTROL,
    122 		    CONFIG_HOOK_POWERCONTROL_LCDLIGHT,
    123 		    CONFIG_HOOK_SHARE, j720lcd_power, sc);
    124 #endif
    125 
    126 	/* Get default brightness/contrast values. */
    127 	config_hook_call(CONFIG_HOOK_GET, CONFIG_HOOK_BRIGHTNESS, &brightness);
    128 	config_hook_call(CONFIG_HOOK_GET, CONFIG_HOOK_CONTRAST, &contrast);
    129 
    130 	printf(": brightness %d, contrast %d\n", brightness, contrast);
    131 }
    132 
    133 static int
    134 j720lcd_param(void *ctx, int type, long id, void *msg)
    135 {
    136 	struct j720lcd_softc *sc = ctx;
    137 	struct j720ssp_softc *ssp = sc->sc_ssp;
    138 	uint32_t data[2], len;
    139 	const int maxval = 255;
    140 	int i, s;
    141 
    142 	switch (type) {
    143 	case CONFIG_HOOK_GET:
    144 		switch (id) {
    145 		case CONFIG_HOOK_BRIGHTNESS_MAX:
    146 		case CONFIG_HOOK_CONTRAST_MAX:
    147 			*(int *)msg = maxval;
    148 			return 1;
    149 		case CONFIG_HOOK_BRIGHTNESS:
    150 			data[0] = 0x6b00;
    151 			data[1] = 0x8800;
    152 			len = 2;
    153 			break;
    154 		case CONFIG_HOOK_CONTRAST:
    155 			data[0] = 0x2b00;
    156 			data[1] = 0x8800;
    157 			len = 2;
    158 			break;
    159 		default:
    160 			return 0;
    161 		}
    162 		break;
    163 
    164 	case CONFIG_HOOK_SET:
    165 		switch (id) {
    166 		case CONFIG_HOOK_BRIGHTNESS:
    167 			if (*(int *)msg >= 0) {
    168 				data[0] = 0xcb00;
    169 				data[1] = maxval - *(int *)msg;
    170 				J720SSP_INVERT(data[1]);
    171 				data[1] <<= 8;
    172 				len = 2;
    173 			} else {
    174 				/* XXX hack */
    175 				data[0] = 0xfb00;
    176 				len = 1;
    177 			}
    178 			break;
    179 		case CONFIG_HOOK_CONTRAST:
    180 			data[0] = 0x8b00;
    181 			data[1] = maxval - *(int *)msg;
    182 			J720SSP_INVERT(data[1]);
    183 			data[1] <<= 8;
    184 			len = 2;
    185 			break;
    186 		default:
    187 			return 0;
    188 		}
    189 		break;
    190 
    191 	default:
    192 		return 0;
    193 	}
    194 
    195 	s = splbio();
    196 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PCR, 0x2000000);
    197 
    198 	for (i = 0; i < len; i++) {
    199 		if (j720ssp_readwrite(ssp, 1, data[i], &data[i], 500) < 0)
    200 			goto out;
    201 	}
    202 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000);
    203 	splx(s);
    204 
    205 	if (type == CONFIG_HOOK_SET)
    206 		return 1;
    207 
    208 	J720SSP_INVERT(data[1]);
    209 	*(int *)msg = maxval - data[1];
    210 
    211 	return 1;
    212 
    213 out:
    214 	bus_space_write_4(ssp->sc_iot, ssp->sc_gpioh, SAGPIO_PSR, 0x2000000);
    215 
    216 	/* reset SSP */
    217 	bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x307);
    218 	delay(100);
    219 	bus_space_write_4(ssp->sc_iot, ssp->sc_ssph, SASSP_CR0, 0x387);
    220 
    221 	splx(s);
    222 
    223 	DPRINTF(("j720lcd_param: error %x %x\n", data[0], data[1]));
    224 	return 0;
    225 
    226 }
    227 
    228 int
    229 j720lcd_power(void *ctx, int type, long id, void *msg)
    230 {
    231 	struct sed1356_softc *sc = ctx;
    232 	struct sa11x0_softc *psc = sc->sc_parent;
    233 	uint32_t reg;
    234 	int val;
    235 
    236 	if (type != CONFIG_HOOK_POWERCONTROL ||
    237 	    id != CONFIG_HOOK_POWERCONTROL_LCDLIGHT)
    238 		return 0;
    239 
    240 	sed1356_init_brightness(sc, 0);
    241 	sed1356_init_contrast(sc, 0);
    242 
    243 	if (msg) {
    244 		bus_space_write_1(sc->sc_iot, sc->sc_regh, 0x1f0, 0);
    245 
    246 		reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
    247 		reg |= 0x1;
    248 		bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
    249 		delay(50000);
    250 
    251 		val = sc->sc_contrast;
    252 		config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_CONTRAST, &val);
    253 		delay(100000);
    254 
    255 		reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
    256 		reg |= 0x4;
    257 		bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
    258 
    259 		val = sc->sc_brightness;
    260 		config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_BRIGHTNESS, &val);
    261 
    262 		reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
    263 		reg |= 0x2;
    264 		bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
    265 	} else {
    266 		reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
    267 		reg &= ~0x2;
    268 		bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
    269 		reg &= ~0x4;
    270 		bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
    271 		delay(100000);
    272 
    273 		val = -2;
    274 		config_hook_call(CONFIG_HOOK_SET, CONFIG_HOOK_BRIGHTNESS, &val);
    275 
    276 		bus_space_write_1(sc->sc_iot, sc->sc_regh, 0x1f0, 1);
    277 
    278 		delay(100000);
    279 		reg = bus_space_read_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR);
    280 		reg &= ~0x1;
    281 		bus_space_write_4(psc->sc_iot, psc->sc_ppch, SAPPC_PSR, reg);
    282 	}
    283 
    284 	return 1;
    285 }
    286