All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/5] Miscellaneous fixes for Azoteq IQS269A
@ 2022-11-29  3:00 Jeff LaBundy
  2022-11-29  3:02 ` [PATCH 1/5] Input: iqs269a - drop unused device node references Jeff LaBundy
                   ` (4 more replies)
  0 siblings, 5 replies; 11+ messages in thread
From: Jeff LaBundy @ 2022-11-29  3:00 UTC (permalink / raw)
  To: dmitry.torokhov; +Cc: linux-input, jeff

This series comprises a handful of minor fixes that result from
continued testing and updated guidance from the vendor.

Jeff LaBundy (5):
  Input: iqs269a - drop unused device node references
  Input: iqs269a - increase interrupt handler return delay
  Input: iqs269a - configure device with a single block write
  Input: iqs269a - do not poll during suspend or resume
  Input: iqs269a - do not poll during ATI

 drivers/input/misc/iqs269a.c | 339 ++++++++++++++---------------------
 1 file changed, 137 insertions(+), 202 deletions(-)

-- 
2.34.1


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 1/5] Input: iqs269a - drop unused device node references
  2022-11-29  3:00 [PATCH 0/5] Miscellaneous fixes for Azoteq IQS269A Jeff LaBundy
@ 2022-11-29  3:02 ` Jeff LaBundy
  2022-12-15  9:31   ` Mattijs Korpershoek
  2022-11-29  3:02 ` [PATCH 2/5] Input: iqs269a - increase interrupt handler return delay Jeff LaBundy
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 11+ messages in thread
From: Jeff LaBundy @ 2022-11-29  3:02 UTC (permalink / raw)
  To: dmitry.torokhov; +Cc: linux-input, jeff

Each call to device/fwnode_get_named_child_node() must be matched
with a call to fwnode_handle_put() once the corresponding node is
no longer in use. This ensures a reference count remains balanced
in the case of dynamic device tree support.

Currently, the driver does not call fwnode_handle_put() on nested
event nodes. This patch solves this problem by adding the missing
instances of fwnode_handle_put().

As part of this change, the logic which parses each channel's key
code is gently refactored in order to reduce the number of places
from which fwnode_handle_put() is called.

Fixes: 04e49867fad1 ("Input: add support for Azoteq IQS269A")
Signed-off-by: Jeff LaBundy <jeff@labundy.com>
---
 drivers/input/misc/iqs269a.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/drivers/input/misc/iqs269a.c b/drivers/input/misc/iqs269a.c
index a348247d3d38..5620a009bf55 100644
--- a/drivers/input/misc/iqs269a.c
+++ b/drivers/input/misc/iqs269a.c
@@ -694,7 +694,8 @@ static int iqs269_parse_chan(struct iqs269_private *iqs269,
 				dev_err(&client->dev,
 					"Invalid channel %u threshold: %u\n",
 					reg, val);
-				return -EINVAL;
+				error = -EINVAL;
+				break;
 			}
 
 			ch_reg->thresh[iqs269_events[i].th_offs] = val;
@@ -707,7 +708,8 @@ static int iqs269_parse_chan(struct iqs269_private *iqs269,
 				dev_err(&client->dev,
 					"Invalid channel %u hysteresis: %u\n",
 					reg, val);
-				return -EINVAL;
+				error = -EINVAL;
+				break;
 			}
 
 			if (i == IQS269_EVENT_DEEP_DN ||
@@ -721,8 +723,19 @@ static int iqs269_parse_chan(struct iqs269_private *iqs269,
 			}
 		}
 
-		if (fwnode_property_read_u32(ev_node, "linux,code", &val))
+		error = fwnode_property_read_u32(ev_node, "linux,code", &val);
+		if (error && error != -EINVAL) {
+			dev_err(&client->dev,
+				"Failed to read channel %u code: %d\n", reg,
+				error);
+			break;
+		}
+
+		fwnode_handle_put(ev_node);
+		if (error) {
+			error = 0;
 			continue;
+		}
 
 		switch (reg) {
 		case IQS269_CHx_HALL_ACTIVE:
@@ -744,7 +757,10 @@ static int iqs269_parse_chan(struct iqs269_private *iqs269,
 		iqs269->sys_reg.event_mask &= ~iqs269_events[i].mask;
 	}
 
-	return 0;
+	if (error)
+		fwnode_handle_put(ev_node);
+
+	return error;
 }
 
 static int iqs269_parse_prop(struct iqs269_private *iqs269)
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 2/5] Input: iqs269a - increase interrupt handler return delay
  2022-11-29  3:00 [PATCH 0/5] Miscellaneous fixes for Azoteq IQS269A Jeff LaBundy
  2022-11-29  3:02 ` [PATCH 1/5] Input: iqs269a - drop unused device node references Jeff LaBundy
