Skip to content

Commit b08134e

Browse files
jlabundydtor
authored andcommitted
Input: iqs269a - do not poll during ATI
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: 04e4986 ("Input: add support for Azoteq IQS269A") Signed-off-by: Jeff LaBundy <[email protected]> Reviewed-by: Mattijs Korpershoek <[email protected]> Link: https://lore.kernel.org/r/Y7RtB2T7AF9rYMjK@nixie71 Signed-off-by: Dmitry Torokhov <[email protected]>
1 parent 18ab69c commit b08134e

File tree

1 file changed

+46
-51
lines changed

1 file changed

+46
-51
lines changed

drivers/input/misc/iqs269a.c

Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* axial sliders presented by the device.
1010
*/
1111

12+
#include <linux/completion.h>
1213
#include <linux/delay.h>
1314
#include <linux/device.h>
1415
#include <linux/err.h>
@@ -144,10 +145,6 @@
144145
#define IQS269_NUM_CH 8
145146
#define IQS269_NUM_SL 2
146147

147-
#define IQS269_ATI_POLL_SLEEP_US (iqs269->delay_mult * 10000)
148-
#define IQS269_ATI_POLL_TIMEOUT_US (iqs269->delay_mult * 500000)
149-
#define IQS269_ATI_STABLE_DELAY_MS (iqs269->delay_mult * 150)
150-
151148
#define iqs269_irq_wait() usleep_range(200, 250)
152149

