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