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