Skip to content

Commit a96fb71

Browse files
committed
Input: matrix_keypad - avoid repeatedly converting GPIO to IRQ
There is no need to do conversion from GPIOs to interrupt numbers. Convert row GPIOs to interrupt numbers once in probe() and use this information when the driver needs to enable or disable given interrupt line. Link: https://lore.kernel.org/r/20240121053232.276968-1-dmitry.torokhov@gmail.com Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
1 parent 3aa182b commit a96fb71

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

drivers/input/keyboard/matrix_keypad.c

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ struct matrix_keypad {
2727
const struct matrix_keypad_platform_data *pdata;
2828
struct input_dev *input_dev;
2929
unsigned int row_shift;
30+
unsigned int row_irqs[MATRIX_MAX_ROWS];
3031

3132
DECLARE_BITMAP(disabled_gpios, MATRIX_MAX_ROWS);
3233

@@ -92,7 +93,7 @@ static void enable_row_irqs(struct matrix_keypad *keypad)
9293
enable_irq(pdata->clustered_irq);
9394
else {
9495
for (i = 0; i < pdata->num_row_gpios; i++)
95-
enable_irq(gpio_to_irq(pdata->row_gpios[i]));
96+
enable_irq(keypad->row_irqs[i]);
9697
}
9798
}
9899

@@ -105,7 +106,7 @@ static void disable_row_irqs(struct matrix_keypad *keypad)
105106
disable_irq_nosync(pdata->clustered_irq);
106107
else {
107108
for (i = 0; i < pdata->num_row_gpios; i++)
108-
disable_irq_nosync(gpio_to_irq(pdata->row_gpios[i]));
109+
disable_irq_nosync(keypad->row_irqs[i]);
109110
}
110111
}
111112

@@ -233,29 +234,23 @@ static void matrix_keypad_stop(struct input_dev *dev)
233234
static void matrix_keypad_enable_wakeup(struct matrix_keypad *keypad)
234235
{
235236
const struct matrix_keypad_platform_data *pdata = keypad->pdata;
236-
unsigned int gpio;
237237
int i;
238238

239239
if (pdata->clustered_irq > 0) {
240240
if (enable_irq_wake(pdata->clustered_irq) == 0)
241241
keypad->gpio_all_disabled = true;
242242
} else {
243243

244-
for (i = 0; i < pdata->num_row_gpios; i++) {
245-
if (!test_bit(i, keypad->disabled_gpios)) {
246-
gpio = pdata->row_gpios[i];
247-
248-
if (enable_irq_wake(gpio_to_irq(gpio)) == 0)
244+
for (i = 0; i < pdata->num_row_gpios; i++)
245+
if (!test_bit(i, keypad->disabled_gpios))
246+
if (enable_irq_wake(keypad->row_irqs[i]) == 0)
249247
__set_bit(i, keypad->disabled_gpios);
250-
}
251-
}
252248
}
253249
}
254250

255251
static void matrix_keypad_disable_wakeup(struct matrix_keypad *keypad)
256252
{
257253
const struct matrix_keypad_platform_data *pdata = keypad->pdata;
258-
unsigned int gpio;
259254
int i;
260255

261256
if (pdata->clustered_irq > 0) {
@@ -264,12 +259,9 @@ static void matrix_keypad_disable_wakeup(struct matrix_keypad *keypad)
264259
keypad->gpio_all_disabled = false;
265260
}
266261
} else {
267-
for (i = 0; i < pdata->num_row_gpios; i++) {
268-
if (test_and_clear_bit(i, keypad->disabled_gpios)) {
269-
gpio = pdata->row_gpios[i];
270-
disable_irq_wake(gpio_to_irq(gpio));
271-
}
272-
}
262+
for (i = 0; i < pdata->num_row_gpios; i++)
263+
if (test_and_clear_bit(i, keypad->disabled_gpios))
264+
disable_irq_wake(keypad->row_irqs[i]);
273265
}
274266
}
275267

@@ -306,7 +298,7 @@ static int matrix_keypad_init_gpio(struct platform_device *pdev,
306298
struct matrix_keypad *keypad)
307299
{
308300
const struct matrix_keypad_platform_data *pdata = keypad->pdata;
309-
int i, err;
301+
int i, irq, err;
310302

311303
/* initialized strobe lines as outputs, activated */
312304
for (i = 0; i < pdata->num_col_gpios; i++) {
@@ -345,18 +337,28 @@ static int matrix_keypad_init_gpio(struct platform_device *pdev,
345337
}
346338
} else {
347339
for (i = 0; i < pdata->num_row_gpios; i++) {
348-
err = request_any_context_irq(
349-
gpio_to_irq(pdata->row_gpios[i]),
340+
irq = gpio_to_irq(pdata->row_gpios[i]);
341+
if (irq < 0) {
342+
err = irq;
343+
dev_err(&pdev->dev,
344+
"Unable to convert GPIO line %i to irq: %d\n",
345+
pdata->row_gpios[i], err);
346+
goto err_free_irqs;
347+
}
348+
349+
err = request_any_context_irq(irq,
350350
matrix_keypad_interrupt,
351351
IRQF_TRIGGER_RISING |
352-
IRQF_TRIGGER_FALLING,
352+
IRQF_TRIGGER_FALLING,
353353
"matrix-keypad", keypad);
354354
if (err < 0) {
355355
dev_err(&pdev->dev,
356356
"Unable to acquire interrupt for GPIO line %i\n",
357357
pdata->row_gpios[i]);
358358
goto err_free_irqs;
359359
}
360+
361+
keypad->row_irqs[i] = irq;
360362
}
361363
}
362364

@@ -366,7 +368,7 @@ static int matrix_keypad_init_gpio(struct platform_device *pdev,
366368

367369
err_free_irqs:
368370
while (--i >= 0)
369-
free_irq(gpio_to_irq(pdata->row_gpios[i]), keypad);
371+
free_irq(keypad->row_irqs[i], keypad);
370372
i = pdata->num_row_gpios;
371373
err_free_rows:
372374
while (--i >= 0)
@@ -388,7 +390,7 @@ static void matrix_keypad_free_gpio(struct matrix_keypad *keypad)
388390
free_irq(pdata->clustered_irq, keypad);
389391
} else {
390392
for (i = 0; i < pdata->num_row_gpios; i++)
391-
free_irq(gpio_to_irq(pdata->row_gpios[i]), keypad);
393+
free_irq(keypad->row_irqs[i], keypad);
392394
}
393395

394396
for (i = 0; i < pdata->num_row_gpios; i++)

0 commit comments

Comments
 (0)