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