Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 2 | /* |
Guenter Roeck | 5ed0488 | 2012-01-19 11:02:18 -0800 | [diff] [blame] | 3 | * hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring |
| 4 | * |
| 5 | * This file defines the sysfs class "hwmon", for use by sensors drivers. |
| 6 | * |
| 7 | * Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com> |
Guenter Roeck | 5ed0488 | 2012-01-19 11:02:18 -0800 | [diff] [blame] | 8 | */ |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 9 | |
Joe Perches | c95df1a | 2010-10-20 06:51:37 +0000 | [diff] [blame] | 10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 11 | |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 12 | #include <linux/bitops.h> |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 13 | #include <linux/device.h> |
| 14 | #include <linux/err.h> |
Tim Schmielau | 8c65b4a | 2005-11-07 00:59:43 -0800 | [diff] [blame] | 15 | #include <linux/gfp.h> |
Guenter Roeck | c9ebbe6 | 2016-07-10 09:43:21 -0700 | [diff] [blame] | 16 | #include <linux/hwmon.h> |
| 17 | #include <linux/idr.h> |
| 18 | #include <linux/module.h> |
Jean Delvare | 2958b1e | 2009-06-15 18:39:50 +0200 | [diff] [blame] | 19 | #include <linux/pci.h> |
Guenter Roeck | c9ebbe6 | 2016-07-10 09:43:21 -0700 | [diff] [blame] | 20 | #include <linux/slab.h> |
Guenter Roeck | 648cd48 | 2014-02-28 10:37:55 -0800 | [diff] [blame] | 21 | #include <linux/string.h> |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 22 | #include <linux/thermal.h> |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 23 | |
Nicolin Chen | 61b8ab2 | 2018-10-09 14:42:19 -0700 | [diff] [blame] | 24 | #define CREATE_TRACE_POINTS |
| 25 | #include <trace/events/hwmon.h> |
| 26 | |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 27 | #define HWMON_ID_PREFIX "hwmon" |
| 28 | #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d" |
| 29 | |
Guenter Roeck | bab2243 | 2013-07-06 13:57:23 -0700 | [diff] [blame] | 30 | struct hwmon_device { |
| 31 | const char *name; |
| 32 | struct device dev; |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 33 | const struct hwmon_chip_info *chip; |
| 34 | |
| 35 | struct attribute_group group; |
| 36 | const struct attribute_group **groups; |
Guenter Roeck | bab2243 | 2013-07-06 13:57:23 -0700 | [diff] [blame] | 37 | }; |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 38 | |
Guenter Roeck | bab2243 | 2013-07-06 13:57:23 -0700 | [diff] [blame] | 39 | #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev) |
| 40 | |
Guenter Roeck | 3a412d5 | 2016-10-16 10:52:04 -0700 | [diff] [blame] | 41 | #define MAX_SYSFS_ATTR_NAME_LENGTH 32 |
| 42 | |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 43 | struct hwmon_device_attribute { |
| 44 | struct device_attribute dev_attr; |
| 45 | const struct hwmon_ops *ops; |
| 46 | enum hwmon_sensor_types type; |
| 47 | u32 attr; |
| 48 | int index; |
Guenter Roeck | 3a412d5 | 2016-10-16 10:52:04 -0700 | [diff] [blame] | 49 | char name[MAX_SYSFS_ATTR_NAME_LENGTH]; |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | #define to_hwmon_attr(d) \ |
| 53 | container_of(d, struct hwmon_device_attribute, dev_attr) |
Guenter Roeck | 3bf8bdc | 2020-01-16 10:44:17 -0800 | [diff] [blame] | 54 | #define to_dev_attr(a) container_of(a, struct device_attribute, attr) |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 55 | |
| 56 | /* |
| 57 | * Thermal zone information |
| 58 | * In addition to the reference to the hwmon device, |
| 59 | * also provides the sensor index. |
| 60 | */ |
| 61 | struct hwmon_thermal_data { |
Guenter Roeck | 3bf8bdc | 2020-01-16 10:44:17 -0800 | [diff] [blame] | 62 | struct device *dev; /* Reference to hwmon device */ |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 63 | int index; /* sensor index */ |
| 64 | }; |
| 65 | |
Guenter Roeck | bab2243 | 2013-07-06 13:57:23 -0700 | [diff] [blame] | 66 | static ssize_t |
Julia Lawall | 2ab0c6c | 2016-12-22 13:04:45 +0100 | [diff] [blame] | 67 | name_show(struct device *dev, struct device_attribute *attr, char *buf) |
Guenter Roeck | bab2243 | 2013-07-06 13:57:23 -0700 | [diff] [blame] | 68 | { |
| 69 | return sprintf(buf, "%s\n", to_hwmon_device(dev)->name); |
| 70 | } |
Julia Lawall | 2ab0c6c | 2016-12-22 13:04:45 +0100 | [diff] [blame] | 71 | static DEVICE_ATTR_RO(name); |
Guenter Roeck | bab2243 | 2013-07-06 13:57:23 -0700 | [diff] [blame] | 72 | |
| 73 | static struct attribute *hwmon_dev_attrs[] = { |
| 74 | &dev_attr_name.attr, |
| 75 | NULL |
| 76 | }; |
| 77 | |
| 78 | static umode_t hwmon_dev_name_is_visible(struct kobject *kobj, |
| 79 | struct attribute *attr, int n) |
| 80 | { |
| 81 | struct device *dev = container_of(kobj, struct device, kobj); |
| 82 | |
| 83 | if (to_hwmon_device(dev)->name == NULL) |
| 84 | return 0; |
| 85 | |
| 86 | return attr->mode; |
| 87 | } |
| 88 | |
Arvind Yadav | 524703ac | 2017-07-05 10:31:05 +0530 | [diff] [blame] | 89 | static const struct attribute_group hwmon_dev_attr_group = { |
Guenter Roeck | bab2243 | 2013-07-06 13:57:23 -0700 | [diff] [blame] | 90 | .attrs = hwmon_dev_attrs, |
| 91 | .is_visible = hwmon_dev_name_is_visible, |
| 92 | }; |
| 93 | |
| 94 | static const struct attribute_group *hwmon_dev_attr_groups[] = { |
| 95 | &hwmon_dev_attr_group, |
| 96 | NULL |
| 97 | }; |
| 98 | |
Guenter Roeck | 3bf8bdc | 2020-01-16 10:44:17 -0800 | [diff] [blame] | 99 | static void hwmon_free_attrs(struct attribute **attrs) |
| 100 | { |
| 101 | int i; |
| 102 | |
| 103 | for (i = 0; attrs[i]; i++) { |
| 104 | struct device_attribute *dattr = to_dev_attr(attrs[i]); |
| 105 | struct hwmon_device_attribute *hattr = to_hwmon_attr(dattr); |
| 106 | |
| 107 | kfree(hattr); |
| 108 | } |
| 109 | kfree(attrs); |
| 110 | } |
| 111 | |
Guenter Roeck | bab2243 | 2013-07-06 13:57:23 -0700 | [diff] [blame] | 112 | static void hwmon_dev_release(struct device *dev) |
| 113 | { |
Guenter Roeck | 3bf8bdc | 2020-01-16 10:44:17 -0800 | [diff] [blame] | 114 | struct hwmon_device *hwdev = to_hwmon_device(dev); |
| 115 | |
| 116 | if (hwdev->group.attrs) |
| 117 | hwmon_free_attrs(hwdev->group.attrs); |
| 118 | kfree(hwdev->groups); |
| 119 | kfree(hwdev); |
Guenter Roeck | bab2243 | 2013-07-06 13:57:23 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static struct class hwmon_class = { |
| 123 | .name = "hwmon", |
| 124 | .owner = THIS_MODULE, |
| 125 | .dev_groups = hwmon_dev_attr_groups, |
| 126 | .dev_release = hwmon_dev_release, |
| 127 | }; |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 128 | |
Jonathan Cameron | 4ca5f46 | 2011-10-31 17:10:09 -0700 | [diff] [blame] | 129 | static DEFINE_IDA(hwmon_ida); |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 130 | |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 131 | /* Thermal zone handling */ |
| 132 | |
Guenter Roeck | 86430c1 | 2016-08-12 06:28:15 -0700 | [diff] [blame] | 133 | /* |
| 134 | * The complex conditional is necessary to avoid a cyclic dependency |
| 135 | * between hwmon and thermal_sys modules. |
| 136 | */ |
Daniel Lezcano | f373533 | 2019-04-02 18:12:50 +0200 | [diff] [blame] | 137 | #ifdef CONFIG_THERMAL_OF |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 138 | static int hwmon_thermal_get_temp(void *data, int *temp) |
| 139 | { |
| 140 | struct hwmon_thermal_data *tdata = data; |
Guenter Roeck | 3bf8bdc | 2020-01-16 10:44:17 -0800 | [diff] [blame] | 141 | struct hwmon_device *hwdev = to_hwmon_device(tdata->dev); |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 142 | int ret; |
| 143 | long t; |
| 144 | |
Guenter Roeck | 3bf8bdc | 2020-01-16 10:44:17 -0800 | [diff] [blame] | 145 | ret = hwdev->chip->ops->read(tdata->dev, hwmon_temp, hwmon_temp_input, |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 146 | tdata->index, &t); |
| 147 | if (ret < 0) |
| 148 | return ret; |
| 149 | |
| 150 | *temp = t; |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
Julia Lawall | c992065 | 2017-08-08 17:09:02 +0200 | [diff] [blame] | 155 | static const struct thermal_zone_of_device_ops hwmon_thermal_ops = { |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 156 | .get_temp = hwmon_thermal_get_temp, |
| 157 | }; |
| 158 | |
Guenter Roeck | 3bf8bdc | 2020-01-16 10:44:17 -0800 | [diff] [blame] | 159 | static int hwmon_thermal_add_sensor(struct device *dev, int index) |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 160 | { |
| 161 | struct hwmon_thermal_data *tdata; |
Linus Walleij | 47c332d | 2017-12-05 09:36:14 +0100 | [diff] [blame] | 162 | struct thermal_zone_device *tzd; |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 163 | |
| 164 | tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL); |
| 165 | if (!tdata) |
| 166 | return -ENOMEM; |
| 167 | |
Guenter Roeck | 3bf8bdc | 2020-01-16 10:44:17 -0800 | [diff] [blame] | 168 | tdata->dev = dev; |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 169 | tdata->index = index; |
| 170 | |
Guenter Roeck | 3bf8bdc | 2020-01-16 10:44:17 -0800 | [diff] [blame] | 171 | tzd = devm_thermal_zone_of_sensor_register(dev, index, tdata, |
Linus Walleij | 47c332d | 2017-12-05 09:36:14 +0100 | [diff] [blame] | 172 | &hwmon_thermal_ops); |
| 173 | /* |
| 174 | * If CONFIG_THERMAL_OF is disabled, this returns -ENODEV, |
| 175 | * so ignore that error but forward any other error. |
| 176 | */ |
| 177 | if (IS_ERR(tzd) && (PTR_ERR(tzd) != -ENODEV)) |
| 178 | return PTR_ERR(tzd); |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 179 | |
| 180 | return 0; |
| 181 | } |
Akinobu Mita | 44e3ad8 | 2020-05-04 23:57:44 +0900 | [diff] [blame^] | 182 | |
| 183 | static int hwmon_thermal_register_sensors(struct device *dev) |
| 184 | { |
| 185 | struct hwmon_device *hwdev = to_hwmon_device(dev); |
| 186 | const struct hwmon_chip_info *chip = hwdev->chip; |
| 187 | const struct hwmon_channel_info **info = chip->info; |
| 188 | void *drvdata = dev_get_drvdata(dev); |
| 189 | int i; |
| 190 | |
| 191 | for (i = 1; info[i]; i++) { |
| 192 | int j; |
| 193 | |
| 194 | if (info[i]->type != hwmon_temp) |
| 195 | continue; |
| 196 | |
| 197 | for (j = 0; info[i]->config[j]; j++) { |
| 198 | int err; |
| 199 | |
| 200 | if (!(info[i]->config[j] & HWMON_T_INPUT) || |
| 201 | !chip->ops->is_visible(drvdata, hwmon_temp, |
| 202 | hwmon_temp_input, j)) |
| 203 | continue; |
| 204 | |
| 205 | err = hwmon_thermal_add_sensor(dev, j); |
| 206 | if (err) |
| 207 | return err; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 214 | #else |
Akinobu Mita | 44e3ad8 | 2020-05-04 23:57:44 +0900 | [diff] [blame^] | 215 | static int hwmon_thermal_register_sensors(struct device *dev) |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 216 | { |
| 217 | return 0; |
| 218 | } |
Guenter Roeck | 86430c1 | 2016-08-12 06:28:15 -0700 | [diff] [blame] | 219 | #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */ |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 220 | |
Nicolin Chen | 61b8ab2 | 2018-10-09 14:42:19 -0700 | [diff] [blame] | 221 | static int hwmon_attr_base(enum hwmon_sensor_types type) |
| 222 | { |
Dr. David Alan Gilbert | 4413405 | 2019-11-24 20:20:29 +0000 | [diff] [blame] | 223 | if (type == hwmon_in || type == hwmon_intrusion) |
Nicolin Chen | 61b8ab2 | 2018-10-09 14:42:19 -0700 | [diff] [blame] | 224 | return 0; |
| 225 | return 1; |
| 226 | } |
| 227 | |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 228 | /* sysfs attribute management */ |
| 229 | |
| 230 | static ssize_t hwmon_attr_show(struct device *dev, |
| 231 | struct device_attribute *devattr, char *buf) |
| 232 | { |
| 233 | struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); |
| 234 | long val; |
| 235 | int ret; |
| 236 | |
| 237 | ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index, |
| 238 | &val); |
| 239 | if (ret < 0) |
| 240 | return ret; |
| 241 | |
Nicolin Chen | 61b8ab2 | 2018-10-09 14:42:19 -0700 | [diff] [blame] | 242 | trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type), |
| 243 | hattr->name, val); |
| 244 | |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 245 | return sprintf(buf, "%ld\n", val); |
| 246 | } |
| 247 | |
Guenter Roeck | e159ab5 | 2016-08-07 20:51:25 -0700 | [diff] [blame] | 248 | static ssize_t hwmon_attr_show_string(struct device *dev, |
| 249 | struct device_attribute *devattr, |
| 250 | char *buf) |
| 251 | { |
| 252 | struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); |
Nicolin Chen | 61b8ab2 | 2018-10-09 14:42:19 -0700 | [diff] [blame] | 253 | enum hwmon_sensor_types type = hattr->type; |
Jean Delvare | 5ba6bcb | 2017-03-07 06:15:15 -0800 | [diff] [blame] | 254 | const char *s; |
Guenter Roeck | e159ab5 | 2016-08-07 20:51:25 -0700 | [diff] [blame] | 255 | int ret; |
| 256 | |
| 257 | ret = hattr->ops->read_string(dev, hattr->type, hattr->attr, |
| 258 | hattr->index, &s); |
| 259 | if (ret < 0) |
| 260 | return ret; |
| 261 | |
Nicolin Chen | 61b8ab2 | 2018-10-09 14:42:19 -0700 | [diff] [blame] | 262 | trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type), |
| 263 | hattr->name, s); |
| 264 | |
Guenter Roeck | e159ab5 | 2016-08-07 20:51:25 -0700 | [diff] [blame] | 265 | return sprintf(buf, "%s\n", s); |
| 266 | } |
| 267 | |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 268 | static ssize_t hwmon_attr_store(struct device *dev, |
| 269 | struct device_attribute *devattr, |
| 270 | const char *buf, size_t count) |
| 271 | { |
| 272 | struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); |
| 273 | long val; |
| 274 | int ret; |
| 275 | |
| 276 | ret = kstrtol(buf, 10, &val); |
| 277 | if (ret < 0) |
| 278 | return ret; |
| 279 | |
| 280 | ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index, |
| 281 | val); |
| 282 | if (ret < 0) |
| 283 | return ret; |
| 284 | |
Nicolin Chen | 61b8ab2 | 2018-10-09 14:42:19 -0700 | [diff] [blame] | 285 | trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type), |
| 286 | hattr->name, val); |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 287 | |
Nicolin Chen | 61b8ab2 | 2018-10-09 14:42:19 -0700 | [diff] [blame] | 288 | return count; |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Guenter Roeck | e159ab5 | 2016-08-07 20:51:25 -0700 | [diff] [blame] | 291 | static bool is_string_attr(enum hwmon_sensor_types type, u32 attr) |
| 292 | { |
| 293 | return (type == hwmon_temp && attr == hwmon_temp_label) || |
| 294 | (type == hwmon_in && attr == hwmon_in_label) || |
| 295 | (type == hwmon_curr && attr == hwmon_curr_label) || |
| 296 | (type == hwmon_power && attr == hwmon_power_label) || |
| 297 | (type == hwmon_energy && attr == hwmon_energy_label) || |
| 298 | (type == hwmon_humidity && attr == hwmon_humidity_label) || |
| 299 | (type == hwmon_fan && attr == hwmon_fan_label); |
| 300 | } |
| 301 | |
Guenter Roeck | 3bf8bdc | 2020-01-16 10:44:17 -0800 | [diff] [blame] | 302 | static struct attribute *hwmon_genattr(const void *drvdata, |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 303 | enum hwmon_sensor_types type, |
| 304 | u32 attr, |
| 305 | int index, |
| 306 | const char *template, |
| 307 | const struct hwmon_ops *ops) |
| 308 | { |
| 309 | struct hwmon_device_attribute *hattr; |
| 310 | struct device_attribute *dattr; |
| 311 | struct attribute *a; |
| 312 | umode_t mode; |
Rasmus Villemoes | 3b443de | 2018-10-27 00:30:59 +0200 | [diff] [blame] | 313 | const char *name; |
Guenter Roeck | e159ab5 | 2016-08-07 20:51:25 -0700 | [diff] [blame] | 314 | bool is_string = is_string_attr(type, attr); |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 315 | |
| 316 | /* The attribute is invisible if there is no template string */ |
| 317 | if (!template) |
| 318 | return ERR_PTR(-ENOENT); |
| 319 | |
| 320 | mode = ops->is_visible(drvdata, type, attr, index); |
| 321 | if (!mode) |
| 322 | return ERR_PTR(-ENOENT); |
| 323 | |
Guenter Roeck | 0d87116 | 2018-12-10 14:02:08 -0800 | [diff] [blame] | 324 | if ((mode & 0444) && ((is_string && !ops->read_string) || |
Guenter Roeck | e159ab5 | 2016-08-07 20:51:25 -0700 | [diff] [blame] | 325 | (!is_string && !ops->read))) |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 326 | return ERR_PTR(-EINVAL); |
Guenter Roeck | 0d87116 | 2018-12-10 14:02:08 -0800 | [diff] [blame] | 327 | if ((mode & 0222) && !ops->write) |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 328 | return ERR_PTR(-EINVAL); |
| 329 | |
Guenter Roeck | 3bf8bdc | 2020-01-16 10:44:17 -0800 | [diff] [blame] | 330 | hattr = kzalloc(sizeof(*hattr), GFP_KERNEL); |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 331 | if (!hattr) |
| 332 | return ERR_PTR(-ENOMEM); |
| 333 | |
Guenter Roeck | 3a412d5 | 2016-10-16 10:52:04 -0700 | [diff] [blame] | 334 | if (type == hwmon_chip) { |
Rasmus Villemoes | 3b443de | 2018-10-27 00:30:59 +0200 | [diff] [blame] | 335 | name = template; |
Guenter Roeck | 3a412d5 | 2016-10-16 10:52:04 -0700 | [diff] [blame] | 336 | } else { |
| 337 | scnprintf(hattr->name, sizeof(hattr->name), template, |
| 338 | index + hwmon_attr_base(type)); |
| 339 | name = hattr->name; |
| 340 | } |
| 341 | |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 342 | hattr->type = type; |
| 343 | hattr->attr = attr; |
| 344 | hattr->index = index; |
| 345 | hattr->ops = ops; |
| 346 | |
| 347 | dattr = &hattr->dev_attr; |
Guenter Roeck | e159ab5 | 2016-08-07 20:51:25 -0700 | [diff] [blame] | 348 | dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show; |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 349 | dattr->store = hwmon_attr_store; |
| 350 | |
| 351 | a = &dattr->attr; |
| 352 | sysfs_attr_init(a); |
| 353 | a->name = name; |
| 354 | a->mode = mode; |
| 355 | |
| 356 | return a; |
| 357 | } |
| 358 | |
Guenter Roeck | f4d325d | 2016-10-16 10:38:52 -0700 | [diff] [blame] | 359 | /* |
| 360 | * Chip attributes are not attribute templates but actual sysfs attributes. |
| 361 | * See hwmon_genattr() for special handling. |
| 362 | */ |
| 363 | static const char * const hwmon_chip_attrs[] = { |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 364 | [hwmon_chip_temp_reset_history] = "temp_reset_history", |
Guenter Roeck | 00d616c | 2016-06-20 11:01:57 -0700 | [diff] [blame] | 365 | [hwmon_chip_in_reset_history] = "in_reset_history", |
Guenter Roeck | 9b26947 | 2016-06-20 11:10:33 -0700 | [diff] [blame] | 366 | [hwmon_chip_curr_reset_history] = "curr_reset_history", |
Guenter Roeck | b308f5c | 2016-06-20 11:27:36 -0700 | [diff] [blame] | 367 | [hwmon_chip_power_reset_history] = "power_reset_history", |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 368 | [hwmon_chip_update_interval] = "update_interval", |
| 369 | [hwmon_chip_alarms] = "alarms", |
Guenter Roeck | 9f00995 | 2019-04-15 13:23:48 -0700 | [diff] [blame] | 370 | [hwmon_chip_samples] = "samples", |
| 371 | [hwmon_chip_curr_samples] = "curr_samples", |
| 372 | [hwmon_chip_in_samples] = "in_samples", |
| 373 | [hwmon_chip_power_samples] = "power_samples", |
| 374 | [hwmon_chip_temp_samples] = "temp_samples", |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 375 | }; |
| 376 | |
| 377 | static const char * const hwmon_temp_attr_templates[] = { |
Guenter Roeck | 002c6b5 | 2018-07-17 10:17:19 -0700 | [diff] [blame] | 378 | [hwmon_temp_enable] = "temp%d_enable", |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 379 | [hwmon_temp_input] = "temp%d_input", |
| 380 | [hwmon_temp_type] = "temp%d_type", |
| 381 | [hwmon_temp_lcrit] = "temp%d_lcrit", |
| 382 | [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst", |
| 383 | [hwmon_temp_min] = "temp%d_min", |
| 384 | [hwmon_temp_min_hyst] = "temp%d_min_hyst", |
| 385 | [hwmon_temp_max] = "temp%d_max", |
| 386 | [hwmon_temp_max_hyst] = "temp%d_max_hyst", |
| 387 | [hwmon_temp_crit] = "temp%d_crit", |
| 388 | [hwmon_temp_crit_hyst] = "temp%d_crit_hyst", |
| 389 | [hwmon_temp_emergency] = "temp%d_emergency", |
| 390 | [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst", |
| 391 | [hwmon_temp_alarm] = "temp%d_alarm", |
| 392 | [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm", |
| 393 | [hwmon_temp_min_alarm] = "temp%d_min_alarm", |
| 394 | [hwmon_temp_max_alarm] = "temp%d_max_alarm", |
| 395 | [hwmon_temp_crit_alarm] = "temp%d_crit_alarm", |
| 396 | [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm", |
| 397 | [hwmon_temp_fault] = "temp%d_fault", |
| 398 | [hwmon_temp_offset] = "temp%d_offset", |
| 399 | [hwmon_temp_label] = "temp%d_label", |
| 400 | [hwmon_temp_lowest] = "temp%d_lowest", |
| 401 | [hwmon_temp_highest] = "temp%d_highest", |
| 402 | [hwmon_temp_reset_history] = "temp%d_reset_history", |
| 403 | }; |
| 404 | |
Guenter Roeck | 00d616c | 2016-06-20 11:01:57 -0700 | [diff] [blame] | 405 | static const char * const hwmon_in_attr_templates[] = { |
Guenter Roeck | 002c6b5 | 2018-07-17 10:17:19 -0700 | [diff] [blame] | 406 | [hwmon_in_enable] = "in%d_enable", |
Guenter Roeck | 00d616c | 2016-06-20 11:01:57 -0700 | [diff] [blame] | 407 | [hwmon_in_input] = "in%d_input", |
| 408 | [hwmon_in_min] = "in%d_min", |
| 409 | [hwmon_in_max] = "in%d_max", |
| 410 | [hwmon_in_lcrit] = "in%d_lcrit", |
| 411 | [hwmon_in_crit] = "in%d_crit", |
| 412 | [hwmon_in_average] = "in%d_average", |
| 413 | [hwmon_in_lowest] = "in%d_lowest", |
| 414 | [hwmon_in_highest] = "in%d_highest", |
| 415 | [hwmon_in_reset_history] = "in%d_reset_history", |
| 416 | [hwmon_in_label] = "in%d_label", |
| 417 | [hwmon_in_alarm] = "in%d_alarm", |
| 418 | [hwmon_in_min_alarm] = "in%d_min_alarm", |
| 419 | [hwmon_in_max_alarm] = "in%d_max_alarm", |
| 420 | [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm", |
| 421 | [hwmon_in_crit_alarm] = "in%d_crit_alarm", |
| 422 | }; |
| 423 | |
Guenter Roeck | 9b26947 | 2016-06-20 11:10:33 -0700 | [diff] [blame] | 424 | static const char * const hwmon_curr_attr_templates[] = { |
Guenter Roeck | 002c6b5 | 2018-07-17 10:17:19 -0700 | [diff] [blame] | 425 | [hwmon_curr_enable] = "curr%d_enable", |
Guenter Roeck | 9b26947 | 2016-06-20 11:10:33 -0700 | [diff] [blame] | 426 | [hwmon_curr_input] = "curr%d_input", |
| 427 | [hwmon_curr_min] = "curr%d_min", |
| 428 | [hwmon_curr_max] = "curr%d_max", |
| 429 | [hwmon_curr_lcrit] = "curr%d_lcrit", |
| 430 | [hwmon_curr_crit] = "curr%d_crit", |
| 431 | [hwmon_curr_average] = "curr%d_average", |
| 432 | [hwmon_curr_lowest] = "curr%d_lowest", |
| 433 | [hwmon_curr_highest] = "curr%d_highest", |
| 434 | [hwmon_curr_reset_history] = "curr%d_reset_history", |
| 435 | [hwmon_curr_label] = "curr%d_label", |
| 436 | [hwmon_curr_alarm] = "curr%d_alarm", |
| 437 | [hwmon_curr_min_alarm] = "curr%d_min_alarm", |
| 438 | [hwmon_curr_max_alarm] = "curr%d_max_alarm", |
| 439 | [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm", |
| 440 | [hwmon_curr_crit_alarm] = "curr%d_crit_alarm", |
| 441 | }; |
| 442 | |
Guenter Roeck | b308f5c | 2016-06-20 11:27:36 -0700 | [diff] [blame] | 443 | static const char * const hwmon_power_attr_templates[] = { |
Guenter Roeck | 002c6b5 | 2018-07-17 10:17:19 -0700 | [diff] [blame] | 444 | [hwmon_power_enable] = "power%d_enable", |
Guenter Roeck | b308f5c | 2016-06-20 11:27:36 -0700 | [diff] [blame] | 445 | [hwmon_power_average] = "power%d_average", |
| 446 | [hwmon_power_average_interval] = "power%d_average_interval", |
| 447 | [hwmon_power_average_interval_max] = "power%d_interval_max", |
| 448 | [hwmon_power_average_interval_min] = "power%d_interval_min", |
| 449 | [hwmon_power_average_highest] = "power%d_average_highest", |
| 450 | [hwmon_power_average_lowest] = "power%d_average_lowest", |
| 451 | [hwmon_power_average_max] = "power%d_average_max", |
| 452 | [hwmon_power_average_min] = "power%d_average_min", |
| 453 | [hwmon_power_input] = "power%d_input", |
| 454 | [hwmon_power_input_highest] = "power%d_input_highest", |
| 455 | [hwmon_power_input_lowest] = "power%d_input_lowest", |
| 456 | [hwmon_power_reset_history] = "power%d_reset_history", |
| 457 | [hwmon_power_accuracy] = "power%d_accuracy", |
| 458 | [hwmon_power_cap] = "power%d_cap", |
| 459 | [hwmon_power_cap_hyst] = "power%d_cap_hyst", |
| 460 | [hwmon_power_cap_max] = "power%d_cap_max", |
| 461 | [hwmon_power_cap_min] = "power%d_cap_min", |
Andrew Lunn | aa7f29b | 2018-07-17 21:48:11 +0200 | [diff] [blame] | 462 | [hwmon_power_min] = "power%d_min", |
Guenter Roeck | b308f5c | 2016-06-20 11:27:36 -0700 | [diff] [blame] | 463 | [hwmon_power_max] = "power%d_max", |
Andrew Lunn | aa7f29b | 2018-07-17 21:48:11 +0200 | [diff] [blame] | 464 | [hwmon_power_lcrit] = "power%d_lcrit", |
Guenter Roeck | b308f5c | 2016-06-20 11:27:36 -0700 | [diff] [blame] | 465 | [hwmon_power_crit] = "power%d_crit", |
| 466 | [hwmon_power_label] = "power%d_label", |
| 467 | [hwmon_power_alarm] = "power%d_alarm", |
| 468 | [hwmon_power_cap_alarm] = "power%d_cap_alarm", |
Andrew Lunn | aa7f29b | 2018-07-17 21:48:11 +0200 | [diff] [blame] | 469 | [hwmon_power_min_alarm] = "power%d_min_alarm", |
Guenter Roeck | b308f5c | 2016-06-20 11:27:36 -0700 | [diff] [blame] | 470 | [hwmon_power_max_alarm] = "power%d_max_alarm", |
Andrew Lunn | aa7f29b | 2018-07-17 21:48:11 +0200 | [diff] [blame] | 471 | [hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm", |
Guenter Roeck | b308f5c | 2016-06-20 11:27:36 -0700 | [diff] [blame] | 472 | [hwmon_power_crit_alarm] = "power%d_crit_alarm", |
| 473 | }; |
| 474 | |
Guenter Roeck | 6bfcca4 | 2016-06-20 11:38:37 -0700 | [diff] [blame] | 475 | static const char * const hwmon_energy_attr_templates[] = { |
Guenter Roeck | 002c6b5 | 2018-07-17 10:17:19 -0700 | [diff] [blame] | 476 | [hwmon_energy_enable] = "energy%d_enable", |
Guenter Roeck | 6bfcca4 | 2016-06-20 11:38:37 -0700 | [diff] [blame] | 477 | [hwmon_energy_input] = "energy%d_input", |
| 478 | [hwmon_energy_label] = "energy%d_label", |
| 479 | }; |
| 480 | |
| 481 | static const char * const hwmon_humidity_attr_templates[] = { |
Guenter Roeck | 002c6b5 | 2018-07-17 10:17:19 -0700 | [diff] [blame] | 482 | [hwmon_humidity_enable] = "humidity%d_enable", |
Guenter Roeck | 6bfcca4 | 2016-06-20 11:38:37 -0700 | [diff] [blame] | 483 | [hwmon_humidity_input] = "humidity%d_input", |
| 484 | [hwmon_humidity_label] = "humidity%d_label", |
| 485 | [hwmon_humidity_min] = "humidity%d_min", |
| 486 | [hwmon_humidity_min_hyst] = "humidity%d_min_hyst", |
| 487 | [hwmon_humidity_max] = "humidity%d_max", |
| 488 | [hwmon_humidity_max_hyst] = "humidity%d_max_hyst", |
| 489 | [hwmon_humidity_alarm] = "humidity%d_alarm", |
| 490 | [hwmon_humidity_fault] = "humidity%d_fault", |
| 491 | }; |
| 492 | |
Guenter Roeck | 8faee73 | 2016-06-25 19:52:13 -0700 | [diff] [blame] | 493 | static const char * const hwmon_fan_attr_templates[] = { |
Guenter Roeck | 002c6b5 | 2018-07-17 10:17:19 -0700 | [diff] [blame] | 494 | [hwmon_fan_enable] = "fan%d_enable", |
Guenter Roeck | 8faee73 | 2016-06-25 19:52:13 -0700 | [diff] [blame] | 495 | [hwmon_fan_input] = "fan%d_input", |
| 496 | [hwmon_fan_label] = "fan%d_label", |
| 497 | [hwmon_fan_min] = "fan%d_min", |
| 498 | [hwmon_fan_max] = "fan%d_max", |
| 499 | [hwmon_fan_div] = "fan%d_div", |
| 500 | [hwmon_fan_pulses] = "fan%d_pulses", |
| 501 | [hwmon_fan_target] = "fan%d_target", |
| 502 | [hwmon_fan_alarm] = "fan%d_alarm", |
| 503 | [hwmon_fan_min_alarm] = "fan%d_min_alarm", |
| 504 | [hwmon_fan_max_alarm] = "fan%d_max_alarm", |
| 505 | [hwmon_fan_fault] = "fan%d_fault", |
| 506 | }; |
| 507 | |
Guenter Roeck | f9f7bb3 | 2016-06-26 12:20:46 -0700 | [diff] [blame] | 508 | static const char * const hwmon_pwm_attr_templates[] = { |
| 509 | [hwmon_pwm_input] = "pwm%d", |
| 510 | [hwmon_pwm_enable] = "pwm%d_enable", |
| 511 | [hwmon_pwm_mode] = "pwm%d_mode", |
| 512 | [hwmon_pwm_freq] = "pwm%d_freq", |
| 513 | }; |
| 514 | |
Dr. David Alan Gilbert | 4413405 | 2019-11-24 20:20:29 +0000 | [diff] [blame] | 515 | static const char * const hwmon_intrusion_attr_templates[] = { |
| 516 | [hwmon_intrusion_alarm] = "intrusion%d_alarm", |
| 517 | [hwmon_intrusion_beep] = "intrusion%d_beep", |
| 518 | }; |
| 519 | |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 520 | static const char * const *__templates[] = { |
Guenter Roeck | f4d325d | 2016-10-16 10:38:52 -0700 | [diff] [blame] | 521 | [hwmon_chip] = hwmon_chip_attrs, |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 522 | [hwmon_temp] = hwmon_temp_attr_templates, |
Guenter Roeck | 00d616c | 2016-06-20 11:01:57 -0700 | [diff] [blame] | 523 | [hwmon_in] = hwmon_in_attr_templates, |
Guenter Roeck | 9b26947 | 2016-06-20 11:10:33 -0700 | [diff] [blame] | 524 | [hwmon_curr] = hwmon_curr_attr_templates, |
Guenter Roeck | b308f5c | 2016-06-20 11:27:36 -0700 | [diff] [blame] | 525 | [hwmon_power] = hwmon_power_attr_templates, |
Guenter Roeck | 6bfcca4 | 2016-06-20 11:38:37 -0700 | [diff] [blame] | 526 | [hwmon_energy] = hwmon_energy_attr_templates, |
| 527 | [hwmon_humidity] = hwmon_humidity_attr_templates, |
Guenter Roeck | 8faee73 | 2016-06-25 19:52:13 -0700 | [diff] [blame] | 528 | [hwmon_fan] = hwmon_fan_attr_templates, |
Guenter Roeck | f9f7bb3 | 2016-06-26 12:20:46 -0700 | [diff] [blame] | 529 | [hwmon_pwm] = hwmon_pwm_attr_templates, |
Dr. David Alan Gilbert | 4413405 | 2019-11-24 20:20:29 +0000 | [diff] [blame] | 530 | [hwmon_intrusion] = hwmon_intrusion_attr_templates, |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 531 | }; |
| 532 | |
| 533 | static const int __templates_size[] = { |
Guenter Roeck | f4d325d | 2016-10-16 10:38:52 -0700 | [diff] [blame] | 534 | [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs), |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 535 | [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates), |
Guenter Roeck | 00d616c | 2016-06-20 11:01:57 -0700 | [diff] [blame] | 536 | [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates), |
Guenter Roeck | 9b26947 | 2016-06-20 11:10:33 -0700 | [diff] [blame] | 537 | [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates), |
Guenter Roeck | b308f5c | 2016-06-20 11:27:36 -0700 | [diff] [blame] | 538 | [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates), |
Guenter Roeck | 6bfcca4 | 2016-06-20 11:38:37 -0700 | [diff] [blame] | 539 | [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates), |
| 540 | [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates), |
Guenter Roeck | 8faee73 | 2016-06-25 19:52:13 -0700 | [diff] [blame] | 541 | [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates), |
Guenter Roeck | f9f7bb3 | 2016-06-26 12:20:46 -0700 | [diff] [blame] | 542 | [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates), |
Dr. David Alan Gilbert | 4413405 | 2019-11-24 20:20:29 +0000 | [diff] [blame] | 543 | [hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates), |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 544 | }; |
| 545 | |
| 546 | static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info) |
| 547 | { |
| 548 | int i, n; |
| 549 | |
| 550 | for (i = n = 0; info->config[i]; i++) |
| 551 | n += hweight32(info->config[i]); |
| 552 | |
| 553 | return n; |
| 554 | } |
| 555 | |
Guenter Roeck | 3bf8bdc | 2020-01-16 10:44:17 -0800 | [diff] [blame] | 556 | static int hwmon_genattrs(const void *drvdata, |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 557 | struct attribute **attrs, |
| 558 | const struct hwmon_ops *ops, |
| 559 | const struct hwmon_channel_info *info) |
| 560 | { |
| 561 | const char * const *templates; |
| 562 | int template_size; |
| 563 | int i, aindex = 0; |
| 564 | |
| 565 | if (info->type >= ARRAY_SIZE(__templates)) |
| 566 | return -EINVAL; |
| 567 | |
| 568 | templates = __templates[info->type]; |
| 569 | template_size = __templates_size[info->type]; |
| 570 | |
| 571 | for (i = 0; info->config[i]; i++) { |
| 572 | u32 attr_mask = info->config[i]; |
| 573 | u32 attr; |
| 574 | |
| 575 | while (attr_mask) { |
| 576 | struct attribute *a; |
| 577 | |
| 578 | attr = __ffs(attr_mask); |
| 579 | attr_mask &= ~BIT(attr); |
| 580 | if (attr >= template_size) |
| 581 | return -EINVAL; |
Guenter Roeck | 3bf8bdc | 2020-01-16 10:44:17 -0800 | [diff] [blame] | 582 | a = hwmon_genattr(drvdata, info->type, attr, i, |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 583 | templates[attr], ops); |
| 584 | if (IS_ERR(a)) { |
| 585 | if (PTR_ERR(a) != -ENOENT) |
| 586 | return PTR_ERR(a); |
| 587 | continue; |
| 588 | } |
| 589 | attrs[aindex++] = a; |
| 590 | } |
| 591 | } |
| 592 | return aindex; |
| 593 | } |
| 594 | |
| 595 | static struct attribute ** |
Guenter Roeck | 3bf8bdc | 2020-01-16 10:44:17 -0800 | [diff] [blame] | 596 | __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip) |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 597 | { |
| 598 | int ret, i, aindex = 0, nattrs = 0; |
| 599 | struct attribute **attrs; |
| 600 | |
| 601 | for (i = 0; chip->info[i]; i++) |
| 602 | nattrs += hwmon_num_channel_attrs(chip->info[i]); |
| 603 | |
| 604 | if (nattrs == 0) |
| 605 | return ERR_PTR(-EINVAL); |
| 606 | |
Guenter Roeck | 3bf8bdc | 2020-01-16 10:44:17 -0800 | [diff] [blame] | 607 | attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL); |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 608 | if (!attrs) |
| 609 | return ERR_PTR(-ENOMEM); |
| 610 | |
| 611 | for (i = 0; chip->info[i]; i++) { |
Guenter Roeck | 3bf8bdc | 2020-01-16 10:44:17 -0800 | [diff] [blame] | 612 | ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops, |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 613 | chip->info[i]); |
Guenter Roeck | 3bf8bdc | 2020-01-16 10:44:17 -0800 | [diff] [blame] | 614 | if (ret < 0) { |
| 615 | hwmon_free_attrs(attrs); |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 616 | return ERR_PTR(ret); |
Guenter Roeck | 3bf8bdc | 2020-01-16 10:44:17 -0800 | [diff] [blame] | 617 | } |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 618 | aindex += ret; |
| 619 | } |
| 620 | |
| 621 | return attrs; |
| 622 | } |
| 623 | |
| 624 | static struct device * |
| 625 | __hwmon_device_register(struct device *dev, const char *name, void *drvdata, |
| 626 | const struct hwmon_chip_info *chip, |
| 627 | const struct attribute_group **groups) |
| 628 | { |
| 629 | struct hwmon_device *hwdev; |
| 630 | struct device *hdev; |
Akinobu Mita | 44e3ad8 | 2020-05-04 23:57:44 +0900 | [diff] [blame^] | 631 | int i, err, id; |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 632 | |
Guenter Roeck | 74d3b64 | 2017-01-27 19:35:57 -0800 | [diff] [blame] | 633 | /* Complain about invalid characters in hwmon name attribute */ |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 634 | if (name && (!strlen(name) || strpbrk(name, "-* \t\n"))) |
Guenter Roeck | 74d3b64 | 2017-01-27 19:35:57 -0800 | [diff] [blame] | 635 | dev_warn(dev, |
| 636 | "hwmon: '%s' is not a valid name attribute, please fix\n", |
| 637 | name); |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 638 | |
| 639 | id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL); |
| 640 | if (id < 0) |
| 641 | return ERR_PTR(id); |
| 642 | |
| 643 | hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL); |
| 644 | if (hwdev == NULL) { |
| 645 | err = -ENOMEM; |
| 646 | goto ida_remove; |
| 647 | } |
| 648 | |
| 649 | hdev = &hwdev->dev; |
| 650 | |
Guenter Roeck | 239552f | 2016-10-16 17:06:20 -0700 | [diff] [blame] | 651 | if (chip) { |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 652 | struct attribute **attrs; |
Guenter Roeck | b2a4cc3 | 2016-10-16 17:11:52 -0700 | [diff] [blame] | 653 | int ngroups = 2; /* terminating NULL plus &hwdev->groups */ |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 654 | |
| 655 | if (groups) |
| 656 | for (i = 0; groups[i]; i++) |
| 657 | ngroups++; |
| 658 | |
Guenter Roeck | 3bf8bdc | 2020-01-16 10:44:17 -0800 | [diff] [blame] | 659 | hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL); |
Colin Ian King | 38d8ed6 | 2016-10-23 21:56:08 +0100 | [diff] [blame] | 660 | if (!hwdev->groups) { |
| 661 | err = -ENOMEM; |
| 662 | goto free_hwmon; |
| 663 | } |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 664 | |
Guenter Roeck | 3bf8bdc | 2020-01-16 10:44:17 -0800 | [diff] [blame] | 665 | attrs = __hwmon_create_attrs(drvdata, chip); |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 666 | if (IS_ERR(attrs)) { |
| 667 | err = PTR_ERR(attrs); |
| 668 | goto free_hwmon; |
| 669 | } |
| 670 | |
| 671 | hwdev->group.attrs = attrs; |
| 672 | ngroups = 0; |
| 673 | hwdev->groups[ngroups++] = &hwdev->group; |
| 674 | |
| 675 | if (groups) { |
| 676 | for (i = 0; groups[i]; i++) |
| 677 | hwdev->groups[ngroups++] = groups[i]; |
| 678 | } |
| 679 | |
| 680 | hdev->groups = hwdev->groups; |
| 681 | } else { |
| 682 | hdev->groups = groups; |
| 683 | } |
| 684 | |
| 685 | hwdev->name = name; |
| 686 | hdev->class = &hwmon_class; |
| 687 | hdev->parent = dev; |
| 688 | hdev->of_node = dev ? dev->of_node : NULL; |
| 689 | hwdev->chip = chip; |
| 690 | dev_set_drvdata(hdev, drvdata); |
| 691 | dev_set_name(hdev, HWMON_ID_FORMAT, id); |
| 692 | err = device_register(hdev); |
| 693 | if (err) |
| 694 | goto free_hwmon; |
| 695 | |
Eduardo Valentin | c41dd48 | 2019-05-29 19:56:04 -0700 | [diff] [blame] | 696 | if (dev && dev->of_node && chip && chip->ops->read && |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 697 | chip->info[0]->type == hwmon_chip && |
| 698 | (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) { |
Akinobu Mita | 44e3ad8 | 2020-05-04 23:57:44 +0900 | [diff] [blame^] | 699 | err = hwmon_thermal_register_sensors(hdev); |
| 700 | if (err) { |
| 701 | device_unregister(hdev); |
| 702 | /* |
| 703 | * Don't worry about hwdev; hwmon_dev_release(), called |
| 704 | * from device_unregister(), will free it. |
| 705 | */ |
| 706 | goto ida_remove; |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
| 710 | return hdev; |
| 711 | |
| 712 | free_hwmon: |
Guenter Roeck | 3bf8bdc | 2020-01-16 10:44:17 -0800 | [diff] [blame] | 713 | hwmon_dev_release(hdev); |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 714 | ida_remove: |
| 715 | ida_simple_remove(&hwmon_ida, id); |
| 716 | return ERR_PTR(err); |
| 717 | } |
| 718 | |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 719 | /** |
Guenter Roeck | bab2243 | 2013-07-06 13:57:23 -0700 | [diff] [blame] | 720 | * hwmon_device_register_with_groups - register w/ hwmon |
| 721 | * @dev: the parent device |
| 722 | * @name: hwmon name attribute |
| 723 | * @drvdata: driver data to attach to created device |
| 724 | * @groups: List of attribute groups to create |
| 725 | * |
| 726 | * hwmon_device_unregister() must be called when the device is no |
| 727 | * longer needed. |
| 728 | * |
| 729 | * Returns the pointer to the new device. |
| 730 | */ |
| 731 | struct device * |
| 732 | hwmon_device_register_with_groups(struct device *dev, const char *name, |
| 733 | void *drvdata, |
| 734 | const struct attribute_group **groups) |
| 735 | { |
Guenter Roeck | 8353863 | 2017-01-24 06:32:57 -0800 | [diff] [blame] | 736 | if (!name) |
| 737 | return ERR_PTR(-EINVAL); |
| 738 | |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 739 | return __hwmon_device_register(dev, name, drvdata, NULL, groups); |
Guenter Roeck | bab2243 | 2013-07-06 13:57:23 -0700 | [diff] [blame] | 740 | } |
| 741 | EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups); |
| 742 | |
| 743 | /** |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 744 | * hwmon_device_register_with_info - register w/ hwmon |
| 745 | * @dev: the parent device |
| 746 | * @name: hwmon name attribute |
| 747 | * @drvdata: driver data to attach to created device |
Guenter Roeck | 3870945 | 2017-12-03 15:15:00 -0800 | [diff] [blame] | 748 | * @chip: pointer to hwmon chip information |
Guenter Roeck | 848ba0a | 2016-10-16 17:20:43 -0700 | [diff] [blame] | 749 | * @extra_groups: pointer to list of additional non-standard attribute groups |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 750 | * |
| 751 | * hwmon_device_unregister() must be called when the device is no |
| 752 | * longer needed. |
| 753 | * |
| 754 | * Returns the pointer to the new device. |
| 755 | */ |
| 756 | struct device * |
| 757 | hwmon_device_register_with_info(struct device *dev, const char *name, |
| 758 | void *drvdata, |
| 759 | const struct hwmon_chip_info *chip, |
Guenter Roeck | 848ba0a | 2016-10-16 17:20:43 -0700 | [diff] [blame] | 760 | const struct attribute_group **extra_groups) |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 761 | { |
Guenter Roeck | 8353863 | 2017-01-24 06:32:57 -0800 | [diff] [blame] | 762 | if (!name) |
| 763 | return ERR_PTR(-EINVAL); |
| 764 | |
Guenter Roeck | 239552f | 2016-10-16 17:06:20 -0700 | [diff] [blame] | 765 | if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info)) |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 766 | return ERR_PTR(-EINVAL); |
| 767 | |
Lucas Magasweran | 59df4f4 | 2018-05-08 04:43:33 -0700 | [diff] [blame] | 768 | if (chip && !dev) |
| 769 | return ERR_PTR(-EINVAL); |
| 770 | |
Guenter Roeck | 848ba0a | 2016-10-16 17:20:43 -0700 | [diff] [blame] | 771 | return __hwmon_device_register(dev, name, drvdata, chip, extra_groups); |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 772 | } |
| 773 | EXPORT_SYMBOL_GPL(hwmon_device_register_with_info); |
| 774 | |
| 775 | /** |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 776 | * hwmon_device_register - register w/ hwmon |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 777 | * @dev: the device to register |
| 778 | * |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 779 | * hwmon_device_unregister() must be called when the device is no |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 780 | * longer needed. |
| 781 | * |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 782 | * Returns the pointer to the new device. |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 783 | */ |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 784 | struct device *hwmon_device_register(struct device *dev) |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 785 | { |
Guenter Roeck | af1bd36 | 2016-10-16 11:31:08 -0700 | [diff] [blame] | 786 | dev_warn(dev, |
| 787 | "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n"); |
| 788 | |
Guenter Roeck | 8353863 | 2017-01-24 06:32:57 -0800 | [diff] [blame] | 789 | return __hwmon_device_register(dev, NULL, NULL, NULL, NULL); |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 790 | } |
Frans Meulenbroeks | 839a9ee | 2012-01-08 19:34:14 +0100 | [diff] [blame] | 791 | EXPORT_SYMBOL_GPL(hwmon_device_register); |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 792 | |
| 793 | /** |
| 794 | * hwmon_device_unregister - removes the previously registered class device |
| 795 | * |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 796 | * @dev: the class device to destroy |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 797 | */ |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 798 | void hwmon_device_unregister(struct device *dev) |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 799 | { |
| 800 | int id; |
| 801 | |
Kay Sievers | 739cf3a | 2009-01-06 10:44:41 -0800 | [diff] [blame] | 802 | if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) { |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 803 | device_unregister(dev); |
Jonathan Cameron | 4ca5f46 | 2011-10-31 17:10:09 -0700 | [diff] [blame] | 804 | ida_simple_remove(&hwmon_ida, id); |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 805 | } else |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 806 | dev_dbg(dev->parent, |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 807 | "hwmon_device_unregister() failed: bad class ID!\n"); |
| 808 | } |
Frans Meulenbroeks | 839a9ee | 2012-01-08 19:34:14 +0100 | [diff] [blame] | 809 | EXPORT_SYMBOL_GPL(hwmon_device_unregister); |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 810 | |
Guenter Roeck | 74188cb | 2013-07-11 20:00:12 -0700 | [diff] [blame] | 811 | static void devm_hwmon_release(struct device *dev, void *res) |
| 812 | { |
| 813 | struct device *hwdev = *(struct device **)res; |
| 814 | |
| 815 | hwmon_device_unregister(hwdev); |
| 816 | } |
| 817 | |
| 818 | /** |
| 819 | * devm_hwmon_device_register_with_groups - register w/ hwmon |
| 820 | * @dev: the parent device |
| 821 | * @name: hwmon name attribute |
| 822 | * @drvdata: driver data to attach to created device |
| 823 | * @groups: List of attribute groups to create |
| 824 | * |
| 825 | * Returns the pointer to the new device. The new device is automatically |
| 826 | * unregistered with the parent device. |
| 827 | */ |
| 828 | struct device * |
| 829 | devm_hwmon_device_register_with_groups(struct device *dev, const char *name, |
| 830 | void *drvdata, |
| 831 | const struct attribute_group **groups) |
| 832 | { |
| 833 | struct device **ptr, *hwdev; |
| 834 | |
| 835 | if (!dev) |
| 836 | return ERR_PTR(-EINVAL); |
| 837 | |
| 838 | ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); |
| 839 | if (!ptr) |
| 840 | return ERR_PTR(-ENOMEM); |
| 841 | |
| 842 | hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups); |
| 843 | if (IS_ERR(hwdev)) |
| 844 | goto error; |
| 845 | |
| 846 | *ptr = hwdev; |
| 847 | devres_add(dev, ptr); |
| 848 | return hwdev; |
| 849 | |
| 850 | error: |
| 851 | devres_free(ptr); |
| 852 | return hwdev; |
| 853 | } |
| 854 | EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups); |
| 855 | |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 856 | /** |
| 857 | * devm_hwmon_device_register_with_info - register w/ hwmon |
Guenter Roeck | 3870945 | 2017-12-03 15:15:00 -0800 | [diff] [blame] | 858 | * @dev: the parent device |
| 859 | * @name: hwmon name attribute |
| 860 | * @drvdata: driver data to attach to created device |
| 861 | * @chip: pointer to hwmon chip information |
| 862 | * @groups: pointer to list of driver specific attribute groups |
Guenter Roeck | d560168 | 2015-08-26 19:38:11 -0700 | [diff] [blame] | 863 | * |
| 864 | * Returns the pointer to the new device. The new device is automatically |
| 865 | * unregistered with the parent device. |
| 866 | */ |
| 867 | struct device * |
| 868 | devm_hwmon_device_register_with_info(struct device *dev, const char *name, |
| 869 | void *drvdata, |
| 870 | const struct hwmon_chip_info *chip, |
| 871 | const struct attribute_group **groups) |
| 872 | { |
| 873 | struct device **ptr, *hwdev; |
| 874 | |
| 875 | if (!dev) |
| 876 | return ERR_PTR(-EINVAL); |
| 877 | |
| 878 | ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); |
| 879 | if (!ptr) |
| 880 | return ERR_PTR(-ENOMEM); |
| 881 | |
| 882 | hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip, |
| 883 | groups); |
| 884 | if (IS_ERR(hwdev)) |
| 885 | goto error; |
| 886 | |
| 887 | *ptr = hwdev; |
| 888 | devres_add(dev, ptr); |
| 889 | |
| 890 | return hwdev; |
| 891 | |
| 892 | error: |
| 893 | devres_free(ptr); |
| 894 | return hwdev; |
| 895 | } |
| 896 | EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info); |
| 897 | |
Guenter Roeck | 74188cb | 2013-07-11 20:00:12 -0700 | [diff] [blame] | 898 | static int devm_hwmon_match(struct device *dev, void *res, void *data) |
| 899 | { |
| 900 | struct device **hwdev = res; |
| 901 | |
| 902 | return *hwdev == data; |
| 903 | } |
| 904 | |
| 905 | /** |
| 906 | * devm_hwmon_device_unregister - removes a previously registered hwmon device |
| 907 | * |
| 908 | * @dev: the parent device of the device to unregister |
| 909 | */ |
| 910 | void devm_hwmon_device_unregister(struct device *dev) |
| 911 | { |
| 912 | WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev)); |
| 913 | } |
| 914 | EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister); |
| 915 | |
Jean Delvare | 2958b1e | 2009-06-15 18:39:50 +0200 | [diff] [blame] | 916 | static void __init hwmon_pci_quirks(void) |
| 917 | { |
| 918 | #if defined CONFIG_X86 && defined CONFIG_PCI |
| 919 | struct pci_dev *sb; |
| 920 | u16 base; |
| 921 | u8 enable; |
| 922 | |
| 923 | /* Open access to 0x295-0x296 on MSI MS-7031 */ |
| 924 | sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL); |
Jean Delvare | d6dab7d | 2012-12-19 22:16:59 +0100 | [diff] [blame] | 925 | if (sb) { |
| 926 | if (sb->subsystem_vendor == 0x1462 && /* MSI */ |
| 927 | sb->subsystem_device == 0x0031) { /* MS-7031 */ |
| 928 | pci_read_config_byte(sb, 0x48, &enable); |
| 929 | pci_read_config_word(sb, 0x64, &base); |
Jean Delvare | 2958b1e | 2009-06-15 18:39:50 +0200 | [diff] [blame] | 930 | |
Jean Delvare | d6dab7d | 2012-12-19 22:16:59 +0100 | [diff] [blame] | 931 | if (base == 0 && !(enable & BIT(2))) { |
| 932 | dev_info(&sb->dev, |
| 933 | "Opening wide generic port at 0x295\n"); |
| 934 | pci_write_config_word(sb, 0x64, 0x295); |
| 935 | pci_write_config_byte(sb, 0x48, |
| 936 | enable | BIT(2)); |
| 937 | } |
Jean Delvare | 2958b1e | 2009-06-15 18:39:50 +0200 | [diff] [blame] | 938 | } |
Jean Delvare | d6dab7d | 2012-12-19 22:16:59 +0100 | [diff] [blame] | 939 | pci_dev_put(sb); |
Jean Delvare | 2958b1e | 2009-06-15 18:39:50 +0200 | [diff] [blame] | 940 | } |
| 941 | #endif |
| 942 | } |
| 943 | |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 944 | static int __init hwmon_init(void) |
| 945 | { |
Guenter Roeck | bab2243 | 2013-07-06 13:57:23 -0700 | [diff] [blame] | 946 | int err; |
| 947 | |
Jean Delvare | 2958b1e | 2009-06-15 18:39:50 +0200 | [diff] [blame] | 948 | hwmon_pci_quirks(); |
| 949 | |
Guenter Roeck | bab2243 | 2013-07-06 13:57:23 -0700 | [diff] [blame] | 950 | err = class_register(&hwmon_class); |
| 951 | if (err) { |
| 952 | pr_err("couldn't register hwmon sysfs class\n"); |
| 953 | return err; |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 954 | } |
| 955 | return 0; |
| 956 | } |
| 957 | |
| 958 | static void __exit hwmon_exit(void) |
| 959 | { |
Guenter Roeck | bab2243 | 2013-07-06 13:57:23 -0700 | [diff] [blame] | 960 | class_unregister(&hwmon_class); |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 961 | } |
| 962 | |
David Brownell | 37f54ee | 2007-02-14 21:15:04 +0100 | [diff] [blame] | 963 | subsys_initcall(hwmon_init); |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 964 | module_exit(hwmon_exit); |
| 965 | |
Mark M. Hoffman | 1236441 | 2005-07-15 21:38:08 -0400 | [diff] [blame] | 966 | MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>"); |
| 967 | MODULE_DESCRIPTION("hardware monitoring sysfs/class support"); |
| 968 | MODULE_LICENSE("GPL"); |
| 969 | |