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