leds-bcm6328.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * Driver for BCM6328 memory-mapped LEDs, based on leds-syscon.c
  3. *
  4. * Copyright 2015 Álvaro Fernández Rojas <noltari@gmail.com>
  5. * Copyright 2015 Jonas Gorski <jogo@openwrt.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. */
  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 BCM6328_REG_INIT 0x00
  19. #define BCM6328_REG_MODE_HI 0x04
  20. #define BCM6328_REG_MODE_LO 0x08
  21. #define BCM6328_REG_HWDIS 0x0c
  22. #define BCM6328_REG_STROBE 0x10
  23. #define BCM6328_REG_LNKACTSEL_HI 0x14
  24. #define BCM6328_REG_LNKACTSEL_LO 0x18
  25. #define BCM6328_REG_RBACK 0x1c
  26. #define BCM6328_REG_SERMUX 0x20
  27. #define BCM6328_LED_MAX_COUNT 24
  28. #define BCM6328_LED_DEF_DELAY 500
  29. #define BCM6328_LED_INTERVAL_MS 20
  30. #define BCM6328_LED_INTV_MASK 0x3f
  31. #define BCM6328_LED_FAST_INTV_SHIFT 6
  32. #define BCM6328_LED_FAST_INTV_MASK (BCM6328_LED_INTV_MASK << \
  33. BCM6328_LED_FAST_INTV_SHIFT)
  34. #define BCM6328_SERIAL_LED_EN BIT(12)
  35. #define BCM6328_SERIAL_LED_MUX BIT(13)
  36. #define BCM6328_SERIAL_LED_CLK_NPOL BIT(14)
  37. #define BCM6328_SERIAL_LED_DATA_PPOL BIT(15)
  38. #define BCM6328_SERIAL_LED_SHIFT_DIR BIT(16)
  39. #define BCM6328_LED_SHIFT_TEST BIT(30)
  40. #define BCM6328_LED_TEST BIT(31)
  41. #define BCM6328_INIT_MASK (BCM6328_SERIAL_LED_EN | \
  42. BCM6328_SERIAL_LED_MUX | \
  43. BCM6328_SERIAL_LED_CLK_NPOL | \
  44. BCM6328_SERIAL_LED_DATA_PPOL | \
  45. BCM6328_SERIAL_LED_SHIFT_DIR)
  46. #define BCM6328_LED_MODE_MASK 3
  47. #define BCM6328_LED_MODE_OFF 0
  48. #define BCM6328_LED_MODE_FAST 1
  49. #define BCM6328_LED_MODE_BLINK 2
  50. #define BCM6328_LED_MODE_ON 3
  51. #define BCM6328_LED_SHIFT(X) ((X) << 1)
  52. /**
  53. * struct bcm6328_led - state container for bcm6328 based LEDs
  54. * @cdev: LED class device for this LED
  55. * @mem: memory resource
  56. * @lock: memory lock
  57. * @pin: LED pin number
  58. * @blink_leds: blinking LEDs
  59. * @blink_delay: blinking delay
  60. * @active_low: LED is active low
  61. */
  62. struct bcm6328_led {
  63. struct led_classdev cdev;
  64. void __iomem *mem;
  65. spinlock_t *lock;
  66. unsigned long pin;
  67. unsigned long *blink_leds;
  68. unsigned long *blink_delay;
  69. bool active_low;
  70. };
  71. static void bcm6328_led_write(void __iomem *reg, unsigned long data)
  72. {
  73. iowrite32be(data, reg);
  74. }
  75. static unsigned long bcm6328_led_read(void __iomem *reg)
  76. {
  77. return ioread32be(reg);
  78. }
  79. /**
  80. * LEDMode 64 bits / 24 LEDs
  81. * bits [31:0] -> LEDs 8-23
  82. * bits [47:32] -> LEDs 0-7
  83. * bits [63:48] -> unused
  84. */
  85. static unsigned long bcm6328_pin2shift(unsigned long pin)
  86. {
  87. if (pin < 8)
  88. return pin + 16; /* LEDs 0-7 (bits 47:32) */
  89. else
  90. return pin - 8; /* LEDs 8-23 (bits 31:0) */
  91. }
  92. static void bcm6328_led_mode(struct bcm6328_led *led, unsigned long value)
  93. {
  94. void __iomem *mode;
  95. unsigned long val, shift;
  96. shift = bcm6328_pin2shift(led->pin);
  97. if (shift / 16)
  98. mode = led->mem + BCM6328_REG_MODE_HI;
  99. else
  100. mode = led->mem + BCM6328_REG_MODE_LO;
  101. val = bcm6328_led_read(mode);
  102. val &= ~(BCM6328_LED_MODE_MASK << BCM6328_LED_SHIFT(shift % 16));
  103. val |= (value << BCM6328_LED_SHIFT(shift % 16));
  104. bcm6328_led_write(mode, val);
  105. }
  106. static void bcm6328_led_set(struct led_classdev *led_cdev,
  107. enum led_brightness value)
  108. {
  109. struct bcm6328_led *led =
  110. container_of(led_cdev, struct bcm6328_led, cdev);
  111. unsigned long flags;
  112. spin_lock_irqsave(led->lock, flags);
  113. *(led->blink_leds) &= ~BIT(led->pin);
  114. if ((led->active_low && value == LED_OFF) ||
  115. (!led->active_low && value != LED_OFF))
  116. bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);
  117. else
  118. bcm6328_led_mode(led, BCM6328_LED_MODE_ON);
  119. spin_unlock_irqrestore(led->lock, flags);
  120. }
  121. static int bcm6328_blink_set(struct led_classdev *led_cdev,
  122. unsigned long *delay_on, unsigned long *delay_off)
  123. {
  124. struct bcm6328_led *led =
  125. container_of(led_cdev, struct bcm6328_led, cdev);
  126. unsigned long delay, flags;
  127. if (!*delay_on)
  128. *delay_on = BCM6328_LED_DEF_DELAY;
  129. if (!*delay_off)
  130. *delay_off = BCM6328_LED_DEF_DELAY;
  131. if (*delay_on != *delay_off) {
  132. dev_dbg(led_cdev->dev,
  133. "fallback to soft blinking (delay_on != delay_off)\n");
  134. return -EINVAL;
  135. }
  136. delay = *delay_on / BCM6328_LED_INTERVAL_MS;
  137. if (delay == 0)
  138. delay = 1;
  139. else if (delay > BCM6328_LED_INTV_MASK) {
  140. dev_dbg(led_cdev->dev,
  141. "fallback to soft blinking (delay > %ums)\n",
  142. BCM6328_LED_INTV_MASK * BCM6328_LED_INTERVAL_MS);
  143. return -EINVAL;
  144. }
  145. spin_lock_irqsave(led->lock, flags);
  146. if (*(led->blink_leds) == 0 ||
  147. *(led->blink_leds) == BIT(led->pin) ||
  148. *(led->blink_delay) == delay) {
  149. unsigned long val;
  150. *(led->blink_leds) |= BIT(led->pin);
  151. *(led->blink_delay) = delay;
  152. val = bcm6328_led_read(led->mem + BCM6328_REG_INIT);
  153. val &= ~BCM6328_LED_FAST_INTV_MASK;
  154. val |= (delay << BCM6328_LED_FAST_INTV_SHIFT);
  155. bcm6328_led_write(led->mem + BCM6328_REG_INIT, val);
  156. bcm6328_led_mode(led, BCM6328_LED_MODE_BLINK);
  157. spin_unlock_irqrestore(led->lock, flags);
  158. } else {
  159. spin_unlock_irqrestore(led->lock, flags);
  160. dev_dbg(led_cdev->dev,
  161. "fallback to soft blinking (delay already set)\n");
  162. return -EINVAL;
  163. }
  164. return 0;
  165. }
  166. static int bcm6328_hwled(struct device *dev, struct device_node *nc, u32 reg,
  167. void __iomem *mem, spinlock_t *lock)
  168. {
  169. int i, cnt;
  170. unsigned long flags, val;
  171. spin_lock_irqsave(lock, flags);
  172. val = bcm6328_led_read(mem + BCM6328_REG_HWDIS);
  173. val &= ~BIT(reg);
  174. bcm6328_led_write(mem + BCM6328_REG_HWDIS, val);
  175. spin_unlock_irqrestore(lock, flags);
  176. /* Only LEDs 0-7 can be activity/link controlled */
  177. if (reg >= 8)
  178. return 0;
  179. cnt = of_property_count_elems_of_size(nc, "brcm,link-signal-sources",
  180. sizeof(u32));
  181. for (i = 0; i < cnt; i++) {
  182. u32 sel;
  183. void __iomem *addr;
  184. if (reg < 4)
  185. addr = mem + BCM6328_REG_LNKACTSEL_LO;
  186. else
  187. addr = mem + BCM6328_REG_LNKACTSEL_HI;
  188. of_property_read_u32_index(nc, "brcm,link-signal-sources", i,
  189. &sel);
  190. if (reg / 4 != sel / 4) {
  191. dev_warn(dev, "invalid link signal source\n");
  192. continue;
  193. }
  194. spin_lock_irqsave(lock, flags);
  195. val = bcm6328_led_read(addr);
  196. val |= (BIT(reg) << (((sel % 4) * 4) + 16));
  197. bcm6328_led_write(addr, val);
  198. spin_unlock_irqrestore(lock, flags);
  199. }
  200. cnt = of_property_count_elems_of_size(nc,
  201. "brcm,activity-signal-sources",
  202. sizeof(u32));
  203. for (i = 0; i < cnt; i++) {
  204. u32 sel;
  205. void __iomem *addr;
  206. if (reg < 4)
  207. addr = mem + BCM6328_REG_LNKACTSEL_LO;
  208. else
  209. addr = mem + BCM6328_REG_LNKACTSEL_HI;
  210. of_property_read_u32_index(nc, "brcm,activity-signal-sources",
  211. i, &sel);
  212. if (reg / 4 != sel / 4) {
  213. dev_warn(dev, "invalid activity signal source\n");
  214. continue;
  215. }
  216. spin_lock_irqsave(lock, flags);
  217. val = bcm6328_led_read(addr);
  218. val |= (BIT(reg) << ((sel % 4) * 4));
  219. bcm6328_led_write(addr, val);
  220. spin_unlock_irqrestore(lock, flags);
  221. }
  222. return 0;
  223. }
  224. static int bcm6328_led(struct device *dev, struct device_node *nc, u32 reg,
  225. void __iomem *mem, spinlock_t *lock,
  226. unsigned long *blink_leds, unsigned long *blink_delay)
  227. {
  228. struct bcm6328_led *led;
  229. unsigned long flags;
  230. const char *state;
  231. int rc;
  232. led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
  233. if (!led)
  234. return -ENOMEM;
  235. led->pin = reg;
  236. led->mem = mem;
  237. led->lock = lock;
  238. led->blink_leds = blink_leds;
  239. led->blink_delay = blink_delay;
  240. if (of_property_read_bool(nc, "active-low"))
  241. led->active_low = true;
  242. led->cdev.name = of_get_property(nc, "label", NULL) ? : nc->name;
  243. led->cdev.default_trigger = of_get_property(nc,
  244. "linux,default-trigger",
  245. NULL);
  246. spin_lock_irqsave(lock, flags);
  247. if (!of_property_read_string(nc, "default-state", &state)) {
  248. if (!strcmp(state, "on")) {
  249. led->cdev.brightness = LED_FULL;
  250. } else if (!strcmp(state, "keep")) {
  251. void __iomem *mode;
  252. unsigned long val, shift;
  253. shift = bcm6328_pin2shift(led->pin);
  254. if (shift / 16)
  255. mode = mem + BCM6328_REG_MODE_HI;
  256. else
  257. mode = mem + BCM6328_REG_MODE_LO;
  258. val = bcm6328_led_read(mode) >>
  259. BCM6328_LED_SHIFT(shift % 16);
  260. val &= BCM6328_LED_MODE_MASK;
  261. if ((led->active_low && val == BCM6328_LED_MODE_ON) ||
  262. (!led->active_low && val == BCM6328_LED_MODE_OFF))
  263. led->cdev.brightness = LED_FULL;
  264. else
  265. led->cdev.brightness = LED_OFF;
  266. } else {
  267. led->cdev.brightness = LED_OFF;
  268. }
  269. } else {
  270. led->cdev.brightness = LED_OFF;
  271. }
  272. if ((led->active_low && led->cdev.brightness == LED_FULL) ||
  273. (!led->active_low && led->cdev.brightness == LED_OFF))
  274. bcm6328_led_mode(led, BCM6328_LED_MODE_ON);
  275. else
  276. bcm6328_led_mode(led, BCM6328_LED_MODE_OFF);
  277. spin_unlock_irqrestore(lock, flags);
  278. led->cdev.brightness_set = bcm6328_led_set;
  279. led->cdev.blink_set = bcm6328_blink_set;
  280. rc = led_classdev_register(dev, &led->cdev);
  281. if (rc < 0)
  282. return rc;
  283. dev_dbg(dev, "registered LED %s\n", led->cdev.name);
  284. return 0;
  285. }
  286. static int bcm6328_leds_probe(struct platform_device *pdev)
  287. {
  288. struct device *dev = &pdev->dev;
  289. struct device_node *np = pdev->dev.of_node;
  290. struct device_node *child;
  291. struct resource *mem_r;
  292. void __iomem *mem;
  293. spinlock_t *lock;
  294. unsigned long val, *blink_leds, *blink_delay;
  295. mem_r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  296. if (!mem_r)
  297. return -EINVAL;
  298. mem = devm_ioremap_resource(dev, mem_r);
  299. if (IS_ERR(mem))
  300. return PTR_ERR(mem);
  301. lock = devm_kzalloc(dev, sizeof(*lock), GFP_KERNEL);
  302. if (!lock)
  303. return -ENOMEM;
  304. blink_leds = devm_kzalloc(dev, sizeof(*blink_leds), GFP_KERNEL);
  305. if (!blink_leds)
  306. return -ENOMEM;
  307. blink_delay = devm_kzalloc(dev, sizeof(*blink_delay), GFP_KERNEL);
  308. if (!blink_delay)
  309. return -ENOMEM;
  310. spin_lock_init(lock);
  311. bcm6328_led_write(mem + BCM6328_REG_HWDIS, ~0);
  312. bcm6328_led_write(mem + BCM6328_REG_LNKACTSEL_HI, 0);
  313. bcm6328_led_write(mem + BCM6328_REG_LNKACTSEL_LO, 0);
  314. val = bcm6328_led_read(mem + BCM6328_REG_INIT);
  315. val &= ~(BCM6328_INIT_MASK);
  316. if (of_property_read_bool(np, "brcm,serial-leds"))
  317. val |= BCM6328_SERIAL_LED_EN;
  318. if (of_property_read_bool(np, "brcm,serial-mux"))
  319. val |= BCM6328_SERIAL_LED_MUX;
  320. if (of_property_read_bool(np, "brcm,serial-clk-low"))
  321. val |= BCM6328_SERIAL_LED_CLK_NPOL;
  322. if (!of_property_read_bool(np, "brcm,serial-dat-low"))
  323. val |= BCM6328_SERIAL_LED_DATA_PPOL;
  324. if (!of_property_read_bool(np, "brcm,serial-shift-inv"))
  325. val |= BCM6328_SERIAL_LED_SHIFT_DIR;
  326. bcm6328_led_write(mem + BCM6328_REG_INIT, val);
  327. for_each_available_child_of_node(np, child) {
  328. int rc;
  329. u32 reg;
  330. if (of_property_read_u32(child, "reg", &reg))
  331. continue;
  332. if (reg >= BCM6328_LED_MAX_COUNT) {
  333. dev_err(dev, "invalid LED (%u >= %d)\n", reg,
  334. BCM6328_LED_MAX_COUNT);
  335. continue;
  336. }
  337. if (of_property_read_bool(child, "brcm,hardware-controlled"))
  338. rc = bcm6328_hwled(dev, child, reg, mem, lock);
  339. else
  340. rc = bcm6328_led(dev, child, reg, mem, lock,
  341. blink_leds, blink_delay);
  342. if (rc < 0) {
  343. of_node_put(child);
  344. return rc;
  345. }
  346. }
  347. return 0;
  348. }
  349. static const struct of_device_id bcm6328_leds_of_match[] = {
  350. { .compatible = "brcm,bcm6328-leds", },
  351. { },
  352. };
  353. MODULE_DEVICE_TABLE(of, bcm6328_leds_of_match);
  354. static struct platform_driver bcm6328_leds_driver = {
  355. .probe = bcm6328_leds_probe,
  356. .driver = {
  357. .name = "leds-bcm6328",
  358. .of_match_table = bcm6328_leds_of_match,
  359. },
  360. };
  361. module_platform_driver(bcm6328_leds_driver);
  362. MODULE_AUTHOR("Álvaro Fernández Rojas <noltari@gmail.com>");
  363. MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
  364. MODULE_DESCRIPTION("LED driver for BCM6328 controllers");
  365. MODULE_LICENSE("GPL v2");
  366. MODULE_ALIAS("platform:leds-bcm6328");