26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <linux/module.h>
29 #include <linux/device.h>
31 #include <linux/slab.h>
32 #include <linux/kdev_t.h>
36 #include <linux/reboot.h>
44 #define THERMAL_NO_TARGET -1UL
73 static int get_idr(
struct idr *
idr,
struct mutex *lock,
int *
id)
95 static void release_idr(
struct idr *idr,
struct mutex *lock,
int id)
106 #define to_thermal_zone(_dev) \
107 container_of(_dev, struct thermal_zone_device, device)
124 if (!tz->
ops->get_temp)
127 ret = tz->
ops->get_temp(tz, &temperature);
132 return sprintf(buf,
"%ld\n", temperature);
142 if (!tz->
ops->get_mode)
145 result = tz->
ops->get_mode(tz, &mode);
155 const char *buf,
size_t count)
160 if (!tz->
ops->set_mode)
163 if (!
strncmp(buf,
"enabled",
sizeof(
"enabled") - 1))
165 else if (!
strncmp(buf,
"disabled",
sizeof(
"disabled") - 1))
184 if (!tz->
ops->get_trip_type)
187 if (!
sscanf(attr->
attr.name,
"trip_point_%d_type", &trip))
190 result = tz->
ops->get_trip_type(tz, trip, &type);
196 return sprintf(buf,
"critical\n");
200 return sprintf(buf,
"passive\n");
202 return sprintf(buf,
"active\n");
204 return sprintf(buf,
"unknown\n");
210 const char *buf,
size_t count)
216 if (!tz->
ops->set_trip_temp)
219 if (!
sscanf(attr->
attr.name,
"trip_point_%d_temp", &trip))
222 if (kstrtoul(buf, 10, &temperature))
225 ret = tz->
ops->set_trip_temp(tz, trip, temperature);
227 return ret ? ret :
count;
238 if (!tz->
ops->get_trip_temp)
241 if (!
sscanf(attr->
attr.name,
"trip_point_%d_temp", &trip))
244 ret = tz->
ops->get_trip_temp(tz, trip, &temperature);
249 return sprintf(buf,
"%ld\n", temperature);
254 const char *buf,
size_t count)
260 if (!tz->
ops->set_trip_hyst)
263 if (!
sscanf(attr->
attr.name,
"trip_point_%d_hyst", &trip))
266 if (kstrtoul(buf, 10, &temperature))
274 ret = tz->
ops->set_trip_hyst(tz, trip, temperature);
276 return ret ? ret :
count;
287 if (!tz->
ops->get_trip_hyst)
290 if (!
sscanf(attr->
attr.name,
"trip_point_%d_hyst", &trip))
293 ret = tz->
ops->get_trip_hyst(tz, trip, &temperature);
295 return ret ? ret :
sprintf(buf,
"%ld\n", temperature);
300 const char *buf,
size_t count)
306 if (!
sscanf(buf,
"%d\n", &state))
312 if (state && state < 1000)
319 sizeof(
"Processor")))
332 sizeof(
"Processor")))
359 static DEVICE_ATTR(mode, 0644, mode_show, mode_store);
363 #define to_cooling_device(_dev) \
364 container_of(_dev, struct thermal_cooling_device, device)
367 thermal_cooling_device_type_show(
struct device *dev,
376 thermal_cooling_device_max_state_show(
struct device *dev,
383 ret = cdev->
ops->get_max_state(cdev, &state);
386 return sprintf(buf,
"%ld\n", state);
390 thermal_cooling_device_cur_state_show(
struct device *dev,
397 ret = cdev->
ops->get_cur_state(cdev, &state);
400 return sprintf(buf,
"%ld\n", state);
404 thermal_cooling_device_cur_state_store(
struct device *dev,
406 const char *buf,
size_t count)
412 if (!
sscanf(buf,
"%ld\n", &state))
418 result = cdev->
ops->set_cur_state(cdev, state);
425 __ATTR(type, 0444, thermal_cooling_device_type_show,
NULL);
427 thermal_cooling_device_max_state_show,
NULL);
429 thermal_cooling_device_cur_state_show,
430 thermal_cooling_device_cur_state_store);
433 thermal_cooling_device_trip_point_show(
struct device *dev,
449 #if defined(CONFIG_THERMAL_HWMON)
455 struct thermal_hwmon_device {
463 struct thermal_hwmon_attr {
469 struct thermal_hwmon_temp {
472 struct thermal_hwmon_attr temp_input;
473 struct thermal_hwmon_attr temp_crit;
482 return sprintf(buf,
"%s\n", hwmon->type);
491 struct thermal_hwmon_attr *hwmon_attr
493 struct thermal_hwmon_temp *
temp
498 ret = tz->
ops->get_temp(tz, &temperature);
503 return sprintf(buf,
"%ld\n", temperature);
510 struct thermal_hwmon_attr *hwmon_attr
512 struct thermal_hwmon_temp *temp
519 ret = tz->
ops->get_trip_temp(tz, 0, &temperature);
523 return sprintf(buf,
"%ld\n", temperature);
527 static struct thermal_hwmon_device *
530 struct thermal_hwmon_device *hwmon;
534 if (!
strcmp(hwmon->type, tz->type)) {
544 static struct thermal_hwmon_temp *
545 thermal_hwmon_lookup_temp(
const struct thermal_hwmon_device *hwmon,
548 struct thermal_hwmon_temp *
temp;
552 if (temp->tz == tz) {
564 struct thermal_hwmon_device *hwmon;
565 struct thermal_hwmon_temp *
temp;
566 int new_hwmon_device = 1;
569 hwmon = thermal_hwmon_lookup_by_type(tz);
571 new_hwmon_device = 0;
572 goto register_sys_interface;
575 hwmon = kzalloc(
sizeof(
struct thermal_hwmon_device),
GFP_KERNEL);
579 INIT_LIST_HEAD(&hwmon->tz_list);
582 if (IS_ERR(hwmon->device)) {
583 result = PTR_ERR(hwmon->device);
591 register_sys_interface:
592 temp = kzalloc(
sizeof(
struct thermal_hwmon_temp),
GFP_KERNEL);
595 goto unregister_name;
601 snprintf(temp->temp_input.name,
sizeof(temp->temp_input.name),
602 "temp%d_input", hwmon->count);
603 temp->temp_input.attr.attr.name = temp->temp_input.name;
604 temp->temp_input.attr.attr.mode = 0444;
605 temp->temp_input.attr.show = temp_input_show;
611 if (tz->
ops->get_crit_temp) {
612 unsigned long temperature;
613 if (!tz->
ops->get_crit_temp(tz, &temperature)) {
615 sizeof(temp->temp_crit.name),
616 "temp%d_crit", hwmon->count);
617 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
618 temp->temp_crit.attr.attr.mode = 0444;
619 temp->temp_crit.attr.show = temp_crit_show;
622 &temp->temp_crit.attr);
624 goto unregister_input;
629 if (new_hwmon_device)
641 if (new_hwmon_device) {
646 if (new_hwmon_device)
655 struct thermal_hwmon_device *hwmon;
656 struct thermal_hwmon_temp *
temp;
658 hwmon = thermal_hwmon_lookup_by_type(tz);
665 temp = thermal_hwmon_lookup_temp(hwmon, tz);
673 if (tz->
ops->get_crit_temp)
679 if (!list_empty(&hwmon->tz_list)) {
736 unsigned long upper,
unsigned long lower)
742 unsigned long max_state;
757 if (tz != pos1 || cdev != pos2)
760 cdev->
ops->get_max_state(cdev, &max_state);
766 if (lower > upper || upper > max_state)
780 result = get_idr(&tz->
idr, &tz->
lock, &dev->
id);
793 dev->
attr.attr.mode = 0444;
794 dev->
attr.show = thermal_cooling_device_trip_point_show;
797 goto remove_symbol_link;
802 if (pos->
tz == tz && pos->
trip == trip && pos->
cdev == cdev) {
820 release_idr(&tz->
idr, &tz->
lock, dev->
id);
845 if (pos->
tz == tz && pos->
trip == trip && pos->
cdev == cdev) {
861 release_idr(&tz->
idr, &tz->
lock, pos->
id);
867 static void thermal_release(
struct device *dev)
872 if (!
strncmp(dev_name(dev),
"thermal_zone",
873 sizeof(
"thermal_zone") - 1)) {
882 static struct class thermal_class = {
884 .dev_release = thermal_release,
912 result = get_idr(&thermal_cdev_idr, &thermal_idr_lock, &cdev->
id);
915 return ERR_PTR(result);
923 cdev->
device.class = &thermal_class;
928 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->
id);
930 return ERR_PTR(result);
949 list_add(&cdev->
node, &thermal_cdev_list);
953 result = pos->
ops->bind(pos, cdev);
964 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->
id);
966 return ERR_PTR(result);
998 if (!tz->
ops->unbind)
1000 tz->
ops->unbind(tz, cdev);
1008 release_idr(&thermal_cdev_idr, &thermal_idr_lock, cdev->
id);
1017 unsigned long target = 0;
1028 if (instance->
target > target)
1029 target = instance->
target;
1032 cdev->
ops->set_cur_state(cdev, target);
1041 thermal_cdev_do_update(instance->cdev);
1061 int trip,
long temp)
1065 unsigned long cur_state, max_state;
1071 trip_temp = tz->forced_passive;
1074 tz->ops->get_trip_temp(tz, trip, &trip_temp);
1075 tz->ops->get_trip_type(tz, trip, &trip_type);
1078 if (!tz->ops->get_trend || tz->ops->get_trend(tz, trip, &trend)) {
1083 if (tz->temperature > tz->last_temperature)
1085 else if (tz->temperature < tz->last_temperature)
1091 if (temp >= trip_temp) {
1093 if (instance->
trip != trip)
1096 cdev = instance->
cdev;
1098 cdev->
ops->get_cur_state(cdev, &cur_state);
1099 cdev->
ops->get_max_state(cdev, &max_state);
1102 cur_state = cur_state < instance->
upper ?
1103 (cur_state + 1) : instance->
upper;
1105 cur_state = cur_state > instance->
lower ?
1106 (cur_state - 1) : instance->
lower;
1115 instance->
target = cur_state;
1120 if (instance->
trip != trip)
1126 cdev = instance->
cdev;
1127 cdev->
ops->get_cur_state(cdev, &cur_state);
1129 cur_state = cur_state > instance->
lower ?
1137 instance->
target = cur_state;
1152 long temp, trip_temp;
1157 if (tz->
ops->get_temp(tz, &temp)) {
1159 pr_warn(
"failed to read out thermal zone %d\n", tz->
id);
1166 for (count = 0; count < tz->
trips; count++) {
1167 tz->
ops->get_trip_type(tz, count, &trip_type);
1168 tz->
ops->get_trip_temp(tz, count, &trip_temp);
1170 switch (trip_type) {
1172 if (temp >= trip_temp) {
1173 if (tz->
ops->notify)
1174 ret = tz->
ops->notify(tz, count,
1177 pr_emerg(
"Critical temperature reached (%ld C), shutting down\n",
1184 if (temp >= trip_temp)
1185 if (tz->
ops->notify)
1186 tz->
ops->notify(tz, count, trip_type);
1189 thermal_zone_trip_update(tz, count, temp);
1192 if (temp >= trip_temp || tz->
passive)
1193 thermal_zone_trip_update(tz, count, temp);
1200 thermal_zone_do_update(tz);
1208 thermal_zone_device_set_polling(tz, 0);
1233 if (tz->
ops->get_trip_hyst) {
1243 for (indx = 0; indx < tz->
trips; indx++) {
1246 "trip_point_%d_type", indx);
1259 "trip_point_%d_temp", indx);
1266 if (mask & (1 << indx)) {
1269 trip_point_temp_store;
1276 if (!tz->
ops->get_trip_hyst)
1279 "trip_point_%d_hyst", indx);
1286 if (tz->
ops->set_trip_hyst) {
1289 trip_point_hyst_store;
1302 for (indx = 0; indx < tz->
trips; indx++) {
1307 if (tz->
ops->get_trip_hyst)
1360 result = get_idr(&thermal_tz_idr, &thermal_idr_lock, &tz->
id);
1363 return ERR_PTR(result);
1368 tz->
device.class = &thermal_class;
1377 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->
id);
1379 return ERR_PTR(result);
1399 result = create_trip_attrs(tz, mask);
1403 for (count = 0; count < trips; count++) {
1404 tz->
ops->get_trip_type(tz, count, &trip_type);
1416 result = thermal_add_hwmon_sysfs(tz);
1424 result = ops->
bind(tz, pos);
1438 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->
id);
1440 return ERR_PTR(result);
1466 if (tz->
ops->unbind)
1468 tz->
ops->unbind(tz, cdev);
1471 thermal_zone_device_set_polling(tz, 0);
1476 if (tz->
ops->get_mode)
1478 remove_trip_attrs(tz);
1480 thermal_remove_hwmon_sysfs(tz);
1481 release_idr(&thermal_tz_idr, &thermal_idr_lock, tz->
id);
1490 static struct genl_family thermal_event_genl_family = {
1509 static unsigned int thermal_event_seqnum;
1520 msg_header =
genlmsg_put(skb, 0, thermal_event_seqnum++,
1521 &thermal_event_genl_family, 0,
1537 thermal_event = nla_data(attr);
1538 if (!thermal_event) {
1549 result = genlmsg_end(skb, msg_header);
1555 result = genlmsg_multicast(skb, 0, thermal_event_mcgrp.
id,
GFP_ATOMIC);
1557 pr_info(
"failed to send netlink event:%d\n", result);
1563 static int genetlink_init(
void)
1572 &thermal_event_mcgrp);
1578 static void genetlink_exit(
void)
1583 static inline int genetlink_init(
void) {
return 0; }
1584 static inline void genetlink_exit(
void) {}
1587 static int __init thermal_init(
void)
1598 result = genetlink_init();
1602 static void __exit thermal_exit(
void)