Lina Iyer | 7e3c038 | 2018-05-07 11:52:29 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Eduardo Valentin | 0dd8879 | 2013-07-03 15:14:28 -0400 | [diff] [blame] | 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> |
Eduardo Valentin | 0dd8879 | 2013-07-03 15:14:28 -0400 | [diff] [blame] | 12 | */ |
Eduardo Valentin | 0dd8879 | 2013-07-03 15:14:28 -0400 | [diff] [blame] | 13 | #include <linux/err.h> |
Amit Kucheria | e5ebf35 | 2020-05-11 17:54:54 +0530 | [diff] [blame] | 14 | #include <linux/export.h> |
Amit Kucheria | 1330e04 | 2020-05-11 17:54:53 +0530 | [diff] [blame] | 15 | #include <linux/hwmon.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/thermal.h> |
| 18 | |
Eduardo Valentin | 0dd8879 | 2013-07-03 15:14:28 -0400 | [diff] [blame] | 19 | #include "thermal_hwmon.h" |
| 20 | |
| 21 | /* hwmon sys I/F */ |
| 22 | /* thermal zone devices with the same type share one hwmon device */ |
| 23 | struct thermal_hwmon_device { |
| 24 | char type[THERMAL_NAME_LENGTH]; |
| 25 | struct device *device; |
| 26 | int count; |
| 27 | struct list_head tz_list; |
| 28 | struct list_head node; |
| 29 | }; |
| 30 | |
| 31 | struct thermal_hwmon_attr { |
| 32 | struct device_attribute attr; |
| 33 | char name[16]; |
| 34 | }; |
| 35 | |
| 36 | /* one temperature input for each thermal zone */ |
| 37 | struct thermal_hwmon_temp { |
| 38 | struct list_head hwmon_node; |
| 39 | struct thermal_zone_device *tz; |
| 40 | struct thermal_hwmon_attr temp_input; /* hwmon sys attr */ |
| 41 | struct thermal_hwmon_attr temp_crit; /* hwmon sys attr */ |
| 42 | }; |
| 43 | |
| 44 | static LIST_HEAD(thermal_hwmon_list); |
| 45 | |
| 46 | static DEFINE_MUTEX(thermal_hwmon_list_lock); |
| 47 | |
| 48 | static ssize_t |
Eduardo Valentin | 0dd8879 | 2013-07-03 15:14:28 -0400 | [diff] [blame] | 49 | temp_input_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 50 | { |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 51 | int temperature; |
Eduardo Valentin | 0dd8879 | 2013-07-03 15:14:28 -0400 | [diff] [blame] | 52 | int ret; |
| 53 | struct thermal_hwmon_attr *hwmon_attr |
| 54 | = container_of(attr, struct thermal_hwmon_attr, attr); |
| 55 | struct thermal_hwmon_temp *temp |
| 56 | = container_of(hwmon_attr, struct thermal_hwmon_temp, |
| 57 | temp_input); |
| 58 | struct thermal_zone_device *tz = temp->tz; |
| 59 | |
| 60 | ret = thermal_zone_get_temp(tz, &temperature); |
| 61 | |
| 62 | if (ret) |
| 63 | return ret; |
| 64 | |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 65 | return sprintf(buf, "%d\n", temperature); |
Eduardo Valentin | 0dd8879 | 2013-07-03 15:14:28 -0400 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | static ssize_t |
| 69 | temp_crit_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 70 | { |
| 71 | struct thermal_hwmon_attr *hwmon_attr |
| 72 | = container_of(attr, struct thermal_hwmon_attr, attr); |
| 73 | struct thermal_hwmon_temp *temp |
| 74 | = container_of(hwmon_attr, struct thermal_hwmon_temp, |
| 75 | temp_crit); |
| 76 | struct thermal_zone_device *tz = temp->tz; |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 77 | int temperature; |
Eduardo Valentin | 0dd8879 | 2013-07-03 15:14:28 -0400 | [diff] [blame] | 78 | int ret; |
| 79 | |
Krzysztof Kozlowski | f37fabb | 2016-11-22 19:22:44 +0200 | [diff] [blame] | 80 | ret = tz->ops->get_crit_temp(tz, &temperature); |
Eduardo Valentin | 0dd8879 | 2013-07-03 15:14:28 -0400 | [diff] [blame] | 81 | if (ret) |
| 82 | return ret; |
| 83 | |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 84 | return sprintf(buf, "%d\n", temperature); |
Eduardo Valentin | 0dd8879 | 2013-07-03 15:14:28 -0400 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | |
| 88 | static struct thermal_hwmon_device * |
| 89 | thermal_hwmon_lookup_by_type(const struct thermal_zone_device *tz) |
| 90 | { |
| 91 | struct thermal_hwmon_device *hwmon; |
Stefan Mavrodiev | 8c7aa184 | 2019-07-26 16:32:36 +0300 | [diff] [blame] | 92 | char type[THERMAL_NAME_LENGTH]; |
Eduardo Valentin | 0dd8879 | 2013-07-03 15:14:28 -0400 | [diff] [blame] | 93 | |
| 94 | mutex_lock(&thermal_hwmon_list_lock); |
Stefan Mavrodiev | 8c7aa184 | 2019-07-26 16:32:36 +0300 | [diff] [blame] | 95 | list_for_each_entry(hwmon, &thermal_hwmon_list, node) { |
| 96 | strcpy(type, tz->type); |
| 97 | strreplace(type, '-', '_'); |
| 98 | if (!strcmp(hwmon->type, type)) { |
Eduardo Valentin | 0dd8879 | 2013-07-03 15:14:28 -0400 | [diff] [blame] | 99 | mutex_unlock(&thermal_hwmon_list_lock); |
| 100 | return hwmon; |
| 101 | } |
Stefan Mavrodiev | 8c7aa184 | 2019-07-26 16:32:36 +0300 | [diff] [blame] | 102 | } |
Eduardo Valentin | 0dd8879 | 2013-07-03 15:14:28 -0400 | [diff] [blame] | 103 | mutex_unlock(&thermal_hwmon_list_lock); |
| 104 | |
| 105 | return NULL; |
| 106 | } |
| 107 | |
| 108 | /* Find the temperature input matching a given thermal zone */ |
| 109 | static struct thermal_hwmon_temp * |
| 110 | thermal_hwmon_lookup_temp(const struct thermal_hwmon_device *hwmon, |
| 111 | const struct thermal_zone_device *tz) |
| 112 | { |
| 113 | struct thermal_hwmon_temp *temp; |
| 114 | |
| 115 | mutex_lock(&thermal_hwmon_list_lock); |
| 116 | list_for_each_entry(temp, &hwmon->tz_list, hwmon_node) |
| 117 | if (temp->tz == tz) { |
| 118 | mutex_unlock(&thermal_hwmon_list_lock); |
| 119 | return temp; |
| 120 | } |
| 121 | mutex_unlock(&thermal_hwmon_list_lock); |
| 122 | |
| 123 | return NULL; |
| 124 | } |
| 125 | |
Aaron Lu | e8db5d6 | 2014-05-21 16:33:27 +0800 | [diff] [blame] | 126 | static bool thermal_zone_crit_temp_valid(struct thermal_zone_device *tz) |
| 127 | { |
Sascha Hauer | 17e8351 | 2015-07-24 08:12:54 +0200 | [diff] [blame] | 128 | int temp; |
Aaron Lu | e8db5d6 | 2014-05-21 16:33:27 +0800 | [diff] [blame] | 129 | return tz->ops->get_crit_temp && !tz->ops->get_crit_temp(tz, &temp); |
| 130 | } |
| 131 | |
Eduardo Valentin | 0dd8879 | 2013-07-03 15:14:28 -0400 | [diff] [blame] | 132 | int thermal_add_hwmon_sysfs(struct thermal_zone_device *tz) |
| 133 | { |
| 134 | struct thermal_hwmon_device *hwmon; |
| 135 | struct thermal_hwmon_temp *temp; |
| 136 | int new_hwmon_device = 1; |
| 137 | int result; |
| 138 | |
| 139 | hwmon = thermal_hwmon_lookup_by_type(tz); |
| 140 | if (hwmon) { |
| 141 | new_hwmon_device = 0; |
| 142 | goto register_sys_interface; |
| 143 | } |
| 144 | |
| 145 | hwmon = kzalloc(sizeof(*hwmon), GFP_KERNEL); |
| 146 | if (!hwmon) |
| 147 | return -ENOMEM; |
| 148 | |
| 149 | INIT_LIST_HEAD(&hwmon->tz_list); |
| 150 | strlcpy(hwmon->type, tz->type, THERMAL_NAME_LENGTH); |
Marc Zyngier | 409ef0b | 2018-07-10 16:40:34 +0100 | [diff] [blame] | 151 | strreplace(hwmon->type, '-', '_'); |
Marc Zyngier | f6b6b52 | 2018-07-10 16:40:35 +0100 | [diff] [blame] | 152 | hwmon->device = hwmon_device_register_with_info(&tz->device, hwmon->type, |
Fabio Estevam | e782bc1 | 2018-01-13 12:08:49 -0200 | [diff] [blame] | 153 | hwmon, NULL, NULL); |
Eduardo Valentin | 0dd8879 | 2013-07-03 15:14:28 -0400 | [diff] [blame] | 154 | if (IS_ERR(hwmon->device)) { |
| 155 | result = PTR_ERR(hwmon->device); |
| 156 | goto free_mem; |
| 157 | } |
Eduardo Valentin | 0dd8879 | 2013-07-03 15:14:28 -0400 | [diff] [blame] | 158 | |
| 159 | register_sys_interface: |
| 160 | temp = kzalloc(sizeof(*temp), GFP_KERNEL); |
| 161 | if (!temp) { |
| 162 | result = -ENOMEM; |
| 163 | goto unregister_name; |
| 164 | } |
| 165 | |
| 166 | temp->tz = tz; |
| 167 | hwmon->count++; |
| 168 | |
| 169 | snprintf(temp->temp_input.name, sizeof(temp->temp_input.name), |
| 170 | "temp%d_input", hwmon->count); |
| 171 | temp->temp_input.attr.attr.name = temp->temp_input.name; |
| 172 | temp->temp_input.attr.attr.mode = 0444; |
| 173 | temp->temp_input.attr.show = temp_input_show; |
| 174 | sysfs_attr_init(&temp->temp_input.attr.attr); |
| 175 | result = device_create_file(hwmon->device, &temp->temp_input.attr); |
| 176 | if (result) |
| 177 | goto free_temp_mem; |
| 178 | |
Aaron Lu | e8db5d6 | 2014-05-21 16:33:27 +0800 | [diff] [blame] | 179 | if (thermal_zone_crit_temp_valid(tz)) { |
| 180 | snprintf(temp->temp_crit.name, |
| 181 | sizeof(temp->temp_crit.name), |
Eduardo Valentin | 0dd8879 | 2013-07-03 15:14:28 -0400 | [diff] [blame] | 182 | "temp%d_crit", hwmon->count); |
Aaron Lu | e8db5d6 | 2014-05-21 16:33:27 +0800 | [diff] [blame] | 183 | temp->temp_crit.attr.attr.name = temp->temp_crit.name; |
| 184 | temp->temp_crit.attr.attr.mode = 0444; |
| 185 | temp->temp_crit.attr.show = temp_crit_show; |
| 186 | sysfs_attr_init(&temp->temp_crit.attr.attr); |
| 187 | result = device_create_file(hwmon->device, |
| 188 | &temp->temp_crit.attr); |
| 189 | if (result) |
| 190 | goto unregister_input; |
Eduardo Valentin | 0dd8879 | 2013-07-03 15:14:28 -0400 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | mutex_lock(&thermal_hwmon_list_lock); |
| 194 | if (new_hwmon_device) |
| 195 | list_add_tail(&hwmon->node, &thermal_hwmon_list); |
| 196 | list_add_tail(&temp->hwmon_node, &hwmon->tz_list); |
| 197 | mutex_unlock(&thermal_hwmon_list_lock); |
| 198 | |
| 199 | return 0; |
| 200 | |
| 201 | unregister_input: |
| 202 | device_remove_file(hwmon->device, &temp->temp_input.attr); |
| 203 | free_temp_mem: |
| 204 | kfree(temp); |
| 205 | unregister_name: |
Fabio Estevam | e782bc1 | 2018-01-13 12:08:49 -0200 | [diff] [blame] | 206 | if (new_hwmon_device) |
Eduardo Valentin | 0dd8879 | 2013-07-03 15:14:28 -0400 | [diff] [blame] | 207 | hwmon_device_unregister(hwmon->device); |
Eduardo Valentin | 0dd8879 | 2013-07-03 15:14:28 -0400 | [diff] [blame] | 208 | free_mem: |
Bernard Zhao | 030a48b | 2020-11-01 18:31:21 -0800 | [diff] [blame] | 209 | kfree(hwmon); |
Eduardo Valentin | 0dd8879 | 2013-07-03 15:14:28 -0400 | [diff] [blame] | 210 | |
| 211 | return result; |
| 212 | } |
Kuninori Morimoto | f4c5924 | 2016-07-04 07:19:32 +0000 | [diff] [blame] | 213 | EXPORT_SYMBOL_GPL(thermal_add_hwmon_sysfs); |
Eduardo Valentin | 0dd8879 | 2013-07-03 15:14:28 -0400 | [diff] [blame] | 214 | |
| 215 | void thermal_remove_hwmon_sysfs(struct thermal_zone_device *tz) |
| 216 | { |
| 217 | struct thermal_hwmon_device *hwmon; |
| 218 | struct thermal_hwmon_temp *temp; |
| 219 | |
| 220 | hwmon = thermal_hwmon_lookup_by_type(tz); |
| 221 | if (unlikely(!hwmon)) { |
| 222 | /* Should never happen... */ |
| 223 | dev_dbg(&tz->device, "hwmon device lookup failed!\n"); |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | temp = thermal_hwmon_lookup_temp(hwmon, tz); |
| 228 | if (unlikely(!temp)) { |
| 229 | /* Should never happen... */ |
| 230 | dev_dbg(&tz->device, "temperature input lookup failed!\n"); |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | device_remove_file(hwmon->device, &temp->temp_input.attr); |
Aaron Lu | e8db5d6 | 2014-05-21 16:33:27 +0800 | [diff] [blame] | 235 | if (thermal_zone_crit_temp_valid(tz)) |
Eduardo Valentin | 0dd8879 | 2013-07-03 15:14:28 -0400 | [diff] [blame] | 236 | device_remove_file(hwmon->device, &temp->temp_crit.attr); |
| 237 | |
| 238 | mutex_lock(&thermal_hwmon_list_lock); |
| 239 | list_del(&temp->hwmon_node); |
| 240 | kfree(temp); |
| 241 | if (!list_empty(&hwmon->tz_list)) { |
| 242 | mutex_unlock(&thermal_hwmon_list_lock); |
| 243 | return; |
| 244 | } |
| 245 | list_del(&hwmon->node); |
| 246 | mutex_unlock(&thermal_hwmon_list_lock); |
| 247 | |
Eduardo Valentin | 0dd8879 | 2013-07-03 15:14:28 -0400 | [diff] [blame] | 248 | hwmon_device_unregister(hwmon->device); |
| 249 | kfree(hwmon); |
| 250 | } |
Kuninori Morimoto | f4c5924 | 2016-07-04 07:19:32 +0000 | [diff] [blame] | 251 | EXPORT_SYMBOL_GPL(thermal_remove_hwmon_sysfs); |
Andrey Smirnov | c7fc403 | 2019-12-10 08:41:52 -0800 | [diff] [blame] | 252 | |
| 253 | static void devm_thermal_hwmon_release(struct device *dev, void *res) |
| 254 | { |
| 255 | thermal_remove_hwmon_sysfs(*(struct thermal_zone_device **)res); |
| 256 | } |
| 257 | |
| 258 | int devm_thermal_add_hwmon_sysfs(struct thermal_zone_device *tz) |
| 259 | { |
| 260 | struct thermal_zone_device **ptr; |
| 261 | int ret; |
| 262 | |
| 263 | ptr = devres_alloc(devm_thermal_hwmon_release, sizeof(*ptr), |
| 264 | GFP_KERNEL); |
| 265 | if (!ptr) |
| 266 | return -ENOMEM; |
| 267 | |
| 268 | ret = thermal_add_hwmon_sysfs(tz); |
| 269 | if (ret) { |
| 270 | devres_free(ptr); |
| 271 | return ret; |
| 272 | } |
| 273 | |
| 274 | *ptr = tz; |
| 275 | devres_add(&tz->device, ptr); |
| 276 | |
| 277 | return ret; |
| 278 | } |
| 279 | EXPORT_SYMBOL_GPL(devm_thermal_add_hwmon_sysfs); |