xref: /netbsd-src/sys/arch/evbarm/mini2440/mini2440_lcd.c (revision 2ddc9e7fa6d85f44007bbaf0a4d7202a54f42b23)
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.8 2024/03/06 20:18:42 andvar 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 
79 #include <dev/cons.h>
80 #include <dev/wscons/wsconsio.h>
81 #include <dev/wscons/wsdisplayvar.h>
82 #include <dev/wscons/wscons_callbacks.h>
83 
84 #include <sys/bus.h>
85 #include <arm/s3c2xx0/s3c24x0var.h>
86 #include <arm/s3c2xx0/s3c24x0reg.h>
87 #include <arm/s3c2xx0/s3c2410reg.h>
88 #include <arm/s3c2xx0/s3c24x0_lcd.h>
89 
90 #include "wsdisplay.h"
91 
92 int	lcd_match(device_t, cfdata_t, void *);
93 void	lcd_attach(device_t, device_t, void *);
94 
95 #ifdef LCD_DEBUG
96 void draw_test_pattern(struct s3c24x0_lcd_softc *,
97 	    struct s3c24x0_lcd_screen *scr);
98 #endif
99 
100 #if NWSDISPLAY > 0
101 
102 /*
103  * Screen geometries.
104  *
105  * S3C24x0's LCD controller can have virtual screens that are bigger
106  * than actual LCD panel. (XXX: wscons can't manage such screens for now)
107  */
108 struct s3c24x0_wsscreen_descr lcd_bpp16_30x32 = {
109 	{
110 		"30x32bpp16", 30, 32,
111 		&s3c24x0_lcd_emulops,
112 		8, 10,
113 		WSSCREEN_WSCOLORS,
114 	},
115 	16				/* bits per pixel */
116 },  lcd_bpp16_30x20 = {
117 	{
118 		"30x20bpp16", 30, 20,
119 		&s3c24x0_lcd_emulops,
120 		8, 16,
121 		WSSCREEN_WSCOLORS,
122 	},
123 	16
124 }, lcd_bpp8_30x32 = {
125 	{
126 		"30x32bpp8", 30, 32,
127 		&s3c24x0_lcd_emulops,
128 		8, 10,
129 		WSSCREEN_WSCOLORS,
130 	},
131 	8
132 }, lcd_bpp8_40x25 = {			/* with big virtual screen */
133 	{
134 		"40x25bpp8", 40, 25,
135 		&s3c24x0_lcd_emulops,
136 		8, 16,
137 		WSSCREEN_WSCOLORS,
138 	},
139 	8
140 }, lcd_bpp4_30x32 = {
141 	{
142 		"30x32bpp4", 30, 32,
143 		&s3c24x0_lcd_emulops,
144 		8, 10,
145 		0,			/* 4bpp mode is grayscale */
146 	},
147 	4
148 };
149 
150 
151 static const struct wsscreen_descr *lcd_scr_descr[] = {
152 #if 0
153 	/* bpp4 needs a patch to rasops4 */
154 	&lcd_bpp4_30x32.c,
155 #endif
156 	&lcd_bpp16_30x20.c,
157 	&lcd_bpp8_30x32.c,
158 	&lcd_bpp8_40x25.c,
159 	&lcd_bpp16_30x32.c,
160 };
161 
162 #define	N_SCR_DESCR	(sizeof lcd_scr_descr / sizeof lcd_scr_descr[0])
163 
164 const struct wsscreen_list lcd_screen_list = {
165 	N_SCR_DESCR,
166 	lcd_scr_descr
167 };
168 
169 const struct wsdisplay_accessops lcd_accessops = {
170 	s3c24x0_lcd_ioctl,
171 	s3c24x0_lcd_mmap,
172 	s3c24x0_lcd_alloc_screen,
173 	s3c24x0_lcd_free_screen,
174 	s3c24x0_lcd_show_screen,
175 	NULL, /* load_font */
176 };
177 
178 #else  /* NWSDISPLAY */
179 /*
180  * Interface to LCD framebuffer without wscons
181  */
182 extern struct cfdriver lcd_cd;
183 
184 dev_type_open(lcdopen);
185 dev_type_close(lcdclose);
186 dev_type_ioctl(lcdioctl);
187 dev_type_mmap(lcdmmap);
188 const struct cdevsw lcd_cdevsw = {
189 	.d_open = lcdopen,
190 	.d_close = lcdclose,
191 	.d_read = noread,
192 	.d_write = nowrite,
193 	.d_ioctl = lcdioctl,
194 	.d_stop = nostop,
195 	.d_tty = notty,
196 	.d_poll = nopoll,
197 	.d_mmap = lcdmmap,
198 	.d_kqfilter = nokqfilter,
199 	.d_discard = nodiscard,
200 	.d_flag = D_TTY
201 };
202 
203 #endif /* NWSDISPLAY */
204 
205 CFATTACH_DECL_NEW(lcd_ssio, sizeof (struct s3c24x0_lcd_softc),  lcd_match,
206     lcd_attach, NULL, NULL);
207 
208 int
lcd_match(device_t parent,cfdata_t cf,void * aux)209 lcd_match(device_t parent, cfdata_t cf, void *aux)
210 {
211 	struct s3c2xx0_attach_args *sa = aux;
212 
213 	/*if (sa->sa_addr == SSIOCF_ADDR_DEFAULT)*/
214         sa->sa_addr = S3C2410_LCDC_BASE;
215 
216 	return 1;
217 }
218 
219 static const struct s3c24x0_lcd_panel_info nec_NL2432HC22 =
220 {
221     240,			/* Width */
222     320,			/* Height */
223     7*1000*1000,		/* pixel clock = 7MHz Really is target VCLK? */
224 
225 #define	_(field, val)	(((val)-1) << (field##_SHIFT))
226 
227     LCDCON1_PNRMODE_TFT,
228 
229     /* LCDCON2: vertical timings */
230     _(LCDCON2_VBPD, 38) | /*4*/
231     _(LCDCON2_VFPD, 4) | /*4*/
232     _(LCDCON2_LINEVAL, 320) |
233     _(LCDCON2_VPSW, 6), /* 2 */
234 
235     /* LCDCON3: horizontal timings */
236     _(LCDCON3_HBPD, 30) | /*38*/
237     _(LCDCON3_HFPD, 2) | /*21*/
238     _(LCDCON3_HOZVAL, 240),
239 
240     /* LCDCON4: horizontal pulse width */
241     _(LCDCON4_HPSW, 6), /* 6 */
242 
243     /* LCDCON5: signal polarities */
244     LCDCON5_FRM565 | LCDCON5_INVVLINE | LCDCON5_INVVFRAME /*| LCDCON5_INVVDEN*/,
245 
246     /* LPCSEL register */
247     LPCSEL_LPC_EN | LPCSEL_RES_SEL | LPCSEL_MODE_SEL,
248 #undef _
249 };
250 
251 void
lcd_attach(device_t parent,device_t self,void * aux)252 lcd_attach(device_t parent, device_t self, void *aux)
253 {
254 	struct s3c24x0_lcd_softc *sc = device_private(self);
255 	bus_space_tag_t iot =  s3c2xx0_softc->sc_iot;
256 	bus_space_handle_t gpio_ioh = s3c2xx0_softc->sc_gpio_ioh;
257 #if NWSDISPLAY > 0
258 	struct wsemuldisplaydev_attach_args aa;
259 #else
260 	struct s3c24x0_lcd_screen *screen;
261 #endif
262 	uint32_t data;
263 
264 	sc->sc_dev = self;
265 	aprint_normal( "\n" );
266 
267 	/* setup GPIO ports for LCD */
268 	/* XXX: some LCD panels may not need all VD signals */
269 	gpio_ioh = s3c2xx0_softc->sc_gpio_ioh;
270 	bus_space_write_4(iot, gpio_ioh, GPIO_PCUP, ~0);
271 	bus_space_write_4(iot, gpio_ioh, GPIO_PCCON, 0xaaaaaaaa);
272 	bus_space_write_4(iot, gpio_ioh, GPIO_PDUP, ~0);
273 	bus_space_write_4(iot, gpio_ioh, GPIO_PDCON, 0xaaaaaaaa);
274 
275 	/* Setup GPIO G4 to b11 LCD_PWRDN  */
276 	data = bus_space_read_4(iot, gpio_ioh, GPIO_PGCON);
277 	data = GPIO_SET_FUNC(data, 4, 0x3);
278 	bus_space_write_4(iot, gpio_ioh, GPIO_PGCON, data);
279 
280 	/* Disable Pull-up for GPIO G4 */
281 	data = bus_space_read_4(iot, gpio_ioh, GPIO_PGUP);
282 	bus_space_write_4(iot, gpio_ioh, GPIO_PGUP, data | (1<<4));
283 
284 	s3c24x0_lcd_attach_sub(sc, aux, &nec_NL2432HC22);
285 
286 #if NWSDISPLAY > 0
287 
288 	aa.console = 0;
289 	aa.scrdata = &lcd_screen_list;
290 	aa.accessops = &lcd_accessops;
291 	aa.accesscookie = sc;
292 
293 
294 	(void) config_found(self, &aa, wsemuldisplaydevprint, CFARGS_NONE);
295 #else
296 
297 	screen = s3c24x0_lcd_new_screen(sc, 240, 320, 16);
298 
299 	if( screen ){
300 		sc->active = screen;
301 		s3c24x0_lcd_start_dma(sc, screen);
302 		s3c24x0_lcd_power(sc, 1);
303 	}
304 #endif /* NWSDISPLAY */
305 
306 }
307 
308 #if NWSDISPLAY == 0
309 
310 int
lcdopen(dev_t dev,int oflags,int devtype,struct lwp * p)311 lcdopen( dev_t dev, int oflags, int devtype, struct lwp *p )
312 {
313 	return 0;
314 }
315 
316 int
lcdclose(dev_t dev,int fflag,int devtype,struct lwp * p)317 lcdclose( dev_t dev, int fflag, int devtype, struct lwp *p )
318 {
319 	return 0;
320 }
321 
322 paddr_t
lcdmmap(dev_t dev,off_t offset,int size)323 lcdmmap( dev_t dev, off_t offset, int size )
324 {
325 	struct s3c24x0_lcd_softc *sc =
326 		device_lookup_private(&lcd_cd, minor(dev));
327 	struct s3c24x0_lcd_screen *scr = sc->active;
328 
329 	return bus_dmamem_mmap(sc->dma_tag, scr->segs, scr->nsegs,
330 	    offset, 0, BUS_DMA_WAITOK|BUS_DMA_COHERENT);
331 }
332 
333 int
lcdioctl(dev_t dev,u_long cmd,void * data,int fflag,struct lwp * l)334 lcdioctl( dev_t dev, u_long cmd, void *data,
335 	    int fflag, struct lwp *l )
336 {
337 	return EOPNOTSUPP;
338 }
339 
340 #endif /* NWSDISPLAY>0 */
341 
342 #ifdef LCD_DEBUG
343 static void
draw_test_pattern_16(struct s3c24x0_lcd_softc * sc,struct s3c24x0_lcd_screen * scr)344 draw_test_pattern_16(struct s3c24x0_lcd_softc *sc,
345     struct s3c24x0_lcd_screen *scr)
346 {
347 	int x, y;
348 	uint16_t color, *line;
349 	char *buf = (char *)(scr->buf_va);
350 
351 #define	rgb(r,g,b)	(((r)<<11) | ((g)<<5) | (b))
352 
353 	for (y=0; y < sc->panel_info->panel_height; ++y) {
354 		line = (uint16_t *)(buf + scr->stride * y);
355 
356 		for (x=0; x < sc->panel_info->panel_width; ++x) {
357 			switch (((x/30) + (y/10)) % 8) {
358 			default:
359 			case 0: color = rgb(0x00, 0x00, 0x00); break;
360 			case 1: color = rgb(0x00, 0x00, 0x1f); break;
361 			case 2: color = rgb(0x00, 0x3f, 0x00); break;
362 			case 3: color = rgb(0x00, 0x3f, 0x1f); break;
363 			case 4: color = rgb(0x1f, 0x00, 0x00); break;
364 			case 5: color = rgb(0x1f, 0x00, 0x1f); break;
365 			case 6: color = rgb(0x1f, 0x3f, 0x00); break;
366 			case 7: color = rgb(0x1f, 0x3f, 0x1f); break;
367 			}
368 
369 			line[x] = color;
370 		}
371 	}
372 
373 	for (x=0; x < MIN(sc->panel_info->panel_height,
374 		 sc->panel_info->panel_width); ++x) {
375 
376 		line = (uint16_t *)(buf + scr->stride * x);
377 		line[x] = rgb(0x1f, 0x3f, 0x1f);
378 	}
379 }
380 
381 static void
draw_test_pattern_8(struct s3c24x0_lcd_softc * sc,struct s3c24x0_lcd_screen * scr)382 draw_test_pattern_8(struct s3c24x0_lcd_softc *sc,
383     struct s3c24x0_lcd_screen *scr)
384 {
385 	int x, y;
386 	uint8_t *line;
387 	char *buf = (char *)(scr->buf_va);
388 
389 
390 	for (y=0; y < sc->panel_info->panel_height; ++y) {
391 		line = (uint8_t *)(buf + scr->stride * y);
392 
393 		for (x=0; x < sc->panel_info->panel_width; ++x) {
394 			line[x] = (((x/15) + (y/20)) % 16);
395 		}
396 	}
397 
398 	for (x=0; x < MIN(sc->panel_info->panel_height,
399 		 sc->panel_info->panel_width); ++x) {
400 
401 		line = (uint8_t *)(buf + scr->stride * x);
402 		line[x] = 7;
403 	}
404 }
405 
406 
407 void
draw_test_pattern(struct s3c24x0_lcd_softc * sc,struct s3c24x0_lcd_screen * scr)408 draw_test_pattern(struct s3c24x0_lcd_softc *sc, struct s3c24x0_lcd_screen *scr)
409 {
410 	switch (scr->depth) {
411 	case 16:
412 		draw_test_pattern_16(sc, scr);
413 		break;
414 	case 8:
415 		draw_test_pattern_8(sc, scr);
416 		break;
417 	}
418 }
419 
420 
421 #endif /* LCD_DEBUG */
422