Loading...
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * System76 ACPI Driver
4 *
5 * Copyright (C) 2019 System76
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/acpi.h>
13#include <linux/hwmon.h>
14#include <linux/hwmon-sysfs.h>
15#include <linux/init.h>
16#include <linux/input.h>
17#include <linux/kernel.h>
18#include <linux/leds.h>
19#include <linux/module.h>
20#include <linux/pci_ids.h>
21#include <linux/power_supply.h>
22#include <linux/sysfs.h>
23#include <linux/types.h>
24
25#include <acpi/battery.h>
26
27struct system76_data {
28 struct acpi_device *acpi_dev;
29 struct led_classdev ap_led;
30 struct led_classdev kb_led;
31 enum led_brightness kb_brightness;
32 enum led_brightness kb_toggle_brightness;
33 int kb_color;
34 struct device *therm;
35 union acpi_object *nfan;
36 union acpi_object *ntmp;
37 struct input_dev *input;
38 bool has_open_ec;
39};
40
41static const struct acpi_device_id device_ids[] = {
42 {"17761776", 0},
43 {"", 0},
44};
45MODULE_DEVICE_TABLE(acpi, device_ids);
46
47// Array of keyboard LED brightness levels
48static const enum led_brightness kb_levels[] = {
49 48,
50 72,
51 96,
52 144,
53 192,
54 255
55};
56
57// Array of keyboard LED colors in 24-bit RGB format
58static const int kb_colors[] = {
59 0xFFFFFF,
60 0x0000FF,
61 0xFF0000,
62 0xFF00FF,
63 0x00FF00,
64 0x00FFFF,
65 0xFFFF00
66};
67
68// Get a System76 ACPI device value by name
69static int system76_get(struct system76_data *data, char *method)
70{
71 acpi_handle handle;
72 acpi_status status;
73 unsigned long long ret = 0;
74
75 handle = acpi_device_handle(data->acpi_dev);
76 status = acpi_evaluate_integer(handle, method, NULL, &ret);
77 if (ACPI_SUCCESS(status))
78 return ret;
79 return -ENODEV;
80}
81
82// Get a System76 ACPI device value by name with index
83static int system76_get_index(struct system76_data *data, char *method, int index)
84{
85 union acpi_object obj;
86 struct acpi_object_list obj_list;
87 acpi_handle handle;
88 acpi_status status;
89 unsigned long long ret = 0;
90
91 obj.type = ACPI_TYPE_INTEGER;
92 obj.integer.value = index;
93 obj_list.count = 1;
94 obj_list.pointer = &obj;
95
96 handle = acpi_device_handle(data->acpi_dev);
97 status = acpi_evaluate_integer(handle, method, &obj_list, &ret);
98 if (ACPI_SUCCESS(status))
99 return ret;
100 return -ENODEV;
101}
102
103// Get a System76 ACPI device object by name
104static int system76_get_object(struct system76_data *data, char *method, union acpi_object **obj)
105{
106 acpi_handle handle;
107 acpi_status status;
108 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
109
110 handle = acpi_device_handle(data->acpi_dev);
111 status = acpi_evaluate_object(handle, method, NULL, &buf);
112 if (ACPI_SUCCESS(status)) {
113 *obj = buf.pointer;
114 return 0;
115 }
116
117 return -ENODEV;
118}
119
120// Get a name from a System76 ACPI device object
121static char *system76_name(union acpi_object *obj, int index)
122{
123 if (obj && obj->type == ACPI_TYPE_PACKAGE && index <= obj->package.count) {
124 if (obj->package.elements[index].type == ACPI_TYPE_STRING)
125 return obj->package.elements[index].string.pointer;
126 }
127
128 return NULL;
129}
130
131// Set a System76 ACPI device value by name
132static int system76_set(struct system76_data *data, char *method, int value)
133{
134 union acpi_object obj;
135 struct acpi_object_list obj_list;
136 acpi_handle handle;
137 acpi_status status;
138
139 obj.type = ACPI_TYPE_INTEGER;
140 obj.integer.value = value;
141 obj_list.count = 1;
142 obj_list.pointer = &obj;
143 handle = acpi_device_handle(data->acpi_dev);
144 status = acpi_evaluate_object(handle, method, &obj_list, NULL);
145 if (ACPI_SUCCESS(status))
146 return 0;
147 else
148 return -1;
149}
150
151#define BATTERY_THRESHOLD_INVALID 0xFF
152
153enum {
154 THRESHOLD_START,
155 THRESHOLD_END,
156};
157
158static ssize_t battery_get_threshold(int which, char *buf)
159{
160 struct acpi_object_list input;
161 union acpi_object param;
162 acpi_handle handle;
163 acpi_status status;
164 unsigned long long ret = BATTERY_THRESHOLD_INVALID;
165
166 handle = ec_get_handle();
167 if (!handle)
168 return -ENODEV;
169
170 input.count = 1;
171 input.pointer = ¶m;
172 // Start/stop selection
173 param.type = ACPI_TYPE_INTEGER;
174 param.integer.value = which;
175
176 status = acpi_evaluate_integer(handle, "GBCT", &input, &ret);
177 if (ACPI_FAILURE(status))
178 return -EIO;
179 if (ret == BATTERY_THRESHOLD_INVALID)
180 return -EINVAL;
181
182 return sysfs_emit(buf, "%d\n", (int)ret);
183}
184
185static ssize_t battery_set_threshold(int which, const char *buf, size_t count)
186{
187 struct acpi_object_list input;
188 union acpi_object params[2];
189 acpi_handle handle;
190 acpi_status status;
191 unsigned int value;
192 int ret;
193
194 handle = ec_get_handle();
195 if (!handle)
196 return -ENODEV;
197
198 ret = kstrtouint(buf, 10, &value);
199 if (ret)
200 return ret;
201
202 if (value > 100)
203 return -EINVAL;
204
205 input.count = 2;
206 input.pointer = params;
207 // Start/stop selection
208 params[0].type = ACPI_TYPE_INTEGER;
209 params[0].integer.value = which;
210 // Threshold value
211 params[1].type = ACPI_TYPE_INTEGER;
212 params[1].integer.value = value;
213
214 status = acpi_evaluate_object(handle, "SBCT", &input, NULL);
215 if (ACPI_FAILURE(status))
216 return -EIO;
217
218 return count;
219}
220
221static ssize_t charge_control_start_threshold_show(struct device *dev,
222 struct device_attribute *attr, char *buf)
223{
224 return battery_get_threshold(THRESHOLD_START, buf);
225}
226
227static ssize_t charge_control_start_threshold_store(struct device *dev,
228 struct device_attribute *attr, const char *buf, size_t count)
229{
230 return battery_set_threshold(THRESHOLD_START, buf, count);
231}
232
233static DEVICE_ATTR_RW(charge_control_start_threshold);
234
235static ssize_t charge_control_end_threshold_show(struct device *dev,
236 struct device_attribute *attr, char *buf)
237{
238 return battery_get_threshold(THRESHOLD_END, buf);
239}
240
241static ssize_t charge_control_end_threshold_store(struct device *dev,
242 struct device_attribute *attr, const char *buf, size_t count)
243{
244 return battery_set_threshold(THRESHOLD_END, buf, count);
245}
246
247static DEVICE_ATTR_RW(charge_control_end_threshold);
248
249static struct attribute *system76_battery_attrs[] = {
250 &dev_attr_charge_control_start_threshold.attr,
251 &dev_attr_charge_control_end_threshold.attr,
252 NULL,
253};
254
255ATTRIBUTE_GROUPS(system76_battery);
256
257static int system76_battery_add(struct power_supply *battery, struct acpi_battery_hook *hook)
258{
259 // System76 EC only supports 1 battery
260 if (strcmp(battery->desc->name, "BAT0") != 0)
261 return -ENODEV;
262
263 if (device_add_groups(&battery->dev, system76_battery_groups))
264 return -ENODEV;
265
266 return 0;
267}
268
269static int system76_battery_remove(struct power_supply *battery, struct acpi_battery_hook *hook)
270{
271 device_remove_groups(&battery->dev, system76_battery_groups);
272 return 0;
273}
274
275static struct acpi_battery_hook system76_battery_hook = {
276 .add_battery = system76_battery_add,
277 .remove_battery = system76_battery_remove,
278 .name = "System76 Battery Extension",
279};
280
281static void system76_battery_init(void)
282{
283 battery_hook_register(&system76_battery_hook);
284}
285
286static void system76_battery_exit(void)
287{
288 battery_hook_unregister(&system76_battery_hook);
289}
290
291// Get the airplane mode LED brightness
292static enum led_brightness ap_led_get(struct led_classdev *led)
293{
294 struct system76_data *data;
295 int value;
296
297 data = container_of(led, struct system76_data, ap_led);
298 value = system76_get(data, "GAPL");
299 if (value > 0)
300 return (enum led_brightness)value;
301 else
302 return LED_OFF;
303}
304
305// Set the airplane mode LED brightness
306static int ap_led_set(struct led_classdev *led, enum led_brightness value)
307{
308 struct system76_data *data;
309
310 data = container_of(led, struct system76_data, ap_led);
311 return system76_set(data, "SAPL", value == LED_OFF ? 0 : 1);
312}
313
314// Get the last set keyboard LED brightness
315static enum led_brightness kb_led_get(struct led_classdev *led)
316{
317 struct system76_data *data;
318
319 data = container_of(led, struct system76_data, kb_led);
320 return data->kb_brightness;
321}
322
323// Set the keyboard LED brightness
324static int kb_led_set(struct led_classdev *led, enum led_brightness value)
325{
326 struct system76_data *data;
327
328 data = container_of(led, struct system76_data, kb_led);
329 data->kb_brightness = value;
330 return system76_set(data, "SKBL", (int)data->kb_brightness);
331}
332
333// Get the last set keyboard LED color
334static ssize_t kb_led_color_show(
335 struct device *dev,
336 struct device_attribute *dev_attr,
337 char *buf)
338{
339 struct led_classdev *led;
340 struct system76_data *data;
341
342 led = dev_get_drvdata(dev);
343 data = container_of(led, struct system76_data, kb_led);
344 return sysfs_emit(buf, "%06X\n", data->kb_color);
345}
346
347// Set the keyboard LED color
348static ssize_t kb_led_color_store(
349 struct device *dev,
350 struct device_attribute *dev_attr,
351 const char *buf,
352 size_t size)
353{
354 struct led_classdev *led;
355 struct system76_data *data;
356 unsigned int val;
357 int ret;
358
359 led = dev_get_drvdata(dev);
360 data = container_of(led, struct system76_data, kb_led);
361 ret = kstrtouint(buf, 16, &val);
362 if (ret)
363 return ret;
364 if (val > 0xFFFFFF)
365 return -EINVAL;
366 data->kb_color = (int)val;
367 system76_set(data, "SKBC", data->kb_color);
368
369 return size;
370}
371
372static struct device_attribute dev_attr_kb_led_color = {
373 .attr = {
374 .name = "color",
375 .mode = 0644,
376 },
377 .show = kb_led_color_show,
378 .store = kb_led_color_store,
379};
380
381static struct attribute *system76_kb_led_color_attrs[] = {
382 &dev_attr_kb_led_color.attr,
383 NULL,
384};
385
386ATTRIBUTE_GROUPS(system76_kb_led_color);
387
388// Notify that the keyboard LED was changed by hardware
389static void kb_led_notify(struct system76_data *data)
390{
391 led_classdev_notify_brightness_hw_changed(
392 &data->kb_led,
393 data->kb_brightness
394 );
395}
396
397// Read keyboard LED brightness as set by hardware
398static void kb_led_hotkey_hardware(struct system76_data *data)
399{
400 int value;
401
402 value = system76_get(data, "GKBL");
403 if (value < 0)
404 return;
405 data->kb_brightness = value;
406 kb_led_notify(data);
407}
408
409// Toggle the keyboard LED
410static void kb_led_hotkey_toggle(struct system76_data *data)
411{
412 if (data->kb_brightness > 0) {
413 data->kb_toggle_brightness = data->kb_brightness;
414 kb_led_set(&data->kb_led, 0);
415 } else {
416 kb_led_set(&data->kb_led, data->kb_toggle_brightness);
417 }
418 kb_led_notify(data);
419}
420
421// Decrease the keyboard LED brightness
422static void kb_led_hotkey_down(struct system76_data *data)
423{
424 int i;
425
426 if (data->kb_brightness > 0) {
427 for (i = ARRAY_SIZE(kb_levels); i > 0; i--) {
428 if (kb_levels[i - 1] < data->kb_brightness) {
429 kb_led_set(&data->kb_led, kb_levels[i - 1]);
430 break;
431 }
432 }
433 } else {
434 kb_led_set(&data->kb_led, data->kb_toggle_brightness);
435 }
436 kb_led_notify(data);
437}
438
439// Increase the keyboard LED brightness
440static void kb_led_hotkey_up(struct system76_data *data)
441{
442 int i;
443
444 if (data->kb_brightness > 0) {
445 for (i = 0; i < ARRAY_SIZE(kb_levels); i++) {
446 if (kb_levels[i] > data->kb_brightness) {
447 kb_led_set(&data->kb_led, kb_levels[i]);
448 break;
449 }
450 }
451 } else {
452 kb_led_set(&data->kb_led, data->kb_toggle_brightness);
453 }
454 kb_led_notify(data);
455}
456
457// Cycle the keyboard LED color
458static void kb_led_hotkey_color(struct system76_data *data)
459{
460 int i;
461
462 if (data->kb_color < 0)
463 return;
464 if (data->kb_brightness > 0) {
465 for (i = 0; i < ARRAY_SIZE(kb_colors); i++) {
466 if (kb_colors[i] == data->kb_color)
467 break;
468 }
469 i += 1;
470 if (i >= ARRAY_SIZE(kb_colors))
471 i = 0;
472 data->kb_color = kb_colors[i];
473 system76_set(data, "SKBC", data->kb_color);
474 } else {
475 kb_led_set(&data->kb_led, data->kb_toggle_brightness);
476 }
477 kb_led_notify(data);
478}
479
480static umode_t thermal_is_visible(const void *drvdata, enum hwmon_sensor_types type,
481 u32 attr, int channel)
482{
483 const struct system76_data *data = drvdata;
484
485 switch (type) {
486 case hwmon_fan:
487 case hwmon_pwm:
488 if (system76_name(data->nfan, channel))
489 return 0444;
490 break;
491
492 case hwmon_temp:
493 if (system76_name(data->ntmp, channel))
494 return 0444;
495 break;
496
497 default:
498 return 0;
499 }
500
501 return 0;
502}
503
504static int thermal_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
505 int channel, long *val)
506{
507 struct system76_data *data = dev_get_drvdata(dev);
508 int raw;
509
510 switch (type) {
511 case hwmon_fan:
512 if (attr == hwmon_fan_input) {
513 raw = system76_get_index(data, "GFAN", channel);
514 if (raw < 0)
515 return raw;
516 *val = (raw >> 8) & 0xFFFF;
517 return 0;
518 }
519 break;
520
521 case hwmon_pwm:
522 if (attr == hwmon_pwm_input) {
523 raw = system76_get_index(data, "GFAN", channel);
524 if (raw < 0)
525 return raw;
526 *val = raw & 0xFF;
527 return 0;
528 }
529 break;
530
531 case hwmon_temp:
532 if (attr == hwmon_temp_input) {
533 raw = system76_get_index(data, "GTMP", channel);
534 if (raw < 0)
535 return raw;
536 *val = raw * 1000;
537 return 0;
538 }
539 break;
540
541 default:
542 return -EOPNOTSUPP;
543 }
544
545 return -EOPNOTSUPP;
546}
547
548static int thermal_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr,
549 int channel, const char **str)
550{
551 struct system76_data *data = dev_get_drvdata(dev);
552
553 switch (type) {
554 case hwmon_fan:
555 if (attr == hwmon_fan_label) {
556 *str = system76_name(data->nfan, channel);
557 if (*str)
558 return 0;
559 }
560 break;
561
562 case hwmon_temp:
563 if (attr == hwmon_temp_label) {
564 *str = system76_name(data->ntmp, channel);
565 if (*str)
566 return 0;
567 }
568 break;
569
570 default:
571 return -EOPNOTSUPP;
572 }
573
574 return -EOPNOTSUPP;
575}
576
577static const struct hwmon_ops thermal_ops = {
578 .is_visible = thermal_is_visible,
579 .read = thermal_read,
580 .read_string = thermal_read_string,
581};
582
583// Allocate up to 8 fans and temperatures
584static const struct hwmon_channel_info *thermal_channel_info[] = {
585 HWMON_CHANNEL_INFO(fan,
586 HWMON_F_INPUT | HWMON_F_LABEL,
587 HWMON_F_INPUT | HWMON_F_LABEL,
588 HWMON_F_INPUT | HWMON_F_LABEL,
589 HWMON_F_INPUT | HWMON_F_LABEL,
590 HWMON_F_INPUT | HWMON_F_LABEL,
591 HWMON_F_INPUT | HWMON_F_LABEL,
592 HWMON_F_INPUT | HWMON_F_LABEL,
593 HWMON_F_INPUT | HWMON_F_LABEL),
594 HWMON_CHANNEL_INFO(pwm,
595 HWMON_PWM_INPUT,
596 HWMON_PWM_INPUT,
597 HWMON_PWM_INPUT,
598 HWMON_PWM_INPUT,
599 HWMON_PWM_INPUT,
600 HWMON_PWM_INPUT,
601 HWMON_PWM_INPUT,
602 HWMON_PWM_INPUT),
603 HWMON_CHANNEL_INFO(temp,
604 HWMON_T_INPUT | HWMON_T_LABEL,
605 HWMON_T_INPUT | HWMON_T_LABEL,
606 HWMON_T_INPUT | HWMON_T_LABEL,
607 HWMON_T_INPUT | HWMON_T_LABEL,
608 HWMON_T_INPUT | HWMON_T_LABEL,
609 HWMON_T_INPUT | HWMON_T_LABEL,
610 HWMON_T_INPUT | HWMON_T_LABEL,
611 HWMON_T_INPUT | HWMON_T_LABEL),
612 NULL
613};
614
615static const struct hwmon_chip_info thermal_chip_info = {
616 .ops = &thermal_ops,
617 .info = thermal_channel_info,
618};
619
620static void input_key(struct system76_data *data, unsigned int code)
621{
622 input_report_key(data->input, code, 1);
623 input_sync(data->input);
624
625 input_report_key(data->input, code, 0);
626 input_sync(data->input);
627}
628
629// Handle ACPI notification
630static void system76_notify(struct acpi_device *acpi_dev, u32 event)
631{
632 struct system76_data *data;
633
634 data = acpi_driver_data(acpi_dev);
635 switch (event) {
636 case 0x80:
637 kb_led_hotkey_hardware(data);
638 break;
639 case 0x81:
640 kb_led_hotkey_toggle(data);
641 break;
642 case 0x82:
643 kb_led_hotkey_down(data);
644 break;
645 case 0x83:
646 kb_led_hotkey_up(data);
647 break;
648 case 0x84:
649 kb_led_hotkey_color(data);
650 break;
651 case 0x85:
652 input_key(data, KEY_SCREENLOCK);
653 break;
654 }
655}
656
657// Add a System76 ACPI device
658static int system76_add(struct acpi_device *acpi_dev)
659{
660 struct system76_data *data;
661 int err;
662
663 data = devm_kzalloc(&acpi_dev->dev, sizeof(*data), GFP_KERNEL);
664 if (!data)
665 return -ENOMEM;
666 acpi_dev->driver_data = data;
667 data->acpi_dev = acpi_dev;
668
669 // Some models do not run open EC firmware. Check for an ACPI method
670 // that only exists on open EC to guard functionality specific to it.
671 data->has_open_ec = acpi_has_method(acpi_device_handle(data->acpi_dev), "NFAN");
672
673 err = system76_get(data, "INIT");
674 if (err)
675 return err;
676 data->ap_led.name = "system76_acpi::airplane";
677 data->ap_led.flags = LED_CORE_SUSPENDRESUME;
678 data->ap_led.brightness_get = ap_led_get;
679 data->ap_led.brightness_set_blocking = ap_led_set;
680 data->ap_led.max_brightness = 1;
681 data->ap_led.default_trigger = "rfkill-none";
682 err = devm_led_classdev_register(&acpi_dev->dev, &data->ap_led);
683 if (err)
684 return err;
685
686 data->kb_led.name = "system76_acpi::kbd_backlight";
687 data->kb_led.flags = LED_BRIGHT_HW_CHANGED | LED_CORE_SUSPENDRESUME;
688 data->kb_led.brightness_get = kb_led_get;
689 data->kb_led.brightness_set_blocking = kb_led_set;
690 if (acpi_has_method(acpi_device_handle(data->acpi_dev), "SKBC")) {
691 data->kb_led.max_brightness = 255;
692 data->kb_led.groups = system76_kb_led_color_groups;
693 data->kb_toggle_brightness = 72;
694 data->kb_color = 0xffffff;
695 system76_set(data, "SKBC", data->kb_color);
696 } else {
697 data->kb_led.max_brightness = 5;
698 data->kb_color = -1;
699 }
700 err = devm_led_classdev_register(&acpi_dev->dev, &data->kb_led);
701 if (err)
702 return err;
703
704 data->input = devm_input_allocate_device(&acpi_dev->dev);
705 if (!data->input)
706 return -ENOMEM;
707
708 data->input->name = "System76 ACPI Hotkeys";
709 data->input->phys = "system76_acpi/input0";
710 data->input->id.bustype = BUS_HOST;
711 data->input->dev.parent = &acpi_dev->dev;
712 input_set_capability(data->input, EV_KEY, KEY_SCREENLOCK);
713
714 err = input_register_device(data->input);
715 if (err)
716 goto error;
717
718 if (data->has_open_ec) {
719 err = system76_get_object(data, "NFAN", &data->nfan);
720 if (err)
721 goto error;
722
723 err = system76_get_object(data, "NTMP", &data->ntmp);
724 if (err)
725 goto error;
726
727 data->therm = devm_hwmon_device_register_with_info(&acpi_dev->dev,
728 "system76_acpi", data, &thermal_chip_info, NULL);
729 err = PTR_ERR_OR_ZERO(data->therm);
730 if (err)
731 goto error;
732
733 system76_battery_init();
734 }
735
736 return 0;
737
738error:
739 if (data->has_open_ec) {
740 kfree(data->ntmp);
741 kfree(data->nfan);
742 }
743 return err;
744}
745
746// Remove a System76 ACPI device
747static void system76_remove(struct acpi_device *acpi_dev)
748{
749 struct system76_data *data;
750
751 data = acpi_driver_data(acpi_dev);
752
753 if (data->has_open_ec) {
754 system76_battery_exit();
755 kfree(data->nfan);
756 kfree(data->ntmp);
757 }
758
759 devm_led_classdev_unregister(&acpi_dev->dev, &data->ap_led);
760 devm_led_classdev_unregister(&acpi_dev->dev, &data->kb_led);
761
762 system76_get(data, "FINI");
763}
764
765static struct acpi_driver system76_driver = {
766 .name = "System76 ACPI Driver",
767 .class = "hotkey",
768 .ids = device_ids,
769 .ops = {
770 .add = system76_add,
771 .remove = system76_remove,
772 .notify = system76_notify,
773 },
774};
775module_acpi_driver(system76_driver);
776
777MODULE_DESCRIPTION("System76 ACPI Driver");
778MODULE_AUTHOR("Jeremy Soller <jeremy@system76.com>");
779MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * System76 ACPI Driver
4 *
5 * Copyright (C) 2023 System76
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/acpi.h>
13#include <linux/hwmon.h>
14#include <linux/hwmon-sysfs.h>
15#include <linux/init.h>
16#include <linux/input.h>
17#include <linux/kernel.h>
18#include <linux/leds.h>
19#include <linux/module.h>
20#include <linux/pci_ids.h>
21#include <linux/power_supply.h>
22#include <linux/sysfs.h>
23#include <linux/types.h>
24
25#include <acpi/battery.h>
26
27enum kbled_type {
28 KBLED_NONE,
29 KBLED_WHITE,
30 KBLED_RGB,
31};
32
33struct system76_data {
34 struct acpi_device *acpi_dev;
35 struct led_classdev ap_led;
36 struct led_classdev kb_led;
37 enum led_brightness kb_brightness;
38 enum led_brightness kb_toggle_brightness;
39 int kb_color;
40 struct device *therm;
41 union acpi_object *nfan;
42 union acpi_object *ntmp;
43 struct input_dev *input;
44 bool has_open_ec;
45 enum kbled_type kbled_type;
46};
47
48static const struct acpi_device_id device_ids[] = {
49 {"17761776", 0},
50 {"", 0},
51};
52MODULE_DEVICE_TABLE(acpi, device_ids);
53
54// Array of keyboard LED brightness levels
55static const enum led_brightness kb_levels[] = {
56 48,
57 72,
58 96,
59 144,
60 192,
61 255
62};
63
64// Array of keyboard LED colors in 24-bit RGB format
65static const int kb_colors[] = {
66 0xFFFFFF,
67 0x0000FF,
68 0xFF0000,
69 0xFF00FF,
70 0x00FF00,
71 0x00FFFF,
72 0xFFFF00
73};
74
75// Get a System76 ACPI device value by name
76static int system76_get(struct system76_data *data, char *method)
77{
78 acpi_handle handle;
79 acpi_status status;
80 unsigned long long ret = 0;
81
82 handle = acpi_device_handle(data->acpi_dev);
83 status = acpi_evaluate_integer(handle, method, NULL, &ret);
84 if (ACPI_SUCCESS(status))
85 return ret;
86 return -ENODEV;
87}
88
89// Get a System76 ACPI device value by name with index
90static int system76_get_index(struct system76_data *data, char *method, int index)
91{
92 union acpi_object obj;
93 struct acpi_object_list obj_list;
94 acpi_handle handle;
95 acpi_status status;
96 unsigned long long ret = 0;
97
98 obj.type = ACPI_TYPE_INTEGER;
99 obj.integer.value = index;
100 obj_list.count = 1;
101 obj_list.pointer = &obj;
102
103 handle = acpi_device_handle(data->acpi_dev);
104 status = acpi_evaluate_integer(handle, method, &obj_list, &ret);
105 if (ACPI_SUCCESS(status))
106 return ret;
107 return -ENODEV;
108}
109
110// Get a System76 ACPI device object by name
111static int system76_get_object(struct system76_data *data, char *method, union acpi_object **obj)
112{
113 acpi_handle handle;
114 acpi_status status;
115 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
116
117 handle = acpi_device_handle(data->acpi_dev);
118 status = acpi_evaluate_object(handle, method, NULL, &buf);
119 if (ACPI_SUCCESS(status)) {
120 *obj = buf.pointer;
121 return 0;
122 }
123
124 return -ENODEV;
125}
126
127// Get a name from a System76 ACPI device object
128static char *system76_name(union acpi_object *obj, int index)
129{
130 if (obj && obj->type == ACPI_TYPE_PACKAGE && index <= obj->package.count) {
131 if (obj->package.elements[index].type == ACPI_TYPE_STRING)
132 return obj->package.elements[index].string.pointer;
133 }
134
135 return NULL;
136}
137
138// Set a System76 ACPI device value by name
139static int system76_set(struct system76_data *data, char *method, int value)
140{
141 union acpi_object obj;
142 struct acpi_object_list obj_list;
143 acpi_handle handle;
144 acpi_status status;
145
146 obj.type = ACPI_TYPE_INTEGER;
147 obj.integer.value = value;
148 obj_list.count = 1;
149 obj_list.pointer = &obj;
150 handle = acpi_device_handle(data->acpi_dev);
151 status = acpi_evaluate_object(handle, method, &obj_list, NULL);
152 if (ACPI_SUCCESS(status))
153 return 0;
154 else
155 return -1;
156}
157
158#define BATTERY_THRESHOLD_INVALID 0xFF
159
160enum {
161 THRESHOLD_START,
162 THRESHOLD_END,
163};
164
165static ssize_t battery_get_threshold(int which, char *buf)
166{
167 struct acpi_object_list input;
168 union acpi_object param;
169 acpi_handle handle;
170 acpi_status status;
171 unsigned long long ret = BATTERY_THRESHOLD_INVALID;
172
173 handle = ec_get_handle();
174 if (!handle)
175 return -ENODEV;
176
177 input.count = 1;
178 input.pointer = ¶m;
179 // Start/stop selection
180 param.type = ACPI_TYPE_INTEGER;
181 param.integer.value = which;
182
183 status = acpi_evaluate_integer(handle, "GBCT", &input, &ret);
184 if (ACPI_FAILURE(status))
185 return -EIO;
186 if (ret == BATTERY_THRESHOLD_INVALID)
187 return -EINVAL;
188
189 return sysfs_emit(buf, "%d\n", (int)ret);
190}
191
192static ssize_t battery_set_threshold(int which, const char *buf, size_t count)
193{
194 struct acpi_object_list input;
195 union acpi_object params[2];
196 acpi_handle handle;
197 acpi_status status;
198 unsigned int value;
199 int ret;
200
201 handle = ec_get_handle();
202 if (!handle)
203 return -ENODEV;
204
205 ret = kstrtouint(buf, 10, &value);
206 if (ret)
207 return ret;
208
209 if (value > 100)
210 return -EINVAL;
211
212 input.count = 2;
213 input.pointer = params;
214 // Start/stop selection
215 params[0].type = ACPI_TYPE_INTEGER;
216 params[0].integer.value = which;
217 // Threshold value
218 params[1].type = ACPI_TYPE_INTEGER;
219 params[1].integer.value = value;
220
221 status = acpi_evaluate_object(handle, "SBCT", &input, NULL);
222 if (ACPI_FAILURE(status))
223 return -EIO;
224
225 return count;
226}
227
228static ssize_t charge_control_start_threshold_show(struct device *dev,
229 struct device_attribute *attr, char *buf)
230{
231 return battery_get_threshold(THRESHOLD_START, buf);
232}
233
234static ssize_t charge_control_start_threshold_store(struct device *dev,
235 struct device_attribute *attr, const char *buf, size_t count)
236{
237 return battery_set_threshold(THRESHOLD_START, buf, count);
238}
239
240static DEVICE_ATTR_RW(charge_control_start_threshold);
241
242static ssize_t charge_control_end_threshold_show(struct device *dev,
243 struct device_attribute *attr, char *buf)
244{
245 return battery_get_threshold(THRESHOLD_END, buf);
246}
247
248static ssize_t charge_control_end_threshold_store(struct device *dev,
249 struct device_attribute *attr, const char *buf, size_t count)
250{
251 return battery_set_threshold(THRESHOLD_END, buf, count);
252}
253
254static DEVICE_ATTR_RW(charge_control_end_threshold);
255
256static struct attribute *system76_battery_attrs[] = {
257 &dev_attr_charge_control_start_threshold.attr,
258 &dev_attr_charge_control_end_threshold.attr,
259 NULL,
260};
261
262ATTRIBUTE_GROUPS(system76_battery);
263
264static int system76_battery_add(struct power_supply *battery, struct acpi_battery_hook *hook)
265{
266 // System76 EC only supports 1 battery
267 if (strcmp(battery->desc->name, "BAT0") != 0)
268 return -ENODEV;
269
270 if (device_add_groups(&battery->dev, system76_battery_groups))
271 return -ENODEV;
272
273 return 0;
274}
275
276static int system76_battery_remove(struct power_supply *battery, struct acpi_battery_hook *hook)
277{
278 device_remove_groups(&battery->dev, system76_battery_groups);
279 return 0;
280}
281
282static struct acpi_battery_hook system76_battery_hook = {
283 .add_battery = system76_battery_add,
284 .remove_battery = system76_battery_remove,
285 .name = "System76 Battery Extension",
286};
287
288static void system76_battery_init(void)
289{
290 battery_hook_register(&system76_battery_hook);
291}
292
293static void system76_battery_exit(void)
294{
295 battery_hook_unregister(&system76_battery_hook);
296}
297
298// Get the airplane mode LED brightness
299static enum led_brightness ap_led_get(struct led_classdev *led)
300{
301 struct system76_data *data;
302 int value;
303
304 data = container_of(led, struct system76_data, ap_led);
305 value = system76_get(data, "GAPL");
306 if (value > 0)
307 return (enum led_brightness)value;
308 else
309 return LED_OFF;
310}
311
312// Set the airplane mode LED brightness
313static int ap_led_set(struct led_classdev *led, enum led_brightness value)
314{
315 struct system76_data *data;
316
317 data = container_of(led, struct system76_data, ap_led);
318 return system76_set(data, "SAPL", value == LED_OFF ? 0 : 1);
319}
320
321// Get the last set keyboard LED brightness
322static enum led_brightness kb_led_get(struct led_classdev *led)
323{
324 struct system76_data *data;
325
326 data = container_of(led, struct system76_data, kb_led);
327 return data->kb_brightness;
328}
329
330// Set the keyboard LED brightness
331static int kb_led_set(struct led_classdev *led, enum led_brightness value)
332{
333 struct system76_data *data;
334
335 data = container_of(led, struct system76_data, kb_led);
336 data->kb_brightness = value;
337 if (acpi_has_method(acpi_device_handle(data->acpi_dev), "GKBK")) {
338 return system76_set(data, "SKBB", (int)data->kb_brightness);
339 } else {
340 return system76_set(data, "SKBL", (int)data->kb_brightness);
341 }
342}
343
344// Get the last set keyboard LED color
345static ssize_t kb_led_color_show(
346 struct device *dev,
347 struct device_attribute *dev_attr,
348 char *buf)
349{
350 struct led_classdev *led;
351 struct system76_data *data;
352
353 led = dev_get_drvdata(dev);
354 data = container_of(led, struct system76_data, kb_led);
355 return sysfs_emit(buf, "%06X\n", data->kb_color);
356}
357
358// Set the keyboard LED color
359static ssize_t kb_led_color_store(
360 struct device *dev,
361 struct device_attribute *dev_attr,
362 const char *buf,
363 size_t size)
364{
365 struct led_classdev *led;
366 struct system76_data *data;
367 unsigned int val;
368 int ret;
369
370 led = dev_get_drvdata(dev);
371 data = container_of(led, struct system76_data, kb_led);
372 ret = kstrtouint(buf, 16, &val);
373 if (ret)
374 return ret;
375 if (val > 0xFFFFFF)
376 return -EINVAL;
377 data->kb_color = (int)val;
378 system76_set(data, "SKBC", data->kb_color);
379
380 return size;
381}
382
383static struct device_attribute dev_attr_kb_led_color = {
384 .attr = {
385 .name = "color",
386 .mode = 0644,
387 },
388 .show = kb_led_color_show,
389 .store = kb_led_color_store,
390};
391
392static struct attribute *system76_kb_led_color_attrs[] = {
393 &dev_attr_kb_led_color.attr,
394 NULL,
395};
396
397ATTRIBUTE_GROUPS(system76_kb_led_color);
398
399// Notify that the keyboard LED was changed by hardware
400static void kb_led_notify(struct system76_data *data)
401{
402 led_classdev_notify_brightness_hw_changed(
403 &data->kb_led,
404 data->kb_brightness
405 );
406}
407
408// Read keyboard LED brightness as set by hardware
409static void kb_led_hotkey_hardware(struct system76_data *data)
410{
411 int value;
412
413 if (acpi_has_method(acpi_device_handle(data->acpi_dev), "GKBK")) {
414 value = system76_get(data, "GKBB");
415 } else {
416 value = system76_get(data, "GKBL");
417 }
418
419 if (value < 0)
420 return;
421 data->kb_brightness = value;
422 kb_led_notify(data);
423}
424
425// Toggle the keyboard LED
426static void kb_led_hotkey_toggle(struct system76_data *data)
427{
428 if (data->kb_brightness > 0) {
429 data->kb_toggle_brightness = data->kb_brightness;
430 kb_led_set(&data->kb_led, 0);
431 } else {
432 kb_led_set(&data->kb_led, data->kb_toggle_brightness);
433 }
434 kb_led_notify(data);
435}
436
437// Decrease the keyboard LED brightness
438static void kb_led_hotkey_down(struct system76_data *data)
439{
440 int i;
441
442 if (data->kb_brightness > 0) {
443 for (i = ARRAY_SIZE(kb_levels); i > 0; i--) {
444 if (kb_levels[i - 1] < data->kb_brightness) {
445 kb_led_set(&data->kb_led, kb_levels[i - 1]);
446 break;
447 }
448 }
449 } else {
450 kb_led_set(&data->kb_led, data->kb_toggle_brightness);
451 }
452 kb_led_notify(data);
453}
454
455// Increase the keyboard LED brightness
456static void kb_led_hotkey_up(struct system76_data *data)
457{
458 int i;
459
460 if (data->kb_brightness > 0) {
461 for (i = 0; i < ARRAY_SIZE(kb_levels); i++) {
462 if (kb_levels[i] > data->kb_brightness) {
463 kb_led_set(&data->kb_led, kb_levels[i]);
464 break;
465 }
466 }
467 } else {
468 kb_led_set(&data->kb_led, data->kb_toggle_brightness);
469 }
470 kb_led_notify(data);
471}
472
473// Cycle the keyboard LED color
474static void kb_led_hotkey_color(struct system76_data *data)
475{
476 int i;
477
478 if (data->kbled_type != KBLED_RGB)
479 return;
480
481 if (data->kb_brightness > 0) {
482 for (i = 0; i < ARRAY_SIZE(kb_colors); i++) {
483 if (kb_colors[i] == data->kb_color)
484 break;
485 }
486 i += 1;
487 if (i >= ARRAY_SIZE(kb_colors))
488 i = 0;
489 data->kb_color = kb_colors[i];
490 system76_set(data, "SKBC", data->kb_color);
491 } else {
492 kb_led_set(&data->kb_led, data->kb_toggle_brightness);
493 }
494 kb_led_notify(data);
495}
496
497static umode_t thermal_is_visible(const void *drvdata, enum hwmon_sensor_types type,
498 u32 attr, int channel)
499{
500 const struct system76_data *data = drvdata;
501
502 switch (type) {
503 case hwmon_fan:
504 case hwmon_pwm:
505 if (system76_name(data->nfan, channel))
506 return 0444;
507 break;
508
509 case hwmon_temp:
510 if (system76_name(data->ntmp, channel))
511 return 0444;
512 break;
513
514 default:
515 return 0;
516 }
517
518 return 0;
519}
520
521static int thermal_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
522 int channel, long *val)
523{
524 struct system76_data *data = dev_get_drvdata(dev);
525 int raw;
526
527 switch (type) {
528 case hwmon_fan:
529 if (attr == hwmon_fan_input) {
530 raw = system76_get_index(data, "GFAN", channel);
531 if (raw < 0)
532 return raw;
533 *val = (raw >> 8) & 0xFFFF;
534 return 0;
535 }
536 break;
537
538 case hwmon_pwm:
539 if (attr == hwmon_pwm_input) {
540 raw = system76_get_index(data, "GFAN", channel);
541 if (raw < 0)
542 return raw;
543 *val = raw & 0xFF;
544 return 0;
545 }
546 break;
547
548 case hwmon_temp:
549 if (attr == hwmon_temp_input) {
550 raw = system76_get_index(data, "GTMP", channel);
551 if (raw < 0)
552 return raw;
553 *val = raw * 1000;
554 return 0;
555 }
556 break;
557
558 default:
559 return -EOPNOTSUPP;
560 }
561
562 return -EOPNOTSUPP;
563}
564
565static int thermal_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr,
566 int channel, const char **str)
567{
568 struct system76_data *data = dev_get_drvdata(dev);
569
570 switch (type) {
571 case hwmon_fan:
572 if (attr == hwmon_fan_label) {
573 *str = system76_name(data->nfan, channel);
574 if (*str)
575 return 0;
576 }
577 break;
578
579 case hwmon_temp:
580 if (attr == hwmon_temp_label) {
581 *str = system76_name(data->ntmp, channel);
582 if (*str)
583 return 0;
584 }
585 break;
586
587 default:
588 return -EOPNOTSUPP;
589 }
590
591 return -EOPNOTSUPP;
592}
593
594static const struct hwmon_ops thermal_ops = {
595 .is_visible = thermal_is_visible,
596 .read = thermal_read,
597 .read_string = thermal_read_string,
598};
599
600// Allocate up to 8 fans and temperatures
601static const struct hwmon_channel_info * const thermal_channel_info[] = {
602 HWMON_CHANNEL_INFO(fan,
603 HWMON_F_INPUT | HWMON_F_LABEL,
604 HWMON_F_INPUT | HWMON_F_LABEL,
605 HWMON_F_INPUT | HWMON_F_LABEL,
606 HWMON_F_INPUT | HWMON_F_LABEL,
607 HWMON_F_INPUT | HWMON_F_LABEL,
608 HWMON_F_INPUT | HWMON_F_LABEL,
609 HWMON_F_INPUT | HWMON_F_LABEL,
610 HWMON_F_INPUT | HWMON_F_LABEL),
611 HWMON_CHANNEL_INFO(pwm,
612 HWMON_PWM_INPUT,
613 HWMON_PWM_INPUT,
614 HWMON_PWM_INPUT,
615 HWMON_PWM_INPUT,
616 HWMON_PWM_INPUT,
617 HWMON_PWM_INPUT,
618 HWMON_PWM_INPUT,
619 HWMON_PWM_INPUT),
620 HWMON_CHANNEL_INFO(temp,
621 HWMON_T_INPUT | HWMON_T_LABEL,
622 HWMON_T_INPUT | HWMON_T_LABEL,
623 HWMON_T_INPUT | HWMON_T_LABEL,
624 HWMON_T_INPUT | HWMON_T_LABEL,
625 HWMON_T_INPUT | HWMON_T_LABEL,
626 HWMON_T_INPUT | HWMON_T_LABEL,
627 HWMON_T_INPUT | HWMON_T_LABEL,
628 HWMON_T_INPUT | HWMON_T_LABEL),
629 NULL
630};
631
632static const struct hwmon_chip_info thermal_chip_info = {
633 .ops = &thermal_ops,
634 .info = thermal_channel_info,
635};
636
637static void input_key(struct system76_data *data, unsigned int code)
638{
639 input_report_key(data->input, code, 1);
640 input_sync(data->input);
641
642 input_report_key(data->input, code, 0);
643 input_sync(data->input);
644}
645
646// Handle ACPI notification
647static void system76_notify(struct acpi_device *acpi_dev, u32 event)
648{
649 struct system76_data *data;
650
651 data = acpi_driver_data(acpi_dev);
652 switch (event) {
653 case 0x80:
654 kb_led_hotkey_hardware(data);
655 break;
656 case 0x81:
657 kb_led_hotkey_toggle(data);
658 break;
659 case 0x82:
660 kb_led_hotkey_down(data);
661 break;
662 case 0x83:
663 kb_led_hotkey_up(data);
664 break;
665 case 0x84:
666 kb_led_hotkey_color(data);
667 break;
668 case 0x85:
669 input_key(data, KEY_SCREENLOCK);
670 break;
671 }
672}
673
674// Add a System76 ACPI device
675static int system76_add(struct acpi_device *acpi_dev)
676{
677 struct system76_data *data;
678 int err;
679
680 data = devm_kzalloc(&acpi_dev->dev, sizeof(*data), GFP_KERNEL);
681 if (!data)
682 return -ENOMEM;
683 acpi_dev->driver_data = data;
684 data->acpi_dev = acpi_dev;
685
686 // Some models do not run open EC firmware. Check for an ACPI method
687 // that only exists on open EC to guard functionality specific to it.
688 data->has_open_ec = acpi_has_method(acpi_device_handle(data->acpi_dev), "NFAN");
689
690 err = system76_get(data, "INIT");
691 if (err)
692 return err;
693 data->ap_led.name = "system76_acpi::airplane";
694 data->ap_led.flags = LED_CORE_SUSPENDRESUME;
695 data->ap_led.brightness_get = ap_led_get;
696 data->ap_led.brightness_set_blocking = ap_led_set;
697 data->ap_led.max_brightness = 1;
698 data->ap_led.default_trigger = "rfkill-none";
699 err = devm_led_classdev_register(&acpi_dev->dev, &data->ap_led);
700 if (err)
701 return err;
702
703 data->kb_led.name = "system76_acpi::kbd_backlight";
704 data->kb_led.flags = LED_BRIGHT_HW_CHANGED | LED_CORE_SUSPENDRESUME;
705 data->kb_led.brightness_get = kb_led_get;
706 data->kb_led.brightness_set_blocking = kb_led_set;
707 if (acpi_has_method(acpi_device_handle(data->acpi_dev), "GKBK")) {
708 // Use the new ACPI methods
709 data->kbled_type = system76_get(data, "GKBK");
710
711 switch (data->kbled_type) {
712 case KBLED_NONE:
713 // Nothing to do: Device will not be registered.
714 break;
715 case KBLED_WHITE:
716 data->kb_led.max_brightness = 255;
717 data->kb_toggle_brightness = 72;
718 break;
719 case KBLED_RGB:
720 data->kb_led.max_brightness = 255;
721 data->kb_led.groups = system76_kb_led_color_groups;
722 data->kb_toggle_brightness = 72;
723 data->kb_color = 0xffffff;
724 system76_set(data, "SKBC", data->kb_color);
725 break;
726 }
727 } else {
728 // Use the old ACPI methods
729 if (acpi_has_method(acpi_device_handle(data->acpi_dev), "SKBC")) {
730 data->kbled_type = KBLED_RGB;
731 data->kb_led.max_brightness = 255;
732 data->kb_led.groups = system76_kb_led_color_groups;
733 data->kb_toggle_brightness = 72;
734 data->kb_color = 0xffffff;
735 system76_set(data, "SKBC", data->kb_color);
736 } else {
737 data->kbled_type = KBLED_WHITE;
738 data->kb_led.max_brightness = 5;
739 }
740 }
741
742 if (data->kbled_type != KBLED_NONE) {
743 err = devm_led_classdev_register(&acpi_dev->dev, &data->kb_led);
744 if (err)
745 return err;
746 }
747
748 data->input = devm_input_allocate_device(&acpi_dev->dev);
749 if (!data->input)
750 return -ENOMEM;
751
752 data->input->name = "System76 ACPI Hotkeys";
753 data->input->phys = "system76_acpi/input0";
754 data->input->id.bustype = BUS_HOST;
755 data->input->dev.parent = &acpi_dev->dev;
756 input_set_capability(data->input, EV_KEY, KEY_SCREENLOCK);
757
758 err = input_register_device(data->input);
759 if (err)
760 goto error;
761
762 if (data->has_open_ec) {
763 err = system76_get_object(data, "NFAN", &data->nfan);
764 if (err)
765 goto error;
766
767 err = system76_get_object(data, "NTMP", &data->ntmp);
768 if (err)
769 goto error;
770
771 data->therm = devm_hwmon_device_register_with_info(&acpi_dev->dev,
772 "system76_acpi", data, &thermal_chip_info, NULL);
773 err = PTR_ERR_OR_ZERO(data->therm);
774 if (err)
775 goto error;
776
777 system76_battery_init();
778 }
779
780 return 0;
781
782error:
783 if (data->has_open_ec) {
784 kfree(data->ntmp);
785 kfree(data->nfan);
786 }
787 return err;
788}
789
790// Remove a System76 ACPI device
791static void system76_remove(struct acpi_device *acpi_dev)
792{
793 struct system76_data *data;
794
795 data = acpi_driver_data(acpi_dev);
796
797 if (data->has_open_ec) {
798 system76_battery_exit();
799 kfree(data->nfan);
800 kfree(data->ntmp);
801 }
802
803 devm_led_classdev_unregister(&acpi_dev->dev, &data->ap_led);
804 devm_led_classdev_unregister(&acpi_dev->dev, &data->kb_led);
805
806 system76_get(data, "FINI");
807}
808
809static struct acpi_driver system76_driver = {
810 .name = "System76 ACPI Driver",
811 .class = "hotkey",
812 .ids = device_ids,
813 .ops = {
814 .add = system76_add,
815 .remove = system76_remove,
816 .notify = system76_notify,
817 },
818};
819module_acpi_driver(system76_driver);
820
821MODULE_DESCRIPTION("System76 ACPI Driver");
822MODULE_AUTHOR("Jeremy Soller <jeremy@system76.com>");
823MODULE_LICENSE("GPL");