2 * Toggles a GPIO pin to power down a device
4 * Jamie Lentin <jm@lentin.co.uk>
5 * Andrew Lunn <andrew@lunn.ch>
7 * Copyright (C) 2012 Jamie Lentin
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.
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/delay.h>
17 #include <linux/platform_device.h>
18 #include <linux/gpio.h>
19 #include <linux/of_platform.h>
20 #include <linux/of_gpio.h>
21 #include <linux/module.h>
24 * Hold configuration here, cannot be more than one instance of the driver
25 * since pm_power_off itself is global.
27 static int gpio_num
= -1;
28 static int gpio_active_low
;
30 static void gpio_poweroff_do_poweroff(void)
32 BUG_ON(gpio_num
== -1);
35 gpio_direction_output(gpio_num
, !gpio_active_low
);
37 /* rising edge or drive inactive */
38 gpio_set_value(gpio_num
, gpio_active_low
);
41 gpio_set_value(gpio_num
, !gpio_active_low
);
43 /* give it some time */
49 static int __devinit
gpio_poweroff_probe(struct platform_device
*pdev
)
51 enum of_gpio_flags flags
;
55 /* If a pm_power_off function has already been added, leave it alone */
56 if (pm_power_off
!= NULL
) {
57 pr_err("%s: pm_power_off function already registered",
62 gpio_num
= of_get_gpio_flags(pdev
->dev
.of_node
, 0, &flags
);
64 pr_err("%s: Could not get GPIO configuration: %d",
68 gpio_active_low
= flags
& OF_GPIO_ACTIVE_LOW
;
70 if (of_get_property(pdev
->dev
.of_node
, "input", NULL
))
73 ret
= gpio_request(gpio_num
, "poweroff-gpio");
75 pr_err("%s: Could not get GPIO %d", __func__
, gpio_num
);
79 if (gpio_direction_input(gpio_num
)) {
80 pr_err("Could not set direction of GPIO %d to input",
85 if (gpio_direction_output(gpio_num
, gpio_active_low
)) {
86 pr_err("Could not set direction of GPIO %d", gpio_num
);
91 pm_power_off
= &gpio_poweroff_do_poweroff
;
99 static int __devexit
gpio_poweroff_remove(struct platform_device
*pdev
)
103 if (pm_power_off
== &gpio_poweroff_do_poweroff
)
109 static const struct of_device_id of_gpio_poweroff_match
[] = {
110 { .compatible
= "gpio-poweroff", },
114 static struct platform_driver gpio_poweroff_driver
= {
115 .probe
= gpio_poweroff_probe
,
116 .remove
= __devexit_p(gpio_poweroff_remove
),
118 .name
= "poweroff-gpio",
119 .owner
= THIS_MODULE
,
120 .of_match_table
= of_gpio_poweroff_match
,
124 module_platform_driver(gpio_poweroff_driver
);
126 MODULE_AUTHOR("Jamie Lentin <jm@lentin.co.uk>");
127 MODULE_DESCRIPTION("GPIO poweroff driver");
128 MODULE_LICENSE("GPL");
129 MODULE_ALIAS("platform:poweroff-gpio");