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