Srinivas Pandruvada | 925c36b | 2013-12-30 12:55:06 -0800 | [diff] [blame] | 1 | /* |
| 2 | * ACPI INT3403 thermal driver |
| 3 | * Copyright (c) 2013, Intel Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/types.h> |
| 19 | #include <linux/acpi.h> |
| 20 | #include <linux/thermal.h> |
| 21 | |
| 22 | #define INT3403_TYPE_SENSOR 0x03 |
| 23 | #define INT3403_PERF_CHANGED_EVENT 0x80 |
| 24 | #define INT3403_THERMAL_EVENT 0x90 |
| 25 | |
| 26 | #define DECI_KELVIN_TO_MILLI_CELSIUS(t, off) (((t) - (off)) * 100) |
| 27 | #define KELVIN_OFFSET 2732 |
| 28 | #define MILLI_CELSIUS_TO_DECI_KELVIN(t, off) (((t) / 100) + (off)) |
| 29 | |
| 30 | #define ACPI_INT3403_CLASS "int3403" |
| 31 | #define ACPI_INT3403_FILE_STATE "state" |
| 32 | |
| 33 | struct int3403_sensor { |
| 34 | struct thermal_zone_device *tzone; |
| 35 | unsigned long *thresholds; |
Srinivas Pandruvada | 5fcdeb2 | 2014-05-30 12:10:32 -0700 | [diff] [blame] | 36 | unsigned long crit_temp; |
| 37 | int crit_trip_id; |
| 38 | unsigned long psv_temp; |
| 39 | int psv_trip_id; |
Srinivas Pandruvada | 925c36b | 2013-12-30 12:55:06 -0800 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | static int sys_get_curr_temp(struct thermal_zone_device *tzone, |
| 43 | unsigned long *temp) |
| 44 | { |
| 45 | struct acpi_device *device = tzone->devdata; |
| 46 | unsigned long long tmp; |
| 47 | acpi_status status; |
| 48 | |
| 49 | status = acpi_evaluate_integer(device->handle, "_TMP", NULL, &tmp); |
| 50 | if (ACPI_FAILURE(status)) |
| 51 | return -EIO; |
| 52 | |
| 53 | *temp = DECI_KELVIN_TO_MILLI_CELSIUS(tmp, KELVIN_OFFSET); |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static int sys_get_trip_hyst(struct thermal_zone_device *tzone, |
| 59 | int trip, unsigned long *temp) |
| 60 | { |
| 61 | struct acpi_device *device = tzone->devdata; |
| 62 | unsigned long long hyst; |
| 63 | acpi_status status; |
| 64 | |
| 65 | status = acpi_evaluate_integer(device->handle, "GTSH", NULL, &hyst); |
| 66 | if (ACPI_FAILURE(status)) |
| 67 | return -EIO; |
| 68 | |
lan,Tianyu | ced2284 | 2014-04-03 14:04:15 +0800 | [diff] [blame] | 69 | /* |
| 70 | * Thermal hysteresis represents a temperature difference. |
| 71 | * Kelvin and Celsius have same degree size. So the |
| 72 | * conversion here between tenths of degree Kelvin unit |
| 73 | * and Milli-Celsius unit is just to multiply 100. |
| 74 | */ |
| 75 | *temp = hyst * 100; |
Srinivas Pandruvada | 925c36b | 2013-12-30 12:55:06 -0800 | [diff] [blame] | 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static int sys_get_trip_temp(struct thermal_zone_device *tzone, |
| 81 | int trip, unsigned long *temp) |
| 82 | { |
| 83 | struct acpi_device *device = tzone->devdata; |
| 84 | struct int3403_sensor *obj = acpi_driver_data(device); |
| 85 | |
Srinivas Pandruvada | 5fcdeb2 | 2014-05-30 12:10:32 -0700 | [diff] [blame] | 86 | if (trip == obj->crit_trip_id) |
| 87 | *temp = obj->crit_temp; |
| 88 | else if (trip == obj->psv_trip_id) |
| 89 | *temp = obj->psv_temp; |
| 90 | else { |
| 91 | /* |
| 92 | * get_trip_temp is a mandatory callback but |
| 93 | * PATx method doesn't return any value, so return |
| 94 | * cached value, which was last set from user space. |
| 95 | */ |
| 96 | *temp = obj->thresholds[trip]; |
| 97 | } |
Srinivas Pandruvada | 925c36b | 2013-12-30 12:55:06 -0800 | [diff] [blame] | 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static int sys_get_trip_type(struct thermal_zone_device *thermal, |
| 103 | int trip, enum thermal_trip_type *type) |
| 104 | { |
Srinivas Pandruvada | 5fcdeb2 | 2014-05-30 12:10:32 -0700 | [diff] [blame] | 105 | struct acpi_device *device = thermal->devdata; |
| 106 | struct int3403_sensor *obj = acpi_driver_data(device); |
| 107 | |
Srinivas Pandruvada | 925c36b | 2013-12-30 12:55:06 -0800 | [diff] [blame] | 108 | /* Mandatory callback, may not mean much here */ |
Srinivas Pandruvada | 5fcdeb2 | 2014-05-30 12:10:32 -0700 | [diff] [blame] | 109 | if (trip == obj->crit_trip_id) |
| 110 | *type = THERMAL_TRIP_CRITICAL; |
| 111 | else |
| 112 | *type = THERMAL_TRIP_PASSIVE; |
Srinivas Pandruvada | 925c36b | 2013-12-30 12:55:06 -0800 | [diff] [blame] | 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | int sys_set_trip_temp(struct thermal_zone_device *tzone, int trip, |
| 118 | unsigned long temp) |
| 119 | { |
| 120 | struct acpi_device *device = tzone->devdata; |
| 121 | acpi_status status; |
| 122 | char name[10]; |
| 123 | int ret = 0; |
| 124 | struct int3403_sensor *obj = acpi_driver_data(device); |
| 125 | |
| 126 | snprintf(name, sizeof(name), "PAT%d", trip); |
| 127 | if (acpi_has_method(device->handle, name)) { |
| 128 | status = acpi_execute_simple_method(device->handle, name, |
| 129 | MILLI_CELSIUS_TO_DECI_KELVIN(temp, |
| 130 | KELVIN_OFFSET)); |
| 131 | if (ACPI_FAILURE(status)) |
| 132 | ret = -EIO; |
| 133 | else |
| 134 | obj->thresholds[trip] = temp; |
| 135 | } else { |
| 136 | ret = -EIO; |
| 137 | dev_err(&device->dev, "sys_set_trip_temp: method not found\n"); |
| 138 | } |
| 139 | |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | static struct thermal_zone_device_ops tzone_ops = { |
| 144 | .get_temp = sys_get_curr_temp, |
| 145 | .get_trip_temp = sys_get_trip_temp, |
| 146 | .get_trip_type = sys_get_trip_type, |
| 147 | .set_trip_temp = sys_set_trip_temp, |
| 148 | .get_trip_hyst = sys_get_trip_hyst, |
| 149 | }; |
| 150 | |
| 151 | static void acpi_thermal_notify(struct acpi_device *device, u32 event) |
| 152 | { |
| 153 | struct int3403_sensor *obj; |
| 154 | |
| 155 | if (!device) |
| 156 | return; |
| 157 | |
| 158 | obj = acpi_driver_data(device); |
| 159 | if (!obj) |
| 160 | return; |
| 161 | |
| 162 | switch (event) { |
| 163 | case INT3403_PERF_CHANGED_EVENT: |
| 164 | break; |
| 165 | case INT3403_THERMAL_EVENT: |
| 166 | thermal_zone_device_update(obj->tzone); |
| 167 | break; |
| 168 | default: |
| 169 | dev_err(&device->dev, "Unsupported event [0x%x]\n", event); |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | |
Srinivas Pandruvada | 5fcdeb2 | 2014-05-30 12:10:32 -0700 | [diff] [blame] | 174 | static int sys_get_trip_crt(struct acpi_device *device, unsigned long *temp) |
| 175 | { |
| 176 | unsigned long long crt; |
| 177 | acpi_status status; |
| 178 | |
| 179 | status = acpi_evaluate_integer(device->handle, "_CRT", NULL, &crt); |
| 180 | if (ACPI_FAILURE(status)) |
| 181 | return -EIO; |
| 182 | |
| 183 | *temp = DECI_KELVIN_TO_MILLI_CELSIUS(crt, KELVIN_OFFSET); |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static int sys_get_trip_psv(struct acpi_device *device, unsigned long *temp) |
| 189 | { |
| 190 | unsigned long long psv; |
| 191 | acpi_status status; |
| 192 | |
| 193 | status = acpi_evaluate_integer(device->handle, "_PSV", NULL, &psv); |
| 194 | if (ACPI_FAILURE(status)) |
| 195 | return -EIO; |
| 196 | |
| 197 | *temp = DECI_KELVIN_TO_MILLI_CELSIUS(psv, KELVIN_OFFSET); |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
Srinivas Pandruvada | 925c36b | 2013-12-30 12:55:06 -0800 | [diff] [blame] | 202 | static int acpi_int3403_add(struct acpi_device *device) |
| 203 | { |
| 204 | int result = 0; |
| 205 | unsigned long long ptyp; |
| 206 | acpi_status status; |
| 207 | struct int3403_sensor *obj; |
| 208 | unsigned long long trip_cnt; |
| 209 | int trip_mask = 0; |
| 210 | |
| 211 | if (!device) |
| 212 | return -EINVAL; |
| 213 | |
| 214 | status = acpi_evaluate_integer(device->handle, "PTYP", NULL, &ptyp); |
| 215 | if (ACPI_FAILURE(status)) |
| 216 | return -EINVAL; |
| 217 | |
| 218 | if (ptyp != INT3403_TYPE_SENSOR) |
| 219 | return -EINVAL; |
| 220 | |
| 221 | obj = devm_kzalloc(&device->dev, sizeof(*obj), GFP_KERNEL); |
| 222 | if (!obj) |
| 223 | return -ENOMEM; |
| 224 | |
| 225 | device->driver_data = obj; |
| 226 | |
| 227 | status = acpi_evaluate_integer(device->handle, "PATC", NULL, |
| 228 | &trip_cnt); |
| 229 | if (ACPI_FAILURE(status)) |
| 230 | trip_cnt = 0; |
| 231 | |
| 232 | if (trip_cnt) { |
| 233 | /* We have to cache, thresholds can't be readback */ |
| 234 | obj->thresholds = devm_kzalloc(&device->dev, |
| 235 | sizeof(*obj->thresholds) * trip_cnt, |
| 236 | GFP_KERNEL); |
| 237 | if (!obj->thresholds) |
| 238 | return -ENOMEM; |
| 239 | trip_mask = BIT(trip_cnt) - 1; |
| 240 | } |
Srinivas Pandruvada | 5fcdeb2 | 2014-05-30 12:10:32 -0700 | [diff] [blame] | 241 | |
| 242 | obj->psv_trip_id = -1; |
| 243 | if (!sys_get_trip_psv(device, &obj->psv_temp)) |
| 244 | obj->psv_trip_id = trip_cnt++; |
| 245 | |
| 246 | obj->crit_trip_id = -1; |
| 247 | if (!sys_get_trip_crt(device, &obj->crit_temp)) |
| 248 | obj->crit_trip_id = trip_cnt++; |
| 249 | |
Srinivas Pandruvada | 925c36b | 2013-12-30 12:55:06 -0800 | [diff] [blame] | 250 | obj->tzone = thermal_zone_device_register(acpi_device_bid(device), |
| 251 | trip_cnt, trip_mask, device, &tzone_ops, |
| 252 | NULL, 0, 0); |
| 253 | if (IS_ERR(obj->tzone)) { |
| 254 | result = PTR_ERR(obj->tzone); |
| 255 | return result; |
| 256 | } |
| 257 | |
| 258 | strcpy(acpi_device_name(device), "INT3403"); |
| 259 | strcpy(acpi_device_class(device), ACPI_INT3403_CLASS); |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | static int acpi_int3403_remove(struct acpi_device *device) |
| 265 | { |
| 266 | struct int3403_sensor *obj; |
| 267 | |
| 268 | obj = acpi_driver_data(device); |
| 269 | thermal_zone_device_unregister(obj->tzone); |
| 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | ACPI_MODULE_NAME("int3403"); |
| 275 | static const struct acpi_device_id int3403_device_ids[] = { |
| 276 | {"INT3403", 0}, |
| 277 | {"", 0}, |
| 278 | }; |
| 279 | MODULE_DEVICE_TABLE(acpi, int3403_device_ids); |
| 280 | |
| 281 | static struct acpi_driver acpi_int3403_driver = { |
| 282 | .name = "INT3403", |
| 283 | .class = ACPI_INT3403_CLASS, |
| 284 | .ids = int3403_device_ids, |
| 285 | .ops = { |
| 286 | .add = acpi_int3403_add, |
| 287 | .remove = acpi_int3403_remove, |
| 288 | .notify = acpi_thermal_notify, |
| 289 | }, |
| 290 | }; |
| 291 | |
| 292 | module_acpi_driver(acpi_int3403_driver); |
| 293 | |
| 294 | MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>"); |
| 295 | MODULE_LICENSE("GPL v2"); |
| 296 | MODULE_DESCRIPTION("ACPI INT3403 thermal driver"); |