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