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 | * |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; version 2 of the License. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 11 | */ |
| 12 | |
Joe Perches | c5a01dd | 2012-03-21 12:55:02 -0700 | [diff] [blame] | 13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 14 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/device.h> |
| 17 | #include <linux/err.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 19 | #include <linux/kdev_t.h> |
| 20 | #include <linux/idr.h> |
| 21 | #include <linux/thermal.h> |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 22 | #include <linux/reboot.h> |
Andy Shevchenko | 42a5bf5 | 2013-05-17 11:52:02 +0000 | [diff] [blame] | 23 | #include <linux/string.h> |
Eduardo Valentin | a116b5d | 2013-09-26 15:55:01 -0400 | [diff] [blame] | 24 | #include <linux/of.h> |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 25 | #include <net/netlink.h> |
| 26 | #include <net/genetlink.h> |
Zhang Rui | ff140fe | 2015-10-30 16:31:58 +0800 | [diff] [blame] | 27 | #include <linux/suspend.h> |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 28 | |
Punit Agrawal | 100a8fd | 2014-07-29 11:50:48 +0100 | [diff] [blame] | 29 | #define CREATE_TRACE_POINTS |
| 30 | #include <trace/events/thermal.h> |
| 31 | |
Durgadoss R | 71350db | 2012-09-18 11:04:53 +0530 | [diff] [blame] | 32 | #include "thermal_core.h" |
Eduardo Valentin | 0dd8879 | 2013-07-03 15:14:28 -0400 | [diff] [blame] | 33 | #include "thermal_hwmon.h" |
Durgadoss R | 71350db | 2012-09-18 11:04:53 +0530 | [diff] [blame] | 34 | |
Zhang Rui | 63c4ec9 | 2008-04-21 16:07:13 +0800 | [diff] [blame] | 35 | MODULE_AUTHOR("Zhang Rui"); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 36 | MODULE_DESCRIPTION("Generic thermal management sysfs support"); |
Eduardo Valentin | 6d8d497 | 2013-04-23 21:48:13 +0000 | [diff] [blame] | 37 | MODULE_LICENSE("GPL v2"); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 38 | |
Matthew Wilcox | b31ef82 | 2016-12-21 09:47:03 -0800 | [diff] [blame^] | 39 | static DEFINE_IDA(thermal_tz_ida); |
| 40 | static DEFINE_IDA(thermal_cdev_ida); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 41 | |
| 42 | static LIST_HEAD(thermal_tz_list); |
| 43 | static LIST_HEAD(thermal_cdev_list); |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 44 | static LIST_HEAD(thermal_governor_list); |
| 45 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 46 | static DEFINE_MUTEX(thermal_list_lock); |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 47 | static DEFINE_MUTEX(thermal_governor_lock); |
| 48 | |
Zhang Rui | ff140fe | 2015-10-30 16:31:58 +0800 | [diff] [blame] | 49 | static atomic_t in_suspend; |
| 50 | |
Zhang Rui | f2234bc | 2014-01-24 10:23:19 +0800 | [diff] [blame] | 51 | static struct thermal_governor *def_governor; |
| 52 | |
Eduardo Valentin | 1b4f484 | 2016-11-07 21:09:05 -0800 | [diff] [blame] | 53 | /* |
| 54 | * Governor section: set of functions to handle thermal governors |
| 55 | * |
| 56 | * Functions to help in the life cycle of thermal governors within |
| 57 | * the thermal core and by the thermal governor code. |
| 58 | */ |
| 59 | |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 60 | static struct thermal_governor *__find_governor(const char *name) |
| 61 | { |
| 62 | struct thermal_governor *pos; |
| 63 | |
Zhang Rui | f2234bc | 2014-01-24 10:23:19 +0800 | [diff] [blame] | 64 | if (!name || !name[0]) |
| 65 | return def_governor; |
| 66 | |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 67 | list_for_each_entry(pos, &thermal_governor_list, governor_list) |
Rasmus Villemoes | 484ac2f | 2014-10-13 15:55:01 -0700 | [diff] [blame] | 68 | if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH)) |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 69 | return pos; |
| 70 | |
| 71 | return NULL; |
| 72 | } |
| 73 | |
Javi Merino | e33df1d | 2015-02-26 19:00:27 +0000 | [diff] [blame] | 74 | /** |
| 75 | * bind_previous_governor() - bind the previous governor of the thermal zone |
| 76 | * @tz: a valid pointer to a struct thermal_zone_device |
| 77 | * @failed_gov_name: the name of the governor that failed to register |
| 78 | * |
| 79 | * Register the previous governor of the thermal zone after a new |
| 80 | * governor has failed to be bound. |
| 81 | */ |
| 82 | static void bind_previous_governor(struct thermal_zone_device *tz, |
| 83 | const char *failed_gov_name) |
| 84 | { |
| 85 | if (tz->governor && tz->governor->bind_to_tz) { |
| 86 | if (tz->governor->bind_to_tz(tz)) { |
| 87 | dev_err(&tz->device, |
| 88 | "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n", |
| 89 | failed_gov_name, tz->governor->name, tz->type); |
| 90 | tz->governor = NULL; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * thermal_set_governor() - Switch to another governor |
| 97 | * @tz: a valid pointer to a struct thermal_zone_device |
| 98 | * @new_gov: pointer to the new governor |
| 99 | * |
| 100 | * Change the governor of thermal zone @tz. |
| 101 | * |
| 102 | * Return: 0 on success, an error if the new governor's bind_to_tz() failed. |
| 103 | */ |
| 104 | static int thermal_set_governor(struct thermal_zone_device *tz, |
| 105 | struct thermal_governor *new_gov) |
| 106 | { |
| 107 | int ret = 0; |
| 108 | |
| 109 | if (tz->governor && tz->governor->unbind_from_tz) |
| 110 | tz->governor->unbind_from_tz(tz); |
| 111 | |
| 112 | if (new_gov && new_gov->bind_to_tz) { |
| 113 | ret = new_gov->bind_to_tz(tz); |
| 114 | if (ret) { |
| 115 | bind_previous_governor(tz, new_gov->name); |
| 116 | |
| 117 | return ret; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | tz->governor = new_gov; |
| 122 | |
| 123 | return ret; |
| 124 | } |
| 125 | |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 126 | int thermal_register_governor(struct thermal_governor *governor) |
| 127 | { |
| 128 | int err; |
| 129 | const char *name; |
| 130 | struct thermal_zone_device *pos; |
| 131 | |
| 132 | if (!governor) |
| 133 | return -EINVAL; |
| 134 | |
| 135 | mutex_lock(&thermal_governor_lock); |
| 136 | |
| 137 | err = -EBUSY; |
Eduardo Valentin | 5027ba3 | 2016-11-07 21:09:20 -0800 | [diff] [blame] | 138 | if (!__find_governor(governor->name)) { |
Eduardo Valentin | eb7be32 | 2016-11-07 21:09:21 -0800 | [diff] [blame] | 139 | bool match_default; |
| 140 | |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 141 | err = 0; |
| 142 | list_add(&governor->governor_list, &thermal_governor_list); |
Eduardo Valentin | eb7be32 | 2016-11-07 21:09:21 -0800 | [diff] [blame] | 143 | match_default = !strncmp(governor->name, |
| 144 | DEFAULT_THERMAL_GOVERNOR, |
| 145 | THERMAL_NAME_LENGTH); |
| 146 | |
| 147 | if (!def_governor && match_default) |
Zhang Rui | f2234bc | 2014-01-24 10:23:19 +0800 | [diff] [blame] | 148 | def_governor = governor; |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | mutex_lock(&thermal_list_lock); |
| 152 | |
| 153 | list_for_each_entry(pos, &thermal_tz_list, node) { |
Zhang Rui | f2234bc | 2014-01-24 10:23:19 +0800 | [diff] [blame] | 154 | /* |
| 155 | * only thermal zones with specified tz->tzp->governor_name |
| 156 | * may run with tz->govenor unset |
| 157 | */ |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 158 | if (pos->governor) |
| 159 | continue; |
Zhang Rui | f2234bc | 2014-01-24 10:23:19 +0800 | [diff] [blame] | 160 | |
| 161 | name = pos->tzp->governor_name; |
| 162 | |
Javi Merino | e33df1d | 2015-02-26 19:00:27 +0000 | [diff] [blame] | 163 | if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) { |
| 164 | int ret; |
| 165 | |
| 166 | ret = thermal_set_governor(pos, governor); |
| 167 | if (ret) |
| 168 | dev_err(&pos->device, |
| 169 | "Failed to set governor %s for thermal zone %s: %d\n", |
| 170 | governor->name, pos->type, ret); |
| 171 | } |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | mutex_unlock(&thermal_list_lock); |
| 175 | mutex_unlock(&thermal_governor_lock); |
| 176 | |
| 177 | return err; |
| 178 | } |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 179 | |
| 180 | void thermal_unregister_governor(struct thermal_governor *governor) |
| 181 | { |
| 182 | struct thermal_zone_device *pos; |
| 183 | |
| 184 | if (!governor) |
| 185 | return; |
| 186 | |
| 187 | mutex_lock(&thermal_governor_lock); |
| 188 | |
Eduardo Valentin | 5027ba3 | 2016-11-07 21:09:20 -0800 | [diff] [blame] | 189 | if (!__find_governor(governor->name)) |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 190 | goto exit; |
| 191 | |
| 192 | mutex_lock(&thermal_list_lock); |
| 193 | |
| 194 | list_for_each_entry(pos, &thermal_tz_list, node) { |
Rasmus Villemoes | 484ac2f | 2014-10-13 15:55:01 -0700 | [diff] [blame] | 195 | if (!strncasecmp(pos->governor->name, governor->name, |
Eduardo Valentin | eb7be32 | 2016-11-07 21:09:21 -0800 | [diff] [blame] | 196 | THERMAL_NAME_LENGTH)) |
Javi Merino | e33df1d | 2015-02-26 19:00:27 +0000 | [diff] [blame] | 197 | thermal_set_governor(pos, NULL); |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | mutex_unlock(&thermal_list_lock); |
| 201 | list_del(&governor->governor_list); |
| 202 | exit: |
| 203 | mutex_unlock(&thermal_governor_lock); |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 204 | } |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 205 | |
Eduardo Valentin | 1b4f484 | 2016-11-07 21:09:05 -0800 | [diff] [blame] | 206 | int thermal_zone_device_set_policy(struct thermal_zone_device *tz, |
| 207 | char *policy) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 208 | { |
Eduardo Valentin | 1b4f484 | 2016-11-07 21:09:05 -0800 | [diff] [blame] | 209 | struct thermal_governor *gov; |
| 210 | int ret = -EINVAL; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 211 | |
Eduardo Valentin | 1b4f484 | 2016-11-07 21:09:05 -0800 | [diff] [blame] | 212 | mutex_lock(&thermal_governor_lock); |
Durgadoss R | 9b4298a | 2012-09-18 11:04:54 +0530 | [diff] [blame] | 213 | mutex_lock(&tz->lock); |
Durgadoss R | 9b4298a | 2012-09-18 11:04:54 +0530 | [diff] [blame] | 214 | |
Eduardo Valentin | 1b4f484 | 2016-11-07 21:09:05 -0800 | [diff] [blame] | 215 | gov = __find_governor(strim(policy)); |
| 216 | if (!gov) |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 217 | goto exit; |
| 218 | |
Eduardo Valentin | 1b4f484 | 2016-11-07 21:09:05 -0800 | [diff] [blame] | 219 | ret = thermal_set_governor(tz, gov); |
| 220 | |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 221 | exit: |
Eduardo Valentin | 1b4f484 | 2016-11-07 21:09:05 -0800 | [diff] [blame] | 222 | mutex_unlock(&tz->lock); |
| 223 | mutex_unlock(&thermal_governor_lock); |
| 224 | |
| 225 | return ret; |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 226 | } |
| 227 | |
Eduardo Valentin | 1b4f484 | 2016-11-07 21:09:05 -0800 | [diff] [blame] | 228 | int thermal_build_list_of_policies(char *buf) |
| 229 | { |
| 230 | struct thermal_governor *pos; |
| 231 | ssize_t count = 0; |
| 232 | ssize_t size = PAGE_SIZE; |
| 233 | |
| 234 | mutex_lock(&thermal_governor_lock); |
| 235 | |
| 236 | list_for_each_entry(pos, &thermal_governor_list, governor_list) { |
| 237 | size = PAGE_SIZE - count; |
| 238 | count += scnprintf(buf + count, size, "%s ", pos->name); |
| 239 | } |
| 240 | count += scnprintf(buf + count, size, "\n"); |
| 241 | |
| 242 | mutex_unlock(&thermal_governor_lock); |
| 243 | |
| 244 | return count; |
| 245 | } |
| 246 | |
| 247 | static int __init thermal_register_governors(void) |
| 248 | { |
| 249 | int result; |
| 250 | |
| 251 | result = thermal_gov_step_wise_register(); |
| 252 | if (result) |
| 253 | return result; |
| 254 | |
| 255 | result = thermal_gov_fair_share_register(); |
| 256 | if (result) |
| 257 | return result; |
| 258 | |
| 259 | result = thermal_gov_bang_bang_register(); |
| 260 | if (result) |
| 261 | return result; |
| 262 | |
| 263 | result = thermal_gov_user_space_register(); |
| 264 | if (result) |
| 265 | return result; |
| 266 | |
| 267 | return thermal_gov_power_allocator_register(); |
| 268 | } |
| 269 | |
| 270 | static void thermal_unregister_governors(void) |
| 271 | { |
| 272 | thermal_gov_step_wise_unregister(); |
| 273 | thermal_gov_fair_share_unregister(); |
| 274 | thermal_gov_bang_bang_unregister(); |
| 275 | thermal_gov_user_space_unregister(); |
| 276 | thermal_gov_power_allocator_unregister(); |
| 277 | } |
| 278 | |
Eduardo Valentin | 8772e18 | 2016-11-07 21:09:15 -0800 | [diff] [blame] | 279 | /* |
| 280 | * Zone update section: main control loop applied to each zone while monitoring |
| 281 | * |
| 282 | * in polling mode. The monitoring is done using a workqueue. |
| 283 | * Same update may be done on a zone by calling thermal_zone_device_update(). |
| 284 | * |
| 285 | * An update means: |
| 286 | * - Non-critical trips will invoke the governor responsible for that zone; |
| 287 | * - Hot trips will produce a notification to userspace; |
| 288 | * - Critical trip point will cause a system shutdown. |
| 289 | */ |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 290 | static void thermal_zone_device_set_polling(struct thermal_zone_device *tz, |
| 291 | int delay) |
| 292 | { |
| 293 | if (delay > 1000) |
| 294 | mod_delayed_work(system_freezable_wq, &tz->poll_queue, |
| 295 | round_jiffies(msecs_to_jiffies(delay))); |
| 296 | else if (delay) |
| 297 | mod_delayed_work(system_freezable_wq, &tz->poll_queue, |
| 298 | msecs_to_jiffies(delay)); |
| 299 | else |
| 300 | cancel_delayed_work(&tz->poll_queue); |
| 301 | } |
| 302 | |
| 303 | static void monitor_thermal_zone(struct thermal_zone_device *tz) |
| 304 | { |
| 305 | mutex_lock(&tz->lock); |
| 306 | |
| 307 | if (tz->passive) |
| 308 | thermal_zone_device_set_polling(tz, tz->passive_delay); |
| 309 | else if (tz->polling_delay) |
| 310 | thermal_zone_device_set_polling(tz, tz->polling_delay); |
| 311 | else |
| 312 | thermal_zone_device_set_polling(tz, 0); |
| 313 | |
| 314 | mutex_unlock(&tz->lock); |
| 315 | } |
| 316 | |
| 317 | static void handle_non_critical_trips(struct thermal_zone_device *tz, |
Eduardo Valentin | eb7be32 | 2016-11-07 21:09:21 -0800 | [diff] [blame] | 318 | int trip, |
| 319 | enum thermal_trip_type trip_type) |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 320 | { |
Zhang Rui | f2234bc | 2014-01-24 10:23:19 +0800 | [diff] [blame] | 321 | tz->governor ? tz->governor->throttle(tz, trip) : |
| 322 | def_governor->throttle(tz, trip); |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | static void handle_critical_trips(struct thermal_zone_device *tz, |
Eduardo Valentin | eb7be32 | 2016-11-07 21:09:21 -0800 | [diff] [blame] | 326 | int trip, enum thermal_trip_type trip_type) |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 327 | { |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 328 | int trip_temp; |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 329 | |
| 330 | tz->ops->get_trip_temp(tz, trip, &trip_temp); |
| 331 | |
| 332 | /* If we have not crossed the trip_temp, we do not care. */ |
Srinivas Pandruvada | 84ffe3e | 2014-11-12 15:43:29 -0800 | [diff] [blame] | 333 | if (trip_temp <= 0 || tz->temperature < trip_temp) |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 334 | return; |
| 335 | |
Punit Agrawal | 208cd82 | 2014-07-29 11:50:50 +0100 | [diff] [blame] | 336 | trace_thermal_zone_trip(tz, trip, trip_type); |
| 337 | |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 338 | if (tz->ops->notify) |
| 339 | tz->ops->notify(tz, trip, trip_type); |
| 340 | |
| 341 | if (trip_type == THERMAL_TRIP_CRITICAL) { |
Eduardo Valentin | 923e0b1 | 2013-01-02 15:29:41 +0000 | [diff] [blame] | 342 | dev_emerg(&tz->device, |
| 343 | "critical temperature reached(%d C),shutting down\n", |
| 344 | tz->temperature / 1000); |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 345 | orderly_poweroff(true); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | static void handle_thermal_trip(struct thermal_zone_device *tz, int trip) |
| 350 | { |
| 351 | enum thermal_trip_type type; |
| 352 | |
Zhang Rui | 81ad427 | 2016-03-18 10:03:24 +0800 | [diff] [blame] | 353 | /* Ignore disabled trip points */ |
| 354 | if (test_bit(trip, &tz->trips_disabled)) |
| 355 | return; |
| 356 | |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 357 | tz->ops->get_trip_type(tz, trip, &type); |
| 358 | |
| 359 | if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT) |
| 360 | handle_critical_trips(tz, trip, type); |
| 361 | else |
| 362 | handle_non_critical_trips(tz, trip, type); |
| 363 | /* |
| 364 | * Alright, we handled this trip successfully. |
| 365 | * So, start monitoring again. |
| 366 | */ |
| 367 | monitor_thermal_zone(tz); |
| 368 | } |
| 369 | |
| 370 | static void update_temperature(struct thermal_zone_device *tz) |
| 371 | { |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 372 | int temp, ret; |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 373 | |
Amit Daniel Kachhap | e6e238c | 2013-02-04 00:30:15 +0000 | [diff] [blame] | 374 | ret = thermal_zone_get_temp(tz, &temp); |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 375 | if (ret) { |
Hans de Goede | 7e497a7 | 2015-03-21 15:02:55 +0100 | [diff] [blame] | 376 | if (ret != -EAGAIN) |
| 377 | dev_warn(&tz->device, |
| 378 | "failed to read out thermal zone (%d)\n", |
| 379 | ret); |
Amit Daniel Kachhap | e6e238c | 2013-02-04 00:30:15 +0000 | [diff] [blame] | 380 | return; |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 381 | } |
| 382 | |
Amit Daniel Kachhap | e6e238c | 2013-02-04 00:30:15 +0000 | [diff] [blame] | 383 | mutex_lock(&tz->lock); |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 384 | tz->last_temperature = tz->temperature; |
| 385 | tz->temperature = temp; |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 386 | mutex_unlock(&tz->lock); |
Aaron Lu | 06475b5 | 2013-12-02 13:54:26 +0800 | [diff] [blame] | 387 | |
Punit Agrawal | 100a8fd | 2014-07-29 11:50:48 +0100 | [diff] [blame] | 388 | trace_thermal_temperature(tz); |
Zhang Rui | bb431ba | 2015-10-30 16:31:47 +0800 | [diff] [blame] | 389 | if (tz->last_temperature == THERMAL_TEMP_INVALID) |
| 390 | dev_dbg(&tz->device, "last_temperature N/A, current_temperature=%d\n", |
| 391 | tz->temperature); |
| 392 | else |
| 393 | dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n", |
| 394 | tz->last_temperature, tz->temperature); |
| 395 | } |
| 396 | |
| 397 | static void thermal_zone_device_reset(struct thermal_zone_device *tz) |
| 398 | { |
| 399 | struct thermal_instance *pos; |
| 400 | |
| 401 | tz->temperature = THERMAL_TEMP_INVALID; |
| 402 | tz->passive = 0; |
| 403 | list_for_each_entry(pos, &tz->thermal_instances, tz_node) |
| 404 | pos->initialized = false; |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 405 | } |
| 406 | |
Srinivas Pandruvada | 0e70f46 | 2016-08-26 16:21:16 -0700 | [diff] [blame] | 407 | void thermal_zone_device_update(struct thermal_zone_device *tz, |
| 408 | enum thermal_notify_event event) |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 409 | { |
| 410 | int count; |
| 411 | |
Zhang Rui | ff140fe | 2015-10-30 16:31:58 +0800 | [diff] [blame] | 412 | if (atomic_read(&in_suspend)) |
| 413 | return; |
| 414 | |
Eduardo Valentin | 81bd4e1 | 2013-09-12 19:15:44 -0400 | [diff] [blame] | 415 | if (!tz->ops->get_temp) |
| 416 | return; |
| 417 | |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 418 | update_temperature(tz); |
| 419 | |
Sascha Hauer | 060c034 | 2016-06-22 16:42:01 +0800 | [diff] [blame] | 420 | thermal_zone_set_trips(tz); |
| 421 | |
Srinivas Pandruvada | 0e70f46 | 2016-08-26 16:21:16 -0700 | [diff] [blame] | 422 | tz->notify_event = event; |
| 423 | |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 424 | for (count = 0; count < tz->trips; count++) |
| 425 | handle_thermal_trip(tz, count); |
| 426 | } |
Eduardo Valentin | 910cb1e | 2013-04-23 21:48:15 +0000 | [diff] [blame] | 427 | EXPORT_SYMBOL_GPL(thermal_zone_device_update); |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 428 | |
Eduardo Valentin | 106339a | 2016-11-07 21:09:14 -0800 | [diff] [blame] | 429 | /** |
| 430 | * thermal_notify_framework - Sensor drivers use this API to notify framework |
| 431 | * @tz: thermal zone device |
| 432 | * @trip: indicates which trip point has been crossed |
| 433 | * |
| 434 | * This function handles the trip events from sensor drivers. It starts |
| 435 | * throttling the cooling devices according to the policy configured. |
| 436 | * For CRITICAL and HOT trip points, this notifies the respective drivers, |
| 437 | * and does actual throttling for other trip points i.e ACTIVE and PASSIVE. |
| 438 | * The throttling policy is based on the configured platform data; if no |
| 439 | * platform data is provided, this uses the step_wise throttling policy. |
| 440 | */ |
| 441 | void thermal_notify_framework(struct thermal_zone_device *tz, int trip) |
| 442 | { |
| 443 | handle_thermal_trip(tz, trip); |
| 444 | } |
| 445 | EXPORT_SYMBOL_GPL(thermal_notify_framework); |
| 446 | |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 447 | static void thermal_zone_device_check(struct work_struct *work) |
| 448 | { |
| 449 | struct thermal_zone_device *tz = container_of(work, struct |
| 450 | thermal_zone_device, |
| 451 | poll_queue.work); |
Srinivas Pandruvada | 0e70f46 | 2016-08-26 16:21:16 -0700 | [diff] [blame] | 452 | thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); |
Durgadoss R | 0c01ebb | 2012-09-18 11:05:04 +0530 | [diff] [blame] | 453 | } |
| 454 | |
Eduardo Valentin | 712afbd | 2016-11-07 21:09:16 -0800 | [diff] [blame] | 455 | /* |
| 456 | * Power actor section: interface to power actors to estimate power |
| 457 | * |
| 458 | * Set of functions used to interact to cooling devices that know |
| 459 | * how to estimate their devices power consumption. |
| 460 | */ |
Javi Merino | 9f38271 | 2015-03-26 15:53:02 +0000 | [diff] [blame] | 461 | |
Javi Merino | 35b11d2 | 2015-02-26 19:00:28 +0000 | [diff] [blame] | 462 | /** |
| 463 | * power_actor_get_max_power() - get the maximum power that a cdev can consume |
| 464 | * @cdev: pointer to &thermal_cooling_device |
| 465 | * @tz: a valid thermal zone device pointer |
| 466 | * @max_power: pointer in which to store the maximum power |
| 467 | * |
| 468 | * Calculate the maximum power consumption in milliwats that the |
| 469 | * cooling device can currently consume and store it in @max_power. |
| 470 | * |
| 471 | * Return: 0 on success, -EINVAL if @cdev doesn't support the |
| 472 | * power_actor API or -E* on other error. |
| 473 | */ |
| 474 | int power_actor_get_max_power(struct thermal_cooling_device *cdev, |
| 475 | struct thermal_zone_device *tz, u32 *max_power) |
| 476 | { |
| 477 | if (!cdev_is_power_actor(cdev)) |
| 478 | return -EINVAL; |
| 479 | |
| 480 | return cdev->ops->state2power(cdev, tz, 0, max_power); |
| 481 | } |
| 482 | |
| 483 | /** |
Javi Merino | c973c3b | 2015-09-14 14:23:50 +0100 | [diff] [blame] | 484 | * power_actor_get_min_power() - get the mainimum power that a cdev can consume |
| 485 | * @cdev: pointer to &thermal_cooling_device |
| 486 | * @tz: a valid thermal zone device pointer |
| 487 | * @min_power: pointer in which to store the minimum power |
| 488 | * |
| 489 | * Calculate the minimum power consumption in milliwatts that the |
| 490 | * cooling device can currently consume and store it in @min_power. |
| 491 | * |
| 492 | * Return: 0 on success, -EINVAL if @cdev doesn't support the |
| 493 | * power_actor API or -E* on other error. |
| 494 | */ |
| 495 | int power_actor_get_min_power(struct thermal_cooling_device *cdev, |
| 496 | struct thermal_zone_device *tz, u32 *min_power) |
| 497 | { |
| 498 | unsigned long max_state; |
| 499 | int ret; |
| 500 | |
| 501 | if (!cdev_is_power_actor(cdev)) |
| 502 | return -EINVAL; |
| 503 | |
| 504 | ret = cdev->ops->get_max_state(cdev, &max_state); |
| 505 | if (ret) |
| 506 | return ret; |
| 507 | |
| 508 | return cdev->ops->state2power(cdev, tz, max_state, min_power); |
| 509 | } |
| 510 | |
| 511 | /** |
Eduardo Valentin | 1a7e7cc | 2016-11-07 21:08:47 -0800 | [diff] [blame] | 512 | * power_actor_set_power() - limit the maximum power a cooling device consumes |
Javi Merino | 35b11d2 | 2015-02-26 19:00:28 +0000 | [diff] [blame] | 513 | * @cdev: pointer to &thermal_cooling_device |
| 514 | * @instance: thermal instance to update |
| 515 | * @power: the power in milliwatts |
| 516 | * |
Eduardo Valentin | 1a7e7cc | 2016-11-07 21:08:47 -0800 | [diff] [blame] | 517 | * Set the cooling device to consume at most @power milliwatts. The limit is |
| 518 | * expected to be a cap at the maximum power consumption. |
Javi Merino | 35b11d2 | 2015-02-26 19:00:28 +0000 | [diff] [blame] | 519 | * |
| 520 | * Return: 0 on success, -EINVAL if the cooling device does not |
| 521 | * implement the power actor API or -E* for other failures. |
| 522 | */ |
| 523 | int power_actor_set_power(struct thermal_cooling_device *cdev, |
| 524 | struct thermal_instance *instance, u32 power) |
| 525 | { |
| 526 | unsigned long state; |
| 527 | int ret; |
| 528 | |
| 529 | if (!cdev_is_power_actor(cdev)) |
| 530 | return -EINVAL; |
| 531 | |
| 532 | ret = cdev->ops->power2state(cdev, instance->tz, power, &state); |
| 533 | if (ret) |
| 534 | return ret; |
| 535 | |
| 536 | instance->target = state; |
Michele Di Giorgio | d0b7306 | 2016-06-02 15:25:31 +0100 | [diff] [blame] | 537 | mutex_lock(&cdev->lock); |
Javi Merino | 35b11d2 | 2015-02-26 19:00:28 +0000 | [diff] [blame] | 538 | cdev->updated = false; |
Michele Di Giorgio | d0b7306 | 2016-06-02 15:25:31 +0100 | [diff] [blame] | 539 | mutex_unlock(&cdev->lock); |
Javi Merino | 35b11d2 | 2015-02-26 19:00:28 +0000 | [diff] [blame] | 540 | thermal_cdev_update(cdev); |
| 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
Eduardo Valentin | 3d0055d | 2016-11-07 21:08:54 -0800 | [diff] [blame] | 545 | void thermal_zone_device_rebind_exception(struct thermal_zone_device *tz, |
| 546 | const char *cdev_type, size_t size) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 547 | { |
Eduardo Valentin | 3d0055d | 2016-11-07 21:08:54 -0800 | [diff] [blame] | 548 | struct thermal_cooling_device *cdev = NULL; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 549 | |
Eduardo Valentin | 3d0055d | 2016-11-07 21:08:54 -0800 | [diff] [blame] | 550 | mutex_lock(&thermal_list_lock); |
| 551 | list_for_each_entry(cdev, &thermal_cdev_list, node) { |
| 552 | /* skip non matching cdevs */ |
| 553 | if (strncmp(cdev_type, cdev->type, size)) |
| 554 | continue; |
| 555 | |
| 556 | /* re binding the exception matching the type pattern */ |
| 557 | thermal_zone_bind_cooling_device(tz, THERMAL_TRIPS_NONE, cdev, |
| 558 | THERMAL_NO_LIMIT, |
| 559 | THERMAL_NO_LIMIT, |
| 560 | THERMAL_WEIGHT_DEFAULT); |
| 561 | } |
| 562 | mutex_unlock(&thermal_list_lock); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 563 | } |
| 564 | |
Eduardo Valentin | 3d0055d | 2016-11-07 21:08:54 -0800 | [diff] [blame] | 565 | void thermal_zone_device_unbind_exception(struct thermal_zone_device *tz, |
| 566 | const char *cdev_type, size_t size) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 567 | { |
Eduardo Valentin | 3d0055d | 2016-11-07 21:08:54 -0800 | [diff] [blame] | 568 | struct thermal_cooling_device *cdev = NULL; |
| 569 | |
| 570 | mutex_lock(&thermal_list_lock); |
| 571 | list_for_each_entry(cdev, &thermal_cdev_list, node) { |
| 572 | /* skip non matching cdevs */ |
| 573 | if (strncmp(cdev_type, cdev->type, size)) |
| 574 | continue; |
| 575 | /* unbinding the exception matching the type pattern */ |
| 576 | thermal_zone_unbind_cooling_device(tz, THERMAL_TRIPS_NONE, |
| 577 | cdev); |
| 578 | } |
| 579 | mutex_unlock(&thermal_list_lock); |
| 580 | } |
| 581 | |
Eduardo Valentin | 81193e2 | 2016-11-07 21:09:17 -0800 | [diff] [blame] | 582 | /* |
| 583 | * Device management section: cooling devices, zones devices, and binding |
| 584 | * |
| 585 | * Set of functions provided by the thermal core for: |
| 586 | * - cooling devices lifecycle: registration, unregistration, |
| 587 | * binding, and unbinding. |
| 588 | * - thermal zone devices lifecycle: registration, unregistration, |
| 589 | * binding, and unbinding. |
| 590 | */ |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 591 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 592 | /** |
Eduardo Valentin | d2e4eb8 | 2013-04-23 21:48:16 +0000 | [diff] [blame] | 593 | * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone |
| 594 | * @tz: pointer to struct thermal_zone_device |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 595 | * @trip: indicates which trip point the cooling devices is |
| 596 | * associated with in this thermal zone. |
Eduardo Valentin | d2e4eb8 | 2013-04-23 21:48:16 +0000 | [diff] [blame] | 597 | * @cdev: pointer to struct thermal_cooling_device |
| 598 | * @upper: the Maximum cooling state for this trip point. |
| 599 | * THERMAL_NO_LIMIT means no upper limit, |
| 600 | * and the cooling device can be in max_state. |
| 601 | * @lower: the Minimum cooling state can be used for this trip point. |
| 602 | * THERMAL_NO_LIMIT means no lower limit, |
| 603 | * and the cooling device can be in cooling state 0. |
Kapileshwar Singh | 6cd9e9f | 2015-02-18 16:04:21 +0000 | [diff] [blame] | 604 | * @weight: The weight of the cooling device to be bound to the |
| 605 | * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the |
| 606 | * default value |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 607 | * |
Eduardo Valentin | d2e4eb8 | 2013-04-23 21:48:16 +0000 | [diff] [blame] | 608 | * This interface function bind a thermal cooling device to the certain trip |
| 609 | * point of a thermal zone device. |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 610 | * This function is usually called in the thermal zone device .bind callback. |
Eduardo Valentin | d2e4eb8 | 2013-04-23 21:48:16 +0000 | [diff] [blame] | 611 | * |
| 612 | * Return: 0 on success, the proper error value otherwise. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 613 | */ |
| 614 | int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz, |
| 615 | int trip, |
Zhang Rui | 9d99842 | 2012-06-26 16:35:57 +0800 | [diff] [blame] | 616 | struct thermal_cooling_device *cdev, |
Kapileshwar Singh | 6cd9e9f | 2015-02-18 16:04:21 +0000 | [diff] [blame] | 617 | unsigned long upper, unsigned long lower, |
| 618 | unsigned int weight) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 619 | { |
Zhang Rui | b81b6ba | 2012-06-27 10:08:19 +0800 | [diff] [blame] | 620 | struct thermal_instance *dev; |
| 621 | struct thermal_instance *pos; |
Thomas Sujith | c751670 | 2008-02-15 00:58:50 -0500 | [diff] [blame] | 622 | struct thermal_zone_device *pos1; |
| 623 | struct thermal_cooling_device *pos2; |
Zhang Rui | 74051ba | 2012-06-26 16:27:22 +0800 | [diff] [blame] | 624 | unsigned long max_state; |
Lukasz Majewski | 9a3031d | 2014-11-18 11:16:30 +0100 | [diff] [blame] | 625 | int result, ret; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 626 | |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 627 | if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE)) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 628 | return -EINVAL; |
| 629 | |
Thomas Sujith | c751670 | 2008-02-15 00:58:50 -0500 | [diff] [blame] | 630 | list_for_each_entry(pos1, &thermal_tz_list, node) { |
| 631 | if (pos1 == tz) |
| 632 | break; |
| 633 | } |
| 634 | list_for_each_entry(pos2, &thermal_cdev_list, node) { |
| 635 | if (pos2 == cdev) |
| 636 | break; |
| 637 | } |
| 638 | |
| 639 | if (tz != pos1 || cdev != pos2) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 640 | return -EINVAL; |
| 641 | |
Lukasz Majewski | 9a3031d | 2014-11-18 11:16:30 +0100 | [diff] [blame] | 642 | ret = cdev->ops->get_max_state(cdev, &max_state); |
| 643 | if (ret) |
| 644 | return ret; |
Zhang Rui | 9d99842 | 2012-06-26 16:35:57 +0800 | [diff] [blame] | 645 | |
| 646 | /* lower default 0, upper default max_state */ |
| 647 | lower = lower == THERMAL_NO_LIMIT ? 0 : lower; |
| 648 | upper = upper == THERMAL_NO_LIMIT ? max_state : upper; |
| 649 | |
| 650 | if (lower > upper || upper > max_state) |
| 651 | return -EINVAL; |
| 652 | |
Eduardo Valentin | 95e3ed1 | 2016-11-07 21:09:25 -0800 | [diff] [blame] | 653 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 654 | if (!dev) |
| 655 | return -ENOMEM; |
| 656 | dev->tz = tz; |
| 657 | dev->cdev = cdev; |
| 658 | dev->trip = trip; |
Zhang Rui | 9d99842 | 2012-06-26 16:35:57 +0800 | [diff] [blame] | 659 | dev->upper = upper; |
| 660 | dev->lower = lower; |
Zhang Rui | ce119f8 | 2012-06-27 14:13:04 +0800 | [diff] [blame] | 661 | dev->target = THERMAL_NO_TARGET; |
Kapileshwar Singh | 6cd9e9f | 2015-02-18 16:04:21 +0000 | [diff] [blame] | 662 | dev->weight = weight; |
Zhang Rui | 74051ba | 2012-06-26 16:27:22 +0800 | [diff] [blame] | 663 | |
Matthew Wilcox | b31ef82 | 2016-12-21 09:47:03 -0800 | [diff] [blame^] | 664 | result = ida_simple_get(&tz->ida, 0, 0, GFP_KERNEL); |
| 665 | if (result < 0) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 666 | goto free_mem; |
| 667 | |
Matthew Wilcox | b31ef82 | 2016-12-21 09:47:03 -0800 | [diff] [blame^] | 668 | dev->id = result; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 669 | sprintf(dev->name, "cdev%d", dev->id); |
| 670 | result = |
| 671 | sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name); |
| 672 | if (result) |
Matthew Wilcox | b31ef82 | 2016-12-21 09:47:03 -0800 | [diff] [blame^] | 673 | goto release_ida; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 674 | |
| 675 | sprintf(dev->attr_name, "cdev%d_trip_point", dev->id); |
Sergey Senozhatsky | 975f8c5 | 2010-04-06 14:34:51 -0700 | [diff] [blame] | 676 | sysfs_attr_init(&dev->attr.attr); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 677 | dev->attr.attr.name = dev->attr_name; |
| 678 | dev->attr.attr.mode = 0444; |
| 679 | dev->attr.show = thermal_cooling_device_trip_point_show; |
| 680 | result = device_create_file(&tz->device, &dev->attr); |
| 681 | if (result) |
| 682 | goto remove_symbol_link; |
| 683 | |
Javi Merino | db91651 | 2015-02-18 16:04:24 +0000 | [diff] [blame] | 684 | sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id); |
| 685 | sysfs_attr_init(&dev->weight_attr.attr); |
| 686 | dev->weight_attr.attr.name = dev->weight_attr_name; |
| 687 | dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO; |
| 688 | dev->weight_attr.show = thermal_cooling_device_weight_show; |
| 689 | dev->weight_attr.store = thermal_cooling_device_weight_store; |
| 690 | result = device_create_file(&tz->device, &dev->weight_attr); |
| 691 | if (result) |
| 692 | goto remove_trip_file; |
| 693 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 694 | mutex_lock(&tz->lock); |
Zhang Rui | f4a821c | 2012-07-24 16:56:21 +0800 | [diff] [blame] | 695 | mutex_lock(&cdev->lock); |
Zhang Rui | cddf31b | 2012-06-27 10:09:36 +0800 | [diff] [blame] | 696 | list_for_each_entry(pos, &tz->thermal_instances, tz_node) |
Eduardo Valentin | b659a30 | 2016-11-07 21:09:23 -0800 | [diff] [blame] | 697 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
| 698 | result = -EEXIST; |
| 699 | break; |
| 700 | } |
Zhang Rui | b5e4ae6 | 2012-06-27 14:11:52 +0800 | [diff] [blame] | 701 | if (!result) { |
Zhang Rui | cddf31b | 2012-06-27 10:09:36 +0800 | [diff] [blame] | 702 | list_add_tail(&dev->tz_node, &tz->thermal_instances); |
Zhang Rui | b5e4ae6 | 2012-06-27 14:11:52 +0800 | [diff] [blame] | 703 | list_add_tail(&dev->cdev_node, &cdev->thermal_instances); |
Chen Yu | 4511f71 | 2015-10-30 16:32:10 +0800 | [diff] [blame] | 704 | atomic_set(&tz->need_update, 1); |
Zhang Rui | b5e4ae6 | 2012-06-27 14:11:52 +0800 | [diff] [blame] | 705 | } |
Zhang Rui | f4a821c | 2012-07-24 16:56:21 +0800 | [diff] [blame] | 706 | mutex_unlock(&cdev->lock); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 707 | mutex_unlock(&tz->lock); |
| 708 | |
| 709 | if (!result) |
| 710 | return 0; |
| 711 | |
Javi Merino | db91651 | 2015-02-18 16:04:24 +0000 | [diff] [blame] | 712 | device_remove_file(&tz->device, &dev->weight_attr); |
| 713 | remove_trip_file: |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 714 | device_remove_file(&tz->device, &dev->attr); |
Joe Perches | caca8b80 | 2012-03-21 12:55:02 -0700 | [diff] [blame] | 715 | remove_symbol_link: |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 716 | sysfs_remove_link(&tz->device.kobj, dev->name); |
Matthew Wilcox | b31ef82 | 2016-12-21 09:47:03 -0800 | [diff] [blame^] | 717 | release_ida: |
| 718 | ida_simple_remove(&tz->ida, dev->id); |
Joe Perches | caca8b80 | 2012-03-21 12:55:02 -0700 | [diff] [blame] | 719 | free_mem: |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 720 | kfree(dev); |
| 721 | return result; |
| 722 | } |
Eduardo Valentin | 910cb1e | 2013-04-23 21:48:15 +0000 | [diff] [blame] | 723 | EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 724 | |
| 725 | /** |
Eduardo Valentin | 9892e5d | 2013-04-23 21:48:17 +0000 | [diff] [blame] | 726 | * thermal_zone_unbind_cooling_device() - unbind a cooling device from a |
| 727 | * thermal zone. |
| 728 | * @tz: pointer to a struct thermal_zone_device. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 729 | * @trip: indicates which trip point the cooling devices is |
| 730 | * associated with in this thermal zone. |
Eduardo Valentin | 9892e5d | 2013-04-23 21:48:17 +0000 | [diff] [blame] | 731 | * @cdev: pointer to a struct thermal_cooling_device. |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 732 | * |
Eduardo Valentin | 9892e5d | 2013-04-23 21:48:17 +0000 | [diff] [blame] | 733 | * This interface function unbind a thermal cooling device from the certain |
| 734 | * trip point of a thermal zone device. |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 735 | * This function is usually called in the thermal zone device .unbind callback. |
Eduardo Valentin | 9892e5d | 2013-04-23 21:48:17 +0000 | [diff] [blame] | 736 | * |
| 737 | * Return: 0 on success, the proper error value otherwise. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 738 | */ |
| 739 | int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz, |
| 740 | int trip, |
| 741 | struct thermal_cooling_device *cdev) |
| 742 | { |
Zhang Rui | b81b6ba | 2012-06-27 10:08:19 +0800 | [diff] [blame] | 743 | struct thermal_instance *pos, *next; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 744 | |
| 745 | mutex_lock(&tz->lock); |
Zhang Rui | f4a821c | 2012-07-24 16:56:21 +0800 | [diff] [blame] | 746 | mutex_lock(&cdev->lock); |
Zhang Rui | cddf31b | 2012-06-27 10:09:36 +0800 | [diff] [blame] | 747 | list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) { |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 748 | if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) { |
Zhang Rui | cddf31b | 2012-06-27 10:09:36 +0800 | [diff] [blame] | 749 | list_del(&pos->tz_node); |
Zhang Rui | b5e4ae6 | 2012-06-27 14:11:52 +0800 | [diff] [blame] | 750 | list_del(&pos->cdev_node); |
Zhang Rui | f4a821c | 2012-07-24 16:56:21 +0800 | [diff] [blame] | 751 | mutex_unlock(&cdev->lock); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 752 | mutex_unlock(&tz->lock); |
| 753 | goto unbind; |
| 754 | } |
| 755 | } |
Zhang Rui | f4a821c | 2012-07-24 16:56:21 +0800 | [diff] [blame] | 756 | mutex_unlock(&cdev->lock); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 757 | mutex_unlock(&tz->lock); |
| 758 | |
| 759 | return -ENODEV; |
| 760 | |
Joe Perches | caca8b80 | 2012-03-21 12:55:02 -0700 | [diff] [blame] | 761 | unbind: |
Viresh Kumar | 528464e | 2015-07-23 14:32:32 +0530 | [diff] [blame] | 762 | device_remove_file(&tz->device, &pos->weight_attr); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 763 | device_remove_file(&tz->device, &pos->attr); |
| 764 | sysfs_remove_link(&tz->device.kobj, pos->name); |
Matthew Wilcox | b31ef82 | 2016-12-21 09:47:03 -0800 | [diff] [blame^] | 765 | ida_simple_remove(&tz->ida, pos->id); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 766 | kfree(pos); |
| 767 | return 0; |
| 768 | } |
Eduardo Valentin | 910cb1e | 2013-04-23 21:48:15 +0000 | [diff] [blame] | 769 | EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 770 | |
| 771 | static void thermal_release(struct device *dev) |
| 772 | { |
| 773 | struct thermal_zone_device *tz; |
| 774 | struct thermal_cooling_device *cdev; |
| 775 | |
Joe Perches | caca8b80 | 2012-03-21 12:55:02 -0700 | [diff] [blame] | 776 | if (!strncmp(dev_name(dev), "thermal_zone", |
| 777 | sizeof("thermal_zone") - 1)) { |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 778 | tz = to_thermal_zone(dev); |
| 779 | kfree(tz); |
Eduardo Valentin | b659a30 | 2016-11-07 21:09:23 -0800 | [diff] [blame] | 780 | } else if (!strncmp(dev_name(dev), "cooling_device", |
| 781 | sizeof("cooling_device") - 1)) { |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 782 | cdev = to_cooling_device(dev); |
| 783 | kfree(cdev); |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | static struct class thermal_class = { |
| 788 | .name = "thermal", |
| 789 | .dev_release = thermal_release, |
| 790 | }; |
| 791 | |
Eduardo Valentin | 4b0d3c2 | 2016-11-07 21:09:13 -0800 | [diff] [blame] | 792 | static inline |
| 793 | void print_bind_err_msg(struct thermal_zone_device *tz, |
| 794 | struct thermal_cooling_device *cdev, int ret) |
Eduardo Valentin | f502ab8 | 2016-11-07 21:09:12 -0800 | [diff] [blame] | 795 | { |
| 796 | dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n", |
| 797 | tz->type, cdev->type, ret); |
| 798 | } |
| 799 | |
| 800 | static void __bind(struct thermal_zone_device *tz, int mask, |
| 801 | struct thermal_cooling_device *cdev, |
| 802 | unsigned long *limits, |
| 803 | unsigned int weight) |
| 804 | { |
| 805 | int i, ret; |
| 806 | |
| 807 | for (i = 0; i < tz->trips; i++) { |
| 808 | if (mask & (1 << i)) { |
| 809 | unsigned long upper, lower; |
| 810 | |
| 811 | upper = THERMAL_NO_LIMIT; |
| 812 | lower = THERMAL_NO_LIMIT; |
| 813 | if (limits) { |
| 814 | lower = limits[i * 2]; |
| 815 | upper = limits[i * 2 + 1]; |
| 816 | } |
| 817 | ret = thermal_zone_bind_cooling_device(tz, i, cdev, |
| 818 | upper, lower, |
| 819 | weight); |
| 820 | if (ret) |
| 821 | print_bind_err_msg(tz, cdev, ret); |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | |
Eduardo Valentin | 949aad8 | 2016-11-07 21:09:09 -0800 | [diff] [blame] | 826 | static void bind_cdev(struct thermal_cooling_device *cdev) |
| 827 | { |
| 828 | int i, ret; |
| 829 | const struct thermal_zone_params *tzp; |
| 830 | struct thermal_zone_device *pos = NULL; |
| 831 | |
| 832 | mutex_lock(&thermal_list_lock); |
| 833 | |
| 834 | list_for_each_entry(pos, &thermal_tz_list, node) { |
| 835 | if (!pos->tzp && !pos->ops->bind) |
| 836 | continue; |
| 837 | |
| 838 | if (pos->ops->bind) { |
| 839 | ret = pos->ops->bind(pos, cdev); |
| 840 | if (ret) |
| 841 | print_bind_err_msg(pos, cdev, ret); |
| 842 | continue; |
| 843 | } |
| 844 | |
| 845 | tzp = pos->tzp; |
| 846 | if (!tzp || !tzp->tbp) |
| 847 | continue; |
| 848 | |
| 849 | for (i = 0; i < tzp->num_tbps; i++) { |
| 850 | if (tzp->tbp[i].cdev || !tzp->tbp[i].match) |
| 851 | continue; |
| 852 | if (tzp->tbp[i].match(pos, cdev)) |
| 853 | continue; |
| 854 | tzp->tbp[i].cdev = cdev; |
| 855 | __bind(pos, tzp->tbp[i].trip_mask, cdev, |
| 856 | tzp->tbp[i].binding_limits, |
| 857 | tzp->tbp[i].weight); |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | mutex_unlock(&thermal_list_lock); |
| 862 | } |
| 863 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 864 | /** |
Eduardo Valentin | a116b5d | 2013-09-26 15:55:01 -0400 | [diff] [blame] | 865 | * __thermal_cooling_device_register() - register a new thermal cooling device |
| 866 | * @np: a pointer to a device tree node. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 867 | * @type: the thermal cooling device type. |
| 868 | * @devdata: device private data. |
| 869 | * @ops: standard thermal cooling devices callbacks. |
Eduardo Valentin | 3a6eccb | 2013-04-23 21:48:18 +0000 | [diff] [blame] | 870 | * |
| 871 | * This interface function adds a new thermal cooling device (fan/processor/...) |
| 872 | * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself |
| 873 | * to all the thermal zone devices registered at the same time. |
Eduardo Valentin | a116b5d | 2013-09-26 15:55:01 -0400 | [diff] [blame] | 874 | * It also gives the opportunity to link the cooling device to a device tree |
| 875 | * 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] | 876 | * |
| 877 | * Return: a pointer to the created struct thermal_cooling_device or an |
| 878 | * ERR_PTR. Caller must check return value with IS_ERR*() helpers. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 879 | */ |
Eduardo Valentin | a116b5d | 2013-09-26 15:55:01 -0400 | [diff] [blame] | 880 | static struct thermal_cooling_device * |
| 881 | __thermal_cooling_device_register(struct device_node *np, |
| 882 | char *type, void *devdata, |
| 883 | const struct thermal_cooling_device_ops *ops) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 884 | { |
| 885 | struct thermal_cooling_device *cdev; |
Chen Yu | 4511f71 | 2015-10-30 16:32:10 +0800 | [diff] [blame] | 886 | struct thermal_zone_device *pos = NULL; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 887 | int result; |
| 888 | |
Guenter Roeck | 204dd1d | 2012-08-07 22:36:45 -0700 | [diff] [blame] | 889 | if (type && strlen(type) >= THERMAL_NAME_LENGTH) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 890 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 891 | |
| 892 | if (!ops || !ops->get_max_state || !ops->get_cur_state || |
Len Brown | 543a956 | 2008-02-07 16:55:08 -0500 | [diff] [blame] | 893 | !ops->set_cur_state) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 894 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 895 | |
Eduardo Valentin | 95e3ed1 | 2016-11-07 21:09:25 -0800 | [diff] [blame] | 896 | cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 897 | if (!cdev) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 898 | return ERR_PTR(-ENOMEM); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 899 | |
Matthew Wilcox | b31ef82 | 2016-12-21 09:47:03 -0800 | [diff] [blame^] | 900 | result = ida_simple_get(&thermal_cdev_ida, 0, 0, GFP_KERNEL); |
| 901 | if (result < 0) { |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 902 | kfree(cdev); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 903 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 904 | } |
| 905 | |
Matthew Wilcox | b31ef82 | 2016-12-21 09:47:03 -0800 | [diff] [blame^] | 906 | cdev->id = result; |
Eduardo Valentin | c7a8b9d | 2013-04-23 21:48:12 +0000 | [diff] [blame] | 907 | strlcpy(cdev->type, type ? : "", sizeof(cdev->type)); |
Zhang Rui | f4a821c | 2012-07-24 16:56:21 +0800 | [diff] [blame] | 908 | mutex_init(&cdev->lock); |
Zhang Rui | b5e4ae6 | 2012-06-27 14:11:52 +0800 | [diff] [blame] | 909 | INIT_LIST_HEAD(&cdev->thermal_instances); |
Eduardo Valentin | a116b5d | 2013-09-26 15:55:01 -0400 | [diff] [blame] | 910 | cdev->np = np; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 911 | cdev->ops = ops; |
Ni Wade | 5ca0cce | 2014-02-17 11:02:55 +0800 | [diff] [blame] | 912 | cdev->updated = false; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 913 | cdev->device.class = &thermal_class; |
Eduardo Valentin | 45cf2ec | 2016-11-07 21:09:02 -0800 | [diff] [blame] | 914 | thermal_cooling_device_setup_sysfs(cdev); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 915 | cdev->devdata = devdata; |
Kay Sievers | 354655e | 2009-01-06 10:44:37 -0800 | [diff] [blame] | 916 | dev_set_name(&cdev->device, "cooling_device%d", cdev->id); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 917 | result = device_register(&cdev->device); |
| 918 | if (result) { |
Matthew Wilcox | b31ef82 | 2016-12-21 09:47:03 -0800 | [diff] [blame^] | 919 | ida_simple_remove(&thermal_cdev_ida, cdev->id); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 920 | kfree(cdev); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 921 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 922 | } |
| 923 | |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 924 | /* Add 'this' new cdev to the global cdev list */ |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 925 | mutex_lock(&thermal_list_lock); |
| 926 | list_add(&cdev->node, &thermal_cdev_list); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 927 | mutex_unlock(&thermal_list_lock); |
| 928 | |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 929 | /* Update binding information for 'this' new cdev */ |
| 930 | bind_cdev(cdev); |
| 931 | |
Chen Yu | 4511f71 | 2015-10-30 16:32:10 +0800 | [diff] [blame] | 932 | mutex_lock(&thermal_list_lock); |
| 933 | list_for_each_entry(pos, &thermal_tz_list, node) |
| 934 | if (atomic_cmpxchg(&pos->need_update, 1, 0)) |
Srinivas Pandruvada | 0e70f46 | 2016-08-26 16:21:16 -0700 | [diff] [blame] | 935 | thermal_zone_device_update(pos, |
| 936 | THERMAL_EVENT_UNSPECIFIED); |
Chen Yu | 4511f71 | 2015-10-30 16:32:10 +0800 | [diff] [blame] | 937 | mutex_unlock(&thermal_list_lock); |
| 938 | |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 939 | return cdev; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 940 | } |
Eduardo Valentin | a116b5d | 2013-09-26 15:55:01 -0400 | [diff] [blame] | 941 | |
| 942 | /** |
| 943 | * thermal_cooling_device_register() - register a new thermal cooling device |
| 944 | * @type: the thermal cooling device type. |
| 945 | * @devdata: device private data. |
| 946 | * @ops: standard thermal cooling devices callbacks. |
| 947 | * |
| 948 | * This interface function adds a new thermal cooling device (fan/processor/...) |
| 949 | * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself |
| 950 | * to all the thermal zone devices registered at the same time. |
| 951 | * |
| 952 | * Return: a pointer to the created struct thermal_cooling_device or an |
| 953 | * ERR_PTR. Caller must check return value with IS_ERR*() helpers. |
| 954 | */ |
| 955 | struct thermal_cooling_device * |
| 956 | thermal_cooling_device_register(char *type, void *devdata, |
| 957 | const struct thermal_cooling_device_ops *ops) |
| 958 | { |
| 959 | return __thermal_cooling_device_register(NULL, type, devdata, ops); |
| 960 | } |
Eduardo Valentin | 910cb1e | 2013-04-23 21:48:15 +0000 | [diff] [blame] | 961 | EXPORT_SYMBOL_GPL(thermal_cooling_device_register); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 962 | |
| 963 | /** |
Eduardo Valentin | a116b5d | 2013-09-26 15:55:01 -0400 | [diff] [blame] | 964 | * thermal_of_cooling_device_register() - register an OF thermal cooling device |
| 965 | * @np: a pointer to a device tree node. |
| 966 | * @type: the thermal cooling device type. |
| 967 | * @devdata: device private data. |
| 968 | * @ops: standard thermal cooling devices callbacks. |
| 969 | * |
| 970 | * This function will register a cooling device with device tree node reference. |
| 971 | * This interface function adds a new thermal cooling device (fan/processor/...) |
| 972 | * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself |
| 973 | * to all the thermal zone devices registered at the same time. |
| 974 | * |
| 975 | * Return: a pointer to the created struct thermal_cooling_device or an |
| 976 | * ERR_PTR. Caller must check return value with IS_ERR*() helpers. |
| 977 | */ |
| 978 | struct thermal_cooling_device * |
| 979 | thermal_of_cooling_device_register(struct device_node *np, |
| 980 | char *type, void *devdata, |
| 981 | const struct thermal_cooling_device_ops *ops) |
| 982 | { |
| 983 | return __thermal_cooling_device_register(np, type, devdata, ops); |
| 984 | } |
| 985 | EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register); |
| 986 | |
Eduardo Valentin | f11997f | 2016-11-07 21:09:08 -0800 | [diff] [blame] | 987 | static void __unbind(struct thermal_zone_device *tz, int mask, |
| 988 | struct thermal_cooling_device *cdev) |
| 989 | { |
| 990 | int i; |
| 991 | |
| 992 | for (i = 0; i < tz->trips; i++) |
| 993 | if (mask & (1 << i)) |
| 994 | thermal_zone_unbind_cooling_device(tz, i, cdev); |
| 995 | } |
| 996 | |
Eduardo Valentin | a116b5d | 2013-09-26 15:55:01 -0400 | [diff] [blame] | 997 | /** |
Eduardo Valentin | 38e7b54 | 2016-11-07 21:09:24 -0800 | [diff] [blame] | 998 | * thermal_cooling_device_unregister - removes a thermal cooling device |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 999 | * @cdev: the thermal cooling device to remove. |
| 1000 | * |
Eduardo Valentin | 38e7b54 | 2016-11-07 21:09:24 -0800 | [diff] [blame] | 1001 | * thermal_cooling_device_unregister() must be called when a registered |
| 1002 | * thermal cooling device is no longer needed. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1003 | */ |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1004 | void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1005 | { |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1006 | int i; |
| 1007 | const struct thermal_zone_params *tzp; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1008 | struct thermal_zone_device *tz; |
| 1009 | struct thermal_cooling_device *pos = NULL; |
| 1010 | |
| 1011 | if (!cdev) |
| 1012 | return; |
| 1013 | |
| 1014 | mutex_lock(&thermal_list_lock); |
| 1015 | list_for_each_entry(pos, &thermal_cdev_list, node) |
Eduardo Valentin | b659a30 | 2016-11-07 21:09:23 -0800 | [diff] [blame] | 1016 | if (pos == cdev) |
| 1017 | break; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1018 | if (pos != cdev) { |
| 1019 | /* thermal cooling device not found */ |
| 1020 | mutex_unlock(&thermal_list_lock); |
| 1021 | return; |
| 1022 | } |
| 1023 | list_del(&cdev->node); |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1024 | |
| 1025 | /* Unbind all thermal zones associated with 'this' cdev */ |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1026 | list_for_each_entry(tz, &thermal_tz_list, node) { |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1027 | if (tz->ops->unbind) { |
| 1028 | tz->ops->unbind(tz, cdev); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1029 | continue; |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | if (!tz->tzp || !tz->tzp->tbp) |
| 1033 | continue; |
| 1034 | |
| 1035 | tzp = tz->tzp; |
| 1036 | for (i = 0; i < tzp->num_tbps; i++) { |
| 1037 | if (tzp->tbp[i].cdev == cdev) { |
| 1038 | __unbind(tz, tzp->tbp[i].trip_mask, cdev); |
| 1039 | tzp->tbp[i].cdev = NULL; |
| 1040 | } |
| 1041 | } |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1042 | } |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1043 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1044 | mutex_unlock(&thermal_list_lock); |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1045 | |
Matthew Wilcox | b31ef82 | 2016-12-21 09:47:03 -0800 | [diff] [blame^] | 1046 | ida_simple_remove(&thermal_cdev_ida, cdev->id); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1047 | device_unregister(&cdev->device); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1048 | } |
Eduardo Valentin | 910cb1e | 2013-04-23 21:48:15 +0000 | [diff] [blame] | 1049 | EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1050 | |
Eduardo Valentin | 90f5b5b | 2016-11-07 21:09:10 -0800 | [diff] [blame] | 1051 | static void bind_tz(struct thermal_zone_device *tz) |
Zhang Rui | ce119f8 | 2012-06-27 14:13:04 +0800 | [diff] [blame] | 1052 | { |
Eduardo Valentin | 90f5b5b | 2016-11-07 21:09:10 -0800 | [diff] [blame] | 1053 | int i, ret; |
| 1054 | struct thermal_cooling_device *pos = NULL; |
| 1055 | const struct thermal_zone_params *tzp = tz->tzp; |
Zhang Rui | ce119f8 | 2012-06-27 14:13:04 +0800 | [diff] [blame] | 1056 | |
Eduardo Valentin | 90f5b5b | 2016-11-07 21:09:10 -0800 | [diff] [blame] | 1057 | if (!tzp && !tz->ops->bind) |
Michele Di Giorgio | d0b7306 | 2016-06-02 15:25:31 +0100 | [diff] [blame] | 1058 | return; |
Eduardo Valentin | 90f5b5b | 2016-11-07 21:09:10 -0800 | [diff] [blame] | 1059 | |
| 1060 | mutex_lock(&thermal_list_lock); |
| 1061 | |
| 1062 | /* If there is ops->bind, try to use ops->bind */ |
| 1063 | if (tz->ops->bind) { |
| 1064 | list_for_each_entry(pos, &thermal_cdev_list, node) { |
| 1065 | ret = tz->ops->bind(tz, pos); |
| 1066 | if (ret) |
| 1067 | print_bind_err_msg(tz, pos, ret); |
| 1068 | } |
| 1069 | goto exit; |
Michele Di Giorgio | d0b7306 | 2016-06-02 15:25:31 +0100 | [diff] [blame] | 1070 | } |
| 1071 | |
Eduardo Valentin | 90f5b5b | 2016-11-07 21:09:10 -0800 | [diff] [blame] | 1072 | if (!tzp || !tzp->tbp) |
| 1073 | goto exit; |
Zhang Rui | ce119f8 | 2012-06-27 14:13:04 +0800 | [diff] [blame] | 1074 | |
Eduardo Valentin | 90f5b5b | 2016-11-07 21:09:10 -0800 | [diff] [blame] | 1075 | list_for_each_entry(pos, &thermal_cdev_list, node) { |
| 1076 | for (i = 0; i < tzp->num_tbps; i++) { |
| 1077 | if (tzp->tbp[i].cdev || !tzp->tbp[i].match) |
| 1078 | continue; |
| 1079 | if (tzp->tbp[i].match(tz, pos)) |
| 1080 | continue; |
| 1081 | tzp->tbp[i].cdev = pos; |
| 1082 | __bind(tz, tzp->tbp[i].trip_mask, pos, |
| 1083 | tzp->tbp[i].binding_limits, |
| 1084 | tzp->tbp[i].weight); |
Durgadoss R | 27365a6 | 2012-07-25 10:10:59 +0800 | [diff] [blame] | 1085 | } |
| 1086 | } |
Eduardo Valentin | 90f5b5b | 2016-11-07 21:09:10 -0800 | [diff] [blame] | 1087 | exit: |
| 1088 | mutex_unlock(&thermal_list_lock); |
Durgadoss R | c56f5c0 | 2012-07-25 10:10:58 +0800 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | /** |
Eduardo Valentin | a00e55f | 2013-04-23 21:48:20 +0000 | [diff] [blame] | 1092 | * thermal_zone_device_register() - register a new thermal zone device |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1093 | * @type: the thermal zone device type |
| 1094 | * @trips: the number of trip points the thermal zone support |
Durgadoss R | c56f5c0 | 2012-07-25 10:10:58 +0800 | [diff] [blame] | 1095 | * @mask: a bit string indicating the writeablility of trip points |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1096 | * @devdata: private device data |
| 1097 | * @ops: standard thermal zone device callbacks |
Durgadoss R | 50125a9 | 2012-09-18 11:04:56 +0530 | [diff] [blame] | 1098 | * @tzp: thermal zone platform parameters |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1099 | * @passive_delay: number of milliseconds to wait between polls when |
| 1100 | * performing passive cooling |
| 1101 | * @polling_delay: number of milliseconds to wait between polls when checking |
| 1102 | * whether trip points have been crossed (0 for interrupt |
| 1103 | * driven systems) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1104 | * |
Eduardo Valentin | a00e55f | 2013-04-23 21:48:20 +0000 | [diff] [blame] | 1105 | * This interface function adds a new thermal zone device (sensor) to |
| 1106 | * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the |
| 1107 | * thermal cooling devices registered at the same time. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1108 | * thermal_zone_device_unregister() must be called when the device is no |
Zhang Rui | 1b7ddb8 | 2012-06-27 09:51:12 +0800 | [diff] [blame] | 1109 | * longer needed. The passive cooling depends on the .get_trend() return value. |
Eduardo Valentin | a00e55f | 2013-04-23 21:48:20 +0000 | [diff] [blame] | 1110 | * |
| 1111 | * Return: a pointer to the created struct thermal_zone_device or an |
| 1112 | * in case of error, an ERR_PTR. Caller must check return value with |
| 1113 | * IS_ERR*() helpers. |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1114 | */ |
Eduardo Valentin | eb7be32 | 2016-11-07 21:09:21 -0800 | [diff] [blame] | 1115 | struct thermal_zone_device * |
| 1116 | thermal_zone_device_register(const char *type, int trips, int mask, |
| 1117 | void *devdata, struct thermal_zone_device_ops *ops, |
| 1118 | struct thermal_zone_params *tzp, int passive_delay, |
| 1119 | int polling_delay) |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1120 | { |
| 1121 | struct thermal_zone_device *tz; |
Matthew Garrett | 03a971a | 2008-12-03 18:00:38 +0000 | [diff] [blame] | 1122 | enum thermal_trip_type trip_type; |
Zhang Rui | 81ad427 | 2016-03-18 10:03:24 +0800 | [diff] [blame] | 1123 | int trip_temp; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1124 | int result; |
| 1125 | int count; |
Javi Merino | e33df1d | 2015-02-26 19:00:27 +0000 | [diff] [blame] | 1126 | struct thermal_governor *governor; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1127 | |
Eduardo Valentin | 54fa38c | 2016-11-07 21:08:39 -0800 | [diff] [blame] | 1128 | if (!type || strlen(type) == 0) |
| 1129 | return ERR_PTR(-EINVAL); |
| 1130 | |
Guenter Roeck | 204dd1d | 2012-08-07 22:36:45 -0700 | [diff] [blame] | 1131 | if (type && strlen(type) >= THERMAL_NAME_LENGTH) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1132 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1133 | |
Durgadoss R | c56f5c0 | 2012-07-25 10:10:58 +0800 | [diff] [blame] | 1134 | if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1135 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1136 | |
Eduardo Valentin | 81bd4e1 | 2013-09-12 19:15:44 -0400 | [diff] [blame] | 1137 | if (!ops) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1138 | return ERR_PTR(-EINVAL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1139 | |
Jonghwa Lee | 83720d0 | 2013-05-18 09:50:26 +0000 | [diff] [blame] | 1140 | if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp)) |
Eduardo Valentin | 6b2aa51 | 2013-01-02 15:29:42 +0000 | [diff] [blame] | 1141 | return ERR_PTR(-EINVAL); |
| 1142 | |
Eduardo Valentin | 95e3ed1 | 2016-11-07 21:09:25 -0800 | [diff] [blame] | 1143 | tz = kzalloc(sizeof(*tz), GFP_KERNEL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1144 | if (!tz) |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1145 | return ERR_PTR(-ENOMEM); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1146 | |
Zhang Rui | 2d37413 | 2012-06-27 10:09:00 +0800 | [diff] [blame] | 1147 | INIT_LIST_HEAD(&tz->thermal_instances); |
Matthew Wilcox | b31ef82 | 2016-12-21 09:47:03 -0800 | [diff] [blame^] | 1148 | ida_init(&tz->ida); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1149 | mutex_init(&tz->lock); |
Matthew Wilcox | b31ef82 | 2016-12-21 09:47:03 -0800 | [diff] [blame^] | 1150 | result = ida_simple_get(&thermal_tz_ida, 0, 0, GFP_KERNEL); |
| 1151 | if (result < 0) { |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1152 | kfree(tz); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1153 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1154 | } |
| 1155 | |
Matthew Wilcox | b31ef82 | 2016-12-21 09:47:03 -0800 | [diff] [blame^] | 1156 | tz->id = result; |
Eduardo Valentin | 54fa38c | 2016-11-07 21:08:39 -0800 | [diff] [blame] | 1157 | strlcpy(tz->type, type, sizeof(tz->type)); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1158 | tz->ops = ops; |
Durgadoss R | 50125a9 | 2012-09-18 11:04:56 +0530 | [diff] [blame] | 1159 | tz->tzp = tzp; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1160 | tz->device.class = &thermal_class; |
| 1161 | tz->devdata = devdata; |
| 1162 | tz->trips = trips; |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1163 | tz->passive_delay = passive_delay; |
| 1164 | tz->polling_delay = polling_delay; |
Eduardo Valentin | 1c60086 | 2016-11-07 21:08:42 -0800 | [diff] [blame] | 1165 | |
Eduardo Valentin | 4d0fe74 | 2016-11-07 21:08:52 -0800 | [diff] [blame] | 1166 | /* sys I/F */ |
Eduardo Valentin | 1c60086 | 2016-11-07 21:08:42 -0800 | [diff] [blame] | 1167 | /* Add nodes that are always present via .groups */ |
Eduardo Valentin | 4d0fe74 | 2016-11-07 21:08:52 -0800 | [diff] [blame] | 1168 | result = thermal_zone_create_device_groups(tz, mask); |
| 1169 | if (result) |
| 1170 | goto unregister; |
| 1171 | |
Chen Yu | 4511f71 | 2015-10-30 16:32:10 +0800 | [diff] [blame] | 1172 | /* A new thermal zone needs to be updated anyway. */ |
| 1173 | atomic_set(&tz->need_update, 1); |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1174 | |
Kay Sievers | 354655e | 2009-01-06 10:44:37 -0800 | [diff] [blame] | 1175 | dev_set_name(&tz->device, "thermal_zone%d", tz->id); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1176 | result = device_register(&tz->device); |
| 1177 | if (result) { |
Matthew Wilcox | b31ef82 | 2016-12-21 09:47:03 -0800 | [diff] [blame^] | 1178 | ida_simple_remove(&thermal_tz_ida, tz->id); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1179 | kfree(tz); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1180 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1181 | } |
| 1182 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1183 | for (count = 0; count < trips; count++) { |
Zhang Rui | 81ad427 | 2016-03-18 10:03:24 +0800 | [diff] [blame] | 1184 | if (tz->ops->get_trip_type(tz, count, &trip_type)) |
| 1185 | set_bit(count, &tz->trips_disabled); |
Zhang Rui | 81ad427 | 2016-03-18 10:03:24 +0800 | [diff] [blame] | 1186 | if (tz->ops->get_trip_temp(tz, count, &trip_temp)) |
| 1187 | set_bit(count, &tz->trips_disabled); |
| 1188 | /* Check for bogus trip points */ |
| 1189 | if (trip_temp == 0) |
| 1190 | set_bit(count, &tz->trips_disabled); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1191 | } |
| 1192 | |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 1193 | /* Update 'this' zone's governor information */ |
| 1194 | mutex_lock(&thermal_governor_lock); |
| 1195 | |
| 1196 | if (tz->tzp) |
Javi Merino | e33df1d | 2015-02-26 19:00:27 +0000 | [diff] [blame] | 1197 | governor = __find_governor(tz->tzp->governor_name); |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 1198 | else |
Javi Merino | e33df1d | 2015-02-26 19:00:27 +0000 | [diff] [blame] | 1199 | governor = def_governor; |
| 1200 | |
| 1201 | result = thermal_set_governor(tz, governor); |
| 1202 | if (result) { |
| 1203 | mutex_unlock(&thermal_governor_lock); |
| 1204 | goto unregister; |
| 1205 | } |
Durgadoss R | a4a1548 | 2012-09-18 11:04:57 +0530 | [diff] [blame] | 1206 | |
| 1207 | mutex_unlock(&thermal_governor_lock); |
| 1208 | |
Eduardo Valentin | ccba4ff | 2013-08-15 11:34:17 -0400 | [diff] [blame] | 1209 | if (!tz->tzp || !tz->tzp->no_hwmon) { |
| 1210 | result = thermal_add_hwmon_sysfs(tz); |
| 1211 | if (result) |
| 1212 | goto unregister; |
| 1213 | } |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 1214 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1215 | mutex_lock(&thermal_list_lock); |
| 1216 | list_add_tail(&tz->node, &thermal_tz_list); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1217 | mutex_unlock(&thermal_list_lock); |
| 1218 | |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1219 | /* Bind cooling devices for this zone */ |
| 1220 | bind_tz(tz); |
| 1221 | |
Eduardo Valentin | b659a30 | 2016-11-07 21:09:23 -0800 | [diff] [blame] | 1222 | INIT_DELAYED_WORK(&tz->poll_queue, thermal_zone_device_check); |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1223 | |
Zhang Rui | bb431ba | 2015-10-30 16:31:47 +0800 | [diff] [blame] | 1224 | thermal_zone_device_reset(tz); |
Chen Yu | 4511f71 | 2015-10-30 16:32:10 +0800 | [diff] [blame] | 1225 | /* Update the new thermal zone and mark it as already updated. */ |
| 1226 | if (atomic_cmpxchg(&tz->need_update, 1, 0)) |
Srinivas Pandruvada | 0e70f46 | 2016-08-26 16:21:16 -0700 | [diff] [blame] | 1227 | thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED); |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1228 | |
Yao Dongdong | 1401586 | 2014-10-28 07:40:25 +0000 | [diff] [blame] | 1229 | return tz; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1230 | |
Joe Perches | caca8b80 | 2012-03-21 12:55:02 -0700 | [diff] [blame] | 1231 | unregister: |
Matthew Wilcox | b31ef82 | 2016-12-21 09:47:03 -0800 | [diff] [blame^] | 1232 | ida_simple_remove(&thermal_tz_ida, tz->id); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1233 | device_unregister(&tz->device); |
Thomas Sujith | 3e6fda5 | 2008-02-15 00:59:50 -0500 | [diff] [blame] | 1234 | return ERR_PTR(result); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1235 | } |
Eduardo Valentin | 910cb1e | 2013-04-23 21:48:15 +0000 | [diff] [blame] | 1236 | EXPORT_SYMBOL_GPL(thermal_zone_device_register); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1237 | |
| 1238 | /** |
| 1239 | * thermal_device_unregister - removes the registered thermal zone device |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1240 | * @tz: the thermal zone device to remove |
| 1241 | */ |
| 1242 | void thermal_zone_device_unregister(struct thermal_zone_device *tz) |
| 1243 | { |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1244 | int i; |
| 1245 | const struct thermal_zone_params *tzp; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1246 | struct thermal_cooling_device *cdev; |
| 1247 | struct thermal_zone_device *pos = NULL; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1248 | |
| 1249 | if (!tz) |
| 1250 | return; |
| 1251 | |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1252 | tzp = tz->tzp; |
| 1253 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1254 | mutex_lock(&thermal_list_lock); |
| 1255 | list_for_each_entry(pos, &thermal_tz_list, node) |
Eduardo Valentin | b659a30 | 2016-11-07 21:09:23 -0800 | [diff] [blame] | 1256 | if (pos == tz) |
| 1257 | break; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1258 | if (pos != tz) { |
| 1259 | /* thermal zone device not found */ |
| 1260 | mutex_unlock(&thermal_list_lock); |
| 1261 | return; |
| 1262 | } |
| 1263 | list_del(&tz->node); |
Durgadoss R | 7e8ee1e | 2012-09-18 11:04:59 +0530 | [diff] [blame] | 1264 | |
| 1265 | /* Unbind all cdevs associated with 'this' thermal zone */ |
| 1266 | list_for_each_entry(cdev, &thermal_cdev_list, node) { |
| 1267 | if (tz->ops->unbind) { |
| 1268 | tz->ops->unbind(tz, cdev); |
| 1269 | continue; |
| 1270 | } |
| 1271 | |
| 1272 | if (!tzp || !tzp->tbp) |
| 1273 | break; |
| 1274 | |
| 1275 | for (i = 0; i < tzp->num_tbps; i++) { |
| 1276 | if (tzp->tbp[i].cdev == cdev) { |
| 1277 | __unbind(tz, tzp->tbp[i].trip_mask, cdev); |
| 1278 | tzp->tbp[i].cdev = NULL; |
| 1279 | } |
| 1280 | } |
| 1281 | } |
| 1282 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1283 | mutex_unlock(&thermal_list_lock); |
| 1284 | |
Matthew Garrett | b1569e9 | 2008-12-03 17:55:32 +0000 | [diff] [blame] | 1285 | thermal_zone_device_set_polling(tz, 0); |
| 1286 | |
Eduardo Valentin | 4d0fe74 | 2016-11-07 21:08:52 -0800 | [diff] [blame] | 1287 | kfree(tz->trip_type_attrs); |
| 1288 | kfree(tz->trip_temp_attrs); |
| 1289 | kfree(tz->trip_hyst_attrs); |
| 1290 | kfree(tz->trips_attribute_group.attrs); |
Javi Merino | e33df1d | 2015-02-26 19:00:27 +0000 | [diff] [blame] | 1291 | thermal_set_governor(tz, NULL); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1292 | |
Zhang Rui | e68b16a | 2008-04-21 16:07:52 +0800 | [diff] [blame] | 1293 | thermal_remove_hwmon_sysfs(tz); |
Matthew Wilcox | b31ef82 | 2016-12-21 09:47:03 -0800 | [diff] [blame^] | 1294 | ida_simple_remove(&thermal_tz_ida, tz->id); |
| 1295 | ida_destroy(&tz->ida); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1296 | mutex_destroy(&tz->lock); |
| 1297 | device_unregister(&tz->device); |
Eduardo Valentin | e161aef | 2016-11-07 21:08:51 -0800 | [diff] [blame] | 1298 | kfree(tz->device.groups); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1299 | } |
Eduardo Valentin | 910cb1e | 2013-04-23 21:48:15 +0000 | [diff] [blame] | 1300 | EXPORT_SYMBOL_GPL(thermal_zone_device_unregister); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1301 | |
Eduardo Valentin | 63c4d91 | 2013-04-05 12:32:28 +0000 | [diff] [blame] | 1302 | /** |
| 1303 | * thermal_zone_get_zone_by_name() - search for a zone and returns its ref |
| 1304 | * @name: thermal zone name to fetch the temperature |
| 1305 | * |
| 1306 | * When only one zone is found with the passed name, returns a reference to it. |
| 1307 | * |
| 1308 | * Return: On success returns a reference to an unique thermal zone with |
| 1309 | * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid |
| 1310 | * paramenters, -ENODEV for not found and -EEXIST for multiple matches). |
| 1311 | */ |
| 1312 | struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name) |
| 1313 | { |
| 1314 | struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL); |
| 1315 | unsigned int found = 0; |
| 1316 | |
| 1317 | if (!name) |
| 1318 | goto exit; |
| 1319 | |
| 1320 | mutex_lock(&thermal_list_lock); |
| 1321 | list_for_each_entry(pos, &thermal_tz_list, node) |
Rasmus Villemoes | 484ac2f | 2014-10-13 15:55:01 -0700 | [diff] [blame] | 1322 | if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) { |
Eduardo Valentin | 63c4d91 | 2013-04-05 12:32:28 +0000 | [diff] [blame] | 1323 | found++; |
| 1324 | ref = pos; |
| 1325 | } |
| 1326 | mutex_unlock(&thermal_list_lock); |
| 1327 | |
| 1328 | /* nothing has been found, thus an error code for it */ |
| 1329 | if (found == 0) |
| 1330 | ref = ERR_PTR(-ENODEV); |
| 1331 | else if (found > 1) |
| 1332 | /* Success only when an unique zone is found */ |
| 1333 | ref = ERR_PTR(-EEXIST); |
| 1334 | |
| 1335 | exit: |
| 1336 | return ref; |
| 1337 | } |
| 1338 | EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name); |
| 1339 | |
Rafael J. Wysocki | af06216 | 2011-03-01 01:12:19 +0100 | [diff] [blame] | 1340 | #ifdef CONFIG_NET |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1341 | static const struct genl_multicast_group thermal_event_mcgrps[] = { |
| 1342 | { .name = THERMAL_GENL_MCAST_GROUP_NAME, }, |
| 1343 | }; |
| 1344 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 1345 | static struct genl_family thermal_event_genl_family __ro_after_init = { |
Johannes Berg | 489111e | 2016-10-24 14:40:03 +0200 | [diff] [blame] | 1346 | .module = THIS_MODULE, |
Rafael J. Wysocki | af06216 | 2011-03-01 01:12:19 +0100 | [diff] [blame] | 1347 | .name = THERMAL_GENL_FAMILY_NAME, |
| 1348 | .version = THERMAL_GENL_VERSION, |
| 1349 | .maxattr = THERMAL_GENL_ATTR_MAX, |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1350 | .mcgrps = thermal_event_mcgrps, |
| 1351 | .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps), |
Rafael J. Wysocki | af06216 | 2011-03-01 01:12:19 +0100 | [diff] [blame] | 1352 | }; |
| 1353 | |
Eduardo Valentin | 8ab3e6a | 2013-01-02 15:29:39 +0000 | [diff] [blame] | 1354 | int thermal_generate_netlink_event(struct thermal_zone_device *tz, |
Eduardo Valentin | eb7be32 | 2016-11-07 21:09:21 -0800 | [diff] [blame] | 1355 | enum events event) |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1356 | { |
| 1357 | struct sk_buff *skb; |
| 1358 | struct nlattr *attr; |
| 1359 | struct thermal_genl_event *thermal_event; |
| 1360 | void *msg_header; |
| 1361 | int size; |
| 1362 | int result; |
Fabio Estevam | b11de07 | 2012-03-21 12:55:00 -0700 | [diff] [blame] | 1363 | static unsigned int thermal_event_seqnum; |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1364 | |
Eduardo Valentin | 8ab3e6a | 2013-01-02 15:29:39 +0000 | [diff] [blame] | 1365 | if (!tz) |
| 1366 | return -EINVAL; |
| 1367 | |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1368 | /* allocate memory */ |
Joe Perches | 886ee54 | 2012-03-21 12:55:01 -0700 | [diff] [blame] | 1369 | size = nla_total_size(sizeof(struct thermal_genl_event)) + |
| 1370 | nla_total_size(0); |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1371 | |
| 1372 | skb = genlmsg_new(size, GFP_ATOMIC); |
| 1373 | if (!skb) |
| 1374 | return -ENOMEM; |
| 1375 | |
| 1376 | /* add the genetlink message header */ |
| 1377 | msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++, |
| 1378 | &thermal_event_genl_family, 0, |
| 1379 | THERMAL_GENL_CMD_EVENT); |
| 1380 | if (!msg_header) { |
| 1381 | nlmsg_free(skb); |
| 1382 | return -ENOMEM; |
| 1383 | } |
| 1384 | |
| 1385 | /* fill the data */ |
Joe Perches | 886ee54 | 2012-03-21 12:55:01 -0700 | [diff] [blame] | 1386 | attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT, |
| 1387 | sizeof(struct thermal_genl_event)); |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1388 | |
| 1389 | if (!attr) { |
| 1390 | nlmsg_free(skb); |
| 1391 | return -EINVAL; |
| 1392 | } |
| 1393 | |
| 1394 | thermal_event = nla_data(attr); |
| 1395 | if (!thermal_event) { |
| 1396 | nlmsg_free(skb); |
| 1397 | return -EINVAL; |
| 1398 | } |
| 1399 | |
| 1400 | memset(thermal_event, 0, sizeof(struct thermal_genl_event)); |
| 1401 | |
Eduardo Valentin | 8ab3e6a | 2013-01-02 15:29:39 +0000 | [diff] [blame] | 1402 | thermal_event->orig = tz->id; |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1403 | thermal_event->event = event; |
| 1404 | |
| 1405 | /* send multicast genetlink message */ |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 1406 | genlmsg_end(skb, msg_header); |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1407 | |
Johannes Berg | 68eb550 | 2013-11-19 15:19:38 +0100 | [diff] [blame] | 1408 | result = genlmsg_multicast(&thermal_event_genl_family, skb, 0, |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1409 | 0, GFP_ATOMIC); |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1410 | if (result) |
Eduardo Valentin | 923e0b1 | 2013-01-02 15:29:41 +0000 | [diff] [blame] | 1411 | dev_err(&tz->device, "Failed to send netlink event:%d", result); |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1412 | |
| 1413 | return result; |
| 1414 | } |
Eduardo Valentin | 910cb1e | 2013-04-23 21:48:15 +0000 | [diff] [blame] | 1415 | EXPORT_SYMBOL_GPL(thermal_generate_netlink_event); |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1416 | |
Johannes Berg | 56989f6 | 2016-10-24 14:40:05 +0200 | [diff] [blame] | 1417 | static int __init genetlink_init(void) |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1418 | { |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 1419 | return genl_register_family(&thermal_event_genl_family); |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1420 | } |
| 1421 | |
Rafael J. Wysocki | af06216 | 2011-03-01 01:12:19 +0100 | [diff] [blame] | 1422 | static void genetlink_exit(void) |
| 1423 | { |
| 1424 | genl_unregister_family(&thermal_event_genl_family); |
| 1425 | } |
| 1426 | #else /* !CONFIG_NET */ |
| 1427 | static inline int genetlink_init(void) { return 0; } |
| 1428 | static inline void genetlink_exit(void) {} |
| 1429 | #endif /* !CONFIG_NET */ |
| 1430 | |
Zhang Rui | ff140fe | 2015-10-30 16:31:58 +0800 | [diff] [blame] | 1431 | static int thermal_pm_notify(struct notifier_block *nb, |
Eduardo Valentin | eb7be32 | 2016-11-07 21:09:21 -0800 | [diff] [blame] | 1432 | unsigned long mode, void *_unused) |
Zhang Rui | ff140fe | 2015-10-30 16:31:58 +0800 | [diff] [blame] | 1433 | { |
| 1434 | struct thermal_zone_device *tz; |
| 1435 | |
| 1436 | switch (mode) { |
| 1437 | case PM_HIBERNATION_PREPARE: |
| 1438 | case PM_RESTORE_PREPARE: |
| 1439 | case PM_SUSPEND_PREPARE: |
| 1440 | atomic_set(&in_suspend, 1); |
| 1441 | break; |
| 1442 | case PM_POST_HIBERNATION: |
| 1443 | case PM_POST_RESTORE: |
| 1444 | case PM_POST_SUSPEND: |
| 1445 | atomic_set(&in_suspend, 0); |
| 1446 | list_for_each_entry(tz, &thermal_tz_list, node) { |
| 1447 | thermal_zone_device_reset(tz); |
Srinivas Pandruvada | 0e70f46 | 2016-08-26 16:21:16 -0700 | [diff] [blame] | 1448 | thermal_zone_device_update(tz, |
| 1449 | THERMAL_EVENT_UNSPECIFIED); |
Zhang Rui | ff140fe | 2015-10-30 16:31:58 +0800 | [diff] [blame] | 1450 | } |
| 1451 | break; |
| 1452 | default: |
| 1453 | break; |
| 1454 | } |
| 1455 | return 0; |
| 1456 | } |
| 1457 | |
| 1458 | static struct notifier_block thermal_pm_nb = { |
| 1459 | .notifier_call = thermal_pm_notify, |
| 1460 | }; |
| 1461 | |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1462 | static int __init thermal_init(void) |
| 1463 | { |
Zhang Rui | 80a26a5 | 2013-03-26 16:38:29 +0800 | [diff] [blame] | 1464 | int result; |
| 1465 | |
| 1466 | result = thermal_register_governors(); |
| 1467 | if (result) |
| 1468 | goto error; |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1469 | |
| 1470 | result = class_register(&thermal_class); |
Zhang Rui | 80a26a5 | 2013-03-26 16:38:29 +0800 | [diff] [blame] | 1471 | if (result) |
| 1472 | goto unregister_governors; |
| 1473 | |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1474 | result = genetlink_init(); |
Zhang Rui | 80a26a5 | 2013-03-26 16:38:29 +0800 | [diff] [blame] | 1475 | if (result) |
| 1476 | goto unregister_class; |
| 1477 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1478 | result = of_parse_thermal_zones(); |
| 1479 | if (result) |
| 1480 | goto exit_netlink; |
| 1481 | |
Zhang Rui | ff140fe | 2015-10-30 16:31:58 +0800 | [diff] [blame] | 1482 | result = register_pm_notifier(&thermal_pm_nb); |
| 1483 | if (result) |
| 1484 | pr_warn("Thermal: Can not register suspend notifier, return %d\n", |
| 1485 | result); |
| 1486 | |
Zhang Rui | 80a26a5 | 2013-03-26 16:38:29 +0800 | [diff] [blame] | 1487 | return 0; |
| 1488 | |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1489 | exit_netlink: |
| 1490 | genetlink_exit(); |
Zhang Rui | 80a26a5 | 2013-03-26 16:38:29 +0800 | [diff] [blame] | 1491 | unregister_class: |
| 1492 | class_unregister(&thermal_class); |
Luis Henriques | 9d367e5 | 2014-12-03 21:20:21 +0000 | [diff] [blame] | 1493 | unregister_governors: |
| 1494 | thermal_unregister_governors(); |
Zhang Rui | 80a26a5 | 2013-03-26 16:38:29 +0800 | [diff] [blame] | 1495 | error: |
Matthew Wilcox | b31ef82 | 2016-12-21 09:47:03 -0800 | [diff] [blame^] | 1496 | ida_destroy(&thermal_tz_ida); |
| 1497 | ida_destroy(&thermal_cdev_ida); |
Zhang Rui | 80a26a5 | 2013-03-26 16:38:29 +0800 | [diff] [blame] | 1498 | mutex_destroy(&thermal_list_lock); |
| 1499 | mutex_destroy(&thermal_governor_lock); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1500 | return result; |
| 1501 | } |
| 1502 | |
| 1503 | static void __exit thermal_exit(void) |
| 1504 | { |
Zhang Rui | ff140fe | 2015-10-30 16:31:58 +0800 | [diff] [blame] | 1505 | unregister_pm_notifier(&thermal_pm_nb); |
Eduardo Valentin | 4e5e470 | 2013-07-03 15:35:39 -0400 | [diff] [blame] | 1506 | of_thermal_destroy_zones(); |
Zhang Rui | 80a26a5 | 2013-03-26 16:38:29 +0800 | [diff] [blame] | 1507 | genetlink_exit(); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1508 | class_unregister(&thermal_class); |
Zhang Rui | 80a26a5 | 2013-03-26 16:38:29 +0800 | [diff] [blame] | 1509 | thermal_unregister_governors(); |
Matthew Wilcox | b31ef82 | 2016-12-21 09:47:03 -0800 | [diff] [blame^] | 1510 | ida_destroy(&thermal_tz_ida); |
| 1511 | ida_destroy(&thermal_cdev_ida); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1512 | mutex_destroy(&thermal_list_lock); |
Zhang Rui | 80a26a5 | 2013-03-26 16:38:29 +0800 | [diff] [blame] | 1513 | mutex_destroy(&thermal_governor_lock); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1514 | } |
| 1515 | |
R.Durgadoss | 4cb1872 | 2010-10-27 03:33:29 +0530 | [diff] [blame] | 1516 | fs_initcall(thermal_init); |
Zhang Rui | 203d3d4 | 2008-01-17 15:51:08 +0800 | [diff] [blame] | 1517 | module_exit(thermal_exit); |