Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce gpio-keys wake-up sources using pinctrl #64538

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions boards/arm/sam4e_xpro/sam4e_xpro-pinctrl.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,38 @@
};
};

gpio_keys_default: gpio_keys_default {
group1 {
pinmux = <PA2X_SUPC_WKUP2>;
bias-pull-up;
};
};

hsmci_default: hsmci_default {
group1 {
pinmux = <PA28C_HSMCI_MCCDA>,
<PA29C_HSMCI_MCCK>,
<PA30C_HSMCI_MCDA0>,
<PA31C_HSMCI_MCDA1>,
<PA26C_HSMCI_MCDA2>,
<PA27C_HSMCI_MCDA3>;
};
};

mdio_default: mdio_default {
group1 {
pinmux = <PD8A_GMAC_GMDC>,
<PD9A_GMAC_GMDIO>;
};
};

pwm0_default: pwm0_default {
group1 {
pinmux = <PD20A_PWM_PWMH0>,
<PD24A_PWM_PWML0>;
};
};

spi0_default: spi0_default {
group1 {
pinmux = <PA12A_SPI_MISO>,
Expand Down Expand Up @@ -87,23 +112,4 @@
<PA25A_USART1_CTS>;
};
};

pwm0_default: pwm0_default {
group1 {
pinmux = <PD20A_PWM_PWMH0>,
<PD24A_PWM_PWML0>;
};
};

hsmci_default: hsmci_default {
group1 {
pinmux = <PA28C_HSMCI_MCCDA>,
<PA29C_HSMCI_MCCK>,
<PA30C_HSMCI_MCDA0>,
<PA31C_HSMCI_MCDA1>,
<PA26C_HSMCI_MCDA2>,
<PA27C_HSMCI_MCDA3>;
};
};

};
4 changes: 4 additions & 0 deletions boards/arm/sam4e_xpro/sam4e_xpro.dts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@

