leds.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * LED driver for Atmel AT91-based boards.
  3. *
  4. * Copyright (C) SAN People (Pty) Ltd
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/gpio.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/platform_device.h>
  16. #include <mach/board.h>
  17. /* ------------------------------------------------------------------------- */
  18. #if defined(CONFIG_NEW_LEDS)
  19. /*
  20. * New cross-platform LED support.
  21. */
  22. static struct gpio_led_platform_data led_data;
  23. static struct platform_device at91_gpio_leds_device = {
  24. .name = "leds-gpio",
  25. .id = -1,
  26. .dev.platform_data = &led_data,
  27. };
  28. void __init at91_gpio_leds(struct gpio_led *leds, int nr)
  29. {
  30. int i;
  31. if (!nr)
  32. return;
  33. for (i = 0; i < nr; i++)
  34. at91_set_gpio_output(leds[i].gpio, leds[i].active_low);
  35. led_data.leds = leds;
  36. led_data.num_leds = nr;
  37. platform_device_register(&at91_gpio_leds_device);
  38. }
  39. #else
  40. void __init at91_gpio_leds(struct gpio_led *leds, int nr) {}
  41. #endif
  42. /* ------------------------------------------------------------------------- */
  43. #if defined (CONFIG_LEDS_ATMEL_PWM)
  44. /*
  45. * PWM Leds
  46. */
  47. static struct gpio_led_platform_data pwm_led_data;
  48. static struct platform_device at91_pwm_leds_device = {
  49. .name = "leds-atmel-pwm",
  50. .id = -1,
  51. .dev.platform_data = &pwm_led_data,
  52. };
  53. void __init at91_pwm_leds(struct gpio_led *leds, int nr)
  54. {
  55. int i;
  56. u32 pwm_mask = 0;
  57. if (!nr)
  58. return;
  59. for (i = 0; i < nr; i++)
  60. pwm_mask |= (1 << leds[i].gpio);
  61. pwm_led_data.leds = leds;
  62. pwm_led_data.num_leds = nr;
  63. at91_add_device_pwm(pwm_mask);
  64. platform_device_register(&at91_pwm_leds_device);
  65. }
  66. #else
  67. void __init at91_pwm_leds(struct gpio_led *leds, int nr){}
  68. #endif
  69. /* ------------------------------------------------------------------------- */
  70. #if defined(CONFIG_LEDS)
  71. #include <asm/leds.h>
  72. /*
  73. * Old ARM-specific LED framework; not fully functional when generic time is
  74. * in use.
  75. */
  76. static u8 at91_leds_cpu;
  77. static u8 at91_leds_timer;
  78. static inline void at91_led_on(unsigned int led)
  79. {
  80. at91_set_gpio_value(led, 0);
  81. }
  82. static inline void at91_led_off(unsigned int led)
  83. {
  84. at91_set_gpio_value(led, 1);
  85. }
  86. static inline void at91_led_toggle(unsigned int led)
  87. {
  88. unsigned long is_off = at91_get_gpio_value(led);
  89. if (is_off)
  90. at91_led_on(led);
  91. else
  92. at91_led_off(led);
  93. }
  94. /*
  95. * Handle LED events.
  96. */
  97. static void at91_leds_event(led_event_t evt)
  98. {
  99. unsigned long flags;
  100. local_irq_save(flags);
  101. switch(evt) {
  102. case led_start: /* System startup */
  103. at91_led_on(at91_leds_cpu);
  104. break;
  105. case led_stop: /* System stop / suspend */
  106. at91_led_off(at91_leds_cpu);
  107. break;
  108. #ifdef CONFIG_LEDS_TIMER
  109. case led_timer: /* Every 50 timer ticks */
  110. at91_led_toggle(at91_leds_timer);
  111. break;
  112. #endif
  113. #ifdef CONFIG_LEDS_CPU
  114. case led_idle_start: /* Entering idle state */
  115. at91_led_off(at91_leds_cpu);
  116. break;
  117. case led_idle_end: /* Exit idle state */
  118. at91_led_on(at91_leds_cpu);
  119. break;
  120. #endif
  121. default:
  122. break;
  123. }
  124. local_irq_restore(flags);
  125. }
  126. static int __init leds_init(void)
  127. {
  128. if (!at91_leds_timer || !at91_leds_cpu)
  129. return -ENODEV;
  130. leds_event = at91_leds_event;
  131. leds_event(led_start);
  132. return 0;
  133. }
  134. __initcall(leds_init);
  135. void __init at91_init_leds(u8 cpu_led, u8 timer_led)
  136. {
  137. /* Enable GPIO to access the LEDs */
  138. at91_set_gpio_output(cpu_led, 1);
  139. at91_set_gpio_output(timer_led, 1);
  140. at91_leds_cpu = cpu_led;
  141. at91_leds_timer = timer_led;
  142. }
  143. #else
  144. void __init at91_init_leds(u8 cpu_led, u8 timer_led) {}
  145. #endif