[PATCH v3 3/3] ARM: tegra: add mXT1386 compatible

2020-09-30 Thread Jiada Wang
Add mXT1386 compatible for "touchscreen@4c".

Signed-off-by: Jiada Wang 
---
 arch/arm/boot/dts/tegra20-acer-a500-picasso.dts | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/arm/boot/dts/tegra20-acer-a500-picasso.dts 
b/arch/arm/boot/dts/tegra20-acer-a500-picasso.dts
index 2d683c9a1a5d..a9eed5f6973b 100644
--- a/arch/arm/boot/dts/tegra20-acer-a500-picasso.dts
+++ b/arch/arm/boot/dts/tegra20-acer-a500-picasso.dts
@@ -428,7 +428,7 @@
};
 
touchscreen@4c {
-   compatible = "atmel,maxtouch";
+   compatible = "atmel,mxt1386", "atmel,maxtouch";
reg = <0x4c>;
 
atmel,cfg_name = "maxtouch-acer-iconia-tab-a500.cfg";
-- 
2.17.1



[PATCH v3 0/3] implement I2C retries for mXT1368

2020-09-30 Thread Jiada Wang
According to datasheet, mXT1386 chip has a WAKE line, it is used
to wake the chip up from deep sleep mode before communicating with
it via the I2C-compatible interface.

if the WAKE line is connected to a GPIO line, the line must be
asserted 25 ms before the host attempts to communicate with the
mXT1386.
If the WAKE line is connected to the SCL pin, the mXT1386 will send
a NACK on the first attempt to address it, the host must then retry
25 ms later.

This patch adds compatible string "atmel,mXT1386" for mXT1386 controller,
when I2C transfer on mXT1386 fails, retry the transfer once after a
25 ms sleep.


Jiada Wang (3):
  dt-bindings: input: atmel: add compatible for mXT1386
  Input: atmel_mxt_ts - implement I2C retries for mXT1368
  ARM: tegra: add mXT1386 compatible

---
v3:
change compatible string to lowercase

v2:
add bool retry_i2c_transfers to struct mxt_data,
to indicate whether retry is needed when i2c transfer fails

v1: initial version
---
 .../bindings/input/atmel,maxtouch.txt |  1 +
 .../boot/dts/tegra20-acer-a500-picasso.dts|  2 +-
 drivers/input/touchscreen/atmel_mxt_ts.c  | 62 +++
 3 files changed, 52 insertions(+), 13 deletions(-)

-- 
2.17.1



[PATCH v3 2/3] Input: atmel_mxt_ts - implement I2C retries for mXT1368

2020-09-30 Thread Jiada Wang
According to datasheet, mXT1386 chip has a WAKE line, it is used
to wake the chip up from deep sleep mode before communicating with
it via the I2C-compatible interface.

if the WAKE line is connected to a GPIO line, the line must be
asserted 25 ms before the host attempts to communicate with the mXT1386.
If the WAKE line is connected to the SCL pin, the mXT1386 will send a
NACK on the first attempt to address it, the host must then retry 25 ms
later.

This patch adds compatible string "atmel,mxt1386" for mXT1386 controller,
when I2C transfer on mXT1386 fails, retry the transfer once after a 25 ms
sleep.

Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 62 +++-
 1 file changed, 50 insertions(+), 12 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 98f17fa3a892..103c3359f3df 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -198,6 +198,7 @@ enum t100_type {
 #define MXT_CRC_TIMEOUT1000/* msec */
 #define MXT_FW_RESET_TIME  3000/* msec */
 #define MXT_FW_CHG_TIMEOUT 300 /* msec */
+#define MXT_WAKEUP_TIME25  /* msec */
 
 /* Command to unlock bootloader */
 #define MXT_UNLOCK_CMD_MSB 0xaa
@@ -340,6 +341,9 @@ struct mxt_data {
unsigned int t19_num_keys;
 
enum mxt_suspend_mode suspend_mode;
+
+   /* Indicates whether retry is needed when i2c transfer fails */
+   bool retry_i2c_transfers;
 };
 
 struct mxt_vb2_buffer {
@@ -627,7 +631,9 @@ static int mxt_send_bootloader_cmd(struct mxt_data *data, 
bool unlock)
 static int __mxt_read_reg(struct i2c_client *client,
   u16 reg, u16 len, void *val)
 {
+   struct mxt_data *data = i2c_get_clientdata(client);
struct i2c_msg xfer[2];
+   bool retried = false;
u8 buf[2];
int ret;
 
@@ -646,22 +652,30 @@ static int __mxt_read_reg(struct i2c_client *client,
xfer[1].len = len;
xfer[1].buf = val;
 
-   ret = i2c_transfer(client->adapter, xfer, 2);
-   if (ret == 2) {
-   ret = 0;
-   } else {
-   if (ret >= 0)
-   ret = -EIO;
+retry_read:
+   ret = i2c_transfer(client->adapter, xfer, ARRAY_SIZE(xfer));
+   if (ret != ARRAY_SIZE(xfer)) {
+   if (data->retry_i2c_transfers && !retried) {
+   dev_dbg(>dev, "i2c retry\n");
+   /* TODO: add WAKE-GPIO support */
+   msleep(MXT_WAKEUP_TIME);
+   retried = true;
+   goto retry_read;
+   }
+   ret = ret < 0 ? ret : -EIO;
dev_err(>dev, "%s: i2c transfer failed (%d)\n",
__func__, ret);
+   return ret;
}
 
-   return ret;
+   return 0;
 }
 
 static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
   const void *val)
 {
+   struct mxt_data *data = i2c_get_clientdata(client);
+   bool retried = false;
u8 *buf;
size_t count;
int ret;
@@ -675,14 +689,21 @@ static int __mxt_write_reg(struct i2c_client *client, u16 
reg, u16 len,
buf[1] = (reg >> 8) & 0xff;
memcpy([2], val, len);
 
+retry_write:
ret = i2c_master_send(client, buf, count);
-   if (ret == count) {
-   ret = 0;
-   } else {
-   if (ret >= 0)
-   ret = -EIO;
+   if (ret != count) {
+   if (data->retry_i2c_transfers && !retried) {
+   dev_dbg(>dev, "i2c retry\n");
+   /* TODO: add WAKE-GPIO support */
+   msleep(MXT_WAKEUP_TIME);
+   retried = true;
+   goto retry_write;
+   }
+   ret = ret < 0 ? ret : -EIO;
dev_err(>dev, "%s: i2c send failed (%d)\n",
__func__, ret);
+   } else {
+   ret = 0;
}
 
kfree(buf);
@@ -3084,6 +3105,7 @@ static const struct dmi_system_id 
chromebook_T9_suspend_dmi[] = {
 
 static int mxt_probe(struct i2c_client *client, const struct i2c_device_id *id)
 {
+   struct device_node *np = client->dev.of_node;
struct mxt_data *data;
int error;
 
@@ -3158,6 +3180,20 @@ static int mxt_probe(struct i2c_client *client, const 
struct i2c_device_id *id)
msleep(MXT_RESET_INVALID_CHG);
}
 
+   /*
+* The mXT1386 has a dedicated WAKE-line that could be connected to a
+* dedicated GPIO, or to the I2C SCL pin, or permanently asserted LOW.
+* It's used for waking controller from a deep-sleep and it needs to be
+* asserted LOW for 25 millisecon

[PATCH v3 1/3] dt-bindings: input: atmel: add compatible for mXT1386

2020-09-30 Thread Jiada Wang
Document the mXT1386 compatible string.

Signed-off-by: Jiada Wang 
---
 Documentation/devicetree/bindings/input/atmel,maxtouch.txt | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Documentation/devicetree/bindings/input/atmel,maxtouch.txt 
b/Documentation/devicetree/bindings/input/atmel,maxtouch.txt
index c88919480d37..fdff8a4cf5c0 100644
--- a/Documentation/devicetree/bindings/input/atmel,maxtouch.txt
+++ b/Documentation/devicetree/bindings/input/atmel,maxtouch.txt
@@ -3,6 +3,7 @@ Atmel maXTouch touchscreen/touchpad
 Required properties:
 - compatible:
 atmel,maxtouch
+atmel,mxt1386
 
 The following compatibles have been used in various products but are
 deprecated:
-- 
2.17.1



[PATCH v2 2/3] Input: atmel_mxt_ts - implement I2C retries for mXT1368

2020-09-25 Thread Jiada Wang
According to datasheet, mXT1386 chip has a WAKE line, it is used
to wake the chip up from deep sleep mode before communicating with
it via the I2C-compatible interface.

if the WAKE line is connected to a GPIO line, the line must be
asserted 25 ms before the host attempts to communicate with the mXT1386.
If the WAKE line is connected to the SCL pin, the mXT1386 will send a
NACK on the first attempt to address it, the host must then retry 25 ms
later.

This patch adds compatible string "atmel,mXT1386" for mXT1386 controller,
when I2C transfer on mXT1386 fails, retry the transfer once after a 25 ms
sleep.

Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 62 +++-
 1 file changed, 50 insertions(+), 12 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 98f17fa3a892..c00743caed27 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -198,6 +198,7 @@ enum t100_type {
 #define MXT_CRC_TIMEOUT1000/* msec */
 #define MXT_FW_RESET_TIME  3000/* msec */
 #define MXT_FW_CHG_TIMEOUT 300 /* msec */
+#define MXT_WAKEUP_TIME25  /* msec */
 
 /* Command to unlock bootloader */
 #define MXT_UNLOCK_CMD_MSB 0xaa
@@ -340,6 +341,9 @@ struct mxt_data {
unsigned int t19_num_keys;
 
enum mxt_suspend_mode suspend_mode;
+
+   /* Indicates whether retry is needed when i2c transfer fails */
+   bool retry_i2c_transfers;
 };
 
 struct mxt_vb2_buffer {
@@ -627,7 +631,9 @@ static int mxt_send_bootloader_cmd(struct mxt_data *data, 
bool unlock)
 static int __mxt_read_reg(struct i2c_client *client,
   u16 reg, u16 len, void *val)
 {
+   struct mxt_data *data = i2c_get_clientdata(client);
struct i2c_msg xfer[2];
+   bool retried = false;
u8 buf[2];
int ret;
 
@@ -646,22 +652,30 @@ static int __mxt_read_reg(struct i2c_client *client,
xfer[1].len = len;
xfer[1].buf = val;
 
-   ret = i2c_transfer(client->adapter, xfer, 2);
-   if (ret == 2) {
-   ret = 0;
-   } else {
-   if (ret >= 0)
-   ret = -EIO;
+retry_read:
+   ret = i2c_transfer(client->adapter, xfer, ARRAY_SIZE(xfer));
+   if (ret != ARRAY_SIZE(xfer)) {
+   if (data->retry_i2c_transfers && !retried) {
+   dev_dbg(>dev, "i2c retry\n");
+   /* TODO: add WAKE-GPIO support */
+   msleep(MXT_WAKEUP_TIME);
+   retried = true;
+   goto retry_read;
+   }
+   ret = ret < 0 ? ret : -EIO;
dev_err(>dev, "%s: i2c transfer failed (%d)\n",
__func__, ret);
+   return ret;
}
 
-   return ret;
+   return 0;
 }
 
 static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
   const void *val)
 {
+   struct mxt_data *data = i2c_get_clientdata(client);
+   bool retried = false;
u8 *buf;
size_t count;
int ret;
@@ -675,14 +689,21 @@ static int __mxt_write_reg(struct i2c_client *client, u16 
reg, u16 len,
buf[1] = (reg >> 8) & 0xff;
memcpy([2], val, len);
 
+retry_write:
ret = i2c_master_send(client, buf, count);
-   if (ret == count) {
-   ret = 0;
-   } else {
-   if (ret >= 0)
-   ret = -EIO;
+   if (ret != count) {
+   if (data->retry_i2c_transfers && !retried) {
+   dev_dbg(>dev, "i2c retry\n");
+   /* TODO: add WAKE-GPIO support */
+   msleep(MXT_WAKEUP_TIME);
+   retried = true;
+   goto retry_write;
+   }
+   ret = ret < 0 ? ret : -EIO;
dev_err(>dev, "%s: i2c send failed (%d)\n",
__func__, ret);
+   } else {
+   ret = 0;
}
 
kfree(buf);
@@ -3084,6 +3105,7 @@ static const struct dmi_system_id 
chromebook_T9_suspend_dmi[] = {
 
 static int mxt_probe(struct i2c_client *client, const struct i2c_device_id *id)
 {
+   struct device_node *np = client->dev.of_node;
struct mxt_data *data;
int error;
 
@@ -3158,6 +3180,20 @@ static int mxt_probe(struct i2c_client *client, const 
struct i2c_device_id *id)
msleep(MXT_RESET_INVALID_CHG);
}
 
+   /*
+* The mXT1386 has a dedicated WAKE-line that could be connected to a
+* dedicated GPIO, or to the I2C SCL pin, or permanently asserted LOW.
+* It's used for waking controller from a deep-sleep and it needs to be
+* asserted LOW for 25 millisecon

[PATCH v2 0/3] implement I2C retries for mXT1368

2020-09-25 Thread Jiada Wang
According to datasheet, mXT1386 chip has a WAKE line, it is used
to wake the chip up from deep sleep mode before communicating with
it via the I2C-compatible interface.

if the WAKE line is connected to a GPIO line, the line must be
asserted 25 ms before the host attempts to communicate with the
mXT1386.
If the WAKE line is connected to the SCL pin, the mXT1386 will send
a NACK on the first attempt to address it, the host must then retry
25 ms later.

This patch adds compatible string "atmel,mXT1386" for mXT1386 controller,
when I2C transfer on mXT1386 fails, retry the transfer once after a
25 ms sleep.


Jiada Wang (3):
  dt-bindings: input: atmel: add compatible for mXT1386
  Input: atmel_mxt_ts - implement I2C retries for mXT1368
  ARM: tegra: add mXT1386 compatible

---
v2:
add bool retry_i2c_transfers to struct mxt_data,
to indicate whether retry is needed when i2c transfer fails

v1: initial version
---
 .../bindings/input/atmel,maxtouch.txt |  1 +
 .../boot/dts/tegra20-acer-a500-picasso.dts|  2 +-
 drivers/input/touchscreen/atmel_mxt_ts.c  | 62 +++
 3 files changed, 52 insertions(+), 13 deletions(-)

-- 
2.17.1



[PATCH v2 1/3] dt-bindings: input: atmel: add compatible for mXT1386

2020-09-25 Thread Jiada Wang
Document the mXT1386 compatible string.

Signed-off-by: Jiada Wang 
---
 Documentation/devicetree/bindings/input/atmel,maxtouch.txt | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Documentation/devicetree/bindings/input/atmel,maxtouch.txt 
b/Documentation/devicetree/bindings/input/atmel,maxtouch.txt
index c88919480d37..c13fc0f3f00b 100644
--- a/Documentation/devicetree/bindings/input/atmel,maxtouch.txt
+++ b/Documentation/devicetree/bindings/input/atmel,maxtouch.txt
@@ -3,6 +3,7 @@ Atmel maXTouch touchscreen/touchpad
 Required properties:
 - compatible:
 atmel,maxtouch
+atmel,mXT1386
 
 The following compatibles have been used in various products but are
 deprecated:
-- 
2.17.1



[PATCH v2 3/3] ARM: tegra: add mXT1386 compatible

2020-09-25 Thread Jiada Wang
Add mXT1386 compatible for "touchscreen@4c".

Signed-off-by: Jiada Wang 
---
 arch/arm/boot/dts/tegra20-acer-a500-picasso.dts | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/arm/boot/dts/tegra20-acer-a500-picasso.dts 
b/arch/arm/boot/dts/tegra20-acer-a500-picasso.dts
index 2d683c9a1a5d..7915b6e9190e 100644
--- a/arch/arm/boot/dts/tegra20-acer-a500-picasso.dts
+++ b/arch/arm/boot/dts/tegra20-acer-a500-picasso.dts
@@ -428,7 +428,7 @@
};
 
touchscreen@4c {
-   compatible = "atmel,maxtouch";
+   compatible = "atmel,mXT1386", "atmel,maxtouch";
reg = <0x4c>;
 
atmel,cfg_name = "maxtouch-acer-iconia-tab-a500.cfg";
-- 
2.17.1



[PATCH v1 2/3] Input: atmel_mxt_ts - implement I2C retries for mXT1368

2020-09-21 Thread Jiada Wang
According to datasheet, mXT1386 chip has a WAKE line, it is used
to wake the chip up from deep sleep mode before communicating with
it via the I2C-compatible interface.

if the WAKE line is connected to a GPIO line, the line must be
asserted 25 ms before the host attempts to communicate with the mXT1386.
If the WAKE line is connected to the SCL pin, the mXT1386 will send a
NACK on the first attempt to address it, the host must then retry 25 ms
later.

This patch adds compatible string "atmel,mXT1386" for mXT1386 controller,
when I2C transfer on mXT1386 fails, retry the transfer once after a 25 ms
sleep.

Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 44 +---
 1 file changed, 32 insertions(+), 12 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 98f17fa3a892..96d5f4d64cb0 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -198,6 +198,7 @@ enum t100_type {
 #define MXT_CRC_TIMEOUT1000/* msec */
 #define MXT_FW_RESET_TIME  3000/* msec */
 #define MXT_FW_CHG_TIMEOUT 300 /* msec */
+#define MXT_WAKEUP_TIME25  /* msec */
 
 /* Command to unlock bootloader */
 #define MXT_UNLOCK_CMD_MSB 0xaa
@@ -627,7 +628,9 @@ static int mxt_send_bootloader_cmd(struct mxt_data *data, 
bool unlock)
 static int __mxt_read_reg(struct i2c_client *client,
   u16 reg, u16 len, void *val)
 {
+   struct device_node *np = client->dev.of_node;
struct i2c_msg xfer[2];
+   bool retried = false;
u8 buf[2];
int ret;
 
@@ -646,22 +649,30 @@ static int __mxt_read_reg(struct i2c_client *client,
xfer[1].len = len;
xfer[1].buf = val;
 
-   ret = i2c_transfer(client->adapter, xfer, 2);
-   if (ret == 2) {
-   ret = 0;
-   } else {
-   if (ret >= 0)
-   ret = -EIO;
+retry_read:
+   ret = i2c_transfer(client->adapter, xfer, ARRAY_SIZE(xfer));
+   if (ret != ARRAY_SIZE(xfer)) {
+   if (of_device_is_compatible(np, "atmel,mXT1386") && !retried) {
+   dev_dbg(>dev, "i2c retry\n");
+   /* TODO: add WAKE-GPIO support */
+   msleep(MXT_WAKEUP_TIME);
+   retried = true;
+   goto retry_read;
+   }
+   ret = ret < 0 ? ret : -EIO;
dev_err(>dev, "%s: i2c transfer failed (%d)\n",
__func__, ret);
+   return ret;
}
 
-   return ret;
+   return 0;
 }
 
 static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
   const void *val)
 {
+   struct device_node *np = client->dev.of_node;
+   bool retried = false;
u8 *buf;
size_t count;
int ret;
@@ -675,14 +686,21 @@ static int __mxt_write_reg(struct i2c_client *client, u16 
reg, u16 len,
buf[1] = (reg >> 8) & 0xff;
memcpy([2], val, len);
 
+retry_write:
ret = i2c_master_send(client, buf, count);
-   if (ret == count) {
-   ret = 0;
-   } else {
-   if (ret >= 0)
-   ret = -EIO;
+   if (ret != count) {
+   if (of_device_is_compatible(np, "atmel,mXT1386") && !retried) {
+   dev_dbg(>dev, "i2c retry\n");
+   /* TODO: add WAKE-GPIO support */
+   msleep(MXT_WAKEUP_TIME);
+   retried = true;
+   goto retry_write;
+   }
+   ret = ret < 0 ? ret : -EIO;
dev_err(>dev, "%s: i2c send failed (%d)\n",
__func__, ret);
+   } else {
+   ret = 0;
}
 
kfree(buf);
@@ -3235,6 +3253,7 @@ static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, 
mxt_resume);
 
 static const struct of_device_id mxt_of_match[] = {
{ .compatible = "atmel,maxtouch", },
+   { .compatible = "atmel,mXT1386", },
/* Compatibles listed below are deprecated */
{ .compatible = "atmel,qt602240_ts", },
{ .compatible = "atmel,atmel_mxt_ts", },
@@ -3259,6 +3278,7 @@ static const struct i2c_device_id mxt_id[] = {
{ "atmel_mxt_tp", 0 },
{ "maxtouch", 0 },
{ "mXT224", 0 },
+   { "mXT1386", 0 },
{ }
 };
 MODULE_DEVICE_TABLE(i2c, mxt_id);
-- 
2.17.1



[PATCH v1 3/3] ARM: tegra: add mXT1386 compatible

2020-09-21 Thread Jiada Wang
Add mXT1386 compatible for "touchscreen@4c".

Signed-off-by: Jiada Wang 
---
 arch/arm/boot/dts/tegra20-acer-a500-picasso.dts | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/arm/boot/dts/tegra20-acer-a500-picasso.dts 
b/arch/arm/boot/dts/tegra20-acer-a500-picasso.dts
index 2d683c9a1a5d..7915b6e9190e 100644
--- a/arch/arm/boot/dts/tegra20-acer-a500-picasso.dts
+++ b/arch/arm/boot/dts/tegra20-acer-a500-picasso.dts
@@ -428,7 +428,7 @@
};
 
touchscreen@4c {
-   compatible = "atmel,maxtouch";
+   compatible = "atmel,mXT1386", "atmel,maxtouch";
reg = <0x4c>;
 
atmel,cfg_name = "maxtouch-acer-iconia-tab-a500.cfg";
-- 
2.17.1



[PATCH v1 1/3] dt-bindings: input: atmel: add compatible for mXT1386

2020-09-21 Thread Jiada Wang
Document the mXT1386 compatible string.

Signed-off-by: Jiada Wang 
---
 Documentation/devicetree/bindings/input/atmel,maxtouch.txt | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Documentation/devicetree/bindings/input/atmel,maxtouch.txt 
b/Documentation/devicetree/bindings/input/atmel,maxtouch.txt
index c88919480d37..c13fc0f3f00b 100644
--- a/Documentation/devicetree/bindings/input/atmel,maxtouch.txt
+++ b/Documentation/devicetree/bindings/input/atmel,maxtouch.txt
@@ -3,6 +3,7 @@ Atmel maXTouch touchscreen/touchpad
 Required properties:
 - compatible:
 atmel,maxtouch
+atmel,mXT1386
 
 The following compatibles have been used in various products but are
 deprecated:
-- 
2.17.1



[PATCH v1 1/2] dt-bindings: input: atmel: add compatible for mXT1386

2020-09-18 Thread Jiada Wang
Document the mXT1386  compatible string.

Signed-off-by: Jiada Wang 
---
 Documentation/devicetree/bindings/input/atmel,maxtouch.txt | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Documentation/devicetree/bindings/input/atmel,maxtouch.txt 
b/Documentation/devicetree/bindings/input/atmel,maxtouch.txt
index c88919480d37..c13fc0f3f00b 100644
--- a/Documentation/devicetree/bindings/input/atmel,maxtouch.txt
+++ b/Documentation/devicetree/bindings/input/atmel,maxtouch.txt
@@ -3,6 +3,7 @@ Atmel maXTouch touchscreen/touchpad
 Required properties:
 - compatible:
 atmel,maxtouch
+atmel,mXT1386
 
 The following compatibles have been used in various products but are
 deprecated:
-- 
2.17.1



[PATCH v2 2/2] Input: atmel_mxt_ts - wake mXT1386 from deep-sleep mode

2020-09-18 Thread Jiada Wang
According to datasheet, mXT1386 chip has a WAKE line, it is used
to wake the chip up from deep sleep mode before communicating with
it via the I2C-compatible interface.

if the WAKE line is connected to a GPIO line, the line must be
asserted 25 ms before the host attempts to communicate with the mXT1386.
If the WAKE line is connected to the SCL pin, the mXT1386 will send a
NACK on the first attempt to address it, the host must then retry 25 ms
later.

This patch introduces mxt_wake() which does a dummy i2c read, follows
with a 25 ms sleep for mXT1386 chip. mxt_wake() is added to
mxt_initialize(), mxt_load_fw() and mxt_start() to ensure before any
actual i2c transfer, mxt_wake() can be executed.

Added new compatible string "atmel,mXT1386".

Signed-off-by: Jiada Wang 
Suggested-by: Dmitry Osipenko 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 27 
 1 file changed, 27 insertions(+)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index a2189739e30f..d580050a237f 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -196,6 +196,7 @@ enum t100_type {
 #define MXT_CRC_TIMEOUT1000/* msec */
 #define MXT_FW_RESET_TIME  3000/* msec */
 #define MXT_FW_CHG_TIMEOUT 300 /* msec */
+#define MXT_WAKEUP_TIME 25  /* msec */
 
 /* Command to unlock bootloader */
 #define MXT_UNLOCK_CMD_MSB 0xaa
@@ -2099,12 +2100,33 @@ static void mxt_config_cb(const struct firmware *cfg, 
void *ctx)
release_firmware(cfg);
 }
 
+static void mxt_wake(struct mxt_data *data)
+{
+   struct i2c_client *client = data->client;
+   struct device *dev = >client->dev;
+   struct device_node *np = dev->of_node;
+   union i2c_smbus_data dummy;
+
+   if (!of_device_is_compatible(np, "atmel,mXT1386"))
+   return;
+
+   /* TODO: add WAKE-GPIO support */
+
+   i2c_smbus_xfer(client->adapter, client->addr,
+  0, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE,
+  );
+
+   msleep(MXT_WAKEUP_TIME);
+}
+
 static int mxt_initialize(struct mxt_data *data)
 {
struct i2c_client *client = data->client;
int recovery_attempts = 0;
int error;
 
+   mxt_wake(data);
+
while (1) {
error = mxt_read_info_block(data);
if (!error)
@@ -2787,6 +2809,8 @@ static int mxt_load_fw(struct device *dev, const char *fn)
if (ret)
goto release_firmware;
 
+   mxt_wake(data);
+
if (!data->in_bootloader) {
/* Change to the bootloader mode */
data->in_bootloader = true;
@@ -2928,6 +2952,7 @@ static const struct attribute_group mxt_attr_group = {
 
 static void mxt_start(struct mxt_data *data)
 {
+   mxt_wake(data);
switch (data->suspend_mode) {
case MXT_SUSPEND_T9_CTRL:
mxt_soft_reset(data);
@@ -3185,6 +3210,7 @@ static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, 
mxt_resume);
 
 static const struct of_device_id mxt_of_match[] = {
{ .compatible = "atmel,maxtouch", },
+   { .compatible = "atmel,mXT1386", },
/* Compatibles listed below are deprecated */
{ .compatible = "atmel,qt602240_ts", },
{ .compatible = "atmel,atmel_mxt_ts", },
@@ -3209,6 +3235,7 @@ static const struct i2c_device_id mxt_id[] = {
{ "atmel_mxt_tp", 0 },
{ "maxtouch", 0 },
{ "mXT224", 0 },
+   { "mXT1386", 0 },
{ }
 };
 MODULE_DEVICE_TABLE(i2c, mxt_id);
-- 
2.17.1



[PATCH v4 1/1] Input: atmel_mxt_ts - implement I2C retries

2020-09-11 Thread Jiada Wang
From: Nick Dyer 

Some maXTouch chips (eg mXT1386) will not respond on the first I2C request
when they are in a sleep state. It must be retried after a delay for the
chip to wake up.

Signed-off-by: Nick Dyer 
[gdavis: Forward port and fix conflicts.]
Signed-off-by: George G. Davis 
[jiada: return exact errno when i2c_transfer & i2c_master_send fails
rename "retry" to "retried" and keep its order in length
set "ret" to correct errno before calling dev_err()
remove reduntant conditional]
Signed-off-by: Jiada Wang 
Reviewed-by: Dmitry Osipenko 
Tested-by: Dmitry Osipenko 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 38 
 1 file changed, 26 insertions(+), 12 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index a2189739e30f..bad3ac58503d 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -196,6 +196,7 @@ enum t100_type {
 #define MXT_CRC_TIMEOUT1000/* msec */
 #define MXT_FW_RESET_TIME  3000/* msec */
 #define MXT_FW_CHG_TIMEOUT 300 /* msec */
+#define MXT_WAKEUP_TIME25  /* msec */
 
 /* Command to unlock bootloader */
 #define MXT_UNLOCK_CMD_MSB 0xaa
@@ -624,6 +625,7 @@ static int __mxt_read_reg(struct i2c_client *client,
   u16 reg, u16 len, void *val)
 {
struct i2c_msg xfer[2];
+   bool retried = false;
u8 buf[2];
int ret;
 
@@ -642,22 +644,28 @@ static int __mxt_read_reg(struct i2c_client *client,
xfer[1].len = len;
xfer[1].buf = val;
 
-   ret = i2c_transfer(client->adapter, xfer, 2);
-   if (ret == 2) {
-   ret = 0;
-   } else {
-   if (ret >= 0)
-   ret = -EIO;
+retry_read:
+   ret = i2c_transfer(client->adapter, xfer, ARRAY_SIZE(xfer));
+   if (ret != ARRAY_SIZE(xfer)) {
+   if (!retried) {
+   dev_dbg(>dev, "i2c retry\n");
+   msleep(MXT_WAKEUP_TIME);
+   retried = true;
+   goto retry_read;
+   }
+   ret = ret < 0 ? ret : -EIO;
dev_err(>dev, "%s: i2c transfer failed (%d)\n",
__func__, ret);
+   return ret;
}
 
-   return ret;
+   return 0;
 }
 
 static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
   const void *val)
 {
+   bool retried = false;
u8 *buf;
size_t count;
int ret;
@@ -671,14 +679,20 @@ static int __mxt_write_reg(struct i2c_client *client, u16 
reg, u16 len,
buf[1] = (reg >> 8) & 0xff;
memcpy([2], val, len);
 
+retry_write:
ret = i2c_master_send(client, buf, count);
-   if (ret == count) {
-   ret = 0;
-   } else {
-   if (ret >= 0)
-   ret = -EIO;
+   if (ret != count) {
+   if (!retried) {
+   dev_dbg(>dev, "i2c retry\n");
+   msleep(MXT_WAKEUP_TIME);
+   retried = true;
+   goto retry_write;
+   }
+   ret = ret < 0 ? ret : -EIO;
dev_err(>dev, "%s: i2c send failed (%d)\n",
__func__, ret);
+   } else {
+   ret = 0;
}
 
kfree(buf);
-- 
2.17.1



[PATCH v3 1/1] Input: atmel_mxt_ts - implement I2C retries

2020-09-08 Thread Jiada Wang
From: Nick Dyer 

Some maXTouch chips (eg mXT1386) will not respond on the first I2C request
when they are in a sleep state. It must be retried after a delay for the
chip to wake up.

Signed-off-by: Nick Dyer 
Acked-by: Yufeng Shen 
(cherry picked from ndyer/linux/for-upstream commit 
63fd7a2cd03c3a572a5db39c52f4856819e1835d)
[gdavis: Forward port and fix conflicts.]
Signed-off-by: George G. Davis 
[jiada: return exact errno when i2c_transfer & i2c_master_send fails,
add "_MS" suffix MXT_WAKEUP_TIME]
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 45 
 1 file changed, 30 insertions(+), 15 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index a2189739e30f..145780f78122 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -196,6 +196,7 @@ enum t100_type {
 #define MXT_CRC_TIMEOUT1000/* msec */
 #define MXT_FW_RESET_TIME  3000/* msec */
 #define MXT_FW_CHG_TIMEOUT 300 /* msec */
+#define MXT_WAKEUP_TIME_MS 25  /* msec */
 
 /* Command to unlock bootloader */
 #define MXT_UNLOCK_CMD_MSB 0xaa
@@ -626,6 +627,7 @@ static int __mxt_read_reg(struct i2c_client *client,
struct i2c_msg xfer[2];
u8 buf[2];
int ret;
+   bool retry = false;
 
buf[0] = reg & 0xff;
buf[1] = (reg >> 8) & 0xff;
@@ -642,17 +644,22 @@ static int __mxt_read_reg(struct i2c_client *client,
xfer[1].len = len;
xfer[1].buf = val;
 
-   ret = i2c_transfer(client->adapter, xfer, 2);
-   if (ret == 2) {
-   ret = 0;
-   } else {
-   if (ret >= 0)
-   ret = -EIO;
-   dev_err(>dev, "%s: i2c transfer failed (%d)\n",
-   __func__, ret);
+retry_read:
+   ret = i2c_transfer(client->adapter, xfer, ARRAY_SIZE(xfer));
+   if (ret != ARRAY_SIZE(xfer)) {
+   if (!retry) {
+   dev_dbg(>dev, "%s: i2c retry\n", __func__);
+   msleep(MXT_WAKEUP_TIME_MS);
+   retry = true;
+   goto retry_read;
+   } else {
+   dev_err(>dev, "%s: i2c transfer failed (%d)\n",
+   __func__, ret);
+   return ret < 0 ? ret : -EIO;
+   }
}
 
-   return ret;
+   return 0;
 }
 
 static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
@@ -661,6 +668,7 @@ static int __mxt_write_reg(struct i2c_client *client, u16 
reg, u16 len,
u8 *buf;
size_t count;
int ret;
+   bool retry = false;
 
count = len + 2;
buf = kmalloc(count, GFP_KERNEL);
@@ -671,14 +679,21 @@ static int __mxt_write_reg(struct i2c_client *client, u16 
reg, u16 len,
buf[1] = (reg >> 8) & 0xff;
memcpy([2], val, len);
 
+retry_write:
ret = i2c_master_send(client, buf, count);
-   if (ret == count) {
-   ret = 0;
+   if (ret != count) {
+   if (!retry) {
+   dev_dbg(>dev, "%s: i2c retry\n", __func__);
+   msleep(MXT_WAKEUP_TIME_MS);
+   retry = true;
+   goto retry_write;
+   } else {
+   dev_err(>dev, "%s: i2c send failed (%d)\n",
+   __func__, ret);
+   ret = ret < 0 ? ret : -EIO;
+   }
} else {
-   if (ret >= 0)
-   ret = -EIO;
-   dev_err(>dev, "%s: i2c send failed (%d)\n",
-   __func__, ret);
+   ret = 0;
}
 
kfree(buf);
-- 
2.17.1



[PATCH v2 1/1] Input: atmel_mxt_ts - implement I2C retries

2020-09-03 Thread Jiada Wang
From: Nick Dyer 

Some maXTouch chips (eg mXT1386) will not respond on the first I2C request
when they are in a sleep state. It must be retried after a delay for the
chip to wake up.

Signed-off-by: Nick Dyer 
Acked-by: Yufeng Shen 
(cherry picked from ndyer/linux/for-upstream commit 
63fd7a2cd03c3a572a5db39c52f4856819e1835d)
[gdavis: Forward port and fix conflicts.]
Signed-off-by: George G. Davis 
[jiada: return exact errno when i2c_transfer & i2c_master_send fails]
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 45 
 1 file changed, 30 insertions(+), 15 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index a2189739e30f..5d4cb15d21dc 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -196,6 +196,7 @@ enum t100_type {
 #define MXT_CRC_TIMEOUT1000/* msec */
 #define MXT_FW_RESET_TIME  3000/* msec */
 #define MXT_FW_CHG_TIMEOUT 300 /* msec */
+#define MXT_WAKEUP_TIME25  /* msec */
 
 /* Command to unlock bootloader */
 #define MXT_UNLOCK_CMD_MSB 0xaa
@@ -626,6 +627,7 @@ static int __mxt_read_reg(struct i2c_client *client,
struct i2c_msg xfer[2];
u8 buf[2];
int ret;
+   bool retry = false;
 
buf[0] = reg & 0xff;
buf[1] = (reg >> 8) & 0xff;
@@ -642,17 +644,22 @@ static int __mxt_read_reg(struct i2c_client *client,
xfer[1].len = len;
xfer[1].buf = val;
 
-   ret = i2c_transfer(client->adapter, xfer, 2);
-   if (ret == 2) {
-   ret = 0;
-   } else {
-   if (ret >= 0)
-   ret = -EIO;
-   dev_err(>dev, "%s: i2c transfer failed (%d)\n",
-   __func__, ret);
+retry_read:
+   ret = i2c_transfer(client->adapter, xfer, ARRAY_SIZE(xfer));
+   if (ret != ARRAY_SIZE(xfer)) {
+   if (!retry) {
+   dev_dbg(>dev, "%s: i2c retry\n", __func__);
+   msleep(MXT_WAKEUP_TIME);
+   retry = true;
+   goto retry_read;
+   } else {
+   dev_err(>dev, "%s: i2c transfer failed (%d)\n",
+   __func__, ret);
+   return ret < 0 ? ret : -EIO;
+   }
}
 
-   return ret;
+   return 0;
 }
 
 static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
@@ -661,6 +668,7 @@ static int __mxt_write_reg(struct i2c_client *client, u16 
reg, u16 len,
u8 *buf;
size_t count;
int ret;
+   bool retry = false;
 
count = len + 2;
buf = kmalloc(count, GFP_KERNEL);
@@ -671,14 +679,21 @@ static int __mxt_write_reg(struct i2c_client *client, u16 
reg, u16 len,
buf[1] = (reg >> 8) & 0xff;
memcpy([2], val, len);
 
+retry_write:
ret = i2c_master_send(client, buf, count);
-   if (ret == count) {
-   ret = 0;
+   if (ret != count) {
+   if (!retry) {
+   dev_dbg(>dev, "%s: i2c retry\n", __func__);
+   msleep(MXT_WAKEUP_TIME);
+   retry = true;
+   goto retry_write;
+   } else {
+   dev_err(>dev, "%s: i2c send failed (%d)\n",
+   __func__, ret);
+   ret = ret < 0 ? ret : -EIO;
+   }
} else {
-   if (ret >= 0)
-   ret = -EIO;
-   dev_err(>dev, "%s: i2c send failed (%d)\n",
-   __func__, ret);
+   ret = 0;
}
 
kfree(buf);
-- 
2.17.1



[PATCH 1/1] Input: atmel_mxt_ts - implement I2C retries

2020-08-21 Thread Jiada Wang
From: Nick Dyer 

Some maXTouch chips (eg mXT1386) will not respond on the first I2C request
when they are in a sleep state. It must be retried after a delay for the
chip to wake up.

Signed-off-by: Nick Dyer 
Acked-by: Yufeng Shen 
(cherry picked from ndyer/linux/for-upstream commit 
63fd7a2cd03c3a572a5db39c52f4856819e1835d)
[gdavis: Forward port and fix conflicts.]
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 45 
 1 file changed, 30 insertions(+), 15 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index a2189739e30f..e93eda1f3d59 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -196,6 +196,7 @@ enum t100_type {
 #define MXT_CRC_TIMEOUT1000/* msec */
 #define MXT_FW_RESET_TIME  3000/* msec */
 #define MXT_FW_CHG_TIMEOUT 300 /* msec */
+#define MXT_WAKEUP_TIME25  /* msec */
 
 /* Command to unlock bootloader */
 #define MXT_UNLOCK_CMD_MSB 0xaa
@@ -626,6 +627,7 @@ static int __mxt_read_reg(struct i2c_client *client,
struct i2c_msg xfer[2];
u8 buf[2];
int ret;
+   bool retry = false;
 
buf[0] = reg & 0xff;
buf[1] = (reg >> 8) & 0xff;
@@ -642,17 +644,22 @@ static int __mxt_read_reg(struct i2c_client *client,
xfer[1].len = len;
xfer[1].buf = val;
 
-   ret = i2c_transfer(client->adapter, xfer, 2);
-   if (ret == 2) {
-   ret = 0;
-   } else {
-   if (ret >= 0)
-   ret = -EIO;
-   dev_err(>dev, "%s: i2c transfer failed (%d)\n",
-   __func__, ret);
+retry_read:
+   ret = i2c_transfer(client->adapter, xfer, ARRAY_SIZE(xfer));
+   if (ret != ARRAY_SIZE(xfer)) {
+   if (!retry) {
+   dev_dbg(>dev, "%s: i2c retry\n", __func__);
+   msleep(MXT_WAKEUP_TIME);
+   retry = true;
+   goto retry_read;
+   } else {
+   dev_err(>dev, "%s: i2c transfer failed (%d)\n",
+   __func__, ret);
+   return -EIO;
+   }
}
 
-   return ret;
+   return 0;
 }
 
 static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
@@ -661,6 +668,7 @@ static int __mxt_write_reg(struct i2c_client *client, u16 
reg, u16 len,
u8 *buf;
size_t count;
int ret;
+   bool retry = false;
 
count = len + 2;
buf = kmalloc(count, GFP_KERNEL);
@@ -671,14 +679,21 @@ static int __mxt_write_reg(struct i2c_client *client, u16 
reg, u16 len,
buf[1] = (reg >> 8) & 0xff;
memcpy([2], val, len);
 
+retry_write:
ret = i2c_master_send(client, buf, count);
-   if (ret == count) {
-   ret = 0;
-   } else {
-   if (ret >= 0)
+   if (ret != count) {
+   if (!retry) {
+   dev_dbg(>dev, "%s: i2c retry\n", __func__);
+   msleep(MXT_WAKEUP_TIME);
+   retry = true;
+   goto retry_write;
+   } else {
+   dev_err(>dev, "%s: i2c send failed (%d)\n",
+   __func__, ret);
ret = -EIO;
-   dev_err(>dev, "%s: i2c send failed (%d)\n",
-   __func__, ret);
+   }
+   } else {
+   ret = 0;
}
 
kfree(buf);
-- 
2.17.1



[PATCH v2 1/1] Input: atmel_mxt_ts - allow specification of firmware file name

2020-08-21 Thread Jiada Wang
From: Nick Dyer 

On platforms which have multiple device instances using this driver, the
firmware may be different on each device. This patch makes the user give
the name of the firmware file when flashing.

If user specified firmware file can't be found, then driver will try to
flash default firmware "maxtouch.fw".

Signed-off-by: Nick Dyer 
Acked-by: Benson Leung 
Acked-by: Yufeng Shen 
(cherry picked from ndyer/linux/for-upstream commit 
76ebb7cee971cb42dfb0a3a9224403b8b09abcf1)
[gdavis: Forward port and fix conflicts.]
Signed-off-by: George G. Davis 
[jiada: change to flash default firmware file, when user specified firmware 
can't be found]
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 33 +++-
 1 file changed, 32 insertions(+), 1 deletion(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index a2189739e30f..a20bc1bf8d52 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -34,6 +34,7 @@
 #define MXT_FW_NAME"maxtouch.fw"
 #define MXT_CFG_NAME   "maxtouch.cfg"
 #define MXT_CFG_MAGIC  "OBP_RAW V1"
+#define MAX_FILENAME_SIZE  64
 
 /* Registers */
 #define MXT_OBJECT_START   0x07
@@ -308,6 +309,7 @@ struct mxt_data {
struct t7_config t7_cfg;
struct mxt_dbg dbg;
struct gpio_desc *reset_gpio;
+   char fw_name[MAX_FILENAME_SIZE];
 
/* Cached parameters from object table */
u16 T5_address;
@@ -2887,6 +2889,26 @@ static int mxt_load_fw(struct device *dev, const char 
*fn)
return ret;
 }
 
+static int mxt_update_file_name(struct device *dev, char *file_name,
+   const char *buf, size_t count)
+{
+   size_t len = count;
+
+   /* Echo into the sysfs entry may append newline at the end of buf */
+   if (buf[count - 1] == '\n')
+   len = count - 1;
+
+   /* Simple sanity check */
+   if (len > MAX_FILENAME_SIZE - 1) {
+   dev_warn(dev, "File name too long\n");
+   return -EINVAL;
+   }
+
+   strscpy(file_name, buf, len + 1);
+
+   return 0;
+}
+
 static ssize_t mxt_update_fw_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
@@ -2894,7 +2916,16 @@ static ssize_t mxt_update_fw_store(struct device *dev,
struct mxt_data *data = dev_get_drvdata(dev);
int error;
 
-   error = mxt_load_fw(dev, MXT_FW_NAME);
+   error = mxt_update_file_name(dev, data->fw_name, buf, count);
+   if (error)
+   return error;
+
+   error = mxt_load_fw(dev, data->fw_name);
+   if (error) {
+   dev_warn(dev, "try %s instead\n", MXT_FW_NAME);
+   error = mxt_load_fw(dev, MXT_FW_NAME);
+   }
+
if (error) {
dev_err(dev, "The firmware update failed(%d)\n", error);
count = error;
-- 
2.17.1



[PATCH 1/1] Input: atmel_mxt_ts - allow specification of firmware file name

2020-07-31 Thread Jiada Wang
From: Nick Dyer 

On platforms which have multiple device instances using this driver, the
firmware may be different on each device. This patch makes the user give
the name of the firmware file when flashing.

This also prevents accidental triggering of the firmware load process.

Signed-off-by: Nick Dyer 
Acked-by: Benson Leung 
Acked-by: Yufeng Shen 
(cherry picked from ndyer/linux/for-upstream commit 
76ebb7cee971cb42dfb0a3a9224403b8b09abcf1)
[gdavis: Forward port and fix conflicts.]
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 42 
 1 file changed, 36 insertions(+), 6 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index a2189739e30f..024dee7a3571 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -30,8 +30,6 @@
 #include 
 #include 
 
-/* Firmware files */
-#define MXT_FW_NAME"maxtouch.fw"
 #define MXT_CFG_NAME   "maxtouch.cfg"
 #define MXT_CFG_MAGIC  "OBP_RAW V1"
 
@@ -308,6 +306,7 @@ struct mxt_data {
struct t7_config t7_cfg;
struct mxt_dbg dbg;
struct gpio_desc *reset_gpio;
+   char *fw_name;
 
/* Cached parameters from object table */
u16 T5_address;
@@ -2766,7 +2765,7 @@ static int mxt_check_firmware_format(struct device *dev,
return -EINVAL;
 }
 
-static int mxt_load_fw(struct device *dev, const char *fn)
+static int mxt_load_fw(struct device *dev)
 {
struct mxt_data *data = dev_get_drvdata(dev);
const struct firmware *fw = NULL;
@@ -2776,9 +2775,9 @@ static int mxt_load_fw(struct device *dev, const char *fn)
unsigned int frame = 0;
int ret;
 
-   ret = request_firmware(, fn, dev);
+   ret = request_firmware(, data->fw_name, dev);
if (ret) {
-   dev_err(dev, "Unable to open firmware %s\n", fn);
+   dev_err(dev, "Unable to open firmware %s\n", data->fw_name);
return ret;
}
 
@@ -2887,6 +2886,33 @@ static int mxt_load_fw(struct device *dev, const char 
*fn)
return ret;
 }
 
+static int mxt_update_file_name(struct device *dev, char **file_name,
+   const char *buf, size_t count)
+{
+   char *file_name_tmp;
+
+   /* Simple sanity check */
+   if (count > 64) {
+   dev_warn(dev, "File name too long\n");
+   return -EINVAL;
+   }
+
+   file_name_tmp = krealloc(*file_name, count + 1, GFP_KERNEL);
+   if (!file_name_tmp)
+   return -ENOMEM;
+
+   *file_name = file_name_tmp;
+   memcpy(*file_name, buf, count);
+
+   /* Echo into the sysfs entry may append newline at the end of buf */
+   if (buf[count - 1] == '\n')
+   (*file_name)[count - 1] = '\0';
+   else
+   (*file_name)[count] = '\0';
+
+   return 0;
+}
+
 static ssize_t mxt_update_fw_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
@@ -2894,7 +2920,11 @@ static ssize_t mxt_update_fw_store(struct device *dev,
struct mxt_data *data = dev_get_drvdata(dev);
int error;
 
-   error = mxt_load_fw(dev, MXT_FW_NAME);
+   error = mxt_update_file_name(dev, >fw_name, buf, count);
+   if (error)
+   return error;
+
+   error = mxt_load_fw(dev);
if (error) {
dev_err(dev, "The firmware update failed(%d)\n", error);
count = error;
-- 
2.17.1



[PATCH 2/2] Input: atmel_mxt_ts - output status from T42 Touch Suppression

2020-07-29 Thread Jiada Wang
From: Nick Dyer 

This patch outputs status from T42 touch suppression

Signed-off-by: Nick Dyer 
Acked-by: Benson Leung 
Acked-by: Yufeng Shen 
(cherry picked from ndyer/linux/for-upstream commit 
ab95b5a30d2c098daaa9f88d9fcfae7eb516)
Signed-off-by: George G. Davis 
[jiada: Replace dev_info() with dev_dbg(),
Move logical continuation to previous line to address checkpatch CHECK]
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 25 
 1 file changed, 25 insertions(+)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 729c8dcb9cd1..b260f8c70c11 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -160,6 +160,9 @@ struct t37_debug {
 #define MXT_T48_MSG_NOISESUP_STATCHG   BIT(4)
 #define MXT_T48_MSG_NOISESUP_NLVLCHG   BIT(5)
 
+/* Define for MXT_PROCI_TOUCHSUPPRESSION_T42 */
+#define MXT_T42_MSG_TCHSUP BIT(0)
+
 /* T100 Multiple Touch Touchscreen */
 #define MXT_T100_CTRL  0
 #define MXT_T100_CFG1  1
@@ -326,6 +329,8 @@ struct mxt_data {
u8 T9_reportid_min;
u8 T9_reportid_max;
u8 T19_reportid;
+   u8 T42_reportid_min;
+   u8 T42_reportid_max;
u16 T44_address;
u8 T48_reportid;
u8 T100_reportid_min;
@@ -960,6 +965,17 @@ static void mxt_proc_t100_message(struct mxt_data *data, 
u8 *message)
data->update_input = true;
 }
 
+static void mxt_proc_t42_messages(struct mxt_data *data, u8 *msg)
+{
+   struct device *dev = >client->dev;
+   u8 status = msg[1];
+
+   if (status & MXT_T42_MSG_TCHSUP)
+   dev_dbg(dev, "T42 suppress\n");
+   else
+   dev_dbg(dev, "T42 normal\n");
+}
+
 static int mxt_proc_t48_messages(struct mxt_data *data, u8 *msg)
 {
struct device *dev = >client->dev;
@@ -987,6 +1003,9 @@ static int mxt_proc_message(struct mxt_data *data, u8 
*message)
 
if (report_id == data->T6_reportid) {
mxt_proc_t6_messages(data, message);
+   } else if (report_id >= data->T42_reportid_min &&
+  report_id <= data->T42_reportid_max) {
+   mxt_proc_t42_messages(data, message);
} else if (report_id == data->T48_reportid) {
mxt_proc_t48_messages(data, message);
} else if (!data->input_dev) {
@@ -1633,6 +1652,8 @@ static void mxt_free_object_table(struct mxt_data *data)
data->T9_reportid_min = 0;
data->T9_reportid_max = 0;
data->T19_reportid = 0;
+   data->T42_reportid_min = 0;
+   data->T42_reportid_max = 0;
data->T44_address = 0;
data->T48_reportid = 0;
data->T100_reportid_min = 0;
@@ -1707,6 +1728,10 @@ static int mxt_parse_object_table(struct mxt_data *data,
object->num_report_ids - 1;
data->num_touchids = object->num_report_ids;
break;
+   case MXT_PROCI_TOUCHSUPPRESSION_T42:
+   data->T42_reportid_min = min_id;
+   data->T42_reportid_max = max_id;
+   break;
case MXT_SPT_MESSAGECOUNT_T44:
data->T44_address = object->start_address;
break;
-- 
2.17.1



[PATCH 1/2] Input: atmel_mxt_ts - output status from T48 Noise Suppression

2020-07-29 Thread Jiada Wang
From: Nick Dyer 

This patch outputs status from T48 Noise Suppression

Signed-off-by: Nick Dyer 
Acked-by: Benson Leung 
Acked-by: Yufeng Shen 
(cherry picked from ndyer/linux/for-upstream commit 
2895a6ff150a49f27a02938f8d262be238b296d8)
Signed-off-by: George G. Davis 
[jiada: Replace bits with symbolic names,
Fixed typo in commit title & message]
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 32 
 1 file changed, 32 insertions(+)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index a2189739e30f..729c8dcb9cd1 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -153,6 +153,13 @@ struct t37_debug {
 #define MXT_RESET_VALUE0x01
 #define MXT_BACKUP_VALUE   0x55
 
+/* Define for MXT_PROCG_NOISESUPPRESSION_T48 */
+#define MXT_T48_MSG_NOISESUP_FREQCHG   BIT(0)
+#define MXT_T48_MSG_NOISESUP_APXCHGBIT(1)
+#define MXT_T48_MSG_NOISESUP_ALGOERR   BIT(2)
+#define MXT_T48_MSG_NOISESUP_STATCHG   BIT(4)
+#define MXT_T48_MSG_NOISESUP_NLVLCHG   BIT(5)
+
 /* T100 Multiple Touch Touchscreen */
 #define MXT_T100_CTRL  0
 #define MXT_T100_CFG1  1
@@ -320,6 +327,7 @@ struct mxt_data {
u8 T9_reportid_max;
u8 T19_reportid;
u16 T44_address;
+   u8 T48_reportid;
u8 T100_reportid_min;
u8 T100_reportid_max;
 
@@ -952,6 +960,24 @@ static void mxt_proc_t100_message(struct mxt_data *data, 
u8 *message)
data->update_input = true;
 }
 
+static int mxt_proc_t48_messages(struct mxt_data *data, u8 *msg)
+{
+   struct device *dev = >client->dev;
+   u8 status, state;
+
+   status = msg[1];
+   state  = msg[4];
+
+   dev_dbg(dev, "T48 state %d status %02X %s%s%s%s%s\n", state, status,
+   status & MXT_T48_MSG_NOISESUP_FREQCHG ? "FREQCHG " : "",
+   status & MXT_T48_MSG_NOISESUP_APXCHG ? "APXCHG " : "",
+   status & MXT_T48_MSG_NOISESUP_ALGOERR ? "ALGOERR " : "",
+   status & MXT_T48_MSG_NOISESUP_STATCHG ? "STATCHG " : "",
+   status & MXT_T48_MSG_NOISESUP_NLVLCHG ? "NLVLCHG " : "");
+
+   return 0;
+}
+
 static int mxt_proc_message(struct mxt_data *data, u8 *message)
 {
u8 report_id = message[0];
@@ -961,6 +987,8 @@ static int mxt_proc_message(struct mxt_data *data, u8 
*message)
 
if (report_id == data->T6_reportid) {
mxt_proc_t6_messages(data, message);
+   } else if (report_id == data->T48_reportid) {
+   mxt_proc_t48_messages(data, message);
} else if (!data->input_dev) {
/*
 * Do not report events if input device
@@ -1606,6 +1634,7 @@ static void mxt_free_object_table(struct mxt_data *data)
data->T9_reportid_max = 0;
data->T19_reportid = 0;
data->T44_address = 0;
+   data->T48_reportid = 0;
data->T100_reportid_min = 0;
data->T100_reportid_max = 0;
data->max_reportid = 0;
@@ -1684,6 +1713,9 @@ static int mxt_parse_object_table(struct mxt_data *data,
case MXT_SPT_GPIOPWM_T19:
data->T19_reportid = min_id;
break;
+   case MXT_PROCG_NOISESUPPRESSION_T48:
+   data->T48_reportid = min_id;
+   break;
case MXT_TOUCH_MULTITOUCHSCREEN_T100:
data->multitouch = MXT_TOUCH_MULTITOUCHSCREEN_T100;
data->T100_reportid_min = min_id;
-- 
2.17.1



[PATCH 1/1] Input: atmel_mxt_ts: split large i2c transfers into blocks

2020-07-29 Thread Jiada Wang
From: Jiada wang 

Some I2C controllers constrain maximum transferred data in an I2C
transaction by set max_[read|write]_len of i2c_adapter_quirk.
Large i2c transfer transaction beyond this limitation may fail to complete,
cause I2C controller driver aborts the transaction and returns failure.

Therefore this patch was created to split large i2c transaction into
smaller chunks which can complete within max_[read|write]_len defined
by I2C controller driver.

CC: Dmitry Osipenko 
Signed-off-by: Jiada wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 60 ++--
 1 file changed, 55 insertions(+), 5 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index a2189739e30f..d7c3c24aa663 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -620,8 +620,8 @@ static int mxt_send_bootloader_cmd(struct mxt_data *data, 
bool unlock)
return 0;
 }
 
-static int __mxt_read_reg(struct i2c_client *client,
-  u16 reg, u16 len, void *val)
+static int __mxt_read_chunk(struct i2c_client *client,
+   u16 reg, u16 len, void *val)
 {
struct i2c_msg xfer[2];
u8 buf[2];
@@ -655,8 +655,33 @@ static int __mxt_read_reg(struct i2c_client *client,
return ret;
 }
 
-static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
-  const void *val)
+static int __mxt_read_reg(struct i2c_client *client,
+ u16 reg, u16 len, void *buf)
+{
+   const struct i2c_adapter_quirks *quirks = client->adapter->quirks;
+   u16 size, offset = 0, max_read_len = len;
+   int ret;
+
+   if (quirks && quirks->max_read_len)
+   max_read_len = quirks->max_read_len;
+
+   while (offset < len) {
+   size = min_t(u16, max_read_len, len - offset);
+
+   ret = __mxt_read_chunk(client,
+  reg + offset,
+  size, buf + offset);
+   if (ret)
+   return ret;
+
+   offset += size;
+   }
+
+   return 0;
+}
+
+static int __mxt_write_chunk(struct i2c_client *client, u16 reg, u16 len,
+const void *val)
 {
u8 *buf;
size_t count;
@@ -685,9 +710,34 @@ static int __mxt_write_reg(struct i2c_client *client, u16 
reg, u16 len,
return ret;
 }
 
+static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
+  const void *val)
+{
+   const struct i2c_adapter_quirks *quirks = client->adapter->quirks;
+   u16 size, offset = 0, max_write_len = len;
+   int ret;
+
+   if (quirks && quirks->max_write_len)
+   max_write_len = quirks->max_write_len;
+
+   while (offset < len) {
+   size = min_t(u16, max_write_len, len - offset);
+
+   ret = __mxt_write_chunk(client,
+   reg + offset,
+   size, val + offset);
+   if (ret)
+   return ret;
+
+   offset += size;
+   }
+
+   return 0;
+}
+
 static int mxt_write_reg(struct i2c_client *client, u16 reg, u8 val)
 {
-   return __mxt_write_reg(client, reg, 1, );
+   return __mxt_write_chunk(client, reg, 1, );
 }
 
 static struct mxt_object *
-- 
2.17.1



[PATCH v2 1/1] Input: atmel_mxt_ts - only read messages in mxt_acquire_irq() when necessary

2020-07-27 Thread Jiada Wang
From: Nick Dyer 

The workaround of reading all messages until an invalid is received is a
way of forcing the CHG line high, which means that when using
edge-triggered interrupts the interrupt can be acquired.

With level-triggered interrupts the workaround is unnecessary.

Also, most recent maXTouch chips have a feature called RETRIGEN which, when
enabled, reasserts the interrupt line every cycle if there are messages
waiting. This also makes the workaround unnecessary.

Note: the RETRIGEN feature is only in some firmware versions/chips, it's
not valid simply to enable the bit.

Signed-off-by: Nick Dyer 
Acked-by: Benson Leung 
Acked-by: Yufeng Shen 
(cherry picked from ndyer/linux/for-upstream commit 
1ae4e8281e491b22442cd5acdfca1862555f8ecb)
[gdavis: Fix conflicts due to v4.6-rc7 commit eb43335c4095 ("Input:
 atmel_mxt_ts - use mxt_acquire_irq in mxt_soft_reset").]
Signed-off-by: George G. Davis 
[jiada: reset use_retrigen_workaround at beginning of mxt_check_retrigen()
call mxt_check_retrigen() after mxt_acquire_irq() in mxt_initialize()
replace white-spaces with tab for MXT_COMMS_RETRIGEN
Changed to check if IRQ is level type]
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 56 ++--
 1 file changed, 53 insertions(+), 3 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index a2189739e30f..6b71b0aff115 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -20,6 +20,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -129,6 +130,7 @@ struct t9_range {
 /* MXT_SPT_COMMSCONFIG_T18 */
 #define MXT_COMMS_CTRL 0
 #define MXT_COMMS_CMD  1
+#define MXT_COMMS_RETRIGEN BIT(6)
 
 /* MXT_DEBUG_DIAGNOSTIC_T37 */
 #define MXT_DIAGNOSTIC_PAGEUP  0x01
@@ -308,6 +310,7 @@ struct mxt_data {
struct t7_config t7_cfg;
struct mxt_dbg dbg;
struct gpio_desc *reset_gpio;
+   bool use_retrigen_workaround;
 
/* Cached parameters from object table */
u16 T5_address;
@@ -318,6 +321,7 @@ struct mxt_data {
u16 T71_address;
u8 T9_reportid_min;
u8 T9_reportid_max;
+   u16 T18_address;
u8 T19_reportid;
u16 T44_address;
u8 T100_reportid_min;
@@ -1190,9 +1194,11 @@ static int mxt_acquire_irq(struct mxt_data *data)
 
enable_irq(data->irq);
 
-   error = mxt_process_messages_until_invalid(data);
-   if (error)
-   return error;
+   if (data->use_retrigen_workaround) {
+   error = mxt_process_messages_until_invalid(data);
+   if (error)
+   return error;
+   }
 
return 0;
 }
@@ -1282,6 +1288,38 @@ static u32 mxt_calculate_crc(u8 *base, off_t start_off, 
off_t end_off)
return crc;
 }
 
+static int mxt_check_retrigen(struct mxt_data *data)
+{
+   struct i2c_client *client = data->client;
+   int error;
+   int val;
+   struct irq_data *irqd;
+
+   data->use_retrigen_workaround = false;
+
+   irqd = irq_get_irq_data(data->irq);
+   if (!irqd)
+   return -EINVAL;
+
+   if (irqd_is_level_type(irqd))
+   return 0;
+
+   if (data->T18_address) {
+   error = __mxt_read_reg(client,
+  data->T18_address + MXT_COMMS_CTRL,
+  1, );
+   if (error)
+   return error;
+
+   if (val & MXT_COMMS_RETRIGEN)
+   return 0;
+   }
+
+   dev_warn(>dev, "Enabling RETRIGEN workaround\n");
+   data->use_retrigen_workaround = true;
+   return 0;
+}
+
 static int mxt_prepare_cfg_mem(struct mxt_data *data, struct mxt_cfg *cfg)
 {
struct device *dev = >client->dev;
@@ -1561,6 +1599,10 @@ static int mxt_update_cfg(struct mxt_data *data, const 
struct firmware *fw)
 
mxt_update_crc(data, MXT_COMMAND_BACKUPNV, MXT_BACKUP_VALUE);
 
+   ret = mxt_check_retrigen(data);
+   if (ret)
+   goto release_mem;
+
ret = mxt_soft_reset(data);
if (ret)
goto release_mem;
@@ -1604,6 +1646,7 @@ static void mxt_free_object_table(struct mxt_data *data)
data->T71_address = 0;
data->T9_reportid_min = 0;
data->T9_reportid_max = 0;
+   data->T18_address = 0;
data->T19_reportid = 0;
data->T44_address = 0;
data->T100_reportid_min = 0;
@@ -1678,6 +1721,9 @@ static int mxt_parse_object_table(struct mxt_data *data,
object->num_report_ids - 1;
data->num_touchids = object->num_report_ids;
break;
+   case MXT_SPT_COMMSCONFIG_T18:
+

[PATCH 1/1] Input: atmel_mxt_ts - only read messages in mxt_acquire_irq() when necessary

2020-07-20 Thread Jiada Wang
From: Nick Dyer 

The workaround of reading all messages until an invalid is received is a
way of forcing the CHG line high, which means that when using
edge-triggered interrupts the interrupt can be acquired.

With level-triggered interrupts the workaround is unnecessary.

Also, most recent maXTouch chips have a feature called RETRIGEN which, when
enabled, reasserts the interrupt line every cycle if there are messages
waiting. This also makes the workaround unnecessary.

Note: the RETRIGEN feature is only in some firmware versions/chips, it's
not valid simply to enable the bit.

Signed-off-by: Nick Dyer 
Acked-by: Benson Leung 
Acked-by: Yufeng Shen 
(cherry picked from ndyer/linux/for-upstream commit 
1ae4e8281e491b22442cd5acdfca1862555f8ecb)
[gdavis: Fix conflicts due to v4.6-rc7 commit eb43335c4095 ("Input:
 atmel_mxt_ts - use mxt_acquire_irq in mxt_soft_reset").]
Signed-off-by: George G. Davis 
[jiada: reset use_retrigen_workaround at beginning of mxt_check_retrigen()
call mxt_check_retrigen() after mxt_acquire_irq() in mxt_initialize()
replace white-spaces with tab for MXT_COMMS_RETRIGEN]
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 51 ++--
 1 file changed, 48 insertions(+), 3 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index a2189739e30f..2b4cb5c87a43 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -20,6 +20,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -129,6 +130,7 @@ struct t9_range {
 /* MXT_SPT_COMMSCONFIG_T18 */
 #define MXT_COMMS_CTRL 0
 #define MXT_COMMS_CMD  1
+#define MXT_COMMS_RETRIGEN BIT(6)
 
 /* MXT_DEBUG_DIAGNOSTIC_T37 */
 #define MXT_DIAGNOSTIC_PAGEUP  0x01
@@ -308,6 +310,7 @@ struct mxt_data {
struct t7_config t7_cfg;
struct mxt_dbg dbg;
struct gpio_desc *reset_gpio;
+   bool use_retrigen_workaround;
 
/* Cached parameters from object table */
u16 T5_address;
@@ -318,6 +321,7 @@ struct mxt_data {
u16 T71_address;
u8 T9_reportid_min;
u8 T9_reportid_max;
+   u16 T18_address;
u8 T19_reportid;
u16 T44_address;
u8 T100_reportid_min;
@@ -1190,9 +1194,11 @@ static int mxt_acquire_irq(struct mxt_data *data)
 
enable_irq(data->irq);
 
-   error = mxt_process_messages_until_invalid(data);
-   if (error)
-   return error;
+   if (data->use_retrigen_workaround) {
+   error = mxt_process_messages_until_invalid(data);
+   if (error)
+   return error;
+   }
 
return 0;
 }
@@ -1282,6 +1288,33 @@ static u32 mxt_calculate_crc(u8 *base, off_t start_off, 
off_t end_off)
return crc;
 }
 
+static int mxt_check_retrigen(struct mxt_data *data)
+{
+   struct i2c_client *client = data->client;
+   int error;
+   int val;
+
+   data->use_retrigen_workaround = false;
+
+   if (irq_get_trigger_type(data->irq) & IRQF_TRIGGER_LOW)
+   return 0;
+
+   if (data->T18_address) {
+   error = __mxt_read_reg(client,
+  data->T18_address + MXT_COMMS_CTRL,
+  1, );
+   if (error)
+   return error;
+
+   if (val & MXT_COMMS_RETRIGEN)
+   return 0;
+   }
+
+   dev_warn(>dev, "Enabling RETRIGEN workaround\n");
+   data->use_retrigen_workaround = true;
+   return 0;
+}
+
 static int mxt_prepare_cfg_mem(struct mxt_data *data, struct mxt_cfg *cfg)
 {
struct device *dev = >client->dev;
@@ -1561,6 +1594,10 @@ static int mxt_update_cfg(struct mxt_data *data, const 
struct firmware *fw)
 
mxt_update_crc(data, MXT_COMMAND_BACKUPNV, MXT_BACKUP_VALUE);
 
+   ret = mxt_check_retrigen(data);
+   if (ret)
+   goto release_mem;
+
ret = mxt_soft_reset(data);
if (ret)
goto release_mem;
@@ -1604,6 +1641,7 @@ static void mxt_free_object_table(struct mxt_data *data)
data->T71_address = 0;
data->T9_reportid_min = 0;
data->T9_reportid_max = 0;
+   data->T18_address = 0;
data->T19_reportid = 0;
data->T44_address = 0;
data->T100_reportid_min = 0;
@@ -1678,6 +1716,9 @@ static int mxt_parse_object_table(struct mxt_data *data,
object->num_report_ids - 1;
data->num_touchids = object->num_report_ids;
break;
+   case MXT_SPT_COMMSCONFIG_T18:
+   data->T18_address = object->start_address;
+   break;
case MXT_SPT_MESSAGECOUNT_T44:
   

[PATCH v11 55/56] input: atmel_mxt_ts: don't disable IRQ before remove of mxt_fw_attr_group

2020-05-08 Thread Jiada Wang
There is issue when firmware is being updated, but mxt_remove()
is called to remove driver, because at very beginning IRQ is disabled
so that firmware can't proceed, thus cause driver to hang.

This patch by move disable_irq() after remove of mxt_fw_attr_group in
mxt_remove() to address this issue.

Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index e75a7e5b0c59..b518c316757d 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -4514,8 +4514,8 @@ static int mxt_remove(struct i2c_client *client)
 {
struct mxt_data *data = i2c_get_clientdata(client);
 
-   disable_irq(data->irq);
sysfs_remove_group(>dev.kobj, _fw_attr_group);
+   disable_irq(data->irq);
if (data->reset_gpio) {
sysfs_remove_link(>dev.kobj, "reset");
gpiod_unexport(data->reset_gpio);
-- 
2.17.1



[PATCH v11 03/56] Input: atmel_mxt_ts - only read messages in mxt_acquire_irq() when necessary

2020-05-08 Thread Jiada Wang
From: Nick Dyer 

The workaround of reading all messages until an invalid is received is a
way of forcing the CHG line high, which means that when using
edge-triggered interrupts the interrupt can be acquired.

With level-triggered interrupts the workaround is unnecessary.

Also, most recent maXTouch chips have a feature called RETRIGEN which, when
enabled, reasserts the interrupt line every cycle if there are messages
waiting. This also makes the workaround unnecessary.

Note: the RETRIGEN feature is only in some firmware versions/chips, it's
not valid simply to enable the bit.

Signed-off-by: Nick Dyer 
Acked-by: Benson Leung 
Acked-by: Yufeng Shen 
(cherry picked from ndyer/linux/for-upstream commit 
1ae4e8281e491b22442cd5acdfca1862555f8ecb)
[gdavis: Fix conflicts due to v4.6-rc7 commit eb43335c4095 ("Input:
 atmel_mxt_ts - use mxt_acquire_irq in mxt_soft_reset").]
Signed-off-by: George G. Davis 
[jiada: reset use_retrigen_workaround at beginning of mxt_check_retrigen()
call mxt_check_retrigen() after mxt_acquire_irq() in mxt_initialize()
replace white-spaces with tab for MXT_COMMS_RETRIGEN]
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 51 ++--
 1 file changed, 48 insertions(+), 3 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 49bdf5cf3a0d..3f1ebe14802f 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -20,6 +20,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -129,6 +130,7 @@ struct t9_range {
 /* MXT_SPT_COMMSCONFIG_T18 */
 #define MXT_COMMS_CTRL 0
 #define MXT_COMMS_CMD  1
+#define MXT_COMMS_RETRIGEN BIT(6)
 
 /* MXT_DEBUG_DIAGNOSTIC_T37 */
 #define MXT_DIAGNOSTIC_PAGEUP  0x01
@@ -308,6 +310,7 @@ struct mxt_data {
struct t7_config t7_cfg;
struct mxt_dbg dbg;
struct gpio_desc *reset_gpio;
+   bool use_retrigen_workaround;
 
/* Cached parameters from object table */
u16 T5_address;
@@ -318,6 +321,7 @@ struct mxt_data {
u16 T71_address;
u8 T9_reportid_min;
u8 T9_reportid_max;
+   u16 T18_address;
u8 T19_reportid;
u16 T44_address;
u8 T100_reportid_min;
@@ -1190,9 +1194,11 @@ static int mxt_acquire_irq(struct mxt_data *data)
 
enable_irq(data->irq);
 
-   error = mxt_process_messages_until_invalid(data);
-   if (error)
-   return error;
+   if (data->use_retrigen_workaround) {
+   error = mxt_process_messages_until_invalid(data);
+   if (error)
+   return error;
+   }
 
return 0;
 }
@@ -1282,6 +1288,33 @@ static u32 mxt_calculate_crc(u8 *base, off_t start_off, 
off_t end_off)
return crc;
 }
 
+static int mxt_check_retrigen(struct mxt_data *data)
+{
+   struct i2c_client *client = data->client;
+   int error;
+   int val;
+
+   data->use_retrigen_workaround = false;
+
+   if (irq_get_trigger_type(data->irq) & IRQF_TRIGGER_LOW)
+   return 0;
+
+   if (data->T18_address) {
+   error = __mxt_read_reg(client,
+  data->T18_address + MXT_COMMS_CTRL,
+  1, );
+   if (error)
+   return error;
+
+   if (val & MXT_COMMS_RETRIGEN)
+   return 0;
+   }
+
+   dev_warn(>dev, "Enabling RETRIGEN workaround\n");
+   data->use_retrigen_workaround = true;
+   return 0;
+}
+
 static int mxt_prepare_cfg_mem(struct mxt_data *data, struct mxt_cfg *cfg)
 {
struct device *dev = >client->dev;
@@ -1561,6 +1594,10 @@ static int mxt_update_cfg(struct mxt_data *data, const 
struct firmware *fw)
 
mxt_update_crc(data, MXT_COMMAND_BACKUPNV, MXT_BACKUP_VALUE);
 
+   ret = mxt_check_retrigen(data);
+   if (ret)
+   goto release_mem;
+
ret = mxt_soft_reset(data);
if (ret)
goto release_mem;
@@ -1604,6 +1641,7 @@ static void mxt_free_object_table(struct mxt_data *data)
data->T71_address = 0;
data->T9_reportid_min = 0;
data->T9_reportid_max = 0;
+   data->T18_address = 0;
data->T19_reportid = 0;
data->T44_address = 0;
data->T100_reportid_min = 0;
@@ -1678,6 +1716,9 @@ static int mxt_parse_object_table(struct mxt_data *data,
object->num_report_ids - 1;
data->num_touchids = object->num_report_ids;
break;
+   case MXT_SPT_COMMSCONFIG_T18:
+   data->T18_address = object->start_address;
+   break;
case MXT_SPT_MESSAGECOUNT_T44:
   

[PATCH v11 48/56] Input: Atmel: improve error handling in mxt_update_cfg()

2020-05-08 Thread Jiada Wang
From: Deepak Das 

mxt_update_cfg() failed to propagate the error
code from mxt_init_t7_power_cfg() so return the error code.

Signed-off-by: Deepak Das 
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index c779cac565a8..83fa2caddeab 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -2172,7 +2172,9 @@ static int mxt_update_cfg(struct mxt_data *data, const 
struct firmware *fw)
dev_info(dev, "Config successfully updated\n");
 
/* T7 config may have changed */
-   mxt_init_t7_power_cfg(data);
+   ret = mxt_init_t7_power_cfg(data);
+   if (ret)
+   dev_warn(dev, "Power Config failed to update\n");
 
 release_mem:
kfree(cfg.mem);
-- 
2.17.1



[PATCH v11 54/56] input: atmel_mxt_ts: added sysfs interface to update atmel T38 data

2020-05-08 Thread Jiada Wang
From: Naveen Chakka 

Atmel touch controller contains T38 object where a user can store its own
data of length 64 bytes. T38 data will not be part of checksum
calculation on executing T6 BACKUP command.

format used to update the T38 data is given below:

  

offset: offset address of the data to be written in the t38 object
(in decimal)

length: length of the data to be written into the t38 object(in decimal)

data: actual data bytes to be written into the t38 object
  (values should be in hex)

Ex:
1. 0 2 10 20
updates first two bytes of the t38 data with values 10 and 20

2. 19 6 10 2f 30 4a 50 60
updates 6 bytes of t38 data from the index 19-24 with hex values

Signed-off-by: Naveen Chakka 
Signed-off-by: Sanjeev Chugh 
Signed-off-by: George G. Davis 
[jiada: Rename mxt_t38_data_show to t38_data_show
Rename mxt_t38_data_store to t38_data_store
Replace DEVICE_ATTR with DEVICE_ATTR_RW]
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 102 +++
 1 file changed, 102 insertions(+)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 5c2f4ea1a362..e75a7e5b0c59 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -3989,6 +3989,106 @@ static ssize_t touch_dev_stat_show(struct device *dev, 
struct
return ret;
 }
 
+static ssize_t t38_data_show(struct device *dev,
+struct device_attribute *attr, char *buf)
+{
+   struct mxt_data *data = dev_get_drvdata(dev);
+   struct mxt_object *object;
+   size_t count = 0, size;
+   u8 i, *t38_buf;
+
+   if (!data->object_table)
+   return -ENXIO;
+
+   object = mxt_get_object(data, MXT_SPT_USERDATA_T38);
+   size = mxt_obj_size(object);
+
+   /* Pre-allocate buffer large enough to hold max size of t38 object.*/
+   t38_buf = kmalloc(size, GFP_KERNEL);
+   if (!t38_buf)
+   return -ENOMEM;
+
+   count = __mxt_read_reg(data->client, object->start_address,
+  size, t38_buf);
+   if (count)
+   goto end;
+
+   for (i = 0; i < size; i++)
+   count += scnprintf(buf + count, PAGE_SIZE - count,
+  "[%2u]: %02x\n", i, t38_buf[i]);
+   count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
+end:
+   kfree(t38_buf);
+   return count;
+}
+
+static ssize_t t38_data_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+   struct mxt_data *data = dev_get_drvdata(dev);
+   struct mxt_object *object;
+   ssize_t ret = 0, pos, offset;
+   unsigned int i, len, index;
+   u8 *t38_buf;
+
+   if (!data->object_table)
+   return -ENXIO;
+
+   object = mxt_get_object(data, MXT_SPT_USERDATA_T38);
+
+   /* Pre-allocate buffer large enough to hold max size of t38 object.*/
+   t38_buf = kmalloc(mxt_obj_size(object), GFP_KERNEL);
+   if (!t38_buf)
+   return -ENOMEM;
+
+   ret = sscanf(buf, "%zd %d%zd", , , );
+   if (ret != 2) {
+   dev_err(dev, "Bad format: Invalid parameter to update t38\n");
+   ret = -EINVAL;
+   goto end;
+   }
+
+   if (len == 0) {
+   dev_err(dev,
+   "Bad format: Data length should not be equal to 0\n");
+   ret = -EINVAL;
+   goto end;
+   }
+
+   if (offset < 0 || ((offset + len) > 64)) {
+   dev_err(dev, "Invalid offset value to update t38\n");
+   ret = -EINVAL;
+   goto end;
+   }
+
+   index = pos;
+   for (i = 0; i < len; i++) {
+   ret = sscanf(buf + index, "%hhx%zd", t38_buf + i, );
+   if (ret != 1) {
+   dev_err(dev, "Bad format: Invalid Data\n");
+   ret = -EINVAL;
+   goto end;
+   }
+   index += pos;
+   }
+
+   ret = __mxt_write_reg(data->client, object->start_address + offset,
+ len, t38_buf);
+   if (ret)
+   goto end;
+
+   ret = mxt_t6_command(data, MXT_COMMAND_BACKUPNV, MXT_BACKUP_VALUE,
+true);
+   if (ret)
+   dev_err(dev, "backup command failed\n");
+   else
+   ret = count;
+end:
+   kfree(t38_buf);
+   return ret;
+}
+
 static DEVICE_ATTR_RO(fw_version);
 static DEVICE_ATTR_RO(hw_version);
 static DEVICE_ATTR_RO(object);
@@ -3999,6 +4099,7 @@ static DEVICE_ATTR_RW(debug_v2_enable);
 static DEVICE_ATTR_RO(debug_notify);
 static DEVICE_ATTR_RW(t25_selftest);
 static DEVICE_ATTR_RO(touch_dev_stat);
+static

[PATCH v11 56/56] Input: atmel_mxt_ts - Fix compilation warning

2020-05-08 Thread Jiada Wang
fix "make W=1" compilation warnings from Atmel driver
as per the compilation logs.

Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index b518c316757d..8d3941e5d2ce 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -1990,7 +1990,7 @@ static int mxt_prepare_cfg_mem(struct mxt_data *data, 
struct mxt_cfg *cfg)
 
byte_offset = reg + i - cfg->start_ofs;
 
-   if (byte_offset >= 0 && byte_offset < cfg->mem_size) {
+   if (byte_offset < cfg->mem_size) {
*(cfg->mem + byte_offset) = val;
} else {
dev_err(dev, "Bad object: reg:%d, T%d, 
ofs=%d\n",
-- 
2.17.1



[PATCH v11 47/56] Input: Atmel: improve error handling in mxt_initialize()

2020-05-08 Thread Jiada Wang
From: Deepak Das 

Currently mxt_initialize() tries to probe bootloader mode
even if valid bootloader address is not specified.

This commit modifies mxt_initialize() to return error
if Device is not in appmode and bootloader address is
not specified.

This commit also returns error code from mxt_send_bootloader_cmd()
in mxt_initialize().

Signed-off-by: Deepak Das 
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 58 +---
 1 file changed, 41 insertions(+), 17 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 906da438d5e8..c779cac565a8 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -716,17 +716,13 @@ static int mxt_lookup_bootloader_address(struct mxt_data 
*data, bool retry)
return 0;
 }
 
-static int mxt_probe_bootloader(struct mxt_data *data, bool alt_address)
+static int mxt_probe_bootloader(struct mxt_data *data)
 {
struct device *dev = >client->dev;
int error;
u8 buf[3];
bool crc_failure, extended_id;
 
-   error = mxt_lookup_bootloader_address(data, alt_address);
-   if (error)
-   return error;
-
/* Check bootloader status and version information */
error = mxt_bootloader_read(data, buf, sizeof(buf));
if (error)
@@ -2909,6 +2905,32 @@ static void mxt_config_cb(const struct firmware *cfg, 
void *ctx)
release_firmware(cfg);
 }
 
+static int mxt_bootloader_status(struct mxt_data *data)
+{
+   struct i2c_client *client = data->client;
+   int error;
+
+   error = mxt_lookup_bootloader_address(data, false);
+   if (error) {
+   dev_info(>dev,
+"Bootloader address is not specified\n");
+   return error;
+   }
+   /* Check bootloader state */
+   error = mxt_probe_bootloader(data);
+   if (error) {
+   dev_info(>dev, "Trying alternate bootloader address\n");
+   mxt_lookup_bootloader_address(data, true);
+   error = mxt_probe_bootloader(data);
+   if (error) {
+   dev_err(>dev,
+   "Chip is not in appmode or bootloader mode\n");
+   return error;
+   }
+   }
+   return 0;
+}
+
 static int mxt_initialize(struct mxt_data *data)
 {
struct i2c_client *client = data->client;
@@ -2920,16 +2942,13 @@ static int mxt_initialize(struct mxt_data *data)
if (!error)
break;
 
-   /* Check bootloader state */
-   error = mxt_probe_bootloader(data, false);
-   if (error) {
-   dev_info(>dev, "Trying alternate bootloader 
address\n");
-   error = mxt_probe_bootloader(data, true);
-   if (error) {
-   /* Chip is not in appmode or bootloader mode */
-   return error;
-   }
-   }
+   dev_info(>dev,
+"info block read failed (%d), so try bootloader 
method\n",
+error);
+
+   error = mxt_bootloader_status(data);
+   if (error)
+   return error;
 
/* OK, we are in bootloader, see if we can recover */
if (++recovery_attempts > 1) {
@@ -2943,7 +2962,9 @@ static int mxt_initialize(struct mxt_data *data)
}
 
/* Attempt to exit bootloader into app mode */
-   mxt_send_bootloader_cmd(data, false);
+   error = mxt_send_bootloader_cmd(data, false);
+   if (error)
+   return error;
msleep(MXT_FW_RESET_TIME);
}
 
@@ -3630,8 +3651,11 @@ static int mxt_enter_bootloader(struct mxt_data *data)
 
msleep(MXT_RESET_TIME);
 
+   ret = mxt_lookup_bootloader_address(data, false);
+   if (ret)
+   return ret;
/* Do not need to scan since we know family ID */
-   ret = mxt_probe_bootloader(data, 0);
+   ret = mxt_probe_bootloader(data);
if (ret)
return ret;
 
-- 
2.17.1



[PATCH v11 53/56] input: touchscreen: atmel_mxt_ts: Added sysfs entry for touchscreen status

2020-05-08 Thread Jiada Wang
From: Naveen Chakka 

To know the current communication status of the touch controller during
runtime, sysfs interface is added

sysfs interface: /sys/class/i2c-dev/i2c-*/device/*/touch_dev_stat
Executing the above sysfs interface provides two output values

1)Status of the touch device
value 0 represents device is inactive
value 1 represents device is active
2)Error counter
value represents the number of times device in inactive since last read

New module_param "debug_state" is introduced, by set its value,
a watchdog work is scheduled to periodically check device state.
default value is false.

Signed-off-by: Naveen Chakka 
Signed-off-by: Sanjeev Chugh 
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 85 +++-
 1 file changed, 82 insertions(+), 3 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 20d6ada778e5..5c2f4ea1a362 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -223,6 +223,7 @@ enum t100_type {
 #define MXT_CHG_DELAY  100 /* msec */
 #define MXT_POWERON_DELAY  150 /* msec */
 #define MXT_BOOTLOADER_WAIT36E5/* 1 minute */
+#define MXT_WATCHDOG_TIMEOUT   1000/* msec */
 
 /* Command to unlock bootloader */
 #define MXT_UNLOCK_CMD_MSB 0xaa
@@ -246,6 +247,9 @@ enum t100_type {
 
 #define DEBUG_MSG_MAX  200
 
+#define MXT_DEBUG_STATEfalse
+static bool debug_state = MXT_DEBUG_STATE;
+
 struct mxt_info {
u8 family_id;
u8 variant_id;
@@ -318,6 +322,11 @@ struct mxt_flash {
struct delayed_work work;
 };
 
+struct mxt_statusinfo {
+   bool dev_status;
+   u32 error_count;
+};
+
 /* Each client has this additional data */
 struct mxt_data {
struct i2c_client *client;
@@ -373,6 +382,8 @@ struct mxt_data {
const char *pcfg_name;
const char *input_name;
struct mxt_flash *flash;
+   struct delayed_work watchdog_work;
+   struct mxt_statusinfo mxt_status;
 
/* Cached parameters from object table */
u16 T5_address;
@@ -2956,6 +2967,26 @@ static int mxt_bootloader_status(struct mxt_data *data)
return 0;
 }
 
+static void mxt_watchdog_work(struct work_struct *work)
+{
+   struct mxt_data *data =
+   container_of(work, struct mxt_data, watchdog_work.work);
+   u16 info_buf;
+   int ret;
+
+   ret = __mxt_read_reg(data->client, 0, sizeof(info_buf), _buf);
+
+   if (ret) {
+   data->mxt_status.error_count++;
+   data->mxt_status.dev_status = false;
+   } else {
+   data->mxt_status.dev_status = true;
+   }
+
+   schedule_delayed_work(>watchdog_work,
+ msecs_to_jiffies(MXT_WATCHDOG_TIMEOUT));
+}
+
 static int mxt_initialize(struct mxt_data *data)
 {
struct i2c_client *client = data->client;
@@ -3730,6 +3761,9 @@ static int mxt_load_fw(struct device *dev)
INIT_DELAYED_WORK(>flash->work, mxt_fw_work);
reinit_completion(>flash->flash_completion);
 
+   if (debug_state)
+   cancel_delayed_work_sync(>watchdog_work);
+
if (!data->in_bootloader) {
ret = mxt_enter_bootloader(data);
if (ret)
@@ -3749,6 +3783,9 @@ static int mxt_load_fw(struct device *dev)
cancel_delayed_work_sync(>flash->work);
data->in_bootloader = false;
 release_firmware:
+   if (debug_state)
+   schedule_delayed_work(>watchdog_work,
+ msecs_to_jiffies(MXT_WATCHDOG_TIMEOUT));
release_firmware(data->flash->fw);
 free:
devm_kfree(dev, data->flash);
@@ -3936,6 +3973,22 @@ static const struct attribute_group mxt_fw_attr_group = {
.attrs = mxt_fw_attrs,
 };
 
+static ssize_t touch_dev_stat_show(struct device *dev, struct
+  device_attribute * attr, char *buf)
+{
+   struct mxt_data *data = dev_get_drvdata(dev);
+   int ret = 0;
+
+   if (data->mxt_status.dev_status)
+   data->mxt_status.error_count = 0;
+
+   ret =  snprintf(buf, PAGE_SIZE, "%d %d\n", data->mxt_status.dev_status,
+   data->mxt_status.error_count);
+   /* clear the error counter once it is read */
+   data->mxt_status.error_count = 0;
+   return ret;
+}
+
 static DEVICE_ATTR_RO(fw_version);
 static DEVICE_ATTR_RO(hw_version);
 static DEVICE_ATTR_RO(object);
@@ -3945,6 +3998,7 @@ static DEVICE_ATTR_RW(debug_enable);
 static DEVICE_ATTR_RW(debug_v2_enable);
 static DEVICE_ATTR_RO(debug_notify);
 static DEVICE_ATTR_RW(t25_selftest);
+static DEVICE_ATTR_RO(touch_dev_stat);
 
 static struct attribute *mxt_attrs[] = {
_attr_fw_version.attr,
@@ -3

[PATCH v11 46/56] Input: Atmel: improve error handling in mxt_start()

2020-05-08 Thread Jiada Wang
From: Deepak Das 

mxt_start() does not return error in any of
the failure cases which will allow input_dev->open()
to return success even in case of any failure.

This commit modifies mxt_start() to return error
in failure cases.

Signed-off-by: Deepak Das 
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 31 
 1 file changed, 16 insertions(+), 15 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 7c530ffac1ba..906da438d5e8 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -3961,12 +3961,13 @@ static int mxt_start(struct mxt_data *data)
 
switch (data->suspend_mode) {
case MXT_SUSPEND_T9_CTRL:
-   mxt_soft_reset(data);
-
+   ret = mxt_soft_reset(data);
+   if (ret)
+   break;
/* Touch enable */
/* 0x83 = SCANEN | RPTEN | ENABLE */
-   mxt_write_object(data,
-   MXT_TOUCH_MULTI_T9, MXT_T9_CTRL, 0x83);
+   ret = mxt_write_object(data,
+  MXT_TOUCH_MULTI_T9, MXT_T9_CTRL, 0x83);
break;
 
case MXT_SUSPEND_REGULATOR:
@@ -3980,27 +3981,26 @@ static int mxt_start(struct mxt_data *data)
 * Discard any touch messages still in message buffer
 * from before chip went to sleep
 */
-   mxt_process_messages_until_invalid(data);
+   ret = mxt_process_messages_until_invalid(data);
+   if (ret)
+   break;
 
ret = mxt_set_t7_power_cfg(data, MXT_POWER_CFG_RUN);
if (ret)
-   return ret;
+   break;
 
/* Recalibrate since chip has been in deep sleep */
ret = mxt_t6_command(data, MXT_COMMAND_CALIBRATE, 1, false);
if (ret)
-   return ret;
+   break;
 
ret = mxt_acquire_irq(data);
-   if (ret)
-   return ret;
-
-   break;
}
 
-   data->suspended = false;
+   if (!ret)
+   data->suspended = false;
 
-   return 0;
+   return ret;
 }
 
 static int mxt_stop(struct mxt_data *data)
@@ -4327,6 +4327,7 @@ static int __maybe_unused mxt_resume(struct device *dev)
struct i2c_client *client = to_i2c_client(dev);
struct mxt_data *data = i2c_get_clientdata(client);
struct input_dev *input_dev = data->input_dev;
+   int ret = 0;
 
if (!input_dev)
return 0;
@@ -4336,11 +4337,11 @@ static int __maybe_unused mxt_resume(struct device *dev)
mutex_lock(_dev->mutex);
 
if (input_dev->users)
-   mxt_start(data);
+   ret = mxt_start(data);
 
mutex_unlock(_dev->mutex);
 
-   return 0;
+   return ret;
 }
 
 static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume);
-- 
2.17.1



[PATCH v11 51/56] Input: Atmel: use T44 object to process T5 messages

2020-05-08 Thread Jiada Wang
From: Deepak Das 

T44 object returns the count of valid T5 messages in the buffer. According
to atmel, this count should be the main criteria to read the number of T5
messages.

Following is the statement from atmel confirming the same :-
"For the readout of messages we recommend to stop after the last message
is read out from the buffer. One way to identify the amount of new messages
is to read T44. The other way is to monitor the /CHG line which indicates
independent of mode 0 or mode 1 if there are still data in the buffer.
0xFF indicates that there is no message pending anymore, but it is not
recommended to use this as the main criteria to control the
data transfer."

This commit modifies the logic to readout the T5 messages on the basis
of T44 object.

Signed-off-by: Deepak Das 
Signed-off-by: Sanjeev Chugh 
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 55 +++-
 1 file changed, 35 insertions(+), 20 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 103881911acc..d134a8b9b3ca 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -1488,7 +1488,7 @@ static u8 mxt_max_msg_read_count(struct mxt_data *data, 
u8 max_T5_msg_count)
return min(T5_msg_count_limit, max_T5_msg_count);
 }
 
-static irqreturn_t mxt_process_messages_t44(struct mxt_data *data)
+static int mxt_process_messages_t44(struct mxt_data *data)
 {
struct device *dev = >client->dev;
int ret;
@@ -1501,7 +1501,7 @@ static irqreturn_t mxt_process_messages_t44(struct 
mxt_data *data)
data->T5_msg_size + 1, data->msg_buf);
if (ret) {
dev_err(dev, "Failed to read T44 and T5 (%d)\n", ret);
-   return IRQ_NONE;
+   return ret;
}
 
T5_msg_count = data->msg_buf[0];
@@ -1511,7 +1511,7 @@ static irqreturn_t mxt_process_messages_t44(struct 
mxt_data *data)
 * Mode 0. It results in unnecessary I2C operations but it is benign.
 */
if (!T5_msg_count)
-   return IRQ_NONE;
+   return processed_valid;
 
if (T5_msg_count > data->max_reportid) {
dev_warn(dev, "T44 count %d exceeded max report id\n",
@@ -1523,12 +1523,14 @@ static irqreturn_t mxt_process_messages_t44(struct 
mxt_data *data)
ret = mxt_proc_message(data, data->msg_buf + 1);
if (ret < 0) {
dev_warn(dev, "Unexpected invalid message\n");
-   return IRQ_NONE;
+   return ret;
}
 
total_pending = T5_msg_count - 1;
-   if (!total_pending)
+   if (!total_pending) {
+   processed_valid = 1;
goto end;
+   }
 
/* Process remaining messages if necessary */
T5_msg_count = mxt_max_msg_read_count(data, total_pending);
@@ -1552,7 +1554,7 @@ static irqreturn_t mxt_process_messages_t44(struct 
mxt_data *data)
data->update_input = false;
}
 
-   return IRQ_HANDLED;
+   return processed_valid;
 }
 
 static int mxt_process_messages_until_invalid(struct mxt_data *data)
@@ -1582,7 +1584,7 @@ static int mxt_process_messages_until_invalid(struct 
mxt_data *data)
return -EBUSY;
 }
 
-static irqreturn_t mxt_process_messages(struct mxt_data *data)
+static int mxt_process_messages(struct mxt_data *data)
 {
int total_handled, num_handled;
u8 count = data->last_message_count;
@@ -1593,7 +1595,7 @@ static irqreturn_t mxt_process_messages(struct mxt_data 
*data)
/* include final invalid message */
total_handled = mxt_read_and_process_messages(data, count + 1);
if (total_handled < 0)
-   return IRQ_NONE;
+   return total_handled;
/* if there were invalid messages, then we are done */
else if (total_handled <= count)
goto update_count;
@@ -1602,7 +1604,7 @@ static irqreturn_t mxt_process_messages(struct mxt_data 
*data)
do {
num_handled = mxt_read_and_process_messages(data, 2);
if (num_handled < 0)
-   return IRQ_NONE;
+   return num_handled;
 
total_handled += num_handled;
 
@@ -1618,12 +1620,13 @@ static irqreturn_t mxt_process_messages(struct mxt_data 
*data)
data->update_input = false;
}
 
-   return IRQ_HANDLED;
+   return total_handled;
 }
 
 static irqreturn_t mxt_interrupt(int irq, void *dev_id)
 {
struct mxt_data *data = dev_id;
+   int ret;
 
if (data->in_bootloader) {
complete(>chg_completion);
@@ -1631,17 +1634,22 @@ static irqreturn_t mxt_interrupt(int irq, void *dev_id)
if (data->flash)
cancel_

[PATCH v11 50/56] Input: Atmel: handle ReportID "0x00" while processing T5 messages

2020-05-08 Thread Jiada Wang
From: Deepak Das 

ReportID "0x00" is reserved by Atmel and should not be used by any
Atmel touch controller.

reportID is the first byte retrieved from T5 message payload.
Currently Atmel driver continues to process the T5 messages even if
the reportID "0x00" is returned by Touch Controller.

This commit modifies Atmel touch driver to return -EINVAL if ReportID
"0x00" is received while processing T5 messages.

Signed-off-by: Deepak Das 
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 8 
 1 file changed, 8 insertions(+)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 92701bf5291f..103881911acc 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -77,6 +77,7 @@
 #define MXT_PROCI_TOUCHSEQUENCELOGGER  93
 #define MXT_TOUCH_MULTITOUCHSCREEN_T100 100
 #define MXT_PROCI_ACTIVESTYLUS_T107107
+#define MXT_RPTID_RESERVED 0
 
 /* MXT_GEN_MESSAGE_T5 object */
 #define MXT_RPTID_NOMSG0xff
@@ -1381,6 +1382,11 @@ static int mxt_proc_message(struct mxt_data *data, u8 
*message)
u8 report_id = message[0];
bool dump = data->debug_enabled;
 
+   if (report_id == MXT_RPTID_RESERVED) {
+   dev_err(>client->dev,
+   "Received Reserved ReportID 0x00\n");
+   return -EINVAL;
+   }
if (report_id == MXT_RPTID_NOMSG)
return 0;
 
@@ -1451,6 +1457,8 @@ static int mxt_read_and_process_messages(struct mxt_data 
*data, u8 count)
ret = mxt_proc_message(data,
data->msg_buf + data->T5_msg_size * i);
 
+   if (ret < 0)
+   return ret;
if (ret == 1)
num_valid++;
}
-- 
2.17.1



[PATCH v11 04/56] Input: atmel_mxt_ts - split large i2c transfers into blocks

2020-05-08 Thread Jiada Wang
From: Nick Dyer 

On some firmware variants, the size of the info block exceeds what can
be read in a single transfer.

Signed-off-by: Nick Dyer 
(cherry picked from ndyer/linux/for-upstream commit 
74c4f5277cfa403d43fafc404119dc57a08677db)
[gdavis: Forward port and fix conflicts due to v4.14.51 commit
 960fe000b1d3 ("Input: atmel_mxt_ts - fix the firmware
 update").]
Signed-off-by: George G. Davis 
[jiada: Change mxt_read_blks() to __mxt_read_reg(), original __mxt_read_reg() to
__mxt_read_chunk()]
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 28 +---
 1 file changed, 25 insertions(+), 3 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 3f1ebe14802f..7e6a66e3e1e0 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -40,7 +40,7 @@
 #define MXT_OBJECT_START   0x07
 #define MXT_OBJECT_SIZE6
 #define MXT_INFO_CHECKSUM_SIZE 3
-#define MXT_MAX_BLOCK_WRITE256
+#define MXT_MAX_BLOCK_WRITE255
 
 /* Object types */
 #define MXT_DEBUG_DIAGNOSTIC_T37   37
@@ -624,8 +624,8 @@ static int mxt_send_bootloader_cmd(struct mxt_data *data, 
bool unlock)
return 0;
 }
 
-static int __mxt_read_reg(struct i2c_client *client,
-  u16 reg, u16 len, void *val)
+static int __mxt_read_chunk(struct i2c_client *client,
+   u16 reg, u16 len, void *val)
 {
struct i2c_msg xfer[2];
u8 buf[2];
@@ -659,6 +659,28 @@ static int __mxt_read_reg(struct i2c_client *client,
return ret;
 }
 
+static int __mxt_read_reg(struct i2c_client *client,
+ u16 reg, u16 len, void *buf)
+{
+   u16 offset = 0;
+   int error;
+   u16 size;
+
+   while (offset < len) {
+   size = min(MXT_MAX_BLOCK_WRITE, len - offset);
+
+   error = __mxt_read_chunk(client,
+reg + offset,
+size, buf + offset);
+   if (error)
+   return error;
+
+   offset += size;
+   }
+
+   return 0;
+}
+
 static int __mxt_write_reg(struct i2c_client *client, u16 reg, u16 len,
   const void *val)
 {
-- 
2.17.1



[PATCH v11 36/56] Input: atmel_mxt_ts - implement debug output for messages

2020-05-08 Thread Jiada Wang
From: Nick Dyer 

Add a debug switch which causes all messages from the touch controller to
be dumped to the dmesg log with a set prefix "MXT MSG:". This is used by
Atmel user-space utilities to debug touch operation. Enabling this output
does impact touch performance.

Signed-off-by: Nick Dyer 
(cherry picked from ndyer/linux/for-upstream commit 
3c3fcfdd4889dfeb1c80ae8cd94a622c6342b06a)
[gdavis: Forward port and fix conflicts.]
Signed-off-by: George G. Davis 
[jiada: Rename mxt_debug_enable_store to debug_enable_store
Rename mxt_debug_enable_show to debug_enable_show
Replace DEVICE_ATTR with DEVICE_ATTR_RW]
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 46 ++--
 1 file changed, 43 insertions(+), 3 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 1027ebbc3978..7d48c4d1f57d 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -336,6 +336,7 @@ struct mxt_data {
u8 t100_aux_ampl;
u8 t100_aux_area;
u8 t100_aux_vect;
+   bool debug_enabled;
u8 max_reportid;
u32 config_crc;
u32 info_crc;
@@ -461,8 +462,11 @@ static bool mxt_object_readable(unsigned int type)
 
 static void mxt_dump_message(struct mxt_data *data, u8 *message)
 {
-   dev_dbg(>client->dev, "message: %*ph\n",
-   data->T5_msg_size, message);
+   /* debug message with prefix 'MXT MSG:' used by
+* Atmel user-space utilities to debug touch operation
+*/
+   dev_dbg(>client->dev, "MXT MSG: %*ph\n",
+  data->T5_msg_size, message);
 }
 
 static int mxt_wait_for_completion(struct mxt_data *data,
@@ -1212,6 +1216,7 @@ static void mxt_proc_t93_messages(struct mxt_data *data, 
u8 *msg)
 static int mxt_proc_message(struct mxt_data *data, u8 *message)
 {
u8 report_id = message[0];
+   bool dump = data->debug_enabled;
 
if (report_id == MXT_RPTID_NOMSG)
return 0;
@@ -1246,9 +1251,12 @@ static int mxt_proc_message(struct mxt_data *data, u8 
*message)
} else if (report_id == data->T93_reportid) {
mxt_proc_t93_messages(data, message);
} else {
-   mxt_dump_message(data, message);
+   dump = true;
}
 
+   if (dump)
+   mxt_dump_message(data, message);
+
return 1;
 }
 
@@ -3498,6 +3506,36 @@ static ssize_t update_cfg_store(struct device *dev,
return ret;
 }
 
+static ssize_t debug_enable_show(struct device *dev,
+   struct device_attribute *attr, char *buf)
+{
+   struct mxt_data *data = dev_get_drvdata(dev);
+   char c;
+
+   c = data->debug_enabled ? '1' : '0';
+   return scnprintf(buf, PAGE_SIZE, "%c\n", c);
+}
+
+static ssize_t debug_enable_store(struct device *dev,
+   struct device_attribute *attr, const char *buf, size_t count)
+{
+   struct mxt_data *data = dev_get_drvdata(dev);
+   u8 i;
+   ssize_t ret;
+
+   if (kstrtou8(buf, 0, ) == 0 && i < 2) {
+   data->debug_enabled = (i == 1);
+
+   dev_dbg(dev, "%s\n", i ? "debug enabled" : "debug disabled");
+   ret = count;
+   } else {
+   dev_dbg(dev, "debug_enabled write error\n");
+   ret = -EINVAL;
+   }
+
+   return ret;
+}
+
 static DEVICE_ATTR_WO(update_fw);
 
 static struct attribute *mxt_fw_attrs[] = {
@@ -3514,6 +3552,7 @@ static DEVICE_ATTR_RO(hw_version);
 static DEVICE_ATTR_RO(object);
 static DEVICE_ATTR_WO(update_cfg);
 static DEVICE_ATTR_RO(config_crc);
+static DEVICE_ATTR_RW(debug_enable);
 
 static struct attribute *mxt_attrs[] = {
_attr_fw_version.attr,
@@ -3521,6 +3560,7 @@ static struct attribute *mxt_attrs[] = {
_attr_object.attr,
_attr_update_cfg.attr,
_attr_config_crc.attr,
+   _attr_debug_enable.attr,
NULL
 };
 
-- 
2.17.1



[PATCH v11 37/56] Input: atmel_mxt_ts - implement improved debug message interface

2020-05-08 Thread Jiada Wang
From: Nick Dyer 

Implement improved debug message interface

Signed-off-by: Nick Dyer 
(cherry picked from ndyer/linux/for-upstream commit 
078569c13c88dcb6f8d882bfe17168587712df7d)
[gdavis: Resolve forward port conflicts due to v4.14.51 commit
 960fe000b1d3 ("Input: atmel_mxt_ts - fix the firmware
 update").]
Signed-off-by: George G. Davis 
[gdavis: Squash fixes from Dirk Behme:
 - Input: atmel_mxt_ts - add missing unlock in error path
 - Input: atmel_mxt_ts - add missing unlock in error path
 - Input: atmel_mxt_ts - call mxt_debug_msg_remove() in error path
 - Input: atmel_mxt_ts - protect debug_v2_enabled by mutex
Signed-off-by: Dirk Behme 
[gdavis: Squash fix from Vladimir Zapolskiy:
 - Input: atmel_mxt_ts - simplify debug_msg binary attribute
   handling]
Signed-off-by: Vladimir Zapolskiy 
[jiada: Rename mxt_debug_notify_show to debug_notify_show
Rename mxt_debug_v2_enable_store to debug_v2_enable_store
Add debug_v2_enable_show
Replace DEVICE_ATTR with DEVICE_ATTR_[RW|RO] ]
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 201 ++-
 1 file changed, 200 insertions(+), 1 deletion(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 7d48c4d1f57d..92c883087a4f 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -243,6 +243,8 @@ enum t100_type {
 
 #define MXT_PIXELS_PER_MM  20
 
+#define DEBUG_MSG_MAX  200
+
 struct mxt_info {
u8 family_id;
u8 variant_id;
@@ -337,6 +339,11 @@ struct mxt_data {
u8 t100_aux_area;
u8 t100_aux_vect;
bool debug_enabled;
+   bool debug_v2_enabled;
+   u8 *debug_msg_data;
+   u16 debug_msg_count;
+   struct bin_attribute *debug_msg_attr;
+   struct mutex debug_msg_lock;
u8 max_reportid;
u32 config_crc;
u32 info_crc;
@@ -469,6 +476,144 @@ static void mxt_dump_message(struct mxt_data *data, u8 
*message)
   data->T5_msg_size, message);
 }
 
+static void mxt_debug_msg_enable(struct mxt_data *data)
+{
+   struct device *dev = >client->dev;
+
+   if (data->debug_v2_enabled)
+   return;
+
+   mutex_lock(>debug_msg_lock);
+
+   data->debug_msg_data = kcalloc(DEBUG_MSG_MAX,
+   data->T5_msg_size, GFP_KERNEL);
+   if (!data->debug_msg_data) {
+   mutex_unlock(>debug_msg_lock);
+   return;
+   }
+
+   data->debug_v2_enabled = true;
+   mutex_unlock(>debug_msg_lock);
+
+   dev_dbg(dev, "Enabled message output\n");
+}
+
+static void mxt_debug_msg_disable(struct mxt_data *data)
+{
+   struct device *dev = >client->dev;
+
+   if (!data->debug_v2_enabled)
+   return;
+
+   mutex_lock(>debug_msg_lock);
+
+   data->debug_v2_enabled = false;
+
+   kfree(data->debug_msg_data);
+   data->debug_msg_data = NULL;
+   data->debug_msg_count = 0;
+   mutex_unlock(>debug_msg_lock);
+   dev_dbg(dev, "Disabled message output\n");
+}
+
+static void mxt_debug_msg_add(struct mxt_data *data, u8 *msg)
+{
+   struct device *dev = >client->dev;
+
+   mutex_lock(>debug_msg_lock);
+
+   if (!data->debug_msg_data) {
+   mutex_unlock(>debug_msg_lock);
+   dev_err(dev, "No buffer!\n");
+   return;
+   }
+
+   if (data->debug_msg_count < DEBUG_MSG_MAX) {
+   memcpy(data->debug_msg_data +
+  data->debug_msg_count * data->T5_msg_size,
+  msg,
+  data->T5_msg_size);
+   data->debug_msg_count++;
+   } else {
+   dev_dbg(dev, "Discarding %u messages\n", data->debug_msg_count);
+   data->debug_msg_count = 0;
+   }
+
+   mutex_unlock(>debug_msg_lock);
+
+   sysfs_notify(>client->dev.kobj, NULL, "debug_notify");
+}
+
+static ssize_t mxt_debug_msg_read(struct file *filp, struct kobject *kobj,
+   struct bin_attribute *bin_attr, char *buf, loff_t off, size_t bytes)
+{
+   struct device *dev = container_of(kobj, struct device, kobj);
+   struct mxt_data *data = dev_get_drvdata(dev);
+   int count;
+   size_t bytes_read;
+
+   if (!data->debug_msg_data) {
+   dev_err(dev, "No buffer!\n");
+   return 0;
+   }
+
+   count = bytes / data->T5_msg_size;
+
+   if (count > DEBUG_MSG_MAX)
+   count = DEBUG_MSG_MAX;
+
+   mutex_lock(>debug_msg_lock);
+
+   if (count > data->debug_msg_count)
+   count = data->debug_msg_count;
+
+   bytes_read = count * data->T5_

[PATCH v11 38/56] Input: atmel_mxt_ts - eliminate data->raw_info_block

2020-05-08 Thread Jiada Wang
Dynamically allocated in mxt_read_info_block() buffer buf is assigned
both to data->info and data->raw_info_block, having both data->info
and data->raw_info_block is redundant and confusing.

This patch eliminates data->raw_info_block.

Signed-off-by: Jiada Wang 
Signed-off-by: George G. Davis 
Signed-off-by: Vladimir Zapolskiy 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 7 ++-
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 92c883087a4f..a92ad9a103a1 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -324,7 +324,6 @@ struct mxt_data {
char phys[64];  /* device physical location */
struct mxt_object *object_table;
struct mxt_info *info;
-   void *raw_info_block;
unsigned int irq;
unsigned int max_x;
unsigned int max_y;
@@ -2053,9 +2052,8 @@ static void mxt_free_object_table(struct mxt_data *data)
v4l2_device_unregister(>dbg.v4l2);
 #endif
data->object_table = NULL;
+   kfree(data->info);
data->info = NULL;
-   kfree(data->raw_info_block);
-   data->raw_info_block = NULL;
kfree(data->msg_buf);
data->msg_buf = NULL;
data->T5_address = 0;
@@ -2224,7 +,7 @@ static int mxt_read_info_block(struct mxt_data *data)
u8 *crc_ptr;
 
/* If info block already allocated, free it */
-   if (data->raw_info_block)
+   if (data->info)
mxt_free_object_table(data);
 
/* Read 7-byte ID information block starting at address 0 */
@@ -2275,7 +2273,6 @@ static int mxt_read_info_block(struct mxt_data *data)
goto err_free_mem;
}
 
-   data->raw_info_block = id_buf;
data->info = (struct mxt_info *)id_buf;
 
dev_info(>dev,
-- 
2.17.1



[PATCH v11 41/56] input: atmel_mxt_ts: export GPIO reset line via sysfs

2020-05-08 Thread Jiada Wang
From: "George G. Davis" 

N.B. Modifying the atmel_mxt_ts GPIO reset line during operation will
cause problems with normal driver operation.  This feature is provided
as a diagnostic debug aid.  It does not take into consideration any
pending operations which may be in progress.  Modifying the atmel_mxt_ts
GPIO reset line at any time will inevitably cause the driver to fail.

Signed-off-by: George G. Davis 
Signed-off-by: Rajeev Kumar 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 21 +
 1 file changed, 21 insertions(+)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 75329f87927b..ceb14b4a8d4d 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -4080,6 +4080,19 @@ static int mxt_probe(struct i2c_client *client, const 
struct i2c_device_id *id)
return error;
}
 
+   if (data->reset_gpio) {
+   error = gpiod_export(data->reset_gpio, 0);
+   if (error)
+   return error;
+
+   error = gpiod_export_link(>dev, "reset",
+ data->reset_gpio);
+   if (error) {
+   gpiod_unexport(data->reset_gpio);
+   return error;
+   }
+   }
+
if (data->suspend_mode == MXT_SUSPEND_REGULATOR) {
enable_irq(data->irq);
 
@@ -4110,6 +4123,10 @@ static int mxt_probe(struct i2c_client *client, const 
struct i2c_device_id *id)
 err_free_object:
mxt_free_input_device(data);
mxt_free_object_table(data);
+   if (data->reset_gpio) {
+   sysfs_remove_link(>dev.kobj, "reset");
+   gpiod_unexport(data->reset_gpio);
+   }
return error;
 }
 
@@ -4119,6 +4136,10 @@ static int mxt_remove(struct i2c_client *client)
 
disable_irq(data->irq);
sysfs_remove_group(>dev.kobj, _fw_attr_group);
+   if (data->reset_gpio) {
+   sysfs_remove_link(>dev.kobj, "reset");
+   gpiod_unexport(data->reset_gpio);
+   }
mxt_debug_msg_remove(data);
mxt_sysfs_remove(data);
mxt_free_input_device(data);
-- 
2.17.1



[PATCH v11 52/56] Input: atmel_mxt_ts: use gpiod_set_value_cansleep for reset pin

2020-05-08 Thread Jiada Wang
From: Balasubramani Vivekanandan 

In case of remote display, touch controller will be also remote.
In such cases, the reset pin of the touch controller will be
controlled through bridging ICs like Deserilizer and Serializer.
Therefore accessing the gpio pins require transactions with the
external IC. Using the function gpiod_set_value will print a
warning like below

WARNING: CPU: 0 PID: 576 at drivers/gpio/gpiolib.c:1441 
gpiod_set_value+0x34/0x60()
CPU: 0 PID: 576 Comm: modprobe Not tainted 3.14.79-08377-g84ea22f-dirty #4
Backtrace:
[<80011c58>] (dump_backtrace) from [<80011e60>] (show_stack+0x18/0x1c)
[<80011e48>] (show_stack) from [<8052d7ac>] (dump_stack+0x7c/0x9c)
[<8052d730>] (dump_stack) from [<800241bc>] (warn_slowpath_common+0x74/0x9c)
[<80024148>] (warn_slowpath_common) from [<80024288>] 
(warn_slowpath_null+0x24/0x2c)
[<80024264>] (warn_slowpath_null) from [<8029e070>] (gpiod_set_value+0x34/0x60)
[<8029e03c>] (gpiod_set_value) from [<7f492e98>] (mxt_probe+0x1e0/0x718 
[atmel_mxt_ts])
[<7f492cb8>] (mxt_probe [atmel_mxt_ts]) from [<803c4d34>] 
(i2c_device_probe+0xcc/0xec)
[<803c4c68>] (i2c_device_probe) from [<803252a0>] 
(driver_probe_device+0xc0/0x200)

Signed-off-by: Balasubramani Vivekanandan 

Signed-off-by: Vladimir Zapolskiy 
Signed-off-by: Sanjeev Chugh 
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index d134a8b9b3ca..20d6ada778e5 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -2476,7 +2476,7 @@ static void mxt_regulator_enable(struct mxt_data *data)
if (!data->reg_vdd || !data->reg_avdd)
return;
 
-   gpiod_set_value(data->reset_gpio, 0);
+   gpiod_set_value_cansleep(data->reset_gpio, 0);
 
error = regulator_enable(data->reg_vdd);
if (error)
@@ -2494,7 +2494,7 @@ static void mxt_regulator_enable(struct mxt_data *data)
 * voltage
 */
msleep(MXT_REGULATOR_DELAY);
-   gpiod_set_value(data->reset_gpio, 1);
+   gpiod_set_value_cansleep(data->reset_gpio, 1);
msleep(MXT_CHG_DELAY);
 
 retry_wait:
@@ -4305,7 +4305,7 @@ static int mxt_probe(struct i2c_client *client, const 
struct i2c_device_id *id)
disable_irq(data->irq);
} else if (data->reset_gpio) {
msleep(MXT_RESET_GPIO_TIME);
-   gpiod_set_value(data->reset_gpio, 1);
+   gpiod_set_value_cansleep(data->reset_gpio, 1);
msleep(MXT_RESET_INVALID_CHG);
} else {
dev_dbg(>dev,
-- 
2.17.1



[PATCH v11 49/56] Input: Atmel: Improve error handling in mxt_initialize_input_device()

2020-05-08 Thread Jiada Wang
From: Deepak Das 

Currently Driver probe continues with a warning message when it
fails to get the proper multitouch object configurations like
TouchScreen resolution.

But Driver probe should fail in case of above scneario because it
will not behave as expected without the proper touchscreen
configurations.

This commit modifies mxt_initialize_input_device() to return error
when it fails to get the proper touch screen configurations.

Signed-off-by: Deepak Das 
Signed-off-by: Dean Jenkins 
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 12 
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 83fa2caddeab..92701bf5291f 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -2748,15 +2748,19 @@ static int mxt_initialize_input_device(struct mxt_data 
*data)
case MXT_TOUCH_MULTI_T9:
num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 
1;
error = mxt_read_t9_resolution(data);
-   if (error)
-   dev_warn(dev, "Failed to initialize T9 resolution\n");
+   if (error) {
+   dev_err(dev, "Failed to initialize T9 resolution\n");
+   return error;
+   }
break;
 
case MXT_TOUCH_MULTITOUCHSCREEN_T100:
num_mt_slots = data->num_touchids;
error = mxt_read_t100_config(data);
-   if (error)
-   dev_warn(dev, "Failed to read T100 config\n");
+   if (error) {
+   dev_err(dev, "Failed to read T100 config\n");
+   return error;
+   }
break;
 
default:
-- 
2.17.1



[PATCH v11 39/56] Input: atmel_mxt_ts - Change call-points of mxt_free_* functions

2020-05-08 Thread Jiada Wang
From: Kautuk Consul 

Revamping the code to call mxt_free_object_table and mxt_free_input_device
functions only in the following scenarios and code paths:
1) The error path of the mxt_probe() entry point
2) The mxt_remove de-init path entry point
3) All paths which definitely expect to populate the object table
   like:
   - the mxt_update_fw_store path which first calls
 mxt_load_fw and then resorts to calling mxt_initialize itself.
   - the mxt_read_info_block function which attempts to fill in the
 object table itself as the main non-error part of the logic.
4) All paths in the code expected to definitely allocate and register
   the input device such as:
   - the mxt_update_fw_store path which first calls
 mxt_load_fw and then resorts to calling mxt_initialize itself.
   - the mxt_update_cfg_store function which will call
 mxt_configure_objects.

Signed-off-by: Kautuk Consul 
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 19 ++-
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index a92ad9a103a1..9281a574ca80 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -3317,21 +3317,21 @@ static int mxt_configure_objects(struct mxt_data *data,
error = mxt_init_t7_power_cfg(data);
if (error) {
dev_err(dev, "Failed to initialize power cfg\n");
-   goto err_free_object_table;
+   return error;
}
 
if (cfg) {
error = mxt_update_cfg(data, cfg);
if (error) {
dev_warn(dev, "Error %d updating config\n", error);
-   goto err_free_object_table;
+   return error;
}
}
 
if (data->multitouch) {
error = mxt_initialize_input_device(data);
if (error)
-   goto err_free_object_table;
+   return error;
} else {
dev_warn(dev, "No touch object detected\n");
}
@@ -3339,10 +3339,6 @@ static int mxt_configure_objects(struct mxt_data *data,
mxt_debug_init(data);
 
return 0;
-
-err_free_object_table:
-   mxt_free_object_table(data);
-   return error;
 }
 
 /* Configuration crc check sum is returned as hex xx */
@@ -4093,16 +4089,21 @@ static int mxt_probe(struct i2c_client *client, const 
struct i2c_device_id *id)
 
error = mxt_initialize(data);
if (error)
-   return error;
+   goto err_free_object;
 
error = sysfs_create_group(>dev.kobj, _fw_attr_group);
if (error) {
dev_err(>dev, "Failure %d creating fw sysfs group\n",
error);
-   return error;
+   goto err_free_object;
}
 
return 0;
+
+err_free_object:
+   mxt_free_input_device(data);
+   mxt_free_object_table(data);
+   return error;
 }
 
 static int mxt_remove(struct i2c_client *client)
-- 
2.17.1



[PATCH v11 42/56] input: atmel_mxt_ts: Add Missing Delay for reset handling of Atmel touch panel controller in detachable displays.

2020-05-08 Thread Jiada Wang
From: keerthikumarp 

In case of attached display, the touchpanel reset is controlled
via imx gpio's from  atmel driver and the delay between
touchpanel reset and the time at which the chip becomes capable to
communicate with the host processor, has be taken care.

However in case of detachable displays, the touchpanel reset is
controlled via a deserializer gpio which is triggered just before
the atmel driver is probed.The delay between touchpanel reset and
the time at which the chip becomes capable to communicate (as
specified in datasheet) was not being accounted for. This patch
introduces that delay.

Signed-off-by: keerthikumarp 
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 4 
 1 file changed, 4 insertions(+)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index ceb14b4a8d4d..3ffd49b383f4 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -4105,6 +4105,10 @@ static int mxt_probe(struct i2c_client *client, const 
struct i2c_device_id *id)
msleep(MXT_RESET_GPIO_TIME);
gpiod_set_value(data->reset_gpio, 1);
msleep(MXT_RESET_INVALID_CHG);
+   } else {
+   dev_dbg(>dev,
+   "atmel reset pin not found in device tree");
+   msleep(MXT_RESET_TIME);
}
 
error = mxt_initialize(data);
-- 
2.17.1



[PATCH v11 43/56] Input: atmel_mxt_ts: Add support for run self-test routine.

2020-05-08 Thread Jiada Wang
From: Nikhil Ravindran 

The self test object T25 runs self test routines in device to find faults
Sysfs entry add to start self test routine and read back the test results
for atmel touchcontrollers.The feature will be used for A-IVI and CAF
projects.

Signed-off-by: Nikhil Ravindran 
Signed-off-by: George G. Davis 
[jiada: Rename mxt_t25_selftest_show to t25_selftest_show
Rename mxt_t25_selftest_store to t25_selftest_show
Rename attr t25 to t25_selftest
Replace DEVICE_ATTR with DEVICE_ATTR_RW]
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 113 +++
 1 file changed, 113 insertions(+)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 3ffd49b383f4..c9ff450fa193 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -337,6 +337,9 @@ struct mxt_data {
u8 t100_aux_ampl;
u8 t100_aux_area;
u8 t100_aux_vect;
+   u16 T25_address;
+   u8  T25_reportid;
+   u8  t25_msg[6];
bool debug_enabled;
bool debug_v2_enabled;
u8 *debug_msg_data;
@@ -414,6 +417,8 @@ struct mxt_data {
 
/* Indicates whether device is updating configuration */
bool updating_config;
+
+   bool t25_status;
 };
 
 struct mxt_vb2_buffer {
@@ -1357,6 +1362,24 @@ static void mxt_proc_t93_messages(struct mxt_data *data, 
u8 *msg)
dev_info(dev, "T93 report double tap %d\n", status);
 }
 
+static void mxt_proc_t25_messages(struct mxt_data *data, u8 *msg)
+{
+   struct device *dev = >client->dev;
+
+   /* Output debug if status has changed */
+   dev_dbg(dev, "T25 Status 0x%x Info: %x %x %x %x %x\n",
+   msg[1],
+   msg[2],
+   msg[3],
+   msg[4],
+   msg[5],
+   msg[6]);
+
+   /* Save current status */
+   memcpy(>t25_msg[0], [1], sizeof(data->t25_msg));
+   data->t25_status = false;
+}
+
 static int mxt_proc_message(struct mxt_data *data, u8 *message)
 {
u8 report_id = message[0];
@@ -1387,6 +1410,8 @@ static int mxt_proc_message(struct mxt_data *data, u8 
*message)
} else if (report_id == data->T19_reportid) {
mxt_input_button(data, message);
data->update_input = true;
+   } else if (report_id == data->T25_reportid) {
+   mxt_proc_t25_messages(data, message);
} else if (report_id >= data->T15_reportid_min
   && report_id <= data->T15_reportid_max) {
mxt_proc_t15_messages(data, message);
@@ -1611,6 +1636,86 @@ static int mxt_t6_command(struct mxt_data *data, u16 
cmd_offset,
return 0;
 }
 
+static int mxt_t25_command(struct mxt_data *data, u8 cmd, bool wait)
+{
+   u16 reg;
+   int timeout_counter = 0;
+   int ret;
+   u8  val[2];
+
+   reg = data->T25_address;
+   val[0] = 0x3;
+   val[1] = cmd;
+
+   data->t25_status = true;
+   ret = __mxt_write_reg(data->client, reg, sizeof(val), val);
+   if (ret) {
+   data->t25_status = false;
+   return ret;
+   }
+
+   if (!wait)
+   return 0;
+
+   do {
+   msleep(MXT_WAKEUP_TIME);
+   ret = __mxt_read_reg(data->client, reg + 1, 1, [1]);
+   if (ret)
+   return ret;
+   } while ((val[1] != 0) && (timeout_counter++ <= 100));
+
+   if (timeout_counter > 100) {
+   dev_err(>client->dev, "Command failed!\n");
+   data->t25_status = false;
+   return -EIO;
+   }
+   return 0;
+}
+
+/* Firmware Version is returned as Major.Minor.Build */
+static ssize_t t25_selftest_show(struct device *dev, struct
+device_attribute * attr, char *buf)
+{
+   struct mxt_data *data = dev_get_drvdata(dev);
+   ssize_t offset = 0;
+
+   if (data->t25_status)
+   return -EAGAIN;
+
+   if (data->t25_msg[0] == 0xFE)
+   offset += scnprintf(buf, PAGE_SIZE, "PASS\n");
+   else
+   offset += scnprintf(buf, PAGE_SIZE, "FAILED\n");
+
+   offset += scnprintf(buf + offset, PAGE_SIZE, "%x %x %x %x %x %x\n",
+data->t25_msg[0],
+data->t25_msg[1],
+data->t25_msg[2],
+data->t25_msg[3],
+data->t25_msg[4],
+data->t25_msg[5]);
+   return offset;
+}
+
+static ssize_t t25_selftest_store(struct device *dev, struct
+ device_attribute * attr, const char *buf,
+ size_t count)
+{
+   struct mxt_data *data = dev_get_drvdata(dev);
+   u32 cmd;
+   int ret;
+
+   ret = kstrt

[PATCH v11 44/56] Input: atmel_mxt_ts: Limit the max bytes transferred in an i2c transaction

2020-05-08 Thread Jiada Wang
From: Balasubramani Vivekanandan 

Some I2C controllers constrain maximum transferred data in an I2C
transaction by set max_[read|write]_len of i2c_adapter_quirk.
Large i2c read transaction beyond this limitation may fail to complete,
cause I2C controller driver aborts the transaction and returns failure.

Therefore this patch was created to split the large i2c transaction into
smaller chunks which can complete
within the max_read_len defined by I2C controller driver.

Signed-off-by: Balasubramani Vivekanandan 

Signed-off-by: Jiada Wang 
CC: Dmitry Osipenko 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 62 ++--
 1 file changed, 48 insertions(+), 14 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index c9ff450fa193..ed850a0bae69 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -1463,11 +1463,34 @@ static int mxt_read_and_process_messages(struct 
mxt_data *data, u8 count)
return num_valid;
 }
 
+static u8 mxt_max_msg_read_count(struct mxt_data *data, u8 max_T5_msg_count)
+{
+   struct i2c_client *client = data->client;
+   u16 max_read_len = client->adapter->quirks->max_read_len;
+   u8 T5_msg_count_limit = max_read_len / data->T5_msg_size;
+
+   if (!max_read_len)
+   return max_T5_msg_count;
+
+   if (max_read_len < data->T5_msg_size) {
+   WARN(1, "max read length is lesser than the T5 message size\n");
+   /* Return count of 1, as fallback */
+   return 1;
+   }
+   /*
+* Return maximum number of T5 messages in single i2c transaction
+* based on max read length.
+*/
+   return min(T5_msg_count_limit, max_T5_msg_count);
+}
+
 static irqreturn_t mxt_process_messages_t44(struct mxt_data *data)
 {
struct device *dev = >client->dev;
int ret;
-   u8 count, num_left;
+   u8 T5_msg_count, total_pending;
+   u8 total_processed = 0;
+   u8 processed_valid = 0;
 
/* Read T44 and T5 together */
ret = __mxt_read_reg(data->client, data->T44_address,
@@ -1477,18 +1500,19 @@ static irqreturn_t mxt_process_messages_t44(struct 
mxt_data *data)
return IRQ_NONE;
}
 
-   count = data->msg_buf[0];
+   T5_msg_count = data->msg_buf[0];
 
/*
 * This condition may be caused by the CHG line being configured in
 * Mode 0. It results in unnecessary I2C operations but it is benign.
 */
-   if (count == 0)
+   if (!T5_msg_count)
return IRQ_NONE;
 
-   if (count > data->max_reportid) {
-   dev_warn(dev, "T44 count %d exceeded max report id\n", count);
-   count = data->max_reportid;
+   if (T5_msg_count > data->max_reportid) {
+   dev_warn(dev, "T44 count %d exceeded max report id\n",
+T5_msg_count);
+   T5_msg_count = data->max_reportid;
}
 
/* Process first message */
@@ -1498,16 +1522,25 @@ static irqreturn_t mxt_process_messages_t44(struct 
mxt_data *data)
return IRQ_NONE;
}
 
-   num_left = count - 1;
+   total_pending = T5_msg_count - 1;
+   if (!total_pending)
+   goto end;
 
/* Process remaining messages if necessary */
-   if (num_left) {
-   ret = mxt_read_and_process_messages(data, num_left);
+   T5_msg_count = mxt_max_msg_read_count(data, total_pending);
+
+   do {
+   if ((total_pending - total_processed) < T5_msg_count)
+   T5_msg_count = total_pending - total_processed;
+   ret = mxt_read_and_process_messages(data, T5_msg_count);
if (ret < 0)
goto end;
-   else if (ret != num_left)
-   dev_warn(dev, "Unexpected invalid message\n");
-   }
+   total_processed += T5_msg_count;
+   processed_valid += ret;
+   } while (total_processed < total_pending);
+
+   if (processed_valid != total_pending)
+   dev_warn(dev, "Unexpected invalid message\n");
 
 end:
if (data->update_input) {
@@ -1522,9 +1555,10 @@ static int mxt_process_messages_until_invalid(struct 
mxt_data *data)
 {
struct device *dev = >client->dev;
int count, read;
-   u8 tries = 2;
+   int tries;
 
-   count = data->max_reportid;
+   count = mxt_max_msg_read_count(data, data->max_reportid);
+   tries = (data->max_reportid / count) + 1;
 
/* Read messages until we force an invalid */
do {
-- 
2.17.1



[PATCH v11 40/56] Input: atmel_mxt_ts - rely on calculated_crc rather than file config_crc

2020-05-08 Thread Jiada Wang
From: Kautuk Consul 

We now prefer to rely on the calculated CRC and not on the CRC stored in
the file.

The new logic is as follows:
1) stored CRC of file != calculated CRC of file, then refuse the possible
   corrupted file
2) calculated CRC of file != CRC of configuration in controller, then
   update configuration in controller
3) calculated CRC of file == CRC of configuration in controller, then
   ignore configuration file

Signed-off-by: Kautuk Consul 
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 65 +---
 1 file changed, 36 insertions(+), 29 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 9281a574ca80..75329f87927b 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -1891,7 +1891,7 @@ static int mxt_update_cfg(struct mxt_data *data, const 
struct firmware *fw)
int ret;
int offset;
int i;
-   u32 info_crc, config_crc, calculated_crc;
+   u32 info_crc, config_crc, calculated_crc = 0;
u16 crc_start = 0;
 
/* Make zero terminated copy of the OBP_RAW file */
@@ -1954,30 +1954,6 @@ static int mxt_update_cfg(struct mxt_data *data, const 
struct firmware *fw)
}
cfg.raw_pos += offset;
 
-   /*
-* The Info Block CRC is calculated over mxt_info and the object
-* table. If it does not match then we are trying to load the
-* configuration from a different chip or firmware version, so
-* the configuration CRC is invalid anyway.
-*/
-   if (info_crc == data->info_crc) {
-   if (config_crc == 0 || data->config_crc == 0) {
-   dev_info(dev, "CRC zero, attempting to apply config\n");
-   } else if (config_crc == data->config_crc) {
-   dev_dbg(dev, "Config CRC 0x%06X: OK\n",
-data->config_crc);
-   ret = 0;
-   goto release_raw;
-   } else {
-   dev_info(dev, "Config CRC 0x%06X: does not match file 
0x%06X\n",
-data->config_crc, config_crc);
-   }
-   } else {
-   dev_warn(dev,
-"Warning: Info CRC error - device=0x%06X 
file=0x%06X\n",
-data->info_crc, info_crc);
-   }
-
/* Malloc memory to store configuration */
cfg.start_ofs = MXT_OBJECT_START +
data->info->object_num * sizeof(struct mxt_object) +
@@ -2001,14 +1977,45 @@ static int mxt_update_cfg(struct mxt_data *data, const 
struct firmware *fw)
else
dev_warn(dev, "Could not find CRC start\n");
 
-   if (crc_start > cfg.start_ofs) {
+   if (crc_start > cfg.start_ofs)
calculated_crc = mxt_calculate_crc(cfg.mem,
   crc_start - cfg.start_ofs,
   cfg.mem_size);
 
-   if (config_crc > 0 && config_crc != calculated_crc)
-   dev_warn(dev, "Config CRC in file inconsistent, 
calculated=%06X, file=%06X\n",
-calculated_crc, config_crc);
+   /* If the CRC stored in the file is not the same as what
+* was calculated by mxt_calculate_crc, this means we
+* have to refuse the config file and abort download.
+*/
+   if (config_crc != calculated_crc) {
+   dev_warn(dev,
+"Config CRC in file inconsistent, calculated=%06X, 
file=%06X\n",
+calculated_crc, config_crc);
+   ret = 0;
+   goto release_mem;
+   }
+
+   /*
+* The Info Block CRC is calculated over mxt_info and the object
+* table. If it does not match then we are trying to load the
+* configuration from a different chip or firmware version, so
+* the configuration CRC is invalid anyway.
+*/
+   if (info_crc == data->info_crc) {
+   if (config_crc == 0 || data->config_crc == 0) {
+   dev_info(dev, "CRC zero, attempting to apply config\n");
+   } else if (config_crc == data->config_crc) {
+   dev_dbg(dev, "Config CRC 0x%06X: OK\n",
+   data->config_crc);
+   ret = 0;
+   goto release_mem;
+   } else {
+   dev_info(dev, "Config CRC 0x%06X: does not match file 
0x%06X\n",
+data->config_crc, config_crc);
+   }
+   } else {
+   dev_warn(dev,
+ 

[PATCH v11 45/56] Input: atmel_mxt_ts: return error from mxt_process_messages_until_invalid()

2020-05-08 Thread Jiada Wang
From: Dean Jenkins 

mxt_process_messages_until_invalid() failed to propagate the error
code from mxt_read_and_process_messages() so return the error code.

Signed-off-by: Dean Jenkins 
Signed-off-by: Deepak Das 
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index ed850a0bae69..7c530ffac1ba 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -1563,6 +1563,8 @@ static int mxt_process_messages_until_invalid(struct 
mxt_data *data)
/* Read messages until we force an invalid */
do {
read = mxt_read_and_process_messages(data, count);
+   if (read < 0)
+   return read;
if (read < count)
return 0;
} while (--tries);
-- 
2.17.1



[PATCH v11 34/56] Input: atmel_mxt_ts - implement I2C retries

2020-05-07 Thread Jiada Wang
From: Nick Dyer 

Some maXTouch chips (eg mXT1386) will not respond on the first I2C request
when they are in a sleep state. It must be retried after a delay for the
chip to wake up.

Signed-off-by: Nick Dyer 
Acked-by: Yufeng Shen 
(cherry picked from ndyer/linux/for-upstream commit 
63fd7a2cd03c3a572a5db39c52f4856819e1835d)
[gdavis: Forward port and fix conflicts.]
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 45 
 1 file changed, 30 insertions(+), 15 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index ab4eceac8fe7..152069c3e15d 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -217,6 +217,7 @@ enum t100_type {
 #define MXT_CRC_TIMEOUT1000/* msec */
 #define MXT_FW_RESET_TIME  3000/* msec */
 #define MXT_FW_CHG_TIMEOUT 300 /* msec */
+#define MXT_WAKEUP_TIME25  /* msec */
 #define MXT_REGULATOR_DELAY150 /* msec */
 #define MXT_CHG_DELAY  100 /* msec */
 #define MXT_POWERON_DELAY  150 /* msec */
@@ -721,6 +722,7 @@ static int __mxt_read_chunk(struct i2c_client *client,
struct i2c_msg xfer[2];
u8 buf[2];
int ret;
+   bool retry = false;
 
buf[0] = reg & 0xff;
buf[1] = (reg >> 8) & 0xff;
@@ -737,17 +739,22 @@ static int __mxt_read_chunk(struct i2c_client *client,
xfer[1].len = len;
xfer[1].buf = val;
 
-   ret = i2c_transfer(client->adapter, xfer, 2);
-   if (ret == 2) {
-   ret = 0;
-   } else {
-   if (ret >= 0)
-   ret = -EIO;
-   dev_err(>dev, "%s: i2c transfer failed (%d)\n",
-   __func__, ret);
+retry_read:
+   ret = i2c_transfer(client->adapter, xfer, ARRAY_SIZE(xfer));
+   if (ret != ARRAY_SIZE(xfer)) {
+   if (!retry) {
+   dev_dbg(>dev, "%s: i2c retry\n", __func__);
+   msleep(MXT_WAKEUP_TIME);
+   retry = true;
+   goto retry_read;
+   } else {
+   dev_err(>dev, "%s: i2c transfer failed (%d)\n",
+   __func__, ret);
+   return -EIO;
+   }
}
 
-   return ret;
+   return 0;
 }
 
 static int __mxt_read_reg(struct i2c_client *client,
@@ -778,6 +785,7 @@ static int __mxt_write_reg(struct i2c_client *client, u16 
reg, u16 len,
u8 *buf;
size_t count;
int ret;
+   bool retry = false;
 
count = len + 2;
buf = kmalloc(count, GFP_KERNEL);
@@ -788,14 +796,21 @@ static int __mxt_write_reg(struct i2c_client *client, u16 
reg, u16 len,
buf[1] = (reg >> 8) & 0xff;
memcpy([2], val, len);
 
+retry_write:
ret = i2c_master_send(client, buf, count);
-   if (ret == count) {
-   ret = 0;
-   } else {
-   if (ret >= 0)
+   if (ret != count) {
+   if (!retry) {
+   dev_dbg(>dev, "%s: i2c retry\n", __func__);
+   msleep(MXT_WAKEUP_TIME);
+   retry = true;
+   goto retry_write;
+   } else {
+   dev_err(>dev, "%s: i2c send failed (%d)\n",
+   __func__, ret);
ret = -EIO;
-   dev_err(>dev, "%s: i2c send failed (%d)\n",
-   __func__, ret);
+   }
+   } else {
+   ret = 0;
}
 
kfree(buf);
-- 
2.17.1



[PATCH v11 24/56] dt-bindings: input: atmel: support to specify input name

2020-05-07 Thread Jiada Wang
Support to specify input name

Signed-off-by: Jiada Wang 
---
 Documentation/devicetree/bindings/input/atmel,maxtouch.txt | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/Documentation/devicetree/bindings/input/atmel,maxtouch.txt 
b/Documentation/devicetree/bindings/input/atmel,maxtouch.txt
index 4705d7753c54..f084afa1660e 100644
--- a/Documentation/devicetree/bindings/input/atmel,maxtouch.txt
+++ b/Documentation/devicetree/bindings/input/atmel,maxtouch.txt
@@ -43,6 +43,8 @@ Optional properties for main touchpad device:
 - atmel,cfg_name: Provide name of configuration file in OBP_RAW format. This
 will be downloaded from the firmware loader on probe to the device.
 
+- atmel,input_name: Override name of input device from the default.
+
 Example:
 
touch@4b {
-- 
2.17.1



[PATCH v11 25/56] Input: atmel_mxt_ts - add config checksum attribute to sysfs

2020-05-07 Thread Jiada Wang
From: karl tsou 

Add config checksum attribute to sysfs

Signed-off-by: karl tsou 
Signed-off-by: Nick Dyer 
(cherry picked from ndyer/linux/for-upstream commit 
03477477ddbe5dcad42853ab3f84166a8f807acf)
[gdavis: Forward port and fix conflicts.]
Signed-off-by: George G. Davis 
[jiada: Add commit description
Rename mxt_config_crc_show to config_crc_show
Replace DEVICE_ATTR with DEVICE_ATTR_RO]
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 11 +++
 1 file changed, 11 insertions(+)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index affd2bf32969..780850343089 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -3124,6 +3124,15 @@ static int mxt_configure_objects(struct mxt_data *data,
return error;
 }
 
+/* Configuration crc check sum is returned as hex xx */
+static ssize_t config_crc_show(struct device *dev,
+  struct device_attribute *attr, char *buf)
+{
+   struct mxt_data *data = dev_get_drvdata(dev);
+
+   return scnprintf(buf, PAGE_SIZE, "%06x\n", data->config_crc);
+}
+
 /* Firmware Version is returned as Major.Minor.Build */
 static ssize_t fw_version_show(struct device *dev,
   struct device_attribute *attr, char *buf)
@@ -3477,12 +3486,14 @@ static DEVICE_ATTR_RO(fw_version);
 static DEVICE_ATTR_RO(hw_version);
 static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
 static DEVICE_ATTR_WO(update_cfg);
+static DEVICE_ATTR_RO(config_crc);
 
 static struct attribute *mxt_attrs[] = {
_attr_fw_version.attr,
_attr_hw_version.attr,
_attr_object.attr,
_attr_update_cfg.attr,
+   _attr_config_crc.attr,
NULL
 };
 
-- 
2.17.1



[PATCH v11 33/56] Input: atmel_mxt_ts - delay enabling IRQ when not using regulators

2020-05-07 Thread Jiada Wang
The path of enabling the IRQ in the probe function is not safe in level
triggered operation, if it was already powered up and there is a message
waiting on the device (eg finger down) because the object table has not yet
been read. This forces the ISR into a hard loop.

Delay enabling the interrupt until it is first needed, by set flag
IRQ_NOAUTOEN.

Signed-off-by: Jiada Wang 
CC: Dmitry Osipenko 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 7 +--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 7c9a738e633a..ab4eceac8fe7 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -3822,6 +3822,7 @@ static int mxt_probe(struct i2c_client *client, const 
struct i2c_device_id *id)
return error;
}
 
+   irq_set_status_flags(client->irq, IRQ_NOAUTOEN);
error = devm_request_threaded_irq(>dev, client->irq,
  NULL, mxt_interrupt, IRQF_ONESHOT,
  client->name, data);
@@ -3831,17 +3832,19 @@ static int mxt_probe(struct i2c_client *client, const 
struct i2c_device_id *id)
}
 
if (data->suspend_mode == MXT_SUSPEND_REGULATOR) {
+   enable_irq(data->irq);
+
error = mxt_probe_regulators(data);
if (error)
return error;
+
+   disable_irq(data->irq);
} else if (data->reset_gpio) {
msleep(MXT_RESET_GPIO_TIME);
gpiod_set_value(data->reset_gpio, 1);
msleep(MXT_RESET_INVALID_CHG);
}
 
-   disable_irq(data->irq);
-
error = mxt_initialize(data);
if (error)
return error;
-- 
2.17.1



[PATCH v11 35/56] Input: atmel_mxt_ts - orientation is not present in hover

2020-05-07 Thread Jiada Wang
From: Nick Dyer 

When in hover, the orientation information is not sent

Signed-off-by: Nick Dyer 
(cherry picked from ndyer/linux/for-upstream commit 
0c885d5bd276bd9240c43aa046fc407cbe2ae864)
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 4 
 1 file changed, 4 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 152069c3e15d..1027ebbc3978 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -1027,10 +1027,6 @@ static void mxt_proc_t100_message(struct mxt_data *data, 
u8 *message)
distance = MXT_DISTANCE_HOVERING;
hover = true;
active = true;
-
-   if (data->t100_aux_vect)
-   orientation = message[data->t100_aux_vect];
-
break;
 
case MXT_T100_TYPE_FINGER:
-- 
2.17.1



[PATCH v11 32/56] Input: atmel_mxt_ts - make bootloader interrupt driven

2020-05-07 Thread Jiada Wang
From: Nick Dyer 

Make bootloader interrupt driven

Signed-off-by: Nick Dyer 
(cherry picked from ndyer/linux/for-upstream commit 
67a3eea0cfc724c3c2a7410ac064f74227c7c6ef)
[gdavis: Resolve forward port conflicts due to applying upstream
 commit 96a938aa214e ("Input: atmel_mxt_ts - remove platform
 data support").]
Signed-off-by: George G. Davis 
[jiada: Replace two use msecs_to_jiffies() instead of HZ,
remove check of >flash->work
don't poll to call mxt_check_bootloader() in mxt_check_bootloader()]
Reported-by: kbuild test robot 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 127 ++-
 1 file changed, 57 insertions(+), 70 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 0d77ae455fde..7c9a738e633a 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -27,6 +27,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -219,6 +220,7 @@ enum t100_type {
 #define MXT_REGULATOR_DELAY150 /* msec */
 #define MXT_CHG_DELAY  100 /* msec */
 #define MXT_POWERON_DELAY  150 /* msec */
+#define MXT_BOOTLOADER_WAIT36E5/* 1 minute */
 
 /* Command to unlock bootloader */
 #define MXT_UNLOCK_CMD_MSB 0xaa
@@ -300,6 +302,7 @@ struct mxt_fw_frame {
 
 /* Firmware update context */
 struct mxt_flash {
+   struct mxt_data *data;
const struct firmware *fw;
struct mxt_fw_frame *frame;
loff_t pos;
@@ -307,8 +310,8 @@ struct mxt_flash {
unsigned int count;
unsigned int retry;
u8 previous;
-   bool complete;
-   bool wait;
+   struct completion flash_completion;
+   struct delayed_work work;
 };
 
 /* Each client has this additional data */
@@ -357,6 +360,7 @@ struct mxt_data {
char *cfg_name;
const char *pcfg_name;
const char *input_name;
+   struct mxt_flash *flash;
 
/* Cached parameters from object table */
u16 T5_address;
@@ -601,35 +605,19 @@ static int mxt_write_firmware_frame(struct mxt_data 
*data, struct mxt_flash *f)
   f->frame_size);
 }
 
-static int mxt_check_bootloader(struct mxt_data *data, struct mxt_flash *f)
+static int mxt_check_bootloader(struct mxt_data *data)
 {
struct device *dev = >client->dev;
+   struct mxt_flash *f = data->flash;
u8 state;
int ret;
 
-   if (f->wait) {
-   /*
-* In application update mode, the interrupt
-* line signals state transitions. We must wait for the
-* CHG assertion before reading the status byte.
-* Once the status byte has been read, the line is deasserted.
-*/
-   ret = mxt_wait_for_completion(data, >chg_completion,
- MXT_FW_CHG_TIMEOUT);
-   if (ret) {
-   /*
-* TODO: handle -ERESTARTSYS better by terminating
-* fw update process before returning to userspace
-* by writing length 0x000 to device (iff we are in
-* WAITING_FRAME_DATA state).
-*/
-   dev_warn(dev, "Update wait error %d\n", ret);
-   return ret;
-   }
+   /* Handle interrupt after download/flash process */
+   if (f->pos >= f->fw->size) {
+   complete(>flash_completion);
+   return 0;
}
 
-   f->wait = false;
-
ret = mxt_bootloader_read(data, , 1);
if (ret)
return ret;
@@ -644,7 +632,6 @@ static int mxt_check_bootloader(struct mxt_data *data, 
struct mxt_flash *f)
ret = mxt_send_bootloader_cmd(data, true);
if (ret)
return ret;
-   f->wait = true;
 
break;
 
@@ -658,14 +645,11 @@ static int mxt_check_bootloader(struct mxt_data *data, 
struct mxt_flash *f)
if (ret)
return ret;
 
-   f->wait = true;
-
break;
 
case MXT_FRAME_CRC_CHECK:
if (f->previous != MXT_WAITING_FRAME_DATA)
goto unexpected;
-   f->wait = true;
break;
 
case MXT_FRAME_CRC_PASS:
@@ -676,16 +660,13 @@ static int mxt_check_bootloader(struct mxt_data *data, 
struct mxt_flash *f)
f->retry = 0;
f->pos += f->frame_size;
f->count++;
-   f->wait = true;
 
-   if (f->pos >= f->fw->size) {
-   f->complete = true;
+   if (f->pos >= f->fw->size)
  

[PATCH v11 28/56] Input: atmel_mxt_ts - refactor code to enter bootloader into separate func

2020-05-07 Thread Jiada Wang
From: Nick Dyer 

Refactor code to enter bootloader into separate func

Signed-off-by: Nick Dyer 
(cherry picked from ndyer/linux/for-upstream commit 
bedd706a32522b946467e15f4f4f24de86a1b4d7)
[gdavis: Resolve forward port conflicts due to applying upstream
 commit 96a938aa214e ("Input: atmel_mxt_ts - remove platform
 data support").]
Signed-off-by: George G. Davis 
[jiada: Squash change from ndyer/linux/for-upstream commit 
d691d3ee6c6de84b38464a42
3207b3e23cb9dc3a
- Input: atmel_mxt_ts - check firmware format before entering bootloader
Add commit description]
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 43 +++-
 1 file changed, 27 insertions(+), 16 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index bb4fc13defea..674763dddcd3 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -3253,23 +3253,10 @@ static int mxt_check_firmware_format(struct device *dev,
return -EINVAL;
 }
 
-static int mxt_load_fw(struct device *dev)
+static int mxt_enter_bootloader(struct mxt_data *data)
 {
-   struct mxt_data *data = dev_get_drvdata(dev);
-   struct mxt_flash f = { 0, };
int ret;
 
-   ret = request_firmware(, data->fw_name, dev);
-   if (ret) {
-   dev_err(dev, "Unable to open firmware %s\n", data->fw_name);
-   return ret;
-   }
-
-   /* Check for incorrect enc file */
-   ret = mxt_check_firmware_format(dev, f.fw);
-   if (ret)
-   goto release_firmware;
-
if (data->suspended) {
if (data->suspend_mode == MXT_SUSPEND_REGULATOR)
mxt_regulator_enable(data);
@@ -3287,14 +3274,14 @@ static int mxt_load_fw(struct device *dev)
ret = mxt_t6_command(data, MXT_COMMAND_RESET,
 MXT_BOOT_VALUE, false);
if (ret)
-   goto release_firmware;
+   return ret;
 
msleep(MXT_RESET_TIME);
 
/* Do not need to scan since we know family ID */
ret = mxt_lookup_bootloader_address(data, 0);
if (ret)
-   goto release_firmware;
+   return ret;
 
mxt_sysfs_remove(data);
mxt_free_input_device(data);
@@ -3305,6 +3292,30 @@ static int mxt_load_fw(struct device *dev)
 
reinit_completion(>bl_completion);
 
+   return 0;
+}
+
+static int mxt_load_fw(struct device *dev)
+{
+   struct mxt_data *data = dev_get_drvdata(dev);
+   struct mxt_flash f = { 0, };
+   int ret;
+
+   ret = request_firmware(, data->fw_name, dev);
+   if (ret) {
+   dev_err(dev, "Unable to open firmware %s\n", data->fw_name);
+   return ret;
+   }
+
+   /* Check for incorrect enc file */
+   ret = mxt_check_firmware_format(dev, f.fw);
+   if (ret)
+   goto release_firmware;
+
+   ret = mxt_enter_bootloader(data);
+   if (ret)
+   goto release_firmware;
+
ret = mxt_check_bootloader(data, MXT_WAITING_BOOTLOAD_CMD, false);
if (ret) {
/* Bootloader may still be unlocked from previous attempt */
-- 
2.17.1



[PATCH v11 31/56] Input: atmel_mxt_ts - rename bl_completion to chg_completion

2020-05-07 Thread Jiada Wang
From: Nick Dyer 

Rename bl_completion to chg_completion

Signed-off-by: Nick Dyer 
(cherry picked from ndyer/linux/for-upstream commit 
dda8453bfb44216645ede798918a314d4fca2481)
[gdavis: Resolve forward port conflicts due to applying upstream
 commit 96a938aa214e ("Input: atmel_mxt_ts - remove platform
 data support").]
Signed-off-by: George G. Davis 
[jiada: call complete(>chg_completion) only when in_bootloader is TRUE
Add commit description]
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 23 +++
 1 file changed, 11 insertions(+), 12 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 4d2240971387..0d77ae455fde 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -383,9 +383,6 @@ struct mxt_data {
u8 T100_reportid_max;
u16 T107_address;
 
-   /* for fw update in bootloader */
-   struct completion bl_completion;
-
/* for reset handling */
struct completion reset_completion;
 
@@ -397,6 +394,9 @@ struct mxt_data {
 
enum mxt_suspend_mode suspend_mode;
 
+   /* for power up handling */
+   struct completion chg_completion;
+
/* Indicates whether device is in suspend */
bool suspended;
 
@@ -614,7 +614,7 @@ static int mxt_check_bootloader(struct mxt_data *data, 
struct mxt_flash *f)
 * CHG assertion before reading the status byte.
 * Once the status byte has been read, the line is deasserted.
 */
-   ret = mxt_wait_for_completion(data, >bl_completion,
+   ret = mxt_wait_for_completion(data, >chg_completion,
  MXT_FW_CHG_TIMEOUT);
if (ret) {
/*
@@ -1415,8 +1415,7 @@ static irqreturn_t mxt_interrupt(int irq, void *dev_id)
struct mxt_data *data = dev_id;
 
if (data->in_bootloader) {
-   /* bootloader state transition completion */
-   complete(>bl_completion);
+   complete(>chg_completion);
return IRQ_HANDLED;
}
 
@@ -2180,9 +2179,9 @@ static void mxt_regulator_enable(struct mxt_data *data)
msleep(MXT_CHG_DELAY);
 
 retry_wait:
-   reinit_completion(>bl_completion);
+   reinit_completion(>chg_completion);
data->in_bootloader = true;
-   error = mxt_wait_for_completion(data, >bl_completion,
+   error = mxt_wait_for_completion(data, >chg_completion,
MXT_POWERON_DELAY);
if (error == -EINTR)
goto retry_wait;
@@ -3342,7 +3341,7 @@ static int mxt_enter_bootloader(struct mxt_data *data)
enable_irq(data->irq);
}
 
-   reinit_completion(>bl_completion);
+   reinit_completion(>chg_completion);
 
return 0;
 }
@@ -3378,7 +3377,7 @@ static int mxt_load_fw(struct device *dev)
}
 
/* Wait for flash. */
-   ret = mxt_wait_for_completion(data, >bl_completion,
+   ret = mxt_wait_for_completion(data, >chg_completion,
  MXT_FW_RESET_TIME);
if (ret)
goto disable_irq;
@@ -3389,7 +3388,7 @@ static int mxt_load_fw(struct device *dev)
 * the CHG line after bootloading has finished, so ignore potential
 * errors.
 */
-   mxt_wait_for_completion(data, >bl_completion, MXT_FW_RESET_TIME);
+   mxt_wait_for_completion(data, >chg_completion, MXT_FW_RESET_TIME);
 
data->in_bootloader = false;
 disable_irq:
@@ -3811,7 +3810,7 @@ static int mxt_probe(struct i2c_client *client, const 
struct i2c_device_id *id)
data->irq = client->irq;
i2c_set_clientdata(client, data);
 
-   init_completion(>bl_completion);
+   init_completion(>chg_completion);
init_completion(>reset_completion);
init_completion(>crc_completion);
 
-- 
2.17.1



[PATCH v11 26/56] Input: atmel_mxt_ts - rename mxt_object_show to object_show

2020-05-07 Thread Jiada Wang
Rename mxt_object_show() to object_show(), so that object attr
can also use DEVICE_ATTR_[RO|WO] to align with other attrs.

Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 780850343089..c3dd6c486c12 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -3171,8 +3171,8 @@ static ssize_t mxt_show_instance(char *buf, int count,
return count;
 }
 
-static ssize_t mxt_object_show(struct device *dev,
-   struct device_attribute *attr, char *buf)
+static ssize_t object_show(struct device *dev,
+  struct device_attribute *attr, char *buf)
 {
struct mxt_data *data = dev_get_drvdata(dev);
struct mxt_object *object;
@@ -3484,7 +3484,7 @@ static const struct attribute_group mxt_fw_attr_group = {
 
 static DEVICE_ATTR_RO(fw_version);
 static DEVICE_ATTR_RO(hw_version);
-static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
+static DEVICE_ATTR_RO(object);
 static DEVICE_ATTR_WO(update_cfg);
 static DEVICE_ATTR_RO(config_crc);
 
-- 
2.17.1



[PATCH v11 30/56] Input: atmel_mxt_ts - improve bootloader state machine handling

2020-05-07 Thread Jiada Wang
From: Nick Dyer 

The code is much clearer if we switch on the actual state the bootloader
is in, rather than the state we want it to be in, and allows the removal
of a goto retry tangle.

Signed-off-by: Nick Dyer 
(cherry picked from ndyer/linux/for-upstream commit 
463e15ee95ee6e6274017ff645839dbe34d75c99)
[gdavis: Squash fix from George G. Davis:
 - input: atmel_mxt_ts - Fix 'mxt_send_bootloader_cmd' was not
   declared warning]
Signed-off-by: George G. Davis 
[jiada: only wait on some status change,
cleanup code style]
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 165 +--
 1 file changed, 95 insertions(+), 70 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index e2538c2dd3f7..4d2240971387 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -306,6 +306,9 @@ struct mxt_flash {
size_t frame_size;
unsigned int count;
unsigned int retry;
+   u8 previous;
+   bool complete;
+   bool wait;
 };
 
 /* Each client has this additional data */
@@ -584,15 +587,27 @@ static int mxt_probe_bootloader(struct mxt_data *data, 
bool alt_address)
return 0;
 }
 
-static int mxt_check_bootloader(struct mxt_data *data, unsigned int state,
-   bool wait)
+static int mxt_send_bootloader_cmd(struct mxt_data *data, bool unlock);
+
+static int mxt_write_firmware_frame(struct mxt_data *data, struct mxt_flash *f)
+{
+   f->frame = (struct mxt_fw_frame *)(f->fw->data + f->pos);
+
+   /* Take account of CRC bytes */
+   f->frame_size = __be16_to_cpu(f->frame->size) + 2U;
+
+   /* Write one frame to device */
+   return mxt_bootloader_write(data, f->fw->data + f->pos,
+  f->frame_size);
+}
+
+static int mxt_check_bootloader(struct mxt_data *data, struct mxt_flash *f)
 {
struct device *dev = >client->dev;
-   u8 val;
+   u8 state;
int ret;
 
-recheck:
-   if (wait) {
+   if (f->wait) {
/*
 * In application update mode, the interrupt
 * line signals state transitions. We must wait for the
@@ -608,40 +623,96 @@ static int mxt_check_bootloader(struct mxt_data *data, 
unsigned int state,
 * by writing length 0x000 to device (iff we are in
 * WAITING_FRAME_DATA state).
 */
-   dev_err(dev, "Update wait error %d\n", ret);
+   dev_warn(dev, "Update wait error %d\n", ret);
return ret;
}
}
 
-   ret = mxt_bootloader_read(data, , 1);
+   f->wait = false;
+
+   ret = mxt_bootloader_read(data, , 1);
if (ret)
return ret;
 
+   /* Remove don't care bits */
+   if (state & ~MXT_BOOT_STATUS_MASK)
+   state &= ~MXT_BOOT_STATUS_MASK;
+
switch (state) {
case MXT_WAITING_BOOTLOAD_CMD:
+   dev_info(dev, "Unlocking bootloader\n");
+   ret = mxt_send_bootloader_cmd(data, true);
+   if (ret)
+   return ret;
+   f->wait = true;
+
+   break;
+
case MXT_WAITING_FRAME_DATA:
-   case MXT_APP_CRC_FAIL:
-   val &= ~MXT_BOOT_STATUS_MASK;
+   if (f->previous != MXT_WAITING_BOOTLOAD_CMD &&
+   f->previous != MXT_FRAME_CRC_PASS &&
+   f->previous != MXT_FRAME_CRC_FAIL)
+   goto unexpected;
+
+   ret = mxt_write_firmware_frame(data, f);
+   if (ret)
+   return ret;
+
+   f->wait = true;
+
+   break;
+
+   case MXT_FRAME_CRC_CHECK:
+   if (f->previous != MXT_WAITING_FRAME_DATA)
+   goto unexpected;
+   f->wait = true;
break;
+
case MXT_FRAME_CRC_PASS:
-   if (val == MXT_FRAME_CRC_CHECK) {
-   goto recheck;
-   } else if (val == MXT_FRAME_CRC_FAIL) {
-   dev_err(dev, "Bootloader CRC fail\n");
-   return -EINVAL;
+   if (f->previous != MXT_FRAME_CRC_CHECK)
+   goto unexpected;
+
+   /* Next frame */
+   f->retry = 0;
+   f->pos += f->frame_size;
+   f->count++;
+   f->wait = true;
+
+   if (f->pos >= f->fw->size) {
+   f->complete = true;
+   dev_info(dev, "Sent %u frames, %zu bytes\n",
+   f->count, f->fw->size

[PATCH v11 23/56] Input: atmel_mxt_ts - allow input name to be specified in platform data

2020-05-07 Thread Jiada Wang
From: Nick Dyer 

Android systems identify the input device and map to IDC file by using the
input device name. To avoid unnecessary deltas to the driver file, allow
this to be set from the platform data.

Signed-off-by: Nick Dyer 
(cherry picked from ndyer/linux/for-upstream commit 
cbf94a7bda754d2e1899d9f50313a0bccc91422d)
[gdavis: Resolve forward port conflicts due to applying upstream
 commit 96a938aa214e ("Input: atmel_mxt_ts - remove platform
 data support").]
Signed-off-by: George G. Davis 
[jiada: Separate Documentation/ portion change to another commit]
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 9 -
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 85b903b8d5c9..affd2bf32969 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -337,6 +337,7 @@ struct mxt_data {
char *fw_name;
char *cfg_name;
const char *pcfg_name;
+   const char *input_name;
 
/* Cached parameters from object table */
u16 T5_address;
@@ -2413,7 +2414,11 @@ static int mxt_initialize_input_device(struct mxt_data 
*data)
if (!input_dev)
return -ENOMEM;
 
-   input_dev->name = "Atmel maXTouch Touchscreen";
+   if (data->input_name)
+   input_dev->name = data->input_name;
+   else
+   input_dev->name = "Atmel maXTouch Touchscreen";
+
input_dev->phys = data->phys;
input_dev->id.bustype = BUS_I2C;
input_dev->dev.parent = dev;
@@ -3649,6 +3654,8 @@ static int mxt_parse_device_properties(struct mxt_data 
*data)
 
device_property_read_string(dev, "atmel,cfg_name", >pcfg_name);
 
+   device_property_read_string(dev, "atmel,input_name", >input_name);
+
if (device_property_present(dev, keymap_property)) {
n_keys = device_property_count_u32(dev, keymap_property);
if (n_keys <= 0) {
-- 
2.17.1



[PATCH v11 22/56] dt-bindings: input: atmel: provide name of configuration file

2020-05-07 Thread Jiada Wang
Add support to set name of configuration file

Signed-off-by: Jiada Wang 
---
 Documentation/devicetree/bindings/input/atmel,maxtouch.txt | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/Documentation/devicetree/bindings/input/atmel,maxtouch.txt 
b/Documentation/devicetree/bindings/input/atmel,maxtouch.txt
index 530312fc7a99..4705d7753c54 100644
--- a/Documentation/devicetree/bindings/input/atmel,maxtouch.txt
+++ b/Documentation/devicetree/bindings/input/atmel,maxtouch.txt
@@ -40,6 +40,9 @@ Optional properties for main touchpad device:
 - vdd: vdd: phandle to Power supply regulator
 - avdd: phandle to Analog Power supply regulator
 
+- atmel,cfg_name: Provide name of configuration file in OBP_RAW format. This
+will be downloaded from the firmware loader on probe to the device.
+
 Example:
 
touch@4b {
-- 
2.17.1



[PATCH v11 20/56] Input: atmel_mxt_ts - handle cfg filename via pdata/sysfs

2020-05-07 Thread Jiada Wang
From: Nick Dyer 

There may be multiple maXTouch chips on a single device which will require
different configuration files. Add a platform data value for the
configuration filename.

Add sysfs entry to write configuration file if the platform data is not
set.

Split out the object initialisation code from mxt_initialize() into
mxt_configure_objects() to allow this.

Signed-off-by: Nick Dyer 
Acked-by: Yufeng Shen 
(cherry picked from ndyer/linux/for-upstream commit 
71a2a4d1954460b949a16b607f72bafab294ca79)
[gdavis: Resolve forward port conflicts due to applying upstream
 commit 96a938aa214e ("Input: atmel_mxt_ts - remove platform
 data support").]
Signed-off-by: George G. Davis 
[gdavis: Squash fix from Vladimir Zapolskiy:
 - Input: atmel_mxt_ts - fix error paths in mxt_configure_objects()]
Signed-off-by: Vladimir Zapolskiy 
[jiada: Separate Documentation/ portion change to another commit
Rename mxt_update_cfg_store to update_cfg_store
Replace DEVICE_ATTR with DEVICE_ATTR_WO
Allow to fallback to legacy cfg name "maxtouch.cfg",
if atmel,cfg_name isn't specified in device-tree]
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 85 ++--
 1 file changed, 79 insertions(+), 6 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 720574417219..503e70603a67 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -335,6 +335,8 @@ struct mxt_data {
struct regulator *reg_vdd;
struct regulator *reg_avdd;
char *fw_name;
+   char *cfg_name;
+   const char *pcfg_name;
 
/* Cached parameters from object table */
u16 T5_address;
@@ -377,6 +379,9 @@ struct mxt_data {
 
/* Indicates whether device is in suspend */
bool suspended;
+
+   /* Indicates whether device is updating configuration */
+   bool updating_config;
 };
 
 struct mxt_vb2_buffer {
@@ -2578,8 +2583,11 @@ static int mxt_initialize(struct mxt_data *data)
if (error)
return error;
 
-   error = request_firmware_nowait(THIS_MODULE, true, MXT_CFG_NAME,
-   >dev, GFP_KERNEL, data,
+   error = request_firmware_nowait(THIS_MODULE, true,
+   data->cfg_name ?
+   data->cfg_name : MXT_CFG_NAME,
+   >dev,
+   GFP_KERNEL, data,
mxt_config_cb);
if (error) {
dev_err(>dev, "Failed to invoke firmware loader: %d\n",
@@ -3081,19 +3089,21 @@ static int mxt_configure_objects(struct mxt_data *data,
error = mxt_init_t7_power_cfg(data);
if (error) {
dev_err(dev, "Failed to initialize power cfg\n");
-   return error;
+   goto err_free_object_table;
}
 
if (cfg) {
error = mxt_update_cfg(data, cfg);
-   if (error)
+   if (error) {
dev_warn(dev, "Error %d updating config\n", error);
+   goto err_free_object_table;
+   }
}
 
if (data->multitouch) {
error = mxt_initialize_input_device(data);
if (error)
-   return error;
+   goto err_free_object_table;
} else {
dev_warn(dev, "No touch object detected\n");
}
@@ -3101,6 +3111,10 @@ static int mxt_configure_objects(struct mxt_data *data,
mxt_debug_init(data);
 
return 0;
+
+err_free_object_table:
+   mxt_free_object_table(data);
+   return error;
 }
 
 /* Firmware Version is returned as Major.Minor.Build */
@@ -3392,6 +3406,55 @@ static ssize_t update_fw_store(struct device *dev,
return count;
 }
 
+static ssize_t update_cfg_store(struct device *dev,
+   struct device_attribute *attr,
+   const char *buf, size_t count)
+{
+   struct mxt_data *data = dev_get_drvdata(dev);
+   const struct firmware *cfg;
+   int ret;
+
+   ret = mxt_update_file_name(dev, >cfg_name, buf, count);
+   if (ret)
+   return ret;
+
+   ret = request_firmware(, data->cfg_name, dev);
+   if (ret < 0) {
+   dev_err(dev, "Failure to request config file %s\n",
+   data->cfg_name);
+   ret = -ENOENT;
+   goto out;
+   }
+
+   data->updating_config = true;
+
+   mxt_free_input_device(data);
+
+   if (data->suspended) {
+   if (data->suspend_mode == MXT_SUSPEND_REGULATOR) {
+   enable_irq(

[PATCH v11 27/56] Input: atmel_mxt_ts - refactor firmware flash to extract context into struct

2020-05-07 Thread Jiada Wang
From: Nick Dyer 

Refactor firmware flash to extract context into struct

Signed-off-by: Nick Dyer 
(cherry picked from ndyer/linux/for-upstream commit 
1bbe20ff3dcd6612e7942c495929eae5c138ece2)
Signed-off-by: George G. Davis 
[jiada: Add commit description]
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 59 +++-
 1 file changed, 36 insertions(+), 23 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index c3dd6c486c12..bb4fc13defea 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -292,6 +292,22 @@ struct mxt_cfg {
struct mxt_info info;
 };
 
+/* Firmware frame structure */
+struct mxt_fw_frame {
+   __be16 size;
+   u8 data[];
+};
+
+/* Firmware update context */
+struct mxt_flash {
+   const struct firmware *fw;
+   struct mxt_fw_frame *frame;
+   loff_t pos;
+   size_t frame_size;
+   unsigned int count;
+   unsigned int retry;
+};
+
 /* Each client has this additional data */
 struct mxt_data {
struct i2c_client *client;
@@ -3240,21 +3256,17 @@ static int mxt_check_firmware_format(struct device *dev,
 static int mxt_load_fw(struct device *dev)
 {
struct mxt_data *data = dev_get_drvdata(dev);
-   const struct firmware *fw = NULL;
-   unsigned int frame_size;
-   unsigned int pos = 0;
-   unsigned int retry = 0;
-   unsigned int frame = 0;
+   struct mxt_flash f = { 0, };
int ret;
 
-   ret = request_firmware(, data->fw_name, dev);
+   ret = request_firmware(, data->fw_name, dev);
if (ret) {
dev_err(dev, "Unable to open firmware %s\n", data->fw_name);
return ret;
}
 
/* Check for incorrect enc file */
-   ret = mxt_check_firmware_format(dev, fw);
+   ret = mxt_check_firmware_format(dev, f.fw);
if (ret)
goto release_firmware;
 
@@ -3308,41 +3320,42 @@ static int mxt_load_fw(struct device *dev)
goto disable_irq;
}
 
-   while (pos < fw->size) {
+   while (f.pos < f.fw->size) {
+   f.frame = (struct mxt_fw_frame *)(f.fw->data + f.pos);
+
ret = mxt_check_bootloader(data, MXT_WAITING_FRAME_DATA, true);
if (ret)
goto disable_irq;
 
-   frame_size = ((*(fw->data + pos) << 8) | *(fw->data + pos + 1));
-
/* Take account of CRC bytes */
-   frame_size += 2;
+   f.frame_size = __be16_to_cpu(f.frame->size) + 2U;
 
/* Write one frame to device */
-   ret = mxt_bootloader_write(data, fw->data + pos, frame_size);
+   ret = mxt_bootloader_write(data, f.fw->data + f.pos,
+  f.frame_size);
if (ret)
goto disable_irq;
 
ret = mxt_check_bootloader(data, MXT_FRAME_CRC_PASS, true);
if (ret) {
-   retry++;
+   f.retry++;
 
/* Back off by 20ms per retry */
-   msleep(retry * 20);
+   msleep(f.retry * 20);
 
-   if (retry > 20) {
+   if (f.retry > 20) {
dev_err(dev, "Retry count exceeded\n");
goto disable_irq;
}
} else {
-   retry = 0;
-   pos += frame_size;
-   frame++;
+   f.retry = 0;
+   f.pos += f.frame_size;
+   f.count++;
}
 
-   if (frame % 50 == 0)
-   dev_dbg(dev, "Sent %d frames, %d/%zd bytes\n",
-   frame, pos, fw->size);
+   if (f.count % 50 == 0)
+   dev_dbg(dev, "Sent %u frames, %lld/%zu bytes\n",
+   f.count, f.pos, f.fw->size);
}
 
/* Wait for flash. */
@@ -3351,7 +3364,7 @@ static int mxt_load_fw(struct device *dev)
if (ret)
goto disable_irq;
 
-   dev_dbg(dev, "Sent %d frames, %d bytes\n", frame, pos);
+   dev_dbg(dev, "Sent %u frames, %lld bytes\n", f.count, f.pos);
 
/*
 * Wait for device to reset. Some bootloader versions do not assert
@@ -3365,7 +3378,7 @@ static int mxt_load_fw(struct device *dev)
 disable_irq:
disable_irq(data->irq);
 release_firmware:
-   release_firmware(fw);
+   release_firmware(f.fw);
return ret;
 }
 
-- 
2.17.1



[PATCH v11 21/56] Input: atmel_mxt_ts - check data->input_dev is not null in mxt_input_sync()

2020-05-07 Thread Jiada Wang
From: Janus Cheng 

* Symptom: if update_fw and update_cfg, kernel panic occurs.
* Reproducibility: 10%
* Root Cause:
  - If update_fw, the T6 will send a CFG_ERR message periodically.
  - After that, update_cfg process begin, the mxt_update_cfg_store() will
invoke mxt_free_input_device() and nullify data->input_dev.
  - The CFG_ERR message will trigger mxt_interrupt(), and mxt_input_sync()
will be invoked by mxt_process_messages_t44(). And mxt_input_sync()
references a NULL data->input_dev and kernel panic occurs.

TrackerRMS TKT-004235

Signed-off-by: Janus Cheng 
Signed-off-by: Nick Dyer 
(cherry picked from ndyer/linux/for-upstream commit 
c909ada856861f305653b127db3ea0fa60264331)
[gdavis: Resolve forward port conflicts due to applying upstream
 commit 96a938aa214e ("Input: atmel_mxt_ts - remove platform
 data support").]
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 8 +---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 503e70603a67..85b903b8d5c9 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -838,9 +838,11 @@ static void mxt_input_button(struct mxt_data *data, u8 
*message)
 
 static void mxt_input_sync(struct mxt_data *data)
 {
-   input_mt_report_pointer_emulation(data->input_dev,
- data->t19_num_keys);
-   input_sync(data->input_dev);
+   if (data->input_dev) {
+   input_mt_report_pointer_emulation(data->input_dev,
+ data->t19_num_keys);
+   input_sync(data->input_dev);
+   }
 }
 
 static void mxt_proc_t9_message(struct mxt_data *data, u8 *message)
-- 
2.17.1



[PATCH v11 29/56] Input: atmel_mxt_ts - combine bootloader version query with probe

2020-05-07 Thread Jiada Wang
From: Nick Dyer 

This removes some complexity from the bootloader state machine, and means
that we always output some debug about the version as soon as we start
talking to the bootloader.

Signed-off-by: Nick Dyer 
(cherry picked from ndyer/linux/for-upstream commit 
a2d141f170c80fea6663af98aab0be32abc0ddb0)
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 45 +++-
 1 file changed, 13 insertions(+), 32 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 674763dddcd3..e2538c2dd3f7 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -559,47 +559,31 @@ static int mxt_probe_bootloader(struct mxt_data *data, 
bool alt_address)
 {
struct device *dev = >client->dev;
int error;
-   u8 val;
-   bool crc_failure;
+   u8 buf[3];
+   bool crc_failure, extended_id;
 
error = mxt_lookup_bootloader_address(data, alt_address);
if (error)
return error;
 
-   error = mxt_bootloader_read(data, , 1);
+   /* Check bootloader status and version information */
+   error = mxt_bootloader_read(data, buf, sizeof(buf));
if (error)
return error;
 
-   /* Check app crc fail mode */
-   crc_failure = (val & ~MXT_BOOT_STATUS_MASK) == MXT_APP_CRC_FAIL;
+   crc_failure = (buf[0] & ~MXT_BOOT_STATUS_MASK) == MXT_APP_CRC_FAIL;
+   extended_id = buf[0] & MXT_BOOT_EXTENDED_ID;
 
-   dev_err(dev, "Detected bootloader, status:%02X%s\n",
-   val, crc_failure ? ", APP_CRC_FAIL" : "");
+   dev_info(dev, "Found bootloader addr:%02x ID:%u%s%u%s\n",
+data->bootloader_addr,
+extended_id ? (buf[1] & MXT_BOOT_ID_MASK) : buf[0],
+extended_id ? " version:" : "",
+extended_id ? buf[2] : 0,
+crc_failure ? ", APP_CRC_FAIL" : "");
 
return 0;
 }
 
-static u8 mxt_get_bootloader_version(struct mxt_data *data, u8 val)
-{
-   struct device *dev = >client->dev;
-   u8 buf[3];
-
-   if (val & MXT_BOOT_EXTENDED_ID) {
-   if (mxt_bootloader_read(data, [0], 3) != 0) {
-   dev_err(dev, "%s: i2c failure\n", __func__);
-   return val;
-   }
-
-   dev_dbg(dev, "Bootloader ID:%d Version:%d\n", buf[1], buf[2]);
-
-   return buf[0];
-   } else {
-   dev_dbg(dev, "Bootloader ID:%d\n", val & MXT_BOOT_ID_MASK);
-
-   return val;
-   }
-}
-
 static int mxt_check_bootloader(struct mxt_data *data, unsigned int state,
bool wait)
 {
@@ -633,9 +617,6 @@ static int mxt_check_bootloader(struct mxt_data *data, 
unsigned int state,
if (ret)
return ret;
 
-   if (state == MXT_WAITING_BOOTLOAD_CMD)
-   val = mxt_get_bootloader_version(data, val);
-
switch (state) {
case MXT_WAITING_BOOTLOAD_CMD:
case MXT_WAITING_FRAME_DATA:
@@ -3279,7 +3260,7 @@ static int mxt_enter_bootloader(struct mxt_data *data)
msleep(MXT_RESET_TIME);
 
/* Do not need to scan since we know family ID */
-   ret = mxt_lookup_bootloader_address(data, 0);
+   ret = mxt_probe_bootloader(data, 0);
if (ret)
return ret;
 
-- 
2.17.1



[PATCH v11 18/56] Input: atmel_mxt_ts: Rename mxt_hw_version_show to hw_version_show

2020-05-07 Thread Jiada Wang
Rename mxt_hw_version_show to hw_version_show to address checkpatch warning

Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index b2a37a9597f3..cec823de4096 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -3114,8 +3114,8 @@ static ssize_t fw_version_show(struct device *dev,
 }
 
 /* Hardware Version is returned as FamilyID.VariantID */
-static ssize_t mxt_hw_version_show(struct device *dev,
-  struct device_attribute *attr, char *buf)
+static ssize_t hw_version_show(struct device *dev,
+  struct device_attribute *attr, char *buf)
 {
struct mxt_data *data = dev_get_drvdata(dev);
struct mxt_info *info = data->info;
@@ -3404,7 +3404,7 @@ static const struct attribute_group mxt_fw_attr_group = {
 };
 
 static DEVICE_ATTR_RO(fw_version);
-static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
+static DEVICE_ATTR_RO(hw_version);
 static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
 
 static struct attribute *mxt_attrs[] = {
-- 
2.17.1



[PATCH v11 16/56] Input: atmel_mxt_ts - allow specification of firmware file name

2020-05-07 Thread Jiada Wang
From: Nick Dyer 

On platforms which have multiple device instances using this driver, the
firmware may be different on each device. This patch makes the user give
the name of the firmware file when flashing.

This also prevents accidental triggering of the firmware load process.

Signed-off-by: Nick Dyer 
Acked-by: Benson Leung 
Acked-by: Yufeng Shen 
(cherry picked from ndyer/linux/for-upstream commit 
76ebb7cee971cb42dfb0a3a9224403b8b09abcf1)
[gdavis: Forward port and fix conflicts.]
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 43 
 1 file changed, 37 insertions(+), 6 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index f8783e37436f..0e30ff372a43 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -33,8 +33,7 @@
 #include 
 #include 
 
-/* Firmware files */
-#define MXT_FW_NAME"maxtouch.fw"
+/* Configuration file */
 #define MXT_CFG_NAME   "maxtouch.cfg"
 #define MXT_CFG_MAGIC  "OBP_RAW V1"
 
@@ -335,6 +334,7 @@ struct mxt_data {
bool use_retrigen_workaround;
struct regulator *reg_vdd;
struct regulator *reg_avdd;
+   char *fw_name;
 
/* Cached parameters from object table */
u16 T5_address;
@@ -3207,7 +3207,7 @@ static int mxt_check_firmware_format(struct device *dev,
return -EINVAL;
 }
 
-static int mxt_load_fw(struct device *dev, const char *fn)
+static int mxt_load_fw(struct device *dev)
 {
struct mxt_data *data = dev_get_drvdata(dev);
const struct firmware *fw = NULL;
@@ -3217,9 +3217,9 @@ static int mxt_load_fw(struct device *dev, const char *fn)
unsigned int frame = 0;
int ret;
 
-   ret = request_firmware(, fn, dev);
+   ret = request_firmware(, data->fw_name, dev);
if (ret) {
-   dev_err(dev, "Unable to open firmware %s\n", fn);
+   dev_err(dev, "Unable to open firmware %s\n", data->fw_name);
return ret;
}
 
@@ -3339,6 +3339,33 @@ static int mxt_load_fw(struct device *dev, const char 
*fn)
return ret;
 }
 
+static int mxt_update_file_name(struct device *dev, char **file_name,
+   const char *buf, size_t count)
+{
+   char *file_name_tmp;
+
+   /* Simple sanity check */
+   if (count > 64) {
+   dev_warn(dev, "File name too long\n");
+   return -EINVAL;
+   }
+
+   file_name_tmp = krealloc(*file_name, count + 1, GFP_KERNEL);
+   if (!file_name_tmp)
+   return -ENOMEM;
+
+   *file_name = file_name_tmp;
+   memcpy(*file_name, buf, count);
+
+   /* Echo into the sysfs entry may append newline at the end of buf */
+   if (buf[count - 1] == '\n')
+   (*file_name)[count - 1] = '\0';
+   else
+   (*file_name)[count] = '\0';
+
+   return 0;
+}
+
 static ssize_t mxt_update_fw_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
@@ -3346,7 +3373,11 @@ static ssize_t mxt_update_fw_store(struct device *dev,
struct mxt_data *data = dev_get_drvdata(dev);
int error;
 
-   error = mxt_load_fw(dev, MXT_FW_NAME);
+   error = mxt_update_file_name(dev, >fw_name, buf, count);
+   if (error)
+   return error;
+
+   error = mxt_load_fw(dev);
if (error) {
dev_err(dev, "The firmware update failed(%d)\n", error);
count = error;
-- 
2.17.1



[PATCH v11 19/56] Input: atmel_mxt_ts: rename mxt_update_fw_store to update_fw_store

2020-05-07 Thread Jiada Wang
Rename mxt_update_fw_store to update_fw_store, to address checkpatch
warning.

Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index cec823de4096..720574417219 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -3366,9 +3366,9 @@ static int mxt_update_file_name(struct device *dev, char 
**file_name,
return 0;
 }
 
-static ssize_t mxt_update_fw_store(struct device *dev,
-   struct device_attribute *attr,
-   const char *buf, size_t count)
+static ssize_t update_fw_store(struct device *dev,
+  struct device_attribute *attr,
+  const char *buf, size_t count)
 {
struct mxt_data *data = dev_get_drvdata(dev);
int error;
@@ -3392,7 +3392,7 @@ static ssize_t mxt_update_fw_store(struct device *dev,
return count;
 }
 
-static DEVICE_ATTR(update_fw, 0200, NULL, mxt_update_fw_store);
+static DEVICE_ATTR_WO(update_fw);
 
 static struct attribute *mxt_fw_attrs[] = {
_attr_update_fw.attr,
-- 
2.17.1



[PATCH v11 17/56] Input: atmel_mxt_ts: Rename mxt_fw_version_show to fw_version_show

2020-05-07 Thread Jiada Wang
Rename mxt_fw_version_show to fw_version_show to address checkpatch warning

Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 0e30ff372a43..b2a37a9597f3 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -3104,8 +3104,8 @@ static int mxt_configure_objects(struct mxt_data *data,
 }
 
 /* Firmware Version is returned as Major.Minor.Build */
-static ssize_t mxt_fw_version_show(struct device *dev,
-  struct device_attribute *attr, char *buf)
+static ssize_t fw_version_show(struct device *dev,
+  struct device_attribute *attr, char *buf)
 {
struct mxt_data *data = dev_get_drvdata(dev);
struct mxt_info *info = data->info;
@@ -3403,7 +3403,7 @@ static const struct attribute_group mxt_fw_attr_group = {
.attrs = mxt_fw_attrs,
 };
 
-static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL);
+static DEVICE_ATTR_RO(fw_version);
 static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
 static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
 
-- 
2.17.1



[PATCH v11 15/56] Input: atmel_mxt_ts - report failures in suspend/resume

2020-05-07 Thread Jiada Wang
From: Nick Dyer 

This patch reports failures in suspend/resume

Signed-off-by: Nick Dyer 
(cherry picked from ndyer/linux/for-upstream commit 93a57575403d)
[gdavis: Resolve forward port conflicts due to applying upstream
 commit 96a938aa214e ("Input: atmel_mxt_ts - remove platform
 data support").]
Signed-off-by: George G. Davis 
[jiada: Fix compilation warning
Add commit description]
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 51 ++--
 1 file changed, 39 insertions(+), 12 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index ef8baf64659e..f8783e37436f 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -3425,10 +3425,12 @@ static void mxt_reset_slots(struct mxt_data *data)
mxt_input_sync(data);
 }
 
-static void mxt_start(struct mxt_data *data)
+static int mxt_start(struct mxt_data *data)
 {
+   int ret = 0;
+
if (!data->suspended || data->in_bootloader)
-   return;
+   return 0;
 
switch (data->suspend_mode) {
case MXT_SUSPEND_T9_CTRL:
@@ -3453,28 +3455,42 @@ static void mxt_start(struct mxt_data *data)
 */
mxt_process_messages_until_invalid(data);
 
-   mxt_set_t7_power_cfg(data, MXT_POWER_CFG_RUN);
+   ret = mxt_set_t7_power_cfg(data, MXT_POWER_CFG_RUN);
+   if (ret)
+   return ret;
 
/* Recalibrate since chip has been in deep sleep */
-   mxt_t6_command(data, MXT_COMMAND_CALIBRATE, 1, false);
+   ret = mxt_t6_command(data, MXT_COMMAND_CALIBRATE, 1, false);
+   if (ret)
+   return ret;
+
+   ret = mxt_acquire_irq(data);
+   if (ret)
+   return ret;
 
-   mxt_acquire_irq(data);
break;
}
 
data->suspended = false;
+
+   return 0;
 }
 
-static void mxt_stop(struct mxt_data *data)
+static int mxt_stop(struct mxt_data *data)
 {
+   int ret;
+
if (data->suspended || data->in_bootloader)
-   return;
+   return 0;
 
switch (data->suspend_mode) {
case MXT_SUSPEND_T9_CTRL:
/* Touch disable */
-   mxt_write_object(data,
+   ret = mxt_write_object(data,
MXT_TOUCH_MULTI_T9, MXT_T9_CTRL, 0);
+   if (ret)
+   return ret;
+
break;
 
case MXT_SUSPEND_REGULATOR:
@@ -3487,29 +3503,40 @@ static void mxt_stop(struct mxt_data *data)
default:
disable_irq(data->irq);
 
-   mxt_set_t7_power_cfg(data, MXT_POWER_CFG_DEEPSLEEP);
+   ret = mxt_set_t7_power_cfg(data, MXT_POWER_CFG_DEEPSLEEP);
+   if (ret)
+   return ret;
 
mxt_reset_slots(data);
break;
}
 
data->suspended = true;
+   return 0;
 }
 
 static int mxt_input_open(struct input_dev *dev)
 {
struct mxt_data *data = input_get_drvdata(dev);
+   int ret;
 
-   mxt_start(data);
+   ret = mxt_start(data);
 
-   return 0;
+   if (ret)
+   dev_err(>client->dev, "%s failed rc=%d\n", __func__, ret);
+
+   return ret;
 }
 
 static void mxt_input_close(struct input_dev *dev)
 {
struct mxt_data *data = input_get_drvdata(dev);
+   int ret;
 
-   mxt_stop(data);
+   ret = mxt_stop(data);
+
+   if (ret)
+   dev_err(>client->dev, "%s failed rc=%d\n", __func__, ret);
 }
 
 static int mxt_parse_device_properties(struct mxt_data *data)
-- 
2.17.1



[PATCH v11 14/56] Input: atmel_mxt_ts - add regulator control support

2020-05-07 Thread Jiada Wang
From: Nick Dyer 

Allow the driver to optionally manage enabling/disable power to the touch
controller itself. If the regulators are not present then use the deep
sleep power mode instead.

For a correct power on sequence, it is required that we have control over
the RESET line.

Signed-off-by: Nick Dyer 
Acked-by: Benson Leung 
Acked-by: Yufeng Shen 
(cherry picked from ndyer/linux/for-upstream commit 
14052b61bb66c2f2283c00e733e131be7a9b8bfc)
[gdavis: Resolve forward port conflicts due to v4.14-rc1 commmit
 f657b00df22e ("Input: atmel_mxt_ts - add support for reset
 line") and applying upstream commit 96a938aa214e ("Input:
 atmel_mxt_ts - remove platform data support").]
Signed-off-by: George G. Davis 
[gdavis: Squash fixes from Dirk Behme:
 - Input: atmel_mxt_ts - in failure case disable the regulator
 - Input: atmel_mxt_ts - disable only enabled regulators
 - Input: atmel_mxt_ts - use devm_regulator_get()]
Signed-off-by: Dirk Behme 
[jiada: Replace white-spaces with tab for MXT_CHG_DELAY
separate Documentation/ and include/dt-bindings/ portion change to 
another commit]
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 130 +--
 1 file changed, 121 insertions(+), 9 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 9aafed92db9c..ef8baf64659e 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -26,10 +26,12 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
 #include 
+#include 
 
 /* Firmware files */
 #define MXT_FW_NAME"maxtouch.fw"
@@ -215,6 +217,9 @@ enum t100_type {
 #define MXT_CRC_TIMEOUT1000/* msec */
 #define MXT_FW_RESET_TIME  3000/* msec */
 #define MXT_FW_CHG_TIMEOUT 300 /* msec */
+#define MXT_REGULATOR_DELAY150 /* msec */
+#define MXT_CHG_DELAY  100 /* msec */
+#define MXT_POWERON_DELAY  150 /* msec */
 
 /* Command to unlock bootloader */
 #define MXT_UNLOCK_CMD_MSB 0xaa
@@ -275,11 +280,6 @@ enum v4l_dbg_inputs {
MXT_V4L_INPUT_MAX,
 };
 
-enum mxt_suspend_mode {
-   MXT_SUSPEND_DEEP_SLEEP  = 0,
-   MXT_SUSPEND_T9_CTRL = 1,
-};
-
 /* Config update context */
 struct mxt_cfg {
u8 *raw;
@@ -333,6 +333,8 @@ struct mxt_data {
u8 stylus_aux_pressure;
u8 stylus_aux_peak;
bool use_retrigen_workaround;
+   struct regulator *reg_vdd;
+   struct regulator *reg_avdd;
 
/* Cached parameters from object table */
u16 T5_address;
@@ -2073,6 +2075,94 @@ static int mxt_read_info_block(struct mxt_data *data)
return error;
 }
 
+static void mxt_regulator_enable(struct mxt_data *data)
+{
+   int error;
+
+   if (!data->reg_vdd || !data->reg_avdd)
+   return;
+
+   gpiod_set_value(data->reset_gpio, 0);
+
+   error = regulator_enable(data->reg_vdd);
+   if (error)
+   return;
+
+   error = regulator_enable(data->reg_avdd);
+   if (error) {
+   regulator_disable(data->reg_vdd);
+   return;
+   }
+
+   /*
+* According to maXTouch power sequencing specification, RESET line
+* must be kept low until some time after regulators come up to
+* voltage
+*/
+   msleep(MXT_REGULATOR_DELAY);
+   gpiod_set_value(data->reset_gpio, 1);
+   msleep(MXT_CHG_DELAY);
+
+retry_wait:
+   reinit_completion(>bl_completion);
+   data->in_bootloader = true;
+   error = mxt_wait_for_completion(data, >bl_completion,
+   MXT_POWERON_DELAY);
+   if (error == -EINTR)
+   goto retry_wait;
+
+   data->in_bootloader = false;
+}
+
+static void mxt_regulator_disable(struct mxt_data *data)
+{
+   if (!data->reg_vdd || !data->reg_avdd)
+   return;
+
+   if (regulator_is_enabled(data->reg_vdd))
+   regulator_disable(data->reg_vdd);
+   if (regulator_is_enabled(data->reg_avdd))
+   regulator_disable(data->reg_avdd);
+}
+
+static int mxt_probe_regulators(struct mxt_data *data)
+{
+   struct device *dev = >client->dev;
+   int error;
+
+   /* Must have reset GPIO to use regulator support */
+   if (!data->reset_gpio) {
+   error = -EINVAL;
+   goto fail;
+   }
+
+   data->reg_vdd = devm_regulator_get(dev, "vdd");
+   if (IS_ERR(data->reg_vdd)) {
+   error = PTR_ERR(data->reg_vdd);
+   dev_err(dev, "Error %d getting vdd regulator\n", error);
+   goto fail;
+   }
+
+   data->reg_avdd = devm_regulator_get(dev, "avdd");
+   if (IS_ERR(data->reg_avdd)) {
+   error

[PATCH v11 11/56] Input: atmel_mxt_ts - add debug for T92 gesture and T93 touch seq msgs

2020-05-07 Thread Jiada Wang
From: Karl Tsou 

output T92 gesture and T93 touch sequence messages.

Signed-off-by: Karl Tsou 
Signed-off-by: Nick Dyer 
(cherry picked from ndyer/linux/for-upstream commit 
cb98986f8342107bf4a536aed4160b20839e97c1)
Signed-off-by: George G. Davis 
Reported-by: kbuild test robot 
[jiada: changed dev_debug() to dev_info()]
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 40 
 1 file changed, 40 insertions(+)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 63db8b66eb67..6126bb8a7acc 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -71,6 +71,8 @@
 #define MXT_SPT_MESSAGECOUNT_T44   44
 #define MXT_SPT_CTECONFIG_T46  46
 #define MXT_SPT_DYNAMICCONFIGURATIONCONTAINER_T71 71
+#define MXT_PROCI_SYMBOLGESTUREPROCESSOR   92
+#define MXT_PROCI_TOUCHSEQUENCELOGGER  93
 #define MXT_TOUCH_MULTITOUCHSCREEN_T100 100
 #define MXT_PROCI_ACTIVESTYLUS_T107107
 
@@ -349,6 +351,10 @@ struct mxt_data {
u8 T42_reportid_max;
u16 T44_address;
u8 T48_reportid;
+   u16 T92_address;
+   u8 T92_reportid;
+   u16 T93_address;
+   u8 T93_reportid;
u8 T100_reportid_min;
u8 T100_reportid_max;
u16 T107_address;
@@ -1113,6 +1119,24 @@ static int mxt_proc_t48_messages(struct mxt_data *data, 
u8 *msg)
return 0;
 }
 
+static void mxt_proc_t92_messages(struct mxt_data *data, u8 *msg)
+{
+   struct device *dev = >client->dev;
+   u8 status = msg[1];
+
+   dev_info(dev, "T92 long stroke LSTR=%d %d\n",
+(status & 0x80) ? 1 : 0,
+status & 0x0F);
+}
+
+static void mxt_proc_t93_messages(struct mxt_data *data, u8 *msg)
+{
+   struct device *dev = >client->dev;
+   u8 status = msg[1];
+
+   dev_info(dev, "T93 report double tap %d\n", status);
+}
+
 static int mxt_proc_message(struct mxt_data *data, u8 *message)
 {
u8 report_id = message[0];
@@ -1145,6 +1169,10 @@ static int mxt_proc_message(struct mxt_data *data, u8 
*message)
} else if (report_id >= data->T15_reportid_min
   && report_id <= data->T15_reportid_max) {
mxt_proc_t15_messages(data, message);
+   } else if (report_id == data->T92_reportid) {
+   mxt_proc_t92_messages(data, message);
+   } else if (report_id == data->T93_reportid) {
+   mxt_proc_t93_messages(data, message);
} else {
mxt_dump_message(data, message);
}
@@ -1814,6 +1842,10 @@ static void mxt_free_object_table(struct mxt_data *data)
data->T42_reportid_max = 0;
data->T44_address = 0;
data->T48_reportid = 0;
+   data->T92_reportid = 0;
+   data->T92_address = 0;
+   data->T93_reportid = 0;
+   data->T93_address = 0;
data->T100_reportid_min = 0;
data->T100_reportid_max = 0;
data->max_reportid = 0;
@@ -1906,6 +1938,14 @@ static int mxt_parse_object_table(struct mxt_data *data,
case MXT_PROCG_NOISESUPPRESSION_T48:
data->T48_reportid = min_id;
break;
+   case MXT_PROCI_SYMBOLGESTUREPROCESSOR:
+   data->T92_reportid = min_id;
+   data->T92_address = object->start_address;
+   break;
+   case MXT_PROCI_TOUCHSEQUENCELOGGER:
+   data->T93_reportid = min_id;
+   data->T93_address = object->start_address;
+   break;
case MXT_TOUCH_MULTITOUCHSCREEN_T100:
data->multitouch = MXT_TOUCH_MULTITOUCHSCREEN_T100;
data->T100_reportid_min = min_id;
-- 
2.17.1



[PATCH v11 10/56] Input: atmel_mxt_ts - implement support for T107 active stylus

2020-05-07 Thread Jiada Wang
From: Nick Dyer 

This patch implements support for T107 active stylus

Signed-off-by: Nick Dyer 
(cherry picked from ndyer/linux/for-upstream commit 
20e357dd9acf8c2040068c8b22d6bc1401a1893f)
[gdavis: Forward port and fix conflicts due to applying upstream commit
 96a938aa214e ("Input: atmel_mxt_ts - remove platform data
 support").]
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 117 ++-
 1 file changed, 113 insertions(+), 4 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index ba58cdd5b76d..63db8b66eb67 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -72,6 +72,7 @@
 #define MXT_SPT_CTECONFIG_T46  46
 #define MXT_SPT_DYNAMICCONFIGURATIONCONTAINER_T71 71
 #define MXT_TOUCH_MULTITOUCHSCREEN_T100 100
+#define MXT_PROCI_ACTIVESTYLUS_T107107
 
 /* MXT_GEN_MESSAGE_T5 object */
 #define MXT_RPTID_NOMSG0xff
@@ -181,6 +182,7 @@ struct t37_debug {
 enum t100_type {
MXT_T100_TYPE_FINGER= 1,
MXT_T100_TYPE_PASSIVE_STYLUS= 2,
+   MXT_T100_TYPE_ACTIVE_STYLUS = 3,
MXT_T100_TYPE_HOVERING_FINGER   = 4,
MXT_T100_TYPE_GLOVE = 5,
MXT_T100_TYPE_LARGE_TOUCH   = 6,
@@ -192,6 +194,16 @@ enum t100_type {
 #define MXT_TOUCH_MAJOR_DEFAULT1
 #define MXT_PRESSURE_DEFAULT   1
 
+/* Gen2 Active Stylus */
+#define MXT_T107_STYLUS_STYAUX 42
+#define MXT_T107_STYLUS_STYAUX_PRESSUREBIT(0)
+#define MXT_T107_STYLUS_STYAUX_PEAKBIT(4)
+
+#define MXT_T107_STYLUS_HOVER  BIT(0)
+#define MXT_T107_STYLUS_TIPSWITCH  BIT(1)
+#define MXT_T107_STYLUS_BUTTON0BIT(2)
+#define MXT_T107_STYLUS_BUTTON1BIT(3)
+
 /* Delay times */
 #define MXT_BACKUP_TIME50  /* msec */
 #define MXT_RESET_GPIO_TIME20  /* msec */
@@ -313,10 +325,12 @@ struct mxt_data {
struct t7_config t7_cfg;
struct mxt_dbg dbg;
struct gpio_desc *reset_gpio;
-   bool use_retrigen_workaround;
unsigned long t15_keystatus;
int t15_num_keys;
const unsigned int *t15_keymap;
+   u8 stylus_aux_pressure;
+   u8 stylus_aux_peak;
+   bool use_retrigen_workaround;
 
/* Cached parameters from object table */
u16 T5_address;
@@ -337,6 +351,7 @@ struct mxt_data {
u8 T48_reportid;
u8 T100_reportid_min;
u8 T100_reportid_max;
+   u16 T107_address;
 
/* for fw update in bootloader */
struct completion bl_completion;
@@ -908,6 +923,8 @@ static void mxt_proc_t100_message(struct mxt_data *data, u8 
*message)
u8 major = 0;
u8 pressure = 0;
u8 orientation = 0;
+   bool active = false;
+   bool hover = false;
 
id = message[0] - data->T100_reportid_min - 2;
 
@@ -926,6 +943,8 @@ static void mxt_proc_t100_message(struct mxt_data *data, u8 
*message)
case MXT_T100_TYPE_HOVERING_FINGER:
tool = MT_TOOL_FINGER;
distance = MXT_DISTANCE_HOVERING;
+   hover = true;
+   active = true;
 
if (data->t100_aux_vect)
orientation = message[data->t100_aux_vect];
@@ -936,6 +955,8 @@ static void mxt_proc_t100_message(struct mxt_data *data, u8 
*message)
case MXT_T100_TYPE_GLOVE:
tool = MT_TOOL_FINGER;
distance = MXT_DISTANCE_ACTIVE_TOUCH;
+   hover = false;
+   active = true;
 
if (data->t100_aux_area)
major = message[data->t100_aux_area];
@@ -950,6 +971,9 @@ static void mxt_proc_t100_message(struct mxt_data *data, u8 
*message)
 
case MXT_T100_TYPE_PASSIVE_STYLUS:
tool = MT_TOOL_PEN;
+   distance = MXT_DISTANCE_ACTIVE_TOUCH;
+   hover = false;
+   active = true;
 
/*
 * Passive stylus is reported with size zero so
@@ -962,6 +986,31 @@ static void mxt_proc_t100_message(struct mxt_data *data, 
u8 *message)
 
break;
 
+   case MXT_T100_TYPE_ACTIVE_STYLUS:
+   /* Report input buttons */
+   input_report_key(input_dev, BTN_STYLUS,
+message[6] & MXT_T107_STYLUS_BUTTON0);
+   input_report_key(input_dev, BTN_STYLUS2,
+message[6] & MXT_T107_STYLUS_BUTTON1);
+
+   /* stylus in range, but position unavailable */
+   if (!(m

[PATCH v11 00/56] atmel_mxt_ts misc

2020-05-07 Thread Jiada Wang
This patch-set forward ports Nick Dyer's work in ndyer/linux github
repository as long as some other features and fixes

Balasubramani Vivekanandan (2):
  Input: atmel_mxt_ts: Limit the max bytes transferred in an i2c
transaction
  Input: atmel_mxt_ts: use gpiod_set_value_cansleep for reset pin

Dean Jenkins (1):
  Input: atmel_mxt_ts: return error from
mxt_process_messages_until_invalid()

Deepak Das (6):
  Input: Atmel: improve error handling in mxt_start()
  Input: Atmel: improve error handling in mxt_initialize()
  Input: Atmel: improve error handling in mxt_update_cfg()
  Input: Atmel: Improve error handling in mxt_initialize_input_device()
  Input: Atmel: handle ReportID "0x00" while processing T5 messages
  Input: Atmel: use T44 object to process T5 messages

George G. Davis (1):
  input: atmel_mxt_ts: export GPIO reset line via sysfs

Janus Cheng (1):
  Input: atmel_mxt_ts - check data->input_dev is not null in
mxt_input_sync()

Jiada Wang (12):
  Input: introduce input_mt_report_slot_inactive
  dt-bindings: input: atmel: add suspend mode support
  Input: atmel_mxt_ts: Rename mxt_fw_version_show to fw_version_show
  Input: atmel_mxt_ts: Rename mxt_hw_version_show to hw_version_show
  Input: atmel_mxt_ts: rename mxt_update_fw_store to update_fw_store
  dt-bindings: input: atmel: provide name of configuration file
  dt-bindings: input: atmel: support to specify input name
  Input: atmel_mxt_ts - rename mxt_object_show to object_show
  Input: atmel_mxt_ts - delay enabling IRQ when not using regulators
  Input: atmel_mxt_ts - eliminate data->raw_info_block
  input: atmel_mxt_ts: don't disable IRQ before remove of
mxt_fw_attr_group
  Input: atmel_mxt_ts - Fix compilation warning

Karl Tsou (1):
  Input: atmel_mxt_ts - add debug for T92 gesture and T93 touch seq msgs

Kautuk Consul (2):
  Input: atmel_mxt_ts - Change call-points of mxt_free_* functions
  Input: atmel_mxt_ts - rely on calculated_crc rather than file
config_crc

Naveen Chakka (2):
  input: touchscreen: atmel_mxt_ts: Added sysfs entry for touchscreen
status
  input: atmel_mxt_ts: added sysfs interface to update atmel T38 data

Nick Dyer (25):
  Input: atmel_mxt_ts - rework sysfs init/remove
  Input: atmel_mxt_ts - only read messages in mxt_acquire_irq() when
necessary
  Input: atmel_mxt_ts - split large i2c transfers into blocks
  Input: atmel_mxt_ts - output status from T48 Noise Supression
  Input: atmel_mxt_ts - output status from T42 Touch Suppression
  Input: atmel_mxt_ts - implement T9 vector/orientation support
  Input: atmel_mxt_ts - implement T15 Key Array support
  Input: atmel_mxt_ts - handle reports from T47 Stylus object
  Input: atmel_mxt_ts - implement support for T107 active stylus
  Input: atmel_mxt_ts - release touch state during suspend
  Input: atmel_mxt_ts - add regulator control support
  Input: atmel_mxt_ts - report failures in suspend/resume
  Input: atmel_mxt_ts - allow specification of firmware file name
  Input: atmel_mxt_ts - handle cfg filename via pdata/sysfs
  Input: atmel_mxt_ts - allow input name to be specified in platform
data
  Input: atmel_mxt_ts - refactor firmware flash to extract context into
struct
  Input: atmel_mxt_ts - refactor code to enter bootloader into separate
func
  Input: atmel_mxt_ts - combine bootloader version query with probe
  Input: atmel_mxt_ts - improve bootloader state machine handling
  Input: atmel_mxt_ts - rename bl_completion to chg_completion
  Input: atmel_mxt_ts - make bootloader interrupt driven
  Input: atmel_mxt_ts - implement I2C retries
  Input: atmel_mxt_ts - orientation is not present in hover
  Input: atmel_mxt_ts - implement debug output for messages
  Input: atmel_mxt_ts - implement improved debug message interface

Nikhil Ravindran (1):
  Input: atmel_mxt_ts: Add support for run self-test routine.

karl tsou (1):
  Input: atmel_mxt_ts - add config checksum attribute to sysfs

keerthikumarp (1):
  input: atmel_mxt_ts: Add Missing Delay for reset handling of Atmel
touch panel controller in detachable displays.
---
v11:
Following commits in v10 have been dropped
dt-bindings: input: atmel: support to set max bytes transferred
Input: atmel_mxt_ts: Implement synchronization during various operation

Following commits have been added
Input: atmel_mxt_ts - check data->input_dev is not null in
mxt_input_sync()
Input: atmel_mxt_ts - rename mxt_object_show to object_show
input: atmel_mxt_ts: don't disable IRQ before remove of
mxt_fw_attr_group

Following commits have been updated to address review findings
dt-bindings: input: atmel: add suspend mode support
input: touchscreen: atmel_mxt_ts: Added sysfs entry for touchscreen
status
Input: atmel_mxt_ts - handle cfg filename via pdata/sysfs
Input: atmel_mxt_ts - delay enabling IRQ when not using regulators

v10:
Following commits have been updated
input: touchscreen: atmel_mxt_ts: Added sysfs entry for touchscreen
status
dt-bindings: input: atmel: add suspend

[PATCH v11 02/56] Input: atmel_mxt_ts - rework sysfs init/remove

2020-05-07 Thread Jiada Wang
From: Nick Dyer 

An error in the sysfs init may otherwise interfere with the async return
from the firmware loader

Signed-off-by: Nick Dyer 
(cherry picked from ndyer/linux/for-upstream commit 
3114584ae77c2b03b6dad87174f010d002e9c05d)
[gdavis: Forward port and fixup conflicts. Also fixed sysfs leaks in
 both the mxt_initialize() and mxt_probe() error return cases.]
Signed-off-by: George G. Davis 
[jiada: keep call mxt_initialize() before sysfs creation
replace S_IWUSR with 0200]
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 64 +++-
 1 file changed, 52 insertions(+), 12 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index a2189739e30f..49bdf5cf3a0d 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -2086,10 +2086,14 @@ static int mxt_initialize_input_device(struct mxt_data 
*data)
return 0;
 
 err_free_mem:
+   data->input_dev = NULL;
input_free_device(input_dev);
return error;
 }
 
+static int mxt_sysfs_init(struct mxt_data *data);
+static void mxt_sysfs_remove(struct mxt_data *data);
+
 static int mxt_configure_objects(struct mxt_data *data,
 const struct firmware *cfg);
 
@@ -2141,16 +2145,24 @@ static int mxt_initialize(struct mxt_data *data)
if (error)
return error;
 
+   error = mxt_sysfs_init(data);
+   if (error)
+   return error;
+
error = request_firmware_nowait(THIS_MODULE, true, MXT_CFG_NAME,
>dev, GFP_KERNEL, data,
mxt_config_cb);
if (error) {
dev_err(>dev, "Failed to invoke firmware loader: %d\n",
error);
-   return error;
+   goto err_free_sysfs;
}
 
return 0;
+
+err_free_sysfs:
+   mxt_sysfs_remove(data);
+   return error;
 }
 
 static int mxt_set_t7_power_cfg(struct mxt_data *data, u8 sleep)
@@ -2803,6 +2815,7 @@ static int mxt_load_fw(struct device *dev, const char *fn)
if (ret)
goto release_firmware;
 
+   mxt_sysfs_remove(data);
mxt_free_input_device(data);
mxt_free_object_table(data);
} else {
@@ -2909,16 +2922,25 @@ static ssize_t mxt_update_fw_store(struct device *dev,
return count;
 }
 
+static DEVICE_ATTR(update_fw, 0200, NULL, mxt_update_fw_store);
+
+static struct attribute *mxt_fw_attrs[] = {
+   _attr_update_fw.attr,
+   NULL
+};
+
+static const struct attribute_group mxt_fw_attr_group = {
+   .attrs = mxt_fw_attrs,
+};
+
 static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL);
 static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
 static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
-static DEVICE_ATTR(update_fw, S_IWUSR, NULL, mxt_update_fw_store);
 
 static struct attribute *mxt_attrs[] = {
_attr_fw_version.attr,
_attr_hw_version.attr,
_attr_object.attr,
-   _attr_update_fw.attr,
NULL
 };
 
@@ -2926,6 +2948,28 @@ static const struct attribute_group mxt_attr_group = {
.attrs = mxt_attrs,
 };
 
+static int mxt_sysfs_init(struct mxt_data *data)
+{
+   struct i2c_client *client = data->client;
+   int error;
+
+   error = sysfs_create_group(>dev.kobj, _attr_group);
+   if (error) {
+   dev_err(>dev, "Failure %d creating sysfs group\n",
+   error);
+   return error;
+   }
+
+   return 0;
+}
+
+static void mxt_sysfs_remove(struct mxt_data *data)
+{
+   struct i2c_client *client = data->client;
+
+   sysfs_remove_group(>dev.kobj, _attr_group);
+}
+
 static void mxt_start(struct mxt_data *data)
 {
switch (data->suspend_mode) {
@@ -3112,19 +3156,14 @@ static int mxt_probe(struct i2c_client *client, const 
struct i2c_device_id *id)
if (error)
return error;
 
-   error = sysfs_create_group(>dev.kobj, _attr_group);
+   error = sysfs_create_group(>dev.kobj, _fw_attr_group);
if (error) {
-   dev_err(>dev, "Failure %d creating sysfs group\n",
+   dev_err(>dev, "Failure %d creating fw sysfs group\n",
error);
-   goto err_free_object;
+   return error;
}
 
return 0;
-
-err_free_object:
-   mxt_free_input_device(data);
-   mxt_free_object_table(data);
-   return error;
 }
 
 static int mxt_remove(struct i2c_client *client)
@@ -3132,7 +3171,8 @@ static int mxt_remove(struct i2c_client *client)
struct mxt_data *data = i2c_get_clientdata(client);
 
disable_irq(data->irq);
-   sysfs_remove_group(>dev.kobj, _attr_group);

[PATCH v11 13/56] dt-bindings: input: atmel: add suspend mode support

2020-05-07 Thread Jiada Wang
Add suspend mode support for atmel touchscreen driver

Signed-off-by: Jiada Wang 
---
 .../bindings/input/atmel,maxtouch.txt |  9 
 MAINTAINERS   |  1 +
 include/dt-bindings/input/atmel_mxt_ts.h  | 23 +++
 3 files changed, 33 insertions(+)
 create mode 100644 include/dt-bindings/input/atmel_mxt_ts.h

diff --git a/Documentation/devicetree/bindings/input/atmel,maxtouch.txt 
b/Documentation/devicetree/bindings/input/atmel,maxtouch.txt
index c88919480d37..530312fc7a99 100644
--- a/Documentation/devicetree/bindings/input/atmel,maxtouch.txt
+++ b/Documentation/devicetree/bindings/input/atmel,maxtouch.txt
@@ -31,6 +31,15 @@ Optional properties for main touchpad device:
 
 - reset-gpios: GPIO specifier for the touchscreen's reset pin (active low)
 
+- atmel,suspend-mode: Select method used to suspend:
+MXT_SUSPEND_DEEP_SLEEP - use T7 to suspend the device into deep sleep
+MXT_SUSPEND_T9_CTRL - use T9.CTRL to turn off touch processing
+MXT_SUSPEND_REGULATOR - use regulators to power down device during suspend
+Definitions are in .
+
+- vdd: vdd: phandle to Power supply regulator
+- avdd: phandle to Analog Power supply regulator
+
 Example:
 
touch@4b {
diff --git a/MAINTAINERS b/MAINTAINERS
index e48ab79879ac..350ae664e6f0 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2873,6 +2873,7 @@ T:git git://github.com/ndyer/linux.git
 S: Maintained
 F: Documentation/devicetree/bindings/input/atmel,maxtouch.txt
 F: drivers/input/touchscreen/atmel_mxt_ts.c
+F: include/dt-bindings/input/atmel_mxt_ts.h
 
 ATMEL WIRELESS DRIVER
 M: Simon Kelley 
diff --git a/include/dt-bindings/input/atmel_mxt_ts.h 
b/include/dt-bindings/input/atmel_mxt_ts.h
new file mode 100644
index ..41ed0f8111aa
--- /dev/null
+++ b/include/dt-bindings/input/atmel_mxt_ts.h
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Atmel maXTouch Touchscreen driver
+ *
+ * Copyright (C) 2015 Atmel Corporation
+ * Author: Nick Dyer 
+ *
+ * This program is free software; you can redistribute  it and/or modify it
+ * under  the terms of  the GNU General  Public License as published by the
+ * Free Software Foundation;  either version 2 of the  License, or (at your
+ * option) any later version.
+ */
+
+#ifndef __DT_BINDINGS_ATMEL_MXT_TS_H
+#define __DT_BINDINGS_ATMEL_MXT_TS_H
+
+enum mxt_suspend_mode {
+   MXT_SUSPEND_DEEP_SLEEP = 0,
+   MXT_SUSPEND_T9_CTRL = 1,
+   MXT_SUSPEND_REGULATOR = 2,
+};
+
+#endif /* __DT_BINDINGS_ATMEL_MXT_TS_H */
-- 
2.17.1



[PATCH v11 06/56] Input: atmel_mxt_ts - output status from T42 Touch Suppression

2020-05-07 Thread Jiada Wang
From: Nick Dyer 

This patch outputs status from T42 touch suppression

Signed-off-by: Nick Dyer 
Acked-by: Benson Leung 
Acked-by: Yufeng Shen 
(cherry picked from ndyer/linux/for-upstream commit 
ab95b5a30d2c098daaa9f88d9fcfae7eb516)
Signed-off-by: George G. Davis 
[jiada: Replace dev_info() with dev_dbg()]
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 25 
 1 file changed, 25 insertions(+)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index a53985a7736f..f6465edaa57e 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -155,6 +155,9 @@ struct t37_debug {
 #define MXT_RESET_VALUE0x01
 #define MXT_BACKUP_VALUE   0x55
 
+/* Define for MXT_PROCI_TOUCHSUPPRESSION_T42 */
+#define MXT_T42_MSG_TCHSUP BIT(0)
+
 /* T100 Multiple Touch Touchscreen */
 #define MXT_T100_CTRL  0
 #define MXT_T100_CFG1  1
@@ -323,6 +326,8 @@ struct mxt_data {
u8 T9_reportid_max;
u16 T18_address;
u8 T19_reportid;
+   u8 T42_reportid_min;
+   u8 T42_reportid_max;
u16 T44_address;
u8 T48_reportid;
u8 T100_reportid_min;
@@ -979,6 +984,17 @@ static void mxt_proc_t100_message(struct mxt_data *data, 
u8 *message)
data->update_input = true;
 }
 
+static void mxt_proc_t42_messages(struct mxt_data *data, u8 *msg)
+{
+   struct device *dev = >client->dev;
+   u8 status = msg[1];
+
+   if (status & MXT_T42_MSG_TCHSUP)
+   dev_dbg(dev, "T42 suppress\n");
+   else
+   dev_dbg(dev, "T42 normal\n");
+}
+
 static int mxt_proc_t48_messages(struct mxt_data *data, u8 *msg)
 {
struct device *dev = >client->dev;
@@ -1006,6 +1022,9 @@ static int mxt_proc_message(struct mxt_data *data, u8 
*message)
 
if (report_id == data->T6_reportid) {
mxt_proc_t6_messages(data, message);
+   } else if (report_id >= data->T42_reportid_min
+  && report_id <= data->T42_reportid_max) {
+   mxt_proc_t42_messages(data, message);
} else if (report_id == data->T48_reportid) {
mxt_proc_t48_messages(data, message);
} else if (!data->input_dev) {
@@ -1686,6 +1705,8 @@ static void mxt_free_object_table(struct mxt_data *data)
data->T9_reportid_max = 0;
data->T18_address = 0;
data->T19_reportid = 0;
+   data->T42_reportid_min = 0;
+   data->T42_reportid_max = 0;
data->T44_address = 0;
data->T48_reportid = 0;
data->T100_reportid_min = 0;
@@ -1763,6 +1784,10 @@ static int mxt_parse_object_table(struct mxt_data *data,
case MXT_SPT_COMMSCONFIG_T18:
data->T18_address = object->start_address;
break;
+   case MXT_PROCI_TOUCHSUPPRESSION_T42:
+   data->T42_reportid_min = min_id;
+   data->T42_reportid_max = max_id;
+   break;
case MXT_SPT_MESSAGECOUNT_T44:
data->T44_address = object->start_address;
break;
-- 
2.17.1



[PATCH v11 01/56] Input: introduce input_mt_report_slot_inactive

2020-05-07 Thread Jiada Wang
input_mt_report_slot_state() ignores the tool when the slot is closed.
which has caused a bit of confusion.
This patch introduces input_mt_report_slot_inactive() to report slot
inactive state.
replaces all input_mt_report_slot_state() with
input_mt_report_slot_inactive() in case of close of slot.

Suggested-by: Dmitry Torokhov 
Reported-by: kernel test robot 
Signed-off-by: Jiada Wang 
---
 drivers/hid/hid-alps.c | 3 +--
 drivers/hid/hid-multitouch.c   | 6 ++
 drivers/input/misc/xen-kbdfront.c  | 2 +-
 drivers/input/mouse/elan_i2c_core.c| 2 +-
 drivers/input/touchscreen/atmel_mxt_ts.c   | 7 +++
 drivers/input/touchscreen/cyttsp4_core.c   | 5 ++---
 drivers/input/touchscreen/cyttsp_core.c| 2 +-
 drivers/input/touchscreen/melfas_mip4.c| 4 ++--
 drivers/input/touchscreen/mms114.c | 2 +-
 drivers/input/touchscreen/raspberrypi-ts.c | 2 +-
 drivers/input/touchscreen/stmfts.c | 2 +-
 include/linux/input/mt.h   | 5 +
 12 files changed, 21 insertions(+), 21 deletions(-)

diff --git a/drivers/hid/hid-alps.c b/drivers/hid/hid-alps.c
index ae79a7c66737..36ca1d815d53 100644
--- a/drivers/hid/hid-alps.c
+++ b/drivers/hid/hid-alps.c
@@ -387,8 +387,7 @@ static int u1_raw_event(struct alps_dev *hdata, u8 *data, 
int size)
input_report_abs(hdata->input,
ABS_MT_PRESSURE, z);
} else {
-   input_mt_report_slot_state(hdata->input,
-   MT_TOOL_FINGER, 0);
+   input_mt_report_slot_inactive(hdata->input);
}
}
 
diff --git a/drivers/hid/hid-multitouch.c b/drivers/hid/hid-multitouch.c
index 362805ddf377..e2ce790ff4a4 100644
--- a/drivers/hid/hid-multitouch.c
+++ b/drivers/hid/hid-multitouch.c
@@ -896,7 +896,7 @@ static void mt_release_pending_palms(struct mt_device *td,
clear_bit(slotnum, app->pending_palm_slots);
 
input_mt_slot(input, slotnum);
-   input_mt_report_slot_state(input, MT_TOOL_PALM, false);
+   input_mt_report_slot_inactive(input);
 
need_sync = true;
}
@@ -1640,9 +1640,7 @@ static void mt_release_contacts(struct hid_device *hid)
if (mt) {
for (i = 0; i < mt->num_slots; i++) {
input_mt_slot(input_dev, i);
-   input_mt_report_slot_state(input_dev,
-  MT_TOOL_FINGER,
-  false);
+   input_mt_report_slot_inactive(input_dev);
}
input_mt_sync_frame(input_dev);
input_sync(input_dev);
diff --git a/drivers/input/misc/xen-kbdfront.c 
b/drivers/input/misc/xen-kbdfront.c
index 24bc5c5d876f..a1bba722b234 100644
--- a/drivers/input/misc/xen-kbdfront.c
+++ b/drivers/input/misc/xen-kbdfront.c
@@ -146,7 +146,7 @@ static void xenkbd_handle_mt_event(struct xenkbd_info *info,
break;
 
case XENKBD_MT_EV_UP:
-   input_mt_report_slot_state(info->mtouch, MT_TOOL_FINGER, false);
+   input_mt_report_slot_inactive(info->mtouch);
break;
 
case XENKBD_MT_EV_SYN:
diff --git a/drivers/input/mouse/elan_i2c_core.c 
b/drivers/input/mouse/elan_i2c_core.c
index 8719da540383..3f9354baac4b 100644
--- a/drivers/input/mouse/elan_i2c_core.c
+++ b/drivers/input/mouse/elan_i2c_core.c
@@ -938,7 +938,7 @@ static void elan_report_contact(struct elan_tp_data *data,
input_report_abs(input, ABS_MT_TOUCH_MINOR, minor);
} else {
input_mt_slot(input, contact_num);
-   input_mt_report_slot_state(input, MT_TOOL_FINGER, false);
+   input_mt_report_slot_inactive(input);
}
 }
 
diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index ae60442efda0..a2189739e30f 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -822,8 +822,7 @@ static void mxt_proc_t9_message(struct mxt_data *data, u8 
*message)
 * have happened.
 */
if (status & MXT_T9_RELEASE) {
-   input_mt_report_slot_state(input_dev,
-  MT_TOOL_FINGER, 0);
+   input_mt_report_slot_inactive(input_dev);
mxt_input_sync(data);
}
 
@@ -839,7 +838,7 @@ static void mxt_proc_t9_message(struct mxt_data *data, u8 
*message)
input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area);
} else {
/* Touch no l

[PATCH v11 05/56] Input: atmel_mxt_ts - output status from T48 Noise Supression

2020-05-07 Thread Jiada Wang
From: Nick Dyer 

This patch outputs status from T48 Noise Supression

Signed-off-by: Nick Dyer 
Acked-by: Benson Leung 
Acked-by: Yufeng Shen 
(cherry picked from ndyer/linux/for-upstream commit 
2895a6ff150a49f27a02938f8d262be238b296d8)
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 25 
 1 file changed, 25 insertions(+)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 7e6a66e3e1e0..a53985a7736f 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -324,6 +324,7 @@ struct mxt_data {
u16 T18_address;
u8 T19_reportid;
u16 T44_address;
+   u8 T48_reportid;
u8 T100_reportid_min;
u8 T100_reportid_max;
 
@@ -978,6 +979,24 @@ static void mxt_proc_t100_message(struct mxt_data *data, 
u8 *message)
data->update_input = true;
 }
 
+static int mxt_proc_t48_messages(struct mxt_data *data, u8 *msg)
+{
+   struct device *dev = >client->dev;
+   u8 status, state;
+
+   status = msg[1];
+   state  = msg[4];
+
+   dev_dbg(dev, "T48 state %d status %02X %s%s%s%s%s\n", state, status,
+   status & 0x01 ? "FREQCHG " : "",
+   status & 0x02 ? "APXCHG " : "",
+   status & 0x04 ? "ALGOERR " : "",
+   status & 0x10 ? "STATCHG " : "",
+   status & 0x20 ? "NLVLCHG " : "");
+
+   return 0;
+}
+
 static int mxt_proc_message(struct mxt_data *data, u8 *message)
 {
u8 report_id = message[0];
@@ -987,6 +1006,8 @@ static int mxt_proc_message(struct mxt_data *data, u8 
*message)
 
if (report_id == data->T6_reportid) {
mxt_proc_t6_messages(data, message);
+   } else if (report_id == data->T48_reportid) {
+   mxt_proc_t48_messages(data, message);
} else if (!data->input_dev) {
/*
 * Do not report events if input device
@@ -1666,6 +1687,7 @@ static void mxt_free_object_table(struct mxt_data *data)
data->T18_address = 0;
data->T19_reportid = 0;
data->T44_address = 0;
+   data->T48_reportid = 0;
data->T100_reportid_min = 0;
data->T100_reportid_max = 0;
data->max_reportid = 0;
@@ -1747,6 +1769,9 @@ static int mxt_parse_object_table(struct mxt_data *data,
case MXT_SPT_GPIOPWM_T19:
data->T19_reportid = min_id;
break;
+   case MXT_PROCG_NOISESUPPRESSION_T48:
+   data->T48_reportid = min_id;
+   break;
case MXT_TOUCH_MULTITOUCHSCREEN_T100:
data->multitouch = MXT_TOUCH_MULTITOUCHSCREEN_T100;
data->T100_reportid_min = min_id;
-- 
2.17.1



[PATCH v11 12/56] Input: atmel_mxt_ts - release touch state during suspend

2020-05-07 Thread Jiada Wang
From: Nick Dyer 

If fingers are down as the MXT chip goes into suspend it does not send a
lift message. In addition, it may not complete its final measurement cycle
immediately, which means touch messages may be received by the interrupt
handler after mxt_stop() has completed.

So:
- disable irq during suspend
- flush any messages created after suspend
- tell app layer that slots were released at suspend

Signed-off-by: Nick Dyer 
Acked-by: Benson Leung 
Acked-by: Yufeng Shen 
(cherry picked from ndyer/linux/for-upstream commit 
26794433086dbc7dea18d2f6a1c8d61ab25bcfda)
[gdavis: Resolve forward port conflicts due to applying upstream
 commit 96a938aa214e ("Input: atmel_mxt_ts - remove platform
 data support").]
Signed-off-by: George G. Davis 
[gdavis: Squash fix from Dirk Behme:
 - Input: atmel_mxt_ts - remove superfluous data->suspended]
Signed-off-by: Dirk Behme 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 52 ++--
 1 file changed, 49 insertions(+), 3 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 6126bb8a7acc..9aafed92db9c 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -372,6 +372,9 @@ struct mxt_data {
unsigned int t19_num_keys;
 
enum mxt_suspend_mode suspend_mode;
+
+   /* Indicates whether device is in suspend */
+   bool suspended;
 };
 
 struct mxt_vb2_buffer {
@@ -1151,10 +1154,10 @@ static int mxt_proc_message(struct mxt_data *data, u8 
*message)
mxt_proc_t42_messages(data, message);
} else if (report_id == data->T48_reportid) {
mxt_proc_t48_messages(data, message);
-   } else if (!data->input_dev) {
+   } else if (!data->input_dev || data->suspended) {
/*
-* Do not report events if input device
-* is not yet registered.
+* Do not report events if input device is not
+* yet registered or returning from suspend
 */
mxt_dump_message(data, message);
} else if (report_id >= data->T9_reportid_min &&
@@ -3135,6 +3138,11 @@ static int mxt_load_fw(struct device *dev, const char 
*fn)
if (ret)
goto release_firmware;
 
+   if (data->suspended) {
+   enable_irq(data->irq);
+   data->suspended = false;
+   }
+
if (!data->in_bootloader) {
/* Change to the bootloader mode */
data->in_bootloader = true;
@@ -3306,8 +3314,27 @@ static void mxt_sysfs_remove(struct mxt_data *data)
sysfs_remove_group(>dev.kobj, _attr_group);
 }
 
+static void mxt_reset_slots(struct mxt_data *data)
+{
+   struct input_dev *input_dev = data->input_dev;
+   int id;
+
+   if (!input_dev)
+   return;
+
+   for (id = 0; id < data->num_touchids; id++) {
+   input_mt_slot(input_dev, id);
+   input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 0);
+   }
+
+   mxt_input_sync(data);
+}
+
 static void mxt_start(struct mxt_data *data)
 {
+   if (!data->suspended || data->in_bootloader)
+   return;
+
switch (data->suspend_mode) {
case MXT_SUSPEND_T9_CTRL:
mxt_soft_reset(data);
@@ -3320,16 +3347,29 @@ static void mxt_start(struct mxt_data *data)
 
case MXT_SUSPEND_DEEP_SLEEP:
default:
+   /*
+* Discard any touch messages still in message buffer
+* from before chip went to sleep
+*/
+   mxt_process_messages_until_invalid(data);
+
mxt_set_t7_power_cfg(data, MXT_POWER_CFG_RUN);
 
/* Recalibrate since chip has been in deep sleep */
mxt_t6_command(data, MXT_COMMAND_CALIBRATE, 1, false);
+
+   mxt_acquire_irq(data);
break;
}
+
+   data->suspended = false;
 }
 
 static void mxt_stop(struct mxt_data *data)
 {
+   if (data->suspended || data->in_bootloader)
+   return;
+
switch (data->suspend_mode) {
case MXT_SUSPEND_T9_CTRL:
/* Touch disable */
@@ -3339,9 +3379,15 @@ static void mxt_stop(struct mxt_data *data)
 
case MXT_SUSPEND_DEEP_SLEEP:
default:
+   disable_irq(data->irq);
+
mxt_set_t7_power_cfg(data, MXT_POWER_CFG_DEEPSLEEP);
+
+   mxt_reset_slots(data);
break;
}
+
+   data->suspended = true;
 }
 
 static int mxt_input_open(struct input_dev *dev)
-- 
2.17.1



[PATCH v11 09/56] Input: atmel_mxt_ts - handle reports from T47 Stylus object

2020-05-07 Thread Jiada Wang
From: Nick Dyer 

This patch handles reports from T47 Stylus object

Signed-off-by: Nick Dyer 
Acked-by: Benson Leung 
Acked-by: Yufeng Shen 
(cherry picked from ndyer/linux/for-upstream commit 
56405a5ea08eb34cfe83f3121867c9de0a5c48c1)
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 12 +++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index d05249b02781..ba58cdd5b76d 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -823,6 +823,7 @@ static void mxt_proc_t9_message(struct mxt_data *data, u8 
*message)
int area;
int amplitude;
u8 vector;
+   int tool;
 
id = message[0] - data->T9_reportid_min;
status = message[1];
@@ -836,6 +837,7 @@ static void mxt_proc_t9_message(struct mxt_data *data, u8 
*message)
y >>= 2;
 
area = message[5];
+
amplitude = message[6];
vector = message[7];
 
@@ -865,12 +867,20 @@ static void mxt_proc_t9_message(struct mxt_data *data, u8 
*message)
mxt_input_sync(data);
}
 
+   /* A size of zero indicates touch is from a linked T47 Stylus */
+   if (area == 0) {
+   area = MXT_TOUCH_MAJOR_DEFAULT;
+   tool = MT_TOOL_PEN;
+   } else {
+   tool = MT_TOOL_FINGER;
+   }
+
/* if active, pressure must be non-zero */
if (!amplitude)
amplitude = MXT_PRESSURE_DEFAULT;
 
/* Touch active */
-   input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, 1);
+   input_mt_report_slot_state(input_dev, tool, 1);
input_report_abs(input_dev, ABS_MT_POSITION_X, x);
input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
input_report_abs(input_dev, ABS_MT_PRESSURE, amplitude);
-- 
2.17.1



[PATCH v11 07/56] Input: atmel_mxt_ts - implement T9 vector/orientation support

2020-05-07 Thread Jiada Wang
From: Nick Dyer 

The atmel touch messages contain orientation information as a byte in a
packed format which can be passed straight on to Android if the input
device configuration is correct.

This requires vector reports to be enabled in maXTouch config (zero
DISVECT bit 3 in T9 CTRL field)

Android converts the format in InputReader.cpp, search for
ORIENTATION_CALIBRATION_VECTOR.

Signed-off-by: Nick Dyer 
Acked-by: Benson Leung 
Acked-by: Yufeng Shen 
(cherry picked from ndyer/linux/for-upstream commit 
a6f0ee919d2631678169b23fb18f55b6dbabcd4c)
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 12 
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index f6465edaa57e..df2e0ba76e63 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -817,6 +817,7 @@ static void mxt_proc_t9_message(struct mxt_data *data, u8 
*message)
int y;
int area;
int amplitude;
+   u8 vector;
 
id = message[0] - data->T9_reportid_min;
status = message[1];
@@ -831,9 +832,10 @@ static void mxt_proc_t9_message(struct mxt_data *data, u8 
*message)
 
area = message[5];
amplitude = message[6];
+   vector = message[7];
 
dev_dbg(dev,
-   "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n",
+   "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u vector: 
%02X\n",
id,
(status & MXT_T9_DETECT) ? 'D' : '.',
(status & MXT_T9_PRESS) ? 'P' : '.',
@@ -843,7 +845,7 @@ static void mxt_proc_t9_message(struct mxt_data *data, u8 
*message)
(status & MXT_T9_AMP) ? 'A' : '.',
(status & MXT_T9_SUPPRESS) ? 'S' : '.',
(status & MXT_T9_UNGRIP) ? 'U' : '.',
-   x, y, area, amplitude);
+   x, y, area, amplitude, vector);
 
input_mt_slot(input_dev, id);
 
@@ -868,6 +870,7 @@ static void mxt_proc_t9_message(struct mxt_data *data, u8 
*message)
input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
input_report_abs(input_dev, ABS_MT_PRESSURE, amplitude);
input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR, area);
+   input_report_abs(input_dev, ABS_MT_ORIENTATION, vector);
} else {
/* Touch no longer active, close out slot */
input_mt_report_slot_inactive(input_dev);
@@ -2180,8 +2183,9 @@ static int mxt_initialize_input_device(struct mxt_data 
*data)
 0, 255, 0, 0);
}
 
-   if (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
-   data->t100_aux_vect) {
+   if (data->multitouch == MXT_TOUCH_MULTI_T9 ||
+   (data->multitouch == MXT_TOUCH_MULTITOUCHSCREEN_T100 &&
+   data->t100_aux_vect)) {
input_set_abs_params(input_dev, ABS_MT_ORIENTATION,
 0, 255, 0, 0);
}
-- 
2.17.1



[PATCH v11 08/56] Input: atmel_mxt_ts - implement T15 Key Array support

2020-05-07 Thread Jiada Wang
From: Nick Dyer 

There is a key array object in many maXTouch chips which allows some X/Y
lines to be used as a key array. This patch maps them to a series of keys
which may be configured in a platform data array.

Signed-off-by: Nick Dyer 
Acked-by: Benson Leung 
Acked-by: Yufeng Shen 
(cherry picked from ndyer/linux/for-upstream commit 
15bb074b5abf3a101f7b79544213f1c110ea4cab)
[gdavis: Resolve forward port conflicts due to applying upstream
 commit 96a938aa214e ("Input: atmel_mxt_ts - remove platform
 data support").]
Signed-off-by: George G. Davis 
[jiada: Fix compilation warning]
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 85 
 1 file changed, 85 insertions(+)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index df2e0ba76e63..d05249b02781 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -314,6 +314,9 @@ struct mxt_data {
struct mxt_dbg dbg;
struct gpio_desc *reset_gpio;
bool use_retrigen_workaround;
+   unsigned long t15_keystatus;
+   int t15_num_keys;
+   const unsigned int *t15_keymap;
 
/* Cached parameters from object table */
u16 T5_address;
@@ -324,6 +327,8 @@ struct mxt_data {
u16 T71_address;
u8 T9_reportid_min;
u8 T9_reportid_max;
+   u8 T15_reportid_min;
+   u8 T15_reportid_max;
u16 T18_address;
u8 T19_reportid;
u8 T42_reportid_min;
@@ -987,6 +992,38 @@ static void mxt_proc_t100_message(struct mxt_data *data, 
u8 *message)
data->update_input = true;
 }
 
+static void mxt_proc_t15_messages(struct mxt_data *data, u8 *msg)
+{
+   struct input_dev *input_dev = data->input_dev;
+   struct device *dev = >client->dev;
+   int key;
+   bool curr_state, new_state;
+   bool sync = false;
+   unsigned long keystates = le32_to_cpu((__force __le32)msg[2]);
+
+   for (key = 0; key < data->t15_num_keys; key++) {
+   curr_state = test_bit(key, >t15_keystatus);
+   new_state = test_bit(key, );
+
+   if (!curr_state && new_state) {
+   dev_dbg(dev, "T15 key press: %u\n", key);
+   __set_bit(key, >t15_keystatus);
+   input_event(input_dev, EV_KEY,
+   data->t15_keymap[key], 1);
+   sync = true;
+   } else if (curr_state && !new_state) {
+   dev_dbg(dev, "T15 key release: %u\n", key);
+   __clear_bit(key, >t15_keystatus);
+   input_event(input_dev, EV_KEY,
+   data->t15_keymap[key], 0);
+   sync = true;
+   }
+   }
+
+   if (sync)
+   input_sync(input_dev);
+}
+
 static void mxt_proc_t42_messages(struct mxt_data *data, u8 *msg)
 {
struct device *dev = >client->dev;
@@ -1045,6 +1082,9 @@ static int mxt_proc_message(struct mxt_data *data, u8 
*message)
} else if (report_id == data->T19_reportid) {
mxt_input_button(data, message);
data->update_input = true;
+   } else if (report_id >= data->T15_reportid_min
+  && report_id <= data->T15_reportid_max) {
+   mxt_proc_t15_messages(data, message);
} else {
mxt_dump_message(data, message);
}
@@ -1706,6 +1746,8 @@ static void mxt_free_object_table(struct mxt_data *data)
data->T71_address = 0;
data->T9_reportid_min = 0;
data->T9_reportid_max = 0;
+   data->T15_reportid_min = 0;
+   data->T15_reportid_max = 0;
data->T18_address = 0;
data->T19_reportid = 0;
data->T42_reportid_min = 0;
@@ -1784,6 +1826,10 @@ static int mxt_parse_object_table(struct mxt_data *data,
object->num_report_ids - 1;
data->num_touchids = object->num_report_ids;
break;
+   case MXT_TOUCH_KEYARRAY_T15:
+   data->T15_reportid_min = min_id;
+   data->T15_reportid_max = max_id;
+   break;
case MXT_SPT_COMMSCONFIG_T18:
data->T18_address = object->start_address;
break;
@@ -2077,6 +2123,7 @@ static int mxt_initialize_input_device(struct mxt_data 
*data)
int error;
unsigned int num_mt_slots;
unsigned int mt_flags = 0;
+   int i;
 
switch (data->multitouch) {
case MXT_TOUCH_MULTI_T9:
@@ -2190,6 +2237,15 @@ static int mxt_initialize_input_device(struct mxt_data 
*data)
  

Re: [PATCH v3 02/49] Input: introduce input_mt_report_slot_inactive

2019-09-24 Thread Jiada Wang

Hi Henrik


On 2019/09/18 3:25, Henrik Rydberg wrote:

Hi Jiada,


input_mt_report_slot_state() ignores the tool when the slot is closed.
which has caused a bit of confusion.
This patch introduces input_mt_report_slot_inactive() to report slot
inactive state.
replaces all input_mt_report_slot_state() with
input_mt_report_slot_inactive() in case of close of slot.


This patch looks very odd, I am afraid.

When a driver needs to use input_mt functions, it first calls 
input_mt_init_slots() during setup. The MT state then remains in effect 
until the driver is destroyed. Thus, there is no valid case when 
input_mt_report_slot_state() would fail to execute the line


    input_event(dev, EV_ABS, ABS_MT_TRACKING_ID, -1)

when active == false.

What input_mt_report_slot_state() does do, however, is to ignore the 
event when no MT state has been set, which does happen for some drivers 
handling both normal and MT devices. Changing such a driver in the way 
you suggest would introduce new events in existing, working cases, and 
possibly break userspace. We should try very hard to avoid it.




thanks for your comment,

Just to make sure, I think your comment is for
patch "[PATCH v3 01/49] Input: switch to use return value of 
input_mt_report_slot_state"
not for "[PATCH v3 02/49] Input: introduce 
input_mt_report_slot_inactive", right?


yes, I agree
by having change:

-input_mt_report_slot_state(dev, tool_type, active);
-if (active) {
+if (input_mt_report_slot_state(dev, tool_type, active)){
 ... ...
 }

the logic of the driver is changed, when (mt == NULL && active == true).
I will drop patch "Input: switch to use return value of" in next version

Thanks,
Jiada


Thanks,

Henrik




[PATCH v3 49/49] Input: atmel_mxt_ts - Fix compilation warning

2019-09-17 Thread Jiada Wang
fix "make W=1" compilation warnings from Atmel driver
as per the compilation logs.

Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index ff6d3ed58604..511ad2bb6b07 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -2049,7 +2049,7 @@ static int mxt_prepare_cfg_mem(struct mxt_data *data, 
struct mxt_cfg *cfg)
 
byte_offset = reg + i - cfg->start_ofs;
 
-   if (byte_offset >= 0 && byte_offset < cfg->mem_size) {
+   if (byte_offset < cfg->mem_size) {
*(cfg->mem + byte_offset) = val;
} else {
dev_err(dev, "Bad object: reg:%d, T%d, 
ofs=%d\n",
-- 
2.19.2



[PATCH v3 43/49] Input: Atmel: handle ReportID "0x00" while processing T5 messages

2019-09-17 Thread Jiada Wang
From: Deepak Das 

ReportID "0x00" is reserved by Atmel and should not be used by any
Atmel touch controller.

reportID is the first byte retrieved from T5 message payload.
Currently Atmel driver continues to process the T5 messages even if
the reportID "0x00" is returned by Touch Controller.

This commit modifies Atmel touch driver to return -EINVAL if ReportID
"0x00" is received while processing T5 messages.

Signed-off-by: Deepak Das 
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 8 
 1 file changed, 8 insertions(+)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index a13d6938de1e..93bb19cad7e1 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -76,6 +76,7 @@
 #define MXT_PROCI_TOUCHSEQUENCELOGGER  93
 #define MXT_TOUCH_MULTITOUCHSCREEN_T100 100
 #define MXT_PROCI_ACTIVESTYLUS_T107107
+#define MXT_RPTID_RESERVED 0
 
 /* MXT_GEN_MESSAGE_T5 object */
 #define MXT_RPTID_NOMSG0xff
@@ -1384,6 +1385,11 @@ static int mxt_proc_message(struct mxt_data *data, u8 
*message)
u8 report_id = message[0];
bool dump = data->debug_enabled;
 
+   if (report_id == MXT_RPTID_RESERVED) {
+   dev_err(>client->dev,
+   "Received Reserved ReportID 0x00\n");
+   return -EINVAL;
+   }
if (report_id == MXT_RPTID_NOMSG)
return 0;
 
@@ -1454,6 +1460,8 @@ static int mxt_read_and_process_messages(struct mxt_data 
*data, u8 count)
ret = mxt_proc_message(data,
data->msg_buf + data->T5_msg_size * i);
 
+   if (ret < 0)
+   return ret;
if (ret == 1)
num_valid++;
}
-- 
2.19.2



[PATCH v3 40/49] Input: Atmel: improve error handling in mxt_initialize()

2019-09-17 Thread Jiada Wang
From: Deepak Das 

Currently mxt_initialize() tries to probe bootloader mode
even if valid bootloader address is not specified.

This commit modifies mxt_initialize() to return error
if Device is not in appmode and bootloader address is
not specified.

This commit also returns error code from mxt_send_bootloader_cmd()
in mxt_initialize().

Signed-off-by: Deepak Das 
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 58 +---
 1 file changed, 41 insertions(+), 17 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 2fac43da0a2b..b260ac155b5e 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -716,17 +716,13 @@ static int mxt_lookup_bootloader_address(struct mxt_data 
*data, bool retry)
return 0;
 }
 
-static int mxt_probe_bootloader(struct mxt_data *data, bool alt_address)
+static int mxt_probe_bootloader(struct mxt_data *data)
 {
struct device *dev = >client->dev;
int error;
u8 buf[3];
bool crc_failure, extended_id;
 
-   error = mxt_lookup_bootloader_address(data, alt_address);
-   if (error)
-   return error;
-
/* Check bootloader status and version information */
error = mxt_bootloader_read(data, buf, sizeof(buf));
if (error)
@@ -2923,6 +2919,32 @@ static void mxt_config_cb(const struct firmware *cfg, 
void *ctx)
release_firmware(cfg);
 }
 
+static int mxt_bootloader_status(struct mxt_data *data)
+{
+   struct i2c_client *client = data->client;
+   int error;
+
+   error = mxt_lookup_bootloader_address(data, false);
+   if (error) {
+   dev_info(>dev,
+"Bootloader address is not specified\n");
+   return error;
+   }
+   /* Check bootloader state */
+   error = mxt_probe_bootloader(data);
+   if (error) {
+   dev_info(>dev, "Trying alternate bootloader address\n");
+   mxt_lookup_bootloader_address(data, true);
+   error = mxt_probe_bootloader(data);
+   if (error) {
+   dev_err(>dev,
+   "Chip is not in appmode or bootloader mode\n");
+   return error;
+   }
+   }
+   return 0;
+}
+
 static int mxt_initialize(struct mxt_data *data)
 {
struct i2c_client *client = data->client;
@@ -2934,16 +2956,13 @@ static int mxt_initialize(struct mxt_data *data)
if (!error)
break;
 
-   /* Check bootloader state */
-   error = mxt_probe_bootloader(data, false);
-   if (error) {
-   dev_info(>dev, "Trying alternate bootloader 
address\n");
-   error = mxt_probe_bootloader(data, true);
-   if (error) {
-   /* Chip is not in appmode or bootloader mode */
-   return error;
-   }
-   }
+   dev_info(>dev,
+"info block read failed (%d), so try bootloader 
method\n",
+error);
+
+   error = mxt_bootloader_status(data);
+   if (error)
+   return error;
 
/* OK, we are in bootloader, see if we can recover */
if (++recovery_attempts > 1) {
@@ -2957,7 +2976,9 @@ static int mxt_initialize(struct mxt_data *data)
}
 
/* Attempt to exit bootloader into app mode */
-   mxt_send_bootloader_cmd(data, false);
+   error = mxt_send_bootloader_cmd(data, false);
+   if (error)
+   return error;
msleep(MXT_FW_RESET_TIME);
}
 
@@ -3649,8 +3670,11 @@ static int mxt_enter_bootloader(struct mxt_data *data)
 
msleep(MXT_RESET_TIME);
 
+   ret = mxt_lookup_bootloader_address(data, false);
+   if (ret)
+   return ret;
/* Do not need to scan since we know family ID */
-   ret = mxt_probe_bootloader(data, 0);
+   ret = mxt_probe_bootloader(data);
if (ret)
return ret;
 
-- 
2.19.2



[PATCH v3 42/49] Input: Atmel: Improve error handling in mxt_initialize_input_device()

2019-09-17 Thread Jiada Wang
From: Deepak Das 

Currently Driver probe continues with a warning message when it fails to
get the proper multitouch object configurations like TouchScreen resolution.
But Driver probe should fail in case of above scneario because it will not 
behave
as expected without the proper touchscreen configurations.

This commit modifies mxt_initialize_input_device() to return error when it fails
to get the proper touch screen configurations.

Signed-off-by: Deepak Das 
Signed-off-by: Dean Jenkins 
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 12 
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 6198149438c3..a13d6938de1e 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -2762,15 +2762,19 @@ static int mxt_initialize_input_device(struct mxt_data 
*data)
case MXT_TOUCH_MULTI_T9:
num_mt_slots = data->T9_reportid_max - data->T9_reportid_min + 
1;
error = mxt_read_t9_resolution(data);
-   if (error)
-   dev_warn(dev, "Failed to initialize T9 resolution\n");
+   if (error) {
+   dev_err(dev, "Failed to initialize T9 resolution\n");
+   return error;
+   }
break;
 
case MXT_TOUCH_MULTITOUCHSCREEN_T100:
num_mt_slots = data->num_touchids;
error = mxt_read_t100_config(data);
-   if (error)
-   dev_warn(dev, "Failed to read T100 config\n");
+   if (error) {
+   dev_err(dev, "Failed to read T100 config\n");
+   return error;
+   }
break;
 
default:
-- 
2.19.2



[PATCH v3 41/49] Input: Atmel: improve error handling in mxt_update_cfg()

2019-09-17 Thread Jiada Wang
From: Deepak Das 

mxt_update_cfg() failed to propagate the error
code from mxt_init_t7_power_cfg() so return the error code.

Signed-off-by: Deepak Das 
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index b260ac155b5e..6198149438c3 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -2186,7 +2186,9 @@ static int mxt_update_cfg(struct mxt_data *data, const 
struct firmware *fw)
dev_info(dev, "Config successfully updated\n");
 
/* T7 config may have changed */
-   mxt_init_t7_power_cfg(data);
+   ret = mxt_init_t7_power_cfg(data);
+   if (ret)
+   dev_warn(dev, "Power Config failed to update\n");
 
 release_mem:
kfree(cfg.mem);
-- 
2.19.2



[PATCH v3 39/49] Input: Atmel: improve error handling in mxt_start()

2019-09-17 Thread Jiada Wang
From: Deepak Das 

mxt_start() does not return error in any of
the failure cases which will allow input_dev->open()
to return success even in case of any failure.

This commit modifies mxt_start() to return error
in failure cases.

Signed-off-by: Deepak Das 
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 31 
 1 file changed, 16 insertions(+), 15 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index f2fa62289577..2fac43da0a2b 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -3974,12 +3974,13 @@ static int mxt_start(struct mxt_data *data)
 
switch (data->suspend_mode) {
case MXT_SUSPEND_T9_CTRL:
-   mxt_soft_reset(data);
-
+   ret = mxt_soft_reset(data);
+   if (ret)
+   break;
/* Touch enable */
/* 0x83 = SCANEN | RPTEN | ENABLE */
-   mxt_write_object(data,
-   MXT_TOUCH_MULTI_T9, MXT_T9_CTRL, 0x83);
+   ret = mxt_write_object(data,
+  MXT_TOUCH_MULTI_T9, MXT_T9_CTRL, 0x83);
break;
 
case MXT_SUSPEND_REGULATOR:
@@ -3993,27 +3994,26 @@ static int mxt_start(struct mxt_data *data)
 * Discard any touch messages still in message buffer
 * from before chip went to sleep
 */
-   mxt_process_messages_until_invalid(data);
+   ret = mxt_process_messages_until_invalid(data);
+   if (ret)
+   break;
 
ret = mxt_set_t7_power_cfg(data, MXT_POWER_CFG_RUN);
if (ret)
-   return ret;
+   break;
 
/* Recalibrate since chip has been in deep sleep */
ret = mxt_t6_command(data, MXT_COMMAND_CALIBRATE, 1, false);
if (ret)
-   return ret;
+   break;
 
ret = mxt_acquire_irq(data);
-   if (ret)
-   return ret;
-
-   break;
}
 
-   data->suspended = false;
+   if (!ret)
+   data->suspended = false;
 
-   return 0;
+   return ret;
 }
 
 static int mxt_stop(struct mxt_data *data)
@@ -4334,6 +4334,7 @@ static int __maybe_unused mxt_resume(struct device *dev)
struct i2c_client *client = to_i2c_client(dev);
struct mxt_data *data = i2c_get_clientdata(client);
struct input_dev *input_dev = data->input_dev;
+   int ret = 0;
 
if (!input_dev)
return 0;
@@ -4341,11 +4342,11 @@ static int __maybe_unused mxt_resume(struct device *dev)
mutex_lock(_dev->mutex);
 
if (input_dev->users)
-   mxt_start(data);
+   ret = mxt_start(data);
 
mutex_unlock(_dev->mutex);
 
-   return 0;
+   return ret;
 }
 
 static SIMPLE_DEV_PM_OPS(mxt_pm_ops, mxt_suspend, mxt_resume);
-- 
2.19.2



[PATCH v3 47/49] input: atmel_mxt_ts: added sysfs interface to update atmel T38 data

2019-09-17 Thread Jiada Wang
From: Naveen Chakka 

Atmel touch controller contains T38 object where a user can store its own
data of length 64 bytes. T38 data will not be part of checksum
calculation on executing T6 BACKUP command.

format used to update the T38 data is given below:

  

offset: offset address of the data to be written in the t38 object
(in decimal)

length: length of the data to be written into the t38 object(in decimal)

data: actual data bytes to be written into the t38 object
  (values should be in hex)

Ex:
1. 0 2 10 20
updates first two bytes of the t38 data with values 10 and 20

2. 19 6 10 2f 30 4a 50 60
updates 6 bytes of t38 data from the index 19-24 with hex values

Signed-off-by: Naveen Chakka 
Signed-off-by: Sanjeev Chugh 
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 102 +++
 1 file changed, 102 insertions(+)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index e67c29f0e0ca..db4ad3b82650 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -4027,6 +4027,106 @@ static ssize_t mxt_touch_device_status(struct device 
*dev, struct
return ret;
 }
 
+static ssize_t mxt_t38_data_show(struct device *dev,
+struct device_attribute *attr, char *buf)
+{
+   struct mxt_data *data = dev_get_drvdata(dev);
+   struct mxt_object *object;
+   size_t count = 0, size;
+   u8 i, *t38_buf;
+
+   if (!data->object_table)
+   return -ENXIO;
+
+   object = mxt_get_object(data, MXT_SPT_USERDATA_T38);
+   size = mxt_obj_size(object);
+
+   /* Pre-allocate buffer large enough to hold max size of t38 object.*/
+   t38_buf = kmalloc(size, GFP_KERNEL);
+   if (!t38_buf)
+   return -ENOMEM;
+
+   count = __mxt_read_reg(data->client, object->start_address,
+  size, t38_buf);
+   if (count)
+   goto end;
+
+   for (i = 0; i < size; i++)
+   count += scnprintf(buf + count, PAGE_SIZE - count,
+  "[%2u]: %02x\n", i, t38_buf[i]);
+   count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
+end:
+   kfree(t38_buf);
+   return count;
+}
+
+static ssize_t mxt_t38_data_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+   struct mxt_data *data = dev_get_drvdata(dev);
+   struct mxt_object *object;
+   ssize_t ret = 0, pos, offset;
+   unsigned int i, len, index;
+   u8 *t38_buf;
+
+   if (!data->object_table)
+   return -ENXIO;
+
+   object = mxt_get_object(data, MXT_SPT_USERDATA_T38);
+
+   /* Pre-allocate buffer large enough to hold max size of t38 object.*/
+   t38_buf = kmalloc(mxt_obj_size(object), GFP_KERNEL);
+   if (!t38_buf)
+   return -ENOMEM;
+
+   ret = sscanf(buf, "%zd %d%zd", , , );
+   if (ret != 2) {
+   dev_err(dev, "Bad format: Invalid parameter to update t38\n");
+   ret = -EINVAL;
+   goto end;
+   }
+
+   if (len == 0) {
+   dev_err(dev,
+   "Bad format: Data length should not be equal to 0\n");
+   ret = -EINVAL;
+   goto end;
+   }
+
+   if (offset < 0 || ((offset + len) > 64)) {
+   dev_err(dev, "Invalid offset value to update t38\n");
+   ret = -EINVAL;
+   goto end;
+   }
+
+   index = pos;
+   for (i = 0; i < len; i++) {
+   ret = sscanf(buf + index, "%hhx%zd", t38_buf + i, );
+   if (ret != 1) {
+   dev_err(dev, "Bad format: Invalid Data\n");
+   ret = -EINVAL;
+   goto end;
+   }
+   index += pos;
+   }
+
+   ret = __mxt_write_reg(data->client, object->start_address + offset,
+ len, t38_buf);
+   if (ret)
+   goto end;
+
+   ret = mxt_t6_command(data, MXT_COMMAND_BACKUPNV, MXT_BACKUP_VALUE,
+true);
+   if (ret)
+   dev_err(dev, "backup command failed\n");
+   else
+   ret = count;
+end:
+   kfree(t38_buf);
+   return ret;
+}
+
 static DEVICE_ATTR(fw_version, S_IRUGO, mxt_fw_version_show, NULL);
 static DEVICE_ATTR(hw_version, S_IRUGO, mxt_hw_version_show, NULL);
 static DEVICE_ATTR(object, S_IRUGO, mxt_object_show, NULL);
@@ -4039,6 +4139,7 @@ static DEVICE_ATTR(debug_v2_enable, S_IWUSR | S_IRUSR, 
NULL,
 static DEVICE_ATTR(debug_notify, S_IRUGO, mxt_debug_notify_show, NULL);
 static DEVICE_ATTR(t25, 0600, mxt_t25_selftest_show, mxt_t25

[PATCH v3 45/49] Input: atmel_mxt_ts: use gpiod_set_value_cansleep for reset pin

2019-09-17 Thread Jiada Wang
From: Balasubramani Vivekanandan 

In case of remote display, touch controller will be also remote.
In such cases, the reset pin of the touch controller will be
controlled through bridging ICs like Deserilizer and Serializer.
Therefore accessing the gpio pins require transactions with the
external IC. Using the function gpiod_set_value will print a
warning like below

WARNING: CPU: 0 PID: 576 at drivers/gpio/gpiolib.c:1441 
gpiod_set_value+0x34/0x60()
CPU: 0 PID: 576 Comm: modprobe Not tainted 3.14.79-08377-g84ea22f-dirty #4
Backtrace:
[<80011c58>] (dump_backtrace) from [<80011e60>] (show_stack+0x18/0x1c)
[<80011e48>] (show_stack) from [<8052d7ac>] (dump_stack+0x7c/0x9c)
[<8052d730>] (dump_stack) from [<800241bc>] (warn_slowpath_common+0x74/0x9c)
[<80024148>] (warn_slowpath_common) from [<80024288>] 
(warn_slowpath_null+0x24/0x2c)
[<80024264>] (warn_slowpath_null) from [<8029e070>] (gpiod_set_value+0x34/0x60)
[<8029e03c>] (gpiod_set_value) from [<7f492e98>] (mxt_probe+0x1e0/0x718 
[atmel_mxt_ts])
[<7f492cb8>] (mxt_probe [atmel_mxt_ts]) from [<803c4d34>] 
(i2c_device_probe+0xcc/0xec)
[<803c4c68>] (i2c_device_probe) from [<803252a0>] 
(driver_probe_device+0xc0/0x200)

Signed-off-by: Balasubramani Vivekanandan 

Signed-off-by: Vladimir Zapolskiy 
Signed-off-by: Sanjeev Chugh 
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 76bda6137bf7..8444f7292e29 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -2490,7 +2490,7 @@ static void mxt_regulator_enable(struct mxt_data *data)
if (!data->reg_vdd || !data->reg_avdd)
return;
 
-   gpiod_set_value(data->reset_gpio, 0);
+   gpiod_set_value_cansleep(data->reset_gpio, 0);
 
error = regulator_enable(data->reg_vdd);
if (error)
@@ -2508,7 +2508,7 @@ static void mxt_regulator_enable(struct mxt_data *data)
 * voltage
 */
msleep(MXT_REGULATOR_DELAY);
-   gpiod_set_value(data->reset_gpio, 1);
+   gpiod_set_value_cansleep(data->reset_gpio, 1);
msleep(MXT_CHG_DELAY);
 
 retry_wait:
@@ -4314,7 +4314,7 @@ static int mxt_probe(struct i2c_client *client, const 
struct i2c_device_id *id)
disable_irq(data->irq);
} else if (data->reset_gpio) {
msleep(MXT_RESET_GPIO_TIME);
-   gpiod_set_value(data->reset_gpio, 1);
+   gpiod_set_value_cansleep(data->reset_gpio, 1);
msleep(MXT_RESET_INVALID_CHG);
} else {
dev_dbg(>dev,
-- 
2.19.2



[PATCH v3 46/49] input: touchscreen: atmel_mxt_ts: Added sysfs entry for touchscreen status

2019-09-17 Thread Jiada Wang
From: Naveen Chakka 

To know the current communication status of the touch controller during
runtime, sysfs interface is added

sysfs interface: /sys/class/i2c-dev/i2c-*/device/*/touch_dev_stat
Executing the above sysfs interface provides two output values

1)Status of the touch device
value 0 represents device is inactive
value 1 represents device is active
2)Error counter
value represents the number of times device in inactive since last read

Signed-off-by: Naveen Chakka 
Signed-off-by: Sanjeev Chugh 
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 109 +--
 1 file changed, 102 insertions(+), 7 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 8444f7292e29..e67c29f0e0ca 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -25,6 +25,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -222,6 +223,7 @@ enum t100_type {
 #define MXT_CHG_DELAY  100 /* msec */
 #define MXT_POWERON_DELAY  150 /* msec */
 #define MXT_BOOTLOADER_WAIT36E5/* 1 minute */
+#define MXT_WATCHDOG_TIMEOUT   1000/* msec */
 
 /* Command to unlock bootloader */
 #define MXT_UNLOCK_CMD_MSB 0xaa
@@ -317,6 +319,12 @@ struct mxt_flash {
struct delayed_work work;
 };
 
+struct mxt_statusinfo {
+   bool dev_status;
+   bool intp_triggered;
+   u32 error_count;
+};
+
 /* Each client has this additional data */
 struct mxt_data {
struct i2c_client *client;
@@ -372,6 +380,9 @@ struct mxt_data {
const char *pcfg_name;
const char *input_name;
struct mxt_flash *flash;
+   struct work_struct watchdog_work;
+   struct timer_list watchdog_timer;
+   struct mxt_statusinfo mxt_status;
 
/* Cached parameters from object table */
u16 T5_address;
@@ -1624,11 +1635,30 @@ static int mxt_process_messages(struct mxt_data *data)
return total_handled;
 }
 
+static void mxt_start_wd_timer(struct mxt_data *data)
+{
+   mod_timer(>watchdog_timer, jiffies +
+   msecs_to_jiffies(MXT_WATCHDOG_TIMEOUT));
+}
+
+static void mxt_stop_wd_timer(struct mxt_data *data)
+{
+   /*
+* Ensure we wait until the watchdog timer
+* running on a different CPU finishes
+*/
+   del_timer_sync(>watchdog_timer);
+   cancel_work_sync(>watchdog_work);
+   del_timer_sync(>watchdog_timer);
+}
+
 static irqreturn_t mxt_interrupt(int irq, void *dev_id)
 {
struct mxt_data *data = dev_id;
int ret;
 
+   data->mxt_status.intp_triggered = true;
+
if (data->in_bootloader) {
complete(>chg_completion);
 
@@ -1636,21 +1666,25 @@ static irqreturn_t mxt_interrupt(int irq, void *dev_id)
cancel_delayed_work_sync(>flash->work);
 
ret = mxt_check_bootloader(data);
-   return IRQ_RETVAL(ret);
+   ret = IRQ_RETVAL(ret);
+   goto exit;
}
 
-   if (!data->object_table)
-   return IRQ_HANDLED;
+   if (!data->object_table) {
+   ret = IRQ_HANDLED;
+   goto exit;
+   }
 
if (data->T44_address)
ret = mxt_process_messages_t44(data);
else
ret = mxt_process_messages(data);
 
-   if (ret <= 0)
-   return IRQ_NONE;
-   else
-   return IRQ_HANDLED;
+   ret = (ret <= 0) ? IRQ_NONE : IRQ_HANDLED;
+
+exit:
+   data->mxt_status.intp_triggered = false;
+   return ret;
 }
 
 static int mxt_t6_command(struct mxt_data *data, u16 cmd_offset,
@@ -2970,6 +3004,36 @@ static int mxt_bootloader_status(struct mxt_data *data)
return 0;
 }
 
+static void mxt_watchdog_timer(struct timer_list *t)
+{
+   struct mxt_data *data = from_timer(data, t, watchdog_timer);
+
+   if (!work_pending(>watchdog_work)) {
+   if (!data->mxt_status.intp_triggered)
+   schedule_work(>watchdog_work);
+   }
+
+   mxt_start_wd_timer(data);
+}
+
+static void mxt_watchdog_work(struct work_struct *work)
+{
+   struct mxt_data *data =
+   container_of(work, struct mxt_data, watchdog_work);
+   u16 info_buf;
+   int ret = 0;
+   u8 size = 2;
+
+   ret = __mxt_read_reg(data->client, 0, size, _buf);
+
+   if (ret) {
+   data->mxt_status.error_count++;
+   data->mxt_status.dev_status = false;
+   } else {
+   data->mxt_status.dev_status = true;
+   }
+}
+
 static int mxt_initialize(struct mxt_data *data)
 {
struct i2c_client *client = data->client;
@@ -3947,6 +4011,22 @@ static const struct attribute_group mxt_fw_attr_group = {
.attrs = mxt_fw_attrs,
 };

[PATCH v3 44/49] Input: Atmel: use T44 object to process T5 messages

2019-09-17 Thread Jiada Wang
From: Deepak Das 

T44 object returns the count of valid T5 messages in the buffer. According
to atmel, this count should be the main criteria to read the number of T5
messages.

Following is the statement from atmel confirming the same :-
"For the readout of messages we recommend to stop after the last message
is read out from the buffer. One way to identify the amount of new messages
is to read T44. The other way is to monitor the /CHG line which indicates
independent of mode 0 or mode 1 if there are still data in the buffer.
0xFF indicates that there is no message pending anymore, but it is not
recommended to use this as the main criteria to control the
data transfer."

This commit modifies the logic to readout the T5 messages on the basis
of T44 object.

Signed-off-by: Deepak Das 
Signed-off-by: Sanjeev Chugh 
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 55 +++-
 1 file changed, 35 insertions(+), 20 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 93bb19cad7e1..76bda6137bf7 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -1489,7 +1489,7 @@ static u8 mxt_max_msg_read_count(struct mxt_data *data, 
u8 max_T5_msg_count)
return min(T5_msg_count_limit, max_T5_msg_count);
 }
 
-static irqreturn_t mxt_process_messages_t44(struct mxt_data *data)
+static int mxt_process_messages_t44(struct mxt_data *data)
 {
struct device *dev = >client->dev;
int ret;
@@ -1502,7 +1502,7 @@ static irqreturn_t mxt_process_messages_t44(struct 
mxt_data *data)
data->T5_msg_size + 1, data->msg_buf);
if (ret) {
dev_err(dev, "Failed to read T44 and T5 (%d)\n", ret);
-   return IRQ_NONE;
+   return ret;
}
 
T5_msg_count = data->msg_buf[0];
@@ -1512,7 +1512,7 @@ static irqreturn_t mxt_process_messages_t44(struct 
mxt_data *data)
 * Mode 0. It results in unnecessary I2C operations but it is benign.
 */
if (!T5_msg_count)
-   return IRQ_NONE;
+   return processed_valid;
 
if (T5_msg_count > data->max_reportid) {
dev_warn(dev, "T44 count %d exceeded max report id\n",
@@ -1524,12 +1524,14 @@ static irqreturn_t mxt_process_messages_t44(struct 
mxt_data *data)
ret = mxt_proc_message(data, data->msg_buf + 1);
if (ret < 0) {
dev_warn(dev, "Unexpected invalid message\n");
-   return IRQ_NONE;
+   return ret;
}
 
total_pending = T5_msg_count - 1;
-   if (!total_pending)
+   if (!total_pending) {
+   processed_valid = 1;
goto end;
+   }
 
/* Process remaining messages if necessary */
T5_msg_count = mxt_max_msg_read_count(data, total_pending);
@@ -1553,7 +1555,7 @@ static irqreturn_t mxt_process_messages_t44(struct 
mxt_data *data)
data->update_input = false;
}
 
-   return IRQ_HANDLED;
+   return processed_valid;
 }
 
 static int mxt_process_messages_until_invalid(struct mxt_data *data)
@@ -1583,7 +1585,7 @@ static int mxt_process_messages_until_invalid(struct 
mxt_data *data)
return -EBUSY;
 }
 
-static irqreturn_t mxt_process_messages(struct mxt_data *data)
+static int mxt_process_messages(struct mxt_data *data)
 {
int total_handled, num_handled;
u8 count = data->last_message_count;
@@ -1594,7 +1596,7 @@ static irqreturn_t mxt_process_messages(struct mxt_data 
*data)
/* include final invalid message */
total_handled = mxt_read_and_process_messages(data, count + 1);
if (total_handled < 0)
-   return IRQ_NONE;
+   return total_handled;
/* if there were invalid messages, then we are done */
else if (total_handled <= count)
goto update_count;
@@ -1603,7 +1605,7 @@ static irqreturn_t mxt_process_messages(struct mxt_data 
*data)
do {
num_handled = mxt_read_and_process_messages(data, 2);
if (num_handled < 0)
-   return IRQ_NONE;
+   return num_handled;
 
total_handled += num_handled;
 
@@ -1619,12 +1621,13 @@ static irqreturn_t mxt_process_messages(struct mxt_data 
*data)
data->update_input = false;
}
 
-   return IRQ_HANDLED;
+   return total_handled;
 }
 
 static irqreturn_t mxt_interrupt(int irq, void *dev_id)
 {
struct mxt_data *data = dev_id;
+   int ret;
 
if (data->in_bootloader) {
complete(>chg_completion);
@@ -1632,17 +1635,22 @@ static irqreturn_t mxt_interrupt(int irq, void *dev_id)
if (data->flash && >flash-&

[PATCH v3 48/49] Input: atmel_mxt_ts: Implement synchronization during various operation

2019-09-17 Thread Jiada Wang
From: Sanjeev Chugh 

There could be scope of race conditions when sysfs is being handled
and at the same time, device removal is occurring. For example,
we don't want the device removal to begin if the Atmel device
cfg update is going on or firmware update is going on. In such
cases, wait for device update to be completed before the removal
continues.

Thread  Thread 2:
=   =
mxt_update_fw_store()   mxt_remove()
mutex_lock(>lock) ...
mxt_initialize()//Tries to acquire lock
  request_firmware_nowait() mutex_lock(>lock)
... ==>waits for lock()
... .
... .
mutex_unlock(>lock)   .
//Gets lock and proceeds
mxt_free_input_device();
...
mutex_unlock(>lock)
//Frees atmel driver data
kfree(data)

If the request_firmware_nowait() completes after the driver removal,
and callback is triggered. But kernel crashes since the module is
already removed.

This commit adds state machine to serialize such scenarios.

Signed-off-by: Sanjeev Chugh 
Signed-off-by: Bhuvanesh Surachari 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 222 ---
 1 file changed, 196 insertions(+), 26 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index db4ad3b82650..ff6d3ed58604 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -224,6 +224,7 @@ enum t100_type {
 #define MXT_POWERON_DELAY  150 /* msec */
 #define MXT_BOOTLOADER_WAIT36E5/* 1 minute */
 #define MXT_WATCHDOG_TIMEOUT   1000/* msec */
+#define MXT_CONFIG_TIMEOUT 1000/* msec */
 
 /* Command to unlock bootloader */
 #define MXT_UNLOCK_CMD_MSB 0xaa
@@ -247,6 +248,20 @@ enum t100_type {
 
 #define DEBUG_MSG_MAX  200
 
+enum device_state {
+   MXT_STATE_READY,
+   MXT_STATE_UPDATING_CONFIG,
+   MXT_STATE_UPDATING_CONFIG_ASYNC,
+   MXT_STATE_START,
+   MXT_STATE_STOP,
+   MXT_STATE_GOING_AWAY
+};
+
+enum mxt_cmd {
+   UPDATE_CFG,
+   UPDATE_FW
+};
+
 struct mxt_info {
u8 family_id;
u8 variant_id;
@@ -426,11 +441,15 @@ struct mxt_data {
/* Indicates whether device is in suspend */
bool suspended;
 
-   /* Indicates whether device is updating configuration */
-   bool updating_config;
+   struct mutex lock;
 
unsigned int mtu;
bool t25_status;
+
+   /* State handling for probe/remove, open/close and config update */
+   enum device_state e_state;
+
+   struct completion update_cfg_completion;
 };
 
 struct mxt_vb2_buffer {
@@ -1657,6 +1676,7 @@ static irqreturn_t mxt_interrupt(int irq, void *dev_id)
struct mxt_data *data = dev_id;
int ret;
 
+   mutex_lock(>lock);
data->mxt_status.intp_triggered = true;
 
if (data->in_bootloader) {
@@ -1684,6 +1704,8 @@ static irqreturn_t mxt_interrupt(int irq, void *dev_id)
 
 exit:
data->mxt_status.intp_triggered = false;
+   mutex_unlock(>lock);
+
return ret;
 }
 
@@ -2264,6 +2286,8 @@ static void mxt_free_object_table(struct mxt_data *data)
video_unregister_device(>dbg.vdev);
v4l2_device_unregister(>dbg.v4l2);
 #endif
+   mutex_lock(>lock);
+
data->object_table = NULL;
kfree(data->info);
data->info = NULL;
@@ -2293,6 +2317,8 @@ static void mxt_free_object_table(struct mxt_data *data)
data->T100_reportid_min = 0;
data->T100_reportid_max = 0;
data->max_reportid = 0;
+
+   mutex_unlock(>lock);
 }
 
 static int mxt_parse_object_table(struct mxt_data *data,
@@ -2974,8 +3000,15 @@ static int mxt_configure_objects(struct mxt_data *data,
 
 static void mxt_config_cb(const struct firmware *cfg, void *ctx)
 {
+   struct mxt_data *data = ctx;
+
mxt_configure_objects(ctx, cfg);
release_firmware(cfg);
+   complete(>update_cfg_completion);
+   mutex_lock(>lock);
+   if (data->e_state != MXT_STATE_GOING_AWAY)
+   data->e_state = MXT_STATE_READY;
+   mutex_unlock(>lock);
 }
 
 static int mxt_bootloader_status(struct mxt_data *data)
@@ -3088,6 +3121,15 @@ static int mxt_initialize(struct mxt_data *data)
goto err_free_sysfs;
 
if (data->cf

[PATCH v3 37/49] Input: atmel_mxt_ts: Limit the max bytes transferred in an i2c transaction

2019-09-17 Thread Jiada Wang
From: Balasubramani Vivekanandan 

In mxt_process_messages_until_invalid() function, driver tries to read
all possible reportid in a single i2c transaction. Number of bytes read
is limited by the max_reportid parameter.
If the max_reportid is a very large value, then a large chunk of bytes
will be requested from the controller in a single i2c transaction.
This transaction can fail due to timeout. This is visible when the
Atmel controller is connected to the SOC via a i2c mux hardware.

mxt_process_messages_t44() reads the T44 message which contains the
pending T5 message count. If the number of pending T5 messages returned
by T44 message is too high then there is a risk of i2c transaction
timeout while reading T5 messages in mxt_process_messages_t44().

New property 'atmel,mtu' is created. This property limits the maximum
number of bytes that can read/transferred in an i2c transcation

Signed-off-by: Balasubramani Vivekanandan 

Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 .../bindings/input/atmel,maxtouch.txt |  3 +
 drivers/input/touchscreen/atmel_mxt_ts.c  | 65 +++
 2 files changed, 54 insertions(+), 14 deletions(-)

diff --git a/Documentation/devicetree/bindings/input/atmel,maxtouch.txt 
b/Documentation/devicetree/bindings/input/atmel,maxtouch.txt
index d7db16920083..62c93d94bc5d 100644
--- a/Documentation/devicetree/bindings/input/atmel,maxtouch.txt
+++ b/Documentation/devicetree/bindings/input/atmel,maxtouch.txt
@@ -42,6 +42,8 @@ Optional properties for main touchpad device:
 
 - atmel,input_name: Override name of input device from the default.
 
+- atmel,mtu: Maximum number of bytes that can read/transferred in an i2c 
transaction
+
 Example:
 
touch@4b {
@@ -49,4 +51,5 @@ Example:
reg = <0x4b>;
interrupt-parent = <>;
interrupts = ;
+   atmel,mtu = <200>
};
diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 183832e3bd71..5a31c1f50376 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -417,6 +417,7 @@ struct mxt_data {
/* Indicates whether device is updating configuration */
bool updating_config;
 
+   unsigned int mtu;
bool t25_status;
 };
 
@@ -1465,11 +1466,32 @@ static int mxt_read_and_process_messages(struct 
mxt_data *data, u8 count)
return num_valid;
 }
 
+static u8 mxt_max_msg_read_count(struct mxt_data *data, u8 max_T5_msg_count)
+{
+   u8 T5_msg_count_limit = data->mtu / data->T5_msg_size;
+
+   if (!data->mtu)
+   return max_T5_msg_count;
+
+   if (data->mtu < data->T5_msg_size) {
+   WARN(1, "mtu set is lesser than the T5 message size\n");
+   /* Return count of 1, as fallback */
+   return 1;
+   }
+   /*
+* Return maximum number of T5 messages in single i2c transaction
+* based on "atmel,mtu" property.
+*/
+   return min(T5_msg_count_limit, max_T5_msg_count);
+}
+
 static irqreturn_t mxt_process_messages_t44(struct mxt_data *data)
 {
struct device *dev = >client->dev;
int ret;
-   u8 count, num_left;
+   u8 T5_msg_count, total_pending;
+   u8 total_processed = 0;
+   u8 processed_valid = 0;
 
/* Read T44 and T5 together */
ret = __mxt_read_reg(data->client, data->T44_address,
@@ -1479,18 +1501,19 @@ static irqreturn_t mxt_process_messages_t44(struct 
mxt_data *data)
return IRQ_NONE;
}
 
-   count = data->msg_buf[0];
+   T5_msg_count = data->msg_buf[0];
 
/*
 * This condition may be caused by the CHG line being configured in
 * Mode 0. It results in unnecessary I2C operations but it is benign.
 */
-   if (count == 0)
+   if (!T5_msg_count)
return IRQ_NONE;
 
-   if (count > data->max_reportid) {
-   dev_warn(dev, "T44 count %d exceeded max report id\n", count);
-   count = data->max_reportid;
+   if (T5_msg_count > data->max_reportid) {
+   dev_warn(dev, "T44 count %d exceeded max report id\n",
+T5_msg_count);
+   T5_msg_count = data->max_reportid;
}
 
/* Process first message */
@@ -1500,16 +1523,25 @@ static irqreturn_t mxt_process_messages_t44(struct 
mxt_data *data)
return IRQ_NONE;
}
 
-   num_left = count - 1;
+   total_pending = T5_msg_count - 1;
+   if (!total_pending)
+   goto end;
 
/* Process remaining messages if necessary */
-   if (num_left) {
-   ret = mxt_read_and_process_messages(data, num_left);
+   T5_msg_count = mxt_max_msg_read_count(data, total_pending);
+
+   do {
+   if ((total_p

[PATCH v3 38/49] Input: atmel_mxt_ts: return error from mxt_process_messages_until_invalid()

2019-09-17 Thread Jiada Wang
From: Dean Jenkins 

mxt_process_messages_until_invalid() failed to propagate the error
code from mxt_read_and_process_messages() so return the error code.

Signed-off-by: Dean Jenkins 
Signed-off-by: Deepak Das 
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 5a31c1f50376..f2fa62289577 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -1564,6 +1564,8 @@ static int mxt_process_messages_until_invalid(struct 
mxt_data *data)
/* Read messages until we force an invalid */
do {
read = mxt_read_and_process_messages(data, count);
+   if (read < 0)
+   return read;
if (read < count)
return 0;
} while (--tries);
-- 
2.19.2



[PATCH v3 32/49] Input: atmel_mxt_ts - Change call-points of mxt_free_* functions

2019-09-17 Thread Jiada Wang
From: Kautuk Consul 

Revamping the code to call mxt_free_object_table and mxt_free_input_device
functions only in the following scenarios and code paths:
1) The error path of the mxt_probe() entry point
2) The mxt_remove de-init path entry point
3) All paths which definitely expect to populate the object table
   like:
   - the mxt_update_fw_store path which first calls
 mxt_load_fw and then resorts to calling mxt_initialize itself.
   - the mxt_read_info_block function which attempts to fill in the
 object table itself as the main non-error part of the logic.
4) All paths in the code expected to definitely allocate and register
   the input device such as:
   - the mxt_update_fw_store path which first calls
 mxt_load_fw and then resorts to calling mxt_initialize itself.
   - the mxt_update_cfg_store function which will call
 mxt_configure_objects.

Signed-off-by: Kautuk Consul 
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 19 ++-
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 560fe8a3bf62..e441eab8b201 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -3339,21 +3339,21 @@ static int mxt_configure_objects(struct mxt_data *data,
error = mxt_init_t7_power_cfg(data);
if (error) {
dev_err(dev, "Failed to initialize power cfg\n");
-   goto err_free_object_table;
+   return error;
}
 
if (cfg) {
error = mxt_update_cfg(data, cfg);
if (error) {
dev_warn(dev, "Error %d updating config\n", error);
-   goto err_free_object_table;
+   return error;
}
}
 
if (data->multitouch) {
error = mxt_initialize_input_device(data);
if (error)
-   goto err_free_object_table;
+   return error;
} else {
dev_warn(dev, "No touch object detected\n");
}
@@ -3361,10 +3361,6 @@ static int mxt_configure_objects(struct mxt_data *data,
mxt_debug_init(data);
 
return 0;
-
-err_free_object_table:
-   mxt_free_object_table(data);
-   return error;
 }
 
 /* Configuration crc check sum is returned as hex xx */
@@ -4101,16 +4097,21 @@ static int mxt_probe(struct i2c_client *client, const 
struct i2c_device_id *id)
 
error = mxt_initialize(data);
if (error)
-   return error;
+   goto err_free_object;
 
error = sysfs_create_group(>dev.kobj, _fw_attr_group);
if (error) {
dev_err(>dev, "Failure %d creating fw sysfs group\n",
error);
-   return error;
+   goto err_free_object;
}
 
return 0;
+
+err_free_object:
+   mxt_free_input_device(data);
+   mxt_free_object_table(data);
+   return error;
 }
 
 static int mxt_remove(struct i2c_client *client)
-- 
2.19.2



[PATCH v3 33/49] Input: atmel_mxt_ts - rely on calculated_crc rather than file config_crc

2019-09-17 Thread Jiada Wang
From: Kautuk Consul 

We now prefer to rely on the calculated CRC and not on the CRC stored in
the file.

The new logic is as follows:
1) stored CRC of file != calculated CRC of file, then refuse the possible
   corrupted file
2) calculated CRC of file != CRC of configuration in controller, then
   update configuration in controller
3) calculated CRC of file == CRC of configuration in controller, then
   ignore configuration file

Signed-off-by: Kautuk Consul 
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 65 +---
 1 file changed, 36 insertions(+), 29 deletions(-)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index e441eab8b201..bdc5088baea8 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -1908,7 +1908,7 @@ static int mxt_update_cfg(struct mxt_data *data, const 
struct firmware *fw)
int ret;
int offset;
int i;
-   u32 info_crc, config_crc, calculated_crc;
+   u32 info_crc, config_crc, calculated_crc = 0;
u16 crc_start = 0;
 
/* Make zero terminated copy of the OBP_RAW file */
@@ -1971,30 +1971,6 @@ static int mxt_update_cfg(struct mxt_data *data, const 
struct firmware *fw)
}
cfg.raw_pos += offset;
 
-   /*
-* The Info Block CRC is calculated over mxt_info and the object
-* table. If it does not match then we are trying to load the
-* configuration from a different chip or firmware version, so
-* the configuration CRC is invalid anyway.
-*/
-   if (info_crc == data->info_crc) {
-   if (config_crc == 0 || data->config_crc == 0) {
-   dev_info(dev, "CRC zero, attempting to apply config\n");
-   } else if (config_crc == data->config_crc) {
-   dev_dbg(dev, "Config CRC 0x%06X: OK\n",
-data->config_crc);
-   ret = 0;
-   goto release_raw;
-   } else {
-   dev_info(dev, "Config CRC 0x%06X: does not match file 
0x%06X\n",
-data->config_crc, config_crc);
-   }
-   } else {
-   dev_warn(dev,
-"Warning: Info CRC error - device=0x%06X 
file=0x%06X\n",
-data->info_crc, info_crc);
-   }
-
/* Malloc memory to store configuration */
cfg.start_ofs = MXT_OBJECT_START +
data->info->object_num * sizeof(struct mxt_object) +
@@ -2018,14 +1994,45 @@ static int mxt_update_cfg(struct mxt_data *data, const 
struct firmware *fw)
else
dev_warn(dev, "Could not find CRC start\n");
 
-   if (crc_start > cfg.start_ofs) {
+   if (crc_start > cfg.start_ofs)
calculated_crc = mxt_calculate_crc(cfg.mem,
   crc_start - cfg.start_ofs,
   cfg.mem_size);
 
-   if (config_crc > 0 && config_crc != calculated_crc)
-   dev_warn(dev, "Config CRC in file inconsistent, 
calculated=%06X, file=%06X\n",
-calculated_crc, config_crc);
+   /* If the CRC stored in the file is not the same as what
+* was calculated by mxt_calculate_crc, this means we
+* have to refuse the config file and abort download.
+*/
+   if (config_crc != calculated_crc) {
+   dev_warn(dev,
+"Config CRC in file inconsistent, calculated=%06X, 
file=%06X\n",
+calculated_crc, config_crc);
+   ret = 0;
+   goto release_mem;
+   }
+
+   /*
+* The Info Block CRC is calculated over mxt_info and the object
+* table. If it does not match then we are trying to load the
+* configuration from a different chip or firmware version, so
+* the configuration CRC is invalid anyway.
+*/
+   if (info_crc == data->info_crc) {
+   if (config_crc == 0 || data->config_crc == 0) {
+   dev_info(dev, "CRC zero, attempting to apply config\n");
+   } else if (config_crc == data->config_crc) {
+   dev_dbg(dev, "Config CRC 0x%06X: OK\n",
+   data->config_crc);
+   ret = 0;
+   goto release_mem;
+   } else {
+   dev_info(dev, "Config CRC 0x%06X: does not match file 
0x%06X\n",
+data->config_crc, config_crc);
+   }
+   } else {
+   dev_warn(dev,
+ 

[PATCH v3 34/49] input: atmel_mxt_ts: export GPIO reset line via sysfs

2019-09-17 Thread Jiada Wang
From: "George G. Davis" 

N.B. Modifying the atmel_mxt_ts GPIO reset line during operation will
cause problems with normal driver operation.  This feature is provided
as a diagnostic debug aid.  It does not take into consideration any
pending operations which may be in progress.  Modifying the atmel_mxt_ts
GPIO reset line at any time will inevitably cause the driver to fail.

Signed-off-by: George G. Davis 
Signed-off-by: Rajeev Kumar 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 21 +
 1 file changed, 21 insertions(+)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index bdc5088baea8..4670880e52a2 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -4086,6 +4086,19 @@ static int mxt_probe(struct i2c_client *client, const 
struct i2c_device_id *id)
return error;
}
 
+   if (data->reset_gpio) {
+   error = gpiod_export(data->reset_gpio, 0);
+   if (error)
+   return error;
+
+   error = gpiod_export_link(>dev, "reset",
+ data->reset_gpio);
+   if (error) {
+   gpiod_unexport(data->reset_gpio);
+   return error;
+   }
+   }
+
if (data->suspend_mode == MXT_SUSPEND_REGULATOR) {
error = mxt_acquire_irq(data);
if (error)
@@ -4118,6 +4131,10 @@ static int mxt_probe(struct i2c_client *client, const 
struct i2c_device_id *id)
 err_free_object:
mxt_free_input_device(data);
mxt_free_object_table(data);
+   if (data->reset_gpio) {
+   sysfs_remove_link(>dev.kobj, "reset");
+   gpiod_unexport(data->reset_gpio);
+   }
return error;
 }
 
@@ -4127,6 +4144,10 @@ static int mxt_remove(struct i2c_client *client)
 
disable_irq(data->irq);
sysfs_remove_group(>dev.kobj, _fw_attr_group);
+   if (data->reset_gpio) {
+   sysfs_remove_link(>dev.kobj, "reset");
+   gpiod_unexport(data->reset_gpio);
+   }
mxt_debug_msg_remove(data);
mxt_sysfs_remove(data);
mxt_free_input_device(data);
-- 
2.19.2



[PATCH v3 35/49] input: atmel_mxt_ts: Add Missing Delay for reset handling of Atmel touch panel controller in detachable displays.

2019-09-17 Thread Jiada Wang
From: keerthikumarp 

In case of attached display, the touchpanel reset is controlled
via imx gpio's from  atmel driver and the delay between
touchpanel reset and the time at which the chip becomes capable to
communicate with the host processor, has be taken care.

However in case of detachable displays, the touchpanel reset is
controlled via a deserializer gpio which is triggered just before
the atmel driver is probed.The delay between touchpanel reset and
the time at which the chip becomes capable to communicate (as
specified in datasheet) was not being accounted for. This patch
introduces that delay.

Signed-off-by: keerthikumarp 
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 4 
 1 file changed, 4 insertions(+)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 4670880e52a2..4fa27e2f7163 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -4113,6 +4113,10 @@ static int mxt_probe(struct i2c_client *client, const 
struct i2c_device_id *id)
msleep(MXT_RESET_GPIO_TIME);
gpiod_set_value(data->reset_gpio, 1);
msleep(MXT_RESET_INVALID_CHG);
+   } else {
+   dev_dbg(>dev,
+   "atmel reset pin not found in device tree");
+   msleep(MXT_RESET_TIME);
}
 
error = mxt_initialize(data);
-- 
2.19.2



[PATCH v3 36/49] Input: atmel_mxt_ts: Add support for run self-test routine.

2019-09-17 Thread Jiada Wang
From: Nikhil Ravindran 

The self test object T25 runs self test routines in device to find faults
Sysfs entry add to start self test routine and read back the test results
for atmel touchcontrollers.The feature will be used for A-IVI and CAF projects.

Signed-off-by: Nikhil Ravindran 
Signed-off-by: George G. Davis 
Signed-off-by: Jiada Wang 
---
 drivers/input/touchscreen/atmel_mxt_ts.c | 111 +++
 1 file changed, 111 insertions(+)

diff --git a/drivers/input/touchscreen/atmel_mxt_ts.c 
b/drivers/input/touchscreen/atmel_mxt_ts.c
index 4fa27e2f7163..183832e3bd71 100644
--- a/drivers/input/touchscreen/atmel_mxt_ts.c
+++ b/drivers/input/touchscreen/atmel_mxt_ts.c
@@ -336,6 +336,9 @@ struct mxt_data {
u8 t100_aux_ampl;
u8 t100_aux_area;
u8 t100_aux_vect;
+   u16 T25_address;
+   u8  T25_reportid;
+   u8  t25_msg[6];
bool debug_enabled;
bool debug_v2_enabled;
u8 *debug_msg_data;
@@ -413,6 +416,8 @@ struct mxt_data {
 
/* Indicates whether device is updating configuration */
bool updating_config;
+
+   bool t25_status;
 };
 
 struct mxt_vb2_buffer {
@@ -1359,6 +1364,24 @@ static void mxt_proc_t93_messages(struct mxt_data *data, 
u8 *msg)
dev_debug(dev, "T93 report double tap %d\n", status);
 }
 
+static void mxt_proc_t25_messages(struct mxt_data *data, u8 *msg)
+{
+   struct device *dev = >client->dev;
+
+   /* Output debug if status has changed */
+   dev_dbg(dev, "T25 Status 0x%x Info: %x %x %x %x %x\n",
+   msg[1],
+   msg[2],
+   msg[3],
+   msg[4],
+   msg[5],
+   msg[6]);
+
+   /* Save current status */
+   memcpy(>t25_msg[0], [1], sizeof(data->t25_msg));
+   data->t25_status = false;
+}
+
 static int mxt_proc_message(struct mxt_data *data, u8 *message)
 {
u8 report_id = message[0];
@@ -1389,6 +1412,8 @@ static int mxt_proc_message(struct mxt_data *data, u8 
*message)
} else if (report_id == data->T19_reportid) {
mxt_input_button(data, message);
data->update_input = true;
+   } else if (report_id == data->T25_reportid) {
+   mxt_proc_t25_messages(data, message);
} else if (report_id >= data->T15_reportid_min
   && report_id <= data->T15_reportid_max) {
mxt_proc_t15_messages(data, message);
@@ -1613,6 +1638,84 @@ static int mxt_t6_command(struct mxt_data *data, u16 
cmd_offset,
return 0;
 }
 
+static int mxt_t25_command(struct mxt_data *data, u8 cmd, bool wait)
+{
+   u16 reg;
+   int timeout_counter = 0;
+   int ret;
+   u8  val[2];
+
+   reg = data->T25_address;
+   val[0] = 0x3;
+   val[1] = cmd;
+
+   data->t25_status = true;
+   ret = __mxt_write_reg(data->client, reg, sizeof(val), val);
+   if (ret) {
+   data->t25_status = false;
+   return ret;
+   }
+
+   if (!wait)
+   return 0;
+
+   do {
+   msleep(MXT_WAKEUP_TIME);
+   ret = __mxt_read_reg(data->client, reg + 1, 1, [1]);
+   if (ret)
+   return ret;
+   } while ((val[1] != 0) && (timeout_counter++ <= 100));
+
+   if (timeout_counter > 100) {
+   dev_err(>client->dev, "Command failed!\n");
+   data->t25_status = false;
+   return -EIO;
+   }
+   return 0;
+}
+
+/* Firmware Version is returned as Major.Minor.Build */
+static ssize_t mxt_t25_selftest_show(struct device *dev, struct
+device_attribute *attr, char *buf)
+{
+   struct mxt_data *data = dev_get_drvdata(dev);
+   ssize_t offset = 0;
+
+   if (data->t25_status)
+   return -EAGAIN;
+
+   if (data->t25_msg[0] == 0xFE)
+   offset += scnprintf(buf, PAGE_SIZE, "PASS\n");
+   else
+   offset += scnprintf(buf, PAGE_SIZE, "FAILED\n");
+
+   offset += scnprintf(buf + offset, PAGE_SIZE, "%x %x %x %x %x %x\n",
+data->t25_msg[0],
+data->t25_msg[1],
+data->t25_msg[2],
+data->t25_msg[3],
+data->t25_msg[4],
+data->t25_msg[5]);
+   return offset;
+}
+
+static ssize_t mxt_t25_selftest_store(struct device *dev, struct
+ device_attribute *attr, const char *buf,
+ size_t count)
+{
+   struct mxt_data *data = dev_get_drvdata(dev);
+   u32 cmd;
+
+   if (sscanf(buf, "%x", ) == 1) {
+   if (mxt_t25_command(data, (u8)cmd, 1) == 0)
+   return count;
+
+   dev_dbg(dev, "mxt_t25_cmd_store write cmd %x error\

  1   2   3   4   5   6   7   >