Patchwork [27/48] tsc2005 overhaul

login
register
mail settings
Submitter Riku Voipio
Date March 26, 2010, 4:06 p.m.
Message ID <8bf5dfc593b90e5988c9da6110514c1a19af3543.1269617186.git.riku.voipio@nokia.com>
Download mbox | patch
Permalink /patch/48706/
State New
Headers show

Comments

Riku Voipio - March 26, 2010, 4:06 p.m.
From: Juha Riihimäki <juha.riihimaki@nokia.com>

Signed-Off-By: Riku Voipio <riku.voipio@nokia.com>
Signed-Off-By: Juha Riihimäki <juha.riihimaki@nokia.com>
---
 hw/tsc2005.c |  173 ++++++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 120 insertions(+), 53 deletions(-)

Patch

diff --git a/hw/tsc2005.c b/hw/tsc2005.c
index b75cc86..da39979 100644
--- a/hw/tsc2005.c
+++ b/hw/tsc2005.c
@@ -2,7 +2,7 @@ 
  * TI TSC2005 emulator.
  *
  * Copyright (c) 2006 Andrzej Zaborowski  <balrog@zabor.org>
- * Copyright (C) 2008 Nokia Corporation
+ * Copyright (c) 2009-2010 Nokia Corporation
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -23,6 +23,15 @@ 
 #include "console.h"
 #include "devices.h"
 
