treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / hwmon / hwmon.c
blob6a30fb453f7adce591062868866e8dda6f158f8d
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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>
8 */
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/module.h>
19 #include <linux/pci.h>
20 #include <linux/slab.h>
21 #include <linux/string.h>
22 #include <linux/thermal.h>
24 #define CREATE_TRACE_POINTS
25 #include <trace/events/hwmon.h>
27 #define HWMON_ID_PREFIX "hwmon"
28 #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
30 struct hwmon_device {
31 const char *name;
32 struct device dev;
33 const struct hwmon_chip_info *chip;
35 struct attribute_group group;
36 const struct attribute_group **groups;
39 #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
41 #define MAX_SYSFS_ATTR_NAME_LENGTH 32
43 struct hwmon_device_attribute {
44 struct device_attribute dev_attr;
45 const struct hwmon_ops *ops;
46 enum hwmon_sensor_types type;
47 u32 attr;
48 int index;
49 char name[MAX_SYSFS_ATTR_NAME_LENGTH];
52 #define to_hwmon_attr(d) \
53 container_of(d, struct hwmon_device_attribute, dev_attr)
54 #define to_dev_attr(a) container_of(a, struct device_attribute, attr)
57 * Thermal zone information
58 * In addition to the reference to the hwmon device,
59 * also provides the sensor index.
61 struct hwmon_thermal_data {
62 struct device *dev; /* Reference to hwmon device */
63 int index; /* sensor index */
66 static ssize_t
67 name_show(struct device *dev, struct device_attribute *attr, char *buf)
69 return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
71 static DEVICE_ATTR_RO(name);
73 static struct attribute *hwmon_dev_attrs[] = {
74 &dev_attr_name.attr,
75 NULL
78 static umode_t hwmon_dev_name_is_visible(struct kobject *kobj,
79 struct attribute *attr, int n)
81 struct device *dev = container_of(kobj, struct device, kobj);
83 if (to_hwmon_device(dev)->name == NULL)
84 return 0;
86 return attr->mode;
89 static const struct attribute_group hwmon_dev_attr_group = {
90 .attrs = hwmon_dev_attrs,
91 .is_visible = hwmon_dev_name_is_visible,
94 static const struct attribute_group *hwmon_dev_attr_groups[] = {
95 &hwmon_dev_attr_group,
96 NULL
99 static void hwmon_free_attrs(struct attribute **attrs)
101 int i;
103 for (i = 0; attrs[i]; i++) {
104 struct device_attribute *dattr = to_dev_attr(attrs[i]);
105 struct hwmon_device_attribute *hattr = to_hwmon_attr(dattr);
107 kfree(hattr);
109 kfree(attrs);
112 static void hwmon_dev_release(struct device *dev)
114 struct hwmon_device *hwdev = to_hwmon_device(dev);
116 if (hwdev->group.attrs)
117 hwmon_free_attrs(hwdev->group.attrs);
118 kfree(hwdev->groups);
119 kfree(hwdev);
122 static struct class hwmon_class = {
123 .name = "hwmon",
124 .owner = THIS_MODULE,
125 .dev_groups = hwmon_dev_attr_groups,
126 .dev_release = hwmon_dev_release,
129 static DEFINE_IDA(hwmon_ida);
131 /* Thermal zone handling */
134 * The complex conditional is necessary to avoid a cyclic dependency
135 * between hwmon and thermal_sys modules.
137 #ifdef CONFIG_THERMAL_OF
138 static int hwmon_thermal_get_temp(void *data, int *temp)
140 struct hwmon_thermal_data *tdata = data;
141 struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
142 int ret;
143 long t;
145 ret = hwdev->chip->ops->read(tdata->dev, hwmon_temp, hwmon_temp_input,
146 tdata->index, &t);
147 if (ret < 0)
148 return ret;
150 *temp = t;
152 return 0;
155 static const struct thermal_zone_of_device_ops hwmon_thermal_ops = {
156 .get_temp = hwmon_thermal_get_temp,
159 static int hwmon_thermal_add_sensor(struct device *dev, int index)
161 struct hwmon_thermal_data *tdata;
162 struct thermal_zone_device *tzd;
164 tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
165 if (!tdata)
166 return -ENOMEM;
168 tdata->dev = dev;
169 tdata->index = index;
171 tzd = devm_thermal_zone_of_sensor_register(dev, index, tdata,
172 &hwmon_thermal_ops);
174 * If CONFIG_THERMAL_OF is disabled, this returns -ENODEV,
175 * so ignore that error but forward any other error.
177 if (IS_ERR(tzd) && (PTR_ERR(tzd) != -ENODEV))
178 return PTR_ERR(tzd);
180 return 0;
182 #else
183 static int hwmon_thermal_add_sensor(struct device *dev, int index)
185 return 0;
187 #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
189 static int hwmon_attr_base(enum hwmon_sensor_types type)
191 if (type == hwmon_in || type == hwmon_intrusion)
192 return 0;
193 return 1;
196 /* sysfs attribute management */
198 static ssize_t hwmon_attr_show(struct device *dev,
199 struct device_attribute *devattr, char *buf)
201 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
202 long val;
203 int ret;
205 ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
206 &val);
207 if (ret < 0)
208 return ret;
210 trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type),
211 hattr->name, val);
213 return sprintf(buf, "%ld\n", val);
216 static ssize_t hwmon_attr_show_string(struct device *dev,
217 struct device_attribute *devattr,
218 char *buf)
220 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
221 enum hwmon_sensor_types type = hattr->type;
222 const char *s;
223 int ret;
225 ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
226 hattr->index, &s);
227 if (ret < 0)
228 return ret;
230 trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type),
231 hattr->name, s);
233 return sprintf(buf, "%s\n", s);
236 static ssize_t hwmon_attr_store(struct device *dev,
237 struct device_attribute *devattr,
238 const char *buf, size_t count)
240 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
241 long val;
242 int ret;
244 ret = kstrtol(buf, 10, &val);
245 if (ret < 0)
246 return ret;
248 ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
249 val);
250 if (ret < 0)
251 return ret;
253 trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type),
254 hattr->name, val);
256 return count;
259 static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
261 return (type == hwmon_temp && attr == hwmon_temp_label) ||
262 (type == hwmon_in && attr == hwmon_in_label) ||
263 (type == hwmon_curr && attr == hwmon_curr_label) ||
264 (type == hwmon_power && attr == hwmon_power_label) ||
265 (type == hwmon_energy && attr == hwmon_energy_label) ||
266 (type == hwmon_humidity && attr == hwmon_humidity_label) ||
267 (type == hwmon_fan && attr == hwmon_fan_label);
270 static struct attribute *hwmon_genattr(const void *drvdata,
271 enum hwmon_sensor_types type,
272 u32 attr,
273 int index,
274 const char *template,
275 const struct hwmon_ops *ops)
277 struct hwmon_device_attribute *hattr;
278 struct device_attribute *dattr;
279 struct attribute *a;
280 umode_t mode;
281 const char *name;
282 bool is_string = is_string_attr(type, attr);
284 /* The attribute is invisible if there is no template string */
285 if (!template)
286 return ERR_PTR(-ENOENT);
288 mode = ops->is_visible(drvdata, type, attr, index);
289 if (!mode)
290 return ERR_PTR(-ENOENT);
292 if ((mode & 0444) && ((is_string && !ops->read_string) ||
293 (!is_string && !ops->read)))
294 return ERR_PTR(-EINVAL);
295 if ((mode & 0222) && !ops->write)
296 return ERR_PTR(-EINVAL);
298 hattr = kzalloc(sizeof(*hattr), GFP_KERNEL);
299 if (!hattr)
300 return ERR_PTR(-ENOMEM);
302 if (type == hwmon_chip) {
303 name = template;
304 } else {
305 scnprintf(hattr->name, sizeof(hattr->name), template,
306 index + hwmon_attr_base(type));
307 name = hattr->name;
310 hattr->type = type;
311 hattr->attr = attr;
312 hattr->index = index;
313 hattr->ops = ops;
315 dattr = &hattr->dev_attr;
316 dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
317 dattr->store = hwmon_attr_store;
319 a = &dattr->attr;
320 sysfs_attr_init(a);
321 a->name = name;
322 a->mode = mode;
324 return a;
328 * Chip attributes are not attribute templates but actual sysfs attributes.
329 * See hwmon_genattr() for special handling.
331 static const char * const hwmon_chip_attrs[] = {
332 [hwmon_chip_temp_reset_history] = "temp_reset_history",
333 [hwmon_chip_in_reset_history] = "in_reset_history",
334 [hwmon_chip_curr_reset_history] = "curr_reset_history",
335 [hwmon_chip_power_reset_history] = "power_reset_history",
336 [hwmon_chip_update_interval] = "update_interval",
337 [hwmon_chip_alarms] = "alarms",
338 [hwmon_chip_samples] = "samples",
339 [hwmon_chip_curr_samples] = "curr_samples",
340 [hwmon_chip_in_samples] = "in_samples",
341 [hwmon_chip_power_samples] = "power_samples",
342 [hwmon_chip_temp_samples] = "temp_samples",
345 static const char * const hwmon_temp_attr_templates[] = {
346 [hwmon_temp_enable] = "temp%d_enable",
347 [hwmon_temp_input] = "temp%d_input",
348 [hwmon_temp_type] = "temp%d_type",
349 [hwmon_temp_lcrit] = "temp%d_lcrit",
350 [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
351 [hwmon_temp_min] = "temp%d_min",
352 [hwmon_temp_min_hyst] = "temp%d_min_hyst",
353 [hwmon_temp_max] = "temp%d_max",
354 [hwmon_temp_max_hyst] = "temp%d_max_hyst",
355 [hwmon_temp_crit] = "temp%d_crit",
356 [hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
357 [hwmon_temp_emergency] = "temp%d_emergency",
358 [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
359 [hwmon_temp_alarm] = "temp%d_alarm",
360 [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
361 [hwmon_temp_min_alarm] = "temp%d_min_alarm",
362 [hwmon_temp_max_alarm] = "temp%d_max_alarm",
363 [hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
364 [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
365 [hwmon_temp_fault] = "temp%d_fault",
366 [hwmon_temp_offset] = "temp%d_offset",
367 [hwmon_temp_label] = "temp%d_label",
368 [hwmon_temp_lowest] = "temp%d_lowest",
369 [hwmon_temp_highest] = "temp%d_highest",
370 [hwmon_temp_reset_history] = "temp%d_reset_history",
373 static const char * const hwmon_in_attr_templates[] = {
374 [hwmon_in_enable] = "in%d_enable",
375 [hwmon_in_input] = "in%d_input",
376 [hwmon_in_min] = "in%d_min",
377 [hwmon_in_max] = "in%d_max",
378 [hwmon_in_lcrit] = "in%d_lcrit",
379 [hwmon_in_crit] = "in%d_crit",
380 [hwmon_in_average] = "in%d_average",
381 [hwmon_in_lowest] = "in%d_lowest",
382 [hwmon_in_highest] = "in%d_highest",
383 [hwmon_in_reset_history] = "in%d_reset_history",
384 [hwmon_in_label] = "in%d_label",
385 [hwmon_in_alarm] = "in%d_alarm",
386 [hwmon_in_min_alarm] = "in%d_min_alarm",
387 [hwmon_in_max_alarm] = "in%d_max_alarm",
388 [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
389 [hwmon_in_crit_alarm] = "in%d_crit_alarm",
392 static const char * const hwmon_curr_attr_templates[] = {
393 [hwmon_curr_enable] = "curr%d_enable",
394 [hwmon_curr_input] = "curr%d_input",
395 [hwmon_curr_min] = "curr%d_min",
396 [hwmon_curr_max] = "curr%d_max",
397 [hwmon_curr_lcrit] = "curr%d_lcrit",
398 [hwmon_curr_crit] = "curr%d_crit",
399 [hwmon_curr_average] = "curr%d_average",
400 [hwmon_curr_lowest] = "curr%d_lowest",
401 [hwmon_curr_highest] = "curr%d_highest",
402 [hwmon_curr_reset_history] = "curr%d_reset_history",
403 [hwmon_curr_label] = "curr%d_label",
404 [hwmon_curr_alarm] = "curr%d_alarm",
405 [hwmon_curr_min_alarm] = "curr%d_min_alarm",
406 [hwmon_curr_max_alarm] = "curr%d_max_alarm",
407 [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
408 [hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
411 static const char * const hwmon_power_attr_templates[] = {
412 [hwmon_power_enable] = "power%d_enable",
413 [hwmon_power_average] = "power%d_average",
414 [hwmon_power_average_interval] = "power%d_average_interval",
415 [hwmon_power_average_interval_max] = "power%d_interval_max",
416 [hwmon_power_average_interval_min] = "power%d_interval_min",
417 [hwmon_power_average_highest] = "power%d_average_highest",
418 [hwmon_power_average_lowest] = "power%d_average_lowest",
419 [hwmon_power_average_max] = "power%d_average_max",
420 [hwmon_power_average_min] = "power%d_average_min",
421 [hwmon_power_input] = "power%d_input",
422 [hwmon_power_input_highest] = "power%d_input_highest",
423 [hwmon_power_input_lowest] = "power%d_input_lowest",
424 [hwmon_power_reset_history] = "power%d_reset_history",
425 [hwmon_power_accuracy] = "power%d_accuracy",
426 [hwmon_power_cap] = "power%d_cap",
427 [hwmon_power_cap_hyst] = "power%d_cap_hyst",
428 [hwmon_power_cap_max] = "power%d_cap_max",
429 [hwmon_power_cap_min] = "power%d_cap_min",
430 [hwmon_power_min] = "power%d_min",
431 [hwmon_power_max] = "power%d_max",
432 [hwmon_power_lcrit] = "power%d_lcrit",
433 [hwmon_power_crit] = "power%d_crit",
434 [hwmon_power_label] = "power%d_label",
435 [hwmon_power_alarm] = "power%d_alarm",
436 [hwmon_power_cap_alarm] = "power%d_cap_alarm",
437 [hwmon_power_min_alarm] = "power%d_min_alarm",
438 [hwmon_power_max_alarm] = "power%d_max_alarm",
439 [hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
440 [hwmon_power_crit_alarm] = "power%d_crit_alarm",
443 static const char * const hwmon_energy_attr_templates[] = {
444 [hwmon_energy_enable] = "energy%d_enable",
445 [hwmon_energy_input] = "energy%d_input",
446 [hwmon_energy_label] = "energy%d_label",
449 static const char * const hwmon_humidity_attr_templates[] = {
450 [hwmon_humidity_enable] = "humidity%d_enable",
451 [hwmon_humidity_input] = "humidity%d_input",
452 [hwmon_humidity_label] = "humidity%d_label",
453 [hwmon_humidity_min] = "humidity%d_min",
454 [hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
455 [hwmon_humidity_max] = "humidity%d_max",
456 [hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
457 [hwmon_humidity_alarm] = "humidity%d_alarm",
458 [hwmon_humidity_fault] = "humidity%d_fault",
461 static const char * const hwmon_fan_attr_templates[] = {
462 [hwmon_fan_enable] = "fan%d_enable",
463 [hwmon_fan_input] = "fan%d_input",
464 [hwmon_fan_label] = "fan%d_label",
465 [hwmon_fan_min] = "fan%d_min",
466 [hwmon_fan_max] = "fan%d_max",
467 [hwmon_fan_div] = "fan%d_div",
468 [hwmon_fan_pulses] = "fan%d_pulses",
469 [hwmon_fan_target] = "fan%d_target",
470 [hwmon_fan_alarm] = "fan%d_alarm",
471 [hwmon_fan_min_alarm] = "fan%d_min_alarm",
472 [hwmon_fan_max_alarm] = "fan%d_max_alarm",
473 [hwmon_fan_fault] = "fan%d_fault",
476 static const char * const hwmon_pwm_attr_templates[] = {
477 [hwmon_pwm_input] = "pwm%d",
478 [hwmon_pwm_enable] = "pwm%d_enable",
479 [hwmon_pwm_mode] = "pwm%d_mode",
480 [hwmon_pwm_freq] = "pwm%d_freq",
483 static const char * const hwmon_intrusion_attr_templates[] = {
484 [hwmon_intrusion_alarm] = "intrusion%d_alarm",
485 [hwmon_intrusion_beep] = "intrusion%d_beep",
488 static const char * const *__templates[] = {
489 [hwmon_chip] = hwmon_chip_attrs,
490 [hwmon_temp] = hwmon_temp_attr_templates,
491 [hwmon_in] = hwmon_in_attr_templates,
492 [hwmon_curr] = hwmon_curr_attr_templates,
493 [hwmon_power] = hwmon_power_attr_templates,
494 [hwmon_energy] = hwmon_energy_attr_templates,
495 [hwmon_humidity] = hwmon_humidity_attr_templates,
496 [hwmon_fan] = hwmon_fan_attr_templates,
497 [hwmon_pwm] = hwmon_pwm_attr_templates,
498 [hwmon_intrusion] = hwmon_intrusion_attr_templates,
501 static const int __templates_size[] = {
502 [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
503 [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
504 [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
505 [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
506 [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
507 [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
508 [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
509 [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
510 [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
511 [hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates),
514 static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
516 int i, n;
518 for (i = n = 0; info->config[i]; i++)
519 n += hweight32(info->config[i]);
521 return n;
524 static int hwmon_genattrs(const void *drvdata,
525 struct attribute **attrs,
526 const struct hwmon_ops *ops,
527 const struct hwmon_channel_info *info)
529 const char * const *templates;
530 int template_size;
531 int i, aindex = 0;
533 if (info->type >= ARRAY_SIZE(__templates))
534 return -EINVAL;
536 templates = __templates[info->type];
537 template_size = __templates_size[info->type];
539 for (i = 0; info->config[i]; i++) {
540 u32 attr_mask = info->config[i];
541 u32 attr;
543 while (attr_mask) {
544 struct attribute *a;
546 attr = __ffs(attr_mask);
547 attr_mask &= ~BIT(attr);
548 if (attr >= template_size)
549 return -EINVAL;
550 a = hwmon_genattr(drvdata, info->type, attr, i,
551 templates[attr], ops);
552 if (IS_ERR(a)) {
553 if (PTR_ERR(a) != -ENOENT)
554 return PTR_ERR(a);
555 continue;
557 attrs[aindex++] = a;
560 return aindex;
563 static struct attribute **
564 __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip)
566 int ret, i, aindex = 0, nattrs = 0;
567 struct attribute **attrs;
569 for (i = 0; chip->info[i]; i++)
570 nattrs += hwmon_num_channel_attrs(chip->info[i]);
572 if (nattrs == 0)
573 return ERR_PTR(-EINVAL);
575 attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL);
576 if (!attrs)
577 return ERR_PTR(-ENOMEM);
579 for (i = 0; chip->info[i]; i++) {
580 ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops,
581 chip->info[i]);
582 if (ret < 0) {
583 hwmon_free_attrs(attrs);
584 return ERR_PTR(ret);
586 aindex += ret;
589 return attrs;
592 static struct device *
593 __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
594 const struct hwmon_chip_info *chip,
595 const struct attribute_group **groups)
597 struct hwmon_device *hwdev;
598 struct device *hdev;
599 int i, j, err, id;
601 /* Complain about invalid characters in hwmon name attribute */
602 if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
603 dev_warn(dev,
604 "hwmon: '%s' is not a valid name attribute, please fix\n",
605 name);
607 id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
608 if (id < 0)
609 return ERR_PTR(id);
611 hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
612 if (hwdev == NULL) {
613 err = -ENOMEM;
614 goto ida_remove;
617 hdev = &hwdev->dev;
619 if (chip) {
620 struct attribute **attrs;
621 int ngroups = 2; /* terminating NULL plus &hwdev->groups */
623 if (groups)
624 for (i = 0; groups[i]; i++)
625 ngroups++;
627 hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL);
628 if (!hwdev->groups) {
629 err = -ENOMEM;
630 goto free_hwmon;
633 attrs = __hwmon_create_attrs(drvdata, chip);
634 if (IS_ERR(attrs)) {
635 err = PTR_ERR(attrs);
636 goto free_hwmon;
639 hwdev->group.attrs = attrs;
640 ngroups = 0;
641 hwdev->groups[ngroups++] = &hwdev->group;
643 if (groups) {
644 for (i = 0; groups[i]; i++)
645 hwdev->groups[ngroups++] = groups[i];
648 hdev->groups = hwdev->groups;
649 } else {
650 hdev->groups = groups;
653 hwdev->name = name;
654 hdev->class = &hwmon_class;
655 hdev->parent = dev;
656 hdev->of_node = dev ? dev->of_node : NULL;
657 hwdev->chip = chip;
658 dev_set_drvdata(hdev, drvdata);
659 dev_set_name(hdev, HWMON_ID_FORMAT, id);
660 err = device_register(hdev);
661 if (err)
662 goto free_hwmon;
664 if (dev && dev->of_node && chip && chip->ops->read &&
665 chip->info[0]->type == hwmon_chip &&
666 (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
667 const struct hwmon_channel_info **info = chip->info;
669 for (i = 1; info[i]; i++) {
670 if (info[i]->type != hwmon_temp)
671 continue;
673 for (j = 0; info[i]->config[j]; j++) {
674 if (!chip->ops->is_visible(drvdata, hwmon_temp,
675 hwmon_temp_input, j))
676 continue;
677 if (info[i]->config[j] & HWMON_T_INPUT) {
678 err = hwmon_thermal_add_sensor(hdev, j);
679 if (err) {
680 device_unregister(hdev);
682 * Don't worry about hwdev;
683 * hwmon_dev_release(), called
684 * from device_unregister(),
685 * will free it.
687 goto ida_remove;
694 return hdev;
696 free_hwmon:
697 hwmon_dev_release(hdev);
698 ida_remove:
699 ida_simple_remove(&hwmon_ida, id);
700 return ERR_PTR(err);
704 * hwmon_device_register_with_groups - register w/ hwmon
705 * @dev: the parent device
706 * @name: hwmon name attribute
707 * @drvdata: driver data to attach to created device
708 * @groups: List of attribute groups to create
710 * hwmon_device_unregister() must be called when the device is no
711 * longer needed.
713 * Returns the pointer to the new device.
715 struct device *
716 hwmon_device_register_with_groups(struct device *dev, const char *name,
717 void *drvdata,
718 const struct attribute_group **groups)
720 if (!name)
721 return ERR_PTR(-EINVAL);
723 return __hwmon_device_register(dev, name, drvdata, NULL, groups);
725 EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
728 * hwmon_device_register_with_info - register w/ hwmon
729 * @dev: the parent device
730 * @name: hwmon name attribute
731 * @drvdata: driver data to attach to created device
732 * @chip: pointer to hwmon chip information
733 * @extra_groups: pointer to list of additional non-standard attribute groups
735 * hwmon_device_unregister() must be called when the device is no
736 * longer needed.
738 * Returns the pointer to the new device.
740 struct device *
741 hwmon_device_register_with_info(struct device *dev, const char *name,
742 void *drvdata,
743 const struct hwmon_chip_info *chip,
744 const struct attribute_group **extra_groups)
746 if (!name)
747 return ERR_PTR(-EINVAL);
749 if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info))
750 return ERR_PTR(-EINVAL);
752 if (chip && !dev)
753 return ERR_PTR(-EINVAL);
755 return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
757 EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
760 * hwmon_device_register - register w/ hwmon
761 * @dev: the device to register
763 * hwmon_device_unregister() must be called when the device is no
764 * longer needed.
766 * Returns the pointer to the new device.
768 struct device *hwmon_device_register(struct device *dev)
770 dev_warn(dev,
771 "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
773 return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
775 EXPORT_SYMBOL_GPL(hwmon_device_register);
778 * hwmon_device_unregister - removes the previously registered class device
780 * @dev: the class device to destroy
782 void hwmon_device_unregister(struct device *dev)
784 int id;
786 if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
787 device_unregister(dev);
788 ida_simple_remove(&hwmon_ida, id);
789 } else
790 dev_dbg(dev->parent,
791 "hwmon_device_unregister() failed: bad class ID!\n");
793 EXPORT_SYMBOL_GPL(hwmon_device_unregister);
795 static void devm_hwmon_release(struct device *dev, void *res)
797 struct device *hwdev = *(struct device **)res;
799 hwmon_device_unregister(hwdev);
803 * devm_hwmon_device_register_with_groups - register w/ hwmon
804 * @dev: the parent device
805 * @name: hwmon name attribute
806 * @drvdata: driver data to attach to created device
807 * @groups: List of attribute groups to create
809 * Returns the pointer to the new device. The new device is automatically
810 * unregistered with the parent device.
812 struct device *
813 devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
814 void *drvdata,
815 const struct attribute_group **groups)
817 struct device **ptr, *hwdev;
819 if (!dev)
820 return ERR_PTR(-EINVAL);
822 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
823 if (!ptr)
824 return ERR_PTR(-ENOMEM);
826 hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
827 if (IS_ERR(hwdev))
828 goto error;
830 *ptr = hwdev;
831 devres_add(dev, ptr);
832 return hwdev;
834 error:
835 devres_free(ptr);
836 return hwdev;
838 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
841 * devm_hwmon_device_register_with_info - register w/ hwmon
842 * @dev: the parent device
843 * @name: hwmon name attribute
844 * @drvdata: driver data to attach to created device
845 * @chip: pointer to hwmon chip information
846 * @groups: pointer to list of driver specific attribute groups
848 * Returns the pointer to the new device. The new device is automatically
849 * unregistered with the parent device.
851 struct device *
852 devm_hwmon_device_register_with_info(struct device *dev, const char *name,
853 void *drvdata,
854 const struct hwmon_chip_info *chip,
855 const struct attribute_group **groups)
857 struct device **ptr, *hwdev;
859 if (!dev)
860 return ERR_PTR(-EINVAL);
862 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
863 if (!ptr)
864 return ERR_PTR(-ENOMEM);
866 hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
867 groups);
868 if (IS_ERR(hwdev))
869 goto error;
871 *ptr = hwdev;
872 devres_add(dev, ptr);
874 return hwdev;
876 error:
877 devres_free(ptr);
878 return hwdev;
880 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
882 static int devm_hwmon_match(struct device *dev, void *res, void *data)
884 struct device **hwdev = res;
886 return *hwdev == data;
890 * devm_hwmon_device_unregister - removes a previously registered hwmon device
892 * @dev: the parent device of the device to unregister
894 void devm_hwmon_device_unregister(struct device *dev)
896 WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
898 EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
900 static void __init hwmon_pci_quirks(void)
902 #if defined CONFIG_X86 && defined CONFIG_PCI
903 struct pci_dev *sb;
904 u16 base;
905 u8 enable;
907 /* Open access to 0x295-0x296 on MSI MS-7031 */
908 sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
909 if (sb) {
910 if (sb->subsystem_vendor == 0x1462 && /* MSI */
911 sb->subsystem_device == 0x0031) { /* MS-7031 */
912 pci_read_config_byte(sb, 0x48, &enable);
913 pci_read_config_word(sb, 0x64, &base);
915 if (base == 0 && !(enable & BIT(2))) {
916 dev_info(&sb->dev,
917 "Opening wide generic port at 0x295\n");
918 pci_write_config_word(sb, 0x64, 0x295);
919 pci_write_config_byte(sb, 0x48,
920 enable | BIT(2));
923 pci_dev_put(sb);
925 #endif
928 static int __init hwmon_init(void)
930 int err;
932 hwmon_pci_quirks();
934 err = class_register(&hwmon_class);
935 if (err) {
936 pr_err("couldn't register hwmon sysfs class\n");
937 return err;
939 return 0;
942 static void __exit hwmon_exit(void)
944 class_unregister(&hwmon_class);
947 subsys_initcall(hwmon_init);
948 module_exit(hwmon_exit);
950 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
951 MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
952 MODULE_LICENSE("GPL");