blob: be3d81ff51cc3f510d85e4eed7a52960e51e7bc1 [file] [log] [blame]
Jamie Lentin96ff0f52012-11-17 09:51:04 +01001/*
2 * Toggles a GPIO pin to power down a device
3 *
4 * Jamie Lentin <jm@lentin.co.uk>
5 * Andrew Lunn <andrew@lunn.ch>
6 *
7 * Copyright (C) 2012 Jamie Lentin
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 */
14#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/delay.h>
17#include <linux/platform_device.h>
Linus Walleij86336ba2014-07-07 12:34:32 +020018#include <linux/gpio/consumer.h>
Jamie Lentin96ff0f52012-11-17 09:51:04 +010019#include <linux/of_platform.h>
Jamie Lentin96ff0f52012-11-17 09:51:04 +010020#include <linux/module.h>
21
22/*
23 * Hold configuration here, cannot be more than one instance of the driver
24 * since pm_power_off itself is global.
25 */
Linus Walleij86336ba2014-07-07 12:34:32 +020026static struct gpio_desc *reset_gpio;
Jamie Lentin96ff0f52012-11-17 09:51:04 +010027
28static void gpio_poweroff_do_poweroff(void)
29{
Linus Walleij86336ba2014-07-07 12:34:32 +020030 BUG_ON(!reset_gpio);
Jamie Lentin96ff0f52012-11-17 09:51:04 +010031
Andrew Lunn53435272013-01-06 11:10:35 +010032 /* drive it active, also inactive->active edge */
Linus Walleij86336ba2014-07-07 12:34:32 +020033 gpiod_direction_output(reset_gpio, 1);
Jamie Lentin96ff0f52012-11-17 09:51:04 +010034 mdelay(100);
Andrew Lunn53435272013-01-06 11:10:35 +010035 /* drive inactive, also active->inactive edge */
Linus Walleij86336ba2014-07-07 12:34:32 +020036 gpiod_set_value(reset_gpio, 0);
Jamie Lentin96ff0f52012-11-17 09:51:04 +010037 mdelay(100);
Andrew Lunn53435272013-01-06 11:10:35 +010038
39 /* drive it active, also inactive->active edge */
Linus Walleij86336ba2014-07-07 12:34:32 +020040 gpiod_set_value(reset_gpio, 1);
Jamie Lentin96ff0f52012-11-17 09:51:04 +010041
42 /* give it some time */
43 mdelay(3000);
44
45 WARN_ON(1);
46}
47
Greg Kroah-Hartman6d2cea42012-12-21 15:04:54 -080048static int gpio_poweroff_probe(struct platform_device *pdev)
Jamie Lentin96ff0f52012-11-17 09:51:04 +010049{
Jamie Lentin96ff0f52012-11-17 09:51:04 +010050 bool input = false;
Uwe Kleine-Königa34c0a82015-05-18 22:45:06 +020051 enum gpiod_flags flags;
Jamie Lentin96ff0f52012-11-17 09:51:04 +010052
53 /* If a pm_power_off function has already been added, leave it alone */
54 if (pm_power_off != NULL) {
Linus Walleij86336ba2014-07-07 12:34:32 +020055 dev_err(&pdev->dev,
56 "%s: pm_power_off function already registered",
Jamie Lentin96ff0f52012-11-17 09:51:04 +010057 __func__);
58 return -EBUSY;
59 }
60
Uwe Kleine-Königa34c0a82015-05-18 22:45:06 +020061 input = of_property_read_bool(pdev->dev.of_node, "input");
62 if (input)
63 flags = GPIOD_IN;
64 else
65 flags = GPIOD_OUT_LOW;
66
67 reset_gpio = devm_gpiod_get(&pdev->dev, NULL, flags);
Linus Walleij86336ba2014-07-07 12:34:32 +020068 if (IS_ERR(reset_gpio))
69 return PTR_ERR(reset_gpio);
Jamie Lentin96ff0f52012-11-17 09:51:04 +010070
Jamie Lentin96ff0f52012-11-17 09:51:04 +010071 pm_power_off = &gpio_poweroff_do_poweroff;
72 return 0;
Jamie Lentin96ff0f52012-11-17 09:51:04 +010073}
74
Greg Kroah-Hartman6d2cea42012-12-21 15:04:54 -080075static int gpio_poweroff_remove(struct platform_device *pdev)
Jamie Lentin96ff0f52012-11-17 09:51:04 +010076{
Jamie Lentin96ff0f52012-11-17 09:51:04 +010077 if (pm_power_off == &gpio_poweroff_do_poweroff)
78 pm_power_off = NULL;
79
80 return 0;
81}
82
83static const struct of_device_id of_gpio_poweroff_match[] = {
84 { .compatible = "gpio-poweroff", },
85 {},
86};
87
88static struct platform_driver gpio_poweroff_driver = {
89 .probe = gpio_poweroff_probe,
Greg Kroah-Hartman6d2cea42012-12-21 15:04:54 -080090 .remove = gpio_poweroff_remove,
Jamie Lentin96ff0f52012-11-17 09:51:04 +010091 .driver = {
Andrew Lunn53435272013-01-06 11:10:35 +010092 .name = "poweroff-gpio",
Andrew Lunn53435272013-01-06 11:10:35 +010093 .of_match_table = of_gpio_poweroff_match,
94 },
Jamie Lentin96ff0f52012-11-17 09:51:04 +010095};
96
97module_platform_driver(gpio_poweroff_driver);
98
99MODULE_AUTHOR("Jamie Lentin <jm@lentin.co.uk>");
100MODULE_DESCRIPTION("GPIO poweroff driver");
Andrew Lunn53435272013-01-06 11:10:35 +0100101MODULE_LICENSE("GPL v2");
Jamie Lentin96ff0f52012-11-17 09:51:04 +0100102MODULE_ALIAS("platform:poweroff-gpio");