1/*	$NetBSD: netwalker_lcd.c,v 1.7 2020/01/15 10:25:47 skrll Exp $	*/
2
3/*-
4 * Copyright (c) 2011, 2012 Genetec corp. All rights reserved.
5 * Written by Hashimoto Kenichi for Genetec corp.
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 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: netwalker_lcd.c,v 1.7 2020/01/15 10:25:47 skrll Exp $");
31
32#include "opt_imx51_ipuv3.h"
33#include "opt_netwalker_lcd.h"
34
35#include "wsdisplay.h"
36#include "ioconf.h"
37#include "netwalker_backlight.h"
38
39#define	_INTR_PRIVATE
40
41#include <sys/param.h>
42#include <sys/device.h>
43#include <sys/gpio.h>
44
45#include <sys/bus.h>
46#include <arm/imx/imx51var.h>
47#include <arm/imx/imx51reg.h>
48#include <arm/imx/imx51_ipuv3var.h>
49#include <arm/imx/imx51_ipuv3reg.h>
50#include <arm/imx/imxgpiovar.h>
51
52#include <evbarm/netwalker/netwalker_backlightvar.h>
53
54int lcd_match(device_t, cfdata_t, void *);
55void lcd_attach(device_t, device_t, void *);
56
57#if NWSDISPLAY == 0
58
59#ifdef LCD_DEBUG
60static void draw_test_pattern(struct imx51_ipuv3_softc *,
61    struct imx51_ipuv3_screen *);
62#endif
63
64/*
65 * Interface to LCD framebuffer without wscons
66 */
67extern struct cfdriver ipu_cd;
68
69dev_type_open(lcdopen);
70dev_type_close(lcdclose);
71dev_type_ioctl(lcdioctl);
72dev_type_mmap(lcdmmap);
73const struct cdevsw ipu_cdevsw = {
74	.d_open = lcdopen,
75	.d_close = lcdclose,
76	.d_read = noread,
77	.d_write = nowrite,
78	.d_ioctl = lcdioctl,
79	.d_stop = nostop,
80	.d_tty = notty,
81	.d_poll = nopoll,
82	.d_mmap = lcdmmap,
83	.d_kqfilter = nokqfilter,
84	.d_discard = nodiscard,
85	.d_flag = D_TTY
86};
87
88#endif
89
90CFATTACH_DECL_NEW(lcd_netwalker, sizeof (struct imx51_ipuv3_softc),
91    lcd_match, lcd_attach, NULL, NULL);
92
93int
94lcd_match( device_t parent, cfdata_t cf, void *aux )
95{
96	return 1;
97}
98
99/* Sharp's LCD */
100static const struct lcd_panel_geometry sharp_panel = {
101	.depth = 16,
102
103	.panel_width = 1024,	/* Width */
104	.panel_height = 600,	/* Height */
105
106	.pixel_clk = 30076000,
107
108	.hsync_width = 8,
109	.left  = 20,
110	.right = 20,
111
112	.vsync_width = 4,
113	.upper = 2,
114	.lower = 2,
115
116	.panel_info = 0,
117};
118
119void lcd_attach( device_t parent, device_t self, void *aux )
120{
121	struct imx51_ipuv3_softc *sc = device_private(self);
122	struct axi_attach_args *axia = aux;
123	bus_space_tag_t iot = axia->aa_iot;
124
125	sc->dev = self;
126
127	/* XXX move this to imx51_ipuv3.c */
128	{
129		bus_space_handle_t mipi_ioh;
130		uint32_t reg;
131
132		if (bus_space_map(iot, 0x83fdc000, 0x1000, 0, &mipi_ioh))
133			aprint_error_dev(self, "can't map MIPI HSC");
134		else {
135			bus_space_write_4(iot, mipi_ioh, 0x000, 0xf00);
136
137			reg = bus_space_read_4(iot, mipi_ioh, 0x800);
138			bus_space_write_4(iot, mipi_ioh, 0x800, reg | 0x0ff);
139
140			reg = bus_space_read_4(iot, mipi_ioh, 0x800);
141			bus_space_write_4(iot, mipi_ioh, 0x800, reg | 0x10000);
142		}
143	}
144
145	/* LCD power on */
146	imxgpio_set_direction(GPIO_NO(4, 9), GPIO_PIN_OUTPUT);
147	imxgpio_set_direction(GPIO_NO(4, 10), GPIO_PIN_OUTPUT);
148	imxgpio_set_direction(GPIO_NO(3, 3), GPIO_PIN_OUTPUT);
149
150	imxgpio_data_write(GPIO_NO(3, 3), GPIO_PIN_HIGH);
151	imxgpio_data_write(GPIO_NO(4, 9), GPIO_PIN_HIGH);
152	delay(180 * 1000);
153	imxgpio_data_write(GPIO_NO(4, 10), GPIO_PIN_HIGH);
154
155	imxgpio_set_direction(GPIO_NO(2, 13), GPIO_PIN_OUTPUT);
156	imxgpio_data_write(GPIO_NO(2, 13), GPIO_PIN_HIGH);
157
158	imx51_ipuv3_attach_sub(sc, aux, &sharp_panel);
159
160#if NWSDISPLAY == 0
161	struct imx51_ipuv3_screen *screen;
162	int error;
163
164	error = imx51_ipuv3_new_screen(sc, &screen);
165#ifdef LCD_DEBUG
166	draw_test_pattern(sc, screen);
167#endif
168	if (error == 0) {
169		sc->active = screen;
170		imx51_ipuv3_start_dma(sc, screen);
171	}
172#endif
173}
174
175#if NWSDISPLAY == 0
176
177int
178lcdopen(dev_t dev, int oflags, int devtype, struct lwp *l)
179{
180	return 0;
181}
182
183int
184lcdclose(dev_t dev, int fflag, int devtype, struct lwp *l)
185{
186	return 0;
187}
188
189paddr_t
190lcdmmap(dev_t dev, off_t offset, int size)
191{
192	struct imx51_ipuv3_softc *sc =
193	    device_lookup_private(&ipu_cd, minor(dev));
194	struct imx51_ipuv3_screen *scr = sc->active;
195
196	return bus_dmamem_mmap(sc->dma_tag, scr->segs, scr->nsegs,
197	    offset, 0, BUS_DMA_WAITOK|BUS_DMA_COHERENT);
198}
199
200int
201lcdioctl(dev_t dev, u_long cmd, void *data,
202    int fflag, struct lwp *l)
203{
204	return EOPNOTSUPP;
205}
206
207#ifdef LCD_DEBUG
208static void
209draw_test_pattern(struct imx51_ipuv3_softc *sc,
210    struct imx51_ipuv3_screen *scr)
211{
212	int x, y;
213	uint16_t color, *line;
214	char *buf = (char *)(scr->buf_va);
215
216	printf("%s: buf_va %p, size 0x%x\n", __func__, buf,
217	       (uint)scr->buf_size);
218	printf("%s: panel %d x %d\n", __func__,
219	    sc->geometry->panel_width,
220	    sc->geometry->panel_height);
221#define	rgb(r,g,b)	(((r)<<11) | ((g)<<5) | (b))
222
223	for (y=0; y < sc->geometry->panel_height; ++y) {
224		line = (uint16_t *)(buf + scr->stride * y);
225
226		for (x=0; x < sc->geometry->panel_width; ++x) {
227			switch (((x/30) + (y/10)) % 8) {
228			default:
229			case 0: color = rgb(0x00, 0x00, 0x00); break;
230			case 1: color = rgb(0x00, 0x00, 0x1f); break;
231			case 2: color = rgb(0x00, 0x3f, 0x00); break;
232			case 3: color = rgb(0x00, 0x3f, 0x1f); break;
233			case 4: color = rgb(0x1f, 0x00, 0x00); break;
234			case 5: color = rgb(0x1f, 0x00, 0x1f); break;
235			case 6: color = rgb(0x1f, 0x3f, 0x00); break;
236			case 7: color = rgb(0x1f, 0x3f, 0x1f); break;
237			}
238
239			line[x] = color;
240		}
241	}
242
243	for (x=0; x < MIN(sc->geometry->panel_height,
244		sc->geometry->panel_width); ++x) {
245		line = (uint16_t *)(buf + scr->stride * x);
246		line[x] = rgb(0x1f, 0x3f, 0x1f);
247	}
248}
249#endif
250
251#endif /* NWSDISPLAY == 0 */
252
253
254