@ 2022-11-29  3:02 ` Jeff LaBundy
  2022-12-15  9:32   ` Mattijs Korpershoek
  2022-11-29  3:02 ` [PATCH 3/5] Input: iqs269a - configure device with a single block write Jeff LaBundy
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 11+ messages in thread
From: Jeff LaBundy @ 2022-11-29  3:02 UTC (permalink / raw)
  To: dmitry.torokhov; +Cc: linux-input, jeff

The time the device takes to deassert its RDY output following an
I2C stop condition scales with the core clock frequency.

To prevent level-triggered interrupts from being reasserted after
the interrupt handler returns, increase the time before returning
to account for the worst-case delay (~140 us) plus margin.

Fixes: 04e49867fad1 ("Input: add support for Azoteq IQS269A")
Signed-off-by: Jeff LaBundy <jeff@labundy.com>
---
 drivers/input/misc/iqs269a.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/input/misc/iqs269a.c b/drivers/input/misc/iqs269a.c
index 5620a009bf55..711e67db71a4 100644
--- a/drivers/input/misc/iqs269a.c
+++ b/drivers/input/misc/iqs269a.c
@@ -153,7 +153,7 @@
 #define IQS269_PWR_MODE_POLL_SLEEP_US		IQS269_ATI_POLL_SLEEP_US
 #define IQS269_PWR_MODE_POLL_TIMEOUT_US		IQS269_ATI_POLL_TIMEOUT_US
 
-#define iqs269_irq_wait()			usleep_range(100, 150)
+#define iqs269_irq_wait()			usleep_range(200, 250)
 
 enum iqs269_local_cap_size {
 	IQS269_LOCAL_CAP_SIZE_0,
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 3/5] Input: iqs269a - configure device with a single block write
  2022-11-29  3:00 [PATCH 0/5] Miscellaneous fixes for Azoteq IQS269A Jeff LaBundy
  2022-11-29  3:02 ` [PATCH 1/5] Input: iqs269a - drop unused device node references Jeff LaBundy
  2022-11-29  3:02 ` [PATCH 2/5] Input: iqs269a - increase interrupt handler return delay Jeff LaBundy
@ 2022-11-29  3:02 ` Jeff LaBundy
  2022-12-15  9:55   ` Mattijs Korpershoek
  2022-11-29  3:02 ` [PATCH 4/5] Input: iqs269a - do not poll during suspend or resume Jeff LaBundy
  2022-11-29  3:03 ` [PATCH 5/5] Input: iqs269a - do not poll during ATI Jeff LaBundy
  4 siblings, 1 reply; 11+ messages in thread
From: Jeff LaBundy @ 2022-11-29  3:02 UTC (permalink / raw)
  To: dmitry.torokhov; +Cc: linux-input, jeff

Unless it is being done as part of servicing a soft reset interrupt,
configuring channels on-the-fly (as is the case when writing to the
ati_trigger attribute) may cause GPIO3 (which reflects the state of
touch for a selected channel) to be inadvertently asserted.

To solve this problem, follow the vendor's recommendation and write
all channel configuration as well as the REDO_ATI register field as
part of a single block write. This ensures the device has been told
to re-calibrate itself following an I2C stop condition, after which
sensing resumes and GPIO3 may be asserted.

Fixes: 04e49867fad1 ("Input: add support for Azoteq IQS269A")
Signed-off-by: Jeff LaBundy <jeff@labundy.com>
---
 drivers/input/misc/iqs269a.c | 98 ++++++++++++++----------------------
 1 file changed, 39 insertions(+), 59 deletions(-)

diff --git a/drivers/input/misc/iqs269a.c b/drivers/input/misc/iqs269a.c
index 711e67db71a4..0eb3cff177e5 100644
--- a/drivers/input/misc/iqs269a.c
+++ b/drivers/input/misc/iqs269a.c
@@ -96,8 +96,6 @@
 #define IQS269_MISC_B_TRACKING_UI_ENABLE	BIT(4)
 #define IQS269_MISC_B_FILT_STR_SLIDER		GENMASK(1, 0)
 
-#define IQS269_CHx_SETTINGS			0x8C
-
 #define IQS269_CHx_ENG_A_MEAS_CAP_SIZE		BIT(15)
 #define IQS269_CHx_ENG_A_RX_GND_INACTIVE	BIT(13)
 #define IQS269_CHx_ENG_A_LOCAL_CAP_SIZE		BIT(12)
@@ -245,6 +243,18 @@ struct iqs269_ver_info {
 	u8 padding;
 } __packed;
 
+struct iqs269_ch_reg {
+	u8 rx_enable;
+	u8 tx_enable;
+	__be16 engine_a;
+	__be16 engine_b;
+	__be16 ati_comp;
+	u8 thresh[3];
+	u8 hyst;
+	u8 assoc_select;
+	u8 assoc_weight;
+} __packed;
+
 struct iqs269_sys_reg {
 	__be16 general;
 	u8 active;
@@ -266,18 +276,7 @@ struct iqs269_sys_reg {
 	u8 timeout_swipe;
 	u8 thresh_swipe;
 	u8 redo_ati;
-} __packed;
-
-struct iqs269_ch_reg {
-	u8 rx_enable;
-	u8 tx_enable;
-	__be16 engine_a;
-	__be16 engine_b;
-	__be16 ati_comp;
-	u8 thresh[3];
-	u8 hyst;
-	u8 assoc_select;
-	u8 assoc_weight;
+	struct iqs269_ch_reg ch_reg[IQS269_NUM_CH];
 } __packed;
 
 struct iqs269_flags {
@@ -292,7 +291,6 @@ struct iqs269_private {
 	struct regmap *regmap;
 	struct mutex lock;
 	struct iqs269_switch_desc switches[ARRAY_SIZE(iqs269_events)];
-	struct iqs269_ch_reg ch_reg[IQS269_NUM_CH];
 	struct iqs269_sys_reg sys_reg;
 	struct input_dev *keypad;
 	struct input_dev *slider[IQS269_NUM_SL];
@@ -307,6 +305,7 @@ struct iqs269_private {
 static int iqs269_ati_mode_set(struct iqs269_private *iqs269,
 			       unsigned int ch_num, unsigned int mode)
 {
+	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
 	u16 engine_a;
 
 	if (ch_num >= IQS269_NUM_CH)
@@ -317,12 +316,12 @@ static int iqs269_ati_mode_set(struct iqs269_private *iqs269,
 
 	mutex_lock(&iqs269->lock);
 
-	engine_a = be16_to_cpu(iqs269->ch_reg[ch_num].engine_a);
+	engine_a = be16_to_cpu(ch_reg[ch_num].engine_a);
 
 	engine_a &= ~IQS269_CHx_ENG_A_ATI_MODE_MASK;
 	engine_a |= (mode << IQS269_CHx_ENG_A_ATI_MODE_SHIFT);
 
-	iqs269->ch_reg[ch_num].engine_a = cpu_to_be16(engine_a);
+	ch_reg[ch_num].engine_a = cpu_to_be16(engine_a);
 	iqs269->ati_current = false;
 
 	mutex_unlock(&iqs269->lock);
@@ -333,13 +332,14 @@ static int iqs269_ati_mode_set(struct iqs269_private *iqs269,
 static int iqs269_ati_mode_get(struct iqs269_private *iqs269,
 			       unsigned int ch_num, unsigned int *mode)
 {
+	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
 	u16 engine_a;
 
 	if (ch_num >= IQS269_NUM_CH)
 		return -EINVAL;
 
 	mutex_lock(&iqs269->lock);
-	engine_a = be16_to_cpu(iqs269->ch_reg[ch_num].engine_a);
+	engine_a = be16_to_cpu(ch_reg[ch_num].engine_a);
 	mutex_unlock(&iqs269->lock);
 
 	engine_a &= IQS269_CHx_ENG_A_ATI_MODE_MASK;
@@ -351,6 +351,7 @@ static int iqs269_ati_mode_get(struct iqs269_private *iqs269,
 static int iqs269_ati_base_set(struct iqs269_private *iqs269,
 			       unsigned int ch_num, unsigned int base)
 {
+	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
 	u16 engine_b;
 
 	if (ch_num >= IQS269_NUM_CH)
@@ -379,12 +380,12 @@ static int iqs269_ati_base_set(struct iqs269_private *iqs269,
 
 	mutex_lock(&iqs269->lock);
 
-	engine_b = be16_to_cpu(iqs269->ch_reg[ch_num].engine_b);
+	engine_b = be16_to_cpu(ch_reg[ch_num].engine_b);
 
 	engine_b &= ~IQS269_CHx_ENG_B_ATI_BASE_MASK;
 	engine_b |= base;
 
-	iqs269->ch_reg[ch_num].engine_b = cpu_to_be16(engine_b);
+	ch_reg[ch_num].engine_b = cpu_to_be16(engine_b);
 	iqs269->ati_current = false;
 
 	mutex_unlock(&iqs269->lock);
@@ -395,13 +396,14 @@ static int iqs269_ati_base_set(struct iqs269_private *iqs269,
 static int iqs269_ati_base_get(struct iqs269_private *iqs269,
 			       unsigned int ch_num, unsigned int *base)
 {
+	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
 	u16 engine_b;
 
 	if (ch_num >= IQS269_NUM_CH)
 		return -EINVAL;
 
 	mutex_lock(&iqs269->lock);
-	engine_b = be16_to_cpu(iqs269->ch_reg[ch_num].engine_b);
+	engine_b = be16_to_cpu(ch_reg[ch_num].engine_b);
 	mutex_unlock(&iqs269->lock);
 
 	switch (engine_b & IQS269_CHx_ENG_B_ATI_BASE_MASK) {
@@ -429,6 +431,7 @@ static int iqs269_ati_base_get(struct iqs269_private *iqs269,
 static int iqs269_ati_target_set(struct iqs269_private *iqs269,
 				 unsigned int ch_num, unsigned int target)
 {
+	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
 	u16 engine_b;
 
 	if (ch_num >= IQS269_NUM_CH)
@@ -439,12 +442,12 @@ static int iqs269_ati_target_set(struct iqs269_private *iqs269,
 
 	mutex_lock(&iqs269->lock);
 
-	engine_b = be16_to_cpu(iqs269->ch_reg[ch_num].engine_b);
+	engine_b = be16_to_cpu(ch_reg[ch_num].engine_b);
 
 	engine_b &= ~IQS269_CHx_ENG_B_ATI_TARGET_MASK;
 	engine_b |= target / 32;
 
-	iqs269->ch_reg[ch_num].engine_b = cpu_to_be16(engine_b);
+	ch_reg[ch_num].engine_b = cpu_to_be16(engine_b);
 	iqs269->ati_current = false;
 
 	mutex_unlock(&iqs269->lock);
@@ -455,13 +458,14 @@ static int iqs269_ati_target_set(struct iqs269_private *iqs269,
 static int iqs269_ati_target_get(struct iqs269_private *iqs269,
 				 unsigned int ch_num, unsigned int *target)
 {
+	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
 	u16 engine_b;
 
 	if (ch_num >= IQS269_NUM_CH)
 		return -EINVAL;
 
 	mutex_lock(&iqs269->lock);
-	engine_b = be16_to_cpu(iqs269->ch_reg[ch_num].engine_b);
+	engine_b = be16_to_cpu(ch_reg[ch_num].engine_b);
 	mutex_unlock(&iqs269->lock);
 
 	*target = (engine_b & IQS269_CHx_ENG_B_ATI_TARGET_MASK) * 32;
@@ -531,13 +535,7 @@ static int iqs269_parse_chan(struct iqs269_private *iqs269,
 	if (fwnode_property_present(ch_node, "azoteq,slider1-select"))
 		iqs269->sys_reg.slider_select[1] |= BIT(reg);
 
-	ch_reg = &iqs269->ch_reg[reg];
-
-	error = regmap_raw_read(iqs269->regmap,
-				IQS269_CHx_SETTINGS + reg * sizeof(*ch_reg) / 2,
-				ch_reg, sizeof(*ch_reg));
-	if (error)
-		return error;
+	ch_reg = &iqs269->sys_reg.ch_reg[reg];
 
 	error = iqs269_parse_mask(ch_node, "azoteq,rx-enable",
 				  &ch_reg->rx_enable);
@@ -1048,10 +1046,8 @@ static int iqs269_parse_prop(struct iqs269_private *iqs269)
 
 static int iqs269_dev_init(struct iqs269_private *iqs269)
 {
-	struct iqs269_sys_reg *sys_reg = &iqs269->sys_reg;
-	struct iqs269_ch_reg *ch_reg;
 	unsigned int val;
-	int error, i;
+	int error;
 
 	mutex_lock(&iqs269->lock);
 
@@ -1061,27 +1057,8 @@ static int iqs269_dev_init(struct iqs269_private *iqs269)
 	if (error)
 		goto err_mutex;
 
-	for (i = 0; i < IQS269_NUM_CH; i++) {
-		if (!(sys_reg->active & BIT(i)))
-			continue;
-
-		ch_reg = &iqs269->ch_reg[i];
-
-		error = regmap_raw_write(iqs269->regmap,
-					 IQS269_CHx_SETTINGS + i *
-					 sizeof(*ch_reg) / 2, ch_reg,
-					 sizeof(*ch_reg));
-		if (error)
-			goto err_mutex;
-	}
-
-	/*
-	 * The REDO-ATI and ATI channel selection fields must be written in the
-	 * same block write, so every field between registers 0x80 through 0x8B
-	 * (inclusive) must be written as well.
-	 */
-	error = regmap_raw_write(iqs269->regmap, IQS269_SYS_SETTINGS, sys_reg,
-				 sizeof(*sys_reg));
+	error = regmap_raw_write(iqs269->regmap, IQS269_SYS_SETTINGS,
+				 &iqs269->sys_reg, sizeof(iqs269->sys_reg));
 	if (error)
 		goto err_mutex;
 
@@ -1355,6 +1332,7 @@ static ssize_t hall_bin_show(struct device *dev,
 			     struct device_attribute *attr, char *buf)
 {
 	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
+	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
 	struct i2c_client *client = iqs269->client;
 	unsigned int val;
 	int error;
@@ -1369,8 +1347,8 @@ static ssize_t hall_bin_show(struct device *dev,
 	if (error)
 		return error;
 
-	switch (iqs269->ch_reg[IQS269_CHx_HALL_ACTIVE].rx_enable &
-		iqs269->ch_reg[IQS269_CHx_HALL_INACTIVE].rx_enable) {
+	switch (ch_reg[IQS269_CHx_HALL_ACTIVE].rx_enable &
+		ch_reg[IQS269_CHx_HALL_INACTIVE].rx_enable) {
 	case IQS269_HALL_PAD_R:
 		val &= IQS269_CAL_DATA_A_HALL_BIN_R_MASK;
 		val >>= IQS269_CAL_DATA_A_HALL_BIN_R_SHIFT;
@@ -1450,9 +1428,10 @@ static ssize_t rx_enable_show(struct device *dev,
 			      struct device_attribute *attr, char *buf)
 {
 	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
+	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
 
 	return scnprintf(buf, PAGE_SIZE, "%u\n",
-			 iqs269->ch_reg[iqs269->ch_num].rx_enable);
+			 ch_reg[iqs269->ch_num].rx_enable);
 }
 
 static ssize_t rx_enable_store(struct device *dev,
@@ -1460,6 +1439,7 @@ static ssize_t rx_enable_store(struct device *dev,
 			       size_t count)
 {
 	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
+	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
 	unsigned int val;
 	int error;
 
@@ -1472,7 +1452,7 @@ static ssize_t rx_enable_store(struct device *dev,
 
 	mutex_lock(&iqs269->lock);
 
-	iqs269->ch_reg[iqs269->ch_num].rx_enable = val;
+	ch_reg[iqs269->ch_num].rx_enable = val;
 	iqs269->ati_current = false;
 
 	mutex_unlock(&iqs269->lock);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 4/5] Input: iqs269a - do not poll during suspend or resume
  2022-11-29  3:00 [PATCH 0/5] Miscellaneous fixes for Azoteq IQS269A Jeff LaBundy
                   ` (2 preceding siblings ...)
  2022-11-29  3:02 ` [PATCH 3/5] Input: iqs269a - configure device with a single block write Jeff LaBundy
@ 2022-11-29  3:02 ` Jeff LaBundy
  2022-12-15 10:07   ` Mattijs Korpershoek
  2022-11-29  3:03 ` [PATCH 5/5] Input: iqs269a - do not poll during ATI Jeff LaBundy
  4 siblings, 1 reply; 11+ messages in thread
