Rasmus Villemoes | 0866760 | 2021-08-19 11:57:04 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | |
| 3 | #include <dm.h> |
| 4 | #include <dm/device_compat.h> |
| 5 | #include <wdt.h> |
| 6 | #include <asm/gpio.h> |
Paul Doelle | 709f037 | 2022-07-04 09:00:25 +0000 | [diff] [blame] | 7 | #include <linux/delay.h> |
| 8 | |
| 9 | enum { |
| 10 | HW_ALGO_TOGGLE, |
| 11 | HW_ALGO_LEVEL, |
| 12 | }; |
Rasmus Villemoes | 0866760 | 2021-08-19 11:57:04 +0200 | [diff] [blame] | 13 | |
| 14 | struct gpio_wdt_priv { |
Paul Doelle | 709f037 | 2022-07-04 09:00:25 +0000 | [diff] [blame] | 15 | struct gpio_desc gpio; |
| 16 | unsigned int hw_algo; |
| 17 | bool always_running; |
| 18 | int state; |
Rasmus Villemoes | 0866760 | 2021-08-19 11:57:04 +0200 | [diff] [blame] | 19 | }; |
| 20 | |
| 21 | static int gpio_wdt_reset(struct udevice *dev) |
| 22 | { |
| 23 | struct gpio_wdt_priv *priv = dev_get_priv(dev); |
| 24 | |
Paul Doelle | 709f037 | 2022-07-04 09:00:25 +0000 | [diff] [blame] | 25 | switch (priv->hw_algo) { |
| 26 | case HW_ALGO_TOGGLE: |
| 27 | /* Toggle output pin */ |
| 28 | priv->state = !priv->state; |
| 29 | dm_gpio_set_value(&priv->gpio, priv->state); |
| 30 | break; |
| 31 | case HW_ALGO_LEVEL: |
| 32 | /* Pulse */ |
| 33 | dm_gpio_set_value(&priv->gpio, 1); |
Rasmus Villemoes | 21df251 | 2022-09-27 09:45:44 +0200 | [diff] [blame] | 34 | __udelay(1); |
Paul Doelle | 709f037 | 2022-07-04 09:00:25 +0000 | [diff] [blame] | 35 | dm_gpio_set_value(&priv->gpio, 0); |
| 36 | break; |
| 37 | } |
| 38 | return 0; |
Rasmus Villemoes | 0866760 | 2021-08-19 11:57:04 +0200 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | static int gpio_wdt_start(struct udevice *dev, u64 timeout, ulong flags) |
| 42 | { |
| 43 | struct gpio_wdt_priv *priv = dev_get_priv(dev); |
| 44 | |
| 45 | if (priv->always_running) |
| 46 | return 0; |
| 47 | |
Rasmus Villemoes | 776442e | 2024-10-02 21:23:23 +0200 | [diff] [blame] | 48 | dm_gpio_set_dir_flags(&priv->gpio, GPIOD_IS_OUT); |
| 49 | gpio_wdt_reset(dev); |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static int gpio_wdt_stop(struct udevice *dev) |
| 55 | { |
| 56 | struct gpio_wdt_priv *priv = dev_get_priv(dev); |
| 57 | |
| 58 | if (priv->always_running) |
| 59 | return -EOPNOTSUPP; |
| 60 | |
| 61 | if (priv->hw_algo == HW_ALGO_TOGGLE) |
| 62 | dm_gpio_set_dir_flags(&priv->gpio, GPIOD_IS_IN); |
| 63 | else |
| 64 | dm_gpio_set_value(&priv->gpio, 1); |
| 65 | |
| 66 | return 0; |
Rasmus Villemoes | 0866760 | 2021-08-19 11:57:04 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static int dm_probe(struct udevice *dev) |
| 70 | { |
| 71 | struct gpio_wdt_priv *priv = dev_get_priv(dev); |
Paul Doelle | 709f037 | 2022-07-04 09:00:25 +0000 | [diff] [blame] | 72 | const char *algo = dev_read_string(dev, "hw_algo"); |
Rasmus Villemoes | 776442e | 2024-10-02 21:23:23 +0200 | [diff] [blame] | 73 | int ret, flags; |
Paul Doelle | 709f037 | 2022-07-04 09:00:25 +0000 | [diff] [blame] | 74 | |
| 75 | if (!algo) |
| 76 | return -EINVAL; |
| 77 | if (!strcmp(algo, "toggle")) |
| 78 | priv->hw_algo = HW_ALGO_TOGGLE; |
| 79 | else if (!strcmp(algo, "level")) |
| 80 | priv->hw_algo = HW_ALGO_LEVEL; |
| 81 | else |
| 82 | return -EINVAL; |
Rasmus Villemoes | 0866760 | 2021-08-19 11:57:04 +0200 | [diff] [blame] | 83 | |
| 84 | priv->always_running = dev_read_bool(dev, "always-running"); |
Rasmus Villemoes | 776442e | 2024-10-02 21:23:23 +0200 | [diff] [blame] | 85 | flags = priv->always_running || priv->hw_algo == HW_ALGO_LEVEL ? |
| 86 | GPIOD_IS_OUT : GPIOD_IS_IN; |
| 87 | ret = gpio_request_by_name(dev, "gpios", 0, &priv->gpio, flags); |
Rasmus Villemoes | 0866760 | 2021-08-19 11:57:04 +0200 | [diff] [blame] | 88 | if (ret < 0) { |
| 89 | dev_err(dev, "Request for wdt gpio failed: %d\n", ret); |
| 90 | return ret; |
| 91 | } |
| 92 | |
| 93 | if (priv->always_running) |
| 94 | ret = gpio_wdt_reset(dev); |
| 95 | |
| 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | static const struct wdt_ops gpio_wdt_ops = { |
| 100 | .start = gpio_wdt_start, |
Rasmus Villemoes | 776442e | 2024-10-02 21:23:23 +0200 | [diff] [blame] | 101 | .stop = gpio_wdt_stop, |
Rasmus Villemoes | 0866760 | 2021-08-19 11:57:04 +0200 | [diff] [blame] | 102 | .reset = gpio_wdt_reset, |
| 103 | }; |
| 104 | |
| 105 | static const struct udevice_id gpio_wdt_ids[] = { |
| 106 | { .compatible = "linux,wdt-gpio" }, |
| 107 | {} |
| 108 | }; |
| 109 | |
| 110 | U_BOOT_DRIVER(wdt_gpio) = { |
| 111 | .name = "wdt_gpio", |
| 112 | .id = UCLASS_WDT, |
| 113 | .of_match = gpio_wdt_ids, |
| 114 | .ops = &gpio_wdt_ops, |
| 115 | .probe = dm_probe, |
| 116 | .priv_auto = sizeof(struct gpio_wdt_priv), |
| 117 | }; |