Path: blob/master/arch/powerpc/platforms/85xx/sgy_cts1000.c
26481 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* Servergy CTS-1000 Setup3*4* Maintained by Ben Collins <[email protected]>5*6* Copyright 2012 by Servergy, Inc.7*/89#define pr_fmt(fmt) "gpio-halt: " fmt1011#include <linux/err.h>12#include <linux/platform_device.h>13#include <linux/device.h>14#include <linux/gpio/consumer.h>15#include <linux/module.h>16#include <linux/of_irq.h>17#include <linux/workqueue.h>18#include <linux/reboot.h>19#include <linux/interrupt.h>2021#include <asm/machdep.h>2223static struct gpio_desc *halt_gpio;24static int halt_irq;2526static const struct of_device_id child_match[] = {27{28.compatible = "sgy,gpio-halt",29},30{},31};3233static void gpio_halt_wfn(struct work_struct *work)34{35/* Likely wont return */36orderly_poweroff(true);37}38static DECLARE_WORK(gpio_halt_wq, gpio_halt_wfn);3940static void __noreturn gpio_halt_cb(void)41{42pr_info("triggering GPIO.\n");4344/* Probably wont return */45gpiod_set_value(halt_gpio, 1);4647panic("Halt failed\n");48}4950/* This IRQ means someone pressed the power button and it is waiting for us51* to handle the shutdown/poweroff. */52static irqreturn_t gpio_halt_irq(int irq, void *__data)53{54struct platform_device *pdev = __data;5556dev_info(&pdev->dev, "scheduling shutdown due to power button IRQ\n");57schedule_work(&gpio_halt_wq);5859return IRQ_HANDLED;60};6162static int __gpio_halt_probe(struct platform_device *pdev,63struct device_node *halt_node)64{65int err;6667halt_gpio = fwnode_gpiod_get_index(of_fwnode_handle(halt_node),68NULL, 0, GPIOD_OUT_LOW, "gpio-halt");69err = PTR_ERR_OR_ZERO(halt_gpio);70if (err) {71dev_err(&pdev->dev, "failed to request halt GPIO: %d\n", err);72return err;73}7475/* Now get the IRQ which tells us when the power button is hit */76halt_irq = irq_of_parse_and_map(halt_node, 0);77err = request_irq(halt_irq, gpio_halt_irq,78IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,79"gpio-halt", pdev);80if (err) {81dev_err(&pdev->dev, "failed to request IRQ %d: %d\n",82halt_irq, err);83gpiod_put(halt_gpio);84halt_gpio = NULL;85return err;86}8788/* Register our halt function */89ppc_md.halt = gpio_halt_cb;90pm_power_off = gpio_halt_cb;9192dev_info(&pdev->dev, "registered halt GPIO, irq: %d\n", halt_irq);9394return 0;95}9697static int gpio_halt_probe(struct platform_device *pdev)98{99struct device_node *halt_node;100int ret;101102if (!pdev->dev.of_node)103return -ENODEV;104105/* If there's no matching child, this isn't really an error */106halt_node = of_find_matching_node(pdev->dev.of_node, child_match);107if (!halt_node)108return -ENODEV;109110ret = __gpio_halt_probe(pdev, halt_node);111of_node_put(halt_node);112113return ret;114}115116static void gpio_halt_remove(struct platform_device *pdev)117{118free_irq(halt_irq, pdev);119cancel_work_sync(&gpio_halt_wq);120121ppc_md.halt = NULL;122pm_power_off = NULL;123124gpiod_put(halt_gpio);125halt_gpio = NULL;126}127128static const struct of_device_id gpio_halt_match[] = {129/* We match on the gpio bus itself and scan the children since they130* wont be matched against us. We know the bus wont match until it131* has been registered too. */132{133.compatible = "fsl,qoriq-gpio",134},135{},136};137MODULE_DEVICE_TABLE(of, gpio_halt_match);138139static struct platform_driver gpio_halt_driver = {140.driver = {141.name = "gpio-halt",142.of_match_table = gpio_halt_match,143},144.probe = gpio_halt_probe,145.remove = gpio_halt_remove,146};147148module_platform_driver(gpio_halt_driver);149150MODULE_DESCRIPTION("Driver to support GPIO triggered system halt for Servergy CTS-1000 Systems.");151MODULE_VERSION("1.0");152MODULE_AUTHOR("Ben Collins <[email protected]>");153MODULE_LICENSE("GPL");154155156