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