source: mainline/uspace/srv/hid/s3c24xx_ts/s3c24xx_ts.c@ 106743d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 106743d was ffa2c8ef, checked in by Martin Decky <martin@…>, 15 years ago

do not intermix low-level IPC methods with async framework methods

  • Property mode set to 100644
File size: 9.9 KB
Line 
1/*
2 * Copyright (c) 2010 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - 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 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup mouse
30 * @{
31 */
32/**
33 * @file
34 * @brief Samsung Samsung S3C24xx on-chip ADC and touch-screen interface driver.
35 *
36 * This interface is present on the Samsung S3C24xx CPU (on the gta02 platform).
37 */
38
39#include <ddi.h>
40#include <libarch/ddi.h>
41#include <devmap.h>
42#include <io/console.h>
43#include <vfs/vfs.h>
44#include <ipc/mouse.h>
45#include <async.h>
46#include <unistd.h>
47#include <stdio.h>
48#include <stdlib.h>
49#include <sysinfo.h>
50#include <errno.h>
51#include <inttypes.h>
52
53#include "s3c24xx_ts.h"
54
55#define NAME "s3c24ser"
56#define NAMESPACE "hid_in"
57
58static irq_cmd_t ts_irq_cmds[] = {
59 {
60 .cmd = CMD_ACCEPT
61 }
62};
63
64static irq_code_t ts_irq_code = {
65 sizeof(ts_irq_cmds) / sizeof(irq_cmd_t),
66 ts_irq_cmds
67};
68
69/** S3C24xx touchscreen instance structure */
70static s3c24xx_ts_t *ts;
71
72static void s3c24xx_ts_connection(ipc_callid_t iid, ipc_call_t *icall);
73static void s3c24xx_ts_irq_handler(ipc_callid_t iid, ipc_call_t *call);
74static void s3c24xx_ts_pen_down(s3c24xx_ts_t *ts);
75static void s3c24xx_ts_pen_up(s3c24xx_ts_t *ts);
76static void s3c24xx_ts_eoc(s3c24xx_ts_t *ts);
77static int s3c24xx_ts_init(s3c24xx_ts_t *ts);
78static void s3c24xx_ts_wait_for_int_mode(s3c24xx_ts_t *ts, ts_updn_t updn);
79static void s3c24xx_ts_convert_samples(int smp0, int smp1, int *x, int *y);
80static int lin_map_range(int v, int i0, int i1, int o0, int o1);
81
82int main(int argc, char *argv[])
83{
84 int rc;
85
86 printf(NAME ": S3C24xx touchscreen driver\n");
87
88 rc = devmap_driver_register(NAME, s3c24xx_ts_connection);
89 if (rc < 0) {
90 printf(NAME ": Unable to register driver.\n");
91 return -1;
92 }
93
94 ts = malloc(sizeof(s3c24xx_ts_t));
95 if (ts == NULL)
96 return -1;
97
98 if (s3c24xx_ts_init(ts) != EOK)
99 return -1;
100
101 rc = devmap_device_register(NAMESPACE "/mouse", &ts->devmap_handle);
102 if (rc != EOK) {
103 printf(NAME ": Unable to register device %s.\n",
104 NAMESPACE "/mouse");
105 return -1;
106 }
107
108 printf(NAME ": Registered device %s.\n", NAMESPACE "/mouse");
109
110 printf(NAME ": Accepting connections\n");
111 task_retval(0);
112 async_manager();
113
114 /* Not reached */
115 return 0;
116}
117
118/** Initialize S3C24xx touchscreen interface. */
119static int s3c24xx_ts_init(s3c24xx_ts_t *ts)
120{
121 void *vaddr;
122 sysarg_t inr;
123
124 inr = S3C24XX_TS_INR;
125 ts->paddr = S3C24XX_TS_ADDR;
126
127 if (pio_enable((void *) ts->paddr, sizeof(s3c24xx_adc_io_t),
128 &vaddr) != 0)
129 return -1;
130
131 ts->io = vaddr;
132 ts->client_phone = -1;
133 ts->state = ts_wait_pendown;
134 ts->last_x = 0;
135 ts->last_y = 0;
136
137 printf(NAME ": device at physical address %p, inr %" PRIun ".\n",
138 (void *) ts->paddr, inr);
139
140 async_set_interrupt_received(s3c24xx_ts_irq_handler);
141 register_irq(inr, device_assign_devno(), 0, &ts_irq_code);
142
143 s3c24xx_ts_wait_for_int_mode(ts, updn_down);
144
145 return EOK;
146}
147
148/** Switch interface to wait for interrupt mode.
149 *
150 * In this mode we receive an interrupt when pen goes up/down, depending
151 * on @a updn.
152 *
153 * @param ts Touchscreen instance
154 * @param updn @c updn_up to wait for pen up, @c updn_down to wait for pen
155 * down.
156 */
157static void s3c24xx_ts_wait_for_int_mode(s3c24xx_ts_t *ts, ts_updn_t updn)
158{
159 uint32_t con, tsc;
160
161 /*
162 * Configure ADCCON register
163 */
164
165 con = pio_read_32(&ts->io->con);
166
167 /* Disable standby, disable start-by-read, clear manual start bit */
168 con = con & ~(ADCCON_STDBM | ADCCON_READ_START | ADCCON_ENABLE_START);
169
170 /* Set prescaler value 0xff, XP for input. */
171 con = con | (ADCCON_PRSCVL(0xff) << 6) | ADCCON_SEL_MUX(SMUX_XP);
172
173 /* Enable prescaler. */
174 con = con | ADCCON_PRSCEN;
175
176 pio_write_32(&ts->io->con, con);
177
178 /*
179 * Configure ADCTSC register
180 */
181
182 tsc = pio_read_32(&ts->io->tsc);
183
184 /* Select whether waiting for pen up or pen down. */
185 if (updn == updn_up)
186 tsc |= ADCTSC_DSUD_UP;
187 else
188 tsc &= ~ADCTSC_DSUD_UP;
189
190 /*
191 * Enable XP pull-up and disable all drivers except YM. This is
192 * according to the manual. This gives us L on XP input when touching
193 * and (pulled up to) H when not touching.
194 */
195 tsc = tsc & ~(ADCTSC_XM_ENABLE | ADCTSC_AUTO_PST |
196 ADCTSC_PULLUP_DISABLE);
197 tsc = tsc | ADCTSC_YP_DISABLE | ADCTSC_XP_DISABLE | ADCTSC_YM_ENABLE;
198
199 /* Select wait-for-interrupt mode. */
200 tsc = (tsc & ~ADCTSC_XY_PST_MASK) | ADCTSC_XY_PST_WAITINT;
201
202 pio_write_32(&ts->io->tsc, tsc);
203}
204
205/** Handle touchscreen interrupt */
206static void s3c24xx_ts_irq_handler(ipc_callid_t iid, ipc_call_t *call)
207{
208 ts_updn_t updn;
209
210 (void) iid; (void) call;
211
212 /* Read up/down interrupt flags. */
213 updn = pio_read_32(&ts->io->updn);
214
215 if (updn & (ADCUPDN_TSC_DN | ADCUPDN_TSC_UP)) {
216 /* Clear up/down interrupt flags. */
217 pio_write_32(&ts->io->updn, updn &
218 ~(ADCUPDN_TSC_DN | ADCUPDN_TSC_UP));
219 }
220
221 if (updn & ADCUPDN_TSC_DN) {
222 /* Pen-down interrupt */
223 s3c24xx_ts_pen_down(ts);
224 } else if (updn & ADCUPDN_TSC_UP) {
225 /* Pen-up interrupt */
226 s3c24xx_ts_pen_up(ts);
227 } else {
228 /* Presumably end-of-conversion interrupt */
229
230 /* Check end-of-conversion flag. */
231 if ((pio_read_32(&ts->io->con) & ADCCON_ECFLG) == 0) {
232 printf(NAME ": Unrecognized ts int.\n");
233 return;
234 }
235
236 if (ts->state != ts_sample_pos) {
237 /*
238 * We got an extra interrupt ater switching to
239 * wait for interrupt mode.
240 */
241 return;
242 }
243
244 /* End-of-conversion interrupt */
245 s3c24xx_ts_eoc(ts);
246 }
247}
248
249/** Handle pen-down interrupt.
250 *
251 * @param ts Touchscreen instance
252 */
253static void s3c24xx_ts_pen_down(s3c24xx_ts_t *ts)
254{
255 /* Pen-down interrupt */
256
257 ts->state = ts_sample_pos;
258
259 /* Enable auto xy-conversion mode */
260 pio_write_32(&ts->io->tsc, (pio_read_32(&ts->io->tsc)
261 & ~3) | 4);
262
263 /* Start the conversion. */
264 pio_write_32(&ts->io->con, pio_read_32(&ts->io->con)
265 | ADCCON_ENABLE_START);
266}
267
268/** Handle pen-up interrupt.
269 *
270 * @param ts Touchscreen instance
271 */
272static void s3c24xx_ts_pen_up(s3c24xx_ts_t *ts)
273{
274 int button, press;
275
276 /* Pen-up interrupt */
277
278 ts->state = ts_wait_pendown;
279
280 button = 1;
281 press = 0;
282 async_msg_2(ts->client_phone, MEVENT_BUTTON, button, press);
283
284 s3c24xx_ts_wait_for_int_mode(ts, updn_down);
285}
286
287/** Handle end-of-conversion interrupt.
288 *
289 * @param ts Touchscreen instance
290 */
291static void s3c24xx_ts_eoc(s3c24xx_ts_t *ts)
292{
293 uint32_t data;
294 int button, press;
295 int smp0, smp1;
296 int x_pos, y_pos;
297 int dx, dy;
298
299 ts->state = ts_wait_penup;
300
301 /* Read in sampled data. */
302
303 data = pio_read_32(&ts->io->dat0);
304 smp0 = data & 0x3ff;
305
306 data = pio_read_32(&ts->io->dat1);
307 smp1 = data & 0x3ff;
308
309 /* Convert to screen coordinates. */
310 s3c24xx_ts_convert_samples(smp0, smp1, &x_pos, &y_pos);
311
312 printf("s0: 0x%03x, s1:0x%03x -> x:%d,y:%d\n", smp0, smp1,
313 x_pos, y_pos);
314
315 /* Get differences. */
316 dx = x_pos - ts->last_x;
317 dy = y_pos - ts->last_y;
318
319 button = 1;
320 press = 1;
321
322 /* Send notifications to client. */
323 async_msg_2(ts->client_phone, MEVENT_MOVE, dx, dy);
324 async_msg_2(ts->client_phone, MEVENT_BUTTON, button, press);
325
326 ts->last_x = x_pos;
327 ts->last_y = y_pos;
328
329 s3c24xx_ts_wait_for_int_mode(ts, updn_up);
330}
331
332/** Convert sampled data to screen coordinates. */
333static void s3c24xx_ts_convert_samples(int smp0, int smp1, int *x, int *y)
334{
335 /*
336 * The orientation and display dimensions are GTA02-specific and the
337 * calibration values might even specific to the individual piece
338 * of hardware.
339 *
340 * The calibration values can be obtained by touching corners
341 * of the screen with the stylus and noting the sampled values.
342 */
343 *x = lin_map_range(smp1, 0xa1, 0x396, 0, 479);
344 *y = lin_map_range(smp0, 0x69, 0x38a, 639, 0);
345}
346
347/** Map integer from one range to another range in a linear fashion.
348 *
349 * i0 < i1 is required. i0 is mapped to o0, i1 to o1. If o1 < o0, then the
350 * mapping will be descending. If v is outside of [i0, i1], it is clamped.
351 *
352 * @param v Value to map.
353 * @param i0 Lower bound of input range.
354 * @param i1 Upper bound of input range.
355 * @param o0 First bound of output range.
356 * @param o1 Second bound of output range.
357 *
358 * @return Mapped value ov, o0 <= ov <= o1.
359 */
360static int lin_map_range(int v, int i0, int i1, int o0, int o1)
361{
362 if (v < i0)
363 v = i0;
364
365 if (v > i1)
366 v = i1;
367
368 return o0 + (o1 - o0) * (v - i0) / (i1 - i0);
369}
370
371/** Handle mouse client connection. */
372static void s3c24xx_ts_connection(ipc_callid_t iid, ipc_call_t *icall)
373{
374 ipc_callid_t callid;
375 ipc_call_t call;
376 int retval;
377
378 async_answer_0(iid, EOK);
379
380 while (1) {
381 callid = async_get_call(&call);
382 switch (IPC_GET_IMETHOD(call)) {
383 case IPC_M_PHONE_HUNGUP:
384 if (ts->client_phone != -1) {
385 async_hangup(ts->client_phone);
386 ts->client_phone = -1;
387 }
388
389 async_answer_0(callid, EOK);
390 return;
391 case IPC_M_CONNECT_TO_ME:
392 if (ts->client_phone != -1) {
393 retval = ELIMIT;
394 break;
395 }
396 ts->client_phone = IPC_GET_ARG5(call);
397 retval = 0;
398 break;
399 default:
400 retval = EINVAL;
401 }
402 async_answer_0(callid, retval);
403 }
404}
405
406/** @}
407 */
Note: See TracBrowser for help on using the repository browser.