GNU Linux-libre 4.14.328-gnu1
[releases.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 #define MAX_SYSFS_ATTR_NAME_LENGTH      32
42
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];
50 };
51
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)
55
56 /*
57  * Thermal zone information
58  * In addition to the reference to the hwmon device,
59  * also provides the sensor index.
60  */
61 struct hwmon_thermal_data {
62         struct device *dev;             /* Reference to hwmon device */
63         int index;                      /* sensor index */
64 };
65
66 static ssize_t
67 name_show(struct device *dev, struct device_attribute *attr, char *buf)
68 {
69         return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
70 }
71 static DEVICE_ATTR_RO(name);
72
73 static struct attribute *hwmon_dev_attrs[] = {
74         &dev_attr_name.attr,
75         NULL
76 };
77
78 static umode_t hwmon_dev_name_is_visible(struct kobject *kobj,
79                                          struct attribute *attr, int n)
80 {
81         struct device *dev = container_of(kobj, struct device, kobj);
82
83         if (to_hwmon_device(dev)->name == NULL)
84                 return 0;
85
86         return attr->mode;
87 }
88
89 static const struct attribute_group hwmon_dev_attr_group = {
90         .attrs          = hwmon_dev_attrs,
91         .is_visible     = hwmon_dev_name_is_visible,
92 };
93
94 static const struct attribute_group *hwmon_dev_attr_groups[] = {
95         &hwmon_dev_attr_group,
96         NULL
97 };
98
99 static void hwmon_free_attrs(struct attribute **attrs)
100 {
101         int i;
102
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);
106
107                 kfree(hattr);
108         }
109         kfree(attrs);
110 }
111
112 static void hwmon_dev_release(struct device *dev)
113 {
114         struct hwmon_device *hwdev = to_hwmon_device(dev);
115
116         if (hwdev->group.attrs)
117                 hwmon_free_attrs(hwdev->group.attrs);
118         kfree(hwdev->groups);
119         kfree(hwdev);
120 }
121
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,
127 };
128
129 static DEFINE_IDA(hwmon_ida);
130
131 /* Thermal zone handling */
132
133 /*
134  * The complex conditional is necessary to avoid a cyclic dependency
135  * between hwmon and thermal_sys modules.
136  */
137 #if IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF) && \
138         (!defined(CONFIG_THERMAL_HWMON) || \
139          !(defined(MODULE) && IS_MODULE(CONFIG_THERMAL)))
140 static int hwmon_thermal_get_temp(void *data, int *temp)
141 {
142         struct hwmon_thermal_data *tdata = data;
143         struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
144         int ret;
145         long t;
146
147         ret = hwdev->chip->ops->read(tdata->dev, hwmon_temp, hwmon_temp_input,
148                                      tdata->index, &t);
149         if (ret < 0)
150                 return ret;
151
152         *temp = t;
153
154         return 0;
155 }
156
157 static const struct thermal_zone_of_device_ops hwmon_thermal_ops = {
158         .get_temp = hwmon_thermal_get_temp,
159 };
160
161 static int hwmon_thermal_add_sensor(struct device *dev, int index)
162 {
163         struct hwmon_thermal_data *tdata;
164         struct thermal_zone_device *tzd;
165
166         tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
167         if (!tdata)
168                 return -ENOMEM;
169
170         tdata->dev = dev;
171         tdata->index = index;
172
173         tzd = devm_thermal_zone_of_sensor_register(dev, index, tdata,
174                                                    &hwmon_thermal_ops);
175         /*
176          * If CONFIG_THERMAL_OF is disabled, this returns -ENODEV,
177          * so ignore that error but forward any other error.
178          */
179         if (IS_ERR(tzd) && (PTR_ERR(tzd) != -ENODEV))
180                 return PTR_ERR(tzd);
181
182         return 0;
183 }
184 #else
185 static int hwmon_thermal_add_sensor(struct device *dev, int index)
186 {
187         return 0;
188 }
189 #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
190
191 /* sysfs attribute management */
192
193 static ssize_t hwmon_attr_show(struct device *dev,
194                                struct device_attribute *devattr, char *buf)
195 {
196         struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
197         long val;
198         int ret;
199
200         ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
201                                &val);
202         if (ret < 0)
203                 return ret;
204
205         return sprintf(buf, "%ld\n", val);
206 }
207
208 static ssize_t hwmon_attr_show_string(struct device *dev,
209                                       struct device_attribute *devattr,
210                                       char *buf)
211 {
212         struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
213         const char *s;
214         int ret;
215
216         ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
217                                       hattr->index, &s);
218         if (ret < 0)
219                 return ret;
220
221         return sprintf(buf, "%s\n", s);
222 }
223
224 static ssize_t hwmon_attr_store(struct device *dev,
225                                 struct device_attribute *devattr,
226                                 const char *buf, size_t count)
227 {
228         struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
229         long val;
230         int ret;
231
232         ret = kstrtol(buf, 10, &val);
233         if (ret < 0)
234                 return ret;
235
236         ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
237                                 val);
238         if (ret < 0)
239                 return ret;
240
241         return count;
242 }
243
244 static int hwmon_attr_base(enum hwmon_sensor_types type)
245 {
246         if (type == hwmon_in)
247                 return 0;
248         return 1;
249 }
250
251 static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
252 {
253         return (type == hwmon_temp && attr == hwmon_temp_label) ||
254                (type == hwmon_in && attr == hwmon_in_label) ||
255                (type == hwmon_curr && attr == hwmon_curr_label) ||
256                (type == hwmon_power && attr == hwmon_power_label) ||
257                (type == hwmon_energy && attr == hwmon_energy_label) ||
258                (type == hwmon_humidity && attr == hwmon_humidity_label) ||
259                (type == hwmon_fan && attr == hwmon_fan_label);
260 }
261
262 static struct attribute *hwmon_genattr(const void *drvdata,
263                                        enum hwmon_sensor_types type,
264                                        u32 attr,
265                                        int index,
266                                        const char *template,
267                                        const struct hwmon_ops *ops)
268 {
269         struct hwmon_device_attribute *hattr;
270         struct device_attribute *dattr;
271         struct attribute *a;
272         umode_t mode;
273         char *name;
274         bool is_string = is_string_attr(type, attr);
275
276         /* The attribute is invisible if there is no template string */
277         if (!template)
278                 return ERR_PTR(-ENOENT);
279
280         mode = ops->is_visible(drvdata, type, attr, index);
281         if (!mode)
282                 return ERR_PTR(-ENOENT);
283
284         if ((mode & S_IRUGO) && ((is_string && !ops->read_string) ||
285                                  (!is_string && !ops->read)))
286                 return ERR_PTR(-EINVAL);
287         if ((mode & S_IWUGO) && !ops->write)
288                 return ERR_PTR(-EINVAL);
289
290         hattr = kzalloc(sizeof(*hattr), GFP_KERNEL);
291         if (!hattr)
292                 return ERR_PTR(-ENOMEM);
293
294         if (type == hwmon_chip) {
295                 name = (char *)template;
296         } else {
297                 scnprintf(hattr->name, sizeof(hattr->name), template,
298                           index + hwmon_attr_base(type));
299                 name = hattr->name;
300         }
301
302         hattr->type = type;
303         hattr->attr = attr;
304         hattr->index = index;
305         hattr->ops = ops;
306
307         dattr = &hattr->dev_attr;
308         dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
309         dattr->store = hwmon_attr_store;
310
311         a = &dattr->attr;
312         sysfs_attr_init(a);
313         a->name = name;
314         a->mode = mode;
315
316         return a;
317 }
318
319 /*
320  * Chip attributes are not attribute templates but actual sysfs attributes.
321  * See hwmon_genattr() for special handling.
322  */
323 static const char * const hwmon_chip_attrs[] = {
324         [hwmon_chip_temp_reset_history] = "temp_reset_history",
325         [hwmon_chip_in_reset_history] = "in_reset_history",
326         [hwmon_chip_curr_reset_history] = "curr_reset_history",
327         [hwmon_chip_power_reset_history] = "power_reset_history",
328         [hwmon_chip_update_interval] = "update_interval",
329         [hwmon_chip_alarms] = "alarms",
330 };
331
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",
357 };
358
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 };
376
377 static const char * const hwmon_curr_attr_templates[] = {
378         [hwmon_curr_input] = "curr%d_input",
379         [hwmon_curr_min] = "curr%d_min",
380         [hwmon_curr_max] = "curr%d_max",
381         [hwmon_curr_lcrit] = "curr%d_lcrit",
382         [hwmon_curr_crit] = "curr%d_crit",
383         [hwmon_curr_average] = "curr%d_average",
384         [hwmon_curr_lowest] = "curr%d_lowest",
385         [hwmon_curr_highest] = "curr%d_highest",
386         [hwmon_curr_reset_history] = "curr%d_reset_history",
387         [hwmon_curr_label] = "curr%d_label",
388         [hwmon_curr_alarm] = "curr%d_alarm",
389         [hwmon_curr_min_alarm] = "curr%d_min_alarm",
390         [hwmon_curr_max_alarm] = "curr%d_max_alarm",
391         [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
392         [hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
393 };
394
395 static const char * const hwmon_power_attr_templates[] = {
396         [hwmon_power_average] = "power%d_average",
397         [hwmon_power_average_interval] = "power%d_average_interval",
398         [hwmon_power_average_interval_max] = "power%d_interval_max",
399         [hwmon_power_average_interval_min] = "power%d_interval_min",
400         [hwmon_power_average_highest] = "power%d_average_highest",
401         [hwmon_power_average_lowest] = "power%d_average_lowest",
402         [hwmon_power_average_max] = "power%d_average_max",
403         [hwmon_power_average_min] = "power%d_average_min",
404         [hwmon_power_input] = "power%d_input",
405         [hwmon_power_input_highest] = "power%d_input_highest",
406         [hwmon_power_input_lowest] = "power%d_input_lowest",
407         [hwmon_power_reset_history] = "power%d_reset_history",
408         [hwmon_power_accuracy] = "power%d_accuracy",
409         [hwmon_power_cap] = "power%d_cap",
410         [hwmon_power_cap_hyst] = "power%d_cap_hyst",
411         [hwmon_power_cap_max] = "power%d_cap_max",
412         [hwmon_power_cap_min] = "power%d_cap_min",
413         [hwmon_power_max] = "power%d_max",
414         [hwmon_power_crit] = "power%d_crit",
415         [hwmon_power_label] = "power%d_label",
416         [hwmon_power_alarm] = "power%d_alarm",
417         [hwmon_power_cap_alarm] = "power%d_cap_alarm",
418         [hwmon_power_max_alarm] = "power%d_max_alarm",
419         [hwmon_power_crit_alarm] = "power%d_crit_alarm",
420 };
421
422 static const char * const hwmon_energy_attr_templates[] = {
423         [hwmon_energy_input] = "energy%d_input",
424         [hwmon_energy_label] = "energy%d_label",
425 };
426
427 static const char * const hwmon_humidity_attr_templates[] = {
428         [hwmon_humidity_input] = "humidity%d_input",
429         [hwmon_humidity_label] = "humidity%d_label",
430         [hwmon_humidity_min] = "humidity%d_min",
431         [hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
432         [hwmon_humidity_max] = "humidity%d_max",
433         [hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
434         [hwmon_humidity_alarm] = "humidity%d_alarm",
435         [hwmon_humidity_fault] = "humidity%d_fault",
436 };
437
438 static const char * const hwmon_fan_attr_templates[] = {
439         [hwmon_fan_input] = "fan%d_input",
440         [hwmon_fan_label] = "fan%d_label",
441         [hwmon_fan_min] = "fan%d_min",
442         [hwmon_fan_max] = "fan%d_max",
443         [hwmon_fan_div] = "fan%d_div",
444         [hwmon_fan_pulses] = "fan%d_pulses",
445         [hwmon_fan_target] = "fan%d_target",
446         [hwmon_fan_alarm] = "fan%d_alarm",
447         [hwmon_fan_min_alarm] = "fan%d_min_alarm",
448         [hwmon_fan_max_alarm] = "fan%d_max_alarm",
449         [hwmon_fan_fault] = "fan%d_fault",
450 };
451
452 static const char * const hwmon_pwm_attr_templates[] = {
453         [hwmon_pwm_input] = "pwm%d",
454         [hwmon_pwm_enable] = "pwm%d_enable",
455         [hwmon_pwm_mode] = "pwm%d_mode",
456         [hwmon_pwm_freq] = "pwm%d_freq",
457 };
458
459 static const char * const *__templates[] = {
460         [hwmon_chip] = hwmon_chip_attrs,
461         [hwmon_temp] = hwmon_temp_attr_templates,
462         [hwmon_in] = hwmon_in_attr_templates,
463         [hwmon_curr] = hwmon_curr_attr_templates,
464         [hwmon_power] = hwmon_power_attr_templates,
465         [hwmon_energy] = hwmon_energy_attr_templates,
466         [hwmon_humidity] = hwmon_humidity_attr_templates,
467         [hwmon_fan] = hwmon_fan_attr_templates,
468         [hwmon_pwm] = hwmon_pwm_attr_templates,
469 };
470
471 static const int __templates_size[] = {
472         [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
473         [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
474         [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
475         [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
476         [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
477         [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
478         [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
479         [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
480         [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
481 };
482
483 static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
484 {
485         int i, n;
486
487         for (i = n = 0; info->config[i]; i++)
488                 n += hweight32(info->config[i]);
489
490         return n;
491 }
492
493 static int hwmon_genattrs(const void *drvdata,
494                           struct attribute **attrs,
495                           const struct hwmon_ops *ops,
496                           const struct hwmon_channel_info *info)
497 {
498         const char * const *templates;
499         int template_size;
500         int i, aindex = 0;
501
502         if (info->type >= ARRAY_SIZE(__templates))
503                 return -EINVAL;
504
505         templates = __templates[info->type];
506         template_size = __templates_size[info->type];
507
508         for (i = 0; info->config[i]; i++) {
509                 u32 attr_mask = info->config[i];
510                 u32 attr;
511
512                 while (attr_mask) {
513                         struct attribute *a;
514
515                         attr = __ffs(attr_mask);
516                         attr_mask &= ~BIT(attr);
517                         if (attr >= template_size)
518                                 return -EINVAL;
519                         a = hwmon_genattr(drvdata, info->type, attr, i,
520                                           templates[attr], ops);
521                         if (IS_ERR(a)) {
522                                 if (PTR_ERR(a) != -ENOENT)
523                                         return PTR_ERR(a);
524                                 continue;
525                         }
526                         attrs[aindex++] = a;
527                 }
528         }
529         return aindex;
530 }
531
532 static struct attribute **
533 __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip)
534 {
535         int ret, i, aindex = 0, nattrs = 0;
536         struct attribute **attrs;
537
538         for (i = 0; chip->info[i]; i++)
539                 nattrs += hwmon_num_channel_attrs(chip->info[i]);
540
541         if (nattrs == 0)
542                 return ERR_PTR(-EINVAL);
543
544         attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL);
545         if (!attrs)
546                 return ERR_PTR(-ENOMEM);
547
548         for (i = 0; chip->info[i]; i++) {
549                 ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops,
550                                      chip->info[i]);
551                 if (ret < 0) {
552                         hwmon_free_attrs(attrs);
553                         return ERR_PTR(ret);
554                 }
555                 aindex += ret;
556         }
557
558         return attrs;
559 }
560
561 static struct device *
562 __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
563                         const struct hwmon_chip_info *chip,
564                         const struct attribute_group **groups)
565 {
566         struct hwmon_device *hwdev;
567         struct device *hdev;
568         int i, j, err, id;
569
570         /* Complain about invalid characters in hwmon name attribute */
571         if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
572                 dev_warn(dev,
573                          "hwmon: '%s' is not a valid name attribute, please fix\n",
574                          name);
575
576         id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
577         if (id < 0)
578                 return ERR_PTR(id);
579
580         hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
581         if (hwdev == NULL) {
582                 err = -ENOMEM;
583                 goto ida_remove;
584         }
585
586         hdev = &hwdev->dev;
587
588         if (chip) {
589                 struct attribute **attrs;
590                 int ngroups = 2; /* terminating NULL plus &hwdev->groups */
591
592                 if (groups)
593                         for (i = 0; groups[i]; i++)
594                                 ngroups++;
595
596                 hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL);
597                 if (!hwdev->groups) {
598                         err = -ENOMEM;
599                         goto free_hwmon;
600                 }
601
602                 attrs = __hwmon_create_attrs(drvdata, chip);
603                 if (IS_ERR(attrs)) {
604                         err = PTR_ERR(attrs);
605                         goto free_hwmon;
606                 }
607
608                 hwdev->group.attrs = attrs;
609                 ngroups = 0;
610                 hwdev->groups[ngroups++] = &hwdev->group;
611
612                 if (groups) {
613                         for (i = 0; groups[i]; i++)
614                                 hwdev->groups[ngroups++] = groups[i];
615                 }
616
617                 hdev->groups = hwdev->groups;
618         } else {
619                 hdev->groups = groups;
620         }
621
622         hwdev->name = name;
623         hdev->class = &hwmon_class;
624         hdev->parent = dev;
625         hdev->of_node = dev ? dev->of_node : NULL;
626         hwdev->chip = chip;
627         dev_set_drvdata(hdev, drvdata);
628         dev_set_name(hdev, HWMON_ID_FORMAT, id);
629         err = device_register(hdev);
630         if (err) {
631                 put_device(hdev);
632                 goto ida_remove;
633         }
634
635         if (dev && dev->of_node && chip && chip->ops->read &&
636             chip->info[0]->type == hwmon_chip &&
637             (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
638                 const struct hwmon_channel_info **info = chip->info;
639
640                 for (i = 1; info[i]; i++) {
641                         if (info[i]->type != hwmon_temp)
642                                 continue;
643
644                         for (j = 0; info[i]->config[j]; j++) {
645                                 if (!chip->ops->is_visible(drvdata, hwmon_temp,
646                                                            hwmon_temp_input, j))
647                                         continue;
648                                 if (info[i]->config[j] & HWMON_T_INPUT) {
649                                         err = hwmon_thermal_add_sensor(hdev, j);
650                                         if (err) {
651                                                 device_unregister(hdev);
652                                                 goto ida_remove;
653                                         }
654                                 }
655                         }
656                 }
657         }
658
659         return hdev;
660
661 free_hwmon:
662         hwmon_dev_release(hdev);
663 ida_remove:
664         ida_simple_remove(&hwmon_ida, id);
665         return ERR_PTR(err);
666 }
667
668 /**
669  * hwmon_device_register_with_groups - register w/ hwmon
670  * @dev: the parent device
671  * @name: hwmon name attribute
672  * @drvdata: driver data to attach to created device
673  * @groups: List of attribute groups to create
674  *
675  * hwmon_device_unregister() must be called when the device is no
676  * longer needed.
677  *
678  * Returns the pointer to the new device.
679  */
680 struct device *
681 hwmon_device_register_with_groups(struct device *dev, const char *name,
682                                   void *drvdata,
683                                   const struct attribute_group **groups)
684 {
685         if (!name)
686                 return ERR_PTR(-EINVAL);
687
688         return __hwmon_device_register(dev, name, drvdata, NULL, groups);
689 }
690 EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
691
692 /**
693  * hwmon_device_register_with_info - register w/ hwmon
694  * @dev: the parent device
695  * @name: hwmon name attribute
696  * @drvdata: driver data to attach to created device
697  * @info: pointer to hwmon chip information
698  * @extra_groups: pointer to list of additional non-standard attribute groups
699  *
700  * hwmon_device_unregister() must be called when the device is no
701  * longer needed.
702  *
703  * Returns the pointer to the new device.
704  */
705 struct device *
706 hwmon_device_register_with_info(struct device *dev, const char *name,
707                                 void *drvdata,
708                                 const struct hwmon_chip_info *chip,
709                                 const struct attribute_group **extra_groups)
710 {
711         if (!name)
712                 return ERR_PTR(-EINVAL);
713
714         if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info))
715                 return ERR_PTR(-EINVAL);
716
717         return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
718 }
719 EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
720
721 /**
722  * hwmon_device_register - register w/ hwmon
723  * @dev: the device to register
724  *
725  * hwmon_device_unregister() must be called when the device is no
726  * longer needed.
727  *
728  * Returns the pointer to the new device.
729  */
730 struct device *hwmon_device_register(struct device *dev)
731 {
732         dev_warn(dev,
733                  "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
734
735         return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
736 }
737 EXPORT_SYMBOL_GPL(hwmon_device_register);
738
739 /**
740  * hwmon_device_unregister - removes the previously registered class device
741  *
742  * @dev: the class device to destroy
743  */
744 void hwmon_device_unregister(struct device *dev)
745 {
746         int id;
747
748         if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
749                 device_unregister(dev);
750                 ida_simple_remove(&hwmon_ida, id);
751         } else
752                 dev_dbg(dev->parent,
753                         "hwmon_device_unregister() failed: bad class ID!\n");
754 }
755 EXPORT_SYMBOL_GPL(hwmon_device_unregister);
756
757 static void devm_hwmon_release(struct device *dev, void *res)
758 {
759         struct device *hwdev = *(struct device **)res;
760
761         hwmon_device_unregister(hwdev);
762 }
763
764 /**
765  * devm_hwmon_device_register_with_groups - register w/ hwmon
766  * @dev: the parent device
767  * @name: hwmon name attribute
768  * @drvdata: driver data to attach to created device
769  * @groups: List of attribute groups to create
770  *
771  * Returns the pointer to the new device. The new device is automatically
772  * unregistered with the parent device.
773  */
774 struct device *
775 devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
776                                        void *drvdata,
777                                        const struct attribute_group **groups)
778 {
779         struct device **ptr, *hwdev;
780
781         if (!dev)
782                 return ERR_PTR(-EINVAL);
783
784         ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
785         if (!ptr)
786                 return ERR_PTR(-ENOMEM);
787
788         hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
789         if (IS_ERR(hwdev))
790                 goto error;
791
792         *ptr = hwdev;
793         devres_add(dev, ptr);
794         return hwdev;
795
796 error:
797         devres_free(ptr);
798         return hwdev;
799 }
800 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
801
802 /**
803  * devm_hwmon_device_register_with_info - register w/ hwmon
804  * @dev: the parent device
805  * @name: hwmon name attribute
806  * @drvdata: driver data to attach to created device
807  * @info: Pointer to hwmon chip information
808  * @groups - pointer to list of driver specific attribute groups
809  *
810  * Returns the pointer to the new device. The new device is automatically
811  * unregistered with the parent device.
812  */
813 struct device *
814 devm_hwmon_device_register_with_info(struct device *dev, const char *name,
815                                      void *drvdata,
816                                      const struct hwmon_chip_info *chip,
817                                      const struct attribute_group **groups)
818 {
819         struct device **ptr, *hwdev;
820
821         if (!dev)
822                 return ERR_PTR(-EINVAL);
823
824         ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
825         if (!ptr)
826                 return ERR_PTR(-ENOMEM);
827
828         hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
829                                                 groups);
830         if (IS_ERR(hwdev))
831                 goto error;
832
833         *ptr = hwdev;
834         devres_add(dev, ptr);
835
836         return hwdev;
837
838 error:
839         devres_free(ptr);
840         return hwdev;
841 }
842 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
843
844 static int devm_hwmon_match(struct device *dev, void *res, void *data)
845 {
846         struct device **hwdev = res;
847
848         return *hwdev == data;
849 }
850
851 /**
852  * devm_hwmon_device_unregister - removes a previously registered hwmon device
853  *
854  * @dev: the parent device of the device to unregister
855  */
856 void devm_hwmon_device_unregister(struct device *dev)
857 {
858         WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
859 }
860 EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
861
862 static void __init hwmon_pci_quirks(void)
863 {
864 #if defined CONFIG_X86 && defined CONFIG_PCI
865         struct pci_dev *sb;
866         u16 base;
867         u8 enable;
868
869         /* Open access to 0x295-0x296 on MSI MS-7031 */
870         sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
871         if (sb) {
872                 if (sb->subsystem_vendor == 0x1462 &&   /* MSI */
873                     sb->subsystem_device == 0x0031) {   /* MS-7031 */
874                         pci_read_config_byte(sb, 0x48, &enable);
875                         pci_read_config_word(sb, 0x64, &base);
876
877                         if (base == 0 && !(enable & BIT(2))) {
878                                 dev_info(&sb->dev,
879                                          "Opening wide generic port at 0x295\n");
880                                 pci_write_config_word(sb, 0x64, 0x295);
881                                 pci_write_config_byte(sb, 0x48,
882                                                       enable | BIT(2));
883                         }
884                 }
885                 pci_dev_put(sb);
886         }
887 #endif
888 }
889
890 static int __init hwmon_init(void)
891 {
892         int err;
893
894         hwmon_pci_quirks();
895
896         err = class_register(&hwmon_class);
897         if (err) {
898                 pr_err("couldn't register hwmon sysfs class\n");
899                 return err;
900         }
901         return 0;
902 }
903
904 static void __exit hwmon_exit(void)
905 {
906         class_unregister(&hwmon_class);
907 }
908
909 subsys_initcall(hwmon_init);
910 module_exit(hwmon_exit);
911
912 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
913 MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
914 MODULE_LICENSE("GPL");
915