Difference between revisions of "GPIO keys"

From ArmadeusWiki
Jump to: navigation, search
(Test)
(Introduction)
 
(11 intermediate revisions by 4 users not shown)
Line 1: Line 1:
How to use gpio-keys driver to read states of the user switch of your Armadeus board (APF51)
 
 
 
==Introduction==
 
==Introduction==
  
Your APF51 board features a user switch connected to a GPIO pin (GPIO1_3). The driver gpio-keys translates GPIO events in key/button events.
+
How to use gpio-keys driver to read states of the user switch of your Armadeus board.
 +
Your APF51|APF27|APF28 development board feature a user switch connected to a GPIO pin. The driver gpio-keys translates GPIO events in key/button events.
 +
Here are the GPIO used for the user button/switch for each APF board:
 +
* APF27: GPIO_PORTF | 13
 +
* APF28: PINID_GPMI_CE1N (Bank 0 - bit 17)
 +
* APF51: GPIO1_3
 +
* OPOS6ULDEV: GPIO2_11
  
 
+
==Configuration==
==Test==
+
<pre class="apf">
+
# cat /dev/input/event0
+
</pre>
+
Then you should see weirds characters '''when pressing''' the user button of the apf51_dev board:
+
<pre class="apf">
+
T
+
����T
+
      �T
+
        �
+
        ��T
+
            %�
+
</pre>
+
* if the test wiped out your console, you can get it back with:
+
<pre class="apf">
+
# reset
+
</pre>
+
==For APF27==
+
  
 
First, you need to enable the gpio_keys in your kernel.
 
First, you need to enable the gpio_keys in your kernel.
Line 35: Line 21:
 
</pre>
 
</pre>
  
Then, in your ''apf27-dev.c'', you need to define your GPIO button <b>before</b> the variable ''platform_devices[]''.
+
=== Without device tree ===
 +
Then, in your ''apfXX-dev.c'', you would need to define your GPIO button <b>before</b> the variable ''platform_devices[]'' and also include gpio_keys.h and input.h. his code is already implemented for the APF27, APF28 and APF51 so the source code hereafter (for the APF27) is only present as a reference sample to understand how to activate a GPIO KEY driver.
  
 
<source lang="c">
 
<source lang="c">
/* GPIO KEYS */
+
#include <linux/gpio_keys.h>
#if 1
+
#include <linux/input.h>
 +
</source>
  
/* PORTA_6 used as gpio_keys (GPIO used as input event) */
+
<source lang="c">
static struct gpio_keys_button apf27_gpio_keys[] = {
+
/* GPIO KEYS */
 +
#if defined(CONFIG_KEYBOARD_GPIO) || defined(CONFIG_KEYBOARD_GPIO_MODULE)
 +
static struct gpio_keys_button apf27dev_gpio_keys[] = {
 
{
 
{
.code = EV_PWR, /* See include/linux/input.h for more event code */
+
.code = BTN_EXTRA, /* See include/linux/input.h */
.gpio = (GPIO_PORTA | 6), /* GPIO number */
+
.gpio = (GPIO_PORTF | 13), /* GPIO number */
.active_low = 0,
+
.active_low = 1,
.desc = "Notification when the AC is deconnected", /* Button description*/
+
.desc = "s1", /* Button description*/
.type = 0, /* See include/linux/input.h for more type code */
+
.wakeup = 0,
 
},
 
},
 
};
 
};
  
static struct gpio_keys_platform_data apf27_gpio_keys_data = {
+
static struct gpio_keys_platform_data apf27dev_gpio_keys_data = {
.buttons = apf27_gpio_keys,
+
.buttons = apf27dev_gpio_keys,
.nbuttons = ARRAY_SIZE(apf27_gpio_keys),
+
.nbuttons = ARRAY_SIZE(apf27dev_gpio_keys),
 
};
 
};
  
static struct platform_device apf27_gpio_keys_device = {
+
static struct platform_device apf27dev_gpio_keys_device = {
 
.name = "gpio-keys",
 
.name = "gpio-keys",
.id = 0,
+
.id = -1,
 
.dev = {
 
.dev = {
.platform_data = &apf27_gpio_keys_data,
+
.platform_data = &apf27dev_gpio_keys_data,
 
},
 
},
 
};
 
};
# define GPIO_KEYS &apf27_gpio_keys_device,
+
#endif /* CONFIG_KEYBOARD_GPIO */
#else
+
 
