blob: b137c4e85254aef6e2109f826233db309a13c2b3 [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 Chandrasekar2dd98f12016-08-04 13:02:03 -0600421 thermal_zone_device_set_polling(system_freezable_wq,
422 tz, tz->polling_delay);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530423 else
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -0600424 thermal_zone_device_set_polling(NULL, tz, 0);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530425
426 mutex_unlock(&tz->lock);
427}
428
429static void handle_non_critical_trips(struct thermal_zone_device *tz,
430 int trip, enum thermal_trip_type trip_type)
431{
Zhang Ruif2234bc2014-01-24 10:23:19 +0800432 tz->governor ? tz->governor->throttle(tz, trip) :
433 def_governor->throttle(tz, trip);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530434}
435
436static void handle_critical_trips(struct thermal_zone_device *tz,
437 int trip, enum thermal_trip_type trip_type)
438{
Sascha Hauer17e83512015-07-24 08:12:54 +0200439 int trip_temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530440
441 tz->ops->get_trip_temp(tz, trip, &trip_temp);
442
443 /* If we have not crossed the trip_temp, we do not care. */
Srinivas Pandruvada84ffe3e2014-11-12 15:43:29 -0800444 if (trip_temp <= 0 || tz->temperature < trip_temp)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530445 return;
446
Ram Chandrasekar5b1c0d12017-03-28 11:35:40 -0600447 trace_thermal_zone_trip(tz, trip, trip_type, true);
Punit Agrawal208cd822014-07-29 11:50:50 +0100448
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530449 if (tz->ops->notify)
450 tz->ops->notify(tz, trip, trip_type);
451
452 if (trip_type == THERMAL_TRIP_CRITICAL) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000453 dev_emerg(&tz->device,
454 "critical temperature reached(%d C),shutting down\n",
455 tz->temperature / 1000);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530456 orderly_poweroff(true);
457 }
458}
459
Ram Chandrasekarcf543602017-03-13 22:59:01 -0600460void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530461{
462 enum thermal_trip_type type;
463
Zhang Rui81ad4272016-03-18 10:03:24 +0800464 /* Ignore disabled trip points */
465 if (test_bit(trip, &tz->trips_disabled))
466 return;
467
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530468 tz->ops->get_trip_type(tz, trip, &type);
469
470 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
471 handle_critical_trips(tz, trip, type);
472 else
473 handle_non_critical_trips(tz, trip, type);
474 /*
475 * Alright, we handled this trip successfully.
476 * So, start monitoring again.
477 */
478 monitor_thermal_zone(tz);
479}
480
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000481/**
Sascha Hauerf6be0582015-07-06 09:46:14 +0200482 * thermal_zone_get_temp() - returns the temperature of a thermal zone
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000483 * @tz: a valid pointer to a struct thermal_zone_device
484 * @temp: a valid pointer to where to store the resulting temperature.
485 *
486 * When a valid thermal zone reference is passed, it will fetch its
487 * temperature and fill @temp.
488 *
489 * Return: On success returns 0, an error code otherwise
490 */
Sascha Hauer17e83512015-07-24 08:12:54 +0200491int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000492{
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000493 int ret = -EINVAL;
Zhang Rui5e20b2e2013-02-06 14:02:12 +0800494 int count;
Sascha Hauer17e83512015-07-24 08:12:54 +0200495 int crit_temp = INT_MAX;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000496 enum thermal_trip_type type;
497
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400498 if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000499 goto exit;
500
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000501 mutex_lock(&tz->lock);
502
503 ret = tz->ops->get_temp(tz, temp);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000504
Sascha Hauer79e54212015-07-06 09:46:16 +0200505 if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
506 for (count = 0; count < tz->trips; count++) {
507 ret = tz->ops->get_trip_type(tz, count, &type);
508 if (!ret && type == THERMAL_TRIP_CRITICAL) {
509 ret = tz->ops->get_trip_temp(tz, count,
510 &crit_temp);
511 break;
512 }
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000513 }
Sascha Hauer79e54212015-07-06 09:46:16 +0200514
Sascha Hauer934c93b2015-07-06 09:46:17 +0200515 /*
516 * Only allow emulating a temperature when the real temperature
517 * is below the critical temperature so that the emulation code
518 * cannot hide critical conditions.
519 */
Sascha Hauer79e54212015-07-06 09:46:16 +0200520 if (!ret && *temp < crit_temp)
521 *temp = tz->emul_temperature;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000522 }
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000523 mutex_unlock(&tz->lock);
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000524exit:
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000525 return ret;
526}
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000527EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000528
Sascha Hauer060c0342016-06-22 16:42:01 +0800529void thermal_zone_set_trips(struct thermal_zone_device *tz)
530{
531 int low = -INT_MAX;
532 int high = INT_MAX;
533 int trip_temp, hysteresis;
534 int i, ret;
535
536 mutex_lock(&tz->lock);
537
538 if (!tz->ops->set_trips || !tz->ops->get_trip_hyst)
539 goto exit;
540
541 for (i = 0; i < tz->trips; i++) {
542 int trip_low;
543
544 tz->ops->get_trip_temp(tz, i, &trip_temp);
545 tz->ops->get_trip_hyst(tz, i, &hysteresis);
546
547 trip_low = trip_temp - hysteresis;
548
549 if (trip_low < tz->temperature && trip_low > low)
550 low = trip_low;
551
552 if (trip_temp > tz->temperature && trip_temp < high)
553 high = trip_temp;
554 }
555
Sascha Hauer060c0342016-06-22 16:42:01 +0800556 tz->prev_low_trip = low;
557 tz->prev_high_trip = high;
558
559 dev_dbg(&tz->device,
560 "new temperature boundaries: %d < x < %d\n", low, high);
561
562 /*
563 * Set a temperature window. When this window is left the driver
564 * must inform the thermal core via thermal_zone_device_update.
565 */
566 ret = tz->ops->set_trips(tz, low, high);
567 if (ret)
568 dev_err(&tz->device, "Failed to set trips: %d\n", ret);
569
570exit:
571 mutex_unlock(&tz->lock);
572}
573EXPORT_SYMBOL_GPL(thermal_zone_set_trips);
574
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530575static void update_temperature(struct thermal_zone_device *tz)
576{
Sascha Hauer17e83512015-07-24 08:12:54 +0200577 int temp, ret;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530578
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000579 ret = thermal_zone_get_temp(tz, &temp);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530580 if (ret) {
Hans de Goede7e497a72015-03-21 15:02:55 +0100581 if (ret != -EAGAIN)
582 dev_warn(&tz->device,
583 "failed to read out thermal zone (%d)\n",
584 ret);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000585 return;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530586 }
587
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000588 mutex_lock(&tz->lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530589 tz->last_temperature = tz->temperature;
590 tz->temperature = temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530591 mutex_unlock(&tz->lock);
Aaron Lu06475b52013-12-02 13:54:26 +0800592
Punit Agrawal100a8fd2014-07-29 11:50:48 +0100593 trace_thermal_temperature(tz);
Zhang Ruibb431ba2015-10-30 16:31:47 +0800594 if (tz->last_temperature == THERMAL_TEMP_INVALID)
595 dev_dbg(&tz->device, "last_temperature N/A, current_temperature=%d\n",
596 tz->temperature);
597 else
598 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
599 tz->last_temperature, tz->temperature);
600}
601
602static void thermal_zone_device_reset(struct thermal_zone_device *tz)
603{
604 struct thermal_instance *pos;
605
606 tz->temperature = THERMAL_TEMP_INVALID;
607 tz->passive = 0;
608 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
609 pos->initialized = false;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530610}
611
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700612void thermal_zone_device_update(struct thermal_zone_device *tz,
613 enum thermal_notify_event event)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530614{
615 int count;
616
Zhang Ruiff140fe2015-10-30 16:31:58 +0800617 if (atomic_read(&in_suspend))
618 return;
619
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400620 if (!tz->ops->get_temp)
621 return;
622
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530623 update_temperature(tz);
624
Sascha Hauer060c0342016-06-22 16:42:01 +0800625 thermal_zone_set_trips(tz);
626
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700627 tz->notify_event = event;
628
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530629 for (count = 0; count < tz->trips; count++)
630 handle_thermal_trip(tz, count);
631}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000632EXPORT_SYMBOL_GPL(thermal_zone_device_update);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530633
634static void thermal_zone_device_check(struct work_struct *work)
635{
636 struct thermal_zone_device *tz = container_of(work, struct
637 thermal_zone_device,
638 poll_queue.work);
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700639 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530640}
641
Zhang Rui203d3d42008-01-17 15:51:08 +0800642/* sys I/F for thermal zone */
643
644#define to_thermal_zone(_dev) \
645 container_of(_dev, struct thermal_zone_device, device)
646
647static ssize_t
648type_show(struct device *dev, struct device_attribute *attr, char *buf)
649{
650 struct thermal_zone_device *tz = to_thermal_zone(dev);
651
652 return sprintf(buf, "%s\n", tz->type);
653}
654
655static ssize_t
656temp_show(struct device *dev, struct device_attribute *attr, char *buf)
657{
658 struct thermal_zone_device *tz = to_thermal_zone(dev);
Sascha Hauer17e83512015-07-24 08:12:54 +0200659 int temperature, ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800660
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000661 ret = thermal_zone_get_temp(tz, &temperature);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000662
663 if (ret)
664 return ret;
665
Sascha Hauer17e83512015-07-24 08:12:54 +0200666 return sprintf(buf, "%d\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800667}
668
669static ssize_t
670mode_show(struct device *dev, struct device_attribute *attr, char *buf)
671{
672 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000673 enum thermal_device_mode mode;
674 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800675
676 if (!tz->ops->get_mode)
677 return -EPERM;
678
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000679 result = tz->ops->get_mode(tz, &mode);
680 if (result)
681 return result;
682
683 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
684 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800685}
686
687static ssize_t
688mode_store(struct device *dev, struct device_attribute *attr,
689 const char *buf, size_t count)
690{
691 struct thermal_zone_device *tz = to_thermal_zone(dev);
692 int result;
693
694 if (!tz->ops->set_mode)
695 return -EPERM;
696
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530697 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000698 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530699 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000700 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
701 else
702 result = -EINVAL;
703
Zhang Rui203d3d42008-01-17 15:51:08 +0800704 if (result)
705 return result;
706
707 return count;
708}
709
710static ssize_t
711trip_point_type_show(struct device *dev, struct device_attribute *attr,
712 char *buf)
713{
714 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000715 enum thermal_trip_type type;
716 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800717
718 if (!tz->ops->get_trip_type)
719 return -EPERM;
720
721 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
722 return -EINVAL;
723
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000724 result = tz->ops->get_trip_type(tz, trip, &type);
725 if (result)
726 return result;
727
728 switch (type) {
729 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300730 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000731 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300732 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000733 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300734 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000735 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300736 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000737 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300738 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000739 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800740}
741
742static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800743trip_point_temp_store(struct device *dev, struct device_attribute *attr,
744 const char *buf, size_t count)
745{
746 struct thermal_zone_device *tz = to_thermal_zone(dev);
747 int trip, ret;
Wei Ni1d0fd422016-03-03 17:33:46 +0800748 int temperature;
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800749
750 if (!tz->ops->set_trip_temp)
751 return -EPERM;
752
753 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
754 return -EINVAL;
755
Wei Ni1d0fd422016-03-03 17:33:46 +0800756 if (kstrtoint(buf, 10, &temperature))
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800757 return -EINVAL;
758
759 ret = tz->ops->set_trip_temp(tz, trip, temperature);
Kuninori Morimotoad74e462015-12-15 01:19:53 +0000760 if (ret)
761 return ret;
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800762
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700763 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Kuninori Morimotoad74e462015-12-15 01:19:53 +0000764
765 return count;
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800766}
767
768static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800769trip_point_temp_show(struct device *dev, struct device_attribute *attr,
770 char *buf)
771{
772 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000773 int trip, ret;
Sascha Hauer17e83512015-07-24 08:12:54 +0200774 int temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800775
776 if (!tz->ops->get_trip_temp)
777 return -EPERM;
778
779 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
780 return -EINVAL;
781
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000782 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
783
784 if (ret)
785 return ret;
786
Sascha Hauer17e83512015-07-24 08:12:54 +0200787 return sprintf(buf, "%d\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800788}
789
Matthew Garrett03a971a2008-12-03 18:00:38 +0000790static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800791trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
792 const char *buf, size_t count)
793{
794 struct thermal_zone_device *tz = to_thermal_zone(dev);
795 int trip, ret;
Sascha Hauer17e83512015-07-24 08:12:54 +0200796 int temperature;
Durgadoss R27365a62012-07-25 10:10:59 +0800797
798 if (!tz->ops->set_trip_hyst)
799 return -EPERM;
800
801 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
802 return -EINVAL;
803
Sascha Hauer17e83512015-07-24 08:12:54 +0200804 if (kstrtoint(buf, 10, &temperature))
Durgadoss R27365a62012-07-25 10:10:59 +0800805 return -EINVAL;
806
807 /*
808 * We are not doing any check on the 'temperature' value
809 * here. The driver implementing 'set_trip_hyst' has to
810 * take care of this.
811 */
812 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
813
Ram Chandrasekarb0363192017-04-13 14:24:24 -0600814 thermal_zone_set_trips(tz);
Sascha Hauer060c0342016-06-22 16:42:01 +0800815
Durgadoss R27365a62012-07-25 10:10:59 +0800816 return ret ? ret : count;
817}
818
819static ssize_t
820trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
821 char *buf)
822{
823 struct thermal_zone_device *tz = to_thermal_zone(dev);
824 int trip, ret;
Sascha Hauer17e83512015-07-24 08:12:54 +0200825 int temperature;
Durgadoss R27365a62012-07-25 10:10:59 +0800826
827 if (!tz->ops->get_trip_hyst)
828 return -EPERM;
829
830 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
831 return -EINVAL;
832
833 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
834
Sascha Hauer17e83512015-07-24 08:12:54 +0200835 return ret ? ret : sprintf(buf, "%d\n", temperature);
Durgadoss R27365a62012-07-25 10:10:59 +0800836}
837
838static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000839passive_store(struct device *dev, struct device_attribute *attr,
840 const char *buf, size_t count)
841{
842 struct thermal_zone_device *tz = to_thermal_zone(dev);
843 struct thermal_cooling_device *cdev = NULL;
844 int state;
845
846 if (!sscanf(buf, "%d\n", &state))
847 return -EINVAL;
848
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100849 /* sanity check: values below 1000 millicelcius don't make sense
850 * and can cause the system to go into a thermal heart attack
851 */
852 if (state && state < 1000)
853 return -EINVAL;
854
Matthew Garrett03a971a2008-12-03 18:00:38 +0000855 if (state && !tz->forced_passive) {
856 mutex_lock(&thermal_list_lock);
857 list_for_each_entry(cdev, &thermal_cdev_list, node) {
858 if (!strncmp("Processor", cdev->type,
859 sizeof("Processor")))
860 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800861 THERMAL_TRIPS_NONE, cdev,
862 THERMAL_NO_LIMIT,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000863 THERMAL_NO_LIMIT,
864 THERMAL_WEIGHT_DEFAULT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000865 }
866 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100867 if (!tz->passive_delay)
868 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000869 } else if (!state && tz->forced_passive) {
870 mutex_lock(&thermal_list_lock);
871 list_for_each_entry(cdev, &thermal_cdev_list, node) {
872 if (!strncmp("Processor", cdev->type,
873 sizeof("Processor")))
874 thermal_zone_unbind_cooling_device(tz,
875 THERMAL_TRIPS_NONE,
876 cdev);
877 }
878 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100879 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000880 }
881
Matthew Garrett03a971a2008-12-03 18:00:38 +0000882 tz->forced_passive = state;
883
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700884 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000885
886 return count;
887}
888
889static ssize_t
890passive_show(struct device *dev, struct device_attribute *attr,
891 char *buf)
892{
893 struct thermal_zone_device *tz = to_thermal_zone(dev);
894
895 return sprintf(buf, "%d\n", tz->forced_passive);
896}
897
Durgadoss R5a2c0902012-09-18 11:04:58 +0530898static ssize_t
899policy_store(struct device *dev, struct device_attribute *attr,
900 const char *buf, size_t count)
901{
902 int ret = -EINVAL;
903 struct thermal_zone_device *tz = to_thermal_zone(dev);
904 struct thermal_governor *gov;
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000905 char name[THERMAL_NAME_LENGTH];
906
907 snprintf(name, sizeof(name), "%s", buf);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530908
909 mutex_lock(&thermal_governor_lock);
Javi Merinob6cc7722014-11-25 16:00:33 +0000910 mutex_lock(&tz->lock);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530911
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000912 gov = __find_governor(strim(name));
Durgadoss R5a2c0902012-09-18 11:04:58 +0530913 if (!gov)
914 goto exit;
915
Javi Merinoe33df1d2015-02-26 19:00:27 +0000916 ret = thermal_set_governor(tz, gov);
917 if (!ret)
918 ret = count;
Durgadoss R5a2c0902012-09-18 11:04:58 +0530919
920exit:
Javi Merinob6cc7722014-11-25 16:00:33 +0000921 mutex_unlock(&tz->lock);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530922 mutex_unlock(&thermal_governor_lock);
923 return ret;
924}
925
926static ssize_t
927policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
928{
929 struct thermal_zone_device *tz = to_thermal_zone(dev);
930
931 return sprintf(buf, "%s\n", tz->governor->name);
932}
933
Ni Wade25a0a5c2015-07-14 15:40:56 +0800934static ssize_t
935available_policies_show(struct device *dev, struct device_attribute *devattr,
936 char *buf)
937{
938 struct thermal_governor *pos;
939 ssize_t count = 0;
940 ssize_t size = PAGE_SIZE;
941
942 mutex_lock(&thermal_governor_lock);
943
944 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
945 size = PAGE_SIZE - count;
946 count += scnprintf(buf + count, size, "%s ", pos->name);
947 }
948 count += scnprintf(buf + count, size, "\n");
949
950 mutex_unlock(&thermal_governor_lock);
951
952 return count;
953}
954
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000955static ssize_t
956emul_temp_store(struct device *dev, struct device_attribute *attr,
957 const char *buf, size_t count)
958{
959 struct thermal_zone_device *tz = to_thermal_zone(dev);
960 int ret = 0;
Wei Ni1d0fd422016-03-03 17:33:46 +0800961 int temperature;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000962
Wei Ni1d0fd422016-03-03 17:33:46 +0800963 if (kstrtoint(buf, 10, &temperature))
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000964 return -EINVAL;
965
966 if (!tz->ops->set_emul_temp) {
967 mutex_lock(&tz->lock);
968 tz->emul_temperature = temperature;
969 mutex_unlock(&tz->lock);
970 } else {
971 ret = tz->ops->set_emul_temp(tz, temperature);
972 }
973
lan,Tianyu800744b2014-01-02 15:47:54 +0800974 if (!ret)
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700975 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
lan,Tianyu800744b2014-01-02 15:47:54 +0800976
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000977 return ret ? ret : count;
978}
979static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000980
Javi Merino9f382712015-03-26 15:53:02 +0000981static ssize_t
982sustainable_power_show(struct device *dev, struct device_attribute *devattr,
983 char *buf)
984{
985 struct thermal_zone_device *tz = to_thermal_zone(dev);
986
987 if (tz->tzp)
988 return sprintf(buf, "%u\n", tz->tzp->sustainable_power);
989 else
990 return -EIO;
991}
992
993static ssize_t
994sustainable_power_store(struct device *dev, struct device_attribute *devattr,
995 const char *buf, size_t count)
996{
997 struct thermal_zone_device *tz = to_thermal_zone(dev);
998 u32 sustainable_power;
999
1000 if (!tz->tzp)
1001 return -EIO;
1002
1003 if (kstrtou32(buf, 10, &sustainable_power))
1004 return -EINVAL;
1005
1006 tz->tzp->sustainable_power = sustainable_power;
1007
1008 return count;
1009}
1010static DEVICE_ATTR(sustainable_power, S_IWUSR | S_IRUGO, sustainable_power_show,
1011 sustainable_power_store);
1012
1013#define create_s32_tzp_attr(name) \
1014 static ssize_t \
1015 name##_show(struct device *dev, struct device_attribute *devattr, \
1016 char *buf) \
1017 { \
1018 struct thermal_zone_device *tz = to_thermal_zone(dev); \
1019 \
1020 if (tz->tzp) \
Leo Yan15333e32016-03-29 19:24:15 +08001021 return sprintf(buf, "%d\n", tz->tzp->name); \
Javi Merino9f382712015-03-26 15:53:02 +00001022 else \
1023 return -EIO; \
1024 } \
1025 \
1026 static ssize_t \
1027 name##_store(struct device *dev, struct device_attribute *devattr, \
1028 const char *buf, size_t count) \
1029 { \
1030 struct thermal_zone_device *tz = to_thermal_zone(dev); \
1031 s32 value; \
1032 \
1033 if (!tz->tzp) \
1034 return -EIO; \
1035 \
1036 if (kstrtos32(buf, 10, &value)) \
1037 return -EINVAL; \
1038 \
1039 tz->tzp->name = value; \
1040 \
1041 return count; \
1042 } \
1043 static DEVICE_ATTR(name, S_IWUSR | S_IRUGO, name##_show, name##_store)
1044
1045create_s32_tzp_attr(k_po);
1046create_s32_tzp_attr(k_pu);
1047create_s32_tzp_attr(k_i);
1048create_s32_tzp_attr(k_d);
1049create_s32_tzp_attr(integral_cutoff);
Eduardo Valentin9d0be7f2015-05-11 19:34:23 -07001050create_s32_tzp_attr(slope);
1051create_s32_tzp_attr(offset);
Javi Merino9f382712015-03-26 15:53:02 +00001052#undef create_s32_tzp_attr
1053
1054static struct device_attribute *dev_tzp_attrs[] = {
1055 &dev_attr_sustainable_power,
1056 &dev_attr_k_po,
1057 &dev_attr_k_pu,
1058 &dev_attr_k_i,
1059 &dev_attr_k_d,
1060 &dev_attr_integral_cutoff,
Eduardo Valentin9d0be7f2015-05-11 19:34:23 -07001061 &dev_attr_slope,
1062 &dev_attr_offset,
Javi Merino9f382712015-03-26 15:53:02 +00001063};
1064
1065static int create_tzp_attrs(struct device *dev)
1066{
1067 int i;
1068
1069 for (i = 0; i < ARRAY_SIZE(dev_tzp_attrs); i++) {
1070 int ret;
1071 struct device_attribute *dev_attr = dev_tzp_attrs[i];
1072
1073 ret = device_create_file(dev, dev_attr);
1074 if (ret)
1075 return ret;
1076 }
1077
1078 return 0;
1079}
1080
Javi Merino35b11d22015-02-26 19:00:28 +00001081/**
1082 * power_actor_get_max_power() - get the maximum power that a cdev can consume
1083 * @cdev: pointer to &thermal_cooling_device
1084 * @tz: a valid thermal zone device pointer
1085 * @max_power: pointer in which to store the maximum power
1086 *
1087 * Calculate the maximum power consumption in milliwats that the
1088 * cooling device can currently consume and store it in @max_power.
1089 *
1090 * Return: 0 on success, -EINVAL if @cdev doesn't support the
1091 * power_actor API or -E* on other error.
1092 */
1093int power_actor_get_max_power(struct thermal_cooling_device *cdev,
1094 struct thermal_zone_device *tz, u32 *max_power)
1095{
1096 if (!cdev_is_power_actor(cdev))
1097 return -EINVAL;
1098
1099 return cdev->ops->state2power(cdev, tz, 0, max_power);
1100}
1101
1102/**
Javi Merinoc973c3b2015-09-14 14:23:50 +01001103 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
1104 * @cdev: pointer to &thermal_cooling_device
1105 * @tz: a valid thermal zone device pointer
1106 * @min_power: pointer in which to store the minimum power
1107 *
1108 * Calculate the minimum power consumption in milliwatts that the
1109 * cooling device can currently consume and store it in @min_power.
1110 *
1111 * Return: 0 on success, -EINVAL if @cdev doesn't support the
1112 * power_actor API or -E* on other error.
1113 */
1114int power_actor_get_min_power(struct thermal_cooling_device *cdev,
1115 struct thermal_zone_device *tz, u32 *min_power)
1116{
1117 unsigned long max_state;
1118 int ret;
1119
1120 if (!cdev_is_power_actor(cdev))
1121 return -EINVAL;
1122
1123 ret = cdev->ops->get_max_state(cdev, &max_state);
1124 if (ret)
1125 return ret;
1126
1127 return cdev->ops->state2power(cdev, tz, max_state, min_power);
1128}
1129
1130/**
Javi Merino35b11d22015-02-26 19:00:28 +00001131 * power_actor_set_power() - limit the maximum power that a cooling device can consume
1132 * @cdev: pointer to &thermal_cooling_device
1133 * @instance: thermal instance to update
1134 * @power: the power in milliwatts
1135 *
1136 * Set the cooling device to consume at most @power milliwatts.
1137 *
1138 * Return: 0 on success, -EINVAL if the cooling device does not
1139 * implement the power actor API or -E* for other failures.
1140 */
1141int power_actor_set_power(struct thermal_cooling_device *cdev,
1142 struct thermal_instance *instance, u32 power)
1143{
1144 unsigned long state;
1145 int ret;
1146
1147 if (!cdev_is_power_actor(cdev))
1148 return -EINVAL;
1149
1150 ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
1151 if (ret)
1152 return ret;
1153
1154 instance->target = state;
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01001155 mutex_lock(&cdev->lock);
Javi Merino35b11d22015-02-26 19:00:28 +00001156 cdev->updated = false;
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01001157 mutex_unlock(&cdev->lock);
Javi Merino35b11d22015-02-26 19:00:28 +00001158 thermal_cdev_update(cdev);
1159
1160 return 0;
1161}
1162
Zhang Rui203d3d42008-01-17 15:51:08 +08001163static DEVICE_ATTR(type, 0444, type_show, NULL);
1164static DEVICE_ATTR(temp, 0444, temp_show, NULL);
1165static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -07001166static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301167static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
Ni Wade25a0a5c2015-07-14 15:40:56 +08001168static DEVICE_ATTR(available_policies, S_IRUGO, available_policies_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001169
Zhang Rui203d3d42008-01-17 15:51:08 +08001170/* sys I/F for cooling device */
1171#define to_cooling_device(_dev) \
1172 container_of(_dev, struct thermal_cooling_device, device)
1173
1174static ssize_t
1175thermal_cooling_device_type_show(struct device *dev,
1176 struct device_attribute *attr, char *buf)
1177{
1178 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1179
1180 return sprintf(buf, "%s\n", cdev->type);
1181}
1182
1183static ssize_t
1184thermal_cooling_device_max_state_show(struct device *dev,
1185 struct device_attribute *attr, char *buf)
1186{
1187 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001188 unsigned long state;
1189 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +08001190
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001191 ret = cdev->ops->get_max_state(cdev, &state);
1192 if (ret)
1193 return ret;
1194 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +08001195}
1196
1197static ssize_t
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001198thermal_cooling_device_min_state_show(struct device *dev,
1199 struct device_attribute *attr, char *buf)
1200{
1201 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1202 unsigned long state;
1203 int ret;
1204
1205 if (cdev->ops->get_min_state)
1206 ret = cdev->ops->get_min_state(cdev, &state);
1207 else
1208 ret = -EPERM;
1209
1210 if (ret)
1211 return ret;
1212
1213 return snprintf(buf, PAGE_SIZE, "%lu\n", state);
1214}
1215
1216static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +08001217thermal_cooling_device_cur_state_show(struct device *dev,
1218 struct device_attribute *attr, char *buf)
1219{
1220 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001221 unsigned long state;
1222 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +08001223
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001224 ret = cdev->ops->get_cur_state(cdev, &state);
1225 if (ret)
1226 return ret;
1227 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +08001228}
1229
1230static ssize_t
1231thermal_cooling_device_cur_state_store(struct device *dev,
1232 struct device_attribute *attr,
1233 const char *buf, size_t count)
1234{
1235 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Ram Chandrasekar5ab1c082016-09-20 17:09:28 -06001236 long state;
Zhang Rui203d3d42008-01-17 15:51:08 +08001237
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001238 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +08001239 return -EINVAL;
1240
Roel Kluinedb94912009-12-15 22:46:50 +01001241 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +08001242 return -EINVAL;
1243
Ram Chandrasekar5ab1c082016-09-20 17:09:28 -06001244 cdev->sysfs_cur_state_req = state;
1245
1246 cdev->updated = false;
1247 thermal_cdev_update(cdev);
1248
Zhang Rui203d3d42008-01-17 15:51:08 +08001249 return count;
1250}
1251
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001252static ssize_t
1253thermal_cooling_device_min_state_store(struct device *dev,
1254 struct device_attribute *attr,
1255 const char *buf, size_t count)
1256{
1257 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1258 long state;
1259 int ret = 0;
1260
1261 ret = sscanf(buf, "%ld\n", &state);
1262 if (ret <= 0)
1263 return (ret < 0) ? ret : -EINVAL;
1264
1265 if ((long)state < 0)
1266 return -EINVAL;
1267
1268 cdev->sysfs_min_state_req = state;
1269
1270 cdev->updated = false;
1271 thermal_cdev_update(cdev);
1272
1273 return count;
1274}
1275
Zhang Rui203d3d42008-01-17 15:51:08 +08001276static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -05001277__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001278static DEVICE_ATTR(max_state, 0444,
1279 thermal_cooling_device_max_state_show, NULL);
1280static DEVICE_ATTR(cur_state, 0644,
1281 thermal_cooling_device_cur_state_show,
1282 thermal_cooling_device_cur_state_store);
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001283static DEVICE_ATTR(min_state, 0644,
1284 thermal_cooling_device_min_state_show,
1285 thermal_cooling_device_min_state_store);
Zhang Rui203d3d42008-01-17 15:51:08 +08001286
1287static ssize_t
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001288thermal_cooling_device_lower_limit_show(struct device *dev,
1289 struct device_attribute *attr, char *buf)
1290{
1291 struct thermal_instance *instance;
1292
1293 instance =
1294 container_of(attr, struct thermal_instance, lower_attr);
1295
1296 return snprintf(buf, PAGE_SIZE, "%lu\n", instance->lower);
1297}
1298
1299static ssize_t
1300thermal_cooling_device_upper_limit_show(struct device *dev,
1301 struct device_attribute *attr, char *buf)
1302{
1303 struct thermal_instance *instance;
1304
1305 instance =
1306 container_of(attr, struct thermal_instance, upper_attr);
1307
1308 return snprintf(buf, PAGE_SIZE, "%lu\n", instance->upper);
1309}
1310
1311static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +08001312thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -05001313 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +08001314{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001315 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +08001316
1317 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001318 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001319
1320 if (instance->trip == THERMAL_TRIPS_NONE)
1321 return sprintf(buf, "-1\n");
1322 else
1323 return sprintf(buf, "%d\n", instance->trip);
1324}
1325
Matthias Kaehlcke2dc10f82015-02-20 18:10:08 -08001326static struct attribute *cooling_device_attrs[] = {
1327 &dev_attr_cdev_type.attr,
1328 &dev_attr_max_state.attr,
1329 &dev_attr_cur_state.attr,
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001330 &dev_attr_min_state.attr,
Matthias Kaehlcke2dc10f82015-02-20 18:10:08 -08001331 NULL,
1332};
1333
1334static const struct attribute_group cooling_device_attr_group = {
1335 .attrs = cooling_device_attrs,
1336};
1337
1338static const struct attribute_group *cooling_device_attr_groups[] = {
1339 &cooling_device_attr_group,
1340 NULL,
1341};
1342
Javi Merinodb916512015-02-18 16:04:24 +00001343static ssize_t
1344thermal_cooling_device_weight_show(struct device *dev,
1345 struct device_attribute *attr, char *buf)
1346{
1347 struct thermal_instance *instance;
1348
1349 instance = container_of(attr, struct thermal_instance, weight_attr);
1350
1351 return sprintf(buf, "%d\n", instance->weight);
1352}
1353
1354static ssize_t
1355thermal_cooling_device_weight_store(struct device *dev,
1356 struct device_attribute *attr,
1357 const char *buf, size_t count)
1358{
1359 struct thermal_instance *instance;
1360 int ret, weight;
1361
1362 ret = kstrtoint(buf, 0, &weight);
1363 if (ret)
1364 return ret;
1365
1366 instance = container_of(attr, struct thermal_instance, weight_attr);
1367 instance->weight = weight;
1368
1369 return count;
1370}
Zhang Rui203d3d42008-01-17 15:51:08 +08001371/* Device management */
1372
1373/**
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001374 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
1375 * @tz: pointer to struct thermal_zone_device
Zhang Rui203d3d42008-01-17 15:51:08 +08001376 * @trip: indicates which trip point the cooling devices is
1377 * associated with in this thermal zone.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001378 * @cdev: pointer to struct thermal_cooling_device
1379 * @upper: the Maximum cooling state for this trip point.
1380 * THERMAL_NO_LIMIT means no upper limit,
1381 * and the cooling device can be in max_state.
1382 * @lower: the Minimum cooling state can be used for this trip point.
1383 * THERMAL_NO_LIMIT means no lower limit,
1384 * and the cooling device can be in cooling state 0.
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001385 * @weight: The weight of the cooling device to be bound to the
1386 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
1387 * default value
Len Brown543a9562008-02-07 16:55:08 -05001388 *
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001389 * This interface function bind a thermal cooling device to the certain trip
1390 * point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001391 * This function is usually called in the thermal zone device .bind callback.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001392 *
1393 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001394 */
1395int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1396 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +08001397 struct thermal_cooling_device *cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001398 unsigned long upper, unsigned long lower,
1399 unsigned int weight)
Zhang Rui203d3d42008-01-17 15:51:08 +08001400{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001401 struct thermal_instance *dev;
1402 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -05001403 struct thermal_zone_device *pos1;
1404 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +08001405 unsigned long max_state;
Lukasz Majewski9a3031d2014-11-18 11:16:30 +01001406 int result, ret;
Zhang Rui203d3d42008-01-17 15:51:08 +08001407
Len Brown543a9562008-02-07 16:55:08 -05001408 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +08001409 return -EINVAL;
1410
Thomas Sujithc7516702008-02-15 00:58:50 -05001411 list_for_each_entry(pos1, &thermal_tz_list, node) {
1412 if (pos1 == tz)
1413 break;
1414 }
1415 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1416 if (pos2 == cdev)
1417 break;
1418 }
1419
1420 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +08001421 return -EINVAL;
1422
Lukasz Majewski9a3031d2014-11-18 11:16:30 +01001423 ret = cdev->ops->get_max_state(cdev, &max_state);
1424 if (ret)
1425 return ret;
Zhang Rui9d998422012-06-26 16:35:57 +08001426
1427 /* lower default 0, upper default max_state */
1428 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1429 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1430
1431 if (lower > upper || upper > max_state)
1432 return -EINVAL;
1433
Zhang Rui203d3d42008-01-17 15:51:08 +08001434 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001435 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001436 if (!dev)
1437 return -ENOMEM;
1438 dev->tz = tz;
1439 dev->cdev = cdev;
1440 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +08001441 dev->upper = upper;
1442 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +08001443 dev->target = THERMAL_NO_TARGET;
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001444 dev->weight = weight;
Zhang Rui74051ba2012-06-26 16:27:22 +08001445
Zhang Rui203d3d42008-01-17 15:51:08 +08001446 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1447 if (result)
1448 goto free_mem;
1449
1450 sprintf(dev->name, "cdev%d", dev->id);
1451 result =
1452 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1453 if (result)
1454 goto release_idr;
1455
1456 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -07001457 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001458 dev->attr.attr.name = dev->attr_name;
1459 dev->attr.attr.mode = 0444;
1460 dev->attr.show = thermal_cooling_device_trip_point_show;
1461 result = device_create_file(&tz->device, &dev->attr);
1462 if (result)
1463 goto remove_symbol_link;
1464
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001465 snprintf(dev->upper_attr_name, THERMAL_NAME_LENGTH,
1466 "cdev%d_upper_limit", dev->id);
1467 sysfs_attr_init(&dev->upper_attr.attr);
1468 dev->upper_attr.attr.name = dev->upper_attr_name;
1469 dev->upper_attr.attr.mode = 0444;
1470 dev->upper_attr.show = thermal_cooling_device_upper_limit_show;
1471 result = device_create_file(&tz->device, &dev->upper_attr);
1472 if (result)
1473 goto remove_trip_file;
1474
1475 snprintf(dev->lower_attr_name, THERMAL_NAME_LENGTH,
1476 "cdev%d_lower_limit", dev->id);
1477 sysfs_attr_init(&dev->lower_attr.attr);
1478 dev->lower_attr.attr.name = dev->lower_attr_name;
1479 dev->lower_attr.attr.mode = 0444;
1480 dev->lower_attr.show = thermal_cooling_device_lower_limit_show;
1481 result = device_create_file(&tz->device, &dev->lower_attr);
1482 if (result)
1483 goto remove_upper_file;
1484
Javi Merinodb916512015-02-18 16:04:24 +00001485 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
1486 sysfs_attr_init(&dev->weight_attr.attr);
1487 dev->weight_attr.attr.name = dev->weight_attr_name;
1488 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
1489 dev->weight_attr.show = thermal_cooling_device_weight_show;
1490 dev->weight_attr.store = thermal_cooling_device_weight_store;
1491 result = device_create_file(&tz->device, &dev->weight_attr);
1492 if (result)
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001493 goto remove_lower_file;
Javi Merinodb916512015-02-18 16:04:24 +00001494
Zhang Rui203d3d42008-01-17 15:51:08 +08001495 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001496 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001497 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +08001498 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1499 result = -EEXIST;
1500 break;
1501 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001502 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001503 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001504 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
Chen Yu4511f712015-10-30 16:32:10 +08001505 atomic_set(&tz->need_update, 1);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001506 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001507 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001508 mutex_unlock(&tz->lock);
1509
1510 if (!result)
1511 return 0;
1512
Javi Merinodb916512015-02-18 16:04:24 +00001513 device_remove_file(&tz->device, &dev->weight_attr);
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001514remove_lower_file:
1515 device_remove_file(&tz->device, &dev->lower_attr);
1516remove_upper_file:
1517 device_remove_file(&tz->device, &dev->upper_attr);
Javi Merinodb916512015-02-18 16:04:24 +00001518remove_trip_file:
Zhang Rui203d3d42008-01-17 15:51:08 +08001519 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -07001520remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +08001521 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -07001522release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +08001523 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -07001524free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +08001525 kfree(dev);
1526 return result;
1527}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001528EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001529
1530/**
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001531 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1532 * thermal zone.
1533 * @tz: pointer to a struct thermal_zone_device.
Zhang Rui203d3d42008-01-17 15:51:08 +08001534 * @trip: indicates which trip point the cooling devices is
1535 * associated with in this thermal zone.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001536 * @cdev: pointer to a struct thermal_cooling_device.
Len Brown543a9562008-02-07 16:55:08 -05001537 *
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001538 * This interface function unbind a thermal cooling device from the certain
1539 * trip point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001540 * This function is usually called in the thermal zone device .unbind callback.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001541 *
1542 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001543 */
1544int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1545 int trip,
1546 struct thermal_cooling_device *cdev)
1547{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001548 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +08001549
1550 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001551 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001552 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -05001553 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001554 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001555 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001556 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001557 mutex_unlock(&tz->lock);
1558 goto unbind;
1559 }
1560 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001561 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001562 mutex_unlock(&tz->lock);
1563
1564 return -ENODEV;
1565
Joe Perchescaca8b82012-03-21 12:55:02 -07001566unbind:
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001567 device_remove_file(&tz->device, &pos->lower_attr);
1568 device_remove_file(&tz->device, &pos->upper_attr);
Viresh Kumar528464e2015-07-23 14:32:32 +05301569 device_remove_file(&tz->device, &pos->weight_attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001570 device_remove_file(&tz->device, &pos->attr);
1571 sysfs_remove_link(&tz->device.kobj, pos->name);
1572 release_idr(&tz->idr, &tz->lock, pos->id);
1573 kfree(pos);
1574 return 0;
1575}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001576EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001577
1578static void thermal_release(struct device *dev)
1579{
1580 struct thermal_zone_device *tz;
1581 struct thermal_cooling_device *cdev;
1582
Joe Perchescaca8b82012-03-21 12:55:02 -07001583 if (!strncmp(dev_name(dev), "thermal_zone",
1584 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001585 tz = to_thermal_zone(dev);
1586 kfree(tz);
durgadoss.r@intel.com732e4c82013-10-02 00:08:00 +05301587 } else if(!strncmp(dev_name(dev), "cooling_device",
1588 sizeof("cooling_device") - 1)){
Zhang Rui203d3d42008-01-17 15:51:08 +08001589 cdev = to_cooling_device(dev);
1590 kfree(cdev);
1591 }
1592}
1593
1594static struct class thermal_class = {
1595 .name = "thermal",
1596 .dev_release = thermal_release,
1597};
1598
1599/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001600 * __thermal_cooling_device_register() - register a new thermal cooling device
1601 * @np: a pointer to a device tree node.
Zhang Rui203d3d42008-01-17 15:51:08 +08001602 * @type: the thermal cooling device type.
1603 * @devdata: device private data.
1604 * @ops: standard thermal cooling devices callbacks.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001605 *
1606 * This interface function adds a new thermal cooling device (fan/processor/...)
1607 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1608 * to all the thermal zone devices registered at the same time.
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001609 * It also gives the opportunity to link the cooling device to a device tree
1610 * node, so that it can be bound to a thermal zone created out of device tree.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001611 *
1612 * Return: a pointer to the created struct thermal_cooling_device or an
1613 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001614 */
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001615static struct thermal_cooling_device *
1616__thermal_cooling_device_register(struct device_node *np,
1617 char *type, void *devdata,
1618 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001619{
1620 struct thermal_cooling_device *cdev;
Chen Yu4511f712015-10-30 16:32:10 +08001621 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001622 int result;
1623
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001624 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001625 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001626
1627 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001628 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001629 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001630
1631 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1632 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001633 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001634
1635 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1636 if (result) {
1637 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001638 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001639 }
1640
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001641 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +08001642 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001643 INIT_LIST_HEAD(&cdev->thermal_instances);
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001644 cdev->np = np;
Zhang Rui203d3d42008-01-17 15:51:08 +08001645 cdev->ops = ops;
Ni Wade5ca0cce2014-02-17 11:02:55 +08001646 cdev->updated = false;
Zhang Rui203d3d42008-01-17 15:51:08 +08001647 cdev->device.class = &thermal_class;
Matthias Kaehlcke2dc10f82015-02-20 18:10:08 -08001648 cdev->device.groups = cooling_device_attr_groups;
Zhang Rui203d3d42008-01-17 15:51:08 +08001649 cdev->devdata = devdata;
Ram Chandrasekar5ab1c082016-09-20 17:09:28 -06001650 cdev->sysfs_cur_state_req = 0;
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001651 cdev->sysfs_min_state_req = ULONG_MAX;
Kay Sievers354655e2009-01-06 10:44:37 -08001652 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001653 result = device_register(&cdev->device);
1654 if (result) {
1655 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1656 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001657 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001658 }
1659
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301660 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +08001661 mutex_lock(&thermal_list_lock);
1662 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001663 mutex_unlock(&thermal_list_lock);
1664
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301665 /* Update binding information for 'this' new cdev */
1666 bind_cdev(cdev);
1667
Chen Yu4511f712015-10-30 16:32:10 +08001668 mutex_lock(&thermal_list_lock);
1669 list_for_each_entry(pos, &thermal_tz_list, node)
1670 if (atomic_cmpxchg(&pos->need_update, 1, 0))
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07001671 thermal_zone_device_update(pos,
1672 THERMAL_EVENT_UNSPECIFIED);
Chen Yu4511f712015-10-30 16:32:10 +08001673 mutex_unlock(&thermal_list_lock);
1674
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301675 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001676}
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001677
1678/**
1679 * thermal_cooling_device_register() - register a new thermal cooling device
1680 * @type: the thermal cooling device type.
1681 * @devdata: device private data.
1682 * @ops: standard thermal cooling devices callbacks.
1683 *
1684 * This interface function adds a new thermal cooling device (fan/processor/...)
1685 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1686 * to all the thermal zone devices registered at the same time.
1687 *
1688 * Return: a pointer to the created struct thermal_cooling_device or an
1689 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1690 */
1691struct thermal_cooling_device *
1692thermal_cooling_device_register(char *type, void *devdata,
1693 const struct thermal_cooling_device_ops *ops)
1694{
1695 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1696}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001697EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001698
1699/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001700 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1701 * @np: a pointer to a device tree node.
1702 * @type: the thermal cooling device type.
1703 * @devdata: device private data.
1704 * @ops: standard thermal cooling devices callbacks.
1705 *
1706 * This function will register a cooling device with device tree node reference.
1707 * This interface function adds a new thermal cooling device (fan/processor/...)
1708 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1709 * to all the thermal zone devices registered at the same time.
1710 *
1711 * Return: a pointer to the created struct thermal_cooling_device or an
1712 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1713 */
1714struct thermal_cooling_device *
1715thermal_of_cooling_device_register(struct device_node *np,
1716 char *type, void *devdata,
1717 const struct thermal_cooling_device_ops *ops)
1718{
1719 return __thermal_cooling_device_register(np, type, devdata, ops);
1720}
1721EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1722
1723/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001724 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001725 * @cdev: the thermal cooling device to remove.
1726 *
1727 * thermal_cooling_device_unregister() must be called when the device is no
1728 * longer needed.
1729 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301730void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001731{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301732 int i;
1733 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001734 struct thermal_zone_device *tz;
1735 struct thermal_cooling_device *pos = NULL;
1736
1737 if (!cdev)
1738 return;
1739
1740 mutex_lock(&thermal_list_lock);
1741 list_for_each_entry(pos, &thermal_cdev_list, node)
1742 if (pos == cdev)
1743 break;
1744 if (pos != cdev) {
1745 /* thermal cooling device not found */
1746 mutex_unlock(&thermal_list_lock);
1747 return;
1748 }
1749 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301750
1751 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001752 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301753 if (tz->ops->unbind) {
1754 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001755 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301756 }
1757
1758 if (!tz->tzp || !tz->tzp->tbp)
1759 continue;
1760
1761 tzp = tz->tzp;
1762 for (i = 0; i < tzp->num_tbps; i++) {
1763 if (tzp->tbp[i].cdev == cdev) {
1764 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1765 tzp->tbp[i].cdev = NULL;
1766 }
1767 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001768 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301769
Zhang Rui203d3d42008-01-17 15:51:08 +08001770 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301771
Zhang Rui203d3d42008-01-17 15:51:08 +08001772 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001773 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001774 device_remove_file(&cdev->device, &dev_attr_max_state);
1775 device_remove_file(&cdev->device, &dev_attr_cur_state);
1776
1777 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1778 device_unregister(&cdev->device);
1779 return;
1780}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001781EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001782
Durgadoss Rdc765482012-09-18 11:05:00 +05301783void thermal_cdev_update(struct thermal_cooling_device *cdev)
Zhang Ruice119f82012-06-27 14:13:04 +08001784{
1785 struct thermal_instance *instance;
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001786 unsigned long current_target = 0, min_target = ULONG_MAX;
Zhang Ruice119f82012-06-27 14:13:04 +08001787
Zhang Ruif4a821c2012-07-24 16:56:21 +08001788 mutex_lock(&cdev->lock);
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01001789 /* cooling device is updated*/
1790 if (cdev->updated) {
1791 mutex_unlock(&cdev->lock);
1792 return;
1793 }
1794
Zhang Ruice119f82012-06-27 14:13:04 +08001795 /* Make sure cdev enters the deepest cooling state */
Ram Chandrasekar5ab1c082016-09-20 17:09:28 -06001796 current_target = cdev->sysfs_cur_state_req;
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001797 min_target = cdev->sysfs_min_state_req;
Zhang Ruice119f82012-06-27 14:13:04 +08001798 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
Aaron Lu06475b52013-12-02 13:54:26 +08001799 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1800 instance->tz->id, instance->target);
Zhang Ruice119f82012-06-27 14:13:04 +08001801 if (instance->target == THERMAL_NO_TARGET)
1802 continue;
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001803 if (instance->tz->governor->min_state_throttle) {
1804 if (instance->target < min_target)
1805 min_target = instance->target;
1806 } else {
1807 if (instance->target > current_target)
1808 current_target = instance->target;
1809 }
Zhang Ruice119f82012-06-27 14:13:04 +08001810 }
Ram Chandrasekar5ab1c082016-09-20 17:09:28 -06001811 cdev->ops->set_cur_state(cdev, current_target);
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001812 if (cdev->ops->set_min_state)
1813 cdev->ops->set_min_state(cdev, min_target);
Zhang Ruice119f82012-06-27 14:13:04 +08001814 cdev->updated = true;
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01001815 mutex_unlock(&cdev->lock);
Ram Chandrasekar5b1c0d12017-03-28 11:35:40 -06001816 trace_cdev_update(cdev, current_target, min_target);
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001817 dev_dbg(&cdev->device, "set to state %lu min state %lu\n",
1818 current_target, min_target);
Zhang Ruice119f82012-06-27 14:13:04 +08001819}
Durgadoss Rdc765482012-09-18 11:05:00 +05301820EXPORT_SYMBOL(thermal_cdev_update);
Zhang Ruice119f82012-06-27 14:13:04 +08001821
Matthew Garrettb1569e92008-12-03 17:55:32 +00001822/**
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001823 * thermal_notify_framework - Sensor drivers use this API to notify framework
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301824 * @tz: thermal zone device
1825 * @trip: indicates which trip point has been crossed
1826 *
1827 * This function handles the trip events from sensor drivers. It starts
1828 * throttling the cooling devices according to the policy configured.
1829 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1830 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1831 * The throttling policy is based on the configured platform data; if no
1832 * platform data is provided, this uses the step_wise throttling policy.
1833 */
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001834void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301835{
1836 handle_thermal_trip(tz, trip);
1837}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001838EXPORT_SYMBOL_GPL(thermal_notify_framework);
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301839
1840/**
Eduardo Valentin269c1742013-04-23 21:48:19 +00001841 * create_trip_attrs() - create attributes for trip points
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001842 * @tz: the thermal zone device
1843 * @mask: Writeable trip point bitmap.
Eduardo Valentin269c1742013-04-23 21:48:19 +00001844 *
1845 * helper function to instantiate sysfs entries for every trip
1846 * point and its properties of a struct thermal_zone_device.
1847 *
1848 * Return: 0 on success, the proper error value otherwise.
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001849 */
1850static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1851{
1852 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001853 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001854
Durgadoss R27365a62012-07-25 10:10:59 +08001855 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001856 if (!tz->trip_type_attrs)
1857 return -ENOMEM;
1858
Durgadoss R27365a62012-07-25 10:10:59 +08001859 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001860 if (!tz->trip_temp_attrs) {
1861 kfree(tz->trip_type_attrs);
1862 return -ENOMEM;
1863 }
1864
Durgadoss R27365a62012-07-25 10:10:59 +08001865 if (tz->ops->get_trip_hyst) {
1866 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1867 if (!tz->trip_hyst_attrs) {
1868 kfree(tz->trip_type_attrs);
1869 kfree(tz->trip_temp_attrs);
1870 return -ENOMEM;
1871 }
1872 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001873
Durgadoss R27365a62012-07-25 10:10:59 +08001874
1875 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001876 /* create trip type attribute */
1877 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1878 "trip_point_%d_type", indx);
1879
1880 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1881 tz->trip_type_attrs[indx].attr.attr.name =
1882 tz->trip_type_attrs[indx].name;
1883 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1884 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1885
1886 device_create_file(&tz->device,
1887 &tz->trip_type_attrs[indx].attr);
1888
1889 /* create trip temp attribute */
1890 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1891 "trip_point_%d_temp", indx);
1892
1893 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1894 tz->trip_temp_attrs[indx].attr.attr.name =
1895 tz->trip_temp_attrs[indx].name;
1896 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1897 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
Punit Agrawal35e94642015-03-03 10:43:03 +00001898 if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS) &&
1899 mask & (1 << indx)) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001900 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1901 tz->trip_temp_attrs[indx].attr.store =
1902 trip_point_temp_store;
1903 }
1904
1905 device_create_file(&tz->device,
1906 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001907
1908 /* create Optional trip hyst attribute */
1909 if (!tz->ops->get_trip_hyst)
1910 continue;
1911 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1912 "trip_point_%d_hyst", indx);
1913
1914 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1915 tz->trip_hyst_attrs[indx].attr.attr.name =
1916 tz->trip_hyst_attrs[indx].name;
1917 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1918 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1919 if (tz->ops->set_trip_hyst) {
1920 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1921 tz->trip_hyst_attrs[indx].attr.store =
1922 trip_point_hyst_store;
1923 }
1924
1925 device_create_file(&tz->device,
1926 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001927 }
1928 return 0;
1929}
1930
1931static void remove_trip_attrs(struct thermal_zone_device *tz)
1932{
1933 int indx;
1934
1935 for (indx = 0; indx < tz->trips; indx++) {
1936 device_remove_file(&tz->device,
1937 &tz->trip_type_attrs[indx].attr);
1938 device_remove_file(&tz->device,
1939 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001940 if (tz->ops->get_trip_hyst)
1941 device_remove_file(&tz->device,
1942 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001943 }
1944 kfree(tz->trip_type_attrs);
1945 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001946 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001947}
1948
1949/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001950 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001951 * @type: the thermal zone device type
1952 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001953 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001954 * @devdata: private device data
1955 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301956 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001957 * @passive_delay: number of milliseconds to wait between polls when
1958 * performing passive cooling
1959 * @polling_delay: number of milliseconds to wait between polls when checking
1960 * whether trip points have been crossed (0 for interrupt
1961 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001962 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001963 * This interface function adds a new thermal zone device (sensor) to
1964 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1965 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08001966 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001967 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001968 *
1969 * Return: a pointer to the created struct thermal_zone_device or an
1970 * in case of error, an ERR_PTR. Caller must check return value with
1971 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001972 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001973struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001974 int trips, int mask, void *devdata,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001975 struct thermal_zone_device_ops *ops,
Javi Merino6b775e82015-03-02 17:17:19 +00001976 struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001977 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001978{
1979 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001980 enum thermal_trip_type trip_type;
Zhang Rui81ad4272016-03-18 10:03:24 +08001981 int trip_temp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001982 int result;
1983 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001984 int passive = 0;
Javi Merinoe33df1d2015-02-26 19:00:27 +00001985 struct thermal_governor *governor;
Zhang Rui203d3d42008-01-17 15:51:08 +08001986
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001987 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001988 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001989
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001990 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001991 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001992
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04001993 if (!ops)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001994 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001995
Jonghwa Lee83720d02013-05-18 09:50:26 +00001996 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00001997 return ERR_PTR(-EINVAL);
1998
Zhang Rui203d3d42008-01-17 15:51:08 +08001999 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
2000 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002001 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08002002
Zhang Rui2d374132012-06-27 10:09:00 +08002003 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08002004 idr_init(&tz->idr);
2005 mutex_init(&tz->lock);
2006 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
2007 if (result) {
2008 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002009 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08002010 }
2011
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00002012 strlcpy(tz->type, type ? : "", sizeof(tz->type));
Zhang Rui203d3d42008-01-17 15:51:08 +08002013 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05302014 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08002015 tz->device.class = &thermal_class;
2016 tz->devdata = devdata;
2017 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00002018 tz->passive_delay = passive_delay;
2019 tz->polling_delay = polling_delay;
Chen Yu4511f712015-10-30 16:32:10 +08002020 /* A new thermal zone needs to be updated anyway. */
2021 atomic_set(&tz->need_update, 1);
Matthew Garrettb1569e92008-12-03 17:55:32 +00002022
Kay Sievers354655e2009-01-06 10:44:37 -08002023 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08002024 result = device_register(&tz->device);
2025 if (result) {
2026 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
2027 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002028 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08002029 }
2030
2031 /* sys I/F */
2032 if (type) {
2033 result = device_create_file(&tz->device, &dev_attr_type);
2034 if (result)
2035 goto unregister;
2036 }
2037
2038 result = device_create_file(&tz->device, &dev_attr_temp);
2039 if (result)
2040 goto unregister;
2041
2042 if (ops->get_mode) {
2043 result = device_create_file(&tz->device, &dev_attr_mode);
2044 if (result)
2045 goto unregister;
2046 }
2047
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002048 result = create_trip_attrs(tz, mask);
2049 if (result)
2050 goto unregister;
2051
Zhang Rui203d3d42008-01-17 15:51:08 +08002052 for (count = 0; count < trips; count++) {
Zhang Rui81ad4272016-03-18 10:03:24 +08002053 if (tz->ops->get_trip_type(tz, count, &trip_type))
2054 set_bit(count, &tz->trips_disabled);
Matthew Garrett03a971a2008-12-03 18:00:38 +00002055 if (trip_type == THERMAL_TRIP_PASSIVE)
2056 passive = 1;
Zhang Rui81ad4272016-03-18 10:03:24 +08002057 if (tz->ops->get_trip_temp(tz, count, &trip_temp))
2058 set_bit(count, &tz->trips_disabled);
2059 /* Check for bogus trip points */
2060 if (trip_temp == 0)
2061 set_bit(count, &tz->trips_disabled);
Zhang Rui203d3d42008-01-17 15:51:08 +08002062 }
2063
Durgadoss R5a2c0902012-09-18 11:04:58 +05302064 if (!passive) {
2065 result = device_create_file(&tz->device, &dev_attr_passive);
2066 if (result)
2067 goto unregister;
2068 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00002069
Sascha Hauer79e54212015-07-06 09:46:16 +02002070 if (IS_ENABLED(CONFIG_THERMAL_EMULATION)) {
2071 result = device_create_file(&tz->device, &dev_attr_emul_temp);
2072 if (result)
2073 goto unregister;
2074 }
2075
Durgadoss R5a2c0902012-09-18 11:04:58 +05302076 /* Create policy attribute */
2077 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00002078 if (result)
2079 goto unregister;
2080
Javi Merino9f382712015-03-26 15:53:02 +00002081 /* Add thermal zone params */
2082 result = create_tzp_attrs(&tz->device);
2083 if (result)
2084 goto unregister;
2085
Ni Wade25a0a5c2015-07-14 15:40:56 +08002086 /* Create available_policies attribute */
2087 result = device_create_file(&tz->device, &dev_attr_available_policies);
2088 if (result)
2089 goto unregister;
2090
Durgadoss Ra4a15482012-09-18 11:04:57 +05302091 /* Update 'this' zone's governor information */
2092 mutex_lock(&thermal_governor_lock);
2093
2094 if (tz->tzp)
Javi Merinoe33df1d2015-02-26 19:00:27 +00002095 governor = __find_governor(tz->tzp->governor_name);
Durgadoss Ra4a15482012-09-18 11:04:57 +05302096 else
Javi Merinoe33df1d2015-02-26 19:00:27 +00002097 governor = def_governor;
2098
2099 result = thermal_set_governor(tz, governor);
2100 if (result) {
2101 mutex_unlock(&thermal_governor_lock);
2102 goto unregister;
2103 }
Durgadoss Ra4a15482012-09-18 11:04:57 +05302104
2105 mutex_unlock(&thermal_governor_lock);
2106
Eduardo Valentinccba4ff2013-08-15 11:34:17 -04002107 if (!tz->tzp || !tz->tzp->no_hwmon) {
2108 result = thermal_add_hwmon_sysfs(tz);
2109 if (result)
2110 goto unregister;
2111 }
Zhang Ruie68b16a2008-04-21 16:07:52 +08002112
Zhang Rui203d3d42008-01-17 15:51:08 +08002113 mutex_lock(&thermal_list_lock);
2114 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08002115 mutex_unlock(&thermal_list_lock);
2116
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05302117 /* Bind cooling devices for this zone */
2118 bind_tz(tz);
2119
Matthew Garrettb1569e92008-12-03 17:55:32 +00002120 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
2121
Zhang Ruibb431ba2015-10-30 16:31:47 +08002122 thermal_zone_device_reset(tz);
Chen Yu4511f712015-10-30 16:32:10 +08002123 /* Update the new thermal zone and mark it as already updated. */
2124 if (atomic_cmpxchg(&tz->need_update, 1, 0))
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07002125 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Matthew Garrettb1569e92008-12-03 17:55:32 +00002126
Yao Dongdong14015862014-10-28 07:40:25 +00002127 return tz;
Zhang Rui203d3d42008-01-17 15:51:08 +08002128
Joe Perchescaca8b82012-03-21 12:55:02 -07002129unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08002130 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
2131 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002132 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08002133}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00002134EXPORT_SYMBOL_GPL(thermal_zone_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08002135
2136/**
2137 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08002138 * @tz: the thermal zone device to remove
2139 */
2140void thermal_zone_device_unregister(struct thermal_zone_device *tz)
2141{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05302142 int i;
2143 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08002144 struct thermal_cooling_device *cdev;
2145 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08002146
2147 if (!tz)
2148 return;
2149
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05302150 tzp = tz->tzp;
2151
Zhang Rui203d3d42008-01-17 15:51:08 +08002152 mutex_lock(&thermal_list_lock);
2153 list_for_each_entry(pos, &thermal_tz_list, node)
2154 if (pos == tz)
2155 break;
2156 if (pos != tz) {
2157 /* thermal zone device not found */
2158 mutex_unlock(&thermal_list_lock);
2159 return;
2160 }
2161 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05302162
2163 /* Unbind all cdevs associated with 'this' thermal zone */
2164 list_for_each_entry(cdev, &thermal_cdev_list, node) {
2165 if (tz->ops->unbind) {
2166 tz->ops->unbind(tz, cdev);
2167 continue;
2168 }
2169
2170 if (!tzp || !tzp->tbp)
2171 break;
2172
2173 for (i = 0; i < tzp->num_tbps; i++) {
2174 if (tzp->tbp[i].cdev == cdev) {
2175 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
2176 tzp->tbp[i].cdev = NULL;
2177 }
2178 }
2179 }
2180
Zhang Rui203d3d42008-01-17 15:51:08 +08002181 mutex_unlock(&thermal_list_lock);
2182
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -06002183 thermal_zone_device_set_polling(NULL, tz, 0);
Matthew Garrettb1569e92008-12-03 17:55:32 +00002184
Zhang Rui203d3d42008-01-17 15:51:08 +08002185 if (tz->type[0])
2186 device_remove_file(&tz->device, &dev_attr_type);
2187 device_remove_file(&tz->device, &dev_attr_temp);
2188 if (tz->ops->get_mode)
2189 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05302190 device_remove_file(&tz->device, &dev_attr_policy);
Ni Wade25a0a5c2015-07-14 15:40:56 +08002191 device_remove_file(&tz->device, &dev_attr_available_policies);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002192 remove_trip_attrs(tz);
Javi Merinoe33df1d2015-02-26 19:00:27 +00002193 thermal_set_governor(tz, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08002194
Zhang Ruie68b16a2008-04-21 16:07:52 +08002195 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08002196 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
2197 idr_destroy(&tz->idr);
2198 mutex_destroy(&tz->lock);
2199 device_unregister(&tz->device);
2200 return;
2201}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00002202EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08002203
Eduardo Valentin63c4d912013-04-05 12:32:28 +00002204/**
2205 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
2206 * @name: thermal zone name to fetch the temperature
2207 *
2208 * When only one zone is found with the passed name, returns a reference to it.
2209 *
2210 * Return: On success returns a reference to an unique thermal zone with
2211 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
2212 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
2213 */
2214struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
2215{
2216 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
2217 unsigned int found = 0;
2218
2219 if (!name)
2220 goto exit;
2221
2222 mutex_lock(&thermal_list_lock);
2223 list_for_each_entry(pos, &thermal_tz_list, node)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -07002224 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
Eduardo Valentin63c4d912013-04-05 12:32:28 +00002225 found++;
2226 ref = pos;
2227 }
2228 mutex_unlock(&thermal_list_lock);
2229
2230 /* nothing has been found, thus an error code for it */
2231 if (found == 0)
2232 ref = ERR_PTR(-ENODEV);
2233 else if (found > 1)
2234 /* Success only when an unique zone is found */
2235 ref = ERR_PTR(-EEXIST);
2236
2237exit:
2238 return ref;
2239}
2240EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
2241
Rajendra Nayak4a7069a2016-05-05 14:21:42 +05302242/**
2243 * thermal_zone_get_slope - return the slope attribute of the thermal zone
2244 * @tz: thermal zone device with the slope attribute
2245 *
2246 * Return: If the thermal zone device has a slope attribute, return it, else
2247 * return 1.
2248 */
2249int thermal_zone_get_slope(struct thermal_zone_device *tz)
2250{
2251 if (tz && tz->tzp)
2252 return tz->tzp->slope;
2253 return 1;
2254}
2255EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
2256
2257/**
2258 * thermal_zone_get_offset - return the offset attribute of the thermal zone
2259 * @tz: thermal zone device with the offset attribute
2260 *
2261 * Return: If the thermal zone device has a offset attribute, return it, else
2262 * return 0.
2263 */
2264int thermal_zone_get_offset(struct thermal_zone_device *tz)
2265{
2266 if (tz && tz->tzp)
2267 return tz->tzp->offset;
2268 return 0;
2269}
2270EXPORT_SYMBOL_GPL(thermal_zone_get_offset);
2271
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002272#ifdef CONFIG_NET
Johannes Berg2a94fe42013-11-19 15:19:39 +01002273static const struct genl_multicast_group thermal_event_mcgrps[] = {
2274 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
2275};
2276
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002277static struct genl_family thermal_event_genl_family = {
2278 .id = GENL_ID_GENERATE,
2279 .name = THERMAL_GENL_FAMILY_NAME,
2280 .version = THERMAL_GENL_VERSION,
2281 .maxattr = THERMAL_GENL_ATTR_MAX,
Johannes Berg2a94fe42013-11-19 15:19:39 +01002282 .mcgrps = thermal_event_mcgrps,
2283 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002284};
2285
Lina Iyer9bf792d2016-07-11 13:27:11 -06002286static int allow_netlink_events;
2287
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00002288int thermal_generate_netlink_event(struct thermal_zone_device *tz,
2289 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05302290{
2291 struct sk_buff *skb;
2292 struct nlattr *attr;
2293 struct thermal_genl_event *thermal_event;
2294 void *msg_header;
2295 int size;
2296 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07002297 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05302298
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00002299 if (!tz)
2300 return -EINVAL;
2301
Lina Iyer9bf792d2016-07-11 13:27:11 -06002302 if (!allow_netlink_events)
2303 return -ENODEV;
2304
R.Durgadoss4cb18722010-10-27 03:33:29 +05302305 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07002306 size = nla_total_size(sizeof(struct thermal_genl_event)) +
2307 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302308
2309 skb = genlmsg_new(size, GFP_ATOMIC);
2310 if (!skb)
2311 return -ENOMEM;
2312
2313 /* add the genetlink message header */
2314 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
2315 &thermal_event_genl_family, 0,
2316 THERMAL_GENL_CMD_EVENT);
2317 if (!msg_header) {
2318 nlmsg_free(skb);
2319 return -ENOMEM;
2320 }
2321
2322 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07002323 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
2324 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05302325
2326 if (!attr) {
2327 nlmsg_free(skb);
2328 return -EINVAL;
2329 }
2330
2331 thermal_event = nla_data(attr);
2332 if (!thermal_event) {
2333 nlmsg_free(skb);
2334 return -EINVAL;
2335 }
2336
2337 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
2338
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00002339 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05302340 thermal_event->event = event;
2341
2342 /* send multicast genetlink message */
Johannes Berg053c0952015-01-16 22:09:00 +01002343 genlmsg_end(skb, msg_header);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302344
Johannes Berg68eb5502013-11-19 15:19:38 +01002345 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01002346 0, GFP_ATOMIC);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302347 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00002348 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302349
2350 return result;
2351}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00002352EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302353
2354static int genetlink_init(void)
2355{
Lina Iyer9bf792d2016-07-11 13:27:11 -06002356 int ret;
2357
2358 ret = genl_register_family(&thermal_event_genl_family);
2359 if (!ret)
2360 allow_netlink_events = true;
2361
2362 return ret;
R.Durgadoss4cb18722010-10-27 03:33:29 +05302363}
2364
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002365static void genetlink_exit(void)
2366{
2367 genl_unregister_family(&thermal_event_genl_family);
2368}
2369#else /* !CONFIG_NET */
2370static inline int genetlink_init(void) { return 0; }
2371static inline void genetlink_exit(void) {}
Lina Iyer9bf792d2016-07-11 13:27:11 -06002372static inline int thermal_generate_netlink_event(struct thermal_zone_device *tz,
2373 enum events event) { return -ENODEV; }
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002374#endif /* !CONFIG_NET */
2375
Zhang Rui80a26a52013-03-26 16:38:29 +08002376static int __init thermal_register_governors(void)
2377{
2378 int result;
2379
2380 result = thermal_gov_step_wise_register();
2381 if (result)
2382 return result;
2383
2384 result = thermal_gov_fair_share_register();
2385 if (result)
2386 return result;
2387
Peter Feuerere4dbf982014-07-22 17:37:13 +02002388 result = thermal_gov_bang_bang_register();
2389 if (result)
2390 return result;
2391
Javi Merino6b775e82015-03-02 17:17:19 +00002392 result = thermal_gov_user_space_register();
2393 if (result)
2394 return result;
2395
Ram Chandrasekar6d44a342016-07-07 11:09:52 -06002396 result = thermal_gov_low_limits_register();
2397 if (result)
2398 return result;
2399
Javi Merino6b775e82015-03-02 17:17:19 +00002400 return thermal_gov_power_allocator_register();
Zhang Rui80a26a52013-03-26 16:38:29 +08002401}
2402
2403static void thermal_unregister_governors(void)
2404{
2405 thermal_gov_step_wise_unregister();
2406 thermal_gov_fair_share_unregister();
Peter Feuerere4dbf982014-07-22 17:37:13 +02002407 thermal_gov_bang_bang_unregister();
Zhang Rui80a26a52013-03-26 16:38:29 +08002408 thermal_gov_user_space_unregister();
Ram Chandrasekar6d44a342016-07-07 11:09:52 -06002409 thermal_gov_low_limits_unregister();
Javi Merino6b775e82015-03-02 17:17:19 +00002410 thermal_gov_power_allocator_unregister();
Zhang Rui80a26a52013-03-26 16:38:29 +08002411}
2412
Zhang Ruiff140fe2015-10-30 16:31:58 +08002413static int thermal_pm_notify(struct notifier_block *nb,
2414 unsigned long mode, void *_unused)
2415{
2416 struct thermal_zone_device *tz;
2417
2418 switch (mode) {
2419 case PM_HIBERNATION_PREPARE:
2420 case PM_RESTORE_PREPARE:
2421 case PM_SUSPEND_PREPARE:
2422 atomic_set(&in_suspend, 1);
2423 break;
2424 case PM_POST_HIBERNATION:
2425 case PM_POST_RESTORE:
2426 case PM_POST_SUSPEND:
2427 atomic_set(&in_suspend, 0);
2428 list_for_each_entry(tz, &thermal_tz_list, node) {
2429 thermal_zone_device_reset(tz);
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07002430 thermal_zone_device_update(tz,
2431 THERMAL_EVENT_UNSPECIFIED);
Zhang Ruiff140fe2015-10-30 16:31:58 +08002432 }
2433 break;
2434 default:
2435 break;
2436 }
2437 return 0;
2438}
2439
2440static struct notifier_block thermal_pm_nb = {
2441 .notifier_call = thermal_pm_notify,
2442};
2443
Zhang Rui203d3d42008-01-17 15:51:08 +08002444static int __init thermal_init(void)
2445{
Zhang Rui80a26a52013-03-26 16:38:29 +08002446 int result;
2447
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -06002448 thermal_passive_wq = alloc_workqueue("thermal_passive_wq",
2449 WQ_HIGHPRI | WQ_UNBOUND
2450 | WQ_FREEZABLE,
2451 THERMAL_MAX_ACTIVE);
2452 if (!thermal_passive_wq) {
2453 result = -ENOMEM;
2454 goto init_exit;
2455 }
2456
Zhang Rui80a26a52013-03-26 16:38:29 +08002457 result = thermal_register_governors();
2458 if (result)
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -06002459 goto destroy_wq;
Zhang Rui203d3d42008-01-17 15:51:08 +08002460
2461 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08002462 if (result)
2463 goto unregister_governors;
2464
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002465 result = of_parse_thermal_zones();
2466 if (result)
Lina Iyer9bf792d2016-07-11 13:27:11 -06002467 goto exit_zone_parse;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002468
Zhang Ruiff140fe2015-10-30 16:31:58 +08002469 result = register_pm_notifier(&thermal_pm_nb);
2470 if (result)
2471 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
2472 result);
2473
Zhang Rui80a26a52013-03-26 16:38:29 +08002474 return 0;
2475
Lina Iyer9bf792d2016-07-11 13:27:11 -06002476exit_zone_parse:
Zhang Rui80a26a52013-03-26 16:38:29 +08002477 class_unregister(&thermal_class);
Luis Henriques9d367e52014-12-03 21:20:21 +00002478unregister_governors:
2479 thermal_unregister_governors();
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -06002480destroy_wq:
2481 destroy_workqueue(thermal_passive_wq);
Lina Iyer9bf792d2016-07-11 13:27:11 -06002482init_exit:
Zhang Rui80a26a52013-03-26 16:38:29 +08002483 idr_destroy(&thermal_tz_idr);
2484 idr_destroy(&thermal_cdev_idr);
2485 mutex_destroy(&thermal_idr_lock);
2486 mutex_destroy(&thermal_list_lock);
2487 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08002488 return result;
2489}
2490
Ram Chandrasekarfe0ae222017-04-06 12:08:38 -06002491static void thermal_exit(void)
Zhang Rui203d3d42008-01-17 15:51:08 +08002492{
Zhang Ruiff140fe2015-10-30 16:31:58 +08002493 unregister_pm_notifier(&thermal_pm_nb);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002494 of_thermal_destroy_zones();
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -06002495 destroy_workqueue(thermal_passive_wq);
Zhang Rui80a26a52013-03-26 16:38:29 +08002496 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08002497 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08002498 thermal_unregister_governors();
Zhang Rui203d3d42008-01-17 15:51:08 +08002499 idr_destroy(&thermal_tz_idr);
2500 idr_destroy(&thermal_cdev_idr);
2501 mutex_destroy(&thermal_idr_lock);
2502 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08002503 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08002504}
2505
Lina Iyer9bf792d2016-07-11 13:27:11 -06002506static int __init thermal_netlink_init(void)
2507{
2508 int ret = 0;
2509
2510 ret = genetlink_init();
2511 if (!ret)
2512 goto exit_netlink;
2513
2514 thermal_exit();
2515exit_netlink:
2516 return ret;
2517}
2518
2519subsys_initcall(thermal_init);
2520fs_initcall(thermal_netlink_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08002521module_exit(thermal_exit);