0003-leds-add-reset-controller-based-driver.patch 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. From ecbd9c87f073f097d9fe56390353e64e963e866a Mon Sep 17 00:00:00 2001
  2. From: John Crispin <john@phrozen.org>
  3. Date: Tue, 6 Mar 2018 10:03:03 +0100
  4. Subject: [PATCH 03/27] leds: add reset-controller based driver
  5. Signed-off-by: John Crispin <john@phrozen.org>
  6. ---
  7. drivers/leds/Kconfig | 11 ++++
  8. drivers/leds/Makefile | 1 +
  9. drivers/leds/leds-reset.c | 137 ++++++++++++++++++++++++++++++++++++++++++++++
  10. 3 files changed, 149 insertions(+)
  11. create mode 100644 drivers/leds/leds-reset.c
  12. --- a/drivers/leds/Kconfig
  13. +++ b/drivers/leds/Kconfig
  14. @@ -696,6 +696,17 @@ config LEDS_NIC78BX
  15. To compile this driver as a module, choose M here: the module
  16. will be called leds-nic78bx.
  17. +config LEDS_RESET
  18. + tristate "LED support for reset-controller API"
  19. + depends on LEDS_CLASS
  20. + depends on RESET_CONTROLLER
  21. + help
  22. + This option enables support for LEDs connected to pins driven by reset
  23. + controllers. Yes, DNI actual built HW like that.
  24. +
  25. + To compile this driver as a module, choose M here: the module
  26. + will be called leds-reset.
  27. +
  28. comment "LED Triggers"
  29. source "drivers/leds/trigger/Kconfig"
  30. --- /dev/null
  31. +++ b/drivers/leds/leds-reset.c
  32. @@ -0,0 +1,140 @@
  33. +/*
  34. + * Copyright (C) 2018 John Crispin <john@phrozen.org>
  35. + *
  36. + * This program is free software; you can redistribute it and/or modify
  37. + * it under the terms of the GNU General Public License version 2 as
  38. + * published by the Free Software Foundation.
  39. + *
  40. + */
  41. +#include <linux/err.h>
  42. +#include <linux/reset.h>
  43. +#include <linux/kernel.h>
  44. +#include <linux/leds.h>
  45. +#include <linux/module.h>
  46. +#include <linux/of.h>
  47. +#include <linux/platform_device.h>
  48. +#include <linux/reset.h>
  49. +
  50. +struct reset_led_data {
  51. + struct led_classdev cdev;
  52. + struct reset_control *rst;
  53. +};
  54. +
  55. +static inline struct reset_led_data *
  56. + cdev_to_reset_led_data(struct led_classdev *led_cdev)
  57. +{
  58. + return container_of(led_cdev, struct reset_led_data, cdev);
  59. +}
  60. +
  61. +static void reset_led_set(struct led_classdev *led_cdev,
  62. + enum led_brightness value)
  63. +{
  64. + struct reset_led_data *led_dat = cdev_to_reset_led_data(led_cdev);
  65. +
  66. + if (value == LED_OFF)
  67. + reset_control_assert(led_dat->rst);
  68. + else
  69. + reset_control_deassert(led_dat->rst);
  70. +}
  71. +
  72. +struct reset_leds_priv {
  73. + int num_leds;
  74. + struct reset_led_data leds[];
  75. +};
  76. +
  77. +static inline int sizeof_reset_leds_priv(int num_leds)
  78. +{
  79. + return sizeof(struct reset_leds_priv) +
  80. + (sizeof(struct reset_led_data) * num_leds);
  81. +}
  82. +
  83. +static struct reset_leds_priv *reset_leds_create(struct platform_device *pdev)
  84. +{
  85. + struct device *dev = &pdev->dev;
  86. + struct fwnode_handle *child;
  87. + struct reset_leds_priv *priv;
  88. + int count, ret;
  89. +
  90. + count = device_get_child_node_count(dev);
  91. + if (!count)
  92. + return ERR_PTR(-ENODEV);
  93. +
  94. + priv = devm_kzalloc(dev, sizeof_reset_leds_priv(count), GFP_KERNEL);
  95. + if (!priv)
  96. + return ERR_PTR(-ENOMEM);
  97. +
  98. + device_for_each_child_node(dev, child) {
  99. + struct reset_led_data *led = &priv->leds[priv->num_leds];
  100. + struct device_node *np = to_of_node(child);
  101. +
  102. + ret = fwnode_property_read_string(child, "label", &led->cdev.name);
  103. + if (!led->cdev.name) {
  104. + fwnode_handle_put(child);
  105. + return ERR_PTR(-EINVAL);
  106. + }
  107. + led->rst = __of_reset_control_get(np, NULL, 0, 0, 0);
  108. + if (IS_ERR(led->rst))
  109. + return ERR_PTR(-EINVAL);
  110. +
  111. + fwnode_property_read_string(child, "linux,default-trigger",
  112. + &led->cdev.default_trigger);
  113. +
  114. + led->cdev.brightness_set = reset_led_set;
  115. + ret = devm_of_led_classdev_register(&pdev->dev, np, &led->cdev);
  116. + if (ret < 0)
  117. + return ERR_PTR(ret);
  118. + led->cdev.dev->of_node = np;
  119. + priv->num_leds++;
  120. + }
  121. +
  122. + return priv;
  123. +}
  124. +
  125. +static const struct of_device_id of_reset_leds_match[] = {
  126. + { .compatible = "reset-leds", },
  127. + {},
  128. +};
  129. +
  130. +MODULE_DEVICE_TABLE(of, of_reset_leds_match);
  131. +
  132. +static int reset_led_probe(struct platform_device *pdev)
  133. +{
  134. + struct reset_leds_priv *priv;
  135. +
  136. + priv = reset_leds_create(pdev);
  137. + if (IS_ERR(priv))
  138. + return PTR_ERR(priv);
  139. +
  140. + platform_set_drvdata(pdev, priv);
  141. +
  142. + return 0;
  143. +}
  144. +
  145. +static void reset_led_shutdown(struct platform_device *pdev)
  146. +{
  147. + struct reset_leds_priv *priv = platform_get_drvdata(pdev);
  148. + int i;
  149. +
  150. + for (i = 0; i < priv->num_leds; i++) {
  151. + struct reset_led_data *led = &priv->leds[i];
  152. +
  153. + if (!(led->cdev.flags & LED_RETAIN_AT_SHUTDOWN))
  154. + reset_led_set(&led->cdev, LED_OFF);
  155. + }
  156. +}
  157. +
  158. +static struct platform_driver reset_led_driver = {
  159. + .probe = reset_led_probe,
  160. + .shutdown = reset_led_shutdown,
  161. + .driver = {
  162. + .name = "leds-reset",
  163. + .of_match_table = of_reset_leds_match,
  164. + },
  165. +};
  166. +
  167. +module_platform_driver(reset_led_driver);
  168. +
  169. +MODULE_AUTHOR("John Crispin <john@phrozen.org>");
  170. +MODULE_DESCRIPTION("reset controller LED driver");
  171. +MODULE_LICENSE("GPL");
  172. +MODULE_ALIAS("platform:leds-reset");
  173. --- a/drivers/leds/Makefile
  174. +++ b/drivers/leds/Makefile
  175. @@ -73,6 +73,7 @@ obj-$(CONFIG_LEDS_PM8058) += leds-pm805
  176. obj-$(CONFIG_LEDS_MLXCPLD) += leds-mlxcpld.o
  177. obj-$(CONFIG_LEDS_NIC78BX) += leds-nic78bx.o
  178. obj-$(CONFIG_LEDS_MT6323) += leds-mt6323.o
  179. +obj-$(CONFIG_LEDS_RESET) += leds-reset.o
  180. # LED SPI Drivers
  181. obj-$(CONFIG_LEDS_DAC124S085) += leds-dac124s085.o