Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * System76 ACPI Driver
0004  *
0005  * Copyright (C) 2019 System76
0006  *
0007  * This program is free software; you can redistribute it and/or modify
0008  * it under the terms of the GNU General Public License version 2 as
0009  * published by the Free Software Foundation.
0010  */
0011 
0012 #include <linux/acpi.h>
0013 #include <linux/hwmon.h>
0014 #include <linux/hwmon-sysfs.h>
0015 #include <linux/init.h>
0016 #include <linux/input.h>
0017 #include <linux/kernel.h>
0018 #include <linux/leds.h>
0019 #include <linux/module.h>
0020 #include <linux/pci_ids.h>
0021 #include <linux/power_supply.h>
0022 #include <linux/sysfs.h>
0023 #include <linux/types.h>
0024 
0025 #include <acpi/battery.h>
0026 
0027 struct system76_data {
0028     struct acpi_device *acpi_dev;
0029     struct led_classdev ap_led;
0030     struct led_classdev kb_led;
0031     enum led_brightness kb_brightness;
0032     enum led_brightness kb_toggle_brightness;
0033     int kb_color;
0034     struct device *therm;
0035     union acpi_object *nfan;
0036     union acpi_object *ntmp;
0037     struct input_dev *input;
0038     bool has_open_ec;
0039 };
0040 
0041 static const struct acpi_device_id device_ids[] = {
0042     {"17761776", 0},
0043     {"", 0},
0044 };
0045 MODULE_DEVICE_TABLE(acpi, device_ids);
0046 
0047 // Array of keyboard LED brightness levels
0048 static const enum led_brightness kb_levels[] = {
0049     48,
0050     72,
0051     96,
0052     144,
0053     192,
0054     255
0055 };
0056 
0057 // Array of keyboard LED colors in 24-bit RGB format
0058 static const int kb_colors[] = {
0059     0xFFFFFF,
0060     0x0000FF,
0061     0xFF0000,
0062     0xFF00FF,
0063     0x00FF00,
0064     0x00FFFF,
0065     0xFFFF00
0066 };
0067 
0068 // Get a System76 ACPI device value by name
0069 static int system76_get(struct system76_data *data, char *method)
0070 {
0071     acpi_handle handle;
0072     acpi_status status;
0073     unsigned long long ret = 0;
0074 
0075     handle = acpi_device_handle(data->acpi_dev);
0076     status = acpi_evaluate_integer(handle, method, NULL, &ret);
0077     if (ACPI_SUCCESS(status))
0078         return ret;
0079     return -ENODEV;
0080 }
0081 
0082 // Get a System76 ACPI device value by name with index
0083 static int system76_get_index(struct system76_data *data, char *method, int index)
0084 {
0085     union acpi_object obj;
0086     struct acpi_object_list obj_list;
0087     acpi_handle handle;
0088     acpi_status status;
0089     unsigned long long ret = 0;
0090 
0091     obj.type = ACPI_TYPE_INTEGER;
0092     obj.integer.value = index;
0093     obj_list.count = 1;
0094     obj_list.pointer = &obj;
0095 
0096     handle = acpi_device_handle(data->acpi_dev);
0097     status = acpi_evaluate_integer(handle, method, &obj_list, &ret);
0098     if (ACPI_SUCCESS(status))
0099         return ret;
0100     return -ENODEV;
0101 }
0102 
0103 // Get a System76 ACPI device object by name
0104 static int system76_get_object(struct system76_data *data, char *method, union acpi_object **obj)
0105 {
0106     acpi_handle handle;
0107     acpi_status status;
0108     struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
0109 
0110     handle = acpi_device_handle(data->acpi_dev);
0111     status = acpi_evaluate_object(handle, method, NULL, &buf);
0112     if (ACPI_SUCCESS(status)) {
0113         *obj = buf.pointer;
0114         return 0;
0115     }
0116 
0117     return -ENODEV;
0118 }
0119 
0120 // Get a name from a System76 ACPI device object
0121 static char *system76_name(union acpi_object *obj, int index)
0122 {
0123     if (obj && obj->type == ACPI_TYPE_PACKAGE && index <= obj->package.count) {
0124         if (obj->package.elements[index].type == ACPI_TYPE_STRING)
0125             return obj->package.elements[index].string.pointer;
0126     }
0127 
0128     return NULL;
0129 }
0130 
0131 // Set a System76 ACPI device value by name
0132 static int system76_set(struct system76_data *data, char *method, int value)
0133 {
0134     union acpi_object obj;
0135     struct acpi_object_list obj_list;
0136     acpi_handle handle;
0137     acpi_status status;
0138 
0139     obj.type = ACPI_TYPE_INTEGER;
0140     obj.integer.value = value;
0141     obj_list.count = 1;
0142     obj_list.pointer = &obj;
0143     handle = acpi_device_handle(data->acpi_dev);
0144     status = acpi_evaluate_object(handle, method, &obj_list, NULL);
0145     if (ACPI_SUCCESS(status))
0146         return 0;
0147     else
0148         return -1;
0149 }
0150 
0151 #define BATTERY_THRESHOLD_INVALID   0xFF
0152 
0153 enum {
0154     THRESHOLD_START,
0155     THRESHOLD_END,
0156 };
0157 
0158 static ssize_t battery_get_threshold(int which, char *buf)
0159 {
0160     struct acpi_object_list input;
0161     union acpi_object param;
0162     acpi_handle handle;
0163     acpi_status status;
0164     unsigned long long ret = BATTERY_THRESHOLD_INVALID;
0165 
0166     handle = ec_get_handle();
0167     if (!handle)
0168         return -ENODEV;
0169 
0170     input.count = 1;
0171     input.pointer = &param;
0172     // Start/stop selection
0173     param.type = ACPI_TYPE_INTEGER;
0174     param.integer.value = which;
0175 
0176     status = acpi_evaluate_integer(handle, "GBCT", &input, &ret);
0177     if (ACPI_FAILURE(status))
0178         return -EIO;
0179     if (ret == BATTERY_THRESHOLD_INVALID)
0180         return -EINVAL;
0181 
0182     return sysfs_emit(buf, "%d\n", (int)ret);
0183 }
0184 
0185 static ssize_t battery_set_threshold(int which, const char *buf, size_t count)
0186 {
0187     struct acpi_object_list input;
0188     union acpi_object params[2];
0189     acpi_handle handle;
0190     acpi_status status;
0191     unsigned int value;
0192     int ret;
0193 
0194     handle = ec_get_handle();
0195     if (!handle)
0196         return -ENODEV;
0197 
0198     ret = kstrtouint(buf, 10, &value);
0199     if (ret)
0200         return ret;
0201 
0202     if (value > 100)
0203         return -EINVAL;
0204 
0205     input.count = 2;
0206     input.pointer = params;
0207     // Start/stop selection
0208     params[0].type = ACPI_TYPE_INTEGER;
0209     params[0].integer.value = which;
0210     // Threshold value
0211     params[1].type = ACPI_TYPE_INTEGER;
0212     params[1].integer.value = value;
0213 
0214     status = acpi_evaluate_object(handle, "SBCT", &input, NULL);
0215     if (ACPI_FAILURE(status))
0216         return -EIO;
0217 
0218     return count;
0219 }
0220 
0221 static ssize_t charge_control_start_threshold_show(struct device *dev,
0222     struct device_attribute *attr, char *buf)
0223 {
0224     return battery_get_threshold(THRESHOLD_START, buf);
0225 }
0226 
0227 static ssize_t charge_control_start_threshold_store(struct device *dev,
0228     struct device_attribute *attr, const char *buf, size_t count)
0229 {
0230     return battery_set_threshold(THRESHOLD_START, buf, count);
0231 }
0232 
0233 static DEVICE_ATTR_RW(charge_control_start_threshold);
0234 
0235 static ssize_t charge_control_end_threshold_show(struct device *dev,
0236     struct device_attribute *attr, char *buf)
0237 {
0238     return battery_get_threshold(THRESHOLD_END, buf);
0239 }
0240 
0241 static ssize_t charge_control_end_threshold_store(struct device *dev,
0242     struct device_attribute *attr, const char *buf, size_t count)
0243 {
0244     return battery_set_threshold(THRESHOLD_END, buf, count);
0245 }
0246 
0247 static DEVICE_ATTR_RW(charge_control_end_threshold);
0248 
0249 static struct attribute *system76_battery_attrs[] = {
0250     &dev_attr_charge_control_start_threshold.attr,
0251     &dev_attr_charge_control_end_threshold.attr,
0252     NULL,
0253 };
0254 
0255 ATTRIBUTE_GROUPS(system76_battery);
0256 
0257 static int system76_battery_add(struct power_supply *battery)
0258 {
0259     // System76 EC only supports 1 battery
0260     if (strcmp(battery->desc->name, "BAT0") != 0)
0261         return -ENODEV;
0262 
0263     if (device_add_groups(&battery->dev, system76_battery_groups))
0264         return -ENODEV;
0265 
0266     return 0;
0267 }
0268 
0269 static int system76_battery_remove(struct power_supply *battery)
0270 {
0271     device_remove_groups(&battery->dev, system76_battery_groups);
0272     return 0;
0273 }
0274 
0275 static struct acpi_battery_hook system76_battery_hook = {
0276     .add_battery = system76_battery_add,
0277     .remove_battery = system76_battery_remove,
0278     .name = "System76 Battery Extension",
0279 };
0280 
0281 static void system76_battery_init(void)
0282 {
0283     battery_hook_register(&system76_battery_hook);
0284 }
0285 
0286 static void system76_battery_exit(void)
0287 {
0288     battery_hook_unregister(&system76_battery_hook);
0289 }
0290 
0291 // Get the airplane mode LED brightness
0292 static enum led_brightness ap_led_get(struct led_classdev *led)
0293 {
0294     struct system76_data *data;
0295     int value;
0296 
0297     data = container_of(led, struct system76_data, ap_led);
0298     value = system76_get(data, "GAPL");
0299     if (value > 0)
0300         return (enum led_brightness)value;
0301     else
0302         return LED_OFF;
0303 }
0304 
0305 // Set the airplane mode LED brightness
0306 static int ap_led_set(struct led_classdev *led, enum led_brightness value)
0307 {
0308     struct system76_data *data;
0309 
0310     data = container_of(led, struct system76_data, ap_led);
0311     return system76_set(data, "SAPL", value == LED_OFF ? 0 : 1);
0312 }
0313 
0314 // Get the last set keyboard LED brightness
0315 static enum led_brightness kb_led_get(struct led_classdev *led)
0316 {
0317     struct system76_data *data;
0318 
0319     data = container_of(led, struct system76_data, kb_led);
0320     return data->kb_brightness;
0321 }
0322 
0323 // Set the keyboard LED brightness
0324 static int kb_led_set(struct led_classdev *led, enum led_brightness value)
0325 {
0326     struct system76_data *data;
0327 
0328     data = container_of(led, struct system76_data, kb_led);
0329     data->kb_brightness = value;
0330     return system76_set(data, "SKBL", (int)data->kb_brightness);
0331 }
0332 
0333 // Get the last set keyboard LED color
0334 static ssize_t kb_led_color_show(
0335     struct device *dev,
0336     struct device_attribute *dev_attr,
0337     char *buf)
0338 {
0339     struct led_classdev *led;
0340     struct system76_data *data;
0341 
0342     led = dev_get_drvdata(dev);
0343     data = container_of(led, struct system76_data, kb_led);
0344     return sysfs_emit(buf, "%06X\n", data->kb_color);
0345 }
0346 
0347 // Set the keyboard LED color
0348 static ssize_t kb_led_color_store(
0349     struct device *dev,
0350     struct device_attribute *dev_attr,
0351     const char *buf,
0352     size_t size)
0353 {
0354     struct led_classdev *led;
0355     struct system76_data *data;
0356     unsigned int val;
0357     int ret;
0358 
0359     led = dev_get_drvdata(dev);
0360     data = container_of(led, struct system76_data, kb_led);
0361     ret = kstrtouint(buf, 16, &val);
0362     if (ret)
0363         return ret;
0364     if (val > 0xFFFFFF)
0365         return -EINVAL;
0366     data->kb_color = (int)val;
0367     system76_set(data, "SKBC", data->kb_color);
0368 
0369     return size;
0370 }
0371 
0372 static struct device_attribute dev_attr_kb_led_color = {
0373     .attr = {
0374         .name = "color",
0375         .mode = 0644,
0376     },
0377     .show = kb_led_color_show,
0378     .store = kb_led_color_store,
0379 };
0380 
0381 static struct attribute *system76_kb_led_color_attrs[] = {
0382     &dev_attr_kb_led_color.attr,
0383     NULL,
0384 };
0385 
0386 ATTRIBUTE_GROUPS(system76_kb_led_color);
0387 
0388 // Notify that the keyboard LED was changed by hardware
0389 static void kb_led_notify(struct system76_data *data)
0390 {
0391     led_classdev_notify_brightness_hw_changed(
0392         &data->kb_led,
0393         data->kb_brightness
0394     );
0395 }
0396 
0397 // Read keyboard LED brightness as set by hardware
0398 static void kb_led_hotkey_hardware(struct system76_data *data)
0399 {
0400     int value;
0401 
0402     value = system76_get(data, "GKBL");
0403     if (value < 0)
0404         return;
0405     data->kb_brightness = value;
0406     kb_led_notify(data);
0407 }
0408 
0409 // Toggle the keyboard LED
0410 static void kb_led_hotkey_toggle(struct system76_data *data)
0411 {
0412     if (data->kb_brightness > 0) {
0413         data->kb_toggle_brightness = data->kb_brightness;
0414         kb_led_set(&data->kb_led, 0);
0415     } else {
0416         kb_led_set(&data->kb_led, data->kb_toggle_brightness);
0417     }
0418     kb_led_notify(data);
0419 }
0420 
0421 // Decrease the keyboard LED brightness
0422 static void kb_led_hotkey_down(struct system76_data *data)
0423 {
0424     int i;
0425 
0426     if (data->kb_brightness > 0) {
0427         for (i = ARRAY_SIZE(kb_levels); i > 0; i--) {
0428             if (kb_levels[i - 1] < data->kb_brightness) {
0429                 kb_led_set(&data->kb_led, kb_levels[i - 1]);
0430                 break;
0431             }
0432         }
0433     } else {
0434         kb_led_set(&data->kb_led, data->kb_toggle_brightness);
0435     }
0436     kb_led_notify(data);
0437 }
0438 
0439 // Increase the keyboard LED brightness
0440 static void kb_led_hotkey_up(struct system76_data *data)
0441 {
0442     int i;
0443 
0444     if (data->kb_brightness > 0) {
0445         for (i = 0; i < ARRAY_SIZE(kb_levels); i++) {
0446             if (kb_levels[i] > data->kb_brightness) {
0447                 kb_led_set(&data->kb_led, kb_levels[i]);
0448                 break;
0449             }
0450         }
0451     } else {
0452         kb_led_set(&data->kb_led, data->kb_toggle_brightness);
0453     }
0454     kb_led_notify(data);
0455 }
0456 
0457 // Cycle the keyboard LED color
0458 static void kb_led_hotkey_color(struct system76_data *data)
0459 {
0460     int i;
0461 
0462     if (data->kb_color < 0)
0463         return;
0464     if (data->kb_brightness > 0) {
0465         for (i = 0; i < ARRAY_SIZE(kb_colors); i++) {
0466             if (kb_colors[i] == data->kb_color)
0467                 break;
0468         }
0469         i += 1;
0470         if (i >= ARRAY_SIZE(kb_colors))
0471             i = 0;
0472         data->kb_color = kb_colors[i];
0473         system76_set(data, "SKBC", data->kb_color);
0474     } else {
0475         kb_led_set(&data->kb_led, data->kb_toggle_brightness);
0476     }
0477     kb_led_notify(data);
0478 }
0479 
0480 static umode_t thermal_is_visible(const void *drvdata, enum hwmon_sensor_types type,
0481                   u32 attr, int channel)
0482 {
0483     const struct system76_data *data = drvdata;
0484 
0485     switch (type) {
0486     case hwmon_fan:
0487     case hwmon_pwm:
0488         if (system76_name(data->nfan, channel))
0489             return 0444;
0490         break;
0491 
0492     case hwmon_temp:
0493         if (system76_name(data->ntmp, channel))
0494             return 0444;
0495         break;
0496 
0497     default:
0498         return 0;
0499     }
0500 
0501     return 0;
0502 }
0503 
0504 static int thermal_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
0505             int channel, long *val)
0506 {
0507     struct system76_data *data = dev_get_drvdata(dev);
0508     int raw;
0509 
0510     switch (type) {
0511     case hwmon_fan:
0512         if (attr == hwmon_fan_input) {
0513             raw = system76_get_index(data, "GFAN", channel);
0514             if (raw < 0)
0515                 return raw;
0516             *val = (raw >> 8) & 0xFFFF;
0517             return 0;
0518         }
0519         break;
0520 
0521     case hwmon_pwm:
0522         if (attr == hwmon_pwm_input) {
0523             raw = system76_get_index(data, "GFAN", channel);
0524             if (raw < 0)
0525                 return raw;
0526             *val = raw & 0xFF;
0527             return 0;
0528         }
0529         break;
0530 
0531     case hwmon_temp:
0532         if (attr == hwmon_temp_input) {
0533             raw = system76_get_index(data, "GTMP", channel);
0534             if (raw < 0)
0535                 return raw;
0536             *val = raw * 1000;
0537             return 0;
0538         }
0539         break;
0540 
0541     default:
0542         return -EOPNOTSUPP;
0543     }
0544 
0545     return -EOPNOTSUPP;
0546 }
0547 
0548 static int thermal_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr,
0549                    int channel, const char **str)
0550 {
0551     struct system76_data *data = dev_get_drvdata(dev);
0552 
0553     switch (type) {
0554     case hwmon_fan:
0555         if (attr == hwmon_fan_label) {
0556             *str = system76_name(data->nfan, channel);
0557             if (*str)
0558                 return 0;
0559         }
0560         break;
0561 
0562     case hwmon_temp:
0563         if (attr == hwmon_temp_label) {
0564             *str = system76_name(data->ntmp, channel);
0565             if (*str)
0566                 return 0;
0567         }
0568         break;
0569 
0570     default:
0571         return -EOPNOTSUPP;
0572     }
0573 
0574     return -EOPNOTSUPP;
0575 }
0576 
0577 static const struct hwmon_ops thermal_ops = {
0578     .is_visible = thermal_is_visible,
0579     .read = thermal_read,
0580     .read_string = thermal_read_string,
0581 };
0582 
0583 // Allocate up to 8 fans and temperatures
0584 static const struct hwmon_channel_info *thermal_channel_info[] = {
0585     HWMON_CHANNEL_INFO(fan,
0586         HWMON_F_INPUT | HWMON_F_LABEL,
0587         HWMON_F_INPUT | HWMON_F_LABEL,
0588         HWMON_F_INPUT | HWMON_F_LABEL,
0589         HWMON_F_INPUT | HWMON_F_LABEL,
0590         HWMON_F_INPUT | HWMON_F_LABEL,
0591         HWMON_F_INPUT | HWMON_F_LABEL,
0592         HWMON_F_INPUT | HWMON_F_LABEL,
0593         HWMON_F_INPUT | HWMON_F_LABEL),
0594     HWMON_CHANNEL_INFO(pwm,
0595         HWMON_PWM_INPUT,
0596         HWMON_PWM_INPUT,
0597         HWMON_PWM_INPUT,
0598         HWMON_PWM_INPUT,
0599         HWMON_PWM_INPUT,
0600         HWMON_PWM_INPUT,
0601         HWMON_PWM_INPUT,
0602         HWMON_PWM_INPUT),
0603     HWMON_CHANNEL_INFO(temp,
0604         HWMON_T_INPUT | HWMON_T_LABEL,
0605         HWMON_T_INPUT | HWMON_T_LABEL,
0606         HWMON_T_INPUT | HWMON_T_LABEL,
0607         HWMON_T_INPUT | HWMON_T_LABEL,
0608         HWMON_T_INPUT | HWMON_T_LABEL,
0609         HWMON_T_INPUT | HWMON_T_LABEL,
0610         HWMON_T_INPUT | HWMON_T_LABEL,
0611         HWMON_T_INPUT | HWMON_T_LABEL),
0612     NULL
0613 };
0614 
0615 static const struct hwmon_chip_info thermal_chip_info = {
0616     .ops = &thermal_ops,
0617     .info = thermal_channel_info,
0618 };
0619 
0620 static void input_key(struct system76_data *data, unsigned int code)
0621 {
0622     input_report_key(data->input, code, 1);
0623     input_sync(data->input);
0624 
0625     input_report_key(data->input, code, 0);
0626     input_sync(data->input);
0627 }
0628 
0629 // Handle ACPI notification
0630 static void system76_notify(struct acpi_device *acpi_dev, u32 event)
0631 {
0632     struct system76_data *data;
0633 
0634     data = acpi_driver_data(acpi_dev);
0635     switch (event) {
0636     case 0x80:
0637         kb_led_hotkey_hardware(data);
0638         break;
0639     case 0x81:
0640         kb_led_hotkey_toggle(data);
0641         break;
0642     case 0x82:
0643         kb_led_hotkey_down(data);
0644         break;
0645     case 0x83:
0646         kb_led_hotkey_up(data);
0647         break;
0648     case 0x84:
0649         kb_led_hotkey_color(data);
0650         break;
0651     case 0x85:
0652         input_key(data, KEY_SCREENLOCK);
0653         break;
0654     }
0655 }
0656 
0657 // Add a System76 ACPI device
0658 static int system76_add(struct acpi_device *acpi_dev)
0659 {
0660     struct system76_data *data;
0661     int err;
0662 
0663     data = devm_kzalloc(&acpi_dev->dev, sizeof(*data), GFP_KERNEL);
0664     if (!data)
0665         return -ENOMEM;
0666     acpi_dev->driver_data = data;
0667     data->acpi_dev = acpi_dev;
0668 
0669     // Some models do not run open EC firmware. Check for an ACPI method
0670     // that only exists on open EC to guard functionality specific to it.
0671     data->has_open_ec = acpi_has_method(acpi_device_handle(data->acpi_dev), "NFAN");
0672 
0673     err = system76_get(data, "INIT");
0674     if (err)
0675         return err;
0676     data->ap_led.name = "system76_acpi::airplane";
0677     data->ap_led.flags = LED_CORE_SUSPENDRESUME;
0678     data->ap_led.brightness_get = ap_led_get;
0679     data->ap_led.brightness_set_blocking = ap_led_set;
0680     data->ap_led.max_brightness = 1;
0681     data->ap_led.default_trigger = "rfkill-none";
0682     err = devm_led_classdev_register(&acpi_dev->dev, &data->ap_led);
0683     if (err)
0684         return err;
0685 
0686     data->kb_led.name = "system76_acpi::kbd_backlight";
0687     data->kb_led.flags = LED_BRIGHT_HW_CHANGED | LED_CORE_SUSPENDRESUME;
0688     data->kb_led.brightness_get = kb_led_get;
0689     data->kb_led.brightness_set_blocking = kb_led_set;
0690     if (acpi_has_method(acpi_device_handle(data->acpi_dev), "SKBC")) {
0691         data->kb_led.max_brightness = 255;
0692         data->kb_led.groups = system76_kb_led_color_groups;
0693         data->kb_toggle_brightness = 72;
0694         data->kb_color = 0xffffff;
0695         system76_set(data, "SKBC", data->kb_color);
0696     } else {
0697         data->kb_led.max_brightness = 5;
0698         data->kb_color = -1;
0699     }
0700     err = devm_led_classdev_register(&acpi_dev->dev, &data->kb_led);
0701     if (err)
0702         return err;
0703 
0704     data->input = devm_input_allocate_device(&acpi_dev->dev);
0705     if (!data->input)
0706         return -ENOMEM;
0707 
0708     data->input->name = "System76 ACPI Hotkeys";
0709     data->input->phys = "system76_acpi/input0";
0710     data->input->id.bustype = BUS_HOST;
0711     data->input->dev.parent = &acpi_dev->dev;
0712     input_set_capability(data->input, EV_KEY, KEY_SCREENLOCK);
0713 
0714     err = input_register_device(data->input);
0715     if (err)
0716         goto error;
0717 
0718     if (data->has_open_ec) {
0719         err = system76_get_object(data, "NFAN", &data->nfan);
0720         if (err)
0721             goto error;
0722 
0723         err = system76_get_object(data, "NTMP", &data->ntmp);
0724         if (err)
0725             goto error;
0726 
0727         data->therm = devm_hwmon_device_register_with_info(&acpi_dev->dev,
0728             "system76_acpi", data, &thermal_chip_info, NULL);
0729         err = PTR_ERR_OR_ZERO(data->therm);
0730         if (err)
0731             goto error;
0732 
0733         system76_battery_init();
0734     }
0735 
0736     return 0;
0737 
0738 error:
0739     if (data->has_open_ec) {
0740         kfree(data->ntmp);
0741         kfree(data->nfan);
0742     }
0743     return err;
0744 }
0745 
0746 // Remove a System76 ACPI device
0747 static int system76_remove(struct acpi_device *acpi_dev)
0748 {
0749     struct system76_data *data;
0750 
0751     data = acpi_driver_data(acpi_dev);
0752 
0753     if (data->has_open_ec) {
0754         system76_battery_exit();
0755         kfree(data->nfan);
0756         kfree(data->ntmp);
0757     }
0758 
0759     devm_led_classdev_unregister(&acpi_dev->dev, &data->ap_led);
0760     devm_led_classdev_unregister(&acpi_dev->dev, &data->kb_led);
0761 
0762     system76_get(data, "FINI");
0763 
0764     return 0;
0765 }
0766 
0767 static struct acpi_driver system76_driver = {
0768     .name = "System76 ACPI Driver",
0769     .class = "hotkey",
0770     .ids = device_ids,
0771     .ops = {
0772         .add = system76_add,
0773         .remove = system76_remove,
0774         .notify = system76_notify,
0775     },
0776 };
0777 module_acpi_driver(system76_driver);
0778 
0779 MODULE_DESCRIPTION("System76 ACPI Driver");
0780 MODULE_AUTHOR("Jeremy Soller <jeremy@system76.com>");
0781 MODULE_LICENSE("GPL");