sgy_cts1000.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Servergy CTS-1000 Setup
  3. *
  4. * Maintained by Ben Collins <ben.c@servergy.com>
  5. *
  6. * Copyright 2012 by Servergy, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/platform_device.h>
  14. #include <linux/device.h>
  15. #include <linux/module.h>
  16. #include <linux/of_gpio.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/reboot.h>
  20. #include <linux/interrupt.h>
  21. #include <asm/machdep.h>
  22. static struct device_node *halt_node;
  23. static const struct of_device_id child_match[] = {
  24. {
  25. .compatible = "sgy,gpio-halt",
  26. },
  27. {},
  28. };
  29. static void gpio_halt_wfn(struct work_struct *work)
  30. {
  31. /* Likely wont return */
  32. orderly_poweroff(true);
  33. }
  34. static DECLARE_WORK(gpio_halt_wq, gpio_halt_wfn);
  35. static void __noreturn gpio_halt_cb(void)
  36. {
  37. enum of_gpio_flags flags;
  38. int trigger, gpio;
  39. if (!halt_node)
  40. panic("No reset GPIO information was provided in DT\n");
  41. gpio = of_get_gpio_flags(halt_node, 0, &flags);
  42. if (!gpio_is_valid(gpio))
  43. panic("Provided GPIO is invalid\n");
  44. trigger = (flags == OF_GPIO_ACTIVE_LOW);
  45. printk(KERN_INFO "gpio-halt: triggering GPIO.\n");
  46. /* Probably wont return */
  47. gpio_set_value(gpio, trigger);
  48. panic("Halt failed\n");
  49. }
  50. /* This IRQ means someone pressed the power button and it is waiting for us
  51. * to handle the shutdown/poweroff. */
  52. static irqreturn_t gpio_halt_irq(int irq, void *__data)
  53. {
  54. printk(KERN_INFO "gpio-halt: shutdown due to power button IRQ.\n");
  55. schedule_work(&gpio_halt_wq);
  56. return IRQ_HANDLED;
  57. };
  58. static int gpio_halt_probe(struct platform_device *pdev)
  59. {
  60. enum of_gpio_flags flags;
  61. struct device_node *node = pdev->dev.of_node;
  62. int gpio, err, irq;
  63. int trigger;
  64. if (!node)
  65. return -ENODEV;
  66. /* If there's no matching child, this isn't really an error */
  67. halt_node = of_find_matching_node(node, child_match);
  68. if (!halt_node)
  69. return 0;
  70. /* Technically we could just read the first one, but punish
  71. * DT writers for invalid form. */
  72. if (of_gpio_count(halt_node) != 1)
  73. return -EINVAL;
  74. /* Get the gpio number relative to the dynamic base. */
  75. gpio = of_get_gpio_flags(halt_node, 0, &flags);
  76. if (!gpio_is_valid(gpio))
  77. return -EINVAL;
  78. err = gpio_request(gpio, "gpio-halt");
  79. if (err) {
  80. printk(KERN_ERR "gpio-halt: error requesting GPIO %d.\n",
  81. gpio);
  82. halt_node = NULL;
  83. return err;
  84. }
  85. trigger = (flags == OF_GPIO_ACTIVE_LOW);
  86. gpio_direction_output(gpio, !trigger);
  87. /* Now get the IRQ which tells us when the power button is hit */
  88. irq = irq_of_parse_and_map(halt_node, 0);
  89. err = request_irq(irq, gpio_halt_irq, IRQF_TRIGGER_RISING |
  90. IRQF_TRIGGER_FALLING, "gpio-halt", halt_node);
  91. if (err) {
  92. printk(KERN_ERR "gpio-halt: error requesting IRQ %d for "
  93. "GPIO %d.\n", irq, gpio);
  94. gpio_free(gpio);
  95. halt_node = NULL;
  96. return err;
  97. }
  98. /* Register our halt function */
  99. ppc_md.halt = gpio_halt_cb;
  100. pm_power_off = gpio_halt_cb;
  101. printk(KERN_INFO "gpio-halt: registered GPIO %d (%d trigger, %d"
  102. " irq).\n", gpio, trigger, irq);
  103. return 0;
  104. }
  105. static int gpio_halt_remove(struct platform_device *pdev)
  106. {
  107. if (halt_node) {
  108. int gpio = of_get_gpio(halt_node, 0);
  109. int irq = irq_of_parse_and_map(halt_node, 0);
  110. free_irq(irq, halt_node);
  111. ppc_md.halt = NULL;
  112. pm_power_off = NULL;
  113. gpio_free(gpio);
  114. halt_node = NULL;
  115. }
  116. return 0;
  117. }
  118. static const struct of_device_id gpio_halt_match[] = {
  119. /* We match on the gpio bus itself and scan the children since they
  120. * wont be matched against us. We know the bus wont match until it
  121. * has been registered too. */
  122. {
  123. .compatible = "fsl,qoriq-gpio",
  124. },
  125. {},
  126. };
  127. MODULE_DEVICE_TABLE(of, gpio_halt_match);
  128. static struct platform_driver gpio_halt_driver = {
  129. .driver = {
  130. .name = "gpio-halt",
  131. .of_match_table = gpio_halt_match,
  132. },
  133. .probe = gpio_halt_probe,
  134. .remove = gpio_halt_remove,
  135. };
  136. module_platform_driver(gpio_halt_driver);
  137. MODULE_DESCRIPTION("Driver to support GPIO triggered system halt for Servergy CTS-1000 Systems.");
  138. MODULE_VERSION("1.0");
  139. MODULE_AUTHOR("Ben Collins <ben.c@servergy.com>");
  140. MODULE_LICENSE("GPL");