1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 Crane Merchandising Systems. All rights reserved.
3 // Copyright (C) 2019 Oleh Kravchenko <oleg@kaa.org.ua>
5 #include <linux/delay.h>
6 #include <linux/leds.h>
7 #include <linux/mod_devicetable.h>
8 #include <linux/module.h>
9 #include <linux/property.h>
10 #include <linux/spi/spi.h>
13 * EL15203000 SPI protocol description:
19 * (*) LEDs MCU board expects 20 msec delay per byte.
22 * +----------+--------------+-------------------------------------------+
23 * | ID | NAME | DESCRIPTION |
24 * +----------+--------------+-------------------------------------------+
25 * | 'P' 0x50 | Pipe | Consists from 5 LEDs, controlled by board |
26 * +----------+--------------+-------------------------------------------+
27 * | 'S' 0x53 | Screen frame | Light tube around the screen |
28 * +----------+--------------+-------------------------------------------+
29 * | 'V' 0x56 | Vending area | Highlights a cup of coffee |
30 * +----------+--------------+-------------------------------------------+
33 * +----------+-----------------+--------------+--------------+
34 * | VALUES | PIPE | SCREEN FRAME | VENDING AREA |
35 * +----------+-----------------+--------------+--------------+
37 * +----------+-----------------------------------------------+
39 * +----------+-----------------+--------------+--------------+
40 * | '2' 0x32 | Cascade | Breathing |
41 * +----------+-----------------+--------------+
42 * | '3' 0x33 | Inverse cascade |
43 * +----------+-----------------+
44 * | '4' 0x34 | Bounce |
45 * +----------+-----------------+
46 * | '5' 0x35 | Inverse bounce |
47 * +----------+-----------------+
50 /* EL15203000 default settings */
51 #define EL_FW_DELAY_USEC 20000ul
52 #define EL_PATTERN_DELAY_MSEC 800u
53 #define EL_PATTERN_LEN 10u
54 #define EL_PATTERN_HALF_LEN (EL_PATTERN_LEN / 2)
56 enum el15203000_command {
62 EL_SCREEN_BREATHING = '2',
65 EL_PIPE_CASCADE = '2',
66 EL_PIPE_INV_CASCADE = '3',
68 EL_PIPE_INV_BOUNCE = '5',
71 struct el15203000_led {
72 struct led_classdev ldev;
73 struct el15203000 *priv;
80 struct spi_device *spi;
83 struct el15203000_led leds[];
86 #define to_el15203000_led(d) container_of(d, struct el15203000_led, ldev)
88 static int el15203000_cmd(struct el15203000_led *led, u8 brightness)
94 mutex_lock(&led->priv->lock);
96 dev_dbg(led->priv->dev, "Set brightness of 0x%02x(%c) to 0x%02x(%c)",
97 led->reg, led->reg, brightness, brightness);
99 /* to avoid SPI mistiming with firmware we should wait some time */
100 if (time_after(led->priv->delay, jiffies)) {
101 dev_dbg(led->priv->dev, "Wait %luus to sync",
104 usleep_range(EL_FW_DELAY_USEC,
105 EL_FW_DELAY_USEC + 1);
111 for (i = 0; i < ARRAY_SIZE(cmd); i++) {
113 usleep_range(EL_FW_DELAY_USEC,
114 EL_FW_DELAY_USEC + 1);
116 ret = spi_write(led->priv->spi, &cmd[i], sizeof(cmd[i]));
118 dev_err(led->priv->dev,
119 "spi_write() error %d", ret);
124 led->priv->delay = jiffies + usecs_to_jiffies(EL_FW_DELAY_USEC);
126 mutex_unlock(&led->priv->lock);
131 static int el15203000_set_blocking(struct led_classdev *ldev,
132 enum led_brightness brightness)
134 struct el15203000_led *led = to_el15203000_led(ldev);
136 return el15203000_cmd(led, brightness == LED_OFF ? EL_OFF : EL_ON);
139 static int el15203000_pattern_set_S(struct led_classdev *ldev,
140 struct led_pattern *pattern,
143 struct el15203000_led *led = to_el15203000_led(ldev);
145 if (repeat > 0 || len != 2 ||
146 pattern[0].delta_t != 4000 || pattern[0].brightness != 0 ||
147 pattern[1].delta_t != 4000 || pattern[1].brightness != 1)
150 dev_dbg(led->priv->dev, "Breathing mode for 0x%02x(%c)",
153 return el15203000_cmd(led, EL_SCREEN_BREATHING);
156 static bool is_cascade(const struct led_pattern *pattern, u32 len,
157 bool inv, bool right)
162 if (len != EL_PATTERN_HALF_LEN)
165 val = right ? BIT(4) : BIT(0);
167 for (i = 0; i < len; i++) {
168 t = inv ? ~val & GENMASK(4, 0) : val;
170 if (pattern[i].delta_t != EL_PATTERN_DELAY_MSEC ||
171 pattern[i].brightness != t)
174 val = right ? val >> 1 : val << 1;
180 static bool is_bounce(const struct led_pattern *pattern, u32 len, bool inv)
182 if (len != EL_PATTERN_LEN)
185 return is_cascade(pattern, EL_PATTERN_HALF_LEN, inv, false) &&
186 is_cascade(pattern + EL_PATTERN_HALF_LEN,
187 EL_PATTERN_HALF_LEN, inv, true);
190 static int el15203000_pattern_set_P(struct led_classdev *ldev,
191 struct led_pattern *pattern,
194 struct el15203000_led *led = to_el15203000_led(ldev);
200 if (is_cascade(pattern, len, false, false)) {
201 dev_dbg(led->priv->dev, "Cascade mode for 0x%02x(%c)",
204 cmd = EL_PIPE_CASCADE;
205 } else if (is_cascade(pattern, len, true, false)) {
206 dev_dbg(led->priv->dev, "Inverse cascade mode for 0x%02x(%c)",
209 cmd = EL_PIPE_INV_CASCADE;
210 } else if (is_bounce(pattern, len, false)) {
211 dev_dbg(led->priv->dev, "Bounce mode for 0x%02x(%c)",
214 cmd = EL_PIPE_BOUNCE;
215 } else if (is_bounce(pattern, len, true)) {
216 dev_dbg(led->priv->dev, "Inverse bounce mode for 0x%02x(%c)",
219 cmd = EL_PIPE_INV_BOUNCE;
221 dev_err(led->priv->dev, "Invalid hw_pattern for 0x%02x(%c)!",
227 return el15203000_cmd(led, cmd);
230 static int el15203000_pattern_clear(struct led_classdev *ldev)
232 struct el15203000_led *led = to_el15203000_led(ldev);
234 return el15203000_cmd(led, EL_OFF);
237 static int el15203000_probe_dt(struct el15203000 *priv)
239 struct el15203000_led *led = priv->leds;
240 struct fwnode_handle *child;
243 device_for_each_child_node(priv->dev, child) {
244 struct led_init_data init_data = {};
246 ret = fwnode_property_read_u32(child, "reg", &led->reg);
248 dev_err(priv->dev, "LED without ID number");
252 if (led->reg > U8_MAX) {
253 dev_err(priv->dev, "LED value %d is invalid", led->reg);
259 led->ldev.max_brightness = LED_ON;
260 led->ldev.brightness_set_blocking = el15203000_set_blocking;
262 if (led->reg == 'S') {
263 led->ldev.pattern_set = el15203000_pattern_set_S;
264 led->ldev.pattern_clear = el15203000_pattern_clear;
265 } else if (led->reg == 'P') {
266 led->ldev.pattern_set = el15203000_pattern_set_P;
267 led->ldev.pattern_clear = el15203000_pattern_clear;
270 init_data.fwnode = child;
271 ret = devm_led_classdev_register_ext(priv->dev, &led->ldev,
275 "failed to register LED device %s, err %d",
276 led->ldev.name, ret);
286 fwnode_handle_put(child);
290 static int el15203000_probe(struct spi_device *spi)
292 struct el15203000 *priv;
295 count = device_get_child_node_count(&spi->dev);
297 dev_err(&spi->dev, "LEDs are not defined in device tree!");
301 priv = devm_kzalloc(&spi->dev, struct_size(priv, leds, count),
306 mutex_init(&priv->lock);
308 priv->dev = &spi->dev;
310 priv->delay = jiffies -
311 usecs_to_jiffies(EL_FW_DELAY_USEC);
313 spi_set_drvdata(spi, priv);
315 return el15203000_probe_dt(priv);
318 static void el15203000_remove(struct spi_device *spi)
320 struct el15203000 *priv = spi_get_drvdata(spi);
322 mutex_destroy(&priv->lock);
325 static const struct of_device_id el15203000_dt_ids[] = {
326 { .compatible = "crane,el15203000", },
330 MODULE_DEVICE_TABLE(of, el15203000_dt_ids);
332 static struct spi_driver el15203000_driver = {
333 .probe = el15203000_probe,
334 .remove = el15203000_remove,
336 .name = KBUILD_MODNAME,
337 .of_match_table = el15203000_dt_ids,
341 module_spi_driver(el15203000_driver);
343 MODULE_AUTHOR("Oleh Kravchenko <oleg@kaa.org.ua>");
344 MODULE_DESCRIPTION("el15203000 LED driver");
345 MODULE_LICENSE("GPL v2");
346 MODULE_ALIAS("spi:el15203000");