sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / hwmon / hwmon.c
blob3932f9276c074d92b7028b32c29e36c6a5aa9738
1 /*
2 * hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring
4 * This file defines the sysfs class "hwmon", for use by sensors drivers.
6 * Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/bitops.h>
16 #include <linux/device.h>
17 #include <linux/err.h>
18 #include <linux/gfp.h>
19 #include <linux/hwmon.h>
20 #include <linux/idr.h>
21 #include <linux/module.h>
22 #include <linux/pci.h>
23 #include <linux/slab.h>
24 #include <linux/string.h>
25 #include <linux/thermal.h>
27 #define 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)
56 * Thermal zone information
57 * In addition to the reference to the hwmon device,
58 * also provides the sensor index.
60 struct hwmon_thermal_data {
61 struct hwmon_device *hwdev; /* Reference to hwmon device */
62 int index; /* sensor index */
65 static ssize_t
66 show_name(struct device *dev, struct device_attribute *attr, char *buf)
68 return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
70 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
72 static struct attribute *hwmon_dev_attrs[] = {
73 &dev_attr_name.attr,
74 NULL
77 static umode_t hwmon_dev_name_is_visible(struct kobject *kobj,
78 struct attribute *attr, int n)
80 struct device *dev = container_of(kobj, struct device, kobj);
82 if (to_hwmon_device(dev)->name == NULL)
83 return 0;
85 return attr->mode;
88 static struct attribute_group hwmon_dev_attr_group = {
89 .attrs = hwmon_dev_attrs,
90 .is_visible = hwmon_dev_name_is_visible,
93 static const struct attribute_group *hwmon_dev_attr_groups[] = {
94 &hwmon_dev_attr_group,
95 NULL
98 static void hwmon_dev_release(struct device *dev)
100 kfree(to_hwmon_device(dev));
103 static struct class hwmon_class = {
104 .name = "hwmon",
105 .owner = THIS_MODULE,
106 .dev_groups = hwmon_dev_attr_groups,
107 .dev_release = hwmon_dev_release,
110 static DEFINE_IDA(hwmon_ida);
112 /* Thermal zone handling */
115 * The complex conditional is necessary to avoid a cyclic dependency
116 * between hwmon and thermal_sys modules.
118 #if IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF) && \
119 (!defined(CONFIG_THERMAL_HWMON) || \
120 !(defined(MODULE) && IS_MODULE(CONFIG_THERMAL)))
121 static int hwmon_thermal_get_temp(void *data, int *temp)
123 struct hwmon_thermal_data *tdata = data;
124 struct hwmon_device *hwdev = tdata->hwdev;
125 int ret;
126 long t;
128 ret = hwdev->chip->ops->read(&hwdev->dev, hwmon_temp, hwmon_temp_input,
129 tdata->index, &t);
130 if (ret < 0)
131 return ret;
133 *temp = t;
135 return 0;
138 static struct thermal_zone_of_device_ops hwmon_thermal_ops = {
139 .get_temp = hwmon_thermal_get_temp,
142 static int hwmon_thermal_add_sensor(struct device *dev,
143 struct hwmon_device *hwdev, int index)
145 struct hwmon_thermal_data *tdata;
147 tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
148 if (!tdata)
149 return -ENOMEM;
151 tdata->hwdev = hwdev;
152 tdata->index = index;
154 devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
155 &hwmon_thermal_ops);
157 return 0;
159 #else
160 static int hwmon_thermal_add_sensor(struct device *dev,
161 struct hwmon_device *hwdev, int index)
163 return 0;
165 #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
167 /* sysfs attribute management */
169 static ssize_t hwmon_attr_show(struct device *dev,
170 struct device_attribute *devattr, char *buf)
172 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
173 long val;
174 int ret;
176 ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
177 &val);
178 if (ret < 0)
179 return ret;
181 return sprintf(buf, "%ld\n", val);
184 static ssize_t hwmon_attr_show_string(struct device *dev,
185 struct device_attribute *devattr,
186 char *buf)
188 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
189 char *s;
190 int ret;
192 ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
193 hattr->index, &s);
194 if (ret < 0)
195 return ret;
197 return sprintf(buf, "%s\n", s);
200 static ssize_t hwmon_attr_store(struct device *dev,
201 struct device_attribute *devattr,
202 const char *buf, size_t count)
204 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
205 long val;
206 int ret;
208 ret = kstrtol(buf, 10, &val);
209 if (ret < 0)
210 return ret;
212 ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
213 val);
214 if (ret < 0)
215 return ret;
217 return count;
220 static int hwmon_attr_base(enum hwmon_sensor_types type)
222 if (type == hwmon_in)
223 return 0;
224 return 1;
227 static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
229 return (type == hwmon_temp && attr == hwmon_temp_label) ||
230 (type == hwmon_in && attr == hwmon_in_label) ||
231 (type == hwmon_curr && attr == hwmon_curr_label) ||
232 (type == hwmon_power && attr == hwmon_power_label) ||
233 (type == hwmon_energy && attr == hwmon_energy_label) ||
234 (type == hwmon_humidity && attr == hwmon_humidity_label) ||
235 (type == hwmon_fan && attr == hwmon_fan_label);
238 static struct attribute *hwmon_genattr(struct device *dev,
239 const void *drvdata,
240 enum hwmon_sensor_types type,
241 u32 attr,
242 int index,
243 const char *template,
244 const struct hwmon_ops *ops)
246 struct hwmon_device_attribute *hattr;
247 struct device_attribute *dattr;
248 struct attribute *a;
249 umode_t mode;
250 char *name;
251 bool is_string = is_string_attr(type, attr);
253 /* The attribute is invisible if there is no template string */
254 if (!template)
255 return ERR_PTR(-ENOENT);
257 mode = ops->is_visible(drvdata, type, attr, index);
258 if (!mode)
259 return ERR_PTR(-ENOENT);
261 if ((mode & S_IRUGO) && ((is_string && !ops->read_string) ||
262 (!is_string && !ops->read)))
263 return ERR_PTR(-EINVAL);
264 if ((mode & S_IWUGO) && !ops->write)
265 return ERR_PTR(-EINVAL);
267 hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL);
268 if (!hattr)
269 return ERR_PTR(-ENOMEM);
271 if (type == hwmon_chip) {
272 name = (char *)template;
273 } else {
274 scnprintf(hattr->name, sizeof(hattr->name), template,
275 index + hwmon_attr_base(type));
276 name = hattr->name;
279 hattr->type = type;
280 hattr->attr = attr;
281 hattr->index = index;
282 hattr->ops = ops;
284 dattr = &hattr->dev_attr;
285 dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
286 dattr->store = hwmon_attr_store;
288 a = &dattr->attr;
289 sysfs_attr_init(a);
290 a->name = name;
291 a->mode = mode;
293 return a;
297 * Chip attributes are not attribute templates but actual sysfs attributes.
298 * See hwmon_genattr() for special handling.
300 static const char * const hwmon_chip_attrs[] = {
301 [hwmon_chip_temp_reset_history] = "temp_reset_history",
302 [hwmon_chip_in_reset_history] = "in_reset_history",
303 [hwmon_chip_curr_reset_history] = "curr_reset_history",
304 [hwmon_chip_power_reset_history] = "power_reset_history",
305 [hwmon_chip_update_interval] = "update_interval",
306 [hwmon_chip_alarms] = "alarms",
309 static const char * const hwmon_temp_attr_templates[] = {
310 [hwmon_temp_input] = "temp%d_input",
311 [hwmon_temp_type] = "temp%d_type",
312 [hwmon_temp_lcrit] = "temp%d_lcrit",
313 [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
314 [hwmon_temp_min] = "temp%d_min",
315 [hwmon_temp_min_hyst] = "temp%d_min_hyst",
316 [hwmon_temp_max] = "temp%d_max",
317 [hwmon_temp_max_hyst] = "temp%d_max_hyst",
318 [hwmon_temp_crit] = "temp%d_crit",
319 [hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
320 [hwmon_temp_emergency] = "temp%d_emergency",
321 [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
322 [hwmon_temp_alarm] = "temp%d_alarm",
323 [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
324 [hwmon_temp_min_alarm] = "temp%d_min_alarm",
325 [hwmon_temp_max_alarm] = "temp%d_max_alarm",
326 [hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
327 [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
328 [hwmon_temp_fault] = "temp%d_fault",
329 [hwmon_temp_offset] = "temp%d_offset",
330 [hwmon_temp_label] = "temp%d_label",
331 [hwmon_temp_lowest] = "temp%d_lowest",
332 [hwmon_temp_highest] = "temp%d_highest",
333 [hwmon_temp_reset_history] = "temp%d_reset_history",
336 static const char * const hwmon_in_attr_templates[] = {
337 [hwmon_in_input] = "in%d_input",
338 [hwmon_in_min] = "in%d_min",
339 [hwmon_in_max] = "in%d_max",
340 [hwmon_in_lcrit] = "in%d_lcrit",
341 [hwmon_in_crit] = "in%d_crit",
342 [hwmon_in_average] = "in%d_average",
343 [hwmon_in_lowest] = "in%d_lowest",
344 [hwmon_in_highest] = "in%d_highest",
345 [hwmon_in_reset_history] = "in%d_reset_history",
346 [hwmon_in_label] = "in%d_label",
347 [hwmon_in_alarm] = "in%d_alarm",
348 [hwmon_in_min_alarm] = "in%d_min_alarm",
349 [hwmon_in_max_alarm] = "in%d_max_alarm",
350 [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
351 [hwmon_in_crit_alarm] = "in%d_crit_alarm",
354 static const char * const hwmon_curr_attr_templates[] = {
355 [hwmon_curr_input] = "curr%d_input",
356 [hwmon_curr_min] = "curr%d_min",
357 [hwmon_curr_max] = "curr%d_max",
358 [hwmon_curr_lcrit] = "curr%d_lcrit",
359 [hwmon_curr_crit] = "curr%d_crit",
360 [hwmon_curr_average] = "curr%d_average",
361 [hwmon_curr_lowest] = "curr%d_lowest",
362 [hwmon_curr_highest] = "curr%d_highest",
363 [hwmon_curr_reset_history] = "curr%d_reset_history",
364 [hwmon_curr_label] = "curr%d_label",
365 [hwmon_curr_alarm] = "curr%d_alarm",
366 [hwmon_curr_min_alarm] = "curr%d_min_alarm",
367 [hwmon_curr_max_alarm] = "curr%d_max_alarm",
368 [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
369 [hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
372 static const char * const hwmon_power_attr_templates[] = {
373 [hwmon_power_average] = "power%d_average",
374 [hwmon_power_average_interval] = "power%d_average_interval",
375 [hwmon_power_average_interval_max] = "power%d_interval_max",
376 [hwmon_power_average_interval_min] = "power%d_interval_min",
377 [hwmon_power_average_highest] = "power%d_average_highest",
378 [hwmon_power_average_lowest] = "power%d_average_lowest",
379 [hwmon_power_average_max] = "power%d_average_max",
380 [hwmon_power_average_min] = "power%d_average_min",
381 [hwmon_power_input] = "power%d_input",
382 [hwmon_power_input_highest] = "power%d_input_highest",
383 [hwmon_power_input_lowest] = "power%d_input_lowest",
384 [hwmon_power_reset_history] = "power%d_reset_history",
385 [hwmon_power_accuracy] = "power%d_accuracy",
386 [hwmon_power_cap] = "power%d_cap",
387 [hwmon_power_cap_hyst] = "power%d_cap_hyst",
388 [hwmon_power_cap_max] = "power%d_cap_max",
389 [hwmon_power_cap_min] = "power%d_cap_min",
390 [hwmon_power_max] = "power%d_max",
391 [hwmon_power_crit] = "power%d_crit",
392 [hwmon_power_label] = "power%d_label",
393 [hwmon_power_alarm] = "power%d_alarm",
394 [hwmon_power_cap_alarm] = "power%d_cap_alarm",
395 [hwmon_power_max_alarm] = "power%d_max_alarm",
396 [hwmon_power_crit_alarm] = "power%d_crit_alarm",
399 static const char * const hwmon_energy_attr_templates[] = {
400 [hwmon_energy_input] = "energy%d_input",
401 [hwmon_energy_label] = "energy%d_label",
404 static const char * const hwmon_humidity_attr_templates[] = {
405 [hwmon_humidity_input] = "humidity%d_input",
406 [hwmon_humidity_label] = "humidity%d_label",
407 [hwmon_humidity_min] = "humidity%d_min",
408 [hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
409 [hwmon_humidity_max] = "humidity%d_max",
410 [hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
411 [hwmon_humidity_alarm] = "humidity%d_alarm",
412 [hwmon_humidity_fault] = "humidity%d_fault",
415 static const char * const hwmon_fan_attr_templates[] = {
416 [hwmon_fan_input] = "fan%d_input",
417 [hwmon_fan_label] = "fan%d_label",
418 [hwmon_fan_min] = "fan%d_min",
419 [hwmon_fan_max] = "fan%d_max",
420 [hwmon_fan_div] = "fan%d_div",
421 [hwmon_fan_pulses] = "fan%d_pulses",
422 [hwmon_fan_target] = "fan%d_target",
423 [hwmon_fan_alarm] = "fan%d_alarm",
424 [hwmon_fan_min_alarm] = "fan%d_min_alarm",
425 [hwmon_fan_max_alarm] = "fan%d_max_alarm",
426 [hwmon_fan_fault] = "fan%d_fault",
429 static const char * const hwmon_pwm_attr_templates[] = {
430 [hwmon_pwm_input] = "pwm%d",
431 [hwmon_pwm_enable] = "pwm%d_enable",
432 [hwmon_pwm_mode] = "pwm%d_mode",
433 [hwmon_pwm_freq] = "pwm%d_freq",
436 static const char * const *__templates[] = {
437 [hwmon_chip] = hwmon_chip_attrs,
438 [hwmon_temp] = hwmon_temp_attr_templates,
439 [hwmon_in] = hwmon_in_attr_templates,
440 [hwmon_curr] = hwmon_curr_attr_templates,
441 [hwmon_power] = hwmon_power_attr_templates,
442 [hwmon_energy] = hwmon_energy_attr_templates,
443 [hwmon_humidity] = hwmon_humidity_attr_templates,
444 [hwmon_fan] = hwmon_fan_attr_templates,
445 [hwmon_pwm] = hwmon_pwm_attr_templates,
448 static const int __templates_size[] = {
449 [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
450 [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
451 [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
452 [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
453 [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
454 [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
455 [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
456 [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
457 [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
460 static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
462 int i, n;
464 for (i = n = 0; info->config[i]; i++)
465 n += hweight32(info->config[i]);
467 return n;
470 static int hwmon_genattrs(struct device *dev,
471 const void *drvdata,
472 struct attribute **attrs,
473 const struct hwmon_ops *ops,
474 const struct hwmon_channel_info *info)
476 const char * const *templates;
477 int template_size;
478 int i, aindex = 0;
480 if (info->type >= ARRAY_SIZE(__templates))
481 return -EINVAL;
483 templates = __templates[info->type];
484 template_size = __templates_size[info->type];
486 for (i = 0; info->config[i]; i++) {
487 u32 attr_mask = info->config[i];
488 u32 attr;
490 while (attr_mask) {
491 struct attribute *a;
493 attr = __ffs(attr_mask);
494 attr_mask &= ~BIT(attr);
495 if (attr >= template_size)
496 return -EINVAL;
497 a = hwmon_genattr(dev, drvdata, info->type, attr, i,
498 templates[attr], ops);
499 if (IS_ERR(a)) {
500 if (PTR_ERR(a) != -ENOENT)
501 return PTR_ERR(a);
502 continue;
504 attrs[aindex++] = a;
507 return aindex;
510 static struct attribute **
511 __hwmon_create_attrs(struct device *dev, const void *drvdata,
512 const struct hwmon_chip_info *chip)
514 int ret, i, aindex = 0, nattrs = 0;
515 struct attribute **attrs;
517 for (i = 0; chip->info[i]; i++)
518 nattrs += hwmon_num_channel_attrs(chip->info[i]);
520 if (nattrs == 0)
521 return ERR_PTR(-EINVAL);
523 attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL);
524 if (!attrs)
525 return ERR_PTR(-ENOMEM);
527 for (i = 0; chip->info[i]; i++) {
528 ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops,
529 chip->info[i]);
530 if (ret < 0)
531 return ERR_PTR(ret);
532 aindex += ret;
535 return attrs;
538 static struct device *
539 __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
540 const struct hwmon_chip_info *chip,
541 const struct attribute_group **groups)
543 struct hwmon_device *hwdev;
544 struct device *hdev;
545 int i, j, err, id;
547 /* Do not accept invalid characters in hwmon name attribute */
548 if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
549 return ERR_PTR(-EINVAL);
551 id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
552 if (id < 0)
553 return ERR_PTR(id);
555 hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
556 if (hwdev == NULL) {
557 err = -ENOMEM;
558 goto ida_remove;
561 hdev = &hwdev->dev;
563 if (chip) {
564 struct attribute **attrs;
565 int ngroups = 2; /* terminating NULL plus &hwdev->groups */
567 if (groups)
568 for (i = 0; groups[i]; i++)
569 ngroups++;
571 hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups),
572 GFP_KERNEL);
573 if (!hwdev->groups) {
574 err = -ENOMEM;
575 goto free_hwmon;
578 attrs = __hwmon_create_attrs(dev, drvdata, chip);
579 if (IS_ERR(attrs)) {
580 err = PTR_ERR(attrs);
581 goto free_hwmon;
584 hwdev->group.attrs = attrs;
585 ngroups = 0;
586 hwdev->groups[ngroups++] = &hwdev->group;
588 if (groups) {
589 for (i = 0; groups[i]; i++)
590 hwdev->groups[ngroups++] = groups[i];
593 hdev->groups = hwdev->groups;
594 } else {
595 hdev->groups = groups;
598 hwdev->name = name;
599 hdev->class = &hwmon_class;
600 hdev->parent = dev;
601 hdev->of_node = dev ? dev->of_node : NULL;
602 hwdev->chip = chip;
603 dev_set_drvdata(hdev, drvdata);
604 dev_set_name(hdev, HWMON_ID_FORMAT, id);
605 err = device_register(hdev);
606 if (err)
607 goto free_hwmon;
609 if (chip && chip->ops->read &&
610 chip->info[0]->type == hwmon_chip &&
611 (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
612 const struct hwmon_channel_info **info = chip->info;
614 for (i = 1; info[i]; i++) {
615 if (info[i]->type != hwmon_temp)
616 continue;
618 for (j = 0; info[i]->config[j]; j++) {
619 if (!chip->ops->is_visible(drvdata, hwmon_temp,
620 hwmon_temp_input, j))
621 continue;
622 if (info[i]->config[j] & HWMON_T_INPUT)
623 hwmon_thermal_add_sensor(dev, hwdev, j);
628 return hdev;
630 free_hwmon:
631 kfree(hwdev);
632 ida_remove:
633 ida_simple_remove(&hwmon_ida, id);
634 return ERR_PTR(err);
638 * hwmon_device_register_with_groups - register w/ hwmon
639 * @dev: the parent device
640 * @name: hwmon name attribute
641 * @drvdata: driver data to attach to created device
642 * @groups: List of attribute groups to create
644 * hwmon_device_unregister() must be called when the device is no
645 * longer needed.
647 * Returns the pointer to the new device.
649 struct device *
650 hwmon_device_register_with_groups(struct device *dev, const char *name,
651 void *drvdata,
652 const struct attribute_group **groups)
654 return __hwmon_device_register(dev, name, drvdata, NULL, groups);
656 EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
659 * hwmon_device_register_with_info - register w/ hwmon
660 * @dev: the parent device
661 * @name: hwmon name attribute
662 * @drvdata: driver data to attach to created device
663 * @info: pointer to hwmon chip information
664 * @extra_groups: pointer to list of additional non-standard attribute groups
666 * hwmon_device_unregister() must be called when the device is no
667 * longer needed.
669 * Returns the pointer to the new device.
671 struct device *
672 hwmon_device_register_with_info(struct device *dev, const char *name,
673 void *drvdata,
674 const struct hwmon_chip_info *chip,
675 const struct attribute_group **extra_groups)
677 if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info))
678 return ERR_PTR(-EINVAL);
680 return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
682 EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
685 * hwmon_device_register - register w/ hwmon
686 * @dev: the device to register
688 * hwmon_device_unregister() must be called when the device is no
689 * longer needed.
691 * Returns the pointer to the new device.
693 struct device *hwmon_device_register(struct device *dev)
695 dev_warn(dev,
696 "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
698 return hwmon_device_register_with_groups(dev, NULL, NULL, NULL);
700 EXPORT_SYMBOL_GPL(hwmon_device_register);
703 * hwmon_device_unregister - removes the previously registered class device
705 * @dev: the class device to destroy
707 void hwmon_device_unregister(struct device *dev)
709 int id;
711 if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
712 device_unregister(dev);
713 ida_simple_remove(&hwmon_ida, id);
714 } else
715 dev_dbg(dev->parent,
716 "hwmon_device_unregister() failed: bad class ID!\n");
718 EXPORT_SYMBOL_GPL(hwmon_device_unregister);
720 static void devm_hwmon_release(struct device *dev, void *res)
722 struct device *hwdev = *(struct device **)res;
724 hwmon_device_unregister(hwdev);
728 * devm_hwmon_device_register_with_groups - register w/ hwmon
729 * @dev: the parent device
730 * @name: hwmon name attribute
731 * @drvdata: driver data to attach to created device
732 * @groups: List of attribute groups to create
734 * Returns the pointer to the new device. The new device is automatically
735 * unregistered with the parent device.
737 struct device *
738 devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
739 void *drvdata,
740 const struct attribute_group **groups)
742 struct device **ptr, *hwdev;
744 if (!dev)
745 return ERR_PTR(-EINVAL);
747 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
748 if (!ptr)
749 return ERR_PTR(-ENOMEM);
751 hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
752 if (IS_ERR(hwdev))
753 goto error;
755 *ptr = hwdev;
756 devres_add(dev, ptr);
757 return hwdev;
759 error:
760 devres_free(ptr);
761 return hwdev;
763 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
766 * devm_hwmon_device_register_with_info - register w/ hwmon
767 * @dev: the parent device
768 * @name: hwmon name attribute
769 * @drvdata: driver data to attach to created device
770 * @info: Pointer to hwmon chip information
771 * @groups - pointer to list of driver specific attribute groups
773 * Returns the pointer to the new device. The new device is automatically
774 * unregistered with the parent device.
776 struct device *
777 devm_hwmon_device_register_with_info(struct device *dev, const char *name,
778 void *drvdata,
779 const struct hwmon_chip_info *chip,
780 const struct attribute_group **groups)
782 struct device **ptr, *hwdev;
784 if (!dev)
785 return ERR_PTR(-EINVAL);
787 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
788 if (!ptr)
789 return ERR_PTR(-ENOMEM);
791 hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
792 groups);
793 if (IS_ERR(hwdev))
794 goto error;
796 *ptr = hwdev;
797 devres_add(dev, ptr);
799 return hwdev;
801 error:
802 devres_free(ptr);
803 return hwdev;
805 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
807 static int devm_hwmon_match(struct device *dev, void *res, void *data)
809 struct device **hwdev = res;
811 return *hwdev == data;
815 * devm_hwmon_device_unregister - removes a previously registered hwmon device
817 * @dev: the parent device of the device to unregister
819 void devm_hwmon_device_unregister(struct device *dev)
821 WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
823 EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
825 static void __init hwmon_pci_quirks(void)
827 #if defined CONFIG_X86 && defined CONFIG_PCI
828 struct pci_dev *sb;
829 u16 base;
830 u8 enable;
832 /* Open access to 0x295-0x296 on MSI MS-7031 */
833 sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
834 if (sb) {
835 if (sb->subsystem_vendor == 0x1462 && /* MSI */
836 sb->subsystem_device == 0x0031) { /* MS-7031 */
837 pci_read_config_byte(sb, 0x48, &enable);
838 pci_read_config_word(sb, 0x64, &base);
840 if (base == 0 && !(enable & BIT(2))) {
841 dev_info(&sb->dev,
842 "Opening wide generic port at 0x295\n");
843 pci_write_config_word(sb, 0x64, 0x295);
844 pci_write_config_byte(sb, 0x48,
845 enable | BIT(2));
848 pci_dev_put(sb);
850 #endif
853 static int __init hwmon_init(void)
855 int err;
857 hwmon_pci_quirks();
859 err = class_register(&hwmon_class);
860 if (err) {
861 pr_err("couldn't register hwmon sysfs class\n");
862 return err;
864 return 0;
867 static void __exit hwmon_exit(void)
869 class_unregister(&hwmon_class);
872 subsys_initcall(hwmon_init);
873 module_exit(hwmon_exit);
875 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
876 MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
877 MODULE_LICENSE("GPL");