via-pmu-led.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * via-pmu LED class device
  3. *
  4. * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  14. * NON INFRINGEMENT. See the GNU General Public License for more
  15. * details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #include <linux/types.h>
  23. #include <linux/kernel.h>
  24. #include <linux/device.h>
  25. #include <linux/leds.h>
  26. #include <linux/adb.h>
  27. #include <linux/pmu.h>
  28. #include <asm/prom.h>
  29. static spinlock_t pmu_blink_lock;
  30. static struct adb_request pmu_blink_req;
  31. /* -1: no change, 0: request off, 1: request on */
  32. static int requested_change;
  33. static int sleeping;
  34. static void pmu_req_done(struct adb_request * req)
  35. {
  36. unsigned long flags;
  37. spin_lock_irqsave(&pmu_blink_lock, flags);
  38. /* if someone requested a change in the meantime
  39. * (we only see the last one which is fine)
  40. * then apply it now */
  41. if (requested_change != -1 && !sleeping)
  42. pmu_request(&pmu_blink_req, NULL, 4, 0xee, 4, 0, requested_change);
  43. /* reset requested change */
  44. requested_change = -1;
  45. spin_unlock_irqrestore(&pmu_blink_lock, flags);
  46. }
  47. static void pmu_led_set(struct led_classdev *led_cdev,
  48. enum led_brightness brightness)
  49. {
  50. unsigned long flags;
  51. spin_lock_irqsave(&pmu_blink_lock, flags);
  52. switch (brightness) {
  53. case LED_OFF:
  54. requested_change = 0;
  55. break;
  56. case LED_FULL:
  57. requested_change = 1;
  58. break;
  59. default:
  60. goto out;
  61. break;
  62. }
  63. /* if request isn't done, then don't do anything */
  64. if (pmu_blink_req.complete && !sleeping)
  65. pmu_request(&pmu_blink_req, NULL, 4, 0xee, 4, 0, requested_change);
  66. out:
  67. spin_unlock_irqrestore(&pmu_blink_lock, flags);
  68. }
  69. static struct led_classdev pmu_led = {
  70. .name = "pmu-front-led",
  71. #ifdef CONFIG_ADB_PMU_LED_IDE
  72. .default_trigger = "ide-disk",
  73. #endif
  74. .brightness_set = pmu_led_set,
  75. };
  76. #ifdef CONFIG_PM
  77. static int pmu_led_sleep_call(struct pmu_sleep_notifier *self, int when)
  78. {
  79. unsigned long flags;
  80. spin_lock_irqsave(&pmu_blink_lock, flags);
  81. switch (when) {
  82. case PBOOK_SLEEP_REQUEST:
  83. sleeping = 1;
  84. break;
  85. case PBOOK_WAKE:
  86. sleeping = 0;
  87. break;
  88. default:
  89. /* do nothing */
  90. break;
  91. }
  92. spin_unlock_irqrestore(&pmu_blink_lock, flags);
  93. return PBOOK_SLEEP_OK;
  94. }
  95. static struct pmu_sleep_notifier via_pmu_led_sleep_notif = {
  96. .notifier_call = pmu_led_sleep_call,
  97. };
  98. #endif
  99. static int __init via_pmu_led_init(void)
  100. {
  101. struct device_node *dt;
  102. const char *model;
  103. /* only do this on keylargo based models */
  104. if (pmu_get_model() != PMU_KEYLARGO_BASED)
  105. return -ENODEV;
  106. dt = of_find_node_by_path("/");
  107. if (dt == NULL)
  108. return -ENODEV;
  109. model = get_property(dt, "model", NULL);
  110. if (model == NULL)
  111. return -ENODEV;
  112. if (strncmp(model, "PowerBook", strlen("PowerBook")) != 0 &&
  113. strncmp(model, "iBook", strlen("iBook")) != 0) {
  114. of_node_put(dt);
  115. /* ignore */
  116. return -ENODEV;
  117. }
  118. of_node_put(dt);
  119. spin_lock_init(&pmu_blink_lock);
  120. /* no outstanding req */
  121. pmu_blink_req.complete = 1;
  122. pmu_blink_req.done = pmu_req_done;
  123. #ifdef CONFIG_PM
  124. pmu_register_sleep_notifier(&via_pmu_led_sleep_notif);
  125. #endif
  126. return led_classdev_register(NULL, &pmu_led);
  127. }
  128. late_initcall(via_pmu_led_init);