René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * LTC2952 (PowerPath) driver |
| 3 | * |
| 4 | * Copyright (C) 2014, Xsens Technologies BV <info@xsens.com> |
| 5 | * Maintainer: René Moll <linux@r-moll.nl> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version 2 |
| 10 | * of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * ---------------------------------------- |
| 18 | * - Description |
| 19 | * ---------------------------------------- |
| 20 | * |
| 21 | * This driver is to be used with an external PowerPath Controller (LTC2952). |
| 22 | * Its function is to determine when a external shut down is triggered |
| 23 | * and react by properly shutting down the system. |
| 24 | * |
| 25 | * This driver expects a device tree with a ltc2952 entry for pin mapping. |
| 26 | * |
| 27 | * ---------------------------------------- |
| 28 | * - GPIO |
| 29 | * ---------------------------------------- |
| 30 | * |
| 31 | * The following GPIOs are used: |
| 32 | * - trigger (input) |
| 33 | * A level change indicates the shut-down trigger. If it's state reverts |
| 34 | * within the time-out defined by trigger_delay, the shut down is not |
Frans Klaver | ee18185 | 2015-01-14 09:15:45 +0100 | [diff] [blame] | 35 | * executed. If no pin is assigned to this input, the driver will start the |
| 36 | * watchdog toggle immediately. The chip will only power off the system if |
| 37 | * it is requested to do so through the kill line. |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 38 | * |
| 39 | * - watchdog (output) |
| 40 | * Once a shut down is triggered, the driver will toggle this signal, |
| 41 | * with an internal (wde_interval) to stall the hardware shut down. |
| 42 | * |
| 43 | * - kill (output) |
| 44 | * The last action during shut down is triggering this signalling, such |
| 45 | * that the PowerPath Control will power down the hardware. |
| 46 | * |
| 47 | * ---------------------------------------- |
| 48 | * - Interrupts |
| 49 | * ---------------------------------------- |
| 50 | * |
| 51 | * The driver requires a non-shared, edge-triggered interrupt on the trigger |
| 52 | * GPIO. |
| 53 | * |
| 54 | */ |
| 55 | |
| 56 | #include <linux/kernel.h> |
| 57 | #include <linux/init.h> |
| 58 | #include <linux/interrupt.h> |
| 59 | #include <linux/device.h> |
| 60 | #include <linux/platform_device.h> |
| 61 | #include <linux/ktime.h> |
| 62 | #include <linux/slab.h> |
| 63 | #include <linux/kmod.h> |
| 64 | #include <linux/module.h> |
| 65 | #include <linux/gpio/consumer.h> |
| 66 | #include <linux/reboot.h> |
| 67 | |
Frans Klaver | a5f67be | 2015-01-14 09:15:38 +0100 | [diff] [blame] | 68 | struct ltc2952_poweroff { |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 69 | struct hrtimer timer_trigger; |
| 70 | struct hrtimer timer_wde; |
| 71 | |
| 72 | ktime_t trigger_delay; |
| 73 | ktime_t wde_interval; |
| 74 | |
| 75 | struct device *dev; |
| 76 | |
Frans Klaver | f66472d | 2015-01-14 09:15:36 +0100 | [diff] [blame] | 77 | struct gpio_desc *gpio_trigger; |
| 78 | struct gpio_desc *gpio_watchdog; |
| 79 | struct gpio_desc *gpio_kill; |
Frans Klaver | 818ca4c | 2015-01-14 09:15:39 +0100 | [diff] [blame] | 80 | |
| 81 | bool kernel_panic; |
| 82 | struct notifier_block panic_notifier; |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
Frans Klaver | a5f67be | 2015-01-14 09:15:38 +0100 | [diff] [blame] | 85 | #define to_ltc2952(p, m) container_of(p, struct ltc2952_poweroff, m) |
| 86 | |
| 87 | /* |
| 88 | * This global variable is only needed for pm_power_off. We should |
| 89 | * remove it entirely once we don't need the global state anymore. |
| 90 | */ |
| 91 | static struct ltc2952_poweroff *ltc2952_data; |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 92 | |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 93 | /** |
| 94 | * ltc2952_poweroff_timer_wde - Timer callback |
| 95 | * Toggles the watchdog reset signal each wde_interval |
| 96 | * |
| 97 | * @timer: corresponding timer |
| 98 | * |
| 99 | * Returns HRTIMER_RESTART for an infinite loop which will only stop when the |
| 100 | * machine actually shuts down |
| 101 | */ |
| 102 | static enum hrtimer_restart ltc2952_poweroff_timer_wde(struct hrtimer *timer) |
| 103 | { |
| 104 | ktime_t now; |
| 105 | int state; |
| 106 | unsigned long overruns; |
Frans Klaver | a5f67be | 2015-01-14 09:15:38 +0100 | [diff] [blame] | 107 | struct ltc2952_poweroff *data = to_ltc2952(timer, timer_wde); |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 108 | |
Frans Klaver | 818ca4c | 2015-01-14 09:15:39 +0100 | [diff] [blame] | 109 | if (data->kernel_panic) |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 110 | return HRTIMER_NORESTART; |
| 111 | |
Frans Klaver | a5f67be | 2015-01-14 09:15:38 +0100 | [diff] [blame] | 112 | state = gpiod_get_value(data->gpio_watchdog); |
| 113 | gpiod_set_value(data->gpio_watchdog, !state); |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 114 | |
| 115 | now = hrtimer_cb_get_time(timer); |
Frans Klaver | a5f67be | 2015-01-14 09:15:38 +0100 | [diff] [blame] | 116 | overruns = hrtimer_forward(timer, now, data->wde_interval); |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 117 | |
| 118 | return HRTIMER_RESTART; |
| 119 | } |
| 120 | |
Frans Klaver | ee18185 | 2015-01-14 09:15:45 +0100 | [diff] [blame] | 121 | static void ltc2952_poweroff_start_wde(struct ltc2952_poweroff *data) |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 122 | { |
Thomas Gleixner | 4101ecc | 2015-04-13 16:43:35 +0200 | [diff] [blame] | 123 | hrtimer_start(&data->timer_wde, data->wde_interval, HRTIMER_MODE_REL); |
Frans Klaver | ee18185 | 2015-01-14 09:15:45 +0100 | [diff] [blame] | 124 | } |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 125 | |
Frans Klaver | ee18185 | 2015-01-14 09:15:45 +0100 | [diff] [blame] | 126 | static enum hrtimer_restart |
| 127 | ltc2952_poweroff_timer_trigger(struct hrtimer *timer) |
| 128 | { |
| 129 | struct ltc2952_poweroff *data = to_ltc2952(timer, timer_trigger); |
| 130 | |
| 131 | ltc2952_poweroff_start_wde(data); |
Frans Klaver | a5f67be | 2015-01-14 09:15:38 +0100 | [diff] [blame] | 132 | dev_info(data->dev, "executing shutdown\n"); |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 133 | orderly_poweroff(true); |
| 134 | |
| 135 | return HRTIMER_NORESTART; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * ltc2952_poweroff_handler - Interrupt handler |
| 140 | * Triggered each time the trigger signal changes state and (de)activates a |
| 141 | * time-out (timer_trigger). Once the time-out is actually reached the shut |
| 142 | * down is executed. |
| 143 | * |
| 144 | * @irq: IRQ number |
| 145 | * @dev_id: pointer to the main data structure |
| 146 | */ |
| 147 | static irqreturn_t ltc2952_poweroff_handler(int irq, void *dev_id) |
| 148 | { |
Frans Klaver | a5f67be | 2015-01-14 09:15:38 +0100 | [diff] [blame] | 149 | struct ltc2952_poweroff *data = dev_id; |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 150 | |
Frans Klaver | 5c3faad | 2015-01-14 09:15:41 +0100 | [diff] [blame] | 151 | if (data->kernel_panic || hrtimer_active(&data->timer_wde)) { |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 152 | /* shutdown is already triggered, nothing to do any more */ |
Frans Klaver | 5c3faad | 2015-01-14 09:15:41 +0100 | [diff] [blame] | 153 | return IRQ_HANDLED; |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Frans Klaver | c1ada2f | 2015-01-14 09:15:44 +0100 | [diff] [blame] | 156 | if (gpiod_get_value(data->gpio_trigger)) { |
Thomas Gleixner | 4101ecc | 2015-04-13 16:43:35 +0200 | [diff] [blame] | 157 | hrtimer_start(&data->timer_trigger, data->trigger_delay, |
| 158 | HRTIMER_MODE_REL); |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 159 | } else { |
Frans Klaver | 5c3faad | 2015-01-14 09:15:41 +0100 | [diff] [blame] | 160 | hrtimer_cancel(&data->timer_trigger); |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 161 | } |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 162 | return IRQ_HANDLED; |
| 163 | } |
| 164 | |
| 165 | static void ltc2952_poweroff_kill(void) |
| 166 | { |
Frans Klaver | f66472d | 2015-01-14 09:15:36 +0100 | [diff] [blame] | 167 | gpiod_set_value(ltc2952_data->gpio_kill, 1); |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Frans Klaver | a5f67be | 2015-01-14 09:15:38 +0100 | [diff] [blame] | 170 | static void ltc2952_poweroff_default(struct ltc2952_poweroff *data) |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 171 | { |
Nathan Chancellor | 23f4bbf | 2021-11-05 08:20:50 -0700 | [diff] [blame] | 172 | data->wde_interval = 300L * NSEC_PER_MSEC; |
| 173 | data->trigger_delay = ktime_set(2, 500L * NSEC_PER_MSEC); |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 174 | |
| 175 | hrtimer_init(&data->timer_trigger, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
Frans Klaver | 5689b78 | 2015-01-14 09:15:42 +0100 | [diff] [blame] | 176 | data->timer_trigger.function = ltc2952_poweroff_timer_trigger; |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 177 | |
| 178 | hrtimer_init(&data->timer_wde, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
Frans Klaver | 5689b78 | 2015-01-14 09:15:42 +0100 | [diff] [blame] | 179 | data->timer_wde.function = ltc2952_poweroff_timer_wde; |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static int ltc2952_poweroff_init(struct platform_device *pdev) |
| 183 | { |
Frans Klaver | ee18185 | 2015-01-14 09:15:45 +0100 | [diff] [blame] | 184 | int ret; |
Frans Klaver | a5f67be | 2015-01-14 09:15:38 +0100 | [diff] [blame] | 185 | struct ltc2952_poweroff *data = platform_get_drvdata(pdev); |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 186 | |
Frans Klaver | a5f67be | 2015-01-14 09:15:38 +0100 | [diff] [blame] | 187 | ltc2952_poweroff_default(data); |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 188 | |
Frans Klaver | a5f67be | 2015-01-14 09:15:38 +0100 | [diff] [blame] | 189 | data->gpio_watchdog = devm_gpiod_get(&pdev->dev, "watchdog", |
| 190 | GPIOD_OUT_LOW); |
| 191 | if (IS_ERR(data->gpio_watchdog)) { |
| 192 | ret = PTR_ERR(data->gpio_watchdog); |
Frans Klaver | f66472d | 2015-01-14 09:15:36 +0100 | [diff] [blame] | 193 | dev_err(&pdev->dev, "unable to claim gpio \"watchdog\"\n"); |
| 194 | return ret; |
| 195 | } |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 196 | |
Frans Klaver | a5f67be | 2015-01-14 09:15:38 +0100 | [diff] [blame] | 197 | data->gpio_kill = devm_gpiod_get(&pdev->dev, "kill", GPIOD_OUT_LOW); |
| 198 | if (IS_ERR(data->gpio_kill)) { |
| 199 | ret = PTR_ERR(data->gpio_kill); |
Frans Klaver | f66472d | 2015-01-14 09:15:36 +0100 | [diff] [blame] | 200 | dev_err(&pdev->dev, "unable to claim gpio \"kill\"\n"); |
Frans Klaver | 62113b3 | 2015-01-14 09:15:37 +0100 | [diff] [blame] | 201 | return ret; |
Frans Klaver | f66472d | 2015-01-14 09:15:36 +0100 | [diff] [blame] | 202 | } |
| 203 | |
Uwe Kleine-König | 9f6cd98 | 2015-05-18 22:45:07 +0200 | [diff] [blame] | 204 | data->gpio_trigger = devm_gpiod_get_optional(&pdev->dev, "trigger", |
| 205 | GPIOD_IN); |
Frans Klaver | ee18185 | 2015-01-14 09:15:45 +0100 | [diff] [blame] | 206 | if (IS_ERR(data->gpio_trigger)) { |
| 207 | /* |
| 208 | * It's not a problem if the trigger gpio isn't available, but |
| 209 | * it is worth a warning if its use was defined in the device |
| 210 | * tree. |
| 211 | */ |
Uwe Kleine-König | 9f6cd98 | 2015-05-18 22:45:07 +0200 | [diff] [blame] | 212 | dev_err(&pdev->dev, "unable to claim gpio \"trigger\"\n"); |
Frans Klaver | ee18185 | 2015-01-14 09:15:45 +0100 | [diff] [blame] | 213 | data->gpio_trigger = NULL; |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Frans Klaver | ee18185 | 2015-01-14 09:15:45 +0100 | [diff] [blame] | 216 | if (devm_request_irq(&pdev->dev, gpiod_to_irq(data->gpio_trigger), |
| 217 | ltc2952_poweroff_handler, |
| 218 | (IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING), |
| 219 | "ltc2952-poweroff", |
| 220 | data)) { |
| 221 | /* |
| 222 | * Some things may have happened: |
| 223 | * - No trigger input was defined |
| 224 | * - Claiming the GPIO failed |
| 225 | * - We could not map to an IRQ |
| 226 | * - We couldn't register an interrupt handler |
| 227 | * |
| 228 | * None of these really are problems, but all of them |
| 229 | * disqualify the push button from controlling the power. |
| 230 | * |
| 231 | * It is therefore important to note that if the ltc2952 |
| 232 | * detects a button press for long enough, it will still start |
| 233 | * its own powerdown window and cut the power on us if we don't |
| 234 | * start the watchdog trigger. |
| 235 | */ |
| 236 | if (data->gpio_trigger) { |
| 237 | dev_warn(&pdev->dev, |
| 238 | "unable to configure the trigger interrupt\n"); |
| 239 | devm_gpiod_put(&pdev->dev, data->gpio_trigger); |
| 240 | data->gpio_trigger = NULL; |
| 241 | } |
| 242 | dev_info(&pdev->dev, |
| 243 | "power down trigger input will not be used\n"); |
| 244 | ltc2952_poweroff_start_wde(data); |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | return 0; |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Frans Klaver | 818ca4c | 2015-01-14 09:15:39 +0100 | [diff] [blame] | 250 | static int ltc2952_poweroff_notify_panic(struct notifier_block *nb, |
| 251 | unsigned long code, void *unused) |
| 252 | { |
| 253 | struct ltc2952_poweroff *data = to_ltc2952(nb, panic_notifier); |
| 254 | |
| 255 | data->kernel_panic = true; |
| 256 | return NOTIFY_DONE; |
| 257 | } |
| 258 | |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 259 | static int ltc2952_poweroff_probe(struct platform_device *pdev) |
| 260 | { |
| 261 | int ret; |
Frans Klaver | a5f67be | 2015-01-14 09:15:38 +0100 | [diff] [blame] | 262 | struct ltc2952_poweroff *data; |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 263 | |
| 264 | if (pm_power_off) { |
| 265 | dev_err(&pdev->dev, "pm_power_off already registered"); |
| 266 | return -EBUSY; |
| 267 | } |
| 268 | |
Frans Klaver | a5f67be | 2015-01-14 09:15:38 +0100 | [diff] [blame] | 269 | data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); |
| 270 | if (!data) |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 271 | return -ENOMEM; |
| 272 | |
Frans Klaver | a5f67be | 2015-01-14 09:15:38 +0100 | [diff] [blame] | 273 | data->dev = &pdev->dev; |
| 274 | platform_set_drvdata(pdev, data); |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 275 | |
| 276 | ret = ltc2952_poweroff_init(pdev); |
| 277 | if (ret) |
Frans Klaver | 0a5c6a2 | 2015-01-14 09:15:34 +0100 | [diff] [blame] | 278 | return ret; |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 279 | |
Frans Klaver | a5f67be | 2015-01-14 09:15:38 +0100 | [diff] [blame] | 280 | /* TODO: remove ltc2952_data */ |
| 281 | ltc2952_data = data; |
Frans Klaver | 5689b78 | 2015-01-14 09:15:42 +0100 | [diff] [blame] | 282 | pm_power_off = ltc2952_poweroff_kill; |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 283 | |
Frans Klaver | 818ca4c | 2015-01-14 09:15:39 +0100 | [diff] [blame] | 284 | data->panic_notifier.notifier_call = ltc2952_poweroff_notify_panic; |
| 285 | atomic_notifier_chain_register(&panic_notifier_list, |
| 286 | &data->panic_notifier); |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 287 | dev_info(&pdev->dev, "probe successful\n"); |
| 288 | |
| 289 | return 0; |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static int ltc2952_poweroff_remove(struct platform_device *pdev) |
| 293 | { |
Frans Klaver | 818ca4c | 2015-01-14 09:15:39 +0100 | [diff] [blame] | 294 | struct ltc2952_poweroff *data = platform_get_drvdata(pdev); |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 295 | |
Frans Klaver | 818ca4c | 2015-01-14 09:15:39 +0100 | [diff] [blame] | 296 | pm_power_off = NULL; |
Frans Klaver | 2f6ea8a | 2015-01-14 09:15:43 +0100 | [diff] [blame] | 297 | hrtimer_cancel(&data->timer_trigger); |
| 298 | hrtimer_cancel(&data->timer_wde); |
Frans Klaver | 818ca4c | 2015-01-14 09:15:39 +0100 | [diff] [blame] | 299 | atomic_notifier_chain_unregister(&panic_notifier_list, |
| 300 | &data->panic_notifier); |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | static const struct of_device_id of_ltc2952_poweroff_match[] = { |
| 305 | { .compatible = "lltc,ltc2952"}, |
| 306 | {}, |
| 307 | }; |
| 308 | MODULE_DEVICE_TABLE(of, of_ltc2952_poweroff_match); |
| 309 | |
| 310 | static struct platform_driver ltc2952_poweroff_driver = { |
| 311 | .probe = ltc2952_poweroff_probe, |
| 312 | .remove = ltc2952_poweroff_remove, |
| 313 | .driver = { |
| 314 | .name = "ltc2952-poweroff", |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 315 | .of_match_table = of_ltc2952_poweroff_match, |
| 316 | }, |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 317 | }; |
| 318 | |
Frans Klaver | 818ca4c | 2015-01-14 09:15:39 +0100 | [diff] [blame] | 319 | module_platform_driver(ltc2952_poweroff_driver); |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 320 | |
| 321 | MODULE_AUTHOR("René Moll <rene.moll@xsens.com>"); |
| 322 | MODULE_DESCRIPTION("LTC PowerPath power-off driver"); |
| 323 | MODULE_LICENSE("GPL v2"); |