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 */ 12186430c1aSGuenter Roeck #if IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF) && \ 12286430c1aSGuenter Roeck (!defined(CONFIG_THERMAL_HWMON) || \ 12386430c1aSGuenter Roeck !(defined(MODULE) && IS_MODULE(CONFIG_THERMAL))) 124d560168bSGuenter Roeck static int hwmon_thermal_get_temp(void *data, int *temp) 125d560168bSGuenter Roeck { 126d560168bSGuenter Roeck struct hwmon_thermal_data *tdata = data; 127d560168bSGuenter Roeck struct hwmon_device *hwdev = tdata->hwdev; 128d560168bSGuenter Roeck int ret; 129d560168bSGuenter Roeck long t; 130d560168bSGuenter Roeck 131d560168bSGuenter Roeck ret = hwdev->chip->ops->read(&hwdev->dev, hwmon_temp, hwmon_temp_input, 132d560168bSGuenter Roeck tdata->index, &t); 133d560168bSGuenter Roeck if (ret < 0) 134d560168bSGuenter Roeck return ret; 135d560168bSGuenter Roeck 136d560168bSGuenter Roeck *temp = t; 137d560168bSGuenter Roeck 138d560168bSGuenter Roeck return 0; 139d560168bSGuenter Roeck } 140d560168bSGuenter Roeck 141c9920650SJulia Lawall static const struct thermal_zone_of_device_ops hwmon_thermal_ops = { 142d560168bSGuenter Roeck .get_temp = hwmon_thermal_get_temp, 143d560168bSGuenter Roeck }; 144d560168bSGuenter Roeck 145d560168bSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev, 146d560168bSGuenter Roeck struct hwmon_device *hwdev, int index) 147d560168bSGuenter Roeck { 148d560168bSGuenter Roeck struct hwmon_thermal_data *tdata; 14947c332deSLinus Walleij struct thermal_zone_device *tzd; 150d560168bSGuenter Roeck 151d560168bSGuenter Roeck tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL); 152d560168bSGuenter Roeck if (!tdata) 153d560168bSGuenter Roeck return -ENOMEM; 154d560168bSGuenter Roeck 155d560168bSGuenter Roeck tdata->hwdev = hwdev; 156d560168bSGuenter Roeck tdata->index = index; 157d560168bSGuenter Roeck 15847c332deSLinus Walleij tzd = devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata, 159d560168bSGuenter Roeck &hwmon_thermal_ops); 16047c332deSLinus Walleij /* 16147c332deSLinus Walleij * If CONFIG_THERMAL_OF is disabled, this returns -ENODEV, 16247c332deSLinus Walleij * so ignore that error but forward any other error. 16347c332deSLinus Walleij */ 16447c332deSLinus Walleij if (IS_ERR(tzd) && (PTR_ERR(tzd) != -ENODEV)) 16547c332deSLinus Walleij return PTR_ERR(tzd); 166d560168bSGuenter Roeck 167d560168bSGuenter Roeck return 0; 168d560168bSGuenter Roeck } 169d560168bSGuenter Roeck #else 170d560168bSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev, 171d560168bSGuenter Roeck struct hwmon_device *hwdev, int index) 172d560168bSGuenter Roeck { 173d560168bSGuenter Roeck return 0; 174d560168bSGuenter Roeck } 17586430c1aSGuenter Roeck #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */ 176d560168bSGuenter Roeck 17761b8ab2cSNicolin Chen static int hwmon_attr_base(enum hwmon_sensor_types type) 17861b8ab2cSNicolin Chen { 17961b8ab2cSNicolin Chen if (type == hwmon_in) 18061b8ab2cSNicolin Chen return 0; 18161b8ab2cSNicolin Chen return 1; 18261b8ab2cSNicolin Chen } 18361b8ab2cSNicolin Chen 184d560168bSGuenter Roeck /* sysfs attribute management */ 185d560168bSGuenter Roeck 186d560168bSGuenter Roeck static ssize_t hwmon_attr_show(struct device *dev, 187d560168bSGuenter Roeck struct device_attribute *devattr, char *buf) 188d560168bSGuenter Roeck { 189d560168bSGuenter Roeck struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); 190d560168bSGuenter Roeck long val; 191d560168bSGuenter Roeck int ret; 192d560168bSGuenter Roeck 193d560168bSGuenter Roeck ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index, 194d560168bSGuenter Roeck &val); 195d560168bSGuenter Roeck if (ret < 0) 196d560168bSGuenter Roeck return ret; 197d560168bSGuenter Roeck 19861b8ab2cSNicolin Chen trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type), 19961b8ab2cSNicolin Chen hattr->name, val); 20061b8ab2cSNicolin Chen 201d560168bSGuenter Roeck return sprintf(buf, "%ld\n", val); 202d560168bSGuenter Roeck } 203d560168bSGuenter Roeck 204e159ab5cSGuenter Roeck static ssize_t hwmon_attr_show_string(struct device *dev, 205e159ab5cSGuenter Roeck struct device_attribute *devattr, 206e159ab5cSGuenter Roeck char *buf) 207e159ab5cSGuenter Roeck { 208e159ab5cSGuenter Roeck struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); 20961b8ab2cSNicolin Chen enum hwmon_sensor_types type = hattr->type; 2105ba6bcbcSJean Delvare const char *s; 211e159ab5cSGuenter Roeck int ret; 212e159ab5cSGuenter Roeck 213e159ab5cSGuenter Roeck ret = hattr->ops->read_string(dev, hattr->type, hattr->attr, 214e159ab5cSGuenter Roeck hattr->index, &s); 215e159ab5cSGuenter Roeck if (ret < 0) 216e159ab5cSGuenter Roeck return ret; 217e159ab5cSGuenter Roeck 21861b8ab2cSNicolin Chen trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type), 21961b8ab2cSNicolin Chen hattr->name, s); 22061b8ab2cSNicolin Chen 221e159ab5cSGuenter Roeck return sprintf(buf, "%s\n", s); 222e159ab5cSGuenter Roeck } 223e159ab5cSGuenter Roeck 224d560168bSGuenter Roeck static ssize_t hwmon_attr_store(struct device *dev, 225d560168bSGuenter Roeck struct device_attribute *devattr, 226d560168bSGuenter Roeck const char *buf, size_t count) 227d560168bSGuenter Roeck { 228d560168bSGuenter Roeck struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); 229d560168bSGuenter Roeck long val; 230d560168bSGuenter Roeck int ret; 231d560168bSGuenter Roeck 232d560168bSGuenter Roeck ret = kstrtol(buf, 10, &val); 233d560168bSGuenter Roeck if (ret < 0) 234d560168bSGuenter Roeck return ret; 235d560168bSGuenter Roeck 236d560168bSGuenter Roeck ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index, 237d560168bSGuenter Roeck val); 238d560168bSGuenter Roeck if (ret < 0) 239d560168bSGuenter Roeck return ret; 240d560168bSGuenter Roeck 24161b8ab2cSNicolin Chen trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type), 24261b8ab2cSNicolin Chen hattr->name, val); 243d560168bSGuenter Roeck 24461b8ab2cSNicolin Chen return count; 245d560168bSGuenter Roeck } 246d560168bSGuenter Roeck 247e159ab5cSGuenter Roeck static bool is_string_attr(enum hwmon_sensor_types type, u32 attr) 248e159ab5cSGuenter Roeck { 249e159ab5cSGuenter Roeck return (type == hwmon_temp && attr == hwmon_temp_label) || 250e159ab5cSGuenter Roeck (type == hwmon_in && attr == hwmon_in_label) || 251e159ab5cSGuenter Roeck (type == hwmon_curr && attr == hwmon_curr_label) || 252e159ab5cSGuenter Roeck (type == hwmon_power && attr == hwmon_power_label) || 253e159ab5cSGuenter Roeck (type == hwmon_energy && attr == hwmon_energy_label) || 254e159ab5cSGuenter Roeck (type == hwmon_humidity && attr == hwmon_humidity_label) || 255e159ab5cSGuenter Roeck (type == hwmon_fan && attr == hwmon_fan_label); 256e159ab5cSGuenter Roeck } 257e159ab5cSGuenter Roeck 258d560168bSGuenter Roeck static struct attribute *hwmon_genattr(struct device *dev, 259d560168bSGuenter Roeck const void *drvdata, 260d560168bSGuenter Roeck enum hwmon_sensor_types type, 261d560168bSGuenter Roeck u32 attr, 262d560168bSGuenter Roeck int index, 263d560168bSGuenter Roeck const char *template, 264d560168bSGuenter Roeck const struct hwmon_ops *ops) 265d560168bSGuenter Roeck { 266d560168bSGuenter Roeck struct hwmon_device_attribute *hattr; 267d560168bSGuenter Roeck struct device_attribute *dattr; 268d560168bSGuenter Roeck struct attribute *a; 269d560168bSGuenter Roeck umode_t mode; 2703b443defSRasmus Villemoes const char *name; 271e159ab5cSGuenter Roeck bool is_string = is_string_attr(type, attr); 272d560168bSGuenter Roeck 273d560168bSGuenter Roeck /* The attribute is invisible if there is no template string */ 274d560168bSGuenter Roeck if (!template) 275d560168bSGuenter Roeck return ERR_PTR(-ENOENT); 276d560168bSGuenter Roeck 277d560168bSGuenter Roeck mode = ops->is_visible(drvdata, type, attr, index); 278d560168bSGuenter Roeck if (!mode) 279d560168bSGuenter Roeck return ERR_PTR(-ENOENT); 280d560168bSGuenter Roeck 2810d87116fSGuenter Roeck if ((mode & 0444) && ((is_string && !ops->read_string) || 282e159ab5cSGuenter Roeck (!is_string && !ops->read))) 283d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 2840d87116fSGuenter Roeck if ((mode & 0222) && !ops->write) 285d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 286d560168bSGuenter Roeck 287d560168bSGuenter Roeck hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL); 288d560168bSGuenter Roeck if (!hattr) 289d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 290d560168bSGuenter Roeck 2913a412d5eSGuenter Roeck if (type == hwmon_chip) { 2923b443defSRasmus Villemoes name = template; 2933a412d5eSGuenter Roeck } else { 2943a412d5eSGuenter Roeck scnprintf(hattr->name, sizeof(hattr->name), template, 2953a412d5eSGuenter Roeck index + hwmon_attr_base(type)); 2963a412d5eSGuenter Roeck name = hattr->name; 2973a412d5eSGuenter Roeck } 2983a412d5eSGuenter Roeck 299d560168bSGuenter Roeck hattr->type = type; 300d560168bSGuenter Roeck hattr->attr = attr; 301d560168bSGuenter Roeck hattr->index = index; 302d560168bSGuenter Roeck hattr->ops = ops; 303d560168bSGuenter Roeck 304d560168bSGuenter Roeck dattr = &hattr->dev_attr; 305e159ab5cSGuenter Roeck dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show; 306d560168bSGuenter Roeck dattr->store = hwmon_attr_store; 307d560168bSGuenter Roeck 308d560168bSGuenter Roeck a = &dattr->attr; 309d560168bSGuenter Roeck sysfs_attr_init(a); 310d560168bSGuenter Roeck a->name = name; 311d560168bSGuenter Roeck a->mode = mode; 312d560168bSGuenter Roeck 313d560168bSGuenter Roeck return a; 314d560168bSGuenter Roeck } 315d560168bSGuenter Roeck 316f4d325d5SGuenter Roeck /* 317f4d325d5SGuenter Roeck * Chip attributes are not attribute templates but actual sysfs attributes. 318f4d325d5SGuenter Roeck * See hwmon_genattr() for special handling. 319f4d325d5SGuenter Roeck */ 320f4d325d5SGuenter Roeck static const char * const hwmon_chip_attrs[] = { 321d560168bSGuenter Roeck [hwmon_chip_temp_reset_history] = "temp_reset_history", 32200d616cfSGuenter Roeck [hwmon_chip_in_reset_history] = "in_reset_history", 3239b26947cSGuenter Roeck [hwmon_chip_curr_reset_history] = "curr_reset_history", 324b308f5c7SGuenter Roeck [hwmon_chip_power_reset_history] = "power_reset_history", 325d560168bSGuenter Roeck [hwmon_chip_update_interval] = "update_interval", 326d560168bSGuenter Roeck [hwmon_chip_alarms] = "alarms", 327*9f00995eSGuenter Roeck [hwmon_chip_samples] = "samples", 328*9f00995eSGuenter Roeck [hwmon_chip_curr_samples] = "curr_samples", 329*9f00995eSGuenter Roeck [hwmon_chip_in_samples] = "in_samples", 330*9f00995eSGuenter Roeck [hwmon_chip_power_samples] = "power_samples", 331*9f00995eSGuenter Roeck [hwmon_chip_temp_samples] = "temp_samples", 332d560168bSGuenter Roeck }; 333d560168bSGuenter Roeck 334d560168bSGuenter Roeck static const char * const hwmon_temp_attr_templates[] = { 335d560168bSGuenter Roeck [hwmon_temp_input] = "temp%d_input", 336d560168bSGuenter Roeck [hwmon_temp_type] = "temp%d_type", 337d560168bSGuenter Roeck [hwmon_temp_lcrit] = "temp%d_lcrit", 338d560168bSGuenter Roeck [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst", 339d560168bSGuenter Roeck [hwmon_temp_min] = "temp%d_min", 340d560168bSGuenter Roeck [hwmon_temp_min_hyst] = "temp%d_min_hyst", 341d560168bSGuenter Roeck [hwmon_temp_max] = "temp%d_max", 342d560168bSGuenter Roeck [hwmon_temp_max_hyst] = "temp%d_max_hyst", 343d560168bSGuenter Roeck [hwmon_temp_crit] = "temp%d_crit", 344d560168bSGuenter Roeck [hwmon_temp_crit_hyst] = "temp%d_crit_hyst", 345d560168bSGuenter Roeck [hwmon_temp_emergency] = "temp%d_emergency", 346d560168bSGuenter Roeck [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst", 347d560168bSGuenter Roeck [hwmon_temp_alarm] = "temp%d_alarm", 348d560168bSGuenter Roeck [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm", 349d560168bSGuenter Roeck [hwmon_temp_min_alarm] = "temp%d_min_alarm", 350d560168bSGuenter Roeck [hwmon_temp_max_alarm] = "temp%d_max_alarm", 351d560168bSGuenter Roeck [hwmon_temp_crit_alarm] = "temp%d_crit_alarm", 352d560168bSGuenter Roeck [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm", 353d560168bSGuenter Roeck [hwmon_temp_fault] = "temp%d_fault", 354d560168bSGuenter Roeck [hwmon_temp_offset] = "temp%d_offset", 355d560168bSGuenter Roeck [hwmon_temp_label] = "temp%d_label", 356d560168bSGuenter Roeck [hwmon_temp_lowest] = "temp%d_lowest", 357d560168bSGuenter Roeck [hwmon_temp_highest] = "temp%d_highest", 358d560168bSGuenter Roeck [hwmon_temp_reset_history] = "temp%d_reset_history", 359d560168bSGuenter Roeck }; 360d560168bSGuenter Roeck 36100d616cfSGuenter Roeck static const char * const hwmon_in_attr_templates[] = { 36200d616cfSGuenter Roeck [hwmon_in_input] = "in%d_input", 36300d616cfSGuenter Roeck [hwmon_in_min] = "in%d_min", 36400d616cfSGuenter Roeck [hwmon_in_max] = "in%d_max", 36500d616cfSGuenter Roeck [hwmon_in_lcrit] = "in%d_lcrit", 36600d616cfSGuenter Roeck [hwmon_in_crit] = "in%d_crit", 36700d616cfSGuenter Roeck [hwmon_in_average] = "in%d_average", 36800d616cfSGuenter Roeck [hwmon_in_lowest] = "in%d_lowest", 36900d616cfSGuenter Roeck [hwmon_in_highest] = "in%d_highest", 37000d616cfSGuenter Roeck [hwmon_in_reset_history] = "in%d_reset_history", 37100d616cfSGuenter Roeck [hwmon_in_label] = "in%d_label", 37200d616cfSGuenter Roeck [hwmon_in_alarm] = "in%d_alarm", 37300d616cfSGuenter Roeck [hwmon_in_min_alarm] = "in%d_min_alarm", 37400d616cfSGuenter Roeck [hwmon_in_max_alarm] = "in%d_max_alarm", 37500d616cfSGuenter Roeck [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm", 37600d616cfSGuenter Roeck [hwmon_in_crit_alarm] = "in%d_crit_alarm", 37768c0d69dSNicolin Chen [hwmon_in_enable] = "in%d_enable", 37800d616cfSGuenter Roeck }; 37900d616cfSGuenter Roeck 3809b26947cSGuenter Roeck static const char * const hwmon_curr_attr_templates[] = { 3819b26947cSGuenter Roeck [hwmon_curr_input] = "curr%d_input", 3829b26947cSGuenter Roeck [hwmon_curr_min] = "curr%d_min", 3839b26947cSGuenter Roeck [hwmon_curr_max] = "curr%d_max", 3849b26947cSGuenter Roeck [hwmon_curr_lcrit] = "curr%d_lcrit", 3859b26947cSGuenter Roeck [hwmon_curr_crit] = "curr%d_crit", 3869b26947cSGuenter Roeck [hwmon_curr_average] = "curr%d_average", 3879b26947cSGuenter Roeck [hwmon_curr_lowest] = "curr%d_lowest", 3889b26947cSGuenter Roeck [hwmon_curr_highest] = "curr%d_highest", 3899b26947cSGuenter Roeck [hwmon_curr_reset_history] = "curr%d_reset_history", 3909b26947cSGuenter Roeck [hwmon_curr_label] = "curr%d_label", 3919b26947cSGuenter Roeck [hwmon_curr_alarm] = "curr%d_alarm", 3929b26947cSGuenter Roeck [hwmon_curr_min_alarm] = "curr%d_min_alarm", 3939b26947cSGuenter Roeck [hwmon_curr_max_alarm] = "curr%d_max_alarm", 3949b26947cSGuenter Roeck [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm", 3959b26947cSGuenter Roeck [hwmon_curr_crit_alarm] = "curr%d_crit_alarm", 3969b26947cSGuenter Roeck }; 3979b26947cSGuenter Roeck 398b308f5c7SGuenter Roeck static const char * const hwmon_power_attr_templates[] = { 399b308f5c7SGuenter Roeck [hwmon_power_average] = "power%d_average", 400b308f5c7SGuenter Roeck [hwmon_power_average_interval] = "power%d_average_interval", 401b308f5c7SGuenter Roeck [hwmon_power_average_interval_max] = "power%d_interval_max", 402b308f5c7SGuenter Roeck [hwmon_power_average_interval_min] = "power%d_interval_min", 403b308f5c7SGuenter Roeck [hwmon_power_average_highest] = "power%d_average_highest", 404b308f5c7SGuenter Roeck [hwmon_power_average_lowest] = "power%d_average_lowest", 405b308f5c7SGuenter Roeck [hwmon_power_average_max] = "power%d_average_max", 406b308f5c7SGuenter Roeck [hwmon_power_average_min] = "power%d_average_min", 407b308f5c7SGuenter Roeck [hwmon_power_input] = "power%d_input", 408b308f5c7SGuenter Roeck [hwmon_power_input_highest] = "power%d_input_highest", 409b308f5c7SGuenter Roeck [hwmon_power_input_lowest] = "power%d_input_lowest", 410b308f5c7SGuenter Roeck [hwmon_power_reset_history] = "power%d_reset_history", 411b308f5c7SGuenter Roeck [hwmon_power_accuracy] = "power%d_accuracy", 412b308f5c7SGuenter Roeck [hwmon_power_cap] = "power%d_cap", 413b308f5c7SGuenter Roeck [hwmon_power_cap_hyst] = "power%d_cap_hyst", 414b308f5c7SGuenter Roeck [hwmon_power_cap_max] = "power%d_cap_max", 415b308f5c7SGuenter Roeck [hwmon_power_cap_min] = "power%d_cap_min", 416aa7f29b0SAndrew Lunn [hwmon_power_min] = "power%d_min", 417b308f5c7SGuenter Roeck [hwmon_power_max] = "power%d_max", 418aa7f29b0SAndrew Lunn [hwmon_power_lcrit] = "power%d_lcrit", 419b308f5c7SGuenter Roeck [hwmon_power_crit] = "power%d_crit", 420b308f5c7SGuenter Roeck [hwmon_power_label] = "power%d_label", 421b308f5c7SGuenter Roeck [hwmon_power_alarm] = "power%d_alarm", 422b308f5c7SGuenter Roeck [hwmon_power_cap_alarm] = "power%d_cap_alarm", 423aa7f29b0SAndrew Lunn [hwmon_power_min_alarm] = "power%d_min_alarm", 424b308f5c7SGuenter Roeck [hwmon_power_max_alarm] = "power%d_max_alarm", 425aa7f29b0SAndrew Lunn [hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm", 426b308f5c7SGuenter Roeck [hwmon_power_crit_alarm] = "power%d_crit_alarm", 427b308f5c7SGuenter Roeck }; 428b308f5c7SGuenter Roeck 4296bfcca44SGuenter Roeck static const char * const hwmon_energy_attr_templates[] = { 4306bfcca44SGuenter Roeck [hwmon_energy_input] = "energy%d_input", 4316bfcca44SGuenter Roeck [hwmon_energy_label] = "energy%d_label", 4326bfcca44SGuenter Roeck }; 4336bfcca44SGuenter Roeck 4346bfcca44SGuenter Roeck static const char * const hwmon_humidity_attr_templates[] = { 4356bfcca44SGuenter Roeck [hwmon_humidity_input] = "humidity%d_input", 4366bfcca44SGuenter Roeck [hwmon_humidity_label] = "humidity%d_label", 4376bfcca44SGuenter Roeck [hwmon_humidity_min] = "humidity%d_min", 4386bfcca44SGuenter Roeck [hwmon_humidity_min_hyst] = "humidity%d_min_hyst", 4396bfcca44SGuenter Roeck [hwmon_humidity_max] = "humidity%d_max", 4406bfcca44SGuenter Roeck [hwmon_humidity_max_hyst] = "humidity%d_max_hyst", 4416bfcca44SGuenter Roeck [hwmon_humidity_alarm] = "humidity%d_alarm", 4426bfcca44SGuenter Roeck [hwmon_humidity_fault] = "humidity%d_fault", 4436bfcca44SGuenter Roeck }; 4446bfcca44SGuenter Roeck 4458faee73fSGuenter Roeck static const char * const hwmon_fan_attr_templates[] = { 4468faee73fSGuenter Roeck [hwmon_fan_input] = "fan%d_input", 4478faee73fSGuenter Roeck [hwmon_fan_label] = "fan%d_label", 4488faee73fSGuenter Roeck [hwmon_fan_min] = "fan%d_min", 4498faee73fSGuenter Roeck [hwmon_fan_max] = "fan%d_max", 4508faee73fSGuenter Roeck [hwmon_fan_div] = "fan%d_div", 4518faee73fSGuenter Roeck [hwmon_fan_pulses] = "fan%d_pulses", 4528faee73fSGuenter Roeck [hwmon_fan_target] = "fan%d_target", 4538faee73fSGuenter Roeck [hwmon_fan_alarm] = "fan%d_alarm", 4548faee73fSGuenter Roeck [hwmon_fan_min_alarm] = "fan%d_min_alarm", 4558faee73fSGuenter Roeck [hwmon_fan_max_alarm] = "fan%d_max_alarm", 4568faee73fSGuenter Roeck [hwmon_fan_fault] = "fan%d_fault", 4578faee73fSGuenter Roeck }; 4588faee73fSGuenter Roeck 459f9f7bb3aSGuenter Roeck static const char * const hwmon_pwm_attr_templates[] = { 460f9f7bb3aSGuenter Roeck [hwmon_pwm_input] = "pwm%d", 461f9f7bb3aSGuenter Roeck [hwmon_pwm_enable] = "pwm%d_enable", 462f9f7bb3aSGuenter Roeck [hwmon_pwm_mode] = "pwm%d_mode", 463f9f7bb3aSGuenter Roeck [hwmon_pwm_freq] = "pwm%d_freq", 464f9f7bb3aSGuenter Roeck }; 465f9f7bb3aSGuenter Roeck 466d560168bSGuenter Roeck static const char * const *__templates[] = { 467f4d325d5SGuenter Roeck [hwmon_chip] = hwmon_chip_attrs, 468d560168bSGuenter Roeck [hwmon_temp] = hwmon_temp_attr_templates, 46900d616cfSGuenter Roeck [hwmon_in] = hwmon_in_attr_templates, 4709b26947cSGuenter Roeck [hwmon_curr] = hwmon_curr_attr_templates, 471b308f5c7SGuenter Roeck [hwmon_power] = hwmon_power_attr_templates, 4726bfcca44SGuenter Roeck [hwmon_energy] = hwmon_energy_attr_templates, 4736bfcca44SGuenter Roeck [hwmon_humidity] = hwmon_humidity_attr_templates, 4748faee73fSGuenter Roeck [hwmon_fan] = hwmon_fan_attr_templates, 475f9f7bb3aSGuenter Roeck [hwmon_pwm] = hwmon_pwm_attr_templates, 476d560168bSGuenter Roeck }; 477d560168bSGuenter Roeck 478d560168bSGuenter Roeck static const int __templates_size[] = { 479f4d325d5SGuenter Roeck [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs), 480d560168bSGuenter Roeck [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates), 48100d616cfSGuenter Roeck [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates), 4829b26947cSGuenter Roeck [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates), 483b308f5c7SGuenter Roeck [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates), 4846bfcca44SGuenter Roeck [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates), 4856bfcca44SGuenter Roeck [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates), 4868faee73fSGuenter Roeck [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates), 487f9f7bb3aSGuenter Roeck [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates), 488d560168bSGuenter Roeck }; 489d560168bSGuenter Roeck 490d560168bSGuenter Roeck static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info) 491d560168bSGuenter Roeck { 492d560168bSGuenter Roeck int i, n; 493d560168bSGuenter Roeck 494d560168bSGuenter Roeck for (i = n = 0; info->config[i]; i++) 495d560168bSGuenter Roeck n += hweight32(info->config[i]); 496d560168bSGuenter Roeck 497d560168bSGuenter Roeck return n; 498d560168bSGuenter Roeck } 499d560168bSGuenter Roeck 500d560168bSGuenter Roeck static int hwmon_genattrs(struct device *dev, 501d560168bSGuenter Roeck const void *drvdata, 502d560168bSGuenter Roeck struct attribute **attrs, 503d560168bSGuenter Roeck const struct hwmon_ops *ops, 504d560168bSGuenter Roeck const struct hwmon_channel_info *info) 505d560168bSGuenter Roeck { 506d560168bSGuenter Roeck const char * const *templates; 507d560168bSGuenter Roeck int template_size; 508d560168bSGuenter Roeck int i, aindex = 0; 509d560168bSGuenter Roeck 510d560168bSGuenter Roeck if (info->type >= ARRAY_SIZE(__templates)) 511d560168bSGuenter Roeck return -EINVAL; 512d560168bSGuenter Roeck 513d560168bSGuenter Roeck templates = __templates[info->type]; 514d560168bSGuenter Roeck template_size = __templates_size[info->type]; 515d560168bSGuenter Roeck 516d560168bSGuenter Roeck for (i = 0; info->config[i]; i++) { 517d560168bSGuenter Roeck u32 attr_mask = info->config[i]; 518d560168bSGuenter Roeck u32 attr; 519d560168bSGuenter Roeck 520d560168bSGuenter Roeck while (attr_mask) { 521d560168bSGuenter Roeck struct attribute *a; 522d560168bSGuenter Roeck 523d560168bSGuenter Roeck attr = __ffs(attr_mask); 524d560168bSGuenter Roeck attr_mask &= ~BIT(attr); 525d560168bSGuenter Roeck if (attr >= template_size) 526d560168bSGuenter Roeck return -EINVAL; 527d560168bSGuenter Roeck a = hwmon_genattr(dev, drvdata, info->type, attr, i, 528d560168bSGuenter Roeck templates[attr], ops); 529d560168bSGuenter Roeck if (IS_ERR(a)) { 530d560168bSGuenter Roeck if (PTR_ERR(a) != -ENOENT) 531d560168bSGuenter Roeck return PTR_ERR(a); 532d560168bSGuenter Roeck continue; 533d560168bSGuenter Roeck } 534d560168bSGuenter Roeck attrs[aindex++] = a; 535d560168bSGuenter Roeck } 536d560168bSGuenter Roeck } 537d560168bSGuenter Roeck return aindex; 538d560168bSGuenter Roeck } 539d560168bSGuenter Roeck 540d560168bSGuenter Roeck static struct attribute ** 541d560168bSGuenter Roeck __hwmon_create_attrs(struct device *dev, const void *drvdata, 542d560168bSGuenter Roeck const struct hwmon_chip_info *chip) 543d560168bSGuenter Roeck { 544d560168bSGuenter Roeck int ret, i, aindex = 0, nattrs = 0; 545d560168bSGuenter Roeck struct attribute **attrs; 546d560168bSGuenter Roeck 547d560168bSGuenter Roeck for (i = 0; chip->info[i]; i++) 548d560168bSGuenter Roeck nattrs += hwmon_num_channel_attrs(chip->info[i]); 549d560168bSGuenter Roeck 550d560168bSGuenter Roeck if (nattrs == 0) 551d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 552d560168bSGuenter Roeck 553d560168bSGuenter Roeck attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL); 554d560168bSGuenter Roeck if (!attrs) 555d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 556d560168bSGuenter Roeck 557d560168bSGuenter Roeck for (i = 0; chip->info[i]; i++) { 558d560168bSGuenter Roeck ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops, 559d560168bSGuenter Roeck chip->info[i]); 560d560168bSGuenter Roeck if (ret < 0) 561d560168bSGuenter Roeck return ERR_PTR(ret); 562d560168bSGuenter Roeck aindex += ret; 563d560168bSGuenter Roeck } 564d560168bSGuenter Roeck 565d560168bSGuenter Roeck return attrs; 566d560168bSGuenter Roeck } 567d560168bSGuenter Roeck 568d560168bSGuenter Roeck static struct device * 569d560168bSGuenter Roeck __hwmon_device_register(struct device *dev, const char *name, void *drvdata, 570d560168bSGuenter Roeck const struct hwmon_chip_info *chip, 571d560168bSGuenter Roeck const struct attribute_group **groups) 572d560168bSGuenter Roeck { 573d560168bSGuenter Roeck struct hwmon_device *hwdev; 574d560168bSGuenter Roeck struct device *hdev; 575d560168bSGuenter Roeck int i, j, err, id; 576d560168bSGuenter Roeck 57774d3b641SGuenter Roeck /* Complain about invalid characters in hwmon name attribute */ 578d560168bSGuenter Roeck if (name && (!strlen(name) || strpbrk(name, "-* \t\n"))) 57974d3b641SGuenter Roeck dev_warn(dev, 58074d3b641SGuenter Roeck "hwmon: '%s' is not a valid name attribute, please fix\n", 58174d3b641SGuenter Roeck name); 582d560168bSGuenter Roeck 583d560168bSGuenter Roeck id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL); 584d560168bSGuenter Roeck if (id < 0) 585d560168bSGuenter Roeck return ERR_PTR(id); 586d560168bSGuenter Roeck 587d560168bSGuenter Roeck hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL); 588d560168bSGuenter Roeck if (hwdev == NULL) { 589d560168bSGuenter Roeck err = -ENOMEM; 590d560168bSGuenter Roeck goto ida_remove; 591d560168bSGuenter Roeck } 592d560168bSGuenter Roeck 593d560168bSGuenter Roeck hdev = &hwdev->dev; 594d560168bSGuenter Roeck 595239552f4SGuenter Roeck if (chip) { 596d560168bSGuenter Roeck struct attribute **attrs; 597b2a4cc3aSGuenter Roeck int ngroups = 2; /* terminating NULL plus &hwdev->groups */ 598d560168bSGuenter Roeck 599d560168bSGuenter Roeck if (groups) 600d560168bSGuenter Roeck for (i = 0; groups[i]; i++) 601d560168bSGuenter Roeck ngroups++; 602d560168bSGuenter Roeck 603d560168bSGuenter Roeck hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups), 604d560168bSGuenter Roeck GFP_KERNEL); 60538d8ed65SColin Ian King if (!hwdev->groups) { 60638d8ed65SColin Ian King err = -ENOMEM; 60738d8ed65SColin Ian King goto free_hwmon; 60838d8ed65SColin Ian King } 609d560168bSGuenter Roeck 610d560168bSGuenter Roeck attrs = __hwmon_create_attrs(dev, drvdata, chip); 611d560168bSGuenter Roeck if (IS_ERR(attrs)) { 612d560168bSGuenter Roeck err = PTR_ERR(attrs); 613d560168bSGuenter Roeck goto free_hwmon; 614d560168bSGuenter Roeck } 615d560168bSGuenter Roeck 616d560168bSGuenter Roeck hwdev->group.attrs = attrs; 617d560168bSGuenter Roeck ngroups = 0; 618d560168bSGuenter Roeck hwdev->groups[ngroups++] = &hwdev->group; 619d560168bSGuenter Roeck 620d560168bSGuenter Roeck if (groups) { 621d560168bSGuenter Roeck for (i = 0; groups[i]; i++) 622d560168bSGuenter Roeck hwdev->groups[ngroups++] = groups[i]; 623d560168bSGuenter Roeck } 624d560168bSGuenter Roeck 625d560168bSGuenter Roeck hdev->groups = hwdev->groups; 626d560168bSGuenter Roeck } else { 627d560168bSGuenter Roeck hdev->groups = groups; 628d560168bSGuenter Roeck } 629d560168bSGuenter Roeck 630d560168bSGuenter Roeck hwdev->name = name; 631d560168bSGuenter Roeck hdev->class = &hwmon_class; 632d560168bSGuenter Roeck hdev->parent = dev; 633d560168bSGuenter Roeck hdev->of_node = dev ? dev->of_node : NULL; 634d560168bSGuenter Roeck hwdev->chip = chip; 635d560168bSGuenter Roeck dev_set_drvdata(hdev, drvdata); 636d560168bSGuenter Roeck dev_set_name(hdev, HWMON_ID_FORMAT, id); 637d560168bSGuenter Roeck err = device_register(hdev); 638d560168bSGuenter Roeck if (err) 639d560168bSGuenter Roeck goto free_hwmon; 640d560168bSGuenter Roeck 641319fe159SGuenter Roeck if (dev && chip && chip->ops->read && 642d560168bSGuenter Roeck chip->info[0]->type == hwmon_chip && 643d560168bSGuenter Roeck (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) { 644d560168bSGuenter Roeck const struct hwmon_channel_info **info = chip->info; 645d560168bSGuenter Roeck 646d560168bSGuenter Roeck for (i = 1; info[i]; i++) { 647d560168bSGuenter Roeck if (info[i]->type != hwmon_temp) 648d560168bSGuenter Roeck continue; 649d560168bSGuenter Roeck 650d560168bSGuenter Roeck for (j = 0; info[i]->config[j]; j++) { 651d560168bSGuenter Roeck if (!chip->ops->is_visible(drvdata, hwmon_temp, 652d560168bSGuenter Roeck hwmon_temp_input, j)) 653d560168bSGuenter Roeck continue; 65447c332deSLinus Walleij if (info[i]->config[j] & HWMON_T_INPUT) { 65547c332deSLinus Walleij err = hwmon_thermal_add_sensor(dev, 65647c332deSLinus Walleij hwdev, j); 65774e35127SDmitry Osipenko if (err) { 65874e35127SDmitry Osipenko device_unregister(hdev); 65974e35127SDmitry Osipenko goto ida_remove; 66074e35127SDmitry Osipenko } 66147c332deSLinus Walleij } 662d560168bSGuenter Roeck } 663d560168bSGuenter Roeck } 664d560168bSGuenter Roeck } 665d560168bSGuenter Roeck 666d560168bSGuenter Roeck return hdev; 667d560168bSGuenter Roeck 668d560168bSGuenter Roeck free_hwmon: 669d560168bSGuenter Roeck kfree(hwdev); 670d560168bSGuenter Roeck ida_remove: 671d560168bSGuenter Roeck ida_simple_remove(&hwmon_ida, id); 672d560168bSGuenter Roeck return ERR_PTR(err); 673d560168bSGuenter Roeck } 674d560168bSGuenter Roeck 6751236441fSMark M. Hoffman /** 676bab2243cSGuenter Roeck * hwmon_device_register_with_groups - register w/ hwmon 677bab2243cSGuenter Roeck * @dev: the parent device 678bab2243cSGuenter Roeck * @name: hwmon name attribute 679bab2243cSGuenter Roeck * @drvdata: driver data to attach to created device 680bab2243cSGuenter Roeck * @groups: List of attribute groups to create 681bab2243cSGuenter Roeck * 682bab2243cSGuenter Roeck * hwmon_device_unregister() must be called when the device is no 683bab2243cSGuenter Roeck * longer needed. 684bab2243cSGuenter Roeck * 685bab2243cSGuenter Roeck * Returns the pointer to the new device. 686bab2243cSGuenter Roeck */ 687bab2243cSGuenter Roeck struct device * 688bab2243cSGuenter Roeck hwmon_device_register_with_groups(struct device *dev, const char *name, 689bab2243cSGuenter Roeck void *drvdata, 690bab2243cSGuenter Roeck const struct attribute_group **groups) 691bab2243cSGuenter Roeck { 6928353863aSGuenter Roeck if (!name) 6938353863aSGuenter Roeck return ERR_PTR(-EINVAL); 6948353863aSGuenter Roeck 695d560168bSGuenter Roeck return __hwmon_device_register(dev, name, drvdata, NULL, groups); 696bab2243cSGuenter Roeck } 697bab2243cSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups); 698bab2243cSGuenter Roeck 699bab2243cSGuenter Roeck /** 700d560168bSGuenter Roeck * hwmon_device_register_with_info - register w/ hwmon 701d560168bSGuenter Roeck * @dev: the parent device 702d560168bSGuenter Roeck * @name: hwmon name attribute 703d560168bSGuenter Roeck * @drvdata: driver data to attach to created device 7043870945aSGuenter Roeck * @chip: pointer to hwmon chip information 705848ba0a2SGuenter Roeck * @extra_groups: pointer to list of additional non-standard attribute groups 706d560168bSGuenter Roeck * 707d560168bSGuenter Roeck * hwmon_device_unregister() must be called when the device is no 708d560168bSGuenter Roeck * longer needed. 709d560168bSGuenter Roeck * 710d560168bSGuenter Roeck * Returns the pointer to the new device. 711d560168bSGuenter Roeck */ 712d560168bSGuenter Roeck struct device * 713d560168bSGuenter Roeck hwmon_device_register_with_info(struct device *dev, const char *name, 714d560168bSGuenter Roeck void *drvdata, 715d560168bSGuenter Roeck const struct hwmon_chip_info *chip, 716848ba0a2SGuenter Roeck const struct attribute_group **extra_groups) 717d560168bSGuenter Roeck { 7188353863aSGuenter Roeck if (!name) 7198353863aSGuenter Roeck return ERR_PTR(-EINVAL); 7208353863aSGuenter Roeck 721239552f4SGuenter Roeck if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info)) 722d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 723d560168bSGuenter Roeck 72459df4f4eSLucas Magasweran if (chip && !dev) 72559df4f4eSLucas Magasweran return ERR_PTR(-EINVAL); 72659df4f4eSLucas Magasweran 727848ba0a2SGuenter Roeck return __hwmon_device_register(dev, name, drvdata, chip, extra_groups); 728d560168bSGuenter Roeck } 729d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_info); 730d560168bSGuenter Roeck 731d560168bSGuenter Roeck /** 7321beeffe4STony Jones * hwmon_device_register - register w/ hwmon 7331236441fSMark M. Hoffman * @dev: the device to register 7341236441fSMark M. Hoffman * 7351beeffe4STony Jones * hwmon_device_unregister() must be called when the device is no 7361236441fSMark M. Hoffman * longer needed. 7371236441fSMark M. Hoffman * 7381beeffe4STony Jones * Returns the pointer to the new device. 7391236441fSMark M. Hoffman */ 7401beeffe4STony Jones struct device *hwmon_device_register(struct device *dev) 7411236441fSMark M. Hoffman { 742af1bd36cSGuenter Roeck dev_warn(dev, 743af1bd36cSGuenter Roeck "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n"); 744af1bd36cSGuenter Roeck 7458353863aSGuenter Roeck return __hwmon_device_register(dev, NULL, NULL, NULL, NULL); 7461236441fSMark M. Hoffman } 747839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_register); 7481236441fSMark M. Hoffman 7491236441fSMark M. Hoffman /** 7501236441fSMark M. Hoffman * hwmon_device_unregister - removes the previously registered class device 7511236441fSMark M. Hoffman * 7521beeffe4STony Jones * @dev: the class device to destroy 7531236441fSMark M. Hoffman */ 7541beeffe4STony Jones void hwmon_device_unregister(struct device *dev) 7551236441fSMark M. Hoffman { 7561236441fSMark M. Hoffman int id; 7571236441fSMark M. Hoffman 758739cf3a2SKay Sievers if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) { 7591beeffe4STony Jones device_unregister(dev); 7604ca5f468SJonathan Cameron ida_simple_remove(&hwmon_ida, id); 7611236441fSMark M. Hoffman } else 7621beeffe4STony Jones dev_dbg(dev->parent, 7631236441fSMark M. Hoffman "hwmon_device_unregister() failed: bad class ID!\n"); 7641236441fSMark M. Hoffman } 765839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_unregister); 7661236441fSMark M. Hoffman 76774188cbaSGuenter Roeck static void devm_hwmon_release(struct device *dev, void *res) 76874188cbaSGuenter Roeck { 76974188cbaSGuenter Roeck struct device *hwdev = *(struct device **)res; 77074188cbaSGuenter Roeck 77174188cbaSGuenter Roeck hwmon_device_unregister(hwdev); 77274188cbaSGuenter Roeck } 77374188cbaSGuenter Roeck 77474188cbaSGuenter Roeck /** 77574188cbaSGuenter Roeck * devm_hwmon_device_register_with_groups - register w/ hwmon 77674188cbaSGuenter Roeck * @dev: the parent device 77774188cbaSGuenter Roeck * @name: hwmon name attribute 77874188cbaSGuenter Roeck * @drvdata: driver data to attach to created device 77974188cbaSGuenter Roeck * @groups: List of attribute groups to create 78074188cbaSGuenter Roeck * 78174188cbaSGuenter Roeck * Returns the pointer to the new device. The new device is automatically 78274188cbaSGuenter Roeck * unregistered with the parent device. 78374188cbaSGuenter Roeck */ 78474188cbaSGuenter Roeck struct device * 78574188cbaSGuenter Roeck devm_hwmon_device_register_with_groups(struct device *dev, const char *name, 78674188cbaSGuenter Roeck void *drvdata, 78774188cbaSGuenter Roeck const struct attribute_group **groups) 78874188cbaSGuenter Roeck { 78974188cbaSGuenter Roeck struct device **ptr, *hwdev; 79074188cbaSGuenter Roeck 79174188cbaSGuenter Roeck if (!dev) 79274188cbaSGuenter Roeck return ERR_PTR(-EINVAL); 79374188cbaSGuenter Roeck 79474188cbaSGuenter Roeck ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); 79574188cbaSGuenter Roeck if (!ptr) 79674188cbaSGuenter Roeck return ERR_PTR(-ENOMEM); 79774188cbaSGuenter Roeck 79874188cbaSGuenter Roeck hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups); 79974188cbaSGuenter Roeck if (IS_ERR(hwdev)) 80074188cbaSGuenter Roeck goto error; 80174188cbaSGuenter Roeck 80274188cbaSGuenter Roeck *ptr = hwdev; 80374188cbaSGuenter Roeck devres_add(dev, ptr); 80474188cbaSGuenter Roeck return hwdev; 80574188cbaSGuenter Roeck 80674188cbaSGuenter Roeck error: 80774188cbaSGuenter Roeck devres_free(ptr); 80874188cbaSGuenter Roeck return hwdev; 80974188cbaSGuenter Roeck } 81074188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups); 81174188cbaSGuenter Roeck 812d560168bSGuenter Roeck /** 813d560168bSGuenter Roeck * devm_hwmon_device_register_with_info - register w/ hwmon 814d560168bSGuenter Roeck * @dev: the parent device 815d560168bSGuenter Roeck * @name: hwmon name attribute 816d560168bSGuenter Roeck * @drvdata: driver data to attach to created device 8173870945aSGuenter Roeck * @chip: pointer to hwmon chip information 8183870945aSGuenter Roeck * @groups: pointer to list of driver specific attribute groups 819d560168bSGuenter Roeck * 820d560168bSGuenter Roeck * Returns the pointer to the new device. The new device is automatically 821d560168bSGuenter Roeck * unregistered with the parent device. 822d560168bSGuenter Roeck */ 823d560168bSGuenter Roeck struct device * 824d560168bSGuenter Roeck devm_hwmon_device_register_with_info(struct device *dev, const char *name, 825d560168bSGuenter Roeck void *drvdata, 826d560168bSGuenter Roeck const struct hwmon_chip_info *chip, 827d560168bSGuenter Roeck const struct attribute_group **groups) 828d560168bSGuenter Roeck { 829d560168bSGuenter Roeck struct device **ptr, *hwdev; 830d560168bSGuenter Roeck 831d560168bSGuenter Roeck if (!dev) 832d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 833d560168bSGuenter Roeck 834d560168bSGuenter Roeck ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); 835d560168bSGuenter Roeck if (!ptr) 836d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 837d560168bSGuenter Roeck 838d560168bSGuenter Roeck hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip, 839d560168bSGuenter Roeck groups); 840d560168bSGuenter Roeck if (IS_ERR(hwdev)) 841d560168bSGuenter Roeck goto error; 842d560168bSGuenter Roeck 843d560168bSGuenter Roeck *ptr = hwdev; 844d560168bSGuenter Roeck devres_add(dev, ptr); 845d560168bSGuenter Roeck 846d560168bSGuenter Roeck return hwdev; 847d560168bSGuenter Roeck 848d560168bSGuenter Roeck error: 849d560168bSGuenter Roeck devres_free(ptr); 850d560168bSGuenter Roeck return hwdev; 851d560168bSGuenter Roeck } 852d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info); 853d560168bSGuenter Roeck 85474188cbaSGuenter Roeck static int devm_hwmon_match(struct device *dev, void *res, void *data) 85574188cbaSGuenter Roeck { 85674188cbaSGuenter Roeck struct device **hwdev = res; 85774188cbaSGuenter Roeck 85874188cbaSGuenter Roeck return *hwdev == data; 85974188cbaSGuenter Roeck } 86074188cbaSGuenter Roeck 86174188cbaSGuenter Roeck /** 86274188cbaSGuenter Roeck * devm_hwmon_device_unregister - removes a previously registered hwmon device 86374188cbaSGuenter Roeck * 86474188cbaSGuenter Roeck * @dev: the parent device of the device to unregister 86574188cbaSGuenter Roeck */ 86674188cbaSGuenter Roeck void devm_hwmon_device_unregister(struct device *dev) 86774188cbaSGuenter Roeck { 86874188cbaSGuenter Roeck WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev)); 86974188cbaSGuenter Roeck } 87074188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister); 87174188cbaSGuenter Roeck 8722958b1ecSJean Delvare static void __init hwmon_pci_quirks(void) 8732958b1ecSJean Delvare { 8742958b1ecSJean Delvare #if defined CONFIG_X86 && defined CONFIG_PCI 8752958b1ecSJean Delvare struct pci_dev *sb; 8762958b1ecSJean Delvare u16 base; 8772958b1ecSJean Delvare u8 enable; 8782958b1ecSJean Delvare 8792958b1ecSJean Delvare /* Open access to 0x295-0x296 on MSI MS-7031 */ 8802958b1ecSJean Delvare sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL); 881d6dab7ddSJean Delvare if (sb) { 882d6dab7ddSJean Delvare if (sb->subsystem_vendor == 0x1462 && /* MSI */ 883d6dab7ddSJean Delvare sb->subsystem_device == 0x0031) { /* MS-7031 */ 8842958b1ecSJean Delvare pci_read_config_byte(sb, 0x48, &enable); 8852958b1ecSJean Delvare pci_read_config_word(sb, 0x64, &base); 8862958b1ecSJean Delvare 8872958b1ecSJean Delvare if (base == 0 && !(enable & BIT(2))) { 8882958b1ecSJean Delvare dev_info(&sb->dev, 8892958b1ecSJean Delvare "Opening wide generic port at 0x295\n"); 8902958b1ecSJean Delvare pci_write_config_word(sb, 0x64, 0x295); 891d6dab7ddSJean Delvare pci_write_config_byte(sb, 0x48, 892d6dab7ddSJean Delvare enable | BIT(2)); 8932958b1ecSJean Delvare } 8942958b1ecSJean Delvare } 895d6dab7ddSJean Delvare pci_dev_put(sb); 896d6dab7ddSJean Delvare } 8972958b1ecSJean Delvare #endif 8982958b1ecSJean Delvare } 8992958b1ecSJean Delvare 9001236441fSMark M. Hoffman static int __init hwmon_init(void) 9011236441fSMark M. Hoffman { 902bab2243cSGuenter Roeck int err; 903bab2243cSGuenter Roeck 9042958b1ecSJean Delvare hwmon_pci_quirks(); 9052958b1ecSJean Delvare 906bab2243cSGuenter Roeck err = class_register(&hwmon_class); 907bab2243cSGuenter Roeck if (err) { 908bab2243cSGuenter Roeck pr_err("couldn't register hwmon sysfs class\n"); 909bab2243cSGuenter Roeck return err; 9101236441fSMark M. Hoffman } 9111236441fSMark M. Hoffman return 0; 9121236441fSMark M. Hoffman } 9131236441fSMark M. Hoffman 9141236441fSMark M. Hoffman static void __exit hwmon_exit(void) 9151236441fSMark M. Hoffman { 916bab2243cSGuenter Roeck class_unregister(&hwmon_class); 9171236441fSMark M. Hoffman } 9181236441fSMark M. Hoffman 91937f54ee5SDavid Brownell subsys_initcall(hwmon_init); 9201236441fSMark M. Hoffman module_exit(hwmon_exit); 9211236441fSMark M. Hoffman 9221236441fSMark M. Hoffman MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>"); 9231236441fSMark M. Hoffman MODULE_DESCRIPTION("hardware monitoring sysfs/class support"); 9241236441fSMark M. Hoffman MODULE_LICENSE("GPL"); 9251236441fSMark M. Hoffman 926