xref: /netbsd-src/sys/arch/evbarm/smdk2xx0/smdk2410_lcd.c (revision 2ddc9e7fa6d85f44007bbaf0a4d7202a54f42b23)
1 /*	$NetBSD: smdk2410_lcd.c,v 1.14 2024/03/06 20:18:42 andvar 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.14 2024/03/06 20:18:42 andvar 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 
51 #include <dev/cons.h>
52 #include <dev/wscons/wsconsio.h>
53 #include <dev/wscons/wsdisplayvar.h>
54 #include <dev/wscons/wscons_callbacks.h>
55 
56 #include <sys/bus.h>
57 #include <arm/s3c2xx0/s3c24x0var.h>
58 #include <arm/s3c2xx0/s3c24x0reg.h>
59 #include <arm/s3c2xx0/s3c2410reg.h>
60 #include <arm/s3c2xx0/s3c24x0_lcd.h>
61 
62 #include "locators.h"
63 #include "wsdisplay.h"
64 
65 int	lcd_match(device_t, cfdata_t, void *);
66 void	lcd_attach(device_t, device_t, 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 	.d_open = lcdopen,
163 	.d_close = lcdclose,
164 	.d_read = noread,
165 	.d_write = nowrite,
166 	.d_ioctl = lcdioctl,
167 	.d_stop = nostop,
168 	.d_tty = notty,
169 	.d_poll = nopoll,
170 	.d_mmap = lcdmmap,
171 	.d_kqfilter = nokqfilter,
172 	.d_discard = nodiscard,
173 	.d_flag = D_TTY
174 };
175 
176 #endif /* NWSDISPLAY */
177 
178 CFATTACH_DECL_NEW(lcd_ssio, sizeof (struct s3c24x0_lcd_softc),  lcd_match,
179     lcd_attach, NULL, NULL);
180 
181 int
lcd_match(device_t parent,cfdata_t cf,void * aux)182 lcd_match(device_t parent, cfdata_t cf, void *aux)
183 {
184 	struct s3c2xx0_attach_args *sa = aux;
185 
186 	if (sa->sa_addr == SSIOCF_ADDR_DEFAULT)
187 		sa->sa_addr = S3C2410_LCDC_BASE;
188 
189 	return 1;
190 }
191 
192 static const struct s3c24x0_lcd_panel_info samsung_LTS350Q1 =
193 {
194     240,			/* Width */
195     320,			/* Height */
196     10*1000*1000,		/* pixel clock = 10MHz */
197 
198 #define	_(field, val)	(((val)-1) << (field##_SHIFT))
199 
200     LCDCON1_PNRMODE_TFT,
201 
202     /* LCDCON2: vertical timings */
203     _(LCDCON2_VBPD, 2) |
204     _(LCDCON2_VFPD, 3) |
205     _(LCDCON2_LINEVAL, 320) |
206     _(LCDCON2_VPSW, 2),
207 
208     /* LCDCON3: horizontal timings */
209     _(LCDCON3_HBPD, 7) |
210     _(LCDCON3_HFPD, 3),
211 
212     /* LCDCON4: horizontal pulse width */
213     _(LCDCON4_HPSW, 4),
214 
215     /* LCDCON5: signal polarities */
216     LCDCON5_FRM565 | LCDCON5_INVVLINE | LCDCON5_INVVFRAME,
217 
218     /* LPCSEL register */
219     LPCSEL_LPC_EN | LPCSEL_RES_SEL | LPCSEL_MODE_SEL,
220 #undef _
221 };
222 
223 void
lcd_attach(device_t parent,device_t self,void * aux)224 lcd_attach(device_t parent, device_t self, void *aux)
225 {
226 	struct s3c24x0_lcd_softc *sc = device_private(self);
227 	bus_space_tag_t iot =  s3c2xx0_softc->sc_iot;
228 	bus_space_handle_t gpio_ioh = s3c2xx0_softc->sc_gpio_ioh;
229 #if NWSDISPLAY > 0
230 	struct wsemuldisplaydev_attach_args aa;
231 #else
232 	struct s3c24x0_lcd_screen *screen;
233 #endif
234 
235 
236 	sc->sc_dev = self;
237 	aprint_normal( "\n" );
238 
239 	/* setup GPIO ports for LCD */
240 	/* XXX: some LCD panels may not need all VD signals */
241 	gpio_ioh = s3c2xx0_softc->sc_gpio_ioh;
242 	bus_space_write_4(iot, gpio_ioh, GPIO_PCUP, ~0);
243 	bus_space_write_4(iot, gpio_ioh, GPIO_PCCON, 0xaaaaaaaa);
244 	bus_space_write_4(iot, gpio_ioh, GPIO_PDUP, ~0);
245 	bus_space_write_4(iot, gpio_ioh, GPIO_PDCON, 0xaaaaaaaa);
246 
247 	s3c24x0_lcd_attach_sub(sc, aux, &samsung_LTS350Q1);
248 
249 #if NWSDISPLAY > 0
250 
251 	aa.console = 0;
252 	aa.scrdata = &lcd_screen_list;
253 	aa.accessops = &lcd_accessops;
254 	aa.accesscookie = sc;
255 
256 
257 	(void) config_found(self, &aa, wsemuldisplaydevprint, CFARGS_NONE);
258 #else
259 
260 	screen = s3c24x0_lcd_new_screen(sc, 240, 320, 16);
261 
262 	if( screen ){
263 		sc->active = screen;
264 		s3c24x0_lcd_start_dma(sc, screen);
265 		s3c24x0_lcd_power(sc, 1);
266 	}
267 #endif /* NWSDISPLAY */
268 
269 }
270 
271 #if NWSDISPLAY == 0
272 
273 int
lcdopen(dev_t dev,int oflags,int devtype,struct proc * p)274 lcdopen( dev_t dev, int oflags, int devtype, struct proc *p )
275 {
276 	return 0;
277 }
278 
279 int
lcdclose(dev_t dev,int fflag,int devtype,struct proc * p)280 lcdclose( dev_t dev, int fflag, int devtype, struct proc *p )
281 {
282 	return 0;
283 }
284 
285 paddr_t
lcdmmap(dev_t dev,off_t offset,int size)286 lcdmmap( dev_t dev, off_t offset, int size )
287 {
288 	struct s3c24x0_lcd_softc *sc =
289 		device_lookup_private(&lcd_cd, minor(dev));
290 	struct s3c24x0_lcd_screen *scr = sc->active;
291 
292 	return bus_dmamem_mmap(sc->dma_tag, scr->segs, scr->nsegs,
293 	    offset, 0, BUS_DMA_WAITOK|BUS_DMA_COHERENT);
294 }
295 
296 int
lcdioctl(dev_t dev,u_long cmd,void * data,int fflag,struct lwp * l)297 lcdioctl( dev_t dev, u_long cmd, void *data,
298 	    int fflag, struct lwp *l )
299 {
300 	return EOPNOTSUPP;
301 }
302 
303 #endif /* NWSDISPLAY>0 */
304 
305 #ifdef LCD_DEBUG
306 static void
draw_test_pattern_16(struct s3c24x0_lcd_softc * sc,struct s3c24x0_lcd_screen * scr)307 draw_test_pattern_16(struct s3c24x0_lcd_softc *sc,
308     struct s3c24x0_lcd_screen *scr)
309 {
310 	int x, y;
311 	uint16_t color, *line;
312 	char *buf = (char *)(scr->buf_va);
313 
314 #define	rgb(r,g,b)	(((r)<<11) | ((g)<<5) | (b))
315 
316 	for (y=0; y < sc->panel_info->panel_height; ++y) {
317 		line = (uint16_t *)(buf + scr->stride * y);
318 
319 		for (x=0; x < sc->panel_info->panel_width; ++x) {
320 			switch (((x/30) + (y/10)) % 8) {
321 			default:
322 			case 0: color = rgb(0x00, 0x00, 0x00); break;
323 			case 1: color = rgb(0x00, 0x00, 0x1f); break;
324 			case 2: color = rgb(0x00, 0x3f, 0x00); break;
325 			case 3: color = rgb(0x00, 0x3f, 0x1f); break;
326 			case 4: color = rgb(0x1f, 0x00, 0x00); break;
327 			case 5: color = rgb(0x1f, 0x00, 0x1f); break;
328 			case 6: color = rgb(0x1f, 0x3f, 0x00); break;
329 			case 7: color = rgb(0x1f, 0x3f, 0x1f); break;
330 			}
331 
332 			line[x] = color;
333 		}
334 	}
335 
336 	for (x=0; x < MIN(sc->panel_info->panel_height,
337 		 sc->panel_info->panel_width); ++x) {
338 
339 		line = (uint16_t *)(buf + scr->stride * x);
340 		line[x] = rgb(0x1f, 0x3f, 0x1f);
341 	}
342 }
343 
344 static void
draw_test_pattern_8(struct s3c24x0_lcd_softc * sc,struct s3c24x0_lcd_screen * scr)345 draw_test_pattern_8(struct s3c24x0_lcd_softc *sc,
346     struct s3c24x0_lcd_screen *scr)
347 {
348 	int x, y;
349 	uint8_t *line;
350 	char *buf = (char *)(scr->buf_va);
351 
352 
353 	for (y=0; y < sc->panel_info->panel_height; ++y) {
354 		line = (uint8_t *)(buf + scr->stride * y);
355 
356 		for (x=0; x < sc->panel_info->panel_width; ++x) {
357 			line[x] = (((x/15) + (y/20)) % 16);
358 		}
359 	}
360 
361 	for (x=0; x < MIN(sc->panel_info->panel_height,
362 		 sc->panel_info->panel_width); ++x) {
363 
364 		line = (uint8_t *)(buf + scr->stride * x);
365 		line[x] = 7;
366 	}
367 }
368 
369 
370 void
draw_test_pattern(struct s3c24x0_lcd_softc * sc,struct s3c24x0_lcd_screen * scr)371 draw_test_pattern(struct s3c24x0_lcd_softc *sc, struct s3c24x0_lcd_screen *scr)
372 {
373 	switch (scr->depth) {
374 	case 16:
375 		draw_test_pattern_16(sc, scr);
376 		break;
377 	case 8:
378 		draw_test_pattern_8(sc, scr);
379 		break;
380 	}
381 }
382 
383 
384 #endif /* LCD_DEBUG */
385 
386