2018-05-07 17:52:29 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2013-07-03 19:14:28 +00:00
|
|
|
/*
|
|
|
|
* thermal_hwmon.c - Generic Thermal Management hwmon support.
|
|
|
|
*
|
|
|
|
* Code based on Intel thermal_core.c. Copyrights of the original code:
|
|
|
|
* Copyright (C) 2008 Intel Corp
|
|
|
|
* Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
|
|
|
|
* Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Texas Instruments
|
|
|
|
* Copyright (C) 2013 Eduardo Valentin <eduardo.valentin@ti.com>
|
|
|
|
*/
|
2020-05-11 12:24:53 +00:00
|
|
|
#include <linux/err.h>
|
2020-05-11 12:24:54 +00:00
|
|
|
#include <linux/export.h>
|
2013-07-03 19:14:28 +00:00
|
|
|
#include <linux/hwmon.h>
|
|
|
|
#include <linux/slab.h>
|
2020-05-11 12:24:53 +00:00
|
|
|
#include <linux/thermal.h>
|
|
|
|
|
2013-07-03 19:14:28 +00:00
|
|
|
#include "thermal_hwmon.h"
|
2023-03-01 20:14:43 +00:00
|
|
|
#include "thermal_core.h"
|
2013-07-03 19:14:28 +00:00
|
|
|
|
|
|
|
/* hwmon sys I/F */
|
|
|
|
/* thermal zone devices with the same type share one hwmon device */
|
|
|
|
struct thermal_hwmon_device {
|
|
|
|
char type[THERMAL_NAME_LENGTH];
|
|
|
|
struct device *device;
|
|
|
|
int count;
|
|
|
|
struct list_head tz_list;
|
|
|
|
struct list_head node;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct thermal_hwmon_attr {
|
|
|
|
struct device_attribute attr;
|
|
|
|
char name[16];
|
|
|
|
};
|
|
|
|
|
|
|
|
/* one temperature input for each thermal zone */
|
|
|
|
struct thermal_hwmon_temp {
|
|
|
|
struct list_head hwmon_node;
|
|
|
|
struct thermal_zone_device *tz;
|
|
|
|
struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
|
|
|
|
struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
|
|
|
|
};
|
|
|
|
|
|
|
|
static LIST_HEAD(thermal_hwmon_list);
|
|
|
|
|
|
|
|
static DEFINE_MUTEX(thermal_hwmon_list_lock);
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
|
|
|
|
{
|
2015-07-24 06:12:54 +00:00
|
|
|
int temperature;
|
2013-07-03 19:14:28 +00:00
|
|
|
int ret;
|
|
|
|
struct thermal_hwmon_attr *hwmon_attr
|
|
|
|
= container_of(attr, struct thermal_hwmon_attr, attr);
|
|
|
|
struct thermal_hwmon_temp *temp
|
|
|
|
= container_of(hwmon_attr, struct thermal_hwmon_temp,
|
|
|
|
temp_input);
|
|
|
|
struct thermal_zone_device *tz = temp->tz;
|
|
|
|
|
|
|
|
ret = thermal_zone_get_temp(tz, &temperature);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2015-07-24 06:12:54 +00:00
|
|
|
return sprintf(buf, "%d\n", temperature);
|
2013-07-03 19:14:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
temp_crit_show(struct device *dev, struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct thermal_hwmon_attr *hwmon_attr
|
|
|
|
= container_of(attr, struct thermal_hwmon_attr, attr);
|
|
|
|
struct thermal_hwmon_temp *temp
|
|
|
|
= container_of(hwmon_attr, struct thermal_hwmon_temp,
|
|
|
|
temp_crit);
|
|
|
|
struct thermal_zone_device *tz = temp->tz;
|
2015-07-24 06:12:54 +00:00
|
|
|
int temperature;
|
2013-07-03 19:14:28 +00:00
|
|
|
int ret;
|
|
|
|
|
2024-10-10 22:05:25 +00:00
|
|
|
guard(thermal_zone)(tz);
|
2022-11-10 15:24:57 +00:00
|
|
|
|
2024-02-22 17:18:01 +00:00
|
|
|
ret = tz->ops.get_crit_temp(tz, &temperature);
|
2013-07-03 19:14:28 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2015-07-24 06:12:54 +00:00
|
|
|
return sprintf(buf, "%d\n", temperature);
|
2013-07-03 19:14:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static struct thermal_hwmon_device *
|
|
|
|
thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
|
|
|
|
{
|
|
|
|
struct thermal_hwmon_device *hwmon;
|
2019-07-26 13:32:36 +00:00
|
|
|
char type[THERMAL_NAME_LENGTH];
|
2013-07-03 19:14:28 +00:00
|
|
|
|
|
|
|
mutex_lock(&thermal_hwmon_list_lock);
|
2019-07-26 13:32:36 +00:00
|
|
|
list_for_each_entry(hwmon, &thermal_hwmon_list, node) {
|
|
|
|
strcpy(type, tz->type);
|
|
|
|
strreplace(type, '-', '_');
|
|
|
|
if (!strcmp(hwmon->type, type)) {
|
2013-07-03 19:14:28 +00:00
|
|
|
mutex_unlock(&thermal_hwmon_list_lock);
|
|
|
|
return hwmon;
|
|
|
|
}
|
2019-07-26 13:32:36 +00:00
|
|
|
}
|
2013-07-03 19:14:28 +00:00
|
|
|
mutex_unlock(&thermal_hwmon_list_lock);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the temperature input matching a given thermal zone */
|
|
|
|
static struct thermal_hwmon_temp *
|
|
|
|
thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
|
|
|
|
const struct thermal_zone_device *tz)
|
|
|
|
{
|
|
|
|
struct thermal_hwmon_temp *temp;
|
|
|
|
|
|
|
|
mutex_lock(&thermal_hwmon_list_lock);
|
|
|
|
list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
|
|
|
|
if (temp->tz == tz) {
|
|
|
|
mutex_unlock(&thermal_hwmon_list_lock);
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
mutex_unlock(&thermal_hwmon_list_lock);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-05-21 08:33:27 +00:00
|
|
|
static bool thermal_zone_crit_temp_valid(struct thermal_zone_device *tz)
|
|
|
|
{
|
2015-07-24 06:12:54 +00:00
|
|
|
int temp;
|
2024-02-22 17:18:01 +00:00
|
|
|
return tz->ops.get_crit_temp && !tz->ops.get_crit_temp(tz, &temp);
|
2014-05-21 08:33:27 +00:00
|
|
|
}
|
|
|
|
|
2013-07-03 19:14:28 +00:00
|
|
|
int thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
|
|
|
|
{
|
|
|
|
struct thermal_hwmon_device *hwmon;
|
|
|
|
struct thermal_hwmon_temp *temp;
|
|
|
|
int new_hwmon_device = 1;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
hwmon = thermal_hwmon_lookup_by_type(tz);
|
|
|
|
if (hwmon) {
|
|
|
|
new_hwmon_device = 0;
|
|
|
|
goto register_sys_interface;
|
|
|
|
}
|
|
|
|
|
|
|
|
hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL);
|
|
|
|
if (!hwmon)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&hwmon->tz_list);
|
2022-08-18 21:01:11 +00:00
|
|
|
strscpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
|
2018-07-10 15:40:34 +00:00
|
|
|
strreplace(hwmon->type, '-', '_');
|
2022-05-11 13:36:29 +00:00
|
|
|
hwmon->device = hwmon_device_register_for_thermal(&tz->device,
|
|
|
|
hwmon->type, hwmon);
|
2013-07-03 19:14:28 +00:00
|
|
|
if (IS_ERR(hwmon->device)) {
|
|
|
|
result = PTR_ERR(hwmon->device);
|
|
|
|
goto free_mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
register_sys_interface:
|
|
|
|
temp = kzalloc(sizeof(*temp), GFP_KERNEL);
|
|
|
|
if (!temp) {
|
|
|
|
result = -ENOMEM;
|
|
|
|
goto unregister_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
temp->tz = tz;
|
|
|
|
hwmon->count++;
|
|
|
|
|
|
|
|
snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
|
|
|
|
"temp%d_input", hwmon->count);
|
|
|
|
temp->temp_input.attr.attr.name = temp->temp_input.name;
|
|
|
|
temp->temp_input.attr.attr.mode = 0444;
|
|
|
|
temp->temp_input.attr.show = temp_input_show;
|
|
|
|
sysfs_attr_init(&temp->temp_input.attr.attr);
|
|
|
|
result = device_create_file(hwmon->device, &temp->temp_input.attr);
|
|
|
|
if (result)
|
|
|
|
goto free_temp_mem;
|
|
|
|
|
2014-05-21 08:33:27 +00:00
|
|
|
if (thermal_zone_crit_temp_valid(tz)) {
|
|
|
|
snprintf(temp->temp_crit.name,
|
|
|
|
sizeof(temp->temp_crit.name),
|
2013-07-03 19:14:28 +00:00
|
|
|
"temp%d_crit", hwmon->count);
|
2014-05-21 08:33:27 +00:00
|
|
|
temp->temp_crit.attr.attr.name = temp->temp_crit.name;
|
|
|
|
temp->temp_crit.attr.attr.mode = 0444;
|
|
|
|
temp->temp_crit.attr.show = temp_crit_show;
|
|
|
|
sysfs_attr_init(&temp->temp_crit.attr.attr);
|
|
|
|
result = device_create_file(hwmon->device,
|
|
|
|
&temp->temp_crit.attr);
|
|
|
|
if (result)
|
|
|
|
goto unregister_input;
|
2013-07-03 19:14:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mutex_lock(&thermal_hwmon_list_lock);
|
|
|
|
if (new_hwmon_device)
|
|
|
|
list_add_tail(&hwmon->node, &thermal_hwmon_list);
|
|
|
|
list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
|
|
|
|
mutex_unlock(&thermal_hwmon_list_lock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
unregister_input:
|
|
|
|
device_remove_file(hwmon->device, &temp->temp_input.attr);
|
|
|
|
free_temp_mem:
|
|
|
|
kfree(temp);
|
|
|
|
unregister_name:
|
2018-01-13 14:08:49 +00:00
|
|
|
if (new_hwmon_device)
|
2013-07-03 19:14:28 +00:00
|
|
|
hwmon_device_unregister(hwmon->device);
|
|
|
|
free_mem:
|
2020-11-02 02:31:21 +00:00
|
|
|
kfree(hwmon);
|
2013-07-03 19:14:28 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2016-07-04 07:19:32 +00:00
|
|
|
EXPORT_SYMBOL_GPL(thermal_add_hwmon_sysfs);
|
2013-07-03 19:14:28 +00:00
|
|
|
|
|
|
|
void thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
|
|
|
|
{
|
|
|
|
struct thermal_hwmon_device *hwmon;
|
|
|
|
struct thermal_hwmon_temp *temp;
|
|
|
|
|
|
|
|
hwmon = thermal_hwmon_lookup_by_type(tz);
|
|
|
|
if (unlikely(!hwmon)) {
|
|
|
|
/* Should never happen... */
|
2023-03-29 09:00:55 +00:00
|
|
|
dev_dbg(&tz->device, "hwmon device lookup failed!\n");
|
2013-07-03 19:14:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
temp = thermal_hwmon_lookup_temp(hwmon, tz);
|
|
|
|
if (unlikely(!temp)) {
|
|
|
|
/* Should never happen... */
|
2023-03-29 17:44:21 +00:00
|
|
|
dev_dbg(&tz->device, "temperature input lookup failed!\n");
|
2013-07-03 19:14:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
device_remove_file(hwmon->device, &temp->temp_input.attr);
|
2014-05-21 08:33:27 +00:00
|
|
|
if (thermal_zone_crit_temp_valid(tz))
|
2013-07-03 19:14:28 +00:00
|
|
|
device_remove_file(hwmon->device, &temp->temp_crit.attr);
|
|
|
|
|
|
|
|
mutex_lock(&thermal_hwmon_list_lock);
|
|
|
|
list_del(&temp->hwmon_node);
|
|
|
|
kfree(temp);
|
|
|
|
if (!list_empty(&hwmon->tz_list)) {
|
|
|
|
mutex_unlock(&thermal_hwmon_list_lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
list_del(&hwmon->node);
|
|
|
|
mutex_unlock(&thermal_hwmon_list_lock);
|
|
|
|
|
|
|
|
hwmon_device_unregister(hwmon->device);
|
|
|
|
kfree(hwmon);
|
|
|
|
}
|
2016-07-04 07:19:32 +00:00
|
|
|
EXPORT_SYMBOL_GPL(thermal_remove_hwmon_sysfs);
|
2019-12-10 16:41:52 +00:00
|
|
|
|
|
|
|
static void devm_thermal_hwmon_release(struct device *dev, void *res)
|
|
|
|
{
|
|
|
|
thermal_remove_hwmon_sysfs(*(struct thermal_zone_device **)res);
|
|
|
|
}
|
|
|
|
|
2023-03-01 20:14:36 +00:00
|
|
|
int devm_thermal_add_hwmon_sysfs(struct device *dev, struct thermal_zone_device *tz)
|
2019-12-10 16:41:52 +00:00
|
|
|
{
|
|
|
|
struct thermal_zone_device **ptr;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ptr = devres_alloc(devm_thermal_hwmon_release, sizeof(*ptr),
|
|
|
|
GFP_KERNEL);
|
2023-06-20 09:07:22 +00:00
|
|
|
if (!ptr) {
|
|
|
|
dev_warn(dev, "Failed to allocate device resource data\n");
|
2019-12-10 16:41:52 +00:00
|
|
|
return -ENOMEM;
|
2023-06-20 09:07:22 +00:00
|
|
|
}
|
2019-12-10 16:41:52 +00:00
|
|
|
|
|
|
|
ret = thermal_add_hwmon_sysfs(tz);
|
|
|
|
if (ret) {
|
2023-06-20 09:07:22 +00:00
|
|
|
dev_warn(dev, "Failed to add hwmon sysfs attributes\n");
|
2019-12-10 16:41:52 +00:00
|
|
|
devres_free(ptr);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ptr = tz;
|
2023-03-01 20:14:36 +00:00
|
|
|
devres_add(dev, ptr);
|
2019-12-10 16:41:52 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(devm_thermal_add_hwmon_sysfs);
|
2022-05-11 13:36:29 +00:00
|
|
|
|
module: Convert symbol namespace to string literal
Clean up the existing export namespace code along the same lines of
commit 33def8498fdd ("treewide: Convert macro and uses of __section(foo)
to __section("foo")") and for the same reason, it is not desired for the
namespace argument to be a macro expansion itself.
Scripted using
git grep -l -e MODULE_IMPORT_NS -e EXPORT_SYMBOL_NS | while read file;
do
awk -i inplace '
/^#define EXPORT_SYMBOL_NS/ {
gsub(/__stringify\(ns\)/, "ns");
print;
next;
}
/^#define MODULE_IMPORT_NS/ {
gsub(/__stringify\(ns\)/, "ns");
print;
next;
}
/MODULE_IMPORT_NS/ {
$0 = gensub(/MODULE_IMPORT_NS\(([^)]*)\)/, "MODULE_IMPORT_NS(\"\\1\")", "g");
}
/EXPORT_SYMBOL_NS/ {
if ($0 ~ /(EXPORT_SYMBOL_NS[^(]*)\(([^,]+),/) {
if ($0 !~ /(EXPORT_SYMBOL_NS[^(]*)\(([^,]+), ([^)]+)\)/ &&
$0 !~ /(EXPORT_SYMBOL_NS[^(]*)\(\)/ &&
$0 !~ /^my/) {
getline line;
gsub(/[[:space:]]*\\$/, "");
gsub(/[[:space:]]/, "", line);
$0 = $0 " " line;
}
$0 = gensub(/(EXPORT_SYMBOL_NS[^(]*)\(([^,]+), ([^)]+)\)/,
"\\1(\\2, \"\\3\")", "g");
}
}
{ print }' $file;
done
Requested-by: Masahiro Yamada <masahiroy@kernel.org>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://mail.google.com/mail/u/2/#inbox/FMfcgzQXKWgMmjdFwwdsfgxzKpVHWPlc
Acked-by: Greg KH <gregkh@linuxfoundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2024-12-02 14:59:47 +00:00
|
|
|
MODULE_IMPORT_NS("HWMON_THERMAL");
|