Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *  thermal_hwmon.c - Generic Thermal Management hwmon support.
0004  *
0005  *  Code based on Intel thermal_core.c. Copyrights of the original code:
0006  *  Copyright (C) 2008 Intel Corp
0007  *  Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com>
0008  *  Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com>
0009  *
0010  *  Copyright (C) 2013 Texas Instruments
0011  *  Copyright (C) 2013 Eduardo Valentin <eduardo.valentin@ti.com>
0012  */
0013 #include <linux/err.h>
0014 #include <linux/export.h>
0015 #include <linux/hwmon.h>
0016 #include <linux/slab.h>
0017 #include <linux/thermal.h>
0018 
0019 #include "thermal_hwmon.h"
0020 
0021 /* hwmon sys I/F */
0022 /* thermal zone devices with the same type share one hwmon device */
0023 struct thermal_hwmon_device {
0024     char type[THERMAL_NAME_LENGTH];
0025     struct device *device;
0026     int count;
0027     struct list_head tz_list;
0028     struct list_head node;
0029 };
0030 
0031 struct thermal_hwmon_attr {
0032     struct device_attribute attr;
0033     char name[16];
0034 };
0035 
0036 /* one temperature input for each thermal zone */
0037 struct thermal_hwmon_temp {
0038     struct list_head hwmon_node;
0039     struct thermal_zone_device *tz;
0040     struct thermal_hwmon_attr temp_input;   /* hwmon sys attr */
0041     struct thermal_hwmon_attr temp_crit;    /* hwmon sys attr */
0042 };
0043 
0044 static LIST_HEAD(thermal_hwmon_list);
0045 
0046 static DEFINE_MUTEX(thermal_hwmon_list_lock);
0047 
0048 static ssize_t
0049 temp_input_show(struct device *dev, struct device_attribute *attr, char *buf)
0050 {
0051     int temperature;
0052     int ret;
0053     struct thermal_hwmon_attr *hwmon_attr
0054             = container_of(attr, struct thermal_hwmon_attr, attr);
0055     struct thermal_hwmon_temp *temp
0056             = container_of(hwmon_attr, struct thermal_hwmon_temp,
0057                        temp_input);
0058     struct thermal_zone_device *tz = temp->tz;
0059 
0060     ret = thermal_zone_get_temp(tz, &temperature);
0061 
0062     if (ret)
0063         return ret;
0064 
0065     return sprintf(buf, "%d\n", temperature);
0066 }
0067 
0068 static ssize_t
0069 temp_crit_show(struct device *dev, struct device_attribute *attr, char *buf)
0070 {
0071     struct thermal_hwmon_attr *hwmon_attr
0072             = container_of(attr, struct thermal_hwmon_attr, attr);
0073     struct thermal_hwmon_temp *temp
0074             = container_of(hwmon_attr, struct thermal_hwmon_temp,
0075                        temp_crit);
0076     struct thermal_zone_device *tz = temp->tz;
0077     int temperature;
0078     int ret;
0079 
0080     ret = tz->ops->get_crit_temp(tz, &temperature);
0081     if (ret)
0082         return ret;
0083 
0084     return sprintf(buf, "%d\n", temperature);
0085 }
0086 
0087 
0088 static struct thermal_hwmon_device *
0089 thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz)
0090 {
0091     struct thermal_hwmon_device *hwmon;
0092     char type[THERMAL_NAME_LENGTH];
0093 
0094     mutex_lock(&thermal_hwmon_list_lock);
0095     list_for_each_entry(hwmon, &thermal_hwmon_list, node) {
0096         strcpy(type, tz->type);
0097         strreplace(type, '-', '_');
0098         if (!strcmp(hwmon->type, type)) {
0099             mutex_unlock(&thermal_hwmon_list_lock);
0100             return hwmon;
0101         }
0102     }
0103     mutex_unlock(&thermal_hwmon_list_lock);
0104 
0105     return NULL;
0106 }
0107 
0108 /* Find the temperature input matching a given thermal zone */
0109 static struct thermal_hwmon_temp *
0110 thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon,
0111               const struct thermal_zone_device *tz)
0112 {
0113     struct thermal_hwmon_temp *temp;
0114 
0115     mutex_lock(&thermal_hwmon_list_lock);
0116     list_for_each_entry(temp, &hwmon->tz_list, hwmon_node)
0117         if (temp->tz == tz) {
0118             mutex_unlock(&thermal_hwmon_list_lock);
0119             return temp;
0120         }
0121     mutex_unlock(&thermal_hwmon_list_lock);
0122 
0123     return NULL;
0124 }
0125 
0126 static bool thermal_zone_crit_temp_valid(struct thermal_zone_device *tz)
0127 {
0128     int temp;
0129     return tz->ops->get_crit_temp && !tz->ops->get_crit_temp(tz, &temp);
0130 }
0131 
0132 int thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
0133 {
0134     struct thermal_hwmon_device *hwmon;
0135     struct thermal_hwmon_temp *temp;
0136     int new_hwmon_device = 1;
0137     int result;
0138 
0139     hwmon = thermal_hwmon_lookup_by_type(tz);
0140     if (hwmon) {
0141         new_hwmon_device = 0;
0142         goto register_sys_interface;
0143     }
0144 
0145     hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL);
0146     if (!hwmon)
0147         return -ENOMEM;
0148 
0149     INIT_LIST_HEAD(&hwmon->tz_list);
0150     strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH);
0151     strreplace(hwmon->type, '-', '_');
0152     hwmon->device = hwmon_device_register_for_thermal(&tz->device,
0153                               hwmon->type, hwmon);
0154     if (IS_ERR(hwmon->device)) {
0155         result = PTR_ERR(hwmon->device);
0156         goto free_mem;
0157     }
0158 
0159  register_sys_interface:
0160     temp = kzalloc(sizeof(*temp), GFP_KERNEL);
0161     if (!temp) {
0162         result = -ENOMEM;
0163         goto unregister_name;
0164     }
0165 
0166     temp->tz = tz;
0167     hwmon->count++;
0168 
0169     snprintf(temp->temp_input.name, sizeof(temp->temp_input.name),
0170          "temp%d_input", hwmon->count);
0171     temp->temp_input.attr.attr.name = temp->temp_input.name;
0172     temp->temp_input.attr.attr.mode = 0444;
0173     temp->temp_input.attr.show = temp_input_show;
0174     sysfs_attr_init(&temp->temp_input.attr.attr);
0175     result = device_create_file(hwmon->device, &temp->temp_input.attr);
0176     if (result)
0177         goto free_temp_mem;
0178 
0179     if (thermal_zone_crit_temp_valid(tz)) {
0180         snprintf(temp->temp_crit.name,
0181                 sizeof(temp->temp_crit.name),
0182                 "temp%d_crit", hwmon->count);
0183         temp->temp_crit.attr.attr.name = temp->temp_crit.name;
0184         temp->temp_crit.attr.attr.mode = 0444;
0185         temp->temp_crit.attr.show = temp_crit_show;
0186         sysfs_attr_init(&temp->temp_crit.attr.attr);
0187         result = device_create_file(hwmon->device,
0188                         &temp->temp_crit.attr);
0189         if (result)
0190             goto unregister_input;
0191     }
0192 
0193     mutex_lock(&thermal_hwmon_list_lock);
0194     if (new_hwmon_device)
0195         list_add_tail(&hwmon->node, &thermal_hwmon_list);
0196     list_add_tail(&temp->hwmon_node, &hwmon->tz_list);
0197     mutex_unlock(&thermal_hwmon_list_lock);
0198 
0199     return 0;
0200 
0201  unregister_input:
0202     device_remove_file(hwmon->device, &temp->temp_input.attr);
0203  free_temp_mem:
0204     kfree(temp);
0205  unregister_name:
0206     if (new_hwmon_device)
0207         hwmon_device_unregister(hwmon->device);
0208  free_mem:
0209     kfree(hwmon);
0210 
0211     return result;
0212 }
0213 EXPORT_SYMBOL_GPL(thermal_add_hwmon_sysfs);
0214 
0215 void thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz)
0216 {
0217     struct thermal_hwmon_device *hwmon;
0218     struct thermal_hwmon_temp *temp;
0219 
0220     hwmon = thermal_hwmon_lookup_by_type(tz);
0221     if (unlikely(!hwmon)) {
0222         /* Should never happen... */
0223         dev_dbg(&tz->device, "hwmon device lookup failed!\n");
0224         return;
0225     }
0226 
0227     temp = thermal_hwmon_lookup_temp(hwmon, tz);
0228     if (unlikely(!temp)) {
0229         /* Should never happen... */
0230         dev_dbg(&tz->device, "temperature input lookup failed!\n");
0231         return;
0232     }
0233 
0234     device_remove_file(hwmon->device, &temp->temp_input.attr);
0235     if (thermal_zone_crit_temp_valid(tz))
0236         device_remove_file(hwmon->device, &temp->temp_crit.attr);
0237 
0238     mutex_lock(&thermal_hwmon_list_lock);
0239     list_del(&temp->hwmon_node);
0240     kfree(temp);
0241     if (!list_empty(&hwmon->tz_list)) {
0242         mutex_unlock(&thermal_hwmon_list_lock);
0243         return;
0244     }
0245     list_del(&hwmon->node);
0246     mutex_unlock(&thermal_hwmon_list_lock);
0247 
0248     hwmon_device_unregister(hwmon->device);
0249     kfree(hwmon);
0250 }
0251 EXPORT_SYMBOL_GPL(thermal_remove_hwmon_sysfs);
0252 
0253 static void devm_thermal_hwmon_release(struct device *dev, void *res)
0254 {
0255     thermal_remove_hwmon_sysfs(*(struct thermal_zone_device **)res);
0256 }
0257 
0258 int devm_thermal_add_hwmon_sysfs(struct thermal_zone_device *tz)
0259 {
0260     struct thermal_zone_device **ptr;
0261     int ret;
0262 
0263     ptr = devres_alloc(devm_thermal_hwmon_release, sizeof(*ptr),
0264                GFP_KERNEL);
0265     if (!ptr)
0266         return -ENOMEM;
0267 
0268     ret = thermal_add_hwmon_sysfs(tz);
0269     if (ret) {
0270         devres_free(ptr);
0271         return ret;
0272     }
0273 
0274     *ptr = tz;
0275     devres_add(&tz->device, ptr);
0276 
0277     return ret;
0278 }
0279 EXPORT_SYMBOL_GPL(devm_thermal_add_hwmon_sysfs);
0280 
0281 MODULE_IMPORT_NS(HWMON_THERMAL);