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 { 8277d76768SYang Li struct device *dev = kobj_to_dev(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 156*a5f6c0f8SDmitry Osipenko static int hwmon_thermal_set_trips(void *data, int low, int high) 157*a5f6c0f8SDmitry Osipenko { 158*a5f6c0f8SDmitry Osipenko struct hwmon_thermal_data *tdata = data; 159*a5f6c0f8SDmitry Osipenko struct hwmon_device *hwdev = to_hwmon_device(tdata->dev); 160*a5f6c0f8SDmitry Osipenko const struct hwmon_chip_info *chip = hwdev->chip; 161*a5f6c0f8SDmitry Osipenko const struct hwmon_channel_info **info = chip->info; 162*a5f6c0f8SDmitry Osipenko unsigned int i; 163*a5f6c0f8SDmitry Osipenko int err; 164*a5f6c0f8SDmitry Osipenko 165*a5f6c0f8SDmitry Osipenko if (!chip->ops->write) 166*a5f6c0f8SDmitry Osipenko return 0; 167*a5f6c0f8SDmitry Osipenko 168*a5f6c0f8SDmitry Osipenko for (i = 0; info[i] && info[i]->type != hwmon_temp; i++) 169*a5f6c0f8SDmitry Osipenko continue; 170*a5f6c0f8SDmitry Osipenko 171*a5f6c0f8SDmitry Osipenko if (!info[i]) 172*a5f6c0f8SDmitry Osipenko return 0; 173*a5f6c0f8SDmitry Osipenko 174*a5f6c0f8SDmitry Osipenko if (info[i]->config[tdata->index] & HWMON_T_MIN) { 175*a5f6c0f8SDmitry Osipenko err = chip->ops->write(tdata->dev, hwmon_temp, 176*a5f6c0f8SDmitry Osipenko hwmon_temp_min, tdata->index, low); 177*a5f6c0f8SDmitry Osipenko if (err && err != -EOPNOTSUPP) 178*a5f6c0f8SDmitry Osipenko return err; 179*a5f6c0f8SDmitry Osipenko } 180*a5f6c0f8SDmitry Osipenko 181*a5f6c0f8SDmitry Osipenko if (info[i]->config[tdata->index] & HWMON_T_MAX) { 182*a5f6c0f8SDmitry Osipenko err = chip->ops->write(tdata->dev, hwmon_temp, 183*a5f6c0f8SDmitry Osipenko hwmon_temp_max, tdata->index, high); 184*a5f6c0f8SDmitry Osipenko if (err && err != -EOPNOTSUPP) 185*a5f6c0f8SDmitry Osipenko return err; 186*a5f6c0f8SDmitry Osipenko } 187*a5f6c0f8SDmitry Osipenko 188*a5f6c0f8SDmitry Osipenko return 0; 189*a5f6c0f8SDmitry Osipenko } 190*a5f6c0f8SDmitry Osipenko 191c9920650SJulia Lawall static const struct thermal_zone_of_device_ops hwmon_thermal_ops = { 192d560168bSGuenter Roeck .get_temp = hwmon_thermal_get_temp, 193*a5f6c0f8SDmitry Osipenko .set_trips = hwmon_thermal_set_trips, 194d560168bSGuenter Roeck }; 195d560168bSGuenter Roeck 1961597b374SGuenter Roeck static void hwmon_thermal_remove_sensor(void *data) 1971597b374SGuenter Roeck { 1981597b374SGuenter Roeck list_del(data); 1991597b374SGuenter Roeck } 2001597b374SGuenter Roeck 2013bf8bdcfSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev, int index) 202d560168bSGuenter Roeck { 2031597b374SGuenter Roeck struct hwmon_device *hwdev = to_hwmon_device(dev); 204d560168bSGuenter Roeck struct hwmon_thermal_data *tdata; 20547c332deSLinus Walleij struct thermal_zone_device *tzd; 2061597b374SGuenter Roeck int err; 207d560168bSGuenter Roeck 208d560168bSGuenter Roeck tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL); 209d560168bSGuenter Roeck if (!tdata) 210d560168bSGuenter Roeck return -ENOMEM; 211d560168bSGuenter Roeck 2123bf8bdcfSGuenter Roeck tdata->dev = dev; 213d560168bSGuenter Roeck tdata->index = index; 214d560168bSGuenter Roeck 2153bf8bdcfSGuenter Roeck tzd = devm_thermal_zone_of_sensor_register(dev, index, tdata, 216d560168bSGuenter Roeck &hwmon_thermal_ops); 21747c332deSLinus Walleij /* 21847c332deSLinus Walleij * If CONFIG_THERMAL_OF is disabled, this returns -ENODEV, 21947c332deSLinus Walleij * so ignore that error but forward any other error. 22047c332deSLinus Walleij */ 22147c332deSLinus Walleij if (IS_ERR(tzd) && (PTR_ERR(tzd) != -ENODEV)) 22247c332deSLinus Walleij return PTR_ERR(tzd); 223d560168bSGuenter Roeck 2241597b374SGuenter Roeck err = devm_add_action(dev, hwmon_thermal_remove_sensor, &tdata->node); 2251597b374SGuenter Roeck if (err) 2261597b374SGuenter Roeck return err; 2271597b374SGuenter Roeck 2281597b374SGuenter Roeck tdata->tzd = tzd; 2291597b374SGuenter Roeck list_add(&tdata->node, &hwdev->tzdata); 2301597b374SGuenter Roeck 231d560168bSGuenter Roeck return 0; 232d560168bSGuenter Roeck } 23344e3ad88SAkinobu Mita 23444e3ad88SAkinobu Mita static int hwmon_thermal_register_sensors(struct device *dev) 23544e3ad88SAkinobu Mita { 23644e3ad88SAkinobu Mita struct hwmon_device *hwdev = to_hwmon_device(dev); 23744e3ad88SAkinobu Mita const struct hwmon_chip_info *chip = hwdev->chip; 23844e3ad88SAkinobu Mita const struct hwmon_channel_info **info = chip->info; 23944e3ad88SAkinobu Mita void *drvdata = dev_get_drvdata(dev); 24044e3ad88SAkinobu Mita int i; 24144e3ad88SAkinobu Mita 24244e3ad88SAkinobu Mita for (i = 1; info[i]; i++) { 24344e3ad88SAkinobu Mita int j; 24444e3ad88SAkinobu Mita 24544e3ad88SAkinobu Mita if (info[i]->type != hwmon_temp) 24644e3ad88SAkinobu Mita continue; 24744e3ad88SAkinobu Mita 24844e3ad88SAkinobu Mita for (j = 0; info[i]->config[j]; j++) { 24944e3ad88SAkinobu Mita int err; 25044e3ad88SAkinobu Mita 25144e3ad88SAkinobu Mita if (!(info[i]->config[j] & HWMON_T_INPUT) || 25244e3ad88SAkinobu Mita !chip->ops->is_visible(drvdata, hwmon_temp, 25344e3ad88SAkinobu Mita hwmon_temp_input, j)) 25444e3ad88SAkinobu Mita continue; 25544e3ad88SAkinobu Mita 25644e3ad88SAkinobu Mita err = hwmon_thermal_add_sensor(dev, j); 25744e3ad88SAkinobu Mita if (err) 25844e3ad88SAkinobu Mita return err; 25944e3ad88SAkinobu Mita } 26044e3ad88SAkinobu Mita } 26144e3ad88SAkinobu Mita 26244e3ad88SAkinobu Mita return 0; 26344e3ad88SAkinobu Mita } 26444e3ad88SAkinobu Mita 2651597b374SGuenter Roeck static void hwmon_thermal_notify(struct device *dev, int index) 2661597b374SGuenter Roeck { 2671597b374SGuenter Roeck struct hwmon_device *hwdev = to_hwmon_device(dev); 2681597b374SGuenter Roeck struct hwmon_thermal_data *tzdata; 2691597b374SGuenter Roeck 2701597b374SGuenter Roeck list_for_each_entry(tzdata, &hwdev->tzdata, node) { 2711597b374SGuenter Roeck if (tzdata->index == index) { 2721597b374SGuenter Roeck thermal_zone_device_update(tzdata->tzd, 2731597b374SGuenter Roeck THERMAL_EVENT_UNSPECIFIED); 2741597b374SGuenter Roeck } 2751597b374SGuenter Roeck } 2761597b374SGuenter Roeck } 2771597b374SGuenter Roeck 278d560168bSGuenter Roeck #else 27944e3ad88SAkinobu Mita static int hwmon_thermal_register_sensors(struct device *dev) 280d560168bSGuenter Roeck { 281d560168bSGuenter Roeck return 0; 282d560168bSGuenter Roeck } 2831597b374SGuenter Roeck 2841597b374SGuenter Roeck static void hwmon_thermal_notify(struct device *dev, int index) { } 2851597b374SGuenter Roeck 28686430c1aSGuenter Roeck #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */ 287d560168bSGuenter Roeck 28861b8ab2cSNicolin Chen static int hwmon_attr_base(enum hwmon_sensor_types type) 28961b8ab2cSNicolin Chen { 2904413405fSDr. David Alan Gilbert if (type == hwmon_in || type == hwmon_intrusion) 29161b8ab2cSNicolin Chen return 0; 29261b8ab2cSNicolin Chen return 1; 29361b8ab2cSNicolin Chen } 29461b8ab2cSNicolin Chen 295d560168bSGuenter Roeck /* sysfs attribute management */ 296d560168bSGuenter Roeck 297d560168bSGuenter Roeck static ssize_t hwmon_attr_show(struct device *dev, 298d560168bSGuenter Roeck struct device_attribute *devattr, char *buf) 299d560168bSGuenter Roeck { 300d560168bSGuenter Roeck struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); 301d560168bSGuenter Roeck long val; 302d560168bSGuenter Roeck int ret; 303d560168bSGuenter Roeck 304d560168bSGuenter Roeck ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index, 305d560168bSGuenter Roeck &val); 306d560168bSGuenter Roeck if (ret < 0) 307d560168bSGuenter Roeck return ret; 308d560168bSGuenter Roeck 30961b8ab2cSNicolin Chen trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type), 31061b8ab2cSNicolin Chen hattr->name, val); 31161b8ab2cSNicolin Chen 312d560168bSGuenter Roeck return sprintf(buf, "%ld\n", val); 313d560168bSGuenter Roeck } 314d560168bSGuenter Roeck 315e159ab5cSGuenter Roeck static ssize_t hwmon_attr_show_string(struct device *dev, 316e159ab5cSGuenter Roeck struct device_attribute *devattr, 317e159ab5cSGuenter Roeck char *buf) 318e159ab5cSGuenter Roeck { 319e159ab5cSGuenter Roeck struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); 32061b8ab2cSNicolin Chen enum hwmon_sensor_types type = hattr->type; 3215ba6bcbcSJean Delvare const char *s; 322e159ab5cSGuenter Roeck int ret; 323e159ab5cSGuenter Roeck 324e159ab5cSGuenter Roeck ret = hattr->ops->read_string(dev, hattr->type, hattr->attr, 325e159ab5cSGuenter Roeck hattr->index, &s); 326e159ab5cSGuenter Roeck if (ret < 0) 327e159ab5cSGuenter Roeck return ret; 328e159ab5cSGuenter Roeck 32961b8ab2cSNicolin Chen trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type), 33061b8ab2cSNicolin Chen hattr->name, s); 33161b8ab2cSNicolin Chen 332e159ab5cSGuenter Roeck return sprintf(buf, "%s\n", s); 333e159ab5cSGuenter Roeck } 334e159ab5cSGuenter Roeck 335d560168bSGuenter Roeck static ssize_t hwmon_attr_store(struct device *dev, 336d560168bSGuenter Roeck struct device_attribute *devattr, 337d560168bSGuenter Roeck const char *buf, size_t count) 338d560168bSGuenter Roeck { 339d560168bSGuenter Roeck struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); 340d560168bSGuenter Roeck long val; 341d560168bSGuenter Roeck int ret; 342d560168bSGuenter Roeck 343d560168bSGuenter Roeck ret = kstrtol(buf, 10, &val); 344d560168bSGuenter Roeck if (ret < 0) 345d560168bSGuenter Roeck return ret; 346d560168bSGuenter Roeck 347d560168bSGuenter Roeck ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index, 348d560168bSGuenter Roeck val); 349d560168bSGuenter Roeck if (ret < 0) 350d560168bSGuenter Roeck return ret; 351d560168bSGuenter Roeck 35261b8ab2cSNicolin Chen trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type), 35361b8ab2cSNicolin Chen hattr->name, val); 354d560168bSGuenter Roeck 35561b8ab2cSNicolin Chen return count; 356d560168bSGuenter Roeck } 357d560168bSGuenter Roeck 358e159ab5cSGuenter Roeck static bool is_string_attr(enum hwmon_sensor_types type, u32 attr) 359e159ab5cSGuenter Roeck { 360e159ab5cSGuenter Roeck return (type == hwmon_temp && attr == hwmon_temp_label) || 361e159ab5cSGuenter Roeck (type == hwmon_in && attr == hwmon_in_label) || 362e159ab5cSGuenter Roeck (type == hwmon_curr && attr == hwmon_curr_label) || 363e159ab5cSGuenter Roeck (type == hwmon_power && attr == hwmon_power_label) || 364e159ab5cSGuenter Roeck (type == hwmon_energy && attr == hwmon_energy_label) || 365e159ab5cSGuenter Roeck (type == hwmon_humidity && attr == hwmon_humidity_label) || 366e159ab5cSGuenter Roeck (type == hwmon_fan && attr == hwmon_fan_label); 367e159ab5cSGuenter Roeck } 368e159ab5cSGuenter Roeck 3693bf8bdcfSGuenter Roeck static struct attribute *hwmon_genattr(const void *drvdata, 370d560168bSGuenter Roeck enum hwmon_sensor_types type, 371d560168bSGuenter Roeck u32 attr, 372d560168bSGuenter Roeck int index, 373d560168bSGuenter Roeck const char *template, 374d560168bSGuenter Roeck const struct hwmon_ops *ops) 375d560168bSGuenter Roeck { 376d560168bSGuenter Roeck struct hwmon_device_attribute *hattr; 377d560168bSGuenter Roeck struct device_attribute *dattr; 378d560168bSGuenter Roeck struct attribute *a; 379d560168bSGuenter Roeck umode_t mode; 3803b443defSRasmus Villemoes const char *name; 381e159ab5cSGuenter Roeck bool is_string = is_string_attr(type, attr); 382d560168bSGuenter Roeck 383d560168bSGuenter Roeck /* The attribute is invisible if there is no template string */ 384d560168bSGuenter Roeck if (!template) 385d560168bSGuenter Roeck return ERR_PTR(-ENOENT); 386d560168bSGuenter Roeck 387d560168bSGuenter Roeck mode = ops->is_visible(drvdata, type, attr, index); 388d560168bSGuenter Roeck if (!mode) 389d560168bSGuenter Roeck return ERR_PTR(-ENOENT); 390d560168bSGuenter Roeck 3910d87116fSGuenter Roeck if ((mode & 0444) && ((is_string && !ops->read_string) || 392e159ab5cSGuenter Roeck (!is_string && !ops->read))) 393d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 3940d87116fSGuenter Roeck if ((mode & 0222) && !ops->write) 395d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 396d560168bSGuenter Roeck 3973bf8bdcfSGuenter Roeck hattr = kzalloc(sizeof(*hattr), GFP_KERNEL); 398d560168bSGuenter Roeck if (!hattr) 399d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 400d560168bSGuenter Roeck 4013a412d5eSGuenter Roeck if (type == hwmon_chip) { 4023b443defSRasmus Villemoes name = template; 4033a412d5eSGuenter Roeck } else { 4043a412d5eSGuenter Roeck scnprintf(hattr->name, sizeof(hattr->name), template, 4053a412d5eSGuenter Roeck index + hwmon_attr_base(type)); 4063a412d5eSGuenter Roeck name = hattr->name; 4073a412d5eSGuenter Roeck } 4083a412d5eSGuenter Roeck 409d560168bSGuenter Roeck hattr->type = type; 410d560168bSGuenter Roeck hattr->attr = attr; 411d560168bSGuenter Roeck hattr->index = index; 412d560168bSGuenter Roeck hattr->ops = ops; 413d560168bSGuenter Roeck 414d560168bSGuenter Roeck dattr = &hattr->dev_attr; 415e159ab5cSGuenter Roeck dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show; 416d560168bSGuenter Roeck dattr->store = hwmon_attr_store; 417d560168bSGuenter Roeck 418d560168bSGuenter Roeck a = &dattr->attr; 419d560168bSGuenter Roeck sysfs_attr_init(a); 420d560168bSGuenter Roeck a->name = name; 421d560168bSGuenter Roeck a->mode = mode; 422d560168bSGuenter Roeck 423d560168bSGuenter Roeck return a; 424d560168bSGuenter Roeck } 425d560168bSGuenter Roeck 426f4d325d5SGuenter Roeck /* 427f4d325d5SGuenter Roeck * Chip attributes are not attribute templates but actual sysfs attributes. 428f4d325d5SGuenter Roeck * See hwmon_genattr() for special handling. 429f4d325d5SGuenter Roeck */ 430f4d325d5SGuenter Roeck static const char * const hwmon_chip_attrs[] = { 431d560168bSGuenter Roeck [hwmon_chip_temp_reset_history] = "temp_reset_history", 43200d616cfSGuenter Roeck [hwmon_chip_in_reset_history] = "in_reset_history", 4339b26947cSGuenter Roeck [hwmon_chip_curr_reset_history] = "curr_reset_history", 434b308f5c7SGuenter Roeck [hwmon_chip_power_reset_history] = "power_reset_history", 435d560168bSGuenter Roeck [hwmon_chip_update_interval] = "update_interval", 436d560168bSGuenter Roeck [hwmon_chip_alarms] = "alarms", 4379f00995eSGuenter Roeck [hwmon_chip_samples] = "samples", 4389f00995eSGuenter Roeck [hwmon_chip_curr_samples] = "curr_samples", 4399f00995eSGuenter Roeck [hwmon_chip_in_samples] = "in_samples", 4409f00995eSGuenter Roeck [hwmon_chip_power_samples] = "power_samples", 4419f00995eSGuenter Roeck [hwmon_chip_temp_samples] = "temp_samples", 442d560168bSGuenter Roeck }; 443d560168bSGuenter Roeck 444d560168bSGuenter Roeck static const char * const hwmon_temp_attr_templates[] = { 445002c6b54SGuenter Roeck [hwmon_temp_enable] = "temp%d_enable", 446d560168bSGuenter Roeck [hwmon_temp_input] = "temp%d_input", 447d560168bSGuenter Roeck [hwmon_temp_type] = "temp%d_type", 448d560168bSGuenter Roeck [hwmon_temp_lcrit] = "temp%d_lcrit", 449d560168bSGuenter Roeck [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst", 450d560168bSGuenter Roeck [hwmon_temp_min] = "temp%d_min", 451d560168bSGuenter Roeck [hwmon_temp_min_hyst] = "temp%d_min_hyst", 452d560168bSGuenter Roeck [hwmon_temp_max] = "temp%d_max", 453d560168bSGuenter Roeck [hwmon_temp_max_hyst] = "temp%d_max_hyst", 454d560168bSGuenter Roeck [hwmon_temp_crit] = "temp%d_crit", 455d560168bSGuenter Roeck [hwmon_temp_crit_hyst] = "temp%d_crit_hyst", 456d560168bSGuenter Roeck [hwmon_temp_emergency] = "temp%d_emergency", 457d560168bSGuenter Roeck [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst", 458d560168bSGuenter Roeck [hwmon_temp_alarm] = "temp%d_alarm", 459d560168bSGuenter Roeck [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm", 460d560168bSGuenter Roeck [hwmon_temp_min_alarm] = "temp%d_min_alarm", 461d560168bSGuenter Roeck [hwmon_temp_max_alarm] = "temp%d_max_alarm", 462d560168bSGuenter Roeck [hwmon_temp_crit_alarm] = "temp%d_crit_alarm", 463d560168bSGuenter Roeck [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm", 464d560168bSGuenter Roeck [hwmon_temp_fault] = "temp%d_fault", 465d560168bSGuenter Roeck [hwmon_temp_offset] = "temp%d_offset", 466d560168bSGuenter Roeck [hwmon_temp_label] = "temp%d_label", 467d560168bSGuenter Roeck [hwmon_temp_lowest] = "temp%d_lowest", 468d560168bSGuenter Roeck [hwmon_temp_highest] = "temp%d_highest", 469d560168bSGuenter Roeck [hwmon_temp_reset_history] = "temp%d_reset_history", 4701967f712SZbigniew Lukwinski [hwmon_temp_rated_min] = "temp%d_rated_min", 4711967f712SZbigniew Lukwinski [hwmon_temp_rated_max] = "temp%d_rated_max", 472d560168bSGuenter Roeck }; 473d560168bSGuenter Roeck 47400d616cfSGuenter Roeck static const char * const hwmon_in_attr_templates[] = { 475002c6b54SGuenter Roeck [hwmon_in_enable] = "in%d_enable", 47600d616cfSGuenter Roeck [hwmon_in_input] = "in%d_input", 47700d616cfSGuenter Roeck [hwmon_in_min] = "in%d_min", 47800d616cfSGuenter Roeck [hwmon_in_max] = "in%d_max", 47900d616cfSGuenter Roeck [hwmon_in_lcrit] = "in%d_lcrit", 48000d616cfSGuenter Roeck [hwmon_in_crit] = "in%d_crit", 48100d616cfSGuenter Roeck [hwmon_in_average] = "in%d_average", 48200d616cfSGuenter Roeck [hwmon_in_lowest] = "in%d_lowest", 48300d616cfSGuenter Roeck [hwmon_in_highest] = "in%d_highest", 48400d616cfSGuenter Roeck [hwmon_in_reset_history] = "in%d_reset_history", 48500d616cfSGuenter Roeck [hwmon_in_label] = "in%d_label", 48600d616cfSGuenter Roeck [hwmon_in_alarm] = "in%d_alarm", 48700d616cfSGuenter Roeck [hwmon_in_min_alarm] = "in%d_min_alarm", 48800d616cfSGuenter Roeck [hwmon_in_max_alarm] = "in%d_max_alarm", 48900d616cfSGuenter Roeck [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm", 49000d616cfSGuenter Roeck [hwmon_in_crit_alarm] = "in%d_crit_alarm", 4911967f712SZbigniew Lukwinski [hwmon_in_rated_min] = "in%d_rated_min", 4921967f712SZbigniew Lukwinski [hwmon_in_rated_max] = "in%d_rated_max", 49300d616cfSGuenter Roeck }; 49400d616cfSGuenter Roeck 4959b26947cSGuenter Roeck static const char * const hwmon_curr_attr_templates[] = { 496002c6b54SGuenter Roeck [hwmon_curr_enable] = "curr%d_enable", 4979b26947cSGuenter Roeck [hwmon_curr_input] = "curr%d_input", 4989b26947cSGuenter Roeck [hwmon_curr_min] = "curr%d_min", 4999b26947cSGuenter Roeck [hwmon_curr_max] = "curr%d_max", 5009b26947cSGuenter Roeck [hwmon_curr_lcrit] = "curr%d_lcrit", 5019b26947cSGuenter Roeck [hwmon_curr_crit] = "curr%d_crit", 5029b26947cSGuenter Roeck [hwmon_curr_average] = "curr%d_average", 5039b26947cSGuenter Roeck [hwmon_curr_lowest] = "curr%d_lowest", 5049b26947cSGuenter Roeck [hwmon_curr_highest] = "curr%d_highest", 5059b26947cSGuenter Roeck [hwmon_curr_reset_history] = "curr%d_reset_history", 5069b26947cSGuenter Roeck [hwmon_curr_label] = "curr%d_label", 5079b26947cSGuenter Roeck [hwmon_curr_alarm] = "curr%d_alarm", 5089b26947cSGuenter Roeck [hwmon_curr_min_alarm] = "curr%d_min_alarm", 5099b26947cSGuenter Roeck [hwmon_curr_max_alarm] = "curr%d_max_alarm", 5109b26947cSGuenter Roeck [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm", 5119b26947cSGuenter Roeck [hwmon_curr_crit_alarm] = "curr%d_crit_alarm", 5121967f712SZbigniew Lukwinski [hwmon_curr_rated_min] = "curr%d_rated_min", 5131967f712SZbigniew Lukwinski [hwmon_curr_rated_max] = "curr%d_rated_max", 5149b26947cSGuenter Roeck }; 5159b26947cSGuenter Roeck 516b308f5c7SGuenter Roeck static const char * const hwmon_power_attr_templates[] = { 517002c6b54SGuenter Roeck [hwmon_power_enable] = "power%d_enable", 518b308f5c7SGuenter Roeck [hwmon_power_average] = "power%d_average", 519b308f5c7SGuenter Roeck [hwmon_power_average_interval] = "power%d_average_interval", 520b308f5c7SGuenter Roeck [hwmon_power_average_interval_max] = "power%d_interval_max", 521b308f5c7SGuenter Roeck [hwmon_power_average_interval_min] = "power%d_interval_min", 522b308f5c7SGuenter Roeck [hwmon_power_average_highest] = "power%d_average_highest", 523b308f5c7SGuenter Roeck [hwmon_power_average_lowest] = "power%d_average_lowest", 524b308f5c7SGuenter Roeck [hwmon_power_average_max] = "power%d_average_max", 525b308f5c7SGuenter Roeck [hwmon_power_average_min] = "power%d_average_min", 526b308f5c7SGuenter Roeck [hwmon_power_input] = "power%d_input", 527b308f5c7SGuenter Roeck [hwmon_power_input_highest] = "power%d_input_highest", 528b308f5c7SGuenter Roeck [hwmon_power_input_lowest] = "power%d_input_lowest", 529b308f5c7SGuenter Roeck [hwmon_power_reset_history] = "power%d_reset_history", 530b308f5c7SGuenter Roeck [hwmon_power_accuracy] = "power%d_accuracy", 531b308f5c7SGuenter Roeck [hwmon_power_cap] = "power%d_cap", 532b308f5c7SGuenter Roeck [hwmon_power_cap_hyst] = "power%d_cap_hyst", 533b308f5c7SGuenter Roeck [hwmon_power_cap_max] = "power%d_cap_max", 534b308f5c7SGuenter Roeck [hwmon_power_cap_min] = "power%d_cap_min", 535aa7f29b0SAndrew Lunn [hwmon_power_min] = "power%d_min", 536b308f5c7SGuenter Roeck [hwmon_power_max] = "power%d_max", 537aa7f29b0SAndrew Lunn [hwmon_power_lcrit] = "power%d_lcrit", 538b308f5c7SGuenter Roeck [hwmon_power_crit] = "power%d_crit", 539b308f5c7SGuenter Roeck [hwmon_power_label] = "power%d_label", 540b308f5c7SGuenter Roeck [hwmon_power_alarm] = "power%d_alarm", 541b308f5c7SGuenter Roeck [hwmon_power_cap_alarm] = "power%d_cap_alarm", 542aa7f29b0SAndrew Lunn [hwmon_power_min_alarm] = "power%d_min_alarm", 543b308f5c7SGuenter Roeck [hwmon_power_max_alarm] = "power%d_max_alarm", 544aa7f29b0SAndrew Lunn [hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm", 545b308f5c7SGuenter Roeck [hwmon_power_crit_alarm] = "power%d_crit_alarm", 5461967f712SZbigniew Lukwinski [hwmon_power_rated_min] = "power%d_rated_min", 5471967f712SZbigniew Lukwinski [hwmon_power_rated_max] = "power%d_rated_max", 548b308f5c7SGuenter Roeck }; 549b308f5c7SGuenter Roeck 5506bfcca44SGuenter Roeck static const char * const hwmon_energy_attr_templates[] = { 551002c6b54SGuenter Roeck [hwmon_energy_enable] = "energy%d_enable", 5526bfcca44SGuenter Roeck [hwmon_energy_input] = "energy%d_input", 5536bfcca44SGuenter Roeck [hwmon_energy_label] = "energy%d_label", 5546bfcca44SGuenter Roeck }; 5556bfcca44SGuenter Roeck 5566bfcca44SGuenter Roeck static const char * const hwmon_humidity_attr_templates[] = { 557002c6b54SGuenter Roeck [hwmon_humidity_enable] = "humidity%d_enable", 5586bfcca44SGuenter Roeck [hwmon_humidity_input] = "humidity%d_input", 5596bfcca44SGuenter Roeck [hwmon_humidity_label] = "humidity%d_label", 5606bfcca44SGuenter Roeck [hwmon_humidity_min] = "humidity%d_min", 5616bfcca44SGuenter Roeck [hwmon_humidity_min_hyst] = "humidity%d_min_hyst", 5626bfcca44SGuenter Roeck [hwmon_humidity_max] = "humidity%d_max", 5636bfcca44SGuenter Roeck [hwmon_humidity_max_hyst] = "humidity%d_max_hyst", 5646bfcca44SGuenter Roeck [hwmon_humidity_alarm] = "humidity%d_alarm", 5656bfcca44SGuenter Roeck [hwmon_humidity_fault] = "humidity%d_fault", 5661967f712SZbigniew Lukwinski [hwmon_humidity_rated_min] = "humidity%d_rated_min", 5671967f712SZbigniew Lukwinski [hwmon_humidity_rated_max] = "humidity%d_rated_max", 5686bfcca44SGuenter Roeck }; 5696bfcca44SGuenter Roeck 5708faee73fSGuenter Roeck static const char * const hwmon_fan_attr_templates[] = { 571002c6b54SGuenter Roeck [hwmon_fan_enable] = "fan%d_enable", 5728faee73fSGuenter Roeck [hwmon_fan_input] = "fan%d_input", 5738faee73fSGuenter Roeck [hwmon_fan_label] = "fan%d_label", 5748faee73fSGuenter Roeck [hwmon_fan_min] = "fan%d_min", 5758faee73fSGuenter Roeck [hwmon_fan_max] = "fan%d_max", 5768faee73fSGuenter Roeck [hwmon_fan_div] = "fan%d_div", 5778faee73fSGuenter Roeck [hwmon_fan_pulses] = "fan%d_pulses", 5788faee73fSGuenter Roeck [hwmon_fan_target] = "fan%d_target", 5798faee73fSGuenter Roeck [hwmon_fan_alarm] = "fan%d_alarm", 5808faee73fSGuenter Roeck [hwmon_fan_min_alarm] = "fan%d_min_alarm", 5818faee73fSGuenter Roeck [hwmon_fan_max_alarm] = "fan%d_max_alarm", 5828faee73fSGuenter Roeck [hwmon_fan_fault] = "fan%d_fault", 5838faee73fSGuenter Roeck }; 5848faee73fSGuenter Roeck 585f9f7bb3aSGuenter Roeck static const char * const hwmon_pwm_attr_templates[] = { 586f9f7bb3aSGuenter Roeck [hwmon_pwm_input] = "pwm%d", 587f9f7bb3aSGuenter Roeck [hwmon_pwm_enable] = "pwm%d_enable", 588f9f7bb3aSGuenter Roeck [hwmon_pwm_mode] = "pwm%d_mode", 589f9f7bb3aSGuenter Roeck [hwmon_pwm_freq] = "pwm%d_freq", 590f9f7bb3aSGuenter Roeck }; 591f9f7bb3aSGuenter Roeck 5924413405fSDr. David Alan Gilbert static const char * const hwmon_intrusion_attr_templates[] = { 5934413405fSDr. David Alan Gilbert [hwmon_intrusion_alarm] = "intrusion%d_alarm", 5944413405fSDr. David Alan Gilbert [hwmon_intrusion_beep] = "intrusion%d_beep", 5954413405fSDr. David Alan Gilbert }; 5964413405fSDr. David Alan Gilbert 597d560168bSGuenter Roeck static const char * const *__templates[] = { 598f4d325d5SGuenter Roeck [hwmon_chip] = hwmon_chip_attrs, 599d560168bSGuenter Roeck [hwmon_temp] = hwmon_temp_attr_templates, 60000d616cfSGuenter Roeck [hwmon_in] = hwmon_in_attr_templates, 6019b26947cSGuenter Roeck [hwmon_curr] = hwmon_curr_attr_templates, 602b308f5c7SGuenter Roeck [hwmon_power] = hwmon_power_attr_templates, 6036bfcca44SGuenter Roeck [hwmon_energy] = hwmon_energy_attr_templates, 6046bfcca44SGuenter Roeck [hwmon_humidity] = hwmon_humidity_attr_templates, 6058faee73fSGuenter Roeck [hwmon_fan] = hwmon_fan_attr_templates, 606f9f7bb3aSGuenter Roeck [hwmon_pwm] = hwmon_pwm_attr_templates, 6074413405fSDr. David Alan Gilbert [hwmon_intrusion] = hwmon_intrusion_attr_templates, 608d560168bSGuenter Roeck }; 609d560168bSGuenter Roeck 610d560168bSGuenter Roeck static const int __templates_size[] = { 611f4d325d5SGuenter Roeck [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs), 612d560168bSGuenter Roeck [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates), 61300d616cfSGuenter Roeck [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates), 6149b26947cSGuenter Roeck [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates), 615b308f5c7SGuenter Roeck [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates), 6166bfcca44SGuenter Roeck [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates), 6176bfcca44SGuenter Roeck [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates), 6188faee73fSGuenter Roeck [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates), 619f9f7bb3aSGuenter Roeck [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates), 6204413405fSDr. David Alan Gilbert [hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates), 621d560168bSGuenter Roeck }; 622d560168bSGuenter Roeck 6231597b374SGuenter Roeck int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type, 6241597b374SGuenter Roeck u32 attr, int channel) 6251597b374SGuenter Roeck { 6261597b374SGuenter Roeck char sattr[MAX_SYSFS_ATTR_NAME_LENGTH]; 6271597b374SGuenter Roeck const char * const *templates; 6281597b374SGuenter Roeck const char *template; 6291597b374SGuenter Roeck int base; 6301597b374SGuenter Roeck 6311597b374SGuenter Roeck if (type >= ARRAY_SIZE(__templates)) 6321597b374SGuenter Roeck return -EINVAL; 6331597b374SGuenter Roeck if (attr >= __templates_size[type]) 6341597b374SGuenter Roeck return -EINVAL; 6351597b374SGuenter Roeck 6361597b374SGuenter Roeck templates = __templates[type]; 6371597b374SGuenter Roeck template = templates[attr]; 6381597b374SGuenter Roeck 6391597b374SGuenter Roeck base = hwmon_attr_base(type); 6401597b374SGuenter Roeck 6411597b374SGuenter Roeck scnprintf(sattr, MAX_SYSFS_ATTR_NAME_LENGTH, template, base + channel); 6421597b374SGuenter Roeck sysfs_notify(&dev->kobj, NULL, sattr); 6431597b374SGuenter Roeck kobject_uevent(&dev->kobj, KOBJ_CHANGE); 6441597b374SGuenter Roeck 6451597b374SGuenter Roeck if (type == hwmon_temp) 6461597b374SGuenter Roeck hwmon_thermal_notify(dev, channel); 6471597b374SGuenter Roeck 6481597b374SGuenter Roeck return 0; 6491597b374SGuenter Roeck } 6501597b374SGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_notify_event); 6511597b374SGuenter Roeck 652d560168bSGuenter Roeck static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info) 653d560168bSGuenter Roeck { 654d560168bSGuenter Roeck int i, n; 655d560168bSGuenter Roeck 656d560168bSGuenter Roeck for (i = n = 0; info->config[i]; i++) 657d560168bSGuenter Roeck n += hweight32(info->config[i]); 658d560168bSGuenter Roeck 659d560168bSGuenter Roeck return n; 660d560168bSGuenter Roeck } 661d560168bSGuenter Roeck 6623bf8bdcfSGuenter Roeck static int hwmon_genattrs(const void *drvdata, 663d560168bSGuenter Roeck struct attribute **attrs, 664d560168bSGuenter Roeck const struct hwmon_ops *ops, 665d560168bSGuenter Roeck const struct hwmon_channel_info *info) 666d560168bSGuenter Roeck { 667d560168bSGuenter Roeck const char * const *templates; 668d560168bSGuenter Roeck int template_size; 669d560168bSGuenter Roeck int i, aindex = 0; 670d560168bSGuenter Roeck 671d560168bSGuenter Roeck if (info->type >= ARRAY_SIZE(__templates)) 672d560168bSGuenter Roeck return -EINVAL; 673d560168bSGuenter Roeck 674d560168bSGuenter Roeck templates = __templates[info->type]; 675d560168bSGuenter Roeck template_size = __templates_size[info->type]; 676d560168bSGuenter Roeck 677d560168bSGuenter Roeck for (i = 0; info->config[i]; i++) { 678d560168bSGuenter Roeck u32 attr_mask = info->config[i]; 679d560168bSGuenter Roeck u32 attr; 680d560168bSGuenter Roeck 681d560168bSGuenter Roeck while (attr_mask) { 682d560168bSGuenter Roeck struct attribute *a; 683d560168bSGuenter Roeck 684d560168bSGuenter Roeck attr = __ffs(attr_mask); 685d560168bSGuenter Roeck attr_mask &= ~BIT(attr); 686d560168bSGuenter Roeck if (attr >= template_size) 687d560168bSGuenter Roeck return -EINVAL; 6883bf8bdcfSGuenter Roeck a = hwmon_genattr(drvdata, info->type, attr, i, 689d560168bSGuenter Roeck templates[attr], ops); 690d560168bSGuenter Roeck if (IS_ERR(a)) { 691d560168bSGuenter Roeck if (PTR_ERR(a) != -ENOENT) 692d560168bSGuenter Roeck return PTR_ERR(a); 693d560168bSGuenter Roeck continue; 694d560168bSGuenter Roeck } 695d560168bSGuenter Roeck attrs[aindex++] = a; 696d560168bSGuenter Roeck } 697d560168bSGuenter Roeck } 698d560168bSGuenter Roeck return aindex; 699d560168bSGuenter Roeck } 700d560168bSGuenter Roeck 701d560168bSGuenter Roeck static struct attribute ** 7023bf8bdcfSGuenter Roeck __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip) 703d560168bSGuenter Roeck { 704d560168bSGuenter Roeck int ret, i, aindex = 0, nattrs = 0; 705d560168bSGuenter Roeck struct attribute **attrs; 706d560168bSGuenter Roeck 707d560168bSGuenter Roeck for (i = 0; chip->info[i]; i++) 708d560168bSGuenter Roeck nattrs += hwmon_num_channel_attrs(chip->info[i]); 709d560168bSGuenter Roeck 710d560168bSGuenter Roeck if (nattrs == 0) 711d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 712d560168bSGuenter Roeck 7133bf8bdcfSGuenter Roeck attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL); 714d560168bSGuenter Roeck if (!attrs) 715d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 716d560168bSGuenter Roeck 717d560168bSGuenter Roeck for (i = 0; chip->info[i]; i++) { 7183bf8bdcfSGuenter Roeck ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops, 719d560168bSGuenter Roeck chip->info[i]); 7203bf8bdcfSGuenter Roeck if (ret < 0) { 7213bf8bdcfSGuenter Roeck hwmon_free_attrs(attrs); 722d560168bSGuenter Roeck return ERR_PTR(ret); 7233bf8bdcfSGuenter Roeck } 724d560168bSGuenter Roeck aindex += ret; 725d560168bSGuenter Roeck } 726d560168bSGuenter Roeck 727d560168bSGuenter Roeck return attrs; 728d560168bSGuenter Roeck } 729d560168bSGuenter Roeck 730d560168bSGuenter Roeck static struct device * 731d560168bSGuenter Roeck __hwmon_device_register(struct device *dev, const char *name, void *drvdata, 732d560168bSGuenter Roeck const struct hwmon_chip_info *chip, 733d560168bSGuenter Roeck const struct attribute_group **groups) 734d560168bSGuenter Roeck { 735d560168bSGuenter Roeck struct hwmon_device *hwdev; 736d560168bSGuenter Roeck struct device *hdev; 73744e3ad88SAkinobu Mita int i, err, id; 738d560168bSGuenter Roeck 73974d3b641SGuenter Roeck /* Complain about invalid characters in hwmon name attribute */ 740d560168bSGuenter Roeck if (name && (!strlen(name) || strpbrk(name, "-* \t\n"))) 74174d3b641SGuenter Roeck dev_warn(dev, 74274d3b641SGuenter Roeck "hwmon: '%s' is not a valid name attribute, please fix\n", 74374d3b641SGuenter Roeck name); 744d560168bSGuenter Roeck 745d560168bSGuenter Roeck id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL); 746d560168bSGuenter Roeck if (id < 0) 747d560168bSGuenter Roeck return ERR_PTR(id); 748d560168bSGuenter Roeck 749d560168bSGuenter Roeck hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL); 750d560168bSGuenter Roeck if (hwdev == NULL) { 751d560168bSGuenter Roeck err = -ENOMEM; 752d560168bSGuenter Roeck goto ida_remove; 753d560168bSGuenter Roeck } 754d560168bSGuenter Roeck 755d560168bSGuenter Roeck hdev = &hwdev->dev; 756d560168bSGuenter Roeck 757239552f4SGuenter Roeck if (chip) { 758d560168bSGuenter Roeck struct attribute **attrs; 759b2a4cc3aSGuenter Roeck int ngroups = 2; /* terminating NULL plus &hwdev->groups */ 760d560168bSGuenter Roeck 761d560168bSGuenter Roeck if (groups) 762d560168bSGuenter Roeck for (i = 0; groups[i]; i++) 763d560168bSGuenter Roeck ngroups++; 764d560168bSGuenter Roeck 7653bf8bdcfSGuenter Roeck hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL); 76638d8ed65SColin Ian King if (!hwdev->groups) { 76738d8ed65SColin Ian King err = -ENOMEM; 76838d8ed65SColin Ian King goto free_hwmon; 76938d8ed65SColin Ian King } 770d560168bSGuenter Roeck 7713bf8bdcfSGuenter Roeck attrs = __hwmon_create_attrs(drvdata, chip); 772d560168bSGuenter Roeck if (IS_ERR(attrs)) { 773d560168bSGuenter Roeck err = PTR_ERR(attrs); 774d560168bSGuenter Roeck goto free_hwmon; 775d560168bSGuenter Roeck } 776d560168bSGuenter Roeck 777d560168bSGuenter Roeck hwdev->group.attrs = attrs; 778d560168bSGuenter Roeck ngroups = 0; 779d560168bSGuenter Roeck hwdev->groups[ngroups++] = &hwdev->group; 780d560168bSGuenter Roeck 781d560168bSGuenter Roeck if (groups) { 782d560168bSGuenter Roeck for (i = 0; groups[i]; i++) 783d560168bSGuenter Roeck hwdev->groups[ngroups++] = groups[i]; 784d560168bSGuenter Roeck } 785d560168bSGuenter Roeck 786d560168bSGuenter Roeck hdev->groups = hwdev->groups; 787d560168bSGuenter Roeck } else { 788d560168bSGuenter Roeck hdev->groups = groups; 789d560168bSGuenter Roeck } 790d560168bSGuenter Roeck 791d560168bSGuenter Roeck hwdev->name = name; 792d560168bSGuenter Roeck hdev->class = &hwmon_class; 793d560168bSGuenter Roeck hdev->parent = dev; 794d560168bSGuenter Roeck hdev->of_node = dev ? dev->of_node : NULL; 795d560168bSGuenter Roeck hwdev->chip = chip; 796d560168bSGuenter Roeck dev_set_drvdata(hdev, drvdata); 797d560168bSGuenter Roeck dev_set_name(hdev, HWMON_ID_FORMAT, id); 798d560168bSGuenter Roeck err = device_register(hdev); 799d560168bSGuenter Roeck if (err) 800d560168bSGuenter Roeck goto free_hwmon; 801d560168bSGuenter Roeck 8021597b374SGuenter Roeck INIT_LIST_HEAD(&hwdev->tzdata); 8031597b374SGuenter Roeck 804c41dd48eSEduardo Valentin if (dev && dev->of_node && chip && chip->ops->read && 805d560168bSGuenter Roeck chip->info[0]->type == hwmon_chip && 806d560168bSGuenter Roeck (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) { 80744e3ad88SAkinobu Mita err = hwmon_thermal_register_sensors(hdev); 80874e35127SDmitry Osipenko if (err) { 80974e35127SDmitry Osipenko device_unregister(hdev); 810792eac18SGuenter Roeck /* 81144e3ad88SAkinobu Mita * Don't worry about hwdev; hwmon_dev_release(), called 81244e3ad88SAkinobu Mita * from device_unregister(), will free it. 813792eac18SGuenter Roeck */ 81474e35127SDmitry Osipenko goto ida_remove; 81574e35127SDmitry Osipenko } 81647c332deSLinus Walleij } 817d560168bSGuenter Roeck 818d560168bSGuenter Roeck return hdev; 819d560168bSGuenter Roeck 820d560168bSGuenter Roeck free_hwmon: 8213bf8bdcfSGuenter Roeck hwmon_dev_release(hdev); 822d560168bSGuenter Roeck ida_remove: 823d560168bSGuenter Roeck ida_simple_remove(&hwmon_ida, id); 824d560168bSGuenter Roeck return ERR_PTR(err); 825d560168bSGuenter Roeck } 826d560168bSGuenter Roeck 8271236441fSMark M. Hoffman /** 828bab2243cSGuenter Roeck * hwmon_device_register_with_groups - register w/ hwmon 829bab2243cSGuenter Roeck * @dev: the parent device 830bab2243cSGuenter Roeck * @name: hwmon name attribute 831bab2243cSGuenter Roeck * @drvdata: driver data to attach to created device 832bab2243cSGuenter Roeck * @groups: List of attribute groups to create 833bab2243cSGuenter Roeck * 834bab2243cSGuenter Roeck * hwmon_device_unregister() must be called when the device is no 835bab2243cSGuenter Roeck * longer needed. 836bab2243cSGuenter Roeck * 837bab2243cSGuenter Roeck * Returns the pointer to the new device. 838bab2243cSGuenter Roeck */ 839bab2243cSGuenter Roeck struct device * 840bab2243cSGuenter Roeck hwmon_device_register_with_groups(struct device *dev, const char *name, 841bab2243cSGuenter Roeck void *drvdata, 842bab2243cSGuenter Roeck const struct attribute_group **groups) 843bab2243cSGuenter Roeck { 8448353863aSGuenter Roeck if (!name) 8458353863aSGuenter Roeck return ERR_PTR(-EINVAL); 8468353863aSGuenter Roeck 847d560168bSGuenter Roeck return __hwmon_device_register(dev, name, drvdata, NULL, groups); 848bab2243cSGuenter Roeck } 849bab2243cSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups); 850bab2243cSGuenter Roeck 851bab2243cSGuenter Roeck /** 852d560168bSGuenter Roeck * hwmon_device_register_with_info - register w/ hwmon 853d560168bSGuenter Roeck * @dev: the parent device 854d560168bSGuenter Roeck * @name: hwmon name attribute 855d560168bSGuenter Roeck * @drvdata: driver data to attach to created device 8563870945aSGuenter Roeck * @chip: pointer to hwmon chip information 857848ba0a2SGuenter Roeck * @extra_groups: pointer to list of additional non-standard attribute groups 858d560168bSGuenter Roeck * 859d560168bSGuenter Roeck * hwmon_device_unregister() must be called when the device is no 860d560168bSGuenter Roeck * longer needed. 861d560168bSGuenter Roeck * 862d560168bSGuenter Roeck * Returns the pointer to the new device. 863d560168bSGuenter Roeck */ 864d560168bSGuenter Roeck struct device * 865d560168bSGuenter Roeck hwmon_device_register_with_info(struct device *dev, const char *name, 866d560168bSGuenter Roeck void *drvdata, 867d560168bSGuenter Roeck const struct hwmon_chip_info *chip, 868848ba0a2SGuenter Roeck const struct attribute_group **extra_groups) 869d560168bSGuenter Roeck { 8708353863aSGuenter Roeck if (!name) 8718353863aSGuenter Roeck return ERR_PTR(-EINVAL); 8728353863aSGuenter Roeck 873239552f4SGuenter Roeck if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info)) 874d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 875d560168bSGuenter Roeck 87659df4f4eSLucas Magasweran if (chip && !dev) 87759df4f4eSLucas Magasweran return ERR_PTR(-EINVAL); 87859df4f4eSLucas Magasweran 879848ba0a2SGuenter Roeck return __hwmon_device_register(dev, name, drvdata, chip, extra_groups); 880d560168bSGuenter Roeck } 881d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_info); 882d560168bSGuenter Roeck 883d560168bSGuenter Roeck /** 8841beeffe4STony Jones * hwmon_device_register - register w/ hwmon 8851236441fSMark M. Hoffman * @dev: the device to register 8861236441fSMark M. Hoffman * 8871beeffe4STony Jones * hwmon_device_unregister() must be called when the device is no 8881236441fSMark M. Hoffman * longer needed. 8891236441fSMark M. Hoffman * 8901beeffe4STony Jones * Returns the pointer to the new device. 8911236441fSMark M. Hoffman */ 8921beeffe4STony Jones struct device *hwmon_device_register(struct device *dev) 8931236441fSMark M. Hoffman { 894af1bd36cSGuenter Roeck dev_warn(dev, 895af1bd36cSGuenter Roeck "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n"); 896af1bd36cSGuenter Roeck 8978353863aSGuenter Roeck return __hwmon_device_register(dev, NULL, NULL, NULL, NULL); 8981236441fSMark M. Hoffman } 899839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_register); 9001236441fSMark M. Hoffman 9011236441fSMark M. Hoffman /** 9021236441fSMark M. Hoffman * hwmon_device_unregister - removes the previously registered class device 9031236441fSMark M. Hoffman * 9041beeffe4STony Jones * @dev: the class device to destroy 9051236441fSMark M. Hoffman */ 9061beeffe4STony Jones void hwmon_device_unregister(struct device *dev) 9071236441fSMark M. Hoffman { 9081236441fSMark M. Hoffman int id; 9091236441fSMark M. Hoffman 910739cf3a2SKay Sievers if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) { 9111beeffe4STony Jones device_unregister(dev); 9124ca5f468SJonathan Cameron ida_simple_remove(&hwmon_ida, id); 9131236441fSMark M. Hoffman } else 9141beeffe4STony Jones dev_dbg(dev->parent, 9151236441fSMark M. Hoffman "hwmon_device_unregister() failed: bad class ID!\n"); 9161236441fSMark M. Hoffman } 917839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_unregister); 9181236441fSMark M. Hoffman 91974188cbaSGuenter Roeck static void devm_hwmon_release(struct device *dev, void *res) 92074188cbaSGuenter Roeck { 92174188cbaSGuenter Roeck struct device *hwdev = *(struct device **)res; 92274188cbaSGuenter Roeck 92374188cbaSGuenter Roeck hwmon_device_unregister(hwdev); 92474188cbaSGuenter Roeck } 92574188cbaSGuenter Roeck 92674188cbaSGuenter Roeck /** 92774188cbaSGuenter Roeck * devm_hwmon_device_register_with_groups - register w/ hwmon 92874188cbaSGuenter Roeck * @dev: the parent device 92974188cbaSGuenter Roeck * @name: hwmon name attribute 93074188cbaSGuenter Roeck * @drvdata: driver data to attach to created device 93174188cbaSGuenter Roeck * @groups: List of attribute groups to create 93274188cbaSGuenter Roeck * 93374188cbaSGuenter Roeck * Returns the pointer to the new device. The new device is automatically 93474188cbaSGuenter Roeck * unregistered with the parent device. 93574188cbaSGuenter Roeck */ 93674188cbaSGuenter Roeck struct device * 93774188cbaSGuenter Roeck devm_hwmon_device_register_with_groups(struct device *dev, const char *name, 93874188cbaSGuenter Roeck void *drvdata, 93974188cbaSGuenter Roeck const struct attribute_group **groups) 94074188cbaSGuenter Roeck { 94174188cbaSGuenter Roeck struct device **ptr, *hwdev; 94274188cbaSGuenter Roeck 94374188cbaSGuenter Roeck if (!dev) 94474188cbaSGuenter Roeck return ERR_PTR(-EINVAL); 94574188cbaSGuenter Roeck 94674188cbaSGuenter Roeck ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); 94774188cbaSGuenter Roeck if (!ptr) 94874188cbaSGuenter Roeck return ERR_PTR(-ENOMEM); 94974188cbaSGuenter Roeck 95074188cbaSGuenter Roeck hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups); 95174188cbaSGuenter Roeck if (IS_ERR(hwdev)) 95274188cbaSGuenter Roeck goto error; 95374188cbaSGuenter Roeck 95474188cbaSGuenter Roeck *ptr = hwdev; 95574188cbaSGuenter Roeck devres_add(dev, ptr); 95674188cbaSGuenter Roeck return hwdev; 95774188cbaSGuenter Roeck 95874188cbaSGuenter Roeck error: 95974188cbaSGuenter Roeck devres_free(ptr); 96074188cbaSGuenter Roeck return hwdev; 96174188cbaSGuenter Roeck } 96274188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups); 96374188cbaSGuenter Roeck 964d560168bSGuenter Roeck /** 965d560168bSGuenter Roeck * devm_hwmon_device_register_with_info - register w/ hwmon 966d560168bSGuenter Roeck * @dev: the parent device 967d560168bSGuenter Roeck * @name: hwmon name attribute 968d560168bSGuenter Roeck * @drvdata: driver data to attach to created device 9693870945aSGuenter Roeck * @chip: pointer to hwmon chip information 9703870945aSGuenter Roeck * @groups: pointer to list of driver specific attribute groups 971d560168bSGuenter Roeck * 972d560168bSGuenter Roeck * Returns the pointer to the new device. The new device is automatically 973d560168bSGuenter Roeck * unregistered with the parent device. 974d560168bSGuenter Roeck */ 975d560168bSGuenter Roeck struct device * 976d560168bSGuenter Roeck devm_hwmon_device_register_with_info(struct device *dev, const char *name, 977d560168bSGuenter Roeck void *drvdata, 978d560168bSGuenter Roeck const struct hwmon_chip_info *chip, 979d560168bSGuenter Roeck const struct attribute_group **groups) 980d560168bSGuenter Roeck { 981d560168bSGuenter Roeck struct device **ptr, *hwdev; 982d560168bSGuenter Roeck 983d560168bSGuenter Roeck if (!dev) 984d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 985d560168bSGuenter Roeck 986d560168bSGuenter Roeck ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); 987d560168bSGuenter Roeck if (!ptr) 988d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 989d560168bSGuenter Roeck 990d560168bSGuenter Roeck hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip, 991d560168bSGuenter Roeck groups); 992d560168bSGuenter Roeck if (IS_ERR(hwdev)) 993d560168bSGuenter Roeck goto error; 994d560168bSGuenter Roeck 995d560168bSGuenter Roeck *ptr = hwdev; 996d560168bSGuenter Roeck devres_add(dev, ptr); 997d560168bSGuenter Roeck 998d560168bSGuenter Roeck return hwdev; 999d560168bSGuenter Roeck 1000d560168bSGuenter Roeck error: 1001d560168bSGuenter Roeck devres_free(ptr); 1002d560168bSGuenter Roeck return hwdev; 1003d560168bSGuenter Roeck } 1004d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info); 1005d560168bSGuenter Roeck 100674188cbaSGuenter Roeck static int devm_hwmon_match(struct device *dev, void *res, void *data) 100774188cbaSGuenter Roeck { 100874188cbaSGuenter Roeck struct device **hwdev = res; 100974188cbaSGuenter Roeck 101074188cbaSGuenter Roeck return *hwdev == data; 101174188cbaSGuenter Roeck } 101274188cbaSGuenter Roeck 101374188cbaSGuenter Roeck /** 101474188cbaSGuenter Roeck * devm_hwmon_device_unregister - removes a previously registered hwmon device 101574188cbaSGuenter Roeck * 101674188cbaSGuenter Roeck * @dev: the parent device of the device to unregister 101774188cbaSGuenter Roeck */ 101874188cbaSGuenter Roeck void devm_hwmon_device_unregister(struct device *dev) 101974188cbaSGuenter Roeck { 102074188cbaSGuenter Roeck WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev)); 102174188cbaSGuenter Roeck } 102274188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister); 102374188cbaSGuenter Roeck 10242958b1ecSJean Delvare static void __init hwmon_pci_quirks(void) 10252958b1ecSJean Delvare { 10262958b1ecSJean Delvare #if defined CONFIG_X86 && defined CONFIG_PCI 10272958b1ecSJean Delvare struct pci_dev *sb; 10282958b1ecSJean Delvare u16 base; 10292958b1ecSJean Delvare u8 enable; 10302958b1ecSJean Delvare 10312958b1ecSJean Delvare /* Open access to 0x295-0x296 on MSI MS-7031 */ 10322958b1ecSJean Delvare sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL); 1033d6dab7ddSJean Delvare if (sb) { 1034d6dab7ddSJean Delvare if (sb->subsystem_vendor == 0x1462 && /* MSI */ 1035d6dab7ddSJean Delvare sb->subsystem_device == 0x0031) { /* MS-7031 */ 10362958b1ecSJean Delvare pci_read_config_byte(sb, 0x48, &enable); 10372958b1ecSJean Delvare pci_read_config_word(sb, 0x64, &base); 10382958b1ecSJean Delvare 10392958b1ecSJean Delvare if (base == 0 && !(enable & BIT(2))) { 10402958b1ecSJean Delvare dev_info(&sb->dev, 10412958b1ecSJean Delvare "Opening wide generic port at 0x295\n"); 10422958b1ecSJean Delvare pci_write_config_word(sb, 0x64, 0x295); 1043d6dab7ddSJean Delvare pci_write_config_byte(sb, 0x48, 1044d6dab7ddSJean Delvare enable | BIT(2)); 10452958b1ecSJean Delvare } 10462958b1ecSJean Delvare } 1047d6dab7ddSJean Delvare pci_dev_put(sb); 1048d6dab7ddSJean Delvare } 10492958b1ecSJean Delvare #endif 10502958b1ecSJean Delvare } 10512958b1ecSJean Delvare 10521236441fSMark M. Hoffman static int __init hwmon_init(void) 10531236441fSMark M. Hoffman { 1054bab2243cSGuenter Roeck int err; 1055bab2243cSGuenter Roeck 10562958b1ecSJean Delvare hwmon_pci_quirks(); 10572958b1ecSJean Delvare 1058bab2243cSGuenter Roeck err = class_register(&hwmon_class); 1059bab2243cSGuenter Roeck if (err) { 1060bab2243cSGuenter Roeck pr_err("couldn't register hwmon sysfs class\n"); 1061bab2243cSGuenter Roeck return err; 10621236441fSMark M. Hoffman } 10631236441fSMark M. Hoffman return 0; 10641236441fSMark M. Hoffman } 10651236441fSMark M. Hoffman 10661236441fSMark M. Hoffman static void __exit hwmon_exit(void) 10671236441fSMark M. Hoffman { 1068bab2243cSGuenter Roeck class_unregister(&hwmon_class); 10691236441fSMark M. Hoffman } 10701236441fSMark M. Hoffman 107137f54ee5SDavid Brownell subsys_initcall(hwmon_init); 10721236441fSMark M. Hoffman module_exit(hwmon_exit); 10731236441fSMark M. Hoffman 10741236441fSMark M. Hoffman MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>"); 10751236441fSMark M. Hoffman MODULE_DESCRIPTION("hardware monitoring sysfs/class support"); 10761236441fSMark M. Hoffman MODULE_LICENSE("GPL"); 10771236441fSMark M. Hoffman 1078