leds-ot200.c 2.7 KB

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