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