xref: /netbsd/sys/arch/cobalt/dev/lcdpanel.c (revision 8ca23f94)
1*8ca23f94Schristos /* $NetBSD: lcdpanel.c,v 1.1 2018/04/09 20:16:16 christos Exp $ */
2*8ca23f94Schristos 
3*8ca23f94Schristos /*
4*8ca23f94Schristos  * Copyright (c) 2002 Dennis I. Chernoivanov
5*8ca23f94Schristos  * All rights reserved.
6*8ca23f94Schristos  *
7*8ca23f94Schristos  * Redistribution and use in source and binary forms, with or without
8*8ca23f94Schristos  * modification, are permitted provided that the following conditions
9*8ca23f94Schristos  * are met:
10*8ca23f94Schristos  * 1. Redistributions of source code must retain the above copyright
11*8ca23f94Schristos  *    notice, this list of conditions and the following disclaimer.
12*8ca23f94Schristos  * 2. Redistributions in binary form must reproduce the above copyright
13*8ca23f94Schristos  *    notice, this list of conditions and the following disclaimer in the
14*8ca23f94Schristos  *    documentation and/or other materials provided with the distribution.
15*8ca23f94Schristos  * 3. The name of the author may not be used to endorse or promote products
16*8ca23f94Schristos  *    derived from this software without specific prior written permission
17*8ca23f94Schristos  *
18*8ca23f94Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19*8ca23f94Schristos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20*8ca23f94Schristos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21*8ca23f94Schristos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22*8ca23f94Schristos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23*8ca23f94Schristos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*8ca23f94Schristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*8ca23f94Schristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*8ca23f94Schristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27*8ca23f94Schristos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*8ca23f94Schristos  */
29*8ca23f94Schristos 
30*8ca23f94Schristos #include <sys/cdefs.h>
31*8ca23f94Schristos __KERNEL_RCSID(0, "$NetBSD: lcdpanel.c,v 1.1 2018/04/09 20:16:16 christos Exp $");
32*8ca23f94Schristos 
33*8ca23f94Schristos #include <sys/param.h>
34*8ca23f94Schristos #include <sys/systm.h>
35*8ca23f94Schristos #include <sys/proc.h>
36*8ca23f94Schristos #include <sys/poll.h>
37*8ca23f94Schristos #include <sys/conf.h>
38*8ca23f94Schristos #include <sys/uio.h>
39*8ca23f94Schristos #include <sys/types.h>
40*8ca23f94Schristos #include <sys/kernel.h>
41*8ca23f94Schristos #include <sys/device.h>
42*8ca23f94Schristos #include <sys/callout.h>
43*8ca23f94Schristos #include <sys/select.h>
44*8ca23f94Schristos #include <sys/reboot.h>
45*8ca23f94Schristos 
46*8ca23f94Schristos #include <sys/bus.h>
47*8ca23f94Schristos #include <machine/autoconf.h>
48*8ca23f94Schristos 
49*8ca23f94Schristos #include <dev/ic/hd44780reg.h>
50*8ca23f94Schristos #include <dev/ic/hd44780var.h>
51*8ca23f94Schristos #include <dev/ic/lcdkp_subr.h>
52*8ca23f94Schristos 
53*8ca23f94Schristos #include "ioconf.h"
54*8ca23f94Schristos 
55*8ca23f94Schristos #define LCDPANEL_POLLRATE	(hz / 10)
56*8ca23f94Schristos #define LCDPANEL_REGION	0x20
57*8ca23f94Schristos #define DATA_OFFSET	0x10
58*8ca23f94Schristos #define LCDPANEL_COLS	16
59*8ca23f94Schristos #define LCDPANEL_VCOLS	40
60*8ca23f94Schristos #define LCDPANEL_ROWS	2
61*8ca23f94Schristos 
62*8ca23f94Schristos struct lcdpanel_softc {
63*8ca23f94Schristos 	device_t sc_dev;
64*8ca23f94Schristos 
65*8ca23f94Schristos 	struct hd44780_chip sc_lcd;
66*8ca23f94Schristos 	struct lcdkp_chip sc_kp;
67*8ca23f94Schristos 
68*8ca23f94Schristos 	struct selinfo sc_selq;
69*8ca23f94Schristos 	struct callout sc_callout;
70*8ca23f94Schristos };
71*8ca23f94Schristos 
72*8ca23f94Schristos struct lcd_message {
73*8ca23f94Schristos 	const char firstcol[LCDPANEL_VCOLS];
74*8ca23f94Schristos 	const char secondcol[LCDPANEL_VCOLS];
75*8ca23f94Schristos };
76*8ca23f94Schristos static const struct lcd_message startup_message = {
77*8ca23f94Schristos 	"NetBSD/cobalt   ",
78*8ca23f94Schristos 	"Starting up...  "
79*8ca23f94Schristos };
80*8ca23f94Schristos static const struct lcd_message halt_message = {
81*8ca23f94Schristos 	"NetBSD/cobalt   ",
82*8ca23f94Schristos 	"Halting...      "
83*8ca23f94Schristos };
84*8ca23f94Schristos static const struct lcd_message reboot_message = {
85*8ca23f94Schristos 	"NetBSD/cobalt   ",
86*8ca23f94Schristos 	"Rebooting...    "
87*8ca23f94Schristos };
88*8ca23f94Schristos 
89*8ca23f94Schristos static int	lcdpanel_match(device_t, cfdata_t, void *);
90*8ca23f94Schristos static void	lcdpanel_attach(device_t, device_t, void *);
91*8ca23f94Schristos static bool	lcdpanel_shutdown(device_t, int);
92*8ca23f94Schristos 
93*8ca23f94Schristos static void	lcdpanel_soft(void *);
94*8ca23f94Schristos 
95*8ca23f94Schristos static uint8_t	lcdpanel_cbt_kprread(bus_space_tag_t, bus_space_handle_t);
96*8ca23f94Schristos static uint8_t	lcdpanel_cbt_hdreadreg(struct hd44780_chip *, uint32_t, uint32_t);
97*8ca23f94Schristos static void	lcdpanel_cbt_hdwritereg(struct hd44780_chip *, uint32_t, uint32_t,
98*8ca23f94Schristos     uint8_t);
99*8ca23f94Schristos 
100*8ca23f94Schristos dev_type_open(lcdpanelopen);
101*8ca23f94Schristos dev_type_close(lcdpanelclose);
102*8ca23f94Schristos dev_type_read(lcdpanelread);
103*8ca23f94Schristos dev_type_write(lcdpanelwrite);
104*8ca23f94Schristos dev_type_ioctl(lcdpanelioctl);
105*8ca23f94Schristos dev_type_poll(lcdpanelpoll);
106*8ca23f94Schristos 
107*8ca23f94Schristos const struct cdevsw lcdpanel_cdevsw = {
108*8ca23f94Schristos 	.d_open = lcdpanelopen,
109*8ca23f94Schristos 	.d_close = lcdpanelclose,
110*8ca23f94Schristos 	.d_read = lcdpanelread,
111*8ca23f94Schristos 	.d_write = lcdpanelwrite,
112*8ca23f94Schristos 	.d_ioctl = lcdpanelioctl,
113*8ca23f94Schristos 	.d_stop = nostop,
114*8ca23f94Schristos 	.d_tty = notty,
115*8ca23f94Schristos 	.d_poll = lcdpanelpoll,
116*8ca23f94Schristos 	.d_mmap = nommap,
117*8ca23f94Schristos 	.d_kqfilter = nokqfilter,
118*8ca23f94Schristos 	.d_discard = nodiscard,
119*8ca23f94Schristos 	.d_flag = 0
120*8ca23f94Schristos };
121*8ca23f94Schristos 
122*8ca23f94Schristos CFATTACH_DECL_NEW(lcdpanel, sizeof(struct lcdpanel_softc),
123*8ca23f94Schristos     lcdpanel_match, lcdpanel_attach, NULL, NULL);
124*8ca23f94Schristos 
125*8ca23f94Schristos static int
lcdpanel_match(device_t parent,cfdata_t cf,void * aux)126*8ca23f94Schristos lcdpanel_match(device_t parent, cfdata_t cf, void *aux)
127*8ca23f94Schristos {
128*8ca23f94Schristos 
129*8ca23f94Schristos 	return 1;
130*8ca23f94Schristos }
131*8ca23f94Schristos 
132*8ca23f94Schristos static void
lcdpanel_attach(device_t parent,device_t self,void * aux)133*8ca23f94Schristos lcdpanel_attach(device_t parent, device_t self, void *aux)
134*8ca23f94Schristos {
135*8ca23f94Schristos 	struct lcdpanel_softc *sc = device_private(self);
136*8ca23f94Schristos 	struct mainbus_attach_args *maa = aux;
137*8ca23f94Schristos 	struct hd44780_io io;
138*8ca23f94Schristos 	static struct lcdkp_xlate keys[] = {
139*8ca23f94Schristos 		{ 0xfa, 'h' },
140*8ca23f94Schristos 		{ 0xf6, 'k' },
141*8ca23f94Schristos 		{ 0xde, 'l' },
142*8ca23f94Schristos 		{ 0xee, 'j' },
143*8ca23f94Schristos 		{ 0x7e, 's' },
144*8ca23f94Schristos 		{ 0xbe, 'e' }
145*8ca23f94Schristos 	};
146*8ca23f94Schristos 
147*8ca23f94Schristos 	sc->sc_lcd.sc_dev = self;
148*8ca23f94Schristos 	sc->sc_lcd.sc_iot = maa->ma_iot;
149*8ca23f94Schristos 	if (bus_space_map(sc->sc_lcd.sc_iot, maa->ma_addr, LCDPANEL_REGION,
150*8ca23f94Schristos 	    0, &sc->sc_lcd.sc_ioir)) {
151*8ca23f94Schristos 		aprint_error(": unable to map registers\n");
152*8ca23f94Schristos 		return;
153*8ca23f94Schristos 	}
154*8ca23f94Schristos 	bus_space_subregion(sc->sc_lcd.sc_iot, sc->sc_lcd.sc_ioir, DATA_OFFSET,
155*8ca23f94Schristos 	    1, &sc->sc_lcd.sc_iodr);
156*8ca23f94Schristos 
157*8ca23f94Schristos 	printf("\n");
158*8ca23f94Schristos 
159*8ca23f94Schristos 	sc->sc_lcd.sc_dev_ok = 1;
160*8ca23f94Schristos 	sc->sc_lcd.sc_cols = LCDPANEL_COLS;
161*8ca23f94Schristos 	sc->sc_lcd.sc_vcols = LCDPANEL_VCOLS;
162*8ca23f94Schristos 	sc->sc_lcd.sc_flags = HD_8BIT | HD_MULTILINE | HD_KEYPAD;
163*8ca23f94Schristos 
164*8ca23f94Schristos 	sc->sc_lcd.sc_writereg = lcdpanel_cbt_hdwritereg;
165*8ca23f94Schristos 	sc->sc_lcd.sc_readreg = lcdpanel_cbt_hdreadreg;
166*8ca23f94Schristos 
167*8ca23f94Schristos 	hd44780_attach_subr(&sc->sc_lcd);
168*8ca23f94Schristos 
169*8ca23f94Schristos 	/* Hello World */
170*8ca23f94Schristos 	io.dat = 0;
171*8ca23f94Schristos 	io.len = LCDPANEL_VCOLS * LCDPANEL_ROWS;
172*8ca23f94Schristos 	memcpy(io.buf, &startup_message, io.len);
173*8ca23f94Schristos 	hd44780_ddram_io(&sc->sc_lcd, sc->sc_lcd.sc_curchip, &io,
174*8ca23f94Schristos 	    HD_DDRAM_WRITE);
175*8ca23f94Schristos 
176*8ca23f94Schristos 	pmf_device_register1(self, NULL, NULL, lcdpanel_shutdown);
177*8ca23f94Schristos 
178*8ca23f94Schristos 	sc->sc_kp.sc_iot = maa->ma_iot;
179*8ca23f94Schristos 	sc->sc_kp.sc_ioh = MIPS_PHYS_TO_KSEG1(LCDPANEL_BASE); /* XXX */
180*8ca23f94Schristos 
181*8ca23f94Schristos 	sc->sc_kp.sc_knum = sizeof(keys) / sizeof(struct lcdkp_xlate);
182*8ca23f94Schristos 	sc->sc_kp.sc_kpad = keys;
183*8ca23f94Schristos 	sc->sc_kp.sc_rread = lcdpanel_cbt_kprread;
184*8ca23f94Schristos 
185*8ca23f94Schristos 	lcdkp_attach_subr(&sc->sc_kp);
186*8ca23f94Schristos 
187*8ca23f94Schristos 	callout_init(&sc->sc_callout, 0);
188*8ca23f94Schristos 	selinit(&sc->sc_selq);
189*8ca23f94Schristos }
190*8ca23f94Schristos 
191*8ca23f94Schristos static bool
lcdpanel_shutdown(device_t self,int howto)192*8ca23f94Schristos lcdpanel_shutdown(device_t self, int howto)
193*8ca23f94Schristos {
194*8ca23f94Schristos 	struct lcdpanel_softc *sc = device_private(self);
195*8ca23f94Schristos 	struct hd44780_io io;
196*8ca23f94Schristos 
197*8ca23f94Schristos 	/* Goodbye World */
198*8ca23f94Schristos 	io.dat = 0;
199*8ca23f94Schristos 	io.len = LCDPANEL_VCOLS * LCDPANEL_ROWS;
200*8ca23f94Schristos 	if (howto & RB_HALT)
201*8ca23f94Schristos 		memcpy(io.buf, &halt_message, io.len);
202*8ca23f94Schristos 	else
203*8ca23f94Schristos 		memcpy(io.buf, &reboot_message, io.len);
204*8ca23f94Schristos 	hd44780_ddram_io(&sc->sc_lcd, sc->sc_lcd.sc_curchip, &io,
205*8ca23f94Schristos 	    HD_DDRAM_WRITE);
206*8ca23f94Schristos 
207*8ca23f94Schristos 	return true;
208*8ca23f94Schristos }
209*8ca23f94Schristos 
210*8ca23f94Schristos static uint8_t
lcdpanel_cbt_kprread(bus_space_tag_t iot,bus_space_handle_t ioh)211*8ca23f94Schristos lcdpanel_cbt_kprread(bus_space_tag_t iot, bus_space_handle_t ioh)
212*8ca23f94Schristos {
213*8ca23f94Schristos 
214*8ca23f94Schristos 	delay(HD_TIMEOUT_NORMAL);
215*8ca23f94Schristos 	return (bus_space_read_4(iot, ioh, 0x00) >> 24) & 0xff;
216*8ca23f94Schristos }
217*8ca23f94Schristos 
218*8ca23f94Schristos 
219*8ca23f94Schristos static void
lcdpanel_cbt_hdwritereg(struct hd44780_chip * hd,uint32_t en,uint32_t rs,uint8_t dat)220*8ca23f94Schristos lcdpanel_cbt_hdwritereg(struct hd44780_chip *hd, uint32_t en, uint32_t rs,
221*8ca23f94Schristos     uint8_t dat)
222*8ca23f94Schristos {
223*8ca23f94Schristos 
224*8ca23f94Schristos 	if (rs)
225*8ca23f94Schristos 		bus_space_write_4(hd->sc_iot, hd->sc_iodr, 0x00, dat << 24);
226*8ca23f94Schristos 	else
227*8ca23f94Schristos 		bus_space_write_4(hd->sc_iot, hd->sc_ioir, 0x00, dat << 24);
228*8ca23f94Schristos 	delay(HD_TIMEOUT_NORMAL);
229*8ca23f94Schristos }
230*8ca23f94Schristos 
231*8ca23f94Schristos static uint8_t
lcdpanel_cbt_hdreadreg(struct hd44780_chip * hd,uint32_t en,uint32_t rs)232*8ca23f94Schristos lcdpanel_cbt_hdreadreg(struct hd44780_chip *hd, uint32_t en, uint32_t rs)
233*8ca23f94Schristos {
234*8ca23f94Schristos 
235*8ca23f94Schristos 	delay(HD_TIMEOUT_NORMAL);
236*8ca23f94Schristos 	if (rs)
237*8ca23f94Schristos 		return (bus_space_read_4(hd->sc_iot, hd->sc_iodr, 0x00) >> 24)
238*8ca23f94Schristos 		    & 0xff;
239*8ca23f94Schristos 	else
240*8ca23f94Schristos 		return (bus_space_read_4(hd->sc_iot, hd->sc_ioir, 0x00) >> 24)
241*8ca23f94Schristos 		    & 0xff;
242*8ca23f94Schristos }
243*8ca23f94Schristos 
244*8ca23f94Schristos int
lcdpanelopen(dev_t dev,int flag,int mode,struct lwp * l)245*8ca23f94Schristos lcdpanelopen(dev_t dev, int flag, int mode, struct lwp *l)
246*8ca23f94Schristos {
247*8ca23f94Schristos 	struct lcdpanel_softc *sc = device_lookup_private(&lcdpanel_cd, minor(dev));
248*8ca23f94Schristos 
249*8ca23f94Schristos 	return (sc->sc_lcd.sc_dev_ok == 0) ? ENXIO : 0;
250*8ca23f94Schristos }
251*8ca23f94Schristos 
252*8ca23f94Schristos int
lcdpanelclose(dev_t dev,int flag,int mode,struct lwp * l)253*8ca23f94Schristos lcdpanelclose(dev_t dev, int flag, int mode, struct lwp *l)
254*8ca23f94Schristos {
255*8ca23f94Schristos 	struct lcdpanel_softc *sc = device_lookup_private(&lcdpanel_cd, minor(dev));
256*8ca23f94Schristos 
257*8ca23f94Schristos 	selnotify(&sc->sc_selq, 0, 0);
258*8ca23f94Schristos 	return 0;
259*8ca23f94Schristos }
260*8ca23f94Schristos 
261*8ca23f94Schristos int
lcdpanelread(dev_t dev,struct uio * uio,int flag)262*8ca23f94Schristos lcdpanelread(dev_t dev, struct uio *uio, int flag)
263*8ca23f94Schristos {
264*8ca23f94Schristos 	int error;
265*8ca23f94Schristos 	uint8_t b;
266*8ca23f94Schristos 	struct lcdpanel_softc *sc = device_lookup_private(&lcdpanel_cd, minor(dev));
267*8ca23f94Schristos 
268*8ca23f94Schristos 	if (uio->uio_resid < sizeof(b))
269*8ca23f94Schristos 		return EIO;
270*8ca23f94Schristos 
271*8ca23f94Schristos 	if ((error = lcdkp_readkey(&sc->sc_kp, &b)) != 0)
272*8ca23f94Schristos 		return error;
273*8ca23f94Schristos 
274*8ca23f94Schristos 	return uiomove((void*)&b, sizeof(b), uio);
275*8ca23f94Schristos }
276*8ca23f94Schristos 
277*8ca23f94Schristos int
lcdpanelwrite(dev_t dev,struct uio * uio,int flag)278*8ca23f94Schristos lcdpanelwrite(dev_t dev, struct uio *uio, int flag)
279*8ca23f94Schristos {
280*8ca23f94Schristos 	int error;
281*8ca23f94Schristos 	struct hd44780_io io;
282*8ca23f94Schristos 	struct lcdpanel_softc *sc = device_lookup_private(&lcdpanel_cd, minor(dev));
283*8ca23f94Schristos 
284*8ca23f94Schristos 	io.dat = 0;
285*8ca23f94Schristos 	io.len = uio->uio_resid;
286*8ca23f94Schristos 	if (io.len > HD_MAX_CHARS)
287*8ca23f94Schristos 		io.len = HD_MAX_CHARS;
288*8ca23f94Schristos 
289*8ca23f94Schristos 	if ((error = uiomove((void*)io.buf, io.len, uio)) != 0)
290*8ca23f94Schristos 		return error;
291*8ca23f94Schristos 
292*8ca23f94Schristos 	hd44780_ddram_redraw(&sc->sc_lcd, 0, &io);
293*8ca23f94Schristos 	return 0;
294*8ca23f94Schristos }
295*8ca23f94Schristos 
296*8ca23f94Schristos int
lcdpanelioctl(dev_t dev,u_long cmd,void * data,int flag,struct lwp * l)297*8ca23f94Schristos lcdpanelioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
298*8ca23f94Schristos {
299*8ca23f94Schristos 	struct lcdpanel_softc *sc = device_lookup_private(&lcdpanel_cd, minor(dev));
300*8ca23f94Schristos 
301*8ca23f94Schristos 	return hd44780_ioctl_subr(&sc->sc_lcd, cmd, data);
302*8ca23f94Schristos }
303*8ca23f94Schristos 
304*8ca23f94Schristos int
lcdpanelpoll(dev_t dev,int events,struct lwp * l)305*8ca23f94Schristos lcdpanelpoll(dev_t dev, int events, struct lwp *l)
306*8ca23f94Schristos {
307*8ca23f94Schristos 	int revents = 0;
308*8ca23f94Schristos 
309*8ca23f94Schristos 	if ((events & (POLLIN | POLLRDNORM)) != 0) {
310*8ca23f94Schristos 		struct lcdpanel_softc *sc;
311*8ca23f94Schristos 
312*8ca23f94Schristos 		sc = device_lookup_private(&lcdpanel_cd, minor(dev));
313*8ca23f94Schristos 		if (lcdkp_scankey(&sc->sc_kp) != 0) {
314*8ca23f94Schristos 			revents = events & (POLLIN | POLLRDNORM);
315*8ca23f94Schristos 		} else {
316*8ca23f94Schristos 			selrecord(l, &sc->sc_selq);
317*8ca23f94Schristos 			callout_reset(&sc->sc_callout, LCDPANEL_POLLRATE,
318*8ca23f94Schristos 					lcdpanel_soft, sc);
319*8ca23f94Schristos 		}
320*8ca23f94Schristos 	}
321*8ca23f94Schristos 
322*8ca23f94Schristos 	return revents;
323*8ca23f94Schristos }
324*8ca23f94Schristos 
325*8ca23f94Schristos static void
lcdpanel_soft(void * arg)326*8ca23f94Schristos lcdpanel_soft(void *arg)
327*8ca23f94Schristos {
328*8ca23f94Schristos 	struct lcdpanel_softc *sc = arg;
329*8ca23f94Schristos 
330*8ca23f94Schristos 	if (lcdkp_scankey(&sc->sc_kp) != 0)
331*8ca23f94Schristos 		selnotify(&sc->sc_selq, 0, 0);
332*8ca23f94Schristos 	else
333*8ca23f94Schristos 		callout_reset(&sc->sc_callout, LCDPANEL_POLLRATE, lcdpanel_soft, sc);
334*8ca23f94Schristos }
335