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