blob: 682bc1ef9c3738a2c425116fd3af0666f73447ec [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 Rui203d3d42008-01-17 15:51:08 +080040
Punit Agrawal100a8fd2014-07-29 11:50:48 +010041#define CREATE_TRACE_POINTS
42#include <trace/events/thermal.h>
43
Durgadoss R71350db2012-09-18 11:04:53 +053044#include "thermal_core.h"
Eduardo Valentin0dd88792013-07-03 15:14:28 -040045#include "thermal_hwmon.h"
Durgadoss R71350db2012-09-18 11:04:53 +053046
Zhang Rui63c4ec92008-04-21 16:07:13 +080047MODULE_AUTHOR("Zhang Rui");
Zhang Rui203d3d42008-01-17 15:51:08 +080048MODULE_DESCRIPTION("Generic thermal management sysfs support");
Eduardo Valentin6d8d4972013-04-23 21:48:13 +000049MODULE_LICENSE("GPL v2");
Zhang Rui203d3d42008-01-17 15:51:08 +080050
Zhang Rui203d3d42008-01-17 15:51:08 +080051static DEFINE_IDR(thermal_tz_idr);
52static DEFINE_IDR(thermal_cdev_idr);
53static DEFINE_MUTEX(thermal_idr_lock);
54
55static LIST_HEAD(thermal_tz_list);
56static LIST_HEAD(thermal_cdev_list);
Durgadoss Ra4a15482012-09-18 11:04:57 +053057static LIST_HEAD(thermal_governor_list);
58
Zhang Rui203d3d42008-01-17 15:51:08 +080059static DEFINE_MUTEX(thermal_list_lock);
Durgadoss Ra4a15482012-09-18 11:04:57 +053060static DEFINE_MUTEX(thermal_governor_lock);
61
Zhang Ruif2234bc2014-01-24 10:23:19 +080062static struct thermal_governor *def_governor;
63
Durgadoss Ra4a15482012-09-18 11:04:57 +053064static struct thermal_governor *__find_governor(const char *name)
65{
66 struct thermal_governor *pos;
67
Zhang Ruif2234bc2014-01-24 10:23:19 +080068 if (!name || !name[0])
69 return def_governor;
70
Durgadoss Ra4a15482012-09-18 11:04:57 +053071 list_for_each_entry(pos, &thermal_governor_list, governor_list)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -070072 if (!strncasecmp(name, pos->name, THERMAL_NAME_LENGTH))
Durgadoss Ra4a15482012-09-18 11:04:57 +053073 return pos;
74
75 return NULL;
76}
77
Javi Merinoe33df1d2015-02-26 19:00:27 +000078/**
79 * bind_previous_governor() - bind the previous governor of the thermal zone
80 * @tz: a valid pointer to a struct thermal_zone_device
81 * @failed_gov_name: the name of the governor that failed to register
82 *
83 * Register the previous governor of the thermal zone after a new
84 * governor has failed to be bound.
85 */
86static void bind_previous_governor(struct thermal_zone_device *tz,
87 const char *failed_gov_name)
88{
89 if (tz->governor && tz->governor->bind_to_tz) {
90 if (tz->governor->bind_to_tz(tz)) {
91 dev_err(&tz->device,
92 "governor %s failed to bind and the previous one (%s) failed to bind again, thermal zone %s has no governor\n",
93 failed_gov_name, tz->governor->name, tz->type);
94 tz->governor = NULL;
95 }
96 }
97}
98
99/**
100 * thermal_set_governor() - Switch to another governor
101 * @tz: a valid pointer to a struct thermal_zone_device
102 * @new_gov: pointer to the new governor
103 *
104 * Change the governor of thermal zone @tz.
105 *
106 * Return: 0 on success, an error if the new governor's bind_to_tz() failed.
107 */
108static int thermal_set_governor(struct thermal_zone_device *tz,
109 struct thermal_governor *new_gov)
110{
111 int ret = 0;
112
113 if (tz->governor && tz->governor->unbind_from_tz)
114 tz->governor->unbind_from_tz(tz);
115
116 if (new_gov && new_gov->bind_to_tz) {
117 ret = new_gov->bind_to_tz(tz);
118 if (ret) {
119 bind_previous_governor(tz, new_gov->name);
120
121 return ret;
122 }
123 }
124
125 tz->governor = new_gov;
126
127 return ret;
128}
129
Durgadoss Ra4a15482012-09-18 11:04:57 +0530130int thermal_register_governor(struct thermal_governor *governor)
131{
132 int err;
133 const char *name;
134 struct thermal_zone_device *pos;
135
136 if (!governor)
137 return -EINVAL;
138
139 mutex_lock(&thermal_governor_lock);
140
141 err = -EBUSY;
142 if (__find_governor(governor->name) == NULL) {
143 err = 0;
144 list_add(&governor->governor_list, &thermal_governor_list);
Zhang Ruif2234bc2014-01-24 10:23:19 +0800145 if (!def_governor && !strncmp(governor->name,
146 DEFAULT_THERMAL_GOVERNOR, THERMAL_NAME_LENGTH))
147 def_governor = governor;
Durgadoss Ra4a15482012-09-18 11:04:57 +0530148 }
149
150 mutex_lock(&thermal_list_lock);
151
152 list_for_each_entry(pos, &thermal_tz_list, node) {
Zhang Ruif2234bc2014-01-24 10:23:19 +0800153 /*
154 * only thermal zones with specified tz->tzp->governor_name
155 * may run with tz->govenor unset
156 */
Durgadoss Ra4a15482012-09-18 11:04:57 +0530157 if (pos->governor)
158 continue;
Zhang Ruif2234bc2014-01-24 10:23:19 +0800159
160 name = pos->tzp->governor_name;
161
Javi Merinoe33df1d2015-02-26 19:00:27 +0000162 if (!strncasecmp(name, governor->name, THERMAL_NAME_LENGTH)) {
163 int ret;
164
165 ret = thermal_set_governor(pos, governor);
166 if (ret)
167 dev_err(&pos->device,
168 "Failed to set governor %s for thermal zone %s: %d\n",
169 governor->name, pos->type, ret);
170 }
Durgadoss Ra4a15482012-09-18 11:04:57 +0530171 }
172
173 mutex_unlock(&thermal_list_lock);
174 mutex_unlock(&thermal_governor_lock);
175
176 return err;
177}
Durgadoss Ra4a15482012-09-18 11:04:57 +0530178
179void thermal_unregister_governor(struct thermal_governor *governor)
180{
181 struct thermal_zone_device *pos;
182
183 if (!governor)
184 return;
185
186 mutex_lock(&thermal_governor_lock);
187
188 if (__find_governor(governor->name) == NULL)
189 goto exit;
190
191 mutex_lock(&thermal_list_lock);
192
193 list_for_each_entry(pos, &thermal_tz_list, node) {
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -0700194 if (!strncasecmp(pos->governor->name, governor->name,
Durgadoss Ra4a15482012-09-18 11:04:57 +0530195 THERMAL_NAME_LENGTH))
Javi Merinoe33df1d2015-02-26 19:00:27 +0000196 thermal_set_governor(pos, NULL);
Durgadoss Ra4a15482012-09-18 11:04:57 +0530197 }
198
199 mutex_unlock(&thermal_list_lock);
200 list_del(&governor->governor_list);
201exit:
202 mutex_unlock(&thermal_governor_lock);
203 return;
204}
Zhang Rui203d3d42008-01-17 15:51:08 +0800205
206static int get_idr(struct idr *idr, struct mutex *lock, int *id)
207{
Tejun Heo6deb69f2013-02-27 17:04:46 -0800208 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800209
210 if (lock)
211 mutex_lock(lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800212 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +0800213 if (lock)
214 mutex_unlock(lock);
Tejun Heo6deb69f2013-02-27 17:04:46 -0800215 if (unlikely(ret < 0))
216 return ret;
217 *id = ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800218 return 0;
219}
220
221static void release_idr(struct idr *idr, struct mutex *lock, int id)
222{
223 if (lock)
224 mutex_lock(lock);
225 idr_remove(idr, id);
226 if (lock)
227 mutex_unlock(lock);
228}
229
Durgadoss R9b4298a2012-09-18 11:04:54 +0530230int get_tz_trend(struct thermal_zone_device *tz, int trip)
231{
232 enum thermal_trend trend;
233
Eduardo Valentin0c872502013-05-29 21:37:00 +0000234 if (tz->emul_temperature || !tz->ops->get_trend ||
235 tz->ops->get_trend(tz, trip, &trend)) {
Durgadoss R9b4298a2012-09-18 11:04:54 +0530236 if (tz->temperature > tz->last_temperature)
237 trend = THERMAL_TREND_RAISING;
238 else if (tz->temperature < tz->last_temperature)
239 trend = THERMAL_TREND_DROPPING;
240 else
241 trend = THERMAL_TREND_STABLE;
242 }
243
244 return trend;
245}
246EXPORT_SYMBOL(get_tz_trend);
247
248struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
249 struct thermal_cooling_device *cdev, int trip)
250{
251 struct thermal_instance *pos = NULL;
252 struct thermal_instance *target_instance = NULL;
253
254 mutex_lock(&tz->lock);
255 mutex_lock(&cdev->lock);
256
257 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
258 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
259 target_instance = pos;
260 break;
261 }
262 }
263
264 mutex_unlock(&cdev->lock);
265 mutex_unlock(&tz->lock);
266
267 return target_instance;
268}
269EXPORT_SYMBOL(get_thermal_instance);
270
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530271static void print_bind_err_msg(struct thermal_zone_device *tz,
272 struct thermal_cooling_device *cdev, int ret)
273{
274 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
275 tz->type, cdev->type, ret);
276}
277
278static void __bind(struct thermal_zone_device *tz, int mask,
Eduardo Valentina8892d832013-07-16 15:26:28 -0400279 struct thermal_cooling_device *cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000280 unsigned long *limits,
281 unsigned int weight)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530282{
283 int i, ret;
284
285 for (i = 0; i < tz->trips; i++) {
286 if (mask & (1 << i)) {
Eduardo Valentina8892d832013-07-16 15:26:28 -0400287 unsigned long upper, lower;
288
289 upper = THERMAL_NO_LIMIT;
290 lower = THERMAL_NO_LIMIT;
291 if (limits) {
292 lower = limits[i * 2];
293 upper = limits[i * 2 + 1];
294 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530295 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000296 upper, lower,
297 weight);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530298 if (ret)
299 print_bind_err_msg(tz, cdev, ret);
300 }
301 }
302}
303
304static void __unbind(struct thermal_zone_device *tz, int mask,
305 struct thermal_cooling_device *cdev)
306{
307 int i;
308
309 for (i = 0; i < tz->trips; i++)
310 if (mask & (1 << i))
311 thermal_zone_unbind_cooling_device(tz, i, cdev);
312}
313
314static void bind_cdev(struct thermal_cooling_device *cdev)
315{
316 int i, ret;
317 const struct thermal_zone_params *tzp;
318 struct thermal_zone_device *pos = NULL;
319
320 mutex_lock(&thermal_list_lock);
321
322 list_for_each_entry(pos, &thermal_tz_list, node) {
323 if (!pos->tzp && !pos->ops->bind)
324 continue;
325
Ni Wadea9f2d192013-11-06 14:30:13 +0800326 if (pos->ops->bind) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530327 ret = pos->ops->bind(pos, cdev);
328 if (ret)
329 print_bind_err_msg(pos, cdev, ret);
Ni Wadea9f2d192013-11-06 14:30:13 +0800330 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530331 }
332
333 tzp = pos->tzp;
Hugh Dickins791700cd2012-09-27 16:48:28 -0700334 if (!tzp || !tzp->tbp)
335 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530336
337 for (i = 0; i < tzp->num_tbps; i++) {
338 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
339 continue;
340 if (tzp->tbp[i].match(pos, cdev))
341 continue;
342 tzp->tbp[i].cdev = cdev;
Eduardo Valentina8892d832013-07-16 15:26:28 -0400343 __bind(pos, tzp->tbp[i].trip_mask, cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000344 tzp->tbp[i].binding_limits,
345 tzp->tbp[i].weight);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530346 }
347 }
348
349 mutex_unlock(&thermal_list_lock);
350}
351
352static void bind_tz(struct thermal_zone_device *tz)
353{
354 int i, ret;
355 struct thermal_cooling_device *pos = NULL;
356 const struct thermal_zone_params *tzp = tz->tzp;
357
358 if (!tzp && !tz->ops->bind)
359 return;
360
361 mutex_lock(&thermal_list_lock);
362
Ni Wadea9f2d192013-11-06 14:30:13 +0800363 /* If there is ops->bind, try to use ops->bind */
364 if (tz->ops->bind) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530365 list_for_each_entry(pos, &thermal_cdev_list, node) {
366 ret = tz->ops->bind(tz, pos);
367 if (ret)
368 print_bind_err_msg(tz, pos, ret);
369 }
370 goto exit;
371 }
372
Hugh Dickins791700cd2012-09-27 16:48:28 -0700373 if (!tzp || !tzp->tbp)
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530374 goto exit;
375
376 list_for_each_entry(pos, &thermal_cdev_list, node) {
377 for (i = 0; i < tzp->num_tbps; i++) {
378 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
379 continue;
380 if (tzp->tbp[i].match(tz, pos))
381 continue;
382 tzp->tbp[i].cdev = pos;
Eduardo Valentina8892d832013-07-16 15:26:28 -0400383 __bind(tz, tzp->tbp[i].trip_mask, pos,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000384 tzp->tbp[i].binding_limits,
385 tzp->tbp[i].weight);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +0530386 }
387 }
388exit:
389 mutex_unlock(&thermal_list_lock);
390}
391
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530392static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
393 int delay)
394{
395 if (delay > 1000)
396 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
397 round_jiffies(msecs_to_jiffies(delay)));
398 else if (delay)
399 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
400 msecs_to_jiffies(delay));
401 else
402 cancel_delayed_work(&tz->poll_queue);
403}
404
405static void monitor_thermal_zone(struct thermal_zone_device *tz)
406{
407 mutex_lock(&tz->lock);
408
409 if (tz->passive)
410 thermal_zone_device_set_polling(tz, tz->passive_delay);
411 else if (tz->polling_delay)
412 thermal_zone_device_set_polling(tz, tz->polling_delay);
413 else
414 thermal_zone_device_set_polling(tz, 0);
415
416 mutex_unlock(&tz->lock);
417}
418
419static void handle_non_critical_trips(struct thermal_zone_device *tz,
420 int trip, enum thermal_trip_type trip_type)
421{
Zhang Ruif2234bc2014-01-24 10:23:19 +0800422 tz->governor ? tz->governor->throttle(tz, trip) :
423 def_governor->throttle(tz, trip);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530424}
425
426static void handle_critical_trips(struct thermal_zone_device *tz,
427 int trip, enum thermal_trip_type trip_type)
428{
Sascha Hauer17e83512015-07-24 08:12:54 +0200429 int trip_temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530430
431 tz->ops->get_trip_temp(tz, trip, &trip_temp);
432
433 /* If we have not crossed the trip_temp, we do not care. */
Srinivas Pandruvada84ffe3e2014-11-12 15:43:29 -0800434 if (trip_temp <= 0 || tz->temperature < trip_temp)
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530435 return;
436
Punit Agrawal208cd822014-07-29 11:50:50 +0100437 trace_thermal_zone_trip(tz, trip, trip_type);
438
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530439 if (tz->ops->notify)
440 tz->ops->notify(tz, trip, trip_type);
441
442 if (trip_type == THERMAL_TRIP_CRITICAL) {
Eduardo Valentin923e0b12013-01-02 15:29:41 +0000443 dev_emerg(&tz->device,
444 "critical temperature reached(%d C),shutting down\n",
445 tz->temperature / 1000);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530446 orderly_poweroff(true);
447 }
448}
449
450static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
451{
452 enum thermal_trip_type type;
453
454 tz->ops->get_trip_type(tz, trip, &type);
455
456 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
457 handle_critical_trips(tz, trip, type);
458 else
459 handle_non_critical_trips(tz, trip, type);
460 /*
461 * Alright, we handled this trip successfully.
462 * So, start monitoring again.
463 */
464 monitor_thermal_zone(tz);
465}
466
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000467/**
Sascha Hauerf6be0582015-07-06 09:46:14 +0200468 * thermal_zone_get_temp() - returns the temperature of a thermal zone
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000469 * @tz: a valid pointer to a struct thermal_zone_device
470 * @temp: a valid pointer to where to store the resulting temperature.
471 *
472 * When a valid thermal zone reference is passed, it will fetch its
473 * temperature and fill @temp.
474 *
475 * Return: On success returns 0, an error code otherwise
476 */
Sascha Hauer17e83512015-07-24 08:12:54 +0200477int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000478{
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000479 int ret = -EINVAL;
Zhang Rui5e20b2e2013-02-06 14:02:12 +0800480 int count;
Sascha Hauer17e83512015-07-24 08:12:54 +0200481 int crit_temp = INT_MAX;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000482 enum thermal_trip_type type;
483
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400484 if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000485 goto exit;
486
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000487 mutex_lock(&tz->lock);
488
489 ret = tz->ops->get_temp(tz, temp);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000490
Sascha Hauer79e54212015-07-06 09:46:16 +0200491 if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
492 for (count = 0; count < tz->trips; count++) {
493 ret = tz->ops->get_trip_type(tz, count, &type);
494 if (!ret && type == THERMAL_TRIP_CRITICAL) {
495 ret = tz->ops->get_trip_temp(tz, count,
496 &crit_temp);
497 break;
498 }
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000499 }
Sascha Hauer79e54212015-07-06 09:46:16 +0200500
Sascha Hauer934c93b2015-07-06 09:46:17 +0200501 /*
502 * Only allow emulating a temperature when the real temperature
503 * is below the critical temperature so that the emulation code
504 * cannot hide critical conditions.
505 */
Sascha Hauer79e54212015-07-06 09:46:16 +0200506 if (!ret && *temp < crit_temp)
507 *temp = tz->emul_temperature;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000508 }
Sascha Hauer79e54212015-07-06 09:46:16 +0200509
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000510 mutex_unlock(&tz->lock);
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000511exit:
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000512 return ret;
513}
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000514EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000515
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530516static void update_temperature(struct thermal_zone_device *tz)
517{
Sascha Hauer17e83512015-07-24 08:12:54 +0200518 int temp, ret;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530519
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000520 ret = thermal_zone_get_temp(tz, &temp);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530521 if (ret) {
Hans de Goede7e497a72015-03-21 15:02:55 +0100522 if (ret != -EAGAIN)
523 dev_warn(&tz->device,
524 "failed to read out thermal zone (%d)\n",
525 ret);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000526 return;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530527 }
528
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000529 mutex_lock(&tz->lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530530 tz->last_temperature = tz->temperature;
531 tz->temperature = temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530532 mutex_unlock(&tz->lock);
Aaron Lu06475b52013-12-02 13:54:26 +0800533
Punit Agrawal100a8fd2014-07-29 11:50:48 +0100534 trace_thermal_temperature(tz);
Zhang Ruibb431ba2015-10-30 16:31:47 +0800535 if (tz->last_temperature == THERMAL_TEMP_INVALID)
536 dev_dbg(&tz->device, "last_temperature N/A, current_temperature=%d\n",
537 tz->temperature);
538 else
539 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
540 tz->last_temperature, tz->temperature);
541}
542
543static void thermal_zone_device_reset(struct thermal_zone_device *tz)
544{
545 struct thermal_instance *pos;
546
547 tz->temperature = THERMAL_TEMP_INVALID;
548 tz->passive = 0;
549 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
550 pos->initialized = false;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530551}
552
553void thermal_zone_device_update(struct thermal_zone_device *tz)
554{
555 int count;
556
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400557 if (!tz->ops->get_temp)
558 return;
559
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530560 update_temperature(tz);
561
562 for (count = 0; count < tz->trips; count++)
563 handle_thermal_trip(tz, count);
564}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000565EXPORT_SYMBOL_GPL(thermal_zone_device_update);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530566
567static void thermal_zone_device_check(struct work_struct *work)
568{
569 struct thermal_zone_device *tz = container_of(work, struct
570 thermal_zone_device,
571 poll_queue.work);
572 thermal_zone_device_update(tz);
573}
574
Zhang Rui203d3d42008-01-17 15:51:08 +0800575/* sys I/F for thermal zone */
576
577#define to_thermal_zone(_dev) \
578 container_of(_dev, struct thermal_zone_device, device)
579
580static ssize_t
581type_show(struct device *dev, struct device_attribute *attr, char *buf)
582{
583 struct thermal_zone_device *tz = to_thermal_zone(dev);
584
585 return sprintf(buf, "%s\n", tz->type);
586}
587
588static ssize_t
589temp_show(struct device *dev, struct device_attribute *attr, char *buf)
590{
591 struct thermal_zone_device *tz = to_thermal_zone(dev);
Sascha Hauer17e83512015-07-24 08:12:54 +0200592 int temperature, ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800593
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000594 ret = thermal_zone_get_temp(tz, &temperature);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000595
596 if (ret)
597 return ret;
598
Sascha Hauer17e83512015-07-24 08:12:54 +0200599 return sprintf(buf, "%d\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800600}
601
602static ssize_t
603mode_show(struct device *dev, struct device_attribute *attr, char *buf)
604{
605 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000606 enum thermal_device_mode mode;
607 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800608
609 if (!tz->ops->get_mode)
610 return -EPERM;
611
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000612 result = tz->ops->get_mode(tz, &mode);
613 if (result)
614 return result;
615
616 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
617 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800618}
619
620static ssize_t
621mode_store(struct device *dev, struct device_attribute *attr,
622 const char *buf, size_t count)
623{
624 struct thermal_zone_device *tz = to_thermal_zone(dev);
625 int result;
626
627 if (!tz->ops->set_mode)
628 return -EPERM;
629
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530630 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000631 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530632 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000633 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
634 else
635 result = -EINVAL;
636
Zhang Rui203d3d42008-01-17 15:51:08 +0800637 if (result)
638 return result;
639
640 return count;
641}
642
643static ssize_t
644trip_point_type_show(struct device *dev, struct device_attribute *attr,
645 char *buf)
646{
647 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000648 enum thermal_trip_type type;
649 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800650
651 if (!tz->ops->get_trip_type)
652 return -EPERM;
653
654 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
655 return -EINVAL;
656
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000657 result = tz->ops->get_trip_type(tz, trip, &type);
658 if (result)
659 return result;
660
661 switch (type) {
662 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300663 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000664 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300665 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000666 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300667 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000668 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300669 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000670 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300671 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000672 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800673}
674
675static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800676trip_point_temp_store(struct device *dev, struct device_attribute *attr,
677 const char *buf, size_t count)
678{
679 struct thermal_zone_device *tz = to_thermal_zone(dev);
680 int trip, ret;
681 unsigned long temperature;
682
683 if (!tz->ops->set_trip_temp)
684 return -EPERM;
685
686 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
687 return -EINVAL;
688
689 if (kstrtoul(buf, 10, &temperature))
690 return -EINVAL;
691
692 ret = tz->ops->set_trip_temp(tz, trip, temperature);
693
694 return ret ? ret : count;
695}
696
697static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800698trip_point_temp_show(struct device *dev, struct device_attribute *attr,
699 char *buf)
700{
701 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000702 int trip, ret;
Sascha Hauer17e83512015-07-24 08:12:54 +0200703 int temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800704
705 if (!tz->ops->get_trip_temp)
706 return -EPERM;
707
708 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
709 return -EINVAL;
710
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000711 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
712
713 if (ret)
714 return ret;
715
Sascha Hauer17e83512015-07-24 08:12:54 +0200716 return sprintf(buf, "%d\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800717}
718
Matthew Garrett03a971a2008-12-03 18:00:38 +0000719static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800720trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
721 const char *buf, size_t count)
722{
723 struct thermal_zone_device *tz = to_thermal_zone(dev);
724 int trip, ret;
Sascha Hauer17e83512015-07-24 08:12:54 +0200725 int temperature;
Durgadoss R27365a62012-07-25 10:10:59 +0800726
727 if (!tz->ops->set_trip_hyst)
728 return -EPERM;
729
730 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
731 return -EINVAL;
732
Sascha Hauer17e83512015-07-24 08:12:54 +0200733 if (kstrtoint(buf, 10, &temperature))
Durgadoss R27365a62012-07-25 10:10:59 +0800734 return -EINVAL;
735
736 /*
737 * We are not doing any check on the 'temperature' value
738 * here. The driver implementing 'set_trip_hyst' has to
739 * take care of this.
740 */
741 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
742
743 return ret ? ret : count;
744}
745
746static ssize_t
747trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
748 char *buf)
749{
750 struct thermal_zone_device *tz = to_thermal_zone(dev);
751 int trip, ret;
Sascha Hauer17e83512015-07-24 08:12:54 +0200752 int temperature;
Durgadoss R27365a62012-07-25 10:10:59 +0800753
754 if (!tz->ops->get_trip_hyst)
755 return -EPERM;
756
757 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
758 return -EINVAL;
759
760 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
761
Sascha Hauer17e83512015-07-24 08:12:54 +0200762 return ret ? ret : sprintf(buf, "%d\n", temperature);
Durgadoss R27365a62012-07-25 10:10:59 +0800763}
764
765static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000766passive_store(struct device *dev, struct device_attribute *attr,
767 const char *buf, size_t count)
768{
769 struct thermal_zone_device *tz = to_thermal_zone(dev);
770 struct thermal_cooling_device *cdev = NULL;
771 int state;
772
773 if (!sscanf(buf, "%d\n", &state))
774 return -EINVAL;
775
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100776 /* sanity check: values below 1000 millicelcius don't make sense
777 * and can cause the system to go into a thermal heart attack
778 */
779 if (state && state < 1000)
780 return -EINVAL;
781
Matthew Garrett03a971a2008-12-03 18:00:38 +0000782 if (state && !tz->forced_passive) {
783 mutex_lock(&thermal_list_lock);
784 list_for_each_entry(cdev, &thermal_cdev_list, node) {
785 if (!strncmp("Processor", cdev->type,
786 sizeof("Processor")))
787 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800788 THERMAL_TRIPS_NONE, cdev,
789 THERMAL_NO_LIMIT,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000790 THERMAL_NO_LIMIT,
791 THERMAL_WEIGHT_DEFAULT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000792 }
793 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100794 if (!tz->passive_delay)
795 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000796 } else if (!state && tz->forced_passive) {
797 mutex_lock(&thermal_list_lock);
798 list_for_each_entry(cdev, &thermal_cdev_list, node) {
799 if (!strncmp("Processor", cdev->type,
800 sizeof("Processor")))
801 thermal_zone_unbind_cooling_device(tz,
802 THERMAL_TRIPS_NONE,
803 cdev);
804 }
805 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100806 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000807 }
808
Matthew Garrett03a971a2008-12-03 18:00:38 +0000809 tz->forced_passive = state;
810
811 thermal_zone_device_update(tz);
812
813 return count;
814}
815
816static ssize_t
817passive_show(struct device *dev, struct device_attribute *attr,
818 char *buf)
819{
820 struct thermal_zone_device *tz = to_thermal_zone(dev);
821
822 return sprintf(buf, "%d\n", tz->forced_passive);
823}
824
Durgadoss R5a2c0902012-09-18 11:04:58 +0530825static ssize_t
826policy_store(struct device *dev, struct device_attribute *attr,
827 const char *buf, size_t count)
828{
829 int ret = -EINVAL;
830 struct thermal_zone_device *tz = to_thermal_zone(dev);
831 struct thermal_governor *gov;
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000832 char name[THERMAL_NAME_LENGTH];
833
834 snprintf(name, sizeof(name), "%s", buf);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530835
836 mutex_lock(&thermal_governor_lock);
Javi Merinob6cc7722014-11-25 16:00:33 +0000837 mutex_lock(&tz->lock);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530838
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000839 gov = __find_governor(strim(name));
Durgadoss R5a2c0902012-09-18 11:04:58 +0530840 if (!gov)
841 goto exit;
842
Javi Merinoe33df1d2015-02-26 19:00:27 +0000843 ret = thermal_set_governor(tz, gov);
844 if (!ret)
845 ret = count;
Durgadoss R5a2c0902012-09-18 11:04:58 +0530846
847exit:
Javi Merinob6cc7722014-11-25 16:00:33 +0000848 mutex_unlock(&tz->lock);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530849 mutex_unlock(&thermal_governor_lock);
850 return ret;
851}
852
853static ssize_t
854policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
855{
856 struct thermal_zone_device *tz = to_thermal_zone(dev);
857
858 return sprintf(buf, "%s\n", tz->governor->name);
859}
860
Ni Wade25a0a5c2015-07-14 15:40:56 +0800861static ssize_t
862available_policies_show(struct device *dev, struct device_attribute *devattr,
863 char *buf)
864{
865 struct thermal_governor *pos;
866 ssize_t count = 0;
867 ssize_t size = PAGE_SIZE;
868
869 mutex_lock(&thermal_governor_lock);
870
871 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
872 size = PAGE_SIZE - count;
873 count += scnprintf(buf + count, size, "%s ", pos->name);
874 }
875 count += scnprintf(buf + count, size, "\n");
876
877 mutex_unlock(&thermal_governor_lock);
878
879 return count;
880}
881
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000882static ssize_t
883emul_temp_store(struct device *dev, struct device_attribute *attr,
884 const char *buf, size_t count)
885{
886 struct thermal_zone_device *tz = to_thermal_zone(dev);
887 int ret = 0;
888 unsigned long temperature;
889
890 if (kstrtoul(buf, 10, &temperature))
891 return -EINVAL;
892
893 if (!tz->ops->set_emul_temp) {
894 mutex_lock(&tz->lock);
895 tz->emul_temperature = temperature;
896 mutex_unlock(&tz->lock);
897 } else {
898 ret = tz->ops->set_emul_temp(tz, temperature);
899 }
900
lan,Tianyu800744b2014-01-02 15:47:54 +0800901 if (!ret)
902 thermal_zone_device_update(tz);
903
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000904 return ret ? ret : count;
905}
906static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000907
Javi Merino9f382712015-03-26 15:53:02 +0000908static ssize_t
909sustainable_power_show(struct device *dev, struct device_attribute *devattr,
910 char *buf)
911{
912 struct thermal_zone_device *tz = to_thermal_zone(dev);
913
914 if (tz->tzp)
915 return sprintf(buf, "%u\n", tz->tzp->sustainable_power);
916 else
917 return -EIO;
918}
919
920static ssize_t
921sustainable_power_store(struct device *dev, struct device_attribute *devattr,
922 const char *buf, size_t count)
923{
924 struct thermal_zone_device *tz = to_thermal_zone(dev);
925 u32 sustainable_power;
926
927 if (!tz->tzp)
928 return -EIO;
929
930 if (kstrtou32(buf, 10, &sustainable_power))
931 return -EINVAL;
932
933 tz->tzp->sustainable_power = sustainable_power;
934
935 return count;
936}
937static DEVICE_ATTR(sustainable_power, S_IWUSR | S_IRUGO, sustainable_power_show,
938 sustainable_power_store);
939
940#define create_s32_tzp_attr(name) \
941 static ssize_t \
942 name##_show(struct device *dev, struct device_attribute *devattr, \
943 char *buf) \
944 { \
945 struct thermal_zone_device *tz = to_thermal_zone(dev); \
946 \
947 if (tz->tzp) \
948 return sprintf(buf, "%u\n", tz->tzp->name); \
949 else \
950 return -EIO; \
951 } \
952 \
953 static ssize_t \
954 name##_store(struct device *dev, struct device_attribute *devattr, \
955 const char *buf, size_t count) \
956 { \
957 struct thermal_zone_device *tz = to_thermal_zone(dev); \
958 s32 value; \
959 \
960 if (!tz->tzp) \
961 return -EIO; \
962 \
963 if (kstrtos32(buf, 10, &value)) \
964 return -EINVAL; \
965 \
966 tz->tzp->name = value; \
967 \
968 return count; \
969 } \
970 static DEVICE_ATTR(name, S_IWUSR | S_IRUGO, name##_show, name##_store)
971
972create_s32_tzp_attr(k_po);
973create_s32_tzp_attr(k_pu);
974create_s32_tzp_attr(k_i);
975create_s32_tzp_attr(k_d);
976create_s32_tzp_attr(integral_cutoff);
Eduardo Valentin9d0be7f2015-05-11 19:34:23 -0700977create_s32_tzp_attr(slope);
978create_s32_tzp_attr(offset);
Javi Merino9f382712015-03-26 15:53:02 +0000979#undef create_s32_tzp_attr
980
981static struct device_attribute *dev_tzp_attrs[] = {
982 &dev_attr_sustainable_power,
983 &dev_attr_k_po,
984 &dev_attr_k_pu,
985 &dev_attr_k_i,
986 &dev_attr_k_d,
987 &dev_attr_integral_cutoff,
Eduardo Valentin9d0be7f2015-05-11 19:34:23 -0700988 &dev_attr_slope,
989 &dev_attr_offset,
Javi Merino9f382712015-03-26 15:53:02 +0000990};
991
992static int create_tzp_attrs(struct device *dev)
993{
994 int i;
995
996 for (i = 0; i < ARRAY_SIZE(dev_tzp_attrs); i++) {
997 int ret;
998 struct device_attribute *dev_attr = dev_tzp_attrs[i];
999
1000 ret = device_create_file(dev, dev_attr);
1001 if (ret)
1002 return ret;
1003 }
1004
1005 return 0;
1006}
1007
Javi Merino35b11d22015-02-26 19:00:28 +00001008/**
1009 * power_actor_get_max_power() - get the maximum power that a cdev can consume
1010 * @cdev: pointer to &thermal_cooling_device
1011 * @tz: a valid thermal zone device pointer
1012 * @max_power: pointer in which to store the maximum power
1013 *
1014 * Calculate the maximum power consumption in milliwats that the
1015 * cooling device can currently consume and store it in @max_power.
1016 *
1017 * Return: 0 on success, -EINVAL if @cdev doesn't support the
1018 * power_actor API or -E* on other error.
1019 */
1020int power_actor_get_max_power(struct thermal_cooling_device *cdev,
1021 struct thermal_zone_device *tz, u32 *max_power)
1022{
1023 if (!cdev_is_power_actor(cdev))
1024 return -EINVAL;
1025
1026 return cdev->ops->state2power(cdev, tz, 0, max_power);
1027}
1028
1029/**
Javi Merinoc973c3b2015-09-14 14:23:50 +01001030 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
1031 * @cdev: pointer to &thermal_cooling_device
1032 * @tz: a valid thermal zone device pointer
1033 * @min_power: pointer in which to store the minimum power
1034 *
1035 * Calculate the minimum power consumption in milliwatts that the
1036 * cooling device can currently consume and store it in @min_power.
1037 *
1038 * Return: 0 on success, -EINVAL if @cdev doesn't support the
1039 * power_actor API or -E* on other error.
1040 */
1041int power_actor_get_min_power(struct thermal_cooling_device *cdev,
1042 struct thermal_zone_device *tz, u32 *min_power)
1043{
1044 unsigned long max_state;
1045 int ret;
1046
1047 if (!cdev_is_power_actor(cdev))
1048 return -EINVAL;
1049
1050 ret = cdev->ops->get_max_state(cdev, &max_state);
1051 if (ret)
1052 return ret;
1053
1054 return cdev->ops->state2power(cdev, tz, max_state, min_power);
1055}
1056
1057/**
Javi Merino35b11d22015-02-26 19:00:28 +00001058 * power_actor_set_power() - limit the maximum power that a cooling device can consume
1059 * @cdev: pointer to &thermal_cooling_device
1060 * @instance: thermal instance to update
1061 * @power: the power in milliwatts
1062 *
1063 * Set the cooling device to consume at most @power milliwatts.
1064 *
1065 * Return: 0 on success, -EINVAL if the cooling device does not
1066 * implement the power actor API or -E* for other failures.
1067 */
1068int power_actor_set_power(struct thermal_cooling_device *cdev,
1069 struct thermal_instance *instance, u32 power)
1070{
1071 unsigned long state;
1072 int ret;
1073
1074 if (!cdev_is_power_actor(cdev))
1075 return -EINVAL;
1076
1077 ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
1078 if (ret)
1079 return ret;
1080
1081 instance->target = state;
1082 cdev->updated = false;
1083 thermal_cdev_update(cdev);
1084
1085 return 0;
1086}
1087
Zhang Rui203d3d42008-01-17 15:51:08 +08001088static DEVICE_ATTR(type, 0444, type_show, NULL);
1089static DEVICE_ATTR(temp, 0444, temp_show, NULL);
1090static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -07001091static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301092static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
Ni Wade25a0a5c2015-07-14 15:40:56 +08001093static DEVICE_ATTR(available_policies, S_IRUGO, available_policies_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001094
Zhang Rui203d3d42008-01-17 15:51:08 +08001095/* sys I/F for cooling device */
1096#define to_cooling_device(_dev) \
1097 container_of(_dev, struct thermal_cooling_device, device)
1098
1099static ssize_t
1100thermal_cooling_device_type_show(struct device *dev,
1101 struct device_attribute *attr, char *buf)
1102{
1103 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1104
1105 return sprintf(buf, "%s\n", cdev->type);
1106}
1107
1108static ssize_t
1109thermal_cooling_device_max_state_show(struct device *dev,
1110 struct device_attribute *attr, char *buf)
1111{
1112 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001113 unsigned long state;
1114 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +08001115
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001116 ret = cdev->ops->get_max_state(cdev, &state);
1117 if (ret)
1118 return ret;
1119 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +08001120}
1121
1122static ssize_t
1123thermal_cooling_device_cur_state_show(struct device *dev,
1124 struct device_attribute *attr, char *buf)
1125{
1126 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001127 unsigned long state;
1128 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +08001129
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001130 ret = cdev->ops->get_cur_state(cdev, &state);
1131 if (ret)
1132 return ret;
1133 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +08001134}
1135
1136static ssize_t
1137thermal_cooling_device_cur_state_store(struct device *dev,
1138 struct device_attribute *attr,
1139 const char *buf, size_t count)
1140{
1141 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001142 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +08001143 int result;
1144
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001145 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +08001146 return -EINVAL;
1147
Roel Kluinedb94912009-12-15 22:46:50 +01001148 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +08001149 return -EINVAL;
1150
1151 result = cdev->ops->set_cur_state(cdev, state);
1152 if (result)
1153 return result;
1154 return count;
1155}
1156
1157static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -05001158__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001159static DEVICE_ATTR(max_state, 0444,
1160 thermal_cooling_device_max_state_show, NULL);
1161static DEVICE_ATTR(cur_state, 0644,
1162 thermal_cooling_device_cur_state_show,
1163 thermal_cooling_device_cur_state_store);
1164
1165static ssize_t
1166thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -05001167 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +08001168{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001169 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +08001170
1171 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001172 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001173
1174 if (instance->trip == THERMAL_TRIPS_NONE)
1175 return sprintf(buf, "-1\n");
1176 else
1177 return sprintf(buf, "%d\n", instance->trip);
1178}
1179
Matthias Kaehlcke2dc10f82015-02-20 18:10:08 -08001180static struct attribute *cooling_device_attrs[] = {
1181 &dev_attr_cdev_type.attr,
1182 &dev_attr_max_state.attr,
1183 &dev_attr_cur_state.attr,
1184 NULL,
1185};
1186
1187static const struct attribute_group cooling_device_attr_group = {
1188 .attrs = cooling_device_attrs,
1189};
1190
1191static const struct attribute_group *cooling_device_attr_groups[] = {
1192 &cooling_device_attr_group,
1193 NULL,
1194};
1195
Javi Merinodb916512015-02-18 16:04:24 +00001196static ssize_t
1197thermal_cooling_device_weight_show(struct device *dev,
1198 struct device_attribute *attr, char *buf)
1199{
1200 struct thermal_instance *instance;
1201
1202 instance = container_of(attr, struct thermal_instance, weight_attr);
1203
1204 return sprintf(buf, "%d\n", instance->weight);
1205}
1206
1207static ssize_t
1208thermal_cooling_device_weight_store(struct device *dev,
1209 struct device_attribute *attr,
1210 const char *buf, size_t count)
1211{
1212 struct thermal_instance *instance;
1213 int ret, weight;
1214
1215 ret = kstrtoint(buf, 0, &weight);
1216 if (ret)
1217 return ret;
1218
1219 instance = container_of(attr, struct thermal_instance, weight_attr);
1220 instance->weight = weight;
1221
1222 return count;
1223}
Zhang Rui203d3d42008-01-17 15:51:08 +08001224/* Device management */
1225
1226/**
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001227 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
1228 * @tz: pointer to struct thermal_zone_device
Zhang Rui203d3d42008-01-17 15:51:08 +08001229 * @trip: indicates which trip point the cooling devices is
1230 * associated with in this thermal zone.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001231 * @cdev: pointer to struct thermal_cooling_device
1232 * @upper: the Maximum cooling state for this trip point.
1233 * THERMAL_NO_LIMIT means no upper limit,
1234 * and the cooling device can be in max_state.
1235 * @lower: the Minimum cooling state can be used for this trip point.
1236 * THERMAL_NO_LIMIT means no lower limit,
1237 * and the cooling device can be in cooling state 0.
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001238 * @weight: The weight of the cooling device to be bound to the
1239 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
1240 * default value
Len Brown543a9562008-02-07 16:55:08 -05001241 *
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001242 * This interface function bind a thermal cooling device to the certain trip
1243 * point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001244 * This function is usually called in the thermal zone device .bind callback.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001245 *
1246 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001247 */
1248int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1249 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +08001250 struct thermal_cooling_device *cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001251 unsigned long upper, unsigned long lower,
1252 unsigned int weight)
Zhang Rui203d3d42008-01-17 15:51:08 +08001253{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001254 struct thermal_instance *dev;
1255 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -05001256 struct thermal_zone_device *pos1;
1257 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +08001258 unsigned long max_state;
Lukasz Majewski9a3031d2014-11-18 11:16:30 +01001259 int result, ret;
Zhang Rui203d3d42008-01-17 15:51:08 +08001260
Len Brown543a9562008-02-07 16:55:08 -05001261 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +08001262 return -EINVAL;
1263
Thomas Sujithc7516702008-02-15 00:58:50 -05001264 list_for_each_entry(pos1, &thermal_tz_list, node) {
1265 if (pos1 == tz)
1266 break;
1267 }
1268 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1269 if (pos2 == cdev)
1270 break;
1271 }
1272
1273 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +08001274 return -EINVAL;
1275
Lukasz Majewski9a3031d2014-11-18 11:16:30 +01001276 ret = cdev->ops->get_max_state(cdev, &max_state);
1277 if (ret)
1278 return ret;
Zhang Rui9d998422012-06-26 16:35:57 +08001279
1280 /* lower default 0, upper default max_state */
1281 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1282 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1283
1284 if (lower > upper || upper > max_state)
1285 return -EINVAL;
1286
Zhang Rui203d3d42008-01-17 15:51:08 +08001287 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001288 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001289 if (!dev)
1290 return -ENOMEM;
1291 dev->tz = tz;
1292 dev->cdev = cdev;
1293 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +08001294 dev->upper = upper;
1295 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +08001296 dev->target = THERMAL_NO_TARGET;
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001297 dev->weight = weight;
Zhang Rui74051ba2012-06-26 16:27:22 +08001298
Zhang Rui203d3d42008-01-17 15:51:08 +08001299 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1300 if (result)
1301 goto free_mem;
1302
1303 sprintf(dev->name, "cdev%d", dev->id);
1304 result =
1305 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1306 if (result)
1307 goto release_idr;
1308
1309 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -07001310 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001311 dev->attr.attr.name = dev->attr_name;
1312 dev->attr.attr.mode = 0444;
1313 dev->attr.show = thermal_cooling_device_trip_point_show;
1314 result = device_create_file(&tz->device, &dev->attr);
1315 if (result)
1316 goto remove_symbol_link;
1317
Javi Merinodb916512015-02-18 16:04:24 +00001318 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
1319 sysfs_attr_init(&dev->weight_attr.attr);
1320 dev->weight_attr.attr.name = dev->weight_attr_name;
1321 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
1322 dev->weight_attr.show = thermal_cooling_device_weight_show;
1323 dev->weight_attr.store = thermal_cooling_device_weight_store;
1324 result = device_create_file(&tz->device, &dev->weight_attr);
1325 if (result)
1326 goto remove_trip_file;
1327
Zhang Rui203d3d42008-01-17 15:51:08 +08001328 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001329 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001330 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +08001331 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1332 result = -EEXIST;
1333 break;
1334 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001335 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001336 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001337 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1338 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001339 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001340 mutex_unlock(&tz->lock);
1341
1342 if (!result)
1343 return 0;
1344
Javi Merinodb916512015-02-18 16:04:24 +00001345 device_remove_file(&tz->device, &dev->weight_attr);
1346remove_trip_file:
Zhang Rui203d3d42008-01-17 15:51:08 +08001347 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -07001348remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +08001349 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -07001350release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +08001351 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -07001352free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +08001353 kfree(dev);
1354 return result;
1355}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001356EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001357
1358/**
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001359 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1360 * thermal zone.
1361 * @tz: pointer to a struct thermal_zone_device.
Zhang Rui203d3d42008-01-17 15:51:08 +08001362 * @trip: indicates which trip point the cooling devices is
1363 * associated with in this thermal zone.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001364 * @cdev: pointer to a struct thermal_cooling_device.
Len Brown543a9562008-02-07 16:55:08 -05001365 *
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001366 * This interface function unbind a thermal cooling device from the certain
1367 * trip point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001368 * This function is usually called in the thermal zone device .unbind callback.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001369 *
1370 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001371 */
1372int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1373 int trip,
1374 struct thermal_cooling_device *cdev)
1375{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001376 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +08001377
1378 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001379 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001380 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -05001381 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001382 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001383 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001384 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001385 mutex_unlock(&tz->lock);
1386 goto unbind;
1387 }
1388 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001389 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001390 mutex_unlock(&tz->lock);
1391
1392 return -ENODEV;
1393
Joe Perchescaca8b82012-03-21 12:55:02 -07001394unbind:
Viresh Kumar528464e2015-07-23 14:32:32 +05301395 device_remove_file(&tz->device, &pos->weight_attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001396 device_remove_file(&tz->device, &pos->attr);
1397 sysfs_remove_link(&tz->device.kobj, pos->name);
1398 release_idr(&tz->idr, &tz->lock, pos->id);
1399 kfree(pos);
1400 return 0;
1401}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001402EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001403
1404static void thermal_release(struct device *dev)
1405{
1406 struct thermal_zone_device *tz;
1407 struct thermal_cooling_device *cdev;
1408
Joe Perchescaca8b82012-03-21 12:55:02 -07001409 if (!strncmp(dev_name(dev), "thermal_zone",
1410 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001411 tz = to_thermal_zone(dev);
1412 kfree(tz);
durgadoss.r@intel.com732e4c82013-10-02 00:08:00 +05301413 } else if(!strncmp(dev_name(dev), "cooling_device",
1414 sizeof("cooling_device") - 1)){
Zhang Rui203d3d42008-01-17 15:51:08 +08001415 cdev = to_cooling_device(dev);
1416 kfree(cdev);
1417 }
1418}
1419
1420static struct class thermal_class = {
1421 .name = "thermal",
1422 .dev_release = thermal_release,
1423};
1424
1425/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001426 * __thermal_cooling_device_register() - register a new thermal cooling device
1427 * @np: a pointer to a device tree node.
Zhang Rui203d3d42008-01-17 15:51:08 +08001428 * @type: the thermal cooling device type.
1429 * @devdata: device private data.
1430 * @ops: standard thermal cooling devices callbacks.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001431 *
1432 * This interface function adds a new thermal cooling device (fan/processor/...)
1433 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1434 * to all the thermal zone devices registered at the same time.
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001435 * It also gives the opportunity to link the cooling device to a device tree
1436 * node, so that it can be bound to a thermal zone created out of device tree.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001437 *
1438 * Return: a pointer to the created struct thermal_cooling_device or an
1439 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001440 */
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001441static struct thermal_cooling_device *
1442__thermal_cooling_device_register(struct device_node *np,
1443 char *type, void *devdata,
1444 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001445{
1446 struct thermal_cooling_device *cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001447 int result;
1448
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001449 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001450 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001451
1452 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001453 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001454 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001455
1456 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1457 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001458 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001459
1460 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1461 if (result) {
1462 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001463 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001464 }
1465
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001466 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +08001467 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001468 INIT_LIST_HEAD(&cdev->thermal_instances);
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001469 cdev->np = np;
Zhang Rui203d3d42008-01-17 15:51:08 +08001470 cdev->ops = ops;
Ni Wade5ca0cce2014-02-17 11:02:55 +08001471 cdev->updated = false;
Zhang Rui203d3d42008-01-17 15:51:08 +08001472 cdev->device.class = &thermal_class;
Matthias Kaehlcke2dc10f82015-02-20 18:10:08 -08001473 cdev->device.groups = cooling_device_attr_groups;
Zhang Rui203d3d42008-01-17 15:51:08 +08001474 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -08001475 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001476 result = device_register(&cdev->device);
1477 if (result) {
1478 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1479 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001480 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001481 }
1482
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301483 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +08001484 mutex_lock(&thermal_list_lock);
1485 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001486 mutex_unlock(&thermal_list_lock);
1487
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301488 /* Update binding information for 'this' new cdev */
1489 bind_cdev(cdev);
1490
1491 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001492}
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001493
1494/**
1495 * thermal_cooling_device_register() - register a new thermal cooling device
1496 * @type: the thermal cooling device type.
1497 * @devdata: device private data.
1498 * @ops: standard thermal cooling devices callbacks.
1499 *
1500 * This interface function adds a new thermal cooling device (fan/processor/...)
1501 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1502 * to all the thermal zone devices registered at the same time.
1503 *
1504 * Return: a pointer to the created struct thermal_cooling_device or an
1505 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1506 */
1507struct thermal_cooling_device *
1508thermal_cooling_device_register(char *type, void *devdata,
1509 const struct thermal_cooling_device_ops *ops)
1510{
1511 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1512}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001513EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001514
1515/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001516 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1517 * @np: a pointer to a device tree node.
1518 * @type: the thermal cooling device type.
1519 * @devdata: device private data.
1520 * @ops: standard thermal cooling devices callbacks.
1521 *
1522 * This function will register a cooling device with device tree node reference.
1523 * This interface function adds a new thermal cooling device (fan/processor/...)
1524 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1525 * to all the thermal zone devices registered at the same time.
1526 *
1527 * Return: a pointer to the created struct thermal_cooling_device or an
1528 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1529 */
1530struct thermal_cooling_device *
1531thermal_of_cooling_device_register(struct device_node *np,
1532 char *type, void *devdata,
1533 const struct thermal_cooling_device_ops *ops)
1534{
1535 return __thermal_cooling_device_register(np, type, devdata, ops);
1536}
1537EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1538
1539/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001540 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001541 * @cdev: the thermal cooling device to remove.
1542 *
1543 * thermal_cooling_device_unregister() must be called when the device is no
1544 * longer needed.
1545 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301546void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001547{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301548 int i;
1549 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001550 struct thermal_zone_device *tz;
1551 struct thermal_cooling_device *pos = NULL;
1552
1553 if (!cdev)
1554 return;
1555
1556 mutex_lock(&thermal_list_lock);
1557 list_for_each_entry(pos, &thermal_cdev_list, node)
1558 if (pos == cdev)
1559 break;
1560 if (pos != cdev) {
1561 /* thermal cooling device not found */
1562 mutex_unlock(&thermal_list_lock);
1563 return;
1564 }
1565 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301566
1567 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001568 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301569 if (tz->ops->unbind) {
1570 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001571 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301572 }
1573
1574 if (!tz->tzp || !tz->tzp->tbp)
1575 continue;
1576
1577 tzp = tz->tzp;
1578 for (i = 0; i < tzp->num_tbps; i++) {
1579 if (tzp->tbp[i].cdev == cdev) {
1580 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1581 tzp->tbp[i].cdev = NULL;
1582 }
1583 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001584 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301585
Zhang Rui203d3d42008-01-17 15:51:08 +08001586 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301587
Zhang Rui203d3d42008-01-17 15:51:08 +08001588 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001589 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001590 device_remove_file(&cdev->device, &dev_attr_max_state);
1591 device_remove_file(&cdev->device, &dev_attr_cur_state);
1592
1593 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1594 device_unregister(&cdev->device);
1595 return;
1596}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001597EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001598
Durgadoss Rdc765482012-09-18 11:05:00 +05301599void thermal_cdev_update(struct thermal_cooling_device *cdev)
Zhang Ruice119f82012-06-27 14:13:04 +08001600{
1601 struct thermal_instance *instance;
1602 unsigned long target = 0;
1603
1604 /* cooling device is updated*/
1605 if (cdev->updated)
1606 return;
1607
Zhang Ruif4a821c2012-07-24 16:56:21 +08001608 mutex_lock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001609 /* Make sure cdev enters the deepest cooling state */
1610 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
Aaron Lu06475b52013-12-02 13:54:26 +08001611 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1612 instance->tz->id, instance->target);
Zhang Ruice119f82012-06-27 14:13:04 +08001613 if (instance->target == THERMAL_NO_TARGET)
1614 continue;
1615 if (instance->target > target)
1616 target = instance->target;
1617 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001618 mutex_unlock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001619 cdev->ops->set_cur_state(cdev, target);
1620 cdev->updated = true;
Punit Agrawal39811562014-07-29 11:50:49 +01001621 trace_cdev_update(cdev, target);
Aaron Lu06475b52013-12-02 13:54:26 +08001622 dev_dbg(&cdev->device, "set to state %lu\n", target);
Zhang Ruice119f82012-06-27 14:13:04 +08001623}
Durgadoss Rdc765482012-09-18 11:05:00 +05301624EXPORT_SYMBOL(thermal_cdev_update);
Zhang Ruice119f82012-06-27 14:13:04 +08001625
Matthew Garrettb1569e92008-12-03 17:55:32 +00001626/**
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001627 * thermal_notify_framework - Sensor drivers use this API to notify framework
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301628 * @tz: thermal zone device
1629 * @trip: indicates which trip point has been crossed
1630 *
1631 * This function handles the trip events from sensor drivers. It starts
1632 * throttling the cooling devices according to the policy configured.
1633 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1634 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1635 * The throttling policy is based on the configured platform data; if no
1636 * platform data is provided, this uses the step_wise throttling policy.
1637 */
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001638void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301639{
1640 handle_thermal_trip(tz, trip);
1641}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001642EXPORT_SYMBOL_GPL(thermal_notify_framework);
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301643
1644/**
Eduardo Valentin269c1742013-04-23 21:48:19 +00001645 * create_trip_attrs() - create attributes for trip points
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001646 * @tz: the thermal zone device
1647 * @mask: Writeable trip point bitmap.
Eduardo Valentin269c1742013-04-23 21:48:19 +00001648 *
1649 * helper function to instantiate sysfs entries for every trip
1650 * point and its properties of a struct thermal_zone_device.
1651 *
1652 * Return: 0 on success, the proper error value otherwise.
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001653 */
1654static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1655{
1656 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001657 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001658
Durgadoss R27365a62012-07-25 10:10:59 +08001659 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001660 if (!tz->trip_type_attrs)
1661 return -ENOMEM;
1662
Durgadoss R27365a62012-07-25 10:10:59 +08001663 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001664 if (!tz->trip_temp_attrs) {
1665 kfree(tz->trip_type_attrs);
1666 return -ENOMEM;
1667 }
1668
Durgadoss R27365a62012-07-25 10:10:59 +08001669 if (tz->ops->get_trip_hyst) {
1670 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1671 if (!tz->trip_hyst_attrs) {
1672 kfree(tz->trip_type_attrs);
1673 kfree(tz->trip_temp_attrs);
1674 return -ENOMEM;
1675 }
1676 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001677
Durgadoss R27365a62012-07-25 10:10:59 +08001678
1679 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001680 /* create trip type attribute */
1681 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1682 "trip_point_%d_type", indx);
1683
1684 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1685 tz->trip_type_attrs[indx].attr.attr.name =
1686 tz->trip_type_attrs[indx].name;
1687 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1688 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1689
1690 device_create_file(&tz->device,
1691 &tz->trip_type_attrs[indx].attr);
1692
1693 /* create trip temp attribute */
1694 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1695 "trip_point_%d_temp", indx);
1696
1697 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1698 tz->trip_temp_attrs[indx].attr.attr.name =
1699 tz->trip_temp_attrs[indx].name;
1700 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1701 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
Punit Agrawal35e94642015-03-03 10:43:03 +00001702 if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS) &&
1703 mask & (1 << indx)) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001704 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1705 tz->trip_temp_attrs[indx].attr.store =
1706 trip_point_temp_store;
1707 }
1708
1709 device_create_file(&tz->device,
1710 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001711
1712 /* create Optional trip hyst attribute */
1713 if (!tz->ops->get_trip_hyst)
1714 continue;
1715 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1716 "trip_point_%d_hyst", indx);
1717
1718 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1719 tz->trip_hyst_attrs[indx].attr.attr.name =
1720 tz->trip_hyst_attrs[indx].name;
1721 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1722 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1723 if (tz->ops->set_trip_hyst) {
1724 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1725 tz->trip_hyst_attrs[indx].attr.store =
1726 trip_point_hyst_store;
1727 }
1728
1729 device_create_file(&tz->device,
1730 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001731 }
1732 return 0;
1733}
1734
1735static void remove_trip_attrs(struct thermal_zone_device *tz)
1736{
1737 int indx;
1738
1739 for (indx = 0; indx < tz->trips; indx++) {
1740 device_remove_file(&tz->device,
1741 &tz->trip_type_attrs[indx].attr);
1742 device_remove_file(&tz->device,
1743 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001744 if (tz->ops->get_trip_hyst)
1745 device_remove_file(&tz->device,
1746 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001747 }
1748 kfree(tz->trip_type_attrs);
1749 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001750 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001751}
1752
1753/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001754 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001755 * @type: the thermal zone device type
1756 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001757 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001758 * @devdata: private device data
1759 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301760 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001761 * @passive_delay: number of milliseconds to wait between polls when
1762 * performing passive cooling
1763 * @polling_delay: number of milliseconds to wait between polls when checking
1764 * whether trip points have been crossed (0 for interrupt
1765 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001766 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001767 * This interface function adds a new thermal zone device (sensor) to
1768 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1769 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08001770 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001771 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001772 *
1773 * Return: a pointer to the created struct thermal_zone_device or an
1774 * in case of error, an ERR_PTR. Caller must check return value with
1775 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001776 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001777struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001778 int trips, int mask, void *devdata,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001779 struct thermal_zone_device_ops *ops,
Javi Merino6b775e82015-03-02 17:17:19 +00001780 struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001781 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001782{
1783 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001784 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001785 int result;
1786 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001787 int passive = 0;
Javi Merinoe33df1d2015-02-26 19:00:27 +00001788 struct thermal_governor *governor;
Zhang Rui203d3d42008-01-17 15:51:08 +08001789
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001790 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001791 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001792
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001793 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001794 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001795
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04001796 if (!ops)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001797 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001798
Jonghwa Lee83720d02013-05-18 09:50:26 +00001799 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00001800 return ERR_PTR(-EINVAL);
1801
Zhang Rui203d3d42008-01-17 15:51:08 +08001802 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1803 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001804 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001805
Zhang Rui2d374132012-06-27 10:09:00 +08001806 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001807 idr_init(&tz->idr);
1808 mutex_init(&tz->lock);
1809 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1810 if (result) {
1811 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001812 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001813 }
1814
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001815 strlcpy(tz->type, type ? : "", sizeof(tz->type));
Zhang Rui203d3d42008-01-17 15:51:08 +08001816 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301817 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001818 tz->device.class = &thermal_class;
1819 tz->devdata = devdata;
1820 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001821 tz->passive_delay = passive_delay;
1822 tz->polling_delay = polling_delay;
1823
Kay Sievers354655e2009-01-06 10:44:37 -08001824 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001825 result = device_register(&tz->device);
1826 if (result) {
1827 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1828 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001829 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001830 }
1831
1832 /* sys I/F */
1833 if (type) {
1834 result = device_create_file(&tz->device, &dev_attr_type);
1835 if (result)
1836 goto unregister;
1837 }
1838
1839 result = device_create_file(&tz->device, &dev_attr_temp);
1840 if (result)
1841 goto unregister;
1842
1843 if (ops->get_mode) {
1844 result = device_create_file(&tz->device, &dev_attr_mode);
1845 if (result)
1846 goto unregister;
1847 }
1848
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001849 result = create_trip_attrs(tz, mask);
1850 if (result)
1851 goto unregister;
1852
Zhang Rui203d3d42008-01-17 15:51:08 +08001853 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001854 tz->ops->get_trip_type(tz, count, &trip_type);
1855 if (trip_type == THERMAL_TRIP_PASSIVE)
1856 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001857 }
1858
Durgadoss R5a2c0902012-09-18 11:04:58 +05301859 if (!passive) {
1860 result = device_create_file(&tz->device, &dev_attr_passive);
1861 if (result)
1862 goto unregister;
1863 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001864
Sascha Hauer79e54212015-07-06 09:46:16 +02001865 if (IS_ENABLED(CONFIG_THERMAL_EMULATION)) {
1866 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1867 if (result)
1868 goto unregister;
1869 }
1870
Durgadoss R5a2c0902012-09-18 11:04:58 +05301871 /* Create policy attribute */
1872 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00001873 if (result)
1874 goto unregister;
1875
Javi Merino9f382712015-03-26 15:53:02 +00001876 /* Add thermal zone params */
1877 result = create_tzp_attrs(&tz->device);
1878 if (result)
1879 goto unregister;
1880
Ni Wade25a0a5c2015-07-14 15:40:56 +08001881 /* Create available_policies attribute */
1882 result = device_create_file(&tz->device, &dev_attr_available_policies);
1883 if (result)
1884 goto unregister;
1885
Durgadoss Ra4a15482012-09-18 11:04:57 +05301886 /* Update 'this' zone's governor information */
1887 mutex_lock(&thermal_governor_lock);
1888
1889 if (tz->tzp)
Javi Merinoe33df1d2015-02-26 19:00:27 +00001890 governor = __find_governor(tz->tzp->governor_name);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301891 else
Javi Merinoe33df1d2015-02-26 19:00:27 +00001892 governor = def_governor;
1893
1894 result = thermal_set_governor(tz, governor);
1895 if (result) {
1896 mutex_unlock(&thermal_governor_lock);
1897 goto unregister;
1898 }
Durgadoss Ra4a15482012-09-18 11:04:57 +05301899
1900 mutex_unlock(&thermal_governor_lock);
1901
Eduardo Valentinccba4ff2013-08-15 11:34:17 -04001902 if (!tz->tzp || !tz->tzp->no_hwmon) {
1903 result = thermal_add_hwmon_sysfs(tz);
1904 if (result)
1905 goto unregister;
1906 }
Zhang Ruie68b16a2008-04-21 16:07:52 +08001907
Zhang Rui203d3d42008-01-17 15:51:08 +08001908 mutex_lock(&thermal_list_lock);
1909 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001910 mutex_unlock(&thermal_list_lock);
1911
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301912 /* Bind cooling devices for this zone */
1913 bind_tz(tz);
1914
Matthew Garrettb1569e92008-12-03 17:55:32 +00001915 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1916
Zhang Ruibb431ba2015-10-30 16:31:47 +08001917 thermal_zone_device_reset(tz);
Matthew Garrettb1569e92008-12-03 17:55:32 +00001918 thermal_zone_device_update(tz);
1919
Yao Dongdong14015862014-10-28 07:40:25 +00001920 return tz;
Zhang Rui203d3d42008-01-17 15:51:08 +08001921
Joe Perchescaca8b82012-03-21 12:55:02 -07001922unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001923 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1924 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001925 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001926}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001927EXPORT_SYMBOL_GPL(thermal_zone_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001928
1929/**
1930 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001931 * @tz: the thermal zone device to remove
1932 */
1933void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1934{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301935 int i;
1936 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001937 struct thermal_cooling_device *cdev;
1938 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001939
1940 if (!tz)
1941 return;
1942
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301943 tzp = tz->tzp;
1944
Zhang Rui203d3d42008-01-17 15:51:08 +08001945 mutex_lock(&thermal_list_lock);
1946 list_for_each_entry(pos, &thermal_tz_list, node)
1947 if (pos == tz)
1948 break;
1949 if (pos != tz) {
1950 /* thermal zone device not found */
1951 mutex_unlock(&thermal_list_lock);
1952 return;
1953 }
1954 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301955
1956 /* Unbind all cdevs associated with 'this' thermal zone */
1957 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1958 if (tz->ops->unbind) {
1959 tz->ops->unbind(tz, cdev);
1960 continue;
1961 }
1962
1963 if (!tzp || !tzp->tbp)
1964 break;
1965
1966 for (i = 0; i < tzp->num_tbps; i++) {
1967 if (tzp->tbp[i].cdev == cdev) {
1968 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1969 tzp->tbp[i].cdev = NULL;
1970 }
1971 }
1972 }
1973
Zhang Rui203d3d42008-01-17 15:51:08 +08001974 mutex_unlock(&thermal_list_lock);
1975
Matthew Garrettb1569e92008-12-03 17:55:32 +00001976 thermal_zone_device_set_polling(tz, 0);
1977
Zhang Rui203d3d42008-01-17 15:51:08 +08001978 if (tz->type[0])
1979 device_remove_file(&tz->device, &dev_attr_type);
1980 device_remove_file(&tz->device, &dev_attr_temp);
1981 if (tz->ops->get_mode)
1982 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301983 device_remove_file(&tz->device, &dev_attr_policy);
Ni Wade25a0a5c2015-07-14 15:40:56 +08001984 device_remove_file(&tz->device, &dev_attr_available_policies);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001985 remove_trip_attrs(tz);
Javi Merinoe33df1d2015-02-26 19:00:27 +00001986 thermal_set_governor(tz, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001987
Zhang Ruie68b16a2008-04-21 16:07:52 +08001988 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001989 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1990 idr_destroy(&tz->idr);
1991 mutex_destroy(&tz->lock);
1992 device_unregister(&tz->device);
1993 return;
1994}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001995EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001996
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001997/**
1998 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1999 * @name: thermal zone name to fetch the temperature
2000 *
2001 * When only one zone is found with the passed name, returns a reference to it.
2002 *
2003 * Return: On success returns a reference to an unique thermal zone with
2004 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
2005 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
2006 */
2007struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
2008{
2009 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
2010 unsigned int found = 0;
2011
2012 if (!name)
2013 goto exit;
2014
2015 mutex_lock(&thermal_list_lock);
2016 list_for_each_entry(pos, &thermal_tz_list, node)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -07002017 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
Eduardo Valentin63c4d912013-04-05 12:32:28 +00002018 found++;
2019 ref = pos;
2020 }
2021 mutex_unlock(&thermal_list_lock);
2022
2023 /* nothing has been found, thus an error code for it */
2024 if (found == 0)
2025 ref = ERR_PTR(-ENODEV);
2026 else if (found > 1)
2027 /* Success only when an unique zone is found */
2028 ref = ERR_PTR(-EEXIST);
2029
2030exit:
2031 return ref;
2032}
2033EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
2034
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002035#ifdef CONFIG_NET
Johannes Berg2a94fe42013-11-19 15:19:39 +01002036static const struct genl_multicast_group thermal_event_mcgrps[] = {
2037 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
2038};
2039
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002040static struct genl_family thermal_event_genl_family = {
2041 .id = GENL_ID_GENERATE,
2042 .name = THERMAL_GENL_FAMILY_NAME,
2043 .version = THERMAL_GENL_VERSION,
2044 .maxattr = THERMAL_GENL_ATTR_MAX,
Johannes Berg2a94fe42013-11-19 15:19:39 +01002045 .mcgrps = thermal_event_mcgrps,
2046 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002047};
2048
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00002049int thermal_generate_netlink_event(struct thermal_zone_device *tz,
2050 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05302051{
2052 struct sk_buff *skb;
2053 struct nlattr *attr;
2054 struct thermal_genl_event *thermal_event;
2055 void *msg_header;
2056 int size;
2057 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07002058 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05302059
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00002060 if (!tz)
2061 return -EINVAL;
2062
R.Durgadoss4cb18722010-10-27 03:33:29 +05302063 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07002064 size = nla_total_size(sizeof(struct thermal_genl_event)) +
2065 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302066
2067 skb = genlmsg_new(size, GFP_ATOMIC);
2068 if (!skb)
2069 return -ENOMEM;
2070
2071 /* add the genetlink message header */
2072 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
2073 &thermal_event_genl_family, 0,
2074 THERMAL_GENL_CMD_EVENT);
2075 if (!msg_header) {
2076 nlmsg_free(skb);
2077 return -ENOMEM;
2078 }
2079
2080 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07002081 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
2082 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05302083
2084 if (!attr) {
2085 nlmsg_free(skb);
2086 return -EINVAL;
2087 }
2088
2089 thermal_event = nla_data(attr);
2090 if (!thermal_event) {
2091 nlmsg_free(skb);
2092 return -EINVAL;
2093 }
2094
2095 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
2096
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00002097 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05302098 thermal_event->event = event;
2099
2100 /* send multicast genetlink message */
Johannes Berg053c0952015-01-16 22:09:00 +01002101 genlmsg_end(skb, msg_header);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302102
Johannes Berg68eb5502013-11-19 15:19:38 +01002103 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01002104 0, GFP_ATOMIC);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302105 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00002106 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302107
2108 return result;
2109}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00002110EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302111
2112static int genetlink_init(void)
2113{
Johannes Berg2a94fe42013-11-19 15:19:39 +01002114 return genl_register_family(&thermal_event_genl_family);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302115}
2116
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002117static void genetlink_exit(void)
2118{
2119 genl_unregister_family(&thermal_event_genl_family);
2120}
2121#else /* !CONFIG_NET */
2122static inline int genetlink_init(void) { return 0; }
2123static inline void genetlink_exit(void) {}
2124#endif /* !CONFIG_NET */
2125
Zhang Rui80a26a52013-03-26 16:38:29 +08002126static int __init thermal_register_governors(void)
2127{
2128 int result;
2129
2130 result = thermal_gov_step_wise_register();
2131 if (result)
2132 return result;
2133
2134 result = thermal_gov_fair_share_register();
2135 if (result)
2136 return result;
2137
Peter Feuerere4dbf982014-07-22 17:37:13 +02002138 result = thermal_gov_bang_bang_register();
2139 if (result)
2140 return result;
2141
Javi Merino6b775e82015-03-02 17:17:19 +00002142 result = thermal_gov_user_space_register();
2143 if (result)
2144 return result;
2145
2146 return thermal_gov_power_allocator_register();
Zhang Rui80a26a52013-03-26 16:38:29 +08002147}
2148
2149static void thermal_unregister_governors(void)
2150{
2151 thermal_gov_step_wise_unregister();
2152 thermal_gov_fair_share_unregister();
Peter Feuerere4dbf982014-07-22 17:37:13 +02002153 thermal_gov_bang_bang_unregister();
Zhang Rui80a26a52013-03-26 16:38:29 +08002154 thermal_gov_user_space_unregister();
Javi Merino6b775e82015-03-02 17:17:19 +00002155 thermal_gov_power_allocator_unregister();
Zhang Rui80a26a52013-03-26 16:38:29 +08002156}
2157
Zhang Rui203d3d42008-01-17 15:51:08 +08002158static int __init thermal_init(void)
2159{
Zhang Rui80a26a52013-03-26 16:38:29 +08002160 int result;
2161
2162 result = thermal_register_governors();
2163 if (result)
2164 goto error;
Zhang Rui203d3d42008-01-17 15:51:08 +08002165
2166 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08002167 if (result)
2168 goto unregister_governors;
2169
R.Durgadoss4cb18722010-10-27 03:33:29 +05302170 result = genetlink_init();
Zhang Rui80a26a52013-03-26 16:38:29 +08002171 if (result)
2172 goto unregister_class;
2173
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002174 result = of_parse_thermal_zones();
2175 if (result)
2176 goto exit_netlink;
2177
Zhang Rui80a26a52013-03-26 16:38:29 +08002178 return 0;
2179
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002180exit_netlink:
2181 genetlink_exit();
Zhang Rui80a26a52013-03-26 16:38:29 +08002182unregister_class:
2183 class_unregister(&thermal_class);
Luis Henriques9d367e52014-12-03 21:20:21 +00002184unregister_governors:
2185 thermal_unregister_governors();
Zhang Rui80a26a52013-03-26 16:38:29 +08002186error:
2187 idr_destroy(&thermal_tz_idr);
2188 idr_destroy(&thermal_cdev_idr);
2189 mutex_destroy(&thermal_idr_lock);
2190 mutex_destroy(&thermal_list_lock);
2191 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08002192 return result;
2193}
2194
2195static void __exit thermal_exit(void)
2196{
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002197 of_thermal_destroy_zones();
Zhang Rui80a26a52013-03-26 16:38:29 +08002198 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08002199 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08002200 thermal_unregister_governors();
Zhang Rui203d3d42008-01-17 15:51:08 +08002201 idr_destroy(&thermal_tz_idr);
2202 idr_destroy(&thermal_cdev_idr);
2203 mutex_destroy(&thermal_idr_lock);
2204 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08002205 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08002206}
2207
R.Durgadoss4cb18722010-10-27 03:33:29 +05302208fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08002209module_exit(thermal_exit);