mfd: kempld-core: Constify variables that point to const structure
[linux-2.6-block.git] / drivers / thermal / thermal_hwmon.c
CommitLineData
7e3c0381 1// SPDX-License-Identifier: GPL-2.0
0dd88793
EV
2/*
3 * thermal_hwmon.c - Generic Thermal Management hwmon support.
4 *
5 * Code based on Intel thermal_core.c. Copyrights of the original code:
6 * Copyright (C) 2008 Intel Corp
7 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
8 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
9 *
10 * Copyright (C) 2013 Texas Instruments
11 * Copyright (C) 2013 Eduardo Valentin <eduardo.valentin@ti.com>
0dd88793
EV
12 */
13#include <linux/hwmon.h>
14#include <linux/thermal.h>
15#include <linux/slab.h>
16#include <linux/err.h>
17#include "thermal_hwmon.h"
18
19/* hwmon sys I/F */
20/* thermal zone devices with the same type share one hwmon device */
21struct thermal_hwmon_device {
22 char type[THERMAL_NAME_LENGTH];
23 struct device *device;
24 int count;
25 struct list_head tz_list;
26 struct list_head node;
27};
28
29struct thermal_hwmon_attr {
30 struct device_attribute attr;
31 char name[16];
32};
33
34/* one temperature input for each thermal zone */
35struct thermal_hwmon_temp {
36 struct list_head hwmon_node;
37 struct thermal_zone_device *tz;
38 struct thermal_hwmon_attr temp_input; /* hwmon sys attr */
39 struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */
40};
41
42static LIST_HEAD(thermal_hwmon_list);
43
44static DEFINE_MUTEX(thermal_hwmon_list_lock);
45
0dd88793
EV
46static ssize_t
47temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
48{
17e8351a 49 int temperature;
0dd88793
EV
50 int ret;
51 struct thermal_hwmon_attr *hwmon_attr
52 = container_of(attr, struct thermal_hwmon_attr, attr);
53 struct thermal_hwmon_temp *temp
54 = container_of(hwmon_attr, struct thermal_hwmon_temp,
55 temp_input);
56 struct thermal_zone_device *tz = temp->tz;
57
58 ret = thermal_zone_get_temp(tz, &temperature);
59
60 if (ret)
61 return ret;
62
17e8351a 63 return sprintf(buf, "%d\n", temperature);
0dd88793
EV
64}
65
66static ssize_t
67temp_crit_show(struct device *dev, struct device_attribute *attr, char *buf)
68{
69 struct thermal_hwmon_attr *hwmon_attr
70 = container_of(attr, struct thermal_hwmon_attr, attr);
71 struct thermal_hwmon_temp *temp
72 = container_of(hwmon_attr, struct thermal_hwmon_temp,
73 temp_crit);
74 struct thermal_zone_device *tz = temp->tz;
17e8351a 75 int temperature;
0dd88793
EV
76 int ret;
77
f37fabb8 78 ret = tz->ops->get_crit_temp(tz, &temperature);
0dd88793
EV
79 if (ret)
80 return ret;
81
17e8351a 82 return sprintf(buf, "%d\n", temperature);
0dd88793
EV
83}
84
85
86static struct thermal_hwmon_device *
87thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
88{
89 struct thermal_hwmon_device *hwmon;
90
91 mutex_lock(&thermal_hwmon_list_lock);
92 list_for_each_entry(hwmon, &thermal_hwmon_list, node)
93 if (!strcmp(hwmon->type, tz->type)) {
94 mutex_unlock(&thermal_hwmon_list_lock);
95 return hwmon;
96 }
97 mutex_unlock(&thermal_hwmon_list_lock);
98
99 return NULL;
100}
101
102/* Find the temperature input matching a given thermal zone */
103static struct thermal_hwmon_temp *
104thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
105 const struct thermal_zone_device *tz)
106{
107 struct thermal_hwmon_temp *temp;
108
109 mutex_lock(&thermal_hwmon_list_lock);
110 list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
111 if (temp->tz == tz) {
112 mutex_unlock(&thermal_hwmon_list_lock);
113 return temp;
114 }
115 mutex_unlock(&thermal_hwmon_list_lock);
116
117 return NULL;
118}
119
e8db5d67
AL
120static bool thermal_zone_crit_temp_valid(struct thermal_zone_device *tz)
121{
17e8351a 122 int temp;
e8db5d67
AL
123 return tz->ops->get_crit_temp && !tz->ops->get_crit_temp(tz, &temp);
124}
125
0dd88793
EV
126int thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
127{
128 struct thermal_hwmon_device *hwmon;
129 struct thermal_hwmon_temp *temp;
130 int new_hwmon_device = 1;
131 int result;
132
133 hwmon = thermal_hwmon_lookup_by_type(tz);
134 if (hwmon) {
135 new_hwmon_device = 0;
136 goto register_sys_interface;
137 }
138
139 hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL);
140 if (!hwmon)
141 return -ENOMEM;
142
143 INIT_LIST_HEAD(&hwmon->tz_list);
144 strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
e782bc16
FE
145 hwmon->device = hwmon_device_register_with_info(NULL, hwmon->type,
146 hwmon, NULL, NULL);
0dd88793
EV
147 if (IS_ERR(hwmon->device)) {
148 result = PTR_ERR(hwmon->device);
149 goto free_mem;
150 }
0dd88793
EV
151
152 register_sys_interface:
153 temp = kzalloc(sizeof(*temp), GFP_KERNEL);
154 if (!temp) {
155 result = -ENOMEM;
156 goto unregister_name;
157 }
158
159 temp->tz = tz;
160 hwmon->count++;
161
162 snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
163 "temp%d_input", hwmon->count);
164 temp->temp_input.attr.attr.name = temp->temp_input.name;
165 temp->temp_input.attr.attr.mode = 0444;
166 temp->temp_input.attr.show = temp_input_show;
167 sysfs_attr_init(&temp->temp_input.attr.attr);
168 result = device_create_file(hwmon->device, &temp->temp_input.attr);
169 if (result)
170 goto free_temp_mem;
171
e8db5d67
AL
172 if (thermal_zone_crit_temp_valid(tz)) {
173 snprintf(temp->temp_crit.name,
174 sizeof(temp->temp_crit.name),
0dd88793 175 "temp%d_crit", hwmon->count);
e8db5d67
AL
176 temp->temp_crit.attr.attr.name = temp->temp_crit.name;
177 temp->temp_crit.attr.attr.mode = 0444;
178 temp->temp_crit.attr.show = temp_crit_show;
179 sysfs_attr_init(&temp->temp_crit.attr.attr);
180 result = device_create_file(hwmon->device,
181 &temp->temp_crit.attr);
182 if (result)
183 goto unregister_input;
0dd88793
EV
184 }
185
186 mutex_lock(&thermal_hwmon_list_lock);
187 if (new_hwmon_device)
188 list_add_tail(&hwmon->node, &thermal_hwmon_list);
189 list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
190 mutex_unlock(&thermal_hwmon_list_lock);
191
192 return 0;
193
194 unregister_input:
195 device_remove_file(hwmon->device, &temp->temp_input.attr);
196 free_temp_mem:
197 kfree(temp);
198 unregister_name:
e782bc16 199 if (new_hwmon_device)
0dd88793 200 hwmon_device_unregister(hwmon->device);
0dd88793
EV
201 free_mem:
202 if (new_hwmon_device)
203 kfree(hwmon);
204
205 return result;
206}
f4c59243 207EXPORT_SYMBOL_GPL(thermal_add_hwmon_sysfs);
0dd88793
EV
208
209void thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
210{
211 struct thermal_hwmon_device *hwmon;
212 struct thermal_hwmon_temp *temp;
213
214 hwmon = thermal_hwmon_lookup_by_type(tz);
215 if (unlikely(!hwmon)) {
216 /* Should never happen... */
217 dev_dbg(&tz->device, "hwmon device lookup failed!\n");
218 return;
219 }
220
221 temp = thermal_hwmon_lookup_temp(hwmon, tz);
222 if (unlikely(!temp)) {
223 /* Should never happen... */
224 dev_dbg(&tz->device, "temperature input lookup failed!\n");
225 return;
226 }
227
228 device_remove_file(hwmon->device, &temp->temp_input.attr);
e8db5d67 229 if (thermal_zone_crit_temp_valid(tz))
0dd88793
EV
230 device_remove_file(hwmon->device, &temp->temp_crit.attr);
231
232 mutex_lock(&thermal_hwmon_list_lock);
233 list_del(&temp->hwmon_node);
234 kfree(temp);
235 if (!list_empty(&hwmon->tz_list)) {
236 mutex_unlock(&thermal_hwmon_list_lock);
237 return;
238 }
239 list_del(&hwmon->node);
240 mutex_unlock(&thermal_hwmon_list_lock);
241
0dd88793
EV
242 hwmon_device_unregister(hwmon->device);
243 kfree(hwmon);
244}
f4c59243 245EXPORT_SYMBOL_GPL(thermal_remove_hwmon_sysfs);