kernel: bump 6.1 to 6.1.72
[openwrt/openwrt.git] / target / linux / generic / backport-6.1 / 815-v6.7-4-leds-turris-omnia-Add-support-for-enabling-disabling.patch
1 From 0efb3f9609d3de5a7d8c31e3835d7eb3e6adce79 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Marek=20Beh=C3=BAn?= <kabel@kernel.org>
3 Date: Mon, 18 Sep 2023 18:11:04 +0200
4 Subject: [PATCH 6/6] leds: turris-omnia: Add support for enabling/disabling HW
5 gamma correction
6 MIME-Version: 1.0
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
9
10 If the MCU on Turris Omnia is running newer firmware versions, the LED
11 controller supports RGB gamma correction (and enables it by default for
12 newer boards).
13
14 Determine whether the gamma correction setting feature is supported and
15 add the ability to set it via sysfs attribute file.
16
17 Signed-off-by: Marek BehĂșn <kabel@kernel.org>
18 Link: https://lore.kernel.org/r/20230918161104.20860-5-kabel@kernel.org
19 Signed-off-by: Lee Jones <lee@kernel.org>
20 ---
21 .../sysfs-class-led-driver-turris-omnia | 14 ++
22 drivers/leds/leds-turris-omnia.c | 137 +++++++++++++++---
23 2 files changed, 134 insertions(+), 17 deletions(-)
24
25 --- a/Documentation/ABI/testing/sysfs-class-led-driver-turris-omnia
26 +++ b/Documentation/ABI/testing/sysfs-class-led-driver-turris-omnia
27 @@ -12,3 +12,17 @@ Description: (RW) On the front panel of
28 able to change this setting from software.
29
30 Format: %i
31 +
32 +What: /sys/class/leds/<led>/device/gamma_correction
33 +Date: August 2023
34 +KernelVersion: 6.6
35 +Contact: Marek BehĂșn <kabel@kernel.org>
36 +Description: (RW) Newer versions of the microcontroller firmware of the
37 + Turris Omnia router support gamma correction for the RGB LEDs.
38 + This feature can be enabled/disabled by writing to this file.
39 +
40 + If the feature is not supported because the MCU firmware is too
41 + old, the file always reads as 0, and writing to the file results
42 + in the EOPNOTSUPP error.
43 +
44 + Format: %i
45 --- a/drivers/leds/leds-turris-omnia.c
46 +++ b/drivers/leds/leds-turris-omnia.c
47 @@ -15,17 +15,30 @@
48 #define OMNIA_BOARD_LEDS 12
49 #define OMNIA_LED_NUM_CHANNELS 3
50
51 -#define CMD_LED_MODE 3
52 -#define CMD_LED_MODE_LED(l) ((l) & 0x0f)
53 -#define CMD_LED_MODE_USER 0x10
54 -
55 -#define CMD_LED_STATE 4
56 -#define CMD_LED_STATE_LED(l) ((l) & 0x0f)
57 -#define CMD_LED_STATE_ON 0x10
58 -
59 -#define CMD_LED_COLOR 5
60 -#define CMD_LED_SET_BRIGHTNESS 7
61 -#define CMD_LED_GET_BRIGHTNESS 8
62 +/* MCU controller commands at I2C address 0x2a */
63 +#define OMNIA_MCU_I2C_ADDR 0x2a
64 +
65 +#define CMD_GET_STATUS_WORD 0x01
66 +#define STS_FEATURES_SUPPORTED BIT(2)
67 +
68 +#define CMD_GET_FEATURES 0x10
69 +#define FEAT_LED_GAMMA_CORRECTION BIT(5)
70 +
71 +/* LED controller commands at I2C address 0x2b */
72 +#define CMD_LED_MODE 0x03
73 +#define CMD_LED_MODE_LED(l) ((l) & 0x0f)
74 +#define CMD_LED_MODE_USER 0x10
75 +
76 +#define CMD_LED_STATE 0x04
77 +#define CMD_LED_STATE_LED(l) ((l) & 0x0f)
78 +#define CMD_LED_STATE_ON 0x10
79 +
80 +#define CMD_LED_COLOR 0x05
81 +#define CMD_LED_SET_BRIGHTNESS 0x07
82 +#define CMD_LED_GET_BRIGHTNESS 0x08
83 +
84 +#define CMD_SET_GAMMA_CORRECTION 0x30
85 +#define CMD_GET_GAMMA_CORRECTION 0x31
86
87 struct omnia_led {
88 struct led_classdev_mc mc_cdev;
89 @@ -40,6 +53,7 @@ struct omnia_led {
90 struct omnia_leds {
91 struct i2c_client *client;
92 struct mutex lock;
93 + bool has_gamma_correction;
94 struct omnia_led leds[];
95 };
96
97 @@ -50,30 +64,42 @@ static int omnia_cmd_write_u8(const stru
98 return i2c_master_send(client, buf, sizeof(buf));
99 }
100
101 -static int omnia_cmd_read_u8(const struct i2c_client *client, u8 cmd)
102 +static int omnia_cmd_read_raw(struct i2c_adapter *adapter, u8 addr, u8 cmd,
103 + void *reply, size_t len)
104 {
105 struct i2c_msg msgs[2];
106 - u8 reply;
107 int ret;
108
109 - msgs[0].addr = client->addr;
110 + msgs[0].addr = addr;
111 msgs[0].flags = 0;
112 msgs[0].len = 1;
113 msgs[0].buf = &cmd;
114 - msgs[1].addr = client->addr;
115 + msgs[1].addr = addr;
116 msgs[1].flags = I2C_M_RD;
117 - msgs[1].len = 1;
118 - msgs[1].buf = &reply;
119 + msgs[1].len = len;
120 + msgs[1].buf = reply;
121
122 - ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
123 + ret = i2c_transfer(adapter, msgs, ARRAY_SIZE(msgs));
124 if (likely(ret == ARRAY_SIZE(msgs)))
125 - return reply;
126 + return len;
127 else if (ret < 0)
128 return ret;
129 else
130 return -EIO;
131 }
132
133 +static int omnia_cmd_read_u8(const struct i2c_client *client, u8 cmd)
134 +{
135 + u8 reply;
136 + int ret;
137 +
138 + ret = omnia_cmd_read_raw(client->adapter, client->addr, cmd, &reply, 1);
139 + if (ret < 0)
140 + return ret;
141 +
142 + return reply;
143 +}
144 +
145 static int omnia_led_send_color_cmd(const struct i2c_client *client,
146 struct omnia_led *led)
147 {
148 @@ -352,12 +378,74 @@ static ssize_t brightness_store(struct d
149 }
150 static DEVICE_ATTR_RW(brightness);
151
152 +static ssize_t gamma_correction_show(struct device *dev,
153 + struct device_attribute *a, char *buf)
154 +{
155 + struct i2c_client *client = to_i2c_client(dev);
156 + struct omnia_leds *leds = i2c_get_clientdata(client);
157 + int ret;
158 +
159 + if (leds->has_gamma_correction) {
160 + ret = omnia_cmd_read_u8(client, CMD_GET_GAMMA_CORRECTION);
161 + if (ret < 0)
162 + return ret;
163 + } else {
164 + ret = 0;
165 + }
166 +
167 + return sysfs_emit(buf, "%d\n", !!ret);
168 +}
169 +
170 +static ssize_t gamma_correction_store(struct device *dev,
171 + struct device_attribute *a,
172 + const char *buf, size_t count)
173 +{
174 + struct i2c_client *client = to_i2c_client(dev);
175 + struct omnia_leds *leds = i2c_get_clientdata(client);
176 + bool val;
177 + int ret;
178 +
179 + if (!leds->has_gamma_correction)
180 + return -EOPNOTSUPP;
181 +
182 + if (kstrtobool(buf, &val) < 0)
183 + return -EINVAL;
184 +
185 + ret = omnia_cmd_write_u8(client, CMD_SET_GAMMA_CORRECTION, val);
186 +
187 + return ret < 0 ? ret : count;
188 +}
189 +static DEVICE_ATTR_RW(gamma_correction);
190 +
191 static struct attribute *omnia_led_controller_attrs[] = {
192 &dev_attr_brightness.attr,
193 + &dev_attr_gamma_correction.attr,
194 NULL,
195 };
196 ATTRIBUTE_GROUPS(omnia_led_controller);
197
198 +static int omnia_mcu_get_features(const struct i2c_client *client)
199 +{
200 + u16 reply;
201 + int err;
202 +
203 + err = omnia_cmd_read_raw(client->adapter, OMNIA_MCU_I2C_ADDR,
204 + CMD_GET_STATUS_WORD, &reply, sizeof(reply));
205 + if (err < 0)
206 + return err;
207 +
208 + /* Check whether MCU firmware supports the CMD_GET_FEAUTRES command */
209 + if (!(le16_to_cpu(reply) & STS_FEATURES_SUPPORTED))
210 + return 0;
211 +
212 + err = omnia_cmd_read_raw(client->adapter, OMNIA_MCU_I2C_ADDR,
213 + CMD_GET_FEATURES, &reply, sizeof(reply));
214 + if (err < 0)
215 + return err;
216 +
217 + return le16_to_cpu(reply);
218 +}
219 +
220 static int omnia_leds_probe(struct i2c_client *client,
221 const struct i2c_device_id *id)
222 {
223 @@ -383,6 +471,21 @@ static int omnia_leds_probe(struct i2c_c
224 leds->client = client;
225 i2c_set_clientdata(client, leds);
226
227 + ret = omnia_mcu_get_features(client);
228 + if (ret < 0) {
229 + dev_err(dev, "Cannot determine MCU supported features: %d\n",
230 + ret);
231 + return ret;
232 + }
233 +
234 + leds->has_gamma_correction = ret & FEAT_LED_GAMMA_CORRECTION;
235 + if (!leds->has_gamma_correction) {
236 + dev_info(dev,
237 + "Your board's MCU firmware does not support the LED gamma correction feature.\n");
238 + dev_info(dev,
239 + "Consider upgrading MCU firmware with the omnia-mcutool utility.\n");
240 + }
241 +
242 mutex_init(&leds->lock);
243
244 ret = devm_led_trigger_register(dev, &omnia_hw_trigger);