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/module.h>
8 #include <linux/of_device.h>
9 #include <linux/spi/spi.h>
12 * EL15203000 SPI protocol description:
18 * (*) LEDs MCU board expects 20 msec delay per byte.
21 * +----------+--------------+-------------------------------------------+
22 * | ID | NAME | DESCRIPTION |
23 * +----------+--------------+-------------------------------------------+
24 * | 'P' 0x50 | Pipe | Consists from 5 LEDs, controlled by board |
25 * +----------+--------------+-------------------------------------------+
26 * | 'S' 0x53 | Screen frame | Light tube around the screen |
27 * +----------+--------------+-------------------------------------------+
28 * | 'V' 0x56 | Vending area | Highlights a cup of coffee |
29 * +----------+--------------+-------------------------------------------+
32 * +----------+-----------------+--------------+--------------+
33 * | VALUES | PIPE | SCREEN FRAME | VENDING AREA |
34 * +----------+-----------------+--------------+--------------+
36 * +----------+-----------------------------------------------+
38 * +----------+-----------------+--------------+--------------+
39 * | '2' 0x32 | Cascade | Breathing |
40 * +----------+-----------------+--------------+
41 * | '3' 0x33 | Inverse cascade |
42 * +----------+-----------------+
43 * | '4' 0x34 | Bounce |
44 * +----------+-----------------+
45 * | '5' 0x35 | Inverse bounce |
46 * +----------+-----------------+
49 /* EL15203000 default settings */
50 #define EL_FW_DELAY_USEC 20000ul
51 #define EL_PATTERN_DELAY_MSEC 800u
52 #define EL_PATTERN_LEN 10u
53 #define EL_PATTERN_HALF_LEN (EL_PATTERN_LEN / 2)
55 enum el15203000_command
{
61 EL_SCREEN_BREATHING
= '2',
64 EL_PIPE_CASCADE
= '2',
65 EL_PIPE_INV_CASCADE
= '3',
67 EL_PIPE_INV_BOUNCE
= '5',
70 struct el15203000_led
{
71 struct el15203000
*priv
;
72 struct led_classdev ldev
;
79 struct spi_device
*spi
;
82 struct el15203000_led leds
[];
85 static int el15203000_cmd(struct el15203000_led
*led
, u8 brightness
)
91 mutex_lock(&led
->priv
->lock
);
93 dev_dbg(led
->priv
->dev
, "Set brightness of 0x%02x(%c) to 0x%02x(%c)",
94 led
->reg
, led
->reg
, brightness
, brightness
);
96 /* to avoid SPI mistiming with firmware we should wait some time */
97 if (time_after(led
->priv
->delay
, jiffies
)) {
98 dev_dbg(led
->priv
->dev
, "Wait %luus to sync",
101 usleep_range(EL_FW_DELAY_USEC
,
102 EL_FW_DELAY_USEC
+ 1);
108 for (i
= 0; i
< ARRAY_SIZE(cmd
); i
++) {
110 usleep_range(EL_FW_DELAY_USEC
,
111 EL_FW_DELAY_USEC
+ 1);
113 ret
= spi_write(led
->priv
->spi
, &cmd
[i
], sizeof(cmd
[i
]));
115 dev_err(led
->priv
->dev
,
116 "spi_write() error %d", ret
);
121 led
->priv
->delay
= jiffies
+ usecs_to_jiffies(EL_FW_DELAY_USEC
);
123 mutex_unlock(&led
->priv
->lock
);
128 static int el15203000_set_blocking(struct led_classdev
*ldev
,
129 enum led_brightness brightness
)
131 struct el15203000_led
*led
= container_of(ldev
,
132 struct el15203000_led
,
135 return el15203000_cmd(led
, brightness
== LED_OFF
? EL_OFF
: EL_ON
);
138 static int el15203000_pattern_set_S(struct led_classdev
*ldev
,
139 struct led_pattern
*pattern
,
142 struct el15203000_led
*led
= container_of(ldev
,
143 struct el15203000_led
,
146 if (repeat
> 0 || len
!= 2 ||
147 pattern
[0].delta_t
!= 4000 || pattern
[0].brightness
!= 0 ||
148 pattern
[1].delta_t
!= 4000 || pattern
[1].brightness
!= 1)
151 dev_dbg(led
->priv
->dev
, "Breathing mode for 0x%02x(%c)",
154 return el15203000_cmd(led
, EL_SCREEN_BREATHING
);
157 static bool is_cascade(const struct led_pattern
*pattern
, u32 len
,
158 bool inv
, bool right
)
163 if (len
!= EL_PATTERN_HALF_LEN
)
166 val
= right
? BIT(4) : BIT(0);
168 for (i
= 0; i
< len
; i
++) {
169 t
= inv
? ~val
& GENMASK(4, 0) : val
;
171 if (pattern
[i
].delta_t
!= EL_PATTERN_DELAY_MSEC
||
172 pattern
[i
].brightness
!= t
)
175 val
= right
? val
>> 1 : val
<< 1;
181 static bool is_bounce(const struct led_pattern
*pattern
, u32 len
, bool inv
)
183 if (len
!= EL_PATTERN_LEN
)
186 return is_cascade(pattern
, EL_PATTERN_HALF_LEN
, inv
, false) &&
187 is_cascade(pattern
+ EL_PATTERN_HALF_LEN
,
188 EL_PATTERN_HALF_LEN
, inv
, true);
191 static int el15203000_pattern_set_P(struct led_classdev
*ldev
,
192 struct led_pattern
*pattern
,
196 struct el15203000_led
*led
= container_of(ldev
,
197 struct el15203000_led
,
203 if (is_cascade(pattern
, len
, false, false)) {
204 dev_dbg(led
->priv
->dev
, "Cascade mode for 0x%02x(%c)",
207 cmd
= EL_PIPE_CASCADE
;
208 } else if (is_cascade(pattern
, len
, true, false)) {
209 dev_dbg(led
->priv
->dev
, "Inverse cascade mode for 0x%02x(%c)",
212 cmd
= EL_PIPE_INV_CASCADE
;
213 } else if (is_bounce(pattern
, len
, false)) {
214 dev_dbg(led
->priv
->dev
, "Bounce mode for 0x%02x(%c)",
217 cmd
= EL_PIPE_BOUNCE
;
218 } else if (is_bounce(pattern
, len
, true)) {
219 dev_dbg(led
->priv
->dev
, "Inverse bounce mode for 0x%02x(%c)",
222 cmd
= EL_PIPE_INV_BOUNCE
;
224 dev_err(led
->priv
->dev
, "Invalid hw_pattern for 0x%02x(%c)!",
230 return el15203000_cmd(led
, cmd
);
233 static int el15203000_pattern_clear(struct led_classdev
*ldev
)
235 struct el15203000_led
*led
= container_of(ldev
,
236 struct el15203000_led
,
239 return el15203000_cmd(led
, EL_OFF
);
242 static int el15203000_probe_dt(struct el15203000
*priv
)
244 struct el15203000_led
*led
= priv
->leds
;
245 struct fwnode_handle
*child
;
248 device_for_each_child_node(priv
->dev
, child
) {
249 struct led_init_data init_data
= {};
251 ret
= fwnode_property_read_u32(child
, "reg", &led
->reg
);
253 dev_err(priv
->dev
, "LED without ID number");
254 fwnode_handle_put(child
);
259 if (led
->reg
> U8_MAX
) {
260 dev_err(priv
->dev
, "LED value %d is invalid", led
->reg
);
261 fwnode_handle_put(child
);
266 fwnode_property_read_string(child
, "linux,default-trigger",
267 &led
->ldev
.default_trigger
);
270 led
->ldev
.max_brightness
= LED_ON
;
271 led
->ldev
.brightness_set_blocking
= el15203000_set_blocking
;
273 if (led
->reg
== 'S') {
274 led
->ldev
.pattern_set
= el15203000_pattern_set_S
;
275 led
->ldev
.pattern_clear
= el15203000_pattern_clear
;
276 } else if (led
->reg
== 'P') {
277 led
->ldev
.pattern_set
= el15203000_pattern_set_P
;
278 led
->ldev
.pattern_clear
= el15203000_pattern_clear
;
281 init_data
.fwnode
= child
;
282 ret
= devm_led_classdev_register_ext(priv
->dev
, &led
->ldev
,
286 "failed to register LED device %s, err %d",
287 led
->ldev
.name
, ret
);
288 fwnode_handle_put(child
);
299 static int el15203000_probe(struct spi_device
*spi
)
301 struct el15203000
*priv
;
304 count
= device_get_child_node_count(&spi
->dev
);
306 dev_err(&spi
->dev
, "LEDs are not defined in device tree!");
310 priv
= devm_kzalloc(&spi
->dev
, struct_size(priv
, leds
, count
),
315 mutex_init(&priv
->lock
);
317 priv
->dev
= &spi
->dev
;
319 priv
->delay
= jiffies
-
320 usecs_to_jiffies(EL_FW_DELAY_USEC
);
322 spi_set_drvdata(spi
, priv
);
324 return el15203000_probe_dt(priv
);
327 static int el15203000_remove(struct spi_device
*spi
)
329 struct el15203000
*priv
= spi_get_drvdata(spi
);
331 mutex_destroy(&priv
->lock
);
336 static const struct of_device_id el15203000_dt_ids
[] = {
337 { .compatible
= "crane,el15203000", },
341 MODULE_DEVICE_TABLE(of
, el15203000_dt_ids
);
343 static struct spi_driver el15203000_driver
= {
344 .probe
= el15203000_probe
,
345 .remove
= el15203000_remove
,
347 .name
= KBUILD_MODNAME
,
348 .of_match_table
= el15203000_dt_ids
,
352 module_spi_driver(el15203000_driver
);
354 MODULE_AUTHOR("Oleh Kravchenko <oleg@kaa.org.ua>");
355 MODULE_DESCRIPTION("el15203000 LED driver");
356 MODULE_LICENSE("GPL v2");
357 MODULE_ALIAS("spi:el15203000");