xref: /netbsd-src/sys/arch/zaurus/dev/zlcd.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /*	$NetBSD: zlcd.c,v 1.9 2009/01/29 16:00:33 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.9 2009/01/29 16:00:33 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 static bool	lcd_suspend(device_t dv PMF_FN_ARGS);
158 static bool	lcd_resume(device_t dv PMF_FN_ARGS);
159 
160 void	lcd_cnattach(void);
161 int	lcd_max_brightness(void);
162 int	lcd_get_brightness(void);
163 void	lcd_set_brightness(int);
164 void	lcd_set_brightness_internal(int);
165 int	lcd_get_backlight(void);
166 void	lcd_set_backlight(int);
167 void	lcd_blank(int);
168 
169 static int
170 lcd_match(device_t parent, cfdata_t cf, void *aux)
171 {
172 
173 	return 1;
174 }
175 
176 static void
177 lcd_attach(device_t parent, device_t self, void *aux)
178 {
179 	struct pxa2x0_lcd_softc *sc = device_private(self);
180 	struct wsemuldisplaydev_attach_args aa;
181 
182 	sc->dev = self;
183 
184 	pxa2x0_lcd_attach_sub(sc, aux, CURRENT_DISPLAY);
185 
186 	aa.console = glass_console;
187 	aa.scrdata = &lcd_screen_list;
188 	aa.accessops = &lcd_accessops;
189 	aa.accesscookie = sc;
190 
191 	(void) config_found(self, &aa, wsemuldisplaydevprint);
192 
193 	/* Start with approximately 40% of full brightness. */
194 	lcd_set_brightness(3);
195 
196 	if (!pmf_device_register(sc->dev, lcd_suspend, lcd_resume))
197 		aprint_error_dev(sc->dev, "couldn't establish power handler\n");
198 }
199 
200 void
201 lcd_cnattach(void)
202 {
203 
204 	pxa2x0_lcd_cnattach(&lcd_std_screen, CURRENT_DISPLAY);
205 }
206 
207 /*
208  * power management
209  */
210 static bool
211 lcd_suspend(device_t dv PMF_FN_ARGS)
212 {
213 	struct pxa2x0_lcd_softc *sc = device_private(dv);
214 
215 	lcd_set_brightness(0);
216 	pxa2x0_lcd_suspend(sc);
217 
218 	return true;
219 }
220 
221 static bool
222 lcd_resume(device_t dv PMF_FN_ARGS)
223 {
224 	struct pxa2x0_lcd_softc *sc = device_private(dv);
225 
226 	pxa2x0_lcd_resume(sc);
227 	lcd_set_brightness(lcd_get_brightness());
228 
229 	return true;
230 }
231 
232 /*
233  * wsdisplay accessops overrides
234  */
235 static int
236 lcd_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
237 {
238 	struct pxa2x0_lcd_softc *sc = (struct pxa2x0_lcd_softc *)v;
239 	struct hpcfb_fbconf *fbconf;
240 	struct hpcfb_dspconf *dspconf;
241 	int res = EINVAL;
242 
243 	switch (cmd) {
244 	case WSDISPLAYIO_GETPARAM:
245 	case WSDISPLAYIO_SETPARAM:
246 		res = lcd_param(sc, cmd, (struct wsdisplay_param *)data);
247 		break;
248 
249 	case HPCFBIO_GCONF:
250 		fbconf = (struct hpcfb_fbconf *)data;
251 		if (fbconf->hf_conf_index != 0 &&
252 		    fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
253 			break;
254 		}
255 
256 		fbconf->hf_conf_index = 0;
257 		fbconf->hf_nconfs = 1;
258 		fbconf->hf_class = HPCFB_CLASS_RGBCOLOR;
259 		strlcpy(fbconf->hf_name, "Sharp Zaurus frame buffer",
260 		    sizeof(fbconf->hf_name));
261 		strlcpy(fbconf->hf_conf_name, "default",
262 		    sizeof(fbconf->hf_conf_name));
263 		fbconf->hf_width = sc->geometry->panel_width;
264 		fbconf->hf_height = sc->geometry->panel_height;
265 		fbconf->hf_baseaddr = (u_long)sc->active->buf_va;
266 		fbconf->hf_offset = 0;
267 		fbconf->hf_bytes_per_line = sc->geometry->panel_width *
268 		    sc->active->depth / 8;
269 		fbconf->hf_nplanes = 1;
270 		fbconf->hf_bytes_per_plane = sc->geometry->panel_width *
271 		    sc->geometry->panel_height * sc->active->depth / 8;
272 		fbconf->hf_pack_width = sc->active->depth;
273 		fbconf->hf_pixels_per_pack = 1;
274 		fbconf->hf_pixel_width = sc->active->depth;
275 		fbconf->hf_access_flags = (0
276 					   | HPCFB_ACCESS_BYTE
277 					   | HPCFB_ACCESS_WORD
278 					   | HPCFB_ACCESS_DWORD);
279 		fbconf->hf_order_flags = 0;
280 		fbconf->hf_reg_offset = 0;
281 
282 		fbconf->hf_class_data_length = sizeof(struct hf_rgb_tag);
283 		fbconf->hf_u.hf_rgb.hf_flags = 0;
284 		fbconf->hf_u.hf_rgb.hf_red_width = 5;
285 		fbconf->hf_u.hf_rgb.hf_red_shift = 11;
286 		fbconf->hf_u.hf_rgb.hf_green_width = 6;
287 		fbconf->hf_u.hf_rgb.hf_green_shift = 5;
288 		fbconf->hf_u.hf_rgb.hf_blue_width = 5;
289 		fbconf->hf_u.hf_rgb.hf_blue_shift = 0;
290 		fbconf->hf_u.hf_rgb.hf_alpha_width = 0;
291 		fbconf->hf_u.hf_rgb.hf_alpha_shift = 0;
292 
293 		fbconf->hf_ext_size = 0;
294 		fbconf->hf_ext_data = NULL;
295 
296 		res = 0;
297 		break;
298 
299 	case HPCFBIO_SCONF:
300 		fbconf = (struct hpcfb_fbconf *)data;
301 		if (fbconf->hf_conf_index != 0 &&
302 		    fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
303 			break;
304 		}
305 		/* nothing to do because we have only one configuration */
306 		res = 0;
307 		break;
308 
309 	case HPCFBIO_GDSPCONF:
310 		dspconf = (struct hpcfb_dspconf *)data;
311 		if ((dspconf->hd_unit_index != 0 &&
312 		     dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
313 		    (dspconf->hd_conf_index != 0 &&
314 		     dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
315 			break;
316 		}
317 
318 		dspconf->hd_unit_index = 0;
319 		dspconf->hd_nunits = 1;
320 		dspconf->hd_class = HPCFB_DSP_CLASS_COLORLCD;
321 		strlcpy(dspconf->hd_name, "Sharp Zaurus LCD",
322 		    sizeof(dspconf->hd_name));
323 		dspconf->hd_op_flags = 0;
324 		dspconf->hd_conf_index = 0;
325 		dspconf->hd_nconfs = 1;
326 		strlcpy(dspconf->hd_conf_name, "default",
327 		    sizeof(dspconf->hd_conf_name));
328 		dspconf->hd_width = sc->geometry->panel_width;
329 		dspconf->hd_height = sc->geometry->panel_height;
330 		dspconf->hd_xdpi = HPCFB_DSP_DPI_UNKNOWN;
331 		dspconf->hd_ydpi = HPCFB_DSP_DPI_UNKNOWN;
332 
333 		res = 0;
334 		break;
335 
336 	case HPCFBIO_SDSPCONF:
337 		dspconf = (struct hpcfb_dspconf *)data;
338 		if ((dspconf->hd_unit_index != 0 &&
339 		     dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
340 		    (dspconf->hd_conf_index != 0 &&
341 		     dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
342 			break;
343 		}
344 		/*
345 		 * nothing to do
346 		 * because we have only one unit and one configuration
347 		 */
348 		res = 0;
349 		break;
350 
351 	case HPCFBIO_GOP:
352 	case HPCFBIO_SOP:
353 		/* curently not implemented...  */
354 		break;
355 	}
356 
357 	if (res == EINVAL)
358 		res = pxa2x0_lcd_ioctl(v, vs, cmd, data, flag, l);
359 	return res;
360 }
361 
362 static int
363 lcd_show_screen(void *v, void *cookie, int waitok,
364     void (*cb_func)(void *, int, int), void *cb_arg)
365 {
366 	int error;
367 
368 	error = pxa2x0_lcd_show_screen(v, cookie, waitok, cb_func, cb_arg);
369 	if (error)
370 		return (error);
371 
372 	/* Turn on LCD */
373 	lcd_set_brightness(lcd_get_brightness());
374 
375 	return 0;
376 }
377 
378 /*
379  * wsdisplay I/O controls
380  */
381 static int
382 lcd_param(struct pxa2x0_lcd_softc *sc, u_long cmd, struct wsdisplay_param *dp)
383 {
384 	int res = EINVAL;
385 
386 	switch (dp->param) {
387 	case WSDISPLAYIO_PARAM_BACKLIGHT:
388 		if (cmd == WSDISPLAYIO_GETPARAM) {
389 			dp->min = 0;
390 			dp->max = 1;
391 			dp->curval = lcd_get_backlight();
392 			res = 0;
393 		} else if (cmd == WSDISPLAYIO_SETPARAM) {
394 			lcd_set_backlight(dp->curval);
395 			res = 0;
396 		}
397 		break;
398 
399 	case WSDISPLAYIO_PARAM_CONTRAST:
400 		/* unsupported */
401 		res = ENOTTY;
402 		break;
403 
404 	case WSDISPLAYIO_PARAM_BRIGHTNESS:
405 		if (cmd == WSDISPLAYIO_GETPARAM) {
406 			dp->min = 1;
407 			dp->max = lcd_max_brightness();
408 			dp->curval = lcd_get_brightness();
409 			res = 0;
410 		} else if (cmd == WSDISPLAYIO_SETPARAM) {
411 			lcd_set_brightness(dp->curval);
412 			res = 0;
413 		}
414 		break;
415 	}
416 
417 	return res;
418 }
419 
420 /*
421  * LCD backlight
422  */
423 
424 static	int lcdbrightnesscurval = 1;
425 static	int lcdislit = 1;
426 static	int lcdisblank = 0;
427 
428 int
429 lcd_max_brightness(void)
430 {
431 	int i;
432 
433 	for (i = 0; CURRENT_BACKLIGHT[i].duty != -1; i++)
434 		continue;
435 	return i - 1;
436 }
437 
438 int
439 lcd_get_brightness(void)
440 {
441 
442 	return lcdbrightnesscurval;
443 }
444 
445 void
446 lcd_set_brightness(int newval)
447 {
448 	int maxval;
449 
450 	maxval = lcd_max_brightness();
451 	if (newval < 0)
452 		newval = 0;
453 	else if (newval > maxval)
454 		newval = maxval;
455 
456 	if (lcd_get_backlight() && !lcdisblank)
457 		lcd_set_brightness_internal(newval);
458 
459 	if (newval > 0)
460 		lcdbrightnesscurval = newval;
461 }
462 
463 void
464 lcd_set_brightness_internal(int newval)
465 {
466 	static int curval = 1;
467 	int i;
468 
469 	/*
470 	 * It appears that the C3000 backlight can draw too much power if we
471 	 * switch it from a low to a high brightness.  Increasing brightness
472 	 * in steps avoids this issue.
473 	 */
474 	if (newval > curval) {
475 		for (i = curval + 1; i <= newval; i++) {
476 			(void)zssp_ic_send(ZSSP_IC_LZ9JG18,
477 			    CURRENT_BACKLIGHT[i].duty);
478 			scoop_set_backlight(CURRENT_BACKLIGHT[i].on,
479 			    CURRENT_BACKLIGHT[i].cont);
480 			delay(5000);
481 		}
482 	} else {
483 		(void)zssp_ic_send(ZSSP_IC_LZ9JG18,
484 		    CURRENT_BACKLIGHT[newval].duty);
485 		scoop_set_backlight(CURRENT_BACKLIGHT[newval].on,
486 		    CURRENT_BACKLIGHT[newval].cont);
487 	}
488 
489 	curval = newval;
490 }
491 
492 int
493 lcd_get_backlight(void)
494 {
495 
496 	return lcdislit;
497 }
498 
499 void
500 lcd_set_backlight(int onoff)
501 {
502 
503 	if (!onoff) {
504 		lcd_set_brightness(0);
505 		lcdislit = 0;
506 	} else {
507 		lcdislit = 1;
508 		lcd_set_brightness(lcd_get_brightness());
509 	}
510 }
511 
512 void
513 lcd_blank(int blank)
514 {
515 
516 	if (blank) {
517 		lcd_set_brightness(0);
518 		lcdisblank = 1;
519 	} else {
520 		lcdisblank = 0;
521 		lcd_set_brightness(lcd_get_brightness());
522 	}
523 }
524