Thermal: Make Thermal trip points writeable
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / thermal / thermal_sys.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>
35#include <linux/spinlock.h>
b1569e99 36#include <linux/reboot.h>
4cb18728
D
37#include <net/netlink.h>
38#include <net/genetlink.h>
203d3d4a 39
63c4ec90 40MODULE_AUTHOR("Zhang Rui");
203d3d4a
ZR
41MODULE_DESCRIPTION("Generic thermal management sysfs support");
42MODULE_LICENSE("GPL");
43
203d3d4a
ZR
44struct thermal_cooling_device_instance {
45 int id;
46 char name[THERMAL_NAME_LENGTH];
47 struct thermal_zone_device *tz;
48 struct thermal_cooling_device *cdev;
49 int trip;
50 char attr_name[THERMAL_NAME_LENGTH];
51 struct device_attribute attr;
52 struct list_head node;
53};
54
55static DEFINE_IDR(thermal_tz_idr);
56static DEFINE_IDR(thermal_cdev_idr);
57static DEFINE_MUTEX(thermal_idr_lock);
58
59static LIST_HEAD(thermal_tz_list);
60static LIST_HEAD(thermal_cdev_list);
61static DEFINE_MUTEX(thermal_list_lock);
62
63static int get_idr(struct idr *idr, struct mutex *lock, int *id)
64{
65 int err;
66
caca8b80 67again:
203d3d4a
ZR
68 if (unlikely(idr_pre_get(idr, GFP_KERNEL) == 0))
69 return -ENOMEM;
70
71 if (lock)
72 mutex_lock(lock);
73 err = idr_get_new(idr, NULL, id);
74 if (lock)
75 mutex_unlock(lock);
76 if (unlikely(err == -EAGAIN))
77 goto again;
78 else if (unlikely(err))
79 return err;
80
81 *id = *id & MAX_ID_MASK;
82 return 0;
83}
84
85static void release_idr(struct idr *idr, struct mutex *lock, int id)
86{
87 if (lock)
88 mutex_lock(lock);
89 idr_remove(idr, id);
90 if (lock)
91 mutex_unlock(lock);
92}
93
94/* sys I/F for thermal zone */
95
96#define to_thermal_zone(_dev) \
97 container_of(_dev, struct thermal_zone_device, device)
98
99static ssize_t
100type_show(struct device *dev, struct device_attribute *attr, char *buf)
101{
102 struct thermal_zone_device *tz = to_thermal_zone(dev);
103
104 return sprintf(buf, "%s\n", tz->type);
105}
106
107static ssize_t
108temp_show(struct device *dev, struct device_attribute *attr, char *buf)
109{
110 struct thermal_zone_device *tz = to_thermal_zone(dev);
6503e5df
MG
111 long temperature;
112 int ret;
203d3d4a
ZR
113
114 if (!tz->ops->get_temp)
115 return -EPERM;
116
6503e5df
MG
117 ret = tz->ops->get_temp(tz, &temperature);
118
119 if (ret)
120 return ret;
121
122 return sprintf(buf, "%ld\n", temperature);
203d3d4a
ZR
123}
124
125static ssize_t
126mode_show(struct device *dev, struct device_attribute *attr, char *buf)
127{
128 struct thermal_zone_device *tz = to_thermal_zone(dev);
6503e5df
MG
129 enum thermal_device_mode mode;
130 int result;
203d3d4a
ZR
131
132 if (!tz->ops->get_mode)
133 return -EPERM;
134
6503e5df
MG
135 result = tz->ops->get_mode(tz, &mode);
136 if (result)
137 return result;
138
139 return sprintf(buf, "%s\n", mode == THERMAL_DEVICE_ENABLED ? "enabled"
140 : "disabled");
203d3d4a
ZR
141}
142
143static ssize_t
144mode_store(struct device *dev, struct device_attribute *attr,
145 const char *buf, size_t count)
146{
147 struct thermal_zone_device *tz = to_thermal_zone(dev);
148 int result;
149
150 if (!tz->ops->set_mode)
151 return -EPERM;
152
f1f0e2ac 153 if (!strncmp(buf, "enabled", sizeof("enabled") - 1))
6503e5df 154 result = tz->ops->set_mode(tz, THERMAL_DEVICE_ENABLED);
f1f0e2ac 155 else if (!strncmp(buf, "disabled", sizeof("disabled") - 1))
6503e5df
MG
156 result = tz->ops->set_mode(tz, THERMAL_DEVICE_DISABLED);
157 else
158 result = -EINVAL;
159
203d3d4a
ZR
160 if (result)
161 return result;
162
163 return count;
164}
165
166static ssize_t
167trip_point_type_show(struct device *dev, struct device_attribute *attr,
168 char *buf)
169{
170 struct thermal_zone_device *tz = to_thermal_zone(dev);
6503e5df
MG
171 enum thermal_trip_type type;
172 int trip, result;
203d3d4a
ZR
173
174 if (!tz->ops->get_trip_type)
175 return -EPERM;
176
177 if (!sscanf(attr->attr.name, "trip_point_%d_type", &trip))
178 return -EINVAL;
179
6503e5df
MG
180 result = tz->ops->get_trip_type(tz, trip, &type);
181 if (result)
182 return result;
183
184 switch (type) {
185 case THERMAL_TRIP_CRITICAL:
625120a4 186 return sprintf(buf, "critical\n");
6503e5df 187 case THERMAL_TRIP_HOT:
625120a4 188 return sprintf(buf, "hot\n");
6503e5df 189 case THERMAL_TRIP_PASSIVE:
625120a4 190 return sprintf(buf, "passive\n");
6503e5df 191 case THERMAL_TRIP_ACTIVE:
625120a4 192 return sprintf(buf, "active\n");
6503e5df 193 default:
625120a4 194 return sprintf(buf, "unknown\n");
6503e5df 195 }
203d3d4a
ZR
196}
197
c56f5c03
D
198static ssize_t
199trip_point_temp_store(struct device *dev, struct device_attribute *attr,
200 const char *buf, size_t count)
201{
202 struct thermal_zone_device *tz = to_thermal_zone(dev);
203 int trip, ret;
204 unsigned long temperature;
205
206 if (!tz->ops->set_trip_temp)
207 return -EPERM;
208
209 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
210 return -EINVAL;
211
212 if (kstrtoul(buf, 10, &temperature))
213 return -EINVAL;
214
215 ret = tz->ops->set_trip_temp(tz, trip, temperature);
216
217 return ret ? ret : count;
218}
219
203d3d4a
ZR
220static ssize_t
221trip_point_temp_show(struct device *dev, struct device_attribute *attr,
222 char *buf)
223{
224 struct thermal_zone_device *tz = to_thermal_zone(dev);
6503e5df
MG
225 int trip, ret;
226 long temperature;
203d3d4a
ZR
227
228 if (!tz->ops->get_trip_temp)
229 return -EPERM;
230
231 if (!sscanf(attr->attr.name, "trip_point_%d_temp", &trip))
232 return -EINVAL;
233
6503e5df
MG
234 ret = tz->ops->get_trip_temp(tz, trip, &temperature);
235
236 if (ret)
237 return ret;
238
239 return sprintf(buf, "%ld\n", temperature);
203d3d4a
ZR
240}
241
03a971a2
MG
242static ssize_t
243passive_store(struct device *dev, struct device_attribute *attr,
244 const char *buf, size_t count)
245{
246 struct thermal_zone_device *tz = to_thermal_zone(dev);
247 struct thermal_cooling_device *cdev = NULL;
248 int state;
249
250 if (!sscanf(buf, "%d\n", &state))
251 return -EINVAL;
252
3d8e3ad8
FP
253 /* sanity check: values below 1000 millicelcius don't make sense
254 * and can cause the system to go into a thermal heart attack
255 */
256 if (state && state < 1000)
257 return -EINVAL;
258
03a971a2
MG
259 if (state && !tz->forced_passive) {
260 mutex_lock(&thermal_list_lock);
261 list_for_each_entry(cdev, &thermal_cdev_list, node) {
262 if (!strncmp("Processor", cdev->type,
263 sizeof("Processor")))
264 thermal_zone_bind_cooling_device(tz,
265 THERMAL_TRIPS_NONE,
266 cdev);
267 }
268 mutex_unlock(&thermal_list_lock);
e4143b03
FP
269 if (!tz->passive_delay)
270 tz->passive_delay = 1000;
03a971a2
MG
271 } else if (!state && tz->forced_passive) {
272 mutex_lock(&thermal_list_lock);
273 list_for_each_entry(cdev, &thermal_cdev_list, node) {
274 if (!strncmp("Processor", cdev->type,
275 sizeof("Processor")))
276 thermal_zone_unbind_cooling_device(tz,
277 THERMAL_TRIPS_NONE,
278 cdev);
279 }
280 mutex_unlock(&thermal_list_lock);
e4143b03 281 tz->passive_delay = 0;
03a971a2
MG
282 }
283
284 tz->tc1 = 1;
285 tz->tc2 = 1;
286
03a971a2
MG
287 tz->forced_passive = state;
288
289 thermal_zone_device_update(tz);
290
291 return count;
292}
293
294static ssize_t
295passive_show(struct device *dev, struct device_attribute *attr,
296 char *buf)
297{
298 struct thermal_zone_device *tz = to_thermal_zone(dev);
299
300 return sprintf(buf, "%d\n", tz->forced_passive);
301}
302
203d3d4a
ZR
303static DEVICE_ATTR(type, 0444, type_show, NULL);
304static DEVICE_ATTR(temp, 0444, temp_show, NULL);
305static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
886ee546 306static DEVICE_ATTR(passive, S_IRUGO | S_IWUSR, passive_show, passive_store);
203d3d4a 307
203d3d4a
ZR
308/* sys I/F for cooling device */
309#define to_cooling_device(_dev) \
310 container_of(_dev, struct thermal_cooling_device, device)
311
312static ssize_t
313thermal_cooling_device_type_show(struct device *dev,
314 struct device_attribute *attr, char *buf)
315{
316 struct thermal_cooling_device *cdev = to_cooling_device(dev);
317
318 return sprintf(buf, "%s\n", cdev->type);
319}
320
321static ssize_t
322thermal_cooling_device_max_state_show(struct device *dev,
323 struct device_attribute *attr, char *buf)
324{
325 struct thermal_cooling_device *cdev = to_cooling_device(dev);
6503e5df
MG
326 unsigned long state;
327 int ret;
203d3d4a 328
6503e5df
MG
329 ret = cdev->ops->get_max_state(cdev, &state);
330 if (ret)
331 return ret;
332 return sprintf(buf, "%ld\n", state);
203d3d4a
ZR
333}
334
335static ssize_t
336thermal_cooling_device_cur_state_show(struct device *dev,
337 struct device_attribute *attr, char *buf)
338{
339 struct thermal_cooling_device *cdev = to_cooling_device(dev);
6503e5df
MG
340 unsigned long state;
341 int ret;
203d3d4a 342
6503e5df
MG
343 ret = cdev->ops->get_cur_state(cdev, &state);
344 if (ret)
345 return ret;
346 return sprintf(buf, "%ld\n", state);
203d3d4a
ZR
347}
348
349static ssize_t
350thermal_cooling_device_cur_state_store(struct device *dev,
351 struct device_attribute *attr,
352 const char *buf, size_t count)
353{
354 struct thermal_cooling_device *cdev = to_cooling_device(dev);
6503e5df 355 unsigned long state;
203d3d4a
ZR
356 int result;
357
6503e5df 358 if (!sscanf(buf, "%ld\n", &state))
203d3d4a
ZR
359 return -EINVAL;
360
edb94918 361 if ((long)state < 0)
203d3d4a
ZR
362 return -EINVAL;
363
364 result = cdev->ops->set_cur_state(cdev, state);
365 if (result)
366 return result;
367 return count;
368}
369
370static struct device_attribute dev_attr_cdev_type =
543a9561 371__ATTR(type, 0444, thermal_cooling_device_type_show, NULL);
203d3d4a
ZR
372static DEVICE_ATTR(max_state, 0444,
373 thermal_cooling_device_max_state_show, NULL);
374static DEVICE_ATTR(cur_state, 0644,
375 thermal_cooling_device_cur_state_show,
376 thermal_cooling_device_cur_state_store);
377
378static ssize_t
379thermal_cooling_device_trip_point_show(struct device *dev,
543a9561 380 struct device_attribute *attr, char *buf)
203d3d4a
ZR
381{
382 struct thermal_cooling_device_instance *instance;
383
384 instance =
385 container_of(attr, struct thermal_cooling_device_instance, attr);
386
387 if (instance->trip == THERMAL_TRIPS_NONE)
388 return sprintf(buf, "-1\n");
389 else
390 return sprintf(buf, "%d\n", instance->trip);
391}
392
393/* Device management */
394
16d75239
RH
395#if defined(CONFIG_THERMAL_HWMON)
396
e68b16ab
ZR
397/* hwmon sys I/F */
398#include <linux/hwmon.h>
31f5396a
JD
399
400/* thermal zone devices with the same type share one hwmon device */
401struct thermal_hwmon_device {
402 char type[THERMAL_NAME_LENGTH];
403 struct device *device;
404 int count;
405 struct list_head tz_list;
406 struct list_head node;
407};
408
409struct thermal_hwmon_attr {
410 struct device_attribute attr;
411 char name[16];
412};
413
414/* one temperature input for each thermal zone */
415struct thermal_hwmon_temp {
416 struct list_head hwmon_node;
417 struct thermal_zone_device *tz;
418 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
419 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
420};
421
e68b16ab
ZR
422static LIST_HEAD(thermal_hwmon_list);
423
424static ssize_t
425name_show(struct device *dev, struct device_attribute *attr, char *buf)
426{
0e968a3b 427 struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
e68b16ab
ZR
428 return sprintf(buf, "%s\n", hwmon->type);
429}
430static DEVICE_ATTR(name, 0444, name_show, NULL);
431
432static ssize_t
433temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
434{
6503e5df
MG
435 long temperature;
436 int ret;
e68b16ab
ZR
437 struct thermal_hwmon_attr *hwmon_attr
438 = container_of(attr, struct thermal_hwmon_attr, attr);
31f5396a
JD
439 struct thermal_hwmon_temp *temp
440 = container_of(hwmon_attr, struct thermal_hwmon_temp,
e68b16ab 441 temp_input);
31f5396a 442 struct thermal_zone_device *tz = temp->tz;
e68b16ab 443
6503e5df
MG
444 ret = tz->ops->get_temp(tz, &temperature);
445
446 if (ret)
447 return ret;
448
449 return sprintf(buf, "%ld\n", temperature);
e68b16ab
ZR
450}
451
452static ssize_t
453temp_crit_show(struct device *dev, struct device_attribute *attr,
454 char *buf)
455{
456 struct thermal_hwmon_attr *hwmon_attr
457 = container_of(attr, struct thermal_hwmon_attr, attr);
31f5396a
JD
458 struct thermal_hwmon_temp *temp
459 = container_of(hwmon_attr, struct thermal_hwmon_temp,
e68b16ab 460 temp_crit);
31f5396a 461 struct thermal_zone_device *tz = temp->tz;
6503e5df
MG
462 long temperature;
463 int ret;
464
465 ret = tz->ops->get_trip_temp(tz, 0, &temperature);
466 if (ret)
467 return ret;
e68b16ab 468
6503e5df 469 return sprintf(buf, "%ld\n", temperature);
e68b16ab
ZR
470}
471
472
0d97d7a4
JD
473static struct thermal_hwmon_device *
474thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
e68b16ab
ZR
475{
476 struct thermal_hwmon_device *hwmon;
e68b16ab
ZR
477
478 mutex_lock(&thermal_list_lock);
479 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
480 if (!strcmp(hwmon->type, tz->type)) {
e68b16ab 481 mutex_unlock(&thermal_list_lock);
0d97d7a4 482 return hwmon;
e68b16ab
ZR
483 }
484 mutex_unlock(&thermal_list_lock);
485
0d97d7a4
JD
486 return NULL;
487}
488
31f5396a
JD
489/* Find the temperature input matching a given thermal zone */
490static struct thermal_hwmon_temp *
491thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
492 const struct thermal_zone_device *tz)
493{
494 struct thermal_hwmon_temp *temp;
495
496 mutex_lock(&thermal_list_lock);
497 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
498 if (temp->tz == tz) {
499 mutex_unlock(&thermal_list_lock);
500 return temp;
501 }
502 mutex_unlock(&thermal_list_lock);
503
504 return NULL;
505}
506
0d97d7a4
JD
507static int
508thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
509{
510 struct thermal_hwmon_device *hwmon;
31f5396a 511 struct thermal_hwmon_temp *temp;
0d97d7a4
JD
512 int new_hwmon_device = 1;
513 int result;
514
515 hwmon = thermal_hwmon_lookup_by_type(tz);
516 if (hwmon) {
517 new_hwmon_device = 0;
518 goto register_sys_interface;
519 }
520
e68b16ab
ZR
521 hwmon = kzalloc(sizeof(struct thermal_hwmon_device), GFP_KERNEL);
522 if (!hwmon)
523 return -ENOMEM;
524
525 INIT_LIST_HEAD(&hwmon->tz_list);
526 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
527 hwmon->device = hwmon_device_register(NULL);
528 if (IS_ERR(hwmon->device)) {
529 result = PTR_ERR(hwmon->device);
530 goto free_mem;
531 }
0e968a3b 532 dev_set_drvdata(hwmon->device, hwmon);
e68b16ab
ZR
533 result = device_create_file(hwmon->device, &dev_attr_name);
534 if (result)
b299eb5c 535 goto free_mem;
e68b16ab
ZR
536
537 register_sys_interface:
31f5396a
JD
538 temp = kzalloc(sizeof(struct thermal_hwmon_temp), GFP_KERNEL);
539 if (!temp) {
540 result = -ENOMEM;
541 goto unregister_name;
542 }
543
544 temp->tz = tz;
e68b16ab
ZR
545 hwmon->count++;
546
31f5396a 547 snprintf(temp->temp_input.name, THERMAL_NAME_LENGTH,
e68b16ab 548 "temp%d_input", hwmon->count);
31f5396a
JD
549 temp->temp_input.attr.attr.name = temp->temp_input.name;
550 temp->temp_input.attr.attr.mode = 0444;
551 temp->temp_input.attr.show = temp_input_show;
552 sysfs_attr_init(&temp->temp_input.attr.attr);
553 result = device_create_file(hwmon->device, &temp->temp_input.attr);
e68b16ab 554 if (result)
31f5396a 555 goto free_temp_mem;
e68b16ab
ZR
556
557 if (tz->ops->get_crit_temp) {
558 unsigned long temperature;
559 if (!tz->ops->get_crit_temp(tz, &temperature)) {
31f5396a 560 snprintf(temp->temp_crit.name, THERMAL_NAME_LENGTH,
e68b16ab 561 "temp%d_crit", hwmon->count);
31f5396a
JD
562 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
563 temp->temp_crit.attr.attr.mode = 0444;
564 temp->temp_crit.attr.show = temp_crit_show;
565 sysfs_attr_init(&temp->temp_crit.attr.attr);
e68b16ab 566 result = device_create_file(hwmon->device,
31f5396a 567 &temp->temp_crit.attr);
e68b16ab 568 if (result)
b299eb5c 569 goto unregister_input;
e68b16ab
ZR
570 }
571 }
572
573 mutex_lock(&thermal_list_lock);
574 if (new_hwmon_device)
575 list_add_tail(&hwmon->node, &thermal_hwmon_list);
31f5396a 576 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
e68b16ab
ZR
577 mutex_unlock(&thermal_list_lock);
578
579 return 0;
580
b299eb5c 581 unregister_input:
31f5396a
JD
582 device_remove_file(hwmon->device, &temp->temp_input.attr);
583 free_temp_mem:
584 kfree(temp);
b299eb5c 585 unregister_name:
e68b16ab
ZR
586 if (new_hwmon_device) {
587 device_remove_file(hwmon->device, &dev_attr_name);
588 hwmon_device_unregister(hwmon->device);
589 }
590 free_mem:
591 if (new_hwmon_device)
592 kfree(hwmon);
593
594 return result;
595}
596
597static void
598thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
599{
31f5396a
JD
600 struct thermal_hwmon_device *hwmon;
601 struct thermal_hwmon_temp *temp;
602
603 hwmon = thermal_hwmon_lookup_by_type(tz);
604 if (unlikely(!hwmon)) {
605 /* Should never happen... */
606 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
607 return;
608 }
609
610 temp = thermal_hwmon_lookup_temp(hwmon, tz);
611 if (unlikely(!temp)) {
612 /* Should never happen... */
613 dev_dbg(&tz->device, "temperature input lookup failed!\n");
614 return;
615 }
e68b16ab 616
31f5396a 617 device_remove_file(hwmon->device, &temp->temp_input.attr);
b299eb5c 618 if (tz->ops->get_crit_temp)
31f5396a 619 device_remove_file(hwmon->device, &temp->temp_crit.attr);
e68b16ab
ZR
620
621 mutex_lock(&thermal_list_lock);
31f5396a
JD
622 list_del(&temp->hwmon_node);
623 kfree(temp);
e68b16ab
ZR
624 if (!list_empty(&hwmon->tz_list)) {
625 mutex_unlock(&thermal_list_lock);
626 return;
627 }
628 list_del(&hwmon->node);
629 mutex_unlock(&thermal_list_lock);
630
631 device_remove_file(hwmon->device, &dev_attr_name);
632 hwmon_device_unregister(hwmon->device);
633 kfree(hwmon);
634}
635#else
636static int
637thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
638{
639 return 0;
640}
641
642static void
643thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
644{
645}
646#endif
647
b1569e99
MG
648static void thermal_zone_device_set_polling(struct thermal_zone_device *tz,
649 int delay)
650{
651 cancel_delayed_work(&(tz->poll_queue));
652
653 if (!delay)
654 return;
655
656 if (delay > 1000)
51e20d0e 657 queue_delayed_work(system_freezable_wq, &(tz->poll_queue),
b1569e99
MG
658 round_jiffies(msecs_to_jiffies(delay)));
659 else
51e20d0e 660 queue_delayed_work(system_freezable_wq, &(tz->poll_queue),
b1569e99
MG
661 msecs_to_jiffies(delay));
662}
663
664static void thermal_zone_device_passive(struct thermal_zone_device *tz,
665 int temp, int trip_temp, int trip)
666{
667 int trend = 0;
668 struct thermal_cooling_device_instance *instance;
669 struct thermal_cooling_device *cdev;
670 long state, max_state;
671
672 /*
673 * Above Trip?
674 * -----------
675 * Calculate the thermal trend (using the passive cooling equation)
676 * and modify the performance limit for all passive cooling devices
677 * accordingly. Note that we assume symmetry.
678 */
679 if (temp >= trip_temp) {
680 tz->passive = true;
681
682 trend = (tz->tc1 * (temp - tz->last_temperature)) +
683 (tz->tc2 * (temp - trip_temp));
684
685 /* Heating up? */
686 if (trend > 0) {
687 list_for_each_entry(instance, &tz->cooling_devices,
688 node) {
689 if (instance->trip != trip)
690 continue;
691 cdev = instance->cdev;
692 cdev->ops->get_cur_state(cdev, &state);
693 cdev->ops->get_max_state(cdev, &max_state);
694 if (state++ < max_state)
695 cdev->ops->set_cur_state(cdev, state);
696 }
697 } else if (trend < 0) { /* Cooling off? */
698 list_for_each_entry(instance, &tz->cooling_devices,
699 node) {
700 if (instance->trip != trip)
701 continue;
702 cdev = instance->cdev;
703 cdev->ops->get_cur_state(cdev, &state);
704 cdev->ops->get_max_state(cdev, &max_state);
705 if (state > 0)
706 cdev->ops->set_cur_state(cdev, --state);
707 }
708 }
709 return;
710 }
711
712 /*
713 * Below Trip?
714 * -----------
715 * Implement passive cooling hysteresis to slowly increase performance
716 * and avoid thrashing around the passive trip point. Note that we
717 * assume symmetry.
718 */
719 list_for_each_entry(instance, &tz->cooling_devices, node) {
720 if (instance->trip != trip)
721 continue;
722 cdev = instance->cdev;
723 cdev->ops->get_cur_state(cdev, &state);
724 cdev->ops->get_max_state(cdev, &max_state);
725 if (state > 0)
726 cdev->ops->set_cur_state(cdev, --state);
727 if (state == 0)
728 tz->passive = false;
729 }
730}
731
732static void thermal_zone_device_check(struct work_struct *work)
733{
734 struct thermal_zone_device *tz = container_of(work, struct
735 thermal_zone_device,
736 poll_queue.work);
737 thermal_zone_device_update(tz);
738}
e68b16ab 739
203d3d4a
ZR
740/**
741 * thermal_zone_bind_cooling_device - bind a cooling device to a thermal zone
203d3d4a
ZR
742 * @tz: thermal zone device
743 * @trip: indicates which trip point the cooling devices is
744 * associated with in this thermal zone.
745 * @cdev: thermal cooling device
543a9561
LB
746 *
747 * This function is usually called in the thermal zone device .bind callback.
203d3d4a
ZR
748 */
749int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
750 int trip,
751 struct thermal_cooling_device *cdev)
752{
753 struct thermal_cooling_device_instance *dev;
754 struct thermal_cooling_device_instance *pos;
c7516709
TS
755 struct thermal_zone_device *pos1;
756 struct thermal_cooling_device *pos2;
203d3d4a
ZR
757 int result;
758
543a9561 759 if (trip >= tz->trips || (trip < 0 && trip != THERMAL_TRIPS_NONE))
203d3d4a
ZR
760 return -EINVAL;
761
c7516709
TS
762 list_for_each_entry(pos1, &thermal_tz_list, node) {
763 if (pos1 == tz)
764 break;
765 }
766 list_for_each_entry(pos2, &thermal_cdev_list, node) {
767 if (pos2 == cdev)
768 break;
769 }
770
771 if (tz != pos1 || cdev != pos2)
203d3d4a
ZR
772 return -EINVAL;
773
774 dev =
775 kzalloc(sizeof(struct thermal_cooling_device_instance), GFP_KERNEL);
776 if (!dev)
777 return -ENOMEM;
778 dev->tz = tz;
779 dev->cdev = cdev;
780 dev->trip = trip;
781 result = get_idr(&tz->idr, &tz->lock, &dev->id);
782 if (result)
783 goto free_mem;
784
785 sprintf(dev->name, "cdev%d", dev->id);
786 result =
787 sysfs_create_link(&tz->device.kobj, &cdev->device.kobj, dev->name);
788 if (result)
789 goto release_idr;
790
791 sprintf(dev->attr_name, "cdev%d_trip_point", dev->id);
975f8c56 792 sysfs_attr_init(&dev->attr.attr);
203d3d4a
ZR
793 dev->attr.attr.name = dev->attr_name;
794 dev->attr.attr.mode = 0444;
795 dev->attr.show = thermal_cooling_device_trip_point_show;
796 result = device_create_file(&tz->device, &dev->attr);
797 if (result)
798 goto remove_symbol_link;
799
800 mutex_lock(&tz->lock);
801 list_for_each_entry(pos, &tz->cooling_devices, node)
802 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
803 result = -EEXIST;
804 break;
805 }
806 if (!result)
807 list_add_tail(&dev->node, &tz->cooling_devices);
808 mutex_unlock(&tz->lock);
809
810 if (!result)
811 return 0;
812
813 device_remove_file(&tz->device, &dev->attr);
caca8b80 814remove_symbol_link:
203d3d4a 815 sysfs_remove_link(&tz->device.kobj, dev->name);
caca8b80 816release_idr:
203d3d4a 817 release_idr(&tz->idr, &tz->lock, dev->id);
caca8b80 818free_mem:
203d3d4a
ZR
819 kfree(dev);
820 return result;
821}
822EXPORT_SYMBOL(thermal_zone_bind_cooling_device);
823
824/**
825 * thermal_zone_unbind_cooling_device - unbind a cooling device from a thermal zone
203d3d4a
ZR
826 * @tz: thermal zone device
827 * @trip: indicates which trip point the cooling devices is
828 * associated with in this thermal zone.
829 * @cdev: thermal cooling device
543a9561
LB
830 *
831 * This function is usually called in the thermal zone device .unbind callback.
203d3d4a
ZR
832 */
833int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
834 int trip,
835 struct thermal_cooling_device *cdev)
836{
837 struct thermal_cooling_device_instance *pos, *next;
838
839 mutex_lock(&tz->lock);
840 list_for_each_entry_safe(pos, next, &tz->cooling_devices, node) {
543a9561 841 if (pos->tz == tz && pos->trip == trip && pos->cdev == cdev) {
203d3d4a
ZR
842 list_del(&pos->node);
843 mutex_unlock(&tz->lock);
844 goto unbind;
845 }
846 }
847 mutex_unlock(&tz->lock);
848
849 return -ENODEV;
850
caca8b80 851unbind:
203d3d4a
ZR
852 device_remove_file(&tz->device, &pos->attr);
853 sysfs_remove_link(&tz->device.kobj, pos->name);
854 release_idr(&tz->idr, &tz->lock, pos->id);
855 kfree(pos);
856 return 0;
857}
858EXPORT_SYMBOL(thermal_zone_unbind_cooling_device);
859
860static void thermal_release(struct device *dev)
861{
862 struct thermal_zone_device *tz;
863 struct thermal_cooling_device *cdev;
864
caca8b80
JP
865 if (!strncmp(dev_name(dev), "thermal_zone",
866 sizeof("thermal_zone") - 1)) {
203d3d4a
ZR
867 tz = to_thermal_zone(dev);
868 kfree(tz);
869 } else {
870 cdev = to_cooling_device(dev);
871 kfree(cdev);
872 }
873}
874
875static struct class thermal_class = {
876 .name = "thermal",
877 .dev_release = thermal_release,
878};
879
880/**
881 * thermal_cooling_device_register - register a new thermal cooling device
882 * @type: the thermal cooling device type.
883 * @devdata: device private data.
884 * @ops: standard thermal cooling devices callbacks.
885 */
caca8b80
JP
886struct thermal_cooling_device *
887thermal_cooling_device_register(char *type, void *devdata,
888 const struct thermal_cooling_device_ops *ops)
203d3d4a
ZR
889{
890 struct thermal_cooling_device *cdev;
891 struct thermal_zone_device *pos;
892 int result;
893
894 if (strlen(type) >= THERMAL_NAME_LENGTH)
3e6fda5c 895 return ERR_PTR(-EINVAL);
203d3d4a
ZR
896
897 if (!ops || !ops->get_max_state || !ops->get_cur_state ||
543a9561 898 !ops->set_cur_state)
3e6fda5c 899 return ERR_PTR(-EINVAL);
203d3d4a
ZR
900
901 cdev = kzalloc(sizeof(struct thermal_cooling_device), GFP_KERNEL);
902 if (!cdev)
3e6fda5c 903 return ERR_PTR(-ENOMEM);
203d3d4a
ZR
904
905 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->id);
906 if (result) {
907 kfree(cdev);
3e6fda5c 908 return ERR_PTR(result);
203d3d4a
ZR
909 }
910
911 strcpy(cdev->type, type);
912 cdev->ops = ops;
913 cdev->device.class = &thermal_class;
914 cdev->devdata = devdata;
354655ea 915 dev_set_name(&cdev->device, "cooling_device%d", cdev->id);
203d3d4a
ZR
916 result = device_register(&cdev->device);
917 if (result) {
918 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
919 kfree(cdev);
3e6fda5c 920 return ERR_PTR(result);
203d3d4a
ZR
921 }
922
923 /* sys I/F */
924 if (type) {
543a9561 925 result = device_create_file(&cdev->device, &dev_attr_cdev_type);
203d3d4a
ZR
926 if (result)
927 goto unregister;
928 }
929
930 result = device_create_file(&cdev->device, &dev_attr_max_state);
931 if (result)
932 goto unregister;
933
934 result = device_create_file(&cdev->device, &dev_attr_cur_state);
935 if (result)
936 goto unregister;
937
938 mutex_lock(&thermal_list_lock);
939 list_add(&cdev->node, &thermal_cdev_list);
940 list_for_each_entry(pos, &thermal_tz_list, node) {
941 if (!pos->ops->bind)
942 continue;
943 result = pos->ops->bind(pos, cdev);
944 if (result)
945 break;
946
947 }
948 mutex_unlock(&thermal_list_lock);
949
950 if (!result)
951 return cdev;
952
caca8b80 953unregister:
203d3d4a
ZR
954 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
955 device_unregister(&cdev->device);
3e6fda5c 956 return ERR_PTR(result);
203d3d4a
ZR
957}
958EXPORT_SYMBOL(thermal_cooling_device_register);
959
960/**
961 * thermal_cooling_device_unregister - removes the registered thermal cooling device
203d3d4a
ZR
962 * @cdev: the thermal cooling device to remove.
963 *
964 * thermal_cooling_device_unregister() must be called when the device is no
965 * longer needed.
966 */
967void thermal_cooling_device_unregister(struct
968 thermal_cooling_device
969 *cdev)
970{
971 struct thermal_zone_device *tz;
972 struct thermal_cooling_device *pos = NULL;
973
974 if (!cdev)
975 return;
976
977 mutex_lock(&thermal_list_lock);
978 list_for_each_entry(pos, &thermal_cdev_list, node)
979 if (pos == cdev)
980 break;
981 if (pos != cdev) {
982 /* thermal cooling device not found */
983 mutex_unlock(&thermal_list_lock);
984 return;
985 }
986 list_del(&cdev->node);
987 list_for_each_entry(tz, &thermal_tz_list, node) {
988 if (!tz->ops->unbind)
989 continue;
990 tz->ops->unbind(tz, cdev);
991 }
992 mutex_unlock(&thermal_list_lock);
993 if (cdev->type[0])
543a9561 994 device_remove_file(&cdev->device, &dev_attr_cdev_type);
203d3d4a
ZR
995 device_remove_file(&cdev->device, &dev_attr_max_state);
996 device_remove_file(&cdev->device, &dev_attr_cur_state);
997
998 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->id);
999 device_unregister(&cdev->device);
1000 return;
1001}
1002EXPORT_SYMBOL(thermal_cooling_device_unregister);
1003
b1569e99
MG
1004/**
1005 * thermal_zone_device_update - force an update of a thermal zone's state
1006 * @ttz: the thermal zone to update
1007 */
1008
1009void thermal_zone_device_update(struct thermal_zone_device *tz)
1010{
1011 int count, ret = 0;
1012 long temp, trip_temp;
1013 enum thermal_trip_type trip_type;
1014 struct thermal_cooling_device_instance *instance;
1015 struct thermal_cooling_device *cdev;
1016
1017 mutex_lock(&tz->lock);
1018
0d288162
MB
1019 if (tz->ops->get_temp(tz, &temp)) {
1020 /* get_temp failed - retry it later */
c5a01dd5 1021 pr_warn("failed to read out thermal zone %d\n", tz->id);
0d288162
MB
1022 goto leave;
1023 }
b1569e99
MG
1024
1025 for (count = 0; count < tz->trips; count++) {
1026 tz->ops->get_trip_type(tz, count, &trip_type);
1027 tz->ops->get_trip_temp(tz, count, &trip_temp);
1028
1029 switch (trip_type) {
1030 case THERMAL_TRIP_CRITICAL:
29321357 1031 if (temp >= trip_temp) {
b1569e99
MG
1032 if (tz->ops->notify)
1033 ret = tz->ops->notify(tz, count,
1034 trip_type);
1035 if (!ret) {
c5a01dd5
JP
1036 pr_emerg("Critical temperature reached (%ld C), shutting down\n",
1037 temp/1000);
b1569e99
MG
1038 orderly_poweroff(true);
1039 }
1040 }
1041 break;
1042 case THERMAL_TRIP_HOT:
29321357 1043 if (temp >= trip_temp)
b1569e99
MG
1044 if (tz->ops->notify)
1045 tz->ops->notify(tz, count, trip_type);
1046 break;
1047 case THERMAL_TRIP_ACTIVE:
1048 list_for_each_entry(instance, &tz->cooling_devices,
1049 node) {
1050 if (instance->trip != count)
1051 continue;
1052
1053 cdev = instance->cdev;
1054
29321357 1055 if (temp >= trip_temp)
b1569e99
MG
1056 cdev->ops->set_cur_state(cdev, 1);
1057 else
1058 cdev->ops->set_cur_state(cdev, 0);
1059 }
1060 break;
1061 case THERMAL_TRIP_PASSIVE:
29321357 1062 if (temp >= trip_temp || tz->passive)
b1569e99
MG
1063 thermal_zone_device_passive(tz, temp,
1064 trip_temp, count);
1065 break;
1066 }
1067 }
03a971a2
MG
1068
1069 if (tz->forced_passive)
1070 thermal_zone_device_passive(tz, temp, tz->forced_passive,
1071 THERMAL_TRIPS_NONE);
1072
b1569e99 1073 tz->last_temperature = temp;
0d288162 1074
caca8b80 1075leave:
b1569e99
MG
1076 if (tz->passive)
1077 thermal_zone_device_set_polling(tz, tz->passive_delay);
1078 else if (tz->polling_delay)
1079 thermal_zone_device_set_polling(tz, tz->polling_delay);
3767cb54
FP
1080 else
1081 thermal_zone_device_set_polling(tz, 0);
b1569e99
MG
1082 mutex_unlock(&tz->lock);
1083}
1084EXPORT_SYMBOL(thermal_zone_device_update);
1085
c56f5c03
D
1086/**
1087 * create_trip_attrs - create attributes for trip points
1088 * @tz: the thermal zone device
1089 * @mask: Writeable trip point bitmap.
1090 */
1091static int create_trip_attrs(struct thermal_zone_device *tz, int mask)
1092{
1093 int indx;
1094
1095 tz->trip_type_attrs =
1096 kzalloc(sizeof(struct thermal_attr) * tz->trips, GFP_KERNEL);
1097 if (!tz->trip_type_attrs)
1098 return -ENOMEM;
1099
1100 tz->trip_temp_attrs =
1101 kzalloc(sizeof(struct thermal_attr) * tz->trips, GFP_KERNEL);
1102 if (!tz->trip_temp_attrs) {
1103 kfree(tz->trip_type_attrs);
1104 return -ENOMEM;
1105 }
1106
1107 for (indx = 0; indx < tz->trips; indx++) {
1108
1109 /* create trip type attribute */
1110 snprintf(tz->trip_type_attrs[indx].name, THERMAL_NAME_LENGTH,
1111 "trip_point_%d_type", indx);
1112
1113 sysfs_attr_init(&tz->trip_type_attrs[indx].attr.attr);
1114 tz->trip_type_attrs[indx].attr.attr.name =
1115 tz->trip_type_attrs[indx].name;
1116 tz->trip_type_attrs[indx].attr.attr.mode = S_IRUGO;
1117 tz->trip_type_attrs[indx].attr.show = trip_point_type_show;
1118
1119 device_create_file(&tz->device,
1120 &tz->trip_type_attrs[indx].attr);
1121
1122 /* create trip temp attribute */
1123 snprintf(tz->trip_temp_attrs[indx].name, THERMAL_NAME_LENGTH,
1124 "trip_point_%d_temp", indx);
1125
1126 sysfs_attr_init(&tz->trip_temp_attrs[indx].attr.attr);
1127 tz->trip_temp_attrs[indx].attr.attr.name =
1128 tz->trip_temp_attrs[indx].name;
1129 tz->trip_temp_attrs[indx].attr.attr.mode = S_IRUGO;
1130 tz->trip_temp_attrs[indx].attr.show = trip_point_temp_show;
1131 if (mask & (1 << indx)) {
1132 tz->trip_temp_attrs[indx].attr.attr.mode |= S_IWUSR;
1133 tz->trip_temp_attrs[indx].attr.store =
1134 trip_point_temp_store;
1135 }
1136
1137 device_create_file(&tz->device,
1138 &tz->trip_temp_attrs[indx].attr);
1139 }
1140 return 0;
1141}
1142
1143static void remove_trip_attrs(struct thermal_zone_device *tz)
1144{
1145 int indx;
1146
1147 for (indx = 0; indx < tz->trips; indx++) {
1148 device_remove_file(&tz->device,
1149 &tz->trip_type_attrs[indx].attr);
1150 device_remove_file(&tz->device,
1151 &tz->trip_temp_attrs[indx].attr);
1152 }
1153 kfree(tz->trip_type_attrs);
1154 kfree(tz->trip_temp_attrs);
1155}
1156
203d3d4a
ZR
1157/**
1158 * thermal_zone_device_register - register a new thermal zone device
1159 * @type: the thermal zone device type
1160 * @trips: the number of trip points the thermal zone support
c56f5c03 1161 * @mask: a bit string indicating the writeablility of trip points
203d3d4a
ZR
1162 * @devdata: private device data
1163 * @ops: standard thermal zone device callbacks
b1569e99
MG
1164 * @tc1: thermal coefficient 1 for passive calculations
1165 * @tc2: thermal coefficient 2 for passive calculations
1166 * @passive_delay: number of milliseconds to wait between polls when
1167 * performing passive cooling
1168 * @polling_delay: number of milliseconds to wait between polls when checking
1169 * whether trip points have been crossed (0 for interrupt
1170 * driven systems)
203d3d4a
ZR
1171 *
1172 * thermal_zone_device_unregister() must be called when the device is no
b1569e99
MG
1173 * longer needed. The passive cooling formula uses tc1 and tc2 as described in
1174 * section 11.1.5.1 of the ACPI specification 3.0.
203d3d4a
ZR
1175 */
1176struct thermal_zone_device *thermal_zone_device_register(char *type,
c56f5c03 1177 int trips, int mask, void *devdata,
5b275ce2
AC
1178 const struct thermal_zone_device_ops *ops,
1179 int tc1, int tc2, int passive_delay, int polling_delay)
203d3d4a
ZR
1180{
1181 struct thermal_zone_device *tz;
1182 struct thermal_cooling_device *pos;
03a971a2 1183 enum thermal_trip_type trip_type;
203d3d4a
ZR
1184 int result;
1185 int count;
03a971a2 1186 int passive = 0;
203d3d4a
ZR
1187
1188 if (strlen(type) >= THERMAL_NAME_LENGTH)
3e6fda5c 1189 return ERR_PTR(-EINVAL);
203d3d4a 1190
c56f5c03 1191 if (trips > THERMAL_MAX_TRIPS || trips < 0 || mask >> trips)
3e6fda5c 1192 return ERR_PTR(-EINVAL);
203d3d4a
ZR
1193
1194 if (!ops || !ops->get_temp)
3e6fda5c 1195 return ERR_PTR(-EINVAL);
203d3d4a
ZR
1196
1197 tz = kzalloc(sizeof(struct thermal_zone_device), GFP_KERNEL);
1198 if (!tz)
3e6fda5c 1199 return ERR_PTR(-ENOMEM);
203d3d4a
ZR
1200
1201 INIT_LIST_HEAD(&tz->cooling_devices);
1202 idr_init(&tz->idr);
1203 mutex_init(&tz->lock);
1204 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->id);
1205 if (result) {
1206 kfree(tz);
3e6fda5c 1207 return ERR_PTR(result);
203d3d4a
ZR
1208 }
1209
1210 strcpy(tz->type, type);
1211 tz->ops = ops;
1212 tz->device.class = &thermal_class;
1213 tz->devdata = devdata;
1214 tz->trips = trips;
b1569e99
MG
1215 tz->tc1 = tc1;
1216 tz->tc2 = tc2;
1217 tz->passive_delay = passive_delay;
1218 tz->polling_delay = polling_delay;
1219
354655ea 1220 dev_set_name(&tz->device, "thermal_zone%d", tz->id);
203d3d4a
ZR
1221 result = device_register(&tz->device);
1222 if (result) {
1223 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1224 kfree(tz);
3e6fda5c 1225 return ERR_PTR(result);
203d3d4a
ZR
1226 }
1227
1228 /* sys I/F */
1229 if (type) {
1230 result = device_create_file(&tz->device, &dev_attr_type);
1231 if (result)
1232 goto unregister;
1233 }
1234
1235 result = device_create_file(&tz->device, &dev_attr_temp);
1236 if (result)
1237 goto unregister;
1238
1239 if (ops->get_mode) {
1240 result = device_create_file(&tz->device, &dev_attr_mode);
1241 if (result)
1242 goto unregister;
1243 }
1244
c56f5c03
D
1245 result = create_trip_attrs(tz, mask);
1246 if (result)
1247 goto unregister;
1248
203d3d4a 1249 for (count = 0; count < trips; count++) {
03a971a2
MG
1250 tz->ops->get_trip_type(tz, count, &trip_type);
1251 if (trip_type == THERMAL_TRIP_PASSIVE)
1252 passive = 1;
203d3d4a
ZR
1253 }
1254
03a971a2
MG
1255 if (!passive)
1256 result = device_create_file(&tz->device,
1257 &dev_attr_passive);
1258
1259 if (result)
1260 goto unregister;
1261
e68b16ab
ZR
1262 result = thermal_add_hwmon_sysfs(tz);
1263 if (result)
1264 goto unregister;
1265
203d3d4a
ZR
1266 mutex_lock(&thermal_list_lock);
1267 list_add_tail(&tz->node, &thermal_tz_list);
1268 if (ops->bind)
1269 list_for_each_entry(pos, &thermal_cdev_list, node) {
543a9561
LB
1270 result = ops->bind(tz, pos);
1271 if (result)
1272 break;
203d3d4a
ZR
1273 }
1274 mutex_unlock(&thermal_list_lock);
1275
b1569e99
MG
1276 INIT_DELAYED_WORK(&(tz->poll_queue), thermal_zone_device_check);
1277
1278 thermal_zone_device_update(tz);
1279
203d3d4a
ZR
1280 if (!result)
1281 return tz;
1282
caca8b80 1283unregister:
203d3d4a
ZR
1284 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1285 device_unregister(&tz->device);
3e6fda5c 1286 return ERR_PTR(result);
203d3d4a
ZR
1287}
1288EXPORT_SYMBOL(thermal_zone_device_register);
1289
1290/**
1291 * thermal_device_unregister - removes the registered thermal zone device
203d3d4a
ZR
1292 * @tz: the thermal zone device to remove
1293 */
1294void thermal_zone_device_unregister(struct thermal_zone_device *tz)
1295{
1296 struct thermal_cooling_device *cdev;
1297 struct thermal_zone_device *pos = NULL;
203d3d4a
ZR
1298
1299 if (!tz)
1300 return;
1301
1302 mutex_lock(&thermal_list_lock);
1303 list_for_each_entry(pos, &thermal_tz_list, node)
1304 if (pos == tz)
1305 break;
1306 if (pos != tz) {
1307 /* thermal zone device not found */
1308 mutex_unlock(&thermal_list_lock);
1309 return;
1310 }
1311 list_del(&tz->node);
1312 if (tz->ops->unbind)
1313 list_for_each_entry(cdev, &thermal_cdev_list, node)
1314 tz->ops->unbind(tz, cdev);
1315 mutex_unlock(&thermal_list_lock);
1316
b1569e99
MG
1317 thermal_zone_device_set_polling(tz, 0);
1318
203d3d4a
ZR
1319 if (tz->type[0])
1320 device_remove_file(&tz->device, &dev_attr_type);
1321 device_remove_file(&tz->device, &dev_attr_temp);
1322 if (tz->ops->get_mode)
1323 device_remove_file(&tz->device, &dev_attr_mode);
c56f5c03 1324 remove_trip_attrs(tz);
203d3d4a 1325
e68b16ab 1326 thermal_remove_hwmon_sysfs(tz);
203d3d4a
ZR
1327 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->id);
1328 idr_destroy(&tz->idr);
1329 mutex_destroy(&tz->lock);
1330 device_unregister(&tz->device);
1331 return;
1332}
1333EXPORT_SYMBOL(thermal_zone_device_unregister);
1334
af06216a
RW
1335#ifdef CONFIG_NET
1336static struct genl_family thermal_event_genl_family = {
1337 .id = GENL_ID_GENERATE,
1338 .name = THERMAL_GENL_FAMILY_NAME,
1339 .version = THERMAL_GENL_VERSION,
1340 .maxattr = THERMAL_GENL_ATTR_MAX,
1341};
1342
1343static struct genl_multicast_group thermal_event_mcgrp = {
1344 .name = THERMAL_GENL_MCAST_GROUP_NAME,
1345};
1346
2d58d7ea 1347int thermal_generate_netlink_event(u32 orig, enum events event)
4cb18728
D
1348{
1349 struct sk_buff *skb;
1350 struct nlattr *attr;
1351 struct thermal_genl_event *thermal_event;
1352 void *msg_header;
1353 int size;
1354 int result;
b11de07c 1355 static unsigned int thermal_event_seqnum;
4cb18728
D
1356
1357 /* allocate memory */
886ee546
JP
1358 size = nla_total_size(sizeof(struct thermal_genl_event)) +
1359 nla_total_size(0);
4cb18728
D
1360
1361 skb = genlmsg_new(size, GFP_ATOMIC);
1362 if (!skb)
1363 return -ENOMEM;
1364
1365 /* add the genetlink message header */
1366 msg_header = genlmsg_put(skb, 0, thermal_event_seqnum++,
1367 &thermal_event_genl_family, 0,
1368 THERMAL_GENL_CMD_EVENT);
1369 if (!msg_header) {
1370 nlmsg_free(skb);
1371 return -ENOMEM;
1372 }
1373
1374 /* fill the data */
886ee546
JP
1375 attr = nla_reserve(skb, THERMAL_GENL_ATTR_EVENT,
1376 sizeof(struct thermal_genl_event));
4cb18728
D
1377
1378 if (!attr) {
1379 nlmsg_free(skb);
1380 return -EINVAL;
1381 }
1382
1383 thermal_event = nla_data(attr);
1384 if (!thermal_event) {
1385 nlmsg_free(skb);
1386 return -EINVAL;
1387 }
1388
1389 memset(thermal_event, 0, sizeof(struct thermal_genl_event));
1390
1391 thermal_event->orig = orig;
1392 thermal_event->event = event;
1393
1394 /* send multicast genetlink message */
1395 result = genlmsg_end(skb, msg_header);
1396 if (result < 0) {
1397 nlmsg_free(skb);
1398 return result;
1399 }
1400
1401 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.id, GFP_ATOMIC);
1402 if (result)
c5a01dd5 1403 pr_info("failed to send netlink event:%d\n", result);
4cb18728
D
1404
1405 return result;
1406}
2d58d7ea 1407EXPORT_SYMBOL(thermal_generate_netlink_event);
4cb18728
D
1408
1409static int genetlink_init(void)
1410{
1411 int result;
1412
1413 result = genl_register_family(&thermal_event_genl_family);
1414 if (result)
1415 return result;
1416
1417 result = genl_register_mc_group(&thermal_event_genl_family,
1418 &thermal_event_mcgrp);
1419 if (result)
1420 genl_unregister_family(&thermal_event_genl_family);
1421 return result;
1422}
1423
af06216a
RW
1424static void genetlink_exit(void)
1425{
1426 genl_unregister_family(&thermal_event_genl_family);
1427}
1428#else /* !CONFIG_NET */
1429static inline int genetlink_init(void) { return 0; }
1430static inline void genetlink_exit(void) {}
1431#endif /* !CONFIG_NET */
1432
203d3d4a
ZR
1433static int __init thermal_init(void)
1434{
1435 int result = 0;
1436
1437 result = class_register(&thermal_class);
1438 if (result) {
1439 idr_destroy(&thermal_tz_idr);
1440 idr_destroy(&thermal_cdev_idr);
1441 mutex_destroy(&thermal_idr_lock);
1442 mutex_destroy(&thermal_list_lock);
1443 }
4cb18728 1444 result = genetlink_init();
203d3d4a
ZR
1445 return result;
1446}
1447
1448static void __exit thermal_exit(void)
1449{
1450 class_unregister(&thermal_class);
1451 idr_destroy(&thermal_tz_idr);
1452 idr_destroy(&thermal_cdev_idr);
1453 mutex_destroy(&thermal_idr_lock);
1454 mutex_destroy(&thermal_list_lock);
4cb18728 1455 genetlink_exit();
203d3d4a
ZR
1456}
1457
4cb18728 1458fs_initcall(thermal_init);
203d3d4a 1459module_exit(thermal_exit);