ltc2952-poweroff.c 8.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
/*
 * LTC2952 (PowerPath) driver
 *
 * Copyright (C) 2014, Xsens Technologies BV <info@xsens.com>
 * Maintainer: Ren Moll <linux@r-moll.nl>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * ----------------------------------------
 * - Description
 * ----------------------------------------
 *
 * This driver is to be used with an external PowerPath Controller (LTC2952).
 * Its function is to determine when a external shut down is triggered
 * and react by properly shutting down the system.
 *
 * This driver expects a device tree with a ltc2952 entry for pin mapping.
 *
 * ----------------------------------------
 * - GPIO
 * ----------------------------------------
 *
 * The following GPIOs are used:
 * - trigger (input)
 *     A level change indicates the shut-down trigger. If it's state reverts
 *     within the time-out defined by trigger_delay, the shut down is not
 *     executed.
 *
 * - watchdog (output)
 *     Once a shut down is triggered, the driver will toggle this signal,
 *     with an internal (wde_interval) to stall the hardware shut down.
 *
 * - kill (output)
 *     The last action during shut down is triggering this signalling, such
 *     that the PowerPath Control will power down the hardware.
 *
 * ----------------------------------------
 * - Interrupts
 * ----------------------------------------
 *
 * The driver requires a non-shared, edge-triggered interrupt on the trigger
 * GPIO.
 *
 */

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/ktime.h>
#include <linux/slab.h>
#include <linux/kmod.h>
#include <linux/module.h>
#include <linux/gpio/consumer.h>
#include <linux/reboot.h>

66
struct ltc2952_poweroff {
67 68 69 70 71 72 73 74
	struct hrtimer timer_trigger;
	struct hrtimer timer_wde;

	ktime_t trigger_delay;
	ktime_t wde_interval;

	struct device *dev;

75 76 77
	struct gpio_desc *gpio_trigger;
	struct gpio_desc *gpio_watchdog;
	struct gpio_desc *gpio_kill;
78 79 80

	bool kernel_panic;
	struct notifier_block panic_notifier;
81 82
};

83 84 85 86 87 88 89
#define to_ltc2952(p, m) container_of(p, struct ltc2952_poweroff, m)

/*
 * This global variable is only needed for pm_power_off. We should
 * remove it entirely once we don't need the global state anymore.
 */
static struct ltc2952_poweroff *ltc2952_data;
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

/**
 * ltc2952_poweroff_timer_wde - Timer callback
 * Toggles the watchdog reset signal each wde_interval
 *
 * @timer: corresponding timer
 *
 * Returns HRTIMER_RESTART for an infinite loop which will only stop when the
 * machine actually shuts down
 */
static enum hrtimer_restart ltc2952_poweroff_timer_wde(struct hrtimer *timer)
{
	ktime_t now;
	int state;
	unsigned long overruns;
105
	struct ltc2952_poweroff *data = to_ltc2952(timer, timer_wde);
106

107
	if (data->kernel_panic)
108 109
		return HRTIMER_NORESTART;

110 111
	state = gpiod_get_value(data->gpio_watchdog);
	gpiod_set_value(data->gpio_watchdog, !state);
112 113

	now = hrtimer_cb_get_time(timer);
114
	overruns = hrtimer_forward(timer, now, data->wde_interval);
115 116 117 118

	return HRTIMER_RESTART;
}

119 120
static enum hrtimer_restart
ltc2952_poweroff_timer_trigger(struct hrtimer *timer)
121
{
122 123 124
	struct ltc2952_poweroff *data = to_ltc2952(timer, timer_trigger);
	int ret = hrtimer_start(&data->timer_wde,
				data->wde_interval, HRTIMER_MODE_REL);
125 126

	if (ret) {
127
		dev_err(data->dev, "unable to start the timer\n");
128 129 130 131 132 133 134 135 136 137
		/*
		 * The device will not toggle the watchdog reset,
		 * thus shut down is only safe if the PowerPath controller
		 * has a long enough time-off before triggering a hardware
		 * power-off.
		 *
		 * Only sending a warning as the system will power-off anyway
		 */
	}

138
	dev_info(data->dev, "executing shutdown\n");
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

	orderly_poweroff(true);

	return HRTIMER_NORESTART;
}

/**
 * ltc2952_poweroff_handler - Interrupt handler
 * Triggered each time the trigger signal changes state and (de)activates a
 * time-out (timer_trigger). Once the time-out is actually reached the shut
 * down is executed.
 *
 * @irq: IRQ number
 * @dev_id: pointer to the main data structure
 */
static irqreturn_t ltc2952_poweroff_handler(int irq, void *dev_id)
{
156
	struct ltc2952_poweroff *data = dev_id;
157

158
	if (data->kernel_panic || hrtimer_active(&data->timer_wde)) {
159
		/* shutdown is already triggered, nothing to do any more */
160
		return IRQ_HANDLED;
161 162
	}

163
	if (gpiod_get_value(data->gpio_trigger)) {
164 165
		if (hrtimer_start(&data->timer_trigger, data->trigger_delay,
				  HRTIMER_MODE_REL))
166 167
			dev_err(data->dev, "unable to start the wait timer\n");
	} else {
168
		hrtimer_cancel(&data->timer_trigger);
169 170 171 172 173 174 175
		/* omitting return value check, timer should have been valid */
	}
	return IRQ_HANDLED;
}

