blob: f8a9a2f2c28db25d2d6c77c1a64f69eb8b81f8ee [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 Chandrasekare30952e2017-11-29 15:31:08 -07001328 mutex_lock(&cdev->lock);
Ram Chandrasekar5ab1c082016-09-20 17:09:28 -06001329 cdev->sysfs_cur_state_req = state;
1330
1331 cdev->updated = false;
Ram Chandrasekare30952e2017-11-29 15:31:08 -07001332 mutex_unlock(&cdev->lock);
Ram Chandrasekar5ab1c082016-09-20 17:09:28 -06001333 thermal_cdev_update(cdev);
1334
Zhang Rui203d3d42008-01-17 15:51:08 +08001335 return count;
1336}
1337
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001338static ssize_t
1339thermal_cooling_device_min_state_store(struct device *dev,
1340 struct device_attribute *attr,
1341 const char *buf, size_t count)
1342{
1343 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1344 long state;
1345 int ret = 0;
1346
1347 ret = sscanf(buf, "%ld\n", &state);
1348 if (ret <= 0)
1349 return (ret < 0) ? ret : -EINVAL;
1350
1351 if ((long)state < 0)
1352 return -EINVAL;
1353
Ram Chandrasekare30952e2017-11-29 15:31:08 -07001354 mutex_lock(&cdev->lock);
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001355 cdev->sysfs_min_state_req = state;
1356
1357 cdev->updated = false;
Ram Chandrasekare30952e2017-11-29 15:31:08 -07001358 mutex_unlock(&cdev->lock);
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001359 thermal_cdev_update(cdev);
1360
1361 return count;
1362}
1363
Zhang Rui203d3d42008-01-17 15:51:08 +08001364static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -05001365__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001366static DEVICE_ATTR(max_state, 0444,
1367 thermal_cooling_device_max_state_show, NULL);
1368static DEVICE_ATTR(cur_state, 0644,
1369 thermal_cooling_device_cur_state_show,
1370 thermal_cooling_device_cur_state_store);
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001371static DEVICE_ATTR(min_state, 0644,
1372 thermal_cooling_device_min_state_show,
1373 thermal_cooling_device_min_state_store);
Zhang Rui203d3d42008-01-17 15:51:08 +08001374
1375static ssize_t
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001376thermal_cooling_device_lower_limit_show(struct device *dev,
1377 struct device_attribute *attr, char *buf)
1378{
1379 struct thermal_instance *instance;
1380
1381 instance =
1382 container_of(attr, struct thermal_instance, lower_attr);
1383
1384 return snprintf(buf, PAGE_SIZE, "%lu\n", instance->lower);
1385}
1386
1387static ssize_t
1388thermal_cooling_device_upper_limit_show(struct device *dev,
1389 struct device_attribute *attr, char *buf)
1390{
1391 struct thermal_instance *instance;
1392
1393 instance =
1394 container_of(attr, struct thermal_instance, upper_attr);
1395
1396 return snprintf(buf, PAGE_SIZE, "%lu\n", instance->upper);
1397}
1398
1399static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +08001400thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -05001401 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +08001402{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001403 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +08001404
1405 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001406 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001407
1408 if (instance->trip == THERMAL_TRIPS_NONE)
1409 return sprintf(buf, "-1\n");
1410 else
1411 return sprintf(buf, "%d\n", instance->trip);
1412}
1413
Ram Chandrasekardc814ca2017-07-10 15:23:47 -06001414static ssize_t
1415thermal_cooling_device_upper_limit_store(struct device *dev,
1416 struct device_attribute *attr,
1417 const char *buf, size_t count)
1418{
1419 struct thermal_zone_device *tz = to_thermal_zone(dev);
1420 struct thermal_instance *instance;
1421 int ret, upper_limit;
1422
1423 instance =
1424 container_of(attr, struct thermal_instance, upper_attr);
1425
1426 ret = kstrtoint(buf, 0, &upper_limit);
1427 if (ret)
1428 return ret;
1429 if (upper_limit < instance->lower)
1430 return -EINVAL;
1431
1432 instance->upper = upper_limit;
1433 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1434
1435 return count;
1436}
1437
1438static ssize_t
1439thermal_cooling_device_lower_limit_store(struct device *dev,
1440 struct device_attribute *attr,
1441 const char *buf, size_t count)
1442{
1443 struct thermal_zone_device *tz = to_thermal_zone(dev);
1444 struct thermal_instance *instance;
1445 int ret, lower_limit;
1446
1447 instance =
1448 container_of(attr, struct thermal_instance, lower_attr);
1449
1450 ret = kstrtoint(buf, 0, &lower_limit);
1451 if (ret)
1452 return ret;
1453 if (lower_limit > instance->upper)
1454 return -EINVAL;
1455
1456 instance->lower = lower_limit;
1457 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
1458
1459 return count;
1460}
1461
Matthias Kaehlcke2dc10f82015-02-20 18:10:08 -08001462static struct attribute *cooling_device_attrs[] = {
1463 &dev_attr_cdev_type.attr,
1464 &dev_attr_max_state.attr,
1465 &dev_attr_cur_state.attr,
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001466 &dev_attr_min_state.attr,
Matthias Kaehlcke2dc10f82015-02-20 18:10:08 -08001467 NULL,
1468};
1469
1470static const struct attribute_group cooling_device_attr_group = {
1471 .attrs = cooling_device_attrs,
1472};
1473
1474static const struct attribute_group *cooling_device_attr_groups[] = {
1475 &cooling_device_attr_group,
1476 NULL,
1477};
1478
Javi Merinodb916512015-02-18 16:04:24 +00001479static ssize_t
1480thermal_cooling_device_weight_show(struct device *dev,
1481 struct device_attribute *attr, char *buf)
1482{
1483 struct thermal_instance *instance;
1484
1485 instance = container_of(attr, struct thermal_instance, weight_attr);
1486
1487 return sprintf(buf, "%d\n", instance->weight);
1488}
1489
1490static ssize_t
1491thermal_cooling_device_weight_store(struct device *dev,
1492 struct device_attribute *attr,
1493 const char *buf, size_t count)
1494{
1495 struct thermal_instance *instance;
1496 int ret, weight;
1497
1498 ret = kstrtoint(buf, 0, &weight);
1499 if (ret)
1500 return ret;
1501
1502 instance = container_of(attr, struct thermal_instance, weight_attr);
1503 instance->weight = weight;
1504
1505 return count;
1506}
Zhang Rui203d3d42008-01-17 15:51:08 +08001507/* Device management */
1508
1509/**
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001510 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
1511 * @tz: pointer to struct thermal_zone_device
Zhang Rui203d3d42008-01-17 15:51:08 +08001512 * @trip: indicates which trip point the cooling devices is
1513 * associated with in this thermal zone.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001514 * @cdev: pointer to struct thermal_cooling_device
1515 * @upper: the Maximum cooling state for this trip point.
1516 * THERMAL_NO_LIMIT means no upper limit,
1517 * and the cooling device can be in max_state.
1518 * @lower: the Minimum cooling state can be used for this trip point.
1519 * THERMAL_NO_LIMIT means no lower limit,
1520 * and the cooling device can be in cooling state 0.
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001521 * @weight: The weight of the cooling device to be bound to the
1522 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
1523 * default value
Len Brown543a9562008-02-07 16:55:08 -05001524 *
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001525 * This interface function bind a thermal cooling device to the certain trip
1526 * point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001527 * This function is usually called in the thermal zone device .bind callback.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001528 *
1529 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001530 */
1531int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1532 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +08001533 struct thermal_cooling_device *cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001534 unsigned long upper, unsigned long lower,
1535 unsigned int weight)
Zhang Rui203d3d42008-01-17 15:51:08 +08001536{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001537 struct thermal_instance *dev;
1538 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -05001539 struct thermal_zone_device *pos1;
1540 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +08001541 unsigned long max_state;
Lukasz Majewski9a3031d2014-11-18 11:16:30 +01001542 int result, ret;
Zhang Rui203d3d42008-01-17 15:51:08 +08001543
Len Brown543a9562008-02-07 16:55:08 -05001544 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +08001545 return -EINVAL;
1546
Thomas Sujithc7516702008-02-15 00:58:50 -05001547 list_for_each_entry(pos1, &thermal_tz_list, node) {
1548 if (pos1 == tz)
1549 break;
1550 }
1551 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1552 if (pos2 == cdev)
1553 break;
1554 }
1555
1556 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +08001557 return -EINVAL;
1558
Lukasz Majewski9a3031d2014-11-18 11:16:30 +01001559 ret = cdev->ops->get_max_state(cdev, &max_state);
1560 if (ret)
1561 return ret;
Zhang Rui9d998422012-06-26 16:35:57 +08001562
Ram Chandrasekar2a1abd12017-06-12 12:52:08 -06001563 /*
1564 * If upper or lower has a MACRO to define the mitigation state,
1565 * based on the MACRO determine the default state to use or the
1566 * offset from the max_state.
1567 */
1568 if (upper > (THERMAL_MAX_LIMIT - max_state)) {
1569 /* upper default max_state */
1570 if (upper == THERMAL_NO_LIMIT)
1571 upper = max_state;
1572 else
1573 upper = max_state - (THERMAL_MAX_LIMIT - upper);
1574 }
1575
1576 if (lower > (THERMAL_MAX_LIMIT - max_state)) {
1577 /* lower default 0 */
1578 if (lower == THERMAL_NO_LIMIT)
1579 lower = 0;
1580 else
1581 lower = max_state - (THERMAL_MAX_LIMIT - lower);
1582 }
Zhang Rui9d998422012-06-26 16:35:57 +08001583
1584 if (lower > upper || upper > max_state)
1585 return -EINVAL;
1586
Zhang Rui203d3d42008-01-17 15:51:08 +08001587 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001588 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001589 if (!dev)
1590 return -ENOMEM;
1591 dev->tz = tz;
1592 dev->cdev = cdev;
1593 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +08001594 dev->upper = upper;
1595 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +08001596 dev->target = THERMAL_NO_TARGET;
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001597 dev->weight = weight;
Zhang Rui74051ba2012-06-26 16:27:22 +08001598
Zhang Rui203d3d42008-01-17 15:51:08 +08001599 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1600 if (result)
1601 goto free_mem;
1602
1603 sprintf(dev->name, "cdev%d", dev->id);
1604 result =
1605 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1606 if (result)
1607 goto release_idr;
1608
1609 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -07001610 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001611 dev->attr.attr.name = dev->attr_name;
1612 dev->attr.attr.mode = 0444;
1613 dev->attr.show = thermal_cooling_device_trip_point_show;
1614 result = device_create_file(&tz->device, &dev->attr);
1615 if (result)
1616 goto remove_symbol_link;
1617
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001618 snprintf(dev->upper_attr_name, THERMAL_NAME_LENGTH,
1619 "cdev%d_upper_limit", dev->id);
1620 sysfs_attr_init(&dev->upper_attr.attr);
1621 dev->upper_attr.attr.name = dev->upper_attr_name;
Ram Chandrasekardc814ca2017-07-10 15:23:47 -06001622 dev->upper_attr.attr.mode = 0644;
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001623 dev->upper_attr.show = thermal_cooling_device_upper_limit_show;
Ram Chandrasekardc814ca2017-07-10 15:23:47 -06001624 dev->upper_attr.store = thermal_cooling_device_upper_limit_store;
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001625 result = device_create_file(&tz->device, &dev->upper_attr);
1626 if (result)
1627 goto remove_trip_file;
1628
1629 snprintf(dev->lower_attr_name, THERMAL_NAME_LENGTH,
1630 "cdev%d_lower_limit", dev->id);
1631 sysfs_attr_init(&dev->lower_attr.attr);
1632 dev->lower_attr.attr.name = dev->lower_attr_name;
Ram Chandrasekardc814ca2017-07-10 15:23:47 -06001633 dev->lower_attr.attr.mode = 0644;
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001634 dev->lower_attr.show = thermal_cooling_device_lower_limit_show;
Ram Chandrasekardc814ca2017-07-10 15:23:47 -06001635 dev->lower_attr.store = thermal_cooling_device_lower_limit_store;
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001636 result = device_create_file(&tz->device, &dev->lower_attr);
1637 if (result)
1638 goto remove_upper_file;
1639
Javi Merinodb916512015-02-18 16:04:24 +00001640 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
1641 sysfs_attr_init(&dev->weight_attr.attr);
1642 dev->weight_attr.attr.name = dev->weight_attr_name;
1643 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
1644 dev->weight_attr.show = thermal_cooling_device_weight_show;
1645 dev->weight_attr.store = thermal_cooling_device_weight_store;
1646 result = device_create_file(&tz->device, &dev->weight_attr);
1647 if (result)
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001648 goto remove_lower_file;
Javi Merinodb916512015-02-18 16:04:24 +00001649
Zhang Rui203d3d42008-01-17 15:51:08 +08001650 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001651 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001652 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +08001653 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1654 result = -EEXIST;
1655 break;
1656 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001657 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001658 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001659 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
Chen Yu4511f712015-10-30 16:32:10 +08001660 atomic_set(&tz->need_update, 1);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001661 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001662 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001663 mutex_unlock(&tz->lock);
1664
1665 if (!result)
1666 return 0;
1667
Javi Merinodb916512015-02-18 16:04:24 +00001668 device_remove_file(&tz->device, &dev->weight_attr);
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001669remove_lower_file:
1670 device_remove_file(&tz->device, &dev->lower_attr);
1671remove_upper_file:
1672 device_remove_file(&tz->device, &dev->upper_attr);
Javi Merinodb916512015-02-18 16:04:24 +00001673remove_trip_file:
Zhang Rui203d3d42008-01-17 15:51:08 +08001674 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -07001675remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +08001676 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -07001677release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +08001678 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -07001679free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +08001680 kfree(dev);
1681 return result;
1682}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001683EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001684
1685/**
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001686 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1687 * thermal zone.
1688 * @tz: pointer to a struct thermal_zone_device.
Zhang Rui203d3d42008-01-17 15:51:08 +08001689 * @trip: indicates which trip point the cooling devices is
1690 * associated with in this thermal zone.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001691 * @cdev: pointer to a struct thermal_cooling_device.
Len Brown543a9562008-02-07 16:55:08 -05001692 *
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001693 * This interface function unbind a thermal cooling device from the certain
1694 * trip point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001695 * This function is usually called in the thermal zone device .unbind callback.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001696 *
1697 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001698 */
1699int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1700 int trip,
1701 struct thermal_cooling_device *cdev)
1702{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001703 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +08001704
1705 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001706 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001707 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -05001708 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001709 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001710 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001711 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001712 mutex_unlock(&tz->lock);
1713 goto unbind;
1714 }
1715 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001716 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001717 mutex_unlock(&tz->lock);
1718
1719 return -ENODEV;
1720
Joe Perchescaca8b82012-03-21 12:55:02 -07001721unbind:
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001722 device_remove_file(&tz->device, &pos->lower_attr);
1723 device_remove_file(&tz->device, &pos->upper_attr);
Viresh Kumar528464e2015-07-23 14:32:32 +05301724 device_remove_file(&tz->device, &pos->weight_attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001725 device_remove_file(&tz->device, &pos->attr);
1726 sysfs_remove_link(&tz->device.kobj, pos->name);
1727 release_idr(&tz->idr, &tz->lock, pos->id);
1728 kfree(pos);
1729 return 0;
1730}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001731EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001732
1733static void thermal_release(struct device *dev)
1734{
1735 struct thermal_zone_device *tz;
1736 struct thermal_cooling_device *cdev;
1737
Joe Perchescaca8b82012-03-21 12:55:02 -07001738 if (!strncmp(dev_name(dev), "thermal_zone",
1739 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001740 tz = to_thermal_zone(dev);
1741 kfree(tz);
durgadoss.r@intel.com732e4c82013-10-02 00:08:00 +05301742 } else if(!strncmp(dev_name(dev), "cooling_device",
1743 sizeof("cooling_device") - 1)){
Zhang Rui203d3d42008-01-17 15:51:08 +08001744 cdev = to_cooling_device(dev);
1745 kfree(cdev);
1746 }
1747}
1748
1749static struct class thermal_class = {
1750 .name = "thermal",
1751 .dev_release = thermal_release,
1752};
1753
1754/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001755 * __thermal_cooling_device_register() - register a new thermal cooling device
1756 * @np: a pointer to a device tree node.
Zhang Rui203d3d42008-01-17 15:51:08 +08001757 * @type: the thermal cooling device type.
1758 * @devdata: device private data.
1759 * @ops: standard thermal cooling devices callbacks.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001760 *
1761 * This interface function adds a new thermal cooling device (fan/processor/...)
1762 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1763 * to all the thermal zone devices registered at the same time.
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001764 * It also gives the opportunity to link the cooling device to a device tree
1765 * node, so that it can be bound to a thermal zone created out of device tree.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001766 *
1767 * Return: a pointer to the created struct thermal_cooling_device or an
1768 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001769 */
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001770static struct thermal_cooling_device *
1771__thermal_cooling_device_register(struct device_node *np,
1772 char *type, void *devdata,
1773 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001774{
1775 struct thermal_cooling_device *cdev;
Chen Yu4511f712015-10-30 16:32:10 +08001776 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001777 int result;
1778
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001779 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001780 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001781
1782 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001783 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001784 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001785
1786 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1787 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001788 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001789
1790 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1791 if (result) {
1792 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001793 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001794 }
1795
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001796 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +08001797 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001798 INIT_LIST_HEAD(&cdev->thermal_instances);
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001799 cdev->np = np;
Zhang Rui203d3d42008-01-17 15:51:08 +08001800 cdev->ops = ops;
Ni Wade5ca0cce2014-02-17 11:02:55 +08001801 cdev->updated = false;
Zhang Rui203d3d42008-01-17 15:51:08 +08001802 cdev->device.class = &thermal_class;
Matthias Kaehlcke2dc10f82015-02-20 18:10:08 -08001803 cdev->device.groups = cooling_device_attr_groups;
Zhang Rui203d3d42008-01-17 15:51:08 +08001804 cdev->devdata = devdata;
Ram Chandrasekar5ab1c082016-09-20 17:09:28 -06001805 cdev->sysfs_cur_state_req = 0;
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001806 cdev->sysfs_min_state_req = ULONG_MAX;
Kay Sievers354655e2009-01-06 10:44:37 -08001807 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001808 result = device_register(&cdev->device);
1809 if (result) {
1810 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1811 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001812 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001813 }
1814
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301815 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +08001816 mutex_lock(&thermal_list_lock);
1817 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001818 mutex_unlock(&thermal_list_lock);
1819
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301820 /* Update binding information for 'this' new cdev */
1821 bind_cdev(cdev);
1822
Chen Yu4511f712015-10-30 16:32:10 +08001823 mutex_lock(&thermal_list_lock);
1824 list_for_each_entry(pos, &thermal_tz_list, node)
1825 if (atomic_cmpxchg(&pos->need_update, 1, 0))
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07001826 thermal_zone_device_update(pos,
1827 THERMAL_EVENT_UNSPECIFIED);
Chen Yu4511f712015-10-30 16:32:10 +08001828 mutex_unlock(&thermal_list_lock);
1829
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301830 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001831}
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001832
1833/**
1834 * thermal_cooling_device_register() - register a new thermal cooling device
1835 * @type: the thermal cooling device type.
1836 * @devdata: device private data.
1837 * @ops: standard thermal cooling devices callbacks.
1838 *
1839 * This interface function adds a new thermal cooling device (fan/processor/...)
1840 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1841 * to all the thermal zone devices registered at the same time.
1842 *
1843 * Return: a pointer to the created struct thermal_cooling_device or an
1844 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1845 */
1846struct thermal_cooling_device *
1847thermal_cooling_device_register(char *type, void *devdata,
1848 const struct thermal_cooling_device_ops *ops)
1849{
1850 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1851}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001852EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001853
1854/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001855 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1856 * @np: a pointer to a device tree node.
1857 * @type: the thermal cooling device type.
1858 * @devdata: device private data.
1859 * @ops: standard thermal cooling devices callbacks.
1860 *
1861 * This function will register a cooling device with device tree node reference.
1862 * This interface function adds a new thermal cooling device (fan/processor/...)
1863 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1864 * to all the thermal zone devices registered at the same time.
1865 *
1866 * Return: a pointer to the created struct thermal_cooling_device or an
1867 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1868 */
1869struct thermal_cooling_device *
1870thermal_of_cooling_device_register(struct device_node *np,
1871 char *type, void *devdata,
1872 const struct thermal_cooling_device_ops *ops)
1873{
1874 return __thermal_cooling_device_register(np, type, devdata, ops);
1875}
1876EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1877
1878/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001879 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001880 * @cdev: the thermal cooling device to remove.
1881 *
1882 * thermal_cooling_device_unregister() must be called when the device is no
1883 * longer needed.
1884 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301885void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001886{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301887 int i;
1888 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001889 struct thermal_zone_device *tz;
1890 struct thermal_cooling_device *pos = NULL;
1891
1892 if (!cdev)
1893 return;
1894
1895 mutex_lock(&thermal_list_lock);
1896 list_for_each_entry(pos, &thermal_cdev_list, node)
1897 if (pos == cdev)
1898 break;
1899 if (pos != cdev) {
1900 /* thermal cooling device not found */
1901 mutex_unlock(&thermal_list_lock);
1902 return;
1903 }
1904 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301905
1906 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001907 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301908 if (tz->ops->unbind) {
1909 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001910 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301911 }
1912
1913 if (!tz->tzp || !tz->tzp->tbp)
1914 continue;
1915
1916 tzp = tz->tzp;
1917 for (i = 0; i < tzp->num_tbps; i++) {
1918 if (tzp->tbp[i].cdev == cdev) {
1919 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1920 tzp->tbp[i].cdev = NULL;
1921 }
1922 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001923 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301924
Zhang Rui203d3d42008-01-17 15:51:08 +08001925 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301926
Zhang Rui203d3d42008-01-17 15:51:08 +08001927 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001928 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001929 device_remove_file(&cdev->device, &dev_attr_max_state);
1930 device_remove_file(&cdev->device, &dev_attr_cur_state);
1931
1932 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1933 device_unregister(&cdev->device);
1934 return;
1935}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001936EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001937
Durgadoss Rdc765482012-09-18 11:05:00 +05301938void thermal_cdev_update(struct thermal_cooling_device *cdev)
Zhang Ruice119f82012-06-27 14:13:04 +08001939{
1940 struct thermal_instance *instance;
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001941 unsigned long current_target = 0, min_target = ULONG_MAX;
Zhang Ruice119f82012-06-27 14:13:04 +08001942
Zhang Ruif4a821c2012-07-24 16:56:21 +08001943 mutex_lock(&cdev->lock);
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01001944 /* cooling device is updated*/
1945 if (cdev->updated) {
1946 mutex_unlock(&cdev->lock);
1947 return;
1948 }
1949
Zhang Ruice119f82012-06-27 14:13:04 +08001950 /* Make sure cdev enters the deepest cooling state */
Ram Chandrasekar5ab1c082016-09-20 17:09:28 -06001951 current_target = cdev->sysfs_cur_state_req;
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001952 min_target = cdev->sysfs_min_state_req;
Zhang Ruice119f82012-06-27 14:13:04 +08001953 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
Aaron Lu06475b52013-12-02 13:54:26 +08001954 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1955 instance->tz->id, instance->target);
Zhang Ruice119f82012-06-27 14:13:04 +08001956 if (instance->target == THERMAL_NO_TARGET)
1957 continue;
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001958 if (instance->tz->governor->min_state_throttle) {
1959 if (instance->target < min_target)
1960 min_target = instance->target;
1961 } else {
1962 if (instance->target > current_target)
1963 current_target = instance->target;
1964 }
Zhang Ruice119f82012-06-27 14:13:04 +08001965 }
Ram Chandrasekar4185d52d2017-07-13 17:27:50 -06001966 trace_cdev_update_start(cdev);
Ram Chandrasekar5ab1c082016-09-20 17:09:28 -06001967 cdev->ops->set_cur_state(cdev, current_target);
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001968 if (cdev->ops->set_min_state)
1969 cdev->ops->set_min_state(cdev, min_target);
Zhang Ruice119f82012-06-27 14:13:04 +08001970 cdev->updated = true;
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01001971 mutex_unlock(&cdev->lock);
Ram Chandrasekar5b1c0d12017-03-28 11:35:40 -06001972 trace_cdev_update(cdev, current_target, min_target);
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001973 dev_dbg(&cdev->device, "set to state %lu min state %lu\n",
1974 current_target, min_target);
Zhang Ruice119f82012-06-27 14:13:04 +08001975}
Durgadoss Rdc765482012-09-18 11:05:00 +05301976EXPORT_SYMBOL(thermal_cdev_update);
Zhang Ruice119f82012-06-27 14:13:04 +08001977
Matthew Garrettb1569e92008-12-03 17:55:32 +00001978/**
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001979 * thermal_notify_framework - Sensor drivers use this API to notify framework
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301980 * @tz: thermal zone device
1981 * @trip: indicates which trip point has been crossed
1982 *
1983 * This function handles the trip events from sensor drivers. It starts
1984 * throttling the cooling devices according to the policy configured.
1985 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1986 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1987 * The throttling policy is based on the configured platform data; if no
1988 * platform data is provided, this uses the step_wise throttling policy.
1989 */
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001990void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301991{
1992 handle_thermal_trip(tz, trip);
1993}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001994EXPORT_SYMBOL_GPL(thermal_notify_framework);
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301995
1996/**
Eduardo Valentin269c1742013-04-23 21:48:19 +00001997 * create_trip_attrs() - create attributes for trip points
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001998 * @tz: the thermal zone device
1999 * @mask: Writeable trip point bitmap.
Eduardo Valentin269c1742013-04-23 21:48:19 +00002000 *
2001 * helper function to instantiate sysfs entries for every trip
2002 * point and its properties of a struct thermal_zone_device.
2003 *
2004 * Return: 0 on success, the proper error value otherwise.
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002005 */
2006static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
2007{
2008 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08002009 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002010
Durgadoss R27365a62012-07-25 10:10:59 +08002011 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002012 if (!tz->trip_type_attrs)
2013 return -ENOMEM;
2014
Durgadoss R27365a62012-07-25 10:10:59 +08002015 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002016 if (!tz->trip_temp_attrs) {
2017 kfree(tz->trip_type_attrs);
2018 return -ENOMEM;
2019 }
2020
Durgadoss R27365a62012-07-25 10:10:59 +08002021 if (tz->ops->get_trip_hyst) {
2022 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
2023 if (!tz->trip_hyst_attrs) {
2024 kfree(tz->trip_type_attrs);
2025 kfree(tz->trip_temp_attrs);
2026 return -ENOMEM;
2027 }
2028 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002029
Durgadoss R27365a62012-07-25 10:10:59 +08002030
2031 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002032 /* create trip type attribute */
2033 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
2034 "trip_point_%d_type", indx);
2035
2036 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
2037 tz->trip_type_attrs[indx].attr.attr.name =
2038 tz->trip_type_attrs[indx].name;
2039 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
2040 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
2041
2042 device_create_file(&tz->device,
2043 &tz->trip_type_attrs[indx].attr);
2044
2045 /* create trip temp attribute */
2046 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
2047 "trip_point_%d_temp", indx);
2048
2049 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
2050 tz->trip_temp_attrs[indx].attr.attr.name =
2051 tz->trip_temp_attrs[indx].name;
2052 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
2053 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
Punit Agrawal35e94642015-03-03 10:43:03 +00002054 if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS) &&
2055 mask & (1 << indx)) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002056 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
2057 tz->trip_temp_attrs[indx].attr.store =
2058 trip_point_temp_store;
2059 }
2060
2061 device_create_file(&tz->device,
2062 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08002063
2064 /* create Optional trip hyst attribute */
2065 if (!tz->ops->get_trip_hyst)
2066 continue;
2067 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
2068 "trip_point_%d_hyst", indx);
2069
2070 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
2071 tz->trip_hyst_attrs[indx].attr.attr.name =
2072 tz->trip_hyst_attrs[indx].name;
2073 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
2074 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
2075 if (tz->ops->set_trip_hyst) {
2076 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
2077 tz->trip_hyst_attrs[indx].attr.store =
2078 trip_point_hyst_store;
2079 }
2080
2081 device_create_file(&tz->device,
2082 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002083 }
2084 return 0;
2085}
2086
2087static void remove_trip_attrs(struct thermal_zone_device *tz)
2088{
2089 int indx;
2090
2091 for (indx = 0; indx < tz->trips; indx++) {
2092 device_remove_file(&tz->device,
2093 &tz->trip_type_attrs[indx].attr);
2094 device_remove_file(&tz->device,
2095 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08002096 if (tz->ops->get_trip_hyst)
2097 device_remove_file(&tz->device,
2098 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002099 }
2100 kfree(tz->trip_type_attrs);
2101 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08002102 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002103}
2104
2105/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00002106 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08002107 * @type: the thermal zone device type
2108 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002109 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08002110 * @devdata: private device data
2111 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05302112 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00002113 * @passive_delay: number of milliseconds to wait between polls when
2114 * performing passive cooling
2115 * @polling_delay: number of milliseconds to wait between polls when checking
2116 * whether trip points have been crossed (0 for interrupt
2117 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08002118 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00002119 * This interface function adds a new thermal zone device (sensor) to
2120 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
2121 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08002122 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08002123 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00002124 *
2125 * Return: a pointer to the created struct thermal_zone_device or an
2126 * in case of error, an ERR_PTR. Caller must check return value with
2127 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08002128 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07002129struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002130 int trips, int mask, void *devdata,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002131 struct thermal_zone_device_ops *ops,
Javi Merino6b775e82015-03-02 17:17:19 +00002132 struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08002133 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08002134{
2135 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00002136 enum thermal_trip_type trip_type;
Zhang Rui81ad4272016-03-18 10:03:24 +08002137 int trip_temp;
Zhang Rui203d3d42008-01-17 15:51:08 +08002138 int result;
2139 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00002140 int passive = 0;
Javi Merinoe33df1d2015-02-26 19:00:27 +00002141 struct thermal_governor *governor;
Zhang Rui203d3d42008-01-17 15:51:08 +08002142
Guenter Roeck204dd1d2012-08-07 22:36:45 -07002143 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002144 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08002145
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002146 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002147 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08002148
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04002149 if (!ops)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002150 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08002151
Jonghwa Lee83720d02013-05-18 09:50:26 +00002152 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00002153 return ERR_PTR(-EINVAL);
2154
Zhang Rui203d3d42008-01-17 15:51:08 +08002155 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
2156 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002157 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08002158
Zhang Rui2d374132012-06-27 10:09:00 +08002159 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08002160 idr_init(&tz->idr);
2161 mutex_init(&tz->lock);
2162 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
2163 if (result) {
2164 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002165 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08002166 }
2167
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00002168 strlcpy(tz->type, type ? : "", sizeof(tz->type));
Zhang Rui203d3d42008-01-17 15:51:08 +08002169 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05302170 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08002171 tz->device.class = &thermal_class;
2172 tz->devdata = devdata;
2173 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00002174 tz->passive_delay = passive_delay;
2175 tz->polling_delay = polling_delay;
Chen Yu4511f712015-10-30 16:32:10 +08002176 /* A new thermal zone needs to be updated anyway. */
2177 atomic_set(&tz->need_update, 1);
Matthew Garrettb1569e92008-12-03 17:55:32 +00002178
Kay Sievers354655e2009-01-06 10:44:37 -08002179 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08002180 result = device_register(&tz->device);
2181 if (result) {
2182 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
2183 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002184 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08002185 }
2186
2187 /* sys I/F */
2188 if (type) {
2189 result = device_create_file(&tz->device, &dev_attr_type);
2190 if (result)
2191 goto unregister;
2192 }
2193
2194 result = device_create_file(&tz->device, &dev_attr_temp);
2195 if (result)
2196 goto unregister;
2197
2198 if (ops->get_mode) {
2199 result = device_create_file(&tz->device, &dev_attr_mode);
2200 if (result)
2201 goto unregister;
2202 }
2203
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002204 result = create_trip_attrs(tz, mask);
2205 if (result)
2206 goto unregister;
2207
Zhang Rui203d3d42008-01-17 15:51:08 +08002208 for (count = 0; count < trips; count++) {
Zhang Rui81ad4272016-03-18 10:03:24 +08002209 if (tz->ops->get_trip_type(tz, count, &trip_type))
2210 set_bit(count, &tz->trips_disabled);
Matthew Garrett03a971a2008-12-03 18:00:38 +00002211 if (trip_type == THERMAL_TRIP_PASSIVE)
2212 passive = 1;
Zhang Rui81ad4272016-03-18 10:03:24 +08002213 if (tz->ops->get_trip_temp(tz, count, &trip_temp))
2214 set_bit(count, &tz->trips_disabled);
2215 /* Check for bogus trip points */
2216 if (trip_temp == 0)
2217 set_bit(count, &tz->trips_disabled);
Zhang Rui203d3d42008-01-17 15:51:08 +08002218 }
2219
Durgadoss R5a2c0902012-09-18 11:04:58 +05302220 if (!passive) {
2221 result = device_create_file(&tz->device, &dev_attr_passive);
2222 if (result)
2223 goto unregister;
2224 }
Ram Chandrasekardc814ca2017-07-10 15:23:47 -06002225 result = device_create_file(&tz->device, &dev_attr_passive_delay);
2226 if (result)
2227 goto unregister;
2228
2229 result = device_create_file(&tz->device, &dev_attr_polling_delay);
2230 if (result)
2231 goto unregister;
Matthew Garrett03a971a2008-12-03 18:00:38 +00002232
Sascha Hauer79e54212015-07-06 09:46:16 +02002233 if (IS_ENABLED(CONFIG_THERMAL_EMULATION)) {
2234 result = device_create_file(&tz->device, &dev_attr_emul_temp);
2235 if (result)
2236 goto unregister;
2237 }
2238
Durgadoss R5a2c0902012-09-18 11:04:58 +05302239 /* Create policy attribute */
2240 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00002241 if (result)
2242 goto unregister;
2243
Javi Merino9f382712015-03-26 15:53:02 +00002244 /* Add thermal zone params */
2245 result = create_tzp_attrs(&tz->device);
2246 if (result)
2247 goto unregister;
2248
Ni Wade25a0a5c2015-07-14 15:40:56 +08002249 /* Create available_policies attribute */
2250 result = device_create_file(&tz->device, &dev_attr_available_policies);
2251 if (result)
2252 goto unregister;
2253
Durgadoss Ra4a15482012-09-18 11:04:57 +05302254 /* Update 'this' zone's governor information */
2255 mutex_lock(&thermal_governor_lock);
2256
2257 if (tz->tzp)
Javi Merinoe33df1d2015-02-26 19:00:27 +00002258 governor = __find_governor(tz->tzp->governor_name);
Durgadoss Ra4a15482012-09-18 11:04:57 +05302259 else
Javi Merinoe33df1d2015-02-26 19:00:27 +00002260 governor = def_governor;
2261
2262 result = thermal_set_governor(tz, governor);
2263 if (result) {
2264 mutex_unlock(&thermal_governor_lock);
2265 goto unregister;
2266 }
Durgadoss Ra4a15482012-09-18 11:04:57 +05302267
2268 mutex_unlock(&thermal_governor_lock);
2269
Eduardo Valentinccba4ff2013-08-15 11:34:17 -04002270 if (!tz->tzp || !tz->tzp->no_hwmon) {
2271 result = thermal_add_hwmon_sysfs(tz);
2272 if (result)
2273 goto unregister;
2274 }
Zhang Ruie68b16a2008-04-21 16:07:52 +08002275
Zhang Rui203d3d42008-01-17 15:51:08 +08002276 mutex_lock(&thermal_list_lock);
2277 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08002278 mutex_unlock(&thermal_list_lock);
2279
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05302280 /* Bind cooling devices for this zone */
2281 bind_tz(tz);
2282
Ram Chandrasekarb17d98c2017-06-23 10:41:23 -06002283 INIT_DEFERRABLE_WORK(&(tz->poll_queue), thermal_zone_device_check);
Matthew Garrettb1569e92008-12-03 17:55:32 +00002284
Zhang Ruibb431ba2015-10-30 16:31:47 +08002285 thermal_zone_device_reset(tz);
Chen Yu4511f712015-10-30 16:32:10 +08002286 /* Update the new thermal zone and mark it as already updated. */
2287 if (atomic_cmpxchg(&tz->need_update, 1, 0))
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07002288 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Matthew Garrettb1569e92008-12-03 17:55:32 +00002289
Yao Dongdong14015862014-10-28 07:40:25 +00002290 return tz;
Zhang Rui203d3d42008-01-17 15:51:08 +08002291
Joe Perchescaca8b82012-03-21 12:55:02 -07002292unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08002293 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
2294 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002295 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08002296}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00002297EXPORT_SYMBOL_GPL(thermal_zone_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08002298
2299/**
2300 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08002301 * @tz: the thermal zone device to remove
2302 */
2303void thermal_zone_device_unregister(struct thermal_zone_device *tz)
2304{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05302305 int i;
2306 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08002307 struct thermal_cooling_device *cdev;
2308 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08002309
2310 if (!tz)
2311 return;
2312
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05302313 tzp = tz->tzp;
2314
Zhang Rui203d3d42008-01-17 15:51:08 +08002315 mutex_lock(&thermal_list_lock);
2316 list_for_each_entry(pos, &thermal_tz_list, node)
2317 if (pos == tz)
2318 break;
2319 if (pos != tz) {
2320 /* thermal zone device not found */
2321 mutex_unlock(&thermal_list_lock);
2322 return;
2323 }
2324 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05302325
2326 /* Unbind all cdevs associated with 'this' thermal zone */
2327 list_for_each_entry(cdev, &thermal_cdev_list, node) {
2328 if (tz->ops->unbind) {
2329 tz->ops->unbind(tz, cdev);
2330 continue;
2331 }
2332
2333 if (!tzp || !tzp->tbp)
2334 break;
2335
2336 for (i = 0; i < tzp->num_tbps; i++) {
2337 if (tzp->tbp[i].cdev == cdev) {
2338 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
2339 tzp->tbp[i].cdev = NULL;
2340 }
2341 }
2342 }
2343
Zhang Rui203d3d42008-01-17 15:51:08 +08002344 mutex_unlock(&thermal_list_lock);
2345
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -06002346 thermal_zone_device_set_polling(NULL, tz, 0);
Matthew Garrettb1569e92008-12-03 17:55:32 +00002347
Zhang Rui203d3d42008-01-17 15:51:08 +08002348 if (tz->type[0])
2349 device_remove_file(&tz->device, &dev_attr_type);
2350 device_remove_file(&tz->device, &dev_attr_temp);
2351 if (tz->ops->get_mode)
2352 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05302353 device_remove_file(&tz->device, &dev_attr_policy);
Ni Wade25a0a5c2015-07-14 15:40:56 +08002354 device_remove_file(&tz->device, &dev_attr_available_policies);
Ram Chandrasekardc814ca2017-07-10 15:23:47 -06002355 device_remove_file(&tz->device, &dev_attr_passive_delay);
2356 device_remove_file(&tz->device, &dev_attr_polling_delay);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002357 remove_trip_attrs(tz);
Javi Merinoe33df1d2015-02-26 19:00:27 +00002358 thermal_set_governor(tz, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08002359
Zhang Ruie68b16a2008-04-21 16:07:52 +08002360 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08002361 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
2362 idr_destroy(&tz->idr);
2363 mutex_destroy(&tz->lock);
2364 device_unregister(&tz->device);
2365 return;
2366}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00002367EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08002368
Eduardo Valentin63c4d912013-04-05 12:32:28 +00002369/**
2370 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
2371 * @name: thermal zone name to fetch the temperature
2372 *
2373 * When only one zone is found with the passed name, returns a reference to it.
2374 *
2375 * Return: On success returns a reference to an unique thermal zone with
2376 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
2377 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
2378 */
2379struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
2380{
2381 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
2382 unsigned int found = 0;
2383
2384 if (!name)
2385 goto exit;
2386
2387 mutex_lock(&thermal_list_lock);
2388 list_for_each_entry(pos, &thermal_tz_list, node)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -07002389 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
Eduardo Valentin63c4d912013-04-05 12:32:28 +00002390 found++;
2391 ref = pos;
2392 }
2393 mutex_unlock(&thermal_list_lock);
2394
2395 /* nothing has been found, thus an error code for it */
2396 if (found == 0)
2397 ref = ERR_PTR(-ENODEV);
2398 else if (found > 1)
2399 /* Success only when an unique zone is found */
2400 ref = ERR_PTR(-EEXIST);
2401
2402exit:
2403 return ref;
2404}
2405EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
2406
Rajendra Nayak4a7069a2016-05-05 14:21:42 +05302407/**
2408 * thermal_zone_get_slope - return the slope attribute of the thermal zone
2409 * @tz: thermal zone device with the slope attribute
2410 *
2411 * Return: If the thermal zone device has a slope attribute, return it, else
2412 * return 1.
2413 */
2414int thermal_zone_get_slope(struct thermal_zone_device *tz)
2415{
2416 if (tz && tz->tzp)
2417 return tz->tzp->slope;
2418 return 1;
2419}
2420EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
2421
2422/**
2423 * thermal_zone_get_offset - return the offset attribute of the thermal zone
2424 * @tz: thermal zone device with the offset attribute
2425 *
2426 * Return: If the thermal zone device has a offset attribute, return it, else
2427 * return 0.
2428 */
2429int thermal_zone_get_offset(struct thermal_zone_device *tz)
2430{
2431 if (tz && tz->tzp)
2432 return tz->tzp->offset;
2433 return 0;
2434}
2435EXPORT_SYMBOL_GPL(thermal_zone_get_offset);
2436
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002437#ifdef CONFIG_NET
Johannes Berg2a94fe42013-11-19 15:19:39 +01002438static const struct genl_multicast_group thermal_event_mcgrps[] = {
2439 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
2440};
2441
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002442static struct genl_family thermal_event_genl_family = {
2443 .id = GENL_ID_GENERATE,
2444 .name = THERMAL_GENL_FAMILY_NAME,
2445 .version = THERMAL_GENL_VERSION,
2446 .maxattr = THERMAL_GENL_ATTR_MAX,
Johannes Berg2a94fe42013-11-19 15:19:39 +01002447 .mcgrps = thermal_event_mcgrps,
2448 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002449};
2450
Lina Iyer9bf792d2016-07-11 13:27:11 -06002451static int allow_netlink_events;
2452
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00002453int thermal_generate_netlink_event(struct thermal_zone_device *tz,
2454 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05302455{
2456 struct sk_buff *skb;
2457 struct nlattr *attr;
2458 struct thermal_genl_event *thermal_event;
2459 void *msg_header;
2460 int size;
2461 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07002462 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05302463
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00002464 if (!tz)
2465 return -EINVAL;
2466
Lina Iyer9bf792d2016-07-11 13:27:11 -06002467 if (!allow_netlink_events)
2468 return -ENODEV;
2469
R.Durgadoss4cb18722010-10-27 03:33:29 +05302470 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07002471 size = nla_total_size(sizeof(struct thermal_genl_event)) +
2472 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302473
2474 skb = genlmsg_new(size, GFP_ATOMIC);
2475 if (!skb)
2476 return -ENOMEM;
2477
2478 /* add the genetlink message header */
2479 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
2480 &thermal_event_genl_family, 0,
2481 THERMAL_GENL_CMD_EVENT);
2482 if (!msg_header) {
2483 nlmsg_free(skb);
2484 return -ENOMEM;
2485 }
2486
2487 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07002488 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
2489 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05302490
2491 if (!attr) {
2492 nlmsg_free(skb);
2493 return -EINVAL;
2494 }
2495
2496 thermal_event = nla_data(attr);
2497 if (!thermal_event) {
2498 nlmsg_free(skb);
2499 return -EINVAL;
2500 }
2501
2502 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
2503
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00002504 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05302505 thermal_event->event = event;
2506
2507 /* send multicast genetlink message */
Johannes Berg053c0952015-01-16 22:09:00 +01002508 genlmsg_end(skb, msg_header);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302509
Johannes Berg68eb5502013-11-19 15:19:38 +01002510 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01002511 0, GFP_ATOMIC);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302512 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00002513 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302514
2515 return result;
2516}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00002517EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302518
2519static int genetlink_init(void)
2520{
Lina Iyer9bf792d2016-07-11 13:27:11 -06002521 int ret;
2522
2523 ret = genl_register_family(&thermal_event_genl_family);
2524 if (!ret)
2525 allow_netlink_events = true;
2526
2527 return ret;
R.Durgadoss4cb18722010-10-27 03:33:29 +05302528}
2529
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002530static void genetlink_exit(void)
2531{
2532 genl_unregister_family(&thermal_event_genl_family);
2533}
2534#else /* !CONFIG_NET */
2535static inline int genetlink_init(void) { return 0; }
2536static inline void genetlink_exit(void) {}
Lina Iyer9bf792d2016-07-11 13:27:11 -06002537static inline int thermal_generate_netlink_event(struct thermal_zone_device *tz,
2538 enum events event) { return -ENODEV; }
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002539#endif /* !CONFIG_NET */
2540
Zhang Rui80a26a52013-03-26 16:38:29 +08002541static int __init thermal_register_governors(void)
2542{
2543 int result;
2544
2545 result = thermal_gov_step_wise_register();
2546 if (result)
2547 return result;
2548
2549 result = thermal_gov_fair_share_register();
2550 if (result)
2551 return result;
2552
Peter Feuerere4dbf982014-07-22 17:37:13 +02002553 result = thermal_gov_bang_bang_register();
2554 if (result)
2555 return result;
2556
Javi Merino6b775e82015-03-02 17:17:19 +00002557 result = thermal_gov_user_space_register();
2558 if (result)
2559 return result;
2560
Ram Chandrasekar6d44a342016-07-07 11:09:52 -06002561 result = thermal_gov_low_limits_register();
2562 if (result)
2563 return result;
2564
Javi Merino6b775e82015-03-02 17:17:19 +00002565 return thermal_gov_power_allocator_register();
Zhang Rui80a26a52013-03-26 16:38:29 +08002566}
2567
2568static void thermal_unregister_governors(void)
2569{
2570 thermal_gov_step_wise_unregister();
2571 thermal_gov_fair_share_unregister();
Peter Feuerere4dbf982014-07-22 17:37:13 +02002572 thermal_gov_bang_bang_unregister();
Zhang Rui80a26a52013-03-26 16:38:29 +08002573 thermal_gov_user_space_unregister();
Ram Chandrasekar6d44a342016-07-07 11:09:52 -06002574 thermal_gov_low_limits_unregister();
Javi Merino6b775e82015-03-02 17:17:19 +00002575 thermal_gov_power_allocator_unregister();
Zhang Rui80a26a52013-03-26 16:38:29 +08002576}
2577
Zhang Ruiff140fe2015-10-30 16:31:58 +08002578static int thermal_pm_notify(struct notifier_block *nb,
2579 unsigned long mode, void *_unused)
2580{
2581 struct thermal_zone_device *tz;
2582
2583 switch (mode) {
2584 case PM_HIBERNATION_PREPARE:
2585 case PM_RESTORE_PREPARE:
2586 case PM_SUSPEND_PREPARE:
2587 atomic_set(&in_suspend, 1);
2588 break;
2589 case PM_POST_HIBERNATION:
2590 case PM_POST_RESTORE:
2591 case PM_POST_SUSPEND:
2592 atomic_set(&in_suspend, 0);
2593 list_for_each_entry(tz, &thermal_tz_list, node) {
2594 thermal_zone_device_reset(tz);
Ram Chandrasekar95b4e5d2017-12-13 17:21:26 -07002595 thermal_zone_device_update(tz,
2596 THERMAL_EVENT_UNSPECIFIED);
Zhang Ruiff140fe2015-10-30 16:31:58 +08002597 }
2598 break;
2599 default:
2600 break;
2601 }
2602 return 0;
2603}
2604
2605static struct notifier_block thermal_pm_nb = {
2606 .notifier_call = thermal_pm_notify,
2607};
2608
Zhang Rui203d3d42008-01-17 15:51:08 +08002609static int __init thermal_init(void)
2610{
Zhang Rui80a26a52013-03-26 16:38:29 +08002611 int result;
2612
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -06002613 thermal_passive_wq = alloc_workqueue("thermal_passive_wq",
2614 WQ_HIGHPRI | WQ_UNBOUND
2615 | WQ_FREEZABLE,
2616 THERMAL_MAX_ACTIVE);
2617 if (!thermal_passive_wq) {
2618 result = -ENOMEM;
2619 goto init_exit;
2620 }
2621
Zhang Rui80a26a52013-03-26 16:38:29 +08002622 result = thermal_register_governors();
2623 if (result)
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -06002624 goto destroy_wq;
Zhang Rui203d3d42008-01-17 15:51:08 +08002625
2626 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08002627 if (result)
2628 goto unregister_governors;
2629
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002630 result = of_parse_thermal_zones();
2631 if (result)
Lina Iyer9bf792d2016-07-11 13:27:11 -06002632 goto exit_zone_parse;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002633
Zhang Ruiff140fe2015-10-30 16:31:58 +08002634 result = register_pm_notifier(&thermal_pm_nb);
2635 if (result)
2636 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
2637 result);
2638
Zhang Rui80a26a52013-03-26 16:38:29 +08002639 return 0;
2640
Lina Iyer9bf792d2016-07-11 13:27:11 -06002641exit_zone_parse:
Zhang Rui80a26a52013-03-26 16:38:29 +08002642 class_unregister(&thermal_class);
Luis Henriques9d367e52014-12-03 21:20:21 +00002643unregister_governors:
2644 thermal_unregister_governors();
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -06002645destroy_wq:
2646 destroy_workqueue(thermal_passive_wq);
Lina Iyer9bf792d2016-07-11 13:27:11 -06002647init_exit:
Zhang Rui80a26a52013-03-26 16:38:29 +08002648 idr_destroy(&thermal_tz_idr);
2649 idr_destroy(&thermal_cdev_idr);
2650 mutex_destroy(&thermal_idr_lock);
2651 mutex_destroy(&thermal_list_lock);
2652 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08002653 return result;
2654}
2655
Ram Chandrasekarfe0ae222017-04-06 12:08:38 -06002656static void thermal_exit(void)
Zhang Rui203d3d42008-01-17 15:51:08 +08002657{
Zhang Ruiff140fe2015-10-30 16:31:58 +08002658 unregister_pm_notifier(&thermal_pm_nb);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002659 of_thermal_destroy_zones();
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -06002660 destroy_workqueue(thermal_passive_wq);
Zhang Rui80a26a52013-03-26 16:38:29 +08002661 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08002662 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08002663 thermal_unregister_governors();
Zhang Rui203d3d42008-01-17 15:51:08 +08002664 idr_destroy(&thermal_tz_idr);
2665 idr_destroy(&thermal_cdev_idr);
2666 mutex_destroy(&thermal_idr_lock);
2667 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08002668 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08002669}
2670
Lina Iyer9bf792d2016-07-11 13:27:11 -06002671static int __init thermal_netlink_init(void)
2672{
2673 int ret = 0;
2674
2675 ret = genetlink_init();
2676 if (!ret)
2677 goto exit_netlink;
2678
2679 thermal_exit();
2680exit_netlink:
2681 return ret;
2682}
2683
2684subsys_initcall(thermal_init);
2685fs_initcall(thermal_netlink_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08002686module_exit(thermal_exit);