xref: /linux/drivers/leds/leds-expresswire.c (revision 6d9b262afe0ec1d6e0ef99321ca9d6b921310471)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Shared library for Kinetic's ExpressWire protocol.
4  * This protocol works by pulsing the ExpressWire IC's control GPIO.
5  * ktd2692 and ktd2801 are known to use this protocol.
6  */
7 
8 #include <linux/bits.h>
9 #include <linux/delay.h>
10 #include <linux/export.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/types.h>
13 
14 #include <linux/leds-expresswire.h>
15 
16 void expresswire_power_off(struct expresswire_common_props *props)
17 {
18 	gpiod_set_value_cansleep(props->ctrl_gpio, 0);
19 	usleep_range(props->timing.poweroff_us, props->timing.poweroff_us * 2);
20 }
21 EXPORT_SYMBOL_NS_GPL(expresswire_power_off, EXPRESSWIRE);
22 
23 void expresswire_enable(struct expresswire_common_props *props)
24 {
25 	gpiod_set_value(props->ctrl_gpio, 1);
26 	udelay(props->timing.detect_delay_us);
27 	gpiod_set_value(props->ctrl_gpio, 0);
28 	udelay(props->timing.detect_us);
29 	gpiod_set_value(props->ctrl_gpio, 1);
30 }
31 EXPORT_SYMBOL_NS_GPL(expresswire_enable, EXPRESSWIRE);
32 
33 void expresswire_start(struct expresswire_common_props *props)
34 {
35 	gpiod_set_value(props->ctrl_gpio, 1);
36 	udelay(props->timing.data_start_us);
37 }
38 EXPORT_SYMBOL_NS_GPL(expresswire_start, EXPRESSWIRE);
39 
40 void expresswire_end(struct expresswire_common_props *props)
41 {
42 	gpiod_set_value(props->ctrl_gpio, 0);
43 	udelay(props->timing.end_of_data_low_us);
44 	gpiod_set_value(props->ctrl_gpio, 1);
45 	udelay(props->timing.end_of_data_high_us);
46 }
47 EXPORT_SYMBOL_NS_GPL(expresswire_end, EXPRESSWIRE);
48 
49 void expresswire_set_bit(struct expresswire_common_props *props, bool bit)
50 {
51 	if (bit) {
52 		gpiod_set_value(props->ctrl_gpio, 0);
53 		udelay(props->timing.short_bitset_us);
54 		gpiod_set_value(props->ctrl_gpio, 1);
55 		udelay(props->timing.long_bitset_us);
56 	} else {
57 		gpiod_set_value(props->ctrl_gpio, 0);
58 		udelay(props->timing.long_bitset_us);
59 		gpiod_set_value(props->ctrl_gpio, 1);
60 		udelay(props->timing.short_bitset_us);
61 	}
62 }
63 EXPORT_SYMBOL_NS_GPL(expresswire_set_bit, EXPRESSWIRE);
64 
65 void expresswire_write_u8(struct expresswire_common_props *props, u8 val)
66 {
67 	expresswire_start(props);
68 	for (int i = 7; i >= 0; i--)
69 		expresswire_set_bit(props, val & BIT(i));
70 	expresswire_end(props);
71 }
72 EXPORT_SYMBOL_NS_GPL(expresswire_write_u8, EXPRESSWIRE);
73