source: mainline/uspace/srv/hid/s3c24xx_ts/s3c24xx_ts.c@ 7e752b2

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