zlcd.c revision 1.1 1 /* $NetBSD: zlcd.c,v 1.1 2006/12/16 05:23:06 ober 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.1 2006/12/16 05:23:06 ober 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 struct pxa2x0_wsscreen_descr lcd_bpp16_screen = {
72 {
73 "std"
74 },
75 16, /* bits per pixel */
76 0/*RI_ROTATE_CW*/ /* quarter clockwise rotation */
77 };
78
79 static const struct wsscreen_descr *lcd_scr_descr[] = {
80 &lcd_bpp16_screen.c
81 };
82
83 const struct wsscreen_list lcd_screen_list = {
84 sizeof lcd_scr_descr / sizeof lcd_scr_descr[0], lcd_scr_descr
85 };
86
87 int lcd_ioctl(void *, void *, u_long, caddr_t, int, struct lwp *);
88 int lcd_param(struct pxa2x0_lcd_softc *, u_long,
89 struct wsdisplay_param *);
90 int lcd_show_screen(void *, void *, int,
91 void (*)(void *, int, int), void *);
92 void lcd_burner(void *, u_int, u_int);
93
94 const struct wsdisplay_accessops lcd_accessops = {
95 lcd_ioctl,
96 pxa2x0_lcd_mmap,
97 pxa2x0_lcd_alloc_screen,
98 pxa2x0_lcd_free_screen,
99 lcd_show_screen,
100 NULL, /* load_font */
101 NULL, /* pollc */
102 NULL, /* scroll */
103 };
104
105 #define CURRENT_DISPLAY &sharp_zaurus_C3000
106
107 const struct lcd_panel_geometry sharp_zaurus_C3000 =
108 {
109 480, /* Width */
110 640, /* Height */
111 0, /* No extra lines */
112
113 LCDPANEL_ACTIVE | LCDPANEL_VSP | LCDPANEL_HSP,
114 1, /* clock divider */
115 0, /* AC bias pin freq */
116
117 0x28, /* horizontal sync pulse width */
118 0x2e, /* BLW */
119 0x7d, /* ELW */
120
121 2, /* vertical sync pulse width */
122 1, /* BFW */
123 0, /* EFW */
124 };
125
126 struct sharp_lcd_backlight {
127 int duty; /* LZ9JG18 DAC value */
128 int cont; /* BACKLIGHT_CONT signal */
129 int on; /* BACKLIGHT_ON signal */
130 };
131
132 #define CURRENT_BACKLIGHT sharp_zaurus_C3000_bl
133
134 const struct sharp_lcd_backlight sharp_zaurus_C3000_bl[] = {
135 { 0x00, 0, 0 }, /* 0: Off */
136 { 0x00, 0, 1 }, /* 1: 0% */
137 { 0x01, 0, 1 }, /* 2: 20% */
138 { 0x07, 0, 1 }, /* 3: 40% */
139 { 0x01, 1, 1 }, /* 4: 60% */
140 { 0x07, 1, 1 }, /* 5: 80% */
141 { 0x11, 1, 1 }, /* 6: 100% */
142 { -1, -1, -1 } /* 7: Invalid */
143 };
144
145 static int lcd_match(struct device *, struct cfdata *, void *);
146 static void lcd_attach(struct device *, struct device *, void *);
147 int lcd_cnattach(void);
148
149 CFATTACH_DECL(zlcd, sizeof(struct pxa2x0_lcd_softc),
150 lcd_match, lcd_attach, NULL, NULL);
151
152 int lcd_max_brightness(void);
153 int lcd_get_brightness(void);
154 void lcd_set_brightness(int);
155 void lcd_set_brightness_internal(int);
156 int lcd_get_backlight(void);
157 void lcd_set_backlight(int);
158 void lcd_blank(int);
159 void lcd_power(int, void *);
160
161 static int
162 lcd_match(struct device *parent, struct cfdata *cf, void *aux)
163 {
164
165 return 1;
166 }
167
168 static void
169 lcd_attach(struct device *parent, struct device *self, void *aux)
170 {
171 struct pxa2x0_lcd_softc *sc = (struct pxa2x0_lcd_softc *)self;
172 struct wsemuldisplaydev_attach_args aa;
173
174 printf("\n");
175
176 pxa2x0_lcd_attach_sub(sc, aux, &lcd_bpp16_screen, CURRENT_DISPLAY,
177 glass_console);
178
179 aa.console = glass_console;
180 aa.scrdata = &lcd_screen_list;
181 aa.accessops = &lcd_accessops;
182 aa.accesscookie = sc;
183
184 (void)config_found(self, &aa, wsemuldisplaydevprint);
185
186 /* Start with approximately 40% of full brightness. */
187 lcd_set_brightness(3);
188
189 (void)powerhook_establish(sc->dev.dv_xname, lcd_power, sc);
190 }
191
192 int
193 lcd_cnattach(void)
194 {
195
196 return pxa2x0_lcd_cnattach(&lcd_bpp16_screen, CURRENT_DISPLAY);
197 }
198
199 /*
200 * wsdisplay accessops overrides
201 */
202 int
203 lcd_ioctl(void *v, void *vs, u_long cmd, caddr_t data, int flag, struct lwp *l)
204 {
205 struct pxa2x0_lcd_softc *sc = (struct pxa2x0_lcd_softc *)v;
206 int res = EINVAL;
207
208 switch (cmd) {
209 case WSDISPLAYIO_GETPARAM:
210 case WSDISPLAYIO_SETPARAM:
211 res = lcd_param(sc, cmd, (struct wsdisplay_param *)data);
212 break;
213 }
214
215 if (res == EINVAL)
216 res = pxa2x0_lcd_ioctl(v, vs, cmd, data, flag, l);
217 return res;
218 }
219
220 void
221 lcd_burner(void *v, u_int on, u_int flags)
222 {
223
224 lcd_set_brightness(on ? lcd_get_brightness() : 0);
225 }
226
227 int
228 lcd_show_screen(void *v, void *cookie, int waitok,
229 void (*cb)(void *, int, int), void *cbarg)
230 {
231 int error;
232
233 error = pxa2x0_lcd_show_screen(v, cookie, waitok, cb, cbarg);
234 if (error)
235 return (error);
236
237 /* Turn on LCD */
238 lcd_burner(v, 1, 0);
239
240 return 0;
241 }
242
243 /*
244 * wsdisplay I/O controls
245 */
246 int
247 lcd_param(struct pxa2x0_lcd_softc *sc, u_long cmd,
248 struct wsdisplay_param *dp)
249 {
250 int res = EINVAL;
251
252 switch (dp->param) {
253 case WSDISPLAYIO_PARAM_BACKLIGHT:
254 if (cmd == WSDISPLAYIO_GETPARAM) {
255 dp->min = 0;
256 dp->max = 1;
257 dp->curval = lcd_get_backlight();
258 res = 0;
259 } else if (cmd == WSDISPLAYIO_SETPARAM) {
260 lcd_set_backlight(dp->curval);
261 res = 0;
262 }
263 break;
264
265 case WSDISPLAYIO_PARAM_CONTRAST:
266 /* unsupported */
267 res = ENOTTY;
268 break;
269
270 case WSDISPLAYIO_PARAM_BRIGHTNESS:
271 if (cmd == WSDISPLAYIO_GETPARAM) {
272 dp->min = 1;
273 dp->max = lcd_max_brightness();
274 dp->curval = lcd_get_brightness();
275 res = 0;
276 } else if (cmd == WSDISPLAYIO_SETPARAM) {
277 lcd_set_brightness(dp->curval);
278 res = 0;
279 }
280 break;
281 }
282
283 return res;
284 }
285
286 /*
287 * LCD backlight
288 */
289
290 static int lcdbrightnesscurval = 1;
291 static int lcdislit = 1;
292 static int lcdisblank = 0;
293
294 int
295 lcd_max_brightness(void)
296 {
297 int i;
298
299 for (i = 0; CURRENT_BACKLIGHT[i].duty != -1; i++)
300 continue;
301 return i - 1;
302 }
303
304 int
305 lcd_get_brightness(void)
306 {
307
308 return lcdbrightnesscurval;
309 }
310
311 void
312 lcd_set_brightness(int newval)
313 {
314 int maxval;
315
316 maxval = lcd_max_brightness();
317 if (newval < 0)
318 newval = 0;
319 else if (newval > maxval)
320 newval = maxval;
321
322 if (lcd_get_backlight() && !lcdisblank)
323 lcd_set_brightness_internal(newval);
324
325 if (newval > 0)
326 lcdbrightnesscurval = newval;
327 }
328
329 void
330 lcd_set_brightness_internal(int newval)
331 {
332 static int curval = 1;
333 int i;
334
335 /*
336 * It appears that the C3000 backlight can draw too much power if we
337 * switch it from a low to a high brightness. Increasing brightness
338 * in steps avoids this issue.
339 */
340 if (newval > curval) {
341 for (i = curval + 1; i <= newval; i++) {
342 (void)zssp_ic_send(ZSSP_IC_LZ9JG18,
343 CURRENT_BACKLIGHT[i].duty);
344 scoop_set_backlight(CURRENT_BACKLIGHT[i].on,
345 CURRENT_BACKLIGHT[i].cont);
346 delay(5000);
347 }
348 } else {
349 (void)zssp_ic_send(ZSSP_IC_LZ9JG18,
350 CURRENT_BACKLIGHT[newval].duty);
351 scoop_set_backlight(CURRENT_BACKLIGHT[newval].on,
352 CURRENT_BACKLIGHT[newval].cont);
353 }
354
355 curval = newval;
356 }
357
358 int
359 lcd_get_backlight(void)
360 {
361
362 return lcdislit;
363 }
364
365 void
366 lcd_set_backlight(int on)
367 {
368
369 if (!on) {
370 lcd_set_brightness(0);
371 lcdislit = 0;
372 } else {
373 lcdislit = 1;
374 lcd_set_brightness(lcd_get_brightness());
375 }
376 }
377
378 void
379 lcd_blank(int blank)
380 {
381
382 if (blank) {
383 lcd_set_brightness(0);
384 lcdisblank = 1;
385 } else {
386 lcdisblank = 0;
387 lcd_set_brightness(lcd_get_brightness());
388 }
389 }
390
391 void
392 lcd_power(int why, void *v)
393 {
394
395 switch (why) {
396 case PWR_SUSPEND:
397 case PWR_STANDBY:
398 lcd_set_brightness(0);
399 pxa2x0_lcd_power(why, v);
400 break;
401
402 case PWR_RESUME:
403 pxa2x0_lcd_power(why, v);
404 lcd_set_brightness(lcd_get_brightness());
405 break;
406 }
407 }
408