1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include <linux/leds.h>
32#include <linux/module.h>
33#include <linux/of_device.h>
34#include <linux/spi/spi.h>
35#include <linux/mutex.h>
36#include <uapi/linux/uleds.h>
37
38struct spi_byte_chipdef {
39
40 u8 off_value;
41
42 u8 max_value;
43};
44
45struct spi_byte_led {
46 struct led_classdev ldev;
47 struct spi_device *spi;
48 char name[LED_MAX_NAME_SIZE];
49 struct mutex mutex;
50 const struct spi_byte_chipdef *cdef;
51};
52
53static const struct spi_byte_chipdef ubnt_acb_spi_led_cdef = {
54 .off_value = 0x0,
55 .max_value = 0x3F,
56};
57
58static const struct of_device_id spi_byte_dt_ids[] = {
59 { .compatible = "ubnt,acb-spi-led", .data = &ubnt_acb_spi_led_cdef },
60 {},
61};
62
63MODULE_DEVICE_TABLE(of, spi_byte_dt_ids);
64
65static int spi_byte_brightness_set_blocking(struct led_classdev *dev,
66 enum led_brightness brightness)
67{
68 struct spi_byte_led *led = container_of(dev, struct spi_byte_led, ldev);
69 u8 value;
70 int ret;
71
72 value = (u8) brightness + led->cdef->off_value;
73
74 mutex_lock(&led->mutex);
75 ret = spi_write(led->spi, &value, sizeof(value));
76 mutex_unlock(&led->mutex);
77
78 return ret;
79}
80
81static int spi_byte_probe(struct spi_device *spi)
82{
83 const struct of_device_id *of_dev_id;
84 struct device_node *child;
85 struct device *dev = &spi->dev;
86 struct spi_byte_led *led;
87 const char *name = "leds-spi-byte::";
88 const char *state;
89 int ret;
90
91 of_dev_id = of_match_device(spi_byte_dt_ids, dev);
92 if (!of_dev_id)
93 return -EINVAL;
94
95 if (of_get_child_count(dev->of_node) != 1) {
96 dev_err(dev, "Device must have exactly one LED sub-node.");
97 return -EINVAL;
98 }
99 child = of_get_next_child(dev->of_node, NULL);
100
101 led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
102 if (!led)
103 return -ENOMEM;
104
105 of_property_read_string(child, "label", &name);
106 strlcpy(led->name, name, sizeof(led->name));
107 led->spi = spi;
108 mutex_init(&led->mutex);
109 led->cdef = of_dev_id->data;
110 led->ldev.name = led->name;
111 led->ldev.brightness = LED_OFF;
112 led->ldev.max_brightness = led->cdef->max_value - led->cdef->off_value;
113 led->ldev.brightness_set_blocking = spi_byte_brightness_set_blocking;
114
115 state = of_get_property(child, "default-state", NULL);
116 if (state) {
117 if (!strcmp(state, "on")) {
118 led->ldev.brightness = led->ldev.max_brightness;
119 } else if (strcmp(state, "off")) {
120
121 dev_err(dev, "default-state can only be 'on' or 'off'");
122 return -EINVAL;
123 }
124 }
125 spi_byte_brightness_set_blocking(&led->ldev,
126 led->ldev.brightness);
127
128 ret = devm_led_classdev_register(&spi->dev, &led->ldev);
129 if (ret) {
130 mutex_destroy(&led->mutex);
131 return ret;
132 }
133 spi_set_drvdata(spi, led);
134
135 return 0;
136}
137
138static int spi_byte_remove(struct spi_device *spi)
139{
140 struct spi_byte_led *led = spi_get_drvdata(spi);
141
142 mutex_destroy(&led->mutex);
143
144 return 0;
145}
146
147static struct spi_driver spi_byte_driver = {
148 .probe = spi_byte_probe,
149 .remove = spi_byte_remove,
150 .driver = {
151 .name = KBUILD_MODNAME,
152 .of_match_table = spi_byte_dt_ids,
153 },
154};
155
156module_spi_driver(spi_byte_driver);
157
158MODULE_AUTHOR("Christian Mauderer <oss@c-mauderer.de>");
159MODULE_DESCRIPTION("single byte SPI LED driver");
160MODULE_LICENSE("GPL v2");
161MODULE_ALIAS("spi:leds-spi-byte");
162