18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Toggles a GPIO pin to power down a device 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Jamie Lentin <jm@lentin.co.uk> 68c2ecf20Sopenharmony_ci * Andrew Lunn <andrew@lunn.ch> 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Copyright (C) 2012 Jamie Lentin 98c2ecf20Sopenharmony_ci */ 108c2ecf20Sopenharmony_ci#include <linux/kernel.h> 118c2ecf20Sopenharmony_ci#include <linux/init.h> 128c2ecf20Sopenharmony_ci#include <linux/delay.h> 138c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 148c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h> 158c2ecf20Sopenharmony_ci#include <linux/of_platform.h> 168c2ecf20Sopenharmony_ci#include <linux/module.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#define DEFAULT_TIMEOUT_MS 3000 198c2ecf20Sopenharmony_ci/* 208c2ecf20Sopenharmony_ci * Hold configuration here, cannot be more than one instance of the driver 218c2ecf20Sopenharmony_ci * since pm_power_off itself is global. 228c2ecf20Sopenharmony_ci */ 238c2ecf20Sopenharmony_cistatic struct gpio_desc *reset_gpio; 248c2ecf20Sopenharmony_cistatic u32 timeout = DEFAULT_TIMEOUT_MS; 258c2ecf20Sopenharmony_cistatic u32 active_delay = 100; 268c2ecf20Sopenharmony_cistatic u32 inactive_delay = 100; 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_cistatic void gpio_poweroff_do_poweroff(void) 298c2ecf20Sopenharmony_ci{ 308c2ecf20Sopenharmony_ci BUG_ON(!reset_gpio); 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci /* drive it active, also inactive->active edge */ 338c2ecf20Sopenharmony_ci gpiod_direction_output(reset_gpio, 1); 348c2ecf20Sopenharmony_ci mdelay(active_delay); 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci /* drive inactive, also active->inactive edge */ 378c2ecf20Sopenharmony_ci gpiod_set_value_cansleep(reset_gpio, 0); 388c2ecf20Sopenharmony_ci mdelay(inactive_delay); 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci /* drive it active, also inactive->active edge */ 418c2ecf20Sopenharmony_ci gpiod_set_value_cansleep(reset_gpio, 1); 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci /* give it some time */ 448c2ecf20Sopenharmony_ci mdelay(timeout); 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci WARN_ON(1); 478c2ecf20Sopenharmony_ci} 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_cistatic int gpio_poweroff_probe(struct platform_device *pdev) 508c2ecf20Sopenharmony_ci{ 518c2ecf20Sopenharmony_ci bool input = false; 528c2ecf20Sopenharmony_ci enum gpiod_flags flags; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci /* If a pm_power_off function has already been added, leave it alone */ 558c2ecf20Sopenharmony_ci if (pm_power_off != NULL) { 568c2ecf20Sopenharmony_ci dev_err(&pdev->dev, 578c2ecf20Sopenharmony_ci "%s: pm_power_off function already registered\n", 588c2ecf20Sopenharmony_ci __func__); 598c2ecf20Sopenharmony_ci return -EBUSY; 608c2ecf20Sopenharmony_ci } 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci input = device_property_read_bool(&pdev->dev, "input"); 638c2ecf20Sopenharmony_ci if (input) 648c2ecf20Sopenharmony_ci flags = GPIOD_IN; 658c2ecf20Sopenharmony_ci else 668c2ecf20Sopenharmony_ci flags = GPIOD_OUT_LOW; 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci device_property_read_u32(&pdev->dev, "active-delay-ms", &active_delay); 698c2ecf20Sopenharmony_ci device_property_read_u32(&pdev->dev, "inactive-delay-ms", 708c2ecf20Sopenharmony_ci &inactive_delay); 718c2ecf20Sopenharmony_ci device_property_read_u32(&pdev->dev, "timeout-ms", &timeout); 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci reset_gpio = devm_gpiod_get(&pdev->dev, NULL, flags); 748c2ecf20Sopenharmony_ci if (IS_ERR(reset_gpio)) 758c2ecf20Sopenharmony_ci return PTR_ERR(reset_gpio); 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci pm_power_off = &gpio_poweroff_do_poweroff; 788c2ecf20Sopenharmony_ci return 0; 798c2ecf20Sopenharmony_ci} 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_cistatic int gpio_poweroff_remove(struct platform_device *pdev) 828c2ecf20Sopenharmony_ci{ 838c2ecf20Sopenharmony_ci if (pm_power_off == &gpio_poweroff_do_poweroff) 848c2ecf20Sopenharmony_ci pm_power_off = NULL; 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci return 0; 878c2ecf20Sopenharmony_ci} 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_cistatic const struct of_device_id of_gpio_poweroff_match[] = { 908c2ecf20Sopenharmony_ci { .compatible = "gpio-poweroff", }, 918c2ecf20Sopenharmony_ci {}, 928c2ecf20Sopenharmony_ci}; 938c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, of_gpio_poweroff_match); 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_cistatic struct platform_driver gpio_poweroff_driver = { 968c2ecf20Sopenharmony_ci .probe = gpio_poweroff_probe, 978c2ecf20Sopenharmony_ci .remove = gpio_poweroff_remove, 988c2ecf20Sopenharmony_ci .driver = { 998c2ecf20Sopenharmony_ci .name = "poweroff-gpio", 1008c2ecf20Sopenharmony_ci .of_match_table = of_gpio_poweroff_match, 1018c2ecf20Sopenharmony_ci }, 1028c2ecf20Sopenharmony_ci}; 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_cimodule_platform_driver(gpio_poweroff_driver); 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jamie Lentin <jm@lentin.co.uk>"); 1078c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("GPIO poweroff driver"); 1088c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 1098c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:poweroff-gpio"); 110