Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * acpi_fan.c - ACPI Fan Driver ($Revision: 29 $) |
| 4 | * |
| 5 | * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> |
| 6 | * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/types.h> |
Sudip Mukherjee | 88989fd | 2014-08-28 19:17:19 +0530 | [diff] [blame] | 13 | #include <linux/uaccess.h> |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 14 | #include <linux/thermal.h> |
Lv Zheng | 8b48463 | 2013-12-03 08:49:16 +0800 | [diff] [blame] | 15 | #include <linux/acpi.h> |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame] | 16 | #include <linux/platform_device.h> |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 17 | #include <linux/sort.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Rafael J. Wysocki | b9370dc | 2021-05-14 21:08:51 +0200 | [diff] [blame] | 19 | #include "fan.h" |
| 20 | |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 21 | MODULE_AUTHOR("Paul Diefenbaugh"); |
Len Brown | 7cda93e | 2007-02-12 23:50:02 -0500 | [diff] [blame] | 22 | MODULE_DESCRIPTION("ACPI Fan Driver"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | MODULE_LICENSE("GPL"); |
| 24 | |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame] | 25 | static int acpi_fan_probe(struct platform_device *pdev); |
| 26 | static int acpi_fan_remove(struct platform_device *pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 28 | static const struct acpi_device_id fan_device_ids[] = { |
Rafael J. Wysocki | b9370dc | 2021-05-14 21:08:51 +0200 | [diff] [blame] | 29 | ACPI_FAN_DEVICE_IDS, |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 30 | {"", 0}, |
| 31 | }; |
| 32 | MODULE_DEVICE_TABLE(acpi, fan_device_ids); |
| 33 | |
Rafael J. Wysocki | 9069240 | 2012-08-09 23:00:02 +0200 | [diff] [blame] | 34 | #ifdef CONFIG_PM_SLEEP |
Rafael J. Wysocki | 62fcbdd | 2012-06-27 23:26:07 +0200 | [diff] [blame] | 35 | static int acpi_fan_suspend(struct device *dev); |
| 36 | static int acpi_fan_resume(struct device *dev); |
Kaiyen Chang | 1d75158 | 2016-02-10 15:23:27 -0800 | [diff] [blame] | 37 | static const struct dev_pm_ops acpi_fan_pm = { |
Aaron Lu | b9b8515 | 2014-02-19 12:16:48 +0800 | [diff] [blame] | 38 | .resume = acpi_fan_resume, |
| 39 | .freeze = acpi_fan_suspend, |
| 40 | .thaw = acpi_fan_resume, |
| 41 | .restore = acpi_fan_resume, |
| 42 | }; |
| 43 | #define FAN_PM_OPS_PTR (&acpi_fan_pm) |
Shuah Khan | b108e0e | 2014-02-12 20:19:08 -0700 | [diff] [blame] | 44 | #else |
Aaron Lu | b9b8515 | 2014-02-19 12:16:48 +0800 | [diff] [blame] | 45 | #define FAN_PM_OPS_PTR NULL |
Rafael J. Wysocki | 9069240 | 2012-08-09 23:00:02 +0200 | [diff] [blame] | 46 | #endif |
Rafael J. Wysocki | 62fcbdd | 2012-06-27 23:26:07 +0200 | [diff] [blame] | 47 | |
Srinivas Pandruvada | d19e470 | 2019-12-13 15:48:40 -0800 | [diff] [blame] | 48 | #define ACPI_FPS_NAME_LEN 20 |
| 49 | |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 50 | struct acpi_fan_fps { |
| 51 | u64 control; |
| 52 | u64 trip_point; |
| 53 | u64 speed; |
| 54 | u64 noise_level; |
| 55 | u64 power; |
Srinivas Pandruvada | d19e470 | 2019-12-13 15:48:40 -0800 | [diff] [blame] | 56 | char name[ACPI_FPS_NAME_LEN]; |
| 57 | struct device_attribute dev_attr; |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | struct acpi_fan_fif { |
| 61 | u64 revision; |
| 62 | u64 fine_grain_ctrl; |
| 63 | u64 step_size; |
| 64 | u64 low_speed_notification; |
| 65 | }; |
| 66 | |
| 67 | struct acpi_fan { |
| 68 | bool acpi4; |
| 69 | struct acpi_fan_fif fif; |
| 70 | struct acpi_fan_fps *fps; |
| 71 | int fps_count; |
| 72 | struct thermal_cooling_device *cdev; |
| 73 | }; |
| 74 | |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame] | 75 | static struct platform_driver acpi_fan_driver = { |
| 76 | .probe = acpi_fan_probe, |
| 77 | .remove = acpi_fan_remove, |
| 78 | .driver = { |
| 79 | .name = "acpi-fan", |
| 80 | .acpi_match_table = fan_device_ids, |
| 81 | .pm = FAN_PM_OPS_PTR, |
| 82 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 85 | /* thermal cooling device callbacks */ |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 86 | static int fan_get_max_state(struct thermal_cooling_device *cdev, unsigned long |
| 87 | *state) |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 88 | { |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 89 | struct acpi_device *device = cdev->devdata; |
| 90 | struct acpi_fan *fan = acpi_driver_data(device); |
| 91 | |
| 92 | if (fan->acpi4) |
| 93 | *state = fan->fps_count - 1; |
| 94 | else |
| 95 | *state = 1; |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 96 | return 0; |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 97 | } |
| 98 | |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 99 | static int fan_get_state_acpi4(struct acpi_device *device, unsigned long *state) |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 100 | { |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 101 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 102 | struct acpi_fan *fan = acpi_driver_data(device); |
| 103 | union acpi_object *obj; |
| 104 | acpi_status status; |
| 105 | int control, i; |
| 106 | |
| 107 | status = acpi_evaluate_object(device->handle, "_FST", NULL, &buffer); |
| 108 | if (ACPI_FAILURE(status)) { |
| 109 | dev_err(&device->dev, "Get fan state failed\n"); |
| 110 | return status; |
| 111 | } |
| 112 | |
| 113 | obj = buffer.pointer; |
| 114 | if (!obj || obj->type != ACPI_TYPE_PACKAGE || |
| 115 | obj->package.count != 3 || |
| 116 | obj->package.elements[1].type != ACPI_TYPE_INTEGER) { |
| 117 | dev_err(&device->dev, "Invalid _FST data\n"); |
| 118 | status = -EINVAL; |
| 119 | goto err; |
| 120 | } |
| 121 | |
| 122 | control = obj->package.elements[1].integer.value; |
| 123 | for (i = 0; i < fan->fps_count; i++) { |
Srinivas Pandruvada | 84baf17 | 2016-10-04 13:51:40 -0700 | [diff] [blame] | 124 | /* |
| 125 | * When Fine Grain Control is set, return the state |
| 126 | * corresponding to maximum fan->fps[i].control |
| 127 | * value compared to the current speed. Here the |
| 128 | * fan->fps[] is sorted array with increasing speed. |
| 129 | */ |
| 130 | if (fan->fif.fine_grain_ctrl && control < fan->fps[i].control) { |
| 131 | i = (i > 0) ? i - 1 : 0; |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 132 | break; |
Srinivas Pandruvada | 84baf17 | 2016-10-04 13:51:40 -0700 | [diff] [blame] | 133 | } else if (control == fan->fps[i].control) { |
| 134 | break; |
| 135 | } |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 136 | } |
| 137 | if (i == fan->fps_count) { |
| 138 | dev_dbg(&device->dev, "Invalid control value returned\n"); |
| 139 | status = -EINVAL; |
| 140 | goto err; |
| 141 | } |
| 142 | |
| 143 | *state = i; |
| 144 | |
| 145 | err: |
| 146 | kfree(obj); |
| 147 | return status; |
| 148 | } |
| 149 | |
| 150 | static int fan_get_state(struct acpi_device *device, unsigned long *state) |
| 151 | { |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 152 | int result; |
Naresh Bhat | 85eb982 | 2013-07-01 19:51:00 +0530 | [diff] [blame] | 153 | int acpi_state = ACPI_STATE_D0; |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 154 | |
Aaron Lu | 2bb3a2b | 2013-11-19 15:43:52 +0800 | [diff] [blame] | 155 | result = acpi_device_update_power(device, &acpi_state); |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 156 | if (result) |
| 157 | return result; |
| 158 | |
Rafael J. Wysocki | 20dacb7 | 2015-05-16 01:55:35 +0200 | [diff] [blame] | 159 | *state = acpi_state == ACPI_STATE_D3_COLD |
| 160 | || acpi_state == ACPI_STATE_D3_HOT ? |
| 161 | 0 : (acpi_state == ACPI_STATE_D0 ? 1 : -1); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 162 | return 0; |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 163 | } |
| 164 | |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 165 | static int fan_get_cur_state(struct thermal_cooling_device *cdev, unsigned long |
| 166 | *state) |
| 167 | { |
| 168 | struct acpi_device *device = cdev->devdata; |
| 169 | struct acpi_fan *fan = acpi_driver_data(device); |
| 170 | |
| 171 | if (fan->acpi4) |
| 172 | return fan_get_state_acpi4(device, state); |
| 173 | else |
| 174 | return fan_get_state(device, state); |
| 175 | } |
| 176 | |
| 177 | static int fan_set_state(struct acpi_device *device, unsigned long state) |
| 178 | { |
| 179 | if (state != 0 && state != 1) |
| 180 | return -EINVAL; |
| 181 | |
| 182 | return acpi_device_set_power(device, |
| 183 | state ? ACPI_STATE_D0 : ACPI_STATE_D3_COLD); |
| 184 | } |
| 185 | |
| 186 | static int fan_set_state_acpi4(struct acpi_device *device, unsigned long state) |
| 187 | { |
| 188 | struct acpi_fan *fan = acpi_driver_data(device); |
| 189 | acpi_status status; |
| 190 | |
| 191 | if (state >= fan->fps_count) |
| 192 | return -EINVAL; |
| 193 | |
| 194 | status = acpi_execute_simple_method(device->handle, "_FSL", |
| 195 | fan->fps[state].control); |
| 196 | if (ACPI_FAILURE(status)) { |
| 197 | dev_dbg(&device->dev, "Failed to set state by _FSL\n"); |
| 198 | return status; |
| 199 | } |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 204 | static int |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 205 | fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state) |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 206 | { |
| 207 | struct acpi_device *device = cdev->devdata; |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 208 | struct acpi_fan *fan = acpi_driver_data(device); |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 209 | |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 210 | if (fan->acpi4) |
| 211 | return fan_set_state_acpi4(device, state); |
| 212 | else |
| 213 | return fan_set_state(device, state); |
Joe Perches | 447a564 | 2018-03-21 15:09:32 -0700 | [diff] [blame] | 214 | } |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 215 | |
Vasiliy Kulikov | 9c8b04b | 2011-06-25 21:07:52 +0400 | [diff] [blame] | 216 | static const struct thermal_cooling_device_ops fan_cooling_ops = { |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 217 | .get_max_state = fan_get_max_state, |
| 218 | .get_cur_state = fan_get_cur_state, |
| 219 | .set_cur_state = fan_set_cur_state, |
| 220 | }; |
| 221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | /* -------------------------------------------------------------------------- |
Sudip Mukherjee | 88989fd | 2014-08-28 19:17:19 +0530 | [diff] [blame] | 223 | * Driver Interface |
| 224 | * -------------------------------------------------------------------------- |
| 225 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 227 | static bool acpi_fan_is_acpi4(struct acpi_device *device) |
| 228 | { |
| 229 | return acpi_has_method(device->handle, "_FIF") && |
| 230 | acpi_has_method(device->handle, "_FPS") && |
| 231 | acpi_has_method(device->handle, "_FSL") && |
| 232 | acpi_has_method(device->handle, "_FST"); |
| 233 | } |
| 234 | |
| 235 | static int acpi_fan_get_fif(struct acpi_device *device) |
| 236 | { |
| 237 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 238 | struct acpi_fan *fan = acpi_driver_data(device); |
| 239 | struct acpi_buffer format = { sizeof("NNNN"), "NNNN" }; |
| 240 | struct acpi_buffer fif = { sizeof(fan->fif), &fan->fif }; |
| 241 | union acpi_object *obj; |
| 242 | acpi_status status; |
| 243 | |
| 244 | status = acpi_evaluate_object(device->handle, "_FIF", NULL, &buffer); |
| 245 | if (ACPI_FAILURE(status)) |
| 246 | return status; |
| 247 | |
| 248 | obj = buffer.pointer; |
| 249 | if (!obj || obj->type != ACPI_TYPE_PACKAGE) { |
| 250 | dev_err(&device->dev, "Invalid _FIF data\n"); |
| 251 | status = -EINVAL; |
| 252 | goto err; |
| 253 | } |
| 254 | |
| 255 | status = acpi_extract_package(obj, &format, &fif); |
| 256 | if (ACPI_FAILURE(status)) { |
| 257 | dev_err(&device->dev, "Invalid _FIF element\n"); |
| 258 | status = -EINVAL; |
| 259 | } |
| 260 | |
| 261 | err: |
| 262 | kfree(obj); |
| 263 | return status; |
| 264 | } |
| 265 | |
| 266 | static int acpi_fan_speed_cmp(const void *a, const void *b) |
| 267 | { |
| 268 | const struct acpi_fan_fps *fps1 = a; |
| 269 | const struct acpi_fan_fps *fps2 = b; |
| 270 | return fps1->speed - fps2->speed; |
| 271 | } |
| 272 | |
Srinivas Pandruvada | d19e470 | 2019-12-13 15:48:40 -0800 | [diff] [blame] | 273 | static ssize_t show_state(struct device *dev, struct device_attribute *attr, char *buf) |
| 274 | { |
| 275 | struct acpi_fan_fps *fps = container_of(attr, struct acpi_fan_fps, dev_attr); |
| 276 | int count; |
| 277 | |
| 278 | if (fps->control == 0xFFFFFFFF || fps->control > 100) |
Takashi Iwai | 949fe25 | 2020-03-11 08:08:51 +0100 | [diff] [blame] | 279 | count = scnprintf(buf, PAGE_SIZE, "not-defined:"); |
Srinivas Pandruvada | d19e470 | 2019-12-13 15:48:40 -0800 | [diff] [blame] | 280 | else |
Takashi Iwai | 949fe25 | 2020-03-11 08:08:51 +0100 | [diff] [blame] | 281 | count = scnprintf(buf, PAGE_SIZE, "%lld:", fps->control); |
Srinivas Pandruvada | d19e470 | 2019-12-13 15:48:40 -0800 | [diff] [blame] | 282 | |
| 283 | if (fps->trip_point == 0xFFFFFFFF || fps->trip_point > 9) |
Takashi Iwai | 949fe25 | 2020-03-11 08:08:51 +0100 | [diff] [blame] | 284 | count += scnprintf(&buf[count], PAGE_SIZE - count, "not-defined:"); |
Srinivas Pandruvada | d19e470 | 2019-12-13 15:48:40 -0800 | [diff] [blame] | 285 | else |
Takashi Iwai | 949fe25 | 2020-03-11 08:08:51 +0100 | [diff] [blame] | 286 | count += scnprintf(&buf[count], PAGE_SIZE - count, "%lld:", fps->trip_point); |
Srinivas Pandruvada | d19e470 | 2019-12-13 15:48:40 -0800 | [diff] [blame] | 287 | |
| 288 | if (fps->speed == 0xFFFFFFFF) |
Takashi Iwai | 949fe25 | 2020-03-11 08:08:51 +0100 | [diff] [blame] | 289 | count += scnprintf(&buf[count], PAGE_SIZE - count, "not-defined:"); |
Srinivas Pandruvada | d19e470 | 2019-12-13 15:48:40 -0800 | [diff] [blame] | 290 | else |
Takashi Iwai | 949fe25 | 2020-03-11 08:08:51 +0100 | [diff] [blame] | 291 | count += scnprintf(&buf[count], PAGE_SIZE - count, "%lld:", fps->speed); |
Srinivas Pandruvada | d19e470 | 2019-12-13 15:48:40 -0800 | [diff] [blame] | 292 | |
| 293 | if (fps->noise_level == 0xFFFFFFFF) |
Takashi Iwai | 949fe25 | 2020-03-11 08:08:51 +0100 | [diff] [blame] | 294 | count += scnprintf(&buf[count], PAGE_SIZE - count, "not-defined:"); |
Srinivas Pandruvada | d19e470 | 2019-12-13 15:48:40 -0800 | [diff] [blame] | 295 | else |
Takashi Iwai | 949fe25 | 2020-03-11 08:08:51 +0100 | [diff] [blame] | 296 | count += scnprintf(&buf[count], PAGE_SIZE - count, "%lld:", fps->noise_level * 100); |
Srinivas Pandruvada | d19e470 | 2019-12-13 15:48:40 -0800 | [diff] [blame] | 297 | |
| 298 | if (fps->power == 0xFFFFFFFF) |
Takashi Iwai | 949fe25 | 2020-03-11 08:08:51 +0100 | [diff] [blame] | 299 | count += scnprintf(&buf[count], PAGE_SIZE - count, "not-defined\n"); |
Srinivas Pandruvada | d19e470 | 2019-12-13 15:48:40 -0800 | [diff] [blame] | 300 | else |
Takashi Iwai | 949fe25 | 2020-03-11 08:08:51 +0100 | [diff] [blame] | 301 | count += scnprintf(&buf[count], PAGE_SIZE - count, "%lld\n", fps->power); |
Srinivas Pandruvada | d19e470 | 2019-12-13 15:48:40 -0800 | [diff] [blame] | 302 | |
| 303 | return count; |
| 304 | } |
| 305 | |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 306 | static int acpi_fan_get_fps(struct acpi_device *device) |
| 307 | { |
| 308 | struct acpi_fan *fan = acpi_driver_data(device); |
| 309 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 310 | union acpi_object *obj; |
| 311 | acpi_status status; |
| 312 | int i; |
| 313 | |
| 314 | status = acpi_evaluate_object(device->handle, "_FPS", NULL, &buffer); |
| 315 | if (ACPI_FAILURE(status)) |
| 316 | return status; |
| 317 | |
| 318 | obj = buffer.pointer; |
| 319 | if (!obj || obj->type != ACPI_TYPE_PACKAGE || obj->package.count < 2) { |
| 320 | dev_err(&device->dev, "Invalid _FPS data\n"); |
| 321 | status = -EINVAL; |
| 322 | goto err; |
| 323 | } |
| 324 | |
| 325 | fan->fps_count = obj->package.count - 1; /* minus revision field */ |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 326 | fan->fps = devm_kcalloc(&device->dev, |
| 327 | fan->fps_count, sizeof(struct acpi_fan_fps), |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 328 | GFP_KERNEL); |
| 329 | if (!fan->fps) { |
| 330 | dev_err(&device->dev, "Not enough memory\n"); |
| 331 | status = -ENOMEM; |
| 332 | goto err; |
| 333 | } |
| 334 | for (i = 0; i < fan->fps_count; i++) { |
| 335 | struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" }; |
Srinivas Pandruvada | d19e470 | 2019-12-13 15:48:40 -0800 | [diff] [blame] | 336 | struct acpi_buffer fps = { offsetof(struct acpi_fan_fps, name), |
| 337 | &fan->fps[i] }; |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 338 | status = acpi_extract_package(&obj->package.elements[i + 1], |
| 339 | &format, &fps); |
| 340 | if (ACPI_FAILURE(status)) { |
| 341 | dev_err(&device->dev, "Invalid _FPS element\n"); |
Srinivas Pandruvada | d19e470 | 2019-12-13 15:48:40 -0800 | [diff] [blame] | 342 | goto err; |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
| 346 | /* sort the state array according to fan speed in increase order */ |
| 347 | sort(fan->fps, fan->fps_count, sizeof(*fan->fps), |
| 348 | acpi_fan_speed_cmp, NULL); |
| 349 | |
Srinivas Pandruvada | d19e470 | 2019-12-13 15:48:40 -0800 | [diff] [blame] | 350 | for (i = 0; i < fan->fps_count; ++i) { |
| 351 | struct acpi_fan_fps *fps = &fan->fps[i]; |
| 352 | |
| 353 | snprintf(fps->name, ACPI_FPS_NAME_LEN, "state%d", i); |
Guenter Roeck | 7dc7a8b | 2020-11-10 15:52:01 -0800 | [diff] [blame] | 354 | sysfs_attr_init(&fps->dev_attr.attr); |
Srinivas Pandruvada | d19e470 | 2019-12-13 15:48:40 -0800 | [diff] [blame] | 355 | fps->dev_attr.show = show_state; |
| 356 | fps->dev_attr.store = NULL; |
| 357 | fps->dev_attr.attr.name = fps->name; |
| 358 | fps->dev_attr.attr.mode = 0444; |
| 359 | status = sysfs_create_file(&device->dev.kobj, &fps->dev_attr.attr); |
| 360 | if (status) { |
| 361 | int j; |
| 362 | |
| 363 | for (j = 0; j < i; ++j) |
| 364 | sysfs_remove_file(&device->dev.kobj, &fan->fps[j].dev_attr.attr); |
| 365 | break; |
| 366 | } |
| 367 | } |
| 368 | |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 369 | err: |
| 370 | kfree(obj); |
| 371 | return status; |
| 372 | } |
| 373 | |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame] | 374 | static int acpi_fan_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 376 | int result = 0; |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 377 | struct thermal_cooling_device *cdev; |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 378 | struct acpi_fan *fan; |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame] | 379 | struct acpi_device *device = ACPI_COMPANION(&pdev->dev); |
Srinivas Pandruvada | bbb16fe | 2014-12-09 16:15:40 -0800 | [diff] [blame] | 380 | char *name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 382 | fan = devm_kzalloc(&pdev->dev, sizeof(*fan), GFP_KERNEL); |
| 383 | if (!fan) { |
| 384 | dev_err(&device->dev, "No memory for fan\n"); |
| 385 | return -ENOMEM; |
| 386 | } |
| 387 | device->driver_data = fan; |
| 388 | platform_set_drvdata(pdev, fan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 390 | if (acpi_fan_is_acpi4(device)) { |
Srinivas Pandruvada | d19e470 | 2019-12-13 15:48:40 -0800 | [diff] [blame] | 391 | result = acpi_fan_get_fif(device); |
| 392 | if (result) |
| 393 | return result; |
| 394 | |
| 395 | result = acpi_fan_get_fps(device); |
| 396 | if (result) |
| 397 | return result; |
| 398 | |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 399 | fan->acpi4 = true; |
| 400 | } else { |
| 401 | result = acpi_device_update_power(device, NULL); |
| 402 | if (result) { |
Andy Lutomirski | f972799 | 2016-01-13 14:25:19 -0800 | [diff] [blame] | 403 | dev_err(&device->dev, "Failed to set initial power state\n"); |
Srinivas Pandruvada | d19e470 | 2019-12-13 15:48:40 -0800 | [diff] [blame] | 404 | goto err_end; |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 405 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Srinivas Pandruvada | bbb16fe | 2014-12-09 16:15:40 -0800 | [diff] [blame] | 408 | if (!strncmp(pdev->name, "PNP0C0B", strlen("PNP0C0B"))) |
| 409 | name = "Fan"; |
| 410 | else |
| 411 | name = acpi_device_bid(device); |
| 412 | |
| 413 | cdev = thermal_cooling_device_register(name, device, |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 414 | &fan_cooling_ops); |
Thomas Sujith | 19b3678 | 2008-02-15 01:01:52 -0500 | [diff] [blame] | 415 | if (IS_ERR(cdev)) { |
| 416 | result = PTR_ERR(cdev); |
Srinivas Pandruvada | d19e470 | 2019-12-13 15:48:40 -0800 | [diff] [blame] | 417 | goto err_end; |
Thomas Sujith | 19b3678 | 2008-02-15 01:01:52 -0500 | [diff] [blame] | 418 | } |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 419 | |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame] | 420 | dev_dbg(&pdev->dev, "registered as cooling_device%d\n", cdev->id); |
Thomas Sujith | 19b3678 | 2008-02-15 01:01:52 -0500 | [diff] [blame] | 421 | |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 422 | fan->cdev = cdev; |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame] | 423 | result = sysfs_create_link(&pdev->dev.kobj, |
Julia Lawall | 9030062 | 2008-04-11 10:09:24 +0800 | [diff] [blame] | 424 | &cdev->device.kobj, |
| 425 | "thermal_cooling"); |
| 426 | if (result) |
Linus Torvalds | 8264fce6 | 2014-10-24 11:21:43 -0700 | [diff] [blame] | 427 | dev_err(&pdev->dev, "Failed to create sysfs link 'thermal_cooling'\n"); |
Julia Lawall | 9030062 | 2008-04-11 10:09:24 +0800 | [diff] [blame] | 428 | |
| 429 | result = sysfs_create_link(&cdev->device.kobj, |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame] | 430 | &pdev->dev.kobj, |
Julia Lawall | 9030062 | 2008-04-11 10:09:24 +0800 | [diff] [blame] | 431 | "device"); |
Srinivas Pandruvada | d19e470 | 2019-12-13 15:48:40 -0800 | [diff] [blame] | 432 | if (result) { |
Linus Torvalds | 8264fce6 | 2014-10-24 11:21:43 -0700 | [diff] [blame] | 433 | dev_err(&pdev->dev, "Failed to create sysfs link 'device'\n"); |
Srinivas Pandruvada | d19e470 | 2019-12-13 15:48:40 -0800 | [diff] [blame] | 434 | goto err_end; |
| 435 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
Srinivas Pandruvada | d19e470 | 2019-12-13 15:48:40 -0800 | [diff] [blame] | 437 | return 0; |
| 438 | |
| 439 | err_end: |
| 440 | if (fan->acpi4) { |
| 441 | int i; |
| 442 | |
| 443 | for (i = 0; i < fan->fps_count; ++i) |
| 444 | sysfs_remove_file(&device->dev.kobj, &fan->fps[i].dev_attr.attr); |
| 445 | } |
| 446 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 447 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame] | 450 | static int acpi_fan_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | { |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 452 | struct acpi_fan *fan = platform_get_drvdata(pdev); |
Zhang Rui | 05a83d9 | 2008-01-17 15:51:24 +0800 | [diff] [blame] | 453 | |
Srinivas Pandruvada | d19e470 | 2019-12-13 15:48:40 -0800 | [diff] [blame] | 454 | if (fan->acpi4) { |
| 455 | struct acpi_device *device = ACPI_COMPANION(&pdev->dev); |
| 456 | int i; |
| 457 | |
| 458 | for (i = 0; i < fan->fps_count; ++i) |
| 459 | sysfs_remove_file(&device->dev.kobj, &fan->fps[i].dev_attr.attr); |
| 460 | } |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame] | 461 | sysfs_remove_link(&pdev->dev.kobj, "thermal_cooling"); |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 462 | sysfs_remove_link(&fan->cdev->device.kobj, "device"); |
| 463 | thermal_cooling_device_unregister(fan->cdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 465 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Rafael J. Wysocki | 9069240 | 2012-08-09 23:00:02 +0200 | [diff] [blame] | 468 | #ifdef CONFIG_PM_SLEEP |
Rafael J. Wysocki | 62fcbdd | 2012-06-27 23:26:07 +0200 | [diff] [blame] | 469 | static int acpi_fan_suspend(struct device *dev) |
Len Brown | ec68373 | 2008-01-23 22:41:20 -0500 | [diff] [blame] | 470 | { |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 471 | struct acpi_fan *fan = dev_get_drvdata(dev); |
| 472 | if (fan->acpi4) |
| 473 | return 0; |
Len Brown | ec68373 | 2008-01-23 22:41:20 -0500 | [diff] [blame] | 474 | |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame] | 475 | acpi_device_set_power(ACPI_COMPANION(dev), ACPI_STATE_D0); |
Len Brown | ec68373 | 2008-01-23 22:41:20 -0500 | [diff] [blame] | 476 | |
| 477 | return AE_OK; |
| 478 | } |
| 479 | |
Rafael J. Wysocki | 62fcbdd | 2012-06-27 23:26:07 +0200 | [diff] [blame] | 480 | static int acpi_fan_resume(struct device *dev) |
Len Brown | ec68373 | 2008-01-23 22:41:20 -0500 | [diff] [blame] | 481 | { |
Rafael J. Wysocki | 488a76c | 2010-11-25 00:11:24 +0100 | [diff] [blame] | 482 | int result; |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 483 | struct acpi_fan *fan = dev_get_drvdata(dev); |
Len Brown | ec68373 | 2008-01-23 22:41:20 -0500 | [diff] [blame] | 484 | |
Aaron Lu | 9519a63 | 2014-04-01 15:50:27 +0800 | [diff] [blame] | 485 | if (fan->acpi4) |
| 486 | return 0; |
Len Brown | ec68373 | 2008-01-23 22:41:20 -0500 | [diff] [blame] | 487 | |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame] | 488 | result = acpi_device_update_power(ACPI_COMPANION(dev), NULL); |
Rafael J. Wysocki | 488a76c | 2010-11-25 00:11:24 +0100 | [diff] [blame] | 489 | if (result) |
Sudip Mukherjee | 88989fd | 2014-08-28 19:17:19 +0530 | [diff] [blame] | 490 | dev_err(dev, "Error updating fan power state\n"); |
Len Brown | ec68373 | 2008-01-23 22:41:20 -0500 | [diff] [blame] | 491 | |
| 492 | return result; |
| 493 | } |
Rafael J. Wysocki | 9069240 | 2012-08-09 23:00:02 +0200 | [diff] [blame] | 494 | #endif |
Len Brown | ec68373 | 2008-01-23 22:41:20 -0500 | [diff] [blame] | 495 | |
Aaron Lu | 19593a1 | 2013-11-19 16:59:20 +0800 | [diff] [blame] | 496 | module_platform_driver(acpi_fan_driver); |