leds-syscon.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. * This driver provides system reboot functionality for APM X-Gene SoC.
  23. * For system shutdown, this is board specify. If a board designer
  24. * implements GPIO shutdown, use the gpio-poweroff.c driver.
  25. */
  26. #include <linux/io.h>
  27. #include <linux/of_device.h>
  28. #include <linux/of_address.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/stat.h>
  31. #include <linux/slab.h>
  32. #include <linux/mfd/syscon.h>
  33. #include <linux/regmap.h>
  34. #include <linux/leds.h>
  35. /**
  36. * struct syscon_led - state container for syscon based LEDs
  37. * @cdev: LED class device for this LED
  38. * @map: regmap to access the syscon device backing this LED
  39. * @offset: the offset into the syscon regmap for the LED register
  40. * @mask: the bit in the register corresponding to the LED
  41. * @state: current state of the LED
  42. */
  43. struct syscon_led {
  44. struct led_classdev cdev;
  45. struct regmap *map;
  46. u32 offset;
  47. u32 mask;
  48. bool state;
  49. };
  50. static void syscon_led_set(struct led_classdev *led_cdev,
  51. enum led_brightness value)
  52. {
  53. struct syscon_led *sled =
  54. container_of(led_cdev, struct syscon_led, cdev);
  55. u32 val;
  56. int ret;
  57. if (value == LED_OFF) {
  58. val = 0;
  59. sled->state = false;
  60. } else {
  61. val = sled->mask;
  62. sled->state = true;
  63. }
  64. ret = regmap_update_bits(sled->map, sled->offset, sled->mask, val);
  65. if (ret < 0)
  66. dev_err(sled->cdev.dev, "error updating LED status\n");
  67. }
  68. static const struct of_device_id syscon_match[] = {
  69. { .compatible = "syscon", },
  70. {},
  71. };
  72. static int __init syscon_leds_init(void)
  73. {
  74. const struct of_device_id *devid;
  75. struct device_node *np;
  76. struct device_node *child;
  77. struct regmap *map;
  78. struct platform_device *pdev;
  79. struct device *dev;
  80. int ret;
  81. np = of_find_matching_node_and_match(NULL, syscon_match,
  82. &devid);
  83. if (!np)
  84. return -ENODEV;
  85. map = syscon_node_to_regmap(np);
  86. if (IS_ERR(map))
  87. return PTR_ERR(map);
  88. /*
  89. * If the map is there, the device should be there, we allocate
  90. * memory on the syscon device's behalf here.
  91. */
  92. pdev = of_find_device_by_node(np);
  93. if (!pdev)
  94. return -ENODEV;
  95. dev = &pdev->dev;
  96. for_each_available_child_of_node(np, child) {
  97. struct syscon_led *sled;
  98. const char *state;
  99. /* Only check for register-bit-leds */
  100. if (of_property_match_string(child, "compatible",
  101. "register-bit-led") < 0)
  102. continue;
  103. sled = devm_kzalloc(dev, sizeof(*sled), GFP_KERNEL);
  104. if (!sled)
  105. return -ENOMEM;
  106. sled->map = map;
  107. if (of_property_read_u32(child, "offset", &sled->offset))
  108. return -EINVAL;
  109. if (of_property_read_u32(child, "mask", &sled->mask))
  110. return -EINVAL;
  111. sled->cdev.name =
  112. of_get_property(child, "label", NULL) ? : child->name;
  113. sled->cdev.default_trigger =
  114. of_get_property(child, "linux,default-trigger", NULL);
  115. state = of_get_property(child, "default-state", NULL);
  116. if (state) {
  117. if (!strcmp(state, "keep")) {
  118. u32 val;
  119. ret = regmap_read(map, sled->offset, &val);
  120. if (ret < 0)
  121. return ret;
  122. sled->state = !!(val & sled->mask);
  123. } else if (!strcmp(state, "on")) {
  124. sled->state = true;
  125. ret = regmap_update_bits(map, sled->offset,
  126. sled->mask,
  127. sled->mask);
  128. if (ret < 0)
  129. return ret;
  130. } else {
  131. sled->state = false;
  132. ret = regmap_update_bits(map, sled->offset,
  133. sled->mask, 0);
  134. if (ret < 0)
  135. return ret;
  136. }
  137. }
  138. sled->cdev.brightness_set = syscon_led_set;
  139. ret = led_classdev_register(dev, &sled->cdev);
  140. if (ret < 0)
  141. return ret;
  142. dev_info(dev, "registered LED %s\n", sled->cdev.name);
  143. }
  144. return 0;
  145. }
  146. device_initcall(syscon_leds_init);