Philippe Reynes | 3483aa2 | 2020-07-24 18:19:45 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2020 Philippe Reynes <philippe.reynes@softathome.com> |
| 4 | * |
| 5 | * Based on led-uclass.c |
| 6 | */ |
| 7 | |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 8 | #define LOG_CATEGORY UCLASS_BUTTON |
| 9 | |
Philippe Reynes | 3483aa2 | 2020-07-24 18:19:45 +0200 | [diff] [blame] | 10 | #include <button.h> |
| 11 | #include <dm.h> |
| 12 | #include <dm/uclass-internal.h> |
Caleb Connolly | 8ebd17a | 2024-07-14 21:49:19 +0200 | [diff] [blame] | 13 | #include <dt-bindings/input/linux-event-codes.h> |
Philippe Reynes | 3483aa2 | 2020-07-24 18:19:45 +0200 | [diff] [blame] | 14 | |
| 15 | int button_get_by_label(const char *label, struct udevice **devp) |
| 16 | { |
| 17 | struct udevice *dev; |
| 18 | struct uclass *uc; |
| 19 | |
| 20 | uclass_id_foreach_dev(UCLASS_BUTTON, dev, uc) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 21 | struct button_uc_plat *uc_plat = dev_get_uclass_plat(dev); |
Philippe Reynes | 3483aa2 | 2020-07-24 18:19:45 +0200 | [diff] [blame] | 22 | |
| 23 | /* Ignore the top-level button node */ |
| 24 | if (uc_plat->label && !strcmp(label, uc_plat->label)) |
| 25 | return uclass_get_device_tail(dev, 0, devp); |
| 26 | } |
| 27 | |
| 28 | return -ENODEV; |
| 29 | } |
| 30 | |
| 31 | enum button_state_t button_get_state(struct udevice *dev) |
| 32 | { |
| 33 | struct button_ops *ops = button_get_ops(dev); |
| 34 | |
| 35 | if (!ops->get_state) |
| 36 | return -ENOSYS; |
| 37 | |
| 38 | return ops->get_state(dev); |
| 39 | } |
| 40 | |
Caleb Connolly | 8ebd17a | 2024-07-14 21:49:19 +0200 | [diff] [blame] | 41 | static int button_remap_phone_keys(int code) |
| 42 | { |
| 43 | switch (code) { |
| 44 | case KEY_VOLUMEUP: |
| 45 | return KEY_UP; |
| 46 | case KEY_VOLUMEDOWN: |
| 47 | return KEY_DOWN; |
| 48 | case KEY_POWER: |
| 49 | return KEY_ENTER; |
| 50 | default: |
| 51 | return code; |
| 52 | } |
| 53 | } |
| 54 | |
Dzmitry Sankouski | 157f2c5 | 2023-01-22 18:21:24 +0300 | [diff] [blame] | 55 | int button_get_code(struct udevice *dev) |
| 56 | { |
| 57 | struct button_ops *ops = button_get_ops(dev); |
Caleb Connolly | 8ebd17a | 2024-07-14 21:49:19 +0200 | [diff] [blame] | 58 | int code; |
Dzmitry Sankouski | 157f2c5 | 2023-01-22 18:21:24 +0300 | [diff] [blame] | 59 | |
| 60 | if (!ops->get_code) |
| 61 | return -ENOSYS; |
| 62 | |
Caleb Connolly | 8ebd17a | 2024-07-14 21:49:19 +0200 | [diff] [blame] | 63 | code = ops->get_code(dev); |
| 64 | if (CONFIG_IS_ENABLED(BUTTON_REMAP_PHONE_KEYS)) |
| 65 | return button_remap_phone_keys(code); |
| 66 | else |
| 67 | return code; |
Dzmitry Sankouski | 157f2c5 | 2023-01-22 18:21:24 +0300 | [diff] [blame] | 68 | } |
| 69 | |
Philippe Reynes | 3483aa2 | 2020-07-24 18:19:45 +0200 | [diff] [blame] | 70 | UCLASS_DRIVER(button) = { |
| 71 | .id = UCLASS_BUTTON, |
| 72 | .name = "button", |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 73 | .per_device_plat_auto = sizeof(struct button_uc_plat), |
Philippe Reynes | 3483aa2 | 2020-07-24 18:19:45 +0200 | [diff] [blame] | 74 | }; |