# define GPIO_KEYS
+
#endif
+
 
</source>
 
</source>
  
Line 74: Line 62:
 
<source lang="c">
 
<source lang="c">
 
static struct platform_device *platform_devices[] __initdata = {
 
static struct platform_device *platform_devices[] __initdata = {
 +
#if defined(CONFIG_KEYBOARD_GPIO) || defined(CONFIG_KEYBOARD_GPIO_MODULE)
 +
&apf27dev_gpio_keys_device,
 +
#endif
 
ALSA_SOUND
 
ALSA_SOUND
GPIO_KEYS
 
 
};
 
};
 
</source>
 
</source>
 +
 +
=== With device tree ===
 +
This description is based to the APF28dev board and the modifications must be done into buildroot/output/build/linux-xxx/arch/arm/boot/dts/imx28-apf28dev.dts.
 +
 +
To define gpio-keys, you need to add a node and a sub-node after all peripheral nodes :
 +
<source lang="c">
 +
gpio-keys {
 +
        compatible = "gpio-keys";
 +
 +
        left-key {
 +
            label = "Left key";
 +
            gpios = <&gpio0 17 0>;
 +
            linux,code = <69>; /* KEY_LEFT */
 +
        };
 +
    }
 +
</source>
 +
 +
It is possible to add as many node you want (within the limits of available pins).
 +
 +
A key node contains :
 +
* a name (left-key)
 +
* a label
 +
* a gpio <&gpioX Y 0> with X is the bank number and Y the pin number
 +
* a linux code, available in buildroot/output/build/linux-xxx/include/uapi/linux/input.h
 +
 +
For each key, you need to add the corresponding GPIO in hog sub-node of pinctrl node
 +
<source lang="c">
 +
hog_pins_apf28dev: hog@0 {
 +
                    ...
 +
                    fsl,pinmux-ids = <
 +
                        ...
 +
                        0x0113 /* MX28_PAD_GPMI_CE1N__GPIO_0_17 - the user button on the apf28dev */
 +
                        ...
 +
                    >;
 +
                    ...
 +
                };
 +
</source>
 +
The pinmux is available in buildroot/output/build/linux-xxx/Documentation/devicetree/bindings/pinctrl/fsl,mxs-pinctrl.txt
 +
 +
==Usage==
 +
<pre class="apf">
 +
# cat /dev/input/event0
 +
</pre>
 +
Then you should see weirds characters '''when pressing''' the user button of the apf51_dev board:
 +
<pre class="apf">
 +
T
 +
����T
 +
      �T
 +
        �
 +
        ��T
 +
            %�
 +
</pre>
 +
* if the test wiped out your console, you can get it back with:
 +
<pre class="apf">
 +
# reset
 +
</pre>
 +
 +
You also can use the tool ''evtest''.
 +
 +
<pre class="config">
 +
Hardware handling  --->
 +
    [*] input-tools
 +
          [*]  evtest
 +
</pre>
 +
 +
<pre class="apf">
 +
# evtest /dev/input/event0
 +
...
 +
Event: time 1335981358.550329, type 22 (EV_PWR), code 0 (), value 1
 +
Event: time 1335981358.550330, -------------- SYN_REPORT ------------
 +
Event: time 1335981358.550329, type 22 (EV_PWR), code 0 (), value 0
 +
Event: time 1335981358.550330, -------------- SYN_REPORT ------------
 +
 +
</pre>
 +
 +
==Links==
 +
http://www.kernel.org/doc/Documentation/input/
  
 
[[Category:UserInput]]
 
[[Category:UserInput]]

Latest revision as of 15:52, 21 June 2017

Introduction

