Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0
  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>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 12 */
 13#include <linux/err.h>
 14#include <linux/export.h>
 15#include <linux/hwmon.h>
 16#include <linux/slab.h>
 17#include <linux/thermal.h>
 18
 
 19#include "thermal_hwmon.h"
 20#include "thermal_core.h"
 21
 22/* hwmon sys I/F */
 23/* thermal zone devices with the same type share one hwmon device */
 24struct thermal_hwmon_device {
 25	char type[THERMAL_NAME_LENGTH];
 26	struct device *device;
 27	int count;
 28	struct list_head tz_list;
 29	struct list_head node;
 30};
 31
 32struct thermal_hwmon_attr {
 33	struct device_attribute attr;
 34	char name[16];
 35};
 36
 37/* one temperature input for each thermal zone */
 38struct thermal_hwmon_temp {
 39	struct list_head hwmon_node;
 40	struct thermal_zone_device *tz;
 41	struct thermal_hwmon_attr temp_input;	/* hwmon sys attr */
 42	struct thermal_hwmon_attr temp_crit;	/* hwmon sys attr */
 43};
 44
 45static LIST_HEAD(thermal_hwmon_list);
 46
 47static DEFINE_MUTEX(thermal_hwmon_list_lock);
 48
 49static ssize_t
 
 
 
 
 
 
 
 
 50temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
 51{
 52	int temperature;
 53	int ret;
 54	struct thermal_hwmon_attr *hwmon_attr
 55			= container_of(attr, struct thermal_hwmon_attr, attr);
 56	struct thermal_hwmon_temp *temp
 57			= container_of(hwmon_attr, struct thermal_hwmon_temp,
 58				       temp_input);
 59	struct thermal_zone_device *tz = temp->tz;
 60
 61	ret = thermal_zone_get_temp(tz, &temperature);
 62
 63	if (ret)
 64		return ret;
 65
 66	return sprintf(buf, "%d\n", temperature);
 67}
 68
 69static ssize_t
 70temp_crit_show(struct device *dev, struct device_attribute *attr, char *buf)
 71{
 72	struct thermal_hwmon_attr *hwmon_attr
 73			= container_of(attr, struct thermal_hwmon_attr, attr);
 74	struct thermal_hwmon_temp *temp
 75			= container_of(hwmon_attr, struct thermal_hwmon_temp,
 76				       temp_crit);
 77	struct thermal_zone_device *tz = temp->tz;
 78	int temperature;
 79	int ret;
 80
 81	guard(thermal_zone)(tz);
 82
 83	ret = tz->ops.get_crit_temp(tz, &temperature);
 84	if (ret)
 85		return ret;
 86
 87	return sprintf(buf, "%d\n", temperature);
 88}
 89
 90
 91static struct thermal_hwmon_device *
 92thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
 93{
 94	struct thermal_hwmon_device *hwmon;
 95	char type[THERMAL_NAME_LENGTH];
 96
 97	mutex_lock(&thermal_hwmon_list_lock);
 98	list_for_each_entry(hwmon, &thermal_hwmon_list, node) {
 99		strcpy(type, tz->type);
100		strreplace(type, '-', '_');
101		if (!strcmp(hwmon->type, type)) {
102			mutex_unlock(&thermal_hwmon_list_lock);
103			return hwmon;
104		}
105	}
106	mutex_unlock(&thermal_hwmon_list_lock);
107
108	return NULL;
109}
110
111/* Find the temperature input matching a given thermal zone */
112static struct thermal_hwmon_temp *
113thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
114			  const struct thermal_zone_device *tz)
115{
116	struct thermal_hwmon_temp *temp;
117
118	mutex_lock(&thermal_hwmon_list_lock);
119	list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
120		if (temp->tz == tz) {
121			mutex_unlock(&thermal_hwmon_list_lock);
122			return temp;
123		}
124	mutex_unlock(&thermal_hwmon_list_lock);
125
126	return NULL;
127}
128
129static bool thermal_zone_crit_temp_valid(struct thermal_zone_device *tz)
130{
131	int temp;
132	return tz->ops.get_crit_temp && !tz->ops.get_crit_temp(tz, &temp);
133}
134
135int thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
136{
137	struct thermal_hwmon_device *hwmon;
138	struct thermal_hwmon_temp *temp;
139	int new_hwmon_device = 1;
140	int result;
141
142	hwmon = thermal_hwmon_lookup_by_type(tz);
143	if (hwmon) {
144		new_hwmon_device = 0;
145		goto register_sys_interface;
146	}
147
148	hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL);
149	if (!hwmon)
150		return -ENOMEM;
151
152	INIT_LIST_HEAD(&hwmon->tz_list);
153	strscpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
154	strreplace(hwmon->type, '-', '_');
155	hwmon->device = hwmon_device_register_for_thermal(&tz->device,
156							  hwmon->type, hwmon);
157	if (IS_ERR(hwmon->device)) {
158		result = PTR_ERR(hwmon->device);
159		goto free_mem;
160	}
 
 
 
 
161
162 register_sys_interface:
163	temp = kzalloc(sizeof(*temp), GFP_KERNEL);
164	if (!temp) {
165		result = -ENOMEM;
166		goto unregister_name;
167	}
168
169	temp->tz = tz;
170	hwmon->count++;
171
172	snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
173		 "temp%d_input", hwmon->count);
174	temp->temp_input.attr.attr.name = temp->temp_input.name;
175	temp->temp_input.attr.attr.mode = 0444;
176	temp->temp_input.attr.show = temp_input_show;
177	sysfs_attr_init(&temp->temp_input.attr.attr);
178	result = device_create_file(hwmon->device, &temp->temp_input.attr);
179	if (result)
180		goto free_temp_mem;
181
182	if (thermal_zone_crit_temp_valid(tz)) {
183		snprintf(temp->temp_crit.name,
184				sizeof(temp->temp_crit.name),
185				"temp%d_crit", hwmon->count);
186		temp->temp_crit.attr.attr.name = temp->temp_crit.name;
187		temp->temp_crit.attr.attr.mode = 0444;
188		temp->temp_crit.attr.show = temp_crit_show;
189		sysfs_attr_init(&temp->temp_crit.attr.attr);
190		result = device_create_file(hwmon->device,
191					    &temp->temp_crit.attr);
192		if (result)
193			goto unregister_input;
194	}
195
196	mutex_lock(&thermal_hwmon_list_lock);
197	if (new_hwmon_device)
198		list_add_tail(&hwmon->node, &thermal_hwmon_list);
199	list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
200	mutex_unlock(&thermal_hwmon_list_lock);
201
202	return 0;
203
204 unregister_input:
205	device_remove_file(hwmon->device, &temp->temp_input.attr);
206 free_temp_mem:
207	kfree(temp);
208 unregister_name:
209	if (new_hwmon_device)
 
