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