Home | History | Annotate | Line # | Download | only in dev
j6x0lcd.c revision 1.5
      1  1.5  uwe /*	$NetBSD: j6x0lcd.c,v 1.5 2005/02/28 16:55:58 uwe Exp $ */
      2  1.1  uwe 
      3  1.1  uwe /*
      4  1.1  uwe  * Copyright (c) 2004 Valeriy E. Ushakov
      5  1.1  uwe  * All rights reserved.
      6  1.1  uwe  *
      7  1.1  uwe  * Redistribution and use in source and binary forms, with or without
      8  1.1  uwe  * modification, are permitted provided that the following conditions
      9  1.1  uwe  * are met:
     10  1.1  uwe  * 1. Redistributions of source code must retain the above copyright
     11  1.1  uwe  *    notice, this list of conditions and the following disclaimer.
     12  1.1  uwe  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  uwe  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  uwe  *    documentation and/or other materials provided with the distribution.
     15  1.1  uwe  * 3. The name of the author may not be used to endorse or promote products
     16  1.1  uwe  *    derived from this software without specific prior written permission
     17  1.1  uwe  *
     18  1.1  uwe  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.1  uwe  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  1.1  uwe  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  1.1  uwe  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  1.1  uwe  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  1.1  uwe  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  1.1  uwe  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  1.1  uwe  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  1.1  uwe  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  1.1  uwe  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.1  uwe  */
     29  1.1  uwe 
     30  1.1  uwe #include <sys/cdefs.h>
     31  1.5  uwe __KERNEL_RCSID(0, "$NetBSD: j6x0lcd.c,v 1.5 2005/02/28 16:55:58 uwe Exp $");
     32  1.1  uwe 
     33  1.1  uwe #include <sys/param.h>
     34  1.1  uwe #include <sys/kernel.h>
     35  1.1  uwe #include <sys/device.h>
     36  1.1  uwe #include <sys/malloc.h>
     37  1.1  uwe #include <sys/systm.h>
     38  1.1  uwe #ifdef GPROF
     39  1.1  uwe #include <sys/gmon.h>
     40  1.1  uwe #endif
     41  1.1  uwe 
     42  1.1  uwe #include <machine/platid.h>
     43  1.1  uwe #include <machine/platid_mask.h>
     44  1.1  uwe 
     45  1.1  uwe #include <machine/config_hook.h>
     46  1.1  uwe 
     47  1.1  uwe #include <sh3/dacreg.h>
     48  1.1  uwe #include <hpcsh/dev/hd64461/hd64461var.h> /* XXX: for hd64461_reg_read_2 &c */
     49  1.1  uwe #include <hpcsh/dev/hd64461/hd64461reg.h>
     50  1.1  uwe #include <hpcsh/dev/hd64461/hd64461gpioreg.h>
     51  1.1  uwe 
     52  1.1  uwe 
     53  1.1  uwe /*
     54  1.1  uwe  * LCD power: controlled by pin 0 in HD64461 GPIO port B.
     55  1.1  uwe  *   0 - power on
     56  1.1  uwe  *   1 - power off
     57  1.1  uwe  */
     58  1.1  uwe #define HD64461_GPBDR_J6X0LCD_OFF	0x01
     59  1.1  uwe 
     60  1.1  uwe #define HD64461_GPBCR_J6X0LCD_OFF_MASK	0xfffc
     61  1.1  uwe #define HD64461_GPBCR_J6X0LCD_OFF_BITS	0x0001
     62  1.1  uwe 
     63  1.1  uwe 
     64  1.1  uwe /*
     65  1.1  uwe  * LCD brightness: controlled by DAC channel 0.  Larger channel values
     66  1.1  uwe  * mean dimmer.  Values smaller (i.e. brighter) then 0x5e seems to
     67  1.1  uwe  * result in no visible changes.
     68  1.1  uwe  */
     69  1.1  uwe #define J6X0LCD_BRIGHTNESS_DA_MAX	0x5e
     70  1.1  uwe #define J6X0LCD_BRIGHTNESS_DA_MIN	0xff
     71  1.1  uwe 
     72  1.1  uwe #define J6X0LCD_DA_TO_BRIGHTNESS(da) \
     73  1.1  uwe 	(J6X0LCD_BRIGHTNESS_DA_MIN - (da))
     74  1.1  uwe 
     75  1.1  uwe #define J6X0LCD_BRIGHTNESS_TO_DA(br) \
     76  1.1  uwe 	(J6X0LCD_BRIGHTNESS_DA_MIN - (br))
     77  1.1  uwe 
     78  1.1  uwe #define J6X0LCD_BRIGHTNESS_MAX \
     79  1.1  uwe 	J6X0LCD_DA_TO_BRIGHTNESS(J6X0LCD_BRIGHTNESS_DA_MAX)
     80  1.1  uwe 
     81  1.1  uwe /* convenience macro to accesses DAC registers */
     82  1.1  uwe #define DAC_(x)    (*((volatile uint8_t *)SH7709_DA ## x))
     83  1.1  uwe 
     84  1.1  uwe 
     85  1.1  uwe /*
     86  1.1  uwe  * LCD contrast: controlled by pins 6,5,4,3 HD64461 GPIO port B.
     87  1.3  uwe  * 6th is the least significant bit, 3rd is the most significant.
     88  1.1  uwe  * The bits are inverted: .1111... = 0, .0111... = 1, etc.
     89  1.1  uwe  *
     90  1.1  uwe  * We control the contrast value by setting bits in the data register
     91  1.1  uwe  * to all ones, and changing the mode of the bits in the control
     92  1.1  uwe  * register, keeping "ones" in gpio output mode (1), and switching
     93  1.1  uwe  * "zeros" to input mode (3).  This is what WinCE also does.
     94  1.1  uwe  *
     95  1.1  uwe  * Alternative method is to set the mode of all bits to gpio output
     96  1.1  uwe  * mode and then change the bits in the data register.  This method
     97  1.1  uwe  * results in significantly less contrast screen for the same values.
     98  1.1  uwe  * E.g., WinCE default contrast value is 11 (in our numbering, 23 in
     99  1.1  uwe  * WinCE's) - which is fine in the "tweak the control register"
    100  1.1  uwe  * method, but is very blurry in the "tweak the data register" method.
    101  1.1  uwe  * Subjectively, 15 in the "tweak control" method looks like 7 in the
    102  1.1  uwe  * "tweak data" method.
    103  1.1  uwe  *
    104  1.1  uwe  * May be it's possible to control a wider range of contrast by
    105  1.1  uwe  * combining the two methods, but values above 7 in the "tweak data"
    106  1.1  uwe  * method are so blurry that they are next to unusable in practice.
    107  1.1  uwe  */
    108  1.1  uwe #define J6X0LCD_CONTRAST_MAX	15
    109  1.1  uwe 
    110  1.1  uwe #define HD64461_GPBDR_J6X0LCD_CONTRAST_MASK	0x87
    111  1.1  uwe #define HD64461_GPBDR_J6X0LCD_CONTRAST_BITS	0x78
    112  1.1  uwe 
    113  1.1  uwe #if 0
    114  1.1  uwe /* "tweak data" */
    115  1.1  uwe static uint8_t j6x0lcd_contrast_data_bits[] = {
    116  1.1  uwe 	0x78, 0x38, 0x58, 0x18, 0x68, 0x28, 0x48, 0x08,
    117  1.1  uwe 	0x70, 0x30, 0x50, 0x10, 0x60, 0x20, 0x40, 0x00
    118  1.1  uwe };
    119  1.1  uwe #endif
    120  1.1  uwe 
    121  1.1  uwe #define HD64461_GPBCR_J6X0LCD_CONTRAST_MASK	0xc03f
    122  1.1  uwe #define HD64461_GPBCR_J6X0LCD_CONTRAST_BITS	0x1540
    123  1.1  uwe 
    124  1.1  uwe /* "tweak control" */
    125  1.1  uwe static uint16_t j6x0lcd_contrast_control_bits[] = {
    126  1.1  uwe 	0x1540, 0x3540, 0x1d40, 0x3d40, 0x1740, 0x3740, 0x1f40, 0x3f40,
    127  1.1  uwe 	0x15c0, 0x35c0, 0x1dc0, 0x3dc0, 0x17c0, 0x37c0, 0x1fc0, 0x3fc0
    128  1.1  uwe };
    129  1.1  uwe 
    130  1.1  uwe 
    131  1.1  uwe struct j6x0lcd_softc {
    132  1.1  uwe 	struct device sc_dev;
    133  1.1  uwe 	int sc_brightness;
    134  1.1  uwe 	int sc_contrast;
    135  1.1  uwe };
    136  1.1  uwe 
    137  1.1  uwe static int	j6x0lcd_match(struct device *, struct cfdata *, void *);
    138  1.1  uwe static void	j6x0lcd_attach(struct device *, struct device *, void *);
    139  1.1  uwe 
    140  1.1  uwe CFATTACH_DECL(j6x0lcd, sizeof(struct j6x0lcd_softc),
    141  1.1  uwe     j6x0lcd_match, j6x0lcd_attach, NULL, NULL);
    142  1.1  uwe 
    143  1.1  uwe 
    144  1.1  uwe static int	j6x0lcd_param(void *, int, long, void *);
    145  1.1  uwe static int	j6x0lcd_power(void *, int, long, void *);
    146  1.1  uwe 
    147  1.1  uwe 
    148  1.1  uwe static int
    149  1.1  uwe j6x0lcd_match(struct device *parent, struct cfdata *cfp, void *aux)
    150  1.1  uwe {
    151  1.1  uwe 
    152  1.1  uwe 	/*
    153  1.1  uwe 	 * XXX: does platid_mask_MACH_HP_LX matches _JORNADA_6XX too?
    154  1.1  uwe 	 * Is 620 wired similarly?
    155  1.1  uwe 	 */
    156  1.1  uwe 	if (!platid_match(&platid, &platid_mask_MACH_HP_JORNADA_6XX))
    157  1.1  uwe 		return (0);
    158  1.1  uwe 
    159  1.1  uwe 	if (strcmp(cfp->cf_name, "j6x0lcd") != 0)
    160  1.1  uwe 		return (0);
    161  1.1  uwe 
    162  1.1  uwe 	return (1);
    163  1.1  uwe }
    164  1.1  uwe 
    165  1.1  uwe 
    166  1.1  uwe static void
    167  1.1  uwe j6x0lcd_attach(struct device *parent, struct device *self, void *aux)
    168  1.1  uwe {
    169  1.1  uwe 	struct j6x0lcd_softc *sc = (struct j6x0lcd_softc *)self;
    170  1.1  uwe 	int contrast, i;
    171  1.1  uwe 	uint16_t bcr, bdr;
    172  1.1  uwe 	uint8_t dcr, ddr;
    173  1.1  uwe 
    174  1.4  uwe 	/*
    175  1.1  uwe 	 * Brightness is controlled by DAC channel 0.
    176  1.1  uwe 	 */
    177  1.1  uwe 	dcr = DAC_(CR);
    178  1.1  uwe 	dcr &= ~SH7709_DACR_DAE; /* want to control each channel separately */
    179  1.1  uwe 	dcr |= SH7709_DACR_DAOE0; /* enable channel 0 */
    180  1.1  uwe 	DAC_(CR) = dcr;
    181  1.1  uwe 
    182  1.1  uwe 	ddr = DAC_(DR0);
    183  1.1  uwe 	sc->sc_brightness = J6X0LCD_DA_TO_BRIGHTNESS(ddr);
    184  1.1  uwe 
    185  1.1  uwe 
    186  1.1  uwe 	/*
    187  1.1  uwe 	 * Contrast and power are controlled by HD64461 GPIO port B.
    188  1.1  uwe 	 */
    189  1.1  uwe 	bcr = hd64461_reg_read_2(HD64461_GPBCR_REG16);
    190  1.1  uwe 	bdr = hd64461_reg_read_2(HD64461_GPBDR_REG16);
    191  1.1  uwe 
    192  1.1  uwe 	contrast = 0xf;		/* bits are inverted */
    193  1.1  uwe 	for (i = 0; i < 4; ++i) {
    194  1.1  uwe 		unsigned int c, v;
    195  1.1  uwe 		c = (bcr >> ((6 - i) << 1)) & 0x3;
    196  1.1  uwe 		if (c == 1)	/* gpio mode? */
    197  1.1  uwe 			v = 1;
    198  1.1  uwe 		else
    199  1.1  uwe 			v = 0;
    200  1.1  uwe 		contrast &= ~(v << i);
    201  1.1  uwe 	}
    202  1.1  uwe 
    203  1.1  uwe 	sc->sc_contrast = contrast;
    204  1.1  uwe 
    205  1.1  uwe 	bdr &= ~HD64461_GPBDR_J6X0LCD_OFF;
    206  1.1  uwe 	bdr |= HD64461_GPBDR_J6X0LCD_CONTRAST_BITS;
    207  1.1  uwe 	hd64461_reg_write_2(HD64461_GPBDR_REG16, bdr);
    208  1.1  uwe 
    209  1.1  uwe 	bcr &= HD64461_GPBCR_J6X0LCD_OFF_MASK
    210  1.1  uwe 		& HD64461_GPBCR_J6X0LCD_CONTRAST_MASK;
    211  1.1  uwe 	bcr |= HD64461_GPBCR_J6X0LCD_OFF_BITS
    212  1.1  uwe 		| j6x0lcd_contrast_control_bits[contrast];
    213  1.1  uwe 	hd64461_reg_write_2(HD64461_GPBCR_REG16, bcr);
    214  1.1  uwe 
    215  1.1  uwe 	printf(": brightness %d, contrast %d\n",
    216  1.1  uwe 	       sc->sc_brightness, sc->sc_contrast);
    217  1.1  uwe 
    218  1.1  uwe 
    219  1.1  uwe 	/* LCD brightness hooks */
    220  1.4  uwe 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_BRIGHTNESS_MAX,
    221  1.1  uwe 		    CONFIG_HOOK_SHARE,
    222  1.1  uwe 		    j6x0lcd_param, sc);
    223  1.4  uwe 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_BRIGHTNESS,
    224  1.1  uwe 		    CONFIG_HOOK_SHARE,
    225  1.1  uwe 		    j6x0lcd_param, sc);
    226  1.4  uwe 	config_hook(CONFIG_HOOK_SET, CONFIG_HOOK_BRIGHTNESS,
    227  1.1  uwe 		    CONFIG_HOOK_SHARE,
    228  1.1  uwe 		    j6x0lcd_param, sc);
    229  1.1  uwe 
    230  1.1  uwe 	/* LCD contrast hooks */
    231  1.4  uwe 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_CONTRAST_MAX,
    232  1.1  uwe 		    CONFIG_HOOK_SHARE,
    233  1.1  uwe 		    j6x0lcd_param, sc);
    234  1.4  uwe 	config_hook(CONFIG_HOOK_GET, CONFIG_HOOK_CONTRAST,
    235  1.1  uwe 		    CONFIG_HOOK_SHARE,
    236  1.1  uwe 		    j6x0lcd_param, sc);
    237  1.4  uwe 	config_hook(CONFIG_HOOK_SET, CONFIG_HOOK_CONTRAST,
    238  1.1  uwe 		    CONFIG_HOOK_SHARE,
    239  1.1  uwe 		    j6x0lcd_param, sc);
    240  1.1  uwe 
    241  1.1  uwe 	/* LCD on/off hook */
    242  1.1  uwe 	config_hook(CONFIG_HOOK_POWERCONTROL,
    243  1.2  uwe 		    CONFIG_HOOK_POWERCONTROL_LCD,
    244  1.1  uwe 		    CONFIG_HOOK_SHARE,
    245  1.1  uwe 		    j6x0lcd_power, sc);
    246  1.1  uwe }
    247  1.1  uwe 
    248  1.1  uwe 
    249  1.1  uwe static int
    250  1.1  uwe j6x0lcd_param(ctx, type, id, msg)
    251  1.1  uwe 	void *ctx;
    252  1.1  uwe 	int type;
    253  1.1  uwe 	long id;
    254  1.1  uwe 	void *msg;
    255  1.1  uwe {
    256  1.1  uwe 	struct j6x0lcd_softc *sc = ctx;
    257  1.1  uwe 	int value;
    258  1.1  uwe 	uint16_t bcr;
    259  1.1  uwe 	uint8_t dr;
    260  1.1  uwe 
    261  1.1  uwe 	switch (type) {
    262  1.1  uwe 	case CONFIG_HOOK_GET:
    263  1.1  uwe 		switch (id) {
    264  1.1  uwe 		case CONFIG_HOOK_CONTRAST:
    265  1.1  uwe 			*(int *)msg = sc->sc_contrast;
    266  1.1  uwe 			return (0);
    267  1.1  uwe 
    268  1.1  uwe 		case CONFIG_HOOK_CONTRAST_MAX:
    269  1.1  uwe 			*(int *)msg = J6X0LCD_CONTRAST_MAX;
    270  1.1  uwe 			return (0);
    271  1.1  uwe 
    272  1.1  uwe 		case CONFIG_HOOK_BRIGHTNESS:
    273  1.1  uwe 			*(int *)msg = sc->sc_brightness;
    274  1.1  uwe 			return (0);
    275  1.1  uwe 
    276  1.1  uwe 		case CONFIG_HOOK_BRIGHTNESS_MAX:
    277  1.1  uwe 			*(int *)msg = J6X0LCD_BRIGHTNESS_MAX;
    278  1.1  uwe 			return (0);
    279  1.1  uwe 		}
    280  1.1  uwe 		break;
    281  1.1  uwe 
    282  1.1  uwe 	case CONFIG_HOOK_SET:
    283  1.1  uwe 		value = *(int *)msg;
    284  1.1  uwe 		if (value < 0)
    285  1.1  uwe 			value = 0;
    286  1.1  uwe 
    287  1.1  uwe 		switch (id) {
    288  1.1  uwe 		case CONFIG_HOOK_CONTRAST:
    289  1.1  uwe 			if (value > J6X0LCD_CONTRAST_MAX)
    290  1.1  uwe 				value = J6X0LCD_CONTRAST_MAX;
    291  1.1  uwe 			sc->sc_contrast = value;
    292  1.1  uwe 
    293  1.1  uwe 			bcr = hd64461_reg_read_2(HD64461_GPBCR_REG16);
    294  1.1  uwe 			bcr &= HD64461_GPBCR_J6X0LCD_CONTRAST_MASK;
    295  1.1  uwe 			bcr |= j6x0lcd_contrast_control_bits[value];
    296  1.1  uwe 			hd64461_reg_write_2(HD64461_GPBCR_REG16, bcr);
    297  1.1  uwe 			return (0);
    298  1.1  uwe 
    299  1.1  uwe 		case CONFIG_HOOK_BRIGHTNESS:
    300  1.1  uwe 			if (value > J6X0LCD_BRIGHTNESS_MAX)
    301  1.1  uwe 				value = J6X0LCD_BRIGHTNESS_MAX;
    302  1.1  uwe 			sc->sc_brightness = value;
    303  1.1  uwe 
    304  1.1  uwe 			dr = J6X0LCD_BRIGHTNESS_TO_DA(value);
    305  1.1  uwe 			DAC_(DR0) = dr;
    306  1.1  uwe 			return (0);
    307  1.1  uwe 		}
    308  1.1  uwe 		break;
    309  1.1  uwe 	}
    310  1.1  uwe 
    311  1.1  uwe 	return (EINVAL);
    312  1.1  uwe }
    313  1.1  uwe 
    314  1.1  uwe 
    315  1.1  uwe static int
    316  1.1  uwe j6x0lcd_power(ctx, type, id, msg)
    317  1.4  uwe 	void *ctx;
    318  1.4  uwe 	int type;
    319  1.4  uwe 	long id;
    320  1.1  uwe 	void *msg;
    321  1.1  uwe {
    322  1.1  uwe 	int on;
    323  1.1  uwe 	uint16_t r;
    324  1.1  uwe 
    325  1.1  uwe 	if (type != CONFIG_HOOK_POWERCONTROL
    326  1.2  uwe 	    || id != CONFIG_HOOK_POWERCONTROL_LCD)
    327  1.1  uwe 		return (EINVAL);
    328  1.1  uwe 
    329  1.1  uwe 	on = (int)msg;
    330  1.1  uwe 
    331  1.1  uwe 	r = hd64461_reg_read_2(HD64461_GPBDR_REG16);
    332  1.1  uwe 	if (on)
    333  1.1  uwe 		r &= ~HD64461_GPBDR_J6X0LCD_OFF;
    334  1.1  uwe 	else
    335  1.1  uwe 		r |= HD64461_GPBDR_J6X0LCD_OFF;
    336  1.1  uwe 	hd64461_reg_write_2(HD64461_GPBDR_REG16, r);
    337  1.1  uwe 
    338  1.1  uwe 	return (0);
    339  1.1  uwe }
    340