dsmg600-power.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * arch/arm/mach-ixp4xx/dsmg600-power.c
  3. *
  4. * DSM-G600 Power/Reset driver
  5. * Author: Michael Westerhof <mwester@dls.net>
  6. *
  7. * Based on nslu2-power.c
  8. * Copyright (C) 2005 Tower Technologies
  9. * Author: Alessandro Zummo <a.zummo@towertech.it>
  10. *
  11. * which was based on nslu2-io.c
  12. * Copyright (C) 2004 Karen Spearel
  13. *
  14. * Maintainers: http://www.nslu2-linux.org/
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License version 2 as
  18. * published by the Free Software Foundation.
  19. *
  20. */
  21. #include <linux/module.h>
  22. #include <linux/reboot.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/irq.h>
  25. #include <linux/jiffies.h>
  26. #include <linux/timer.h>
  27. #include <asm/gpio.h>
  28. #include <asm/mach-types.h>
  29. /* This is used to make sure the power-button pusher is serious. The button
  30. * must be held until the value of this counter reaches zero.
  31. */
  32. static int power_button_countdown;
  33. /* Must hold the button down for at least this many counts to be processed */
  34. #define PBUTTON_HOLDDOWN_COUNT 4 /* 2 secs */
  35. static void dsmg600_power_handler(unsigned long data);
  36. static DEFINE_TIMER(dsmg600_power_timer, dsmg600_power_handler, 0, 0);
  37. static void dsmg600_power_handler(unsigned long data)
  38. {
  39. /* This routine is called twice per second to check the
  40. * state of the power button.
  41. */
  42. if (gpio_get_value(DSMG600_PB_GPIO)) {
  43. /* IO Pin is 1 (button pushed) */
  44. if (power_button_countdown > 0)
  45. power_button_countdown--;
  46. } else {
  47. /* Done on button release, to allow for auto-power-on mods. */
  48. if (power_button_countdown == 0) {
  49. /* Signal init to do the ctrlaltdel action,
  50. * this will bypass init if it hasn't started
  51. * and do a kernel_restart.
  52. */
  53. ctrl_alt_del();
  54. /* Change the state of the power LED to "blink" */
  55. gpio_line_set(DSMG600_LED_PWR_GPIO, IXP4XX_GPIO_LOW);
  56. } else {
  57. power_button_countdown = PBUTTON_HOLDDOWN_COUNT;
  58. }
  59. }
  60. mod_timer(&dsmg600_power_timer, jiffies + msecs_to_jiffies(500));
  61. }
  62. static irqreturn_t dsmg600_reset_handler(int irq, void *dev_id)
  63. {
  64. /* This is the paper-clip reset, it shuts the machine down directly. */
  65. machine_power_off();
  66. return IRQ_HANDLED;
  67. }
  68. static int __init dsmg600_power_init(void)
  69. {
  70. if (!(machine_is_dsmg600()))
  71. return 0;
  72. if (request_irq(gpio_to_irq(DSMG600_RB_GPIO), &dsmg600_reset_handler,
  73. IRQF_DISABLED | IRQF_TRIGGER_LOW, "DSM-G600 reset button",
  74. NULL) < 0) {
  75. printk(KERN_DEBUG "Reset Button IRQ %d not available\n",
  76. gpio_to_irq(DSMG600_RB_GPIO));
  77. return -EIO;
  78. }
  79. /* The power button on the D-Link DSM-G600 is on GPIO 15, but
  80. * it cannot handle interrupts on that GPIO line. So we'll
  81. * have to poll it with a kernel timer.
  82. */
  83. /* Make sure that the power button GPIO is set up as an input */
  84. gpio_line_config(DSMG600_PB_GPIO, IXP4XX_GPIO_IN);
  85. /* Set the initial value for the power button IRQ handler */
  86. power_button_countdown = PBUTTON_HOLDDOWN_COUNT;
  87. mod_timer(&dsmg600_power_timer, jiffies + msecs_to_jiffies(500));
  88. return 0;
  89. }
  90. static void __exit dsmg600_power_exit(void)
  91. {
  92. if (!(machine_is_dsmg600()))
  93. return;
  94. del_timer_sync(&dsmg600_power_timer);
  95. free_irq(gpio_to_irq(DSMG600_RB_GPIO), NULL);
  96. }
  97. module_init(dsmg600_power_init);
  98. module_exit(dsmg600_power_exit);
  99. MODULE_AUTHOR("Michael Westerhof <mwester@dls.net>");
  100. MODULE_DESCRIPTION("DSM-G600 Power/Reset driver");
  101. MODULE_LICENSE("GPL");