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