mini2440_lcd.c revision 1.1 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 (at) 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.1 2012/01/30 03:28:34 nisimura 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(struct device *, struct cfdata *, void *);
94 void lcd_attach(struct device *, struct device *, 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 lcdopen, lcdclose, noread, nowrite, lcdioctl,
191 nostop, notty, nopoll, lcdmmap, nokqfilter, D_TTY
192 };
193
194 #endif /* NWSDISPLAY */
195
196 CFATTACH_DECL_NEW(lcd_ssio, sizeof (struct s3c24x0_lcd_softc), lcd_match,
197 lcd_attach, NULL, NULL);
198
199 int
200 lcd_match(struct device *parent, struct cfdata *cf, void *aux)
201 {
202 struct s3c2xx0_attach_args *sa = aux;
203
204 /*if (sa->sa_addr == SSIOCF_ADDR_DEFAULT)*/
205 sa->sa_addr = S3C2410_LCDC_BASE;
206
207 return 1;
208 }
209
210 static const struct s3c24x0_lcd_panel_info nec_NL2432HC22 =
211 {
212 240, /* Width */
213 320, /* Height */
214 7*1000*1000, /* pixel clock = 7MHz Really is target VCLK? */
215
216 #define _(field, val) (((val)-1) << (field##_SHIFT))
217
218 LCDCON1_PNRMODE_TFT,
219
220 /* LCDCON2: vertical timings */
221 _(LCDCON2_VBPD, 38) | /*4*/
222 _(LCDCON2_VFPD, 4) | /*4*/
223 _(LCDCON2_LINEVAL, 320) |
224 _(LCDCON2_VPSW, 6), /* 2 */
225
226 /* LCDCON3: horizontal timings */
227 _(LCDCON3_HBPD, 30) | /*38*/
228 _(LCDCON3_HFPD, 2) | /*21*/
229 _(LCDCON3_HOZVAL, 240),
230
231 /* LCDCON4: horizontaol pulse width */
232 _(LCDCON4_HPSW, 6), /* 6 */
233
234 /* LCDCON5: signal polarities */
235 LCDCON5_FRM565 | LCDCON5_INVVLINE | LCDCON5_INVVFRAME /*| LCDCON5_INVVDEN*/,
236
237 /* LPCSEL register */
238 LPCSEL_LPC_EN | LPCSEL_RES_SEL | LPCSEL_MODE_SEL,
239 #undef _
240 };
241
242 void
243 lcd_attach(struct device *parent, struct device *self, void *aux)
244 {
245 struct s3c24x0_lcd_softc *sc = device_private(self);
246 bus_space_tag_t iot = s3c2xx0_softc->sc_iot;
247 bus_space_handle_t gpio_ioh = s3c2xx0_softc->sc_gpio_ioh;
248 #if NWSDISPLAY > 0
249 struct wsemuldisplaydev_attach_args aa;
250 #else
251 struct s3c24x0_lcd_screen *screen;
252 #endif
253 uint32_t data;
254
255 sc->sc_dev = self;
256 aprint_normal( "\n" );
257
258 /* setup GPIO ports for LCD */
259 /* XXX: some LCD panels may not need all VD signals */
260 gpio_ioh = s3c2xx0_softc->sc_gpio_ioh;
261 bus_space_write_4(iot, gpio_ioh, GPIO_PCUP, ~0);
262 bus_space_write_4(iot, gpio_ioh, GPIO_PCCON, 0xaaaaaaaa);
263 bus_space_write_4(iot, gpio_ioh, GPIO_PDUP, ~0);
264 bus_space_write_4(iot, gpio_ioh, GPIO_PDCON, 0xaaaaaaaa);
265
266 /* Setup GPIO G4 to b11 LCD_PWRDN */
267 data = bus_space_read_4(iot, gpio_ioh, GPIO_PGCON);
268 data = GPIO_SET_FUNC(data, 4, 0x3);
269 bus_space_write_4(iot, gpio_ioh, GPIO_PGCON, data);
270
271 /* Disable Pull-up for GPIO G4 */
272 data = bus_space_read_4(iot, gpio_ioh, GPIO_PGUP);
273 bus_space_write_4(iot, gpio_ioh, GPIO_PGUP, data | (1<<4));
274
275 s3c24x0_lcd_attach_sub(sc, aux, &nec_NL2432HC22);
276
277 #if NWSDISPLAY > 0
278
279 aa.console = 0;
280 aa.scrdata = &lcd_screen_list;
281 aa.accessops = &lcd_accessops;
282 aa.accesscookie = sc;
283
284
285 (void) config_found(self, &aa, wsemuldisplaydevprint);
286 #else
287
288 screen = s3c24x0_lcd_new_screen(sc, 240, 320, 16);
289
290 if( screen ){
291 sc->active = screen;
292 s3c24x0_lcd_start_dma(sc, screen);
293 s3c24x0_lcd_power(sc, 1);
294 }
295 #endif /* NWSDISPLAY */
296
297 }
298
299 #if NWSDISPLAY == 0
300
301 int
302 lcdopen( dev_t dev, int oflags, int devtype, struct lwp *p )
303 {
304 return 0;
305 }
306
307 int
308 lcdclose( dev_t dev, int fflag, int devtype, struct lwp *p )
309 {
310 return 0;
311 }
312
313 paddr_t
314 lcdmmap( dev_t dev, off_t offset, int size )
315 {
316 struct s3c24x0_lcd_softc *sc =
317 device_lookup_private(&lcd_cd, minor(dev));
318 struct s3c24x0_lcd_screen *scr = sc->active;
319
320 return bus_dmamem_mmap(sc->dma_tag, scr->segs, scr->nsegs,
321 offset, 0, BUS_DMA_WAITOK|BUS_DMA_COHERENT);
322 }
323
324 int
325 lcdioctl( dev_t dev, u_long cmd, void *data,
326 int fflag, struct lwp *l )
327 {
328 return EOPNOTSUPP;
329 }
330
331 #endif /* NWSDISPLAY>0 */
332
333 #ifdef LCD_DEBUG
334 static void
335 draw_test_pattern_16(struct s3c24x0_lcd_softc *sc,
336 struct s3c24x0_lcd_screen *scr)
337 {
338 int x, y;
339 uint16_t color, *line;
340 char *buf = (char *)(scr->buf_va);
341
342 #define rgb(r,g,b) (((r)<<11) | ((g)<<5) | (b))
343
344 for (y=0; y < sc->panel_info->panel_height; ++y) {
345 line = (uint16_t *)(buf + scr->stride * y);
346
347 for (x=0; x < sc->panel_info->panel_width; ++x) {
348 switch (((x/30) + (y/10)) % 8) {
349 default:
350 case 0: color = rgb(0x00, 0x00, 0x00); break;
351 case 1: color = rgb(0x00, 0x00, 0x1f); break;
352 case 2: color = rgb(0x00, 0x3f, 0x00); break;
353 case 3: color = rgb(0x00, 0x3f, 0x1f); break;
354 case 4: color = rgb(0x1f, 0x00, 0x00); break;
355 case 5: color = rgb(0x1f, 0x00, 0x1f); break;
356 case 6: color = rgb(0x1f, 0x3f, 0x00); break;
357 case 7: color = rgb(0x1f, 0x3f, 0x1f); break;
358 }
359
360 line[x] = color;
361 }
362 }
363
364 for (x=0; x < MIN(sc->panel_info->panel_height,
365 sc->panel_info->panel_width); ++x) {
366
367 line = (uint16_t *)(buf + scr->stride * x);
368 line[x] = rgb(0x1f, 0x3f, 0x1f);
369 }
370 }
371
372 static void
373 draw_test_pattern_8(struct s3c24x0_lcd_softc *sc,
374 struct s3c24x0_lcd_screen *scr)
375 {
376 int x, y;
377 uint8_t *line;
378 char *buf = (char *)(scr->buf_va);
379
380
381 for (y=0; y < sc->panel_info->panel_height; ++y) {
382 line = (uint8_t *)(buf + scr->stride * y);
383
384 for (x=0; x < sc->panel_info->panel_width; ++x) {
385 line[x] = (((x/15) + (y/20)) % 16);
386 }
387 }
388
389 for (x=0; x < MIN(sc->panel_info->panel_height,
390 sc->panel_info->panel_width); ++x) {
391
392 line = (uint8_t *)(buf + scr->stride * x);
393 line[x] = 7;
394 }
395 }
396
397
398 void
399 draw_test_pattern(struct s3c24x0_lcd_softc *sc, struct s3c24x0_lcd_screen *scr)
400 {
401 switch (scr->depth) {
402 case 16:
403 draw_test_pattern_16(sc, scr);
404 break;
405 case 8:
406 draw_test_pattern_8(sc, scr);
407 break;
408 }
409 }
410
411
412 #endif /* LCD_DEBUG */
413