leds-bcm6328.c 11 KB

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