Merge tag 'media/v5.1-1' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[linux-2.6-block.git] / drivers / power / reset / gpio-poweroff.c
CommitLineData
96ff0f5c
JL
1/*
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>
86336ba4 18#include <linux/gpio/consumer.h>
96ff0f5c 19#include <linux/of_platform.h>
96ff0f5c
JL
20#include <linux/module.h>
21
d85b4f7b 22#define DEFAULT_TIMEOUT_MS 3000
96ff0f5c
JL
23/*
24 * Hold configuration here, cannot be more than one instance of the driver
25 * since pm_power_off itself is global.
26 */
86336ba4 27static struct gpio_desc *reset_gpio;
d85b4f7b 28static u32 timeout = DEFAULT_TIMEOUT_MS;
76ee875c
HS
29static u32 active_delay = 100;
30static u32 inactive_delay = 100;
96ff0f5c
JL
31
32static void gpio_poweroff_do_poweroff(void)
33{
86336ba4 34 BUG_ON(!reset_gpio);
96ff0f5c 35
5343527b 36 /* drive it active, also inactive->active edge */
86336ba4 37 gpiod_direction_output(reset_gpio, 1);
76ee875c
HS
38 mdelay(active_delay);
39
5343527b 40 /* drive inactive, also active->inactive edge */
77142a61 41 gpiod_set_value_cansleep(reset_gpio, 0);
76ee875c 42 mdelay(inactive_delay);
5343527b
AL
43
44 /* drive it active, also inactive->active edge */
77142a61 45 gpiod_set_value_cansleep(reset_gpio, 1);
96ff0f5c
JL
46
47 /* give it some time */
d85b4f7b 48 mdelay(timeout);
96ff0f5c
JL
49
50 WARN_ON(1);
51}
52
6d2cea4f 53static int gpio_poweroff_probe(struct platform_device *pdev)
96ff0f5c 54{
96ff0f5c 55 bool input = false;
a34c0a8b 56 enum gpiod_flags flags;
96ff0f5c
JL
57
58 /* If a pm_power_off function has already been added, leave it alone */
59 if (pm_power_off != NULL) {
86336ba4
LW
60 dev_err(&pdev->dev,
61 "%s: pm_power_off function already registered",
96ff0f5c
JL
62 __func__);
63 return -EBUSY;
64 }
65
d85b4f7b 66 input = device_property_read_bool(&pdev->dev, "input");
a34c0a8b
UKK
67 if (input)
68 flags = GPIOD_IN;
69 else
70 flags = GPIOD_OUT_LOW;
96ff0f5c 71
76ee875c
HS
72 device_property_read_u32(&pdev->dev, "active-delay-ms", &active_delay);
73 device_property_read_u32(&pdev->dev, "inactive-delay-ms",
74 &inactive_delay);
d85b4f7b
MF
75 device_property_read_u32(&pdev->dev, "timeout-ms", &timeout);
76
a34c0a8b
UKK
77 reset_gpio = devm_gpiod_get(&pdev->dev, NULL, flags);
78 if (IS_ERR(reset_gpio))
79 return PTR_ERR(reset_gpio);
96ff0f5c
JL
80
81 pm_power_off = &gpio_poweroff_do_poweroff;
82 return 0;
96ff0f5c
JL
83}
84
6d2cea4f 85static int gpio_poweroff_remove(struct platform_device *pdev)
96ff0f5c 86{
96ff0f5c
JL
87 if (pm_power_off == &gpio_poweroff_do_poweroff)
88 pm_power_off = NULL;
89
90 return 0;
91}
92
93static const struct of_device_id of_gpio_poweroff_match[] = {
94 { .compatible = "gpio-poweroff", },
95 {},
96};
97
98static struct platform_driver gpio_poweroff_driver = {
99 .probe = gpio_poweroff_probe,
6d2cea4f 100 .remove = gpio_poweroff_remove,
96ff0f5c 101 .driver = {
5343527b 102 .name = "poweroff-gpio",
5343527b
AL
103 .of_match_table = of_gpio_poweroff_match,
104 },
96ff0f5c
JL
105};
106
107module_platform_driver(gpio_poweroff_driver);
108
109MODULE_AUTHOR("Jamie Lentin <jm@lentin.co.uk>");
110MODULE_DESCRIPTION("GPIO poweroff driver");
5343527b 111MODULE_LICENSE("GPL v2");
96ff0f5c 112MODULE_ALIAS("platform:poweroff-gpio");