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