Input: matrix-keypad - Add device tree support
Subsystems (3)
cover Input: matrix-keypad - Add device tree support
Add device tree support to matrix keypad driver by adding all necessary DT parameters. Also added device tree bindings.
These patches were tested on AM335x-EVM with DT patches, DT patches will be submitted once these are accepted.
AnilKumar Ch (2):
Input: matrix-keypad - remove const from pointer to array of gpios
Input: matrix-keypad - Add device tree support
.../devicetree/bindings/input/matrix-keypad.txt | 52 ++++++++++ drivers/input/keyboard/matrix_keypad.c | 104 +++++++++++++++++++- include/linux/input/matrix_keypad.h | 4 +- 3 files changed, 157 insertions(+), 3 deletions(-) create mode 100644 Documentation/devicetree/bindings/input/matrix-keypad.txt
Signature · 1 line
1.7.9.5
PATCH 1/2 Input: matrix-keypad - remove const from pointer to array of gpios
Remove const from pointer to array of gpios in matrix_keypad_platform_data struct. This is required if we update row_gpios and col_gpios based on device tree data.
--- include/linux/input/matrix_keypad.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/input/matrix_keypad.h +2 -2
@@ -56,8 +56,8 @@ struct matrix_keymap_data {struct matrix_keypad_platform_data {const struct matrix_keymap_data *keymap_data; -const unsigned int *row_gpios; -const unsigned int *col_gpios; +unsigned int *row_gpios; +unsigned int *col_gpios;unsigned int num_row_gpios;unsigned int num_col_gpios;
Signature · 1 line
1.7.9.5
Dmitry Torokhov
maintainerRe: [PATCH 1/2] Input: matrix-keypad - remove const from pointer to array of gpios
Quoted 3 lines · AnilKumar Ch
Remove const from pointer to array of gpios in
matrix_keypad_platform_datastruct. This is required if we updaterow_gpiosandcol_gpiosbased on device tree data.
Then don't. Set them up via non-const aliases instead.
Thanks.
Signature · 1 line
Dmitry
RE: [PATCH 1/2] Input: matrix-keypad - remove const from pointer to array of gpios
Quoted 7 lines · Dmitry Torokhov
Quoted 3 lines · AnilKumar Ch
Remove const from pointer to array of gpios in
matrix_keypad_platform_datastruct. This is required if we updaterow_gpiosandcol_gpiosbased on device tree data.Then don't. Set them up via non-const aliases instead.
Changed.
Thanks
AnilKumar
PATCH 2/2 Input: matrix-keypad - Add device tree support
In reply to AnilKumar ChAdd device tree support to matrix keypad driver and usage details are added to device tree documentation. Driver was tested on AM335x EVM.
--- .../devicetree/bindings/input/matrix-keypad.txt | 52 ++++++++++ drivers/input/keyboard/matrix_keypad.c | 104 +++++++++++++++++++- 2 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 Documentation/devicetree/bindings/input/matrix-keypad.txt
Documentation/devicetree/bindings/input/matrix-keypad.txt new file +52 -0
@@ -0,0 +1,52 @@ +* GPIO driven matrix keypad device tree bindings + +GPIO driven matrix keypad is used to interface a SoC with a matrix keypad. +The matrix keypad supports multiple row and column lines, a key can be +placed at each intersection of a unique row and a unique column. The matrix +keypad can sense a key-press and key-release by means of GPIO lines and +report the event using GPIO interrupts to the cpu. + +Required Properties: +- compatible: Should be "matix-keypad" +- keypad,num-row-gpios: Number of row lines connected to the keypad + controller. +- keypad,num-col-gpios: Number of column lines connected to the keypad + controller. +- row-gpios: List of gpios used as row lines. The gpio specifier + for this property depends on the gpio controller to + which these row lines are connected. +- col-gpios: List of gpios used as column lines. The gpio specifier + for this property depends on the gpio controller to + which these column lines are connected. + +Optional Properties: +- linux,no-autorepeat: do no enable autorepeat feature. +- linux,wakeup: use any event on keypad as wakeup event. +- debounce-delay-ms: debounce interval in milliseconds +- col-scan-delay-us: delay, measured in microseconds, that is needed + before we can scan keypad after activating column gpio +- clustered-irq: have clustered irq number +- clustered-irq-flags: have clustered irq flags + +Example: + matrix-keypad { + compatible = "matrix-keypad"; + keypad,num-row-gpios = <3>; + keypad,num-col-gpios = <2>; + debounce-delay-ms = <5>; + col-scan-delay-us = <2>; + + row-gpios = <&gpio2 25 0 + &gpio2 26 0 + &gpio2 27 0>; + + col-gpios = <&gpio2 21 0 + &gpio2 22 0>; + + linux,keymap = <0x0000008B + 0x0100009E + 0x02000069 + 0x0001006A + 0x0101001C + 0x0201006C>; + };
drivers/input/keyboard/matrix_keypad.c +103 -1
134 lines not on this page · raw message
Signature · 1 line
1.7.9.5
Quoted 17 lines · AnilKumar Ch
Add device tree support to matrix keypad driver and usage details are added to device tree documentation. Driver was tested on AM335x EVM.
Signed-off-by AnilKumar Ch <[email protected]>--- .../devicetree/bindings/input/matrix-keypad.txt | 52 ++++++++++ drivers/input/keyboard/matrix_keypad.c | 104 +++++++++++++++++++- 2 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 Documentation/devicetree/bindings/input/matrix-keypad.txtDocumentation/devicetree/bindings/input/matrix-keypad.txt new file +0
-0
Documentation/devicetree/bindings/input/matrix-keypad.txt line 10
Quoted 10 lines
Documentation/devicetree/bindings/input/matrix-keypad.txt new file +10
-0@@ -0,0 +1,52 @@ +* GPIO driven matrix keypad device tree bindings + +GPIO driven matrix keypad is used to interface a SoC with a matrix keypad. +The matrix keypad supports multiple row and column lines, a key can be +placed at each intersection of a unique row and a unique column. The matrix +keypad can sense a key-press and key-release by means of GPIO lines and +report the event using GPIO interrupts to the cpu. + +Required Properties: +- compatible: Should be "matix-keypad"
How about gpio-matrix-keypad?
Documentation/devicetree/bindings/input/matrix-keypad.txt line 14
Quoted 4 lines
Documentation/devicetree/bindings/input/matrix-keypad.txt new file +4
-0+- keypad,num-row-gpios: Number of row lines connected to the keypad + controller. +- keypad,num-col-gpios: Number of column lines connected to the keypad + controller.
Isn't the number of gpios just the count of gpios listed below? So you don't need these props.
Quoted 5 lines
Documentation/devicetree/bindings/input/matrix-keypad.txt new file +5
-0+- row-gpios: List of gpios used as row lines. The gpio specifier + for this property depends on the gpio controller to + which these row lines are connected. +- col-gpios: List of gpios used as column lines. The gpio specifier + for this property depends on the gpio controller to
Documentation/devicetree/bindings/input/matrix-keypad.txt line 29
Quoted 10 lines
Documentation/devicetree/bindings/input/matrix-keypad.txt new file +10
-0+ which these column lines are connected. + +Optional Properties: +- linux,no-autorepeat: do no enable autorepeat feature. +- linux,wakeup: use any event on keypad as wakeup event. +- debounce-delay-ms: debounce interval in milliseconds +- col-scan-delay-us: delay, measured in microseconds, that is needed + before we can scan keypad after activating column gpio +- clustered-irq: have clustered irq number +- clustered-irq-flags: have clustered irq flags
It's not clear what clustered means here. If I have to go read Linux code to understand, you are doing it wrong. Describe the h/w, not Linux data structs.
Quoted 76 lines
Documentation/devicetree/bindings/input/matrix-keypad.txt new file +23
-0+ +Example: + matrix-keypad { + compatible = "matrix-keypad"; + keypad,num-row-gpios = <3>; + keypad,num-col-gpios = <2>; + debounce-delay-ms = <5>; + col-scan-delay-us = <2>; + + row-gpios = <&gpio2 25 0 + &gpio2 26 0 + &gpio2 27 0>; + + col-gpios = <&gpio2 21 0 + &gpio2 22 0>; + + linux,keymap = <0x0000008B + 0x0100009E + 0x02000069 + 0x0001006A + 0x0101001C + 0x0201006C>; + };drivers/input/keyboard/matrix_keypad.c +38
-0@@ -23,6 +23,9 @@#include <linux/gpio.h> #include <linux/input/matrix_keypad.h> #include <linux/slab.h> +#include <linux/of.h> +#include <linux/of_gpio.h> +#include <linux/of_platform.h> struct matrix_keypad {const struct matrix_keypad_platform_data *pdata;@@ -394,6 +397,91 @@ static void matrix_keypad_free_gpio(struct matrix_keypad *keypad)gpio_free (pdata->col_gpios[i]); } +#ifdef CONFIG_OF +static +struct matrix_keypad_platform_data *matrix_keypad_parse_dt (struct device *dev) +{ +struct matrix_keypad_platform_data *pdata; +struct matrix_keymap_data *keymap_data; +struct device_node *np = dev->of_node; +struct property *prop; +int key_count =0 , length, row, col; +uint32_t *keymap; + + pdata =devm_kzalloc (dev,sizeof (*pdata),GFP_KERNEL ); +if (!pdata) { +dev_err (dev,"could not allocate memory for platform data\n" ); +return NULL ; + } + +of_property_read_u32 (np,"keypad,num-row-gpios" , &pdata->num_row_gpios); +of_property_read_u32 (np,"keypad,num-col-gpios" , &pdata->num_col_gpios); +if (!pdata->num_row_gpios || !pdata->num_col_gpios) { +dev_err (dev,"number of keypad rows/columns not specified\n" ); +return NULL ; + } + + keymap_data =devm_kzalloc (dev,sizeof (*keymap_data),GFP_KERNEL ); +if (!keymap_data) { +dev_err (dev,"could not allocate memory for keymap data\n" ); +return NULL ; + } + pdata->keymap_data = keymap_data; + + prop =of_find_property (np,"linux,keymap" , &length); +if (!prop) +return NULL ; +
drivers/input/keyboard/matrix_keypad.c static void matrix_keypad_free_gpio(struct matrix_keypad *keypad) · line 444
Quoted 10 lines
drivers/input/keyboard/matrix_keypad.c +10
-0+ key_count = length /sizeof (u32); + keymap_data->keymap_size = key_count; + keymap =devm_kzalloc (dev,sizeof (uint32_t ) * key_count,GFP_KERNEL ); +if (!keymap) { +dev_err (dev,"could not allocate memory for keymap\n" ); +return NULL ; + } + keymap_data->keymap = keymap; + +of_property_read_u32_array (np,"linux,keymap" , keymap, key_count);
Can't you use matrix_keypad_parse_of_keymap?
Quoted 9 lines
drivers/input/keyboard/matrix_keypad.c +9
-0+ +if (of_get_property (np,"linux,no-autorepeat" ,NULL )) + pdata->no_autorepeat = true; +if (of_get_property (np,"linux,wakeup" ,NULL )) + pdata->wakeup = true; +if (of_get_property (np,"gpio-activelow" ,NULL )) + pdata->active_low = true; + +of_property_read_u32 (np,"debounce-delay-ms" , &pdata->debounce_ms);
drivers/input/keyboard/matrix_keypad.c static void matrix_keypad_free_gpio(struct matrix_keypad *keypad) · line 463
Quoted 10 lines
drivers/input/keyboard/matrix_keypad.c +10
-0+of_property_read_u32 (np,"col-scan-delay-us" , + &pdata->col_scan_delay_us); +of_property_read_u32 (np,"clustered-irq" , &pdata->clustered_irq); +of_property_read_u32 (np,"clustered-irq-flags" , + &pdata->clustered_irq_flags); + + pdata->row_gpios =devm_kzalloc (dev,sizeof (unsigned int ) * + pdata->num_row_gpios,GFP_KERNEL ); + pdata->col_gpios =devm_kzalloc (dev,sizeof (unsigned int ) * + pdata->num_col_gpios,GFP_KERNEL );
There's kind of a lot of allocations in this function. It would be good to combine some of them.
Quoted 39 lines
drivers/input/keyboard/matrix_keypad.c +25
-1+if (!pdata->row_gpios || !pdata->col_gpios) { +dev_err (dev,"could not allocate memory for gpios\n" ); +return NULL ; + } + +for (row =0 ; row < pdata->num_row_gpios; row++) + pdata->row_gpios[row] =of_get_named_gpio (np,"row-gpios" , row); + +for (col =0 ; col < pdata->num_col_gpios; col++) + pdata->col_gpios[col] =of_get_named_gpio (np,"col-gpios" , col); + + return pdata; +} +#else +static +struct matrix_keypad_platform_data *matrix_keypad_parse_dt (struct device *dev) +{ +return NULL ; +} +#endif +static int __devinit matrix_keypad_probe (struct platform_device *pdev) {const struct matrix_keypad_platform_data *pdata;@@ -404,7 +492,10 @@ static int __devinit matrix_keypad_probe(struct platform_device *pdev)size_t keymap_size;int err; - pdata = pdev->dev.platform_data; +if (pdev->dev.of_node) + pdata =matrix_keypad_parse_dt (&pdev->dev); +else + pdata = pdev->dev.platform_data;if (!pdata) {dev_err (&pdev->dev,"no platform data defined\n" );return -EINVAL ;@@ -488,6 +579,16 @@ static int __devexit matrix_keypad_remove(struct platform_device *pdev)return 0 ; }
drivers/input/keyboard/matrix_keypad.c static int __devexit matrix_keypad_remove(struct platform_device *pdev) · line 590
Quoted 10 lines
drivers/input/keyboard/matrix_keypad.c +9
-0+#ifdef CONFIG_OF +static const struct of_device_id matrix_keypad_dt_match[] = { + { .compatible ="matrix-keypad" }, + {}, +}; +MODULE_DEVICE_TABLE (of, matrix_keypad_dt_match); +#else +#define matrix_keypad_dt_match NULL +#endif
Use of_match_ptr() and remove the #else
Rob
Quoted 13 lines
drivers/input/keyboard/matrix_keypad.c +2
-0+static struct platform_driver matrix_keypad_driver = { .probe = matrix_keypad_probe, .remove =__devexit_p (matrix_keypad_remove),@@ -495,6 +596,7 @@ static struct platform_driver matrix_keypad_driver = { .name = "matrix-keypad", .owner = THIS_MODULE, .pm = &matrix_keypad_pm_ops, + .of_match_table = matrix_keypad_dt_match, }, };module_platform_driver (matrix_keypad_driver);
Hi Rob,
Thanks for the comments.
Quoted 22 lines · Rob Herring
Quoted 21 lines · AnilKumar Ch
Add device tree support to matrix keypad driver and usage details are added to device tree documentation. Driver was tested on AM335x EVM.
Signed-off-by AnilKumar Ch <[email protected]>--- .../devicetree/bindings/input/matrix-keypad.txt | 52 ++++++++++ drivers/input/keyboard/matrix_keypad.c | 104 +++++++++++++++++++- 2 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 Documentation/devicetree/bindings/input/matrix-keypad.txtDocumentation/devicetree/bindings/input/matrix-keypad.txt new file +4
-0@@ -0,0 +1,52 @@ +* GPIO driven matrix keypad device tree bindings + +GPIO driven matrix keypad is used to interface a SoC with a matrix keypad. +The matrix keypad supports multiple row and column lines, a key can be
Quoted 8 lines
Documentation/devicetree/bindings/input/matrix-keypad.txt line 10
Quoted 6 lines
Documentation/devicetree/bindings/input/matrix-keypad.txt new file +6
-0+placed at each intersection of a unique row and a unique column. The matrix +keypad can sense a key-press and key-release by means of GPIO lines and +report the event using GPIO interrupts to the cpu. + +Required Properties: +- compatible: Should be "matix-keypad"How about gpio-matrix-keypad?
More meaningful, changed
Quoted 8 lines
Documentation/devicetree/bindings/input/matrix-keypad.txt line 14
Quoted 5 lines
Documentation/devicetree/bindings/input/matrix-keypad.txt new file +4
-0+- keypad,num-row-gpios: Number of row lines connected to the keypad + controller. +- keypad,num-col-gpios: Number of column lines connected to the keypad + controller.Isn't the number of gpios just the count of gpios listed below? So you don't need these props.
Removed
Quoted 12 lines
Quoted 12 lines
Documentation/devicetree/bindings/input/matrix-keypad.txt new file +11
-0+- row-gpios: List of gpios used as row lines. The gpio specifier + for this property depends on the gpio controller to + which these row lines are connected. +- col-gpios: List of gpios used as column lines. The gpio specifier + for this property depends on the gpio controller to + which these column lines are connected. + +Optional Properties: +- linux,no-autorepeat: do no enable autorepeat feature. +- linux,wakeup: use any event on keypad as wakeup event. +- debounce-delay-ms: debounce interval in milliseconds
Quoted 8 lines
Documentation/devicetree/bindings/input/matrix-keypad.txt line 29
Quoted 4 lines
Documentation/devicetree/bindings/input/matrix-keypad.txt new file +4
-0+- col-scan-delay-us: delay, measured in microseconds, that is needed + before we can scan keypad after activating column gpio +- clustered-irq: have clustered irq number +- clustered-irq-flags: have clustered irq flagsIt's not clear what clustered means here. If I have to go read Linux code to understand, you are doing it wrong. Describe the h/w, not Linux data structs.
I have added based on pdata of the driver, I am not using these parameters because AM335x-EVM have different interrupts for all gpio pins.
clustered-irq: combined irq source for the whole matrix keypad, like all gpio keys are connected to a gpio expander
Quoted 81 lines
Quoted 81 lines
Documentation/devicetree/bindings/input/matrix-keypad.txt new file +23
-0+ +Example: + matrix-keypad { + compatible = "matrix-keypad"; + keypad,num-row-gpios = <3>; + keypad,num-col-gpios = <2>; + debounce-delay-ms = <5>; + col-scan-delay-us = <2>; + + row-gpios = <&gpio2 25 0 + &gpio2 26 0 + &gpio2 27 0>; + + col-gpios = <&gpio2 21 0 + &gpio2 22 0>; + + linux,keymap = <0x0000008B + 0x0100009E + 0x02000069 + 0x0001006A + 0x0101001C + 0x0201006C>; + };drivers/input/keyboard/matrix_keypad.c +42
-0@@ -23,6 +23,9 @@#include <linux/gpio.h> #include <linux/input/matrix_keypad.h> #include <linux/slab.h> +#include <linux/of.h> +#include <linux/of_gpio.h> +#include <linux/of_platform.h> struct matrix_keypad {const struct matrix_keypad_platform_data *pdata;@@ -394,6 +397,91 @@ static void matrix_keypad_free_gpio(struct matrix_keypad *keypad)gpio_free (pdata->col_gpios[i]); } +#ifdef CONFIG_OF +static +struct matrix_keypad_platform_data *matrix_keypad_parse_dt (struct device *dev) +{ +struct matrix_keypad_platform_data *pdata; +struct matrix_keymap_data *keymap_data; +struct device_node *np = dev->of_node; +struct property *prop; +int key_count =0 , length, row, col; +uint32_t *keymap; + + pdata =devm_kzalloc (dev,sizeof (*pdata),GFP_KERNEL ); +if (!pdata) { +dev_err (dev,"could not allocate memory for platform data\n" ); +return NULL ; + } + +of_property_read_u32 (np,"keypad,num-row-gpios" , &pdata->num_row_gpios); +of_property_read_u32 (np,"keypad,num-col-gpios" , &pdata->num_col_gpios); +if (!pdata->num_row_gpios || !pdata->num_col_gpios) { +dev_err (dev,"number of keypad rows/columns not specified\n" ); +return NULL ; + } + + keymap_data =devm_kzalloc (dev,sizeof (*keymap_data),GFP_KERNEL ); +if (!keymap_data) { +dev_err (dev,"could not allocate memory for keymap data\n" ); +return NULL ; + } + pdata->keymap_data = keymap_data; + + prop =of_find_property (np,"linux,keymap" , &length); +if (!prop) +return NULL ; + + key_count = length /sizeof (u32); + keymap_data->keymap_size = key_count; + keymap =devm_kzalloc (dev,sizeof (uint32_t ) * key_count,GFP_KERNEL ); +if (!keymap) {
Quoted 8 lines
drivers/input/keyboard/matrix_keypad.c static void matrix_keypad_free_gpio(struct matrix_keypad *keypad) · line 444
Quoted 6 lines
drivers/input/keyboard/matrix_keypad.c +6
-0+dev_err (dev,"could not allocate memory for keymap\n" ); +return NULL ; + } + keymap_data->keymap = keymap; + +of_property_read_u32_array (np,"linux,keymap" , keymap, key_count);Can't you use
matrix_keypad_parse_of_keymap?
To use this API matrix_keypad_build_keymap() in probe should be removed if so non-dt case have the problem.
Quoted 15 lines
Quoted 15 lines
drivers/input/keyboard/matrix_keypad.c +14
-0+ +if (of_get_property (np,"linux,no-autorepeat" ,NULL )) + pdata->no_autorepeat = true; +if (of_get_property (np,"linux,wakeup" ,NULL )) + pdata->wakeup = true; +if (of_get_property (np,"gpio-activelow" ,NULL )) + pdata->active_low = true; + +of_property_read_u32 (np,"debounce-delay-ms" , &pdata->debounce_ms); +of_property_read_u32 (np,"col-scan-delay-us" , + &pdata->col_scan_delay_us); +of_property_read_u32 (np,"clustered-irq" , &pdata->clustered_irq); +of_property_read_u32 (np,"clustered-irq-flags" , + &pdata->clustered_irq_flags);
Quoted 8 lines
drivers/input/keyboard/matrix_keypad.c static void matrix_keypad_free_gpio(struct matrix_keypad *keypad) · line 463
Quoted 5 lines
drivers/input/keyboard/matrix_keypad.c +5
-0+ + pdata->row_gpios =devm_kzalloc (dev,sizeof (unsigned int ) * + pdata->num_row_gpios,GFP_KERNEL ); + pdata->col_gpios =devm_kzalloc (dev,sizeof (unsigned int ) * + pdata->num_col_gpios,GFP_KERNEL );There's kind of a lot of allocations in this function. It would be good to combine some of them.
These are non-const aliases, so these are required.
Quoted 41 lines
Quoted 41 lines
drivers/input/keyboard/matrix_keypad.c +25
-1+if (!pdata->row_gpios || !pdata->col_gpios) { +dev_err (dev,"could not allocate memory for gpios\n" ); +return NULL ; + } + +for (row =0 ; row < pdata->num_row_gpios; row++) + pdata->row_gpios[row] =of_get_named_gpio (np,"row-gpios" , row); + +for (col =0 ; col < pdata->num_col_gpios; col++) + pdata->col_gpios[col] =of_get_named_gpio (np,"col-gpios" , col); + + return pdata; +} +#else +static +struct matrix_keypad_platform_data *matrix_keypad_parse_dt (struct device *dev) +{ +return NULL ; +} +#endif +static int __devinit matrix_keypad_probe (struct platform_device *pdev) {const struct matrix_keypad_platform_data *pdata;@@ -404,7 +492,10 @@ static int __devinit matrix_keypad_probe(struct platform_device *pdev)size_t keymap_size;int err; - pdata = pdev->dev.platform_data; +if (pdev->dev.of_node) + pdata =matrix_keypad_parse_dt (&pdev->dev); +else + pdata = pdev->dev.platform_data;if (!pdata) {dev_err (&pdev->dev,"no platform data defined\n" );return -EINVAL ;@@ -488,6 +579,16 @@ static int __devexit matrix_keypad_remove(struct platform_device *pdev)return 0 ; }
Quoted 12 lines
drivers/input/keyboard/matrix_keypad.c static int __devexit matrix_keypad_remove(struct platform_device *pdev) · line 590
Quoted 9 lines
drivers/input/keyboard/matrix_keypad.c +9
-0+#ifdef CONFIG_OF +static const struct of_device_id matrix_keypad_dt_match[] = { + { .compatible ="matrix-keypad" }, + {}, +}; +MODULE_DEVICE_TABLE (of, matrix_keypad_dt_match); +#else +#define matrix_keypad_dt_match NULL +#endif Use
of_match_ptr()and remove the #else
Changed.
Thanks
AnilKumar