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