+//#define TSC2005_DEBUG
+
+#ifdef TSC2005_DEBUG
+#define TRACE(fmt, ...) fprintf(stderr, "%s@%d: " fmt "\n", \
+                                __FUNCTION__, __LINE__, ##__VA_ARGS__)
+#else
+#define TRACE(...)
+#endif
+
 #define TSC_CUT_RESOLUTION(value, p)	((value) >> (16 - (p ? 12 : 10)))
 
 typedef struct {
@@ -54,6 +63,7 @@  typedef struct {
     uint16_t aux_thr[2];
 
     int tr[8];
+    int z1_cons, z2_cons;
 } TSC2005State;
 
 enum {
@@ -98,10 +108,15 @@  static const uint16_t mode_regs[16] = {
     ((s->y * s->tr[0] - s->x * s->tr[1]) / s->tr[2] + s->tr[3])
 #define Y_TRANSFORM(s)			\
     ((s->y * s->tr[4] - s->x * s->tr[5]) / s->tr[6] + s->tr[7])
+/*
 #define Z1_TRANSFORM(s)			\
-    ((400 - ((s)->x >> 7) + ((s)->pressure << 10)) << 4)
+    (((s)->z1_cons - ((s)->x >> 7) + ((s)->pressure << 10)) << 4)
 #define Z2_TRANSFORM(s)			\
-    ((4000 + ((s)->y >> 7) - ((s)->pressure << 10)) << 4)
+    (((s)->z2_cons + ((s)->y >> 7) - ((s)->pressure << 10)) << 4)
+ */
+/* these simpler forms work much better */
+#define Z1_TRANSFORM(s) (((s)->z1_cons) << 4)
+#define Z2_TRANSFORM(s) (((s)->z2_cons) << 4)
 
 #define AUX_VAL				(700 << 4)	/* +/- 3 at 12-bit */
 #define TEMP1_VAL			(1264 << 4)	/* +/- 5 at 12-bit */
@@ -114,33 +129,46 @@  static uint16_t tsc2005_read(TSC2005State *s, int reg)
     switch (reg) {
     case 0x0:	/* X */
         s->dav &= ~mode_regs[TSC_MODE_X];
+        TRACE("X = %d", TSC_CUT_RESOLUTION(X_TRANSFORM(s), s->precision) +
+              (s->noise & 3));
         return TSC_CUT_RESOLUTION(X_TRANSFORM(s), s->precision) +
                 (s->noise & 3);
     case 0x1:	/* Y */
         s->dav &= ~mode_regs[TSC_MODE_Y];
         s->noise ++;
+        TRACE("Y = %d", TSC_CUT_RESOLUTION(Y_TRANSFORM(s), s->precision) ^
+              (s->noise & 3));
         return TSC_CUT_RESOLUTION(Y_TRANSFORM(s), s->precision) ^
                 (s->noise & 3);
     case 0x2:	/* Z1 */
         s->dav &= 0xdfff;
+        TRACE("Z1 = %d", TSC_CUT_RESOLUTION(Z1_TRANSFORM(s), s->precision) -
+              (s->noise & 3));
         return TSC_CUT_RESOLUTION(Z1_TRANSFORM(s), s->precision) -
                 (s->noise & 3);
     case 0x3:	/* Z2 */
         s->dav &= 0xefff;
+        TRACE("Z2 = %d", TSC_CUT_RESOLUTION(Z2_TRANSFORM(s), s->precision) |
+              (s->noise & 3));
         return TSC_CUT_RESOLUTION(Z2_TRANSFORM(s), s->precision) |
                 (s->noise & 3);
 
     case 0x4:	/* AUX */
         s->dav &= ~mode_regs[TSC_MODE_AUX];
+        TRACE("AUX = %d", TSC_CUT_RESOLUTION(AUX_VAL, s->precision));
         return TSC_CUT_RESOLUTION(AUX_VAL, s->precision);
 
     case 0x5:	/* TEMP1 */
         s->dav &= ~mode_regs[TSC_MODE_TEMP1];
+        TRACE("TEMP1 = %d", TSC_CUT_RESOLUTION(TEMP1_VAL, s->precision) -
+              (s->noise & 5));
         return TSC_CUT_RESOLUTION(TEMP1_VAL, s->precision) -
                 (s->noise & 5);
     case 0x6:	/* TEMP2 */
         s->dav &= 0xdfff;
         s->dav &= ~mode_regs[TSC_MODE_TEMP2];
+        TRACE("TEMP2 = %d", TSC_CUT_RESOLUTION(TEMP2_VAL, s->precision) ^
+              (s->noise & 3));
         return TSC_CUT_RESOLUTION(TEMP2_VAL, s->precision) ^
                 (s->noise & 3);
 
@@ -149,31 +177,43 @@  static uint16_t tsc2005_read(TSC2005State *s, int reg)
         s->dav &= ~(mode_regs[TSC_MODE_X_TEST] | mode_regs[TSC_MODE_Y_TEST] |
                         mode_regs[TSC_MODE_TS_TEST]);
         s->reset = 1;
+        TRACE("STATUS = 0x%04x", ret);
         return ret;
 
     case 0x8:	/* AUX high treshold */
+        TRACE("AUX high threshold = 0x%04x", s->aux_thr[1]);
         return s->aux_thr[1];
     case 0x9:	/* AUX low treshold */
+        TRACE("AUX low threshold = 0x%04x", s->aux_thr[0]);
         return s->aux_thr[0];
 
     case 0xa:	/* TEMP high treshold */
+        TRACE("TEMP high threshold = 0x%04x", s->temp_thr[1]);
         return s->temp_thr[1];
     case 0xb:	/* TEMP low treshold */
+        TRACE("TEMP low threshold = 0x%04x", s->temp_thr[0]);
         return s->temp_thr[0];
 
     case 0xc:	/* CFR0 */
+        TRACE("CFR0 = 0x%04x", (s->pressure << 15) | ((!s->busy) << 14) |
+              (s->nextprecision << 13) | s->timing[0]);
         return (s->pressure << 15) | ((!s->busy) << 14) |
                 (s->nextprecision << 13) | s->timing[0]; 
     case 0xd:	/* CFR1 */
+        TRACE("CFR1 = 0x%04x", s->timing[1]);
         return s->timing[1];
     case 0xe:	/* CFR2 */
+        TRACE("CFR2 = 0x%04x", (s->pin_func << 14) | s->filter);
         return (s->pin_func << 14) | s->filter;
 
     case 0xf:	/* Function select status */
+        TRACE("function select status = 0x%04x",
+              s->function >= 0 ? 1 << s->function : 0);
         return s->function >= 0 ? 1 << s->function : 0;
     }
 
     /* Never gets here */
+    TRACE("unknown register = 0xffff");
     return 0xffff;
 }
 
@@ -181,46 +221,52 @@  static void tsc2005_write(TSC2005State *s, int reg, uint16_t data)
 {
     switch (reg) {
     case 0x8:	/* AUX high treshold */
+        TRACE("AUX high threshold = 0x%04x", data);
         s->aux_thr[1] = data;
         break;
     case 0x9:	/* AUX low treshold */
+        TRACE("AUX low threshold = 0x%04x", data);
         s->aux_thr[0] = data;
         break;
 
     case 0xa:	/* TEMP high treshold */
+        TRACE("TEMP high threshold = 0x%04x", data);
         s->temp_thr[1] = data;
         break;
     case 0xb:	/* TEMP low treshold */
+        TRACE("TEMP low threshold = 0x%04x", data);
         s->temp_thr[0] = data;
         break;
 
     case 0xc:	/* CFR0 */
+        TRACE("CFR0 = 0x%04x", data);
         s->host_mode = data >> 15;
         if (s->enabled != !(data & 0x4000)) {
             s->enabled = !(data & 0x4000);
-            fprintf(stderr, "%s: touchscreen sense %sabled\n",
-                            __FUNCTION__, s->enabled ? "en" : "dis");
-            if (s->busy && !s->enabled)
+            TRACE("touchscreen sense %sabled", s->enabled ? "en" : "dis");
+            if (s->busy && !s->enabled) {
                 qemu_del_timer(s->timer);
+            }
             s->busy &= s->enabled;
         }
         s->nextprecision = (data >> 13) & 1;
         s->timing[0] = data & 0x1fff;
-        if ((s->timing[0] >> 11) == 3)
-            fprintf(stderr, "%s: illegal conversion clock setting\n",
-                            __FUNCTION__);
+        if ((s->timing[0] >> 11) == 3) {
+            TRACE("illegal conversion clock setting");
+        }
         break;
     case 0xd:	/* CFR1 */
+        TRACE("CFR1 = 0x%04x", data);
         s->timing[1] = data & 0xf07;
         break;
     case 0xe:	/* CFR2 */
+        TRACE("CFR2 = 0x%04x", data);
         s->pin_func = (data >> 14) & 3;
         s->filter = data & 0x3fff;
         break;
 
     default:
-        fprintf(stderr, "%s: write into read-only register %x\n",
-                        __FUNCTION__, reg);
+        TRACE("write into read-only register 0x%x, value 0x%04x", reg, data);
     }
 }
 
@@ -228,29 +274,16 @@  static void tsc2005_write(TSC2005State *s, int reg, uint16_t data)
 static void tsc2005_pin_update(TSC2005State *s)
 {
     int64_t expires;
-    int pin_state;
-
-    switch (s->pin_func) {
-    case 0:
-        pin_state = !s->pressure && !!s->dav;
-        break;
-    case 1:
-    case 3:
-    default:
-        pin_state = !s->dav;
-        break;
-    case 2:
-        pin_state = !s->pressure;
-    }
-
-    if (pin_state != s->irq) {
-        s->irq = pin_state;
-        qemu_set_irq(s->pint, s->irq);
-    }
-
+    TRACE("nextf=%d, press=%d, ena=%d, busy=%d, dav=0x%04x, irq=%d",
+          s->nextfunction, s->pressure, s->enabled, s->busy, s->dav, s->irq);
     switch (s->nextfunction) {
     case TSC_MODE_XYZ_SCAN:
     case TSC_MODE_XY_SCAN:
+            if (!s->pin_func && !s->dav) {
+                TRACE("all values read, lowering irq");
+                s->irq = 0;
+                qemu_set_irq(s->pint, s->irq);
+            }
         if (!s->host_mode && s->dav)
             s->enabled = 0;
         if (!s->pressure)
@@ -283,15 +316,14 @@  static void tsc2005_pin_update(TSC2005State *s)
         return;
     }
 
-    if (!s->enabled || s->busy)
-        return;
-
-    s->busy = 1;
-    s->precision = s->nextprecision;
-    s->function = s->nextfunction;
-    s->pdst = !s->pnd0;	/* Synchronised on internal clock */
-    expires = qemu_get_clock(vm_clock) + (get_ticks_per_sec() >> 7);
-    qemu_mod_timer(s->timer, expires);
+    if (s->enabled && !s->busy) {
+        s->busy = 1;
+        s->precision = s->nextprecision;
+        s->function = s->nextfunction;
+        s->pdst = !s->pnd0;	/* Synchronised on internal clock */
+        expires = qemu_get_clock(vm_clock) + (get_ticks_per_sec() >> 7);
+        qemu_mod_timer(s->timer, expires);
+    }
 }
 
 static void tsc2005_reset(TSC2005State *s)
@@ -322,20 +354,20 @@  static uint8_t tsc2005_txrx_word(void *opaque, uint8_t value)
 {
     TSC2005State *s = opaque;
     uint32_t ret = 0;
-
+    TRACE("value = 0x%08x, state=%d", value, s->state + 1);
     switch (s->state ++) {
     case 0:
         if (value & 0x80) {
             /* Command */
-            if (value & (1 << 1))
+            if (value & (1 << 1)) {
                 tsc2005_reset(s);
-            else {
+            } else {
                 s->nextfunction = (value >> 3) & 0xf;
                 s->nextprecision = (value >> 2) & 1;
                 if (s->enabled != !(value & 1)) {
                     s->enabled = !(value & 1);
-                    fprintf(stderr, "%s: touchscreen sense %sabled\n",
-                                    __FUNCTION__, s->enabled ? "en" : "dis");
+                    TRACE("touchscreen sense %sabled",
+                          s->enabled ? "en" : "dis");
                     if (s->busy && !s->enabled)
                         qemu_del_timer(s->timer);
                     s->busy &= s->enabled;
@@ -386,7 +418,7 @@  static uint8_t tsc2005_txrx_word(void *opaque, uint8_t value)
 uint32_t tsc2005_txrx(void *opaque, uint32_t value, int len)
 {
     uint32_t ret = 0;
-
+    TRACE("value=0x%08x, len=%d", value, len);
     len &= ~7;
     while (len > 0) {
         len -= 8;
@@ -398,17 +430,44 @@  uint32_t tsc2005_txrx(void *opaque, uint32_t value, int len)
 
 static void tsc2005_timer_tick(void *opaque)
 {
+	int pin_state;
     TSC2005State *s = opaque;
 
     /* Timer ticked -- a set of conversions has been finished.  */
 
-    if (!s->busy)
+    if (!s->busy) {
+        TRACE("not busy -> exit");
         return;
+    }
 
-    s->busy = 0;
-    s->dav |= mode_regs[s->function];
-    s->function = -1;
-    tsc2005_pin_update(s);
+	switch (s->pin_func) {
+		case 0:
+			pin_state = s->pressure || !s->dav;
+			break;
+		case 1:
+		case 3:
+		default:
+			pin_state = !s->dav;
+			break;
+		case 2:
+			pin_state = s->pressure;
+    }
+	s->busy = 0;
+	if (!s->dav) {
+        TRACE("report new conversions ready");
+        s->dav |= mode_regs[s->function];
+    }
+    TRACE("pin_func=%d, pin_state=%d, pressure=%d, irq=%d, dav=0x%04x",
+          s->pin_func, pin_state, s->pressure, s->irq, s->dav);
+	s->function = -1;
+	tsc2005_pin_update(s);
+
+    if (pin_state != s->irq) {
+        TRACE("changing IRQ state to %d", pin_state);
+        s->irq = pin_state;
+        qemu_set_irq(s->pint, s->irq);
+    }
+    TRACE("done");
 }
 
 static void tsc2005_touchscreen_event(void *opaque,
@@ -428,8 +487,9 @@  static void tsc2005_touchscreen_event(void *opaque,
      * signaling TS events immediately, but for now we simulate
      * the first conversion delay for sake of correctness.
      */
-    if (p != s->pressure)
+    if (p != s->pressure) {
         tsc2005_pin_update(s);
+    }
 }
 
 static void tsc2005_save(QEMUFile *f, void *opaque)
@@ -541,6 +601,9 @@  void *tsc2005_init(qemu_irq pintdav)
     s->tr[5] = 0;
     s->tr[6] = 1;
     s->tr[7] = 0;
+    
+    s->z1_cons = 400;
+    s->z2_cons = 4000;
 
     tsc2005_reset(s);
 
@@ -558,7 +621,8 @@  void *tsc2005_init(qemu_irq pintdav)
  * from the touchscreen.  Assuming 12-bit precision was used during
  * tslib calibration.
  */
-void tsc2005_set_transform(void *opaque, MouseTransformInfo *info)
+void tsc2005_set_transform(void *opaque, MouseTransformInfo *info,
+                           int z1_cons, int z2_cons)
 {
     TSC2005State *s = (TSC2005State *) opaque;
 
@@ -590,4 +654,7 @@  void tsc2005_set_transform(void *opaque, MouseTransformInfo *info)
     s->tr[4] >>= 11;
     s->tr[5] >>= 11;
     s->tr[7] <<= 4;
+    
+    s->z1_cons = z1_cons;
+    s->z2_cons = z2_cons;
 }