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