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