drivers/power/reset/gpio-poweroff.c
Source file repositories/reference/linux-study-clean/drivers/power/reset/gpio-poweroff.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/power/reset/gpio-poweroff.c- Extension
.c- Size
- 3418 bytes
- Lines
- 122
- Domain
- Driver Families
- Bucket
- drivers/power
- Inferred role
- Driver Families: implementation source
- Status
- source implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/kernel.hlinux/init.hlinux/delay.hlinux/platform_device.hlinux/property.hlinux/gpio/consumer.hlinux/mod_devicetable.hlinux/module.hlinux/reboot.h
Detected Declarations
struct gpio_powerofffunction gpio_poweroff_do_powerofffunction gpio_poweroff_probe
Annotated Snippet
struct gpio_poweroff {
struct gpio_desc *reset_gpio;
u32 timeout_ms;
u32 active_delay_ms;
u32 inactive_delay_ms;
};
static int gpio_poweroff_do_poweroff(struct sys_off_data *data)
{
struct gpio_poweroff *gpio_poweroff = data->cb_data;
/* drive it active, also inactive->active edge */
gpiod_direction_output(gpio_poweroff->reset_gpio, 1);
mdelay(gpio_poweroff->active_delay_ms);
/* drive inactive, also active->inactive edge */
gpiod_set_value_cansleep(gpio_poweroff->reset_gpio, 0);
mdelay(gpio_poweroff->inactive_delay_ms);
/* drive it active, also inactive->active edge */
gpiod_set_value_cansleep(gpio_poweroff->reset_gpio, 1);
/* give it some time */
mdelay(gpio_poweroff->timeout_ms);
/*
* If code reaches this point, it means that gpio-poweroff has failed
* to actually power off the system.
* Warn the user that the attempt to poweroff via gpio-poweroff
* has gone wrong.
*/
WARN(1, "Failed to poweroff via gpio-poweroff mechanism\n");
return NOTIFY_DONE;
}
static int gpio_poweroff_probe(struct platform_device *pdev)
{
struct gpio_poweroff *gpio_poweroff;
bool input = false;
enum gpiod_flags flags;
int priority = SYS_OFF_PRIO_DEFAULT;
int ret;
gpio_poweroff = devm_kzalloc(&pdev->dev, sizeof(*gpio_poweroff), GFP_KERNEL);
if (!gpio_poweroff)
return -ENOMEM;
input = device_property_read_bool(&pdev->dev, "input");
if (input)
flags = GPIOD_IN;
else
flags = GPIOD_OUT_LOW;
gpio_poweroff->active_delay_ms = 100;
gpio_poweroff->inactive_delay_ms = 100;
gpio_poweroff->timeout_ms = DEFAULT_TIMEOUT_MS;
device_property_read_u32(&pdev->dev, "active-delay-ms", &gpio_poweroff->active_delay_ms);
device_property_read_u32(&pdev->dev, "inactive-delay-ms",
&gpio_poweroff->inactive_delay_ms);
device_property_read_u32(&pdev->dev, "timeout-ms", &gpio_poweroff->timeout_ms);
device_property_read_u32(&pdev->dev, "priority", &priority);
if (priority > 255) {
dev_err(&pdev->dev, "Invalid priority property: %u\n", priority);
return -EINVAL;
}
gpio_poweroff->reset_gpio = devm_gpiod_get(&pdev->dev, NULL, flags);
if (IS_ERR(gpio_poweroff->reset_gpio))
return PTR_ERR(gpio_poweroff->reset_gpio);
ret = devm_register_sys_off_handler(&pdev->dev, SYS_OFF_MODE_POWER_OFF,
priority, gpio_poweroff_do_poweroff, gpio_poweroff);
if (ret)
return dev_err_probe(&pdev->dev, ret, "Cannot register poweroff handler\n");
return 0;
}
static const struct of_device_id of_gpio_poweroff_match[] = {
{ .compatible = "gpio-poweroff", },
{},
};
MODULE_DEVICE_TABLE(of, of_gpio_poweroff_match);
static struct platform_driver gpio_poweroff_driver = {
.probe = gpio_poweroff_probe,
.driver = {
Annotation
- Immediate include surface: `linux/kernel.h`, `linux/init.h`, `linux/delay.h`, `linux/platform_device.h`, `linux/property.h`, `linux/gpio/consumer.h`, `linux/mod_devicetable.h`, `linux/module.h`.
- Detected declarations: `struct gpio_poweroff`, `function gpio_poweroff_do_poweroff`, `function gpio_poweroff_probe`.
- Atlas domain: Driver Families / drivers/power.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.