2 * hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring
4 * This file defines the sysfs class "hwmon", for use by sensors drivers.
6 * Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/bitops.h>
16 #include <linux/device.h>
17 #include <linux/err.h>
18 #include <linux/gfp.h>
19 #include <linux/hwmon.h>
20 #include <linux/idr.h>
21 #include <linux/module.h>
22 #include <linux/pci.h>
23 #include <linux/slab.h>
24 #include <linux/string.h>
25 #include <linux/thermal.h>
27 #define HWMON_ID_PREFIX "hwmon"
28 #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
33 const struct hwmon_chip_info *chip;
35 struct attribute_group group;
36 const struct attribute_group **groups;
39 #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
41 struct hwmon_device_attribute {
42 struct device_attribute dev_attr;
43 const struct hwmon_ops *ops;
44 enum hwmon_sensor_types type;
49 #define to_hwmon_attr(d) \
50 container_of(d, struct hwmon_device_attribute, dev_attr)
53 * Thermal zone information
54 * In addition to the reference to the hwmon device,
55 * also provides the sensor index.
57 struct hwmon_thermal_data {
58 struct hwmon_device *hwdev; /* Reference to hwmon device */
59 int index; /* sensor index */
63 show_name(struct device *dev, struct device_attribute *attr, char *buf)
65 return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
67 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
69 static struct attribute *hwmon_dev_attrs[] = {
74 static umode_t hwmon_dev_name_is_visible(struct kobject *kobj,
75 struct attribute *attr, int n)
77 struct device *dev = container_of(kobj, struct device, kobj);
79 if (to_hwmon_device(dev)->name == NULL)
85 static struct attribute_group hwmon_dev_attr_group = {
86 .attrs = hwmon_dev_attrs,
87 .is_visible = hwmon_dev_name_is_visible,
90 static const struct attribute_group *hwmon_dev_attr_groups[] = {
91 &hwmon_dev_attr_group,
95 static void hwmon_dev_release(struct device *dev)
97 kfree(to_hwmon_device(dev));
100 static struct class hwmon_class = {
102 .owner = THIS_MODULE,
103 .dev_groups = hwmon_dev_attr_groups,
104 .dev_release = hwmon_dev_release,
107 static DEFINE_IDA(hwmon_ida);
109 /* Thermal zone handling */
111 #if IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF)
112 static int hwmon_thermal_get_temp(void *data, int *temp)
114 struct hwmon_thermal_data *tdata = data;
115 struct hwmon_device *hwdev = tdata->hwdev;
119 ret = hwdev->chip->ops->read(&hwdev->dev, hwmon_temp, hwmon_temp_input,
129 static struct thermal_zone_of_device_ops hwmon_thermal_ops = {
130 .get_temp = hwmon_thermal_get_temp,
133 static int hwmon_thermal_add_sensor(struct device *dev,
134 struct hwmon_device *hwdev, int index)
136 struct hwmon_thermal_data *tdata;
138 tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
142 tdata->hwdev = hwdev;
143 tdata->index = index;
145 devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
151 static int hwmon_thermal_add_sensor(struct device *dev,
152 struct hwmon_device *hwdev, int index)
156 #endif /* IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF) */
158 /* sysfs attribute management */
160 static ssize_t hwmon_attr_show(struct device *dev,
161 struct device_attribute *devattr, char *buf)
163 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
167 ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
172 return sprintf(buf, "%ld\n", val);
175 static ssize_t hwmon_attr_store(struct device *dev,
176 struct device_attribute *devattr,
177 const char *buf, size_t count)
179 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
183 ret = kstrtol(buf, 10, &val);
187 ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
195 static int hwmon_attr_base(enum hwmon_sensor_types type)
197 if (type == hwmon_in)
202 static struct attribute *hwmon_genattr(struct device *dev,
204 enum hwmon_sensor_types type,
207 const char *template,
208 const struct hwmon_ops *ops)
210 struct hwmon_device_attribute *hattr;
211 struct device_attribute *dattr;
216 /* The attribute is invisible if there is no template string */
218 return ERR_PTR(-ENOENT);
220 mode = ops->is_visible(drvdata, type, attr, index);
222 return ERR_PTR(-ENOENT);
224 if ((mode & S_IRUGO) && !ops->read)
225 return ERR_PTR(-EINVAL);
226 if ((mode & S_IWUGO) && !ops->write)
227 return ERR_PTR(-EINVAL);
229 if (type == hwmon_chip) {
230 name = (char *)template;
232 name = devm_kzalloc(dev, strlen(template) + 16, GFP_KERNEL);
234 return ERR_PTR(-ENOMEM);
235 scnprintf(name, strlen(template) + 16, template,
236 index + hwmon_attr_base(type));
239 hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL);
241 return ERR_PTR(-ENOMEM);
245 hattr->index = index;
248 dattr = &hattr->dev_attr;
249 dattr->show = hwmon_attr_show;
250 dattr->store = hwmon_attr_store;
260 static const char * const hwmon_chip_attr_templates[] = {
261 [hwmon_chip_temp_reset_history] = "temp_reset_history",
262 [hwmon_chip_in_reset_history] = "in_reset_history",
263 [hwmon_chip_curr_reset_history] = "curr_reset_history",
264 [hwmon_chip_power_reset_history] = "power_reset_history",
265 [hwmon_chip_update_interval] = "update_interval",
266 [hwmon_chip_alarms] = "alarms",
269 static const char * const hwmon_temp_attr_templates[] = {
270 [hwmon_temp_input] = "temp%d_input",
271 [hwmon_temp_type] = "temp%d_type",
272 [hwmon_temp_lcrit] = "temp%d_lcrit",
273 [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
274 [hwmon_temp_min] = "temp%d_min",
275 [hwmon_temp_min_hyst] = "temp%d_min_hyst",
276 [hwmon_temp_max] = "temp%d_max",
277 [hwmon_temp_max_hyst] = "temp%d_max_hyst",
278 [hwmon_temp_crit] = "temp%d_crit",
279 [hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
280 [hwmon_temp_emergency] = "temp%d_emergency",
281 [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
282 [hwmon_temp_alarm] = "temp%d_alarm",
283 [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
284 [hwmon_temp_min_alarm] = "temp%d_min_alarm",
285 [hwmon_temp_max_alarm] = "temp%d_max_alarm",
286 [hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
287 [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
288 [hwmon_temp_fault] = "temp%d_fault",
289 [hwmon_temp_offset] = "temp%d_offset",
290 [hwmon_temp_label] = "temp%d_label",
291 [hwmon_temp_lowest] = "temp%d_lowest",
292 [hwmon_temp_highest] = "temp%d_highest",
293 [hwmon_temp_reset_history] = "temp%d_reset_history",
296 static const char * const hwmon_in_attr_templates[] = {
297 [hwmon_in_input] = "in%d_input",
298 [hwmon_in_min] = "in%d_min",
299 [hwmon_in_max] = "in%d_max",
300 [hwmon_in_lcrit] = "in%d_lcrit",
301 [hwmon_in_crit] = "in%d_crit",
302 [hwmon_in_average] = "in%d_average",
303 [hwmon_in_lowest] = "in%d_lowest",
304 [hwmon_in_highest] = "in%d_highest",
305 [hwmon_in_reset_history] = "in%d_reset_history",
306 [hwmon_in_label] = "in%d_label",
307 [hwmon_in_alarm] = "in%d_alarm",
308 [hwmon_in_min_alarm] = "in%d_min_alarm",
309 [hwmon_in_max_alarm] = "in%d_max_alarm",
310 [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
311 [hwmon_in_crit_alarm] = "in%d_crit_alarm",
314 static const char * const hwmon_curr_attr_templates[] = {
315 [hwmon_curr_input] = "curr%d_input",
316 [hwmon_curr_min] = "curr%d_min",
317 [hwmon_curr_max] = "curr%d_max",
318 [hwmon_curr_lcrit] = "curr%d_lcrit",
319 [hwmon_curr_crit] = "curr%d_crit",
320 [hwmon_curr_average] = "curr%d_average",
321 [hwmon_curr_lowest] = "curr%d_lowest",
322 [hwmon_curr_highest] = "curr%d_highest",
323 [hwmon_curr_reset_history] = "curr%d_reset_history",
324 [hwmon_curr_label] = "curr%d_label",
325 [hwmon_curr_alarm] = "curr%d_alarm",
326 [hwmon_curr_min_alarm] = "curr%d_min_alarm",
327 [hwmon_curr_max_alarm] = "curr%d_max_alarm",
328 [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
329 [hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
332 static const char * const hwmon_power_attr_templates[] = {
333 [hwmon_power_average] = "power%d_average",
334 [hwmon_power_average_interval] = "power%d_average_interval",
335 [hwmon_power_average_interval_max] = "power%d_interval_max",
336 [hwmon_power_average_interval_min] = "power%d_interval_min",
337 [hwmon_power_average_highest] = "power%d_average_highest",
338 [hwmon_power_average_lowest] = "power%d_average_lowest",
339 [hwmon_power_average_max] = "power%d_average_max",
340 [hwmon_power_average_min] = "power%d_average_min",
341 [hwmon_power_input] = "power%d_input",
342 [hwmon_power_input_highest] = "power%d_input_highest",
343 [hwmon_power_input_lowest] = "power%d_input_lowest",
344 [hwmon_power_reset_history] = "power%d_reset_history",
345 [hwmon_power_accuracy] = "power%d_accuracy",
346 [hwmon_power_cap] = "power%d_cap",
347 [hwmon_power_cap_hyst] = "power%d_cap_hyst",
348 [hwmon_power_cap_max] = "power%d_cap_max",
349 [hwmon_power_cap_min] = "power%d_cap_min",
350 [hwmon_power_max] = "power%d_max",
351 [hwmon_power_crit] = "power%d_crit",
352 [hwmon_power_label] = "power%d_label",
353 [hwmon_power_alarm] = "power%d_alarm",
354 [hwmon_power_cap_alarm] = "power%d_cap_alarm",
355 [hwmon_power_max_alarm] = "power%d_max_alarm",
356 [hwmon_power_crit_alarm] = "power%d_crit_alarm",
359 static const char * const hwmon_energy_attr_templates[] = {
360 [hwmon_energy_input] = "energy%d_input",
361 [hwmon_energy_label] = "energy%d_label",
364 static const char * const hwmon_humidity_attr_templates[] = {
365 [hwmon_humidity_input] = "humidity%d_input",
366 [hwmon_humidity_label] = "humidity%d_label",
367 [hwmon_humidity_min] = "humidity%d_min",
368 [hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
369 [hwmon_humidity_max] = "humidity%d_max",
370 [hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
371 [hwmon_humidity_alarm] = "humidity%d_alarm",
372 [hwmon_humidity_fault] = "humidity%d_fault",
375 static const char * const hwmon_fan_attr_templates[] = {
376 [hwmon_fan_input] = "fan%d_input",
377 [hwmon_fan_label] = "fan%d_label",
378 [hwmon_fan_min] = "fan%d_min",
379 [hwmon_fan_max] = "fan%d_max",
380 [hwmon_fan_div] = "fan%d_div",
381 [hwmon_fan_pulses] = "fan%d_pulses",
382 [hwmon_fan_target] = "fan%d_target",
383 [hwmon_fan_alarm] = "fan%d_alarm",
384 [hwmon_fan_min_alarm] = "fan%d_min_alarm",
385 [hwmon_fan_max_alarm] = "fan%d_max_alarm",
386 [hwmon_fan_fault] = "fan%d_fault",
389 static const char * const *__templates[] = {
390 [hwmon_chip] = hwmon_chip_attr_templates,
391 [hwmon_temp] = hwmon_temp_attr_templates,
392 [hwmon_in] = hwmon_in_attr_templates,
393 [hwmon_curr] = hwmon_curr_attr_templates,
394 [hwmon_power] = hwmon_power_attr_templates,
395 [hwmon_energy] = hwmon_energy_attr_templates,
396 [hwmon_humidity] = hwmon_humidity_attr_templates,
397 [hwmon_fan] = hwmon_fan_attr_templates,
400 static const int __templates_size[] = {
401 [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attr_templates),
402 [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
403 [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
404 [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
405 [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
406 [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
407 [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
408 [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
411 static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
415 for (i = n = 0; info->config[i]; i++)
416 n += hweight32(info->config[i]);
421 static int hwmon_genattrs(struct device *dev,
423 struct attribute **attrs,
424 const struct hwmon_ops *ops,
425 const struct hwmon_channel_info *info)
427 const char * const *templates;
431 if (info->type >= ARRAY_SIZE(__templates))
434 templates = __templates[info->type];
435 template_size = __templates_size[info->type];
437 for (i = 0; info->config[i]; i++) {
438 u32 attr_mask = info->config[i];
444 attr = __ffs(attr_mask);
445 attr_mask &= ~BIT(attr);
446 if (attr >= template_size)
448 a = hwmon_genattr(dev, drvdata, info->type, attr, i,
449 templates[attr], ops);
451 if (PTR_ERR(a) != -ENOENT)
461 static struct attribute **
462 __hwmon_create_attrs(struct device *dev, const void *drvdata,
463 const struct hwmon_chip_info *chip)
465 int ret, i, aindex = 0, nattrs = 0;
466 struct attribute **attrs;
468 for (i = 0; chip->info[i]; i++)
469 nattrs += hwmon_num_channel_attrs(chip->info[i]);
472 return ERR_PTR(-EINVAL);
474 attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL);
476 return ERR_PTR(-ENOMEM);
478 for (i = 0; chip->info[i]; i++) {
479 ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops,
489 static struct device *
490 __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
491 const struct hwmon_chip_info *chip,
492 const struct attribute_group **groups)
494 struct hwmon_device *hwdev;
498 /* Do not accept invalid characters in hwmon name attribute */
499 if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
500 return ERR_PTR(-EINVAL);
502 id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
506 hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
514 if (chip && chip->ops->is_visible) {
515 struct attribute **attrs;
519 for (i = 0; groups[i]; i++)
522 hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups),
525 return ERR_PTR(-ENOMEM);
527 attrs = __hwmon_create_attrs(dev, drvdata, chip);
529 err = PTR_ERR(attrs);
533 hwdev->group.attrs = attrs;
535 hwdev->groups[ngroups++] = &hwdev->group;
538 for (i = 0; groups[i]; i++)
539 hwdev->groups[ngroups++] = groups[i];
542 hdev->groups = hwdev->groups;
544 hdev->groups = groups;
548 hdev->class = &hwmon_class;
550 hdev->of_node = dev ? dev->of_node : NULL;
552 dev_set_drvdata(hdev, drvdata);
553 dev_set_name(hdev, HWMON_ID_FORMAT, id);
554 err = device_register(hdev);
558 if (chip && chip->ops->is_visible && chip->ops->read &&
559 chip->info[0]->type == hwmon_chip &&
560 (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
561 const struct hwmon_channel_info **info = chip->info;
563 for (i = 1; info[i]; i++) {
564 if (info[i]->type != hwmon_temp)
567 for (j = 0; info[i]->config[j]; j++) {
568 if (!chip->ops->is_visible(drvdata, hwmon_temp,
569 hwmon_temp_input, j))
571 if (info[i]->config[j] & HWMON_T_INPUT)
572 hwmon_thermal_add_sensor(dev, hwdev, j);
582 ida_simple_remove(&hwmon_ida, id);
587 * hwmon_device_register_with_groups - register w/ hwmon
588 * @dev: the parent device
589 * @name: hwmon name attribute
590 * @drvdata: driver data to attach to created device
591 * @groups: List of attribute groups to create
593 * hwmon_device_unregister() must be called when the device is no
596 * Returns the pointer to the new device.
599 hwmon_device_register_with_groups(struct device *dev, const char *name,
601 const struct attribute_group **groups)
603 return __hwmon_device_register(dev, name, drvdata, NULL, groups);
605 EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
608 * hwmon_device_register_with_info - register w/ hwmon
609 * @dev: the parent device
610 * @name: hwmon name attribute
611 * @drvdata: driver data to attach to created device
612 * @info: Pointer to hwmon chip information
613 * @groups - pointer to list of driver specific attribute groups
615 * hwmon_device_unregister() must be called when the device is no
618 * Returns the pointer to the new device.
621 hwmon_device_register_with_info(struct device *dev, const char *name,
623 const struct hwmon_chip_info *chip,
624 const struct attribute_group **groups)
626 if (chip && (!chip->ops || !chip->info))
627 return ERR_PTR(-EINVAL);
629 return __hwmon_device_register(dev, name, drvdata, chip, groups);
631 EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
634 * hwmon_device_register - register w/ hwmon
635 * @dev: the device to register
637 * hwmon_device_unregister() must be called when the device is no
640 * Returns the pointer to the new device.
642 struct device *hwmon_device_register(struct device *dev)
644 return hwmon_device_register_with_groups(dev, NULL, NULL, NULL);
646 EXPORT_SYMBOL_GPL(hwmon_device_register);
649 * hwmon_device_unregister - removes the previously registered class device
651 * @dev: the class device to destroy
653 void hwmon_device_unregister(struct device *dev)
657 if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
658 device_unregister(dev);
659 ida_simple_remove(&hwmon_ida, id);
662 "hwmon_device_unregister() failed: bad class ID!\n");
664 EXPORT_SYMBOL_GPL(hwmon_device_unregister);
666 static void devm_hwmon_release(struct device *dev, void *res)
668 struct device *hwdev = *(struct device **)res;
670 hwmon_device_unregister(hwdev);
674 * devm_hwmon_device_register_with_groups - register w/ hwmon
675 * @dev: the parent device
676 * @name: hwmon name attribute
677 * @drvdata: driver data to attach to created device
678 * @groups: List of attribute groups to create
680 * Returns the pointer to the new device. The new device is automatically
681 * unregistered with the parent device.
684 devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
686 const struct attribute_group **groups)
688 struct device **ptr, *hwdev;
691 return ERR_PTR(-EINVAL);
693 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
695 return ERR_PTR(-ENOMEM);
697 hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
702 devres_add(dev, ptr);
709 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
712 * devm_hwmon_device_register_with_info - register w/ hwmon
713 * @dev: the parent device
714 * @name: hwmon name attribute
715 * @drvdata: driver data to attach to created device
716 * @info: Pointer to hwmon chip information
717 * @groups - pointer to list of driver specific attribute groups
719 * Returns the pointer to the new device. The new device is automatically
720 * unregistered with the parent device.
723 devm_hwmon_device_register_with_info(struct device *dev, const char *name,
725 const struct hwmon_chip_info *chip,
726 const struct attribute_group **groups)
728 struct device **ptr, *hwdev;
731 return ERR_PTR(-EINVAL);
733 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
735 return ERR_PTR(-ENOMEM);
737 hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
743 devres_add(dev, ptr);
751 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
753 static int devm_hwmon_match(struct device *dev, void *res, void *data)
755 struct device **hwdev = res;
757 return *hwdev == data;
761 * devm_hwmon_device_unregister - removes a previously registered hwmon device
763 * @dev: the parent device of the device to unregister
765 void devm_hwmon_device_unregister(struct device *dev)
767 WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
769 EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
771 static void __init hwmon_pci_quirks(void)
773 #if defined CONFIG_X86 && defined CONFIG_PCI
778 /* Open access to 0x295-0x296 on MSI MS-7031 */
779 sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
781 if (sb->subsystem_vendor == 0x1462 && /* MSI */
782 sb->subsystem_device == 0x0031) { /* MS-7031 */
783 pci_read_config_byte(sb, 0x48, &enable);
784 pci_read_config_word(sb, 0x64, &base);
786 if (base == 0 && !(enable & BIT(2))) {
788 "Opening wide generic port at 0x295\n");
789 pci_write_config_word(sb, 0x64, 0x295);
790 pci_write_config_byte(sb, 0x48,
799 static int __init hwmon_init(void)
805 err = class_register(&hwmon_class);
807 pr_err("couldn't register hwmon sysfs class\n");
813 static void __exit hwmon_exit(void)
815 class_unregister(&hwmon_class);
818 subsys_initcall(hwmon_init);
819 module_exit(hwmon_exit);
821 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
822 MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
823 MODULE_LICENSE("GPL");