Skip to contentSkip to conversation

Input: ili210x - Improve polled sample spacing

PATCH Applied Marek Vasut 2 participants 4 messages Nov 8, 2021 – Nov 10, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Input: ili210x - Improve polled sample spacing

Currently the ili210x driver implements a threaded interrupt handler which starts upon edge on the interrupt line, and then polls the touch controller for samples. Every time a sample is obtained from the controller, the thread function checks whether further polling is required, and if so, waits fixed amount of time before polling for next sample.

The delay between consecutive samples can thus vary greatly, because the I2C transfer required to retrieve the sample from the controller takes different amount of time on different platforms. Furthermore, different models of the touch controllers supported by this driver require different delays during retrieval of samples too.

Instead of waiting fixed amount of time before polling for next sample, determine how much time passed since the beginning of sampling cycle and then wait only the remaining amount of time within the sampling cycle. This makes the driver deliver samples with equal spacing between them.

Cc Dmitry Torokhov <[email protected]>
Cc Joe Hung <[email protected]>
Cc Luca Hsu <[email protected]>
---
 drivers/input/touchscreen/ili210x.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/input/touchscreen/ili210x.c +8 -2
index a3b71a9511eb3..b2d9fe1e1c707 100644
@@ -328,10 +328,13 @@ static irqreturn_t ili210x_irq(int irq, void *irq_data)
 	const struct ili2xxx_chip *chip = priv->chip;
 	u8 touchdata[ILI210X_DATA_SIZE] = { 0 };
 	bool keep_polling;
