1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21236441fSMark M. Hoffman /* 35ed04880SGuenter Roeck * hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring 45ed04880SGuenter Roeck * 55ed04880SGuenter Roeck * This file defines the sysfs class "hwmon", for use by sensors drivers. 65ed04880SGuenter Roeck * 75ed04880SGuenter Roeck * Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com> 81236441fSMark M. Hoffman */ 91236441fSMark M. Hoffman 10c95df1aeSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 11c95df1aeSJoe Perches 12d560168bSGuenter Roeck #include <linux/bitops.h> 131236441fSMark M. Hoffman #include <linux/device.h> 141236441fSMark M. Hoffman #include <linux/err.h> 158c65b4a6STim Schmielau #include <linux/gfp.h> 16c9ebbe6fSGuenter Roeck #include <linux/hwmon.h> 17c9ebbe6fSGuenter Roeck #include <linux/idr.h> 181597b374SGuenter Roeck #include <linux/list.h> 19c9ebbe6fSGuenter Roeck #include <linux/module.h> 202958b1ecSJean Delvare #include <linux/pci.h> 21c9ebbe6fSGuenter Roeck #include <linux/slab.h> 22648cd48cSGuenter Roeck #include <linux/string.h> 23d560168bSGuenter Roeck #include <linux/thermal.h> 241236441fSMark M. Hoffman 2561b8ab2cSNicolin Chen #define CREATE_TRACE_POINTS 2661b8ab2cSNicolin Chen #include <trace/events/hwmon.h> 2761b8ab2cSNicolin Chen 281236441fSMark M. Hoffman #define HWMON_ID_PREFIX "hwmon" 291236441fSMark M. Hoffman #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d" 301236441fSMark M. Hoffman 31bab2243cSGuenter Roeck struct hwmon_device { 32bab2243cSGuenter Roeck const char *name; 33bab2243cSGuenter Roeck struct device dev; 34d560168bSGuenter Roeck const struct hwmon_chip_info *chip; 351597b374SGuenter Roeck struct list_head tzdata; 36d560168bSGuenter Roeck struct attribute_group group; 37d560168bSGuenter Roeck const struct attribute_group **groups; 38bab2243cSGuenter Roeck }; 39d560168bSGuenter Roeck 40bab2243cSGuenter Roeck #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev) 41bab2243cSGuenter Roeck 423a412d5eSGuenter Roeck #define MAX_SYSFS_ATTR_NAME_LENGTH 32 433a412d5eSGuenter Roeck 44d560168bSGuenter Roeck struct hwmon_device_attribute { 45d560168bSGuenter Roeck struct device_attribute dev_attr; 46d560168bSGuenter Roeck const struct hwmon_ops *ops; 47d560168bSGuenter Roeck enum hwmon_sensor_types type; 48d560168bSGuenter Roeck u32 attr; 49d560168bSGuenter Roeck int index; 503a412d5eSGuenter Roeck char name[MAX_SYSFS_ATTR_NAME_LENGTH]; 51d560168bSGuenter Roeck }; 52d560168bSGuenter Roeck 53d560168bSGuenter Roeck #define to_hwmon_attr(d) \ 54d560168bSGuenter Roeck container_of(d, struct hwmon_device_attribute, dev_attr) 553bf8bdcfSGuenter Roeck #define to_dev_attr(a) container_of(a, struct device_attribute, attr) 56d560168bSGuenter Roeck 57d560168bSGuenter Roeck /* 58d560168bSGuenter Roeck * Thermal zone information 59d560168bSGuenter Roeck */ 60d560168bSGuenter Roeck struct hwmon_thermal_data { 611597b374SGuenter Roeck struct list_head node; /* hwmon tzdata list entry */ 623bf8bdcfSGuenter Roeck struct device *dev; /* Reference to hwmon device */ 63d560168bSGuenter Roeck int index; /* sensor index */ 641597b374SGuenter Roeck struct thermal_zone_device *tzd;/* thermal zone device */ 65d560168bSGuenter Roeck }; 66d560168bSGuenter Roeck 67bab2243cSGuenter Roeck static ssize_t 682ab0c6c5SJulia Lawall name_show(struct device *dev, struct device_attribute *attr, char *buf) 69bab2243cSGuenter Roeck { 70bab2243cSGuenter Roeck return sprintf(buf, "%s\n", to_hwmon_device(dev)->name); 71bab2243cSGuenter Roeck } 722ab0c6c5SJulia Lawall static DEVICE_ATTR_RO(name); 73bab2243cSGuenter Roeck 74bab2243cSGuenter Roeck static struct attribute *hwmon_dev_attrs[] = { 75bab2243cSGuenter Roeck &dev_attr_name.attr, 76bab2243cSGuenter Roeck NULL 77bab2243cSGuenter Roeck }; 78bab2243cSGuenter Roeck 79bab2243cSGuenter Roeck static umode_t hwmon_dev_name_is_visible(struct kobject *kobj, 80bab2243cSGuenter Roeck struct attribute *attr, int n) 81bab2243cSGuenter Roeck { 82bab2243cSGuenter Roeck struct device *dev = container_of(kobj, struct device, kobj); 83bab2243cSGuenter Roeck 84bab2243cSGuenter Roeck if (to_hwmon_device(dev)->name == NULL) 85bab2243cSGuenter Roeck return 0; 86bab2243cSGuenter Roeck 87bab2243cSGuenter Roeck return attr->mode; 88bab2243cSGuenter Roeck } 89bab2243cSGuenter Roeck 90524703acSArvind Yadav static const struct attribute_group hwmon_dev_attr_group = { 91bab2243cSGuenter Roeck .attrs = hwmon_dev_attrs, 92bab2243cSGuenter Roeck .is_visible = hwmon_dev_name_is_visible, 93bab2243cSGuenter Roeck }; 94bab2243cSGuenter Roeck 95bab2243cSGuenter Roeck static const struct attribute_group *hwmon_dev_attr_groups[] = { 96bab2243cSGuenter Roeck &hwmon_dev_attr_group, 97bab2243cSGuenter Roeck NULL 98bab2243cSGuenter Roeck }; 99bab2243cSGuenter Roeck 1003bf8bdcfSGuenter Roeck static void hwmon_free_attrs(struct attribute **attrs) 1013bf8bdcfSGuenter Roeck { 1023bf8bdcfSGuenter Roeck int i; 1033bf8bdcfSGuenter Roeck 1043bf8bdcfSGuenter Roeck for (i = 0; attrs[i]; i++) { 1053bf8bdcfSGuenter Roeck struct device_attribute *dattr = to_dev_attr(attrs[i]); 1063bf8bdcfSGuenter Roeck struct hwmon_device_attribute *hattr = to_hwmon_attr(dattr); 1073bf8bdcfSGuenter Roeck 1083bf8bdcfSGuenter Roeck kfree(hattr); 1093bf8bdcfSGuenter Roeck } 1103bf8bdcfSGuenter Roeck kfree(attrs); 1113bf8bdcfSGuenter Roeck } 1123bf8bdcfSGuenter Roeck 113bab2243cSGuenter Roeck static void hwmon_dev_release(struct device *dev) 114bab2243cSGuenter Roeck { 1153bf8bdcfSGuenter Roeck struct hwmon_device *hwdev = to_hwmon_device(dev); 1163bf8bdcfSGuenter Roeck 1173bf8bdcfSGuenter Roeck if (hwdev->group.attrs) 1183bf8bdcfSGuenter Roeck hwmon_free_attrs(hwdev->group.attrs); 1193bf8bdcfSGuenter Roeck kfree(hwdev->groups); 1203bf8bdcfSGuenter Roeck kfree(hwdev); 121bab2243cSGuenter Roeck } 122bab2243cSGuenter Roeck 123bab2243cSGuenter Roeck static struct class hwmon_class = { 124bab2243cSGuenter Roeck .name = "hwmon", 125bab2243cSGuenter Roeck .owner = THIS_MODULE, 126bab2243cSGuenter Roeck .dev_groups = hwmon_dev_attr_groups, 127bab2243cSGuenter Roeck .dev_release = hwmon_dev_release, 128bab2243cSGuenter Roeck }; 1291236441fSMark M. Hoffman 1304ca5f468SJonathan Cameron static DEFINE_IDA(hwmon_ida); 1311236441fSMark M. Hoffman 132d560168bSGuenter Roeck /* Thermal zone handling */ 133d560168bSGuenter Roeck 13486430c1aSGuenter Roeck /* 13586430c1aSGuenter Roeck * The complex conditional is necessary to avoid a cyclic dependency 13686430c1aSGuenter Roeck * between hwmon and thermal_sys modules. 13786430c1aSGuenter Roeck */ 138f3735332SDaniel Lezcano #ifdef CONFIG_THERMAL_OF 139d560168bSGuenter Roeck static int hwmon_thermal_get_temp(void *data, int *temp) 140d560168bSGuenter Roeck { 141d560168bSGuenter Roeck struct hwmon_thermal_data *tdata = data; 1423bf8bdcfSGuenter Roeck struct hwmon_device *hwdev = to_hwmon_device(tdata->dev); 143d560168bSGuenter Roeck int ret; 144d560168bSGuenter Roeck long t; 145d560168bSGuenter Roeck 1463bf8bdcfSGuenter Roeck ret = hwdev->chip->ops->read(tdata->dev, hwmon_temp, hwmon_temp_input, 147d560168bSGuenter Roeck tdata->index, &t); 148d560168bSGuenter Roeck if (ret < 0) 149d560168bSGuenter Roeck return ret; 150d560168bSGuenter Roeck 151d560168bSGuenter Roeck *temp = t; 152d560168bSGuenter Roeck 153d560168bSGuenter Roeck return 0; 154d560168bSGuenter Roeck } 155d560168bSGuenter Roeck 156c9920650SJulia Lawall static const struct thermal_zone_of_device_ops hwmon_thermal_ops = { 157d560168bSGuenter Roeck .get_temp = hwmon_thermal_get_temp, 158d560168bSGuenter Roeck }; 159d560168bSGuenter Roeck 1601597b374SGuenter Roeck static void hwmon_thermal_remove_sensor(void *data) 1611597b374SGuenter Roeck { 1621597b374SGuenter Roeck list_del(data); 1631597b374SGuenter Roeck } 1641597b374SGuenter Roeck 1653bf8bdcfSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev, int index) 166d560168bSGuenter Roeck { 1671597b374SGuenter Roeck struct hwmon_device *hwdev = to_hwmon_device(dev); 168d560168bSGuenter Roeck struct hwmon_thermal_data *tdata; 16947c332deSLinus Walleij struct thermal_zone_device *tzd; 1701597b374SGuenter Roeck int err; 171d560168bSGuenter Roeck 172d560168bSGuenter Roeck tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL); 173d560168bSGuenter Roeck if (!tdata) 174d560168bSGuenter Roeck return -ENOMEM; 175d560168bSGuenter Roeck 1763bf8bdcfSGuenter Roeck tdata->dev = dev; 177d560168bSGuenter Roeck tdata->index = index; 178d560168bSGuenter Roeck 1793bf8bdcfSGuenter Roeck tzd = devm_thermal_zone_of_sensor_register(dev, index, tdata, 180d560168bSGuenter Roeck &hwmon_thermal_ops); 18147c332deSLinus Walleij /* 18247c332deSLinus Walleij * If CONFIG_THERMAL_OF is disabled, this returns -ENODEV, 18347c332deSLinus Walleij * so ignore that error but forward any other error. 18447c332deSLinus Walleij */ 18547c332deSLinus Walleij if (IS_ERR(tzd) && (PTR_ERR(tzd) != -ENODEV)) 18647c332deSLinus Walleij return PTR_ERR(tzd); 187d560168bSGuenter Roeck 1881597b374SGuenter Roeck err = devm_add_action(dev, hwmon_thermal_remove_sensor, &tdata->node); 1891597b374SGuenter Roeck if (err) 1901597b374SGuenter Roeck return err; 1911597b374SGuenter Roeck 1921597b374SGuenter Roeck tdata->tzd = tzd; 1931597b374SGuenter Roeck list_add(&tdata->node, &hwdev->tzdata); 1941597b374SGuenter Roeck 195d560168bSGuenter Roeck return 0; 196d560168bSGuenter Roeck } 19744e3ad88SAkinobu Mita 19844e3ad88SAkinobu Mita static int hwmon_thermal_register_sensors(struct device *dev) 19944e3ad88SAkinobu Mita { 20044e3ad88SAkinobu Mita struct hwmon_device *hwdev = to_hwmon_device(dev); 20144e3ad88SAkinobu Mita const struct hwmon_chip_info *chip = hwdev->chip; 20244e3ad88SAkinobu Mita const struct hwmon_channel_info **info = chip->info; 20344e3ad88SAkinobu Mita void *drvdata = dev_get_drvdata(dev); 20444e3ad88SAkinobu Mita int i; 20544e3ad88SAkinobu Mita 20644e3ad88SAkinobu Mita for (i = 1; info[i]; i++) { 20744e3ad88SAkinobu Mita int j; 20844e3ad88SAkinobu Mita 20944e3ad88SAkinobu Mita if (info[i]->type != hwmon_temp) 21044e3ad88SAkinobu Mita continue; 21144e3ad88SAkinobu Mita 21244e3ad88SAkinobu Mita for (j = 0; info[i]->config[j]; j++) { 21344e3ad88SAkinobu Mita int err; 21444e3ad88SAkinobu Mita 21544e3ad88SAkinobu Mita if (!(info[i]->config[j] & HWMON_T_INPUT) || 21644e3ad88SAkinobu Mita !chip->ops->is_visible(drvdata, hwmon_temp, 21744e3ad88SAkinobu Mita hwmon_temp_input, j)) 21844e3ad88SAkinobu Mita continue; 21944e3ad88SAkinobu Mita 22044e3ad88SAkinobu Mita err = hwmon_thermal_add_sensor(dev, j); 22144e3ad88SAkinobu Mita if (err) 22244e3ad88SAkinobu Mita return err; 22344e3ad88SAkinobu Mita } 22444e3ad88SAkinobu Mita } 22544e3ad88SAkinobu Mita 22644e3ad88SAkinobu Mita return 0; 22744e3ad88SAkinobu Mita } 22844e3ad88SAkinobu Mita 2291597b374SGuenter Roeck static void hwmon_thermal_notify(struct device *dev, int index) 2301597b374SGuenter Roeck { 2311597b374SGuenter Roeck struct hwmon_device *hwdev = to_hwmon_device(dev); 2321597b374SGuenter Roeck struct hwmon_thermal_data *tzdata; 2331597b374SGuenter Roeck 2341597b374SGuenter Roeck list_for_each_entry(tzdata, &hwdev->tzdata, node) { 2351597b374SGuenter Roeck if (tzdata->index == index) { 2361597b374SGuenter Roeck thermal_zone_device_update(tzdata->tzd, 2371597b374SGuenter Roeck THERMAL_EVENT_UNSPECIFIED); 2381597b374SGuenter Roeck } 2391597b374SGuenter Roeck } 2401597b374SGuenter Roeck } 2411597b374SGuenter Roeck 242d560168bSGuenter Roeck #else 24344e3ad88SAkinobu Mita static int hwmon_thermal_register_sensors(struct device *dev) 244d560168bSGuenter Roeck { 245d560168bSGuenter Roeck return 0; 246d560168bSGuenter Roeck } 2471597b374SGuenter Roeck 2481597b374SGuenter Roeck static void hwmon_thermal_notify(struct device *dev, int index) { } 2491597b374SGuenter Roeck 25086430c1aSGuenter Roeck #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */ 251d560168bSGuenter Roeck 25261b8ab2cSNicolin Chen static int hwmon_attr_base(enum hwmon_sensor_types type) 25361b8ab2cSNicolin Chen { 2544413405fSDr. David Alan Gilbert if (type == hwmon_in || type == hwmon_intrusion) 25561b8ab2cSNicolin Chen return 0; 25661b8ab2cSNicolin Chen return 1; 25761b8ab2cSNicolin Chen } 25861b8ab2cSNicolin Chen 259d560168bSGuenter Roeck /* sysfs attribute management */ 260d560168bSGuenter Roeck 261d560168bSGuenter Roeck static ssize_t hwmon_attr_show(struct device *dev, 262d560168bSGuenter Roeck struct device_attribute *devattr, char *buf) 263d560168bSGuenter Roeck { 264d560168bSGuenter Roeck struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); 265d560168bSGuenter Roeck long val; 266d560168bSGuenter Roeck int ret; 267d560168bSGuenter Roeck 268d560168bSGuenter Roeck ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index, 269d560168bSGuenter Roeck &val); 270d560168bSGuenter Roeck if (ret < 0) 271d560168bSGuenter Roeck return ret; 272d560168bSGuenter Roeck 27361b8ab2cSNicolin Chen trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type), 27461b8ab2cSNicolin Chen hattr->name, val); 27561b8ab2cSNicolin Chen 276d560168bSGuenter Roeck return sprintf(buf, "%ld\n", val); 277d560168bSGuenter Roeck } 278d560168bSGuenter Roeck 279e159ab5cSGuenter Roeck static ssize_t hwmon_attr_show_string(struct device *dev, 280e159ab5cSGuenter Roeck struct device_attribute *devattr, 281e159ab5cSGuenter Roeck char *buf) 282e159ab5cSGuenter Roeck { 283e159ab5cSGuenter Roeck struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); 28461b8ab2cSNicolin Chen enum hwmon_sensor_types type = hattr->type; 2855ba6bcbcSJean Delvare const char *s; 286e159ab5cSGuenter Roeck int ret; 287e159ab5cSGuenter Roeck 288e159ab5cSGuenter Roeck ret = hattr->ops->read_string(dev, hattr->type, hattr->attr, 289e159ab5cSGuenter Roeck hattr->index, &s); 290e159ab5cSGuenter Roeck if (ret < 0) 291e159ab5cSGuenter Roeck return ret; 292e159ab5cSGuenter Roeck 29361b8ab2cSNicolin Chen trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type), 29461b8ab2cSNicolin Chen hattr->name, s); 29561b8ab2cSNicolin Chen 296e159ab5cSGuenter Roeck return sprintf(buf, "%s\n", s); 297e159ab5cSGuenter Roeck } 298e159ab5cSGuenter Roeck 299d560168bSGuenter Roeck static ssize_t hwmon_attr_store(struct device *dev, 300d560168bSGuenter Roeck struct device_attribute *devattr, 301d560168bSGuenter Roeck const char *buf, size_t count) 302d560168bSGuenter Roeck { 303d560168bSGuenter Roeck struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); 304d560168bSGuenter Roeck long val; 305d560168bSGuenter Roeck int ret; 306d560168bSGuenter Roeck 307d560168bSGuenter Roeck ret = kstrtol(buf, 10, &val); 308d560168bSGuenter Roeck if (ret < 0) 309d560168bSGuenter Roeck return ret; 310d560168bSGuenter Roeck 311d560168bSGuenter Roeck ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index, 312d560168bSGuenter Roeck val); 313d560168bSGuenter Roeck if (ret < 0) 314d560168bSGuenter Roeck return ret; 315d560168bSGuenter Roeck 31661b8ab2cSNicolin Chen trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type), 31761b8ab2cSNicolin Chen hattr->name, val); 318d560168bSGuenter Roeck 31961b8ab2cSNicolin Chen return count; 320d560168bSGuenter Roeck } 321d560168bSGuenter Roeck 322e159ab5cSGuenter Roeck static bool is_string_attr(enum hwmon_sensor_types type, u32 attr) 323e159ab5cSGuenter Roeck { 324e159ab5cSGuenter Roeck return (type == hwmon_temp && attr == hwmon_temp_label) || 325e159ab5cSGuenter Roeck (type == hwmon_in && attr == hwmon_in_label) || 326e159ab5cSGuenter Roeck (type == hwmon_curr && attr == hwmon_curr_label) || 327e159ab5cSGuenter Roeck (type == hwmon_power && attr == hwmon_power_label) || 328e159ab5cSGuenter Roeck (type == hwmon_energy && attr == hwmon_energy_label) || 329e159ab5cSGuenter Roeck (type == hwmon_humidity && attr == hwmon_humidity_label) || 330e159ab5cSGuenter Roeck (type == hwmon_fan && attr == hwmon_fan_label); 331e159ab5cSGuenter Roeck } 332e159ab5cSGuenter Roeck 3333bf8bdcfSGuenter Roeck static struct attribute *hwmon_genattr(const void *drvdata, 334d560168bSGuenter Roeck enum hwmon_sensor_types type, 335d560168bSGuenter Roeck u32 attr, 336d560168bSGuenter Roeck int index, 337d560168bSGuenter Roeck const char *template, 338d560168bSGuenter Roeck const struct hwmon_ops *ops) 339d560168bSGuenter Roeck { 340d560168bSGuenter Roeck struct hwmon_device_attribute *hattr; 341d560168bSGuenter Roeck struct device_attribute *dattr; 342d560168bSGuenter Roeck struct attribute *a; 343d560168bSGuenter Roeck umode_t mode; 3443b443defSRasmus Villemoes const char *name; 345e159ab5cSGuenter Roeck bool is_string = is_string_attr(type, attr); 346d560168bSGuenter Roeck 347d560168bSGuenter Roeck /* The attribute is invisible if there is no template string */ 348d560168bSGuenter Roeck if (!template) 349d560168bSGuenter Roeck return ERR_PTR(-ENOENT); 350d560168bSGuenter Roeck 351d560168bSGuenter Roeck mode = ops->is_visible(drvdata, type, attr, index); 352d560168bSGuenter Roeck if (!mode) 353d560168bSGuenter Roeck return ERR_PTR(-ENOENT); 354d560168bSGuenter Roeck 3550d87116fSGuenter Roeck if ((mode & 0444) && ((is_string && !ops->read_string) || 356e159ab5cSGuenter Roeck (!is_string && !ops->read))) 357d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 3580d87116fSGuenter Roeck if ((mode & 0222) && !ops->write) 359d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 360d560168bSGuenter Roeck 3613bf8bdcfSGuenter Roeck hattr = kzalloc(sizeof(*hattr), GFP_KERNEL); 362d560168bSGuenter Roeck if (!hattr) 363d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 364d560168bSGuenter Roeck 3653a412d5eSGuenter Roeck if (type == hwmon_chip) { 3663b443defSRasmus Villemoes name = template; 3673a412d5eSGuenter Roeck } else { 3683a412d5eSGuenter Roeck scnprintf(hattr->name, sizeof(hattr->name), template, 3693a412d5eSGuenter Roeck index + hwmon_attr_base(type)); 3703a412d5eSGuenter Roeck name = hattr->name; 3713a412d5eSGuenter Roeck } 3723a412d5eSGuenter Roeck 373d560168bSGuenter Roeck hattr->type = type; 374d560168bSGuenter Roeck hattr->attr = attr; 375d560168bSGuenter Roeck hattr->index = index; 376d560168bSGuenter Roeck hattr->ops = ops; 377d560168bSGuenter Roeck 378d560168bSGuenter Roeck dattr = &hattr->dev_attr; 379e159ab5cSGuenter Roeck dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show; 380d560168bSGuenter Roeck dattr->store = hwmon_attr_store; 381d560168bSGuenter Roeck 382d560168bSGuenter Roeck a = &dattr->attr; 383d560168bSGuenter Roeck sysfs_attr_init(a); 384d560168bSGuenter Roeck a->name = name; 385d560168bSGuenter Roeck a->mode = mode; 386d560168bSGuenter Roeck 387d560168bSGuenter Roeck return a; 388d560168bSGuenter Roeck } 389d560168bSGuenter Roeck 390f4d325d5SGuenter Roeck /* 391f4d325d5SGuenter Roeck * Chip attributes are not attribute templates but actual sysfs attributes. 392f4d325d5SGuenter Roeck * See hwmon_genattr() for special handling. 393f4d325d5SGuenter Roeck */ 394f4d325d5SGuenter Roeck static const char * const hwmon_chip_attrs[] = { 395d560168bSGuenter Roeck [hwmon_chip_temp_reset_history] = "temp_reset_history", 39600d616cfSGuenter Roeck [hwmon_chip_in_reset_history] = "in_reset_history", 3979b26947cSGuenter Roeck [hwmon_chip_curr_reset_history] = "curr_reset_history", 398b308f5c7SGuenter Roeck [hwmon_chip_power_reset_history] = "power_reset_history", 399d560168bSGuenter Roeck [hwmon_chip_update_interval] = "update_interval", 400d560168bSGuenter Roeck [hwmon_chip_alarms] = "alarms", 4019f00995eSGuenter Roeck [hwmon_chip_samples] = "samples", 4029f00995eSGuenter Roeck [hwmon_chip_curr_samples] = "curr_samples", 4039f00995eSGuenter Roeck [hwmon_chip_in_samples] = "in_samples", 4049f00995eSGuenter Roeck [hwmon_chip_power_samples] = "power_samples", 4059f00995eSGuenter Roeck [hwmon_chip_temp_samples] = "temp_samples", 406d560168bSGuenter Roeck }; 407d560168bSGuenter Roeck 408d560168bSGuenter Roeck static const char * const hwmon_temp_attr_templates[] = { 409002c6b54SGuenter Roeck [hwmon_temp_enable] = "temp%d_enable", 410d560168bSGuenter Roeck [hwmon_temp_input] = "temp%d_input", 411d560168bSGuenter Roeck [hwmon_temp_type] = "temp%d_type", 412d560168bSGuenter Roeck [hwmon_temp_lcrit] = "temp%d_lcrit", 413d560168bSGuenter Roeck [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst", 414d560168bSGuenter Roeck [hwmon_temp_min] = "temp%d_min", 415d560168bSGuenter Roeck [hwmon_temp_min_hyst] = "temp%d_min_hyst", 416d560168bSGuenter Roeck [hwmon_temp_max] = "temp%d_max", 417d560168bSGuenter Roeck [hwmon_temp_max_hyst] = "temp%d_max_hyst", 418d560168bSGuenter Roeck [hwmon_temp_crit] = "temp%d_crit", 419d560168bSGuenter Roeck [hwmon_temp_crit_hyst] = "temp%d_crit_hyst", 420d560168bSGuenter Roeck [hwmon_temp_emergency] = "temp%d_emergency", 421d560168bSGuenter Roeck [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst", 422d560168bSGuenter Roeck [hwmon_temp_alarm] = "temp%d_alarm", 423d560168bSGuenter Roeck [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm", 424d560168bSGuenter Roeck [hwmon_temp_min_alarm] = "temp%d_min_alarm", 425d560168bSGuenter Roeck [hwmon_temp_max_alarm] = "temp%d_max_alarm", 426d560168bSGuenter Roeck [hwmon_temp_crit_alarm] = "temp%d_crit_alarm", 427d560168bSGuenter Roeck [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm", 428d560168bSGuenter Roeck [hwmon_temp_fault] = "temp%d_fault", 429d560168bSGuenter Roeck [hwmon_temp_offset] = "temp%d_offset", 430d560168bSGuenter Roeck [hwmon_temp_label] = "temp%d_label", 431d560168bSGuenter Roeck [hwmon_temp_lowest] = "temp%d_lowest", 432d560168bSGuenter Roeck [hwmon_temp_highest] = "temp%d_highest", 433d560168bSGuenter Roeck [hwmon_temp_reset_history] = "temp%d_reset_history", 434*1967f712SZbigniew Lukwinski [hwmon_temp_rated_min] = "temp%d_rated_min", 435*1967f712SZbigniew Lukwinski [hwmon_temp_rated_max] = "temp%d_rated_max", 436d560168bSGuenter Roeck }; 437d560168bSGuenter Roeck 43800d616cfSGuenter Roeck static const char * const hwmon_in_attr_templates[] = { 439002c6b54SGuenter Roeck [hwmon_in_enable] = "in%d_enable", 44000d616cfSGuenter Roeck [hwmon_in_input] = "in%d_input", 44100d616cfSGuenter Roeck [hwmon_in_min] = "in%d_min", 44200d616cfSGuenter Roeck [hwmon_in_max] = "in%d_max", 44300d616cfSGuenter Roeck [hwmon_in_lcrit] = "in%d_lcrit", 44400d616cfSGuenter Roeck [hwmon_in_crit] = "in%d_crit", 44500d616cfSGuenter Roeck [hwmon_in_average] = "in%d_average", 44600d616cfSGuenter Roeck [hwmon_in_lowest] = "in%d_lowest", 44700d616cfSGuenter Roeck [hwmon_in_highest] = "in%d_highest", 44800d616cfSGuenter Roeck [hwmon_in_reset_history] = "in%d_reset_history", 44900d616cfSGuenter Roeck [hwmon_in_label] = "in%d_label", 45000d616cfSGuenter Roeck [hwmon_in_alarm] = "in%d_alarm", 45100d616cfSGuenter Roeck [hwmon_in_min_alarm] = "in%d_min_alarm", 45200d616cfSGuenter Roeck [hwmon_in_max_alarm] = "in%d_max_alarm", 45300d616cfSGuenter Roeck [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm", 45400d616cfSGuenter Roeck [hwmon_in_crit_alarm] = "in%d_crit_alarm", 455*1967f712SZbigniew Lukwinski [hwmon_in_rated_min] = "in%d_rated_min", 456*1967f712SZbigniew Lukwinski [hwmon_in_rated_max] = "in%d_rated_max", 45700d616cfSGuenter Roeck }; 45800d616cfSGuenter Roeck 4599b26947cSGuenter Roeck static const char * const hwmon_curr_attr_templates[] = { 460002c6b54SGuenter Roeck [hwmon_curr_enable] = "curr%d_enable", 4619b26947cSGuenter Roeck [hwmon_curr_input] = "curr%d_input", 4629b26947cSGuenter Roeck [hwmon_curr_min] = "curr%d_min", 4639b26947cSGuenter Roeck [hwmon_curr_max] = "curr%d_max", 4649b26947cSGuenter Roeck [hwmon_curr_lcrit] = "curr%d_lcrit", 4659b26947cSGuenter Roeck [hwmon_curr_crit] = "curr%d_crit", 4669b26947cSGuenter Roeck [hwmon_curr_average] = "curr%d_average", 4679b26947cSGuenter Roeck [hwmon_curr_lowest] = "curr%d_lowest", 4689b26947cSGuenter Roeck [hwmon_curr_highest] = "curr%d_highest", 4699b26947cSGuenter Roeck [hwmon_curr_reset_history] = "curr%d_reset_history", 4709b26947cSGuenter Roeck [hwmon_curr_label] = "curr%d_label", 4719b26947cSGuenter Roeck [hwmon_curr_alarm] = "curr%d_alarm", 4729b26947cSGuenter Roeck [hwmon_curr_min_alarm] = "curr%d_min_alarm", 4739b26947cSGuenter Roeck [hwmon_curr_max_alarm] = "curr%d_max_alarm", 4749b26947cSGuenter Roeck [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm", 4759b26947cSGuenter Roeck [hwmon_curr_crit_alarm] = "curr%d_crit_alarm", 476*1967f712SZbigniew Lukwinski [hwmon_curr_rated_min] = "curr%d_rated_min", 477*1967f712SZbigniew Lukwinski [hwmon_curr_rated_max] = "curr%d_rated_max", 4789b26947cSGuenter Roeck }; 4799b26947cSGuenter Roeck 480b308f5c7SGuenter Roeck static const char * const hwmon_power_attr_templates[] = { 481002c6b54SGuenter Roeck [hwmon_power_enable] = "power%d_enable", 482b308f5c7SGuenter Roeck [hwmon_power_average] = "power%d_average", 483b308f5c7SGuenter Roeck [hwmon_power_average_interval] = "power%d_average_interval", 484b308f5c7SGuenter Roeck [hwmon_power_average_interval_max] = "power%d_interval_max", 485b308f5c7SGuenter Roeck [hwmon_power_average_interval_min] = "power%d_interval_min", 486b308f5c7SGuenter Roeck [hwmon_power_average_highest] = "power%d_average_highest", 487b308f5c7SGuenter Roeck [hwmon_power_average_lowest] = "power%d_average_lowest", 488b308f5c7SGuenter Roeck [hwmon_power_average_max] = "power%d_average_max", 489b308f5c7SGuenter Roeck [hwmon_power_average_min] = "power%d_average_min", 490b308f5c7SGuenter Roeck [hwmon_power_input] = "power%d_input", 491b308f5c7SGuenter Roeck [hwmon_power_input_highest] = "power%d_input_highest", 492b308f5c7SGuenter Roeck [hwmon_power_input_lowest] = "power%d_input_lowest", 493b308f5c7SGuenter Roeck [hwmon_power_reset_history] = "power%d_reset_history", 494b308f5c7SGuenter Roeck [hwmon_power_accuracy] = "power%d_accuracy", 495b308f5c7SGuenter Roeck [hwmon_power_cap] = "power%d_cap", 496b308f5c7SGuenter Roeck [hwmon_power_cap_hyst] = "power%d_cap_hyst", 497b308f5c7SGuenter Roeck [hwmon_power_cap_max] = "power%d_cap_max", 498b308f5c7SGuenter Roeck [hwmon_power_cap_min] = "power%d_cap_min", 499aa7f29b0SAndrew Lunn [hwmon_power_min] = "power%d_min", 500b308f5c7SGuenter Roeck [hwmon_power_max] = "power%d_max", 501aa7f29b0SAndrew Lunn [hwmon_power_lcrit] = "power%d_lcrit", 502b308f5c7SGuenter Roeck [hwmon_power_crit] = "power%d_crit", 503b308f5c7SGuenter Roeck [hwmon_power_label] = "power%d_label", 504b308f5c7SGuenter Roeck [hwmon_power_alarm] = "power%d_alarm", 505b308f5c7SGuenter Roeck [hwmon_power_cap_alarm] = "power%d_cap_alarm", 506aa7f29b0SAndrew Lunn [hwmon_power_min_alarm] = "power%d_min_alarm", 507b308f5c7SGuenter Roeck [hwmon_power_max_alarm] = "power%d_max_alarm", 508aa7f29b0SAndrew Lunn [hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm", 509b308f5c7SGuenter Roeck [hwmon_power_crit_alarm] = "power%d_crit_alarm", 510*1967f712SZbigniew Lukwinski [hwmon_power_rated_min] = "power%d_rated_min", 511*1967f712SZbigniew Lukwinski [hwmon_power_rated_max] = "power%d_rated_max", 512b308f5c7SGuenter Roeck }; 513b308f5c7SGuenter Roeck 5146bfcca44SGuenter Roeck static const char * const hwmon_energy_attr_templates[] = { 515002c6b54SGuenter Roeck [hwmon_energy_enable] = "energy%d_enable", 5166bfcca44SGuenter Roeck [hwmon_energy_input] = "energy%d_input", 5176bfcca44SGuenter Roeck [hwmon_energy_label] = "energy%d_label", 5186bfcca44SGuenter Roeck }; 5196bfcca44SGuenter Roeck 5206bfcca44SGuenter Roeck static const char * const hwmon_humidity_attr_templates[] = { 521002c6b54SGuenter Roeck [hwmon_humidity_enable] = "humidity%d_enable", 5226bfcca44SGuenter Roeck [hwmon_humidity_input] = "humidity%d_input", 5236bfcca44SGuenter Roeck [hwmon_humidity_label] = "humidity%d_label", 5246bfcca44SGuenter Roeck [hwmon_humidity_min] = "humidity%d_min", 5256bfcca44SGuenter Roeck [hwmon_humidity_min_hyst] = "humidity%d_min_hyst", 5266bfcca44SGuenter Roeck [hwmon_humidity_max] = "humidity%d_max", 5276bfcca44SGuenter Roeck [hwmon_humidity_max_hyst] = "humidity%d_max_hyst", 5286bfcca44SGuenter Roeck [hwmon_humidity_alarm] = "humidity%d_alarm", 5296bfcca44SGuenter Roeck [hwmon_humidity_fault] = "humidity%d_fault", 530*1967f712SZbigniew Lukwinski [hwmon_humidity_rated_min] = "humidity%d_rated_min", 531*1967f712SZbigniew Lukwinski [hwmon_humidity_rated_max] = "humidity%d_rated_max", 5326bfcca44SGuenter Roeck }; 5336bfcca44SGuenter Roeck 5348faee73fSGuenter Roeck static const char * const hwmon_fan_attr_templates[] = { 535002c6b54SGuenter Roeck [hwmon_fan_enable] = "fan%d_enable", 5368faee73fSGuenter Roeck [hwmon_fan_input] = "fan%d_input", 5378faee73fSGuenter Roeck [hwmon_fan_label] = "fan%d_label", 5388faee73fSGuenter Roeck [hwmon_fan_min] = "fan%d_min", 5398faee73fSGuenter Roeck [hwmon_fan_max] = "fan%d_max", 5408faee73fSGuenter Roeck [hwmon_fan_div] = "fan%d_div", 5418faee73fSGuenter Roeck [hwmon_fan_pulses] = "fan%d_pulses", 5428faee73fSGuenter Roeck [hwmon_fan_target] = "fan%d_target", 5438faee73fSGuenter Roeck [hwmon_fan_alarm] = "fan%d_alarm", 5448faee73fSGuenter Roeck [hwmon_fan_min_alarm] = "fan%d_min_alarm", 5458faee73fSGuenter Roeck [hwmon_fan_max_alarm] = "fan%d_max_alarm", 5468faee73fSGuenter Roeck [hwmon_fan_fault] = "fan%d_fault", 5478faee73fSGuenter Roeck }; 5488faee73fSGuenter Roeck 549f9f7bb3aSGuenter Roeck static const char * const hwmon_pwm_attr_templates[] = { 550f9f7bb3aSGuenter Roeck [hwmon_pwm_input] = "pwm%d", 551f9f7bb3aSGuenter Roeck [hwmon_pwm_enable] = "pwm%d_enable", 552f9f7bb3aSGuenter Roeck [hwmon_pwm_mode] = "pwm%d_mode", 553f9f7bb3aSGuenter Roeck [hwmon_pwm_freq] = "pwm%d_freq", 554f9f7bb3aSGuenter Roeck }; 555f9f7bb3aSGuenter Roeck 5564413405fSDr. David Alan Gilbert static const char * const hwmon_intrusion_attr_templates[] = { 5574413405fSDr. David Alan Gilbert [hwmon_intrusion_alarm] = "intrusion%d_alarm", 5584413405fSDr. David Alan Gilbert [hwmon_intrusion_beep] = "intrusion%d_beep", 5594413405fSDr. David Alan Gilbert }; 5604413405fSDr. David Alan Gilbert 561d560168bSGuenter Roeck static const char * const *__templates[] = { 562f4d325d5SGuenter Roeck [hwmon_chip] = hwmon_chip_attrs, 563d560168bSGuenter Roeck [hwmon_temp] = hwmon_temp_attr_templates, 56400d616cfSGuenter Roeck [hwmon_in] = hwmon_in_attr_templates, 5659b26947cSGuenter Roeck [hwmon_curr] = hwmon_curr_attr_templates, 566b308f5c7SGuenter Roeck [hwmon_power] = hwmon_power_attr_templates, 5676bfcca44SGuenter Roeck [hwmon_energy] = hwmon_energy_attr_templates, 5686bfcca44SGuenter Roeck [hwmon_humidity] = hwmon_humidity_attr_templates, 5698faee73fSGuenter Roeck [hwmon_fan] = hwmon_fan_attr_templates, 570f9f7bb3aSGuenter Roeck [hwmon_pwm] = hwmon_pwm_attr_templates, 5714413405fSDr. David Alan Gilbert [hwmon_intrusion] = hwmon_intrusion_attr_templates, 572d560168bSGuenter Roeck }; 573d560168bSGuenter Roeck 574d560168bSGuenter Roeck static const int __templates_size[] = { 575f4d325d5SGuenter Roeck [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs), 576d560168bSGuenter Roeck [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates), 57700d616cfSGuenter Roeck [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates), 5789b26947cSGuenter Roeck [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates), 579b308f5c7SGuenter Roeck [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates), 5806bfcca44SGuenter Roeck [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates), 5816bfcca44SGuenter Roeck [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates), 5828faee73fSGuenter Roeck [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates), 583f9f7bb3aSGuenter Roeck [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates), 5844413405fSDr. David Alan Gilbert [hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates), 585d560168bSGuenter Roeck }; 586d560168bSGuenter Roeck 5871597b374SGuenter Roeck int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type, 5881597b374SGuenter Roeck u32 attr, int channel) 5891597b374SGuenter Roeck { 5901597b374SGuenter Roeck char sattr[MAX_SYSFS_ATTR_NAME_LENGTH]; 5911597b374SGuenter Roeck const char * const *templates; 5921597b374SGuenter Roeck const char *template; 5931597b374SGuenter Roeck int base; 5941597b374SGuenter Roeck 5951597b374SGuenter Roeck if (type >= ARRAY_SIZE(__templates)) 5961597b374SGuenter Roeck return -EINVAL; 5971597b374SGuenter Roeck if (attr >= __templates_size[type]) 5981597b374SGuenter Roeck return -EINVAL; 5991597b374SGuenter Roeck 6001597b374SGuenter Roeck templates = __templates[type]; 6011597b374SGuenter Roeck template = templates[attr]; 6021597b374SGuenter Roeck 6031597b374SGuenter Roeck base = hwmon_attr_base(type); 6041597b374SGuenter Roeck 6051597b374SGuenter Roeck scnprintf(sattr, MAX_SYSFS_ATTR_NAME_LENGTH, template, base + channel); 6061597b374SGuenter Roeck sysfs_notify(&dev->kobj, NULL, sattr); 6071597b374SGuenter Roeck kobject_uevent(&dev->kobj, KOBJ_CHANGE); 6081597b374SGuenter Roeck 6091597b374SGuenter Roeck if (type == hwmon_temp) 6101597b374SGuenter Roeck hwmon_thermal_notify(dev, channel); 6111597b374SGuenter Roeck 6121597b374SGuenter Roeck return 0; 6131597b374SGuenter Roeck } 6141597b374SGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_notify_event); 6151597b374SGuenter Roeck 616d560168bSGuenter Roeck static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info) 617d560168bSGuenter Roeck { 618d560168bSGuenter Roeck int i, n; 619d560168bSGuenter Roeck 620d560168bSGuenter Roeck for (i = n = 0; info->config[i]; i++) 621d560168bSGuenter Roeck n += hweight32(info->config[i]); 622d560168bSGuenter Roeck 623d560168bSGuenter Roeck return n; 624d560168bSGuenter Roeck } 625d560168bSGuenter Roeck 6263bf8bdcfSGuenter Roeck static int hwmon_genattrs(const void *drvdata, 627d560168bSGuenter Roeck struct attribute **attrs, 628d560168bSGuenter Roeck const struct hwmon_ops *ops, 629d560168bSGuenter Roeck const struct hwmon_channel_info *info) 630d560168bSGuenter Roeck { 631d560168bSGuenter Roeck const char * const *templates; 632d560168bSGuenter Roeck int template_size; 633d560168bSGuenter Roeck int i, aindex = 0; 634d560168bSGuenter Roeck 635d560168bSGuenter Roeck if (info->type >= ARRAY_SIZE(__templates)) 636d560168bSGuenter Roeck return -EINVAL; 637d560168bSGuenter Roeck 638d560168bSGuenter Roeck templates = __templates[info->type]; 639d560168bSGuenter Roeck template_size = __templates_size[info->type]; 640d560168bSGuenter Roeck 641d560168bSGuenter Roeck for (i = 0; info->config[i]; i++) { 642d560168bSGuenter Roeck u32 attr_mask = info->config[i]; 643d560168bSGuenter Roeck u32 attr; 644d560168bSGuenter Roeck 645d560168bSGuenter Roeck while (attr_mask) { 646d560168bSGuenter Roeck struct attribute *a; 647d560168bSGuenter Roeck 648d560168bSGuenter Roeck attr = __ffs(attr_mask); 649d560168bSGuenter Roeck attr_mask &= ~BIT(attr); 650d560168bSGuenter Roeck if (attr >= template_size) 651d560168bSGuenter Roeck return -EINVAL; 6523bf8bdcfSGuenter Roeck a = hwmon_genattr(drvdata, info->type, attr, i, 653d560168bSGuenter Roeck templates[attr], ops); 654d560168bSGuenter Roeck if (IS_ERR(a)) { 655d560168bSGuenter Roeck if (PTR_ERR(a) != -ENOENT) 656d560168bSGuenter Roeck return PTR_ERR(a); 657d560168bSGuenter Roeck continue; 658d560168bSGuenter Roeck } 659d560168bSGuenter Roeck attrs[aindex++] = a; 660d560168bSGuenter Roeck } 661d560168bSGuenter Roeck } 662d560168bSGuenter Roeck return aindex; 663d560168bSGuenter Roeck } 664d560168bSGuenter Roeck 665d560168bSGuenter Roeck static struct attribute ** 6663bf8bdcfSGuenter Roeck __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip) 667d560168bSGuenter Roeck { 668d560168bSGuenter Roeck int ret, i, aindex = 0, nattrs = 0; 669d560168bSGuenter Roeck struct attribute **attrs; 670d560168bSGuenter Roeck 671d560168bSGuenter Roeck for (i = 0; chip->info[i]; i++) 672d560168bSGuenter Roeck nattrs += hwmon_num_channel_attrs(chip->info[i]); 673d560168bSGuenter Roeck 674d560168bSGuenter Roeck if (nattrs == 0) 675d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 676d560168bSGuenter Roeck 6773bf8bdcfSGuenter Roeck attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL); 678d560168bSGuenter Roeck if (!attrs) 679d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 680d560168bSGuenter Roeck 681d560168bSGuenter Roeck for (i = 0; chip->info[i]; i++) { 6823bf8bdcfSGuenter Roeck ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops, 683d560168bSGuenter Roeck chip->info[i]); 6843bf8bdcfSGuenter Roeck if (ret < 0) { 6853bf8bdcfSGuenter Roeck hwmon_free_attrs(attrs); 686d560168bSGuenter Roeck return ERR_PTR(ret); 6873bf8bdcfSGuenter Roeck } 688d560168bSGuenter Roeck aindex += ret; 689d560168bSGuenter Roeck } 690d560168bSGuenter Roeck 691d560168bSGuenter Roeck return attrs; 692d560168bSGuenter Roeck } 693d560168bSGuenter Roeck 694d560168bSGuenter Roeck static struct device * 695d560168bSGuenter Roeck __hwmon_device_register(struct device *dev, const char *name, void *drvdata, 696d560168bSGuenter Roeck const struct hwmon_chip_info *chip, 697d560168bSGuenter Roeck const struct attribute_group **groups) 698d560168bSGuenter Roeck { 699d560168bSGuenter Roeck struct hwmon_device *hwdev; 700d560168bSGuenter Roeck struct device *hdev; 70144e3ad88SAkinobu Mita int i, err, id; 702d560168bSGuenter Roeck 70374d3b641SGuenter Roeck /* Complain about invalid characters in hwmon name attribute */ 704d560168bSGuenter Roeck if (name && (!strlen(name) || strpbrk(name, "-* \t\n"))) 70574d3b641SGuenter Roeck dev_warn(dev, 70674d3b641SGuenter Roeck "hwmon: '%s' is not a valid name attribute, please fix\n", 70774d3b641SGuenter Roeck name); 708d560168bSGuenter Roeck 709d560168bSGuenter Roeck id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL); 710d560168bSGuenter Roeck if (id < 0) 711d560168bSGuenter Roeck return ERR_PTR(id); 712d560168bSGuenter Roeck 713d560168bSGuenter Roeck hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL); 714d560168bSGuenter Roeck if (hwdev == NULL) { 715d560168bSGuenter Roeck err = -ENOMEM; 716d560168bSGuenter Roeck goto ida_remove; 717d560168bSGuenter Roeck } 718d560168bSGuenter Roeck 719d560168bSGuenter Roeck hdev = &hwdev->dev; 720d560168bSGuenter Roeck 721239552f4SGuenter Roeck if (chip) { 722d560168bSGuenter Roeck struct attribute **attrs; 723b2a4cc3aSGuenter Roeck int ngroups = 2; /* terminating NULL plus &hwdev->groups */ 724d560168bSGuenter Roeck 725d560168bSGuenter Roeck if (groups) 726d560168bSGuenter Roeck for (i = 0; groups[i]; i++) 727d560168bSGuenter Roeck ngroups++; 728d560168bSGuenter Roeck 7293bf8bdcfSGuenter Roeck hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL); 73038d8ed65SColin Ian King if (!hwdev->groups) { 73138d8ed65SColin Ian King err = -ENOMEM; 73238d8ed65SColin Ian King goto free_hwmon; 73338d8ed65SColin Ian King } 734d560168bSGuenter Roeck 7353bf8bdcfSGuenter Roeck attrs = __hwmon_create_attrs(drvdata, chip); 736d560168bSGuenter Roeck if (IS_ERR(attrs)) { 737d560168bSGuenter Roeck err = PTR_ERR(attrs); 738d560168bSGuenter Roeck goto free_hwmon; 739d560168bSGuenter Roeck } 740d560168bSGuenter Roeck 741d560168bSGuenter Roeck hwdev->group.attrs = attrs; 742d560168bSGuenter Roeck ngroups = 0; 743d560168bSGuenter Roeck hwdev->groups[ngroups++] = &hwdev->group; 744d560168bSGuenter Roeck 745d560168bSGuenter Roeck if (groups) { 746d560168bSGuenter Roeck for (i = 0; groups[i]; i++) 747d560168bSGuenter Roeck hwdev->groups[ngroups++] = groups[i]; 748d560168bSGuenter Roeck } 749d560168bSGuenter Roeck 750d560168bSGuenter Roeck hdev->groups = hwdev->groups; 751d560168bSGuenter Roeck } else { 752d560168bSGuenter Roeck hdev->groups = groups; 753d560168bSGuenter Roeck } 754d560168bSGuenter Roeck 755d560168bSGuenter Roeck hwdev->name = name; 756d560168bSGuenter Roeck hdev->class = &hwmon_class; 757d560168bSGuenter Roeck hdev->parent = dev; 758d560168bSGuenter Roeck hdev->of_node = dev ? dev->of_node : NULL; 759d560168bSGuenter Roeck hwdev->chip = chip; 760d560168bSGuenter Roeck dev_set_drvdata(hdev, drvdata); 761d560168bSGuenter Roeck dev_set_name(hdev, HWMON_ID_FORMAT, id); 762d560168bSGuenter Roeck err = device_register(hdev); 763d560168bSGuenter Roeck if (err) 764d560168bSGuenter Roeck goto free_hwmon; 765d560168bSGuenter Roeck 7661597b374SGuenter Roeck INIT_LIST_HEAD(&hwdev->tzdata); 7671597b374SGuenter Roeck 768c41dd48eSEduardo Valentin if (dev && dev->of_node && chip && chip->ops->read && 769d560168bSGuenter Roeck chip->info[0]->type == hwmon_chip && 770d560168bSGuenter Roeck (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) { 77144e3ad88SAkinobu Mita err = hwmon_thermal_register_sensors(hdev); 77274e35127SDmitry Osipenko if (err) { 77374e35127SDmitry Osipenko device_unregister(hdev); 774792eac18SGuenter Roeck /* 77544e3ad88SAkinobu Mita * Don't worry about hwdev; hwmon_dev_release(), called 77644e3ad88SAkinobu Mita * from device_unregister(), will free it. 777792eac18SGuenter Roeck */ 77874e35127SDmitry Osipenko goto ida_remove; 77974e35127SDmitry Osipenko } 78047c332deSLinus Walleij } 781d560168bSGuenter Roeck 782d560168bSGuenter Roeck return hdev; 783d560168bSGuenter Roeck 784d560168bSGuenter Roeck free_hwmon: 7853bf8bdcfSGuenter Roeck hwmon_dev_release(hdev); 786d560168bSGuenter Roeck ida_remove: 787d560168bSGuenter Roeck ida_simple_remove(&hwmon_ida, id); 788d560168bSGuenter Roeck return ERR_PTR(err); 789d560168bSGuenter Roeck } 790d560168bSGuenter Roeck 7911236441fSMark M. Hoffman /** 792bab2243cSGuenter Roeck * hwmon_device_register_with_groups - register w/ hwmon 793bab2243cSGuenter Roeck * @dev: the parent device 794bab2243cSGuenter Roeck * @name: hwmon name attribute 795bab2243cSGuenter Roeck * @drvdata: driver data to attach to created device 796bab2243cSGuenter Roeck * @groups: List of attribute groups to create 797bab2243cSGuenter Roeck * 798bab2243cSGuenter Roeck * hwmon_device_unregister() must be called when the device is no 799bab2243cSGuenter Roeck * longer needed. 800bab2243cSGuenter Roeck * 801bab2243cSGuenter Roeck * Returns the pointer to the new device. 802bab2243cSGuenter Roeck */ 803bab2243cSGuenter Roeck struct device * 804bab2243cSGuenter Roeck hwmon_device_register_with_groups(struct device *dev, const char *name, 805bab2243cSGuenter Roeck void *drvdata, 806bab2243cSGuenter Roeck const struct attribute_group **groups) 807bab2243cSGuenter Roeck { 8088353863aSGuenter Roeck if (!name) 8098353863aSGuenter Roeck return ERR_PTR(-EINVAL); 8108353863aSGuenter Roeck 811d560168bSGuenter Roeck return __hwmon_device_register(dev, name, drvdata, NULL, groups); 812bab2243cSGuenter Roeck } 813bab2243cSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups); 814bab2243cSGuenter Roeck 815bab2243cSGuenter Roeck /** 816d560168bSGuenter Roeck * hwmon_device_register_with_info - register w/ hwmon 817d560168bSGuenter Roeck * @dev: the parent device 818d560168bSGuenter Roeck * @name: hwmon name attribute 819d560168bSGuenter Roeck * @drvdata: driver data to attach to created device 8203870945aSGuenter Roeck * @chip: pointer to hwmon chip information 821848ba0a2SGuenter Roeck * @extra_groups: pointer to list of additional non-standard attribute groups 822d560168bSGuenter Roeck * 823d560168bSGuenter Roeck * hwmon_device_unregister() must be called when the device is no 824d560168bSGuenter Roeck * longer needed. 825d560168bSGuenter Roeck * 826d560168bSGuenter Roeck * Returns the pointer to the new device. 827d560168bSGuenter Roeck */ 828d560168bSGuenter Roeck struct device * 829d560168bSGuenter Roeck hwmon_device_register_with_info(struct device *dev, const char *name, 830d560168bSGuenter Roeck void *drvdata, 831d560168bSGuenter Roeck const struct hwmon_chip_info *chip, 832848ba0a2SGuenter Roeck const struct attribute_group **extra_groups) 833d560168bSGuenter Roeck { 8348353863aSGuenter Roeck if (!name) 8358353863aSGuenter Roeck return ERR_PTR(-EINVAL); 8368353863aSGuenter Roeck 837239552f4SGuenter Roeck if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info)) 838d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 839d560168bSGuenter Roeck 84059df4f4eSLucas Magasweran if (chip && !dev) 84159df4f4eSLucas Magasweran return ERR_PTR(-EINVAL); 84259df4f4eSLucas Magasweran 843848ba0a2SGuenter Roeck return __hwmon_device_register(dev, name, drvdata, chip, extra_groups); 844d560168bSGuenter Roeck } 845d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_info); 846d560168bSGuenter Roeck 847d560168bSGuenter Roeck /** 8481beeffe4STony Jones * hwmon_device_register - register w/ hwmon 8491236441fSMark M. Hoffman * @dev: the device to register 8501236441fSMark M. Hoffman * 8511beeffe4STony Jones * hwmon_device_unregister() must be called when the device is no 8521236441fSMark M. Hoffman * longer needed. 8531236441fSMark M. Hoffman * 8541beeffe4STony Jones * Returns the pointer to the new device. 8551236441fSMark M. Hoffman */ 8561beeffe4STony Jones struct device *hwmon_device_register(struct device *dev) 8571236441fSMark M. Hoffman { 858af1bd36cSGuenter Roeck dev_warn(dev, 859af1bd36cSGuenter Roeck "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n"); 860af1bd36cSGuenter Roeck 8618353863aSGuenter Roeck return __hwmon_device_register(dev, NULL, NULL, NULL, NULL); 8621236441fSMark M. Hoffman } 863839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_register); 8641236441fSMark M. Hoffman 8651236441fSMark M. Hoffman /** 8661236441fSMark M. Hoffman * hwmon_device_unregister - removes the previously registered class device 8671236441fSMark M. Hoffman * 8681beeffe4STony Jones * @dev: the class device to destroy 8691236441fSMark M. Hoffman */ 8701beeffe4STony Jones void hwmon_device_unregister(struct device *dev) 8711236441fSMark M. Hoffman { 8721236441fSMark M. Hoffman int id; 8731236441fSMark M. Hoffman 874739cf3a2SKay Sievers if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) { 8751beeffe4STony Jones device_unregister(dev); 8764ca5f468SJonathan Cameron ida_simple_remove(&hwmon_ida, id); 8771236441fSMark M. Hoffman } else 8781beeffe4STony Jones dev_dbg(dev->parent, 8791236441fSMark M. Hoffman "hwmon_device_unregister() failed: bad class ID!\n"); 8801236441fSMark M. Hoffman } 881839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_unregister); 8821236441fSMark M. Hoffman 88374188cbaSGuenter Roeck static void devm_hwmon_release(struct device *dev, void *res) 88474188cbaSGuenter Roeck { 88574188cbaSGuenter Roeck struct device *hwdev = *(struct device **)res; 88674188cbaSGuenter Roeck 88774188cbaSGuenter Roeck hwmon_device_unregister(hwdev); 88874188cbaSGuenter Roeck } 88974188cbaSGuenter Roeck 89074188cbaSGuenter Roeck /** 89174188cbaSGuenter Roeck * devm_hwmon_device_register_with_groups - register w/ hwmon 89274188cbaSGuenter Roeck * @dev: the parent device 89374188cbaSGuenter Roeck * @name: hwmon name attribute 89474188cbaSGuenter Roeck * @drvdata: driver data to attach to created device 89574188cbaSGuenter Roeck * @groups: List of attribute groups to create 89674188cbaSGuenter Roeck * 89774188cbaSGuenter Roeck * Returns the pointer to the new device. The new device is automatically 89874188cbaSGuenter Roeck * unregistered with the parent device. 89974188cbaSGuenter Roeck */ 90074188cbaSGuenter Roeck struct device * 90174188cbaSGuenter Roeck devm_hwmon_device_register_with_groups(struct device *dev, const char *name, 90274188cbaSGuenter Roeck void *drvdata, 90374188cbaSGuenter Roeck const struct attribute_group **groups) 90474188cbaSGuenter Roeck { 90574188cbaSGuenter Roeck struct device **ptr, *hwdev; 90674188cbaSGuenter Roeck 90774188cbaSGuenter Roeck if (!dev) 90874188cbaSGuenter Roeck return ERR_PTR(-EINVAL); 90974188cbaSGuenter Roeck 91074188cbaSGuenter Roeck ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); 91174188cbaSGuenter Roeck if (!ptr) 91274188cbaSGuenter Roeck return ERR_PTR(-ENOMEM); 91374188cbaSGuenter Roeck 91474188cbaSGuenter Roeck hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups); 91574188cbaSGuenter Roeck if (IS_ERR(hwdev)) 91674188cbaSGuenter Roeck goto error; 91774188cbaSGuenter Roeck 91874188cbaSGuenter Roeck *ptr = hwdev; 91974188cbaSGuenter Roeck devres_add(dev, ptr); 92074188cbaSGuenter Roeck return hwdev; 92174188cbaSGuenter Roeck 92274188cbaSGuenter Roeck error: 92374188cbaSGuenter Roeck devres_free(ptr); 92474188cbaSGuenter Roeck return hwdev; 92574188cbaSGuenter Roeck } 92674188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups); 92774188cbaSGuenter Roeck 928d560168bSGuenter Roeck /** 929d560168bSGuenter Roeck * devm_hwmon_device_register_with_info - register w/ hwmon 930d560168bSGuenter Roeck * @dev: the parent device 931d560168bSGuenter Roeck * @name: hwmon name attribute 932d560168bSGuenter Roeck * @drvdata: driver data to attach to created device 9333870945aSGuenter Roeck * @chip: pointer to hwmon chip information 9343870945aSGuenter Roeck * @groups: pointer to list of driver specific attribute groups 935d560168bSGuenter Roeck * 936d560168bSGuenter Roeck * Returns the pointer to the new device. The new device is automatically 937d560168bSGuenter Roeck * unregistered with the parent device. 938d560168bSGuenter Roeck */ 939d560168bSGuenter Roeck struct device * 940d560168bSGuenter Roeck devm_hwmon_device_register_with_info(struct device *dev, const char *name, 941d560168bSGuenter Roeck void *drvdata, 942d560168bSGuenter Roeck const struct hwmon_chip_info *chip, 943d560168bSGuenter Roeck const struct attribute_group **groups) 944d560168bSGuenter Roeck { 945d560168bSGuenter Roeck struct device **ptr, *hwdev; 946d560168bSGuenter Roeck 947d560168bSGuenter Roeck if (!dev) 948d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 949d560168bSGuenter Roeck 950d560168bSGuenter Roeck ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); 951d560168bSGuenter Roeck if (!ptr) 952d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 953d560168bSGuenter Roeck 954d560168bSGuenter Roeck hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip, 955d560168bSGuenter Roeck groups); 956d560168bSGuenter Roeck if (IS_ERR(hwdev)) 957d560168bSGuenter Roeck goto error; 958d560168bSGuenter Roeck 959d560168bSGuenter Roeck *ptr = hwdev; 960d560168bSGuenter Roeck devres_add(dev, ptr); 961d560168bSGuenter Roeck 962d560168bSGuenter Roeck return hwdev; 963d560168bSGuenter Roeck 964d560168bSGuenter Roeck error: 965d560168bSGuenter Roeck devres_free(ptr); 966d560168bSGuenter Roeck return hwdev; 967d560168bSGuenter Roeck } 968d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info); 969d560168bSGuenter Roeck 97074188cbaSGuenter Roeck static int devm_hwmon_match(struct device *dev, void *res, void *data) 97174188cbaSGuenter Roeck { 97274188cbaSGuenter Roeck struct device **hwdev = res; 97374188cbaSGuenter Roeck 97474188cbaSGuenter Roeck return *hwdev == data; 97574188cbaSGuenter Roeck } 97674188cbaSGuenter Roeck 97774188cbaSGuenter Roeck /** 97874188cbaSGuenter Roeck * devm_hwmon_device_unregister - removes a previously registered hwmon device 97974188cbaSGuenter Roeck * 98074188cbaSGuenter Roeck * @dev: the parent device of the device to unregister 98174188cbaSGuenter Roeck */ 98274188cbaSGuenter Roeck void devm_hwmon_device_unregister(struct device *dev) 98374188cbaSGuenter Roeck { 98474188cbaSGuenter Roeck WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev)); 98574188cbaSGuenter Roeck } 98674188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister); 98774188cbaSGuenter Roeck 9882958b1ecSJean Delvare static void __init hwmon_pci_quirks(void) 9892958b1ecSJean Delvare { 9902958b1ecSJean Delvare #if defined CONFIG_X86 && defined CONFIG_PCI 9912958b1ecSJean Delvare struct pci_dev *sb; 9922958b1ecSJean Delvare u16 base; 9932958b1ecSJean Delvare u8 enable; 9942958b1ecSJean Delvare 9952958b1ecSJean Delvare /* Open access to 0x295-0x296 on MSI MS-7031 */ 9962958b1ecSJean Delvare sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL); 997d6dab7ddSJean Delvare if (sb) { 998d6dab7ddSJean Delvare if (sb->subsystem_vendor == 0x1462 && /* MSI */ 999d6dab7ddSJean Delvare sb->subsystem_device == 0x0031) { /* MS-7031 */ 10002958b1ecSJean Delvare pci_read_config_byte(sb, 0x48, &enable); 10012958b1ecSJean Delvare pci_read_config_word(sb, 0x64, &base); 10022958b1ecSJean Delvare 10032958b1ecSJean Delvare if (base == 0 && !(enable & BIT(2))) { 10042958b1ecSJean Delvare dev_info(&sb->dev, 10052958b1ecSJean Delvare "Opening wide generic port at 0x295\n"); 10062958b1ecSJean Delvare pci_write_config_word(sb, 0x64, 0x295); 1007d6dab7ddSJean Delvare pci_write_config_byte(sb, 0x48, 1008d6dab7ddSJean Delvare enable | BIT(2)); 10092958b1ecSJean Delvare } 10102958b1ecSJean Delvare } 1011d6dab7ddSJean Delvare pci_dev_put(sb); 1012d6dab7ddSJean Delvare } 10132958b1ecSJean Delvare #endif 10142958b1ecSJean Delvare } 10152958b1ecSJean Delvare 10161236441fSMark M. Hoffman static int __init hwmon_init(void) 10171236441fSMark M. Hoffman { 1018bab2243cSGuenter Roeck int err; 1019bab2243cSGuenter Roeck 10202958b1ecSJean Delvare hwmon_pci_quirks(); 10212958b1ecSJean Delvare 1022bab2243cSGuenter Roeck err = class_register(&hwmon_class); 1023bab2243cSGuenter Roeck if (err) { 1024bab2243cSGuenter Roeck pr_err("couldn't register hwmon sysfs class\n"); 1025bab2243cSGuenter Roeck return err; 10261236441fSMark M. Hoffman } 10271236441fSMark M. Hoffman return 0; 10281236441fSMark M. Hoffman } 10291236441fSMark M. Hoffman 10301236441fSMark M. Hoffman static void __exit hwmon_exit(void) 10311236441fSMark M. Hoffman { 1032bab2243cSGuenter Roeck class_unregister(&hwmon_class); 10331236441fSMark M. Hoffman } 10341236441fSMark M. Hoffman 103537f54ee5SDavid Brownell subsys_initcall(hwmon_init); 10361236441fSMark M. Hoffman module_exit(hwmon_exit); 10371236441fSMark M. Hoffman 10381236441fSMark M. Hoffman MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>"); 10391236441fSMark M. Hoffman MODULE_DESCRIPTION("hardware monitoring sysfs/class support"); 10401236441fSMark M. Hoffman MODULE_LICENSE("GPL"); 10411236441fSMark M. Hoffman 1042