From: Jeff LaBundy @ 2022-11-29  3:02 UTC (permalink / raw)
  To: dmitry.torokhov; +Cc: linux-input, jeff

Polling the device while it transitions from automatic to manual
power mode switching may keep the device from actually finishing
the transition. The process appears to time out depending on the
polling rate and the device's core clock frequency.

This is ultimately unnecessary in the first place; instead it is
sufficient to write the desired mode during initialization, then
disable automatic switching at suspend. This eliminates the need
to ensure the device is prepared for a manual change and removes
the 'suspend_mode' variable.

Similarly, polling the device while it transitions from one mode
to another under manual control may time out as well. This added
step does not appear to be necessary either, so drop it.

Fixes: 04e49867fad1 ("Input: add support for Azoteq IQS269A")
Signed-off-by: Jeff LaBundy <jeff@labundy.com>
---
 drivers/input/misc/iqs269a.c | 118 +++++++++--------------------------
 1 file changed, 31 insertions(+), 87 deletions(-)

diff --git a/drivers/input/misc/iqs269a.c b/drivers/input/misc/iqs269a.c
index 0eb3cff177e5..eca680bf8c20 100644
--- a/drivers/input/misc/iqs269a.c
+++ b/drivers/input/misc/iqs269a.c
@@ -148,9 +148,6 @@
 #define IQS269_ATI_POLL_TIMEOUT_US		(iqs269->delay_mult * 500000)
 #define IQS269_ATI_STABLE_DELAY_MS		(iqs269->delay_mult * 150)
 
