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