1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Servergy CTS-1000 Setup
5 * Maintained by Ben Collins <ben.c@servergy.com>
7 * Copyright 2012 by Servergy, Inc.
10 #include <linux/platform_device.h>
11 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/of_gpio.h>
14 #include <linux/of_irq.h>
15 #include <linux/workqueue.h>
16 #include <linux/reboot.h>
17 #include <linux/interrupt.h>
19 #include <asm/machdep.h>
21 static struct device_node *halt_node;
23 static const struct of_device_id child_match[] = {
25 .compatible = "sgy,gpio-halt",
30 static void gpio_halt_wfn(struct work_struct *work)
32 /* Likely wont return */
33 orderly_poweroff(true);
35 static DECLARE_WORK(gpio_halt_wq, gpio_halt_wfn);
37 static void __noreturn gpio_halt_cb(void)
39 enum of_gpio_flags flags;
43 panic("No reset GPIO information was provided in DT\n");
45 gpio = of_get_gpio_flags(halt_node, 0, &flags);
47 if (!gpio_is_valid(gpio))
48 panic("Provided GPIO is invalid\n");
50 trigger = (flags == OF_GPIO_ACTIVE_LOW);
52 printk(KERN_INFO "gpio-halt: triggering GPIO.\n");
54 /* Probably wont return */
55 gpio_set_value(gpio, trigger);
57 panic("Halt failed\n");
60 /* This IRQ means someone pressed the power button and it is waiting for us
61 * to handle the shutdown/poweroff. */
62 static irqreturn_t gpio_halt_irq(int irq, void *__data)
64 printk(KERN_INFO "gpio-halt: shutdown due to power button IRQ.\n");
65 schedule_work(&gpio_halt_wq);
70 static int gpio_halt_probe(struct platform_device *pdev)
72 enum of_gpio_flags flags;
73 struct device_node *node = pdev->dev.of_node;
80 /* If there's no matching child, this isn't really an error */
81 halt_node = of_find_matching_node(node, child_match);
85 /* Technically we could just read the first one, but punish
86 * DT writers for invalid form. */
87 if (of_gpio_count(halt_node) != 1)
90 /* Get the gpio number relative to the dynamic base. */
91 gpio = of_get_gpio_flags(halt_node, 0, &flags);
92 if (!gpio_is_valid(gpio))
95 err = gpio_request(gpio, "gpio-halt");
97 printk(KERN_ERR "gpio-halt: error requesting GPIO %d.\n",
103 trigger = (flags == OF_GPIO_ACTIVE_LOW);
105 gpio_direction_output(gpio, !trigger);
107 /* Now get the IRQ which tells us when the power button is hit */
108 irq = irq_of_parse_and_map(halt_node, 0);
109 err = request_irq(irq, gpio_halt_irq, IRQF_TRIGGER_RISING |
110 IRQF_TRIGGER_FALLING, "gpio-halt", halt_node);
112 printk(KERN_ERR "gpio-halt: error requesting IRQ %d for "
113 "GPIO %d.\n", irq, gpio);
119 /* Register our halt function */
120 ppc_md.halt = gpio_halt_cb;
121 pm_power_off = gpio_halt_cb;
123 printk(KERN_INFO "gpio-halt: registered GPIO %d (%d trigger, %d"
124 " irq).\n", gpio, trigger, irq);
129 static int gpio_halt_remove(struct platform_device *pdev)
132 int gpio = of_get_gpio(halt_node, 0);
133 int irq = irq_of_parse_and_map(halt_node, 0);
135 free_irq(irq, halt_node);
148 static const struct of_device_id gpio_halt_match[] = {
149 /* We match on the gpio bus itself and scan the children since they
150 * wont be matched against us. We know the bus wont match until it
151 * has been registered too. */
153 .compatible = "fsl,qoriq-gpio",
157 MODULE_DEVICE_TABLE(of, gpio_halt_match);
159 static struct platform_driver gpio_halt_driver = {
162 .of_match_table = gpio_halt_match,
164 .probe = gpio_halt_probe,
165 .remove = gpio_halt_remove,
168 module_platform_driver(gpio_halt_driver);
170 MODULE_DESCRIPTION("Driver to support GPIO triggered system halt for Servergy CTS-1000 Systems.");
171 MODULE_VERSION("1.0");
172 MODULE_AUTHOR("Ben Collins <ben.c@servergy.com>");
173 MODULE_LICENSE("GPL");