1 // SPDX-License-Identifier: GPL-2.0-only
3 * hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring
5 * This file defines the sysfs class "hwmon", for use by sensors drivers.
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/bitops.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/gfp.h>
16 #include <linux/hwmon.h>
17 #include <linux/i2c.h>
18 #include <linux/idr.h>
19 #include <linux/kstrtox.h>
20 #include <linux/list.h>
21 #include <linux/module.h>
22 #include <linux/pci.h>
23 #include <linux/property.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/thermal.h>
28 #define CREATE_TRACE_POINTS
29 #include <trace/events/hwmon.h>
31 #define HWMON_ID_PREFIX "hwmon"
32 #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
38 const struct hwmon_chip_info *chip;
39 struct list_head tzdata;
40 struct attribute_group group;
41 const struct attribute_group **groups;
44 #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
46 #define MAX_SYSFS_ATTR_NAME_LENGTH 32
48 struct hwmon_device_attribute {
49 struct device_attribute dev_attr;
50 const struct hwmon_ops *ops;
51 enum hwmon_sensor_types type;
54 char name[MAX_SYSFS_ATTR_NAME_LENGTH];
57 #define to_hwmon_attr(d) \
58 container_of(d, struct hwmon_device_attribute, dev_attr)
59 #define to_dev_attr(a) container_of(a, struct device_attribute, attr)
62 * Thermal zone information
64 struct hwmon_thermal_data {
65 struct list_head node; /* hwmon tzdata list entry */
66 struct device *dev; /* Reference to hwmon device */
67 int index; /* sensor index */
68 struct thermal_zone_device *tzd;/* thermal zone device */
72 name_show(struct device *dev, struct device_attribute *attr, char *buf)
74 return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
76 static DEVICE_ATTR_RO(name);
79 label_show(struct device *dev, struct device_attribute *attr, char *buf)
81 return sysfs_emit(buf, "%s\n", to_hwmon_device(dev)->label);
83 static DEVICE_ATTR_RO(label);
85 static struct attribute *hwmon_dev_attrs[] = {
91 static umode_t hwmon_dev_attr_is_visible(struct kobject *kobj,
92 struct attribute *attr, int n)
94 struct device *dev = kobj_to_dev(kobj);
95 struct hwmon_device *hdev = to_hwmon_device(dev);
97 if (attr == &dev_attr_name.attr && hdev->name == NULL)
100 if (attr == &dev_attr_label.attr && hdev->label == NULL)
106 static const struct attribute_group hwmon_dev_attr_group = {
107 .attrs = hwmon_dev_attrs,
108 .is_visible = hwmon_dev_attr_is_visible,
111 static const struct attribute_group *hwmon_dev_attr_groups[] = {
112 &hwmon_dev_attr_group,
116 static void hwmon_free_attrs(struct attribute **attrs)
120 for (i = 0; attrs[i]; i++) {
121 struct device_attribute *dattr = to_dev_attr(attrs[i]);
122 struct hwmon_device_attribute *hattr = to_hwmon_attr(dattr);
129 static void hwmon_dev_release(struct device *dev)
131 struct hwmon_device *hwdev = to_hwmon_device(dev);
133 if (hwdev->group.attrs)
134 hwmon_free_attrs(hwdev->group.attrs);
135 kfree(hwdev->groups);
140 static const struct class hwmon_class = {
142 .dev_groups = hwmon_dev_attr_groups,
143 .dev_release = hwmon_dev_release,
146 static DEFINE_IDA(hwmon_ida);
148 /* Thermal zone handling */
151 * The complex conditional is necessary to avoid a cyclic dependency
152 * between hwmon and thermal_sys modules.
154 #ifdef CONFIG_THERMAL_OF
155 static int hwmon_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
157 struct hwmon_thermal_data *tdata = thermal_zone_device_priv(tz);
158 struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
162 ret = hwdev->chip->ops->read(tdata->dev, hwmon_temp, hwmon_temp_input,
172 static int hwmon_thermal_set_trips(struct thermal_zone_device *tz, int low, int high)
174 struct hwmon_thermal_data *tdata = thermal_zone_device_priv(tz);
175 struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
176 const struct hwmon_chip_info *chip = hwdev->chip;
177 const struct hwmon_channel_info * const *info = chip->info;
181 if (!chip->ops->write)
184 for (i = 0; info[i] && info[i]->type != hwmon_temp; i++)
190 if (info[i]->config[tdata->index] & HWMON_T_MIN) {
191 err = chip->ops->write(tdata->dev, hwmon_temp,
192 hwmon_temp_min, tdata->index, low);
193 if (err && err != -EOPNOTSUPP)
197 if (info[i]->config[tdata->index] & HWMON_T_MAX) {
198 err = chip->ops->write(tdata->dev, hwmon_temp,
199 hwmon_temp_max, tdata->index, high);
200 if (err && err != -EOPNOTSUPP)
207 static const struct thermal_zone_device_ops hwmon_thermal_ops = {
208 .get_temp = hwmon_thermal_get_temp,
209 .set_trips = hwmon_thermal_set_trips,
212 static void hwmon_thermal_remove_sensor(void *data)
217 static int hwmon_thermal_add_sensor(struct device *dev, int index)
219 struct hwmon_device *hwdev = to_hwmon_device(dev);
220 struct hwmon_thermal_data *tdata;
221 struct thermal_zone_device *tzd;
224 tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
229 tdata->index = index;
231 tzd = devm_thermal_of_zone_register(dev, index, tdata,
234 if (PTR_ERR(tzd) != -ENODEV)
236 dev_info(dev, "temp%d_input not attached to any thermal zone\n",
238 devm_kfree(dev, tdata);
242 err = devm_add_action(dev, hwmon_thermal_remove_sensor, &tdata->node);
247 list_add(&tdata->node, &hwdev->tzdata);
252 static int hwmon_thermal_register_sensors(struct device *dev)
254 struct hwmon_device *hwdev = to_hwmon_device(dev);
255 const struct hwmon_chip_info *chip = hwdev->chip;
256 const struct hwmon_channel_info * const *info = chip->info;
257 void *drvdata = dev_get_drvdata(dev);
260 for (i = 1; info[i]; i++) {
263 if (info[i]->type != hwmon_temp)
266 for (j = 0; info[i]->config[j]; j++) {
269 if (!(info[i]->config[j] & HWMON_T_INPUT) ||
270 !chip->ops->is_visible(drvdata, hwmon_temp,
271 hwmon_temp_input, j))
274 err = hwmon_thermal_add_sensor(dev, j);
283 static void hwmon_thermal_notify(struct device *dev, int index)
285 struct hwmon_device *hwdev = to_hwmon_device(dev);
286 struct hwmon_thermal_data *tzdata;
288 list_for_each_entry(tzdata, &hwdev->tzdata, node) {
289 if (tzdata->index == index) {
290 thermal_zone_device_update(tzdata->tzd,
291 THERMAL_EVENT_UNSPECIFIED);
297 static int hwmon_thermal_register_sensors(struct device *dev)
302 static void hwmon_thermal_notify(struct device *dev, int index) { }
304 #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
306 static int hwmon_attr_base(enum hwmon_sensor_types type)
308 if (type == hwmon_in || type == hwmon_intrusion)
313 #if IS_REACHABLE(CONFIG_I2C)
318 * The 'pec' attribute is attached to I2C client devices. It is only provided
319 * if the i2c controller supports PEC.
321 * The mutex ensures that PEC configuration between i2c device and the hardware
322 * is consistent. Use a single mutex because attribute writes are supposed to be
323 * rare, and maintaining a separate mutex for each hardware monitoring device
324 * would add substantial complexity to the driver for little if any gain.
326 * The hardware monitoring device is identified as child of the i2c client
327 * device. This assumes that only a single hardware monitoring device is
328 * attached to an i2c client device.
331 static DEFINE_MUTEX(hwmon_pec_mutex);
333 static int hwmon_match_device(struct device *dev, void *data)
335 return dev->class == &hwmon_class;
338 static ssize_t pec_show(struct device *dev, struct device_attribute *dummy,
341 struct i2c_client *client = to_i2c_client(dev);
343 return sysfs_emit(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
346 static ssize_t pec_store(struct device *dev, struct device_attribute *devattr,
347 const char *buf, size_t count)
349 struct i2c_client *client = to_i2c_client(dev);
350 struct hwmon_device *hwdev;
355 err = kstrtobool(buf, &val);
359 hdev = device_find_child(dev, NULL, hwmon_match_device);
363 mutex_lock(&hwmon_pec_mutex);
366 * If there is no write function, we assume that chip specific
367 * handling is not required.
369 hwdev = to_hwmon_device(hdev);
370 if (hwdev->chip->ops->write) {
371 err = hwdev->chip->ops->write(hdev, hwmon_chip, hwmon_chip_pec, 0, val);
372 if (err && err != -EOPNOTSUPP)
377 client->flags &= ~I2C_CLIENT_PEC;
379 client->flags |= I2C_CLIENT_PEC;
383 mutex_unlock(&hwmon_pec_mutex);
389 static DEVICE_ATTR_RW(pec);
391 static void hwmon_remove_pec(void *dev)
393 device_remove_file(dev, &dev_attr_pec);
396 static int hwmon_pec_register(struct device *hdev)
398 struct i2c_client *client = i2c_verify_client(hdev->parent);
404 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_PEC))
407 err = device_create_file(&client->dev, &dev_attr_pec);
411 return devm_add_action_or_reset(hdev, hwmon_remove_pec, &client->dev);
414 #else /* CONFIG_I2C */
415 static int hwmon_pec_register(struct device *hdev)
419 #endif /* CONFIG_I2C */
421 /* sysfs attribute management */
423 static ssize_t hwmon_attr_show(struct device *dev,
424 struct device_attribute *devattr, char *buf)
426 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
430 ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
435 trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type),
438 return sprintf(buf, "%ld\n", val);
441 static ssize_t hwmon_attr_show_string(struct device *dev,
442 struct device_attribute *devattr,
445 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
446 enum hwmon_sensor_types type = hattr->type;
450 ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
455 trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type),
458 return sprintf(buf, "%s\n", s);
461 static ssize_t hwmon_attr_store(struct device *dev,
462 struct device_attribute *devattr,
463 const char *buf, size_t count)
465 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
469 ret = kstrtol(buf, 10, &val);
473 ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
478 trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type),
484 static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
486 return (type == hwmon_temp && attr == hwmon_temp_label) ||
487 (type == hwmon_in && attr == hwmon_in_label) ||
488 (type == hwmon_curr && attr == hwmon_curr_label) ||
489 (type == hwmon_power && attr == hwmon_power_label) ||
490 (type == hwmon_energy && attr == hwmon_energy_label) ||
491 (type == hwmon_humidity && attr == hwmon_humidity_label) ||
492 (type == hwmon_fan && attr == hwmon_fan_label);
495 static struct attribute *hwmon_genattr(const void *drvdata,
496 enum hwmon_sensor_types type,
499 const char *template,
500 const struct hwmon_ops *ops)
502 struct hwmon_device_attribute *hattr;
503 struct device_attribute *dattr;
507 bool is_string = is_string_attr(type, attr);
509 mode = ops->is_visible(drvdata, type, attr, index);
511 return ERR_PTR(-ENOENT);
513 if ((mode & 0444) && ((is_string && !ops->read_string) ||
514 (!is_string && !ops->read)))
515 return ERR_PTR(-EINVAL);
516 if ((mode & 0222) && !ops->write)
517 return ERR_PTR(-EINVAL);
519 hattr = kzalloc(sizeof(*hattr), GFP_KERNEL);
521 return ERR_PTR(-ENOMEM);
523 if (type == hwmon_chip) {
526 scnprintf(hattr->name, sizeof(hattr->name), template,
527 index + hwmon_attr_base(type));
533 hattr->index = index;
536 dattr = &hattr->dev_attr;
537 dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
538 dattr->store = hwmon_attr_store;
549 * Chip attributes are not attribute templates but actual sysfs attributes.
550 * See hwmon_genattr() for special handling.
552 static const char * const hwmon_chip_attrs[] = {
553 [hwmon_chip_temp_reset_history] = "temp_reset_history",
554 [hwmon_chip_in_reset_history] = "in_reset_history",
555 [hwmon_chip_curr_reset_history] = "curr_reset_history",
556 [hwmon_chip_power_reset_history] = "power_reset_history",
557 [hwmon_chip_update_interval] = "update_interval",
558 [hwmon_chip_alarms] = "alarms",
559 [hwmon_chip_samples] = "samples",
560 [hwmon_chip_curr_samples] = "curr_samples",
561 [hwmon_chip_in_samples] = "in_samples",
562 [hwmon_chip_power_samples] = "power_samples",
563 [hwmon_chip_temp_samples] = "temp_samples",
564 [hwmon_chip_beep_enable] = "beep_enable",
567 static const char * const hwmon_temp_attr_templates[] = {
568 [hwmon_temp_enable] = "temp%d_enable",
569 [hwmon_temp_input] = "temp%d_input",
570 [hwmon_temp_type] = "temp%d_type",
571 [hwmon_temp_lcrit] = "temp%d_lcrit",
572 [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
573 [hwmon_temp_min] = "temp%d_min",
574 [hwmon_temp_min_hyst] = "temp%d_min_hyst",
575 [hwmon_temp_max] = "temp%d_max",
576 [hwmon_temp_max_hyst] = "temp%d_max_hyst",
577 [hwmon_temp_crit] = "temp%d_crit",
578 [hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
579 [hwmon_temp_emergency] = "temp%d_emergency",
580 [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
581 [hwmon_temp_alarm] = "temp%d_alarm",
582 [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
583 [hwmon_temp_min_alarm] = "temp%d_min_alarm",
584 [hwmon_temp_max_alarm] = "temp%d_max_alarm",
585 [hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
586 [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
587 [hwmon_temp_fault] = "temp%d_fault",
588 [hwmon_temp_offset] = "temp%d_offset",
589 [hwmon_temp_label] = "temp%d_label",
590 [hwmon_temp_lowest] = "temp%d_lowest",
591 [hwmon_temp_highest] = "temp%d_highest",
592 [hwmon_temp_reset_history] = "temp%d_reset_history",
593 [hwmon_temp_rated_min] = "temp%d_rated_min",
594 [hwmon_temp_rated_max] = "temp%d_rated_max",
595 [hwmon_temp_beep] = "temp%d_beep",
598 static const char * const hwmon_in_attr_templates[] = {
599 [hwmon_in_enable] = "in%d_enable",
600 [hwmon_in_input] = "in%d_input",
601 [hwmon_in_min] = "in%d_min",
602 [hwmon_in_max] = "in%d_max",
603 [hwmon_in_lcrit] = "in%d_lcrit",
604 [hwmon_in_crit] = "in%d_crit",
605 [hwmon_in_average] = "in%d_average",
606 [hwmon_in_lowest] = "in%d_lowest",
607 [hwmon_in_highest] = "in%d_highest",
608 [hwmon_in_reset_history] = "in%d_reset_history",
609 [hwmon_in_label] = "in%d_label",
610 [hwmon_in_alarm] = "in%d_alarm",
611 [hwmon_in_min_alarm] = "in%d_min_alarm",
612 [hwmon_in_max_alarm] = "in%d_max_alarm",
613 [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
614 [hwmon_in_crit_alarm] = "in%d_crit_alarm",
615 [hwmon_in_rated_min] = "in%d_rated_min",
616 [hwmon_in_rated_max] = "in%d_rated_max",
617 [hwmon_in_beep] = "in%d_beep",
618 [hwmon_in_fault] = "in%d_fault",
621 static const char * const hwmon_curr_attr_templates[] = {
622 [hwmon_curr_enable] = "curr%d_enable",
623 [hwmon_curr_input] = "curr%d_input",
624 [hwmon_curr_min] = "curr%d_min",
625 [hwmon_curr_max] = "curr%d_max",
626 [hwmon_curr_lcrit] = "curr%d_lcrit",
627 [hwmon_curr_crit] = "curr%d_crit",
628 [hwmon_curr_average] = "curr%d_average",
629 [hwmon_curr_lowest] = "curr%d_lowest",
630 [hwmon_curr_highest] = "curr%d_highest",
631 [hwmon_curr_reset_history] = "curr%d_reset_history",
632 [hwmon_curr_label] = "curr%d_label",
633 [hwmon_curr_alarm] = "curr%d_alarm",
634 [hwmon_curr_min_alarm] = "curr%d_min_alarm",
635 [hwmon_curr_max_alarm] = "curr%d_max_alarm",
636 [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
637 [hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
638 [hwmon_curr_rated_min] = "curr%d_rated_min",
639 [hwmon_curr_rated_max] = "curr%d_rated_max",
640 [hwmon_curr_beep] = "curr%d_beep",
643 static const char * const hwmon_power_attr_templates[] = {
644 [hwmon_power_enable] = "power%d_enable",
645 [hwmon_power_average] = "power%d_average",
646 [hwmon_power_average_interval] = "power%d_average_interval",
647 [hwmon_power_average_interval_max] = "power%d_interval_max",
648 [hwmon_power_average_interval_min] = "power%d_interval_min",
649 [hwmon_power_average_highest] = "power%d_average_highest",
650 [hwmon_power_average_lowest] = "power%d_average_lowest",
651 [hwmon_power_average_max] = "power%d_average_max",
652 [hwmon_power_average_min] = "power%d_average_min",
653 [hwmon_power_input] = "power%d_input",
654 [hwmon_power_input_highest] = "power%d_input_highest",
655 [hwmon_power_input_lowest] = "power%d_input_lowest",
656 [hwmon_power_reset_history] = "power%d_reset_history",
657 [hwmon_power_accuracy] = "power%d_accuracy",
658 [hwmon_power_cap] = "power%d_cap",
659 [hwmon_power_cap_hyst] = "power%d_cap_hyst",
660 [hwmon_power_cap_max] = "power%d_cap_max",
661 [hwmon_power_cap_min] = "power%d_cap_min",
662 [hwmon_power_min] = "power%d_min",
663 [hwmon_power_max] = "power%d_max",
664 [hwmon_power_lcrit] = "power%d_lcrit",
665 [hwmon_power_crit] = "power%d_crit",
666 [hwmon_power_label] = "power%d_label",
667 [hwmon_power_alarm] = "power%d_alarm",
668 [hwmon_power_cap_alarm] = "power%d_cap_alarm",
669 [hwmon_power_min_alarm] = "power%d_min_alarm",
670 [hwmon_power_max_alarm] = "power%d_max_alarm",
671 [hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
672 [hwmon_power_crit_alarm] = "power%d_crit_alarm",
673 [hwmon_power_rated_min] = "power%d_rated_min",
674 [hwmon_power_rated_max] = "power%d_rated_max",
677 static const char * const hwmon_energy_attr_templates[] = {
678 [hwmon_energy_enable] = "energy%d_enable",
679 [hwmon_energy_input] = "energy%d_input",
680 [hwmon_energy_label] = "energy%d_label",
683 static const char * const hwmon_humidity_attr_templates[] = {
684 [hwmon_humidity_enable] = "humidity%d_enable",
685 [hwmon_humidity_input] = "humidity%d_input",
686 [hwmon_humidity_label] = "humidity%d_label",
687 [hwmon_humidity_min] = "humidity%d_min",
688 [hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
689 [hwmon_humidity_max] = "humidity%d_max",
690 [hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
691 [hwmon_humidity_alarm] = "humidity%d_alarm",
692 [hwmon_humidity_fault] = "humidity%d_fault",
693 [hwmon_humidity_rated_min] = "humidity%d_rated_min",
694 [hwmon_humidity_rated_max] = "humidity%d_rated_max",
695 [hwmon_humidity_min_alarm] = "humidity%d_min_alarm",
696 [hwmon_humidity_max_alarm] = "humidity%d_max_alarm",
699 static const char * const hwmon_fan_attr_templates[] = {
700 [hwmon_fan_enable] = "fan%d_enable",
701 [hwmon_fan_input] = "fan%d_input",
702 [hwmon_fan_label] = "fan%d_label",
703 [hwmon_fan_min] = "fan%d_min",
704 [hwmon_fan_max] = "fan%d_max",
705 [hwmon_fan_div] = "fan%d_div",
706 [hwmon_fan_pulses] = "fan%d_pulses",
707 [hwmon_fan_target] = "fan%d_target",
708 [hwmon_fan_alarm] = "fan%d_alarm",
709 [hwmon_fan_min_alarm] = "fan%d_min_alarm",
710 [hwmon_fan_max_alarm] = "fan%d_max_alarm",
711 [hwmon_fan_fault] = "fan%d_fault",
712 [hwmon_fan_beep] = "fan%d_beep",
715 static const char * const hwmon_pwm_attr_templates[] = {
716 [hwmon_pwm_input] = "pwm%d",
717 [hwmon_pwm_enable] = "pwm%d_enable",
718 [hwmon_pwm_mode] = "pwm%d_mode",
719 [hwmon_pwm_freq] = "pwm%d_freq",
720 [hwmon_pwm_auto_channels_temp] = "pwm%d_auto_channels_temp",
723 static const char * const hwmon_intrusion_attr_templates[] = {
724 [hwmon_intrusion_alarm] = "intrusion%d_alarm",
725 [hwmon_intrusion_beep] = "intrusion%d_beep",
728 static const char * const *__templates[] = {
729 [hwmon_chip] = hwmon_chip_attrs,
730 [hwmon_temp] = hwmon_temp_attr_templates,
731 [hwmon_in] = hwmon_in_attr_templates,
732 [hwmon_curr] = hwmon_curr_attr_templates,
733 [hwmon_power] = hwmon_power_attr_templates,
734 [hwmon_energy] = hwmon_energy_attr_templates,
735 [hwmon_humidity] = hwmon_humidity_attr_templates,
736 [hwmon_fan] = hwmon_fan_attr_templates,
737 [hwmon_pwm] = hwmon_pwm_attr_templates,
738 [hwmon_intrusion] = hwmon_intrusion_attr_templates,
741 static const int __templates_size[] = {
742 [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
743 [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
744 [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
745 [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
746 [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
747 [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
748 [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
749 [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
750 [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
751 [hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates),
754 int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type,
755 u32 attr, int channel)
757 char event[MAX_SYSFS_ATTR_NAME_LENGTH + 5];
758 char sattr[MAX_SYSFS_ATTR_NAME_LENGTH];
759 char *envp[] = { event, NULL };
760 const char * const *templates;
761 const char *template;
764 if (type >= ARRAY_SIZE(__templates))
766 if (attr >= __templates_size[type])
769 templates = __templates[type];
770 template = templates[attr];
772 base = hwmon_attr_base(type);
774 scnprintf(sattr, MAX_SYSFS_ATTR_NAME_LENGTH, template, base + channel);
775 scnprintf(event, sizeof(event), "NAME=%s", sattr);
776 sysfs_notify(&dev->kobj, NULL, sattr);
777 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
779 if (type == hwmon_temp)
780 hwmon_thermal_notify(dev, channel);
784 EXPORT_SYMBOL_GPL(hwmon_notify_event);
786 static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
790 for (i = n = 0; info->config[i]; i++)
791 n += hweight32(info->config[i]);
796 static int hwmon_genattrs(const void *drvdata,
797 struct attribute **attrs,
798 const struct hwmon_ops *ops,
799 const struct hwmon_channel_info *info)
801 const char * const *templates;
805 if (info->type >= ARRAY_SIZE(__templates))
808 templates = __templates[info->type];
809 template_size = __templates_size[info->type];
811 for (i = 0; info->config[i]; i++) {
812 u32 attr_mask = info->config[i];
818 attr = __ffs(attr_mask);
819 attr_mask &= ~BIT(attr);
820 if (attr >= template_size || !templates[attr])
821 continue; /* attribute is invisible */
822 a = hwmon_genattr(drvdata, info->type, attr, i,
823 templates[attr], ops);
825 if (PTR_ERR(a) != -ENOENT)
835 static struct attribute **
836 __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip)
838 int ret, i, aindex = 0, nattrs = 0;
839 struct attribute **attrs;
841 for (i = 0; chip->info[i]; i++)
842 nattrs += hwmon_num_channel_attrs(chip->info[i]);
845 return ERR_PTR(-EINVAL);
847 attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL);
849 return ERR_PTR(-ENOMEM);
851 for (i = 0; chip->info[i]; i++) {
852 ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops,
855 hwmon_free_attrs(attrs);
864 static struct device *
865 __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
866 const struct hwmon_chip_info *chip,
867 const struct attribute_group **groups)
869 struct hwmon_device *hwdev;
872 struct device *tdev = dev;
875 /* Complain about invalid characters in hwmon name attribute */
876 if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
878 "hwmon: '%s' is not a valid name attribute, please fix\n",
881 id = ida_alloc(&hwmon_ida, GFP_KERNEL);
885 hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
894 struct attribute **attrs;
895 int ngroups = 2; /* terminating NULL plus &hwdev->groups */
898 for (i = 0; groups[i]; i++)
901 hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL);
902 if (!hwdev->groups) {
907 attrs = __hwmon_create_attrs(drvdata, chip);
909 err = PTR_ERR(attrs);
913 hwdev->group.attrs = attrs;
915 hwdev->groups[ngroups++] = &hwdev->group;
918 for (i = 0; groups[i]; i++)
919 hwdev->groups[ngroups++] = groups[i];
922 hdev->groups = hwdev->groups;
924 hdev->groups = groups;
927 if (dev && device_property_present(dev, "label")) {
928 err = device_property_read_string(dev, "label", &label);
932 hwdev->label = kstrdup(label, GFP_KERNEL);
933 if (hwdev->label == NULL) {
940 hdev->class = &hwmon_class;
942 while (tdev && !tdev->of_node)
944 hdev->of_node = tdev ? tdev->of_node : NULL;
946 dev_set_drvdata(hdev, drvdata);
947 dev_set_name(hdev, HWMON_ID_FORMAT, id);
948 err = device_register(hdev);
954 INIT_LIST_HEAD(&hwdev->tzdata);
956 if (hdev->of_node && chip && chip->ops->read &&
957 chip->info[0]->type == hwmon_chip) {
958 u32 config = chip->info[0]->config[0];
960 if (config & HWMON_C_REGISTER_TZ) {
961 err = hwmon_thermal_register_sensors(hdev);
963 device_unregister(hdev);
965 * Don't worry about hwdev; hwmon_dev_release(),
966 * called from device_unregister(), will free it.
971 if (config & HWMON_C_PEC) {
972 err = hwmon_pec_register(hdev);
974 device_unregister(hdev);
983 hwmon_dev_release(hdev);
985 ida_free(&hwmon_ida, id);
990 * hwmon_device_register_with_groups - register w/ hwmon
991 * @dev: the parent device
992 * @name: hwmon name attribute
993 * @drvdata: driver data to attach to created device
994 * @groups: List of attribute groups to create
996 * hwmon_device_unregister() must be called when the device is no
999 * Returns the pointer to the new device.
1002 hwmon_device_register_with_groups(struct device *dev, const char *name,
1004 const struct attribute_group **groups)
1007 return ERR_PTR(-EINVAL);
1009 return __hwmon_device_register(dev, name, drvdata, NULL, groups);
1011 EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
1014 * hwmon_device_register_with_info - register w/ hwmon
1015 * @dev: the parent device (mandatory)
1016 * @name: hwmon name attribute (mandatory)
1017 * @drvdata: driver data to attach to created device (optional)
1018 * @chip: pointer to hwmon chip information (mandatory)
1019 * @extra_groups: pointer to list of additional non-standard attribute groups
1022 * hwmon_device_unregister() must be called when the device is no
1025 * Returns the pointer to the new device.
1028 hwmon_device_register_with_info(struct device *dev, const char *name,
1030 const struct hwmon_chip_info *chip,
1031 const struct attribute_group **extra_groups)
1033 if (!dev || !name || !chip)
1034 return ERR_PTR(-EINVAL);
1036 if (!chip->ops || !chip->ops->is_visible || !chip->info)
1037 return ERR_PTR(-EINVAL);
1039 return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
1041 EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
1044 * hwmon_device_register_for_thermal - register hwmon device for thermal subsystem
1045 * @dev: the parent device
1046 * @name: hwmon name attribute
1047 * @drvdata: driver data to attach to created device
1049 * The use of this function is restricted. It is provided for legacy reasons
1050 * and must only be called from the thermal subsystem.
1052 * hwmon_device_unregister() must be called when the device is no
1055 * Returns the pointer to the new device.
1058 hwmon_device_register_for_thermal(struct device *dev, const char *name,
1062 return ERR_PTR(-EINVAL);
1064 return __hwmon_device_register(dev, name, drvdata, NULL, NULL);
1066 EXPORT_SYMBOL_NS_GPL(hwmon_device_register_for_thermal, HWMON_THERMAL);
1069 * hwmon_device_register - register w/ hwmon
1070 * @dev: the device to register
1072 * hwmon_device_unregister() must be called when the device is no
1075 * Returns the pointer to the new device.
1077 struct device *hwmon_device_register(struct device *dev)
1080 "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
1082 return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
1084 EXPORT_SYMBOL_GPL(hwmon_device_register);
1087 * hwmon_device_unregister - removes the previously registered class device
1089 * @dev: the class device to destroy
1091 void hwmon_device_unregister(struct device *dev)
1095 if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
1096 device_unregister(dev);
1097 ida_free(&hwmon_ida, id);
1099 dev_dbg(dev->parent,
1100 "hwmon_device_unregister() failed: bad class ID!\n");
1102 EXPORT_SYMBOL_GPL(hwmon_device_unregister);
1104 static void devm_hwmon_release(struct device *dev, void *res)
1106 struct device *hwdev = *(struct device **)res;
1108 hwmon_device_unregister(hwdev);
1112 * devm_hwmon_device_register_with_groups - register w/ hwmon
1113 * @dev: the parent device
1114 * @name: hwmon name attribute
1115 * @drvdata: driver data to attach to created device
1116 * @groups: List of attribute groups to create
1118 * Returns the pointer to the new device. The new device is automatically
1119 * unregistered with the parent device.
1122 devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
1124 const struct attribute_group **groups)
1126 struct device **ptr, *hwdev;
1129 return ERR_PTR(-EINVAL);
1131 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
1133 return ERR_PTR(-ENOMEM);
1135 hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
1140 devres_add(dev, ptr);
1147 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
1150 * devm_hwmon_device_register_with_info - register w/ hwmon
1151 * @dev: the parent device
1152 * @name: hwmon name attribute
1153 * @drvdata: driver data to attach to created device
1154 * @chip: pointer to hwmon chip information
1155 * @extra_groups: pointer to list of driver specific attribute groups
1157 * Returns the pointer to the new device. The new device is automatically
1158 * unregistered with the parent device.
1161 devm_hwmon_device_register_with_info(struct device *dev, const char *name,
1163 const struct hwmon_chip_info *chip,
1164 const struct attribute_group **extra_groups)
1166 struct device **ptr, *hwdev;
1169 return ERR_PTR(-EINVAL);
1171 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
1173 return ERR_PTR(-ENOMEM);
1175 hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
1181 devres_add(dev, ptr);
1189 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
1191 static char *__hwmon_sanitize_name(struct device *dev, const char *old_name)
1196 name = devm_kstrdup(dev, old_name, GFP_KERNEL);
1198 name = kstrdup(old_name, GFP_KERNEL);
1200 return ERR_PTR(-ENOMEM);
1202 for (p = name; *p; p++)
1203 if (hwmon_is_bad_char(*p))
1210 * hwmon_sanitize_name - Replaces invalid characters in a hwmon name
1211 * @name: NUL-terminated name
1213 * Allocates a new string where any invalid characters will be replaced
1214 * by an underscore. It is the responsibility of the caller to release
1217 * Returns newly allocated name, or ERR_PTR on error.
1219 char *hwmon_sanitize_name(const char *name)
1221 return __hwmon_sanitize_name(NULL, name);
1223 EXPORT_SYMBOL_GPL(hwmon_sanitize_name);
1226 * devm_hwmon_sanitize_name - resource managed hwmon_sanitize_name()
1227 * @dev: device to allocate memory for
1228 * @name: NUL-terminated name
1230 * Allocates a new string where any invalid characters will be replaced
1233 * Returns newly allocated name, or ERR_PTR on error.
1235 char *devm_hwmon_sanitize_name(struct device *dev, const char *name)
1238 return ERR_PTR(-EINVAL);
1240 return __hwmon_sanitize_name(dev, name);
1242 EXPORT_SYMBOL_GPL(devm_hwmon_sanitize_name);
1244 static void __init hwmon_pci_quirks(void)
1246 #if defined CONFIG_X86 && defined CONFIG_PCI
1251 /* Open access to 0x295-0x296 on MSI MS-7031 */
1252 sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
1254 if (sb->subsystem_vendor == 0x1462 && /* MSI */
1255 sb->subsystem_device == 0x0031) { /* MS-7031 */
1256 pci_read_config_byte(sb, 0x48, &enable);
1257 pci_read_config_word(sb, 0x64, &base);
1259 if (base == 0 && !(enable & BIT(2))) {
1261 "Opening wide generic port at 0x295\n");
1262 pci_write_config_word(sb, 0x64, 0x295);
1263 pci_write_config_byte(sb, 0x48,
1272 static int __init hwmon_init(void)
1278 err = class_register(&hwmon_class);
1280 pr_err("couldn't register hwmon sysfs class\n");
1286 static void __exit hwmon_exit(void)
1288 class_unregister(&hwmon_class);
1291 subsys_initcall(hwmon_init);
1292 module_exit(hwmon_exit);
1295 MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
1296 MODULE_LICENSE("GPL");