static void ltc2952_poweroff_kill(void)
{
176
	gpiod_set_value(ltc2952_data->gpio_kill, 1);
177 178
}

179
static void ltc2952_poweroff_default(struct ltc2952_poweroff *data)
180 181 182 183 184
{
	data->wde_interval = ktime_set(0, 300L*1E6L);
	data->trigger_delay = ktime_set(2, 500L*1E6L);

	hrtimer_init(&data->timer_trigger, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
185
	data->timer_trigger.function = ltc2952_poweroff_timer_trigger;
186 187

	hrtimer_init(&data->timer_wde, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
188
	data->timer_wde.function = ltc2952_poweroff_timer_wde;
189 190 191 192 193
}

static int ltc2952_poweroff_init(struct platform_device *pdev)
{
	int ret, virq;
194
	struct ltc2952_poweroff *data = platform_get_drvdata(pdev);
195

196
	ltc2952_poweroff_default(data);
197

198 199 200 201
	data->gpio_watchdog = devm_gpiod_get(&pdev->dev, "watchdog",
					     GPIOD_OUT_LOW);
	if (IS_ERR(data->gpio_watchdog)) {
		ret = PTR_ERR(data->gpio_watchdog);
202 203 204
		dev_err(&pdev->dev, "unable to claim gpio \"watchdog\"\n");
		return ret;
	}
205

206 207 208
	data->gpio_kill = devm_gpiod_get(&pdev->dev, "kill", GPIOD_OUT_LOW);
	if (IS_ERR(data->gpio_kill)) {
		ret = PTR_ERR(data->gpio_kill);
209
		dev_err(&pdev->dev, "unable to claim gpio \"kill\"\n");
210
		return ret;
211 212
	}

213 214
	data->gpio_trigger = devm_gpiod_get(&pdev->dev, "trigger",
					    GPIOD_IN);
215 216 217
	if (IS_ERR(ltc2952_data->gpio_trigger)) {
		ret = PTR_ERR(ltc2952_data->gpio_trigger);
		dev_err(&pdev->dev, "unable to claim gpio \"trigger\"\n");
218
		return ret;
219 220
	}

221
	virq = gpiod_to_irq(data->gpio_trigger);
222 223
	if (virq < 0) {
		dev_err(&pdev->dev, "cannot map GPIO as interrupt");
224
		return ret;
225 226
	}

227 228 229 230
	ret = devm_request_irq(&pdev->dev, virq,
			       ltc2952_poweroff_handler,
			       (IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING),
			       "ltc2952-poweroff",
231
			       data);
232 233 234

	if (ret) {
		dev_err(&pdev->dev, "cannot configure an interrupt handler\n");
235
		return ret;
236 237 238 239 240
	}

	return 0;
}

241 242 243 244 245 246 247 248 249
static int ltc2952_poweroff_notify_panic(struct notifier_block *nb,
					 unsigned long code, void *unused)
{
	struct ltc2952_poweroff *data = to_ltc2952(nb, panic_notifier);

	data->kernel_panic = true;
	return NOTIFY_DONE;
}

250 251 252
static int ltc2952_poweroff_probe(struct platform_device *pdev)
{
	int ret;
253
	struct ltc2952_poweroff *data;
254 255 256 257 258 259

	if (pm_power_off) {
		dev_err(&pdev->dev, "pm_power_off already registered");
		return -EBUSY;
	}

260 261
	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
	if (!data)
262 263
		return -ENOMEM;

264 265
	data->dev = &pdev->dev;
	platform_set_drvdata(pdev, data);
266 267 268

	ret = ltc2952_poweroff_init(pdev);
	if (ret)
269
		return ret;
270

271 272
	/* TODO: remove ltc2952_data */
	ltc2952_data = data;
273
	pm_power_off = ltc2952_poweroff_kill;
274

275 276 277
	data->panic_notifier.notifier_call = ltc2952_poweroff_notify_panic;
	atomic_notifier_chain_register(&panic_notifier_list,
				       &data->panic_notifier);
278 279 280 281 282 283 284
	dev_info(&pdev->dev, "probe successful\n");

	return 0;
}

static int ltc2952_poweroff_remove(struct platform_device *pdev)
{
285
	struct ltc2952_poweroff *data = platform_get_drvdata(pdev);
286

287
	pm_power_off = NULL;
288 289
	hrtimer_cancel(&data->timer_trigger);
	hrtimer_cancel(&data->timer_wde);
290 291
	atomic_notifier_chain_unregister(&panic_notifier_list,
					 &data->panic_notifier);
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
	return 0;
}

static const struct of_device_id of_ltc2952_poweroff_match[] = {
	{ .compatible = "lltc,ltc2952"},
	{},
};
MODULE_DEVICE_TABLE(of, of_ltc2952_poweroff_match);

static struct platform_driver ltc2952_poweroff_driver = {
	.probe = ltc2952_poweroff_probe,
	.remove = ltc2952_poweroff_remove,
	.driver = {
		.name = "ltc2952-poweroff",
		.of_match_table = of_ltc2952_poweroff_match,
	},
};

310
module_platform_driver(ltc2952_poweroff_driver);
311 312 313 314

MODULE_AUTHOR("Ren Moll <rene.moll@xsens.com>");
MODULE_DESCRIPTION("LTC PowerPath power-off driver");
MODULE_LICENSE("GPL v2");