Home | History | Annotate | Line # | Download | only in netwalker
netwalker_lcd.c revision 1.3
      1  1.3   hkenken /*	$NetBSD: netwalker_lcd.c,v 1.3 2014/05/06 11:22:53 hkenken Exp $	*/
      2  1.1       bsh 
      3  1.1       bsh /*-
      4  1.1       bsh  * Copyright (c) 2011, 2012 Genetec corp. All rights reserved.
      5  1.1       bsh  * Written by Hashimoto Kenichi for Genetec corp.
      6  1.1       bsh  *
      7  1.1       bsh  * Redistribution and use in source and binary forms, with or without
      8  1.1       bsh  * modification, are permitted provided that the following conditions
      9  1.1       bsh  * are met:
     10  1.1       bsh  * 1. Redistributions of source code must retain the above copyright
     11  1.1       bsh  *    notice, this list of conditions and the following disclaimer.
     12  1.1       bsh  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1       bsh  *    notice, this list of conditions and the following disclaimer in the
     14  1.1       bsh  *    documentation and/or other materials provided with the distribution.
     15  1.1       bsh  *
     16  1.1       bsh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1       bsh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1       bsh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1       bsh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1       bsh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1       bsh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1       bsh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1       bsh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1       bsh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1       bsh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1       bsh  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1       bsh  */
     28  1.3   hkenken 
     29  1.3   hkenken #include <sys/cdefs.h>
     30  1.3   hkenken __KERNEL_RCSID(0, "$NetBSD: netwalker_lcd.c,v 1.3 2014/05/06 11:22:53 hkenken Exp $");
     31  1.3   hkenken 
     32  1.3   hkenken #include "opt_imx51_ipuv3.h"
     33  1.3   hkenken #include "opt_netwalker_lcd.h"
     34  1.3   hkenken 
     35  1.3   hkenken #include "wsdisplay.h"
     36  1.3   hkenken #include "ioconf.h"
     37  1.3   hkenken #include "netwalker_backlight.h"
     38  1.3   hkenken 
     39  1.1       bsh #include <sys/param.h>
     40  1.1       bsh #include <sys/systm.h>
     41  1.1       bsh #include <sys/conf.h>
     42  1.1       bsh #include <sys/uio.h>
     43  1.1       bsh #include <sys/malloc.h>
     44  1.1       bsh #include <sys/device.h>
     45  1.3   hkenken #include <sys/pmf.h>
     46  1.1       bsh 
     47  1.1       bsh #include <dev/cons.h>
     48  1.1       bsh #include <dev/wscons/wsconsio.h>
     49  1.1       bsh #include <dev/wscons/wsdisplayvar.h>
     50  1.1       bsh #include <dev/wscons/wscons_callbacks.h>
     51  1.1       bsh 
     52  1.1       bsh #include <sys/bus.h>
     53  1.1       bsh #include <arm/imx/imx51var.h>
     54  1.1       bsh #include <arm/imx/imx51reg.h>
     55  1.1       bsh #include <arm/imx/imx51_ipuv3var.h>
     56  1.1       bsh #include <arm/imx/imx51_ipuv3reg.h>
     57  1.1       bsh #include <arm/imx/imxgpiovar.h>
     58  1.1       bsh 
     59  1.3   hkenken #include <evbarm/netwalker/netwalker_backlightvar.h>
     60  1.1       bsh 
     61  1.1       bsh int lcd_match(device_t, cfdata_t, void *);
     62  1.1       bsh void lcd_attach(device_t, device_t, void *);
     63  1.1       bsh 
     64  1.1       bsh void netwalker_cnattach(void);
     65  1.1       bsh 
     66  1.1       bsh #if NWSDISPLAY > 0
     67  1.3   hkenken static int netwalker_lcd_ioctl(void *, void *, u_long, void *, int,
     68  1.3   hkenken     struct lwp *);
     69  1.3   hkenken static int netwalker_lcd_show_screen(void *, void *, int,
     70  1.3   hkenken     void (*)(void *, int, int), void *);
     71  1.3   hkenken 
     72  1.3   hkenken bool netwalker_lcd_console = 0;
     73  1.3   hkenken 
     74  1.3   hkenken /*
     75  1.3   hkenken  * wsdisplay glue
     76  1.3   hkenken  */
     77  1.3   hkenken static struct imx51_wsscreen_descr netwalker_lcd_stdscreen = {
     78  1.3   hkenken 	.c = {
     79  1.3   hkenken 		.name	      = "std",
     80  1.3   hkenken 		.ncols	      = 0,
     81  1.3   hkenken 		.nrows	      = 0,
     82  1.3   hkenken 		.textops      = NULL,
     83  1.3   hkenken 		.fontwidth    = 8,
     84  1.3   hkenken 		.fontheight   = 16,
     85  1.3   hkenken 		.capabilities = WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
     86  1.3   hkenken 		.modecookie   = NULL
     87  1.3   hkenken 	},
     88  1.3   hkenken 	.depth = 16,		/* bits per pixel */
     89  1.3   hkenken 	.flags = RI_CENTER | RI_FULLCLEAR
     90  1.3   hkenken };
     91  1.3   hkenken 
     92  1.3   hkenken static const struct wsscreen_descr *netwalker_lcd_scr_descr[] = {
     93  1.3   hkenken 	&netwalker_lcd_stdscreen.c,
     94  1.3   hkenken };
     95  1.3   hkenken 
     96  1.3   hkenken const struct wsscreen_list netwalker_lcd_screen_list = {
     97  1.3   hkenken 	sizeof netwalker_lcd_scr_descr / sizeof netwalker_lcd_scr_descr[0],
     98  1.3   hkenken 	netwalker_lcd_scr_descr
     99  1.3   hkenken };
    100  1.3   hkenken 
    101  1.3   hkenken struct wsdisplay_accessops netwalker_lcd_accessops = {
    102  1.3   hkenken 	.ioctl	      = netwalker_lcd_ioctl,
    103  1.3   hkenken 	.mmap	      = imx51_ipuv3_mmap,
    104  1.3   hkenken 	.alloc_screen = imx51_ipuv3_alloc_screen,
    105  1.3   hkenken 	.free_screen  = imx51_ipuv3_free_screen,
    106  1.3   hkenken 	.show_screen  = netwalker_lcd_show_screen,
    107  1.3   hkenken 	.load_font    = NULL,
    108  1.3   hkenken 	.pollc	      = NULL,
    109  1.3   hkenken 	.scroll	      = NULL
    110  1.3   hkenken };
    111  1.1       bsh #else
    112  1.1       bsh #ifdef LCD_DEBUG
    113  1.1       bsh static void draw_test_pattern(struct imx51_ipuv3_softc *,
    114  1.1       bsh     struct imx51_ipuv3_screen *);
    115  1.1       bsh #endif
    116  1.1       bsh 
    117  1.1       bsh /*
    118  1.1       bsh  * Interface to LCD framebuffer without wscons
    119  1.1       bsh  */
    120  1.1       bsh extern struct cfdriver ipu_cd;
    121  1.1       bsh 
    122  1.1       bsh dev_type_open(lcdopen);
    123  1.1       bsh dev_type_close(lcdclose);
    124  1.1       bsh dev_type_ioctl(lcdioctl);
    125  1.1       bsh dev_type_mmap(lcdmmap);
    126  1.1       bsh const struct cdevsw ipu_cdevsw = {
    127  1.2  dholland 	.d_open = lcdopen,
    128  1.2  dholland 	.d_close = lcdclose,
    129  1.2  dholland 	.d_read = noread,
    130  1.2  dholland 	.d_write = nowrite,
    131  1.2  dholland 	.d_ioctl = lcdioctl,
    132  1.2  dholland 	.d_stop = nostop,
    133  1.2  dholland 	.d_tty = notty,
    134  1.2  dholland 	.d_poll = nopoll,
    135  1.2  dholland 	.d_mmap = lcdmmap,
    136  1.2  dholland 	.d_kqfilter = nokqfilter,
    137  1.2  dholland 	.d_flag = D_TTY
    138  1.1       bsh };
    139  1.1       bsh 
    140  1.1       bsh #endif
    141  1.1       bsh 
    142  1.1       bsh CFATTACH_DECL_NEW(lcd_netwalker, sizeof (struct imx51_ipuv3_softc),
    143  1.1       bsh     lcd_match, lcd_attach, NULL, NULL);
    144  1.1       bsh 
    145  1.1       bsh int
    146  1.1       bsh lcd_match( device_t parent, cfdata_t cf, void *aux )
    147  1.1       bsh {
    148  1.1       bsh 	return 1;
    149  1.1       bsh }
    150  1.1       bsh 
    151  1.1       bsh /* Sharp's LCD */
    152  1.1       bsh static const struct lcd_panel_geometry sharp_panel =
    153  1.1       bsh {
    154  1.1       bsh 	.panel_width = 1024,	/* Width */
    155  1.1       bsh 	.panel_height = 600,	/* Height */
    156  1.1       bsh 
    157  1.1       bsh 	.pixel_clk = 30076000,
    158  1.1       bsh 
    159  1.1       bsh 	.hsync_width = 8,
    160  1.1       bsh 	.left  = 20,
    161  1.1       bsh 	.right = 20,
    162  1.1       bsh 
    163  1.1       bsh 	.vsync_width = 4,
    164  1.1       bsh 	.upper = 2,
    165  1.1       bsh 	.lower = 2,
    166  1.1       bsh 
    167  1.1       bsh 	.panel_info = 0,
    168  1.1       bsh };
    169  1.1       bsh 
    170  1.1       bsh void lcd_attach( device_t parent, device_t self, void *aux )
    171  1.1       bsh {
    172  1.1       bsh 	struct imx51_ipuv3_softc *sc = device_private(self);
    173  1.1       bsh 	struct axi_attach_args *axia = aux;
    174  1.1       bsh 	bus_space_tag_t iot = axia->aa_iot;
    175  1.1       bsh 
    176  1.1       bsh 	sc->dev = self;
    177  1.1       bsh 
    178  1.3   hkenken #if defined(IMXIPUCONSOLE)
    179  1.3   hkenken 	netwalker_lcd_console = 1;
    180  1.3   hkenken #endif
    181  1.3   hkenken #if (NWSDISPLAY > 0)
    182  1.1       bsh 	netwalker_cnattach();
    183  1.1       bsh #endif
    184  1.1       bsh 
    185  1.1       bsh 	/* XXX move this to imx51_ipuv3.c */
    186  1.1       bsh 	{
    187  1.1       bsh 		bus_space_handle_t mipi_ioh;
    188  1.1       bsh 		uint32_t reg;
    189  1.1       bsh 
    190  1.1       bsh 		if (bus_space_map(iot, 0x83fdc000, 0x1000, 0, &mipi_ioh))
    191  1.1       bsh 			aprint_error_dev(self, "can't map MIPI HSC");
    192  1.1       bsh 		else {
    193  1.1       bsh 			bus_space_write_4(iot, mipi_ioh, 0x000, 0xf00);
    194  1.1       bsh 
    195  1.1       bsh 			reg = bus_space_read_4(iot, mipi_ioh, 0x800);
    196  1.1       bsh 			bus_space_write_4(iot, mipi_ioh, 0x800, reg | 0x0ff);
    197  1.1       bsh 
    198  1.1       bsh 			reg = bus_space_read_4(iot, mipi_ioh, 0x800);
    199  1.1       bsh 			bus_space_write_4(iot, mipi_ioh, 0x800, reg | 0x10000);
    200  1.1       bsh 		}
    201  1.1       bsh 	}
    202  1.1       bsh 
    203  1.1       bsh 	/* LCD power on */
    204  1.1       bsh 	gpio_set_direction(GPIO_NO(4, 9), GPIO_DIR_OUT);
    205  1.1       bsh 	gpio_set_direction(GPIO_NO(4, 10), GPIO_DIR_OUT);
    206  1.1       bsh 	gpio_set_direction(GPIO_NO(3, 3), GPIO_DIR_OUT);
    207  1.1       bsh 
    208  1.1       bsh 	gpio_data_write(GPIO_NO(3, 3), 1);
    209  1.1       bsh 	gpio_data_write(GPIO_NO(4, 9), 1);
    210  1.1       bsh 	delay(180 * 1000);
    211  1.1       bsh 	gpio_data_write(GPIO_NO(4, 10), 1);
    212  1.1       bsh 
    213  1.1       bsh 	gpio_set_direction(GPIO_NO(2, 13), GPIO_DIR_OUT);
    214  1.1       bsh 	gpio_data_write(GPIO_NO(2, 13), 1);
    215  1.1       bsh 
    216  1.3   hkenken 	imx51_ipuv3_attach_sub(sc, aux, &sharp_panel);
    217  1.1       bsh 
    218  1.1       bsh #if NWSDISPLAY == 0
    219  1.3   hkenken 	struct imx51_ipuv3_screen *screen;
    220  1.3   hkenken 	int error;
    221  1.1       bsh 
    222  1.3   hkenken 	error = imx51_ipuv3_new_screen(sc, 16, &screen);
    223  1.1       bsh #ifdef LCD_DEBUG
    224  1.3   hkenken 	draw_test_pattern(sc, screen);
    225  1.1       bsh #endif
    226  1.3   hkenken 	if (error == 0) {
    227  1.3   hkenken 		sc->active = screen;
    228  1.3   hkenken 		imx51_ipuv3_start_dma(sc, screen);
    229  1.1       bsh 	}
    230  1.3   hkenken #else
    231  1.3   hkenken 	struct wsemuldisplaydev_attach_args aa;
    232  1.3   hkenken 
    233  1.3   hkenken #if defined(IMXIPUCONSOLE)
    234  1.3   hkenken 	aa.console = true;
    235  1.3   hkenken #else
    236  1.3   hkenken 	aa.console = false;
    237  1.3   hkenken #endif
    238  1.3   hkenken 	aa.scrdata = &netwalker_lcd_screen_list;
    239  1.3   hkenken 	aa.accessops = &netwalker_lcd_accessops;
    240  1.3   hkenken 	aa.accesscookie = &sc->vd;
    241  1.3   hkenken 
    242  1.3   hkenken 	(void) config_found(sc->dev, &aa, wsemuldisplaydevprint);
    243  1.1       bsh #endif
    244  1.1       bsh }
    245  1.1       bsh 
    246  1.1       bsh #if NWSDISPLAY > 0
    247  1.1       bsh void
    248  1.1       bsh netwalker_cnattach(void)
    249  1.1       bsh {
    250  1.3   hkenken 	imx51_ipuv3_cnattach(netwalker_lcd_console, &netwalker_lcd_stdscreen,
    251  1.3   hkenken 	    &netwalker_lcd_accessops, &sharp_panel);
    252  1.1       bsh 	return;
    253  1.1       bsh }
    254  1.3   hkenken 
    255  1.3   hkenken /*
    256  1.3   hkenken  * wsdisplay accessops overrides
    257  1.3   hkenken  */
    258  1.3   hkenken static int
    259  1.3   hkenken netwalker_lcd_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
    260  1.3   hkenken {
    261  1.3   hkenken 	int res = EINVAL;
    262  1.3   hkenken 
    263  1.3   hkenken 	switch (cmd) {
    264  1.3   hkenken #if NNETWALKER_BACKLIGHT > 0
    265  1.3   hkenken 	case WSDISPLAYIO_GETPARAM:
    266  1.3   hkenken 	case WSDISPLAYIO_SETPARAM:
    267  1.3   hkenken 		res = netwalker_lcd_param_ioctl(cmd, (struct wsdisplay_param *)data);
    268  1.3   hkenken 		break;
    269  1.3   hkenken #endif
    270  1.3   hkenken 	}
    271  1.3   hkenken 
    272  1.3   hkenken 	if (res == EINVAL)
    273  1.3   hkenken 		res = imx51_ipuv3_ioctl(v, vs, cmd, data, flag, l);
    274  1.3   hkenken 	return res;
    275  1.3   hkenken }
    276  1.3   hkenken 
    277  1.3   hkenken static int
    278  1.3   hkenken netwalker_lcd_show_screen(void *v, void *cookie, int waitok,
    279  1.3   hkenken     void (*cb_func)(void *, int, int), void *cb_arg)
    280  1.3   hkenken {
    281  1.3   hkenken 	int error;
    282  1.3   hkenken 
    283  1.3   hkenken 	error = imx51_ipuv3_show_screen(v, cookie, waitok, cb_func, cb_arg);
    284  1.3   hkenken 	if (error)
    285  1.3   hkenken 		return (error);
    286  1.3   hkenken 
    287  1.3   hkenken 	return 0;
    288  1.3   hkenken }
    289  1.1       bsh #else
    290  1.1       bsh 
    291  1.1       bsh int
    292  1.1       bsh lcdopen(dev_t dev, int oflags, int devtype, struct lwp *l)
    293  1.1       bsh {
    294  1.1       bsh 	return 0;
    295  1.1       bsh }
    296  1.1       bsh 
    297  1.1       bsh int
    298  1.1       bsh lcdclose(dev_t dev, int fflag, int devtype, struct lwp *l)
    299  1.1       bsh {
    300  1.1       bsh 	return 0;
    301  1.1       bsh }
    302  1.1       bsh 
    303  1.1       bsh paddr_t
    304  1.1       bsh lcdmmap(dev_t dev, off_t offset, int size)
    305  1.1       bsh {
    306  1.1       bsh 	struct imx51_ipuv3_softc *sc =
    307  1.1       bsh 	    device_lookup_private(&ipu_cd, minor(dev));
    308  1.1       bsh 	struct imx51_ipuv3_screen *scr = sc->active;
    309  1.1       bsh 
    310  1.1       bsh 	return bus_dmamem_mmap(sc->dma_tag, scr->segs, scr->nsegs,
    311  1.1       bsh 	    offset, 0, BUS_DMA_WAITOK|BUS_DMA_COHERENT);
    312  1.1       bsh }
    313  1.1       bsh 
    314  1.1       bsh int
    315  1.1       bsh lcdioctl(dev_t dev, u_long cmd, void *data,
    316  1.1       bsh     int fflag, struct lwp *l)
    317  1.1       bsh {
    318  1.1       bsh 	return EOPNOTSUPP;
    319  1.1       bsh }
    320  1.1       bsh 
    321  1.1       bsh #ifdef LCD_DEBUG
    322  1.1       bsh static void
    323  1.1       bsh draw_test_pattern(struct imx51_ipuv3_softc *sc,
    324  1.1       bsh     struct imx51_ipuv3_screen *scr)
    325  1.1       bsh {
    326  1.1       bsh 	int x, y;
    327  1.1       bsh 	uint16_t color, *line;
    328  1.1       bsh 	char *buf = (char *)(scr->buf_va);
    329  1.1       bsh 
    330  1.1       bsh 	printf("%s: buf_va %p, size 0x%x\n", __func__, buf,
    331  1.1       bsh 	       (uint)scr->buf_size);
    332  1.1       bsh 	printf("%s: panel %d x %d\n", __func__,
    333  1.1       bsh 	    sc->geometry->panel_width,
    334  1.1       bsh 	    sc->geometry->panel_height);
    335  1.1       bsh #define	rgb(r,g,b)	(((r)<<11) | ((g)<<5) | (b))
    336  1.1       bsh 
    337  1.1       bsh 	for (y=0; y < sc->geometry->panel_height; ++y) {
    338  1.1       bsh 		line = (uint16_t *)(buf + scr->stride * y);
    339  1.1       bsh 
    340  1.1       bsh 		for (x=0; x < sc->geometry->panel_width; ++x) {
    341  1.1       bsh 			switch (((x/30) + (y/10)) % 8) {
    342  1.1       bsh 			default:
    343  1.1       bsh 			case 0: color = rgb(0x00, 0x00, 0x00); break;
    344  1.1       bsh 			case 1: color = rgb(0x00, 0x00, 0x1f); break;
    345  1.1       bsh 			case 2: color = rgb(0x00, 0x3f, 0x00); break;
    346  1.1       bsh 			case 3: color = rgb(0x00, 0x3f, 0x1f); break;
    347  1.1       bsh 			case 4: color = rgb(0x1f, 0x00, 0x00); break;
    348  1.1       bsh 			case 5: color = rgb(0x1f, 0x00, 0x1f); break;
    349  1.1       bsh 			case 6: color = rgb(0x1f, 0x3f, 0x00); break;
    350  1.1       bsh 			case 7: color = rgb(0x1f, 0x3f, 0x1f); break;
    351  1.1       bsh 			}
    352  1.1       bsh 
    353  1.1       bsh 			line[x] = color;
    354  1.1       bsh 		}
    355  1.1       bsh 	}
    356  1.1       bsh 
    357  1.1       bsh 	for (x=0; x < MIN(sc->geometry->panel_height,
    358  1.1       bsh 		sc->geometry->panel_width); ++x) {
    359  1.1       bsh 		line = (uint16_t *)(buf + scr->stride * x);
    360  1.1       bsh 		line[x] = rgb(0x1f, 0x3f, 0x1f);
    361  1.1       bsh 	}
    362  1.1       bsh }
    363  1.1       bsh #endif
    364  1.1       bsh 
    365  1.1       bsh #endif /* NWSDISPLAY > 0 */
    366  1.1       bsh 
    367  1.1       bsh 
    368