11236441fSMark M. Hoffman /* 25ed04880SGuenter Roeck * hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring 35ed04880SGuenter Roeck * 45ed04880SGuenter Roeck * This file defines the sysfs class "hwmon", for use by sensors drivers. 55ed04880SGuenter Roeck * 65ed04880SGuenter Roeck * Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com> 75ed04880SGuenter Roeck * 85ed04880SGuenter Roeck * This program is free software; you can redistribute it and/or modify 95ed04880SGuenter Roeck * it under the terms of the GNU General Public License as published by 105ed04880SGuenter Roeck * the Free Software Foundation; version 2 of the License. 111236441fSMark M. Hoffman */ 121236441fSMark M. Hoffman 13c95df1aeSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 14c95df1aeSJoe Perches 15d560168bSGuenter Roeck #include <linux/bitops.h> 161236441fSMark M. Hoffman #include <linux/device.h> 171236441fSMark M. Hoffman #include <linux/err.h> 188c65b4a6STim Schmielau #include <linux/gfp.h> 19c9ebbe6fSGuenter Roeck #include <linux/hwmon.h> 20c9ebbe6fSGuenter Roeck #include <linux/idr.h> 21c9ebbe6fSGuenter Roeck #include <linux/module.h> 222958b1ecSJean Delvare #include <linux/pci.h> 23c9ebbe6fSGuenter Roeck #include <linux/slab.h> 24648cd48cSGuenter Roeck #include <linux/string.h> 25d560168bSGuenter Roeck #include <linux/thermal.h> 261236441fSMark M. Hoffman 2761b8ab2cSNicolin Chen #define CREATE_TRACE_POINTS 2861b8ab2cSNicolin Chen #include <trace/events/hwmon.h> 2961b8ab2cSNicolin Chen 301236441fSMark M. Hoffman #define HWMON_ID_PREFIX "hwmon" 311236441fSMark M. Hoffman #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d" 321236441fSMark M. Hoffman 33bab2243cSGuenter Roeck struct hwmon_device { 34bab2243cSGuenter Roeck const char *name; 35bab2243cSGuenter Roeck struct device dev; 36d560168bSGuenter Roeck const struct hwmon_chip_info *chip; 37d560168bSGuenter Roeck 38d560168bSGuenter Roeck struct attribute_group group; 39d560168bSGuenter Roeck const struct attribute_group **groups; 40bab2243cSGuenter Roeck }; 41d560168bSGuenter Roeck 42bab2243cSGuenter Roeck #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev) 43bab2243cSGuenter Roeck 443a412d5eSGuenter Roeck #define MAX_SYSFS_ATTR_NAME_LENGTH 32 453a412d5eSGuenter Roeck 46d560168bSGuenter Roeck struct hwmon_device_attribute { 47d560168bSGuenter Roeck struct device_attribute dev_attr; 48d560168bSGuenter Roeck const struct hwmon_ops *ops; 49d560168bSGuenter Roeck enum hwmon_sensor_types type; 50d560168bSGuenter Roeck u32 attr; 51d560168bSGuenter Roeck int index; 523a412d5eSGuenter Roeck char name[MAX_SYSFS_ATTR_NAME_LENGTH]; 53d560168bSGuenter Roeck }; 54d560168bSGuenter Roeck 55d560168bSGuenter Roeck #define to_hwmon_attr(d) \ 56d560168bSGuenter Roeck container_of(d, struct hwmon_device_attribute, dev_attr) 57d560168bSGuenter Roeck 58d560168bSGuenter Roeck /* 59d560168bSGuenter Roeck * Thermal zone information 60d560168bSGuenter Roeck * In addition to the reference to the hwmon device, 61d560168bSGuenter Roeck * also provides the sensor index. 62d560168bSGuenter Roeck */ 63d560168bSGuenter Roeck struct hwmon_thermal_data { 64d560168bSGuenter Roeck struct hwmon_device *hwdev; /* Reference to hwmon device */ 65d560168bSGuenter Roeck int index; /* sensor index */ 66d560168bSGuenter Roeck }; 67d560168bSGuenter Roeck 68bab2243cSGuenter Roeck static ssize_t 692ab0c6c5SJulia Lawall name_show(struct device *dev, struct device_attribute *attr, char *buf) 70bab2243cSGuenter Roeck { 71bab2243cSGuenter Roeck return sprintf(buf, "%s\n", to_hwmon_device(dev)->name); 72bab2243cSGuenter Roeck } 732ab0c6c5SJulia Lawall static DEVICE_ATTR_RO(name); 74bab2243cSGuenter Roeck 75bab2243cSGuenter Roeck static struct attribute *hwmon_dev_attrs[] = { 76bab2243cSGuenter Roeck &dev_attr_name.attr, 77bab2243cSGuenter Roeck NULL 78bab2243cSGuenter Roeck }; 79bab2243cSGuenter Roeck 80bab2243cSGuenter Roeck static umode_t hwmon_dev_name_is_visible(struct kobject *kobj, 81bab2243cSGuenter Roeck struct attribute *attr, int n) 82bab2243cSGuenter Roeck { 83bab2243cSGuenter Roeck struct device *dev = container_of(kobj, struct device, kobj); 84bab2243cSGuenter Roeck 85bab2243cSGuenter Roeck if (to_hwmon_device(dev)->name == NULL) 86bab2243cSGuenter Roeck return 0; 87bab2243cSGuenter Roeck 88bab2243cSGuenter Roeck return attr->mode; 89bab2243cSGuenter Roeck } 90bab2243cSGuenter Roeck 91524703acSArvind Yadav static const struct attribute_group hwmon_dev_attr_group = { 92bab2243cSGuenter Roeck .attrs = hwmon_dev_attrs, 93bab2243cSGuenter Roeck .is_visible = hwmon_dev_name_is_visible, 94bab2243cSGuenter Roeck }; 95bab2243cSGuenter Roeck 96bab2243cSGuenter Roeck static const struct attribute_group *hwmon_dev_attr_groups[] = { 97bab2243cSGuenter Roeck &hwmon_dev_attr_group, 98bab2243cSGuenter Roeck NULL 99bab2243cSGuenter Roeck }; 100bab2243cSGuenter Roeck 101bab2243cSGuenter Roeck static void hwmon_dev_release(struct device *dev) 102bab2243cSGuenter Roeck { 103bab2243cSGuenter Roeck kfree(to_hwmon_device(dev)); 104bab2243cSGuenter Roeck } 105bab2243cSGuenter Roeck 106bab2243cSGuenter Roeck static struct class hwmon_class = { 107bab2243cSGuenter Roeck .name = "hwmon", 108bab2243cSGuenter Roeck .owner = THIS_MODULE, 109bab2243cSGuenter Roeck .dev_groups = hwmon_dev_attr_groups, 110bab2243cSGuenter Roeck .dev_release = hwmon_dev_release, 111bab2243cSGuenter Roeck }; 1121236441fSMark M. Hoffman 1134ca5f468SJonathan Cameron static DEFINE_IDA(hwmon_ida); 1141236441fSMark M. Hoffman 115d560168bSGuenter Roeck /* Thermal zone handling */ 116d560168bSGuenter Roeck 11786430c1aSGuenter Roeck /* 11886430c1aSGuenter Roeck * The complex conditional is necessary to avoid a cyclic dependency 11986430c1aSGuenter Roeck * between hwmon and thermal_sys modules. 12086430c1aSGuenter Roeck */ 121f3735332SDaniel Lezcano #ifdef CONFIG_THERMAL_OF 122d560168bSGuenter Roeck static int hwmon_thermal_get_temp(void *data, int *temp) 123d560168bSGuenter Roeck { 124d560168bSGuenter Roeck struct hwmon_thermal_data *tdata = data; 125d560168bSGuenter Roeck struct hwmon_device *hwdev = tdata->hwdev; 126d560168bSGuenter Roeck int ret; 127d560168bSGuenter Roeck long t; 128d560168bSGuenter Roeck 129d560168bSGuenter Roeck ret = hwdev->chip->ops->read(&hwdev->dev, hwmon_temp, hwmon_temp_input, 130d560168bSGuenter Roeck tdata->index, &t); 131d560168bSGuenter Roeck if (ret < 0) 132d560168bSGuenter Roeck return ret; 133d560168bSGuenter Roeck 134d560168bSGuenter Roeck *temp = t; 135d560168bSGuenter Roeck 136d560168bSGuenter Roeck return 0; 137d560168bSGuenter Roeck } 138d560168bSGuenter Roeck 139c9920650SJulia Lawall static const struct thermal_zone_of_device_ops hwmon_thermal_ops = { 140d560168bSGuenter Roeck .get_temp = hwmon_thermal_get_temp, 141d560168bSGuenter Roeck }; 142d560168bSGuenter Roeck 143d560168bSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev, 144d560168bSGuenter Roeck struct hwmon_device *hwdev, int index) 145d560168bSGuenter Roeck { 146d560168bSGuenter Roeck struct hwmon_thermal_data *tdata; 14747c332deSLinus Walleij struct thermal_zone_device *tzd; 148d560168bSGuenter Roeck 149d560168bSGuenter Roeck tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL); 150d560168bSGuenter Roeck if (!tdata) 151d560168bSGuenter Roeck return -ENOMEM; 152d560168bSGuenter Roeck 153d560168bSGuenter Roeck tdata->hwdev = hwdev; 154d560168bSGuenter Roeck tdata->index = index; 155d560168bSGuenter Roeck 15647c332deSLinus Walleij tzd = devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata, 157d560168bSGuenter Roeck &hwmon_thermal_ops); 15847c332deSLinus Walleij /* 15947c332deSLinus Walleij * If CONFIG_THERMAL_OF is disabled, this returns -ENODEV, 16047c332deSLinus Walleij * so ignore that error but forward any other error. 16147c332deSLinus Walleij */ 16247c332deSLinus Walleij if (IS_ERR(tzd) && (PTR_ERR(tzd) != -ENODEV)) 16347c332deSLinus Walleij return PTR_ERR(tzd); 164d560168bSGuenter Roeck 165d560168bSGuenter Roeck return 0; 166d560168bSGuenter Roeck } 167d560168bSGuenter Roeck #else 168d560168bSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev, 169d560168bSGuenter Roeck struct hwmon_device *hwdev, int index) 170d560168bSGuenter Roeck { 171d560168bSGuenter Roeck return 0; 172d560168bSGuenter Roeck } 17386430c1aSGuenter Roeck #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */ 174d560168bSGuenter Roeck 17561b8ab2cSNicolin Chen static int hwmon_attr_base(enum hwmon_sensor_types type) 17661b8ab2cSNicolin Chen { 17761b8ab2cSNicolin Chen if (type == hwmon_in) 17861b8ab2cSNicolin Chen return 0; 17961b8ab2cSNicolin Chen return 1; 18061b8ab2cSNicolin Chen } 18161b8ab2cSNicolin Chen 182d560168bSGuenter Roeck /* sysfs attribute management */ 183d560168bSGuenter Roeck 184d560168bSGuenter Roeck static ssize_t hwmon_attr_show(struct device *dev, 185d560168bSGuenter Roeck struct device_attribute *devattr, char *buf) 186d560168bSGuenter Roeck { 187d560168bSGuenter Roeck struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); 188d560168bSGuenter Roeck long val; 189d560168bSGuenter Roeck int ret; 190d560168bSGuenter Roeck 191d560168bSGuenter Roeck ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index, 192d560168bSGuenter Roeck &val); 193d560168bSGuenter Roeck if (ret < 0) 194d560168bSGuenter Roeck return ret; 195d560168bSGuenter Roeck 19661b8ab2cSNicolin Chen trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type), 19761b8ab2cSNicolin Chen hattr->name, val); 19861b8ab2cSNicolin Chen 199d560168bSGuenter Roeck return sprintf(buf, "%ld\n", val); 200d560168bSGuenter Roeck } 201d560168bSGuenter Roeck 202e159ab5cSGuenter Roeck static ssize_t hwmon_attr_show_string(struct device *dev, 203e159ab5cSGuenter Roeck struct device_attribute *devattr, 204e159ab5cSGuenter Roeck char *buf) 205e159ab5cSGuenter Roeck { 206e159ab5cSGuenter Roeck struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); 20761b8ab2cSNicolin Chen enum hwmon_sensor_types type = hattr->type; 2085ba6bcbcSJean Delvare const char *s; 209e159ab5cSGuenter Roeck int ret; 210e159ab5cSGuenter Roeck 211e159ab5cSGuenter Roeck ret = hattr->ops->read_string(dev, hattr->type, hattr->attr, 212e159ab5cSGuenter Roeck hattr->index, &s); 213e159ab5cSGuenter Roeck if (ret < 0) 214e159ab5cSGuenter Roeck return ret; 215e159ab5cSGuenter Roeck 21661b8ab2cSNicolin Chen trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type), 21761b8ab2cSNicolin Chen hattr->name, s); 21861b8ab2cSNicolin Chen 219e159ab5cSGuenter Roeck return sprintf(buf, "%s\n", s); 220e159ab5cSGuenter Roeck } 221e159ab5cSGuenter Roeck 222d560168bSGuenter Roeck static ssize_t hwmon_attr_store(struct device *dev, 223d560168bSGuenter Roeck struct device_attribute *devattr, 224d560168bSGuenter Roeck const char *buf, size_t count) 225d560168bSGuenter Roeck { 226d560168bSGuenter Roeck struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); 227d560168bSGuenter Roeck long val; 228d560168bSGuenter Roeck int ret; 229d560168bSGuenter Roeck 230d560168bSGuenter Roeck ret = kstrtol(buf, 10, &val); 231d560168bSGuenter Roeck if (ret < 0) 232d560168bSGuenter Roeck return ret; 233d560168bSGuenter Roeck 234d560168bSGuenter Roeck ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index, 235d560168bSGuenter Roeck val); 236d560168bSGuenter Roeck if (ret < 0) 237d560168bSGuenter Roeck return ret; 238d560168bSGuenter Roeck 23961b8ab2cSNicolin Chen trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type), 24061b8ab2cSNicolin Chen hattr->name, val); 241d560168bSGuenter Roeck 24261b8ab2cSNicolin Chen return count; 243d560168bSGuenter Roeck } 244d560168bSGuenter Roeck 245e159ab5cSGuenter Roeck static bool is_string_attr(enum hwmon_sensor_types type, u32 attr) 246e159ab5cSGuenter Roeck { 247e159ab5cSGuenter Roeck return (type == hwmon_temp && attr == hwmon_temp_label) || 248e159ab5cSGuenter Roeck (type == hwmon_in && attr == hwmon_in_label) || 249e159ab5cSGuenter Roeck (type == hwmon_curr && attr == hwmon_curr_label) || 250e159ab5cSGuenter Roeck (type == hwmon_power && attr == hwmon_power_label) || 251e159ab5cSGuenter Roeck (type == hwmon_energy && attr == hwmon_energy_label) || 252e159ab5cSGuenter Roeck (type == hwmon_humidity && attr == hwmon_humidity_label) || 253e159ab5cSGuenter Roeck (type == hwmon_fan && attr == hwmon_fan_label); 254e159ab5cSGuenter Roeck } 255e159ab5cSGuenter Roeck 256d560168bSGuenter Roeck static struct attribute *hwmon_genattr(struct device *dev, 257d560168bSGuenter Roeck const void *drvdata, 258d560168bSGuenter Roeck enum hwmon_sensor_types type, 259d560168bSGuenter Roeck u32 attr, 260d560168bSGuenter Roeck int index, 261d560168bSGuenter Roeck const char *template, 262d560168bSGuenter Roeck const struct hwmon_ops *ops) 263d560168bSGuenter Roeck { 264d560168bSGuenter Roeck struct hwmon_device_attribute *hattr; 265d560168bSGuenter Roeck struct device_attribute *dattr; 266d560168bSGuenter Roeck struct attribute *a; 267d560168bSGuenter Roeck umode_t mode; 2683b443defSRasmus Villemoes const char *name; 269e159ab5cSGuenter Roeck bool is_string = is_string_attr(type, attr); 270d560168bSGuenter Roeck 271d560168bSGuenter Roeck /* The attribute is invisible if there is no template string */ 272d560168bSGuenter Roeck if (!template) 273d560168bSGuenter Roeck return ERR_PTR(-ENOENT); 274d560168bSGuenter Roeck 275d560168bSGuenter Roeck mode = ops->is_visible(drvdata, type, attr, index); 276d560168bSGuenter Roeck if (!mode) 277d560168bSGuenter Roeck return ERR_PTR(-ENOENT); 278d560168bSGuenter Roeck 2790d87116fSGuenter Roeck if ((mode & 0444) && ((is_string && !ops->read_string) || 280e159ab5cSGuenter Roeck (!is_string && !ops->read))) 281d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 2820d87116fSGuenter Roeck if ((mode & 0222) && !ops->write) 283d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 284d560168bSGuenter Roeck 285d560168bSGuenter Roeck hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL); 286d560168bSGuenter Roeck if (!hattr) 287d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 288d560168bSGuenter Roeck 2893a412d5eSGuenter Roeck if (type == hwmon_chip) { 2903b443defSRasmus Villemoes name = template; 2913a412d5eSGuenter Roeck } else { 2923a412d5eSGuenter Roeck scnprintf(hattr->name, sizeof(hattr->name), template, 2933a412d5eSGuenter Roeck index + hwmon_attr_base(type)); 2943a412d5eSGuenter Roeck name = hattr->name; 2953a412d5eSGuenter Roeck } 2963a412d5eSGuenter Roeck 297d560168bSGuenter Roeck hattr->type = type; 298d560168bSGuenter Roeck hattr->attr = attr; 299d560168bSGuenter Roeck hattr->index = index; 300d560168bSGuenter Roeck hattr->ops = ops; 301d560168bSGuenter Roeck 302d560168bSGuenter Roeck dattr = &hattr->dev_attr; 303e159ab5cSGuenter Roeck dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show; 304d560168bSGuenter Roeck dattr->store = hwmon_attr_store; 305d560168bSGuenter Roeck 306d560168bSGuenter Roeck a = &dattr->attr; 307d560168bSGuenter Roeck sysfs_attr_init(a); 308d560168bSGuenter Roeck a->name = name; 309d560168bSGuenter Roeck a->mode = mode; 310d560168bSGuenter Roeck 311d560168bSGuenter Roeck return a; 312d560168bSGuenter Roeck } 313d560168bSGuenter Roeck 314f4d325d5SGuenter Roeck /* 315f4d325d5SGuenter Roeck * Chip attributes are not attribute templates but actual sysfs attributes. 316f4d325d5SGuenter Roeck * See hwmon_genattr() for special handling. 317f4d325d5SGuenter Roeck */ 318f4d325d5SGuenter Roeck static const char * const hwmon_chip_attrs[] = { 319d560168bSGuenter Roeck [hwmon_chip_temp_reset_history] = "temp_reset_history", 32000d616cfSGuenter Roeck [hwmon_chip_in_reset_history] = "in_reset_history", 3219b26947cSGuenter Roeck [hwmon_chip_curr_reset_history] = "curr_reset_history", 322b308f5c7SGuenter Roeck [hwmon_chip_power_reset_history] = "power_reset_history", 323d560168bSGuenter Roeck [hwmon_chip_update_interval] = "update_interval", 324d560168bSGuenter Roeck [hwmon_chip_alarms] = "alarms", 3259f00995eSGuenter Roeck [hwmon_chip_samples] = "samples", 3269f00995eSGuenter Roeck [hwmon_chip_curr_samples] = "curr_samples", 3279f00995eSGuenter Roeck [hwmon_chip_in_samples] = "in_samples", 3289f00995eSGuenter Roeck [hwmon_chip_power_samples] = "power_samples", 3299f00995eSGuenter Roeck [hwmon_chip_temp_samples] = "temp_samples", 330d560168bSGuenter Roeck }; 331d560168bSGuenter Roeck 332d560168bSGuenter Roeck static const char * const hwmon_temp_attr_templates[] = { 333d560168bSGuenter Roeck [hwmon_temp_input] = "temp%d_input", 334d560168bSGuenter Roeck [hwmon_temp_type] = "temp%d_type", 335d560168bSGuenter Roeck [hwmon_temp_lcrit] = "temp%d_lcrit", 336d560168bSGuenter Roeck [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst", 337d560168bSGuenter Roeck [hwmon_temp_min] = "temp%d_min", 338d560168bSGuenter Roeck [hwmon_temp_min_hyst] = "temp%d_min_hyst", 339d560168bSGuenter Roeck [hwmon_temp_max] = "temp%d_max", 340d560168bSGuenter Roeck [hwmon_temp_max_hyst] = "temp%d_max_hyst", 341d560168bSGuenter Roeck [hwmon_temp_crit] = "temp%d_crit", 342d560168bSGuenter Roeck [hwmon_temp_crit_hyst] = "temp%d_crit_hyst", 343d560168bSGuenter Roeck [hwmon_temp_emergency] = "temp%d_emergency", 344d560168bSGuenter Roeck [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst", 345d560168bSGuenter Roeck [hwmon_temp_alarm] = "temp%d_alarm", 346d560168bSGuenter Roeck [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm", 347d560168bSGuenter Roeck [hwmon_temp_min_alarm] = "temp%d_min_alarm", 348d560168bSGuenter Roeck [hwmon_temp_max_alarm] = "temp%d_max_alarm", 349d560168bSGuenter Roeck [hwmon_temp_crit_alarm] = "temp%d_crit_alarm", 350d560168bSGuenter Roeck [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm", 351d560168bSGuenter Roeck [hwmon_temp_fault] = "temp%d_fault", 352d560168bSGuenter Roeck [hwmon_temp_offset] = "temp%d_offset", 353d560168bSGuenter Roeck [hwmon_temp_label] = "temp%d_label", 354d560168bSGuenter Roeck [hwmon_temp_lowest] = "temp%d_lowest", 355d560168bSGuenter Roeck [hwmon_temp_highest] = "temp%d_highest", 356d560168bSGuenter Roeck [hwmon_temp_reset_history] = "temp%d_reset_history", 357d560168bSGuenter Roeck }; 358d560168bSGuenter Roeck 35900d616cfSGuenter Roeck static const char * const hwmon_in_attr_templates[] = { 36000d616cfSGuenter Roeck [hwmon_in_input] = "in%d_input", 36100d616cfSGuenter Roeck [hwmon_in_min] = "in%d_min", 36200d616cfSGuenter Roeck [hwmon_in_max] = "in%d_max", 36300d616cfSGuenter Roeck [hwmon_in_lcrit] = "in%d_lcrit", 36400d616cfSGuenter Roeck [hwmon_in_crit] = "in%d_crit", 36500d616cfSGuenter Roeck [hwmon_in_average] = "in%d_average", 36600d616cfSGuenter Roeck [hwmon_in_lowest] = "in%d_lowest", 36700d616cfSGuenter Roeck [hwmon_in_highest] = "in%d_highest", 36800d616cfSGuenter Roeck [hwmon_in_reset_history] = "in%d_reset_history", 36900d616cfSGuenter Roeck [hwmon_in_label] = "in%d_label", 37000d616cfSGuenter Roeck [hwmon_in_alarm] = "in%d_alarm", 37100d616cfSGuenter Roeck [hwmon_in_min_alarm] = "in%d_min_alarm", 37200d616cfSGuenter Roeck [hwmon_in_max_alarm] = "in%d_max_alarm", 37300d616cfSGuenter Roeck [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm", 37400d616cfSGuenter Roeck [hwmon_in_crit_alarm] = "in%d_crit_alarm", 37568c0d69dSNicolin Chen [hwmon_in_enable] = "in%d_enable", 37600d616cfSGuenter Roeck }; 37700d616cfSGuenter Roeck 3789b26947cSGuenter Roeck static const char * const hwmon_curr_attr_templates[] = { 3799b26947cSGuenter Roeck [hwmon_curr_input] = "curr%d_input", 3809b26947cSGuenter Roeck [hwmon_curr_min] = "curr%d_min", 3819b26947cSGuenter Roeck [hwmon_curr_max] = "curr%d_max", 3829b26947cSGuenter Roeck [hwmon_curr_lcrit] = "curr%d_lcrit", 3839b26947cSGuenter Roeck [hwmon_curr_crit] = "curr%d_crit", 3849b26947cSGuenter Roeck [hwmon_curr_average] = "curr%d_average", 3859b26947cSGuenter Roeck [hwmon_curr_lowest] = "curr%d_lowest", 3869b26947cSGuenter Roeck [hwmon_curr_highest] = "curr%d_highest", 3879b26947cSGuenter Roeck [hwmon_curr_reset_history] = "curr%d_reset_history", 3889b26947cSGuenter Roeck [hwmon_curr_label] = "curr%d_label", 3899b26947cSGuenter Roeck [hwmon_curr_alarm] = "curr%d_alarm", 3909b26947cSGuenter Roeck [hwmon_curr_min_alarm] = "curr%d_min_alarm", 3919b26947cSGuenter Roeck [hwmon_curr_max_alarm] = "curr%d_max_alarm", 3929b26947cSGuenter Roeck [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm", 3939b26947cSGuenter Roeck [hwmon_curr_crit_alarm] = "curr%d_crit_alarm", 3949b26947cSGuenter Roeck }; 3959b26947cSGuenter Roeck 396b308f5c7SGuenter Roeck static const char * const hwmon_power_attr_templates[] = { 397b308f5c7SGuenter Roeck [hwmon_power_average] = "power%d_average", 398b308f5c7SGuenter Roeck [hwmon_power_average_interval] = "power%d_average_interval", 399b308f5c7SGuenter Roeck [hwmon_power_average_interval_max] = "power%d_interval_max", 400b308f5c7SGuenter Roeck [hwmon_power_average_interval_min] = "power%d_interval_min", 401b308f5c7SGuenter Roeck [hwmon_power_average_highest] = "power%d_average_highest", 402b308f5c7SGuenter Roeck [hwmon_power_average_lowest] = "power%d_average_lowest", 403b308f5c7SGuenter Roeck [hwmon_power_average_max] = "power%d_average_max", 404b308f5c7SGuenter Roeck [hwmon_power_average_min] = "power%d_average_min", 405b308f5c7SGuenter Roeck [hwmon_power_input] = "power%d_input", 406b308f5c7SGuenter Roeck [hwmon_power_input_highest] = "power%d_input_highest", 407b308f5c7SGuenter Roeck [hwmon_power_input_lowest] = "power%d_input_lowest", 408b308f5c7SGuenter Roeck [hwmon_power_reset_history] = "power%d_reset_history", 409b308f5c7SGuenter Roeck [hwmon_power_accuracy] = "power%d_accuracy", 410b308f5c7SGuenter Roeck [hwmon_power_cap] = "power%d_cap", 411b308f5c7SGuenter Roeck [hwmon_power_cap_hyst] = "power%d_cap_hyst", 412b308f5c7SGuenter Roeck [hwmon_power_cap_max] = "power%d_cap_max", 413b308f5c7SGuenter Roeck [hwmon_power_cap_min] = "power%d_cap_min", 414aa7f29b0SAndrew Lunn [hwmon_power_min] = "power%d_min", 415b308f5c7SGuenter Roeck [hwmon_power_max] = "power%d_max", 416aa7f29b0SAndrew Lunn [hwmon_power_lcrit] = "power%d_lcrit", 417b308f5c7SGuenter Roeck [hwmon_power_crit] = "power%d_crit", 418b308f5c7SGuenter Roeck [hwmon_power_label] = "power%d_label", 419b308f5c7SGuenter Roeck [hwmon_power_alarm] = "power%d_alarm", 420b308f5c7SGuenter Roeck [hwmon_power_cap_alarm] = "power%d_cap_alarm", 421aa7f29b0SAndrew Lunn [hwmon_power_min_alarm] = "power%d_min_alarm", 422b308f5c7SGuenter Roeck [hwmon_power_max_alarm] = "power%d_max_alarm", 423aa7f29b0SAndrew Lunn [hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm", 424b308f5c7SGuenter Roeck [hwmon_power_crit_alarm] = "power%d_crit_alarm", 425b308f5c7SGuenter Roeck }; 426b308f5c7SGuenter Roeck 4276bfcca44SGuenter Roeck static const char * const hwmon_energy_attr_templates[] = { 4286bfcca44SGuenter Roeck [hwmon_energy_input] = "energy%d_input", 4296bfcca44SGuenter Roeck [hwmon_energy_label] = "energy%d_label", 4306bfcca44SGuenter Roeck }; 4316bfcca44SGuenter Roeck 4326bfcca44SGuenter Roeck static const char * const hwmon_humidity_attr_templates[] = { 4336bfcca44SGuenter Roeck [hwmon_humidity_input] = "humidity%d_input", 4346bfcca44SGuenter Roeck [hwmon_humidity_label] = "humidity%d_label", 4356bfcca44SGuenter Roeck [hwmon_humidity_min] = "humidity%d_min", 4366bfcca44SGuenter Roeck [hwmon_humidity_min_hyst] = "humidity%d_min_hyst", 4376bfcca44SGuenter Roeck [hwmon_humidity_max] = "humidity%d_max", 4386bfcca44SGuenter Roeck [hwmon_humidity_max_hyst] = "humidity%d_max_hyst", 4396bfcca44SGuenter Roeck [hwmon_humidity_alarm] = "humidity%d_alarm", 4406bfcca44SGuenter Roeck [hwmon_humidity_fault] = "humidity%d_fault", 4416bfcca44SGuenter Roeck }; 4426bfcca44SGuenter Roeck 4438faee73fSGuenter Roeck static const char * const hwmon_fan_attr_templates[] = { 4448faee73fSGuenter Roeck [hwmon_fan_input] = "fan%d_input", 4458faee73fSGuenter Roeck [hwmon_fan_label] = "fan%d_label", 4468faee73fSGuenter Roeck [hwmon_fan_min] = "fan%d_min", 4478faee73fSGuenter Roeck [hwmon_fan_max] = "fan%d_max", 4488faee73fSGuenter Roeck [hwmon_fan_div] = "fan%d_div", 4498faee73fSGuenter Roeck [hwmon_fan_pulses] = "fan%d_pulses", 4508faee73fSGuenter Roeck [hwmon_fan_target] = "fan%d_target", 4518faee73fSGuenter Roeck [hwmon_fan_alarm] = "fan%d_alarm", 4528faee73fSGuenter Roeck [hwmon_fan_min_alarm] = "fan%d_min_alarm", 4538faee73fSGuenter Roeck [hwmon_fan_max_alarm] = "fan%d_max_alarm", 4548faee73fSGuenter Roeck [hwmon_fan_fault] = "fan%d_fault", 4558faee73fSGuenter Roeck }; 4568faee73fSGuenter Roeck 457f9f7bb3aSGuenter Roeck static const char * const hwmon_pwm_attr_templates[] = { 458f9f7bb3aSGuenter Roeck [hwmon_pwm_input] = "pwm%d", 459f9f7bb3aSGuenter Roeck [hwmon_pwm_enable] = "pwm%d_enable", 460f9f7bb3aSGuenter Roeck [hwmon_pwm_mode] = "pwm%d_mode", 461f9f7bb3aSGuenter Roeck [hwmon_pwm_freq] = "pwm%d_freq", 462f9f7bb3aSGuenter Roeck }; 463f9f7bb3aSGuenter Roeck 464d560168bSGuenter Roeck static const char * const *__templates[] = { 465f4d325d5SGuenter Roeck [hwmon_chip] = hwmon_chip_attrs, 466d560168bSGuenter Roeck [hwmon_temp] = hwmon_temp_attr_templates, 46700d616cfSGuenter Roeck [hwmon_in] = hwmon_in_attr_templates, 4689b26947cSGuenter Roeck [hwmon_curr] = hwmon_curr_attr_templates, 469b308f5c7SGuenter Roeck [hwmon_power] = hwmon_power_attr_templates, 4706bfcca44SGuenter Roeck [hwmon_energy] = hwmon_energy_attr_templates, 4716bfcca44SGuenter Roeck [hwmon_humidity] = hwmon_humidity_attr_templates, 4728faee73fSGuenter Roeck [hwmon_fan] = hwmon_fan_attr_templates, 473f9f7bb3aSGuenter Roeck [hwmon_pwm] = hwmon_pwm_attr_templates, 474d560168bSGuenter Roeck }; 475d560168bSGuenter Roeck 476d560168bSGuenter Roeck static const int __templates_size[] = { 477f4d325d5SGuenter Roeck [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs), 478d560168bSGuenter Roeck [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates), 47900d616cfSGuenter Roeck [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates), 4809b26947cSGuenter Roeck [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates), 481b308f5c7SGuenter Roeck [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates), 4826bfcca44SGuenter Roeck [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates), 4836bfcca44SGuenter Roeck [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates), 4848faee73fSGuenter Roeck [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates), 485f9f7bb3aSGuenter Roeck [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates), 486d560168bSGuenter Roeck }; 487d560168bSGuenter Roeck 488d560168bSGuenter Roeck static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info) 489d560168bSGuenter Roeck { 490d560168bSGuenter Roeck int i, n; 491d560168bSGuenter Roeck 492d560168bSGuenter Roeck for (i = n = 0; info->config[i]; i++) 493d560168bSGuenter Roeck n += hweight32(info->config[i]); 494d560168bSGuenter Roeck 495d560168bSGuenter Roeck return n; 496d560168bSGuenter Roeck } 497d560168bSGuenter Roeck 498d560168bSGuenter Roeck static int hwmon_genattrs(struct device *dev, 499d560168bSGuenter Roeck const void *drvdata, 500d560168bSGuenter Roeck struct attribute **attrs, 501d560168bSGuenter Roeck const struct hwmon_ops *ops, 502d560168bSGuenter Roeck const struct hwmon_channel_info *info) 503d560168bSGuenter Roeck { 504d560168bSGuenter Roeck const char * const *templates; 505d560168bSGuenter Roeck int template_size; 506d560168bSGuenter Roeck int i, aindex = 0; 507d560168bSGuenter Roeck 508d560168bSGuenter Roeck if (info->type >= ARRAY_SIZE(__templates)) 509d560168bSGuenter Roeck return -EINVAL; 510d560168bSGuenter Roeck 511d560168bSGuenter Roeck templates = __templates[info->type]; 512d560168bSGuenter Roeck template_size = __templates_size[info->type]; 513d560168bSGuenter Roeck 514d560168bSGuenter Roeck for (i = 0; info->config[i]; i++) { 515d560168bSGuenter Roeck u32 attr_mask = info->config[i]; 516d560168bSGuenter Roeck u32 attr; 517d560168bSGuenter Roeck 518d560168bSGuenter Roeck while (attr_mask) { 519d560168bSGuenter Roeck struct attribute *a; 520d560168bSGuenter Roeck 521d560168bSGuenter Roeck attr = __ffs(attr_mask); 522d560168bSGuenter Roeck attr_mask &= ~BIT(attr); 523d560168bSGuenter Roeck if (attr >= template_size) 524d560168bSGuenter Roeck return -EINVAL; 525d560168bSGuenter Roeck a = hwmon_genattr(dev, drvdata, info->type, attr, i, 526d560168bSGuenter Roeck templates[attr], ops); 527d560168bSGuenter Roeck if (IS_ERR(a)) { 528d560168bSGuenter Roeck if (PTR_ERR(a) != -ENOENT) 529d560168bSGuenter Roeck return PTR_ERR(a); 530d560168bSGuenter Roeck continue; 531d560168bSGuenter Roeck } 532d560168bSGuenter Roeck attrs[aindex++] = a; 533d560168bSGuenter Roeck } 534d560168bSGuenter Roeck } 535d560168bSGuenter Roeck return aindex; 536d560168bSGuenter Roeck } 537d560168bSGuenter Roeck 538d560168bSGuenter Roeck static struct attribute ** 539d560168bSGuenter Roeck __hwmon_create_attrs(struct device *dev, const void *drvdata, 540d560168bSGuenter Roeck const struct hwmon_chip_info *chip) 541d560168bSGuenter Roeck { 542d560168bSGuenter Roeck int ret, i, aindex = 0, nattrs = 0; 543d560168bSGuenter Roeck struct attribute **attrs; 544d560168bSGuenter Roeck 545d560168bSGuenter Roeck for (i = 0; chip->info[i]; i++) 546d560168bSGuenter Roeck nattrs += hwmon_num_channel_attrs(chip->info[i]); 547d560168bSGuenter Roeck 548d560168bSGuenter Roeck if (nattrs == 0) 549d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 550d560168bSGuenter Roeck 551d560168bSGuenter Roeck attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL); 552d560168bSGuenter Roeck if (!attrs) 553d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 554d560168bSGuenter Roeck 555d560168bSGuenter Roeck for (i = 0; chip->info[i]; i++) { 556d560168bSGuenter Roeck ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops, 557d560168bSGuenter Roeck chip->info[i]); 558d560168bSGuenter Roeck if (ret < 0) 559d560168bSGuenter Roeck return ERR_PTR(ret); 560d560168bSGuenter Roeck aindex += ret; 561d560168bSGuenter Roeck } 562d560168bSGuenter Roeck 563d560168bSGuenter Roeck return attrs; 564d560168bSGuenter Roeck } 565d560168bSGuenter Roeck 566d560168bSGuenter Roeck static struct device * 567d560168bSGuenter Roeck __hwmon_device_register(struct device *dev, const char *name, void *drvdata, 568d560168bSGuenter Roeck const struct hwmon_chip_info *chip, 569d560168bSGuenter Roeck const struct attribute_group **groups) 570d560168bSGuenter Roeck { 571d560168bSGuenter Roeck struct hwmon_device *hwdev; 572d560168bSGuenter Roeck struct device *hdev; 573d560168bSGuenter Roeck int i, j, err, id; 574d560168bSGuenter Roeck 57574d3b641SGuenter Roeck /* Complain about invalid characters in hwmon name attribute */ 576d560168bSGuenter Roeck if (name && (!strlen(name) || strpbrk(name, "-* \t\n"))) 57774d3b641SGuenter Roeck dev_warn(dev, 57874d3b641SGuenter Roeck "hwmon: '%s' is not a valid name attribute, please fix\n", 57974d3b641SGuenter Roeck name); 580d560168bSGuenter Roeck 581d560168bSGuenter Roeck id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL); 582d560168bSGuenter Roeck if (id < 0) 583d560168bSGuenter Roeck return ERR_PTR(id); 584d560168bSGuenter Roeck 585d560168bSGuenter Roeck hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL); 586d560168bSGuenter Roeck if (hwdev == NULL) { 587d560168bSGuenter Roeck err = -ENOMEM; 588d560168bSGuenter Roeck goto ida_remove; 589d560168bSGuenter Roeck } 590d560168bSGuenter Roeck 591d560168bSGuenter Roeck hdev = &hwdev->dev; 592d560168bSGuenter Roeck 593239552f4SGuenter Roeck if (chip) { 594d560168bSGuenter Roeck struct attribute **attrs; 595b2a4cc3aSGuenter Roeck int ngroups = 2; /* terminating NULL plus &hwdev->groups */ 596d560168bSGuenter Roeck 597d560168bSGuenter Roeck if (groups) 598d560168bSGuenter Roeck for (i = 0; groups[i]; i++) 599d560168bSGuenter Roeck ngroups++; 600d560168bSGuenter Roeck 601d560168bSGuenter Roeck hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups), 602d560168bSGuenter Roeck GFP_KERNEL); 60338d8ed65SColin Ian King if (!hwdev->groups) { 60438d8ed65SColin Ian King err = -ENOMEM; 60538d8ed65SColin Ian King goto free_hwmon; 60638d8ed65SColin Ian King } 607d560168bSGuenter Roeck 608d560168bSGuenter Roeck attrs = __hwmon_create_attrs(dev, drvdata, chip); 609d560168bSGuenter Roeck if (IS_ERR(attrs)) { 610d560168bSGuenter Roeck err = PTR_ERR(attrs); 611d560168bSGuenter Roeck goto free_hwmon; 612d560168bSGuenter Roeck } 613d560168bSGuenter Roeck 614d560168bSGuenter Roeck hwdev->group.attrs = attrs; 615d560168bSGuenter Roeck ngroups = 0; 616d560168bSGuenter Roeck hwdev->groups[ngroups++] = &hwdev->group; 617d560168bSGuenter Roeck 618d560168bSGuenter Roeck if (groups) { 619d560168bSGuenter Roeck for (i = 0; groups[i]; i++) 620d560168bSGuenter Roeck hwdev->groups[ngroups++] = groups[i]; 621d560168bSGuenter Roeck } 622d560168bSGuenter Roeck 623d560168bSGuenter Roeck hdev->groups = hwdev->groups; 624d560168bSGuenter Roeck } else { 625d560168bSGuenter Roeck hdev->groups = groups; 626d560168bSGuenter Roeck } 627d560168bSGuenter Roeck 628d560168bSGuenter Roeck hwdev->name = name; 629d560168bSGuenter Roeck hdev->class = &hwmon_class; 630d560168bSGuenter Roeck hdev->parent = dev; 631d560168bSGuenter Roeck hdev->of_node = dev ? dev->of_node : NULL; 632d560168bSGuenter Roeck hwdev->chip = chip; 633d560168bSGuenter Roeck dev_set_drvdata(hdev, drvdata); 634d560168bSGuenter Roeck dev_set_name(hdev, HWMON_ID_FORMAT, id); 635d560168bSGuenter Roeck err = device_register(hdev); 636d560168bSGuenter Roeck if (err) 637d560168bSGuenter Roeck goto free_hwmon; 638d560168bSGuenter Roeck 639*c41dd48eSEduardo Valentin if (dev && dev->of_node && chip && chip->ops->read && 640d560168bSGuenter Roeck chip->info[0]->type == hwmon_chip && 641d560168bSGuenter Roeck (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) { 642d560168bSGuenter Roeck const struct hwmon_channel_info **info = chip->info; 643d560168bSGuenter Roeck 644d560168bSGuenter Roeck for (i = 1; info[i]; i++) { 645d560168bSGuenter Roeck if (info[i]->type != hwmon_temp) 646d560168bSGuenter Roeck continue; 647d560168bSGuenter Roeck 648d560168bSGuenter Roeck for (j = 0; info[i]->config[j]; j++) { 649d560168bSGuenter Roeck if (!chip->ops->is_visible(drvdata, hwmon_temp, 650d560168bSGuenter Roeck hwmon_temp_input, j)) 651d560168bSGuenter Roeck continue; 65247c332deSLinus Walleij if (info[i]->config[j] & HWMON_T_INPUT) { 65347c332deSLinus Walleij err = hwmon_thermal_add_sensor(dev, 65447c332deSLinus Walleij hwdev, j); 65574e35127SDmitry Osipenko if (err) { 65674e35127SDmitry Osipenko device_unregister(hdev); 65774e35127SDmitry Osipenko goto ida_remove; 65874e35127SDmitry Osipenko } 65947c332deSLinus Walleij } 660d560168bSGuenter Roeck } 661d560168bSGuenter Roeck } 662d560168bSGuenter Roeck } 663d560168bSGuenter Roeck 664d560168bSGuenter Roeck return hdev; 665d560168bSGuenter Roeck 666d560168bSGuenter Roeck free_hwmon: 667d560168bSGuenter Roeck kfree(hwdev); 668d560168bSGuenter Roeck ida_remove: 669d560168bSGuenter Roeck ida_simple_remove(&hwmon_ida, id); 670d560168bSGuenter Roeck return ERR_PTR(err); 671d560168bSGuenter Roeck } 672d560168bSGuenter Roeck 6731236441fSMark M. Hoffman /** 674bab2243cSGuenter Roeck * hwmon_device_register_with_groups - register w/ hwmon 675bab2243cSGuenter Roeck * @dev: the parent device 676bab2243cSGuenter Roeck * @name: hwmon name attribute 677bab2243cSGuenter Roeck * @drvdata: driver data to attach to created device 678bab2243cSGuenter Roeck * @groups: List of attribute groups to create 679bab2243cSGuenter Roeck * 680bab2243cSGuenter Roeck * hwmon_device_unregister() must be called when the device is no 681bab2243cSGuenter Roeck * longer needed. 682bab2243cSGuenter Roeck * 683bab2243cSGuenter Roeck * Returns the pointer to the new device. 684bab2243cSGuenter Roeck */ 685bab2243cSGuenter Roeck struct device * 686bab2243cSGuenter Roeck hwmon_device_register_with_groups(struct device *dev, const char *name, 687bab2243cSGuenter Roeck void *drvdata, 688bab2243cSGuenter Roeck const struct attribute_group **groups) 689bab2243cSGuenter Roeck { 6908353863aSGuenter Roeck if (!name) 6918353863aSGuenter Roeck return ERR_PTR(-EINVAL); 6928353863aSGuenter Roeck 693d560168bSGuenter Roeck return __hwmon_device_register(dev, name, drvdata, NULL, groups); 694bab2243cSGuenter Roeck } 695bab2243cSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups); 696bab2243cSGuenter Roeck 697bab2243cSGuenter Roeck /** 698d560168bSGuenter Roeck * hwmon_device_register_with_info - register w/ hwmon 699d560168bSGuenter Roeck * @dev: the parent device 700d560168bSGuenter Roeck * @name: hwmon name attribute 701d560168bSGuenter Roeck * @drvdata: driver data to attach to created device 7023870945aSGuenter Roeck * @chip: pointer to hwmon chip information 703848ba0a2SGuenter Roeck * @extra_groups: pointer to list of additional non-standard attribute groups 704d560168bSGuenter Roeck * 705d560168bSGuenter Roeck * hwmon_device_unregister() must be called when the device is no 706d560168bSGuenter Roeck * longer needed. 707d560168bSGuenter Roeck * 708d560168bSGuenter Roeck * Returns the pointer to the new device. 709d560168bSGuenter Roeck */ 710d560168bSGuenter Roeck struct device * 711d560168bSGuenter Roeck hwmon_device_register_with_info(struct device *dev, const char *name, 712d560168bSGuenter Roeck void *drvdata, 713d560168bSGuenter Roeck const struct hwmon_chip_info *chip, 714848ba0a2SGuenter Roeck const struct attribute_group **extra_groups) 715d560168bSGuenter Roeck { 7168353863aSGuenter Roeck if (!name) 7178353863aSGuenter Roeck return ERR_PTR(-EINVAL); 7188353863aSGuenter Roeck 719239552f4SGuenter Roeck if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info)) 720d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 721d560168bSGuenter Roeck 72259df4f4eSLucas Magasweran if (chip && !dev) 72359df4f4eSLucas Magasweran return ERR_PTR(-EINVAL); 72459df4f4eSLucas Magasweran 725848ba0a2SGuenter Roeck return __hwmon_device_register(dev, name, drvdata, chip, extra_groups); 726d560168bSGuenter Roeck } 727d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_info); 728d560168bSGuenter Roeck 729d560168bSGuenter Roeck /** 7301beeffe4STony Jones * hwmon_device_register - register w/ hwmon 7311236441fSMark M. Hoffman * @dev: the device to register 7321236441fSMark M. Hoffman * 7331beeffe4STony Jones * hwmon_device_unregister() must be called when the device is no 7341236441fSMark M. Hoffman * longer needed. 7351236441fSMark M. Hoffman * 7361beeffe4STony Jones * Returns the pointer to the new device. 7371236441fSMark M. Hoffman */ 7381beeffe4STony Jones struct device *hwmon_device_register(struct device *dev) 7391236441fSMark M. Hoffman { 740af1bd36cSGuenter Roeck dev_warn(dev, 741af1bd36cSGuenter Roeck "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n"); 742af1bd36cSGuenter Roeck 7438353863aSGuenter Roeck return __hwmon_device_register(dev, NULL, NULL, NULL, NULL); 7441236441fSMark M. Hoffman } 745839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_register); 7461236441fSMark M. Hoffman 7471236441fSMark M. Hoffman /** 7481236441fSMark M. Hoffman * hwmon_device_unregister - removes the previously registered class device 7491236441fSMark M. Hoffman * 7501beeffe4STony Jones * @dev: the class device to destroy 7511236441fSMark M. Hoffman */ 7521beeffe4STony Jones void hwmon_device_unregister(struct device *dev) 7531236441fSMark M. Hoffman { 7541236441fSMark M. Hoffman int id; 7551236441fSMark M. Hoffman 756739cf3a2SKay Sievers if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) { 7571beeffe4STony Jones device_unregister(dev); 7584ca5f468SJonathan Cameron ida_simple_remove(&hwmon_ida, id); 7591236441fSMark M. Hoffman } else 7601beeffe4STony Jones dev_dbg(dev->parent, 7611236441fSMark M. Hoffman "hwmon_device_unregister() failed: bad class ID!\n"); 7621236441fSMark M. Hoffman } 763839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_unregister); 7641236441fSMark M. Hoffman 76574188cbaSGuenter Roeck static void devm_hwmon_release(struct device *dev, void *res) 76674188cbaSGuenter Roeck { 76774188cbaSGuenter Roeck struct device *hwdev = *(struct device **)res; 76874188cbaSGuenter Roeck 76974188cbaSGuenter Roeck hwmon_device_unregister(hwdev); 77074188cbaSGuenter Roeck } 77174188cbaSGuenter Roeck 77274188cbaSGuenter Roeck /** 77374188cbaSGuenter Roeck * devm_hwmon_device_register_with_groups - register w/ hwmon 77474188cbaSGuenter Roeck * @dev: the parent device 77574188cbaSGuenter Roeck * @name: hwmon name attribute 77674188cbaSGuenter Roeck * @drvdata: driver data to attach to created device 77774188cbaSGuenter Roeck * @groups: List of attribute groups to create 77874188cbaSGuenter Roeck * 77974188cbaSGuenter Roeck * Returns the pointer to the new device. The new device is automatically 78074188cbaSGuenter Roeck * unregistered with the parent device. 78174188cbaSGuenter Roeck */ 78274188cbaSGuenter Roeck struct device * 78374188cbaSGuenter Roeck devm_hwmon_device_register_with_groups(struct device *dev, const char *name, 78474188cbaSGuenter Roeck void *drvdata, 78574188cbaSGuenter Roeck const struct attribute_group **groups) 78674188cbaSGuenter Roeck { 78774188cbaSGuenter Roeck struct device **ptr, *hwdev; 78874188cbaSGuenter Roeck 78974188cbaSGuenter Roeck if (!dev) 79074188cbaSGuenter Roeck return ERR_PTR(-EINVAL); 79174188cbaSGuenter Roeck 79274188cbaSGuenter Roeck ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); 79374188cbaSGuenter Roeck if (!ptr) 79474188cbaSGuenter Roeck return ERR_PTR(-ENOMEM); 79574188cbaSGuenter Roeck 79674188cbaSGuenter Roeck hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups); 79774188cbaSGuenter Roeck if (IS_ERR(hwdev)) 79874188cbaSGuenter Roeck goto error; 79974188cbaSGuenter Roeck 80074188cbaSGuenter Roeck *ptr = hwdev; 80174188cbaSGuenter Roeck devres_add(dev, ptr); 80274188cbaSGuenter Roeck return hwdev; 80374188cbaSGuenter Roeck 80474188cbaSGuenter Roeck error: 80574188cbaSGuenter Roeck devres_free(ptr); 80674188cbaSGuenter Roeck return hwdev; 80774188cbaSGuenter Roeck } 80874188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups); 80974188cbaSGuenter Roeck 810d560168bSGuenter Roeck /** 811d560168bSGuenter Roeck * devm_hwmon_device_register_with_info - register w/ hwmon 812d560168bSGuenter Roeck * @dev: the parent device 813d560168bSGuenter Roeck * @name: hwmon name attribute 814d560168bSGuenter Roeck * @drvdata: driver data to attach to created device 8153870945aSGuenter Roeck * @chip: pointer to hwmon chip information 8163870945aSGuenter Roeck * @groups: pointer to list of driver specific attribute groups 817d560168bSGuenter Roeck * 818d560168bSGuenter Roeck * Returns the pointer to the new device. The new device is automatically 819d560168bSGuenter Roeck * unregistered with the parent device. 820d560168bSGuenter Roeck */ 821d560168bSGuenter Roeck struct device * 822d560168bSGuenter Roeck devm_hwmon_device_register_with_info(struct device *dev, const char *name, 823d560168bSGuenter Roeck void *drvdata, 824d560168bSGuenter Roeck const struct hwmon_chip_info *chip, 825d560168bSGuenter Roeck const struct attribute_group **groups) 826d560168bSGuenter Roeck { 827d560168bSGuenter Roeck struct device **ptr, *hwdev; 828d560168bSGuenter Roeck 829d560168bSGuenter Roeck if (!dev) 830d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 831d560168bSGuenter Roeck 832d560168bSGuenter Roeck ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); 833d560168bSGuenter Roeck if (!ptr) 834d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 835d560168bSGuenter Roeck 836d560168bSGuenter Roeck hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip, 837d560168bSGuenter Roeck groups); 838d560168bSGuenter Roeck if (IS_ERR(hwdev)) 839d560168bSGuenter Roeck goto error; 840d560168bSGuenter Roeck 841d560168bSGuenter Roeck *ptr = hwdev; 842d560168bSGuenter Roeck devres_add(dev, ptr); 843d560168bSGuenter Roeck 844d560168bSGuenter Roeck return hwdev; 845d560168bSGuenter Roeck 846d560168bSGuenter Roeck error: 847d560168bSGuenter Roeck devres_free(ptr); 848d560168bSGuenter Roeck return hwdev; 849d560168bSGuenter Roeck } 850d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info); 851d560168bSGuenter Roeck 85274188cbaSGuenter Roeck static int devm_hwmon_match(struct device *dev, void *res, void *data) 85374188cbaSGuenter Roeck { 85474188cbaSGuenter Roeck struct device **hwdev = res; 85574188cbaSGuenter Roeck 85674188cbaSGuenter Roeck return *hwdev == data; 85774188cbaSGuenter Roeck } 85874188cbaSGuenter Roeck 85974188cbaSGuenter Roeck /** 86074188cbaSGuenter Roeck * devm_hwmon_device_unregister - removes a previously registered hwmon device 86174188cbaSGuenter Roeck * 86274188cbaSGuenter Roeck * @dev: the parent device of the device to unregister 86374188cbaSGuenter Roeck */ 86474188cbaSGuenter Roeck void devm_hwmon_device_unregister(struct device *dev) 86574188cbaSGuenter Roeck { 86674188cbaSGuenter Roeck WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev)); 86774188cbaSGuenter Roeck } 86874188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister); 86974188cbaSGuenter Roeck 8702958b1ecSJean Delvare static void __init hwmon_pci_quirks(void) 8712958b1ecSJean Delvare { 8722958b1ecSJean Delvare #if defined CONFIG_X86 && defined CONFIG_PCI 8732958b1ecSJean Delvare struct pci_dev *sb; 8742958b1ecSJean Delvare u16 base; 8752958b1ecSJean Delvare u8 enable; 8762958b1ecSJean Delvare 8772958b1ecSJean Delvare /* Open access to 0x295-0x296 on MSI MS-7031 */ 8782958b1ecSJean Delvare sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL); 879d6dab7ddSJean Delvare if (sb) { 880d6dab7ddSJean Delvare if (sb->subsystem_vendor == 0x1462 && /* MSI */ 881d6dab7ddSJean Delvare sb->subsystem_device == 0x0031) { /* MS-7031 */ 8822958b1ecSJean Delvare pci_read_config_byte(sb, 0x48, &enable); 8832958b1ecSJean Delvare pci_read_config_word(sb, 0x64, &base); 8842958b1ecSJean Delvare 8852958b1ecSJean Delvare if (base == 0 && !(enable & BIT(2))) { 8862958b1ecSJean Delvare dev_info(&sb->dev, 8872958b1ecSJean Delvare "Opening wide generic port at 0x295\n"); 8882958b1ecSJean Delvare pci_write_config_word(sb, 0x64, 0x295); 889d6dab7ddSJean Delvare pci_write_config_byte(sb, 0x48, 890d6dab7ddSJean Delvare enable | BIT(2)); 8912958b1ecSJean Delvare } 8922958b1ecSJean Delvare } 893d6dab7ddSJean Delvare pci_dev_put(sb); 894d6dab7ddSJean Delvare } 8952958b1ecSJean Delvare #endif 8962958b1ecSJean Delvare } 8972958b1ecSJean Delvare 8981236441fSMark M. Hoffman static int __init hwmon_init(void) 8991236441fSMark M. Hoffman { 900bab2243cSGuenter Roeck int err; 901bab2243cSGuenter Roeck 9022958b1ecSJean Delvare hwmon_pci_quirks(); 9032958b1ecSJean Delvare 904bab2243cSGuenter Roeck err = class_register(&hwmon_class); 905bab2243cSGuenter Roeck if (err) { 906bab2243cSGuenter Roeck pr_err("couldn't register hwmon sysfs class\n"); 907bab2243cSGuenter Roeck return err; 9081236441fSMark M. Hoffman } 9091236441fSMark M. Hoffman return 0; 9101236441fSMark M. Hoffman } 9111236441fSMark M. Hoffman 9121236441fSMark M. Hoffman static void __exit hwmon_exit(void) 9131236441fSMark M. Hoffman { 914bab2243cSGuenter Roeck class_unregister(&hwmon_class); 9151236441fSMark M. Hoffman } 9161236441fSMark M. Hoffman 91737f54ee5SDavid Brownell subsys_initcall(hwmon_init); 9181236441fSMark M. Hoffman module_exit(hwmon_exit); 9191236441fSMark M. Hoffman 9201236441fSMark M. Hoffman MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>"); 9211236441fSMark M. Hoffman MODULE_DESCRIPTION("hardware monitoring sysfs/class support"); 9221236441fSMark M. Hoffman MODULE_LICENSE("GPL"); 9231236441fSMark M. Hoffman 924