Home | History | Annotate | Line # | Download | only in netwalker
netwalker_lcd.c revision 1.1
      1  1.1  bsh /*	$NetBSD: netwalker_lcd.c,v 1.1 2012/04/17 10:19:57 bsh 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.1  bsh #include <sys/param.h>
     29  1.1  bsh #include <sys/systm.h>
     30  1.1  bsh #include <sys/conf.h>
     31  1.1  bsh #include <sys/uio.h>
     32  1.1  bsh #include <sys/malloc.h>
     33  1.1  bsh #include <sys/device.h>
     34  1.1  bsh 
     35  1.1  bsh #include <dev/cons.h>
     36  1.1  bsh #include <dev/wscons/wsconsio.h>
     37  1.1  bsh #include <dev/wscons/wsdisplayvar.h>
     38  1.1  bsh #include <dev/wscons/wscons_callbacks.h>
     39  1.1  bsh 
     40  1.1  bsh #include <sys/bus.h>
     41  1.1  bsh #include <arm/imx/imx51var.h>
     42  1.1  bsh #include <arm/imx/imx51reg.h>
     43  1.1  bsh #include <arm/imx/imx51_ipuv3var.h>
     44  1.1  bsh #include <arm/imx/imx51_ipuv3reg.h>
     45  1.1  bsh #include <arm/imx/imx51_iomuxreg.h>
     46  1.1  bsh #include <arm/imx/imxgpiovar.h>
     47  1.1  bsh 
     48  1.1  bsh #include "opt_imx51_ipuv3.h"
     49  1.1  bsh #include "opt_netwalker_lcd.h"
     50  1.1  bsh 
     51  1.1  bsh #include "wsdisplay.h"
     52  1.1  bsh #include "ioconf.h"
     53  1.1  bsh 
     54  1.1  bsh int lcd_match(device_t, cfdata_t, void *);
     55  1.1  bsh void lcd_attach(device_t, device_t, void *);
     56  1.1  bsh 
     57  1.1  bsh void netwalker_cnattach(void);
     58  1.1  bsh 
     59  1.1  bsh #if NWSDISPLAY > 0
     60  1.1  bsh #else
     61  1.1  bsh #ifdef LCD_DEBUG
     62  1.1  bsh static void draw_test_pattern(struct imx51_ipuv3_softc *,
     63  1.1  bsh     struct imx51_ipuv3_screen *);
     64  1.1  bsh #endif
     65  1.1  bsh 
     66  1.1  bsh /*
     67  1.1  bsh  * Interface to LCD framebuffer without wscons
     68  1.1  bsh  */
     69  1.1  bsh extern struct cfdriver ipu_cd;
     70  1.1  bsh 
     71  1.1  bsh dev_type_open(lcdopen);
     72  1.1  bsh dev_type_close(lcdclose);
     73  1.1  bsh dev_type_ioctl(lcdioctl);
     74  1.1  bsh dev_type_mmap(lcdmmap);
     75  1.1  bsh const struct cdevsw ipu_cdevsw = {
     76  1.1  bsh 	lcdopen, lcdclose, noread, nowrite, lcdioctl,
     77  1.1  bsh 	nostop, notty, nopoll, lcdmmap, nokqfilter, D_TTY
     78  1.1  bsh };
     79  1.1  bsh 
     80  1.1  bsh #endif
     81  1.1  bsh 
     82  1.1  bsh CFATTACH_DECL_NEW(lcd_netwalker, sizeof (struct imx51_ipuv3_softc),
     83  1.1  bsh     lcd_match, lcd_attach, NULL, NULL);
     84  1.1  bsh 
     85  1.1  bsh int
     86  1.1  bsh lcd_match( device_t parent, cfdata_t cf, void *aux )
     87  1.1  bsh {
     88  1.1  bsh 	return 1;
     89  1.1  bsh }
     90  1.1  bsh 
     91  1.1  bsh /* Sharp's LCD */
     92  1.1  bsh static const struct lcd_panel_geometry sharp_panel =
     93  1.1  bsh {
     94  1.1  bsh 	.panel_width = 1024,	/* Width */
     95  1.1  bsh 	.panel_height = 600,	/* Height */
     96  1.1  bsh 
     97  1.1  bsh 	.pixel_clk = 30076000,
     98  1.1  bsh 
     99  1.1  bsh 	.hsync_width = 8,
    100  1.1  bsh 	.left  = 20,
    101  1.1  bsh 	.right = 20,
    102  1.1  bsh 
    103  1.1  bsh 	.vsync_width = 4,
    104  1.1  bsh 	.upper = 2,
    105  1.1  bsh 	.lower = 2,
    106  1.1  bsh 
    107  1.1  bsh 	.panel_info = 0,
    108  1.1  bsh };
    109  1.1  bsh 
    110  1.1  bsh #define PANEL	sharp_panel
    111  1.1  bsh 
    112  1.1  bsh void lcd_attach( device_t parent, device_t self, void *aux )
    113  1.1  bsh {
    114  1.1  bsh 	struct imx51_ipuv3_softc *sc = device_private(self);
    115  1.1  bsh 	struct axi_attach_args *axia = aux;
    116  1.1  bsh 	bus_space_tag_t iot = axia->aa_iot;
    117  1.1  bsh 
    118  1.1  bsh 	sc->dev = self;
    119  1.1  bsh 
    120  1.1  bsh #if (NWSDISPLAY > 0) && defined(IMXIPUCONSOLE)
    121  1.1  bsh 	netwalker_cnattach();
    122  1.1  bsh #endif
    123  1.1  bsh 
    124  1.1  bsh #if 0
    125  1.1  bsh 	/* IOMUX registers are already set correctly for LCD display */
    126  1.1  bsh 	iomux_mux_config(iomux_ipuv3_config);
    127  1.1  bsh #endif
    128  1.1  bsh 
    129  1.1  bsh 	/* XXX move this to imx51_ipuv3.c */
    130  1.1  bsh 	{
    131  1.1  bsh 		bus_space_handle_t mipi_ioh;
    132  1.1  bsh 		uint32_t reg;
    133  1.1  bsh 
    134  1.1  bsh 		if (bus_space_map(iot, 0x83fdc000, 0x1000, 0, &mipi_ioh))
    135  1.1  bsh 			aprint_error_dev(self, "can't map MIPI HSC");
    136  1.1  bsh 		else {
    137  1.1  bsh 			bus_space_write_4(iot, mipi_ioh, 0x000, 0xf00);
    138  1.1  bsh 
    139  1.1  bsh 			reg = bus_space_read_4(iot, mipi_ioh, 0x800);
    140  1.1  bsh 			bus_space_write_4(iot, mipi_ioh, 0x800, reg | 0x0ff);
    141  1.1  bsh 
    142  1.1  bsh 			reg = bus_space_read_4(iot, mipi_ioh, 0x800);
    143  1.1  bsh 			bus_space_write_4(iot, mipi_ioh, 0x800, reg | 0x10000);
    144  1.1  bsh 		}
    145  1.1  bsh 	}
    146  1.1  bsh 
    147  1.1  bsh 	/* LCD power on */
    148  1.1  bsh 	gpio_set_direction(GPIO_NO(4, 9), GPIO_DIR_OUT);
    149  1.1  bsh 	gpio_set_direction(GPIO_NO(4, 10), GPIO_DIR_OUT);
    150  1.1  bsh 	gpio_set_direction(GPIO_NO(3, 3), GPIO_DIR_OUT);
    151  1.1  bsh 
    152  1.1  bsh 	gpio_data_write(GPIO_NO(3, 3), 1);
    153  1.1  bsh 	gpio_data_write(GPIO_NO(4, 9), 1);
    154  1.1  bsh 	delay(180 * 1000);
    155  1.1  bsh 	gpio_data_write(GPIO_NO(4, 10), 1);
    156  1.1  bsh 
    157  1.1  bsh 	/* pwm pin (100%) */
    158  1.1  bsh 	gpio_set_direction(GPIO_NO(1, 2), GPIO_DIR_OUT);
    159  1.1  bsh 	gpio_data_write(GPIO_NO(1, 2), 1);
    160  1.1  bsh 
    161  1.1  bsh 	gpio_set_direction(GPIO_NO(2, 13), GPIO_DIR_OUT);
    162  1.1  bsh 	gpio_data_write(GPIO_NO(2, 13), 1);
    163  1.1  bsh 
    164  1.1  bsh 	imx51_ipuv3_attach_sub(sc, aux, &PANEL);
    165  1.1  bsh 
    166  1.1  bsh #if NWSDISPLAY == 0
    167  1.1  bsh 	{
    168  1.1  bsh 		struct imx51_ipuv3_screen *screen;
    169  1.1  bsh 		int error;
    170  1.1  bsh 
    171  1.1  bsh 		error = imx51_ipuv3_new_screen(sc, 16, &screen);
    172  1.1  bsh #ifdef LCD_DEBUG
    173  1.1  bsh 		draw_test_pattern(sc, screen);
    174  1.1  bsh #endif
    175  1.1  bsh 		if (error == 0) {
    176  1.1  bsh 			sc->active = screen;
    177  1.1  bsh 			imx51_ipuv3_start_dma(sc, screen);
    178  1.1  bsh 		}
    179  1.1  bsh 	}
    180  1.1  bsh #endif
    181  1.1  bsh }
    182  1.1  bsh 
    183  1.1  bsh #if NWSDISPLAY > 0
    184  1.1  bsh void
    185  1.1  bsh netwalker_cnattach(void)
    186  1.1  bsh {
    187  1.1  bsh 	imx51_ipuv3_cnattach(&PANEL);
    188  1.1  bsh 	return;
    189  1.1  bsh }
    190  1.1  bsh #else
    191  1.1  bsh 
    192  1.1  bsh int
    193  1.1  bsh lcdopen(dev_t dev, int oflags, int devtype, struct lwp *l)
    194  1.1  bsh {
    195  1.1  bsh 	return 0;
    196  1.1  bsh }
    197  1.1  bsh 
    198  1.1  bsh int
    199  1.1  bsh lcdclose(dev_t dev, int fflag, int devtype, struct lwp *l)
    200  1.1  bsh {
    201  1.1  bsh 	return 0;
    202  1.1  bsh }
    203  1.1  bsh 
    204  1.1  bsh paddr_t
    205  1.1  bsh lcdmmap(dev_t dev, off_t offset, int size)
    206  1.1  bsh {
    207  1.1  bsh 	struct imx51_ipuv3_softc *sc =
    208  1.1  bsh 	    device_lookup_private(&ipu_cd, minor(dev));
    209  1.1  bsh 	struct imx51_ipuv3_screen *scr = sc->active;
    210  1.1  bsh 
    211  1.1  bsh 	return bus_dmamem_mmap(sc->dma_tag, scr->segs, scr->nsegs,
    212  1.1  bsh 	    offset, 0, BUS_DMA_WAITOK|BUS_DMA_COHERENT);
    213  1.1  bsh }
    214  1.1  bsh 
    215  1.1  bsh int
    216  1.1  bsh lcdioctl(dev_t dev, u_long cmd, void *data,
    217  1.1  bsh     int fflag, struct lwp *l)
    218  1.1  bsh {
    219  1.1  bsh 	return EOPNOTSUPP;
    220  1.1  bsh }
    221  1.1  bsh 
    222  1.1  bsh #ifdef LCD_DEBUG
    223  1.1  bsh static void
    224  1.1  bsh draw_test_pattern(struct imx51_ipuv3_softc *sc,
    225  1.1  bsh     struct imx51_ipuv3_screen *scr)
    226  1.1  bsh {
    227  1.1  bsh 	int x, y;
    228  1.1  bsh 	uint16_t color, *line;
    229  1.1  bsh 	char *buf = (char *)(scr->buf_va);
    230  1.1  bsh 
    231  1.1  bsh 	printf("%s: buf_va %p, size 0x%x\n", __func__, buf,
    232  1.1  bsh 	       (uint)scr->buf_size);
    233  1.1  bsh 	printf("%s: panel %d x %d\n", __func__,
    234  1.1  bsh 	    sc->geometry->panel_width,
    235  1.1  bsh 	    sc->geometry->panel_height);
    236  1.1  bsh #define	rgb(r,g,b)	(((r)<<11) | ((g)<<5) | (b))
    237  1.1  bsh 
    238  1.1  bsh 	for (y=0; y < sc->geometry->panel_height; ++y) {
    239  1.1  bsh 		line = (uint16_t *)(buf + scr->stride * y);
    240  1.1  bsh 
    241  1.1  bsh 		for (x=0; x < sc->geometry->panel_width; ++x) {
    242  1.1  bsh 			switch (((x/30) + (y/10)) % 8) {
    243  1.1  bsh 			default:
    244  1.1  bsh 			case 0: color = rgb(0x00, 0x00, 0x00); break;
    245  1.1  bsh 			case 1: color = rgb(0x00, 0x00, 0x1f); break;
    246  1.1  bsh 			case 2: color = rgb(0x00, 0x3f, 0x00); break;
    247  1.1  bsh 			case 3: color = rgb(0x00, 0x3f, 0x1f); break;
    248  1.1  bsh 			case 4: color = rgb(0x1f, 0x00, 0x00); break;
    249  1.1  bsh 			case 5: color = rgb(0x1f, 0x00, 0x1f); break;
    250  1.1  bsh 			case 6: color = rgb(0x1f, 0x3f, 0x00); break;
    251  1.1  bsh 			case 7: color = rgb(0x1f, 0x3f, 0x1f); break;
    252  1.1  bsh 			}
    253  1.1  bsh 
    254  1.1  bsh 			line[x] = color;
    255  1.1  bsh 		}
    256  1.1  bsh 	}
    257  1.1  bsh 
    258  1.1  bsh 	for (x=0; x < MIN(sc->geometry->panel_height,
    259  1.1  bsh 		sc->geometry->panel_width); ++x) {
    260  1.1  bsh 		line = (uint16_t *)(buf + scr->stride * x);
    261  1.1  bsh 		line[x] = rgb(0x1f, 0x3f, 0x1f);
    262  1.1  bsh 	}
    263  1.1  bsh }
    264  1.1  bsh #endif
    265  1.1  bsh 
    266  1.1  bsh #endif /* NWSDISPLAY > 0 */
    267  1.1  bsh 
    268  1.1  bsh 
    269