7f6bac37dd15ecc0e7e9ef6253f2c5ac2bcd51ef
[openwrt/staging/xback.git] /
1 From aaf38273cf766d88296f4aa3f2e4e3c0d399a4a2 Mon Sep 17 00:00:00 2001
2 From: Marek BehĂșn <kabel@kernel.org>
3 Date: Mon, 18 Sep 2023 18:11:03 +0200
4 Subject: leds: turris-omnia: Support HW controlled mode via private trigger
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Add support for enabling MCU controlled mode of the Turris Omnia LEDs
10 via a LED private trigger called "omnia-mcu". Recall that private LED
11 triggers will only be listed in the sysfs trigger file for LEDs that
12 support them (currently there is no user of this mechanism).
13
14 When in MCU controlled mode, the user can still set LED color, but the
15 blinking is done by MCU, which does different things for different LEDs:
16 - WAN LED is blinked according to the LED[0] pin of the WAN PHY
17 - LAN LEDs are blinked according to the LED[0] output of the
18 corresponding port of the LAN switch
19 - PCIe LEDs are blinked according to the logical OR of the MiniPCIe port
20 LED pins
21
22 In the future I want to make the netdev trigger to transparently offload
23 the blinking to the HW if user sets compatible settings for the netdev
24 trigger (for LEDs associated with network devices).
25 There was some work on this already, and hopefully we will be able to
26 complete it sometime, but for now there are still multiple blockers for
27 this, and even if there weren't, we still would not be able to configure
28 HW controlled mode for the LEDs associated with MiniPCIe ports.
29
30 In the meantime let's support HW controlled mode via the private LED
31 trigger mechanism. If, in the future, we manage to complete the netdev
32 trigger offloading, we can still keep this private trigger for backwards
33 compatibility, if needed.
34
35 We also set "omnia-mcu" to cdev->default_trigger, so that the MCU keeps
36 control until the user first wants to take over it. If a different
37 default trigger is specified in device-tree via the
38 'linux,default-trigger' property, LED class will overwrite
39 cdev->default_trigger, and so the DT property will be respected.
40
41 Signed-off-by: Marek BehĂșn <kabel@kernel.org>
42 Link: https://lore.kernel.org/r/20230918161104.20860-4-kabel@kernel.org
43 Signed-off-by: Lee Jones <lee@kernel.org>
44 ---
45 drivers/leds/Kconfig | 1 +
46 drivers/leds/leds-turris-omnia.c | 98 ++++++++++++++++++++++++++++++++++++----
47 2 files changed, 91 insertions(+), 8 deletions(-)
48
49 --- a/drivers/leds/Kconfig
50 +++ b/drivers/leds/Kconfig
51 @@ -164,6 +164,7 @@ config LEDS_TURRIS_OMNIA
52 depends on I2C
53 depends on MACH_ARMADA_38X || COMPILE_TEST
54 depends on OF
55 + select LEDS_TRIGGERS
56 help
57 This option enables basic support for the LEDs found on the front
58 side of CZ.NIC's Turris Omnia router. There are 12 RGB LEDs on the
59 --- a/drivers/leds/leds-turris-omnia.c
60 +++ b/drivers/leds/leds-turris-omnia.c
61 @@ -31,7 +31,7 @@ struct omnia_led {
62 struct led_classdev_mc mc_cdev;
63 struct mc_subled subled_info[OMNIA_LED_NUM_CHANNELS];
64 u8 cached_channels[OMNIA_LED_NUM_CHANNELS];
65 - bool on;
66 + bool on, hwtrig;
67 int reg;
68 };
69
70 @@ -120,12 +120,14 @@ static int omnia_led_brightness_set_bloc
71
72 /*
73 * Only recalculate RGB brightnesses from intensities if brightness is
74 - * non-zero. Otherwise we won't be using them and we can save ourselves
75 - * some software divisions (Omnia's CPU does not implement the division
76 - * instruction).
77 + * non-zero (if it is zero and the LED is in HW blinking mode, we use
78 + * max_brightness as brightness). Otherwise we won't be using them and
79 + * we can save ourselves some software divisions (Omnia's CPU does not
80 + * implement the division instruction).
81 */
82 - if (brightness) {
83 - led_mc_calc_color_components(mc_cdev, brightness);
84 + if (brightness || led->hwtrig) {
85 + led_mc_calc_color_components(mc_cdev, brightness ?:
86 + cdev->max_brightness);
87
88 /*
89 * Send color command only if brightness is non-zero and the RGB
90 @@ -135,8 +137,11 @@ static int omnia_led_brightness_set_bloc
91 err = omnia_led_send_color_cmd(leds->client, led);
92 }
93
94 - /* Send on/off state change only if (bool)brightness changed */
95 - if (!err && !brightness != !led->on) {
96 + /*
97 + * Send on/off state change only if (bool)brightness changed and the LED
98 + * is not being blinked by HW.
99 + */
100 + if (!err && !led->hwtrig && !brightness != !led->on) {
101 u8 state = CMD_LED_STATE_LED(led->reg);
102
103 if (brightness)
104 @@ -152,6 +157,71 @@ static int omnia_led_brightness_set_bloc
105 return err;
106 }
107
108 +static struct led_hw_trigger_type omnia_hw_trigger_type;
109 +
110 +static int omnia_hwtrig_activate(struct led_classdev *cdev)
111 +{
112 + struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(cdev);
113 + struct omnia_leds *leds = dev_get_drvdata(cdev->dev->parent);
114 + struct omnia_led *led = to_omnia_led(mc_cdev);
115 + int err = 0;
116 +
117 + mutex_lock(&leds->lock);
118 +
119 + if (!led->on) {
120 + /*
121 + * If the LED is off (brightness was set to 0), the last
122 + * configured color was not necessarily sent to the MCU.
123 + * Recompute with max_brightness and send if needed.
124 + */
125 + led_mc_calc_color_components(mc_cdev, cdev->max_brightness);
126 +
127 + if (omnia_led_channels_changed(led))
128 + err = omnia_led_send_color_cmd(leds->client, led);
129 + }
130 +
131 + if (!err) {
132 + /* Put the LED into MCU controlled mode */
133 + err = omnia_cmd_write_u8(leds->client, CMD_LED_MODE,
134 + CMD_LED_MODE_LED(led->reg));
135 + if (!err)
136 + led->hwtrig = true;
137 + }
138 +
139 + mutex_unlock(&leds->lock);
140 +
141 + return err;
142 +}
143 +
144 +static void omnia_hwtrig_deactivate(struct led_classdev *cdev)
145 +{
146 + struct omnia_leds *leds = dev_get_drvdata(cdev->dev->parent);
147 + struct omnia_led *led = to_omnia_led(lcdev_to_mccdev(cdev));
148 + int err;
149 +
150 + mutex_lock(&leds->lock);
151 +
152 + led->hwtrig = false;
153 +
154 + /* Put the LED into software mode */
155 + err = omnia_cmd_write_u8(leds->client, CMD_LED_MODE,
156 + CMD_LED_MODE_LED(led->reg) |
157 + CMD_LED_MODE_USER);
158 +
159 + mutex_unlock(&leds->lock);
160 +
161 + if (err < 0)
162 + dev_err(cdev->dev, "Cannot put LED to software mode: %i\n",
163 + err);
164 +}
165 +
166 +static struct led_trigger omnia_hw_trigger = {
167 + .name = "omnia-mcu",
168 + .activate = omnia_hwtrig_activate,
169 + .deactivate = omnia_hwtrig_deactivate,
170 + .trigger_type = &omnia_hw_trigger_type,
171 +};
172 +
173 static int omnia_led_register(struct i2c_client *client, struct omnia_led *led,
174 struct device_node *np)
175 {
176 @@ -195,6 +265,12 @@ static int omnia_led_register(struct i2c
177 cdev = &led->mc_cdev.led_cdev;
178 cdev->max_brightness = 255;
179 cdev->brightness_set_blocking = omnia_led_brightness_set_blocking;
180 + cdev->trigger_type = &omnia_hw_trigger_type;
181 + /*
182 + * Use the omnia-mcu trigger as the default trigger. It may be rewritten
183 + * by LED class from the linux,default-trigger property.
184 + */
185 + cdev->default_trigger = omnia_hw_trigger.name;
186
187 /* put the LED into software mode */
188 ret = omnia_cmd_write_u8(client, CMD_LED_MODE,
189 @@ -309,6 +385,12 @@ static int omnia_leds_probe(struct i2c_c
190
191 mutex_init(&leds->lock);
192
193 + ret = devm_led_trigger_register(dev, &omnia_hw_trigger);
194 + if (ret < 0) {
195 + dev_err(dev, "Cannot register private LED trigger: %d\n", ret);
196 + return ret;
197 + }
198 +
199 led = &leds->leds[0];
200 for_each_available_child_of_node(np, child) {
201 ret = omnia_led_register(client, led, child);