zlcd.c revision 1.8 1 /* $NetBSD: zlcd.c,v 1.8 2009/01/29 12:28:15 nonaka 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.8 2009/01/29 12:28:15 nonaka 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 <dev/hpc/hpcfbio.h>
60
61 #include <machine/bus.h>
62 #include <arm/xscale/pxa2x0var.h>
63 #include <arm/xscale/pxa2x0reg.h>
64 #include <arm/xscale/pxa2x0_lcd.h>
65
66 #include <zaurus/dev/scoopvar.h>
67 #include <zaurus/dev/zsspvar.h>
68 #include <zaurus/zaurus/zaurus_var.h>
69
70 /*
71 * wsdisplay glue
72 */
73 static struct pxa2x0_wsscreen_descr lcd_std_screen = {
74 .c = {
75 .name = "std",
76 .textops = &pxa2x0_lcd_emulops,
77 .fontwidth = 8,
78 .fontheight = 16,
79 .capabilities = WSSCREEN_WSCOLORS,
80 },
81 .depth = 16, /* bits per pixel */
82 .flags = RI_ROTATE_CW, /* quarter clockwise rotation */
83 };
84
85 static const struct wsscreen_descr *lcd_scr_descr[] = {
86 &lcd_std_screen.c
87 };
88
89 static const struct wsscreen_list lcd_screen_list = {
90 .nscreens = __arraycount(lcd_scr_descr),
91 .screens = lcd_scr_descr,
92 };
93
94 static int lcd_ioctl(void *, void *, u_long, void *, int, struct lwp *);
95 static int lcd_param(struct pxa2x0_lcd_softc *, u_long,
96 struct wsdisplay_param *);
97 static int lcd_show_screen(void *, void *, int,
98 void (*)(void *, int, int), void *);
99
100 struct wsdisplay_accessops lcd_accessops = {
101 lcd_ioctl,
102 pxa2x0_lcd_mmap,
103 pxa2x0_lcd_alloc_screen,
104 pxa2x0_lcd_free_screen,
105 lcd_show_screen,
106 NULL,
107 NULL,
108 NULL,
109 };
110
111 #define CURRENT_DISPLAY &sharp_zaurus_C3000
112
113 const struct lcd_panel_geometry sharp_zaurus_C3000 =
114 {
115 480, /* Width */
116 640, /* Height */
117 0, /* No extra lines */
118
119 LCDPANEL_ACTIVE | LCDPANEL_VSP | LCDPANEL_HSP,
120 1, /* clock divider */
121 0, /* AC bias pin freq */
122
123 0x28, /* horizontal sync pulse width */
124 0x2e, /* BLW */
125 0x7d, /* ELW */
126
127 2, /* vertical sync pulse width */
128 1, /* BFW */
129 0, /* EFW */
130 };
131
132 struct sharp_lcd_backlight {
133 int duty; /* LZ9JG18 DAC value */
134 int cont; /* BACKLIGHT_CONT signal */
135 int on; /* BACKLIGHT_ON signal */
136 };
137
138 #define CURRENT_BACKLIGHT sharp_zaurus_C3000_bl
139
140 const struct sharp_lcd_backlight sharp_zaurus_C3000_bl[] = {
141 { 0x00, 0, 0 }, /* 0: Off */
142 { 0x00, 0, 1 }, /* 1: 0% */
143 { 0x01, 0, 1 }, /* 2: 20% */
144 { 0x07, 0, 1 }, /* 3: 40% */
145 { 0x01, 1, 1 }, /* 4: 60% */
146 { 0x07, 1, 1 }, /* 5: 80% */
147 { 0x11, 1, 1 }, /* 6: 100% */
148 { -1, -1, -1 }, /* 7: Invalid */
149 };
150
151 static int lcd_match(device_t, cfdata_t, void *);
152 static void lcd_attach(device_t, device_t, void *);
153
154 CFATTACH_DECL_NEW(zlcd, sizeof(struct pxa2x0_lcd_softc),
155 lcd_match, lcd_attach, NULL, NULL);
156
157 void lcd_cnattach(void);
158 int lcd_max_brightness(void);
159 int lcd_get_brightness(void);
160 void lcd_set_brightness(int);
161 void lcd_set_brightness_internal(int);
162 int lcd_get_backlight(void);
163 void lcd_set_backlight(int);
164 void lcd_blank(int);
165 void lcd_power(int, void *);
166
167 static int
168 lcd_match(device_t parent, cfdata_t cf, void *aux)
169 {
170
171 return 1;
172 }
173
174 static void
175 lcd_attach(device_t parent, device_t self, void *aux)
176 {
177 struct pxa2x0_lcd_softc *sc = device_private(self);
178 struct wsemuldisplaydev_attach_args aa;
179
180 sc->dev = self;
181
182 pxa2x0_lcd_attach_sub(sc, aux, CURRENT_DISPLAY);
183
184 aa.console = glass_console;
185 aa.scrdata = &lcd_screen_list;
186 aa.accessops = &lcd_accessops;
187 aa.accesscookie = sc;
188
189 (void) config_found(self, &aa, wsemuldisplaydevprint);
190
191 /* Start with approximately 40% of full brightness. */
192 lcd_set_brightness(3);
193
194 (void) powerhook_establish(device_xname(sc->dev), lcd_power, sc);
195 }
196
197 void
198 lcd_cnattach(void)
199 {
200
201 pxa2x0_lcd_cnattach(&lcd_std_screen, CURRENT_DISPLAY);
202 }
203
204 /*
205 * wsdisplay accessops overrides
206 */
207 static int
208 lcd_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
209 {
210 struct pxa2x0_lcd_softc *sc = (struct pxa2x0_lcd_softc *)v;
211 struct hpcfb_fbconf *fbconf;
212 struct hpcfb_dspconf *dspconf;
213 int res = EINVAL;
214
215 switch (cmd) {
216 case WSDISPLAYIO_GETPARAM:
217 case WSDISPLAYIO_SETPARAM:
218 res = lcd_param(sc, cmd, (struct wsdisplay_param *)data);
219 break;
220
221 case HPCFBIO_GCONF:
222 fbconf = (struct hpcfb_fbconf *)data;
223 if (fbconf->hf_conf_index != 0 &&
224 fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
225 break;
226 }
227
228 fbconf->hf_conf_index = 0;
229 fbconf->hf_nconfs = 1;
230 fbconf->hf_class = HPCFB_CLASS_RGBCOLOR;
231 strlcpy(fbconf->hf_name, "Sharp Zaurus frame buffer",
232 sizeof(fbconf->hf_name));
233 strlcpy(fbconf->hf_conf_name, "default",
234 sizeof(fbconf->hf_conf_name));
235 fbconf->hf_width = sc->geometry->panel_width;
236 fbconf->hf_height = sc->geometry->panel_height;
237 fbconf->hf_baseaddr = (u_long)sc->active->buf_va;
238 fbconf->hf_offset = 0;
239 fbconf->hf_bytes_per_line = sc->geometry->panel_width *
240 sc->active->depth / 8;
241 fbconf->hf_nplanes = 1;
242 fbconf->hf_bytes_per_plane = sc->geometry->panel_width *
243 sc->geometry->panel_height * sc->active->depth / 8;
244 fbconf->hf_pack_width = sc->active->depth;
245 fbconf->hf_pixels_per_pack = 1;
246 fbconf->hf_pixel_width = sc->active->depth;
247 fbconf->hf_access_flags = (0
248 | HPCFB_ACCESS_BYTE
249 | HPCFB_ACCESS_WORD
250 | HPCFB_ACCESS_DWORD);
251 fbconf->hf_order_flags = 0;
252 fbconf->hf_reg_offset = 0;
253
254 fbconf->hf_class_data_length = sizeof(struct hf_rgb_tag);
255 fbconf->hf_u.hf_rgb.hf_flags = 0;
256 fbconf->hf_u.hf_rgb.hf_red_width = 5;
257 fbconf->hf_u.hf_rgb.hf_red_shift = 11;
258 fbconf->hf_u.hf_rgb.hf_green_width = 6;
259 fbconf->hf_u.hf_rgb.hf_green_shift = 5;
260 fbconf->hf_u.hf_rgb.hf_blue_width = 5;
261 fbconf->hf_u.hf_rgb.hf_blue_shift = 0;
262 fbconf->hf_u.hf_rgb.hf_alpha_width = 0;
263 fbconf->hf_u.hf_rgb.hf_alpha_shift = 0;
264
265 fbconf->hf_ext_size = 0;
266 fbconf->hf_ext_data = NULL;
267
268 res = 0;
269 break;
270
271 case HPCFBIO_SCONF:
272 fbconf = (struct hpcfb_fbconf *)data;
273 if (fbconf->hf_conf_index != 0 &&
274 fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
275 break;
276 }
277 /* nothing to do because we have only one configuration */
278 res = 0;
279 break;
280
281 case HPCFBIO_GDSPCONF:
282 dspconf = (struct hpcfb_dspconf *)data;
283 if ((dspconf->hd_unit_index != 0 &&
284 dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
285 (dspconf->hd_conf_index != 0 &&
286 dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
287 break;
288 }
289
290 dspconf->hd_unit_index = 0;
291 dspconf->hd_nunits = 1;
292 dspconf->hd_class = HPCFB_DSP_CLASS_COLORLCD;
293 strlcpy(dspconf->hd_name, "Sharp Zaurus LCD",
294 sizeof(dspconf->hd_name));
295 dspconf->hd_op_flags = 0;
296 dspconf->hd_conf_index = 0;
297 dspconf->hd_nconfs = 1;
298 strlcpy(dspconf->hd_conf_name, "default",
299 sizeof(dspconf->hd_conf_name));
300 dspconf->hd_width = sc->geometry->panel_width;
301 dspconf->hd_height = sc->geometry->panel_height;
302 dspconf->hd_xdpi = HPCFB_DSP_DPI_UNKNOWN;
303 dspconf->hd_ydpi = HPCFB_DSP_DPI_UNKNOWN;
304
305 res = 0;
306 break;
307
308 case HPCFBIO_SDSPCONF:
309 dspconf = (struct hpcfb_dspconf *)data;
310 if ((dspconf->hd_unit_index != 0 &&
311 dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
312 (dspconf->hd_conf_index != 0 &&
313 dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
314 break;
315 }
316 /*
317 * nothing to do
318 * because we have only one unit and one configuration
319 */
320 res = 0;
321 break;
322
323 case HPCFBIO_GOP:
324 case HPCFBIO_SOP:
325 /* curently not implemented... */
326 break;
327 }
328
329 if (res == EINVAL)
330 res = pxa2x0_lcd_ioctl(v, vs, cmd, data, flag, l);
331 return res;
332 }
333
334 static int
335 lcd_show_screen(void *v, void *cookie, int waitok,
336 void (*cb_func)(void *, int, int), void *cb_arg)
337 {
338 int error;
339
340 error = pxa2x0_lcd_show_screen(v, cookie, waitok, cb_func, cb_arg);
341 if (error)
342 return (error);
343
344 /* Turn on LCD */
345 lcd_set_brightness(lcd_get_brightness());
346
347 return 0;
348 }
349
350 /*
351 * wsdisplay I/O controls
352 */
353 static int
354 lcd_param(struct pxa2x0_lcd_softc *sc, u_long cmd, struct wsdisplay_param *dp)
355 {
356 int res = EINVAL;
357
358 switch (dp->param) {
359 case WSDISPLAYIO_PARAM_BACKLIGHT:
360 if (cmd == WSDISPLAYIO_GETPARAM) {
361 dp->min = 0;
362 dp->max = 1;
363 dp->curval = lcd_get_backlight();
364 res = 0;
365 } else if (cmd == WSDISPLAYIO_SETPARAM) {
366 lcd_set_backlight(dp->curval);
367 res = 0;
368 }
369 break;
370
371 case WSDISPLAYIO_PARAM_CONTRAST:
372 /* unsupported */
373 res = ENOTTY;
374 break;
375
376 case WSDISPLAYIO_PARAM_BRIGHTNESS:
377 if (cmd == WSDISPLAYIO_GETPARAM) {
378 dp->min = 1;
379 dp->max = lcd_max_brightness();
380 dp->curval = lcd_get_brightness();
381 res = 0;
382 } else if (cmd == WSDISPLAYIO_SETPARAM) {
383 lcd_set_brightness(dp->curval);
384 res = 0;
385 }
386 break;
387 }
388
389 return res;
390 }
391
392 /*
393 * LCD backlight
394 */
395
396 static int lcdbrightnesscurval = 1;
397 static int lcdislit = 1;
398 static int lcdisblank = 0;
399
400 int
401 lcd_max_brightness(void)
402 {
403 int i;
404
405 for (i = 0; CURRENT_BACKLIGHT[i].duty != -1; i++)
406 continue;
407 return i - 1;
408 }
409
410 int
411 lcd_get_brightness(void)
412 {
413
414 return lcdbrightnesscurval;
415 }
416
417 void
418 lcd_set_brightness(int newval)
419 {
420 int maxval;
421
422 maxval = lcd_max_brightness();
423 if (newval < 0)
424 newval = 0;
425 else if (newval > maxval)
426 newval = maxval;
427
428 if (lcd_get_backlight() && !lcdisblank)
429 lcd_set_brightness_internal(newval);
430
431 if (newval > 0)
432 lcdbrightnesscurval = newval;
433 }
434
435 void
436 lcd_set_brightness_internal(int newval)
437 {
438 static int curval = 1;
439 int i;
440
441 /*
442 * It appears that the C3000 backlight can draw too much power if we
443 * switch it from a low to a high brightness. Increasing brightness
444 * in steps avoids this issue.
445 */
446 if (newval > curval) {
447 for (i = curval + 1; i <= newval; i++) {
448 (void)zssp_ic_send(ZSSP_IC_LZ9JG18,
449 CURRENT_BACKLIGHT[i].duty);
450 scoop_set_backlight(CURRENT_BACKLIGHT[i].on,
451 CURRENT_BACKLIGHT[i].cont);
452 delay(5000);
453 }
454 } else {
455 (void)zssp_ic_send(ZSSP_IC_LZ9JG18,
456 CURRENT_BACKLIGHT[newval].duty);
457 scoop_set_backlight(CURRENT_BACKLIGHT[newval].on,
458 CURRENT_BACKLIGHT[newval].cont);
459 }
460
461 curval = newval;
462 }
463
464 int
465 lcd_get_backlight(void)
466 {
467
468 return lcdislit;
469 }
470
471 void
472 lcd_set_backlight(int onoff)
473 {
474
475 if (!onoff) {
476 lcd_set_brightness(0);
477 lcdislit = 0;
478 } else {
479 lcdislit = 1;
480 lcd_set_brightness(lcd_get_brightness());
481 }
482 }
483
484 void
485 lcd_blank(int blank)
486 {
487
488 if (blank) {
489 lcd_set_brightness(0);
490 lcdisblank = 1;
491 } else {
492 lcdisblank = 0;
493 lcd_set_brightness(lcd_get_brightness());
494 }
495 }
496
497 void
498 lcd_power(int why, void *v)
499 {
500
501 switch (why) {
502 case PWR_SUSPEND:
503 case PWR_STANDBY:
504 lcd_set_brightness(0);
505 pxa2x0_lcd_power(why, v);
506 break;
507
508 case PWR_RESUME:
509 pxa2x0_lcd_power(why, v);
510 lcd_set_brightness(lcd_get_brightness());
511 break;
512 }
513 }
514