leds-ixp4xx-gpio.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * IXP4XX GPIO driver LED driver
  3. *
  4. * Author: John Bowler <jbowler@acm.org>
  5. *
  6. * Copyright (c) 2006 John Bowler
  7. *
  8. * Permission is hereby granted, free of charge, to any
  9. * person obtaining a copy of this software and associated
  10. * documentation files (the "Software"), to deal in the
  11. * Software without restriction, including without
  12. * limitation the rights to use, copy, modify, merge,
  13. * publish, distribute, sublicense, and/or sell copies of
  14. * the Software, and to permit persons to whom the
  15. * Software is furnished to do so, subject to the
  16. * following conditions:
  17. *
  18. * The above copyright notice and this permission notice
  19. * shall be included in all copies or substantial portions
  20. * of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
  23. * ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  24. * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  25. * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  26. * SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  27. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  29. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  30. * OTHER DEALINGS IN THE SOFTWARE.
  31. *
  32. */
  33. #include <linux/config.h>
  34. #include <linux/kernel.h>
  35. #include <linux/init.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/spinlock.h>
  38. #include <linux/leds.h>
  39. #include <asm/arch/hardware.h>
  40. extern spinlock_t gpio_lock;
  41. /* Up to 16 gpio lines are possible. */
  42. #define GPIO_MAX 16
  43. static struct ixp4xxgpioled_device {
  44. struct led_classdev ancestor;
  45. int flags;
  46. } ixp4xxgpioled_devices[GPIO_MAX];
  47. void ixp4xxgpioled_brightness_set(struct led_classdev *pled,
  48. enum led_brightness value)
  49. {
  50. const struct ixp4xxgpioled_device *const ixp4xx_dev =
  51. container_of(pled, struct ixp4xxgpioled_device, ancestor);
  52. const u32 gpio_pin = ixp4xx_dev - ixp4xxgpioled_devices;
  53. if (gpio_pin < GPIO_MAX && ixp4xx_dev->ancestor.name != 0) {
  54. /* Set or clear the 'gpio_pin' bit according to the style
  55. * and the required setting (value > 0 == on)
  56. */
  57. const int gpio_value =
  58. (value > 0) == (ixp4xx_dev->flags != IXP4XX_GPIO_LOW) ?
  59. IXP4XX_GPIO_HIGH : IXP4XX_GPIO_LOW;
  60. {
  61. unsigned long flags;
  62. spin_lock_irqsave(&gpio_lock, flags);
  63. gpio_line_set(gpio_pin, gpio_value);
  64. spin_unlock_irqrestore(&gpio_lock, flags);
  65. }
  66. }
  67. }
  68. /* LEDs are described in resources, the following iterates over the valid
  69. * LED resources.
  70. */
  71. #define for_all_leds(i, pdev) \
  72. for (i=0; i<pdev->num_resources; ++i) \
  73. if (pdev->resource[i].start < GPIO_MAX && \
  74. pdev->resource[i].name != 0)
  75. /* The following applies 'operation' to each LED from the given platform,
  76. * the function always returns 0 to allow tail call elimination.
  77. */
  78. static int apply_to_all_leds(struct platform_device *pdev,
  79. void (*operation)(struct led_classdev *pled))
  80. {
  81. int i;
  82. for_all_leds(i, pdev)
  83. operation(&ixp4xxgpioled_devices[pdev->resource[i].start].ancestor);
  84. return 0;
  85. }
  86. #ifdef CONFIG_PM
  87. static int ixp4xxgpioled_suspend(struct platform_device *pdev,
  88. pm_message_t state)
  89. {
  90. return apply_to_all_leds(pdev, led_classdev_suspend);
  91. }
  92. static int ixp4xxgpioled_resume(struct platform_device *pdev)
  93. {
  94. return apply_to_all_leds(pdev, led_classdev_resume);
  95. }
  96. #endif
  97. static void ixp4xxgpioled_remove_one_led(struct led_classdev *pled)
  98. {
  99. led_classdev_unregister(pled);
  100. pled->name = 0;
  101. }
  102. static int ixp4xxgpioled_remove(struct platform_device *pdev)
  103. {
  104. return apply_to_all_leds(pdev, ixp4xxgpioled_remove_one_led);
  105. }
  106. static int ixp4xxgpioled_probe(struct platform_device *pdev)
  107. {
  108. /* The board level has to tell the driver where the
  109. * LEDs are connected - there is no way to find out
  110. * electrically. It must also say whether the GPIO
  111. * lines are active high or active low.
  112. *
  113. * To do this read the num_resources (the number of
  114. * LEDs) and the struct resource (the data for each
  115. * LED). The name comes from the resource, and it
  116. * isn't copied.
  117. */
  118. int i;
  119. for_all_leds(i, pdev) {
  120. const u8 gpio_pin = pdev->resource[i].start;
  121. int rc;
  122. if (ixp4xxgpioled_devices[gpio_pin].ancestor.name == 0) {
  123. unsigned long flags;
  124. spin_lock_irqsave(&gpio_lock, flags);
  125. gpio_line_config(gpio_pin, IXP4XX_GPIO_OUT);
  126. /* The config can, apparently, reset the state,
  127. * I suspect the gpio line may be an input and
  128. * the config may cause the line to be latched,
  129. * so the setting depends on how the LED is
  130. * connected to the line (which affects how it
  131. * floats if not driven).
  132. */
  133. gpio_line_set(gpio_pin, IXP4XX_GPIO_HIGH);
  134. spin_unlock_irqrestore(&gpio_lock, flags);
  135. ixp4xxgpioled_devices[gpio_pin].flags =
  136. pdev->resource[i].flags & IORESOURCE_BITS;
  137. ixp4xxgpioled_devices[gpio_pin].ancestor.name =
  138. pdev->resource[i].name;
  139. /* This is how a board manufacturer makes the LED
  140. * come on on reset - the GPIO line will be high, so
  141. * make the LED light when the line is low...
  142. */
  143. if (ixp4xxgpioled_devices[gpio_pin].flags != IXP4XX_GPIO_LOW)
  144. ixp4xxgpioled_devices[gpio_pin].ancestor.brightness = 100;
  145. else
  146. ixp4xxgpioled_devices[gpio_pin].ancestor.brightness = 0;
  147. ixp4xxgpioled_devices[gpio_pin].ancestor.flags = 0;
  148. ixp4xxgpioled_devices[gpio_pin].ancestor.brightness_set =
  149. ixp4xxgpioled_brightness_set;
  150. ixp4xxgpioled_devices[gpio_pin].ancestor.default_trigger = 0;
  151. }
  152. rc = led_classdev_register(&pdev->dev,
  153. &ixp4xxgpioled_devices[gpio_pin].ancestor);
  154. if (rc < 0) {
  155. ixp4xxgpioled_devices[gpio_pin].ancestor.name = 0;
  156. ixp4xxgpioled_remove(pdev);
  157. return rc;
  158. }
  159. }
  160. return 0;
  161. }
  162. static struct platform_driver ixp4xxgpioled_driver = {
  163. .probe = ixp4xxgpioled_probe,
  164. .remove = ixp4xxgpioled_remove,
  165. #ifdef CONFIG_PM
  166. .suspend = ixp4xxgpioled_suspend,
  167. .resume = ixp4xxgpioled_resume,
  168. #endif
  169. .driver = {
  170. .name = "IXP4XX-GPIO-LED",
  171. },
  172. };
  173. static int __init ixp4xxgpioled_init(void)
  174. {
  175. return platform_driver_register(&ixp4xxgpioled_driver);
  176. }
  177. static void __exit ixp4xxgpioled_exit(void)
  178. {
  179. platform_driver_unregister(&ixp4xxgpioled_driver);
  180. }
  181. module_init(ixp4xxgpioled_init);
  182. module_exit(ixp4xxgpioled_exit);
  183. MODULE_AUTHOR("John Bowler <jbowler@acm.org>");
  184. MODULE_DESCRIPTION("IXP4XX GPIO LED driver");
  185. MODULE_LICENSE("Dual MIT/GPL");