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