[527298a] | 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>
|
---|
[15f3c3f] | 41 | #include <loc.h>
|
---|
[527298a] | 42 | #include <io/console.h>
|
---|
| 43 | #include <vfs/vfs.h>
|
---|
[022d9f67] | 44 | #include <ipc/mouseev.h>
|
---|
[527298a] | 45 | #include <async.h>
|
---|
| 46 | #include <unistd.h>
|
---|
| 47 | #include <stdio.h>
|
---|
| 48 | #include <stdlib.h>
|
---|
| 49 | #include <sysinfo.h>
|
---|
| 50 | #include <errno.h>
|
---|
[7e752b2] | 51 | #include <inttypes.h>
|
---|
[527298a] | 52 | #include "s3c24xx_ts.h"
|
---|
| 53 |
|
---|
[5da7199] | 54 | #define NAME "s3c24ser"
|
---|
| 55 | #define NAMESPACE "hid"
|
---|
[527298a] | 56 |
|
---|
| 57 | static irq_cmd_t ts_irq_cmds[] = {
|
---|
| 58 | {
|
---|
| 59 | .cmd = CMD_ACCEPT
|
---|
| 60 | }
|
---|
| 61 | };
|
---|
| 62 |
|
---|
| 63 | static irq_code_t ts_irq_code = {
|
---|
| 64 | sizeof(ts_irq_cmds) / sizeof(irq_cmd_t),
|
---|
| 65 | ts_irq_cmds
|
---|
| 66 | };
|
---|
| 67 |
|
---|
| 68 | /** S3C24xx touchscreen instance structure */
|
---|
| 69 | static s3c24xx_ts_t *ts;
|
---|
| 70 |
|
---|
[9934f7d] | 71 | static void s3c24xx_ts_connection(ipc_callid_t iid, ipc_call_t *icall,
|
---|
| 72 | void *arg);
|
---|
[527298a] | 73 | static void s3c24xx_ts_irq_handler(ipc_callid_t iid, ipc_call_t *call);
|
---|
| 74 | static void s3c24xx_ts_pen_down(s3c24xx_ts_t *ts);
|
---|
| 75 | static void s3c24xx_ts_pen_up(s3c24xx_ts_t *ts);
|
---|
| 76 | static void s3c24xx_ts_eoc(s3c24xx_ts_t *ts);
|
---|
| 77 | static int s3c24xx_ts_init(s3c24xx_ts_t *ts);
|
---|
| 78 | static void s3c24xx_ts_wait_for_int_mode(s3c24xx_ts_t *ts, ts_updn_t updn);
|
---|
| 79 | static void s3c24xx_ts_convert_samples(int smp0, int smp1, int *x, int *y);
|
---|
| 80 | static int lin_map_range(int v, int i0, int i1, int o0, int o1);
|
---|
| 81 |
|
---|
| 82 | int main(int argc, char *argv[])
|
---|
| 83 | {
|
---|
| 84 | int rc;
|
---|
| 85 |
|
---|
| 86 | printf(NAME ": S3C24xx touchscreen driver\n");
|
---|
| 87 |
|
---|
[15f3c3f] | 88 | rc = loc_server_register(NAME, s3c24xx_ts_connection);
|
---|
[527298a] | 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 |
|
---|
[15f3c3f] | 101 | rc = loc_service_register(NAMESPACE "/mouse", &ts->service_id);
|
---|
[527298a] | 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. */
|
---|
| 119 | static 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;
|
---|
[5da7199] | 132 | ts->client_sess = NULL;
|
---|
[527298a] | 133 | ts->state = ts_wait_pendown;
|
---|
| 134 | ts->last_x = 0;
|
---|
| 135 | ts->last_y = 0;
|
---|
| 136 |
|
---|
[7e752b2] | 137 | printf(NAME ": device at physical address %p, inr %" PRIun ".\n",
|
---|
| 138 | (void *) ts->paddr, inr);
|
---|
[527298a] | 139 |
|
---|
| 140 | async_set_interrupt_received(s3c24xx_ts_irq_handler);
|
---|
[ffa2c8ef] | 141 | register_irq(inr, device_assign_devno(), 0, &ts_irq_code);
|
---|
[527298a] | 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 | */
|
---|
| 157 | static 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 */
|
---|
| 206 | static 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 | */
|
---|
| 253 | static 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 | */
|
---|
| 272 | static 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;
|
---|
[5da7199] | 282 |
|
---|
| 283 | async_exch_t *exch = async_exchange_begin(ts->client_sess);
|
---|
| 284 | async_msg_2(exch, MOUSEEV_BUTTON_EVENT, button, press);
|
---|
| 285 | async_exchange_end(exch);
|
---|
[527298a] | 286 |
|
---|
| 287 | s3c24xx_ts_wait_for_int_mode(ts, updn_down);
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | /** Handle end-of-conversion interrupt.
|
---|
| 291 | *
|
---|
| 292 | * @param ts Touchscreen instance
|
---|
| 293 | */
|
---|
| 294 | static void s3c24xx_ts_eoc(s3c24xx_ts_t *ts)
|
---|
| 295 | {
|
---|
| 296 | uint32_t data;
|
---|
| 297 | int button, press;
|
---|
| 298 | int smp0, smp1;
|
---|
| 299 | int x_pos, y_pos;
|
---|
| 300 | int dx, dy;
|
---|
| 301 |
|
---|
| 302 | ts->state = ts_wait_penup;
|
---|
| 303 |
|
---|
| 304 | /* Read in sampled data. */
|
---|
| 305 |
|
---|
| 306 | data = pio_read_32(&ts->io->dat0);
|
---|
| 307 | smp0 = data & 0x3ff;
|
---|
| 308 |
|
---|
| 309 | data = pio_read_32(&ts->io->dat1);
|
---|
| 310 | smp1 = data & 0x3ff;
|
---|
| 311 |
|
---|
| 312 | /* Convert to screen coordinates. */
|
---|
| 313 | s3c24xx_ts_convert_samples(smp0, smp1, &x_pos, &y_pos);
|
---|
| 314 |
|
---|
| 315 | printf("s0: 0x%03x, s1:0x%03x -> x:%d,y:%d\n", smp0, smp1,
|
---|
| 316 | x_pos, y_pos);
|
---|
| 317 |
|
---|
| 318 | /* Get differences. */
|
---|
| 319 | dx = x_pos - ts->last_x;
|
---|
| 320 | dy = y_pos - ts->last_y;
|
---|
| 321 |
|
---|
| 322 | button = 1;
|
---|
| 323 | press = 1;
|
---|
| 324 |
|
---|
| 325 | /* Send notifications to client. */
|
---|
[5da7199] | 326 | async_exch_t *exch = async_exchange_begin(ts->client_sess);
|
---|
| 327 | async_msg_2(exch, MOUSEEV_MOVE_EVENT, dx, dy);
|
---|
| 328 | async_msg_2(exch, MOUSEEV_BUTTON_EVENT, button, press);
|
---|
| 329 | async_exchange_end(exch);
|
---|
[527298a] | 330 |
|
---|
| 331 | ts->last_x = x_pos;
|
---|
| 332 | ts->last_y = y_pos;
|
---|
| 333 |
|
---|
| 334 | s3c24xx_ts_wait_for_int_mode(ts, updn_up);
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 | /** Convert sampled data to screen coordinates. */
|
---|
| 338 | static void s3c24xx_ts_convert_samples(int smp0, int smp1, int *x, int *y)
|
---|
| 339 | {
|
---|
| 340 | /*
|
---|
| 341 | * The orientation and display dimensions are GTA02-specific and the
|
---|
| 342 | * calibration values might even specific to the individual piece
|
---|
| 343 | * of hardware.
|
---|
| 344 | *
|
---|
| 345 | * The calibration values can be obtained by touching corners
|
---|
| 346 | * of the screen with the stylus and noting the sampled values.
|
---|
| 347 | */
|
---|
| 348 | *x = lin_map_range(smp1, 0xa1, 0x396, 0, 479);
|
---|
| 349 | *y = lin_map_range(smp0, 0x69, 0x38a, 639, 0);
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | /** Map integer from one range to another range in a linear fashion.
|
---|
| 353 | *
|
---|
| 354 | * i0 < i1 is required. i0 is mapped to o0, i1 to o1. If o1 < o0, then the
|
---|
| 355 | * mapping will be descending. If v is outside of [i0, i1], it is clamped.
|
---|
| 356 | *
|
---|
| 357 | * @param v Value to map.
|
---|
| 358 | * @param i0 Lower bound of input range.
|
---|
| 359 | * @param i1 Upper bound of input range.
|
---|
| 360 | * @param o0 First bound of output range.
|
---|
| 361 | * @param o1 Second bound of output range.
|
---|
| 362 | *
|
---|
| 363 | * @return Mapped value ov, o0 <= ov <= o1.
|
---|
| 364 | */
|
---|
| 365 | static int lin_map_range(int v, int i0, int i1, int o0, int o1)
|
---|
| 366 | {
|
---|
| 367 | if (v < i0)
|
---|
| 368 | v = i0;
|
---|
| 369 |
|
---|
| 370 | if (v > i1)
|
---|
| 371 | v = i1;
|
---|
| 372 |
|
---|
| 373 | return o0 + (o1 - o0) * (v - i0) / (i1 - i0);
|
---|
| 374 | }
|
---|
| 375 |
|
---|
| 376 | /** Handle mouse client connection. */
|
---|
[9934f7d] | 377 | static void s3c24xx_ts_connection(ipc_callid_t iid, ipc_call_t *icall,
|
---|
| 378 | void *arg)
|
---|
[527298a] | 379 | {
|
---|
[ffa2c8ef] | 380 | async_answer_0(iid, EOK);
|
---|
[5da7199] | 381 |
|
---|
| 382 | while (true) {
|
---|
| 383 | ipc_call_t call;
|
---|
| 384 | ipc_callid_t callid = async_get_call(&call);
|
---|
[79ae36dd] | 385 |
|
---|
| 386 | if (!IPC_GET_IMETHOD(call)) {
|
---|
[5da7199] | 387 | if (ts->client_sess != NULL) {
|
---|
| 388 | async_hangup(ts->client_sess);
|
---|
| 389 | ts->client_sess = NULL;
|
---|
[527298a] | 390 | }
|
---|
[5da7199] | 391 |
|
---|
[ffa2c8ef] | 392 | async_answer_0(callid, EOK);
|
---|
[527298a] | 393 | return;
|
---|
[79ae36dd] | 394 | }
|
---|
| 395 |
|
---|
[5da7199] | 396 | async_sess_t *sess =
|
---|
| 397 | async_callback_receive_start(EXCHANGE_SERIALIZE, &call);
|
---|
| 398 | if (sess != NULL) {
|
---|
| 399 | if (ts->client_sess == NULL) {
|
---|
| 400 | ts->client_sess = sess;
|
---|
| 401 | async_answer_0(callid, EOK);
|
---|
| 402 | } else
|
---|
| 403 | async_answer_0(callid, ELIMIT);
|
---|
| 404 | } else
|
---|
| 405 | async_answer_0(callid, EINVAL);
|
---|
[527298a] | 406 | }
|
---|
| 407 | }
|
---|
| 408 |
|
---|
| 409 | /** @}
|
---|
| 410 | */
|
---|