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