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