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. 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. 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 68 struct ltc2952_poweroff { 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 77 struct gpio_desc *gpio_trigger; 78 struct gpio_desc *gpio_watchdog; 79 struct gpio_desc *gpio_kill; 80 81 bool kernel_panic; 82 struct notifier_block panic_notifier; 83 }; 84 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; 92 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; 107 struct ltc2952_poweroff *data = to_ltc2952(timer, timer_wde); 108 109 if (data->kernel_panic) 110 return HRTIMER_NORESTART; 111 112 state = gpiod_get_value(data->gpio_watchdog); 113 gpiod_set_value(data->gpio_watchdog, !state); 114 115 now = hrtimer_cb_get_time(timer); 116 overruns = hrtimer_forward(timer, now, data->wde_interval); 117 118 return HRTIMER_RESTART; 119 } 120 121 static void ltc2952_poweroff_start_wde(struct ltc2952_poweroff *data) 122 { 123 if (hrtimer_start(&data->timer_wde, data->wde_interval, 124 HRTIMER_MODE_REL)) { 125 /* 126 * The device will not toggle the watchdog reset, 127 * thus shut down is only safe if the PowerPath controller 128 * has a long enough time-off before triggering a hardware 129 * power-off. 130 * 131 * Only sending a warning as the system will power-off anyway 132 */ 133 dev_err(data->dev, "unable to start the timer\n"); 134 } 135 } 136 137 static enum hrtimer_restart 138 ltc2952_poweroff_timer_trigger(struct hrtimer *timer) 139 { 140 struct ltc2952_poweroff *data = to_ltc2952(timer, timer_trigger); 141 142 ltc2952_poweroff_start_wde(data); 143 dev_info(data->dev, "executing shutdown\n"); 144 orderly_poweroff(true); 145 146 return HRTIMER_NORESTART; 147 } 148 149 /** 150 * ltc2952_poweroff_handler - Interrupt handler 151 * Triggered each time the trigger signal changes state and (de)activates a 152 * time-out (timer_trigger). Once the time-out is actually reached the shut 153 * down is executed. 154 * 155 * @irq: IRQ number 156 * @dev_id: pointer to the main data structure 157 */ 158 static irqreturn_t ltc2952_poweroff_handler(int irq, void *dev_id) 159 { 160 struct ltc2952_poweroff *data = dev_id; 161 162 if (data->kernel_panic || hrtimer_active(&data->timer_wde)) { 163 /* shutdown is already triggered, nothing to do any more */ 164 return IRQ_HANDLED; 165 } 166 167 if (gpiod_get_value(data->gpio_trigger)) { 168 if (hrtimer_start(&data->timer_trigger, data->trigger_delay, 169 HRTIMER_MODE_REL)) 170 dev_err(data->dev, "unable to start the wait timer\n"); 171 } else { 172 hrtimer_cancel(&data->timer_trigger); 173 /* omitting return value check, timer should have been valid */ 174 } 175 return IRQ_HANDLED; 176 } 177 178 static void ltc2952_poweroff_kill(void) 179 { 180 gpiod_set_value(ltc2952_data->gpio_kill, 1); 181 } 182 183 static void ltc2952_poweroff_default(struct ltc2952_poweroff *data) 184 { 185 data->wde_interval = ktime_set(0, 300L*1E6L); 186 data->trigger_delay = ktime_set(2, 500L*1E6L); 187 188 hrtimer_init(&data->timer_trigger, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 189 data->timer_trigger.function = ltc2952_poweroff_timer_trigger; 190 191 hrtimer_init(&data->timer_wde, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 192 data->timer_wde.function = ltc2952_poweroff_timer_wde; 193 } 194 195 static int ltc2952_poweroff_init(struct platform_device *pdev) 196 { 197 int ret; 198 struct ltc2952_poweroff *data = platform_get_drvdata(pdev); 199 200 ltc2952_poweroff_default(data); 201 202 data->gpio_watchdog = devm_gpiod_get(&pdev->dev, "watchdog", 203 GPIOD_OUT_LOW); 204 if (IS_ERR(data->gpio_watchdog)) { 205 ret = PTR_ERR(data->gpio_watchdog); 206 dev_err(&pdev->dev, "unable to claim gpio \"watchdog\"\n"); 207 return ret; 208 } 209 210 data->gpio_kill = devm_gpiod_get(&pdev->dev, "kill", GPIOD_OUT_LOW); 211 if (IS_ERR(data->gpio_kill)) { 212 ret = PTR_ERR(data->gpio_kill); 213 dev_err(&pdev->dev, "unable to claim gpio \"kill\"\n"); 214 return ret; 215 } 216 217 data->gpio_trigger = devm_gpiod_get(&pdev->dev, "trigger", GPIOD_IN); 218 if (IS_ERR(data->gpio_trigger)) { 219 /* 220 * It's not a problem if the trigger gpio isn't available, but 221 * it is worth a warning if its use was defined in the device 222 * tree. 223 */ 224 if (PTR_ERR(data->gpio_trigger) != -ENOENT) 225 dev_err(&pdev->dev, 226 "unable to claim gpio \"trigger\"\n"); 227 data->gpio_trigger = NULL; 228 } 229 230 if (devm_request_irq(&pdev->dev, gpiod_to_irq(data->gpio_trigger), 231 ltc2952_poweroff_handler, 232 (IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING), 233 "ltc2952-poweroff", 234 data)) { 235 /* 236 * Some things may have happened: 237 * - No trigger input was defined 238 * - Claiming the GPIO failed 239 * - We could not map to an IRQ 240 * - We couldn't register an interrupt handler 241 * 242 * None of these really are problems, but all of them 243 * disqualify the push button from controlling the power. 244 * 245 * It is therefore important to note that if the ltc2952 246 * detects a button press for long enough, it will still start 247 * its own powerdown window and cut the power on us if we don't 248 * start the watchdog trigger. 249 */ 250 if (data->gpio_trigger) { 251 dev_warn(&pdev->dev, 252 "unable to configure the trigger interrupt\n"); 253 devm_gpiod_put(&pdev->dev, data->gpio_trigger); 254 data->gpio_trigger = NULL; 255 } 256 dev_info(&pdev->dev, 257 "power down trigger input will not be used\n"); 258 ltc2952_poweroff_start_wde(data); 259 } 260 261 return 0; 262 } 263 264 static int ltc2952_poweroff_notify_panic(struct notifier_block *nb, 265 unsigned long code, void *unused) 266 { 267 struct ltc2952_poweroff *data = to_ltc2952(nb, panic_notifier); 268 269 data->kernel_panic = true; 270 return NOTIFY_DONE; 271 } 272 273 static int ltc2952_poweroff_probe(struct platform_device *pdev) 274 { 275 int ret; 276 struct ltc2952_poweroff *data; 277 278 if (pm_power_off) { 279 dev_err(&pdev->dev, "pm_power_off already registered"); 280 return -EBUSY; 281 } 282 283 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); 284 if (!data) 285 return -ENOMEM; 286 287 data->dev = &pdev->dev; 288 platform_set_drvdata(pdev, data); 289 290 ret = ltc2952_poweroff_init(pdev); 291 if (ret) 292 return ret; 293 294 /* TODO: remove ltc2952_data */ 295 ltc2952_data = data; 296 pm_power_off = ltc2952_poweroff_kill; 297 298 data->panic_notifier.notifier_call = ltc2952_poweroff_notify_panic; 299 atomic_notifier_chain_register(&panic_notifier_list, 300 &data->panic_notifier); 301 dev_info(&pdev->dev, "probe successful\n"); 302 303 return 0; 304 } 305 306 static int ltc2952_poweroff_remove(struct platform_device *pdev) 307 { 308 struct ltc2952_poweroff *data = platform_get_drvdata(pdev); 309 310 pm_power_off = NULL; 311 hrtimer_cancel(&data->timer_trigger); 312 hrtimer_cancel(&data->timer_wde); 313 atomic_notifier_chain_unregister(&panic_notifier_list, 314 &data->panic_notifier); 315 return 0; 316 } 317 318 static const struct of_device_id of_ltc2952_poweroff_match[] = { 319 { .compatible = "lltc,ltc2952"}, 320 {}, 321 }; 322 MODULE_DEVICE_TABLE(of, of_ltc2952_poweroff_match); 323 324 static struct platform_driver ltc2952_poweroff_driver = { 325 .probe = ltc2952_poweroff_probe, 326 .remove = ltc2952_poweroff_remove, 327 .driver = { 328 .name = "ltc2952-poweroff", 329 .of_match_table = of_ltc2952_poweroff_match, 330 }, 331 }; 332 333 module_platform_driver(ltc2952_poweroff_driver); 334 335 MODULE_AUTHOR("Ren� Moll <rene.moll@xsens.com>"); 336 MODULE_DESCRIPTION("LTC PowerPath power-off driver"); 337 MODULE_LICENSE("GPL v2"); 338