xref: /netbsd-src/sys/arch/evbarm/smdk2xx0/smdk2410_lcd.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: smdk2410_lcd.c,v 1.3 2007/03/04 05:59:45 christos Exp $ */
2 
3 /*
4  * Copyright (c) 2004  Genetec Corporation.  All rights reserved.
5  * Written by Hiroyuki Bessho for Genetec Corporation.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of Genetec Corporation may not be used to endorse or
16  *    promote products derived from this software without specific prior
17  *    written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: smdk2410_lcd.c,v 1.3 2007/03/04 05:59:45 christos Exp $");
35 
36 /*
37  * LCD driver for Samsung SMDK2410.
38  *
39  * Controlling LCD is almost completely done through S3C2410's
40  * integrated LCD controller.  Codes for it is arm/s3c2xx0/s3c24x0_lcd.c.
41  *
42  * Codes in this file provide the platform's LCD panel information.
43  */
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/conf.h>
48 #include <sys/event.h>
49 #include <sys/uio.h>
50 #include <sys/malloc.h>
51 
52 #include <dev/cons.h>
53 #include <dev/wscons/wsconsio.h>
54 #include <dev/wscons/wsdisplayvar.h>
55 #include <dev/wscons/wscons_callbacks.h>
56 
57 #include <machine/bus.h>
58 #include <arm/s3c2xx0/s3c24x0var.h>
59 #include <arm/s3c2xx0/s3c24x0reg.h>
60 #include <arm/s3c2xx0/s3c2410reg.h>
61 #include <arm/s3c2xx0/s3c24x0_lcd.h>
62 
63 #include "wsdisplay.h"
64 
65 int	lcd_match(struct device *, struct cfdata *, void *);
66 void	lcd_attach(struct device *, struct device *, void *);
67 
68 #ifdef LCD_DEBUG
69 void draw_test_pattern(struct s3c24x0_lcd_softc *,
70 	    struct s3c24x0_lcd_screen *scr);
71 #endif
72 
73 #if NWSDISPLAY > 0
74 
75 /*
76  * Screen geometries.
77  *
78  * S3C24x0's LCD controller can have virtual screens that are bigger
79  * than actual LCD panel. (XXX: wscons can't manage such screens for now)
80  */
81 struct s3c24x0_wsscreen_descr lcd_bpp16_30x32 = {
82 	{
83 		"30x32bpp16", 30, 32,
84 		&s3c24x0_lcd_emulops,
85 		8, 10,
86 		WSSCREEN_WSCOLORS,
87 	},
88 	16				/* bits per pixel */
89 },  lcd_bpp16_30x20 = {
90 	{
91 		"30x20bpp16", 30, 20,
92 		&s3c24x0_lcd_emulops,
93 		8, 16,
94 		WSSCREEN_WSCOLORS,
95 	},
96 	16
97 }, lcd_bpp8_30x32 = {
98 	{
99 		"30x32bpp8", 30, 32,
100 		&s3c24x0_lcd_emulops,
101 		8, 10,
102 		WSSCREEN_WSCOLORS,
103 	},
104 	8
105 }, lcd_bpp8_40x25 = {			/* with big virtual screen */
106 	{
107 		"40x25bpp8", 40, 25,
108 		&s3c24x0_lcd_emulops,
109 		8, 16,
110 		WSSCREEN_WSCOLORS,
111 	},
112 	8
113 }, lcd_bpp4_30x32 = {
114 	{
115 		"30x32bpp4", 30, 32,
116 		&s3c24x0_lcd_emulops,
117 		8, 10,
118 		0,			/* 4bpp mode is grayscale */
119 	},
120 	4
121 };
122 
123 
124 static const struct wsscreen_descr *lcd_scr_descr[] = {
125 #if 0
126 	/* bpp4 needs a patch to rasops4 */
127 	&lcd_bpp4_30x32.c,
128 #endif
129 	&lcd_bpp8_30x32.c,
130 	&lcd_bpp8_40x25.c,
131 	&lcd_bpp16_30x32.c,
132 	&lcd_bpp16_30x20.c,
133 };
134 
135 #define	N_SCR_DESCR	(sizeof lcd_scr_descr / sizeof lcd_scr_descr[0])
136 
137 const struct wsscreen_list lcd_screen_list = {
138 	N_SCR_DESCR,
139 	lcd_scr_descr
140 };
141 
142 const struct wsdisplay_accessops lcd_accessops = {
143 	s3c24x0_lcd_ioctl,
144 	s3c24x0_lcd_mmap,
145 	s3c24x0_lcd_alloc_screen,
146 	s3c24x0_lcd_free_screen,
147 	s3c24x0_lcd_show_screen,
148 	NULL, /* load_font */
149 };
150 
151 #else  /* NWSDISPLAY */
152 /*
153  * Interface to LCD framebuffer without wscons
154  */
155 extern struct cfdriver lcd_cd;
156 
157 dev_type_open(lcdopen);
158 dev_type_close(lcdclose);
159 dev_type_ioctl(lcdioctl);
160 dev_type_mmap(lcdmmap);
161 const struct cdevsw lcd_cdevsw = {
162 	lcdopen, lcdclose, noread, nowrite, lcdioctl,
163 	nostop, notty, nopoll, lcdmmap, nokqfilter, D_TTY
164 };
165 
166 #endif /* NWSDISPLAY */
167 
168 CFATTACH_DECL(lcd_ssio, sizeof (struct s3c24x0_lcd_softc),  lcd_match,
169     lcd_attach, NULL, NULL);
170 
171 int
172 lcd_match(struct device *parent, struct cfdata *cf, void *aux)
173 {
174 	struct s3c2xx0_attach_args *sa = aux;
175 
176 	if (sa->sa_addr == SSIOCF_ADDR_DEFAULT)
177 		sa->sa_addr = S3C2410_LCDC_BASE;
178 
179 	return 1;
180 }
181 
182 static const struct s3c24x0_lcd_panel_info samsung_LTS350Q1 =
183 {
184     240,			/* Width */
185     320,			/* Height */
186     10*1000*1000,		/* pixel clock = 10MHz */
187 
188 #define	_(field, val)	(((val)-1) << (field##_SHIFT))
189 
190     LCDCON1_PNRMODE_TFT,
191 
192     /* LCDCON2: vertical timings */
193     _(LCDCON2_VBPD, 2) |
194     _(LCDCON2_VFPD, 3) |
195     _(LCDCON2_LINEVAL, 320) |
196     _(LCDCON2_VPSW, 2),
197 
198     /* LCDCON3: horizontal timings */
199     _(LCDCON3_HBPD, 7) |
200     _(LCDCON3_HFPD, 3),
201 
202     /* LCDCON4: horizontaol pulse width */
203     _(LCDCON4_HPSW, 4),
204 
205     /* LCDCON5: signal polarities */
206     LCDCON5_FRM565 | LCDCON5_INVVLINE | LCDCON5_INVVFRAME,
207 
208     /* LPCSEL register */
209     LPCSEL_LPC_EN | LPCSEL_RES_SEL | LPCSEL_MODE_SEL,
210 #undef _
211 };
212 
213 void
214 lcd_attach(struct device *parent, struct device *self, void *aux)
215 {
216 	struct s3c24x0_lcd_softc *sc = (struct s3c24x0_lcd_softc *)self;
217 	bus_space_tag_t iot =  s3c2xx0_softc->sc_iot;
218 	bus_space_handle_t gpio_ioh = s3c2xx0_softc->sc_gpio_ioh;
219 #if NWSDISPLAY > 0
220 	struct wsemuldisplaydev_attach_args aa;
221 #else
222 	struct s3c24x0_lcd_screen *screen;
223 #endif
224 
225 
226 	aprint_normal( "\n" );
227 
228 	/* setup GPIO ports for LCD */
229 	/* XXX: some LCD panels may not need all VD signals */
230 	gpio_ioh = s3c2xx0_softc->sc_gpio_ioh;
231 	bus_space_write_4(iot, gpio_ioh, GPIO_PCUP, ~0);
232 	bus_space_write_4(iot, gpio_ioh, GPIO_PCCON, 0xaaaaaaaa);
233 	bus_space_write_4(iot, gpio_ioh, GPIO_PDUP, ~0);
234 	bus_space_write_4(iot, gpio_ioh, GPIO_PDCON, 0xaaaaaaaa);
235 
236 	s3c24x0_lcd_attach_sub(sc, aux, &samsung_LTS350Q1);
237 
238 #if NWSDISPLAY > 0
239 
240 	aa.console = 0;
241 	aa.scrdata = &lcd_screen_list;
242 	aa.accessops = &lcd_accessops;
243 	aa.accesscookie = sc;
244 
245 
246 	(void) config_found(self, &aa, wsemuldisplaydevprint);
247 #else
248 
249 	screen = s3c24x0_lcd_new_screen(sc, 240, 320, 16);
250 
251 	if( screen ){
252 		sc->active = screen;
253 		s3c24x0_lcd_start_dma(sc, screen);
254 		s3c24x0_lcd_power(sc, 1);
255 	}
256 #endif /* NWSDISPLAY */
257 
258 }
259 
260 #if NWSDISPLAY == 0
261 
262 int
263 lcdopen( dev_t dev, int oflags, int devtype, struct proc *p )
264 {
265 	return 0;
266 }
267 
268 int
269 lcdclose( dev_t dev, int fflag, int devtype, struct proc *p )
270 {
271 	return 0;
272 }
273 
274 paddr_t
275 lcdmmap( dev_t dev, off_t offset, int size )
276 {
277 	struct s3c24x0_lcd_softc *sc = device_lookup(&lcd_cd, minor(dev));
278 	struct s3c24x0_lcd_screen *scr = sc->active;
279 
280 	return bus_dmamem_mmap(sc->dma_tag, scr->segs, scr->nsegs,
281 	    offset, 0, BUS_DMA_WAITOK|BUS_DMA_COHERENT);
282 }
283 
284 int
285 lcdioctl( dev_t dev, u_long cmd, void *data,
286 	    int fflag, struct lwp *l )
287 {
288 	return EOPNOTSUPP;
289 }
290 
291 #endif /* NWSDISPLAY>0 */
292 
293 #ifdef LCD_DEBUG
294 static void
295 draw_test_pattern_16(struct s3c24x0_lcd_softc *sc,
296     struct s3c24x0_lcd_screen *scr)
297 {
298 	int x, y;
299 	uint16_t color, *line;
300 	char *buf = (char *)(scr->buf_va);
301 
302 #define	rgb(r,g,b)	(((r)<<11) | ((g)<<5) | (b))
303 
304 	for (y=0; y < sc->panel_info->panel_height; ++y) {
305 		line = (uint16_t *)(buf + scr->stride * y);
306 
307 		for (x=0; x < sc->panel_info->panel_width; ++x) {
308 			switch (((x/30) + (y/10)) % 8) {
309 			default:
310 			case 0: color = rgb(0x00, 0x00, 0x00); break;
311 			case 1: color = rgb(0x00, 0x00, 0x1f); break;
312 			case 2: color = rgb(0x00, 0x3f, 0x00); break;
313 			case 3: color = rgb(0x00, 0x3f, 0x1f); break;
314 			case 4: color = rgb(0x1f, 0x00, 0x00); break;
315 			case 5: color = rgb(0x1f, 0x00, 0x1f); break;
316 			case 6: color = rgb(0x1f, 0x3f, 0x00); break;
317 			case 7: color = rgb(0x1f, 0x3f, 0x1f); break;
318 			}
319 
320 			line[x] = color;
321 		}
322 	}
323 
324 	for (x=0; x < MIN(sc->panel_info->panel_height,
325 		 sc->panel_info->panel_width); ++x) {
326 
327 		line = (uint16_t *)(buf + scr->stride * x);
328 		line[x] = rgb(0x1f, 0x3f, 0x1f);
329 	}
330 }
331 
332 static void
333 draw_test_pattern_8(struct s3c24x0_lcd_softc *sc,
334     struct s3c24x0_lcd_screen *scr)
335 {
336 	int x, y;
337 	uint8_t *line;
338 	char *buf = (char *)(scr->buf_va);
339 
340 
341 	for (y=0; y < sc->panel_info->panel_height; ++y) {
342 		line = (uint8_t *)(buf + scr->stride * y);
343 
344 		for (x=0; x < sc->panel_info->panel_width; ++x) {
345 			line[x] = (((x/15) + (y/20)) % 16);
346 		}
347 	}
348 
349 	for (x=0; x < MIN(sc->panel_info->panel_height,
350 		 sc->panel_info->panel_width); ++x) {
351 
352 		line = (uint8_t *)(buf + scr->stride * x);
353 		line[x] = 7;
354 	}
355 }
356 
357 
358 void
359 draw_test_pattern(struct s3c24x0_lcd_softc *sc, struct s3c24x0_lcd_screen *scr)
360 {
361 	switch (scr->depth) {
362 	case 16:
363 		draw_test_pattern_16(sc, scr);
364 		break;
365 	case 8:
366 		draw_test_pattern_8(sc, scr);
367 		break;
368 	}
369 }
370 
371 
372 #endif /* LCD_DEBUG */
373 
374