blob: e694c46ff039cce3c38cfee073d92d0e1b0f4b40 [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
Nicolin Chen61b8ab22018-10-09 14:42:19 -070027#define CREATE_TRACE_POINTS
28#include <trace/events/hwmon.h>
29
Mark M. Hoffman12364412005-07-15 21:38:08 -040030#define HWMON_ID_PREFIX "hwmon"
31#define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
32
Guenter Roeckbab22432013-07-06 13:57:23 -070033struct hwmon_device {
34 const char *name;
35 struct device dev;
Guenter Roeckd5601682015-08-26 19:38:11 -070036 const struct hwmon_chip_info *chip;
37
38 struct attribute_group group;
39 const struct attribute_group **groups;
Guenter Roeckbab22432013-07-06 13:57:23 -070040};
Guenter Roeckd5601682015-08-26 19:38:11 -070041
Guenter Roeckbab22432013-07-06 13:57:23 -070042#define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
43
Guenter Roeck3a412d52016-10-16 10:52:04 -070044#define MAX_SYSFS_ATTR_NAME_LENGTH 32
45
Guenter Roeckd5601682015-08-26 19:38:11 -070046struct hwmon_device_attribute {
47 struct device_attribute dev_attr;
48 const struct hwmon_ops *ops;
49 enum hwmon_sensor_types type;
50 u32 attr;
51 int index;
Guenter Roeck3a412d52016-10-16 10:52:04 -070052 char name[MAX_SYSFS_ATTR_NAME_LENGTH];
Guenter Roeckd5601682015-08-26 19:38:11 -070053};
54
55#define to_hwmon_attr(d) \
56 container_of(d, struct hwmon_device_attribute, dev_attr)
57
58/*
59 * Thermal zone information
60 * In addition to the reference to the hwmon device,
61 * also provides the sensor index.
62 */
63struct hwmon_thermal_data {
64 struct hwmon_device *hwdev; /* Reference to hwmon device */
65 int index; /* sensor index */
66};
67
Guenter Roeckbab22432013-07-06 13:57:23 -070068static ssize_t
Julia Lawall2ab0c6c2016-12-22 13:04:45 +010069name_show(struct device *dev, struct device_attribute *attr, char *buf)
Guenter Roeckbab22432013-07-06 13:57:23 -070070{
71 return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
72}
Julia Lawall2ab0c6c2016-12-22 13:04:45 +010073static DEVICE_ATTR_RO(name);
Guenter Roeckbab22432013-07-06 13:57:23 -070074
75static struct attribute *hwmon_dev_attrs[] = {
76 &dev_attr_name.attr,
77 NULL
78};
79
80static umode_t hwmon_dev_name_is_visible(struct kobject *kobj,
81 struct attribute *attr, int n)
82{
83 struct device *dev = container_of(kobj, struct device, kobj);
84
85 if (to_hwmon_device(dev)->name == NULL)
86 return 0;
87
88 return attr->mode;
89}
90
Arvind Yadav524703ac2017-07-05 10:31:05 +053091static const struct attribute_group hwmon_dev_attr_group = {
Guenter Roeckbab22432013-07-06 13:57:23 -070092 .attrs = hwmon_dev_attrs,
93 .is_visible = hwmon_dev_name_is_visible,
94};
95
96static const struct attribute_group *hwmon_dev_attr_groups[] = {
97 &hwmon_dev_attr_group,
98 NULL
99};
100
101static void hwmon_dev_release(struct device *dev)
102{
103 kfree(to_hwmon_device(dev));
104}
105
106static struct class hwmon_class = {
107 .name = "hwmon",
108 .owner = THIS_MODULE,
109 .dev_groups = hwmon_dev_attr_groups,
110 .dev_release = hwmon_dev_release,
111};
Mark M. Hoffman12364412005-07-15 21:38:08 -0400112
Jonathan Cameron4ca5f462011-10-31 17:10:09 -0700113static DEFINE_IDA(hwmon_ida);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400114
Guenter Roeckd5601682015-08-26 19:38:11 -0700115/* Thermal zone handling */
116
Guenter Roeck86430c12016-08-12 06:28:15 -0700117/*
118 * The complex conditional is necessary to avoid a cyclic dependency
119 * between hwmon and thermal_sys modules.
120 */
Daniel Lezcanof3735332019-04-02 18:12:50 +0200121#ifdef CONFIG_THERMAL_OF
Guenter Roeckd5601682015-08-26 19:38:11 -0700122static int hwmon_thermal_get_temp(void *data, int *temp)
123{
124 struct hwmon_thermal_data *tdata = data;
125 struct hwmon_device *hwdev = tdata->hwdev;
126 int ret;
127 long t;
128
129 ret = hwdev->chip->ops->read(&hwdev->dev, hwmon_temp, hwmon_temp_input,
130 tdata->index, &t);
131 if (ret < 0)
132 return ret;
133
134 *temp = t;
135
136 return 0;
137}
138
Julia Lawallc9920652017-08-08 17:09:02 +0200139static const struct thermal_zone_of_device_ops hwmon_thermal_ops = {
Guenter Roeckd5601682015-08-26 19:38:11 -0700140 .get_temp = hwmon_thermal_get_temp,
141};
142
143static int hwmon_thermal_add_sensor(struct device *dev,
144 struct hwmon_device *hwdev, int index)
145{
146 struct hwmon_thermal_data *tdata;
Linus Walleij47c332d2017-12-05 09:36:14 +0100147 struct thermal_zone_device *tzd;
Guenter Roeckd5601682015-08-26 19:38:11 -0700148
149 tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
150 if (!tdata)
151 return -ENOMEM;
152
153 tdata->hwdev = hwdev;
154 tdata->index = index;
155
Linus Walleij47c332d2017-12-05 09:36:14 +0100156 tzd = devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
157 &hwmon_thermal_ops);
158 /*
159 * If CONFIG_THERMAL_OF is disabled, this returns -ENODEV,
160 * so ignore that error but forward any other error.
161 */
162 if (IS_ERR(tzd) && (PTR_ERR(tzd) != -ENODEV))
163 return PTR_ERR(tzd);
Guenter Roeckd5601682015-08-26 19:38:11 -0700164
165 return 0;
166}
167#else
168static int hwmon_thermal_add_sensor(struct device *dev,
169 struct hwmon_device *hwdev, int index)
170{
171 return 0;
172}
Guenter Roeck86430c12016-08-12 06:28:15 -0700173#endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
Guenter Roeckd5601682015-08-26 19:38:11 -0700174
Nicolin Chen61b8ab22018-10-09 14:42:19 -0700175static int hwmon_attr_base(enum hwmon_sensor_types type)
176{
177 if (type == hwmon_in)
178 return 0;
179 return 1;
180}
181
Guenter Roeckd5601682015-08-26 19:38:11 -0700182/* sysfs attribute management */
183
184static ssize_t hwmon_attr_show(struct device *dev,
185 struct device_attribute *devattr, char *buf)
186{
187 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
188 long val;
189 int ret;
190
191 ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
192 &val);
193 if (ret < 0)
194 return ret;
195
Nicolin Chen61b8ab22018-10-09 14:42:19 -0700196 trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type),
197 hattr->name, val);
198
Guenter Roeckd5601682015-08-26 19:38:11 -0700199 return sprintf(buf, "%ld\n", val);
200}
201
Guenter Roecke159ab52016-08-07 20:51:25 -0700202static ssize_t hwmon_attr_show_string(struct device *dev,
203 struct device_attribute *devattr,
204 char *buf)
205{
206 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
Nicolin Chen61b8ab22018-10-09 14:42:19 -0700207 enum hwmon_sensor_types type = hattr->type;
Jean Delvare5ba6bcb2017-03-07 06:15:15 -0800208 const char *s;
Guenter Roecke159ab52016-08-07 20:51:25 -0700209 int ret;
210
211 ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
212 hattr->index, &s);
213 if (ret < 0)
214 return ret;
215
Nicolin Chen61b8ab22018-10-09 14:42:19 -0700216 trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type),
217 hattr->name, s);
218
Guenter Roecke159ab52016-08-07 20:51:25 -0700219 return sprintf(buf, "%s\n", s);
220}
221
Guenter Roeckd5601682015-08-26 19:38:11 -0700222static ssize_t hwmon_attr_store(struct device *dev,
223 struct device_attribute *devattr,
224 const char *buf, size_t count)
225{
226 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
227 long val;
228 int ret;
229
230 ret = kstrtol(buf, 10, &val);
231 if (ret < 0)
232 return ret;
233
234 ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
235 val);
236 if (ret < 0)
237 return ret;
238
Nicolin Chen61b8ab22018-10-09 14:42:19 -0700239 trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type),
240 hattr->name, val);
Guenter Roeckd5601682015-08-26 19:38:11 -0700241
Nicolin Chen61b8ab22018-10-09 14:42:19 -0700242 return count;
Guenter Roeckd5601682015-08-26 19:38:11 -0700243}
244
Guenter Roecke159ab52016-08-07 20:51:25 -0700245static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
246{
247 return (type == hwmon_temp && attr == hwmon_temp_label) ||
248 (type == hwmon_in && attr == hwmon_in_label) ||
249 (type == hwmon_curr && attr == hwmon_curr_label) ||
250 (type == hwmon_power && attr == hwmon_power_label) ||
251 (type == hwmon_energy && attr == hwmon_energy_label) ||
252 (type == hwmon_humidity && attr == hwmon_humidity_label) ||
253 (type == hwmon_fan && attr == hwmon_fan_label);
254}
255
Guenter Roeckd5601682015-08-26 19:38:11 -0700256static struct attribute *hwmon_genattr(struct device *dev,
257 const void *drvdata,
258 enum hwmon_sensor_types type,
259 u32 attr,
260 int index,
261 const char *template,
262 const struct hwmon_ops *ops)
263{
264 struct hwmon_device_attribute *hattr;
265 struct device_attribute *dattr;
266 struct attribute *a;
267 umode_t mode;
Rasmus Villemoes3b443de2018-10-27 00:30:59 +0200268 const char *name;
Guenter Roecke159ab52016-08-07 20:51:25 -0700269 bool is_string = is_string_attr(type, attr);
Guenter Roeckd5601682015-08-26 19:38:11 -0700270
271 /* The attribute is invisible if there is no template string */
272 if (!template)
273 return ERR_PTR(-ENOENT);
274
275 mode = ops->is_visible(drvdata, type, attr, index);
276 if (!mode)
277 return ERR_PTR(-ENOENT);
278
Guenter Roeck0d871162018-12-10 14:02:08 -0800279 if ((mode & 0444) && ((is_string && !ops->read_string) ||
Guenter Roecke159ab52016-08-07 20:51:25 -0700280 (!is_string && !ops->read)))
Guenter Roeckd5601682015-08-26 19:38:11 -0700281 return ERR_PTR(-EINVAL);
Guenter Roeck0d871162018-12-10 14:02:08 -0800282 if ((mode & 0222) && !ops->write)
Guenter Roeckd5601682015-08-26 19:38:11 -0700283 return ERR_PTR(-EINVAL);
284
Guenter Roeckd5601682015-08-26 19:38:11 -0700285 hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL);
286 if (!hattr)
287 return ERR_PTR(-ENOMEM);
288
Guenter Roeck3a412d52016-10-16 10:52:04 -0700289 if (type == hwmon_chip) {
Rasmus Villemoes3b443de2018-10-27 00:30:59 +0200290 name = template;
Guenter Roeck3a412d52016-10-16 10:52:04 -0700291 } else {
292 scnprintf(hattr->name, sizeof(hattr->name), template,
293 index + hwmon_attr_base(type));
294 name = hattr->name;
295 }
296
Guenter Roeckd5601682015-08-26 19:38:11 -0700297 hattr->type = type;
298 hattr->attr = attr;
299 hattr->index = index;
300 hattr->ops = ops;
301
302 dattr = &hattr->dev_attr;
Guenter Roecke159ab52016-08-07 20:51:25 -0700303 dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
Guenter Roeckd5601682015-08-26 19:38:11 -0700304 dattr->store = hwmon_attr_store;
305
306 a = &dattr->attr;
307 sysfs_attr_init(a);
308 a->name = name;
309 a->mode = mode;
310
311 return a;
312}
313
Guenter Roeckf4d325d2016-10-16 10:38:52 -0700314/*
315 * Chip attributes are not attribute templates but actual sysfs attributes.
316 * See hwmon_genattr() for special handling.
317 */
318static const char * const hwmon_chip_attrs[] = {
Guenter Roeckd5601682015-08-26 19:38:11 -0700319 [hwmon_chip_temp_reset_history] = "temp_reset_history",
Guenter Roeck00d616c2016-06-20 11:01:57 -0700320 [hwmon_chip_in_reset_history] = "in_reset_history",
Guenter Roeck9b269472016-06-20 11:10:33 -0700321 [hwmon_chip_curr_reset_history] = "curr_reset_history",
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700322 [hwmon_chip_power_reset_history] = "power_reset_history",
Guenter Roeckd5601682015-08-26 19:38:11 -0700323 [hwmon_chip_update_interval] = "update_interval",
324 [hwmon_chip_alarms] = "alarms",
Guenter Roeck9f009952019-04-15 13:23:48 -0700325 [hwmon_chip_samples] = "samples",
326 [hwmon_chip_curr_samples] = "curr_samples",
327 [hwmon_chip_in_samples] = "in_samples",
328 [hwmon_chip_power_samples] = "power_samples",
329 [hwmon_chip_temp_samples] = "temp_samples",
Guenter Roeckd5601682015-08-26 19:38:11 -0700330};
331
332static const char * const hwmon_temp_attr_templates[] = {
333 [hwmon_temp_input] = "temp%d_input",
334 [hwmon_temp_type] = "temp%d_type",
335 [hwmon_temp_lcrit] = "temp%d_lcrit",
336 [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
337 [hwmon_temp_min] = "temp%d_min",
338 [hwmon_temp_min_hyst] = "temp%d_min_hyst",
339 [hwmon_temp_max] = "temp%d_max",
340 [hwmon_temp_max_hyst] = "temp%d_max_hyst",
341 [hwmon_temp_crit] = "temp%d_crit",
342 [hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
343 [hwmon_temp_emergency] = "temp%d_emergency",
344 [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
345 [hwmon_temp_alarm] = "temp%d_alarm",
346 [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
347 [hwmon_temp_min_alarm] = "temp%d_min_alarm",
348 [hwmon_temp_max_alarm] = "temp%d_max_alarm",
349 [hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
350 [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
351 [hwmon_temp_fault] = "temp%d_fault",
352 [hwmon_temp_offset] = "temp%d_offset",
353 [hwmon_temp_label] = "temp%d_label",
354 [hwmon_temp_lowest] = "temp%d_lowest",
355 [hwmon_temp_highest] = "temp%d_highest",
356 [hwmon_temp_reset_history] = "temp%d_reset_history",
357};
358
Guenter Roeck00d616c2016-06-20 11:01:57 -0700359static const char * const hwmon_in_attr_templates[] = {
360 [hwmon_in_input] = "in%d_input",
361 [hwmon_in_min] = "in%d_min",
362 [hwmon_in_max] = "in%d_max",
363 [hwmon_in_lcrit] = "in%d_lcrit",
364 [hwmon_in_crit] = "in%d_crit",
365 [hwmon_in_average] = "in%d_average",
366 [hwmon_in_lowest] = "in%d_lowest",
367 [hwmon_in_highest] = "in%d_highest",
368 [hwmon_in_reset_history] = "in%d_reset_history",
369 [hwmon_in_label] = "in%d_label",
370 [hwmon_in_alarm] = "in%d_alarm",
371 [hwmon_in_min_alarm] = "in%d_min_alarm",
372 [hwmon_in_max_alarm] = "in%d_max_alarm",
373 [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
374 [hwmon_in_crit_alarm] = "in%d_crit_alarm",
Nicolin Chen68c0d692018-10-05 16:59:04 -0700375 [hwmon_in_enable] = "in%d_enable",
Guenter Roeck00d616c2016-06-20 11:01:57 -0700376};
377
Guenter Roeck9b269472016-06-20 11:10:33 -0700378static const char * const hwmon_curr_attr_templates[] = {
379 [hwmon_curr_input] = "curr%d_input",
380 [hwmon_curr_min] = "curr%d_min",
381 [hwmon_curr_max] = "curr%d_max",
382 [hwmon_curr_lcrit] = "curr%d_lcrit",
383 [hwmon_curr_crit] = "curr%d_crit",
384 [hwmon_curr_average] = "curr%d_average",
385 [hwmon_curr_lowest] = "curr%d_lowest",
386 [hwmon_curr_highest] = "curr%d_highest",
387 [hwmon_curr_reset_history] = "curr%d_reset_history",
388 [hwmon_curr_label] = "curr%d_label",
389 [hwmon_curr_alarm] = "curr%d_alarm",
390 [hwmon_curr_min_alarm] = "curr%d_min_alarm",
391 [hwmon_curr_max_alarm] = "curr%d_max_alarm",
392 [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
393 [hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
394};
395
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700396static const char * const hwmon_power_attr_templates[] = {
397 [hwmon_power_average] = "power%d_average",
398 [hwmon_power_average_interval] = "power%d_average_interval",
399 [hwmon_power_average_interval_max] = "power%d_interval_max",
400 [hwmon_power_average_interval_min] = "power%d_interval_min",
401 [hwmon_power_average_highest] = "power%d_average_highest",
402 [hwmon_power_average_lowest] = "power%d_average_lowest",
403 [hwmon_power_average_max] = "power%d_average_max",
404 [hwmon_power_average_min] = "power%d_average_min",
405 [hwmon_power_input] = "power%d_input",
406 [hwmon_power_input_highest] = "power%d_input_highest",
407 [hwmon_power_input_lowest] = "power%d_input_lowest",
408 [hwmon_power_reset_history] = "power%d_reset_history",
409 [hwmon_power_accuracy] = "power%d_accuracy",
410 [hwmon_power_cap] = "power%d_cap",
411 [hwmon_power_cap_hyst] = "power%d_cap_hyst",
412 [hwmon_power_cap_max] = "power%d_cap_max",
413 [hwmon_power_cap_min] = "power%d_cap_min",
Andrew Lunnaa7f29b2018-07-17 21:48:11 +0200414 [hwmon_power_min] = "power%d_min",
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700415 [hwmon_power_max] = "power%d_max",
Andrew Lunnaa7f29b2018-07-17 21:48:11 +0200416 [hwmon_power_lcrit] = "power%d_lcrit",
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700417 [hwmon_power_crit] = "power%d_crit",
418 [hwmon_power_label] = "power%d_label",
419 [hwmon_power_alarm] = "power%d_alarm",
420 [hwmon_power_cap_alarm] = "power%d_cap_alarm",
Andrew Lunnaa7f29b2018-07-17 21:48:11 +0200421 [hwmon_power_min_alarm] = "power%d_min_alarm",
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700422 [hwmon_power_max_alarm] = "power%d_max_alarm",
Andrew Lunnaa7f29b2018-07-17 21:48:11 +0200423 [hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700424 [hwmon_power_crit_alarm] = "power%d_crit_alarm",
425};
426
Guenter Roeck6bfcca42016-06-20 11:38:37 -0700427static const char * const hwmon_energy_attr_templates[] = {
428 [hwmon_energy_input] = "energy%d_input",
429 [hwmon_energy_label] = "energy%d_label",
430};
431
432static const char * const hwmon_humidity_attr_templates[] = {
433 [hwmon_humidity_input] = "humidity%d_input",
434 [hwmon_humidity_label] = "humidity%d_label",
435 [hwmon_humidity_min] = "humidity%d_min",
436 [hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
437 [hwmon_humidity_max] = "humidity%d_max",
438 [hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
439 [hwmon_humidity_alarm] = "humidity%d_alarm",
440 [hwmon_humidity_fault] = "humidity%d_fault",
441};
442
Guenter Roeck8faee732016-06-25 19:52:13 -0700443static const char * const hwmon_fan_attr_templates[] = {
444 [hwmon_fan_input] = "fan%d_input",
445 [hwmon_fan_label] = "fan%d_label",
446 [hwmon_fan_min] = "fan%d_min",
447 [hwmon_fan_max] = "fan%d_max",
448 [hwmon_fan_div] = "fan%d_div",
449 [hwmon_fan_pulses] = "fan%d_pulses",
450 [hwmon_fan_target] = "fan%d_target",
451 [hwmon_fan_alarm] = "fan%d_alarm",
452 [hwmon_fan_min_alarm] = "fan%d_min_alarm",
453 [hwmon_fan_max_alarm] = "fan%d_max_alarm",
454 [hwmon_fan_fault] = "fan%d_fault",
455};
456
Guenter Roeckf9f7bb32016-06-26 12:20:46 -0700457static const char * const hwmon_pwm_attr_templates[] = {
458 [hwmon_pwm_input] = "pwm%d",
459 [hwmon_pwm_enable] = "pwm%d_enable",
460 [hwmon_pwm_mode] = "pwm%d_mode",
461 [hwmon_pwm_freq] = "pwm%d_freq",
462};
463
Guenter Roeckd5601682015-08-26 19:38:11 -0700464static const char * const *__templates[] = {
Guenter Roeckf4d325d2016-10-16 10:38:52 -0700465 [hwmon_chip] = hwmon_chip_attrs,
Guenter Roeckd5601682015-08-26 19:38:11 -0700466 [hwmon_temp] = hwmon_temp_attr_templates,
Guenter Roeck00d616c2016-06-20 11:01:57 -0700467 [hwmon_in] = hwmon_in_attr_templates,
Guenter Roeck9b269472016-06-20 11:10:33 -0700468 [hwmon_curr] = hwmon_curr_attr_templates,
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700469 [hwmon_power] = hwmon_power_attr_templates,
Guenter Roeck6bfcca42016-06-20 11:38:37 -0700470 [hwmon_energy] = hwmon_energy_attr_templates,
471 [hwmon_humidity] = hwmon_humidity_attr_templates,
Guenter Roeck8faee732016-06-25 19:52:13 -0700472 [hwmon_fan] = hwmon_fan_attr_templates,
Guenter Roeckf9f7bb32016-06-26 12:20:46 -0700473 [hwmon_pwm] = hwmon_pwm_attr_templates,
Guenter Roeckd5601682015-08-26 19:38:11 -0700474};
475
476static const int __templates_size[] = {
Guenter Roeckf4d325d2016-10-16 10:38:52 -0700477 [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
Guenter Roeckd5601682015-08-26 19:38:11 -0700478 [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
Guenter Roeck00d616c2016-06-20 11:01:57 -0700479 [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
Guenter Roeck9b269472016-06-20 11:10:33 -0700480 [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700481 [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
Guenter Roeck6bfcca42016-06-20 11:38:37 -0700482 [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
483 [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
Guenter Roeck8faee732016-06-25 19:52:13 -0700484 [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
Guenter Roeckf9f7bb32016-06-26 12:20:46 -0700485 [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
Guenter Roeckd5601682015-08-26 19:38:11 -0700486};
487
488static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
489{
490 int i, n;
491
492 for (i = n = 0; info->config[i]; i++)
493 n += hweight32(info->config[i]);
494
495 return n;
496}
497
498static int hwmon_genattrs(struct device *dev,
499 const void *drvdata,
500 struct attribute **attrs,
501 const struct hwmon_ops *ops,
502 const struct hwmon_channel_info *info)
503{
504 const char * const *templates;
505 int template_size;
506 int i, aindex = 0;
507
508 if (info->type >= ARRAY_SIZE(__templates))
509 return -EINVAL;
510
511 templates = __templates[info->type];
512 template_size = __templates_size[info->type];
513
514 for (i = 0; info->config[i]; i++) {
515 u32 attr_mask = info->config[i];
516 u32 attr;
517
518 while (attr_mask) {
519 struct attribute *a;
520
521 attr = __ffs(attr_mask);
522 attr_mask &= ~BIT(attr);
523 if (attr >= template_size)
524 return -EINVAL;
525 a = hwmon_genattr(dev, drvdata, info->type, attr, i,
526 templates[attr], ops);
527 if (IS_ERR(a)) {
528 if (PTR_ERR(a) != -ENOENT)
529 return PTR_ERR(a);
530 continue;
531 }
532 attrs[aindex++] = a;
533 }
534 }
535 return aindex;
536}
537
538static struct attribute **
539__hwmon_create_attrs(struct device *dev, const void *drvdata,
540 const struct hwmon_chip_info *chip)
541{
542 int ret, i, aindex = 0, nattrs = 0;
543 struct attribute **attrs;
544
545 for (i = 0; chip->info[i]; i++)
546 nattrs += hwmon_num_channel_attrs(chip->info[i]);
547
548 if (nattrs == 0)
549 return ERR_PTR(-EINVAL);
550
551 attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL);
552 if (!attrs)
553 return ERR_PTR(-ENOMEM);
554
555 for (i = 0; chip->info[i]; i++) {
556 ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops,
557 chip->info[i]);
558 if (ret < 0)
559 return ERR_PTR(ret);
560 aindex += ret;
561 }
562
563 return attrs;
564}
565
566static struct device *
567__hwmon_device_register(struct device *dev, const char *name, void *drvdata,
568 const struct hwmon_chip_info *chip,
569 const struct attribute_group **groups)
570{
571 struct hwmon_device *hwdev;
572 struct device *hdev;
573 int i, j, err, id;
574
Guenter Roeck74d3b642017-01-27 19:35:57 -0800575 /* Complain about invalid characters in hwmon name attribute */
Guenter Roeckd5601682015-08-26 19:38:11 -0700576 if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
Guenter Roeck74d3b642017-01-27 19:35:57 -0800577 dev_warn(dev,
578 "hwmon: '%s' is not a valid name attribute, please fix\n",
579 name);
Guenter Roeckd5601682015-08-26 19:38:11 -0700580
581 id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
582 if (id < 0)
583 return ERR_PTR(id);
584
585 hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
586 if (hwdev == NULL) {
587 err = -ENOMEM;
588 goto ida_remove;
589 }
590
591 hdev = &hwdev->dev;
592
Guenter Roeck239552f2016-10-16 17:06:20 -0700593 if (chip) {
Guenter Roeckd5601682015-08-26 19:38:11 -0700594 struct attribute **attrs;
Guenter Roeckb2a4cc32016-10-16 17:11:52 -0700595 int ngroups = 2; /* terminating NULL plus &hwdev->groups */
Guenter Roeckd5601682015-08-26 19:38:11 -0700596
597 if (groups)
598 for (i = 0; groups[i]; i++)
599 ngroups++;
600
601 hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups),
602 GFP_KERNEL);
Colin Ian King38d8ed62016-10-23 21:56:08 +0100603 if (!hwdev->groups) {
604 err = -ENOMEM;
605 goto free_hwmon;
606 }
Guenter Roeckd5601682015-08-26 19:38:11 -0700607
608 attrs = __hwmon_create_attrs(dev, drvdata, chip);
609 if (IS_ERR(attrs)) {
610 err = PTR_ERR(attrs);
611 goto free_hwmon;
612 }
613
614 hwdev->group.attrs = attrs;
615 ngroups = 0;
616 hwdev->groups[ngroups++] = &hwdev->group;
617
618 if (groups) {
619 for (i = 0; groups[i]; i++)
620 hwdev->groups[ngroups++] = groups[i];
621 }
622
623 hdev->groups = hwdev->groups;
624 } else {
625 hdev->groups = groups;
626 }
627
628 hwdev->name = name;
629 hdev->class = &hwmon_class;
630 hdev->parent = dev;
631 hdev->of_node = dev ? dev->of_node : NULL;
632 hwdev->chip = chip;
633 dev_set_drvdata(hdev, drvdata);
634 dev_set_name(hdev, HWMON_ID_FORMAT, id);
635 err = device_register(hdev);
636 if (err)
637 goto free_hwmon;
638
Guenter Roeck319fe152017-01-31 03:56:08 -0800639 if (dev && chip && chip->ops->read &&
Guenter Roeckd5601682015-08-26 19:38:11 -0700640 chip->info[0]->type == hwmon_chip &&
641 (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
642 const struct hwmon_channel_info **info = chip->info;
643
644 for (i = 1; info[i]; i++) {
645 if (info[i]->type != hwmon_temp)
646 continue;
647
648 for (j = 0; info[i]->config[j]; j++) {
649 if (!chip->ops->is_visible(drvdata, hwmon_temp,
650 hwmon_temp_input, j))
651 continue;
Linus Walleij47c332d2017-12-05 09:36:14 +0100652 if (info[i]->config[j] & HWMON_T_INPUT) {
653 err = hwmon_thermal_add_sensor(dev,
654 hwdev, j);
Dmitry Osipenko74e35122018-10-24 22:37:13 +0300655 if (err) {
656 device_unregister(hdev);
657 goto ida_remove;
658 }
Linus Walleij47c332d2017-12-05 09:36:14 +0100659 }
Guenter Roeckd5601682015-08-26 19:38:11 -0700660 }
661 }
662 }
663
664 return hdev;
665
666free_hwmon:
667 kfree(hwdev);
668ida_remove:
669 ida_simple_remove(&hwmon_ida, id);
670 return ERR_PTR(err);
671}
672
Mark M. Hoffman12364412005-07-15 21:38:08 -0400673/**
Guenter Roeckbab22432013-07-06 13:57:23 -0700674 * hwmon_device_register_with_groups - register w/ hwmon
675 * @dev: the parent device
676 * @name: hwmon name attribute
677 * @drvdata: driver data to attach to created device
678 * @groups: List of attribute groups to create
679 *
680 * hwmon_device_unregister() must be called when the device is no
681 * longer needed.
682 *
683 * Returns the pointer to the new device.
684 */
685struct device *
686hwmon_device_register_with_groups(struct device *dev, const char *name,
687 void *drvdata,
688 const struct attribute_group **groups)
689{
Guenter Roeck83538632017-01-24 06:32:57 -0800690 if (!name)
691 return ERR_PTR(-EINVAL);
692
Guenter Roeckd5601682015-08-26 19:38:11 -0700693 return __hwmon_device_register(dev, name, drvdata, NULL, groups);
Guenter Roeckbab22432013-07-06 13:57:23 -0700694}
695EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
696
697/**
Guenter Roeckd5601682015-08-26 19:38:11 -0700698 * hwmon_device_register_with_info - register w/ hwmon
699 * @dev: the parent device
700 * @name: hwmon name attribute
701 * @drvdata: driver data to attach to created device
Guenter Roeck38709452017-12-03 15:15:00 -0800702 * @chip: pointer to hwmon chip information
Guenter Roeck848ba0a2016-10-16 17:20:43 -0700703 * @extra_groups: pointer to list of additional non-standard attribute groups
Guenter Roeckd5601682015-08-26 19:38:11 -0700704 *
705 * hwmon_device_unregister() must be called when the device is no
706 * longer needed.
707 *
708 * Returns the pointer to the new device.
709 */
710struct device *
711hwmon_device_register_with_info(struct device *dev, const char *name,
712 void *drvdata,
713 const struct hwmon_chip_info *chip,
Guenter Roeck848ba0a2016-10-16 17:20:43 -0700714 const struct attribute_group **extra_groups)
Guenter Roeckd5601682015-08-26 19:38:11 -0700715{
Guenter Roeck83538632017-01-24 06:32:57 -0800716 if (!name)
717 return ERR_PTR(-EINVAL);
718
Guenter Roeck239552f2016-10-16 17:06:20 -0700719 if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info))
Guenter Roeckd5601682015-08-26 19:38:11 -0700720 return ERR_PTR(-EINVAL);
721
Lucas Magasweran59df4f42018-05-08 04:43:33 -0700722 if (chip && !dev)
723 return ERR_PTR(-EINVAL);
724
Guenter Roeck848ba0a2016-10-16 17:20:43 -0700725 return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
Guenter Roeckd5601682015-08-26 19:38:11 -0700726}
727EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
728
729/**
Tony Jones1beeffe2007-08-20 13:46:20 -0700730 * hwmon_device_register - register w/ hwmon
Mark M. Hoffman12364412005-07-15 21:38:08 -0400731 * @dev: the device to register
732 *
Tony Jones1beeffe2007-08-20 13:46:20 -0700733 * hwmon_device_unregister() must be called when the device is no
Mark M. Hoffman12364412005-07-15 21:38:08 -0400734 * longer needed.
735 *
Tony Jones1beeffe2007-08-20 13:46:20 -0700736 * Returns the pointer to the new device.
Mark M. Hoffman12364412005-07-15 21:38:08 -0400737 */
Tony Jones1beeffe2007-08-20 13:46:20 -0700738struct device *hwmon_device_register(struct device *dev)
Mark M. Hoffman12364412005-07-15 21:38:08 -0400739{
Guenter Roeckaf1bd362016-10-16 11:31:08 -0700740 dev_warn(dev,
741 "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
742
Guenter Roeck83538632017-01-24 06:32:57 -0800743 return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400744}
Frans Meulenbroeks839a9ee2012-01-08 19:34:14 +0100745EXPORT_SYMBOL_GPL(hwmon_device_register);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400746
747/**
748 * hwmon_device_unregister - removes the previously registered class device
749 *
Tony Jones1beeffe2007-08-20 13:46:20 -0700750 * @dev: the class device to destroy
Mark M. Hoffman12364412005-07-15 21:38:08 -0400751 */
Tony Jones1beeffe2007-08-20 13:46:20 -0700752void hwmon_device_unregister(struct device *dev)
Mark M. Hoffman12364412005-07-15 21:38:08 -0400753{
754 int id;
755
Kay Sievers739cf3a2009-01-06 10:44:41 -0800756 if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
Tony Jones1beeffe2007-08-20 13:46:20 -0700757 device_unregister(dev);
Jonathan Cameron4ca5f462011-10-31 17:10:09 -0700758 ida_simple_remove(&hwmon_ida, id);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400759 } else
Tony Jones1beeffe2007-08-20 13:46:20 -0700760 dev_dbg(dev->parent,
Mark M. Hoffman12364412005-07-15 21:38:08 -0400761 "hwmon_device_unregister() failed: bad class ID!\n");
762}
Frans Meulenbroeks839a9ee2012-01-08 19:34:14 +0100763EXPORT_SYMBOL_GPL(hwmon_device_unregister);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400764
Guenter Roeck74188cb2013-07-11 20:00:12 -0700765static void devm_hwmon_release(struct device *dev, void *res)
766{
767 struct device *hwdev = *(struct device **)res;
768
769 hwmon_device_unregister(hwdev);
770}
771
772/**
773 * devm_hwmon_device_register_with_groups - register w/ hwmon
774 * @dev: the parent device
775 * @name: hwmon name attribute
776 * @drvdata: driver data to attach to created device
777 * @groups: List of attribute groups to create
778 *
779 * Returns the pointer to the new device. The new device is automatically
780 * unregistered with the parent device.
781 */
782struct device *
783devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
784 void *drvdata,
785 const struct attribute_group **groups)
786{
787 struct device **ptr, *hwdev;
788
789 if (!dev)
790 return ERR_PTR(-EINVAL);
791
792 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
793 if (!ptr)
794 return ERR_PTR(-ENOMEM);
795
796 hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
797 if (IS_ERR(hwdev))
798 goto error;
799
800 *ptr = hwdev;
801 devres_add(dev, ptr);
802 return hwdev;
803
804error:
805 devres_free(ptr);
806 return hwdev;
807}
808EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
809
Guenter Roeckd5601682015-08-26 19:38:11 -0700810/**
811 * devm_hwmon_device_register_with_info - register w/ hwmon
Guenter Roeck38709452017-12-03 15:15:00 -0800812 * @dev: the parent device
813 * @name: hwmon name attribute
814 * @drvdata: driver data to attach to created device
815 * @chip: pointer to hwmon chip information
816 * @groups: pointer to list of driver specific attribute groups
Guenter Roeckd5601682015-08-26 19:38:11 -0700817 *
818 * Returns the pointer to the new device. The new device is automatically
819 * unregistered with the parent device.
820 */
821struct device *
822devm_hwmon_device_register_with_info(struct device *dev, const char *name,
823 void *drvdata,
824 const struct hwmon_chip_info *chip,
825 const struct attribute_group **groups)
826{
827 struct device **ptr, *hwdev;
828
829 if (!dev)
830 return ERR_PTR(-EINVAL);
831
832 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
833 if (!ptr)
834 return ERR_PTR(-ENOMEM);
835
836 hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
837 groups);
838 if (IS_ERR(hwdev))
839 goto error;
840
841 *ptr = hwdev;
842 devres_add(dev, ptr);
843
844 return hwdev;
845
846error:
847 devres_free(ptr);
848 return hwdev;
849}
850EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
851
Guenter Roeck74188cb2013-07-11 20:00:12 -0700852static int devm_hwmon_match(struct device *dev, void *res, void *data)
853{
854 struct device **hwdev = res;
855
856 return *hwdev == data;
857}
858
859/**
860 * devm_hwmon_device_unregister - removes a previously registered hwmon device
861 *
862 * @dev: the parent device of the device to unregister
863 */
864void devm_hwmon_device_unregister(struct device *dev)
865{
866 WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
867}
868EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
869
Jean Delvare2958b1e2009-06-15 18:39:50 +0200870static void __init hwmon_pci_quirks(void)
871{
872#if defined CONFIG_X86 && defined CONFIG_PCI
873 struct pci_dev *sb;
874 u16 base;
875 u8 enable;
876
877 /* Open access to 0x295-0x296 on MSI MS-7031 */
878 sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
Jean Delvared6dab7d2012-12-19 22:16:59 +0100879 if (sb) {
880 if (sb->subsystem_vendor == 0x1462 && /* MSI */
881 sb->subsystem_device == 0x0031) { /* MS-7031 */
882 pci_read_config_byte(sb, 0x48, &enable);
883 pci_read_config_word(sb, 0x64, &base);
Jean Delvare2958b1e2009-06-15 18:39:50 +0200884
Jean Delvared6dab7d2012-12-19 22:16:59 +0100885 if (base == 0 && !(enable & BIT(2))) {
886 dev_info(&sb->dev,
887 "Opening wide generic port at 0x295\n");
888 pci_write_config_word(sb, 0x64, 0x295);
889 pci_write_config_byte(sb, 0x48,
890 enable | BIT(2));
891 }
Jean Delvare2958b1e2009-06-15 18:39:50 +0200892 }
Jean Delvared6dab7d2012-12-19 22:16:59 +0100893 pci_dev_put(sb);
Jean Delvare2958b1e2009-06-15 18:39:50 +0200894 }
895#endif
896}
897
Mark M. Hoffman12364412005-07-15 21:38:08 -0400898static int __init hwmon_init(void)
899{
Guenter Roeckbab22432013-07-06 13:57:23 -0700900 int err;
901
Jean Delvare2958b1e2009-06-15 18:39:50 +0200902 hwmon_pci_quirks();
903
Guenter Roeckbab22432013-07-06 13:57:23 -0700904 err = class_register(&hwmon_class);
905 if (err) {
906 pr_err("couldn't register hwmon sysfs class\n");
907 return err;
Mark M. Hoffman12364412005-07-15 21:38:08 -0400908 }
909 return 0;
910}
911
912static void __exit hwmon_exit(void)
913{
Guenter Roeckbab22432013-07-06 13:57:23 -0700914 class_unregister(&hwmon_class);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400915}
916
David Brownell37f54ee2007-02-14 21:15:04 +0100917subsys_initcall(hwmon_init);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400918module_exit(hwmon_exit);
919
Mark M. Hoffman12364412005-07-15 21:38:08 -0400920MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
921MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
922MODULE_LICENSE("GPL");
923