blob: cd82ae34ddfa3e7c0735fcf6e288f8382a17e2b8 [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
Durgadoss Ra4a15482012-09-18 11:04:57 +053067static struct thermal_governor *__find_governor(const char *name)
68{
69 struct thermal_governor *pos;
70
Zhang Ruif2234bc2014-01-24 10:23:19 +080071 if (!name || !name[0])
72 return def_governor;
73
Durgadoss Ra4a15482012-09-18 11:04:57 +053074 list_for_each_entry(pos, &thermal_governor_list, governor_list)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -070075 if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
Durgadoss Ra4a15482012-09-18 11:04:57 +053076 return pos;
77
78 return NULL;
79}
80
Javi Merinoe33df1d2015-02-26 19:00:27 +000081/**
82 * bind_previous_governor() - bind the previous governor of the thermal zone
83 * @tz: a valid pointer to a struct thermal_zone_device
84 * @failed_gov_name: the name of the governor that failed to register
85 *
86 * Register the previous governor of the thermal zone after a new
87 * governor has failed to be bound.
88 */
89static void bind_previous_governor(struct thermal_zone_device *tz,
90 const char *failed_gov_name)
91{
92 if (tz->governor && tz->governor->bind_to_tz) {
93 if (tz->governor->bind_to_tz(tz)) {
94 dev_err(&tz->device,
95 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
96 failed_gov_name, tz->governor->name, tz->type);
97 tz->governor = NULL;
98 }
99 }
100}
101
102/**
103 * thermal_set_governor() - Switch to another governor
104 * @tz: a valid pointer to a struct thermal_zone_device
105 * @new_gov: pointer to the new governor
106 *
107 * Change the governor of thermal zone @tz.
108 *
109 * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
110 */
111static int thermal_set_governor(struct thermal_zone_device *tz,
112 struct thermal_governor *new_gov)
113{
114 int ret = 0;
115
116 if (tz->governor && tz->governor->unbind_from_tz)
117 tz->governor->unbind_from_tz(tz);
118
119 if (new_gov && new_gov->bind_to_tz) {
120 ret = new_gov->bind_to_tz(tz);
121 if (ret) {
122 bind_previous_governor(tz, new_gov->name);
123
124 return ret;
125 }
126 }
127
128 tz->governor = new_gov;
129
130 return ret;
131}
132
Durgadoss Ra4a15482012-09-18 11:04:57 +0530133int thermal_register_governor(struct thermal_governor *governor)
134{
135 int err;
136 const char *name;
137 struct thermal_zone_device *pos;
138
139 if (!governor)
140 return -EINVAL;
141
142 mutex_lock(&thermal_governor_lock);
143
144 err = -EBUSY;
145 if (__find_governor(governor->name) == NULL) {
146 err = 0;
147 list_add(&governor->governor_list, &thermal_governor_list);
Zhang Ruif2234bc2014-01-24 10:23:19 +0800148 if (!def_governor && !strncmp(governor->name,
149 DEFAULT_THERMAL_GOVERNOR, THERMAL_NAME_LENGTH))
150 def_governor = governor;
Durgadoss Ra4a15482012-09-18 11:04:57 +0530151 }
152
153 mutex_lock(&thermal_list_lock);
154
155 list_for_each_entry(pos, &thermal_tz_list, node) {
Zhang Ruif2234bc2014-01-24 10:23:19 +0800156 /*
157 * only thermal zones with specified tz->tzp->governor_name
158 * may run with tz->govenor unset
159 */
Durgadoss Ra4a15482012-09-18 11:04:57 +0530160 if (pos->governor)
161 continue;
Zhang Ruif2234bc2014-01-24 10:23:19 +0800162
163 name = pos->tzp->governor_name;
164
Javi Merinoe33df1d2015-02-26 19:00:27 +0000165 if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {
166 int ret;
167
168 ret = thermal_set_governor(pos, governor);
169 if (ret)
170 dev_err(&pos->device,
171 "Failed to set governor %s for thermal zone %s: %d\n",
172 governor->name, pos->type, ret);
173 }
Durgadoss Ra4a15482012-09-18 11:04:57 +0530174 }
175
176 mutex_unlock(&thermal_list_lock);
177 mutex_unlock(&thermal_governor_lock);
178
179 return err;
180}
Durgadoss Ra4a15482012-09-18 11:04:57 +0530181
182void thermal_unregister_governor(struct thermal_governor *governor)
183{
184 struct thermal_zone_device *pos;
185
186 if (!governor)
187 return;
188
189 mutex_lock(&thermal_governor_lock);
190
191 if (__find_governor(governor->name) == NULL)
192 goto exit;
193
194 mutex_lock(&thermal_list_lock);
195
196 list_for_each_entry(pos, &thermal_tz_list, node) {
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -0700197 if (!strncasecmp(pos->governor->name, governor->name,
Durgadoss Ra4a15482012-09-18 11:04:57 +0530198 THERMAL_NAME_LENGTH))
Javi Merinoe33df1d2015-02-26 19:00:27 +0000199 thermal_set_governor(pos, NULL);
Durgadoss Ra4a15482012-09-18 11:04:57 +0530200 }
201
202 mutex_unlock(&thermal_list_lock);
203 list_del(&governor->governor_list);
204exit:
205 mutex_unlock(&thermal_governor_lock);
206 return;
207}
Zhang Rui203d3d42008-01-17 15:51:08 +0800208
209static int get_idr(struct idr *idr, struct mutex *lock, int *id)
210{
Tejun Heo6deb69f2013-02-27 17:04:46 -0800211 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800212
213 if (lock)
214 mutex_lock(lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800215 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800216 if (lock)
217 mutex_unlock(lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800218 if (unlikely(ret < 0))
219 return ret;
220 *id = ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800221 return 0;
222}
223
224static void release_idr(struct idr *idr, struct mutex *lock, int id)
225{
226 if (lock)
227 mutex_lock(lock);
228 idr_remove(idr, id);
229 if (lock)
230 mutex_unlock(lock);
231}
232
Durgadoss R9b4298a2012-09-18 11:04:54 +0530233int get_tz_trend(struct thermal_zone_device *tz, int trip)
234{
235 enum thermal_trend trend;
236
Eduardo Valentin0c872502013-05-29 21:37:00 +0000237 if (tz->emul_temperature || !tz->ops->get_trend ||
238 tz->ops->get_trend(tz, trip, &trend)) {
Durgadoss R9b4298a2012-09-18 11:04:54 +0530239 if (tz->temperature > tz->last_temperature)
240 trend = THERMAL_TREND_RAISING;
241 else if (tz->temperature < tz->last_temperature)
242 trend = THERMAL_TREND_DROPPING;
243 else
244 trend = THERMAL_TREND_STABLE;
245 }
246
247 return trend;
248}
249EXPORT_SYMBOL(get_tz_trend);
250
251struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
252 struct thermal_cooling_device *cdev, int trip)
253{
254 struct thermal_instance *pos = NULL;
255 struct thermal_instance *target_instance = NULL;
256
257 mutex_lock(&tz->lock);
258 mutex_lock(&cdev->lock);
259
260 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
261 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
262 target_instance = pos;
263 break;
264 }
265 }
266
267 mutex_unlock(&cdev->lock);
268 mutex_unlock(&tz->lock);
269
270 return target_instance;
271}
272EXPORT_SYMBOL(get_thermal_instance);
273
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530274static void print_bind_err_msg(struct thermal_zone_device *tz,
275 struct thermal_cooling_device *cdev, int ret)
276{
277 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
278 tz->type, cdev->type, ret);
279}
280
281static void __bind(struct thermal_zone_device *tz, int mask,
Eduardo Valentina8892d832013-07-16 15:26:28 -0400282 struct thermal_cooling_device *cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000283 unsigned long *limits,
284 unsigned int weight)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530285{
286 int i, ret;
287
288 for (i = 0; i < tz->trips; i++) {
289 if (mask & (1 << i)) {
Eduardo Valentina8892d832013-07-16 15:26:28 -0400290 unsigned long upper, lower;
291
292 upper = THERMAL_NO_LIMIT;
293 lower = THERMAL_NO_LIMIT;
294 if (limits) {
295 lower = limits[i * 2];
296 upper = limits[i * 2 + 1];
297 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530298 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000299 upper, lower,
300 weight);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530301 if (ret)
302 print_bind_err_msg(tz, cdev, ret);
303 }
304 }
305}
306
307static void __unbind(struct thermal_zone_device *tz, int mask,
308 struct thermal_cooling_device *cdev)
309{
310 int i;
311
312 for (i = 0; i < tz->trips; i++)
313 if (mask & (1 << i))
314 thermal_zone_unbind_cooling_device(tz, i, cdev);
315}
316
317static void bind_cdev(struct thermal_cooling_device *cdev)
318{
319 int i, ret;
320 const struct thermal_zone_params *tzp;
321 struct thermal_zone_device *pos = NULL;
322
323 mutex_lock(&thermal_list_lock);
324
325 list_for_each_entry(pos, &thermal_tz_list, node) {
326 if (!pos->tzp && !pos->ops->bind)
327 continue;
328
Ni Wadea9f2d192013-11-06 14:30:13 +0800329 if (pos->ops->bind) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530330 ret = pos->ops->bind(pos, cdev);
331 if (ret)
332 print_bind_err_msg(pos, cdev, ret);
Ni Wadea9f2d192013-11-06 14:30:13 +0800333 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530334 }
335
336 tzp = pos->tzp;
Hugh Dickins791700c2012-09-27 16:48:28 -0700337 if (!tzp || !tzp->tbp)
338 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530339
340 for (i = 0; i < tzp->num_tbps; i++) {
341 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
342 continue;
343 if (tzp->tbp[i].match(pos, cdev))
344 continue;
345 tzp->tbp[i].cdev = cdev;
Eduardo Valentina8892d832013-07-16 15:26:28 -0400346 __bind(pos, tzp->tbp[i].trip_mask, cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000347 tzp->tbp[i].binding_limits,
348 tzp->tbp[i].weight);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530349 }
350 }
351
352 mutex_unlock(&thermal_list_lock);
353}
354
355static void bind_tz(struct thermal_zone_device *tz)
356{
357 int i, ret;
358 struct thermal_cooling_device *pos = NULL;
359 const struct thermal_zone_params *tzp = tz->tzp;
360
361 if (!tzp && !tz->ops->bind)
362 return;
363
364 mutex_lock(&thermal_list_lock);
365
Ni Wadea9f2d192013-11-06 14:30:13 +0800366 /* If there is ops->bind, try to use ops->bind */
367 if (tz->ops->bind) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530368 list_for_each_entry(pos, &thermal_cdev_list, node) {
369 ret = tz->ops->bind(tz, pos);
370 if (ret)
371 print_bind_err_msg(tz, pos, ret);
372 }
373 goto exit;
374 }
375
Hugh Dickins791700c2012-09-27 16:48:28 -0700376 if (!tzp || !tzp->tbp)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530377 goto exit;
378
379 list_for_each_entry(pos, &thermal_cdev_list, node) {
380 for (i = 0; i < tzp->num_tbps; i++) {
381 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
382 continue;
383 if (tzp->tbp[i].match(tz, pos))
384 continue;
385 tzp->tbp[i].cdev = pos;
Eduardo Valentina8892d832013-07-16 15:26:28 -0400386 __bind(tz, tzp->tbp[i].trip_mask, pos,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000387 tzp->tbp[i].binding_limits,
388 tzp->tbp[i].weight);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530389 }
390 }
391exit:
392 mutex_unlock(&thermal_list_lock);
393}
394
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530395static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
396 int delay)
397{
398 if (delay > 1000)
399 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
400 round_jiffies(msecs_to_jiffies(delay)));
401 else if (delay)
402 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
403 msecs_to_jiffies(delay));
404 else
405 cancel_delayed_work(&tz->poll_queue);
406}
407
408static void monitor_thermal_zone(struct thermal_zone_device *tz)
409{
410 mutex_lock(&tz->lock);
411
412 if (tz->passive)
413 thermal_zone_device_set_polling(tz, tz->passive_delay);
414 else if (tz->polling_delay)
415 thermal_zone_device_set_polling(tz, tz->polling_delay);
416 else
417 thermal_zone_device_set_polling(tz, 0);
418
419 mutex_unlock(&tz->lock);
420}
421
422static void handle_non_critical_trips(struct thermal_zone_device *tz,
423 int trip, enum thermal_trip_type trip_type)
424{
Zhang Ruif2234bc2014-01-24 10:23:19 +0800425 tz->governor ? tz->governor->throttle(tz, trip) :
426 def_governor->throttle(tz, trip);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530427}
428
429static void handle_critical_trips(struct thermal_zone_device *tz,
430 int trip, enum thermal_trip_type trip_type)
431{
Sascha Hauer17e83512015-07-24 08:12:54 +0200432 int trip_temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530433
434 tz->ops->get_trip_temp(tz, trip, &trip_temp);
435
436 /* If we have not crossed the trip_temp, we do not care. */
Srinivas Pandruvada84ffe3e2014-11-12 15:43:29 -0800437 if (trip_temp <= 0 || tz->temperature < trip_temp)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530438 return;
439
Punit Agrawal208cd822014-07-29 11:50:50 +0100440 trace_thermal_zone_trip(tz, trip, trip_type);
441
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530442 if (tz->ops->notify)
443 tz->ops->notify(tz, trip, trip_type);
444
445 if (trip_type == THERMAL_TRIP_CRITICAL) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000446 dev_emerg(&tz->device,
447 "critical temperature reached(%d C),shutting down\n",
448 tz->temperature / 1000);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530449 orderly_poweroff(true);
450 }
451}
452
453static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
454{
455 enum thermal_trip_type type;
456
Zhang Rui81ad4272016-03-18 10:03:24 +0800457 /* Ignore disabled trip points */
458 if (test_bit(trip, &tz->trips_disabled))
459 return;
460
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530461 tz->ops->get_trip_type(tz, trip, &type);
462
463 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
464 handle_critical_trips(tz, trip, type);
465 else
466 handle_non_critical_trips(tz, trip, type);
467 /*
468 * Alright, we handled this trip successfully.
469 * So, start monitoring again.
470 */
471 monitor_thermal_zone(tz);
472}
473
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000474/**
Sascha Hauerf6be0582015-07-06 09:46:14 +0200475 * thermal_zone_get_temp() - returns the temperature of a thermal zone
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000476 * @tz: a valid pointer to a struct thermal_zone_device
477 * @temp: a valid pointer to where to store the resulting temperature.
478 *
479 * When a valid thermal zone reference is passed, it will fetch its
480 * temperature and fill @temp.
481 *
482 * Return: On success returns 0, an error code otherwise
483 */
Sascha Hauer17e83512015-07-24 08:12:54 +0200484int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000485{
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000486 int ret = -EINVAL;
Zhang Rui5e20b2e2013-02-06 14:02:12 +0800487 int count;
Sascha Hauer17e83512015-07-24 08:12:54 +0200488 int crit_temp = INT_MAX;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000489 enum thermal_trip_type type;
490
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400491 if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000492 goto exit;
493
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000494 mutex_lock(&tz->lock);
495
496 ret = tz->ops->get_temp(tz, temp);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000497
Sascha Hauer79e54212015-07-06 09:46:16 +0200498 if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
499 for (count = 0; count < tz->trips; count++) {
500 ret = tz->ops->get_trip_type(tz, count, &type);
501 if (!ret && type == THERMAL_TRIP_CRITICAL) {
502 ret = tz->ops->get_trip_temp(tz, count,
503 &crit_temp);
504 break;
505 }
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000506 }
Sascha Hauer79e54212015-07-06 09:46:16 +0200507
Sascha Hauer934c93b2015-07-06 09:46:17 +0200508 /*
509 * Only allow emulating a temperature when the real temperature
510 * is below the critical temperature so that the emulation code
511 * cannot hide critical conditions.
512 */
Sascha Hauer79e54212015-07-06 09:46:16 +0200513 if (!ret && *temp < crit_temp)
514 *temp = tz->emul_temperature;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000515 }
Sascha Hauer79e54212015-07-06 09:46:16 +0200516
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000517 mutex_unlock(&tz->lock);
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000518exit:
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000519 return ret;
520}
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000521EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000522
Sascha Hauer060c0342016-06-22 16:42:01 +0800523void thermal_zone_set_trips(struct thermal_zone_device *tz)
524{
525 int low = -INT_MAX;
526 int high = INT_MAX;
527 int trip_temp, hysteresis;
528 int i, ret;
529
530 mutex_lock(&tz->lock);
531
532 if (!tz->ops->set_trips || !tz->ops->get_trip_hyst)
533 goto exit;
534
535 for (i = 0; i < tz->trips; i++) {
536 int trip_low;
537
538 tz->ops->get_trip_temp(tz, i, &trip_temp);
539 tz->ops->get_trip_hyst(tz, i, &hysteresis);
540
541 trip_low = trip_temp - hysteresis;
542
543 if (trip_low < tz->temperature && trip_low > low)
544 low = trip_low;
545
546 if (trip_temp > tz->temperature && trip_temp < high)
547 high = trip_temp;
548 }
549
550 /* No need to change trip points */
551 if (tz->prev_low_trip == low && tz->prev_high_trip == high)
552 goto exit;
553
554 tz->prev_low_trip = low;
555 tz->prev_high_trip = high;
556
557 dev_dbg(&tz->device,
558 "new temperature boundaries: %d < x < %d\n", low, high);
559
560 /*
561 * Set a temperature window. When this window is left the driver
562 * must inform the thermal core via thermal_zone_device_update.
563 */
564 ret = tz->ops->set_trips(tz, low, high);
565 if (ret)
566 dev_err(&tz->device, "Failed to set trips: %d\n", ret);
567
568exit:
569 mutex_unlock(&tz->lock);
570}
571EXPORT_SYMBOL_GPL(thermal_zone_set_trips);
572
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530573static void update_temperature(struct thermal_zone_device *tz)
574{
Sascha Hauer17e83512015-07-24 08:12:54 +0200575 int temp, ret;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530576
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000577 ret = thermal_zone_get_temp(tz, &temp);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530578 if (ret) {
Hans de Goede7e497a72015-03-21 15:02:55 +0100579 if (ret != -EAGAIN)
580 dev_warn(&tz->device,
581 "failed to read out thermal zone (%d)\n",
582 ret);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000583 return;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530584 }
585
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000586 mutex_lock(&tz->lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530587 tz->last_temperature = tz->temperature;
588 tz->temperature = temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530589 mutex_unlock(&tz->lock);
Aaron Lu06475b52013-12-02 13:54:26 +0800590
Punit Agrawal100a8fd2014-07-29 11:50:48 +0100591 trace_thermal_temperature(tz);
Zhang Ruibb431ba2015-10-30 16:31:47 +0800592 if (tz->last_temperature == THERMAL_TEMP_INVALID)
593 dev_dbg(&tz->device, "last_temperature N/A, current_temperature=%d\n",
594 tz->temperature);
595 else
596 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
597 tz->last_temperature, tz->temperature);
598}
599
Wei Wanga61cdb82018-11-07 14:36:11 -0800600static void thermal_zone_device_init(struct thermal_zone_device *tz)
Zhang Ruibb431ba2015-10-30 16:31:47 +0800601{
602 struct thermal_instance *pos;
Zhang Ruibb431ba2015-10-30 16:31:47 +0800603 tz->temperature = THERMAL_TEMP_INVALID;
Zhang Ruibb431ba2015-10-30 16:31:47 +0800604 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
605 pos->initialized = false;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530606}
607
Wei Wanga61cdb82018-11-07 14:36:11 -0800608static void thermal_zone_device_reset(struct thermal_zone_device *tz)
609{
610 tz->passive = 0;
611 thermal_zone_device_init(tz);
612}
613
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700614void thermal_zone_device_update(struct thermal_zone_device *tz,
615 enum thermal_notify_event event)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530616{
617 int count;
618
Zhang Ruiff140fe2015-10-30 16:31:58 +0800619 if (atomic_read(&in_suspend))
620 return;
621
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400622 if (!tz->ops->get_temp)
623 return;
624
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530625 update_temperature(tz);
626
Sascha Hauer060c0342016-06-22 16:42:01 +0800627 thermal_zone_set_trips(tz);
628
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700629 tz->notify_event = event;
630
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530631 for (count = 0; count < tz->trips; count++)
632 handle_thermal_trip(tz, count);
633}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000634EXPORT_SYMBOL_GPL(thermal_zone_device_update);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530635
636static void thermal_zone_device_check(struct work_struct *work)
637{
638 struct thermal_zone_device *tz = container_of(work, struct
639 thermal_zone_device,
640 poll_queue.work);
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700641 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530642}
643
Zhang Rui203d3d42008-01-17 15:51:08 +0800644/* sys I/F for thermal zone */
645
646#define to_thermal_zone(_dev) \
647 container_of(_dev, struct thermal_zone_device, device)
648
649static ssize_t
650type_show(struct device *dev, struct device_attribute *attr, char *buf)
651{
652 struct thermal_zone_device *tz = to_thermal_zone(dev);
653
654 return sprintf(buf, "%s\n", tz->type);
655}
656
657static ssize_t
658temp_show(struct device *dev, struct device_attribute *attr, char *buf)
659{
660 struct thermal_zone_device *tz = to_thermal_zone(dev);
Sascha Hauer17e83512015-07-24 08:12:54 +0200661 int temperature, ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800662
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000663 ret = thermal_zone_get_temp(tz, &temperature);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000664
665 if (ret)
666 return ret;
667
Sascha Hauer17e83512015-07-24 08:12:54 +0200668 return sprintf(buf, "%d\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800669}
670
671static ssize_t
672mode_show(struct device *dev, struct device_attribute *attr, char *buf)
673{
674 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000675 enum thermal_device_mode mode;
676 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800677
678 if (!tz->ops->get_mode)
679 return -EPERM;
680
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000681 result = tz->ops->get_mode(tz, &mode);
682 if (result)
683 return result;
684
685 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
686 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800687}
688
689static ssize_t
690mode_store(struct device *dev, struct device_attribute *attr,
691 const char *buf, size_t count)
692{
693 struct thermal_zone_device *tz = to_thermal_zone(dev);
694 int result;
695
696 if (!tz->ops->set_mode)
697 return -EPERM;
698
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530699 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000700 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530701 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000702 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
703 else
704 result = -EINVAL;
705
Zhang Rui203d3d42008-01-17 15:51:08 +0800706 if (result)
707 return result;
708
709 return count;
710}
711
712static ssize_t
713trip_point_type_show(struct device *dev, struct device_attribute *attr,
714 char *buf)
715{
716 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000717 enum thermal_trip_type type;
718 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800719
720 if (!tz->ops->get_trip_type)
721 return -EPERM;
722
723 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
724 return -EINVAL;
725
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000726 result = tz->ops->get_trip_type(tz, trip, &type);
727 if (result)
728 return result;
729
730 switch (type) {
731 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300732 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000733 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300734 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000735 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300736 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000737 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300738 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000739 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300740 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000741 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800742}
743
744static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800745trip_point_temp_store(struct device *dev, struct device_attribute *attr,
746 const char *buf, size_t count)
747{
748 struct thermal_zone_device *tz = to_thermal_zone(dev);
749 int trip, ret;
Wei Ni1d0fd422016-03-03 17:33:46 +0800750 int temperature;
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800751
752 if (!tz->ops->set_trip_temp)
753 return -EPERM;
754
755 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
756 return -EINVAL;
757
Wei Ni1d0fd422016-03-03 17:33:46 +0800758 if (kstrtoint(buf, 10, &temperature))
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800759 return -EINVAL;
760
761 ret = tz->ops->set_trip_temp(tz, trip, temperature);
Kuninori Morimotoad74e462015-12-15 01:19:53 +0000762 if (ret)
763 return ret;
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800764
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700765 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Kuninori Morimotoad74e462015-12-15 01:19:53 +0000766
767 return count;
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800768}
769
770static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800771trip_point_temp_show(struct device *dev, struct device_attribute *attr,
772 char *buf)
773{
774 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000775 int trip, ret;
Sascha Hauer17e83512015-07-24 08:12:54 +0200776 int temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800777
778 if (!tz->ops->get_trip_temp)
779 return -EPERM;
780
781 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
782 return -EINVAL;
783
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000784 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
785
786 if (ret)
787 return ret;
788
Sascha Hauer17e83512015-07-24 08:12:54 +0200789 return sprintf(buf, "%d\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800790}
791
Matthew Garrett03a971a2008-12-03 18:00:38 +0000792static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800793trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
794 const char *buf, size_t count)
795{
796 struct thermal_zone_device *tz = to_thermal_zone(dev);
797 int trip, ret;
Sascha Hauer17e83512015-07-24 08:12:54 +0200798 int temperature;
Durgadoss R27365a62012-07-25 10:10:59 +0800799
800 if (!tz->ops->set_trip_hyst)
801 return -EPERM;
802
803 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
804 return -EINVAL;
805
Sascha Hauer17e83512015-07-24 08:12:54 +0200806 if (kstrtoint(buf, 10, &temperature))
Durgadoss R27365a62012-07-25 10:10:59 +0800807 return -EINVAL;
808
809 /*
810 * We are not doing any check on the 'temperature' value
811 * here. The driver implementing 'set_trip_hyst' has to
812 * take care of this.
813 */
814 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
815
Sascha Hauer060c0342016-06-22 16:42:01 +0800816 if (!ret)
817 thermal_zone_set_trips(tz);
818
Durgadoss R27365a62012-07-25 10:10:59 +0800819 return ret ? ret : count;
820}
821
822static ssize_t
823trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
824 char *buf)
825{
826 struct thermal_zone_device *tz = to_thermal_zone(dev);
827 int trip, ret;
Sascha Hauer17e83512015-07-24 08:12:54 +0200828 int temperature;
Durgadoss R27365a62012-07-25 10:10:59 +0800829
830 if (!tz->ops->get_trip_hyst)
831 return -EPERM;
832
833 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
834 return -EINVAL;
835
836 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
837
Sascha Hauer17e83512015-07-24 08:12:54 +0200838 return ret ? ret : sprintf(buf, "%d\n", temperature);
Durgadoss R27365a62012-07-25 10:10:59 +0800839}
840
841static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000842passive_store(struct device *dev, struct device_attribute *attr,
843 const char *buf, size_t count)
844{
845 struct thermal_zone_device *tz = to_thermal_zone(dev);
846 struct thermal_cooling_device *cdev = NULL;
847 int state;
848
849 if (!sscanf(buf, "%d\n", &state))
850 return -EINVAL;
851
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100852 /* sanity check: values below 1000 millicelcius don't make sense
853 * and can cause the system to go into a thermal heart attack
854 */
855 if (state && state < 1000)
856 return -EINVAL;
857
Matthew Garrett03a971a2008-12-03 18:00:38 +0000858 if (state && !tz->forced_passive) {
859 mutex_lock(&thermal_list_lock);
860 list_for_each_entry(cdev, &thermal_cdev_list, node) {
861 if (!strncmp("Processor", cdev->type,
862 sizeof("Processor")))
863 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800864 THERMAL_TRIPS_NONE, cdev,
865 THERMAL_NO_LIMIT,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000866 THERMAL_NO_LIMIT,
867 THERMAL_WEIGHT_DEFAULT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000868 }
869 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100870 if (!tz->passive_delay)
871 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000872 } else if (!state && tz->forced_passive) {
873 mutex_lock(&thermal_list_lock);
874 list_for_each_entry(cdev, &thermal_cdev_list, node) {
875 if (!strncmp("Processor", cdev->type,
876 sizeof("Processor")))
877 thermal_zone_unbind_cooling_device(tz,
878 THERMAL_TRIPS_NONE,
879 cdev);
880 }
881 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100882 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000883 }
884
Matthew Garrett03a971a2008-12-03 18:00:38 +0000885 tz->forced_passive = state;
886
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700887 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000888
889 return count;
890}
891
892static ssize_t
893passive_show(struct device *dev, struct device_attribute *attr,
894 char *buf)
895{
896 struct thermal_zone_device *tz = to_thermal_zone(dev);
897
898 return sprintf(buf, "%d\n", tz->forced_passive);
899}
900
Durgadoss R5a2c0902012-09-18 11:04:58 +0530901static ssize_t
902policy_store(struct device *dev, struct device_attribute *attr,
903 const char *buf, size_t count)
904{
905 int ret = -EINVAL;
906 struct thermal_zone_device *tz = to_thermal_zone(dev);
907 struct thermal_governor *gov;
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000908 char name[THERMAL_NAME_LENGTH];
909
910 snprintf(name, sizeof(name), "%s", buf);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530911
912 mutex_lock(&thermal_governor_lock);
Javi Merinob6cc7722014-11-25 16:00:33 +0000913 mutex_lock(&tz->lock);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530914
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000915 gov = __find_governor(strim(name));
Durgadoss R5a2c0902012-09-18 11:04:58 +0530916 if (!gov)
917 goto exit;
918
Javi Merinoe33df1d2015-02-26 19:00:27 +0000919 ret = thermal_set_governor(tz, gov);
920 if (!ret)
921 ret = count;
Durgadoss R5a2c0902012-09-18 11:04:58 +0530922
923exit:
Javi Merinob6cc7722014-11-25 16:00:33 +0000924 mutex_unlock(&tz->lock);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530925 mutex_unlock(&thermal_governor_lock);
926 return ret;
927}
928
929static ssize_t
930policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
931{
932 struct thermal_zone_device *tz = to_thermal_zone(dev);
933
934 return sprintf(buf, "%s\n", tz->governor->name);
935}
936
Ni Wade25a0a5c2015-07-14 15:40:56 +0800937static ssize_t
938available_policies_show(struct device *dev, struct device_attribute *devattr,
939 char *buf)
940{
941 struct thermal_governor *pos;
942 ssize_t count = 0;
943 ssize_t size = PAGE_SIZE;
944
945 mutex_lock(&thermal_governor_lock);
946
947 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
948 size = PAGE_SIZE - count;
949 count += scnprintf(buf + count, size, "%s ", pos->name);
950 }
951 count += scnprintf(buf + count, size, "\n");
952
953 mutex_unlock(&thermal_governor_lock);
954
955 return count;
956}
957
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000958static ssize_t
959emul_temp_store(struct device *dev, struct device_attribute *attr,
960 const char *buf, size_t count)
961{
962 struct thermal_zone_device *tz = to_thermal_zone(dev);
963 int ret = 0;
Wei Ni1d0fd422016-03-03 17:33:46 +0800964 int temperature;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000965
Wei Ni1d0fd422016-03-03 17:33:46 +0800966 if (kstrtoint(buf, 10, &temperature))
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000967 return -EINVAL;
968
969 if (!tz->ops->set_emul_temp) {
970 mutex_lock(&tz->lock);
971 tz->emul_temperature = temperature;
972 mutex_unlock(&tz->lock);
973 } else {
974 ret = tz->ops->set_emul_temp(tz, temperature);
975 }
976
lan,Tianyu800744b2014-01-02 15:47:54 +0800977 if (!ret)
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700978 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
lan,Tianyu800744b2014-01-02 15:47:54 +0800979
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000980 return ret ? ret : count;
981}
982static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000983
Javi Merino9f382712015-03-26 15:53:02 +0000984static ssize_t
985sustainable_power_show(struct device *dev, struct device_attribute *devattr,
986 char *buf)
987{
988 struct thermal_zone_device *tz = to_thermal_zone(dev);
989
990 if (tz->tzp)
991 return sprintf(buf, "%u\n", tz->tzp->sustainable_power);
992 else
993 return -EIO;
994}
995
996static ssize_t
997sustainable_power_store(struct device *dev, struct device_attribute *devattr,
998 const char *buf, size_t count)
999{
1000 struct thermal_zone_device *tz = to_thermal_zone(dev);
1001 u32 sustainable_power;
1002
1003 if (!tz->tzp)
1004 return -EIO;
1005
1006 if (kstrtou32(buf, 10, &sustainable_power))
1007 return -EINVAL;
1008
1009 tz->tzp->sustainable_power = sustainable_power;
1010
1011 return count;
1012}
1013static DEVICE_ATTR(sustainable_power, S_IWUSR | S_IRUGO, sustainable_power_show,
1014 sustainable_power_store);
1015
1016#define create_s32_tzp_attr(name) \
1017 static ssize_t \
1018 name##_show(struct device *dev, struct device_attribute *devattr, \
1019 char *buf) \
1020 { \
1021 struct thermal_zone_device *tz = to_thermal_zone(dev); \
1022 \
1023 if (tz->tzp) \
Leo Yan15333e32016-03-29 19:24:15 +08001024 return sprintf(buf, "%d\n", tz->tzp->name); \
Javi Merino9f382712015-03-26 15:53:02 +00001025 else \
1026 return -EIO; \
1027 } \
1028 \
1029 static ssize_t \
1030 name##_store(struct device *dev, struct device_attribute *devattr, \
1031 const char *buf, size_t count) \
1032 { \
1033 struct thermal_zone_device *tz = to_thermal_zone(dev); \
1034 s32 value; \
1035 \
1036 if (!tz->tzp) \
1037 return -EIO; \
1038 \
1039 if (kstrtos32(buf, 10, &value)) \
1040 return -EINVAL; \
1041 \
1042 tz->tzp->name = value; \
1043 \
1044 return count; \
1045 } \
1046 static DEVICE_ATTR(name, S_IWUSR | S_IRUGO, name##_show, name##_store)
1047
1048create_s32_tzp_attr(k_po);
1049create_s32_tzp_attr(k_pu);
1050create_s32_tzp_attr(k_i);
1051create_s32_tzp_attr(k_d);
1052create_s32_tzp_attr(integral_cutoff);
Eduardo Valentin9d0be7f2015-05-11 19:34:23 -07001053create_s32_tzp_attr(slope);
1054create_s32_tzp_attr(offset);
Javi Merino9f382712015-03-26 15:53:02 +00001055#undef create_s32_tzp_attr
1056
1057static struct device_attribute *dev_tzp_attrs[] = {
1058 &dev_attr_sustainable_power,
1059 &dev_attr_k_po,
1060 &dev_attr_k_pu,
1061 &dev_attr_k_i,
1062 &dev_attr_k_d,
1063 &dev_attr_integral_cutoff,
Eduardo Valentin9d0be7f2015-05-11 19:34:23 -07001064 &dev_attr_slope,
1065 &dev_attr_offset,
Javi Merino9f382712015-03-26 15:53:02 +00001066};
1067
1068static int create_tzp_attrs(struct device *dev)
1069{
1070 int i;
1071
1072 for (i = 0; i < ARRAY_SIZE(dev_tzp_attrs); i++) {
1073 int ret;
1074 struct device_attribute *dev_attr = dev_tzp_attrs[i];
1075
1076 ret = device_create_file(dev, dev_attr);
1077 if (ret)
1078 return ret;
1079 }
1080
1081 return 0;
1082}
1083
Javi Merino35b11d22015-02-26 19:00:28 +00001084/**
1085 * power_actor_get_max_power() - get the maximum power that a cdev can consume
1086 * @cdev: pointer to &thermal_cooling_device
1087 * @tz: a valid thermal zone device pointer
1088 * @max_power: pointer in which to store the maximum power
1089 *
1090 * Calculate the maximum power consumption in milliwats that the
1091 * cooling device can currently consume and store it in @max_power.
1092 *
1093 * Return: 0 on success, -EINVAL if @cdev doesn't support the
1094 * power_actor API or -E* on other error.
1095 */
1096int power_actor_get_max_power(struct thermal_cooling_device *cdev,
1097 struct thermal_zone_device *tz, u32 *max_power)
1098{
1099 if (!cdev_is_power_actor(cdev))
1100 return -EINVAL;
1101
1102 return cdev->ops->state2power(cdev, tz, 0, max_power);
1103}
1104
1105/**
Javi Merinoc973c3b2015-09-14 14:23:50 +01001106 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
1107 * @cdev: pointer to &thermal_cooling_device
1108 * @tz: a valid thermal zone device pointer
1109 * @min_power: pointer in which to store the minimum power
1110 *
1111 * Calculate the minimum power consumption in milliwatts that the
1112 * cooling device can currently consume and store it in @min_power.
1113 *
1114 * Return: 0 on success, -EINVAL if @cdev doesn't support the
1115 * power_actor API or -E* on other error.
1116 */
1117int power_actor_get_min_power(struct thermal_cooling_device *cdev,
1118 struct thermal_zone_device *tz, u32 *min_power)
1119{
1120 unsigned long max_state;
1121 int ret;
1122
1123 if (!cdev_is_power_actor(cdev))
1124 return -EINVAL;
1125
1126 ret = cdev->ops->get_max_state(cdev, &max_state);
1127 if (ret)
1128 return ret;
1129
1130 return cdev->ops->state2power(cdev, tz, max_state, min_power);
1131}
1132
1133/**
Javi Merino35b11d22015-02-26 19:00:28 +00001134 * power_actor_set_power() - limit the maximum power that a cooling device can consume
1135 * @cdev: pointer to &thermal_cooling_device
1136 * @instance: thermal instance to update
1137 * @power: the power in milliwatts
1138 *
1139 * Set the cooling device to consume at most @power milliwatts.
1140 *
1141 * Return: 0 on success, -EINVAL if the cooling device does not
1142 * implement the power actor API or -E* for other failures.
1143 */
1144int power_actor_set_power(struct thermal_cooling_device *cdev,
1145 struct thermal_instance *instance, u32 power)
1146{
1147 unsigned long state;
1148 int ret;
1149
1150 if (!cdev_is_power_actor(cdev))
1151 return -EINVAL;
1152
1153 ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
1154 if (ret)
1155 return ret;
1156
1157 instance->target = state;
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01001158 mutex_lock(&cdev->lock);
Javi Merino35b11d22015-02-26 19:00:28 +00001159 cdev->updated = false;
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01001160 mutex_unlock(&cdev->lock);
Javi Merino35b11d22015-02-26 19:00:28 +00001161 thermal_cdev_update(cdev);
1162
1163 return 0;
1164}
1165
Zhang Rui203d3d42008-01-17 15:51:08 +08001166static DEVICE_ATTR(type, 0444, type_show, NULL);
1167static DEVICE_ATTR(temp, 0444, temp_show, NULL);
1168static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -07001169static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301170static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
Ni Wade25a0a5c2015-07-14 15:40:56 +08001171static DEVICE_ATTR(available_policies, S_IRUGO, available_policies_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001172
Zhang Rui203d3d42008-01-17 15:51:08 +08001173/* sys I/F for cooling device */
1174#define to_cooling_device(_dev) \
1175 container_of(_dev, struct thermal_cooling_device, device)
1176
1177static ssize_t
1178thermal_cooling_device_type_show(struct device *dev,
1179 struct device_attribute *attr, char *buf)
1180{
1181 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1182
1183 return sprintf(buf, "%s\n", cdev->type);
1184}
1185
1186static ssize_t
1187thermal_cooling_device_max_state_show(struct device *dev,
1188 struct device_attribute *attr, char *buf)
1189{
1190 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001191 unsigned long state;
1192 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +08001193
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001194 ret = cdev->ops->get_max_state(cdev, &state);
1195 if (ret)
1196 return ret;
1197 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +08001198}
1199
1200static ssize_t
1201thermal_cooling_device_cur_state_show(struct device *dev,
1202 struct device_attribute *attr, char *buf)
1203{
1204 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001205 unsigned long state;
1206 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +08001207
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001208 ret = cdev->ops->get_cur_state(cdev, &state);
1209 if (ret)
1210 return ret;
1211 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +08001212}
1213
1214static ssize_t
1215thermal_cooling_device_cur_state_store(struct device *dev,
1216 struct device_attribute *attr,
1217 const char *buf, size_t count)
1218{
1219 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001220 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +08001221 int result;
1222
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001223 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +08001224 return -EINVAL;
1225
Roel Kluinedb94912009-12-15 22:46:50 +01001226 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +08001227 return -EINVAL;
1228
1229 result = cdev->ops->set_cur_state(cdev, state);
1230 if (result)
1231 return result;
1232 return count;
1233}
1234
1235static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -05001236__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001237static DEVICE_ATTR(max_state, 0444,
1238 thermal_cooling_device_max_state_show, NULL);
1239static DEVICE_ATTR(cur_state, 0644,
1240 thermal_cooling_device_cur_state_show,
1241 thermal_cooling_device_cur_state_store);
1242
1243static ssize_t
1244thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -05001245 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +08001246{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001247 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +08001248
1249 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001250 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001251
1252 if (instance->trip == THERMAL_TRIPS_NONE)
1253 return sprintf(buf, "-1\n");
1254 else
1255 return sprintf(buf, "%d\n", instance->trip);
1256}
1257
Matthias Kaehlcke2dc10f82015-02-20 18:10:08 -08001258static struct attribute *cooling_device_attrs[] = {
1259 &dev_attr_cdev_type.attr,
1260 &dev_attr_max_state.attr,
1261 &dev_attr_cur_state.attr,
1262 NULL,
1263};
1264
1265static const struct attribute_group cooling_device_attr_group = {
1266 .attrs = cooling_device_attrs,
1267};
1268
1269static const struct attribute_group *cooling_device_attr_groups[] = {
1270 &cooling_device_attr_group,
1271 NULL,
1272};
1273
Javi Merinodb916512015-02-18 16:04:24 +00001274static ssize_t
1275thermal_cooling_device_weight_show(struct device *dev,
1276 struct device_attribute *attr, char *buf)
1277{
1278 struct thermal_instance *instance;
1279
1280 instance = container_of(attr, struct thermal_instance, weight_attr);
1281
1282 return sprintf(buf, "%d\n", instance->weight);
1283}
1284
1285static ssize_t
1286thermal_cooling_device_weight_store(struct device *dev,
1287 struct device_attribute *attr,
1288 const char *buf, size_t count)
1289{
1290 struct thermal_instance *instance;
1291 int ret, weight;
1292
1293 ret = kstrtoint(buf, 0, &weight);
1294 if (ret)
1295 return ret;
1296
1297 instance = container_of(attr, struct thermal_instance, weight_attr);
1298 instance->weight = weight;
1299
1300 return count;
1301}
Zhang Rui203d3d42008-01-17 15:51:08 +08001302/* Device management */
1303
1304/**
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001305 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
1306 * @tz: pointer to struct thermal_zone_device
Zhang Rui203d3d42008-01-17 15:51:08 +08001307 * @trip: indicates which trip point the cooling devices is
1308 * associated with in this thermal zone.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001309 * @cdev: pointer to struct thermal_cooling_device
1310 * @upper: the Maximum cooling state for this trip point.
1311 * THERMAL_NO_LIMIT means no upper limit,
1312 * and the cooling device can be in max_state.
1313 * @lower: the Minimum cooling state can be used for this trip point.
1314 * THERMAL_NO_LIMIT means no lower limit,
1315 * and the cooling device can be in cooling state 0.
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001316 * @weight: The weight of the cooling device to be bound to the
1317 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
1318 * default value
Len Brown543a9562008-02-07 16:55:08 -05001319 *
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001320 * This interface function bind a thermal cooling device to the certain trip
1321 * point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001322 * This function is usually called in the thermal zone device .bind callback.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001323 *
1324 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001325 */
1326int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1327 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +08001328 struct thermal_cooling_device *cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001329 unsigned long upper, unsigned long lower,
1330 unsigned int weight)
Zhang Rui203d3d42008-01-17 15:51:08 +08001331{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001332 struct thermal_instance *dev;
1333 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -05001334 struct thermal_zone_device *pos1;
1335 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +08001336 unsigned long max_state;
Lukasz Majewski9a3031d2014-11-18 11:16:30 +01001337 int result, ret;
Zhang Rui203d3d42008-01-17 15:51:08 +08001338
Len Brown543a9562008-02-07 16:55:08 -05001339 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +08001340 return -EINVAL;
1341
Thomas Sujithc7516702008-02-15 00:58:50 -05001342 list_for_each_entry(pos1, &thermal_tz_list, node) {
1343 if (pos1 == tz)
1344 break;
1345 }
1346 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1347 if (pos2 == cdev)
1348 break;
1349 }
1350
1351 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +08001352 return -EINVAL;
1353
Lukasz Majewski9a3031d2014-11-18 11:16:30 +01001354 ret = cdev->ops->get_max_state(cdev, &max_state);
1355 if (ret)
1356 return ret;
Zhang Rui9d998422012-06-26 16:35:57 +08001357
1358 /* lower default 0, upper default max_state */
1359 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1360 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1361
1362 if (lower > upper || upper > max_state)
1363 return -EINVAL;
1364
Zhang Rui203d3d42008-01-17 15:51:08 +08001365 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001366 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001367 if (!dev)
1368 return -ENOMEM;
1369 dev->tz = tz;
1370 dev->cdev = cdev;
1371 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +08001372 dev->upper = upper;
1373 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +08001374 dev->target = THERMAL_NO_TARGET;
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001375 dev->weight = weight;
Zhang Rui74051ba2012-06-26 16:27:22 +08001376
Zhang Rui203d3d42008-01-17 15:51:08 +08001377 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1378 if (result)
1379 goto free_mem;
1380
1381 sprintf(dev->name, "cdev%d", dev->id);
1382 result =
1383 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1384 if (result)
1385 goto release_idr;
1386
1387 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -07001388 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001389 dev->attr.attr.name = dev->attr_name;
1390 dev->attr.attr.mode = 0444;
1391 dev->attr.show = thermal_cooling_device_trip_point_show;
1392 result = device_create_file(&tz->device, &dev->attr);
1393 if (result)
1394 goto remove_symbol_link;
1395
Javi Merinodb916512015-02-18 16:04:24 +00001396 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
1397 sysfs_attr_init(&dev->weight_attr.attr);
1398 dev->weight_attr.attr.name = dev->weight_attr_name;
1399 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
1400 dev->weight_attr.show = thermal_cooling_device_weight_show;
1401 dev->weight_attr.store = thermal_cooling_device_weight_store;
1402 result = device_create_file(&tz->device, &dev->weight_attr);
1403 if (result)
1404 goto remove_trip_file;
1405
Zhang Rui203d3d42008-01-17 15:51:08 +08001406 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001407 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001408 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +08001409 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1410 result = -EEXIST;
1411 break;
1412 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001413 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001414 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001415 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
Chen Yu4511f712015-10-30 16:32:10 +08001416 atomic_set(&tz->need_update, 1);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001417 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001418 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001419 mutex_unlock(&tz->lock);
1420
1421 if (!result)
1422 return 0;
1423
Javi Merinodb916512015-02-18 16:04:24 +00001424 device_remove_file(&tz->device, &dev->weight_attr);
1425remove_trip_file:
Zhang Rui203d3d42008-01-17 15:51:08 +08001426 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -07001427remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +08001428 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -07001429release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +08001430 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -07001431free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +08001432 kfree(dev);
1433 return result;
1434}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001435EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001436
1437/**
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001438 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1439 * thermal zone.
1440 * @tz: pointer to a struct thermal_zone_device.
Zhang Rui203d3d42008-01-17 15:51:08 +08001441 * @trip: indicates which trip point the cooling devices is
1442 * associated with in this thermal zone.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001443 * @cdev: pointer to a struct thermal_cooling_device.
Len Brown543a9562008-02-07 16:55:08 -05001444 *
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001445 * This interface function unbind a thermal cooling device from the certain
1446 * trip point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001447 * This function is usually called in the thermal zone device .unbind callback.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001448 *
1449 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001450 */
1451int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1452 int trip,
1453 struct thermal_cooling_device *cdev)
1454{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001455 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +08001456
1457 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001458 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001459 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -05001460 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001461 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001462 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001463 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001464 mutex_unlock(&tz->lock);
1465 goto unbind;
1466 }
1467 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001468 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001469 mutex_unlock(&tz->lock);
1470
1471 return -ENODEV;
1472
Joe Perchescaca8b82012-03-21 12:55:02 -07001473unbind:
Viresh Kumar528464e2015-07-23 14:32:32 +05301474 device_remove_file(&tz->device, &pos->weight_attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001475 device_remove_file(&tz->device, &pos->attr);
1476 sysfs_remove_link(&tz->device.kobj, pos->name);
1477 release_idr(&tz->idr, &tz->lock, pos->id);
1478 kfree(pos);
1479 return 0;
1480}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001481EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001482
1483static void thermal_release(struct device *dev)
1484{
1485 struct thermal_zone_device *tz;
1486 struct thermal_cooling_device *cdev;
1487
Joe Perchescaca8b82012-03-21 12:55:02 -07001488 if (!strncmp(dev_name(dev), "thermal_zone",
1489 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001490 tz = to_thermal_zone(dev);
1491 kfree(tz);
durgadoss.r@intel.com732e4c82013-10-02 00:08:00 +05301492 } else if(!strncmp(dev_name(dev), "cooling_device",
1493 sizeof("cooling_device") - 1)){
Zhang Rui203d3d42008-01-17 15:51:08 +08001494 cdev = to_cooling_device(dev);
1495 kfree(cdev);
1496 }
1497}
1498
1499static struct class thermal_class = {
1500 .name = "thermal",
1501 .dev_release = thermal_release,
1502};
1503
1504/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001505 * __thermal_cooling_device_register() - register a new thermal cooling device
1506 * @np: a pointer to a device tree node.
Zhang Rui203d3d42008-01-17 15:51:08 +08001507 * @type: the thermal cooling device type.
1508 * @devdata: device private data.
1509 * @ops: standard thermal cooling devices callbacks.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001510 *
1511 * This interface function adds a new thermal cooling device (fan/processor/...)
1512 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1513 * to all the thermal zone devices registered at the same time.
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001514 * It also gives the opportunity to link the cooling device to a device tree
1515 * node, so that it can be bound to a thermal zone created out of device tree.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001516 *
1517 * Return: a pointer to the created struct thermal_cooling_device or an
1518 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001519 */
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001520static struct thermal_cooling_device *
1521__thermal_cooling_device_register(struct device_node *np,
1522 char *type, void *devdata,
1523 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001524{
1525 struct thermal_cooling_device *cdev;
Chen Yu4511f712015-10-30 16:32:10 +08001526 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001527 int result;
1528
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001529 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001530 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001531
1532 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001533 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001534 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001535
1536 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1537 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001538 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001539
1540 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1541 if (result) {
1542 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001543 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001544 }
1545
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001546 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +08001547 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001548 INIT_LIST_HEAD(&cdev->thermal_instances);
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001549 cdev->np = np;
Zhang Rui203d3d42008-01-17 15:51:08 +08001550 cdev->ops = ops;
Ni Wade5ca0cce2014-02-17 11:02:55 +08001551 cdev->updated = false;
Zhang Rui203d3d42008-01-17 15:51:08 +08001552 cdev->device.class = &thermal_class;
Matthias Kaehlcke2dc10f82015-02-20 18:10:08 -08001553 cdev->device.groups = cooling_device_attr_groups;
Zhang Rui203d3d42008-01-17 15:51:08 +08001554 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -08001555 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001556 result = device_register(&cdev->device);
1557 if (result) {
1558 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1559 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001560 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001561 }
1562
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301563 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +08001564 mutex_lock(&thermal_list_lock);
1565 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001566 mutex_unlock(&thermal_list_lock);
1567
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301568 /* Update binding information for 'this' new cdev */
1569 bind_cdev(cdev);
1570
Chen Yu4511f712015-10-30 16:32:10 +08001571 mutex_lock(&thermal_list_lock);
1572 list_for_each_entry(pos, &thermal_tz_list, node)
1573 if (atomic_cmpxchg(&pos->need_update, 1, 0))
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07001574 thermal_zone_device_update(pos,
1575 THERMAL_EVENT_UNSPECIFIED);
Chen Yu4511f712015-10-30 16:32:10 +08001576 mutex_unlock(&thermal_list_lock);
1577
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301578 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001579}
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001580
1581/**
1582 * thermal_cooling_device_register() - register a new thermal cooling device
1583 * @type: the thermal cooling device type.
1584 * @devdata: device private data.
1585 * @ops: standard thermal cooling devices callbacks.
1586 *
1587 * This interface function adds a new thermal cooling device (fan/processor/...)
1588 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1589 * to all the thermal zone devices registered at the same time.
1590 *
1591 * Return: a pointer to the created struct thermal_cooling_device or an
1592 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1593 */
1594struct thermal_cooling_device *
1595thermal_cooling_device_register(char *type, void *devdata,
1596 const struct thermal_cooling_device_ops *ops)
1597{
1598 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1599}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001600EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001601
1602/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001603 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1604 * @np: a pointer to a device tree node.
1605 * @type: the thermal cooling device type.
1606 * @devdata: device private data.
1607 * @ops: standard thermal cooling devices callbacks.
1608 *
1609 * This function will register a cooling device with device tree node reference.
1610 * This interface function adds a new thermal cooling device (fan/processor/...)
1611 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1612 * to all the thermal zone devices registered at the same time.
1613 *
1614 * Return: a pointer to the created struct thermal_cooling_device or an
1615 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1616 */
1617struct thermal_cooling_device *
1618thermal_of_cooling_device_register(struct device_node *np,
1619 char *type, void *devdata,
1620 const struct thermal_cooling_device_ops *ops)
1621{
1622 return __thermal_cooling_device_register(np, type, devdata, ops);
1623}
1624EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1625
1626/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001627 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001628 * @cdev: the thermal cooling device to remove.
1629 *
1630 * thermal_cooling_device_unregister() must be called when the device is no
1631 * longer needed.
1632 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301633void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001634{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301635 int i;
1636 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001637 struct thermal_zone_device *tz;
1638 struct thermal_cooling_device *pos = NULL;
1639
1640 if (!cdev)
1641 return;
1642
1643 mutex_lock(&thermal_list_lock);
1644 list_for_each_entry(pos, &thermal_cdev_list, node)
1645 if (pos == cdev)
1646 break;
1647 if (pos != cdev) {
1648 /* thermal cooling device not found */
1649 mutex_unlock(&thermal_list_lock);
1650 return;
1651 }
1652 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301653
1654 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001655 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301656 if (tz->ops->unbind) {
1657 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001658 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301659 }
1660
1661 if (!tz->tzp || !tz->tzp->tbp)
1662 continue;
1663
1664 tzp = tz->tzp;
1665 for (i = 0; i < tzp->num_tbps; i++) {
1666 if (tzp->tbp[i].cdev == cdev) {
1667 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1668 tzp->tbp[i].cdev = NULL;
1669 }
1670 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001671 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301672
Zhang Rui203d3d42008-01-17 15:51:08 +08001673 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301674
Zhang Rui203d3d42008-01-17 15:51:08 +08001675 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001676 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001677 device_remove_file(&cdev->device, &dev_attr_max_state);
1678 device_remove_file(&cdev->device, &dev_attr_cur_state);
1679
1680 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1681 device_unregister(&cdev->device);
1682 return;
1683}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001684EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001685
Durgadoss Rdc765482012-09-18 11:05:00 +05301686void thermal_cdev_update(struct thermal_cooling_device *cdev)
Zhang Ruice119f82012-06-27 14:13:04 +08001687{
1688 struct thermal_instance *instance;
1689 unsigned long target = 0;
1690
Zhang Ruif4a821c2012-07-24 16:56:21 +08001691 mutex_lock(&cdev->lock);
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01001692 /* cooling device is updated*/
1693 if (cdev->updated) {
1694 mutex_unlock(&cdev->lock);
1695 return;
1696 }
1697
Zhang Ruice119f82012-06-27 14:13:04 +08001698 /* Make sure cdev enters the deepest cooling state */
1699 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
Aaron Lu06475b52013-12-02 13:54:26 +08001700 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1701 instance->tz->id, instance->target);
Zhang Ruice119f82012-06-27 14:13:04 +08001702 if (instance->target == THERMAL_NO_TARGET)
1703 continue;
1704 if (instance->target > target)
1705 target = instance->target;
1706 }
1707 cdev->ops->set_cur_state(cdev, target);
1708 cdev->updated = true;
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01001709 mutex_unlock(&cdev->lock);
Punit Agrawal39811562014-07-29 11:50:49 +01001710 trace_cdev_update(cdev, target);
Aaron Lu06475b52013-12-02 13:54:26 +08001711 dev_dbg(&cdev->device, "set to state %lu\n", target);
Zhang Ruice119f82012-06-27 14:13:04 +08001712}
Durgadoss Rdc765482012-09-18 11:05:00 +05301713EXPORT_SYMBOL(thermal_cdev_update);
Zhang Ruice119f82012-06-27 14:13:04 +08001714
Matthew Garrettb1569e92008-12-03 17:55:32 +00001715/**
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001716 * thermal_notify_framework - Sensor drivers use this API to notify framework
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301717 * @tz: thermal zone device
1718 * @trip: indicates which trip point has been crossed
1719 *
1720 * This function handles the trip events from sensor drivers. It starts
1721 * throttling the cooling devices according to the policy configured.
1722 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1723 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1724 * The throttling policy is based on the configured platform data; if no
1725 * platform data is provided, this uses the step_wise throttling policy.
1726 */
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001727void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301728{
1729 handle_thermal_trip(tz, trip);
1730}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001731EXPORT_SYMBOL_GPL(thermal_notify_framework);
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301732
1733/**
Eduardo Valentin269c1742013-04-23 21:48:19 +00001734 * create_trip_attrs() - create attributes for trip points
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001735 * @tz: the thermal zone device
1736 * @mask: Writeable trip point bitmap.
Eduardo Valentin269c1742013-04-23 21:48:19 +00001737 *
1738 * helper function to instantiate sysfs entries for every trip
1739 * point and its properties of a struct thermal_zone_device.
1740 *
1741 * Return: 0 on success, the proper error value otherwise.
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001742 */
1743static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1744{
1745 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001746 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001747
Durgadoss R27365a62012-07-25 10:10:59 +08001748 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001749 if (!tz->trip_type_attrs)
1750 return -ENOMEM;
1751
Durgadoss R27365a62012-07-25 10:10:59 +08001752 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001753 if (!tz->trip_temp_attrs) {
1754 kfree(tz->trip_type_attrs);
1755 return -ENOMEM;
1756 }
1757
Durgadoss R27365a62012-07-25 10:10:59 +08001758 if (tz->ops->get_trip_hyst) {
1759 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1760 if (!tz->trip_hyst_attrs) {
1761 kfree(tz->trip_type_attrs);
1762 kfree(tz->trip_temp_attrs);
1763 return -ENOMEM;
1764 }
1765 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001766
Durgadoss R27365a62012-07-25 10:10:59 +08001767
1768 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001769 /* create trip type attribute */
1770 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1771 "trip_point_%d_type", indx);
1772
1773 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1774 tz->trip_type_attrs[indx].attr.attr.name =
1775 tz->trip_type_attrs[indx].name;
1776 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1777 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1778
1779 device_create_file(&tz->device,
1780 &tz->trip_type_attrs[indx].attr);
1781
1782 /* create trip temp attribute */
1783 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1784 "trip_point_%d_temp", indx);
1785
1786 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1787 tz->trip_temp_attrs[indx].attr.attr.name =
1788 tz->trip_temp_attrs[indx].name;
1789 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1790 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
Punit Agrawal35e94642015-03-03 10:43:03 +00001791 if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS) &&
1792 mask & (1 << indx)) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001793 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1794 tz->trip_temp_attrs[indx].attr.store =
1795 trip_point_temp_store;
1796 }
1797
1798 device_create_file(&tz->device,
1799 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001800
1801 /* create Optional trip hyst attribute */
1802 if (!tz->ops->get_trip_hyst)
1803 continue;
1804 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1805 "trip_point_%d_hyst", indx);
1806
1807 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1808 tz->trip_hyst_attrs[indx].attr.attr.name =
1809 tz->trip_hyst_attrs[indx].name;
1810 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1811 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1812 if (tz->ops->set_trip_hyst) {
1813 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1814 tz->trip_hyst_attrs[indx].attr.store =
1815 trip_point_hyst_store;
1816 }
1817
1818 device_create_file(&tz->device,
1819 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001820 }
1821 return 0;
1822}
1823
1824static void remove_trip_attrs(struct thermal_zone_device *tz)
1825{
1826 int indx;
1827
1828 for (indx = 0; indx < tz->trips; indx++) {
1829 device_remove_file(&tz->device,
1830 &tz->trip_type_attrs[indx].attr);
1831 device_remove_file(&tz->device,
1832 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001833 if (tz->ops->get_trip_hyst)
1834 device_remove_file(&tz->device,
1835 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001836 }
1837 kfree(tz->trip_type_attrs);
1838 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001839 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001840}
1841
1842/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001843 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001844 * @type: the thermal zone device type
1845 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001846 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001847 * @devdata: private device data
1848 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301849 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001850 * @passive_delay: number of milliseconds to wait between polls when
1851 * performing passive cooling
1852 * @polling_delay: number of milliseconds to wait between polls when checking
1853 * whether trip points have been crossed (0 for interrupt
1854 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001855 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001856 * This interface function adds a new thermal zone device (sensor) to
1857 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1858 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08001859 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001860 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001861 *
1862 * Return: a pointer to the created struct thermal_zone_device or an
1863 * in case of error, an ERR_PTR. Caller must check return value with
1864 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001865 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001866struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001867 int trips, int mask, void *devdata,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001868 struct thermal_zone_device_ops *ops,
Javi Merino6b775e82015-03-02 17:17:19 +00001869 struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001870 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001871{
1872 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001873 enum thermal_trip_type trip_type;
Zhang Rui81ad4272016-03-18 10:03:24 +08001874 int trip_temp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001875 int result;
1876 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001877 int passive = 0;
Javi Merinoe33df1d2015-02-26 19:00:27 +00001878 struct thermal_governor *governor;
Zhang Rui203d3d42008-01-17 15:51:08 +08001879
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001880 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001881 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001882
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001883 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001884 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001885
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04001886 if (!ops)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001887 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001888
Jonghwa Lee83720d02013-05-18 09:50:26 +00001889 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00001890 return ERR_PTR(-EINVAL);
1891
Zhang Rui203d3d42008-01-17 15:51:08 +08001892 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1893 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001894 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001895
Zhang Rui2d374132012-06-27 10:09:00 +08001896 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001897 idr_init(&tz->idr);
1898 mutex_init(&tz->lock);
1899 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1900 if (result) {
1901 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001902 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001903 }
1904
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001905 strlcpy(tz->type, type ? : "", sizeof(tz->type));
Zhang Rui203d3d42008-01-17 15:51:08 +08001906 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301907 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001908 tz->device.class = &thermal_class;
1909 tz->devdata = devdata;
1910 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001911 tz->passive_delay = passive_delay;
1912 tz->polling_delay = polling_delay;
Chen Yu4511f712015-10-30 16:32:10 +08001913 /* A new thermal zone needs to be updated anyway. */
1914 atomic_set(&tz->need_update, 1);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001915
Kay Sievers354655e2009-01-06 10:44:37 -08001916 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001917 result = device_register(&tz->device);
1918 if (result) {
1919 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1920 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001921 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001922 }
1923
1924 /* sys I/F */
1925 if (type) {
1926 result = device_create_file(&tz->device, &dev_attr_type);
1927 if (result)
1928 goto unregister;
1929 }
1930
1931 result = device_create_file(&tz->device, &dev_attr_temp);
1932 if (result)
1933 goto unregister;
1934
1935 if (ops->get_mode) {
1936 result = device_create_file(&tz->device, &dev_attr_mode);
1937 if (result)
1938 goto unregister;
1939 }
1940
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001941 result = create_trip_attrs(tz, mask);
1942 if (result)
1943 goto unregister;
1944
Zhang Rui203d3d42008-01-17 15:51:08 +08001945 for (count = 0; count < trips; count++) {
Zhang Rui81ad4272016-03-18 10:03:24 +08001946 if (tz->ops->get_trip_type(tz, count, &trip_type))
1947 set_bit(count, &tz->trips_disabled);
Matthew Garrett03a971a2008-12-03 18:00:38 +00001948 if (trip_type == THERMAL_TRIP_PASSIVE)
1949 passive = 1;
Zhang Rui81ad4272016-03-18 10:03:24 +08001950 if (tz->ops->get_trip_temp(tz, count, &trip_temp))
1951 set_bit(count, &tz->trips_disabled);
1952 /* Check for bogus trip points */
1953 if (trip_temp == 0)
1954 set_bit(count, &tz->trips_disabled);
Zhang Rui203d3d42008-01-17 15:51:08 +08001955 }
1956
Durgadoss R5a2c0902012-09-18 11:04:58 +05301957 if (!passive) {
1958 result = device_create_file(&tz->device, &dev_attr_passive);
1959 if (result)
1960 goto unregister;
1961 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001962
Sascha Hauer79e54212015-07-06 09:46:16 +02001963 if (IS_ENABLED(CONFIG_THERMAL_EMULATION)) {
1964 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1965 if (result)
1966 goto unregister;
1967 }
1968
Durgadoss R5a2c0902012-09-18 11:04:58 +05301969 /* Create policy attribute */
1970 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00001971 if (result)
1972 goto unregister;
1973
Javi Merino9f382712015-03-26 15:53:02 +00001974 /* Add thermal zone params */
1975 result = create_tzp_attrs(&tz->device);
1976 if (result)
1977 goto unregister;
1978
Ni Wade25a0a5c2015-07-14 15:40:56 +08001979 /* Create available_policies attribute */
1980 result = device_create_file(&tz->device, &dev_attr_available_policies);
1981 if (result)
1982 goto unregister;
1983
Durgadoss Ra4a15482012-09-18 11:04:57 +05301984 /* Update 'this' zone's governor information */
1985 mutex_lock(&thermal_governor_lock);
1986
1987 if (tz->tzp)
Javi Merinoe33df1d2015-02-26 19:00:27 +00001988 governor = __find_governor(tz->tzp->governor_name);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301989 else
Javi Merinoe33df1d2015-02-26 19:00:27 +00001990 governor = def_governor;
1991
1992 result = thermal_set_governor(tz, governor);
1993 if (result) {
1994 mutex_unlock(&thermal_governor_lock);
1995 goto unregister;
1996 }
Durgadoss Ra4a15482012-09-18 11:04:57 +05301997
1998 mutex_unlock(&thermal_governor_lock);
1999
Eduardo Valentinccba4ff2013-08-15 11:34:17 -04002000 if (!tz->tzp || !tz->tzp->no_hwmon) {
2001 result = thermal_add_hwmon_sysfs(tz);
2002 if (result)
2003 goto unregister;
2004 }
Zhang Ruie68b16a2008-04-21 16:07:52 +08002005
Zhang Rui203d3d42008-01-17 15:51:08 +08002006 mutex_lock(&thermal_list_lock);
2007 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08002008 mutex_unlock(&thermal_list_lock);
2009
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05302010 /* Bind cooling devices for this zone */
2011 bind_tz(tz);
2012
Matthew Garrettb1569e92008-12-03 17:55:32 +00002013 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
2014
Zhang Ruibb431ba2015-10-30 16:31:47 +08002015 thermal_zone_device_reset(tz);
Chen Yu4511f712015-10-30 16:32:10 +08002016 /* Update the new thermal zone and mark it as already updated. */
2017 if (atomic_cmpxchg(&tz->need_update, 1, 0))
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07002018 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Matthew Garrettb1569e92008-12-03 17:55:32 +00002019
Yao Dongdong14015862014-10-28 07:40:25 +00002020 return tz;
Zhang Rui203d3d42008-01-17 15:51:08 +08002021
Joe Perchescaca8b82012-03-21 12:55:02 -07002022unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08002023 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
2024 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002025 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08002026}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00002027EXPORT_SYMBOL_GPL(thermal_zone_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08002028
2029/**
2030 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08002031 * @tz: the thermal zone device to remove
2032 */
2033void thermal_zone_device_unregister(struct thermal_zone_device *tz)
2034{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05302035 int i;
2036 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08002037 struct thermal_cooling_device *cdev;
2038 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08002039
2040 if (!tz)
2041 return;
2042
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05302043 tzp = tz->tzp;
2044
Zhang Rui203d3d42008-01-17 15:51:08 +08002045 mutex_lock(&thermal_list_lock);
2046 list_for_each_entry(pos, &thermal_tz_list, node)
2047 if (pos == tz)
2048 break;
2049 if (pos != tz) {
2050 /* thermal zone device not found */
2051 mutex_unlock(&thermal_list_lock);
2052 return;
2053 }
2054 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05302055
2056 /* Unbind all cdevs associated with 'this' thermal zone */
2057 list_for_each_entry(cdev, &thermal_cdev_list, node) {
2058 if (tz->ops->unbind) {
2059 tz->ops->unbind(tz, cdev);
2060 continue;
2061 }
2062
2063 if (!tzp || !tzp->tbp)
2064 break;
2065
2066 for (i = 0; i < tzp->num_tbps; i++) {
2067 if (tzp->tbp[i].cdev == cdev) {
2068 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
2069 tzp->tbp[i].cdev = NULL;
2070 }
2071 }
2072 }
2073
Zhang Rui203d3d42008-01-17 15:51:08 +08002074 mutex_unlock(&thermal_list_lock);
2075
Matthew Garrettb1569e92008-12-03 17:55:32 +00002076 thermal_zone_device_set_polling(tz, 0);
2077
Zhang Rui203d3d42008-01-17 15:51:08 +08002078 if (tz->type[0])
2079 device_remove_file(&tz->device, &dev_attr_type);
2080 device_remove_file(&tz->device, &dev_attr_temp);
2081 if (tz->ops->get_mode)
2082 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05302083 device_remove_file(&tz->device, &dev_attr_policy);
Ni Wade25a0a5c2015-07-14 15:40:56 +08002084 device_remove_file(&tz->device, &dev_attr_available_policies);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002085 remove_trip_attrs(tz);
Javi Merinoe33df1d2015-02-26 19:00:27 +00002086 thermal_set_governor(tz, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08002087
Zhang Ruie68b16a2008-04-21 16:07:52 +08002088 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08002089 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
2090 idr_destroy(&tz->idr);
2091 mutex_destroy(&tz->lock);
2092 device_unregister(&tz->device);
2093 return;
2094}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00002095EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08002096
Eduardo Valentin63c4d912013-04-05 12:32:28 +00002097/**
2098 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
2099 * @name: thermal zone name to fetch the temperature
2100 *
2101 * When only one zone is found with the passed name, returns a reference to it.
2102 *
2103 * Return: On success returns a reference to an unique thermal zone with
2104 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
2105 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
2106 */
2107struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
2108{
2109 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
2110 unsigned int found = 0;
2111
2112 if (!name)
2113 goto exit;
2114
2115 mutex_lock(&thermal_list_lock);
2116 list_for_each_entry(pos, &thermal_tz_list, node)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -07002117 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
Eduardo Valentin63c4d912013-04-05 12:32:28 +00002118 found++;
2119 ref = pos;
2120 }
2121 mutex_unlock(&thermal_list_lock);
2122
2123 /* nothing has been found, thus an error code for it */
2124 if (found == 0)
2125 ref = ERR_PTR(-ENODEV);
2126 else if (found > 1)
2127 /* Success only when an unique zone is found */
2128 ref = ERR_PTR(-EEXIST);
2129
2130exit:
2131 return ref;
2132}
2133EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
2134
Rajendra Nayak4a7069a2016-05-05 14:21:42 +05302135/**
2136 * thermal_zone_get_slope - return the slope attribute of the thermal zone
2137 * @tz: thermal zone device with the slope attribute
2138 *
2139 * Return: If the thermal zone device has a slope attribute, return it, else
2140 * return 1.
2141 */
2142int thermal_zone_get_slope(struct thermal_zone_device *tz)
2143{
2144 if (tz && tz->tzp)
2145 return tz->tzp->slope;
2146 return 1;
2147}
2148EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
2149
2150/**
2151 * thermal_zone_get_offset - return the offset attribute of the thermal zone
2152 * @tz: thermal zone device with the offset attribute
2153 *
2154 * Return: If the thermal zone device has a offset attribute, return it, else
2155 * return 0.
2156 */
2157int thermal_zone_get_offset(struct thermal_zone_device *tz)
2158{
2159 if (tz && tz->tzp)
2160 return tz->tzp->offset;
2161 return 0;
2162}
2163EXPORT_SYMBOL_GPL(thermal_zone_get_offset);
2164
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002165#ifdef CONFIG_NET
Johannes Berg2a94fe42013-11-19 15:19:39 +01002166static const struct genl_multicast_group thermal_event_mcgrps[] = {
2167 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
2168};
2169
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002170static struct genl_family thermal_event_genl_family = {
2171 .id = GENL_ID_GENERATE,
2172 .name = THERMAL_GENL_FAMILY_NAME,
2173 .version = THERMAL_GENL_VERSION,
2174 .maxattr = THERMAL_GENL_ATTR_MAX,
Johannes Berg2a94fe42013-11-19 15:19:39 +01002175 .mcgrps = thermal_event_mcgrps,
2176 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002177};
2178
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00002179int thermal_generate_netlink_event(struct thermal_zone_device *tz,
2180 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05302181{
2182 struct sk_buff *skb;
2183 struct nlattr *attr;
2184 struct thermal_genl_event *thermal_event;
2185 void *msg_header;
2186 int size;
2187 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07002188 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05302189
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00002190 if (!tz)
2191 return -EINVAL;
2192
R.Durgadoss4cb18722010-10-27 03:33:29 +05302193 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07002194 size = nla_total_size(sizeof(struct thermal_genl_event)) +
2195 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302196
2197 skb = genlmsg_new(size, GFP_ATOMIC);
2198 if (!skb)
2199 return -ENOMEM;
2200
2201 /* add the genetlink message header */
2202 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
2203 &thermal_event_genl_family, 0,
2204 THERMAL_GENL_CMD_EVENT);
2205 if (!msg_header) {
2206 nlmsg_free(skb);
2207 return -ENOMEM;
2208 }
2209
2210 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07002211 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
2212 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05302213
2214 if (!attr) {
2215 nlmsg_free(skb);
2216 return -EINVAL;
2217 }
2218
2219 thermal_event = nla_data(attr);
2220 if (!thermal_event) {
2221 nlmsg_free(skb);
2222 return -EINVAL;
2223 }
2224
2225 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
2226
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00002227 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05302228 thermal_event->event = event;
2229
2230 /* send multicast genetlink message */
Johannes Berg053c0952015-01-16 22:09:00 +01002231 genlmsg_end(skb, msg_header);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302232
Johannes Berg68eb5502013-11-19 15:19:38 +01002233 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01002234 0, GFP_ATOMIC);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302235 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00002236 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302237
2238 return result;
2239}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00002240EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302241
2242static int genetlink_init(void)
2243{
Johannes Berg2a94fe42013-11-19 15:19:39 +01002244 return genl_register_family(&thermal_event_genl_family);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302245}
2246
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002247static void genetlink_exit(void)
2248{
2249 genl_unregister_family(&thermal_event_genl_family);
2250}
2251#else /* !CONFIG_NET */
2252static inline int genetlink_init(void) { return 0; }
2253static inline void genetlink_exit(void) {}
2254#endif /* !CONFIG_NET */
2255
Zhang Rui80a26a52013-03-26 16:38:29 +08002256static int __init thermal_register_governors(void)
2257{
2258 int result;
2259
2260 result = thermal_gov_step_wise_register();
2261 if (result)
2262 return result;
2263
2264 result = thermal_gov_fair_share_register();
2265 if (result)
2266 return result;
2267
Peter Feuerere4dbf982014-07-22 17:37:13 +02002268 result = thermal_gov_bang_bang_register();
2269 if (result)
2270 return result;
2271
Javi Merino6b775e82015-03-02 17:17:19 +00002272 result = thermal_gov_user_space_register();
2273 if (result)
2274 return result;
2275
2276 return thermal_gov_power_allocator_register();
Zhang Rui80a26a52013-03-26 16:38:29 +08002277}
2278
2279static void thermal_unregister_governors(void)
2280{
2281 thermal_gov_step_wise_unregister();
2282 thermal_gov_fair_share_unregister();
Peter Feuerere4dbf982014-07-22 17:37:13 +02002283 thermal_gov_bang_bang_unregister();
Zhang Rui80a26a52013-03-26 16:38:29 +08002284 thermal_gov_user_space_unregister();
Javi Merino6b775e82015-03-02 17:17:19 +00002285 thermal_gov_power_allocator_unregister();
Zhang Rui80a26a52013-03-26 16:38:29 +08002286}
2287
Zhang Ruiff140fe2015-10-30 16:31:58 +08002288static int thermal_pm_notify(struct notifier_block *nb,
2289 unsigned long mode, void *_unused)
2290{
2291 struct thermal_zone_device *tz;
2292
2293 switch (mode) {
2294 case PM_HIBERNATION_PREPARE:
2295 case PM_RESTORE_PREPARE:
2296 case PM_SUSPEND_PREPARE:
2297 atomic_set(&in_suspend, 1);
2298 break;
2299 case PM_POST_HIBERNATION:
2300 case PM_POST_RESTORE:
2301 case PM_POST_SUSPEND:
2302 atomic_set(&in_suspend, 0);
2303 list_for_each_entry(tz, &thermal_tz_list, node) {
Wei Wanga61cdb82018-11-07 14:36:11 -08002304 thermal_zone_device_init(tz);
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07002305 thermal_zone_device_update(tz,
2306 THERMAL_EVENT_UNSPECIFIED);
Zhang Ruiff140fe2015-10-30 16:31:58 +08002307 }
2308 break;
2309 default:
2310 break;
2311 }
2312 return 0;
2313}
2314
2315static struct notifier_block thermal_pm_nb = {
2316 .notifier_call = thermal_pm_notify,
2317};
2318
Zhang Rui203d3d42008-01-17 15:51:08 +08002319static int __init thermal_init(void)
2320{
Zhang Rui80a26a52013-03-26 16:38:29 +08002321 int result;
2322
2323 result = thermal_register_governors();
2324 if (result)
2325 goto error;
Zhang Rui203d3d42008-01-17 15:51:08 +08002326
2327 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08002328 if (result)
2329 goto unregister_governors;
2330
R.Durgadoss4cb18722010-10-27 03:33:29 +05302331 result = genetlink_init();
Zhang Rui80a26a52013-03-26 16:38:29 +08002332 if (result)
2333 goto unregister_class;
2334
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002335 result = of_parse_thermal_zones();
2336 if (result)
2337 goto exit_netlink;
2338
Zhang Ruiff140fe2015-10-30 16:31:58 +08002339 result = register_pm_notifier(&thermal_pm_nb);
2340 if (result)
2341 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
2342 result);
2343
Zhang Rui80a26a52013-03-26 16:38:29 +08002344 return 0;
2345
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002346exit_netlink:
2347 genetlink_exit();
Zhang Rui80a26a52013-03-26 16:38:29 +08002348unregister_class:
2349 class_unregister(&thermal_class);
Luis Henriques9d367e52014-12-03 21:20:21 +00002350unregister_governors:
2351 thermal_unregister_governors();
Zhang Rui80a26a52013-03-26 16:38:29 +08002352error:
2353 idr_destroy(&thermal_tz_idr);
2354 idr_destroy(&thermal_cdev_idr);
2355 mutex_destroy(&thermal_idr_lock);
2356 mutex_destroy(&thermal_list_lock);
2357 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08002358 return result;
2359}
2360
2361static void __exit thermal_exit(void)
2362{
Zhang Ruiff140fe2015-10-30 16:31:58 +08002363 unregister_pm_notifier(&thermal_pm_nb);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002364 of_thermal_destroy_zones();
Zhang Rui80a26a52013-03-26 16:38:29 +08002365 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08002366 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08002367 thermal_unregister_governors();
Zhang Rui203d3d42008-01-17 15:51:08 +08002368 idr_destroy(&thermal_tz_idr);
2369 idr_destroy(&thermal_cdev_idr);
2370 mutex_destroy(&thermal_idr_lock);
2371 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08002372 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08002373}
2374
R.Durgadoss4cb18722010-10-27 03:33:29 +05302375fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08002376module_exit(thermal_exit);