Re: [PATCH V2] matrix_keypad: add support for clustered irq

From: Eric Miao
Date: Thu May 27 2010 - 11:15:27 EST


On Thu, May 27, 2010 at 2:43 PM, Luotao Fu <l.fu@xxxxxxxxxxxxxx> wrote:
> This one adds support of a combined irq source for the whole matrix keypad.
> This can be useful if all rows and columns of the keypad are e.g. connected
> to a GPIO expander, which only has one interrupt line for all events on
> every single GPIO.
>
> Signed-off-by: Luotao Fu <l.fu@xxxxxxxxxxxxxx>

I feel OK. Acked-by: Eric Miao <eric.y.miao@xxxxxxxxx>

> ---
> V2 Changes:
> * create separate functions for suspend/resume calls.
> * add bool flag to signal enable/disable state of all gpios.
> * add spinlock to suspend and resume callbacks.
>
> Âdrivers/input/keyboard/matrix_keypad.c | Â138 ++++++++++++++++++++++++--------
> Âinclude/linux/input/matrix_keypad.h  Â|  Â5 +
> Â2 files changed, 109 insertions(+), 34 deletions(-)
>
> diff --git a/drivers/input/keyboard/matrix_keypad.c b/drivers/input/keyboard/matrix_keypad.c
> index b443e08..99b7204 100644
> --- a/drivers/input/keyboard/matrix_keypad.c
> +++ b/drivers/input/keyboard/matrix_keypad.c
> @@ -37,6 +37,7 @@ struct matrix_keypad {
> Â Â Â Âspinlock_t lock;
> Â Â Â Âbool scan_pending;
> Â Â Â Âbool stopped;
> + Â Â Â bool gpio_all_disabled;
> Â};
>
> Â/*
> @@ -87,8 +88,12 @@ static void enable_row_irqs(struct matrix_keypad *keypad)
> Â Â Â Âconst struct matrix_keypad_platform_data *pdata = keypad->pdata;
> Â Â Â Âint i;
>
> - Â Â Â for (i = 0; i < pdata->num_row_gpios; i++)
> - Â Â Â Â Â Â Â enable_irq(gpio_to_irq(pdata->row_gpios[i]));
> + Â Â Â if (pdata->clustered_irq > 0)
> + Â Â Â Â Â Â Â enable_irq(pdata->clustered_irq);
> + Â Â Â else {
> + Â Â Â Â Â Â Â for (i = 0; i < pdata->num_row_gpios; i++)
> + Â Â Â Â Â Â Â Â Â Â Â enable_irq(gpio_to_irq(pdata->row_gpios[i]));
> + Â Â Â }
> Â}
>
> Âstatic void disable_row_irqs(struct matrix_keypad *keypad)
> @@ -96,8 +101,12 @@ static void disable_row_irqs(struct matrix_keypad *keypad)
> Â Â Â Âconst struct matrix_keypad_platform_data *pdata = keypad->pdata;
> Â Â Â Âint i;
>
> - Â Â Â for (i = 0; i < pdata->num_row_gpios; i++)
> - Â Â Â Â Â Â Â disable_irq_nosync(gpio_to_irq(pdata->row_gpios[i]));
> + Â Â Â if (pdata->clustered_irq > 0)
> + Â Â Â Â Â Â Â disable_irq_nosync(pdata->clustered_irq);
> + Â Â Â else {
> + Â Â Â Â Â Â Â for (i = 0; i < pdata->num_row_gpios; i++)
> + Â Â Â Â Â Â Â Â Â Â Â disable_irq_nosync(gpio_to_irq(pdata->row_gpios[i]));
> + Â Â Â }
> Â}
>
> Â/*
> @@ -216,25 +225,74 @@ static void matrix_keypad_stop(struct input_dev *dev)
> Â}
>
> Â#ifdef CONFIG_PM
> -static int matrix_keypad_suspend(struct device *dev)
> +static inline void __suspend_keypad(struct matrix_keypad *keypad)
> Â{
> - Â Â Â struct platform_device *pdev = to_platform_device(dev);
> - Â Â Â struct matrix_keypad *keypad = platform_get_drvdata(pdev);
> Â Â Â Âconst struct matrix_keypad_platform_data *pdata = keypad->pdata;
> + Â Â Â unsigned int cirq = pdata->clustered_irq;
> + Â Â Â unsigned int gpio;
> + Â Â Â unsigned long flags;
> Â Â Â Âint i;
>
> - Â Â Â matrix_keypad_stop(keypad->input_dev);
> + Â Â Â spin_lock_irqsave(&keypad->lock, flags);
>
> - Â Â Â if (device_may_wakeup(&pdev->dev)) {
> - Â Â Â Â Â Â Â for (i = 0; i < pdata->num_row_gpios; i++) {
> - Â Â Â Â Â Â Â Â Â Â Â if (!test_bit(i, keypad->disabled_gpios)) {
> - Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â unsigned int gpio = pdata->row_gpios[i];
> + Â Â Â if (pdata->clustered_irq > 0) {
> + Â Â Â Â Â Â Â if (enable_irq_wake(cirq) == 0)
> + Â Â Â Â Â Â Â Â Â Â Â keypad->gpio_all_disabled = true;
>
> - Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â if (enable_irq_wake(gpio_to_irq(gpio)) == 0)
> - Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â __set_bit(i, keypad->disabled_gpios);
> - Â Â Â Â Â Â Â Â Â Â Â }
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â }
> +
> + Â Â Â for (i = 0; i < pdata->num_row_gpios; i++) {
> + Â Â Â Â Â Â Â if (!test_bit(i, keypad->disabled_gpios)) {
> + Â Â Â Â Â Â Â Â Â Â Â gpio = pdata->row_gpios[i];
> +
> + Â Â Â Â Â Â Â Â Â Â Â if (enable_irq_wake(gpio_to_irq(gpio)) == 0)
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â __set_bit(i, keypad->disabled_gpios);
> + Â Â Â Â Â Â Â }
> + Â Â Â }
> +out:
> + Â Â Â spin_unlock_irqrestore(&keypad->lock, flags);
> +}
> +
> +static inline void __resume_keypad(struct matrix_keypad *keypad)
> +{
> + Â Â Â const struct matrix_keypad_platform_data *pdata = keypad->pdata;
> + Â Â Â unsigned int cirq = pdata->clustered_irq;
> + Â Â Â unsigned int gpio;
> + Â Â Â unsigned long flags;
> + Â Â Â int i;
> +
> + Â Â Â spin_lock_irqsave(&keypad->lock, flags);
> +
> + Â Â Â if (pdata->clustered_irq > 0) {
> + Â Â Â Â Â Â Â if (keypad->gpio_all_disabled) {
> + Â Â Â Â Â Â Â Â Â Â Â disable_irq_wake(cirq);
> + Â Â Â Â Â Â Â Â Â Â Â keypad->gpio_all_disabled = false;
> + Â Â Â Â Â Â Â }
> +
> + Â Â Â Â Â Â Â goto out;
> + Â Â Â }
> +
> + Â Â Â for (i = 0; i < pdata->num_row_gpios; i++) {
> + Â Â Â Â Â Â Â if (test_and_clear_bit(i, keypad->disabled_gpios)) {
> + Â Â Â Â Â Â Â Â Â Â Â gpio = pdata->row_gpios[i];
> + Â Â Â Â Â Â Â Â Â Â Â disable_irq_wake(gpio_to_irq(gpio));
> Â Â Â Â Â Â Â Â}
> Â Â Â Â}
> +out:
> + Â Â Â spin_unlock_irqrestore(&keypad->lock, flags);
> +
> +}
> +
> +static int matrix_keypad_suspend(struct device *dev)
> +{
> + Â Â Â struct platform_device *pdev = to_platform_device(dev);
> + Â Â Â struct matrix_keypad *keypad = platform_get_drvdata(pdev);
> +
> + Â Â Â matrix_keypad_stop(keypad->input_dev);
> +
> + Â Â Â if (device_may_wakeup(&pdev->dev))
> + Â Â Â Â Â Â Â __suspend_keypad(keypad);
>
> Â Â Â Âreturn 0;
> Â}
> @@ -246,15 +304,8 @@ static int matrix_keypad_resume(struct device *dev)
> Â Â Â Âconst struct matrix_keypad_platform_data *pdata = keypad->pdata;
> Â Â Â Âint i;
>
> - Â Â Â if (device_may_wakeup(&pdev->dev)) {
> - Â Â Â Â Â Â Â for (i = 0; i < pdata->num_row_gpios; i++) {
> - Â Â Â Â Â Â Â Â Â Â Â if (test_and_clear_bit(i, keypad->disabled_gpios)) {
> - Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â unsigned int gpio = pdata->row_gpios[i];
> -
> - Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â disable_irq_wake(gpio_to_irq(gpio));
> - Â Â Â Â Â Â Â Â Â Â Â }
> - Â Â Â Â Â Â Â }
> - Â Â Â }
> + Â Â Â if (device_may_wakeup(&pdev->dev))
> + Â Â Â Â Â Â Â __resume_keypad(keypad);
>
> Â Â Â Âmatrix_keypad_start(keypad->input_dev);
>
> @@ -296,17 +347,31 @@ static int __devinit init_matrix_gpio(struct platform_device *pdev,
> Â Â Â Â Â Â Â Âgpio_direction_input(pdata->row_gpios[i]);
> Â Â Â Â}
>
> - Â Â Â for (i = 0; i < pdata->num_row_gpios; i++) {
> - Â Â Â Â Â Â Â err = request_irq(gpio_to_irq(pdata->row_gpios[i]),
> + Â Â Â if (pdata->clustered_irq > 0) {
> + Â Â Â Â Â Â Â err = request_irq(pdata->clustered_irq,
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âmatrix_keypad_interrupt,
> - Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â IRQF_DISABLED |
> - Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pdata->clustered_irq_flags,
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â"matrix-keypad", keypad);
> Â Â Â Â Â Â Â Âif (err) {
> Â Â Â Â Â Â Â Â Â Â Â Âdev_err(&pdev->dev,
> - Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "Unable to acquire interrupt for GPIO line %i\n",
> - Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pdata->row_gpios[i]);
> - Â Â Â Â Â Â Â Â Â Â Â goto err_free_irqs;
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "Unable to acquire clustered interrupt\n");
> + Â Â Â Â Â Â Â Â Â Â Â goto err_free_rows;
> + Â Â Â Â Â Â Â }
> + Â Â Â } else {
> + Â Â Â Â Â Â Â for (i = 0; i < pdata->num_row_gpios; i++) {
> + Â Â Â Â Â Â Â Â Â Â Â err = request_irq(gpio_to_irq(pdata->row_gpios[i]),
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â matrix_keypad_interrupt,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â IRQF_DISABLED |
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â IRQF_TRIGGER_RISING |
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â IRQF_TRIGGER_FALLING,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "matrix-keypad", keypad);
> + Â Â Â Â Â Â Â Â Â Â Â if (err) {
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â dev_err(&pdev->dev,
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "Unable to acquire interrupt "
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "for GPIO line %i\n",
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â pdata->row_gpios[i]);
> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â goto err_free_irqs;
> + Â Â Â Â Â Â Â Â Â Â Â }
> Â Â Â Â Â Â Â Â}
> Â Â Â Â}
>
> @@ -418,11 +483,16 @@ static int __devexit matrix_keypad_remove(struct platform_device *pdev)
>
> Â Â Â Âdevice_init_wakeup(&pdev->dev, 0);
>
> - Â Â Â for (i = 0; i < pdata->num_row_gpios; i++) {
> - Â Â Â Â Â Â Â free_irq(gpio_to_irq(pdata->row_gpios[i]), keypad);
> - Â Â Â Â Â Â Â gpio_free(pdata->row_gpios[i]);
> + Â Â Â if (pdata->clustered_irq > 0) {
> + Â Â Â Â Â Â Â free_irq(pdata->clustered_irq, keypad);
> + Â Â Â } else {
> + Â Â Â Â Â Â Â for (i = 0; i < pdata->num_row_gpios; i++)
> + Â Â Â Â Â Â Â Â Â Â Â free_irq(gpio_to_irq(pdata->row_gpios[i]), keypad);
> Â Â Â Â}
>
> + Â Â Â for (i = 0; i < pdata->num_row_gpios; i++)
> + Â Â Â Â Â Â Â gpio_free(pdata->row_gpios[i]);
> +
> Â Â Â Âfor (i = 0; i < pdata->num_col_gpios; i++)
> Â Â Â Â Â Â Â Âgpio_free(pdata->col_gpios[i]);
>
> diff --git a/include/linux/input/matrix_keypad.h b/include/linux/input/matrix_keypad.h
> index c964cd7..8e8dc2e 100644
> --- a/include/linux/input/matrix_keypad.h
> +++ b/include/linux/input/matrix_keypad.h
> @@ -63,6 +63,11 @@ struct matrix_keypad_platform_data {
> Â Â Â Â/* key debounce interval in milli-second */
>    Âunsigned int  Âdebounce_ms;
>
> + Â Â Â /* used if interrupts of all row/column GPIOs are bundled to one single
> + Â Â Â Â* irq */
> +    unsigned int  Âclustered_irq;
> +    unsigned int  Âclustered_irq_flags;
> +
>    Âbool      Âactive_low;
>    Âbool      Âwakeup;
>    Âbool      Âno_autorepeat;
> --
> 1.7.0
>
>
èº{.nÇ+‰·Ÿ®‰­†+%ŠËlzwm…ébëæìr¸›zX§»®w¥Š{ayºÊÚë,j­¢f£¢·hš‹àz¹®w¥¢¸ ¢·¦j:+v‰¨ŠwèjØm¶Ÿÿ¾«‘êçzZ+ƒùšŽŠÝj"ú!¶iO•æ¬z·švØ^¶m§ÿðà nÆàþY&—