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