210		hwmon_device_unregister(hwmon->device);
 
211 free_mem:
212	kfree(hwmon);
 
213
214	return result;
215}
216EXPORT_SYMBOL_GPL(thermal_add_hwmon_sysfs);
217
218void thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
219{
220	struct thermal_hwmon_device *hwmon;
221	struct thermal_hwmon_temp *temp;
222
223	hwmon = thermal_hwmon_lookup_by_type(tz);
224	if (unlikely(!hwmon)) {
225		/* Should never happen... */
226		dev_dbg(&tz->device, "hwmon device lookup failed!\n");
227		return;
228	}
229
230	temp = thermal_hwmon_lookup_temp(hwmon, tz);
231	if (unlikely(!temp)) {
232		/* Should never happen... */
233		dev_dbg(&tz->device, "temperature input lookup failed!\n");
234		return;
235	}
236
237	device_remove_file(hwmon->device, &temp->temp_input.attr);
238	if (thermal_zone_crit_temp_valid(tz))
239		device_remove_file(hwmon->device, &temp->temp_crit.attr);
240
241	mutex_lock(&thermal_hwmon_list_lock);
242	list_del(&temp->hwmon_node);
243	kfree(temp);
244	if (!list_empty(&hwmon->tz_list)) {
245		mutex_unlock(&thermal_hwmon_list_lock);
246		return;
247	}
248	list_del(&hwmon->node);
249	mutex_unlock(&thermal_hwmon_list_lock);
250
 
251	hwmon_device_unregister(hwmon->device);
252	kfree(hwmon);
253}
254EXPORT_SYMBOL_GPL(thermal_remove_hwmon_sysfs);
255
256static void devm_thermal_hwmon_release(struct device *dev, void *res)
257{
258	thermal_remove_hwmon_sysfs(*(struct thermal_zone_device **)res);
259}
260
261int devm_thermal_add_hwmon_sysfs(struct device *dev, struct thermal_zone_device *tz)
262{
263	struct thermal_zone_device **ptr;
264	int ret;
265
266	ptr = devres_alloc(devm_thermal_hwmon_release, sizeof(*ptr),
267			   GFP_KERNEL);
268	if (!ptr) {
269		dev_warn(dev, "Failed to allocate device resource data\n");
270		return -ENOMEM;
271	}
272
273	ret = thermal_add_hwmon_sysfs(tz);
274	if (ret) {
275		dev_warn(dev, "Failed to add hwmon sysfs attributes\n");
276		devres_free(ptr);
277		return ret;
278	}
279
280	*ptr = tz;
281	devres_add(dev, ptr);
282
283	return ret;
284}
285EXPORT_SYMBOL_GPL(devm_thermal_add_hwmon_sysfs);
286
287MODULE_IMPORT_NS("HWMON_THERMAL");
v4.6
 
  1/*
  2 *  thermal_hwmon.c - Generic Thermal Management hwmon support.
  3 *
  4 *  Code based on Intel thermal_core.c. Copyrights of the original code:
  5 *  Copyright (C) 2008 Intel Corp
  6 *  Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
  7 *  Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
  8 *
  9 *  Copyright (C) 2013 Texas Instruments
 10 *  Copyright (C) 2013 Eduardo Valentin <eduardo.valentin@ti.com>
 11 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 12 *
 13 *  This program is free software; you can redistribute it and/or modify
 14 *  it under the terms of the GNU General Public License as published by
 15 *  the Free Software Foundation; version 2 of the License.
 16 *
 17 *  This program is distributed in the hope that it will be useful, but
 18 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 19 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 20 *  General Public License for more details.
 21 *
 22 *  You should have received a copy of the GNU General Public License along
 23 *  with this program; if not, write to the Free Software Foundation, Inc.,
 24 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 25 *
 26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 27 */
 
 
 28#include <linux/hwmon.h>
 
 29#include <linux/thermal.h>
 30#include <linux/slab.h>
 31#include <linux/err.h>
 32#include "thermal_hwmon.h"
 
 33
 34/* hwmon sys I/F */
 35/* thermal zone devices with the same type share one hwmon device */
 36struct thermal_hwmon_device {
 37	char type[THERMAL_NAME_LENGTH];
 38	struct device *device;
 39	int count;
 40	struct list_head tz_list;
 41	struct list_head node;
 42};
 43
 44struct thermal_hwmon_attr {
 45	struct device_attribute attr;
 46	char name[16];
 47};
 48
 49/* one temperature input for each thermal zone */
 50struct thermal_hwmon_temp {
 51	struct list_head hwmon_node;
 52	struct thermal_zone_device *tz;
 53	struct thermal_hwmon_attr temp_input;	/* hwmon sys attr */
 54	struct thermal_hwmon_attr temp_crit;	/* hwmon sys attr */
 55};
 56
 57static LIST_HEAD(thermal_hwmon_list);
 58
 59static DEFINE_MUTEX(thermal_hwmon_list_lock);
 60
 61static ssize_t
 62name_show(struct device *dev, struct device_attribute *attr, char *buf)
 63{
 64	struct thermal_hwmon_device *hwmon = dev_get_drvdata(dev);
 65	return sprintf(buf, "%s\n", hwmon->type);
 66}
 67static DEVICE_ATTR(name, 0444, name_show, NULL);
 68
 69static ssize_t
 70temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
 71{
 72	int temperature;
 73	int ret;
 74	struct thermal_hwmon_attr *hwmon_attr
 75			= container_of(attr, struct thermal_hwmon_attr, attr);
 76	struct thermal_hwmon_temp *temp
 77			= container_of(hwmon_attr, struct thermal_hwmon_temp,
 78				       temp_input);
 79	struct thermal_zone_device *tz = temp->tz;
 80
 81	ret = thermal_zone_get_temp(tz, &temperature);
 82
 83	if (ret)
 84		return ret;
 85
 86	return sprintf(buf, "%d\n", temperature);
 87}
 88
 89static ssize_t
 90temp_crit_show(struct device *dev, struct device_attribute *attr, char *buf)
 91{
 92	struct thermal_hwmon_attr *hwmon_attr
 93			= container_of(attr, struct thermal_hwmon_attr, attr);
 94	struct thermal_hwmon_temp *temp
 95			= container_of(hwmon_attr, struct thermal_hwmon_temp,
 96				       temp_crit);
 97	struct thermal_zone_device *tz = temp->tz;
 98	int temperature;
 99	int ret;
100
101	ret = tz->ops->get_trip_temp(tz, 0, &temperature);
 
 
102	if (ret)
103		return ret;
104
105	return sprintf(buf, "%d\n", temperature);
106}
107
108
109static struct thermal_hwmon_device *
110thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
111{
112	struct thermal_hwmon_device *hwmon;
 
113
114	mutex_lock(&thermal_hwmon_list_lock);
115	list_for_each_entry(hwmon, &thermal_hwmon_list, node)
116		if (!strcmp(hwmon->type, tz->type)) {
 
 
117			mutex_unlock(&thermal_hwmon_list_lock);
118			return hwmon;
119		}
 
120	mutex_unlock(&thermal_hwmon_list_lock);
121
122	return NULL;
123}
124
125/* Find the temperature input matching a given thermal zone */
126static struct thermal_hwmon_temp *
127thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
128			  const struct thermal_zone_device *tz)
129{
130	struct thermal_hwmon_temp *temp;
131
132	mutex_lock(&thermal_hwmon_list_lock);
133	list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
134		if (temp->tz == tz) {
135			mutex_unlock(&thermal_hwmon_list_lock);
136			return temp;
137		}
138	mutex_unlock(&thermal_hwmon_list_lock);
139
140	return NULL;
141}
142
143static bool thermal_zone_crit_temp_valid(struct thermal_zone_device *tz)
144{
145	int temp;
146	return tz->ops->get_crit_temp && !tz->ops->get_crit_temp(tz, &temp);
147}
148
149int thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
150{
151	struct thermal_hwmon_device *hwmon;
152	struct thermal_hwmon_temp *temp;
153	int new_hwmon_device = 1;
154	int result;
155
156	hwmon = thermal_hwmon_lookup_by_type(tz);
157	if (hwmon) {
158		new_hwmon_device = 0;
159		goto register_sys_interface;
160	}
161
162	hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL);
163	if (!hwmon)
164		return -ENOMEM;
165
166	INIT_LIST_HEAD(&hwmon->tz_list);
167	strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
168	hwmon->device = hwmon_device_register(NULL);
 
 
169	if (IS_ERR(hwmon->device)) {
170		result = PTR_ERR(hwmon->device);
171		goto free_mem;
172	}
173	dev_set_drvdata(hwmon->device, hwmon);
174	result = device_create_file(hwmon->device, &dev_attr_name);
175	if (result)
176		goto free_mem;
177
178 register_sys_interface:
179	temp = kzalloc(sizeof(*temp), GFP_KERNEL);
180	if (!temp) {
181		result = -ENOMEM;
182		goto unregister_name;
183	}
184
185	temp->tz = tz;
186	hwmon->count++;
187
188	snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
189		 "temp%d_input", hwmon->count);
190	temp->temp_input.attr.attr.name = temp->temp_input.name;
191	temp->temp_input.attr.attr.mode = 0444;
192	temp->temp_input.attr.show = temp_input_show;
193	sysfs_attr_init(&temp->temp_input.attr.attr);
194	result = device_create_file(hwmon->device, &temp->temp_input.attr);
195	if (result)
196		goto free_temp_mem;
197
198	if (thermal_zone_crit_temp_valid(tz)) {
199		snprintf(temp->temp_crit.name,
200				sizeof(temp->temp_crit.name),
201				"temp%d_crit", hwmon->count);
202		temp->temp_crit.attr.attr.name = temp->temp_crit.name;
203		temp->temp_crit.attr.attr.mode = 0444;
204		temp->temp_crit.attr.show = temp_crit_show;
205		sysfs_attr_init(&temp->temp_crit.attr.attr);
206		result = device_create_file(hwmon->device,
207					    &temp->temp_crit.attr);
208		if (result)
209			goto unregister_input;
210	}
211
212	mutex_lock(&thermal_hwmon_list_lock);
213	if (new_hwmon_device)
214		list_add_tail(&hwmon->node, &thermal_hwmon_list);
215	list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
216	mutex_unlock(&thermal_hwmon_list_lock);
217
218	return 0;
219
220 unregister_input:
221	device_remove_file(hwmon->device, &temp->temp_input.attr);
222 free_temp_mem:
223	kfree(temp);
224 unregister_name:
225	if (new_hwmon_device) {
226		device_remove_file(hwmon->device, &dev_attr_name);
227		hwmon_device_unregister(hwmon->device);
228	}
229 free_mem:
230	if (new_hwmon_device)
231		kfree(hwmon);
232
233	return result;
234}
 
