blob: 7663e3cca3a4cc229172f9f8c5c8101592c27f8b [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
410 cancel_delayed_work(&tz->poll_queue);
411}
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 Chandrasekarcf543602017-03-13 22:59:01 -0600461void 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
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530579static void update_temperature(struct thermal_zone_device *tz)
580{
Sascha Hauer17e83512015-07-24 08:12:54 +0200581 int temp, ret;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530582
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000583 ret = thermal_zone_get_temp(tz, &temp);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530584 if (ret) {
Hans de Goede7e497a72015-03-21 15:02:55 +0100585 if (ret != -EAGAIN)
586 dev_warn(&tz->device,
587 "failed to read out thermal zone (%d)\n",
588 ret);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000589 return;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530590 }
591
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000592 mutex_lock(&tz->lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530593 tz->last_temperature = tz->temperature;
594 tz->temperature = temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530595 mutex_unlock(&tz->lock);
Aaron Lu06475b52013-12-02 13:54:26 +0800596
Punit Agrawal100a8fd2014-07-29 11:50:48 +0100597 trace_thermal_temperature(tz);
Ram Chandrasekar37c816d2017-08-25 12:34:02 -0600598 if (tz->last_temperature == THERMAL_TEMP_INVALID ||
599 tz->last_temperature == THERMAL_TEMP_INVALID_LOW)
Zhang Ruibb431ba2015-10-30 16:31:47 +0800600 dev_dbg(&tz->device, "last_temperature N/A, current_temperature=%d\n",
601 tz->temperature);
602 else
603 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
604 tz->last_temperature, tz->temperature);
605}
606
607static void thermal_zone_device_reset(struct thermal_zone_device *tz)
608{
609 struct thermal_instance *pos;
610
611 tz->temperature = THERMAL_TEMP_INVALID;
612 tz->passive = 0;
613 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
614 pos->initialized = false;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530615}
616
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700617void thermal_zone_device_update(struct thermal_zone_device *tz,
618 enum thermal_notify_event event)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530619{
620 int count;
621
Zhang Ruiff140fe2015-10-30 16:31:58 +0800622 if (atomic_read(&in_suspend))
623 return;
624
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400625 if (!tz->ops->get_temp)
626 return;
627
Ram Chandrasekar4185d52d2017-07-13 17:27:50 -0600628 trace_thermal_device_update(tz, event);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530629 update_temperature(tz);
630
Sascha Hauer060c0342016-06-22 16:42:01 +0800631 thermal_zone_set_trips(tz);
632
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700633 tz->notify_event = event;
634
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530635 for (count = 0; count < tz->trips; count++)
636 handle_thermal_trip(tz, count);
637}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000638EXPORT_SYMBOL_GPL(thermal_zone_device_update);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530639
640static void thermal_zone_device_check(struct work_struct *work)
641{
642 struct thermal_zone_device *tz = container_of(work, struct
643 thermal_zone_device,
644 poll_queue.work);
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700645 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530646}
647
Zhang Rui203d3d42008-01-17 15:51:08 +0800648/* sys I/F for thermal zone */
649
650#define to_thermal_zone(_dev) \
651 container_of(_dev, struct thermal_zone_device, device)
652
653static ssize_t
654type_show(struct device *dev, struct device_attribute *attr, char *buf)
655{
656 struct thermal_zone_device *tz = to_thermal_zone(dev);
657
658 return sprintf(buf, "%s\n", tz->type);
659}
660
661static ssize_t
662temp_show(struct device *dev, struct device_attribute *attr, char *buf)
663{
664 struct thermal_zone_device *tz = to_thermal_zone(dev);
Sascha Hauer17e83512015-07-24 08:12:54 +0200665 int temperature, ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800666
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000667 ret = thermal_zone_get_temp(tz, &temperature);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000668
669 if (ret)
670 return ret;
671
Sascha Hauer17e83512015-07-24 08:12:54 +0200672 return sprintf(buf, "%d\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800673}
674
675static ssize_t
676mode_show(struct device *dev, struct device_attribute *attr, char *buf)
677{
678 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000679 enum thermal_device_mode mode;
680 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800681
682 if (!tz->ops->get_mode)
683 return -EPERM;
684
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000685 result = tz->ops->get_mode(tz, &mode);
686 if (result)
687 return result;
688
689 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
690 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800691}
692
Ram Chandrasekar37c816d2017-08-25 12:34:02 -0600693static int thermal_zone_device_clear(struct thermal_zone_device *tz)
694{
695 struct thermal_instance *pos;
696 int ret = 0;
697
698 ret = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
699 mutex_lock(&tz->lock);
700 thermal_zone_device_reset(tz);
701 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
702 pos->target = THERMAL_NO_TARGET;
703 mutex_lock(&pos->cdev->lock);
704 pos->cdev->updated = false; /* cdev needs update */
705 mutex_unlock(&pos->cdev->lock);
706 thermal_cdev_update(pos->cdev);
707 }
708 mutex_unlock(&tz->lock);
709
710 return ret;
711}
712
Zhang Rui203d3d42008-01-17 15:51:08 +0800713static ssize_t
714mode_store(struct device *dev, struct device_attribute *attr,
715 const char *buf, size_t count)
716{
717 struct thermal_zone_device *tz = to_thermal_zone(dev);
718 int result;
719
720 if (!tz->ops->set_mode)
721 return -EPERM;
722
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530723 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000724 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530725 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Ram Chandrasekar37c816d2017-08-25 12:34:02 -0600726 result = thermal_zone_device_clear(tz);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000727 else
728 result = -EINVAL;
729
Zhang Rui203d3d42008-01-17 15:51:08 +0800730 if (result)
731 return result;
732
733 return count;
734}
735
736static ssize_t
737trip_point_type_show(struct device *dev, struct device_attribute *attr,
738 char *buf)
739{
740 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000741 enum thermal_trip_type type;
742 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800743
744 if (!tz->ops->get_trip_type)
745 return -EPERM;
746
747 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
748 return -EINVAL;
749
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000750 result = tz->ops->get_trip_type(tz, trip, &type);
751 if (result)
752 return result;
753
754 switch (type) {
755 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300756 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000757 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300758 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000759 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300760 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000761 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300762 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000763 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300764 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000765 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800766}
767
768static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800769trip_point_temp_store(struct device *dev, struct device_attribute *attr,
770 const char *buf, size_t count)
771{
772 struct thermal_zone_device *tz = to_thermal_zone(dev);
773 int trip, ret;
Wei Ni1d0fd422016-03-03 17:33:46 +0800774 int temperature;
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800775
776 if (!tz->ops->set_trip_temp)
777 return -EPERM;
778
779 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
780 return -EINVAL;
781
Wei Ni1d0fd422016-03-03 17:33:46 +0800782 if (kstrtoint(buf, 10, &temperature))
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800783 return -EINVAL;
784
785 ret = tz->ops->set_trip_temp(tz, trip, temperature);
Kuninori Morimotoad74e462015-12-15 01:19:53 +0000786 if (ret)
787 return ret;
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800788
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700789 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Kuninori Morimotoad74e462015-12-15 01:19:53 +0000790
791 return count;
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800792}
793
794static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800795trip_point_temp_show(struct device *dev, struct device_attribute *attr,
796 char *buf)
797{
798 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000799 int trip, ret;
Sascha Hauer17e83512015-07-24 08:12:54 +0200800 int temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800801
802 if (!tz->ops->get_trip_temp)
803 return -EPERM;
804
805 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
806 return -EINVAL;
807
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000808 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
809
810 if (ret)
811 return ret;
812
Sascha Hauer17e83512015-07-24 08:12:54 +0200813 return sprintf(buf, "%d\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800814}
815
Matthew Garrett03a971a2008-12-03 18:00:38 +0000816static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800817trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
818 const char *buf, size_t count)
819{
820 struct thermal_zone_device *tz = to_thermal_zone(dev);
821 int trip, ret;
Sascha Hauer17e83512015-07-24 08:12:54 +0200822 int temperature;
Durgadoss R27365a62012-07-25 10:10:59 +0800823
824 if (!tz->ops->set_trip_hyst)
825 return -EPERM;
826
827 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
828 return -EINVAL;
829
Sascha Hauer17e83512015-07-24 08:12:54 +0200830 if (kstrtoint(buf, 10, &temperature))
Durgadoss R27365a62012-07-25 10:10:59 +0800831 return -EINVAL;
832
833 /*
834 * We are not doing any check on the 'temperature' value
835 * here. The driver implementing 'set_trip_hyst' has to
836 * take care of this.
837 */
838 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
839
Ram Chandrasekarb0363192017-04-13 14:24:24 -0600840 thermal_zone_set_trips(tz);
Sascha Hauer060c0342016-06-22 16:42:01 +0800841
Durgadoss R27365a62012-07-25 10:10:59 +0800842 return ret ? ret : count;
843}
844
845static ssize_t
846trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
847 char *buf)
848{
849 struct thermal_zone_device *tz = to_thermal_zone(dev);
850 int trip, ret;
Sascha Hauer17e83512015-07-24 08:12:54 +0200851 int temperature;
Durgadoss R27365a62012-07-25 10:10:59 +0800852
853 if (!tz->ops->get_trip_hyst)
854 return -EPERM;
855
856 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
857 return -EINVAL;
858
859 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
860
Sascha Hauer17e83512015-07-24 08:12:54 +0200861 return ret ? ret : sprintf(buf, "%d\n", temperature);
Durgadoss R27365a62012-07-25 10:10:59 +0800862}
863
864static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000865passive_store(struct device *dev, struct device_attribute *attr,
866 const char *buf, size_t count)
867{
868 struct thermal_zone_device *tz = to_thermal_zone(dev);
869 struct thermal_cooling_device *cdev = NULL;
870 int state;
871
872 if (!sscanf(buf, "%d\n", &state))
873 return -EINVAL;
874
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100875 /* sanity check: values below 1000 millicelcius don't make sense
876 * and can cause the system to go into a thermal heart attack
877 */
878 if (state && state < 1000)
879 return -EINVAL;
880
Matthew Garrett03a971a2008-12-03 18:00:38 +0000881 if (state && !tz->forced_passive) {
882 mutex_lock(&thermal_list_lock);
883 list_for_each_entry(cdev, &thermal_cdev_list, node) {
884 if (!strncmp("Processor", cdev->type,
885 sizeof("Processor")))
886 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800887 THERMAL_TRIPS_NONE, cdev,
888 THERMAL_NO_LIMIT,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000889 THERMAL_NO_LIMIT,
890 THERMAL_WEIGHT_DEFAULT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000891 }
892 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100893 if (!tz->passive_delay)
894 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000895 } else if (!state && tz->forced_passive) {
896 mutex_lock(&thermal_list_lock);
897 list_for_each_entry(cdev, &thermal_cdev_list, node) {
898 if (!strncmp("Processor", cdev->type,
899 sizeof("Processor")))
900 thermal_zone_unbind_cooling_device(tz,
901 THERMAL_TRIPS_NONE,
902 cdev);
903 }
904 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100905 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000906 }
907
Matthew Garrett03a971a2008-12-03 18:00:38 +0000908 tz->forced_passive = state;
909
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700910 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000911
912 return count;
913}
914
915static ssize_t
916passive_show(struct device *dev, struct device_attribute *attr,
917 char *buf)
918{
919 struct thermal_zone_device *tz = to_thermal_zone(dev);
920
921 return sprintf(buf, "%d\n", tz->forced_passive);
922}
923
Durgadoss R5a2c0902012-09-18 11:04:58 +0530924static ssize_t
Ram Chandrasekardc814ca2017-07-10 15:23:47 -0600925polling_delay_show(struct device *dev, struct device_attribute *attr,
926 char *buf)
927{
928 struct thermal_zone_device *tz = to_thermal_zone(dev);
929
930 return snprintf(buf, PAGE_SIZE, "%d\n", tz->polling_delay);
931}
932
933static ssize_t
934polling_delay_store(struct device *dev, struct device_attribute *attr,
935 const char *buf, size_t count)
936{
937 struct thermal_zone_device *tz = to_thermal_zone(dev);
938 int delay;
939
940 if (kstrtoint(buf, 10, &delay))
941 return -EINVAL;
942
943 mutex_lock(&tz->lock);
944 tz->polling_delay = delay;
945 mutex_unlock(&tz->lock);
946 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
947
948 return count;
949}
950
951static ssize_t
952passive_delay_show(struct device *dev, struct device_attribute *attr,
953 char *buf)
954{
955 struct thermal_zone_device *tz = to_thermal_zone(dev);
956
957 return snprintf(buf, PAGE_SIZE, "%d\n", tz->passive_delay);
958}
959
960static ssize_t
961passive_delay_store(struct device *dev, struct device_attribute *attr,
962 const char *buf, size_t count)
963{
964 struct thermal_zone_device *tz = to_thermal_zone(dev);
965 int delay;
966
967 if (kstrtoint(buf, 10, &delay))
968 return -EINVAL;
969
970 mutex_lock(&tz->lock);
971 tz->passive_delay = delay;
972 mutex_unlock(&tz->lock);
973 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
974
975 return count;
976}
977
978static ssize_t
Durgadoss R5a2c0902012-09-18 11:04:58 +0530979policy_store(struct device *dev, struct device_attribute *attr,
980 const char *buf, size_t count)
981{
982 int ret = -EINVAL;
983 struct thermal_zone_device *tz = to_thermal_zone(dev);
984 struct thermal_governor *gov;
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000985 char name[THERMAL_NAME_LENGTH];
986
987 snprintf(name, sizeof(name), "%s", buf);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530988
989 mutex_lock(&thermal_governor_lock);
Javi Merinob6cc7722014-11-25 16:00:33 +0000990 mutex_lock(&tz->lock);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530991
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000992 gov = __find_governor(strim(name));
Durgadoss R5a2c0902012-09-18 11:04:58 +0530993 if (!gov)
994 goto exit;
995
Javi Merinoe33df1d2015-02-26 19:00:27 +0000996 ret = thermal_set_governor(tz, gov);
997 if (!ret)
998 ret = count;
Durgadoss R5a2c0902012-09-18 11:04:58 +0530999
1000exit:
Javi Merinob6cc7722014-11-25 16:00:33 +00001001 mutex_unlock(&tz->lock);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301002 mutex_unlock(&thermal_governor_lock);
1003 return ret;
1004}
1005
1006static ssize_t
1007policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
1008{
1009 struct thermal_zone_device *tz = to_thermal_zone(dev);
1010
1011 return sprintf(buf, "%s\n", tz->governor->name);
1012}
1013
Ni Wade25a0a5c2015-07-14 15:40:56 +08001014static ssize_t
1015available_policies_show(struct device *dev, struct device_attribute *devattr,
1016 char *buf)
1017{
1018 struct thermal_governor *pos;
1019 ssize_t count = 0;
1020 ssize_t size = PAGE_SIZE;
1021
1022 mutex_lock(&thermal_governor_lock);
1023
1024 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
1025 size = PAGE_SIZE - count;
1026 count += scnprintf(buf + count, size, "%s ", pos->name);
1027 }
1028 count += scnprintf(buf + count, size, "\n");
1029
1030 mutex_unlock(&thermal_governor_lock);
1031
1032 return count;
1033}
1034
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +00001035static ssize_t
1036emul_temp_store(struct device *dev, struct device_attribute *attr,
1037 const char *buf, size_t count)
1038{
1039 struct thermal_zone_device *tz = to_thermal_zone(dev);
1040 int ret = 0;
Wei Ni1d0fd422016-03-03 17:33:46 +08001041 int temperature;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +00001042
Wei Ni1d0fd422016-03-03 17:33:46 +08001043 if (kstrtoint(buf, 10, &temperature))
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +00001044 return -EINVAL;
1045
1046 if (!tz->ops->set_emul_temp) {
1047 mutex_lock(&tz->lock);
1048 tz->emul_temperature = temperature;
1049 mutex_unlock(&tz->lock);
1050 } else {
1051 ret = tz->ops->set_emul_temp(tz, temperature);
1052 }
1053
lan,Tianyu800744b2014-01-02 15:47:54 +08001054 if (!ret)
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07001055 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
lan,Tianyu800744b2014-01-02 15:47:54 +08001056
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +00001057 return ret ? ret : count;
1058}
1059static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +00001060
Javi Merino9f382712015-03-26 15:53:02 +00001061static ssize_t
1062sustainable_power_show(struct device *dev, struct device_attribute *devattr,
1063 char *buf)
1064{
1065 struct thermal_zone_device *tz = to_thermal_zone(dev);
1066
1067 if (tz->tzp)
1068 return sprintf(buf, "%u\n", tz->tzp->sustainable_power);
1069 else
1070 return -EIO;
1071}
1072
1073static ssize_t
1074sustainable_power_store(struct device *dev, struct device_attribute *devattr,
1075 const char *buf, size_t count)
1076{
1077 struct thermal_zone_device *tz = to_thermal_zone(dev);
1078 u32 sustainable_power;
1079
1080 if (!tz->tzp)
1081 return -EIO;
1082
1083 if (kstrtou32(buf, 10, &sustainable_power))
1084 return -EINVAL;
1085
1086 tz->tzp->sustainable_power = sustainable_power;
1087
1088 return count;
1089}
1090static DEVICE_ATTR(sustainable_power, S_IWUSR | S_IRUGO, sustainable_power_show,
1091 sustainable_power_store);
1092
1093#define create_s32_tzp_attr(name) \
1094 static ssize_t \
1095 name##_show(struct device *dev, struct device_attribute *devattr, \
1096 char *buf) \
1097 { \
1098 struct thermal_zone_device *tz = to_thermal_zone(dev); \
1099 \
1100 if (tz->tzp) \
Leo Yan15333e32016-03-29 19:24:15 +08001101 return sprintf(buf, "%d\n", tz->tzp->name); \
Javi Merino9f382712015-03-26 15:53:02 +00001102 else \
1103 return -EIO; \
1104 } \
1105 \
1106 static ssize_t \
1107 name##_store(struct device *dev, struct device_attribute *devattr, \
1108 const char *buf, size_t count) \
1109 { \
1110 struct thermal_zone_device *tz = to_thermal_zone(dev); \
1111 s32 value; \
1112 \
1113 if (!tz->tzp) \
1114 return -EIO; \
1115 \
1116 if (kstrtos32(buf, 10, &value)) \
1117 return -EINVAL; \
1118 \
1119 tz->tzp->name = value; \
1120 \
1121 return count; \
1122 } \
1123 static DEVICE_ATTR(name, S_IWUSR | S_IRUGO, name##_show, name##_store)
1124
1125create_s32_tzp_attr(k_po);
1126create_s32_tzp_attr(k_pu);
1127create_s32_tzp_attr(k_i);
1128create_s32_tzp_attr(k_d);
1129create_s32_tzp_attr(integral_cutoff);
Eduardo Valentin9d0be7f2015-05-11 19:34:23 -07001130create_s32_tzp_attr(slope);
1131create_s32_tzp_attr(offset);
Javi Merino9f382712015-03-26 15:53:02 +00001132#undef create_s32_tzp_attr
1133
1134static struct device_attribute *dev_tzp_attrs[] = {
1135 &dev_attr_sustainable_power,
1136 &dev_attr_k_po,
1137 &dev_attr_k_pu,
1138 &dev_attr_k_i,
1139 &dev_attr_k_d,
1140 &dev_attr_integral_cutoff,
Eduardo Valentin9d0be7f2015-05-11 19:34:23 -07001141 &dev_attr_slope,
1142 &dev_attr_offset,
Javi Merino9f382712015-03-26 15:53:02 +00001143};
1144
1145static int create_tzp_attrs(struct device *dev)
1146{
1147 int i;
1148
1149 for (i = 0; i < ARRAY_SIZE(dev_tzp_attrs); i++) {
1150 int ret;
1151 struct device_attribute *dev_attr = dev_tzp_attrs[i];
1152
1153 ret = device_create_file(dev, dev_attr);
1154 if (ret)
1155 return ret;
1156 }
1157
1158 return 0;
1159}
1160
Javi Merino35b11d22015-02-26 19:00:28 +00001161/**
1162 * power_actor_get_max_power() - get the maximum power that a cdev can consume
1163 * @cdev: pointer to &thermal_cooling_device
1164 * @tz: a valid thermal zone device pointer
1165 * @max_power: pointer in which to store the maximum power
1166 *
1167 * Calculate the maximum power consumption in milliwats that the
1168 * cooling device can currently consume and store it in @max_power.
1169 *
1170 * Return: 0 on success, -EINVAL if @cdev doesn't support the
1171 * power_actor API or -E* on other error.
1172 */
1173int power_actor_get_max_power(struct thermal_cooling_device *cdev,
1174 struct thermal_zone_device *tz, u32 *max_power)
1175{
1176 if (!cdev_is_power_actor(cdev))
1177 return -EINVAL;
1178
1179 return cdev->ops->state2power(cdev, tz, 0, max_power);
1180}
1181
1182/**
Javi Merinoc973c3b2015-09-14 14:23:50 +01001183 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
1184 * @cdev: pointer to &thermal_cooling_device
1185 * @tz: a valid thermal zone device pointer
1186 * @min_power: pointer in which to store the minimum power
1187 *
1188 * Calculate the minimum power consumption in milliwatts that the
1189 * cooling device can currently consume and store it in @min_power.
1190 *
1191 * Return: 0 on success, -EINVAL if @cdev doesn't support the
1192 * power_actor API or -E* on other error.
1193 */
1194int power_actor_get_min_power(struct thermal_cooling_device *cdev,
1195 struct thermal_zone_device *tz, u32 *min_power)
1196{
1197 unsigned long max_state;
1198 int ret;
1199
1200 if (!cdev_is_power_actor(cdev))
1201 return -EINVAL;
1202
1203 ret = cdev->ops->get_max_state(cdev, &max_state);
1204 if (ret)
1205 return ret;
1206
1207 return cdev->ops->state2power(cdev, tz, max_state, min_power);
1208}
1209
1210/**
Javi Merino35b11d22015-02-26 19:00:28 +00001211 * power_actor_set_power() - limit the maximum power that a cooling device can consume
1212 * @cdev: pointer to &thermal_cooling_device
1213 * @instance: thermal instance to update
1214 * @power: the power in milliwatts
1215 *
1216 * Set the cooling device to consume at most @power milliwatts.
1217 *
1218 * Return: 0 on success, -EINVAL if the cooling device does not
1219 * implement the power actor API or -E* for other failures.
1220 */
1221int power_actor_set_power(struct thermal_cooling_device *cdev,
1222 struct thermal_instance *instance, u32 power)
1223{
1224 unsigned long state;
1225 int ret;
1226
1227 if (!cdev_is_power_actor(cdev))
1228 return -EINVAL;
1229
1230 ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
1231 if (ret)
1232 return ret;
1233
1234 instance->target = state;
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01001235 mutex_lock(&cdev->lock);
Javi Merino35b11d22015-02-26 19:00:28 +00001236 cdev->updated = false;
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01001237 mutex_unlock(&cdev->lock);
Javi Merino35b11d22015-02-26 19:00:28 +00001238 thermal_cdev_update(cdev);
1239
1240 return 0;
1241}
1242
Zhang Rui203d3d42008-01-17 15:51:08 +08001243static DEVICE_ATTR(type, 0444, type_show, NULL);
1244static DEVICE_ATTR(temp, 0444, temp_show, NULL);
1245static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -07001246static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301247static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
Ni Wade25a0a5c2015-07-14 15:40:56 +08001248static DEVICE_ATTR(available_policies, S_IRUGO, available_policies_show, NULL);
Ram Chandrasekardc814ca2017-07-10 15:23:47 -06001249static DEVICE_ATTR(passive_delay, 0644, passive_delay_show,
1250 passive_delay_store);
1251static DEVICE_ATTR(polling_delay, 0644, polling_delay_show,
1252 polling_delay_store);
Zhang Rui203d3d42008-01-17 15:51:08 +08001253
Zhang Rui203d3d42008-01-17 15:51:08 +08001254/* sys I/F for cooling device */
1255#define to_cooling_device(_dev) \
1256 container_of(_dev, struct thermal_cooling_device, device)
1257
1258static ssize_t
1259thermal_cooling_device_type_show(struct device *dev,
1260 struct device_attribute *attr, char *buf)
1261{
1262 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1263
1264 return sprintf(buf, "%s\n", cdev->type);
1265}
1266
1267static ssize_t
1268thermal_cooling_device_max_state_show(struct device *dev,
1269 struct device_attribute *attr, char *buf)
1270{
1271 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001272 unsigned long state;
1273 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +08001274
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001275 ret = cdev->ops->get_max_state(cdev, &state);
1276 if (ret)
1277 return ret;
1278 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +08001279}
1280
1281static ssize_t
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001282thermal_cooling_device_min_state_show(struct device *dev,
1283 struct device_attribute *attr, char *buf)
1284{
1285 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1286 unsigned long state;
1287 int ret;
1288
1289 if (cdev->ops->get_min_state)
1290 ret = cdev->ops->get_min_state(cdev, &state);
1291 else
1292 ret = -EPERM;
1293
1294 if (ret)
1295 return ret;
1296
1297 return snprintf(buf, PAGE_SIZE, "%lu\n", state);
1298}
1299
1300static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +08001301thermal_cooling_device_cur_state_show(struct device *dev,
1302 struct device_attribute *attr, char *buf)
1303{
1304 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001305 unsigned long state;
1306 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +08001307
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001308 ret = cdev->ops->get_cur_state(cdev, &state);
1309 if (ret)
1310 return ret;
1311 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +08001312}
1313
1314static ssize_t
1315thermal_cooling_device_cur_state_store(struct device *dev,
1316 struct device_attribute *attr,
1317 const char *buf, size_t count)
1318{
1319 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Ram Chandrasekar5ab1c082016-09-20 17:09:28 -06001320 long state;
Zhang Rui203d3d42008-01-17 15:51:08 +08001321
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001322 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +08001323 return -EINVAL;
1324
Roel Kluinedb94912009-12-15 22:46:50 +01001325 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +08001326 return -EINVAL;
1327
Ram Chandrasekar5ab1c082016-09-20 17:09:28 -06001328 cdev->sysfs_cur_state_req = state;
1329
1330 cdev->updated = false;
1331 thermal_cdev_update(cdev);
1332
Zhang Rui203d3d42008-01-17 15:51:08 +08001333 return count;
1334}
1335
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001336static ssize_t
1337thermal_cooling_device_min_state_store(struct device *dev,
1338 struct device_attribute *attr,
1339 const char *buf, size_t count)
1340{
1341 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1342 long state;
1343 int ret = 0;
1344
1345 ret = sscanf(buf, "%ld\n", &state);
1346 if (ret <= 0)
1347 return (ret < 0) ? ret : -EINVAL;
1348
1349 if ((long)state < 0)
1350 return -EINVAL;
1351
1352 cdev->sysfs_min_state_req = state;
1353
1354 cdev->updated = false;
1355 thermal_cdev_update(cdev);
1356
1357 return count;
1358}
1359
Zhang Rui203d3d42008-01-17 15:51:08 +08001360static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -05001361__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001362static DEVICE_ATTR(max_state, 0444,
1363 thermal_cooling_device_max_state_show, NULL);
1364static DEVICE_ATTR(cur_state, 0644,
1365 thermal_cooling_device_cur_state_show,
1366 thermal_cooling_device_cur_state_store);
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001367static DEVICE_ATTR(min_state, 0644,
1368 thermal_cooling_device_min_state_show,
1369 thermal_cooling_device_min_state_store);
Zhang Rui203d3d42008-01-17 15:51:08 +08001370
1371static ssize_t
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001372thermal_cooling_device_lower_limit_show(struct device *dev,
1373 struct device_attribute *attr, char *buf)
1374{
1375 struct thermal_instance *instance;
1376
1377 instance =
1378 container_of(attr, struct thermal_instance, lower_attr);
1379
1380 return snprintf(buf, PAGE_SIZE, "%lu\n", instance->lower);
1381}
1382
1383static ssize_t
1384thermal_cooling_device_upper_limit_show(struct device *dev,
1385 struct device_attribute *attr, char *buf)
1386{
1387 struct thermal_instance *instance;
1388
1389 instance =
1390 container_of(attr, struct thermal_instance, upper_attr);
1391
1392 return snprintf(buf, PAGE_SIZE, "%lu\n", instance->upper);
1393}
1394
1395static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +08001396thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -05001397 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +08001398{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001399 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +08001400
1401 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001402 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001403
1404 if (instance->trip == THERMAL_TRIPS_NONE)
1405 return sprintf(buf, "-1\n");
1406 else
1407 return sprintf(buf, "%d\n", instance->trip);
1408}
1409
Ram Chandrasekardc814ca2017-07-10 15:23:47 -06001410static ssize_t
1411thermal_cooling_device_upper_limit_store(struct device *dev,
1412 struct device_attribute *attr,
1413 const char *buf, size_t count)
1414{
1415 struct thermal_zone_device *tz = to_thermal_zone(dev);
1416 struct thermal_instance *instance;
1417 int ret, upper_limit;
1418
1419 instance =
1420 container_of(attr, struct thermal_instance, upper_attr);
1421
1422 ret = kstrtoint(buf, 0, &upper_limit);
1423 if (ret)
1424 return ret;
1425 if (upper_limit < instance->lower)
1426 return -EINVAL;
1427
1428 instance->upper = upper_limit;
1429 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1430
1431 return count;
1432}
1433
1434static ssize_t
1435thermal_cooling_device_lower_limit_store(struct device *dev,
1436 struct device_attribute *attr,
1437 const char *buf, size_t count)
1438{
1439 struct thermal_zone_device *tz = to_thermal_zone(dev);
1440 struct thermal_instance *instance;
1441 int ret, lower_limit;
1442
1443 instance =
1444 container_of(attr, struct thermal_instance, lower_attr);
1445
1446 ret = kstrtoint(buf, 0, &lower_limit);
1447 if (ret)
1448 return ret;
1449 if (lower_limit > instance->upper)
1450 return -EINVAL;
1451
1452 instance->lower = lower_limit;
1453 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1454
1455 return count;
1456}
1457
Matthias Kaehlcke2dc10f82015-02-20 18:10:08 -08001458static struct attribute *cooling_device_attrs[] = {
1459 &dev_attr_cdev_type.attr,
1460 &dev_attr_max_state.attr,
1461 &dev_attr_cur_state.attr,
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001462 &dev_attr_min_state.attr,
Matthias Kaehlcke2dc10f82015-02-20 18:10:08 -08001463 NULL,
1464};
1465
1466static const struct attribute_group cooling_device_attr_group = {
1467 .attrs = cooling_device_attrs,
1468};
1469
1470static const struct attribute_group *cooling_device_attr_groups[] = {
1471 &cooling_device_attr_group,
1472 NULL,
1473};
1474
Javi Merinodb916512015-02-18 16:04:24 +00001475static ssize_t
1476thermal_cooling_device_weight_show(struct device *dev,
1477 struct device_attribute *attr, char *buf)
1478{
1479 struct thermal_instance *instance;
1480
1481 instance = container_of(attr, struct thermal_instance, weight_attr);
1482
1483 return sprintf(buf, "%d\n", instance->weight);
1484}
1485
1486static ssize_t
1487thermal_cooling_device_weight_store(struct device *dev,
1488 struct device_attribute *attr,
1489 const char *buf, size_t count)
1490{
1491 struct thermal_instance *instance;
1492 int ret, weight;
1493
1494 ret = kstrtoint(buf, 0, &weight);
1495 if (ret)
1496 return ret;
1497
1498 instance = container_of(attr, struct thermal_instance, weight_attr);
1499 instance->weight = weight;
1500
1501 return count;
1502}
Zhang Rui203d3d42008-01-17 15:51:08 +08001503/* Device management */
1504
1505/**
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001506 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
1507 * @tz: pointer to struct thermal_zone_device
Zhang Rui203d3d42008-01-17 15:51:08 +08001508 * @trip: indicates which trip point the cooling devices is
1509 * associated with in this thermal zone.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001510 * @cdev: pointer to struct thermal_cooling_device
1511 * @upper: the Maximum cooling state for this trip point.
1512 * THERMAL_NO_LIMIT means no upper limit,
1513 * and the cooling device can be in max_state.
1514 * @lower: the Minimum cooling state can be used for this trip point.
1515 * THERMAL_NO_LIMIT means no lower limit,
1516 * and the cooling device can be in cooling state 0.
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001517 * @weight: The weight of the cooling device to be bound to the
1518 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
1519 * default value
Len Brown543a9562008-02-07 16:55:08 -05001520 *
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001521 * This interface function bind a thermal cooling device to the certain trip
1522 * point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001523 * This function is usually called in the thermal zone device .bind callback.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001524 *
1525 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001526 */
1527int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1528 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +08001529 struct thermal_cooling_device *cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001530 unsigned long upper, unsigned long lower,
1531 unsigned int weight)
Zhang Rui203d3d42008-01-17 15:51:08 +08001532{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001533 struct thermal_instance *dev;
1534 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -05001535 struct thermal_zone_device *pos1;
1536 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +08001537 unsigned long max_state;
Lukasz Majewski9a3031d2014-11-18 11:16:30 +01001538 int result, ret;
Zhang Rui203d3d42008-01-17 15:51:08 +08001539
Len Brown543a9562008-02-07 16:55:08 -05001540 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +08001541 return -EINVAL;
1542
Thomas Sujithc7516702008-02-15 00:58:50 -05001543 list_for_each_entry(pos1, &thermal_tz_list, node) {
1544 if (pos1 == tz)
1545 break;
1546 }
1547 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1548 if (pos2 == cdev)
1549 break;
1550 }
1551
1552 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +08001553 return -EINVAL;
1554
Lukasz Majewski9a3031d2014-11-18 11:16:30 +01001555 ret = cdev->ops->get_max_state(cdev, &max_state);
1556 if (ret)
1557 return ret;
Zhang Rui9d998422012-06-26 16:35:57 +08001558
Ram Chandrasekar2a1abd12017-06-12 12:52:08 -06001559 /*
1560 * If upper or lower has a MACRO to define the mitigation state,
1561 * based on the MACRO determine the default state to use or the
1562 * offset from the max_state.
1563 */
1564 if (upper > (THERMAL_MAX_LIMIT - max_state)) {
1565 /* upper default max_state */
1566 if (upper == THERMAL_NO_LIMIT)
1567 upper = max_state;
1568 else
1569 upper = max_state - (THERMAL_MAX_LIMIT - upper);
1570 }
1571
1572 if (lower > (THERMAL_MAX_LIMIT - max_state)) {
1573 /* lower default 0 */
1574 if (lower == THERMAL_NO_LIMIT)
1575 lower = 0;
1576 else
1577 lower = max_state - (THERMAL_MAX_LIMIT - lower);
1578 }
Zhang Rui9d998422012-06-26 16:35:57 +08001579
1580 if (lower > upper || upper > max_state)
1581 return -EINVAL;
1582
Zhang Rui203d3d42008-01-17 15:51:08 +08001583 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001584 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001585 if (!dev)
1586 return -ENOMEM;
1587 dev->tz = tz;
1588 dev->cdev = cdev;
1589 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +08001590 dev->upper = upper;
1591 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +08001592 dev->target = THERMAL_NO_TARGET;
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001593 dev->weight = weight;
Zhang Rui74051ba2012-06-26 16:27:22 +08001594
Zhang Rui203d3d42008-01-17 15:51:08 +08001595 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1596 if (result)
1597 goto free_mem;
1598
1599 sprintf(dev->name, "cdev%d", dev->id);
1600 result =
1601 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1602 if (result)
1603 goto release_idr;
1604
1605 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -07001606 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001607 dev->attr.attr.name = dev->attr_name;
1608 dev->attr.attr.mode = 0444;
1609 dev->attr.show = thermal_cooling_device_trip_point_show;
1610 result = device_create_file(&tz->device, &dev->attr);
1611 if (result)
1612 goto remove_symbol_link;
1613
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001614 snprintf(dev->upper_attr_name, THERMAL_NAME_LENGTH,
1615 "cdev%d_upper_limit", dev->id);
1616 sysfs_attr_init(&dev->upper_attr.attr);
1617 dev->upper_attr.attr.name = dev->upper_attr_name;
Ram Chandrasekardc814ca2017-07-10 15:23:47 -06001618 dev->upper_attr.attr.mode = 0644;
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001619 dev->upper_attr.show = thermal_cooling_device_upper_limit_show;
Ram Chandrasekardc814ca2017-07-10 15:23:47 -06001620 dev->upper_attr.store = thermal_cooling_device_upper_limit_store;
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001621 result = device_create_file(&tz->device, &dev->upper_attr);
1622 if (result)
1623 goto remove_trip_file;
1624
1625 snprintf(dev->lower_attr_name, THERMAL_NAME_LENGTH,
1626 "cdev%d_lower_limit", dev->id);
1627 sysfs_attr_init(&dev->lower_attr.attr);
1628 dev->lower_attr.attr.name = dev->lower_attr_name;
Ram Chandrasekardc814ca2017-07-10 15:23:47 -06001629 dev->lower_attr.attr.mode = 0644;
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001630 dev->lower_attr.show = thermal_cooling_device_lower_limit_show;
Ram Chandrasekardc814ca2017-07-10 15:23:47 -06001631 dev->lower_attr.store = thermal_cooling_device_lower_limit_store;
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001632 result = device_create_file(&tz->device, &dev->lower_attr);
1633 if (result)
1634 goto remove_upper_file;
1635
Javi Merinodb916512015-02-18 16:04:24 +00001636 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
1637 sysfs_attr_init(&dev->weight_attr.attr);
1638 dev->weight_attr.attr.name = dev->weight_attr_name;
1639 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
1640 dev->weight_attr.show = thermal_cooling_device_weight_show;
1641 dev->weight_attr.store = thermal_cooling_device_weight_store;
1642 result = device_create_file(&tz->device, &dev->weight_attr);
1643 if (result)
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001644 goto remove_lower_file;
Javi Merinodb916512015-02-18 16:04:24 +00001645
Zhang Rui203d3d42008-01-17 15:51:08 +08001646 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001647 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001648 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +08001649 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1650 result = -EEXIST;
1651 break;
1652 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001653 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001654 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001655 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
Chen Yu4511f712015-10-30 16:32:10 +08001656 atomic_set(&tz->need_update, 1);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001657 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001658 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001659 mutex_unlock(&tz->lock);
1660
1661 if (!result)
1662 return 0;
1663
Javi Merinodb916512015-02-18 16:04:24 +00001664 device_remove_file(&tz->device, &dev->weight_attr);
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001665remove_lower_file:
1666 device_remove_file(&tz->device, &dev->lower_attr);
1667remove_upper_file:
1668 device_remove_file(&tz->device, &dev->upper_attr);
Javi Merinodb916512015-02-18 16:04:24 +00001669remove_trip_file:
Zhang Rui203d3d42008-01-17 15:51:08 +08001670 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -07001671remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +08001672 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -07001673release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +08001674 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -07001675free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +08001676 kfree(dev);
1677 return result;
1678}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001679EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001680
1681/**
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001682 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1683 * thermal zone.
1684 * @tz: pointer to a struct thermal_zone_device.
Zhang Rui203d3d42008-01-17 15:51:08 +08001685 * @trip: indicates which trip point the cooling devices is
1686 * associated with in this thermal zone.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001687 * @cdev: pointer to a struct thermal_cooling_device.
Len Brown543a9562008-02-07 16:55:08 -05001688 *
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001689 * This interface function unbind a thermal cooling device from the certain
1690 * trip point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001691 * This function is usually called in the thermal zone device .unbind callback.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001692 *
1693 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001694 */
1695int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1696 int trip,
1697 struct thermal_cooling_device *cdev)
1698{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001699 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +08001700
1701 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001702 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001703 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -05001704 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001705 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001706 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001707 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001708 mutex_unlock(&tz->lock);
1709 goto unbind;
1710 }
1711 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001712 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001713 mutex_unlock(&tz->lock);
1714
1715 return -ENODEV;
1716
Joe Perchescaca8b82012-03-21 12:55:02 -07001717unbind:
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001718 device_remove_file(&tz->device, &pos->lower_attr);
1719 device_remove_file(&tz->device, &pos->upper_attr);
Viresh Kumar528464e2015-07-23 14:32:32 +05301720 device_remove_file(&tz->device, &pos->weight_attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001721 device_remove_file(&tz->device, &pos->attr);
1722 sysfs_remove_link(&tz->device.kobj, pos->name);
1723 release_idr(&tz->idr, &tz->lock, pos->id);
1724 kfree(pos);
1725 return 0;
1726}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001727EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001728
1729static void thermal_release(struct device *dev)
1730{
1731 struct thermal_zone_device *tz;
1732 struct thermal_cooling_device *cdev;
1733
Joe Perchescaca8b82012-03-21 12:55:02 -07001734 if (!strncmp(dev_name(dev), "thermal_zone",
1735 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001736 tz = to_thermal_zone(dev);
1737 kfree(tz);
durgadoss.r@intel.com732e4c82013-10-02 00:08:00 +05301738 } else if(!strncmp(dev_name(dev), "cooling_device",
1739 sizeof("cooling_device") - 1)){
Zhang Rui203d3d42008-01-17 15:51:08 +08001740 cdev = to_cooling_device(dev);
1741 kfree(cdev);
1742 }
1743}
1744
1745static struct class thermal_class = {
1746 .name = "thermal",
1747 .dev_release = thermal_release,
1748};
1749
1750/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001751 * __thermal_cooling_device_register() - register a new thermal cooling device
1752 * @np: a pointer to a device tree node.
Zhang Rui203d3d42008-01-17 15:51:08 +08001753 * @type: the thermal cooling device type.
1754 * @devdata: device private data.
1755 * @ops: standard thermal cooling devices callbacks.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001756 *
1757 * This interface function adds a new thermal cooling device (fan/processor/...)
1758 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1759 * to all the thermal zone devices registered at the same time.
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001760 * It also gives the opportunity to link the cooling device to a device tree
1761 * node, so that it can be bound to a thermal zone created out of device tree.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001762 *
1763 * Return: a pointer to the created struct thermal_cooling_device or an
1764 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001765 */
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001766static struct thermal_cooling_device *
1767__thermal_cooling_device_register(struct device_node *np,
1768 char *type, void *devdata,
1769 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001770{
1771 struct thermal_cooling_device *cdev;
Chen Yu4511f712015-10-30 16:32:10 +08001772 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001773 int result;
1774
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001775 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001776 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001777
1778 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001779 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001780 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001781
1782 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1783 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001784 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001785
1786 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1787 if (result) {
1788 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001789 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001790 }
1791
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001792 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +08001793 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001794 INIT_LIST_HEAD(&cdev->thermal_instances);
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001795 cdev->np = np;
Zhang Rui203d3d42008-01-17 15:51:08 +08001796 cdev->ops = ops;
Ni Wade5ca0cce2014-02-17 11:02:55 +08001797 cdev->updated = false;
Zhang Rui203d3d42008-01-17 15:51:08 +08001798 cdev->device.class = &thermal_class;
Matthias Kaehlcke2dc10f82015-02-20 18:10:08 -08001799 cdev->device.groups = cooling_device_attr_groups;
Zhang Rui203d3d42008-01-17 15:51:08 +08001800 cdev->devdata = devdata;
Ram Chandrasekar5ab1c082016-09-20 17:09:28 -06001801 cdev->sysfs_cur_state_req = 0;
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001802 cdev->sysfs_min_state_req = ULONG_MAX;
Kay Sievers354655e2009-01-06 10:44:37 -08001803 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001804 result = device_register(&cdev->device);
1805 if (result) {
1806 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1807 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001808 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001809 }
1810
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301811 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +08001812 mutex_lock(&thermal_list_lock);
1813 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001814 mutex_unlock(&thermal_list_lock);
1815
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301816 /* Update binding information for 'this' new cdev */
1817 bind_cdev(cdev);
1818
Chen Yu4511f712015-10-30 16:32:10 +08001819 mutex_lock(&thermal_list_lock);
1820 list_for_each_entry(pos, &thermal_tz_list, node)
1821 if (atomic_cmpxchg(&pos->need_update, 1, 0))
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07001822 thermal_zone_device_update(pos,
1823 THERMAL_EVENT_UNSPECIFIED);
Chen Yu4511f712015-10-30 16:32:10 +08001824 mutex_unlock(&thermal_list_lock);
1825
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301826 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001827}
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001828
1829/**
1830 * thermal_cooling_device_register() - register a new thermal cooling device
1831 * @type: the thermal cooling device type.
1832 * @devdata: device private data.
1833 * @ops: standard thermal cooling devices callbacks.
1834 *
1835 * This interface function adds a new thermal cooling device (fan/processor/...)
1836 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1837 * to all the thermal zone devices registered at the same time.
1838 *
1839 * Return: a pointer to the created struct thermal_cooling_device or an
1840 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1841 */
1842struct thermal_cooling_device *
1843thermal_cooling_device_register(char *type, void *devdata,
1844 const struct thermal_cooling_device_ops *ops)
1845{
1846 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1847}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001848EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001849
1850/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001851 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1852 * @np: a pointer to a device tree node.
1853 * @type: the thermal cooling device type.
1854 * @devdata: device private data.
1855 * @ops: standard thermal cooling devices callbacks.
1856 *
1857 * This function will register a cooling device with device tree node reference.
1858 * This interface function adds a new thermal cooling device (fan/processor/...)
1859 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1860 * to all the thermal zone devices registered at the same time.
1861 *
1862 * Return: a pointer to the created struct thermal_cooling_device or an
1863 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1864 */
1865struct thermal_cooling_device *
1866thermal_of_cooling_device_register(struct device_node *np,
1867 char *type, void *devdata,
1868 const struct thermal_cooling_device_ops *ops)
1869{
1870 return __thermal_cooling_device_register(np, type, devdata, ops);
1871}
1872EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1873
1874/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001875 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001876 * @cdev: the thermal cooling device to remove.
1877 *
1878 * thermal_cooling_device_unregister() must be called when the device is no
1879 * longer needed.
1880 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301881void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001882{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301883 int i;
1884 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001885 struct thermal_zone_device *tz;
1886 struct thermal_cooling_device *pos = NULL;
1887
1888 if (!cdev)
1889 return;
1890
1891 mutex_lock(&thermal_list_lock);
1892 list_for_each_entry(pos, &thermal_cdev_list, node)
1893 if (pos == cdev)
1894 break;
1895 if (pos != cdev) {
1896 /* thermal cooling device not found */
1897 mutex_unlock(&thermal_list_lock);
1898 return;
1899 }
1900 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301901
1902 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001903 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301904 if (tz->ops->unbind) {
1905 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001906 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301907 }
1908
1909 if (!tz->tzp || !tz->tzp->tbp)
1910 continue;
1911
1912 tzp = tz->tzp;
1913 for (i = 0; i < tzp->num_tbps; i++) {
1914 if (tzp->tbp[i].cdev == cdev) {
1915 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1916 tzp->tbp[i].cdev = NULL;
1917 }
1918 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001919 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301920
Zhang Rui203d3d42008-01-17 15:51:08 +08001921 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301922
Zhang Rui203d3d42008-01-17 15:51:08 +08001923 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001924 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001925 device_remove_file(&cdev->device, &dev_attr_max_state);
1926 device_remove_file(&cdev->device, &dev_attr_cur_state);
1927
1928 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1929 device_unregister(&cdev->device);
1930 return;
1931}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001932EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001933
Durgadoss Rdc765482012-09-18 11:05:00 +05301934void thermal_cdev_update(struct thermal_cooling_device *cdev)
Zhang Ruice119f82012-06-27 14:13:04 +08001935{
1936 struct thermal_instance *instance;
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001937 unsigned long current_target = 0, min_target = ULONG_MAX;
Zhang Ruice119f82012-06-27 14:13:04 +08001938
Zhang Ruif4a821c2012-07-24 16:56:21 +08001939 mutex_lock(&cdev->lock);
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01001940 /* cooling device is updated*/
1941 if (cdev->updated) {
1942 mutex_unlock(&cdev->lock);
1943 return;
1944 }
1945
Zhang Ruice119f82012-06-27 14:13:04 +08001946 /* Make sure cdev enters the deepest cooling state */
Ram Chandrasekar5ab1c082016-09-20 17:09:28 -06001947 current_target = cdev->sysfs_cur_state_req;
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001948 min_target = cdev->sysfs_min_state_req;
Zhang Ruice119f82012-06-27 14:13:04 +08001949 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
Aaron Lu06475b52013-12-02 13:54:26 +08001950 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1951 instance->tz->id, instance->target);
Zhang Ruice119f82012-06-27 14:13:04 +08001952 if (instance->target == THERMAL_NO_TARGET)
1953 continue;
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001954 if (instance->tz->governor->min_state_throttle) {
1955 if (instance->target < min_target)
1956 min_target = instance->target;
1957 } else {
1958 if (instance->target > current_target)
1959 current_target = instance->target;
1960 }
Zhang Ruice119f82012-06-27 14:13:04 +08001961 }
Ram Chandrasekar4185d52d2017-07-13 17:27:50 -06001962 trace_cdev_update_start(cdev);
Ram Chandrasekar5ab1c082016-09-20 17:09:28 -06001963 cdev->ops->set_cur_state(cdev, current_target);
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001964 if (cdev->ops->set_min_state)
1965 cdev->ops->set_min_state(cdev, min_target);
Zhang Ruice119f82012-06-27 14:13:04 +08001966 cdev->updated = true;
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01001967 mutex_unlock(&cdev->lock);
Ram Chandrasekar5b1c0d12017-03-28 11:35:40 -06001968 trace_cdev_update(cdev, current_target, min_target);
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001969 dev_dbg(&cdev->device, "set to state %lu min state %lu\n",
1970 current_target, min_target);
Zhang Ruice119f82012-06-27 14:13:04 +08001971}
Durgadoss Rdc765482012-09-18 11:05:00 +05301972EXPORT_SYMBOL(thermal_cdev_update);
Zhang Ruice119f82012-06-27 14:13:04 +08001973
Matthew Garrettb1569e92008-12-03 17:55:32 +00001974/**
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001975 * thermal_notify_framework - Sensor drivers use this API to notify framework
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301976 * @tz: thermal zone device
1977 * @trip: indicates which trip point has been crossed
1978 *
1979 * This function handles the trip events from sensor drivers. It starts
1980 * throttling the cooling devices according to the policy configured.
1981 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1982 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1983 * The throttling policy is based on the configured platform data; if no
1984 * platform data is provided, this uses the step_wise throttling policy.
1985 */
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001986void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301987{
1988 handle_thermal_trip(tz, trip);
1989}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001990EXPORT_SYMBOL_GPL(thermal_notify_framework);
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301991
1992/**
Eduardo Valentin269c1742013-04-23 21:48:19 +00001993 * create_trip_attrs() - create attributes for trip points
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001994 * @tz: the thermal zone device
1995 * @mask: Writeable trip point bitmap.
Eduardo Valentin269c1742013-04-23 21:48:19 +00001996 *
1997 * helper function to instantiate sysfs entries for every trip
1998 * point and its properties of a struct thermal_zone_device.
1999 *
2000 * Return: 0 on success, the proper error value otherwise.
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002001 */
2002static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
2003{
2004 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08002005 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002006
Durgadoss R27365a62012-07-25 10:10:59 +08002007 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002008 if (!tz->trip_type_attrs)
2009 return -ENOMEM;
2010
Durgadoss R27365a62012-07-25 10:10:59 +08002011 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002012 if (!tz->trip_temp_attrs) {
2013 kfree(tz->trip_type_attrs);
2014 return -ENOMEM;
2015 }
2016
Durgadoss R27365a62012-07-25 10:10:59 +08002017 if (tz->ops->get_trip_hyst) {
2018 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
2019 if (!tz->trip_hyst_attrs) {
2020 kfree(tz->trip_type_attrs);
2021 kfree(tz->trip_temp_attrs);
2022 return -ENOMEM;
2023 }
2024 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002025
Durgadoss R27365a62012-07-25 10:10:59 +08002026
2027 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002028 /* create trip type attribute */
2029 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
2030 "trip_point_%d_type", indx);
2031
2032 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
2033 tz->trip_type_attrs[indx].attr.attr.name =
2034 tz->trip_type_attrs[indx].name;
2035 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
2036 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
2037
2038 device_create_file(&tz->device,
2039 &tz->trip_type_attrs[indx].attr);
2040
2041 /* create trip temp attribute */
2042 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
2043 "trip_point_%d_temp", indx);
2044
2045 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
2046 tz->trip_temp_attrs[indx].attr.attr.name =
2047 tz->trip_temp_attrs[indx].name;
2048 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
2049 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
Punit Agrawal35e94642015-03-03 10:43:03 +00002050 if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS) &&
2051 mask & (1 << indx)) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002052 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
2053 tz->trip_temp_attrs[indx].attr.store =
2054 trip_point_temp_store;
2055 }
2056
2057 device_create_file(&tz->device,
2058 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08002059
2060 /* create Optional trip hyst attribute */
2061 if (!tz->ops->get_trip_hyst)
2062 continue;
2063 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
2064 "trip_point_%d_hyst", indx);
2065
2066 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
2067 tz->trip_hyst_attrs[indx].attr.attr.name =
2068 tz->trip_hyst_attrs[indx].name;
2069 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
2070 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
2071 if (tz->ops->set_trip_hyst) {
2072 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
2073 tz->trip_hyst_attrs[indx].attr.store =
2074 trip_point_hyst_store;
2075 }
2076
2077 device_create_file(&tz->device,
2078 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002079 }
2080 return 0;
2081}
2082
2083static void remove_trip_attrs(struct thermal_zone_device *tz)
2084{
2085 int indx;
2086
2087 for (indx = 0; indx < tz->trips; indx++) {
2088 device_remove_file(&tz->device,
2089 &tz->trip_type_attrs[indx].attr);
2090 device_remove_file(&tz->device,
2091 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08002092 if (tz->ops->get_trip_hyst)
2093 device_remove_file(&tz->device,
2094 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002095 }
2096 kfree(tz->trip_type_attrs);
2097 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08002098 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002099}
2100
2101/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00002102 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08002103 * @type: the thermal zone device type
2104 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002105 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08002106 * @devdata: private device data
2107 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05302108 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00002109 * @passive_delay: number of milliseconds to wait between polls when
2110 * performing passive cooling
2111 * @polling_delay: number of milliseconds to wait between polls when checking
2112 * whether trip points have been crossed (0 for interrupt
2113 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08002114 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00002115 * This interface function adds a new thermal zone device (sensor) to
2116 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
2117 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08002118 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08002119 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00002120 *
2121 * Return: a pointer to the created struct thermal_zone_device or an
2122 * in case of error, an ERR_PTR. Caller must check return value with
2123 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08002124 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07002125struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002126 int trips, int mask, void *devdata,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002127 struct thermal_zone_device_ops *ops,
Javi Merino6b775e82015-03-02 17:17:19 +00002128 struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08002129 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08002130{
2131 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00002132 enum thermal_trip_type trip_type;
Zhang Rui81ad4272016-03-18 10:03:24 +08002133 int trip_temp;
Zhang Rui203d3d42008-01-17 15:51:08 +08002134 int result;
2135 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00002136 int passive = 0;
Javi Merinoe33df1d2015-02-26 19:00:27 +00002137 struct thermal_governor *governor;
Zhang Rui203d3d42008-01-17 15:51:08 +08002138
Guenter Roeck204dd1d2012-08-07 22:36:45 -07002139 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002140 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08002141
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002142 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002143 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08002144
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04002145 if (!ops)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002146 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08002147
Jonghwa Lee83720d02013-05-18 09:50:26 +00002148 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00002149 return ERR_PTR(-EINVAL);
2150
Zhang Rui203d3d42008-01-17 15:51:08 +08002151 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
2152 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002153 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08002154
Zhang Rui2d374132012-06-27 10:09:00 +08002155 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08002156 idr_init(&tz->idr);
2157 mutex_init(&tz->lock);
2158 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
2159 if (result) {
2160 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002161 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08002162 }
2163
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00002164 strlcpy(tz->type, type ? : "", sizeof(tz->type));
Zhang Rui203d3d42008-01-17 15:51:08 +08002165 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05302166 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08002167 tz->device.class = &thermal_class;
2168 tz->devdata = devdata;
2169 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00002170 tz->passive_delay = passive_delay;
2171 tz->polling_delay = polling_delay;
Chen Yu4511f712015-10-30 16:32:10 +08002172 /* A new thermal zone needs to be updated anyway. */
2173 atomic_set(&tz->need_update, 1);
Matthew Garrettb1569e92008-12-03 17:55:32 +00002174
Kay Sievers354655e2009-01-06 10:44:37 -08002175 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08002176 result = device_register(&tz->device);
2177 if (result) {
2178 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
2179 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002180 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08002181 }
2182
2183 /* sys I/F */
2184 if (type) {
2185 result = device_create_file(&tz->device, &dev_attr_type);
2186 if (result)
2187 goto unregister;
2188 }
2189
2190 result = device_create_file(&tz->device, &dev_attr_temp);
2191 if (result)
2192 goto unregister;
2193
2194 if (ops->get_mode) {
2195 result = device_create_file(&tz->device, &dev_attr_mode);
2196 if (result)
2197 goto unregister;
2198 }
2199
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002200 result = create_trip_attrs(tz, mask);
2201 if (result)
2202 goto unregister;
2203
Zhang Rui203d3d42008-01-17 15:51:08 +08002204 for (count = 0; count < trips; count++) {
Zhang Rui81ad4272016-03-18 10:03:24 +08002205 if (tz->ops->get_trip_type(tz, count, &trip_type))
2206 set_bit(count, &tz->trips_disabled);
Matthew Garrett03a971a2008-12-03 18:00:38 +00002207 if (trip_type == THERMAL_TRIP_PASSIVE)
2208 passive = 1;
Zhang Rui81ad4272016-03-18 10:03:24 +08002209 if (tz->ops->get_trip_temp(tz, count, &trip_temp))
2210 set_bit(count, &tz->trips_disabled);
2211 /* Check for bogus trip points */
2212 if (trip_temp == 0)
2213 set_bit(count, &tz->trips_disabled);
Zhang Rui203d3d42008-01-17 15:51:08 +08002214 }
2215
Durgadoss R5a2c0902012-09-18 11:04:58 +05302216 if (!passive) {
2217 result = device_create_file(&tz->device, &dev_attr_passive);
2218 if (result)
2219 goto unregister;
2220 }
Ram Chandrasekardc814ca2017-07-10 15:23:47 -06002221 result = device_create_file(&tz->device, &dev_attr_passive_delay);
2222 if (result)
2223 goto unregister;
2224
2225 result = device_create_file(&tz->device, &dev_attr_polling_delay);
2226 if (result)
2227 goto unregister;
Matthew Garrett03a971a2008-12-03 18:00:38 +00002228
Sascha Hauer79e54212015-07-06 09:46:16 +02002229 if (IS_ENABLED(CONFIG_THERMAL_EMULATION)) {
2230 result = device_create_file(&tz->device, &dev_attr_emul_temp);
2231 if (result)
2232 goto unregister;
2233 }
2234
Durgadoss R5a2c0902012-09-18 11:04:58 +05302235 /* Create policy attribute */
2236 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00002237 if (result)
2238 goto unregister;
2239
Javi Merino9f382712015-03-26 15:53:02 +00002240 /* Add thermal zone params */
2241 result = create_tzp_attrs(&tz->device);
2242 if (result)
2243 goto unregister;
2244
Ni Wade25a0a5c2015-07-14 15:40:56 +08002245 /* Create available_policies attribute */
2246 result = device_create_file(&tz->device, &dev_attr_available_policies);
2247 if (result)
2248 goto unregister;
2249
Durgadoss Ra4a15482012-09-18 11:04:57 +05302250 /* Update 'this' zone's governor information */
2251 mutex_lock(&thermal_governor_lock);
2252
2253 if (tz->tzp)
Javi Merinoe33df1d2015-02-26 19:00:27 +00002254 governor = __find_governor(tz->tzp->governor_name);
Durgadoss Ra4a15482012-09-18 11:04:57 +05302255 else
Javi Merinoe33df1d2015-02-26 19:00:27 +00002256 governor = def_governor;
2257
2258 result = thermal_set_governor(tz, governor);
2259 if (result) {
2260 mutex_unlock(&thermal_governor_lock);
2261 goto unregister;
2262 }
Durgadoss Ra4a15482012-09-18 11:04:57 +05302263
2264 mutex_unlock(&thermal_governor_lock);
2265
Eduardo Valentinccba4ff2013-08-15 11:34:17 -04002266 if (!tz->tzp || !tz->tzp->no_hwmon) {
2267 result = thermal_add_hwmon_sysfs(tz);
2268 if (result)
2269 goto unregister;
2270 }
Zhang Ruie68b16a2008-04-21 16:07:52 +08002271
Zhang Rui203d3d42008-01-17 15:51:08 +08002272 mutex_lock(&thermal_list_lock);
2273 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08002274 mutex_unlock(&thermal_list_lock);
2275
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05302276 /* Bind cooling devices for this zone */
2277 bind_tz(tz);
2278
Ram Chandrasekarb17d98c2017-06-23 10:41:23 -06002279 INIT_DEFERRABLE_WORK(&(tz->poll_queue), thermal_zone_device_check);
Matthew Garrettb1569e92008-12-03 17:55:32 +00002280
Zhang Ruibb431ba2015-10-30 16:31:47 +08002281 thermal_zone_device_reset(tz);
Chen Yu4511f712015-10-30 16:32:10 +08002282 /* Update the new thermal zone and mark it as already updated. */
2283 if (atomic_cmpxchg(&tz->need_update, 1, 0))
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07002284 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Matthew Garrettb1569e92008-12-03 17:55:32 +00002285
Yao Dongdong14015862014-10-28 07:40:25 +00002286 return tz;
Zhang Rui203d3d42008-01-17 15:51:08 +08002287
Joe Perchescaca8b82012-03-21 12:55:02 -07002288unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08002289 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
2290 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002291 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08002292}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00002293EXPORT_SYMBOL_GPL(thermal_zone_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08002294
2295/**
2296 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08002297 * @tz: the thermal zone device to remove
2298 */
2299void thermal_zone_device_unregister(struct thermal_zone_device *tz)
2300{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05302301 int i;
2302 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08002303 struct thermal_cooling_device *cdev;
2304 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08002305
2306 if (!tz)
2307 return;
2308
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05302309 tzp = tz->tzp;
2310
Zhang Rui203d3d42008-01-17 15:51:08 +08002311 mutex_lock(&thermal_list_lock);
2312 list_for_each_entry(pos, &thermal_tz_list, node)
2313 if (pos == tz)
2314 break;
2315 if (pos != tz) {
2316 /* thermal zone device not found */
2317 mutex_unlock(&thermal_list_lock);
2318 return;
2319 }
2320 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05302321
2322 /* Unbind all cdevs associated with 'this' thermal zone */
2323 list_for_each_entry(cdev, &thermal_cdev_list, node) {
2324 if (tz->ops->unbind) {
2325 tz->ops->unbind(tz, cdev);
2326 continue;
2327 }
2328
2329 if (!tzp || !tzp->tbp)
2330 break;
2331
2332 for (i = 0; i < tzp->num_tbps; i++) {
2333 if (tzp->tbp[i].cdev == cdev) {
2334 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
2335 tzp->tbp[i].cdev = NULL;
2336 }
2337 }
2338 }
2339
Zhang Rui203d3d42008-01-17 15:51:08 +08002340 mutex_unlock(&thermal_list_lock);
2341
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -06002342 thermal_zone_device_set_polling(NULL, tz, 0);
Matthew Garrettb1569e92008-12-03 17:55:32 +00002343
Zhang Rui203d3d42008-01-17 15:51:08 +08002344 if (tz->type[0])
2345 device_remove_file(&tz->device, &dev_attr_type);
2346 device_remove_file(&tz->device, &dev_attr_temp);
2347 if (tz->ops->get_mode)
2348 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05302349 device_remove_file(&tz->device, &dev_attr_policy);
Ni Wade25a0a5c2015-07-14 15:40:56 +08002350 device_remove_file(&tz->device, &dev_attr_available_policies);
Ram Chandrasekardc814ca2017-07-10 15:23:47 -06002351 device_remove_file(&tz->device, &dev_attr_passive_delay);
2352 device_remove_file(&tz->device, &dev_attr_polling_delay);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002353 remove_trip_attrs(tz);
Javi Merinoe33df1d2015-02-26 19:00:27 +00002354 thermal_set_governor(tz, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08002355
Zhang Ruie68b16a2008-04-21 16:07:52 +08002356 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08002357 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
2358 idr_destroy(&tz->idr);
2359 mutex_destroy(&tz->lock);
2360 device_unregister(&tz->device);
2361 return;
2362}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00002363EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08002364
Eduardo Valentin63c4d912013-04-05 12:32:28 +00002365/**
2366 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
2367 * @name: thermal zone name to fetch the temperature
2368 *
2369 * When only one zone is found with the passed name, returns a reference to it.
2370 *
2371 * Return: On success returns a reference to an unique thermal zone with
2372 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
2373 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
2374 */
2375struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
2376{
2377 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
2378 unsigned int found = 0;
2379
2380 if (!name)
2381 goto exit;
2382
2383 mutex_lock(&thermal_list_lock);
2384 list_for_each_entry(pos, &thermal_tz_list, node)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -07002385 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
Eduardo Valentin63c4d912013-04-05 12:32:28 +00002386 found++;
2387 ref = pos;
2388 }
2389 mutex_unlock(&thermal_list_lock);
2390
2391 /* nothing has been found, thus an error code for it */
2392 if (found == 0)
2393 ref = ERR_PTR(-ENODEV);
2394 else if (found > 1)
2395 /* Success only when an unique zone is found */
2396 ref = ERR_PTR(-EEXIST);
2397
2398exit:
2399 return ref;
2400}
2401EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
2402
Rajendra Nayak4a7069a2016-05-05 14:21:42 +05302403/**
2404 * thermal_zone_get_slope - return the slope attribute of the thermal zone
2405 * @tz: thermal zone device with the slope attribute
2406 *
2407 * Return: If the thermal zone device has a slope attribute, return it, else
2408 * return 1.
2409 */
2410int thermal_zone_get_slope(struct thermal_zone_device *tz)
2411{
2412 if (tz && tz->tzp)
2413 return tz->tzp->slope;
2414 return 1;
2415}
2416EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
2417
2418/**
2419 * thermal_zone_get_offset - return the offset attribute of the thermal zone
2420 * @tz: thermal zone device with the offset attribute
2421 *
2422 * Return: If the thermal zone device has a offset attribute, return it, else
2423 * return 0.
2424 */
2425int thermal_zone_get_offset(struct thermal_zone_device *tz)
2426{
2427 if (tz && tz->tzp)
2428 return tz->tzp->offset;
2429 return 0;
2430}
2431EXPORT_SYMBOL_GPL(thermal_zone_get_offset);
2432
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002433#ifdef CONFIG_NET
Johannes Berg2a94fe42013-11-19 15:19:39 +01002434static const struct genl_multicast_group thermal_event_mcgrps[] = {
2435 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
2436};
2437
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002438static struct genl_family thermal_event_genl_family = {
2439 .id = GENL_ID_GENERATE,
2440 .name = THERMAL_GENL_FAMILY_NAME,
2441 .version = THERMAL_GENL_VERSION,
2442 .maxattr = THERMAL_GENL_ATTR_MAX,
Johannes Berg2a94fe42013-11-19 15:19:39 +01002443 .mcgrps = thermal_event_mcgrps,
2444 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002445};
2446
Lina Iyer9bf792d2016-07-11 13:27:11 -06002447static int allow_netlink_events;
2448
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00002449int thermal_generate_netlink_event(struct thermal_zone_device *tz,
2450 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05302451{
2452 struct sk_buff *skb;
2453 struct nlattr *attr;
2454 struct thermal_genl_event *thermal_event;
2455 void *msg_header;
2456 int size;
2457 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07002458 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05302459
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00002460 if (!tz)
2461 return -EINVAL;
2462
Lina Iyer9bf792d2016-07-11 13:27:11 -06002463 if (!allow_netlink_events)
2464 return -ENODEV;
2465
R.Durgadoss4cb18722010-10-27 03:33:29 +05302466 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07002467 size = nla_total_size(sizeof(struct thermal_genl_event)) +
2468 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302469
2470 skb = genlmsg_new(size, GFP_ATOMIC);
2471 if (!skb)
2472 return -ENOMEM;
2473
2474 /* add the genetlink message header */
2475 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
2476 &thermal_event_genl_family, 0,
2477 THERMAL_GENL_CMD_EVENT);
2478 if (!msg_header) {
2479 nlmsg_free(skb);
2480 return -ENOMEM;
2481 }
2482
2483 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07002484 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
2485 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05302486
2487 if (!attr) {
2488 nlmsg_free(skb);
2489 return -EINVAL;
2490 }
2491
2492 thermal_event = nla_data(attr);
2493 if (!thermal_event) {
2494 nlmsg_free(skb);
2495 return -EINVAL;
2496 }
2497
2498 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
2499
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00002500 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05302501 thermal_event->event = event;
2502
2503 /* send multicast genetlink message */
Johannes Berg053c0952015-01-16 22:09:00 +01002504 genlmsg_end(skb, msg_header);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302505
Johannes Berg68eb5502013-11-19 15:19:38 +01002506 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01002507 0, GFP_ATOMIC);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302508 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00002509 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302510
2511 return result;
2512}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00002513EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302514
2515static int genetlink_init(void)
2516{
Lina Iyer9bf792d2016-07-11 13:27:11 -06002517 int ret;
2518
2519 ret = genl_register_family(&thermal_event_genl_family);
2520 if (!ret)
2521 allow_netlink_events = true;
2522
2523 return ret;
R.Durgadoss4cb18722010-10-27 03:33:29 +05302524}
2525
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002526static void genetlink_exit(void)
2527{
2528 genl_unregister_family(&thermal_event_genl_family);
2529}
2530#else /* !CONFIG_NET */
2531static inline int genetlink_init(void) { return 0; }
2532static inline void genetlink_exit(void) {}
Lina Iyer9bf792d2016-07-11 13:27:11 -06002533static inline int thermal_generate_netlink_event(struct thermal_zone_device *tz,
2534 enum events event) { return -ENODEV; }
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002535#endif /* !CONFIG_NET */
2536
Zhang Rui80a26a52013-03-26 16:38:29 +08002537static int __init thermal_register_governors(void)
2538{
2539 int result;
2540
2541 result = thermal_gov_step_wise_register();
2542 if (result)
2543 return result;
2544
2545 result = thermal_gov_fair_share_register();
2546 if (result)
2547 return result;
2548
Peter Feuerere4dbf982014-07-22 17:37:13 +02002549 result = thermal_gov_bang_bang_register();
2550 if (result)
2551 return result;
2552
Javi Merino6b775e82015-03-02 17:17:19 +00002553 result = thermal_gov_user_space_register();
2554 if (result)
2555 return result;
2556
Ram Chandrasekar6d44a342016-07-07 11:09:52 -06002557 result = thermal_gov_low_limits_register();
2558 if (result)
2559 return result;
2560
Javi Merino6b775e82015-03-02 17:17:19 +00002561 return thermal_gov_power_allocator_register();
Zhang Rui80a26a52013-03-26 16:38:29 +08002562}
2563
2564static void thermal_unregister_governors(void)
2565{
2566 thermal_gov_step_wise_unregister();
2567 thermal_gov_fair_share_unregister();
Peter Feuerere4dbf982014-07-22 17:37:13 +02002568 thermal_gov_bang_bang_unregister();
Zhang Rui80a26a52013-03-26 16:38:29 +08002569 thermal_gov_user_space_unregister();
Ram Chandrasekar6d44a342016-07-07 11:09:52 -06002570 thermal_gov_low_limits_unregister();
Javi Merino6b775e82015-03-02 17:17:19 +00002571 thermal_gov_power_allocator_unregister();
Zhang Rui80a26a52013-03-26 16:38:29 +08002572}
2573
Zhang Ruiff140fe2015-10-30 16:31:58 +08002574static int thermal_pm_notify(struct notifier_block *nb,
2575 unsigned long mode, void *_unused)
2576{
2577 struct thermal_zone_device *tz;
2578
2579 switch (mode) {
2580 case PM_HIBERNATION_PREPARE:
2581 case PM_RESTORE_PREPARE:
2582 case PM_SUSPEND_PREPARE:
2583 atomic_set(&in_suspend, 1);
2584 break;
2585 case PM_POST_HIBERNATION:
2586 case PM_POST_RESTORE:
2587 case PM_POST_SUSPEND:
2588 atomic_set(&in_suspend, 0);
2589 list_for_each_entry(tz, &thermal_tz_list, node) {
Ram Chandrasekar79043192017-09-28 13:23:36 -06002590 mutex_lock(&tz->lock);
Zhang Ruiff140fe2015-10-30 16:31:58 +08002591 thermal_zone_device_reset(tz);
Ram Chandrasekar79043192017-09-28 13:23:36 -06002592 mod_delayed_work(system_freezable_power_efficient_wq,
2593 &tz->poll_queue, 0);
2594 mutex_unlock(&tz->lock);
Zhang Ruiff140fe2015-10-30 16:31:58 +08002595 }
2596 break;
2597 default:
2598 break;
2599 }
2600 return 0;
2601}
2602
2603static struct notifier_block thermal_pm_nb = {
2604 .notifier_call = thermal_pm_notify,
2605};
2606
Zhang Rui203d3d42008-01-17 15:51:08 +08002607static int __init thermal_init(void)
2608{
Zhang Rui80a26a52013-03-26 16:38:29 +08002609 int result;
2610
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -06002611 thermal_passive_wq = alloc_workqueue("thermal_passive_wq",
2612 WQ_HIGHPRI | WQ_UNBOUND
2613 | WQ_FREEZABLE,
2614 THERMAL_MAX_ACTIVE);
2615 if (!thermal_passive_wq) {
2616 result = -ENOMEM;
2617 goto init_exit;
2618 }
2619
Zhang Rui80a26a52013-03-26 16:38:29 +08002620 result = thermal_register_governors();
2621 if (result)
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -06002622 goto destroy_wq;
Zhang Rui203d3d42008-01-17 15:51:08 +08002623
2624 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08002625 if (result)
2626 goto unregister_governors;
2627
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002628 result = of_parse_thermal_zones();
2629 if (result)
Lina Iyer9bf792d2016-07-11 13:27:11 -06002630 goto exit_zone_parse;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002631
Zhang Ruiff140fe2015-10-30 16:31:58 +08002632 result = register_pm_notifier(&thermal_pm_nb);
2633 if (result)
2634 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
2635 result);
2636
Zhang Rui80a26a52013-03-26 16:38:29 +08002637 return 0;
2638
Lina Iyer9bf792d2016-07-11 13:27:11 -06002639exit_zone_parse:
Zhang Rui80a26a52013-03-26 16:38:29 +08002640 class_unregister(&thermal_class);
Luis Henriques9d367e52014-12-03 21:20:21 +00002641unregister_governors:
2642 thermal_unregister_governors();
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -06002643destroy_wq:
2644 destroy_workqueue(thermal_passive_wq);
Lina Iyer9bf792d2016-07-11 13:27:11 -06002645init_exit:
Zhang Rui80a26a52013-03-26 16:38:29 +08002646 idr_destroy(&thermal_tz_idr);
2647 idr_destroy(&thermal_cdev_idr);
2648 mutex_destroy(&thermal_idr_lock);
2649 mutex_destroy(&thermal_list_lock);
2650 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08002651 return result;
2652}
2653
Ram Chandrasekarfe0ae222017-04-06 12:08:38 -06002654static void thermal_exit(void)
Zhang Rui203d3d42008-01-17 15:51:08 +08002655{
Zhang Ruiff140fe2015-10-30 16:31:58 +08002656 unregister_pm_notifier(&thermal_pm_nb);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002657 of_thermal_destroy_zones();
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -06002658 destroy_workqueue(thermal_passive_wq);
Zhang Rui80a26a52013-03-26 16:38:29 +08002659 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08002660 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08002661 thermal_unregister_governors();
Zhang Rui203d3d42008-01-17 15:51:08 +08002662 idr_destroy(&thermal_tz_idr);
2663 idr_destroy(&thermal_cdev_idr);
2664 mutex_destroy(&thermal_idr_lock);
2665 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08002666 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08002667}
2668
Lina Iyer9bf792d2016-07-11 13:27:11 -06002669static int __init thermal_netlink_init(void)
2670{
2671 int ret = 0;
2672
2673 ret = genetlink_init();
2674 if (!ret)
2675 goto exit_netlink;
2676
2677 thermal_exit();
2678exit_netlink:
2679 return ret;
2680}
2681
2682subsys_initcall(thermal_init);
2683fs_initcall(thermal_netlink_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08002684module_exit(thermal_exit);