leds-syscon.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Generic Syscon LEDs Driver
  3. *
  4. * Copyright (c) 2014, Linaro Limited
  5. * Author: Linus Walleij <linus.walleij@linaro.org>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include <linux/io.h>
  23. #include <linux/of_device.h>
  24. #include <linux/of_address.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/stat.h>
  27. #include <linux/slab.h>
  28. #include <linux/mfd/syscon.h>
  29. #include <linux/regmap.h>
  30. #include <linux/leds.h>
  31. /**
  32. * struct syscon_led - state container for syscon based LEDs
  33. * @cdev: LED class device for this LED
  34. * @map: regmap to access the syscon device backing this LED
  35. * @offset: the offset into the syscon regmap for the LED register
  36. * @mask: the bit in the register corresponding to the LED
  37. * @state: current state of the LED
  38. */
  39. struct syscon_led {
  40. struct led_classdev cdev;
  41. struct regmap *map;
  42. u32 offset;
  43. u32 mask;
  44. bool state;
  45. };
  46. static void syscon_led_set(struct led_classdev *led_cdev,
  47. enum led_brightness value)
  48. {
  49. struct syscon_led *sled =
  50. container_of(led_cdev, struct syscon_led, cdev);
  51. u32 val;
  52. int ret;
  53. if (value == LED_OFF) {
  54. val = 0;
  55. sled->state = false;
  56. } else {
  57. val = sled->mask;
  58. sled->state = true;
  59. }
  60. ret = regmap_update_bits(sled->map, sled->offset, sled->mask, val);
  61. if (ret < 0)
  62. dev_err(sled->cdev.dev, "error updating LED status\n");
  63. }
  64. static int __init syscon_leds_spawn(struct device_node *np,
  65. struct device *dev,
  66. struct regmap *map)
  67. {
  68. struct device_node *child;
  69. int ret;
  70. for_each_available_child_of_node(np, child) {
  71. struct syscon_led *sled;
  72. const char *state;
  73. /* Only check for register-bit-leds */
  74. if (of_property_match_string(child, "compatible",
  75. "register-bit-led") < 0)
  76. continue;
  77. sled = devm_kzalloc(dev, sizeof(*sled), GFP_KERNEL);
  78. if (!sled)
  79. return -ENOMEM;
  80. sled->map = map;
  81. if (of_property_read_u32(child, "offset", &sled->offset))
  82. return -EINVAL;
  83. if (of_property_read_u32(child, "mask", &sled->mask))
  84. return -EINVAL;
  85. sled->cdev.name =
  86. of_get_property(child, "label", NULL) ? : child->name;
  87. sled->cdev.default_trigger =
  88. of_get_property(child, "linux,default-trigger", NULL);
  89. state = of_get_property(child, "default-state", NULL);
  90. if (state) {
  91. if (!strcmp(state, "keep")) {
  92. u32 val;
  93. ret = regmap_read(map, sled->offset, &val);
  94. if (ret < 0)
  95. return ret;
  96. sled->state = !!(val & sled->mask);
  97. } else if (!strcmp(state, "on")) {
  98. sled->state = true;
  99. ret = regmap_update_bits(map, sled->offset,
  100. sled->mask,
  101. sled->mask);
  102. if (ret < 0)
  103. return ret;
  104. } else {
  105. sled->state = false;
  106. ret = regmap_update_bits(map, sled->offset,
  107. sled->mask, 0);
  108. if (ret < 0)
  109. return ret;
  110. }
  111. }
  112. sled->cdev.brightness_set = syscon_led_set;
  113. ret = led_classdev_register(dev, &sled->cdev);
  114. if (ret < 0)
  115. return ret;
  116. dev_info(dev, "registered LED %s\n", sled->cdev.name);
  117. }
  118. return 0;
  119. }
  120. static int __init syscon_leds_init(void)
  121. {
  122. struct device_node *np;
  123. for_each_of_allnodes(np) {
  124. struct platform_device *pdev;
  125. struct regmap *map;
  126. int ret;
  127. if (!of_device_is_compatible(np, "syscon"))
  128. continue;
  129. map = syscon_node_to_regmap(np);
  130. if (IS_ERR(map)) {
  131. pr_err("error getting regmap for syscon LEDs\n");
  132. continue;
  133. }
  134. /*
  135. * If the map is there, the device should be there, we allocate
  136. * memory on the syscon device's behalf here.
  137. */
  138. pdev = of_find_device_by_node(np);
  139. if (!pdev)
  140. return -ENODEV;
  141. ret = syscon_leds_spawn(np, &pdev->dev, map);
  142. if (ret)
  143. dev_err(&pdev->dev, "could not spawn syscon LEDs\n");
  144. }
  145. return 0;
  146. }
  147. device_initcall(syscon_leds_init);