blob: 962de1847cc08b40855df8c532e9f7e2d5ed0766 [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{
429 long trip_temp;
430
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/**
468 * thermal_zone_get_temp() - returns its the temperature of thermal zone
469 * @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 */
477int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *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#ifdef CONFIG_THERMAL_EMULATION
481 int count;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000482 unsigned long crit_temp = -1UL;
483 enum thermal_trip_type type;
Zhang Rui5e20b2e2013-02-06 14:02:12 +0800484#endif
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000485
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400486 if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000487 goto exit;
488
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000489 mutex_lock(&tz->lock);
490
491 ret = tz->ops->get_temp(tz, temp);
492#ifdef CONFIG_THERMAL_EMULATION
493 if (!tz->emul_temperature)
494 goto skip_emul;
495
496 for (count = 0; count < tz->trips; count++) {
497 ret = tz->ops->get_trip_type(tz, count, &type);
498 if (!ret && type == THERMAL_TRIP_CRITICAL) {
499 ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
500 break;
501 }
502 }
503
504 if (ret)
505 goto skip_emul;
506
507 if (*temp < crit_temp)
508 *temp = tz->emul_temperature;
509skip_emul:
510#endif
511 mutex_unlock(&tz->lock);
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000512exit:
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000513 return ret;
514}
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000515EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000516
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530517static void update_temperature(struct thermal_zone_device *tz)
518{
519 long temp;
520 int ret;
521
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000522 ret = thermal_zone_get_temp(tz, &temp);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530523 if (ret) {
Hans de Goede7e497a72015-03-21 15:02:55 +0100524 if (ret != -EAGAIN)
525 dev_warn(&tz->device,
526 "failed to read out thermal zone (%d)\n",
527 ret);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000528 return;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530529 }
530
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000531 mutex_lock(&tz->lock);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530532 tz->last_temperature = tz->temperature;
533 tz->temperature = temp;
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530534 mutex_unlock(&tz->lock);
Aaron Lu06475b52013-12-02 13:54:26 +0800535
Punit Agrawal100a8fd2014-07-29 11:50:48 +0100536 trace_thermal_temperature(tz);
Aaron Lu06475b52013-12-02 13:54:26 +0800537 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
538 tz->last_temperature, tz->temperature);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530539}
540
541void thermal_zone_device_update(struct thermal_zone_device *tz)
542{
543 int count;
544
Eduardo Valentin81bd4e12013-09-12 19:15:44 -0400545 if (!tz->ops->get_temp)
546 return;
547
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530548 update_temperature(tz);
549
550 for (count = 0; count < tz->trips; count++)
551 handle_thermal_trip(tz, count);
552}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +0000553EXPORT_SYMBOL_GPL(thermal_zone_device_update);
Durgadoss R0c01ebb2012-09-18 11:05:04 +0530554
555static void thermal_zone_device_check(struct work_struct *work)
556{
557 struct thermal_zone_device *tz = container_of(work, struct
558 thermal_zone_device,
559 poll_queue.work);
560 thermal_zone_device_update(tz);
561}
562
Zhang Rui203d3d42008-01-17 15:51:08 +0800563/* sys I/F for thermal zone */
564
565#define to_thermal_zone(_dev) \
566 container_of(_dev, struct thermal_zone_device, device)
567
568static ssize_t
569type_show(struct device *dev, struct device_attribute *attr, char *buf)
570{
571 struct thermal_zone_device *tz = to_thermal_zone(dev);
572
573 return sprintf(buf, "%s\n", tz->type);
574}
575
576static ssize_t
577temp_show(struct device *dev, struct device_attribute *attr, char *buf)
578{
579 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000580 long temperature;
581 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +0800582
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000583 ret = thermal_zone_get_temp(tz, &temperature);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000584
585 if (ret)
586 return ret;
587
588 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800589}
590
591static ssize_t
592mode_show(struct device *dev, struct device_attribute *attr, char *buf)
593{
594 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000595 enum thermal_device_mode mode;
596 int result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800597
598 if (!tz->ops->get_mode)
599 return -EPERM;
600
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000601 result = tz->ops->get_mode(tz, &mode);
602 if (result)
603 return result;
604
605 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
606 : "disabled");
Zhang Rui203d3d42008-01-17 15:51:08 +0800607}
608
609static ssize_t
610mode_store(struct device *dev, struct device_attribute *attr,
611 const char *buf, size_t count)
612{
613 struct thermal_zone_device *tz = to_thermal_zone(dev);
614 int result;
615
616 if (!tz->ops->set_mode)
617 return -EPERM;
618
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530619 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000620 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
Amit Daniel Kachhapf1f0e2a2012-03-21 16:40:01 +0530621 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000622 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
623 else
624 result = -EINVAL;
625
Zhang Rui203d3d42008-01-17 15:51:08 +0800626 if (result)
627 return result;
628
629 return count;
630}
631
632static ssize_t
633trip_point_type_show(struct device *dev, struct device_attribute *attr,
634 char *buf)
635{
636 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000637 enum thermal_trip_type type;
638 int trip, result;
Zhang Rui203d3d42008-01-17 15:51:08 +0800639
640 if (!tz->ops->get_trip_type)
641 return -EPERM;
642
643 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
644 return -EINVAL;
645
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000646 result = tz->ops->get_trip_type(tz, trip, &type);
647 if (result)
648 return result;
649
650 switch (type) {
651 case THERMAL_TRIP_CRITICAL:
Amit Kucheria625120a42009-10-16 12:46:02 +0300652 return sprintf(buf, "critical\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000653 case THERMAL_TRIP_HOT:
Amit Kucheria625120a42009-10-16 12:46:02 +0300654 return sprintf(buf, "hot\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000655 case THERMAL_TRIP_PASSIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300656 return sprintf(buf, "passive\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000657 case THERMAL_TRIP_ACTIVE:
Amit Kucheria625120a42009-10-16 12:46:02 +0300658 return sprintf(buf, "active\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000659 default:
Amit Kucheria625120a42009-10-16 12:46:02 +0300660 return sprintf(buf, "unknown\n");
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000661 }
Zhang Rui203d3d42008-01-17 15:51:08 +0800662}
663
664static ssize_t
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800665trip_point_temp_store(struct device *dev, struct device_attribute *attr,
666 const char *buf, size_t count)
667{
668 struct thermal_zone_device *tz = to_thermal_zone(dev);
669 int trip, ret;
670 unsigned long temperature;
671
672 if (!tz->ops->set_trip_temp)
673 return -EPERM;
674
675 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
676 return -EINVAL;
677
678 if (kstrtoul(buf, 10, &temperature))
679 return -EINVAL;
680
681 ret = tz->ops->set_trip_temp(tz, trip, temperature);
682
683 return ret ? ret : count;
684}
685
686static ssize_t
Zhang Rui203d3d42008-01-17 15:51:08 +0800687trip_point_temp_show(struct device *dev, struct device_attribute *attr,
688 char *buf)
689{
690 struct thermal_zone_device *tz = to_thermal_zone(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000691 int trip, ret;
692 long temperature;
Zhang Rui203d3d42008-01-17 15:51:08 +0800693
694 if (!tz->ops->get_trip_temp)
695 return -EPERM;
696
697 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
698 return -EINVAL;
699
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000700 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
701
702 if (ret)
703 return ret;
704
705 return sprintf(buf, "%ld\n", temperature);
Zhang Rui203d3d42008-01-17 15:51:08 +0800706}
707
Matthew Garrett03a971a2008-12-03 18:00:38 +0000708static ssize_t
Durgadoss R27365a62012-07-25 10:10:59 +0800709trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
710 const char *buf, size_t count)
711{
712 struct thermal_zone_device *tz = to_thermal_zone(dev);
713 int trip, ret;
714 unsigned long temperature;
715
716 if (!tz->ops->set_trip_hyst)
717 return -EPERM;
718
719 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
720 return -EINVAL;
721
722 if (kstrtoul(buf, 10, &temperature))
723 return -EINVAL;
724
725 /*
726 * We are not doing any check on the 'temperature' value
727 * here. The driver implementing 'set_trip_hyst' has to
728 * take care of this.
729 */
730 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
731
732 return ret ? ret : count;
733}
734
735static ssize_t
736trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
737 char *buf)
738{
739 struct thermal_zone_device *tz = to_thermal_zone(dev);
740 int trip, ret;
741 unsigned long temperature;
742
743 if (!tz->ops->get_trip_hyst)
744 return -EPERM;
745
746 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
747 return -EINVAL;
748
749 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
750
751 return ret ? ret : sprintf(buf, "%ld\n", temperature);
752}
753
754static ssize_t
Matthew Garrett03a971a2008-12-03 18:00:38 +0000755passive_store(struct device *dev, struct device_attribute *attr,
756 const char *buf, size_t count)
757{
758 struct thermal_zone_device *tz = to_thermal_zone(dev);
759 struct thermal_cooling_device *cdev = NULL;
760 int state;
761
762 if (!sscanf(buf, "%d\n", &state))
763 return -EINVAL;
764
Frans Pop3d8e3ad2009-10-26 08:39:02 +0100765 /* sanity check: values below 1000 millicelcius don't make sense
766 * and can cause the system to go into a thermal heart attack
767 */
768 if (state && state < 1000)
769 return -EINVAL;
770
Matthew Garrett03a971a2008-12-03 18:00:38 +0000771 if (state && !tz->forced_passive) {
772 mutex_lock(&thermal_list_lock);
773 list_for_each_entry(cdev, &thermal_cdev_list, node) {
774 if (!strncmp("Processor", cdev->type,
775 sizeof("Processor")))
776 thermal_zone_bind_cooling_device(tz,
Zhang Rui9d998422012-06-26 16:35:57 +0800777 THERMAL_TRIPS_NONE, cdev,
778 THERMAL_NO_LIMIT,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +0000779 THERMAL_NO_LIMIT,
780 THERMAL_WEIGHT_DEFAULT);
Matthew Garrett03a971a2008-12-03 18:00:38 +0000781 }
782 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100783 if (!tz->passive_delay)
784 tz->passive_delay = 1000;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000785 } else if (!state && tz->forced_passive) {
786 mutex_lock(&thermal_list_lock);
787 list_for_each_entry(cdev, &thermal_cdev_list, node) {
788 if (!strncmp("Processor", cdev->type,
789 sizeof("Processor")))
790 thermal_zone_unbind_cooling_device(tz,
791 THERMAL_TRIPS_NONE,
792 cdev);
793 }
794 mutex_unlock(&thermal_list_lock);
Frans Pope4143b02009-10-26 08:39:03 +0100795 tz->passive_delay = 0;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000796 }
797
Matthew Garrett03a971a2008-12-03 18:00:38 +0000798 tz->forced_passive = state;
799
800 thermal_zone_device_update(tz);
801
802 return count;
803}
804
805static ssize_t
806passive_show(struct device *dev, struct device_attribute *attr,
807 char *buf)
808{
809 struct thermal_zone_device *tz = to_thermal_zone(dev);
810
811 return sprintf(buf, "%d\n", tz->forced_passive);
812}
813
Durgadoss R5a2c0902012-09-18 11:04:58 +0530814static ssize_t
815policy_store(struct device *dev, struct device_attribute *attr,
816 const char *buf, size_t count)
817{
818 int ret = -EINVAL;
819 struct thermal_zone_device *tz = to_thermal_zone(dev);
820 struct thermal_governor *gov;
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000821 char name[THERMAL_NAME_LENGTH];
822
823 snprintf(name, sizeof(name), "%s", buf);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530824
825 mutex_lock(&thermal_governor_lock);
Javi Merinob6cc7722014-11-25 16:00:33 +0000826 mutex_lock(&tz->lock);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530827
Andy Shevchenko42a5bf52013-05-17 11:52:02 +0000828 gov = __find_governor(strim(name));
Durgadoss R5a2c0902012-09-18 11:04:58 +0530829 if (!gov)
830 goto exit;
831
Javi Merinoe33df1d2015-02-26 19:00:27 +0000832 ret = thermal_set_governor(tz, gov);
833 if (!ret)
834 ret = count;
Durgadoss R5a2c0902012-09-18 11:04:58 +0530835
836exit:
Javi Merinob6cc7722014-11-25 16:00:33 +0000837 mutex_unlock(&tz->lock);
Durgadoss R5a2c0902012-09-18 11:04:58 +0530838 mutex_unlock(&thermal_governor_lock);
839 return ret;
840}
841
842static ssize_t
843policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
844{
845 struct thermal_zone_device *tz = to_thermal_zone(dev);
846
847 return sprintf(buf, "%s\n", tz->governor->name);
848}
849
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000850#ifdef CONFIG_THERMAL_EMULATION
851static ssize_t
852emul_temp_store(struct device *dev, struct device_attribute *attr,
853 const char *buf, size_t count)
854{
855 struct thermal_zone_device *tz = to_thermal_zone(dev);
856 int ret = 0;
857 unsigned long temperature;
858
859 if (kstrtoul(buf, 10, &temperature))
860 return -EINVAL;
861
862 if (!tz->ops->set_emul_temp) {
863 mutex_lock(&tz->lock);
864 tz->emul_temperature = temperature;
865 mutex_unlock(&tz->lock);
866 } else {
867 ret = tz->ops->set_emul_temp(tz, temperature);
868 }
869
lan,Tianyu800744b2014-01-02 15:47:54 +0800870 if (!ret)
871 thermal_zone_device_update(tz);
872
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000873 return ret ? ret : count;
874}
875static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
876#endif/*CONFIG_THERMAL_EMULATION*/
877
Javi Merino9f382712015-03-26 15:53:02 +0000878static ssize_t
879sustainable_power_show(struct device *dev, struct device_attribute *devattr,
880 char *buf)
881{
882 struct thermal_zone_device *tz = to_thermal_zone(dev);
883
884 if (tz->tzp)
885 return sprintf(buf, "%u\n", tz->tzp->sustainable_power);
886 else
887 return -EIO;
888}
889
890static ssize_t
891sustainable_power_store(struct device *dev, struct device_attribute *devattr,
892 const char *buf, size_t count)
893{
894 struct thermal_zone_device *tz = to_thermal_zone(dev);
895 u32 sustainable_power;
896
897 if (!tz->tzp)
898 return -EIO;
899
900 if (kstrtou32(buf, 10, &sustainable_power))
901 return -EINVAL;
902
903 tz->tzp->sustainable_power = sustainable_power;
904
905 return count;
906}
907static DEVICE_ATTR(sustainable_power, S_IWUSR | S_IRUGO, sustainable_power_show,
908 sustainable_power_store);
909
910#define create_s32_tzp_attr(name) \
911 static ssize_t \
912 name##_show(struct device *dev, struct device_attribute *devattr, \
913 char *buf) \
914 { \
915 struct thermal_zone_device *tz = to_thermal_zone(dev); \
916 \
917 if (tz->tzp) \
918 return sprintf(buf, "%u\n", tz->tzp->name); \
919 else \
920 return -EIO; \
921 } \
922 \
923 static ssize_t \
924 name##_store(struct device *dev, struct device_attribute *devattr, \
925 const char *buf, size_t count) \
926 { \
927 struct thermal_zone_device *tz = to_thermal_zone(dev); \
928 s32 value; \
929 \
930 if (!tz->tzp) \
931 return -EIO; \
932 \
933 if (kstrtos32(buf, 10, &value)) \
934 return -EINVAL; \
935 \
936 tz->tzp->name = value; \
937 \
938 return count; \
939 } \
940 static DEVICE_ATTR(name, S_IWUSR | S_IRUGO, name##_show, name##_store)
941
942create_s32_tzp_attr(k_po);
943create_s32_tzp_attr(k_pu);
944create_s32_tzp_attr(k_i);
945create_s32_tzp_attr(k_d);
946create_s32_tzp_attr(integral_cutoff);
947#undef create_s32_tzp_attr
948
949static struct device_attribute *dev_tzp_attrs[] = {
950 &dev_attr_sustainable_power,
951 &dev_attr_k_po,
952 &dev_attr_k_pu,
953 &dev_attr_k_i,
954 &dev_attr_k_d,
955 &dev_attr_integral_cutoff,
956};
957
958static int create_tzp_attrs(struct device *dev)
959{
960 int i;
961
962 for (i = 0; i < ARRAY_SIZE(dev_tzp_attrs); i++) {
963 int ret;
964 struct device_attribute *dev_attr = dev_tzp_attrs[i];
965
966 ret = device_create_file(dev, dev_attr);
967 if (ret)
968 return ret;
969 }
970
971 return 0;
972}
973
Javi Merino35b11d22015-02-26 19:00:28 +0000974/**
975 * power_actor_get_max_power() - get the maximum power that a cdev can consume
976 * @cdev: pointer to &thermal_cooling_device
977 * @tz: a valid thermal zone device pointer
978 * @max_power: pointer in which to store the maximum power
979 *
980 * Calculate the maximum power consumption in milliwats that the
981 * cooling device can currently consume and store it in @max_power.
982 *
983 * Return: 0 on success, -EINVAL if @cdev doesn't support the
984 * power_actor API or -E* on other error.
985 */
986int power_actor_get_max_power(struct thermal_cooling_device *cdev,
987 struct thermal_zone_device *tz, u32 *max_power)
988{
989 if (!cdev_is_power_actor(cdev))
990 return -EINVAL;
991
992 return cdev->ops->state2power(cdev, tz, 0, max_power);
993}
994
995/**
996 * power_actor_set_power() - limit the maximum power that a cooling device can consume
997 * @cdev: pointer to &thermal_cooling_device
998 * @instance: thermal instance to update
999 * @power: the power in milliwatts
1000 *
1001 * Set the cooling device to consume at most @power milliwatts.
1002 *
1003 * Return: 0 on success, -EINVAL if the cooling device does not
1004 * implement the power actor API or -E* for other failures.
1005 */
1006int power_actor_set_power(struct thermal_cooling_device *cdev,
1007 struct thermal_instance *instance, u32 power)
1008{
1009 unsigned long state;
1010 int ret;
1011
1012 if (!cdev_is_power_actor(cdev))
1013 return -EINVAL;
1014
1015 ret = cdev->ops->power2state(cdev, instance->tz, power, &state);
1016 if (ret)
1017 return ret;
1018
1019 instance->target = state;
1020 cdev->updated = false;
1021 thermal_cdev_update(cdev);
1022
1023 return 0;
1024}
1025
Zhang Rui203d3d42008-01-17 15:51:08 +08001026static DEVICE_ATTR(type, 0444, type_show, NULL);
1027static DEVICE_ATTR(temp, 0444, temp_show, NULL);
1028static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
Joe Perches886ee542012-03-21 12:55:01 -07001029static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301030static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
Zhang Rui203d3d42008-01-17 15:51:08 +08001031
Zhang Rui203d3d42008-01-17 15:51:08 +08001032/* sys I/F for cooling device */
1033#define to_cooling_device(_dev) \
1034 container_of(_dev, struct thermal_cooling_device, device)
1035
1036static ssize_t
1037thermal_cooling_device_type_show(struct device *dev,
1038 struct device_attribute *attr, char *buf)
1039{
1040 struct thermal_cooling_device *cdev = to_cooling_device(dev);
1041
1042 return sprintf(buf, "%s\n", cdev->type);
1043}
1044
1045static ssize_t
1046thermal_cooling_device_max_state_show(struct device *dev,
1047 struct device_attribute *attr, char *buf)
1048{
1049 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001050 unsigned long state;
1051 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +08001052
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001053 ret = cdev->ops->get_max_state(cdev, &state);
1054 if (ret)
1055 return ret;
1056 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +08001057}
1058
1059static ssize_t
1060thermal_cooling_device_cur_state_show(struct device *dev,
1061 struct device_attribute *attr, char *buf)
1062{
1063 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001064 unsigned long state;
1065 int ret;
Zhang Rui203d3d42008-01-17 15:51:08 +08001066
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001067 ret = cdev->ops->get_cur_state(cdev, &state);
1068 if (ret)
1069 return ret;
1070 return sprintf(buf, "%ld\n", state);
Zhang Rui203d3d42008-01-17 15:51:08 +08001071}
1072
1073static ssize_t
1074thermal_cooling_device_cur_state_store(struct device *dev,
1075 struct device_attribute *attr,
1076 const char *buf, size_t count)
1077{
1078 struct thermal_cooling_device *cdev = to_cooling_device(dev);
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001079 unsigned long state;
Zhang Rui203d3d42008-01-17 15:51:08 +08001080 int result;
1081
Matthew Garrett6503e5d2008-11-27 17:48:13 +00001082 if (!sscanf(buf, "%ld\n", &state))
Zhang Rui203d3d42008-01-17 15:51:08 +08001083 return -EINVAL;
1084
Roel Kluinedb94912009-12-15 22:46:50 +01001085 if ((long)state < 0)
Zhang Rui203d3d42008-01-17 15:51:08 +08001086 return -EINVAL;
1087
1088 result = cdev->ops->set_cur_state(cdev, state);
1089 if (result)
1090 return result;
1091 return count;
1092}
1093
1094static struct device_attribute dev_attr_cdev_type =
Len Brown543a9562008-02-07 16:55:08 -05001095__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001096static DEVICE_ATTR(max_state, 0444,
1097 thermal_cooling_device_max_state_show, NULL);
1098static DEVICE_ATTR(cur_state, 0644,
1099 thermal_cooling_device_cur_state_show,
1100 thermal_cooling_device_cur_state_store);
1101
1102static ssize_t
1103thermal_cooling_device_trip_point_show(struct device *dev,
Len Brown543a9562008-02-07 16:55:08 -05001104 struct device_attribute *attr, char *buf)
Zhang Rui203d3d42008-01-17 15:51:08 +08001105{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001106 struct thermal_instance *instance;
Zhang Rui203d3d42008-01-17 15:51:08 +08001107
1108 instance =
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001109 container_of(attr, struct thermal_instance, attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001110
1111 if (instance->trip == THERMAL_TRIPS_NONE)
1112 return sprintf(buf, "-1\n");
1113 else
1114 return sprintf(buf, "%d\n", instance->trip);
1115}
1116
Matthias Kaehlcke2dc10f82015-02-20 18:10:08 -08001117static struct attribute *cooling_device_attrs[] = {
1118 &dev_attr_cdev_type.attr,
1119 &dev_attr_max_state.attr,
1120 &dev_attr_cur_state.attr,
1121 NULL,
1122};
1123
1124static const struct attribute_group cooling_device_attr_group = {
1125 .attrs = cooling_device_attrs,
1126};
1127
1128static const struct attribute_group *cooling_device_attr_groups[] = {
1129 &cooling_device_attr_group,
1130 NULL,
1131};
1132
Javi Merinodb916512015-02-18 16:04:24 +00001133static ssize_t
1134thermal_cooling_device_weight_show(struct device *dev,
1135 struct device_attribute *attr, char *buf)
1136{
1137 struct thermal_instance *instance;
1138
1139 instance = container_of(attr, struct thermal_instance, weight_attr);
1140
1141 return sprintf(buf, "%d\n", instance->weight);
1142}
1143
1144static ssize_t
1145thermal_cooling_device_weight_store(struct device *dev,
1146 struct device_attribute *attr,
1147 const char *buf, size_t count)
1148{
1149 struct thermal_instance *instance;
1150 int ret, weight;
1151
1152 ret = kstrtoint(buf, 0, &weight);
1153 if (ret)
1154 return ret;
1155
1156 instance = container_of(attr, struct thermal_instance, weight_attr);
1157 instance->weight = weight;
1158
1159 return count;
1160}
Zhang Rui203d3d42008-01-17 15:51:08 +08001161/* Device management */
1162
1163/**
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001164 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
1165 * @tz: pointer to struct thermal_zone_device
Zhang Rui203d3d42008-01-17 15:51:08 +08001166 * @trip: indicates which trip point the cooling devices is
1167 * associated with in this thermal zone.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001168 * @cdev: pointer to struct thermal_cooling_device
1169 * @upper: the Maximum cooling state for this trip point.
1170 * THERMAL_NO_LIMIT means no upper limit,
1171 * and the cooling device can be in max_state.
1172 * @lower: the Minimum cooling state can be used for this trip point.
1173 * THERMAL_NO_LIMIT means no lower limit,
1174 * and the cooling device can be in cooling state 0.
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001175 * @weight: The weight of the cooling device to be bound to the
1176 * thermal zone. Use THERMAL_WEIGHT_DEFAULT for the
1177 * default value
Len Brown543a9562008-02-07 16:55:08 -05001178 *
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001179 * This interface function bind a thermal cooling device to the certain trip
1180 * point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001181 * This function is usually called in the thermal zone device .bind callback.
Eduardo Valentind2e4eb82013-04-23 21:48:16 +00001182 *
1183 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001184 */
1185int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
1186 int trip,
Zhang Rui9d998422012-06-26 16:35:57 +08001187 struct thermal_cooling_device *cdev,
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001188 unsigned long upper, unsigned long lower,
1189 unsigned int weight)
Zhang Rui203d3d42008-01-17 15:51:08 +08001190{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001191 struct thermal_instance *dev;
1192 struct thermal_instance *pos;
Thomas Sujithc7516702008-02-15 00:58:50 -05001193 struct thermal_zone_device *pos1;
1194 struct thermal_cooling_device *pos2;
Zhang Rui74051ba2012-06-26 16:27:22 +08001195 unsigned long max_state;
Lukasz Majewski9a3031d2014-11-18 11:16:30 +01001196 int result, ret;
Zhang Rui203d3d42008-01-17 15:51:08 +08001197
Len Brown543a9562008-02-07 16:55:08 -05001198 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
Zhang Rui203d3d42008-01-17 15:51:08 +08001199 return -EINVAL;
1200
Thomas Sujithc7516702008-02-15 00:58:50 -05001201 list_for_each_entry(pos1, &thermal_tz_list, node) {
1202 if (pos1 == tz)
1203 break;
1204 }
1205 list_for_each_entry(pos2, &thermal_cdev_list, node) {
1206 if (pos2 == cdev)
1207 break;
1208 }
1209
1210 if (tz != pos1 || cdev != pos2)
Zhang Rui203d3d42008-01-17 15:51:08 +08001211 return -EINVAL;
1212
Lukasz Majewski9a3031d2014-11-18 11:16:30 +01001213 ret = cdev->ops->get_max_state(cdev, &max_state);
1214 if (ret)
1215 return ret;
Zhang Rui9d998422012-06-26 16:35:57 +08001216
1217 /* lower default 0, upper default max_state */
1218 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
1219 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
1220
1221 if (lower > upper || upper > max_state)
1222 return -EINVAL;
1223
Zhang Rui203d3d42008-01-17 15:51:08 +08001224 dev =
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001225 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001226 if (!dev)
1227 return -ENOMEM;
1228 dev->tz = tz;
1229 dev->cdev = cdev;
1230 dev->trip = trip;
Zhang Rui9d998422012-06-26 16:35:57 +08001231 dev->upper = upper;
1232 dev->lower = lower;
Zhang Ruice119f82012-06-27 14:13:04 +08001233 dev->target = THERMAL_NO_TARGET;
Kapileshwar Singh6cd9e9f2015-02-18 16:04:21 +00001234 dev->weight = weight;
Zhang Rui74051ba2012-06-26 16:27:22 +08001235
Zhang Rui203d3d42008-01-17 15:51:08 +08001236 result = get_idr(&tz->idr, &tz->lock, &dev->id);
1237 if (result)
1238 goto free_mem;
1239
1240 sprintf(dev->name, "cdev%d", dev->id);
1241 result =
1242 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
1243 if (result)
1244 goto release_idr;
1245
1246 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
Sergey Senozhatsky975f8c52010-04-06 14:34:51 -07001247 sysfs_attr_init(&dev->attr.attr);
Zhang Rui203d3d42008-01-17 15:51:08 +08001248 dev->attr.attr.name = dev->attr_name;
1249 dev->attr.attr.mode = 0444;
1250 dev->attr.show = thermal_cooling_device_trip_point_show;
1251 result = device_create_file(&tz->device, &dev->attr);
1252 if (result)
1253 goto remove_symbol_link;
1254
Javi Merinodb916512015-02-18 16:04:24 +00001255 sprintf(dev->weight_attr_name, "cdev%d_weight", dev->id);
1256 sysfs_attr_init(&dev->weight_attr.attr);
1257 dev->weight_attr.attr.name = dev->weight_attr_name;
1258 dev->weight_attr.attr.mode = S_IWUSR | S_IRUGO;
1259 dev->weight_attr.show = thermal_cooling_device_weight_show;
1260 dev->weight_attr.store = thermal_cooling_device_weight_store;
1261 result = device_create_file(&tz->device, &dev->weight_attr);
1262 if (result)
1263 goto remove_trip_file;
1264
Zhang Rui203d3d42008-01-17 15:51:08 +08001265 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001266 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001267 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
Zhang Rui203d3d42008-01-17 15:51:08 +08001268 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
1269 result = -EEXIST;
1270 break;
1271 }
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001272 if (!result) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001273 list_add_tail(&dev->tz_node, &tz->thermal_instances);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001274 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
1275 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001276 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001277 mutex_unlock(&tz->lock);
1278
1279 if (!result)
1280 return 0;
1281
Javi Merinodb916512015-02-18 16:04:24 +00001282 device_remove_file(&tz->device, &dev->weight_attr);
1283remove_trip_file:
Zhang Rui203d3d42008-01-17 15:51:08 +08001284 device_remove_file(&tz->device, &dev->attr);
Joe Perchescaca8b82012-03-21 12:55:02 -07001285remove_symbol_link:
Zhang Rui203d3d42008-01-17 15:51:08 +08001286 sysfs_remove_link(&tz->device.kobj, dev->name);
Joe Perchescaca8b82012-03-21 12:55:02 -07001287release_idr:
Zhang Rui203d3d42008-01-17 15:51:08 +08001288 release_idr(&tz->idr, &tz->lock, dev->id);
Joe Perchescaca8b82012-03-21 12:55:02 -07001289free_mem:
Zhang Rui203d3d42008-01-17 15:51:08 +08001290 kfree(dev);
1291 return result;
1292}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001293EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001294
1295/**
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001296 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1297 * thermal zone.
1298 * @tz: pointer to a struct thermal_zone_device.
Zhang Rui203d3d42008-01-17 15:51:08 +08001299 * @trip: indicates which trip point the cooling devices is
1300 * associated with in this thermal zone.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001301 * @cdev: pointer to a struct thermal_cooling_device.
Len Brown543a9562008-02-07 16:55:08 -05001302 *
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001303 * This interface function unbind a thermal cooling device from the certain
1304 * trip point of a thermal zone device.
Len Brown543a9562008-02-07 16:55:08 -05001305 * This function is usually called in the thermal zone device .unbind callback.
Eduardo Valentin9892e5d2013-04-23 21:48:17 +00001306 *
1307 * Return: 0 on success, the proper error value otherwise.
Zhang Rui203d3d42008-01-17 15:51:08 +08001308 */
1309int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1310 int trip,
1311 struct thermal_cooling_device *cdev)
1312{
Zhang Ruib81b6ba2012-06-27 10:08:19 +08001313 struct thermal_instance *pos, *next;
Zhang Rui203d3d42008-01-17 15:51:08 +08001314
1315 mutex_lock(&tz->lock);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001316 mutex_lock(&cdev->lock);
Zhang Ruicddf31b2012-06-27 10:09:36 +08001317 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
Len Brown543a9562008-02-07 16:55:08 -05001318 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
Zhang Ruicddf31b2012-06-27 10:09:36 +08001319 list_del(&pos->tz_node);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001320 list_del(&pos->cdev_node);
Zhang Ruif4a821c2012-07-24 16:56:21 +08001321 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001322 mutex_unlock(&tz->lock);
1323 goto unbind;
1324 }
1325 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001326 mutex_unlock(&cdev->lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08001327 mutex_unlock(&tz->lock);
1328
1329 return -ENODEV;
1330
Joe Perchescaca8b82012-03-21 12:55:02 -07001331unbind:
Zhang Rui203d3d42008-01-17 15:51:08 +08001332 device_remove_file(&tz->device, &pos->attr);
1333 sysfs_remove_link(&tz->device.kobj, pos->name);
1334 release_idr(&tz->idr, &tz->lock, pos->id);
1335 kfree(pos);
1336 return 0;
1337}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001338EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
Zhang Rui203d3d42008-01-17 15:51:08 +08001339
1340static void thermal_release(struct device *dev)
1341{
1342 struct thermal_zone_device *tz;
1343 struct thermal_cooling_device *cdev;
1344
Joe Perchescaca8b82012-03-21 12:55:02 -07001345 if (!strncmp(dev_name(dev), "thermal_zone",
1346 sizeof("thermal_zone") - 1)) {
Zhang Rui203d3d42008-01-17 15:51:08 +08001347 tz = to_thermal_zone(dev);
1348 kfree(tz);
durgadoss.r@intel.com732e4c82013-10-02 00:08:00 +05301349 } else if(!strncmp(dev_name(dev), "cooling_device",
1350 sizeof("cooling_device") - 1)){
Zhang Rui203d3d42008-01-17 15:51:08 +08001351 cdev = to_cooling_device(dev);
1352 kfree(cdev);
1353 }
1354}
1355
1356static struct class thermal_class = {
1357 .name = "thermal",
1358 .dev_release = thermal_release,
1359};
1360
1361/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001362 * __thermal_cooling_device_register() - register a new thermal cooling device
1363 * @np: a pointer to a device tree node.
Zhang Rui203d3d42008-01-17 15:51:08 +08001364 * @type: the thermal cooling device type.
1365 * @devdata: device private data.
1366 * @ops: standard thermal cooling devices callbacks.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001367 *
1368 * This interface function adds a new thermal cooling device (fan/processor/...)
1369 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1370 * to all the thermal zone devices registered at the same time.
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001371 * It also gives the opportunity to link the cooling device to a device tree
1372 * node, so that it can be bound to a thermal zone created out of device tree.
Eduardo Valentin3a6eccb2013-04-23 21:48:18 +00001373 *
1374 * Return: a pointer to the created struct thermal_cooling_device or an
1375 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001376 */
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001377static struct thermal_cooling_device *
1378__thermal_cooling_device_register(struct device_node *np,
1379 char *type, void *devdata,
1380 const struct thermal_cooling_device_ops *ops)
Zhang Rui203d3d42008-01-17 15:51:08 +08001381{
1382 struct thermal_cooling_device *cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001383 int result;
1384
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001385 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001386 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001387
1388 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
Len Brown543a9562008-02-07 16:55:08 -05001389 !ops->set_cur_state)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001390 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001391
1392 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1393 if (!cdev)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001394 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001395
1396 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1397 if (result) {
1398 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001399 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001400 }
1401
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001402 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
Zhang Ruif4a821c2012-07-24 16:56:21 +08001403 mutex_init(&cdev->lock);
Zhang Ruib5e4ae62012-06-27 14:11:52 +08001404 INIT_LIST_HEAD(&cdev->thermal_instances);
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001405 cdev->np = np;
Zhang Rui203d3d42008-01-17 15:51:08 +08001406 cdev->ops = ops;
Ni Wade5ca0cce2014-02-17 11:02:55 +08001407 cdev->updated = false;
Zhang Rui203d3d42008-01-17 15:51:08 +08001408 cdev->device.class = &thermal_class;
Matthias Kaehlcke2dc10f82015-02-20 18:10:08 -08001409 cdev->device.groups = cooling_device_attr_groups;
Zhang Rui203d3d42008-01-17 15:51:08 +08001410 cdev->devdata = devdata;
Kay Sievers354655e2009-01-06 10:44:37 -08001411 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001412 result = device_register(&cdev->device);
1413 if (result) {
1414 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1415 kfree(cdev);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001416 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001417 }
1418
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301419 /* Add 'this' new cdev to the global cdev list */
Zhang Rui203d3d42008-01-17 15:51:08 +08001420 mutex_lock(&thermal_list_lock);
1421 list_add(&cdev->node, &thermal_cdev_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001422 mutex_unlock(&thermal_list_lock);
1423
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301424 /* Update binding information for 'this' new cdev */
1425 bind_cdev(cdev);
1426
1427 return cdev;
Zhang Rui203d3d42008-01-17 15:51:08 +08001428}
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001429
1430/**
1431 * thermal_cooling_device_register() - register a new thermal cooling device
1432 * @type: the thermal cooling device type.
1433 * @devdata: device private data.
1434 * @ops: standard thermal cooling devices callbacks.
1435 *
1436 * This interface function adds a new thermal cooling device (fan/processor/...)
1437 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1438 * to all the thermal zone devices registered at the same time.
1439 *
1440 * Return: a pointer to the created struct thermal_cooling_device or an
1441 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1442 */
1443struct thermal_cooling_device *
1444thermal_cooling_device_register(char *type, void *devdata,
1445 const struct thermal_cooling_device_ops *ops)
1446{
1447 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1448}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001449EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001450
1451/**
Eduardo Valentina116b5d2013-09-26 15:55:01 -04001452 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1453 * @np: a pointer to a device tree node.
1454 * @type: the thermal cooling device type.
1455 * @devdata: device private data.
1456 * @ops: standard thermal cooling devices callbacks.
1457 *
1458 * This function will register a cooling device with device tree node reference.
1459 * This interface function adds a new thermal cooling device (fan/processor/...)
1460 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1461 * to all the thermal zone devices registered at the same time.
1462 *
1463 * Return: a pointer to the created struct thermal_cooling_device or an
1464 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1465 */
1466struct thermal_cooling_device *
1467thermal_of_cooling_device_register(struct device_node *np,
1468 char *type, void *devdata,
1469 const struct thermal_cooling_device_ops *ops)
1470{
1471 return __thermal_cooling_device_register(np, type, devdata, ops);
1472}
1473EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1474
1475/**
Zhang Rui203d3d42008-01-17 15:51:08 +08001476 * thermal_cooling_device_unregister - removes the registered thermal cooling device
Zhang Rui203d3d42008-01-17 15:51:08 +08001477 * @cdev: the thermal cooling device to remove.
1478 *
1479 * thermal_cooling_device_unregister() must be called when the device is no
1480 * longer needed.
1481 */
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301482void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
Zhang Rui203d3d42008-01-17 15:51:08 +08001483{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301484 int i;
1485 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001486 struct thermal_zone_device *tz;
1487 struct thermal_cooling_device *pos = NULL;
1488
1489 if (!cdev)
1490 return;
1491
1492 mutex_lock(&thermal_list_lock);
1493 list_for_each_entry(pos, &thermal_cdev_list, node)
1494 if (pos == cdev)
1495 break;
1496 if (pos != cdev) {
1497 /* thermal cooling device not found */
1498 mutex_unlock(&thermal_list_lock);
1499 return;
1500 }
1501 list_del(&cdev->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301502
1503 /* Unbind all thermal zones associated with 'this' cdev */
Zhang Rui203d3d42008-01-17 15:51:08 +08001504 list_for_each_entry(tz, &thermal_tz_list, node) {
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301505 if (tz->ops->unbind) {
1506 tz->ops->unbind(tz, cdev);
Zhang Rui203d3d42008-01-17 15:51:08 +08001507 continue;
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301508 }
1509
1510 if (!tz->tzp || !tz->tzp->tbp)
1511 continue;
1512
1513 tzp = tz->tzp;
1514 for (i = 0; i < tzp->num_tbps; i++) {
1515 if (tzp->tbp[i].cdev == cdev) {
1516 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1517 tzp->tbp[i].cdev = NULL;
1518 }
1519 }
Zhang Rui203d3d42008-01-17 15:51:08 +08001520 }
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301521
Zhang Rui203d3d42008-01-17 15:51:08 +08001522 mutex_unlock(&thermal_list_lock);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301523
Zhang Rui203d3d42008-01-17 15:51:08 +08001524 if (cdev->type[0])
Len Brown543a9562008-02-07 16:55:08 -05001525 device_remove_file(&cdev->device, &dev_attr_cdev_type);
Zhang Rui203d3d42008-01-17 15:51:08 +08001526 device_remove_file(&cdev->device, &dev_attr_max_state);
1527 device_remove_file(&cdev->device, &dev_attr_cur_state);
1528
1529 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1530 device_unregister(&cdev->device);
1531 return;
1532}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001533EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001534
Durgadoss Rdc765482012-09-18 11:05:00 +05301535void thermal_cdev_update(struct thermal_cooling_device *cdev)
Zhang Ruice119f82012-06-27 14:13:04 +08001536{
1537 struct thermal_instance *instance;
1538 unsigned long target = 0;
1539
1540 /* cooling device is updated*/
1541 if (cdev->updated)
1542 return;
1543
Zhang Ruif4a821c2012-07-24 16:56:21 +08001544 mutex_lock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001545 /* Make sure cdev enters the deepest cooling state */
1546 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
Aaron Lu06475b52013-12-02 13:54:26 +08001547 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1548 instance->tz->id, instance->target);
Zhang Ruice119f82012-06-27 14:13:04 +08001549 if (instance->target == THERMAL_NO_TARGET)
1550 continue;
1551 if (instance->target > target)
1552 target = instance->target;
1553 }
Zhang Ruif4a821c2012-07-24 16:56:21 +08001554 mutex_unlock(&cdev->lock);
Zhang Ruice119f82012-06-27 14:13:04 +08001555 cdev->ops->set_cur_state(cdev, target);
1556 cdev->updated = true;
Punit Agrawal39811562014-07-29 11:50:49 +01001557 trace_cdev_update(cdev, target);
Aaron Lu06475b52013-12-02 13:54:26 +08001558 dev_dbg(&cdev->device, "set to state %lu\n", target);
Zhang Ruice119f82012-06-27 14:13:04 +08001559}
Durgadoss Rdc765482012-09-18 11:05:00 +05301560EXPORT_SYMBOL(thermal_cdev_update);
Zhang Ruice119f82012-06-27 14:13:04 +08001561
Matthew Garrettb1569e92008-12-03 17:55:32 +00001562/**
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001563 * thermal_notify_framework - Sensor drivers use this API to notify framework
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301564 * @tz: thermal zone device
1565 * @trip: indicates which trip point has been crossed
1566 *
1567 * This function handles the trip events from sensor drivers. It starts
1568 * throttling the cooling devices according to the policy configured.
1569 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1570 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1571 * The throttling policy is based on the configured platform data; if no
1572 * platform data is provided, this uses the step_wise throttling policy.
1573 */
Eduardo Valentin7b73c992013-04-23 21:48:14 +00001574void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301575{
1576 handle_thermal_trip(tz, trip);
1577}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001578EXPORT_SYMBOL_GPL(thermal_notify_framework);
Durgadoss Rf2b4caa2012-09-18 11:05:05 +05301579
1580/**
Eduardo Valentin269c1742013-04-23 21:48:19 +00001581 * create_trip_attrs() - create attributes for trip points
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001582 * @tz: the thermal zone device
1583 * @mask: Writeable trip point bitmap.
Eduardo Valentin269c1742013-04-23 21:48:19 +00001584 *
1585 * helper function to instantiate sysfs entries for every trip
1586 * point and its properties of a struct thermal_zone_device.
1587 *
1588 * Return: 0 on success, the proper error value otherwise.
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001589 */
1590static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1591{
1592 int indx;
Durgadoss R27365a62012-07-25 10:10:59 +08001593 int size = sizeof(struct thermal_attr) * tz->trips;
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001594
Durgadoss R27365a62012-07-25 10:10:59 +08001595 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001596 if (!tz->trip_type_attrs)
1597 return -ENOMEM;
1598
Durgadoss R27365a62012-07-25 10:10:59 +08001599 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001600 if (!tz->trip_temp_attrs) {
1601 kfree(tz->trip_type_attrs);
1602 return -ENOMEM;
1603 }
1604
Durgadoss R27365a62012-07-25 10:10:59 +08001605 if (tz->ops->get_trip_hyst) {
1606 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1607 if (!tz->trip_hyst_attrs) {
1608 kfree(tz->trip_type_attrs);
1609 kfree(tz->trip_temp_attrs);
1610 return -ENOMEM;
1611 }
1612 }
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001613
Durgadoss R27365a62012-07-25 10:10:59 +08001614
1615 for (indx = 0; indx < tz->trips; indx++) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001616 /* create trip type attribute */
1617 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1618 "trip_point_%d_type", indx);
1619
1620 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1621 tz->trip_type_attrs[indx].attr.attr.name =
1622 tz->trip_type_attrs[indx].name;
1623 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1624 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1625
1626 device_create_file(&tz->device,
1627 &tz->trip_type_attrs[indx].attr);
1628
1629 /* create trip temp attribute */
1630 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1631 "trip_point_%d_temp", indx);
1632
1633 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1634 tz->trip_temp_attrs[indx].attr.attr.name =
1635 tz->trip_temp_attrs[indx].name;
1636 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1637 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
Punit Agrawal35e94642015-03-03 10:43:03 +00001638 if (IS_ENABLED(CONFIG_THERMAL_WRITABLE_TRIPS) &&
1639 mask & (1 << indx)) {
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001640 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1641 tz->trip_temp_attrs[indx].attr.store =
1642 trip_point_temp_store;
1643 }
1644
1645 device_create_file(&tz->device,
1646 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001647
1648 /* create Optional trip hyst attribute */
1649 if (!tz->ops->get_trip_hyst)
1650 continue;
1651 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1652 "trip_point_%d_hyst", indx);
1653
1654 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1655 tz->trip_hyst_attrs[indx].attr.attr.name =
1656 tz->trip_hyst_attrs[indx].name;
1657 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1658 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1659 if (tz->ops->set_trip_hyst) {
1660 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1661 tz->trip_hyst_attrs[indx].attr.store =
1662 trip_point_hyst_store;
1663 }
1664
1665 device_create_file(&tz->device,
1666 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001667 }
1668 return 0;
1669}
1670
1671static void remove_trip_attrs(struct thermal_zone_device *tz)
1672{
1673 int indx;
1674
1675 for (indx = 0; indx < tz->trips; indx++) {
1676 device_remove_file(&tz->device,
1677 &tz->trip_type_attrs[indx].attr);
1678 device_remove_file(&tz->device,
1679 &tz->trip_temp_attrs[indx].attr);
Durgadoss R27365a62012-07-25 10:10:59 +08001680 if (tz->ops->get_trip_hyst)
1681 device_remove_file(&tz->device,
1682 &tz->trip_hyst_attrs[indx].attr);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001683 }
1684 kfree(tz->trip_type_attrs);
1685 kfree(tz->trip_temp_attrs);
Durgadoss R27365a62012-07-25 10:10:59 +08001686 kfree(tz->trip_hyst_attrs);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001687}
1688
1689/**
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001690 * thermal_zone_device_register() - register a new thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001691 * @type: the thermal zone device type
1692 * @trips: the number of trip points the thermal zone support
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001693 * @mask: a bit string indicating the writeablility of trip points
Zhang Rui203d3d42008-01-17 15:51:08 +08001694 * @devdata: private device data
1695 * @ops: standard thermal zone device callbacks
Durgadoss R50125a92012-09-18 11:04:56 +05301696 * @tzp: thermal zone platform parameters
Matthew Garrettb1569e92008-12-03 17:55:32 +00001697 * @passive_delay: number of milliseconds to wait between polls when
1698 * performing passive cooling
1699 * @polling_delay: number of milliseconds to wait between polls when checking
1700 * whether trip points have been crossed (0 for interrupt
1701 * driven systems)
Zhang Rui203d3d42008-01-17 15:51:08 +08001702 *
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001703 * This interface function adds a new thermal zone device (sensor) to
1704 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1705 * thermal cooling devices registered at the same time.
Zhang Rui203d3d42008-01-17 15:51:08 +08001706 * thermal_zone_device_unregister() must be called when the device is no
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001707 * longer needed. The passive cooling depends on the .get_trend() return value.
Eduardo Valentina00e55f2013-04-23 21:48:20 +00001708 *
1709 * Return: a pointer to the created struct thermal_zone_device or an
1710 * in case of error, an ERR_PTR. Caller must check return value with
1711 * IS_ERR*() helpers.
Zhang Rui203d3d42008-01-17 15:51:08 +08001712 */
Anton Vorontsov4b1bf582012-07-31 04:39:30 -07001713struct thermal_zone_device *thermal_zone_device_register(const char *type,
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001714 int trips, int mask, void *devdata,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04001715 struct thermal_zone_device_ops *ops,
Javi Merino6b775e82015-03-02 17:17:19 +00001716 struct thermal_zone_params *tzp,
Zhang Rui1b7ddb82012-06-27 09:51:12 +08001717 int passive_delay, int polling_delay)
Zhang Rui203d3d42008-01-17 15:51:08 +08001718{
1719 struct thermal_zone_device *tz;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001720 enum thermal_trip_type trip_type;
Zhang Rui203d3d42008-01-17 15:51:08 +08001721 int result;
1722 int count;
Matthew Garrett03a971a2008-12-03 18:00:38 +00001723 int passive = 0;
Javi Merinoe33df1d2015-02-26 19:00:27 +00001724 struct thermal_governor *governor;
Zhang Rui203d3d42008-01-17 15:51:08 +08001725
Guenter Roeck204dd1d2012-08-07 22:36:45 -07001726 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001727 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001728
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001729 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001730 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001731
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04001732 if (!ops)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001733 return ERR_PTR(-EINVAL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001734
Jonghwa Lee83720d02013-05-18 09:50:26 +00001735 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
Eduardo Valentin6b2aa512013-01-02 15:29:42 +00001736 return ERR_PTR(-EINVAL);
1737
Zhang Rui203d3d42008-01-17 15:51:08 +08001738 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1739 if (!tz)
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001740 return ERR_PTR(-ENOMEM);
Zhang Rui203d3d42008-01-17 15:51:08 +08001741
Zhang Rui2d374132012-06-27 10:09:00 +08001742 INIT_LIST_HEAD(&tz->thermal_instances);
Zhang Rui203d3d42008-01-17 15:51:08 +08001743 idr_init(&tz->idr);
1744 mutex_init(&tz->lock);
1745 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1746 if (result) {
1747 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001748 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001749 }
1750
Eduardo Valentinc7a8b9d2013-04-23 21:48:12 +00001751 strlcpy(tz->type, type ? : "", sizeof(tz->type));
Zhang Rui203d3d42008-01-17 15:51:08 +08001752 tz->ops = ops;
Durgadoss R50125a92012-09-18 11:04:56 +05301753 tz->tzp = tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001754 tz->device.class = &thermal_class;
1755 tz->devdata = devdata;
1756 tz->trips = trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +00001757 tz->passive_delay = passive_delay;
1758 tz->polling_delay = polling_delay;
1759
Kay Sievers354655e2009-01-06 10:44:37 -08001760 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
Zhang Rui203d3d42008-01-17 15:51:08 +08001761 result = device_register(&tz->device);
1762 if (result) {
1763 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1764 kfree(tz);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001765 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001766 }
1767
1768 /* sys I/F */
1769 if (type) {
1770 result = device_create_file(&tz->device, &dev_attr_type);
1771 if (result)
1772 goto unregister;
1773 }
1774
1775 result = device_create_file(&tz->device, &dev_attr_temp);
1776 if (result)
1777 goto unregister;
1778
1779 if (ops->get_mode) {
1780 result = device_create_file(&tz->device, &dev_attr_mode);
1781 if (result)
1782 goto unregister;
1783 }
1784
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001785 result = create_trip_attrs(tz, mask);
1786 if (result)
1787 goto unregister;
1788
Zhang Rui203d3d42008-01-17 15:51:08 +08001789 for (count = 0; count < trips; count++) {
Matthew Garrett03a971a2008-12-03 18:00:38 +00001790 tz->ops->get_trip_type(tz, count, &trip_type);
1791 if (trip_type == THERMAL_TRIP_PASSIVE)
1792 passive = 1;
Zhang Rui203d3d42008-01-17 15:51:08 +08001793 }
1794
Durgadoss R5a2c0902012-09-18 11:04:58 +05301795 if (!passive) {
1796 result = device_create_file(&tz->device, &dev_attr_passive);
1797 if (result)
1798 goto unregister;
1799 }
Matthew Garrett03a971a2008-12-03 18:00:38 +00001800
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +00001801#ifdef CONFIG_THERMAL_EMULATION
1802 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1803 if (result)
1804 goto unregister;
1805#endif
Durgadoss R5a2c0902012-09-18 11:04:58 +05301806 /* Create policy attribute */
1807 result = device_create_file(&tz->device, &dev_attr_policy);
Matthew Garrett03a971a2008-12-03 18:00:38 +00001808 if (result)
1809 goto unregister;
1810
Javi Merino9f382712015-03-26 15:53:02 +00001811 /* Add thermal zone params */
1812 result = create_tzp_attrs(&tz->device);
1813 if (result)
1814 goto unregister;
1815
Durgadoss Ra4a15482012-09-18 11:04:57 +05301816 /* Update 'this' zone's governor information */
1817 mutex_lock(&thermal_governor_lock);
1818
1819 if (tz->tzp)
Javi Merinoe33df1d2015-02-26 19:00:27 +00001820 governor = __find_governor(tz->tzp->governor_name);
Durgadoss Ra4a15482012-09-18 11:04:57 +05301821 else
Javi Merinoe33df1d2015-02-26 19:00:27 +00001822 governor = def_governor;
1823
1824 result = thermal_set_governor(tz, governor);
1825 if (result) {
1826 mutex_unlock(&thermal_governor_lock);
1827 goto unregister;
1828 }
Durgadoss Ra4a15482012-09-18 11:04:57 +05301829
1830 mutex_unlock(&thermal_governor_lock);
1831
Eduardo Valentinccba4ff2013-08-15 11:34:17 -04001832 if (!tz->tzp || !tz->tzp->no_hwmon) {
1833 result = thermal_add_hwmon_sysfs(tz);
1834 if (result)
1835 goto unregister;
1836 }
Zhang Ruie68b16a2008-04-21 16:07:52 +08001837
Zhang Rui203d3d42008-01-17 15:51:08 +08001838 mutex_lock(&thermal_list_lock);
1839 list_add_tail(&tz->node, &thermal_tz_list);
Zhang Rui203d3d42008-01-17 15:51:08 +08001840 mutex_unlock(&thermal_list_lock);
1841
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301842 /* Bind cooling devices for this zone */
1843 bind_tz(tz);
1844
Matthew Garrettb1569e92008-12-03 17:55:32 +00001845 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1846
Eduardo Valentin81bd4e12013-09-12 19:15:44 -04001847 if (!tz->ops->get_temp)
1848 thermal_zone_device_set_polling(tz, 0);
1849
Matthew Garrettb1569e92008-12-03 17:55:32 +00001850 thermal_zone_device_update(tz);
1851
Yao Dongdong14015862014-10-28 07:40:25 +00001852 return tz;
Zhang Rui203d3d42008-01-17 15:51:08 +08001853
Joe Perchescaca8b82012-03-21 12:55:02 -07001854unregister:
Zhang Rui203d3d42008-01-17 15:51:08 +08001855 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1856 device_unregister(&tz->device);
Thomas Sujith3e6fda52008-02-15 00:59:50 -05001857 return ERR_PTR(result);
Zhang Rui203d3d42008-01-17 15:51:08 +08001858}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001859EXPORT_SYMBOL_GPL(thermal_zone_device_register);
Zhang Rui203d3d42008-01-17 15:51:08 +08001860
1861/**
1862 * thermal_device_unregister - removes the registered thermal zone device
Zhang Rui203d3d42008-01-17 15:51:08 +08001863 * @tz: the thermal zone device to remove
1864 */
1865void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1866{
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301867 int i;
1868 const struct thermal_zone_params *tzp;
Zhang Rui203d3d42008-01-17 15:51:08 +08001869 struct thermal_cooling_device *cdev;
1870 struct thermal_zone_device *pos = NULL;
Zhang Rui203d3d42008-01-17 15:51:08 +08001871
1872 if (!tz)
1873 return;
1874
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301875 tzp = tz->tzp;
1876
Zhang Rui203d3d42008-01-17 15:51:08 +08001877 mutex_lock(&thermal_list_lock);
1878 list_for_each_entry(pos, &thermal_tz_list, node)
1879 if (pos == tz)
1880 break;
1881 if (pos != tz) {
1882 /* thermal zone device not found */
1883 mutex_unlock(&thermal_list_lock);
1884 return;
1885 }
1886 list_del(&tz->node);
Durgadoss R7e8ee1e2012-09-18 11:04:59 +05301887
1888 /* Unbind all cdevs associated with 'this' thermal zone */
1889 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1890 if (tz->ops->unbind) {
1891 tz->ops->unbind(tz, cdev);
1892 continue;
1893 }
1894
1895 if (!tzp || !tzp->tbp)
1896 break;
1897
1898 for (i = 0; i < tzp->num_tbps; i++) {
1899 if (tzp->tbp[i].cdev == cdev) {
1900 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1901 tzp->tbp[i].cdev = NULL;
1902 }
1903 }
1904 }
1905
Zhang Rui203d3d42008-01-17 15:51:08 +08001906 mutex_unlock(&thermal_list_lock);
1907
Matthew Garrettb1569e92008-12-03 17:55:32 +00001908 thermal_zone_device_set_polling(tz, 0);
1909
Zhang Rui203d3d42008-01-17 15:51:08 +08001910 if (tz->type[0])
1911 device_remove_file(&tz->device, &dev_attr_type);
1912 device_remove_file(&tz->device, &dev_attr_temp);
1913 if (tz->ops->get_mode)
1914 device_remove_file(&tz->device, &dev_attr_mode);
Durgadoss R5a2c0902012-09-18 11:04:58 +05301915 device_remove_file(&tz->device, &dev_attr_policy);
Durgadoss Rc56f5c02012-07-25 10:10:58 +08001916 remove_trip_attrs(tz);
Javi Merinoe33df1d2015-02-26 19:00:27 +00001917 thermal_set_governor(tz, NULL);
Zhang Rui203d3d42008-01-17 15:51:08 +08001918
Zhang Ruie68b16a2008-04-21 16:07:52 +08001919 thermal_remove_hwmon_sysfs(tz);
Zhang Rui203d3d42008-01-17 15:51:08 +08001920 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1921 idr_destroy(&tz->idr);
1922 mutex_destroy(&tz->lock);
1923 device_unregister(&tz->device);
1924 return;
1925}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00001926EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
Zhang Rui203d3d42008-01-17 15:51:08 +08001927
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001928/**
1929 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1930 * @name: thermal zone name to fetch the temperature
1931 *
1932 * When only one zone is found with the passed name, returns a reference to it.
1933 *
1934 * Return: On success returns a reference to an unique thermal zone with
1935 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1936 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1937 */
1938struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1939{
1940 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1941 unsigned int found = 0;
1942
1943 if (!name)
1944 goto exit;
1945
1946 mutex_lock(&thermal_list_lock);
1947 list_for_each_entry(pos, &thermal_tz_list, node)
Rasmus Villemoes484ac2f2014-10-13 15:55:01 -07001948 if (!strncasecmp(name, pos->type, THERMAL_NAME_LENGTH)) {
Eduardo Valentin63c4d912013-04-05 12:32:28 +00001949 found++;
1950 ref = pos;
1951 }
1952 mutex_unlock(&thermal_list_lock);
1953
1954 /* nothing has been found, thus an error code for it */
1955 if (found == 0)
1956 ref = ERR_PTR(-ENODEV);
1957 else if (found > 1)
1958 /* Success only when an unique zone is found */
1959 ref = ERR_PTR(-EEXIST);
1960
1961exit:
1962 return ref;
1963}
1964EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1965
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001966#ifdef CONFIG_NET
Johannes Berg2a94fe42013-11-19 15:19:39 +01001967static const struct genl_multicast_group thermal_event_mcgrps[] = {
1968 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
1969};
1970
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001971static struct genl_family thermal_event_genl_family = {
1972 .id = GENL_ID_GENERATE,
1973 .name = THERMAL_GENL_FAMILY_NAME,
1974 .version = THERMAL_GENL_VERSION,
1975 .maxattr = THERMAL_GENL_ATTR_MAX,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001976 .mcgrps = thermal_event_mcgrps,
1977 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01001978};
1979
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001980int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1981 enum events event)
R.Durgadoss4cb18722010-10-27 03:33:29 +05301982{
1983 struct sk_buff *skb;
1984 struct nlattr *attr;
1985 struct thermal_genl_event *thermal_event;
1986 void *msg_header;
1987 int size;
1988 int result;
Fabio Estevamb11de072012-03-21 12:55:00 -07001989 static unsigned int thermal_event_seqnum;
R.Durgadoss4cb18722010-10-27 03:33:29 +05301990
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00001991 if (!tz)
1992 return -EINVAL;
1993
R.Durgadoss4cb18722010-10-27 03:33:29 +05301994 /* allocate memory */
Joe Perches886ee542012-03-21 12:55:01 -07001995 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1996 nla_total_size(0);
R.Durgadoss4cb18722010-10-27 03:33:29 +05301997
1998 skb = genlmsg_new(size, GFP_ATOMIC);
1999 if (!skb)
2000 return -ENOMEM;
2001
2002 /* add the genetlink message header */
2003 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
2004 &thermal_event_genl_family, 0,
2005 THERMAL_GENL_CMD_EVENT);
2006 if (!msg_header) {
2007 nlmsg_free(skb);
2008 return -ENOMEM;
2009 }
2010
2011 /* fill the data */
Joe Perches886ee542012-03-21 12:55:01 -07002012 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
2013 sizeof(struct thermal_genl_event));
R.Durgadoss4cb18722010-10-27 03:33:29 +05302014
2015 if (!attr) {
2016 nlmsg_free(skb);
2017 return -EINVAL;
2018 }
2019
2020 thermal_event = nla_data(attr);
2021 if (!thermal_event) {
2022 nlmsg_free(skb);
2023 return -EINVAL;
2024 }
2025
2026 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
2027
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +00002028 thermal_event->orig = tz->id;
R.Durgadoss4cb18722010-10-27 03:33:29 +05302029 thermal_event->event = event;
2030
2031 /* send multicast genetlink message */
Johannes Berg053c0952015-01-16 22:09:00 +01002032 genlmsg_end(skb, msg_header);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302033
Johannes Berg68eb5502013-11-19 15:19:38 +01002034 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01002035 0, GFP_ATOMIC);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302036 if (result)
Eduardo Valentin923e0b12013-01-02 15:29:41 +00002037 dev_err(&tz->device, "Failed to send netlink event:%d", result);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302038
2039 return result;
2040}
Eduardo Valentin910cb1e2013-04-23 21:48:15 +00002041EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302042
2043static int genetlink_init(void)
2044{
Johannes Berg2a94fe42013-11-19 15:19:39 +01002045 return genl_register_family(&thermal_event_genl_family);
R.Durgadoss4cb18722010-10-27 03:33:29 +05302046}
2047
Rafael J. Wysockiaf062162011-03-01 01:12:19 +01002048static void genetlink_exit(void)
2049{
2050 genl_unregister_family(&thermal_event_genl_family);
2051}
2052#else /* !CONFIG_NET */
2053static inline int genetlink_init(void) { return 0; }
2054static inline void genetlink_exit(void) {}
2055#endif /* !CONFIG_NET */
2056
Zhang Rui80a26a52013-03-26 16:38:29 +08002057static int __init thermal_register_governors(void)
2058{
2059 int result;
2060
2061 result = thermal_gov_step_wise_register();
2062 if (result)
2063 return result;
2064
2065 result = thermal_gov_fair_share_register();
2066 if (result)
2067 return result;
2068
Peter Feuerere4dbf982014-07-22 17:37:13 +02002069 result = thermal_gov_bang_bang_register();
2070 if (result)
2071 return result;
2072
Javi Merino6b775e82015-03-02 17:17:19 +00002073 result = thermal_gov_user_space_register();
2074 if (result)
2075 return result;
2076
2077 return thermal_gov_power_allocator_register();
Zhang Rui80a26a52013-03-26 16:38:29 +08002078}
2079
2080static void thermal_unregister_governors(void)
2081{
2082 thermal_gov_step_wise_unregister();
2083 thermal_gov_fair_share_unregister();
Peter Feuerere4dbf982014-07-22 17:37:13 +02002084 thermal_gov_bang_bang_unregister();
Zhang Rui80a26a52013-03-26 16:38:29 +08002085 thermal_gov_user_space_unregister();
Javi Merino6b775e82015-03-02 17:17:19 +00002086 thermal_gov_power_allocator_unregister();
Zhang Rui80a26a52013-03-26 16:38:29 +08002087}
2088
Zhang Rui203d3d42008-01-17 15:51:08 +08002089static int __init thermal_init(void)
2090{
Zhang Rui80a26a52013-03-26 16:38:29 +08002091 int result;
2092
2093 result = thermal_register_governors();
2094 if (result)
2095 goto error;
Zhang Rui203d3d42008-01-17 15:51:08 +08002096
2097 result = class_register(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08002098 if (result)
2099 goto unregister_governors;
2100
R.Durgadoss4cb18722010-10-27 03:33:29 +05302101 result = genetlink_init();
Zhang Rui80a26a52013-03-26 16:38:29 +08002102 if (result)
2103 goto unregister_class;
2104
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002105 result = of_parse_thermal_zones();
2106 if (result)
2107 goto exit_netlink;
2108
Zhang Rui80a26a52013-03-26 16:38:29 +08002109 return 0;
2110
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002111exit_netlink:
2112 genetlink_exit();
Zhang Rui80a26a52013-03-26 16:38:29 +08002113unregister_class:
2114 class_unregister(&thermal_class);
Luis Henriques9d367e52014-12-03 21:20:21 +00002115unregister_governors:
2116 thermal_unregister_governors();
Zhang Rui80a26a52013-03-26 16:38:29 +08002117error:
2118 idr_destroy(&thermal_tz_idr);
2119 idr_destroy(&thermal_cdev_idr);
2120 mutex_destroy(&thermal_idr_lock);
2121 mutex_destroy(&thermal_list_lock);
2122 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08002123 return result;
2124}
2125
2126static void __exit thermal_exit(void)
2127{
Eduardo Valentin4e5e4702013-07-03 15:35:39 -04002128 of_thermal_destroy_zones();
Zhang Rui80a26a52013-03-26 16:38:29 +08002129 genetlink_exit();
Zhang Rui203d3d42008-01-17 15:51:08 +08002130 class_unregister(&thermal_class);
Zhang Rui80a26a52013-03-26 16:38:29 +08002131 thermal_unregister_governors();
Zhang Rui203d3d42008-01-17 15:51:08 +08002132 idr_destroy(&thermal_tz_idr);
2133 idr_destroy(&thermal_cdev_idr);
2134 mutex_destroy(&thermal_idr_lock);
2135 mutex_destroy(&thermal_list_lock);
Zhang Rui80a26a52013-03-26 16:38:29 +08002136 mutex_destroy(&thermal_governor_lock);
Zhang Rui203d3d42008-01-17 15:51:08 +08002137}
2138
R.Durgadoss4cb18722010-10-27 03:33:29 +05302139fs_initcall(thermal_init);
Zhang Rui203d3d42008-01-17 15:51:08 +08002140module_exit(thermal_exit);