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 |
| 35 | * executed. |
| 36 | * |
| 37 | * - watchdog (output) |
| 38 | * Once a shut down is triggered, the driver will toggle this signal, |
| 39 | * with an internal (wde_interval) to stall the hardware shut down. |
| 40 | * |
| 41 | * - kill (output) |
| 42 | * The last action during shut down is triggering this signalling, such |
| 43 | * that the PowerPath Control will power down the hardware. |
| 44 | * |
| 45 | * ---------------------------------------- |
| 46 | * - Interrupts |
| 47 | * ---------------------------------------- |
| 48 | * |
| 49 | * The driver requires a non-shared, edge-triggered interrupt on the trigger |
| 50 | * GPIO. |
| 51 | * |
| 52 | */ |
| 53 | |
| 54 | #include <linux/kernel.h> |
| 55 | #include <linux/init.h> |
| 56 | #include <linux/interrupt.h> |
| 57 | #include <linux/device.h> |
| 58 | #include <linux/platform_device.h> |
| 59 | #include <linux/ktime.h> |
| 60 | #include <linux/slab.h> |
| 61 | #include <linux/kmod.h> |
| 62 | #include <linux/module.h> |
| 63 | #include <linux/gpio/consumer.h> |
| 64 | #include <linux/reboot.h> |
| 65 | |
| 66 | struct ltc2952_poweroff_data { |
| 67 | struct hrtimer timer_trigger; |
| 68 | struct hrtimer timer_wde; |
| 69 | |
| 70 | ktime_t trigger_delay; |
| 71 | ktime_t wde_interval; |
| 72 | |
| 73 | struct device *dev; |
| 74 | |
Frans Klaver | f66472d | 2015-01-14 09:15:36 +0100 | [diff] [blame^] | 75 | struct gpio_desc *gpio_trigger; |
| 76 | struct gpio_desc *gpio_watchdog; |
| 77 | struct gpio_desc *gpio_kill; |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | static int ltc2952_poweroff_panic; |
| 81 | static struct ltc2952_poweroff_data *ltc2952_data; |
| 82 | |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 83 | /** |
| 84 | * ltc2952_poweroff_timer_wde - Timer callback |
| 85 | * Toggles the watchdog reset signal each wde_interval |
| 86 | * |
| 87 | * @timer: corresponding timer |
| 88 | * |
| 89 | * Returns HRTIMER_RESTART for an infinite loop which will only stop when the |
| 90 | * machine actually shuts down |
| 91 | */ |
| 92 | static enum hrtimer_restart ltc2952_poweroff_timer_wde(struct hrtimer *timer) |
| 93 | { |
| 94 | ktime_t now; |
| 95 | int state; |
| 96 | unsigned long overruns; |
| 97 | |
| 98 | if (ltc2952_poweroff_panic) |
| 99 | return HRTIMER_NORESTART; |
| 100 | |
Frans Klaver | f66472d | 2015-01-14 09:15:36 +0100 | [diff] [blame^] | 101 | state = gpiod_get_value(ltc2952_data->gpio_watchdog); |
| 102 | gpiod_set_value(ltc2952_data->gpio_watchdog, !state); |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 103 | |
| 104 | now = hrtimer_cb_get_time(timer); |
| 105 | overruns = hrtimer_forward(timer, now, ltc2952_data->wde_interval); |
| 106 | |
| 107 | return HRTIMER_RESTART; |
| 108 | } |
| 109 | |
| 110 | static enum hrtimer_restart ltc2952_poweroff_timer_trigger( |
| 111 | struct hrtimer *timer) |
| 112 | { |
| 113 | int ret; |
| 114 | |
| 115 | ret = hrtimer_start(<c2952_data->timer_wde, |
Frans Klaver | f66472d | 2015-01-14 09:15:36 +0100 | [diff] [blame^] | 116 | ltc2952_data->wde_interval, HRTIMER_MODE_REL); |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 117 | |
| 118 | if (ret) { |
| 119 | dev_err(ltc2952_data->dev, "unable to start the timer\n"); |
| 120 | /* |
| 121 | * The device will not toggle the watchdog reset, |
| 122 | * thus shut down is only safe if the PowerPath controller |
| 123 | * has a long enough time-off before triggering a hardware |
| 124 | * power-off. |
| 125 | * |
| 126 | * Only sending a warning as the system will power-off anyway |
| 127 | */ |
| 128 | } |
| 129 | |
| 130 | dev_info(ltc2952_data->dev, "executing shutdown\n"); |
| 131 | |
| 132 | orderly_poweroff(true); |
| 133 | |
| 134 | return HRTIMER_NORESTART; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * ltc2952_poweroff_handler - Interrupt handler |
| 139 | * Triggered each time the trigger signal changes state and (de)activates a |
| 140 | * time-out (timer_trigger). Once the time-out is actually reached the shut |
| 141 | * down is executed. |
| 142 | * |
| 143 | * @irq: IRQ number |
| 144 | * @dev_id: pointer to the main data structure |
| 145 | */ |
| 146 | static irqreturn_t ltc2952_poweroff_handler(int irq, void *dev_id) |
| 147 | { |
| 148 | int ret; |
| 149 | struct ltc2952_poweroff_data *data = dev_id; |
| 150 | |
| 151 | if (ltc2952_poweroff_panic) |
| 152 | goto irq_ok; |
| 153 | |
| 154 | if (hrtimer_active(&data->timer_wde)) { |
| 155 | /* shutdown is already triggered, nothing to do any more */ |
| 156 | goto irq_ok; |
| 157 | } |
| 158 | |
| 159 | if (!hrtimer_active(&data->timer_trigger)) { |
| 160 | ret = hrtimer_start(&data->timer_trigger, data->trigger_delay, |
| 161 | HRTIMER_MODE_REL); |
| 162 | |
| 163 | if (ret) |
| 164 | dev_err(data->dev, "unable to start the wait timer\n"); |
| 165 | } else { |
| 166 | ret = hrtimer_cancel(&data->timer_trigger); |
| 167 | /* omitting return value check, timer should have been valid */ |
| 168 | } |
| 169 | |
| 170 | irq_ok: |
| 171 | return IRQ_HANDLED; |
| 172 | } |
| 173 | |
| 174 | static void ltc2952_poweroff_kill(void) |
| 175 | { |
Frans Klaver | f66472d | 2015-01-14 09:15:36 +0100 | [diff] [blame^] | 176 | gpiod_set_value(ltc2952_data->gpio_kill, 1); |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | static int ltc2952_poweroff_suspend(struct platform_device *pdev, |
| 180 | pm_message_t state) |
| 181 | { |
| 182 | return -ENOSYS; |
| 183 | } |
| 184 | |
| 185 | static int ltc2952_poweroff_resume(struct platform_device *pdev) |
| 186 | { |
| 187 | return -ENOSYS; |
| 188 | } |
| 189 | |
| 190 | static void ltc2952_poweroff_default(struct ltc2952_poweroff_data *data) |
| 191 | { |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 192 | data->wde_interval = ktime_set(0, 300L*1E6L); |
| 193 | data->trigger_delay = ktime_set(2, 500L*1E6L); |
| 194 | |
| 195 | hrtimer_init(&data->timer_trigger, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 196 | data->timer_trigger.function = <c2952_poweroff_timer_trigger; |
| 197 | |
| 198 | hrtimer_init(&data->timer_wde, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 199 | data->timer_wde.function = <c2952_poweroff_timer_wde; |
| 200 | } |
| 201 | |
| 202 | static int ltc2952_poweroff_init(struct platform_device *pdev) |
| 203 | { |
| 204 | int ret, virq; |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 205 | struct ltc2952_poweroff_data *data; |
| 206 | |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 207 | data = ltc2952_data; |
| 208 | ltc2952_poweroff_default(ltc2952_data); |
| 209 | |
Frans Klaver | f66472d | 2015-01-14 09:15:36 +0100 | [diff] [blame^] | 210 | ltc2952_data->gpio_watchdog = gpiod_get(&pdev->dev, "watchdog"); |
| 211 | if (IS_ERR(ltc2952_data->gpio_watchdog)) { |
| 212 | ret = PTR_ERR(ltc2952_data->gpio_watchdog); |
| 213 | dev_err(&pdev->dev, "unable to claim gpio \"watchdog\"\n"); |
| 214 | return ret; |
| 215 | } |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 216 | |
Frans Klaver | f66472d | 2015-01-14 09:15:36 +0100 | [diff] [blame^] | 217 | ltc2952_data->gpio_kill = gpiod_get(&pdev->dev, "kill"); |
| 218 | if (IS_ERR(ltc2952_data->gpio_kill)) { |
| 219 | ret = PTR_ERR(ltc2952_data->gpio_kill); |
| 220 | dev_err(&pdev->dev, "unable to claim gpio \"kill\"\n"); |
| 221 | goto err_kill; |
| 222 | } |
| 223 | |
| 224 | ltc2952_data->gpio_trigger = gpiod_get(&pdev->dev, "trigger"); |
| 225 | if (IS_ERR(ltc2952_data->gpio_trigger)) { |
| 226 | ret = PTR_ERR(ltc2952_data->gpio_trigger); |
| 227 | dev_err(&pdev->dev, "unable to claim gpio \"trigger\"\n"); |
| 228 | goto err_trigger; |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | ret = gpiod_direction_output( |
Frans Klaver | f66472d | 2015-01-14 09:15:36 +0100 | [diff] [blame^] | 232 | ltc2952_data->gpio_watchdog, 0); |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 233 | if (ret) { |
| 234 | dev_err(&pdev->dev, "unable to use watchdog-gpio as output\n"); |
| 235 | goto err_io; |
| 236 | } |
| 237 | |
Frans Klaver | f66472d | 2015-01-14 09:15:36 +0100 | [diff] [blame^] | 238 | ret = gpiod_direction_output(ltc2952_data->gpio_kill, 0); |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 239 | if (ret) { |
| 240 | dev_err(&pdev->dev, "unable to use kill-gpio as output\n"); |
| 241 | goto err_io; |
| 242 | } |
| 243 | |
Frans Klaver | f66472d | 2015-01-14 09:15:36 +0100 | [diff] [blame^] | 244 | virq = gpiod_to_irq(ltc2952_data->gpio_trigger); |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 245 | if (virq < 0) { |
| 246 | dev_err(&pdev->dev, "cannot map GPIO as interrupt"); |
| 247 | goto err_io; |
| 248 | } |
| 249 | |
Frans Klaver | 07d0823 | 2015-01-14 09:15:35 +0100 | [diff] [blame] | 250 | ret = devm_request_irq(&pdev->dev, virq, |
| 251 | ltc2952_poweroff_handler, |
| 252 | (IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING), |
| 253 | "ltc2952-poweroff", |
| 254 | ltc2952_data); |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 255 | |
| 256 | if (ret) { |
| 257 | dev_err(&pdev->dev, "cannot configure an interrupt handler\n"); |
| 258 | goto err_io; |
| 259 | } |
| 260 | |
| 261 | return 0; |
| 262 | |
| 263 | err_io: |
Frans Klaver | f66472d | 2015-01-14 09:15:36 +0100 | [diff] [blame^] | 264 | gpiod_put(ltc2952_data->gpio_trigger); |
| 265 | err_trigger: |
| 266 | gpiod_put(ltc2952_data->gpio_kill); |
| 267 | err_kill: |
| 268 | gpiod_put(ltc2952_data->gpio_watchdog); |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 269 | |
| 270 | return ret; |
| 271 | } |
| 272 | |
| 273 | static int ltc2952_poweroff_probe(struct platform_device *pdev) |
| 274 | { |
| 275 | int ret; |
| 276 | |
| 277 | if (pm_power_off) { |
| 278 | dev_err(&pdev->dev, "pm_power_off already registered"); |
| 279 | return -EBUSY; |
| 280 | } |
| 281 | |
Frans Klaver | 0a5c6a2 | 2015-01-14 09:15:34 +0100 | [diff] [blame] | 282 | ltc2952_data = devm_kzalloc(&pdev->dev, sizeof(*ltc2952_data), |
| 283 | GFP_KERNEL); |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 284 | if (!ltc2952_data) |
| 285 | return -ENOMEM; |
| 286 | |
| 287 | ltc2952_data->dev = &pdev->dev; |
| 288 | |
| 289 | ret = ltc2952_poweroff_init(pdev); |
| 290 | if (ret) |
Frans Klaver | 0a5c6a2 | 2015-01-14 09:15:34 +0100 | [diff] [blame] | 291 | return ret; |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 292 | |
| 293 | pm_power_off = <c2952_poweroff_kill; |
| 294 | |
| 295 | dev_info(&pdev->dev, "probe successful\n"); |
| 296 | |
| 297 | return 0; |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | static int ltc2952_poweroff_remove(struct platform_device *pdev) |
| 301 | { |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 302 | pm_power_off = NULL; |
| 303 | |
Frans Klaver | f66472d | 2015-01-14 09:15:36 +0100 | [diff] [blame^] | 304 | if (ltc2952_data) { |
| 305 | gpiod_put(ltc2952_data->gpio_trigger); |
| 306 | gpiod_put(ltc2952_data->gpio_watchdog); |
| 307 | gpiod_put(ltc2952_data->gpio_kill); |
| 308 | } |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static const struct of_device_id of_ltc2952_poweroff_match[] = { |
| 313 | { .compatible = "lltc,ltc2952"}, |
| 314 | {}, |
| 315 | }; |
| 316 | MODULE_DEVICE_TABLE(of, of_ltc2952_poweroff_match); |
| 317 | |
| 318 | static struct platform_driver ltc2952_poweroff_driver = { |
| 319 | .probe = ltc2952_poweroff_probe, |
| 320 | .remove = ltc2952_poweroff_remove, |
| 321 | .driver = { |
| 322 | .name = "ltc2952-poweroff", |
René Moll | 6647156 | 2014-08-08 13:12:17 +0000 | [diff] [blame] | 323 | .of_match_table = of_ltc2952_poweroff_match, |
| 324 | }, |
| 325 | .suspend = ltc2952_poweroff_suspend, |
| 326 | .resume = ltc2952_poweroff_resume, |
| 327 | }; |
| 328 | |
| 329 | static int ltc2952_poweroff_notify_panic(struct notifier_block *nb, |
| 330 | unsigned long code, void *unused) |
| 331 | { |
| 332 | ltc2952_poweroff_panic = 1; |
| 333 | return NOTIFY_DONE; |
| 334 | } |
| 335 | |
| 336 | static struct notifier_block ltc2952_poweroff_panic_nb = { |
| 337 | .notifier_call = ltc2952_poweroff_notify_panic, |
| 338 | }; |
| 339 | |
| 340 | static int __init ltc2952_poweroff_platform_init(void) |
| 341 | { |
| 342 | ltc2952_poweroff_panic = 0; |
| 343 | |
| 344 | atomic_notifier_chain_register(&panic_notifier_list, |
| 345 | <c2952_poweroff_panic_nb); |
| 346 | |
| 347 | return platform_driver_register(<c2952_poweroff_driver); |
| 348 | } |
| 349 | |
| 350 | static void __exit ltc2952_poweroff_platform_exit(void) |
| 351 | { |
| 352 | atomic_notifier_chain_unregister(&panic_notifier_list, |
| 353 | <c2952_poweroff_panic_nb); |
| 354 | |
| 355 | platform_driver_unregister(<c2952_poweroff_driver); |
| 356 | } |
| 357 | |
| 358 | module_init(ltc2952_poweroff_platform_init); |
| 359 | module_exit(ltc2952_poweroff_platform_exit); |
| 360 | |
| 361 | MODULE_AUTHOR("René Moll <rene.moll@xsens.com>"); |
| 362 | MODULE_DESCRIPTION("LTC PowerPath power-off driver"); |
| 363 | MODULE_LICENSE("GPL v2"); |