xref: /netbsd-src/sys/arch/hpcmips/dev/optpoint.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: optpoint.c,v 1.7 2012/10/27 17:17:52 chs Exp $ */
2 
3 /*-
4  * Copyright (c) 2005 HAMAJIMA Katsuomi. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /*
29  * OptOpint on Telios HC-AJ2
30  */
31 
32 #include <sys/cdefs.h>
33 
34 __KERNEL_RCSID(0, "$NetBSD: optpoint.c,v 1.7 2012/10/27 17:17:52 chs Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 #include <machine/bus.h>
40 #include <machine/config_hook.h>
41 #include <dev/hpc/hpciovar.h>
42 #include <dev/wscons/wsconsio.h>
43 #include <dev/wscons/wsmousevar.h>
44 #include <hpcmips/tx/tx39var.h>
45 #include <hpcmips/tx/tx39spivar.h>
46 #include <hpcmips/tx/tx39icureg.h>
47 
48 #undef	OPTPOINTDEBUG
49 
50 #ifdef OPTPOINTDEBUG
51 #define	DPRINTF(arg) printf arg
52 #else
53 #define	DPRINTF(arg)
54 #endif
55 
56 struct optpoint_softc {
57 	device_t sc_dev;
58 	tx_chipset_tag_t sc_tc;
59 	struct tx39spi_softc *sc_spi;
60 	struct hpcio_chip *sc_hc;
61 	device_t sc_wsmousedev;
62 	void *sc_powerhook;	/* power management hook */
63 	char packet[4];
64 	int index;	/* number of bytes received for this packet */
65 	u_int buttons;	/* mouse button status */
66 	int enabled;
67 };
68 
69 static int optpoint_match(device_t, cfdata_t, void *);
70 static void optpoint_attach(device_t, device_t, void *);
71 static int optpoint_intr(void *);
72 static int optpoint_enable(void *);
73 static void optpoint_disable(void *);
74 static int optpoint_ioctl(void *, u_long, void *, int, struct lwp *);
75 static int optpoint_initialize(void *);
76 static void optpoint_send(struct optpoint_softc *, int);
77 static int optpoint_recv(struct optpoint_softc *);
78 static int optpoint_power(void *, int, long, void *);
79 
80 #define LBUTMASK 0x01
81 #define RBUTMASK 0x02
82 
83 #define TX39_INTRSTATUS4_OPTPOINTINT	TX39_INTRSTATUS4_CARDIORDNEGINT
84 #define TX39_IO_MFIO_CARDREG	11
85 #define TX39_IO_MFIO_CARDIOWR	10
86 #define TX39_IO_MFIO_CARDIORD	9
87 #define TELIOS_MFIO_OPTP_T_RDY		TX39_IO_MFIO_CARDREG
88 #define TELIOS_MFIO_OPTP_C_REQ		TX39_IO_MFIO_CARDIOWR
89 #define TELIOS_MFIO_OPTP_S_ENB_N	TX39_IO_MFIO_CARDIORD
90 
91 CFATTACH_DECL_NEW(optpoint, sizeof(struct optpoint_softc),
92     optpoint_match, optpoint_attach, NULL, NULL);
93 
94 const struct wsmouse_accessops optpoint_accessops = {
95 	optpoint_enable,
96 	optpoint_ioctl,
97 	optpoint_disable,
98 };
99 
100 int
101 optpoint_match(device_t parent, cfdata_t cf, void *aux)
102 {
103 	return (ATTACH_NORMAL);
104 }
105 
106 void
107 optpoint_attach(device_t parent, device_t self, void *aux)
108 {
109 	struct txspi_attach_args *ta = aux;
110 	struct optpoint_softc *sc = device_private(self);
111 	struct tx39spi_softc *spi = sc->sc_spi = device_private(parent);
112 	tx_chipset_tag_t tc = sc->sc_tc = ta->sa_tc;
113 	struct wsmousedev_attach_args wsmaa;
114 
115 	sc->sc_dev = self;
116 	sc->sc_hc = tc->tc_iochip[MFIO];
117 	sc->enabled = 0;
118 
119 	/* Specific SPI settings for OptPoint of HC-AJ2 */
120 	tx39spi_delayval(spi, 0);
121 	tx39spi_baudrate(spi, 4);	/* SPICLK Rate = 737.3 kHz */
122 	tx39spi_word(spi, 0);		/* Use 8bits of data */
123 	tx39spi_phapol(spi, 0);
124 	tx39spi_clkpol(spi, 1);
125 	tx39spi_lsb(spi, 0);		/* MSB first */
126 
127 	optpoint_enable(sc);
128 	printf("\n");
129 
130 	wsmaa.accessops = &optpoint_accessops;
131 	wsmaa.accesscookie = sc;
132 	/* attach the wsmouse */
133 	sc->sc_wsmousedev = config_found(self, &wsmaa, wsmousedevprint);
134 
135 	/* Add a hard power hook to power saving */
136 	sc->sc_powerhook = config_hook(CONFIG_HOOK_PMEVENT,
137 				       CONFIG_HOOK_PMEVENT_HARDPOWER,
138 				       CONFIG_HOOK_SHARE,
139 				       optpoint_power, sc);
140 #ifdef DIAGNOSTIC
141 	if (sc->sc_powerhook == 0)
142 		printf("%s: unable to establish hard power hook",
143 		       device_xname(sc->sc_dev));
144 #endif
145 }
146 
147 int
148 optpoint_intr(void *arg)
149 {
150 	struct optpoint_softc *sc = arg;
151 	tx_chipset_tag_t tc = sc->sc_tc;
152 	char data = optpoint_recv(sc) & 0xff;
153 
154 #ifdef DIAGNOSTIC
155 	if (sc->index >= 3){
156 		printf("%s: Receive buffer overflow\n", device_xname(sc->sc_dev));
157 		sc->index = 0;
158 		memset(sc->packet, 0, 3);
159 	}
160 #endif
161 	if ((sc->index == 1) && (data & 0xcc) != 0x08){
162 		DPRINTF(("%s: Bad second byte (0x%02x)\n",
163 			 device_xname(sc->sc_dev), data));
164 		tx_conf_write(tc, TX39_INTRCLEAR4_REG,
165 				  TX39_INTRSTATUS4_OPTPOINTINT);
166 		return 0;
167 	}
168 
169 	sc->packet[sc->index++] = data;
170 	if (sc->index >= 3){
171 		u_int newbuttons = ((sc->packet[1] & LBUTMASK) ? 0x1 : 0)
172 				 | ((sc->packet[1] & RBUTMASK) ? 0x2 : 0);
173 		int dx = sc->packet[2];
174 		int dy = sc->packet[0];
175 		u_int changed = (sc->buttons ^ newbuttons);
176 
177 		if (dx || dy || changed){
178 			DPRINTF(("%s: buttons=0x%x, dx=%d, dy=%d\n",
179 				 device_xname(sc->sc_dev), newbuttons, dx, dy));
180 			wsmouse_input(sc->sc_wsmousedev,
181 					newbuttons,
182 					dx, dy, 0, 0,
183 					WSMOUSE_INPUT_DELTA);
184 		}
185 		sc->buttons = newbuttons;
186 		sc->index = 0;
187 		memset(sc->packet, 0, 3);
188 	}
189 	tx_conf_write(tc, TX39_INTRCLEAR4_REG, TX39_INTRSTATUS4_OPTPOINTINT);
190 
191 	return 0;
192 }
193 
194 int
195 optpoint_enable(void *arg)
196 {
197 	struct optpoint_softc *sc = arg;
198 
199 	if (!sc->enabled){
200 		tx_chipset_tag_t tc = sc->sc_tc;
201 		struct hpcio_chip *hc = sc->sc_hc;
202 		int s = spltty();
203 
204 		DPRINTF(("%s: enable\n", device_xname(sc->sc_dev)));
205 
206 		sc->enabled = 1;
207 		sc->index = 0;
208 		sc->buttons = 0;
209 		sc->packet[0] = 0xf5;	/* Disable */
210 		sc->packet[1] = 0xf2;	/* Set Stream Mode */
211 		sc->packet[2] = 0xf8;	/* Stanby */
212 		sc->packet[3] = 0xf4;	/* Enable */
213 
214 		tx39spi_enable(sc->sc_spi, 1);
215 		tx_intr_establish(tc, MAKEINTR(4, TX39_INTRSTATUS4_OPTPOINTINT),
216 				  IST_EDGE, IPL_TTY, optpoint_initialize, sc);
217 		(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_C_REQ, 1);
218 		(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_T_RDY, 1);
219 		splx(s);
220 	}
221 
222         return 0;
223 }
224 
225 void
226 optpoint_disable(void *arg)
227 {
228 	struct optpoint_softc *sc = arg;
229 
230 	if (sc->enabled){
231 		tx_chipset_tag_t tc = sc->sc_tc;
232 		struct hpcio_chip *hc = sc->sc_hc;
233 		int s = spltty();
234 
235 		DPRINTF(("%s: disable\n", device_xname(sc->sc_dev)));
236 
237 		sc->enabled = 0;
238 		(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_C_REQ, 0);
239 		(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_T_RDY, 0);
240 		tx_intr_disestablish(tc,
241 			    (void *)MAKEINTR(4, TX39_INTRSTATUS4_OPTPOINTINT));
242 		tx39spi_enable(sc->sc_spi, 0);
243 		splx(s);
244 	}
245 }
246 
247 int
248 optpoint_ioctl(void *cookie, u_long cmd, void *data, int flag, struct lwp *l)
249 {
250 	switch (cmd) {
251 	case WSMOUSEIO_GTYPE:
252 		*(u_int *)data = WSMOUSE_TYPE_PS2;
253 		break;
254 
255 	default:
256 		return (EPASSTHROUGH);
257 	}
258         return 0;
259 }
260 
261 int
262 optpoint_initialize(void *arg)
263 {
264 	struct optpoint_softc *sc = arg;
265 	struct hpcio_chip *hc = sc->sc_hc;
266 	tx_chipset_tag_t tc = sc->sc_tc;
267 
268 	if (sc->index < 4){
269 		optpoint_send(sc, sc->packet[sc->index++]);
270 		(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_C_REQ, 1);
271 		(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_T_RDY, 1);
272 	} else {
273 		tx_intr_disestablish(tc,
274 			    (void *)MAKEINTR(4, TX39_INTRSTATUS4_OPTPOINTINT));
275 		sc->index = 0;
276 		memset(sc->packet, 0, 3);
277 		tx_intr_establish(tc,
278 				  MAKEINTR(4, TX39_INTRSTATUS4_OPTPOINTINT),
279 				  IST_EDGE, IPL_TTY, optpoint_intr, sc);
280 		(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_C_REQ, 0);
281 		(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_T_RDY, 1);
282 	}
283 	tx_conf_write(tc, TX39_INTRCLEAR4_REG, TX39_INTRSTATUS4_OPTPOINTINT);
284 
285 	return 0;
286 }
287 
288 void
289 optpoint_send(struct optpoint_softc *sc, int data)
290 {
291 	struct hpcio_chip *hc = sc->sc_hc;
292 
293 	while ((*hc->hc_portread)(hc, TELIOS_MFIO_OPTP_S_ENB_N))
294 		;
295 	tx39spi_put_word(sc->sc_spi, data & 0xff);
296 }
297 
298 int
299 optpoint_recv(struct optpoint_softc *sc)
300 {
301 	struct hpcio_chip *hc = sc->sc_hc;
302 
303 	optpoint_send(sc, 0x00);
304 	while ((*hc->hc_portread)(hc, TELIOS_MFIO_OPTP_S_ENB_N))
305 		;
306 	return tx39spi_get_word(sc->sc_spi) & 0xff;
307 }
308 
309 int
310 optpoint_power(void *arg, int type, long id, void *msg)
311 {
312 	struct optpoint_softc *sc = arg;
313 	int why = (int)msg;
314 
315 	switch (why) {
316 	case PWR_RESUME:
317 		/* power on */
318 		optpoint_enable(sc);
319 		break;
320 	case PWR_SUSPEND:
321 		/* FALLTHROUGH */
322 	case PWR_STANDBY:
323 		/* power off */
324 		optpoint_disable(sc);
325 		break;
326 	}
327 
328 	return 0;
329 }
330