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