s3c2440_touch.c revision 1.1.4.2 1 1.1.4.2 mrg /*-
2 1.1.4.2 mrg * Copyright (c) 2012 The NetBSD Foundation, Inc.
3 1.1.4.2 mrg * All rights reserved.
4 1.1.4.2 mrg *
5 1.1.4.2 mrg * This code is derived from software contributed to The NetBSD Foundation
6 1.1.4.2 mrg * by Paul Fleischer <paul (at) xpg.dk>
7 1.1.4.2 mrg *
8 1.1.4.2 mrg * Redistribution and use in source and binary forms, with or without
9 1.1.4.2 mrg * modification, are permitted provided that the following conditions
10 1.1.4.2 mrg * are met:
11 1.1.4.2 mrg * 1. Redistributions of source code must retain the above copyright
12 1.1.4.2 mrg * notice, this list of conditions and the following disclaimer.
13 1.1.4.2 mrg * 2. Redistributions in binary form must reproduce the above copyright
14 1.1.4.2 mrg * notice, this list of conditions and the following disclaimer in the
15 1.1.4.2 mrg * documentation and/or other materials provided with the distribution.
16 1.1.4.2 mrg *
17 1.1.4.2 mrg * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 1.1.4.2 mrg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 1.1.4.2 mrg * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 1.1.4.2 mrg * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 1.1.4.2 mrg * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 1.1.4.2 mrg * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 1.1.4.2 mrg * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.1.4.2 mrg * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 1.1.4.2 mrg * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 1.1.4.2 mrg * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 1.1.4.2 mrg * POSSIBILITY OF SUCH DAMAGE.
28 1.1.4.2 mrg */
29 1.1.4.2 mrg #include <sys/cdefs.h>
30 1.1.4.2 mrg
31 1.1.4.2 mrg #include <sys/param.h>
32 1.1.4.2 mrg #include <sys/systm.h>
33 1.1.4.2 mrg #include <sys/conf.h>
34 1.1.4.2 mrg #include <sys/callout.h>
35 1.1.4.2 mrg #include <sys/kernel.h>
36 1.1.4.2 mrg
37 1.1.4.2 mrg #include <sys/bus.h>
38 1.1.4.2 mrg
39 1.1.4.2 mrg #include <arm/s3c2xx0/s3c24x0var.h>
40 1.1.4.2 mrg #include <arm/s3c2xx0/s3c2440var.h>
41 1.1.4.2 mrg #include <arm/s3c2xx0/s3c2440reg.h>
42 1.1.4.2 mrg
43 1.1.4.2 mrg #include <dev/wscons/wsconsio.h>
44 1.1.4.2 mrg #include <dev/wscons/wsmousevar.h>
45 1.1.4.2 mrg #include <dev/wscons/tpcalibvar.h>
46 1.1.4.2 mrg
47 1.1.4.2 mrg #include <lib/libsa/qsort.c>
48 1.1.4.2 mrg
49 1.1.4.2 mrg #include <dev/hpc/hpcfbio.h>
50 1.1.4.2 mrg
51 1.1.4.2 mrg #define MAX_SAMPLES 20
52 1.1.4.2 mrg
53 1.1.4.2 mrg struct sstouch_softc {
54 1.1.4.2 mrg device_t dev;
55 1.1.4.2 mrg
56 1.1.4.2 mrg bus_space_tag_t iot;
57 1.1.4.2 mrg bus_space_handle_t ioh;
58 1.1.4.2 mrg
59 1.1.4.2 mrg uint32_t next_stylus_intr;
60 1.1.4.2 mrg
61 1.1.4.2 mrg device_t wsmousedev;
62 1.1.4.2 mrg
63 1.1.4.2 mrg struct tpcalib_softc tpcalib;
64 1.1.4.2 mrg
65 1.1.4.2 mrg int sample_count;
66 1.1.4.2 mrg int samples_x[MAX_SAMPLES];
67 1.1.4.2 mrg int samples_y[MAX_SAMPLES];
68 1.1.4.2 mrg
69 1.1.4.2 mrg callout_t callout;
70 1.1.4.2 mrg };
71 1.1.4.2 mrg
72 1.1.4.2 mrg /* Basic Driver Stuff */
73 1.1.4.2 mrg static int sstouch_match (struct device *, struct cfdata *, void *);
74 1.1.4.2 mrg static void sstouch_attach (struct device *, struct device *, void *);
75 1.1.4.2 mrg
76 1.1.4.2 mrg CFATTACH_DECL_NEW(sstouch, sizeof(struct sstouch_softc), sstouch_match,
77 1.1.4.2 mrg sstouch_attach, NULL, NULL);
78 1.1.4.2 mrg
79 1.1.4.2 mrg /* wsmousedev */
80 1.1.4.2 mrg int sstouch_enable(void *);
81 1.1.4.2 mrg int sstouch_ioctl(void *, u_long, void *, int, struct lwp *);
82 1.1.4.2 mrg void sstouch_disable(void *);
83 1.1.4.2 mrg
84 1.1.4.2 mrg const struct wsmouse_accessops sstouch_accessops = {
85 1.1.4.2 mrg sstouch_enable,
86 1.1.4.2 mrg sstouch_ioctl,
87 1.1.4.2 mrg sstouch_disable
88 1.1.4.2 mrg };
89 1.1.4.2 mrg
90 1.1.4.2 mrg /* Interrupt Handlers */
91 1.1.4.2 mrg int sstouch_tc_intr(void *arg);
92 1.1.4.2 mrg int sstouch_adc_intr(void *arg);
93 1.1.4.2 mrg
94 1.1.4.2 mrg void sstouch_callout(void *arg);
95 1.1.4.2 mrg int sstouch_filter_values(int *vals, int val_count);
96 1.1.4.2 mrg void sstouch_initialize(struct sstouch_softc *sc);
97 1.1.4.2 mrg
98 1.1.4.2 mrg #define STYLUS_DOWN 0
99 1.1.4.2 mrg #define STYLUS_UP ADCTSC_UD_SEN
100 1.1.4.2 mrg
101 1.1.4.2 mrg static struct wsmouse_calibcoords default_calib = {
102 1.1.4.2 mrg .minx = 0,
103 1.1.4.2 mrg .miny = 0,
104 1.1.4.2 mrg .maxx = 0,
105 1.1.4.2 mrg .maxy = 0,
106 1.1.4.2 mrg .samplelen = WSMOUSE_CALIBCOORDS_RESET
107 1.1.4.2 mrg };
108 1.1.4.2 mrg
109 1.1.4.2 mrg /* IMPLEMENTATION PART */
110 1.1.4.2 mrg int
111 1.1.4.2 mrg sstouch_match(struct device *parent, struct cfdata *match, void *aux)
112 1.1.4.2 mrg {
113 1.1.4.2 mrg /* XXX: Check CPU type? */
114 1.1.4.2 mrg return 1;
115 1.1.4.2 mrg }
116 1.1.4.2 mrg
117 1.1.4.2 mrg void
118 1.1.4.2 mrg sstouch_attach(struct device *parent,
119 1.1.4.2 mrg struct device *self,
120 1.1.4.2 mrg void *aux)
121 1.1.4.2 mrg {
122 1.1.4.2 mrg struct sstouch_softc *sc = device_private(self);
123 1.1.4.2 mrg struct s3c2xx0_attach_args *sa = (struct s3c2xx0_attach_args*)aux;
124 1.1.4.2 mrg struct wsmousedev_attach_args mas;
125 1.1.4.2 mrg
126 1.1.4.2 mrg sc->dev = self;
127 1.1.4.2 mrg sc->iot = sa->sa_iot;
128 1.1.4.2 mrg
129 1.1.4.2 mrg if (bus_space_map(sc->iot, S3C2440_ADC_BASE,
130 1.1.4.2 mrg S3C2440_ADC_SIZE, 0, &sc->ioh)) {
131 1.1.4.2 mrg aprint_error(": failed to map registers");
132 1.1.4.2 mrg return;
133 1.1.4.2 mrg }
134 1.1.4.2 mrg
135 1.1.4.2 mrg sc->next_stylus_intr = STYLUS_DOWN;
136 1.1.4.2 mrg
137 1.1.4.2 mrg
138 1.1.4.2 mrg /* XXX: Is IPL correct? */
139 1.1.4.2 mrg s3c24x0_intr_establish(S3C2440_INT_TC, IPL_BIO, IST_EDGE_RISING,
140 1.1.4.2 mrg sstouch_tc_intr, sc);
141 1.1.4.2 mrg s3c24x0_intr_establish(S3C2440_INT_ADC, IPL_BIO, IST_EDGE_RISING,
142 1.1.4.2 mrg sstouch_adc_intr, sc);
143 1.1.4.2 mrg
144 1.1.4.2 mrg aprint_normal("\n");
145 1.1.4.2 mrg
146 1.1.4.2 mrg mas.accessops = &sstouch_accessops;
147 1.1.4.2 mrg mas.accesscookie = sc;
148 1.1.4.2 mrg
149 1.1.4.2 mrg sc->wsmousedev = config_found_ia(self, "wsmousedev", &mas,
150 1.1.4.2 mrg wsmousedevprint);
151 1.1.4.2 mrg
152 1.1.4.2 mrg tpcalib_init(&sc->tpcalib);
153 1.1.4.2 mrg tpcalib_ioctl(&sc->tpcalib, WSMOUSEIO_SCALIBCOORDS,
154 1.1.4.2 mrg (void*)&default_calib, 0, 0);
155 1.1.4.2 mrg
156 1.1.4.2 mrg sc->sample_count = 0;
157 1.1.4.2 mrg
158 1.1.4.2 mrg /* Add CALLOUT_MPSAFE to avoid holding the global kernel lock */
159 1.1.4.2 mrg callout_init(&sc->callout, 0);
160 1.1.4.2 mrg callout_setfunc(&sc->callout, sstouch_callout, sc);
161 1.1.4.2 mrg
162 1.1.4.2 mrg /* Actual initialization is performed by sstouch_initialize(),
163 1.1.4.2 mrg which is called by sstouch_enable() */
164 1.1.4.2 mrg }
165 1.1.4.2 mrg
166 1.1.4.2 mrg /* sstouch_tc_intr is the TC interrupt handler.
167 1.1.4.2 mrg The TC interrupt is generated when the stylus changes up->down,
168 1.1.4.2 mrg or down->up state (depending on configuration of ADC_ADCTSC).
169 1.1.4.2 mrg */
170 1.1.4.2 mrg int
171 1.1.4.2 mrg sstouch_tc_intr(void *arg)
172 1.1.4.2 mrg {
173 1.1.4.2 mrg struct sstouch_softc *sc = (struct sstouch_softc*)arg;
174 1.1.4.2 mrg uint32_t reg;
175 1.1.4.2 mrg
176 1.1.4.2 mrg /*aprint_normal("%s\n", __func__);*/
177 1.1.4.2 mrg
178 1.1.4.2 mrg /* Figure out if the stylus was lifted or lowered */
179 1.1.4.2 mrg reg = bus_space_read_4(sc->iot, sc->ioh, ADC_ADCUPDN);
180 1.1.4.2 mrg bus_space_write_4(sc->iot, sc->ioh, ADC_ADCUPDN, 0x0);
181 1.1.4.2 mrg if( sc->next_stylus_intr == STYLUS_DOWN && (reg & ADCUPDN_TSC_DN) ) {
182 1.1.4.2 mrg sc->next_stylus_intr = STYLUS_UP;
183 1.1.4.2 mrg
184 1.1.4.2 mrg sstouch_callout(sc);
185 1.1.4.2 mrg
186 1.1.4.2 mrg } else if (sc->next_stylus_intr == STYLUS_UP && (reg & ADCUPDN_TSC_UP)) {
187 1.1.4.2 mrg uint32_t adctsc = 0;
188 1.1.4.2 mrg sc->next_stylus_intr = STYLUS_DOWN;
189 1.1.4.2 mrg
190 1.1.4.2 mrg wsmouse_input(sc->wsmousedev, 0x0, 0, 0, 0, 0, 0);
191 1.1.4.2 mrg
192 1.1.4.2 mrg sc->sample_count = 0;
193 1.1.4.2 mrg
194 1.1.4.2 mrg adctsc |= ADCTSC_YM_SEN | ADCTSC_YP_SEN | ADCTSC_XP_SEN |
195 1.1.4.2 mrg sc->next_stylus_intr |
196 1.1.4.2 mrg 3; /* 3 selects "Waiting for Interrupt Mode" */
197 1.1.4.2 mrg bus_space_write_4(sc->iot, sc->ioh, ADC_ADCTSC, adctsc);
198 1.1.4.2 mrg }
199 1.1.4.2 mrg
200 1.1.4.2 mrg return 1;
201 1.1.4.2 mrg }
202 1.1.4.2 mrg
203 1.1.4.2 mrg /* sstouch_adc_intr is ADC interrupt handler.
204 1.1.4.2 mrg ADC interrupt is triggered when the ADC controller has a measurement ready.
205 1.1.4.2 mrg */
206 1.1.4.2 mrg int
207 1.1.4.2 mrg sstouch_adc_intr(void *arg)
208 1.1.4.2 mrg {
209 1.1.4.2 mrg struct sstouch_softc *sc = (struct sstouch_softc*)arg;
210 1.1.4.2 mrg uint32_t reg;
211 1.1.4.2 mrg uint32_t adctsc = 0;
212 1.1.4.2 mrg int x, y;
213 1.1.4.2 mrg
214 1.1.4.2 mrg reg = bus_space_read_4(sc->iot, sc->ioh, ADC_ADCDAT0);
215 1.1.4.2 mrg y = reg & ADCDAT_DATAMASK;
216 1.1.4.2 mrg
217 1.1.4.2 mrg reg = bus_space_read_4(sc->iot, sc->ioh, ADC_ADCDAT1);
218 1.1.4.2 mrg x = reg & ADCDAT_DATAMASK;
219 1.1.4.2 mrg
220 1.1.4.2 mrg
221 1.1.4.2 mrg sc->samples_x[sc->sample_count] = x;
222 1.1.4.2 mrg sc->samples_y[sc->sample_count] = y;
223 1.1.4.2 mrg
224 1.1.4.2 mrg sc->sample_count++;
225 1.1.4.2 mrg
226 1.1.4.2 mrg x = sstouch_filter_values(sc->samples_x, sc->sample_count);
227 1.1.4.2 mrg y = sstouch_filter_values(sc->samples_y, sc->sample_count);
228 1.1.4.2 mrg
229 1.1.4.2 mrg if (x == -1 || y == -1) {
230 1.1.4.2 mrg /* If we do not have enough measurements, make some more. */
231 1.1.4.2 mrg sstouch_callout(sc);
232 1.1.4.2 mrg return 1;
233 1.1.4.2 mrg }
234 1.1.4.2 mrg
235 1.1.4.2 mrg sc->sample_count = 0;
236 1.1.4.2 mrg
237 1.1.4.2 mrg tpcalib_trans(&sc->tpcalib, x, y, &x, &y);
238 1.1.4.2 mrg
239 1.1.4.2 mrg wsmouse_input(sc->wsmousedev, 0x1, x, y, 0, 0,
240 1.1.4.2 mrg WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
241 1.1.4.2 mrg
242 1.1.4.2 mrg /* Schedule a new adc measurement, unless the stylus has been lifed */
243 1.1.4.2 mrg if (sc->next_stylus_intr == STYLUS_UP) {
244 1.1.4.2 mrg callout_schedule(&sc->callout, hz/50);
245 1.1.4.2 mrg }
246 1.1.4.2 mrg
247 1.1.4.2 mrg /* Until measurement is to be performed, listen for stylus up-events */
248 1.1.4.2 mrg adctsc |= ADCTSC_YM_SEN | ADCTSC_YP_SEN | ADCTSC_XP_SEN |
249 1.1.4.2 mrg sc->next_stylus_intr |
250 1.1.4.2 mrg 3; /* 3 selects "Waiting for Interrupt Mode" */
251 1.1.4.2 mrg bus_space_write_4(sc->iot, sc->ioh, ADC_ADCTSC, adctsc);
252 1.1.4.2 mrg
253 1.1.4.2 mrg
254 1.1.4.2 mrg return 1;
255 1.1.4.2 mrg }
256 1.1.4.2 mrg
257 1.1.4.2 mrg int
258 1.1.4.2 mrg sstouch_enable(void *arg)
259 1.1.4.2 mrg {
260 1.1.4.2 mrg struct sstouch_softc *sc = arg;
261 1.1.4.2 mrg
262 1.1.4.2 mrg sstouch_initialize(sc);
263 1.1.4.2 mrg
264 1.1.4.2 mrg return 0;
265 1.1.4.2 mrg }
266 1.1.4.2 mrg
267 1.1.4.2 mrg int
268 1.1.4.2 mrg sstouch_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
269 1.1.4.2 mrg {
270 1.1.4.2 mrg struct sstouch_softc *sc = v;
271 1.1.4.2 mrg
272 1.1.4.2 mrg aprint_normal("%s\n", __func__);
273 1.1.4.2 mrg
274 1.1.4.2 mrg switch (cmd) {
275 1.1.4.2 mrg case WSMOUSEIO_GTYPE:
276 1.1.4.2 mrg *(uint *)data = WSMOUSE_TYPE_PSEUDO;
277 1.1.4.2 mrg break;
278 1.1.4.2 mrg case WSMOUSEIO_GCALIBCOORDS:
279 1.1.4.2 mrg case WSMOUSEIO_SCALIBCOORDS:
280 1.1.4.2 mrg return tpcalib_ioctl(&sc->tpcalib, cmd, data, flag, l);
281 1.1.4.2 mrg default:
282 1.1.4.2 mrg return EPASSTHROUGH;
283 1.1.4.2 mrg }
284 1.1.4.2 mrg
285 1.1.4.2 mrg return 0;
286 1.1.4.2 mrg }
287 1.1.4.2 mrg
288 1.1.4.2 mrg void
289 1.1.4.2 mrg sstouch_disable(void *arg)
290 1.1.4.2 mrg {
291 1.1.4.2 mrg struct sstouch_softc *sc = (struct sstouch_softc*)arg;
292 1.1.4.2 mrg
293 1.1.4.2 mrg /* By setting ADCCON register to 0, we also disable
294 1.1.4.2 mrg the prescaler, which should disable any interrupts.
295 1.1.4.2 mrg */
296 1.1.4.2 mrg bus_space_write_4(sc->iot, sc->ioh, ADC_ADCCON, 0);
297 1.1.4.2 mrg }
298 1.1.4.2 mrg
299 1.1.4.2 mrg void
300 1.1.4.2 mrg sstouch_callout(void *arg)
301 1.1.4.2 mrg {
302 1.1.4.2 mrg struct sstouch_softc *sc = (struct sstouch_softc*)arg;
303 1.1.4.2 mrg
304 1.1.4.2 mrg /* If stylus is down, perform a measurement */
305 1.1.4.2 mrg if (sc->next_stylus_intr == STYLUS_UP) {
306 1.1.4.2 mrg uint32_t reg;
307 1.1.4.2 mrg bus_space_write_4(sc->iot, sc->ioh, ADC_ADCTSC,
308 1.1.4.2 mrg ADCTSC_YM_SEN | ADCTSC_YP_SEN |
309 1.1.4.2 mrg ADCTSC_XP_SEN | ADCTSC_PULL_UP |
310 1.1.4.2 mrg ADCTSC_AUTO_PST);
311 1.1.4.2 mrg
312 1.1.4.2 mrg reg = bus_space_read_4(sc->iot, sc->ioh, ADC_ADCCON);
313 1.1.4.2 mrg bus_space_write_4(sc->iot, sc->ioh, ADC_ADCCON,
314 1.1.4.2 mrg reg | ADCCON_ENABLE_START);
315 1.1.4.2 mrg }
316 1.1.4.2 mrg
317 1.1.4.2 mrg }
318 1.1.4.2 mrg
319 1.1.4.2 mrg /* Do some very simple filtering on the measured values */
320 1.1.4.2 mrg int
321 1.1.4.2 mrg sstouch_filter_values(int *vals, int val_count)
322 1.1.4.2 mrg {
323 1.1.4.2 mrg int sum = 0;
324 1.1.4.2 mrg
325 1.1.4.2 mrg if (val_count < 5)
326 1.1.4.2 mrg return -1;
327 1.1.4.2 mrg
328 1.1.4.2 mrg for (int i=0; i<val_count; i++) {
329 1.1.4.2 mrg sum += vals[i];
330 1.1.4.2 mrg }
331 1.1.4.2 mrg
332 1.1.4.2 mrg return sum/val_count;
333 1.1.4.2 mrg }
334 1.1.4.2 mrg
335 1.1.4.2 mrg void
336 1.1.4.2 mrg sstouch_initialize(struct sstouch_softc *sc)
337 1.1.4.2 mrg {
338 1.1.4.2 mrg int prescaler;
339 1.1.4.2 mrg uint32_t adccon = 0;
340 1.1.4.2 mrg uint32_t adctsc = 0;
341 1.1.4.2 mrg
342 1.1.4.2 mrg /* ADC Conversion rate is calculated by:
343 1.1.4.2 mrg f(ADC) = PCLK/(prescaler+1)
344 1.1.4.2 mrg
345 1.1.4.2 mrg The ADC can operate at a maximum frequency of 2.5MHz for
346 1.1.4.2 mrg 500 KSPS.
347 1.1.4.2 mrg */
348 1.1.4.2 mrg
349 1.1.4.2 mrg /* Set f(ADC) = 50MHz / 256 = 1,95MHz */
350 1.1.4.2 mrg prescaler = 0xff;
351 1.1.4.2 mrg
352 1.1.4.2 mrg adccon |= ((prescaler<<ADCCON_PRSCVL_SHIFT) &
353 1.1.4.2 mrg ADCCON_PRSCVL_MASK);
354 1.1.4.2 mrg adccon |= ADCCON_PRSCEN;
355 1.1.4.2 mrg bus_space_write_4(sc->iot, sc->ioh, ADC_ADCCON, adccon);
356 1.1.4.2 mrg
357 1.1.4.2 mrg /* Use Auto Sequential measurement of X and Y positions */
358 1.1.4.2 mrg adctsc |= ADCTSC_YM_SEN | ADCTSC_YP_SEN | ADCTSC_XP_SEN |
359 1.1.4.2 mrg sc->next_stylus_intr |
360 1.1.4.2 mrg 3; /* 3 selects "Waiting for Interrupt Mode" */
361 1.1.4.2 mrg bus_space_write_4(sc->iot, sc->ioh, ADC_ADCTSC, adctsc);
362 1.1.4.2 mrg
363 1.1.4.2 mrg bus_space_write_4(sc->iot, sc->ioh, ADC_ADCUPDN, 0x0);
364 1.1.4.2 mrg
365 1.1.4.2 mrg /* Time used to measure each X/Y position value? */
366 1.1.4.2 mrg bus_space_write_4(sc->iot, sc->ioh, ADC_ADCDLY, 10000);
367 1.1.4.2 mrg }
368