sgy_cts1000.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 gpio_halt_cb(void)
  36. {
  37. enum of_gpio_flags flags;
  38. int trigger, gpio;
  39. if (!halt_node)
  40. return;
  41. gpio = of_get_gpio_flags(halt_node, 0, &flags);
  42. if (!gpio_is_valid(gpio))
  43. return;
  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. }
  49. /* This IRQ means someone pressed the power button and it is waiting for us
  50. * to handle the shutdown/poweroff. */
  51. static irqreturn_t gpio_halt_irq(int irq, void *__data)
  52. {
  53. printk(KERN_INFO "gpio-halt: shutdown due to power button IRQ.\n");
  54. schedule_work(&gpio_halt_wq);
  55. return IRQ_HANDLED;
  56. };
  57. static int gpio_halt_probe(struct platform_device *pdev)
  58. {
  59. enum of_gpio_flags flags;
  60. struct device_node *node = pdev->dev.of_node;
  61. int gpio, err, irq;
  62. int trigger;
  63. if (!node)
  64. return -ENODEV;
  65. /* If there's no matching child, this isn't really an error */
  66. halt_node = of_find_matching_node(node, child_match);
  67. if (!halt_node)
  68. return 0;
  69. /* Technically we could just read the first one, but punish
  70. * DT writers for invalid form. */
  71. if (of_gpio_count(halt_node) != 1)
  72. return -EINVAL;
  73. /* Get the gpio number relative to the dynamic base. */
  74. gpio = of_get_gpio_flags(halt_node, 0, &flags);
  75. if (!gpio_is_valid(gpio))
  76. return -EINVAL;
  77. err = gpio_request(gpio, "gpio-halt");
  78. if (err) {
  79. printk(KERN_ERR "gpio-halt: error requesting GPIO %d.\n",
  80. gpio);
  81. halt_node = NULL;
  82. return err;
  83. }
  84. trigger = (flags == OF_GPIO_ACTIVE_LOW);
  85. gpio_direction_output(gpio, !trigger);
  86. /* Now get the IRQ which tells us when the power button is hit */
  87. irq = irq_of_parse_and_map(halt_node, 0);
  88. err = request_irq(irq, gpio_halt_irq, IRQF_TRIGGER_RISING |
  89. IRQF_TRIGGER_FALLING, "gpio-halt", halt_node);
  90. if (err) {
  91. printk(KERN_ERR "gpio-halt: error requesting IRQ %d for "
  92. "GPIO %d.\n", irq, gpio);
  93. gpio_free(gpio);
  94. halt_node = NULL;
  95. return err;
  96. }
  97. /* Register our halt function */
  98. ppc_md.halt = gpio_halt_cb;
  99. pm_power_off = gpio_halt_cb;
  100. printk(KERN_INFO "gpio-halt: registered GPIO %d (%d trigger, %d"
  101. " irq).\n", gpio, trigger, irq);
  102. return 0;
  103. }
  104. static int gpio_halt_remove(struct platform_device *pdev)
  105. {
  106. if (halt_node) {
  107. int gpio = of_get_gpio(halt_node, 0);
  108. int irq = irq_of_parse_and_map(halt_node, 0);
  109. free_irq(irq, halt_node);
  110. ppc_md.halt = NULL;
  111. pm_power_off = NULL;
  112. gpio_free(gpio);
  113. halt_node = NULL;
  114. }
  115. return 0;
  116. }
  117. static const struct of_device_id gpio_halt_match[] = {
  118. /* We match on the gpio bus itself and scan the children since they
  119. * wont be matched against us. We know the bus wont match until it
  120. * has been registered too. */
  121. {
  122. .compatible = "fsl,qoriq-gpio",
  123. },
  124. {},
  125. };
  126. MODULE_DEVICE_TABLE(of, gpio_halt_match);
  127. static struct platform_driver gpio_halt_driver = {
  128. .driver = {
  129. .name = "gpio-halt",
  130. .of_match_table = gpio_halt_match,
  131. },
  132. .probe = gpio_halt_probe,
  133. .remove = gpio_halt_remove,
  134. };
  135. module_platform_driver(gpio_halt_driver);
  136. MODULE_DESCRIPTION("Driver to support GPIO triggered system halt for Servergy CTS-1000 Systems.");
  137. MODULE_VERSION("1.0");
  138. MODULE_AUTHOR("Ben Collins <ben.c@servergy.com>");
  139. MODULE_LICENSE("GPL");