leds-bcm6358.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #ifdef CONFIG_CPU_BIG_ENDIAN
  47. iowrite32be(data, reg);
  48. #else
  49. writel(data, reg);
  50. #endif
  51. }
  52. static unsigned long bcm6358_led_read(void __iomem *reg)
  53. {
  54. #ifdef CONFIG_CPU_BIG_ENDIAN
  55. return ioread32be(reg);
  56. #else
  57. return readl(reg);
  58. #endif
  59. }
  60. static unsigned long bcm6358_led_busy(void __iomem *mem)
  61. {
  62. unsigned long val;
  63. while ((val = bcm6358_led_read(mem + BCM6358_REG_CTRL)) &
  64. BCM6358_SLED_BUSY)
  65. udelay(BCM6358_SLED_WAIT);
  66. return val;
  67. }
  68. static void bcm6358_led_set(struct led_classdev *led_cdev,
  69. enum led_brightness value)
  70. {
  71. struct bcm6358_led *led =
  72. container_of(led_cdev, struct bcm6358_led, cdev);
  73. unsigned long flags, val;
  74. spin_lock_irqsave(led->lock, flags);
  75. bcm6358_led_busy(led->mem);
  76. val = bcm6358_led_read(led->mem + BCM6358_REG_MODE);
  77. if ((led->active_low && value == LED_OFF) ||
  78. (!led->active_low && value != LED_OFF))
  79. val |= BIT(led->pin);
  80. else
  81. val &= ~(BIT(led->pin));
  82. bcm6358_led_write(led->mem + BCM6358_REG_MODE, val);
  83. spin_unlock_irqrestore(led->lock, flags);
  84. }
  85. static int bcm6358_led(struct device *dev, struct device_node *nc, u32 reg,
  86. void __iomem *mem, spinlock_t *lock)
  87. {
  88. struct bcm6358_led *led;
  89. const char *state;
  90. int rc;
  91. led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
  92. if (!led)
  93. return -ENOMEM;
  94. led->pin = reg;
  95. led->mem = mem;
  96. led->lock = lock;
  97. if (of_property_read_bool(nc, "active-low"))
  98. led->active_low = true;
  99. led->cdev.name = of_get_property(nc, "label", NULL) ? : nc->name;
  100. led->cdev.default_trigger = of_get_property(nc,
  101. "linux,default-trigger",
  102. NULL);
  103. if (!of_property_read_string(nc, "default-state", &state)) {
  104. if (!strcmp(state, "on")) {
  105. led->cdev.brightness = LED_FULL;
  106. } else if (!strcmp(state, "keep")) {
  107. unsigned long val;
  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_set(&led->cdev, led->cdev.brightness);
  122. led->cdev.brightness_set = bcm6358_led_set;
  123. rc = led_classdev_register(dev, &led->cdev);
  124. if (rc < 0)
  125. return rc;
  126. dev_dbg(dev, "registered LED %s\n", led->cdev.name);
  127. return 0;
  128. }
  129. static int bcm6358_leds_probe(struct platform_device *pdev)
  130. {
  131. struct device *dev = &pdev->dev;
  132. struct device_node *np = pdev->dev.of_node;
  133. struct device_node *child;
  134. struct resource *mem_r;
  135. void __iomem *mem;
  136. spinlock_t *lock; /* memory lock */
  137. unsigned long val;
  138. u32 clk_div;
  139. mem_r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  140. if (!mem_r)
  141. return -EINVAL;
  142. mem = devm_ioremap_resource(dev, mem_r);
  143. if (IS_ERR(mem))
  144. return PTR_ERR(mem);
  145. lock = devm_kzalloc(dev, sizeof(*lock), GFP_KERNEL);
  146. if (!lock)
  147. return -ENOMEM;
  148. spin_lock_init(lock);
  149. val = bcm6358_led_busy(mem);
  150. val &= ~(BCM6358_SLED_POLARITY | BCM6358_SLED_CLKDIV_MASK);
  151. if (of_property_read_bool(np, "brcm,clk-dat-low"))
  152. val |= BCM6358_SLED_POLARITY;
  153. of_property_read_u32(np, "brcm,clk-div", &clk_div);
  154. switch (clk_div) {
  155. case 8:
  156. val |= BCM6358_SLED_CLKDIV_8;
  157. break;
  158. case 4:
  159. val |= BCM6358_SLED_CLKDIV_4;
  160. break;
  161. case 2:
  162. val |= BCM6358_SLED_CLKDIV_2;
  163. break;
  164. default:
  165. val |= BCM6358_SLED_CLKDIV_1;
  166. break;
  167. }
  168. bcm6358_led_write(mem + BCM6358_REG_CTRL, val);
  169. for_each_available_child_of_node(np, child) {
  170. int rc;
  171. u32 reg;
  172. if (of_property_read_u32(child, "reg", &reg))
  173. continue;
  174. if (reg >= BCM6358_SLED_MAX_COUNT) {
  175. dev_err(dev, "invalid LED (%u >= %d)\n", reg,
  176. BCM6358_SLED_MAX_COUNT);
  177. continue;
  178. }
  179. rc = bcm6358_led(dev, child, reg, mem, lock);
  180. if (rc < 0) {
  181. of_node_put(child);
  182. return rc;
  183. }
  184. }
  185. return 0;
  186. }
  187. static const struct of_device_id bcm6358_leds_of_match[] = {
  188. { .compatible = "brcm,bcm6358-leds", },
  189. { },
  190. };
  191. MODULE_DEVICE_TABLE(of, bcm6358_leds_of_match);
  192. static struct platform_driver bcm6358_leds_driver = {
  193. .probe = bcm6358_leds_probe,
  194. .driver = {
  195. .name = "leds-bcm6358",
  196. .of_match_table = bcm6358_leds_of_match,
  197. },
  198. };
  199. module_platform_driver(bcm6358_leds_driver);
  200. MODULE_AUTHOR("Álvaro Fernández Rojas <noltari@gmail.com>");
  201. MODULE_DESCRIPTION("LED driver for BCM6358 controllers");
  202. MODULE_LICENSE("GPL v2");
  203. MODULE_ALIAS("platform:leds-bcm6358");