blob: 6273ad3b411d8e32d33907225fb25455c19c6587 [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
Moritz Fischerd85b4f72018-02-22 15:17:14 -080022#define DEFAULT_TIMEOUT_MS 3000
Jamie Lentin96ff0f52012-11-17 09:51:04 +010023/*
24 * Hold configuration here, cannot be more than one instance of the driver
25 * since pm_power_off itself is global.
26 */
Linus Walleij86336ba2014-07-07 12:34:32 +020027static struct gpio_desc *reset_gpio;
Moritz Fischerd85b4f72018-02-22 15:17:14 -080028static u32 timeout = DEFAULT_TIMEOUT_MS;
Jamie Lentin96ff0f52012-11-17 09:51:04 +010029
30static void gpio_poweroff_do_poweroff(void)
31{
Linus Walleij86336ba2014-07-07 12:34:32 +020032 BUG_ON(!reset_gpio);
Jamie Lentin96ff0f52012-11-17 09:51:04 +010033
Andrew Lunn53435272013-01-06 11:10:35 +010034 /* drive it active, also inactive->active edge */
Linus Walleij86336ba2014-07-07 12:34:32 +020035 gpiod_direction_output(reset_gpio, 1);
Jamie Lentin96ff0f52012-11-17 09:51:04 +010036 mdelay(100);
Andrew Lunn53435272013-01-06 11:10:35 +010037 /* drive inactive, also active->inactive edge */
Linus Walleij86336ba2014-07-07 12:34:32 +020038 gpiod_set_value(reset_gpio, 0);
Jamie Lentin96ff0f52012-11-17 09:51:04 +010039 mdelay(100);
Andrew Lunn53435272013-01-06 11:10:35 +010040
41 /* drive it active, also inactive->active edge */
Linus Walleij86336ba2014-07-07 12:34:32 +020042 gpiod_set_value(reset_gpio, 1);
Jamie Lentin96ff0f52012-11-17 09:51:04 +010043
44 /* give it some time */
Moritz Fischerd85b4f72018-02-22 15:17:14 -080045 mdelay(timeout);
Jamie Lentin96ff0f52012-11-17 09:51:04 +010046
47 WARN_ON(1);
48}
49
Greg Kroah-Hartman6d2cea42012-12-21 15:04:54 -080050static int gpio_poweroff_probe(struct platform_device *pdev)
Jamie Lentin96ff0f52012-11-17 09:51:04 +010051{
Jamie Lentin96ff0f52012-11-17 09:51:04 +010052 bool input = false;
Uwe Kleine-Königa34c0a82015-05-18 22:45:06 +020053 enum gpiod_flags flags;
Jamie Lentin96ff0f52012-11-17 09:51:04 +010054
55 /* If a pm_power_off function has already been added, leave it alone */
56 if (pm_power_off != NULL) {
Linus Walleij86336ba2014-07-07 12:34:32 +020057 dev_err(&pdev->dev,
58 "%s: pm_power_off function already registered",
Jamie Lentin96ff0f52012-11-17 09:51:04 +010059 __func__);
60 return -EBUSY;
61 }
62
Moritz Fischerd85b4f72018-02-22 15:17:14 -080063 input = device_property_read_bool(&pdev->dev, "input");
Uwe Kleine-Königa34c0a82015-05-18 22:45:06 +020064 if (input)
65 flags = GPIOD_IN;
66 else
67 flags = GPIOD_OUT_LOW;
68
Moritz Fischerd85b4f72018-02-22 15:17:14 -080069 device_property_read_u32(&pdev->dev, "timeout-ms", &timeout);
70
Uwe Kleine-Königa34c0a82015-05-18 22:45:06 +020071 reset_gpio = devm_gpiod_get(&pdev->dev, NULL, flags);
Linus Walleij86336ba2014-07-07 12:34:32 +020072 if (IS_ERR(reset_gpio))
73 return PTR_ERR(reset_gpio);
Jamie Lentin96ff0f52012-11-17 09:51:04 +010074
Jamie Lentin96ff0f52012-11-17 09:51:04 +010075 pm_power_off = &gpio_poweroff_do_poweroff;
76 return 0;
Jamie Lentin96ff0f52012-11-17 09:51:04 +010077}
78
Greg Kroah-Hartman6d2cea42012-12-21 15:04:54 -080079static int gpio_poweroff_remove(struct platform_device *pdev)
Jamie Lentin96ff0f52012-11-17 09:51:04 +010080{
Jamie Lentin96ff0f52012-11-17 09:51:04 +010081 if (pm_power_off == &gpio_poweroff_do_poweroff)
82 pm_power_off = NULL;
83
84 return 0;
85}
86
87static const struct of_device_id of_gpio_poweroff_match[] = {
88 { .compatible = "gpio-poweroff", },
89 {},
90};
91
92static struct platform_driver gpio_poweroff_driver = {
93 .probe = gpio_poweroff_probe,
Greg Kroah-Hartman6d2cea42012-12-21 15:04:54 -080094 .remove = gpio_poweroff_remove,
Jamie Lentin96ff0f52012-11-17 09:51:04 +010095 .driver = {
Andrew Lunn53435272013-01-06 11:10:35 +010096 .name = "poweroff-gpio",
Andrew Lunn53435272013-01-06 11:10:35 +010097 .of_match_table = of_gpio_poweroff_match,
98 },
Jamie Lentin96ff0f52012-11-17 09:51:04 +010099};
100
101module_platform_driver(gpio_poweroff_driver);
102
103MODULE_AUTHOR("Jamie Lentin <jm@lentin.co.uk>");
104MODULE_DESCRIPTION("GPIO poweroff driver");
Andrew Lunn53435272013-01-06 11:10:35 +0100105MODULE_LICENSE("GPL v2");
Jamie Lentin96ff0f52012-11-17 09:51:04 +0100106MODULE_ALIAS("platform:poweroff-gpio");