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> |
| 13 | |
| 14 | int button_get_by_label(const char *label, struct udevice **devp) |
| 15 | { |
| 16 | struct udevice *dev; |
| 17 | struct uclass *uc; |
| 18 | |
| 19 | uclass_id_foreach_dev(UCLASS_BUTTON, dev, uc) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 20 | struct button_uc_plat *uc_plat = dev_get_uclass_plat(dev); |
Philippe Reynes | 3483aa2 | 2020-07-24 18:19:45 +0200 | [diff] [blame] | 21 | |
| 22 | /* Ignore the top-level button node */ |
| 23 | if (uc_plat->label && !strcmp(label, uc_plat->label)) |
| 24 | return uclass_get_device_tail(dev, 0, devp); |
| 25 | } |
| 26 | |
| 27 | return -ENODEV; |
| 28 | } |
| 29 | |
| 30 | enum button_state_t button_get_state(struct udevice *dev) |
| 31 | { |
| 32 | struct button_ops *ops = button_get_ops(dev); |
| 33 | |
| 34 | if (!ops->get_state) |
| 35 | return -ENOSYS; |
| 36 | |
| 37 | return ops->get_state(dev); |
| 38 | } |
| 39 | |
Dzmitry Sankouski | 157f2c5 | 2023-01-22 18:21:24 +0300 | [diff] [blame] | 40 | int button_get_code(struct udevice *dev) |
| 41 | { |
| 42 | struct button_ops *ops = button_get_ops(dev); |
| 43 | |
| 44 | if (!ops->get_code) |
| 45 | return -ENOSYS; |
| 46 | |
| 47 | return ops->get_code(dev); |
| 48 | } |
| 49 | |
Philippe Reynes | 3483aa2 | 2020-07-24 18:19:45 +0200 | [diff] [blame] | 50 | UCLASS_DRIVER(button) = { |
| 51 | .id = UCLASS_BUTTON, |
| 52 | .name = "button", |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 53 | .per_device_plat_auto = sizeof(struct button_uc_plat), |
Philippe Reynes | 3483aa2 | 2020-07-24 18:19:45 +0200 | [diff] [blame] | 54 | }; |