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