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; |
| 36 | }; |
| 37 | |
| 38 | static int sys_get_curr_temp(struct thermal_zone_device *tzone, |
| 39 | unsigned long *temp) |
| 40 | { |
| 41 | struct acpi_device *device = tzone->devdata; |
| 42 | unsigned long long tmp; |
| 43 | acpi_status status; |
| 44 | |
| 45 | status = acpi_evaluate_integer(device->handle, "_TMP", NULL, &tmp); |
| 46 | if (ACPI_FAILURE(status)) |
| 47 | return -EIO; |
| 48 | |
| 49 | *temp = DECI_KELVIN_TO_MILLI_CELSIUS(tmp, KELVIN_OFFSET); |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static int sys_get_trip_hyst(struct thermal_zone_device *tzone, |
| 55 | int trip, unsigned long *temp) |
| 56 | { |
| 57 | struct acpi_device *device = tzone->devdata; |
| 58 | unsigned long long hyst; |
| 59 | acpi_status status; |
| 60 | |
| 61 | status = acpi_evaluate_integer(device->handle, "GTSH", NULL, &hyst); |
| 62 | if (ACPI_FAILURE(status)) |
| 63 | return -EIO; |
| 64 | |
lan,Tianyu | ced2284 | 2014-04-03 14:04:15 +0800 | [diff] [blame] | 65 | /* |
| 66 | * Thermal hysteresis represents a temperature difference. |
| 67 | * Kelvin and Celsius have same degree size. So the |
| 68 | * conversion here between tenths of degree Kelvin unit |
| 69 | * and Milli-Celsius unit is just to multiply 100. |
| 70 | */ |
| 71 | *temp = hyst * 100; |
Srinivas Pandruvada | 925c36b | 2013-12-30 12:55:06 -0800 | [diff] [blame] | 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static int sys_get_trip_temp(struct thermal_zone_device *tzone, |
| 77 | int trip, unsigned long *temp) |
| 78 | { |
| 79 | struct acpi_device *device = tzone->devdata; |
| 80 | struct int3403_sensor *obj = acpi_driver_data(device); |
| 81 | |
| 82 | /* |
| 83 | * get_trip_temp is a mandatory callback but |
| 84 | * PATx method doesn't return any value, so return |
| 85 | * cached value, which was last set from user space. |
| 86 | */ |
| 87 | *temp = obj->thresholds[trip]; |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static int sys_get_trip_type(struct thermal_zone_device *thermal, |
| 93 | int trip, enum thermal_trip_type *type) |
| 94 | { |
| 95 | /* Mandatory callback, may not mean much here */ |
| 96 | *type = THERMAL_TRIP_PASSIVE; |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | int sys_set_trip_temp(struct thermal_zone_device *tzone, int trip, |
| 102 | unsigned long temp) |
| 103 | { |
| 104 | struct acpi_device *device = tzone->devdata; |
| 105 | acpi_status status; |
| 106 | char name[10]; |
| 107 | int ret = 0; |
| 108 | struct int3403_sensor *obj = acpi_driver_data(device); |
| 109 | |
| 110 | snprintf(name, sizeof(name), "PAT%d", trip); |
| 111 | if (acpi_has_method(device->handle, name)) { |
| 112 | status = acpi_execute_simple_method(device->handle, name, |
| 113 | MILLI_CELSIUS_TO_DECI_KELVIN(temp, |
| 114 | KELVIN_OFFSET)); |
| 115 | if (ACPI_FAILURE(status)) |
| 116 | ret = -EIO; |
| 117 | else |
| 118 | obj->thresholds[trip] = temp; |
| 119 | } else { |
| 120 | ret = -EIO; |
| 121 | dev_err(&device->dev, "sys_set_trip_temp: method not found\n"); |
| 122 | } |
| 123 | |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | static struct thermal_zone_device_ops tzone_ops = { |
| 128 | .get_temp = sys_get_curr_temp, |
| 129 | .get_trip_temp = sys_get_trip_temp, |
| 130 | .get_trip_type = sys_get_trip_type, |
| 131 | .set_trip_temp = sys_set_trip_temp, |
| 132 | .get_trip_hyst = sys_get_trip_hyst, |
| 133 | }; |
| 134 | |
| 135 | static void acpi_thermal_notify(struct acpi_device *device, u32 event) |
| 136 | { |
| 137 | struct int3403_sensor *obj; |
| 138 | |
| 139 | if (!device) |
| 140 | return; |
| 141 | |
| 142 | obj = acpi_driver_data(device); |
| 143 | if (!obj) |
| 144 | return; |
| 145 | |
| 146 | switch (event) { |
| 147 | case INT3403_PERF_CHANGED_EVENT: |
| 148 | break; |
| 149 | case INT3403_THERMAL_EVENT: |
| 150 | thermal_zone_device_update(obj->tzone); |
| 151 | break; |
| 152 | default: |
| 153 | dev_err(&device->dev, "Unsupported event [0x%x]\n", event); |
| 154 | break; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | static int acpi_int3403_add(struct acpi_device *device) |
| 159 | { |
| 160 | int result = 0; |
| 161 | unsigned long long ptyp; |
| 162 | acpi_status status; |
| 163 | struct int3403_sensor *obj; |
| 164 | unsigned long long trip_cnt; |
| 165 | int trip_mask = 0; |
| 166 | |
| 167 | if (!device) |
| 168 | return -EINVAL; |
| 169 | |
| 170 | status = acpi_evaluate_integer(device->handle, "PTYP", NULL, &ptyp); |
| 171 | if (ACPI_FAILURE(status)) |
| 172 | return -EINVAL; |
| 173 | |
| 174 | if (ptyp != INT3403_TYPE_SENSOR) |
| 175 | return -EINVAL; |
| 176 | |
| 177 | obj = devm_kzalloc(&device->dev, sizeof(*obj), GFP_KERNEL); |
| 178 | if (!obj) |
| 179 | return -ENOMEM; |
| 180 | |
| 181 | device->driver_data = obj; |
| 182 | |
| 183 | status = acpi_evaluate_integer(device->handle, "PATC", NULL, |
| 184 | &trip_cnt); |
| 185 | if (ACPI_FAILURE(status)) |
| 186 | trip_cnt = 0; |
| 187 | |
| 188 | if (trip_cnt) { |
| 189 | /* We have to cache, thresholds can't be readback */ |
| 190 | obj->thresholds = devm_kzalloc(&device->dev, |
| 191 | sizeof(*obj->thresholds) * trip_cnt, |
| 192 | GFP_KERNEL); |
| 193 | if (!obj->thresholds) |
| 194 | return -ENOMEM; |
| 195 | trip_mask = BIT(trip_cnt) - 1; |
| 196 | } |
| 197 | obj->tzone = thermal_zone_device_register(acpi_device_bid(device), |
| 198 | trip_cnt, trip_mask, device, &tzone_ops, |
| 199 | NULL, 0, 0); |
| 200 | if (IS_ERR(obj->tzone)) { |
| 201 | result = PTR_ERR(obj->tzone); |
| 202 | return result; |
| 203 | } |
| 204 | |
| 205 | strcpy(acpi_device_name(device), "INT3403"); |
| 206 | strcpy(acpi_device_class(device), ACPI_INT3403_CLASS); |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static int acpi_int3403_remove(struct acpi_device *device) |
| 212 | { |
| 213 | struct int3403_sensor *obj; |
| 214 | |
| 215 | obj = acpi_driver_data(device); |
| 216 | thermal_zone_device_unregister(obj->tzone); |
| 217 | |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | ACPI_MODULE_NAME("int3403"); |
| 222 | static const struct acpi_device_id int3403_device_ids[] = { |
| 223 | {"INT3403", 0}, |
| 224 | {"", 0}, |
| 225 | }; |
| 226 | MODULE_DEVICE_TABLE(acpi, int3403_device_ids); |
| 227 | |
| 228 | static struct acpi_driver acpi_int3403_driver = { |
| 229 | .name = "INT3403", |
| 230 | .class = ACPI_INT3403_CLASS, |
| 231 | .ids = int3403_device_ids, |
| 232 | .ops = { |
| 233 | .add = acpi_int3403_add, |
| 234 | .remove = acpi_int3403_remove, |
| 235 | .notify = acpi_thermal_notify, |
| 236 | }, |
| 237 | }; |
| 238 | |
| 239 | module_acpi_driver(acpi_int3403_driver); |
| 240 | |
| 241 | MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>"); |
| 242 | MODULE_LICENSE("GPL v2"); |
| 243 | MODULE_DESCRIPTION("ACPI INT3403 thermal driver"); |