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