leds-ot200.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Bachmann ot200 leds driver.
  3. *
  4. * Author: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
  5. * Christian Gmeiner <christian.gmeiner@gmail.com>
  6. *
  7. * License: GPL as published by the FSF.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/init.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/slab.h>
  13. #include <linux/leds.h>
  14. #include <linux/io.h>
  15. #include <linux/module.h>
  16. struct ot200_led {
  17. struct led_classdev cdev;
  18. const char *name;
  19. unsigned long port;
  20. u8 mask;
  21. };
  22. /*
  23. * The device has three leds on the back panel (led_err, led_init and led_run)
  24. * and can handle up to seven leds on the front panel.
  25. */
  26. static struct ot200_led leds[] = {
  27. {
  28. .name = "led_run",
  29. .port = 0x5a,
  30. .mask = BIT(0),
  31. },
  32. {
  33. .name = "led_init",
  34. .port = 0x5a,
  35. .mask = BIT(1),
  36. },
  37. {
  38. .name = "led_err",
  39. .port = 0x5a,
  40. .mask = BIT(2),
  41. },
  42. {
  43. .name = "led_1",
  44. .port = 0x49,
  45. .mask = BIT(6),
  46. },
  47. {
  48. .name = "led_2",
  49. .port = 0x49,
  50. .mask = BIT(5),
  51. },
  52. {
  53. .name = "led_3",
  54. .port = 0x49,
  55. .mask = BIT(4),
  56. },
  57. {
  58. .name = "led_4",
  59. .port = 0x49,
  60. .mask = BIT(3),
  61. },
  62. {
  63. .name = "led_5",
  64. .port = 0x49,
  65. .mask = BIT(2),
  66. },
  67. {
  68. .name = "led_6",
  69. .port = 0x49,
  70. .mask = BIT(1),
  71. },
  72. {
  73. .name = "led_7",
  74. .port = 0x49,
  75. .mask = BIT(0),
  76. }
  77. };
  78. static DEFINE_SPINLOCK(value_lock);
  79. /*
  80. * we need to store the current led states, as it is not
  81. * possible to read the current led state via inb().
  82. */
  83. static u8 leds_back;
  84. static u8 leds_front;
  85. static void ot200_led_brightness_set(struct led_classdev *led_cdev,
  86. enum led_brightness value)
  87. {
  88. struct ot200_led *led = container_of(led_cdev, struct ot200_led, cdev);
  89. u8 *val;
  90. unsigned long flags;
  91. spin_lock_irqsave(&value_lock, flags);
  92. if (led->port == 0x49)
  93. val = &leds_front;
  94. else if (led->port == 0x5a)
  95. val = &leds_back;
  96. else
  97. BUG();
  98. if (value == LED_OFF)
  99. *val &= ~led->mask;
  100. else
  101. *val |= led->mask;
  102. outb(*val, led->port);
  103. spin_unlock_irqrestore(&value_lock, flags);
  104. }
  105. static int __devinit ot200_led_probe(struct platform_device *pdev)
  106. {
  107. int i;
  108. int ret;
  109. for (i = 0; i < ARRAY_SIZE(leds); i++) {
  110. leds[i].cdev.name = leds[i].name;
  111. leds[i].cdev.brightness_set = ot200_led_brightness_set;
  112. ret = led_classdev_register(&pdev->dev, &leds[i].cdev);
  113. if (ret < 0)
  114. goto err;
  115. }
  116. leds_front = 0; /* turn off all front leds */
  117. leds_back = BIT(1); /* turn on init led */
  118. outb(leds_front, 0x49);
  119. outb(leds_back, 0x5a);
  120. return 0;
  121. err:
  122. for (i = i - 1; i >= 0; i--)
  123. led_classdev_unregister(&leds[i].cdev);
  124. return ret;
  125. }
  126. static int __devexit ot200_led_remove(struct platform_device *pdev)
  127. {
  128. int i;
  129. for (i = 0; i < ARRAY_SIZE(leds); i++)
  130. led_classdev_unregister(&leds[i].cdev);
  131. return 0;
  132. }
  133. static struct platform_driver ot200_led_driver = {
  134. .probe = ot200_led_probe,
  135. .remove = __devexit_p(ot200_led_remove),
  136. .driver = {
  137. .name = "leds-ot200",
  138. .owner = THIS_MODULE,
  139. },
  140. };
  141. module_platform_driver(ot200_led_driver);
  142. MODULE_AUTHOR("Sebastian A. Siewior <bigeasy@linutronix.de>");
  143. MODULE_DESCRIPTION("ot200 LED driver");
  144. MODULE_LICENSE("GPL");
  145. MODULE_ALIAS("platform:leds-ot200");