Skip to content

Commit

Permalink
dm: button: add support for linux_code in button-gpio.c driver
Browse files Browse the repository at this point in the history
Linux event code must be used in input devices, using buttons.

Signed-off-by: Dzmitry Sankouski <dsankouski@gmail.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
  • Loading branch information
Dzmitry Sankouski authored and trini committed Feb 10, 2023
1 parent 298ffdd commit ea6fdc1
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
2 changes: 2 additions & 0 deletions arch/sandbox/dts/test.dts
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,13 @@
btn1 {
gpios = <&gpio_a 3 0>;
label = "button1";
linux,code = <BTN_1>;
};

btn2 {
gpios = <&gpio_a 4 0>;
label = "button2";
linux,code = <BTN_2>;
};
};

Expand Down
17 changes: 16 additions & 1 deletion drivers/button/button-gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

struct button_gpio_priv {
struct gpio_desc gpio;
int linux_code;
};

static enum button_state_t button_gpio_get_state(struct udevice *dev)
Expand All @@ -29,6 +30,17 @@ static enum button_state_t button_gpio_get_state(struct udevice *dev)
return ret ? BUTTON_ON : BUTTON_OFF;
}

static int button_gpio_get_code(struct udevice *dev)
{
struct button_gpio_priv *priv = dev_get_priv(dev);
int code = priv->linux_code;

if (!code)
return -ENODATA;

return code;
}

static int button_gpio_probe(struct udevice *dev)
{
struct button_uc_plat *uc_plat = dev_get_uclass_plat(dev);
Expand All @@ -43,7 +55,9 @@ static int button_gpio_probe(struct udevice *dev)
if (ret)
return ret;

return 0;
ret = dev_read_u32(dev, "linux,code", &priv->linux_code);

return ret;
}

static int button_gpio_remove(struct udevice *dev)
Expand Down Expand Up @@ -92,6 +106,7 @@ static int button_gpio_bind(struct udevice *parent)

static const struct button_ops button_gpio_ops = {
.get_state = button_gpio_get_state,
.get_code = button_gpio_get_code,
};

static const struct udevice_id button_gpio_ids[] = {
Expand Down
10 changes: 10 additions & 0 deletions drivers/button/button-uclass.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ enum button_state_t button_get_state(struct udevice *dev)
return ops->get_state(dev);
}

int button_get_code(struct udevice *dev)
{
struct button_ops *ops = button_get_ops(dev);

if (!ops->get_code)
return -ENOSYS;

return ops->get_code(dev);
}

UCLASS_DRIVER(button) = {
.id = UCLASS_BUTTON,
.name = "button",
Expand Down
16 changes: 16 additions & 0 deletions include/button.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ struct button_ops {
* @return button state button_state_t, or -ve on error
*/
enum button_state_t (*get_state)(struct udevice *dev);

/**
* get_code() - get linux event code of a button
*
* @dev: button device to change
* @return button code, or -ENODATA on error
*/
int (*get_code)(struct udevice *dev);
};

#define button_get_ops(dev) ((struct button_ops *)(dev)->driver->ops)
Expand All @@ -58,4 +66,12 @@ int button_get_by_label(const char *label, struct udevice **devp);
*/
enum button_state_t button_get_state(struct udevice *dev);

/**
* button_get_code() - get linux event code of a button
*
* @dev: button device to change
* @return button code, or -ve on error
*/
int button_get_code(struct udevice *dev);

#endif
13 changes: 13 additions & 0 deletions test/dm/button.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <power/sandbox_pmic.h>
#include <asm/gpio.h>
#include <dm/test.h>
#include <dt-bindings/input/input.h>
#include <test/ut.h>

/* Base test of the button uclass */
Expand Down Expand Up @@ -85,6 +86,18 @@ static int dm_test_button_label(struct unit_test_state *uts)
}
DM_TEST(dm_test_button_label, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);

/* Test button has linux,code */
static int dm_test_button_linux_code(struct unit_test_state *uts)
{
struct udevice *dev;

ut_assertok(uclass_get_device(UCLASS_BUTTON, 1, &dev));
ut_asserteq(BTN_1, button_get_code(dev));

return 0;
}
DM_TEST(dm_test_button_linux_code, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);

/* Test adc-keys driver */
static int dm_test_button_keys_adc(struct unit_test_state *uts)
{
Expand Down

0 comments on commit ea6fdc1

Please sign in to comment.