blob: bf108db6ae5dddb89d6a1ca1b16878d3694d4537 [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
Eduardo Valentin8772e182016-11-07 21:09:15 -0800289/*
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 R0c01ebb2012-09-18 11:05:04 +0530300static 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
313static 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
327static void handle_non_critical_trips(struct thermal_zone_device *tz,
328 int trip, enum thermal_trip_type trip_type)
329{
Zhang Ruif2234bc2014-01-24 10:23:19 +0800330 tz->governor ? tz->governor->throttle(tz, trip) :
331 def_governor->throttle(tz, trip);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530332}
333
334static void handle_critical_trips(struct thermal_zone_device *tz,
335 int trip, enum thermal_trip_type trip_type)
336{
Sascha Hauer17e83512015-07-24 08:12:54 +0200337 int trip_temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530338
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 Pandruvada84ffe3e2014-11-12 15:43:29 -0800342 if (trip_temp <= 0 || tz->temperature < trip_temp)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530343 return;
344
Punit Agrawal208cd822014-07-29 11:50:50 +0100345 trace_thermal_zone_trip(tz, trip, trip_type);
346
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530347 if (tz->ops->notify)
348 tz->ops->notify(tz, trip, trip_type);
349
350 if (trip_type == THERMAL_TRIP_CRITICAL) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000351 dev_emerg(&tz->device,
352 "critical temperature reached(%d C),shutting down\n",
353 tz->temperature / 1000);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530354 orderly_poweroff(true);
355 }
356}
357
358static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
359{
360 enum thermal_trip_type type;
361
Zhang Rui81ad4272016-03-18 10:03:24 +0800362 /* Ignore disabled trip points */
363 if (test_bit(trip, &tz->trips_disabled))
364 return;
365
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530366 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
379static void update_temperature(struct thermal_zone_device *tz)
380{
Sascha Hauer17e83512015-07-24 08:12:54 +0200381 int temp, ret;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530382
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000383 ret = thermal_zone_get_temp(tz, &temp);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530384 if (ret) {
Hans de Goede7e497a72015-03-21 15:02:55 +0100385 if (ret != -EAGAIN)
386 dev_warn(&tz->device,
387 "failed to read out thermal zone (%d)\n",
388 ret);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000389 return;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530390 }
391
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000392 mutex_lock(&tz->lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530393 tz->last_temperature = tz->temperature;
394 tz->temperature = temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530395 mutex_unlock(&tz->lock);
Aaron Lu06475b52013-12-02 13:54:26 +0800396
Punit Agrawal100a8fd2014-07-29 11:50:48 +0100397 trace_thermal_temperature(tz);
Zhang Ruibb431ba2015-10-30 16:31:47 +0800398 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
406static 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 R0c01ebb2012-09-18 11:05:04 +0530414}
415
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700416void thermal_zone_device_update(struct thermal_zone_device *tz,
417 enum thermal_notify_event event)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530418{
419 int count;
420
Zhang Ruiff140fe2015-10-30 16:31:58 +0800421 if (atomic_read(&in_suspend))
422 return;
423
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400424 if (!tz->ops->get_temp)
425 return;
426
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530427 update_temperature(tz);
428
Sascha Hauer060c0342016-06-22 16:42:01 +0800429 thermal_zone_set_trips(tz);
430
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700431 tz->notify_event = event;
432
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530433 for (count = 0; count < tz->trips; count++)
434 handle_thermal_trip(tz, count);
435}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000436EXPORT_SYMBOL_GPL(thermal_zone_device_update);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530437
Eduardo Valentin106339a2016-11-07 21:09:14 -0800438/**
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 */
450void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
451{
452 handle_thermal_trip(tz, trip);
453}
454EXPORT_SYMBOL_GPL(thermal_notify_framework);
455
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530456static 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 Pandruvada0e70f462016-08-26 16:21:16 -0700461 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530462}
463
Eduardo Valentin1a7e7cc2016-11-07 21:08:47 -0800464/**
465 * power_actor_get_max_power() - get the maximum power that a cdev can consume
466 * @cdev: pointer to &thermal_cooling_device
467 * @tz: a valid thermal zone device pointer
468 * @max_power: pointer in which to store the maximum power
469 *
470 * Calculate the maximum power consumption in milliwats that the
471 * cooling device can currently consume and store it in @max_power.
472 *
473 * Return: 0 on success, -EINVAL if @cdev doesn't support the
474 * power_actor API or -E* on other error.
475 */
476int power_actor_get_max_power(struct thermal_cooling_device *cdev,
477 struct thermal_zone_device *tz, u32 *max_power)
478{
479 if (!cdev_is_power_actor(cdev))
480 return -EINVAL;
481
482 return cdev->ops->state2power(cdev, tz, 0, max_power);
483}
484
485/**
486 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
487 * @cdev: pointer to &thermal_cooling_device
488 * @tz: a valid thermal zone device pointer
489 * @min_power: pointer in which to store the minimum power
490 *
491 * Calculate the minimum power consumption in milliwatts that the
492 * cooling device can currently consume and store it in @min_power.
493 *
494 * Return: 0 on success, -EINVAL if @cdev doesn't support the
495 * power_actor API or -E* on other error.
496 */
497int power_actor_get_min_power(struct thermal_cooling_device *cdev,
498 struct thermal_zone_device *tz, u32 *min_power)
499{
500 unsigned long max_state;
501 int ret;
502
503 if (!cdev_is_power_actor(cdev))
504 return -EINVAL;
505
506 ret = cdev->ops->get_max_state(cdev, &max_state);
507 if (ret)
508 return ret;
509
510 return cdev->ops->state2power(cdev, tz, max_state, min_power);
511}
512
513/**
514 * power_actor_set_power() - limit the maximum power a cooling device consumes
515 * @cdev: pointer to &thermal_cooling_device
516 * @instance: thermal instance to update
517 * @power: the power in milliwatts
518 *
519 * Set the cooling device to consume at most @power milliwatts. The limit is
520 * expected to be a cap at the maximum power consumption.
521 *
522 * Return: 0 on success, -EINVAL if the cooling device does not
523 * implement the power actor API or -E* for other failures.
524 */
525int power_actor_set_power(struct thermal_cooling_device *cdev,
526 struct thermal_instance *instance, u32 power)
527{
528 unsigned long state;
529 int ret;
530
531 if (!cdev_is_power_actor(cdev))
532 return -EINVAL;
533
534 ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
535 if (ret)
536 return ret;
537
538 instance->target = state;
539 mutex_lock(&cdev->lock);
540 cdev->updated = false;
541 mutex_unlock(&cdev->lock);
542 thermal_cdev_update(cdev);
543
544 return 0;
545}
546
Eduardo Valentin3d0055d2016-11-07 21:08:54 -0800547void thermal_zone_device_rebind_exception(struct thermal_zone_device *tz,
548 const char *cdev_type, size_t size)
549{
550 struct thermal_cooling_device *cdev = NULL;
551
552 mutex_lock(&thermal_list_lock);
553 list_for_each_entry(cdev, &thermal_cdev_list, node) {
554 /* skip non matching cdevs */
555 if (strncmp(cdev_type, cdev->type, size))
556 continue;
557
558 /* re binding the exception matching the type pattern */
559 thermal_zone_bind_cooling_device(tz, THERMAL_TRIPS_NONE, cdev,
560 THERMAL_NO_LIMIT,
561 THERMAL_NO_LIMIT,
562 THERMAL_WEIGHT_DEFAULT);
563 }
564 mutex_unlock(&thermal_list_lock);
565}
566
567void thermal_zone_device_unbind_exception(struct thermal_zone_device *tz,
568 const char *cdev_type, size_t size)
569{
570 struct thermal_cooling_device *cdev = NULL;
571
572 mutex_lock(&thermal_list_lock);
573 list_for_each_entry(cdev, &thermal_cdev_list, node) {
574 /* skip non matching cdevs */
575 if (strncmp(cdev_type, cdev->type, size))
576 continue;
577 /* unbinding the exception matching the type pattern */
578 thermal_zone_unbind_cooling_device(tz, THERMAL_TRIPS_NONE,
579 cdev);
580 }
581 mutex_unlock(&thermal_list_lock);
582}
583
Zhang Rui203d3d42008-01-17 15:51:08 +0800584/* Device management */
585
Eduardo Valentinc30176f2016-11-07 21:09:06 -0800586static int get_idr(struct idr *idr, struct mutex *lock, int *id)
587{
588 int ret;
589
590 if (lock)
591 mutex_lock(lock);
592 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
593 if (lock)
594 mutex_unlock(lock);
595 if (unlikely(ret < 0))
596 return ret;
597 *id = ret;
598 return 0;
599}
600
601static void release_idr(struct idr *idr, struct mutex *lock, int id)
602{
603 if (lock)
604 mutex_lock(lock);
605 idr_remove(idr, id);
606 if (lock)
607 mutex_unlock(lock);
608}
609
Zhang Rui203d3d42008-01-17 15:51:08 +0800610/**
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000611 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
612 * @tz: pointer to struct thermal_zone_device
Zhang Rui203d3d42008-01-17 15:51:08 +0800613 * @trip: indicates which trip point the cooling devices is
614 * associated with in this thermal zone.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000615 * @cdev: pointer to struct thermal_cooling_device
616 * @upper: the Maximum cooling state for this trip point.
617 * THERMAL_NO_LIMIT means no upper limit,
618 * and the cooling device can be in max_state.
619 * @lower: the Minimum cooling state can be used for this trip point.
620 * THERMAL_NO_LIMIT means no lower limit,
621 * and the cooling device can be in cooling state 0.
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000622 * @weight: The weight of the cooling device to be bound to the
623 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
624 * default value
Len Brown543a9562008-02-07 16:55:08 -0500625 *
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000626 * This interface function bind a thermal cooling device to the certain trip
627 * point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -0500628 * This function is usually called in the thermal zone device .bind callback.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000629 *
630 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +0800631 */
632int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
633 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +0800634 struct thermal_cooling_device *cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000635 unsigned long upper, unsigned long lower,
636 unsigned int weight)
Zhang Rui203d3d42008-01-17 15:51:08 +0800637{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800638 struct thermal_instance *dev;
639 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500640 struct thermal_zone_device *pos1;
641 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +0800642 unsigned long max_state;
Lukasz Majewski9a3031d2014-11-18 11:16:30 +0100643 int result, ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800644
Len Brown543a9562008-02-07 16:55:08 -0500645 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800646 return -EINVAL;
647
Thomas Sujithc7516702008-02-15 00:58:50 -0500648 list_for_each_entry(pos1, &thermal_tz_list, node) {
649 if (pos1 == tz)
650 break;
651 }
652 list_for_each_entry(pos2, &thermal_cdev_list, node) {
653 if (pos2 == cdev)
654 break;
655 }
656
657 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800658 return -EINVAL;
659
Lukasz Majewski9a3031d2014-11-18 11:16:30 +0100660 ret = cdev->ops->get_max_state(cdev, &max_state);
661 if (ret)
662 return ret;
Zhang Rui9d998422012-06-26 16:35:57 +0800663
664 /* lower default 0, upper default max_state */
665 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
666 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
667
668 if (lower > upper || upper > max_state)
669 return -EINVAL;
670
Zhang Rui203d3d42008-01-17 15:51:08 +0800671 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800672 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800673 if (!dev)
674 return -ENOMEM;
675 dev->tz = tz;
676 dev->cdev = cdev;
677 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +0800678 dev->upper = upper;
679 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +0800680 dev->target = THERMAL_NO_TARGET;
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000681 dev->weight = weight;
Zhang Rui74051ba2012-06-26 16:27:22 +0800682
Zhang Rui203d3d42008-01-17 15:51:08 +0800683 result = get_idr(&tz->idr, &tz->lock, &dev->id);
684 if (result)
685 goto free_mem;
686
687 sprintf(dev->name, "cdev%d", dev->id);
688 result =
689 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
690 if (result)
691 goto release_idr;
692
693 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700694 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800695 dev->attr.attr.name = dev->attr_name;
696 dev->attr.attr.mode = 0444;
697 dev->attr.show = thermal_cooling_device_trip_point_show;
698 result = device_create_file(&tz->device, &dev->attr);
699 if (result)
700 goto remove_symbol_link;
701
Javi Merinodb916512015-02-18 16:04:24 +0000702 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
703 sysfs_attr_init(&dev->weight_attr.attr);
704 dev->weight_attr.attr.name = dev->weight_attr_name;
705 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
706 dev->weight_attr.show = thermal_cooling_device_weight_show;
707 dev->weight_attr.store = thermal_cooling_device_weight_store;
708 result = device_create_file(&tz->device, &dev->weight_attr);
709 if (result)
710 goto remove_trip_file;
711
Zhang Rui203d3d42008-01-17 15:51:08 +0800712 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800713 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800714 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +0800715 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
716 result = -EEXIST;
717 break;
718 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800719 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800720 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800721 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
Chen Yu4511f712015-10-30 16:32:10 +0800722 atomic_set(&tz->need_update, 1);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800723 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800724 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800725 mutex_unlock(&tz->lock);
726
727 if (!result)
728 return 0;
729
Javi Merinodb916512015-02-18 16:04:24 +0000730 device_remove_file(&tz->device, &dev->weight_attr);
731remove_trip_file:
Zhang Rui203d3d42008-01-17 15:51:08 +0800732 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -0700733remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +0800734 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -0700735release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +0800736 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -0700737free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +0800738 kfree(dev);
739 return result;
740}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000741EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +0800742
743/**
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000744 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
745 * thermal zone.
746 * @tz: pointer to a struct thermal_zone_device.
Zhang Rui203d3d42008-01-17 15:51:08 +0800747 * @trip: indicates which trip point the cooling devices is
748 * associated with in this thermal zone.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000749 * @cdev: pointer to a struct thermal_cooling_device.
Len Brown543a9562008-02-07 16:55:08 -0500750 *
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000751 * This interface function unbind a thermal cooling device from the certain
752 * trip point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -0500753 * This function is usually called in the thermal zone device .unbind callback.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000754 *
755 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +0800756 */
757int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
758 int trip,
759 struct thermal_cooling_device *cdev)
760{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800761 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +0800762
763 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800764 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800765 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -0500766 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800767 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800768 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800769 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800770 mutex_unlock(&tz->lock);
771 goto unbind;
772 }
773 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800774 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800775 mutex_unlock(&tz->lock);
776
777 return -ENODEV;
778
Joe Perchescaca8b82012-03-21 12:55:02 -0700779unbind:
Viresh Kumar528464e2015-07-23 14:32:32 +0530780 device_remove_file(&tz->device, &pos->weight_attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800781 device_remove_file(&tz->device, &pos->attr);
782 sysfs_remove_link(&tz->device.kobj, pos->name);
783 release_idr(&tz->idr, &tz->lock, pos->id);
784 kfree(pos);
785 return 0;
786}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000787EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +0800788
789static void thermal_release(struct device *dev)
790{
791 struct thermal_zone_device *tz;
792 struct thermal_cooling_device *cdev;
793
Joe Perchescaca8b82012-03-21 12:55:02 -0700794 if (!strncmp(dev_name(dev), "thermal_zone",
795 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800796 tz = to_thermal_zone(dev);
797 kfree(tz);
durgadoss.r@intel.com732e4c82013-10-02 00:08:00 +0530798 } else if(!strncmp(dev_name(dev), "cooling_device",
799 sizeof("cooling_device") - 1)){
Zhang Rui203d3d42008-01-17 15:51:08 +0800800 cdev = to_cooling_device(dev);
801 kfree(cdev);
802 }
803}
804
805static struct class thermal_class = {
806 .name = "thermal",
807 .dev_release = thermal_release,
808};
809
Eduardo Valentin4b0d3c22016-11-07 21:09:13 -0800810static inline
811void print_bind_err_msg(struct thermal_zone_device *tz,
812 struct thermal_cooling_device *cdev, int ret)
Eduardo Valentinf502ab82016-11-07 21:09:12 -0800813{
814 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
815 tz->type, cdev->type, ret);
816}
817
818static void __bind(struct thermal_zone_device *tz, int mask,
819 struct thermal_cooling_device *cdev,
820 unsigned long *limits,
821 unsigned int weight)
822{
823 int i, ret;
824
825 for (i = 0; i < tz->trips; i++) {
826 if (mask & (1 << i)) {
827 unsigned long upper, lower;
828
829 upper = THERMAL_NO_LIMIT;
830 lower = THERMAL_NO_LIMIT;
831 if (limits) {
832 lower = limits[i * 2];
833 upper = limits[i * 2 + 1];
834 }
835 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
836 upper, lower,
837 weight);
838 if (ret)
839 print_bind_err_msg(tz, cdev, ret);
840 }
841 }
842}
843
Eduardo Valentin949aad82016-11-07 21:09:09 -0800844static void bind_cdev(struct thermal_cooling_device *cdev)
845{
846 int i, ret;
847 const struct thermal_zone_params *tzp;
848 struct thermal_zone_device *pos = NULL;
849
850 mutex_lock(&thermal_list_lock);
851
852 list_for_each_entry(pos, &thermal_tz_list, node) {
853 if (!pos->tzp && !pos->ops->bind)
854 continue;
855
856 if (pos->ops->bind) {
857 ret = pos->ops->bind(pos, cdev);
858 if (ret)
859 print_bind_err_msg(pos, cdev, ret);
860 continue;
861 }
862
863 tzp = pos->tzp;
864 if (!tzp || !tzp->tbp)
865 continue;
866
867 for (i = 0; i < tzp->num_tbps; i++) {
868 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
869 continue;
870 if (tzp->tbp[i].match(pos, cdev))
871 continue;
872 tzp->tbp[i].cdev = cdev;
873 __bind(pos, tzp->tbp[i].trip_mask, cdev,
874 tzp->tbp[i].binding_limits,
875 tzp->tbp[i].weight);
876 }
877 }
878
879 mutex_unlock(&thermal_list_lock);
880}
881
Zhang Rui203d3d42008-01-17 15:51:08 +0800882/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400883 * __thermal_cooling_device_register() - register a new thermal cooling device
884 * @np: a pointer to a device tree node.
Zhang Rui203d3d42008-01-17 15:51:08 +0800885 * @type: the thermal cooling device type.
886 * @devdata: device private data.
887 * @ops: standard thermal cooling devices callbacks.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +0000888 *
889 * This interface function adds a new thermal cooling device (fan/processor/...)
890 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
891 * to all the thermal zone devices registered at the same time.
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400892 * It also gives the opportunity to link the cooling device to a device tree
893 * node, so that it can be bound to a thermal zone created out of device tree.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +0000894 *
895 * Return: a pointer to the created struct thermal_cooling_device or an
896 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +0800897 */
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400898static struct thermal_cooling_device *
899__thermal_cooling_device_register(struct device_node *np,
900 char *type, void *devdata,
901 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +0800902{
903 struct thermal_cooling_device *cdev;
Chen Yu4511f712015-10-30 16:32:10 +0800904 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +0800905 int result;
906
Guenter Roeck204dd1d2012-08-07 22:36:45 -0700907 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500908 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800909
910 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -0500911 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500912 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800913
914 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
915 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500916 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +0800917
918 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
919 if (result) {
920 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500921 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800922 }
923
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +0000924 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +0800925 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800926 INIT_LIST_HEAD(&cdev->thermal_instances);
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400927 cdev->np = np;
Zhang Rui203d3d42008-01-17 15:51:08 +0800928 cdev->ops = ops;
Ni Wade5ca0cce2014-02-17 11:02:55 +0800929 cdev->updated = false;
Zhang Rui203d3d42008-01-17 15:51:08 +0800930 cdev->device.class = &thermal_class;
Eduardo Valentin45cf2ec2016-11-07 21:09:02 -0800931 thermal_cooling_device_setup_sysfs(cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +0800932 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -0800933 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +0800934 result = device_register(&cdev->device);
935 if (result) {
936 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
937 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500938 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800939 }
940
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530941 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +0800942 mutex_lock(&thermal_list_lock);
943 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +0800944 mutex_unlock(&thermal_list_lock);
945
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530946 /* Update binding information for 'this' new cdev */
947 bind_cdev(cdev);
948
Chen Yu4511f712015-10-30 16:32:10 +0800949 mutex_lock(&thermal_list_lock);
950 list_for_each_entry(pos, &thermal_tz_list, node)
951 if (atomic_cmpxchg(&pos->need_update, 1, 0))
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700952 thermal_zone_device_update(pos,
953 THERMAL_EVENT_UNSPECIFIED);
Chen Yu4511f712015-10-30 16:32:10 +0800954 mutex_unlock(&thermal_list_lock);
955
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530956 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +0800957}
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400958
959/**
960 * thermal_cooling_device_register() - register a new thermal cooling device
961 * @type: the thermal cooling device type.
962 * @devdata: device private data.
963 * @ops: standard thermal cooling devices callbacks.
964 *
965 * This interface function adds a new thermal cooling device (fan/processor/...)
966 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
967 * to all the thermal zone devices registered at the same time.
968 *
969 * Return: a pointer to the created struct thermal_cooling_device or an
970 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
971 */
972struct thermal_cooling_device *
973thermal_cooling_device_register(char *type, void *devdata,
974 const struct thermal_cooling_device_ops *ops)
975{
976 return __thermal_cooling_device_register(NULL, type, devdata, ops);
977}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000978EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +0800979
980/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400981 * thermal_of_cooling_device_register() - register an OF thermal cooling device
982 * @np: a pointer to a device tree node.
983 * @type: the thermal cooling device type.
984 * @devdata: device private data.
985 * @ops: standard thermal cooling devices callbacks.
986 *
987 * This function will register a cooling device with device tree node reference.
988 * This interface function adds a new thermal cooling device (fan/processor/...)
989 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
990 * to all the thermal zone devices registered at the same time.
991 *
992 * Return: a pointer to the created struct thermal_cooling_device or an
993 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
994 */
995struct thermal_cooling_device *
996thermal_of_cooling_device_register(struct device_node *np,
997 char *type, void *devdata,
998 const struct thermal_cooling_device_ops *ops)
999{
1000 return __thermal_cooling_device_register(np, type, devdata, ops);
1001}
1002EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1003
Eduardo Valentinf11997f2016-11-07 21:09:08 -08001004static void __unbind(struct thermal_zone_device *tz, int mask,
1005 struct thermal_cooling_device *cdev)
1006{
1007 int i;
1008
1009 for (i = 0; i < tz->trips; i++)
1010 if (mask & (1 << i))
1011 thermal_zone_unbind_cooling_device(tz, i, cdev);
1012}
1013
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001014/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001015 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001016 * @cdev: the thermal cooling device to remove.
1017 *
1018 * thermal_cooling_device_unregister() must be called when the device is no
1019 * longer needed.
1020 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301021void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001022{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301023 int i;
1024 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001025 struct thermal_zone_device *tz;
1026 struct thermal_cooling_device *pos = NULL;
1027
1028 if (!cdev)
1029 return;
1030
1031 mutex_lock(&thermal_list_lock);
1032 list_for_each_entry(pos, &thermal_cdev_list, node)
1033 if (pos == cdev)
1034 break;
1035 if (pos != cdev) {
1036 /* thermal cooling device not found */
1037 mutex_unlock(&thermal_list_lock);
1038 return;
1039 }
1040 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301041
1042 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001043 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301044 if (tz->ops->unbind) {
1045 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001046 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301047 }
1048
1049 if (!tz->tzp || !tz->tzp->tbp)
1050 continue;
1051
1052 tzp = tz->tzp;
1053 for (i = 0; i < tzp->num_tbps; i++) {
1054 if (tzp->tbp[i].cdev == cdev) {
1055 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1056 tzp->tbp[i].cdev = NULL;
1057 }
1058 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001059 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301060
Zhang Rui203d3d42008-01-17 15:51:08 +08001061 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301062
Zhang Rui203d3d42008-01-17 15:51:08 +08001063 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1064 device_unregister(&cdev->device);
1065 return;
1066}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001067EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001068
Eduardo Valentin90f5b5b2016-11-07 21:09:10 -08001069static void bind_tz(struct thermal_zone_device *tz)
1070{
1071 int i, ret;
1072 struct thermal_cooling_device *pos = NULL;
1073 const struct thermal_zone_params *tzp = tz->tzp;
1074
1075 if (!tzp && !tz->ops->bind)
1076 return;
1077
1078 mutex_lock(&thermal_list_lock);
1079
1080 /* If there is ops->bind, try to use ops->bind */
1081 if (tz->ops->bind) {
1082 list_for_each_entry(pos, &thermal_cdev_list, node) {
1083 ret = tz->ops->bind(tz, pos);
1084 if (ret)
1085 print_bind_err_msg(tz, pos, ret);
1086 }
1087 goto exit;
1088 }
1089
1090 if (!tzp || !tzp->tbp)
1091 goto exit;
1092
1093 list_for_each_entry(pos, &thermal_cdev_list, node) {
1094 for (i = 0; i < tzp->num_tbps; i++) {
1095 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1096 continue;
1097 if (tzp->tbp[i].match(tz, pos))
1098 continue;
1099 tzp->tbp[i].cdev = pos;
1100 __bind(tz, tzp->tbp[i].trip_mask, pos,
1101 tzp->tbp[i].binding_limits,
1102 tzp->tbp[i].weight);
1103 }
1104 }
1105exit:
1106 mutex_unlock(&thermal_list_lock);
1107}
1108
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301109/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001110 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001111 * @type: the thermal zone device type
1112 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001113 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001114 * @devdata: private device data
1115 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301116 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001117 * @passive_delay: number of milliseconds to wait between polls when
1118 * performing passive cooling
1119 * @polling_delay: number of milliseconds to wait between polls when checking
1120 * whether trip points have been crossed (0 for interrupt
1121 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001122 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001123 * This interface function adds a new thermal zone device (sensor) to
1124 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1125 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08001126 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001127 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001128 *
1129 * Return: a pointer to the created struct thermal_zone_device or an
1130 * in case of error, an ERR_PTR. Caller must check return value with
1131 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001132 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001133struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001134 int trips, int mask, void *devdata,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001135 struct thermal_zone_device_ops *ops,
Javi Merino6b775e82015-03-02 17:17:19 +00001136 struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001137 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001138{
1139 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001140 enum thermal_trip_type trip_type;
Zhang Rui81ad4272016-03-18 10:03:24 +08001141 int trip_temp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001142 int result;
1143 int count;
Javi Merinoe33df1d2015-02-26 19:00:27 +00001144 struct thermal_governor *governor;
Zhang Rui203d3d42008-01-17 15:51:08 +08001145
Eduardo Valentin54fa38c2016-11-07 21:08:39 -08001146 if (!type || strlen(type) == 0)
1147 return ERR_PTR(-EINVAL);
1148
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001149 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001150 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001151
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001152 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001153 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001154
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04001155 if (!ops)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001156 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001157
Jonghwa Lee83720d02013-05-18 09:50:26 +00001158 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00001159 return ERR_PTR(-EINVAL);
1160
Zhang Rui203d3d42008-01-17 15:51:08 +08001161 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1162 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001163 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001164
Zhang Rui2d374132012-06-27 10:09:00 +08001165 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001166 idr_init(&tz->idr);
1167 mutex_init(&tz->lock);
1168 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1169 if (result) {
1170 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001171 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001172 }
1173
Eduardo Valentin54fa38c2016-11-07 21:08:39 -08001174 strlcpy(tz->type, type, sizeof(tz->type));
Zhang Rui203d3d42008-01-17 15:51:08 +08001175 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301176 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001177 tz->device.class = &thermal_class;
1178 tz->devdata = devdata;
1179 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001180 tz->passive_delay = passive_delay;
1181 tz->polling_delay = polling_delay;
Eduardo Valentin1c600862016-11-07 21:08:42 -08001182
Eduardo Valentin4d0fe742016-11-07 21:08:52 -08001183 /* sys I/F */
Eduardo Valentin1c600862016-11-07 21:08:42 -08001184 /* Add nodes that are always present via .groups */
Eduardo Valentin4d0fe742016-11-07 21:08:52 -08001185 result = thermal_zone_create_device_groups(tz, mask);
1186 if (result)
1187 goto unregister;
1188
Chen Yu4511f712015-10-30 16:32:10 +08001189 /* A new thermal zone needs to be updated anyway. */
1190 atomic_set(&tz->need_update, 1);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001191
Kay Sievers354655e2009-01-06 10:44:37 -08001192 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001193 result = device_register(&tz->device);
1194 if (result) {
1195 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1196 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001197 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001198 }
1199
Zhang Rui203d3d42008-01-17 15:51:08 +08001200 for (count = 0; count < trips; count++) {
Zhang Rui81ad4272016-03-18 10:03:24 +08001201 if (tz->ops->get_trip_type(tz, count, &trip_type))
1202 set_bit(count, &tz->trips_disabled);
Zhang Rui81ad4272016-03-18 10:03:24 +08001203 if (tz->ops->get_trip_temp(tz, count, &trip_temp))
1204 set_bit(count, &tz->trips_disabled);
1205 /* Check for bogus trip points */
1206 if (trip_temp == 0)
1207 set_bit(count, &tz->trips_disabled);
Zhang Rui203d3d42008-01-17 15:51:08 +08001208 }
1209
Durgadoss Ra4a15482012-09-18 11:04:57 +05301210 /* Update 'this' zone's governor information */
1211 mutex_lock(&thermal_governor_lock);
1212
1213 if (tz->tzp)
Javi Merinoe33df1d2015-02-26 19:00:27 +00001214 governor = __find_governor(tz->tzp->governor_name);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301215 else
Javi Merinoe33df1d2015-02-26 19:00:27 +00001216 governor = def_governor;
1217
1218 result = thermal_set_governor(tz, governor);
1219 if (result) {
1220 mutex_unlock(&thermal_governor_lock);
1221 goto unregister;
1222 }
Durgadoss Ra4a15482012-09-18 11:04:57 +05301223
1224 mutex_unlock(&thermal_governor_lock);
1225
Eduardo Valentinccba4ff2013-08-15 11:34:17 -04001226 if (!tz->tzp || !tz->tzp->no_hwmon) {
1227 result = thermal_add_hwmon_sysfs(tz);
1228 if (result)
1229 goto unregister;
1230 }
Zhang Ruie68b16a2008-04-21 16:07:52 +08001231
Zhang Rui203d3d42008-01-17 15:51:08 +08001232 mutex_lock(&thermal_list_lock);
1233 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001234 mutex_unlock(&thermal_list_lock);
1235
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301236 /* Bind cooling devices for this zone */
1237 bind_tz(tz);
1238
Matthew Garrettb1569e92008-12-03 17:55:32 +00001239 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1240
Zhang Ruibb431ba2015-10-30 16:31:47 +08001241 thermal_zone_device_reset(tz);
Chen Yu4511f712015-10-30 16:32:10 +08001242 /* Update the new thermal zone and mark it as already updated. */
1243 if (atomic_cmpxchg(&tz->need_update, 1, 0))
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07001244 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001245
Yao Dongdong14015862014-10-28 07:40:25 +00001246 return tz;
Zhang Rui203d3d42008-01-17 15:51:08 +08001247
Joe Perchescaca8b82012-03-21 12:55:02 -07001248unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001249 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1250 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001251 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001252}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001253EXPORT_SYMBOL_GPL(thermal_zone_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001254
1255/**
1256 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001257 * @tz: the thermal zone device to remove
1258 */
1259void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1260{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301261 int i;
1262 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001263 struct thermal_cooling_device *cdev;
1264 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001265
1266 if (!tz)
1267 return;
1268
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301269 tzp = tz->tzp;
1270
Zhang Rui203d3d42008-01-17 15:51:08 +08001271 mutex_lock(&thermal_list_lock);
1272 list_for_each_entry(pos, &thermal_tz_list, node)
1273 if (pos == tz)
1274 break;
1275 if (pos != tz) {
1276 /* thermal zone device not found */
1277 mutex_unlock(&thermal_list_lock);
1278 return;
1279 }
1280 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301281
1282 /* Unbind all cdevs associated with 'this' thermal zone */
1283 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1284 if (tz->ops->unbind) {
1285 tz->ops->unbind(tz, cdev);
1286 continue;
1287 }
1288
1289 if (!tzp || !tzp->tbp)
1290 break;
1291
1292 for (i = 0; i < tzp->num_tbps; i++) {
1293 if (tzp->tbp[i].cdev == cdev) {
1294 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1295 tzp->tbp[i].cdev = NULL;
1296 }
1297 }
1298 }
1299
Zhang Rui203d3d42008-01-17 15:51:08 +08001300 mutex_unlock(&thermal_list_lock);
1301
Matthew Garrettb1569e92008-12-03 17:55:32 +00001302 thermal_zone_device_set_polling(tz, 0);
1303
Eduardo Valentin4d0fe742016-11-07 21:08:52 -08001304 kfree(tz->trip_type_attrs);
1305 kfree(tz->trip_temp_attrs);
1306 kfree(tz->trip_hyst_attrs);
1307 kfree(tz->trips_attribute_group.attrs);
Javi Merinoe33df1d2015-02-26 19:00:27 +00001308 thermal_set_governor(tz, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001309
Zhang Ruie68b16a2008-04-21 16:07:52 +08001310 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001311 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1312 idr_destroy(&tz->idr);
1313 mutex_destroy(&tz->lock);
1314 device_unregister(&tz->device);
Eduardo Valentine161aef2016-11-07 21:08:51 -08001315 kfree(tz->device.groups);
Zhang Rui203d3d42008-01-17 15:51:08 +08001316}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001317EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001318
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001319/**
1320 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1321 * @name: thermal zone name to fetch the temperature
1322 *
1323 * When only one zone is found with the passed name, returns a reference to it.
1324 *
1325 * Return: On success returns a reference to an unique thermal zone with
1326 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1327 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1328 */
1329struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1330{
1331 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1332 unsigned int found = 0;
1333
1334 if (!name)
1335 goto exit;
1336
1337 mutex_lock(&thermal_list_lock);
1338 list_for_each_entry(pos, &thermal_tz_list, node)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -07001339 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001340 found++;
1341 ref = pos;
1342 }
1343 mutex_unlock(&thermal_list_lock);
1344
1345 /* nothing has been found, thus an error code for it */
1346 if (found == 0)
1347 ref = ERR_PTR(-ENODEV);
1348 else if (found > 1)
1349 /* Success only when an unique zone is found */
1350 ref = ERR_PTR(-EEXIST);
1351
1352exit:
1353 return ref;
1354}
1355EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1356
Rajendra Nayak4a7069a2016-05-05 14:21:42 +05301357/**
1358 * thermal_zone_get_slope - return the slope attribute of the thermal zone
1359 * @tz: thermal zone device with the slope attribute
1360 *
1361 * Return: If the thermal zone device has a slope attribute, return it, else
1362 * return 1.
1363 */
1364int thermal_zone_get_slope(struct thermal_zone_device *tz)
1365{
1366 if (tz && tz->tzp)
1367 return tz->tzp->slope;
1368 return 1;
1369}
1370EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
1371
1372/**
1373 * thermal_zone_get_offset - return the offset attribute of the thermal zone
1374 * @tz: thermal zone device with the offset attribute
1375 *
1376 * Return: If the thermal zone device has a offset attribute, return it, else
1377 * return 0.
1378 */
1379int thermal_zone_get_offset(struct thermal_zone_device *tz)
1380{
1381 if (tz && tz->tzp)
1382 return tz->tzp->offset;
1383 return 0;
1384}
1385EXPORT_SYMBOL_GPL(thermal_zone_get_offset);
1386
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001387#ifdef CONFIG_NET
Johannes Berg2a94fe42013-11-19 15:19:39 +01001388static const struct genl_multicast_group thermal_event_mcgrps[] = {
1389 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
1390};
1391
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001392static struct genl_family thermal_event_genl_family = {
1393 .id = GENL_ID_GENERATE,
1394 .name = THERMAL_GENL_FAMILY_NAME,
1395 .version = THERMAL_GENL_VERSION,
1396 .maxattr = THERMAL_GENL_ATTR_MAX,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001397 .mcgrps = thermal_event_mcgrps,
1398 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001399};
1400
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001401int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1402 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301403{
1404 struct sk_buff *skb;
1405 struct nlattr *attr;
1406 struct thermal_genl_event *thermal_event;
1407 void *msg_header;
1408 int size;
1409 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001410 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301411
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001412 if (!tz)
1413 return -EINVAL;
1414
R.Durgadoss4cb18722010-10-27 03:33:29 +05301415 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001416 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1417 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301418
1419 skb = genlmsg_new(size, GFP_ATOMIC);
1420 if (!skb)
1421 return -ENOMEM;
1422
1423 /* add the genetlink message header */
1424 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1425 &thermal_event_genl_family, 0,
1426 THERMAL_GENL_CMD_EVENT);
1427 if (!msg_header) {
1428 nlmsg_free(skb);
1429 return -ENOMEM;
1430 }
1431
1432 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001433 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1434 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301435
1436 if (!attr) {
1437 nlmsg_free(skb);
1438 return -EINVAL;
1439 }
1440
1441 thermal_event = nla_data(attr);
1442 if (!thermal_event) {
1443 nlmsg_free(skb);
1444 return -EINVAL;
1445 }
1446
1447 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1448
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001449 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301450 thermal_event->event = event;
1451
1452 /* send multicast genetlink message */
Johannes Berg053c0952015-01-16 22:09:00 +01001453 genlmsg_end(skb, msg_header);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301454
Johannes Berg68eb5502013-11-19 15:19:38 +01001455 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001456 0, GFP_ATOMIC);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301457 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00001458 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301459
1460 return result;
1461}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001462EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301463
1464static int genetlink_init(void)
1465{
Johannes Berg2a94fe42013-11-19 15:19:39 +01001466 return genl_register_family(&thermal_event_genl_family);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301467}
1468
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001469static void genetlink_exit(void)
1470{
1471 genl_unregister_family(&thermal_event_genl_family);
1472}
1473#else /* !CONFIG_NET */
1474static inline int genetlink_init(void) { return 0; }
1475static inline void genetlink_exit(void) {}
1476#endif /* !CONFIG_NET */
1477
Zhang Ruiff140fe2015-10-30 16:31:58 +08001478static int thermal_pm_notify(struct notifier_block *nb,
1479 unsigned long mode, void *_unused)
1480{
1481 struct thermal_zone_device *tz;
1482
1483 switch (mode) {
1484 case PM_HIBERNATION_PREPARE:
1485 case PM_RESTORE_PREPARE:
1486 case PM_SUSPEND_PREPARE:
1487 atomic_set(&in_suspend, 1);
1488 break;
1489 case PM_POST_HIBERNATION:
1490 case PM_POST_RESTORE:
1491 case PM_POST_SUSPEND:
1492 atomic_set(&in_suspend, 0);
1493 list_for_each_entry(tz, &thermal_tz_list, node) {
1494 thermal_zone_device_reset(tz);
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07001495 thermal_zone_device_update(tz,
1496 THERMAL_EVENT_UNSPECIFIED);
Zhang Ruiff140fe2015-10-30 16:31:58 +08001497 }
1498 break;
1499 default:
1500 break;
1501 }
1502 return 0;
1503}
1504
1505static struct notifier_block thermal_pm_nb = {
1506 .notifier_call = thermal_pm_notify,
1507};
1508
Zhang Rui203d3d42008-01-17 15:51:08 +08001509static int __init thermal_init(void)
1510{
Zhang Rui80a26a52013-03-26 16:38:29 +08001511 int result;
1512
1513 result = thermal_register_governors();
1514 if (result)
1515 goto error;
Zhang Rui203d3d42008-01-17 15:51:08 +08001516
1517 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001518 if (result)
1519 goto unregister_governors;
1520
R.Durgadoss4cb18722010-10-27 03:33:29 +05301521 result = genetlink_init();
Zhang Rui80a26a52013-03-26 16:38:29 +08001522 if (result)
1523 goto unregister_class;
1524
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001525 result = of_parse_thermal_zones();
1526 if (result)
1527 goto exit_netlink;
1528
Zhang Ruiff140fe2015-10-30 16:31:58 +08001529 result = register_pm_notifier(&thermal_pm_nb);
1530 if (result)
1531 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1532 result);
1533
Zhang Rui80a26a52013-03-26 16:38:29 +08001534 return 0;
1535
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001536exit_netlink:
1537 genetlink_exit();
Zhang Rui80a26a52013-03-26 16:38:29 +08001538unregister_class:
1539 class_unregister(&thermal_class);
Luis Henriques9d367e52014-12-03 21:20:21 +00001540unregister_governors:
1541 thermal_unregister_governors();
Zhang Rui80a26a52013-03-26 16:38:29 +08001542error:
1543 idr_destroy(&thermal_tz_idr);
1544 idr_destroy(&thermal_cdev_idr);
1545 mutex_destroy(&thermal_idr_lock);
1546 mutex_destroy(&thermal_list_lock);
1547 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001548 return result;
1549}
1550
1551static void __exit thermal_exit(void)
1552{
Zhang Ruiff140fe2015-10-30 16:31:58 +08001553 unregister_pm_notifier(&thermal_pm_nb);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001554 of_thermal_destroy_zones();
Zhang Rui80a26a52013-03-26 16:38:29 +08001555 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001556 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001557 thermal_unregister_governors();
Zhang Rui203d3d42008-01-17 15:51:08 +08001558 idr_destroy(&thermal_tz_idr);
1559 idr_destroy(&thermal_cdev_idr);
1560 mutex_destroy(&thermal_idr_lock);
1561 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08001562 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001563}
1564
R.Durgadoss4cb18722010-10-27 03:33:29 +05301565fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001566module_exit(thermal_exit);