blob: 884235f216bd30c7f5adff77fdf07ce243597ce6 [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
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530289static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
290 int delay)
291{
292 if (delay > 1000)
293 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
294 round_jiffies(msecs_to_jiffies(delay)));
295 else if (delay)
296 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
297 msecs_to_jiffies(delay));
298 else
299 cancel_delayed_work(&tz->poll_queue);
300}
301
302static void monitor_thermal_zone(struct thermal_zone_device *tz)
303{
304 mutex_lock(&tz->lock);
305
306 if (tz->passive)
307 thermal_zone_device_set_polling(tz, tz->passive_delay);
308 else if (tz->polling_delay)
309 thermal_zone_device_set_polling(tz, tz->polling_delay);
310 else
311 thermal_zone_device_set_polling(tz, 0);
312
313 mutex_unlock(&tz->lock);
314}
315
316static void handle_non_critical_trips(struct thermal_zone_device *tz,
317 int trip, enum thermal_trip_type trip_type)
318{
Zhang Ruif2234bc2014-01-24 10:23:19 +0800319 tz->governor ? tz->governor->throttle(tz, trip) :
320 def_governor->throttle(tz, trip);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530321}
322
323static void handle_critical_trips(struct thermal_zone_device *tz,
324 int trip, enum thermal_trip_type trip_type)
325{
Sascha Hauer17e83512015-07-24 08:12:54 +0200326 int trip_temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530327
328 tz->ops->get_trip_temp(tz, trip, &trip_temp);
329
330 /* If we have not crossed the trip_temp, we do not care. */
Srinivas Pandruvada84ffe3e2014-11-12 15:43:29 -0800331 if (trip_temp <= 0 || tz->temperature < trip_temp)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530332 return;
333
Punit Agrawal208cd822014-07-29 11:50:50 +0100334 trace_thermal_zone_trip(tz, trip, trip_type);
335
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530336 if (tz->ops->notify)
337 tz->ops->notify(tz, trip, trip_type);
338
339 if (trip_type == THERMAL_TRIP_CRITICAL) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000340 dev_emerg(&tz->device,
341 "critical temperature reached(%d C),shutting down\n",
342 tz->temperature / 1000);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530343 orderly_poweroff(true);
344 }
345}
346
347static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
348{
349 enum thermal_trip_type type;
350
Zhang Rui81ad4272016-03-18 10:03:24 +0800351 /* Ignore disabled trip points */
352 if (test_bit(trip, &tz->trips_disabled))
353 return;
354
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530355 tz->ops->get_trip_type(tz, trip, &type);
356
357 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
358 handle_critical_trips(tz, trip, type);
359 else
360 handle_non_critical_trips(tz, trip, type);
361 /*
362 * Alright, we handled this trip successfully.
363 * So, start monitoring again.
364 */
365 monitor_thermal_zone(tz);
366}
367
368static void update_temperature(struct thermal_zone_device *tz)
369{
Sascha Hauer17e83512015-07-24 08:12:54 +0200370 int temp, ret;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530371
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000372 ret = thermal_zone_get_temp(tz, &temp);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530373 if (ret) {
Hans de Goede7e497a72015-03-21 15:02:55 +0100374 if (ret != -EAGAIN)
375 dev_warn(&tz->device,
376 "failed to read out thermal zone (%d)\n",
377 ret);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000378 return;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530379 }
380
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000381 mutex_lock(&tz->lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530382 tz->last_temperature = tz->temperature;
383 tz->temperature = temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530384 mutex_unlock(&tz->lock);
Aaron Lu06475b52013-12-02 13:54:26 +0800385
Punit Agrawal100a8fd2014-07-29 11:50:48 +0100386 trace_thermal_temperature(tz);
Zhang Ruibb431ba2015-10-30 16:31:47 +0800387 if (tz->last_temperature == THERMAL_TEMP_INVALID)
388 dev_dbg(&tz->device, "last_temperature N/A, current_temperature=%d\n",
389 tz->temperature);
390 else
391 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
392 tz->last_temperature, tz->temperature);
393}
394
395static void thermal_zone_device_reset(struct thermal_zone_device *tz)
396{
397 struct thermal_instance *pos;
398
399 tz->temperature = THERMAL_TEMP_INVALID;
400 tz->passive = 0;
401 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
402 pos->initialized = false;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530403}
404
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700405void thermal_zone_device_update(struct thermal_zone_device *tz,
406 enum thermal_notify_event event)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530407{
408 int count;
409
Zhang Ruiff140fe2015-10-30 16:31:58 +0800410 if (atomic_read(&in_suspend))
411 return;
412
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400413 if (!tz->ops->get_temp)
414 return;
415
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530416 update_temperature(tz);
417
Sascha Hauer060c0342016-06-22 16:42:01 +0800418 thermal_zone_set_trips(tz);
419
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700420 tz->notify_event = event;
421
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530422 for (count = 0; count < tz->trips; count++)
423 handle_thermal_trip(tz, count);
424}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000425EXPORT_SYMBOL_GPL(thermal_zone_device_update);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530426
Eduardo Valentin106339a2016-11-07 21:09:14 -0800427/**
428 * thermal_notify_framework - Sensor drivers use this API to notify framework
429 * @tz: thermal zone device
430 * @trip: indicates which trip point has been crossed
431 *
432 * This function handles the trip events from sensor drivers. It starts
433 * throttling the cooling devices according to the policy configured.
434 * For CRITICAL and HOT trip points, this notifies the respective drivers,
435 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
436 * The throttling policy is based on the configured platform data; if no
437 * platform data is provided, this uses the step_wise throttling policy.
438 */
439void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
440{
441 handle_thermal_trip(tz, trip);
442}
443EXPORT_SYMBOL_GPL(thermal_notify_framework);
444
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530445static void thermal_zone_device_check(struct work_struct *work)
446{
447 struct thermal_zone_device *tz = container_of(work, struct
448 thermal_zone_device,
449 poll_queue.work);
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700450 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530451}
452
Eduardo Valentin1a7e7cc2016-11-07 21:08:47 -0800453/**
454 * power_actor_get_max_power() - get the maximum power that a cdev can consume
455 * @cdev: pointer to &thermal_cooling_device
456 * @tz: a valid thermal zone device pointer
457 * @max_power: pointer in which to store the maximum power
458 *
459 * Calculate the maximum power consumption in milliwats that the
460 * cooling device can currently consume and store it in @max_power.
461 *
462 * Return: 0 on success, -EINVAL if @cdev doesn't support the
463 * power_actor API or -E* on other error.
464 */
465int power_actor_get_max_power(struct thermal_cooling_device *cdev,
466 struct thermal_zone_device *tz, u32 *max_power)
467{
468 if (!cdev_is_power_actor(cdev))
469 return -EINVAL;
470
471 return cdev->ops->state2power(cdev, tz, 0, max_power);
472}
473
474/**
475 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
476 * @cdev: pointer to &thermal_cooling_device
477 * @tz: a valid thermal zone device pointer
478 * @min_power: pointer in which to store the minimum power
479 *
480 * Calculate the minimum power consumption in milliwatts that the
481 * cooling device can currently consume and store it in @min_power.
482 *
483 * Return: 0 on success, -EINVAL if @cdev doesn't support the
484 * power_actor API or -E* on other error.
485 */
486int power_actor_get_min_power(struct thermal_cooling_device *cdev,
487 struct thermal_zone_device *tz, u32 *min_power)
488{
489 unsigned long max_state;
490 int ret;
491
492 if (!cdev_is_power_actor(cdev))
493 return -EINVAL;
494
495 ret = cdev->ops->get_max_state(cdev, &max_state);
496 if (ret)
497 return ret;
498
499 return cdev->ops->state2power(cdev, tz, max_state, min_power);
500}
501
502/**
503 * power_actor_set_power() - limit the maximum power a cooling device consumes
504 * @cdev: pointer to &thermal_cooling_device
505 * @instance: thermal instance to update
506 * @power: the power in milliwatts
507 *
508 * Set the cooling device to consume at most @power milliwatts. The limit is
509 * expected to be a cap at the maximum power consumption.
510 *
511 * Return: 0 on success, -EINVAL if the cooling device does not
512 * implement the power actor API or -E* for other failures.
513 */
514int power_actor_set_power(struct thermal_cooling_device *cdev,
515 struct thermal_instance *instance, u32 power)
516{
517 unsigned long state;
518 int ret;
519
520 if (!cdev_is_power_actor(cdev))
521 return -EINVAL;
522
523 ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
524 if (ret)
525 return ret;
526
527 instance->target = state;
528 mutex_lock(&cdev->lock);
529 cdev->updated = false;
530 mutex_unlock(&cdev->lock);
531 thermal_cdev_update(cdev);
532
533 return 0;
534}
535
Eduardo Valentin3d0055d2016-11-07 21:08:54 -0800536void thermal_zone_device_rebind_exception(struct thermal_zone_device *tz,
537 const char *cdev_type, size_t size)
538{
539 struct thermal_cooling_device *cdev = NULL;
540
541 mutex_lock(&thermal_list_lock);
542 list_for_each_entry(cdev, &thermal_cdev_list, node) {
543 /* skip non matching cdevs */
544 if (strncmp(cdev_type, cdev->type, size))
545 continue;
546
547 /* re binding the exception matching the type pattern */
548 thermal_zone_bind_cooling_device(tz, THERMAL_TRIPS_NONE, cdev,
549 THERMAL_NO_LIMIT,
550 THERMAL_NO_LIMIT,
551 THERMAL_WEIGHT_DEFAULT);
552 }
553 mutex_unlock(&thermal_list_lock);
554}
555
556void thermal_zone_device_unbind_exception(struct thermal_zone_device *tz,
557 const char *cdev_type, size_t size)
558{
559 struct thermal_cooling_device *cdev = NULL;
560
561 mutex_lock(&thermal_list_lock);
562 list_for_each_entry(cdev, &thermal_cdev_list, node) {
563 /* skip non matching cdevs */
564 if (strncmp(cdev_type, cdev->type, size))
565 continue;
566 /* unbinding the exception matching the type pattern */
567 thermal_zone_unbind_cooling_device(tz, THERMAL_TRIPS_NONE,
568 cdev);
569 }
570 mutex_unlock(&thermal_list_lock);
571}
572
Zhang Rui203d3d42008-01-17 15:51:08 +0800573/* Device management */
574
Eduardo Valentinc30176f2016-11-07 21:09:06 -0800575static int get_idr(struct idr *idr, struct mutex *lock, int *id)
576{
577 int ret;
578
579 if (lock)
580 mutex_lock(lock);
581 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
582 if (lock)
583 mutex_unlock(lock);
584 if (unlikely(ret < 0))
585 return ret;
586 *id = ret;
587 return 0;
588}
589
590static void release_idr(struct idr *idr, struct mutex *lock, int id)
591{
592 if (lock)
593 mutex_lock(lock);
594 idr_remove(idr, id);
595 if (lock)
596 mutex_unlock(lock);
597}
598
Zhang Rui203d3d42008-01-17 15:51:08 +0800599/**
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000600 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
601 * @tz: pointer to struct thermal_zone_device
Zhang Rui203d3d42008-01-17 15:51:08 +0800602 * @trip: indicates which trip point the cooling devices is
603 * associated with in this thermal zone.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000604 * @cdev: pointer to struct thermal_cooling_device
605 * @upper: the Maximum cooling state for this trip point.
606 * THERMAL_NO_LIMIT means no upper limit,
607 * and the cooling device can be in max_state.
608 * @lower: the Minimum cooling state can be used for this trip point.
609 * THERMAL_NO_LIMIT means no lower limit,
610 * and the cooling device can be in cooling state 0.
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000611 * @weight: The weight of the cooling device to be bound to the
612 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
613 * default value
Len Brown543a9562008-02-07 16:55:08 -0500614 *
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000615 * This interface function bind a thermal cooling device to the certain trip
616 * point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -0500617 * This function is usually called in the thermal zone device .bind callback.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +0000618 *
619 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +0800620 */
621int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
622 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +0800623 struct thermal_cooling_device *cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000624 unsigned long upper, unsigned long lower,
625 unsigned int weight)
Zhang Rui203d3d42008-01-17 15:51:08 +0800626{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800627 struct thermal_instance *dev;
628 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -0500629 struct thermal_zone_device *pos1;
630 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +0800631 unsigned long max_state;
Lukasz Majewski9a3031d2014-11-18 11:16:30 +0100632 int result, ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800633
Len Brown543a9562008-02-07 16:55:08 -0500634 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +0800635 return -EINVAL;
636
Thomas Sujithc7516702008-02-15 00:58:50 -0500637 list_for_each_entry(pos1, &thermal_tz_list, node) {
638 if (pos1 == tz)
639 break;
640 }
641 list_for_each_entry(pos2, &thermal_cdev_list, node) {
642 if (pos2 == cdev)
643 break;
644 }
645
646 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +0800647 return -EINVAL;
648
Lukasz Majewski9a3031d2014-11-18 11:16:30 +0100649 ret = cdev->ops->get_max_state(cdev, &max_state);
650 if (ret)
651 return ret;
Zhang Rui9d998422012-06-26 16:35:57 +0800652
653 /* lower default 0, upper default max_state */
654 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
655 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
656
657 if (lower > upper || upper > max_state)
658 return -EINVAL;
659
Zhang Rui203d3d42008-01-17 15:51:08 +0800660 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800661 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800662 if (!dev)
663 return -ENOMEM;
664 dev->tz = tz;
665 dev->cdev = cdev;
666 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +0800667 dev->upper = upper;
668 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +0800669 dev->target = THERMAL_NO_TARGET;
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000670 dev->weight = weight;
Zhang Rui74051ba2012-06-26 16:27:22 +0800671
Zhang Rui203d3d42008-01-17 15:51:08 +0800672 result = get_idr(&tz->idr, &tz->lock, &dev->id);
673 if (result)
674 goto free_mem;
675
676 sprintf(dev->name, "cdev%d", dev->id);
677 result =
678 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
679 if (result)
680 goto release_idr;
681
682 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -0700683 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800684 dev->attr.attr.name = dev->attr_name;
685 dev->attr.attr.mode = 0444;
686 dev->attr.show = thermal_cooling_device_trip_point_show;
687 result = device_create_file(&tz->device, &dev->attr);
688 if (result)
689 goto remove_symbol_link;
690
Javi Merinodb916512015-02-18 16:04:24 +0000691 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
692 sysfs_attr_init(&dev->weight_attr.attr);
693 dev->weight_attr.attr.name = dev->weight_attr_name;
694 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
695 dev->weight_attr.show = thermal_cooling_device_weight_show;
696 dev->weight_attr.store = thermal_cooling_device_weight_store;
697 result = device_create_file(&tz->device, &dev->weight_attr);
698 if (result)
699 goto remove_trip_file;
700
Zhang Rui203d3d42008-01-17 15:51:08 +0800701 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800702 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800703 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +0800704 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
705 result = -EEXIST;
706 break;
707 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800708 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800709 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800710 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
Chen Yu4511f712015-10-30 16:32:10 +0800711 atomic_set(&tz->need_update, 1);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800712 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800713 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800714 mutex_unlock(&tz->lock);
715
716 if (!result)
717 return 0;
718
Javi Merinodb916512015-02-18 16:04:24 +0000719 device_remove_file(&tz->device, &dev->weight_attr);
720remove_trip_file:
Zhang Rui203d3d42008-01-17 15:51:08 +0800721 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -0700722remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +0800723 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -0700724release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +0800725 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -0700726free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +0800727 kfree(dev);
728 return result;
729}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000730EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +0800731
732/**
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000733 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
734 * thermal zone.
735 * @tz: pointer to a struct thermal_zone_device.
Zhang Rui203d3d42008-01-17 15:51:08 +0800736 * @trip: indicates which trip point the cooling devices is
737 * associated with in this thermal zone.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000738 * @cdev: pointer to a struct thermal_cooling_device.
Len Brown543a9562008-02-07 16:55:08 -0500739 *
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000740 * This interface function unbind a thermal cooling device from the certain
741 * trip point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -0500742 * This function is usually called in the thermal zone device .unbind callback.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +0000743 *
744 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +0800745 */
746int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
747 int trip,
748 struct thermal_cooling_device *cdev)
749{
Zhang Ruib81b6ba2012-06-27 10:08:19 +0800750 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +0800751
752 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800753 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +0800754 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -0500755 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +0800756 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800757 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +0800758 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800759 mutex_unlock(&tz->lock);
760 goto unbind;
761 }
762 }
Zhang Ruif4a821c2012-07-24 16:56:21 +0800763 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +0800764 mutex_unlock(&tz->lock);
765
766 return -ENODEV;
767
Joe Perchescaca8b82012-03-21 12:55:02 -0700768unbind:
Viresh Kumar528464e2015-07-23 14:32:32 +0530769 device_remove_file(&tz->device, &pos->weight_attr);
Zhang Rui203d3d42008-01-17 15:51:08 +0800770 device_remove_file(&tz->device, &pos->attr);
771 sysfs_remove_link(&tz->device.kobj, pos->name);
772 release_idr(&tz->idr, &tz->lock, pos->id);
773 kfree(pos);
774 return 0;
775}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000776EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +0800777
778static void thermal_release(struct device *dev)
779{
780 struct thermal_zone_device *tz;
781 struct thermal_cooling_device *cdev;
782
Joe Perchescaca8b82012-03-21 12:55:02 -0700783 if (!strncmp(dev_name(dev), "thermal_zone",
784 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +0800785 tz = to_thermal_zone(dev);
786 kfree(tz);
durgadoss.r@intel.com732e4c82013-10-02 00:08:00 +0530787 } else if(!strncmp(dev_name(dev), "cooling_device",
788 sizeof("cooling_device") - 1)){
Zhang Rui203d3d42008-01-17 15:51:08 +0800789 cdev = to_cooling_device(dev);
790 kfree(cdev);
791 }
792}
793
794static struct class thermal_class = {
795 .name = "thermal",
796 .dev_release = thermal_release,
797};
798
Eduardo Valentin4b0d3c22016-11-07 21:09:13 -0800799static inline
800void print_bind_err_msg(struct thermal_zone_device *tz,
801 struct thermal_cooling_device *cdev, int ret)
Eduardo Valentinf502ab82016-11-07 21:09:12 -0800802{
803 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
804 tz->type, cdev->type, ret);
805}
806
807static void __bind(struct thermal_zone_device *tz, int mask,
808 struct thermal_cooling_device *cdev,
809 unsigned long *limits,
810 unsigned int weight)
811{
812 int i, ret;
813
814 for (i = 0; i < tz->trips; i++) {
815 if (mask & (1 << i)) {
816 unsigned long upper, lower;
817
818 upper = THERMAL_NO_LIMIT;
819 lower = THERMAL_NO_LIMIT;
820 if (limits) {
821 lower = limits[i * 2];
822 upper = limits[i * 2 + 1];
823 }
824 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
825 upper, lower,
826 weight);
827 if (ret)
828 print_bind_err_msg(tz, cdev, ret);
829 }
830 }
831}
832
Eduardo Valentin949aad82016-11-07 21:09:09 -0800833static void bind_cdev(struct thermal_cooling_device *cdev)
834{
835 int i, ret;
836 const struct thermal_zone_params *tzp;
837 struct thermal_zone_device *pos = NULL;
838
839 mutex_lock(&thermal_list_lock);
840
841 list_for_each_entry(pos, &thermal_tz_list, node) {
842 if (!pos->tzp && !pos->ops->bind)
843 continue;
844
845 if (pos->ops->bind) {
846 ret = pos->ops->bind(pos, cdev);
847 if (ret)
848 print_bind_err_msg(pos, cdev, ret);
849 continue;
850 }
851
852 tzp = pos->tzp;
853 if (!tzp || !tzp->tbp)
854 continue;
855
856 for (i = 0; i < tzp->num_tbps; i++) {
857 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
858 continue;
859 if (tzp->tbp[i].match(pos, cdev))
860 continue;
861 tzp->tbp[i].cdev = cdev;
862 __bind(pos, tzp->tbp[i].trip_mask, cdev,
863 tzp->tbp[i].binding_limits,
864 tzp->tbp[i].weight);
865 }
866 }
867
868 mutex_unlock(&thermal_list_lock);
869}
870
Zhang Rui203d3d42008-01-17 15:51:08 +0800871/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400872 * __thermal_cooling_device_register() - register a new thermal cooling device
873 * @np: a pointer to a device tree node.
Zhang Rui203d3d42008-01-17 15:51:08 +0800874 * @type: the thermal cooling device type.
875 * @devdata: device private data.
876 * @ops: standard thermal cooling devices callbacks.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +0000877 *
878 * This interface function adds a new thermal cooling device (fan/processor/...)
879 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
880 * to all the thermal zone devices registered at the same time.
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400881 * It also gives the opportunity to link the cooling device to a device tree
882 * node, so that it can be bound to a thermal zone created out of device tree.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +0000883 *
884 * Return: a pointer to the created struct thermal_cooling_device or an
885 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +0800886 */
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400887static struct thermal_cooling_device *
888__thermal_cooling_device_register(struct device_node *np,
889 char *type, void *devdata,
890 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +0800891{
892 struct thermal_cooling_device *cdev;
Chen Yu4511f712015-10-30 16:32:10 +0800893 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +0800894 int result;
895
Guenter Roeck204dd1d2012-08-07 22:36:45 -0700896 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500897 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800898
899 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -0500900 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500901 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800902
903 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
904 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500905 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +0800906
907 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
908 if (result) {
909 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500910 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800911 }
912
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +0000913 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +0800914 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800915 INIT_LIST_HEAD(&cdev->thermal_instances);
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400916 cdev->np = np;
Zhang Rui203d3d42008-01-17 15:51:08 +0800917 cdev->ops = ops;
Ni Wade5ca0cce2014-02-17 11:02:55 +0800918 cdev->updated = false;
Zhang Rui203d3d42008-01-17 15:51:08 +0800919 cdev->device.class = &thermal_class;
Eduardo Valentin45cf2ec2016-11-07 21:09:02 -0800920 thermal_cooling_device_setup_sysfs(cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +0800921 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -0800922 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +0800923 result = device_register(&cdev->device);
924 if (result) {
925 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
926 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -0500927 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +0800928 }
929
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530930 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +0800931 mutex_lock(&thermal_list_lock);
932 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +0800933 mutex_unlock(&thermal_list_lock);
934
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530935 /* Update binding information for 'this' new cdev */
936 bind_cdev(cdev);
937
Chen Yu4511f712015-10-30 16:32:10 +0800938 mutex_lock(&thermal_list_lock);
939 list_for_each_entry(pos, &thermal_tz_list, node)
940 if (atomic_cmpxchg(&pos->need_update, 1, 0))
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700941 thermal_zone_device_update(pos,
942 THERMAL_EVENT_UNSPECIFIED);
Chen Yu4511f712015-10-30 16:32:10 +0800943 mutex_unlock(&thermal_list_lock);
944
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530945 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +0800946}
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400947
948/**
949 * thermal_cooling_device_register() - register a new thermal cooling device
950 * @type: the thermal cooling device type.
951 * @devdata: device private data.
952 * @ops: standard thermal cooling devices callbacks.
953 *
954 * This interface function adds a new thermal cooling device (fan/processor/...)
955 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
956 * to all the thermal zone devices registered at the same time.
957 *
958 * Return: a pointer to the created struct thermal_cooling_device or an
959 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
960 */
961struct thermal_cooling_device *
962thermal_cooling_device_register(char *type, void *devdata,
963 const struct thermal_cooling_device_ops *ops)
964{
965 return __thermal_cooling_device_register(NULL, type, devdata, ops);
966}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000967EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +0800968
969/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400970 * thermal_of_cooling_device_register() - register an OF thermal cooling device
971 * @np: a pointer to a device tree node.
972 * @type: the thermal cooling device type.
973 * @devdata: device private data.
974 * @ops: standard thermal cooling devices callbacks.
975 *
976 * This function will register a cooling device with device tree node reference.
977 * This interface function adds a new thermal cooling device (fan/processor/...)
978 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
979 * to all the thermal zone devices registered at the same time.
980 *
981 * Return: a pointer to the created struct thermal_cooling_device or an
982 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
983 */
984struct thermal_cooling_device *
985thermal_of_cooling_device_register(struct device_node *np,
986 char *type, void *devdata,
987 const struct thermal_cooling_device_ops *ops)
988{
989 return __thermal_cooling_device_register(np, type, devdata, ops);
990}
991EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
992
Eduardo Valentinf11997f2016-11-07 21:09:08 -0800993static void __unbind(struct thermal_zone_device *tz, int mask,
994 struct thermal_cooling_device *cdev)
995{
996 int i;
997
998 for (i = 0; i < tz->trips; i++)
999 if (mask & (1 << i))
1000 thermal_zone_unbind_cooling_device(tz, i, cdev);
1001}
1002
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001003/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001004 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001005 * @cdev: the thermal cooling device to remove.
1006 *
1007 * thermal_cooling_device_unregister() must be called when the device is no
1008 * longer needed.
1009 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301010void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001011{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301012 int i;
1013 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001014 struct thermal_zone_device *tz;
1015 struct thermal_cooling_device *pos = NULL;
1016
1017 if (!cdev)
1018 return;
1019
1020 mutex_lock(&thermal_list_lock);
1021 list_for_each_entry(pos, &thermal_cdev_list, node)
1022 if (pos == cdev)
1023 break;
1024 if (pos != cdev) {
1025 /* thermal cooling device not found */
1026 mutex_unlock(&thermal_list_lock);
1027 return;
1028 }
1029 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301030
1031 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001032 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301033 if (tz->ops->unbind) {
1034 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001035 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301036 }
1037
1038 if (!tz->tzp || !tz->tzp->tbp)
1039 continue;
1040
1041 tzp = tz->tzp;
1042 for (i = 0; i < tzp->num_tbps; i++) {
1043 if (tzp->tbp[i].cdev == cdev) {
1044 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1045 tzp->tbp[i].cdev = NULL;
1046 }
1047 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001048 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301049
Zhang Rui203d3d42008-01-17 15:51:08 +08001050 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301051
Zhang Rui203d3d42008-01-17 15:51:08 +08001052 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1053 device_unregister(&cdev->device);
1054 return;
1055}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001056EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001057
Eduardo Valentin90f5b5b2016-11-07 21:09:10 -08001058static void bind_tz(struct thermal_zone_device *tz)
1059{
1060 int i, ret;
1061 struct thermal_cooling_device *pos = NULL;
1062 const struct thermal_zone_params *tzp = tz->tzp;
1063
1064 if (!tzp && !tz->ops->bind)
1065 return;
1066
1067 mutex_lock(&thermal_list_lock);
1068
1069 /* If there is ops->bind, try to use ops->bind */
1070 if (tz->ops->bind) {
1071 list_for_each_entry(pos, &thermal_cdev_list, node) {
1072 ret = tz->ops->bind(tz, pos);
1073 if (ret)
1074 print_bind_err_msg(tz, pos, ret);
1075 }
1076 goto exit;
1077 }
1078
1079 if (!tzp || !tzp->tbp)
1080 goto exit;
1081
1082 list_for_each_entry(pos, &thermal_cdev_list, node) {
1083 for (i = 0; i < tzp->num_tbps; i++) {
1084 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
1085 continue;
1086 if (tzp->tbp[i].match(tz, pos))
1087 continue;
1088 tzp->tbp[i].cdev = pos;
1089 __bind(tz, tzp->tbp[i].trip_mask, pos,
1090 tzp->tbp[i].binding_limits,
1091 tzp->tbp[i].weight);
1092 }
1093 }
1094exit:
1095 mutex_unlock(&thermal_list_lock);
1096}
1097
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301098/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001099 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001100 * @type: the thermal zone device type
1101 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001102 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001103 * @devdata: private device data
1104 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301105 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001106 * @passive_delay: number of milliseconds to wait between polls when
1107 * performing passive cooling
1108 * @polling_delay: number of milliseconds to wait between polls when checking
1109 * whether trip points have been crossed (0 for interrupt
1110 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001111 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001112 * This interface function adds a new thermal zone device (sensor) to
1113 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1114 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08001115 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001116 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001117 *
1118 * Return: a pointer to the created struct thermal_zone_device or an
1119 * in case of error, an ERR_PTR. Caller must check return value with
1120 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001121 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001122struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001123 int trips, int mask, void *devdata,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001124 struct thermal_zone_device_ops *ops,
Javi Merino6b775e82015-03-02 17:17:19 +00001125 struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001126 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001127{
1128 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001129 enum thermal_trip_type trip_type;
Zhang Rui81ad4272016-03-18 10:03:24 +08001130 int trip_temp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001131 int result;
1132 int count;
Javi Merinoe33df1d2015-02-26 19:00:27 +00001133 struct thermal_governor *governor;
Zhang Rui203d3d42008-01-17 15:51:08 +08001134
Eduardo Valentin54fa38c2016-11-07 21:08:39 -08001135 if (!type || strlen(type) == 0)
1136 return ERR_PTR(-EINVAL);
1137
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001138 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001139 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001140
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001141 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001142 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001143
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04001144 if (!ops)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001145 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001146
Jonghwa Lee83720d02013-05-18 09:50:26 +00001147 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00001148 return ERR_PTR(-EINVAL);
1149
Zhang Rui203d3d42008-01-17 15:51:08 +08001150 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1151 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001152 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001153
Zhang Rui2d374132012-06-27 10:09:00 +08001154 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001155 idr_init(&tz->idr);
1156 mutex_init(&tz->lock);
1157 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1158 if (result) {
1159 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001160 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001161 }
1162
Eduardo Valentin54fa38c2016-11-07 21:08:39 -08001163 strlcpy(tz->type, type, sizeof(tz->type));
Zhang Rui203d3d42008-01-17 15:51:08 +08001164 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301165 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001166 tz->device.class = &thermal_class;
1167 tz->devdata = devdata;
1168 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001169 tz->passive_delay = passive_delay;
1170 tz->polling_delay = polling_delay;
Eduardo Valentin1c600862016-11-07 21:08:42 -08001171
Eduardo Valentin4d0fe742016-11-07 21:08:52 -08001172 /* sys I/F */
Eduardo Valentin1c600862016-11-07 21:08:42 -08001173 /* Add nodes that are always present via .groups */
Eduardo Valentin4d0fe742016-11-07 21:08:52 -08001174 result = thermal_zone_create_device_groups(tz, mask);
1175 if (result)
1176 goto unregister;
1177
Chen Yu4511f712015-10-30 16:32:10 +08001178 /* A new thermal zone needs to be updated anyway. */
1179 atomic_set(&tz->need_update, 1);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001180
Kay Sievers354655e2009-01-06 10:44:37 -08001181 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001182 result = device_register(&tz->device);
1183 if (result) {
1184 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1185 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001186 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001187 }
1188
Zhang Rui203d3d42008-01-17 15:51:08 +08001189 for (count = 0; count < trips; count++) {
Zhang Rui81ad4272016-03-18 10:03:24 +08001190 if (tz->ops->get_trip_type(tz, count, &trip_type))
1191 set_bit(count, &tz->trips_disabled);
Zhang Rui81ad4272016-03-18 10:03:24 +08001192 if (tz->ops->get_trip_temp(tz, count, &trip_temp))
1193 set_bit(count, &tz->trips_disabled);
1194 /* Check for bogus trip points */
1195 if (trip_temp == 0)
1196 set_bit(count, &tz->trips_disabled);
Zhang Rui203d3d42008-01-17 15:51:08 +08001197 }
1198
Durgadoss Ra4a15482012-09-18 11:04:57 +05301199 /* Update 'this' zone's governor information */
1200 mutex_lock(&thermal_governor_lock);
1201
1202 if (tz->tzp)
Javi Merinoe33df1d2015-02-26 19:00:27 +00001203 governor = __find_governor(tz->tzp->governor_name);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301204 else
Javi Merinoe33df1d2015-02-26 19:00:27 +00001205 governor = def_governor;
1206
1207 result = thermal_set_governor(tz, governor);
1208 if (result) {
1209 mutex_unlock(&thermal_governor_lock);
1210 goto unregister;
1211 }
Durgadoss Ra4a15482012-09-18 11:04:57 +05301212
1213 mutex_unlock(&thermal_governor_lock);
1214
Eduardo Valentinccba4ff2013-08-15 11:34:17 -04001215 if (!tz->tzp || !tz->tzp->no_hwmon) {
1216 result = thermal_add_hwmon_sysfs(tz);
1217 if (result)
1218 goto unregister;
1219 }
Zhang Ruie68b16a2008-04-21 16:07:52 +08001220
Zhang Rui203d3d42008-01-17 15:51:08 +08001221 mutex_lock(&thermal_list_lock);
1222 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001223 mutex_unlock(&thermal_list_lock);
1224
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301225 /* Bind cooling devices for this zone */
1226 bind_tz(tz);
1227
Matthew Garrettb1569e92008-12-03 17:55:32 +00001228 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1229
Zhang Ruibb431ba2015-10-30 16:31:47 +08001230 thermal_zone_device_reset(tz);
Chen Yu4511f712015-10-30 16:32:10 +08001231 /* Update the new thermal zone and mark it as already updated. */
1232 if (atomic_cmpxchg(&tz->need_update, 1, 0))
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07001233 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001234
Yao Dongdong14015862014-10-28 07:40:25 +00001235 return tz;
Zhang Rui203d3d42008-01-17 15:51:08 +08001236
Joe Perchescaca8b82012-03-21 12:55:02 -07001237unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001238 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1239 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001240 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001241}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001242EXPORT_SYMBOL_GPL(thermal_zone_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001243
1244/**
1245 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001246 * @tz: the thermal zone device to remove
1247 */
1248void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1249{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301250 int i;
1251 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001252 struct thermal_cooling_device *cdev;
1253 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001254
1255 if (!tz)
1256 return;
1257
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301258 tzp = tz->tzp;
1259
Zhang Rui203d3d42008-01-17 15:51:08 +08001260 mutex_lock(&thermal_list_lock);
1261 list_for_each_entry(pos, &thermal_tz_list, node)
1262 if (pos == tz)
1263 break;
1264 if (pos != tz) {
1265 /* thermal zone device not found */
1266 mutex_unlock(&thermal_list_lock);
1267 return;
1268 }
1269 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301270
1271 /* Unbind all cdevs associated with 'this' thermal zone */
1272 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1273 if (tz->ops->unbind) {
1274 tz->ops->unbind(tz, cdev);
1275 continue;
1276 }
1277
1278 if (!tzp || !tzp->tbp)
1279 break;
1280
1281 for (i = 0; i < tzp->num_tbps; i++) {
1282 if (tzp->tbp[i].cdev == cdev) {
1283 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1284 tzp->tbp[i].cdev = NULL;
1285 }
1286 }
1287 }
1288
Zhang Rui203d3d42008-01-17 15:51:08 +08001289 mutex_unlock(&thermal_list_lock);
1290
Matthew Garrettb1569e92008-12-03 17:55:32 +00001291 thermal_zone_device_set_polling(tz, 0);
1292
Eduardo Valentin4d0fe742016-11-07 21:08:52 -08001293 kfree(tz->trip_type_attrs);
1294 kfree(tz->trip_temp_attrs);
1295 kfree(tz->trip_hyst_attrs);
1296 kfree(tz->trips_attribute_group.attrs);
Javi Merinoe33df1d2015-02-26 19:00:27 +00001297 thermal_set_governor(tz, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001298
Zhang Ruie68b16a2008-04-21 16:07:52 +08001299 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001300 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1301 idr_destroy(&tz->idr);
1302 mutex_destroy(&tz->lock);
1303 device_unregister(&tz->device);
Eduardo Valentine161aef2016-11-07 21:08:51 -08001304 kfree(tz->device.groups);
Zhang Rui203d3d42008-01-17 15:51:08 +08001305}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001306EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001307
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001308/**
1309 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1310 * @name: thermal zone name to fetch the temperature
1311 *
1312 * When only one zone is found with the passed name, returns a reference to it.
1313 *
1314 * Return: On success returns a reference to an unique thermal zone with
1315 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1316 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1317 */
1318struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1319{
1320 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1321 unsigned int found = 0;
1322
1323 if (!name)
1324 goto exit;
1325
1326 mutex_lock(&thermal_list_lock);
1327 list_for_each_entry(pos, &thermal_tz_list, node)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -07001328 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001329 found++;
1330 ref = pos;
1331 }
1332 mutex_unlock(&thermal_list_lock);
1333
1334 /* nothing has been found, thus an error code for it */
1335 if (found == 0)
1336 ref = ERR_PTR(-ENODEV);
1337 else if (found > 1)
1338 /* Success only when an unique zone is found */
1339 ref = ERR_PTR(-EEXIST);
1340
1341exit:
1342 return ref;
1343}
1344EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1345
Rajendra Nayak4a7069a2016-05-05 14:21:42 +05301346/**
1347 * thermal_zone_get_slope - return the slope attribute of the thermal zone
1348 * @tz: thermal zone device with the slope attribute
1349 *
1350 * Return: If the thermal zone device has a slope attribute, return it, else
1351 * return 1.
1352 */
1353int thermal_zone_get_slope(struct thermal_zone_device *tz)
1354{
1355 if (tz && tz->tzp)
1356 return tz->tzp->slope;
1357 return 1;
1358}
1359EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
1360
1361/**
1362 * thermal_zone_get_offset - return the offset attribute of the thermal zone
1363 * @tz: thermal zone device with the offset attribute
1364 *
1365 * Return: If the thermal zone device has a offset attribute, return it, else
1366 * return 0.
1367 */
1368int thermal_zone_get_offset(struct thermal_zone_device *tz)
1369{
1370 if (tz && tz->tzp)
1371 return tz->tzp->offset;
1372 return 0;
1373}
1374EXPORT_SYMBOL_GPL(thermal_zone_get_offset);
1375
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001376#ifdef CONFIG_NET
Johannes Berg2a94fe42013-11-19 15:19:39 +01001377static const struct genl_multicast_group thermal_event_mcgrps[] = {
1378 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
1379};
1380
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001381static struct genl_family thermal_event_genl_family = {
1382 .id = GENL_ID_GENERATE,
1383 .name = THERMAL_GENL_FAMILY_NAME,
1384 .version = THERMAL_GENL_VERSION,
1385 .maxattr = THERMAL_GENL_ATTR_MAX,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001386 .mcgrps = thermal_event_mcgrps,
1387 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001388};
1389
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001390int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1391 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301392{
1393 struct sk_buff *skb;
1394 struct nlattr *attr;
1395 struct thermal_genl_event *thermal_event;
1396 void *msg_header;
1397 int size;
1398 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001399 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301400
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001401 if (!tz)
1402 return -EINVAL;
1403
R.Durgadoss4cb18722010-10-27 03:33:29 +05301404 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001405 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1406 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301407
1408 skb = genlmsg_new(size, GFP_ATOMIC);
1409 if (!skb)
1410 return -ENOMEM;
1411
1412 /* add the genetlink message header */
1413 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1414 &thermal_event_genl_family, 0,
1415 THERMAL_GENL_CMD_EVENT);
1416 if (!msg_header) {
1417 nlmsg_free(skb);
1418 return -ENOMEM;
1419 }
1420
1421 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07001422 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1423 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05301424
1425 if (!attr) {
1426 nlmsg_free(skb);
1427 return -EINVAL;
1428 }
1429
1430 thermal_event = nla_data(attr);
1431 if (!thermal_event) {
1432 nlmsg_free(skb);
1433 return -EINVAL;
1434 }
1435
1436 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1437
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001438 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301439 thermal_event->event = event;
1440
1441 /* send multicast genetlink message */
Johannes Berg053c0952015-01-16 22:09:00 +01001442 genlmsg_end(skb, msg_header);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301443
Johannes Berg68eb5502013-11-19 15:19:38 +01001444 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001445 0, GFP_ATOMIC);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301446 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00001447 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301448
1449 return result;
1450}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001451EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301452
1453static int genetlink_init(void)
1454{
Johannes Berg2a94fe42013-11-19 15:19:39 +01001455 return genl_register_family(&thermal_event_genl_family);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301456}
1457
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001458static void genetlink_exit(void)
1459{
1460 genl_unregister_family(&thermal_event_genl_family);
1461}
1462#else /* !CONFIG_NET */
1463static inline int genetlink_init(void) { return 0; }
1464static inline void genetlink_exit(void) {}
1465#endif /* !CONFIG_NET */
1466
Zhang Ruiff140fe2015-10-30 16:31:58 +08001467static int thermal_pm_notify(struct notifier_block *nb,
1468 unsigned long mode, void *_unused)
1469{
1470 struct thermal_zone_device *tz;
1471
1472 switch (mode) {
1473 case PM_HIBERNATION_PREPARE:
1474 case PM_RESTORE_PREPARE:
1475 case PM_SUSPEND_PREPARE:
1476 atomic_set(&in_suspend, 1);
1477 break;
1478 case PM_POST_HIBERNATION:
1479 case PM_POST_RESTORE:
1480 case PM_POST_SUSPEND:
1481 atomic_set(&in_suspend, 0);
1482 list_for_each_entry(tz, &thermal_tz_list, node) {
1483 thermal_zone_device_reset(tz);
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07001484 thermal_zone_device_update(tz,
1485 THERMAL_EVENT_UNSPECIFIED);
Zhang Ruiff140fe2015-10-30 16:31:58 +08001486 }
1487 break;
1488 default:
1489 break;
1490 }
1491 return 0;
1492}
1493
1494static struct notifier_block thermal_pm_nb = {
1495 .notifier_call = thermal_pm_notify,
1496};
1497
Zhang Rui203d3d42008-01-17 15:51:08 +08001498static int __init thermal_init(void)
1499{
Zhang Rui80a26a52013-03-26 16:38:29 +08001500 int result;
1501
1502 result = thermal_register_governors();
1503 if (result)
1504 goto error;
Zhang Rui203d3d42008-01-17 15:51:08 +08001505
1506 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001507 if (result)
1508 goto unregister_governors;
1509
R.Durgadoss4cb18722010-10-27 03:33:29 +05301510 result = genetlink_init();
Zhang Rui80a26a52013-03-26 16:38:29 +08001511 if (result)
1512 goto unregister_class;
1513
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001514 result = of_parse_thermal_zones();
1515 if (result)
1516 goto exit_netlink;
1517
Zhang Ruiff140fe2015-10-30 16:31:58 +08001518 result = register_pm_notifier(&thermal_pm_nb);
1519 if (result)
1520 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
1521 result);
1522
Zhang Rui80a26a52013-03-26 16:38:29 +08001523 return 0;
1524
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001525exit_netlink:
1526 genetlink_exit();
Zhang Rui80a26a52013-03-26 16:38:29 +08001527unregister_class:
1528 class_unregister(&thermal_class);
Luis Henriques9d367e52014-12-03 21:20:21 +00001529unregister_governors:
1530 thermal_unregister_governors();
Zhang Rui80a26a52013-03-26 16:38:29 +08001531error:
1532 idr_destroy(&thermal_tz_idr);
1533 idr_destroy(&thermal_cdev_idr);
1534 mutex_destroy(&thermal_idr_lock);
1535 mutex_destroy(&thermal_list_lock);
1536 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001537 return result;
1538}
1539
1540static void __exit thermal_exit(void)
1541{
Zhang Ruiff140fe2015-10-30 16:31:58 +08001542 unregister_pm_notifier(&thermal_pm_nb);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001543 of_thermal_destroy_zones();
Zhang Rui80a26a52013-03-26 16:38:29 +08001544 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08001545 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08001546 thermal_unregister_governors();
Zhang Rui203d3d42008-01-17 15:51:08 +08001547 idr_destroy(&thermal_tz_idr);
1548 idr_destroy(&thermal_cdev_idr);
1549 mutex_destroy(&thermal_idr_lock);
1550 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08001551 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001552}
1553
R.Durgadoss4cb18722010-10-27 03:33:29 +05301554fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08001555module_exit(thermal_exit);