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) 54*3bf8bdcfSGuenter Roeck #define to_dev_attr(a) container_of(a, struct device_attribute, attr) 55d560168bSGuenter Roeck 56d560168bSGuenter Roeck /* 57d560168bSGuenter Roeck * Thermal zone information 58d560168bSGuenter Roeck * In addition to the reference to the hwmon device, 59d560168bSGuenter Roeck * also provides the sensor index. 60d560168bSGuenter Roeck */ 61d560168bSGuenter Roeck struct hwmon_thermal_data { 62*3bf8bdcfSGuenter Roeck struct device *dev; /* Reference to hwmon device */ 63d560168bSGuenter Roeck int index; /* sensor index */ 64d560168bSGuenter Roeck }; 65d560168bSGuenter Roeck 66bab2243cSGuenter Roeck static ssize_t 672ab0c6c5SJulia Lawall name_show(struct device *dev, struct device_attribute *attr, char *buf) 68bab2243cSGuenter Roeck { 69bab2243cSGuenter Roeck return sprintf(buf, "%s\n", to_hwmon_device(dev)->name); 70bab2243cSGuenter Roeck } 712ab0c6c5SJulia Lawall static DEVICE_ATTR_RO(name); 72bab2243cSGuenter Roeck 73bab2243cSGuenter Roeck static struct attribute *hwmon_dev_attrs[] = { 74bab2243cSGuenter Roeck &dev_attr_name.attr, 75bab2243cSGuenter Roeck NULL 76bab2243cSGuenter Roeck }; 77bab2243cSGuenter Roeck 78bab2243cSGuenter Roeck static umode_t hwmon_dev_name_is_visible(struct kobject *kobj, 79bab2243cSGuenter Roeck struct attribute *attr, int n) 80bab2243cSGuenter Roeck { 81bab2243cSGuenter Roeck struct device *dev = container_of(kobj, struct device, kobj); 82bab2243cSGuenter Roeck 83bab2243cSGuenter Roeck if (to_hwmon_device(dev)->name == NULL) 84bab2243cSGuenter Roeck return 0; 85bab2243cSGuenter Roeck 86bab2243cSGuenter Roeck return attr->mode; 87bab2243cSGuenter Roeck } 88bab2243cSGuenter Roeck 89524703acSArvind Yadav static const struct attribute_group hwmon_dev_attr_group = { 90bab2243cSGuenter Roeck .attrs = hwmon_dev_attrs, 91bab2243cSGuenter Roeck .is_visible = hwmon_dev_name_is_visible, 92bab2243cSGuenter Roeck }; 93bab2243cSGuenter Roeck 94bab2243cSGuenter Roeck static const struct attribute_group *hwmon_dev_attr_groups[] = { 95bab2243cSGuenter Roeck &hwmon_dev_attr_group, 96bab2243cSGuenter Roeck NULL 97bab2243cSGuenter Roeck }; 98bab2243cSGuenter Roeck 99*3bf8bdcfSGuenter Roeck static void hwmon_free_attrs(struct attribute **attrs) 100*3bf8bdcfSGuenter Roeck { 101*3bf8bdcfSGuenter Roeck int i; 102*3bf8bdcfSGuenter Roeck 103*3bf8bdcfSGuenter Roeck for (i = 0; attrs[i]; i++) { 104*3bf8bdcfSGuenter Roeck struct device_attribute *dattr = to_dev_attr(attrs[i]); 105*3bf8bdcfSGuenter Roeck struct hwmon_device_attribute *hattr = to_hwmon_attr(dattr); 106*3bf8bdcfSGuenter Roeck 107*3bf8bdcfSGuenter Roeck kfree(hattr); 108*3bf8bdcfSGuenter Roeck } 109*3bf8bdcfSGuenter Roeck kfree(attrs); 110*3bf8bdcfSGuenter Roeck } 111*3bf8bdcfSGuenter Roeck 112bab2243cSGuenter Roeck static void hwmon_dev_release(struct device *dev) 113bab2243cSGuenter Roeck { 114*3bf8bdcfSGuenter Roeck struct hwmon_device *hwdev = to_hwmon_device(dev); 115*3bf8bdcfSGuenter Roeck 116*3bf8bdcfSGuenter Roeck if (hwdev->group.attrs) 117*3bf8bdcfSGuenter Roeck hwmon_free_attrs(hwdev->group.attrs); 118*3bf8bdcfSGuenter Roeck kfree(hwdev->groups); 119*3bf8bdcfSGuenter Roeck kfree(hwdev); 120bab2243cSGuenter Roeck } 121bab2243cSGuenter Roeck 122bab2243cSGuenter Roeck static struct class hwmon_class = { 123bab2243cSGuenter Roeck .name = "hwmon", 124bab2243cSGuenter Roeck .owner = THIS_MODULE, 125bab2243cSGuenter Roeck .dev_groups = hwmon_dev_attr_groups, 126bab2243cSGuenter Roeck .dev_release = hwmon_dev_release, 127bab2243cSGuenter Roeck }; 1281236441fSMark M. Hoffman 1294ca5f468SJonathan Cameron static DEFINE_IDA(hwmon_ida); 1301236441fSMark M. Hoffman 131d560168bSGuenter Roeck /* Thermal zone handling */ 132d560168bSGuenter Roeck 13386430c1aSGuenter Roeck /* 13486430c1aSGuenter Roeck * The complex conditional is necessary to avoid a cyclic dependency 13586430c1aSGuenter Roeck * between hwmon and thermal_sys modules. 13686430c1aSGuenter Roeck */ 137f3735332SDaniel Lezcano #ifdef CONFIG_THERMAL_OF 138d560168bSGuenter Roeck static int hwmon_thermal_get_temp(void *data, int *temp) 139d560168bSGuenter Roeck { 140d560168bSGuenter Roeck struct hwmon_thermal_data *tdata = data; 141*3bf8bdcfSGuenter Roeck struct hwmon_device *hwdev = to_hwmon_device(tdata->dev); 142d560168bSGuenter Roeck int ret; 143d560168bSGuenter Roeck long t; 144d560168bSGuenter Roeck 145*3bf8bdcfSGuenter Roeck ret = hwdev->chip->ops->read(tdata->dev, hwmon_temp, hwmon_temp_input, 146d560168bSGuenter Roeck tdata->index, &t); 147d560168bSGuenter Roeck if (ret < 0) 148d560168bSGuenter Roeck return ret; 149d560168bSGuenter Roeck 150d560168bSGuenter Roeck *temp = t; 151d560168bSGuenter Roeck 152d560168bSGuenter Roeck return 0; 153d560168bSGuenter Roeck } 154d560168bSGuenter Roeck 155c9920650SJulia Lawall static const struct thermal_zone_of_device_ops hwmon_thermal_ops = { 156d560168bSGuenter Roeck .get_temp = hwmon_thermal_get_temp, 157d560168bSGuenter Roeck }; 158d560168bSGuenter Roeck 159*3bf8bdcfSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev, int index) 160d560168bSGuenter Roeck { 161d560168bSGuenter Roeck struct hwmon_thermal_data *tdata; 16247c332deSLinus Walleij struct thermal_zone_device *tzd; 163d560168bSGuenter Roeck 164d560168bSGuenter Roeck tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL); 165d560168bSGuenter Roeck if (!tdata) 166d560168bSGuenter Roeck return -ENOMEM; 167d560168bSGuenter Roeck 168*3bf8bdcfSGuenter Roeck tdata->dev = dev; 169d560168bSGuenter Roeck tdata->index = index; 170d560168bSGuenter Roeck 171*3bf8bdcfSGuenter Roeck tzd = devm_thermal_zone_of_sensor_register(dev, index, tdata, 172d560168bSGuenter Roeck &hwmon_thermal_ops); 17347c332deSLinus Walleij /* 17447c332deSLinus Walleij * If CONFIG_THERMAL_OF is disabled, this returns -ENODEV, 17547c332deSLinus Walleij * so ignore that error but forward any other error. 17647c332deSLinus Walleij */ 17747c332deSLinus Walleij if (IS_ERR(tzd) && (PTR_ERR(tzd) != -ENODEV)) 17847c332deSLinus Walleij return PTR_ERR(tzd); 179d560168bSGuenter Roeck 180d560168bSGuenter Roeck return 0; 181d560168bSGuenter Roeck } 182d560168bSGuenter Roeck #else 183*3bf8bdcfSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev, int index) 184d560168bSGuenter Roeck { 185d560168bSGuenter Roeck return 0; 186d560168bSGuenter Roeck } 18786430c1aSGuenter Roeck #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */ 188d560168bSGuenter Roeck 18961b8ab2cSNicolin Chen static int hwmon_attr_base(enum hwmon_sensor_types type) 19061b8ab2cSNicolin Chen { 19161b8ab2cSNicolin Chen if (type == hwmon_in) 19261b8ab2cSNicolin Chen return 0; 19361b8ab2cSNicolin Chen return 1; 19461b8ab2cSNicolin Chen } 19561b8ab2cSNicolin Chen 196d560168bSGuenter Roeck /* sysfs attribute management */ 197d560168bSGuenter Roeck 198d560168bSGuenter Roeck static ssize_t hwmon_attr_show(struct device *dev, 199d560168bSGuenter Roeck struct device_attribute *devattr, char *buf) 200d560168bSGuenter Roeck { 201d560168bSGuenter Roeck struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); 202d560168bSGuenter Roeck long val; 203d560168bSGuenter Roeck int ret; 204d560168bSGuenter Roeck 205d560168bSGuenter Roeck ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index, 206d560168bSGuenter Roeck &val); 207d560168bSGuenter Roeck if (ret < 0) 208d560168bSGuenter Roeck return ret; 209d560168bSGuenter Roeck 21061b8ab2cSNicolin Chen trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type), 21161b8ab2cSNicolin Chen hattr->name, val); 21261b8ab2cSNicolin Chen 213d560168bSGuenter Roeck return sprintf(buf, "%ld\n", val); 214d560168bSGuenter Roeck } 215d560168bSGuenter Roeck 216e159ab5cSGuenter Roeck static ssize_t hwmon_attr_show_string(struct device *dev, 217e159ab5cSGuenter Roeck struct device_attribute *devattr, 218e159ab5cSGuenter Roeck char *buf) 219e159ab5cSGuenter Roeck { 220e159ab5cSGuenter Roeck struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); 22161b8ab2cSNicolin Chen enum hwmon_sensor_types type = hattr->type; 2225ba6bcbcSJean Delvare const char *s; 223e159ab5cSGuenter Roeck int ret; 224e159ab5cSGuenter Roeck 225e159ab5cSGuenter Roeck ret = hattr->ops->read_string(dev, hattr->type, hattr->attr, 226e159ab5cSGuenter Roeck hattr->index, &s); 227e159ab5cSGuenter Roeck if (ret < 0) 228e159ab5cSGuenter Roeck return ret; 229e159ab5cSGuenter Roeck 23061b8ab2cSNicolin Chen trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type), 23161b8ab2cSNicolin Chen hattr->name, s); 23261b8ab2cSNicolin Chen 233e159ab5cSGuenter Roeck return sprintf(buf, "%s\n", s); 234e159ab5cSGuenter Roeck } 235e159ab5cSGuenter Roeck 236d560168bSGuenter Roeck static ssize_t hwmon_attr_store(struct device *dev, 237d560168bSGuenter Roeck struct device_attribute *devattr, 238d560168bSGuenter Roeck const char *buf, size_t count) 239d560168bSGuenter Roeck { 240d560168bSGuenter Roeck struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); 241d560168bSGuenter Roeck long val; 242d560168bSGuenter Roeck int ret; 243d560168bSGuenter Roeck 244d560168bSGuenter Roeck ret = kstrtol(buf, 10, &val); 245d560168bSGuenter Roeck if (ret < 0) 246d560168bSGuenter Roeck return ret; 247d560168bSGuenter Roeck 248d560168bSGuenter Roeck ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index, 249d560168bSGuenter Roeck val); 250d560168bSGuenter Roeck if (ret < 0) 251d560168bSGuenter Roeck return ret; 252d560168bSGuenter Roeck 25361b8ab2cSNicolin Chen trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type), 25461b8ab2cSNicolin Chen hattr->name, val); 255d560168bSGuenter Roeck 25661b8ab2cSNicolin Chen return count; 257d560168bSGuenter Roeck } 258d560168bSGuenter Roeck 259e159ab5cSGuenter Roeck static bool is_string_attr(enum hwmon_sensor_types type, u32 attr) 260e159ab5cSGuenter Roeck { 261e159ab5cSGuenter Roeck return (type == hwmon_temp && attr == hwmon_temp_label) || 262e159ab5cSGuenter Roeck (type == hwmon_in && attr == hwmon_in_label) || 263e159ab5cSGuenter Roeck (type == hwmon_curr && attr == hwmon_curr_label) || 264e159ab5cSGuenter Roeck (type == hwmon_power && attr == hwmon_power_label) || 265e159ab5cSGuenter Roeck (type == hwmon_energy && attr == hwmon_energy_label) || 266e159ab5cSGuenter Roeck (type == hwmon_humidity && attr == hwmon_humidity_label) || 267e159ab5cSGuenter Roeck (type == hwmon_fan && attr == hwmon_fan_label); 268e159ab5cSGuenter Roeck } 269e159ab5cSGuenter Roeck 270*3bf8bdcfSGuenter Roeck static struct attribute *hwmon_genattr(const void *drvdata, 271d560168bSGuenter Roeck enum hwmon_sensor_types type, 272d560168bSGuenter Roeck u32 attr, 273d560168bSGuenter Roeck int index, 274d560168bSGuenter Roeck const char *template, 275d560168bSGuenter Roeck const struct hwmon_ops *ops) 276d560168bSGuenter Roeck { 277d560168bSGuenter Roeck struct hwmon_device_attribute *hattr; 278d560168bSGuenter Roeck struct device_attribute *dattr; 279d560168bSGuenter Roeck struct attribute *a; 280d560168bSGuenter Roeck umode_t mode; 2813b443defSRasmus Villemoes const char *name; 282e159ab5cSGuenter Roeck bool is_string = is_string_attr(type, attr); 283d560168bSGuenter Roeck 284d560168bSGuenter Roeck /* The attribute is invisible if there is no template string */ 285d560168bSGuenter Roeck if (!template) 286d560168bSGuenter Roeck return ERR_PTR(-ENOENT); 287d560168bSGuenter Roeck 288d560168bSGuenter Roeck mode = ops->is_visible(drvdata, type, attr, index); 289d560168bSGuenter Roeck if (!mode) 290d560168bSGuenter Roeck return ERR_PTR(-ENOENT); 291d560168bSGuenter Roeck 2920d87116fSGuenter Roeck if ((mode & 0444) && ((is_string && !ops->read_string) || 293e159ab5cSGuenter Roeck (!is_string && !ops->read))) 294d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 2950d87116fSGuenter Roeck if ((mode & 0222) && !ops->write) 296d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 297d560168bSGuenter Roeck 298*3bf8bdcfSGuenter Roeck hattr = kzalloc(sizeof(*hattr), GFP_KERNEL); 299d560168bSGuenter Roeck if (!hattr) 300d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 301d560168bSGuenter Roeck 3023a412d5eSGuenter Roeck if (type == hwmon_chip) { 3033b443defSRasmus Villemoes name = template; 3043a412d5eSGuenter Roeck } else { 3053a412d5eSGuenter Roeck scnprintf(hattr->name, sizeof(hattr->name), template, 3063a412d5eSGuenter Roeck index + hwmon_attr_base(type)); 3073a412d5eSGuenter Roeck name = hattr->name; 3083a412d5eSGuenter Roeck } 3093a412d5eSGuenter Roeck 310d560168bSGuenter Roeck hattr->type = type; 311d560168bSGuenter Roeck hattr->attr = attr; 312d560168bSGuenter Roeck hattr->index = index; 313d560168bSGuenter Roeck hattr->ops = ops; 314d560168bSGuenter Roeck 315d560168bSGuenter Roeck dattr = &hattr->dev_attr; 316e159ab5cSGuenter Roeck dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show; 317d560168bSGuenter Roeck dattr->store = hwmon_attr_store; 318d560168bSGuenter Roeck 319d560168bSGuenter Roeck a = &dattr->attr; 320d560168bSGuenter Roeck sysfs_attr_init(a); 321d560168bSGuenter Roeck a->name = name; 322d560168bSGuenter Roeck a->mode = mode; 323d560168bSGuenter Roeck 324d560168bSGuenter Roeck return a; 325d560168bSGuenter Roeck } 326d560168bSGuenter Roeck 327f4d325d5SGuenter Roeck /* 328f4d325d5SGuenter Roeck * Chip attributes are not attribute templates but actual sysfs attributes. 329f4d325d5SGuenter Roeck * See hwmon_genattr() for special handling. 330f4d325d5SGuenter Roeck */ 331f4d325d5SGuenter Roeck static const char * const hwmon_chip_attrs[] = { 332d560168bSGuenter Roeck [hwmon_chip_temp_reset_history] = "temp_reset_history", 33300d616cfSGuenter Roeck [hwmon_chip_in_reset_history] = "in_reset_history", 3349b26947cSGuenter Roeck [hwmon_chip_curr_reset_history] = "curr_reset_history", 335b308f5c7SGuenter Roeck [hwmon_chip_power_reset_history] = "power_reset_history", 336d560168bSGuenter Roeck [hwmon_chip_update_interval] = "update_interval", 337d560168bSGuenter Roeck [hwmon_chip_alarms] = "alarms", 3389f00995eSGuenter Roeck [hwmon_chip_samples] = "samples", 3399f00995eSGuenter Roeck [hwmon_chip_curr_samples] = "curr_samples", 3409f00995eSGuenter Roeck [hwmon_chip_in_samples] = "in_samples", 3419f00995eSGuenter Roeck [hwmon_chip_power_samples] = "power_samples", 3429f00995eSGuenter Roeck [hwmon_chip_temp_samples] = "temp_samples", 343d560168bSGuenter Roeck }; 344d560168bSGuenter Roeck 345d560168bSGuenter Roeck static const char * const hwmon_temp_attr_templates[] = { 346d560168bSGuenter Roeck [hwmon_temp_input] = "temp%d_input", 347d560168bSGuenter Roeck [hwmon_temp_type] = "temp%d_type", 348d560168bSGuenter Roeck [hwmon_temp_lcrit] = "temp%d_lcrit", 349d560168bSGuenter Roeck [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst", 350d560168bSGuenter Roeck [hwmon_temp_min] = "temp%d_min", 351d560168bSGuenter Roeck [hwmon_temp_min_hyst] = "temp%d_min_hyst", 352d560168bSGuenter Roeck [hwmon_temp_max] = "temp%d_max", 353d560168bSGuenter Roeck [hwmon_temp_max_hyst] = "temp%d_max_hyst", 354d560168bSGuenter Roeck [hwmon_temp_crit] = "temp%d_crit", 355d560168bSGuenter Roeck [hwmon_temp_crit_hyst] = "temp%d_crit_hyst", 356d560168bSGuenter Roeck [hwmon_temp_emergency] = "temp%d_emergency", 357d560168bSGuenter Roeck [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst", 358d560168bSGuenter Roeck [hwmon_temp_alarm] = "temp%d_alarm", 359d560168bSGuenter Roeck [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm", 360d560168bSGuenter Roeck [hwmon_temp_min_alarm] = "temp%d_min_alarm", 361d560168bSGuenter Roeck [hwmon_temp_max_alarm] = "temp%d_max_alarm", 362d560168bSGuenter Roeck [hwmon_temp_crit_alarm] = "temp%d_crit_alarm", 363d560168bSGuenter Roeck [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm", 364d560168bSGuenter Roeck [hwmon_temp_fault] = "temp%d_fault", 365d560168bSGuenter Roeck [hwmon_temp_offset] = "temp%d_offset", 366d560168bSGuenter Roeck [hwmon_temp_label] = "temp%d_label", 367d560168bSGuenter Roeck [hwmon_temp_lowest] = "temp%d_lowest", 368d560168bSGuenter Roeck [hwmon_temp_highest] = "temp%d_highest", 369d560168bSGuenter Roeck [hwmon_temp_reset_history] = "temp%d_reset_history", 370d560168bSGuenter Roeck }; 371d560168bSGuenter Roeck 37200d616cfSGuenter Roeck static const char * const hwmon_in_attr_templates[] = { 37300d616cfSGuenter Roeck [hwmon_in_input] = "in%d_input", 37400d616cfSGuenter Roeck [hwmon_in_min] = "in%d_min", 37500d616cfSGuenter Roeck [hwmon_in_max] = "in%d_max", 37600d616cfSGuenter Roeck [hwmon_in_lcrit] = "in%d_lcrit", 37700d616cfSGuenter Roeck [hwmon_in_crit] = "in%d_crit", 37800d616cfSGuenter Roeck [hwmon_in_average] = "in%d_average", 37900d616cfSGuenter Roeck [hwmon_in_lowest] = "in%d_lowest", 38000d616cfSGuenter Roeck [hwmon_in_highest] = "in%d_highest", 38100d616cfSGuenter Roeck [hwmon_in_reset_history] = "in%d_reset_history", 38200d616cfSGuenter Roeck [hwmon_in_label] = "in%d_label", 38300d616cfSGuenter Roeck [hwmon_in_alarm] = "in%d_alarm", 38400d616cfSGuenter Roeck [hwmon_in_min_alarm] = "in%d_min_alarm", 38500d616cfSGuenter Roeck [hwmon_in_max_alarm] = "in%d_max_alarm", 38600d616cfSGuenter Roeck [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm", 38700d616cfSGuenter Roeck [hwmon_in_crit_alarm] = "in%d_crit_alarm", 38868c0d69dSNicolin Chen [hwmon_in_enable] = "in%d_enable", 38900d616cfSGuenter Roeck }; 39000d616cfSGuenter Roeck 3919b26947cSGuenter Roeck static const char * const hwmon_curr_attr_templates[] = { 3929b26947cSGuenter Roeck [hwmon_curr_input] = "curr%d_input", 3939b26947cSGuenter Roeck [hwmon_curr_min] = "curr%d_min", 3949b26947cSGuenter Roeck [hwmon_curr_max] = "curr%d_max", 3959b26947cSGuenter Roeck [hwmon_curr_lcrit] = "curr%d_lcrit", 3969b26947cSGuenter Roeck [hwmon_curr_crit] = "curr%d_crit", 3979b26947cSGuenter Roeck [hwmon_curr_average] = "curr%d_average", 3989b26947cSGuenter Roeck [hwmon_curr_lowest] = "curr%d_lowest", 3999b26947cSGuenter Roeck [hwmon_curr_highest] = "curr%d_highest", 4009b26947cSGuenter Roeck [hwmon_curr_reset_history] = "curr%d_reset_history", 4019b26947cSGuenter Roeck [hwmon_curr_label] = "curr%d_label", 4029b26947cSGuenter Roeck [hwmon_curr_alarm] = "curr%d_alarm", 4039b26947cSGuenter Roeck [hwmon_curr_min_alarm] = "curr%d_min_alarm", 4049b26947cSGuenter Roeck [hwmon_curr_max_alarm] = "curr%d_max_alarm", 4059b26947cSGuenter Roeck [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm", 4069b26947cSGuenter Roeck [hwmon_curr_crit_alarm] = "curr%d_crit_alarm", 4079b26947cSGuenter Roeck }; 4089b26947cSGuenter Roeck 409b308f5c7SGuenter Roeck static const char * const hwmon_power_attr_templates[] = { 410b308f5c7SGuenter Roeck [hwmon_power_average] = "power%d_average", 411b308f5c7SGuenter Roeck [hwmon_power_average_interval] = "power%d_average_interval", 412b308f5c7SGuenter Roeck [hwmon_power_average_interval_max] = "power%d_interval_max", 413b308f5c7SGuenter Roeck [hwmon_power_average_interval_min] = "power%d_interval_min", 414b308f5c7SGuenter Roeck [hwmon_power_average_highest] = "power%d_average_highest", 415b308f5c7SGuenter Roeck [hwmon_power_average_lowest] = "power%d_average_lowest", 416b308f5c7SGuenter Roeck [hwmon_power_average_max] = "power%d_average_max", 417b308f5c7SGuenter Roeck [hwmon_power_average_min] = "power%d_average_min", 418b308f5c7SGuenter Roeck [hwmon_power_input] = "power%d_input", 419b308f5c7SGuenter Roeck [hwmon_power_input_highest] = "power%d_input_highest", 420b308f5c7SGuenter Roeck [hwmon_power_input_lowest] = "power%d_input_lowest", 421b308f5c7SGuenter Roeck [hwmon_power_reset_history] = "power%d_reset_history", 422b308f5c7SGuenter Roeck [hwmon_power_accuracy] = "power%d_accuracy", 423b308f5c7SGuenter Roeck [hwmon_power_cap] = "power%d_cap", 424b308f5c7SGuenter Roeck [hwmon_power_cap_hyst] = "power%d_cap_hyst", 425b308f5c7SGuenter Roeck [hwmon_power_cap_max] = "power%d_cap_max", 426b308f5c7SGuenter Roeck [hwmon_power_cap_min] = "power%d_cap_min", 427aa7f29b0SAndrew Lunn [hwmon_power_min] = "power%d_min", 428b308f5c7SGuenter Roeck [hwmon_power_max] = "power%d_max", 429aa7f29b0SAndrew Lunn [hwmon_power_lcrit] = "power%d_lcrit", 430b308f5c7SGuenter Roeck [hwmon_power_crit] = "power%d_crit", 431b308f5c7SGuenter Roeck [hwmon_power_label] = "power%d_label", 432b308f5c7SGuenter Roeck [hwmon_power_alarm] = "power%d_alarm", 433b308f5c7SGuenter Roeck [hwmon_power_cap_alarm] = "power%d_cap_alarm", 434aa7f29b0SAndrew Lunn [hwmon_power_min_alarm] = "power%d_min_alarm", 435b308f5c7SGuenter Roeck [hwmon_power_max_alarm] = "power%d_max_alarm", 436aa7f29b0SAndrew Lunn [hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm", 437b308f5c7SGuenter Roeck [hwmon_power_crit_alarm] = "power%d_crit_alarm", 438b308f5c7SGuenter Roeck }; 439b308f5c7SGuenter Roeck 4406bfcca44SGuenter Roeck static const char * const hwmon_energy_attr_templates[] = { 4416bfcca44SGuenter Roeck [hwmon_energy_input] = "energy%d_input", 4426bfcca44SGuenter Roeck [hwmon_energy_label] = "energy%d_label", 4436bfcca44SGuenter Roeck }; 4446bfcca44SGuenter Roeck 4456bfcca44SGuenter Roeck static const char * const hwmon_humidity_attr_templates[] = { 4466bfcca44SGuenter Roeck [hwmon_humidity_input] = "humidity%d_input", 4476bfcca44SGuenter Roeck [hwmon_humidity_label] = "humidity%d_label", 4486bfcca44SGuenter Roeck [hwmon_humidity_min] = "humidity%d_min", 4496bfcca44SGuenter Roeck [hwmon_humidity_min_hyst] = "humidity%d_min_hyst", 4506bfcca44SGuenter Roeck [hwmon_humidity_max] = "humidity%d_max", 4516bfcca44SGuenter Roeck [hwmon_humidity_max_hyst] = "humidity%d_max_hyst", 4526bfcca44SGuenter Roeck [hwmon_humidity_alarm] = "humidity%d_alarm", 4536bfcca44SGuenter Roeck [hwmon_humidity_fault] = "humidity%d_fault", 4546bfcca44SGuenter Roeck }; 4556bfcca44SGuenter Roeck 4568faee73fSGuenter Roeck static const char * const hwmon_fan_attr_templates[] = { 4578faee73fSGuenter Roeck [hwmon_fan_input] = "fan%d_input", 4588faee73fSGuenter Roeck [hwmon_fan_label] = "fan%d_label", 4598faee73fSGuenter Roeck [hwmon_fan_min] = "fan%d_min", 4608faee73fSGuenter Roeck [hwmon_fan_max] = "fan%d_max", 4618faee73fSGuenter Roeck [hwmon_fan_div] = "fan%d_div", 4628faee73fSGuenter Roeck [hwmon_fan_pulses] = "fan%d_pulses", 4638faee73fSGuenter Roeck [hwmon_fan_target] = "fan%d_target", 4648faee73fSGuenter Roeck [hwmon_fan_alarm] = "fan%d_alarm", 4658faee73fSGuenter Roeck [hwmon_fan_min_alarm] = "fan%d_min_alarm", 4668faee73fSGuenter Roeck [hwmon_fan_max_alarm] = "fan%d_max_alarm", 4678faee73fSGuenter Roeck [hwmon_fan_fault] = "fan%d_fault", 4688faee73fSGuenter Roeck }; 4698faee73fSGuenter Roeck 470f9f7bb3aSGuenter Roeck static const char * const hwmon_pwm_attr_templates[] = { 471f9f7bb3aSGuenter Roeck [hwmon_pwm_input] = "pwm%d", 472f9f7bb3aSGuenter Roeck [hwmon_pwm_enable] = "pwm%d_enable", 473f9f7bb3aSGuenter Roeck [hwmon_pwm_mode] = "pwm%d_mode", 474f9f7bb3aSGuenter Roeck [hwmon_pwm_freq] = "pwm%d_freq", 475f9f7bb3aSGuenter Roeck }; 476f9f7bb3aSGuenter Roeck 477d560168bSGuenter Roeck static const char * const *__templates[] = { 478f4d325d5SGuenter Roeck [hwmon_chip] = hwmon_chip_attrs, 479d560168bSGuenter Roeck [hwmon_temp] = hwmon_temp_attr_templates, 48000d616cfSGuenter Roeck [hwmon_in] = hwmon_in_attr_templates, 4819b26947cSGuenter Roeck [hwmon_curr] = hwmon_curr_attr_templates, 482b308f5c7SGuenter Roeck [hwmon_power] = hwmon_power_attr_templates, 4836bfcca44SGuenter Roeck [hwmon_energy] = hwmon_energy_attr_templates, 4846bfcca44SGuenter Roeck [hwmon_humidity] = hwmon_humidity_attr_templates, 4858faee73fSGuenter Roeck [hwmon_fan] = hwmon_fan_attr_templates, 486f9f7bb3aSGuenter Roeck [hwmon_pwm] = hwmon_pwm_attr_templates, 487d560168bSGuenter Roeck }; 488d560168bSGuenter Roeck 489d560168bSGuenter Roeck static const int __templates_size[] = { 490f4d325d5SGuenter Roeck [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs), 491d560168bSGuenter Roeck [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates), 49200d616cfSGuenter Roeck [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates), 4939b26947cSGuenter Roeck [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates), 494b308f5c7SGuenter Roeck [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates), 4956bfcca44SGuenter Roeck [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates), 4966bfcca44SGuenter Roeck [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates), 4978faee73fSGuenter Roeck [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates), 498f9f7bb3aSGuenter Roeck [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates), 499d560168bSGuenter Roeck }; 500d560168bSGuenter Roeck 501d560168bSGuenter Roeck static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info) 502d560168bSGuenter Roeck { 503d560168bSGuenter Roeck int i, n; 504d560168bSGuenter Roeck 505d560168bSGuenter Roeck for (i = n = 0; info->config[i]; i++) 506d560168bSGuenter Roeck n += hweight32(info->config[i]); 507d560168bSGuenter Roeck 508d560168bSGuenter Roeck return n; 509d560168bSGuenter Roeck } 510d560168bSGuenter Roeck 511*3bf8bdcfSGuenter Roeck static int hwmon_genattrs(const void *drvdata, 512d560168bSGuenter Roeck struct attribute **attrs, 513d560168bSGuenter Roeck const struct hwmon_ops *ops, 514d560168bSGuenter Roeck const struct hwmon_channel_info *info) 515d560168bSGuenter Roeck { 516d560168bSGuenter Roeck const char * const *templates; 517d560168bSGuenter Roeck int template_size; 518d560168bSGuenter Roeck int i, aindex = 0; 519d560168bSGuenter Roeck 520d560168bSGuenter Roeck if (info->type >= ARRAY_SIZE(__templates)) 521d560168bSGuenter Roeck return -EINVAL; 522d560168bSGuenter Roeck 523d560168bSGuenter Roeck templates = __templates[info->type]; 524d560168bSGuenter Roeck template_size = __templates_size[info->type]; 525d560168bSGuenter Roeck 526d560168bSGuenter Roeck for (i = 0; info->config[i]; i++) { 527d560168bSGuenter Roeck u32 attr_mask = info->config[i]; 528d560168bSGuenter Roeck u32 attr; 529d560168bSGuenter Roeck 530d560168bSGuenter Roeck while (attr_mask) { 531d560168bSGuenter Roeck struct attribute *a; 532d560168bSGuenter Roeck 533d560168bSGuenter Roeck attr = __ffs(attr_mask); 534d560168bSGuenter Roeck attr_mask &= ~BIT(attr); 535d560168bSGuenter Roeck if (attr >= template_size) 536d560168bSGuenter Roeck return -EINVAL; 537*3bf8bdcfSGuenter Roeck a = hwmon_genattr(drvdata, info->type, attr, i, 538d560168bSGuenter Roeck templates[attr], ops); 539d560168bSGuenter Roeck if (IS_ERR(a)) { 540d560168bSGuenter Roeck if (PTR_ERR(a) != -ENOENT) 541d560168bSGuenter Roeck return PTR_ERR(a); 542d560168bSGuenter Roeck continue; 543d560168bSGuenter Roeck } 544d560168bSGuenter Roeck attrs[aindex++] = a; 545d560168bSGuenter Roeck } 546d560168bSGuenter Roeck } 547d560168bSGuenter Roeck return aindex; 548d560168bSGuenter Roeck } 549d560168bSGuenter Roeck 550d560168bSGuenter Roeck static struct attribute ** 551*3bf8bdcfSGuenter Roeck __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip) 552d560168bSGuenter Roeck { 553d560168bSGuenter Roeck int ret, i, aindex = 0, nattrs = 0; 554d560168bSGuenter Roeck struct attribute **attrs; 555d560168bSGuenter Roeck 556d560168bSGuenter Roeck for (i = 0; chip->info[i]; i++) 557d560168bSGuenter Roeck nattrs += hwmon_num_channel_attrs(chip->info[i]); 558d560168bSGuenter Roeck 559d560168bSGuenter Roeck if (nattrs == 0) 560d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 561d560168bSGuenter Roeck 562*3bf8bdcfSGuenter Roeck attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL); 563d560168bSGuenter Roeck if (!attrs) 564d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 565d560168bSGuenter Roeck 566d560168bSGuenter Roeck for (i = 0; chip->info[i]; i++) { 567*3bf8bdcfSGuenter Roeck ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops, 568d560168bSGuenter Roeck chip->info[i]); 569*3bf8bdcfSGuenter Roeck if (ret < 0) { 570*3bf8bdcfSGuenter Roeck hwmon_free_attrs(attrs); 571d560168bSGuenter Roeck return ERR_PTR(ret); 572*3bf8bdcfSGuenter Roeck } 573d560168bSGuenter Roeck aindex += ret; 574d560168bSGuenter Roeck } 575d560168bSGuenter Roeck 576d560168bSGuenter Roeck return attrs; 577d560168bSGuenter Roeck } 578d560168bSGuenter Roeck 579d560168bSGuenter Roeck static struct device * 580d560168bSGuenter Roeck __hwmon_device_register(struct device *dev, const char *name, void *drvdata, 581d560168bSGuenter Roeck const struct hwmon_chip_info *chip, 582d560168bSGuenter Roeck const struct attribute_group **groups) 583d560168bSGuenter Roeck { 584d560168bSGuenter Roeck struct hwmon_device *hwdev; 585d560168bSGuenter Roeck struct device *hdev; 586d560168bSGuenter Roeck int i, j, err, id; 587d560168bSGuenter Roeck 58874d3b641SGuenter Roeck /* Complain about invalid characters in hwmon name attribute */ 589d560168bSGuenter Roeck if (name && (!strlen(name) || strpbrk(name, "-* \t\n"))) 59074d3b641SGuenter Roeck dev_warn(dev, 59174d3b641SGuenter Roeck "hwmon: '%s' is not a valid name attribute, please fix\n", 59274d3b641SGuenter Roeck name); 593d560168bSGuenter Roeck 594d560168bSGuenter Roeck id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL); 595d560168bSGuenter Roeck if (id < 0) 596d560168bSGuenter Roeck return ERR_PTR(id); 597d560168bSGuenter Roeck 598d560168bSGuenter Roeck hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL); 599d560168bSGuenter Roeck if (hwdev == NULL) { 600d560168bSGuenter Roeck err = -ENOMEM; 601d560168bSGuenter Roeck goto ida_remove; 602d560168bSGuenter Roeck } 603d560168bSGuenter Roeck 604d560168bSGuenter Roeck hdev = &hwdev->dev; 605d560168bSGuenter Roeck 606239552f4SGuenter Roeck if (chip) { 607d560168bSGuenter Roeck struct attribute **attrs; 608b2a4cc3aSGuenter Roeck int ngroups = 2; /* terminating NULL plus &hwdev->groups */ 609d560168bSGuenter Roeck 610d560168bSGuenter Roeck if (groups) 611d560168bSGuenter Roeck for (i = 0; groups[i]; i++) 612d560168bSGuenter Roeck ngroups++; 613d560168bSGuenter Roeck 614*3bf8bdcfSGuenter Roeck hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL); 61538d8ed65SColin Ian King if (!hwdev->groups) { 61638d8ed65SColin Ian King err = -ENOMEM; 61738d8ed65SColin Ian King goto free_hwmon; 61838d8ed65SColin Ian King } 619d560168bSGuenter Roeck 620*3bf8bdcfSGuenter Roeck attrs = __hwmon_create_attrs(drvdata, chip); 621d560168bSGuenter Roeck if (IS_ERR(attrs)) { 622d560168bSGuenter Roeck err = PTR_ERR(attrs); 623d560168bSGuenter Roeck goto free_hwmon; 624d560168bSGuenter Roeck } 625d560168bSGuenter Roeck 626d560168bSGuenter Roeck hwdev->group.attrs = attrs; 627d560168bSGuenter Roeck ngroups = 0; 628d560168bSGuenter Roeck hwdev->groups[ngroups++] = &hwdev->group; 629d560168bSGuenter Roeck 630d560168bSGuenter Roeck if (groups) { 631d560168bSGuenter Roeck for (i = 0; groups[i]; i++) 632d560168bSGuenter Roeck hwdev->groups[ngroups++] = groups[i]; 633d560168bSGuenter Roeck } 634d560168bSGuenter Roeck 635d560168bSGuenter Roeck hdev->groups = hwdev->groups; 636d560168bSGuenter Roeck } else { 637d560168bSGuenter Roeck hdev->groups = groups; 638d560168bSGuenter Roeck } 639d560168bSGuenter Roeck 640d560168bSGuenter Roeck hwdev->name = name; 641d560168bSGuenter Roeck hdev->class = &hwmon_class; 642d560168bSGuenter Roeck hdev->parent = dev; 643d560168bSGuenter Roeck hdev->of_node = dev ? dev->of_node : NULL; 644d560168bSGuenter Roeck hwdev->chip = chip; 645d560168bSGuenter Roeck dev_set_drvdata(hdev, drvdata); 646d560168bSGuenter Roeck dev_set_name(hdev, HWMON_ID_FORMAT, id); 647d560168bSGuenter Roeck err = device_register(hdev); 648d560168bSGuenter Roeck if (err) 649d560168bSGuenter Roeck goto free_hwmon; 650d560168bSGuenter Roeck 651c41dd48eSEduardo Valentin if (dev && dev->of_node && chip && chip->ops->read && 652d560168bSGuenter Roeck chip->info[0]->type == hwmon_chip && 653d560168bSGuenter Roeck (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) { 654d560168bSGuenter Roeck const struct hwmon_channel_info **info = chip->info; 655d560168bSGuenter Roeck 656d560168bSGuenter Roeck for (i = 1; info[i]; i++) { 657d560168bSGuenter Roeck if (info[i]->type != hwmon_temp) 658d560168bSGuenter Roeck continue; 659d560168bSGuenter Roeck 660d560168bSGuenter Roeck for (j = 0; info[i]->config[j]; j++) { 661d560168bSGuenter Roeck if (!chip->ops->is_visible(drvdata, hwmon_temp, 662d560168bSGuenter Roeck hwmon_temp_input, j)) 663d560168bSGuenter Roeck continue; 66447c332deSLinus Walleij if (info[i]->config[j] & HWMON_T_INPUT) { 665*3bf8bdcfSGuenter Roeck err = hwmon_thermal_add_sensor(hdev, j); 66674e35127SDmitry Osipenko if (err) { 66774e35127SDmitry Osipenko device_unregister(hdev); 668792eac18SGuenter Roeck /* 669792eac18SGuenter Roeck * Don't worry about hwdev; 670792eac18SGuenter Roeck * hwmon_dev_release(), called 671792eac18SGuenter Roeck * from device_unregister(), 672792eac18SGuenter Roeck * will free it. 673792eac18SGuenter Roeck */ 67474e35127SDmitry Osipenko goto ida_remove; 67574e35127SDmitry Osipenko } 67647c332deSLinus Walleij } 677d560168bSGuenter Roeck } 678d560168bSGuenter Roeck } 679d560168bSGuenter Roeck } 680d560168bSGuenter Roeck 681d560168bSGuenter Roeck return hdev; 682d560168bSGuenter Roeck 683d560168bSGuenter Roeck free_hwmon: 684*3bf8bdcfSGuenter Roeck hwmon_dev_release(hdev); 685d560168bSGuenter Roeck ida_remove: 686d560168bSGuenter Roeck ida_simple_remove(&hwmon_ida, id); 687d560168bSGuenter Roeck return ERR_PTR(err); 688d560168bSGuenter Roeck } 689d560168bSGuenter Roeck 6901236441fSMark M. Hoffman /** 691bab2243cSGuenter Roeck * hwmon_device_register_with_groups - register w/ hwmon 692bab2243cSGuenter Roeck * @dev: the parent device 693bab2243cSGuenter Roeck * @name: hwmon name attribute 694bab2243cSGuenter Roeck * @drvdata: driver data to attach to created device 695bab2243cSGuenter Roeck * @groups: List of attribute groups to create 696bab2243cSGuenter Roeck * 697bab2243cSGuenter Roeck * hwmon_device_unregister() must be called when the device is no 698bab2243cSGuenter Roeck * longer needed. 699bab2243cSGuenter Roeck * 700bab2243cSGuenter Roeck * Returns the pointer to the new device. 701bab2243cSGuenter Roeck */ 702bab2243cSGuenter Roeck struct device * 703bab2243cSGuenter Roeck hwmon_device_register_with_groups(struct device *dev, const char *name, 704bab2243cSGuenter Roeck void *drvdata, 705bab2243cSGuenter Roeck const struct attribute_group **groups) 706bab2243cSGuenter Roeck { 7078353863aSGuenter Roeck if (!name) 7088353863aSGuenter Roeck return ERR_PTR(-EINVAL); 7098353863aSGuenter Roeck 710d560168bSGuenter Roeck return __hwmon_device_register(dev, name, drvdata, NULL, groups); 711bab2243cSGuenter Roeck } 712bab2243cSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups); 713bab2243cSGuenter Roeck 714bab2243cSGuenter Roeck /** 715d560168bSGuenter Roeck * hwmon_device_register_with_info - register w/ hwmon 716d560168bSGuenter Roeck * @dev: the parent device 717d560168bSGuenter Roeck * @name: hwmon name attribute 718d560168bSGuenter Roeck * @drvdata: driver data to attach to created device 7193870945aSGuenter Roeck * @chip: pointer to hwmon chip information 720848ba0a2SGuenter Roeck * @extra_groups: pointer to list of additional non-standard attribute groups 721d560168bSGuenter Roeck * 722d560168bSGuenter Roeck * hwmon_device_unregister() must be called when the device is no 723d560168bSGuenter Roeck * longer needed. 724d560168bSGuenter Roeck * 725d560168bSGuenter Roeck * Returns the pointer to the new device. 726d560168bSGuenter Roeck */ 727d560168bSGuenter Roeck struct device * 728d560168bSGuenter Roeck hwmon_device_register_with_info(struct device *dev, const char *name, 729d560168bSGuenter Roeck void *drvdata, 730d560168bSGuenter Roeck const struct hwmon_chip_info *chip, 731848ba0a2SGuenter Roeck const struct attribute_group **extra_groups) 732d560168bSGuenter Roeck { 7338353863aSGuenter Roeck if (!name) 7348353863aSGuenter Roeck return ERR_PTR(-EINVAL); 7358353863aSGuenter Roeck 736239552f4SGuenter Roeck if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info)) 737d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 738d560168bSGuenter Roeck 73959df4f4eSLucas Magasweran if (chip && !dev) 74059df4f4eSLucas Magasweran return ERR_PTR(-EINVAL); 74159df4f4eSLucas Magasweran 742848ba0a2SGuenter Roeck return __hwmon_device_register(dev, name, drvdata, chip, extra_groups); 743d560168bSGuenter Roeck } 744d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_info); 745d560168bSGuenter Roeck 746d560168bSGuenter Roeck /** 7471beeffe4STony Jones * hwmon_device_register - register w/ hwmon 7481236441fSMark M. Hoffman * @dev: the device to register 7491236441fSMark M. Hoffman * 7501beeffe4STony Jones * hwmon_device_unregister() must be called when the device is no 7511236441fSMark M. Hoffman * longer needed. 7521236441fSMark M. Hoffman * 7531beeffe4STony Jones * Returns the pointer to the new device. 7541236441fSMark M. Hoffman */ 7551beeffe4STony Jones struct device *hwmon_device_register(struct device *dev) 7561236441fSMark M. Hoffman { 757af1bd36cSGuenter Roeck dev_warn(dev, 758af1bd36cSGuenter Roeck "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n"); 759af1bd36cSGuenter Roeck 7608353863aSGuenter Roeck return __hwmon_device_register(dev, NULL, NULL, NULL, NULL); 7611236441fSMark M. Hoffman } 762839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_register); 7631236441fSMark M. Hoffman 7641236441fSMark M. Hoffman /** 7651236441fSMark M. Hoffman * hwmon_device_unregister - removes the previously registered class device 7661236441fSMark M. Hoffman * 7671beeffe4STony Jones * @dev: the class device to destroy 7681236441fSMark M. Hoffman */ 7691beeffe4STony Jones void hwmon_device_unregister(struct device *dev) 7701236441fSMark M. Hoffman { 7711236441fSMark M. Hoffman int id; 7721236441fSMark M. Hoffman 773739cf3a2SKay Sievers if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) { 7741beeffe4STony Jones device_unregister(dev); 7754ca5f468SJonathan Cameron ida_simple_remove(&hwmon_ida, id); 7761236441fSMark M. Hoffman } else 7771beeffe4STony Jones dev_dbg(dev->parent, 7781236441fSMark M. Hoffman "hwmon_device_unregister() failed: bad class ID!\n"); 7791236441fSMark M. Hoffman } 780839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_unregister); 7811236441fSMark M. Hoffman 78274188cbaSGuenter Roeck static void devm_hwmon_release(struct device *dev, void *res) 78374188cbaSGuenter Roeck { 78474188cbaSGuenter Roeck struct device *hwdev = *(struct device **)res; 78574188cbaSGuenter Roeck 78674188cbaSGuenter Roeck hwmon_device_unregister(hwdev); 78774188cbaSGuenter Roeck } 78874188cbaSGuenter Roeck 78974188cbaSGuenter Roeck /** 79074188cbaSGuenter Roeck * devm_hwmon_device_register_with_groups - register w/ hwmon 79174188cbaSGuenter Roeck * @dev: the parent device 79274188cbaSGuenter Roeck * @name: hwmon name attribute 79374188cbaSGuenter Roeck * @drvdata: driver data to attach to created device 79474188cbaSGuenter Roeck * @groups: List of attribute groups to create 79574188cbaSGuenter Roeck * 79674188cbaSGuenter Roeck * Returns the pointer to the new device. The new device is automatically 79774188cbaSGuenter Roeck * unregistered with the parent device. 79874188cbaSGuenter Roeck */ 79974188cbaSGuenter Roeck struct device * 80074188cbaSGuenter Roeck devm_hwmon_device_register_with_groups(struct device *dev, const char *name, 80174188cbaSGuenter Roeck void *drvdata, 80274188cbaSGuenter Roeck const struct attribute_group **groups) 80374188cbaSGuenter Roeck { 80474188cbaSGuenter Roeck struct device **ptr, *hwdev; 80574188cbaSGuenter Roeck 80674188cbaSGuenter Roeck if (!dev) 80774188cbaSGuenter Roeck return ERR_PTR(-EINVAL); 80874188cbaSGuenter Roeck 80974188cbaSGuenter Roeck ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); 81074188cbaSGuenter Roeck if (!ptr) 81174188cbaSGuenter Roeck return ERR_PTR(-ENOMEM); 81274188cbaSGuenter Roeck 81374188cbaSGuenter Roeck hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups); 81474188cbaSGuenter Roeck if (IS_ERR(hwdev)) 81574188cbaSGuenter Roeck goto error; 81674188cbaSGuenter Roeck 81774188cbaSGuenter Roeck *ptr = hwdev; 81874188cbaSGuenter Roeck devres_add(dev, ptr); 81974188cbaSGuenter Roeck return hwdev; 82074188cbaSGuenter Roeck 82174188cbaSGuenter Roeck error: 82274188cbaSGuenter Roeck devres_free(ptr); 82374188cbaSGuenter Roeck return hwdev; 82474188cbaSGuenter Roeck } 82574188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups); 82674188cbaSGuenter Roeck 827d560168bSGuenter Roeck /** 828d560168bSGuenter Roeck * devm_hwmon_device_register_with_info - register w/ hwmon 829d560168bSGuenter Roeck * @dev: the parent device 830d560168bSGuenter Roeck * @name: hwmon name attribute 831d560168bSGuenter Roeck * @drvdata: driver data to attach to created device 8323870945aSGuenter Roeck * @chip: pointer to hwmon chip information 8333870945aSGuenter Roeck * @groups: pointer to list of driver specific attribute groups 834d560168bSGuenter Roeck * 835d560168bSGuenter Roeck * Returns the pointer to the new device. The new device is automatically 836d560168bSGuenter Roeck * unregistered with the parent device. 837d560168bSGuenter Roeck */ 838d560168bSGuenter Roeck struct device * 839d560168bSGuenter Roeck devm_hwmon_device_register_with_info(struct device *dev, const char *name, 840d560168bSGuenter Roeck void *drvdata, 841d560168bSGuenter Roeck const struct hwmon_chip_info *chip, 842d560168bSGuenter Roeck const struct attribute_group **groups) 843d560168bSGuenter Roeck { 844d560168bSGuenter Roeck struct device **ptr, *hwdev; 845d560168bSGuenter Roeck 846d560168bSGuenter Roeck if (!dev) 847d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 848d560168bSGuenter Roeck 849d560168bSGuenter Roeck ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); 850d560168bSGuenter Roeck if (!ptr) 851d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 852d560168bSGuenter Roeck 853d560168bSGuenter Roeck hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip, 854d560168bSGuenter Roeck groups); 855d560168bSGuenter Roeck if (IS_ERR(hwdev)) 856d560168bSGuenter Roeck goto error; 857d560168bSGuenter Roeck 858d560168bSGuenter Roeck *ptr = hwdev; 859d560168bSGuenter Roeck devres_add(dev, ptr); 860d560168bSGuenter Roeck 861d560168bSGuenter Roeck return hwdev; 862d560168bSGuenter Roeck 863d560168bSGuenter Roeck error: 864d560168bSGuenter Roeck devres_free(ptr); 865d560168bSGuenter Roeck return hwdev; 866d560168bSGuenter Roeck } 867d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info); 868d560168bSGuenter Roeck 86974188cbaSGuenter Roeck static int devm_hwmon_match(struct device *dev, void *res, void *data) 87074188cbaSGuenter Roeck { 87174188cbaSGuenter Roeck struct device **hwdev = res; 87274188cbaSGuenter Roeck 87374188cbaSGuenter Roeck return *hwdev == data; 87474188cbaSGuenter Roeck } 87574188cbaSGuenter Roeck 87674188cbaSGuenter Roeck /** 87774188cbaSGuenter Roeck * devm_hwmon_device_unregister - removes a previously registered hwmon device 87874188cbaSGuenter Roeck * 87974188cbaSGuenter Roeck * @dev: the parent device of the device to unregister 88074188cbaSGuenter Roeck */ 88174188cbaSGuenter Roeck void devm_hwmon_device_unregister(struct device *dev) 88274188cbaSGuenter Roeck { 88374188cbaSGuenter Roeck WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev)); 88474188cbaSGuenter Roeck } 88574188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister); 88674188cbaSGuenter Roeck 8872958b1ecSJean Delvare static void __init hwmon_pci_quirks(void) 8882958b1ecSJean Delvare { 8892958b1ecSJean Delvare #if defined CONFIG_X86 && defined CONFIG_PCI 8902958b1ecSJean Delvare struct pci_dev *sb; 8912958b1ecSJean Delvare u16 base; 8922958b1ecSJean Delvare u8 enable; 8932958b1ecSJean Delvare 8942958b1ecSJean Delvare /* Open access to 0x295-0x296 on MSI MS-7031 */ 8952958b1ecSJean Delvare sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL); 896d6dab7ddSJean Delvare if (sb) { 897d6dab7ddSJean Delvare if (sb->subsystem_vendor == 0x1462 && /* MSI */ 898d6dab7ddSJean Delvare sb->subsystem_device == 0x0031) { /* MS-7031 */ 8992958b1ecSJean Delvare pci_read_config_byte(sb, 0x48, &enable); 9002958b1ecSJean Delvare pci_read_config_word(sb, 0x64, &base); 9012958b1ecSJean Delvare 9022958b1ecSJean Delvare if (base == 0 && !(enable & BIT(2))) { 9032958b1ecSJean Delvare dev_info(&sb->dev, 9042958b1ecSJean Delvare "Opening wide generic port at 0x295\n"); 9052958b1ecSJean Delvare pci_write_config_word(sb, 0x64, 0x295); 906d6dab7ddSJean Delvare pci_write_config_byte(sb, 0x48, 907d6dab7ddSJean Delvare enable | BIT(2)); 9082958b1ecSJean Delvare } 9092958b1ecSJean Delvare } 910d6dab7ddSJean Delvare pci_dev_put(sb); 911d6dab7ddSJean Delvare } 9122958b1ecSJean Delvare #endif 9132958b1ecSJean Delvare } 9142958b1ecSJean Delvare 9151236441fSMark M. Hoffman static int __init hwmon_init(void) 9161236441fSMark M. Hoffman { 917bab2243cSGuenter Roeck int err; 918bab2243cSGuenter Roeck 9192958b1ecSJean Delvare hwmon_pci_quirks(); 9202958b1ecSJean Delvare 921bab2243cSGuenter Roeck err = class_register(&hwmon_class); 922bab2243cSGuenter Roeck if (err) { 923bab2243cSGuenter Roeck pr_err("couldn't register hwmon sysfs class\n"); 924bab2243cSGuenter Roeck return err; 9251236441fSMark M. Hoffman } 9261236441fSMark M. Hoffman return 0; 9271236441fSMark M. Hoffman } 9281236441fSMark M. Hoffman 9291236441fSMark M. Hoffman static void __exit hwmon_exit(void) 9301236441fSMark M. Hoffman { 931bab2243cSGuenter Roeck class_unregister(&hwmon_class); 9321236441fSMark M. Hoffman } 9331236441fSMark M. Hoffman 93437f54ee5SDavid Brownell subsys_initcall(hwmon_init); 9351236441fSMark M. Hoffman module_exit(hwmon_exit); 9361236441fSMark M. Hoffman 9371236441fSMark M. Hoffman MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>"); 9381236441fSMark M. Hoffman MODULE_DESCRIPTION("hardware monitoring sysfs/class support"); 9391236441fSMark M. Hoffman MODULE_LICENSE("GPL"); 9401236441fSMark M. Hoffman 941