235
236void thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
237{
238	struct thermal_hwmon_device *hwmon;
239	struct thermal_hwmon_temp *temp;
240
241	hwmon = thermal_hwmon_lookup_by_type(tz);
242	if (unlikely(!hwmon)) {
243		/* Should never happen... */
244		dev_dbg(&tz->device, "hwmon device lookup failed!\n");
245		return;
246	}
247
248	temp = thermal_hwmon_lookup_temp(hwmon, tz);
249	if (unlikely(!temp)) {
250		/* Should never happen... */
251		dev_dbg(&tz->device, "temperature input lookup failed!\n");
252		return;
253	}
254
255	device_remove_file(hwmon->device, &temp->temp_input.attr);
256	if (thermal_zone_crit_temp_valid(tz))
257		device_remove_file(hwmon->device, &temp->temp_crit.attr);
258
259	mutex_lock(&thermal_hwmon_list_lock);
260	list_del(&temp->hwmon_node);
261	kfree(temp);
262	if (!list_empty(&hwmon->tz_list)) {
263		mutex_unlock(&thermal_hwmon_list_lock);
264		return;
265	}
266	list_del(&hwmon->node);
267	mutex_unlock(&thermal_hwmon_list_lock);
268
269	device_remove_file(hwmon->device, &dev_attr_name);
270	hwmon_device_unregister(hwmon->device);
271	kfree(hwmon);
272}