blob: fade170977e806e69ea82d3107380b8c38fbba39 [file] [log] [blame]
Mark M. Hoffman12364412005-07-15 21:38:08 -04001/*
Guenter Roeck5ed04882012-01-19 11:02:18 -08002 * 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 */
Mark M. Hoffman12364412005-07-15 21:38:08 -040012
Joe Perchesc95df1a2010-10-20 06:51:37 +000013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Guenter Roeckd5601682015-08-26 19:38:11 -070015#include <linux/bitops.h>
Mark M. Hoffman12364412005-07-15 21:38:08 -040016#include <linux/device.h>
17#include <linux/err.h>
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080018#include <linux/gfp.h>
Guenter Roeckc9ebbe62016-07-10 09:43:21 -070019#include <linux/hwmon.h>
20#include <linux/idr.h>
21#include <linux/module.h>
Jean Delvare2958b1e2009-06-15 18:39:50 +020022#include <linux/pci.h>
Guenter Roeckc9ebbe62016-07-10 09:43:21 -070023#include <linux/slab.h>
Guenter Roeck648cd482014-02-28 10:37:55 -080024#include <linux/string.h>
Guenter Roeckd5601682015-08-26 19:38:11 -070025#include <linux/thermal.h>
Mark M. Hoffman12364412005-07-15 21:38:08 -040026
27#define HWMON_ID_PREFIX "hwmon"
28#define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
29
Guenter Roeckbab22432013-07-06 13:57:23 -070030struct hwmon_device {
31 const char *name;
32 struct device dev;
Guenter Roeckd5601682015-08-26 19:38:11 -070033 const struct hwmon_chip_info *chip;
34
35 struct attribute_group group;
36 const struct attribute_group **groups;
Guenter Roeckbab22432013-07-06 13:57:23 -070037};
Guenter Roeckd5601682015-08-26 19:38:11 -070038
Guenter Roeckbab22432013-07-06 13:57:23 -070039#define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
40
Guenter Roeckd5601682015-08-26 19:38:11 -070041struct hwmon_device_attribute {
42 struct device_attribute dev_attr;
43 const struct hwmon_ops *ops;
44 enum hwmon_sensor_types type;
45 u32 attr;
46 int index;
47};
48
49#define to_hwmon_attr(d) \
50 container_of(d, struct hwmon_device_attribute, dev_attr)
51
52/*
53 * Thermal zone information
54 * In addition to the reference to the hwmon device,
55 * also provides the sensor index.
56 */
57struct hwmon_thermal_data {
58 struct hwmon_device *hwdev; /* Reference to hwmon device */
59 int index; /* sensor index */
60};
61
Guenter Roeckbab22432013-07-06 13:57:23 -070062static ssize_t
63show_name(struct device *dev, struct device_attribute *attr, char *buf)
64{
65 return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
66}
67static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
68
69static struct attribute *hwmon_dev_attrs[] = {
70 &dev_attr_name.attr,
71 NULL
72};
73
74static umode_t hwmon_dev_name_is_visible(struct kobject *kobj,
75 struct attribute *attr, int n)
76{
77 struct device *dev = container_of(kobj, struct device, kobj);
78
79 if (to_hwmon_device(dev)->name == NULL)
80 return 0;
81
82 return attr->mode;
83}
84
85static struct attribute_group hwmon_dev_attr_group = {
86 .attrs = hwmon_dev_attrs,
87 .is_visible = hwmon_dev_name_is_visible,
88};
89
90static const struct attribute_group *hwmon_dev_attr_groups[] = {
91 &hwmon_dev_attr_group,
92 NULL
93};
94
95static void hwmon_dev_release(struct device *dev)
96{
97 kfree(to_hwmon_device(dev));
98}
99
100static struct class hwmon_class = {
101 .name = "hwmon",
102 .owner = THIS_MODULE,
103 .dev_groups = hwmon_dev_attr_groups,
104 .dev_release = hwmon_dev_release,
105};
Mark M. Hoffman12364412005-07-15 21:38:08 -0400106
Jonathan Cameron4ca5f462011-10-31 17:10:09 -0700107static DEFINE_IDA(hwmon_ida);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400108
Guenter Roeckd5601682015-08-26 19:38:11 -0700109/* Thermal zone handling */
110
111#if IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF)
112static int hwmon_thermal_get_temp(void *data, int *temp)
113{
114 struct hwmon_thermal_data *tdata = data;
115 struct hwmon_device *hwdev = tdata->hwdev;
116 int ret;
117 long t;
118
119 ret = hwdev->chip->ops->read(&hwdev->dev, hwmon_temp, hwmon_temp_input,
120 tdata->index, &t);
121 if (ret < 0)
122 return ret;
123
124 *temp = t;
125
126 return 0;
127}
128
129static struct thermal_zone_of_device_ops hwmon_thermal_ops = {
130 .get_temp = hwmon_thermal_get_temp,
131};
132
133static int hwmon_thermal_add_sensor(struct device *dev,
134 struct hwmon_device *hwdev, int index)
135{
136 struct hwmon_thermal_data *tdata;
137
138 tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
139 if (!tdata)
140 return -ENOMEM;
141
142 tdata->hwdev = hwdev;
143 tdata->index = index;
144
145 devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
146 &hwmon_thermal_ops);
147
148 return 0;
149}
150#else
151static int hwmon_thermal_add_sensor(struct device *dev,
152 struct hwmon_device *hwdev, int index)
153{
154 return 0;
155}
156#endif /* IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF) */
157
158/* sysfs attribute management */
159
160static ssize_t hwmon_attr_show(struct device *dev,
161 struct device_attribute *devattr, char *buf)
162{
163 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
164 long val;
165 int ret;
166
167 ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
168 &val);
169 if (ret < 0)
170 return ret;
171
172 return sprintf(buf, "%ld\n", val);
173}
174
175static ssize_t hwmon_attr_store(struct device *dev,
176 struct device_attribute *devattr,
177 const char *buf, size_t count)
178{
179 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
180 long val;
181 int ret;
182
183 ret = kstrtol(buf, 10, &val);
184 if (ret < 0)
185 return ret;
186
187 ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
188 val);
189 if (ret < 0)
190 return ret;
191
192 return count;
193}
194
195static int hwmon_attr_base(enum hwmon_sensor_types type)
196{
197 if (type == hwmon_in)
198 return 0;
199 return 1;
200}
201
202static struct attribute *hwmon_genattr(struct device *dev,
203 const void *drvdata,
204 enum hwmon_sensor_types type,
205 u32 attr,
206 int index,
207 const char *template,
208 const struct hwmon_ops *ops)
209{
210 struct hwmon_device_attribute *hattr;
211 struct device_attribute *dattr;
212 struct attribute *a;
213 umode_t mode;
214 char *name;
215
216 /* The attribute is invisible if there is no template string */
217 if (!template)
218 return ERR_PTR(-ENOENT);
219
220 mode = ops->is_visible(drvdata, type, attr, index);
221 if (!mode)
222 return ERR_PTR(-ENOENT);
223
224 if ((mode & S_IRUGO) && !ops->read)
225 return ERR_PTR(-EINVAL);
226 if ((mode & S_IWUGO) && !ops->write)
227 return ERR_PTR(-EINVAL);
228
229 if (type == hwmon_chip) {
230 name = (char *)template;
231 } else {
232 name = devm_kzalloc(dev, strlen(template) + 16, GFP_KERNEL);
233 if (!name)
234 return ERR_PTR(-ENOMEM);
235 scnprintf(name, strlen(template) + 16, template,
236 index + hwmon_attr_base(type));
237 }
238
239 hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL);
240 if (!hattr)
241 return ERR_PTR(-ENOMEM);
242
243 hattr->type = type;
244 hattr->attr = attr;
245 hattr->index = index;
246 hattr->ops = ops;
247
248 dattr = &hattr->dev_attr;
249 dattr->show = hwmon_attr_show;
250 dattr->store = hwmon_attr_store;
251
252 a = &dattr->attr;
253 sysfs_attr_init(a);
254 a->name = name;
255 a->mode = mode;
256
257 return a;
258}
259
260static const char * const hwmon_chip_attr_templates[] = {
261 [hwmon_chip_temp_reset_history] = "temp_reset_history",
Guenter Roeck00d616c2016-06-20 11:01:57 -0700262 [hwmon_chip_in_reset_history] = "in_reset_history",
Guenter Roeck9b269472016-06-20 11:10:33 -0700263 [hwmon_chip_curr_reset_history] = "curr_reset_history",
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700264 [hwmon_chip_power_reset_history] = "power_reset_history",
Guenter Roeckd5601682015-08-26 19:38:11 -0700265 [hwmon_chip_update_interval] = "update_interval",
266 [hwmon_chip_alarms] = "alarms",
267};
268
269static const char * const hwmon_temp_attr_templates[] = {
270 [hwmon_temp_input] = "temp%d_input",
271 [hwmon_temp_type] = "temp%d_type",
272 [hwmon_temp_lcrit] = "temp%d_lcrit",
273 [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
274 [hwmon_temp_min] = "temp%d_min",
275 [hwmon_temp_min_hyst] = "temp%d_min_hyst",
276 [hwmon_temp_max] = "temp%d_max",
277 [hwmon_temp_max_hyst] = "temp%d_max_hyst",
278 [hwmon_temp_crit] = "temp%d_crit",
279 [hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
280 [hwmon_temp_emergency] = "temp%d_emergency",
281 [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
282 [hwmon_temp_alarm] = "temp%d_alarm",
283 [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
284 [hwmon_temp_min_alarm] = "temp%d_min_alarm",
285 [hwmon_temp_max_alarm] = "temp%d_max_alarm",
286 [hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
287 [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
288 [hwmon_temp_fault] = "temp%d_fault",
289 [hwmon_temp_offset] = "temp%d_offset",
290 [hwmon_temp_label] = "temp%d_label",
291 [hwmon_temp_lowest] = "temp%d_lowest",
292 [hwmon_temp_highest] = "temp%d_highest",
293 [hwmon_temp_reset_history] = "temp%d_reset_history",
294};
295
Guenter Roeck00d616c2016-06-20 11:01:57 -0700296static const char * const hwmon_in_attr_templates[] = {
297 [hwmon_in_input] = "in%d_input",
298 [hwmon_in_min] = "in%d_min",
299 [hwmon_in_max] = "in%d_max",
300 [hwmon_in_lcrit] = "in%d_lcrit",
301 [hwmon_in_crit] = "in%d_crit",
302 [hwmon_in_average] = "in%d_average",
303 [hwmon_in_lowest] = "in%d_lowest",
304 [hwmon_in_highest] = "in%d_highest",
305 [hwmon_in_reset_history] = "in%d_reset_history",
306 [hwmon_in_label] = "in%d_label",
307 [hwmon_in_alarm] = "in%d_alarm",
308 [hwmon_in_min_alarm] = "in%d_min_alarm",
309 [hwmon_in_max_alarm] = "in%d_max_alarm",
310 [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
311 [hwmon_in_crit_alarm] = "in%d_crit_alarm",
312};
313
Guenter Roeck9b269472016-06-20 11:10:33 -0700314static const char * const hwmon_curr_attr_templates[] = {
315 [hwmon_curr_input] = "curr%d_input",
316 [hwmon_curr_min] = "curr%d_min",
317 [hwmon_curr_max] = "curr%d_max",
318 [hwmon_curr_lcrit] = "curr%d_lcrit",
319 [hwmon_curr_crit] = "curr%d_crit",
320 [hwmon_curr_average] = "curr%d_average",
321 [hwmon_curr_lowest] = "curr%d_lowest",
322 [hwmon_curr_highest] = "curr%d_highest",
323 [hwmon_curr_reset_history] = "curr%d_reset_history",
324 [hwmon_curr_label] = "curr%d_label",
325 [hwmon_curr_alarm] = "curr%d_alarm",
326 [hwmon_curr_min_alarm] = "curr%d_min_alarm",
327 [hwmon_curr_max_alarm] = "curr%d_max_alarm",
328 [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
329 [hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
330};
331
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700332static const char * const hwmon_power_attr_templates[] = {
333 [hwmon_power_average] = "power%d_average",
334 [hwmon_power_average_interval] = "power%d_average_interval",
335 [hwmon_power_average_interval_max] = "power%d_interval_max",
336 [hwmon_power_average_interval_min] = "power%d_interval_min",
337 [hwmon_power_average_highest] = "power%d_average_highest",
338 [hwmon_power_average_lowest] = "power%d_average_lowest",
339 [hwmon_power_average_max] = "power%d_average_max",
340 [hwmon_power_average_min] = "power%d_average_min",
341 [hwmon_power_input] = "power%d_input",
342 [hwmon_power_input_highest] = "power%d_input_highest",
343 [hwmon_power_input_lowest] = "power%d_input_lowest",
344 [hwmon_power_reset_history] = "power%d_reset_history",
345 [hwmon_power_accuracy] = "power%d_accuracy",
346 [hwmon_power_cap] = "power%d_cap",
347 [hwmon_power_cap_hyst] = "power%d_cap_hyst",
348 [hwmon_power_cap_max] = "power%d_cap_max",
349 [hwmon_power_cap_min] = "power%d_cap_min",
350 [hwmon_power_max] = "power%d_max",
351 [hwmon_power_crit] = "power%d_crit",
352 [hwmon_power_label] = "power%d_label",
353 [hwmon_power_alarm] = "power%d_alarm",
354 [hwmon_power_cap_alarm] = "power%d_cap_alarm",
355 [hwmon_power_max_alarm] = "power%d_max_alarm",
356 [hwmon_power_crit_alarm] = "power%d_crit_alarm",
357};
358
Guenter Roeck6bfcca42016-06-20 11:38:37 -0700359static const char * const hwmon_energy_attr_templates[] = {
360 [hwmon_energy_input] = "energy%d_input",
361 [hwmon_energy_label] = "energy%d_label",
362};
363
364static const char * const hwmon_humidity_attr_templates[] = {
365 [hwmon_humidity_input] = "humidity%d_input",
366 [hwmon_humidity_label] = "humidity%d_label",
367 [hwmon_humidity_min] = "humidity%d_min",
368 [hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
369 [hwmon_humidity_max] = "humidity%d_max",
370 [hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
371 [hwmon_humidity_alarm] = "humidity%d_alarm",
372 [hwmon_humidity_fault] = "humidity%d_fault",
373};
374
Guenter Roeck8faee732016-06-25 19:52:13 -0700375static const char * const hwmon_fan_attr_templates[] = {
376 [hwmon_fan_input] = "fan%d_input",
377 [hwmon_fan_label] = "fan%d_label",
378 [hwmon_fan_min] = "fan%d_min",
379 [hwmon_fan_max] = "fan%d_max",
380 [hwmon_fan_div] = "fan%d_div",
381 [hwmon_fan_pulses] = "fan%d_pulses",
382 [hwmon_fan_target] = "fan%d_target",
383 [hwmon_fan_alarm] = "fan%d_alarm",
384 [hwmon_fan_min_alarm] = "fan%d_min_alarm",
385 [hwmon_fan_max_alarm] = "fan%d_max_alarm",
386 [hwmon_fan_fault] = "fan%d_fault",
387};
388
Guenter Roeckf9f7bb32016-06-26 12:20:46 -0700389static const char * const hwmon_pwm_attr_templates[] = {
390 [hwmon_pwm_input] = "pwm%d",
391 [hwmon_pwm_enable] = "pwm%d_enable",
392 [hwmon_pwm_mode] = "pwm%d_mode",
393 [hwmon_pwm_freq] = "pwm%d_freq",
394};
395
Guenter Roeckd5601682015-08-26 19:38:11 -0700396static const char * const *__templates[] = {
397 [hwmon_chip] = hwmon_chip_attr_templates,
398 [hwmon_temp] = hwmon_temp_attr_templates,
Guenter Roeck00d616c2016-06-20 11:01:57 -0700399 [hwmon_in] = hwmon_in_attr_templates,
Guenter Roeck9b269472016-06-20 11:10:33 -0700400 [hwmon_curr] = hwmon_curr_attr_templates,
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700401 [hwmon_power] = hwmon_power_attr_templates,
Guenter Roeck6bfcca42016-06-20 11:38:37 -0700402 [hwmon_energy] = hwmon_energy_attr_templates,
403 [hwmon_humidity] = hwmon_humidity_attr_templates,
Guenter Roeck8faee732016-06-25 19:52:13 -0700404 [hwmon_fan] = hwmon_fan_attr_templates,
Guenter Roeckf9f7bb32016-06-26 12:20:46 -0700405 [hwmon_pwm] = hwmon_pwm_attr_templates,
Guenter Roeckd5601682015-08-26 19:38:11 -0700406};
407
408static const int __templates_size[] = {
409 [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attr_templates),
410 [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
Guenter Roeck00d616c2016-06-20 11:01:57 -0700411 [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
Guenter Roeck9b269472016-06-20 11:10:33 -0700412 [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700413 [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
Guenter Roeck6bfcca42016-06-20 11:38:37 -0700414 [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
415 [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
Guenter Roeck8faee732016-06-25 19:52:13 -0700416 [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
Guenter Roeckf9f7bb32016-06-26 12:20:46 -0700417 [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
Guenter Roeckd5601682015-08-26 19:38:11 -0700418};
419
420static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
421{
422 int i, n;
423
424 for (i = n = 0; info->config[i]; i++)
425 n += hweight32(info->config[i]);
426
427 return n;
428}
429
430static int hwmon_genattrs(struct device *dev,
431 const void *drvdata,
432 struct attribute **attrs,
433 const struct hwmon_ops *ops,
434 const struct hwmon_channel_info *info)
435{
436 const char * const *templates;
437 int template_size;
438 int i, aindex = 0;
439
440 if (info->type >= ARRAY_SIZE(__templates))
441 return -EINVAL;
442
443 templates = __templates[info->type];
444 template_size = __templates_size[info->type];
445
446 for (i = 0; info->config[i]; i++) {
447 u32 attr_mask = info->config[i];
448 u32 attr;
449
450 while (attr_mask) {
451 struct attribute *a;
452
453 attr = __ffs(attr_mask);
454 attr_mask &= ~BIT(attr);
455 if (attr >= template_size)
456 return -EINVAL;
457 a = hwmon_genattr(dev, drvdata, info->type, attr, i,
458 templates[attr], ops);
459 if (IS_ERR(a)) {
460 if (PTR_ERR(a) != -ENOENT)
461 return PTR_ERR(a);
462 continue;
463 }
464 attrs[aindex++] = a;
465 }
466 }
467 return aindex;
468}
469
470static struct attribute **
471__hwmon_create_attrs(struct device *dev, const void *drvdata,
472 const struct hwmon_chip_info *chip)
473{
474 int ret, i, aindex = 0, nattrs = 0;
475 struct attribute **attrs;
476
477 for (i = 0; chip->info[i]; i++)
478 nattrs += hwmon_num_channel_attrs(chip->info[i]);
479
480 if (nattrs == 0)
481 return ERR_PTR(-EINVAL);
482
483 attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL);
484 if (!attrs)
485 return ERR_PTR(-ENOMEM);
486
487 for (i = 0; chip->info[i]; i++) {
488 ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops,
489 chip->info[i]);
490 if (ret < 0)
491 return ERR_PTR(ret);
492 aindex += ret;
493 }
494
495 return attrs;
496}
497
498static struct device *
499__hwmon_device_register(struct device *dev, const char *name, void *drvdata,
500 const struct hwmon_chip_info *chip,
501 const struct attribute_group **groups)
502{
503 struct hwmon_device *hwdev;
504 struct device *hdev;
505 int i, j, err, id;
506
507 /* Do not accept invalid characters in hwmon name attribute */
508 if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
509 return ERR_PTR(-EINVAL);
510
511 id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
512 if (id < 0)
513 return ERR_PTR(id);
514
515 hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
516 if (hwdev == NULL) {
517 err = -ENOMEM;
518 goto ida_remove;
519 }
520
521 hdev = &hwdev->dev;
522
523 if (chip && chip->ops->is_visible) {
524 struct attribute **attrs;
525 int ngroups = 2;
526
527 if (groups)
528 for (i = 0; groups[i]; i++)
529 ngroups++;
530
531 hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups),
532 GFP_KERNEL);
533 if (!hwdev->groups)
534 return ERR_PTR(-ENOMEM);
535
536 attrs = __hwmon_create_attrs(dev, drvdata, chip);
537 if (IS_ERR(attrs)) {
538 err = PTR_ERR(attrs);
539 goto free_hwmon;
540 }
541
542 hwdev->group.attrs = attrs;
543 ngroups = 0;
544 hwdev->groups[ngroups++] = &hwdev->group;
545
546 if (groups) {
547 for (i = 0; groups[i]; i++)
548 hwdev->groups[ngroups++] = groups[i];
549 }
550
551 hdev->groups = hwdev->groups;
552 } else {
553 hdev->groups = groups;
554 }
555
556 hwdev->name = name;
557 hdev->class = &hwmon_class;
558 hdev->parent = dev;
559 hdev->of_node = dev ? dev->of_node : NULL;
560 hwdev->chip = chip;
561 dev_set_drvdata(hdev, drvdata);
562 dev_set_name(hdev, HWMON_ID_FORMAT, id);
563 err = device_register(hdev);
564 if (err)
565 goto free_hwmon;
566
567 if (chip && chip->ops->is_visible && chip->ops->read &&
568 chip->info[0]->type == hwmon_chip &&
569 (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
570 const struct hwmon_channel_info **info = chip->info;
571
572 for (i = 1; info[i]; i++) {
573 if (info[i]->type != hwmon_temp)
574 continue;
575
576 for (j = 0; info[i]->config[j]; j++) {
577 if (!chip->ops->is_visible(drvdata, hwmon_temp,
578 hwmon_temp_input, j))
579 continue;
580 if (info[i]->config[j] & HWMON_T_INPUT)
581 hwmon_thermal_add_sensor(dev, hwdev, j);
582 }
583 }
584 }
585
586 return hdev;
587
588free_hwmon:
589 kfree(hwdev);
590ida_remove:
591 ida_simple_remove(&hwmon_ida, id);
592 return ERR_PTR(err);
593}
594
Mark M. Hoffman12364412005-07-15 21:38:08 -0400595/**
Guenter Roeckbab22432013-07-06 13:57:23 -0700596 * hwmon_device_register_with_groups - register w/ hwmon
597 * @dev: the parent device
598 * @name: hwmon name attribute
599 * @drvdata: driver data to attach to created device
600 * @groups: List of attribute groups to create
601 *
602 * hwmon_device_unregister() must be called when the device is no
603 * longer needed.
604 *
605 * Returns the pointer to the new device.
606 */
607struct device *
608hwmon_device_register_with_groups(struct device *dev, const char *name,
609 void *drvdata,
610 const struct attribute_group **groups)
611{
Guenter Roeckd5601682015-08-26 19:38:11 -0700612 return __hwmon_device_register(dev, name, drvdata, NULL, groups);
Guenter Roeckbab22432013-07-06 13:57:23 -0700613}
614EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
615
616/**
Guenter Roeckd5601682015-08-26 19:38:11 -0700617 * hwmon_device_register_with_info - register w/ hwmon
618 * @dev: the parent device
619 * @name: hwmon name attribute
620 * @drvdata: driver data to attach to created device
621 * @info: Pointer to hwmon chip information
622 * @groups - pointer to list of driver specific attribute groups
623 *
624 * hwmon_device_unregister() must be called when the device is no
625 * longer needed.
626 *
627 * Returns the pointer to the new device.
628 */
629struct device *
630hwmon_device_register_with_info(struct device *dev, const char *name,
631 void *drvdata,
632 const struct hwmon_chip_info *chip,
633 const struct attribute_group **groups)
634{
635 if (chip && (!chip->ops || !chip->info))
636 return ERR_PTR(-EINVAL);
637
638 return __hwmon_device_register(dev, name, drvdata, chip, groups);
639}
640EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
641
642/**
Tony Jones1beeffe2007-08-20 13:46:20 -0700643 * hwmon_device_register - register w/ hwmon
Mark M. Hoffman12364412005-07-15 21:38:08 -0400644 * @dev: the device to register
645 *
Tony Jones1beeffe2007-08-20 13:46:20 -0700646 * hwmon_device_unregister() must be called when the device is no
Mark M. Hoffman12364412005-07-15 21:38:08 -0400647 * longer needed.
648 *
Tony Jones1beeffe2007-08-20 13:46:20 -0700649 * Returns the pointer to the new device.
Mark M. Hoffman12364412005-07-15 21:38:08 -0400650 */
Tony Jones1beeffe2007-08-20 13:46:20 -0700651struct device *hwmon_device_register(struct device *dev)
Mark M. Hoffman12364412005-07-15 21:38:08 -0400652{
Guenter Roeckbab22432013-07-06 13:57:23 -0700653 return hwmon_device_register_with_groups(dev, NULL, NULL, NULL);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400654}
Frans Meulenbroeks839a9ee2012-01-08 19:34:14 +0100655EXPORT_SYMBOL_GPL(hwmon_device_register);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400656
657/**
658 * hwmon_device_unregister - removes the previously registered class device
659 *
Tony Jones1beeffe2007-08-20 13:46:20 -0700660 * @dev: the class device to destroy
Mark M. Hoffman12364412005-07-15 21:38:08 -0400661 */
Tony Jones1beeffe2007-08-20 13:46:20 -0700662void hwmon_device_unregister(struct device *dev)
Mark M. Hoffman12364412005-07-15 21:38:08 -0400663{
664 int id;
665
Kay Sievers739cf3a2009-01-06 10:44:41 -0800666 if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
Tony Jones1beeffe2007-08-20 13:46:20 -0700667 device_unregister(dev);
Jonathan Cameron4ca5f462011-10-31 17:10:09 -0700668 ida_simple_remove(&hwmon_ida, id);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400669 } else
Tony Jones1beeffe2007-08-20 13:46:20 -0700670 dev_dbg(dev->parent,
Mark M. Hoffman12364412005-07-15 21:38:08 -0400671 "hwmon_device_unregister() failed: bad class ID!\n");
672}
Frans Meulenbroeks839a9ee2012-01-08 19:34:14 +0100673EXPORT_SYMBOL_GPL(hwmon_device_unregister);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400674
Guenter Roeck74188cb2013-07-11 20:00:12 -0700675static void devm_hwmon_release(struct device *dev, void *res)
676{
677 struct device *hwdev = *(struct device **)res;
678
679 hwmon_device_unregister(hwdev);
680}
681
682/**
683 * devm_hwmon_device_register_with_groups - register w/ hwmon
684 * @dev: the parent device
685 * @name: hwmon name attribute
686 * @drvdata: driver data to attach to created device
687 * @groups: List of attribute groups to create
688 *
689 * Returns the pointer to the new device. The new device is automatically
690 * unregistered with the parent device.
691 */
692struct device *
693devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
694 void *drvdata,
695 const struct attribute_group **groups)
696{
697 struct device **ptr, *hwdev;
698
699 if (!dev)
700 return ERR_PTR(-EINVAL);
701
702 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
703 if (!ptr)
704 return ERR_PTR(-ENOMEM);
705
706 hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
707 if (IS_ERR(hwdev))
708 goto error;
709
710 *ptr = hwdev;
711 devres_add(dev, ptr);
712 return hwdev;
713
714error:
715 devres_free(ptr);
716 return hwdev;
717}
718EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
719
Guenter Roeckd5601682015-08-26 19:38:11 -0700720/**
721 * devm_hwmon_device_register_with_info - register w/ hwmon
722 * @dev: the parent device
723 * @name: hwmon name attribute
724 * @drvdata: driver data to attach to created device
725 * @info: Pointer to hwmon chip information
726 * @groups - pointer to list of driver specific attribute groups
727 *
728 * Returns the pointer to the new device. The new device is automatically
729 * unregistered with the parent device.
730 */
731struct device *
732devm_hwmon_device_register_with_info(struct device *dev, const char *name,
733 void *drvdata,
734 const struct hwmon_chip_info *chip,
735 const struct attribute_group **groups)
736{
737 struct device **ptr, *hwdev;
738
739 if (!dev)
740 return ERR_PTR(-EINVAL);
741
742 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
743 if (!ptr)
744 return ERR_PTR(-ENOMEM);
745
746 hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
747 groups);
748 if (IS_ERR(hwdev))
749 goto error;
750
751 *ptr = hwdev;
752 devres_add(dev, ptr);
753
754 return hwdev;
755
756error:
757 devres_free(ptr);
758 return hwdev;
759}
760EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
761
Guenter Roeck74188cb2013-07-11 20:00:12 -0700762static int devm_hwmon_match(struct device *dev, void *res, void *data)
763{
764 struct device **hwdev = res;
765
766 return *hwdev == data;
767}
768
769/**
770 * devm_hwmon_device_unregister - removes a previously registered hwmon device
771 *
772 * @dev: the parent device of the device to unregister
773 */
774void devm_hwmon_device_unregister(struct device *dev)
775{
776 WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
777}
778EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
779
Jean Delvare2958b1e2009-06-15 18:39:50 +0200780static void __init hwmon_pci_quirks(void)
781{
782#if defined CONFIG_X86 && defined CONFIG_PCI
783 struct pci_dev *sb;
784 u16 base;
785 u8 enable;
786
787 /* Open access to 0x295-0x296 on MSI MS-7031 */
788 sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
Jean Delvared6dab7d2012-12-19 22:16:59 +0100789 if (sb) {
790 if (sb->subsystem_vendor == 0x1462 && /* MSI */
791 sb->subsystem_device == 0x0031) { /* MS-7031 */
792 pci_read_config_byte(sb, 0x48, &enable);
793 pci_read_config_word(sb, 0x64, &base);
Jean Delvare2958b1e2009-06-15 18:39:50 +0200794
Jean Delvared6dab7d2012-12-19 22:16:59 +0100795 if (base == 0 && !(enable & BIT(2))) {
796 dev_info(&sb->dev,
797 "Opening wide generic port at 0x295\n");
798 pci_write_config_word(sb, 0x64, 0x295);
799 pci_write_config_byte(sb, 0x48,
800 enable | BIT(2));
801 }
Jean Delvare2958b1e2009-06-15 18:39:50 +0200802 }
Jean Delvared6dab7d2012-12-19 22:16:59 +0100803 pci_dev_put(sb);
Jean Delvare2958b1e2009-06-15 18:39:50 +0200804 }
805#endif
806}
807
Mark M. Hoffman12364412005-07-15 21:38:08 -0400808static int __init hwmon_init(void)
809{
Guenter Roeckbab22432013-07-06 13:57:23 -0700810 int err;
811
Jean Delvare2958b1e2009-06-15 18:39:50 +0200812 hwmon_pci_quirks();
813
Guenter Roeckbab22432013-07-06 13:57:23 -0700814 err = class_register(&hwmon_class);
815 if (err) {
816 pr_err("couldn't register hwmon sysfs class\n");
817 return err;
Mark M. Hoffman12364412005-07-15 21:38:08 -0400818 }
819 return 0;
820}
821
822static void __exit hwmon_exit(void)
823{
Guenter Roeckbab22432013-07-06 13:57:23 -0700824 class_unregister(&hwmon_class);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400825}
826
David Brownell37f54ee2007-02-14 21:15:04 +0100827subsys_initcall(hwmon_init);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400828module_exit(hwmon_exit);
829
Mark M. Hoffman12364412005-07-15 21:38:08 -0400830MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
831MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
832MODULE_LICENSE("GPL");
833