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