leds-spi-byte.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/mod_devicetable.h>
  31. #include <linux/module.h>
  32. #include <linux/mutex.h>
  33. #include <linux/property.h>
  34. #include <linux/spi/spi.h>
  35. #include <uapi/linux/uleds.h>
  36. struct spi_byte_chipdef {
  37. /* SPI byte that will be send to switch the LED off */
  38. u8 off_value;
  39. /* SPI byte that will be send to switch the LED to maximum brightness */
  40. u8 max_value;
  41. };
  42. struct spi_byte_led {
  43. struct led_classdev ldev;
  44. struct spi_device *spi;
  45. char name[LED_MAX_NAME_SIZE];
  46. struct mutex mutex;
  47. const struct spi_byte_chipdef *cdef;
  48. };
  49. static const struct spi_byte_chipdef ubnt_acb_spi_led_cdef = {
  50. .off_value = 0x0,
  51. .max_value = 0x3F,
  52. };
  53. static int spi_byte_brightness_set_blocking(struct led_classdev *dev,
  54. enum led_brightness brightness)
  55. {
  56. struct spi_byte_led *led = container_of(dev, struct spi_byte_led, ldev);
  57. u8 value;
  58. int ret;
  59. value = (u8) brightness + led->cdef->off_value;
  60. mutex_lock(&led->mutex);
  61. ret = spi_write(led->spi, &value, sizeof(value));
  62. mutex_unlock(&led->mutex);
  63. return ret;
  64. }
  65. static int spi_byte_probe(struct spi_device *spi)
  66. {
  67. struct fwnode_handle *child __free(fwnode_handle) = NULL;
  68. struct device *dev = &spi->dev;
  69. struct spi_byte_led *led;
  70. struct led_init_data init_data = {};
  71. enum led_default_state state;
  72. int ret;
  73. if (device_get_child_node_count(dev) != 1) {
  74. dev_err(dev, "Device must have exactly one LED sub-node.");
  75. return -EINVAL;
  76. }
  77. led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
  78. if (!led)
  79. return -ENOMEM;
  80. ret = devm_mutex_init(dev, &led->mutex);
  81. if (ret)
  82. return ret;
  83. led->spi = spi;
  84. led->cdef = device_get_match_data(dev);
  85. led->ldev.brightness = LED_OFF;
  86. led->ldev.max_brightness = led->cdef->max_value - led->cdef->off_value;
  87. led->ldev.brightness_set_blocking = spi_byte_brightness_set_blocking;
  88. child = device_get_next_child_node(dev, NULL);
  89. state = led_init_default_state_get(child);
  90. if (state == LEDS_DEFSTATE_ON)
  91. led->ldev.brightness = led->ldev.max_brightness;
  92. spi_byte_brightness_set_blocking(&led->ldev,
  93. led->ldev.brightness);
  94. init_data.fwnode = child;
  95. init_data.devicename = "leds-spi-byte";
  96. init_data.default_label = ":";
  97. return devm_led_classdev_register_ext(dev, &led->ldev, &init_data);
  98. }
  99. static const struct of_device_id spi_byte_dt_ids[] = {
  100. { .compatible = "ubnt,acb-spi-led", .data = &ubnt_acb_spi_led_cdef },
  101. {}
  102. };
  103. MODULE_DEVICE_TABLE(of, spi_byte_dt_ids);
  104. static struct spi_driver spi_byte_driver = {
  105. .probe = spi_byte_probe,
  106. .driver = {
  107. .name = KBUILD_MODNAME,
  108. .of_match_table = spi_byte_dt_ids,
  109. },
  110. };
  111. module_spi_driver(spi_byte_driver);
  112. MODULE_AUTHOR("Christian Mauderer <oss@c-mauderer.de>");
  113. MODULE_DESCRIPTION("single byte SPI LED driver");
  114. MODULE_LICENSE("GPL v2");
  115. MODULE_ALIAS("spi:leds-spi-byte");