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