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 = container_of(kobj, struct device, 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 const struct thermal_zone_of_device_ops hwmon_thermal_ops = {
157 .get_temp = hwmon_thermal_get_temp,
160 static void hwmon_thermal_remove_sensor(void *data)
165 static int hwmon_thermal_add_sensor(struct device *dev, int index)
167 struct hwmon_device *hwdev = to_hwmon_device(dev);
168 struct hwmon_thermal_data *tdata;
169 struct thermal_zone_device *tzd;
172 tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
177 tdata->index = index;
179 tzd = devm_thermal_zone_of_sensor_register(dev, index, tdata,
182 * If CONFIG_THERMAL_OF is disabled, this returns -ENODEV,
183 * so ignore that error but forward any other error.
185 if (IS_ERR(tzd) && (PTR_ERR(tzd) != -ENODEV))
188 err = devm_add_action(dev, hwmon_thermal_remove_sensor, &tdata->node);
193 list_add(&tdata->node, &hwdev->tzdata);
198 static int hwmon_thermal_register_sensors(struct device *dev)
200 struct hwmon_device *hwdev = to_hwmon_device(dev);
201 const struct hwmon_chip_info *chip = hwdev->chip;
202 const struct hwmon_channel_info **info = chip->info;
203 void *drvdata = dev_get_drvdata(dev);
206 for (i = 1; info[i]; i++) {
209 if (info[i]->type != hwmon_temp)
212 for (j = 0; info[i]->config[j]; j++) {
215 if (!(info[i]->config[j] & HWMON_T_INPUT) ||
216 !chip->ops->is_visible(drvdata, hwmon_temp,
217 hwmon_temp_input, j))
220 err = hwmon_thermal_add_sensor(dev, j);
229 static void hwmon_thermal_notify(struct device *dev, int index)
231 struct hwmon_device *hwdev = to_hwmon_device(dev);
232 struct hwmon_thermal_data *tzdata;
234 list_for_each_entry(tzdata, &hwdev->tzdata, node) {
235 if (tzdata->index == index) {
236 thermal_zone_device_update(tzdata->tzd,
237 THERMAL_EVENT_UNSPECIFIED);
243 static int hwmon_thermal_register_sensors(struct device *dev)
248 static void hwmon_thermal_notify(struct device *dev, int index) { }
250 #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
252 static int hwmon_attr_base(enum hwmon_sensor_types type)
254 if (type == hwmon_in || type == hwmon_intrusion)
259 /* sysfs attribute management */
261 static ssize_t hwmon_attr_show(struct device *dev,
262 struct device_attribute *devattr, char *buf)
264 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
268 ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
273 trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type),
276 return sprintf(buf, "%ld\n", val);
279 static ssize_t hwmon_attr_show_string(struct device *dev,
280 struct device_attribute *devattr,
283 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
284 enum hwmon_sensor_types type = hattr->type;
288 ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
293 trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type),
296 return sprintf(buf, "%s\n", s);
299 static ssize_t hwmon_attr_store(struct device *dev,
300 struct device_attribute *devattr,
301 const char *buf, size_t count)
303 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
307 ret = kstrtol(buf, 10, &val);
311 ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
316 trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type),
322 static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
324 return (type == hwmon_temp && attr == hwmon_temp_label) ||
325 (type == hwmon_in && attr == hwmon_in_label) ||
326 (type == hwmon_curr && attr == hwmon_curr_label) ||
327 (type == hwmon_power && attr == hwmon_power_label) ||
328 (type == hwmon_energy && attr == hwmon_energy_label) ||
329 (type == hwmon_humidity && attr == hwmon_humidity_label) ||
330 (type == hwmon_fan && attr == hwmon_fan_label);
333 static struct attribute *hwmon_genattr(const void *drvdata,
334 enum hwmon_sensor_types type,
337 const char *template,
338 const struct hwmon_ops *ops)
340 struct hwmon_device_attribute *hattr;
341 struct device_attribute *dattr;
345 bool is_string = is_string_attr(type, attr);
347 /* The attribute is invisible if there is no template string */
349 return ERR_PTR(-ENOENT);
351 mode = ops->is_visible(drvdata, type, attr, index);
353 return ERR_PTR(-ENOENT);
355 if ((mode & 0444) && ((is_string && !ops->read_string) ||
356 (!is_string && !ops->read)))
357 return ERR_PTR(-EINVAL);
358 if ((mode & 0222) && !ops->write)
359 return ERR_PTR(-EINVAL);
361 hattr = kzalloc(sizeof(*hattr), GFP_KERNEL);
363 return ERR_PTR(-ENOMEM);
365 if (type == hwmon_chip) {
368 scnprintf(hattr->name, sizeof(hattr->name), template,
369 index + hwmon_attr_base(type));
375 hattr->index = index;
378 dattr = &hattr->dev_attr;
379 dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
380 dattr->store = hwmon_attr_store;
391 * Chip attributes are not attribute templates but actual sysfs attributes.
392 * See hwmon_genattr() for special handling.
394 static const char * const hwmon_chip_attrs[] = {
395 [hwmon_chip_temp_reset_history] = "temp_reset_history",
396 [hwmon_chip_in_reset_history] = "in_reset_history",
397 [hwmon_chip_curr_reset_history] = "curr_reset_history",
398 [hwmon_chip_power_reset_history] = "power_reset_history",
399 [hwmon_chip_update_interval] = "update_interval",
400 [hwmon_chip_alarms] = "alarms",
401 [hwmon_chip_samples] = "samples",
402 [hwmon_chip_curr_samples] = "curr_samples",
403 [hwmon_chip_in_samples] = "in_samples",
404 [hwmon_chip_power_samples] = "power_samples",
405 [hwmon_chip_temp_samples] = "temp_samples",
408 static const char * const hwmon_temp_attr_templates[] = {
409 [hwmon_temp_enable] = "temp%d_enable",
410 [hwmon_temp_input] = "temp%d_input",
411 [hwmon_temp_type] = "temp%d_type",
412 [hwmon_temp_lcrit] = "temp%d_lcrit",
413 [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
414 [hwmon_temp_min] = "temp%d_min",
415 [hwmon_temp_min_hyst] = "temp%d_min_hyst",
416 [hwmon_temp_max] = "temp%d_max",
417 [hwmon_temp_max_hyst] = "temp%d_max_hyst",
418 [hwmon_temp_crit] = "temp%d_crit",
419 [hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
420 [hwmon_temp_emergency] = "temp%d_emergency",
421 [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
422 [hwmon_temp_alarm] = "temp%d_alarm",
423 [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
424 [hwmon_temp_min_alarm] = "temp%d_min_alarm",
425 [hwmon_temp_max_alarm] = "temp%d_max_alarm",
426 [hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
427 [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
428 [hwmon_temp_fault] = "temp%d_fault",
429 [hwmon_temp_offset] = "temp%d_offset",
430 [hwmon_temp_label] = "temp%d_label",
431 [hwmon_temp_lowest] = "temp%d_lowest",
432 [hwmon_temp_highest] = "temp%d_highest",
433 [hwmon_temp_reset_history] = "temp%d_reset_history",
434 [hwmon_temp_rated_min] = "temp%d_rated_min",
435 [hwmon_temp_rated_max] = "temp%d_rated_max",
438 static const char * const hwmon_in_attr_templates[] = {
439 [hwmon_in_enable] = "in%d_enable",
440 [hwmon_in_input] = "in%d_input",
441 [hwmon_in_min] = "in%d_min",
442 [hwmon_in_max] = "in%d_max",
443 [hwmon_in_lcrit] = "in%d_lcrit",
444 [hwmon_in_crit] = "in%d_crit",
445 [hwmon_in_average] = "in%d_average",
446 [hwmon_in_lowest] = "in%d_lowest",
447 [hwmon_in_highest] = "in%d_highest",
448 [hwmon_in_reset_history] = "in%d_reset_history",
449 [hwmon_in_label] = "in%d_label",
450 [hwmon_in_alarm] = "in%d_alarm",
451 [hwmon_in_min_alarm] = "in%d_min_alarm",
452 [hwmon_in_max_alarm] = "in%d_max_alarm",
453 [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
454 [hwmon_in_crit_alarm] = "in%d_crit_alarm",
455 [hwmon_in_rated_min] = "in%d_rated_min",
456 [hwmon_in_rated_max] = "in%d_rated_max",
459 static const char * const hwmon_curr_attr_templates[] = {
460 [hwmon_curr_enable] = "curr%d_enable",
461 [hwmon_curr_input] = "curr%d_input",
462 [hwmon_curr_min] = "curr%d_min",
463 [hwmon_curr_max] = "curr%d_max",
464 [hwmon_curr_lcrit] = "curr%d_lcrit",
465 [hwmon_curr_crit] = "curr%d_crit",
466 [hwmon_curr_average] = "curr%d_average",
467 [hwmon_curr_lowest] = "curr%d_lowest",
468 [hwmon_curr_highest] = "curr%d_highest",
469 [hwmon_curr_reset_history] = "curr%d_reset_history",
470 [hwmon_curr_label] = "curr%d_label",
471 [hwmon_curr_alarm] = "curr%d_alarm",
472 [hwmon_curr_min_alarm] = "curr%d_min_alarm",
473 [hwmon_curr_max_alarm] = "curr%d_max_alarm",
474 [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
475 [hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
476 [hwmon_curr_rated_min] = "curr%d_rated_min",
477 [hwmon_curr_rated_max] = "curr%d_rated_max",
480 static const char * const hwmon_power_attr_templates[] = {
481 [hwmon_power_enable] = "power%d_enable",
482 [hwmon_power_average] = "power%d_average",
483 [hwmon_power_average_interval] = "power%d_average_interval",
484 [hwmon_power_average_interval_max] = "power%d_interval_max",
485 [hwmon_power_average_interval_min] = "power%d_interval_min",
486 [hwmon_power_average_highest] = "power%d_average_highest",
487 [hwmon_power_average_lowest] = "power%d_average_lowest",
488 [hwmon_power_average_max] = "power%d_average_max",
489 [hwmon_power_average_min] = "power%d_average_min",
490 [hwmon_power_input] = "power%d_input",
491 [hwmon_power_input_highest] = "power%d_input_highest",
492 [hwmon_power_input_lowest] = "power%d_input_lowest",
493 [hwmon_power_reset_history] = "power%d_reset_history",
494 [hwmon_power_accuracy] = "power%d_accuracy",
495 [hwmon_power_cap] = "power%d_cap",
496 [hwmon_power_cap_hyst] = "power%d_cap_hyst",
497 [hwmon_power_cap_max] = "power%d_cap_max",
498 [hwmon_power_cap_min] = "power%d_cap_min",
499 [hwmon_power_min] = "power%d_min",
500 [hwmon_power_max] = "power%d_max",
501 [hwmon_power_lcrit] = "power%d_lcrit",
502 [hwmon_power_crit] = "power%d_crit",
503 [hwmon_power_label] = "power%d_label",
504 [hwmon_power_alarm] = "power%d_alarm",
505 [hwmon_power_cap_alarm] = "power%d_cap_alarm",
506 [hwmon_power_min_alarm] = "power%d_min_alarm",
507 [hwmon_power_max_alarm] = "power%d_max_alarm",
508 [hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
509 [hwmon_power_crit_alarm] = "power%d_crit_alarm",
510 [hwmon_power_rated_min] = "power%d_rated_min",
511 [hwmon_power_rated_max] = "power%d_rated_max",
514 static const char * const hwmon_energy_attr_templates[] = {
515 [hwmon_energy_enable] = "energy%d_enable",
516 [hwmon_energy_input] = "energy%d_input",
517 [hwmon_energy_label] = "energy%d_label",
520 static const char * const hwmon_humidity_attr_templates[] = {
521 [hwmon_humidity_enable] = "humidity%d_enable",
522 [hwmon_humidity_input] = "humidity%d_input",
523 [hwmon_humidity_label] = "humidity%d_label",
524 [hwmon_humidity_min] = "humidity%d_min",
525 [hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
526 [hwmon_humidity_max] = "humidity%d_max",
527 [hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
528 [hwmon_humidity_alarm] = "humidity%d_alarm",
529 [hwmon_humidity_fault] = "humidity%d_fault",
530 [hwmon_humidity_rated_min] = "humidity%d_rated_min",
531 [hwmon_humidity_rated_max] = "humidity%d_rated_max",
534 static const char * const hwmon_fan_attr_templates[] = {
535 [hwmon_fan_enable] = "fan%d_enable",
536 [hwmon_fan_input] = "fan%d_input",
537 [hwmon_fan_label] = "fan%d_label",
538 [hwmon_fan_min] = "fan%d_min",
539 [hwmon_fan_max] = "fan%d_max",
540 [hwmon_fan_div] = "fan%d_div",
541 [hwmon_fan_pulses] = "fan%d_pulses",
542 [hwmon_fan_target] = "fan%d_target",
543 [hwmon_fan_alarm] = "fan%d_alarm",
544 [hwmon_fan_min_alarm] = "fan%d_min_alarm",
545 [hwmon_fan_max_alarm] = "fan%d_max_alarm",
546 [hwmon_fan_fault] = "fan%d_fault",
549 static const char * const hwmon_pwm_attr_templates[] = {
550 [hwmon_pwm_input] = "pwm%d",
551 [hwmon_pwm_enable] = "pwm%d_enable",
552 [hwmon_pwm_mode] = "pwm%d_mode",
553 [hwmon_pwm_freq] = "pwm%d_freq",
556 static const char * const hwmon_intrusion_attr_templates[] = {
557 [hwmon_intrusion_alarm] = "intrusion%d_alarm",
558 [hwmon_intrusion_beep] = "intrusion%d_beep",
561 static const char * const *__templates[] = {
562 [hwmon_chip] = hwmon_chip_attrs,
563 [hwmon_temp] = hwmon_temp_attr_templates,
564 [hwmon_in] = hwmon_in_attr_templates,
565 [hwmon_curr] = hwmon_curr_attr_templates,
566 [hwmon_power] = hwmon_power_attr_templates,
567 [hwmon_energy] = hwmon_energy_attr_templates,
568 [hwmon_humidity] = hwmon_humidity_attr_templates,
569 [hwmon_fan] = hwmon_fan_attr_templates,
570 [hwmon_pwm] = hwmon_pwm_attr_templates,
571 [hwmon_intrusion] = hwmon_intrusion_attr_templates,
574 static const int __templates_size[] = {
575 [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
576 [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
577 [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
578 [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
579 [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
580 [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
581 [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
582 [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
583 [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
584 [hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates),
587 int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type,
588 u32 attr, int channel)
590 char sattr[MAX_SYSFS_ATTR_NAME_LENGTH];
591 const char * const *templates;
592 const char *template;
595 if (type >= ARRAY_SIZE(__templates))
597 if (attr >= __templates_size[type])
600 templates = __templates[type];
601 template = templates[attr];
603 base = hwmon_attr_base(type);
605 scnprintf(sattr, MAX_SYSFS_ATTR_NAME_LENGTH, template, base + channel);
606 sysfs_notify(&dev->kobj, NULL, sattr);
607 kobject_uevent(&dev->kobj, KOBJ_CHANGE);
609 if (type == hwmon_temp)
610 hwmon_thermal_notify(dev, channel);
614 EXPORT_SYMBOL_GPL(hwmon_notify_event);
616 static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
620 for (i = n = 0; info->config[i]; i++)
621 n += hweight32(info->config[i]);
626 static int hwmon_genattrs(const void *drvdata,
627 struct attribute **attrs,
628 const struct hwmon_ops *ops,
629 const struct hwmon_channel_info *info)
631 const char * const *templates;
635 if (info->type >= ARRAY_SIZE(__templates))
638 templates = __templates[info->type];
639 template_size = __templates_size[info->type];
641 for (i = 0; info->config[i]; i++) {
642 u32 attr_mask = info->config[i];
648 attr = __ffs(attr_mask);
649 attr_mask &= ~BIT(attr);
650 if (attr >= template_size)
652 a = hwmon_genattr(drvdata, info->type, attr, i,
653 templates[attr], ops);
655 if (PTR_ERR(a) != -ENOENT)
665 static struct attribute **
666 __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip)
668 int ret, i, aindex = 0, nattrs = 0;
669 struct attribute **attrs;
671 for (i = 0; chip->info[i]; i++)
672 nattrs += hwmon_num_channel_attrs(chip->info[i]);
675 return ERR_PTR(-EINVAL);
677 attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL);
679 return ERR_PTR(-ENOMEM);
681 for (i = 0; chip->info[i]; i++) {
682 ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops,
685 hwmon_free_attrs(attrs);
694 static struct device *
695 __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
696 const struct hwmon_chip_info *chip,
697 const struct attribute_group **groups)
699 struct hwmon_device *hwdev;
703 /* Complain about invalid characters in hwmon name attribute */
704 if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
706 "hwmon: '%s' is not a valid name attribute, please fix\n",
709 id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
713 hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
722 struct attribute **attrs;
723 int ngroups = 2; /* terminating NULL plus &hwdev->groups */
726 for (i = 0; groups[i]; i++)
729 hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL);
730 if (!hwdev->groups) {
735 attrs = __hwmon_create_attrs(drvdata, chip);
737 err = PTR_ERR(attrs);
741 hwdev->group.attrs = attrs;
743 hwdev->groups[ngroups++] = &hwdev->group;
746 for (i = 0; groups[i]; i++)
747 hwdev->groups[ngroups++] = groups[i];
750 hdev->groups = hwdev->groups;
752 hdev->groups = groups;
756 hdev->class = &hwmon_class;
758 hdev->of_node = dev ? dev->of_node : NULL;
760 dev_set_drvdata(hdev, drvdata);
761 dev_set_name(hdev, HWMON_ID_FORMAT, id);
762 err = device_register(hdev);
766 INIT_LIST_HEAD(&hwdev->tzdata);
768 if (dev && dev->of_node && chip && chip->ops->read &&
769 chip->info[0]->type == hwmon_chip &&
770 (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
771 err = hwmon_thermal_register_sensors(hdev);
773 device_unregister(hdev);
775 * Don't worry about hwdev; hwmon_dev_release(), called
776 * from device_unregister(), will free it.
785 hwmon_dev_release(hdev);
787 ida_simple_remove(&hwmon_ida, id);
792 * hwmon_device_register_with_groups - register w/ hwmon
793 * @dev: the parent device
794 * @name: hwmon name attribute
795 * @drvdata: driver data to attach to created device
796 * @groups: List of attribute groups to create
798 * hwmon_device_unregister() must be called when the device is no
801 * Returns the pointer to the new device.
804 hwmon_device_register_with_groups(struct device *dev, const char *name,
806 const struct attribute_group **groups)
809 return ERR_PTR(-EINVAL);
811 return __hwmon_device_register(dev, name, drvdata, NULL, groups);
813 EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
816 * hwmon_device_register_with_info - register w/ hwmon
817 * @dev: the parent device
818 * @name: hwmon name attribute
819 * @drvdata: driver data to attach to created device
820 * @chip: pointer to hwmon chip information
821 * @extra_groups: pointer to list of additional non-standard attribute groups
823 * hwmon_device_unregister() must be called when the device is no
826 * Returns the pointer to the new device.
829 hwmon_device_register_with_info(struct device *dev, const char *name,
831 const struct hwmon_chip_info *chip,
832 const struct attribute_group **extra_groups)
835 return ERR_PTR(-EINVAL);
837 if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info))
838 return ERR_PTR(-EINVAL);
841 return ERR_PTR(-EINVAL);
843 return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
845 EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
848 * hwmon_device_register - register w/ hwmon
849 * @dev: the device to register
851 * hwmon_device_unregister() must be called when the device is no
854 * Returns the pointer to the new device.
856 struct device *hwmon_device_register(struct device *dev)
859 "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
861 return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
863 EXPORT_SYMBOL_GPL(hwmon_device_register);
866 * hwmon_device_unregister - removes the previously registered class device
868 * @dev: the class device to destroy
870 void hwmon_device_unregister(struct device *dev)
874 if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
875 device_unregister(dev);
876 ida_simple_remove(&hwmon_ida, id);
879 "hwmon_device_unregister() failed: bad class ID!\n");
881 EXPORT_SYMBOL_GPL(hwmon_device_unregister);
883 static void devm_hwmon_release(struct device *dev, void *res)
885 struct device *hwdev = *(struct device **)res;
887 hwmon_device_unregister(hwdev);
891 * devm_hwmon_device_register_with_groups - register w/ hwmon
892 * @dev: the parent device
893 * @name: hwmon name attribute
894 * @drvdata: driver data to attach to created device
895 * @groups: List of attribute groups to create
897 * Returns the pointer to the new device. The new device is automatically
898 * unregistered with the parent device.
901 devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
903 const struct attribute_group **groups)
905 struct device **ptr, *hwdev;
908 return ERR_PTR(-EINVAL);
910 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
912 return ERR_PTR(-ENOMEM);
914 hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
919 devres_add(dev, ptr);
926 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
929 * devm_hwmon_device_register_with_info - register w/ hwmon
930 * @dev: the parent device
931 * @name: hwmon name attribute
932 * @drvdata: driver data to attach to created device
933 * @chip: pointer to hwmon chip information
934 * @groups: pointer to list of driver specific attribute groups
936 * Returns the pointer to the new device. The new device is automatically
937 * unregistered with the parent device.
940 devm_hwmon_device_register_with_info(struct device *dev, const char *name,
942 const struct hwmon_chip_info *chip,
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_info(dev, name, drvdata, chip,
960 devres_add(dev, ptr);
968 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
970 static int devm_hwmon_match(struct device *dev, void *res, void *data)
972 struct device **hwdev = res;
974 return *hwdev == data;
978 * devm_hwmon_device_unregister - removes a previously registered hwmon device
980 * @dev: the parent device of the device to unregister
982 void devm_hwmon_device_unregister(struct device *dev)
984 WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
986 EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
988 static void __init hwmon_pci_quirks(void)
990 #if defined CONFIG_X86 && defined CONFIG_PCI
995 /* Open access to 0x295-0x296 on MSI MS-7031 */
996 sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
998 if (sb->subsystem_vendor == 0x1462 && /* MSI */
999 sb->subsystem_device == 0x0031) { /* MS-7031 */
1000 pci_read_config_byte(sb, 0x48, &enable);
1001 pci_read_config_word(sb, 0x64, &base);
1003 if (base == 0 && !(enable & BIT(2))) {
1005 "Opening wide generic port at 0x295\n");
1006 pci_write_config_word(sb, 0x64, 0x295);
1007 pci_write_config_byte(sb, 0x48,
1016 static int __init hwmon_init(void)
1022 err = class_register(&hwmon_class);
1024 pr_err("couldn't register hwmon sysfs class\n");
1030 static void __exit hwmon_exit(void)
1032 class_unregister(&hwmon_class);
1035 subsys_initcall(hwmon_init);
1036 module_exit(hwmon_exit);
1038 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
1039 MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
1040 MODULE_LICENSE("GPL");