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