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 271236441fSMark M. Hoffman #define HWMON_ID_PREFIX "hwmon" 281236441fSMark M. Hoffman #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d" 291236441fSMark M. Hoffman 30bab2243cSGuenter Roeck struct hwmon_device { 31bab2243cSGuenter Roeck const char *name; 32bab2243cSGuenter Roeck struct device dev; 33d560168bSGuenter Roeck const struct hwmon_chip_info *chip; 34d560168bSGuenter Roeck 35d560168bSGuenter Roeck struct attribute_group group; 36d560168bSGuenter Roeck const struct attribute_group **groups; 37bab2243cSGuenter Roeck }; 38d560168bSGuenter Roeck 39bab2243cSGuenter Roeck #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev) 40bab2243cSGuenter Roeck 41d560168bSGuenter Roeck struct hwmon_device_attribute { 42d560168bSGuenter Roeck struct device_attribute dev_attr; 43d560168bSGuenter Roeck const struct hwmon_ops *ops; 44d560168bSGuenter Roeck enum hwmon_sensor_types type; 45d560168bSGuenter Roeck u32 attr; 46d560168bSGuenter Roeck int index; 47d560168bSGuenter Roeck }; 48d560168bSGuenter Roeck 49d560168bSGuenter Roeck #define to_hwmon_attr(d) \ 50d560168bSGuenter Roeck container_of(d, struct hwmon_device_attribute, dev_attr) 51d560168bSGuenter Roeck 52d560168bSGuenter Roeck /* 53d560168bSGuenter Roeck * Thermal zone information 54d560168bSGuenter Roeck * In addition to the reference to the hwmon device, 55d560168bSGuenter Roeck * also provides the sensor index. 56d560168bSGuenter Roeck */ 57d560168bSGuenter Roeck struct hwmon_thermal_data { 58d560168bSGuenter Roeck struct hwmon_device *hwdev; /* Reference to hwmon device */ 59d560168bSGuenter Roeck int index; /* sensor index */ 60d560168bSGuenter Roeck }; 61d560168bSGuenter Roeck 62bab2243cSGuenter Roeck static ssize_t 63bab2243cSGuenter Roeck show_name(struct device *dev, struct device_attribute *attr, char *buf) 64bab2243cSGuenter Roeck { 65bab2243cSGuenter Roeck return sprintf(buf, "%s\n", to_hwmon_device(dev)->name); 66bab2243cSGuenter Roeck } 67bab2243cSGuenter Roeck static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); 68bab2243cSGuenter Roeck 69bab2243cSGuenter Roeck static struct attribute *hwmon_dev_attrs[] = { 70bab2243cSGuenter Roeck &dev_attr_name.attr, 71bab2243cSGuenter Roeck NULL 72bab2243cSGuenter Roeck }; 73bab2243cSGuenter Roeck 74bab2243cSGuenter Roeck static umode_t hwmon_dev_name_is_visible(struct kobject *kobj, 75bab2243cSGuenter Roeck struct attribute *attr, int n) 76bab2243cSGuenter Roeck { 77bab2243cSGuenter Roeck struct device *dev = container_of(kobj, struct device, kobj); 78bab2243cSGuenter Roeck 79bab2243cSGuenter Roeck if (to_hwmon_device(dev)->name == NULL) 80bab2243cSGuenter Roeck return 0; 81bab2243cSGuenter Roeck 82bab2243cSGuenter Roeck return attr->mode; 83bab2243cSGuenter Roeck } 84bab2243cSGuenter Roeck 85bab2243cSGuenter Roeck static struct attribute_group hwmon_dev_attr_group = { 86bab2243cSGuenter Roeck .attrs = hwmon_dev_attrs, 87bab2243cSGuenter Roeck .is_visible = hwmon_dev_name_is_visible, 88bab2243cSGuenter Roeck }; 89bab2243cSGuenter Roeck 90bab2243cSGuenter Roeck static const struct attribute_group *hwmon_dev_attr_groups[] = { 91bab2243cSGuenter Roeck &hwmon_dev_attr_group, 92bab2243cSGuenter Roeck NULL 93bab2243cSGuenter Roeck }; 94bab2243cSGuenter Roeck 95bab2243cSGuenter Roeck static void hwmon_dev_release(struct device *dev) 96bab2243cSGuenter Roeck { 97bab2243cSGuenter Roeck kfree(to_hwmon_device(dev)); 98bab2243cSGuenter Roeck } 99bab2243cSGuenter Roeck 100bab2243cSGuenter Roeck static struct class hwmon_class = { 101bab2243cSGuenter Roeck .name = "hwmon", 102bab2243cSGuenter Roeck .owner = THIS_MODULE, 103bab2243cSGuenter Roeck .dev_groups = hwmon_dev_attr_groups, 104bab2243cSGuenter Roeck .dev_release = hwmon_dev_release, 105bab2243cSGuenter Roeck }; 1061236441fSMark M. Hoffman 1074ca5f468SJonathan Cameron static DEFINE_IDA(hwmon_ida); 1081236441fSMark M. Hoffman 109d560168bSGuenter Roeck /* Thermal zone handling */ 110d560168bSGuenter Roeck 111d560168bSGuenter Roeck #if IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF) 112d560168bSGuenter Roeck static int hwmon_thermal_get_temp(void *data, int *temp) 113d560168bSGuenter Roeck { 114d560168bSGuenter Roeck struct hwmon_thermal_data *tdata = data; 115d560168bSGuenter Roeck struct hwmon_device *hwdev = tdata->hwdev; 116d560168bSGuenter Roeck int ret; 117d560168bSGuenter Roeck long t; 118d560168bSGuenter Roeck 119d560168bSGuenter Roeck ret = hwdev->chip->ops->read(&hwdev->dev, hwmon_temp, hwmon_temp_input, 120d560168bSGuenter Roeck tdata->index, &t); 121d560168bSGuenter Roeck if (ret < 0) 122d560168bSGuenter Roeck return ret; 123d560168bSGuenter Roeck 124d560168bSGuenter Roeck *temp = t; 125d560168bSGuenter Roeck 126d560168bSGuenter Roeck return 0; 127d560168bSGuenter Roeck } 128d560168bSGuenter Roeck 129d560168bSGuenter Roeck static struct thermal_zone_of_device_ops hwmon_thermal_ops = { 130d560168bSGuenter Roeck .get_temp = hwmon_thermal_get_temp, 131d560168bSGuenter Roeck }; 132d560168bSGuenter Roeck 133d560168bSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev, 134d560168bSGuenter Roeck struct hwmon_device *hwdev, int index) 135d560168bSGuenter Roeck { 136d560168bSGuenter Roeck struct hwmon_thermal_data *tdata; 137d560168bSGuenter Roeck 138d560168bSGuenter Roeck tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL); 139d560168bSGuenter Roeck if (!tdata) 140d560168bSGuenter Roeck return -ENOMEM; 141d560168bSGuenter Roeck 142d560168bSGuenter Roeck tdata->hwdev = hwdev; 143d560168bSGuenter Roeck tdata->index = index; 144d560168bSGuenter Roeck 145d560168bSGuenter Roeck devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata, 146d560168bSGuenter Roeck &hwmon_thermal_ops); 147d560168bSGuenter Roeck 148d560168bSGuenter Roeck return 0; 149d560168bSGuenter Roeck } 150d560168bSGuenter Roeck #else 151d560168bSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev, 152d560168bSGuenter Roeck struct hwmon_device *hwdev, int index) 153d560168bSGuenter Roeck { 154d560168bSGuenter Roeck return 0; 155d560168bSGuenter Roeck } 156d560168bSGuenter Roeck #endif /* IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF) */ 157d560168bSGuenter Roeck 158d560168bSGuenter Roeck /* sysfs attribute management */ 159d560168bSGuenter Roeck 160d560168bSGuenter Roeck static ssize_t hwmon_attr_show(struct device *dev, 161d560168bSGuenter Roeck struct device_attribute *devattr, char *buf) 162d560168bSGuenter Roeck { 163d560168bSGuenter Roeck struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); 164d560168bSGuenter Roeck long val; 165d560168bSGuenter Roeck int ret; 166d560168bSGuenter Roeck 167d560168bSGuenter Roeck ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index, 168d560168bSGuenter Roeck &val); 169d560168bSGuenter Roeck if (ret < 0) 170d560168bSGuenter Roeck return ret; 171d560168bSGuenter Roeck 172d560168bSGuenter Roeck return sprintf(buf, "%ld\n", val); 173d560168bSGuenter Roeck } 174d560168bSGuenter Roeck 175d560168bSGuenter Roeck static ssize_t hwmon_attr_store(struct device *dev, 176d560168bSGuenter Roeck struct device_attribute *devattr, 177d560168bSGuenter Roeck const char *buf, size_t count) 178d560168bSGuenter Roeck { 179d560168bSGuenter Roeck struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); 180d560168bSGuenter Roeck long val; 181d560168bSGuenter Roeck int ret; 182d560168bSGuenter Roeck 183d560168bSGuenter Roeck ret = kstrtol(buf, 10, &val); 184d560168bSGuenter Roeck if (ret < 0) 185d560168bSGuenter Roeck return ret; 186d560168bSGuenter Roeck 187d560168bSGuenter Roeck ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index, 188d560168bSGuenter Roeck val); 189d560168bSGuenter Roeck if (ret < 0) 190d560168bSGuenter Roeck return ret; 191d560168bSGuenter Roeck 192d560168bSGuenter Roeck return count; 193d560168bSGuenter Roeck } 194d560168bSGuenter Roeck 195d560168bSGuenter Roeck static int hwmon_attr_base(enum hwmon_sensor_types type) 196d560168bSGuenter Roeck { 197d560168bSGuenter Roeck if (type == hwmon_in) 198d560168bSGuenter Roeck return 0; 199d560168bSGuenter Roeck return 1; 200d560168bSGuenter Roeck } 201d560168bSGuenter Roeck 202d560168bSGuenter Roeck static struct attribute *hwmon_genattr(struct device *dev, 203d560168bSGuenter Roeck const void *drvdata, 204d560168bSGuenter Roeck enum hwmon_sensor_types type, 205d560168bSGuenter Roeck u32 attr, 206d560168bSGuenter Roeck int index, 207d560168bSGuenter Roeck const char *template, 208d560168bSGuenter Roeck const struct hwmon_ops *ops) 209d560168bSGuenter Roeck { 210d560168bSGuenter Roeck struct hwmon_device_attribute *hattr; 211d560168bSGuenter Roeck struct device_attribute *dattr; 212d560168bSGuenter Roeck struct attribute *a; 213d560168bSGuenter Roeck umode_t mode; 214d560168bSGuenter Roeck char *name; 215d560168bSGuenter Roeck 216d560168bSGuenter Roeck /* The attribute is invisible if there is no template string */ 217d560168bSGuenter Roeck if (!template) 218d560168bSGuenter Roeck return ERR_PTR(-ENOENT); 219d560168bSGuenter Roeck 220d560168bSGuenter Roeck mode = ops->is_visible(drvdata, type, attr, index); 221d560168bSGuenter Roeck if (!mode) 222d560168bSGuenter Roeck return ERR_PTR(-ENOENT); 223d560168bSGuenter Roeck 224d560168bSGuenter Roeck if ((mode & S_IRUGO) && !ops->read) 225d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 226d560168bSGuenter Roeck if ((mode & S_IWUGO) && !ops->write) 227d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 228d560168bSGuenter Roeck 229d560168bSGuenter Roeck if (type == hwmon_chip) { 230d560168bSGuenter Roeck name = (char *)template; 231d560168bSGuenter Roeck } else { 232d560168bSGuenter Roeck name = devm_kzalloc(dev, strlen(template) + 16, GFP_KERNEL); 233d560168bSGuenter Roeck if (!name) 234d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 235d560168bSGuenter Roeck scnprintf(name, strlen(template) + 16, template, 236d560168bSGuenter Roeck index + hwmon_attr_base(type)); 237d560168bSGuenter Roeck } 238d560168bSGuenter Roeck 239d560168bSGuenter Roeck hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL); 240d560168bSGuenter Roeck if (!hattr) 241d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 242d560168bSGuenter Roeck 243d560168bSGuenter Roeck hattr->type = type; 244d560168bSGuenter Roeck hattr->attr = attr; 245d560168bSGuenter Roeck hattr->index = index; 246d560168bSGuenter Roeck hattr->ops = ops; 247d560168bSGuenter Roeck 248d560168bSGuenter Roeck dattr = &hattr->dev_attr; 249d560168bSGuenter Roeck dattr->show = hwmon_attr_show; 250d560168bSGuenter Roeck dattr->store = hwmon_attr_store; 251d560168bSGuenter Roeck 252d560168bSGuenter Roeck a = &dattr->attr; 253d560168bSGuenter Roeck sysfs_attr_init(a); 254d560168bSGuenter Roeck a->name = name; 255d560168bSGuenter Roeck a->mode = mode; 256d560168bSGuenter Roeck 257d560168bSGuenter Roeck return a; 258d560168bSGuenter Roeck } 259d560168bSGuenter Roeck 260d560168bSGuenter Roeck static const char * const hwmon_chip_attr_templates[] = { 261d560168bSGuenter Roeck [hwmon_chip_temp_reset_history] = "temp_reset_history", 262*00d616cfSGuenter Roeck [hwmon_chip_in_reset_history] = "in_reset_history", 263d560168bSGuenter Roeck [hwmon_chip_update_interval] = "update_interval", 264d560168bSGuenter Roeck [hwmon_chip_alarms] = "alarms", 265d560168bSGuenter Roeck }; 266d560168bSGuenter Roeck 267d560168bSGuenter Roeck static const char * const hwmon_temp_attr_templates[] = { 268d560168bSGuenter Roeck [hwmon_temp_input] = "temp%d_input", 269d560168bSGuenter Roeck [hwmon_temp_type] = "temp%d_type", 270d560168bSGuenter Roeck [hwmon_temp_lcrit] = "temp%d_lcrit", 271d560168bSGuenter Roeck [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst", 272d560168bSGuenter Roeck [hwmon_temp_min] = "temp%d_min", 273d560168bSGuenter Roeck [hwmon_temp_min_hyst] = "temp%d_min_hyst", 274d560168bSGuenter Roeck [hwmon_temp_max] = "temp%d_max", 275d560168bSGuenter Roeck [hwmon_temp_max_hyst] = "temp%d_max_hyst", 276d560168bSGuenter Roeck [hwmon_temp_crit] = "temp%d_crit", 277d560168bSGuenter Roeck [hwmon_temp_crit_hyst] = "temp%d_crit_hyst", 278d560168bSGuenter Roeck [hwmon_temp_emergency] = "temp%d_emergency", 279d560168bSGuenter Roeck [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst", 280d560168bSGuenter Roeck [hwmon_temp_alarm] = "temp%d_alarm", 281d560168bSGuenter Roeck [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm", 282d560168bSGuenter Roeck [hwmon_temp_min_alarm] = "temp%d_min_alarm", 283d560168bSGuenter Roeck [hwmon_temp_max_alarm] = "temp%d_max_alarm", 284d560168bSGuenter Roeck [hwmon_temp_crit_alarm] = "temp%d_crit_alarm", 285d560168bSGuenter Roeck [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm", 286d560168bSGuenter Roeck [hwmon_temp_fault] = "temp%d_fault", 287d560168bSGuenter Roeck [hwmon_temp_offset] = "temp%d_offset", 288d560168bSGuenter Roeck [hwmon_temp_label] = "temp%d_label", 289d560168bSGuenter Roeck [hwmon_temp_lowest] = "temp%d_lowest", 290d560168bSGuenter Roeck [hwmon_temp_highest] = "temp%d_highest", 291d560168bSGuenter Roeck [hwmon_temp_reset_history] = "temp%d_reset_history", 292d560168bSGuenter Roeck }; 293d560168bSGuenter Roeck 294*00d616cfSGuenter Roeck static const char * const hwmon_in_attr_templates[] = { 295*00d616cfSGuenter Roeck [hwmon_in_input] = "in%d_input", 296*00d616cfSGuenter Roeck [hwmon_in_min] = "in%d_min", 297*00d616cfSGuenter Roeck [hwmon_in_max] = "in%d_max", 298*00d616cfSGuenter Roeck [hwmon_in_lcrit] = "in%d_lcrit", 299*00d616cfSGuenter Roeck [hwmon_in_crit] = "in%d_crit", 300*00d616cfSGuenter Roeck [hwmon_in_average] = "in%d_average", 301*00d616cfSGuenter Roeck [hwmon_in_lowest] = "in%d_lowest", 302*00d616cfSGuenter Roeck [hwmon_in_highest] = "in%d_highest", 303*00d616cfSGuenter Roeck [hwmon_in_reset_history] = "in%d_reset_history", 304*00d616cfSGuenter Roeck [hwmon_in_label] = "in%d_label", 305*00d616cfSGuenter Roeck [hwmon_in_alarm] = "in%d_alarm", 306*00d616cfSGuenter Roeck [hwmon_in_min_alarm] = "in%d_min_alarm", 307*00d616cfSGuenter Roeck [hwmon_in_max_alarm] = "in%d_max_alarm", 308*00d616cfSGuenter Roeck [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm", 309*00d616cfSGuenter Roeck [hwmon_in_crit_alarm] = "in%d_crit_alarm", 310*00d616cfSGuenter Roeck }; 311*00d616cfSGuenter Roeck 312d560168bSGuenter Roeck static const char * const *__templates[] = { 313d560168bSGuenter Roeck [hwmon_chip] = hwmon_chip_attr_templates, 314d560168bSGuenter Roeck [hwmon_temp] = hwmon_temp_attr_templates, 315*00d616cfSGuenter Roeck [hwmon_in] = hwmon_in_attr_templates, 316d560168bSGuenter Roeck }; 317d560168bSGuenter Roeck 318d560168bSGuenter Roeck static const int __templates_size[] = { 319d560168bSGuenter Roeck [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attr_templates), 320d560168bSGuenter Roeck [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates), 321*00d616cfSGuenter Roeck [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates), 322d560168bSGuenter Roeck }; 323d560168bSGuenter Roeck 324d560168bSGuenter Roeck static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info) 325d560168bSGuenter Roeck { 326d560168bSGuenter Roeck int i, n; 327d560168bSGuenter Roeck 328d560168bSGuenter Roeck for (i = n = 0; info->config[i]; i++) 329d560168bSGuenter Roeck n += hweight32(info->config[i]); 330d560168bSGuenter Roeck 331d560168bSGuenter Roeck return n; 332d560168bSGuenter Roeck } 333d560168bSGuenter Roeck 334d560168bSGuenter Roeck static int hwmon_genattrs(struct device *dev, 335d560168bSGuenter Roeck const void *drvdata, 336d560168bSGuenter Roeck struct attribute **attrs, 337d560168bSGuenter Roeck const struct hwmon_ops *ops, 338d560168bSGuenter Roeck const struct hwmon_channel_info *info) 339d560168bSGuenter Roeck { 340d560168bSGuenter Roeck const char * const *templates; 341d560168bSGuenter Roeck int template_size; 342d560168bSGuenter Roeck int i, aindex = 0; 343d560168bSGuenter Roeck 344d560168bSGuenter Roeck if (info->type >= ARRAY_SIZE(__templates)) 345d560168bSGuenter Roeck return -EINVAL; 346d560168bSGuenter Roeck 347d560168bSGuenter Roeck templates = __templates[info->type]; 348d560168bSGuenter Roeck template_size = __templates_size[info->type]; 349d560168bSGuenter Roeck 350d560168bSGuenter Roeck for (i = 0; info->config[i]; i++) { 351d560168bSGuenter Roeck u32 attr_mask = info->config[i]; 352d560168bSGuenter Roeck u32 attr; 353d560168bSGuenter Roeck 354d560168bSGuenter Roeck while (attr_mask) { 355d560168bSGuenter Roeck struct attribute *a; 356d560168bSGuenter Roeck 357d560168bSGuenter Roeck attr = __ffs(attr_mask); 358d560168bSGuenter Roeck attr_mask &= ~BIT(attr); 359d560168bSGuenter Roeck if (attr >= template_size) 360d560168bSGuenter Roeck return -EINVAL; 361d560168bSGuenter Roeck a = hwmon_genattr(dev, drvdata, info->type, attr, i, 362d560168bSGuenter Roeck templates[attr], ops); 363d560168bSGuenter Roeck if (IS_ERR(a)) { 364d560168bSGuenter Roeck if (PTR_ERR(a) != -ENOENT) 365d560168bSGuenter Roeck return PTR_ERR(a); 366d560168bSGuenter Roeck continue; 367d560168bSGuenter Roeck } 368d560168bSGuenter Roeck attrs[aindex++] = a; 369d560168bSGuenter Roeck } 370d560168bSGuenter Roeck } 371d560168bSGuenter Roeck return aindex; 372d560168bSGuenter Roeck } 373d560168bSGuenter Roeck 374d560168bSGuenter Roeck static struct attribute ** 375d560168bSGuenter Roeck __hwmon_create_attrs(struct device *dev, const void *drvdata, 376d560168bSGuenter Roeck const struct hwmon_chip_info *chip) 377d560168bSGuenter Roeck { 378d560168bSGuenter Roeck int ret, i, aindex = 0, nattrs = 0; 379d560168bSGuenter Roeck struct attribute **attrs; 380d560168bSGuenter Roeck 381d560168bSGuenter Roeck for (i = 0; chip->info[i]; i++) 382d560168bSGuenter Roeck nattrs += hwmon_num_channel_attrs(chip->info[i]); 383d560168bSGuenter Roeck 384d560168bSGuenter Roeck if (nattrs == 0) 385d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 386d560168bSGuenter Roeck 387d560168bSGuenter Roeck attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL); 388d560168bSGuenter Roeck if (!attrs) 389d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 390d560168bSGuenter Roeck 391d560168bSGuenter Roeck for (i = 0; chip->info[i]; i++) { 392d560168bSGuenter Roeck ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops, 393d560168bSGuenter Roeck chip->info[i]); 394d560168bSGuenter Roeck if (ret < 0) 395d560168bSGuenter Roeck return ERR_PTR(ret); 396d560168bSGuenter Roeck aindex += ret; 397d560168bSGuenter Roeck } 398d560168bSGuenter Roeck 399d560168bSGuenter Roeck return attrs; 400d560168bSGuenter Roeck } 401d560168bSGuenter Roeck 402d560168bSGuenter Roeck static struct device * 403d560168bSGuenter Roeck __hwmon_device_register(struct device *dev, const char *name, void *drvdata, 404d560168bSGuenter Roeck const struct hwmon_chip_info *chip, 405d560168bSGuenter Roeck const struct attribute_group **groups) 406d560168bSGuenter Roeck { 407d560168bSGuenter Roeck struct hwmon_device *hwdev; 408d560168bSGuenter Roeck struct device *hdev; 409d560168bSGuenter Roeck int i, j, err, id; 410d560168bSGuenter Roeck 411d560168bSGuenter Roeck /* Do not accept invalid characters in hwmon name attribute */ 412d560168bSGuenter Roeck if (name && (!strlen(name) || strpbrk(name, "-* \t\n"))) 413d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 414d560168bSGuenter Roeck 415d560168bSGuenter Roeck id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL); 416d560168bSGuenter Roeck if (id < 0) 417d560168bSGuenter Roeck return ERR_PTR(id); 418d560168bSGuenter Roeck 419d560168bSGuenter Roeck hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL); 420d560168bSGuenter Roeck if (hwdev == NULL) { 421d560168bSGuenter Roeck err = -ENOMEM; 422d560168bSGuenter Roeck goto ida_remove; 423d560168bSGuenter Roeck } 424d560168bSGuenter Roeck 425d560168bSGuenter Roeck hdev = &hwdev->dev; 426d560168bSGuenter Roeck 427d560168bSGuenter Roeck if (chip && chip->ops->is_visible) { 428d560168bSGuenter Roeck struct attribute **attrs; 429d560168bSGuenter Roeck int ngroups = 2; 430d560168bSGuenter Roeck 431d560168bSGuenter Roeck if (groups) 432d560168bSGuenter Roeck for (i = 0; groups[i]; i++) 433d560168bSGuenter Roeck ngroups++; 434d560168bSGuenter Roeck 435d560168bSGuenter Roeck hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups), 436d560168bSGuenter Roeck GFP_KERNEL); 437d560168bSGuenter Roeck if (!hwdev->groups) 438d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 439d560168bSGuenter Roeck 440d560168bSGuenter Roeck attrs = __hwmon_create_attrs(dev, drvdata, chip); 441d560168bSGuenter Roeck if (IS_ERR(attrs)) { 442d560168bSGuenter Roeck err = PTR_ERR(attrs); 443d560168bSGuenter Roeck goto free_hwmon; 444d560168bSGuenter Roeck } 445d560168bSGuenter Roeck 446d560168bSGuenter Roeck hwdev->group.attrs = attrs; 447d560168bSGuenter Roeck ngroups = 0; 448d560168bSGuenter Roeck hwdev->groups[ngroups++] = &hwdev->group; 449d560168bSGuenter Roeck 450d560168bSGuenter Roeck if (groups) { 451d560168bSGuenter Roeck for (i = 0; groups[i]; i++) 452d560168bSGuenter Roeck hwdev->groups[ngroups++] = groups[i]; 453d560168bSGuenter Roeck } 454d560168bSGuenter Roeck 455d560168bSGuenter Roeck hdev->groups = hwdev->groups; 456d560168bSGuenter Roeck } else { 457d560168bSGuenter Roeck hdev->groups = groups; 458d560168bSGuenter Roeck } 459d560168bSGuenter Roeck 460d560168bSGuenter Roeck hwdev->name = name; 461d560168bSGuenter Roeck hdev->class = &hwmon_class; 462d560168bSGuenter Roeck hdev->parent = dev; 463d560168bSGuenter Roeck hdev->of_node = dev ? dev->of_node : NULL; 464d560168bSGuenter Roeck hwdev->chip = chip; 465d560168bSGuenter Roeck dev_set_drvdata(hdev, drvdata); 466d560168bSGuenter Roeck dev_set_name(hdev, HWMON_ID_FORMAT, id); 467d560168bSGuenter Roeck err = device_register(hdev); 468d560168bSGuenter Roeck if (err) 469d560168bSGuenter Roeck goto free_hwmon; 470d560168bSGuenter Roeck 471d560168bSGuenter Roeck if (chip && chip->ops->is_visible && chip->ops->read && 472d560168bSGuenter Roeck chip->info[0]->type == hwmon_chip && 473d560168bSGuenter Roeck (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) { 474d560168bSGuenter Roeck const struct hwmon_channel_info **info = chip->info; 475d560168bSGuenter Roeck 476d560168bSGuenter Roeck for (i = 1; info[i]; i++) { 477d560168bSGuenter Roeck if (info[i]->type != hwmon_temp) 478d560168bSGuenter Roeck continue; 479d560168bSGuenter Roeck 480d560168bSGuenter Roeck for (j = 0; info[i]->config[j]; j++) { 481d560168bSGuenter Roeck if (!chip->ops->is_visible(drvdata, hwmon_temp, 482d560168bSGuenter Roeck hwmon_temp_input, j)) 483d560168bSGuenter Roeck continue; 484d560168bSGuenter Roeck if (info[i]->config[j] & HWMON_T_INPUT) 485d560168bSGuenter Roeck hwmon_thermal_add_sensor(dev, hwdev, j); 486d560168bSGuenter Roeck } 487d560168bSGuenter Roeck } 488d560168bSGuenter Roeck } 489d560168bSGuenter Roeck 490d560168bSGuenter Roeck return hdev; 491d560168bSGuenter Roeck 492d560168bSGuenter Roeck free_hwmon: 493d560168bSGuenter Roeck kfree(hwdev); 494d560168bSGuenter Roeck ida_remove: 495d560168bSGuenter Roeck ida_simple_remove(&hwmon_ida, id); 496d560168bSGuenter Roeck return ERR_PTR(err); 497d560168bSGuenter Roeck } 498d560168bSGuenter Roeck 4991236441fSMark M. Hoffman /** 500bab2243cSGuenter Roeck * hwmon_device_register_with_groups - register w/ hwmon 501bab2243cSGuenter Roeck * @dev: the parent device 502bab2243cSGuenter Roeck * @name: hwmon name attribute 503bab2243cSGuenter Roeck * @drvdata: driver data to attach to created device 504bab2243cSGuenter Roeck * @groups: List of attribute groups to create 505bab2243cSGuenter Roeck * 506bab2243cSGuenter Roeck * hwmon_device_unregister() must be called when the device is no 507bab2243cSGuenter Roeck * longer needed. 508bab2243cSGuenter Roeck * 509bab2243cSGuenter Roeck * Returns the pointer to the new device. 510bab2243cSGuenter Roeck */ 511bab2243cSGuenter Roeck struct device * 512bab2243cSGuenter Roeck hwmon_device_register_with_groups(struct device *dev, const char *name, 513bab2243cSGuenter Roeck void *drvdata, 514bab2243cSGuenter Roeck const struct attribute_group **groups) 515bab2243cSGuenter Roeck { 516d560168bSGuenter Roeck return __hwmon_device_register(dev, name, drvdata, NULL, groups); 517bab2243cSGuenter Roeck } 518bab2243cSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups); 519bab2243cSGuenter Roeck 520bab2243cSGuenter Roeck /** 521d560168bSGuenter Roeck * hwmon_device_register_with_info - register w/ hwmon 522d560168bSGuenter Roeck * @dev: the parent device 523d560168bSGuenter Roeck * @name: hwmon name attribute 524d560168bSGuenter Roeck * @drvdata: driver data to attach to created device 525d560168bSGuenter Roeck * @info: Pointer to hwmon chip information 526d560168bSGuenter Roeck * @groups - pointer to list of driver specific attribute groups 527d560168bSGuenter Roeck * 528d560168bSGuenter Roeck * hwmon_device_unregister() must be called when the device is no 529d560168bSGuenter Roeck * longer needed. 530d560168bSGuenter Roeck * 531d560168bSGuenter Roeck * Returns the pointer to the new device. 532d560168bSGuenter Roeck */ 533d560168bSGuenter Roeck struct device * 534d560168bSGuenter Roeck hwmon_device_register_with_info(struct device *dev, const char *name, 535d560168bSGuenter Roeck void *drvdata, 536d560168bSGuenter Roeck const struct hwmon_chip_info *chip, 537d560168bSGuenter Roeck const struct attribute_group **groups) 538d560168bSGuenter Roeck { 539d560168bSGuenter Roeck if (chip && (!chip->ops || !chip->info)) 540d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 541d560168bSGuenter Roeck 542d560168bSGuenter Roeck return __hwmon_device_register(dev, name, drvdata, chip, groups); 543d560168bSGuenter Roeck } 544d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_info); 545d560168bSGuenter Roeck 546d560168bSGuenter Roeck /** 5471beeffe4STony Jones * hwmon_device_register - register w/ hwmon 5481236441fSMark M. Hoffman * @dev: the device to register 5491236441fSMark M. Hoffman * 5501beeffe4STony Jones * hwmon_device_unregister() must be called when the device is no 5511236441fSMark M. Hoffman * longer needed. 5521236441fSMark M. Hoffman * 5531beeffe4STony Jones * Returns the pointer to the new device. 5541236441fSMark M. Hoffman */ 5551beeffe4STony Jones struct device *hwmon_device_register(struct device *dev) 5561236441fSMark M. Hoffman { 557bab2243cSGuenter Roeck return hwmon_device_register_with_groups(dev, NULL, NULL, NULL); 5581236441fSMark M. Hoffman } 559839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_register); 5601236441fSMark M. Hoffman 5611236441fSMark M. Hoffman /** 5621236441fSMark M. Hoffman * hwmon_device_unregister - removes the previously registered class device 5631236441fSMark M. Hoffman * 5641beeffe4STony Jones * @dev: the class device to destroy 5651236441fSMark M. Hoffman */ 5661beeffe4STony Jones void hwmon_device_unregister(struct device *dev) 5671236441fSMark M. Hoffman { 5681236441fSMark M. Hoffman int id; 5691236441fSMark M. Hoffman 570739cf3a2SKay Sievers if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) { 5711beeffe4STony Jones device_unregister(dev); 5724ca5f468SJonathan Cameron ida_simple_remove(&hwmon_ida, id); 5731236441fSMark M. Hoffman } else 5741beeffe4STony Jones dev_dbg(dev->parent, 5751236441fSMark M. Hoffman "hwmon_device_unregister() failed: bad class ID!\n"); 5761236441fSMark M. Hoffman } 577839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_unregister); 5781236441fSMark M. Hoffman 57974188cbaSGuenter Roeck static void devm_hwmon_release(struct device *dev, void *res) 58074188cbaSGuenter Roeck { 58174188cbaSGuenter Roeck struct device *hwdev = *(struct device **)res; 58274188cbaSGuenter Roeck 58374188cbaSGuenter Roeck hwmon_device_unregister(hwdev); 58474188cbaSGuenter Roeck } 58574188cbaSGuenter Roeck 58674188cbaSGuenter Roeck /** 58774188cbaSGuenter Roeck * devm_hwmon_device_register_with_groups - register w/ hwmon 58874188cbaSGuenter Roeck * @dev: the parent device 58974188cbaSGuenter Roeck * @name: hwmon name attribute 59074188cbaSGuenter Roeck * @drvdata: driver data to attach to created device 59174188cbaSGuenter Roeck * @groups: List of attribute groups to create 59274188cbaSGuenter Roeck * 59374188cbaSGuenter Roeck * Returns the pointer to the new device. The new device is automatically 59474188cbaSGuenter Roeck * unregistered with the parent device. 59574188cbaSGuenter Roeck */ 59674188cbaSGuenter Roeck struct device * 59774188cbaSGuenter Roeck devm_hwmon_device_register_with_groups(struct device *dev, const char *name, 59874188cbaSGuenter Roeck void *drvdata, 59974188cbaSGuenter Roeck const struct attribute_group **groups) 60074188cbaSGuenter Roeck { 60174188cbaSGuenter Roeck struct device **ptr, *hwdev; 60274188cbaSGuenter Roeck 60374188cbaSGuenter Roeck if (!dev) 60474188cbaSGuenter Roeck return ERR_PTR(-EINVAL); 60574188cbaSGuenter Roeck 60674188cbaSGuenter Roeck ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); 60774188cbaSGuenter Roeck if (!ptr) 60874188cbaSGuenter Roeck return ERR_PTR(-ENOMEM); 60974188cbaSGuenter Roeck 61074188cbaSGuenter Roeck hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups); 61174188cbaSGuenter Roeck if (IS_ERR(hwdev)) 61274188cbaSGuenter Roeck goto error; 61374188cbaSGuenter Roeck 61474188cbaSGuenter Roeck *ptr = hwdev; 61574188cbaSGuenter Roeck devres_add(dev, ptr); 61674188cbaSGuenter Roeck return hwdev; 61774188cbaSGuenter Roeck 61874188cbaSGuenter Roeck error: 61974188cbaSGuenter Roeck devres_free(ptr); 62074188cbaSGuenter Roeck return hwdev; 62174188cbaSGuenter Roeck } 62274188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups); 62374188cbaSGuenter Roeck 624d560168bSGuenter Roeck /** 625d560168bSGuenter Roeck * devm_hwmon_device_register_with_info - register w/ hwmon 626d560168bSGuenter Roeck * @dev: the parent device 627d560168bSGuenter Roeck * @name: hwmon name attribute 628d560168bSGuenter Roeck * @drvdata: driver data to attach to created device 629d560168bSGuenter Roeck * @info: Pointer to hwmon chip information 630d560168bSGuenter Roeck * @groups - pointer to list of driver specific attribute groups 631d560168bSGuenter Roeck * 632d560168bSGuenter Roeck * Returns the pointer to the new device. The new device is automatically 633d560168bSGuenter Roeck * unregistered with the parent device. 634d560168bSGuenter Roeck */ 635d560168bSGuenter Roeck struct device * 636d560168bSGuenter Roeck devm_hwmon_device_register_with_info(struct device *dev, const char *name, 637d560168bSGuenter Roeck void *drvdata, 638d560168bSGuenter Roeck const struct hwmon_chip_info *chip, 639d560168bSGuenter Roeck const struct attribute_group **groups) 640d560168bSGuenter Roeck { 641d560168bSGuenter Roeck struct device **ptr, *hwdev; 642d560168bSGuenter Roeck 643d560168bSGuenter Roeck if (!dev) 644d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 645d560168bSGuenter Roeck 646d560168bSGuenter Roeck ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); 647d560168bSGuenter Roeck if (!ptr) 648d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 649d560168bSGuenter Roeck 650d560168bSGuenter Roeck hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip, 651d560168bSGuenter Roeck groups); 652d560168bSGuenter Roeck if (IS_ERR(hwdev)) 653d560168bSGuenter Roeck goto error; 654d560168bSGuenter Roeck 655d560168bSGuenter Roeck *ptr = hwdev; 656d560168bSGuenter Roeck devres_add(dev, ptr); 657d560168bSGuenter Roeck 658d560168bSGuenter Roeck return hwdev; 659d560168bSGuenter Roeck 660d560168bSGuenter Roeck error: 661d560168bSGuenter Roeck devres_free(ptr); 662d560168bSGuenter Roeck return hwdev; 663d560168bSGuenter Roeck } 664d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info); 665d560168bSGuenter Roeck 66674188cbaSGuenter Roeck static int devm_hwmon_match(struct device *dev, void *res, void *data) 66774188cbaSGuenter Roeck { 66874188cbaSGuenter Roeck struct device **hwdev = res; 66974188cbaSGuenter Roeck 67074188cbaSGuenter Roeck return *hwdev == data; 67174188cbaSGuenter Roeck } 67274188cbaSGuenter Roeck 67374188cbaSGuenter Roeck /** 67474188cbaSGuenter Roeck * devm_hwmon_device_unregister - removes a previously registered hwmon device 67574188cbaSGuenter Roeck * 67674188cbaSGuenter Roeck * @dev: the parent device of the device to unregister 67774188cbaSGuenter Roeck */ 67874188cbaSGuenter Roeck void devm_hwmon_device_unregister(struct device *dev) 67974188cbaSGuenter Roeck { 68074188cbaSGuenter Roeck WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev)); 68174188cbaSGuenter Roeck } 68274188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister); 68374188cbaSGuenter Roeck 6842958b1ecSJean Delvare static void __init hwmon_pci_quirks(void) 6852958b1ecSJean Delvare { 6862958b1ecSJean Delvare #if defined CONFIG_X86 && defined CONFIG_PCI 6872958b1ecSJean Delvare struct pci_dev *sb; 6882958b1ecSJean Delvare u16 base; 6892958b1ecSJean Delvare u8 enable; 6902958b1ecSJean Delvare 6912958b1ecSJean Delvare /* Open access to 0x295-0x296 on MSI MS-7031 */ 6922958b1ecSJean Delvare sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL); 693d6dab7ddSJean Delvare if (sb) { 694d6dab7ddSJean Delvare if (sb->subsystem_vendor == 0x1462 && /* MSI */ 695d6dab7ddSJean Delvare sb->subsystem_device == 0x0031) { /* MS-7031 */ 6962958b1ecSJean Delvare pci_read_config_byte(sb, 0x48, &enable); 6972958b1ecSJean Delvare pci_read_config_word(sb, 0x64, &base); 6982958b1ecSJean Delvare 6992958b1ecSJean Delvare if (base == 0 && !(enable & BIT(2))) { 7002958b1ecSJean Delvare dev_info(&sb->dev, 7012958b1ecSJean Delvare "Opening wide generic port at 0x295\n"); 7022958b1ecSJean Delvare pci_write_config_word(sb, 0x64, 0x295); 703d6dab7ddSJean Delvare pci_write_config_byte(sb, 0x48, 704d6dab7ddSJean Delvare enable | BIT(2)); 7052958b1ecSJean Delvare } 7062958b1ecSJean Delvare } 707d6dab7ddSJean Delvare pci_dev_put(sb); 708d6dab7ddSJean Delvare } 7092958b1ecSJean Delvare #endif 7102958b1ecSJean Delvare } 7112958b1ecSJean Delvare 7121236441fSMark M. Hoffman static int __init hwmon_init(void) 7131236441fSMark M. Hoffman { 714bab2243cSGuenter Roeck int err; 715bab2243cSGuenter Roeck 7162958b1ecSJean Delvare hwmon_pci_quirks(); 7172958b1ecSJean Delvare 718bab2243cSGuenter Roeck err = class_register(&hwmon_class); 719bab2243cSGuenter Roeck if (err) { 720bab2243cSGuenter Roeck pr_err("couldn't register hwmon sysfs class\n"); 721bab2243cSGuenter Roeck return err; 7221236441fSMark M. Hoffman } 7231236441fSMark M. Hoffman return 0; 7241236441fSMark M. Hoffman } 7251236441fSMark M. Hoffman 7261236441fSMark M. Hoffman static void __exit hwmon_exit(void) 7271236441fSMark M. Hoffman { 728bab2243cSGuenter Roeck class_unregister(&hwmon_class); 7291236441fSMark M. Hoffman } 7301236441fSMark M. Hoffman 73137f54ee5SDavid Brownell subsys_initcall(hwmon_init); 7321236441fSMark M. Hoffman module_exit(hwmon_exit); 7331236441fSMark M. Hoffman 7341236441fSMark M. Hoffman MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>"); 7351236441fSMark M. Hoffman MODULE_DESCRIPTION("hardware monitoring sysfs/class support"); 7361236441fSMark M. Hoffman MODULE_LICENSE("GPL"); 7371236441fSMark M. Hoffman 738