Home | History | Annotate | Line # | Download | only in mini2440
mini2440_lcd.c revision 1.3
      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.3 2014/03/16 05:20:24 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_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
    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: horizontaol 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
    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);
    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
    311 lcdopen( dev_t dev, int oflags, int devtype, struct lwp *p )
    312 {
    313 	return 0;
    314 }
    315 
    316 int
    317 lcdclose( dev_t dev, int fflag, int devtype, struct lwp *p )
    318 {
    319 	return 0;
    320 }
    321 
    322 paddr_t
    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
    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
    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
    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
    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