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