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