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