]>
Commit | Line | Data |
---|---|---|
1236441f | 1 | /* |
5ed04880 GR |
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 | */ | |
1236441f | 12 | |
c95df1ae JP |
13 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
14 | ||
d560168b | 15 | #include <linux/bitops.h> |
1236441f MH |
16 | #include <linux/device.h> |
17 | #include <linux/err.h> | |
8c65b4a6 | 18 | #include <linux/gfp.h> |
c9ebbe6f GR |
19 | #include <linux/hwmon.h> |
20 | #include <linux/idr.h> | |
21 | #include <linux/module.h> | |
2958b1ec | 22 | #include <linux/pci.h> |
c9ebbe6f | 23 | #include <linux/slab.h> |
648cd48c | 24 | #include <linux/string.h> |
d560168b | 25 | #include <linux/thermal.h> |
1236441f MH |
26 | |
27 | #define HWMON_ID_PREFIX "hwmon" | |
28 | #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d" | |
29 | ||
bab2243c GR |
30 | struct hwmon_device { |
31 | const char *name; | |
32 | struct device dev; | |
d560168b GR |
33 | const struct hwmon_chip_info *chip; |
34 | ||
35 | struct attribute_group group; | |
36 | const struct attribute_group **groups; | |
bab2243c | 37 | }; |
d560168b | 38 | |
bab2243c GR |
39 | #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev) |
40 | ||
d560168b GR |
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 | ||
bab2243c GR |
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 | }; | |
1236441f | 106 | |
4ca5f468 | 107 | static DEFINE_IDA(hwmon_ida); |
1236441f | 108 | |
d560168b GR |
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", | |
00d616cf | 262 | [hwmon_chip_in_reset_history] = "in_reset_history", |
d560168b GR |
263 | [hwmon_chip_update_interval] = "update_interval", |
264 | [hwmon_chip_alarms] = "alarms", | |
265 | }; | |
266 | ||
267 | static const char * const hwmon_temp_attr_templates[] = { | |
268 | [hwmon_temp_input] = "temp%d_input", | |
269 | [hwmon_temp_type] = "temp%d_type", | |
270 | [hwmon_temp_lcrit] = "temp%d_lcrit", | |
271 | [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst", | |
272 | [hwmon_temp_min] = "temp%d_min", | |
273 | [hwmon_temp_min_hyst] = "temp%d_min_hyst", | |
274 | [hwmon_temp_max] = "temp%d_max", | |
275 | [hwmon_temp_max_hyst] = "temp%d_max_hyst", | |
276 | [hwmon_temp_crit] = "temp%d_crit", | |
277 | [hwmon_temp_crit_hyst] = "temp%d_crit_hyst", | |
278 | [hwmon_temp_emergency] = "temp%d_emergency", | |
279 | [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst", | |
280 | [hwmon_temp_alarm] = "temp%d_alarm", | |
281 | [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm", | |
282 | [hwmon_temp_min_alarm] = "temp%d_min_alarm", | |
283 | [hwmon_temp_max_alarm] = "temp%d_max_alarm", | |
284 | [hwmon_temp_crit_alarm] = "temp%d_crit_alarm", | |
285 | [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm", | |
286 | [hwmon_temp_fault] = "temp%d_fault", | |
287 | [hwmon_temp_offset] = "temp%d_offset", | |
288 | [hwmon_temp_label] = "temp%d_label", | |
289 | [hwmon_temp_lowest] = "temp%d_lowest", | |
290 | [hwmon_temp_highest] = "temp%d_highest", | |
291 | [hwmon_temp_reset_history] = "temp%d_reset_history", | |
292 | }; | |
293 | ||
00d616cf GR |
294 | static const char * const hwmon_in_attr_templates[] = { |
295 | [hwmon_in_input] = "in%d_input", | |
296 | [hwmon_in_min] = "in%d_min", | |
297 | [hwmon_in_max] = "in%d_max", | |
298 | [hwmon_in_lcrit] = "in%d_lcrit", | |
299 | [hwmon_in_crit] = "in%d_crit", | |
300 | [hwmon_in_average] = "in%d_average", | |
301 | [hwmon_in_lowest] = "in%d_lowest", | |
302 | [hwmon_in_highest] = "in%d_highest", | |
303 | [hwmon_in_reset_history] = "in%d_reset_history", | |
304 | [hwmon_in_label] = "in%d_label", | |
305 | [hwmon_in_alarm] = "in%d_alarm", | |
306 | [hwmon_in_min_alarm] = "in%d_min_alarm", | |
307 | [hwmon_in_max_alarm] = "in%d_max_alarm", | |
308 | [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm", | |
309 | [hwmon_in_crit_alarm] = "in%d_crit_alarm", | |
310 | }; | |
311 | ||
d560168b GR |
312 | static const char * const *__templates[] = { |
313 | [hwmon_chip] = hwmon_chip_attr_templates, | |
314 | [hwmon_temp] = hwmon_temp_attr_templates, | |
00d616cf | 315 | [hwmon_in] = hwmon_in_attr_templates, |
d560168b GR |
316 | }; |
317 | ||
318 | static const int __templates_size[] = { | |
319 | [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attr_templates), | |
320 | [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates), | |
00d616cf | 321 | [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates), |
d560168b GR |
322 | }; |
323 | ||
324 | static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info) | |
325 | { | |
326 | int i, n; | |
327 | ||
328 | for (i = n = 0; info->config[i]; i++) | |
329 | n += hweight32(info->config[i]); | |
330 | ||
331 | return n; | |
332 | } | |
333 | ||
334 | static int hwmon_genattrs(struct device *dev, | |
335 | const void *drvdata, | |
336 | struct attribute **attrs, | |
337 | const struct hwmon_ops *ops, | |
338 | const struct hwmon_channel_info *info) | |
339 | { | |
340 | const char * const *templates; | |
341 | int template_size; | |
342 | int i, aindex = 0; | |
343 | ||
344 | if (info->type >= ARRAY_SIZE(__templates)) | |
345 | return -EINVAL; | |
346 | ||
347 | templates = __templates[info->type]; | |
348 | template_size = __templates_size[info->type]; | |
349 | ||
350 | for (i = 0; info->config[i]; i++) { | |
351 | u32 attr_mask = info->config[i]; | |
352 | u32 attr; | |
353 | ||
354 | while (attr_mask) { | |
355 | struct attribute *a; | |
356 | ||
357 | attr = __ffs(attr_mask); | |
358 | attr_mask &= ~BIT(attr); | |
359 | if (attr >= template_size) | |
360 | return -EINVAL; | |
361 | a = hwmon_genattr(dev, drvdata, info->type, attr, i, | |
362 | templates[attr], ops); | |
363 | if (IS_ERR(a)) { | |
364 | if (PTR_ERR(a) != -ENOENT) | |
365 | return PTR_ERR(a); | |
366 | continue; | |
367 | } | |
368 | attrs[aindex++] = a; | |
369 | } | |
370 | } | |
371 | return aindex; | |
372 | } | |
373 | ||
374 | static struct attribute ** | |
375 | __hwmon_create_attrs(struct device *dev, const void *drvdata, | |
376 | const struct hwmon_chip_info *chip) | |
377 | { | |
378 | int ret, i, aindex = 0, nattrs = 0; | |
379 | struct attribute **attrs; | |
380 | ||
381 | for (i = 0; chip->info[i]; i++) | |
382 | nattrs += hwmon_num_channel_attrs(chip->info[i]); | |
383 | ||
384 | if (nattrs == 0) | |
385 | return ERR_PTR(-EINVAL); | |
386 | ||
387 | attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL); | |
388 | if (!attrs) | |
389 | return ERR_PTR(-ENOMEM); | |
390 | ||
391 | for (i = 0; chip->info[i]; i++) { | |
392 | ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops, | |
393 | chip->info[i]); | |
394 | if (ret < 0) | |
395 | return ERR_PTR(ret); | |
396 | aindex += ret; | |
397 | } | |
398 | ||
399 | return attrs; | |
400 | } | |
401 | ||
402 | static struct device * | |
403 | __hwmon_device_register(struct device *dev, const char *name, void *drvdata, | |
404 | const struct hwmon_chip_info *chip, | |
405 | const struct attribute_group **groups) | |
1236441f | 406 | { |
bab2243c | 407 | struct hwmon_device *hwdev; |
d560168b GR |
408 | struct device *hdev; |
409 | int i, j, err, id; | |
ded2b666 | 410 | |
648cd48c GR |
411 | /* Do not accept invalid characters in hwmon name attribute */ |
412 | if (name && (!strlen(name) || strpbrk(name, "-* \t\n"))) | |
413 | return ERR_PTR(-EINVAL); | |
414 | ||
4ca5f468 JC |
415 | id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL); |
416 | if (id < 0) | |
417 | return ERR_PTR(id); | |
1236441f | 418 | |
bab2243c GR |
419 | hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL); |
420 | if (hwdev == NULL) { | |
421 | err = -ENOMEM; | |
422 | goto ida_remove; | |
423 | } | |
1236441f | 424 | |
d560168b GR |
425 | hdev = &hwdev->dev; |
426 | ||
427 | if (chip && chip->ops->is_visible) { | |
428 | struct attribute **attrs; | |
429 | int ngroups = 2; | |
430 | ||
431 | if (groups) | |
432 | for (i = 0; groups[i]; i++) | |
433 | ngroups++; | |
434 | ||
435 | hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups), | |
436 | GFP_KERNEL); | |
437 | if (!hwdev->groups) | |
438 | return ERR_PTR(-ENOMEM); | |
439 | ||
440 | attrs = __hwmon_create_attrs(dev, drvdata, chip); | |
441 | if (IS_ERR(attrs)) { | |
442 | err = PTR_ERR(attrs); | |
443 | goto free_hwmon; | |
444 | } | |
445 | ||
446 | hwdev->group.attrs = attrs; | |
447 | ngroups = 0; | |
448 | hwdev->groups[ngroups++] = &hwdev->group; | |
449 | ||
450 | if (groups) { | |
451 | for (i = 0; groups[i]; i++) | |
452 | hwdev->groups[ngroups++] = groups[i]; | |
453 | } | |
454 | ||
455 | hdev->groups = hwdev->groups; | |
456 | } else { | |
457 | hdev->groups = groups; | |
458 | } | |
459 | ||
bab2243c | 460 | hwdev->name = name; |
d560168b GR |
461 | hdev->class = &hwmon_class; |
462 | hdev->parent = dev; | |
463 | hdev->of_node = dev ? dev->of_node : NULL; | |
464 | hwdev->chip = chip; | |
465 | dev_set_drvdata(hdev, drvdata); | |
466 | dev_set_name(hdev, HWMON_ID_FORMAT, id); | |
467 | err = device_register(hdev); | |
bab2243c | 468 | if (err) |
d560168b GR |
469 | goto free_hwmon; |
470 | ||
471 | if (chip && chip->ops->is_visible && chip->ops->read && | |
472 | chip->info[0]->type == hwmon_chip && | |
473 | (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) { | |
474 | const struct hwmon_channel_info **info = chip->info; | |
475 | ||
476 | for (i = 1; info[i]; i++) { | |
477 | if (info[i]->type != hwmon_temp) | |
478 | continue; | |
479 | ||
480 | for (j = 0; info[i]->config[j]; j++) { | |
481 | if (!chip->ops->is_visible(drvdata, hwmon_temp, | |
482 | hwmon_temp_input, j)) | |
483 | continue; | |
484 | if (info[i]->config[j] & HWMON_T_INPUT) | |
485 | hwmon_thermal_add_sensor(dev, hwdev, j); | |
486 | } | |
487 | } | |
488 | } | |
bab2243c | 489 | |
d560168b | 490 | return hdev; |
bab2243c | 491 | |
d560168b | 492 | free_hwmon: |
bab2243c GR |
493 | kfree(hwdev); |
494 | ida_remove: | |
495 | ida_simple_remove(&hwmon_ida, id); | |
496 | return ERR_PTR(err); | |
497 | } | |
d560168b GR |
498 | |
499 | /** | |
500 | * hwmon_device_register_with_groups - register w/ hwmon | |
501 | * @dev: the parent device | |
502 | * @name: hwmon name attribute | |
503 | * @drvdata: driver data to attach to created device | |
504 | * @groups: List of attribute groups to create | |
505 | * | |
506 | * hwmon_device_unregister() must be called when the device is no | |
507 | * longer needed. | |
508 | * | |
509 | * Returns the pointer to the new device. | |
510 | */ | |
511 | struct device * | |
512 | hwmon_device_register_with_groups(struct device *dev, const char *name, | |
513 | void *drvdata, | |
514 | const struct attribute_group **groups) | |
515 | { | |
516 | return __hwmon_device_register(dev, name, drvdata, NULL, groups); | |
517 | } | |
bab2243c | 518 | EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups); |
1236441f | 519 | |
d560168b GR |
520 | /** |
521 | * hwmon_device_register_with_info - register w/ hwmon | |
522 | * @dev: the parent device | |
523 | * @name: hwmon name attribute | |
524 | * @drvdata: driver data to attach to created device | |
525 | * @info: Pointer to hwmon chip information | |
526 | * @groups - pointer to list of driver specific attribute groups | |
527 | * | |
528 | * hwmon_device_unregister() must be called when the device is no | |
529 | * longer needed. | |
530 | * | |
531 | * Returns the pointer to the new device. | |
532 | */ | |
533 | struct device * | |
534 | hwmon_device_register_with_info(struct device *dev, const char *name, | |
535 | void *drvdata, | |
536 | const struct hwmon_chip_info *chip, | |
537 | const struct attribute_group **groups) | |
538 | { | |
539 | if (chip && (!chip->ops || !chip->info)) | |
540 | return ERR_PTR(-EINVAL); | |
541 | ||
542 | return __hwmon_device_register(dev, name, drvdata, chip, groups); | |
543 | } | |
544 | EXPORT_SYMBOL_GPL(hwmon_device_register_with_info); | |
545 | ||
bab2243c GR |
546 | /** |
547 | * hwmon_device_register - register w/ hwmon | |
548 | * @dev: the device to register | |
549 | * | |
550 | * hwmon_device_unregister() must be called when the device is no | |
551 | * longer needed. | |
552 | * | |
553 | * Returns the pointer to the new device. | |
554 | */ | |
555 | struct device *hwmon_device_register(struct device *dev) | |
556 | { | |
557 | return hwmon_device_register_with_groups(dev, NULL, NULL, NULL); | |
1236441f | 558 | } |
839a9eef | 559 | EXPORT_SYMBOL_GPL(hwmon_device_register); |
1236441f MH |
560 | |
561 | /** | |
562 | * hwmon_device_unregister - removes the previously registered class device | |
563 | * | |
1beeffe4 | 564 | * @dev: the class device to destroy |
1236441f | 565 | */ |
1beeffe4 | 566 | void hwmon_device_unregister(struct device *dev) |
1236441f MH |
567 | { |
568 | int id; | |
569 | ||
739cf3a2 | 570 | if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) { |
1beeffe4 | 571 | device_unregister(dev); |
4ca5f468 | 572 | ida_simple_remove(&hwmon_ida, id); |
1236441f | 573 | } else |
1beeffe4 | 574 | dev_dbg(dev->parent, |
1236441f MH |
575 | "hwmon_device_unregister() failed: bad class ID!\n"); |
576 | } | |
839a9eef | 577 | EXPORT_SYMBOL_GPL(hwmon_device_unregister); |
1236441f | 578 | |
74188cba GR |
579 | static void devm_hwmon_release(struct device *dev, void *res) |
580 | { | |
581 | struct device *hwdev = *(struct device **)res; | |
582 | ||
583 | hwmon_device_unregister(hwdev); | |
584 | } | |
585 | ||
586 | /** | |
587 | * devm_hwmon_device_register_with_groups - register w/ hwmon | |
588 | * @dev: the parent device | |
589 | * @name: hwmon name attribute | |
590 | * @drvdata: driver data to attach to created device | |
591 | * @groups: List of attribute groups to create | |
592 | * | |
593 | * Returns the pointer to the new device. The new device is automatically | |
594 | * unregistered with the parent device. | |
595 | */ | |
596 | struct device * | |
597 | devm_hwmon_device_register_with_groups(struct device *dev, const char *name, | |
598 | void *drvdata, | |
599 | const struct attribute_group **groups) | |
600 | { | |
601 | struct device **ptr, *hwdev; | |
602 | ||
603 | if (!dev) | |
604 | return ERR_PTR(-EINVAL); | |
605 | ||
606 | ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); | |
607 | if (!ptr) | |
608 | return ERR_PTR(-ENOMEM); | |
609 | ||
610 | hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups); | |
611 | if (IS_ERR(hwdev)) | |
612 | goto error; | |
613 | ||
614 | *ptr = hwdev; | |
615 | devres_add(dev, ptr); | |
616 | return hwdev; | |
617 | ||
618 | error: | |
619 | devres_free(ptr); | |
620 | return hwdev; | |
621 | } | |
622 | EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups); | |
623 | ||
d560168b GR |
624 | /** |
625 | * devm_hwmon_device_register_with_info - register w/ hwmon | |
626 | * @dev: the parent device | |
627 | * @name: hwmon name attribute | |
628 | * @drvdata: driver data to attach to created device | |
629 | * @info: Pointer to hwmon chip information | |
630 | * @groups - pointer to list of driver specific attribute groups | |
631 | * | |
632 | * Returns the pointer to the new device. The new device is automatically | |
633 | * unregistered with the parent device. | |
634 | */ | |
635 | struct device * | |
636 | devm_hwmon_device_register_with_info(struct device *dev, const char *name, | |
637 | void *drvdata, | |
638 | const struct hwmon_chip_info *chip, | |
639 | const struct attribute_group **groups) | |
640 | { | |
641 | struct device **ptr, *hwdev; | |
642 | ||
643 | if (!dev) | |
644 | return ERR_PTR(-EINVAL); | |
645 | ||
646 | ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); | |
647 | if (!ptr) | |
648 | return ERR_PTR(-ENOMEM); | |
649 | ||
650 | hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip, | |
651 | groups); | |
652 | if (IS_ERR(hwdev)) | |
653 | goto error; | |
654 | ||
655 | *ptr = hwdev; | |
656 | devres_add(dev, ptr); | |
657 | ||
658 | return hwdev; | |
659 | ||
660 | error: | |
661 | devres_free(ptr); | |
662 | return hwdev; | |
663 | } | |
664 | EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info); | |
665 | ||
74188cba GR |
666 | static int devm_hwmon_match(struct device *dev, void *res, void *data) |
667 | { | |
668 | struct device **hwdev = res; | |
669 | ||
670 | return *hwdev == data; | |
671 | } | |
672 | ||
673 | /** | |
674 | * devm_hwmon_device_unregister - removes a previously registered hwmon device | |
675 | * | |
676 | * @dev: the parent device of the device to unregister | |
677 | */ | |
678 | void devm_hwmon_device_unregister(struct device *dev) | |
679 | { | |
680 | WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev)); | |
681 | } | |
682 | EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister); | |
683 | ||
2958b1ec JD |
684 | static void __init hwmon_pci_quirks(void) |
685 | { | |
686 | #if defined CONFIG_X86 && defined CONFIG_PCI | |
687 | struct pci_dev *sb; | |
688 | u16 base; | |
689 | u8 enable; | |
690 | ||
691 | /* Open access to 0x295-0x296 on MSI MS-7031 */ | |
692 | sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL); | |
d6dab7dd JD |
693 | if (sb) { |
694 | if (sb->subsystem_vendor == 0x1462 && /* MSI */ | |
695 | sb->subsystem_device == 0x0031) { /* MS-7031 */ | |
696 | pci_read_config_byte(sb, 0x48, &enable); | |
697 | pci_read_config_word(sb, 0x64, &base); | |
698 | ||
699 | if (base == 0 && !(enable & BIT(2))) { | |
700 | dev_info(&sb->dev, | |
701 | "Opening wide generic port at 0x295\n"); | |
702 | pci_write_config_word(sb, 0x64, 0x295); | |
703 | pci_write_config_byte(sb, 0x48, | |
704 | enable | BIT(2)); | |
705 | } | |
2958b1ec | 706 | } |
d6dab7dd | 707 | pci_dev_put(sb); |
2958b1ec JD |
708 | } |
709 | #endif | |
710 | } | |
711 | ||
1236441f MH |
712 | static int __init hwmon_init(void) |
713 | { | |
bab2243c GR |
714 | int err; |
715 | ||
2958b1ec JD |
716 | hwmon_pci_quirks(); |
717 | ||
bab2243c GR |
718 | err = class_register(&hwmon_class); |
719 | if (err) { | |
720 | pr_err("couldn't register hwmon sysfs class\n"); | |
721 | return err; | |
1236441f MH |
722 | } |
723 | return 0; | |
724 | } | |
725 | ||
726 | static void __exit hwmon_exit(void) | |
727 | { | |
bab2243c | 728 | class_unregister(&hwmon_class); |
1236441f MH |
729 | } |
730 | ||
37f54ee5 | 731 | subsys_initcall(hwmon_init); |
1236441f MH |
732 | module_exit(hwmon_exit); |
733 | ||
1236441f MH |
734 | MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>"); |
735 | MODULE_DESCRIPTION("hardware monitoring sysfs/class support"); | |
736 | MODULE_LICENSE("GPL"); | |
737 |