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