153150
enum iqs269_local_cap_size {
@@ -289,10 +286,10 @@ struct iqs269_private {
289286
struct mutex lock;
290287
struct iqs269_switch_desc switches[ARRAY_SIZE(iqs269_events)];
291288
struct iqs269_sys_reg sys_reg;
289+
struct completion ati_done;
292290
struct input_dev *keypad;
293291
struct input_dev *slider[IQS269_NUM_SL];
294292
unsigned int keycode[ARRAY_SIZE(iqs269_events) * IQS269_NUM_CH];
295-
unsigned int delay_mult;
296293
unsigned int ch_num;
297294
bool hall_enable;
298295
bool ati_current;
@@ -973,13 +970,8 @@ static int iqs269_parse_prop(struct iqs269_private *iqs269)
973970

974971
general = be16_to_cpu(sys_reg->general);
975972

976-
if (device_property_present(&client->dev, "azoteq,clk-div")) {
973+
if (device_property_present(&client->dev, "azoteq,clk-div"))
977974
general |= IQS269_SYS_SETTINGS_CLK_DIV;
978-
iqs269->delay_mult = 4;
979-
} else {
980-
general &= ~IQS269_SYS_SETTINGS_CLK_DIV;
981-
iqs269->delay_mult = 1;
982-
}
983975

984976
/*
985977
* Configure the device to automatically switch between normal and low-
@@ -1036,7 +1028,6 @@ static int iqs269_parse_prop(struct iqs269_private *iqs269)
10361028

10371029
static int iqs269_dev_init(struct iqs269_private *iqs269)
10381030
{
1039-
unsigned int val;
10401031
int error;
10411032

10421033
mutex_lock(&iqs269->lock);
@@ -1052,14 +1043,12 @@ static int iqs269_dev_init(struct iqs269_private *iqs269)
10521043
if (error)
10531044
goto err_mutex;
10541045

1055-
error = regmap_read_poll_timeout(iqs269->regmap, IQS269_SYS_FLAGS, val,
1056-
!(val & IQS269_SYS_FLAGS_IN_ATI),
1057-
IQS269_ATI_POLL_SLEEP_US,
1058-
IQS269_ATI_POLL_TIMEOUT_US);
1059-
if (error)
1060-
goto err_mutex;
1046+
/*
1047+
* The following delay gives the device time to deassert its RDY output
1048+
* so as to prevent an interrupt from being serviced prematurely.
1049+
*/
1050+
usleep_range(2000, 2100);
10611051

1062-
msleep(IQS269_ATI_STABLE_DELAY_MS);
10631052
iqs269->ati_current = true;
10641053

10651054
err_mutex:
@@ -1071,10 +1060,8 @@ static int iqs269_dev_init(struct iqs269_private *iqs269)
10711060
static int iqs269_input_init(struct iqs269_private *iqs269)
10721061
{
10731062
struct i2c_client *client = iqs269->client;
1074-
struct iqs269_flags flags;
10751063
unsigned int sw_code, keycode;
10761064
int error, i, j;
1077-
u8 dir_mask, state;
10781065

10791066
iqs269->keypad = devm_input_allocate_device(&client->dev);
10801067
if (!iqs269->keypad)
@@ -1087,23 +1074,7 @@ static int iqs269_input_init(struct iqs269_private *iqs269)
10871074
iqs269->keypad->name = "iqs269a_keypad";
10881075
iqs269->keypad->id.bustype = BUS_I2C;
10891076

1090-
if (iqs269->hall_enable) {
1091-
error = regmap_raw_read(iqs269->regmap, IQS269_SYS_FLAGS,
1092-
&flags, sizeof(flags));
1093-
if (error) {
1094-
dev_err(&client->dev,
1095-
"Failed to read initial status: %d\n", error);
1096-
return error;
1097-
}
1098-
}
1099-
11001077
for (i = 0; i < ARRAY_SIZE(iqs269_events); i++) {
1101-
dir_mask = flags.states[IQS269_ST_OFFS_DIR];
1102-
if (!iqs269_events[i].dir_up)
1103-
dir_mask = ~dir_mask;
1104-
1105-
state = flags.states[iqs269_events[i].st_offs] & dir_mask;
1106-
11071078
sw_code = iqs269->switches[i].code;
11081079

11091080
for (j = 0; j < IQS269_NUM_CH; j++) {
@@ -1116,13 +1087,9 @@ static int iqs269_input_init(struct iqs269_private *iqs269)
11161087
switch (j) {
11171088
case IQS269_CHx_HALL_ACTIVE:
11181089
if (iqs269->hall_enable &&
1119-
iqs269->switches[i].enabled) {
1090+
iqs269->switches[i].enabled)
11201091
input_set_capability(iqs269->keypad,
11211092
EV_SW, sw_code);
1122-
input_report_switch(iqs269->keypad,
1123-
sw_code,
1124-
state & BIT(j));
1125-
}
11261093
fallthrough;
11271094

11281095
case IQS269_CHx_HALL_INACTIVE:
@@ -1138,14 +1105,6 @@ static int iqs269_input_init(struct iqs269_private *iqs269)
11381105
}
11391106
}
11401107

1141-
input_sync(iqs269->keypad);
1142-
1143-
error = input_register_device(iqs269->keypad);
1144-
if (error) {
1145-
dev_err(&client->dev, "Failed to register keypad: %d\n", error);
1146-
return error;
1147-
}
1148-
11491108
for (i = 0; i < IQS269_NUM_SL; i++) {
11501109
if (!iqs269->sys_reg.slider_select[i])
11511110
continue;
@@ -1205,6 +1164,9 @@ static int iqs269_report(struct iqs269_private *iqs269)
12051164
return error;
12061165
}
12071166

1167+
if (be16_to_cpu(flags.system) & IQS269_SYS_FLAGS_IN_ATI)
1168+
return 0;
1169+
12081170
error = regmap_raw_read(iqs269->regmap, IQS269_SLIDER_X, slider_x,
12091171
sizeof(slider_x));
12101172
if (error) {
@@ -1267,6 +1229,12 @@ static int iqs269_report(struct iqs269_private *iqs269)
12671229

12681230
input_sync(iqs269->keypad);
12691231

1232+
/*
1233+
* The following completion signals that ATI has finished, any initial
1234+
* switch states have been reported and the keypad can be registered.
1235+
*/
1236+
complete_all(&iqs269->ati_done);
1237+
12701238
return 0;
12711239
}
12721240

@@ -1298,6 +1266,9 @@ static ssize_t counts_show(struct device *dev,
12981266
if (!iqs269->ati_current || iqs269->hall_enable)
12991267
return -EPERM;
13001268

1269+
if (!completion_done(&iqs269->ati_done))
1270+
return -EBUSY;
1271+
13011272
/*
13021273
* Unsolicited I2C communication prompts the device to assert its RDY
13031274
* pin, so disable the interrupt line until the operation is finished
@@ -1554,7 +1525,9 @@ static ssize_t ati_trigger_show(struct device *dev,
15541525
{
15551526
struct iqs269_private *iqs269 = dev_get_drvdata(dev);
15561527

1557-
return scnprintf(buf, PAGE_SIZE, "%u\n", iqs269->ati_current);
1528+
return scnprintf(buf, PAGE_SIZE, "%u\n",
1529+
iqs269->ati_current &&
1530+
completion_done(&iqs269->ati_done));
15581531
}
15591532

15601533
static ssize_t ati_trigger_store(struct device *dev,
@@ -1574,6 +1547,7 @@ static ssize_t ati_trigger_store(struct device *dev,
15741547
return count;
15751548

15761549
disable_irq(client->irq);
1550+
reinit_completion(&iqs269->ati_done);
15771551

15781552
error = iqs269_dev_init(iqs269);
15791553

@@ -1583,6 +1557,10 @@ static ssize_t ati_trigger_store(struct device *dev,
15831557
if (error)
15841558
return error;
15851559

1560+
if (!wait_for_completion_timeout(&iqs269->ati_done,
1561+
msecs_to_jiffies(2000)))
1562+
return -ETIMEDOUT;
1563+
15861564
return count;
15871565
}
15881566

@@ -1641,6 +1619,7 @@ static int iqs269_probe(struct i2c_client *client)
16411619
}
16421620

16431621
mutex_init(&iqs269->lock);
1622+
init_completion(&iqs269->ati_done);
16441623

16451624
error = regmap_raw_read(iqs269->regmap, IQS269_VER_INFO, &ver_info,
16461625
sizeof(ver_info));
@@ -1676,6 +1655,22 @@ static int iqs269_probe(struct i2c_client *client)
16761655
return error;
16771656
}
16781657

1658+
if (!wait_for_completion_timeout(&iqs269->ati_done,
1659+
msecs_to_jiffies(2000))) {
1660+
dev_err(&client->dev, "Failed to complete ATI\n");
1661+
return -ETIMEDOUT;
1662+
}
1663+
1664+
/*
1665+
* The keypad may include one or more switches and is not registered
1666+
* until ATI is complete and the initial switch states are read.
1667+
*/
1668+
error = input_register_device(iqs269->keypad);
1669+
if (error) {
1670+
dev_err(&client->dev, "Failed to register keypad: %d\n", error);
1671+
return error;
1672+
}
1673+
16791674
error = devm_device_add_group(&client->dev, &iqs269_attr_group);
16801675
if (error)
16811676
dev_err(&client->dev, "Failed to add attributes: %d\n", error);

0 commit comments

Comments
 (0)