zlcd.c revision 1.2 1 /* $NetBSD: zlcd.c,v 1.2 2006/12/17 16:07:11 peter Exp $ */
2 /* $OpenBSD: zaurus_lcd.c,v 1.20 2006/06/02 20:50:14 miod Exp $ */
3 /* NetBSD: lubbock_lcd.c,v 1.1 2003/08/09 19:38:53 bsh Exp */
4
5 /*
6 * Copyright (c) 2002, 2003 Genetec Corporation. All rights reserved.
7 * Written by Hiroyuki Bessho for Genetec Corporation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of Genetec Corporation may not be used to endorse or
18 * promote products derived from this software without specific prior
19 * written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENETEC CORPORATION
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * LCD driver for Sharp Zaurus (based on the Intel Lubbock driver).
36 *
37 * Controlling LCD is almost completely done through PXA2X0's
38 * integrated LCD controller. Codes for it is arm/xscale/pxa2x0_lcd.c.
39 *
40 * Codes in this file provide platform specific things including:
41 * LCD on/off switch and backlight brightness
42 * LCD panel geometry
43 */
44
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: zlcd.c,v 1.2 2006/12/17 16:07:11 peter Exp $");
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/conf.h>
51 #include <sys/uio.h>
52 #include <sys/malloc.h>
53
54 #include <dev/cons.h>
55 #include <dev/wscons/wsconsio.h>
56 #include <dev/wscons/wsdisplayvar.h>
57 #include <dev/wscons/wscons_callbacks.h>
58
59 #include <machine/bus.h>
60 #include <arm/xscale/pxa2x0var.h>
61 #include <arm/xscale/pxa2x0reg.h>
62 #include <arm/xscale/pxa2x0_lcd.h>
63
64 #include <zaurus/dev/scoopvar.h>
65 #include <zaurus/dev/zsspvar.h>
66 #include <zaurus/zaurus/zaurus_var.h>
67
68 /*
69 * wsdisplay glue
70 */
71 static struct pxa2x0_wsscreen_descr lcd_std_screen = {
72 .c = {
73 .name = "std",
74 .textops = &pxa2x0_lcd_emulops,
75 .fontwidth = 8,
76 .fontheight = 16,
77 .capabilities = WSSCREEN_WSCOLORS,
78 },
79 .depth = 16, /* bits per pixel */
80 .flags = 0/*RI_ROTATE_CW*/, /* quarter clockwise rotation */
81 };
82
83 static const struct wsscreen_descr *lcd_scr_descr[] = {
84 &lcd_std_screen.c
85 };
86
87 static const struct wsscreen_list lcd_screen_list = {
88 .nscreens = __arraycount(lcd_scr_descr),
89 .screens = lcd_scr_descr,
90 };
91
92 static int lcd_ioctl(void *, void *, u_long, caddr_t, int, struct lwp *);
93 static int lcd_param(struct pxa2x0_lcd_softc *, u_long,
94 struct wsdisplay_param *);
95 static int lcd_show_screen(void *, void *, int,
96 void (*)(void *, int, int), void *);
97
98 struct wsdisplay_accessops lcd_accessops = {
99 lcd_ioctl,
100 pxa2x0_lcd_mmap,
101 pxa2x0_lcd_alloc_screen,
102 pxa2x0_lcd_free_screen,
103 lcd_show_screen,
104 NULL,
105 NULL,
106 NULL,
107 };
108
109 #define CURRENT_DISPLAY &sharp_zaurus_C3000
110
111 const struct lcd_panel_geometry sharp_zaurus_C3000 =
112 {
113 480, /* Width */
114 640, /* Height */
115 0, /* No extra lines */
116
117 LCDPANEL_ACTIVE | LCDPANEL_VSP | LCDPANEL_HSP,
118 1, /* clock divider */
119 0, /* AC bias pin freq */
120
121 0x28, /* horizontal sync pulse width */
122 0x2e, /* BLW */
123 0x7d, /* ELW */
124
125 2, /* vertical sync pulse width */
126 1, /* BFW */
127 0, /* EFW */
128 };
129
130 struct sharp_lcd_backlight {
131 int duty; /* LZ9JG18 DAC value */
132 int cont; /* BACKLIGHT_CONT signal */
133 int on; /* BACKLIGHT_ON signal */
134 };
135
136 #define CURRENT_BACKLIGHT sharp_zaurus_C3000_bl
137
138 const struct sharp_lcd_backlight sharp_zaurus_C3000_bl[] = {
139 { 0x00, 0, 0 }, /* 0: Off */
140 { 0x00, 0, 1 }, /* 1: 0% */
141 { 0x01, 0, 1 }, /* 2: 20% */
142 { 0x07, 0, 1 }, /* 3: 40% */
143 { 0x01, 1, 1 }, /* 4: 60% */
144 { 0x07, 1, 1 }, /* 5: 80% */
145 { 0x11, 1, 1 }, /* 6: 100% */
146 { -1, -1, -1 }, /* 7: Invalid */
147 };
148
149 static int lcd_match(struct device *, struct cfdata *, void *);
150 static void lcd_attach(struct device *, struct device *, void *);
151
152 CFATTACH_DECL(zlcd, sizeof(struct pxa2x0_lcd_softc),
153 lcd_match, lcd_attach, NULL, NULL);
154
155 int lcd_max_brightness(void);
156 int lcd_get_brightness(void);
157 void lcd_set_brightness(int);
158 void lcd_set_brightness_internal(int);
159 int lcd_get_backlight(void);
160 void lcd_set_backlight(int);
161 void lcd_blank(int);
162 void lcd_power(int, void *);
163
164 static int
165 lcd_match(struct device *parent, struct cfdata *cf, void *aux)
166 {
167
168 return 1;
169 }
170
171 static void
172 lcd_attach(struct device *parent, struct device *self, void *aux)
173 {
174 struct pxa2x0_lcd_softc *sc = (struct pxa2x0_lcd_softc *)self;
175 struct wsemuldisplaydev_attach_args aa;
176
177 pxa2x0_lcd_attach_sub(sc, aux, &lcd_std_screen, CURRENT_DISPLAY,
178 glass_console);
179
180 aa.console = glass_console;
181 aa.scrdata = &lcd_screen_list;
182 aa.accessops = &lcd_accessops;
183 aa.accesscookie = sc;
184
185 (void) config_found(self, &aa, wsemuldisplaydevprint);
186
187 /* Start with approximately 40% of full brightness. */
188 lcd_set_brightness(3);
189
190 (void) powerhook_establish(sc->dev.dv_xname, lcd_power, sc);
191 }
192
193 /*
194 * wsdisplay accessops overrides
195 */
196 static int
197 lcd_ioctl(void *v, void *vs, u_long cmd, caddr_t data, int flag, struct lwp *l)
198 {
199 struct pxa2x0_lcd_softc *sc = (struct pxa2x0_lcd_softc *)v;
200 int res = EINVAL;
201
202 switch (cmd) {
203 case WSDISPLAYIO_GETPARAM:
204 case WSDISPLAYIO_SETPARAM:
205 res = lcd_param(sc, cmd, (struct wsdisplay_param *)data);
206 break;
207 }
208
209 if (res == EINVAL)
210 res = pxa2x0_lcd_ioctl(v, vs, cmd, data, flag, l);
211 return res;
212 }
213
214 static int
215 lcd_show_screen(void *v, void *cookie, int waitok,
216 void (*cb_func)(void *, int, int), void *cb_arg)
217 {
218 int error;
219
220 error = pxa2x0_lcd_show_screen(v, cookie, waitok, cb_func, cb_arg);
221 if (error)
222 return (error);
223
224 /* Turn on LCD */
225 lcd_set_brightness(lcd_get_brightness());
226
227 return 0;
228 }
229
230 /*
231 * wsdisplay I/O controls
232 */
233 static int
234 lcd_param(struct pxa2x0_lcd_softc *sc, u_long cmd,
235 struct wsdisplay_param *dp)
236 {
237 int res = EINVAL;
238
239 switch (dp->param) {
240 case WSDISPLAYIO_PARAM_BACKLIGHT:
241 if (cmd == WSDISPLAYIO_GETPARAM) {
242 dp->min = 0;
243 dp->max = 1;
244 dp->curval = lcd_get_backlight();
245 res = 0;
246 } else if (cmd == WSDISPLAYIO_SETPARAM) {
247 lcd_set_backlight(dp->curval);
248 res = 0;
249 }
250 break;
251
252 case WSDISPLAYIO_PARAM_CONTRAST:
253 /* unsupported */
254 res = ENOTTY;
255 break;
256
257 case WSDISPLAYIO_PARAM_BRIGHTNESS:
258 if (cmd == WSDISPLAYIO_GETPARAM) {
259 dp->min = 1;
260 dp->max = lcd_max_brightness();
261 dp->curval = lcd_get_brightness();
262 res = 0;
263 } else if (cmd == WSDISPLAYIO_SETPARAM) {
264 lcd_set_brightness(dp->curval);
265 res = 0;
266 }
267 break;
268 }
269
270 return res;
271 }
272
273 /*
274 * LCD backlight
275 */
276
277 static int lcdbrightnesscurval = 1;
278 static int lcdislit = 1;
279 static int lcdisblank = 0;
280
281 int
282 lcd_max_brightness(void)
283 {
284 int i;
285
286 for (i = 0; CURRENT_BACKLIGHT[i].duty != -1; i++)
287 continue;
288 return i - 1;
289 }
290
291 int
292 lcd_get_brightness(void)
293 {
294
295 return lcdbrightnesscurval;
296 }
297
298 void
299 lcd_set_brightness(int newval)
300 {
301 int maxval;
302
303 maxval = lcd_max_brightness();
304 if (newval < 0)
305 newval = 0;
306 else if (newval > maxval)
307 newval = maxval;
308
309 if (lcd_get_backlight() && !lcdisblank)
310 lcd_set_brightness_internal(newval);
311
312 if (newval > 0)
313 lcdbrightnesscurval = newval;
314 }
315
316 void
317 lcd_set_brightness_internal(int newval)
318 {
319 static int curval = 1;
320 int i;
321
322 /*
323 * It appears that the C3000 backlight can draw too much power if we
324 * switch it from a low to a high brightness. Increasing brightness
325 * in steps avoids this issue.
326 */
327 if (newval > curval) {
328 for (i = curval + 1; i <= newval; i++) {
329 (void)zssp_ic_send(ZSSP_IC_LZ9JG18,
330 CURRENT_BACKLIGHT[i].duty);
331 scoop_set_backlight(CURRENT_BACKLIGHT[i].on,
332 CURRENT_BACKLIGHT[i].cont);
333 delay(5000);
334 }
335 } else {
336 (void)zssp_ic_send(ZSSP_IC_LZ9JG18,
337 CURRENT_BACKLIGHT[newval].duty);
338 scoop_set_backlight(CURRENT_BACKLIGHT[newval].on,
339 CURRENT_BACKLIGHT[newval].cont);
340 }
341
342 curval = newval;
343 }
344
345 int
346 lcd_get_backlight(void)
347 {
348
349 return lcdislit;
350 }
351
352 void
353 lcd_set_backlight(int onoff)
354 {
355
356 if (!onoff) {
357 lcd_set_brightness(0);
358 lcdislit = 0;
359 } else {
360 lcdislit = 1;
361 lcd_set_brightness(lcd_get_brightness());
362 }
363 }
364
365 void
366 lcd_blank(int blank)
367 {
368
369 if (blank) {
370 lcd_set_brightness(0);
371 lcdisblank = 1;
372 } else {
373 lcdisblank = 0;
374 lcd_set_brightness(lcd_get_brightness());
375 }
376 }
377
378 void
379 lcd_power(int why, void *v)
380 {
381
382 switch (why) {
383 case PWR_SUSPEND:
384 case PWR_STANDBY:
385 lcd_set_brightness(0);
386 pxa2x0_lcd_power(why, v);
387 break;
388
389 case PWR_RESUME:
390 pxa2x0_lcd_power(why, v);
391 lcd_set_brightness(lcd_get_brightness());
392 break;
393 }
394 }
395