source: mainline/uspace/srv/hid/s3c24xx_ts/s3c24xx_ts.c@ fa77af7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since fa77af7 was 24abb85d, checked in by Jakub Jermar <jakub@…>, 8 years ago

Remove SYS_DEVICE_ASSIGN_DEVNO

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