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>
4 
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>
10 
11 /*
12  * EL15203000 SPI protocol description:
13  * +-----+---------+
14  * | LED | COMMAND |
15  * +-----+---------+
16  * |  1  |    1    |
17  * +-----+---------+
18  * (*) LEDs MCU board expects 20 msec delay per byte.
19  *
20  * LEDs:
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  * +----------+--------------+-------------------------------------------+
30  *
31  * COMMAND:
32  * +----------+-----------------+--------------+--------------+
33  * |  VALUES  |       PIPE      | SCREEN FRAME | VENDING AREA |
34  * +----------+-----------------+--------------+--------------+
35  * | '0' 0x30 |                      Off                      |
36  * +----------+-----------------------------------------------+
37  * | '1' 0x31 |                      On                       |
38  * +----------+-----------------+--------------+--------------+
39  * | '2' 0x32 |     Cascade     |   Breathing  |
40  * +----------+-----------------+--------------+
41  * | '3' 0x33 | Inverse cascade |
42  * +----------+-----------------+
43  * | '4' 0x34 |     Bounce      |
44  * +----------+-----------------+
45  * | '5' 0x35 | Inverse bounce  |
46  * +----------+-----------------+
47  */
48 
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)
54 
55 enum el15203000_command {
56 	/* for all LEDs */
57 	EL_OFF			= '0',
58 	EL_ON			= '1',
59 
60 	/* for Screen LED */
61 	EL_SCREEN_BREATHING	= '2',
62 
63 	/* for Pipe LED */
64 	EL_PIPE_CASCADE		= '2',
65 	EL_PIPE_INV_CASCADE	= '3',
66 	EL_PIPE_BOUNCE		= '4',
67 	EL_PIPE_INV_BOUNCE	= '5',
68 };
69 
70 struct el15203000_led {
71 	struct led_classdev	ldev;
72 	struct el15203000	*priv;
73 	u32			reg;
74 };
75 
76 struct el15203000 {
77 	struct device		*dev;
78 	struct mutex		lock;
79 	struct spi_device	*spi;
80 	unsigned long		delay;
81 	size_t			count;
82 	struct el15203000_led	leds[];
83 };
84 
85 #define to_el15203000_led(d)	container_of(d, struct el15203000_led, ldev)
86 
87 static int el15203000_cmd(struct el15203000_led *led, u8 brightness)
88 {
89 	int		ret;
90 	u8		cmd[2];
91 	size_t		i;
92 
93 	mutex_lock(&led->priv->lock);
94 
95 	dev_dbg(led->priv->dev, "Set brightness of 0x%02x(%c) to 0x%02x(%c)",
96 		led->reg, led->reg, brightness, brightness);
97 
98 	/* to avoid SPI mistiming with firmware we should wait some time */
99 	if (time_after(led->priv->delay, jiffies)) {
100 		dev_dbg(led->priv->dev, "Wait %luus to sync",
101 			EL_FW_DELAY_USEC);
102 
103 		usleep_range(EL_FW_DELAY_USEC,
104 			     EL_FW_DELAY_USEC + 1);
105 	}
106 
107 	cmd[0] = led->reg;
108 	cmd[1] = brightness;
109 
110 	for (i = 0; i < ARRAY_SIZE(cmd); i++) {
111 		if (i)
112 			usleep_range(EL_FW_DELAY_USEC,
113 				     EL_FW_DELAY_USEC + 1);
114 
115 		ret = spi_write(led->priv->spi, &cmd[i], sizeof(cmd[i]));
116 		if (ret) {
117 			dev_err(led->priv->dev,
118 				"spi_write() error %d", ret);
119 			break;
120 		}
121 	}
122 
123 	led->priv->delay = jiffies + usecs_to_jiffies(EL_FW_DELAY_USEC);
124 
125 	mutex_unlock(&led->priv->lock);
126 
127 	return ret;
128 }
129 
130 static int el15203000_set_blocking(struct led_classdev *ldev,
131 				   enum led_brightness brightness)
132 {
133 	struct el15203000_led *led = to_el15203000_led(ldev);
134 
135 	return el15203000_cmd(led, brightness == LED_OFF ? EL_OFF : EL_ON);
136 }
137 
138 static int el15203000_pattern_set_S(struct led_classdev *ldev,
139 				    struct led_pattern *pattern,
140 				    u32 len, int repeat)
141 {
142 	struct el15203000_led *led = to_el15203000_led(ldev);
143 
144 	if (repeat > 0 || len != 2 ||
145 	    pattern[0].delta_t != 4000 || pattern[0].brightness != 0 ||
146 	    pattern[1].delta_t != 4000 || pattern[1].brightness != 1)
147 		return -EINVAL;
148 
149 	dev_dbg(led->priv->dev, "Breathing mode for 0x%02x(%c)",
150 		led->reg, led->reg);
151 
152 	return el15203000_cmd(led, EL_SCREEN_BREATHING);
153 }
154 
155 static bool is_cascade(const struct led_pattern *pattern, u32 len,
156 		       bool inv, bool right)
157 {
158 	int val, t;
159 	u32 i;
160 
161 	if (len != EL_PATTERN_HALF_LEN)
162 		return false;
163 
164 	val = right ? BIT(4) : BIT(0);
165 
166 	for (i = 0; i < len; i++) {
167 		t = inv ? ~val & GENMASK(4, 0) : val;
168 
169 		if (pattern[i].delta_t != EL_PATTERN_DELAY_MSEC ||
170 		    pattern[i].brightness != t)
171 			return false;
172 
173 		val = right ? val >> 1 : val << 1;
174 	}
175 
176 	return true;
177 }
178 
179 static bool is_bounce(const struct led_pattern *pattern, u32 len, bool inv)
180 {
181 	if (len != EL_PATTERN_LEN)
182 		return false;
183 
184 	return is_cascade(pattern, EL_PATTERN_HALF_LEN, inv, false) &&
185 	       is_cascade(pattern + EL_PATTERN_HALF_LEN,
186 			  EL_PATTERN_HALF_LEN, inv, true);
187 }
188 
189 static int el15203000_pattern_set_P(struct led_classdev *ldev,
190 				    struct led_pattern *pattern,
191 				    u32 len, int repeat)
192 {
193 	struct el15203000_led	*led = to_el15203000_led(ldev);
194 	u8			cmd;
195 
196 	if (repeat > 0)
197 		return -EINVAL;
198 
199 	if (is_cascade(pattern, len, false, false)) {
200 		dev_dbg(led->priv->dev, "Cascade mode for 0x%02x(%c)",
201 			led->reg, led->reg);
202 
203 		cmd = EL_PIPE_CASCADE;
204 	} else if (is_cascade(pattern, len, true, false)) {
205 		dev_dbg(led->priv->dev, "Inverse cascade mode for 0x%02x(%c)",
206 			led->reg, led->reg);
207 
208 		cmd = EL_PIPE_INV_CASCADE;
209 	} else if (is_bounce(pattern, len, false)) {
210 		dev_dbg(led->priv->dev, "Bounce mode for 0x%02x(%c)",
211 			led->reg, led->reg);
212 
213 		cmd = EL_PIPE_BOUNCE;
214 	} else if (is_bounce(pattern, len, true)) {
215 		dev_dbg(led->priv->dev, "Inverse bounce mode for 0x%02x(%c)",
216 			led->reg, led->reg);
217 
218 		cmd = EL_PIPE_INV_BOUNCE;
219 	} else {
220 		dev_err(led->priv->dev, "Invalid hw_pattern for 0x%02x(%c)!",
221 			led->reg, led->reg);
222 
223 		return -EINVAL;
224 	}
225 
226 	return el15203000_cmd(led, cmd);
227 }
228 
229 static int el15203000_pattern_clear(struct led_classdev *ldev)
230 {
231 	struct el15203000_led *led = to_el15203000_led(ldev);
232 
233 	return el15203000_cmd(led, EL_OFF);
234 }
235 
236 static int el15203000_probe_dt(struct el15203000 *priv)
237 {
238 	struct el15203000_led	*led = priv->leds;
239 	struct fwnode_handle	*child;
240 	int			ret;
241 
242 	device_for_each_child_node(priv->dev, child) {
243 		struct led_init_data init_data = {};
244 
245 		ret = fwnode_property_read_u32(child, "reg", &led->reg);
246 		if (ret) {
247 			dev_err(priv->dev, "LED without ID number");
248 			goto err_child_out;
249 		}
250 
251 		if (led->reg > U8_MAX) {
252 			dev_err(priv->dev, "LED value %d is invalid", led->reg);
253 			ret = -EINVAL;
254 			goto err_child_out;
255 		}
256 
257 		led->priv			  = priv;
258 		led->ldev.max_brightness	  = LED_ON;
259 		led->ldev.brightness_set_blocking = el15203000_set_blocking;
260 
261 		if (led->reg == 'S') {
262 			led->ldev.pattern_set	= el15203000_pattern_set_S;
263 			led->ldev.pattern_clear	= el15203000_pattern_clear;
264 		} else if (led->reg == 'P') {
265 			led->ldev.pattern_set	= el15203000_pattern_set_P;
266 			led->ldev.pattern_clear	= el15203000_pattern_clear;
267 		}
268 
269 		init_data.fwnode = child;
270 		ret = devm_led_classdev_register_ext(priv->dev, &led->ldev,
271 						     &init_data);
272 		if (ret) {
273 			dev_err(priv->dev,
274 				"failed to register LED device %s, err %d",
275 				led->ldev.name, ret);
276 			goto err_child_out;
277 		}
278 
279 		led++;
280 	}
281 
282 	return 0;
283 
284 err_child_out:
285 	fwnode_handle_put(child);
286 	return ret;
287 }
288 
289 static int el15203000_probe(struct spi_device *spi)
290 {
291 	struct el15203000	*priv;
292 	size_t			count;
293 
294 	count = device_get_child_node_count(&spi->dev);
295 	if (!count) {
296 		dev_err(&spi->dev, "LEDs are not defined in device tree!");
297 		return -ENODEV;
298 	}
299 
300 	priv = devm_kzalloc(&spi->dev, struct_size(priv, leds, count),
301 			    GFP_KERNEL);
302 	if (!priv)
303 		return -ENOMEM;
304 
305 	mutex_init(&priv->lock);
306 	priv->count	= count;
307 	priv->dev	= &spi->dev;
308 	priv->spi	= spi;
309 	priv->delay	= jiffies -
310 			  usecs_to_jiffies(EL_FW_DELAY_USEC);
311 
312 	spi_set_drvdata(spi, priv);
313 
314 	return el15203000_probe_dt(priv);
315 }
316 
317 static int el15203000_remove(struct spi_device *spi)
318 {
319 	struct el15203000 *priv = spi_get_drvdata(spi);
320 
321 	mutex_destroy(&priv->lock);
322 
323 	return 0;
324 }
325 
326 static const struct of_device_id el15203000_dt_ids[] = {
327 	{ .compatible = "crane,el15203000", },
328 	{},
329 };
330 
331 MODULE_DEVICE_TABLE(of, el15203000_dt_ids);
332 
333 static struct spi_driver el15203000_driver = {
334 	.probe		= el15203000_probe,
335 	.remove		= el15203000_remove,
336 	.driver = {
337 		.name		= KBUILD_MODNAME,
338 		.of_match_table	= el15203000_dt_ids,
339 	},
340 };
341 
342 module_spi_driver(el15203000_driver);
343 
344 MODULE_AUTHOR("Oleh Kravchenko <oleg@kaa.org.ua>");
345 MODULE_DESCRIPTION("el15203000 LED driver");
346 MODULE_LICENSE("GPL v2");
347 MODULE_ALIAS("spi:el15203000");
348