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