+	ktime_t time_next;
+	s64 time_delta;
 	bool touch;
 	int error;
 
 	do {
+		time_next = ktime_add_ms(ktime_get(), ILI2XXX_POLL_PERIOD);
 		error = chip->get_touch_data(client, touchdata);
 		if (error) {
 			dev_err(&client->dev,
@@ -341,8 +344,11 @@ static irqreturn_t ili210x_irq(int irq, void *irq_data)
 
 		touch = ili210x_report_events(priv, touchdata);
 		keep_polling = chip->continue_polling(touchdata, touch);
-		if (keep_polling)
-			msleep(ILI2XXX_POLL_PERIOD);
+		if (keep_polling) {
+			time_delta = ktime_us_delta(time_next, ktime_get());
+			if (time_delta > 0)
+				usleep_range(time_delta, time_delta + 1000);
+		}
 	} while (!priv->stop && keep_polling);
 
 	return IRQ_HANDLED;
Signature · 1 line
2.33.0
maintainer
#1 lore raw

Hi Marek,

Quoted 44 lines · Marek Vasut

Currently the ili210x driver implements a threaded interrupt handler which starts upon edge on the interrupt line, and then polls the touch controller for samples. Every time a sample is obtained from the controller, the thread function checks whether further polling is required, and if so, waits fixed amount of time before polling for next sample.

The delay between consecutive samples can thus vary greatly, because the I2C transfer required to retrieve the sample from the controller takes different amount of time on different platforms. Furthermore, different models of the touch controllers supported by this driver require different delays during retrieval of samples too.

Instead of waiting fixed amount of time before polling for next sample, determine how much time passed since the beginning of sampling cycle and then wait only the remaining amount of time within the sampling cycle. This makes the driver deliver samples with equal spacing between them.

Cc Dmitry Torokhov <[email protected]>
Cc Joe Hung <[email protected]>
Cc Luca Hsu <[email protected]>
---
 drivers/input/touchscreen/ili210x.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/input/touchscreen/ili210x.c +3 -0
index a3b71a9511eb3..b2d9fe1e1c707 100644
@@ -328,10 +328,13 @@ static irqreturn_t ili210x_irq(int irq, void *irq_data)
 	const struct ili2xxx_chip *chip = priv->chip;
 	u8 touchdata[ILI210X_DATA_SIZE] = { 0 };
 	bool keep_polling;
+	ktime_t time_next;
+	s64 time_delta;
 	bool touch;
 	int error;
 
 	do {
+		time_next = ktime_add_ms(ktime_get(), ILI2XXX_POLL_PERIOD);
 		error = chip->get_touch_data(client, touchdata);
 		if (error) {
 			dev_err(&client->dev,

drivers/input/touchscreen/ili210x.c static irqreturn_t ili210x_irq(int irq, void *irq_data) · line 348

Quoted 7 lines
drivers/input/touchscreen/ili210x.c +2 -2
@@ -341,8 +344,11 @@ static irqreturn_t ili210x_irq(int irq, void *irq_data)
 
 		touch = ili210x_report_events(priv, touchdata);
 		keep_polling = chip->continue_polling(touchdata, touch);
-		if (keep_polling)
-			msleep(ILI2XXX_POLL_PERIOD);
+		if (keep_polling) {
+			time_delta = ktime_us_delta(time_next, ktime_get());

Do we really need to use exact time, or ktime_get_coarse() is good enough, as it is cheaper?

Quoted 9 lines
drivers/input/touchscreen/ili210x.c +3 -0
+			if (time_delta > 0)
+				usleep_range(time_delta, time_delta + 1000);
+		}
 	} while (!priv->stop && keep_polling);
 
 	return IRQ_HANDLED;
Signature · 1 line
2.33.0

Thanks.

Signature · 1 line
Dmitry
#2 lore raw
Quoted 1 line · Dmitry Torokhov

Hi Marek,

Hi,

[...]

Quoted 20 lines
Quoted 20 lines
drivers/input/touchscreen/ili210x.c +3 -0
index a3b71a9511eb3..b2d9fe1e1c707 100644
@@ -328,10 +328,13 @@ static irqreturn_t ili210x_irq(int irq, void *irq_data)
  	const struct ili2xxx_chip *chip = priv->chip;
  	u8 touchdata[ILI210X_DATA_SIZE] = { 0 };
  	bool keep_polling;
+	ktime_t time_next;
+	s64 time_delta;
  	bool touch;
  	int error;
 
  	do {
+		time_next = ktime_add_ms(ktime_get(), ILI2XXX_POLL_PERIOD);
  		error = chip->get_touch_data(client, touchdata);
  		if (error) {
  			dev_err(&client->dev,
@@ -341,8 +344,11 @@ static irqreturn_t ili210x_irq(int irq, void *irq_data)
 
Quoted 9 lines

drivers/input/touchscreen/ili210x.c static irqreturn_t ili210x_irq(int irq, void *irq_data) · line 348

Quoted 6 lines
drivers/input/touchscreen/ili210x.c +2 -2
  		touch = ili210x_report_events(priv, touchdata);
  		keep_polling = chip->continue_polling(touchdata, touch);
-		if (keep_polling)
-			msleep(ILI2XXX_POLL_PERIOD);
+		if (keep_polling) {
+			time_delta = ktime_us_delta(time_next, ktime_get());

Do we really need to use exact time, or ktime_get_coarse() is good enough, as it is cheaper?

ktime_get_coarse() introduces multi-millisecond jitter into the samples if you have low HZ setting, which isn't really nice. That's also why I use ktime and not jiffies here, jiffies has the same problem.

maintainer Applied
#3 lore raw
Quoted 35 lines · Marek Vasut
Quoted 1 line · Dmitry Torokhov

Hi Marek,

Hi,

[...]

Quoted 19 lines
Quoted 19 lines
drivers/input/touchscreen/ili210x.c +3 -0
index a3b71a9511eb3..b2d9fe1e1c707 100644
@@ -328,10 +328,13 @@ static irqreturn_t ili210x_irq(int irq, void *irq_data)
  	const struct ili2xxx_chip *chip = priv->chip;
  	u8 touchdata[ILI210X_DATA_SIZE] = { 0 };
  	bool keep_polling;
+	ktime_t time_next;
+	s64 time_delta;
  	bool touch;
  	int error;
  	do {
+		time_next = ktime_add_ms(ktime_get(), ILI2XXX_POLL_PERIOD);
  		error = chip->get_touch_data(client, touchdata);
  		if (error) {
  			dev_err(&client->dev,
@@ -341,8 +344,11 @@ static irqreturn_t ili210x_irq(int irq, void *irq_data)
  		touch = ili210x_report_events(priv, touchdata);
Quoted 8 lines

drivers/input/touchscreen/ili210x.c static irqreturn_t ili210x_irq(int irq, void *irq_data) · line 348

Quoted 5 lines
drivers/input/touchscreen/ili210x.c +2 -2
  		keep_polling = chip->continue_polling(touchdata, touch);
-		if (keep_polling)
-			msleep(ILI2XXX_POLL_PERIOD);
+		if (keep_polling) {
+			time_delta = ktime_us_delta(time_next, ktime_get());

Do we really need to use exact time, or ktime_get_coarse() is good enough, as it is cheaper?

Quoted 3 lines

ktime_get_coarse() introduces multi-millisecond jitter into the samples if you have low HZ setting, which isn't really nice. That's also why I use ktime and not jiffies here, jiffies has the same problem.

Applied, thank you.

Signature · 1 line
Dmitry