-#define IQS269_PWR_MODE_POLL_SLEEP_US		IQS269_ATI_POLL_SLEEP_US
-#define IQS269_PWR_MODE_POLL_TIMEOUT_US		IQS269_ATI_POLL_TIMEOUT_US
-
 #define iqs269_irq_wait()			usleep_range(200, 250)
 
 enum iqs269_local_cap_size {
@@ -295,7 +292,6 @@ struct iqs269_private {
 	struct input_dev *keypad;
 	struct input_dev *slider[IQS269_NUM_SL];
 	unsigned int keycode[ARRAY_SIZE(iqs269_events) * IQS269_NUM_CH];
-	unsigned int suspend_mode;
 	unsigned int delay_mult;
 	unsigned int ch_num;
 	bool hall_enable;
@@ -773,17 +769,6 @@ static int iqs269_parse_prop(struct iqs269_private *iqs269)
 	iqs269->hall_enable = device_property_present(&client->dev,
 						      "azoteq,hall-enable");
 
-	if (!device_property_read_u32(&client->dev, "azoteq,suspend-mode",
-				      &val)) {
-		if (val > IQS269_SYS_SETTINGS_PWR_MODE_MAX) {
-			dev_err(&client->dev, "Invalid suspend mode: %u\n",
-				val);
-			return -EINVAL;
-		}
-
-		iqs269->suspend_mode = val;
-	}
-
 	error = regmap_raw_read(iqs269->regmap, IQS269_SYS_SETTINGS, sys_reg,
 				sizeof(*sys_reg));
 	if (error)
@@ -1011,6 +996,17 @@ static int iqs269_parse_prop(struct iqs269_private *iqs269)
 	general &= ~IQS269_SYS_SETTINGS_DIS_AUTO;
 	general &= ~IQS269_SYS_SETTINGS_PWR_MODE_MASK;
 
+	if (!device_property_read_u32(&client->dev, "azoteq,suspend-mode",
+				      &val)) {
+		if (val > IQS269_SYS_SETTINGS_PWR_MODE_MAX) {
+			dev_err(&client->dev, "Invalid suspend mode: %u\n",
+				val);
+			return -EINVAL;
+		}
+
+		general |= (val << IQS269_SYS_SETTINGS_PWR_MODE_SHIFT);
+	}
+
 	if (!device_property_read_u32(&client->dev, "azoteq,ulp-update",
 				      &val)) {
 		if (val > IQS269_SYS_SETTINGS_ULP_UPDATE_MAX) {
@@ -1693,59 +1689,30 @@ static int iqs269_probe(struct i2c_client *client)
 	return error;
 }
 
+static u16 iqs269_general_get(struct iqs269_private *iqs269)
+{
+	u16 general = be16_to_cpu(iqs269->sys_reg.general);
+
+	general &= ~IQS269_SYS_SETTINGS_REDO_ATI;
+	general &= ~IQS269_SYS_SETTINGS_ACK_RESET;
+
+	return general | IQS269_SYS_SETTINGS_DIS_AUTO;
+}
+
 static int __maybe_unused iqs269_suspend(struct device *dev)
 {
 	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
 	struct i2c_client *client = iqs269->client;
-	unsigned int val;
 	int error;
+	u16 general = iqs269_general_get(iqs269);
 
-	if (!iqs269->suspend_mode)
+	if (!(general & IQS269_SYS_SETTINGS_PWR_MODE_MASK))
 		return 0;
 
 	disable_irq(client->irq);
 
-	/*
-	 * Automatic power mode switching must be disabled before the device is
-	 * forced into any particular power mode. In this case, the device will
-	 * transition into normal-power mode.
-	 */
-	error = regmap_update_bits(iqs269->regmap, IQS269_SYS_SETTINGS,
-				   IQS269_SYS_SETTINGS_DIS_AUTO, ~0);
-	if (error)
-		goto err_irq;
-
-	/*
-	 * The following check ensures the device has completed its transition
-	 * into normal-power mode before a manual mode switch is performed.
-	 */
-	error = regmap_read_poll_timeout(iqs269->regmap, IQS269_SYS_FLAGS, val,
-					!(val & IQS269_SYS_FLAGS_PWR_MODE_MASK),
-					 IQS269_PWR_MODE_POLL_SLEEP_US,
-					 IQS269_PWR_MODE_POLL_TIMEOUT_US);
-	if (error)
-		goto err_irq;
-
-	error = regmap_update_bits(iqs269->regmap, IQS269_SYS_SETTINGS,
-				   IQS269_SYS_SETTINGS_PWR_MODE_MASK,
-				   iqs269->suspend_mode <<
-				   IQS269_SYS_SETTINGS_PWR_MODE_SHIFT);
-	if (error)
-		goto err_irq;
-
-	/*
-	 * This last check ensures the device has completed its transition into
-	 * the desired power mode to prevent any spurious interrupts from being
-	 * triggered after iqs269_suspend has already returned.
-	 */
-	error = regmap_read_poll_timeout(iqs269->regmap, IQS269_SYS_FLAGS, val,
-					 (val & IQS269_SYS_FLAGS_PWR_MODE_MASK)
-					 == (iqs269->suspend_mode <<
-					     IQS269_SYS_FLAGS_PWR_MODE_SHIFT),
-					 IQS269_PWR_MODE_POLL_SLEEP_US,
-					 IQS269_PWR_MODE_POLL_TIMEOUT_US);
+	error = regmap_write(iqs269->regmap, IQS269_SYS_SETTINGS, general);
 
-err_irq:
 	iqs269_irq_wait();
 	enable_irq(client->irq);
 
@@ -1756,43 +1723,20 @@ static int __maybe_unused iqs269_resume(struct device *dev)
 {
 	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
 	struct i2c_client *client = iqs269->client;
-	unsigned int val;
 	int error;
+	u16 general = iqs269_general_get(iqs269);
 
-	if (!iqs269->suspend_mode)
+	if (!(general & IQS269_SYS_SETTINGS_PWR_MODE_MASK))
 		return 0;
 
 	disable_irq(client->irq);
 
-	error = regmap_update_bits(iqs269->regmap, IQS269_SYS_SETTINGS,
-				   IQS269_SYS_SETTINGS_PWR_MODE_MASK, 0);
-	if (error)
-		goto err_irq;
-
-	/*
-	 * This check ensures the device has returned to normal-power mode
-	 * before automatic power mode switching is re-enabled.
-	 */
-	error = regmap_read_poll_timeout(iqs269->regmap, IQS269_SYS_FLAGS, val,
-					!(val & IQS269_SYS_FLAGS_PWR_MODE_MASK),
-					 IQS269_PWR_MODE_POLL_SLEEP_US,
-					 IQS269_PWR_MODE_POLL_TIMEOUT_US);
-	if (error)
-		goto err_irq;
-
-	error = regmap_update_bits(iqs269->regmap, IQS269_SYS_SETTINGS,
-				   IQS269_SYS_SETTINGS_DIS_AUTO, 0);
-	if (error)
-		goto err_irq;
-
-	/*
-	 * This step reports any events that may have been "swallowed" as a
-	 * result of polling PWR_MODE (which automatically acknowledges any
-	 * pending interrupts).
-	 */
-	error = iqs269_report(iqs269);
+	error = regmap_write(iqs269->regmap, IQS269_SYS_SETTINGS,
+			     general & ~IQS269_SYS_SETTINGS_PWR_MODE_MASK);
+	if (!error)
+		error = regmap_write(iqs269->regmap, IQS269_SYS_SETTINGS,
+				     general & ~IQS269_SYS_SETTINGS_DIS_AUTO);
 
-err_irq:
 	iqs269_irq_wait();
 	enable_irq(client->irq);
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 5/5] Input: iqs269a - do not poll during ATI
  2022-11-29  3:00 [PATCH 0/5] Miscellaneous fixes for Azoteq IQS269A Jeff LaBundy
                   ` (3 preceding siblings ...)
  2022-11-29  3:02 ` [PATCH 4/5] Input: iqs269a - do not poll during suspend or resume Jeff LaBundy
@ 2022-11-29  3:03 ` Jeff LaBundy
  2022-12-15 10:22   ` Mattijs Korpershoek
  4 siblings, 1 reply; 11+ messages in thread
From: Jeff LaBundy @ 2022-11-29  3:03 UTC (permalink / raw)
  To: dmitry.torokhov; +Cc: linux-input, jeff

After initial start-up, the driver triggers ATI (calibration) with
the newly loaded register configuration in place. Next, the driver
polls a register field to ensure ATI completed in a timely fashion
and that the device is ready to sense.

However, communicating with the device over I2C while ATI is under-
way may induce noise in the device and cause ATI to fail. As such,
the vendor recommends not to poll the device during ATI.

To solve this problem, let the device naturally signal to the host
that ATI is complete by way of an interrupt. A completion prevents
the device from successfully probing until this happens.

As an added benefit, initial switch states are now reported in the
interrupt handler at the same time ATI status is checked. As such,
duplicate code that reports initial switch states has been removed
from iqs269_input_init().

The former logic that scaled ATI timeout and filter settling delay
is not carried forward with the new implementation, as it produces
overly conservative delays at the lower clock rate.

Rather, a single timeout that covers both clock rates is used. The
filter settling delay does not happen to be necessary and has been
removed as well.

Fixes: 04e49867fad1 ("Input: add support for Azoteq IQS269A")
Signed-off-by: Jeff LaBundy <jeff@labundy.com>
---
 drivers/input/misc/iqs269a.c | 97 +++++++++++++++++-------------------
 1 file changed, 46 insertions(+), 51 deletions(-)

diff --git a/drivers/input/misc/iqs269a.c b/drivers/input/misc/iqs269a.c
index eca680bf8c20..4e7b46d30052 100644
--- a/drivers/input/misc/iqs269a.c
+++ b/drivers/input/misc/iqs269a.c
@@ -9,6 +9,7 @@
  * axial sliders presented by the device.
  */
 
+#include <linux/completion.h>
 #include <linux/delay.h>
 #include <linux/device.h>
 #include <linux/err.h>
@@ -144,10 +145,6 @@
 #define IQS269_NUM_CH				8
 #define IQS269_NUM_SL				2
 
-#define IQS269_ATI_POLL_SLEEP_US		(iqs269->delay_mult * 10000)
-#define IQS269_ATI_POLL_TIMEOUT_US		(iqs269->delay_mult * 500000)
-#define IQS269_ATI_STABLE_DELAY_MS		(iqs269->delay_mult * 150)
-
 #define iqs269_irq_wait()			usleep_range(200, 250)
 
 enum iqs269_local_cap_size {
@@ -289,10 +286,10 @@ struct iqs269_private {
 	struct mutex lock;
 	struct iqs269_switch_desc switches[ARRAY_SIZE(iqs269_events)];
 	struct iqs269_sys_reg sys_reg;
+	struct completion ati_done;
 	struct input_dev *keypad;
 	struct input_dev *slider[IQS269_NUM_SL];
 	unsigned int keycode[ARRAY_SIZE(iqs269_events) * IQS269_NUM_CH];
-	unsigned int delay_mult;
 	unsigned int ch_num;
 	bool hall_enable;
 	bool ati_current;
@@ -979,13 +976,8 @@ static int iqs269_parse_prop(struct iqs269_private *iqs269)
 
 	general = be16_to_cpu(sys_reg->general);
 
-	if (device_property_present(&client->dev, "azoteq,clk-div")) {
+	if (device_property_present(&client->dev, "azoteq,clk-div"))
 		general |= IQS269_SYS_SETTINGS_CLK_DIV;
-		iqs269->delay_mult = 4;
-	} else {
-		general &= ~IQS269_SYS_SETTINGS_CLK_DIV;
-		iqs269->delay_mult = 1;
-	}
 
 	/*
 	 * Configure the device to automatically switch between normal and low-
@@ -1042,7 +1034,6 @@ static int iqs269_parse_prop(struct iqs269_private *iqs269)
 
 static int iqs269_dev_init(struct iqs269_private *iqs269)
 {
-	unsigned int val;
 	int error;
 
 	mutex_lock(&iqs269->lock);
@@ -1058,14 +1049,12 @@ static int iqs269_dev_init(struct iqs269_private *iqs269)
 	if (error)
 		goto err_mutex;
 
-	error = regmap_read_poll_timeout(iqs269->regmap, IQS269_SYS_FLAGS, val,
-					!(val & IQS269_SYS_FLAGS_IN_ATI),
-					 IQS269_ATI_POLL_SLEEP_US,
-					 IQS269_ATI_POLL_TIMEOUT_US);
-	if (error)
-		goto err_mutex;
+	/*
+	 * The following delay gives the device time to deassert its RDY output
+	 * so as to prevent an interrupt from being serviced prematurely.
+	 */
+	usleep_range(2000, 2100);
 
-	msleep(IQS269_ATI_STABLE_DELAY_MS);
 	iqs269->ati_current = true;
 
 err_mutex:
@@ -1077,10 +1066,8 @@ static int iqs269_dev_init(struct iqs269_private *iqs269)
 static int iqs269_input_init(struct iqs269_private *iqs269)
 {
 	struct i2c_client *client = iqs269->client;
-	struct iqs269_flags flags;
 	unsigned int sw_code, keycode;
 	int error, i, j;
-	u8 dir_mask, state;
 
 	iqs269->keypad = devm_input_allocate_device(&client->dev);
 	if (!iqs269->keypad)
@@ -1093,23 +1080,7 @@ static int iqs269_input_init(struct iqs269_private *iqs269)
 	iqs269->keypad->name = "iqs269a_keypad";
 	iqs269->keypad->id.bustype = BUS_I2C;
 
-	if (iqs269->hall_enable) {
-		error = regmap_raw_read(iqs269->regmap, IQS269_SYS_FLAGS,
-					&flags, sizeof(flags));
-		if (error) {
-			dev_err(&client->dev,
-				"Failed to read initial status: %d\n", error);
-			return error;
-		}
-	}
-
 	for (i = 0; i < ARRAY_SIZE(iqs269_events); i++) {
-		dir_mask = flags.states[IQS269_ST_OFFS_DIR];
-		if (!iqs269_events[i].dir_up)
-			dir_mask = ~dir_mask;
-
-		state = flags.states[iqs269_events[i].st_offs] & dir_mask;
-
 		sw_code = iqs269->switches[i].code;
 
 		for (j = 0; j < IQS269_NUM_CH; j++) {
@@ -1122,13 +1093,9 @@ static int iqs269_input_init(struct iqs269_private *iqs269)
 			switch (j) {
 			case IQS269_CHx_HALL_ACTIVE:
 				if (iqs269->hall_enable &&
-				    iqs269->switches[i].enabled) {
+				    iqs269->switches[i].enabled)
 					input_set_capability(iqs269->keypad,
 							     EV_SW, sw_code);
-					input_report_switch(iqs269->keypad,
-							    sw_code,
-							    state & BIT(j));
-				}
 				fallthrough;
 
 			case IQS269_CHx_HALL_INACTIVE:
@@ -1144,14 +1111,6 @@ static int iqs269_input_init(struct iqs269_private *iqs269)
 		}
 	}
 
-	input_sync(iqs269->keypad);
-
-	error = input_register_device(iqs269->keypad);
-	if (error) {
-		dev_err(&client->dev, "Failed to register keypad: %d\n", error);
-		return error;
-	}
-
 	for (i = 0; i < IQS269_NUM_SL; i++) {
 		if (!iqs269->sys_reg.slider_select[i])
 			continue;
@@ -1211,6 +1170,9 @@ static int iqs269_report(struct iqs269_private *iqs269)
 		return error;
 	}
 
+	if (be16_to_cpu(flags.system) & IQS269_SYS_FLAGS_IN_ATI)
+		return 0;
+
 	error = regmap_raw_read(iqs269->regmap, IQS269_SLIDER_X, slider_x,
 				sizeof(slider_x));
 	if (error) {
@@ -1273,6 +1235,12 @@ static int iqs269_report(struct iqs269_private *iqs269)
 
 	input_sync(iqs269->keypad);
 
+	/*
+	 * The following completion signals that ATI has finished, any initial
+	 * switch states have been reported and the keypad can be registered.
+	 */
+	complete_all(&iqs269->ati_done);
+
 	return 0;
 }
 
@@ -1304,6 +1272,9 @@ static ssize_t counts_show(struct device *dev,
 	if (!iqs269->ati_current || iqs269->hall_enable)
 		return -EPERM;
 
+	if (!completion_done(&iqs269->ati_done))
+		return -EBUSY;
+
 	/*
 	 * Unsolicited I2C communication prompts the device to assert its RDY
 	 * pin, so disable the interrupt line until the operation is finished
@@ -1560,7 +1531,9 @@ static ssize_t ati_trigger_show(struct device *dev,
 {
 	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
 
-	return scnprintf(buf, PAGE_SIZE, "%u\n", iqs269->ati_current);
+	return scnprintf(buf, PAGE_SIZE, "%u\n",
+			 iqs269->ati_current &&
+			 completion_done(&iqs269->ati_done));
 }
 
 static ssize_t ati_trigger_store(struct device *dev,
@@ -1580,6 +1553,7 @@ static ssize_t ati_trigger_store(struct device *dev,
 		return count;
 
 	disable_irq(client->irq);
+	reinit_completion(&iqs269->ati_done);
 
 	error = iqs269_dev_init(iqs269);
 
@@ -1589,6 +1563,10 @@ static ssize_t ati_trigger_store(struct device *dev,
 	if (error)
 		return error;
 
+	if (!wait_for_completion_timeout(&iqs269->ati_done,
+					 msecs_to_jiffies(2000)))
+		return -ETIMEDOUT;
+
 	return count;
 }
 
@@ -1647,6 +1625,7 @@ static int iqs269_probe(struct i2c_client *client)
 	}
 
 	mutex_init(&iqs269->lock);
+	init_completion(&iqs269->ati_done);
 
 	error = regmap_raw_read(iqs269->regmap, IQS269_VER_INFO, &ver_info,
 				sizeof(ver_info));
@@ -1682,6 +1661,22 @@ static int iqs269_probe(struct i2c_client *client)
 		return error;
 	}
 
+	if (!wait_for_completion_timeout(&iqs269->ati_done,
+					 msecs_to_jiffies(2000))) {
+		dev_err(&client->dev, "Failed to complete ATI\n");
+		return -ETIMEDOUT;
+	}
+
+	/*
+	 * The keypad may include one or more switches and is not registered
+	 * until ATI is complete and the initial switch states are read.
+	 */
+	error = input_register_device(iqs269->keypad);
+	if (error) {
+		dev_err(&client->dev, "Failed to register keypad: %d\n", error);
+		return error;
+	}
+
 	error = devm_device_add_group(&client->dev, &iqs269_attr_group);
 	if (error)
 		dev_err(&client->dev, "Failed to add attributes: %d\n", error);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH 1/5] Input: iqs269a - drop unused device node references
  2022-11-29  3:02 ` [PATCH 1/5] Input: iqs269a - drop unused device node references Jeff LaBundy
@ 2022-12-15  9:31   ` Mattijs Korpershoek
  0 siblings, 0 replies; 11+ messages in thread
From: Mattijs Korpershoek @ 2022-12-15  9:31 UTC (permalink / raw)
  To: Jeff LaBundy, dmitry.torokhov; +Cc: linux-input, jeff

On Mon, Nov 28, 2022 at 21:02, Jeff LaBundy <jeff@labundy.com> wrote:

> Each call to device/fwnode_get_named_child_node() must be matched
> with a call to fwnode_handle_put() once the corresponding node is
> no longer in use. This ensures a reference count remains balanced
> in the case of dynamic device tree support.
>
> Currently, the driver does not call fwnode_handle_put() on nested
> event nodes. This patch solves this problem by adding the missing
> instances of fwnode_handle_put().
>
> As part of this change, the logic which parses each channel's key
> code is gently refactored in order to reduce the number of places
> from which fwnode_handle_put() is called.
>
> Fixes: 04e49867fad1 ("Input: add support for Azoteq IQS269A")
> Signed-off-by: Jeff LaBundy <jeff@labundy.com>

Reviewed-by: Mattijs Korpershoek <mkorpershoek@baylibre.com>

> ---
>  drivers/input/misc/iqs269a.c | 24 ++++++++++++++++++++----
>  1 file changed, 20 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/input/misc/iqs269a.c b/drivers/input/misc/iqs269a.c
> index a348247d3d38..5620a009bf55 100644
> --- a/drivers/input/misc/iqs269a.c
> +++ b/drivers/input/misc/iqs269a.c
> @@ -694,7 +694,8 @@ static int iqs269_parse_chan(struct iqs269_private *iqs269,
>  				dev_err(&client->dev,
>  					"Invalid channel %u threshold: %u\n",
>  					reg, val);
> -				return -EINVAL;
> +				error = -EINVAL;
> +				break;
>  			}
>  
>  			ch_reg->thresh[iqs269_events[i].th_offs] = val;
> @@ -707,7 +708,8 @@ static int iqs269_parse_chan(struct iqs269_private *iqs269,
>  				dev_err(&client->dev,
>  					"Invalid channel %u hysteresis: %u\n",
>  					reg, val);
> -				return -EINVAL;
> +				error = -EINVAL;
> +				break;
>  			}
>  
>  			if (i == IQS269_EVENT_DEEP_DN ||
> @@ -721,8 +723,19 @@ static int iqs269_parse_chan(struct iqs269_private *iqs269,
>  			}
>  		}
>  
> -		if (fwnode_property_read_u32(ev_node, "linux,code", &val))
> +		error = fwnode_property_read_u32(ev_node, "linux,code", &val);
> +		if (error && error != -EINVAL) {
> +			dev_err(&client->dev,
> +				"Failed to read channel %u code: %d\n", reg,
> +				error);
> +			break;
> +		}
> +
> +		fwnode_handle_put(ev_node);
> +		if (error) {
> +			error = 0;
>  			continue;
> +		}
>  
>  		switch (reg) {
>  		case IQS269_CHx_HALL_ACTIVE:
> @@ -744,7 +757,10 @@ static int iqs269_parse_chan(struct iqs269_private *iqs269,
>  		iqs269->sys_reg.event_mask &= ~iqs269_events[i].mask;
>  	}
>  
> -	return 0;
> +	if (error)
> +		fwnode_handle_put(ev_node);
> +
> +	return error;
>  }
>  
>  static int iqs269_parse_prop(struct iqs269_private *iqs269)
> -- 
> 2.34.1

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 2/5] Input: iqs269a - increase interrupt handler return delay
  2022-11-29  3:02 ` [PATCH 2/5] Input: iqs269a - increase interrupt handler return delay Jeff LaBundy
@ 2022-12-15  9:32   ` Mattijs Korpershoek
  0 siblings, 0 replies; 11+ messages in thread
From: Mattijs Korpershoek @ 2022-12-15  9:32 UTC (permalink / raw)
  To: Jeff LaBundy, dmitry.torokhov; +Cc: linux-input, jeff

On Mon, Nov 28, 2022 at 21:02, Jeff LaBundy <jeff@labundy.com> wrote:

> The time the device takes to deassert its RDY output following an
> I2C stop condition scales with the core clock frequency.
>
> To prevent level-triggered interrupts from being reasserted after
> the interrupt handler returns, increase the time before returning
> to account for the worst-case delay (~140 us) plus margin.
>
> Fixes: 04e49867fad1 ("Input: add support for Azoteq IQS269A")
> Signed-off-by: Jeff LaBundy <jeff@labundy.com>

Reviewed-by: Mattijs Korpershoek <mkorpershoek@baylibre.com>

> ---
>  drivers/input/misc/iqs269a.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/input/misc/iqs269a.c b/drivers/input/misc/iqs269a.c
> index 5620a009bf55..711e67db71a4 100644
> --- a/drivers/input/misc/iqs269a.c
> +++ b/drivers/input/misc/iqs269a.c
> @@ -153,7 +153,7 @@
>  #define IQS269_PWR_MODE_POLL_SLEEP_US		IQS269_ATI_POLL_SLEEP_US
>  #define IQS269_PWR_MODE_POLL_TIMEOUT_US		IQS269_ATI_POLL_TIMEOUT_US
>  
> -#define iqs269_irq_wait()			usleep_range(100, 150)
> +#define iqs269_irq_wait()			usleep_range(200, 250)
>  
>  enum iqs269_local_cap_size {
>  	IQS269_LOCAL_CAP_SIZE_0,
> -- 
> 2.34.1

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 3/5] Input: iqs269a - configure device with a single block write
  2022-11-29  3:02 ` [PATCH 3/5] Input: iqs269a - configure device with a single block write Jeff LaBundy
@ 2022-12-15  9:55   ` Mattijs Korpershoek
  0 siblings, 0 replies; 11+ messages in thread
From: Mattijs Korpershoek @ 2022-12-15  9:55 UTC (permalink / raw)
  To: Jeff LaBundy, dmitry.torokhov; +Cc: linux-input, jeff

On Mon, Nov 28, 2022 at 21:02, Jeff LaBundy <jeff@labundy.com> wrote:

> Unless it is being done as part of servicing a soft reset interrupt,
> configuring channels on-the-fly (as is the case when writing to the
> ati_trigger attribute) may cause GPIO3 (which reflects the state of
> touch for a selected channel) to be inadvertently asserted.
>
> To solve this problem, follow the vendor's recommendation and write
> all channel configuration as well as the REDO_ATI register field as
> part of a single block write. This ensures the device has been told
> to re-calibrate itself following an I2C stop condition, after which
> sensing resumes and GPIO3 may be asserted.
>
> Fixes: 04e49867fad1 ("Input: add support for Azoteq IQS269A")
> Signed-off-by: Jeff LaBundy <jeff@labundy.com>

Reviewed-by: Mattijs Korpershoek <mkorpershoek@baylibre.com>

> ---
>  drivers/input/misc/iqs269a.c | 98 ++++++++++++++----------------------
>  1 file changed, 39 insertions(+), 59 deletions(-)
>
> diff --git a/drivers/input/misc/iqs269a.c b/drivers/input/misc/iqs269a.c
> index 711e67db71a4..0eb3cff177e5 100644
> --- a/drivers/input/misc/iqs269a.c
> +++ b/drivers/input/misc/iqs269a.c
> @@ -96,8 +96,6 @@
>  #define IQS269_MISC_B_TRACKING_UI_ENABLE	BIT(4)
>  #define IQS269_MISC_B_FILT_STR_SLIDER		GENMASK(1, 0)
>  
> -#define IQS269_CHx_SETTINGS			0x8C
> -
>  #define IQS269_CHx_ENG_A_MEAS_CAP_SIZE		BIT(15)
>  #define IQS269_CHx_ENG_A_RX_GND_INACTIVE	BIT(13)
>  #define IQS269_CHx_ENG_A_LOCAL_CAP_SIZE		BIT(12)
> @@ -245,6 +243,18 @@ struct iqs269_ver_info {
>  	u8 padding;
>  } __packed;
>  
> +struct iqs269_ch_reg {
> +	u8 rx_enable;
> +	u8 tx_enable;
> +	__be16 engine_a;
> +	__be16 engine_b;
> +	__be16 ati_comp;
> +	u8 thresh[3];
> +	u8 hyst;
> +	u8 assoc_select;
> +	u8 assoc_weight;
> +} __packed;
> +
>  struct iqs269_sys_reg {
>  	__be16 general;
>  	u8 active;
> @@ -266,18 +276,7 @@ struct iqs269_sys_reg {
>  	u8 timeout_swipe;
>  	u8 thresh_swipe;
>  	u8 redo_ati;
> -} __packed;
> -
> -struct iqs269_ch_reg {
> -	u8 rx_enable;
> -	u8 tx_enable;
> -	__be16 engine_a;
> -	__be16 engine_b;
> -	__be16 ati_comp;
> -	u8 thresh[3];
> -	u8 hyst;
> -	u8 assoc_select;
> -	u8 assoc_weight;
> +	struct iqs269_ch_reg ch_reg[IQS269_NUM_CH];
>  } __packed;
>  
>  struct iqs269_flags {
> @@ -292,7 +291,6 @@ struct iqs269_private {
>  	struct regmap *regmap;
>  	struct mutex lock;
>  	struct iqs269_switch_desc switches[ARRAY_SIZE(iqs269_events)];
> -	struct iqs269_ch_reg ch_reg[IQS269_NUM_CH];
>  	struct iqs269_sys_reg sys_reg;
>  	struct input_dev *keypad;
>  	struct input_dev *slider[IQS269_NUM_SL];
> @@ -307,6 +305,7 @@ struct iqs269_private {
>  static int iqs269_ati_mode_set(struct iqs269_private *iqs269,
>  			       unsigned int ch_num, unsigned int mode)
>  {
> +	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
>  	u16 engine_a;
>  
>  	if (ch_num >= IQS269_NUM_CH)
> @@ -317,12 +316,12 @@ static int iqs269_ati_mode_set(struct iqs269_private *iqs269,
>  
>  	mutex_lock(&iqs269->lock);
>  
> -	engine_a = be16_to_cpu(iqs269->ch_reg[ch_num].engine_a);
> +	engine_a = be16_to_cpu(ch_reg[ch_num].engine_a);
>  
>  	engine_a &= ~IQS269_CHx_ENG_A_ATI_MODE_MASK;
>  	engine_a |= (mode << IQS269_CHx_ENG_A_ATI_MODE_SHIFT);
>  
> -	iqs269->ch_reg[ch_num].engine_a = cpu_to_be16(engine_a);
> +	ch_reg[ch_num].engine_a = cpu_to_be16(engine_a);
>  	iqs269->ati_current = false;
>  
>  	mutex_unlock(&iqs269->lock);
> @@ -333,13 +332,14 @@ static int iqs269_ati_mode_set(struct iqs269_private *iqs269,
>  static int iqs269_ati_mode_get(struct iqs269_private *iqs269,
>  			       unsigned int ch_num, unsigned int *mode)
>  {
> +	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
>  	u16 engine_a;
>  
>  	if (ch_num >= IQS269_NUM_CH)
>  		return -EINVAL;
>  
>  	mutex_lock(&iqs269->lock);
> -	engine_a = be16_to_cpu(iqs269->ch_reg[ch_num].engine_a);
> +	engine_a = be16_to_cpu(ch_reg[ch_num].engine_a);
>  	mutex_unlock(&iqs269->lock);
>  
>  	engine_a &= IQS269_CHx_ENG_A_ATI_MODE_MASK;
> @@ -351,6 +351,7 @@ static int iqs269_ati_mode_get(struct iqs269_private *iqs269,
>  static int iqs269_ati_base_set(struct iqs269_private *iqs269,
>  			       unsigned int ch_num, unsigned int base)
>  {
> +	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
>  	u16 engine_b;
>  
>  	if (ch_num >= IQS269_NUM_CH)
> @@ -379,12 +380,12 @@ static int iqs269_ati_base_set(struct iqs269_private *iqs269,
>  
>  	mutex_lock(&iqs269->lock);
>  
> -	engine_b = be16_to_cpu(iqs269->ch_reg[ch_num].engine_b);
> +	engine_b = be16_to_cpu(ch_reg[ch_num].engine_b);
>  
>  	engine_b &= ~IQS269_CHx_ENG_B_ATI_BASE_MASK;
>  	engine_b |= base;
>  
> -	iqs269->ch_reg[ch_num].engine_b = cpu_to_be16(engine_b);
> +	ch_reg[ch_num].engine_b = cpu_to_be16(engine_b);
>  	iqs269->ati_current = false;
>  
>  	mutex_unlock(&iqs269->lock);
> @@ -395,13 +396,14 @@ static int iqs269_ati_base_set(struct iqs269_private *iqs269,
>  static int iqs269_ati_base_get(struct iqs269_private *iqs269,
>  			       unsigned int ch_num, unsigned int *base)
>  {
> +	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
>  	u16 engine_b;
>  
>  	if (ch_num >= IQS269_NUM_CH)
>  		return -EINVAL;
>  
>  	mutex_lock(&iqs269->lock);
> -	engine_b = be16_to_cpu(iqs269->ch_reg[ch_num].engine_b);
> +	engine_b = be16_to_cpu(ch_reg[ch_num].engine_b);
>  	mutex_unlock(&iqs269->lock);
>  
>  	switch (engine_b & IQS269_CHx_ENG_B_ATI_BASE_MASK) {
> @@ -429,6 +431,7 @@ static int iqs269_ati_base_get(struct iqs269_private *iqs269,
>  static int iqs269_ati_target_set(struct iqs269_private *iqs269,
>  				 unsigned int ch_num, unsigned int target)
>  {
> +	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
>  	u16 engine_b;
>  
>  	if (ch_num >= IQS269_NUM_CH)
> @@ -439,12 +442,12 @@ static int iqs269_ati_target_set(struct iqs269_private *iqs269,
>  
>  	mutex_lock(&iqs269->lock);
>  
> -	engine_b = be16_to_cpu(iqs269->ch_reg[ch_num].engine_b);
> +	engine_b = be16_to_cpu(ch_reg[ch_num].engine_b);
>  
>  	engine_b &= ~IQS269_CHx_ENG_B_ATI_TARGET_MASK;
>  	engine_b |= target / 32;
>  
> -	iqs269->ch_reg[ch_num].engine_b = cpu_to_be16(engine_b);
> +	ch_reg[ch_num].engine_b = cpu_to_be16(engine_b);
>  	iqs269->ati_current = false;
>  
>  	mutex_unlock(&iqs269->lock);
> @@ -455,13 +458,14 @@ static int iqs269_ati_target_set(struct iqs269_private *iqs269,
>  static int iqs269_ati_target_get(struct iqs269_private *iqs269,
>  				 unsigned int ch_num, unsigned int *target)
>  {
> +	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
>  	u16 engine_b;
>  
>  	if (ch_num >= IQS269_NUM_CH)
>  		return -EINVAL;
>  
>  	mutex_lock(&iqs269->lock);
> -	engine_b = be16_to_cpu(iqs269->ch_reg[ch_num].engine_b);
> +	engine_b = be16_to_cpu(ch_reg[ch_num].engine_b);
>  	mutex_unlock(&iqs269->lock);
>  
>  	*target = (engine_b & IQS269_CHx_ENG_B_ATI_TARGET_MASK) * 32;
> @@ -531,13 +535,7 @@ static int iqs269_parse_chan(struct iqs269_private *iqs269,
>  	if (fwnode_property_present(ch_node, "azoteq,slider1-select"))
>  		iqs269->sys_reg.slider_select[1] |= BIT(reg);
>  
> -	ch_reg = &iqs269->ch_reg[reg];
> -
> -	error = regmap_raw_read(iqs269->regmap,
> -				IQS269_CHx_SETTINGS + reg * sizeof(*ch_reg) / 2,
> -				ch_reg, sizeof(*ch_reg));
> -	if (error)
> -		return error;
> +	ch_reg = &iqs269->sys_reg.ch_reg[reg];
>  
>  	error = iqs269_parse_mask(ch_node, "azoteq,rx-enable",
>  				  &ch_reg->rx_enable);
> @@ -1048,10 +1046,8 @@ static int iqs269_parse_prop(struct iqs269_private *iqs269)
>  
>  static int iqs269_dev_init(struct iqs269_private *iqs269)
>  {
> -	struct iqs269_sys_reg *sys_reg = &iqs269->sys_reg;
> -	struct iqs269_ch_reg *ch_reg;
>  	unsigned int val;
> -	int error, i;
> +	int error;
>  
>  	mutex_lock(&iqs269->lock);
>  
> @@ -1061,27 +1057,8 @@ static int iqs269_dev_init(struct iqs269_private *iqs269)
>  	if (error)
>  		goto err_mutex;
>  
> -	for (i = 0; i < IQS269_NUM_CH; i++) {
> -		if (!(sys_reg->active & BIT(i)))
> -			continue;
> -
> -		ch_reg = &iqs269->ch_reg[i];
> -
> -		error = regmap_raw_write(iqs269->regmap,
> -					 IQS269_CHx_SETTINGS + i *
> -					 sizeof(*ch_reg) / 2, ch_reg,
> -					 sizeof(*ch_reg));
> -		if (error)
> -			goto err_mutex;
> -	}
> -
> -	/*
> -	 * The REDO-ATI and ATI channel selection fields must be written in the
> -	 * same block write, so every field between registers 0x80 through 0x8B
> -	 * (inclusive) must be written as well.
> -	 */
> -	error = regmap_raw_write(iqs269->regmap, IQS269_SYS_SETTINGS, sys_reg,
> -				 sizeof(*sys_reg));
> +	error = regmap_raw_write(iqs269->regmap, IQS269_SYS_SETTINGS,
> +				 &iqs269->sys_reg, sizeof(iqs269->sys_reg));
>  	if (error)
>  		goto err_mutex;
>  
> @@ -1355,6 +1332,7 @@ static ssize_t hall_bin_show(struct device *dev,
>  			     struct device_attribute *attr, char *buf)
>  {
>  	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
> +	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
>  	struct i2c_client *client = iqs269->client;
>  	unsigned int val;
>  	int error;
> @@ -1369,8 +1347,8 @@ static ssize_t hall_bin_show(struct device *dev,
>  	if (error)
>  		return error;
>  
> -	switch (iqs269->ch_reg[IQS269_CHx_HALL_ACTIVE].rx_enable &
> -		iqs269->ch_reg[IQS269_CHx_HALL_INACTIVE].rx_enable) {
> +	switch (ch_reg[IQS269_CHx_HALL_ACTIVE].rx_enable &
> +		ch_reg[IQS269_CHx_HALL_INACTIVE].rx_enable) {
>  	case IQS269_HALL_PAD_R:
>  		val &= IQS269_CAL_DATA_A_HALL_BIN_R_MASK;
>  		val >>= IQS269_CAL_DATA_A_HALL_BIN_R_SHIFT;
> @@ -1450,9 +1428,10 @@ static ssize_t rx_enable_show(struct device *dev,
>  			      struct device_attribute *attr, char *buf)
>  {
>  	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
> +	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
>  
>  	return scnprintf(buf, PAGE_SIZE, "%u\n",
> -			 iqs269->ch_reg[iqs269->ch_num].rx_enable);
> +			 ch_reg[iqs269->ch_num].rx_enable);
>  }
>  
>  static ssize_t rx_enable_store(struct device *dev,
> @@ -1460,6 +1439,7 @@ static ssize_t rx_enable_store(struct device *dev,
>  			       size_t count)
>  {
>  	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
> +	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
>  	unsigned int val;
>  	int error;
>  
> @@ -1472,7 +1452,7 @@ static ssize_t rx_enable_store(struct device *dev,
>  
>  	mutex_lock(&iqs269->lock);
>  
> -	iqs269->ch_reg[iqs269->ch_num].rx_enable = val;
> +	ch_reg[iqs269->ch_num].rx_enable = val;
>  	iqs269->ati_current = false;
>  
>  	mutex_unlock(&iqs269->lock);
> -- 
> 2.34.1

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 4/5] Input: iqs269a - do not poll during suspend or resume
  2022-11-29  3:02 ` [PATCH 4/5] Input: iqs269a - do not poll during suspend or resume Jeff LaBundy
@ 2022-12-15 10:07   ` Mattijs Korpershoek
  0 siblings, 0 replies; 11+ messages in thread
From: Mattijs Korpershoek @ 2022-12-15 10:07 UTC (permalink / raw)
  To: Jeff LaBundy, dmitry.torokhov; +Cc: linux-input, jeff

On Mon, Nov 28, 2022 at 21:02, Jeff LaBundy <jeff@labundy.com> wrote:

> Polling the device while it transitions from automatic to manual
> power mode switching may keep the device from actually finishing
> the transition. The process appears to time out depending on the
> polling rate and the device's core clock frequency.
>
> This is ultimately unnecessary in the first place; instead it is
> sufficient to write the desired mode during initialization, then
> disable automatic switching at suspend. This eliminates the need
> to ensure the device is prepared for a manual change and removes
> the 'suspend_mode' variable.
>
> Similarly, polling the device while it transitions from one mode
> to another under manual control may time out as well. This added
> step does not appear to be necessary either, so drop it.
>
> Fixes: 04e49867fad1 ("Input: add support for Azoteq IQS269A")
> Signed-off-by: Jeff LaBundy <jeff@labundy.com>

Reviewed-by: Mattijs Korpershoek <mkorpershoek@baylibre.com>

> ---
>  drivers/input/misc/iqs269a.c | 118 +++++++++--------------------------
>  1 file changed, 31 insertions(+), 87 deletions(-)
>
> diff --git a/drivers/input/misc/iqs269a.c b/drivers/input/misc/iqs269a.c
> index 0eb3cff177e5..eca680bf8c20 100644
> --- a/drivers/input/misc/iqs269a.c
> +++ b/drivers/input/misc/iqs269a.c
> @@ -148,9 +148,6 @@
>  #define IQS269_ATI_POLL_TIMEOUT_US		(iqs269->delay_mult * 500000)
>  #define IQS269_ATI_STABLE_DELAY_MS		(iqs269->delay_mult * 150)
>  
> -#define IQS269_PWR_MODE_POLL_SLEEP_US		IQS269_ATI_POLL_SLEEP_US
> -#define IQS269_PWR_MODE_POLL_TIMEOUT_US		IQS269_ATI_POLL_TIMEOUT_US
> -
>  #define iqs269_irq_wait()			usleep_range(200, 250)
>  
>  enum iqs269_local_cap_size {
> @@ -295,7 +292,6 @@ struct iqs269_private {
>  	struct input_dev *keypad;
>  	struct input_dev *slider[IQS269_NUM_SL];
>  	unsigned int keycode[ARRAY_SIZE(iqs269_events) * IQS269_NUM_CH];
> -	unsigned int suspend_mode;
>  	unsigned int delay_mult;
>  	unsigned int ch_num;
>  	bool hall_enable;
> @@ -773,17 +769,6 @@ static int iqs269_parse_prop(struct iqs269_private *iqs269)
>  	iqs269->hall_enable = device_property_present(&client->dev,
>  						      "azoteq,hall-enable");
>  
> -	if (!device_property_read_u32(&client->dev, "azoteq,suspend-mode",
> -				      &val)) {
> -		if (val > IQS269_SYS_SETTINGS_PWR_MODE_MAX) {
> -			dev_err(&client->dev, "Invalid suspend mode: %u\n",
> -				val);
> -			return -EINVAL;
> -		}
> -
> -		iqs269->suspend_mode = val;
> -	}
> -
>  	error = regmap_raw_read(iqs269->regmap, IQS269_SYS_SETTINGS, sys_reg,
>  				sizeof(*sys_reg));
>  	if (error)
> @@ -1011,6 +996,17 @@ static int iqs269_parse_prop(struct iqs269_private *iqs269)
>  	general &= ~IQS269_SYS_SETTINGS_DIS_AUTO;
>  	general &= ~IQS269_SYS_SETTINGS_PWR_MODE_MASK;
>  
> +	if (!device_property_read_u32(&client->dev, "azoteq,suspend-mode",
> +				      &val)) {
> +		if (val > IQS269_SYS_SETTINGS_PWR_MODE_MAX) {
> +			dev_err(&client->dev, "Invalid suspend mode: %u\n",
> +				val);
> +			return -EINVAL;
> +		}
> +
> +		general |= (val << IQS269_SYS_SETTINGS_PWR_MODE_SHIFT);
> +	}
> +
>  	if (!device_property_read_u32(&client->dev, "azoteq,ulp-update",
>  				      &val)) {
>  		if (val > IQS269_SYS_SETTINGS_ULP_UPDATE_MAX) {
> @@ -1693,59 +1689,30 @@ static int iqs269_probe(struct i2c_client *client)
>  	return error;
>  }
>  
> +static u16 iqs269_general_get(struct iqs269_private *iqs269)
> +{
> +	u16 general = be16_to_cpu(iqs269->sys_reg.general);
> +
> +	general &= ~IQS269_SYS_SETTINGS_REDO_ATI;
> +	general &= ~IQS269_SYS_SETTINGS_ACK_RESET;
> +
> +	return general | IQS269_SYS_SETTINGS_DIS_AUTO;
> +}
> +
>  static int __maybe_unused iqs269_suspend(struct device *dev)
>  {
>  	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
>  	struct i2c_client *client = iqs269->client;
> -	unsigned int val;
>  	int error;
> +	u16 general = iqs269_general_get(iqs269);
>  
> -	if (!iqs269->suspend_mode)
> +	if (!(general & IQS269_SYS_SETTINGS_PWR_MODE_MASK))
>  		return 0;
>  
>  	disable_irq(client->irq);
>  
> -	/*
> -	 * Automatic power mode switching must be disabled before the device is
> -	 * forced into any particular power mode. In this case, the device will
> -	 * transition into normal-power mode.
> -	 */
> -	error = regmap_update_bits(iqs269->regmap, IQS269_SYS_SETTINGS,
> -				   IQS269_SYS_SETTINGS_DIS_AUTO, ~0);
> -	if (error)
> -		goto err_irq;
> -
> -	/*
> -	 * The following check ensures the device has completed its transition
> -	 * into normal-power mode before a manual mode switch is performed.
> -	 */
> -	error = regmap_read_poll_timeout(iqs269->regmap, IQS269_SYS_FLAGS, val,
> -					!(val & IQS269_SYS_FLAGS_PWR_MODE_MASK),
> -					 IQS269_PWR_MODE_POLL_SLEEP_US,
> -					 IQS269_PWR_MODE_POLL_TIMEOUT_US);
> -	if (error)
> -		goto err_irq;
> -
> -	error = regmap_update_bits(iqs269->regmap, IQS269_SYS_SETTINGS,
> -				   IQS269_SYS_SETTINGS_PWR_MODE_MASK,
> -				   iqs269->suspend_mode <<
> -				   IQS269_SYS_SETTINGS_PWR_MODE_SHIFT);
> -	if (error)
> -		goto err_irq;
> -
> -	/*
> -	 * This last check ensures the device has completed its transition into
> -	 * the desired power mode to prevent any spurious interrupts from being
> -	 * triggered after iqs269_suspend has already returned.
> -	 */
> -	error = regmap_read_poll_timeout(iqs269->regmap, IQS269_SYS_FLAGS, val,
> -					 (val & IQS269_SYS_FLAGS_PWR_MODE_MASK)
> -					 == (iqs269->suspend_mode <<
> -					     IQS269_SYS_FLAGS_PWR_MODE_SHIFT),
> -					 IQS269_PWR_MODE_POLL_SLEEP_US,
> -					 IQS269_PWR_MODE_POLL_TIMEOUT_US);
> +	error = regmap_write(iqs269->regmap, IQS269_SYS_SETTINGS, general);
>  
> -err_irq:
>  	iqs269_irq_wait();
>  	enable_irq(client->irq);
>  
> @@ -1756,43 +1723,20 @@ static int __maybe_unused iqs269_resume(struct device *dev)
>  {
>  	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
>  	struct i2c_client *client = iqs269->client;
> -	unsigned int val;
>  	int error;
> +	u16 general = iqs269_general_get(iqs269);
>  
> -	if (!iqs269->suspend_mode)
> +	if (!(general & IQS269_SYS_SETTINGS_PWR_MODE_MASK))
>  		return 0;
>  
>  	disable_irq(client->irq);
>  
> -	error = regmap_update_bits(iqs269->regmap, IQS269_SYS_SETTINGS,
> -				   IQS269_SYS_SETTINGS_PWR_MODE_MASK, 0);
> -	if (error)
> -		goto err_irq;
> -
> -	/*
> -	 * This check ensures the device has returned to normal-power mode
> -	 * before automatic power mode switching is re-enabled.
> -	 */
> -	error = regmap_read_poll_timeout(iqs269->regmap, IQS269_SYS_FLAGS, val,
> -					!(val & IQS269_SYS_FLAGS_PWR_MODE_MASK),
> -					 IQS269_PWR_MODE_POLL_SLEEP_US,
> -					 IQS269_PWR_MODE_POLL_TIMEOUT_US);
> -	if (error)
> -		goto err_irq;
> -
> -	error = regmap_update_bits(iqs269->regmap, IQS269_SYS_SETTINGS,
> -				   IQS269_SYS_SETTINGS_DIS_AUTO, 0);
> -	if (error)
> -		goto err_irq;
> -
> -	/*
> -	 * This step reports any events that may have been "swallowed" as a
> -	 * result of polling PWR_MODE (which automatically acknowledges any
> -	 * pending interrupts).
> -	 */
> -	error = iqs269_report(iqs269);
> +	error = regmap_write(iqs269->regmap, IQS269_SYS_SETTINGS,
> +			     general & ~IQS269_SYS_SETTINGS_PWR_MODE_MASK);
> +	if (!error)
> +		error = regmap_write(iqs269->regmap, IQS269_SYS_SETTINGS,
> +				     general & ~IQS269_SYS_SETTINGS_DIS_AUTO);
>  
> -err_irq:
>  	iqs269_irq_wait();
>  	enable_irq(client->irq);
>  
> -- 
> 2.34.1

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 5/5] Input: iqs269a - do not poll during ATI
  2022-11-29  3:03 ` [PATCH 5/5] Input: iqs269a - do not poll during ATI Jeff LaBundy
@ 2022-12-15 10:22   ` Mattijs Korpershoek
  0 siblings, 0 replies; 11+ messages in thread
From: Mattijs Korpershoek @ 2022-12-15 10:22 UTC (permalink / raw)
  To: Jeff LaBundy, dmitry.torokhov; +Cc: linux-input, jeff

On Mon, Nov 28, 2022 at 21:03, Jeff LaBundy <jeff@labundy.com> wrote:

> After initial start-up, the driver triggers ATI (calibration) with
> the newly loaded register configuration in place. Next, the driver
> polls a register field to ensure ATI completed in a timely fashion
> and that the device is ready to sense.
>
> However, communicating with the device over I2C while ATI is under-
> way may induce noise in the device and cause ATI to fail. As such,
> the vendor recommends not to poll the device during ATI.
>
> To solve this problem, let the device naturally signal to the host
> that ATI is complete by way of an interrupt. A completion prevents
> the device from successfully probing until this happens.
>
> As an added benefit, initial switch states are now reported in the
> interrupt handler at the same time ATI status is checked. As such,
> duplicate code that reports initial switch states has been removed
> from iqs269_input_init().
>
> The former logic that scaled ATI timeout and filter settling delay
> is not carried forward with the new implementation, as it produces
> overly conservative delays at the lower clock rate.
>
> Rather, a single timeout that covers both clock rates is used. The
> filter settling delay does not happen to be necessary and has been
> removed as well.
>
> Fixes: 04e49867fad1 ("Input: add support for Azoteq IQS269A")
> Signed-off-by: Jeff LaBundy <jeff@labundy.com>

Reviewed-by: Mattijs Korpershoek <mkorpershoek@baylibre.com>

> ---
>  drivers/input/misc/iqs269a.c | 97 +++++++++++++++++-------------------
>  1 file changed, 46 insertions(+), 51 deletions(-)
>
> diff --git a/drivers/input/misc/iqs269a.c b/drivers/input/misc/iqs269a.c
> index eca680bf8c20..4e7b46d30052 100644
> --- a/drivers/input/misc/iqs269a.c
> +++ b/drivers/input/misc/iqs269a.c
> @@ -9,6 +9,7 @@
>   * axial sliders presented by the device.
>   */
>  
> +#include <linux/completion.h>
>  #include <linux/delay.h>
>  #include <linux/device.h>
>  #include <linux/err.h>
> @@ -144,10 +145,6 @@
>  #define IQS269_NUM_CH				8
>  #define IQS269_NUM_SL				2
>  
> -#define IQS269_ATI_POLL_SLEEP_US		(iqs269->delay_mult * 10000)
> -#define IQS269_ATI_POLL_TIMEOUT_US		(iqs269->delay_mult * 500000)
> -#define IQS269_ATI_STABLE_DELAY_MS		(iqs269->delay_mult * 150)
> -
>  #define iqs269_irq_wait()			usleep_range(200, 250)
>  
>  enum iqs269_local_cap_size {
> @@ -289,10 +286,10 @@ struct iqs269_private {
>  	struct mutex lock;
>  	struct iqs269_switch_desc switches[ARRAY_SIZE(iqs269_events)];
>  	struct iqs269_sys_reg sys_reg;
> +	struct completion ati_done;
>  	struct input_dev *keypad;
>  	struct input_dev *slider[IQS269_NUM_SL];
>  	unsigned int keycode[ARRAY_SIZE(iqs269_events) * IQS269_NUM_CH];
> -	unsigned int delay_mult;
>  	unsigned int ch_num;
>  	bool hall_enable;
>  	bool ati_current;
> @@ -979,13 +976,8 @@ static int iqs269_parse_prop(struct iqs269_private *iqs269)
>  
>  	general = be16_to_cpu(sys_reg->general);
>  
> -	if (device_property_present(&client->dev, "azoteq,clk-div")) {
> +	if (device_property_present(&client->dev, "azoteq,clk-div"))
>  		general |= IQS269_SYS_SETTINGS_CLK_DIV;
> -		iqs269->delay_mult = 4;
> -	} else {
> -		general &= ~IQS269_SYS_SETTINGS_CLK_DIV;
> -		iqs269->delay_mult = 1;
> -	}
>  
>  	/*
>  	 * Configure the device to automatically switch between normal and low-
> @@ -1042,7 +1034,6 @@ static int iqs269_parse_prop(struct iqs269_private *iqs269)
>  
>  static int iqs269_dev_init(struct iqs269_private *iqs269)
>  {
> -	unsigned int val;
>  	int error;
>  
>  	mutex_lock(&iqs269->lock);
> @@ -1058,14 +1049,12 @@ static int iqs269_dev_init(struct iqs269_private *iqs269)
>  	if (error)
>  		goto err_mutex;
>  
> -	error = regmap_read_poll_timeout(iqs269->regmap, IQS269_SYS_FLAGS, val,
> -					!(val & IQS269_SYS_FLAGS_IN_ATI),
> -					 IQS269_ATI_POLL_SLEEP_US,
> -					 IQS269_ATI_POLL_TIMEOUT_US);
> -	if (error)
> -		goto err_mutex;
> +	/*
> +	 * The following delay gives the device time to deassert its RDY output
> +	 * so as to prevent an interrupt from being serviced prematurely.
> +	 */
> +	usleep_range(2000, 2100);
>  
> -	msleep(IQS269_ATI_STABLE_DELAY_MS);
>  	iqs269->ati_current = true;
>  
>  err_mutex:
> @@ -1077,10 +1066,8 @@ static int iqs269_dev_init(struct iqs269_private *iqs269)
>  static int iqs269_input_init(struct iqs269_private *iqs269)
>  {
>  	struct i2c_client *client = iqs269->client;
> -	struct iqs269_flags flags;
>  	unsigned int sw_code, keycode;
>  	int error, i, j;
> -	u8 dir_mask, state;
>  
>  	iqs269->keypad = devm_input_allocate_device(&client->dev);
>  	if (!iqs269->keypad)
> @@ -1093,23 +1080,7 @@ static int iqs269_input_init(struct iqs269_private *iqs269)
>  	iqs269->keypad->name = "iqs269a_keypad";
>  	iqs269->keypad->id.bustype = BUS_I2C;
>  
> -	if (iqs269->hall_enable) {
> -		error = regmap_raw_read(iqs269->regmap, IQS269_SYS_FLAGS,
> -					&flags, sizeof(flags));
> -		if (error) {
> -			dev_err(&client->dev,
> -				"Failed to read initial status: %d\n", error);
> -			return error;
> -		}
> -	}
> -
>  	for (i = 0; i < ARRAY_SIZE(iqs269_events); i++) {
> -		dir_mask = flags.states[IQS269_ST_OFFS_DIR];
> -		if (!iqs269_events[i].dir_up)
> -			dir_mask = ~dir_mask;
> -
> -		state = flags.states[iqs269_events[i].st_offs] & dir_mask;
> -
>  		sw_code = iqs269->switches[i].code;
>  
>  		for (j = 0; j < IQS269_NUM_CH; j++) {
> @@ -1122,13 +1093,9 @@ static int iqs269_input_init(struct iqs269_private *iqs269)
>  			switch (j) {
>  			case IQS269_CHx_HALL_ACTIVE:
>  				if (iqs269->hall_enable &&
> -				    iqs269->switches[i].enabled) {
> +				    iqs269->switches[i].enabled)
>  					input_set_capability(iqs269->keypad,
>  							     EV_SW, sw_code);
> -					input_report_switch(iqs269->keypad,
> -							    sw_code,
> -							    state & BIT(j));
> -				}
>  				fallthrough;
>  
>  			case IQS269_CHx_HALL_INACTIVE:
> @@ -1144,14 +1111,6 @@ static int iqs269_input_init(struct iqs269_private *iqs269)
>  		}
>  	}
>  
> -	input_sync(iqs269->keypad);
> -
> -	error = input_register_device(iqs269->keypad);
> -	if (error) {
> -		dev_err(&client->dev, "Failed to register keypad: %d\n", error);
> -		return error;
> -	}
> -
>  	for (i = 0; i < IQS269_NUM_SL; i++) {
>  		if (!iqs269->sys_reg.slider_select[i])
>  			continue;
> @@ -1211,6 +1170,9 @@ static int iqs269_report(struct iqs269_private *iqs269)
>  		return error;
>  	}
>  
> +	if (be16_to_cpu(flags.system) & IQS269_SYS_FLAGS_IN_ATI)
> +		return 0;
> +
>  	error = regmap_raw_read(iqs269->regmap, IQS269_SLIDER_X, slider_x,
>  				sizeof(slider_x));
>  	if (error) {
> @@ -1273,6 +1235,12 @@ static int iqs269_report(struct iqs269_private *iqs269)
>  
>  	input_sync(iqs269->keypad);
>  
> +	/*
> +	 * The following completion signals that ATI has finished, any initial
> +	 * switch states have been reported and the keypad can be registered.
> +	 */
> +	complete_all(&iqs269->ati_done);
> +
>  	return 0;
>  }
>  
> @@ -1304,6 +1272,9 @@ static ssize_t counts_show(struct device *dev,
>  	if (!iqs269->ati_current || iqs269->hall_enable)
>  		return -EPERM;
>  
> +	if (!completion_done(&iqs269->ati_done))
> +		return -EBUSY;
> +
>  	/*
>  	 * Unsolicited I2C communication prompts the device to assert its RDY
>  	 * pin, so disable the interrupt line until the operation is finished
> @@ -1560,7 +1531,9 @@ static ssize_t ati_trigger_show(struct device *dev,
>  {
>  	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
>  
> -	return scnprintf(buf, PAGE_SIZE, "%u\n", iqs269->ati_current);
> +	return scnprintf(buf, PAGE_SIZE, "%u\n",
> +			 iqs269->ati_current &&
> +			 completion_done(&iqs269->ati_done));
>  }
>  
>  static ssize_t ati_trigger_store(struct device *dev,
> @@ -1580,6 +1553,7 @@ static ssize_t ati_trigger_store(struct device *dev,
>  		return count;
>  
>  	disable_irq(client->irq);
> +	reinit_completion(&iqs269->ati_done);
>  
>  	error = iqs269_dev_init(iqs269);
>  
> @@ -1589,6 +1563,10 @@ static ssize_t ati_trigger_store(struct device *dev,
>  	if (error)
>  		return error;
>  
> +	if (!wait_for_completion_timeout(&iqs269->ati_done,
> +					 msecs_to_jiffies(2000)))
> +		return -ETIMEDOUT;
> +
>  	return count;
>  }
>  
> @@ -1647,6 +1625,7 @@ static int iqs269_probe(struct i2c_client *client)
>  	}
>  
>  	mutex_init(&iqs269->lock);
> +	init_completion(&iqs269->ati_done);
>  
>  	error = regmap_raw_read(iqs269->regmap, IQS269_VER_INFO, &ver_info,
>  				sizeof(ver_info));
> @@ -1682,6 +1661,22 @@ static int iqs269_probe(struct i2c_client *client)
>  		return error;
>  	}
>  
> +	if (!wait_for_completion_timeout(&iqs269->ati_done,
> +					 msecs_to_jiffies(2000))) {
> +		dev_err(&client->dev, "Failed to complete ATI\n");
> +		return -ETIMEDOUT;
> +	}
> +
> +	/*
> +	 * The keypad may include one or more switches and is not registered
> +	 * until ATI is complete and the initial switch states are read.
> +	 */
> +	error = input_register_device(iqs269->keypad);
> +	if (error) {
> +		dev_err(&client->dev, "Failed to register keypad: %d\n", error);
> +		return error;
> +	}
> +
>  	error = devm_device_add_group(&client->dev, &iqs269_attr_group);
>  	if (error)
>  		dev_err(&client->dev, "Failed to add attributes: %d\n", error);
> -- 
> 2.34.1

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2022-12-15 10:23 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-29  3:00 [PATCH 0/5] Miscellaneous fixes for Azoteq IQS269A Jeff LaBundy
2022-11-29  3:02 ` [PATCH 1/5] Input: iqs269a - drop unused device node references Jeff LaBundy
2022-12-15  9:31   ` Mattijs Korpershoek
2022-11-29  3:02 ` [PATCH 2/5] Input: iqs269a - increase interrupt handler return delay Jeff LaBundy
2022-12-15  9:32   ` Mattijs Korpershoek
2022-11-29  3:02 ` [PATCH 3/5] Input: iqs269a - configure device with a single block write Jeff LaBundy
2022-12-15  9:55   ` Mattijs Korpershoek
2022-11-29  3:02 ` [PATCH 4/5] Input: iqs269a - do not poll during suspend or resume Jeff LaBundy
2022-12-15 10:07   ` Mattijs Korpershoek
2022-11-29  3:03 ` [PATCH 5/5] Input: iqs269a - do not poll during ATI Jeff LaBundy
2022-12-15 10:22   ` Mattijs Korpershoek

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.