thermal: trace: Trace when a cooling device's state is updated
[deliverable/linux.git] / drivers / thermal / thermal_core.c
CommitLineData
203d3d4a
ZR
1/*
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
c5a01dd5
JP
26#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
203d3d4a
ZR
28#include <linux/module.h>
29#include <linux/device.h>
30#include <linux/err.h>
5a0e3ad6 31#include <linux/slab.h>
203d3d4a
ZR
32#include <linux/kdev_t.h>
33#include <linux/idr.h>
34#include <linux/thermal.h>
b1569e99 35#include <linux/reboot.h>
42a5bf50 36#include <linux/string.h>
a116b5d4 37#include <linux/of.h>
4cb18728
D
38#include <net/netlink.h>
39#include <net/genetlink.h>
203d3d4a 40
100a8fdb
PA
41#define CREATE_TRACE_POINTS
42#include <trace/events/thermal.h>
43
71350db4 44#include "thermal_core.h"
0dd88793 45#include "thermal_hwmon.h"
71350db4 46
63c4ec90 47MODULE_AUTHOR("Zhang Rui");
203d3d4a 48MODULE_DESCRIPTION("Generic thermal management sysfs support");
6d8d4974 49MODULE_LICENSE("GPL v2");
203d3d4a 50
203d3d4a
ZR
51static 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);
a4a15485
D
57static LIST_HEAD(thermal_governor_list);
58
203d3d4a 59static DEFINE_MUTEX(thermal_list_lock);
a4a15485
D
60static DEFINE_MUTEX(thermal_governor_lock);
61
f2234bcd
ZR
62static struct thermal_governor *def_governor;
63
a4a15485
D
64static struct thermal_governor *__find_governor(const char *name)
65{
66 struct thermal_governor *pos;
67
f2234bcd
ZR
68 if (!name || !name[0])
69 return def_governor;
70
a4a15485
D
71 list_for_each_entry(pos, &thermal_governor_list, governor_list)
72 if (!strnicmp(name, pos->name, THERMAL_NAME_LENGTH))
73 return pos;
74
75 return NULL;
76}
77
78int thermal_register_governor(struct thermal_governor *governor)
79{
80 int err;
81 const char *name;
82 struct thermal_zone_device *pos;
83
84 if (!governor)
85 return -EINVAL;
86
87 mutex_lock(&thermal_governor_lock);
88
89 err = -EBUSY;
90 if (__find_governor(governor->name) == NULL) {
91 err = 0;
92 list_add(&governor->governor_list, &thermal_governor_list);
f2234bcd
ZR
93 if (!def_governor && !strncmp(governor->name,
94 DEFAULT_THERMAL_GOVERNOR, THERMAL_NAME_LENGTH))
95 def_governor = governor;
a4a15485
D
96 }
97
98 mutex_lock(&thermal_list_lock);
99
100 list_for_each_entry(pos, &thermal_tz_list, node) {
f2234bcd
ZR
101 /*
102 * only thermal zones with specified tz->tzp->governor_name
103 * may run with tz->govenor unset
104 */
a4a15485
D
105 if (pos->governor)
106 continue;
f2234bcd
ZR
107
108 name = pos->tzp->governor_name;
109
a4a15485
D
110 if (!strnicmp(name, governor->name, THERMAL_NAME_LENGTH))
111 pos->governor = governor;
112 }
113
114 mutex_unlock(&thermal_list_lock);
115 mutex_unlock(&thermal_governor_lock);
116
117 return err;
118}
a4a15485
D
119
120void thermal_unregister_governor(struct thermal_governor *governor)
121{
122 struct thermal_zone_device *pos;
123
124 if (!governor)
125 return;
126
127 mutex_lock(&thermal_governor_lock);
128
129 if (__find_governor(governor->name) == NULL)
130 goto exit;
131
132 mutex_lock(&thermal_list_lock);
133
134 list_for_each_entry(pos, &thermal_tz_list, node) {
135 if (!strnicmp(pos->governor->name, governor->name,
136 THERMAL_NAME_LENGTH))
137 pos->governor = NULL;
138 }
139
140 mutex_unlock(&thermal_list_lock);
141 list_del(&governor->governor_list);
142exit:
143 mutex_unlock(&thermal_governor_lock);
144 return;
145}
203d3d4a
ZR
146
147static int get_idr(struct idr *idr, struct mutex *lock, int *id)
148{
6deb69fa 149 int ret;
203d3d4a
ZR
150
151 if (lock)
152 mutex_lock(lock);
6deb69fa 153 ret = idr_alloc(idr, NULL, 0, 0, GFP_KERNEL);
203d3d4a
ZR
154 if (lock)
155 mutex_unlock(lock);
6deb69fa
TH
156 if (unlikely(ret < 0))
157 return ret;
158 *id = ret;
203d3d4a
ZR
159 return 0;
160}
161
162static void release_idr(struct idr *idr, struct mutex *lock, int id)
163{
164 if (lock)
165 mutex_lock(lock);
166 idr_remove(idr, id);
167 if (lock)
168 mutex_unlock(lock);
169}
170
9b4298a0
D
171int get_tz_trend(struct thermal_zone_device *tz, int trip)
172{
173 enum thermal_trend trend;
174
0c872507
EV
175 if (tz->emul_temperature || !tz->ops->get_trend ||
176 tz->ops->get_trend(tz, trip, &trend)) {
9b4298a0
D
177 if (tz->temperature > tz->last_temperature)
178 trend = THERMAL_TREND_RAISING;
179 else if (tz->temperature < tz->last_temperature)
180 trend = THERMAL_TREND_DROPPING;
181 else
182 trend = THERMAL_TREND_STABLE;
183 }
184
185 return trend;
186}
187EXPORT_SYMBOL(get_tz_trend);
188
189struct thermal_instance *get_thermal_instance(struct thermal_zone_device *tz,
190 struct thermal_cooling_device *cdev, int trip)
191{
192 struct thermal_instance *pos = NULL;
193 struct thermal_instance *target_instance = NULL;
194
195 mutex_lock(&tz->lock);
196 mutex_lock(&cdev->lock);
197
198 list_for_each_entry(pos, &tz->thermal_instances, tz_node) {
199 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
200 target_instance = pos;
201 break;
202 }
203 }
204
205 mutex_unlock(&cdev->lock);
206 mutex_unlock(&tz->lock);
207
208 return target_instance;
209}
210EXPORT_SYMBOL(get_thermal_instance);
211
7e8ee1e9
D
212static void print_bind_err_msg(struct thermal_zone_device *tz,
213 struct thermal_cooling_device *cdev, int ret)
214{
215 dev_err(&tz->device, "binding zone %s with cdev %s failed:%d\n",
216 tz->type, cdev->type, ret);
217}
218
219static void __bind(struct thermal_zone_device *tz, int mask,
a8892d83
EV
220 struct thermal_cooling_device *cdev,
221 unsigned long *limits)
7e8ee1e9
D
222{
223 int i, ret;
224
225 for (i = 0; i < tz->trips; i++) {
226 if (mask & (1 << i)) {
a8892d83
EV
227 unsigned long upper, lower;
228
229 upper = THERMAL_NO_LIMIT;
230 lower = THERMAL_NO_LIMIT;
231 if (limits) {
232 lower = limits[i * 2];
233 upper = limits[i * 2 + 1];
234 }
7e8ee1e9 235 ret = thermal_zone_bind_cooling_device(tz, i, cdev,
a8892d83 236 upper, lower);
7e8ee1e9
D
237 if (ret)
238 print_bind_err_msg(tz, cdev, ret);
239 }
240 }
241}
242
243static void __unbind(struct thermal_zone_device *tz, int mask,
244 struct thermal_cooling_device *cdev)
245{
246 int i;
247
248 for (i = 0; i < tz->trips; i++)
249 if (mask & (1 << i))
250 thermal_zone_unbind_cooling_device(tz, i, cdev);
251}
252
253static void bind_cdev(struct thermal_cooling_device *cdev)
254{
255 int i, ret;
256 const struct thermal_zone_params *tzp;
257 struct thermal_zone_device *pos = NULL;
258
259 mutex_lock(&thermal_list_lock);
260
261 list_for_each_entry(pos, &thermal_tz_list, node) {
262 if (!pos->tzp && !pos->ops->bind)
263 continue;
264
a9f2d19b 265 if (pos->ops->bind) {
7e8ee1e9
D
266 ret = pos->ops->bind(pos, cdev);
267 if (ret)
268 print_bind_err_msg(pos, cdev, ret);
a9f2d19b 269 continue;
7e8ee1e9
D
270 }
271
272 tzp = pos->tzp;
791700cd
HD
273 if (!tzp || !tzp->tbp)
274 continue;
7e8ee1e9
D
275
276 for (i = 0; i < tzp->num_tbps; i++) {
277 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
278 continue;
279 if (tzp->tbp[i].match(pos, cdev))
280 continue;
281 tzp->tbp[i].cdev = cdev;
a8892d83
EV
282 __bind(pos, tzp->tbp[i].trip_mask, cdev,
283 tzp->tbp[i].binding_limits);
7e8ee1e9
D
284 }
285 }
286
287 mutex_unlock(&thermal_list_lock);
288}
289
290static void bind_tz(struct thermal_zone_device *tz)
291{
292 int i, ret;
293 struct thermal_cooling_device *pos = NULL;
294 const struct thermal_zone_params *tzp = tz->tzp;
295
296 if (!tzp && !tz->ops->bind)
297 return;
298
299 mutex_lock(&thermal_list_lock);
300
a9f2d19b
NW
301 /* If there is ops->bind, try to use ops->bind */
302 if (tz->ops->bind) {
7e8ee1e9
D
303 list_for_each_entry(pos, &thermal_cdev_list, node) {
304 ret = tz->ops->bind(tz, pos);
305 if (ret)
306 print_bind_err_msg(tz, pos, ret);
307 }
308 goto exit;
309 }
310
791700cd 311 if (!tzp || !tzp->tbp)
7e8ee1e9
D
312 goto exit;
313
314 list_for_each_entry(pos, &thermal_cdev_list, node) {
315 for (i = 0; i < tzp->num_tbps; i++) {
316 if (tzp->tbp[i].cdev || !tzp->tbp[i].match)
317 continue;
318 if (tzp->tbp[i].match(tz, pos))
319 continue;
320 tzp->tbp[i].cdev = pos;
a8892d83
EV
321 __bind(tz, tzp->tbp[i].trip_mask, pos,
322 tzp->tbp[i].binding_limits);
7e8ee1e9
D
323 }
324 }
325exit:
326 mutex_unlock(&thermal_list_lock);
327}
328
0c01ebbf
D
329static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
330 int delay)
331{
332 if (delay > 1000)
333 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
334 round_jiffies(msecs_to_jiffies(delay)));
335 else if (delay)
336 mod_delayed_work(system_freezable_wq, &tz->poll_queue,
337 msecs_to_jiffies(delay));
338 else
339 cancel_delayed_work(&tz->poll_queue);
340}
341
342static void monitor_thermal_zone(struct thermal_zone_device *tz)
343{
344 mutex_lock(&tz->lock);
345
346 if (tz->passive)
347 thermal_zone_device_set_polling(tz, tz->passive_delay);
348 else if (tz->polling_delay)
349 thermal_zone_device_set_polling(tz, tz->polling_delay);
350 else
351 thermal_zone_device_set_polling(tz, 0);
352
353 mutex_unlock(&tz->lock);
354}
355
356static void handle_non_critical_trips(struct thermal_zone_device *tz,
357 int trip, enum thermal_trip_type trip_type)
358{
f2234bcd
ZR
359 tz->governor ? tz->governor->throttle(tz, trip) :
360 def_governor->throttle(tz, trip);
0c01ebbf
D
361}
362
363static void handle_critical_trips(struct thermal_zone_device *tz,
364 int trip, enum thermal_trip_type trip_type)
365{
366 long trip_temp;
367
368 tz->ops->get_trip_temp(tz, trip, &trip_temp);
369
370 /* If we have not crossed the trip_temp, we do not care. */
371 if (tz->temperature < trip_temp)
372 return;
373
374 if (tz->ops->notify)
375 tz->ops->notify(tz, trip, trip_type);
376
377 if (trip_type == THERMAL_TRIP_CRITICAL) {
923e0b1e
EV
378 dev_emerg(&tz->device,
379 "critical temperature reached(%d C),shutting down\n",
380 tz->temperature / 1000);
0c01ebbf
D
381 orderly_poweroff(true);
382 }
383}
384
385static void handle_thermal_trip(struct thermal_zone_device *tz, int trip)
386{
387 enum thermal_trip_type type;
388
389 tz->ops->get_trip_type(tz, trip, &type);
390
391 if (type == THERMAL_TRIP_CRITICAL || type == THERMAL_TRIP_HOT)
392 handle_critical_trips(tz, trip, type);
393 else
394 handle_non_critical_trips(tz, trip, type);
395 /*
396 * Alright, we handled this trip successfully.
397 * So, start monitoring again.
398 */
399 monitor_thermal_zone(tz);
400}
401
837b26bb
EV
402/**
403 * thermal_zone_get_temp() - returns its the temperature of thermal zone
404 * @tz: a valid pointer to a struct thermal_zone_device
405 * @temp: a valid pointer to where to store the resulting temperature.
406 *
407 * When a valid thermal zone reference is passed, it will fetch its
408 * temperature and fill @temp.
409 *
410 * Return: On success returns 0, an error code otherwise
411 */
412int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp)
e6e238c3 413{
837b26bb 414 int ret = -EINVAL;
5e20b2e5
ZR
415#ifdef CONFIG_THERMAL_EMULATION
416 int count;
e6e238c3
ADK
417 unsigned long crit_temp = -1UL;
418 enum thermal_trip_type type;
5e20b2e5 419#endif
e6e238c3 420
81bd4e1c 421 if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
837b26bb
EV
422 goto exit;
423
e6e238c3
ADK
424 mutex_lock(&tz->lock);
425
426 ret = tz->ops->get_temp(tz, temp);
427#ifdef CONFIG_THERMAL_EMULATION
428 if (!tz->emul_temperature)
429 goto skip_emul;
430
431 for (count = 0; count < tz->trips; count++) {
432 ret = tz->ops->get_trip_type(tz, count, &type);
433 if (!ret && type == THERMAL_TRIP_CRITICAL) {
434 ret = tz->ops->get_trip_temp(tz, count, &crit_temp);
435 break;
436 }
437 }
438
439 if (ret)
440 goto skip_emul;
441
442 if (*temp < crit_temp)
443 *temp = tz->emul_temperature;
444skip_emul:
445#endif
446 mutex_unlock(&tz->lock);
837b26bb 447exit:
e6e238c3
ADK
448 return ret;
449}
837b26bb 450EXPORT_SYMBOL_GPL(thermal_zone_get_temp);
e6e238c3 451
0c01ebbf
D
452static void update_temperature(struct thermal_zone_device *tz)
453{
454 long temp;
455 int ret;
456
e6e238c3 457 ret = thermal_zone_get_temp(tz, &temp);
0c01ebbf 458 if (ret) {
923e0b1e
EV
459 dev_warn(&tz->device, "failed to read out thermal zone %d\n",
460 tz->id);
e6e238c3 461 return;
0c01ebbf
D
462 }
463
e6e238c3 464 mutex_lock(&tz->lock);
0c01ebbf
D
465 tz->last_temperature = tz->temperature;
466 tz->temperature = temp;
0c01ebbf 467 mutex_unlock(&tz->lock);
06475b55 468
100a8fdb 469 trace_thermal_temperature(tz);
06475b55
AL
470 dev_dbg(&tz->device, "last_temperature=%d, current_temperature=%d\n",
471 tz->last_temperature, tz->temperature);
0c01ebbf
D
472}
473
474void thermal_zone_device_update(struct thermal_zone_device *tz)
475{
476 int count;
477
81bd4e1c
EV
478 if (!tz->ops->get_temp)
479 return;
480
0c01ebbf
D
481 update_temperature(tz);
482
483 for (count = 0; count < tz->trips; count++)
484 handle_thermal_trip(tz, count);
485}
910cb1e3 486EXPORT_SYMBOL_GPL(thermal_zone_device_update);
0c01ebbf
D
487
488static void thermal_zone_device_check(struct work_struct *work)
489{
490 struct thermal_zone_device *tz = container_of(work, struct
491 thermal_zone_device,
492 poll_queue.work);
493 thermal_zone_device_update(tz);
494}
495
203d3d4a
ZR
496/* sys I/F for thermal zone */
497
498#define to_thermal_zone(_dev) \
499 container_of(_dev, struct thermal_zone_device, device)
500
501static ssize_t
502type_show(struct device *dev, struct device_attribute *attr, char *buf)
503{
504 struct thermal_zone_device *tz = to_thermal_zone(dev);
505
506 return sprintf(buf, "%s\n", tz->type);
507}
508
509static ssize_t
510temp_show(struct device *dev, struct device_attribute *attr, char *buf)
511{
512 struct thermal_zone_device *tz = to_thermal_zone(dev);
6503e5df
MG
513 long temperature;
514 int ret;
203d3d4a 515
e6e238c3 516 ret = thermal_zone_get_temp(tz, &temperature);
6503e5df
MG
517
518 if (ret)
519 return ret;
520
521 return sprintf(buf, "%ld\n", temperature);
203d3d4a
ZR
522}
523
524static ssize_t
525mode_show(struct device *dev, struct device_attribute *attr, char *buf)
526{
527 struct thermal_zone_device *tz = to_thermal_zone(dev);
6503e5df
MG
528 enum thermal_device_mode mode;
529 int result;
203d3d4a
ZR
530
531 if (!tz->ops->get_mode)
532 return -EPERM;
533
6503e5df
MG
534 result = tz->ops->get_mode(tz, &mode);
535 if (result)
536 return result;
537
538 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
539 : "disabled");
203d3d4a
ZR
540}
541
542static ssize_t
543mode_store(struct device *dev, struct device_attribute *attr,
544 const char *buf, size_t count)
545{
546 struct thermal_zone_device *tz = to_thermal_zone(dev);
547 int result;
548
549 if (!tz->ops->set_mode)
550 return -EPERM;
551
f1f0e2ac 552 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
6503e5df 553 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
f1f0e2ac 554 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
6503e5df
MG
555 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
556 else
557 result = -EINVAL;
558
203d3d4a
ZR
559 if (result)
560 return result;
561
562 return count;
563}
564
565static ssize_t
566trip_point_type_show(struct device *dev, struct device_attribute *attr,
567 char *buf)
568{
569 struct thermal_zone_device *tz = to_thermal_zone(dev);
6503e5df
MG
570 enum thermal_trip_type type;
571 int trip, result;
203d3d4a
ZR
572
573 if (!tz->ops->get_trip_type)
574 return -EPERM;
575
576 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
577 return -EINVAL;
578
6503e5df
MG
579 result = tz->ops->get_trip_type(tz, trip, &type);
580 if (result)
581 return result;
582
583 switch (type) {
584 case THERMAL_TRIP_CRITICAL:
625120a4 585 return sprintf(buf, "critical\n");
6503e5df 586 case THERMAL_TRIP_HOT:
625120a4 587 return sprintf(buf, "hot\n");
6503e5df 588 case THERMAL_TRIP_PASSIVE:
625120a4 589 return sprintf(buf, "passive\n");
6503e5df 590 case THERMAL_TRIP_ACTIVE:
625120a4 591 return sprintf(buf, "active\n");
6503e5df 592 default:
625120a4 593 return sprintf(buf, "unknown\n");
6503e5df 594 }
203d3d4a
ZR
595}
596
c56f5c03
D
597static ssize_t
598trip_point_temp_store(struct device *dev, struct device_attribute *attr,
599 const char *buf, size_t count)
600{
601 struct thermal_zone_device *tz = to_thermal_zone(dev);
602 int trip, ret;
603 unsigned long temperature;
604
605 if (!tz->ops->set_trip_temp)
606 return -EPERM;
607
608 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
609 return -EINVAL;
610
611 if (kstrtoul(buf, 10, &temperature))
612 return -EINVAL;
613
614 ret = tz->ops->set_trip_temp(tz, trip, temperature);
615
616 return ret ? ret : count;
617}
618
203d3d4a
ZR
619static ssize_t
620trip_point_temp_show(struct device *dev, struct device_attribute *attr,
621 char *buf)
622{
623 struct thermal_zone_device *tz = to_thermal_zone(dev);
6503e5df
MG
624 int trip, ret;
625 long temperature;
203d3d4a
ZR
626
627 if (!tz->ops->get_trip_temp)
628 return -EPERM;
629
630 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
631 return -EINVAL;
632
6503e5df
MG
633 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
634
635 if (ret)
636 return ret;
637
638 return sprintf(buf, "%ld\n", temperature);
203d3d4a
ZR
639}
640
27365a6c
D
641static ssize_t
642trip_point_hyst_store(struct device *dev, struct device_attribute *attr,
643 const char *buf, size_t count)
644{
645 struct thermal_zone_device *tz = to_thermal_zone(dev);
646 int trip, ret;
647 unsigned long temperature;
648
649 if (!tz->ops->set_trip_hyst)
650 return -EPERM;
651
652 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
653 return -EINVAL;
654
655 if (kstrtoul(buf, 10, &temperature))
656 return -EINVAL;
657
658 /*
659 * We are not doing any check on the 'temperature' value
660 * here. The driver implementing 'set_trip_hyst' has to
661 * take care of this.
662 */
663 ret = tz->ops->set_trip_hyst(tz, trip, temperature);
664
665 return ret ? ret : count;
666}
667
668static ssize_t
669trip_point_hyst_show(struct device *dev, struct device_attribute *attr,
670 char *buf)
671{
672 struct thermal_zone_device *tz = to_thermal_zone(dev);
673 int trip, ret;
674 unsigned long temperature;
675
676 if (!tz->ops->get_trip_hyst)
677 return -EPERM;
678
679 if (!sscanf(attr->attr.name, "trip_point_%d_hyst", &trip))
680 return -EINVAL;
681
682 ret = tz->ops->get_trip_hyst(tz, trip, &temperature);
683
684 return ret ? ret : sprintf(buf, "%ld\n", temperature);
685}
686
03a971a2
MG
687static ssize_t
688passive_store(struct device *dev, struct device_attribute *attr,
689 const char *buf, size_t count)
690{
691 struct thermal_zone_device *tz = to_thermal_zone(dev);
692 struct thermal_cooling_device *cdev = NULL;
693 int state;
694
695 if (!sscanf(buf, "%d\n", &state))
696 return -EINVAL;
697
3d8e3ad8
FP
698 /* sanity check: values below 1000 millicelcius don't make sense
699 * and can cause the system to go into a thermal heart attack
700 */
701 if (state && state < 1000)
702 return -EINVAL;
703
03a971a2
MG
704 if (state && !tz->forced_passive) {
705 mutex_lock(&thermal_list_lock);
706 list_for_each_entry(cdev, &thermal_cdev_list, node) {
707 if (!strncmp("Processor", cdev->type,
708 sizeof("Processor")))
709 thermal_zone_bind_cooling_device(tz,
9d99842f
ZR
710 THERMAL_TRIPS_NONE, cdev,
711 THERMAL_NO_LIMIT,
712 THERMAL_NO_LIMIT);
03a971a2
MG
713 }
714 mutex_unlock(&thermal_list_lock);
e4143b03
FP
715 if (!tz->passive_delay)
716 tz->passive_delay = 1000;
03a971a2
MG
717 } else if (!state && tz->forced_passive) {
718 mutex_lock(&thermal_list_lock);
719 list_for_each_entry(cdev, &thermal_cdev_list, node) {
720 if (!strncmp("Processor", cdev->type,
721 sizeof("Processor")))
722 thermal_zone_unbind_cooling_device(tz,
723 THERMAL_TRIPS_NONE,
724 cdev);
725 }
726 mutex_unlock(&thermal_list_lock);
e4143b03 727 tz->passive_delay = 0;
03a971a2
MG
728 }
729
03a971a2
MG
730 tz->forced_passive = state;
731
732 thermal_zone_device_update(tz);
733
734 return count;
735}
736
737static ssize_t
738passive_show(struct device *dev, struct device_attribute *attr,
739 char *buf)
740{
741 struct thermal_zone_device *tz = to_thermal_zone(dev);
742
743 return sprintf(buf, "%d\n", tz->forced_passive);
744}
745
5a2c090b
D
746static ssize_t
747policy_store(struct device *dev, struct device_attribute *attr,
748 const char *buf, size_t count)
749{
750 int ret = -EINVAL;
751 struct thermal_zone_device *tz = to_thermal_zone(dev);
752 struct thermal_governor *gov;
42a5bf50
AS
753 char name[THERMAL_NAME_LENGTH];
754
755 snprintf(name, sizeof(name), "%s", buf);
5a2c090b
D
756
757 mutex_lock(&thermal_governor_lock);
758
42a5bf50 759 gov = __find_governor(strim(name));
5a2c090b
D
760 if (!gov)
761 goto exit;
762
763 tz->governor = gov;
764 ret = count;
765
766exit:
767 mutex_unlock(&thermal_governor_lock);
768 return ret;
769}
770
771static ssize_t
772policy_show(struct device *dev, struct device_attribute *devattr, char *buf)
773{
774 struct thermal_zone_device *tz = to_thermal_zone(dev);
775
776 return sprintf(buf, "%s\n", tz->governor->name);
777}
778
e6e238c3
ADK
779#ifdef CONFIG_THERMAL_EMULATION
780static ssize_t
781emul_temp_store(struct device *dev, struct device_attribute *attr,
782 const char *buf, size_t count)
783{
784 struct thermal_zone_device *tz = to_thermal_zone(dev);
785 int ret = 0;
786 unsigned long temperature;
787
788 if (kstrtoul(buf, 10, &temperature))
789 return -EINVAL;
790
791 if (!tz->ops->set_emul_temp) {
792 mutex_lock(&tz->lock);
793 tz->emul_temperature = temperature;
794 mutex_unlock(&tz->lock);
795 } else {
796 ret = tz->ops->set_emul_temp(tz, temperature);
797 }
798
800744bf
T
799 if (!ret)
800 thermal_zone_device_update(tz);
801
e6e238c3
ADK
802 return ret ? ret : count;
803}
804static DEVICE_ATTR(emul_temp, S_IWUSR, NULL, emul_temp_store);
805#endif/*CONFIG_THERMAL_EMULATION*/
806
203d3d4a
ZR
807static DEVICE_ATTR(type, 0444, type_show, NULL);
808static DEVICE_ATTR(temp, 0444, temp_show, NULL);
809static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
886ee546 810static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
5a2c090b 811static DEVICE_ATTR(policy, S_IRUGO | S_IWUSR, policy_show, policy_store);
203d3d4a 812
203d3d4a
ZR
813/* sys I/F for cooling device */
814#define to_cooling_device(_dev) \
815 container_of(_dev, struct thermal_cooling_device, device)
816
817static ssize_t
818thermal_cooling_device_type_show(struct device *dev,
819 struct device_attribute *attr, char *buf)
820{
821 struct thermal_cooling_device *cdev = to_cooling_device(dev);
822
823 return sprintf(buf, "%s\n", cdev->type);
824}
825
826static ssize_t
827thermal_cooling_device_max_state_show(struct device *dev,
828 struct device_attribute *attr, char *buf)
829{
830 struct thermal_cooling_device *cdev = to_cooling_device(dev);
6503e5df
MG
831 unsigned long state;
832 int ret;
203d3d4a 833
6503e5df
MG
834 ret = cdev->ops->get_max_state(cdev, &state);
835 if (ret)
836 return ret;
837 return sprintf(buf, "%ld\n", state);
203d3d4a
ZR
838}
839
840static ssize_t
841thermal_cooling_device_cur_state_show(struct device *dev,
842 struct device_attribute *attr, char *buf)
843{
844 struct thermal_cooling_device *cdev = to_cooling_device(dev);
6503e5df
MG
845 unsigned long state;
846 int ret;
203d3d4a 847
6503e5df
MG
848 ret = cdev->ops->get_cur_state(cdev, &state);
849 if (ret)
850 return ret;
851 return sprintf(buf, "%ld\n", state);
203d3d4a
ZR
852}
853
854static ssize_t
855thermal_cooling_device_cur_state_store(struct device *dev,
856 struct device_attribute *attr,
857 const char *buf, size_t count)
858{
859 struct thermal_cooling_device *cdev = to_cooling_device(dev);
6503e5df 860 unsigned long state;
203d3d4a
ZR
861 int result;
862
6503e5df 863 if (!sscanf(buf, "%ld\n", &state))
203d3d4a
ZR
864 return -EINVAL;
865
edb94918 866 if ((long)state < 0)
203d3d4a
ZR
867 return -EINVAL;
868
869 result = cdev->ops->set_cur_state(cdev, state);
870 if (result)
871 return result;
872 return count;
873}
874
875static struct device_attribute dev_attr_cdev_type =
543a9561 876__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
203d3d4a
ZR
877static DEVICE_ATTR(max_state, 0444,
878 thermal_cooling_device_max_state_show, NULL);
879static DEVICE_ATTR(cur_state, 0644,
880 thermal_cooling_device_cur_state_show,
881 thermal_cooling_device_cur_state_store);
882
883static ssize_t
884thermal_cooling_device_trip_point_show(struct device *dev,
543a9561 885 struct device_attribute *attr, char *buf)
203d3d4a 886{
b81b6ba3 887 struct thermal_instance *instance;
203d3d4a
ZR
888
889 instance =
b81b6ba3 890 container_of(attr, struct thermal_instance, attr);
203d3d4a
ZR
891
892 if (instance->trip == THERMAL_TRIPS_NONE)
893 return sprintf(buf, "-1\n");
894 else
895 return sprintf(buf, "%d\n", instance->trip);
896}
897
898/* Device management */
899
900/**
d2e4eb83
EV
901 * thermal_zone_bind_cooling_device() - bind a cooling device to a thermal zone
902 * @tz: pointer to struct thermal_zone_device
203d3d4a
ZR
903 * @trip: indicates which trip point the cooling devices is
904 * associated with in this thermal zone.
d2e4eb83
EV
905 * @cdev: pointer to struct thermal_cooling_device
906 * @upper: the Maximum cooling state for this trip point.
907 * THERMAL_NO_LIMIT means no upper limit,
908 * and the cooling device can be in max_state.
909 * @lower: the Minimum cooling state can be used for this trip point.
910 * THERMAL_NO_LIMIT means no lower limit,
911 * and the cooling device can be in cooling state 0.
543a9561 912 *
d2e4eb83
EV
913 * This interface function bind a thermal cooling device to the certain trip
914 * point of a thermal zone device.
543a9561 915 * This function is usually called in the thermal zone device .bind callback.
d2e4eb83
EV
916 *
917 * Return: 0 on success, the proper error value otherwise.
203d3d4a
ZR
918 */
919int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
920 int trip,
9d99842f
ZR
921 struct thermal_cooling_device *cdev,
922 unsigned long upper, unsigned long lower)
203d3d4a 923{
b81b6ba3
ZR
924 struct thermal_instance *dev;
925 struct thermal_instance *pos;
c7516709
TS
926 struct thermal_zone_device *pos1;
927 struct thermal_cooling_device *pos2;
74051ba5 928 unsigned long max_state;
203d3d4a
ZR
929 int result;
930
543a9561 931 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
203d3d4a
ZR
932 return -EINVAL;
933
c7516709
TS
934 list_for_each_entry(pos1, &thermal_tz_list, node) {
935 if (pos1 == tz)
936 break;
937 }
938 list_for_each_entry(pos2, &thermal_cdev_list, node) {
939 if (pos2 == cdev)
940 break;
941 }
942
943 if (tz != pos1 || cdev != pos2)
203d3d4a
ZR
944 return -EINVAL;
945
9d99842f
ZR
946 cdev->ops->get_max_state(cdev, &max_state);
947
948 /* lower default 0, upper default max_state */
949 lower = lower == THERMAL_NO_LIMIT ? 0 : lower;
950 upper = upper == THERMAL_NO_LIMIT ? max_state : upper;
951
952 if (lower > upper || upper > max_state)
953 return -EINVAL;
954
203d3d4a 955 dev =
b81b6ba3 956 kzalloc(sizeof(struct thermal_instance), GFP_KERNEL);
203d3d4a
ZR
957 if (!dev)
958 return -ENOMEM;
959 dev->tz = tz;
960 dev->cdev = cdev;
961 dev->trip = trip;
9d99842f
ZR
962 dev->upper = upper;
963 dev->lower = lower;
ce119f83 964 dev->target = THERMAL_NO_TARGET;
74051ba5 965
203d3d4a
ZR
966 result = get_idr(&tz->idr, &tz->lock, &dev->id);
967 if (result)
968 goto free_mem;
969
970 sprintf(dev->name, "cdev%d", dev->id);
971 result =
972 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
973 if (result)
974 goto release_idr;
975
976 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
975f8c56 977 sysfs_attr_init(&dev->attr.attr);
203d3d4a
ZR
978 dev->attr.attr.name = dev->attr_name;
979 dev->attr.attr.mode = 0444;
980 dev->attr.show = thermal_cooling_device_trip_point_show;
981 result = device_create_file(&tz->device, &dev->attr);
982 if (result)
983 goto remove_symbol_link;
984
985 mutex_lock(&tz->lock);
f4a821ce 986 mutex_lock(&cdev->lock);
cddf31b3 987 list_for_each_entry(pos, &tz->thermal_instances, tz_node)
203d3d4a
ZR
988 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
989 result = -EEXIST;
990 break;
991 }
b5e4ae62 992 if (!result) {
cddf31b3 993 list_add_tail(&dev->tz_node, &tz->thermal_instances);
b5e4ae62
ZR
994 list_add_tail(&dev->cdev_node, &cdev->thermal_instances);
995 }
f4a821ce 996 mutex_unlock(&cdev->lock);
203d3d4a
ZR
997 mutex_unlock(&tz->lock);
998
999 if (!result)
1000 return 0;
1001
1002 device_remove_file(&tz->device, &dev->attr);
caca8b80 1003remove_symbol_link:
203d3d4a 1004 sysfs_remove_link(&tz->device.kobj, dev->name);
caca8b80 1005release_idr:
203d3d4a 1006 release_idr(&tz->idr, &tz->lock, dev->id);
caca8b80 1007free_mem:
203d3d4a
ZR
1008 kfree(dev);
1009 return result;
1010}
910cb1e3 1011EXPORT_SYMBOL_GPL(thermal_zone_bind_cooling_device);
203d3d4a
ZR
1012
1013/**
9892e5dc
EV
1014 * thermal_zone_unbind_cooling_device() - unbind a cooling device from a
1015 * thermal zone.
1016 * @tz: pointer to a struct thermal_zone_device.
203d3d4a
ZR
1017 * @trip: indicates which trip point the cooling devices is
1018 * associated with in this thermal zone.
9892e5dc 1019 * @cdev: pointer to a struct thermal_cooling_device.
543a9561 1020 *
9892e5dc
EV
1021 * This interface function unbind a thermal cooling device from the certain
1022 * trip point of a thermal zone device.
543a9561 1023 * This function is usually called in the thermal zone device .unbind callback.
9892e5dc
EV
1024 *
1025 * Return: 0 on success, the proper error value otherwise.
203d3d4a
ZR
1026 */
1027int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
1028 int trip,
1029 struct thermal_cooling_device *cdev)
1030{
b81b6ba3 1031 struct thermal_instance *pos, *next;
203d3d4a
ZR
1032
1033 mutex_lock(&tz->lock);
f4a821ce 1034 mutex_lock(&cdev->lock);
cddf31b3 1035 list_for_each_entry_safe(pos, next, &tz->thermal_instances, tz_node) {
543a9561 1036 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
cddf31b3 1037 list_del(&pos->tz_node);
b5e4ae62 1038 list_del(&pos->cdev_node);
f4a821ce 1039 mutex_unlock(&cdev->lock);
203d3d4a
ZR
1040 mutex_unlock(&tz->lock);
1041 goto unbind;
1042 }
1043 }
f4a821ce 1044 mutex_unlock(&cdev->lock);
203d3d4a
ZR
1045 mutex_unlock(&tz->lock);
1046
1047 return -ENODEV;
1048
caca8b80 1049unbind:
203d3d4a
ZR
1050 device_remove_file(&tz->device, &pos->attr);
1051 sysfs_remove_link(&tz->device.kobj, pos->name);
1052 release_idr(&tz->idr, &tz->lock, pos->id);
1053 kfree(pos);
1054 return 0;
1055}
910cb1e3 1056EXPORT_SYMBOL_GPL(thermal_zone_unbind_cooling_device);
203d3d4a
ZR
1057
1058static void thermal_release(struct device *dev)
1059{
1060 struct thermal_zone_device *tz;
1061 struct thermal_cooling_device *cdev;
1062
caca8b80
JP
1063 if (!strncmp(dev_name(dev), "thermal_zone",
1064 sizeof("thermal_zone") - 1)) {
203d3d4a
ZR
1065 tz = to_thermal_zone(dev);
1066 kfree(tz);
732e4c8d 1067 } else if(!strncmp(dev_name(dev), "cooling_device",
1068 sizeof("cooling_device") - 1)){
203d3d4a
ZR
1069 cdev = to_cooling_device(dev);
1070 kfree(cdev);
1071 }
1072}
1073
1074static struct class thermal_class = {
1075 .name = "thermal",
1076 .dev_release = thermal_release,
1077};
1078
1079/**
a116b5d4
EV
1080 * __thermal_cooling_device_register() - register a new thermal cooling device
1081 * @np: a pointer to a device tree node.
203d3d4a
ZR
1082 * @type: the thermal cooling device type.
1083 * @devdata: device private data.
1084 * @ops: standard thermal cooling devices callbacks.
3a6eccb3
EV
1085 *
1086 * This interface function adds a new thermal cooling device (fan/processor/...)
1087 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1088 * to all the thermal zone devices registered at the same time.
a116b5d4
EV
1089 * It also gives the opportunity to link the cooling device to a device tree
1090 * node, so that it can be bound to a thermal zone created out of device tree.
3a6eccb3
EV
1091 *
1092 * Return: a pointer to the created struct thermal_cooling_device or an
1093 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
203d3d4a 1094 */
a116b5d4
EV
1095static struct thermal_cooling_device *
1096__thermal_cooling_device_register(struct device_node *np,
1097 char *type, void *devdata,
1098 const struct thermal_cooling_device_ops *ops)
203d3d4a
ZR
1099{
1100 struct thermal_cooling_device *cdev;
203d3d4a
ZR
1101 int result;
1102
204dd1d3 1103 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
3e6fda5c 1104 return ERR_PTR(-EINVAL);
203d3d4a
ZR
1105
1106 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
543a9561 1107 !ops->set_cur_state)
3e6fda5c 1108 return ERR_PTR(-EINVAL);
203d3d4a
ZR
1109
1110 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
1111 if (!cdev)
3e6fda5c 1112 return ERR_PTR(-ENOMEM);
203d3d4a
ZR
1113
1114 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
1115 if (result) {
1116 kfree(cdev);
3e6fda5c 1117 return ERR_PTR(result);
203d3d4a
ZR
1118 }
1119
c7a8b9d9 1120 strlcpy(cdev->type, type ? : "", sizeof(cdev->type));
f4a821ce 1121 mutex_init(&cdev->lock);
b5e4ae62 1122 INIT_LIST_HEAD(&cdev->thermal_instances);
a116b5d4 1123 cdev->np = np;
203d3d4a 1124 cdev->ops = ops;
5ca0cce5 1125 cdev->updated = false;
203d3d4a
ZR
1126 cdev->device.class = &thermal_class;
1127 cdev->devdata = devdata;
354655ea 1128 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
203d3d4a
ZR
1129 result = device_register(&cdev->device);
1130 if (result) {
1131 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1132 kfree(cdev);
3e6fda5c 1133 return ERR_PTR(result);
203d3d4a
ZR
1134 }
1135
1136 /* sys I/F */
1137 if (type) {
543a9561 1138 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
203d3d4a
ZR
1139 if (result)
1140 goto unregister;
1141 }
1142
1143 result = device_create_file(&cdev->device, &dev_attr_max_state);
1144 if (result)
1145 goto unregister;
1146
1147 result = device_create_file(&cdev->device, &dev_attr_cur_state);
1148 if (result)
1149 goto unregister;
1150
7e8ee1e9 1151 /* Add 'this' new cdev to the global cdev list */
203d3d4a
ZR
1152 mutex_lock(&thermal_list_lock);
1153 list_add(&cdev->node, &thermal_cdev_list);
203d3d4a
ZR
1154 mutex_unlock(&thermal_list_lock);
1155
7e8ee1e9
D
1156 /* Update binding information for 'this' new cdev */
1157 bind_cdev(cdev);
1158
1159 return cdev;
203d3d4a 1160
caca8b80 1161unregister:
203d3d4a
ZR
1162 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1163 device_unregister(&cdev->device);
3e6fda5c 1164 return ERR_PTR(result);
203d3d4a 1165}
a116b5d4
EV
1166
1167/**
1168 * thermal_cooling_device_register() - register a new thermal cooling device
1169 * @type: the thermal cooling device type.
1170 * @devdata: device private data.
1171 * @ops: standard thermal cooling devices callbacks.
1172 *
1173 * This interface function adds a new thermal cooling device (fan/processor/...)
1174 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1175 * to all the thermal zone devices registered at the same time.
1176 *
1177 * Return: a pointer to the created struct thermal_cooling_device or an
1178 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1179 */
1180struct thermal_cooling_device *
1181thermal_cooling_device_register(char *type, void *devdata,
1182 const struct thermal_cooling_device_ops *ops)
1183{
1184 return __thermal_cooling_device_register(NULL, type, devdata, ops);
1185}
910cb1e3 1186EXPORT_SYMBOL_GPL(thermal_cooling_device_register);
203d3d4a 1187
a116b5d4
EV
1188/**
1189 * thermal_of_cooling_device_register() - register an OF thermal cooling device
1190 * @np: a pointer to a device tree node.
1191 * @type: the thermal cooling device type.
1192 * @devdata: device private data.
1193 * @ops: standard thermal cooling devices callbacks.
1194 *
1195 * This function will register a cooling device with device tree node reference.
1196 * This interface function adds a new thermal cooling device (fan/processor/...)
1197 * to /sys/class/thermal/ folder as cooling_device[0-*]. It tries to bind itself
1198 * to all the thermal zone devices registered at the same time.
1199 *
1200 * Return: a pointer to the created struct thermal_cooling_device or an
1201 * ERR_PTR. Caller must check return value with IS_ERR*() helpers.
1202 */
1203struct thermal_cooling_device *
1204thermal_of_cooling_device_register(struct device_node *np,
1205 char *type, void *devdata,
1206 const struct thermal_cooling_device_ops *ops)
1207{
1208 return __thermal_cooling_device_register(np, type, devdata, ops);
1209}
1210EXPORT_SYMBOL_GPL(thermal_of_cooling_device_register);
1211
203d3d4a
ZR
1212/**
1213 * thermal_cooling_device_unregister - removes the registered thermal cooling device
203d3d4a
ZR
1214 * @cdev: the thermal cooling device to remove.
1215 *
1216 * thermal_cooling_device_unregister() must be called when the device is no
1217 * longer needed.
1218 */
7e8ee1e9 1219void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
203d3d4a 1220{
7e8ee1e9
D
1221 int i;
1222 const struct thermal_zone_params *tzp;
203d3d4a
ZR
1223 struct thermal_zone_device *tz;
1224 struct thermal_cooling_device *pos = NULL;
1225
1226 if (!cdev)
1227 return;
1228
1229 mutex_lock(&thermal_list_lock);
1230 list_for_each_entry(pos, &thermal_cdev_list, node)
1231 if (pos == cdev)
1232 break;
1233 if (pos != cdev) {
1234 /* thermal cooling device not found */
1235 mutex_unlock(&thermal_list_lock);
1236 return;
1237 }
1238 list_del(&cdev->node);
7e8ee1e9
D
1239
1240 /* Unbind all thermal zones associated with 'this' cdev */
203d3d4a 1241 list_for_each_entry(tz, &thermal_tz_list, node) {
7e8ee1e9
D
1242 if (tz->ops->unbind) {
1243 tz->ops->unbind(tz, cdev);
1244 continue;
1245 }
1246
1247 if (!tz->tzp || !tz->tzp->tbp)
203d3d4a 1248 continue;
7e8ee1e9
D
1249
1250 tzp = tz->tzp;
1251 for (i = 0; i < tzp->num_tbps; i++) {
1252 if (tzp->tbp[i].cdev == cdev) {
1253 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1254 tzp->tbp[i].cdev = NULL;
1255 }
1256 }
203d3d4a 1257 }
7e8ee1e9 1258
203d3d4a 1259 mutex_unlock(&thermal_list_lock);
7e8ee1e9 1260
203d3d4a 1261 if (cdev->type[0])
543a9561 1262 device_remove_file(&cdev->device, &dev_attr_cdev_type);
203d3d4a
ZR
1263 device_remove_file(&cdev->device, &dev_attr_max_state);
1264 device_remove_file(&cdev->device, &dev_attr_cur_state);
1265
1266 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
1267 device_unregister(&cdev->device);
1268 return;
1269}
910cb1e3 1270EXPORT_SYMBOL_GPL(thermal_cooling_device_unregister);
203d3d4a 1271
dc765482 1272void thermal_cdev_update(struct thermal_cooling_device *cdev)
ce119f83
ZR
1273{
1274 struct thermal_instance *instance;
1275 unsigned long target = 0;
1276
1277 /* cooling device is updated*/
1278 if (cdev->updated)
1279 return;
1280
f4a821ce 1281 mutex_lock(&cdev->lock);
ce119f83
ZR
1282 /* Make sure cdev enters the deepest cooling state */
1283 list_for_each_entry(instance, &cdev->thermal_instances, cdev_node) {
06475b55
AL
1284 dev_dbg(&cdev->device, "zone%d->target=%lu\n",
1285 instance->tz->id, instance->target);
ce119f83
ZR
1286 if (instance->target == THERMAL_NO_TARGET)
1287 continue;
1288 if (instance->target > target)
1289 target = instance->target;
1290 }
f4a821ce 1291 mutex_unlock(&cdev->lock);
ce119f83
ZR
1292 cdev->ops->set_cur_state(cdev, target);
1293 cdev->updated = true;
39811569 1294 trace_cdev_update(cdev, target);
06475b55 1295 dev_dbg(&cdev->device, "set to state %lu\n", target);
ce119f83 1296}
dc765482 1297EXPORT_SYMBOL(thermal_cdev_update);
ce119f83 1298
f2b4caaf 1299/**
7b73c993 1300 * thermal_notify_framework - Sensor drivers use this API to notify framework
f2b4caaf
D
1301 * @tz: thermal zone device
1302 * @trip: indicates which trip point has been crossed
1303 *
1304 * This function handles the trip events from sensor drivers. It starts
1305 * throttling the cooling devices according to the policy configured.
1306 * For CRITICAL and HOT trip points, this notifies the respective drivers,
1307 * and does actual throttling for other trip points i.e ACTIVE and PASSIVE.
1308 * The throttling policy is based on the configured platform data; if no
1309 * platform data is provided, this uses the step_wise throttling policy.
1310 */
7b73c993 1311void thermal_notify_framework(struct thermal_zone_device *tz, int trip)
f2b4caaf
D
1312{
1313 handle_thermal_trip(tz, trip);
1314}
910cb1e3 1315EXPORT_SYMBOL_GPL(thermal_notify_framework);
f2b4caaf 1316
c56f5c03 1317/**
269c174f 1318 * create_trip_attrs() - create attributes for trip points
c56f5c03
D
1319 * @tz: the thermal zone device
1320 * @mask: Writeable trip point bitmap.
269c174f
EV
1321 *
1322 * helper function to instantiate sysfs entries for every trip
1323 * point and its properties of a struct thermal_zone_device.
1324 *
1325 * Return: 0 on success, the proper error value otherwise.
c56f5c03
D
1326 */
1327static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1328{
1329 int indx;
27365a6c 1330 int size = sizeof(struct thermal_attr) * tz->trips;
c56f5c03 1331
27365a6c 1332 tz->trip_type_attrs = kzalloc(size, GFP_KERNEL);
c56f5c03
D
1333 if (!tz->trip_type_attrs)
1334 return -ENOMEM;
1335
27365a6c 1336 tz->trip_temp_attrs = kzalloc(size, GFP_KERNEL);
c56f5c03
D
1337 if (!tz->trip_temp_attrs) {
1338 kfree(tz->trip_type_attrs);
1339 return -ENOMEM;
1340 }
1341
27365a6c
D
1342 if (tz->ops->get_trip_hyst) {
1343 tz->trip_hyst_attrs = kzalloc(size, GFP_KERNEL);
1344 if (!tz->trip_hyst_attrs) {
1345 kfree(tz->trip_type_attrs);
1346 kfree(tz->trip_temp_attrs);
1347 return -ENOMEM;
1348 }
1349 }
c56f5c03 1350
27365a6c
D
1351
1352 for (indx = 0; indx < tz->trips; indx++) {
c56f5c03
D
1353 /* create trip type attribute */
1354 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1355 "trip_point_%d_type", indx);
1356
1357 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1358 tz->trip_type_attrs[indx].attr.attr.name =
1359 tz->trip_type_attrs[indx].name;
1360 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1361 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1362
1363 device_create_file(&tz->device,
1364 &tz->trip_type_attrs[indx].attr);
1365
1366 /* create trip temp attribute */
1367 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1368 "trip_point_%d_temp", indx);
1369
1370 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1371 tz->trip_temp_attrs[indx].attr.attr.name =
1372 tz->trip_temp_attrs[indx].name;
1373 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1374 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1375 if (mask & (1 << indx)) {
1376 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1377 tz->trip_temp_attrs[indx].attr.store =
1378 trip_point_temp_store;
1379 }
1380
1381 device_create_file(&tz->device,
1382 &tz->trip_temp_attrs[indx].attr);
27365a6c
D
1383
1384 /* create Optional trip hyst attribute */
1385 if (!tz->ops->get_trip_hyst)
1386 continue;
1387 snprintf(tz->trip_hyst_attrs[indx].name, THERMAL_NAME_LENGTH,
1388 "trip_point_%d_hyst", indx);
1389
1390 sysfs_attr_init(&tz->trip_hyst_attrs[indx].attr.attr);
1391 tz->trip_hyst_attrs[indx].attr.attr.name =
1392 tz->trip_hyst_attrs[indx].name;
1393 tz->trip_hyst_attrs[indx].attr.attr.mode = S_IRUGO;
1394 tz->trip_hyst_attrs[indx].attr.show = trip_point_hyst_show;
1395 if (tz->ops->set_trip_hyst) {
1396 tz->trip_hyst_attrs[indx].attr.attr.mode |= S_IWUSR;
1397 tz->trip_hyst_attrs[indx].attr.store =
1398 trip_point_hyst_store;
1399 }
1400
1401 device_create_file(&tz->device,
1402 &tz->trip_hyst_attrs[indx].attr);
c56f5c03
D
1403 }
1404 return 0;
1405}
1406
1407static void remove_trip_attrs(struct thermal_zone_device *tz)
1408{
1409 int indx;
1410
1411 for (indx = 0; indx < tz->trips; indx++) {
1412 device_remove_file(&tz->device,
1413 &tz->trip_type_attrs[indx].attr);
1414 device_remove_file(&tz->device,
1415 &tz->trip_temp_attrs[indx].attr);
27365a6c
D
1416 if (tz->ops->get_trip_hyst)
1417 device_remove_file(&tz->device,
1418 &tz->trip_hyst_attrs[indx].attr);
c56f5c03
D
1419 }
1420 kfree(tz->trip_type_attrs);
1421 kfree(tz->trip_temp_attrs);
27365a6c 1422 kfree(tz->trip_hyst_attrs);
c56f5c03
D
1423}
1424
203d3d4a 1425/**
a00e55f9 1426 * thermal_zone_device_register() - register a new thermal zone device
203d3d4a
ZR
1427 * @type: the thermal zone device type
1428 * @trips: the number of trip points the thermal zone support
c56f5c03 1429 * @mask: a bit string indicating the writeablility of trip points
203d3d4a
ZR
1430 * @devdata: private device data
1431 * @ops: standard thermal zone device callbacks
50125a9b 1432 * @tzp: thermal zone platform parameters
b1569e99
MG
1433 * @passive_delay: number of milliseconds to wait between polls when
1434 * performing passive cooling
1435 * @polling_delay: number of milliseconds to wait between polls when checking
1436 * whether trip points have been crossed (0 for interrupt
1437 * driven systems)
203d3d4a 1438 *
a00e55f9
EV
1439 * This interface function adds a new thermal zone device (sensor) to
1440 * /sys/class/thermal folder as thermal_zone[0-*]. It tries to bind all the
1441 * thermal cooling devices registered at the same time.
203d3d4a 1442 * thermal_zone_device_unregister() must be called when the device is no
1b7ddb84 1443 * longer needed. The passive cooling depends on the .get_trend() return value.
a00e55f9
EV
1444 *
1445 * Return: a pointer to the created struct thermal_zone_device or an
1446 * in case of error, an ERR_PTR. Caller must check return value with
1447 * IS_ERR*() helpers.
203d3d4a 1448 */
4b1bf587 1449struct thermal_zone_device *thermal_zone_device_register(const char *type,
c56f5c03 1450 int trips, int mask, void *devdata,
4e5e4705 1451 struct thermal_zone_device_ops *ops,
50125a9b 1452 const struct thermal_zone_params *tzp,
1b7ddb84 1453 int passive_delay, int polling_delay)
203d3d4a
ZR
1454{
1455 struct thermal_zone_device *tz;
03a971a2 1456 enum thermal_trip_type trip_type;
203d3d4a
ZR
1457 int result;
1458 int count;
03a971a2 1459 int passive = 0;
203d3d4a 1460
204dd1d3 1461 if (type && strlen(type) >= THERMAL_NAME_LENGTH)
3e6fda5c 1462 return ERR_PTR(-EINVAL);
203d3d4a 1463
c56f5c03 1464 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
3e6fda5c 1465 return ERR_PTR(-EINVAL);
203d3d4a 1466
81bd4e1c 1467 if (!ops)
3e6fda5c 1468 return ERR_PTR(-EINVAL);
203d3d4a 1469
83720d0b 1470 if (trips > 0 && (!ops->get_trip_type || !ops->get_trip_temp))
6b2aa51d
EV
1471 return ERR_PTR(-EINVAL);
1472
203d3d4a
ZR
1473 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1474 if (!tz)
3e6fda5c 1475 return ERR_PTR(-ENOMEM);
203d3d4a 1476
2d374139 1477 INIT_LIST_HEAD(&tz->thermal_instances);
203d3d4a
ZR
1478 idr_init(&tz->idr);
1479 mutex_init(&tz->lock);
1480 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1481 if (result) {
1482 kfree(tz);
3e6fda5c 1483 return ERR_PTR(result);
203d3d4a
ZR
1484 }
1485
c7a8b9d9 1486 strlcpy(tz->type, type ? : "", sizeof(tz->type));
203d3d4a 1487 tz->ops = ops;
50125a9b 1488 tz->tzp = tzp;
203d3d4a
ZR
1489 tz->device.class = &thermal_class;
1490 tz->devdata = devdata;
1491 tz->trips = trips;
b1569e99
MG
1492 tz->passive_delay = passive_delay;
1493 tz->polling_delay = polling_delay;
1494
354655ea 1495 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
203d3d4a
ZR
1496 result = device_register(&tz->device);
1497 if (result) {
1498 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1499 kfree(tz);
3e6fda5c 1500 return ERR_PTR(result);
203d3d4a
ZR
1501 }
1502
1503 /* sys I/F */
1504 if (type) {
1505 result = device_create_file(&tz->device, &dev_attr_type);
1506 if (result)
1507 goto unregister;
1508 }
1509
1510 result = device_create_file(&tz->device, &dev_attr_temp);
1511 if (result)
1512 goto unregister;
1513
1514 if (ops->get_mode) {
1515 result = device_create_file(&tz->device, &dev_attr_mode);
1516 if (result)
1517 goto unregister;
1518 }
1519
c56f5c03
D
1520 result = create_trip_attrs(tz, mask);
1521 if (result)
1522 goto unregister;
1523
203d3d4a 1524 for (count = 0; count < trips; count++) {
03a971a2
MG
1525 tz->ops->get_trip_type(tz, count, &trip_type);
1526 if (trip_type == THERMAL_TRIP_PASSIVE)
1527 passive = 1;
203d3d4a
ZR
1528 }
1529
5a2c090b
D
1530 if (!passive) {
1531 result = device_create_file(&tz->device, &dev_attr_passive);
1532 if (result)
1533 goto unregister;
1534 }
03a971a2 1535
e6e238c3
ADK
1536#ifdef CONFIG_THERMAL_EMULATION
1537 result = device_create_file(&tz->device, &dev_attr_emul_temp);
1538 if (result)
1539 goto unregister;
1540#endif
5a2c090b
D
1541 /* Create policy attribute */
1542 result = device_create_file(&tz->device, &dev_attr_policy);
03a971a2
MG
1543 if (result)
1544 goto unregister;
1545
a4a15485
D
1546 /* Update 'this' zone's governor information */
1547 mutex_lock(&thermal_governor_lock);
1548
1549 if (tz->tzp)
1550 tz->governor = __find_governor(tz->tzp->governor_name);
1551 else
f2234bcd 1552 tz->governor = def_governor;
a4a15485
D
1553
1554 mutex_unlock(&thermal_governor_lock);
1555
ccba4ffd
EV
1556 if (!tz->tzp || !tz->tzp->no_hwmon) {
1557 result = thermal_add_hwmon_sysfs(tz);
1558 if (result)
1559 goto unregister;
1560 }
e68b16ab 1561
203d3d4a
ZR
1562 mutex_lock(&thermal_list_lock);
1563 list_add_tail(&tz->node, &thermal_tz_list);
203d3d4a
ZR
1564 mutex_unlock(&thermal_list_lock);
1565
7e8ee1e9
D
1566 /* Bind cooling devices for this zone */
1567 bind_tz(tz);
1568
b1569e99
MG
1569 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1570
81bd4e1c
EV
1571 if (!tz->ops->get_temp)
1572 thermal_zone_device_set_polling(tz, 0);
1573
b1569e99
MG
1574 thermal_zone_device_update(tz);
1575
203d3d4a
ZR
1576 if (!result)
1577 return tz;
1578
caca8b80 1579unregister:
203d3d4a
ZR
1580 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1581 device_unregister(&tz->device);
3e6fda5c 1582 return ERR_PTR(result);
203d3d4a 1583}
910cb1e3 1584EXPORT_SYMBOL_GPL(thermal_zone_device_register);
203d3d4a
ZR
1585
1586/**
1587 * thermal_device_unregister - removes the registered thermal zone device
203d3d4a
ZR
1588 * @tz: the thermal zone device to remove
1589 */
1590void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1591{
7e8ee1e9
D
1592 int i;
1593 const struct thermal_zone_params *tzp;
203d3d4a
ZR
1594 struct thermal_cooling_device *cdev;
1595 struct thermal_zone_device *pos = NULL;
203d3d4a
ZR
1596
1597 if (!tz)
1598 return;
1599
7e8ee1e9
D
1600 tzp = tz->tzp;
1601
203d3d4a
ZR
1602 mutex_lock(&thermal_list_lock);
1603 list_for_each_entry(pos, &thermal_tz_list, node)
1604 if (pos == tz)
1605 break;
1606 if (pos != tz) {
1607 /* thermal zone device not found */
1608 mutex_unlock(&thermal_list_lock);
1609 return;
1610 }
1611 list_del(&tz->node);
7e8ee1e9
D
1612
1613 /* Unbind all cdevs associated with 'this' thermal zone */
1614 list_for_each_entry(cdev, &thermal_cdev_list, node) {
1615 if (tz->ops->unbind) {
1616 tz->ops->unbind(tz, cdev);
1617 continue;
1618 }
1619
1620 if (!tzp || !tzp->tbp)
1621 break;
1622
1623 for (i = 0; i < tzp->num_tbps; i++) {
1624 if (tzp->tbp[i].cdev == cdev) {
1625 __unbind(tz, tzp->tbp[i].trip_mask, cdev);
1626 tzp->tbp[i].cdev = NULL;
1627 }
1628 }
1629 }
1630
203d3d4a
ZR
1631 mutex_unlock(&thermal_list_lock);
1632
b1569e99
MG
1633 thermal_zone_device_set_polling(tz, 0);
1634
203d3d4a
ZR
1635 if (tz->type[0])
1636 device_remove_file(&tz->device, &dev_attr_type);
1637 device_remove_file(&tz->device, &dev_attr_temp);
1638 if (tz->ops->get_mode)
1639 device_remove_file(&tz->device, &dev_attr_mode);
5a2c090b 1640 device_remove_file(&tz->device, &dev_attr_policy);
c56f5c03 1641 remove_trip_attrs(tz);
a4a15485 1642 tz->governor = NULL;
203d3d4a 1643
e68b16ab 1644 thermal_remove_hwmon_sysfs(tz);
203d3d4a
ZR
1645 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1646 idr_destroy(&tz->idr);
1647 mutex_destroy(&tz->lock);
1648 device_unregister(&tz->device);
1649 return;
1650}
910cb1e3 1651EXPORT_SYMBOL_GPL(thermal_zone_device_unregister);
203d3d4a 1652
63c4d919
EV
1653/**
1654 * thermal_zone_get_zone_by_name() - search for a zone and returns its ref
1655 * @name: thermal zone name to fetch the temperature
1656 *
1657 * When only one zone is found with the passed name, returns a reference to it.
1658 *
1659 * Return: On success returns a reference to an unique thermal zone with
1660 * matching name equals to @name, an ERR_PTR otherwise (-EINVAL for invalid
1661 * paramenters, -ENODEV for not found and -EEXIST for multiple matches).
1662 */
1663struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name)
1664{
1665 struct thermal_zone_device *pos = NULL, *ref = ERR_PTR(-EINVAL);
1666 unsigned int found = 0;
1667
1668 if (!name)
1669 goto exit;
1670
1671 mutex_lock(&thermal_list_lock);
1672 list_for_each_entry(pos, &thermal_tz_list, node)
1673 if (!strnicmp(name, pos->type, THERMAL_NAME_LENGTH)) {
1674 found++;
1675 ref = pos;
1676 }
1677 mutex_unlock(&thermal_list_lock);
1678
1679 /* nothing has been found, thus an error code for it */
1680 if (found == 0)
1681 ref = ERR_PTR(-ENODEV);
1682 else if (found > 1)
1683 /* Success only when an unique zone is found */
1684 ref = ERR_PTR(-EEXIST);
1685
1686exit:
1687 return ref;
1688}
1689EXPORT_SYMBOL_GPL(thermal_zone_get_zone_by_name);
1690
af06216a 1691#ifdef CONFIG_NET
2a94fe48
JB
1692static const struct genl_multicast_group thermal_event_mcgrps[] = {
1693 { .name = THERMAL_GENL_MCAST_GROUP_NAME, },
1694};
1695
af06216a
RW
1696static struct genl_family thermal_event_genl_family = {
1697 .id = GENL_ID_GENERATE,
1698 .name = THERMAL_GENL_FAMILY_NAME,
1699 .version = THERMAL_GENL_VERSION,
1700 .maxattr = THERMAL_GENL_ATTR_MAX,
2a94fe48
JB
1701 .mcgrps = thermal_event_mcgrps,
1702 .n_mcgrps = ARRAY_SIZE(thermal_event_mcgrps),
af06216a
RW
1703};
1704
8ab3e6a0
EV
1705int thermal_generate_netlink_event(struct thermal_zone_device *tz,
1706 enum events event)
4cb18728
D
1707{
1708 struct sk_buff *skb;
1709 struct nlattr *attr;
1710 struct thermal_genl_event *thermal_event;
1711 void *msg_header;
1712 int size;
1713 int result;
b11de07c 1714 static unsigned int thermal_event_seqnum;
4cb18728 1715
8ab3e6a0
EV
1716 if (!tz)
1717 return -EINVAL;
1718
4cb18728 1719 /* allocate memory */
886ee546
JP
1720 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1721 nla_total_size(0);
4cb18728
D
1722
1723 skb = genlmsg_new(size, GFP_ATOMIC);
1724 if (!skb)
1725 return -ENOMEM;
1726
1727 /* add the genetlink message header */
1728 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1729 &thermal_event_genl_family, 0,
1730 THERMAL_GENL_CMD_EVENT);
1731 if (!msg_header) {
1732 nlmsg_free(skb);
1733 return -ENOMEM;
1734 }
1735
1736 /* fill the data */
886ee546
JP
1737 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1738 sizeof(struct thermal_genl_event));
4cb18728
D
1739
1740 if (!attr) {
1741 nlmsg_free(skb);
1742 return -EINVAL;
1743 }
1744
1745 thermal_event = nla_data(attr);
1746 if (!thermal_event) {
1747 nlmsg_free(skb);
1748 return -EINVAL;
1749 }
1750
1751 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1752
8ab3e6a0 1753 thermal_event->orig = tz->id;
4cb18728
D
1754 thermal_event->event = event;
1755
1756 /* send multicast genetlink message */
1757 result = genlmsg_end(skb, msg_header);
1758 if (result < 0) {
1759 nlmsg_free(skb);
1760 return result;
1761 }
1762
68eb5503 1763 result = genlmsg_multicast(&thermal_event_genl_family, skb, 0,
2a94fe48 1764 0, GFP_ATOMIC);
4cb18728 1765 if (result)
923e0b1e 1766 dev_err(&tz->device, "Failed to send netlink event:%d", result);
4cb18728
D
1767
1768 return result;
1769}
910cb1e3 1770EXPORT_SYMBOL_GPL(thermal_generate_netlink_event);
4cb18728
D
1771
1772static int genetlink_init(void)
1773{
2a94fe48 1774 return genl_register_family(&thermal_event_genl_family);
4cb18728
D
1775}
1776
af06216a
RW
1777static void genetlink_exit(void)
1778{
1779 genl_unregister_family(&thermal_event_genl_family);
1780}
1781#else /* !CONFIG_NET */
1782static inline int genetlink_init(void) { return 0; }
1783static inline void genetlink_exit(void) {}
1784#endif /* !CONFIG_NET */
1785
80a26a5c
ZR
1786static int __init thermal_register_governors(void)
1787{
1788 int result;
1789
1790 result = thermal_gov_step_wise_register();
1791 if (result)
1792 return result;
1793
1794 result = thermal_gov_fair_share_register();
1795 if (result)
1796 return result;
1797
1798 return thermal_gov_user_space_register();
1799}
1800
1801static void thermal_unregister_governors(void)
1802{
1803 thermal_gov_step_wise_unregister();
1804 thermal_gov_fair_share_unregister();
1805 thermal_gov_user_space_unregister();
1806}
1807
203d3d4a
ZR
1808static int __init thermal_init(void)
1809{
80a26a5c
ZR
1810 int result;
1811
1812 result = thermal_register_governors();
1813 if (result)
1814 goto error;
203d3d4a
ZR
1815
1816 result = class_register(&thermal_class);
80a26a5c
ZR
1817 if (result)
1818 goto unregister_governors;
1819
4cb18728 1820 result = genetlink_init();
80a26a5c
ZR
1821 if (result)
1822 goto unregister_class;
1823
4e5e4705
EV
1824 result = of_parse_thermal_zones();
1825 if (result)
1826 goto exit_netlink;
1827
80a26a5c
ZR
1828 return 0;
1829
4e5e4705
EV
1830exit_netlink:
1831 genetlink_exit();
80a26a5c
ZR
1832unregister_governors:
1833 thermal_unregister_governors();
1834unregister_class:
1835 class_unregister(&thermal_class);
1836error:
1837 idr_destroy(&thermal_tz_idr);
1838 idr_destroy(&thermal_cdev_idr);
1839 mutex_destroy(&thermal_idr_lock);
1840 mutex_destroy(&thermal_list_lock);
1841 mutex_destroy(&thermal_governor_lock);
203d3d4a
ZR
1842 return result;
1843}
1844
1845static void __exit thermal_exit(void)
1846{
4e5e4705 1847 of_thermal_destroy_zones();
80a26a5c 1848 genetlink_exit();
203d3d4a 1849 class_unregister(&thermal_class);
80a26a5c 1850 thermal_unregister_governors();
203d3d4a
ZR
1851 idr_destroy(&thermal_tz_idr);
1852 idr_destroy(&thermal_cdev_idr);
1853 mutex_destroy(&thermal_idr_lock);
1854 mutex_destroy(&thermal_list_lock);
80a26a5c 1855 mutex_destroy(&thermal_governor_lock);
203d3d4a
ZR
1856}
1857
4cb18728 1858fs_initcall(thermal_init);
203d3d4a 1859module_exit(thermal_exit);
This page took 0.473804 seconds and 5 git commands to generate.