brintos

brintos / linux-shallow public Read only

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