drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / drivers / leds / leds-el15203000.c
blobd40194a3029f570f4f6122c5500f36a9384f1179
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:
14 * +-----+---------+
15 * | LED | COMMAND |
16 * +-----+---------+
17 * | 1 | 1 |
18 * +-----+---------+
19 * (*) LEDs MCU board expects 20 msec delay per byte.
21 * LEDs:
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 * +----------+--------------+-------------------------------------------+
32 * COMMAND:
33 * +----------+-----------------+--------------+--------------+
34 * | VALUES | PIPE | SCREEN FRAME | VENDING AREA |
35 * +----------+-----------------+--------------+--------------+
36 * | '0' 0x30 | Off |
37 * +----------+-----------------------------------------------+
38 * | '1' 0x31 | On |
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 {
57 /* for all LEDs */
58 EL_OFF = '0',
59 EL_ON = '1',
61 /* for Screen LED */
62 EL_SCREEN_BREATHING = '2',
64 /* for Pipe LED */
65 EL_PIPE_CASCADE = '2',
66 EL_PIPE_INV_CASCADE = '3',
67 EL_PIPE_BOUNCE = '4',
68 EL_PIPE_INV_BOUNCE = '5',
71 struct el15203000_led {
72 struct led_classdev ldev;
73 struct el15203000 *priv;
74 u32 reg;
77 struct el15203000 {
78 struct device *dev;
79 struct mutex lock;
80 struct spi_device *spi;
81 unsigned long delay;
82 size_t count;
83 struct el15203000_led leds[] __counted_by(count);
86 #define to_el15203000_led(d) container_of(d, struct el15203000_led, ldev)
88 static int el15203000_cmd(struct el15203000_led *led, u8 brightness)
90 int ret;
91 u8 cmd[2];
92 size_t i;
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",
102 EL_FW_DELAY_USEC);
104 usleep_range(EL_FW_DELAY_USEC,
105 EL_FW_DELAY_USEC + 1);
108 cmd[0] = led->reg;
109 cmd[1] = brightness;
111 for (i = 0; i < ARRAY_SIZE(cmd); i++) {
112 if (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]));
117 if (ret) {
118 dev_err(led->priv->dev,
119 "spi_write() error %d", ret);
120 break;
124 led->priv->delay = jiffies + usecs_to_jiffies(EL_FW_DELAY_USEC);
126 mutex_unlock(&led->priv->lock);
128 return ret;
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,
141 u32 len, int repeat)
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)
148 return -EINVAL;
150 dev_dbg(led->priv->dev, "Breathing mode for 0x%02x(%c)",
151 led->reg, led->reg);
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)
159 int val, t;
160 u32 i;
162 if (len != EL_PATTERN_HALF_LEN)
163 return false;
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)
172 return false;
174 val = right ? val >> 1 : val << 1;
177 return true;
180 static bool is_bounce(const struct led_pattern *pattern, u32 len, bool inv)
182 if (len != EL_PATTERN_LEN)
183 return false;
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,
192 u32 len, int repeat)
194 struct el15203000_led *led = to_el15203000_led(ldev);
195 u8 cmd;
197 if (repeat > 0)
198 return -EINVAL;
200 if (is_cascade(pattern, len, false, false)) {
201 dev_dbg(led->priv->dev, "Cascade mode for 0x%02x(%c)",
202 led->reg, led->reg);
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)",
207 led->reg, led->reg);
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)",
212 led->reg, led->reg);
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)",
217 led->reg, led->reg);
219 cmd = EL_PIPE_INV_BOUNCE;
220 } else {
221 dev_err(led->priv->dev, "Invalid hw_pattern for 0x%02x(%c)!",
222 led->reg, led->reg);
224 return -EINVAL;
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;
241 int ret;
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);
247 if (ret) {
248 dev_err(priv->dev, "LED without ID number");
249 goto err_child_out;
252 if (led->reg > U8_MAX) {
253 dev_err(priv->dev, "LED value %d is invalid", led->reg);
254 ret = -EINVAL;
255 goto err_child_out;
258 led->priv = priv;
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,
272 &init_data);
273 if (ret) {
274 dev_err(priv->dev,
275 "failed to register LED device %s, err %d",
276 led->ldev.name, ret);
277 goto err_child_out;
280 led++;
283 return 0;
285 err_child_out:
286 fwnode_handle_put(child);
287 return ret;
290 static int el15203000_probe(struct spi_device *spi)
292 struct el15203000 *priv;
293 size_t count;
295 count = device_get_child_node_count(&spi->dev);
296 if (!count) {
297 dev_err(&spi->dev, "LEDs are not defined in device tree!");
298 return -ENODEV;
301 priv = devm_kzalloc(&spi->dev, struct_size(priv, leds, count),
302 GFP_KERNEL);
303 if (!priv)
304 return -ENOMEM;
306 mutex_init(&priv->lock);
307 priv->count = count;
308 priv->dev = &spi->dev;
309 priv->spi = spi;
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,
335 .driver = {
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");