Input: st1232 - wait until device is ready before reading resolution
authorGeert Uytterhoeven <geert+renesas@glider.be>
Sat, 2 Jan 2021 06:15:15 +0000 (22:15 -0800)
committerDmitry Torokhov <dmitry.torokhov@gmail.com>
Sat, 2 Jan 2021 06:19:03 +0000 (22:19 -0800)
According to the st1232 datasheet, the host has to wait for the device
to change into Normal state before accessing registers other than the
Status Register.

If the reset GPIO is wired, the device is powered on during driver
probe, just before reading the resolution.  However, the latter may
happen before the device is ready, leading to a probe failure:

    st1232-ts 1-0055: Failed to read resolution: -6

Fix this by waiting until the device is ready, by trying to read the
Status Register until it indicates so, or until timeout.

On Armadillo 800 EVA, typically the first read fails with an I2C
transfer error, while the second read indicates the device is ready.

Fixes: 3a54a215410b1650 ("Input: st1232 - add support resolution reading")
Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be>
Link: https://lore.kernel.org/r/20201229162601.2154566-4-geert+renesas@glider.be
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
drivers/input/touchscreen/st1232.c

index 459701056f2bda961179c35d8cafdb43862ef23f..b4e7bcbe9b91d9c5e527ccb4d773e20ae4b8d847 100644 (file)
 #define ST1232_TS_NAME "st1232-ts"
 #define ST1633_TS_NAME "st1633-ts"
 
+#define REG_STATUS             0x01    /* Device Status | Error Code */
+
+#define STATUS_NORMAL          0x00
+#define STATUS_INIT            0x01
+#define STATUS_ERROR           0x02
+#define STATUS_AUTO_TUNING     0x03
+#define STATUS_IDLE            0x04
+#define STATUS_POWER_DOWN      0x05
+
+#define ERROR_NONE             0x00
+#define ERROR_INVALID_ADDRESS  0x10
+#define ERROR_INVALID_VALUE    0x20
+#define ERROR_INVALID_PLATFORM 0x30
+
 #define REG_XY_RESOLUTION      0x04
 #define REG_XY_COORDINATES     0x12
 #define ST_TS_MAX_FINGERS      10
@@ -73,6 +87,22 @@ static int st1232_ts_read_data(struct st1232_ts_data *ts, u8 reg,
        return 0;
 }
 
+static int st1232_ts_wait_ready(struct st1232_ts_data *ts)
+{
+       unsigned int retries;
+       int error;
+
+       for (retries = 10; retries; retries--) {
+               error = st1232_ts_read_data(ts, REG_STATUS, 1);
+               if (!error && ts->read_buf[0] == (STATUS_NORMAL | ERROR_NONE))
+                       return 0;
+
+               usleep_range(1000, 2000);
+       }
+
+       return -ENXIO;
+}
+
 static int st1232_ts_read_resolution(struct st1232_ts_data *ts, u16 *max_x,
                                     u16 *max_y)
 {
@@ -252,6 +282,11 @@ static int st1232_ts_probe(struct i2c_client *client,
        input_dev->name = "st1232-touchscreen";
        input_dev->id.bustype = BUS_I2C;
 
+       /* Wait until device is ready */
+       error = st1232_ts_wait_ready(ts);
+       if (error)
+               return error;
+
        /* Read resolution from the chip */
        error = st1232_ts_read_resolution(ts, &max_x, &max_y);
        if (error) {