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