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