blob: dcd4445d4570c5072581f52fc0abc6a94e8ae06f [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
Mark M. Hoffman12364412005-07-15 21:38:08 -04002/*
Guenter Roeck5ed04882012-01-19 11:02:18 -08003 * 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>
Guenter Roeck5ed04882012-01-19 11:02:18 -08008 */
Mark M. Hoffman12364412005-07-15 21:38:08 -04009
Joe Perchesc95df1a2010-10-20 06:51:37 +000010#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
Guenter Roeckd5601682015-08-26 19:38:11 -070012#include <linux/bitops.h>
Mark M. Hoffman12364412005-07-15 21:38:08 -040013#include <linux/device.h>
14#include <linux/err.h>
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080015#include <linux/gfp.h>
Guenter Roeckc9ebbe62016-07-10 09:43:21 -070016#include <linux/hwmon.h>
17#include <linux/idr.h>
18#include <linux/module.h>
Jean Delvare2958b1e2009-06-15 18:39:50 +020019#include <linux/pci.h>
Guenter Roeckc9ebbe62016-07-10 09:43:21 -070020#include <linux/slab.h>
Guenter Roeck648cd482014-02-28 10:37:55 -080021#include <linux/string.h>
Guenter Roeckd5601682015-08-26 19:38:11 -070022#include <linux/thermal.h>
Mark M. Hoffman12364412005-07-15 21:38:08 -040023
Nicolin Chen61b8ab22018-10-09 14:42:19 -070024#define CREATE_TRACE_POINTS
25#include <trace/events/hwmon.h>
26
Mark M. Hoffman12364412005-07-15 21:38:08 -040027#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 Roeck3a412d52016-10-16 10:52:04 -070041#define MAX_SYSFS_ATTR_NAME_LENGTH 32
42
Guenter Roeckd5601682015-08-26 19:38:11 -070043struct 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;
Guenter Roeck3a412d52016-10-16 10:52:04 -070049 char name[MAX_SYSFS_ATTR_NAME_LENGTH];
Guenter Roeckd5601682015-08-26 19:38:11 -070050};
51
52#define to_hwmon_attr(d) \
53 container_of(d, struct hwmon_device_attribute, dev_attr)
Guenter Roeck3bf8bdc2020-01-16 10:44:17 -080054#define to_dev_attr(a) container_of(a, struct device_attribute, attr)
Guenter Roeckd5601682015-08-26 19:38:11 -070055
56/*
57 * Thermal zone information
58 * In addition to the reference to the hwmon device,
59 * also provides the sensor index.
60 */
61struct hwmon_thermal_data {
Guenter Roeck3bf8bdc2020-01-16 10:44:17 -080062 struct device *dev; /* Reference to hwmon device */
Guenter Roeckd5601682015-08-26 19:38:11 -070063 int index; /* sensor index */
64};
65
Guenter Roeckbab22432013-07-06 13:57:23 -070066static ssize_t
Julia Lawall2ab0c6c2016-12-22 13:04:45 +010067name_show(struct device *dev, struct device_attribute *attr, char *buf)
Guenter Roeckbab22432013-07-06 13:57:23 -070068{
69 return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
70}
Julia Lawall2ab0c6c2016-12-22 13:04:45 +010071static DEVICE_ATTR_RO(name);
Guenter Roeckbab22432013-07-06 13:57:23 -070072
73static struct attribute *hwmon_dev_attrs[] = {
74 &dev_attr_name.attr,
75 NULL
76};
77
78static umode_t hwmon_dev_name_is_visible(struct kobject *kobj,
79 struct attribute *attr, int n)
80{
81 struct device *dev = container_of(kobj, struct device, kobj);
82
83 if (to_hwmon_device(dev)->name == NULL)
84 return 0;
85
86 return attr->mode;
87}
88
Arvind Yadav524703ac2017-07-05 10:31:05 +053089static const struct attribute_group hwmon_dev_attr_group = {
Guenter Roeckbab22432013-07-06 13:57:23 -070090 .attrs = hwmon_dev_attrs,
91 .is_visible = hwmon_dev_name_is_visible,
92};
93
94static const struct attribute_group *hwmon_dev_attr_groups[] = {
95 &hwmon_dev_attr_group,
96 NULL
97};
98
Guenter Roeck3bf8bdc2020-01-16 10:44:17 -080099static void hwmon_free_attrs(struct attribute **attrs)
100{
101 int i;
102
103 for (i = 0; attrs[i]; i++) {
104 struct device_attribute *dattr = to_dev_attr(attrs[i]);
105 struct hwmon_device_attribute *hattr = to_hwmon_attr(dattr);
106
107 kfree(hattr);
108 }
109 kfree(attrs);
110}
111
Guenter Roeckbab22432013-07-06 13:57:23 -0700112static void hwmon_dev_release(struct device *dev)
113{
Guenter Roeck3bf8bdc2020-01-16 10:44:17 -0800114 struct hwmon_device *hwdev = to_hwmon_device(dev);
115
116 if (hwdev->group.attrs)
117 hwmon_free_attrs(hwdev->group.attrs);
118 kfree(hwdev->groups);
119 kfree(hwdev);
Guenter Roeckbab22432013-07-06 13:57:23 -0700120}
121
122static struct class hwmon_class = {
123 .name = "hwmon",
124 .owner = THIS_MODULE,
125 .dev_groups = hwmon_dev_attr_groups,
126 .dev_release = hwmon_dev_release,
127};
Mark M. Hoffman12364412005-07-15 21:38:08 -0400128
Jonathan Cameron4ca5f462011-10-31 17:10:09 -0700129static DEFINE_IDA(hwmon_ida);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400130
Guenter Roeckd5601682015-08-26 19:38:11 -0700131/* Thermal zone handling */
132
Guenter Roeck86430c12016-08-12 06:28:15 -0700133/*
134 * The complex conditional is necessary to avoid a cyclic dependency
135 * between hwmon and thermal_sys modules.
136 */
Daniel Lezcanof3735332019-04-02 18:12:50 +0200137#ifdef CONFIG_THERMAL_OF
Guenter Roeckd5601682015-08-26 19:38:11 -0700138static int hwmon_thermal_get_temp(void *data, int *temp)
139{
140 struct hwmon_thermal_data *tdata = data;
Guenter Roeck3bf8bdc2020-01-16 10:44:17 -0800141 struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
Guenter Roeckd5601682015-08-26 19:38:11 -0700142 int ret;
143 long t;
144
Guenter Roeck3bf8bdc2020-01-16 10:44:17 -0800145 ret = hwdev->chip->ops->read(tdata->dev, hwmon_temp, hwmon_temp_input,
Guenter Roeckd5601682015-08-26 19:38:11 -0700146 tdata->index, &t);
147 if (ret < 0)
148 return ret;
149
150 *temp = t;
151
152 return 0;
153}
154
Julia Lawallc9920652017-08-08 17:09:02 +0200155static const struct thermal_zone_of_device_ops hwmon_thermal_ops = {
Guenter Roeckd5601682015-08-26 19:38:11 -0700156 .get_temp = hwmon_thermal_get_temp,
157};
158
Guenter Roeck3bf8bdc2020-01-16 10:44:17 -0800159static int hwmon_thermal_add_sensor(struct device *dev, int index)
Guenter Roeckd5601682015-08-26 19:38:11 -0700160{
161 struct hwmon_thermal_data *tdata;
Linus Walleij47c332d2017-12-05 09:36:14 +0100162 struct thermal_zone_device *tzd;
Guenter Roeckd5601682015-08-26 19:38:11 -0700163
164 tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
165 if (!tdata)
166 return -ENOMEM;
167
Guenter Roeck3bf8bdc2020-01-16 10:44:17 -0800168 tdata->dev = dev;
Guenter Roeckd5601682015-08-26 19:38:11 -0700169 tdata->index = index;
170
Guenter Roeck3bf8bdc2020-01-16 10:44:17 -0800171 tzd = devm_thermal_zone_of_sensor_register(dev, index, tdata,
Linus Walleij47c332d2017-12-05 09:36:14 +0100172 &hwmon_thermal_ops);
173 /*
174 * If CONFIG_THERMAL_OF is disabled, this returns -ENODEV,
175 * so ignore that error but forward any other error.
176 */
177 if (IS_ERR(tzd) && (PTR_ERR(tzd) != -ENODEV))
178 return PTR_ERR(tzd);
Guenter Roeckd5601682015-08-26 19:38:11 -0700179
180 return 0;
181}
Akinobu Mita44e3ad82020-05-04 23:57:44 +0900182
183static int hwmon_thermal_register_sensors(struct device *dev)
184{
185 struct hwmon_device *hwdev = to_hwmon_device(dev);
186 const struct hwmon_chip_info *chip = hwdev->chip;
187 const struct hwmon_channel_info **info = chip->info;
188 void *drvdata = dev_get_drvdata(dev);
189 int i;
190
191 for (i = 1; info[i]; i++) {
192 int j;
193
194 if (info[i]->type != hwmon_temp)
195 continue;
196
197 for (j = 0; info[i]->config[j]; j++) {
198 int err;
199
200 if (!(info[i]->config[j] & HWMON_T_INPUT) ||
201 !chip->ops->is_visible(drvdata, hwmon_temp,
202 hwmon_temp_input, j))
203 continue;
204
205 err = hwmon_thermal_add_sensor(dev, j);
206 if (err)
207 return err;
208 }
209 }
210
211 return 0;
212}
213
Guenter Roeckd5601682015-08-26 19:38:11 -0700214#else
Akinobu Mita44e3ad82020-05-04 23:57:44 +0900215static int hwmon_thermal_register_sensors(struct device *dev)
Guenter Roeckd5601682015-08-26 19:38:11 -0700216{
217 return 0;
218}
Guenter Roeck86430c12016-08-12 06:28:15 -0700219#endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
Guenter Roeckd5601682015-08-26 19:38:11 -0700220
Nicolin Chen61b8ab22018-10-09 14:42:19 -0700221static int hwmon_attr_base(enum hwmon_sensor_types type)
222{
Dr. David Alan Gilbert44134052019-11-24 20:20:29 +0000223 if (type == hwmon_in || type == hwmon_intrusion)
Nicolin Chen61b8ab22018-10-09 14:42:19 -0700224 return 0;
225 return 1;
226}
227
Guenter Roeckd5601682015-08-26 19:38:11 -0700228/* sysfs attribute management */
229
230static ssize_t hwmon_attr_show(struct device *dev,
231 struct device_attribute *devattr, char *buf)
232{
233 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
234 long val;
235 int ret;
236
237 ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
238 &val);
239 if (ret < 0)
240 return ret;
241
Nicolin Chen61b8ab22018-10-09 14:42:19 -0700242 trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type),
243 hattr->name, val);
244
Guenter Roeckd5601682015-08-26 19:38:11 -0700245 return sprintf(buf, "%ld\n", val);
246}
247
Guenter Roecke159ab52016-08-07 20:51:25 -0700248static ssize_t hwmon_attr_show_string(struct device *dev,
249 struct device_attribute *devattr,
250 char *buf)
251{
252 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
Nicolin Chen61b8ab22018-10-09 14:42:19 -0700253 enum hwmon_sensor_types type = hattr->type;
Jean Delvare5ba6bcb2017-03-07 06:15:15 -0800254 const char *s;
Guenter Roecke159ab52016-08-07 20:51:25 -0700255 int ret;
256
257 ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
258 hattr->index, &s);
259 if (ret < 0)
260 return ret;
261
Nicolin Chen61b8ab22018-10-09 14:42:19 -0700262 trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type),
263 hattr->name, s);
264
Guenter Roecke159ab52016-08-07 20:51:25 -0700265 return sprintf(buf, "%s\n", s);
266}
267
Guenter Roeckd5601682015-08-26 19:38:11 -0700268static ssize_t hwmon_attr_store(struct device *dev,
269 struct device_attribute *devattr,
270 const char *buf, size_t count)
271{
272 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
273 long val;
274 int ret;
275
276 ret = kstrtol(buf, 10, &val);
277 if (ret < 0)
278 return ret;
279
280 ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
281 val);
282 if (ret < 0)
283 return ret;
284
Nicolin Chen61b8ab22018-10-09 14:42:19 -0700285 trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type),
286 hattr->name, val);
Guenter Roeckd5601682015-08-26 19:38:11 -0700287
Nicolin Chen61b8ab22018-10-09 14:42:19 -0700288 return count;
Guenter Roeckd5601682015-08-26 19:38:11 -0700289}
290
Guenter Roecke159ab52016-08-07 20:51:25 -0700291static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
292{
293 return (type == hwmon_temp && attr == hwmon_temp_label) ||
294 (type == hwmon_in && attr == hwmon_in_label) ||
295 (type == hwmon_curr && attr == hwmon_curr_label) ||
296 (type == hwmon_power && attr == hwmon_power_label) ||
297 (type == hwmon_energy && attr == hwmon_energy_label) ||
298 (type == hwmon_humidity && attr == hwmon_humidity_label) ||
299 (type == hwmon_fan && attr == hwmon_fan_label);
300}
301
Guenter Roeck3bf8bdc2020-01-16 10:44:17 -0800302static struct attribute *hwmon_genattr(const void *drvdata,
Guenter Roeckd5601682015-08-26 19:38:11 -0700303 enum hwmon_sensor_types type,
304 u32 attr,
305 int index,
306 const char *template,
307 const struct hwmon_ops *ops)
308{
309 struct hwmon_device_attribute *hattr;
310 struct device_attribute *dattr;
311 struct attribute *a;
312 umode_t mode;
Rasmus Villemoes3b443de2018-10-27 00:30:59 +0200313 const char *name;
Guenter Roecke159ab52016-08-07 20:51:25 -0700314 bool is_string = is_string_attr(type, attr);
Guenter Roeckd5601682015-08-26 19:38:11 -0700315
316 /* The attribute is invisible if there is no template string */
317 if (!template)
318 return ERR_PTR(-ENOENT);
319
320 mode = ops->is_visible(drvdata, type, attr, index);
321 if (!mode)
322 return ERR_PTR(-ENOENT);
323
Guenter Roeck0d871162018-12-10 14:02:08 -0800324 if ((mode & 0444) && ((is_string && !ops->read_string) ||
Guenter Roecke159ab52016-08-07 20:51:25 -0700325 (!is_string && !ops->read)))
Guenter Roeckd5601682015-08-26 19:38:11 -0700326 return ERR_PTR(-EINVAL);
Guenter Roeck0d871162018-12-10 14:02:08 -0800327 if ((mode & 0222) && !ops->write)
Guenter Roeckd5601682015-08-26 19:38:11 -0700328 return ERR_PTR(-EINVAL);
329
Guenter Roeck3bf8bdc2020-01-16 10:44:17 -0800330 hattr = kzalloc(sizeof(*hattr), GFP_KERNEL);
Guenter Roeckd5601682015-08-26 19:38:11 -0700331 if (!hattr)
332 return ERR_PTR(-ENOMEM);
333
Guenter Roeck3a412d52016-10-16 10:52:04 -0700334 if (type == hwmon_chip) {
Rasmus Villemoes3b443de2018-10-27 00:30:59 +0200335 name = template;
Guenter Roeck3a412d52016-10-16 10:52:04 -0700336 } else {
337 scnprintf(hattr->name, sizeof(hattr->name), template,
338 index + hwmon_attr_base(type));
339 name = hattr->name;
340 }
341
Guenter Roeckd5601682015-08-26 19:38:11 -0700342 hattr->type = type;
343 hattr->attr = attr;
344 hattr->index = index;
345 hattr->ops = ops;
346
347 dattr = &hattr->dev_attr;
Guenter Roecke159ab52016-08-07 20:51:25 -0700348 dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
Guenter Roeckd5601682015-08-26 19:38:11 -0700349 dattr->store = hwmon_attr_store;
350
351 a = &dattr->attr;
352 sysfs_attr_init(a);
353 a->name = name;
354 a->mode = mode;
355
356 return a;
357}
358
Guenter Roeckf4d325d2016-10-16 10:38:52 -0700359/*
360 * Chip attributes are not attribute templates but actual sysfs attributes.
361 * See hwmon_genattr() for special handling.
362 */
363static const char * const hwmon_chip_attrs[] = {
Guenter Roeckd5601682015-08-26 19:38:11 -0700364 [hwmon_chip_temp_reset_history] = "temp_reset_history",
Guenter Roeck00d616c2016-06-20 11:01:57 -0700365 [hwmon_chip_in_reset_history] = "in_reset_history",
Guenter Roeck9b269472016-06-20 11:10:33 -0700366 [hwmon_chip_curr_reset_history] = "curr_reset_history",
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700367 [hwmon_chip_power_reset_history] = "power_reset_history",
Guenter Roeckd5601682015-08-26 19:38:11 -0700368 [hwmon_chip_update_interval] = "update_interval",
369 [hwmon_chip_alarms] = "alarms",
Guenter Roeck9f009952019-04-15 13:23:48 -0700370 [hwmon_chip_samples] = "samples",
371 [hwmon_chip_curr_samples] = "curr_samples",
372 [hwmon_chip_in_samples] = "in_samples",
373 [hwmon_chip_power_samples] = "power_samples",
374 [hwmon_chip_temp_samples] = "temp_samples",
Guenter Roeckd5601682015-08-26 19:38:11 -0700375};
376
377static const char * const hwmon_temp_attr_templates[] = {
Guenter Roeck002c6b52018-07-17 10:17:19 -0700378 [hwmon_temp_enable] = "temp%d_enable",
Guenter Roeckd5601682015-08-26 19:38:11 -0700379 [hwmon_temp_input] = "temp%d_input",
380 [hwmon_temp_type] = "temp%d_type",
381 [hwmon_temp_lcrit] = "temp%d_lcrit",
382 [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
383 [hwmon_temp_min] = "temp%d_min",
384 [hwmon_temp_min_hyst] = "temp%d_min_hyst",
385 [hwmon_temp_max] = "temp%d_max",
386 [hwmon_temp_max_hyst] = "temp%d_max_hyst",
387 [hwmon_temp_crit] = "temp%d_crit",
388 [hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
389 [hwmon_temp_emergency] = "temp%d_emergency",
390 [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
391 [hwmon_temp_alarm] = "temp%d_alarm",
392 [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
393 [hwmon_temp_min_alarm] = "temp%d_min_alarm",
394 [hwmon_temp_max_alarm] = "temp%d_max_alarm",
395 [hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
396 [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
397 [hwmon_temp_fault] = "temp%d_fault",
398 [hwmon_temp_offset] = "temp%d_offset",
399 [hwmon_temp_label] = "temp%d_label",
400 [hwmon_temp_lowest] = "temp%d_lowest",
401 [hwmon_temp_highest] = "temp%d_highest",
402 [hwmon_temp_reset_history] = "temp%d_reset_history",
403};
404
Guenter Roeck00d616c2016-06-20 11:01:57 -0700405static const char * const hwmon_in_attr_templates[] = {
Guenter Roeck002c6b52018-07-17 10:17:19 -0700406 [hwmon_in_enable] = "in%d_enable",
Guenter Roeck00d616c2016-06-20 11:01:57 -0700407 [hwmon_in_input] = "in%d_input",
408 [hwmon_in_min] = "in%d_min",
409 [hwmon_in_max] = "in%d_max",
410 [hwmon_in_lcrit] = "in%d_lcrit",
411 [hwmon_in_crit] = "in%d_crit",
412 [hwmon_in_average] = "in%d_average",
413 [hwmon_in_lowest] = "in%d_lowest",
414 [hwmon_in_highest] = "in%d_highest",
415 [hwmon_in_reset_history] = "in%d_reset_history",
416 [hwmon_in_label] = "in%d_label",
417 [hwmon_in_alarm] = "in%d_alarm",
418 [hwmon_in_min_alarm] = "in%d_min_alarm",
419 [hwmon_in_max_alarm] = "in%d_max_alarm",
420 [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
421 [hwmon_in_crit_alarm] = "in%d_crit_alarm",
422};
423
Guenter Roeck9b269472016-06-20 11:10:33 -0700424static const char * const hwmon_curr_attr_templates[] = {
Guenter Roeck002c6b52018-07-17 10:17:19 -0700425 [hwmon_curr_enable] = "curr%d_enable",
Guenter Roeck9b269472016-06-20 11:10:33 -0700426 [hwmon_curr_input] = "curr%d_input",
427 [hwmon_curr_min] = "curr%d_min",
428 [hwmon_curr_max] = "curr%d_max",
429 [hwmon_curr_lcrit] = "curr%d_lcrit",
430 [hwmon_curr_crit] = "curr%d_crit",
431 [hwmon_curr_average] = "curr%d_average",
432 [hwmon_curr_lowest] = "curr%d_lowest",
433 [hwmon_curr_highest] = "curr%d_highest",
434 [hwmon_curr_reset_history] = "curr%d_reset_history",
435 [hwmon_curr_label] = "curr%d_label",
436 [hwmon_curr_alarm] = "curr%d_alarm",
437 [hwmon_curr_min_alarm] = "curr%d_min_alarm",
438 [hwmon_curr_max_alarm] = "curr%d_max_alarm",
439 [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
440 [hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
441};
442
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700443static const char * const hwmon_power_attr_templates[] = {
Guenter Roeck002c6b52018-07-17 10:17:19 -0700444 [hwmon_power_enable] = "power%d_enable",
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700445 [hwmon_power_average] = "power%d_average",
446 [hwmon_power_average_interval] = "power%d_average_interval",
447 [hwmon_power_average_interval_max] = "power%d_interval_max",
448 [hwmon_power_average_interval_min] = "power%d_interval_min",
449 [hwmon_power_average_highest] = "power%d_average_highest",
450 [hwmon_power_average_lowest] = "power%d_average_lowest",
451 [hwmon_power_average_max] = "power%d_average_max",
452 [hwmon_power_average_min] = "power%d_average_min",
453 [hwmon_power_input] = "power%d_input",
454 [hwmon_power_input_highest] = "power%d_input_highest",
455 [hwmon_power_input_lowest] = "power%d_input_lowest",
456 [hwmon_power_reset_history] = "power%d_reset_history",
457 [hwmon_power_accuracy] = "power%d_accuracy",
458 [hwmon_power_cap] = "power%d_cap",
459 [hwmon_power_cap_hyst] = "power%d_cap_hyst",
460 [hwmon_power_cap_max] = "power%d_cap_max",
461 [hwmon_power_cap_min] = "power%d_cap_min",
Andrew Lunnaa7f29b2018-07-17 21:48:11 +0200462 [hwmon_power_min] = "power%d_min",
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700463 [hwmon_power_max] = "power%d_max",
Andrew Lunnaa7f29b2018-07-17 21:48:11 +0200464 [hwmon_power_lcrit] = "power%d_lcrit",
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700465 [hwmon_power_crit] = "power%d_crit",
466 [hwmon_power_label] = "power%d_label",
467 [hwmon_power_alarm] = "power%d_alarm",
468 [hwmon_power_cap_alarm] = "power%d_cap_alarm",
Andrew Lunnaa7f29b2018-07-17 21:48:11 +0200469 [hwmon_power_min_alarm] = "power%d_min_alarm",
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700470 [hwmon_power_max_alarm] = "power%d_max_alarm",
Andrew Lunnaa7f29b2018-07-17 21:48:11 +0200471 [hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700472 [hwmon_power_crit_alarm] = "power%d_crit_alarm",
473};
474
Guenter Roeck6bfcca42016-06-20 11:38:37 -0700475static const char * const hwmon_energy_attr_templates[] = {
Guenter Roeck002c6b52018-07-17 10:17:19 -0700476 [hwmon_energy_enable] = "energy%d_enable",
Guenter Roeck6bfcca42016-06-20 11:38:37 -0700477 [hwmon_energy_input] = "energy%d_input",
478 [hwmon_energy_label] = "energy%d_label",
479};
480
481static const char * const hwmon_humidity_attr_templates[] = {
Guenter Roeck002c6b52018-07-17 10:17:19 -0700482 [hwmon_humidity_enable] = "humidity%d_enable",
Guenter Roeck6bfcca42016-06-20 11:38:37 -0700483 [hwmon_humidity_input] = "humidity%d_input",
484 [hwmon_humidity_label] = "humidity%d_label",
485 [hwmon_humidity_min] = "humidity%d_min",
486 [hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
487 [hwmon_humidity_max] = "humidity%d_max",
488 [hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
489 [hwmon_humidity_alarm] = "humidity%d_alarm",
490 [hwmon_humidity_fault] = "humidity%d_fault",
491};
492
Guenter Roeck8faee732016-06-25 19:52:13 -0700493static const char * const hwmon_fan_attr_templates[] = {
Guenter Roeck002c6b52018-07-17 10:17:19 -0700494 [hwmon_fan_enable] = "fan%d_enable",
Guenter Roeck8faee732016-06-25 19:52:13 -0700495 [hwmon_fan_input] = "fan%d_input",
496 [hwmon_fan_label] = "fan%d_label",
497 [hwmon_fan_min] = "fan%d_min",
498 [hwmon_fan_max] = "fan%d_max",
499 [hwmon_fan_div] = "fan%d_div",
500 [hwmon_fan_pulses] = "fan%d_pulses",
501 [hwmon_fan_target] = "fan%d_target",
502 [hwmon_fan_alarm] = "fan%d_alarm",
503 [hwmon_fan_min_alarm] = "fan%d_min_alarm",
504 [hwmon_fan_max_alarm] = "fan%d_max_alarm",
505 [hwmon_fan_fault] = "fan%d_fault",
506};
507
Guenter Roeckf9f7bb32016-06-26 12:20:46 -0700508static const char * const hwmon_pwm_attr_templates[] = {
509 [hwmon_pwm_input] = "pwm%d",
510 [hwmon_pwm_enable] = "pwm%d_enable",
511 [hwmon_pwm_mode] = "pwm%d_mode",
512 [hwmon_pwm_freq] = "pwm%d_freq",
513};
514
Dr. David Alan Gilbert44134052019-11-24 20:20:29 +0000515static const char * const hwmon_intrusion_attr_templates[] = {
516 [hwmon_intrusion_alarm] = "intrusion%d_alarm",
517 [hwmon_intrusion_beep] = "intrusion%d_beep",
518};
519
Guenter Roeckd5601682015-08-26 19:38:11 -0700520static const char * const *__templates[] = {
Guenter Roeckf4d325d2016-10-16 10:38:52 -0700521 [hwmon_chip] = hwmon_chip_attrs,
Guenter Roeckd5601682015-08-26 19:38:11 -0700522 [hwmon_temp] = hwmon_temp_attr_templates,
Guenter Roeck00d616c2016-06-20 11:01:57 -0700523 [hwmon_in] = hwmon_in_attr_templates,
Guenter Roeck9b269472016-06-20 11:10:33 -0700524 [hwmon_curr] = hwmon_curr_attr_templates,
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700525 [hwmon_power] = hwmon_power_attr_templates,
Guenter Roeck6bfcca42016-06-20 11:38:37 -0700526 [hwmon_energy] = hwmon_energy_attr_templates,
527 [hwmon_humidity] = hwmon_humidity_attr_templates,
Guenter Roeck8faee732016-06-25 19:52:13 -0700528 [hwmon_fan] = hwmon_fan_attr_templates,
Guenter Roeckf9f7bb32016-06-26 12:20:46 -0700529 [hwmon_pwm] = hwmon_pwm_attr_templates,
Dr. David Alan Gilbert44134052019-11-24 20:20:29 +0000530 [hwmon_intrusion] = hwmon_intrusion_attr_templates,
Guenter Roeckd5601682015-08-26 19:38:11 -0700531};
532
533static const int __templates_size[] = {
Guenter Roeckf4d325d2016-10-16 10:38:52 -0700534 [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
Guenter Roeckd5601682015-08-26 19:38:11 -0700535 [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
Guenter Roeck00d616c2016-06-20 11:01:57 -0700536 [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
Guenter Roeck9b269472016-06-20 11:10:33 -0700537 [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700538 [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
Guenter Roeck6bfcca42016-06-20 11:38:37 -0700539 [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
540 [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
Guenter Roeck8faee732016-06-25 19:52:13 -0700541 [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
Guenter Roeckf9f7bb32016-06-26 12:20:46 -0700542 [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
Dr. David Alan Gilbert44134052019-11-24 20:20:29 +0000543 [hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates),
Guenter Roeckd5601682015-08-26 19:38:11 -0700544};
545
546static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
547{
548 int i, n;
549
550 for (i = n = 0; info->config[i]; i++)
551 n += hweight32(info->config[i]);
552
553 return n;
554}
555
Guenter Roeck3bf8bdc2020-01-16 10:44:17 -0800556static int hwmon_genattrs(const void *drvdata,
Guenter Roeckd5601682015-08-26 19:38:11 -0700557 struct attribute **attrs,
558 const struct hwmon_ops *ops,
559 const struct hwmon_channel_info *info)
560{
561 const char * const *templates;
562 int template_size;
563 int i, aindex = 0;
564
565 if (info->type >= ARRAY_SIZE(__templates))
566 return -EINVAL;
567
568 templates = __templates[info->type];
569 template_size = __templates_size[info->type];
570
571 for (i = 0; info->config[i]; i++) {
572 u32 attr_mask = info->config[i];
573 u32 attr;
574
575 while (attr_mask) {
576 struct attribute *a;
577
578 attr = __ffs(attr_mask);
579 attr_mask &= ~BIT(attr);
580 if (attr >= template_size)
581 return -EINVAL;
Guenter Roeck3bf8bdc2020-01-16 10:44:17 -0800582 a = hwmon_genattr(drvdata, info->type, attr, i,
Guenter Roeckd5601682015-08-26 19:38:11 -0700583 templates[attr], ops);
584 if (IS_ERR(a)) {
585 if (PTR_ERR(a) != -ENOENT)
586 return PTR_ERR(a);
587 continue;
588 }
589 attrs[aindex++] = a;
590 }
591 }
592 return aindex;
593}
594
595static struct attribute **
Guenter Roeck3bf8bdc2020-01-16 10:44:17 -0800596__hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip)
Guenter Roeckd5601682015-08-26 19:38:11 -0700597{
598 int ret, i, aindex = 0, nattrs = 0;
599 struct attribute **attrs;
600
601 for (i = 0; chip->info[i]; i++)
602 nattrs += hwmon_num_channel_attrs(chip->info[i]);
603
604 if (nattrs == 0)
605 return ERR_PTR(-EINVAL);
606
Guenter Roeck3bf8bdc2020-01-16 10:44:17 -0800607 attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL);
Guenter Roeckd5601682015-08-26 19:38:11 -0700608 if (!attrs)
609 return ERR_PTR(-ENOMEM);
610
611 for (i = 0; chip->info[i]; i++) {
Guenter Roeck3bf8bdc2020-01-16 10:44:17 -0800612 ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops,
Guenter Roeckd5601682015-08-26 19:38:11 -0700613 chip->info[i]);
Guenter Roeck3bf8bdc2020-01-16 10:44:17 -0800614 if (ret < 0) {
615 hwmon_free_attrs(attrs);
Guenter Roeckd5601682015-08-26 19:38:11 -0700616 return ERR_PTR(ret);
Guenter Roeck3bf8bdc2020-01-16 10:44:17 -0800617 }
Guenter Roeckd5601682015-08-26 19:38:11 -0700618 aindex += ret;
619 }
620
621 return attrs;
622}
623
624static struct device *
625__hwmon_device_register(struct device *dev, const char *name, void *drvdata,
626 const struct hwmon_chip_info *chip,
627 const struct attribute_group **groups)
628{
629 struct hwmon_device *hwdev;
630 struct device *hdev;
Akinobu Mita44e3ad82020-05-04 23:57:44 +0900631 int i, err, id;
Guenter Roeckd5601682015-08-26 19:38:11 -0700632
Guenter Roeck74d3b642017-01-27 19:35:57 -0800633 /* Complain about invalid characters in hwmon name attribute */
Guenter Roeckd5601682015-08-26 19:38:11 -0700634 if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
Guenter Roeck74d3b642017-01-27 19:35:57 -0800635 dev_warn(dev,
636 "hwmon: '%s' is not a valid name attribute, please fix\n",
637 name);
Guenter Roeckd5601682015-08-26 19:38:11 -0700638
639 id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
640 if (id < 0)
641 return ERR_PTR(id);
642
643 hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
644 if (hwdev == NULL) {
645 err = -ENOMEM;
646 goto ida_remove;
647 }
648
649 hdev = &hwdev->dev;
650
Guenter Roeck239552f2016-10-16 17:06:20 -0700651 if (chip) {
Guenter Roeckd5601682015-08-26 19:38:11 -0700652 struct attribute **attrs;
Guenter Roeckb2a4cc32016-10-16 17:11:52 -0700653 int ngroups = 2; /* terminating NULL plus &hwdev->groups */
Guenter Roeckd5601682015-08-26 19:38:11 -0700654
655 if (groups)
656 for (i = 0; groups[i]; i++)
657 ngroups++;
658
Guenter Roeck3bf8bdc2020-01-16 10:44:17 -0800659 hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL);
Colin Ian King38d8ed62016-10-23 21:56:08 +0100660 if (!hwdev->groups) {
661 err = -ENOMEM;
662 goto free_hwmon;
663 }
Guenter Roeckd5601682015-08-26 19:38:11 -0700664
Guenter Roeck3bf8bdc2020-01-16 10:44:17 -0800665 attrs = __hwmon_create_attrs(drvdata, chip);
Guenter Roeckd5601682015-08-26 19:38:11 -0700666 if (IS_ERR(attrs)) {
667 err = PTR_ERR(attrs);
668 goto free_hwmon;
669 }
670
671 hwdev->group.attrs = attrs;
672 ngroups = 0;
673 hwdev->groups[ngroups++] = &hwdev->group;
674
675 if (groups) {
676 for (i = 0; groups[i]; i++)
677 hwdev->groups[ngroups++] = groups[i];
678 }
679
680 hdev->groups = hwdev->groups;
681 } else {
682 hdev->groups = groups;
683 }
684
685 hwdev->name = name;
686 hdev->class = &hwmon_class;
687 hdev->parent = dev;
688 hdev->of_node = dev ? dev->of_node : NULL;
689 hwdev->chip = chip;
690 dev_set_drvdata(hdev, drvdata);
691 dev_set_name(hdev, HWMON_ID_FORMAT, id);
692 err = device_register(hdev);
693 if (err)
694 goto free_hwmon;
695
Eduardo Valentinc41dd482019-05-29 19:56:04 -0700696 if (dev && dev->of_node && chip && chip->ops->read &&
Guenter Roeckd5601682015-08-26 19:38:11 -0700697 chip->info[0]->type == hwmon_chip &&
698 (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
Akinobu Mita44e3ad82020-05-04 23:57:44 +0900699 err = hwmon_thermal_register_sensors(hdev);
700 if (err) {
701 device_unregister(hdev);
702 /*
703 * Don't worry about hwdev; hwmon_dev_release(), called
704 * from device_unregister(), will free it.
705 */
706 goto ida_remove;
Guenter Roeckd5601682015-08-26 19:38:11 -0700707 }
708 }
709
710 return hdev;
711
712free_hwmon:
Guenter Roeck3bf8bdc2020-01-16 10:44:17 -0800713 hwmon_dev_release(hdev);
Guenter Roeckd5601682015-08-26 19:38:11 -0700714ida_remove:
715 ida_simple_remove(&hwmon_ida, id);
716 return ERR_PTR(err);
717}
718
Mark M. Hoffman12364412005-07-15 21:38:08 -0400719/**
Guenter Roeckbab22432013-07-06 13:57:23 -0700720 * hwmon_device_register_with_groups - register w/ hwmon
721 * @dev: the parent device
722 * @name: hwmon name attribute
723 * @drvdata: driver data to attach to created device
724 * @groups: List of attribute groups to create
725 *
726 * hwmon_device_unregister() must be called when the device is no
727 * longer needed.
728 *
729 * Returns the pointer to the new device.
730 */
731struct device *
732hwmon_device_register_with_groups(struct device *dev, const char *name,
733 void *drvdata,
734 const struct attribute_group **groups)
735{
Guenter Roeck83538632017-01-24 06:32:57 -0800736 if (!name)
737 return ERR_PTR(-EINVAL);
738
Guenter Roeckd5601682015-08-26 19:38:11 -0700739 return __hwmon_device_register(dev, name, drvdata, NULL, groups);
Guenter Roeckbab22432013-07-06 13:57:23 -0700740}
741EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
742
743/**
Guenter Roeckd5601682015-08-26 19:38:11 -0700744 * hwmon_device_register_with_info - register w/ hwmon
745 * @dev: the parent device
746 * @name: hwmon name attribute
747 * @drvdata: driver data to attach to created device
Guenter Roeck38709452017-12-03 15:15:00 -0800748 * @chip: pointer to hwmon chip information
Guenter Roeck848ba0a2016-10-16 17:20:43 -0700749 * @extra_groups: pointer to list of additional non-standard attribute groups
Guenter Roeckd5601682015-08-26 19:38:11 -0700750 *
751 * hwmon_device_unregister() must be called when the device is no
752 * longer needed.
753 *
754 * Returns the pointer to the new device.
755 */
756struct device *
757hwmon_device_register_with_info(struct device *dev, const char *name,
758 void *drvdata,
759 const struct hwmon_chip_info *chip,
Guenter Roeck848ba0a2016-10-16 17:20:43 -0700760 const struct attribute_group **extra_groups)
Guenter Roeckd5601682015-08-26 19:38:11 -0700761{
Guenter Roeck83538632017-01-24 06:32:57 -0800762 if (!name)
763 return ERR_PTR(-EINVAL);
764
Guenter Roeck239552f2016-10-16 17:06:20 -0700765 if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info))
Guenter Roeckd5601682015-08-26 19:38:11 -0700766 return ERR_PTR(-EINVAL);
767
Lucas Magasweran59df4f42018-05-08 04:43:33 -0700768 if (chip && !dev)
769 return ERR_PTR(-EINVAL);
770
Guenter Roeck848ba0a2016-10-16 17:20:43 -0700771 return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
Guenter Roeckd5601682015-08-26 19:38:11 -0700772}
773EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
774
775/**
Tony Jones1beeffe2007-08-20 13:46:20 -0700776 * hwmon_device_register - register w/ hwmon
Mark M. Hoffman12364412005-07-15 21:38:08 -0400777 * @dev: the device to register
778 *
Tony Jones1beeffe2007-08-20 13:46:20 -0700779 * hwmon_device_unregister() must be called when the device is no
Mark M. Hoffman12364412005-07-15 21:38:08 -0400780 * longer needed.
781 *
Tony Jones1beeffe2007-08-20 13:46:20 -0700782 * Returns the pointer to the new device.
Mark M. Hoffman12364412005-07-15 21:38:08 -0400783 */
Tony Jones1beeffe2007-08-20 13:46:20 -0700784struct device *hwmon_device_register(struct device *dev)
Mark M. Hoffman12364412005-07-15 21:38:08 -0400785{
Guenter Roeckaf1bd362016-10-16 11:31:08 -0700786 dev_warn(dev,
787 "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
788
Guenter Roeck83538632017-01-24 06:32:57 -0800789 return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400790}
Frans Meulenbroeks839a9ee2012-01-08 19:34:14 +0100791EXPORT_SYMBOL_GPL(hwmon_device_register);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400792
793/**
794 * hwmon_device_unregister - removes the previously registered class device
795 *
Tony Jones1beeffe2007-08-20 13:46:20 -0700796 * @dev: the class device to destroy
Mark M. Hoffman12364412005-07-15 21:38:08 -0400797 */
Tony Jones1beeffe2007-08-20 13:46:20 -0700798void hwmon_device_unregister(struct device *dev)
Mark M. Hoffman12364412005-07-15 21:38:08 -0400799{
800 int id;
801
Kay Sievers739cf3a2009-01-06 10:44:41 -0800802 if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
Tony Jones1beeffe2007-08-20 13:46:20 -0700803 device_unregister(dev);
Jonathan Cameron4ca5f462011-10-31 17:10:09 -0700804 ida_simple_remove(&hwmon_ida, id);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400805 } else
Tony Jones1beeffe2007-08-20 13:46:20 -0700806 dev_dbg(dev->parent,
Mark M. Hoffman12364412005-07-15 21:38:08 -0400807 "hwmon_device_unregister() failed: bad class ID!\n");
808}
Frans Meulenbroeks839a9ee2012-01-08 19:34:14 +0100809EXPORT_SYMBOL_GPL(hwmon_device_unregister);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400810
Guenter Roeck74188cb2013-07-11 20:00:12 -0700811static void devm_hwmon_release(struct device *dev, void *res)
812{
813 struct device *hwdev = *(struct device **)res;
814
815 hwmon_device_unregister(hwdev);
816}
817
818/**
819 * devm_hwmon_device_register_with_groups - register w/ hwmon
820 * @dev: the parent device
821 * @name: hwmon name attribute
822 * @drvdata: driver data to attach to created device
823 * @groups: List of attribute groups to create
824 *
825 * Returns the pointer to the new device. The new device is automatically
826 * unregistered with the parent device.
827 */
828struct device *
829devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
830 void *drvdata,
831 const struct attribute_group **groups)
832{
833 struct device **ptr, *hwdev;
834
835 if (!dev)
836 return ERR_PTR(-EINVAL);
837
838 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
839 if (!ptr)
840 return ERR_PTR(-ENOMEM);
841
842 hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
843 if (IS_ERR(hwdev))
844 goto error;
845
846 *ptr = hwdev;
847 devres_add(dev, ptr);
848 return hwdev;
849
850error:
851 devres_free(ptr);
852 return hwdev;
853}
854EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
855
Guenter Roeckd5601682015-08-26 19:38:11 -0700856/**
857 * devm_hwmon_device_register_with_info - register w/ hwmon
Guenter Roeck38709452017-12-03 15:15:00 -0800858 * @dev: the parent device
859 * @name: hwmon name attribute
860 * @drvdata: driver data to attach to created device
861 * @chip: pointer to hwmon chip information
862 * @groups: pointer to list of driver specific attribute groups
Guenter Roeckd5601682015-08-26 19:38:11 -0700863 *
864 * Returns the pointer to the new device. The new device is automatically
865 * unregistered with the parent device.
866 */
867struct device *
868devm_hwmon_device_register_with_info(struct device *dev, const char *name,
869 void *drvdata,
870 const struct hwmon_chip_info *chip,
871 const struct attribute_group **groups)
872{
873 struct device **ptr, *hwdev;
874
875 if (!dev)
876 return ERR_PTR(-EINVAL);
877
878 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
879 if (!ptr)
880 return ERR_PTR(-ENOMEM);
881
882 hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
883 groups);
884 if (IS_ERR(hwdev))
885 goto error;
886
887 *ptr = hwdev;
888 devres_add(dev, ptr);
889
890 return hwdev;
891
892error:
893 devres_free(ptr);
894 return hwdev;
895}
896EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
897
Guenter Roeck74188cb2013-07-11 20:00:12 -0700898static int devm_hwmon_match(struct device *dev, void *res, void *data)
899{
900 struct device **hwdev = res;
901
902 return *hwdev == data;
903}
904
905/**
906 * devm_hwmon_device_unregister - removes a previously registered hwmon device
907 *
908 * @dev: the parent device of the device to unregister
909 */
910void devm_hwmon_device_unregister(struct device *dev)
911{
912 WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
913}
914EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
915
Jean Delvare2958b1e2009-06-15 18:39:50 +0200916static void __init hwmon_pci_quirks(void)
917{
918#if defined CONFIG_X86 && defined CONFIG_PCI
919 struct pci_dev *sb;
920 u16 base;
921 u8 enable;
922
923 /* Open access to 0x295-0x296 on MSI MS-7031 */
924 sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
Jean Delvared6dab7d2012-12-19 22:16:59 +0100925 if (sb) {
926 if (sb->subsystem_vendor == 0x1462 && /* MSI */
927 sb->subsystem_device == 0x0031) { /* MS-7031 */
928 pci_read_config_byte(sb, 0x48, &enable);
929 pci_read_config_word(sb, 0x64, &base);
Jean Delvare2958b1e2009-06-15 18:39:50 +0200930
Jean Delvared6dab7d2012-12-19 22:16:59 +0100931 if (base == 0 && !(enable & BIT(2))) {
932 dev_info(&sb->dev,
933 "Opening wide generic port at 0x295\n");
934 pci_write_config_word(sb, 0x64, 0x295);
935 pci_write_config_byte(sb, 0x48,
936 enable | BIT(2));
937 }
Jean Delvare2958b1e2009-06-15 18:39:50 +0200938 }
Jean Delvared6dab7d2012-12-19 22:16:59 +0100939 pci_dev_put(sb);
Jean Delvare2958b1e2009-06-15 18:39:50 +0200940 }
941#endif
942}
943
Mark M. Hoffman12364412005-07-15 21:38:08 -0400944static int __init hwmon_init(void)
945{
Guenter Roeckbab22432013-07-06 13:57:23 -0700946 int err;
947
Jean Delvare2958b1e2009-06-15 18:39:50 +0200948 hwmon_pci_quirks();
949
Guenter Roeckbab22432013-07-06 13:57:23 -0700950 err = class_register(&hwmon_class);
951 if (err) {
952 pr_err("couldn't register hwmon sysfs class\n");
953 return err;
Mark M. Hoffman12364412005-07-15 21:38:08 -0400954 }
955 return 0;
956}
957
958static void __exit hwmon_exit(void)
959{
Guenter Roeckbab22432013-07-06 13:57:23 -0700960 class_unregister(&hwmon_class);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400961}
962
David Brownell37f54ee2007-02-14 21:15:04 +0100963subsys_initcall(hwmon_init);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400964module_exit(hwmon_exit);
965
Mark M. Hoffman12364412005-07-15 21:38:08 -0400966MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
967MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
968MODULE_LICENSE("GPL");
969