How to use gpio-keys driver to read states of the user switch of your Armadeus board. Your APF51|APF27|APF28 development board feature a user switch connected to a GPIO pin. The driver gpio-keys translates GPIO events in key/button events. Here are the GPIO used for the user button/switch for each APF board:

  • APF27: GPIO_PORTF | 13
  • APF28: PINID_GPMI_CE1N (Bank 0 - bit 17)
  • APF51: GPIO1_3
  • OPOS6ULDEV: GPIO2_11

Configuration

First, you need to enable the gpio_keys in your kernel.

Device Drivers  --->
     Input device support  --->
           <*>   Event interface
           [*]   Keyboards  --->
               <*>   GPIO Buttons

Without device tree

Then, in your apfXX-dev.c, you would need to define your GPIO button before the variable platform_devices[] and also include gpio_keys.h and input.h. his code is already implemented for the APF27, APF28 and APF51 so the source code hereafter (for the APF27) is only present as a reference sample to understand how to activate a GPIO KEY driver.

#include <linux/gpio_keys.h>
#include <linux/input.h>
/* GPIO KEYS */
#if defined(CONFIG_KEYBOARD_GPIO) || defined(CONFIG_KEYBOARD_GPIO_MODULE)
static struct gpio_keys_button apf27dev_gpio_keys[] = {
	{
		.code = BTN_EXTRA, /* See include/linux/input.h */
		.gpio = (GPIO_PORTF | 13), /* GPIO number */
		.active_low = 1,
		.desc = "s1", /* Button description*/
		.wakeup = 0,
	},
};

static struct gpio_keys_platform_data apf27dev_gpio_keys_data = {
	.buttons = apf27dev_gpio_keys,
	.nbuttons = ARRAY_SIZE(apf27dev_gpio_keys),
};

static struct platform_device apf27dev_gpio_keys_device = {
	.name = "gpio-keys",
	.id = -1,
	.dev = {
		.platform_data = &apf27dev_gpio_keys_data,
	},
};
#endif /* CONFIG_KEYBOARD_GPIO */

Add the button to get it recognized by the card.

static struct platform_device *platform_devices[] __initdata = {
#if defined(CONFIG_KEYBOARD_GPIO) || defined(CONFIG_KEYBOARD_GPIO_MODULE)
	&apf27dev_gpio_keys_device,
#endif
	ALSA_SOUND
};

With device tree

This description is based to the APF28dev board and the modifications must be done into buildroot/output/build/linux-xxx/arch/arm/boot/dts/imx28-apf28dev.dts.

To define gpio-keys, you need to add a node and a sub-node after all peripheral nodes :

gpio-keys {
        compatible = "gpio-keys";

        left-key {
            label = "Left key";
            gpios = <&gpio0 17 0>;
            linux,code = <69>; /* KEY_LEFT */
        };
    }

It is possible to add as many node you want (within the limits of available pins).

A key node contains :

* a name (left-key)
* a label 
* a gpio <&gpioX Y 0> with X is the bank number and Y the pin number
* a linux code, available in buildroot/output/build/linux-xxx/include/uapi/linux/input.h

For each key, you need to add the corresponding GPIO in hog sub-node of pinctrl node

hog_pins_apf28dev: hog@0 {
                    ...
                    fsl,pinmux-ids = <
                        ...
                        0x0113 /* MX28_PAD_GPMI_CE1N__GPIO_0_17 - the user button on the apf28dev */
                        ...
                    >;
                    ...
                };

The pinmux is available in buildroot/output/build/linux-xxx/Documentation/devicetree/bindings/pinctrl/fsl,mxs-pinctrl.txt

Usage

# cat /dev/input/event0

Then you should see weirds characters when pressing the user button of the apf51_dev board:

 T
 ����T
      �T
        �
         ��T
            %�
  • if the test wiped out your console, you can get it back with:
# reset

You also can use the tool evtest.

Hardware handling  --->
     [*] input-tools
          [*]   evtest
 # evtest /dev/input/event0
...
Event: time 1335981358.550329, type 22 (EV_PWR), code 0 (), value 1
Event: time 1335981358.550330, -------------- SYN_REPORT ------------
Event: time 1335981358.550329, type 22 (EV_PWR), code 0 (), value 0
Event: time 1335981358.550330, -------------- SYN_REPORT ------------

Links

http://www.kernel.org/doc/Documentation/input/