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