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.
7 * Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com>
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/list.h>
19 #include <linux/module.h>
20 #include <linux/pci.h>
21 #include <linux/slab.h>
22 #include <linux/string.h>
23 #include <linux/thermal.h>
25 #define CREATE_TRACE_POINTS
26 #include <trace/events/hwmon.h>
28 #define HWMON_ID_PREFIX "hwmon"
29 #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
34 const struct hwmon_chip_info *chip;
35 struct list_head tzdata;
36 struct attribute_group group;
37 const struct attribute_group **groups;
40 #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
42 #define MAX_SYSFS_ATTR_NAME_LENGTH 32
44 struct hwmon_device_attribute {
45 struct device_attribute dev_attr;
46 const struct hwmon_ops *ops;
47 enum hwmon_sensor_types type;
50 char name[MAX_SYSFS_ATTR_NAME_LENGTH];
53 #define to_hwmon_attr(d) \
54 container_of(d, struct hwmon_device_attribute, dev_attr)
55 #define to_dev_attr(a) container_of(a, struct device_attribute, attr)
58 * Thermal zone information
60 struct hwmon_thermal_data {
61 struct list_head node; /* hwmon tzdata list entry */
62 struct device *dev; /* Reference to hwmon device */
63 int index; /* sensor index */
64 struct thermal_zone_device *tzd;/* thermal zone device */
68 name_show(struct device *dev, struct device_attribute *attr, char *buf)
70 return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
72 static DEVICE_ATTR_RO(name);
74 static struct attribute *hwmon_dev_attrs[] = {
79 static umode_t hwmon_dev_name_is_visible(struct kobject *kobj,
80 struct attribute *attr, int n)
82 struct device *dev = kobj_to_dev(kobj);
84 if (to_hwmon_device(dev)->name == NULL)
90 static const struct attribute_group hwmon_dev_attr_group = {
91 .attrs = hwmon_dev_attrs,
92 .is_visible = hwmon_dev_name_is_visible,
95 static const struct attribute_group *hwmon_dev_attr_groups[] = {
96 &hwmon_dev_attr_group,
100 static void hwmon_free_attrs(struct attribute **attrs)
104 for (i = 0; attrs[i]; i++) {
105 struct device_attribute *dattr = to_dev_attr(attrs[i]);
106 struct hwmon_device_attribute *hattr = to_hwmon_attr(dattr);
113 static void hwmon_dev_release(struct device *dev)
115 struct hwmon_device *hwdev = to_hwmon_device(dev);
117 if (hwdev->group.attrs)
118 hwmon_free_attrs(hwdev->group.attrs);
119 kfree(hwdev->groups);
123 static struct class hwmon_class = {
125 .owner = THIS_MODULE,
126 .dev_groups = hwmon_dev_attr_groups,
127 .dev_release = hwmon_dev_release,
130 static DEFINE_IDA(hwmon_ida);
132 /* Thermal zone handling */
135 * The complex conditional is necessary to avoid a cyclic dependency
136 * between hwmon and thermal_sys modules.
138 #ifdef CONFIG_THERMAL_OF
139 static int hwmon_thermal_get_temp(void *data, int *temp)
141 struct hwmon_thermal_data *tdata = data;
142 struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
146 ret = hwdev->chip->ops->read(tdata->dev, hwmon_temp, hwmon_temp_input,
156 static int hwmon_thermal_set_trips(void *data, int low, int high)
158 struct hwmon_thermal_data *tdata = data;
159 struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
160 const struct hwmon_chip_info *chip = hwdev->chip;
161 const struct hwmon_channel_info **info = chip->info;
165 if (!chip->ops->write)
168 for (i = 0; info[i] && info[i]->type != hwmon_temp; i++)
174 if (info[i]->config[tdata->index] & HWMON_T_MIN) {
175 err = chip->ops->write(tdata->dev, hwmon_temp,
176 hwmon_temp_min, tdata->index, low);
177 if (err && err != -EOPNOTSUPP)
181 if (info[i]->config[tdata->index] & HWMON_T_MAX) {
182 err = chip->ops->write(tdata->dev, hwmon_temp,
183 hwmon_temp_max, tdata->index, high);
184 if (err && err != -EOPNOTSUPP)
191 static const struct thermal_zone_of_device_ops hwmon_thermal_ops = {
192 .get_temp = hwmon_thermal_get_temp,
193 .set_trips = hwmon_thermal_set_trips,
196 static void hwmon_thermal_remove_sensor(void *data)
201 static int hwmon_thermal_add_sensor(struct device *dev, int index)
203 struct hwmon_device *hwdev = to_hwmon_device(dev);
204 struct hwmon_thermal_data *tdata;
205 struct thermal_zone_device *tzd;
208 tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
213 tdata->index = index;
215 tzd = devm_thermal_zone_of_sensor_register(dev, index, tdata,
218 if (PTR_ERR(tzd) != -ENODEV)
220 dev_info(dev, "temp%d_input not attached to any thermal zone\n",
222 devm_kfree(dev, tdata);
226 err = devm_add_action(dev, hwmon_thermal_remove_sensor, &tdata->node);
231 list_add(&tdata->node, &hwdev->tzdata);
236 static int hwmon_thermal_register_sensors(struct device *dev)
238 struct hwmon_device *hwdev = to_hwmon_device(dev);
239 const struct hwmon_chip_info *chip = hwdev->chip;
240 const struct hwmon_channel_info **info = chip->info;
241 void *drvdata = dev_get_drvdata(dev);
244 for (i = 1; info[i]; i++) {
247 if (info[i]->type != hwmon_temp)
250 for (j = 0; info[i]->config[j]; j++) {
253 if (!(info[i]->config[j] & HWMON_T_INPUT) ||
254 !chip->ops->is_visible(drvdata, hwmon_temp,
255 hwmon_temp_input, j))
258 err = hwmon_thermal_add_sensor(dev, j);
267 static void hwmon_thermal_notify(struct device *dev, int index)
269 struct hwmon_device *hwdev = to_hwmon_device(dev);
270 struct hwmon_thermal_data *tzdata;
272 list_for_each_entry(tzdata, &hwdev->tzdata, node) {
273 if (tzdata->index == index) {
274 thermal_zone_device_update(tzdata->tzd,
275 THERMAL_EVENT_UNSPECIFIED);
281 static int hwmon_thermal_register_sensors(struct device *dev)
286 static void hwmon_thermal_notify(struct device *dev, int index) { }
288 #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
290 static int hwmon_attr_base(enum hwmon_sensor_types type)
292 if (type == hwmon_in || type == hwmon_intrusion)
297 /* sysfs attribute management */
299 static ssize_t hwmon_attr_show(struct device *dev,
300 struct device_attribute *devattr, char *buf)
302 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
306 ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
311 trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type),
314 return sprintf(buf, "%ld\n", val);
317 static ssize_t hwmon_attr_show_string(struct device *dev,
318 struct device_attribute *devattr,
321 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
322 enum hwmon_sensor_types type = hattr->type;
326 ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
331 trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type),
334 return sprintf(buf, "%s\n", s);
337 static ssize_t hwmon_attr_store(struct device *dev,
338 struct device_attribute *devattr,
339 const char *buf, size_t count)
341 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
345 ret = kstrtol(buf, 10, &val);
349 ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
354 trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type),
360 static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
362 return (type == hwmon_temp && attr == hwmon_temp_label) ||
363 (type == hwmon_in && attr == hwmon_in_label) ||
364 (type == hwmon_curr && attr == hwmon_curr_label) ||
365 (type == hwmon_power && attr == hwmon_power_label) ||
366 (type == hwmon_energy && attr == hwmon_energy_label) ||
367 (type == hwmon_humidity && attr == hwmon_humidity_label) ||
368 (type == hwmon_fan && attr == hwmon_fan_label);
371 static struct attribute *hwmon_genattr(const void *drvdata,
372 enum hwmon_sensor_types type,
375 const char *template,
376 const struct hwmon_ops *ops)
378 struct hwmon_device_attribute *hattr;
379 struct device_attribute *dattr;
383 bool is_string = is_string_attr(type, attr);
385 /* The attribute is invisible if there is no template string */
387 return ERR_PTR(-ENOENT);
389 mode = ops->is_visible(drvdata, type, attr, index);
391 return ERR_PTR(-ENOENT);
393 if ((mode & 0444) && ((is_string && !ops->read_string) ||
394 (!is_string && !ops->read)))
395 return ERR_PTR(-EINVAL);
396 if ((mode & 0222) && !ops->write)
397 return ERR_PTR(-EINVAL);
399 hattr = kzalloc(sizeof(*hattr), GFP_KERNEL);
401 return ERR_PTR(-ENOMEM);
403 if (type == hwmon_chip) {
406 scnprintf(hattr->name, sizeof(hattr->name), template,
407 index + hwmon_attr_base(type));
413 hattr->index = index;
416 dattr = &hattr->dev_attr;
417 dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
418 dattr->store = hwmon_attr_store;
429 * Chip attributes are not attribute templates but actual sysfs attributes.
430 * See hwmon_genattr() for special handling.
432 static const char * const hwmon_chip_attrs[] = {
433 [hwmon_chip_temp_reset_history] = "temp_reset_history",
434 [hwmon_chip_in_reset_history] = "in_reset_history",
435 [hwmon_chip_curr_reset_history] = "curr_reset_history",
436 [hwmon_chip_power_reset_history] = "power_reset_history",
437 [hwmon_chip_update_interval] = "update_interval",
438 [hwmon_chip_alarms] = "alarms",
439 [hwmon_chip_samples] = "samples",
440 [hwmon_chip_curr_samples] = "curr_samples",
441 [hwmon_chip_in_samples] = "in_samples",
442 [hwmon_chip_power_samples] = "power_samples",
443 [hwmon_chip_temp_samples] = "temp_samples",
446 static const char * const hwmon_temp_attr_templates[] = {
447 [hwmon_temp_enable] = "temp%d_enable",
448 [hwmon_temp_input] = "temp%d_input",
449 [hwmon_temp_type] = "temp%d_type",
450 [hwmon_temp_lcrit] = "temp%d_lcrit",
451 [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
452 [hwmon_temp_min] = "temp%d_min",
453 [hwmon_temp_min_hyst] = "temp%d_min_hyst",
454 [hwmon_temp_max] = "temp%d_max",
455 [hwmon_temp_max_hyst] = "temp%d_max_hyst",
456 [hwmon_temp_crit] = "temp%d_crit",
457 [hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
458 [hwmon_temp_emergency] = "temp%d_emergency",
459 [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
460 [hwmon_temp_alarm] = "temp%d_alarm",
461 [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
462 [hwmon_temp_min_alarm] = "temp%d_min_alarm",
463 [hwmon_temp_max_alarm] = "temp%d_max_alarm",
464 [hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
465 [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
466 [hwmon_temp_fault] = "temp%d_fault",
467 [hwmon_temp_offset] = "temp%d_offset",
468 [hwmon_temp_label] = "temp%d_label",
469 [hwmon_temp_lowest] = "temp%d_lowest",
470 [hwmon_temp_highest] = "temp%d_highest",
471 [hwmon_temp_reset_history] = "temp%d_reset_history",
472 [hwmon_temp_rated_min] = "temp%d_rated_min",
473 [hwmon_temp_rated_max] = "temp%d_rated_max",
476 static const char * const hwmon_in_attr_templates[] = {
477 [hwmon_in_enable] = "in%d_enable",
478 [hwmon_in_input] = "in%d_input",
479 [hwmon_in_min] = "in%d_min",
480 [hwmon_in_max] = "in%d_max",
481 [hwmon_in_lcrit] = "in%d_lcrit",
482 [hwmon_in_crit] = "in%d_crit",
483 [hwmon_in_average] = "in%d_average",
484 [hwmon_in_lowest] = "in%d_lowest",
485 [hwmon_in_highest] = "in%d_highest",
486 [hwmon_in_reset_history] = "in%d_reset_history",
487 [hwmon_in_label] = "in%d_label",
488 [hwmon_in_alarm] = "in%d_alarm",
489 [hwmon_in_min_alarm] = "in%d_min_alarm",
490 [hwmon_in_max_alarm] = "in%d_max_alarm",
491 [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
492 [hwmon_in_crit_alarm] = "in%d_crit_alarm",
493 [hwmon_in_rated_min] = "in%d_rated_min",
494 [hwmon_in_rated_max] = "in%d_rated_max",
497 static const char * const hwmon_curr_attr_templates[] = {
498 [hwmon_curr_enable] = "curr%d_enable",
499 [hwmon_curr_input] = "curr%d_input",
500 [hwmon_curr_min] = "curr%d_min",
501 [hwmon_curr_max] = "curr%d_max",
502 [hwmon_curr_lcrit] = "curr%d_lcrit",
503 [hwmon_curr_crit] = "curr%d_crit",
504 [hwmon_curr_average] = "curr%d_average",
505 [hwmon_curr_lowest] = "curr%d_lowest",
506 [hwmon_curr_highest] = "curr%d_highest",
507 [hwmon_curr_reset_history] = "curr%d_reset_history",
508 [hwmon_curr_label] = "curr%d_label",
509 [hwmon_curr_alarm] = "curr%d_alarm",
510 [hwmon_curr_min_alarm] = "curr%d_min_alarm",
511 [hwmon_curr_max_alarm] = "curr%d_max_alarm",
512 [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
513 [hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
514 [hwmon_curr_rated_min] = "curr%d_rated_min",
515 [hwmon_curr_rated_max] = "curr%d_rated_max",
518 static const char * const hwmon_power_attr_templates[] = {
519 [hwmon_power_enable] = "power%d_enable",
520 [hwmon_power_average] = "power%d_average",
521 [hwmon_power_average_interval] = "power%d_average_interval",
522 [hwmon_power_average_interval_max] = "power%d_interval_max",
523 [hwmon_power_average_interval_min] = "power%d_interval_min",
524 [hwmon_power_average_highest] = "power%d_average_highest",
525 [hwmon_power_average_lowest] = "power%d_average_lowest",
526 [hwmon_power_average_max] = "power%d_average_max",
527 [hwmon_power_average_min] = "power%d_average_min",
528 [hwmon_power_input] = "power%d_input",
529 [hwmon_power_input_highest] = "power%d_input_highest",
530 [hwmon_power_input_lowest] = "power%d_input_lowest",
531 [hwmon_power_reset_history] = "power%d_reset_history",
532 [hwmon_power_accuracy] = "power%d_accuracy",
533 [hwmon_power_cap] = "power%d_cap",
534 [hwmon_power_cap_hyst] = "power%d_cap_hyst",
535 [hwmon_power_cap_max] = "power%d_cap_max",
536 [hwmon_power_cap_min] = "power%d_cap_min",
537 [hwmon_power_min] = "power%d_min",
538 [hwmon_power_max] = "power%d_max",
539 [hwmon_power_lcrit] = "power%d_lcrit",
540 [hwmon_power_crit] = "power%d_crit",
541 [hwmon_power_label] = "power%d_label",
542 [hwmon_power_alarm] = "power%d_alarm",
543 [hwmon_power_cap_alarm] = "power%d_cap_alarm",
544 [hwmon_power_min_alarm] = "power%d_min_alarm",
545 [hwmon_power_max_alarm] = "power%d_max_alarm",
546 [hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
547 [hwmon_power_crit_alarm] = "power%d_crit_alarm",
548 [hwmon_power_rated_min] = "power%d_rated_min",
549 [hwmon_power_rated_max] = "power%d_rated_max",
552 static const char * const hwmon_energy_attr_templates[] = {
553 [hwmon_energy_enable] = "energy%d_enable",
554 [hwmon_energy_input] = "energy%d_input",
555 [hwmon_energy_label] = "energy%d_label",
558 static const char * const hwmon_humidity_attr_templates[] = {
559 [hwmon_humidity_enable] = "humidity%d_enable",
560 [hwmon_humidity_input] = "humidity%d_input",
561 [hwmon_humidity_label] = "humidity%d_label",
562 [hwmon_humidity_min] = "humidity%d_min",
563 [hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
564 [hwmon_humidity_max] = "humidity%d_max",
565 [hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
566 [hwmon_humidity_alarm] = "humidity%d_alarm",
567 [hwmon_humidity_fault] = "humidity%d_fault",
568 [hwmon_humidity_rated_min] = "humidity%d_rated_min",
569 [hwmon_humidity_rated_max] = "humidity%d_rated_max",
572 static const char * const hwmon_fan_attr_templates[] = {
573 [hwmon_fan_enable] = "fan%d_enable",
574 [hwmon_fan_input] = "fan%d_input",
575 [hwmon_fan_label] = "fan%d_label",
576 [hwmon_fan_min] = "fan%d_min",
577 [hwmon_fan_max] = "fan%d_max",
578 [hwmon_fan_div] = "fan%d_div",
579 [hwmon_fan_pulses] = "fan%d_pulses",
580 [hwmon_fan_target] = "fan%d_target",
581 [hwmon_fan_alarm] = "fan%d_alarm",
582 [hwmon_fan_min_alarm] = "fan%d_min_alarm",
583 [hwmon_fan_max_alarm] = "fan%d_max_alarm",
584 [hwmon_fan_fault] = "fan%d_fault",
587 static const char * const hwmon_pwm_attr_templates[] = {
588 [hwmon_pwm_input] = "pwm%d",
589 [hwmon_pwm_enable] = "pwm%d_enable",
590 [hwmon_pwm_mode] = "pwm%d_mode",
591 [hwmon_pwm_freq] = "pwm%d_freq",
594 static const char * const hwmon_intrusion_attr_templates[] = {
595 [hwmon_intrusion_alarm] = "intrusion%d_alarm",
596 [hwmon_intrusion_beep] = "intrusion%d_beep",
599 static const char * const *__templates[] = {
600 [hwmon_chip] = hwmon_chip_attrs,
601 [hwmon_temp] = hwmon_temp_attr_templates,
602 [hwmon_in] = hwmon_in_attr_templates,
603 [hwmon_curr] = hwmon_curr_attr_templates,
604 [hwmon_power] = hwmon_power_attr_templates,
605 [hwmon_energy] = hwmon_energy_attr_templates,
606 [hwmon_humidity] = hwmon_humidity_attr_templates,
607 [hwmon_fan] = hwmon_fan_attr_templates,
608 [hwmon_pwm] = hwmon_pwm_attr_templates,
609 [hwmon_intrusion] = hwmon_intrusion_attr_templates,
612 static const int __templates_size[] = {
613 [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
614 [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
615 [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
616 [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
617 [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
618 [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
619 [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
620 [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
621 [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
622 [hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates),
625 int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type,
626 u32 attr, int channel)
628 char sattr[MAX_SYSFS_ATTR_NAME_LENGTH];
629 const char * const *templates;
630 const char *template;
633 if (type >= ARRAY_SIZE(__templates))
635 if (attr >= __templates_size[type])
638 templates = __templates[type];
639 template = templates[attr];
641 base = hwmon_attr_base(type);
643 scnprintf(sattr, MAX_SYSFS_ATTR_NAME_LENGTH, template, base + channel);
644 sysfs_notify(&dev->kobj, NULL, sattr);
645 kobject_uevent(&dev->kobj, KOBJ_CHANGE);
647 if (type == hwmon_temp)
648 hwmon_thermal_notify(dev, channel);
652 EXPORT_SYMBOL_GPL(hwmon_notify_event);
654 static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
658 for (i = n = 0; info->config[i]; i++)
659 n += hweight32(info->config[i]);
664 static int hwmon_genattrs(const void *drvdata,
665 struct attribute **attrs,
666 const struct hwmon_ops *ops,
667 const struct hwmon_channel_info *info)
669 const char * const *templates;
673 if (info->type >= ARRAY_SIZE(__templates))
676 templates = __templates[info->type];
677 template_size = __templates_size[info->type];
679 for (i = 0; info->config[i]; i++) {
680 u32 attr_mask = info->config[i];
686 attr = __ffs(attr_mask);
687 attr_mask &= ~BIT(attr);
688 if (attr >= template_size)
690 a = hwmon_genattr(drvdata, info->type, attr, i,
691 templates[attr], ops);
693 if (PTR_ERR(a) != -ENOENT)
703 static struct attribute **
704 __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip)
706 int ret, i, aindex = 0, nattrs = 0;
707 struct attribute **attrs;
709 for (i = 0; chip->info[i]; i++)
710 nattrs += hwmon_num_channel_attrs(chip->info[i]);
713 return ERR_PTR(-EINVAL);
715 attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL);
717 return ERR_PTR(-ENOMEM);
719 for (i = 0; chip->info[i]; i++) {
720 ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops,
723 hwmon_free_attrs(attrs);
732 static struct device *
733 __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
734 const struct hwmon_chip_info *chip,
735 const struct attribute_group **groups)
737 struct hwmon_device *hwdev;
741 /* Complain about invalid characters in hwmon name attribute */
742 if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
744 "hwmon: '%s' is not a valid name attribute, please fix\n",
747 id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
751 hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
760 struct attribute **attrs;
761 int ngroups = 2; /* terminating NULL plus &hwdev->groups */
764 for (i = 0; groups[i]; i++)
767 hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL);
768 if (!hwdev->groups) {
773 attrs = __hwmon_create_attrs(drvdata, chip);
775 err = PTR_ERR(attrs);
779 hwdev->group.attrs = attrs;
781 hwdev->groups[ngroups++] = &hwdev->group;
784 for (i = 0; groups[i]; i++)
785 hwdev->groups[ngroups++] = groups[i];
788 hdev->groups = hwdev->groups;
790 hdev->groups = groups;
794 hdev->class = &hwmon_class;
796 hdev->of_node = dev ? dev->of_node : NULL;
798 dev_set_drvdata(hdev, drvdata);
799 dev_set_name(hdev, HWMON_ID_FORMAT, id);
800 err = device_register(hdev);
806 INIT_LIST_HEAD(&hwdev->tzdata);
808 if (dev && dev->of_node && chip && chip->ops->read &&
809 chip->info[0]->type == hwmon_chip &&
810 (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
811 err = hwmon_thermal_register_sensors(hdev);
813 device_unregister(hdev);
815 * Don't worry about hwdev; hwmon_dev_release(), called
816 * from device_unregister(), will free it.
825 hwmon_dev_release(hdev);
827 ida_simple_remove(&hwmon_ida, id);
832 * hwmon_device_register_with_groups - register w/ hwmon
833 * @dev: the parent device
834 * @name: hwmon name attribute
835 * @drvdata: driver data to attach to created device
836 * @groups: List of attribute groups to create
838 * hwmon_device_unregister() must be called when the device is no
841 * Returns the pointer to the new device.
844 hwmon_device_register_with_groups(struct device *dev, const char *name,
846 const struct attribute_group **groups)
849 return ERR_PTR(-EINVAL);
851 return __hwmon_device_register(dev, name, drvdata, NULL, groups);
853 EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
856 * hwmon_device_register_with_info - register w/ hwmon
857 * @dev: the parent device
858 * @name: hwmon name attribute
859 * @drvdata: driver data to attach to created device
860 * @chip: pointer to hwmon chip information
861 * @extra_groups: pointer to list of additional non-standard attribute groups
863 * hwmon_device_unregister() must be called when the device is no
866 * Returns the pointer to the new device.
869 hwmon_device_register_with_info(struct device *dev, const char *name,
871 const struct hwmon_chip_info *chip,
872 const struct attribute_group **extra_groups)
875 return ERR_PTR(-EINVAL);
877 if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info))
878 return ERR_PTR(-EINVAL);
881 return ERR_PTR(-EINVAL);
883 return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
885 EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
888 * hwmon_device_register - register w/ hwmon
889 * @dev: the device to register
891 * hwmon_device_unregister() must be called when the device is no
894 * Returns the pointer to the new device.
896 struct device *hwmon_device_register(struct device *dev)
899 "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
901 return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
903 EXPORT_SYMBOL_GPL(hwmon_device_register);
906 * hwmon_device_unregister - removes the previously registered class device
908 * @dev: the class device to destroy
910 void hwmon_device_unregister(struct device *dev)
914 if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
915 device_unregister(dev);
916 ida_simple_remove(&hwmon_ida, id);
919 "hwmon_device_unregister() failed: bad class ID!\n");
921 EXPORT_SYMBOL_GPL(hwmon_device_unregister);
923 static void devm_hwmon_release(struct device *dev, void *res)
925 struct device *hwdev = *(struct device **)res;
927 hwmon_device_unregister(hwdev);
931 * devm_hwmon_device_register_with_groups - register w/ hwmon
932 * @dev: the parent device
933 * @name: hwmon name attribute
934 * @drvdata: driver data to attach to created device
935 * @groups: List of attribute groups to create
937 * Returns the pointer to the new device. The new device is automatically
938 * unregistered with the parent device.
941 devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
943 const struct attribute_group **groups)
945 struct device **ptr, *hwdev;
948 return ERR_PTR(-EINVAL);
950 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
952 return ERR_PTR(-ENOMEM);
954 hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
959 devres_add(dev, ptr);
966 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
969 * devm_hwmon_device_register_with_info - register w/ hwmon
970 * @dev: the parent device
971 * @name: hwmon name attribute
972 * @drvdata: driver data to attach to created device
973 * @chip: pointer to hwmon chip information
974 * @groups: pointer to list of driver specific attribute groups
976 * Returns the pointer to the new device. The new device is automatically
977 * unregistered with the parent device.
980 devm_hwmon_device_register_with_info(struct device *dev, const char *name,
982 const struct hwmon_chip_info *chip,
983 const struct attribute_group **groups)
985 struct device **ptr, *hwdev;
988 return ERR_PTR(-EINVAL);
990 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
992 return ERR_PTR(-ENOMEM);
994 hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
1000 devres_add(dev, ptr);
1008 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
1010 static int devm_hwmon_match(struct device *dev, void *res, void *data)
1012 struct device **hwdev = res;
1014 return *hwdev == data;
1018 * devm_hwmon_device_unregister - removes a previously registered hwmon device
1020 * @dev: the parent device of the device to unregister
1022 void devm_hwmon_device_unregister(struct device *dev)
1024 WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
1026 EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
1028 static void __init hwmon_pci_quirks(void)
1030 #if defined CONFIG_X86 && defined CONFIG_PCI
1035 /* Open access to 0x295-0x296 on MSI MS-7031 */
1036 sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
1038 if (sb->subsystem_vendor == 0x1462 && /* MSI */
1039 sb->subsystem_device == 0x0031) { /* MS-7031 */
1040 pci_read_config_byte(sb, 0x48, &enable);
1041 pci_read_config_word(sb, 0x64, &base);
1043 if (base == 0 && !(enable & BIT(2))) {
1045 "Opening wide generic port at 0x295\n");
1046 pci_write_config_word(sb, 0x64, 0x295);
1047 pci_write_config_byte(sb, 0x48,
1056 static int __init hwmon_init(void)
1062 err = class_register(&hwmon_class);
1064 pr_err("couldn't register hwmon sysfs class\n");
1070 static void __exit hwmon_exit(void)
1072 class_unregister(&hwmon_class);
1075 subsys_initcall(hwmon_init);
1076 module_exit(hwmon_exit);
1078 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
1079 MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
1080 MODULE_LICENSE("GPL");