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