Home | History | Annotate | Line # | Download | only in dev
psh3lcd.c revision 1.2.18.3
      1 /*	$NetBSD: psh3lcd.c,v 1.2.18.3 2007/02/26 09:06:39 yamt Exp $	*/
      2 /*
      3  * Copyright (c) 2005 KIYOHARA Takashi
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  * POSSIBILITY OF SUCH DAMAGE.
     26  *
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 
     31 #include <sys/types.h>
     32 #include <sys/param.h>
     33 #include <sys/device.h>
     34 #include <sys/errno.h>
     35 #include <sys/kernel.h>
     36 #include <sys/systm.h>
     37 #include <sys/callout.h>
     38 
     39 #include <machine/platid.h>
     40 #include <machine/platid_mask.h>
     41 
     42 #include <machine/config_hook.h>
     43 
     44 #include <sh3/pfcreg.h>
     45 #include <hpcsh/dev/hd64461/hd64461var.h>
     46 #include <hpcsh/dev/hd64461/hd64461reg.h>
     47 #include <hpcsh/dev/hd64461/hd64461gpioreg.h>
     48 
     49 
     50 /*
     51  * LCD contrast INC#: controlled by pin 0 in HD64461 GPIO port A.
     52  * LCD contrast  CS#: controlled by pin 1 in HD64461 GPIO port A.
     53  * LCD contrast U/D#: controlled by pin 0 in SH7709 GPIO port D.
     54  *   0 - down
     55  *   1 - up
     56  */
     57 #define PSH3LCD_CONTRAST_INC		0x01
     58 #define PSH3LCD_CONTRAST_CS		0x02
     59 #define PSH3LCD_CONTRAST_UD		0x01
     60 
     61 /*
     62  * LCD brightness: controled by HG71C105FE at AREA2.
     63  *	XXXX: That is custom IC. We don't know spec.  X-<
     64  */
     65 #define PSH3LCD_BRIGHTNESS_REG0		0xaa000072
     66 #define PSH3LCD_BRIGHTNESS_REG1		0xaa000150
     67 #define PSH3LCD_BRIGHTNESS_REG2		0xaa000152
     68 
     69 /* brightness control data */
     70 static const struct psh3lcd_x0_bcd {	/* 50PA, 30PA */
     71 	uint8_t reg0;
     72 	uint8_t reg1;
     73 	uint8_t reg2;
     74 } psh3lcd_x0_bcd[] = {
     75 	{ 0x05, 0x08, 0x1e },
     76 	{ 0x05, 0x1d, 0x09 },
     77 	{ 0x04, 0x1e, 0x1e },
     78 	{ 0x06, 0x1e, 0x1e },
     79 	{ 0x07, 0x1e, 0x1e },
     80 	{ 0x00, 0x00, 0x00 }
     81 };
     82 static const struct psh3lcd_xx0_bcd {	/* 200JC */
     83 	uint8_t reg1;
     84 	uint8_t reg2;
     85 } psh3lcd_xx0_bcd[] = {
     86 	{ 0x33, 0x0c },
     87 	{ 0x2d, 0x12 },
     88 	{ 0x26, 0x19 },
     89 	{ 0x20, 0x1f },
     90 	{ 0x1a, 0x25 },
     91 	{ 0x13, 0x2c },
     92 	{ 0x0d, 0x32 },
     93 	{ 0x07, 0x38 },
     94 	{ 0x01, 0x00 },
     95 	{ 0x00, 0x00 }
     96 };
     97 
     98 #define PSH3LCD_XX0_BRIGHTNESS_MAX	8
     99 #define PSH3LCD_X0_BRIGHTNESS_MAX	4
    100 
    101 #define PSH3LCD_CONTRAST_MAX		2	/* XXX */
    102 #define PSH3LCD_CONTRAST		1
    103 #define PSH3LCD_CONTRAST_UP		2
    104 #define PSH3LCD_CONTRAST_DOWN		0
    105 
    106 #define BCD_NO_MATCH	(-1)
    107 
    108 
    109 struct psh3lcd_softc {
    110 	struct device sc_dev;
    111 	int sc_brightness;
    112 	int sc_brightness_max;
    113 	void (*sc_set_brightness)(int);
    114 };
    115 
    116 static int psh3lcd_match(struct device *, struct cfdata *, void *);
    117 static void psh3lcd_attach(struct device *, struct device *, void *);
    118 
    119 CFATTACH_DECL(psh3lcd, sizeof(struct psh3lcd_softc),
    120     psh3lcd_match, psh3lcd_attach, NULL, NULL);
    121 
    122 
    123 static inline int psh3lcd_x0_bcd_get(void);
    124 static inline int psh3lcd_xx0_bcd_get(void);
    125 static void psh3lcd_x0_set_brightness(int);
    126 static void psh3lcd_xx0_set_brightness(int);
    127 static void psh3lcd_set_contrast(int);
    128 static int psh3lcd_param(void *, int, long, void *);
    129 static int psh3lcd_power(void *, int, long, void *);
    130 
    131 
    132 static inline int
    133 psh3lcd_x0_bcd_get()
    134 {
    135 	int i;
    136 	uint8_t bcr0, bcr1, bcr2;
    137 
    138 	bcr0 = _reg_read_1(PSH3LCD_BRIGHTNESS_REG0);
    139 	bcr1 = _reg_read_1(PSH3LCD_BRIGHTNESS_REG1);
    140 	bcr2 = _reg_read_1(PSH3LCD_BRIGHTNESS_REG2);
    141 
    142 	for (i = 0; psh3lcd_x0_bcd[i].reg0 != 0; i++)
    143 		if (bcr0 == psh3lcd_x0_bcd[i].reg0 &&
    144 		    bcr1 == psh3lcd_x0_bcd[i].reg1 &&
    145 		    bcr2 == psh3lcd_x0_bcd[i].reg2)
    146 			break;
    147 	if (psh3lcd_x0_bcd[i].reg0 == 0)
    148 		return BCD_NO_MATCH;
    149 	return i;
    150 }
    151 
    152 static inline int
    153 psh3lcd_xx0_bcd_get()
    154 {
    155 	int i;
    156 	uint8_t bcr1, bcr2;
    157 
    158 	bcr1 = _reg_read_1(PSH3LCD_BRIGHTNESS_REG1);
    159 	bcr2 = _reg_read_1(PSH3LCD_BRIGHTNESS_REG2);
    160 
    161 	for (i = 0; psh3lcd_xx0_bcd[i].reg1 != 0; i++)
    162 		if (bcr1 == psh3lcd_xx0_bcd[i].reg1 &&
    163 		    bcr2 == psh3lcd_xx0_bcd[i].reg2)
    164 			break;
    165 	if (psh3lcd_xx0_bcd[i].reg1 == 0)
    166 		return BCD_NO_MATCH;
    167 	return i;
    168 }
    169 
    170 static void
    171 psh3lcd_xx0_set_brightness(int index)
    172 {
    173 
    174 	_reg_write_1(PSH3LCD_BRIGHTNESS_REG1, psh3lcd_xx0_bcd[index].reg1);
    175 	_reg_write_1(PSH3LCD_BRIGHTNESS_REG2, psh3lcd_xx0_bcd[index].reg2);
    176 }
    177 
    178 static void
    179 psh3lcd_x0_set_brightness(int index)
    180 {
    181 
    182 	_reg_write_1(PSH3LCD_BRIGHTNESS_REG0, psh3lcd_x0_bcd[index].reg0);
    183 	_reg_write_1(PSH3LCD_BRIGHTNESS_REG1, psh3lcd_x0_bcd[index].reg1);
    184 	_reg_write_1(PSH3LCD_BRIGHTNESS_REG2, psh3lcd_x0_bcd[index].reg2);
    185 }
    186 
    187 /*
    188  * contrast control function.  controlled by IC (X9313W).
    189  */
    190 inline void
    191 psh3lcd_set_contrast(int value)
    192 {
    193 	uint16_t gpadr;
    194 	uint8_t pddr;
    195 
    196 	/* CS assert */
    197 	gpadr = hd64461_reg_read_2(HD64461_GPADR_REG16);
    198 	gpadr &= ~PSH3LCD_CONTRAST_CS;
    199 	hd64461_reg_write_2(HD64461_GPADR_REG16, gpadr);
    200 	delay(1);
    201 
    202 	/* set U/D# */
    203 	pddr = _reg_read_1(SH7709_PDDR);
    204 	if (value == PSH3LCD_CONTRAST_UP)
    205 		pddr |= PSH3LCD_CONTRAST_UD;
    206 	else if (value == PSH3LCD_CONTRAST_DOWN)
    207 		pddr &= ~PSH3LCD_CONTRAST_UD;
    208 	_reg_write_1(SH7709_PDDR, pddr);
    209 	delay(3);
    210 
    211 	/* INCrement */
    212 	hd64461_reg_write_2(HD64461_GPADR_REG16, gpadr & ~PSH3LCD_CONTRAST_INC);
    213 	delay(1);
    214 	hd64461_reg_write_2(HD64461_GPADR_REG16, gpadr);
    215 	delay(1);
    216 
    217 	/* CS deassert */
    218 	gpadr |= PSH3LCD_CONTRAST_CS;
    219 	hd64461_reg_write_2(HD64461_GPADR_REG16, gpadr);
    220 	delay(1);
    221 }
    222 
    223 /* ARGSUSED */
    224 static int
    225 psh3lcd_match(struct device *parent __unused, struct cfdata *cfp,
    226 	      void *aux __unused)
    227 {
    228 	uint8_t bcr0;
    229 
    230 	if (!platid_match(&platid, &platid_mask_MACH_HITACHI_PERSONA))
    231 		return 0;
    232 
    233 	if (strcmp(cfp->cf_name, "psh3lcd") != 0)
    234 		return 0;
    235 
    236 	bcr0 = _reg_read_1(PSH3LCD_BRIGHTNESS_REG0);
    237 	if (bcr0 == 0) {
    238 		if (psh3lcd_xx0_bcd_get() == BCD_NO_MATCH)
    239 			return 0;
    240 	} else {
    241 		if (psh3lcd_x0_bcd_get() == BCD_NO_MATCH)
    242 			return 0;
    243 	}
    244 
    245 	return 1;
    246 }
    247 
    248 /* ARGSUSED */
    249 static void
    250 psh3lcd_attach(struct device *parent __unused, struct device *self,
    251 	       void *aux __unused)
    252 {
    253 	struct psh3lcd_softc *sc = device_private(self);
    254 	uint8_t bcr0, bcr1, bcr2;
    255 
    256 	bcr0 = _reg_read_1(PSH3LCD_BRIGHTNESS_REG0);
    257 	bcr1 = _reg_read_1(PSH3LCD_BRIGHTNESS_REG1);
    258 	bcr2 = _reg_read_1(PSH3LCD_BRIGHTNESS_REG2);
    259 	if (bcr0 == 0) {
    260 		sc->sc_set_brightness = psh3lcd_xx0_set_brightness;
    261 		sc->sc_brightness = psh3lcd_xx0_bcd_get();
    262 		sc->sc_brightness_max = PSH3LCD_XX0_BRIGHTNESS_MAX;
    263 	} else {
    264 		sc->sc_set_brightness = psh3lcd_x0_set_brightness;
    265 		sc->sc_brightness = psh3lcd_x0_bcd_get();
    266 		sc->sc_brightness_max = PSH3LCD_X0_BRIGHTNESS_MAX;
    267 	}
    268 	aprint_naive("\n");
    269 	aprint_normal(": brightness %d\n", sc->sc_brightness);
    270 
    271 	/* LCD contrast hooks */
    272         config_hook(CONFIG_HOOK_GET,
    273 	    CONFIG_HOOK_CONTRAST_MAX, CONFIG_HOOK_SHARE, psh3lcd_param, sc);
    274         config_hook(CONFIG_HOOK_GET,
    275 	    CONFIG_HOOK_CONTRAST, CONFIG_HOOK_SHARE, psh3lcd_param, sc);
    276         config_hook(CONFIG_HOOK_SET,
    277 	    CONFIG_HOOK_CONTRAST, CONFIG_HOOK_SHARE, psh3lcd_param, sc);
    278 
    279 	/* LCD brightness hooks */
    280         config_hook(CONFIG_HOOK_GET,
    281 	    CONFIG_HOOK_BRIGHTNESS_MAX, CONFIG_HOOK_SHARE, psh3lcd_param, sc);
    282         config_hook(CONFIG_HOOK_GET,
    283 	    CONFIG_HOOK_BRIGHTNESS, CONFIG_HOOK_SHARE, psh3lcd_param, sc);
    284         config_hook(CONFIG_HOOK_SET,
    285 	    CONFIG_HOOK_BRIGHTNESS, CONFIG_HOOK_SHARE, psh3lcd_param, sc);
    286 
    287 	/* LCD on/off hook */
    288 	config_hook(CONFIG_HOOK_POWERCONTROL,
    289 	    CONFIG_HOOK_POWERCONTROL_LCD, CONFIG_HOOK_SHARE, psh3lcd_power, sc);
    290 }
    291 
    292 
    293 static int
    294 psh3lcd_param(void *ctx, int type, long id, void *msg)
    295 {
    296 	struct psh3lcd_softc *sc = ctx;
    297 	int value;
    298 
    299 	switch (type) {
    300 	case CONFIG_HOOK_GET:
    301 		switch (id) {
    302 		case CONFIG_HOOK_CONTRAST:
    303 			*(int *)msg = PSH3LCD_CONTRAST;
    304 			return 0;
    305 
    306 		case CONFIG_HOOK_CONTRAST_MAX:
    307 			*(int *)msg = PSH3LCD_CONTRAST_MAX;
    308 			return 0;
    309 
    310 		case CONFIG_HOOK_BRIGHTNESS:
    311 			*(int *)msg = sc->sc_brightness;
    312 			return 0;
    313 
    314 		case CONFIG_HOOK_BRIGHTNESS_MAX:
    315 			*(int *)msg = sc->sc_brightness_max;
    316 			return 0;
    317 		}
    318 		break;
    319 
    320 	case CONFIG_HOOK_SET:
    321 		value = *(int *)msg;
    322 
    323 		switch (id) {
    324 		case CONFIG_HOOK_CONTRAST:
    325 			if (value != PSH3LCD_CONTRAST_UP &&
    326 			    value != PSH3LCD_CONTRAST_DOWN)
    327 				return EINVAL;
    328 			psh3lcd_set_contrast(value);
    329 			return 0;
    330 
    331 		case CONFIG_HOOK_BRIGHTNESS:
    332 			if (value < 0)
    333 				value = 0;
    334 			if (value > sc->sc_brightness_max)
    335 				value = sc->sc_brightness_max;
    336 			sc->sc_brightness = value;
    337 			sc->sc_set_brightness(sc->sc_brightness);
    338 			return 0;
    339 		}
    340 		break;
    341 	}
    342 
    343 	return EINVAL;
    344 }
    345 
    346 
    347 static int
    348 psh3lcd_power(void *ctx, int type, long id, void *msg)
    349 {
    350 	struct psh3lcd_softc *sc = ctx;
    351 	int on;
    352 
    353 	if (type != CONFIG_HOOK_POWERCONTROL ||
    354 	    id != CONFIG_HOOK_POWERCONTROL_LCD)
    355 		return EINVAL;
    356 
    357 	on = (int)msg;
    358 	if (on)
    359 		sc->sc_set_brightness(sc->sc_brightness);
    360 	else
    361 		sc->sc_set_brightness(sc->sc_brightness_max + 1);
    362 
    363 	return 0;
    364 }
    365