Jamie Lentin | 96ff0f5 | 2012-11-17 09:51:04 +0100 | [diff] [blame] | 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> |
Linus Walleij | 86336ba | 2014-07-07 12:34:32 +0200 | [diff] [blame] | 18 | #include <linux/gpio/consumer.h> |
Jamie Lentin | 96ff0f5 | 2012-11-17 09:51:04 +0100 | [diff] [blame] | 19 | #include <linux/of_platform.h> |
Jamie Lentin | 96ff0f5 | 2012-11-17 09:51:04 +0100 | [diff] [blame] | 20 | #include <linux/module.h> |
| 21 | |
Moritz Fischer | d85b4f7 | 2018-02-22 15:17:14 -0800 | [diff] [blame^] | 22 | #define DEFAULT_TIMEOUT_MS 3000 |
Jamie Lentin | 96ff0f5 | 2012-11-17 09:51:04 +0100 | [diff] [blame] | 23 | /* |
| 24 | * Hold configuration here, cannot be more than one instance of the driver |
| 25 | * since pm_power_off itself is global. |
| 26 | */ |
Linus Walleij | 86336ba | 2014-07-07 12:34:32 +0200 | [diff] [blame] | 27 | static struct gpio_desc *reset_gpio; |
Moritz Fischer | d85b4f7 | 2018-02-22 15:17:14 -0800 | [diff] [blame^] | 28 | static u32 timeout = DEFAULT_TIMEOUT_MS; |
Jamie Lentin | 96ff0f5 | 2012-11-17 09:51:04 +0100 | [diff] [blame] | 29 | |
| 30 | static void gpio_poweroff_do_poweroff(void) |
| 31 | { |
Linus Walleij | 86336ba | 2014-07-07 12:34:32 +0200 | [diff] [blame] | 32 | BUG_ON(!reset_gpio); |
Jamie Lentin | 96ff0f5 | 2012-11-17 09:51:04 +0100 | [diff] [blame] | 33 | |
Andrew Lunn | 5343527 | 2013-01-06 11:10:35 +0100 | [diff] [blame] | 34 | /* drive it active, also inactive->active edge */ |
Linus Walleij | 86336ba | 2014-07-07 12:34:32 +0200 | [diff] [blame] | 35 | gpiod_direction_output(reset_gpio, 1); |
Jamie Lentin | 96ff0f5 | 2012-11-17 09:51:04 +0100 | [diff] [blame] | 36 | mdelay(100); |
Andrew Lunn | 5343527 | 2013-01-06 11:10:35 +0100 | [diff] [blame] | 37 | /* drive inactive, also active->inactive edge */ |
Linus Walleij | 86336ba | 2014-07-07 12:34:32 +0200 | [diff] [blame] | 38 | gpiod_set_value(reset_gpio, 0); |
Jamie Lentin | 96ff0f5 | 2012-11-17 09:51:04 +0100 | [diff] [blame] | 39 | mdelay(100); |
Andrew Lunn | 5343527 | 2013-01-06 11:10:35 +0100 | [diff] [blame] | 40 | |
| 41 | /* drive it active, also inactive->active edge */ |
Linus Walleij | 86336ba | 2014-07-07 12:34:32 +0200 | [diff] [blame] | 42 | gpiod_set_value(reset_gpio, 1); |
Jamie Lentin | 96ff0f5 | 2012-11-17 09:51:04 +0100 | [diff] [blame] | 43 | |
| 44 | /* give it some time */ |
Moritz Fischer | d85b4f7 | 2018-02-22 15:17:14 -0800 | [diff] [blame^] | 45 | mdelay(timeout); |
Jamie Lentin | 96ff0f5 | 2012-11-17 09:51:04 +0100 | [diff] [blame] | 46 | |
| 47 | WARN_ON(1); |
| 48 | } |
| 49 | |
Greg Kroah-Hartman | 6d2cea4 | 2012-12-21 15:04:54 -0800 | [diff] [blame] | 50 | static int gpio_poweroff_probe(struct platform_device *pdev) |
Jamie Lentin | 96ff0f5 | 2012-11-17 09:51:04 +0100 | [diff] [blame] | 51 | { |
Jamie Lentin | 96ff0f5 | 2012-11-17 09:51:04 +0100 | [diff] [blame] | 52 | bool input = false; |
Uwe Kleine-König | a34c0a8 | 2015-05-18 22:45:06 +0200 | [diff] [blame] | 53 | enum gpiod_flags flags; |
Jamie Lentin | 96ff0f5 | 2012-11-17 09:51:04 +0100 | [diff] [blame] | 54 | |
| 55 | /* If a pm_power_off function has already been added, leave it alone */ |
| 56 | if (pm_power_off != NULL) { |
Linus Walleij | 86336ba | 2014-07-07 12:34:32 +0200 | [diff] [blame] | 57 | dev_err(&pdev->dev, |
| 58 | "%s: pm_power_off function already registered", |
Jamie Lentin | 96ff0f5 | 2012-11-17 09:51:04 +0100 | [diff] [blame] | 59 | __func__); |
| 60 | return -EBUSY; |
| 61 | } |
| 62 | |
Moritz Fischer | d85b4f7 | 2018-02-22 15:17:14 -0800 | [diff] [blame^] | 63 | input = device_property_read_bool(&pdev->dev, "input"); |
Uwe Kleine-König | a34c0a8 | 2015-05-18 22:45:06 +0200 | [diff] [blame] | 64 | if (input) |
| 65 | flags = GPIOD_IN; |
| 66 | else |
| 67 | flags = GPIOD_OUT_LOW; |
| 68 | |
Moritz Fischer | d85b4f7 | 2018-02-22 15:17:14 -0800 | [diff] [blame^] | 69 | device_property_read_u32(&pdev->dev, "timeout-ms", &timeout); |
| 70 | |
Uwe Kleine-König | a34c0a8 | 2015-05-18 22:45:06 +0200 | [diff] [blame] | 71 | reset_gpio = devm_gpiod_get(&pdev->dev, NULL, flags); |
Linus Walleij | 86336ba | 2014-07-07 12:34:32 +0200 | [diff] [blame] | 72 | if (IS_ERR(reset_gpio)) |
| 73 | return PTR_ERR(reset_gpio); |
Jamie Lentin | 96ff0f5 | 2012-11-17 09:51:04 +0100 | [diff] [blame] | 74 | |
Jamie Lentin | 96ff0f5 | 2012-11-17 09:51:04 +0100 | [diff] [blame] | 75 | pm_power_off = &gpio_poweroff_do_poweroff; |
| 76 | return 0; |
Jamie Lentin | 96ff0f5 | 2012-11-17 09:51:04 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Greg Kroah-Hartman | 6d2cea4 | 2012-12-21 15:04:54 -0800 | [diff] [blame] | 79 | static int gpio_poweroff_remove(struct platform_device *pdev) |
Jamie Lentin | 96ff0f5 | 2012-11-17 09:51:04 +0100 | [diff] [blame] | 80 | { |
Jamie Lentin | 96ff0f5 | 2012-11-17 09:51:04 +0100 | [diff] [blame] | 81 | if (pm_power_off == &gpio_poweroff_do_poweroff) |
| 82 | pm_power_off = NULL; |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | static const struct of_device_id of_gpio_poweroff_match[] = { |
| 88 | { .compatible = "gpio-poweroff", }, |
| 89 | {}, |
| 90 | }; |
| 91 | |
| 92 | static struct platform_driver gpio_poweroff_driver = { |
| 93 | .probe = gpio_poweroff_probe, |
Greg Kroah-Hartman | 6d2cea4 | 2012-12-21 15:04:54 -0800 | [diff] [blame] | 94 | .remove = gpio_poweroff_remove, |
Jamie Lentin | 96ff0f5 | 2012-11-17 09:51:04 +0100 | [diff] [blame] | 95 | .driver = { |
Andrew Lunn | 5343527 | 2013-01-06 11:10:35 +0100 | [diff] [blame] | 96 | .name = "poweroff-gpio", |
Andrew Lunn | 5343527 | 2013-01-06 11:10:35 +0100 | [diff] [blame] | 97 | .of_match_table = of_gpio_poweroff_match, |
| 98 | }, |
Jamie Lentin | 96ff0f5 | 2012-11-17 09:51:04 +0100 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | module_platform_driver(gpio_poweroff_driver); |
| 102 | |
| 103 | MODULE_AUTHOR("Jamie Lentin <jm@lentin.co.uk>"); |
| 104 | MODULE_DESCRIPTION("GPIO poweroff driver"); |
Andrew Lunn | 5343527 | 2013-01-06 11:10:35 +0100 | [diff] [blame] | 105 | MODULE_LICENSE("GPL v2"); |
Jamie Lentin | 96ff0f5 | 2012-11-17 09:51:04 +0100 | [diff] [blame] | 106 | MODULE_ALIAS("platform:poweroff-gpio"); |