blob: 5b6475a8804e6725d9c58e44d324efa28a4e5ad6 [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
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -060052#define THERMAL_MAX_ACTIVE 16
53
Zhang Rui203d3d42008-01-17 15:51:08 +080054static DEFINE_IDR(thermal_tz_idr);
55static DEFINE_IDR(thermal_cdev_idr);
56static DEFINE_MUTEX(thermal_idr_lock);
57
58static LIST_HEAD(thermal_tz_list);
59static LIST_HEAD(thermal_cdev_list);
Durgadoss Ra4a15482012-09-18 11:04:57 +053060static LIST_HEAD(thermal_governor_list);
61
Zhang Rui203d3d42008-01-17 15:51:08 +080062static DEFINE_MUTEX(thermal_list_lock);
Durgadoss Ra4a15482012-09-18 11:04:57 +053063static DEFINE_MUTEX(thermal_governor_lock);
64
Zhang Ruiff140fe2015-10-30 16:31:58 +080065static atomic_t in_suspend;
66
Zhang Ruif2234bc2014-01-24 10:23:19 +080067static struct thermal_governor *def_governor;
68
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -060069static struct workqueue_struct *thermal_passive_wq;
70
Durgadoss Ra4a15482012-09-18 11:04:57 +053071static struct thermal_governor *__find_governor(const char *name)
72{
73 struct thermal_governor *pos;
74
Zhang Ruif2234bc2014-01-24 10:23:19 +080075 if (!name || !name[0])
76 return def_governor;
77
Durgadoss Ra4a15482012-09-18 11:04:57 +053078 list_for_each_entry(pos, &thermal_governor_list, governor_list)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -070079 if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
Durgadoss Ra4a15482012-09-18 11:04:57 +053080 return pos;
81
82 return NULL;
83}
84
Javi Merinoe33df1d2015-02-26 19:00:27 +000085/**
86 * bind_previous_governor() - bind the previous governor of the thermal zone
87 * @tz: a valid pointer to a struct thermal_zone_device
88 * @failed_gov_name: the name of the governor that failed to register
89 *
90 * Register the previous governor of the thermal zone after a new
91 * governor has failed to be bound.
92 */
93static void bind_previous_governor(struct thermal_zone_device *tz,
94 const char *failed_gov_name)
95{
96 if (tz->governor && tz->governor->bind_to_tz) {
97 if (tz->governor->bind_to_tz(tz)) {
98 dev_err(&tz->device,
99 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
100 failed_gov_name, tz->governor->name, tz->type);
101 tz->governor = NULL;
102 }
103 }
104}
105
106/**
107 * thermal_set_governor() - Switch to another governor
108 * @tz: a valid pointer to a struct thermal_zone_device
109 * @new_gov: pointer to the new governor
110 *
111 * Change the governor of thermal zone @tz.
112 *
113 * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
114 */
115static int thermal_set_governor(struct thermal_zone_device *tz,
116 struct thermal_governor *new_gov)
117{
118 int ret = 0;
119
120 if (tz->governor && tz->governor->unbind_from_tz)
121 tz->governor->unbind_from_tz(tz);
122
123 if (new_gov && new_gov->bind_to_tz) {
124 ret = new_gov->bind_to_tz(tz);
125 if (ret) {
126 bind_previous_governor(tz, new_gov->name);
127
128 return ret;
129 }
130 }
131
132 tz->governor = new_gov;
133
134 return ret;
135}
136
Durgadoss Ra4a15482012-09-18 11:04:57 +0530137int thermal_register_governor(struct thermal_governor *governor)
138{
139 int err;
140 const char *name;
141 struct thermal_zone_device *pos;
142
143 if (!governor)
144 return -EINVAL;
145
146 mutex_lock(&thermal_governor_lock);
147
148 err = -EBUSY;
149 if (__find_governor(governor->name) == NULL) {
150 err = 0;
151 list_add(&governor->governor_list, &thermal_governor_list);
Zhang Ruif2234bc2014-01-24 10:23:19 +0800152 if (!def_governor && !strncmp(governor->name,
153 DEFAULT_THERMAL_GOVERNOR, THERMAL_NAME_LENGTH))
154 def_governor = governor;
Durgadoss Ra4a15482012-09-18 11:04:57 +0530155 }
156
157 mutex_lock(&thermal_list_lock);
158
159 list_for_each_entry(pos, &thermal_tz_list, node) {
Zhang Ruif2234bc2014-01-24 10:23:19 +0800160 /*
161 * only thermal zones with specified tz->tzp->governor_name
162 * may run with tz->govenor unset
163 */
Durgadoss Ra4a15482012-09-18 11:04:57 +0530164 if (pos->governor)
165 continue;
Zhang Ruif2234bc2014-01-24 10:23:19 +0800166
167 name = pos->tzp->governor_name;
168
Javi Merinoe33df1d2015-02-26 19:00:27 +0000169 if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {
170 int ret;
171
172 ret = thermal_set_governor(pos, governor);
173 if (ret)
174 dev_err(&pos->device,
175 "Failed to set governor %s for thermal zone %s: %d\n",
176 governor->name, pos->type, ret);
177 }
Durgadoss Ra4a15482012-09-18 11:04:57 +0530178 }
179
180 mutex_unlock(&thermal_list_lock);
181 mutex_unlock(&thermal_governor_lock);
182
183 return err;
184}
Durgadoss Ra4a15482012-09-18 11:04:57 +0530185
186void thermal_unregister_governor(struct thermal_governor *governor)
187{
188 struct thermal_zone_device *pos;
189
190 if (!governor)
191 return;
192
193 mutex_lock(&thermal_governor_lock);
194
195 if (__find_governor(governor->name) == NULL)
196 goto exit;
197
198 mutex_lock(&thermal_list_lock);
199
200 list_for_each_entry(pos, &thermal_tz_list, node) {
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -0700201 if (!strncasecmp(pos->governor->name, governor->name,
Durgadoss Ra4a15482012-09-18 11:04:57 +0530202 THERMAL_NAME_LENGTH))
Javi Merinoe33df1d2015-02-26 19:00:27 +0000203 thermal_set_governor(pos, NULL);
Durgadoss Ra4a15482012-09-18 11:04:57 +0530204 }
205
206 mutex_unlock(&thermal_list_lock);
207 list_del(&governor->governor_list);
208exit:
209 mutex_unlock(&thermal_governor_lock);
210 return;
211}
Zhang Rui203d3d42008-01-17 15:51:08 +0800212
213static int get_idr(struct idr *idr, struct mutex *lock, int *id)
214{
Tejun Heo6deb69f2013-02-27 17:04:46 -0800215 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800216
217 if (lock)
218 mutex_lock(lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800219 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800220 if (lock)
221 mutex_unlock(lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800222 if (unlikely(ret < 0))
223 return ret;
224 *id = ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800225 return 0;
226}
227
228static void release_idr(struct idr *idr, struct mutex *lock, int id)
229{
230 if (lock)
231 mutex_lock(lock);
232 idr_remove(idr, id);
233 if (lock)
234 mutex_unlock(lock);
235}
236
Durgadoss R9b4298a2012-09-18 11:04:54 +0530237int get_tz_trend(struct thermal_zone_device *tz, int trip)
238{
239 enum thermal_trend trend;
240
Eduardo Valentin0c872502013-05-29 21:37:00 +0000241 if (tz->emul_temperature || !tz->ops->get_trend ||
242 tz->ops->get_trend(tz, trip, &trend)) {
Durgadoss R9b4298a2012-09-18 11:04:54 +0530243 if (tz->temperature > tz->last_temperature)
244 trend = THERMAL_TREND_RAISING;
245 else if (tz->temperature < tz->last_temperature)
246 trend = THERMAL_TREND_DROPPING;
247 else
248 trend = THERMAL_TREND_STABLE;
249 }
250
251 return trend;
252}
253EXPORT_SYMBOL(get_tz_trend);
254
255struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
256 struct thermal_cooling_device *cdev, int trip)
257{
258 struct thermal_instance *pos = NULL;
259 struct thermal_instance *target_instance = NULL;
260
261 mutex_lock(&tz->lock);
262 mutex_lock(&cdev->lock);
263
264 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
265 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
266 target_instance = pos;
267 break;
268 }
269 }
270
271 mutex_unlock(&cdev->lock);
272 mutex_unlock(&tz->lock);
273
274 return target_instance;
275}
276EXPORT_SYMBOL(get_thermal_instance);
277
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530278static void print_bind_err_msg(struct thermal_zone_device *tz,
279 struct thermal_cooling_device *cdev, int ret)
280{
281 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
282 tz->type, cdev->type, ret);
283}
284
285static void __bind(struct thermal_zone_device *tz, int mask,
Eduardo Valentina8892d832013-07-16 15:26:28 -0400286 struct thermal_cooling_device *cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000287 unsigned long *limits,
288 unsigned int weight)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530289{
290 int i, ret;
291
292 for (i = 0; i < tz->trips; i++) {
293 if (mask & (1 << i)) {
Eduardo Valentina8892d832013-07-16 15:26:28 -0400294 unsigned long upper, lower;
295
296 upper = THERMAL_NO_LIMIT;
297 lower = THERMAL_NO_LIMIT;
298 if (limits) {
299 lower = limits[i * 2];
300 upper = limits[i * 2 + 1];
301 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530302 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000303 upper, lower,
304 weight);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530305 if (ret)
306 print_bind_err_msg(tz, cdev, ret);
307 }
308 }
309}
310
311static void __unbind(struct thermal_zone_device *tz, int mask,
312 struct thermal_cooling_device *cdev)
313{
314 int i;
315
316 for (i = 0; i < tz->trips; i++)
317 if (mask & (1 << i))
318 thermal_zone_unbind_cooling_device(tz, i, cdev);
319}
320
321static void bind_cdev(struct thermal_cooling_device *cdev)
322{
323 int i, ret;
324 const struct thermal_zone_params *tzp;
325 struct thermal_zone_device *pos = NULL;
326
327 mutex_lock(&thermal_list_lock);
328
329 list_for_each_entry(pos, &thermal_tz_list, node) {
330 if (!pos->tzp && !pos->ops->bind)
331 continue;
332
Ni Wadea9f2d192013-11-06 14:30:13 +0800333 if (pos->ops->bind) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530334 ret = pos->ops->bind(pos, cdev);
335 if (ret)
336 print_bind_err_msg(pos, cdev, ret);
Ni Wadea9f2d192013-11-06 14:30:13 +0800337 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530338 }
339
340 tzp = pos->tzp;
Hugh Dickins791700c2012-09-27 16:48:28 -0700341 if (!tzp || !tzp->tbp)
342 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530343
344 for (i = 0; i < tzp->num_tbps; i++) {
345 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
346 continue;
347 if (tzp->tbp[i].match(pos, cdev))
348 continue;
349 tzp->tbp[i].cdev = cdev;
Eduardo Valentina8892d832013-07-16 15:26:28 -0400350 __bind(pos, tzp->tbp[i].trip_mask, cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000351 tzp->tbp[i].binding_limits,
352 tzp->tbp[i].weight);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530353 }
354 }
355
356 mutex_unlock(&thermal_list_lock);
357}
358
359static void bind_tz(struct thermal_zone_device *tz)
360{
361 int i, ret;
362 struct thermal_cooling_device *pos = NULL;
363 const struct thermal_zone_params *tzp = tz->tzp;
364
365 if (!tzp && !tz->ops->bind)
366 return;
367
368 mutex_lock(&thermal_list_lock);
369
Ni Wadea9f2d192013-11-06 14:30:13 +0800370 /* If there is ops->bind, try to use ops->bind */
371 if (tz->ops->bind) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530372 list_for_each_entry(pos, &thermal_cdev_list, node) {
373 ret = tz->ops->bind(tz, pos);
374 if (ret)
375 print_bind_err_msg(tz, pos, ret);
376 }
377 goto exit;
378 }
379
Hugh Dickins791700c2012-09-27 16:48:28 -0700380 if (!tzp || !tzp->tbp)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530381 goto exit;
382
383 list_for_each_entry(pos, &thermal_cdev_list, node) {
384 for (i = 0; i < tzp->num_tbps; i++) {
385 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
386 continue;
387 if (tzp->tbp[i].match(tz, pos))
388 continue;
389 tzp->tbp[i].cdev = pos;
Eduardo Valentina8892d832013-07-16 15:26:28 -0400390 __bind(tz, tzp->tbp[i].trip_mask, pos,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000391 tzp->tbp[i].binding_limits,
392 tzp->tbp[i].weight);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530393 }
394 }
395exit:
396 mutex_unlock(&thermal_list_lock);
397}
398
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -0600399static void thermal_zone_device_set_polling(struct workqueue_struct *queue,
400 struct thermal_zone_device *tz,
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530401 int delay)
402{
403 if (delay > 1000)
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -0600404 mod_delayed_work(queue, &tz->poll_queue,
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530405 round_jiffies(msecs_to_jiffies(delay)));
406 else if (delay)
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -0600407 mod_delayed_work(queue, &tz->poll_queue,
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530408 msecs_to_jiffies(delay));
409 else
Wei Wang1634c192019-11-14 21:08:52 +0530410 cancel_delayed_work(&tz->poll_queue);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530411}
412
413static void monitor_thermal_zone(struct thermal_zone_device *tz)
414{
415 mutex_lock(&tz->lock);
416
417 if (tz->passive)
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -0600418 thermal_zone_device_set_polling(thermal_passive_wq,
419 tz, tz->passive_delay);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530420 else if (tz->polling_delay)
Ram Chandrasekarb17d98c2017-06-23 10:41:23 -0600421 thermal_zone_device_set_polling(
422 system_freezable_power_efficient_wq,
423 tz, tz->polling_delay);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530424 else
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -0600425 thermal_zone_device_set_polling(NULL, tz, 0);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530426
427 mutex_unlock(&tz->lock);
428}
429
430static void handle_non_critical_trips(struct thermal_zone_device *tz,
431 int trip, enum thermal_trip_type trip_type)
432{
Zhang Ruif2234bc2014-01-24 10:23:19 +0800433 tz->governor ? tz->governor->throttle(tz, trip) :
434 def_governor->throttle(tz, trip);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530435}
436
437static void handle_critical_trips(struct thermal_zone_device *tz,
438 int trip, enum thermal_trip_type trip_type)
439{
Sascha Hauer17e83512015-07-24 08:12:54 +0200440 int trip_temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530441
442 tz->ops->get_trip_temp(tz, trip, &trip_temp);
443
444 /* If we have not crossed the trip_temp, we do not care. */
Srinivas Pandruvada84ffe3e2014-11-12 15:43:29 -0800445 if (trip_temp <= 0 || tz->temperature < trip_temp)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530446 return;
447
Ram Chandrasekar5b1c0d12017-03-28 11:35:40 -0600448 trace_thermal_zone_trip(tz, trip, trip_type, true);
Punit Agrawal208cd822014-07-29 11:50:50 +0100449
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530450 if (tz->ops->notify)
451 tz->ops->notify(tz, trip, trip_type);
452
453 if (trip_type == THERMAL_TRIP_CRITICAL) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000454 dev_emerg(&tz->device,
455 "critical temperature reached(%d C),shutting down\n",
456 tz->temperature / 1000);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530457 orderly_poweroff(true);
458 }
459}
460
Ram Chandrasekarbae93a62018-11-02 12:23:40 -0600461static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530462{
463 enum thermal_trip_type type;
464
Zhang Rui81ad4272016-03-18 10:03:24 +0800465 /* Ignore disabled trip points */
466 if (test_bit(trip, &tz->trips_disabled))
467 return;
468
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530469 tz->ops->get_trip_type(tz, trip, &type);
470
471 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
472 handle_critical_trips(tz, trip, type);
473 else
474 handle_non_critical_trips(tz, trip, type);
475 /*
476 * Alright, we handled this trip successfully.
477 * So, start monitoring again.
478 */
479 monitor_thermal_zone(tz);
Ram Chandrasekar4185d52d2017-07-13 17:27:50 -0600480 trace_thermal_handle_trip(tz, trip);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530481}
482
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000483/**
Sascha Hauerf6be0582015-07-06 09:46:14 +0200484 * thermal_zone_get_temp() - returns the temperature of a thermal zone
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000485 * @tz: a valid pointer to a struct thermal_zone_device
486 * @temp: a valid pointer to where to store the resulting temperature.
487 *
488 * When a valid thermal zone reference is passed, it will fetch its
489 * temperature and fill @temp.
490 *
491 * Return: On success returns 0, an error code otherwise
492 */
Sascha Hauer17e83512015-07-24 08:12:54 +0200493int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000494{
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000495 int ret = -EINVAL;
Zhang Rui5e20b2e2013-02-06 14:02:12 +0800496 int count;
Sascha Hauer17e83512015-07-24 08:12:54 +0200497 int crit_temp = INT_MAX;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000498 enum thermal_trip_type type;
499
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400500 if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000501 goto exit;
502
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000503 mutex_lock(&tz->lock);
504
505 ret = tz->ops->get_temp(tz, temp);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000506
Sascha Hauer79e54212015-07-06 09:46:16 +0200507 if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
508 for (count = 0; count < tz->trips; count++) {
509 ret = tz->ops->get_trip_type(tz, count, &type);
510 if (!ret && type == THERMAL_TRIP_CRITICAL) {
511 ret = tz->ops->get_trip_temp(tz, count,
512 &crit_temp);
513 break;
514 }
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000515 }
Sascha Hauer79e54212015-07-06 09:46:16 +0200516
Sascha Hauer934c93b2015-07-06 09:46:17 +0200517 /*
518 * Only allow emulating a temperature when the real temperature
519 * is below the critical temperature so that the emulation code
520 * cannot hide critical conditions.
521 */
Sascha Hauer79e54212015-07-06 09:46:16 +0200522 if (!ret && *temp < crit_temp)
523 *temp = tz->emul_temperature;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000524 }
Ram Chandrasekarc64569d2017-09-26 17:31:18 -0600525 trace_thermal_query_temp(tz, *temp);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000526 mutex_unlock(&tz->lock);
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000527exit:
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000528 return ret;
529}
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000530EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000531
Sascha Hauer060c0342016-06-22 16:42:01 +0800532void thermal_zone_set_trips(struct thermal_zone_device *tz)
533{
534 int low = -INT_MAX;
535 int high = INT_MAX;
536 int trip_temp, hysteresis;
537 int i, ret;
538
539 mutex_lock(&tz->lock);
540
541 if (!tz->ops->set_trips || !tz->ops->get_trip_hyst)
542 goto exit;
543
544 for (i = 0; i < tz->trips; i++) {
545 int trip_low;
546
547 tz->ops->get_trip_temp(tz, i, &trip_temp);
548 tz->ops->get_trip_hyst(tz, i, &hysteresis);
549
550 trip_low = trip_temp - hysteresis;
551
552 if (trip_low < tz->temperature && trip_low > low)
553 low = trip_low;
554
555 if (trip_temp > tz->temperature && trip_temp < high)
556 high = trip_temp;
557 }
558
Sascha Hauer060c0342016-06-22 16:42:01 +0800559 tz->prev_low_trip = low;
560 tz->prev_high_trip = high;
561
562 dev_dbg(&tz->device,
563 "new temperature boundaries: %d < x < %d\n", low, high);
564
565 /*
566 * Set a temperature window. When this window is left the driver
567 * must inform the thermal core via thermal_zone_device_update.
568 */
569 ret = tz->ops->set_trips(tz, low, high);
570 if (ret)
571 dev_err(&tz->device, "Failed to set trips: %d\n", ret);
Ram Chandrasekar4185d52d2017-07-13 17:27:50 -0600572 trace_thermal_set_trip(tz);
Sascha Hauer060c0342016-06-22 16:42:01 +0800573
574exit:
575 mutex_unlock(&tz->lock);
576}
577EXPORT_SYMBOL_GPL(thermal_zone_set_trips);
578
Ram Chandrasekarbae93a62018-11-02 12:23:40 -0600579static void store_temperature(struct thermal_zone_device *tz, int temp)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530580{
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000581 mutex_lock(&tz->lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530582 tz->last_temperature = tz->temperature;
583 tz->temperature = temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530584 mutex_unlock(&tz->lock);
Aaron Lu06475b52013-12-02 13:54:26 +0800585
Punit Agrawal100a8fd2014-07-29 11:50:48 +0100586 trace_thermal_temperature(tz);
Ram Chandrasekar37c816d2017-08-25 12:34:02 -0600587 if (tz->last_temperature == THERMAL_TEMP_INVALID ||
588 tz->last_temperature == THERMAL_TEMP_INVALID_LOW)
Zhang Ruibb431ba2015-10-30 16:31:47 +0800589 dev_dbg(&tz->device, "last_temperature N/A, current_temperature=%d\n",
590 tz->temperature);
591 else
592 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
593 tz->last_temperature, tz->temperature);
594}
595
Ram Chandrasekarbae93a62018-11-02 12:23:40 -0600596static void update_temperature(struct thermal_zone_device *tz)
597{
598 int temp, ret;
599
600 ret = thermal_zone_get_temp(tz, &temp);
601 if (ret) {
602 if (ret != -EAGAIN)
603 dev_warn(&tz->device,
604 "failed to read out thermal zone (%d)\n",
605 ret);
606 return;
607 }
608 store_temperature(tz, temp);
609}
610
Wei Wanga61cdb82018-11-07 14:36:11 -0800611static void thermal_zone_device_init(struct thermal_zone_device *tz)
Zhang Ruibb431ba2015-10-30 16:31:47 +0800612{
613 struct thermal_instance *pos;
Zhang Ruibb431ba2015-10-30 16:31:47 +0800614 tz->temperature = THERMAL_TEMP_INVALID;
Zhang Ruibb431ba2015-10-30 16:31:47 +0800615 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
616 pos->initialized = false;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530617}
618
Wei Wanga61cdb82018-11-07 14:36:11 -0800619static void thermal_zone_device_reset(struct thermal_zone_device *tz)
620{
621 tz->passive = 0;
622 thermal_zone_device_init(tz);
623}
624
Ram Chandrasekarbae93a62018-11-02 12:23:40 -0600625void thermal_zone_device_update_temp(struct thermal_zone_device *tz,
626 enum thermal_notify_event event, int temp)
627{
628 int count;
629
Manaf Meethalavalappu Pallikunhi11175e32019-02-14 18:03:00 +0530630 if (atomic_read(&in_suspend) && (!tz->ops->is_wakeable ||
631 !(tz->ops->is_wakeable(tz))))
Ram Chandrasekarbae93a62018-11-02 12:23:40 -0600632 return;
633
634 trace_thermal_device_update(tz, event);
635 store_temperature(tz, temp);
636
637 thermal_zone_set_trips(tz);
638
639 tz->notify_event = event;
640
641 for (count = 0; count < tz->trips; count++)
642 handle_thermal_trip(tz, count);
643}
644EXPORT_SYMBOL(thermal_zone_device_update_temp);
645
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700646void thermal_zone_device_update(struct thermal_zone_device *tz,
647 enum thermal_notify_event event)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530648{
649 int count;
650
Manaf Meethalavalappu Pallikunhi11175e32019-02-14 18:03:00 +0530651 if (atomic_read(&in_suspend) && (!tz->ops->is_wakeable ||
652 !(tz->ops->is_wakeable(tz))))
Zhang Ruiff140fe2015-10-30 16:31:58 +0800653 return;
654
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400655 if (!tz->ops->get_temp)
656 return;
657
Ram Chandrasekar4185d52d2017-07-13 17:27:50 -0600658 trace_thermal_device_update(tz, event);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530659 update_temperature(tz);
660
Sascha Hauer060c0342016-06-22 16:42:01 +0800661 thermal_zone_set_trips(tz);
662
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700663 tz->notify_event = event;
664
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530665 for (count = 0; count < tz->trips; count++)
666 handle_thermal_trip(tz, count);
667}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000668EXPORT_SYMBOL_GPL(thermal_zone_device_update);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530669
670static void thermal_zone_device_check(struct work_struct *work)
671{
672 struct thermal_zone_device *tz = container_of(work, struct
673 thermal_zone_device,
674 poll_queue.work);
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700675 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530676}
677
Zhang Rui203d3d42008-01-17 15:51:08 +0800678/* sys I/F for thermal zone */
679
680#define to_thermal_zone(_dev) \
681 container_of(_dev, struct thermal_zone_device, device)
682
683static ssize_t
684type_show(struct device *dev, struct device_attribute *attr, char *buf)
685{
686 struct thermal_zone_device *tz = to_thermal_zone(dev);
687
688 return sprintf(buf, "%s\n", tz->type);
689}
690
691static ssize_t
692temp_show(struct device *dev, struct device_attribute *attr, char *buf)
693{
694 struct thermal_zone_device *tz = to_thermal_zone(dev);
Sascha Hauer17e83512015-07-24 08:12:54 +0200695 int temperature, ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800696
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000697 ret = thermal_zone_get_temp(tz, &temperature);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000698
699 if (ret)
700 return ret;
701
Sascha Hauer17e83512015-07-24 08:12:54 +0200702 return sprintf(buf, "%d\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800703}
704
705static ssize_t
706mode_show(struct device *dev, struct device_attribute *attr, char *buf)
707{
708 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000709 enum thermal_device_mode mode;
710 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800711
712 if (!tz->ops->get_mode)
713 return -EPERM;
714
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000715 result = tz->ops->get_mode(tz, &mode);
716 if (result)
717 return result;
718
719 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
720 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800721}
722
Ram Chandrasekar37c816d2017-08-25 12:34:02 -0600723static int thermal_zone_device_clear(struct thermal_zone_device *tz)
724{
725 struct thermal_instance *pos;
726 int ret = 0;
727
728 ret = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
729 mutex_lock(&tz->lock);
730 thermal_zone_device_reset(tz);
731 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
732 pos->target = THERMAL_NO_TARGET;
733 mutex_lock(&pos->cdev->lock);
734 pos->cdev->updated = false; /* cdev needs update */
735 mutex_unlock(&pos->cdev->lock);
736 thermal_cdev_update(pos->cdev);
737 }
738 mutex_unlock(&tz->lock);
739
740 return ret;
741}
742
Zhang Rui203d3d42008-01-17 15:51:08 +0800743static ssize_t
744mode_store(struct device *dev, struct device_attribute *attr,
745 const char *buf, size_t count)
746{
747 struct thermal_zone_device *tz = to_thermal_zone(dev);
748 int result;
749
750 if (!tz->ops->set_mode)
751 return -EPERM;
752
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530753 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000754 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530755 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Ram Chandrasekar37c816d2017-08-25 12:34:02 -0600756 result = thermal_zone_device_clear(tz);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000757 else
758 result = -EINVAL;
759
Zhang Rui203d3d42008-01-17 15:51:08 +0800760 if (result)
761 return result;
762
763 return count;
764}
765
766static ssize_t
767trip_point_type_show(struct device *dev, struct device_attribute *attr,
768 char *buf)
769{
770 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000771 enum thermal_trip_type type;
772 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800773
774 if (!tz->ops->get_trip_type)
775 return -EPERM;
776
777 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
778 return -EINVAL;
779
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000780 result = tz->ops->get_trip_type(tz, trip, &type);
781 if (result)
782 return result;
783
784 switch (type) {
785 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300786 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000787 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300788 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000789 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300790 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000791 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300792 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000793 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300794 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000795 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800796}
797
798static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800799trip_point_temp_store(struct device *dev, struct device_attribute *attr,
800 const char *buf, size_t count)
801{
802 struct thermal_zone_device *tz = to_thermal_zone(dev);
803 int trip, ret;
Wei Ni1d0fd422016-03-03 17:33:46 +0800804 int temperature;
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800805
806 if (!tz->ops->set_trip_temp)
807 return -EPERM;
808
809 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
810 return -EINVAL;
811
Wei Ni1d0fd422016-03-03 17:33:46 +0800812 if (kstrtoint(buf, 10, &temperature))
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800813 return -EINVAL;
814
815 ret = tz->ops->set_trip_temp(tz, trip, temperature);
Kuninori Morimotoad74e462015-12-15 01:19:53 +0000816 if (ret)
817 return ret;
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800818
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700819 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Kuninori Morimotoad74e462015-12-15 01:19:53 +0000820
821 return count;
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800822}
823
824static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800825trip_point_temp_show(struct device *dev, struct device_attribute *attr,
826 char *buf)
827{
828 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000829 int trip, ret;
Sascha Hauer17e83512015-07-24 08:12:54 +0200830 int temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800831
832 if (!tz->ops->get_trip_temp)
833 return -EPERM;
834
835 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
836 return -EINVAL;
837
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000838 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
839
840 if (ret)
841 return ret;
842
Sascha Hauer17e83512015-07-24 08:12:54 +0200843 return sprintf(buf, "%d\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800844}
845
Matthew Garrett03a971a2008-12-03 18:00:38 +0000846static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800847trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
848 const char *buf, size_t count)
849{
850 struct thermal_zone_device *tz = to_thermal_zone(dev);
851 int trip, ret;
Sascha Hauer17e83512015-07-24 08:12:54 +0200852 int temperature;
Durgadoss R27365a62012-07-25 10:10:59 +0800853
854 if (!tz->ops->set_trip_hyst)
855 return -EPERM;
856
857 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
858 return -EINVAL;
859
Sascha Hauer17e83512015-07-24 08:12:54 +0200860 if (kstrtoint(buf, 10, &temperature))
Durgadoss R27365a62012-07-25 10:10:59 +0800861 return -EINVAL;
862
863 /*
864 * We are not doing any check on the 'temperature' value
865 * here. The driver implementing 'set_trip_hyst' has to
866 * take care of this.
867 */
868 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
869
Ram Chandrasekarb0363192017-04-13 14:24:24 -0600870 thermal_zone_set_trips(tz);
Sascha Hauer060c0342016-06-22 16:42:01 +0800871
Durgadoss R27365a62012-07-25 10:10:59 +0800872 return ret ? ret : count;
873}
874
875static ssize_t
876trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
877 char *buf)
878{
879 struct thermal_zone_device *tz = to_thermal_zone(dev);
880 int trip, ret;
Sascha Hauer17e83512015-07-24 08:12:54 +0200881 int temperature;
Durgadoss R27365a62012-07-25 10:10:59 +0800882
883 if (!tz->ops->get_trip_hyst)
884 return -EPERM;
885
886 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
887 return -EINVAL;
888
889 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
890
Sascha Hauer17e83512015-07-24 08:12:54 +0200891 return ret ? ret : sprintf(buf, "%d\n", temperature);
Durgadoss R27365a62012-07-25 10:10:59 +0800892}
893
894static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000895passive_store(struct device *dev, struct device_attribute *attr,
896 const char *buf, size_t count)
897{
898 struct thermal_zone_device *tz = to_thermal_zone(dev);
899 struct thermal_cooling_device *cdev = NULL;
900 int state;
901
902 if (!sscanf(buf, "%d\n", &state))
903 return -EINVAL;
904
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100905 /* sanity check: values below 1000 millicelcius don't make sense
906 * and can cause the system to go into a thermal heart attack
907 */
908 if (state && state < 1000)
909 return -EINVAL;
910
Matthew Garrett03a971a2008-12-03 18:00:38 +0000911 if (state && !tz->forced_passive) {
912 mutex_lock(&thermal_list_lock);
913 list_for_each_entry(cdev, &thermal_cdev_list, node) {
914 if (!strncmp("Processor", cdev->type,
915 sizeof("Processor")))
916 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800917 THERMAL_TRIPS_NONE, cdev,
918 THERMAL_NO_LIMIT,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000919 THERMAL_NO_LIMIT,
920 THERMAL_WEIGHT_DEFAULT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000921 }
922 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100923 if (!tz->passive_delay)
924 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000925 } else if (!state && tz->forced_passive) {
926 mutex_lock(&thermal_list_lock);
927 list_for_each_entry(cdev, &thermal_cdev_list, node) {
928 if (!strncmp("Processor", cdev->type,
929 sizeof("Processor")))
930 thermal_zone_unbind_cooling_device(tz,
931 THERMAL_TRIPS_NONE,
932 cdev);
933 }
934 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100935 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000936 }
937
Matthew Garrett03a971a2008-12-03 18:00:38 +0000938 tz->forced_passive = state;
939
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700940 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000941
942 return count;
943}
944
945static ssize_t
946passive_show(struct device *dev, struct device_attribute *attr,
947 char *buf)
948{
949 struct thermal_zone_device *tz = to_thermal_zone(dev);
950
951 return sprintf(buf, "%d\n", tz->forced_passive);
952}
953
Durgadoss R5a2c0902012-09-18 11:04:58 +0530954static ssize_t
Ram Chandrasekardc814ca2017-07-10 15:23:47 -0600955polling_delay_show(struct device *dev, struct device_attribute *attr,
956 char *buf)
957{
958 struct thermal_zone_device *tz = to_thermal_zone(dev);
959
960 return snprintf(buf, PAGE_SIZE, "%d\n", tz->polling_delay);
961}
962
963static ssize_t
964polling_delay_store(struct device *dev, struct device_attribute *attr,
965 const char *buf, size_t count)
966{
967 struct thermal_zone_device *tz = to_thermal_zone(dev);
968 int delay;
969
970 if (kstrtoint(buf, 10, &delay))
971 return -EINVAL;
972
973 mutex_lock(&tz->lock);
974 tz->polling_delay = delay;
975 mutex_unlock(&tz->lock);
976 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
977
978 return count;
979}
980
981static ssize_t
982passive_delay_show(struct device *dev, struct device_attribute *attr,
983 char *buf)
984{
985 struct thermal_zone_device *tz = to_thermal_zone(dev);
986
987 return snprintf(buf, PAGE_SIZE, "%d\n", tz->passive_delay);
988}
989
990static ssize_t
991passive_delay_store(struct device *dev, struct device_attribute *attr,
992 const char *buf, size_t count)
993{
994 struct thermal_zone_device *tz = to_thermal_zone(dev);
995 int delay;
996
997 if (kstrtoint(buf, 10, &delay))
998 return -EINVAL;
999
1000 mutex_lock(&tz->lock);
1001 tz->passive_delay = delay;
1002 mutex_unlock(&tz->lock);
1003 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1004
1005 return count;
1006}
1007
1008static ssize_t
Durgadoss R5a2c0902012-09-18 11:04:58 +05301009policy_store(struct device *dev, struct device_attribute *attr,
1010 const char *buf, size_t count)
1011{
1012 int ret = -EINVAL;
1013 struct thermal_zone_device *tz = to_thermal_zone(dev);
1014 struct thermal_governor *gov;
Andy Shevchenko42a5bf52013-05-17 11:52:02 +00001015 char name[THERMAL_NAME_LENGTH];
1016
1017 snprintf(name, sizeof(name), "%s", buf);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301018
1019 mutex_lock(&thermal_governor_lock);
Javi Merinob6cc7722014-11-25 16:00:33 +00001020 mutex_lock(&tz->lock);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301021
Andy Shevchenko42a5bf52013-05-17 11:52:02 +00001022 gov = __find_governor(strim(name));
Durgadoss R5a2c0902012-09-18 11:04:58 +05301023 if (!gov)
1024 goto exit;
1025
Javi Merinoe33df1d2015-02-26 19:00:27 +00001026 ret = thermal_set_governor(tz, gov);
1027 if (!ret)
1028 ret = count;
Durgadoss R5a2c0902012-09-18 11:04:58 +05301029
1030exit:
Javi Merinob6cc7722014-11-25 16:00:33 +00001031 mutex_unlock(&tz->lock);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301032 mutex_unlock(&thermal_governor_lock);
1033 return ret;
1034}
1035
1036static ssize_t
1037policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
1038{
1039 struct thermal_zone_device *tz = to_thermal_zone(dev);
1040
1041 return sprintf(buf, "%s\n", tz->governor->name);
1042}
1043
Ni Wade25a0a5c2015-07-14 15:40:56 +08001044static ssize_t
1045available_policies_show(struct device *dev, struct device_attribute *devattr,
1046 char *buf)
1047{
1048 struct thermal_governor *pos;
1049 ssize_t count = 0;
1050 ssize_t size = PAGE_SIZE;
1051
1052 mutex_lock(&thermal_governor_lock);
1053
1054 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
1055 size = PAGE_SIZE - count;
1056 count += scnprintf(buf + count, size, "%s ", pos->name);
1057 }
1058 count += scnprintf(buf + count, size, "\n");
1059
1060 mutex_unlock(&thermal_governor_lock);
1061
1062 return count;
1063}
1064
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +00001065static ssize_t
1066emul_temp_store(struct device *dev, struct device_attribute *attr,
1067 const char *buf, size_t count)
1068{
1069 struct thermal_zone_device *tz = to_thermal_zone(dev);
1070 int ret = 0;
Wei Ni1d0fd422016-03-03 17:33:46 +08001071 int temperature;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +00001072
Wei Ni1d0fd422016-03-03 17:33:46 +08001073 if (kstrtoint(buf, 10, &temperature))
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +00001074 return -EINVAL;
1075
1076 if (!tz->ops->set_emul_temp) {
1077 mutex_lock(&tz->lock);
1078 tz->emul_temperature = temperature;
1079 mutex_unlock(&tz->lock);
1080 } else {
1081 ret = tz->ops->set_emul_temp(tz, temperature);
1082 }
1083
lan,Tianyu800744b2014-01-02 15:47:54 +08001084 if (!ret)
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07001085 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
lan,Tianyu800744b2014-01-02 15:47:54 +08001086
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +00001087 return ret ? ret : count;
1088}
1089static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +00001090
Javi Merino9f382712015-03-26 15:53:02 +00001091static ssize_t
1092sustainable_power_show(struct device *dev, struct device_attribute *devattr,
1093 char *buf)
1094{
1095 struct thermal_zone_device *tz = to_thermal_zone(dev);
1096
1097 if (tz->tzp)
1098 return sprintf(buf, "%u\n", tz->tzp->sustainable_power);
1099 else
1100 return -EIO;
1101}
1102
1103static ssize_t
1104sustainable_power_store(struct device *dev, struct device_attribute *devattr,
1105 const char *buf, size_t count)
1106{
1107 struct thermal_zone_device *tz = to_thermal_zone(dev);
1108 u32 sustainable_power;
1109
1110 if (!tz->tzp)
1111 return -EIO;
1112
1113 if (kstrtou32(buf, 10, &sustainable_power))
1114 return -EINVAL;
1115
1116 tz->tzp->sustainable_power = sustainable_power;
1117
1118 return count;
1119}
1120static DEVICE_ATTR(sustainable_power, S_IWUSR | S_IRUGO, sustainable_power_show,
1121 sustainable_power_store);
1122
1123#define create_s32_tzp_attr(name) \
1124 static ssize_t \
1125 name##_show(struct device *dev, struct device_attribute *devattr, \
1126 char *buf) \
1127 { \
1128 struct thermal_zone_device *tz = to_thermal_zone(dev); \
1129 \
1130 if (tz->tzp) \
Leo Yan15333e32016-03-29 19:24:15 +08001131 return sprintf(buf, "%d\n", tz->tzp->name); \
Javi Merino9f382712015-03-26 15:53:02 +00001132 else \
1133 return -EIO; \
1134 } \
1135 \
1136 static ssize_t \
1137 name##_store(struct device *dev, struct device_attribute *devattr, \
1138 const char *buf, size_t count) \
1139 { \
1140 struct thermal_zone_device *tz = to_thermal_zone(dev); \
1141 s32 value; \
1142 \
1143 if (!tz->tzp) \
1144 return -EIO; \
1145 \
1146 if (kstrtos32(buf, 10, &value)) \
1147 return -EINVAL; \
1148 \
1149 tz->tzp->name = value; \
1150 \
1151 return count; \
1152 } \
1153 static DEVICE_ATTR(name, S_IWUSR | S_IRUGO, name##_show, name##_store)
1154
1155create_s32_tzp_attr(k_po);
1156create_s32_tzp_attr(k_pu);
1157create_s32_tzp_attr(k_i);
1158create_s32_tzp_attr(k_d);
1159create_s32_tzp_attr(integral_cutoff);
Eduardo Valentin9d0be7f2015-05-11 19:34:23 -07001160create_s32_tzp_attr(slope);
1161create_s32_tzp_attr(offset);
Javi Merino9f382712015-03-26 15:53:02 +00001162#undef create_s32_tzp_attr
1163
1164static struct device_attribute *dev_tzp_attrs[] = {
1165 &dev_attr_sustainable_power,
1166 &dev_attr_k_po,
1167 &dev_attr_k_pu,
1168 &dev_attr_k_i,
1169 &dev_attr_k_d,
1170 &dev_attr_integral_cutoff,
Eduardo Valentin9d0be7f2015-05-11 19:34:23 -07001171 &dev_attr_slope,
1172 &dev_attr_offset,
Javi Merino9f382712015-03-26 15:53:02 +00001173};
1174
1175static int create_tzp_attrs(struct device *dev)
1176{
1177 int i;
1178
1179 for (i = 0; i < ARRAY_SIZE(dev_tzp_attrs); i++) {
1180 int ret;
1181 struct device_attribute *dev_attr = dev_tzp_attrs[i];
1182
1183 ret = device_create_file(dev, dev_attr);
1184 if (ret)
1185 return ret;
1186 }
1187
1188 return 0;
1189}
1190
Javi Merino35b11d22015-02-26 19:00:28 +00001191/**
1192 * power_actor_get_max_power() - get the maximum power that a cdev can consume
1193 * @cdev: pointer to &thermal_cooling_device
1194 * @tz: a valid thermal zone device pointer
1195 * @max_power: pointer in which to store the maximum power
1196 *
1197 * Calculate the maximum power consumption in milliwats that the
1198 * cooling device can currently consume and store it in @max_power.
1199 *
1200 * Return: 0 on success, -EINVAL if @cdev doesn't support the
1201 * power_actor API or -E* on other error.
1202 */
1203int power_actor_get_max_power(struct thermal_cooling_device *cdev,
1204 struct thermal_zone_device *tz, u32 *max_power)
1205{
1206 if (!cdev_is_power_actor(cdev))
1207 return -EINVAL;
1208
1209 return cdev->ops->state2power(cdev, tz, 0, max_power);
1210}
1211
1212/**
Javi Merinoc973c3b2015-09-14 14:23:50 +01001213 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
1214 * @cdev: pointer to &thermal_cooling_device
1215 * @tz: a valid thermal zone device pointer
1216 * @min_power: pointer in which to store the minimum power
1217 *
1218 * Calculate the minimum power consumption in milliwatts that the
1219 * cooling device can currently consume and store it in @min_power.
1220 *
1221 * Return: 0 on success, -EINVAL if @cdev doesn't support the
1222 * power_actor API or -E* on other error.
1223 */
1224int power_actor_get_min_power(struct thermal_cooling_device *cdev,
1225 struct thermal_zone_device *tz, u32 *min_power)
1226{
1227 unsigned long max_state;
1228 int ret;
1229
1230 if (!cdev_is_power_actor(cdev))
1231 return -EINVAL;
1232
1233 ret = cdev->ops->get_max_state(cdev, &max_state);
1234 if (ret)
1235 return ret;
1236
1237 return cdev->ops->state2power(cdev, tz, max_state, min_power);
1238}
1239
1240/**
Javi Merino35b11d22015-02-26 19:00:28 +00001241 * power_actor_set_power() - limit the maximum power that a cooling device can consume
1242 * @cdev: pointer to &thermal_cooling_device
1243 * @instance: thermal instance to update
1244 * @power: the power in milliwatts
1245 *
1246 * Set the cooling device to consume at most @power milliwatts.
1247 *
1248 * Return: 0 on success, -EINVAL if the cooling device does not
1249 * implement the power actor API or -E* for other failures.
1250 */
1251int power_actor_set_power(struct thermal_cooling_device *cdev,
1252 struct thermal_instance *instance, u32 power)
1253{
1254 unsigned long state;
1255 int ret;
1256
1257 if (!cdev_is_power_actor(cdev))
1258 return -EINVAL;
1259
1260 ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
1261 if (ret)
1262 return ret;
1263
1264 instance->target = state;
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01001265 mutex_lock(&cdev->lock);
Javi Merino35b11d22015-02-26 19:00:28 +00001266 cdev->updated = false;
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01001267 mutex_unlock(&cdev->lock);
Javi Merino35b11d22015-02-26 19:00:28 +00001268 thermal_cdev_update(cdev);
1269
1270 return 0;
1271}
1272
Zhang Rui203d3d42008-01-17 15:51:08 +08001273static DEVICE_ATTR(type, 0444, type_show, NULL);
1274static DEVICE_ATTR(temp, 0444, temp_show, NULL);
1275static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -07001276static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301277static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
Ni Wade25a0a5c2015-07-14 15:40:56 +08001278static DEVICE_ATTR(available_policies, S_IRUGO, available_policies_show, NULL);
Ram Chandrasekardc814ca2017-07-10 15:23:47 -06001279static DEVICE_ATTR(passive_delay, 0644, passive_delay_show,
1280 passive_delay_store);
1281static DEVICE_ATTR(polling_delay, 0644, polling_delay_show,
1282 polling_delay_store);
Zhang Rui203d3d42008-01-17 15:51:08 +08001283
Zhang Rui203d3d42008-01-17 15:51:08 +08001284/* sys I/F for cooling device */
1285#define to_cooling_device(_dev) \
1286 container_of(_dev, struct thermal_cooling_device, device)
1287
1288static ssize_t
1289thermal_cooling_device_type_show(struct device *dev,
1290 struct device_attribute *attr, char *buf)
1291{
1292 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1293
1294 return sprintf(buf, "%s\n", cdev->type);
1295}
1296
1297static ssize_t
1298thermal_cooling_device_max_state_show(struct device *dev,
1299 struct device_attribute *attr, char *buf)
1300{
1301 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001302 unsigned long state;
1303 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +08001304
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001305 ret = cdev->ops->get_max_state(cdev, &state);
1306 if (ret)
1307 return ret;
1308 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +08001309}
1310
1311static ssize_t
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001312thermal_cooling_device_min_state_show(struct device *dev,
1313 struct device_attribute *attr, char *buf)
1314{
1315 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1316 unsigned long state;
1317 int ret;
1318
1319 if (cdev->ops->get_min_state)
1320 ret = cdev->ops->get_min_state(cdev, &state);
1321 else
1322 ret = -EPERM;
1323
1324 if (ret)
1325 return ret;
1326
1327 return snprintf(buf, PAGE_SIZE, "%lu\n", state);
1328}
1329
1330static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +08001331thermal_cooling_device_cur_state_show(struct device *dev,
1332 struct device_attribute *attr, char *buf)
1333{
1334 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001335 unsigned long state;
1336 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +08001337
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001338 ret = cdev->ops->get_cur_state(cdev, &state);
1339 if (ret)
1340 return ret;
1341 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +08001342}
1343
1344static ssize_t
1345thermal_cooling_device_cur_state_store(struct device *dev,
1346 struct device_attribute *attr,
1347 const char *buf, size_t count)
1348{
1349 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Ram Chandrasekar5ab1c082016-09-20 17:09:28 -06001350 long state;
Zhang Rui203d3d42008-01-17 15:51:08 +08001351
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001352 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +08001353 return -EINVAL;
1354
Roel Kluinedb94912009-12-15 22:46:50 +01001355 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +08001356 return -EINVAL;
1357
Ram Chandrasekare30952e2017-11-29 15:31:08 -07001358 mutex_lock(&cdev->lock);
Ram Chandrasekar5ab1c082016-09-20 17:09:28 -06001359 cdev->sysfs_cur_state_req = state;
1360
1361 cdev->updated = false;
Ram Chandrasekare30952e2017-11-29 15:31:08 -07001362 mutex_unlock(&cdev->lock);
Ram Chandrasekar5ab1c082016-09-20 17:09:28 -06001363 thermal_cdev_update(cdev);
1364
Zhang Rui203d3d42008-01-17 15:51:08 +08001365 return count;
1366}
1367
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001368static ssize_t
1369thermal_cooling_device_min_state_store(struct device *dev,
1370 struct device_attribute *attr,
1371 const char *buf, size_t count)
1372{
1373 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1374 long state;
1375 int ret = 0;
1376
1377 ret = sscanf(buf, "%ld\n", &state);
1378 if (ret <= 0)
1379 return (ret < 0) ? ret : -EINVAL;
1380
1381 if ((long)state < 0)
1382 return -EINVAL;
1383
Ram Chandrasekare30952e2017-11-29 15:31:08 -07001384 mutex_lock(&cdev->lock);
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001385 cdev->sysfs_min_state_req = state;
1386
1387 cdev->updated = false;
Ram Chandrasekare30952e2017-11-29 15:31:08 -07001388 mutex_unlock(&cdev->lock);
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001389 thermal_cdev_update(cdev);
1390
1391 return count;
1392}
1393
Zhang Rui203d3d42008-01-17 15:51:08 +08001394static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -05001395__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001396static DEVICE_ATTR(max_state, 0444,
1397 thermal_cooling_device_max_state_show, NULL);
1398static DEVICE_ATTR(cur_state, 0644,
1399 thermal_cooling_device_cur_state_show,
1400 thermal_cooling_device_cur_state_store);
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001401static DEVICE_ATTR(min_state, 0644,
1402 thermal_cooling_device_min_state_show,
1403 thermal_cooling_device_min_state_store);
Zhang Rui203d3d42008-01-17 15:51:08 +08001404
1405static ssize_t
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001406thermal_cooling_device_lower_limit_show(struct device *dev,
1407 struct device_attribute *attr, char *buf)
1408{
1409 struct thermal_instance *instance;
1410
1411 instance =
1412 container_of(attr, struct thermal_instance, lower_attr);
1413
1414 return snprintf(buf, PAGE_SIZE, "%lu\n", instance->lower);
1415}
1416
1417static ssize_t
1418thermal_cooling_device_upper_limit_show(struct device *dev,
1419 struct device_attribute *attr, char *buf)
1420{
1421 struct thermal_instance *instance;
1422
1423 instance =
1424 container_of(attr, struct thermal_instance, upper_attr);
1425
1426 return snprintf(buf, PAGE_SIZE, "%lu\n", instance->upper);
1427}
1428
1429static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +08001430thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -05001431 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +08001432{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001433 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +08001434
1435 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001436 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001437
1438 if (instance->trip == THERMAL_TRIPS_NONE)
1439 return sprintf(buf, "-1\n");
1440 else
1441 return sprintf(buf, "%d\n", instance->trip);
1442}
1443
Ram Chandrasekardc814ca2017-07-10 15:23:47 -06001444static ssize_t
1445thermal_cooling_device_upper_limit_store(struct device *dev,
1446 struct device_attribute *attr,
1447 const char *buf, size_t count)
1448{
1449 struct thermal_zone_device *tz = to_thermal_zone(dev);
1450 struct thermal_instance *instance;
1451 int ret, upper_limit;
1452
1453 instance =
1454 container_of(attr, struct thermal_instance, upper_attr);
1455
1456 ret = kstrtoint(buf, 0, &upper_limit);
1457 if (ret)
1458 return ret;
1459 if (upper_limit < instance->lower)
1460 return -EINVAL;
1461
1462 instance->upper = upper_limit;
1463 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1464
1465 return count;
1466}
1467
1468static ssize_t
1469thermal_cooling_device_lower_limit_store(struct device *dev,
1470 struct device_attribute *attr,
1471 const char *buf, size_t count)
1472{
1473 struct thermal_zone_device *tz = to_thermal_zone(dev);
1474 struct thermal_instance *instance;
1475 int ret, lower_limit;
1476
1477 instance =
1478 container_of(attr, struct thermal_instance, lower_attr);
1479
1480 ret = kstrtoint(buf, 0, &lower_limit);
1481 if (ret)
1482 return ret;
1483 if (lower_limit > instance->upper)
1484 return -EINVAL;
1485
1486 instance->lower = lower_limit;
1487 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1488
1489 return count;
1490}
1491
Matthias Kaehlcke2dc10f82015-02-20 18:10:08 -08001492static struct attribute *cooling_device_attrs[] = {
1493 &dev_attr_cdev_type.attr,
1494 &dev_attr_max_state.attr,
1495 &dev_attr_cur_state.attr,
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001496 &dev_attr_min_state.attr,
Matthias Kaehlcke2dc10f82015-02-20 18:10:08 -08001497 NULL,
1498};
1499
1500static const struct attribute_group cooling_device_attr_group = {
1501 .attrs = cooling_device_attrs,
1502};
1503
1504static const struct attribute_group *cooling_device_attr_groups[] = {
1505 &cooling_device_attr_group,
1506 NULL,
1507};
1508
Javi Merinodb916512015-02-18 16:04:24 +00001509static ssize_t
1510thermal_cooling_device_weight_show(struct device *dev,
1511 struct device_attribute *attr, char *buf)
1512{
1513 struct thermal_instance *instance;
1514
1515 instance = container_of(attr, struct thermal_instance, weight_attr);
1516
1517 return sprintf(buf, "%d\n", instance->weight);
1518}
1519
1520static ssize_t
1521thermal_cooling_device_weight_store(struct device *dev,
1522 struct device_attribute *attr,
1523 const char *buf, size_t count)
1524{
1525 struct thermal_instance *instance;
1526 int ret, weight;
1527
1528 ret = kstrtoint(buf, 0, &weight);
1529 if (ret)
1530 return ret;
1531
1532 instance = container_of(attr, struct thermal_instance, weight_attr);
1533 instance->weight = weight;
1534
1535 return count;
1536}
Zhang Rui203d3d42008-01-17 15:51:08 +08001537/* Device management */
1538
1539/**
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001540 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
1541 * @tz: pointer to struct thermal_zone_device
Zhang Rui203d3d42008-01-17 15:51:08 +08001542 * @trip: indicates which trip point the cooling devices is
1543 * associated with in this thermal zone.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001544 * @cdev: pointer to struct thermal_cooling_device
1545 * @upper: the Maximum cooling state for this trip point.
1546 * THERMAL_NO_LIMIT means no upper limit,
1547 * and the cooling device can be in max_state.
1548 * @lower: the Minimum cooling state can be used for this trip point.
1549 * THERMAL_NO_LIMIT means no lower limit,
1550 * and the cooling device can be in cooling state 0.
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001551 * @weight: The weight of the cooling device to be bound to the
1552 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
1553 * default value
Len Brown543a9562008-02-07 16:55:08 -05001554 *
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001555 * This interface function bind a thermal cooling device to the certain trip
1556 * point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001557 * This function is usually called in the thermal zone device .bind callback.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001558 *
1559 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001560 */
1561int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1562 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +08001563 struct thermal_cooling_device *cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001564 unsigned long upper, unsigned long lower,
1565 unsigned int weight)
Zhang Rui203d3d42008-01-17 15:51:08 +08001566{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001567 struct thermal_instance *dev;
1568 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -05001569 struct thermal_zone_device *pos1;
1570 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +08001571 unsigned long max_state;
Lukasz Majewski9a3031d2014-11-18 11:16:30 +01001572 int result, ret;
Zhang Rui203d3d42008-01-17 15:51:08 +08001573
Len Brown543a9562008-02-07 16:55:08 -05001574 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +08001575 return -EINVAL;
1576
Thomas Sujithc7516702008-02-15 00:58:50 -05001577 list_for_each_entry(pos1, &thermal_tz_list, node) {
1578 if (pos1 == tz)
1579 break;
1580 }
1581 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1582 if (pos2 == cdev)
1583 break;
1584 }
1585
1586 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +08001587 return -EINVAL;
1588
Lukasz Majewski9a3031d2014-11-18 11:16:30 +01001589 ret = cdev->ops->get_max_state(cdev, &max_state);
1590 if (ret)
1591 return ret;
Zhang Rui9d998422012-06-26 16:35:57 +08001592
Ram Chandrasekar2a1abd12017-06-12 12:52:08 -06001593 /*
1594 * If upper or lower has a MACRO to define the mitigation state,
1595 * based on the MACRO determine the default state to use or the
1596 * offset from the max_state.
1597 */
Manaf Meethalavalappu Pallikunhicd3ed412018-04-16 17:16:41 +05301598 if (upper >= (THERMAL_MAX_LIMIT - max_state)) {
Ram Chandrasekar2a1abd12017-06-12 12:52:08 -06001599 /* upper default max_state */
1600 if (upper == THERMAL_NO_LIMIT)
1601 upper = max_state;
1602 else
1603 upper = max_state - (THERMAL_MAX_LIMIT - upper);
1604 }
1605
Manaf Meethalavalappu Pallikunhicd3ed412018-04-16 17:16:41 +05301606 if (lower >= (THERMAL_MAX_LIMIT - max_state)) {
Ram Chandrasekar2a1abd12017-06-12 12:52:08 -06001607 /* lower default 0 */
1608 if (lower == THERMAL_NO_LIMIT)
1609 lower = 0;
1610 else
1611 lower = max_state - (THERMAL_MAX_LIMIT - lower);
1612 }
Zhang Rui9d998422012-06-26 16:35:57 +08001613
1614 if (lower > upper || upper > max_state)
1615 return -EINVAL;
1616
Zhang Rui203d3d42008-01-17 15:51:08 +08001617 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001618 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001619 if (!dev)
1620 return -ENOMEM;
1621 dev->tz = tz;
1622 dev->cdev = cdev;
1623 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +08001624 dev->upper = upper;
1625 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +08001626 dev->target = THERMAL_NO_TARGET;
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001627 dev->weight = weight;
Zhang Rui74051ba2012-06-26 16:27:22 +08001628
Zhang Rui203d3d42008-01-17 15:51:08 +08001629 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1630 if (result)
1631 goto free_mem;
1632
1633 sprintf(dev->name, "cdev%d", dev->id);
1634 result =
1635 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1636 if (result)
1637 goto release_idr;
1638
1639 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -07001640 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001641 dev->attr.attr.name = dev->attr_name;
1642 dev->attr.attr.mode = 0444;
1643 dev->attr.show = thermal_cooling_device_trip_point_show;
1644 result = device_create_file(&tz->device, &dev->attr);
1645 if (result)
1646 goto remove_symbol_link;
1647
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001648 snprintf(dev->upper_attr_name, THERMAL_NAME_LENGTH,
1649 "cdev%d_upper_limit", dev->id);
1650 sysfs_attr_init(&dev->upper_attr.attr);
1651 dev->upper_attr.attr.name = dev->upper_attr_name;
Ram Chandrasekardc814ca2017-07-10 15:23:47 -06001652 dev->upper_attr.attr.mode = 0644;
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001653 dev->upper_attr.show = thermal_cooling_device_upper_limit_show;
Ram Chandrasekardc814ca2017-07-10 15:23:47 -06001654 dev->upper_attr.store = thermal_cooling_device_upper_limit_store;
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001655 result = device_create_file(&tz->device, &dev->upper_attr);
1656 if (result)
1657 goto remove_trip_file;
1658
1659 snprintf(dev->lower_attr_name, THERMAL_NAME_LENGTH,
1660 "cdev%d_lower_limit", dev->id);
1661 sysfs_attr_init(&dev->lower_attr.attr);
1662 dev->lower_attr.attr.name = dev->lower_attr_name;
Ram Chandrasekardc814ca2017-07-10 15:23:47 -06001663 dev->lower_attr.attr.mode = 0644;
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001664 dev->lower_attr.show = thermal_cooling_device_lower_limit_show;
Ram Chandrasekardc814ca2017-07-10 15:23:47 -06001665 dev->lower_attr.store = thermal_cooling_device_lower_limit_store;
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001666 result = device_create_file(&tz->device, &dev->lower_attr);
1667 if (result)
1668 goto remove_upper_file;
1669
Javi Merinodb916512015-02-18 16:04:24 +00001670 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
1671 sysfs_attr_init(&dev->weight_attr.attr);
1672 dev->weight_attr.attr.name = dev->weight_attr_name;
1673 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
1674 dev->weight_attr.show = thermal_cooling_device_weight_show;
1675 dev->weight_attr.store = thermal_cooling_device_weight_store;
1676 result = device_create_file(&tz->device, &dev->weight_attr);
1677 if (result)
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001678 goto remove_lower_file;
Javi Merinodb916512015-02-18 16:04:24 +00001679
Zhang Rui203d3d42008-01-17 15:51:08 +08001680 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001681 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001682 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +08001683 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1684 result = -EEXIST;
1685 break;
1686 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001687 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001688 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001689 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
Chen Yu4511f712015-10-30 16:32:10 +08001690 atomic_set(&tz->need_update, 1);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001691 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001692 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001693 mutex_unlock(&tz->lock);
1694
1695 if (!result)
1696 return 0;
1697
Javi Merinodb916512015-02-18 16:04:24 +00001698 device_remove_file(&tz->device, &dev->weight_attr);
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001699remove_lower_file:
1700 device_remove_file(&tz->device, &dev->lower_attr);
1701remove_upper_file:
1702 device_remove_file(&tz->device, &dev->upper_attr);
Javi Merinodb916512015-02-18 16:04:24 +00001703remove_trip_file:
Zhang Rui203d3d42008-01-17 15:51:08 +08001704 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -07001705remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +08001706 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -07001707release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +08001708 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -07001709free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +08001710 kfree(dev);
1711 return result;
1712}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001713EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001714
1715/**
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001716 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1717 * thermal zone.
1718 * @tz: pointer to a struct thermal_zone_device.
Zhang Rui203d3d42008-01-17 15:51:08 +08001719 * @trip: indicates which trip point the cooling devices is
1720 * associated with in this thermal zone.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001721 * @cdev: pointer to a struct thermal_cooling_device.
Len Brown543a9562008-02-07 16:55:08 -05001722 *
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001723 * This interface function unbind a thermal cooling device from the certain
1724 * trip point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001725 * This function is usually called in the thermal zone device .unbind callback.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001726 *
1727 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001728 */
1729int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1730 int trip,
1731 struct thermal_cooling_device *cdev)
1732{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001733 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +08001734
1735 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001736 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001737 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -05001738 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001739 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001740 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001741 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001742 mutex_unlock(&tz->lock);
1743 goto unbind;
1744 }
1745 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001746 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001747 mutex_unlock(&tz->lock);
1748
1749 return -ENODEV;
1750
Joe Perchescaca8b82012-03-21 12:55:02 -07001751unbind:
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001752 device_remove_file(&tz->device, &pos->lower_attr);
1753 device_remove_file(&tz->device, &pos->upper_attr);
Viresh Kumar528464e2015-07-23 14:32:32 +05301754 device_remove_file(&tz->device, &pos->weight_attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001755 device_remove_file(&tz->device, &pos->attr);
1756 sysfs_remove_link(&tz->device.kobj, pos->name);
1757 release_idr(&tz->idr, &tz->lock, pos->id);
1758 kfree(pos);
1759 return 0;
1760}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001761EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001762
1763static void thermal_release(struct device *dev)
1764{
1765 struct thermal_zone_device *tz;
1766 struct thermal_cooling_device *cdev;
1767
Joe Perchescaca8b82012-03-21 12:55:02 -07001768 if (!strncmp(dev_name(dev), "thermal_zone",
1769 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001770 tz = to_thermal_zone(dev);
1771 kfree(tz);
durgadoss.r@intel.com732e4c82013-10-02 00:08:00 +05301772 } else if(!strncmp(dev_name(dev), "cooling_device",
1773 sizeof("cooling_device") - 1)){
Zhang Rui203d3d42008-01-17 15:51:08 +08001774 cdev = to_cooling_device(dev);
1775 kfree(cdev);
1776 }
1777}
1778
1779static struct class thermal_class = {
1780 .name = "thermal",
1781 .dev_release = thermal_release,
1782};
1783
1784/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001785 * __thermal_cooling_device_register() - register a new thermal cooling device
1786 * @np: a pointer to a device tree node.
Zhang Rui203d3d42008-01-17 15:51:08 +08001787 * @type: the thermal cooling device type.
1788 * @devdata: device private data.
1789 * @ops: standard thermal cooling devices callbacks.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001790 *
1791 * This interface function adds a new thermal cooling device (fan/processor/...)
1792 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1793 * to all the thermal zone devices registered at the same time.
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001794 * It also gives the opportunity to link the cooling device to a device tree
1795 * node, so that it can be bound to a thermal zone created out of device tree.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001796 *
1797 * Return: a pointer to the created struct thermal_cooling_device or an
1798 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001799 */
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001800static struct thermal_cooling_device *
1801__thermal_cooling_device_register(struct device_node *np,
1802 char *type, void *devdata,
1803 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001804{
1805 struct thermal_cooling_device *cdev;
Chen Yu4511f712015-10-30 16:32:10 +08001806 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001807 int result;
1808
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001809 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001810 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001811
1812 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001813 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001814 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001815
1816 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1817 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001818 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001819
1820 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1821 if (result) {
1822 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001823 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001824 }
1825
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001826 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +08001827 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001828 INIT_LIST_HEAD(&cdev->thermal_instances);
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001829 cdev->np = np;
Zhang Rui203d3d42008-01-17 15:51:08 +08001830 cdev->ops = ops;
Ni Wade5ca0cce2014-02-17 11:02:55 +08001831 cdev->updated = false;
Zhang Rui203d3d42008-01-17 15:51:08 +08001832 cdev->device.class = &thermal_class;
Matthias Kaehlcke2dc10f82015-02-20 18:10:08 -08001833 cdev->device.groups = cooling_device_attr_groups;
Zhang Rui203d3d42008-01-17 15:51:08 +08001834 cdev->devdata = devdata;
Ram Chandrasekar5ab1c082016-09-20 17:09:28 -06001835 cdev->sysfs_cur_state_req = 0;
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001836 cdev->sysfs_min_state_req = ULONG_MAX;
Kay Sievers354655e2009-01-06 10:44:37 -08001837 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001838 result = device_register(&cdev->device);
1839 if (result) {
1840 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1841 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001842 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001843 }
1844
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301845 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +08001846 mutex_lock(&thermal_list_lock);
1847 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001848 mutex_unlock(&thermal_list_lock);
1849
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301850 /* Update binding information for 'this' new cdev */
1851 bind_cdev(cdev);
1852
Chen Yu4511f712015-10-30 16:32:10 +08001853 mutex_lock(&thermal_list_lock);
1854 list_for_each_entry(pos, &thermal_tz_list, node)
1855 if (atomic_cmpxchg(&pos->need_update, 1, 0))
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07001856 thermal_zone_device_update(pos,
1857 THERMAL_EVENT_UNSPECIFIED);
Chen Yu4511f712015-10-30 16:32:10 +08001858 mutex_unlock(&thermal_list_lock);
1859
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301860 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001861}
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001862
1863/**
1864 * thermal_cooling_device_register() - register a new thermal cooling device
1865 * @type: the thermal cooling device type.
1866 * @devdata: device private data.
1867 * @ops: standard thermal cooling devices callbacks.
1868 *
1869 * This interface function adds a new thermal cooling device (fan/processor/...)
1870 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1871 * to all the thermal zone devices registered at the same time.
1872 *
1873 * Return: a pointer to the created struct thermal_cooling_device or an
1874 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1875 */
1876struct thermal_cooling_device *
1877thermal_cooling_device_register(char *type, void *devdata,
1878 const struct thermal_cooling_device_ops *ops)
1879{
1880 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1881}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001882EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001883
1884/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001885 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1886 * @np: a pointer to a device tree node.
1887 * @type: the thermal cooling device type.
1888 * @devdata: device private data.
1889 * @ops: standard thermal cooling devices callbacks.
1890 *
1891 * This function will register a cooling device with device tree node reference.
1892 * This interface function adds a new thermal cooling device (fan/processor/...)
1893 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1894 * to all the thermal zone devices registered at the same time.
1895 *
1896 * Return: a pointer to the created struct thermal_cooling_device or an
1897 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1898 */
1899struct thermal_cooling_device *
1900thermal_of_cooling_device_register(struct device_node *np,
1901 char *type, void *devdata,
1902 const struct thermal_cooling_device_ops *ops)
1903{
1904 return __thermal_cooling_device_register(np, type, devdata, ops);
1905}
1906EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1907
1908/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001909 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001910 * @cdev: the thermal cooling device to remove.
1911 *
1912 * thermal_cooling_device_unregister() must be called when the device is no
1913 * longer needed.
1914 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301915void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001916{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301917 int i;
1918 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001919 struct thermal_zone_device *tz;
1920 struct thermal_cooling_device *pos = NULL;
1921
1922 if (!cdev)
1923 return;
1924
1925 mutex_lock(&thermal_list_lock);
1926 list_for_each_entry(pos, &thermal_cdev_list, node)
1927 if (pos == cdev)
1928 break;
1929 if (pos != cdev) {
1930 /* thermal cooling device not found */
1931 mutex_unlock(&thermal_list_lock);
1932 return;
1933 }
1934 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301935
1936 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001937 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301938 if (tz->ops->unbind) {
1939 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001940 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301941 }
1942
1943 if (!tz->tzp || !tz->tzp->tbp)
1944 continue;
1945
1946 tzp = tz->tzp;
1947 for (i = 0; i < tzp->num_tbps; i++) {
1948 if (tzp->tbp[i].cdev == cdev) {
1949 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1950 tzp->tbp[i].cdev = NULL;
1951 }
1952 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001953 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301954
Zhang Rui203d3d42008-01-17 15:51:08 +08001955 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301956
Zhang Rui203d3d42008-01-17 15:51:08 +08001957 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001958 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001959 device_remove_file(&cdev->device, &dev_attr_max_state);
1960 device_remove_file(&cdev->device, &dev_attr_cur_state);
1961
1962 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1963 device_unregister(&cdev->device);
1964 return;
1965}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001966EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001967
Durgadoss Rdc765482012-09-18 11:05:00 +05301968void thermal_cdev_update(struct thermal_cooling_device *cdev)
Zhang Ruice119f82012-06-27 14:13:04 +08001969{
1970 struct thermal_instance *instance;
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001971 unsigned long current_target = 0, min_target = ULONG_MAX;
Zhang Ruice119f82012-06-27 14:13:04 +08001972
Zhang Ruif4a821c2012-07-24 16:56:21 +08001973 mutex_lock(&cdev->lock);
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01001974 /* cooling device is updated*/
1975 if (cdev->updated) {
1976 mutex_unlock(&cdev->lock);
1977 return;
1978 }
1979
Zhang Ruice119f82012-06-27 14:13:04 +08001980 /* Make sure cdev enters the deepest cooling state */
Ram Chandrasekar5ab1c082016-09-20 17:09:28 -06001981 current_target = cdev->sysfs_cur_state_req;
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001982 min_target = cdev->sysfs_min_state_req;
Zhang Ruice119f82012-06-27 14:13:04 +08001983 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
Aaron Lu06475b52013-12-02 13:54:26 +08001984 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1985 instance->tz->id, instance->target);
Zhang Ruice119f82012-06-27 14:13:04 +08001986 if (instance->target == THERMAL_NO_TARGET)
1987 continue;
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001988 if (instance->tz->governor->min_state_throttle) {
1989 if (instance->target < min_target)
1990 min_target = instance->target;
1991 } else {
1992 if (instance->target > current_target)
1993 current_target = instance->target;
1994 }
Zhang Ruice119f82012-06-27 14:13:04 +08001995 }
Ram Chandrasekar4185d52d2017-07-13 17:27:50 -06001996 trace_cdev_update_start(cdev);
Ram Chandrasekar5ab1c082016-09-20 17:09:28 -06001997 cdev->ops->set_cur_state(cdev, current_target);
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001998 if (cdev->ops->set_min_state)
1999 cdev->ops->set_min_state(cdev, min_target);
Zhang Ruice119f82012-06-27 14:13:04 +08002000 cdev->updated = true;
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01002001 mutex_unlock(&cdev->lock);
Ram Chandrasekar5b1c0d12017-03-28 11:35:40 -06002002 trace_cdev_update(cdev, current_target, min_target);
Ram Chandrasekarbd155702017-03-17 16:42:15 -06002003 dev_dbg(&cdev->device, "set to state %lu min state %lu\n",
2004 current_target, min_target);
Zhang Ruice119f82012-06-27 14:13:04 +08002005}
Durgadoss Rdc765482012-09-18 11:05:00 +05302006EXPORT_SYMBOL(thermal_cdev_update);
Zhang Ruice119f82012-06-27 14:13:04 +08002007
Matthew Garrettb1569e92008-12-03 17:55:32 +00002008/**
Eduardo Valentin7b73c992013-04-23 21:48:14 +00002009 * thermal_notify_framework - Sensor drivers use this API to notify framework
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05302010 * @tz: thermal zone device
2011 * @trip: indicates which trip point has been crossed
2012 *
2013 * This function handles the trip events from sensor drivers. It starts
2014 * throttling the cooling devices according to the policy configured.
2015 * For CRITICAL and HOT trip points, this notifies the respective drivers,
2016 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
2017 * The throttling policy is based on the configured platform data; if no
2018 * platform data is provided, this uses the step_wise throttling policy.
2019 */
Eduardo Valentin7b73c992013-04-23 21:48:14 +00002020void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05302021{
2022 handle_thermal_trip(tz, trip);
2023}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00002024EXPORT_SYMBOL_GPL(thermal_notify_framework);
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05302025
2026/**
Eduardo Valentin269c1742013-04-23 21:48:19 +00002027 * create_trip_attrs() - create attributes for trip points
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002028 * @tz: the thermal zone device
2029 * @mask: Writeable trip point bitmap.
Eduardo Valentin269c1742013-04-23 21:48:19 +00002030 *
2031 * helper function to instantiate sysfs entries for every trip
2032 * point and its properties of a struct thermal_zone_device.
2033 *
2034 * Return: 0 on success, the proper error value otherwise.
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002035 */
2036static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
2037{
2038 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08002039 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002040
Durgadoss R27365a62012-07-25 10:10:59 +08002041 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002042 if (!tz->trip_type_attrs)
2043 return -ENOMEM;
2044
Durgadoss R27365a62012-07-25 10:10:59 +08002045 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002046 if (!tz->trip_temp_attrs) {
2047 kfree(tz->trip_type_attrs);
2048 return -ENOMEM;
2049 }
2050
Durgadoss R27365a62012-07-25 10:10:59 +08002051 if (tz->ops->get_trip_hyst) {
2052 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
2053 if (!tz->trip_hyst_attrs) {
2054 kfree(tz->trip_type_attrs);
2055 kfree(tz->trip_temp_attrs);
2056 return -ENOMEM;
2057 }
2058 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002059
Durgadoss R27365a62012-07-25 10:10:59 +08002060
2061 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002062 /* create trip type attribute */
2063 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
2064 "trip_point_%d_type", indx);
2065
2066 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
2067 tz->trip_type_attrs[indx].attr.attr.name =
2068 tz->trip_type_attrs[indx].name;
2069 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
2070 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
2071
2072 device_create_file(&tz->device,
2073 &tz->trip_type_attrs[indx].attr);
2074
2075 /* create trip temp attribute */
2076 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
2077 "trip_point_%d_temp", indx);
2078
2079 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
2080 tz->trip_temp_attrs[indx].attr.attr.name =
2081 tz->trip_temp_attrs[indx].name;
2082 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
2083 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
Punit Agrawal35e94642015-03-03 10:43:03 +00002084 if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS) &&
2085 mask & (1 << indx)) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002086 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
2087 tz->trip_temp_attrs[indx].attr.store =
2088 trip_point_temp_store;
2089 }
2090
2091 device_create_file(&tz->device,
2092 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08002093
2094 /* create Optional trip hyst attribute */
2095 if (!tz->ops->get_trip_hyst)
2096 continue;
2097 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
2098 "trip_point_%d_hyst", indx);
2099
2100 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
2101 tz->trip_hyst_attrs[indx].attr.attr.name =
2102 tz->trip_hyst_attrs[indx].name;
2103 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
2104 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
2105 if (tz->ops->set_trip_hyst) {
2106 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
2107 tz->trip_hyst_attrs[indx].attr.store =
2108 trip_point_hyst_store;
2109 }
2110
2111 device_create_file(&tz->device,
2112 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002113 }
2114 return 0;
2115}
2116
2117static void remove_trip_attrs(struct thermal_zone_device *tz)
2118{
2119 int indx;
2120
2121 for (indx = 0; indx < tz->trips; indx++) {
2122 device_remove_file(&tz->device,
2123 &tz->trip_type_attrs[indx].attr);
2124 device_remove_file(&tz->device,
2125 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08002126 if (tz->ops->get_trip_hyst)
2127 device_remove_file(&tz->device,
2128 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002129 }
2130 kfree(tz->trip_type_attrs);
2131 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08002132 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002133}
2134
2135/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00002136 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08002137 * @type: the thermal zone device type
2138 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002139 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08002140 * @devdata: private device data
2141 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05302142 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00002143 * @passive_delay: number of milliseconds to wait between polls when
2144 * performing passive cooling
2145 * @polling_delay: number of milliseconds to wait between polls when checking
2146 * whether trip points have been crossed (0 for interrupt
2147 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08002148 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00002149 * This interface function adds a new thermal zone device (sensor) to
2150 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
2151 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08002152 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08002153 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00002154 *
2155 * Return: a pointer to the created struct thermal_zone_device or an
2156 * in case of error, an ERR_PTR. Caller must check return value with
2157 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08002158 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07002159struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002160 int trips, int mask, void *devdata,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002161 struct thermal_zone_device_ops *ops,
Javi Merino6b775e82015-03-02 17:17:19 +00002162 struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08002163 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08002164{
2165 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00002166 enum thermal_trip_type trip_type;
Zhang Rui81ad4272016-03-18 10:03:24 +08002167 int trip_temp;
Zhang Rui203d3d42008-01-17 15:51:08 +08002168 int result;
2169 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00002170 int passive = 0;
Javi Merinoe33df1d2015-02-26 19:00:27 +00002171 struct thermal_governor *governor;
Zhang Rui203d3d42008-01-17 15:51:08 +08002172
Guenter Roeck204dd1d2012-08-07 22:36:45 -07002173 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002174 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08002175
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002176 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002177 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08002178
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04002179 if (!ops)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002180 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08002181
Jonghwa Lee83720d02013-05-18 09:50:26 +00002182 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00002183 return ERR_PTR(-EINVAL);
2184
Zhang Rui203d3d42008-01-17 15:51:08 +08002185 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
2186 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002187 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08002188
Zhang Rui2d374132012-06-27 10:09:00 +08002189 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08002190 idr_init(&tz->idr);
2191 mutex_init(&tz->lock);
2192 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
2193 if (result) {
2194 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002195 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08002196 }
2197
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00002198 strlcpy(tz->type, type ? : "", sizeof(tz->type));
Zhang Rui203d3d42008-01-17 15:51:08 +08002199 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05302200 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08002201 tz->device.class = &thermal_class;
2202 tz->devdata = devdata;
2203 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00002204 tz->passive_delay = passive_delay;
2205 tz->polling_delay = polling_delay;
Chen Yu4511f712015-10-30 16:32:10 +08002206 /* A new thermal zone needs to be updated anyway. */
2207 atomic_set(&tz->need_update, 1);
Matthew Garrettb1569e92008-12-03 17:55:32 +00002208
Kay Sievers354655e2009-01-06 10:44:37 -08002209 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08002210 result = device_register(&tz->device);
2211 if (result) {
2212 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
2213 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002214 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08002215 }
2216
2217 /* sys I/F */
2218 if (type) {
2219 result = device_create_file(&tz->device, &dev_attr_type);
2220 if (result)
2221 goto unregister;
2222 }
2223
2224 result = device_create_file(&tz->device, &dev_attr_temp);
2225 if (result)
2226 goto unregister;
2227
2228 if (ops->get_mode) {
2229 result = device_create_file(&tz->device, &dev_attr_mode);
2230 if (result)
2231 goto unregister;
2232 }
2233
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002234 result = create_trip_attrs(tz, mask);
2235 if (result)
2236 goto unregister;
2237
Zhang Rui203d3d42008-01-17 15:51:08 +08002238 for (count = 0; count < trips; count++) {
Zhang Rui81ad4272016-03-18 10:03:24 +08002239 if (tz->ops->get_trip_type(tz, count, &trip_type))
2240 set_bit(count, &tz->trips_disabled);
Matthew Garrett03a971a2008-12-03 18:00:38 +00002241 if (trip_type == THERMAL_TRIP_PASSIVE)
2242 passive = 1;
Zhang Rui81ad4272016-03-18 10:03:24 +08002243 if (tz->ops->get_trip_temp(tz, count, &trip_temp))
2244 set_bit(count, &tz->trips_disabled);
2245 /* Check for bogus trip points */
2246 if (trip_temp == 0)
2247 set_bit(count, &tz->trips_disabled);
Zhang Rui203d3d42008-01-17 15:51:08 +08002248 }
2249
Durgadoss R5a2c0902012-09-18 11:04:58 +05302250 if (!passive) {
2251 result = device_create_file(&tz->device, &dev_attr_passive);
2252 if (result)
2253 goto unregister;
2254 }
Ram Chandrasekardc814ca2017-07-10 15:23:47 -06002255 result = device_create_file(&tz->device, &dev_attr_passive_delay);
2256 if (result)
2257 goto unregister;
2258
2259 result = device_create_file(&tz->device, &dev_attr_polling_delay);
2260 if (result)
2261 goto unregister;
Matthew Garrett03a971a2008-12-03 18:00:38 +00002262
Sascha Hauer79e54212015-07-06 09:46:16 +02002263 if (IS_ENABLED(CONFIG_THERMAL_EMULATION)) {
2264 result = device_create_file(&tz->device, &dev_attr_emul_temp);
2265 if (result)
2266 goto unregister;
2267 }
2268
Durgadoss R5a2c0902012-09-18 11:04:58 +05302269 /* Create policy attribute */
2270 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00002271 if (result)
2272 goto unregister;
2273
Javi Merino9f382712015-03-26 15:53:02 +00002274 /* Add thermal zone params */
2275 result = create_tzp_attrs(&tz->device);
2276 if (result)
2277 goto unregister;
2278
Ni Wade25a0a5c2015-07-14 15:40:56 +08002279 /* Create available_policies attribute */
2280 result = device_create_file(&tz->device, &dev_attr_available_policies);
2281 if (result)
2282 goto unregister;
2283
Durgadoss Ra4a15482012-09-18 11:04:57 +05302284 /* Update 'this' zone's governor information */
2285 mutex_lock(&thermal_governor_lock);
2286
2287 if (tz->tzp)
Javi Merinoe33df1d2015-02-26 19:00:27 +00002288 governor = __find_governor(tz->tzp->governor_name);
Durgadoss Ra4a15482012-09-18 11:04:57 +05302289 else
Javi Merinoe33df1d2015-02-26 19:00:27 +00002290 governor = def_governor;
2291
2292 result = thermal_set_governor(tz, governor);
2293 if (result) {
2294 mutex_unlock(&thermal_governor_lock);
2295 goto unregister;
2296 }
Durgadoss Ra4a15482012-09-18 11:04:57 +05302297
2298 mutex_unlock(&thermal_governor_lock);
2299
Eduardo Valentinccba4ff2013-08-15 11:34:17 -04002300 if (!tz->tzp || !tz->tzp->no_hwmon) {
2301 result = thermal_add_hwmon_sysfs(tz);
2302 if (result)
2303 goto unregister;
2304 }
Zhang Ruie68b16a2008-04-21 16:07:52 +08002305
Zhang Rui203d3d42008-01-17 15:51:08 +08002306 mutex_lock(&thermal_list_lock);
2307 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08002308 mutex_unlock(&thermal_list_lock);
2309
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05302310 /* Bind cooling devices for this zone */
2311 bind_tz(tz);
2312
Ram Chandrasekarb17d98c2017-06-23 10:41:23 -06002313 INIT_DEFERRABLE_WORK(&(tz->poll_queue), thermal_zone_device_check);
Matthew Garrettb1569e92008-12-03 17:55:32 +00002314
Zhang Ruibb431ba2015-10-30 16:31:47 +08002315 thermal_zone_device_reset(tz);
Chen Yu4511f712015-10-30 16:32:10 +08002316 /* Update the new thermal zone and mark it as already updated. */
2317 if (atomic_cmpxchg(&tz->need_update, 1, 0))
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07002318 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Matthew Garrettb1569e92008-12-03 17:55:32 +00002319
Yao Dongdong14015862014-10-28 07:40:25 +00002320 return tz;
Zhang Rui203d3d42008-01-17 15:51:08 +08002321
Joe Perchescaca8b82012-03-21 12:55:02 -07002322unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08002323 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
2324 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002325 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08002326}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00002327EXPORT_SYMBOL_GPL(thermal_zone_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08002328
2329/**
2330 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08002331 * @tz: the thermal zone device to remove
2332 */
2333void thermal_zone_device_unregister(struct thermal_zone_device *tz)
2334{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05302335 int i;
2336 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08002337 struct thermal_cooling_device *cdev;
2338 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08002339
2340 if (!tz)
2341 return;
2342
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05302343 tzp = tz->tzp;
2344
Zhang Rui203d3d42008-01-17 15:51:08 +08002345 mutex_lock(&thermal_list_lock);
2346 list_for_each_entry(pos, &thermal_tz_list, node)
2347 if (pos == tz)
2348 break;
2349 if (pos != tz) {
2350 /* thermal zone device not found */
2351 mutex_unlock(&thermal_list_lock);
2352 return;
2353 }
2354 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05302355
2356 /* Unbind all cdevs associated with 'this' thermal zone */
2357 list_for_each_entry(cdev, &thermal_cdev_list, node) {
2358 if (tz->ops->unbind) {
2359 tz->ops->unbind(tz, cdev);
2360 continue;
2361 }
2362
2363 if (!tzp || !tzp->tbp)
2364 break;
2365
2366 for (i = 0; i < tzp->num_tbps; i++) {
2367 if (tzp->tbp[i].cdev == cdev) {
2368 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
2369 tzp->tbp[i].cdev = NULL;
2370 }
2371 }
2372 }
2373
Zhang Rui203d3d42008-01-17 15:51:08 +08002374 mutex_unlock(&thermal_list_lock);
2375
Wei Wang1634c192019-11-14 21:08:52 +05302376 cancel_delayed_work_sync(&tz->poll_queue);
Matthew Garrettb1569e92008-12-03 17:55:32 +00002377
Zhang Rui203d3d42008-01-17 15:51:08 +08002378 if (tz->type[0])
2379 device_remove_file(&tz->device, &dev_attr_type);
2380 device_remove_file(&tz->device, &dev_attr_temp);
2381 if (tz->ops->get_mode)
2382 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05302383 device_remove_file(&tz->device, &dev_attr_policy);
Ni Wade25a0a5c2015-07-14 15:40:56 +08002384 device_remove_file(&tz->device, &dev_attr_available_policies);
Ram Chandrasekardc814ca2017-07-10 15:23:47 -06002385 device_remove_file(&tz->device, &dev_attr_passive_delay);
2386 device_remove_file(&tz->device, &dev_attr_polling_delay);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002387 remove_trip_attrs(tz);
Javi Merinoe33df1d2015-02-26 19:00:27 +00002388 thermal_set_governor(tz, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08002389
Zhang Ruie68b16a2008-04-21 16:07:52 +08002390 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08002391 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
2392 idr_destroy(&tz->idr);
2393 mutex_destroy(&tz->lock);
2394 device_unregister(&tz->device);
2395 return;
2396}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00002397EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08002398
Eduardo Valentin63c4d912013-04-05 12:32:28 +00002399/**
2400 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
2401 * @name: thermal zone name to fetch the temperature
2402 *
2403 * When only one zone is found with the passed name, returns a reference to it.
2404 *
2405 * Return: On success returns a reference to an unique thermal zone with
2406 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
2407 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
2408 */
2409struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
2410{
2411 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
2412 unsigned int found = 0;
2413
2414 if (!name)
2415 goto exit;
2416
2417 mutex_lock(&thermal_list_lock);
2418 list_for_each_entry(pos, &thermal_tz_list, node)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -07002419 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
Eduardo Valentin63c4d912013-04-05 12:32:28 +00002420 found++;
2421 ref = pos;
2422 }
2423 mutex_unlock(&thermal_list_lock);
2424
2425 /* nothing has been found, thus an error code for it */
2426 if (found == 0)
2427 ref = ERR_PTR(-ENODEV);
2428 else if (found > 1)
2429 /* Success only when an unique zone is found */
2430 ref = ERR_PTR(-EEXIST);
2431
2432exit:
2433 return ref;
2434}
2435EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
2436
Rajendra Nayak4a7069a2016-05-05 14:21:42 +05302437/**
2438 * thermal_zone_get_slope - return the slope attribute of the thermal zone
2439 * @tz: thermal zone device with the slope attribute
2440 *
2441 * Return: If the thermal zone device has a slope attribute, return it, else
2442 * return 1.
2443 */
2444int thermal_zone_get_slope(struct thermal_zone_device *tz)
2445{
2446 if (tz && tz->tzp)
2447 return tz->tzp->slope;
2448 return 1;
2449}
2450EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
2451
2452/**
2453 * thermal_zone_get_offset - return the offset attribute of the thermal zone
2454 * @tz: thermal zone device with the offset attribute
2455 *
2456 * Return: If the thermal zone device has a offset attribute, return it, else
2457 * return 0.
2458 */
2459int thermal_zone_get_offset(struct thermal_zone_device *tz)
2460{
2461 if (tz && tz->tzp)
2462 return tz->tzp->offset;
2463 return 0;
2464}
2465EXPORT_SYMBOL_GPL(thermal_zone_get_offset);
2466
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002467#ifdef CONFIG_NET
Johannes Berg2a94fe42013-11-19 15:19:39 +01002468static const struct genl_multicast_group thermal_event_mcgrps[] = {
2469 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
2470};
2471
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002472static struct genl_family thermal_event_genl_family = {
2473 .id = GENL_ID_GENERATE,
2474 .name = THERMAL_GENL_FAMILY_NAME,
2475 .version = THERMAL_GENL_VERSION,
2476 .maxattr = THERMAL_GENL_ATTR_MAX,
Johannes Berg2a94fe42013-11-19 15:19:39 +01002477 .mcgrps = thermal_event_mcgrps,
2478 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002479};
2480
Lina Iyer9bf792d2016-07-11 13:27:11 -06002481static int allow_netlink_events;
2482
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00002483int thermal_generate_netlink_event(struct thermal_zone_device *tz,
2484 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05302485{
2486 struct sk_buff *skb;
2487 struct nlattr *attr;
2488 struct thermal_genl_event *thermal_event;
2489 void *msg_header;
2490 int size;
2491 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07002492 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05302493
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00002494 if (!tz)
2495 return -EINVAL;
2496
Lina Iyer9bf792d2016-07-11 13:27:11 -06002497 if (!allow_netlink_events)
2498 return -ENODEV;
2499
R.Durgadoss4cb18722010-10-27 03:33:29 +05302500 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07002501 size = nla_total_size(sizeof(struct thermal_genl_event)) +
2502 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302503
2504 skb = genlmsg_new(size, GFP_ATOMIC);
2505 if (!skb)
2506 return -ENOMEM;
2507
2508 /* add the genetlink message header */
2509 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
2510 &thermal_event_genl_family, 0,
2511 THERMAL_GENL_CMD_EVENT);
2512 if (!msg_header) {
2513 nlmsg_free(skb);
2514 return -ENOMEM;
2515 }
2516
2517 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07002518 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
2519 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05302520
2521 if (!attr) {
2522 nlmsg_free(skb);
2523 return -EINVAL;
2524 }
2525
2526 thermal_event = nla_data(attr);
2527 if (!thermal_event) {
2528 nlmsg_free(skb);
2529 return -EINVAL;
2530 }
2531
2532 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
2533
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00002534 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05302535 thermal_event->event = event;
2536
2537 /* send multicast genetlink message */
Johannes Berg053c0952015-01-16 22:09:00 +01002538 genlmsg_end(skb, msg_header);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302539
Johannes Berg68eb5502013-11-19 15:19:38 +01002540 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01002541 0, GFP_ATOMIC);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302542 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00002543 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302544
2545 return result;
2546}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00002547EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302548
2549static int genetlink_init(void)
2550{
Lina Iyer9bf792d2016-07-11 13:27:11 -06002551 int ret;
2552
2553 ret = genl_register_family(&thermal_event_genl_family);
2554 if (!ret)
2555 allow_netlink_events = true;
2556
2557 return ret;
R.Durgadoss4cb18722010-10-27 03:33:29 +05302558}
2559
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002560static void genetlink_exit(void)
2561{
2562 genl_unregister_family(&thermal_event_genl_family);
2563}
2564#else /* !CONFIG_NET */
2565static inline int genetlink_init(void) { return 0; }
2566static inline void genetlink_exit(void) {}
Lina Iyer9bf792d2016-07-11 13:27:11 -06002567static inline int thermal_generate_netlink_event(struct thermal_zone_device *tz,
2568 enum events event) { return -ENODEV; }
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002569#endif /* !CONFIG_NET */
2570
Zhang Rui80a26a52013-03-26 16:38:29 +08002571static int __init thermal_register_governors(void)
2572{
2573 int result;
2574
2575 result = thermal_gov_step_wise_register();
2576 if (result)
2577 return result;
2578
2579 result = thermal_gov_fair_share_register();
2580 if (result)
2581 return result;
2582
Peter Feuerere4dbf982014-07-22 17:37:13 +02002583 result = thermal_gov_bang_bang_register();
2584 if (result)
2585 return result;
2586
Javi Merino6b775e82015-03-02 17:17:19 +00002587 result = thermal_gov_user_space_register();
2588 if (result)
2589 return result;
2590
Ram Chandrasekar6d44a342016-07-07 11:09:52 -06002591 result = thermal_gov_low_limits_register();
2592 if (result)
2593 return result;
2594
Javi Merino6b775e82015-03-02 17:17:19 +00002595 return thermal_gov_power_allocator_register();
Zhang Rui80a26a52013-03-26 16:38:29 +08002596}
2597
2598static void thermal_unregister_governors(void)
2599{
2600 thermal_gov_step_wise_unregister();
2601 thermal_gov_fair_share_unregister();
Peter Feuerere4dbf982014-07-22 17:37:13 +02002602 thermal_gov_bang_bang_unregister();
Zhang Rui80a26a52013-03-26 16:38:29 +08002603 thermal_gov_user_space_unregister();
Ram Chandrasekar6d44a342016-07-07 11:09:52 -06002604 thermal_gov_low_limits_unregister();
Javi Merino6b775e82015-03-02 17:17:19 +00002605 thermal_gov_power_allocator_unregister();
Zhang Rui80a26a52013-03-26 16:38:29 +08002606}
2607
Zhang Ruiff140fe2015-10-30 16:31:58 +08002608static int thermal_pm_notify(struct notifier_block *nb,
2609 unsigned long mode, void *_unused)
2610{
2611 struct thermal_zone_device *tz;
2612
2613 switch (mode) {
2614 case PM_HIBERNATION_PREPARE:
2615 case PM_RESTORE_PREPARE:
2616 case PM_SUSPEND_PREPARE:
2617 atomic_set(&in_suspend, 1);
2618 break;
2619 case PM_POST_HIBERNATION:
2620 case PM_POST_RESTORE:
2621 case PM_POST_SUSPEND:
2622 atomic_set(&in_suspend, 0);
2623 list_for_each_entry(tz, &thermal_tz_list, node) {
Manaf Meethalavalappu Pallikunhi11175e32019-02-14 18:03:00 +05302624 if (tz->ops->is_wakeable &&
2625 tz->ops->is_wakeable(tz))
2626 continue;
Wei Wanga61cdb82018-11-07 14:36:11 -08002627 thermal_zone_device_init(tz);
Ram Chandrasekar95b4e5d2017-12-13 17:21:26 -07002628 thermal_zone_device_update(tz,
2629 THERMAL_EVENT_UNSPECIFIED);
Zhang Ruiff140fe2015-10-30 16:31:58 +08002630 }
2631 break;
2632 default:
2633 break;
2634 }
2635 return 0;
2636}
2637
2638static struct notifier_block thermal_pm_nb = {
2639 .notifier_call = thermal_pm_notify,
2640};
2641
Zhang Rui203d3d42008-01-17 15:51:08 +08002642static int __init thermal_init(void)
2643{
Zhang Rui80a26a52013-03-26 16:38:29 +08002644 int result;
2645
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -06002646 thermal_passive_wq = alloc_workqueue("thermal_passive_wq",
2647 WQ_HIGHPRI | WQ_UNBOUND
2648 | WQ_FREEZABLE,
2649 THERMAL_MAX_ACTIVE);
2650 if (!thermal_passive_wq) {
2651 result = -ENOMEM;
2652 goto init_exit;
2653 }
2654
Zhang Rui80a26a52013-03-26 16:38:29 +08002655 result = thermal_register_governors();
2656 if (result)
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -06002657 goto destroy_wq;
Zhang Rui203d3d42008-01-17 15:51:08 +08002658
2659 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08002660 if (result)
2661 goto unregister_governors;
2662
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002663 result = of_parse_thermal_zones();
2664 if (result)
Lina Iyer9bf792d2016-07-11 13:27:11 -06002665 goto exit_zone_parse;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002666
Zhang Ruiff140fe2015-10-30 16:31:58 +08002667 result = register_pm_notifier(&thermal_pm_nb);
2668 if (result)
2669 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
2670 result);
2671
Zhang Rui80a26a52013-03-26 16:38:29 +08002672 return 0;
2673
Lina Iyer9bf792d2016-07-11 13:27:11 -06002674exit_zone_parse:
Zhang Rui80a26a52013-03-26 16:38:29 +08002675 class_unregister(&thermal_class);
Luis Henriques9d367e52014-12-03 21:20:21 +00002676unregister_governors:
2677 thermal_unregister_governors();
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -06002678destroy_wq:
2679 destroy_workqueue(thermal_passive_wq);
Lina Iyer9bf792d2016-07-11 13:27:11 -06002680init_exit:
Zhang Rui80a26a52013-03-26 16:38:29 +08002681 idr_destroy(&thermal_tz_idr);
2682 idr_destroy(&thermal_cdev_idr);
2683 mutex_destroy(&thermal_idr_lock);
2684 mutex_destroy(&thermal_list_lock);
2685 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08002686 return result;
2687}
2688
Ram Chandrasekarfe0ae222017-04-06 12:08:38 -06002689static void thermal_exit(void)
Zhang Rui203d3d42008-01-17 15:51:08 +08002690{
Zhang Ruiff140fe2015-10-30 16:31:58 +08002691 unregister_pm_notifier(&thermal_pm_nb);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002692 of_thermal_destroy_zones();
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -06002693 destroy_workqueue(thermal_passive_wq);
Zhang Rui80a26a52013-03-26 16:38:29 +08002694 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08002695 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08002696 thermal_unregister_governors();
Zhang Rui203d3d42008-01-17 15:51:08 +08002697 idr_destroy(&thermal_tz_idr);
2698 idr_destroy(&thermal_cdev_idr);
2699 mutex_destroy(&thermal_idr_lock);
2700 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08002701 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08002702}
2703
Lina Iyer9bf792d2016-07-11 13:27:11 -06002704static int __init thermal_netlink_init(void)
2705{
2706 int ret = 0;
2707
2708 ret = genetlink_init();
2709 if (!ret)
2710 goto exit_netlink;
2711
2712 thermal_exit();
2713exit_netlink:
2714 return ret;
2715}
2716
2717subsys_initcall(thermal_init);
2718fs_initcall(thermal_netlink_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08002719module_exit(thermal_exit);