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