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