blob: a74c075a30ec49608308ddd45715f1addc104c84 [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
Guenter Roeck86430c12016-08-12 06:28:15 -0700111/*
112 * The complex conditional is necessary to avoid a cyclic dependency
113 * between hwmon and thermal_sys modules.
114 */
115#if IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF) && \
116 (!defined(CONFIG_THERMAL_HWMON) || \
117 !(defined(MODULE) && IS_MODULE(CONFIG_THERMAL)))
Guenter Roeckd5601682015-08-26 19:38:11 -0700118static int hwmon_thermal_get_temp(void *data, int *temp)
119{
120 struct hwmon_thermal_data *tdata = data;
121 struct hwmon_device *hwdev = tdata->hwdev;
122 int ret;
123 long t;
124
125 ret = hwdev->chip->ops->read(&hwdev->dev, hwmon_temp, hwmon_temp_input,
126 tdata->index, &t);
127 if (ret < 0)
128 return ret;
129
130 *temp = t;
131
132 return 0;
133}
134
135static struct thermal_zone_of_device_ops hwmon_thermal_ops = {
136 .get_temp = hwmon_thermal_get_temp,
137};
138
139static int hwmon_thermal_add_sensor(struct device *dev,
140 struct hwmon_device *hwdev, int index)
141{
142 struct hwmon_thermal_data *tdata;
143
144 tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
145 if (!tdata)
146 return -ENOMEM;
147
148 tdata->hwdev = hwdev;
149 tdata->index = index;
150
151 devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
152 &hwmon_thermal_ops);
153
154 return 0;
155}
156#else
157static int hwmon_thermal_add_sensor(struct device *dev,
158 struct hwmon_device *hwdev, int index)
159{
160 return 0;
161}
Guenter Roeck86430c12016-08-12 06:28:15 -0700162#endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
Guenter Roeckd5601682015-08-26 19:38:11 -0700163
164/* sysfs attribute management */
165
166static ssize_t hwmon_attr_show(struct device *dev,
167 struct device_attribute *devattr, char *buf)
168{
169 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
170 long val;
171 int ret;
172
173 ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
174 &val);
175 if (ret < 0)
176 return ret;
177
178 return sprintf(buf, "%ld\n", val);
179}
180
181static ssize_t hwmon_attr_store(struct device *dev,
182 struct device_attribute *devattr,
183 const char *buf, size_t count)
184{
185 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
186 long val;
187 int ret;
188
189 ret = kstrtol(buf, 10, &val);
190 if (ret < 0)
191 return ret;
192
193 ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
194 val);
195 if (ret < 0)
196 return ret;
197
198 return count;
199}
200
201static int hwmon_attr_base(enum hwmon_sensor_types type)
202{
203 if (type == hwmon_in)
204 return 0;
205 return 1;
206}
207
208static struct attribute *hwmon_genattr(struct device *dev,
209 const void *drvdata,
210 enum hwmon_sensor_types type,
211 u32 attr,
212 int index,
213 const char *template,
214 const struct hwmon_ops *ops)
215{
216 struct hwmon_device_attribute *hattr;
217 struct device_attribute *dattr;
218 struct attribute *a;
219 umode_t mode;
220 char *name;
221
222 /* The attribute is invisible if there is no template string */
223 if (!template)
224 return ERR_PTR(-ENOENT);
225
226 mode = ops->is_visible(drvdata, type, attr, index);
227 if (!mode)
228 return ERR_PTR(-ENOENT);
229
230 if ((mode & S_IRUGO) && !ops->read)
231 return ERR_PTR(-EINVAL);
232 if ((mode & S_IWUGO) && !ops->write)
233 return ERR_PTR(-EINVAL);
234
235 if (type == hwmon_chip) {
236 name = (char *)template;
237 } else {
238 name = devm_kzalloc(dev, strlen(template) + 16, GFP_KERNEL);
239 if (!name)
240 return ERR_PTR(-ENOMEM);
241 scnprintf(name, strlen(template) + 16, template,
242 index + hwmon_attr_base(type));
243 }
244
245 hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL);
246 if (!hattr)
247 return ERR_PTR(-ENOMEM);
248
249 hattr->type = type;
250 hattr->attr = attr;
251 hattr->index = index;
252 hattr->ops = ops;
253
254 dattr = &hattr->dev_attr;
255 dattr->show = hwmon_attr_show;
256 dattr->store = hwmon_attr_store;
257
258 a = &dattr->attr;
259 sysfs_attr_init(a);
260 a->name = name;
261 a->mode = mode;
262
263 return a;
264}
265
266static const char * const hwmon_chip_attr_templates[] = {
267 [hwmon_chip_temp_reset_history] = "temp_reset_history",
Guenter Roeck00d616c2016-06-20 11:01:57 -0700268 [hwmon_chip_in_reset_history] = "in_reset_history",
Guenter Roeck9b269472016-06-20 11:10:33 -0700269 [hwmon_chip_curr_reset_history] = "curr_reset_history",
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700270 [hwmon_chip_power_reset_history] = "power_reset_history",
Guenter Roeckd5601682015-08-26 19:38:11 -0700271 [hwmon_chip_update_interval] = "update_interval",
272 [hwmon_chip_alarms] = "alarms",
273};
274
275static const char * const hwmon_temp_attr_templates[] = {
276 [hwmon_temp_input] = "temp%d_input",
277 [hwmon_temp_type] = "temp%d_type",
278 [hwmon_temp_lcrit] = "temp%d_lcrit",
279 [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
280 [hwmon_temp_min] = "temp%d_min",
281 [hwmon_temp_min_hyst] = "temp%d_min_hyst",
282 [hwmon_temp_max] = "temp%d_max",
283 [hwmon_temp_max_hyst] = "temp%d_max_hyst",
284 [hwmon_temp_crit] = "temp%d_crit",
285 [hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
286 [hwmon_temp_emergency] = "temp%d_emergency",
287 [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
288 [hwmon_temp_alarm] = "temp%d_alarm",
289 [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
290 [hwmon_temp_min_alarm] = "temp%d_min_alarm",
291 [hwmon_temp_max_alarm] = "temp%d_max_alarm",
292 [hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
293 [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
294 [hwmon_temp_fault] = "temp%d_fault",
295 [hwmon_temp_offset] = "temp%d_offset",
296 [hwmon_temp_label] = "temp%d_label",
297 [hwmon_temp_lowest] = "temp%d_lowest",
298 [hwmon_temp_highest] = "temp%d_highest",
299 [hwmon_temp_reset_history] = "temp%d_reset_history",
300};
301
Guenter Roeck00d616c2016-06-20 11:01:57 -0700302static const char * const hwmon_in_attr_templates[] = {
303 [hwmon_in_input] = "in%d_input",
304 [hwmon_in_min] = "in%d_min",
305 [hwmon_in_max] = "in%d_max",
306 [hwmon_in_lcrit] = "in%d_lcrit",
307 [hwmon_in_crit] = "in%d_crit",
308 [hwmon_in_average] = "in%d_average",
309 [hwmon_in_lowest] = "in%d_lowest",
310 [hwmon_in_highest] = "in%d_highest",
311 [hwmon_in_reset_history] = "in%d_reset_history",
312 [hwmon_in_label] = "in%d_label",
313 [hwmon_in_alarm] = "in%d_alarm",
314 [hwmon_in_min_alarm] = "in%d_min_alarm",
315 [hwmon_in_max_alarm] = "in%d_max_alarm",
316 [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
317 [hwmon_in_crit_alarm] = "in%d_crit_alarm",
318};
319
Guenter Roeck9b269472016-06-20 11:10:33 -0700320static const char * const hwmon_curr_attr_templates[] = {
321 [hwmon_curr_input] = "curr%d_input",
322 [hwmon_curr_min] = "curr%d_min",
323 [hwmon_curr_max] = "curr%d_max",
324 [hwmon_curr_lcrit] = "curr%d_lcrit",
325 [hwmon_curr_crit] = "curr%d_crit",
326 [hwmon_curr_average] = "curr%d_average",
327 [hwmon_curr_lowest] = "curr%d_lowest",
328 [hwmon_curr_highest] = "curr%d_highest",
329 [hwmon_curr_reset_history] = "curr%d_reset_history",
330 [hwmon_curr_label] = "curr%d_label",
331 [hwmon_curr_alarm] = "curr%d_alarm",
332 [hwmon_curr_min_alarm] = "curr%d_min_alarm",
333 [hwmon_curr_max_alarm] = "curr%d_max_alarm",
334 [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
335 [hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
336};
337
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700338static const char * const hwmon_power_attr_templates[] = {
339 [hwmon_power_average] = "power%d_average",
340 [hwmon_power_average_interval] = "power%d_average_interval",
341 [hwmon_power_average_interval_max] = "power%d_interval_max",
342 [hwmon_power_average_interval_min] = "power%d_interval_min",
343 [hwmon_power_average_highest] = "power%d_average_highest",
344 [hwmon_power_average_lowest] = "power%d_average_lowest",
345 [hwmon_power_average_max] = "power%d_average_max",
346 [hwmon_power_average_min] = "power%d_average_min",
347 [hwmon_power_input] = "power%d_input",
348 [hwmon_power_input_highest] = "power%d_input_highest",
349 [hwmon_power_input_lowest] = "power%d_input_lowest",
350 [hwmon_power_reset_history] = "power%d_reset_history",
351 [hwmon_power_accuracy] = "power%d_accuracy",
352 [hwmon_power_cap] = "power%d_cap",
353 [hwmon_power_cap_hyst] = "power%d_cap_hyst",
354 [hwmon_power_cap_max] = "power%d_cap_max",
355 [hwmon_power_cap_min] = "power%d_cap_min",
356 [hwmon_power_max] = "power%d_max",
357 [hwmon_power_crit] = "power%d_crit",
358 [hwmon_power_label] = "power%d_label",
359 [hwmon_power_alarm] = "power%d_alarm",
360 [hwmon_power_cap_alarm] = "power%d_cap_alarm",
361 [hwmon_power_max_alarm] = "power%d_max_alarm",
362 [hwmon_power_crit_alarm] = "power%d_crit_alarm",
363};
364
Guenter Roeck6bfcca42016-06-20 11:38:37 -0700365static const char * const hwmon_energy_attr_templates[] = {
366 [hwmon_energy_input] = "energy%d_input",
367 [hwmon_energy_label] = "energy%d_label",
368};
369
370static const char * const hwmon_humidity_attr_templates[] = {
371 [hwmon_humidity_input] = "humidity%d_input",
372 [hwmon_humidity_label] = "humidity%d_label",
373 [hwmon_humidity_min] = "humidity%d_min",
374 [hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
375 [hwmon_humidity_max] = "humidity%d_max",
376 [hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
377 [hwmon_humidity_alarm] = "humidity%d_alarm",
378 [hwmon_humidity_fault] = "humidity%d_fault",
379};
380
Guenter Roeck8faee732016-06-25 19:52:13 -0700381static const char * const hwmon_fan_attr_templates[] = {
382 [hwmon_fan_input] = "fan%d_input",
383 [hwmon_fan_label] = "fan%d_label",
384 [hwmon_fan_min] = "fan%d_min",
385 [hwmon_fan_max] = "fan%d_max",
386 [hwmon_fan_div] = "fan%d_div",
387 [hwmon_fan_pulses] = "fan%d_pulses",
388 [hwmon_fan_target] = "fan%d_target",
389 [hwmon_fan_alarm] = "fan%d_alarm",
390 [hwmon_fan_min_alarm] = "fan%d_min_alarm",
391 [hwmon_fan_max_alarm] = "fan%d_max_alarm",
392 [hwmon_fan_fault] = "fan%d_fault",
393};
394
Guenter Roeckf9f7bb32016-06-26 12:20:46 -0700395static const char * const hwmon_pwm_attr_templates[] = {
396 [hwmon_pwm_input] = "pwm%d",
397 [hwmon_pwm_enable] = "pwm%d_enable",
398 [hwmon_pwm_mode] = "pwm%d_mode",
399 [hwmon_pwm_freq] = "pwm%d_freq",
400};
401
Guenter Roeckd5601682015-08-26 19:38:11 -0700402static const char * const *__templates[] = {
403 [hwmon_chip] = hwmon_chip_attr_templates,
404 [hwmon_temp] = hwmon_temp_attr_templates,
Guenter Roeck00d616c2016-06-20 11:01:57 -0700405 [hwmon_in] = hwmon_in_attr_templates,
Guenter Roeck9b269472016-06-20 11:10:33 -0700406 [hwmon_curr] = hwmon_curr_attr_templates,
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700407 [hwmon_power] = hwmon_power_attr_templates,
Guenter Roeck6bfcca42016-06-20 11:38:37 -0700408 [hwmon_energy] = hwmon_energy_attr_templates,
409 [hwmon_humidity] = hwmon_humidity_attr_templates,
Guenter Roeck8faee732016-06-25 19:52:13 -0700410 [hwmon_fan] = hwmon_fan_attr_templates,
Guenter Roeckf9f7bb32016-06-26 12:20:46 -0700411 [hwmon_pwm] = hwmon_pwm_attr_templates,
Guenter Roeckd5601682015-08-26 19:38:11 -0700412};
413
414static const int __templates_size[] = {
415 [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attr_templates),
416 [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
Guenter Roeck00d616c2016-06-20 11:01:57 -0700417 [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
Guenter Roeck9b269472016-06-20 11:10:33 -0700418 [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700419 [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
Guenter Roeck6bfcca42016-06-20 11:38:37 -0700420 [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
421 [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
Guenter Roeck8faee732016-06-25 19:52:13 -0700422 [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
Guenter Roeckf9f7bb32016-06-26 12:20:46 -0700423 [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
Guenter Roeckd5601682015-08-26 19:38:11 -0700424};
425
426static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
427{
428 int i, n;
429
430 for (i = n = 0; info->config[i]; i++)
431 n += hweight32(info->config[i]);
432
433 return n;
434}
435
436static int hwmon_genattrs(struct device *dev,
437 const void *drvdata,
438 struct attribute **attrs,
439 const struct hwmon_ops *ops,
440 const struct hwmon_channel_info *info)
441{
442 const char * const *templates;
443 int template_size;
444 int i, aindex = 0;
445
446 if (info->type >= ARRAY_SIZE(__templates))
447 return -EINVAL;
448
449 templates = __templates[info->type];
450 template_size = __templates_size[info->type];
451
452 for (i = 0; info->config[i]; i++) {
453 u32 attr_mask = info->config[i];
454 u32 attr;
455
456 while (attr_mask) {
457 struct attribute *a;
458
459 attr = __ffs(attr_mask);
460 attr_mask &= ~BIT(attr);
461 if (attr >= template_size)
462 return -EINVAL;
463 a = hwmon_genattr(dev, drvdata, info->type, attr, i,
464 templates[attr], ops);
465 if (IS_ERR(a)) {
466 if (PTR_ERR(a) != -ENOENT)
467 return PTR_ERR(a);
468 continue;
469 }
470 attrs[aindex++] = a;
471 }
472 }
473 return aindex;
474}
475
476static struct attribute **
477__hwmon_create_attrs(struct device *dev, const void *drvdata,
478 const struct hwmon_chip_info *chip)
479{
480 int ret, i, aindex = 0, nattrs = 0;
481 struct attribute **attrs;
482
483 for (i = 0; chip->info[i]; i++)
484 nattrs += hwmon_num_channel_attrs(chip->info[i]);
485
486 if (nattrs == 0)
487 return ERR_PTR(-EINVAL);
488
489 attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL);
490 if (!attrs)
491 return ERR_PTR(-ENOMEM);
492
493 for (i = 0; chip->info[i]; i++) {
494 ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops,
495 chip->info[i]);
496 if (ret < 0)
497 return ERR_PTR(ret);
498 aindex += ret;
499 }
500
501 return attrs;
502}
503
504static struct device *
505__hwmon_device_register(struct device *dev, const char *name, void *drvdata,
506 const struct hwmon_chip_info *chip,
507 const struct attribute_group **groups)
508{
509 struct hwmon_device *hwdev;
510 struct device *hdev;
511 int i, j, err, id;
512
513 /* Do not accept invalid characters in hwmon name attribute */
514 if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
515 return ERR_PTR(-EINVAL);
516
517 id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
518 if (id < 0)
519 return ERR_PTR(id);
520
521 hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
522 if (hwdev == NULL) {
523 err = -ENOMEM;
524 goto ida_remove;
525 }
526
527 hdev = &hwdev->dev;
528
529 if (chip && chip->ops->is_visible) {
530 struct attribute **attrs;
531 int ngroups = 2;
532
533 if (groups)
534 for (i = 0; groups[i]; i++)
535 ngroups++;
536
537 hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups),
538 GFP_KERNEL);
Colin Ian King38d8ed62016-10-23 21:56:08 +0100539 if (!hwdev->groups) {
540 err = -ENOMEM;
541 goto free_hwmon;
542 }
Guenter Roeckd5601682015-08-26 19:38:11 -0700543
544 attrs = __hwmon_create_attrs(dev, drvdata, chip);
545 if (IS_ERR(attrs)) {
546 err = PTR_ERR(attrs);
547 goto free_hwmon;
548 }
549
550 hwdev->group.attrs = attrs;
551 ngroups = 0;
552 hwdev->groups[ngroups++] = &hwdev->group;
553
554 if (groups) {
555 for (i = 0; groups[i]; i++)
556 hwdev->groups[ngroups++] = groups[i];
557 }
558
559 hdev->groups = hwdev->groups;
560 } else {
561 hdev->groups = groups;
562 }
563
564 hwdev->name = name;
565 hdev->class = &hwmon_class;
566 hdev->parent = dev;
567 hdev->of_node = dev ? dev->of_node : NULL;
568 hwdev->chip = chip;
569 dev_set_drvdata(hdev, drvdata);
570 dev_set_name(hdev, HWMON_ID_FORMAT, id);
571 err = device_register(hdev);
572 if (err)
573 goto free_hwmon;
574
575 if (chip && chip->ops->is_visible && chip->ops->read &&
576 chip->info[0]->type == hwmon_chip &&
577 (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
578 const struct hwmon_channel_info **info = chip->info;
579
580 for (i = 1; info[i]; i++) {
581 if (info[i]->type != hwmon_temp)
582 continue;
583
584 for (j = 0; info[i]->config[j]; j++) {
585 if (!chip->ops->is_visible(drvdata, hwmon_temp,
586 hwmon_temp_input, j))
587 continue;
588 if (info[i]->config[j] & HWMON_T_INPUT)
589 hwmon_thermal_add_sensor(dev, hwdev, j);
590 }
591 }
592 }
593
594 return hdev;
595
596free_hwmon:
597 kfree(hwdev);
598ida_remove:
599 ida_simple_remove(&hwmon_ida, id);
600 return ERR_PTR(err);
601}
602
Mark M. Hoffman12364412005-07-15 21:38:08 -0400603/**
Guenter Roeckbab22432013-07-06 13:57:23 -0700604 * hwmon_device_register_with_groups - register w/ hwmon
605 * @dev: the parent device
606 * @name: hwmon name attribute
607 * @drvdata: driver data to attach to created device
608 * @groups: List of attribute groups to create
609 *
610 * hwmon_device_unregister() must be called when the device is no
611 * longer needed.
612 *
613 * Returns the pointer to the new device.
614 */
615struct device *
616hwmon_device_register_with_groups(struct device *dev, const char *name,
617 void *drvdata,
618 const struct attribute_group **groups)
619{
Guenter Roeckd5601682015-08-26 19:38:11 -0700620 return __hwmon_device_register(dev, name, drvdata, NULL, groups);
Guenter Roeckbab22432013-07-06 13:57:23 -0700621}
622EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
623
624/**
Guenter Roeckd5601682015-08-26 19:38:11 -0700625 * hwmon_device_register_with_info - register w/ hwmon
626 * @dev: the parent device
627 * @name: hwmon name attribute
628 * @drvdata: driver data to attach to created device
629 * @info: Pointer to hwmon chip information
630 * @groups - pointer to list of driver specific attribute groups
631 *
632 * hwmon_device_unregister() must be called when the device is no
633 * longer needed.
634 *
635 * Returns the pointer to the new device.
636 */
637struct device *
638hwmon_device_register_with_info(struct device *dev, const char *name,
639 void *drvdata,
640 const struct hwmon_chip_info *chip,
641 const struct attribute_group **groups)
642{
643 if (chip && (!chip->ops || !chip->info))
644 return ERR_PTR(-EINVAL);
645
646 return __hwmon_device_register(dev, name, drvdata, chip, groups);
647}
648EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
649
650/**
Tony Jones1beeffe2007-08-20 13:46:20 -0700651 * hwmon_device_register - register w/ hwmon
Mark M. Hoffman12364412005-07-15 21:38:08 -0400652 * @dev: the device to register
653 *
Tony Jones1beeffe2007-08-20 13:46:20 -0700654 * hwmon_device_unregister() must be called when the device is no
Mark M. Hoffman12364412005-07-15 21:38:08 -0400655 * longer needed.
656 *
Tony Jones1beeffe2007-08-20 13:46:20 -0700657 * Returns the pointer to the new device.
Mark M. Hoffman12364412005-07-15 21:38:08 -0400658 */
Tony Jones1beeffe2007-08-20 13:46:20 -0700659struct device *hwmon_device_register(struct device *dev)
Mark M. Hoffman12364412005-07-15 21:38:08 -0400660{
Guenter Roeckbab22432013-07-06 13:57:23 -0700661 return hwmon_device_register_with_groups(dev, NULL, NULL, NULL);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400662}
Frans Meulenbroeks839a9ee2012-01-08 19:34:14 +0100663EXPORT_SYMBOL_GPL(hwmon_device_register);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400664
665/**
666 * hwmon_device_unregister - removes the previously registered class device
667 *
Tony Jones1beeffe2007-08-20 13:46:20 -0700668 * @dev: the class device to destroy
Mark M. Hoffman12364412005-07-15 21:38:08 -0400669 */
Tony Jones1beeffe2007-08-20 13:46:20 -0700670void hwmon_device_unregister(struct device *dev)
Mark M. Hoffman12364412005-07-15 21:38:08 -0400671{
672 int id;
673
Kay Sievers739cf3a2009-01-06 10:44:41 -0800674 if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
Tony Jones1beeffe2007-08-20 13:46:20 -0700675 device_unregister(dev);
Jonathan Cameron4ca5f462011-10-31 17:10:09 -0700676 ida_simple_remove(&hwmon_ida, id);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400677 } else
Tony Jones1beeffe2007-08-20 13:46:20 -0700678 dev_dbg(dev->parent,
Mark M. Hoffman12364412005-07-15 21:38:08 -0400679 "hwmon_device_unregister() failed: bad class ID!\n");
680}
Frans Meulenbroeks839a9ee2012-01-08 19:34:14 +0100681EXPORT_SYMBOL_GPL(hwmon_device_unregister);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400682
Guenter Roeck74188cb2013-07-11 20:00:12 -0700683static void devm_hwmon_release(struct device *dev, void *res)
684{
685 struct device *hwdev = *(struct device **)res;
686
687 hwmon_device_unregister(hwdev);
688}
689
690/**
691 * devm_hwmon_device_register_with_groups - register w/ hwmon
692 * @dev: the parent device
693 * @name: hwmon name attribute
694 * @drvdata: driver data to attach to created device
695 * @groups: List of attribute groups to create
696 *
697 * Returns the pointer to the new device. The new device is automatically
698 * unregistered with the parent device.
699 */
700struct device *
701devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
702 void *drvdata,
703 const struct attribute_group **groups)
704{
705 struct device **ptr, *hwdev;
706
707 if (!dev)
708 return ERR_PTR(-EINVAL);
709
710 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
711 if (!ptr)
712 return ERR_PTR(-ENOMEM);
713
714 hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
715 if (IS_ERR(hwdev))
716 goto error;
717
718 *ptr = hwdev;
719 devres_add(dev, ptr);
720 return hwdev;
721
722error:
723 devres_free(ptr);
724 return hwdev;
725}
726EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
727
Guenter Roeckd5601682015-08-26 19:38:11 -0700728/**
729 * devm_hwmon_device_register_with_info - register w/ hwmon
730 * @dev: the parent device
731 * @name: hwmon name attribute
732 * @drvdata: driver data to attach to created device
733 * @info: Pointer to hwmon chip information
734 * @groups - pointer to list of driver specific attribute groups
735 *
736 * Returns the pointer to the new device. The new device is automatically
737 * unregistered with the parent device.
738 */
739struct device *
740devm_hwmon_device_register_with_info(struct device *dev, const char *name,
741 void *drvdata,
742 const struct hwmon_chip_info *chip,
743 const struct attribute_group **groups)
744{
745 struct device **ptr, *hwdev;
746
747 if (!dev)
748 return ERR_PTR(-EINVAL);
749
750 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
751 if (!ptr)
752 return ERR_PTR(-ENOMEM);
753
754 hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
755 groups);
756 if (IS_ERR(hwdev))
757 goto error;
758
759 *ptr = hwdev;
760 devres_add(dev, ptr);
761
762 return hwdev;
763
764error:
765 devres_free(ptr);
766 return hwdev;
767}
768EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
769
Guenter Roeck74188cb2013-07-11 20:00:12 -0700770static int devm_hwmon_match(struct device *dev, void *res, void *data)
771{
772 struct device **hwdev = res;
773
774 return *hwdev == data;
775}
776
777/**
778 * devm_hwmon_device_unregister - removes a previously registered hwmon device
779 *
780 * @dev: the parent device of the device to unregister
781 */
782void devm_hwmon_device_unregister(struct device *dev)
783{
784 WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
785}
786EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
787
Jean Delvare2958b1e2009-06-15 18:39:50 +0200788static void __init hwmon_pci_quirks(void)
789{
790#if defined CONFIG_X86 && defined CONFIG_PCI
791 struct pci_dev *sb;
792 u16 base;
793 u8 enable;
794
795 /* Open access to 0x295-0x296 on MSI MS-7031 */
796 sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
Jean Delvared6dab7d2012-12-19 22:16:59 +0100797 if (sb) {
798 if (sb->subsystem_vendor == 0x1462 && /* MSI */
799 sb->subsystem_device == 0x0031) { /* MS-7031 */
800 pci_read_config_byte(sb, 0x48, &enable);
801 pci_read_config_word(sb, 0x64, &base);
Jean Delvare2958b1e2009-06-15 18:39:50 +0200802
Jean Delvared6dab7d2012-12-19 22:16:59 +0100803 if (base == 0 && !(enable & BIT(2))) {
804 dev_info(&sb->dev,
805 "Opening wide generic port at 0x295\n");
806 pci_write_config_word(sb, 0x64, 0x295);
807 pci_write_config_byte(sb, 0x48,
808 enable | BIT(2));
809 }
Jean Delvare2958b1e2009-06-15 18:39:50 +0200810 }
Jean Delvared6dab7d2012-12-19 22:16:59 +0100811 pci_dev_put(sb);
Jean Delvare2958b1e2009-06-15 18:39:50 +0200812 }
813#endif
814}
815
Mark M. Hoffman12364412005-07-15 21:38:08 -0400816static int __init hwmon_init(void)
817{
Guenter Roeckbab22432013-07-06 13:57:23 -0700818 int err;
819
Jean Delvare2958b1e2009-06-15 18:39:50 +0200820 hwmon_pci_quirks();
821
Guenter Roeckbab22432013-07-06 13:57:23 -0700822 err = class_register(&hwmon_class);
823 if (err) {
824 pr_err("couldn't register hwmon sysfs class\n");
825 return err;
Mark M. Hoffman12364412005-07-15 21:38:08 -0400826 }
827 return 0;
828}
829
830static void __exit hwmon_exit(void)
831{
Guenter Roeckbab22432013-07-06 13:57:23 -0700832 class_unregister(&hwmon_class);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400833}
834
David Brownell37f54ee2007-02-14 21:15:04 +0100835subsys_initcall(hwmon_init);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400836module_exit(hwmon_exit);
837
Mark M. Hoffman12364412005-07-15 21:38:08 -0400838MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
839MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
840MODULE_LICENSE("GPL");
841