blob: e36a6a6e58dbfb2ede0fd617715b3b08f5a48d2d [file] [log] [blame]
Zhang Rui203d3d42008-01-17 15:51:08 +08001/*
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 Perchesc5a01dd2012-03-21 12:55:02 -070026#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
Zhang Rui203d3d42008-01-17 15:51:08 +080028#include <linux/module.h>
29#include <linux/device.h>
30#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Zhang Rui203d3d42008-01-17 15:51:08 +080032#include <linux/kdev_t.h>
33#include <linux/idr.h>
34#include <linux/thermal.h>
Matthew Garrettb1569e92008-12-03 17:55:32 +000035#include <linux/reboot.h>
Andy Shevchenko42a5bf52013-05-17 11:52:02 +000036#include <linux/string.h>
Eduardo Valentina116b5d2013-09-26 15:55:01 -040037#include <linux/of.h>
R.Durgadoss4cb18722010-10-27 03:33:29 +053038#include <net/netlink.h>
39#include <net/genetlink.h>
Zhang Ruiff140fe2015-10-30 16:31:58 +080040#include <linux/suspend.h>
Zhang Rui203d3d42008-01-17 15:51:08 +080041
Punit Agrawal100a8fd2014-07-29 11:50:48 +010042#define CREATE_TRACE_POINTS
43#include <trace/events/thermal.h>
44
Durgadoss R71350db2012-09-18 11:04:53 +053045#include "thermal_core.h"
Eduardo Valentin0dd88792013-07-03 15:14:28 -040046#include "thermal_hwmon.h"
Durgadoss R71350db2012-09-18 11:04:53 +053047
Zhang Rui63c4ec92008-04-21 16:07:13 +080048MODULE_AUTHOR("Zhang Rui");
Zhang Rui203d3d42008-01-17 15:51:08 +080049MODULE_DESCRIPTION("Generic thermal management sysfs support");
Eduardo Valentin6d8d4972013-04-23 21:48:13 +000050MODULE_LICENSE("GPL v2");
Zhang Rui203d3d42008-01-17 15:51:08 +080051
Zhang Rui203d3d42008-01-17 15:51:08 +080052static DEFINE_IDR(thermal_tz_idr);
53static DEFINE_IDR(thermal_cdev_idr);
54static DEFINE_MUTEX(thermal_idr_lock);
55
56static LIST_HEAD(thermal_tz_list);
57static LIST_HEAD(thermal_cdev_list);
Durgadoss Ra4a15482012-09-18 11:04:57 +053058static LIST_HEAD(thermal_governor_list);
59
Zhang Rui203d3d42008-01-17 15:51:08 +080060static DEFINE_MUTEX(thermal_list_lock);
Durgadoss Ra4a15482012-09-18 11:04:57 +053061static DEFINE_MUTEX(thermal_governor_lock);
62
Zhang Ruiff140fe2015-10-30 16:31:58 +080063static atomic_t in_suspend;
64
Zhang Ruif2234bc2014-01-24 10:23:19 +080065static struct thermal_governor *def_governor;
66
Eduardo Valentin1b4f4842016-11-07 21:09:05 -080067/*
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 Ra4a15482012-09-18 11:04:57 +053074static struct thermal_governor *__find_governor(const char *name)
75{
76 struct thermal_governor *pos;
77
Zhang Ruif2234bc2014-01-24 10:23:19 +080078 if (!name || !name[0])
79 return def_governor;
80
Durgadoss Ra4a15482012-09-18 11:04:57 +053081 list_for_each_entry(pos, &thermal_governor_list, governor_list)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -070082 if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
Durgadoss Ra4a15482012-09-18 11:04:57 +053083 return pos;
84
85 return NULL;
86}
87
Javi Merinoe33df1d2015-02-26 19:00:27 +000088/**
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 */
96static 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 */
118static 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 Ra4a15482012-09-18 11:04:57 +0530140int 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 Ruif2234bc2014-01-24 10:23:19 +0800155 if (!def_governor && !strncmp(governor->name,
156 DEFAULT_THERMAL_GOVERNOR, THERMAL_NAME_LENGTH))
157 def_governor = governor;
Durgadoss Ra4a15482012-09-18 11:04:57 +0530158 }
159
160 mutex_lock(&thermal_list_lock);
161
162 list_for_each_entry(pos, &thermal_tz_list, node) {
Zhang Ruif2234bc2014-01-24 10:23:19 +0800163 /*
164 * only thermal zones with specified tz->tzp->governor_name
165 * may run with tz->govenor unset
166 */
Durgadoss Ra4a15482012-09-18 11:04:57 +0530167 if (pos->governor)
168 continue;
Zhang Ruif2234bc2014-01-24 10:23:19 +0800169
170 name = pos->tzp->governor_name;
171
Javi Merinoe33df1d2015-02-26 19:00:27 +0000172 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 Ra4a15482012-09-18 11:04:57 +0530181 }
182
183 mutex_unlock(&thermal_list_lock);
184 mutex_unlock(&thermal_governor_lock);
185
186 return err;
187}
Durgadoss Ra4a15482012-09-18 11:04:57 +0530188
189void 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 Villemoes484ac2f2014-10-13 15:55:01 -0700204 if (!strncasecmp(pos->governor->name, governor->name,
Durgadoss Ra4a15482012-09-18 11:04:57 +0530205 THERMAL_NAME_LENGTH))
Javi Merinoe33df1d2015-02-26 19:00:27 +0000206 thermal_set_governor(pos, NULL);
Durgadoss Ra4a15482012-09-18 11:04:57 +0530207 }
208
209 mutex_unlock(&thermal_list_lock);
210 list_del(&governor->governor_list);
211exit:
212 mutex_unlock(&thermal_governor_lock);
213 return;
214}
Zhang Rui203d3d42008-01-17 15:51:08 +0800215
Eduardo Valentin1b4f4842016-11-07 21:09:05 -0800216int 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
231exit:
232 mutex_unlock(&tz->lock);
233 mutex_unlock(&thermal_governor_lock);
234
235 return ret;
236}
237
238int 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
257static 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
280static 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
Zhang Rui203d3d42008-01-17 15:51:08 +0800289static int get_idr(struct idr *idr, struct mutex *lock, int *id)
290{
Tejun Heo6deb69f2013-02-27 17:04:46 -0800291 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800292
293 if (lock)
294 mutex_lock(lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800295 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800296 if (lock)
297 mutex_unlock(lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800298 if (unlikely(ret < 0))
299 return ret;
300 *id = ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800301 return 0;
302}
303
304static void release_idr(struct idr *idr, struct mutex *lock, int id)
305{
306 if (lock)
307 mutex_lock(lock);
308 idr_remove(idr, id);
309 if (lock)
310 mutex_unlock(lock);
311}
312
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530313static void print_bind_err_msg(struct thermal_zone_device *tz,
314 struct thermal_cooling_device *cdev, int ret)
315{
316 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
317 tz->type, cdev->type, ret);
318}
319
320static void __bind(struct thermal_zone_device *tz, int mask,
Eduardo Valentina8892d832013-07-16 15:26:28 -0400321 struct thermal_cooling_device *cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000322 unsigned long *limits,
323 unsigned int weight)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530324{
325 int i, ret;
326
327 for (i = 0; i < tz->trips; i++) {
328 if (mask & (1 << i)) {
Eduardo Valentina8892d832013-07-16 15:26:28 -0400329 unsigned long upper, lower;
330
331 upper = THERMAL_NO_LIMIT;
332 lower = THERMAL_NO_LIMIT;
333 if (limits) {
334 lower = limits[i * 2];
335 upper = limits[i * 2 + 1];
336 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530337 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000338 upper, lower,
339 weight);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530340 if (ret)
341 print_bind_err_msg(tz, cdev, ret);
342 }
343 }
344}
345
346static void __unbind(struct thermal_zone_device *tz, int mask,
347 struct thermal_cooling_device *cdev)
348{
349 int i;
350
351 for (i = 0; i < tz->trips; i++)
352 if (mask & (1 << i))
353 thermal_zone_unbind_cooling_device(tz, i, cdev);
354}
355
356static void bind_cdev(struct thermal_cooling_device *cdev)
357{
358 int i, ret;
359 const struct thermal_zone_params *tzp;
360 struct thermal_zone_device *pos = NULL;
361
362 mutex_lock(&thermal_list_lock);
363
364 list_for_each_entry(pos, &thermal_tz_list, node) {
365 if (!pos->tzp && !pos->ops->bind)
366 continue;
367
Ni Wadea9f2d192013-11-06 14:30:13 +0800368 if (pos->ops->bind) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530369 ret = pos->ops->bind(pos, cdev);
370 if (ret)
371 print_bind_err_msg(pos, cdev, ret);
Ni Wadea9f2d192013-11-06 14:30:13 +0800372 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530373 }
374
375 tzp = pos->tzp;
Hugh Dickins791700cd2012-09-27 16:48:28 -0700376 if (!tzp || !tzp->tbp)
377 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530378
379 for (i = 0; i < tzp->num_tbps; i++) {
380 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
381 continue;
382 if (tzp->tbp[i].match(pos, cdev))
383 continue;
384 tzp->tbp[i].cdev = cdev;
Eduardo Valentina8892d832013-07-16 15:26:28 -0400385 __bind(pos, tzp->tbp[i].trip_mask, cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000386 tzp->tbp[i].binding_limits,
387 tzp->tbp[i].weight);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530388 }
389 }
390
391 mutex_unlock(&thermal_list_lock);
392}
393
394static void bind_tz(struct thermal_zone_device *tz)
395{
396 int i, ret;
397 struct thermal_cooling_device *pos = NULL;
398 const struct thermal_zone_params *tzp = tz->tzp;
399
400 if (!tzp && !tz->ops->bind)
401 return;
402
403 mutex_lock(&thermal_list_lock);
404
Ni Wadea9f2d192013-11-06 14:30:13 +0800405 /* If there is ops->bind, try to use ops->bind */
406 if (tz->ops->bind) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530407 list_for_each_entry(pos, &thermal_cdev_list, node) {
408 ret = tz->ops->bind(tz, pos);
409 if (ret)
410 print_bind_err_msg(tz, pos, ret);
411 }
412 goto exit;
413 }
414
Hugh Dickins791700cd2012-09-27 16:48:28 -0700415 if (!tzp || !tzp->tbp)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530416 goto exit;
417
418 list_for_each_entry(pos, &thermal_cdev_list, node) {
419 for (i = 0; i < tzp->num_tbps; i++) {
420 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
421 continue;
422 if (tzp->tbp[i].match(tz, pos))
423 continue;
424 tzp->tbp[i].cdev = pos;
Eduardo Valentina8892d832013-07-16 15:26:28 -0400425 __bind(tz, tzp->tbp[i].trip_mask, pos,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000426 tzp->tbp[i].binding_limits,
427 tzp->tbp[i].weight);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530428 }
429 }
430exit:
431 mutex_unlock(&thermal_list_lock);
432}
433
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530434static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
435 int delay)
436{
437 if (delay > 1000)
438 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
439 round_jiffies(msecs_to_jiffies(delay)));
440 else if (delay)
441 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
442 msecs_to_jiffies(delay));
443 else
444 cancel_delayed_work(&tz->poll_queue);
445}
446
447static void monitor_thermal_zone(struct thermal_zone_device *tz)
448{
449 mutex_lock(&tz->lock);
450
451 if (tz->passive)
452 thermal_zone_device_set_polling(tz, tz->passive_delay);
453 else if (tz->polling_delay)
454 thermal_zone_device_set_polling(tz, tz->polling_delay);
455 else
456 thermal_zone_device_set_polling(tz, 0);
457
458 mutex_unlock(&tz->lock);
459}
460
461static void handle_non_critical_trips(struct thermal_zone_device *tz,
462 int trip, enum thermal_trip_type trip_type)
463{
Zhang Ruif2234bc2014-01-24 10:23:19 +0800464 tz->governor ? tz->governor->throttle(tz, trip) :
465 def_governor->throttle(tz, trip);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530466}
467
468static void handle_critical_trips(struct thermal_zone_device *tz,
469 int trip, enum thermal_trip_type trip_type)
470{
Sascha Hauer17e83512015-07-24 08:12:54 +0200471 int trip_temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530472
473 tz->ops->get_trip_temp(tz, trip, &trip_temp);
474
475 /* If we have not crossed the trip_temp, we do not care. */
Srinivas Pandruvada84ffe3e2014-11-12 15:43:29 -0800476 if (trip_temp <= 0 || tz->temperature < trip_temp)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530477 return;
478
Punit Agrawal208cd822014-07-29 11:50:50 +0100479 trace_thermal_zone_trip(tz, trip, trip_type);
480
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530481 if (tz->ops->notify)
482 tz->ops->notify(tz, trip, trip_type);
483
484 if (trip_type == THERMAL_TRIP_CRITICAL) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000485 dev_emerg(&tz->device,
486 "critical temperature reached(%d C),shutting down\n",
487 tz->temperature / 1000);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530488 orderly_poweroff(true);
489 }
490}
491
492static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
493{
494 enum thermal_trip_type type;
495
Zhang Rui81ad4272016-03-18 10:03:24 +0800496 /* Ignore disabled trip points */
497 if (test_bit(trip, &tz->trips_disabled))
498 return;
499
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530500 tz->ops->get_trip_type(tz, trip, &type);
501
502 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
503 handle_critical_trips(tz, trip, type);
504 else
505 handle_non_critical_trips(tz, trip, type);
506 /*
507 * Alright, we handled this trip successfully.
508 * So, start monitoring again.
509 */
510 monitor_thermal_zone(tz);
511}
512
513static void update_temperature(struct thermal_zone_device *tz)
514{
Sascha Hauer17e83512015-07-24 08:12:54 +0200515 int temp, ret;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530516
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000517 ret = thermal_zone_get_temp(tz, &temp);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530518 if (ret) {
Hans de Goede7e497a72015-03-21 15:02:55 +0100519 if (ret != -EAGAIN)
520 dev_warn(&tz->device,
521 "failed to read out thermal zone (%d)\n",
522 ret);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000523 return;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530524 }
525
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000526 mutex_lock(&tz->lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530527 tz->last_temperature = tz->temperature;
528 tz->temperature = temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530529 mutex_unlock(&tz->lock);
Aaron Lu06475b52013-12-02 13:54:26 +0800530
Punit Agrawal100a8fd2014-07-29 11:50:48 +0100531 trace_thermal_temperature(tz);
Zhang Ruibb431ba2015-10-30 16:31:47 +0800532 if (tz->last_temperature == THERMAL_TEMP_INVALID)
533 dev_dbg(&tz->device, "last_temperature N/A, current_temperature=%d\n",
534 tz->temperature);
535 else
536 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
537 tz->last_temperature, tz->temperature);
538}
539
540static void thermal_zone_device_reset(struct thermal_zone_device *tz)
541{
542 struct thermal_instance *pos;
543
544 tz->temperature = THERMAL_TEMP_INVALID;
545 tz->passive = 0;
546 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
547 pos->initialized = false;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530548}
549
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700550void thermal_zone_device_update(struct thermal_zone_device *tz,
551 enum thermal_notify_event event)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530552{
553 int count;
554
Zhang Ruiff140fe2015-10-30 16:31:58 +0800555 if (atomic_read(&in_suspend))
556 return;
557
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400558 if (!tz->ops->get_temp)
559 return;
560
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530561 update_temperature(tz);
562
Sascha Hauer060c0342016-06-22 16:42:01 +0800563 thermal_zone_set_trips(tz);
564
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700565 tz->notify_event = event;
566
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530567 for (count = 0; count < tz->trips; count++)
568 handle_thermal_trip(tz, count);
569}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000570EXPORT_SYMBOL_GPL(thermal_zone_device_update);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530571
572static void thermal_zone_device_check(struct work_struct *work)
573{
574 struct thermal_zone_device *tz = container_of(work, struct
575 thermal_zone_device,
576 poll_queue.work);
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700577 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530578}
579
Eduardo Valentin1a7e7cc2016-11-07 21:08:47 -0800580/**
581 * power_actor_get_max_power() - get the maximum power that a cdev can consume
582 * @cdev: pointer to &thermal_cooling_device
583 * @tz: a valid thermal zone device pointer
584 * @max_power: pointer in which to store the maximum power
585 *
586 * Calculate the maximum power consumption in milliwats that the
587 * cooling device can currently consume and store it in @max_power.
588 *
589 * Return: 0 on success, -EINVAL if @cdev doesn't support the
590 * power_actor API or -E* on other error.
591 */
592int power_actor_get_max_power(struct thermal_cooling_device *cdev,
593 struct thermal_zone_device *tz, u32 *max_power)
594{
595 if (!cdev_is_power_actor(cdev))
596 return -EINVAL;
597
598 return cdev->ops->state2power(cdev, tz, 0, max_power);
599}
600
601/**
602 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
603 * @cdev: pointer to &thermal_cooling_device
604 * @tz: a valid thermal zone device pointer
605 * @min_power: pointer in which to store the minimum power
606 *
607 * Calculate the minimum power consumption in milliwatts that the
608 * cooling device can currently consume and store it in @min_power.
609 *
610 * Return: 0 on success, -EINVAL if @cdev doesn't support the
611 * power_actor API or -E* on other error.
612 */
613int power_actor_get_min_power(struct thermal_cooling_device *cdev,
614 struct thermal_zone_device *tz, u32 *min_power)
615{
616 unsigned long max_state;
617 int ret;
618
619 if (!cdev_is_power_actor(cdev))
620 return -EINVAL;
621
622 ret = cdev->ops->get_max_state(cdev, &max_state);
623 if (ret)
624 return ret;
625
626 return cdev->ops->state2power(cdev, tz, max_state, min_power);
627}
628
629/**
630 * power_actor_set_power() - limit the maximum power a cooling device consumes
631 * @cdev: pointer to &thermal_cooling_device
632 * @instance: thermal instance to update
633 * @power: the power in milliwatts
634 *
635 * Set the cooling device to consume at most @power milliwatts. The limit is
636 * expected to be a cap at the maximum power consumption.
637 *
638 * Return: 0 on success, -EINVAL if the cooling device does not
639 * implement the power actor API or -E* for other failures.
640 */
641int power_actor_set_power(struct thermal_cooling_device *cdev,
642 struct thermal_instance *instance, u32 power)
643{
644 unsigned long state;
645 int ret;
646
647 if (!cdev_is_power_actor(cdev))
648 return -EINVAL;
649
650 ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
651 if (ret)
652 return ret;
653
654 instance->target = state;
655 mutex_lock(&cdev->lock);
656 cdev->updated = false;
657 mutex_unlock(&cdev->lock);
658 thermal_cdev_update(cdev);
659
660 return 0;
661}
662
Eduardo Valentin3d0055d2016-11-07 21:08:54 -0800663void thermal_zone_device_rebind_exception(struct thermal_zone_device *tz,
664 const char *cdev_type, size_t size)
665{
666 struct thermal_cooling_device *cdev = NULL;
667
668 mutex_lock(&thermal_list_lock);
669 list_for_each_entry(cdev, &thermal_cdev_list, node) {
670 /* skip non matching cdevs */
671 if (strncmp(cdev_type, cdev->type, size))
672 continue;
673
674 /* re binding the exception matching the type pattern */
675 thermal_zone_bind_cooling_device(tz, THERMAL_TRIPS_NONE, cdev,
676 THERMAL_NO_LIMIT,
677 THERMAL_NO_LIMIT,
678 THERMAL_WEIGHT_DEFAULT);
679 }
680 mutex_unlock(&thermal_list_lock);
681}
682
683void thermal_zone_device_unbind_exception(struct thermal_zone_device *tz,
684 const char *cdev_type, size_t size)
685{
686 struct thermal_cooling_device *cdev = NULL;
687
688 mutex_lock(&thermal_list_lock);
689 list_for_each_entry(cdev, &thermal_cdev_list, node) {
690 /* skip non matching cdevs */
691 if (strncmp(cdev_type, cdev->type, size))
692 continue;
693 /* unbinding the exception matching the type pattern */
694 thermal_zone_unbind_cooling_device(tz, THERMAL_TRIPS_NONE,
695 cdev);
696 }
697 mutex_unlock(&thermal_list_lock);
698}
699
Zhang Rui203d3d42008-01-17 15:51:08 +0800700/* Device management */
701
702/**
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000703 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
704 * @tz: pointer to struct thermal_zone_device
Zhang Rui203d3d42008-01-17 15:51:08 +0800705 * @trip: indicates which trip point the cooling devices is
706 * associated with in this thermal zone.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000707 * @cdev: pointer to struct thermal_cooling_device
708 * @upper: the Maximum cooling state for this trip point.
709 * THERMAL_NO_LIMIT means no upper limit,
710 * and the cooling device can be in max_state.
711 * @lower: the Minimum cooling state can be used for this trip point.
712 * THERMAL_NO_LIMIT means no lower limit,
713 * and the cooling device can be in cooling state 0.
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000714 * @weight: The weight of the cooling device to be bound to the
715 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
716 * default value
Len Brown543a9562008-02-07 16:55:08 -0500717 *
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000718 * This interface function bind a thermal cooling device to the certain trip
719 * point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -0500720 * This function is usually called in the thermal zone device .bind callback.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000721 *
722 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +0800723 */
724int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
725 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +0800726 struct thermal_cooling_device *cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000727 unsigned long upper, unsigned long lower,
728 unsigned int weight)
Zhang Rui203d3d42008-01-17 15:51:08 +0800729{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800730 struct thermal_instance *dev;
731 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500732 struct thermal_zone_device *pos1;
733 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +0800734 unsigned long max_state;
Lukasz Majewski9a3031d2014-11-18 11:16:30 +0100735 int result, ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800736
Len Brown543a9562008-02-07 16:55:08 -0500737 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800738 return -EINVAL;
739
Thomas Sujithc7516702008-02-15 00:58:50 -0500740 list_for_each_entry(pos1, &thermal_tz_list, node) {
741 if (pos1 == tz)
742 break;
743 }
744 list_for_each_entry(pos2, &thermal_cdev_list, node) {
745 if (pos2 == cdev)
746 break;
747 }
748
749 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800750 return -EINVAL;
751
Lukasz Majewski9a3031d2014-11-18 11:16:30 +0100752 ret = cdev->ops->get_max_state(cdev, &max_state);
753 if (ret)
754 return ret;
Zhang Rui9d998422012-06-26 16:35:57 +0800755
756 /* lower default 0, upper default max_state */
757 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
758 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
759
760 if (lower > upper || upper > max_state)
761 return -EINVAL;
762
Zhang Rui203d3d42008-01-17 15:51:08 +0800763 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800764 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800765 if (!dev)
766 return -ENOMEM;
767 dev->tz = tz;
768 dev->cdev = cdev;
769 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +0800770 dev->upper = upper;
771 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +0800772 dev->target = THERMAL_NO_TARGET;
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000773 dev->weight = weight;
Zhang Rui74051ba2012-06-26 16:27:22 +0800774
Zhang Rui203d3d42008-01-17 15:51:08 +0800775 result = get_idr(&tz->idr, &tz->lock, &dev->id);
776 if (result)
777 goto free_mem;
778
779 sprintf(dev->name, "cdev%d", dev->id);
780 result =
781 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
782 if (result)
783 goto release_idr;
784
785 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700786 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800787 dev->attr.attr.name = dev->attr_name;
788 dev->attr.attr.mode = 0444;
789 dev->attr.show = thermal_cooling_device_trip_point_show;
790 result = device_create_file(&tz->device, &dev->attr);
791 if (result)
792 goto remove_symbol_link;
793
Javi Merinodb916512015-02-18 16:04:24 +0000794 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
795 sysfs_attr_init(&dev->weight_attr.attr);
796 dev->weight_attr.attr.name = dev->weight_attr_name;
797 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
798 dev->weight_attr.show = thermal_cooling_device_weight_show;
799 dev->weight_attr.store = thermal_cooling_device_weight_store;
800 result = device_create_file(&tz->device, &dev->weight_attr);
801 if (result)
802 goto remove_trip_file;
803
Zhang Rui203d3d42008-01-17 15:51:08 +0800804 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800805 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800806 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +0800807 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
808 result = -EEXIST;
809 break;
810 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800811 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800812 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800813 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
Chen Yu4511f712015-10-30 16:32:10 +0800814 atomic_set(&tz->need_update, 1);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800815 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800816 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800817 mutex_unlock(&tz->lock);
818
819 if (!result)
820 return 0;
821
Javi Merinodb916512015-02-18 16:04:24 +0000822 device_remove_file(&tz->device, &dev->weight_attr);
823remove_trip_file:
Zhang Rui203d3d42008-01-17 15:51:08 +0800824 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -0700825remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +0800826 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -0700827release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +0800828 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -0700829free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +0800830 kfree(dev);
831 return result;
832}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000833EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +0800834
835/**
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000836 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
837 * thermal zone.
838 * @tz: pointer to a struct thermal_zone_device.
Zhang Rui203d3d42008-01-17 15:51:08 +0800839 * @trip: indicates which trip point the cooling devices is
840 * associated with in this thermal zone.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000841 * @cdev: pointer to a struct thermal_cooling_device.
Len Brown543a9562008-02-07 16:55:08 -0500842 *
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000843 * This interface function unbind a thermal cooling device from the certain
844 * trip point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -0500845 * This function is usually called in the thermal zone device .unbind callback.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000846 *
847 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +0800848 */
849int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
850 int trip,
851 struct thermal_cooling_device *cdev)
852{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800853 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +0800854
855 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800856 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800857 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -0500858 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800859 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800860 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800861 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800862 mutex_unlock(&tz->lock);
863 goto unbind;
864 }
865 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800866 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800867 mutex_unlock(&tz->lock);
868
869 return -ENODEV;
870
Joe Perchescaca8b82012-03-21 12:55:02 -0700871unbind:
Viresh Kumar528464e2015-07-23 14:32:32 +0530872 device_remove_file(&tz->device, &pos->weight_attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800873 device_remove_file(&tz->device, &pos->attr);
874 sysfs_remove_link(&tz->device.kobj, pos->name);
875 release_idr(&tz->idr, &tz->lock, pos->id);
876 kfree(pos);
877 return 0;
878}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000879EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +0800880
881static void thermal_release(struct device *dev)
882{
883 struct thermal_zone_device *tz;
884 struct thermal_cooling_device *cdev;
885
Joe Perchescaca8b82012-03-21 12:55:02 -0700886 if (!strncmp(dev_name(dev), "thermal_zone",
887 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800888 tz = to_thermal_zone(dev);
889 kfree(tz);
durgadoss.r@intel.com732e4c82013-10-02 00:08:00 +0530890 } else if(!strncmp(dev_name(dev), "cooling_device",
891 sizeof("cooling_device") - 1)){
Zhang Rui203d3d42008-01-17 15:51:08 +0800892 cdev = to_cooling_device(dev);
893 kfree(cdev);
894 }
895}
896
897static struct class thermal_class = {
898 .name = "thermal",
899 .dev_release = thermal_release,
900};
901
902/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400903 * __thermal_cooling_device_register() - register a new thermal cooling device
904 * @np: a pointer to a device tree node.
Zhang Rui203d3d42008-01-17 15:51:08 +0800905 * @type: the thermal cooling device type.
906 * @devdata: device private data.
907 * @ops: standard thermal cooling devices callbacks.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +0000908 *
909 * This interface function adds a new thermal cooling device (fan/processor/...)
910 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
911 * to all the thermal zone devices registered at the same time.
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400912 * It also gives the opportunity to link the cooling device to a device tree
913 * node, so that it can be bound to a thermal zone created out of device tree.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +0000914 *
915 * Return: a pointer to the created struct thermal_cooling_device or an
916 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +0800917 */
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400918static struct thermal_cooling_device *
919__thermal_cooling_device_register(struct device_node *np,
920 char *type, void *devdata,
921 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +0800922{
923 struct thermal_cooling_device *cdev;
Chen Yu4511f712015-10-30 16:32:10 +0800924 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +0800925 int result;
926
Guenter Roeck204dd1d2012-08-07 22:36:45 -0700927 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500928 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800929
930 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -0500931 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500932 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800933
934 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
935 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500936 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +0800937
938 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
939 if (result) {
940 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500941 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800942 }
943
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +0000944 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +0800945 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800946 INIT_LIST_HEAD(&cdev->thermal_instances);
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400947 cdev->np = np;
Zhang Rui203d3d42008-01-17 15:51:08 +0800948 cdev->ops = ops;
Ni Wade5ca0cce2014-02-17 11:02:55 +0800949 cdev->updated = false;
Zhang Rui203d3d42008-01-17 15:51:08 +0800950 cdev->device.class = &thermal_class;
Eduardo Valentin45cf2ec2016-11-07 21:09:02 -0800951 thermal_cooling_device_setup_sysfs(cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +0800952 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -0800953 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +0800954 result = device_register(&cdev->device);
955 if (result) {
956 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
957 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500958 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800959 }
960
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530961 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +0800962 mutex_lock(&thermal_list_lock);
963 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +0800964 mutex_unlock(&thermal_list_lock);
965
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530966 /* Update binding information for 'this' new cdev */
967 bind_cdev(cdev);
968
Chen Yu4511f712015-10-30 16:32:10 +0800969 mutex_lock(&thermal_list_lock);
970 list_for_each_entry(pos, &thermal_tz_list, node)
971 if (atomic_cmpxchg(&pos->need_update, 1, 0))
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700972 thermal_zone_device_update(pos,
973 THERMAL_EVENT_UNSPECIFIED);
Chen Yu4511f712015-10-30 16:32:10 +0800974 mutex_unlock(&thermal_list_lock);
975
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530976 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +0800977}
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400978
979/**
980 * thermal_cooling_device_register() - register a new thermal cooling device
981 * @type: the thermal cooling device type.
982 * @devdata: device private data.
983 * @ops: standard thermal cooling devices callbacks.
984 *
985 * This interface function adds a new thermal cooling device (fan/processor/...)
986 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
987 * to all the thermal zone devices registered at the same time.
988 *
989 * Return: a pointer to the created struct thermal_cooling_device or an
990 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
991 */
992struct thermal_cooling_device *
993thermal_cooling_device_register(char *type, void *devdata,
994 const struct thermal_cooling_device_ops *ops)
995{
996 return __thermal_cooling_device_register(NULL, type, devdata, ops);
997}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000998EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +0800999
1000/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001001 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1002 * @np: a pointer to a device tree node.
1003 * @type: the thermal cooling device type.
1004 * @devdata: device private data.
1005 * @ops: standard thermal cooling devices callbacks.
1006 *
1007 * This function will register a cooling device with device tree node reference.
1008 * This interface function adds a new thermal cooling device (fan/processor/...)
1009 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1010 * to all the thermal zone devices registered at the same time.
1011 *
1012 * Return: a pointer to the created struct thermal_cooling_device or an
1013 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1014 */
1015struct thermal_cooling_device *
1016thermal_of_cooling_device_register(struct device_node *np,
1017 char *type, void *devdata,
1018 const struct thermal_cooling_device_ops *ops)
1019{
1020 return __thermal_cooling_device_register(np, type, devdata, ops);
1021}
1022EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1023
1024/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001025 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001026 * @cdev: the thermal cooling device to remove.
1027 *
1028 * thermal_cooling_device_unregister() must be called when the device is no
1029 * longer needed.
1030 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301031void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001032{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301033 int i;
1034 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001035 struct thermal_zone_device *tz;
1036 struct thermal_cooling_device *pos = NULL;
1037
1038 if (!cdev)
1039 return;
1040
1041 mutex_lock(&thermal_list_lock);
1042 list_for_each_entry(pos, &thermal_cdev_list, node)
1043 if (pos == cdev)
1044 break;
1045 if (pos != cdev) {
1046 /* thermal cooling device not found */
1047 mutex_unlock(&thermal_list_lock);
1048 return;
1049 }
1050 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301051
1052 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001053 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301054 if (tz->ops->unbind) {
1055 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001056 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301057 }
1058
1059 if (!tz->tzp || !tz->tzp->tbp)
1060 continue;
1061
1062 tzp = tz->tzp;
1063 for (i = 0; i < tzp->num_tbps; i++) {
1064 if (tzp->tbp[i].cdev == cdev) {
1065 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1066 tzp->tbp[i].cdev = NULL;
1067 }
1068 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001069 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301070
Zhang Rui203d3d42008-01-17 15:51:08 +08001071 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301072
Zhang Rui203d3d42008-01-17 15:51:08 +08001073 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1074 device_unregister(&cdev->device);
1075 return;
1076}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001077EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001078
Matthew Garrettb1569e92008-12-03 17:55:32 +00001079/**
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001080 * thermal_notify_framework - Sensor drivers use this API to notify framework
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301081 * @tz: thermal zone device
1082 * @trip: indicates which trip point has been crossed
1083 *
1084 * This function handles the trip events from sensor drivers. It starts
1085 * throttling the cooling devices according to the policy configured.
1086 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1087 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1088 * The throttling policy is based on the configured platform data; if no
1089 * platform data is provided, this uses the step_wise throttling policy.
1090 */
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001091void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301092{
1093 handle_thermal_trip(tz, trip);
1094}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001095EXPORT_SYMBOL_GPL(thermal_notify_framework);
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301096
1097/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001098 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001099 * @type: the thermal zone device type
1100 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001101 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001102 * @devdata: private device data
1103 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301104 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001105 * @passive_delay: number of milliseconds to wait between polls when
1106 * performing passive cooling
1107 * @polling_delay: number of milliseconds to wait between polls when checking
1108 * whether trip points have been crossed (0 for interrupt
1109 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001110 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001111 * This interface function adds a new thermal zone device (sensor) to
1112 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1113 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08001114 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001115 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001116 *
1117 * Return: a pointer to the created struct thermal_zone_device or an
1118 * in case of error, an ERR_PTR. Caller must check return value with
1119 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001120 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001121struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001122 int trips, int mask, void *devdata,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001123 struct thermal_zone_device_ops *ops,
Javi Merino6b775e82015-03-02 17:17:19 +00001124 struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001125 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001126{
1127 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001128 enum thermal_trip_type trip_type;
Zhang Rui81ad4272016-03-18 10:03:24 +08001129 int trip_temp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001130 int result;
1131 int count;
Javi Merinoe33df1d2015-02-26 19:00:27 +00001132 struct thermal_governor *governor;
Zhang Rui203d3d42008-01-17 15:51:08 +08001133
Eduardo Valentin54fa38c2016-11-07 21:08:39 -08001134 if (!type || strlen(type) == 0)
1135 return ERR_PTR(-EINVAL);
1136
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001137 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001138 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001139
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001140 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001141 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001142
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04001143 if (!ops)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001144 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001145
Jonghwa Lee83720d02013-05-18 09:50:26 +00001146 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00001147 return ERR_PTR(-EINVAL);
1148
Zhang Rui203d3d42008-01-17 15:51:08 +08001149 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1150 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001151 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001152
Zhang Rui2d374132012-06-27 10:09:00 +08001153 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001154 idr_init(&tz->idr);
1155 mutex_init(&tz->lock);
1156 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1157 if (result) {
1158 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001159 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001160 }
1161
Eduardo Valentin54fa38c2016-11-07 21:08:39 -08001162 strlcpy(tz->type, type, sizeof(tz->type));
Zhang Rui203d3d42008-01-17 15:51:08 +08001163 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301164 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001165 tz->device.class = &thermal_class;
1166 tz->devdata = devdata;
1167 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001168 tz->passive_delay = passive_delay;
1169 tz->polling_delay = polling_delay;
Eduardo Valentin1c600862016-11-07 21:08:42 -08001170
Eduardo Valentin4d0fe742016-11-07 21:08:52 -08001171 /* sys I/F */
Eduardo Valentin1c600862016-11-07 21:08:42 -08001172 /* Add nodes that are always present via .groups */
Eduardo Valentin4d0fe742016-11-07 21:08:52 -08001173 result = thermal_zone_create_device_groups(tz, mask);
1174 if (result)
1175 goto unregister;
1176
Chen Yu4511f712015-10-30 16:32:10 +08001177 /* A new thermal zone needs to be updated anyway. */
1178 atomic_set(&tz->need_update, 1);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001179
Kay Sievers354655e2009-01-06 10:44:37 -08001180 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001181 result = device_register(&tz->device);
1182 if (result) {
1183 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1184 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001185 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001186 }
1187
Zhang Rui203d3d42008-01-17 15:51:08 +08001188 for (count = 0; count < trips; count++) {
Zhang Rui81ad4272016-03-18 10:03:24 +08001189 if (tz->ops->get_trip_type(tz, count, &trip_type))
1190 set_bit(count, &tz->trips_disabled);
Zhang Rui81ad4272016-03-18 10:03:24 +08001191 if (tz->ops->get_trip_temp(tz, count, &trip_temp))
1192 set_bit(count, &tz->trips_disabled);
1193 /* Check for bogus trip points */
1194 if (trip_temp == 0)
1195 set_bit(count, &tz->trips_disabled);
Zhang Rui203d3d42008-01-17 15:51:08 +08001196 }
1197
Durgadoss Ra4a15482012-09-18 11:04:57 +05301198 /* Update 'this' zone's governor information */
1199 mutex_lock(&thermal_governor_lock);
1200
1201 if (tz->tzp)
Javi Merinoe33df1d2015-02-26 19:00:27 +00001202 governor = __find_governor(tz->tzp->governor_name);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301203 else
Javi Merinoe33df1d2015-02-26 19:00:27 +00001204 governor = def_governor;
1205
1206 result = thermal_set_governor(tz, governor);
1207 if (result) {
1208 mutex_unlock(&thermal_governor_lock);
1209 goto unregister;
1210 }
Durgadoss Ra4a15482012-09-18 11:04:57 +05301211
1212 mutex_unlock(&thermal_governor_lock);
1213
Eduardo Valentinccba4ff2013-08-15 11:34:17 -04001214 if (!tz->tzp || !tz->tzp->no_hwmon) {
1215 result = thermal_add_hwmon_sysfs(tz);
1216 if (result)
1217 goto unregister;
1218 }
Zhang Ruie68b16a2008-04-21 16:07:52 +08001219
Zhang Rui203d3d42008-01-17 15:51:08 +08001220 mutex_lock(&thermal_list_lock);
1221 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001222 mutex_unlock(&thermal_list_lock);
1223
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301224 /* Bind cooling devices for this zone */
1225 bind_tz(tz);
1226
Matthew Garrettb1569e92008-12-03 17:55:32 +00001227 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1228
Zhang Ruibb431ba2015-10-30 16:31:47 +08001229 thermal_zone_device_reset(tz);
Chen Yu4511f712015-10-30 16:32:10 +08001230 /* Update the new thermal zone and mark it as already updated. */
1231 if (atomic_cmpxchg(&tz->need_update, 1, 0))
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07001232 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001233
Yao Dongdong14015862014-10-28 07:40:25 +00001234 return tz;
Zhang Rui203d3d42008-01-17 15:51:08 +08001235
Joe Perchescaca8b82012-03-21 12:55:02 -07001236unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001237 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1238 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001239 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001240}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001241EXPORT_SYMBOL_GPL(thermal_zone_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001242
1243/**
1244 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001245 * @tz: the thermal zone device to remove
1246 */
1247void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1248{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301249 int i;
1250 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001251 struct thermal_cooling_device *cdev;
1252 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001253
1254 if (!tz)
1255 return;
1256
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301257 tzp = tz->tzp;
1258
Zhang Rui203d3d42008-01-17 15:51:08 +08001259 mutex_lock(&thermal_list_lock);
1260 list_for_each_entry(pos, &thermal_tz_list, node)
1261 if (pos == tz)
1262 break;
1263 if (pos != tz) {
1264 /* thermal zone device not found */
1265 mutex_unlock(&thermal_list_lock);
1266 return;
1267 }
1268 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301269
1270 /* Unbind all cdevs associated with 'this' thermal zone */
1271 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1272 if (tz->ops->unbind) {
1273 tz->ops->unbind(tz, cdev);
1274 continue;
1275 }
1276
1277 if (!tzp || !tzp->tbp)
1278 break;
1279
1280 for (i = 0; i < tzp->num_tbps; i++) {
1281 if (tzp->tbp[i].cdev == cdev) {
1282 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1283 tzp->tbp[i].cdev = NULL;
1284 }
1285 }
1286 }
1287
Zhang Rui203d3d42008-01-17 15:51:08 +08001288 mutex_unlock(&thermal_list_lock);
1289
Matthew Garrettb1569e92008-12-03 17:55:32 +00001290 thermal_zone_device_set_polling(tz, 0);
1291
Eduardo Valentin4d0fe742016-11-07 21:08:52 -08001292 kfree(tz->trip_type_attrs);
1293 kfree(tz->trip_temp_attrs);
1294 kfree(tz->trip_hyst_attrs);
1295 kfree(tz->trips_attribute_group.attrs);
Javi Merinoe33df1d2015-02-26 19:00:27 +00001296 thermal_set_governor(tz, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001297
Zhang Ruie68b16a2008-04-21 16:07:52 +08001298 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001299 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1300 idr_destroy(&tz->idr);
1301 mutex_destroy(&tz->lock);
1302 device_unregister(&tz->device);
Eduardo Valentine161aef2016-11-07 21:08:51 -08001303 kfree(tz->device.groups);
Zhang Rui203d3d42008-01-17 15:51:08 +08001304}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001305EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001306
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001307/**
1308 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1309 * @name: thermal zone name to fetch the temperature
1310 *
1311 * When only one zone is found with the passed name, returns a reference to it.
1312 *
1313 * Return: On success returns a reference to an unique thermal zone with
1314 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1315 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1316 */
1317struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1318{
1319 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1320 unsigned int found = 0;
1321
1322 if (!name)
1323 goto exit;
1324
1325 mutex_lock(&thermal_list_lock);
1326 list_for_each_entry(pos, &thermal_tz_list, node)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -07001327 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001328 found++;
1329 ref = pos;
1330 }
1331 mutex_unlock(&thermal_list_lock);
1332
1333 /* nothing has been found, thus an error code for it */
1334 if (found == 0)
1335 ref = ERR_PTR(-ENODEV);
1336 else if (found > 1)
1337 /* Success only when an unique zone is found */
1338 ref = ERR_PTR(-EEXIST);
1339
1340exit:
1341 return ref;
1342}
1343EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1344
Rajendra Nayak4a7069a2016-05-05 14:21:42 +05301345/**
1346 * thermal_zone_get_slope - return the slope attribute of the thermal zone
1347 * @tz: thermal zone device with the slope attribute
1348 *
1349 * Return: If the thermal zone device has a slope attribute, return it, else
1350 * return 1.
1351 */
1352int thermal_zone_get_slope(struct thermal_zone_device *tz)
1353{
1354 if (tz && tz->tzp)
1355 return tz->tzp->slope;
1356 return 1;
1357}
1358EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
1359
1360/**
1361 * thermal_zone_get_offset - return the offset attribute of the thermal zone
1362 * @tz: thermal zone device with the offset attribute
1363 *
1364 * Return: If the thermal zone device has a offset attribute, return it, else
1365 * return 0.
1366 */
1367int thermal_zone_get_offset(struct thermal_zone_device *tz)
1368{
1369 if (tz && tz->tzp)
1370 return tz->tzp->offset;
1371 return 0;
1372}
1373EXPORT_SYMBOL_GPL(thermal_zone_get_offset);
1374
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001375#ifdef CONFIG_NET
Johannes Berg2a94fe42013-11-19 15:19:39 +01001376static const struct genl_multicast_group thermal_event_mcgrps[] = {
1377 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
1378};
1379
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001380static struct genl_family thermal_event_genl_family = {
1381 .id = GENL_ID_GENERATE,
1382 .name = THERMAL_GENL_FAMILY_NAME,
1383 .version = THERMAL_GENL_VERSION,
1384 .maxattr = THERMAL_GENL_ATTR_MAX,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001385 .mcgrps = thermal_event_mcgrps,
1386 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001387};
1388
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001389int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1390 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301391{
1392 struct sk_buff *skb;
1393 struct nlattr *attr;
1394 struct thermal_genl_event *thermal_event;
1395 void *msg_header;
1396 int size;
1397 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001398 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301399
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001400 if (!tz)
1401 return -EINVAL;
1402
R.Durgadoss4cb18722010-10-27 03:33:29 +05301403 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001404 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1405 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301406
1407 skb = genlmsg_new(size, GFP_ATOMIC);
1408 if (!skb)
1409 return -ENOMEM;
1410
1411 /* add the genetlink message header */
1412 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1413 &thermal_event_genl_family, 0,
1414 THERMAL_GENL_CMD_EVENT);
1415 if (!msg_header) {
1416 nlmsg_free(skb);
1417 return -ENOMEM;
1418 }
1419
1420 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001421 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1422 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301423
1424 if (!attr) {
1425 nlmsg_free(skb);
1426 return -EINVAL;
1427 }
1428
1429 thermal_event = nla_data(attr);
1430 if (!thermal_event) {
1431 nlmsg_free(skb);
1432 return -EINVAL;
1433 }
1434
1435 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1436
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001437 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301438 thermal_event->event = event;
1439
1440 /* send multicast genetlink message */
Johannes Berg053c0952015-01-16 22:09:00 +01001441 genlmsg_end(skb, msg_header);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301442
Johannes Berg68eb5502013-11-19 15:19:38 +01001443 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001444 0, GFP_ATOMIC);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301445 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00001446 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301447
1448 return result;
1449}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001450EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301451
1452static int genetlink_init(void)
1453{
Johannes Berg2a94fe42013-11-19 15:19:39 +01001454 return genl_register_family(&thermal_event_genl_family);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301455}
1456
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001457static void genetlink_exit(void)
1458{
1459 genl_unregister_family(&thermal_event_genl_family);
1460}
1461#else /* !CONFIG_NET */
1462static inline int genetlink_init(void) { return 0; }
1463static inline void genetlink_exit(void) {}
1464#endif /* !CONFIG_NET */
1465
Zhang Ruiff140fe2015-10-30 16:31:58 +08001466static int thermal_pm_notify(struct notifier_block *nb,
1467 unsigned long mode, void *_unused)
1468{
1469 struct thermal_zone_device *tz;
1470
1471 switch (mode) {
1472 case PM_HIBERNATION_PREPARE:
1473 case PM_RESTORE_PREPARE:
1474 case PM_SUSPEND_PREPARE:
1475 atomic_set(&in_suspend, 1);
1476 break;
1477 case PM_POST_HIBERNATION:
1478 case PM_POST_RESTORE:
1479 case PM_POST_SUSPEND:
1480 atomic_set(&in_suspend, 0);
1481 list_for_each_entry(tz, &thermal_tz_list, node) {
1482 thermal_zone_device_reset(tz);
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07001483 thermal_zone_device_update(tz,
1484 THERMAL_EVENT_UNSPECIFIED);
Zhang Ruiff140fe2015-10-30 16:31:58 +08001485 }
1486 break;
1487 default:
1488 break;
1489 }
1490 return 0;
1491}
1492
1493static struct notifier_block thermal_pm_nb = {
1494 .notifier_call = thermal_pm_notify,
1495};
1496
Zhang Rui203d3d42008-01-17 15:51:08 +08001497static int __init thermal_init(void)
1498{
Zhang Rui80a26a52013-03-26 16:38:29 +08001499 int result;
1500
1501 result = thermal_register_governors();
1502 if (result)
1503 goto error;
Zhang Rui203d3d42008-01-17 15:51:08 +08001504
1505 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001506 if (result)
1507 goto unregister_governors;
1508
R.Durgadoss4cb18722010-10-27 03:33:29 +05301509 result = genetlink_init();
Zhang Rui80a26a52013-03-26 16:38:29 +08001510 if (result)
1511 goto unregister_class;
1512
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001513 result = of_parse_thermal_zones();
1514 if (result)
1515 goto exit_netlink;
1516
Zhang Ruiff140fe2015-10-30 16:31:58 +08001517 result = register_pm_notifier(&thermal_pm_nb);
1518 if (result)
1519 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1520 result);
1521
Zhang Rui80a26a52013-03-26 16:38:29 +08001522 return 0;
1523
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001524exit_netlink:
1525 genetlink_exit();
Zhang Rui80a26a52013-03-26 16:38:29 +08001526unregister_class:
1527 class_unregister(&thermal_class);
Luis Henriques9d367e52014-12-03 21:20:21 +00001528unregister_governors:
1529 thermal_unregister_governors();
Zhang Rui80a26a52013-03-26 16:38:29 +08001530error:
1531 idr_destroy(&thermal_tz_idr);
1532 idr_destroy(&thermal_cdev_idr);
1533 mutex_destroy(&thermal_idr_lock);
1534 mutex_destroy(&thermal_list_lock);
1535 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001536 return result;
1537}
1538
1539static void __exit thermal_exit(void)
1540{
Zhang Ruiff140fe2015-10-30 16:31:58 +08001541 unregister_pm_notifier(&thermal_pm_nb);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001542 of_thermal_destroy_zones();
Zhang Rui80a26a52013-03-26 16:38:29 +08001543 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001544 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001545 thermal_unregister_governors();
Zhang Rui203d3d42008-01-17 15:51:08 +08001546 idr_destroy(&thermal_tz_idr);
1547 idr_destroy(&thermal_cdev_idr);
1548 mutex_destroy(&thermal_idr_lock);
1549 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08001550 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001551}
1552
R.Durgadoss4cb18722010-10-27 03:33:29 +05301553fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001554module_exit(thermal_exit);