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