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