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