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 11186430c1aSGuenter Roeck /* 11286430c1aSGuenter Roeck * The complex conditional is necessary to avoid a cyclic dependency 11386430c1aSGuenter Roeck * between hwmon and thermal_sys modules. 11486430c1aSGuenter Roeck */ 11586430c1aSGuenter Roeck #if IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF) && \ 11686430c1aSGuenter Roeck (!defined(CONFIG_THERMAL_HWMON) || \ 11786430c1aSGuenter Roeck !(defined(MODULE) && IS_MODULE(CONFIG_THERMAL))) 118d560168bSGuenter Roeck static int hwmon_thermal_get_temp(void *data, int *temp) 119d560168bSGuenter Roeck { 120d560168bSGuenter Roeck struct hwmon_thermal_data *tdata = data; 121d560168bSGuenter Roeck struct hwmon_device *hwdev = tdata->hwdev; 122d560168bSGuenter Roeck int ret; 123d560168bSGuenter Roeck long t; 124d560168bSGuenter Roeck 125d560168bSGuenter Roeck ret = hwdev->chip->ops->read(&hwdev->dev, hwmon_temp, hwmon_temp_input, 126d560168bSGuenter Roeck tdata->index, &t); 127d560168bSGuenter Roeck if (ret < 0) 128d560168bSGuenter Roeck return ret; 129d560168bSGuenter Roeck 130d560168bSGuenter Roeck *temp = t; 131d560168bSGuenter Roeck 132d560168bSGuenter Roeck return 0; 133d560168bSGuenter Roeck } 134d560168bSGuenter Roeck 135d560168bSGuenter Roeck static struct thermal_zone_of_device_ops hwmon_thermal_ops = { 136d560168bSGuenter Roeck .get_temp = hwmon_thermal_get_temp, 137d560168bSGuenter Roeck }; 138d560168bSGuenter Roeck 139d560168bSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev, 140d560168bSGuenter Roeck struct hwmon_device *hwdev, int index) 141d560168bSGuenter Roeck { 142d560168bSGuenter Roeck struct hwmon_thermal_data *tdata; 143d560168bSGuenter Roeck 144d560168bSGuenter Roeck tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL); 145d560168bSGuenter Roeck if (!tdata) 146d560168bSGuenter Roeck return -ENOMEM; 147d560168bSGuenter Roeck 148d560168bSGuenter Roeck tdata->hwdev = hwdev; 149d560168bSGuenter Roeck tdata->index = index; 150d560168bSGuenter Roeck 151d560168bSGuenter Roeck devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata, 152d560168bSGuenter Roeck &hwmon_thermal_ops); 153d560168bSGuenter Roeck 154d560168bSGuenter Roeck return 0; 155d560168bSGuenter Roeck } 156d560168bSGuenter Roeck #else 157d560168bSGuenter Roeck static int hwmon_thermal_add_sensor(struct device *dev, 158d560168bSGuenter Roeck struct hwmon_device *hwdev, int index) 159d560168bSGuenter Roeck { 160d560168bSGuenter Roeck return 0; 161d560168bSGuenter Roeck } 16286430c1aSGuenter Roeck #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */ 163d560168bSGuenter Roeck 164d560168bSGuenter Roeck /* sysfs attribute management */ 165d560168bSGuenter Roeck 166d560168bSGuenter Roeck static ssize_t hwmon_attr_show(struct device *dev, 167d560168bSGuenter Roeck struct device_attribute *devattr, char *buf) 168d560168bSGuenter Roeck { 169d560168bSGuenter Roeck struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); 170d560168bSGuenter Roeck long val; 171d560168bSGuenter Roeck int ret; 172d560168bSGuenter Roeck 173d560168bSGuenter Roeck ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index, 174d560168bSGuenter Roeck &val); 175d560168bSGuenter Roeck if (ret < 0) 176d560168bSGuenter Roeck return ret; 177d560168bSGuenter Roeck 178d560168bSGuenter Roeck return sprintf(buf, "%ld\n", val); 179d560168bSGuenter Roeck } 180d560168bSGuenter Roeck 181d560168bSGuenter Roeck static ssize_t hwmon_attr_store(struct device *dev, 182d560168bSGuenter Roeck struct device_attribute *devattr, 183d560168bSGuenter Roeck const char *buf, size_t count) 184d560168bSGuenter Roeck { 185d560168bSGuenter Roeck struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); 186d560168bSGuenter Roeck long val; 187d560168bSGuenter Roeck int ret; 188d560168bSGuenter Roeck 189d560168bSGuenter Roeck ret = kstrtol(buf, 10, &val); 190d560168bSGuenter Roeck if (ret < 0) 191d560168bSGuenter Roeck return ret; 192d560168bSGuenter Roeck 193d560168bSGuenter Roeck ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index, 194d560168bSGuenter Roeck val); 195d560168bSGuenter Roeck if (ret < 0) 196d560168bSGuenter Roeck return ret; 197d560168bSGuenter Roeck 198d560168bSGuenter Roeck return count; 199d560168bSGuenter Roeck } 200d560168bSGuenter Roeck 201d560168bSGuenter Roeck static int hwmon_attr_base(enum hwmon_sensor_types type) 202d560168bSGuenter Roeck { 203d560168bSGuenter Roeck if (type == hwmon_in) 204d560168bSGuenter Roeck return 0; 205d560168bSGuenter Roeck return 1; 206d560168bSGuenter Roeck } 207d560168bSGuenter Roeck 208d560168bSGuenter Roeck static struct attribute *hwmon_genattr(struct device *dev, 209d560168bSGuenter Roeck const void *drvdata, 210d560168bSGuenter Roeck enum hwmon_sensor_types type, 211d560168bSGuenter Roeck u32 attr, 212d560168bSGuenter Roeck int index, 213d560168bSGuenter Roeck const char *template, 214d560168bSGuenter Roeck const struct hwmon_ops *ops) 215d560168bSGuenter Roeck { 216d560168bSGuenter Roeck struct hwmon_device_attribute *hattr; 217d560168bSGuenter Roeck struct device_attribute *dattr; 218d560168bSGuenter Roeck struct attribute *a; 219d560168bSGuenter Roeck umode_t mode; 220d560168bSGuenter Roeck char *name; 221d560168bSGuenter Roeck 222d560168bSGuenter Roeck /* The attribute is invisible if there is no template string */ 223d560168bSGuenter Roeck if (!template) 224d560168bSGuenter Roeck return ERR_PTR(-ENOENT); 225d560168bSGuenter Roeck 226d560168bSGuenter Roeck mode = ops->is_visible(drvdata, type, attr, index); 227d560168bSGuenter Roeck if (!mode) 228d560168bSGuenter Roeck return ERR_PTR(-ENOENT); 229d560168bSGuenter Roeck 230d560168bSGuenter Roeck if ((mode & S_IRUGO) && !ops->read) 231d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 232d560168bSGuenter Roeck if ((mode & S_IWUGO) && !ops->write) 233d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 234d560168bSGuenter Roeck 235d560168bSGuenter Roeck if (type == hwmon_chip) { 236d560168bSGuenter Roeck name = (char *)template; 237d560168bSGuenter Roeck } else { 238d560168bSGuenter Roeck name = devm_kzalloc(dev, strlen(template) + 16, GFP_KERNEL); 239d560168bSGuenter Roeck if (!name) 240d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 241d560168bSGuenter Roeck scnprintf(name, strlen(template) + 16, template, 242d560168bSGuenter Roeck index + hwmon_attr_base(type)); 243d560168bSGuenter Roeck } 244d560168bSGuenter Roeck 245d560168bSGuenter Roeck hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL); 246d560168bSGuenter Roeck if (!hattr) 247d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 248d560168bSGuenter Roeck 249d560168bSGuenter Roeck hattr->type = type; 250d560168bSGuenter Roeck hattr->attr = attr; 251d560168bSGuenter Roeck hattr->index = index; 252d560168bSGuenter Roeck hattr->ops = ops; 253d560168bSGuenter Roeck 254d560168bSGuenter Roeck dattr = &hattr->dev_attr; 255d560168bSGuenter Roeck dattr->show = hwmon_attr_show; 256d560168bSGuenter Roeck dattr->store = hwmon_attr_store; 257d560168bSGuenter Roeck 258d560168bSGuenter Roeck a = &dattr->attr; 259d560168bSGuenter Roeck sysfs_attr_init(a); 260d560168bSGuenter Roeck a->name = name; 261d560168bSGuenter Roeck a->mode = mode; 262d560168bSGuenter Roeck 263d560168bSGuenter Roeck return a; 264d560168bSGuenter Roeck } 265d560168bSGuenter Roeck 266d560168bSGuenter Roeck static const char * const hwmon_chip_attr_templates[] = { 267d560168bSGuenter Roeck [hwmon_chip_temp_reset_history] = "temp_reset_history", 26800d616cfSGuenter Roeck [hwmon_chip_in_reset_history] = "in_reset_history", 2699b26947cSGuenter Roeck [hwmon_chip_curr_reset_history] = "curr_reset_history", 270b308f5c7SGuenter Roeck [hwmon_chip_power_reset_history] = "power_reset_history", 271d560168bSGuenter Roeck [hwmon_chip_update_interval] = "update_interval", 272d560168bSGuenter Roeck [hwmon_chip_alarms] = "alarms", 273d560168bSGuenter Roeck }; 274d560168bSGuenter Roeck 275d560168bSGuenter Roeck static const char * const hwmon_temp_attr_templates[] = { 276d560168bSGuenter Roeck [hwmon_temp_input] = "temp%d_input", 277d560168bSGuenter Roeck [hwmon_temp_type] = "temp%d_type", 278d560168bSGuenter Roeck [hwmon_temp_lcrit] = "temp%d_lcrit", 279d560168bSGuenter Roeck [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst", 280d560168bSGuenter Roeck [hwmon_temp_min] = "temp%d_min", 281d560168bSGuenter Roeck [hwmon_temp_min_hyst] = "temp%d_min_hyst", 282d560168bSGuenter Roeck [hwmon_temp_max] = "temp%d_max", 283d560168bSGuenter Roeck [hwmon_temp_max_hyst] = "temp%d_max_hyst", 284d560168bSGuenter Roeck [hwmon_temp_crit] = "temp%d_crit", 285d560168bSGuenter Roeck [hwmon_temp_crit_hyst] = "temp%d_crit_hyst", 286d560168bSGuenter Roeck [hwmon_temp_emergency] = "temp%d_emergency", 287d560168bSGuenter Roeck [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst", 288d560168bSGuenter Roeck [hwmon_temp_alarm] = "temp%d_alarm", 289d560168bSGuenter Roeck [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm", 290d560168bSGuenter Roeck [hwmon_temp_min_alarm] = "temp%d_min_alarm", 291d560168bSGuenter Roeck [hwmon_temp_max_alarm] = "temp%d_max_alarm", 292d560168bSGuenter Roeck [hwmon_temp_crit_alarm] = "temp%d_crit_alarm", 293d560168bSGuenter Roeck [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm", 294d560168bSGuenter Roeck [hwmon_temp_fault] = "temp%d_fault", 295d560168bSGuenter Roeck [hwmon_temp_offset] = "temp%d_offset", 296d560168bSGuenter Roeck [hwmon_temp_label] = "temp%d_label", 297d560168bSGuenter Roeck [hwmon_temp_lowest] = "temp%d_lowest", 298d560168bSGuenter Roeck [hwmon_temp_highest] = "temp%d_highest", 299d560168bSGuenter Roeck [hwmon_temp_reset_history] = "temp%d_reset_history", 300d560168bSGuenter Roeck }; 301d560168bSGuenter Roeck 30200d616cfSGuenter Roeck static const char * const hwmon_in_attr_templates[] = { 30300d616cfSGuenter Roeck [hwmon_in_input] = "in%d_input", 30400d616cfSGuenter Roeck [hwmon_in_min] = "in%d_min", 30500d616cfSGuenter Roeck [hwmon_in_max] = "in%d_max", 30600d616cfSGuenter Roeck [hwmon_in_lcrit] = "in%d_lcrit", 30700d616cfSGuenter Roeck [hwmon_in_crit] = "in%d_crit", 30800d616cfSGuenter Roeck [hwmon_in_average] = "in%d_average", 30900d616cfSGuenter Roeck [hwmon_in_lowest] = "in%d_lowest", 31000d616cfSGuenter Roeck [hwmon_in_highest] = "in%d_highest", 31100d616cfSGuenter Roeck [hwmon_in_reset_history] = "in%d_reset_history", 31200d616cfSGuenter Roeck [hwmon_in_label] = "in%d_label", 31300d616cfSGuenter Roeck [hwmon_in_alarm] = "in%d_alarm", 31400d616cfSGuenter Roeck [hwmon_in_min_alarm] = "in%d_min_alarm", 31500d616cfSGuenter Roeck [hwmon_in_max_alarm] = "in%d_max_alarm", 31600d616cfSGuenter Roeck [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm", 31700d616cfSGuenter Roeck [hwmon_in_crit_alarm] = "in%d_crit_alarm", 31800d616cfSGuenter Roeck }; 31900d616cfSGuenter Roeck 3209b26947cSGuenter Roeck static const char * const hwmon_curr_attr_templates[] = { 3219b26947cSGuenter Roeck [hwmon_curr_input] = "curr%d_input", 3229b26947cSGuenter Roeck [hwmon_curr_min] = "curr%d_min", 3239b26947cSGuenter Roeck [hwmon_curr_max] = "curr%d_max", 3249b26947cSGuenter Roeck [hwmon_curr_lcrit] = "curr%d_lcrit", 3259b26947cSGuenter Roeck [hwmon_curr_crit] = "curr%d_crit", 3269b26947cSGuenter Roeck [hwmon_curr_average] = "curr%d_average", 3279b26947cSGuenter Roeck [hwmon_curr_lowest] = "curr%d_lowest", 3289b26947cSGuenter Roeck [hwmon_curr_highest] = "curr%d_highest", 3299b26947cSGuenter Roeck [hwmon_curr_reset_history] = "curr%d_reset_history", 3309b26947cSGuenter Roeck [hwmon_curr_label] = "curr%d_label", 3319b26947cSGuenter Roeck [hwmon_curr_alarm] = "curr%d_alarm", 3329b26947cSGuenter Roeck [hwmon_curr_min_alarm] = "curr%d_min_alarm", 3339b26947cSGuenter Roeck [hwmon_curr_max_alarm] = "curr%d_max_alarm", 3349b26947cSGuenter Roeck [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm", 3359b26947cSGuenter Roeck [hwmon_curr_crit_alarm] = "curr%d_crit_alarm", 3369b26947cSGuenter Roeck }; 3379b26947cSGuenter Roeck 338b308f5c7SGuenter Roeck static const char * const hwmon_power_attr_templates[] = { 339b308f5c7SGuenter Roeck [hwmon_power_average] = "power%d_average", 340b308f5c7SGuenter Roeck [hwmon_power_average_interval] = "power%d_average_interval", 341b308f5c7SGuenter Roeck [hwmon_power_average_interval_max] = "power%d_interval_max", 342b308f5c7SGuenter Roeck [hwmon_power_average_interval_min] = "power%d_interval_min", 343b308f5c7SGuenter Roeck [hwmon_power_average_highest] = "power%d_average_highest", 344b308f5c7SGuenter Roeck [hwmon_power_average_lowest] = "power%d_average_lowest", 345b308f5c7SGuenter Roeck [hwmon_power_average_max] = "power%d_average_max", 346b308f5c7SGuenter Roeck [hwmon_power_average_min] = "power%d_average_min", 347b308f5c7SGuenter Roeck [hwmon_power_input] = "power%d_input", 348b308f5c7SGuenter Roeck [hwmon_power_input_highest] = "power%d_input_highest", 349b308f5c7SGuenter Roeck [hwmon_power_input_lowest] = "power%d_input_lowest", 350b308f5c7SGuenter Roeck [hwmon_power_reset_history] = "power%d_reset_history", 351b308f5c7SGuenter Roeck [hwmon_power_accuracy] = "power%d_accuracy", 352b308f5c7SGuenter Roeck [hwmon_power_cap] = "power%d_cap", 353b308f5c7SGuenter Roeck [hwmon_power_cap_hyst] = "power%d_cap_hyst", 354b308f5c7SGuenter Roeck [hwmon_power_cap_max] = "power%d_cap_max", 355b308f5c7SGuenter Roeck [hwmon_power_cap_min] = "power%d_cap_min", 356b308f5c7SGuenter Roeck [hwmon_power_max] = "power%d_max", 357b308f5c7SGuenter Roeck [hwmon_power_crit] = "power%d_crit", 358b308f5c7SGuenter Roeck [hwmon_power_label] = "power%d_label", 359b308f5c7SGuenter Roeck [hwmon_power_alarm] = "power%d_alarm", 360b308f5c7SGuenter Roeck [hwmon_power_cap_alarm] = "power%d_cap_alarm", 361b308f5c7SGuenter Roeck [hwmon_power_max_alarm] = "power%d_max_alarm", 362b308f5c7SGuenter Roeck [hwmon_power_crit_alarm] = "power%d_crit_alarm", 363b308f5c7SGuenter Roeck }; 364b308f5c7SGuenter Roeck 3656bfcca44SGuenter Roeck static const char * const hwmon_energy_attr_templates[] = { 3666bfcca44SGuenter Roeck [hwmon_energy_input] = "energy%d_input", 3676bfcca44SGuenter Roeck [hwmon_energy_label] = "energy%d_label", 3686bfcca44SGuenter Roeck }; 3696bfcca44SGuenter Roeck 3706bfcca44SGuenter Roeck static const char * const hwmon_humidity_attr_templates[] = { 3716bfcca44SGuenter Roeck [hwmon_humidity_input] = "humidity%d_input", 3726bfcca44SGuenter Roeck [hwmon_humidity_label] = "humidity%d_label", 3736bfcca44SGuenter Roeck [hwmon_humidity_min] = "humidity%d_min", 3746bfcca44SGuenter Roeck [hwmon_humidity_min_hyst] = "humidity%d_min_hyst", 3756bfcca44SGuenter Roeck [hwmon_humidity_max] = "humidity%d_max", 3766bfcca44SGuenter Roeck [hwmon_humidity_max_hyst] = "humidity%d_max_hyst", 3776bfcca44SGuenter Roeck [hwmon_humidity_alarm] = "humidity%d_alarm", 3786bfcca44SGuenter Roeck [hwmon_humidity_fault] = "humidity%d_fault", 3796bfcca44SGuenter Roeck }; 3806bfcca44SGuenter Roeck 3818faee73fSGuenter Roeck static const char * const hwmon_fan_attr_templates[] = { 3828faee73fSGuenter Roeck [hwmon_fan_input] = "fan%d_input", 3838faee73fSGuenter Roeck [hwmon_fan_label] = "fan%d_label", 3848faee73fSGuenter Roeck [hwmon_fan_min] = "fan%d_min", 3858faee73fSGuenter Roeck [hwmon_fan_max] = "fan%d_max", 3868faee73fSGuenter Roeck [hwmon_fan_div] = "fan%d_div", 3878faee73fSGuenter Roeck [hwmon_fan_pulses] = "fan%d_pulses", 3888faee73fSGuenter Roeck [hwmon_fan_target] = "fan%d_target", 3898faee73fSGuenter Roeck [hwmon_fan_alarm] = "fan%d_alarm", 3908faee73fSGuenter Roeck [hwmon_fan_min_alarm] = "fan%d_min_alarm", 3918faee73fSGuenter Roeck [hwmon_fan_max_alarm] = "fan%d_max_alarm", 3928faee73fSGuenter Roeck [hwmon_fan_fault] = "fan%d_fault", 3938faee73fSGuenter Roeck }; 3948faee73fSGuenter Roeck 395f9f7bb3aSGuenter Roeck static const char * const hwmon_pwm_attr_templates[] = { 396f9f7bb3aSGuenter Roeck [hwmon_pwm_input] = "pwm%d", 397f9f7bb3aSGuenter Roeck [hwmon_pwm_enable] = "pwm%d_enable", 398f9f7bb3aSGuenter Roeck [hwmon_pwm_mode] = "pwm%d_mode", 399f9f7bb3aSGuenter Roeck [hwmon_pwm_freq] = "pwm%d_freq", 400f9f7bb3aSGuenter Roeck }; 401f9f7bb3aSGuenter Roeck 402d560168bSGuenter Roeck static const char * const *__templates[] = { 403d560168bSGuenter Roeck [hwmon_chip] = hwmon_chip_attr_templates, 404d560168bSGuenter Roeck [hwmon_temp] = hwmon_temp_attr_templates, 40500d616cfSGuenter Roeck [hwmon_in] = hwmon_in_attr_templates, 4069b26947cSGuenter Roeck [hwmon_curr] = hwmon_curr_attr_templates, 407b308f5c7SGuenter Roeck [hwmon_power] = hwmon_power_attr_templates, 4086bfcca44SGuenter Roeck [hwmon_energy] = hwmon_energy_attr_templates, 4096bfcca44SGuenter Roeck [hwmon_humidity] = hwmon_humidity_attr_templates, 4108faee73fSGuenter Roeck [hwmon_fan] = hwmon_fan_attr_templates, 411f9f7bb3aSGuenter Roeck [hwmon_pwm] = hwmon_pwm_attr_templates, 412d560168bSGuenter Roeck }; 413d560168bSGuenter Roeck 414d560168bSGuenter Roeck static const int __templates_size[] = { 415d560168bSGuenter Roeck [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attr_templates), 416d560168bSGuenter Roeck [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates), 41700d616cfSGuenter Roeck [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates), 4189b26947cSGuenter Roeck [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates), 419b308f5c7SGuenter Roeck [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates), 4206bfcca44SGuenter Roeck [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates), 4216bfcca44SGuenter Roeck [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates), 4228faee73fSGuenter Roeck [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates), 423f9f7bb3aSGuenter Roeck [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates), 424d560168bSGuenter Roeck }; 425d560168bSGuenter Roeck 426d560168bSGuenter Roeck static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info) 427d560168bSGuenter Roeck { 428d560168bSGuenter Roeck int i, n; 429d560168bSGuenter Roeck 430d560168bSGuenter Roeck for (i = n = 0; info->config[i]; i++) 431d560168bSGuenter Roeck n += hweight32(info->config[i]); 432d560168bSGuenter Roeck 433d560168bSGuenter Roeck return n; 434d560168bSGuenter Roeck } 435d560168bSGuenter Roeck 436d560168bSGuenter Roeck static int hwmon_genattrs(struct device *dev, 437d560168bSGuenter Roeck const void *drvdata, 438d560168bSGuenter Roeck struct attribute **attrs, 439d560168bSGuenter Roeck const struct hwmon_ops *ops, 440d560168bSGuenter Roeck const struct hwmon_channel_info *info) 441d560168bSGuenter Roeck { 442d560168bSGuenter Roeck const char * const *templates; 443d560168bSGuenter Roeck int template_size; 444d560168bSGuenter Roeck int i, aindex = 0; 445d560168bSGuenter Roeck 446d560168bSGuenter Roeck if (info->type >= ARRAY_SIZE(__templates)) 447d560168bSGuenter Roeck return -EINVAL; 448d560168bSGuenter Roeck 449d560168bSGuenter Roeck templates = __templates[info->type]; 450d560168bSGuenter Roeck template_size = __templates_size[info->type]; 451d560168bSGuenter Roeck 452d560168bSGuenter Roeck for (i = 0; info->config[i]; i++) { 453d560168bSGuenter Roeck u32 attr_mask = info->config[i]; 454d560168bSGuenter Roeck u32 attr; 455d560168bSGuenter Roeck 456d560168bSGuenter Roeck while (attr_mask) { 457d560168bSGuenter Roeck struct attribute *a; 458d560168bSGuenter Roeck 459d560168bSGuenter Roeck attr = __ffs(attr_mask); 460d560168bSGuenter Roeck attr_mask &= ~BIT(attr); 461d560168bSGuenter Roeck if (attr >= template_size) 462d560168bSGuenter Roeck return -EINVAL; 463d560168bSGuenter Roeck a = hwmon_genattr(dev, drvdata, info->type, attr, i, 464d560168bSGuenter Roeck templates[attr], ops); 465d560168bSGuenter Roeck if (IS_ERR(a)) { 466d560168bSGuenter Roeck if (PTR_ERR(a) != -ENOENT) 467d560168bSGuenter Roeck return PTR_ERR(a); 468d560168bSGuenter Roeck continue; 469d560168bSGuenter Roeck } 470d560168bSGuenter Roeck attrs[aindex++] = a; 471d560168bSGuenter Roeck } 472d560168bSGuenter Roeck } 473d560168bSGuenter Roeck return aindex; 474d560168bSGuenter Roeck } 475d560168bSGuenter Roeck 476d560168bSGuenter Roeck static struct attribute ** 477d560168bSGuenter Roeck __hwmon_create_attrs(struct device *dev, const void *drvdata, 478d560168bSGuenter Roeck const struct hwmon_chip_info *chip) 479d560168bSGuenter Roeck { 480d560168bSGuenter Roeck int ret, i, aindex = 0, nattrs = 0; 481d560168bSGuenter Roeck struct attribute **attrs; 482d560168bSGuenter Roeck 483d560168bSGuenter Roeck for (i = 0; chip->info[i]; i++) 484d560168bSGuenter Roeck nattrs += hwmon_num_channel_attrs(chip->info[i]); 485d560168bSGuenter Roeck 486d560168bSGuenter Roeck if (nattrs == 0) 487d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 488d560168bSGuenter Roeck 489d560168bSGuenter Roeck attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL); 490d560168bSGuenter Roeck if (!attrs) 491d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 492d560168bSGuenter Roeck 493d560168bSGuenter Roeck for (i = 0; chip->info[i]; i++) { 494d560168bSGuenter Roeck ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops, 495d560168bSGuenter Roeck chip->info[i]); 496d560168bSGuenter Roeck if (ret < 0) 497d560168bSGuenter Roeck return ERR_PTR(ret); 498d560168bSGuenter Roeck aindex += ret; 499d560168bSGuenter Roeck } 500d560168bSGuenter Roeck 501d560168bSGuenter Roeck return attrs; 502d560168bSGuenter Roeck } 503d560168bSGuenter Roeck 504d560168bSGuenter Roeck static struct device * 505d560168bSGuenter Roeck __hwmon_device_register(struct device *dev, const char *name, void *drvdata, 506d560168bSGuenter Roeck const struct hwmon_chip_info *chip, 507d560168bSGuenter Roeck const struct attribute_group **groups) 508d560168bSGuenter Roeck { 509d560168bSGuenter Roeck struct hwmon_device *hwdev; 510d560168bSGuenter Roeck struct device *hdev; 511d560168bSGuenter Roeck int i, j, err, id; 512d560168bSGuenter Roeck 513d560168bSGuenter Roeck /* Do not accept invalid characters in hwmon name attribute */ 514d560168bSGuenter Roeck if (name && (!strlen(name) || strpbrk(name, "-* \t\n"))) 515d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 516d560168bSGuenter Roeck 517d560168bSGuenter Roeck id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL); 518d560168bSGuenter Roeck if (id < 0) 519d560168bSGuenter Roeck return ERR_PTR(id); 520d560168bSGuenter Roeck 521d560168bSGuenter Roeck hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL); 522d560168bSGuenter Roeck if (hwdev == NULL) { 523d560168bSGuenter Roeck err = -ENOMEM; 524d560168bSGuenter Roeck goto ida_remove; 525d560168bSGuenter Roeck } 526d560168bSGuenter Roeck 527d560168bSGuenter Roeck hdev = &hwdev->dev; 528d560168bSGuenter Roeck 529d560168bSGuenter Roeck if (chip && chip->ops->is_visible) { 530d560168bSGuenter Roeck struct attribute **attrs; 531d560168bSGuenter Roeck int ngroups = 2; 532d560168bSGuenter Roeck 533d560168bSGuenter Roeck if (groups) 534d560168bSGuenter Roeck for (i = 0; groups[i]; i++) 535d560168bSGuenter Roeck ngroups++; 536d560168bSGuenter Roeck 537d560168bSGuenter Roeck hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups), 538d560168bSGuenter Roeck GFP_KERNEL); 539*38d8ed65SColin Ian King if (!hwdev->groups) { 540*38d8ed65SColin Ian King err = -ENOMEM; 541*38d8ed65SColin Ian King goto free_hwmon; 542*38d8ed65SColin Ian King } 543d560168bSGuenter Roeck 544d560168bSGuenter Roeck attrs = __hwmon_create_attrs(dev, drvdata, chip); 545d560168bSGuenter Roeck if (IS_ERR(attrs)) { 546d560168bSGuenter Roeck err = PTR_ERR(attrs); 547d560168bSGuenter Roeck goto free_hwmon; 548d560168bSGuenter Roeck } 549d560168bSGuenter Roeck 550d560168bSGuenter Roeck hwdev->group.attrs = attrs; 551d560168bSGuenter Roeck ngroups = 0; 552d560168bSGuenter Roeck hwdev->groups[ngroups++] = &hwdev->group; 553d560168bSGuenter Roeck 554d560168bSGuenter Roeck if (groups) { 555d560168bSGuenter Roeck for (i = 0; groups[i]; i++) 556d560168bSGuenter Roeck hwdev->groups[ngroups++] = groups[i]; 557d560168bSGuenter Roeck } 558d560168bSGuenter Roeck 559d560168bSGuenter Roeck hdev->groups = hwdev->groups; 560d560168bSGuenter Roeck } else { 561d560168bSGuenter Roeck hdev->groups = groups; 562d560168bSGuenter Roeck } 563d560168bSGuenter Roeck 564d560168bSGuenter Roeck hwdev->name = name; 565d560168bSGuenter Roeck hdev->class = &hwmon_class; 566d560168bSGuenter Roeck hdev->parent = dev; 567d560168bSGuenter Roeck hdev->of_node = dev ? dev->of_node : NULL; 568d560168bSGuenter Roeck hwdev->chip = chip; 569d560168bSGuenter Roeck dev_set_drvdata(hdev, drvdata); 570d560168bSGuenter Roeck dev_set_name(hdev, HWMON_ID_FORMAT, id); 571d560168bSGuenter Roeck err = device_register(hdev); 572d560168bSGuenter Roeck if (err) 573d560168bSGuenter Roeck goto free_hwmon; 574d560168bSGuenter Roeck 575d560168bSGuenter Roeck if (chip && chip->ops->is_visible && chip->ops->read && 576d560168bSGuenter Roeck chip->info[0]->type == hwmon_chip && 577d560168bSGuenter Roeck (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) { 578d560168bSGuenter Roeck const struct hwmon_channel_info **info = chip->info; 579d560168bSGuenter Roeck 580d560168bSGuenter Roeck for (i = 1; info[i]; i++) { 581d560168bSGuenter Roeck if (info[i]->type != hwmon_temp) 582d560168bSGuenter Roeck continue; 583d560168bSGuenter Roeck 584d560168bSGuenter Roeck for (j = 0; info[i]->config[j]; j++) { 585d560168bSGuenter Roeck if (!chip->ops->is_visible(drvdata, hwmon_temp, 586d560168bSGuenter Roeck hwmon_temp_input, j)) 587d560168bSGuenter Roeck continue; 588d560168bSGuenter Roeck if (info[i]->config[j] & HWMON_T_INPUT) 589d560168bSGuenter Roeck hwmon_thermal_add_sensor(dev, hwdev, j); 590d560168bSGuenter Roeck } 591d560168bSGuenter Roeck } 592d560168bSGuenter Roeck } 593d560168bSGuenter Roeck 594d560168bSGuenter Roeck return hdev; 595d560168bSGuenter Roeck 596d560168bSGuenter Roeck free_hwmon: 597d560168bSGuenter Roeck kfree(hwdev); 598d560168bSGuenter Roeck ida_remove: 599d560168bSGuenter Roeck ida_simple_remove(&hwmon_ida, id); 600d560168bSGuenter Roeck return ERR_PTR(err); 601d560168bSGuenter Roeck } 602d560168bSGuenter Roeck 6031236441fSMark M. Hoffman /** 604bab2243cSGuenter Roeck * hwmon_device_register_with_groups - register w/ hwmon 605bab2243cSGuenter Roeck * @dev: the parent device 606bab2243cSGuenter Roeck * @name: hwmon name attribute 607bab2243cSGuenter Roeck * @drvdata: driver data to attach to created device 608bab2243cSGuenter Roeck * @groups: List of attribute groups to create 609bab2243cSGuenter Roeck * 610bab2243cSGuenter Roeck * hwmon_device_unregister() must be called when the device is no 611bab2243cSGuenter Roeck * longer needed. 612bab2243cSGuenter Roeck * 613bab2243cSGuenter Roeck * Returns the pointer to the new device. 614bab2243cSGuenter Roeck */ 615bab2243cSGuenter Roeck struct device * 616bab2243cSGuenter Roeck hwmon_device_register_with_groups(struct device *dev, const char *name, 617bab2243cSGuenter Roeck void *drvdata, 618bab2243cSGuenter Roeck const struct attribute_group **groups) 619bab2243cSGuenter Roeck { 620d560168bSGuenter Roeck return __hwmon_device_register(dev, name, drvdata, NULL, groups); 621bab2243cSGuenter Roeck } 622bab2243cSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups); 623bab2243cSGuenter Roeck 624bab2243cSGuenter Roeck /** 625d560168bSGuenter Roeck * 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 * hwmon_device_unregister() must be called when the device is no 633d560168bSGuenter Roeck * longer needed. 634d560168bSGuenter Roeck * 635d560168bSGuenter Roeck * Returns the pointer to the new device. 636d560168bSGuenter Roeck */ 637d560168bSGuenter Roeck struct device * 638d560168bSGuenter Roeck hwmon_device_register_with_info(struct device *dev, const char *name, 639d560168bSGuenter Roeck void *drvdata, 640d560168bSGuenter Roeck const struct hwmon_chip_info *chip, 641d560168bSGuenter Roeck const struct attribute_group **groups) 642d560168bSGuenter Roeck { 643d560168bSGuenter Roeck if (chip && (!chip->ops || !chip->info)) 644d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 645d560168bSGuenter Roeck 646d560168bSGuenter Roeck return __hwmon_device_register(dev, name, drvdata, chip, groups); 647d560168bSGuenter Roeck } 648d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(hwmon_device_register_with_info); 649d560168bSGuenter Roeck 650d560168bSGuenter Roeck /** 6511beeffe4STony Jones * hwmon_device_register - register w/ hwmon 6521236441fSMark M. Hoffman * @dev: the device to register 6531236441fSMark M. Hoffman * 6541beeffe4STony Jones * hwmon_device_unregister() must be called when the device is no 6551236441fSMark M. Hoffman * longer needed. 6561236441fSMark M. Hoffman * 6571beeffe4STony Jones * Returns the pointer to the new device. 6581236441fSMark M. Hoffman */ 6591beeffe4STony Jones struct device *hwmon_device_register(struct device *dev) 6601236441fSMark M. Hoffman { 661bab2243cSGuenter Roeck return hwmon_device_register_with_groups(dev, NULL, NULL, NULL); 6621236441fSMark M. Hoffman } 663839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_register); 6641236441fSMark M. Hoffman 6651236441fSMark M. Hoffman /** 6661236441fSMark M. Hoffman * hwmon_device_unregister - removes the previously registered class device 6671236441fSMark M. Hoffman * 6681beeffe4STony Jones * @dev: the class device to destroy 6691236441fSMark M. Hoffman */ 6701beeffe4STony Jones void hwmon_device_unregister(struct device *dev) 6711236441fSMark M. Hoffman { 6721236441fSMark M. Hoffman int id; 6731236441fSMark M. Hoffman 674739cf3a2SKay Sievers if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) { 6751beeffe4STony Jones device_unregister(dev); 6764ca5f468SJonathan Cameron ida_simple_remove(&hwmon_ida, id); 6771236441fSMark M. Hoffman } else 6781beeffe4STony Jones dev_dbg(dev->parent, 6791236441fSMark M. Hoffman "hwmon_device_unregister() failed: bad class ID!\n"); 6801236441fSMark M. Hoffman } 681839a9eefSFrans Meulenbroeks EXPORT_SYMBOL_GPL(hwmon_device_unregister); 6821236441fSMark M. Hoffman 68374188cbaSGuenter Roeck static void devm_hwmon_release(struct device *dev, void *res) 68474188cbaSGuenter Roeck { 68574188cbaSGuenter Roeck struct device *hwdev = *(struct device **)res; 68674188cbaSGuenter Roeck 68774188cbaSGuenter Roeck hwmon_device_unregister(hwdev); 68874188cbaSGuenter Roeck } 68974188cbaSGuenter Roeck 69074188cbaSGuenter Roeck /** 69174188cbaSGuenter Roeck * devm_hwmon_device_register_with_groups - register w/ hwmon 69274188cbaSGuenter Roeck * @dev: the parent device 69374188cbaSGuenter Roeck * @name: hwmon name attribute 69474188cbaSGuenter Roeck * @drvdata: driver data to attach to created device 69574188cbaSGuenter Roeck * @groups: List of attribute groups to create 69674188cbaSGuenter Roeck * 69774188cbaSGuenter Roeck * Returns the pointer to the new device. The new device is automatically 69874188cbaSGuenter Roeck * unregistered with the parent device. 69974188cbaSGuenter Roeck */ 70074188cbaSGuenter Roeck struct device * 70174188cbaSGuenter Roeck devm_hwmon_device_register_with_groups(struct device *dev, const char *name, 70274188cbaSGuenter Roeck void *drvdata, 70374188cbaSGuenter Roeck const struct attribute_group **groups) 70474188cbaSGuenter Roeck { 70574188cbaSGuenter Roeck struct device **ptr, *hwdev; 70674188cbaSGuenter Roeck 70774188cbaSGuenter Roeck if (!dev) 70874188cbaSGuenter Roeck return ERR_PTR(-EINVAL); 70974188cbaSGuenter Roeck 71074188cbaSGuenter Roeck ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); 71174188cbaSGuenter Roeck if (!ptr) 71274188cbaSGuenter Roeck return ERR_PTR(-ENOMEM); 71374188cbaSGuenter Roeck 71474188cbaSGuenter Roeck hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups); 71574188cbaSGuenter Roeck if (IS_ERR(hwdev)) 71674188cbaSGuenter Roeck goto error; 71774188cbaSGuenter Roeck 71874188cbaSGuenter Roeck *ptr = hwdev; 71974188cbaSGuenter Roeck devres_add(dev, ptr); 72074188cbaSGuenter Roeck return hwdev; 72174188cbaSGuenter Roeck 72274188cbaSGuenter Roeck error: 72374188cbaSGuenter Roeck devres_free(ptr); 72474188cbaSGuenter Roeck return hwdev; 72574188cbaSGuenter Roeck } 72674188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups); 72774188cbaSGuenter Roeck 728d560168bSGuenter Roeck /** 729d560168bSGuenter Roeck * devm_hwmon_device_register_with_info - register w/ hwmon 730d560168bSGuenter Roeck * @dev: the parent device 731d560168bSGuenter Roeck * @name: hwmon name attribute 732d560168bSGuenter Roeck * @drvdata: driver data to attach to created device 733d560168bSGuenter Roeck * @info: Pointer to hwmon chip information 734d560168bSGuenter Roeck * @groups - pointer to list of driver specific attribute groups 735d560168bSGuenter Roeck * 736d560168bSGuenter Roeck * Returns the pointer to the new device. The new device is automatically 737d560168bSGuenter Roeck * unregistered with the parent device. 738d560168bSGuenter Roeck */ 739d560168bSGuenter Roeck struct device * 740d560168bSGuenter Roeck devm_hwmon_device_register_with_info(struct device *dev, const char *name, 741d560168bSGuenter Roeck void *drvdata, 742d560168bSGuenter Roeck const struct hwmon_chip_info *chip, 743d560168bSGuenter Roeck const struct attribute_group **groups) 744d560168bSGuenter Roeck { 745d560168bSGuenter Roeck struct device **ptr, *hwdev; 746d560168bSGuenter Roeck 747d560168bSGuenter Roeck if (!dev) 748d560168bSGuenter Roeck return ERR_PTR(-EINVAL); 749d560168bSGuenter Roeck 750d560168bSGuenter Roeck ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); 751d560168bSGuenter Roeck if (!ptr) 752d560168bSGuenter Roeck return ERR_PTR(-ENOMEM); 753d560168bSGuenter Roeck 754d560168bSGuenter Roeck hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip, 755d560168bSGuenter Roeck groups); 756d560168bSGuenter Roeck if (IS_ERR(hwdev)) 757d560168bSGuenter Roeck goto error; 758d560168bSGuenter Roeck 759d560168bSGuenter Roeck *ptr = hwdev; 760d560168bSGuenter Roeck devres_add(dev, ptr); 761d560168bSGuenter Roeck 762d560168bSGuenter Roeck return hwdev; 763d560168bSGuenter Roeck 764d560168bSGuenter Roeck error: 765d560168bSGuenter Roeck devres_free(ptr); 766d560168bSGuenter Roeck return hwdev; 767d560168bSGuenter Roeck } 768d560168bSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info); 769d560168bSGuenter Roeck 77074188cbaSGuenter Roeck static int devm_hwmon_match(struct device *dev, void *res, void *data) 77174188cbaSGuenter Roeck { 77274188cbaSGuenter Roeck struct device **hwdev = res; 77374188cbaSGuenter Roeck 77474188cbaSGuenter Roeck return *hwdev == data; 77574188cbaSGuenter Roeck } 77674188cbaSGuenter Roeck 77774188cbaSGuenter Roeck /** 77874188cbaSGuenter Roeck * devm_hwmon_device_unregister - removes a previously registered hwmon device 77974188cbaSGuenter Roeck * 78074188cbaSGuenter Roeck * @dev: the parent device of the device to unregister 78174188cbaSGuenter Roeck */ 78274188cbaSGuenter Roeck void devm_hwmon_device_unregister(struct device *dev) 78374188cbaSGuenter Roeck { 78474188cbaSGuenter Roeck WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev)); 78574188cbaSGuenter Roeck } 78674188cbaSGuenter Roeck EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister); 78774188cbaSGuenter Roeck 7882958b1ecSJean Delvare static void __init hwmon_pci_quirks(void) 7892958b1ecSJean Delvare { 7902958b1ecSJean Delvare #if defined CONFIG_X86 && defined CONFIG_PCI 7912958b1ecSJean Delvare struct pci_dev *sb; 7922958b1ecSJean Delvare u16 base; 7932958b1ecSJean Delvare u8 enable; 7942958b1ecSJean Delvare 7952958b1ecSJean Delvare /* Open access to 0x295-0x296 on MSI MS-7031 */ 7962958b1ecSJean Delvare sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL); 797d6dab7ddSJean Delvare if (sb) { 798d6dab7ddSJean Delvare if (sb->subsystem_vendor == 0x1462 && /* MSI */ 799d6dab7ddSJean Delvare sb->subsystem_device == 0x0031) { /* MS-7031 */ 8002958b1ecSJean Delvare pci_read_config_byte(sb, 0x48, &enable); 8012958b1ecSJean Delvare pci_read_config_word(sb, 0x64, &base); 8022958b1ecSJean Delvare 8032958b1ecSJean Delvare if (base == 0 && !(enable & BIT(2))) { 8042958b1ecSJean Delvare dev_info(&sb->dev, 8052958b1ecSJean Delvare "Opening wide generic port at 0x295\n"); 8062958b1ecSJean Delvare pci_write_config_word(sb, 0x64, 0x295); 807d6dab7ddSJean Delvare pci_write_config_byte(sb, 0x48, 808d6dab7ddSJean Delvare enable | BIT(2)); 8092958b1ecSJean Delvare } 8102958b1ecSJean Delvare } 811d6dab7ddSJean Delvare pci_dev_put(sb); 812d6dab7ddSJean Delvare } 8132958b1ecSJean Delvare #endif 8142958b1ecSJean Delvare } 8152958b1ecSJean Delvare 8161236441fSMark M. Hoffman static int __init hwmon_init(void) 8171236441fSMark M. Hoffman { 818bab2243cSGuenter Roeck int err; 819bab2243cSGuenter Roeck 8202958b1ecSJean Delvare hwmon_pci_quirks(); 8212958b1ecSJean Delvare 822bab2243cSGuenter Roeck err = class_register(&hwmon_class); 823bab2243cSGuenter Roeck if (err) { 824bab2243cSGuenter Roeck pr_err("couldn't register hwmon sysfs class\n"); 825bab2243cSGuenter Roeck return err; 8261236441fSMark M. Hoffman } 8271236441fSMark M. Hoffman return 0; 8281236441fSMark M. Hoffman } 8291236441fSMark M. Hoffman 8301236441fSMark M. Hoffman static void __exit hwmon_exit(void) 8311236441fSMark M. Hoffman { 832bab2243cSGuenter Roeck class_unregister(&hwmon_class); 8331236441fSMark M. Hoffman } 8341236441fSMark M. Hoffman 83537f54ee5SDavid Brownell subsys_initcall(hwmon_init); 8361236441fSMark M. Hoffman module_exit(hwmon_exit); 8371236441fSMark M. Hoffman 8381236441fSMark M. Hoffman MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>"); 8391236441fSMark M. Hoffman MODULE_DESCRIPTION("hardware monitoring sysfs/class support"); 8401236441fSMark M. Hoffman MODULE_LICENSE("GPL"); 8411236441fSMark M. Hoffman 842