2 * hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring
4 * This file defines the sysfs class "hwmon", for use by sensors drivers.
6 * Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/bitops.h>
16 #include <linux/device.h>
17 #include <linux/err.h>
18 #include <linux/gfp.h>
19 #include <linux/hwmon.h>
20 #include <linux/idr.h>
21 #include <linux/module.h>
22 #include <linux/pci.h>
23 #include <linux/slab.h>
24 #include <linux/string.h>
25 #include <linux/thermal.h>
27 #define CREATE_TRACE_POINTS
28 #include <trace/events/hwmon.h>
30 #define HWMON_ID_PREFIX "hwmon"
31 #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
36 const struct hwmon_chip_info *chip;
38 struct attribute_group group;
39 const struct attribute_group **groups;
42 #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
44 #define MAX_SYSFS_ATTR_NAME_LENGTH 32
46 struct hwmon_device_attribute {
47 struct device_attribute dev_attr;
48 const struct hwmon_ops *ops;
49 enum hwmon_sensor_types type;
52 char name[MAX_SYSFS_ATTR_NAME_LENGTH];
55 #define to_hwmon_attr(d) \
56 container_of(d, struct hwmon_device_attribute, dev_attr)
59 * Thermal zone information
60 * In addition to the reference to the hwmon device,
61 * also provides the sensor index.
63 struct hwmon_thermal_data {
64 struct hwmon_device *hwdev; /* Reference to hwmon device */
65 int index; /* sensor index */
69 name_show(struct device *dev, struct device_attribute *attr, char *buf)
71 return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
73 static DEVICE_ATTR_RO(name);
75 static struct attribute *hwmon_dev_attrs[] = {
80 static umode_t hwmon_dev_name_is_visible(struct kobject *kobj,
81 struct attribute *attr, int n)
83 struct device *dev = container_of(kobj, struct device, kobj);
85 if (to_hwmon_device(dev)->name == NULL)
91 static const struct attribute_group hwmon_dev_attr_group = {
92 .attrs = hwmon_dev_attrs,
93 .is_visible = hwmon_dev_name_is_visible,
96 static const struct attribute_group *hwmon_dev_attr_groups[] = {
97 &hwmon_dev_attr_group,
101 static void hwmon_dev_release(struct device *dev)
103 kfree(to_hwmon_device(dev));
106 static struct class hwmon_class = {
108 .owner = THIS_MODULE,
109 .dev_groups = hwmon_dev_attr_groups,
110 .dev_release = hwmon_dev_release,
113 static DEFINE_IDA(hwmon_ida);
115 /* Thermal zone handling */
118 * The complex conditional is necessary to avoid a cyclic dependency
119 * between hwmon and thermal_sys modules.
121 #ifdef CONFIG_THERMAL_OF
122 static int hwmon_thermal_get_temp(void *data, int *temp)
124 struct hwmon_thermal_data *tdata = data;
125 struct hwmon_device *hwdev = tdata->hwdev;
129 ret = hwdev->chip->ops->read(&hwdev->dev, hwmon_temp, hwmon_temp_input,
139 static const struct thermal_zone_of_device_ops hwmon_thermal_ops = {
140 .get_temp = hwmon_thermal_get_temp,
143 static int hwmon_thermal_add_sensor(struct device *dev,
144 struct hwmon_device *hwdev, int index)
146 struct hwmon_thermal_data *tdata;
147 struct thermal_zone_device *tzd;
149 tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
153 tdata->hwdev = hwdev;
154 tdata->index = index;
156 tzd = devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
159 * If CONFIG_THERMAL_OF is disabled, this returns -ENODEV,
160 * so ignore that error but forward any other error.
162 if (IS_ERR(tzd) && (PTR_ERR(tzd) != -ENODEV))
168 static int hwmon_thermal_add_sensor(struct device *dev,
169 struct hwmon_device *hwdev, int index)
173 #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
175 static int hwmon_attr_base(enum hwmon_sensor_types type)
177 if (type == hwmon_in)
182 /* sysfs attribute management */
184 static ssize_t hwmon_attr_show(struct device *dev,
185 struct device_attribute *devattr, char *buf)
187 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
191 ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
196 trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type),
199 return sprintf(buf, "%ld\n", val);
202 static ssize_t hwmon_attr_show_string(struct device *dev,
203 struct device_attribute *devattr,
206 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
207 enum hwmon_sensor_types type = hattr->type;
211 ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
216 trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type),
219 return sprintf(buf, "%s\n", s);
222 static ssize_t hwmon_attr_store(struct device *dev,
223 struct device_attribute *devattr,
224 const char *buf, size_t count)
226 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
230 ret = kstrtol(buf, 10, &val);
234 ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
239 trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type),
245 static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
247 return (type == hwmon_temp && attr == hwmon_temp_label) ||
248 (type == hwmon_in && attr == hwmon_in_label) ||
249 (type == hwmon_curr && attr == hwmon_curr_label) ||
250 (type == hwmon_power && attr == hwmon_power_label) ||
251 (type == hwmon_energy && attr == hwmon_energy_label) ||
252 (type == hwmon_humidity && attr == hwmon_humidity_label) ||
253 (type == hwmon_fan && attr == hwmon_fan_label);
256 static struct attribute *hwmon_genattr(struct device *dev,
258 enum hwmon_sensor_types type,
261 const char *template,
262 const struct hwmon_ops *ops)
264 struct hwmon_device_attribute *hattr;
265 struct device_attribute *dattr;
269 bool is_string = is_string_attr(type, attr);
271 /* The attribute is invisible if there is no template string */
273 return ERR_PTR(-ENOENT);
275 mode = ops->is_visible(drvdata, type, attr, index);
277 return ERR_PTR(-ENOENT);
279 if ((mode & 0444) && ((is_string && !ops->read_string) ||
280 (!is_string && !ops->read)))
281 return ERR_PTR(-EINVAL);
282 if ((mode & 0222) && !ops->write)
283 return ERR_PTR(-EINVAL);
285 hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL);
287 return ERR_PTR(-ENOMEM);
289 if (type == hwmon_chip) {
292 scnprintf(hattr->name, sizeof(hattr->name), template,
293 index + hwmon_attr_base(type));
299 hattr->index = index;
302 dattr = &hattr->dev_attr;
303 dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
304 dattr->store = hwmon_attr_store;
315 * Chip attributes are not attribute templates but actual sysfs attributes.
316 * See hwmon_genattr() for special handling.
318 static const char * const hwmon_chip_attrs[] = {
319 [hwmon_chip_temp_reset_history] = "temp_reset_history",
320 [hwmon_chip_in_reset_history] = "in_reset_history",
321 [hwmon_chip_curr_reset_history] = "curr_reset_history",
322 [hwmon_chip_power_reset_history] = "power_reset_history",
323 [hwmon_chip_update_interval] = "update_interval",
324 [hwmon_chip_alarms] = "alarms",
325 [hwmon_chip_samples] = "samples",
326 [hwmon_chip_curr_samples] = "curr_samples",
327 [hwmon_chip_in_samples] = "in_samples",
328 [hwmon_chip_power_samples] = "power_samples",
329 [hwmon_chip_temp_samples] = "temp_samples",
332 static const char * const hwmon_temp_attr_templates[] = {
333 [hwmon_temp_input] = "temp%d_input",
334 [hwmon_temp_type] = "temp%d_type",
335 [hwmon_temp_lcrit] = "temp%d_lcrit",
336 [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
337 [hwmon_temp_min] = "temp%d_min",
338 [hwmon_temp_min_hyst] = "temp%d_min_hyst",
339 [hwmon_temp_max] = "temp%d_max",
340 [hwmon_temp_max_hyst] = "temp%d_max_hyst",
341 [hwmon_temp_crit] = "temp%d_crit",
342 [hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
343 [hwmon_temp_emergency] = "temp%d_emergency",
344 [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
345 [hwmon_temp_alarm] = "temp%d_alarm",
346 [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
347 [hwmon_temp_min_alarm] = "temp%d_min_alarm",
348 [hwmon_temp_max_alarm] = "temp%d_max_alarm",
349 [hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
350 [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
351 [hwmon_temp_fault] = "temp%d_fault",
352 [hwmon_temp_offset] = "temp%d_offset",
353 [hwmon_temp_label] = "temp%d_label",
354 [hwmon_temp_lowest] = "temp%d_lowest",
355 [hwmon_temp_highest] = "temp%d_highest",
356 [hwmon_temp_reset_history] = "temp%d_reset_history",
359 static const char * const hwmon_in_attr_templates[] = {
360 [hwmon_in_input] = "in%d_input",
361 [hwmon_in_min] = "in%d_min",
362 [hwmon_in_max] = "in%d_max",
363 [hwmon_in_lcrit] = "in%d_lcrit",
364 [hwmon_in_crit] = "in%d_crit",
365 [hwmon_in_average] = "in%d_average",
366 [hwmon_in_lowest] = "in%d_lowest",
367 [hwmon_in_highest] = "in%d_highest",
368 [hwmon_in_reset_history] = "in%d_reset_history",
369 [hwmon_in_label] = "in%d_label",
370 [hwmon_in_alarm] = "in%d_alarm",
371 [hwmon_in_min_alarm] = "in%d_min_alarm",
372 [hwmon_in_max_alarm] = "in%d_max_alarm",
373 [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
374 [hwmon_in_crit_alarm] = "in%d_crit_alarm",
375 [hwmon_in_enable] = "in%d_enable",
378 static const char * const hwmon_curr_attr_templates[] = {
379 [hwmon_curr_input] = "curr%d_input",
380 [hwmon_curr_min] = "curr%d_min",
381 [hwmon_curr_max] = "curr%d_max",
382 [hwmon_curr_lcrit] = "curr%d_lcrit",
383 [hwmon_curr_crit] = "curr%d_crit",
384 [hwmon_curr_average] = "curr%d_average",
385 [hwmon_curr_lowest] = "curr%d_lowest",
386 [hwmon_curr_highest] = "curr%d_highest",
387 [hwmon_curr_reset_history] = "curr%d_reset_history",
388 [hwmon_curr_label] = "curr%d_label",
389 [hwmon_curr_alarm] = "curr%d_alarm",
390 [hwmon_curr_min_alarm] = "curr%d_min_alarm",
391 [hwmon_curr_max_alarm] = "curr%d_max_alarm",
392 [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
393 [hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
396 static const char * const hwmon_power_attr_templates[] = {
397 [hwmon_power_average] = "power%d_average",
398 [hwmon_power_average_interval] = "power%d_average_interval",
399 [hwmon_power_average_interval_max] = "power%d_interval_max",
400 [hwmon_power_average_interval_min] = "power%d_interval_min",
401 [hwmon_power_average_highest] = "power%d_average_highest",
402 [hwmon_power_average_lowest] = "power%d_average_lowest",
403 [hwmon_power_average_max] = "power%d_average_max",
404 [hwmon_power_average_min] = "power%d_average_min",
405 [hwmon_power_input] = "power%d_input",
406 [hwmon_power_input_highest] = "power%d_input_highest",
407 [hwmon_power_input_lowest] = "power%d_input_lowest",
408 [hwmon_power_reset_history] = "power%d_reset_history",
409 [hwmon_power_accuracy] = "power%d_accuracy",
410 [hwmon_power_cap] = "power%d_cap",
411 [hwmon_power_cap_hyst] = "power%d_cap_hyst",
412 [hwmon_power_cap_max] = "power%d_cap_max",
413 [hwmon_power_cap_min] = "power%d_cap_min",
414 [hwmon_power_min] = "power%d_min",
415 [hwmon_power_max] = "power%d_max",
416 [hwmon_power_lcrit] = "power%d_lcrit",
417 [hwmon_power_crit] = "power%d_crit",
418 [hwmon_power_label] = "power%d_label",
419 [hwmon_power_alarm] = "power%d_alarm",
420 [hwmon_power_cap_alarm] = "power%d_cap_alarm",
421 [hwmon_power_min_alarm] = "power%d_min_alarm",
422 [hwmon_power_max_alarm] = "power%d_max_alarm",
423 [hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
424 [hwmon_power_crit_alarm] = "power%d_crit_alarm",
427 static const char * const hwmon_energy_attr_templates[] = {
428 [hwmon_energy_input] = "energy%d_input",
429 [hwmon_energy_label] = "energy%d_label",
432 static const char * const hwmon_humidity_attr_templates[] = {
433 [hwmon_humidity_input] = "humidity%d_input",
434 [hwmon_humidity_label] = "humidity%d_label",
435 [hwmon_humidity_min] = "humidity%d_min",
436 [hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
437 [hwmon_humidity_max] = "humidity%d_max",
438 [hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
439 [hwmon_humidity_alarm] = "humidity%d_alarm",
440 [hwmon_humidity_fault] = "humidity%d_fault",
443 static const char * const hwmon_fan_attr_templates[] = {
444 [hwmon_fan_input] = "fan%d_input",
445 [hwmon_fan_label] = "fan%d_label",
446 [hwmon_fan_min] = "fan%d_min",
447 [hwmon_fan_max] = "fan%d_max",
448 [hwmon_fan_div] = "fan%d_div",
449 [hwmon_fan_pulses] = "fan%d_pulses",
450 [hwmon_fan_target] = "fan%d_target",
451 [hwmon_fan_alarm] = "fan%d_alarm",
452 [hwmon_fan_min_alarm] = "fan%d_min_alarm",
453 [hwmon_fan_max_alarm] = "fan%d_max_alarm",
454 [hwmon_fan_fault] = "fan%d_fault",
457 static const char * const hwmon_pwm_attr_templates[] = {
458 [hwmon_pwm_input] = "pwm%d",
459 [hwmon_pwm_enable] = "pwm%d_enable",
460 [hwmon_pwm_mode] = "pwm%d_mode",
461 [hwmon_pwm_freq] = "pwm%d_freq",
464 static const char * const *__templates[] = {
465 [hwmon_chip] = hwmon_chip_attrs,
466 [hwmon_temp] = hwmon_temp_attr_templates,
467 [hwmon_in] = hwmon_in_attr_templates,
468 [hwmon_curr] = hwmon_curr_attr_templates,
469 [hwmon_power] = hwmon_power_attr_templates,
470 [hwmon_energy] = hwmon_energy_attr_templates,
471 [hwmon_humidity] = hwmon_humidity_attr_templates,
472 [hwmon_fan] = hwmon_fan_attr_templates,
473 [hwmon_pwm] = hwmon_pwm_attr_templates,
476 static const int __templates_size[] = {
477 [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
478 [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
479 [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
480 [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
481 [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
482 [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
483 [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
484 [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
485 [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
488 static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
492 for (i = n = 0; info->config[i]; i++)
493 n += hweight32(info->config[i]);
498 static int hwmon_genattrs(struct device *dev,
500 struct attribute **attrs,
501 const struct hwmon_ops *ops,
502 const struct hwmon_channel_info *info)
504 const char * const *templates;
508 if (info->type >= ARRAY_SIZE(__templates))
511 templates = __templates[info->type];
512 template_size = __templates_size[info->type];
514 for (i = 0; info->config[i]; i++) {
515 u32 attr_mask = info->config[i];
521 attr = __ffs(attr_mask);
522 attr_mask &= ~BIT(attr);
523 if (attr >= template_size)
525 a = hwmon_genattr(dev, drvdata, info->type, attr, i,
526 templates[attr], ops);
528 if (PTR_ERR(a) != -ENOENT)
538 static struct attribute **
539 __hwmon_create_attrs(struct device *dev, const void *drvdata,
540 const struct hwmon_chip_info *chip)
542 int ret, i, aindex = 0, nattrs = 0;
543 struct attribute **attrs;
545 for (i = 0; chip->info[i]; i++)
546 nattrs += hwmon_num_channel_attrs(chip->info[i]);
549 return ERR_PTR(-EINVAL);
551 attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL);
553 return ERR_PTR(-ENOMEM);
555 for (i = 0; chip->info[i]; i++) {
556 ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops,
566 static struct device *
567 __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
568 const struct hwmon_chip_info *chip,
569 const struct attribute_group **groups)
571 struct hwmon_device *hwdev;
575 /* Complain about invalid characters in hwmon name attribute */
576 if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
578 "hwmon: '%s' is not a valid name attribute, please fix\n",
581 id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
585 hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
594 struct attribute **attrs;
595 int ngroups = 2; /* terminating NULL plus &hwdev->groups */
598 for (i = 0; groups[i]; i++)
601 hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups),
603 if (!hwdev->groups) {
608 attrs = __hwmon_create_attrs(dev, drvdata, chip);
610 err = PTR_ERR(attrs);
614 hwdev->group.attrs = attrs;
616 hwdev->groups[ngroups++] = &hwdev->group;
619 for (i = 0; groups[i]; i++)
620 hwdev->groups[ngroups++] = groups[i];
623 hdev->groups = hwdev->groups;
625 hdev->groups = groups;
629 hdev->class = &hwmon_class;
631 hdev->of_node = dev ? dev->of_node : NULL;
633 dev_set_drvdata(hdev, drvdata);
634 dev_set_name(hdev, HWMON_ID_FORMAT, id);
635 err = device_register(hdev);
639 if (dev && chip && chip->ops->read &&
640 chip->info[0]->type == hwmon_chip &&
641 (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
642 const struct hwmon_channel_info **info = chip->info;
644 for (i = 1; info[i]; i++) {
645 if (info[i]->type != hwmon_temp)
648 for (j = 0; info[i]->config[j]; j++) {
649 if (!chip->ops->is_visible(drvdata, hwmon_temp,
650 hwmon_temp_input, j))
652 if (info[i]->config[j] & HWMON_T_INPUT) {
653 err = hwmon_thermal_add_sensor(dev,
656 device_unregister(hdev);
669 ida_simple_remove(&hwmon_ida, id);
674 * hwmon_device_register_with_groups - register w/ hwmon
675 * @dev: the parent device
676 * @name: hwmon name attribute
677 * @drvdata: driver data to attach to created device
678 * @groups: List of attribute groups to create
680 * hwmon_device_unregister() must be called when the device is no
683 * Returns the pointer to the new device.
686 hwmon_device_register_with_groups(struct device *dev, const char *name,
688 const struct attribute_group **groups)
691 return ERR_PTR(-EINVAL);
693 return __hwmon_device_register(dev, name, drvdata, NULL, groups);
695 EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
698 * hwmon_device_register_with_info - register w/ hwmon
699 * @dev: the parent device
700 * @name: hwmon name attribute
701 * @drvdata: driver data to attach to created device
702 * @chip: pointer to hwmon chip information
703 * @extra_groups: pointer to list of additional non-standard attribute groups
705 * hwmon_device_unregister() must be called when the device is no
708 * Returns the pointer to the new device.
711 hwmon_device_register_with_info(struct device *dev, const char *name,
713 const struct hwmon_chip_info *chip,
714 const struct attribute_group **extra_groups)
717 return ERR_PTR(-EINVAL);
719 if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info))
720 return ERR_PTR(-EINVAL);
723 return ERR_PTR(-EINVAL);
725 return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
727 EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
730 * hwmon_device_register - register w/ hwmon
731 * @dev: the device to register
733 * hwmon_device_unregister() must be called when the device is no
736 * Returns the pointer to the new device.
738 struct device *hwmon_device_register(struct device *dev)
741 "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
743 return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
745 EXPORT_SYMBOL_GPL(hwmon_device_register);
748 * hwmon_device_unregister - removes the previously registered class device
750 * @dev: the class device to destroy
752 void hwmon_device_unregister(struct device *dev)
756 if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
757 device_unregister(dev);
758 ida_simple_remove(&hwmon_ida, id);
761 "hwmon_device_unregister() failed: bad class ID!\n");
763 EXPORT_SYMBOL_GPL(hwmon_device_unregister);
765 static void devm_hwmon_release(struct device *dev, void *res)
767 struct device *hwdev = *(struct device **)res;
769 hwmon_device_unregister(hwdev);
773 * devm_hwmon_device_register_with_groups - register w/ hwmon
774 * @dev: the parent device
775 * @name: hwmon name attribute
776 * @drvdata: driver data to attach to created device
777 * @groups: List of attribute groups to create
779 * Returns the pointer to the new device. The new device is automatically
780 * unregistered with the parent device.
783 devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
785 const struct attribute_group **groups)
787 struct device **ptr, *hwdev;
790 return ERR_PTR(-EINVAL);
792 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
794 return ERR_PTR(-ENOMEM);
796 hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
801 devres_add(dev, ptr);
808 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
811 * devm_hwmon_device_register_with_info - register w/ hwmon
812 * @dev: the parent device
813 * @name: hwmon name attribute
814 * @drvdata: driver data to attach to created device
815 * @chip: pointer to hwmon chip information
816 * @groups: pointer to list of driver specific attribute groups
818 * Returns the pointer to the new device. The new device is automatically
819 * unregistered with the parent device.
822 devm_hwmon_device_register_with_info(struct device *dev, const char *name,
824 const struct hwmon_chip_info *chip,
825 const struct attribute_group **groups)
827 struct device **ptr, *hwdev;
830 return ERR_PTR(-EINVAL);
832 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
834 return ERR_PTR(-ENOMEM);
836 hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
842 devres_add(dev, ptr);
850 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
852 static int devm_hwmon_match(struct device *dev, void *res, void *data)
854 struct device **hwdev = res;
856 return *hwdev == data;
860 * devm_hwmon_device_unregister - removes a previously registered hwmon device
862 * @dev: the parent device of the device to unregister
864 void devm_hwmon_device_unregister(struct device *dev)
866 WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
868 EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
870 static void __init hwmon_pci_quirks(void)
872 #if defined CONFIG_X86 && defined CONFIG_PCI
877 /* Open access to 0x295-0x296 on MSI MS-7031 */
878 sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
880 if (sb->subsystem_vendor == 0x1462 && /* MSI */
881 sb->subsystem_device == 0x0031) { /* MS-7031 */
882 pci_read_config_byte(sb, 0x48, &enable);
883 pci_read_config_word(sb, 0x64, &base);
885 if (base == 0 && !(enable & BIT(2))) {
887 "Opening wide generic port at 0x295\n");
888 pci_write_config_word(sb, 0x64, 0x295);
889 pci_write_config_byte(sb, 0x48,
898 static int __init hwmon_init(void)
904 err = class_register(&hwmon_class);
906 pr_err("couldn't register hwmon sysfs class\n");
912 static void __exit hwmon_exit(void)
914 class_unregister(&hwmon_class);
917 subsys_initcall(hwmon_init);
918 module_exit(hwmon_exit);
920 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
921 MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
922 MODULE_LICENSE("GPL");