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; 270*3b443defSRasmus 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 281e159ab5cSGuenter Roeck if ((mode & S_IRUGO) && ((is_string && !ops->read_string) || 282e159ab5cSGuenter Roeck (!is_string && !ops->read))) 283d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 284d560168bSGuenter Roeck if ((mode & S_IWUGO) && !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) { 292*3b443defSRasmus 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", 327d560168bSGuenter Roeck }; 328d560168bSGuenter Roeck 329d560168bSGuenter Roeck static const char * const hwmon_temp_attr_templates[] = { 330d560168bSGuenter Roeck [hwmon_temp_input] = "temp%d_input", 331d560168bSGuenter Roeck [hwmon_temp_type] = "temp%d_type", 332d560168bSGuenter Roeck [hwmon_temp_lcrit] = "temp%d_lcrit", 333d560168bSGuenter Roeck [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst", 334d560168bSGuenter Roeck [hwmon_temp_min] = "temp%d_min", 335d560168bSGuenter Roeck [hwmon_temp_min_hyst] = "temp%d_min_hyst", 336d560168bSGuenter Roeck [hwmon_temp_max] = "temp%d_max", 337d560168bSGuenter Roeck [hwmon_temp_max_hyst] = "temp%d_max_hyst", 338d560168bSGuenter Roeck [hwmon_temp_crit] = "temp%d_crit", 339d560168bSGuenter Roeck [hwmon_temp_crit_hyst] = "temp%d_crit_hyst", 340d560168bSGuenter Roeck [hwmon_temp_emergency] = "temp%d_emergency", 341d560168bSGuenter Roeck [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst", 342d560168bSGuenter Roeck [hwmon_temp_alarm] = "temp%d_alarm", 343d560168bSGuenter Roeck [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm", 344d560168bSGuenter Roeck [hwmon_temp_min_alarm] = "temp%d_min_alarm", 345d560168bSGuenter Roeck [hwmon_temp_max_alarm] = "temp%d_max_alarm", 346d560168bSGuenter Roeck [hwmon_temp_crit_alarm] = "temp%d_crit_alarm", 347d560168bSGuenter Roeck [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm", 348d560168bSGuenter Roeck [hwmon_temp_fault] = "temp%d_fault", 349d560168bSGuenter Roeck [hwmon_temp_offset] = "temp%d_offset", 350d560168bSGuenter Roeck [hwmon_temp_label] = "temp%d_label", 351d560168bSGuenter Roeck [hwmon_temp_lowest] = "temp%d_lowest", 352d560168bSGuenter Roeck [hwmon_temp_highest] = "temp%d_highest", 353d560168bSGuenter Roeck [hwmon_temp_reset_history] = "temp%d_reset_history", 354d560168bSGuenter Roeck }; 355d560168bSGuenter Roeck 35600d616cfSGuenter Roeck static const char * const hwmon_in_attr_templates[] = { 35700d616cfSGuenter Roeck [hwmon_in_input] = "in%d_input", 35800d616cfSGuenter Roeck [hwmon_in_min] = "in%d_min", 35900d616cfSGuenter Roeck [hwmon_in_max] = "in%d_max", 36000d616cfSGuenter Roeck [hwmon_in_lcrit] = "in%d_lcrit", 36100d616cfSGuenter Roeck [hwmon_in_crit] = "in%d_crit", 36200d616cfSGuenter Roeck [hwmon_in_average] = "in%d_average", 36300d616cfSGuenter Roeck [hwmon_in_lowest] = "in%d_lowest", 36400d616cfSGuenter Roeck [hwmon_in_highest] = "in%d_highest", 36500d616cfSGuenter Roeck [hwmon_in_reset_history] = "in%d_reset_history", 36600d616cfSGuenter Roeck [hwmon_in_label] = "in%d_label", 36700d616cfSGuenter Roeck [hwmon_in_alarm] = "in%d_alarm", 36800d616cfSGuenter Roeck [hwmon_in_min_alarm] = "in%d_min_alarm", 36900d616cfSGuenter Roeck [hwmon_in_max_alarm] = "in%d_max_alarm", 37000d616cfSGuenter Roeck [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm", 37100d616cfSGuenter Roeck [hwmon_in_crit_alarm] = "in%d_crit_alarm", 37268c0d69dSNicolin Chen [hwmon_in_enable] = "in%d_enable", 37300d616cfSGuenter Roeck }; 37400d616cfSGuenter Roeck 3759b26947cSGuenter Roeck static const char * const hwmon_curr_attr_templates[] = { 3769b26947cSGuenter Roeck [hwmon_curr_input] = "curr%d_input", 3779b26947cSGuenter Roeck [hwmon_curr_min] = "curr%d_min", 3789b26947cSGuenter Roeck [hwmon_curr_max] = "curr%d_max", 3799b26947cSGuenter Roeck [hwmon_curr_lcrit] = "curr%d_lcrit", 3809b26947cSGuenter Roeck [hwmon_curr_crit] = "curr%d_crit", 3819b26947cSGuenter Roeck [hwmon_curr_average] = "curr%d_average", 3829b26947cSGuenter Roeck [hwmon_curr_lowest] = "curr%d_lowest", 3839b26947cSGuenter Roeck [hwmon_curr_highest] = "curr%d_highest", 3849b26947cSGuenter Roeck [hwmon_curr_reset_history] = "curr%d_reset_history", 3859b26947cSGuenter Roeck [hwmon_curr_label] = "curr%d_label", 3869b26947cSGuenter Roeck [hwmon_curr_alarm] = "curr%d_alarm", 3879b26947cSGuenter Roeck [hwmon_curr_min_alarm] = "curr%d_min_alarm", 3889b26947cSGuenter Roeck [hwmon_curr_max_alarm] = "curr%d_max_alarm", 3899b26947cSGuenter Roeck [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm", 3909b26947cSGuenter Roeck [hwmon_curr_crit_alarm] = "curr%d_crit_alarm", 3919b26947cSGuenter Roeck }; 3929b26947cSGuenter Roeck 393b308f5c7SGuenter Roeck static const char * const hwmon_power_attr_templates[] = { 394b308f5c7SGuenter Roeck [hwmon_power_average] = "power%d_average", 395b308f5c7SGuenter Roeck [hwmon_power_average_interval] = "power%d_average_interval", 396b308f5c7SGuenter Roeck [hwmon_power_average_interval_max] = "power%d_interval_max", 397b308f5c7SGuenter Roeck [hwmon_power_average_interval_min] = "power%d_interval_min", 398b308f5c7SGuenter Roeck [hwmon_power_average_highest] = "power%d_average_highest", 399b308f5c7SGuenter Roeck [hwmon_power_average_lowest] = "power%d_average_lowest", 400b308f5c7SGuenter Roeck [hwmon_power_average_max] = "power%d_average_max", 401b308f5c7SGuenter Roeck [hwmon_power_average_min] = "power%d_average_min", 402b308f5c7SGuenter Roeck [hwmon_power_input] = "power%d_input", 403b308f5c7SGuenter Roeck [hwmon_power_input_highest] = "power%d_input_highest", 404b308f5c7SGuenter Roeck [hwmon_power_input_lowest] = "power%d_input_lowest", 405b308f5c7SGuenter Roeck [hwmon_power_reset_history] = "power%d_reset_history", 406b308f5c7SGuenter Roeck [hwmon_power_accuracy] = "power%d_accuracy", 407b308f5c7SGuenter Roeck [hwmon_power_cap] = "power%d_cap", 408b308f5c7SGuenter Roeck [hwmon_power_cap_hyst] = "power%d_cap_hyst", 409b308f5c7SGuenter Roeck [hwmon_power_cap_max] = "power%d_cap_max", 410b308f5c7SGuenter Roeck [hwmon_power_cap_min] = "power%d_cap_min", 411aa7f29b0SAndrew Lunn [hwmon_power_min] = "power%d_min", 412b308f5c7SGuenter Roeck [hwmon_power_max] = "power%d_max", 413aa7f29b0SAndrew Lunn [hwmon_power_lcrit] = "power%d_lcrit", 414b308f5c7SGuenter Roeck [hwmon_power_crit] = "power%d_crit", 415b308f5c7SGuenter Roeck [hwmon_power_label] = "power%d_label", 416b308f5c7SGuenter Roeck [hwmon_power_alarm] = "power%d_alarm", 417b308f5c7SGuenter Roeck [hwmon_power_cap_alarm] = "power%d_cap_alarm", 418aa7f29b0SAndrew Lunn [hwmon_power_min_alarm] = "power%d_min_alarm", 419b308f5c7SGuenter Roeck [hwmon_power_max_alarm] = "power%d_max_alarm", 420aa7f29b0SAndrew Lunn [hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm", 421b308f5c7SGuenter Roeck [hwmon_power_crit_alarm] = "power%d_crit_alarm", 422b308f5c7SGuenter Roeck }; 423b308f5c7SGuenter Roeck 4246bfcca44SGuenter Roeck static const char * const hwmon_energy_attr_templates[] = { 4256bfcca44SGuenter Roeck [hwmon_energy_input] = "energy%d_input", 4266bfcca44SGuenter Roeck [hwmon_energy_label] = "energy%d_label", 4276bfcca44SGuenter Roeck }; 4286bfcca44SGuenter Roeck 4296bfcca44SGuenter Roeck static const char * const hwmon_humidity_attr_templates[] = { 4306bfcca44SGuenter Roeck [hwmon_humidity_input] = "humidity%d_input", 4316bfcca44SGuenter Roeck [hwmon_humidity_label] = "humidity%d_label", 4326bfcca44SGuenter Roeck [hwmon_humidity_min] = "humidity%d_min", 4336bfcca44SGuenter Roeck [hwmon_humidity_min_hyst] = "humidity%d_min_hyst", 4346bfcca44SGuenter Roeck [hwmon_humidity_max] = "humidity%d_max", 4356bfcca44SGuenter Roeck [hwmon_humidity_max_hyst] = "humidity%d_max_hyst", 4366bfcca44SGuenter Roeck [hwmon_humidity_alarm] = "humidity%d_alarm", 4376bfcca44SGuenter Roeck [hwmon_humidity_fault] = "humidity%d_fault", 4386bfcca44SGuenter Roeck }; 4396bfcca44SGuenter Roeck 4408faee73fSGuenter Roeck static const char * const hwmon_fan_attr_templates[] = { 4418faee73fSGuenter Roeck [hwmon_fan_input] = "fan%d_input", 4428faee73fSGuenter Roeck [hwmon_fan_label] = "fan%d_label", 4438faee73fSGuenter Roeck [hwmon_fan_min] = "fan%d_min", 4448faee73fSGuenter Roeck [hwmon_fan_max] = "fan%d_max", 4458faee73fSGuenter Roeck [hwmon_fan_div] = "fan%d_div", 4468faee73fSGuenter Roeck [hwmon_fan_pulses] = "fan%d_pulses", 4478faee73fSGuenter Roeck [hwmon_fan_target] = "fan%d_target", 4488faee73fSGuenter Roeck [hwmon_fan_alarm] = "fan%d_alarm", 4498faee73fSGuenter Roeck [hwmon_fan_min_alarm] = "fan%d_min_alarm", 4508faee73fSGuenter Roeck [hwmon_fan_max_alarm] = "fan%d_max_alarm", 4518faee73fSGuenter Roeck [hwmon_fan_fault] = "fan%d_fault", 4528faee73fSGuenter Roeck }; 4538faee73fSGuenter Roeck 454f9f7bb3aSGuenter Roeck static const char * const hwmon_pwm_attr_templates[] = { 455f9f7bb3aSGuenter Roeck [hwmon_pwm_input] = "pwm%d", 456f9f7bb3aSGuenter Roeck [hwmon_pwm_enable] = "pwm%d_enable", 457f9f7bb3aSGuenter Roeck [hwmon_pwm_mode] = "pwm%d_mode", 458f9f7bb3aSGuenter Roeck [hwmon_pwm_freq] = "pwm%d_freq", 459f9f7bb3aSGuenter Roeck }; 460f9f7bb3aSGuenter Roeck 461d560168bSGuenter Roeck static const char * const *__templates[] = { 462f4d325d5SGuenter Roeck [hwmon_chip] = hwmon_chip_attrs, 463d560168bSGuenter Roeck [hwmon_temp] = hwmon_temp_attr_templates, 46400d616cfSGuenter Roeck [hwmon_in] = hwmon_in_attr_templates, 4659b26947cSGuenter Roeck [hwmon_curr] = hwmon_curr_attr_templates, 466b308f5c7SGuenter Roeck [hwmon_power] = hwmon_power_attr_templates, 4676bfcca44SGuenter Roeck [hwmon_energy] = hwmon_energy_attr_templates, 4686bfcca44SGuenter Roeck [hwmon_humidity] = hwmon_humidity_attr_templates, 4698faee73fSGuenter Roeck [hwmon_fan] = hwmon_fan_attr_templates, 470f9f7bb3aSGuenter Roeck [hwmon_pwm] = hwmon_pwm_attr_templates, 471d560168bSGuenter Roeck }; 472d560168bSGuenter Roeck 473d560168bSGuenter Roeck static const int __templates_size[] = { 474f4d325d5SGuenter Roeck [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs), 475d560168bSGuenter Roeck [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates), 47600d616cfSGuenter Roeck [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates), 4779b26947cSGuenter Roeck [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates), 478b308f5c7SGuenter Roeck [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates), 4796bfcca44SGuenter Roeck [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates), 4806bfcca44SGuenter Roeck [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates), 4818faee73fSGuenter Roeck [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates), 482f9f7bb3aSGuenter Roeck [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates), 483d560168bSGuenter Roeck }; 484d560168bSGuenter Roeck 485d560168bSGuenter Roeck static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info) 486d560168bSGuenter Roeck { 487d560168bSGuenter Roeck int i, n; 488d560168bSGuenter Roeck 489d560168bSGuenter Roeck for (i = n = 0; info->config[i]; i++) 490d560168bSGuenter Roeck n += hweight32(info->config[i]); 491d560168bSGuenter Roeck 492d560168bSGuenter Roeck return n; 493d560168bSGuenter Roeck } 494d560168bSGuenter Roeck 495d560168bSGuenter Roeck static int hwmon_genattrs(struct device *dev, 496d560168bSGuenter Roeck const void *drvdata, 497d560168bSGuenter Roeck struct attribute **attrs, 498d560168bSGuenter Roeck const struct hwmon_ops *ops, 499d560168bSGuenter Roeck const struct hwmon_channel_info *info) 500d560168bSGuenter Roeck { 501d560168bSGuenter Roeck const char * const *templates; 502d560168bSGuenter Roeck int template_size; 503d560168bSGuenter Roeck int i, aindex = 0; 504d560168bSGuenter Roeck 505d560168bSGuenter Roeck if (info->type >= ARRAY_SIZE(__templates)) 506d560168bSGuenter Roeck return -EINVAL; 507d560168bSGuenter Roeck 508d560168bSGuenter Roeck templates = __templates[info->type]; 509d560168bSGuenter Roeck template_size = __templates_size[info->type]; 510d560168bSGuenter Roeck 511d560168bSGuenter Roeck for (i = 0; info->config[i]; i++) { 512d560168bSGuenter Roeck u32 attr_mask = info->config[i]; 513d560168bSGuenter Roeck u32 attr; 514d560168bSGuenter Roeck 515d560168bSGuenter Roeck while (attr_mask) { 516d560168bSGuenter Roeck struct attribute *a; 517d560168bSGuenter Roeck 518d560168bSGuenter Roeck attr = __ffs(attr_mask); 519d560168bSGuenter Roeck attr_mask &= ~BIT(attr); 520d560168bSGuenter Roeck if (attr >= template_size) 521d560168bSGuenter Roeck return -EINVAL; 522d560168bSGuenter Roeck a = hwmon_genattr(dev, drvdata, info->type, attr, i, 523d560168bSGuenter Roeck templates[attr], ops); 524d560168bSGuenter Roeck if (IS_ERR(a)) { 525d560168bSGuenter Roeck if (PTR_ERR(a) != -ENOENT) 526d560168bSGuenter Roeck return PTR_ERR(a); 527d560168bSGuenter Roeck continue; 528d560168bSGuenter Roeck } 529d560168bSGuenter Roeck attrs[aindex++] = a; 530d560168bSGuenter Roeck } 531d560168bSGuenter Roeck } 532d560168bSGuenter Roeck return aindex; 533d560168bSGuenter Roeck } 534d560168bSGuenter Roeck 535d560168bSGuenter Roeck static struct attribute ** 536d560168bSGuenter Roeck __hwmon_create_attrs(struct device *dev, const void *drvdata, 537d560168bSGuenter Roeck const struct hwmon_chip_info *chip) 538d560168bSGuenter Roeck { 539d560168bSGuenter Roeck int ret, i, aindex = 0, nattrs = 0; 540d560168bSGuenter Roeck struct attribute **attrs; 541d560168bSGuenter Roeck 542d560168bSGuenter Roeck for (i = 0; chip->info[i]; i++) 543d560168bSGuenter Roeck nattrs += hwmon_num_channel_attrs(chip->info[i]); 544d560168bSGuenter Roeck 545d560168bSGuenter Roeck if (nattrs == 0) 546d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 547d560168bSGuenter Roeck 548d560168bSGuenter Roeck attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL); 549d560168bSGuenter Roeck if (!attrs) 550d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 551d560168bSGuenter Roeck 552d560168bSGuenter Roeck for (i = 0; chip->info[i]; i++) { 553d560168bSGuenter Roeck ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops, 554d560168bSGuenter Roeck chip->info[i]); 555d560168bSGuenter Roeck if (ret < 0) 556d560168bSGuenter Roeck return ERR_PTR(ret); 557d560168bSGuenter Roeck aindex += ret; 558d560168bSGuenter Roeck } 559d560168bSGuenter Roeck 560d560168bSGuenter Roeck return attrs; 561d560168bSGuenter Roeck } 562d560168bSGuenter Roeck 563d560168bSGuenter Roeck static struct device * 564d560168bSGuenter Roeck __hwmon_device_register(struct device *dev, const char *name, void *drvdata, 565d560168bSGuenter Roeck const struct hwmon_chip_info *chip, 566d560168bSGuenter Roeck const struct attribute_group **groups) 567d560168bSGuenter Roeck { 568d560168bSGuenter Roeck struct hwmon_device *hwdev; 569d560168bSGuenter Roeck struct device *hdev; 570d560168bSGuenter Roeck int i, j, err, id; 571d560168bSGuenter Roeck 57274d3b641SGuenter Roeck /* Complain about invalid characters in hwmon name attribute */ 573d560168bSGuenter Roeck if (name && (!strlen(name) || strpbrk(name, "-* \t\n"))) 57474d3b641SGuenter Roeck dev_warn(dev, 57574d3b641SGuenter Roeck "hwmon: '%s' is not a valid name attribute, please fix\n", 57674d3b641SGuenter Roeck name); 577d560168bSGuenter Roeck 578d560168bSGuenter Roeck id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL); 579d560168bSGuenter Roeck if (id < 0) 580d560168bSGuenter Roeck return ERR_PTR(id); 581d560168bSGuenter Roeck 582d560168bSGuenter Roeck hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL); 583d560168bSGuenter Roeck if (hwdev == NULL) { 584d560168bSGuenter Roeck err = -ENOMEM; 585d560168bSGuenter Roeck goto ida_remove; 586d560168bSGuenter Roeck } 587d560168bSGuenter Roeck 588d560168bSGuenter Roeck hdev = &hwdev->dev; 589d560168bSGuenter Roeck 590239552f4SGuenter Roeck if (chip) { 591d560168bSGuenter Roeck struct attribute **attrs; 592b2a4cc3aSGuenter Roeck int ngroups = 2; /* terminating NULL plus &hwdev->groups */ 593d560168bSGuenter Roeck 594d560168bSGuenter Roeck if (groups) 595d560168bSGuenter Roeck for (i = 0; groups[i]; i++) 596d560168bSGuenter Roeck ngroups++; 597d560168bSGuenter Roeck 598d560168bSGuenter Roeck hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups), 599d560168bSGuenter Roeck GFP_KERNEL); 60038d8ed65SColin Ian King if (!hwdev->groups) { 60138d8ed65SColin Ian King err = -ENOMEM; 60238d8ed65SColin Ian King goto free_hwmon; 60338d8ed65SColin Ian King } 604d560168bSGuenter Roeck 605d560168bSGuenter Roeck attrs = __hwmon_create_attrs(dev, drvdata, chip); 606d560168bSGuenter Roeck if (IS_ERR(attrs)) { 607d560168bSGuenter Roeck err = PTR_ERR(attrs); 608d560168bSGuenter Roeck goto free_hwmon; 609d560168bSGuenter Roeck } 610d560168bSGuenter Roeck 611d560168bSGuenter Roeck hwdev->group.attrs = attrs; 612d560168bSGuenter Roeck ngroups = 0; 613d560168bSGuenter Roeck hwdev->groups[ngroups++] = &hwdev->group; 614d560168bSGuenter Roeck 615d560168bSGuenter Roeck if (groups) { 616d560168bSGuenter Roeck for (i = 0; groups[i]; i++) 617d560168bSGuenter Roeck hwdev->groups[ngroups++] = groups[i]; 618d560168bSGuenter Roeck } 619d560168bSGuenter Roeck 620d560168bSGuenter Roeck hdev->groups = hwdev->groups; 621d560168bSGuenter Roeck } else { 622d560168bSGuenter Roeck hdev->groups = groups; 623d560168bSGuenter Roeck } 624d560168bSGuenter Roeck 625d560168bSGuenter Roeck hwdev->name = name; 626d560168bSGuenter Roeck hdev->class = &hwmon_class; 627d560168bSGuenter Roeck hdev->parent = dev; 628d560168bSGuenter Roeck hdev->of_node = dev ? dev->of_node : NULL; 629d560168bSGuenter Roeck hwdev->chip = chip; 630d560168bSGuenter Roeck dev_set_drvdata(hdev, drvdata); 631d560168bSGuenter Roeck dev_set_name(hdev, HWMON_ID_FORMAT, id); 632d560168bSGuenter Roeck err = device_register(hdev); 633d560168bSGuenter Roeck if (err) 634d560168bSGuenter Roeck goto free_hwmon; 635d560168bSGuenter Roeck 636319fe159SGuenter Roeck if (dev && chip && chip->ops->read && 637d560168bSGuenter Roeck chip->info[0]->type == hwmon_chip && 638d560168bSGuenter Roeck (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) { 639d560168bSGuenter Roeck const struct hwmon_channel_info **info = chip->info; 640d560168bSGuenter Roeck 641d560168bSGuenter Roeck for (i = 1; info[i]; i++) { 642d560168bSGuenter Roeck if (info[i]->type != hwmon_temp) 643d560168bSGuenter Roeck continue; 644d560168bSGuenter Roeck 645d560168bSGuenter Roeck for (j = 0; info[i]->config[j]; j++) { 646d560168bSGuenter Roeck if (!chip->ops->is_visible(drvdata, hwmon_temp, 647d560168bSGuenter Roeck hwmon_temp_input, j)) 648d560168bSGuenter Roeck continue; 64947c332deSLinus Walleij if (info[i]->config[j] & HWMON_T_INPUT) { 65047c332deSLinus Walleij err = hwmon_thermal_add_sensor(dev, 65147c332deSLinus Walleij hwdev, j); 65274e35127SDmitry Osipenko if (err) { 65374e35127SDmitry Osipenko device_unregister(hdev); 65474e35127SDmitry Osipenko goto ida_remove; 65574e35127SDmitry Osipenko } 65647c332deSLinus Walleij } 657d560168bSGuenter Roeck } 658d560168bSGuenter Roeck } 659d560168bSGuenter Roeck } 660d560168bSGuenter Roeck 661d560168bSGuenter Roeck return hdev; 662d560168bSGuenter Roeck 663d560168bSGuenter Roeck free_hwmon: 664d560168bSGuenter Roeck kfree(hwdev); 665d560168bSGuenter Roeck ida_remove: 666d560168bSGuenter Roeck ida_simple_remove(&hwmon_ida, id); 667d560168bSGuenter Roeck return ERR_PTR(err); 668d560168bSGuenter Roeck } 669d560168bSGuenter Roeck 6701236441fSMark M. Hoffman /** 671bab2243cSGuenter Roeck * hwmon_device_register_with_groups - register w/ hwmon 672bab2243cSGuenter Roeck * @dev: the parent device 673bab2243cSGuenter Roeck * @name: hwmon name attribute 674bab2243cSGuenter Roeck * @drvdata: driver data to attach to created device 675bab2243cSGuenter Roeck * @groups: List of attribute groups to create 676bab2243cSGuenter Roeck * 677bab2243cSGuenter Roeck * hwmon_device_unregister() must be called when the device is no 678bab2243cSGuenter Roeck * longer needed. 679bab2243cSGuenter Roeck * 680bab2243cSGuenter Roeck * Returns the pointer to the new device. 681bab2243cSGuenter Roeck */ 682bab2243cSGuenter Roeck struct device * 683bab2243cSGuenter Roeck hwmon_device_register_with_groups(struct device *dev, const char *name, 684bab2243cSGuenter Roeck void *drvdata, 685bab2243cSGuenter Roeck const struct attribute_group **groups) 686bab2243cSGuenter Roeck { 6878353863aSGuenter Roeck if (!name) 6888353863aSGuenter Roeck return ERR_PTR(-EINVAL); 6898353863aSGuenter Roeck 690d560168bSGuenter Roeck return __hwmon_device_register(dev, name, drvdata, NULL, groups); 691bab2243cSGuenter Roeck } 692bab2243cSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups); 693bab2243cSGuenter Roeck 694bab2243cSGuenter Roeck /** 695d560168bSGuenter Roeck * hwmon_device_register_with_info - register w/ hwmon 696d560168bSGuenter Roeck * @dev: the parent device 697d560168bSGuenter Roeck * @name: hwmon name attribute 698d560168bSGuenter Roeck * @drvdata: driver data to attach to created device 6993870945aSGuenter Roeck * @chip: pointer to hwmon chip information 700848ba0a2SGuenter Roeck * @extra_groups: pointer to list of additional non-standard attribute groups 701d560168bSGuenter Roeck * 702d560168bSGuenter Roeck * hwmon_device_unregister() must be called when the device is no 703d560168bSGuenter Roeck * longer needed. 704d560168bSGuenter Roeck * 705d560168bSGuenter Roeck * Returns the pointer to the new device. 706d560168bSGuenter Roeck */ 707d560168bSGuenter Roeck struct device * 708d560168bSGuenter Roeck hwmon_device_register_with_info(struct device *dev, const char *name, 709d560168bSGuenter Roeck void *drvdata, 710d560168bSGuenter Roeck const struct hwmon_chip_info *chip, 711848ba0a2SGuenter Roeck const struct attribute_group **extra_groups) 712d560168bSGuenter Roeck { 7138353863aSGuenter Roeck if (!name) 7148353863aSGuenter Roeck return ERR_PTR(-EINVAL); 7158353863aSGuenter Roeck 716239552f4SGuenter Roeck if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info)) 717d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 718d560168bSGuenter Roeck 71959df4f4eSLucas Magasweran if (chip && !dev) 72059df4f4eSLucas Magasweran return ERR_PTR(-EINVAL); 72159df4f4eSLucas Magasweran 722848ba0a2SGuenter Roeck return __hwmon_device_register(dev, name, drvdata, chip, extra_groups); 723d560168bSGuenter Roeck } 724d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_info); 725d560168bSGuenter Roeck 726d560168bSGuenter Roeck /** 7271beeffe4STony Jones * hwmon_device_register - register w/ hwmon 7281236441fSMark M. Hoffman * @dev: the device to register 7291236441fSMark M. Hoffman * 7301beeffe4STony Jones * hwmon_device_unregister() must be called when the device is no 7311236441fSMark M. Hoffman * longer needed. 7321236441fSMark M. Hoffman * 7331beeffe4STony Jones * Returns the pointer to the new device. 7341236441fSMark M. Hoffman */ 7351beeffe4STony Jones struct device *hwmon_device_register(struct device *dev) 7361236441fSMark M. Hoffman { 737af1bd36cSGuenter Roeck dev_warn(dev, 738af1bd36cSGuenter Roeck "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n"); 739af1bd36cSGuenter Roeck 7408353863aSGuenter Roeck return __hwmon_device_register(dev, NULL, NULL, NULL, NULL); 7411236441fSMark M. Hoffman } 742839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_register); 7431236441fSMark M. Hoffman 7441236441fSMark M. Hoffman /** 7451236441fSMark M. Hoffman * hwmon_device_unregister - removes the previously registered class device 7461236441fSMark M. Hoffman * 7471beeffe4STony Jones * @dev: the class device to destroy 7481236441fSMark M. Hoffman */ 7491beeffe4STony Jones void hwmon_device_unregister(struct device *dev) 7501236441fSMark M. Hoffman { 7511236441fSMark M. Hoffman int id; 7521236441fSMark M. Hoffman 753739cf3a2SKay Sievers if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) { 7541beeffe4STony Jones device_unregister(dev); 7554ca5f468SJonathan Cameron ida_simple_remove(&hwmon_ida, id); 7561236441fSMark M. Hoffman } else 7571beeffe4STony Jones dev_dbg(dev->parent, 7581236441fSMark M. Hoffman "hwmon_device_unregister() failed: bad class ID!\n"); 7591236441fSMark M. Hoffman } 760839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_unregister); 7611236441fSMark M. Hoffman 76274188cbaSGuenter Roeck static void devm_hwmon_release(struct device *dev, void *res) 76374188cbaSGuenter Roeck { 76474188cbaSGuenter Roeck struct device *hwdev = *(struct device **)res; 76574188cbaSGuenter Roeck 76674188cbaSGuenter Roeck hwmon_device_unregister(hwdev); 76774188cbaSGuenter Roeck } 76874188cbaSGuenter Roeck 76974188cbaSGuenter Roeck /** 77074188cbaSGuenter Roeck * devm_hwmon_device_register_with_groups - register w/ hwmon 77174188cbaSGuenter Roeck * @dev: the parent device 77274188cbaSGuenter Roeck * @name: hwmon name attribute 77374188cbaSGuenter Roeck * @drvdata: driver data to attach to created device 77474188cbaSGuenter Roeck * @groups: List of attribute groups to create 77574188cbaSGuenter Roeck * 77674188cbaSGuenter Roeck * Returns the pointer to the new device. The new device is automatically 77774188cbaSGuenter Roeck * unregistered with the parent device. 77874188cbaSGuenter Roeck */ 77974188cbaSGuenter Roeck struct device * 78074188cbaSGuenter Roeck devm_hwmon_device_register_with_groups(struct device *dev, const char *name, 78174188cbaSGuenter Roeck void *drvdata, 78274188cbaSGuenter Roeck const struct attribute_group **groups) 78374188cbaSGuenter Roeck { 78474188cbaSGuenter Roeck struct device **ptr, *hwdev; 78574188cbaSGuenter Roeck 78674188cbaSGuenter Roeck if (!dev) 78774188cbaSGuenter Roeck return ERR_PTR(-EINVAL); 78874188cbaSGuenter Roeck 78974188cbaSGuenter Roeck ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); 79074188cbaSGuenter Roeck if (!ptr) 79174188cbaSGuenter Roeck return ERR_PTR(-ENOMEM); 79274188cbaSGuenter Roeck 79374188cbaSGuenter Roeck hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups); 79474188cbaSGuenter Roeck if (IS_ERR(hwdev)) 79574188cbaSGuenter Roeck goto error; 79674188cbaSGuenter Roeck 79774188cbaSGuenter Roeck *ptr = hwdev; 79874188cbaSGuenter Roeck devres_add(dev, ptr); 79974188cbaSGuenter Roeck return hwdev; 80074188cbaSGuenter Roeck 80174188cbaSGuenter Roeck error: 80274188cbaSGuenter Roeck devres_free(ptr); 80374188cbaSGuenter Roeck return hwdev; 80474188cbaSGuenter Roeck } 80574188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups); 80674188cbaSGuenter Roeck 807d560168bSGuenter Roeck /** 808d560168bSGuenter Roeck * devm_hwmon_device_register_with_info - register w/ hwmon 809d560168bSGuenter Roeck * @dev: the parent device 810d560168bSGuenter Roeck * @name: hwmon name attribute 811d560168bSGuenter Roeck * @drvdata: driver data to attach to created device 8123870945aSGuenter Roeck * @chip: pointer to hwmon chip information 8133870945aSGuenter Roeck * @groups: pointer to list of driver specific attribute groups 814d560168bSGuenter Roeck * 815d560168bSGuenter Roeck * Returns the pointer to the new device. The new device is automatically 816d560168bSGuenter Roeck * unregistered with the parent device. 817d560168bSGuenter Roeck */ 818d560168bSGuenter Roeck struct device * 819d560168bSGuenter Roeck devm_hwmon_device_register_with_info(struct device *dev, const char *name, 820d560168bSGuenter Roeck void *drvdata, 821d560168bSGuenter Roeck const struct hwmon_chip_info *chip, 822d560168bSGuenter Roeck const struct attribute_group **groups) 823d560168bSGuenter Roeck { 824d560168bSGuenter Roeck struct device **ptr, *hwdev; 825d560168bSGuenter Roeck 826d560168bSGuenter Roeck if (!dev) 827d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 828d560168bSGuenter Roeck 829d560168bSGuenter Roeck ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); 830d560168bSGuenter Roeck if (!ptr) 831d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 832d560168bSGuenter Roeck 833d560168bSGuenter Roeck hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip, 834d560168bSGuenter Roeck groups); 835d560168bSGuenter Roeck if (IS_ERR(hwdev)) 836d560168bSGuenter Roeck goto error; 837d560168bSGuenter Roeck 838d560168bSGuenter Roeck *ptr = hwdev; 839d560168bSGuenter Roeck devres_add(dev, ptr); 840d560168bSGuenter Roeck 841d560168bSGuenter Roeck return hwdev; 842d560168bSGuenter Roeck 843d560168bSGuenter Roeck error: 844d560168bSGuenter Roeck devres_free(ptr); 845d560168bSGuenter Roeck return hwdev; 846d560168bSGuenter Roeck } 847d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info); 848d560168bSGuenter Roeck 84974188cbaSGuenter Roeck static int devm_hwmon_match(struct device *dev, void *res, void *data) 85074188cbaSGuenter Roeck { 85174188cbaSGuenter Roeck struct device **hwdev = res; 85274188cbaSGuenter Roeck 85374188cbaSGuenter Roeck return *hwdev == data; 85474188cbaSGuenter Roeck } 85574188cbaSGuenter Roeck 85674188cbaSGuenter Roeck /** 85774188cbaSGuenter Roeck * devm_hwmon_device_unregister - removes a previously registered hwmon device 85874188cbaSGuenter Roeck * 85974188cbaSGuenter Roeck * @dev: the parent device of the device to unregister 86074188cbaSGuenter Roeck */ 86174188cbaSGuenter Roeck void devm_hwmon_device_unregister(struct device *dev) 86274188cbaSGuenter Roeck { 86374188cbaSGuenter Roeck WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev)); 86474188cbaSGuenter Roeck } 86574188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister); 86674188cbaSGuenter Roeck 8672958b1ecSJean Delvare static void __init hwmon_pci_quirks(void) 8682958b1ecSJean Delvare { 8692958b1ecSJean Delvare #if defined CONFIG_X86 && defined CONFIG_PCI 8702958b1ecSJean Delvare struct pci_dev *sb; 8712958b1ecSJean Delvare u16 base; 8722958b1ecSJean Delvare u8 enable; 8732958b1ecSJean Delvare 8742958b1ecSJean Delvare /* Open access to 0x295-0x296 on MSI MS-7031 */ 8752958b1ecSJean Delvare sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL); 876d6dab7ddSJean Delvare if (sb) { 877d6dab7ddSJean Delvare if (sb->subsystem_vendor == 0x1462 && /* MSI */ 878d6dab7ddSJean Delvare sb->subsystem_device == 0x0031) { /* MS-7031 */ 8792958b1ecSJean Delvare pci_read_config_byte(sb, 0x48, &enable); 8802958b1ecSJean Delvare pci_read_config_word(sb, 0x64, &base); 8812958b1ecSJean Delvare 8822958b1ecSJean Delvare if (base == 0 && !(enable & BIT(2))) { 8832958b1ecSJean Delvare dev_info(&sb->dev, 8842958b1ecSJean Delvare "Opening wide generic port at 0x295\n"); 8852958b1ecSJean Delvare pci_write_config_word(sb, 0x64, 0x295); 886d6dab7ddSJean Delvare pci_write_config_byte(sb, 0x48, 887d6dab7ddSJean Delvare enable | BIT(2)); 8882958b1ecSJean Delvare } 8892958b1ecSJean Delvare } 890d6dab7ddSJean Delvare pci_dev_put(sb); 891d6dab7ddSJean Delvare } 8922958b1ecSJean Delvare #endif 8932958b1ecSJean Delvare } 8942958b1ecSJean Delvare 8951236441fSMark M. Hoffman static int __init hwmon_init(void) 8961236441fSMark M. Hoffman { 897bab2243cSGuenter Roeck int err; 898bab2243cSGuenter Roeck 8992958b1ecSJean Delvare hwmon_pci_quirks(); 9002958b1ecSJean Delvare 901bab2243cSGuenter Roeck err = class_register(&hwmon_class); 902bab2243cSGuenter Roeck if (err) { 903bab2243cSGuenter Roeck pr_err("couldn't register hwmon sysfs class\n"); 904bab2243cSGuenter Roeck return err; 9051236441fSMark M. Hoffman } 9061236441fSMark M. Hoffman return 0; 9071236441fSMark M. Hoffman } 9081236441fSMark M. Hoffman 9091236441fSMark M. Hoffman static void __exit hwmon_exit(void) 9101236441fSMark M. Hoffman { 911bab2243cSGuenter Roeck class_unregister(&hwmon_class); 9121236441fSMark M. Hoffman } 9131236441fSMark M. Hoffman 91437f54ee5SDavid Brownell subsys_initcall(hwmon_init); 9151236441fSMark M. Hoffman module_exit(hwmon_exit); 9161236441fSMark M. Hoffman 9171236441fSMark M. Hoffman MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>"); 9181236441fSMark M. Hoffman MODULE_DESCRIPTION("hardware monitoring sysfs/class support"); 9191236441fSMark M. Hoffman MODULE_LICENSE("GPL"); 9201236441fSMark M. Hoffman 921