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