gpio_keys {
compatible = "gpio-keys";

pinctrl-0 = <&gpio_keys_default>;
pinctrl-names = "default";
Comment on lines +45 to +46
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you look at Linux, the way gpio-keys are configured as wake-up sources is via the wakeup-source property (in each subnode). So, I guess the proper way to handle this is to make the input driver handle such property and forward it to the GPIO/pinctrl(?) driver when configuring the pin.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you look at Linux, the way gpio-keys are configured as wake-up sources is via the wakeup-source property (in each subnode). So, I guess the proper way to handle this is to make the input driver handle such property and forward it to the GPIO/pinctrl(?) driver when configuring the pin.

This PR seems to be the most far I can go with what is implemented in tree. IMHO the use of wakeup-source can be addressed in another moment because it seems to require a dedicated API which, for the moment, is out of scope for this PR. To propose an API change or new API require in deep knowledge from other architectures which I don't have.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine adding these pinctrl entries (thanks for the https://github.com/torvalds/linux/blob/750b95887e567848ac2c851dae47922cac6db946/arch/arm/boot/dts/microchip/at91-sama5d2_xplained.dts#L708-L720 example). However, these entries should be of course optional, and the driver needs to be prepared to handle the case when they're not present, that is, handle the -ENOENT error case gracefully. Also, the wakeup-source can be used as a toggle to apply such configuration, I guess.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine adding these pinctrl entries

Ok, thank you. Can I get your approval on #64213 since it a dependency?

However, these entries should be of course optional, and the driver needs to be prepared to handle the case when they're not present, that is, handle the -ENOENT error case gracefully.

Perfect! I'll do it.

Also, the wakeup-source can be used as a toggle to apply such configuration, I guess.

This is what I want add later on another PR but that is the final idea.


user_button: button_1 {
label = "User Button";
gpios = <&pioa 2 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
Expand Down
78 changes: 44 additions & 34 deletions boards/arm/sam4s_xplained/sam4s_xplained-pinctrl.dtsi
Original file line number Diff line number Diff line change
@@ -1,59 +1,33 @@
/*
* Copyright (c) 2022, Gerson Fernando Budke <[email protected]>
* Copyright (c) 2022-2023, Gerson Fernando Budke <[email protected]>
* SPDX-License-Identifier: Apache-2.0
*/

#include <dt-bindings/pinctrl/sam4sXc-pinctrl.h>

&pinctrl {
spi0_default: spi0_default {
adc0_default: adc0_default {
group1 {
pinmux = <PA12A_SPI_MISO>,
<PA13A_SPI_MOSI>,
<PA14A_SPI_SPCK>,
<PA31A_SPI_NPCS1>,
<PA30B_SPI_NPCS2>;
pinmux = <PB0X_ADC_AD4>,
<PB1X_ADC_AD5>;
};
};

twi0_default: twi0_default {
gpio_keys_default: gpio_keys_default {
group1 {
pinmux = <PA4A_TWI0_TWCK>,
<PA3A_TWI0_TWD>;
pinmux = <PA5X_SUPC_WKUP4>;
bias-pull-up;
};
};

uart0_default: uart0_default {
group1 {
pinmux = <PA9A_UART0_RXD>,
<PA10A_UART0_TXD>;
};
};
uart1_default: uart1_default {
group1 {
pinmux = <PB2A_UART1_RXD>,
<PB3A_UART1_TXD>;
};
};
usart1_default: usart1_default {
group1 {
pinmux = <PA21A_USART1_RXD>,
<PA22A_USART1_TXD>;
};
};
pwm0_default: pwm0_default {
group1 {
pinmux = <PA12B_PWM_PWMH1>,
<PA13B_PWM_PWMH2>,
<PA14B_PWM_PWMH3>;
};
};
adc0_default: adc0_default {
group1 {
pinmux = <PB0X_ADC_AD4>,
<PB1X_ADC_AD5>;
};
};

smc_default: smc_default {
group1 {
pinmux = <PC18A_EBI_A0>,
Expand Down Expand Up @@ -89,4 +63,40 @@
<PC8A_EBI_NWE>;
};
};

spi0_default: spi0_default {
group1 {
pinmux = <PA12A_SPI_MISO>,
<PA13A_SPI_MOSI>,
<PA14A_SPI_SPCK>,
<PA31A_SPI_NPCS1>,
<PA30B_SPI_NPCS2>;
};
};

twi0_default: twi0_default {
group1 {
pinmux = <PA4A_TWI0_TWCK>,
<PA3A_TWI0_TWD>;
};
};

uart0_default: uart0_default {
group1 {
pinmux = <PA9A_UART0_RXD>,
<PA10A_UART0_TXD>;
};
};
uart1_default: uart1_default {
group1 {
pinmux = <PB2A_UART1_RXD>,
<PB3A_UART1_TXD>;
};
};
usart1_default: usart1_default {
group1 {
pinmux = <PA21A_USART1_RXD>,
<PA22A_USART1_TXD>;
};
};
};
6 changes: 6 additions & 0 deletions boards/arm/sam4s_xplained/sam4s_xplained.dts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/*
* Copyright (c) Justin Watson 2017
* Copyright (c) 2020-2023 Gerson Fernando Budke
*
* SPDX-License-Identifier: Apache-2.0
*/

Expand Down Expand Up @@ -58,6 +60,10 @@

gpio_keys {
compatible = "gpio-keys";

pinctrl-0 = <&gpio_keys_default>;
pinctrl-names = "default";

user_button: button_1 {
label = "User Button";
gpios = <&pioa 5 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
Expand Down
5 changes: 4 additions & 1 deletion boards/arm/sam_e70_xplained/sam_e70_xplained-common.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) 2017 Piotr Mienkowski
* Copyright (c) 2017 Justin Watson
* Copyright (c) 2020 Stephanos Ioannidis <[email protected]>
* Copyright (c) 2020 Gerson Fernando Budke <[email protected]>
* Copyright (c) 2020-2023 Gerson Fernando Budke <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -39,6 +39,9 @@
gpio_keys {
compatible = "gpio-keys";

pinctrl-0 = <&gpio_keys_default>;
pinctrl-names = "default";

/* The switch is labeled SW300 in the schematic, and labeled
* SW0 on the board, and labeld SW1 User Button on docs
*/
Expand Down
10 changes: 8 additions & 2 deletions boards/arm/sam_e70_xplained/sam_e70_xplained-pinctrl.dtsi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Gerson Fernando Budke <[email protected]>
* Copyright (c) 2022-2023, Gerson Fernando Budke <[email protected]>
* SPDX-License-Identifier: Apache-2.0
*/

Expand All @@ -13,7 +13,6 @@
<PA17X_AFE0_AD6>;
};
};

afec1_default: afec1_default { /* ADCH - J504 */
group1 {
pinmux = <PC31X_AFE1_AD6>;
Expand All @@ -40,6 +39,13 @@
};
};

gpio_keys_default: gpio_keys_default {
group1 {
pinmux = <PA11X_SUPC_WKUP7>;
bias-pull-up;
};
};

mdio_default: mdio_default {
group1 {
pinmux = <PD8A_GMAC_GMDC>,
Expand Down
5 changes: 4 additions & 1 deletion boards/arm/sam_v71_xult/sam_v71_xult-common.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) 2017 Piotr Mienkowski
* Copyright (c) 2017 Justin Watson
* Copyright (c) 2020 Stephanos Ioannidis <[email protected]>
* Copyright (c) 2019-2022 Gerson Fernando Budke <[email protected]>
* Copyright (c) 2019-2023 Gerson Fernando Budke <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -54,6 +54,9 @@
gpio_keys {
compatible = "gpio-keys";

pinctrl-0 = <&gpio_keys_default>;
pinctrl-names = "default";

/* The switch is labeled SW300/301 in the schematic, and
* labeled SW0 on the board, and labeled ERASE User Button
* on docs
Expand Down
8 changes: 7 additions & 1 deletion boards/arm/sam_v71_xult/sam_v71_xult-pinctrl.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<PA19X_AFE0_AD8>;
};
};

afec1_default: afec1_default {
group1 {
pinmux = <PC13X_AFE1_AD1>,
Expand Down Expand Up @@ -40,6 +39,13 @@
};
};

gpio_keys_default: gpio_keys_default {
group1 {
pinmux = <PA9X_SUPC_WKUP6>;
bias-pull-up;
};
};

mdio_default: mdio_default {
group1 {
pinmux = <PD8A_GMAC_GMDC>,
Expand Down
16 changes: 16 additions & 0 deletions drivers/input/input_gpio_keys.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2022 Google LLC
* Copyright (c) 2023 Gerson Fernando Budke <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -8,6 +9,9 @@

#include <zephyr/device.h>
#include <zephyr/drivers/gpio.h>
#ifdef CONFIG_PINCTRL
#include <zephyr/drivers/pinctrl.h>
#endif
#include <zephyr/input/input.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
Expand All @@ -31,6 +35,9 @@ struct gpio_keys_config {
uint32_t debounce_interval_ms;
const int num_keys;
const struct gpio_keys_pin_config *pin_cfg;
#ifdef CONFIG_PINCTRL
const struct pinctrl_dev_config *pinctrl_cfg;
#endif
};

struct gpio_keys_pin_data {
Expand Down Expand Up @@ -123,6 +130,13 @@ static int gpio_keys_init(const struct device *dev)
const struct gpio_keys_config *cfg = dev->config;
int ret;

#ifdef CONFIG_PINCTRL
ret = pinctrl_apply_state(cfg->pinctrl_cfg, PINCTRL_STATE_DEFAULT);
if (ret < 0) {
return ret;
}
Comment on lines +135 to +137
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will make the driver useless to any platform that has CONFIG_PINCTRL=y and no pinctrl entries for each gpio-keys subnode.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is your suggestion about how to handle it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see my comment above

#endif

for (int i = 0; i < cfg->num_keys; i++) {
const struct gpio_dt_spec *gpio = &cfg->pin_cfg[i].spec;

Expand Down Expand Up @@ -163,13 +177,15 @@ static int gpio_keys_init(const struct device *dev)
}

#define GPIO_KEYS_INIT(i) \
IF_ENABLED(CONFIG_PINCTRL, (PINCTRL_DT_INST_DEFINE(i);)) \
DT_INST_FOREACH_CHILD_STATUS_OKAY(i, GPIO_KEYS_CFG_CHECK); \
static const struct gpio_keys_pin_config gpio_keys_pin_config_##i[] = { \
DT_INST_FOREACH_CHILD_STATUS_OKAY_SEP(i, GPIO_KEYS_CFG_DEF, (,))}; \
static const struct gpio_keys_config gpio_keys_config_##i = { \
.debounce_interval_ms = DT_INST_PROP(i, debounce_interval_ms), \
.num_keys = ARRAY_SIZE(gpio_keys_pin_config_##i), \
.pin_cfg = gpio_keys_pin_config_##i, \
IF_ENABLED(CONFIG_PINCTRL, (.pinctrl_cfg = PINCTRL_DT_INST_DEV_CONFIG_GET(i),)) \
}; \
static struct gpio_keys_pin_data \
gpio_keys_pin_data_##i[ARRAY_SIZE(gpio_keys_pin_config_##i)]; \
Expand Down
6 changes: 5 additions & 1 deletion drivers/pinctrl/pinctrl_sam.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023, Gerson Fernando Budke <[email protected]>
* Copyright (c) 2022-2023 Gerson Fernando Budke <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -71,6 +71,10 @@ static void pinctrl_configure_pin(pinctrl_soc_pin_t pin)
if (port_func == SAM_PINMUX_FUNC_periph) {
soc_pin.flags |= (SAM_PINMUX_PERIPH_GET(pin)
<< SOC_GPIO_FUNC_POS);
} else if (port_func == SAM_PINMUX_FUNC_wakeup) {
soc_pin.flags |= (SAM_PINMUX_PERIPH_GET(pin)
<< SOC_GPIO_FUNC_POS)
| SAM_PINCTRL_WAKEUP;
}

soc_gpio_configure(&soc_pin);
Expand Down
2 changes: 1 addition & 1 deletion dts/bindings/input/gpio-keys.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ description: |

compatible: "gpio-keys"

include: base.yaml
include: pinctrl-device.yaml

properties:
debounce-interval-ms:
Expand Down
Loading
Loading