leds-spi-byte.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2019 Christian Mauderer <oss@c-mauderer.de>
  3. /*
  4. * The driver supports controllers with a very simple SPI protocol:
  5. * - one LED is controlled by a single byte on MOSI
  6. * - the value of the byte gives the brightness between two values (lowest to
  7. * highest)
  8. * - no return value is necessary (no MISO signal)
  9. *
  10. * The value for minimum and maximum brightness depends on the device
  11. * (compatible string).
  12. *
  13. * Supported devices:
  14. * - "ubnt,acb-spi-led": Microcontroller (SONiX 8F26E611LA) based device used
  15. * for example in Ubiquiti airCube ISP. Reverse engineered protocol for this
  16. * controller:
  17. * * Higher two bits set a mode. Lower six bits are a parameter.
  18. * * Mode: 00 -> set brightness between 0x00 (min) and 0x3F (max)
  19. * * Mode: 01 -> pulsing pattern (min -> max -> min) with an interval. From
  20. * some tests, the period is about (50ms + 102ms * parameter). There is a
  21. * slightly different pattern starting from 0x10 (longer gap between the
  22. * pulses) but the time still follows that calculation.
  23. * * Mode: 10 -> same as 01 but with only a ramp from min to max. Again a
  24. * slight jump in the pattern at 0x10.
  25. * * Mode: 11 -> blinking (off -> 25% -> off -> 25% -> ...) with a period of
  26. * (105ms * parameter)
  27. * NOTE: This driver currently only supports mode 00.
  28. */
  29. #include <linux/leds.h>
  30. #include <linux/module.h>
  31. #include <linux/of_device.h>
  32. #include <linux/spi/spi.h>
  33. #include <linux/mutex.h>
  34. #include <uapi/linux/uleds.h>
  35. struct spi_byte_chipdef {
  36. /* SPI byte that will be send to switch the LED off */
  37. u8 off_value;
  38. /* SPI byte that will be send to switch the LED to maximum brightness */
  39. u8 max_value;
  40. };
  41. struct spi_byte_led {
  42. struct led_classdev ldev;
  43. struct spi_device *spi;
  44. char name[LED_MAX_NAME_SIZE];
  45. struct mutex mutex;
  46. const struct spi_byte_chipdef *cdef;
  47. };
  48. static const struct spi_byte_chipdef ubnt_acb_spi_led_cdef = {
  49. .off_value = 0x0,
  50. .max_value = 0x3F,
  51. };
  52. static const struct of_device_id spi_byte_dt_ids[] = {
  53. { .compatible = "ubnt,acb-spi-led", .data = &ubnt_acb_spi_led_cdef },
  54. {},
  55. };
  56. MODULE_DEVICE_TABLE(of, spi_byte_dt_ids);
  57. static int spi_byte_brightness_set_blocking(struct led_classdev *dev,
  58. enum led_brightness brightness)
  59. {
  60. struct spi_byte_led *led = container_of(dev, struct spi_byte_led, ldev);
  61. u8 value;
  62. int ret;
  63. value = (u8) brightness + led->cdef->off_value;
  64. mutex_lock(&led->mutex);
  65. ret = spi_write(led->spi, &value, sizeof(value));
  66. mutex_unlock(&led->mutex);
  67. return ret;
  68. }
  69. static int spi_byte_probe(struct spi_device *spi)
  70. {
  71. struct device_node *child;
  72. struct device *dev = &spi->dev;
  73. struct spi_byte_led *led;
  74. const char *name = "leds-spi-byte::";
  75. const char *state;
  76. int ret;
  77. if (of_get_available_child_count(dev_of_node(dev)) != 1) {
  78. dev_err(dev, "Device must have exactly one LED sub-node.");
  79. return -EINVAL;
  80. }
  81. child = of_get_next_available_child(dev_of_node(dev), NULL);
  82. led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
  83. if (!led)
  84. return -ENOMEM;
  85. of_property_read_string(child, "label", &name);
  86. strlcpy(led->name, name, sizeof(led->name));
  87. led->spi = spi;
  88. mutex_init(&led->mutex);
  89. led->cdef = device_get_match_data(dev);
  90. led->ldev.name = led->name;
  91. led->ldev.brightness = LED_OFF;
  92. led->ldev.max_brightness = led->cdef->max_value - led->cdef->off_value;
  93. led->ldev.brightness_set_blocking = spi_byte_brightness_set_blocking;
  94. state = of_get_property(child, "default-state", NULL);
  95. if (state) {
  96. if (!strcmp(state, "on")) {
  97. led->ldev.brightness = led->ldev.max_brightness;
  98. } else if (strcmp(state, "off")) {
  99. /* all other cases except "off" */
  100. dev_err(dev, "default-state can only be 'on' or 'off'");
  101. return -EINVAL;
  102. }
  103. }
  104. spi_byte_brightness_set_blocking(&led->ldev,
  105. led->ldev.brightness);
  106. ret = devm_led_classdev_register(&spi->dev, &led->ldev);
  107. if (ret) {
  108. mutex_destroy(&led->mutex);
  109. return ret;
  110. }
  111. spi_set_drvdata(spi, led);
  112. return 0;
  113. }
  114. static int spi_byte_remove(struct spi_device *spi)
  115. {
  116. struct spi_byte_led *led = spi_get_drvdata(spi);
  117. mutex_destroy(&led->mutex);
  118. return 0;
  119. }
  120. static struct spi_driver spi_byte_driver = {
  121. .probe = spi_byte_probe,
  122. .remove = spi_byte_remove,
  123. .driver = {
  124. .name = KBUILD_MODNAME,
  125. .of_match_table = spi_byte_dt_ids,
  126. },
  127. };
  128. module_spi_driver(spi_byte_driver);
  129. MODULE_AUTHOR("Christian Mauderer <oss@c-mauderer.de>");
  130. MODULE_DESCRIPTION("single byte SPI LED driver");
  131. MODULE_LICENSE("GPL v2");
  132. MODULE_ALIAS("spi:leds-spi-byte");