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