-
Notifications
You must be signed in to change notification settings - Fork 0
/
system76_legacy_ap-led.c
128 lines (101 loc) · 2.81 KB
/
system76_legacy_ap-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
/*
* ap_led.c
*
* Copyright (C) 2019 Simon Doppler <dopsi@dopsi.ch>
* Copyright (C) 2017 Jeremy Soller <jeremy@system76.com>
*
* 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/>.
*/
static enum led_brightness ap_led_brightness = 1;
static bool ap_led_invert = TRUE;
static enum led_brightness ap_led_get(struct led_classdev *led_cdev) {
return ap_led_brightness;
}
static int ap_led_set(struct led_classdev *led_cdev, enum led_brightness value) {
u8 byte;
ec_read(0xD9, &byte);
if (value > 0) {
ap_led_brightness = 1;
if (ap_led_invert) {
byte &= ~0x40;
} else {
byte |= 0x40;
}
} else {
ap_led_brightness = 0;
if (ap_led_invert) {
byte |= 0x40;
} else {
byte &= ~0x40;
}
}
ec_write(0xD9, byte);
return 0;
}
static struct led_classdev ap_led = {
.name = "system76_legacy::airplane",
.brightness_get = ap_led_get,
.brightness_set_blocking = ap_led_set,
.max_brightness = 1,
.default_trigger = "rfkill0"
};
static ssize_t ap_led_invert_show(struct device *dev, struct device_attribute *attr, char *buf) {
return sprintf(buf, "%d\n", (int)ap_led_invert);
}
static ssize_t ap_led_invert_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) {
unsigned int val;
int ret;
enum led_brightness brightness;
ret = kstrtouint(buf, 0, &val);
if (ret) {
return ret;
}
brightness = ap_led_get(&ap_led);
if (val) {
ap_led_invert = TRUE;
} else {
ap_led_invert = FALSE;
}
ap_led_set(&ap_led, brightness);
return size;
}
static struct device_attribute ap_led_invert_dev_attr = {
.attr = {
.name = "invert",
.mode = 0644,
},
.show = ap_led_invert_show,
.store = ap_led_invert_store,
};
static void ap_led_resume(void) {
ap_led_set(&ap_led, ap_led_brightness);
}
static int __init ap_led_init(struct device *dev) {
int err;
err = led_classdev_register(dev, &ap_led);
if (unlikely(err)) {
return err;
}
if (device_create_file(ap_led.dev, &ap_led_invert_dev_attr) != 0) {
S76LEGACY_ERROR("failed to create ap_led_invert\n");
}
ap_led_resume();
return 0;
}
static void __exit ap_led_exit(void) {
device_remove_file(ap_led.dev, &ap_led_invert_dev_attr);
if (!IS_ERR_OR_NULL(ap_led.dev)) {
led_classdev_unregister(&ap_led);
}
}