source: mainline/uspace/srv/hid/s3c24xx_ts/s3c24xx_ts.c@ 64d2b10

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 64d2b10 was 7ea7db31, checked in by Jakub Jermar <jakub@…>, 15 years ago

Cease using devmap_get_phone() and devmap_hangup_phone() in drivers.

  • 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/ipc.h>
45#include <ipc/mouse.h>
46#include <async.h>
47#include <unistd.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <sysinfo.h>
51#include <errno.h>
52#include <inttypes.h>
53
54#include "s3c24xx_ts.h"
55
56#define NAME "s3c24ser"
57#define NAMESPACE "hid_in"
58
59static irq_cmd_t ts_irq_cmds[] = {
60 {
61 .cmd = CMD_ACCEPT
62 }
63};
64
65static irq_code_t ts_irq_code = {
66 sizeof(ts_irq_cmds) / sizeof(irq_cmd_t),
67 ts_irq_cmds
68};
69
70/** S3C24xx touchscreen instance structure */
71static s3c24xx_ts_t *ts;
72
73static void s3c24xx_ts_connection(ipc_callid_t iid, ipc_call_t *icall);
74static void s3c24xx_ts_irq_handler(ipc_callid_t iid, ipc_call_t *call);
75static void s3c24xx_ts_pen_down(s3c24xx_ts_t *ts);
76static void s3c24xx_ts_pen_up(s3c24xx_ts_t *ts);
77static void s3c24xx_ts_eoc(s3c24xx_ts_t *ts);
78static int s3c24xx_ts_init(s3c24xx_ts_t *ts);
79static void s3c24xx_ts_wait_for_int_mode(s3c24xx_ts_t *ts, ts_updn_t updn);
80static void s3c24xx_ts_convert_samples(int smp0, int smp1, int *x, int *y);
81static int lin_map_range(int v, int i0, int i1, int o0, int o1);
82
83int main(int argc, char *argv[])
84{
85 int rc;
86
87 printf(NAME ": S3C24xx touchscreen driver\n");
88
89 rc = devmap_driver_register(NAME, s3c24xx_ts_connection);
90 if (rc < 0) {
91 printf(NAME ": Unable to register driver.\n");
92 return -1;
93 }
94
95 ts = malloc(sizeof(s3c24xx_ts_t));
96 if (ts == NULL)
97 return -1;
98
99 if (s3c24xx_ts_init(ts) != EOK)
100 return -1;
101
102 rc = devmap_device_register(NAMESPACE "/mouse", &ts->devmap_handle);
103 if (rc != EOK) {
104 printf(NAME ": Unable to register device %s.\n",
105 NAMESPACE "/mouse");
106 return -1;
107 }
108
109 printf(NAME ": Registered device %s.\n", NAMESPACE "/mouse");
110
111 printf(NAME ": Accepting connections\n");
112 task_retval(0);
113 async_manager();
114
115 /* Not reached */
116 return 0;
117}
118
119/** Initialize S3C24xx touchscreen interface. */
120static int s3c24xx_ts_init(s3c24xx_ts_t *ts)
121{
122 void *vaddr;
123 sysarg_t inr;
124
125 inr = S3C24XX_TS_INR;
126 ts->paddr = S3C24XX_TS_ADDR;
127
128 if (pio_enable((void *) ts->paddr, sizeof(s3c24xx_adc_io_t),
129 &vaddr) != 0)
130 return -1;
131
132 ts->io = vaddr;
133 ts->client_phone = -1;
134 ts->state = ts_wait_pendown;
135 ts->last_x = 0;
136 ts->last_y = 0;
137
138 printf(NAME ": device at physical address %p, inr %" PRIun ".\n",
139 (void *) ts->paddr, inr);
140
141 async_set_interrupt_received(s3c24xx_ts_irq_handler);
142 ipc_register_irq(inr, device_assign_devno(), 0, &ts_irq_code);
143
144 s3c24xx_ts_wait_for_int_mode(ts, updn_down);
145
146 return EOK;
147}
148
149/** Switch interface to wait for interrupt mode.
150 *
151 * In this mode we receive an interrupt when pen goes up/down, depending
152 * on @a updn.
153 *
154 * @param ts Touchscreen instance
155 * @param updn @c updn_up to wait for pen up, @c updn_down to wait for pen
156 * down.
157 */
158static void s3c24xx_ts_wait_for_int_mode(s3c24xx_ts_t *ts, ts_updn_t updn)
159{
160 uint32_t con, tsc;
161
162 /*
163 * Configure ADCCON register
164 */
165
166 con = pio_read_32(&ts->io->con);
167
168 /* Disable standby, disable start-by-read, clear manual start bit */
169 con = con & ~(ADCCON_STDBM | ADCCON_READ_START | ADCCON_ENABLE_START);
170
171 /* Set prescaler value 0xff, XP for input. */
172 con = con | (ADCCON_PRSCVL(0xff) << 6) | ADCCON_SEL_MUX(SMUX_XP);
173
174 /* Enable prescaler. */
175 con = con | ADCCON_PRSCEN;
176
177 pio_write_32(&ts->io->con, con);
178
179 /*
180 * Configure ADCTSC register
181 */
182
183 tsc = pio_read_32(&ts->io->tsc);
184
185 /* Select whether waiting for pen up or pen down. */
186 if (updn == updn_up)
187 tsc |= ADCTSC_DSUD_UP;
188 else
189 tsc &= ~ADCTSC_DSUD_UP;
190
191 /*
192 * Enable XP pull-up and disable all drivers except YM. This is
193 * according to the manual. This gives us L on XP input when touching
194 * and (pulled up to) H when not touching.
195 */
196 tsc = tsc & ~(ADCTSC_XM_ENABLE | ADCTSC_AUTO_PST |
197 ADCTSC_PULLUP_DISABLE);
198 tsc = tsc | ADCTSC_YP_DISABLE | ADCTSC_XP_DISABLE | ADCTSC_YM_ENABLE;
199
200 /* Select wait-for-interrupt mode. */
201 tsc = (tsc & ~ADCTSC_XY_PST_MASK) | ADCTSC_XY_PST_WAITINT;
202
203 pio_write_32(&ts->io->tsc, tsc);
204}
205
206/** Handle touchscreen interrupt */
207static void s3c24xx_ts_irq_handler(ipc_callid_t iid, ipc_call_t *call)
208{
209 ts_updn_t updn;
210
211 (void) iid; (void) call;
212
213 /* Read up/down interrupt flags. */
214 updn = pio_read_32(&ts->io->updn);
215
216 if (updn & (ADCUPDN_TSC_DN | ADCUPDN_TSC_UP)) {
217 /* Clear up/down interrupt flags. */
218 pio_write_32(&ts->io->updn, updn &
219 ~(ADCUPDN_TSC_DN | ADCUPDN_TSC_UP));
220 }
221
222 if (updn & ADCUPDN_TSC_DN) {
223 /* Pen-down interrupt */
224 s3c24xx_ts_pen_down(ts);
225 } else if (updn & ADCUPDN_TSC_UP) {
226 /* Pen-up interrupt */
227 s3c24xx_ts_pen_up(ts);
228 } else {
229 /* Presumably end-of-conversion interrupt */
230
231 /* Check end-of-conversion flag. */
232 if ((pio_read_32(&ts->io->con) & ADCCON_ECFLG) == 0) {
233 printf(NAME ": Unrecognized ts int.\n");
234 return;
235 }
236
237 if (ts->state != ts_sample_pos) {
238 /*
239 * We got an extra interrupt ater switching to
240 * wait for interrupt mode.
241 */
242 return;
243 }
244
245 /* End-of-conversion interrupt */
246 s3c24xx_ts_eoc(ts);
247 }
248}
249
250/** Handle pen-down interrupt.
251 *
252 * @param ts Touchscreen instance
253 */
254static void s3c24xx_ts_pen_down(s3c24xx_ts_t *ts)
255{
256 /* Pen-down interrupt */
257
258 ts->state = ts_sample_pos;
259
260 /* Enable auto xy-conversion mode */
261 pio_write_32(&ts->io->tsc, (pio_read_32(&ts->io->tsc)
262 & ~3) | 4);
263
264 /* Start the conversion. */
265 pio_write_32(&ts->io->con, pio_read_32(&ts->io->con)
266 | ADCCON_ENABLE_START);
267}
268
269/** Handle pen-up interrupt.
270 *
271 * @param ts Touchscreen instance
272 */
273static void s3c24xx_ts_pen_up(s3c24xx_ts_t *ts)
274{
275 int button, press;
276
277 /* Pen-up interrupt */
278
279 ts->state = ts_wait_pendown;
280
281 button = 1;
282 press = 0;
283 async_msg_2(ts->client_phone, MEVENT_BUTTON, button, press);
284
285 s3c24xx_ts_wait_for_int_mode(ts, updn_down);
286}
287
288/** Handle end-of-conversion interrupt.
289 *
290 * @param ts Touchscreen instance
291 */
292static void s3c24xx_ts_eoc(s3c24xx_ts_t *ts)
293{
294 uint32_t data;
295 int button, press;
296 int smp0, smp1;
297 int x_pos, y_pos;
298 int dx, dy;
299
300 ts->state = ts_wait_penup;
301
302 /* Read in sampled data. */
303
304 data = pio_read_32(&ts->io->dat0);
305 smp0 = data & 0x3ff;
306
307 data = pio_read_32(&ts->io->dat1);
308 smp1 = data & 0x3ff;
309
310 /* Convert to screen coordinates. */
311 s3c24xx_ts_convert_samples(smp0, smp1, &x_pos, &y_pos);
312
313 printf("s0: 0x%03x, s1:0x%03x -> x:%d,y:%d\n", smp0, smp1,
314 x_pos, y_pos);
315
316 /* Get differences. */
317 dx = x_pos - ts->last_x;
318 dy = y_pos - ts->last_y;
319
320 button = 1;
321 press = 1;
322
323 /* Send notifications to client. */
324 async_msg_2(ts->client_phone, MEVENT_MOVE, dx, dy);
325 async_msg_2(ts->client_phone, MEVENT_BUTTON, button, press);
326
327 ts->last_x = x_pos;
328 ts->last_y = y_pos;
329
330 s3c24xx_ts_wait_for_int_mode(ts, updn_up);
331}
332
333/** Convert sampled data to screen coordinates. */
334static void s3c24xx_ts_convert_samples(int smp0, int smp1, int *x, int *y)
335{
336 /*
337 * The orientation and display dimensions are GTA02-specific and the
338 * calibration values might even specific to the individual piece
339 * of hardware.
340 *
341 * The calibration values can be obtained by touching corners
342 * of the screen with the stylus and noting the sampled values.
343 */
344 *x = lin_map_range(smp1, 0xa1, 0x396, 0, 479);
345 *y = lin_map_range(smp0, 0x69, 0x38a, 639, 0);
346}
347
348/** Map integer from one range to another range in a linear fashion.
349 *
350 * i0 < i1 is required. i0 is mapped to o0, i1 to o1. If o1 < o0, then the
351 * mapping will be descending. If v is outside of [i0, i1], it is clamped.
352 *
353 * @param v Value to map.
354 * @param i0 Lower bound of input range.
355 * @param i1 Upper bound of input range.
356 * @param o0 First bound of output range.
357 * @param o1 Second bound of output range.
358 *
359 * @return Mapped value ov, o0 <= ov <= o1.
360 */
361static int lin_map_range(int v, int i0, int i1, int o0, int o1)
362{
363 if (v < i0)
364 v = i0;
365
366 if (v > i1)
367 v = i1;
368
369 return o0 + (o1 - o0) * (v - i0) / (i1 - i0);
370}
371
372/** Handle mouse client connection. */
373static void s3c24xx_ts_connection(ipc_callid_t iid, ipc_call_t *icall)
374{
375 ipc_callid_t callid;
376 ipc_call_t call;
377 int retval;
378
379 ipc_answer_0(iid, EOK);
380
381 while (1) {
382 callid = async_get_call(&call);
383 switch (IPC_GET_IMETHOD(call)) {
384 case IPC_M_PHONE_HUNGUP:
385 if (ts->client_phone != -1) {
386 ipc_hangup(ts->client_phone);
387 ts->client_phone = -1;
388 }
389
390 ipc_answer_0(callid, EOK);
391 return;
392 case IPC_M_CONNECT_TO_ME:
393 if (ts->client_phone != -1) {
394 retval = ELIMIT;
395 break;
396 }
397 ts->client_phone = IPC_GET_ARG5(call);
398 retval = 0;
399 break;
400 default:
401 retval = EINVAL;
402 }
403 ipc_answer_0(callid, retval);
404 }
405}
406
407/** @}
408 */
Note: See TracBrowser for help on using the repository browser.