Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1 | /* |
| 2 | * thermal.c - Generic Thermal Management Sysfs support. |
| 3 | * |
| 4 | * Copyright (C) 2008 Intel Corp |
| 5 | * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com> |
| 6 | * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com> |
| 7 | * |
| 8 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; version 2 of the License. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 22 | * |
| 23 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 24 | */ |
| 25 | |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/device.h> |
| 28 | #include <linux/err.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 30 | #include <linux/kdev_t.h> |
| 31 | #include <linux/idr.h> |
| 32 | #include <linux/thermal.h> |
| 33 | #include <linux/spinlock.h> |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 34 | #include <linux/reboot.h> |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 35 | #include <net/netlink.h> |
| 36 | #include <net/genetlink.h> |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 37 | |
Zhang Rui | 63c4ec9 | 2008-04-21 16:07:13 +0800 | [diff] [blame] | 38 | MODULE_AUTHOR("Zhang Rui"); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 39 | MODULE_DESCRIPTION("Generic thermal management sysfs support"); |
| 40 | MODULE_LICENSE("GPL"); |
| 41 | |
| 42 | #define PREFIX "Thermal: " |
| 43 | |
| 44 | struct thermal_cooling_device_instance { |
| 45 | int id; |
| 46 | char name[THERMAL_NAME_LENGTH]; |
| 47 | struct thermal_zone_device *tz; |
| 48 | struct thermal_cooling_device *cdev; |
| 49 | int trip; |
| 50 | char attr_name[THERMAL_NAME_LENGTH]; |
| 51 | struct device_attribute attr; |
| 52 | struct list_head node; |
| 53 | }; |
| 54 | |
| 55 | static DEFINE_IDR(thermal_tz_idr); |
| 56 | static DEFINE_IDR(thermal_cdev_idr); |
| 57 | static DEFINE_MUTEX(thermal_idr_lock); |
| 58 | |
| 59 | static LIST_HEAD(thermal_tz_list); |
| 60 | static LIST_HEAD(thermal_cdev_list); |
| 61 | static DEFINE_MUTEX(thermal_list_lock); |
| 62 | |
| 63 | static int get_idr(struct idr *idr, struct mutex *lock, int *id) |
| 64 | { |
| 65 | int err; |
| 66 | |
| 67 | again: |
| 68 | if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0)) |
| 69 | return -ENOMEM; |
| 70 | |
| 71 | if (lock) |
| 72 | mutex_lock(lock); |
| 73 | err = idr_get_new(idr, NULL, id); |
| 74 | if (lock) |
| 75 | mutex_unlock(lock); |
| 76 | if (unlikely(err == -EAGAIN)) |
| 77 | goto again; |
| 78 | else if (unlikely(err)) |
| 79 | return err; |
| 80 | |
| 81 | *id = *id & MAX_ID_MASK; |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static void release_idr(struct idr *idr, struct mutex *lock, int id) |
| 86 | { |
| 87 | if (lock) |
| 88 | mutex_lock(lock); |
| 89 | idr_remove(idr, id); |
| 90 | if (lock) |
| 91 | mutex_unlock(lock); |
| 92 | } |
| 93 | |
| 94 | /* sys I/F for thermal zone */ |
| 95 | |
| 96 | #define to_thermal_zone(_dev) \ |
| 97 | container_of(_dev, struct thermal_zone_device, device) |
| 98 | |
| 99 | static ssize_t |
| 100 | type_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 101 | { |
| 102 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
| 103 | |
| 104 | return sprintf(buf, "%s\n", tz->type); |
| 105 | } |
| 106 | |
| 107 | static ssize_t |
| 108 | temp_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 109 | { |
| 110 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 111 | long temperature; |
| 112 | int ret; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 113 | |
| 114 | if (!tz->ops->get_temp) |
| 115 | return -EPERM; |
| 116 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 117 | ret = tz->ops->get_temp(tz, &temperature); |
| 118 | |
| 119 | if (ret) |
| 120 | return ret; |
| 121 | |
| 122 | return sprintf(buf, "%ld\n", temperature); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | static ssize_t |
| 126 | mode_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 127 | { |
| 128 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 129 | enum thermal_device_mode mode; |
| 130 | int result; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 131 | |
| 132 | if (!tz->ops->get_mode) |
| 133 | return -EPERM; |
| 134 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 135 | result = tz->ops->get_mode(tz, &mode); |
| 136 | if (result) |
| 137 | return result; |
| 138 | |
| 139 | return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled" |
| 140 | : "disabled"); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static ssize_t |
| 144 | mode_store(struct device *dev, struct device_attribute *attr, |
| 145 | const char *buf, size_t count) |
| 146 | { |
| 147 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
| 148 | int result; |
| 149 | |
| 150 | if (!tz->ops->set_mode) |
| 151 | return -EPERM; |
| 152 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 153 | if (!strncmp(buf, "enabled", sizeof("enabled"))) |
| 154 | result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED); |
| 155 | else if (!strncmp(buf, "disabled", sizeof("disabled"))) |
| 156 | result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED); |
| 157 | else |
| 158 | result = -EINVAL; |
| 159 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 160 | if (result) |
| 161 | return result; |
| 162 | |
| 163 | return count; |
| 164 | } |
| 165 | |
| 166 | static ssize_t |
| 167 | trip_point_type_show(struct device *dev, struct device_attribute *attr, |
| 168 | char *buf) |
| 169 | { |
| 170 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 171 | enum thermal_trip_type type; |
| 172 | int trip, result; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 173 | |
| 174 | if (!tz->ops->get_trip_type) |
| 175 | return -EPERM; |
| 176 | |
| 177 | if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip)) |
| 178 | return -EINVAL; |
| 179 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 180 | result = tz->ops->get_trip_type(tz, trip, &type); |
| 181 | if (result) |
| 182 | return result; |
| 183 | |
| 184 | switch (type) { |
| 185 | case THERMAL_TRIP_CRITICAL: |
Amit Kucheria | 625120a4 | 2009-10-16 12:46:02 +0300 | [diff] [blame] | 186 | return sprintf(buf, "critical\n"); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 187 | case THERMAL_TRIP_HOT: |
Amit Kucheria | 625120a4 | 2009-10-16 12:46:02 +0300 | [diff] [blame] | 188 | return sprintf(buf, "hot\n"); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 189 | case THERMAL_TRIP_PASSIVE: |
Amit Kucheria | 625120a4 | 2009-10-16 12:46:02 +0300 | [diff] [blame] | 190 | return sprintf(buf, "passive\n"); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 191 | case THERMAL_TRIP_ACTIVE: |
Amit Kucheria | 625120a4 | 2009-10-16 12:46:02 +0300 | [diff] [blame] | 192 | return sprintf(buf, "active\n"); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 193 | default: |
Amit Kucheria | 625120a4 | 2009-10-16 12:46:02 +0300 | [diff] [blame] | 194 | return sprintf(buf, "unknown\n"); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 195 | } |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static ssize_t |
| 199 | trip_point_temp_show(struct device *dev, struct device_attribute *attr, |
| 200 | char *buf) |
| 201 | { |
| 202 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 203 | int trip, ret; |
| 204 | long temperature; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 205 | |
| 206 | if (!tz->ops->get_trip_temp) |
| 207 | return -EPERM; |
| 208 | |
| 209 | if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip)) |
| 210 | return -EINVAL; |
| 211 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 212 | ret = tz->ops->get_trip_temp(tz, trip, &temperature); |
| 213 | |
| 214 | if (ret) |
| 215 | return ret; |
| 216 | |
| 217 | return sprintf(buf, "%ld\n", temperature); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 218 | } |
| 219 | |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 220 | static ssize_t |
| 221 | passive_store(struct device *dev, struct device_attribute *attr, |
| 222 | const char *buf, size_t count) |
| 223 | { |
| 224 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
| 225 | struct thermal_cooling_device *cdev = NULL; |
| 226 | int state; |
| 227 | |
| 228 | if (!sscanf(buf, "%d\n", &state)) |
| 229 | return -EINVAL; |
| 230 | |
Frans Pop | 3d8e3ad | 2009-10-26 08:39:02 +0100 | [diff] [blame] | 231 | /* sanity check: values below 1000 millicelcius don't make sense |
| 232 | * and can cause the system to go into a thermal heart attack |
| 233 | */ |
| 234 | if (state && state < 1000) |
| 235 | return -EINVAL; |
| 236 | |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 237 | if (state && !tz->forced_passive) { |
| 238 | mutex_lock(&thermal_list_lock); |
| 239 | list_for_each_entry(cdev, &thermal_cdev_list, node) { |
| 240 | if (!strncmp("Processor", cdev->type, |
| 241 | sizeof("Processor"))) |
| 242 | thermal_zone_bind_cooling_device(tz, |
| 243 | THERMAL_TRIPS_NONE, |
| 244 | cdev); |
| 245 | } |
| 246 | mutex_unlock(&thermal_list_lock); |
Frans Pop | e4143b0 | 2009-10-26 08:39:03 +0100 | [diff] [blame] | 247 | if (!tz->passive_delay) |
| 248 | tz->passive_delay = 1000; |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 249 | } else if (!state && tz->forced_passive) { |
| 250 | mutex_lock(&thermal_list_lock); |
| 251 | list_for_each_entry(cdev, &thermal_cdev_list, node) { |
| 252 | if (!strncmp("Processor", cdev->type, |
| 253 | sizeof("Processor"))) |
| 254 | thermal_zone_unbind_cooling_device(tz, |
| 255 | THERMAL_TRIPS_NONE, |
| 256 | cdev); |
| 257 | } |
| 258 | mutex_unlock(&thermal_list_lock); |
Frans Pop | e4143b0 | 2009-10-26 08:39:03 +0100 | [diff] [blame] | 259 | tz->passive_delay = 0; |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | tz->tc1 = 1; |
| 263 | tz->tc2 = 1; |
| 264 | |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 265 | tz->forced_passive = state; |
| 266 | |
| 267 | thermal_zone_device_update(tz); |
| 268 | |
| 269 | return count; |
| 270 | } |
| 271 | |
| 272 | static ssize_t |
| 273 | passive_show(struct device *dev, struct device_attribute *attr, |
| 274 | char *buf) |
| 275 | { |
| 276 | struct thermal_zone_device *tz = to_thermal_zone(dev); |
| 277 | |
| 278 | return sprintf(buf, "%d\n", tz->forced_passive); |
| 279 | } |
| 280 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 281 | static DEVICE_ATTR(type, 0444, type_show, NULL); |
| 282 | static DEVICE_ATTR(temp, 0444, temp_show, NULL); |
| 283 | static DEVICE_ATTR(mode, 0644, mode_show, mode_store); |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 284 | static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, \ |
| 285 | passive_store); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 286 | |
| 287 | static struct device_attribute trip_point_attrs[] = { |
| 288 | __ATTR(trip_point_0_type, 0444, trip_point_type_show, NULL), |
| 289 | __ATTR(trip_point_0_temp, 0444, trip_point_temp_show, NULL), |
| 290 | __ATTR(trip_point_1_type, 0444, trip_point_type_show, NULL), |
| 291 | __ATTR(trip_point_1_temp, 0444, trip_point_temp_show, NULL), |
| 292 | __ATTR(trip_point_2_type, 0444, trip_point_type_show, NULL), |
| 293 | __ATTR(trip_point_2_temp, 0444, trip_point_temp_show, NULL), |
| 294 | __ATTR(trip_point_3_type, 0444, trip_point_type_show, NULL), |
| 295 | __ATTR(trip_point_3_temp, 0444, trip_point_temp_show, NULL), |
| 296 | __ATTR(trip_point_4_type, 0444, trip_point_type_show, NULL), |
| 297 | __ATTR(trip_point_4_temp, 0444, trip_point_temp_show, NULL), |
| 298 | __ATTR(trip_point_5_type, 0444, trip_point_type_show, NULL), |
| 299 | __ATTR(trip_point_5_temp, 0444, trip_point_temp_show, NULL), |
| 300 | __ATTR(trip_point_6_type, 0444, trip_point_type_show, NULL), |
| 301 | __ATTR(trip_point_6_temp, 0444, trip_point_temp_show, NULL), |
| 302 | __ATTR(trip_point_7_type, 0444, trip_point_type_show, NULL), |
| 303 | __ATTR(trip_point_7_temp, 0444, trip_point_temp_show, NULL), |
| 304 | __ATTR(trip_point_8_type, 0444, trip_point_type_show, NULL), |
| 305 | __ATTR(trip_point_8_temp, 0444, trip_point_temp_show, NULL), |
| 306 | __ATTR(trip_point_9_type, 0444, trip_point_type_show, NULL), |
| 307 | __ATTR(trip_point_9_temp, 0444, trip_point_temp_show, NULL), |
Krzysztof Helt | 5f1a3f2 | 2008-04-15 14:34:47 -0700 | [diff] [blame] | 308 | __ATTR(trip_point_10_type, 0444, trip_point_type_show, NULL), |
| 309 | __ATTR(trip_point_10_temp, 0444, trip_point_temp_show, NULL), |
| 310 | __ATTR(trip_point_11_type, 0444, trip_point_type_show, NULL), |
| 311 | __ATTR(trip_point_11_temp, 0444, trip_point_temp_show, NULL), |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 312 | }; |
| 313 | |
| 314 | #define TRIP_POINT_ATTR_ADD(_dev, _index, result) \ |
| 315 | do { \ |
| 316 | result = device_create_file(_dev, \ |
| 317 | &trip_point_attrs[_index * 2]); \ |
| 318 | if (result) \ |
| 319 | break; \ |
| 320 | result = device_create_file(_dev, \ |
| 321 | &trip_point_attrs[_index * 2 + 1]); \ |
| 322 | } while (0) |
| 323 | |
| 324 | #define TRIP_POINT_ATTR_REMOVE(_dev, _index) \ |
| 325 | do { \ |
| 326 | device_remove_file(_dev, &trip_point_attrs[_index * 2]); \ |
| 327 | device_remove_file(_dev, &trip_point_attrs[_index * 2 + 1]); \ |
| 328 | } while (0) |
| 329 | |
| 330 | /* sys I/F for cooling device */ |
| 331 | #define to_cooling_device(_dev) \ |
| 332 | container_of(_dev, struct thermal_cooling_device, device) |
| 333 | |
| 334 | static ssize_t |
| 335 | thermal_cooling_device_type_show(struct device *dev, |
| 336 | struct device_attribute *attr, char *buf) |
| 337 | { |
| 338 | struct thermal_cooling_device *cdev = to_cooling_device(dev); |
| 339 | |
| 340 | return sprintf(buf, "%s\n", cdev->type); |
| 341 | } |
| 342 | |
| 343 | static ssize_t |
| 344 | thermal_cooling_device_max_state_show(struct device *dev, |
| 345 | struct device_attribute *attr, char *buf) |
| 346 | { |
| 347 | struct thermal_cooling_device *cdev = to_cooling_device(dev); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 348 | unsigned long state; |
| 349 | int ret; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 350 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 351 | ret = cdev->ops->get_max_state(cdev, &state); |
| 352 | if (ret) |
| 353 | return ret; |
| 354 | return sprintf(buf, "%ld\n", state); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | static ssize_t |
| 358 | thermal_cooling_device_cur_state_show(struct device *dev, |
| 359 | struct device_attribute *attr, char *buf) |
| 360 | { |
| 361 | struct thermal_cooling_device *cdev = to_cooling_device(dev); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 362 | unsigned long state; |
| 363 | int ret; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 364 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 365 | ret = cdev->ops->get_cur_state(cdev, &state); |
| 366 | if (ret) |
| 367 | return ret; |
| 368 | return sprintf(buf, "%ld\n", state); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | static ssize_t |
| 372 | thermal_cooling_device_cur_state_store(struct device *dev, |
| 373 | struct device_attribute *attr, |
| 374 | const char *buf, size_t count) |
| 375 | { |
| 376 | struct thermal_cooling_device *cdev = to_cooling_device(dev); |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 377 | unsigned long state; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 378 | int result; |
| 379 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 380 | if (!sscanf(buf, "%ld\n", &state)) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 381 | return -EINVAL; |
| 382 | |
Roel Kluin | edb9491 | 2009-12-15 22:46:50 +0100 | [diff] [blame] | 383 | if ((long)state < 0) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 384 | return -EINVAL; |
| 385 | |
| 386 | result = cdev->ops->set_cur_state(cdev, state); |
| 387 | if (result) |
| 388 | return result; |
| 389 | return count; |
| 390 | } |
| 391 | |
| 392 | static struct device_attribute dev_attr_cdev_type = |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 393 | __ATTR(type, 0444, thermal_cooling_device_type_show, NULL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 394 | static DEVICE_ATTR(max_state, 0444, |
| 395 | thermal_cooling_device_max_state_show, NULL); |
| 396 | static DEVICE_ATTR(cur_state, 0644, |
| 397 | thermal_cooling_device_cur_state_show, |
| 398 | thermal_cooling_device_cur_state_store); |
| 399 | |
| 400 | static ssize_t |
| 401 | thermal_cooling_device_trip_point_show(struct device *dev, |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 402 | struct device_attribute *attr, char *buf) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 403 | { |
| 404 | struct thermal_cooling_device_instance *instance; |
| 405 | |
| 406 | instance = |
| 407 | container_of(attr, struct thermal_cooling_device_instance, attr); |
| 408 | |
| 409 | if (instance->trip == THERMAL_TRIPS_NONE) |
| 410 | return sprintf(buf, "-1\n"); |
| 411 | else |
| 412 | return sprintf(buf, "%d\n", instance->trip); |
| 413 | } |
| 414 | |
| 415 | /* Device management */ |
| 416 | |
Rene Herman | 16d7523 | 2008-06-24 19:38:56 +0200 | [diff] [blame] | 417 | #if defined(CONFIG_THERMAL_HWMON) |
| 418 | |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 419 | /* hwmon sys I/F */ |
| 420 | #include <linux/hwmon.h> |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 421 | |
| 422 | /* thermal zone devices with the same type share one hwmon device */ |
| 423 | struct thermal_hwmon_device { |
| 424 | char type[THERMAL_NAME_LENGTH]; |
| 425 | struct device *device; |
| 426 | int count; |
| 427 | struct list_head tz_list; |
| 428 | struct list_head node; |
| 429 | }; |
| 430 | |
| 431 | struct thermal_hwmon_attr { |
| 432 | struct device_attribute attr; |
| 433 | char name[16]; |
| 434 | }; |
| 435 | |
| 436 | /* one temperature input for each thermal zone */ |
| 437 | struct thermal_hwmon_temp { |
| 438 | struct list_head hwmon_node; |
| 439 | struct thermal_zone_device *tz; |
| 440 | struct thermal_hwmon_attr temp_input; /* hwmon sys attr */ |
| 441 | struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */ |
| 442 | }; |
| 443 | |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 444 | static LIST_HEAD(thermal_hwmon_list); |
| 445 | |
| 446 | static ssize_t |
| 447 | name_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 448 | { |
Greg Kroah-Hartman | 0e968a3 | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 449 | struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 450 | return sprintf(buf, "%s\n", hwmon->type); |
| 451 | } |
| 452 | static DEVICE_ATTR(name, 0444, name_show, NULL); |
| 453 | |
| 454 | static ssize_t |
| 455 | temp_input_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 456 | { |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 457 | long temperature; |
| 458 | int ret; |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 459 | struct thermal_hwmon_attr *hwmon_attr |
| 460 | = container_of(attr, struct thermal_hwmon_attr, attr); |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 461 | struct thermal_hwmon_temp *temp |
| 462 | = container_of(hwmon_attr, struct thermal_hwmon_temp, |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 463 | temp_input); |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 464 | struct thermal_zone_device *tz = temp->tz; |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 465 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 466 | ret = tz->ops->get_temp(tz, &temperature); |
| 467 | |
| 468 | if (ret) |
| 469 | return ret; |
| 470 | |
| 471 | return sprintf(buf, "%ld\n", temperature); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | static ssize_t |
| 475 | temp_crit_show(struct device *dev, struct device_attribute *attr, |
| 476 | char *buf) |
| 477 | { |
| 478 | struct thermal_hwmon_attr *hwmon_attr |
| 479 | = container_of(attr, struct thermal_hwmon_attr, attr); |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 480 | struct thermal_hwmon_temp *temp |
| 481 | = container_of(hwmon_attr, struct thermal_hwmon_temp, |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 482 | temp_crit); |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 483 | struct thermal_zone_device *tz = temp->tz; |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 484 | long temperature; |
| 485 | int ret; |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 486 | |
Matthew Garrett | 6503e5d | 2008-11-27 17:48:13 +0000 | [diff] [blame] | 487 | ret = tz->ops->get_trip_temp(tz, 0, &temperature); |
| 488 | if (ret) |
| 489 | return ret; |
| 490 | |
| 491 | return sprintf(buf, "%ld\n", temperature); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | |
Jean Delvare | 0d97d7a | 2011-07-28 13:48:41 -0700 | [diff] [blame] | 495 | static struct thermal_hwmon_device * |
| 496 | thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz) |
| 497 | { |
| 498 | struct thermal_hwmon_device *hwmon; |
| 499 | |
| 500 | mutex_lock(&thermal_list_lock); |
| 501 | list_for_each_entry(hwmon, &thermal_hwmon_list, node) |
| 502 | if (!strcmp(hwmon->type, tz->type)) { |
| 503 | mutex_unlock(&thermal_list_lock); |
| 504 | return hwmon; |
| 505 | } |
| 506 | mutex_unlock(&thermal_list_lock); |
| 507 | |
| 508 | return NULL; |
| 509 | } |
| 510 | |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 511 | /* Find the temperature input matching a given thermal zone */ |
| 512 | static struct thermal_hwmon_temp * |
| 513 | thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon, |
| 514 | const struct thermal_zone_device *tz) |
| 515 | { |
| 516 | struct thermal_hwmon_temp *temp; |
| 517 | |
| 518 | mutex_lock(&thermal_list_lock); |
| 519 | list_for_each_entry(temp, &hwmon->tz_list, hwmon_node) |
| 520 | if (temp->tz == tz) { |
| 521 | mutex_unlock(&thermal_list_lock); |
| 522 | return temp; |
| 523 | } |
| 524 | mutex_unlock(&thermal_list_lock); |
| 525 | |
| 526 | return NULL; |
| 527 | } |
| 528 | |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 529 | static int |
| 530 | thermal_add_hwmon_sysfs(struct thermal_zone_device *tz) |
| 531 | { |
| 532 | struct thermal_hwmon_device *hwmon; |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 533 | struct thermal_hwmon_temp *temp; |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 534 | int new_hwmon_device = 1; |
| 535 | int result; |
| 536 | |
Jean Delvare | 0d97d7a | 2011-07-28 13:48:41 -0700 | [diff] [blame] | 537 | hwmon = thermal_hwmon_lookup_by_type(tz); |
| 538 | if (hwmon) { |
| 539 | new_hwmon_device = 0; |
| 540 | goto register_sys_interface; |
| 541 | } |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 542 | |
| 543 | hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL); |
| 544 | if (!hwmon) |
| 545 | return -ENOMEM; |
| 546 | |
| 547 | INIT_LIST_HEAD(&hwmon->tz_list); |
| 548 | strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH); |
| 549 | hwmon->device = hwmon_device_register(NULL); |
| 550 | if (IS_ERR(hwmon->device)) { |
| 551 | result = PTR_ERR(hwmon->device); |
| 552 | goto free_mem; |
| 553 | } |
Greg Kroah-Hartman | 0e968a3 | 2009-04-30 14:43:31 -0700 | [diff] [blame] | 554 | dev_set_drvdata(hwmon->device, hwmon); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 555 | result = device_create_file(hwmon->device, &dev_attr_name); |
| 556 | if (result) |
Durgadoss R | b299eb5 | 2011-03-03 04:30:13 +0530 | [diff] [blame] | 557 | goto free_mem; |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 558 | |
| 559 | register_sys_interface: |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 560 | temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL); |
| 561 | if (!temp) { |
| 562 | result = -ENOMEM; |
| 563 | goto unregister_name; |
| 564 | } |
| 565 | |
| 566 | temp->tz = tz; |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 567 | hwmon->count++; |
| 568 | |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 569 | snprintf(temp->temp_input.name, THERMAL_NAME_LENGTH, |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 570 | "temp%d_input", hwmon->count); |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 571 | temp->temp_input.attr.attr.name = temp->temp_input.name; |
| 572 | temp->temp_input.attr.attr.mode = 0444; |
| 573 | temp->temp_input.attr.show = temp_input_show; |
| 574 | sysfs_attr_init(&temp->temp_input.attr.attr); |
| 575 | result = device_create_file(hwmon->device, &temp->temp_input.attr); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 576 | if (result) |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 577 | goto free_temp_mem; |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 578 | |
| 579 | if (tz->ops->get_crit_temp) { |
| 580 | unsigned long temperature; |
| 581 | if (!tz->ops->get_crit_temp(tz, &temperature)) { |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 582 | snprintf(temp->temp_crit.name, THERMAL_NAME_LENGTH, |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 583 | "temp%d_crit", hwmon->count); |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 584 | temp->temp_crit.attr.attr.name = temp->temp_crit.name; |
| 585 | temp->temp_crit.attr.attr.mode = 0444; |
| 586 | temp->temp_crit.attr.show = temp_crit_show; |
| 587 | sysfs_attr_init(&temp->temp_crit.attr.attr); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 588 | result = device_create_file(hwmon->device, |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 589 | &temp->temp_crit.attr); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 590 | if (result) |
Durgadoss R | b299eb5 | 2011-03-03 04:30:13 +0530 | [diff] [blame] | 591 | goto unregister_input; |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 592 | } |
| 593 | } |
| 594 | |
| 595 | mutex_lock(&thermal_list_lock); |
| 596 | if (new_hwmon_device) |
| 597 | list_add_tail(&hwmon->node, &thermal_hwmon_list); |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 598 | list_add_tail(&temp->hwmon_node, &hwmon->tz_list); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 599 | mutex_unlock(&thermal_list_lock); |
| 600 | |
| 601 | return 0; |
| 602 | |
Durgadoss R | b299eb5 | 2011-03-03 04:30:13 +0530 | [diff] [blame] | 603 | unregister_input: |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 604 | device_remove_file(hwmon->device, &temp->temp_input.attr); |
| 605 | free_temp_mem: |
| 606 | kfree(temp); |
Durgadoss R | b299eb5 | 2011-03-03 04:30:13 +0530 | [diff] [blame] | 607 | unregister_name: |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 608 | if (new_hwmon_device) { |
| 609 | device_remove_file(hwmon->device, &dev_attr_name); |
| 610 | hwmon_device_unregister(hwmon->device); |
| 611 | } |
| 612 | free_mem: |
| 613 | if (new_hwmon_device) |
| 614 | kfree(hwmon); |
| 615 | |
| 616 | return result; |
| 617 | } |
| 618 | |
| 619 | static void |
| 620 | thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz) |
| 621 | { |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 622 | struct thermal_hwmon_device *hwmon; |
| 623 | struct thermal_hwmon_temp *temp; |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 624 | |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 625 | hwmon = thermal_hwmon_lookup_by_type(tz); |
| 626 | if (unlikely(!hwmon)) { |
| 627 | /* Should never happen... */ |
| 628 | dev_dbg(&tz->device, "hwmon device lookup failed!\n"); |
| 629 | return; |
| 630 | } |
| 631 | |
| 632 | temp = thermal_hwmon_lookup_temp(hwmon, tz); |
| 633 | if (unlikely(!temp)) { |
| 634 | /* Should never happen... */ |
| 635 | dev_dbg(&tz->device, "temperature input lookup failed!\n"); |
| 636 | return; |
| 637 | } |
| 638 | |
| 639 | device_remove_file(hwmon->device, &temp->temp_input.attr); |
Durgadoss R | b299eb5 | 2011-03-03 04:30:13 +0530 | [diff] [blame] | 640 | if (tz->ops->get_crit_temp) |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 641 | device_remove_file(hwmon->device, &temp->temp_crit.attr); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 642 | |
| 643 | mutex_lock(&thermal_list_lock); |
Jean Delvare | 31f5396 | 2011-07-28 13:48:42 -0700 | [diff] [blame] | 644 | list_del(&temp->hwmon_node); |
| 645 | kfree(temp); |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 646 | if (!list_empty(&hwmon->tz_list)) { |
| 647 | mutex_unlock(&thermal_list_lock); |
| 648 | return; |
| 649 | } |
| 650 | list_del(&hwmon->node); |
| 651 | mutex_unlock(&thermal_list_lock); |
| 652 | |
| 653 | device_remove_file(hwmon->device, &dev_attr_name); |
| 654 | hwmon_device_unregister(hwmon->device); |
| 655 | kfree(hwmon); |
| 656 | } |
| 657 | #else |
| 658 | static int |
| 659 | thermal_add_hwmon_sysfs(struct thermal_zone_device *tz) |
| 660 | { |
| 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | static void |
| 665 | thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz) |
| 666 | { |
| 667 | } |
| 668 | #endif |
| 669 | |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 670 | static void thermal_zone_device_set_polling(struct thermal_zone_device *tz, |
| 671 | int delay) |
| 672 | { |
| 673 | cancel_delayed_work(&(tz->poll_queue)); |
| 674 | |
| 675 | if (!delay) |
| 676 | return; |
| 677 | |
| 678 | if (delay > 1000) |
Rafael J. Wysocki | 51e20d0 | 2011-11-06 14:21:38 +0100 | [diff] [blame] | 679 | queue_delayed_work(system_freezable_wq, &(tz->poll_queue), |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 680 | round_jiffies(msecs_to_jiffies(delay))); |
| 681 | else |
Rafael J. Wysocki | 51e20d0 | 2011-11-06 14:21:38 +0100 | [diff] [blame] | 682 | queue_delayed_work(system_freezable_wq, &(tz->poll_queue), |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 683 | msecs_to_jiffies(delay)); |
| 684 | } |
| 685 | |
| 686 | static void thermal_zone_device_passive(struct thermal_zone_device *tz, |
| 687 | int temp, int trip_temp, int trip) |
| 688 | { |
| 689 | int trend = 0; |
| 690 | struct thermal_cooling_device_instance *instance; |
| 691 | struct thermal_cooling_device *cdev; |
| 692 | long state, max_state; |
| 693 | |
| 694 | /* |
| 695 | * Above Trip? |
| 696 | * ----------- |
| 697 | * Calculate the thermal trend (using the passive cooling equation) |
| 698 | * and modify the performance limit for all passive cooling devices |
| 699 | * accordingly. Note that we assume symmetry. |
| 700 | */ |
| 701 | if (temp >= trip_temp) { |
| 702 | tz->passive = true; |
| 703 | |
| 704 | trend = (tz->tc1 * (temp - tz->last_temperature)) + |
| 705 | (tz->tc2 * (temp - trip_temp)); |
| 706 | |
| 707 | /* Heating up? */ |
| 708 | if (trend > 0) { |
| 709 | list_for_each_entry(instance, &tz->cooling_devices, |
| 710 | node) { |
| 711 | if (instance->trip != trip) |
| 712 | continue; |
| 713 | cdev = instance->cdev; |
| 714 | cdev->ops->get_cur_state(cdev, &state); |
| 715 | cdev->ops->get_max_state(cdev, &max_state); |
| 716 | if (state++ < max_state) |
| 717 | cdev->ops->set_cur_state(cdev, state); |
| 718 | } |
| 719 | } else if (trend < 0) { /* Cooling off? */ |
| 720 | list_for_each_entry(instance, &tz->cooling_devices, |
| 721 | node) { |
| 722 | if (instance->trip != trip) |
| 723 | continue; |
| 724 | cdev = instance->cdev; |
| 725 | cdev->ops->get_cur_state(cdev, &state); |
| 726 | cdev->ops->get_max_state(cdev, &max_state); |
| 727 | if (state > 0) |
| 728 | cdev->ops->set_cur_state(cdev, --state); |
| 729 | } |
| 730 | } |
| 731 | return; |
| 732 | } |
| 733 | |
| 734 | /* |
| 735 | * Below Trip? |
| 736 | * ----------- |
| 737 | * Implement passive cooling hysteresis to slowly increase performance |
| 738 | * and avoid thrashing around the passive trip point. Note that we |
| 739 | * assume symmetry. |
| 740 | */ |
| 741 | list_for_each_entry(instance, &tz->cooling_devices, node) { |
| 742 | if (instance->trip != trip) |
| 743 | continue; |
| 744 | cdev = instance->cdev; |
| 745 | cdev->ops->get_cur_state(cdev, &state); |
| 746 | cdev->ops->get_max_state(cdev, &max_state); |
| 747 | if (state > 0) |
| 748 | cdev->ops->set_cur_state(cdev, --state); |
| 749 | if (state == 0) |
| 750 | tz->passive = false; |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | static void thermal_zone_device_check(struct work_struct *work) |
| 755 | { |
| 756 | struct thermal_zone_device *tz = container_of(work, struct |
| 757 | thermal_zone_device, |
| 758 | poll_queue.work); |
| 759 | thermal_zone_device_update(tz); |
| 760 | } |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 761 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 762 | /** |
| 763 | * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 764 | * @tz: thermal zone device |
| 765 | * @trip: indicates which trip point the cooling devices is |
| 766 | * associated with in this thermal zone. |
| 767 | * @cdev: thermal cooling device |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 768 | * |
| 769 | * This function is usually called in the thermal zone device .bind callback. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 770 | */ |
| 771 | int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz, |
| 772 | int trip, |
| 773 | struct thermal_cooling_device *cdev) |
| 774 | { |
| 775 | struct thermal_cooling_device_instance *dev; |
| 776 | struct thermal_cooling_device_instance *pos; |
Thomas Sujith | c751670 | 2008-02-15 00:58:50 -0500 | [diff] [blame] | 777 | struct thermal_zone_device *pos1; |
| 778 | struct thermal_cooling_device *pos2; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 779 | int result; |
| 780 | |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 781 | if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE)) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 782 | return -EINVAL; |
| 783 | |
Thomas Sujith | c751670 | 2008-02-15 00:58:50 -0500 | [diff] [blame] | 784 | list_for_each_entry(pos1, &thermal_tz_list, node) { |
| 785 | if (pos1 == tz) |
| 786 | break; |
| 787 | } |
| 788 | list_for_each_entry(pos2, &thermal_cdev_list, node) { |
| 789 | if (pos2 == cdev) |
| 790 | break; |
| 791 | } |
| 792 | |
| 793 | if (tz != pos1 || cdev != pos2) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 794 | return -EINVAL; |
| 795 | |
| 796 | dev = |
| 797 | kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL); |
| 798 | if (!dev) |
| 799 | return -ENOMEM; |
| 800 | dev->tz = tz; |
| 801 | dev->cdev = cdev; |
| 802 | dev->trip = trip; |
| 803 | result = get_idr(&tz->idr, &tz->lock, &dev->id); |
| 804 | if (result) |
| 805 | goto free_mem; |
| 806 | |
| 807 | sprintf(dev->name, "cdev%d", dev->id); |
| 808 | result = |
| 809 | sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name); |
| 810 | if (result) |
| 811 | goto release_idr; |
| 812 | |
| 813 | sprintf(dev->attr_name, "cdev%d_trip_point", dev->id); |
Sergey Senozhatsky | 975f8c5 | 2010-04-06 14:34:51 -0700 | [diff] [blame] | 814 | sysfs_attr_init(&dev->attr.attr); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 815 | dev->attr.attr.name = dev->attr_name; |
| 816 | dev->attr.attr.mode = 0444; |
| 817 | dev->attr.show = thermal_cooling_device_trip_point_show; |
| 818 | result = device_create_file(&tz->device, &dev->attr); |
| 819 | if (result) |
| 820 | goto remove_symbol_link; |
| 821 | |
| 822 | mutex_lock(&tz->lock); |
| 823 | list_for_each_entry(pos, &tz->cooling_devices, node) |
| 824 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
| 825 | result = -EEXIST; |
| 826 | break; |
| 827 | } |
| 828 | if (!result) |
| 829 | list_add_tail(&dev->node, &tz->cooling_devices); |
| 830 | mutex_unlock(&tz->lock); |
| 831 | |
| 832 | if (!result) |
| 833 | return 0; |
| 834 | |
| 835 | device_remove_file(&tz->device, &dev->attr); |
| 836 | remove_symbol_link: |
| 837 | sysfs_remove_link(&tz->device.kobj, dev->name); |
| 838 | release_idr: |
| 839 | release_idr(&tz->idr, &tz->lock, dev->id); |
| 840 | free_mem: |
| 841 | kfree(dev); |
| 842 | return result; |
| 843 | } |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 844 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 845 | EXPORT_SYMBOL(thermal_zone_bind_cooling_device); |
| 846 | |
| 847 | /** |
| 848 | * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 849 | * @tz: thermal zone device |
| 850 | * @trip: indicates which trip point the cooling devices is |
| 851 | * associated with in this thermal zone. |
| 852 | * @cdev: thermal cooling device |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 853 | * |
| 854 | * This function is usually called in the thermal zone device .unbind callback. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 855 | */ |
| 856 | int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz, |
| 857 | int trip, |
| 858 | struct thermal_cooling_device *cdev) |
| 859 | { |
| 860 | struct thermal_cooling_device_instance *pos, *next; |
| 861 | |
| 862 | mutex_lock(&tz->lock); |
| 863 | list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) { |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 864 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 865 | list_del(&pos->node); |
| 866 | mutex_unlock(&tz->lock); |
| 867 | goto unbind; |
| 868 | } |
| 869 | } |
| 870 | mutex_unlock(&tz->lock); |
| 871 | |
| 872 | return -ENODEV; |
| 873 | |
| 874 | unbind: |
| 875 | device_remove_file(&tz->device, &pos->attr); |
| 876 | sysfs_remove_link(&tz->device.kobj, pos->name); |
| 877 | release_idr(&tz->idr, &tz->lock, pos->id); |
| 878 | kfree(pos); |
| 879 | return 0; |
| 880 | } |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 881 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 882 | EXPORT_SYMBOL(thermal_zone_unbind_cooling_device); |
| 883 | |
| 884 | static void thermal_release(struct device *dev) |
| 885 | { |
| 886 | struct thermal_zone_device *tz; |
| 887 | struct thermal_cooling_device *cdev; |
| 888 | |
Kay Sievers | 354655e | 2009-01-06 10:44:37 -0800 | [diff] [blame] | 889 | if (!strncmp(dev_name(dev), "thermal_zone", sizeof "thermal_zone" - 1)) { |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 890 | tz = to_thermal_zone(dev); |
| 891 | kfree(tz); |
| 892 | } else { |
| 893 | cdev = to_cooling_device(dev); |
| 894 | kfree(cdev); |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | static struct class thermal_class = { |
| 899 | .name = "thermal", |
| 900 | .dev_release = thermal_release, |
| 901 | }; |
| 902 | |
| 903 | /** |
| 904 | * thermal_cooling_device_register - register a new thermal cooling device |
| 905 | * @type: the thermal cooling device type. |
| 906 | * @devdata: device private data. |
| 907 | * @ops: standard thermal cooling devices callbacks. |
| 908 | */ |
Alan Cox | 5b275ce | 2010-11-11 15:27:29 +0000 | [diff] [blame] | 909 | struct thermal_cooling_device *thermal_cooling_device_register( |
| 910 | char *type, void *devdata, const struct thermal_cooling_device_ops *ops) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 911 | { |
| 912 | struct thermal_cooling_device *cdev; |
| 913 | struct thermal_zone_device *pos; |
| 914 | int result; |
| 915 | |
| 916 | if (strlen(type) >= THERMAL_NAME_LENGTH) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 917 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 918 | |
| 919 | if (!ops || !ops->get_max_state || !ops->get_cur_state || |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 920 | !ops->set_cur_state) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 921 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 922 | |
| 923 | cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL); |
| 924 | if (!cdev) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 925 | return ERR_PTR(-ENOMEM); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 926 | |
| 927 | result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id); |
| 928 | if (result) { |
| 929 | kfree(cdev); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 930 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | strcpy(cdev->type, type); |
| 934 | cdev->ops = ops; |
| 935 | cdev->device.class = &thermal_class; |
| 936 | cdev->devdata = devdata; |
Kay Sievers | 354655e | 2009-01-06 10:44:37 -0800 | [diff] [blame] | 937 | dev_set_name(&cdev->device, "cooling_device%d", cdev->id); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 938 | result = device_register(&cdev->device); |
| 939 | if (result) { |
| 940 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); |
| 941 | kfree(cdev); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 942 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | /* sys I/F */ |
| 946 | if (type) { |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 947 | result = device_create_file(&cdev->device, &dev_attr_cdev_type); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 948 | if (result) |
| 949 | goto unregister; |
| 950 | } |
| 951 | |
| 952 | result = device_create_file(&cdev->device, &dev_attr_max_state); |
| 953 | if (result) |
| 954 | goto unregister; |
| 955 | |
| 956 | result = device_create_file(&cdev->device, &dev_attr_cur_state); |
| 957 | if (result) |
| 958 | goto unregister; |
| 959 | |
| 960 | mutex_lock(&thermal_list_lock); |
| 961 | list_add(&cdev->node, &thermal_cdev_list); |
| 962 | list_for_each_entry(pos, &thermal_tz_list, node) { |
| 963 | if (!pos->ops->bind) |
| 964 | continue; |
| 965 | result = pos->ops->bind(pos, cdev); |
| 966 | if (result) |
| 967 | break; |
| 968 | |
| 969 | } |
| 970 | mutex_unlock(&thermal_list_lock); |
| 971 | |
| 972 | if (!result) |
| 973 | return cdev; |
| 974 | |
| 975 | unregister: |
| 976 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); |
| 977 | device_unregister(&cdev->device); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 978 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 979 | } |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 980 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 981 | EXPORT_SYMBOL(thermal_cooling_device_register); |
| 982 | |
| 983 | /** |
| 984 | * thermal_cooling_device_unregister - removes the registered thermal cooling device |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 985 | * @cdev: the thermal cooling device to remove. |
| 986 | * |
| 987 | * thermal_cooling_device_unregister() must be called when the device is no |
| 988 | * longer needed. |
| 989 | */ |
| 990 | void thermal_cooling_device_unregister(struct |
| 991 | thermal_cooling_device |
| 992 | *cdev) |
| 993 | { |
| 994 | struct thermal_zone_device *tz; |
| 995 | struct thermal_cooling_device *pos = NULL; |
| 996 | |
| 997 | if (!cdev) |
| 998 | return; |
| 999 | |
| 1000 | mutex_lock(&thermal_list_lock); |
| 1001 | list_for_each_entry(pos, &thermal_cdev_list, node) |
| 1002 | if (pos == cdev) |
| 1003 | break; |
| 1004 | if (pos != cdev) { |
| 1005 | /* thermal cooling device not found */ |
| 1006 | mutex_unlock(&thermal_list_lock); |
| 1007 | return; |
| 1008 | } |
| 1009 | list_del(&cdev->node); |
| 1010 | list_for_each_entry(tz, &thermal_tz_list, node) { |
| 1011 | if (!tz->ops->unbind) |
| 1012 | continue; |
| 1013 | tz->ops->unbind(tz, cdev); |
| 1014 | } |
| 1015 | mutex_unlock(&thermal_list_lock); |
| 1016 | if (cdev->type[0]) |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 1017 | device_remove_file(&cdev->device, &dev_attr_cdev_type); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1018 | device_remove_file(&cdev->device, &dev_attr_max_state); |
| 1019 | device_remove_file(&cdev->device, &dev_attr_cur_state); |
| 1020 | |
| 1021 | release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id); |
| 1022 | device_unregister(&cdev->device); |
| 1023 | return; |
| 1024 | } |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 1025 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1026 | EXPORT_SYMBOL(thermal_cooling_device_unregister); |
| 1027 | |
| 1028 | /** |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1029 | * thermal_zone_device_update - force an update of a thermal zone's state |
| 1030 | * @ttz: the thermal zone to update |
| 1031 | */ |
| 1032 | |
| 1033 | void thermal_zone_device_update(struct thermal_zone_device *tz) |
| 1034 | { |
| 1035 | int count, ret = 0; |
| 1036 | long temp, trip_temp; |
| 1037 | enum thermal_trip_type trip_type; |
| 1038 | struct thermal_cooling_device_instance *instance; |
| 1039 | struct thermal_cooling_device *cdev; |
| 1040 | |
| 1041 | mutex_lock(&tz->lock); |
| 1042 | |
Michael Brunner | 0d28816 | 2009-08-26 14:29:25 -0700 | [diff] [blame] | 1043 | if (tz->ops->get_temp(tz, &temp)) { |
| 1044 | /* get_temp failed - retry it later */ |
| 1045 | printk(KERN_WARNING PREFIX "failed to read out thermal zone " |
| 1046 | "%d\n", tz->id); |
| 1047 | goto leave; |
| 1048 | } |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1049 | |
| 1050 | for (count = 0; count < tz->trips; count++) { |
| 1051 | tz->ops->get_trip_type(tz, count, &trip_type); |
| 1052 | tz->ops->get_trip_temp(tz, count, &trip_temp); |
| 1053 | |
| 1054 | switch (trip_type) { |
| 1055 | case THERMAL_TRIP_CRITICAL: |
Vladimir Zajac | 2932135 | 2009-05-06 19:34:21 +0200 | [diff] [blame] | 1056 | if (temp >= trip_temp) { |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1057 | if (tz->ops->notify) |
| 1058 | ret = tz->ops->notify(tz, count, |
| 1059 | trip_type); |
| 1060 | if (!ret) { |
| 1061 | printk(KERN_EMERG |
| 1062 | "Critical temperature reached (%ld C), shutting down.\n", |
| 1063 | temp/1000); |
| 1064 | orderly_poweroff(true); |
| 1065 | } |
| 1066 | } |
| 1067 | break; |
| 1068 | case THERMAL_TRIP_HOT: |
Vladimir Zajac | 2932135 | 2009-05-06 19:34:21 +0200 | [diff] [blame] | 1069 | if (temp >= trip_temp) |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1070 | if (tz->ops->notify) |
| 1071 | tz->ops->notify(tz, count, trip_type); |
| 1072 | break; |
| 1073 | case THERMAL_TRIP_ACTIVE: |
| 1074 | list_for_each_entry(instance, &tz->cooling_devices, |
| 1075 | node) { |
| 1076 | if (instance->trip != count) |
| 1077 | continue; |
| 1078 | |
| 1079 | cdev = instance->cdev; |
| 1080 | |
Vladimir Zajac | 2932135 | 2009-05-06 19:34:21 +0200 | [diff] [blame] | 1081 | if (temp >= trip_temp) |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1082 | cdev->ops->set_cur_state(cdev, 1); |
| 1083 | else |
| 1084 | cdev->ops->set_cur_state(cdev, 0); |
| 1085 | } |
| 1086 | break; |
| 1087 | case THERMAL_TRIP_PASSIVE: |
Vladimir Zajac | 2932135 | 2009-05-06 19:34:21 +0200 | [diff] [blame] | 1088 | if (temp >= trip_temp || tz->passive) |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1089 | thermal_zone_device_passive(tz, temp, |
| 1090 | trip_temp, count); |
| 1091 | break; |
| 1092 | } |
| 1093 | } |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 1094 | |
| 1095 | if (tz->forced_passive) |
| 1096 | thermal_zone_device_passive(tz, temp, tz->forced_passive, |
| 1097 | THERMAL_TRIPS_NONE); |
| 1098 | |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1099 | tz->last_temperature = temp; |
Michael Brunner | 0d28816 | 2009-08-26 14:29:25 -0700 | [diff] [blame] | 1100 | |
| 1101 | leave: |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1102 | if (tz->passive) |
| 1103 | thermal_zone_device_set_polling(tz, tz->passive_delay); |
| 1104 | else if (tz->polling_delay) |
| 1105 | thermal_zone_device_set_polling(tz, tz->polling_delay); |
Frans Pop | 3767cb5 | 2009-10-26 08:39:04 +0100 | [diff] [blame] | 1106 | else |
| 1107 | thermal_zone_device_set_polling(tz, 0); |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1108 | mutex_unlock(&tz->lock); |
| 1109 | } |
| 1110 | EXPORT_SYMBOL(thermal_zone_device_update); |
| 1111 | |
| 1112 | /** |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1113 | * thermal_zone_device_register - register a new thermal zone device |
| 1114 | * @type: the thermal zone device type |
| 1115 | * @trips: the number of trip points the thermal zone support |
| 1116 | * @devdata: private device data |
| 1117 | * @ops: standard thermal zone device callbacks |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1118 | * @tc1: thermal coefficient 1 for passive calculations |
| 1119 | * @tc2: thermal coefficient 2 for passive calculations |
| 1120 | * @passive_delay: number of milliseconds to wait between polls when |
| 1121 | * performing passive cooling |
| 1122 | * @polling_delay: number of milliseconds to wait between polls when checking |
| 1123 | * whether trip points have been crossed (0 for interrupt |
| 1124 | * driven systems) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1125 | * |
| 1126 | * thermal_zone_device_unregister() must be called when the device is no |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1127 | * longer needed. The passive cooling formula uses tc1 and tc2 as described in |
| 1128 | * section 11.1.5.1 of the ACPI specification 3.0. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1129 | */ |
| 1130 | struct thermal_zone_device *thermal_zone_device_register(char *type, |
Alan Cox | 5b275ce | 2010-11-11 15:27:29 +0000 | [diff] [blame] | 1131 | int trips, void *devdata, |
| 1132 | const struct thermal_zone_device_ops *ops, |
| 1133 | int tc1, int tc2, int passive_delay, int polling_delay) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1134 | { |
| 1135 | struct thermal_zone_device *tz; |
| 1136 | struct thermal_cooling_device *pos; |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 1137 | enum thermal_trip_type trip_type; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1138 | int result; |
| 1139 | int count; |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 1140 | int passive = 0; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1141 | |
| 1142 | if (strlen(type) >= THERMAL_NAME_LENGTH) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1143 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1144 | |
| 1145 | if (trips > THERMAL_MAX_TRIPS || trips < 0) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1146 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1147 | |
| 1148 | if (!ops || !ops->get_temp) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1149 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1150 | |
| 1151 | tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL); |
| 1152 | if (!tz) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1153 | return ERR_PTR(-ENOMEM); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1154 | |
| 1155 | INIT_LIST_HEAD(&tz->cooling_devices); |
| 1156 | idr_init(&tz->idr); |
| 1157 | mutex_init(&tz->lock); |
| 1158 | result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id); |
| 1159 | if (result) { |
| 1160 | kfree(tz); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1161 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | strcpy(tz->type, type); |
| 1165 | tz->ops = ops; |
| 1166 | tz->device.class = &thermal_class; |
| 1167 | tz->devdata = devdata; |
| 1168 | tz->trips = trips; |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1169 | tz->tc1 = tc1; |
| 1170 | tz->tc2 = tc2; |
| 1171 | tz->passive_delay = passive_delay; |
| 1172 | tz->polling_delay = polling_delay; |
| 1173 | |
Kay Sievers | 354655e | 2009-01-06 10:44:37 -0800 | [diff] [blame] | 1174 | dev_set_name(&tz->device, "thermal_zone%d", tz->id); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1175 | result = device_register(&tz->device); |
| 1176 | if (result) { |
| 1177 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); |
| 1178 | kfree(tz); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1179 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | /* sys I/F */ |
| 1183 | if (type) { |
| 1184 | result = device_create_file(&tz->device, &dev_attr_type); |
| 1185 | if (result) |
| 1186 | goto unregister; |
| 1187 | } |
| 1188 | |
| 1189 | result = device_create_file(&tz->device, &dev_attr_temp); |
| 1190 | if (result) |
| 1191 | goto unregister; |
| 1192 | |
| 1193 | if (ops->get_mode) { |
| 1194 | result = device_create_file(&tz->device, &dev_attr_mode); |
| 1195 | if (result) |
| 1196 | goto unregister; |
| 1197 | } |
| 1198 | |
| 1199 | for (count = 0; count < trips; count++) { |
| 1200 | TRIP_POINT_ATTR_ADD(&tz->device, count, result); |
| 1201 | if (result) |
| 1202 | goto unregister; |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 1203 | tz->ops->get_trip_type(tz, count, &trip_type); |
| 1204 | if (trip_type == THERMAL_TRIP_PASSIVE) |
| 1205 | passive = 1; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1206 | } |
| 1207 | |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 1208 | if (!passive) |
| 1209 | result = device_create_file(&tz->device, |
| 1210 | &dev_attr_passive); |
| 1211 | |
| 1212 | if (result) |
| 1213 | goto unregister; |
| 1214 | |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 1215 | result = thermal_add_hwmon_sysfs(tz); |
| 1216 | if (result) |
| 1217 | goto unregister; |
| 1218 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1219 | mutex_lock(&thermal_list_lock); |
| 1220 | list_add_tail(&tz->node, &thermal_tz_list); |
| 1221 | if (ops->bind) |
| 1222 | list_for_each_entry(pos, &thermal_cdev_list, node) { |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 1223 | result = ops->bind(tz, pos); |
| 1224 | if (result) |
| 1225 | break; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1226 | } |
| 1227 | mutex_unlock(&thermal_list_lock); |
| 1228 | |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1229 | INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check); |
| 1230 | |
| 1231 | thermal_zone_device_update(tz); |
| 1232 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1233 | if (!result) |
| 1234 | return tz; |
| 1235 | |
| 1236 | unregister: |
| 1237 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); |
| 1238 | device_unregister(&tz->device); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1239 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1240 | } |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 1241 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1242 | EXPORT_SYMBOL(thermal_zone_device_register); |
| 1243 | |
| 1244 | /** |
| 1245 | * thermal_device_unregister - removes the registered thermal zone device |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1246 | * @tz: the thermal zone device to remove |
| 1247 | */ |
| 1248 | void thermal_zone_device_unregister(struct thermal_zone_device *tz) |
| 1249 | { |
| 1250 | struct thermal_cooling_device *cdev; |
| 1251 | struct thermal_zone_device *pos = NULL; |
| 1252 | int count; |
| 1253 | |
| 1254 | if (!tz) |
| 1255 | return; |
| 1256 | |
| 1257 | mutex_lock(&thermal_list_lock); |
| 1258 | list_for_each_entry(pos, &thermal_tz_list, node) |
| 1259 | if (pos == tz) |
| 1260 | break; |
| 1261 | if (pos != tz) { |
| 1262 | /* thermal zone device not found */ |
| 1263 | mutex_unlock(&thermal_list_lock); |
| 1264 | return; |
| 1265 | } |
| 1266 | list_del(&tz->node); |
| 1267 | if (tz->ops->unbind) |
| 1268 | list_for_each_entry(cdev, &thermal_cdev_list, node) |
| 1269 | tz->ops->unbind(tz, cdev); |
| 1270 | mutex_unlock(&thermal_list_lock); |
| 1271 | |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1272 | thermal_zone_device_set_polling(tz, 0); |
| 1273 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1274 | if (tz->type[0]) |
| 1275 | device_remove_file(&tz->device, &dev_attr_type); |
| 1276 | device_remove_file(&tz->device, &dev_attr_temp); |
| 1277 | if (tz->ops->get_mode) |
| 1278 | device_remove_file(&tz->device, &dev_attr_mode); |
| 1279 | |
| 1280 | for (count = 0; count < tz->trips; count++) |
| 1281 | TRIP_POINT_ATTR_REMOVE(&tz->device, count); |
| 1282 | |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 1283 | thermal_remove_hwmon_sysfs(tz); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1284 | release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id); |
| 1285 | idr_destroy(&tz->idr); |
| 1286 | mutex_destroy(&tz->lock); |
| 1287 | device_unregister(&tz->device); |
| 1288 | return; |
| 1289 | } |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 1290 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1291 | EXPORT_SYMBOL(thermal_zone_device_unregister); |
| 1292 | |
Rafael J. Wysocki | af06216 | 2011-03-01 01:12:19 +0100 | [diff] [blame] | 1293 | #ifdef CONFIG_NET |
| 1294 | static struct genl_family thermal_event_genl_family = { |
| 1295 | .id = GENL_ID_GENERATE, |
| 1296 | .name = THERMAL_GENL_FAMILY_NAME, |
| 1297 | .version = THERMAL_GENL_VERSION, |
| 1298 | .maxattr = THERMAL_GENL_ATTR_MAX, |
| 1299 | }; |
| 1300 | |
| 1301 | static struct genl_multicast_group thermal_event_mcgrp = { |
| 1302 | .name = THERMAL_GENL_MCAST_GROUP_NAME, |
| 1303 | }; |
| 1304 | |
Jean Delvare | 2d58d7e | 2011-11-04 10:31:04 +0100 | [diff] [blame] | 1305 | int thermal_generate_netlink_event(u32 orig, enum events event) |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1306 | { |
| 1307 | struct sk_buff *skb; |
| 1308 | struct nlattr *attr; |
| 1309 | struct thermal_genl_event *thermal_event; |
| 1310 | void *msg_header; |
| 1311 | int size; |
| 1312 | int result; |
Fabio Estevam | b11de07 | 2012-03-21 12:55:00 -0700 | [diff] [blame^] | 1313 | static unsigned int thermal_event_seqnum; |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1314 | |
| 1315 | /* allocate memory */ |
| 1316 | size = nla_total_size(sizeof(struct thermal_genl_event)) + \ |
| 1317 | nla_total_size(0); |
| 1318 | |
| 1319 | skb = genlmsg_new(size, GFP_ATOMIC); |
| 1320 | if (!skb) |
| 1321 | return -ENOMEM; |
| 1322 | |
| 1323 | /* add the genetlink message header */ |
| 1324 | msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++, |
| 1325 | &thermal_event_genl_family, 0, |
| 1326 | THERMAL_GENL_CMD_EVENT); |
| 1327 | if (!msg_header) { |
| 1328 | nlmsg_free(skb); |
| 1329 | return -ENOMEM; |
| 1330 | } |
| 1331 | |
| 1332 | /* fill the data */ |
| 1333 | attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT, \ |
| 1334 | sizeof(struct thermal_genl_event)); |
| 1335 | |
| 1336 | if (!attr) { |
| 1337 | nlmsg_free(skb); |
| 1338 | return -EINVAL; |
| 1339 | } |
| 1340 | |
| 1341 | thermal_event = nla_data(attr); |
| 1342 | if (!thermal_event) { |
| 1343 | nlmsg_free(skb); |
| 1344 | return -EINVAL; |
| 1345 | } |
| 1346 | |
| 1347 | memset(thermal_event, 0, sizeof(struct thermal_genl_event)); |
| 1348 | |
| 1349 | thermal_event->orig = orig; |
| 1350 | thermal_event->event = event; |
| 1351 | |
| 1352 | /* send multicast genetlink message */ |
| 1353 | result = genlmsg_end(skb, msg_header); |
| 1354 | if (result < 0) { |
| 1355 | nlmsg_free(skb); |
| 1356 | return result; |
| 1357 | } |
| 1358 | |
| 1359 | result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC); |
| 1360 | if (result) |
| 1361 | printk(KERN_INFO "failed to send netlink event:%d", result); |
| 1362 | |
| 1363 | return result; |
| 1364 | } |
Jean Delvare | 2d58d7e | 2011-11-04 10:31:04 +0100 | [diff] [blame] | 1365 | EXPORT_SYMBOL(thermal_generate_netlink_event); |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1366 | |
| 1367 | static int genetlink_init(void) |
| 1368 | { |
| 1369 | int result; |
| 1370 | |
| 1371 | result = genl_register_family(&thermal_event_genl_family); |
| 1372 | if (result) |
| 1373 | return result; |
| 1374 | |
| 1375 | result = genl_register_mc_group(&thermal_event_genl_family, |
| 1376 | &thermal_event_mcgrp); |
| 1377 | if (result) |
| 1378 | genl_unregister_family(&thermal_event_genl_family); |
| 1379 | return result; |
| 1380 | } |
| 1381 | |
Rafael J. Wysocki | af06216 | 2011-03-01 01:12:19 +0100 | [diff] [blame] | 1382 | static void genetlink_exit(void) |
| 1383 | { |
| 1384 | genl_unregister_family(&thermal_event_genl_family); |
| 1385 | } |
| 1386 | #else /* !CONFIG_NET */ |
| 1387 | static inline int genetlink_init(void) { return 0; } |
| 1388 | static inline void genetlink_exit(void) {} |
| 1389 | #endif /* !CONFIG_NET */ |
| 1390 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1391 | static int __init thermal_init(void) |
| 1392 | { |
| 1393 | int result = 0; |
| 1394 | |
| 1395 | result = class_register(&thermal_class); |
| 1396 | if (result) { |
| 1397 | idr_destroy(&thermal_tz_idr); |
| 1398 | idr_destroy(&thermal_cdev_idr); |
| 1399 | mutex_destroy(&thermal_idr_lock); |
| 1400 | mutex_destroy(&thermal_list_lock); |
| 1401 | } |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1402 | result = genetlink_init(); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1403 | return result; |
| 1404 | } |
| 1405 | |
| 1406 | static void __exit thermal_exit(void) |
| 1407 | { |
| 1408 | class_unregister(&thermal_class); |
| 1409 | idr_destroy(&thermal_tz_idr); |
| 1410 | idr_destroy(&thermal_cdev_idr); |
| 1411 | mutex_destroy(&thermal_idr_lock); |
| 1412 | mutex_destroy(&thermal_list_lock); |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1413 | genetlink_exit(); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1414 | } |
| 1415 | |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1416 | fs_initcall(thermal_init); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1417 | module_exit(thermal_exit); |