synaptics_s3708: cleanup retry logic

Support was added in commit 72638ba to recognize i2c bus errors (e.g.,
unknown slave addresses). Now, I can remove some code that was used to
probe whether the touch controller was on the bus or not. This reduces
latency and makes the code more readable.

Bug: 30595437
Change-Id: Id064d2ff565aba1fabc7eb73866a6905f09c8713
diff --git a/firmware/src/drivers/synaptics_s3708/synaptics_s3708.c b/firmware/src/drivers/synaptics_s3708/synaptics_s3708.c
index 3aec4d1..04288cb 100644
--- a/firmware/src/drivers/synaptics_s3708/synaptics_s3708.c
+++ b/firmware/src/drivers/synaptics_s3708/synaptics_s3708.c
@@ -42,8 +42,6 @@
 #define I2C_SPEED                   400000
 #define I2C_ADDR                    0x20
 
-#define S3708_ID                    0x34
-
 #define S3708_REG_PAGE_SELECT       0xFF
 
 #define S3708_REG_F01_DATA_BASE     0x06
@@ -62,8 +60,6 @@
 #define S3708_REPORT_MODE_CONT      0x00
 #define S3708_REPORT_MODE_LPWG      0x02
 
-#define S3708_REG_PDT_BASE          0xee
-
 #define MAX_PENDING_I2C_REQUESTS    4
 #define MAX_I2C_TRANSFER_SIZE       8
 #define MAX_I2C_RETRY_DELAY         250000000ull // 250 milliseconds
@@ -100,7 +96,6 @@
 {
     STATE_ENABLE_0,
     STATE_ENABLE_1,
-    STATE_ENABLE_2,
     STATE_DISABLE_0,
     STATE_INT_HANDLE_0,
     STATE_INT_HANDLE_1,
@@ -165,8 +160,13 @@
     xfer->err = err;
 
     osEnqueuePrivateEvt(EVT_SENSOR_I2C, cookie, NULL, mTask.id);
-    if (err != 0)
+    // Do not print error for ENXIO since we expect there to be times where we
+    // cannot talk to the touch controller.
+    if (err == -ENXIO) {
+        DEBUG_PRINT("i2c error (tx: %d, rx: %d, err: %d)\n", tx, rx, err);
+    } else if (err != 0) {
         ERROR_PRINT("i2c error (tx: %d, rx: %d, err: %d)\n", tx, rx, err);
+    }
 }
 
 static void retryTimerCallback(uint32_t timerId, void *cookie)
@@ -253,6 +253,19 @@
     return false;
 }
 
+static void setRetryTimer()
+{
+    mTask.retryCnt++;
+    if (mTask.retryCnt < MAX_I2C_RETRY_COUNT) {
+        mTask.retryTimerHandle = timTimerSet(MAX_I2C_RETRY_DELAY, 0, 50, retryTimerCallback, NULL, true);
+        if (!mTask.retryTimerHandle) {
+            ERROR_PRINT("failed to allocate timer");
+        }
+    } else {
+        ERROR_PRINT("could not communicate with touch controller");
+    }
+}
+
 static bool callbackPower(bool on, void *cookie)
 {
     bool ret;
@@ -324,53 +337,31 @@
     .sensorFlush = callbackFlush,
 };
 
-static void handleI2cEvent(struct I2cTransfer *xfer)
+static void processI2cResponse(struct I2cTransfer *xfer)
 {
     struct I2cTransfer *nextXfer;
     union EmbeddedDataPoint sample;
 
     switch (xfer->state) {
         case STATE_ENABLE_0:
-            // Disable sleep
             setSleepEnable(false, STATE_ENABLE_1);
             break;
 
         case STATE_ENABLE_1:
-            // Verify that we can talk to the chip
-            nextXfer = allocXfer(STATE_ENABLE_2);
-            if (nextXfer != NULL) {
-                nextXfer->txrxBuf[0] = S3708_REG_PDT_BASE;
-                performXfer(nextXfer, 1, 1);
-            }
-            break;
-
-        case STATE_ENABLE_2:
-            DEBUG_PRINT("ID: 0x%02x", xfer->txrxBuf[0]);
-
             // HACK: DozeService reactivates pickup gesture before the screen
             // comes on, so we need to wait for some time after enabling before
             // trying to talk to touch controller. We may see the touch
             // controller on the first few samples and then have communication
             // switched off. So, wait HACK_RETRY_SKIP_COUNT samples before we
             // consider the transaction.
-            if ((mTask.retryCnt < HACK_RETRY_SKIP_COUNT) || (xfer->err != 0) || (xfer->txrxBuf[0] != S3708_ID)) {
-                mTask.retryCnt++;
-                if (mTask.retryCnt < MAX_I2C_RETRY_COUNT) {
-                    mTask.retryTimerHandle = timTimerSet(MAX_I2C_RETRY_DELAY, 0, 50, retryTimerCallback, NULL, true);
-                    if (!mTask.retryTimerHandle) {
-                        ERROR_PRINT("failed to allocate timer");
-                    }
-                } else {
-                    ERROR_PRINT("could not communicate with touch controller");
-                }
+            if (mTask.retryCnt < HACK_RETRY_SKIP_COUNT) {
+                setRetryTimer();
             } else {
-                // Set reporting control to lpwg mode
                 setReportingMode(S3708_REPORT_MODE_LPWG, STATE_IDLE);
             }
             break;
 
         case STATE_DISABLE_0:
-            // Enable sleep
             setSleepEnable(true, STATE_IDLE);
             break;
 
@@ -397,6 +388,15 @@
         default:
             break;
     }
+}
+
+static void handleI2cEvent(struct I2cTransfer *xfer)
+{
+    if (xfer->err == 0) {
+        processI2cResponse(xfer);
+    } else if (xfer->state == STATE_ENABLE_0 || xfer->state == STATE_ENABLE_1) {
+        setRetryTimer();
+    }
 
     xfer->inUse = false;
 }