leds-bcm6358.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Driver for BCM6358 memory-mapped LEDs, based on leds-syscon.c
  3. *
  4. * Copyright 2015 Álvaro Fernández Rojas <noltari@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/io.h>
  13. #include <linux/leds.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/spinlock.h>
  18. #define BCM6358_REG_MODE 0x0
  19. #define BCM6358_REG_CTRL 0x4
  20. #define BCM6358_SLED_CLKDIV_MASK 3
  21. #define BCM6358_SLED_CLKDIV_1 0
  22. #define BCM6358_SLED_CLKDIV_2 1
  23. #define BCM6358_SLED_CLKDIV_4 2
  24. #define BCM6358_SLED_CLKDIV_8 3
  25. #define BCM6358_SLED_POLARITY BIT(2)
  26. #define BCM6358_SLED_BUSY BIT(3)
  27. #define BCM6358_SLED_MAX_COUNT 32
  28. #define BCM6358_SLED_WAIT 100
  29. /**
  30. * struct bcm6358_led - state container for bcm6358 based LEDs
  31. * @cdev: LED class device for this LED
  32. * @mem: memory resource
  33. * @lock: memory lock
  34. * @pin: LED pin number
  35. * @active_low: LED is active low
  36. */
  37. struct bcm6358_led {
  38. struct led_classdev cdev;
  39. void __iomem *mem;
  40. spinlock_t *lock;
  41. unsigned long pin;
  42. bool active_low;
  43. };
  44. static void bcm6358_led_write(void __iomem *reg, unsigned long data)
  45. {
  46. iowrite32be(data, reg);
  47. }
  48. static unsigned long bcm6358_led_read(void __iomem *reg)
  49. {
  50. return ioread32be(reg);
  51. }
  52. static unsigned long bcm6358_led_busy(void __iomem *mem)
  53. {
  54. unsigned long val;
  55. while ((val = bcm6358_led_read(mem + BCM6358_REG_CTRL)) &
  56. BCM6358_SLED_BUSY)
  57. udelay(BCM6358_SLED_WAIT);
  58. return val;
  59. }
  60. static void bcm6358_led_mode(struct bcm6358_led *led, unsigned long value)
  61. {
  62. unsigned long val;
  63. bcm6358_led_busy(led->mem);
  64. val = bcm6358_led_read(led->mem + BCM6358_REG_MODE);
  65. if ((led->active_low && value == LED_OFF) ||
  66. (!led->active_low && value != LED_OFF))
  67. val |= BIT(led->pin);
  68. else
  69. val &= ~(BIT(led->pin));
  70. bcm6358_led_write(led->mem + BCM6358_REG_MODE, val);
  71. }
  72. static void bcm6358_led_set(struct led_classdev *led_cdev,
  73. enum led_brightness value)
  74. {
  75. struct bcm6358_led *led =
  76. container_of(led_cdev, struct bcm6358_led, cdev);
  77. unsigned long flags;
  78. spin_lock_irqsave(led->lock, flags);
  79. bcm6358_led_mode(led, value);
  80. spin_unlock_irqrestore(led->lock, flags);
  81. }
  82. static int bcm6358_led(struct device *dev, struct device_node *nc, u32 reg,
  83. void __iomem *mem, spinlock_t *lock)
  84. {
  85. struct bcm6358_led *led;
  86. unsigned long flags;
  87. const char *state;
  88. int rc;
  89. led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
  90. if (!led)
  91. return -ENOMEM;
  92. led->pin = reg;
  93. led->mem = mem;
  94. led->lock = lock;
  95. if (of_property_read_bool(nc, "active-low"))
  96. led->active_low = true;
  97. led->cdev.name = of_get_property(nc, "label", NULL) ? : nc->name;
  98. led->cdev.default_trigger = of_get_property(nc,
  99. "linux,default-trigger",
  100. NULL);
  101. spin_lock_irqsave(lock, flags);
  102. if (!of_property_read_string(nc, "default-state", &state)) {
  103. if (!strcmp(state, "on")) {
  104. led->cdev.brightness = LED_FULL;
  105. } else if (!strcmp(state, "keep")) {
  106. unsigned long val;
  107. bcm6358_led_busy(led->mem);
  108. val = bcm6358_led_read(led->mem + BCM6358_REG_MODE);
  109. val &= BIT(led->pin);
  110. if ((led->active_low && !val) ||
  111. (!led->active_low && val))
  112. led->cdev.brightness = LED_FULL;
  113. else
  114. led->cdev.brightness = LED_OFF;
  115. } else {
  116. led->cdev.brightness = LED_OFF;
  117. }
  118. } else {
  119. led->cdev.brightness = LED_OFF;
  120. }
  121. bcm6358_led_mode(led, led->cdev.brightness);
  122. spin_unlock_irqrestore(lock, flags);
  123. led->cdev.brightness_set = bcm6358_led_set;
  124. rc = led_classdev_register(dev, &led->cdev);
  125. if (rc < 0)
  126. return rc;
  127. dev_dbg(dev, "registered LED %s\n", led->cdev.name);
  128. return 0;
  129. }
  130. static int bcm6358_leds_probe(struct platform_device *pdev)
  131. {
  132. struct device *dev = &pdev->dev;
  133. struct device_node *np = pdev->dev.of_node;
  134. struct device_node *child;
  135. struct resource *mem_r;
  136. void __iomem *mem;
  137. spinlock_t *lock; /* memory lock */
  138. unsigned long val;
  139. u32 clk_div;
  140. mem_r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  141. if (!mem_r)
  142. return -EINVAL;
  143. mem = devm_ioremap_resource(dev, mem_r);
  144. if (IS_ERR(mem))
  145. return PTR_ERR(mem);
  146. lock = devm_kzalloc(dev, sizeof(*lock), GFP_KERNEL);
  147. if (!lock)
  148. return -ENOMEM;
  149. spin_lock_init(lock);
  150. val = bcm6358_led_busy(mem);
  151. val &= ~(BCM6358_SLED_POLARITY | BCM6358_SLED_CLKDIV_MASK);
  152. if (of_property_read_bool(np, "brcm,clk-dat-low"))
  153. val |= BCM6358_SLED_POLARITY;
  154. of_property_read_u32(np, "brcm,clk-div", &clk_div);
  155. switch (clk_div) {
  156. case 8:
  157. val |= BCM6358_SLED_CLKDIV_8;
  158. break;
  159. case 4:
  160. val |= BCM6358_SLED_CLKDIV_4;
  161. break;
  162. case 2:
  163. val |= BCM6358_SLED_CLKDIV_2;
  164. break;
  165. default:
  166. val |= BCM6358_SLED_CLKDIV_1;
  167. break;
  168. }
  169. bcm6358_led_write(mem + BCM6358_REG_CTRL, val);
  170. for_each_available_child_of_node(np, child) {
  171. int rc;
  172. u32 reg;
  173. if (of_property_read_u32(child, "reg", &reg))
  174. continue;
  175. if (reg >= BCM6358_SLED_MAX_COUNT) {
  176. dev_err(dev, "invalid LED (%u >= %d)\n", reg,
  177. BCM6358_SLED_MAX_COUNT);
  178. continue;
  179. }
  180. rc = bcm6358_led(dev, child, reg, mem, lock);
  181. if (rc < 0) {
  182. of_node_put(child);
  183. return rc;
  184. }
  185. }
  186. return 0;
  187. }
  188. static const struct of_device_id bcm6358_leds_of_match[] = {
  189. { .compatible = "brcm,bcm6358-leds", },
  190. { },
  191. };
  192. MODULE_DEVICE_TABLE(of, bcm6358_leds_of_match);
  193. static struct platform_driver bcm6358_leds_driver = {
  194. .probe = bcm6358_leds_probe,
  195. .driver = {
  196. .name = "leds-bcm6358",
  197. .of_match_table = bcm6358_leds_of_match,
  198. },
  199. };
  200. module_platform_driver(bcm6358_leds_driver);
  201. MODULE_AUTHOR("Álvaro Fernández Rojas <noltari@gmail.com>");
  202. MODULE_DESCRIPTION("LED driver for BCM6358 controllers");
  203. MODULE_LICENSE("GPL v2");
  204. MODULE_ALIAS("platform:leds-bcm6358");