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