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 { 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 75 struct gpio_desc *gpio_trigger; 76 struct gpio_desc *gpio_watchdog; 77 struct gpio_desc *gpio_kill; 78 }; 79 80 #define to_ltc2952(p, m) container_of(p, struct ltc2952_poweroff, m) 81 82 /* 83 * This global variable is only needed for pm_power_off. We should 84 * remove it entirely once we don't need the global state anymore. 85 */ 86 static struct ltc2952_poweroff *ltc2952_data; 87 static int ltc2952_poweroff_panic; 88 89 /** 90 * ltc2952_poweroff_timer_wde - Timer callback 91 * Toggles the watchdog reset signal each wde_interval 92 * 93 * @timer: corresponding timer 94 * 95 * Returns HRTIMER_RESTART for an infinite loop which will only stop when the 96 * machine actually shuts down 97 */ 98 static enum hrtimer_restart ltc2952_poweroff_timer_wde(struct hrtimer *timer) 99 { 100 ktime_t now; 101 int state; 102 unsigned long overruns; 103 struct ltc2952_poweroff *data = to_ltc2952(timer, timer_wde); 104 105 if (ltc2952_poweroff_panic) 106 return HRTIMER_NORESTART; 107 108 state = gpiod_get_value(data->gpio_watchdog); 109 gpiod_set_value(data->gpio_watchdog, !state); 110 111 now = hrtimer_cb_get_time(timer); 112 overruns = hrtimer_forward(timer, now, data->wde_interval); 113 114 return HRTIMER_RESTART; 115 } 116 117 static enum hrtimer_restart 118 ltc2952_poweroff_timer_trigger(struct hrtimer *timer) 119 { 120 struct ltc2952_poweroff *data = to_ltc2952(timer, timer_trigger); 121 int ret = hrtimer_start(&data->timer_wde, 122 data->wde_interval, HRTIMER_MODE_REL); 123 124 if (ret) { 125 dev_err(data->dev, "unable to start the timer\n"); 126 /* 127 * The device will not toggle the watchdog reset, 128 * thus shut down is only safe if the PowerPath controller 129 * has a long enough time-off before triggering a hardware 130 * power-off. 131 * 132 * Only sending a warning as the system will power-off anyway 133 */ 134 } 135 136 dev_info(data->dev, "executing shutdown\n"); 137 138 orderly_poweroff(true); 139 140 return HRTIMER_NORESTART; 141 } 142 143 /** 144 * ltc2952_poweroff_handler - Interrupt handler 145 * Triggered each time the trigger signal changes state and (de)activates a 146 * time-out (timer_trigger). Once the time-out is actually reached the shut 147 * down is executed. 148 * 149 * @irq: IRQ number 150 * @dev_id: pointer to the main data structure 151 */ 152 static irqreturn_t ltc2952_poweroff_handler(int irq, void *dev_id) 153 { 154 int ret; 155 struct ltc2952_poweroff *data = dev_id; 156 157 if (ltc2952_poweroff_panic) 158 goto irq_ok; 159 160 if (hrtimer_active(&data->timer_wde)) { 161 /* shutdown is already triggered, nothing to do any more */ 162 goto irq_ok; 163 } 164 165 if (!hrtimer_active(&data->timer_trigger)) { 166 ret = hrtimer_start(&data->timer_trigger, data->trigger_delay, 167 HRTIMER_MODE_REL); 168 169 if (ret) 170 dev_err(data->dev, "unable to start the wait timer\n"); 171 } else { 172 ret = hrtimer_cancel(&data->timer_trigger); 173 /* omitting return value check, timer should have been valid */ 174 } 175 176 irq_ok: 177 return IRQ_HANDLED; 178 } 179 180 static void ltc2952_poweroff_kill(void) 181 { 182 gpiod_set_value(ltc2952_data->gpio_kill, 1); 183 } 184 185 static int ltc2952_poweroff_suspend(struct platform_device *pdev, 186 pm_message_t state) 187 { 188 return -ENOSYS; 189 } 190 191 static int ltc2952_poweroff_resume(struct platform_device *pdev) 192 { 193 return -ENOSYS; 194 } 195 196 static void ltc2952_poweroff_default(struct ltc2952_poweroff *data) 197 { 198 data->wde_interval = ktime_set(0, 300L*1E6L); 199 data->trigger_delay = ktime_set(2, 500L*1E6L); 200 201 hrtimer_init(&data->timer_trigger, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 202 data->timer_trigger.function = <c2952_poweroff_timer_trigger; 203 204 hrtimer_init(&data->timer_wde, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 205 data->timer_wde.function = <c2952_poweroff_timer_wde; 206 } 207 208 static int ltc2952_poweroff_init(struct platform_device *pdev) 209 { 210 int ret, virq; 211 struct ltc2952_poweroff *data = platform_get_drvdata(pdev); 212 213 ltc2952_poweroff_default(data); 214 215 data->gpio_watchdog = devm_gpiod_get(&pdev->dev, "watchdog", 216 GPIOD_OUT_LOW); 217 if (IS_ERR(data->gpio_watchdog)) { 218 ret = PTR_ERR(data->gpio_watchdog); 219 dev_err(&pdev->dev, "unable to claim gpio \"watchdog\"\n"); 220 return ret; 221 } 222 223 data->gpio_kill = devm_gpiod_get(&pdev->dev, "kill", GPIOD_OUT_LOW); 224 if (IS_ERR(data->gpio_kill)) { 225 ret = PTR_ERR(data->gpio_kill); 226 dev_err(&pdev->dev, "unable to claim gpio \"kill\"\n"); 227 return ret; 228 } 229 230 data->gpio_trigger = devm_gpiod_get(&pdev->dev, "trigger", 231 GPIOD_IN); 232 if (IS_ERR(ltc2952_data->gpio_trigger)) { 233 ret = PTR_ERR(ltc2952_data->gpio_trigger); 234 dev_err(&pdev->dev, "unable to claim gpio \"trigger\"\n"); 235 return ret; 236 } 237 238 virq = gpiod_to_irq(data->gpio_trigger); 239 if (virq < 0) { 240 dev_err(&pdev->dev, "cannot map GPIO as interrupt"); 241 return ret; 242 } 243 244 ret = devm_request_irq(&pdev->dev, virq, 245 ltc2952_poweroff_handler, 246 (IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING), 247 "ltc2952-poweroff", 248 data); 249 250 if (ret) { 251 dev_err(&pdev->dev, "cannot configure an interrupt handler\n"); 252 return ret; 253 } 254 255 return 0; 256 } 257 258 static int ltc2952_poweroff_probe(struct platform_device *pdev) 259 { 260 int ret; 261 struct ltc2952_poweroff *data; 262 263 if (pm_power_off) { 264 dev_err(&pdev->dev, "pm_power_off already registered"); 265 return -EBUSY; 266 } 267 268 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); 269 if (!data) 270 return -ENOMEM; 271 272 data->dev = &pdev->dev; 273 platform_set_drvdata(pdev, data); 274 275 ret = ltc2952_poweroff_init(pdev); 276 if (ret) 277 return ret; 278 279 /* TODO: remove ltc2952_data */ 280 ltc2952_data = data; 281 pm_power_off = <c2952_poweroff_kill; 282 283 dev_info(&pdev->dev, "probe successful\n"); 284 285 return 0; 286 } 287 288 static int ltc2952_poweroff_remove(struct platform_device *pdev) 289 { 290 pm_power_off = NULL; 291 292 return 0; 293 } 294 295 static const struct of_device_id of_ltc2952_poweroff_match[] = { 296 { .compatible = "lltc,ltc2952"}, 297 {}, 298 }; 299 MODULE_DEVICE_TABLE(of, of_ltc2952_poweroff_match); 300 301 static struct platform_driver ltc2952_poweroff_driver = { 302 .probe = ltc2952_poweroff_probe, 303 .remove = ltc2952_poweroff_remove, 304 .driver = { 305 .name = "ltc2952-poweroff", 306 .of_match_table = of_ltc2952_poweroff_match, 307 }, 308 .suspend = ltc2952_poweroff_suspend, 309 .resume = ltc2952_poweroff_resume, 310 }; 311 312 static int ltc2952_poweroff_notify_panic(struct notifier_block *nb, 313 unsigned long code, void *unused) 314 { 315 ltc2952_poweroff_panic = 1; 316 return NOTIFY_DONE; 317 } 318 319 static struct notifier_block ltc2952_poweroff_panic_nb = { 320 .notifier_call = ltc2952_poweroff_notify_panic, 321 }; 322 323 static int __init ltc2952_poweroff_platform_init(void) 324 { 325 ltc2952_poweroff_panic = 0; 326 327 atomic_notifier_chain_register(&panic_notifier_list, 328 <c2952_poweroff_panic_nb); 329 330 return platform_driver_register(<c2952_poweroff_driver); 331 } 332 333 static void __exit ltc2952_poweroff_platform_exit(void) 334 { 335 atomic_notifier_chain_unregister(&panic_notifier_list, 336 <c2952_poweroff_panic_nb); 337 338 platform_driver_unregister(<c2952_poweroff_driver); 339 } 340 341 module_init(ltc2952_poweroff_platform_init); 342 module_exit(ltc2952_poweroff_platform_exit); 343 344 MODULE_AUTHOR("Ren� Moll <rene.moll@xsens.com>"); 345 MODULE_DESCRIPTION("LTC PowerPath power-off driver"); 346 MODULE_LICENSE("GPL v2"); 347