at commit c03c1a14
| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring |
| 4 | * |
| 5 | * This file defines the sysfs class "hwmon", for use by sensors drivers. |
| 6 | * |
| 7 | * Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com> |
| 8 | */ |
| 9 | |
| 10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 11 | |
| 12 | #include <linux/bitops.h> |
| 13 | #include <linux/device.h> |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/gfp.h> |
| 16 | #include <linux/hwmon.h> |
| 17 | #include <linux/i2c.h> |
| 18 | #include <linux/idr.h> |
| 19 | #include <linux/kstrtox.h> |
| 20 | #include <linux/list.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/mutex.h> |
| 23 | #include <linux/pci.h> |
| 24 | #include <linux/property.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/string.h> |
| 27 | #include <linux/thermal.h> |
| 28 | |
| 29 | #define CREATE_TRACE_POINTS |
| 30 | #include <trace/events/hwmon.h> |
| 31 | |
| 32 | #define HWMON_ID_PREFIX "hwmon" |
| 33 | #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d" |
| 34 | |
| 35 | struct hwmon_device { |
| 36 | const char *name; |
| 37 | const char *label; |
| 38 | struct device dev; |
| 39 | const struct hwmon_chip_info *chip; |
| 40 | struct mutex lock; |
| 41 | struct list_head tzdata; |
| 42 | struct attribute_group group; |
| 43 | const struct attribute_group **groups; |
| 44 | }; |
| 45 | |
| 46 | #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev) |
| 47 | |
| 48 | #define MAX_SYSFS_ATTR_NAME_LENGTH 32 |
| 49 | |
| 50 | struct hwmon_device_attribute { |
| 51 | struct device_attribute dev_attr; |
| 52 | const struct hwmon_ops *ops; |
| 53 | enum hwmon_sensor_types type; |
| 54 | u32 attr; |
| 55 | int index; |
| 56 | char name[MAX_SYSFS_ATTR_NAME_LENGTH]; |
| 57 | }; |
| 58 | |
| 59 | #define to_hwmon_attr(d) \ |
| 60 | container_of_const(d, struct hwmon_device_attribute, dev_attr) |
| 61 | #define to_dev_attr(a) container_of_const(a, struct device_attribute, attr) |
| 62 | |
| 63 | /* |
| 64 | * Thermal zone information |
| 65 | */ |
| 66 | struct hwmon_thermal_data { |
| 67 | struct list_head node; /* hwmon tzdata list entry */ |
| 68 | struct device *dev; /* Reference to hwmon device */ |
| 69 | int index; /* sensor index */ |
| 70 | struct thermal_zone_device *tzd;/* thermal zone device */ |
| 71 | }; |
| 72 | |
| 73 | static ssize_t |
| 74 | name_show(struct device *dev, const struct device_attribute *attr, char *buf) |
| 75 | { |
| 76 | return sysfs_emit(buf, "%s\n", to_hwmon_device(dev)->name); |
| 77 | } |
| 78 | static const DEVICE_ATTR_RO(name); |
| 79 | |
| 80 | static ssize_t |
| 81 | label_show(struct device *dev, const struct device_attribute *attr, char *buf) |
| 82 | { |
| 83 | return sysfs_emit(buf, "%s\n", to_hwmon_device(dev)->label); |
| 84 | } |
| 85 | static const DEVICE_ATTR_RO(label); |
| 86 | |
| 87 | static const struct attribute *const hwmon_dev_attrs[] = { |
| 88 | &dev_attr_name.attr, |
| 89 | &dev_attr_label.attr, |
| 90 | NULL |
| 91 | }; |
| 92 | |
| 93 | static umode_t hwmon_dev_attr_is_visible(struct kobject *kobj, |
| 94 | const struct attribute *attr, int n) |
| 95 | { |
| 96 | struct device *dev = kobj_to_dev(kobj); |
| 97 | struct hwmon_device *hdev = to_hwmon_device(dev); |
| 98 | |
| 99 | if (attr == &dev_attr_name.attr && hdev->name == NULL) |
| 100 | return 0; |
| 101 | |
| 102 | if (attr == &dev_attr_label.attr && hdev->label == NULL) |
| 103 | return 0; |
| 104 | |
| 105 | return attr->mode; |
| 106 | } |
| 107 | |
| 108 | static const struct attribute_group hwmon_dev_attr_group = { |
| 109 | .attrs_const = hwmon_dev_attrs, |
| 110 | .is_visible_const = hwmon_dev_attr_is_visible, |
| 111 | }; |
| 112 | |
| 113 | static const struct attribute_group *hwmon_dev_attr_groups[] = { |
| 114 | &hwmon_dev_attr_group, |
| 115 | NULL |
| 116 | }; |
| 117 | |
| 118 | static void hwmon_free_attrs(const struct attribute *const *attrs) |
| 119 | { |
| 120 | int i; |
| 121 | |
| 122 | for (i = 0; attrs[i]; i++) { |
| 123 | const struct device_attribute *dattr = to_dev_attr(attrs[i]); |
| 124 | const struct hwmon_device_attribute *hattr = to_hwmon_attr(dattr); |
| 125 | |
| 126 | kfree(hattr); |
| 127 | } |
| 128 | kfree(attrs); |
| 129 | } |
| 130 | |
| 131 | static void hwmon_dev_release(struct device *dev) |
| 132 | { |
| 133 | struct hwmon_device *hwdev = to_hwmon_device(dev); |
| 134 | |
| 135 | if (hwdev->group.attrs_const) |
| 136 | hwmon_free_attrs(hwdev->group.attrs_const); |
| 137 | kfree(hwdev->groups); |
| 138 | kfree(hwdev->label); |
| 139 | kfree(hwdev); |
| 140 | } |
| 141 | |
| 142 | static const struct class hwmon_class = { |
| 143 | .name = "hwmon", |
| 144 | .dev_groups = hwmon_dev_attr_groups, |
| 145 | .dev_release = hwmon_dev_release, |
| 146 | }; |
| 147 | |
| 148 | static DEFINE_IDA(hwmon_ida); |
| 149 | |
| 150 | static umode_t hwmon_is_visible(const struct hwmon_ops *ops, |
| 151 | const void *drvdata, |
| 152 | enum hwmon_sensor_types type, |
| 153 | u32 attr, int channel) |
| 154 | { |
| 155 | if (ops->visible) |
| 156 | return ops->visible; |
| 157 | |
| 158 | return ops->is_visible(drvdata, type, attr, channel); |
| 159 | } |
| 160 | |
| 161 | /* Thermal zone handling */ |
| 162 | |
| 163 | static int hwmon_thermal_get_temp(struct thermal_zone_device *tz, int *temp) |
| 164 | { |
| 165 | struct hwmon_thermal_data *tdata = thermal_zone_device_priv(tz); |
| 166 | struct hwmon_device *hwdev = to_hwmon_device(tdata->dev); |
| 167 | int ret; |
| 168 | long t; |
| 169 | |
| 170 | guard(mutex)(&hwdev->lock); |
| 171 | |
| 172 | ret = hwdev->chip->ops->read(tdata->dev, hwmon_temp, hwmon_temp_input, |
| 173 | tdata->index, &t); |
| 174 | if (ret < 0) |
| 175 | return ret; |
| 176 | |
| 177 | *temp = t; |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static int hwmon_thermal_set_trips(struct thermal_zone_device *tz, int low, int high) |
| 183 | { |
| 184 | struct hwmon_thermal_data *tdata = thermal_zone_device_priv(tz); |
| 185 | struct hwmon_device *hwdev = to_hwmon_device(tdata->dev); |
| 186 | const struct hwmon_chip_info *chip = hwdev->chip; |
| 187 | const struct hwmon_channel_info * const *info = chip->info; |
| 188 | unsigned int i; |
| 189 | int err; |
| 190 | |
| 191 | if (!chip->ops->write) |
| 192 | return 0; |
| 193 | |
| 194 | for (i = 0; info[i] && info[i]->type != hwmon_temp; i++) |
| 195 | continue; |
| 196 | |
| 197 | if (!info[i]) |
| 198 | return 0; |
| 199 | |
| 200 | guard(mutex)(&hwdev->lock); |
| 201 | |
| 202 | if (info[i]->config[tdata->index] & HWMON_T_MIN) { |
| 203 | err = chip->ops->write(tdata->dev, hwmon_temp, |
| 204 | hwmon_temp_min, tdata->index, low); |
| 205 | if (err && err != -EOPNOTSUPP) |
| 206 | return err; |
| 207 | } |
| 208 | |
| 209 | if (info[i]->config[tdata->index] & HWMON_T_MAX) { |
| 210 | err = chip->ops->write(tdata->dev, hwmon_temp, |
| 211 | hwmon_temp_max, tdata->index, high); |
| 212 | if (err && err != -EOPNOTSUPP) |
| 213 | return err; |
| 214 | } |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static const struct thermal_zone_device_ops hwmon_thermal_ops = { |
| 220 | .get_temp = hwmon_thermal_get_temp, |
| 221 | .set_trips = hwmon_thermal_set_trips, |
| 222 | }; |
| 223 | |
| 224 | static void hwmon_thermal_remove_sensor(void *data) |
| 225 | { |
| 226 | list_del(data); |
| 227 | } |
| 228 | |
| 229 | static int hwmon_thermal_add_sensor(struct device *dev, int index) |
| 230 | { |
| 231 | struct hwmon_device *hwdev = to_hwmon_device(dev); |
| 232 | struct hwmon_thermal_data *tdata; |
| 233 | struct thermal_zone_device *tzd; |
| 234 | int err; |
| 235 | |
| 236 | tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL); |
| 237 | if (!tdata) |
| 238 | return -ENOMEM; |
| 239 | |
| 240 | tdata->dev = dev; |
| 241 | tdata->index = index; |
| 242 | |
| 243 | tzd = devm_thermal_of_zone_register(dev, index, tdata, |
| 244 | &hwmon_thermal_ops); |
| 245 | if (IS_ERR(tzd)) { |
| 246 | if (PTR_ERR(tzd) != -ENODEV) |
| 247 | return PTR_ERR(tzd); |
| 248 | dev_info(dev, "temp%d_input not attached to any thermal zone\n", |
| 249 | index + 1); |
| 250 | devm_kfree(dev, tdata); |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | err = devm_add_action(dev, hwmon_thermal_remove_sensor, &tdata->node); |
| 255 | if (err) |
| 256 | return err; |
| 257 | |
| 258 | tdata->tzd = tzd; |
| 259 | list_add(&tdata->node, &hwdev->tzdata); |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | static int hwmon_thermal_register_sensors(struct device *dev) |
| 265 | { |
| 266 | struct hwmon_device *hwdev = to_hwmon_device(dev); |
| 267 | const struct hwmon_chip_info *chip = hwdev->chip; |
| 268 | const struct hwmon_channel_info * const *info = chip->info; |
| 269 | void *drvdata = dev_get_drvdata(dev); |
| 270 | int i; |
| 271 | |
| 272 | if (!IS_ENABLED(CONFIG_THERMAL_OF)) |
| 273 | return 0; |
| 274 | |
| 275 | for (i = 1; info[i]; i++) { |
| 276 | int j; |
| 277 | |
| 278 | if (info[i]->type != hwmon_temp) |
| 279 | continue; |
| 280 | |
| 281 | for (j = 0; info[i]->config[j]; j++) { |
| 282 | int err; |
| 283 | |
| 284 | if (!(info[i]->config[j] & HWMON_T_INPUT) || |
| 285 | !hwmon_is_visible(chip->ops, drvdata, hwmon_temp, |
| 286 | hwmon_temp_input, j)) |
| 287 | continue; |
| 288 | |
| 289 | err = hwmon_thermal_add_sensor(dev, j); |
| 290 | if (err) |
| 291 | return err; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static void hwmon_thermal_notify(struct device *dev, int index) |
| 299 | { |
| 300 | struct hwmon_device *hwdev = to_hwmon_device(dev); |
| 301 | struct hwmon_thermal_data *tzdata; |
| 302 | |
| 303 | if (!IS_ENABLED(CONFIG_THERMAL_OF)) |
| 304 | return; |
| 305 | |
| 306 | list_for_each_entry(tzdata, &hwdev->tzdata, node) { |
| 307 | if (tzdata->index == index) { |
| 308 | thermal_zone_device_update(tzdata->tzd, |
| 309 | THERMAL_EVENT_UNSPECIFIED); |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | static int hwmon_attr_base(enum hwmon_sensor_types type) |
| 315 | { |
| 316 | if (type == hwmon_in || type == hwmon_intrusion) |
| 317 | return 0; |
| 318 | return 1; |
| 319 | } |
| 320 | |
| 321 | static bool is_hwmon_device(struct device *dev) |
| 322 | { |
| 323 | return dev->class == &hwmon_class; |
| 324 | } |
| 325 | |
| 326 | #if IS_REACHABLE(CONFIG_I2C) |
| 327 | |
| 328 | /* |
| 329 | * PEC support |
| 330 | * |
| 331 | * The 'pec' attribute is attached to I2C client devices. It is only provided |
| 332 | * if the i2c controller supports PEC. |
| 333 | * |
| 334 | * The mutex ensures that PEC configuration between i2c device and the hardware |
| 335 | * is consistent. Use a single mutex because attribute writes are supposed to be |
| 336 | * rare, and maintaining a separate mutex for each hardware monitoring device |
| 337 | * would add substantial complexity to the driver for little if any gain. |
| 338 | * |
| 339 | * The hardware monitoring device is identified as child of the i2c client |
| 340 | * device. This assumes that only a single hardware monitoring device is |
| 341 | * attached to an i2c client device. |
| 342 | */ |
| 343 | |
| 344 | static int hwmon_match_device(struct device *dev, const void *data) |
| 345 | { |
| 346 | return is_hwmon_device(dev); |
| 347 | } |
| 348 | |
| 349 | static ssize_t pec_show(struct device *dev, const struct device_attribute *dummy, |
| 350 | char *buf) |
| 351 | { |
| 352 | struct i2c_client *client = to_i2c_client(dev); |
| 353 | |
| 354 | return sysfs_emit(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC)); |
| 355 | } |
| 356 | |
| 357 | static ssize_t pec_store(struct device *dev, const struct device_attribute *devattr, |
| 358 | const char *buf, size_t count) |
| 359 | { |
| 360 | struct i2c_client *client = to_i2c_client(dev); |
| 361 | struct hwmon_device *hwdev; |
| 362 | struct device *hdev; |
| 363 | bool val; |
| 364 | int err; |
| 365 | |
| 366 | err = kstrtobool(buf, &val); |
| 367 | if (err < 0) |
| 368 | return err; |
| 369 | |
| 370 | hdev = device_find_child(dev, NULL, hwmon_match_device); |
| 371 | if (!hdev) |
| 372 | return -ENODEV; |
| 373 | |
| 374 | /* |
| 375 | * If there is no write function, we assume that chip specific |
| 376 | * handling is not required. |
| 377 | */ |
| 378 | hwdev = to_hwmon_device(hdev); |
| 379 | scoped_guard(mutex, &hwdev->lock) { |
| 380 | if (hwdev->chip->ops->write) { |
| 381 | err = hwdev->chip->ops->write(hdev, hwmon_chip, hwmon_chip_pec, 0, val); |
| 382 | if (err && err != -EOPNOTSUPP) |
| 383 | goto put; |
| 384 | } |
| 385 | if (!val) |
| 386 | client->flags &= ~I2C_CLIENT_PEC; |
| 387 | else |
| 388 | client->flags |= I2C_CLIENT_PEC; |
| 389 | } |
| 390 | err = count; |
| 391 | put: |
| 392 | put_device(hdev); |
| 393 | |
| 394 | return err; |
| 395 | } |
| 396 | |
| 397 | static const DEVICE_ATTR_RW(pec); |
| 398 | |
| 399 | static void hwmon_remove_pec(void *dev) |
| 400 | { |
| 401 | device_remove_file(dev, &dev_attr_pec); |
| 402 | } |
| 403 | |
| 404 | static int hwmon_pec_register(struct device *hdev) |
| 405 | { |
| 406 | struct i2c_client *client = i2c_verify_client(hdev->parent); |
| 407 | int err; |
| 408 | |
| 409 | if (!client) |
| 410 | return -EINVAL; |
| 411 | |
| 412 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_PEC)) |
| 413 | return 0; |
| 414 | |
| 415 | err = device_create_file(&client->dev, &dev_attr_pec); |
| 416 | if (err) |
| 417 | return err; |
| 418 | |
| 419 | return devm_add_action_or_reset(hdev, hwmon_remove_pec, &client->dev); |
| 420 | } |
| 421 | |
| 422 | #else /* CONFIG_I2C */ |
| 423 | static int hwmon_pec_register(struct device *hdev) |
| 424 | { |
| 425 | return -EINVAL; |
| 426 | } |
| 427 | #endif /* CONFIG_I2C */ |
| 428 | |
| 429 | /* sysfs attribute management */ |
| 430 | |
| 431 | static ssize_t hwmon_attr_show(struct device *dev, |
| 432 | const struct device_attribute *devattr, char *buf) |
| 433 | { |
| 434 | const struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); |
| 435 | struct hwmon_device *hwdev = to_hwmon_device(dev); |
| 436 | s64 val64; |
| 437 | long val; |
| 438 | int ret; |
| 439 | |
| 440 | guard(mutex)(&hwdev->lock); |
| 441 | |
| 442 | ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index, |
| 443 | (hattr->type == hwmon_energy64) ? (long *)&val64 : &val); |
| 444 | if (ret < 0) |
| 445 | return ret; |
| 446 | |
| 447 | if (hattr->type != hwmon_energy64) |
| 448 | val64 = val; |
| 449 | |
| 450 | trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type), |
| 451 | hattr->name, val64); |
| 452 | |
| 453 | return sysfs_emit(buf, "%lld\n", val64); |
| 454 | } |
| 455 | |
| 456 | static ssize_t hwmon_attr_show_string(struct device *dev, |
| 457 | const struct device_attribute *devattr, |
| 458 | char *buf) |
| 459 | { |
| 460 | const struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); |
| 461 | struct hwmon_device *hwdev = to_hwmon_device(dev); |
| 462 | enum hwmon_sensor_types type = hattr->type; |
| 463 | const char *s; |
| 464 | int ret; |
| 465 | |
| 466 | guard(mutex)(&hwdev->lock); |
| 467 | |
| 468 | ret = hattr->ops->read_string(dev, hattr->type, hattr->attr, |
| 469 | hattr->index, &s); |
| 470 | if (ret < 0) |
| 471 | return ret; |
| 472 | |
| 473 | trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type), |
| 474 | hattr->name, s); |
| 475 | |
| 476 | return sysfs_emit(buf, "%s\n", s); |
| 477 | } |
| 478 | |
| 479 | static ssize_t hwmon_attr_store(struct device *dev, |
| 480 | const struct device_attribute *devattr, |
| 481 | const char *buf, size_t count) |
| 482 | { |
| 483 | const struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); |
| 484 | struct hwmon_device *hwdev = to_hwmon_device(dev); |
| 485 | long val; |
| 486 | int ret; |
| 487 | |
| 488 | ret = kstrtol(buf, 10, &val); |
| 489 | if (ret < 0) |
| 490 | return ret; |
| 491 | |
| 492 | guard(mutex)(&hwdev->lock); |
| 493 | |
| 494 | ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index, |
| 495 | val); |
| 496 | if (ret < 0) |
| 497 | return ret; |
| 498 | |
| 499 | trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type), |
| 500 | hattr->name, (s64)val); |
| 501 | |
| 502 | return count; |
| 503 | } |
| 504 | |
| 505 | static bool is_string_attr(enum hwmon_sensor_types type, u32 attr) |
| 506 | { |
| 507 | return (type == hwmon_temp && attr == hwmon_temp_label) || |
| 508 | (type == hwmon_in && attr == hwmon_in_label) || |
| 509 | (type == hwmon_curr && attr == hwmon_curr_label) || |
| 510 | (type == hwmon_power && attr == hwmon_power_label) || |
| 511 | (type == hwmon_energy && attr == hwmon_energy_label) || |
| 512 | (type == hwmon_energy64 && attr == hwmon_energy_label) || |
| 513 | (type == hwmon_humidity && attr == hwmon_humidity_label) || |
| 514 | (type == hwmon_fan && attr == hwmon_fan_label); |
| 515 | } |
| 516 | |
| 517 | static const struct attribute *hwmon_genattr(const void *drvdata, |
| 518 | enum hwmon_sensor_types type, |
| 519 | u32 attr, |
| 520 | int index, |
| 521 | const char *template, |
| 522 | const struct hwmon_ops *ops) |
| 523 | { |
| 524 | struct hwmon_device_attribute *hattr; |
| 525 | struct device_attribute *dattr; |
| 526 | struct attribute *a; |
| 527 | umode_t mode; |
| 528 | const char *name; |
| 529 | bool is_string = is_string_attr(type, attr); |
| 530 | |
| 531 | mode = hwmon_is_visible(ops, drvdata, type, attr, index); |
| 532 | if (!mode) |
| 533 | return ERR_PTR(-ENOENT); |
| 534 | |
| 535 | if ((mode & 0444) && ((is_string && !ops->read_string) || |
| 536 | (!is_string && !ops->read))) |
| 537 | return ERR_PTR(-EINVAL); |
| 538 | if ((mode & 0222) && !ops->write) |
| 539 | return ERR_PTR(-EINVAL); |
| 540 | |
| 541 | hattr = kzalloc_obj(*hattr); |
| 542 | if (!hattr) |
| 543 | return ERR_PTR(-ENOMEM); |
| 544 | |
| 545 | if (type == hwmon_chip) { |
| 546 | name = template; |
| 547 | } else { |
| 548 | scnprintf(hattr->name, sizeof(hattr->name), template, |
| 549 | index + hwmon_attr_base(type)); |
| 550 | name = hattr->name; |
| 551 | } |
| 552 | |
| 553 | hattr->type = type; |
| 554 | hattr->attr = attr; |
| 555 | hattr->index = index; |
| 556 | hattr->ops = ops; |
| 557 | |
| 558 | dattr = &hattr->dev_attr; |
| 559 | dattr->show_const = is_string ? hwmon_attr_show_string : hwmon_attr_show; |
| 560 | dattr->store_const = hwmon_attr_store; |
| 561 | |
| 562 | a = &dattr->attr; |
| 563 | sysfs_attr_init(a); |
| 564 | a->name = name; |
| 565 | a->mode = mode; |
| 566 | |
| 567 | return a; |
| 568 | } |
| 569 | |
| 570 | /* |
| 571 | * Chip attributes are not attribute templates but actual sysfs attributes. |
| 572 | * See hwmon_genattr() for special handling. |
| 573 | */ |
| 574 | static const char * const hwmon_chip_attrs[] = { |
| 575 | [hwmon_chip_temp_reset_history] = "temp_reset_history", |
| 576 | [hwmon_chip_in_reset_history] = "in_reset_history", |
| 577 | [hwmon_chip_curr_reset_history] = "curr_reset_history", |
| 578 | [hwmon_chip_power_reset_history] = "power_reset_history", |
| 579 | [hwmon_chip_update_interval] = "update_interval", |
| 580 | [hwmon_chip_update_interval_us] = "update_interval_us", |
| 581 | [hwmon_chip_alarms] = "alarms", |
| 582 | [hwmon_chip_samples] = "samples", |
| 583 | [hwmon_chip_curr_samples] = "curr_samples", |
| 584 | [hwmon_chip_in_samples] = "in_samples", |
| 585 | [hwmon_chip_power_samples] = "power_samples", |
| 586 | [hwmon_chip_temp_samples] = "temp_samples", |
| 587 | [hwmon_chip_beep_enable] = "beep_enable", |
| 588 | }; |
| 589 | |
| 590 | static const char * const hwmon_temp_attr_templates[] = { |
| 591 | [hwmon_temp_enable] = "temp%d_enable", |
| 592 | [hwmon_temp_input] = "temp%d_input", |
| 593 | [hwmon_temp_type] = "temp%d_type", |
| 594 | [hwmon_temp_lcrit] = "temp%d_lcrit", |
| 595 | [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst", |
| 596 | [hwmon_temp_min] = "temp%d_min", |
| 597 | [hwmon_temp_min_hyst] = "temp%d_min_hyst", |
| 598 | [hwmon_temp_max] = "temp%d_max", |
| 599 | [hwmon_temp_max_hyst] = "temp%d_max_hyst", |
| 600 | [hwmon_temp_crit] = "temp%d_crit", |
| 601 | [hwmon_temp_crit_hyst] = "temp%d_crit_hyst", |
| 602 | [hwmon_temp_emergency] = "temp%d_emergency", |
| 603 | [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst", |
| 604 | [hwmon_temp_alarm] = "temp%d_alarm", |
| 605 | [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm", |
| 606 | [hwmon_temp_min_alarm] = "temp%d_min_alarm", |
| 607 | [hwmon_temp_max_alarm] = "temp%d_max_alarm", |
| 608 | [hwmon_temp_crit_alarm] = "temp%d_crit_alarm", |
| 609 | [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm", |
| 610 | [hwmon_temp_fault] = "temp%d_fault", |
| 611 | [hwmon_temp_offset] = "temp%d_offset", |
| 612 | [hwmon_temp_label] = "temp%d_label", |
| 613 | [hwmon_temp_lowest] = "temp%d_lowest", |
| 614 | [hwmon_temp_highest] = "temp%d_highest", |
| 615 | [hwmon_temp_reset_history] = "temp%d_reset_history", |
| 616 | [hwmon_temp_rated_min] = "temp%d_rated_min", |
| 617 | [hwmon_temp_rated_max] = "temp%d_rated_max", |
| 618 | [hwmon_temp_beep] = "temp%d_beep", |
| 619 | }; |
| 620 | |
| 621 | static const char * const hwmon_in_attr_templates[] = { |
| 622 | [hwmon_in_enable] = "in%d_enable", |
| 623 | [hwmon_in_input] = "in%d_input", |
| 624 | [hwmon_in_min] = "in%d_min", |
| 625 | [hwmon_in_max] = "in%d_max", |
| 626 | [hwmon_in_lcrit] = "in%d_lcrit", |
| 627 | [hwmon_in_crit] = "in%d_crit", |
| 628 | [hwmon_in_average] = "in%d_average", |
| 629 | [hwmon_in_lowest] = "in%d_lowest", |
| 630 | [hwmon_in_highest] = "in%d_highest", |
| 631 | [hwmon_in_reset_history] = "in%d_reset_history", |
| 632 | [hwmon_in_label] = "in%d_label", |
| 633 | [hwmon_in_alarm] = "in%d_alarm", |
| 634 | [hwmon_in_min_alarm] = "in%d_min_alarm", |
| 635 | [hwmon_in_max_alarm] = "in%d_max_alarm", |
| 636 | [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm", |
| 637 | [hwmon_in_crit_alarm] = "in%d_crit_alarm", |
| 638 | [hwmon_in_rated_min] = "in%d_rated_min", |
| 639 | [hwmon_in_rated_max] = "in%d_rated_max", |
| 640 | [hwmon_in_beep] = "in%d_beep", |
| 641 | [hwmon_in_fault] = "in%d_fault", |
| 642 | }; |
| 643 | |
| 644 | static const char * const hwmon_curr_attr_templates[] = { |
| 645 | [hwmon_curr_enable] = "curr%d_enable", |
| 646 | [hwmon_curr_input] = "curr%d_input", |
| 647 | [hwmon_curr_min] = "curr%d_min", |
| 648 | [hwmon_curr_max] = "curr%d_max", |
| 649 | [hwmon_curr_lcrit] = "curr%d_lcrit", |
| 650 | [hwmon_curr_crit] = "curr%d_crit", |
| 651 | [hwmon_curr_average] = "curr%d_average", |
| 652 | [hwmon_curr_lowest] = "curr%d_lowest", |
| 653 | [hwmon_curr_highest] = "curr%d_highest", |
| 654 | [hwmon_curr_reset_history] = "curr%d_reset_history", |
| 655 | [hwmon_curr_label] = "curr%d_label", |
| 656 | [hwmon_curr_alarm] = "curr%d_alarm", |
| 657 | [hwmon_curr_min_alarm] = "curr%d_min_alarm", |
| 658 | [hwmon_curr_max_alarm] = "curr%d_max_alarm", |
| 659 | [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm", |
| 660 | [hwmon_curr_crit_alarm] = "curr%d_crit_alarm", |
| 661 | [hwmon_curr_rated_min] = "curr%d_rated_min", |
| 662 | [hwmon_curr_rated_max] = "curr%d_rated_max", |
| 663 | [hwmon_curr_beep] = "curr%d_beep", |
| 664 | }; |
| 665 | |
| 666 | static const char * const hwmon_power_attr_templates[] = { |
| 667 | [hwmon_power_enable] = "power%d_enable", |
| 668 | [hwmon_power_average] = "power%d_average", |
| 669 | [hwmon_power_average_interval] = "power%d_average_interval", |
| 670 | [hwmon_power_average_interval_max] = "power%d_average_interval_max", |
| 671 | [hwmon_power_average_interval_min] = "power%d_average_interval_min", |
| 672 | [hwmon_power_average_highest] = "power%d_average_highest", |
| 673 | [hwmon_power_average_lowest] = "power%d_average_lowest", |
| 674 | [hwmon_power_average_max] = "power%d_average_max", |
| 675 | [hwmon_power_average_min] = "power%d_average_min", |
| 676 | [hwmon_power_input] = "power%d_input", |
| 677 | [hwmon_power_input_highest] = "power%d_input_highest", |
| 678 | [hwmon_power_input_lowest] = "power%d_input_lowest", |
| 679 | [hwmon_power_reset_history] = "power%d_reset_history", |
| 680 | [hwmon_power_accuracy] = "power%d_accuracy", |
| 681 | [hwmon_power_cap] = "power%d_cap", |
| 682 | [hwmon_power_cap_hyst] = "power%d_cap_hyst", |
| 683 | [hwmon_power_cap_max] = "power%d_cap_max", |
| 684 | [hwmon_power_cap_min] = "power%d_cap_min", |
| 685 | [hwmon_power_min] = "power%d_min", |
| 686 | [hwmon_power_max] = "power%d_max", |
| 687 | [hwmon_power_lcrit] = "power%d_lcrit", |
| 688 | [hwmon_power_crit] = "power%d_crit", |
| 689 | [hwmon_power_label] = "power%d_label", |
| 690 | [hwmon_power_alarm] = "power%d_alarm", |
| 691 | [hwmon_power_cap_alarm] = "power%d_cap_alarm", |
| 692 | [hwmon_power_min_alarm] = "power%d_min_alarm", |
| 693 | [hwmon_power_max_alarm] = "power%d_max_alarm", |
| 694 | [hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm", |
| 695 | [hwmon_power_crit_alarm] = "power%d_crit_alarm", |
| 696 | [hwmon_power_rated_min] = "power%d_rated_min", |
| 697 | [hwmon_power_rated_max] = "power%d_rated_max", |
| 698 | }; |
| 699 | |
| 700 | static const char * const hwmon_energy_attr_templates[] = { |
| 701 | [hwmon_energy_enable] = "energy%d_enable", |
| 702 | [hwmon_energy_input] = "energy%d_input", |
| 703 | [hwmon_energy_label] = "energy%d_label", |
| 704 | }; |
| 705 | |
| 706 | static const char * const hwmon_humidity_attr_templates[] = { |
| 707 | [hwmon_humidity_enable] = "humidity%d_enable", |
| 708 | [hwmon_humidity_input] = "humidity%d_input", |
| 709 | [hwmon_humidity_label] = "humidity%d_label", |
| 710 | [hwmon_humidity_min] = "humidity%d_min", |
| 711 | [hwmon_humidity_min_hyst] = "humidity%d_min_hyst", |
| 712 | [hwmon_humidity_max] = "humidity%d_max", |
| 713 | [hwmon_humidity_max_hyst] = "humidity%d_max_hyst", |
| 714 | [hwmon_humidity_alarm] = "humidity%d_alarm", |
| 715 | [hwmon_humidity_fault] = "humidity%d_fault", |
| 716 | [hwmon_humidity_rated_min] = "humidity%d_rated_min", |
| 717 | [hwmon_humidity_rated_max] = "humidity%d_rated_max", |
| 718 | [hwmon_humidity_min_alarm] = "humidity%d_min_alarm", |
| 719 | [hwmon_humidity_max_alarm] = "humidity%d_max_alarm", |
| 720 | }; |
| 721 | |
| 722 | static const char * const hwmon_fan_attr_templates[] = { |
| 723 | [hwmon_fan_enable] = "fan%d_enable", |
| 724 | [hwmon_fan_input] = "fan%d_input", |
| 725 | [hwmon_fan_label] = "fan%d_label", |
| 726 | [hwmon_fan_min] = "fan%d_min", |
| 727 | [hwmon_fan_max] = "fan%d_max", |
| 728 | [hwmon_fan_div] = "fan%d_div", |
| 729 | [hwmon_fan_pulses] = "fan%d_pulses", |
| 730 | [hwmon_fan_target] = "fan%d_target", |
| 731 | [hwmon_fan_alarm] = "fan%d_alarm", |
| 732 | [hwmon_fan_min_alarm] = "fan%d_min_alarm", |
| 733 | [hwmon_fan_max_alarm] = "fan%d_max_alarm", |
| 734 | [hwmon_fan_fault] = "fan%d_fault", |
| 735 | [hwmon_fan_beep] = "fan%d_beep", |
| 736 | }; |
| 737 | |
| 738 | static const char * const hwmon_pwm_attr_templates[] = { |
| 739 | [hwmon_pwm_input] = "pwm%d", |
| 740 | [hwmon_pwm_enable] = "pwm%d_enable", |
| 741 | [hwmon_pwm_mode] = "pwm%d_mode", |
| 742 | [hwmon_pwm_freq] = "pwm%d_freq", |
| 743 | [hwmon_pwm_auto_channels_temp] = "pwm%d_auto_channels_temp", |
| 744 | }; |
| 745 | |
| 746 | static const char * const hwmon_intrusion_attr_templates[] = { |
| 747 | [hwmon_intrusion_alarm] = "intrusion%d_alarm", |
| 748 | [hwmon_intrusion_beep] = "intrusion%d_beep", |
| 749 | }; |
| 750 | |
| 751 | static const char * const *__templates[] = { |
| 752 | [hwmon_chip] = hwmon_chip_attrs, |
| 753 | [hwmon_temp] = hwmon_temp_attr_templates, |
| 754 | [hwmon_in] = hwmon_in_attr_templates, |
| 755 | [hwmon_curr] = hwmon_curr_attr_templates, |
| 756 | [hwmon_power] = hwmon_power_attr_templates, |
| 757 | [hwmon_energy] = hwmon_energy_attr_templates, |
| 758 | [hwmon_energy64] = hwmon_energy_attr_templates, |
| 759 | [hwmon_humidity] = hwmon_humidity_attr_templates, |
| 760 | [hwmon_fan] = hwmon_fan_attr_templates, |
| 761 | [hwmon_pwm] = hwmon_pwm_attr_templates, |
| 762 | [hwmon_intrusion] = hwmon_intrusion_attr_templates, |
| 763 | }; |
| 764 | |
| 765 | static const int __templates_size[] = { |
| 766 | [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs), |
| 767 | [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates), |
| 768 | [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates), |
| 769 | [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates), |
| 770 | [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates), |
| 771 | [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates), |
| 772 | [hwmon_energy64] = ARRAY_SIZE(hwmon_energy_attr_templates), |
| 773 | [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates), |
| 774 | [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates), |
| 775 | [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates), |
| 776 | [hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates), |
| 777 | }; |
| 778 | |
| 779 | int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type, |
| 780 | u32 attr, int channel) |
| 781 | { |
| 782 | char event[MAX_SYSFS_ATTR_NAME_LENGTH + 5]; |
| 783 | char sattr[MAX_SYSFS_ATTR_NAME_LENGTH]; |
| 784 | char *envp[] = { event, NULL }; |
| 785 | const char * const *templates; |
| 786 | const char *template; |
| 787 | int base; |
| 788 | |
| 789 | if (WARN(!is_hwmon_device(dev), "%s is not a hardware monitoring device\n", |
| 790 | dev_name(dev))) |
| 791 | return -EINVAL; |
| 792 | if (type >= ARRAY_SIZE(__templates)) |
| 793 | return -EINVAL; |
| 794 | if (attr >= __templates_size[type]) |
| 795 | return -EINVAL; |
| 796 | |
| 797 | templates = __templates[type]; |
| 798 | template = templates[attr]; |
| 799 | |
| 800 | base = hwmon_attr_base(type); |
| 801 | |
| 802 | scnprintf(sattr, MAX_SYSFS_ATTR_NAME_LENGTH, template, base + channel); |
| 803 | scnprintf(event, sizeof(event), "NAME=%s", sattr); |
| 804 | sysfs_notify(&dev->kobj, NULL, sattr); |
| 805 | kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp); |
| 806 | |
| 807 | if (type == hwmon_temp) |
| 808 | hwmon_thermal_notify(dev, channel); |
| 809 | |
| 810 | return 0; |
| 811 | } |
| 812 | EXPORT_SYMBOL_GPL(hwmon_notify_event); |
| 813 | |
| 814 | void hwmon_lock(struct device *dev) |
| 815 | { |
| 816 | struct hwmon_device *hwdev = to_hwmon_device(dev); |
| 817 | |
| 818 | mutex_lock(&hwdev->lock); |
| 819 | } |
| 820 | EXPORT_SYMBOL_GPL(hwmon_lock); |
| 821 | |
| 822 | void hwmon_unlock(struct device *dev) |
| 823 | { |
| 824 | struct hwmon_device *hwdev = to_hwmon_device(dev); |
| 825 | |
| 826 | mutex_unlock(&hwdev->lock); |
| 827 | } |
| 828 | EXPORT_SYMBOL_GPL(hwmon_unlock); |
| 829 | |
| 830 | static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info) |
| 831 | { |
| 832 | int i, n; |
| 833 | |
| 834 | for (i = n = 0; info->config[i]; i++) |
| 835 | n += hweight32(info->config[i]); |
| 836 | |
| 837 | return n; |
| 838 | } |
| 839 | |
| 840 | static int hwmon_genattrs(const void *drvdata, |
| 841 | const struct attribute **attrs, |
| 842 | const struct hwmon_ops *ops, |
| 843 | const struct hwmon_channel_info *info) |
| 844 | { |
| 845 | const char * const *templates; |
| 846 | int template_size; |
| 847 | int i, aindex = 0; |
| 848 | |
| 849 | if (info->type >= ARRAY_SIZE(__templates)) |
| 850 | return -EINVAL; |
| 851 | |
| 852 | templates = __templates[info->type]; |
| 853 | template_size = __templates_size[info->type]; |
| 854 | |
| 855 | for (i = 0; info->config[i]; i++) { |
| 856 | u32 attr_mask = info->config[i]; |
| 857 | u32 attr; |
| 858 | |
| 859 | while (attr_mask) { |
| 860 | const struct attribute *a; |
| 861 | |
| 862 | attr = __ffs(attr_mask); |
| 863 | attr_mask &= ~BIT(attr); |
| 864 | if (attr >= template_size || !templates[attr]) |
| 865 | continue; /* attribute is invisible */ |
| 866 | a = hwmon_genattr(drvdata, info->type, attr, i, |
| 867 | templates[attr], ops); |
| 868 | if (IS_ERR(a)) { |
| 869 | if (PTR_ERR(a) != -ENOENT) |
| 870 | return PTR_ERR(a); |
| 871 | continue; |
| 872 | } |
| 873 | attrs[aindex++] = a; |
| 874 | } |
| 875 | } |
| 876 | return aindex; |
| 877 | } |
| 878 | |
| 879 | static const struct attribute ** |
| 880 | __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip) |
| 881 | { |
| 882 | int ret, i, aindex = 0, nattrs = 0; |
| 883 | const struct attribute **attrs; |
| 884 | |
| 885 | for (i = 0; chip->info[i]; i++) |
| 886 | nattrs += hwmon_num_channel_attrs(chip->info[i]); |
| 887 | |
| 888 | if (nattrs == 0) |
| 889 | return ERR_PTR(-EINVAL); |
| 890 | |
| 891 | attrs = kzalloc_objs(*attrs, nattrs + 1); |
| 892 | if (!attrs) |
| 893 | return ERR_PTR(-ENOMEM); |
| 894 | |
| 895 | for (i = 0; chip->info[i]; i++) { |
| 896 | ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops, |
| 897 | chip->info[i]); |
| 898 | if (ret < 0) { |
| 899 | hwmon_free_attrs(attrs); |
| 900 | return ERR_PTR(ret); |
| 901 | } |
| 902 | aindex += ret; |
| 903 | } |
| 904 | |
| 905 | return attrs; |
| 906 | } |
| 907 | |
| 908 | static struct device * |
| 909 | __hwmon_device_register(struct device *dev, const char *name, void *drvdata, |
| 910 | const struct hwmon_chip_info *chip, |
| 911 | const struct attribute_group **groups) |
| 912 | { |
| 913 | struct hwmon_device *hwdev; |
| 914 | const char *label; |
| 915 | struct device *hdev; |
| 916 | struct device *tdev = dev; |
| 917 | int i, err, id; |
| 918 | |
| 919 | /* Complain about invalid characters in hwmon name attribute */ |
| 920 | if (name && (!strlen(name) || strpbrk(name, "-* \t\n"))) |
| 921 | dev_warn(dev, |
| 922 | "hwmon: '%s' is not a valid name attribute, please fix\n", |
| 923 | name); |
| 924 | |
| 925 | id = ida_alloc(&hwmon_ida, GFP_KERNEL); |
| 926 | if (id < 0) |
| 927 | return ERR_PTR(id); |
| 928 | |
| 929 | hwdev = kzalloc_obj(*hwdev); |
| 930 | if (hwdev == NULL) { |
| 931 | err = -ENOMEM; |
| 932 | goto ida_remove; |
| 933 | } |
| 934 | |
| 935 | hdev = &hwdev->dev; |
| 936 | |
| 937 | if (chip) { |
| 938 | const struct attribute **attrs; |
| 939 | int ngroups = 2; /* terminating NULL plus &hwdev->groups */ |
| 940 | |
| 941 | if (groups) |
| 942 | for (i = 0; groups[i]; i++) |
| 943 | ngroups++; |
| 944 | |
| 945 | hwdev->groups = kzalloc_objs(*groups, ngroups); |
| 946 | if (!hwdev->groups) { |
| 947 | err = -ENOMEM; |
| 948 | goto free_hwmon; |
| 949 | } |
| 950 | |
| 951 | attrs = __hwmon_create_attrs(drvdata, chip); |
| 952 | if (IS_ERR(attrs)) { |
| 953 | err = PTR_ERR(attrs); |
| 954 | goto free_hwmon; |
| 955 | } |
| 956 | |
| 957 | hwdev->group.attrs_const = attrs; |
| 958 | ngroups = 0; |
| 959 | hwdev->groups[ngroups++] = &hwdev->group; |
| 960 | |
| 961 | if (groups) { |
| 962 | for (i = 0; groups[i]; i++) |
| 963 | hwdev->groups[ngroups++] = groups[i]; |
| 964 | } |
| 965 | |
| 966 | hdev->groups = hwdev->groups; |
| 967 | } else { |
| 968 | hdev->groups = groups; |
| 969 | } |
| 970 | |
| 971 | if (dev && device_property_present(dev, "label")) { |
| 972 | err = device_property_read_string(dev, "label", &label); |
| 973 | if (err < 0) |
| 974 | goto free_hwmon; |
| 975 | |
| 976 | hwdev->label = kstrdup(label, GFP_KERNEL); |
| 977 | if (hwdev->label == NULL) { |
| 978 | err = -ENOMEM; |
| 979 | goto free_hwmon; |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | hwdev->name = name; |
| 984 | hdev->class = &hwmon_class; |
| 985 | hdev->parent = dev; |
| 986 | while (tdev && !tdev->of_node) |
| 987 | tdev = tdev->parent; |
| 988 | hdev->of_node = tdev ? tdev->of_node : NULL; |
| 989 | hwdev->chip = chip; |
| 990 | mutex_init(&hwdev->lock); |
| 991 | dev_set_drvdata(hdev, drvdata); |
| 992 | dev_set_name(hdev, HWMON_ID_FORMAT, id); |
| 993 | err = device_register(hdev); |
| 994 | if (err) { |
| 995 | put_device(hdev); |
| 996 | goto ida_remove; |
| 997 | } |
| 998 | |
| 999 | INIT_LIST_HEAD(&hwdev->tzdata); |
| 1000 | |
| 1001 | if (hdev->of_node && chip && chip->ops->read && |
| 1002 | chip->info[0]->type == hwmon_chip) { |
| 1003 | u32 config = chip->info[0]->config[0]; |
| 1004 | |
| 1005 | if (config & HWMON_C_REGISTER_TZ) { |
| 1006 | err = hwmon_thermal_register_sensors(hdev); |
| 1007 | if (err) { |
| 1008 | device_unregister(hdev); |
| 1009 | /* |
| 1010 | * Don't worry about hwdev; hwmon_dev_release(), |
| 1011 | * called from device_unregister(), will free it. |
| 1012 | */ |
| 1013 | goto ida_remove; |
| 1014 | } |
| 1015 | } |
| 1016 | if (config & HWMON_C_PEC) { |
| 1017 | err = hwmon_pec_register(hdev); |
| 1018 | if (err) { |
| 1019 | device_unregister(hdev); |
| 1020 | goto ida_remove; |
| 1021 | } |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | return hdev; |
| 1026 | |
| 1027 | free_hwmon: |
| 1028 | hwmon_dev_release(hdev); |
| 1029 | ida_remove: |
| 1030 | ida_free(&hwmon_ida, id); |
| 1031 | return ERR_PTR(err); |
| 1032 | } |
| 1033 | |
| 1034 | /** |
| 1035 | * hwmon_device_register_with_groups - register w/ hwmon |
| 1036 | * @dev: the parent device |
| 1037 | * @name: hwmon name attribute |
| 1038 | * @drvdata: driver data to attach to created device |
| 1039 | * @groups: List of attribute groups to create |
| 1040 | * |
| 1041 | * hwmon_device_unregister() must be called when the device is no |
| 1042 | * longer needed. |
| 1043 | * |
| 1044 | * Returns the pointer to the new device. |
| 1045 | */ |
| 1046 | struct device * |
| 1047 | hwmon_device_register_with_groups(struct device *dev, const char *name, |
| 1048 | void *drvdata, |
| 1049 | const struct attribute_group **groups) |
| 1050 | { |
| 1051 | if (!name) |
| 1052 | return ERR_PTR(-EINVAL); |
| 1053 | |
| 1054 | return __hwmon_device_register(dev, name, drvdata, NULL, groups); |
| 1055 | } |
| 1056 | EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups); |
| 1057 | |
| 1058 | /** |
| 1059 | * hwmon_device_register_with_info - register w/ hwmon |
| 1060 | * @dev: the parent device (mandatory) |
| 1061 | * @name: hwmon name attribute (mandatory) |
| 1062 | * @drvdata: driver data to attach to created device (optional) |
| 1063 | * @chip: pointer to hwmon chip information (mandatory) |
| 1064 | * @extra_groups: pointer to list of additional non-standard attribute groups |
| 1065 | * (optional) |
| 1066 | * |
| 1067 | * hwmon_device_unregister() must be called when the device is no |
| 1068 | * longer needed. |
| 1069 | * |
| 1070 | * Returns the pointer to the new device. |
| 1071 | */ |
| 1072 | struct device * |
| 1073 | hwmon_device_register_with_info(struct device *dev, const char *name, |
| 1074 | void *drvdata, |
| 1075 | const struct hwmon_chip_info *chip, |
| 1076 | const struct attribute_group **extra_groups) |
| 1077 | { |
| 1078 | if (!dev || !name || !chip) |
| 1079 | return ERR_PTR(-EINVAL); |
| 1080 | |
| 1081 | if (!chip->ops || !(chip->ops->visible || chip->ops->is_visible) || !chip->info) |
| 1082 | return ERR_PTR(-EINVAL); |
| 1083 | |
| 1084 | return __hwmon_device_register(dev, name, drvdata, chip, extra_groups); |
| 1085 | } |
| 1086 | EXPORT_SYMBOL_GPL(hwmon_device_register_with_info); |
| 1087 | |
| 1088 | /** |
| 1089 | * hwmon_device_register_for_thermal - register hwmon device for thermal subsystem |
| 1090 | * @dev: the parent device |
| 1091 | * @name: hwmon name attribute |
| 1092 | * @drvdata: driver data to attach to created device |
| 1093 | * |
| 1094 | * The use of this function is restricted. It is provided for legacy reasons |
| 1095 | * and must only be called from the thermal subsystem. |
| 1096 | * |
| 1097 | * hwmon_device_unregister() must be called when the device is no |
| 1098 | * longer needed. |
| 1099 | * |
| 1100 | * Returns the pointer to the new device. |
| 1101 | */ |
| 1102 | struct device * |
| 1103 | hwmon_device_register_for_thermal(struct device *dev, const char *name, |
| 1104 | void *drvdata) |
| 1105 | { |
| 1106 | if (!name || !dev) |
| 1107 | return ERR_PTR(-EINVAL); |
| 1108 | |
| 1109 | return __hwmon_device_register(dev, name, drvdata, NULL, NULL); |
| 1110 | } |
| 1111 | EXPORT_SYMBOL_NS_GPL(hwmon_device_register_for_thermal, "HWMON_THERMAL"); |
| 1112 | |
| 1113 | /** |
| 1114 | * hwmon_device_register - register w/ hwmon |
| 1115 | * @dev: the device to register |
| 1116 | * |
| 1117 | * hwmon_device_unregister() must be called when the device is no |
| 1118 | * longer needed. |
| 1119 | * |
| 1120 | * Returns the pointer to the new device. |
| 1121 | */ |
| 1122 | struct device *hwmon_device_register(struct device *dev) |
| 1123 | { |
| 1124 | dev_warn(dev, |
| 1125 | "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n"); |
| 1126 | |
| 1127 | return __hwmon_device_register(dev, NULL, NULL, NULL, NULL); |
| 1128 | } |
| 1129 | EXPORT_SYMBOL_GPL(hwmon_device_register); |
| 1130 | |
| 1131 | /** |
| 1132 | * hwmon_device_unregister - removes the previously registered class device |
| 1133 | * |
| 1134 | * @dev: the class device to destroy |
| 1135 | */ |
| 1136 | void hwmon_device_unregister(struct device *dev) |
| 1137 | { |
| 1138 | int id; |
| 1139 | |
| 1140 | if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) { |
| 1141 | device_unregister(dev); |
| 1142 | ida_free(&hwmon_ida, id); |
| 1143 | } else |
| 1144 | dev_dbg(dev->parent, |
| 1145 | "hwmon_device_unregister() failed: bad class ID!\n"); |
| 1146 | } |
| 1147 | EXPORT_SYMBOL_GPL(hwmon_device_unregister); |
| 1148 | |
| 1149 | static void devm_hwmon_release(struct device *dev, void *res) |
| 1150 | { |
| 1151 | struct device *hwdev = *(struct device **)res; |
| 1152 | |
| 1153 | hwmon_device_unregister(hwdev); |
| 1154 | } |
| 1155 | |
| 1156 | /** |
| 1157 | * devm_hwmon_device_register_with_groups - register w/ hwmon |
| 1158 | * @dev: the parent device |
| 1159 | * @name: hwmon name attribute |
| 1160 | * @drvdata: driver data to attach to created device |
| 1161 | * @groups: List of attribute groups to create |
| 1162 | * |
| 1163 | * Returns the pointer to the new device. The new device is automatically |
| 1164 | * unregistered with the parent device. |
| 1165 | */ |
| 1166 | struct device * |
| 1167 | devm_hwmon_device_register_with_groups(struct device *dev, const char *name, |
| 1168 | void *drvdata, |
| 1169 | const struct attribute_group **groups) |
| 1170 | { |
| 1171 | struct device **ptr, *hwdev; |
| 1172 | |
| 1173 | if (!dev) |
| 1174 | return ERR_PTR(-EINVAL); |
| 1175 | |
| 1176 | ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); |
| 1177 | if (!ptr) |
| 1178 | return ERR_PTR(-ENOMEM); |
| 1179 | |
| 1180 | hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups); |
| 1181 | if (IS_ERR(hwdev)) |
| 1182 | goto error; |
| 1183 | |
| 1184 | *ptr = hwdev; |
| 1185 | devres_add(dev, ptr); |
| 1186 | return hwdev; |
| 1187 | |
| 1188 | error: |
| 1189 | devres_free(ptr); |
| 1190 | return hwdev; |
| 1191 | } |
| 1192 | EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups); |
| 1193 | |
| 1194 | /** |
| 1195 | * devm_hwmon_device_register_with_info - register w/ hwmon |
| 1196 | * @dev: the parent device |
| 1197 | * @name: hwmon name attribute |
| 1198 | * @drvdata: driver data to attach to created device |
| 1199 | * @chip: pointer to hwmon chip information |
| 1200 | * @extra_groups: pointer to list of driver specific attribute groups |
| 1201 | * |
| 1202 | * Returns the pointer to the new device. The new device is automatically |
| 1203 | * unregistered with the parent device. |
| 1204 | */ |
| 1205 | struct device * |
| 1206 | devm_hwmon_device_register_with_info(struct device *dev, const char *name, |
| 1207 | void *drvdata, |
| 1208 | const struct hwmon_chip_info *chip, |
| 1209 | const struct attribute_group **extra_groups) |
| 1210 | { |
| 1211 | struct device **ptr, *hwdev; |
| 1212 | |
| 1213 | if (!dev) |
| 1214 | return ERR_PTR(-EINVAL); |
| 1215 | |
| 1216 | if (!name) { |
| 1217 | name = devm_hwmon_sanitize_name(dev, dev_name(dev)); |
| 1218 | if (IS_ERR(name)) |
| 1219 | return ERR_CAST(name); |
| 1220 | } |
| 1221 | |
| 1222 | ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); |
| 1223 | if (!ptr) |
| 1224 | return ERR_PTR(-ENOMEM); |
| 1225 | |
| 1226 | hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip, |
| 1227 | extra_groups); |
| 1228 | if (IS_ERR(hwdev)) |
| 1229 | goto error; |
| 1230 | |
| 1231 | *ptr = hwdev; |
| 1232 | devres_add(dev, ptr); |
| 1233 | |
| 1234 | return hwdev; |
| 1235 | |
| 1236 | error: |
| 1237 | devres_free(ptr); |
| 1238 | return hwdev; |
| 1239 | } |
| 1240 | EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info); |
| 1241 | |
| 1242 | static char *__hwmon_sanitize_name(struct device *dev, const char *old_name) |
| 1243 | { |
| 1244 | char *name, *p; |
| 1245 | |
| 1246 | if (dev) |
| 1247 | name = devm_kstrdup(dev, old_name, GFP_KERNEL); |
| 1248 | else |
| 1249 | name = kstrdup(old_name, GFP_KERNEL); |
| 1250 | if (!name) |
| 1251 | return ERR_PTR(-ENOMEM); |
| 1252 | |
| 1253 | for (p = name; *p; p++) |
| 1254 | if (hwmon_is_bad_char(*p)) |
| 1255 | *p = '_'; |
| 1256 | |
| 1257 | return name; |
| 1258 | } |
| 1259 | |
| 1260 | /** |
| 1261 | * hwmon_sanitize_name - Replaces invalid characters in a hwmon name |
| 1262 | * @name: NUL-terminated name |
| 1263 | * |
| 1264 | * Allocates a new string where any invalid characters will be replaced |
| 1265 | * by an underscore. It is the responsibility of the caller to release |
| 1266 | * the memory. |
| 1267 | * |
| 1268 | * Returns newly allocated name, or ERR_PTR on error. |
| 1269 | */ |
| 1270 | char *hwmon_sanitize_name(const char *name) |
| 1271 | { |
| 1272 | if (!name) |
| 1273 | return ERR_PTR(-EINVAL); |
| 1274 | |
| 1275 | return __hwmon_sanitize_name(NULL, name); |
| 1276 | } |
| 1277 | EXPORT_SYMBOL_GPL(hwmon_sanitize_name); |
| 1278 | |
| 1279 | /** |
| 1280 | * devm_hwmon_sanitize_name - resource managed hwmon_sanitize_name() |
| 1281 | * @dev: device to allocate memory for |
| 1282 | * @name: NUL-terminated name |
| 1283 | * |
| 1284 | * Allocates a new string where any invalid characters will be replaced |
| 1285 | * by an underscore. |
| 1286 | * |
| 1287 | * Returns newly allocated name, or ERR_PTR on error. |
| 1288 | */ |
| 1289 | char *devm_hwmon_sanitize_name(struct device *dev, const char *name) |
| 1290 | { |
| 1291 | if (!dev || !name) |
| 1292 | return ERR_PTR(-EINVAL); |
| 1293 | |
| 1294 | return __hwmon_sanitize_name(dev, name); |
| 1295 | } |
| 1296 | EXPORT_SYMBOL_GPL(devm_hwmon_sanitize_name); |
| 1297 | |
| 1298 | static void __init hwmon_pci_quirks(void) |
| 1299 | { |
| 1300 | #if defined CONFIG_X86 && defined CONFIG_PCI |
| 1301 | struct pci_dev *sb; |
| 1302 | u16 base; |
| 1303 | u8 enable; |
| 1304 | |
| 1305 | /* Open access to 0x295-0x296 on MSI MS-7031 */ |
| 1306 | sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL); |
| 1307 | if (sb) { |
| 1308 | if (sb->subsystem_vendor == 0x1462 && /* MSI */ |
| 1309 | sb->subsystem_device == 0x0031) { /* MS-7031 */ |
| 1310 | pci_read_config_byte(sb, 0x48, &enable); |
| 1311 | pci_read_config_word(sb, 0x64, &base); |
| 1312 | |
| 1313 | if (base == 0 && !(enable & BIT(2))) { |
| 1314 | dev_info(&sb->dev, |
| 1315 | "Opening wide generic port at 0x295\n"); |
| 1316 | pci_write_config_word(sb, 0x64, 0x295); |
| 1317 | pci_write_config_byte(sb, 0x48, |
| 1318 | enable | BIT(2)); |
| 1319 | } |
| 1320 | } |
| 1321 | pci_dev_put(sb); |
| 1322 | } |
| 1323 | #endif |
| 1324 | } |
| 1325 | |
| 1326 | static int __init hwmon_init(void) |
| 1327 | { |
| 1328 | int err; |
| 1329 | |
| 1330 | hwmon_pci_quirks(); |
| 1331 | |
| 1332 | err = class_register(&hwmon_class); |
| 1333 | if (err) { |
| 1334 | pr_err("couldn't register hwmon sysfs class\n"); |
| 1335 | return err; |
| 1336 | } |
| 1337 | return 0; |
| 1338 | } |
| 1339 | |
| 1340 | static void __exit hwmon_exit(void) |
| 1341 | { |
| 1342 | class_unregister(&hwmon_class); |
| 1343 | } |
| 1344 | |
| 1345 | subsys_initcall(hwmon_init); |
| 1346 | module_exit(hwmon_exit); |
| 1347 | |
| 1348 | MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>"); |
| 1349 | MODULE_DESCRIPTION("hardware monitoring sysfs/class support"); |
| 1350 | MODULE_LICENSE("GPL"); |