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> 18c9ebbe6fSGuenter Roeck #include <linux/module.h> 192958b1ecSJean Delvare #include <linux/pci.h> 20c9ebbe6fSGuenter Roeck #include <linux/slab.h> 21648cd48cSGuenter Roeck #include <linux/string.h> 22d560168bSGuenter Roeck #include <linux/thermal.h> 231236441fSMark M. Hoffman 2461b8ab2cSNicolin Chen #define CREATE_TRACE_POINTS 2561b8ab2cSNicolin Chen #include <trace/events/hwmon.h> 2661b8ab2cSNicolin Chen 271236441fSMark M. Hoffman #define HWMON_ID_PREFIX "hwmon" 281236441fSMark M. Hoffman #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d" 291236441fSMark M. Hoffman 30bab2243cSGuenter Roeck struct hwmon_device { 31bab2243cSGuenter Roeck const char *name; 32bab2243cSGuenter Roeck struct device dev; 33d560168bSGuenter Roeck const struct hwmon_chip_info *chip; 34d560168bSGuenter Roeck 35d560168bSGuenter Roeck struct attribute_group group; 36d560168bSGuenter Roeck const struct attribute_group **groups; 37bab2243cSGuenter Roeck }; 38d560168bSGuenter Roeck 39bab2243cSGuenter Roeck #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev) 40bab2243cSGuenter Roeck 413a412d5eSGuenter Roeck #define MAX_SYSFS_ATTR_NAME_LENGTH 32 423a412d5eSGuenter Roeck 43d560168bSGuenter Roeck struct hwmon_device_attribute { 44d560168bSGuenter Roeck struct device_attribute dev_attr; 45d560168bSGuenter Roeck const struct hwmon_ops *ops; 46d560168bSGuenter Roeck enum hwmon_sensor_types type; 47d560168bSGuenter Roeck u32 attr; 48d560168bSGuenter Roeck int index; 493a412d5eSGuenter Roeck char name[MAX_SYSFS_ATTR_NAME_LENGTH]; 50d560168bSGuenter Roeck }; 51d560168bSGuenter Roeck 52d560168bSGuenter Roeck #define to_hwmon_attr(d) \ 53d560168bSGuenter Roeck container_of(d, struct hwmon_device_attribute, dev_attr) 54d560168bSGuenter Roeck 55d560168bSGuenter Roeck /* 56d560168bSGuenter Roeck * Thermal zone information 57d560168bSGuenter Roeck * In addition to the reference to the hwmon device, 58d560168bSGuenter Roeck * also provides the sensor index. 59d560168bSGuenter Roeck */ 60d560168bSGuenter Roeck struct hwmon_thermal_data { 61d560168bSGuenter Roeck struct hwmon_device *hwdev; /* Reference to hwmon device */ 62d560168bSGuenter Roeck int index; /* sensor index */ 63d560168bSGuenter Roeck }; 64d560168bSGuenter Roeck 65bab2243cSGuenter Roeck static ssize_t 662ab0c6c5SJulia Lawall name_show(struct device *dev, struct device_attribute *attr, char *buf) 67bab2243cSGuenter Roeck { 68bab2243cSGuenter Roeck return sprintf(buf, "%s\n", to_hwmon_device(dev)->name); 69bab2243cSGuenter Roeck } 702ab0c6c5SJulia Lawall static DEVICE_ATTR_RO(name); 71bab2243cSGuenter Roeck 72bab2243cSGuenter Roeck static struct attribute *hwmon_dev_attrs[] = { 73bab2243cSGuenter Roeck &dev_attr_name.attr, 74bab2243cSGuenter Roeck NULL 75bab2243cSGuenter Roeck }; 76bab2243cSGuenter Roeck 77bab2243cSGuenter Roeck static umode_t hwmon_dev_name_is_visible(struct kobject *kobj, 78bab2243cSGuenter Roeck struct attribute *attr, int n) 79bab2243cSGuenter Roeck { 80bab2243cSGuenter Roeck struct device *dev = container_of(kobj, struct device, kobj); 81bab2243cSGuenter Roeck 82bab2243cSGuenter Roeck if (to_hwmon_device(dev)->name == NULL) 83bab2243cSGuenter Roeck return 0; 84bab2243cSGuenter Roeck 85bab2243cSGuenter Roeck return attr->mode; 86bab2243cSGuenter Roeck } 87bab2243cSGuenter Roeck 88524703acSArvind Yadav static const struct attribute_group hwmon_dev_attr_group = { 89bab2243cSGuenter Roeck .attrs = hwmon_dev_attrs, 90bab2243cSGuenter Roeck .is_visible = hwmon_dev_name_is_visible, 91bab2243cSGuenter Roeck }; 92bab2243cSGuenter Roeck 93bab2243cSGuenter Roeck static const struct attribute_group *hwmon_dev_attr_groups[] = { 94bab2243cSGuenter Roeck &hwmon_dev_attr_group, 95bab2243cSGuenter Roeck NULL 96bab2243cSGuenter Roeck }; 97bab2243cSGuenter Roeck 98bab2243cSGuenter Roeck static void hwmon_dev_release(struct device *dev) 99bab2243cSGuenter Roeck { 100bab2243cSGuenter Roeck kfree(to_hwmon_device(dev)); 101bab2243cSGuenter Roeck } 102bab2243cSGuenter Roeck 103bab2243cSGuenter Roeck static struct class hwmon_class = { 104bab2243cSGuenter Roeck .name = "hwmon", 105bab2243cSGuenter Roeck .owner = THIS_MODULE, 106bab2243cSGuenter Roeck .dev_groups = hwmon_dev_attr_groups, 107bab2243cSGuenter Roeck .dev_release = hwmon_dev_release, 108bab2243cSGuenter Roeck }; 1091236441fSMark M. Hoffman 1104ca5f468SJonathan Cameron static DEFINE_IDA(hwmon_ida); 1111236441fSMark M. Hoffman 112d560168bSGuenter Roeck /* Thermal zone handling */ 113d560168bSGuenter Roeck 11486430c1aSGuenter Roeck /* 11586430c1aSGuenter Roeck * The complex conditional is necessary to avoid a cyclic dependency 11686430c1aSGuenter Roeck * between hwmon and thermal_sys modules. 11786430c1aSGuenter Roeck */ 118f3735332SDaniel Lezcano #ifdef CONFIG_THERMAL_OF 119d560168bSGuenter Roeck static int hwmon_thermal_get_temp(void *data, int *temp) 120d560168bSGuenter Roeck { 121d560168bSGuenter Roeck struct hwmon_thermal_data *tdata = data; 122d560168bSGuenter Roeck struct hwmon_device *hwdev = tdata->hwdev; 123d560168bSGuenter Roeck int ret; 124d560168bSGuenter Roeck long t; 125d560168bSGuenter Roeck 126d560168bSGuenter Roeck ret = hwdev->chip->ops->read(&hwdev->dev, hwmon_temp, hwmon_temp_input, 127d560168bSGuenter Roeck tdata->index, &t); 128d560168bSGuenter Roeck if (ret < 0) 129d560168bSGuenter Roeck return ret; 130d560168bSGuenter Roeck 131d560168bSGuenter Roeck *temp = t; 132d560168bSGuenter Roeck 133d560168bSGuenter Roeck return 0; 134d560168bSGuenter Roeck } 135d560168bSGuenter Roeck 136c9920650SJulia Lawall static const struct thermal_zone_of_device_ops hwmon_thermal_ops = { 137d560168bSGuenter Roeck .get_temp = hwmon_thermal_get_temp, 138d560168bSGuenter Roeck }; 139d560168bSGuenter Roeck 140d560168bSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev, 141d560168bSGuenter Roeck struct hwmon_device *hwdev, int index) 142d560168bSGuenter Roeck { 143d560168bSGuenter Roeck struct hwmon_thermal_data *tdata; 14447c332deSLinus Walleij struct thermal_zone_device *tzd; 145d560168bSGuenter Roeck 146d560168bSGuenter Roeck tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL); 147d560168bSGuenter Roeck if (!tdata) 148d560168bSGuenter Roeck return -ENOMEM; 149d560168bSGuenter Roeck 150d560168bSGuenter Roeck tdata->hwdev = hwdev; 151d560168bSGuenter Roeck tdata->index = index; 152d560168bSGuenter Roeck 15347c332deSLinus Walleij tzd = devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata, 154d560168bSGuenter Roeck &hwmon_thermal_ops); 15547c332deSLinus Walleij /* 15647c332deSLinus Walleij * If CONFIG_THERMAL_OF is disabled, this returns -ENODEV, 15747c332deSLinus Walleij * so ignore that error but forward any other error. 15847c332deSLinus Walleij */ 15947c332deSLinus Walleij if (IS_ERR(tzd) && (PTR_ERR(tzd) != -ENODEV)) 16047c332deSLinus Walleij return PTR_ERR(tzd); 161d560168bSGuenter Roeck 162d560168bSGuenter Roeck return 0; 163d560168bSGuenter Roeck } 164d560168bSGuenter Roeck #else 165d560168bSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev, 166d560168bSGuenter Roeck struct hwmon_device *hwdev, int index) 167d560168bSGuenter Roeck { 168d560168bSGuenter Roeck return 0; 169d560168bSGuenter Roeck } 17086430c1aSGuenter Roeck #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */ 171d560168bSGuenter Roeck 17261b8ab2cSNicolin Chen static int hwmon_attr_base(enum hwmon_sensor_types type) 17361b8ab2cSNicolin Chen { 17461b8ab2cSNicolin Chen if (type == hwmon_in) 17561b8ab2cSNicolin Chen return 0; 17661b8ab2cSNicolin Chen return 1; 17761b8ab2cSNicolin Chen } 17861b8ab2cSNicolin Chen 179d560168bSGuenter Roeck /* sysfs attribute management */ 180d560168bSGuenter Roeck 181d560168bSGuenter Roeck static ssize_t hwmon_attr_show(struct device *dev, 182d560168bSGuenter Roeck struct device_attribute *devattr, char *buf) 183d560168bSGuenter Roeck { 184d560168bSGuenter Roeck struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); 185d560168bSGuenter Roeck long val; 186d560168bSGuenter Roeck int ret; 187d560168bSGuenter Roeck 188d560168bSGuenter Roeck ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index, 189d560168bSGuenter Roeck &val); 190d560168bSGuenter Roeck if (ret < 0) 191d560168bSGuenter Roeck return ret; 192d560168bSGuenter Roeck 19361b8ab2cSNicolin Chen trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type), 19461b8ab2cSNicolin Chen hattr->name, val); 19561b8ab2cSNicolin Chen 196d560168bSGuenter Roeck return sprintf(buf, "%ld\n", val); 197d560168bSGuenter Roeck } 198d560168bSGuenter Roeck 199e159ab5cSGuenter Roeck static ssize_t hwmon_attr_show_string(struct device *dev, 200e159ab5cSGuenter Roeck struct device_attribute *devattr, 201e159ab5cSGuenter Roeck char *buf) 202e159ab5cSGuenter Roeck { 203e159ab5cSGuenter Roeck struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); 20461b8ab2cSNicolin Chen enum hwmon_sensor_types type = hattr->type; 2055ba6bcbcSJean Delvare const char *s; 206e159ab5cSGuenter Roeck int ret; 207e159ab5cSGuenter Roeck 208e159ab5cSGuenter Roeck ret = hattr->ops->read_string(dev, hattr->type, hattr->attr, 209e159ab5cSGuenter Roeck hattr->index, &s); 210e159ab5cSGuenter Roeck if (ret < 0) 211e159ab5cSGuenter Roeck return ret; 212e159ab5cSGuenter Roeck 21361b8ab2cSNicolin Chen trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type), 21461b8ab2cSNicolin Chen hattr->name, s); 21561b8ab2cSNicolin Chen 216e159ab5cSGuenter Roeck return sprintf(buf, "%s\n", s); 217e159ab5cSGuenter Roeck } 218e159ab5cSGuenter Roeck 219d560168bSGuenter Roeck static ssize_t hwmon_attr_store(struct device *dev, 220d560168bSGuenter Roeck struct device_attribute *devattr, 221d560168bSGuenter Roeck const char *buf, size_t count) 222d560168bSGuenter Roeck { 223d560168bSGuenter Roeck struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); 224d560168bSGuenter Roeck long val; 225d560168bSGuenter Roeck int ret; 226d560168bSGuenter Roeck 227d560168bSGuenter Roeck ret = kstrtol(buf, 10, &val); 228d560168bSGuenter Roeck if (ret < 0) 229d560168bSGuenter Roeck return ret; 230d560168bSGuenter Roeck 231d560168bSGuenter Roeck ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index, 232d560168bSGuenter Roeck val); 233d560168bSGuenter Roeck if (ret < 0) 234d560168bSGuenter Roeck return ret; 235d560168bSGuenter Roeck 23661b8ab2cSNicolin Chen trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type), 23761b8ab2cSNicolin Chen hattr->name, val); 238d560168bSGuenter Roeck 23961b8ab2cSNicolin Chen return count; 240d560168bSGuenter Roeck } 241d560168bSGuenter Roeck 242e159ab5cSGuenter Roeck static bool is_string_attr(enum hwmon_sensor_types type, u32 attr) 243e159ab5cSGuenter Roeck { 244e159ab5cSGuenter Roeck return (type == hwmon_temp && attr == hwmon_temp_label) || 245e159ab5cSGuenter Roeck (type == hwmon_in && attr == hwmon_in_label) || 246e159ab5cSGuenter Roeck (type == hwmon_curr && attr == hwmon_curr_label) || 247e159ab5cSGuenter Roeck (type == hwmon_power && attr == hwmon_power_label) || 248e159ab5cSGuenter Roeck (type == hwmon_energy && attr == hwmon_energy_label) || 249e159ab5cSGuenter Roeck (type == hwmon_humidity && attr == hwmon_humidity_label) || 250e159ab5cSGuenter Roeck (type == hwmon_fan && attr == hwmon_fan_label); 251e159ab5cSGuenter Roeck } 252e159ab5cSGuenter Roeck 253d560168bSGuenter Roeck static struct attribute *hwmon_genattr(struct device *dev, 254d560168bSGuenter Roeck const void *drvdata, 255d560168bSGuenter Roeck enum hwmon_sensor_types type, 256d560168bSGuenter Roeck u32 attr, 257d560168bSGuenter Roeck int index, 258d560168bSGuenter Roeck const char *template, 259d560168bSGuenter Roeck const struct hwmon_ops *ops) 260d560168bSGuenter Roeck { 261d560168bSGuenter Roeck struct hwmon_device_attribute *hattr; 262d560168bSGuenter Roeck struct device_attribute *dattr; 263d560168bSGuenter Roeck struct attribute *a; 264d560168bSGuenter Roeck umode_t mode; 2653b443defSRasmus Villemoes const char *name; 266e159ab5cSGuenter Roeck bool is_string = is_string_attr(type, attr); 267d560168bSGuenter Roeck 268d560168bSGuenter Roeck /* The attribute is invisible if there is no template string */ 269d560168bSGuenter Roeck if (!template) 270d560168bSGuenter Roeck return ERR_PTR(-ENOENT); 271d560168bSGuenter Roeck 272d560168bSGuenter Roeck mode = ops->is_visible(drvdata, type, attr, index); 273d560168bSGuenter Roeck if (!mode) 274d560168bSGuenter Roeck return ERR_PTR(-ENOENT); 275d560168bSGuenter Roeck 2760d87116fSGuenter Roeck if ((mode & 0444) && ((is_string && !ops->read_string) || 277e159ab5cSGuenter Roeck (!is_string && !ops->read))) 278d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 2790d87116fSGuenter Roeck if ((mode & 0222) && !ops->write) 280d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 281d560168bSGuenter Roeck 282d560168bSGuenter Roeck hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL); 283d560168bSGuenter Roeck if (!hattr) 284d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 285d560168bSGuenter Roeck 2863a412d5eSGuenter Roeck if (type == hwmon_chip) { 2873b443defSRasmus Villemoes name = template; 2883a412d5eSGuenter Roeck } else { 2893a412d5eSGuenter Roeck scnprintf(hattr->name, sizeof(hattr->name), template, 2903a412d5eSGuenter Roeck index + hwmon_attr_base(type)); 2913a412d5eSGuenter Roeck name = hattr->name; 2923a412d5eSGuenter Roeck } 2933a412d5eSGuenter Roeck 294d560168bSGuenter Roeck hattr->type = type; 295d560168bSGuenter Roeck hattr->attr = attr; 296d560168bSGuenter Roeck hattr->index = index; 297d560168bSGuenter Roeck hattr->ops = ops; 298d560168bSGuenter Roeck 299d560168bSGuenter Roeck dattr = &hattr->dev_attr; 300e159ab5cSGuenter Roeck dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show; 301d560168bSGuenter Roeck dattr->store = hwmon_attr_store; 302d560168bSGuenter Roeck 303d560168bSGuenter Roeck a = &dattr->attr; 304d560168bSGuenter Roeck sysfs_attr_init(a); 305d560168bSGuenter Roeck a->name = name; 306d560168bSGuenter Roeck a->mode = mode; 307d560168bSGuenter Roeck 308d560168bSGuenter Roeck return a; 309d560168bSGuenter Roeck } 310d560168bSGuenter Roeck 311f4d325d5SGuenter Roeck /* 312f4d325d5SGuenter Roeck * Chip attributes are not attribute templates but actual sysfs attributes. 313f4d325d5SGuenter Roeck * See hwmon_genattr() for special handling. 314f4d325d5SGuenter Roeck */ 315f4d325d5SGuenter Roeck static const char * const hwmon_chip_attrs[] = { 316d560168bSGuenter Roeck [hwmon_chip_temp_reset_history] = "temp_reset_history", 31700d616cfSGuenter Roeck [hwmon_chip_in_reset_history] = "in_reset_history", 3189b26947cSGuenter Roeck [hwmon_chip_curr_reset_history] = "curr_reset_history", 319b308f5c7SGuenter Roeck [hwmon_chip_power_reset_history] = "power_reset_history", 320d560168bSGuenter Roeck [hwmon_chip_update_interval] = "update_interval", 321d560168bSGuenter Roeck [hwmon_chip_alarms] = "alarms", 3229f00995eSGuenter Roeck [hwmon_chip_samples] = "samples", 3239f00995eSGuenter Roeck [hwmon_chip_curr_samples] = "curr_samples", 3249f00995eSGuenter Roeck [hwmon_chip_in_samples] = "in_samples", 3259f00995eSGuenter Roeck [hwmon_chip_power_samples] = "power_samples", 3269f00995eSGuenter Roeck [hwmon_chip_temp_samples] = "temp_samples", 327d560168bSGuenter Roeck }; 328d560168bSGuenter Roeck 329d560168bSGuenter Roeck static const char * const hwmon_temp_attr_templates[] = { 330d560168bSGuenter Roeck [hwmon_temp_input] = "temp%d_input", 331d560168bSGuenter Roeck [hwmon_temp_type] = "temp%d_type", 332d560168bSGuenter Roeck [hwmon_temp_lcrit] = "temp%d_lcrit", 333d560168bSGuenter Roeck [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst", 334d560168bSGuenter Roeck [hwmon_temp_min] = "temp%d_min", 335d560168bSGuenter Roeck [hwmon_temp_min_hyst] = "temp%d_min_hyst", 336d560168bSGuenter Roeck [hwmon_temp_max] = "temp%d_max", 337d560168bSGuenter Roeck [hwmon_temp_max_hyst] = "temp%d_max_hyst", 338d560168bSGuenter Roeck [hwmon_temp_crit] = "temp%d_crit", 339d560168bSGuenter Roeck [hwmon_temp_crit_hyst] = "temp%d_crit_hyst", 340d560168bSGuenter Roeck [hwmon_temp_emergency] = "temp%d_emergency", 341d560168bSGuenter Roeck [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst", 342d560168bSGuenter Roeck [hwmon_temp_alarm] = "temp%d_alarm", 343d560168bSGuenter Roeck [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm", 344d560168bSGuenter Roeck [hwmon_temp_min_alarm] = "temp%d_min_alarm", 345d560168bSGuenter Roeck [hwmon_temp_max_alarm] = "temp%d_max_alarm", 346d560168bSGuenter Roeck [hwmon_temp_crit_alarm] = "temp%d_crit_alarm", 347d560168bSGuenter Roeck [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm", 348d560168bSGuenter Roeck [hwmon_temp_fault] = "temp%d_fault", 349d560168bSGuenter Roeck [hwmon_temp_offset] = "temp%d_offset", 350d560168bSGuenter Roeck [hwmon_temp_label] = "temp%d_label", 351d560168bSGuenter Roeck [hwmon_temp_lowest] = "temp%d_lowest", 352d560168bSGuenter Roeck [hwmon_temp_highest] = "temp%d_highest", 353d560168bSGuenter Roeck [hwmon_temp_reset_history] = "temp%d_reset_history", 354d560168bSGuenter Roeck }; 355d560168bSGuenter Roeck 35600d616cfSGuenter Roeck static const char * const hwmon_in_attr_templates[] = { 35700d616cfSGuenter Roeck [hwmon_in_input] = "in%d_input", 35800d616cfSGuenter Roeck [hwmon_in_min] = "in%d_min", 35900d616cfSGuenter Roeck [hwmon_in_max] = "in%d_max", 36000d616cfSGuenter Roeck [hwmon_in_lcrit] = "in%d_lcrit", 36100d616cfSGuenter Roeck [hwmon_in_crit] = "in%d_crit", 36200d616cfSGuenter Roeck [hwmon_in_average] = "in%d_average", 36300d616cfSGuenter Roeck [hwmon_in_lowest] = "in%d_lowest", 36400d616cfSGuenter Roeck [hwmon_in_highest] = "in%d_highest", 36500d616cfSGuenter Roeck [hwmon_in_reset_history] = "in%d_reset_history", 36600d616cfSGuenter Roeck [hwmon_in_label] = "in%d_label", 36700d616cfSGuenter Roeck [hwmon_in_alarm] = "in%d_alarm", 36800d616cfSGuenter Roeck [hwmon_in_min_alarm] = "in%d_min_alarm", 36900d616cfSGuenter Roeck [hwmon_in_max_alarm] = "in%d_max_alarm", 37000d616cfSGuenter Roeck [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm", 37100d616cfSGuenter Roeck [hwmon_in_crit_alarm] = "in%d_crit_alarm", 37268c0d69dSNicolin Chen [hwmon_in_enable] = "in%d_enable", 37300d616cfSGuenter Roeck }; 37400d616cfSGuenter Roeck 3759b26947cSGuenter Roeck static const char * const hwmon_curr_attr_templates[] = { 3769b26947cSGuenter Roeck [hwmon_curr_input] = "curr%d_input", 3779b26947cSGuenter Roeck [hwmon_curr_min] = "curr%d_min", 3789b26947cSGuenter Roeck [hwmon_curr_max] = "curr%d_max", 3799b26947cSGuenter Roeck [hwmon_curr_lcrit] = "curr%d_lcrit", 3809b26947cSGuenter Roeck [hwmon_curr_crit] = "curr%d_crit", 3819b26947cSGuenter Roeck [hwmon_curr_average] = "curr%d_average", 3829b26947cSGuenter Roeck [hwmon_curr_lowest] = "curr%d_lowest", 3839b26947cSGuenter Roeck [hwmon_curr_highest] = "curr%d_highest", 3849b26947cSGuenter Roeck [hwmon_curr_reset_history] = "curr%d_reset_history", 3859b26947cSGuenter Roeck [hwmon_curr_label] = "curr%d_label", 3869b26947cSGuenter Roeck [hwmon_curr_alarm] = "curr%d_alarm", 3879b26947cSGuenter Roeck [hwmon_curr_min_alarm] = "curr%d_min_alarm", 3889b26947cSGuenter Roeck [hwmon_curr_max_alarm] = "curr%d_max_alarm", 3899b26947cSGuenter Roeck [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm", 3909b26947cSGuenter Roeck [hwmon_curr_crit_alarm] = "curr%d_crit_alarm", 3919b26947cSGuenter Roeck }; 3929b26947cSGuenter Roeck 393b308f5c7SGuenter Roeck static const char * const hwmon_power_attr_templates[] = { 394b308f5c7SGuenter Roeck [hwmon_power_average] = "power%d_average", 395b308f5c7SGuenter Roeck [hwmon_power_average_interval] = "power%d_average_interval", 396b308f5c7SGuenter Roeck [hwmon_power_average_interval_max] = "power%d_interval_max", 397b308f5c7SGuenter Roeck [hwmon_power_average_interval_min] = "power%d_interval_min", 398b308f5c7SGuenter Roeck [hwmon_power_average_highest] = "power%d_average_highest", 399b308f5c7SGuenter Roeck [hwmon_power_average_lowest] = "power%d_average_lowest", 400b308f5c7SGuenter Roeck [hwmon_power_average_max] = "power%d_average_max", 401b308f5c7SGuenter Roeck [hwmon_power_average_min] = "power%d_average_min", 402b308f5c7SGuenter Roeck [hwmon_power_input] = "power%d_input", 403b308f5c7SGuenter Roeck [hwmon_power_input_highest] = "power%d_input_highest", 404b308f5c7SGuenter Roeck [hwmon_power_input_lowest] = "power%d_input_lowest", 405b308f5c7SGuenter Roeck [hwmon_power_reset_history] = "power%d_reset_history", 406b308f5c7SGuenter Roeck [hwmon_power_accuracy] = "power%d_accuracy", 407b308f5c7SGuenter Roeck [hwmon_power_cap] = "power%d_cap", 408b308f5c7SGuenter Roeck [hwmon_power_cap_hyst] = "power%d_cap_hyst", 409b308f5c7SGuenter Roeck [hwmon_power_cap_max] = "power%d_cap_max", 410b308f5c7SGuenter Roeck [hwmon_power_cap_min] = "power%d_cap_min", 411aa7f29b0SAndrew Lunn [hwmon_power_min] = "power%d_min", 412b308f5c7SGuenter Roeck [hwmon_power_max] = "power%d_max", 413aa7f29b0SAndrew Lunn [hwmon_power_lcrit] = "power%d_lcrit", 414b308f5c7SGuenter Roeck [hwmon_power_crit] = "power%d_crit", 415b308f5c7SGuenter Roeck [hwmon_power_label] = "power%d_label", 416b308f5c7SGuenter Roeck [hwmon_power_alarm] = "power%d_alarm", 417b308f5c7SGuenter Roeck [hwmon_power_cap_alarm] = "power%d_cap_alarm", 418aa7f29b0SAndrew Lunn [hwmon_power_min_alarm] = "power%d_min_alarm", 419b308f5c7SGuenter Roeck [hwmon_power_max_alarm] = "power%d_max_alarm", 420aa7f29b0SAndrew Lunn [hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm", 421b308f5c7SGuenter Roeck [hwmon_power_crit_alarm] = "power%d_crit_alarm", 422b308f5c7SGuenter Roeck }; 423b308f5c7SGuenter Roeck 4246bfcca44SGuenter Roeck static const char * const hwmon_energy_attr_templates[] = { 4256bfcca44SGuenter Roeck [hwmon_energy_input] = "energy%d_input", 4266bfcca44SGuenter Roeck [hwmon_energy_label] = "energy%d_label", 4276bfcca44SGuenter Roeck }; 4286bfcca44SGuenter Roeck 4296bfcca44SGuenter Roeck static const char * const hwmon_humidity_attr_templates[] = { 4306bfcca44SGuenter Roeck [hwmon_humidity_input] = "humidity%d_input", 4316bfcca44SGuenter Roeck [hwmon_humidity_label] = "humidity%d_label", 4326bfcca44SGuenter Roeck [hwmon_humidity_min] = "humidity%d_min", 4336bfcca44SGuenter Roeck [hwmon_humidity_min_hyst] = "humidity%d_min_hyst", 4346bfcca44SGuenter Roeck [hwmon_humidity_max] = "humidity%d_max", 4356bfcca44SGuenter Roeck [hwmon_humidity_max_hyst] = "humidity%d_max_hyst", 4366bfcca44SGuenter Roeck [hwmon_humidity_alarm] = "humidity%d_alarm", 4376bfcca44SGuenter Roeck [hwmon_humidity_fault] = "humidity%d_fault", 4386bfcca44SGuenter Roeck }; 4396bfcca44SGuenter Roeck 4408faee73fSGuenter Roeck static const char * const hwmon_fan_attr_templates[] = { 4418faee73fSGuenter Roeck [hwmon_fan_input] = "fan%d_input", 4428faee73fSGuenter Roeck [hwmon_fan_label] = "fan%d_label", 4438faee73fSGuenter Roeck [hwmon_fan_min] = "fan%d_min", 4448faee73fSGuenter Roeck [hwmon_fan_max] = "fan%d_max", 4458faee73fSGuenter Roeck [hwmon_fan_div] = "fan%d_div", 4468faee73fSGuenter Roeck [hwmon_fan_pulses] = "fan%d_pulses", 4478faee73fSGuenter Roeck [hwmon_fan_target] = "fan%d_target", 4488faee73fSGuenter Roeck [hwmon_fan_alarm] = "fan%d_alarm", 4498faee73fSGuenter Roeck [hwmon_fan_min_alarm] = "fan%d_min_alarm", 4508faee73fSGuenter Roeck [hwmon_fan_max_alarm] = "fan%d_max_alarm", 4518faee73fSGuenter Roeck [hwmon_fan_fault] = "fan%d_fault", 4528faee73fSGuenter Roeck }; 4538faee73fSGuenter Roeck 454f9f7bb3aSGuenter Roeck static const char * const hwmon_pwm_attr_templates[] = { 455f9f7bb3aSGuenter Roeck [hwmon_pwm_input] = "pwm%d", 456f9f7bb3aSGuenter Roeck [hwmon_pwm_enable] = "pwm%d_enable", 457f9f7bb3aSGuenter Roeck [hwmon_pwm_mode] = "pwm%d_mode", 458f9f7bb3aSGuenter Roeck [hwmon_pwm_freq] = "pwm%d_freq", 459f9f7bb3aSGuenter Roeck }; 460f9f7bb3aSGuenter Roeck 461d560168bSGuenter Roeck static const char * const *__templates[] = { 462f4d325d5SGuenter Roeck [hwmon_chip] = hwmon_chip_attrs, 463d560168bSGuenter Roeck [hwmon_temp] = hwmon_temp_attr_templates, 46400d616cfSGuenter Roeck [hwmon_in] = hwmon_in_attr_templates, 4659b26947cSGuenter Roeck [hwmon_curr] = hwmon_curr_attr_templates, 466b308f5c7SGuenter Roeck [hwmon_power] = hwmon_power_attr_templates, 4676bfcca44SGuenter Roeck [hwmon_energy] = hwmon_energy_attr_templates, 4686bfcca44SGuenter Roeck [hwmon_humidity] = hwmon_humidity_attr_templates, 4698faee73fSGuenter Roeck [hwmon_fan] = hwmon_fan_attr_templates, 470f9f7bb3aSGuenter Roeck [hwmon_pwm] = hwmon_pwm_attr_templates, 471d560168bSGuenter Roeck }; 472d560168bSGuenter Roeck 473d560168bSGuenter Roeck static const int __templates_size[] = { 474f4d325d5SGuenter Roeck [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs), 475d560168bSGuenter Roeck [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates), 47600d616cfSGuenter Roeck [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates), 4779b26947cSGuenter Roeck [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates), 478b308f5c7SGuenter Roeck [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates), 4796bfcca44SGuenter Roeck [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates), 4806bfcca44SGuenter Roeck [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates), 4818faee73fSGuenter Roeck [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates), 482f9f7bb3aSGuenter Roeck [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates), 483d560168bSGuenter Roeck }; 484d560168bSGuenter Roeck 485d560168bSGuenter Roeck static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info) 486d560168bSGuenter Roeck { 487d560168bSGuenter Roeck int i, n; 488d560168bSGuenter Roeck 489d560168bSGuenter Roeck for (i = n = 0; info->config[i]; i++) 490d560168bSGuenter Roeck n += hweight32(info->config[i]); 491d560168bSGuenter Roeck 492d560168bSGuenter Roeck return n; 493d560168bSGuenter Roeck } 494d560168bSGuenter Roeck 495d560168bSGuenter Roeck static int hwmon_genattrs(struct device *dev, 496d560168bSGuenter Roeck const void *drvdata, 497d560168bSGuenter Roeck struct attribute **attrs, 498d560168bSGuenter Roeck const struct hwmon_ops *ops, 499d560168bSGuenter Roeck const struct hwmon_channel_info *info) 500d560168bSGuenter Roeck { 501d560168bSGuenter Roeck const char * const *templates; 502d560168bSGuenter Roeck int template_size; 503d560168bSGuenter Roeck int i, aindex = 0; 504d560168bSGuenter Roeck 505d560168bSGuenter Roeck if (info->type >= ARRAY_SIZE(__templates)) 506d560168bSGuenter Roeck return -EINVAL; 507d560168bSGuenter Roeck 508d560168bSGuenter Roeck templates = __templates[info->type]; 509d560168bSGuenter Roeck template_size = __templates_size[info->type]; 510d560168bSGuenter Roeck 511d560168bSGuenter Roeck for (i = 0; info->config[i]; i++) { 512d560168bSGuenter Roeck u32 attr_mask = info->config[i]; 513d560168bSGuenter Roeck u32 attr; 514d560168bSGuenter Roeck 515d560168bSGuenter Roeck while (attr_mask) { 516d560168bSGuenter Roeck struct attribute *a; 517d560168bSGuenter Roeck 518d560168bSGuenter Roeck attr = __ffs(attr_mask); 519d560168bSGuenter Roeck attr_mask &= ~BIT(attr); 520d560168bSGuenter Roeck if (attr >= template_size) 521d560168bSGuenter Roeck return -EINVAL; 522d560168bSGuenter Roeck a = hwmon_genattr(dev, drvdata, info->type, attr, i, 523d560168bSGuenter Roeck templates[attr], ops); 524d560168bSGuenter Roeck if (IS_ERR(a)) { 525d560168bSGuenter Roeck if (PTR_ERR(a) != -ENOENT) 526d560168bSGuenter Roeck return PTR_ERR(a); 527d560168bSGuenter Roeck continue; 528d560168bSGuenter Roeck } 529d560168bSGuenter Roeck attrs[aindex++] = a; 530d560168bSGuenter Roeck } 531d560168bSGuenter Roeck } 532d560168bSGuenter Roeck return aindex; 533d560168bSGuenter Roeck } 534d560168bSGuenter Roeck 535d560168bSGuenter Roeck static struct attribute ** 536d560168bSGuenter Roeck __hwmon_create_attrs(struct device *dev, const void *drvdata, 537d560168bSGuenter Roeck const struct hwmon_chip_info *chip) 538d560168bSGuenter Roeck { 539d560168bSGuenter Roeck int ret, i, aindex = 0, nattrs = 0; 540d560168bSGuenter Roeck struct attribute **attrs; 541d560168bSGuenter Roeck 542d560168bSGuenter Roeck for (i = 0; chip->info[i]; i++) 543d560168bSGuenter Roeck nattrs += hwmon_num_channel_attrs(chip->info[i]); 544d560168bSGuenter Roeck 545d560168bSGuenter Roeck if (nattrs == 0) 546d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 547d560168bSGuenter Roeck 548d560168bSGuenter Roeck attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL); 549d560168bSGuenter Roeck if (!attrs) 550d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 551d560168bSGuenter Roeck 552d560168bSGuenter Roeck for (i = 0; chip->info[i]; i++) { 553d560168bSGuenter Roeck ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops, 554d560168bSGuenter Roeck chip->info[i]); 555d560168bSGuenter Roeck if (ret < 0) 556d560168bSGuenter Roeck return ERR_PTR(ret); 557d560168bSGuenter Roeck aindex += ret; 558d560168bSGuenter Roeck } 559d560168bSGuenter Roeck 560d560168bSGuenter Roeck return attrs; 561d560168bSGuenter Roeck } 562d560168bSGuenter Roeck 563d560168bSGuenter Roeck static struct device * 564d560168bSGuenter Roeck __hwmon_device_register(struct device *dev, const char *name, void *drvdata, 565d560168bSGuenter Roeck const struct hwmon_chip_info *chip, 566d560168bSGuenter Roeck const struct attribute_group **groups) 567d560168bSGuenter Roeck { 568d560168bSGuenter Roeck struct hwmon_device *hwdev; 569d560168bSGuenter Roeck struct device *hdev; 570d560168bSGuenter Roeck int i, j, err, id; 571d560168bSGuenter Roeck 57274d3b641SGuenter Roeck /* Complain about invalid characters in hwmon name attribute */ 573d560168bSGuenter Roeck if (name && (!strlen(name) || strpbrk(name, "-* \t\n"))) 57474d3b641SGuenter Roeck dev_warn(dev, 57574d3b641SGuenter Roeck "hwmon: '%s' is not a valid name attribute, please fix\n", 57674d3b641SGuenter Roeck name); 577d560168bSGuenter Roeck 578d560168bSGuenter Roeck id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL); 579d560168bSGuenter Roeck if (id < 0) 580d560168bSGuenter Roeck return ERR_PTR(id); 581d560168bSGuenter Roeck 582d560168bSGuenter Roeck hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL); 583d560168bSGuenter Roeck if (hwdev == NULL) { 584d560168bSGuenter Roeck err = -ENOMEM; 585d560168bSGuenter Roeck goto ida_remove; 586d560168bSGuenter Roeck } 587d560168bSGuenter Roeck 588d560168bSGuenter Roeck hdev = &hwdev->dev; 589d560168bSGuenter Roeck 590239552f4SGuenter Roeck if (chip) { 591d560168bSGuenter Roeck struct attribute **attrs; 592b2a4cc3aSGuenter Roeck int ngroups = 2; /* terminating NULL plus &hwdev->groups */ 593d560168bSGuenter Roeck 594d560168bSGuenter Roeck if (groups) 595d560168bSGuenter Roeck for (i = 0; groups[i]; i++) 596d560168bSGuenter Roeck ngroups++; 597d560168bSGuenter Roeck 598d560168bSGuenter Roeck hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups), 599d560168bSGuenter Roeck GFP_KERNEL); 60038d8ed65SColin Ian King if (!hwdev->groups) { 60138d8ed65SColin Ian King err = -ENOMEM; 60238d8ed65SColin Ian King goto free_hwmon; 60338d8ed65SColin Ian King } 604d560168bSGuenter Roeck 605d560168bSGuenter Roeck attrs = __hwmon_create_attrs(dev, drvdata, chip); 606d560168bSGuenter Roeck if (IS_ERR(attrs)) { 607d560168bSGuenter Roeck err = PTR_ERR(attrs); 608d560168bSGuenter Roeck goto free_hwmon; 609d560168bSGuenter Roeck } 610d560168bSGuenter Roeck 611d560168bSGuenter Roeck hwdev->group.attrs = attrs; 612d560168bSGuenter Roeck ngroups = 0; 613d560168bSGuenter Roeck hwdev->groups[ngroups++] = &hwdev->group; 614d560168bSGuenter Roeck 615d560168bSGuenter Roeck if (groups) { 616d560168bSGuenter Roeck for (i = 0; groups[i]; i++) 617d560168bSGuenter Roeck hwdev->groups[ngroups++] = groups[i]; 618d560168bSGuenter Roeck } 619d560168bSGuenter Roeck 620d560168bSGuenter Roeck hdev->groups = hwdev->groups; 621d560168bSGuenter Roeck } else { 622d560168bSGuenter Roeck hdev->groups = groups; 623d560168bSGuenter Roeck } 624d560168bSGuenter Roeck 625d560168bSGuenter Roeck hwdev->name = name; 626d560168bSGuenter Roeck hdev->class = &hwmon_class; 627d560168bSGuenter Roeck hdev->parent = dev; 628d560168bSGuenter Roeck hdev->of_node = dev ? dev->of_node : NULL; 629d560168bSGuenter Roeck hwdev->chip = chip; 630d560168bSGuenter Roeck dev_set_drvdata(hdev, drvdata); 631d560168bSGuenter Roeck dev_set_name(hdev, HWMON_ID_FORMAT, id); 632d560168bSGuenter Roeck err = device_register(hdev); 633d560168bSGuenter Roeck if (err) 634d560168bSGuenter Roeck goto free_hwmon; 635d560168bSGuenter Roeck 636c41dd48eSEduardo Valentin if (dev && dev->of_node && chip && chip->ops->read && 637d560168bSGuenter Roeck chip->info[0]->type == hwmon_chip && 638d560168bSGuenter Roeck (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) { 639d560168bSGuenter Roeck const struct hwmon_channel_info **info = chip->info; 640d560168bSGuenter Roeck 641d560168bSGuenter Roeck for (i = 1; info[i]; i++) { 642d560168bSGuenter Roeck if (info[i]->type != hwmon_temp) 643d560168bSGuenter Roeck continue; 644d560168bSGuenter Roeck 645d560168bSGuenter Roeck for (j = 0; info[i]->config[j]; j++) { 646d560168bSGuenter Roeck if (!chip->ops->is_visible(drvdata, hwmon_temp, 647d560168bSGuenter Roeck hwmon_temp_input, j)) 648d560168bSGuenter Roeck continue; 64947c332deSLinus Walleij if (info[i]->config[j] & HWMON_T_INPUT) { 65047c332deSLinus Walleij err = hwmon_thermal_add_sensor(dev, 65147c332deSLinus Walleij hwdev, j); 65274e35127SDmitry Osipenko if (err) { 65374e35127SDmitry Osipenko device_unregister(hdev); 654*792eac18SGuenter Roeck /* 655*792eac18SGuenter Roeck * Don't worry about hwdev; 656*792eac18SGuenter Roeck * hwmon_dev_release(), called 657*792eac18SGuenter Roeck * from device_unregister(), 658*792eac18SGuenter Roeck * will free it. 659*792eac18SGuenter Roeck */ 66074e35127SDmitry Osipenko goto ida_remove; 66174e35127SDmitry Osipenko } 66247c332deSLinus Walleij } 663d560168bSGuenter Roeck } 664d560168bSGuenter Roeck } 665d560168bSGuenter Roeck } 666d560168bSGuenter Roeck 667d560168bSGuenter Roeck return hdev; 668d560168bSGuenter Roeck 669d560168bSGuenter Roeck free_hwmon: 670d560168bSGuenter Roeck kfree(hwdev); 671d560168bSGuenter Roeck ida_remove: 672d560168bSGuenter Roeck ida_simple_remove(&hwmon_ida, id); 673d560168bSGuenter Roeck return ERR_PTR(err); 674d560168bSGuenter Roeck } 675d560168bSGuenter Roeck 6761236441fSMark M. Hoffman /** 677bab2243cSGuenter Roeck * hwmon_device_register_with_groups - register w/ hwmon 678bab2243cSGuenter Roeck * @dev: the parent device 679bab2243cSGuenter Roeck * @name: hwmon name attribute 680bab2243cSGuenter Roeck * @drvdata: driver data to attach to created device 681bab2243cSGuenter Roeck * @groups: List of attribute groups to create 682bab2243cSGuenter Roeck * 683bab2243cSGuenter Roeck * hwmon_device_unregister() must be called when the device is no 684bab2243cSGuenter Roeck * longer needed. 685bab2243cSGuenter Roeck * 686bab2243cSGuenter Roeck * Returns the pointer to the new device. 687bab2243cSGuenter Roeck */ 688bab2243cSGuenter Roeck struct device * 689bab2243cSGuenter Roeck hwmon_device_register_with_groups(struct device *dev, const char *name, 690bab2243cSGuenter Roeck void *drvdata, 691bab2243cSGuenter Roeck const struct attribute_group **groups) 692bab2243cSGuenter Roeck { 6938353863aSGuenter Roeck if (!name) 6948353863aSGuenter Roeck return ERR_PTR(-EINVAL); 6958353863aSGuenter Roeck 696d560168bSGuenter Roeck return __hwmon_device_register(dev, name, drvdata, NULL, groups); 697bab2243cSGuenter Roeck } 698bab2243cSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups); 699bab2243cSGuenter Roeck 700bab2243cSGuenter Roeck /** 701d560168bSGuenter Roeck * hwmon_device_register_with_info - register w/ hwmon 702d560168bSGuenter Roeck * @dev: the parent device 703d560168bSGuenter Roeck * @name: hwmon name attribute 704d560168bSGuenter Roeck * @drvdata: driver data to attach to created device 7053870945aSGuenter Roeck * @chip: pointer to hwmon chip information 706848ba0a2SGuenter Roeck * @extra_groups: pointer to list of additional non-standard attribute groups 707d560168bSGuenter Roeck * 708d560168bSGuenter Roeck * hwmon_device_unregister() must be called when the device is no 709d560168bSGuenter Roeck * longer needed. 710d560168bSGuenter Roeck * 711d560168bSGuenter Roeck * Returns the pointer to the new device. 712d560168bSGuenter Roeck */ 713d560168bSGuenter Roeck struct device * 714d560168bSGuenter Roeck hwmon_device_register_with_info(struct device *dev, const char *name, 715d560168bSGuenter Roeck void *drvdata, 716d560168bSGuenter Roeck const struct hwmon_chip_info *chip, 717848ba0a2SGuenter Roeck const struct attribute_group **extra_groups) 718d560168bSGuenter Roeck { 7198353863aSGuenter Roeck if (!name) 7208353863aSGuenter Roeck return ERR_PTR(-EINVAL); 7218353863aSGuenter Roeck 722239552f4SGuenter Roeck if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info)) 723d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 724d560168bSGuenter Roeck 72559df4f4eSLucas Magasweran if (chip && !dev) 72659df4f4eSLucas Magasweran return ERR_PTR(-EINVAL); 72759df4f4eSLucas Magasweran 728848ba0a2SGuenter Roeck return __hwmon_device_register(dev, name, drvdata, chip, extra_groups); 729d560168bSGuenter Roeck } 730d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_info); 731d560168bSGuenter Roeck 732d560168bSGuenter Roeck /** 7331beeffe4STony Jones * hwmon_device_register - register w/ hwmon 7341236441fSMark M. Hoffman * @dev: the device to register 7351236441fSMark M. Hoffman * 7361beeffe4STony Jones * hwmon_device_unregister() must be called when the device is no 7371236441fSMark M. Hoffman * longer needed. 7381236441fSMark M. Hoffman * 7391beeffe4STony Jones * Returns the pointer to the new device. 7401236441fSMark M. Hoffman */ 7411beeffe4STony Jones struct device *hwmon_device_register(struct device *dev) 7421236441fSMark M. Hoffman { 743af1bd36cSGuenter Roeck dev_warn(dev, 744af1bd36cSGuenter Roeck "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n"); 745af1bd36cSGuenter Roeck 7468353863aSGuenter Roeck return __hwmon_device_register(dev, NULL, NULL, NULL, NULL); 7471236441fSMark M. Hoffman } 748839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_register); 7491236441fSMark M. Hoffman 7501236441fSMark M. Hoffman /** 7511236441fSMark M. Hoffman * hwmon_device_unregister - removes the previously registered class device 7521236441fSMark M. Hoffman * 7531beeffe4STony Jones * @dev: the class device to destroy 7541236441fSMark M. Hoffman */ 7551beeffe4STony Jones void hwmon_device_unregister(struct device *dev) 7561236441fSMark M. Hoffman { 7571236441fSMark M. Hoffman int id; 7581236441fSMark M. Hoffman 759739cf3a2SKay Sievers if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) { 7601beeffe4STony Jones device_unregister(dev); 7614ca5f468SJonathan Cameron ida_simple_remove(&hwmon_ida, id); 7621236441fSMark M. Hoffman } else 7631beeffe4STony Jones dev_dbg(dev->parent, 7641236441fSMark M. Hoffman "hwmon_device_unregister() failed: bad class ID!\n"); 7651236441fSMark M. Hoffman } 766839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_unregister); 7671236441fSMark M. Hoffman 76874188cbaSGuenter Roeck static void devm_hwmon_release(struct device *dev, void *res) 76974188cbaSGuenter Roeck { 77074188cbaSGuenter Roeck struct device *hwdev = *(struct device **)res; 77174188cbaSGuenter Roeck 77274188cbaSGuenter Roeck hwmon_device_unregister(hwdev); 77374188cbaSGuenter Roeck } 77474188cbaSGuenter Roeck 77574188cbaSGuenter Roeck /** 77674188cbaSGuenter Roeck * devm_hwmon_device_register_with_groups - register w/ hwmon 77774188cbaSGuenter Roeck * @dev: the parent device 77874188cbaSGuenter Roeck * @name: hwmon name attribute 77974188cbaSGuenter Roeck * @drvdata: driver data to attach to created device 78074188cbaSGuenter Roeck * @groups: List of attribute groups to create 78174188cbaSGuenter Roeck * 78274188cbaSGuenter Roeck * Returns the pointer to the new device. The new device is automatically 78374188cbaSGuenter Roeck * unregistered with the parent device. 78474188cbaSGuenter Roeck */ 78574188cbaSGuenter Roeck struct device * 78674188cbaSGuenter Roeck devm_hwmon_device_register_with_groups(struct device *dev, const char *name, 78774188cbaSGuenter Roeck void *drvdata, 78874188cbaSGuenter Roeck const struct attribute_group **groups) 78974188cbaSGuenter Roeck { 79074188cbaSGuenter Roeck struct device **ptr, *hwdev; 79174188cbaSGuenter Roeck 79274188cbaSGuenter Roeck if (!dev) 79374188cbaSGuenter Roeck return ERR_PTR(-EINVAL); 79474188cbaSGuenter Roeck 79574188cbaSGuenter Roeck ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); 79674188cbaSGuenter Roeck if (!ptr) 79774188cbaSGuenter Roeck return ERR_PTR(-ENOMEM); 79874188cbaSGuenter Roeck 79974188cbaSGuenter Roeck hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups); 80074188cbaSGuenter Roeck if (IS_ERR(hwdev)) 80174188cbaSGuenter Roeck goto error; 80274188cbaSGuenter Roeck 80374188cbaSGuenter Roeck *ptr = hwdev; 80474188cbaSGuenter Roeck devres_add(dev, ptr); 80574188cbaSGuenter Roeck return hwdev; 80674188cbaSGuenter Roeck 80774188cbaSGuenter Roeck error: 80874188cbaSGuenter Roeck devres_free(ptr); 80974188cbaSGuenter Roeck return hwdev; 81074188cbaSGuenter Roeck } 81174188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups); 81274188cbaSGuenter Roeck 813d560168bSGuenter Roeck /** 814d560168bSGuenter Roeck * devm_hwmon_device_register_with_info - register w/ hwmon 815d560168bSGuenter Roeck * @dev: the parent device 816d560168bSGuenter Roeck * @name: hwmon name attribute 817d560168bSGuenter Roeck * @drvdata: driver data to attach to created device 8183870945aSGuenter Roeck * @chip: pointer to hwmon chip information 8193870945aSGuenter Roeck * @groups: pointer to list of driver specific attribute groups 820d560168bSGuenter Roeck * 821d560168bSGuenter Roeck * Returns the pointer to the new device. The new device is automatically 822d560168bSGuenter Roeck * unregistered with the parent device. 823d560168bSGuenter Roeck */ 824d560168bSGuenter Roeck struct device * 825d560168bSGuenter Roeck devm_hwmon_device_register_with_info(struct device *dev, const char *name, 826d560168bSGuenter Roeck void *drvdata, 827d560168bSGuenter Roeck const struct hwmon_chip_info *chip, 828d560168bSGuenter Roeck const struct attribute_group **groups) 829d560168bSGuenter Roeck { 830d560168bSGuenter Roeck struct device **ptr, *hwdev; 831d560168bSGuenter Roeck 832d560168bSGuenter Roeck if (!dev) 833d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 834d560168bSGuenter Roeck 835d560168bSGuenter Roeck ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); 836d560168bSGuenter Roeck if (!ptr) 837d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 838d560168bSGuenter Roeck 839d560168bSGuenter Roeck hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip, 840d560168bSGuenter Roeck groups); 841d560168bSGuenter Roeck if (IS_ERR(hwdev)) 842d560168bSGuenter Roeck goto error; 843d560168bSGuenter Roeck 844d560168bSGuenter Roeck *ptr = hwdev; 845d560168bSGuenter Roeck devres_add(dev, ptr); 846d560168bSGuenter Roeck 847d560168bSGuenter Roeck return hwdev; 848d560168bSGuenter Roeck 849d560168bSGuenter Roeck error: 850d560168bSGuenter Roeck devres_free(ptr); 851d560168bSGuenter Roeck return hwdev; 852d560168bSGuenter Roeck } 853d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info); 854d560168bSGuenter Roeck 85574188cbaSGuenter Roeck static int devm_hwmon_match(struct device *dev, void *res, void *data) 85674188cbaSGuenter Roeck { 85774188cbaSGuenter Roeck struct device **hwdev = res; 85874188cbaSGuenter Roeck 85974188cbaSGuenter Roeck return *hwdev == data; 86074188cbaSGuenter Roeck } 86174188cbaSGuenter Roeck 86274188cbaSGuenter Roeck /** 86374188cbaSGuenter Roeck * devm_hwmon_device_unregister - removes a previously registered hwmon device 86474188cbaSGuenter Roeck * 86574188cbaSGuenter Roeck * @dev: the parent device of the device to unregister 86674188cbaSGuenter Roeck */ 86774188cbaSGuenter Roeck void devm_hwmon_device_unregister(struct device *dev) 86874188cbaSGuenter Roeck { 86974188cbaSGuenter Roeck WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev)); 87074188cbaSGuenter Roeck } 87174188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister); 87274188cbaSGuenter Roeck 8732958b1ecSJean Delvare static void __init hwmon_pci_quirks(void) 8742958b1ecSJean Delvare { 8752958b1ecSJean Delvare #if defined CONFIG_X86 && defined CONFIG_PCI 8762958b1ecSJean Delvare struct pci_dev *sb; 8772958b1ecSJean Delvare u16 base; 8782958b1ecSJean Delvare u8 enable; 8792958b1ecSJean Delvare 8802958b1ecSJean Delvare /* Open access to 0x295-0x296 on MSI MS-7031 */ 8812958b1ecSJean Delvare sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL); 882d6dab7ddSJean Delvare if (sb) { 883d6dab7ddSJean Delvare if (sb->subsystem_vendor == 0x1462 && /* MSI */ 884d6dab7ddSJean Delvare sb->subsystem_device == 0x0031) { /* MS-7031 */ 8852958b1ecSJean Delvare pci_read_config_byte(sb, 0x48, &enable); 8862958b1ecSJean Delvare pci_read_config_word(sb, 0x64, &base); 8872958b1ecSJean Delvare 8882958b1ecSJean Delvare if (base == 0 && !(enable & BIT(2))) { 8892958b1ecSJean Delvare dev_info(&sb->dev, 8902958b1ecSJean Delvare "Opening wide generic port at 0x295\n"); 8912958b1ecSJean Delvare pci_write_config_word(sb, 0x64, 0x295); 892d6dab7ddSJean Delvare pci_write_config_byte(sb, 0x48, 893d6dab7ddSJean Delvare enable | BIT(2)); 8942958b1ecSJean Delvare } 8952958b1ecSJean Delvare } 896d6dab7ddSJean Delvare pci_dev_put(sb); 897d6dab7ddSJean Delvare } 8982958b1ecSJean Delvare #endif 8992958b1ecSJean Delvare } 9002958b1ecSJean Delvare 9011236441fSMark M. Hoffman static int __init hwmon_init(void) 9021236441fSMark M. Hoffman { 903bab2243cSGuenter Roeck int err; 904bab2243cSGuenter Roeck 9052958b1ecSJean Delvare hwmon_pci_quirks(); 9062958b1ecSJean Delvare 907bab2243cSGuenter Roeck err = class_register(&hwmon_class); 908bab2243cSGuenter Roeck if (err) { 909bab2243cSGuenter Roeck pr_err("couldn't register hwmon sysfs class\n"); 910bab2243cSGuenter Roeck return err; 9111236441fSMark M. Hoffman } 9121236441fSMark M. Hoffman return 0; 9131236441fSMark M. Hoffman } 9141236441fSMark M. Hoffman 9151236441fSMark M. Hoffman static void __exit hwmon_exit(void) 9161236441fSMark M. Hoffman { 917bab2243cSGuenter Roeck class_unregister(&hwmon_class); 9181236441fSMark M. Hoffman } 9191236441fSMark M. Hoffman 92037f54ee5SDavid Brownell subsys_initcall(hwmon_init); 9211236441fSMark M. Hoffman module_exit(hwmon_exit); 9221236441fSMark M. Hoffman 9231236441fSMark M. Hoffman MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>"); 9241236441fSMark M. Hoffman MODULE_DESCRIPTION("hardware monitoring sysfs/class support"); 9251236441fSMark M. Hoffman MODULE_LICENSE("GPL"); 9261236441fSMark M. Hoffman 927