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