forked from pop-os/system76-dkms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem76_kb-led.c
338 lines (269 loc) · 8.31 KB
/
system76_kb-led.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* kb_led.c
*
* Copyright (C) 2017 Jeremy Soller <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define SET_KB_LED 0x67
union kb_led_color {
u32 rgb;
struct { u32 b:8, g:8, r:8, : 8; };
};
enum kb_led_region {
KB_LED_REGION_LEFT,
KB_LED_REGION_CENTER,
KB_LED_REGION_RIGHT,
KB_LED_REGION_EXTRA,
};
static enum led_brightness kb_led_brightness = 0;
static enum led_brightness kb_led_toggle_brightness = 72;
static enum led_brightness kb_led_levels[] = { 48, 72, 96, 144, 192, 255 };
static union kb_led_color kb_led_regions[] = {
{ .rgb = 0xFFFFFF },
{ .rgb = 0xFFFFFF },
{ .rgb = 0xFFFFFF },
{ .rgb = 0xFFFFFF }
};
static int kb_led_colors_i = 0;
static union kb_led_color kb_led_colors[] = {
{ .rgb = 0xFFFFFF },
{ .rgb = 0x0000FF },
{ .rgb = 0xFF0000 },
{ .rgb = 0xFF00FF },
{ .rgb = 0x00FF00 },
{ .rgb = 0x00FFFF },
{ .rgb = 0xFFFF00 }
};
static enum led_brightness kb_led_get(struct led_classdev *led_cdev) {
return kb_led_brightness;
}
static int kb_led_set(struct led_classdev *led_cdev, enum led_brightness value) {
S76_DEBUG("kb_led_set %d\n", (int)value);
if (!s76_wmbb(SET_KB_LED, 0xF4000000 | value, NULL)) {
kb_led_brightness = value;
}
return 0;
}
static void kb_led_color_set(enum kb_led_region region, union kb_led_color color) {
u32 cmd;
S76_DEBUG("kb_led_color_set %d %06X\n", (int)region, (int)color.rgb);
switch (region) {
case KB_LED_REGION_LEFT:
cmd = 0xF0000000;
break;
case KB_LED_REGION_CENTER:
cmd = 0xF1000000;
break;
case KB_LED_REGION_RIGHT:
cmd = 0xF2000000;
break;
case KB_LED_REGION_EXTRA:
cmd = 0xF3000000;
break;
default:
return;
}
cmd |= color.b << 16;
cmd |= color.r << 8;
cmd |= color.g << 0;
if (!s76_wmbb(SET_KB_LED, cmd, NULL)) {
kb_led_regions[region] = color;
}
}
static struct led_classdev kb_led = {
.name = "system76::kbd_backlight",
.flags = LED_BRIGHT_HW_CHANGED,
.brightness_get = kb_led_get,
.brightness_set_blocking = kb_led_set,
.max_brightness = 255,
};
static ssize_t kb_led_color_show(enum kb_led_region region, char *buf) {
return sprintf(buf, "%06X\n", (int)kb_led_regions[region].rgb);
}
static ssize_t kb_led_color_store(enum kb_led_region region, const char *buf, size_t size) {
unsigned int val;
int ret;
union kb_led_color color;
ret = kstrtouint(buf, 16, &val);
if (ret) {
return ret;
}
color.rgb = (u32)val;
kb_led_color_set(region, color);
return size;
}
static ssize_t kb_led_color_left_show(struct device *dev, struct device_attribute *attr, char *buf) {
return kb_led_color_show(KB_LED_REGION_LEFT, buf);
}
static ssize_t kb_led_color_left_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) {
return kb_led_color_store(KB_LED_REGION_LEFT, buf, size);
}
static struct device_attribute kb_led_color_left_dev_attr = {
.attr = {
.name = "color_left",
.mode = 0644,
},
.show = kb_led_color_left_show,
.store = kb_led_color_left_store,
};
static ssize_t kb_led_color_center_show(struct device *dev, struct device_attribute *attr, char *buf) {
return kb_led_color_show(KB_LED_REGION_CENTER, buf);
}
static ssize_t kb_led_color_center_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) {
return kb_led_color_store(KB_LED_REGION_CENTER, buf, size);
}
static struct device_attribute kb_led_color_center_dev_attr = {
.attr = {
.name = "color_center",
.mode = 0644,
},
.show = kb_led_color_center_show,
.store = kb_led_color_center_store,
};
static ssize_t kb_led_color_right_show(struct device *dev, struct device_attribute *attr, char *buf) {
return kb_led_color_show(KB_LED_REGION_RIGHT, buf);
}
static ssize_t kb_led_color_right_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) {
return kb_led_color_store(KB_LED_REGION_RIGHT, buf, size);
}
static struct device_attribute kb_led_color_right_dev_attr = {
.attr = {
.name = "color_right",
.mode = 0644,
},
.show = kb_led_color_right_show,
.store = kb_led_color_right_store,
};
static ssize_t kb_led_color_extra_show(struct device *dev, struct device_attribute *attr, char *buf) {
return kb_led_color_show(KB_LED_REGION_EXTRA, buf);
}
static ssize_t kb_led_color_extra_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) {
return kb_led_color_store(KB_LED_REGION_EXTRA, buf, size);
}
static struct device_attribute kb_led_color_extra_dev_attr = {
.attr = {
.name = "color_extra",
.mode = 0644,
},
.show = kb_led_color_extra_show,
.store = kb_led_color_extra_store,
};
static void kb_led_enable(void) {
S76_DEBUG("kb_led_enable\n");
s76_wmbb(SET_KB_LED, 0xE007F001, NULL);
}
static void kb_led_disable(void) {
S76_DEBUG("kb_led_disable\n");
s76_wmbb(SET_KB_LED, 0xE0003001, NULL);
}
static void kb_led_suspend(void) {
S76_DEBUG("kb_led_suspend\n");
// Disable keyboard backlight
kb_led_disable();
}
static void kb_led_resume(void) {
enum kb_led_region region;
S76_DEBUG("kb_led_resume\n");
// Disable keyboard backlight
kb_led_disable();
// Reset current color
for (region = 0; region < sizeof(kb_led_regions)/sizeof(union kb_led_color); region++) {
kb_led_color_set(region, kb_led_regions[region]);
}
// Reset current brightness
kb_led_set(&kb_led, kb_led_brightness);
// Enable keyboard backlight
kb_led_enable();
}
static int __init kb_led_init(struct device *dev) {
int err;
err = led_classdev_register(dev, &kb_led);
if (unlikely(err)) {
return err;
}
if (device_create_file(kb_led.dev, &kb_led_color_left_dev_attr) != 0) {
S76_ERROR("failed to create kb_led_color_left\n");
}
if (device_create_file(kb_led.dev, &kb_led_color_center_dev_attr) != 0) {
S76_ERROR("failed to create kb_led_color_center\n");
}
if (device_create_file(kb_led.dev, &kb_led_color_right_dev_attr) != 0) {
S76_ERROR("failed to create kb_led_color_right\n");
}
if (device_create_file(kb_led.dev, &kb_led_color_extra_dev_attr) != 0) {
S76_ERROR("failed to create kb_led_color_extra\n");
}
kb_led_resume();
return 0;
}
static void __exit kb_led_exit(void) {
device_remove_file(kb_led.dev, &kb_led_color_extra_dev_attr);
device_remove_file(kb_led.dev, &kb_led_color_right_dev_attr);
device_remove_file(kb_led.dev, &kb_led_color_center_dev_attr);
device_remove_file(kb_led.dev, &kb_led_color_left_dev_attr);
if (!IS_ERR_OR_NULL(kb_led.dev)) {
led_classdev_unregister(&kb_led);
}
}
static void kb_wmi_brightness(enum led_brightness value) {
S76_DEBUG("kb_wmi_brightness %d\n", (int)value);
kb_led_set(&kb_led, value);
led_classdev_notify_brightness_hw_changed(&kb_led, value);
}
static void kb_wmi_toggle(void) {
if (kb_led_brightness > 0) {
kb_led_toggle_brightness = kb_led_brightness;
kb_wmi_brightness(LED_OFF);
} else {
kb_wmi_brightness(kb_led_toggle_brightness);
}
}
static void kb_wmi_dec(void) {
int i;
if (kb_led_brightness > 0) {
for (i = sizeof(kb_led_levels)/sizeof(enum led_brightness); i > 0; i--) {
if (kb_led_levels[i - 1] < kb_led_brightness) {
kb_wmi_brightness(kb_led_levels[i - 1]);
break;
}
}
} else {
kb_wmi_toggle();
}
}
static void kb_wmi_inc(void) {
int i;
if (kb_led_brightness > 0) {
for (i = 0; i < sizeof(kb_led_levels)/sizeof(enum led_brightness); i++) {
if (kb_led_levels[i] > kb_led_brightness) {
kb_wmi_brightness(kb_led_levels[i]);
break;
}
}
} else {
kb_wmi_toggle();
}
}
static void kb_wmi_color(void) {
enum kb_led_region region;
kb_led_colors_i += 1;
if (kb_led_colors_i >= sizeof(kb_led_colors)/sizeof(union kb_led_color)) {
kb_led_colors_i = 0;
}
for (region = 0; region < sizeof(kb_led_regions)/sizeof(union kb_led_color); region++) {
kb_led_color_set(region, kb_led_colors[kb_led_colors_i]);
}
led_classdev_notify_brightness_hw_changed(&kb_led, kb_led_brightness);
}