hwmon: (core) Add current attribute support to new API
[cascardo/linux.git] / drivers / hwmon / hwmon.c
1 /*
2  * hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring
3  *
4  * This file defines the sysfs class "hwmon", for use by sensors drivers.
5  *
6  * Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com>
7  *
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.
11  */
12
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
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>
26
27 #define HWMON_ID_PREFIX "hwmon"
28 #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
29
30 struct hwmon_device {
31         const char *name;
32         struct device dev;
33         const struct hwmon_chip_info *chip;
34
35         struct attribute_group group;
36         const struct attribute_group **groups;
37 };
38
39 #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
40
41 struct hwmon_device_attribute {
42         struct device_attribute dev_attr;
43         const struct hwmon_ops *ops;
44         enum hwmon_sensor_types type;
45         u32 attr;
46         int index;
47 };
48
49 #define to_hwmon_attr(d) \
50         container_of(d, struct hwmon_device_attribute, dev_attr)
51
52 /*
53  * Thermal zone information
54  * In addition to the reference to the hwmon device,
55  * also provides the sensor index.
56  */
57 struct hwmon_thermal_data {
58         struct hwmon_device *hwdev;     /* Reference to hwmon device */
59         int index;                      /* sensor index */
60 };
61
62 static ssize_t
63 show_name(struct device *dev, struct device_attribute *attr, char *buf)
64 {
65         return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
66 }
67 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
68
69 static struct attribute *hwmon_dev_attrs[] = {
70         &dev_attr_name.attr,
71         NULL
72 };
73
74 static umode_t hwmon_dev_name_is_visible(struct kobject *kobj,
75                                          struct attribute *attr, int n)
76 {
77         struct device *dev = container_of(kobj, struct device, kobj);
78
79         if (to_hwmon_device(dev)->name == NULL)
80                 return 0;
81
82         return attr->mode;
83 }
84
85 static struct attribute_group hwmon_dev_attr_group = {
86         .attrs          = hwmon_dev_attrs,
87         .is_visible     = hwmon_dev_name_is_visible,
88 };
89
90 static const struct attribute_group *hwmon_dev_attr_groups[] = {
91         &hwmon_dev_attr_group,
92         NULL
93 };
94
95 static void hwmon_dev_release(struct device *dev)
96 {
97         kfree(to_hwmon_device(dev));
98 }
99
100 static struct class hwmon_class = {
101         .name = "hwmon",
102         .owner = THIS_MODULE,
103         .dev_groups = hwmon_dev_attr_groups,
104         .dev_release = hwmon_dev_release,
105 };
106
107 static DEFINE_IDA(hwmon_ida);
108
109 /* Thermal zone handling */
110
111 #if IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF)
112 static int hwmon_thermal_get_temp(void *data, int *temp)
113 {
114         struct hwmon_thermal_data *tdata = data;
115         struct hwmon_device *hwdev = tdata->hwdev;
116         int ret;
117         long t;
118
119         ret = hwdev->chip->ops->read(&hwdev->dev, hwmon_temp, hwmon_temp_input,
120                                      tdata->index, &t);
121         if (ret < 0)
122                 return ret;
123
124         *temp = t;
125
126         return 0;
127 }
128
129 static struct thermal_zone_of_device_ops hwmon_thermal_ops = {
130         .get_temp = hwmon_thermal_get_temp,
131 };
132
133 static int hwmon_thermal_add_sensor(struct device *dev,
134                                     struct hwmon_device *hwdev, int index)
135 {
136         struct hwmon_thermal_data *tdata;
137
138         tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
139         if (!tdata)
140                 return -ENOMEM;
141
142         tdata->hwdev = hwdev;
143         tdata->index = index;
144
145         devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
146                                              &hwmon_thermal_ops);
147
148         return 0;
149 }
150 #else
151 static int hwmon_thermal_add_sensor(struct device *dev,
152                                     struct hwmon_device *hwdev, int index)
153 {
154         return 0;
155 }
156 #endif /* IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF) */
157
158 /* sysfs attribute management */
159
160 static ssize_t hwmon_attr_show(struct device *dev,
161                                struct device_attribute *devattr, char *buf)
162 {
163         struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
164         long val;
165         int ret;
166
167         ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
168                                &val);
169         if (ret < 0)
170                 return ret;
171
172         return sprintf(buf, "%ld\n", val);
173 }
174
175 static ssize_t hwmon_attr_store(struct device *dev,
176                                 struct device_attribute *devattr,
177                                 const char *buf, size_t count)
178 {
179         struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
180         long val;
181         int ret;
182
183         ret = kstrtol(buf, 10, &val);
184         if (ret < 0)
185                 return ret;
186
187         ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
188                                 val);
189         if (ret < 0)
190                 return ret;
191
192         return count;
193 }
194
195 static int hwmon_attr_base(enum hwmon_sensor_types type)
196 {
197         if (type == hwmon_in)
198                 return 0;
199         return 1;
200 }
201
202 static struct attribute *hwmon_genattr(struct device *dev,
203                                        const void *drvdata,
204                                        enum hwmon_sensor_types type,
205                                        u32 attr,
206                                        int index,
207                                        const char *template,
208                                        const struct hwmon_ops *ops)
209 {
210         struct hwmon_device_attribute *hattr;
211         struct device_attribute *dattr;
212         struct attribute *a;
213         umode_t mode;
214         char *name;
215
216         /* The attribute is invisible if there is no template string */
217         if (!template)
218                 return ERR_PTR(-ENOENT);
219
220         mode = ops->is_visible(drvdata, type, attr, index);
221         if (!mode)
222                 return ERR_PTR(-ENOENT);
223
224         if ((mode & S_IRUGO) && !ops->read)
225                 return ERR_PTR(-EINVAL);
226         if ((mode & S_IWUGO) && !ops->write)
227                 return ERR_PTR(-EINVAL);
228
229         if (type == hwmon_chip) {
230                 name = (char *)template;
231         } else {
232                 name = devm_kzalloc(dev, strlen(template) + 16, GFP_KERNEL);
233                 if (!name)
234                         return ERR_PTR(-ENOMEM);
235                 scnprintf(name, strlen(template) + 16, template,
236                           index + hwmon_attr_base(type));
237         }
238
239         hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL);
240         if (!hattr)
241                 return ERR_PTR(-ENOMEM);
242
243         hattr->type = type;
244         hattr->attr = attr;
245         hattr->index = index;
246         hattr->ops = ops;
247
248         dattr = &hattr->dev_attr;
249         dattr->show = hwmon_attr_show;
250         dattr->store = hwmon_attr_store;
251
252         a = &dattr->attr;
253         sysfs_attr_init(a);
254         a->name = name;
255         a->mode = mode;
256
257         return a;
258 }
259
260 static const char * const hwmon_chip_attr_templates[] = {
261         [hwmon_chip_temp_reset_history] = "temp_reset_history",
262         [hwmon_chip_in_reset_history] = "in_reset_history",
263         [hwmon_chip_curr_reset_history] = "curr_reset_history",
264         [hwmon_chip_update_interval] = "update_interval",
265         [hwmon_chip_alarms] = "alarms",
266 };
267
268 static const char * const hwmon_temp_attr_templates[] = {
269         [hwmon_temp_input] = "temp%d_input",
270         [hwmon_temp_type] = "temp%d_type",
271         [hwmon_temp_lcrit] = "temp%d_lcrit",
272         [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
273         [hwmon_temp_min] = "temp%d_min",
274         [hwmon_temp_min_hyst] = "temp%d_min_hyst",
275         [hwmon_temp_max] = "temp%d_max",
276         [hwmon_temp_max_hyst] = "temp%d_max_hyst",
277         [hwmon_temp_crit] = "temp%d_crit",
278         [hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
279         [hwmon_temp_emergency] = "temp%d_emergency",
280         [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
281         [hwmon_temp_alarm] = "temp%d_alarm",
282         [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
283         [hwmon_temp_min_alarm] = "temp%d_min_alarm",
284         [hwmon_temp_max_alarm] = "temp%d_max_alarm",
285         [hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
286         [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
287         [hwmon_temp_fault] = "temp%d_fault",
288         [hwmon_temp_offset] = "temp%d_offset",
289         [hwmon_temp_label] = "temp%d_label",
290         [hwmon_temp_lowest] = "temp%d_lowest",
291         [hwmon_temp_highest] = "temp%d_highest",
292         [hwmon_temp_reset_history] = "temp%d_reset_history",
293 };
294
295 static const char * const hwmon_in_attr_templates[] = {
296         [hwmon_in_input] = "in%d_input",
297         [hwmon_in_min] = "in%d_min",
298         [hwmon_in_max] = "in%d_max",
299         [hwmon_in_lcrit] = "in%d_lcrit",
300         [hwmon_in_crit] = "in%d_crit",
301         [hwmon_in_average] = "in%d_average",
302         [hwmon_in_lowest] = "in%d_lowest",
303         [hwmon_in_highest] = "in%d_highest",
304         [hwmon_in_reset_history] = "in%d_reset_history",
305         [hwmon_in_label] = "in%d_label",
306         [hwmon_in_alarm] = "in%d_alarm",
307         [hwmon_in_min_alarm] = "in%d_min_alarm",
308         [hwmon_in_max_alarm] = "in%d_max_alarm",
309         [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
310         [hwmon_in_crit_alarm] = "in%d_crit_alarm",
311 };
312
313 static const char * const hwmon_curr_attr_templates[] = {
314         [hwmon_curr_input] = "curr%d_input",
315         [hwmon_curr_min] = "curr%d_min",
316         [hwmon_curr_max] = "curr%d_max",
317         [hwmon_curr_lcrit] = "curr%d_lcrit",
318         [hwmon_curr_crit] = "curr%d_crit",
319         [hwmon_curr_average] = "curr%d_average",
320         [hwmon_curr_lowest] = "curr%d_lowest",
321         [hwmon_curr_highest] = "curr%d_highest",
322         [hwmon_curr_reset_history] = "curr%d_reset_history",
323         [hwmon_curr_label] = "curr%d_label",
324         [hwmon_curr_alarm] = "curr%d_alarm",
325         [hwmon_curr_min_alarm] = "curr%d_min_alarm",
326         [hwmon_curr_max_alarm] = "curr%d_max_alarm",
327         [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
328         [hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
329 };
330
331 static const char * const *__templates[] = {
332         [hwmon_chip] = hwmon_chip_attr_templates,
333         [hwmon_temp] = hwmon_temp_attr_templates,
334         [hwmon_in] = hwmon_in_attr_templates,
335         [hwmon_curr] = hwmon_curr_attr_templates,
336 };
337
338 static const int __templates_size[] = {
339         [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attr_templates),
340         [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
341         [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
342         [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
343 };
344
345 static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
346 {
347         int i, n;
348
349         for (i = n = 0; info->config[i]; i++)
350                 n += hweight32(info->config[i]);
351
352         return n;
353 }
354
355 static int hwmon_genattrs(struct device *dev,
356                           const void *drvdata,
357                           struct attribute **attrs,
358                           const struct hwmon_ops *ops,
359                           const struct hwmon_channel_info *info)
360 {
361         const char * const *templates;
362         int template_size;
363         int i, aindex = 0;
364
365         if (info->type >= ARRAY_SIZE(__templates))
366                 return -EINVAL;
367
368         templates = __templates[info->type];
369         template_size = __templates_size[info->type];
370
371         for (i = 0; info->config[i]; i++) {
372                 u32 attr_mask = info->config[i];
373                 u32 attr;
374
375                 while (attr_mask) {
376                         struct attribute *a;
377
378                         attr = __ffs(attr_mask);
379                         attr_mask &= ~BIT(attr);
380                         if (attr >= template_size)
381                                 return -EINVAL;
382                         a = hwmon_genattr(dev, drvdata, info->type, attr, i,
383                                           templates[attr], ops);
384                         if (IS_ERR(a)) {
385                                 if (PTR_ERR(a) != -ENOENT)
386                                         return PTR_ERR(a);
387                                 continue;
388                         }
389                         attrs[aindex++] = a;
390                 }
391         }
392         return aindex;
393 }
394
395 static struct attribute **
396 __hwmon_create_attrs(struct device *dev, const void *drvdata,
397                      const struct hwmon_chip_info *chip)
398 {
399         int ret, i, aindex = 0, nattrs = 0;
400         struct attribute **attrs;
401
402         for (i = 0; chip->info[i]; i++)
403                 nattrs += hwmon_num_channel_attrs(chip->info[i]);
404
405         if (nattrs == 0)
406                 return ERR_PTR(-EINVAL);
407
408         attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL);
409         if (!attrs)
410                 return ERR_PTR(-ENOMEM);
411
412         for (i = 0; chip->info[i]; i++) {
413                 ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops,
414                                      chip->info[i]);
415                 if (ret < 0)
416                         return ERR_PTR(ret);
417                 aindex += ret;
418         }
419
420         return attrs;
421 }
422
423 static struct device *
424 __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
425                         const struct hwmon_chip_info *chip,
426                         const struct attribute_group **groups)
427 {
428         struct hwmon_device *hwdev;
429         struct device *hdev;
430         int i, j, err, id;
431
432         /* Do not accept invalid characters in hwmon name attribute */
433         if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
434                 return ERR_PTR(-EINVAL);
435
436         id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
437         if (id < 0)
438                 return ERR_PTR(id);
439
440         hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
441         if (hwdev == NULL) {
442                 err = -ENOMEM;
443                 goto ida_remove;
444         }
445
446         hdev = &hwdev->dev;
447
448         if (chip && chip->ops->is_visible) {
449                 struct attribute **attrs;
450                 int ngroups = 2;
451
452                 if (groups)
453                         for (i = 0; groups[i]; i++)
454                                 ngroups++;
455
456                 hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups),
457                                              GFP_KERNEL);
458                 if (!hwdev->groups)
459                         return ERR_PTR(-ENOMEM);
460
461                 attrs = __hwmon_create_attrs(dev, drvdata, chip);
462                 if (IS_ERR(attrs)) {
463                         err = PTR_ERR(attrs);
464                         goto free_hwmon;
465                 }
466
467                 hwdev->group.attrs = attrs;
468                 ngroups = 0;
469                 hwdev->groups[ngroups++] = &hwdev->group;
470
471                 if (groups) {
472                         for (i = 0; groups[i]; i++)
473                                 hwdev->groups[ngroups++] = groups[i];
474                 }
475
476                 hdev->groups = hwdev->groups;
477         } else {
478                 hdev->groups = groups;
479         }
480
481         hwdev->name = name;
482         hdev->class = &hwmon_class;
483         hdev->parent = dev;
484         hdev->of_node = dev ? dev->of_node : NULL;
485         hwdev->chip = chip;
486         dev_set_drvdata(hdev, drvdata);
487         dev_set_name(hdev, HWMON_ID_FORMAT, id);
488         err = device_register(hdev);
489         if (err)
490                 goto free_hwmon;
491
492         if (chip && chip->ops->is_visible && chip->ops->read &&
493             chip->info[0]->type == hwmon_chip &&
494             (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
495                 const struct hwmon_channel_info **info = chip->info;
496
497                 for (i = 1; info[i]; i++) {
498                         if (info[i]->type != hwmon_temp)
499                                 continue;
500
501                         for (j = 0; info[i]->config[j]; j++) {
502                                 if (!chip->ops->is_visible(drvdata, hwmon_temp,
503                                                            hwmon_temp_input, j))
504                                         continue;
505                                 if (info[i]->config[j] & HWMON_T_INPUT)
506                                         hwmon_thermal_add_sensor(dev, hwdev, j);
507                         }
508                 }
509         }
510
511         return hdev;
512
513 free_hwmon:
514         kfree(hwdev);
515 ida_remove:
516         ida_simple_remove(&hwmon_ida, id);
517         return ERR_PTR(err);
518 }
519
520 /**
521  * hwmon_device_register_with_groups - register w/ hwmon
522  * @dev: the parent device
523  * @name: hwmon name attribute
524  * @drvdata: driver data to attach to created device
525  * @groups: List of attribute groups to create
526  *
527  * hwmon_device_unregister() must be called when the device is no
528  * longer needed.
529  *
530  * Returns the pointer to the new device.
531  */
532 struct device *
533 hwmon_device_register_with_groups(struct device *dev, const char *name,
534                                   void *drvdata,
535                                   const struct attribute_group **groups)
536 {
537         return __hwmon_device_register(dev, name, drvdata, NULL, groups);
538 }
539 EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
540
541 /**
542  * hwmon_device_register_with_info - register w/ hwmon
543  * @dev: the parent device
544  * @name: hwmon name attribute
545  * @drvdata: driver data to attach to created device
546  * @info: Pointer to hwmon chip information
547  * @groups - pointer to list of driver specific attribute groups
548  *
549  * hwmon_device_unregister() must be called when the device is no
550  * longer needed.
551  *
552  * Returns the pointer to the new device.
553  */
554 struct device *
555 hwmon_device_register_with_info(struct device *dev, const char *name,
556                                 void *drvdata,
557                                 const struct hwmon_chip_info *chip,
558                                 const struct attribute_group **groups)
559 {
560         if (chip && (!chip->ops || !chip->info))
561                 return ERR_PTR(-EINVAL);
562
563         return __hwmon_device_register(dev, name, drvdata, chip, groups);
564 }
565 EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
566
567 /**
568  * hwmon_device_register - register w/ hwmon
569  * @dev: the device to register
570  *
571  * hwmon_device_unregister() must be called when the device is no
572  * longer needed.
573  *
574  * Returns the pointer to the new device.
575  */
576 struct device *hwmon_device_register(struct device *dev)
577 {
578         return hwmon_device_register_with_groups(dev, NULL, NULL, NULL);
579 }
580 EXPORT_SYMBOL_GPL(hwmon_device_register);
581
582 /**
583  * hwmon_device_unregister - removes the previously registered class device
584  *
585  * @dev: the class device to destroy
586  */
587 void hwmon_device_unregister(struct device *dev)
588 {
589         int id;
590
591         if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
592                 device_unregister(dev);
593                 ida_simple_remove(&hwmon_ida, id);
594         } else
595                 dev_dbg(dev->parent,
596                         "hwmon_device_unregister() failed: bad class ID!\n");
597 }
598 EXPORT_SYMBOL_GPL(hwmon_device_unregister);
599
600 static void devm_hwmon_release(struct device *dev, void *res)
601 {
602         struct device *hwdev = *(struct device **)res;
603
604         hwmon_device_unregister(hwdev);
605 }
606
607 /**
608  * devm_hwmon_device_register_with_groups - register w/ hwmon
609  * @dev: the parent device
610  * @name: hwmon name attribute
611  * @drvdata: driver data to attach to created device
612  * @groups: List of attribute groups to create
613  *
614  * Returns the pointer to the new device. The new device is automatically
615  * unregistered with the parent device.
616  */
617 struct device *
618 devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
619                                        void *drvdata,
620                                        const struct attribute_group **groups)
621 {
622         struct device **ptr, *hwdev;
623
624         if (!dev)
625                 return ERR_PTR(-EINVAL);
626
627         ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
628         if (!ptr)
629                 return ERR_PTR(-ENOMEM);
630
631         hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
632         if (IS_ERR(hwdev))
633                 goto error;
634
635         *ptr = hwdev;
636         devres_add(dev, ptr);
637         return hwdev;
638
639 error:
640         devres_free(ptr);
641         return hwdev;
642 }
643 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
644
645 /**
646  * devm_hwmon_device_register_with_info - register w/ hwmon
647  * @dev: the parent device
648  * @name: hwmon name attribute
649  * @drvdata: driver data to attach to created device
650  * @info: Pointer to hwmon chip information
651  * @groups - pointer to list of driver specific attribute groups
652  *
653  * Returns the pointer to the new device. The new device is automatically
654  * unregistered with the parent device.
655  */
656 struct device *
657 devm_hwmon_device_register_with_info(struct device *dev, const char *name,
658                                      void *drvdata,
659                                      const struct hwmon_chip_info *chip,
660                                      const struct attribute_group **groups)
661 {
662         struct device **ptr, *hwdev;
663
664         if (!dev)
665                 return ERR_PTR(-EINVAL);
666
667         ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
668         if (!ptr)
669                 return ERR_PTR(-ENOMEM);
670
671         hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
672                                                 groups);
673         if (IS_ERR(hwdev))
674                 goto error;
675
676         *ptr = hwdev;
677         devres_add(dev, ptr);
678
679         return hwdev;
680
681 error:
682         devres_free(ptr);
683         return hwdev;
684 }
685 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
686
687 static int devm_hwmon_match(struct device *dev, void *res, void *data)
688 {
689         struct device **hwdev = res;
690
691         return *hwdev == data;
692 }
693
694 /**
695  * devm_hwmon_device_unregister - removes a previously registered hwmon device
696  *
697  * @dev: the parent device of the device to unregister
698  */
699 void devm_hwmon_device_unregister(struct device *dev)
700 {
701         WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
702 }
703 EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
704
705 static void __init hwmon_pci_quirks(void)
706 {
707 #if defined CONFIG_X86 && defined CONFIG_PCI
708         struct pci_dev *sb;
709         u16 base;
710         u8 enable;
711
712         /* Open access to 0x295-0x296 on MSI MS-7031 */
713         sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
714         if (sb) {
715                 if (sb->subsystem_vendor == 0x1462 &&   /* MSI */
716                     sb->subsystem_device == 0x0031) {   /* MS-7031 */
717                         pci_read_config_byte(sb, 0x48, &enable);
718                         pci_read_config_word(sb, 0x64, &base);
719
720                         if (base == 0 && !(enable & BIT(2))) {
721                                 dev_info(&sb->dev,
722                                          "Opening wide generic port at 0x295\n");
723                                 pci_write_config_word(sb, 0x64, 0x295);
724                                 pci_write_config_byte(sb, 0x48,
725                                                       enable | BIT(2));
726                         }
727                 }
728                 pci_dev_put(sb);
729         }
730 #endif
731 }
732
733 static int __init hwmon_init(void)
734 {
735         int err;
736
737         hwmon_pci_quirks();
738
739         err = class_register(&hwmon_class);
740         if (err) {
741                 pr_err("couldn't register hwmon sysfs class\n");
742                 return err;
743         }
744         return 0;
745 }
746
747 static void __exit hwmon_exit(void)
748 {
749         class_unregister(&hwmon_class);
750 }
751
752 subsys_initcall(hwmon_init);
753 module_exit(hwmon_exit);
754
755 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
756 MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
757 MODULE_LICENSE("GPL");
758