source: mainline/uspace/srv/hid/s3c24xx_ts/s3c24xx_ts.c@ 33c08929

Last change on this file since 33c08929 was a635535, checked in by Jiri Svoboda <jiri@…>, 22 months ago

Move console/con_srv out of libc into a separate library

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