Commit | Line | Data |
---|---|---|
b886d83c | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1236441f | 2 | /* |
5ed04880 GR |
3 | * hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring |
4 | * | |
5 | * This file defines the sysfs class "hwmon", for use by sensors drivers. | |
6 | * | |
7 | * Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com> | |
5ed04880 | 8 | */ |
1236441f | 9 | |
c95df1ae JP |
10 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
11 | ||
d560168b | 12 | #include <linux/bitops.h> |
1236441f MH |
13 | #include <linux/device.h> |
14 | #include <linux/err.h> | |
8c65b4a6 | 15 | #include <linux/gfp.h> |
c9ebbe6f GR |
16 | #include <linux/hwmon.h> |
17 | #include <linux/idr.h> | |
25f98688 | 18 | #include <linux/kstrtox.h> |
1597b374 | 19 | #include <linux/list.h> |
c9ebbe6f | 20 | #include <linux/module.h> |
2958b1ec | 21 | #include <linux/pci.h> |
e1c9d6d6 | 22 | #include <linux/property.h> |
c9ebbe6f | 23 | #include <linux/slab.h> |
648cd48c | 24 | #include <linux/string.h> |
d560168b | 25 | #include <linux/thermal.h> |
1236441f | 26 | |
61b8ab2c NC |
27 | #define CREATE_TRACE_POINTS |
28 | #include <trace/events/hwmon.h> | |
29 | ||
1236441f MH |
30 | #define HWMON_ID_PREFIX "hwmon" |
31 | #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d" | |
32 | ||
bab2243c GR |
33 | struct hwmon_device { |
34 | const char *name; | |
e1c9d6d6 | 35 | const char *label; |
bab2243c | 36 | struct device dev; |
d560168b | 37 | const struct hwmon_chip_info *chip; |
1597b374 | 38 | struct list_head tzdata; |
d560168b GR |
39 | struct attribute_group group; |
40 | const struct attribute_group **groups; | |
bab2243c | 41 | }; |
d560168b | 42 | |
bab2243c GR |
43 | #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev) |
44 | ||
3a412d5e GR |
45 | #define MAX_SYSFS_ATTR_NAME_LENGTH 32 |
46 | ||
d560168b GR |
47 | struct hwmon_device_attribute { |
48 | struct device_attribute dev_attr; | |
49 | const struct hwmon_ops *ops; | |
50 | enum hwmon_sensor_types type; | |
51 | u32 attr; | |
52 | int index; | |
3a412d5e | 53 | char name[MAX_SYSFS_ATTR_NAME_LENGTH]; |
d560168b GR |
54 | }; |
55 | ||
56 | #define to_hwmon_attr(d) \ | |
57 | container_of(d, struct hwmon_device_attribute, dev_attr) | |
3bf8bdcf | 58 | #define to_dev_attr(a) container_of(a, struct device_attribute, attr) |
d560168b GR |
59 | |
60 | /* | |
61 | * Thermal zone information | |
d560168b GR |
62 | */ |
63 | struct hwmon_thermal_data { | |
1597b374 | 64 | struct list_head node; /* hwmon tzdata list entry */ |
3bf8bdcf | 65 | struct device *dev; /* Reference to hwmon device */ |
d560168b | 66 | int index; /* sensor index */ |
1597b374 | 67 | struct thermal_zone_device *tzd;/* thermal zone device */ |
d560168b GR |
68 | }; |
69 | ||
bab2243c | 70 | static ssize_t |
2ab0c6c5 | 71 | name_show(struct device *dev, struct device_attribute *attr, char *buf) |
bab2243c GR |
72 | { |
73 | return sprintf(buf, "%s\n", to_hwmon_device(dev)->name); | |
74 | } | |
2ab0c6c5 | 75 | static DEVICE_ATTR_RO(name); |
bab2243c | 76 | |
e1c9d6d6 PC |
77 | static ssize_t |
78 | label_show(struct device *dev, struct device_attribute *attr, char *buf) | |
79 | { | |
80 | return sysfs_emit(buf, "%s\n", to_hwmon_device(dev)->label); | |
81 | } | |
82 | static DEVICE_ATTR_RO(label); | |
83 | ||
bab2243c GR |
84 | static struct attribute *hwmon_dev_attrs[] = { |
85 | &dev_attr_name.attr, | |
e1c9d6d6 | 86 | &dev_attr_label.attr, |
bab2243c GR |
87 | NULL |
88 | }; | |
89 | ||
e1c9d6d6 | 90 | static umode_t hwmon_dev_attr_is_visible(struct kobject *kobj, |
bab2243c GR |
91 | struct attribute *attr, int n) |
92 | { | |
77d76768 | 93 | struct device *dev = kobj_to_dev(kobj); |
e1c9d6d6 | 94 | struct hwmon_device *hdev = to_hwmon_device(dev); |
bab2243c | 95 | |
e1c9d6d6 PC |
96 | if (attr == &dev_attr_name.attr && hdev->name == NULL) |
97 | return 0; | |
98 | ||
99 | if (attr == &dev_attr_label.attr && hdev->label == NULL) | |
bab2243c GR |
100 | return 0; |
101 | ||
102 | return attr->mode; | |
103 | } | |
104 | ||
524703ac | 105 | static const struct attribute_group hwmon_dev_attr_group = { |
bab2243c | 106 | .attrs = hwmon_dev_attrs, |
e1c9d6d6 | 107 | .is_visible = hwmon_dev_attr_is_visible, |
bab2243c GR |
108 | }; |
109 | ||
110 | static const struct attribute_group *hwmon_dev_attr_groups[] = { | |
111 | &hwmon_dev_attr_group, | |
112 | NULL | |
113 | }; | |
114 | ||
3bf8bdcf GR |
115 | static void hwmon_free_attrs(struct attribute **attrs) |
116 | { | |
117 | int i; | |
118 | ||
119 | for (i = 0; attrs[i]; i++) { | |
120 | struct device_attribute *dattr = to_dev_attr(attrs[i]); | |
121 | struct hwmon_device_attribute *hattr = to_hwmon_attr(dattr); | |
122 | ||
123 | kfree(hattr); | |
124 | } | |
125 | kfree(attrs); | |
126 | } | |
127 | ||
bab2243c GR |
128 | static void hwmon_dev_release(struct device *dev) |
129 | { | |
3bf8bdcf GR |
130 | struct hwmon_device *hwdev = to_hwmon_device(dev); |
131 | ||
132 | if (hwdev->group.attrs) | |
133 | hwmon_free_attrs(hwdev->group.attrs); | |
134 | kfree(hwdev->groups); | |
e1c9d6d6 | 135 | kfree(hwdev->label); |
3bf8bdcf | 136 | kfree(hwdev); |
bab2243c GR |
137 | } |
138 | ||
139 | static struct class hwmon_class = { | |
140 | .name = "hwmon", | |
141 | .owner = THIS_MODULE, | |
142 | .dev_groups = hwmon_dev_attr_groups, | |
143 | .dev_release = hwmon_dev_release, | |
144 | }; | |
1236441f | 145 | |
4ca5f468 | 146 | static DEFINE_IDA(hwmon_ida); |
1236441f | 147 | |
d560168b GR |
148 | /* Thermal zone handling */ |
149 | ||
86430c1a GR |
150 | /* |
151 | * The complex conditional is necessary to avoid a cyclic dependency | |
152 | * between hwmon and thermal_sys modules. | |
153 | */ | |
f3735332 | 154 | #ifdef CONFIG_THERMAL_OF |
e5181331 | 155 | static int hwmon_thermal_get_temp(struct thermal_zone_device *tz, int *temp) |
d560168b | 156 | { |
e5181331 | 157 | struct hwmon_thermal_data *tdata = tz->devdata; |
3bf8bdcf | 158 | struct hwmon_device *hwdev = to_hwmon_device(tdata->dev); |
d560168b GR |
159 | int ret; |
160 | long t; | |
161 | ||
3bf8bdcf | 162 | ret = hwdev->chip->ops->read(tdata->dev, hwmon_temp, hwmon_temp_input, |
d560168b GR |
163 | tdata->index, &t); |
164 | if (ret < 0) | |
165 | return ret; | |
166 | ||
167 | *temp = t; | |
168 | ||
169 | return 0; | |
170 | } | |
171 | ||
e5181331 | 172 | static int hwmon_thermal_set_trips(struct thermal_zone_device *tz, int low, int high) |
a5f6c0f8 | 173 | { |
e5181331 | 174 | struct hwmon_thermal_data *tdata = tz->devdata; |
a5f6c0f8 DO |
175 | struct hwmon_device *hwdev = to_hwmon_device(tdata->dev); |
176 | const struct hwmon_chip_info *chip = hwdev->chip; | |
177 | const struct hwmon_channel_info **info = chip->info; | |
178 | unsigned int i; | |
179 | int err; | |
180 | ||
181 | if (!chip->ops->write) | |
182 | return 0; | |
183 | ||
184 | for (i = 0; info[i] && info[i]->type != hwmon_temp; i++) | |
185 | continue; | |
186 | ||
187 | if (!info[i]) | |
188 | return 0; | |
189 | ||
190 | if (info[i]->config[tdata->index] & HWMON_T_MIN) { | |
191 | err = chip->ops->write(tdata->dev, hwmon_temp, | |
192 | hwmon_temp_min, tdata->index, low); | |
193 | if (err && err != -EOPNOTSUPP) | |
194 | return err; | |
195 | } | |
196 | ||
197 | if (info[i]->config[tdata->index] & HWMON_T_MAX) { | |
198 | err = chip->ops->write(tdata->dev, hwmon_temp, | |
199 | hwmon_temp_max, tdata->index, high); | |
200 | if (err && err != -EOPNOTSUPP) | |
201 | return err; | |
202 | } | |
203 | ||
204 | return 0; | |
205 | } | |
206 | ||
e5181331 | 207 | static const struct thermal_zone_device_ops hwmon_thermal_ops = { |
d560168b | 208 | .get_temp = hwmon_thermal_get_temp, |
a5f6c0f8 | 209 | .set_trips = hwmon_thermal_set_trips, |
d560168b GR |
210 | }; |
211 | ||
1597b374 GR |
212 | static void hwmon_thermal_remove_sensor(void *data) |
213 | { | |
214 | list_del(data); | |
215 | } | |
216 | ||
3bf8bdcf | 217 | static int hwmon_thermal_add_sensor(struct device *dev, int index) |
d560168b | 218 | { |
1597b374 | 219 | struct hwmon_device *hwdev = to_hwmon_device(dev); |
d560168b | 220 | struct hwmon_thermal_data *tdata; |
47c332de | 221 | struct thermal_zone_device *tzd; |
1597b374 | 222 | int err; |
d560168b GR |
223 | |
224 | tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL); | |
225 | if (!tdata) | |
226 | return -ENOMEM; | |
227 | ||
3bf8bdcf | 228 | tdata->dev = dev; |
d560168b GR |
229 | tdata->index = index; |
230 | ||
e5181331 DL |
231 | tzd = devm_thermal_of_zone_register(dev, index, tdata, |
232 | &hwmon_thermal_ops); | |
1b5f517c GR |
233 | if (IS_ERR(tzd)) { |
234 | if (PTR_ERR(tzd) != -ENODEV) | |
235 | return PTR_ERR(tzd); | |
236 | dev_info(dev, "temp%d_input not attached to any thermal zone\n", | |
237 | index + 1); | |
238 | devm_kfree(dev, tdata); | |
239 | return 0; | |
240 | } | |
d560168b | 241 | |
1597b374 GR |
242 | err = devm_add_action(dev, hwmon_thermal_remove_sensor, &tdata->node); |
243 | if (err) | |
244 | return err; | |
245 | ||
246 | tdata->tzd = tzd; | |
247 | list_add(&tdata->node, &hwdev->tzdata); | |
248 | ||
d560168b GR |
249 | return 0; |
250 | } | |
44e3ad88 AM |
251 | |
252 | static int hwmon_thermal_register_sensors(struct device *dev) | |
253 | { | |
254 | struct hwmon_device *hwdev = to_hwmon_device(dev); | |
255 | const struct hwmon_chip_info *chip = hwdev->chip; | |
256 | const struct hwmon_channel_info **info = chip->info; | |
257 | void *drvdata = dev_get_drvdata(dev); | |
258 | int i; | |
259 | ||
260 | for (i = 1; info[i]; i++) { | |
261 | int j; | |
262 | ||
263 | if (info[i]->type != hwmon_temp) | |
264 | continue; | |
265 | ||
266 | for (j = 0; info[i]->config[j]; j++) { | |
267 | int err; | |
268 | ||
269 | if (!(info[i]->config[j] & HWMON_T_INPUT) || | |
270 | !chip->ops->is_visible(drvdata, hwmon_temp, | |
271 | hwmon_temp_input, j)) | |
272 | continue; | |
273 | ||
274 | err = hwmon_thermal_add_sensor(dev, j); | |
275 | if (err) | |
276 | return err; | |
277 | } | |
278 | } | |
279 | ||
280 | return 0; | |
281 | } | |
282 | ||
1597b374 GR |
283 | static void hwmon_thermal_notify(struct device *dev, int index) |
284 | { | |
285 | struct hwmon_device *hwdev = to_hwmon_device(dev); | |
286 | struct hwmon_thermal_data *tzdata; | |
287 | ||
288 | list_for_each_entry(tzdata, &hwdev->tzdata, node) { | |
289 | if (tzdata->index == index) { | |
290 | thermal_zone_device_update(tzdata->tzd, | |
291 | THERMAL_EVENT_UNSPECIFIED); | |
292 | } | |
293 | } | |
294 | } | |
295 | ||
d560168b | 296 | #else |
44e3ad88 | 297 | static int hwmon_thermal_register_sensors(struct device *dev) |
d560168b GR |
298 | { |
299 | return 0; | |
300 | } | |
1597b374 GR |
301 | |
302 | static void hwmon_thermal_notify(struct device *dev, int index) { } | |
303 | ||
86430c1a | 304 | #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */ |
d560168b | 305 | |
61b8ab2c NC |
306 | static int hwmon_attr_base(enum hwmon_sensor_types type) |
307 | { | |
4413405f | 308 | if (type == hwmon_in || type == hwmon_intrusion) |
61b8ab2c NC |
309 | return 0; |
310 | return 1; | |
311 | } | |
312 | ||
d560168b GR |
313 | /* sysfs attribute management */ |
314 | ||
315 | static ssize_t hwmon_attr_show(struct device *dev, | |
316 | struct device_attribute *devattr, char *buf) | |
317 | { | |
318 | struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); | |
319 | long val; | |
320 | int ret; | |
321 | ||
322 | ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index, | |
323 | &val); | |
324 | if (ret < 0) | |
325 | return ret; | |
326 | ||
61b8ab2c NC |
327 | trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type), |
328 | hattr->name, val); | |
329 | ||
d560168b GR |
330 | return sprintf(buf, "%ld\n", val); |
331 | } | |
332 | ||
e159ab5c GR |
333 | static ssize_t hwmon_attr_show_string(struct device *dev, |
334 | struct device_attribute *devattr, | |
335 | char *buf) | |
336 | { | |
337 | struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); | |
61b8ab2c | 338 | enum hwmon_sensor_types type = hattr->type; |
5ba6bcbc | 339 | const char *s; |
e159ab5c GR |
340 | int ret; |
341 | ||
342 | ret = hattr->ops->read_string(dev, hattr->type, hattr->attr, | |
343 | hattr->index, &s); | |
344 | if (ret < 0) | |
345 | return ret; | |
346 | ||
61b8ab2c NC |
347 | trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type), |
348 | hattr->name, s); | |
349 | ||
e159ab5c GR |
350 | return sprintf(buf, "%s\n", s); |
351 | } | |
352 | ||
d560168b GR |
353 | static ssize_t hwmon_attr_store(struct device *dev, |
354 | struct device_attribute *devattr, | |
355 | const char *buf, size_t count) | |
356 | { | |
357 | struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr); | |
358 | long val; | |
359 | int ret; | |
360 | ||
361 | ret = kstrtol(buf, 10, &val); | |
362 | if (ret < 0) | |
363 | return ret; | |
364 | ||
365 | ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index, | |
366 | val); | |
367 | if (ret < 0) | |
368 | return ret; | |
369 | ||
61b8ab2c NC |
370 | trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type), |
371 | hattr->name, val); | |
d560168b | 372 | |
61b8ab2c | 373 | return count; |
d560168b GR |
374 | } |
375 | ||
e159ab5c GR |
376 | static bool is_string_attr(enum hwmon_sensor_types type, u32 attr) |
377 | { | |
378 | return (type == hwmon_temp && attr == hwmon_temp_label) || | |
379 | (type == hwmon_in && attr == hwmon_in_label) || | |
380 | (type == hwmon_curr && attr == hwmon_curr_label) || | |
381 | (type == hwmon_power && attr == hwmon_power_label) || | |
382 | (type == hwmon_energy && attr == hwmon_energy_label) || | |
383 | (type == hwmon_humidity && attr == hwmon_humidity_label) || | |
384 | (type == hwmon_fan && attr == hwmon_fan_label); | |
385 | } | |
386 | ||
3bf8bdcf | 387 | static struct attribute *hwmon_genattr(const void *drvdata, |
d560168b GR |
388 | enum hwmon_sensor_types type, |
389 | u32 attr, | |
390 | int index, | |
391 | const char *template, | |
392 | const struct hwmon_ops *ops) | |
393 | { | |
394 | struct hwmon_device_attribute *hattr; | |
395 | struct device_attribute *dattr; | |
396 | struct attribute *a; | |
397 | umode_t mode; | |
3b443def | 398 | const char *name; |
e159ab5c | 399 | bool is_string = is_string_attr(type, attr); |
d560168b GR |
400 | |
401 | /* The attribute is invisible if there is no template string */ | |
402 | if (!template) | |
403 | return ERR_PTR(-ENOENT); | |
404 | ||
405 | mode = ops->is_visible(drvdata, type, attr, index); | |
406 | if (!mode) | |
407 | return ERR_PTR(-ENOENT); | |
408 | ||
0d87116f | 409 | if ((mode & 0444) && ((is_string && !ops->read_string) || |
e159ab5c | 410 | (!is_string && !ops->read))) |
d560168b | 411 | return ERR_PTR(-EINVAL); |
0d87116f | 412 | if ((mode & 0222) && !ops->write) |
d560168b GR |
413 | return ERR_PTR(-EINVAL); |
414 | ||
3bf8bdcf | 415 | hattr = kzalloc(sizeof(*hattr), GFP_KERNEL); |
3a412d5e GR |
416 | if (!hattr) |
417 | return ERR_PTR(-ENOMEM); | |
418 | ||
d560168b | 419 | if (type == hwmon_chip) { |
3b443def | 420 | name = template; |
d560168b | 421 | } else { |
3a412d5e | 422 | scnprintf(hattr->name, sizeof(hattr->name), template, |
d560168b | 423 | index + hwmon_attr_base(type)); |
3a412d5e | 424 | name = hattr->name; |
d560168b GR |
425 | } |
426 | ||
d560168b GR |
427 | hattr->type = type; |
428 | hattr->attr = attr; | |
429 | hattr->index = index; | |
430 | hattr->ops = ops; | |
431 | ||
432 | dattr = &hattr->dev_attr; | |
e159ab5c | 433 | dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show; |
d560168b GR |
434 | dattr->store = hwmon_attr_store; |
435 | ||
436 | a = &dattr->attr; | |
437 | sysfs_attr_init(a); | |
438 | a->name = name; | |
439 | a->mode = mode; | |
440 | ||
441 | return a; | |
442 | } | |
443 | ||
f4d325d5 GR |
444 | /* |
445 | * Chip attributes are not attribute templates but actual sysfs attributes. | |
446 | * See hwmon_genattr() for special handling. | |
447 | */ | |
448 | static const char * const hwmon_chip_attrs[] = { | |
d560168b | 449 | [hwmon_chip_temp_reset_history] = "temp_reset_history", |
00d616cf | 450 | [hwmon_chip_in_reset_history] = "in_reset_history", |
9b26947c | 451 | [hwmon_chip_curr_reset_history] = "curr_reset_history", |
b308f5c7 | 452 | [hwmon_chip_power_reset_history] = "power_reset_history", |
d560168b GR |
453 | [hwmon_chip_update_interval] = "update_interval", |
454 | [hwmon_chip_alarms] = "alarms", | |
9f00995e GR |
455 | [hwmon_chip_samples] = "samples", |
456 | [hwmon_chip_curr_samples] = "curr_samples", | |
457 | [hwmon_chip_in_samples] = "in_samples", | |
458 | [hwmon_chip_power_samples] = "power_samples", | |
459 | [hwmon_chip_temp_samples] = "temp_samples", | |
d560168b GR |
460 | }; |
461 | ||
462 | static const char * const hwmon_temp_attr_templates[] = { | |
002c6b54 | 463 | [hwmon_temp_enable] = "temp%d_enable", |
d560168b GR |
464 | [hwmon_temp_input] = "temp%d_input", |
465 | [hwmon_temp_type] = "temp%d_type", | |
466 | [hwmon_temp_lcrit] = "temp%d_lcrit", | |
467 | [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst", | |
468 | [hwmon_temp_min] = "temp%d_min", | |
469 | [hwmon_temp_min_hyst] = "temp%d_min_hyst", | |
470 | [hwmon_temp_max] = "temp%d_max", | |
471 | [hwmon_temp_max_hyst] = "temp%d_max_hyst", | |
472 | [hwmon_temp_crit] = "temp%d_crit", | |
473 | [hwmon_temp_crit_hyst] = "temp%d_crit_hyst", | |
474 | [hwmon_temp_emergency] = "temp%d_emergency", | |
475 | [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst", | |
476 | [hwmon_temp_alarm] = "temp%d_alarm", | |
477 | [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm", | |
478 | [hwmon_temp_min_alarm] = "temp%d_min_alarm", | |
479 | [hwmon_temp_max_alarm] = "temp%d_max_alarm", | |
480 | [hwmon_temp_crit_alarm] = "temp%d_crit_alarm", | |
481 | [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm", | |
482 | [hwmon_temp_fault] = "temp%d_fault", | |
483 | [hwmon_temp_offset] = "temp%d_offset", | |
484 | [hwmon_temp_label] = "temp%d_label", | |
485 | [hwmon_temp_lowest] = "temp%d_lowest", | |
486 | [hwmon_temp_highest] = "temp%d_highest", | |
487 | [hwmon_temp_reset_history] = "temp%d_reset_history", | |
1967f712 ZL |
488 | [hwmon_temp_rated_min] = "temp%d_rated_min", |
489 | [hwmon_temp_rated_max] = "temp%d_rated_max", | |
d560168b GR |
490 | }; |
491 | ||
00d616cf | 492 | static const char * const hwmon_in_attr_templates[] = { |
002c6b54 | 493 | [hwmon_in_enable] = "in%d_enable", |
00d616cf GR |
494 | [hwmon_in_input] = "in%d_input", |
495 | [hwmon_in_min] = "in%d_min", | |
496 | [hwmon_in_max] = "in%d_max", | |
497 | [hwmon_in_lcrit] = "in%d_lcrit", | |
498 | [hwmon_in_crit] = "in%d_crit", | |
499 | [hwmon_in_average] = "in%d_average", | |
500 | [hwmon_in_lowest] = "in%d_lowest", | |
501 | [hwmon_in_highest] = "in%d_highest", | |
502 | [hwmon_in_reset_history] = "in%d_reset_history", | |
503 | [hwmon_in_label] = "in%d_label", | |
504 | [hwmon_in_alarm] = "in%d_alarm", | |
505 | [hwmon_in_min_alarm] = "in%d_min_alarm", | |
506 | [hwmon_in_max_alarm] = "in%d_max_alarm", | |
507 | [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm", | |
508 | [hwmon_in_crit_alarm] = "in%d_crit_alarm", | |
1967f712 ZL |
509 | [hwmon_in_rated_min] = "in%d_rated_min", |
510 | [hwmon_in_rated_max] = "in%d_rated_max", | |
00d616cf GR |
511 | }; |
512 | ||
9b26947c | 513 | static const char * const hwmon_curr_attr_templates[] = { |
002c6b54 | 514 | [hwmon_curr_enable] = "curr%d_enable", |
9b26947c GR |
515 | [hwmon_curr_input] = "curr%d_input", |
516 | [hwmon_curr_min] = "curr%d_min", | |
517 | [hwmon_curr_max] = "curr%d_max", | |
518 | [hwmon_curr_lcrit] = "curr%d_lcrit", | |
519 | [hwmon_curr_crit] = "curr%d_crit", | |
520 | [hwmon_curr_average] = "curr%d_average", | |
521 | [hwmon_curr_lowest] = "curr%d_lowest", | |
522 | [hwmon_curr_highest] = "curr%d_highest", | |
523 | [hwmon_curr_reset_history] = "curr%d_reset_history", | |
524 | [hwmon_curr_label] = "curr%d_label", | |
525 | [hwmon_curr_alarm] = "curr%d_alarm", | |
526 | [hwmon_curr_min_alarm] = "curr%d_min_alarm", | |
527 | [hwmon_curr_max_alarm] = "curr%d_max_alarm", | |
528 | [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm", | |
529 | [hwmon_curr_crit_alarm] = "curr%d_crit_alarm", | |
1967f712 ZL |
530 | [hwmon_curr_rated_min] = "curr%d_rated_min", |
531 | [hwmon_curr_rated_max] = "curr%d_rated_max", | |
9b26947c GR |
532 | }; |
533 | ||
b308f5c7 | 534 | static const char * const hwmon_power_attr_templates[] = { |
002c6b54 | 535 | [hwmon_power_enable] = "power%d_enable", |
b308f5c7 GR |
536 | [hwmon_power_average] = "power%d_average", |
537 | [hwmon_power_average_interval] = "power%d_average_interval", | |
538 | [hwmon_power_average_interval_max] = "power%d_interval_max", | |
539 | [hwmon_power_average_interval_min] = "power%d_interval_min", | |
540 | [hwmon_power_average_highest] = "power%d_average_highest", | |
541 | [hwmon_power_average_lowest] = "power%d_average_lowest", | |
542 | [hwmon_power_average_max] = "power%d_average_max", | |
543 | [hwmon_power_average_min] = "power%d_average_min", | |
544 | [hwmon_power_input] = "power%d_input", | |
545 | [hwmon_power_input_highest] = "power%d_input_highest", | |
546 | [hwmon_power_input_lowest] = "power%d_input_lowest", | |
547 | [hwmon_power_reset_history] = "power%d_reset_history", | |
548 | [hwmon_power_accuracy] = "power%d_accuracy", | |
549 | [hwmon_power_cap] = "power%d_cap", | |
550 | [hwmon_power_cap_hyst] = "power%d_cap_hyst", | |
551 | [hwmon_power_cap_max] = "power%d_cap_max", | |
552 | [hwmon_power_cap_min] = "power%d_cap_min", | |
aa7f29b0 | 553 | [hwmon_power_min] = "power%d_min", |
b308f5c7 | 554 | [hwmon_power_max] = "power%d_max", |
aa7f29b0 | 555 | [hwmon_power_lcrit] = "power%d_lcrit", |
b308f5c7 GR |
556 | [hwmon_power_crit] = "power%d_crit", |
557 | [hwmon_power_label] = "power%d_label", | |
558 | [hwmon_power_alarm] = "power%d_alarm", | |
559 | [hwmon_power_cap_alarm] = "power%d_cap_alarm", | |
aa7f29b0 | 560 | [hwmon_power_min_alarm] = "power%d_min_alarm", |
b308f5c7 | 561 | [hwmon_power_max_alarm] = "power%d_max_alarm", |
aa7f29b0 | 562 | [hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm", |
b308f5c7 | 563 | [hwmon_power_crit_alarm] = "power%d_crit_alarm", |
1967f712 ZL |
564 | [hwmon_power_rated_min] = "power%d_rated_min", |
565 | [hwmon_power_rated_max] = "power%d_rated_max", | |
b308f5c7 GR |
566 | }; |
567 | ||
6bfcca44 | 568 | static const char * const hwmon_energy_attr_templates[] = { |
002c6b54 | 569 | [hwmon_energy_enable] = "energy%d_enable", |
6bfcca44 GR |
570 | [hwmon_energy_input] = "energy%d_input", |
571 | [hwmon_energy_label] = "energy%d_label", | |
572 | }; | |
573 | ||
574 | static const char * const hwmon_humidity_attr_templates[] = { | |
002c6b54 | 575 | [hwmon_humidity_enable] = "humidity%d_enable", |
6bfcca44 GR |
576 | [hwmon_humidity_input] = "humidity%d_input", |
577 | [hwmon_humidity_label] = "humidity%d_label", | |
578 | [hwmon_humidity_min] = "humidity%d_min", | |
579 | [hwmon_humidity_min_hyst] = "humidity%d_min_hyst", | |
580 | [hwmon_humidity_max] = "humidity%d_max", | |
581 | [hwmon_humidity_max_hyst] = "humidity%d_max_hyst", | |
582 | [hwmon_humidity_alarm] = "humidity%d_alarm", | |
583 | [hwmon_humidity_fault] = "humidity%d_fault", | |
1967f712 ZL |
584 | [hwmon_humidity_rated_min] = "humidity%d_rated_min", |
585 | [hwmon_humidity_rated_max] = "humidity%d_rated_max", | |
6bfcca44 GR |
586 | }; |
587 | ||
8faee73f | 588 | static const char * const hwmon_fan_attr_templates[] = { |
002c6b54 | 589 | [hwmon_fan_enable] = "fan%d_enable", |
8faee73f GR |
590 | [hwmon_fan_input] = "fan%d_input", |
591 | [hwmon_fan_label] = "fan%d_label", | |
592 | [hwmon_fan_min] = "fan%d_min", | |
593 | [hwmon_fan_max] = "fan%d_max", | |
594 | [hwmon_fan_div] = "fan%d_div", | |
595 | [hwmon_fan_pulses] = "fan%d_pulses", | |
596 | [hwmon_fan_target] = "fan%d_target", | |
597 | [hwmon_fan_alarm] = "fan%d_alarm", | |
598 | [hwmon_fan_min_alarm] = "fan%d_min_alarm", | |
599 | [hwmon_fan_max_alarm] = "fan%d_max_alarm", | |
600 | [hwmon_fan_fault] = "fan%d_fault", | |
601 | }; | |
602 | ||
f9f7bb3a GR |
603 | static const char * const hwmon_pwm_attr_templates[] = { |
604 | [hwmon_pwm_input] = "pwm%d", | |
605 | [hwmon_pwm_enable] = "pwm%d_enable", | |
606 | [hwmon_pwm_mode] = "pwm%d_mode", | |
607 | [hwmon_pwm_freq] = "pwm%d_freq", | |
e75d16e5 | 608 | [hwmon_pwm_auto_channels_temp] = "pwm%d_auto_channels_temp", |
f9f7bb3a GR |
609 | }; |
610 | ||
4413405f DDAG |
611 | static const char * const hwmon_intrusion_attr_templates[] = { |
612 | [hwmon_intrusion_alarm] = "intrusion%d_alarm", | |
613 | [hwmon_intrusion_beep] = "intrusion%d_beep", | |
614 | }; | |
615 | ||
d560168b | 616 | static const char * const *__templates[] = { |
f4d325d5 | 617 | [hwmon_chip] = hwmon_chip_attrs, |
d560168b | 618 | [hwmon_temp] = hwmon_temp_attr_templates, |
00d616cf | 619 | [hwmon_in] = hwmon_in_attr_templates, |
9b26947c | 620 | [hwmon_curr] = hwmon_curr_attr_templates, |
b308f5c7 | 621 | [hwmon_power] = hwmon_power_attr_templates, |
6bfcca44 GR |
622 | [hwmon_energy] = hwmon_energy_attr_templates, |
623 | [hwmon_humidity] = hwmon_humidity_attr_templates, | |
8faee73f | 624 | [hwmon_fan] = hwmon_fan_attr_templates, |
f9f7bb3a | 625 | [hwmon_pwm] = hwmon_pwm_attr_templates, |
4413405f | 626 | [hwmon_intrusion] = hwmon_intrusion_attr_templates, |
d560168b GR |
627 | }; |
628 | ||
629 | static const int __templates_size[] = { | |
f4d325d5 | 630 | [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs), |
d560168b | 631 | [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates), |
00d616cf | 632 | [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates), |
9b26947c | 633 | [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates), |
b308f5c7 | 634 | [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates), |
6bfcca44 GR |
635 | [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates), |
636 | [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates), | |
8faee73f | 637 | [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates), |
f9f7bb3a | 638 | [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates), |
4413405f | 639 | [hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates), |
d560168b GR |
640 | }; |
641 | ||
1597b374 GR |
642 | int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type, |
643 | u32 attr, int channel) | |
644 | { | |
7f3cc8f8 | 645 | char event[MAX_SYSFS_ATTR_NAME_LENGTH + 5]; |
1597b374 | 646 | char sattr[MAX_SYSFS_ATTR_NAME_LENGTH]; |
7f3cc8f8 | 647 | char *envp[] = { event, NULL }; |
1597b374 GR |
648 | const char * const *templates; |
649 | const char *template; | |
650 | int base; | |
651 | ||
652 | if (type >= ARRAY_SIZE(__templates)) | |
653 | return -EINVAL; | |
654 | if (attr >= __templates_size[type]) | |
655 | return -EINVAL; | |
656 | ||
657 | templates = __templates[type]; | |
658 | template = templates[attr]; | |
659 | ||
660 | base = hwmon_attr_base(type); | |
661 | ||
662 | scnprintf(sattr, MAX_SYSFS_ATTR_NAME_LENGTH, template, base + channel); | |
7f3cc8f8 | 663 | scnprintf(event, sizeof(event), "NAME=%s", sattr); |
1597b374 | 664 | sysfs_notify(&dev->kobj, NULL, sattr); |
7f3cc8f8 | 665 | kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp); |
1597b374 GR |
666 | |
667 | if (type == hwmon_temp) | |
668 | hwmon_thermal_notify(dev, channel); | |
669 | ||
670 | return 0; | |
671 | } | |
672 | EXPORT_SYMBOL_GPL(hwmon_notify_event); | |
673 | ||
d560168b GR |
674 | static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info) |
675 | { | |
676 | int i, n; | |
677 | ||
678 | for (i = n = 0; info->config[i]; i++) | |
679 | n += hweight32(info->config[i]); | |
680 | ||
681 | return n; | |
682 | } | |
683 | ||
3bf8bdcf | 684 | static int hwmon_genattrs(const void *drvdata, |
d560168b GR |
685 | struct attribute **attrs, |
686 | const struct hwmon_ops *ops, | |
687 | const struct hwmon_channel_info *info) | |
688 | { | |
689 | const char * const *templates; | |
690 | int template_size; | |
691 | int i, aindex = 0; | |
692 | ||
693 | if (info->type >= ARRAY_SIZE(__templates)) | |
694 | return -EINVAL; | |
695 | ||
696 | templates = __templates[info->type]; | |
697 | template_size = __templates_size[info->type]; | |
698 | ||
699 | for (i = 0; info->config[i]; i++) { | |
700 | u32 attr_mask = info->config[i]; | |
701 | u32 attr; | |
702 | ||
703 | while (attr_mask) { | |
704 | struct attribute *a; | |
705 | ||
706 | attr = __ffs(attr_mask); | |
707 | attr_mask &= ~BIT(attr); | |
708 | if (attr >= template_size) | |
709 | return -EINVAL; | |
3bf8bdcf | 710 | a = hwmon_genattr(drvdata, info->type, attr, i, |
d560168b GR |
711 | templates[attr], ops); |
712 | if (IS_ERR(a)) { | |
713 | if (PTR_ERR(a) != -ENOENT) | |
714 | return PTR_ERR(a); | |
715 | continue; | |
716 | } | |
717 | attrs[aindex++] = a; | |
718 | } | |
719 | } | |
720 | return aindex; | |
721 | } | |
722 | ||
723 | static struct attribute ** | |
3bf8bdcf | 724 | __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip) |
d560168b GR |
725 | { |
726 | int ret, i, aindex = 0, nattrs = 0; | |
727 | struct attribute **attrs; | |
728 | ||
729 | for (i = 0; chip->info[i]; i++) | |
730 | nattrs += hwmon_num_channel_attrs(chip->info[i]); | |
731 | ||
732 | if (nattrs == 0) | |
733 | return ERR_PTR(-EINVAL); | |
734 | ||
3bf8bdcf | 735 | attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL); |
d560168b GR |
736 | if (!attrs) |
737 | return ERR_PTR(-ENOMEM); | |
738 | ||
739 | for (i = 0; chip->info[i]; i++) { | |
3bf8bdcf | 740 | ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops, |
d560168b | 741 | chip->info[i]); |
3bf8bdcf GR |
742 | if (ret < 0) { |
743 | hwmon_free_attrs(attrs); | |
d560168b | 744 | return ERR_PTR(ret); |
3bf8bdcf | 745 | } |
d560168b GR |
746 | aindex += ret; |
747 | } | |
748 | ||
749 | return attrs; | |
750 | } | |
751 | ||
752 | static struct device * | |
753 | __hwmon_device_register(struct device *dev, const char *name, void *drvdata, | |
754 | const struct hwmon_chip_info *chip, | |
755 | const struct attribute_group **groups) | |
1236441f | 756 | { |
bab2243c | 757 | struct hwmon_device *hwdev; |
e1c9d6d6 | 758 | const char *label; |
d560168b | 759 | struct device *hdev; |
44e3ad88 | 760 | int i, err, id; |
ded2b666 | 761 | |
74d3b641 | 762 | /* Complain about invalid characters in hwmon name attribute */ |
648cd48c | 763 | if (name && (!strlen(name) || strpbrk(name, "-* \t\n"))) |
74d3b641 GR |
764 | dev_warn(dev, |
765 | "hwmon: '%s' is not a valid name attribute, please fix\n", | |
766 | name); | |
648cd48c | 767 | |
718fbfa5 | 768 | id = ida_alloc(&hwmon_ida, GFP_KERNEL); |
4ca5f468 JC |
769 | if (id < 0) |
770 | return ERR_PTR(id); | |
1236441f | 771 | |
bab2243c GR |
772 | hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL); |
773 | if (hwdev == NULL) { | |
774 | err = -ENOMEM; | |
775 | goto ida_remove; | |
776 | } | |
1236441f | 777 | |
d560168b GR |
778 | hdev = &hwdev->dev; |
779 | ||
239552f4 | 780 | if (chip) { |
d560168b | 781 | struct attribute **attrs; |
b2a4cc3a | 782 | int ngroups = 2; /* terminating NULL plus &hwdev->groups */ |
d560168b GR |
783 | |
784 | if (groups) | |
785 | for (i = 0; groups[i]; i++) | |
786 | ngroups++; | |
787 | ||
3bf8bdcf | 788 | hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL); |
38d8ed65 CIK |
789 | if (!hwdev->groups) { |
790 | err = -ENOMEM; | |
791 | goto free_hwmon; | |
792 | } | |
d560168b | 793 | |
3bf8bdcf | 794 | attrs = __hwmon_create_attrs(drvdata, chip); |
d560168b GR |
795 | if (IS_ERR(attrs)) { |
796 | err = PTR_ERR(attrs); | |
797 | goto free_hwmon; | |
798 | } | |
799 | ||
800 | hwdev->group.attrs = attrs; | |
801 | ngroups = 0; | |
802 | hwdev->groups[ngroups++] = &hwdev->group; | |
803 | ||
804 | if (groups) { | |
805 | for (i = 0; groups[i]; i++) | |
806 | hwdev->groups[ngroups++] = groups[i]; | |
807 | } | |
808 | ||
809 | hdev->groups = hwdev->groups; | |
810 | } else { | |
811 | hdev->groups = groups; | |
812 | } | |
813 | ||
07320c91 | 814 | if (dev && device_property_present(dev, "label")) { |
e1c9d6d6 PC |
815 | err = device_property_read_string(dev, "label", &label); |
816 | if (err < 0) | |
817 | goto free_hwmon; | |
818 | ||
819 | hwdev->label = kstrdup(label, GFP_KERNEL); | |
820 | if (hwdev->label == NULL) { | |
821 | err = -ENOMEM; | |
822 | goto free_hwmon; | |
823 | } | |
824 | } | |
825 | ||
bab2243c | 826 | hwdev->name = name; |
d560168b GR |
827 | hdev->class = &hwmon_class; |
828 | hdev->parent = dev; | |
829 | hdev->of_node = dev ? dev->of_node : NULL; | |
830 | hwdev->chip = chip; | |
831 | dev_set_drvdata(hdev, drvdata); | |
832 | dev_set_name(hdev, HWMON_ID_FORMAT, id); | |
833 | err = device_register(hdev); | |
ada61aa0 YY |
834 | if (err) { |
835 | put_device(hdev); | |
836 | goto ida_remove; | |
837 | } | |
d560168b | 838 | |
1597b374 GR |
839 | INIT_LIST_HEAD(&hwdev->tzdata); |
840 | ||
c41dd48e | 841 | if (dev && dev->of_node && chip && chip->ops->read && |
d560168b GR |
842 | chip->info[0]->type == hwmon_chip && |
843 | (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) { | |
44e3ad88 AM |
844 | err = hwmon_thermal_register_sensors(hdev); |
845 | if (err) { | |
846 | device_unregister(hdev); | |
847 | /* | |
848 | * Don't worry about hwdev; hwmon_dev_release(), called | |
849 | * from device_unregister(), will free it. | |
850 | */ | |
851 | goto ida_remove; | |
d560168b GR |
852 | } |
853 | } | |
bab2243c | 854 | |
d560168b | 855 | return hdev; |
bab2243c | 856 | |
d560168b | 857 | free_hwmon: |
3bf8bdcf | 858 | hwmon_dev_release(hdev); |
bab2243c | 859 | ida_remove: |
718fbfa5 | 860 | ida_free(&hwmon_ida, id); |
bab2243c GR |
861 | return ERR_PTR(err); |
862 | } | |
d560168b GR |
863 | |
864 | /** | |
865 | * hwmon_device_register_with_groups - register w/ hwmon | |
866 | * @dev: the parent device | |
867 | * @name: hwmon name attribute | |
868 | * @drvdata: driver data to attach to created device | |
869 | * @groups: List of attribute groups to create | |
870 | * | |
871 | * hwmon_device_unregister() must be called when the device is no | |
872 | * longer needed. | |
873 | * | |
874 | * Returns the pointer to the new device. | |
875 | */ | |
876 | struct device * | |
877 | hwmon_device_register_with_groups(struct device *dev, const char *name, | |
878 | void *drvdata, | |
879 | const struct attribute_group **groups) | |
880 | { | |
8353863a GR |
881 | if (!name) |
882 | return ERR_PTR(-EINVAL); | |
883 | ||
d560168b GR |
884 | return __hwmon_device_register(dev, name, drvdata, NULL, groups); |
885 | } | |
bab2243c | 886 | EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups); |
1236441f | 887 | |
d560168b GR |
888 | /** |
889 | * hwmon_device_register_with_info - register w/ hwmon | |
ddaefa20 GR |
890 | * @dev: the parent device (mandatory) |
891 | * @name: hwmon name attribute (mandatory) | |
892 | * @drvdata: driver data to attach to created device (optional) | |
893 | * @chip: pointer to hwmon chip information (mandatory) | |
848ba0a2 | 894 | * @extra_groups: pointer to list of additional non-standard attribute groups |
ddaefa20 | 895 | * (optional) |
d560168b GR |
896 | * |
897 | * hwmon_device_unregister() must be called when the device is no | |
898 | * longer needed. | |
899 | * | |
900 | * Returns the pointer to the new device. | |
901 | */ | |
902 | struct device * | |
903 | hwmon_device_register_with_info(struct device *dev, const char *name, | |
904 | void *drvdata, | |
905 | const struct hwmon_chip_info *chip, | |
848ba0a2 | 906 | const struct attribute_group **extra_groups) |
d560168b | 907 | { |
ddaefa20 | 908 | if (!dev || !name || !chip) |
d560168b GR |
909 | return ERR_PTR(-EINVAL); |
910 | ||
ddaefa20 | 911 | if (!chip->ops || !chip->ops->is_visible || !chip->info) |
59df4f4e LM |
912 | return ERR_PTR(-EINVAL); |
913 | ||
848ba0a2 | 914 | return __hwmon_device_register(dev, name, drvdata, chip, extra_groups); |
d560168b GR |
915 | } |
916 | EXPORT_SYMBOL_GPL(hwmon_device_register_with_info); | |
917 | ||
e5d21072 GR |
918 | /** |
919 | * hwmon_device_register_for_thermal - register hwmon device for thermal subsystem | |
920 | * @dev: the parent device | |
921 | * @name: hwmon name attribute | |
922 | * @drvdata: driver data to attach to created device | |
923 | * | |
924 | * The use of this function is restricted. It is provided for legacy reasons | |
925 | * and must only be called from the thermal subsystem. | |
926 | * | |
927 | * hwmon_device_unregister() must be called when the device is no | |
928 | * longer needed. | |
929 | * | |
930 | * Returns the pointer to the new device. | |
931 | */ | |
932 | struct device * | |
933 | hwmon_device_register_for_thermal(struct device *dev, const char *name, | |
934 | void *drvdata) | |
935 | { | |
936 | if (!name || !dev) | |
937 | return ERR_PTR(-EINVAL); | |
938 | ||
939 | return __hwmon_device_register(dev, name, drvdata, NULL, NULL); | |
940 | } | |
941 | EXPORT_SYMBOL_NS_GPL(hwmon_device_register_for_thermal, HWMON_THERMAL); | |
942 | ||
bab2243c GR |
943 | /** |
944 | * hwmon_device_register - register w/ hwmon | |
945 | * @dev: the device to register | |
946 | * | |
947 | * hwmon_device_unregister() must be called when the device is no | |
948 | * longer needed. | |
949 | * | |
950 | * Returns the pointer to the new device. | |
951 | */ | |
952 | struct device *hwmon_device_register(struct device *dev) | |
953 | { | |
af1bd36c GR |
954 | dev_warn(dev, |
955 | "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n"); | |
956 | ||
8353863a | 957 | return __hwmon_device_register(dev, NULL, NULL, NULL, NULL); |
1236441f | 958 | } |
839a9eef | 959 | EXPORT_SYMBOL_GPL(hwmon_device_register); |
1236441f MH |
960 | |
961 | /** | |
962 | * hwmon_device_unregister - removes the previously registered class device | |
963 | * | |
1beeffe4 | 964 | * @dev: the class device to destroy |
1236441f | 965 | */ |
1beeffe4 | 966 | void hwmon_device_unregister(struct device *dev) |
1236441f MH |
967 | { |
968 | int id; | |
969 | ||
739cf3a2 | 970 | if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) { |
1beeffe4 | 971 | device_unregister(dev); |
718fbfa5 | 972 | ida_free(&hwmon_ida, id); |
1236441f | 973 | } else |
1beeffe4 | 974 | dev_dbg(dev->parent, |
1236441f MH |
975 | "hwmon_device_unregister() failed: bad class ID!\n"); |
976 | } | |
839a9eef | 977 | EXPORT_SYMBOL_GPL(hwmon_device_unregister); |
1236441f | 978 | |
74188cba GR |
979 | static void devm_hwmon_release(struct device *dev, void *res) |
980 | { | |
981 | struct device *hwdev = *(struct device **)res; | |
982 | ||
983 | hwmon_device_unregister(hwdev); | |
984 | } | |
985 | ||
986 | /** | |
987 | * devm_hwmon_device_register_with_groups - register w/ hwmon | |
988 | * @dev: the parent device | |
989 | * @name: hwmon name attribute | |
990 | * @drvdata: driver data to attach to created device | |
991 | * @groups: List of attribute groups to create | |
992 | * | |
993 | * Returns the pointer to the new device. The new device is automatically | |
994 | * unregistered with the parent device. | |
995 | */ | |
996 | struct device * | |
997 | devm_hwmon_device_register_with_groups(struct device *dev, const char *name, | |
998 | void *drvdata, | |
999 | const struct attribute_group **groups) | |
1000 | { | |
1001 | struct device **ptr, *hwdev; | |
1002 | ||
1003 | if (!dev) | |
1004 | return ERR_PTR(-EINVAL); | |
1005 | ||
1006 | ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); | |
1007 | if (!ptr) | |
1008 | return ERR_PTR(-ENOMEM); | |
1009 | ||
1010 | hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups); | |
1011 | if (IS_ERR(hwdev)) | |
1012 | goto error; | |
1013 | ||
1014 | *ptr = hwdev; | |
1015 | devres_add(dev, ptr); | |
1016 | return hwdev; | |
1017 | ||
1018 | error: | |
1019 | devres_free(ptr); | |
1020 | return hwdev; | |
1021 | } | |
1022 | EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups); | |
1023 | ||
d560168b GR |
1024 | /** |
1025 | * devm_hwmon_device_register_with_info - register w/ hwmon | |
3870945a GR |
1026 | * @dev: the parent device |
1027 | * @name: hwmon name attribute | |
1028 | * @drvdata: driver data to attach to created device | |
1029 | * @chip: pointer to hwmon chip information | |
1030 | * @groups: pointer to list of driver specific attribute groups | |
d560168b GR |
1031 | * |
1032 | * Returns the pointer to the new device. The new device is automatically | |
1033 | * unregistered with the parent device. | |
1034 | */ | |
1035 | struct device * | |
1036 | devm_hwmon_device_register_with_info(struct device *dev, const char *name, | |
1037 | void *drvdata, | |
1038 | const struct hwmon_chip_info *chip, | |
1039 | const struct attribute_group **groups) | |
1040 | { | |
1041 | struct device **ptr, *hwdev; | |
1042 | ||
1043 | if (!dev) | |
1044 | return ERR_PTR(-EINVAL); | |
1045 | ||
1046 | ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL); | |
1047 | if (!ptr) | |
1048 | return ERR_PTR(-ENOMEM); | |
1049 | ||
1050 | hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip, | |
1051 | groups); | |
1052 | if (IS_ERR(hwdev)) | |
1053 | goto error; | |
1054 | ||
1055 | *ptr = hwdev; | |
1056 | devres_add(dev, ptr); | |
1057 | ||
1058 | return hwdev; | |
1059 | ||
1060 | error: | |
1061 | devres_free(ptr); | |
1062 | return hwdev; | |
1063 | } | |
1064 | EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info); | |
1065 | ||
74188cba GR |
1066 | static int devm_hwmon_match(struct device *dev, void *res, void *data) |
1067 | { | |
1068 | struct device **hwdev = res; | |
1069 | ||
1070 | return *hwdev == data; | |
1071 | } | |
1072 | ||
1073 | /** | |
1074 | * devm_hwmon_device_unregister - removes a previously registered hwmon device | |
1075 | * | |
1076 | * @dev: the parent device of the device to unregister | |
1077 | */ | |
1078 | void devm_hwmon_device_unregister(struct device *dev) | |
1079 | { | |
1080 | WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev)); | |
1081 | } | |
1082 | EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister); | |
1083 | ||
1ad6c3b7 MW |
1084 | static char *__hwmon_sanitize_name(struct device *dev, const char *old_name) |
1085 | { | |
1086 | char *name, *p; | |
1087 | ||
1088 | if (dev) | |
1089 | name = devm_kstrdup(dev, old_name, GFP_KERNEL); | |
1090 | else | |
1091 | name = kstrdup(old_name, GFP_KERNEL); | |
1092 | if (!name) | |
1093 | return ERR_PTR(-ENOMEM); | |
1094 | ||
1095 | for (p = name; *p; p++) | |
1096 | if (hwmon_is_bad_char(*p)) | |
1097 | *p = '_'; | |
1098 | ||
1099 | return name; | |
1100 | } | |
1101 | ||
1102 | /** | |
1103 | * hwmon_sanitize_name - Replaces invalid characters in a hwmon name | |
1104 | * @name: NUL-terminated name | |
1105 | * | |
1106 | * Allocates a new string where any invalid characters will be replaced | |
1107 | * by an underscore. It is the responsibility of the caller to release | |
1108 | * the memory. | |
1109 | * | |
1110 | * Returns newly allocated name, or ERR_PTR on error. | |
1111 | */ | |
1112 | char *hwmon_sanitize_name(const char *name) | |
1113 | { | |
1114 | return __hwmon_sanitize_name(NULL, name); | |
1115 | } | |
1116 | EXPORT_SYMBOL_GPL(hwmon_sanitize_name); | |
1117 | ||
1118 | /** | |
1119 | * devm_hwmon_sanitize_name - resource managed hwmon_sanitize_name() | |
1120 | * @dev: device to allocate memory for | |
1121 | * @name: NUL-terminated name | |
1122 | * | |
1123 | * Allocates a new string where any invalid characters will be replaced | |
1124 | * by an underscore. | |
1125 | * | |
1126 | * Returns newly allocated name, or ERR_PTR on error. | |
1127 | */ | |
1128 | char *devm_hwmon_sanitize_name(struct device *dev, const char *name) | |
1129 | { | |
1130 | if (!dev) | |
1131 | return ERR_PTR(-EINVAL); | |
1132 | ||
1133 | return __hwmon_sanitize_name(dev, name); | |
1134 | } | |
1135 | EXPORT_SYMBOL_GPL(devm_hwmon_sanitize_name); | |
1136 | ||
2958b1ec JD |
1137 | static void __init hwmon_pci_quirks(void) |
1138 | { | |
1139 | #if defined CONFIG_X86 && defined CONFIG_PCI | |
1140 | struct pci_dev *sb; | |
1141 | u16 base; | |
1142 | u8 enable; | |
1143 | ||
1144 | /* Open access to 0x295-0x296 on MSI MS-7031 */ | |
1145 | sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL); | |
d6dab7dd JD |
1146 | if (sb) { |
1147 | if (sb->subsystem_vendor == 0x1462 && /* MSI */ | |
1148 | sb->subsystem_device == 0x0031) { /* MS-7031 */ | |
1149 | pci_read_config_byte(sb, 0x48, &enable); | |
1150 | pci_read_config_word(sb, 0x64, &base); | |
1151 | ||
1152 | if (base == 0 && !(enable & BIT(2))) { | |
1153 | dev_info(&sb->dev, | |
1154 | "Opening wide generic port at 0x295\n"); | |
1155 | pci_write_config_word(sb, 0x64, 0x295); | |
1156 | pci_write_config_byte(sb, 0x48, | |
1157 | enable | BIT(2)); | |
1158 | } | |
2958b1ec | 1159 | } |
d6dab7dd | 1160 | pci_dev_put(sb); |
2958b1ec JD |
1161 | } |
1162 | #endif | |
1163 | } | |
1164 | ||
1236441f MH |
1165 | static int __init hwmon_init(void) |
1166 | { | |
bab2243c GR |
1167 | int err; |
1168 | ||
2958b1ec JD |
1169 | hwmon_pci_quirks(); |
1170 | ||
bab2243c GR |
1171 | err = class_register(&hwmon_class); |
1172 | if (err) { | |
1173 | pr_err("couldn't register hwmon sysfs class\n"); | |
1174 | return err; | |
1236441f MH |
1175 | } |
1176 | return 0; | |
1177 | } | |
1178 | ||
1179 | static void __exit hwmon_exit(void) | |
1180 | { | |
bab2243c | 1181 | class_unregister(&hwmon_class); |
1236441f MH |
1182 | } |
1183 | ||
37f54ee5 | 1184 | subsys_initcall(hwmon_init); |
1236441f MH |
1185 | module_exit(hwmon_exit); |
1186 | ||
1236441f MH |
1187 | MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>"); |
1188 | MODULE_DESCRIPTION("hardware monitoring sysfs/class support"); | |
1189 | MODULE_LICENSE("GPL"); | |
1190 |