blob: 1179bc55da8cb7b96b5ccbd2150262cb91c51352 [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
556 /* No need to change trip points */
557 if (tz->prev_low_trip == low && tz->prev_high_trip == high)
558 goto exit;
559
560 tz->prev_low_trip = low;
561 tz->prev_high_trip = high;
562
563 dev_dbg(&tz->device,
564 "new temperature boundaries: %d < x < %d\n", low, high);
565
566 /*
567 * Set a temperature window. When this window is left the driver
568 * must inform the thermal core via thermal_zone_device_update.
569 */
570 ret = tz->ops->set_trips(tz, low, high);
571 if (ret)
572 dev_err(&tz->device, "Failed to set trips: %d\n", ret);
573
574exit:
575 mutex_unlock(&tz->lock);
576}
577EXPORT_SYMBOL_GPL(thermal_zone_set_trips);
578
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530579static void update_temperature(struct thermal_zone_device *tz)
580{
Sascha Hauer17e83512015-07-24 08:12:54 +0200581 int temp, ret;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530582
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000583 ret = thermal_zone_get_temp(tz, &temp);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530584 if (ret) {
Hans de Goede7e497a72015-03-21 15:02:55 +0100585 if (ret != -EAGAIN)
586 dev_warn(&tz->device,
587 "failed to read out thermal zone (%d)\n",
588 ret);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000589 return;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530590 }
591
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000592 mutex_lock(&tz->lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530593 tz->last_temperature = tz->temperature;
594 tz->temperature = temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530595 mutex_unlock(&tz->lock);
Aaron Lu06475b52013-12-02 13:54:26 +0800596
Punit Agrawal100a8fd2014-07-29 11:50:48 +0100597 trace_thermal_temperature(tz);
Zhang Ruibb431ba2015-10-30 16:31:47 +0800598 if (tz->last_temperature == THERMAL_TEMP_INVALID)
599 dev_dbg(&tz->device, "last_temperature N/A, current_temperature=%d\n",
600 tz->temperature);
601 else
602 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
603 tz->last_temperature, tz->temperature);
604}
605
606static void thermal_zone_device_reset(struct thermal_zone_device *tz)
607{
608 struct thermal_instance *pos;
609
610 tz->temperature = THERMAL_TEMP_INVALID;
611 tz->passive = 0;
612 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
613 pos->initialized = false;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530614}
615
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700616void thermal_zone_device_update(struct thermal_zone_device *tz,
617 enum thermal_notify_event event)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530618{
619 int count;
620
Zhang Ruiff140fe2015-10-30 16:31:58 +0800621 if (atomic_read(&in_suspend))
622 return;
623
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400624 if (!tz->ops->get_temp)
625 return;
626
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530627 update_temperature(tz);
628
Sascha Hauer060c0342016-06-22 16:42:01 +0800629 thermal_zone_set_trips(tz);
630
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700631 tz->notify_event = event;
632
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530633 for (count = 0; count < tz->trips; count++)
634 handle_thermal_trip(tz, count);
635}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000636EXPORT_SYMBOL_GPL(thermal_zone_device_update);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530637
638static void thermal_zone_device_check(struct work_struct *work)
639{
640 struct thermal_zone_device *tz = container_of(work, struct
641 thermal_zone_device,
642 poll_queue.work);
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700643 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530644}
645
Zhang Rui203d3d42008-01-17 15:51:08 +0800646/* sys I/F for thermal zone */
647
648#define to_thermal_zone(_dev) \
649 container_of(_dev, struct thermal_zone_device, device)
650
651static ssize_t
652type_show(struct device *dev, struct device_attribute *attr, char *buf)
653{
654 struct thermal_zone_device *tz = to_thermal_zone(dev);
655
656 return sprintf(buf, "%s\n", tz->type);
657}
658
659static ssize_t
660temp_show(struct device *dev, struct device_attribute *attr, char *buf)
661{
662 struct thermal_zone_device *tz = to_thermal_zone(dev);
Sascha Hauer17e83512015-07-24 08:12:54 +0200663 int temperature, ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800664
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000665 ret = thermal_zone_get_temp(tz, &temperature);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000666
667 if (ret)
668 return ret;
669
Sascha Hauer17e83512015-07-24 08:12:54 +0200670 return sprintf(buf, "%d\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800671}
672
673static ssize_t
674mode_show(struct device *dev, struct device_attribute *attr, char *buf)
675{
676 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000677 enum thermal_device_mode mode;
678 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800679
680 if (!tz->ops->get_mode)
681 return -EPERM;
682
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000683 result = tz->ops->get_mode(tz, &mode);
684 if (result)
685 return result;
686
687 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
688 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800689}
690
691static ssize_t
692mode_store(struct device *dev, struct device_attribute *attr,
693 const char *buf, size_t count)
694{
695 struct thermal_zone_device *tz = to_thermal_zone(dev);
696 int result;
697
698 if (!tz->ops->set_mode)
699 return -EPERM;
700
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530701 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000702 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530703 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000704 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
705 else
706 result = -EINVAL;
707
Zhang Rui203d3d42008-01-17 15:51:08 +0800708 if (result)
709 return result;
710
711 return count;
712}
713
714static ssize_t
715trip_point_type_show(struct device *dev, struct device_attribute *attr,
716 char *buf)
717{
718 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000719 enum thermal_trip_type type;
720 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800721
722 if (!tz->ops->get_trip_type)
723 return -EPERM;
724
725 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
726 return -EINVAL;
727
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000728 result = tz->ops->get_trip_type(tz, trip, &type);
729 if (result)
730 return result;
731
732 switch (type) {
733 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300734 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000735 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300736 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000737 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300738 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000739 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300740 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000741 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300742 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000743 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800744}
745
746static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800747trip_point_temp_store(struct device *dev, struct device_attribute *attr,
748 const char *buf, size_t count)
749{
750 struct thermal_zone_device *tz = to_thermal_zone(dev);
751 int trip, ret;
Wei Ni1d0fd422016-03-03 17:33:46 +0800752 int temperature;
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800753
754 if (!tz->ops->set_trip_temp)
755 return -EPERM;
756
757 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
758 return -EINVAL;
759
Wei Ni1d0fd422016-03-03 17:33:46 +0800760 if (kstrtoint(buf, 10, &temperature))
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800761 return -EINVAL;
762
763 ret = tz->ops->set_trip_temp(tz, trip, temperature);
Kuninori Morimotoad74e462015-12-15 01:19:53 +0000764 if (ret)
765 return ret;
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800766
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700767 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Kuninori Morimotoad74e462015-12-15 01:19:53 +0000768
769 return count;
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800770}
771
772static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800773trip_point_temp_show(struct device *dev, struct device_attribute *attr,
774 char *buf)
775{
776 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000777 int trip, ret;
Sascha Hauer17e83512015-07-24 08:12:54 +0200778 int temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800779
780 if (!tz->ops->get_trip_temp)
781 return -EPERM;
782
783 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
784 return -EINVAL;
785
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000786 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
787
788 if (ret)
789 return ret;
790
Sascha Hauer17e83512015-07-24 08:12:54 +0200791 return sprintf(buf, "%d\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800792}
793
Matthew Garrett03a971a2008-12-03 18:00:38 +0000794static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800795trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
796 const char *buf, size_t count)
797{
798 struct thermal_zone_device *tz = to_thermal_zone(dev);
799 int trip, ret;
Sascha Hauer17e83512015-07-24 08:12:54 +0200800 int temperature;
Durgadoss R27365a62012-07-25 10:10:59 +0800801
802 if (!tz->ops->set_trip_hyst)
803 return -EPERM;
804
805 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
806 return -EINVAL;
807
Sascha Hauer17e83512015-07-24 08:12:54 +0200808 if (kstrtoint(buf, 10, &temperature))
Durgadoss R27365a62012-07-25 10:10:59 +0800809 return -EINVAL;
810
811 /*
812 * We are not doing any check on the 'temperature' value
813 * here. The driver implementing 'set_trip_hyst' has to
814 * take care of this.
815 */
816 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
817
Sascha Hauer060c0342016-06-22 16:42:01 +0800818 if (!ret)
819 thermal_zone_set_trips(tz);
820
Durgadoss R27365a62012-07-25 10:10:59 +0800821 return ret ? ret : count;
822}
823
824static ssize_t
825trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
826 char *buf)
827{
828 struct thermal_zone_device *tz = to_thermal_zone(dev);
829 int trip, ret;
Sascha Hauer17e83512015-07-24 08:12:54 +0200830 int temperature;
Durgadoss R27365a62012-07-25 10:10:59 +0800831
832 if (!tz->ops->get_trip_hyst)
833 return -EPERM;
834
835 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
836 return -EINVAL;
837
838 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
839
Sascha Hauer17e83512015-07-24 08:12:54 +0200840 return ret ? ret : sprintf(buf, "%d\n", temperature);
Durgadoss R27365a62012-07-25 10:10:59 +0800841}
842
843static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000844passive_store(struct device *dev, struct device_attribute *attr,
845 const char *buf, size_t count)
846{
847 struct thermal_zone_device *tz = to_thermal_zone(dev);
848 struct thermal_cooling_device *cdev = NULL;
849 int state;
850
851 if (!sscanf(buf, "%d\n", &state))
852 return -EINVAL;
853
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100854 /* sanity check: values below 1000 millicelcius don't make sense
855 * and can cause the system to go into a thermal heart attack
856 */
857 if (state && state < 1000)
858 return -EINVAL;
859
Matthew Garrett03a971a2008-12-03 18:00:38 +0000860 if (state && !tz->forced_passive) {
861 mutex_lock(&thermal_list_lock);
862 list_for_each_entry(cdev, &thermal_cdev_list, node) {
863 if (!strncmp("Processor", cdev->type,
864 sizeof("Processor")))
865 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800866 THERMAL_TRIPS_NONE, cdev,
867 THERMAL_NO_LIMIT,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000868 THERMAL_NO_LIMIT,
869 THERMAL_WEIGHT_DEFAULT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000870 }
871 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100872 if (!tz->passive_delay)
873 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000874 } else if (!state && tz->forced_passive) {
875 mutex_lock(&thermal_list_lock);
876 list_for_each_entry(cdev, &thermal_cdev_list, node) {
877 if (!strncmp("Processor", cdev->type,
878 sizeof("Processor")))
879 thermal_zone_unbind_cooling_device(tz,
880 THERMAL_TRIPS_NONE,
881 cdev);
882 }
883 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100884 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000885 }
886
Matthew Garrett03a971a2008-12-03 18:00:38 +0000887 tz->forced_passive = state;
888
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700889 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000890
891 return count;
892}
893
894static ssize_t
895passive_show(struct device *dev, struct device_attribute *attr,
896 char *buf)
897{
898 struct thermal_zone_device *tz = to_thermal_zone(dev);
899
900 return sprintf(buf, "%d\n", tz->forced_passive);
901}
902
Durgadoss R5a2c0902012-09-18 11:04:58 +0530903static ssize_t
904policy_store(struct device *dev, struct device_attribute *attr,
905 const char *buf, size_t count)
906{
907 int ret = -EINVAL;
908 struct thermal_zone_device *tz = to_thermal_zone(dev);
909 struct thermal_governor *gov;
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000910 char name[THERMAL_NAME_LENGTH];
911
912 snprintf(name, sizeof(name), "%s", buf);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530913
914 mutex_lock(&thermal_governor_lock);
Javi Merinob6cc7722014-11-25 16:00:33 +0000915 mutex_lock(&tz->lock);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530916
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000917 gov = __find_governor(strim(name));
Durgadoss R5a2c0902012-09-18 11:04:58 +0530918 if (!gov)
919 goto exit;
920
Javi Merinoe33df1d2015-02-26 19:00:27 +0000921 ret = thermal_set_governor(tz, gov);
922 if (!ret)
923 ret = count;
Durgadoss R5a2c0902012-09-18 11:04:58 +0530924
925exit:
Javi Merinob6cc7722014-11-25 16:00:33 +0000926 mutex_unlock(&tz->lock);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530927 mutex_unlock(&thermal_governor_lock);
928 return ret;
929}
930
931static ssize_t
932policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
933{
934 struct thermal_zone_device *tz = to_thermal_zone(dev);
935
936 return sprintf(buf, "%s\n", tz->governor->name);
937}
938
Ni Wade25a0a5c2015-07-14 15:40:56 +0800939static ssize_t
940available_policies_show(struct device *dev, struct device_attribute *devattr,
941 char *buf)
942{
943 struct thermal_governor *pos;
944 ssize_t count = 0;
945 ssize_t size = PAGE_SIZE;
946
947 mutex_lock(&thermal_governor_lock);
948
949 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
950 size = PAGE_SIZE - count;
951 count += scnprintf(buf + count, size, "%s ", pos->name);
952 }
953 count += scnprintf(buf + count, size, "\n");
954
955 mutex_unlock(&thermal_governor_lock);
956
957 return count;
958}
959
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000960static ssize_t
961emul_temp_store(struct device *dev, struct device_attribute *attr,
962 const char *buf, size_t count)
963{
964 struct thermal_zone_device *tz = to_thermal_zone(dev);
965 int ret = 0;
Wei Ni1d0fd422016-03-03 17:33:46 +0800966 int temperature;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000967
Wei Ni1d0fd422016-03-03 17:33:46 +0800968 if (kstrtoint(buf, 10, &temperature))
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000969 return -EINVAL;
970
971 if (!tz->ops->set_emul_temp) {
972 mutex_lock(&tz->lock);
973 tz->emul_temperature = temperature;
974 mutex_unlock(&tz->lock);
975 } else {
976 ret = tz->ops->set_emul_temp(tz, temperature);
977 }
978
lan,Tianyu800744b2014-01-02 15:47:54 +0800979 if (!ret)
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700980 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
lan,Tianyu800744b2014-01-02 15:47:54 +0800981
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000982 return ret ? ret : count;
983}
984static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000985
Javi Merino9f382712015-03-26 15:53:02 +0000986static ssize_t
987sustainable_power_show(struct device *dev, struct device_attribute *devattr,
988 char *buf)
989{
990 struct thermal_zone_device *tz = to_thermal_zone(dev);
991
992 if (tz->tzp)
993 return sprintf(buf, "%u\n", tz->tzp->sustainable_power);
994 else
995 return -EIO;
996}
997
998static ssize_t
999sustainable_power_store(struct device *dev, struct device_attribute *devattr,
1000 const char *buf, size_t count)
1001{
1002 struct thermal_zone_device *tz = to_thermal_zone(dev);
1003 u32 sustainable_power;
1004
1005 if (!tz->tzp)
1006 return -EIO;
1007
1008 if (kstrtou32(buf, 10, &sustainable_power))
1009 return -EINVAL;
1010
1011 tz->tzp->sustainable_power = sustainable_power;
1012
1013 return count;
1014}
1015static DEVICE_ATTR(sustainable_power, S_IWUSR | S_IRUGO, sustainable_power_show,
1016 sustainable_power_store);
1017
1018#define create_s32_tzp_attr(name) \
1019 static ssize_t \
1020 name##_show(struct device *dev, struct device_attribute *devattr, \
1021 char *buf) \
1022 { \
1023 struct thermal_zone_device *tz = to_thermal_zone(dev); \
1024 \
1025 if (tz->tzp) \
Leo Yan15333e32016-03-29 19:24:15 +08001026 return sprintf(buf, "%d\n", tz->tzp->name); \
Javi Merino9f382712015-03-26 15:53:02 +00001027 else \
1028 return -EIO; \
1029 } \
1030 \
1031 static ssize_t \
1032 name##_store(struct device *dev, struct device_attribute *devattr, \
1033 const char *buf, size_t count) \
1034 { \
1035 struct thermal_zone_device *tz = to_thermal_zone(dev); \
1036 s32 value; \
1037 \
1038 if (!tz->tzp) \
1039 return -EIO; \
1040 \
1041 if (kstrtos32(buf, 10, &value)) \
1042 return -EINVAL; \
1043 \
1044 tz->tzp->name = value; \
1045 \
1046 return count; \
1047 } \
1048 static DEVICE_ATTR(name, S_IWUSR | S_IRUGO, name##_show, name##_store)
1049
1050create_s32_tzp_attr(k_po);
1051create_s32_tzp_attr(k_pu);
1052create_s32_tzp_attr(k_i);
1053create_s32_tzp_attr(k_d);
1054create_s32_tzp_attr(integral_cutoff);
Eduardo Valentin9d0be7f2015-05-11 19:34:23 -07001055create_s32_tzp_attr(slope);
1056create_s32_tzp_attr(offset);
Javi Merino9f382712015-03-26 15:53:02 +00001057#undef create_s32_tzp_attr
1058
1059static struct device_attribute *dev_tzp_attrs[] = {
1060 &dev_attr_sustainable_power,
1061 &dev_attr_k_po,
1062 &dev_attr_k_pu,
1063 &dev_attr_k_i,
1064 &dev_attr_k_d,
1065 &dev_attr_integral_cutoff,
Eduardo Valentin9d0be7f2015-05-11 19:34:23 -07001066 &dev_attr_slope,
1067 &dev_attr_offset,
Javi Merino9f382712015-03-26 15:53:02 +00001068};
1069
1070static int create_tzp_attrs(struct device *dev)
1071{
1072 int i;
1073
1074 for (i = 0; i < ARRAY_SIZE(dev_tzp_attrs); i++) {
1075 int ret;
1076 struct device_attribute *dev_attr = dev_tzp_attrs[i];
1077
1078 ret = device_create_file(dev, dev_attr);
1079 if (ret)
1080 return ret;
1081 }
1082
1083 return 0;
1084}
1085
Javi Merino35b11d22015-02-26 19:00:28 +00001086/**
1087 * power_actor_get_max_power() - get the maximum power that a cdev can consume
1088 * @cdev: pointer to &thermal_cooling_device
1089 * @tz: a valid thermal zone device pointer
1090 * @max_power: pointer in which to store the maximum power
1091 *
1092 * Calculate the maximum power consumption in milliwats that the
1093 * cooling device can currently consume and store it in @max_power.
1094 *
1095 * Return: 0 on success, -EINVAL if @cdev doesn't support the
1096 * power_actor API or -E* on other error.
1097 */
1098int power_actor_get_max_power(struct thermal_cooling_device *cdev,
1099 struct thermal_zone_device *tz, u32 *max_power)
1100{
1101 if (!cdev_is_power_actor(cdev))
1102 return -EINVAL;
1103
1104 return cdev->ops->state2power(cdev, tz, 0, max_power);
1105}
1106
1107/**
Javi Merinoc973c3b2015-09-14 14:23:50 +01001108 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
1109 * @cdev: pointer to &thermal_cooling_device
1110 * @tz: a valid thermal zone device pointer
1111 * @min_power: pointer in which to store the minimum power
1112 *
1113 * Calculate the minimum power consumption in milliwatts that the
1114 * cooling device can currently consume and store it in @min_power.
1115 *
1116 * Return: 0 on success, -EINVAL if @cdev doesn't support the
1117 * power_actor API or -E* on other error.
1118 */
1119int power_actor_get_min_power(struct thermal_cooling_device *cdev,
1120 struct thermal_zone_device *tz, u32 *min_power)
1121{
1122 unsigned long max_state;
1123 int ret;
1124
1125 if (!cdev_is_power_actor(cdev))
1126 return -EINVAL;
1127
1128 ret = cdev->ops->get_max_state(cdev, &max_state);
1129 if (ret)
1130 return ret;
1131
1132 return cdev->ops->state2power(cdev, tz, max_state, min_power);
1133}
1134
1135/**
Javi Merino35b11d22015-02-26 19:00:28 +00001136 * power_actor_set_power() - limit the maximum power that a cooling device can consume
1137 * @cdev: pointer to &thermal_cooling_device
1138 * @instance: thermal instance to update
1139 * @power: the power in milliwatts
1140 *
1141 * Set the cooling device to consume at most @power milliwatts.
1142 *
1143 * Return: 0 on success, -EINVAL if the cooling device does not
1144 * implement the power actor API or -E* for other failures.
1145 */
1146int power_actor_set_power(struct thermal_cooling_device *cdev,
1147 struct thermal_instance *instance, u32 power)
1148{
1149 unsigned long state;
1150 int ret;
1151
1152 if (!cdev_is_power_actor(cdev))
1153 return -EINVAL;
1154
1155 ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
1156 if (ret)
1157 return ret;
1158
1159 instance->target = state;
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01001160 mutex_lock(&cdev->lock);
Javi Merino35b11d22015-02-26 19:00:28 +00001161 cdev->updated = false;
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01001162 mutex_unlock(&cdev->lock);
Javi Merino35b11d22015-02-26 19:00:28 +00001163 thermal_cdev_update(cdev);
1164
1165 return 0;
1166}
1167
Zhang Rui203d3d42008-01-17 15:51:08 +08001168static DEVICE_ATTR(type, 0444, type_show, NULL);
1169static DEVICE_ATTR(temp, 0444, temp_show, NULL);
1170static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -07001171static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301172static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
Ni Wade25a0a5c2015-07-14 15:40:56 +08001173static DEVICE_ATTR(available_policies, S_IRUGO, available_policies_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001174
Zhang Rui203d3d42008-01-17 15:51:08 +08001175/* sys I/F for cooling device */
1176#define to_cooling_device(_dev) \
1177 container_of(_dev, struct thermal_cooling_device, device)
1178
1179static ssize_t
1180thermal_cooling_device_type_show(struct device *dev,
1181 struct device_attribute *attr, char *buf)
1182{
1183 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1184
1185 return sprintf(buf, "%s\n", cdev->type);
1186}
1187
1188static ssize_t
1189thermal_cooling_device_max_state_show(struct device *dev,
1190 struct device_attribute *attr, char *buf)
1191{
1192 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001193 unsigned long state;
1194 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +08001195
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001196 ret = cdev->ops->get_max_state(cdev, &state);
1197 if (ret)
1198 return ret;
1199 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +08001200}
1201
1202static ssize_t
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001203thermal_cooling_device_min_state_show(struct device *dev,
1204 struct device_attribute *attr, char *buf)
1205{
1206 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1207 unsigned long state;
1208 int ret;
1209
1210 if (cdev->ops->get_min_state)
1211 ret = cdev->ops->get_min_state(cdev, &state);
1212 else
1213 ret = -EPERM;
1214
1215 if (ret)
1216 return ret;
1217
1218 return snprintf(buf, PAGE_SIZE, "%lu\n", state);
1219}
1220
1221static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +08001222thermal_cooling_device_cur_state_show(struct device *dev,
1223 struct device_attribute *attr, char *buf)
1224{
1225 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001226 unsigned long state;
1227 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +08001228
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001229 ret = cdev->ops->get_cur_state(cdev, &state);
1230 if (ret)
1231 return ret;
1232 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +08001233}
1234
1235static ssize_t
1236thermal_cooling_device_cur_state_store(struct device *dev,
1237 struct device_attribute *attr,
1238 const char *buf, size_t count)
1239{
1240 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Ram Chandrasekar5ab1c082016-09-20 17:09:28 -06001241 long state;
Zhang Rui203d3d42008-01-17 15:51:08 +08001242
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001243 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +08001244 return -EINVAL;
1245
Roel Kluinedb94912009-12-15 22:46:50 +01001246 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +08001247 return -EINVAL;
1248
Ram Chandrasekar5ab1c082016-09-20 17:09:28 -06001249 cdev->sysfs_cur_state_req = state;
1250
1251 cdev->updated = false;
1252 thermal_cdev_update(cdev);
1253
Zhang Rui203d3d42008-01-17 15:51:08 +08001254 return count;
1255}
1256
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001257static ssize_t
1258thermal_cooling_device_min_state_store(struct device *dev,
1259 struct device_attribute *attr,
1260 const char *buf, size_t count)
1261{
1262 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1263 long state;
1264 int ret = 0;
1265
1266 ret = sscanf(buf, "%ld\n", &state);
1267 if (ret <= 0)
1268 return (ret < 0) ? ret : -EINVAL;
1269
1270 if ((long)state < 0)
1271 return -EINVAL;
1272
1273 cdev->sysfs_min_state_req = state;
1274
1275 cdev->updated = false;
1276 thermal_cdev_update(cdev);
1277
1278 return count;
1279}
1280
Zhang Rui203d3d42008-01-17 15:51:08 +08001281static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -05001282__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001283static DEVICE_ATTR(max_state, 0444,
1284 thermal_cooling_device_max_state_show, NULL);
1285static DEVICE_ATTR(cur_state, 0644,
1286 thermal_cooling_device_cur_state_show,
1287 thermal_cooling_device_cur_state_store);
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001288static DEVICE_ATTR(min_state, 0644,
1289 thermal_cooling_device_min_state_show,
1290 thermal_cooling_device_min_state_store);
Zhang Rui203d3d42008-01-17 15:51:08 +08001291
1292static ssize_t
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001293thermal_cooling_device_lower_limit_show(struct device *dev,
1294 struct device_attribute *attr, char *buf)
1295{
1296 struct thermal_instance *instance;
1297
1298 instance =
1299 container_of(attr, struct thermal_instance, lower_attr);
1300
1301 return snprintf(buf, PAGE_SIZE, "%lu\n", instance->lower);
1302}
1303
1304static ssize_t
1305thermal_cooling_device_upper_limit_show(struct device *dev,
1306 struct device_attribute *attr, char *buf)
1307{
1308 struct thermal_instance *instance;
1309
1310 instance =
1311 container_of(attr, struct thermal_instance, upper_attr);
1312
1313 return snprintf(buf, PAGE_SIZE, "%lu\n", instance->upper);
1314}
1315
1316static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +08001317thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -05001318 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +08001319{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001320 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +08001321
1322 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001323 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001324
1325 if (instance->trip == THERMAL_TRIPS_NONE)
1326 return sprintf(buf, "-1\n");
1327 else
1328 return sprintf(buf, "%d\n", instance->trip);
1329}
1330
Matthias Kaehlcke2dc10f82015-02-20 18:10:08 -08001331static struct attribute *cooling_device_attrs[] = {
1332 &dev_attr_cdev_type.attr,
1333 &dev_attr_max_state.attr,
1334 &dev_attr_cur_state.attr,
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001335 &dev_attr_min_state.attr,
Matthias Kaehlcke2dc10f82015-02-20 18:10:08 -08001336 NULL,
1337};
1338
1339static const struct attribute_group cooling_device_attr_group = {
1340 .attrs = cooling_device_attrs,
1341};
1342
1343static const struct attribute_group *cooling_device_attr_groups[] = {
1344 &cooling_device_attr_group,
1345 NULL,
1346};
1347
Javi Merinodb916512015-02-18 16:04:24 +00001348static ssize_t
1349thermal_cooling_device_weight_show(struct device *dev,
1350 struct device_attribute *attr, char *buf)
1351{
1352 struct thermal_instance *instance;
1353
1354 instance = container_of(attr, struct thermal_instance, weight_attr);
1355
1356 return sprintf(buf, "%d\n", instance->weight);
1357}
1358
1359static ssize_t
1360thermal_cooling_device_weight_store(struct device *dev,
1361 struct device_attribute *attr,
1362 const char *buf, size_t count)
1363{
1364 struct thermal_instance *instance;
1365 int ret, weight;
1366
1367 ret = kstrtoint(buf, 0, &weight);
1368 if (ret)
1369 return ret;
1370
1371 instance = container_of(attr, struct thermal_instance, weight_attr);
1372 instance->weight = weight;
1373
1374 return count;
1375}
Zhang Rui203d3d42008-01-17 15:51:08 +08001376/* Device management */
1377
1378/**
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001379 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
1380 * @tz: pointer to struct thermal_zone_device
Zhang Rui203d3d42008-01-17 15:51:08 +08001381 * @trip: indicates which trip point the cooling devices is
1382 * associated with in this thermal zone.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001383 * @cdev: pointer to struct thermal_cooling_device
1384 * @upper: the Maximum cooling state for this trip point.
1385 * THERMAL_NO_LIMIT means no upper limit,
1386 * and the cooling device can be in max_state.
1387 * @lower: the Minimum cooling state can be used for this trip point.
1388 * THERMAL_NO_LIMIT means no lower limit,
1389 * and the cooling device can be in cooling state 0.
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001390 * @weight: The weight of the cooling device to be bound to the
1391 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
1392 * default value
Len Brown543a9562008-02-07 16:55:08 -05001393 *
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001394 * This interface function bind a thermal cooling device to the certain trip
1395 * point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001396 * This function is usually called in the thermal zone device .bind callback.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001397 *
1398 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001399 */
1400int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1401 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +08001402 struct thermal_cooling_device *cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001403 unsigned long upper, unsigned long lower,
1404 unsigned int weight)
Zhang Rui203d3d42008-01-17 15:51:08 +08001405{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001406 struct thermal_instance *dev;
1407 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -05001408 struct thermal_zone_device *pos1;
1409 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +08001410 unsigned long max_state;
Lukasz Majewski9a3031d2014-11-18 11:16:30 +01001411 int result, ret;
Zhang Rui203d3d42008-01-17 15:51:08 +08001412
Len Brown543a9562008-02-07 16:55:08 -05001413 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +08001414 return -EINVAL;
1415
Thomas Sujithc7516702008-02-15 00:58:50 -05001416 list_for_each_entry(pos1, &thermal_tz_list, node) {
1417 if (pos1 == tz)
1418 break;
1419 }
1420 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1421 if (pos2 == cdev)
1422 break;
1423 }
1424
1425 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +08001426 return -EINVAL;
1427
Lukasz Majewski9a3031d2014-11-18 11:16:30 +01001428 ret = cdev->ops->get_max_state(cdev, &max_state);
1429 if (ret)
1430 return ret;
Zhang Rui9d998422012-06-26 16:35:57 +08001431
1432 /* lower default 0, upper default max_state */
1433 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1434 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1435
1436 if (lower > upper || upper > max_state)
1437 return -EINVAL;
1438
Zhang Rui203d3d42008-01-17 15:51:08 +08001439 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001440 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001441 if (!dev)
1442 return -ENOMEM;
1443 dev->tz = tz;
1444 dev->cdev = cdev;
1445 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +08001446 dev->upper = upper;
1447 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +08001448 dev->target = THERMAL_NO_TARGET;
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001449 dev->weight = weight;
Zhang Rui74051ba2012-06-26 16:27:22 +08001450
Zhang Rui203d3d42008-01-17 15:51:08 +08001451 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1452 if (result)
1453 goto free_mem;
1454
1455 sprintf(dev->name, "cdev%d", dev->id);
1456 result =
1457 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1458 if (result)
1459 goto release_idr;
1460
1461 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -07001462 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001463 dev->attr.attr.name = dev->attr_name;
1464 dev->attr.attr.mode = 0444;
1465 dev->attr.show = thermal_cooling_device_trip_point_show;
1466 result = device_create_file(&tz->device, &dev->attr);
1467 if (result)
1468 goto remove_symbol_link;
1469
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001470 snprintf(dev->upper_attr_name, THERMAL_NAME_LENGTH,
1471 "cdev%d_upper_limit", dev->id);
1472 sysfs_attr_init(&dev->upper_attr.attr);
1473 dev->upper_attr.attr.name = dev->upper_attr_name;
1474 dev->upper_attr.attr.mode = 0444;
1475 dev->upper_attr.show = thermal_cooling_device_upper_limit_show;
1476 result = device_create_file(&tz->device, &dev->upper_attr);
1477 if (result)
1478 goto remove_trip_file;
1479
1480 snprintf(dev->lower_attr_name, THERMAL_NAME_LENGTH,
1481 "cdev%d_lower_limit", dev->id);
1482 sysfs_attr_init(&dev->lower_attr.attr);
1483 dev->lower_attr.attr.name = dev->lower_attr_name;
1484 dev->lower_attr.attr.mode = 0444;
1485 dev->lower_attr.show = thermal_cooling_device_lower_limit_show;
1486 result = device_create_file(&tz->device, &dev->lower_attr);
1487 if (result)
1488 goto remove_upper_file;
1489
Javi Merinodb916512015-02-18 16:04:24 +00001490 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
1491 sysfs_attr_init(&dev->weight_attr.attr);
1492 dev->weight_attr.attr.name = dev->weight_attr_name;
1493 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
1494 dev->weight_attr.show = thermal_cooling_device_weight_show;
1495 dev->weight_attr.store = thermal_cooling_device_weight_store;
1496 result = device_create_file(&tz->device, &dev->weight_attr);
1497 if (result)
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001498 goto remove_lower_file;
Javi Merinodb916512015-02-18 16:04:24 +00001499
Zhang Rui203d3d42008-01-17 15:51:08 +08001500 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001501 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001502 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +08001503 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1504 result = -EEXIST;
1505 break;
1506 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001507 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001508 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001509 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
Chen Yu4511f712015-10-30 16:32:10 +08001510 atomic_set(&tz->need_update, 1);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001511 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001512 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001513 mutex_unlock(&tz->lock);
1514
1515 if (!result)
1516 return 0;
1517
Javi Merinodb916512015-02-18 16:04:24 +00001518 device_remove_file(&tz->device, &dev->weight_attr);
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001519remove_lower_file:
1520 device_remove_file(&tz->device, &dev->lower_attr);
1521remove_upper_file:
1522 device_remove_file(&tz->device, &dev->upper_attr);
Javi Merinodb916512015-02-18 16:04:24 +00001523remove_trip_file:
Zhang Rui203d3d42008-01-17 15:51:08 +08001524 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -07001525remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +08001526 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -07001527release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +08001528 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -07001529free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +08001530 kfree(dev);
1531 return result;
1532}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001533EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001534
1535/**
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001536 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1537 * thermal zone.
1538 * @tz: pointer to a struct thermal_zone_device.
Zhang Rui203d3d42008-01-17 15:51:08 +08001539 * @trip: indicates which trip point the cooling devices is
1540 * associated with in this thermal zone.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001541 * @cdev: pointer to a struct thermal_cooling_device.
Len Brown543a9562008-02-07 16:55:08 -05001542 *
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001543 * This interface function unbind a thermal cooling device from the certain
1544 * trip point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001545 * This function is usually called in the thermal zone device .unbind callback.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001546 *
1547 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001548 */
1549int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1550 int trip,
1551 struct thermal_cooling_device *cdev)
1552{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001553 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +08001554
1555 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001556 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001557 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -05001558 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001559 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001560 list_del(&pos->cdev_node);
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 goto unbind;
1564 }
1565 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001566 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001567 mutex_unlock(&tz->lock);
1568
1569 return -ENODEV;
1570
Joe Perchescaca8b82012-03-21 12:55:02 -07001571unbind:
Ram Chandrasekara66af1452017-03-28 15:16:39 -06001572 device_remove_file(&tz->device, &pos->lower_attr);
1573 device_remove_file(&tz->device, &pos->upper_attr);
Viresh Kumar528464e2015-07-23 14:32:32 +05301574 device_remove_file(&tz->device, &pos->weight_attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001575 device_remove_file(&tz->device, &pos->attr);
1576 sysfs_remove_link(&tz->device.kobj, pos->name);
1577 release_idr(&tz->idr, &tz->lock, pos->id);
1578 kfree(pos);
1579 return 0;
1580}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001581EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001582
1583static void thermal_release(struct device *dev)
1584{
1585 struct thermal_zone_device *tz;
1586 struct thermal_cooling_device *cdev;
1587
Joe Perchescaca8b82012-03-21 12:55:02 -07001588 if (!strncmp(dev_name(dev), "thermal_zone",
1589 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001590 tz = to_thermal_zone(dev);
1591 kfree(tz);
durgadoss.r@intel.com732e4c82013-10-02 00:08:00 +05301592 } else if(!strncmp(dev_name(dev), "cooling_device",
1593 sizeof("cooling_device") - 1)){
Zhang Rui203d3d42008-01-17 15:51:08 +08001594 cdev = to_cooling_device(dev);
1595 kfree(cdev);
1596 }
1597}
1598
1599static struct class thermal_class = {
1600 .name = "thermal",
1601 .dev_release = thermal_release,
1602};
1603
1604/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001605 * __thermal_cooling_device_register() - register a new thermal cooling device
1606 * @np: a pointer to a device tree node.
Zhang Rui203d3d42008-01-17 15:51:08 +08001607 * @type: the thermal cooling device type.
1608 * @devdata: device private data.
1609 * @ops: standard thermal cooling devices callbacks.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001610 *
1611 * This interface function adds a new thermal cooling device (fan/processor/...)
1612 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1613 * to all the thermal zone devices registered at the same time.
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001614 * It also gives the opportunity to link the cooling device to a device tree
1615 * node, so that it can be bound to a thermal zone created out of device tree.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001616 *
1617 * Return: a pointer to the created struct thermal_cooling_device or an
1618 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001619 */
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001620static struct thermal_cooling_device *
1621__thermal_cooling_device_register(struct device_node *np,
1622 char *type, void *devdata,
1623 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001624{
1625 struct thermal_cooling_device *cdev;
Chen Yu4511f712015-10-30 16:32:10 +08001626 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001627 int result;
1628
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001629 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001630 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001631
1632 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001633 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001634 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001635
1636 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1637 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001638 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001639
1640 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1641 if (result) {
1642 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001643 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001644 }
1645
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001646 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +08001647 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001648 INIT_LIST_HEAD(&cdev->thermal_instances);
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001649 cdev->np = np;
Zhang Rui203d3d42008-01-17 15:51:08 +08001650 cdev->ops = ops;
Ni Wade5ca0cce2014-02-17 11:02:55 +08001651 cdev->updated = false;
Zhang Rui203d3d42008-01-17 15:51:08 +08001652 cdev->device.class = &thermal_class;
Matthias Kaehlcke2dc10f82015-02-20 18:10:08 -08001653 cdev->device.groups = cooling_device_attr_groups;
Zhang Rui203d3d42008-01-17 15:51:08 +08001654 cdev->devdata = devdata;
Ram Chandrasekar5ab1c082016-09-20 17:09:28 -06001655 cdev->sysfs_cur_state_req = 0;
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001656 cdev->sysfs_min_state_req = ULONG_MAX;
Kay Sievers354655e2009-01-06 10:44:37 -08001657 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001658 result = device_register(&cdev->device);
1659 if (result) {
1660 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1661 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001662 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001663 }
1664
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301665 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +08001666 mutex_lock(&thermal_list_lock);
1667 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001668 mutex_unlock(&thermal_list_lock);
1669
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301670 /* Update binding information for 'this' new cdev */
1671 bind_cdev(cdev);
1672
Chen Yu4511f712015-10-30 16:32:10 +08001673 mutex_lock(&thermal_list_lock);
1674 list_for_each_entry(pos, &thermal_tz_list, node)
1675 if (atomic_cmpxchg(&pos->need_update, 1, 0))
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07001676 thermal_zone_device_update(pos,
1677 THERMAL_EVENT_UNSPECIFIED);
Chen Yu4511f712015-10-30 16:32:10 +08001678 mutex_unlock(&thermal_list_lock);
1679
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301680 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001681}
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001682
1683/**
1684 * thermal_cooling_device_register() - register a new thermal cooling device
1685 * @type: the thermal cooling device type.
1686 * @devdata: device private data.
1687 * @ops: standard thermal cooling devices callbacks.
1688 *
1689 * This interface function adds a new thermal cooling device (fan/processor/...)
1690 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1691 * to all the thermal zone devices registered at the same time.
1692 *
1693 * Return: a pointer to the created struct thermal_cooling_device or an
1694 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1695 */
1696struct thermal_cooling_device *
1697thermal_cooling_device_register(char *type, void *devdata,
1698 const struct thermal_cooling_device_ops *ops)
1699{
1700 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1701}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001702EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001703
1704/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001705 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1706 * @np: a pointer to a device tree node.
1707 * @type: the thermal cooling device type.
1708 * @devdata: device private data.
1709 * @ops: standard thermal cooling devices callbacks.
1710 *
1711 * This function will register a cooling device with device tree node reference.
1712 * This interface function adds a new thermal cooling device (fan/processor/...)
1713 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1714 * to all the thermal zone devices registered at the same time.
1715 *
1716 * Return: a pointer to the created struct thermal_cooling_device or an
1717 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1718 */
1719struct thermal_cooling_device *
1720thermal_of_cooling_device_register(struct device_node *np,
1721 char *type, void *devdata,
1722 const struct thermal_cooling_device_ops *ops)
1723{
1724 return __thermal_cooling_device_register(np, type, devdata, ops);
1725}
1726EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1727
1728/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001729 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001730 * @cdev: the thermal cooling device to remove.
1731 *
1732 * thermal_cooling_device_unregister() must be called when the device is no
1733 * longer needed.
1734 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301735void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001736{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301737 int i;
1738 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001739 struct thermal_zone_device *tz;
1740 struct thermal_cooling_device *pos = NULL;
1741
1742 if (!cdev)
1743 return;
1744
1745 mutex_lock(&thermal_list_lock);
1746 list_for_each_entry(pos, &thermal_cdev_list, node)
1747 if (pos == cdev)
1748 break;
1749 if (pos != cdev) {
1750 /* thermal cooling device not found */
1751 mutex_unlock(&thermal_list_lock);
1752 return;
1753 }
1754 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301755
1756 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001757 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301758 if (tz->ops->unbind) {
1759 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001760 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301761 }
1762
1763 if (!tz->tzp || !tz->tzp->tbp)
1764 continue;
1765
1766 tzp = tz->tzp;
1767 for (i = 0; i < tzp->num_tbps; i++) {
1768 if (tzp->tbp[i].cdev == cdev) {
1769 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1770 tzp->tbp[i].cdev = NULL;
1771 }
1772 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001773 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301774
Zhang Rui203d3d42008-01-17 15:51:08 +08001775 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301776
Zhang Rui203d3d42008-01-17 15:51:08 +08001777 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001778 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001779 device_remove_file(&cdev->device, &dev_attr_max_state);
1780 device_remove_file(&cdev->device, &dev_attr_cur_state);
1781
1782 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1783 device_unregister(&cdev->device);
1784 return;
1785}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001786EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001787
Durgadoss Rdc765482012-09-18 11:05:00 +05301788void thermal_cdev_update(struct thermal_cooling_device *cdev)
Zhang Ruice119f82012-06-27 14:13:04 +08001789{
1790 struct thermal_instance *instance;
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001791 unsigned long current_target = 0, min_target = ULONG_MAX;
Zhang Ruice119f82012-06-27 14:13:04 +08001792
Zhang Ruif4a821c2012-07-24 16:56:21 +08001793 mutex_lock(&cdev->lock);
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01001794 /* cooling device is updated*/
1795 if (cdev->updated) {
1796 mutex_unlock(&cdev->lock);
1797 return;
1798 }
1799
Zhang Ruice119f82012-06-27 14:13:04 +08001800 /* Make sure cdev enters the deepest cooling state */
Ram Chandrasekar5ab1c082016-09-20 17:09:28 -06001801 current_target = cdev->sysfs_cur_state_req;
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001802 min_target = cdev->sysfs_min_state_req;
Zhang Ruice119f82012-06-27 14:13:04 +08001803 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
Aaron Lu06475b52013-12-02 13:54:26 +08001804 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1805 instance->tz->id, instance->target);
Zhang Ruice119f82012-06-27 14:13:04 +08001806 if (instance->target == THERMAL_NO_TARGET)
1807 continue;
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001808 if (instance->tz->governor->min_state_throttle) {
1809 if (instance->target < min_target)
1810 min_target = instance->target;
1811 } else {
1812 if (instance->target > current_target)
1813 current_target = instance->target;
1814 }
Zhang Ruice119f82012-06-27 14:13:04 +08001815 }
Ram Chandrasekar5ab1c082016-09-20 17:09:28 -06001816 cdev->ops->set_cur_state(cdev, current_target);
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001817 if (cdev->ops->set_min_state)
1818 cdev->ops->set_min_state(cdev, min_target);
Zhang Ruice119f82012-06-27 14:13:04 +08001819 cdev->updated = true;
Michele Di Giorgiod0b73062016-06-02 15:25:31 +01001820 mutex_unlock(&cdev->lock);
Ram Chandrasekar5b1c0d12017-03-28 11:35:40 -06001821 trace_cdev_update(cdev, current_target, min_target);
Ram Chandrasekarbd155702017-03-17 16:42:15 -06001822 dev_dbg(&cdev->device, "set to state %lu min state %lu\n",
1823 current_target, min_target);
Zhang Ruice119f82012-06-27 14:13:04 +08001824}
Durgadoss Rdc765482012-09-18 11:05:00 +05301825EXPORT_SYMBOL(thermal_cdev_update);
Zhang Ruice119f82012-06-27 14:13:04 +08001826
Matthew Garrettb1569e92008-12-03 17:55:32 +00001827/**
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001828 * thermal_notify_framework - Sensor drivers use this API to notify framework
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301829 * @tz: thermal zone device
1830 * @trip: indicates which trip point has been crossed
1831 *
1832 * This function handles the trip events from sensor drivers. It starts
1833 * throttling the cooling devices according to the policy configured.
1834 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1835 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1836 * The throttling policy is based on the configured platform data; if no
1837 * platform data is provided, this uses the step_wise throttling policy.
1838 */
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001839void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301840{
1841 handle_thermal_trip(tz, trip);
1842}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001843EXPORT_SYMBOL_GPL(thermal_notify_framework);
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301844
1845/**
Eduardo Valentin269c1742013-04-23 21:48:19 +00001846 * create_trip_attrs() - create attributes for trip points
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001847 * @tz: the thermal zone device
1848 * @mask: Writeable trip point bitmap.
Eduardo Valentin269c1742013-04-23 21:48:19 +00001849 *
1850 * helper function to instantiate sysfs entries for every trip
1851 * point and its properties of a struct thermal_zone_device.
1852 *
1853 * Return: 0 on success, the proper error value otherwise.
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001854 */
1855static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1856{
1857 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001858 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001859
Durgadoss R27365a62012-07-25 10:10:59 +08001860 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001861 if (!tz->trip_type_attrs)
1862 return -ENOMEM;
1863
Durgadoss R27365a62012-07-25 10:10:59 +08001864 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001865 if (!tz->trip_temp_attrs) {
1866 kfree(tz->trip_type_attrs);
1867 return -ENOMEM;
1868 }
1869
Durgadoss R27365a62012-07-25 10:10:59 +08001870 if (tz->ops->get_trip_hyst) {
1871 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1872 if (!tz->trip_hyst_attrs) {
1873 kfree(tz->trip_type_attrs);
1874 kfree(tz->trip_temp_attrs);
1875 return -ENOMEM;
1876 }
1877 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001878
Durgadoss R27365a62012-07-25 10:10:59 +08001879
1880 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001881 /* create trip type attribute */
1882 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1883 "trip_point_%d_type", indx);
1884
1885 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1886 tz->trip_type_attrs[indx].attr.attr.name =
1887 tz->trip_type_attrs[indx].name;
1888 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1889 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1890
1891 device_create_file(&tz->device,
1892 &tz->trip_type_attrs[indx].attr);
1893
1894 /* create trip temp attribute */
1895 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1896 "trip_point_%d_temp", indx);
1897
1898 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1899 tz->trip_temp_attrs[indx].attr.attr.name =
1900 tz->trip_temp_attrs[indx].name;
1901 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1902 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
Punit Agrawal35e94642015-03-03 10:43:03 +00001903 if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS) &&
1904 mask & (1 << indx)) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001905 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1906 tz->trip_temp_attrs[indx].attr.store =
1907 trip_point_temp_store;
1908 }
1909
1910 device_create_file(&tz->device,
1911 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001912
1913 /* create Optional trip hyst attribute */
1914 if (!tz->ops->get_trip_hyst)
1915 continue;
1916 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1917 "trip_point_%d_hyst", indx);
1918
1919 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1920 tz->trip_hyst_attrs[indx].attr.attr.name =
1921 tz->trip_hyst_attrs[indx].name;
1922 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1923 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1924 if (tz->ops->set_trip_hyst) {
1925 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1926 tz->trip_hyst_attrs[indx].attr.store =
1927 trip_point_hyst_store;
1928 }
1929
1930 device_create_file(&tz->device,
1931 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001932 }
1933 return 0;
1934}
1935
1936static void remove_trip_attrs(struct thermal_zone_device *tz)
1937{
1938 int indx;
1939
1940 for (indx = 0; indx < tz->trips; indx++) {
1941 device_remove_file(&tz->device,
1942 &tz->trip_type_attrs[indx].attr);
1943 device_remove_file(&tz->device,
1944 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001945 if (tz->ops->get_trip_hyst)
1946 device_remove_file(&tz->device,
1947 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001948 }
1949 kfree(tz->trip_type_attrs);
1950 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001951 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001952}
1953
1954/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001955 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001956 * @type: the thermal zone device type
1957 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001958 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001959 * @devdata: private device data
1960 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301961 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001962 * @passive_delay: number of milliseconds to wait between polls when
1963 * performing passive cooling
1964 * @polling_delay: number of milliseconds to wait between polls when checking
1965 * whether trip points have been crossed (0 for interrupt
1966 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001967 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001968 * This interface function adds a new thermal zone device (sensor) to
1969 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1970 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08001971 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001972 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001973 *
1974 * Return: a pointer to the created struct thermal_zone_device or an
1975 * in case of error, an ERR_PTR. Caller must check return value with
1976 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001977 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001978struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001979 int trips, int mask, void *devdata,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001980 struct thermal_zone_device_ops *ops,
Javi Merino6b775e82015-03-02 17:17:19 +00001981 struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001982 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001983{
1984 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001985 enum thermal_trip_type trip_type;
Zhang Rui81ad4272016-03-18 10:03:24 +08001986 int trip_temp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001987 int result;
1988 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001989 int passive = 0;
Javi Merinoe33df1d2015-02-26 19:00:27 +00001990 struct thermal_governor *governor;
Zhang Rui203d3d42008-01-17 15:51:08 +08001991
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001992 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001993 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001994
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001995 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001996 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001997
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04001998 if (!ops)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001999 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08002000
Jonghwa Lee83720d02013-05-18 09:50:26 +00002001 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00002002 return ERR_PTR(-EINVAL);
2003
Zhang Rui203d3d42008-01-17 15:51:08 +08002004 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
2005 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002006 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08002007
Zhang Rui2d374132012-06-27 10:09:00 +08002008 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08002009 idr_init(&tz->idr);
2010 mutex_init(&tz->lock);
2011 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
2012 if (result) {
2013 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002014 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08002015 }
2016
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00002017 strlcpy(tz->type, type ? : "", sizeof(tz->type));
Zhang Rui203d3d42008-01-17 15:51:08 +08002018 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05302019 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08002020 tz->device.class = &thermal_class;
2021 tz->devdata = devdata;
2022 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00002023 tz->passive_delay = passive_delay;
2024 tz->polling_delay = polling_delay;
Chen Yu4511f712015-10-30 16:32:10 +08002025 /* A new thermal zone needs to be updated anyway. */
2026 atomic_set(&tz->need_update, 1);
Matthew Garrettb1569e92008-12-03 17:55:32 +00002027
Kay Sievers354655e2009-01-06 10:44:37 -08002028 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08002029 result = device_register(&tz->device);
2030 if (result) {
2031 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
2032 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002033 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08002034 }
2035
2036 /* sys I/F */
2037 if (type) {
2038 result = device_create_file(&tz->device, &dev_attr_type);
2039 if (result)
2040 goto unregister;
2041 }
2042
2043 result = device_create_file(&tz->device, &dev_attr_temp);
2044 if (result)
2045 goto unregister;
2046
2047 if (ops->get_mode) {
2048 result = device_create_file(&tz->device, &dev_attr_mode);
2049 if (result)
2050 goto unregister;
2051 }
2052
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002053 result = create_trip_attrs(tz, mask);
2054 if (result)
2055 goto unregister;
2056
Zhang Rui203d3d42008-01-17 15:51:08 +08002057 for (count = 0; count < trips; count++) {
Zhang Rui81ad4272016-03-18 10:03:24 +08002058 if (tz->ops->get_trip_type(tz, count, &trip_type))
2059 set_bit(count, &tz->trips_disabled);
Matthew Garrett03a971a2008-12-03 18:00:38 +00002060 if (trip_type == THERMAL_TRIP_PASSIVE)
2061 passive = 1;
Zhang Rui81ad4272016-03-18 10:03:24 +08002062 if (tz->ops->get_trip_temp(tz, count, &trip_temp))
2063 set_bit(count, &tz->trips_disabled);
2064 /* Check for bogus trip points */
2065 if (trip_temp == 0)
2066 set_bit(count, &tz->trips_disabled);
Zhang Rui203d3d42008-01-17 15:51:08 +08002067 }
2068
Durgadoss R5a2c0902012-09-18 11:04:58 +05302069 if (!passive) {
2070 result = device_create_file(&tz->device, &dev_attr_passive);
2071 if (result)
2072 goto unregister;
2073 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00002074
Sascha Hauer79e54212015-07-06 09:46:16 +02002075 if (IS_ENABLED(CONFIG_THERMAL_EMULATION)) {
2076 result = device_create_file(&tz->device, &dev_attr_emul_temp);
2077 if (result)
2078 goto unregister;
2079 }
2080
Durgadoss R5a2c0902012-09-18 11:04:58 +05302081 /* Create policy attribute */
2082 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00002083 if (result)
2084 goto unregister;
2085
Javi Merino9f382712015-03-26 15:53:02 +00002086 /* Add thermal zone params */
2087 result = create_tzp_attrs(&tz->device);
2088 if (result)
2089 goto unregister;
2090
Ni Wade25a0a5c2015-07-14 15:40:56 +08002091 /* Create available_policies attribute */
2092 result = device_create_file(&tz->device, &dev_attr_available_policies);
2093 if (result)
2094 goto unregister;
2095
Durgadoss Ra4a15482012-09-18 11:04:57 +05302096 /* Update 'this' zone's governor information */
2097 mutex_lock(&thermal_governor_lock);
2098
2099 if (tz->tzp)
Javi Merinoe33df1d2015-02-26 19:00:27 +00002100 governor = __find_governor(tz->tzp->governor_name);
Durgadoss Ra4a15482012-09-18 11:04:57 +05302101 else
Javi Merinoe33df1d2015-02-26 19:00:27 +00002102 governor = def_governor;
2103
2104 result = thermal_set_governor(tz, governor);
2105 if (result) {
2106 mutex_unlock(&thermal_governor_lock);
2107 goto unregister;
2108 }
Durgadoss Ra4a15482012-09-18 11:04:57 +05302109
2110 mutex_unlock(&thermal_governor_lock);
2111
Eduardo Valentinccba4ff2013-08-15 11:34:17 -04002112 if (!tz->tzp || !tz->tzp->no_hwmon) {
2113 result = thermal_add_hwmon_sysfs(tz);
2114 if (result)
2115 goto unregister;
2116 }
Zhang Ruie68b16a2008-04-21 16:07:52 +08002117
Zhang Rui203d3d42008-01-17 15:51:08 +08002118 mutex_lock(&thermal_list_lock);
2119 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08002120 mutex_unlock(&thermal_list_lock);
2121
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05302122 /* Bind cooling devices for this zone */
2123 bind_tz(tz);
2124
Matthew Garrettb1569e92008-12-03 17:55:32 +00002125 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
2126
Zhang Ruibb431ba2015-10-30 16:31:47 +08002127 thermal_zone_device_reset(tz);
Chen Yu4511f712015-10-30 16:32:10 +08002128 /* Update the new thermal zone and mark it as already updated. */
2129 if (atomic_cmpxchg(&tz->need_update, 1, 0))
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07002130 thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
Matthew Garrettb1569e92008-12-03 17:55:32 +00002131
Yao Dongdong14015862014-10-28 07:40:25 +00002132 return tz;
Zhang Rui203d3d42008-01-17 15:51:08 +08002133
Joe Perchescaca8b82012-03-21 12:55:02 -07002134unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08002135 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
2136 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05002137 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08002138}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00002139EXPORT_SYMBOL_GPL(thermal_zone_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08002140
2141/**
2142 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08002143 * @tz: the thermal zone device to remove
2144 */
2145void thermal_zone_device_unregister(struct thermal_zone_device *tz)
2146{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05302147 int i;
2148 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08002149 struct thermal_cooling_device *cdev;
2150 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08002151
2152 if (!tz)
2153 return;
2154
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05302155 tzp = tz->tzp;
2156
Zhang Rui203d3d42008-01-17 15:51:08 +08002157 mutex_lock(&thermal_list_lock);
2158 list_for_each_entry(pos, &thermal_tz_list, node)
2159 if (pos == tz)
2160 break;
2161 if (pos != tz) {
2162 /* thermal zone device not found */
2163 mutex_unlock(&thermal_list_lock);
2164 return;
2165 }
2166 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05302167
2168 /* Unbind all cdevs associated with 'this' thermal zone */
2169 list_for_each_entry(cdev, &thermal_cdev_list, node) {
2170 if (tz->ops->unbind) {
2171 tz->ops->unbind(tz, cdev);
2172 continue;
2173 }
2174
2175 if (!tzp || !tzp->tbp)
2176 break;
2177
2178 for (i = 0; i < tzp->num_tbps; i++) {
2179 if (tzp->tbp[i].cdev == cdev) {
2180 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
2181 tzp->tbp[i].cdev = NULL;
2182 }
2183 }
2184 }
2185
Zhang Rui203d3d42008-01-17 15:51:08 +08002186 mutex_unlock(&thermal_list_lock);
2187
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -06002188 thermal_zone_device_set_polling(NULL, tz, 0);
Matthew Garrettb1569e92008-12-03 17:55:32 +00002189
Zhang Rui203d3d42008-01-17 15:51:08 +08002190 if (tz->type[0])
2191 device_remove_file(&tz->device, &dev_attr_type);
2192 device_remove_file(&tz->device, &dev_attr_temp);
2193 if (tz->ops->get_mode)
2194 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05302195 device_remove_file(&tz->device, &dev_attr_policy);
Ni Wade25a0a5c2015-07-14 15:40:56 +08002196 device_remove_file(&tz->device, &dev_attr_available_policies);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08002197 remove_trip_attrs(tz);
Javi Merinoe33df1d2015-02-26 19:00:27 +00002198 thermal_set_governor(tz, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08002199
Zhang Ruie68b16a2008-04-21 16:07:52 +08002200 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08002201 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
2202 idr_destroy(&tz->idr);
2203 mutex_destroy(&tz->lock);
2204 device_unregister(&tz->device);
2205 return;
2206}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00002207EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08002208
Eduardo Valentin63c4d912013-04-05 12:32:28 +00002209/**
2210 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
2211 * @name: thermal zone name to fetch the temperature
2212 *
2213 * When only one zone is found with the passed name, returns a reference to it.
2214 *
2215 * Return: On success returns a reference to an unique thermal zone with
2216 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
2217 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
2218 */
2219struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
2220{
2221 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
2222 unsigned int found = 0;
2223
2224 if (!name)
2225 goto exit;
2226
2227 mutex_lock(&thermal_list_lock);
2228 list_for_each_entry(pos, &thermal_tz_list, node)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -07002229 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
Eduardo Valentin63c4d912013-04-05 12:32:28 +00002230 found++;
2231 ref = pos;
2232 }
2233 mutex_unlock(&thermal_list_lock);
2234
2235 /* nothing has been found, thus an error code for it */
2236 if (found == 0)
2237 ref = ERR_PTR(-ENODEV);
2238 else if (found > 1)
2239 /* Success only when an unique zone is found */
2240 ref = ERR_PTR(-EEXIST);
2241
2242exit:
2243 return ref;
2244}
2245EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
2246
Rajendra Nayak4a7069a2016-05-05 14:21:42 +05302247/**
2248 * thermal_zone_get_slope - return the slope attribute of the thermal zone
2249 * @tz: thermal zone device with the slope attribute
2250 *
2251 * Return: If the thermal zone device has a slope attribute, return it, else
2252 * return 1.
2253 */
2254int thermal_zone_get_slope(struct thermal_zone_device *tz)
2255{
2256 if (tz && tz->tzp)
2257 return tz->tzp->slope;
2258 return 1;
2259}
2260EXPORT_SYMBOL_GPL(thermal_zone_get_slope);
2261
2262/**
2263 * thermal_zone_get_offset - return the offset attribute of the thermal zone
2264 * @tz: thermal zone device with the offset attribute
2265 *
2266 * Return: If the thermal zone device has a offset attribute, return it, else
2267 * return 0.
2268 */
2269int thermal_zone_get_offset(struct thermal_zone_device *tz)
2270{
2271 if (tz && tz->tzp)
2272 return tz->tzp->offset;
2273 return 0;
2274}
2275EXPORT_SYMBOL_GPL(thermal_zone_get_offset);
2276
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002277#ifdef CONFIG_NET
Johannes Berg2a94fe42013-11-19 15:19:39 +01002278static const struct genl_multicast_group thermal_event_mcgrps[] = {
2279 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
2280};
2281
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002282static struct genl_family thermal_event_genl_family = {
2283 .id = GENL_ID_GENERATE,
2284 .name = THERMAL_GENL_FAMILY_NAME,
2285 .version = THERMAL_GENL_VERSION,
2286 .maxattr = THERMAL_GENL_ATTR_MAX,
Johannes Berg2a94fe42013-11-19 15:19:39 +01002287 .mcgrps = thermal_event_mcgrps,
2288 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002289};
2290
Lina Iyer9bf792d2016-07-11 13:27:11 -06002291static int allow_netlink_events;
2292
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00002293int thermal_generate_netlink_event(struct thermal_zone_device *tz,
2294 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05302295{
2296 struct sk_buff *skb;
2297 struct nlattr *attr;
2298 struct thermal_genl_event *thermal_event;
2299 void *msg_header;
2300 int size;
2301 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07002302 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05302303
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00002304 if (!tz)
2305 return -EINVAL;
2306
Lina Iyer9bf792d2016-07-11 13:27:11 -06002307 if (!allow_netlink_events)
2308 return -ENODEV;
2309
R.Durgadoss4cb18722010-10-27 03:33:29 +05302310 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07002311 size = nla_total_size(sizeof(struct thermal_genl_event)) +
2312 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302313
2314 skb = genlmsg_new(size, GFP_ATOMIC);
2315 if (!skb)
2316 return -ENOMEM;
2317
2318 /* add the genetlink message header */
2319 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
2320 &thermal_event_genl_family, 0,
2321 THERMAL_GENL_CMD_EVENT);
2322 if (!msg_header) {
2323 nlmsg_free(skb);
2324 return -ENOMEM;
2325 }
2326
2327 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07002328 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
2329 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05302330
2331 if (!attr) {
2332 nlmsg_free(skb);
2333 return -EINVAL;
2334 }
2335
2336 thermal_event = nla_data(attr);
2337 if (!thermal_event) {
2338 nlmsg_free(skb);
2339 return -EINVAL;
2340 }
2341
2342 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
2343
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00002344 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05302345 thermal_event->event = event;
2346
2347 /* send multicast genetlink message */
Johannes Berg053c0952015-01-16 22:09:00 +01002348 genlmsg_end(skb, msg_header);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302349
Johannes Berg68eb5502013-11-19 15:19:38 +01002350 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01002351 0, GFP_ATOMIC);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302352 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00002353 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302354
2355 return result;
2356}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00002357EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302358
2359static int genetlink_init(void)
2360{
Lina Iyer9bf792d2016-07-11 13:27:11 -06002361 int ret;
2362
2363 ret = genl_register_family(&thermal_event_genl_family);
2364 if (!ret)
2365 allow_netlink_events = true;
2366
2367 return ret;
R.Durgadoss4cb18722010-10-27 03:33:29 +05302368}
2369
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002370static void genetlink_exit(void)
2371{
2372 genl_unregister_family(&thermal_event_genl_family);
2373}
2374#else /* !CONFIG_NET */
2375static inline int genetlink_init(void) { return 0; }
2376static inline void genetlink_exit(void) {}
Lina Iyer9bf792d2016-07-11 13:27:11 -06002377static inline int thermal_generate_netlink_event(struct thermal_zone_device *tz,
2378 enum events event) { return -ENODEV; }
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002379#endif /* !CONFIG_NET */
2380
Zhang Rui80a26a52013-03-26 16:38:29 +08002381static int __init thermal_register_governors(void)
2382{
2383 int result;
2384
2385 result = thermal_gov_step_wise_register();
2386 if (result)
2387 return result;
2388
2389 result = thermal_gov_fair_share_register();
2390 if (result)
2391 return result;
2392
Peter Feuerere4dbf982014-07-22 17:37:13 +02002393 result = thermal_gov_bang_bang_register();
2394 if (result)
2395 return result;
2396
Javi Merino6b775e82015-03-02 17:17:19 +00002397 result = thermal_gov_user_space_register();
2398 if (result)
2399 return result;
2400
Ram Chandrasekar6d44a342016-07-07 11:09:52 -06002401 result = thermal_gov_low_limits_register();
2402 if (result)
2403 return result;
2404
Javi Merino6b775e82015-03-02 17:17:19 +00002405 return thermal_gov_power_allocator_register();
Zhang Rui80a26a52013-03-26 16:38:29 +08002406}
2407
2408static void thermal_unregister_governors(void)
2409{
2410 thermal_gov_step_wise_unregister();
2411 thermal_gov_fair_share_unregister();
Peter Feuerere4dbf982014-07-22 17:37:13 +02002412 thermal_gov_bang_bang_unregister();
Zhang Rui80a26a52013-03-26 16:38:29 +08002413 thermal_gov_user_space_unregister();
Ram Chandrasekar6d44a342016-07-07 11:09:52 -06002414 thermal_gov_low_limits_unregister();
Javi Merino6b775e82015-03-02 17:17:19 +00002415 thermal_gov_power_allocator_unregister();
Zhang Rui80a26a52013-03-26 16:38:29 +08002416}
2417
Zhang Ruiff140fe2015-10-30 16:31:58 +08002418static int thermal_pm_notify(struct notifier_block *nb,
2419 unsigned long mode, void *_unused)
2420{
2421 struct thermal_zone_device *tz;
2422
2423 switch (mode) {
2424 case PM_HIBERNATION_PREPARE:
2425 case PM_RESTORE_PREPARE:
2426 case PM_SUSPEND_PREPARE:
2427 atomic_set(&in_suspend, 1);
2428 break;
2429 case PM_POST_HIBERNATION:
2430 case PM_POST_RESTORE:
2431 case PM_POST_SUSPEND:
2432 atomic_set(&in_suspend, 0);
2433 list_for_each_entry(tz, &thermal_tz_list, node) {
2434 thermal_zone_device_reset(tz);
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -07002435 thermal_zone_device_update(tz,
2436 THERMAL_EVENT_UNSPECIFIED);
Zhang Ruiff140fe2015-10-30 16:31:58 +08002437 }
2438 break;
2439 default:
2440 break;
2441 }
2442 return 0;
2443}
2444
2445static struct notifier_block thermal_pm_nb = {
2446 .notifier_call = thermal_pm_notify,
2447};
2448
Zhang Rui203d3d42008-01-17 15:51:08 +08002449static int __init thermal_init(void)
2450{
Zhang Rui80a26a52013-03-26 16:38:29 +08002451 int result;
2452
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -06002453 thermal_passive_wq = alloc_workqueue("thermal_passive_wq",
2454 WQ_HIGHPRI | WQ_UNBOUND
2455 | WQ_FREEZABLE,
2456 THERMAL_MAX_ACTIVE);
2457 if (!thermal_passive_wq) {
2458 result = -ENOMEM;
2459 goto init_exit;
2460 }
2461
Zhang Rui80a26a52013-03-26 16:38:29 +08002462 result = thermal_register_governors();
2463 if (result)
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -06002464 goto destroy_wq;
Zhang Rui203d3d42008-01-17 15:51:08 +08002465
2466 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08002467 if (result)
2468 goto unregister_governors;
2469
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002470 result = of_parse_thermal_zones();
2471 if (result)
Lina Iyer9bf792d2016-07-11 13:27:11 -06002472 goto exit_zone_parse;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002473
Zhang Ruiff140fe2015-10-30 16:31:58 +08002474 result = register_pm_notifier(&thermal_pm_nb);
2475 if (result)
2476 pr_warn("Thermal: Can not register suspend notifier, return %d\n",
2477 result);
2478
Zhang Rui80a26a52013-03-26 16:38:29 +08002479 return 0;
2480
Lina Iyer9bf792d2016-07-11 13:27:11 -06002481exit_zone_parse:
Zhang Rui80a26a52013-03-26 16:38:29 +08002482 class_unregister(&thermal_class);
Luis Henriques9d367e52014-12-03 21:20:21 +00002483unregister_governors:
2484 thermal_unregister_governors();
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -06002485destroy_wq:
2486 destroy_workqueue(thermal_passive_wq);
Lina Iyer9bf792d2016-07-11 13:27:11 -06002487init_exit:
Zhang Rui80a26a52013-03-26 16:38:29 +08002488 idr_destroy(&thermal_tz_idr);
2489 idr_destroy(&thermal_cdev_idr);
2490 mutex_destroy(&thermal_idr_lock);
2491 mutex_destroy(&thermal_list_lock);
2492 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08002493 return result;
2494}
2495
Ram Chandrasekarfe0ae222017-04-06 12:08:38 -06002496static void thermal_exit(void)
Zhang Rui203d3d42008-01-17 15:51:08 +08002497{
Zhang Ruiff140fe2015-10-30 16:31:58 +08002498 unregister_pm_notifier(&thermal_pm_nb);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002499 of_thermal_destroy_zones();
Ram Chandrasekar2dd98f12016-08-04 13:02:03 -06002500 destroy_workqueue(thermal_passive_wq);
Zhang Rui80a26a52013-03-26 16:38:29 +08002501 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08002502 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08002503 thermal_unregister_governors();
Zhang Rui203d3d42008-01-17 15:51:08 +08002504 idr_destroy(&thermal_tz_idr);
2505 idr_destroy(&thermal_cdev_idr);
2506 mutex_destroy(&thermal_idr_lock);
2507 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08002508 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08002509}
2510
Lina Iyer9bf792d2016-07-11 13:27:11 -06002511static int __init thermal_netlink_init(void)
2512{
2513 int ret = 0;
2514
2515 ret = genetlink_init();
2516 if (!ret)
2517 goto exit_netlink;
2518
2519 thermal_exit();
2520exit_netlink:
2521 return ret;
2522}
2523
2524subsys_initcall(thermal_init);
2525fs_initcall(thermal_netlink_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08002526module_exit(thermal_exit);