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/idr.h>
18 #include <linux/kstrtox.h>
19 #include <linux/list.h>
20 #include <linux/module.h>
21 #include <linux/pci.h>
22 #include <linux/property.h>
23 #include <linux/slab.h>
24 #include <linux/string.h>
25 #include <linux/thermal.h>
27 #define CREATE_TRACE_POINTS
28 #include <trace/events/hwmon.h>
30 #define HWMON_ID_PREFIX "hwmon"
31 #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
37 const struct hwmon_chip_info *chip;
38 struct list_head tzdata;
39 struct attribute_group group;
40 const struct attribute_group **groups;
43 #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
45 #define MAX_SYSFS_ATTR_NAME_LENGTH 32
47 struct hwmon_device_attribute {
48 struct device_attribute dev_attr;
49 const struct hwmon_ops *ops;
50 enum hwmon_sensor_types type;
53 char name[MAX_SYSFS_ATTR_NAME_LENGTH];
56 #define to_hwmon_attr(d) \
57 container_of(d, struct hwmon_device_attribute, dev_attr)
58 #define to_dev_attr(a) container_of(a, struct device_attribute, attr)
61 * Thermal zone information
63 struct hwmon_thermal_data {
64 struct list_head node; /* hwmon tzdata list entry */
65 struct device *dev; /* Reference to hwmon device */
66 int index; /* sensor index */
67 struct thermal_zone_device *tzd;/* thermal zone device */
71 name_show(struct device *dev, struct device_attribute *attr, char *buf)
73 return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
75 static DEVICE_ATTR_RO(name);
78 label_show(struct device *dev, struct device_attribute *attr, char *buf)
80 return sysfs_emit(buf, "%s\n", to_hwmon_device(dev)->label);
82 static DEVICE_ATTR_RO(label);
84 static struct attribute *hwmon_dev_attrs[] = {
90 static umode_t hwmon_dev_attr_is_visible(struct kobject *kobj,
91 struct attribute *attr, int n)
93 struct device *dev = kobj_to_dev(kobj);
94 struct hwmon_device *hdev = to_hwmon_device(dev);
96 if (attr == &dev_attr_name.attr && hdev->name == NULL)
99 if (attr == &dev_attr_label.attr && hdev->label == NULL)
105 static const struct attribute_group hwmon_dev_attr_group = {
106 .attrs = hwmon_dev_attrs,
107 .is_visible = hwmon_dev_attr_is_visible,
110 static const struct attribute_group *hwmon_dev_attr_groups[] = {
111 &hwmon_dev_attr_group,
115 static void hwmon_free_attrs(struct attribute **attrs)
119 for (i = 0; attrs[i]; i++) {
120 struct device_attribute *dattr = to_dev_attr(attrs[i]);
121 struct hwmon_device_attribute *hattr = to_hwmon_attr(dattr);
128 static void hwmon_dev_release(struct device *dev)
130 struct hwmon_device *hwdev = to_hwmon_device(dev);
132 if (hwdev->group.attrs)
133 hwmon_free_attrs(hwdev->group.attrs);
134 kfree(hwdev->groups);
139 static struct class hwmon_class = {
141 .owner = THIS_MODULE,
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 = tz->devdata;
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 = tz->devdata;
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 /* sysfs attribute management */
315 static ssize_t hwmon_attr_show(struct device *dev,
316 struct device_attribute *devattr, char *buf)
318 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
322 ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
327 trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type),
330 return sprintf(buf, "%ld\n", val);
333 static ssize_t hwmon_attr_show_string(struct device *dev,
334 struct device_attribute *devattr,
337 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
338 enum hwmon_sensor_types type = hattr->type;
342 ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
347 trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type),
350 return sprintf(buf, "%s\n", s);
353 static ssize_t hwmon_attr_store(struct device *dev,
354 struct device_attribute *devattr,
355 const char *buf, size_t count)
357 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
361 ret = kstrtol(buf, 10, &val);
365 ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
370 trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type),
376 static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
378 return (type == hwmon_temp && attr == hwmon_temp_label) ||
379 (type == hwmon_in && attr == hwmon_in_label) ||
380 (type == hwmon_curr && attr == hwmon_curr_label) ||
381 (type == hwmon_power && attr == hwmon_power_label) ||
382 (type == hwmon_energy && attr == hwmon_energy_label) ||
383 (type == hwmon_humidity && attr == hwmon_humidity_label) ||
384 (type == hwmon_fan && attr == hwmon_fan_label);
387 static struct attribute *hwmon_genattr(const void *drvdata,
388 enum hwmon_sensor_types type,
391 const char *template,
392 const struct hwmon_ops *ops)
394 struct hwmon_device_attribute *hattr;
395 struct device_attribute *dattr;
399 bool is_string = is_string_attr(type, attr);
401 /* The attribute is invisible if there is no template string */
403 return ERR_PTR(-ENOENT);
405 mode = ops->is_visible(drvdata, type, attr, index);
407 return ERR_PTR(-ENOENT);
409 if ((mode & 0444) && ((is_string && !ops->read_string) ||
410 (!is_string && !ops->read)))
411 return ERR_PTR(-EINVAL);
412 if ((mode & 0222) && !ops->write)
413 return ERR_PTR(-EINVAL);
415 hattr = kzalloc(sizeof(*hattr), GFP_KERNEL);
417 return ERR_PTR(-ENOMEM);
419 if (type == hwmon_chip) {
422 scnprintf(hattr->name, sizeof(hattr->name), template,
423 index + hwmon_attr_base(type));
429 hattr->index = index;
432 dattr = &hattr->dev_attr;
433 dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
434 dattr->store = hwmon_attr_store;
445 * Chip attributes are not attribute templates but actual sysfs attributes.
446 * See hwmon_genattr() for special handling.
448 static const char * const hwmon_chip_attrs[] = {
449 [hwmon_chip_temp_reset_history] = "temp_reset_history",
450 [hwmon_chip_in_reset_history] = "in_reset_history",
451 [hwmon_chip_curr_reset_history] = "curr_reset_history",
452 [hwmon_chip_power_reset_history] = "power_reset_history",
453 [hwmon_chip_update_interval] = "update_interval",
454 [hwmon_chip_alarms] = "alarms",
455 [hwmon_chip_samples] = "samples",
456 [hwmon_chip_curr_samples] = "curr_samples",
457 [hwmon_chip_in_samples] = "in_samples",
458 [hwmon_chip_power_samples] = "power_samples",
459 [hwmon_chip_temp_samples] = "temp_samples",
462 static const char * const hwmon_temp_attr_templates[] = {
463 [hwmon_temp_enable] = "temp%d_enable",
464 [hwmon_temp_input] = "temp%d_input",
465 [hwmon_temp_type] = "temp%d_type",
466 [hwmon_temp_lcrit] = "temp%d_lcrit",
467 [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
468 [hwmon_temp_min] = "temp%d_min",
469 [hwmon_temp_min_hyst] = "temp%d_min_hyst",
470 [hwmon_temp_max] = "temp%d_max",
471 [hwmon_temp_max_hyst] = "temp%d_max_hyst",
472 [hwmon_temp_crit] = "temp%d_crit",
473 [hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
474 [hwmon_temp_emergency] = "temp%d_emergency",
475 [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
476 [hwmon_temp_alarm] = "temp%d_alarm",
477 [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
478 [hwmon_temp_min_alarm] = "temp%d_min_alarm",
479 [hwmon_temp_max_alarm] = "temp%d_max_alarm",
480 [hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
481 [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
482 [hwmon_temp_fault] = "temp%d_fault",
483 [hwmon_temp_offset] = "temp%d_offset",
484 [hwmon_temp_label] = "temp%d_label",
485 [hwmon_temp_lowest] = "temp%d_lowest",
486 [hwmon_temp_highest] = "temp%d_highest",
487 [hwmon_temp_reset_history] = "temp%d_reset_history",
488 [hwmon_temp_rated_min] = "temp%d_rated_min",
489 [hwmon_temp_rated_max] = "temp%d_rated_max",
492 static const char * const hwmon_in_attr_templates[] = {
493 [hwmon_in_enable] = "in%d_enable",
494 [hwmon_in_input] = "in%d_input",
495 [hwmon_in_min] = "in%d_min",
496 [hwmon_in_max] = "in%d_max",
497 [hwmon_in_lcrit] = "in%d_lcrit",
498 [hwmon_in_crit] = "in%d_crit",
499 [hwmon_in_average] = "in%d_average",
500 [hwmon_in_lowest] = "in%d_lowest",
501 [hwmon_in_highest] = "in%d_highest",
502 [hwmon_in_reset_history] = "in%d_reset_history",
503 [hwmon_in_label] = "in%d_label",
504 [hwmon_in_alarm] = "in%d_alarm",
505 [hwmon_in_min_alarm] = "in%d_min_alarm",
506 [hwmon_in_max_alarm] = "in%d_max_alarm",
507 [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
508 [hwmon_in_crit_alarm] = "in%d_crit_alarm",
509 [hwmon_in_rated_min] = "in%d_rated_min",
510 [hwmon_in_rated_max] = "in%d_rated_max",
513 static const char * const hwmon_curr_attr_templates[] = {
514 [hwmon_curr_enable] = "curr%d_enable",
515 [hwmon_curr_input] = "curr%d_input",
516 [hwmon_curr_min] = "curr%d_min",
517 [hwmon_curr_max] = "curr%d_max",
518 [hwmon_curr_lcrit] = "curr%d_lcrit",
519 [hwmon_curr_crit] = "curr%d_crit",
520 [hwmon_curr_average] = "curr%d_average",
521 [hwmon_curr_lowest] = "curr%d_lowest",
522 [hwmon_curr_highest] = "curr%d_highest",
523 [hwmon_curr_reset_history] = "curr%d_reset_history",
524 [hwmon_curr_label] = "curr%d_label",
525 [hwmon_curr_alarm] = "curr%d_alarm",
526 [hwmon_curr_min_alarm] = "curr%d_min_alarm",
527 [hwmon_curr_max_alarm] = "curr%d_max_alarm",
528 [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
529 [hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
530 [hwmon_curr_rated_min] = "curr%d_rated_min",
531 [hwmon_curr_rated_max] = "curr%d_rated_max",
534 static const char * const hwmon_power_attr_templates[] = {
535 [hwmon_power_enable] = "power%d_enable",
536 [hwmon_power_average] = "power%d_average",
537 [hwmon_power_average_interval] = "power%d_average_interval",
538 [hwmon_power_average_interval_max] = "power%d_interval_max",
539 [hwmon_power_average_interval_min] = "power%d_interval_min",
540 [hwmon_power_average_highest] = "power%d_average_highest",
541 [hwmon_power_average_lowest] = "power%d_average_lowest",
542 [hwmon_power_average_max] = "power%d_average_max",
543 [hwmon_power_average_min] = "power%d_average_min",
544 [hwmon_power_input] = "power%d_input",
545 [hwmon_power_input_highest] = "power%d_input_highest",
546 [hwmon_power_input_lowest] = "power%d_input_lowest",
547 [hwmon_power_reset_history] = "power%d_reset_history",
548 [hwmon_power_accuracy] = "power%d_accuracy",
549 [hwmon_power_cap] = "power%d_cap",
550 [hwmon_power_cap_hyst] = "power%d_cap_hyst",
551 [hwmon_power_cap_max] = "power%d_cap_max",
552 [hwmon_power_cap_min] = "power%d_cap_min",
553 [hwmon_power_min] = "power%d_min",
554 [hwmon_power_max] = "power%d_max",
555 [hwmon_power_lcrit] = "power%d_lcrit",
556 [hwmon_power_crit] = "power%d_crit",
557 [hwmon_power_label] = "power%d_label",
558 [hwmon_power_alarm] = "power%d_alarm",
559 [hwmon_power_cap_alarm] = "power%d_cap_alarm",
560 [hwmon_power_min_alarm] = "power%d_min_alarm",
561 [hwmon_power_max_alarm] = "power%d_max_alarm",
562 [hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
563 [hwmon_power_crit_alarm] = "power%d_crit_alarm",
564 [hwmon_power_rated_min] = "power%d_rated_min",
565 [hwmon_power_rated_max] = "power%d_rated_max",
568 static const char * const hwmon_energy_attr_templates[] = {
569 [hwmon_energy_enable] = "energy%d_enable",
570 [hwmon_energy_input] = "energy%d_input",
571 [hwmon_energy_label] = "energy%d_label",
574 static const char * const hwmon_humidity_attr_templates[] = {
575 [hwmon_humidity_enable] = "humidity%d_enable",
576 [hwmon_humidity_input] = "humidity%d_input",
577 [hwmon_humidity_label] = "humidity%d_label",
578 [hwmon_humidity_min] = "humidity%d_min",
579 [hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
580 [hwmon_humidity_max] = "humidity%d_max",
581 [hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
582 [hwmon_humidity_alarm] = "humidity%d_alarm",
583 [hwmon_humidity_fault] = "humidity%d_fault",
584 [hwmon_humidity_rated_min] = "humidity%d_rated_min",
585 [hwmon_humidity_rated_max] = "humidity%d_rated_max",
588 static const char * const hwmon_fan_attr_templates[] = {
589 [hwmon_fan_enable] = "fan%d_enable",
590 [hwmon_fan_input] = "fan%d_input",
591 [hwmon_fan_label] = "fan%d_label",
592 [hwmon_fan_min] = "fan%d_min",
593 [hwmon_fan_max] = "fan%d_max",
594 [hwmon_fan_div] = "fan%d_div",
595 [hwmon_fan_pulses] = "fan%d_pulses",
596 [hwmon_fan_target] = "fan%d_target",
597 [hwmon_fan_alarm] = "fan%d_alarm",
598 [hwmon_fan_min_alarm] = "fan%d_min_alarm",
599 [hwmon_fan_max_alarm] = "fan%d_max_alarm",
600 [hwmon_fan_fault] = "fan%d_fault",
603 static const char * const hwmon_pwm_attr_templates[] = {
604 [hwmon_pwm_input] = "pwm%d",
605 [hwmon_pwm_enable] = "pwm%d_enable",
606 [hwmon_pwm_mode] = "pwm%d_mode",
607 [hwmon_pwm_freq] = "pwm%d_freq",
608 [hwmon_pwm_auto_channels_temp] = "pwm%d_auto_channels_temp",
611 static const char * const hwmon_intrusion_attr_templates[] = {
612 [hwmon_intrusion_alarm] = "intrusion%d_alarm",
613 [hwmon_intrusion_beep] = "intrusion%d_beep",
616 static const char * const *__templates[] = {
617 [hwmon_chip] = hwmon_chip_attrs,
618 [hwmon_temp] = hwmon_temp_attr_templates,
619 [hwmon_in] = hwmon_in_attr_templates,
620 [hwmon_curr] = hwmon_curr_attr_templates,
621 [hwmon_power] = hwmon_power_attr_templates,
622 [hwmon_energy] = hwmon_energy_attr_templates,
623 [hwmon_humidity] = hwmon_humidity_attr_templates,
624 [hwmon_fan] = hwmon_fan_attr_templates,
625 [hwmon_pwm] = hwmon_pwm_attr_templates,
626 [hwmon_intrusion] = hwmon_intrusion_attr_templates,
629 static const int __templates_size[] = {
630 [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
631 [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
632 [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
633 [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
634 [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
635 [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
636 [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
637 [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
638 [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
639 [hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates),
642 int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type,
643 u32 attr, int channel)
645 char event[MAX_SYSFS_ATTR_NAME_LENGTH + 5];
646 char sattr[MAX_SYSFS_ATTR_NAME_LENGTH];
647 char *envp[] = { event, NULL };
648 const char * const *templates;
649 const char *template;
652 if (type >= ARRAY_SIZE(__templates))
654 if (attr >= __templates_size[type])
657 templates = __templates[type];
658 template = templates[attr];
660 base = hwmon_attr_base(type);
662 scnprintf(sattr, MAX_SYSFS_ATTR_NAME_LENGTH, template, base + channel);
663 scnprintf(event, sizeof(event), "NAME=%s", sattr);
664 sysfs_notify(&dev->kobj, NULL, sattr);
665 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
667 if (type == hwmon_temp)
668 hwmon_thermal_notify(dev, channel);
672 EXPORT_SYMBOL_GPL(hwmon_notify_event);
674 static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
678 for (i = n = 0; info->config[i]; i++)
679 n += hweight32(info->config[i]);
684 static int hwmon_genattrs(const void *drvdata,
685 struct attribute **attrs,
686 const struct hwmon_ops *ops,
687 const struct hwmon_channel_info *info)
689 const char * const *templates;
693 if (info->type >= ARRAY_SIZE(__templates))
696 templates = __templates[info->type];
697 template_size = __templates_size[info->type];
699 for (i = 0; info->config[i]; i++) {
700 u32 attr_mask = info->config[i];
706 attr = __ffs(attr_mask);
707 attr_mask &= ~BIT(attr);
708 if (attr >= template_size)
710 a = hwmon_genattr(drvdata, info->type, attr, i,
711 templates[attr], ops);
713 if (PTR_ERR(a) != -ENOENT)
723 static struct attribute **
724 __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip)
726 int ret, i, aindex = 0, nattrs = 0;
727 struct attribute **attrs;
729 for (i = 0; chip->info[i]; i++)
730 nattrs += hwmon_num_channel_attrs(chip->info[i]);
733 return ERR_PTR(-EINVAL);
735 attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL);
737 return ERR_PTR(-ENOMEM);
739 for (i = 0; chip->info[i]; i++) {
740 ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops,
743 hwmon_free_attrs(attrs);
752 static struct device *
753 __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
754 const struct hwmon_chip_info *chip,
755 const struct attribute_group **groups)
757 struct hwmon_device *hwdev;
760 struct device *tdev = dev;
763 /* Complain about invalid characters in hwmon name attribute */
764 if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
766 "hwmon: '%s' is not a valid name attribute, please fix\n",
769 id = ida_alloc(&hwmon_ida, GFP_KERNEL);
773 hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
782 struct attribute **attrs;
783 int ngroups = 2; /* terminating NULL plus &hwdev->groups */
786 for (i = 0; groups[i]; i++)
789 hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL);
790 if (!hwdev->groups) {
795 attrs = __hwmon_create_attrs(drvdata, chip);
797 err = PTR_ERR(attrs);
801 hwdev->group.attrs = attrs;
803 hwdev->groups[ngroups++] = &hwdev->group;
806 for (i = 0; groups[i]; i++)
807 hwdev->groups[ngroups++] = groups[i];
810 hdev->groups = hwdev->groups;
812 hdev->groups = groups;
815 if (dev && device_property_present(dev, "label")) {
816 err = device_property_read_string(dev, "label", &label);
820 hwdev->label = kstrdup(label, GFP_KERNEL);
821 if (hwdev->label == NULL) {
828 hdev->class = &hwmon_class;
830 while (tdev && !tdev->of_node)
832 hdev->of_node = tdev ? tdev->of_node : NULL;
834 dev_set_drvdata(hdev, drvdata);
835 dev_set_name(hdev, HWMON_ID_FORMAT, id);
836 err = device_register(hdev);
842 INIT_LIST_HEAD(&hwdev->tzdata);
844 if (hdev->of_node && chip && chip->ops->read &&
845 chip->info[0]->type == hwmon_chip &&
846 (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
847 err = hwmon_thermal_register_sensors(hdev);
849 device_unregister(hdev);
851 * Don't worry about hwdev; hwmon_dev_release(), called
852 * from device_unregister(), will free it.
861 hwmon_dev_release(hdev);
863 ida_free(&hwmon_ida, id);
868 * hwmon_device_register_with_groups - register w/ hwmon
869 * @dev: the parent device
870 * @name: hwmon name attribute
871 * @drvdata: driver data to attach to created device
872 * @groups: List of attribute groups to create
874 * hwmon_device_unregister() must be called when the device is no
877 * Returns the pointer to the new device.
880 hwmon_device_register_with_groups(struct device *dev, const char *name,
882 const struct attribute_group **groups)
885 return ERR_PTR(-EINVAL);
887 return __hwmon_device_register(dev, name, drvdata, NULL, groups);
889 EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
892 * hwmon_device_register_with_info - register w/ hwmon
893 * @dev: the parent device (mandatory)
894 * @name: hwmon name attribute (mandatory)
895 * @drvdata: driver data to attach to created device (optional)
896 * @chip: pointer to hwmon chip information (mandatory)
897 * @extra_groups: pointer to list of additional non-standard attribute groups
900 * hwmon_device_unregister() must be called when the device is no
903 * Returns the pointer to the new device.
906 hwmon_device_register_with_info(struct device *dev, const char *name,
908 const struct hwmon_chip_info *chip,
909 const struct attribute_group **extra_groups)
911 if (!dev || !name || !chip)
912 return ERR_PTR(-EINVAL);
914 if (!chip->ops || !chip->ops->is_visible || !chip->info)
915 return ERR_PTR(-EINVAL);
917 return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
919 EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
922 * hwmon_device_register_for_thermal - register hwmon device for thermal subsystem
923 * @dev: the parent device
924 * @name: hwmon name attribute
925 * @drvdata: driver data to attach to created device
927 * The use of this function is restricted. It is provided for legacy reasons
928 * and must only be called from the thermal subsystem.
930 * hwmon_device_unregister() must be called when the device is no
933 * Returns the pointer to the new device.
936 hwmon_device_register_for_thermal(struct device *dev, const char *name,
940 return ERR_PTR(-EINVAL);
942 return __hwmon_device_register(dev, name, drvdata, NULL, NULL);
944 EXPORT_SYMBOL_NS_GPL(hwmon_device_register_for_thermal, HWMON_THERMAL);
947 * hwmon_device_register - register w/ hwmon
948 * @dev: the device to register
950 * hwmon_device_unregister() must be called when the device is no
953 * Returns the pointer to the new device.
955 struct device *hwmon_device_register(struct device *dev)
958 "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
960 return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
962 EXPORT_SYMBOL_GPL(hwmon_device_register);
965 * hwmon_device_unregister - removes the previously registered class device
967 * @dev: the class device to destroy
969 void hwmon_device_unregister(struct device *dev)
973 if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
974 device_unregister(dev);
975 ida_free(&hwmon_ida, id);
978 "hwmon_device_unregister() failed: bad class ID!\n");
980 EXPORT_SYMBOL_GPL(hwmon_device_unregister);
982 static void devm_hwmon_release(struct device *dev, void *res)
984 struct device *hwdev = *(struct device **)res;
986 hwmon_device_unregister(hwdev);
990 * devm_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 * Returns the pointer to the new device. The new device is automatically
997 * unregistered with the parent device.
1000 devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
1002 const struct attribute_group **groups)
1004 struct device **ptr, *hwdev;
1007 return ERR_PTR(-EINVAL);
1009 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
1011 return ERR_PTR(-ENOMEM);
1013 hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
1018 devres_add(dev, ptr);
1025 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
1028 * devm_hwmon_device_register_with_info - register w/ hwmon
1029 * @dev: the parent device
1030 * @name: hwmon name attribute
1031 * @drvdata: driver data to attach to created device
1032 * @chip: pointer to hwmon chip information
1033 * @groups: pointer to list of driver specific attribute groups
1035 * Returns the pointer to the new device. The new device is automatically
1036 * unregistered with the parent device.
1039 devm_hwmon_device_register_with_info(struct device *dev, const char *name,
1041 const struct hwmon_chip_info *chip,
1042 const struct attribute_group **groups)
1044 struct device **ptr, *hwdev;
1047 return ERR_PTR(-EINVAL);
1049 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
1051 return ERR_PTR(-ENOMEM);
1053 hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
1059 devres_add(dev, ptr);
1067 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
1069 static int devm_hwmon_match(struct device *dev, void *res, void *data)
1071 struct device **hwdev = res;
1073 return *hwdev == data;
1077 * devm_hwmon_device_unregister - removes a previously registered hwmon device
1079 * @dev: the parent device of the device to unregister
1081 void devm_hwmon_device_unregister(struct device *dev)
1083 WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
1085 EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
1087 static char *__hwmon_sanitize_name(struct device *dev, const char *old_name)
1092 name = devm_kstrdup(dev, old_name, GFP_KERNEL);
1094 name = kstrdup(old_name, GFP_KERNEL);
1096 return ERR_PTR(-ENOMEM);
1098 for (p = name; *p; p++)
1099 if (hwmon_is_bad_char(*p))
1106 * hwmon_sanitize_name - Replaces invalid characters in a hwmon name
1107 * @name: NUL-terminated name
1109 * Allocates a new string where any invalid characters will be replaced
1110 * by an underscore. It is the responsibility of the caller to release
1113 * Returns newly allocated name, or ERR_PTR on error.
1115 char *hwmon_sanitize_name(const char *name)
1117 return __hwmon_sanitize_name(NULL, name);
1119 EXPORT_SYMBOL_GPL(hwmon_sanitize_name);
1122 * devm_hwmon_sanitize_name - resource managed hwmon_sanitize_name()
1123 * @dev: device to allocate memory for
1124 * @name: NUL-terminated name
1126 * Allocates a new string where any invalid characters will be replaced
1129 * Returns newly allocated name, or ERR_PTR on error.
1131 char *devm_hwmon_sanitize_name(struct device *dev, const char *name)
1134 return ERR_PTR(-EINVAL);
1136 return __hwmon_sanitize_name(dev, name);
1138 EXPORT_SYMBOL_GPL(devm_hwmon_sanitize_name);
1140 static void __init hwmon_pci_quirks(void)
1142 #if defined CONFIG_X86 && defined CONFIG_PCI
1147 /* Open access to 0x295-0x296 on MSI MS-7031 */
1148 sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
1150 if (sb->subsystem_vendor == 0x1462 && /* MSI */
1151 sb->subsystem_device == 0x0031) { /* MS-7031 */
1152 pci_read_config_byte(sb, 0x48, &enable);
1153 pci_read_config_word(sb, 0x64, &base);
1155 if (base == 0 && !(enable & BIT(2))) {
1157 "Opening wide generic port at 0x295\n");
1158 pci_write_config_word(sb, 0x64, 0x295);
1159 pci_write_config_byte(sb, 0x48,
1168 static int __init hwmon_init(void)
1174 err = class_register(&hwmon_class);
1176 pr_err("couldn't register hwmon sysfs class\n");
1182 static void __exit hwmon_exit(void)
1184 class_unregister(&hwmon_class);
1187 subsys_initcall(hwmon_init);
1188 module_exit(hwmon_exit);
1191 MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
1192 MODULE_LICENSE("GPL");