xref: /netbsd-src/sys/arch/evbarm/g42xxeb/g42xxeb_lcd.c (revision 7433666e375b3ac4cc764df5a6726be98bc1cdd5)
1 /* $NetBSD: g42xxeb_lcd.c,v 1.19 2023/12/20 13:55:17 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 2001, 2002, 2005 Genetec corp.
5  * All rights reserved.
6  *
7  * LCD driver for Genetec G4250EB-X002.
8  * Written by Hiroyuki Bessho for Genetec corp.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of The NetBSD Foundation nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 #include "opt_g42xxlcd.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/conf.h>
39 #include <sys/uio.h>
40 
41 #include <dev/cons.h>
42 #include <dev/wscons/wsconsio.h>
43 #include <dev/wscons/wsdisplayvar.h>
44 #include <dev/wscons/wscons_callbacks.h>
45 
46 #include <sys/bus.h>
47 #include <arm/sa11x0/sa11x0_var.h>
48 #include <arm/xscale/pxa2x0var.h>
49 #include <arm/xscale/pxa2x0reg.h>
50 #include <arm/xscale/pxa2x0_lcd.h>
51 
52 #include <arch/evbarm/g42xxeb/g42xxeb_reg.h>
53 #include <arch/evbarm/g42xxeb/g42xxeb_var.h>
54 
55 #include "wsdisplay.h"
56 #include "ioconf.h"
57 
58 int	lcd_match(device_t, cfdata_t, void *);
59 void	lcd_attach(device_t, device_t, void *);
60 int	lcdintr(void *);
61 
62 #if NWSDISPLAY > 0
63 
64 /*
65  * wsdisplay glue
66  */
67 struct pxa2x0_wsscreen_descr lcd_bpp16_screen = {
68 	{
69 		"bpp16", 0, 0,
70 		&pxa2x0_lcd_emulops,
71 		0, 0,
72 		WSSCREEN_WSCOLORS,
73 	},
74 	16				/* bits per pixel */
75 }, lcd_bpp8_screen = {
76 	{
77 		"bpp8", 0, 0,
78 		&pxa2x0_lcd_emulops,
79 		0, 0,
80 		WSSCREEN_WSCOLORS,
81 	},
82 	8				/* bits per pixel */
83 }, lcd_bpp4_screen = {
84 	{
85 		"bpp4", 0, 0,
86 		&pxa2x0_lcd_emulops,
87 		0, 0,
88 		WSSCREEN_WSCOLORS,
89 	},
90 	4				/* bits per pixel */
91 };
92 
93 
94 static const struct wsscreen_descr *lcd_scr_descr[] = {
95 	&lcd_bpp4_screen.c,
96 	&lcd_bpp8_screen.c,
97 	&lcd_bpp16_screen.c,
98 };
99 
100 const struct wsscreen_list lcd_screen_list = {
101 	sizeof lcd_scr_descr / sizeof lcd_scr_descr[0],
102 	lcd_scr_descr
103 };
104 
105 int	lcd_ioctl(void *, void *, u_long, void *, int, struct lwp *);
106 
107 int	lcd_show_screen(void *, void *, int,
108 	    void (*)(void *, int, int), void *);
109 
110 const struct wsdisplay_accessops lcd_accessops = {
111 	lcd_ioctl,
112 	pxa2x0_lcd_mmap,
113 	pxa2x0_lcd_alloc_screen,
114 	pxa2x0_lcd_free_screen,
115 	lcd_show_screen,
116 	NULL, /* load_font */
117 };
118 
119 #else
120 /*
121  * Interface to LCD framebuffer without wscons
122  */
123 dev_type_open(lcdopen);
124 dev_type_close(lcdclose);
125 dev_type_ioctl(lcdioctl);
126 dev_type_mmap(lcdmmap);
127 const struct cdevsw lcd_cdevsw = {
128 	.d_open = lcdopen,
129 	.d_close = lcdclose,
130 	.d_read = noread,
131 	.d_write = nowrite,
132 	.d_ioctl = lcdioctl,
133 	.d_stop = nostop,
134 	.d_tty = notty,
135 	.d_poll = nopoll,
136 	.d_mmap = lcdmmap,
137 	.d_kqfilter = nokqfilter,
138 	.d_discard = nodiscard,
139 	.d_flag = D_TTY
140 };
141 
142 #endif
143 
144 CFATTACH_DECL_NEW(lcd_obio, sizeof (struct pxa2x0_lcd_softc),
145     lcd_match, lcd_attach, NULL, NULL);
146 
147 int
lcd_match(device_t parent,cfdata_t cf,void * aux)148 lcd_match( device_t parent, cfdata_t cf, void *aux )
149 {
150 	return 1;
151 }
152 
153 #ifdef G4250_LCD_NEC_NL3224BC35
154 /* NEC's QVGA LCD */
155 static const struct lcd_panel_geometry nec_NL3224BC35 =
156 {
157     320,			/* Width */
158     240,			/* Height */
159     0,				/* No extra lines */
160 
161     LCDPANEL_SINGLE|LCDPANEL_ACTIVE|LCDPANEL_PCP|
162     LCDPANEL_VSP|LCDPANEL_HSP,
163     7,				/* clock divider, 6.25MHz at 100MHz */
164     0xff,			/* AC bias pin freq */
165 
166     6,				/* horizontal sync pulse width */
167     70,				/* BLW */
168     7,				/* ELW */
169 
170     10,				/* vertical sync pulse width */
171     11,				/* BFW */
172     1,				/* EFW */
173 };
174 #endif
175 
176 #ifdef G4250_LCD_TOSHIBA_LTM035
177 const struct lcd_panel_geometry toshiba_LTM035 =
178 {
179     240,			/* Width */
180     320,			/* Height */
181     0,				/* No extra lines */
182 
183     LCDPANEL_SINGLE|LCDPANEL_ACTIVE|   /* LCDPANEL_PCP| */
184     LCDPANEL_VSP|LCDPANEL_HSP,
185     11,				/* clock divider, 4.5 MHz at 100 MHz */
186 				/* required: 4.28..4.7 MHz  */
187     0xff,			/* AC bias pin freq */
188 
189     4,				/* horizontal sync pulse width */
190     8,				/* BLW (back porch) */
191     4,				/* ELW (front porch) */
192 
193     2,				/* vertical sync pulse width */
194     2,				/* BFW (back porch) */
195     3,				/* EFW (front porch) */
196 
197 };
198 #endif /* G4250_LCD_TOSHIBA_LTM035 */
199 
lcd_attach(device_t parent,device_t self,void * aux)200 void lcd_attach(device_t parent, device_t self, void *aux)
201 {
202 	struct pxa2x0_lcd_softc *sc = device_private(self);
203 	struct pxaip_attach_args paa;
204 	struct obio_attach_args *oba = aux;
205 
206 	sc->dev = self;
207 
208 	paa.pxa_name = "obio";
209 	paa.pxa_iot = oba->oba_iot;
210 	paa.pxa_addr = oba->oba_addr;
211 	paa.pxa_size = 0;		/* XXX */
212 	paa.pxa_intr = oba->oba_intr;
213 
214 #ifdef G4250_LCD_TOSHIBA_LTM035
215 # define PANEL	toshiba_LTM035
216 #else
217 # define PANEL	nec_NL3224BC35
218 #endif
219 
220 	pxa2x0_lcd_attach_sub(sc, &paa, &PANEL);
221 
222 
223 #if NWSDISPLAY > 0
224 
225 	{
226 		struct wsemuldisplaydev_attach_args aa;
227 
228 		/* make wsdisplay screen list */
229 		pxa2x0_lcd_setup_wsscreen(&lcd_bpp16_screen, &PANEL, NULL);
230 		pxa2x0_lcd_setup_wsscreen(&lcd_bpp8_screen, &PANEL, NULL);
231 		pxa2x0_lcd_setup_wsscreen(&lcd_bpp4_screen, &PANEL, NULL);
232 
233 		aa.console = 0;
234 		aa.scrdata = &lcd_screen_list;
235 		aa.accessops = &lcd_accessops;
236 		aa.accesscookie = sc;
237 
238 		(void) config_found(self, &aa, wsemuldisplaydevprint,
239 		    CFARGS_NONE);
240 	}
241 #else
242 	{
243 		struct pxa2x0_lcd_screen *screen;
244 		int error;
245 
246 		error = pxa2x0_lcd_new_screen( sc, 16, &screen );
247 		if (error == 0) {
248 			sc->active = screen;
249 			pxa2x0_lcd_start_dma(sc, screen);
250 		}
251 	}
252 #endif
253 
254 #undef PANEL
255 
256 }
257 
258 #if NWSDISPLAY > 0
259 
260 int
lcd_ioctl(void * v,void * vs,u_long cmd,void * data,int flag,struct lwp * l)261 lcd_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
262 {
263 	struct pxa2x0_lcd_softc *sc = v;
264 	struct obio_softc *osc =
265 	    device_private(device_parent(sc->dev));
266 	uint16_t reg;
267 
268 	switch (cmd) {
269 	case WSDISPLAYIO_SVIDEO:
270 		reg = bus_space_read_2(osc->sc_iot, osc->sc_obioreg_ioh,
271 		    G42XXEB_LCDCTL);
272 		if (*(int *)data == WSDISPLAYIO_VIDEO_ON)
273 			reg |= LCDCTL_BL_ON;
274 		else
275 			reg &= ~LCDCTL_BL_ON;
276 		bus_space_write_2(osc->sc_iot, osc->sc_obioreg_ioh,
277 			G42XXEB_LCDCTL, reg);
278 		bus_space_write_1(osc->sc_iot, osc->sc_obioreg_ioh,
279 			G42XXEB_LED, reg);
280 		printf("LCD control: %x\n", reg);
281 		break;			/* turn on/off LCD controller */
282 	}
283 
284 	return pxa2x0_lcd_ioctl(v, vs, cmd, data, flag, l);
285 }
286 
287 int
lcd_show_screen(void * v,void * cookie,int waitok,void (* cb)(void *,int,int),void * cbarg)288 lcd_show_screen(void *v, void *cookie, int waitok,
289     void (*cb)(void *, int, int), void *cbarg)
290 {
291 	struct pxa2x0_lcd_softc *sc = v;
292 	struct obio_softc *osc =
293 	    device_private(device_parent(sc->dev));
294 	uint16_t reg;
295 
296 	pxa2x0_lcd_show_screen(v,cookie,waitok,cb,cbarg);
297 
298 	/* Turn on LCD backlight.
299 	   XXX: with fixed blightness. want new ioctl to set blightness. */
300 	reg = bus_space_read_2(osc->sc_iot, osc->sc_obioreg_ioh, G42XXEB_LCDCTL);
301 	bus_space_write_2(osc->sc_iot, osc->sc_obioreg_ioh, G42XXEB_LCDCTL,
302 	    (reg & ~LCDCTL_BL_PWN) | 0x4000 | LCDCTL_BL_ON);
303 
304 	return 0;
305 }
306 
307 
308 
309 #else  /* NWSDISPLAY==0 */
310 
311 int
lcdopen(dev_t dev,int oflags,int devtype,struct lwp * l)312 lcdopen(dev_t dev, int oflags, int devtype, struct lwp *l)
313 {
314 	struct pxa2x0_lcd_softc *sc =
315 		device_lookup_private(&lcd_cd, minor(dev));
316 	struct obio_softc *osc =
317 	    device_private(device_parent(sc->dev));
318 	uint16_t reg;
319 
320 	/* Turn on LCD backlight.
321 	   XXX: with fixed blightness. want new ioctl to set blightness. */
322 	reg = bus_space_read_2(osc->sc_iot, osc->sc_obioreg_ioh, G42XXEB_LCDCTL);
323 	bus_space_write_2(osc->sc_iot, osc->sc_obioreg_ioh, G42XXEB_LCDCTL,
324 	    (reg & ~LCDCTL_BL_PWN) | 0x4000 | LCDCTL_BL_ON);
325 
326 
327 	return 0;
328 }
329 
330 int
lcdclose(dev_t dev,int fflag,int devtype,struct lwp * l)331 lcdclose(dev_t dev, int fflag, int devtype, struct lwp *l)
332 {
333 	return 0;
334 }
335 
336 paddr_t
lcdmmap(dev_t dev,off_t offset,int size)337 lcdmmap(dev_t dev, off_t offset, int size)
338 {
339 	struct pxa2x0_lcd_softc *sc =
340 		device_lookup_private(&lcd_cd, minor(dev));
341 	struct pxa2x0_lcd_screen *scr = sc->active;
342 
343 	return bus_dmamem_mmap( &pxa2x0_bus_dma_tag, scr->segs, scr->nsegs,
344 	    offset, 0, BUS_DMA_WAITOK|BUS_DMA_COHERENT );
345 }
346 
347 int
lcdioctl(dev_t dev,u_long cmd,void * data,int fflag,struct lwp * l)348 lcdioctl(dev_t dev, u_long cmd, void *data,
349 	    int fflag, struct lwp *l)
350 {
351 	return EOPNOTSUPP;
352 }
353 
354 #endif /* NWSDISPLAY>0 */
355