xref: /netbsd-src/sys/arch/evbarm/netwalker/netwalker_lcd.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: netwalker_lcd.c,v 1.3 2014/05/06 11:22:53 hkenken 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.3 2014/05/06 11:22:53 hkenken 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 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/conf.h>
42 #include <sys/uio.h>
43 #include <sys/malloc.h>
44 #include <sys/device.h>
45 #include <sys/pmf.h>
46 
47 #include <dev/cons.h>
48 #include <dev/wscons/wsconsio.h>
49 #include <dev/wscons/wsdisplayvar.h>
50 #include <dev/wscons/wscons_callbacks.h>
51 
52 #include <sys/bus.h>
53 #include <arm/imx/imx51var.h>
54 #include <arm/imx/imx51reg.h>
55 #include <arm/imx/imx51_ipuv3var.h>
56 #include <arm/imx/imx51_ipuv3reg.h>
57 #include <arm/imx/imxgpiovar.h>
58 
59 #include <evbarm/netwalker/netwalker_backlightvar.h>
60 
61 int lcd_match(device_t, cfdata_t, void *);
62 void lcd_attach(device_t, device_t, void *);
63 
64 void netwalker_cnattach(void);
65 
66 #if NWSDISPLAY > 0
67 static int netwalker_lcd_ioctl(void *, void *, u_long, void *, int,
68     struct lwp *);
69 static int netwalker_lcd_show_screen(void *, void *, int,
70     void (*)(void *, int, int), void *);
71 
72 bool netwalker_lcd_console = 0;
73 
74 /*
75  * wsdisplay glue
76  */
77 static struct imx51_wsscreen_descr netwalker_lcd_stdscreen = {
78 	.c = {
79 		.name	      = "std",
80 		.ncols	      = 0,
81 		.nrows	      = 0,
82 		.textops      = NULL,
83 		.fontwidth    = 8,
84 		.fontheight   = 16,
85 		.capabilities = WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
86 		.modecookie   = NULL
87 	},
88 	.depth = 16,		/* bits per pixel */
89 	.flags = RI_CENTER | RI_FULLCLEAR
90 };
91 
92 static const struct wsscreen_descr *netwalker_lcd_scr_descr[] = {
93 	&netwalker_lcd_stdscreen.c,
94 };
95 
96 const struct wsscreen_list netwalker_lcd_screen_list = {
97 	sizeof netwalker_lcd_scr_descr / sizeof netwalker_lcd_scr_descr[0],
98 	netwalker_lcd_scr_descr
99 };
100 
101 struct wsdisplay_accessops netwalker_lcd_accessops = {
102 	.ioctl	      = netwalker_lcd_ioctl,
103 	.mmap	      = imx51_ipuv3_mmap,
104 	.alloc_screen = imx51_ipuv3_alloc_screen,
105 	.free_screen  = imx51_ipuv3_free_screen,
106 	.show_screen  = netwalker_lcd_show_screen,
107 	.load_font    = NULL,
108 	.pollc	      = NULL,
109 	.scroll	      = NULL
110 };
111 #else
112 #ifdef LCD_DEBUG
113 static void draw_test_pattern(struct imx51_ipuv3_softc *,
114     struct imx51_ipuv3_screen *);
115 #endif
116 
117 /*
118  * Interface to LCD framebuffer without wscons
119  */
120 extern struct cfdriver ipu_cd;
121 
122 dev_type_open(lcdopen);
123 dev_type_close(lcdclose);
124 dev_type_ioctl(lcdioctl);
125 dev_type_mmap(lcdmmap);
126 const struct cdevsw ipu_cdevsw = {
127 	.d_open = lcdopen,
128 	.d_close = lcdclose,
129 	.d_read = noread,
130 	.d_write = nowrite,
131 	.d_ioctl = lcdioctl,
132 	.d_stop = nostop,
133 	.d_tty = notty,
134 	.d_poll = nopoll,
135 	.d_mmap = lcdmmap,
136 	.d_kqfilter = nokqfilter,
137 	.d_flag = D_TTY
138 };
139 
140 #endif
141 
142 CFATTACH_DECL_NEW(lcd_netwalker, sizeof (struct imx51_ipuv3_softc),
143     lcd_match, lcd_attach, NULL, NULL);
144 
145 int
146 lcd_match( device_t parent, cfdata_t cf, void *aux )
147 {
148 	return 1;
149 }
150 
151 /* Sharp's LCD */
152 static const struct lcd_panel_geometry sharp_panel =
153 {
154 	.panel_width = 1024,	/* Width */
155 	.panel_height = 600,	/* Height */
156 
157 	.pixel_clk = 30076000,
158 
159 	.hsync_width = 8,
160 	.left  = 20,
161 	.right = 20,
162 
163 	.vsync_width = 4,
164 	.upper = 2,
165 	.lower = 2,
166 
167 	.panel_info = 0,
168 };
169 
170 void lcd_attach( device_t parent, device_t self, void *aux )
171 {
172 	struct imx51_ipuv3_softc *sc = device_private(self);
173 	struct axi_attach_args *axia = aux;
174 	bus_space_tag_t iot = axia->aa_iot;
175 
176 	sc->dev = self;
177 
178 #if defined(IMXIPUCONSOLE)
179 	netwalker_lcd_console = 1;
180 #endif
181 #if (NWSDISPLAY > 0)
182 	netwalker_cnattach();
183 #endif
184 
185 	/* XXX move this to imx51_ipuv3.c */
186 	{
187 		bus_space_handle_t mipi_ioh;
188 		uint32_t reg;
189 
190 		if (bus_space_map(iot, 0x83fdc000, 0x1000, 0, &mipi_ioh))
191 			aprint_error_dev(self, "can't map MIPI HSC");
192 		else {
193 			bus_space_write_4(iot, mipi_ioh, 0x000, 0xf00);
194 
195 			reg = bus_space_read_4(iot, mipi_ioh, 0x800);
196 			bus_space_write_4(iot, mipi_ioh, 0x800, reg | 0x0ff);
197 
198 			reg = bus_space_read_4(iot, mipi_ioh, 0x800);
199 			bus_space_write_4(iot, mipi_ioh, 0x800, reg | 0x10000);
200 		}
201 	}
202 
203 	/* LCD power on */
204 	gpio_set_direction(GPIO_NO(4, 9), GPIO_DIR_OUT);
205 	gpio_set_direction(GPIO_NO(4, 10), GPIO_DIR_OUT);
206 	gpio_set_direction(GPIO_NO(3, 3), GPIO_DIR_OUT);
207 
208 	gpio_data_write(GPIO_NO(3, 3), 1);
209 	gpio_data_write(GPIO_NO(4, 9), 1);
210 	delay(180 * 1000);
211 	gpio_data_write(GPIO_NO(4, 10), 1);
212 
213 	gpio_set_direction(GPIO_NO(2, 13), GPIO_DIR_OUT);
214 	gpio_data_write(GPIO_NO(2, 13), 1);
215 
216 	imx51_ipuv3_attach_sub(sc, aux, &sharp_panel);
217 
218 #if NWSDISPLAY == 0
219 	struct imx51_ipuv3_screen *screen;
220 	int error;
221 
222 	error = imx51_ipuv3_new_screen(sc, 16, &screen);
223 #ifdef LCD_DEBUG
224 	draw_test_pattern(sc, screen);
225 #endif
226 	if (error == 0) {
227 		sc->active = screen;
228 		imx51_ipuv3_start_dma(sc, screen);
229 	}
230 #else
231 	struct wsemuldisplaydev_attach_args aa;
232 
233 #if defined(IMXIPUCONSOLE)
234 	aa.console = true;
235 #else
236 	aa.console = false;
237 #endif
238 	aa.scrdata = &netwalker_lcd_screen_list;
239 	aa.accessops = &netwalker_lcd_accessops;
240 	aa.accesscookie = &sc->vd;
241 
242 	(void) config_found(sc->dev, &aa, wsemuldisplaydevprint);
243 #endif
244 }
245 
246 #if NWSDISPLAY > 0
247 void
248 netwalker_cnattach(void)
249 {
250 	imx51_ipuv3_cnattach(netwalker_lcd_console, &netwalker_lcd_stdscreen,
251 	    &netwalker_lcd_accessops, &sharp_panel);
252 	return;
253 }
254 
255 /*
256  * wsdisplay accessops overrides
257  */
258 static int
259 netwalker_lcd_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
260 {
261 	int res = EINVAL;
262 
263 	switch (cmd) {
264 #if NNETWALKER_BACKLIGHT > 0
265 	case WSDISPLAYIO_GETPARAM:
266 	case WSDISPLAYIO_SETPARAM:
267 		res = netwalker_lcd_param_ioctl(cmd, (struct wsdisplay_param *)data);
268 		break;
269 #endif
270 	}
271 
272 	if (res == EINVAL)
273 		res = imx51_ipuv3_ioctl(v, vs, cmd, data, flag, l);
274 	return res;
275 }
276 
277 static int
278 netwalker_lcd_show_screen(void *v, void *cookie, int waitok,
279     void (*cb_func)(void *, int, int), void *cb_arg)
280 {
281 	int error;
282 
283 	error = imx51_ipuv3_show_screen(v, cookie, waitok, cb_func, cb_arg);
284 	if (error)
285 		return (error);
286 
287 	return 0;
288 }
289 #else
290 
291 int
292 lcdopen(dev_t dev, int oflags, int devtype, struct lwp *l)
293 {
294 	return 0;
295 }
296 
297 int
298 lcdclose(dev_t dev, int fflag, int devtype, struct lwp *l)
299 {
300 	return 0;
301 }
302 
303 paddr_t
304 lcdmmap(dev_t dev, off_t offset, int size)
305 {
306 	struct imx51_ipuv3_softc *sc =
307 	    device_lookup_private(&ipu_cd, minor(dev));
308 	struct imx51_ipuv3_screen *scr = sc->active;
309 
310 	return bus_dmamem_mmap(sc->dma_tag, scr->segs, scr->nsegs,
311 	    offset, 0, BUS_DMA_WAITOK|BUS_DMA_COHERENT);
312 }
313 
314 int
315 lcdioctl(dev_t dev, u_long cmd, void *data,
316     int fflag, struct lwp *l)
317 {
318 	return EOPNOTSUPP;
319 }
320 
321 #ifdef LCD_DEBUG
322 static void
323 draw_test_pattern(struct imx51_ipuv3_softc *sc,
324     struct imx51_ipuv3_screen *scr)
325 {
326 	int x, y;
327 	uint16_t color, *line;
328 	char *buf = (char *)(scr->buf_va);
329 
330 	printf("%s: buf_va %p, size 0x%x\n", __func__, buf,
331 	       (uint)scr->buf_size);
332 	printf("%s: panel %d x %d\n", __func__,
333 	    sc->geometry->panel_width,
334 	    sc->geometry->panel_height);
335 #define	rgb(r,g,b)	(((r)<<11) | ((g)<<5) | (b))
336 
337 	for (y=0; y < sc->geometry->panel_height; ++y) {
338 		line = (uint16_t *)(buf + scr->stride * y);
339 
340 		for (x=0; x < sc->geometry->panel_width; ++x) {
341 			switch (((x/30) + (y/10)) % 8) {
342 			default:
343 			case 0: color = rgb(0x00, 0x00, 0x00); break;
344 			case 1: color = rgb(0x00, 0x00, 0x1f); break;
345 			case 2: color = rgb(0x00, 0x3f, 0x00); break;
346 			case 3: color = rgb(0x00, 0x3f, 0x1f); break;
347 			case 4: color = rgb(0x1f, 0x00, 0x00); break;
348 			case 5: color = rgb(0x1f, 0x00, 0x1f); break;
349 			case 6: color = rgb(0x1f, 0x3f, 0x00); break;
350 			case 7: color = rgb(0x1f, 0x3f, 0x1f); break;
351 			}
352 
353 			line[x] = color;
354 		}
355 	}
356 
357 	for (x=0; x < MIN(sc->geometry->panel_height,
358 		sc->geometry->panel_width); ++x) {
359 		line = (uint16_t *)(buf + scr->stride * x);
360 		line[x] = rgb(0x1f, 0x3f, 0x1f);
361 	}
362 }
363 #endif
364 
365 #endif /* NWSDISPLAY > 0 */
366 
367 
368