blob: 768fb10eb962ddece9790cd79fd750db7477b974 [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 Dickins791700c2012-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 Dickins791700c2012-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);
Aaron Lu06475b52013-12-02 13:54:26 +0800535 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
536 tz->last_temperature, tz->temperature);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530537}
538
539void thermal_zone_device_update(struct thermal_zone_device *tz)
540{
541 int count;
542
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400543 if (!tz->ops->get_temp)
544 return;
545
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530546 update_temperature(tz);
547
548 for (count = 0; count < tz->trips; count++)
549 handle_thermal_trip(tz, count);
550}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000551EXPORT_SYMBOL_GPL(thermal_zone_device_update);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530552
553static void thermal_zone_device_check(struct work_struct *work)
554{
555 struct thermal_zone_device *tz = container_of(work, struct
556 thermal_zone_device,
557 poll_queue.work);
558 thermal_zone_device_update(tz);
559}
560
Zhang Rui203d3d42008-01-17 15:51:08 +0800561/* sys I/F for thermal zone */
562
563#define to_thermal_zone(_dev) \
564 container_of(_dev, struct thermal_zone_device, device)
565
566static ssize_t
567type_show(struct device *dev, struct device_attribute *attr, char *buf)
568{
569 struct thermal_zone_device *tz = to_thermal_zone(dev);
570
571 return sprintf(buf, "%s\n", tz->type);
572}
573
574static ssize_t
575temp_show(struct device *dev, struct device_attribute *attr, char *buf)
576{
577 struct thermal_zone_device *tz = to_thermal_zone(dev);
Sascha Hauer17e83512015-07-24 08:12:54 +0200578 int temperature, ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800579
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000580 ret = thermal_zone_get_temp(tz, &temperature);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000581
582 if (ret)
583 return ret;
584
Sascha Hauer17e83512015-07-24 08:12:54 +0200585 return sprintf(buf, "%d\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800586}
587
588static ssize_t
589mode_show(struct device *dev, struct device_attribute *attr, char *buf)
590{
591 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000592 enum thermal_device_mode mode;
593 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800594
595 if (!tz->ops->get_mode)
596 return -EPERM;
597
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000598 result = tz->ops->get_mode(tz, &mode);
599 if (result)
600 return result;
601
602 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
603 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800604}
605
606static ssize_t
607mode_store(struct device *dev, struct device_attribute *attr,
608 const char *buf, size_t count)
609{
610 struct thermal_zone_device *tz = to_thermal_zone(dev);
611 int result;
612
613 if (!tz->ops->set_mode)
614 return -EPERM;
615
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530616 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000617 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530618 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000619 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
620 else
621 result = -EINVAL;
622
Zhang Rui203d3d42008-01-17 15:51:08 +0800623 if (result)
624 return result;
625
626 return count;
627}
628
629static ssize_t
630trip_point_type_show(struct device *dev, struct device_attribute *attr,
631 char *buf)
632{
633 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000634 enum thermal_trip_type type;
635 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800636
637 if (!tz->ops->get_trip_type)
638 return -EPERM;
639
640 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
641 return -EINVAL;
642
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000643 result = tz->ops->get_trip_type(tz, trip, &type);
644 if (result)
645 return result;
646
647 switch (type) {
648 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300649 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000650 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300651 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000652 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300653 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000654 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300655 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000656 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300657 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000658 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800659}
660
661static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800662trip_point_temp_store(struct device *dev, struct device_attribute *attr,
663 const char *buf, size_t count)
664{
665 struct thermal_zone_device *tz = to_thermal_zone(dev);
666 int trip, ret;
667 unsigned long temperature;
668
669 if (!tz->ops->set_trip_temp)
670 return -EPERM;
671
672 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
673 return -EINVAL;
674
675 if (kstrtoul(buf, 10, &temperature))
676 return -EINVAL;
677
678 ret = tz->ops->set_trip_temp(tz, trip, temperature);
Kuninori Morimotoad74e462015-12-15 01:19:53 +0000679 if (ret)
680 return ret;
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800681
Kuninori Morimotoad74e462015-12-15 01:19:53 +0000682 thermal_zone_device_update(tz);
683
684 return count;
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800685}
686
687static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800688trip_point_temp_show(struct device *dev, struct device_attribute *attr,
689 char *buf)
690{
691 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000692 int trip, ret;
Sascha Hauer17e83512015-07-24 08:12:54 +0200693 int temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800694
695 if (!tz->ops->get_trip_temp)
696 return -EPERM;
697
698 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
699 return -EINVAL;
700
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000701 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
702
703 if (ret)
704 return ret;
705
Sascha Hauer17e83512015-07-24 08:12:54 +0200706 return sprintf(buf, "%d\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800707}
708
Matthew Garrett03a971a2008-12-03 18:00:38 +0000709static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800710trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
711 const char *buf, size_t count)
712{
713 struct thermal_zone_device *tz = to_thermal_zone(dev);
714 int trip, ret;
Sascha Hauer17e83512015-07-24 08:12:54 +0200715 int temperature;
Durgadoss R27365a62012-07-25 10:10:59 +0800716
717 if (!tz->ops->set_trip_hyst)
718 return -EPERM;
719
720 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
721 return -EINVAL;
722
Sascha Hauer17e83512015-07-24 08:12:54 +0200723 if (kstrtoint(buf, 10, &temperature))
Durgadoss R27365a62012-07-25 10:10:59 +0800724 return -EINVAL;
725
726 /*
727 * We are not doing any check on the 'temperature' value
728 * here. The driver implementing 'set_trip_hyst' has to
729 * take care of this.
730 */
731 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
732
733 return ret ? ret : count;
734}
735
736static ssize_t
737trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
738 char *buf)
739{
740 struct thermal_zone_device *tz = to_thermal_zone(dev);
741 int trip, ret;
Sascha Hauer17e83512015-07-24 08:12:54 +0200742 int temperature;
Durgadoss R27365a62012-07-25 10:10:59 +0800743
744 if (!tz->ops->get_trip_hyst)
745 return -EPERM;
746
747 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
748 return -EINVAL;
749
750 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
751
Sascha Hauer17e83512015-07-24 08:12:54 +0200752 return ret ? ret : sprintf(buf, "%d\n", temperature);
Durgadoss R27365a62012-07-25 10:10:59 +0800753}
754
755static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000756passive_store(struct device *dev, struct device_attribute *attr,
757 const char *buf, size_t count)
758{
759 struct thermal_zone_device *tz = to_thermal_zone(dev);
760 struct thermal_cooling_device *cdev = NULL;
761 int state;
762
763 if (!sscanf(buf, "%d\n", &state))
764 return -EINVAL;
765
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100766 /* sanity check: values below 1000 millicelcius don't make sense
767 * and can cause the system to go into a thermal heart attack
768 */
769 if (state && state < 1000)
770 return -EINVAL;
771
Matthew Garrett03a971a2008-12-03 18:00:38 +0000772 if (state && !tz->forced_passive) {
773 mutex_lock(&thermal_list_lock);
774 list_for_each_entry(cdev, &thermal_cdev_list, node) {
775 if (!strncmp("Processor", cdev->type,
776 sizeof("Processor")))
777 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800778 THERMAL_TRIPS_NONE, cdev,
779 THERMAL_NO_LIMIT,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000780 THERMAL_NO_LIMIT,
781 THERMAL_WEIGHT_DEFAULT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000782 }
783 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100784 if (!tz->passive_delay)
785 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000786 } else if (!state && tz->forced_passive) {
787 mutex_lock(&thermal_list_lock);
788 list_for_each_entry(cdev, &thermal_cdev_list, node) {
789 if (!strncmp("Processor", cdev->type,
790 sizeof("Processor")))
791 thermal_zone_unbind_cooling_device(tz,
792 THERMAL_TRIPS_NONE,
793 cdev);
794 }
795 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100796 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000797 }
798
Matthew Garrett03a971a2008-12-03 18:00:38 +0000799 tz->forced_passive = state;
800
801 thermal_zone_device_update(tz);
802
803 return count;
804}
805
806static ssize_t
807passive_show(struct device *dev, struct device_attribute *attr,
808 char *buf)
809{
810 struct thermal_zone_device *tz = to_thermal_zone(dev);
811
812 return sprintf(buf, "%d\n", tz->forced_passive);
813}
814
Durgadoss R5a2c0902012-09-18 11:04:58 +0530815static ssize_t
816policy_store(struct device *dev, struct device_attribute *attr,
817 const char *buf, size_t count)
818{
819 int ret = -EINVAL;
820 struct thermal_zone_device *tz = to_thermal_zone(dev);
821 struct thermal_governor *gov;
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000822 char name[THERMAL_NAME_LENGTH];
823
824 snprintf(name, sizeof(name), "%s", buf);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530825
826 mutex_lock(&thermal_governor_lock);
Javi Merinob6cc7722014-11-25 16:00:33 +0000827 mutex_lock(&tz->lock);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530828
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000829 gov = __find_governor(strim(name));
Durgadoss R5a2c0902012-09-18 11:04:58 +0530830 if (!gov)
831 goto exit;
832
Javi Merinoe33df1d2015-02-26 19:00:27 +0000833 ret = thermal_set_governor(tz, gov);
834 if (!ret)
835 ret = count;
Durgadoss R5a2c0902012-09-18 11:04:58 +0530836
837exit:
Javi Merinob6cc7722014-11-25 16:00:33 +0000838 mutex_unlock(&tz->lock);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530839 mutex_unlock(&thermal_governor_lock);
840 return ret;
841}
842
843static ssize_t
844policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
845{
846 struct thermal_zone_device *tz = to_thermal_zone(dev);
847
848 return sprintf(buf, "%s\n", tz->governor->name);
849}
850
Ni Wade25a0a5c2015-07-14 15:40:56 +0800851static ssize_t
852available_policies_show(struct device *dev, struct device_attribute *devattr,
853 char *buf)
854{
855 struct thermal_governor *pos;
856 ssize_t count = 0;
857 ssize_t size = PAGE_SIZE;
858
859 mutex_lock(&thermal_governor_lock);
860
861 list_for_each_entry(pos, &thermal_governor_list, governor_list) {
862 size = PAGE_SIZE - count;
863 count += scnprintf(buf + count, size, "%s ", pos->name);
864 }
865 count += scnprintf(buf + count, size, "\n");
866
867 mutex_unlock(&thermal_governor_lock);
868
869 return count;
870}
871
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000872static ssize_t
873emul_temp_store(struct device *dev, struct device_attribute *attr,
874 const char *buf, size_t count)
875{
876 struct thermal_zone_device *tz = to_thermal_zone(dev);
877 int ret = 0;
878 unsigned long temperature;
879
880 if (kstrtoul(buf, 10, &temperature))
881 return -EINVAL;
882
883 if (!tz->ops->set_emul_temp) {
884 mutex_lock(&tz->lock);
885 tz->emul_temperature = temperature;
886 mutex_unlock(&tz->lock);
887 } else {
888 ret = tz->ops->set_emul_temp(tz, temperature);
889 }
890
lan,Tianyu800744b2014-01-02 15:47:54 +0800891 if (!ret)
892 thermal_zone_device_update(tz);
893
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000894 return ret ? ret : count;
895}
896static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000897
Javi Merino9f382712015-03-26 15:53:02 +0000898static ssize_t
899sustainable_power_show(struct device *dev, struct device_attribute *devattr,
900 char *buf)
901{
902 struct thermal_zone_device *tz = to_thermal_zone(dev);
903
904 if (tz->tzp)
905 return sprintf(buf, "%u\n", tz->tzp->sustainable_power);
906 else
907 return -EIO;
908}
909
910static ssize_t
911sustainable_power_store(struct device *dev, struct device_attribute *devattr,
912 const char *buf, size_t count)
913{
914 struct thermal_zone_device *tz = to_thermal_zone(dev);
915 u32 sustainable_power;
916
917 if (!tz->tzp)
918 return -EIO;
919
920 if (kstrtou32(buf, 10, &sustainable_power))
921 return -EINVAL;
922
923 tz->tzp->sustainable_power = sustainable_power;
924
925 return count;
926}
927static DEVICE_ATTR(sustainable_power, S_IWUSR | S_IRUGO, sustainable_power_show,
928 sustainable_power_store);
929
930#define create_s32_tzp_attr(name) \
931 static ssize_t \
932 name##_show(struct device *dev, struct device_attribute *devattr, \
933 char *buf) \
934 { \
935 struct thermal_zone_device *tz = to_thermal_zone(dev); \
936 \
937 if (tz->tzp) \
938 return sprintf(buf, "%u\n", tz->tzp->name); \
939 else \
940 return -EIO; \
941 } \
942 \
943 static ssize_t \
944 name##_store(struct device *dev, struct device_attribute *devattr, \
945 const char *buf, size_t count) \
946 { \
947 struct thermal_zone_device *tz = to_thermal_zone(dev); \
948 s32 value; \
949 \
950 if (!tz->tzp) \
951 return -EIO; \
952 \
953 if (kstrtos32(buf, 10, &value)) \
954 return -EINVAL; \
955 \
956 tz->tzp->name = value; \
957 \
958 return count; \
959 } \
960 static DEVICE_ATTR(name, S_IWUSR | S_IRUGO, name##_show, name##_store)
961
962create_s32_tzp_attr(k_po);
963create_s32_tzp_attr(k_pu);
964create_s32_tzp_attr(k_i);
965create_s32_tzp_attr(k_d);
966create_s32_tzp_attr(integral_cutoff);
Eduardo Valentin9d0be7f2015-05-11 19:34:23 -0700967create_s32_tzp_attr(slope);
968create_s32_tzp_attr(offset);
Javi Merino9f382712015-03-26 15:53:02 +0000969#undef create_s32_tzp_attr
970
971static struct device_attribute *dev_tzp_attrs[] = {
972 &dev_attr_sustainable_power,
973 &dev_attr_k_po,
974 &dev_attr_k_pu,
975 &dev_attr_k_i,
976 &dev_attr_k_d,
977 &dev_attr_integral_cutoff,
Eduardo Valentin9d0be7f2015-05-11 19:34:23 -0700978 &dev_attr_slope,
979 &dev_attr_offset,
Javi Merino9f382712015-03-26 15:53:02 +0000980};
981
982static int create_tzp_attrs(struct device *dev)
983{
984 int i;
985
986 for (i = 0; i < ARRAY_SIZE(dev_tzp_attrs); i++) {
987 int ret;
988 struct device_attribute *dev_attr = dev_tzp_attrs[i];
989
990 ret = device_create_file(dev, dev_attr);
991 if (ret)
992 return ret;
993 }
994
995 return 0;
996}
997
Javi Merino35b11d22015-02-26 19:00:28 +0000998/**
999 * power_actor_get_max_power() - get the maximum power that a cdev can consume
1000 * @cdev: pointer to &thermal_cooling_device
1001 * @tz: a valid thermal zone device pointer
1002 * @max_power: pointer in which to store the maximum power
1003 *
1004 * Calculate the maximum power consumption in milliwats that the
1005 * cooling device can currently consume and store it in @max_power.
1006 *
1007 * Return: 0 on success, -EINVAL if @cdev doesn't support the
1008 * power_actor API or -E* on other error.
1009 */
1010int power_actor_get_max_power(struct thermal_cooling_device *cdev,
1011 struct thermal_zone_device *tz, u32 *max_power)
1012{
1013 if (!cdev_is_power_actor(cdev))
1014 return -EINVAL;
1015
1016 return cdev->ops->state2power(cdev, tz, 0, max_power);
1017}
1018
1019/**
Javi Merinoc973c3b2015-09-14 14:23:50 +01001020 * power_actor_get_min_power() - get the mainimum power that a cdev can consume
1021 * @cdev: pointer to &thermal_cooling_device
1022 * @tz: a valid thermal zone device pointer
1023 * @min_power: pointer in which to store the minimum power
1024 *
1025 * Calculate the minimum power consumption in milliwatts that the
1026 * cooling device can currently consume and store it in @min_power.
1027 *
1028 * Return: 0 on success, -EINVAL if @cdev doesn't support the
1029 * power_actor API or -E* on other error.
1030 */
1031int power_actor_get_min_power(struct thermal_cooling_device *cdev,
1032 struct thermal_zone_device *tz, u32 *min_power)
1033{
1034 unsigned long max_state;
1035 int ret;
1036
1037 if (!cdev_is_power_actor(cdev))
1038 return -EINVAL;
1039
1040 ret = cdev->ops->get_max_state(cdev, &max_state);
1041 if (ret)
1042 return ret;
1043
1044 return cdev->ops->state2power(cdev, tz, max_state, min_power);
1045}
1046
1047/**
Javi Merino35b11d22015-02-26 19:00:28 +00001048 * power_actor_set_power() - limit the maximum power that a cooling device can consume
1049 * @cdev: pointer to &thermal_cooling_device
1050 * @instance: thermal instance to update
1051 * @power: the power in milliwatts
1052 *
1053 * Set the cooling device to consume at most @power milliwatts.
1054 *
1055 * Return: 0 on success, -EINVAL if the cooling device does not
1056 * implement the power actor API or -E* for other failures.
1057 */
1058int power_actor_set_power(struct thermal_cooling_device *cdev,
1059 struct thermal_instance *instance, u32 power)
1060{
1061 unsigned long state;
1062 int ret;
1063
1064 if (!cdev_is_power_actor(cdev))
1065 return -EINVAL;
1066
1067 ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
1068 if (ret)
1069 return ret;
1070
1071 instance->target = state;
1072 cdev->updated = false;
1073 thermal_cdev_update(cdev);
1074
1075 return 0;
1076}
1077
Zhang Rui203d3d42008-01-17 15:51:08 +08001078static DEVICE_ATTR(type, 0444, type_show, NULL);
1079static DEVICE_ATTR(temp, 0444, temp_show, NULL);
1080static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -07001081static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301082static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
Ni Wade25a0a5c2015-07-14 15:40:56 +08001083static DEVICE_ATTR(available_policies, S_IRUGO, available_policies_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001084
Zhang Rui203d3d42008-01-17 15:51:08 +08001085/* sys I/F for cooling device */
1086#define to_cooling_device(_dev) \
1087 container_of(_dev, struct thermal_cooling_device, device)
1088
1089static ssize_t
1090thermal_cooling_device_type_show(struct device *dev,
1091 struct device_attribute *attr, char *buf)
1092{
1093 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1094
1095 return sprintf(buf, "%s\n", cdev->type);
1096}
1097
1098static ssize_t
1099thermal_cooling_device_max_state_show(struct device *dev,
1100 struct device_attribute *attr, char *buf)
1101{
1102 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001103 unsigned long state;
1104 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +08001105
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001106 ret = cdev->ops->get_max_state(cdev, &state);
1107 if (ret)
1108 return ret;
1109 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +08001110}
1111
1112static ssize_t
1113thermal_cooling_device_cur_state_show(struct device *dev,
1114 struct device_attribute *attr, char *buf)
1115{
1116 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001117 unsigned long state;
1118 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +08001119
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001120 ret = cdev->ops->get_cur_state(cdev, &state);
1121 if (ret)
1122 return ret;
1123 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +08001124}
1125
1126static ssize_t
1127thermal_cooling_device_cur_state_store(struct device *dev,
1128 struct device_attribute *attr,
1129 const char *buf, size_t count)
1130{
1131 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001132 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +08001133 int result;
1134
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001135 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +08001136 return -EINVAL;
1137
Roel Kluinedb94912009-12-15 22:46:50 +01001138 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +08001139 return -EINVAL;
1140
1141 result = cdev->ops->set_cur_state(cdev, state);
1142 if (result)
1143 return result;
1144 return count;
1145}
1146
1147static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -05001148__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001149static DEVICE_ATTR(max_state, 0444,
1150 thermal_cooling_device_max_state_show, NULL);
1151static DEVICE_ATTR(cur_state, 0644,
1152 thermal_cooling_device_cur_state_show,
1153 thermal_cooling_device_cur_state_store);
1154
1155static ssize_t
1156thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -05001157 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +08001158{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001159 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +08001160
1161 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001162 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001163
1164 if (instance->trip == THERMAL_TRIPS_NONE)
1165 return sprintf(buf, "-1\n");
1166 else
1167 return sprintf(buf, "%d\n", instance->trip);
1168}
1169
Matthias Kaehlcke2dc10f82015-02-20 18:10:08 -08001170static struct attribute *cooling_device_attrs[] = {
1171 &dev_attr_cdev_type.attr,
1172 &dev_attr_max_state.attr,
1173 &dev_attr_cur_state.attr,
1174 NULL,
1175};
1176
1177static const struct attribute_group cooling_device_attr_group = {
1178 .attrs = cooling_device_attrs,
1179};
1180
1181static const struct attribute_group *cooling_device_attr_groups[] = {
1182 &cooling_device_attr_group,
1183 NULL,
1184};
1185
Javi Merinodb916512015-02-18 16:04:24 +00001186static ssize_t
1187thermal_cooling_device_weight_show(struct device *dev,
1188 struct device_attribute *attr, char *buf)
1189{
1190 struct thermal_instance *instance;
1191
1192 instance = container_of(attr, struct thermal_instance, weight_attr);
1193
1194 return sprintf(buf, "%d\n", instance->weight);
1195}
1196
1197static ssize_t
1198thermal_cooling_device_weight_store(struct device *dev,
1199 struct device_attribute *attr,
1200 const char *buf, size_t count)
1201{
1202 struct thermal_instance *instance;
1203 int ret, weight;
1204
1205 ret = kstrtoint(buf, 0, &weight);
1206 if (ret)
1207 return ret;
1208
1209 instance = container_of(attr, struct thermal_instance, weight_attr);
1210 instance->weight = weight;
1211
1212 return count;
1213}
Zhang Rui203d3d42008-01-17 15:51:08 +08001214/* Device management */
1215
1216/**
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001217 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
1218 * @tz: pointer to struct thermal_zone_device
Zhang Rui203d3d42008-01-17 15:51:08 +08001219 * @trip: indicates which trip point the cooling devices is
1220 * associated with in this thermal zone.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001221 * @cdev: pointer to struct thermal_cooling_device
1222 * @upper: the Maximum cooling state for this trip point.
1223 * THERMAL_NO_LIMIT means no upper limit,
1224 * and the cooling device can be in max_state.
1225 * @lower: the Minimum cooling state can be used for this trip point.
1226 * THERMAL_NO_LIMIT means no lower limit,
1227 * and the cooling device can be in cooling state 0.
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001228 * @weight: The weight of the cooling device to be bound to the
1229 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
1230 * default value
Len Brown543a9562008-02-07 16:55:08 -05001231 *
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001232 * This interface function bind a thermal cooling device to the certain trip
1233 * point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001234 * This function is usually called in the thermal zone device .bind callback.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001235 *
1236 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001237 */
1238int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1239 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +08001240 struct thermal_cooling_device *cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001241 unsigned long upper, unsigned long lower,
1242 unsigned int weight)
Zhang Rui203d3d42008-01-17 15:51:08 +08001243{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001244 struct thermal_instance *dev;
1245 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -05001246 struct thermal_zone_device *pos1;
1247 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +08001248 unsigned long max_state;
Lukasz Majewski9a3031d2014-11-18 11:16:30 +01001249 int result, ret;
Zhang Rui203d3d42008-01-17 15:51:08 +08001250
Len Brown543a9562008-02-07 16:55:08 -05001251 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +08001252 return -EINVAL;
1253
Thomas Sujithc7516702008-02-15 00:58:50 -05001254 list_for_each_entry(pos1, &thermal_tz_list, node) {
1255 if (pos1 == tz)
1256 break;
1257 }
1258 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1259 if (pos2 == cdev)
1260 break;
1261 }
1262
1263 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +08001264 return -EINVAL;
1265
Lukasz Majewski9a3031d2014-11-18 11:16:30 +01001266 ret = cdev->ops->get_max_state(cdev, &max_state);
1267 if (ret)
1268 return ret;
Zhang Rui9d998422012-06-26 16:35:57 +08001269
1270 /* lower default 0, upper default max_state */
1271 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1272 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1273
1274 if (lower > upper || upper > max_state)
1275 return -EINVAL;
1276
Zhang Rui203d3d42008-01-17 15:51:08 +08001277 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001278 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001279 if (!dev)
1280 return -ENOMEM;
1281 dev->tz = tz;
1282 dev->cdev = cdev;
1283 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +08001284 dev->upper = upper;
1285 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +08001286 dev->target = THERMAL_NO_TARGET;
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001287 dev->weight = weight;
Zhang Rui74051ba2012-06-26 16:27:22 +08001288
Zhang Rui203d3d42008-01-17 15:51:08 +08001289 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1290 if (result)
1291 goto free_mem;
1292
1293 sprintf(dev->name, "cdev%d", dev->id);
1294 result =
1295 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1296 if (result)
1297 goto release_idr;
1298
1299 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -07001300 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001301 dev->attr.attr.name = dev->attr_name;
1302 dev->attr.attr.mode = 0444;
1303 dev->attr.show = thermal_cooling_device_trip_point_show;
1304 result = device_create_file(&tz->device, &dev->attr);
1305 if (result)
1306 goto remove_symbol_link;
1307
Javi Merinodb916512015-02-18 16:04:24 +00001308 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
1309 sysfs_attr_init(&dev->weight_attr.attr);
1310 dev->weight_attr.attr.name = dev->weight_attr_name;
1311 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
1312 dev->weight_attr.show = thermal_cooling_device_weight_show;
1313 dev->weight_attr.store = thermal_cooling_device_weight_store;
1314 result = device_create_file(&tz->device, &dev->weight_attr);
1315 if (result)
1316 goto remove_trip_file;
1317
Zhang Rui203d3d42008-01-17 15:51:08 +08001318 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001319 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001320 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +08001321 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1322 result = -EEXIST;
1323 break;
1324 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001325 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001326 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001327 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1328 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001329 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001330 mutex_unlock(&tz->lock);
1331
1332 if (!result)
1333 return 0;
1334
Javi Merinodb916512015-02-18 16:04:24 +00001335 device_remove_file(&tz->device, &dev->weight_attr);
1336remove_trip_file:
Zhang Rui203d3d42008-01-17 15:51:08 +08001337 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -07001338remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +08001339 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -07001340release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +08001341 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -07001342free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +08001343 kfree(dev);
1344 return result;
1345}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001346EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001347
1348/**
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001349 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1350 * thermal zone.
1351 * @tz: pointer to a struct thermal_zone_device.
Zhang Rui203d3d42008-01-17 15:51:08 +08001352 * @trip: indicates which trip point the cooling devices is
1353 * associated with in this thermal zone.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001354 * @cdev: pointer to a struct thermal_cooling_device.
Len Brown543a9562008-02-07 16:55:08 -05001355 *
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001356 * This interface function unbind a thermal cooling device from the certain
1357 * trip point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001358 * This function is usually called in the thermal zone device .unbind callback.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001359 *
1360 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001361 */
1362int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1363 int trip,
1364 struct thermal_cooling_device *cdev)
1365{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001366 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +08001367
1368 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001369 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001370 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -05001371 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001372 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001373 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001374 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001375 mutex_unlock(&tz->lock);
1376 goto unbind;
1377 }
1378 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001379 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001380 mutex_unlock(&tz->lock);
1381
1382 return -ENODEV;
1383
Joe Perchescaca8b82012-03-21 12:55:02 -07001384unbind:
Viresh Kumar528464e2015-07-23 14:32:32 +05301385 device_remove_file(&tz->device, &pos->weight_attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001386 device_remove_file(&tz->device, &pos->attr);
1387 sysfs_remove_link(&tz->device.kobj, pos->name);
1388 release_idr(&tz->idr, &tz->lock, pos->id);
1389 kfree(pos);
1390 return 0;
1391}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001392EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001393
1394static void thermal_release(struct device *dev)
1395{
1396 struct thermal_zone_device *tz;
1397 struct thermal_cooling_device *cdev;
1398
Joe Perchescaca8b82012-03-21 12:55:02 -07001399 if (!strncmp(dev_name(dev), "thermal_zone",
1400 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001401 tz = to_thermal_zone(dev);
1402 kfree(tz);
durgadoss.r@intel.com732e4c82013-10-02 00:08:00 +05301403 } else if(!strncmp(dev_name(dev), "cooling_device",
1404 sizeof("cooling_device") - 1)){
Zhang Rui203d3d42008-01-17 15:51:08 +08001405 cdev = to_cooling_device(dev);
1406 kfree(cdev);
1407 }
1408}
1409
1410static struct class thermal_class = {
1411 .name = "thermal",
1412 .dev_release = thermal_release,
1413};
1414
1415/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001416 * __thermal_cooling_device_register() - register a new thermal cooling device
1417 * @np: a pointer to a device tree node.
Zhang Rui203d3d42008-01-17 15:51:08 +08001418 * @type: the thermal cooling device type.
1419 * @devdata: device private data.
1420 * @ops: standard thermal cooling devices callbacks.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001421 *
1422 * This interface function adds a new thermal cooling device (fan/processor/...)
1423 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1424 * to all the thermal zone devices registered at the same time.
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001425 * It also gives the opportunity to link the cooling device to a device tree
1426 * node, so that it can be bound to a thermal zone created out of device tree.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001427 *
1428 * Return: a pointer to the created struct thermal_cooling_device or an
1429 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001430 */
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001431static struct thermal_cooling_device *
1432__thermal_cooling_device_register(struct device_node *np,
1433 char *type, void *devdata,
1434 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001435{
1436 struct thermal_cooling_device *cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001437 int result;
1438
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001439 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001440 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001441
1442 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001443 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001444 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001445
1446 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1447 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001448 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001449
1450 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1451 if (result) {
1452 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001453 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001454 }
1455
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001456 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +08001457 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001458 INIT_LIST_HEAD(&cdev->thermal_instances);
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001459 cdev->np = np;
Zhang Rui203d3d42008-01-17 15:51:08 +08001460 cdev->ops = ops;
Ni Wade5ca0cce2014-02-17 11:02:55 +08001461 cdev->updated = false;
Zhang Rui203d3d42008-01-17 15:51:08 +08001462 cdev->device.class = &thermal_class;
Matthias Kaehlcke2dc10f82015-02-20 18:10:08 -08001463 cdev->device.groups = cooling_device_attr_groups;
Zhang Rui203d3d42008-01-17 15:51:08 +08001464 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -08001465 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001466 result = device_register(&cdev->device);
1467 if (result) {
1468 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1469 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001470 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001471 }
1472
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301473 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +08001474 mutex_lock(&thermal_list_lock);
1475 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001476 mutex_unlock(&thermal_list_lock);
1477
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301478 /* Update binding information for 'this' new cdev */
1479 bind_cdev(cdev);
1480
1481 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001482}
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001483
1484/**
1485 * thermal_cooling_device_register() - register a new thermal cooling device
1486 * @type: the thermal cooling device type.
1487 * @devdata: device private data.
1488 * @ops: standard thermal cooling devices callbacks.
1489 *
1490 * This interface function adds a new thermal cooling device (fan/processor/...)
1491 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1492 * to all the thermal zone devices registered at the same time.
1493 *
1494 * Return: a pointer to the created struct thermal_cooling_device or an
1495 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1496 */
1497struct thermal_cooling_device *
1498thermal_cooling_device_register(char *type, void *devdata,
1499 const struct thermal_cooling_device_ops *ops)
1500{
1501 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1502}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001503EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001504
1505/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001506 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1507 * @np: a pointer to a device tree node.
1508 * @type: the thermal cooling device type.
1509 * @devdata: device private data.
1510 * @ops: standard thermal cooling devices callbacks.
1511 *
1512 * This function will register a cooling device with device tree node reference.
1513 * This interface function adds a new thermal cooling device (fan/processor/...)
1514 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1515 * to all the thermal zone devices registered at the same time.
1516 *
1517 * Return: a pointer to the created struct thermal_cooling_device or an
1518 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1519 */
1520struct thermal_cooling_device *
1521thermal_of_cooling_device_register(struct device_node *np,
1522 char *type, void *devdata,
1523 const struct thermal_cooling_device_ops *ops)
1524{
1525 return __thermal_cooling_device_register(np, type, devdata, ops);
1526}
1527EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1528
1529/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001530 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001531 * @cdev: the thermal cooling device to remove.
1532 *
1533 * thermal_cooling_device_unregister() must be called when the device is no
1534 * longer needed.
1535 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301536void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001537{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301538 int i;
1539 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001540 struct thermal_zone_device *tz;
1541 struct thermal_cooling_device *pos = NULL;
1542
1543 if (!cdev)
1544 return;
1545
1546 mutex_lock(&thermal_list_lock);
1547 list_for_each_entry(pos, &thermal_cdev_list, node)
1548 if (pos == cdev)
1549 break;
1550 if (pos != cdev) {
1551 /* thermal cooling device not found */
1552 mutex_unlock(&thermal_list_lock);
1553 return;
1554 }
1555 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301556
1557 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001558 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301559 if (tz->ops->unbind) {
1560 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001561 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301562 }
1563
1564 if (!tz->tzp || !tz->tzp->tbp)
1565 continue;
1566
1567 tzp = tz->tzp;
1568 for (i = 0; i < tzp->num_tbps; i++) {
1569 if (tzp->tbp[i].cdev == cdev) {
1570 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1571 tzp->tbp[i].cdev = NULL;
1572 }
1573 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001574 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301575
Zhang Rui203d3d42008-01-17 15:51:08 +08001576 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301577
Zhang Rui203d3d42008-01-17 15:51:08 +08001578 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001579 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001580 device_remove_file(&cdev->device, &dev_attr_max_state);
1581 device_remove_file(&cdev->device, &dev_attr_cur_state);
1582
1583 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1584 device_unregister(&cdev->device);
1585 return;
1586}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001587EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001588
Durgadoss Rdc765482012-09-18 11:05:00 +05301589void thermal_cdev_update(struct thermal_cooling_device *cdev)
Zhang Ruice119f82012-06-27 14:13:04 +08001590{
1591 struct thermal_instance *instance;
1592 unsigned long target = 0;
1593
1594 /* cooling device is updated*/
1595 if (cdev->updated)
1596 return;
1597
Zhang Ruif4a821c2012-07-24 16:56:21 +08001598 mutex_lock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001599 /* Make sure cdev enters the deepest cooling state */
1600 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
Aaron Lu06475b52013-12-02 13:54:26 +08001601 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1602 instance->tz->id, instance->target);
Zhang Ruice119f82012-06-27 14:13:04 +08001603 if (instance->target == THERMAL_NO_TARGET)
1604 continue;
1605 if (instance->target > target)
1606 target = instance->target;
1607 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001608 mutex_unlock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001609 cdev->ops->set_cur_state(cdev, target);
1610 cdev->updated = true;
Punit Agrawal39811562014-07-29 11:50:49 +01001611 trace_cdev_update(cdev, target);
Aaron Lu06475b52013-12-02 13:54:26 +08001612 dev_dbg(&cdev->device, "set to state %lu\n", target);
Zhang Ruice119f82012-06-27 14:13:04 +08001613}
Durgadoss Rdc765482012-09-18 11:05:00 +05301614EXPORT_SYMBOL(thermal_cdev_update);
Zhang Ruice119f82012-06-27 14:13:04 +08001615
Matthew Garrettb1569e92008-12-03 17:55:32 +00001616/**
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001617 * thermal_notify_framework - Sensor drivers use this API to notify framework
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301618 * @tz: thermal zone device
1619 * @trip: indicates which trip point has been crossed
1620 *
1621 * This function handles the trip events from sensor drivers. It starts
1622 * throttling the cooling devices according to the policy configured.
1623 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1624 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1625 * The throttling policy is based on the configured platform data; if no
1626 * platform data is provided, this uses the step_wise throttling policy.
1627 */
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001628void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301629{
1630 handle_thermal_trip(tz, trip);
1631}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001632EXPORT_SYMBOL_GPL(thermal_notify_framework);
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301633
1634/**
Eduardo Valentin269c1742013-04-23 21:48:19 +00001635 * create_trip_attrs() - create attributes for trip points
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001636 * @tz: the thermal zone device
1637 * @mask: Writeable trip point bitmap.
Eduardo Valentin269c1742013-04-23 21:48:19 +00001638 *
1639 * helper function to instantiate sysfs entries for every trip
1640 * point and its properties of a struct thermal_zone_device.
1641 *
1642 * Return: 0 on success, the proper error value otherwise.
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001643 */
1644static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1645{
1646 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001647 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001648
Durgadoss R27365a62012-07-25 10:10:59 +08001649 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001650 if (!tz->trip_type_attrs)
1651 return -ENOMEM;
1652
Durgadoss R27365a62012-07-25 10:10:59 +08001653 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001654 if (!tz->trip_temp_attrs) {
1655 kfree(tz->trip_type_attrs);
1656 return -ENOMEM;
1657 }
1658
Durgadoss R27365a62012-07-25 10:10:59 +08001659 if (tz->ops->get_trip_hyst) {
1660 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1661 if (!tz->trip_hyst_attrs) {
1662 kfree(tz->trip_type_attrs);
1663 kfree(tz->trip_temp_attrs);
1664 return -ENOMEM;
1665 }
1666 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001667
Durgadoss R27365a62012-07-25 10:10:59 +08001668
1669 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001670 /* create trip type attribute */
1671 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1672 "trip_point_%d_type", indx);
1673
1674 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1675 tz->trip_type_attrs[indx].attr.attr.name =
1676 tz->trip_type_attrs[indx].name;
1677 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1678 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1679
1680 device_create_file(&tz->device,
1681 &tz->trip_type_attrs[indx].attr);
1682
1683 /* create trip temp attribute */
1684 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1685 "trip_point_%d_temp", indx);
1686
1687 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1688 tz->trip_temp_attrs[indx].attr.attr.name =
1689 tz->trip_temp_attrs[indx].name;
1690 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1691 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
Punit Agrawal35e94642015-03-03 10:43:03 +00001692 if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS) &&
1693 mask & (1 << indx)) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001694 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1695 tz->trip_temp_attrs[indx].attr.store =
1696 trip_point_temp_store;
1697 }
1698
1699 device_create_file(&tz->device,
1700 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001701
1702 /* create Optional trip hyst attribute */
1703 if (!tz->ops->get_trip_hyst)
1704 continue;
1705 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1706 "trip_point_%d_hyst", indx);
1707
1708 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1709 tz->trip_hyst_attrs[indx].attr.attr.name =
1710 tz->trip_hyst_attrs[indx].name;
1711 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1712 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1713 if (tz->ops->set_trip_hyst) {
1714 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1715 tz->trip_hyst_attrs[indx].attr.store =
1716 trip_point_hyst_store;
1717 }
1718
1719 device_create_file(&tz->device,
1720 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001721 }
1722 return 0;
1723}
1724
1725static void remove_trip_attrs(struct thermal_zone_device *tz)
1726{
1727 int indx;
1728
1729 for (indx = 0; indx < tz->trips; indx++) {
1730 device_remove_file(&tz->device,
1731 &tz->trip_type_attrs[indx].attr);
1732 device_remove_file(&tz->device,
1733 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001734 if (tz->ops->get_trip_hyst)
1735 device_remove_file(&tz->device,
1736 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001737 }
1738 kfree(tz->trip_type_attrs);
1739 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001740 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001741}
1742
1743/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001744 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001745 * @type: the thermal zone device type
1746 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001747 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001748 * @devdata: private device data
1749 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301750 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001751 * @passive_delay: number of milliseconds to wait between polls when
1752 * performing passive cooling
1753 * @polling_delay: number of milliseconds to wait between polls when checking
1754 * whether trip points have been crossed (0 for interrupt
1755 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001756 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001757 * This interface function adds a new thermal zone device (sensor) to
1758 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1759 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08001760 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001761 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001762 *
1763 * Return: a pointer to the created struct thermal_zone_device or an
1764 * in case of error, an ERR_PTR. Caller must check return value with
1765 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001766 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001767struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001768 int trips, int mask, void *devdata,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001769 struct thermal_zone_device_ops *ops,
Javi Merino6b775e82015-03-02 17:17:19 +00001770 struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001771 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001772{
1773 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001774 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001775 int result;
1776 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001777 int passive = 0;
Javi Merinoe33df1d2015-02-26 19:00:27 +00001778 struct thermal_governor *governor;
Zhang Rui203d3d42008-01-17 15:51:08 +08001779
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001780 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001781 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001782
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001783 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001784 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001785
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04001786 if (!ops)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001787 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001788
Jonghwa Lee83720d02013-05-18 09:50:26 +00001789 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00001790 return ERR_PTR(-EINVAL);
1791
Zhang Rui203d3d42008-01-17 15:51:08 +08001792 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1793 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001794 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001795
Zhang Rui2d374132012-06-27 10:09:00 +08001796 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001797 idr_init(&tz->idr);
1798 mutex_init(&tz->lock);
1799 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1800 if (result) {
1801 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001802 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001803 }
1804
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001805 strlcpy(tz->type, type ? : "", sizeof(tz->type));
Zhang Rui203d3d42008-01-17 15:51:08 +08001806 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301807 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001808 tz->device.class = &thermal_class;
1809 tz->devdata = devdata;
1810 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001811 tz->passive_delay = passive_delay;
1812 tz->polling_delay = polling_delay;
1813
Kay Sievers354655e2009-01-06 10:44:37 -08001814 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001815 result = device_register(&tz->device);
1816 if (result) {
1817 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1818 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001819 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001820 }
1821
1822 /* sys I/F */
1823 if (type) {
1824 result = device_create_file(&tz->device, &dev_attr_type);
1825 if (result)
1826 goto unregister;
1827 }
1828
1829 result = device_create_file(&tz->device, &dev_attr_temp);
1830 if (result)
1831 goto unregister;
1832
1833 if (ops->get_mode) {
1834 result = device_create_file(&tz->device, &dev_attr_mode);
1835 if (result)
1836 goto unregister;
1837 }
1838
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001839 result = create_trip_attrs(tz, mask);
1840 if (result)
1841 goto unregister;
1842
Zhang Rui203d3d42008-01-17 15:51:08 +08001843 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001844 tz->ops->get_trip_type(tz, count, &trip_type);
1845 if (trip_type == THERMAL_TRIP_PASSIVE)
1846 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001847 }
1848
Durgadoss R5a2c0902012-09-18 11:04:58 +05301849 if (!passive) {
1850 result = device_create_file(&tz->device, &dev_attr_passive);
1851 if (result)
1852 goto unregister;
1853 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001854
Sascha Hauer79e54212015-07-06 09:46:16 +02001855 if (IS_ENABLED(CONFIG_THERMAL_EMULATION)) {
1856 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1857 if (result)
1858 goto unregister;
1859 }
1860
Durgadoss R5a2c0902012-09-18 11:04:58 +05301861 /* Create policy attribute */
1862 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00001863 if (result)
1864 goto unregister;
1865
Javi Merino9f382712015-03-26 15:53:02 +00001866 /* Add thermal zone params */
1867 result = create_tzp_attrs(&tz->device);
1868 if (result)
1869 goto unregister;
1870
Ni Wade25a0a5c2015-07-14 15:40:56 +08001871 /* Create available_policies attribute */
1872 result = device_create_file(&tz->device, &dev_attr_available_policies);
1873 if (result)
1874 goto unregister;
1875
Durgadoss Ra4a15482012-09-18 11:04:57 +05301876 /* Update 'this' zone's governor information */
1877 mutex_lock(&thermal_governor_lock);
1878
1879 if (tz->tzp)
Javi Merinoe33df1d2015-02-26 19:00:27 +00001880 governor = __find_governor(tz->tzp->governor_name);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301881 else
Javi Merinoe33df1d2015-02-26 19:00:27 +00001882 governor = def_governor;
1883
1884 result = thermal_set_governor(tz, governor);
1885 if (result) {
1886 mutex_unlock(&thermal_governor_lock);
1887 goto unregister;
1888 }
Durgadoss Ra4a15482012-09-18 11:04:57 +05301889
1890 mutex_unlock(&thermal_governor_lock);
1891
Eduardo Valentinccba4ff2013-08-15 11:34:17 -04001892 if (!tz->tzp || !tz->tzp->no_hwmon) {
1893 result = thermal_add_hwmon_sysfs(tz);
1894 if (result)
1895 goto unregister;
1896 }
Zhang Ruie68b16a2008-04-21 16:07:52 +08001897
Zhang Rui203d3d42008-01-17 15:51:08 +08001898 mutex_lock(&thermal_list_lock);
1899 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001900 mutex_unlock(&thermal_list_lock);
1901
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301902 /* Bind cooling devices for this zone */
1903 bind_tz(tz);
1904
Matthew Garrettb1569e92008-12-03 17:55:32 +00001905 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1906
1907 thermal_zone_device_update(tz);
1908
Yao Dongdong14015862014-10-28 07:40:25 +00001909 return tz;
Zhang Rui203d3d42008-01-17 15:51:08 +08001910
Joe Perchescaca8b82012-03-21 12:55:02 -07001911unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001912 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1913 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001914 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001915}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001916EXPORT_SYMBOL_GPL(thermal_zone_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001917
1918/**
1919 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001920 * @tz: the thermal zone device to remove
1921 */
1922void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1923{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301924 int i;
1925 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001926 struct thermal_cooling_device *cdev;
1927 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001928
1929 if (!tz)
1930 return;
1931
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301932 tzp = tz->tzp;
1933
Zhang Rui203d3d42008-01-17 15:51:08 +08001934 mutex_lock(&thermal_list_lock);
1935 list_for_each_entry(pos, &thermal_tz_list, node)
1936 if (pos == tz)
1937 break;
1938 if (pos != tz) {
1939 /* thermal zone device not found */
1940 mutex_unlock(&thermal_list_lock);
1941 return;
1942 }
1943 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301944
1945 /* Unbind all cdevs associated with 'this' thermal zone */
1946 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1947 if (tz->ops->unbind) {
1948 tz->ops->unbind(tz, cdev);
1949 continue;
1950 }
1951
1952 if (!tzp || !tzp->tbp)
1953 break;
1954
1955 for (i = 0; i < tzp->num_tbps; i++) {
1956 if (tzp->tbp[i].cdev == cdev) {
1957 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1958 tzp->tbp[i].cdev = NULL;
1959 }
1960 }
1961 }
1962
Zhang Rui203d3d42008-01-17 15:51:08 +08001963 mutex_unlock(&thermal_list_lock);
1964
Matthew Garrettb1569e92008-12-03 17:55:32 +00001965 thermal_zone_device_set_polling(tz, 0);
1966
Zhang Rui203d3d42008-01-17 15:51:08 +08001967 if (tz->type[0])
1968 device_remove_file(&tz->device, &dev_attr_type);
1969 device_remove_file(&tz->device, &dev_attr_temp);
1970 if (tz->ops->get_mode)
1971 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301972 device_remove_file(&tz->device, &dev_attr_policy);
Ni Wade25a0a5c2015-07-14 15:40:56 +08001973 device_remove_file(&tz->device, &dev_attr_available_policies);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001974 remove_trip_attrs(tz);
Javi Merinoe33df1d2015-02-26 19:00:27 +00001975 thermal_set_governor(tz, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001976
Zhang Ruie68b16a2008-04-21 16:07:52 +08001977 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001978 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1979 idr_destroy(&tz->idr);
1980 mutex_destroy(&tz->lock);
1981 device_unregister(&tz->device);
1982 return;
1983}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001984EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001985
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001986/**
1987 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1988 * @name: thermal zone name to fetch the temperature
1989 *
1990 * When only one zone is found with the passed name, returns a reference to it.
1991 *
1992 * Return: On success returns a reference to an unique thermal zone with
1993 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1994 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1995 */
1996struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1997{
1998 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1999 unsigned int found = 0;
2000
2001 if (!name)
2002 goto exit;
2003
2004 mutex_lock(&thermal_list_lock);
2005 list_for_each_entry(pos, &thermal_tz_list, node)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -07002006 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
Eduardo Valentin63c4d912013-04-05 12:32:28 +00002007 found++;
2008 ref = pos;
2009 }
2010 mutex_unlock(&thermal_list_lock);
2011
2012 /* nothing has been found, thus an error code for it */
2013 if (found == 0)
2014 ref = ERR_PTR(-ENODEV);
2015 else if (found > 1)
2016 /* Success only when an unique zone is found */
2017 ref = ERR_PTR(-EEXIST);
2018
2019exit:
2020 return ref;
2021}
2022EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
2023
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002024#ifdef CONFIG_NET
Johannes Berg2a94fe42013-11-19 15:19:39 +01002025static const struct genl_multicast_group thermal_event_mcgrps[] = {
2026 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
2027};
2028
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002029static struct genl_family thermal_event_genl_family = {
2030 .id = GENL_ID_GENERATE,
2031 .name = THERMAL_GENL_FAMILY_NAME,
2032 .version = THERMAL_GENL_VERSION,
2033 .maxattr = THERMAL_GENL_ATTR_MAX,
Johannes Berg2a94fe42013-11-19 15:19:39 +01002034 .mcgrps = thermal_event_mcgrps,
2035 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002036};
2037
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00002038int thermal_generate_netlink_event(struct thermal_zone_device *tz,
2039 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05302040{
2041 struct sk_buff *skb;
2042 struct nlattr *attr;
2043 struct thermal_genl_event *thermal_event;
2044 void *msg_header;
2045 int size;
2046 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07002047 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05302048
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00002049 if (!tz)
2050 return -EINVAL;
2051
R.Durgadoss4cb18722010-10-27 03:33:29 +05302052 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07002053 size = nla_total_size(sizeof(struct thermal_genl_event)) +
2054 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302055
2056 skb = genlmsg_new(size, GFP_ATOMIC);
2057 if (!skb)
2058 return -ENOMEM;
2059
2060 /* add the genetlink message header */
2061 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
2062 &thermal_event_genl_family, 0,
2063 THERMAL_GENL_CMD_EVENT);
2064 if (!msg_header) {
2065 nlmsg_free(skb);
2066 return -ENOMEM;
2067 }
2068
2069 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07002070 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
2071 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05302072
2073 if (!attr) {
2074 nlmsg_free(skb);
2075 return -EINVAL;
2076 }
2077
2078 thermal_event = nla_data(attr);
2079 if (!thermal_event) {
2080 nlmsg_free(skb);
2081 return -EINVAL;
2082 }
2083
2084 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
2085
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00002086 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05302087 thermal_event->event = event;
2088
2089 /* send multicast genetlink message */
Johannes Berg053c0952015-01-16 22:09:00 +01002090 genlmsg_end(skb, msg_header);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302091
Johannes Berg68eb5502013-11-19 15:19:38 +01002092 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01002093 0, GFP_ATOMIC);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302094 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00002095 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302096
2097 return result;
2098}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00002099EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302100
2101static int genetlink_init(void)
2102{
Johannes Berg2a94fe42013-11-19 15:19:39 +01002103 return genl_register_family(&thermal_event_genl_family);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302104}
2105
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002106static void genetlink_exit(void)
2107{
2108 genl_unregister_family(&thermal_event_genl_family);
2109}
2110#else /* !CONFIG_NET */
2111static inline int genetlink_init(void) { return 0; }
2112static inline void genetlink_exit(void) {}
2113#endif /* !CONFIG_NET */
2114
Zhang Rui80a26a52013-03-26 16:38:29 +08002115static int __init thermal_register_governors(void)
2116{
2117 int result;
2118
2119 result = thermal_gov_step_wise_register();
2120 if (result)
2121 return result;
2122
2123 result = thermal_gov_fair_share_register();
2124 if (result)
2125 return result;
2126
Peter Feuerere4dbf982014-07-22 17:37:13 +02002127 result = thermal_gov_bang_bang_register();
2128 if (result)
2129 return result;
2130
Javi Merino6b775e82015-03-02 17:17:19 +00002131 result = thermal_gov_user_space_register();
2132 if (result)
2133 return result;
2134
2135 return thermal_gov_power_allocator_register();
Zhang Rui80a26a52013-03-26 16:38:29 +08002136}
2137
2138static void thermal_unregister_governors(void)
2139{
2140 thermal_gov_step_wise_unregister();
2141 thermal_gov_fair_share_unregister();
Peter Feuerere4dbf982014-07-22 17:37:13 +02002142 thermal_gov_bang_bang_unregister();
Zhang Rui80a26a52013-03-26 16:38:29 +08002143 thermal_gov_user_space_unregister();
Javi Merino6b775e82015-03-02 17:17:19 +00002144 thermal_gov_power_allocator_unregister();
Zhang Rui80a26a52013-03-26 16:38:29 +08002145}
2146
Zhang Rui203d3d42008-01-17 15:51:08 +08002147static int __init thermal_init(void)
2148{
Zhang Rui80a26a52013-03-26 16:38:29 +08002149 int result;
2150
2151 result = thermal_register_governors();
2152 if (result)
2153 goto error;
Zhang Rui203d3d42008-01-17 15:51:08 +08002154
2155 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08002156 if (result)
2157 goto unregister_governors;
2158
R.Durgadoss4cb18722010-10-27 03:33:29 +05302159 result = genetlink_init();
Zhang Rui80a26a52013-03-26 16:38:29 +08002160 if (result)
2161 goto unregister_class;
2162
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002163 result = of_parse_thermal_zones();
2164 if (result)
2165 goto exit_netlink;
2166
Zhang Rui80a26a52013-03-26 16:38:29 +08002167 return 0;
2168
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002169exit_netlink:
2170 genetlink_exit();
Zhang Rui80a26a52013-03-26 16:38:29 +08002171unregister_class:
2172 class_unregister(&thermal_class);
Luis Henriques9d367e52014-12-03 21:20:21 +00002173unregister_governors:
2174 thermal_unregister_governors();
Zhang Rui80a26a52013-03-26 16:38:29 +08002175error:
2176 idr_destroy(&thermal_tz_idr);
2177 idr_destroy(&thermal_cdev_idr);
2178 mutex_destroy(&thermal_idr_lock);
2179 mutex_destroy(&thermal_list_lock);
2180 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08002181 return result;
2182}
2183
2184static void __exit thermal_exit(void)
2185{
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002186 of_thermal_destroy_zones();
Zhang Rui80a26a52013-03-26 16:38:29 +08002187 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08002188 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08002189 thermal_unregister_governors();
Zhang Rui203d3d42008-01-17 15:51:08 +08002190 idr_destroy(&thermal_tz_idr);
2191 idr_destroy(&thermal_cdev_idr);
2192 mutex_destroy(&thermal_idr_lock);
2193 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08002194 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08002195}
2196
R.Durgadoss4cb18722010-10-27 03:33:29 +05302197fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08002198module_exit(thermal_exit);