sgy_cts1000.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/init.h>
  17. #include <linux/of_gpio.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/reboot.h>
  21. #include <linux/interrupt.h>
  22. #include <asm/machdep.h>
  23. static struct device_node *halt_node;
  24. static struct of_device_id child_match[] = {
  25. {
  26. .compatible = "sgy,gpio-halt",
  27. },
  28. {},
  29. };
  30. static void gpio_halt_wfn(struct work_struct *work)
  31. {
  32. /* Likely wont return */
  33. orderly_poweroff(true);
  34. }
  35. static DECLARE_WORK(gpio_halt_wq, gpio_halt_wfn);
  36. static void gpio_halt_cb(void)
  37. {
  38. enum of_gpio_flags flags;
  39. int trigger, gpio;
  40. if (!halt_node)
  41. return;
  42. gpio = of_get_gpio_flags(halt_node, 0, &flags);
  43. if (!gpio_is_valid(gpio))
  44. return;
  45. trigger = (flags == OF_GPIO_ACTIVE_LOW);
  46. printk(KERN_INFO "gpio-halt: triggering GPIO.\n");
  47. /* Probably wont return */
  48. gpio_set_value(gpio, trigger);
  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. ppc_md.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. ppc_md.power_off = NULL;
  113. gpio_free(gpio);
  114. halt_node = NULL;
  115. }
  116. return 0;
  117. }
  118. static 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. .owner = THIS_MODULE,
  132. .of_match_table = gpio_halt_match,
  133. },
  134. .probe = gpio_halt_probe,
  135. .remove = gpio_halt_remove,
  136. };
  137. module_platform_driver(gpio_halt_driver);
  138. MODULE_DESCRIPTION("Driver to support GPIO triggered system halt for Servergy CTS-1000 Systems.");
  139. MODULE_VERSION("1.0");
  140. MODULE_AUTHOR("Ben Collins <ben.c@servergy.com>");
  141. MODULE_LICENSE("GPL");