1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
// SPDX-License-Identifier: GPL-2.0
/*
* 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>
*/
#include <linux/err.h>
#include <linux/export.h>
#include <linux/hwmon.h>
#include <linux/slab.h>
#include <linux/thermal.h>
#include "thermal_hwmon.h"
#include "thermal_core.h"
/*
* Needs to be large enough to hold a thermal zone type string followed by an
* underline character and a 32-bit integer in decimal representation.
*/
#define THERMAL_HWMON_NAME_LENGTH (THERMAL_NAME_LENGTH + 11)
/* hwmon sys I/F */
/* thermal zone devices with the same type share one hwmon device */
struct thermal_hwmon_device {
char name[THERMAL_HWMON_NAME_LENGTH];
struct device *device;
struct list_head node;
struct thermal_zone_device *tz;
};
static LIST_HEAD(thermal_hwmon_list);
static DEFINE_MUTEX(thermal_hwmon_list_lock);
static ssize_t
temp1_input_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
struct thermal_zone_device *tz = hwmon->tz;
int temperature;
int ret;
ret = thermal_zone_get_temp(tz, &temperature);
if (ret)
return ret;
return sysfs_emit(buf, "%d\n", temperature);
}
static ssize_t
temp1_crit_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
struct thermal_zone_device *tz = hwmon->tz;
int temperature;
int ret;
guard(thermal_zone)(tz);
ret = tz->ops.get_crit_temp(tz, &temperature);
if (ret)
return ret;
return sysfs_emit(buf, "%d\n", temperature);
}
static DEVICE_ATTR_RO(temp1_input);
static DEVICE_ATTR_RO(temp1_crit);
static struct attribute *thermal_hwmon_attrs[] = {
&dev_attr_temp1_input.attr,
&dev_attr_temp1_crit.attr,
NULL,
};
static umode_t thermal_hwmon_attr_is_visible(struct kobject *kobj,
struct attribute *a, int n)
{
if (a == &dev_attr_temp1_input.attr)
return a->mode;
if (a == &dev_attr_temp1_crit.attr) {
struct thermal_hwmon_device *hwmon = dev_get_drvdata(kobj_to_dev(kobj));
struct thermal_zone_device *tz = hwmon->tz;
int dummy;
if (tz->ops.get_crit_temp && !tz->ops.get_crit_temp(tz, &dummy))
return a->mode;
}
return 0;
}
static const struct attribute_group thermal_hwmon_group = {
.attrs = thermal_hwmon_attrs,
.is_visible = thermal_hwmon_attr_is_visible,
};
__ATTRIBUTE_GROUPS(thermal_hwmon);
int thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
{
struct thermal_hwmon_device *hwmon;
hwmon = kzalloc_obj(*hwmon);
if (!hwmon)
return -ENOMEM;
hwmon->tz = tz;
/*
* Append the thermal zone ID preceded by an underline character to the
* type to disambiguate the sensors command output.
*/
scnprintf(hwmon->name, THERMAL_HWMON_NAME_LENGTH, "%s_%d", tz->type, tz->id);
strreplace(hwmon->name, '-', '_');
hwmon->device = hwmon_device_register_for_thermal(&tz->device,
hwmon->name, hwmon,
thermal_hwmon_groups);
if (IS_ERR(hwmon->device)) {
int result = PTR_ERR(hwmon->device);
kfree(hwmon);
return result;
}
/* The list is needed for hwmon lookup during removal. */
mutex_lock(&thermal_hwmon_list_lock);
list_add_tail(&hwmon->node, &thermal_hwmon_list);
mutex_unlock(&thermal_hwmon_list_lock);
return 0;
}
EXPORT_SYMBOL_GPL(thermal_add_hwmon_sysfs);
static struct thermal_hwmon_device *
thermal_hwmon_lookup(const struct thermal_zone_device *tz)
{
struct thermal_hwmon_device *hwmon;
list_for_each_entry(hwmon, &thermal_hwmon_list, node) {
if (hwmon->tz == tz)
return hwmon;
}
return NULL;
}
void thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
{
struct thermal_hwmon_device *hwmon;
scoped_guard(mutex, &thermal_hwmon_list_lock) {
hwmon = thermal_hwmon_lookup(tz);
if (!hwmon)
return;
list_del(&hwmon->node);
}
hwmon_device_unregister(hwmon->device);
kfree(hwmon);
}
EXPORT_SYMBOL_GPL(thermal_remove_hwmon_sysfs);
static void devm_thermal_hwmon_release(struct device *dev, void *res)
{
thermal_remove_hwmon_sysfs(*(struct thermal_zone_device **)res);
}
int devm_thermal_add_hwmon_sysfs(struct device *dev, struct thermal_zone_device *tz)
{
struct thermal_zone_device **ptr;
int ret;
ptr = devres_alloc(devm_thermal_hwmon_release, sizeof(*ptr),
GFP_KERNEL);
if (!ptr) {
dev_warn(dev, "Failed to allocate device resource data\n");
return -ENOMEM;
}
ret = thermal_add_hwmon_sysfs(tz);
if (ret) {
dev_warn(dev, "Failed to add hwmon sysfs attributes\n");
devres_free(ptr);
return ret;
}
*ptr = tz;
devres_add(dev, ptr);
return ret;
}
EXPORT_SYMBOL_GPL(devm_thermal_add_hwmon_sysfs);
MODULE_IMPORT_NS("HWMON_THERMAL");
|