blob: e0a1a118514f92ec3df98c03fba2861307a33bd7 [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 Roeckcb4412e2016-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 Roeckcb4412e2016-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 Roeck974185ee2020-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 Roeck974185ee2020-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
67show_name(struct device *dev, struct device_attribute *attr, char *buf)
68{
69 return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
70}
71static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
72
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
89static struct attribute_group hwmon_dev_attr_group = {
90 .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 Roeck974185ee2020-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 Roeck974185ee2020-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 */
137#if IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF) && \
138 (!defined(CONFIG_THERMAL_HWMON) || \
139 !(defined(MODULE) && IS_MODULE(CONFIG_THERMAL)))
Guenter Roeckd5601682015-08-26 19:38:11 -0700140static int hwmon_thermal_get_temp(void *data, int *temp)
141{
142 struct hwmon_thermal_data *tdata = data;
Guenter Roeck974185ee2020-01-16 10:44:17 -0800143 struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
Guenter Roeckd5601682015-08-26 19:38:11 -0700144 int ret;
145 long t;
146
Guenter Roeck974185ee2020-01-16 10:44:17 -0800147 ret = hwdev->chip->ops->read(tdata->dev, hwmon_temp, hwmon_temp_input,
Guenter Roeckd5601682015-08-26 19:38:11 -0700148 tdata->index, &t);
149 if (ret < 0)
150 return ret;
151
152 *temp = t;
153
154 return 0;
155}
156
157static struct thermal_zone_of_device_ops hwmon_thermal_ops = {
158 .get_temp = hwmon_thermal_get_temp,
159};
160
Guenter Roeck974185ee2020-01-16 10:44:17 -0800161static int hwmon_thermal_add_sensor(struct device *dev, int index)
Guenter Roeckd5601682015-08-26 19:38:11 -0700162{
163 struct hwmon_thermal_data *tdata;
Linus Walleija34d0232017-12-05 09:36:14 +0100164 struct thermal_zone_device *tzd;
Guenter Roeckd5601682015-08-26 19:38:11 -0700165
166 tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
167 if (!tdata)
168 return -ENOMEM;
169
Guenter Roeck974185ee2020-01-16 10:44:17 -0800170 tdata->dev = dev;
Guenter Roeckd5601682015-08-26 19:38:11 -0700171 tdata->index = index;
172
Guenter Roeck974185ee2020-01-16 10:44:17 -0800173 tzd = devm_thermal_zone_of_sensor_register(dev, index, tdata,
Linus Walleija34d0232017-12-05 09:36:14 +0100174 &hwmon_thermal_ops);
175 /*
176 * If CONFIG_THERMAL_OF is disabled, this returns -ENODEV,
177 * so ignore that error but forward any other error.
178 */
179 if (IS_ERR(tzd) && (PTR_ERR(tzd) != -ENODEV))
180 return PTR_ERR(tzd);
Guenter Roeckd5601682015-08-26 19:38:11 -0700181
182 return 0;
183}
184#else
Guenter Roeck974185ee2020-01-16 10:44:17 -0800185static int hwmon_thermal_add_sensor(struct device *dev, int index)
Guenter Roeckd5601682015-08-26 19:38:11 -0700186{
187 return 0;
188}
Guenter Roeck86430c12016-08-12 06:28:15 -0700189#endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
Guenter Roeckd5601682015-08-26 19:38:11 -0700190
191/* sysfs attribute management */
192
193static ssize_t hwmon_attr_show(struct device *dev,
194 struct device_attribute *devattr, char *buf)
195{
196 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
197 long val;
198 int ret;
199
200 ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
201 &val);
202 if (ret < 0)
203 return ret;
204
205 return sprintf(buf, "%ld\n", val);
206}
207
208static ssize_t hwmon_attr_store(struct device *dev,
209 struct device_attribute *devattr,
210 const char *buf, size_t count)
211{
212 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
213 long val;
214 int ret;
215
216 ret = kstrtol(buf, 10, &val);
217 if (ret < 0)
218 return ret;
219
220 ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
221 val);
222 if (ret < 0)
223 return ret;
224
225 return count;
226}
227
228static int hwmon_attr_base(enum hwmon_sensor_types type)
229{
230 if (type == hwmon_in)
231 return 0;
232 return 1;
233}
234
Guenter Roeck974185ee2020-01-16 10:44:17 -0800235static struct attribute *hwmon_genattr(const void *drvdata,
Guenter Roeckd5601682015-08-26 19:38:11 -0700236 enum hwmon_sensor_types type,
237 u32 attr,
238 int index,
239 const char *template,
240 const struct hwmon_ops *ops)
241{
242 struct hwmon_device_attribute *hattr;
243 struct device_attribute *dattr;
244 struct attribute *a;
245 umode_t mode;
246 char *name;
247
248 /* The attribute is invisible if there is no template string */
249 if (!template)
250 return ERR_PTR(-ENOENT);
251
252 mode = ops->is_visible(drvdata, type, attr, index);
253 if (!mode)
254 return ERR_PTR(-ENOENT);
255
256 if ((mode & S_IRUGO) && !ops->read)
257 return ERR_PTR(-EINVAL);
258 if ((mode & S_IWUGO) && !ops->write)
259 return ERR_PTR(-EINVAL);
260
Guenter Roeck974185ee2020-01-16 10:44:17 -0800261 hattr = kzalloc(sizeof(*hattr), GFP_KERNEL);
Guenter Roeckd5601682015-08-26 19:38:11 -0700262 if (!hattr)
263 return ERR_PTR(-ENOMEM);
264
Guenter Roeckcb4412e2016-10-16 10:52:04 -0700265 if (type == hwmon_chip) {
266 name = (char *)template;
267 } else {
268 scnprintf(hattr->name, sizeof(hattr->name), template,
269 index + hwmon_attr_base(type));
270 name = hattr->name;
271 }
272
Guenter Roeckd5601682015-08-26 19:38:11 -0700273 hattr->type = type;
274 hattr->attr = attr;
275 hattr->index = index;
276 hattr->ops = ops;
277
278 dattr = &hattr->dev_attr;
279 dattr->show = hwmon_attr_show;
280 dattr->store = hwmon_attr_store;
281
282 a = &dattr->attr;
283 sysfs_attr_init(a);
284 a->name = name;
285 a->mode = mode;
286
287 return a;
288}
289
290static const char * const hwmon_chip_attr_templates[] = {
291 [hwmon_chip_temp_reset_history] = "temp_reset_history",
Guenter Roeck00d616c2016-06-20 11:01:57 -0700292 [hwmon_chip_in_reset_history] = "in_reset_history",
Guenter Roeck9b269472016-06-20 11:10:33 -0700293 [hwmon_chip_curr_reset_history] = "curr_reset_history",
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700294 [hwmon_chip_power_reset_history] = "power_reset_history",
Guenter Roeckd5601682015-08-26 19:38:11 -0700295 [hwmon_chip_update_interval] = "update_interval",
296 [hwmon_chip_alarms] = "alarms",
297};
298
299static const char * const hwmon_temp_attr_templates[] = {
300 [hwmon_temp_input] = "temp%d_input",
301 [hwmon_temp_type] = "temp%d_type",
302 [hwmon_temp_lcrit] = "temp%d_lcrit",
303 [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
304 [hwmon_temp_min] = "temp%d_min",
305 [hwmon_temp_min_hyst] = "temp%d_min_hyst",
306 [hwmon_temp_max] = "temp%d_max",
307 [hwmon_temp_max_hyst] = "temp%d_max_hyst",
308 [hwmon_temp_crit] = "temp%d_crit",
309 [hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
310 [hwmon_temp_emergency] = "temp%d_emergency",
311 [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
312 [hwmon_temp_alarm] = "temp%d_alarm",
313 [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
314 [hwmon_temp_min_alarm] = "temp%d_min_alarm",
315 [hwmon_temp_max_alarm] = "temp%d_max_alarm",
316 [hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
317 [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
318 [hwmon_temp_fault] = "temp%d_fault",
319 [hwmon_temp_offset] = "temp%d_offset",
320 [hwmon_temp_label] = "temp%d_label",
321 [hwmon_temp_lowest] = "temp%d_lowest",
322 [hwmon_temp_highest] = "temp%d_highest",
323 [hwmon_temp_reset_history] = "temp%d_reset_history",
324};
325
Guenter Roeck00d616c2016-06-20 11:01:57 -0700326static const char * const hwmon_in_attr_templates[] = {
327 [hwmon_in_input] = "in%d_input",
328 [hwmon_in_min] = "in%d_min",
329 [hwmon_in_max] = "in%d_max",
330 [hwmon_in_lcrit] = "in%d_lcrit",
331 [hwmon_in_crit] = "in%d_crit",
332 [hwmon_in_average] = "in%d_average",
333 [hwmon_in_lowest] = "in%d_lowest",
334 [hwmon_in_highest] = "in%d_highest",
335 [hwmon_in_reset_history] = "in%d_reset_history",
336 [hwmon_in_label] = "in%d_label",
337 [hwmon_in_alarm] = "in%d_alarm",
338 [hwmon_in_min_alarm] = "in%d_min_alarm",
339 [hwmon_in_max_alarm] = "in%d_max_alarm",
340 [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
341 [hwmon_in_crit_alarm] = "in%d_crit_alarm",
342};
343
Guenter Roeck9b269472016-06-20 11:10:33 -0700344static const char * const hwmon_curr_attr_templates[] = {
345 [hwmon_curr_input] = "curr%d_input",
346 [hwmon_curr_min] = "curr%d_min",
347 [hwmon_curr_max] = "curr%d_max",
348 [hwmon_curr_lcrit] = "curr%d_lcrit",
349 [hwmon_curr_crit] = "curr%d_crit",
350 [hwmon_curr_average] = "curr%d_average",
351 [hwmon_curr_lowest] = "curr%d_lowest",
352 [hwmon_curr_highest] = "curr%d_highest",
353 [hwmon_curr_reset_history] = "curr%d_reset_history",
354 [hwmon_curr_label] = "curr%d_label",
355 [hwmon_curr_alarm] = "curr%d_alarm",
356 [hwmon_curr_min_alarm] = "curr%d_min_alarm",
357 [hwmon_curr_max_alarm] = "curr%d_max_alarm",
358 [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
359 [hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
360};
361
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700362static const char * const hwmon_power_attr_templates[] = {
363 [hwmon_power_average] = "power%d_average",
364 [hwmon_power_average_interval] = "power%d_average_interval",
365 [hwmon_power_average_interval_max] = "power%d_interval_max",
366 [hwmon_power_average_interval_min] = "power%d_interval_min",
367 [hwmon_power_average_highest] = "power%d_average_highest",
368 [hwmon_power_average_lowest] = "power%d_average_lowest",
369 [hwmon_power_average_max] = "power%d_average_max",
370 [hwmon_power_average_min] = "power%d_average_min",
371 [hwmon_power_input] = "power%d_input",
372 [hwmon_power_input_highest] = "power%d_input_highest",
373 [hwmon_power_input_lowest] = "power%d_input_lowest",
374 [hwmon_power_reset_history] = "power%d_reset_history",
375 [hwmon_power_accuracy] = "power%d_accuracy",
376 [hwmon_power_cap] = "power%d_cap",
377 [hwmon_power_cap_hyst] = "power%d_cap_hyst",
378 [hwmon_power_cap_max] = "power%d_cap_max",
379 [hwmon_power_cap_min] = "power%d_cap_min",
380 [hwmon_power_max] = "power%d_max",
381 [hwmon_power_crit] = "power%d_crit",
382 [hwmon_power_label] = "power%d_label",
383 [hwmon_power_alarm] = "power%d_alarm",
384 [hwmon_power_cap_alarm] = "power%d_cap_alarm",
385 [hwmon_power_max_alarm] = "power%d_max_alarm",
386 [hwmon_power_crit_alarm] = "power%d_crit_alarm",
387};
388
Guenter Roeck6bfcca42016-06-20 11:38:37 -0700389static const char * const hwmon_energy_attr_templates[] = {
390 [hwmon_energy_input] = "energy%d_input",
391 [hwmon_energy_label] = "energy%d_label",
392};
393
394static const char * const hwmon_humidity_attr_templates[] = {
395 [hwmon_humidity_input] = "humidity%d_input",
396 [hwmon_humidity_label] = "humidity%d_label",
397 [hwmon_humidity_min] = "humidity%d_min",
398 [hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
399 [hwmon_humidity_max] = "humidity%d_max",
400 [hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
401 [hwmon_humidity_alarm] = "humidity%d_alarm",
402 [hwmon_humidity_fault] = "humidity%d_fault",
403};
404
Guenter Roeck8faee732016-06-25 19:52:13 -0700405static const char * const hwmon_fan_attr_templates[] = {
406 [hwmon_fan_input] = "fan%d_input",
407 [hwmon_fan_label] = "fan%d_label",
408 [hwmon_fan_min] = "fan%d_min",
409 [hwmon_fan_max] = "fan%d_max",
410 [hwmon_fan_div] = "fan%d_div",
411 [hwmon_fan_pulses] = "fan%d_pulses",
412 [hwmon_fan_target] = "fan%d_target",
413 [hwmon_fan_alarm] = "fan%d_alarm",
414 [hwmon_fan_min_alarm] = "fan%d_min_alarm",
415 [hwmon_fan_max_alarm] = "fan%d_max_alarm",
416 [hwmon_fan_fault] = "fan%d_fault",
417};
418
Guenter Roeckf9f7bb32016-06-26 12:20:46 -0700419static const char * const hwmon_pwm_attr_templates[] = {
420 [hwmon_pwm_input] = "pwm%d",
421 [hwmon_pwm_enable] = "pwm%d_enable",
422 [hwmon_pwm_mode] = "pwm%d_mode",
423 [hwmon_pwm_freq] = "pwm%d_freq",
424};
425
Guenter Roeckd5601682015-08-26 19:38:11 -0700426static const char * const *__templates[] = {
427 [hwmon_chip] = hwmon_chip_attr_templates,
428 [hwmon_temp] = hwmon_temp_attr_templates,
Guenter Roeck00d616c2016-06-20 11:01:57 -0700429 [hwmon_in] = hwmon_in_attr_templates,
Guenter Roeck9b269472016-06-20 11:10:33 -0700430 [hwmon_curr] = hwmon_curr_attr_templates,
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700431 [hwmon_power] = hwmon_power_attr_templates,
Guenter Roeck6bfcca42016-06-20 11:38:37 -0700432 [hwmon_energy] = hwmon_energy_attr_templates,
433 [hwmon_humidity] = hwmon_humidity_attr_templates,
Guenter Roeck8faee732016-06-25 19:52:13 -0700434 [hwmon_fan] = hwmon_fan_attr_templates,
Guenter Roeckf9f7bb32016-06-26 12:20:46 -0700435 [hwmon_pwm] = hwmon_pwm_attr_templates,
Guenter Roeckd5601682015-08-26 19:38:11 -0700436};
437
438static const int __templates_size[] = {
439 [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attr_templates),
440 [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
Guenter Roeck00d616c2016-06-20 11:01:57 -0700441 [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
Guenter Roeck9b269472016-06-20 11:10:33 -0700442 [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700443 [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
Guenter Roeck6bfcca42016-06-20 11:38:37 -0700444 [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
445 [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
Guenter Roeck8faee732016-06-25 19:52:13 -0700446 [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
Guenter Roeckf9f7bb32016-06-26 12:20:46 -0700447 [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
Guenter Roeckd5601682015-08-26 19:38:11 -0700448};
449
450static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
451{
452 int i, n;
453
454 for (i = n = 0; info->config[i]; i++)
455 n += hweight32(info->config[i]);
456
457 return n;
458}
459
Guenter Roeck974185ee2020-01-16 10:44:17 -0800460static int hwmon_genattrs(const void *drvdata,
Guenter Roeckd5601682015-08-26 19:38:11 -0700461 struct attribute **attrs,
462 const struct hwmon_ops *ops,
463 const struct hwmon_channel_info *info)
464{
465 const char * const *templates;
466 int template_size;
467 int i, aindex = 0;
468
469 if (info->type >= ARRAY_SIZE(__templates))
470 return -EINVAL;
471
472 templates = __templates[info->type];
473 template_size = __templates_size[info->type];
474
475 for (i = 0; info->config[i]; i++) {
476 u32 attr_mask = info->config[i];
477 u32 attr;
478
479 while (attr_mask) {
480 struct attribute *a;
481
482 attr = __ffs(attr_mask);
483 attr_mask &= ~BIT(attr);
484 if (attr >= template_size)
485 return -EINVAL;
Guenter Roeck974185ee2020-01-16 10:44:17 -0800486 a = hwmon_genattr(drvdata, info->type, attr, i,
Guenter Roeckd5601682015-08-26 19:38:11 -0700487 templates[attr], ops);
488 if (IS_ERR(a)) {
489 if (PTR_ERR(a) != -ENOENT)
490 return PTR_ERR(a);
491 continue;
492 }
493 attrs[aindex++] = a;
494 }
495 }
496 return aindex;
497}
498
499static struct attribute **
Guenter Roeck974185ee2020-01-16 10:44:17 -0800500__hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip)
Guenter Roeckd5601682015-08-26 19:38:11 -0700501{
502 int ret, i, aindex = 0, nattrs = 0;
503 struct attribute **attrs;
504
505 for (i = 0; chip->info[i]; i++)
506 nattrs += hwmon_num_channel_attrs(chip->info[i]);
507
508 if (nattrs == 0)
509 return ERR_PTR(-EINVAL);
510
Guenter Roeck974185ee2020-01-16 10:44:17 -0800511 attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL);
Guenter Roeckd5601682015-08-26 19:38:11 -0700512 if (!attrs)
513 return ERR_PTR(-ENOMEM);
514
515 for (i = 0; chip->info[i]; i++) {
Guenter Roeck974185ee2020-01-16 10:44:17 -0800516 ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops,
Guenter Roeckd5601682015-08-26 19:38:11 -0700517 chip->info[i]);
Guenter Roeck974185ee2020-01-16 10:44:17 -0800518 if (ret < 0) {
519 hwmon_free_attrs(attrs);
Guenter Roeckd5601682015-08-26 19:38:11 -0700520 return ERR_PTR(ret);
Guenter Roeck974185ee2020-01-16 10:44:17 -0800521 }
Guenter Roeckd5601682015-08-26 19:38:11 -0700522 aindex += ret;
523 }
524
525 return attrs;
526}
527
528static struct device *
529__hwmon_device_register(struct device *dev, const char *name, void *drvdata,
530 const struct hwmon_chip_info *chip,
531 const struct attribute_group **groups)
532{
533 struct hwmon_device *hwdev;
534 struct device *hdev;
535 int i, j, err, id;
536
537 /* Do not accept invalid characters in hwmon name attribute */
538 if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
539 return ERR_PTR(-EINVAL);
540
541 id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
542 if (id < 0)
543 return ERR_PTR(id);
544
545 hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
546 if (hwdev == NULL) {
547 err = -ENOMEM;
548 goto ida_remove;
549 }
550
551 hdev = &hwdev->dev;
552
553 if (chip && chip->ops->is_visible) {
554 struct attribute **attrs;
555 int ngroups = 2;
556
557 if (groups)
558 for (i = 0; groups[i]; i++)
559 ngroups++;
560
Guenter Roeck974185ee2020-01-16 10:44:17 -0800561 hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL);
Colin Ian King38d8ed62016-10-23 21:56:08 +0100562 if (!hwdev->groups) {
563 err = -ENOMEM;
564 goto free_hwmon;
565 }
Guenter Roeckd5601682015-08-26 19:38:11 -0700566
Guenter Roeck974185ee2020-01-16 10:44:17 -0800567 attrs = __hwmon_create_attrs(drvdata, chip);
Guenter Roeckd5601682015-08-26 19:38:11 -0700568 if (IS_ERR(attrs)) {
569 err = PTR_ERR(attrs);
570 goto free_hwmon;
571 }
572
573 hwdev->group.attrs = attrs;
574 ngroups = 0;
575 hwdev->groups[ngroups++] = &hwdev->group;
576
577 if (groups) {
578 for (i = 0; groups[i]; i++)
579 hwdev->groups[ngroups++] = groups[i];
580 }
581
582 hdev->groups = hwdev->groups;
583 } else {
584 hdev->groups = groups;
585 }
586
587 hwdev->name = name;
588 hdev->class = &hwmon_class;
589 hdev->parent = dev;
590 hdev->of_node = dev ? dev->of_node : NULL;
591 hwdev->chip = chip;
592 dev_set_drvdata(hdev, drvdata);
593 dev_set_name(hdev, HWMON_ID_FORMAT, id);
594 err = device_register(hdev);
595 if (err)
596 goto free_hwmon;
597
598 if (chip && chip->ops->is_visible && chip->ops->read &&
599 chip->info[0]->type == hwmon_chip &&
600 (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
601 const struct hwmon_channel_info **info = chip->info;
602
603 for (i = 1; info[i]; i++) {
604 if (info[i]->type != hwmon_temp)
605 continue;
606
607 for (j = 0; info[i]->config[j]; j++) {
608 if (!chip->ops->is_visible(drvdata, hwmon_temp,
609 hwmon_temp_input, j))
610 continue;
Linus Walleija34d0232017-12-05 09:36:14 +0100611 if (info[i]->config[j] & HWMON_T_INPUT) {
Guenter Roeck974185ee2020-01-16 10:44:17 -0800612 err = hwmon_thermal_add_sensor(hdev, j);
Dmitry Osipenko81ca3dc2018-10-24 22:37:13 +0300613 if (err) {
614 device_unregister(hdev);
615 goto ida_remove;
616 }
Linus Walleija34d0232017-12-05 09:36:14 +0100617 }
Guenter Roeckd5601682015-08-26 19:38:11 -0700618 }
619 }
620 }
621
622 return hdev;
623
624free_hwmon:
Guenter Roeck974185ee2020-01-16 10:44:17 -0800625 hwmon_dev_release(hdev);
Guenter Roeckd5601682015-08-26 19:38:11 -0700626ida_remove:
627 ida_simple_remove(&hwmon_ida, id);
628 return ERR_PTR(err);
629}
630
Mark M. Hoffman12364412005-07-15 21:38:08 -0400631/**
Guenter Roeckbab22432013-07-06 13:57:23 -0700632 * hwmon_device_register_with_groups - register w/ hwmon
633 * @dev: the parent device
634 * @name: hwmon name attribute
635 * @drvdata: driver data to attach to created device
636 * @groups: List of attribute groups to create
637 *
638 * hwmon_device_unregister() must be called when the device is no
639 * longer needed.
640 *
641 * Returns the pointer to the new device.
642 */
643struct device *
644hwmon_device_register_with_groups(struct device *dev, const char *name,
645 void *drvdata,
646 const struct attribute_group **groups)
647{
Guenter Roeckd5601682015-08-26 19:38:11 -0700648 return __hwmon_device_register(dev, name, drvdata, NULL, groups);
Guenter Roeckbab22432013-07-06 13:57:23 -0700649}
650EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
651
652/**
Guenter Roeckd5601682015-08-26 19:38:11 -0700653 * hwmon_device_register_with_info - register w/ hwmon
654 * @dev: the parent device
655 * @name: hwmon name attribute
656 * @drvdata: driver data to attach to created device
657 * @info: Pointer to hwmon chip information
658 * @groups - pointer to list of driver specific attribute groups
659 *
660 * hwmon_device_unregister() must be called when the device is no
661 * longer needed.
662 *
663 * Returns the pointer to the new device.
664 */
665struct device *
666hwmon_device_register_with_info(struct device *dev, const char *name,
667 void *drvdata,
668 const struct hwmon_chip_info *chip,
669 const struct attribute_group **groups)
670{
671 if (chip && (!chip->ops || !chip->info))
672 return ERR_PTR(-EINVAL);
673
674 return __hwmon_device_register(dev, name, drvdata, chip, groups);
675}
676EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
677
678/**
Tony Jones1beeffe2007-08-20 13:46:20 -0700679 * hwmon_device_register - register w/ hwmon
Mark M. Hoffman12364412005-07-15 21:38:08 -0400680 * @dev: the device to register
681 *
Tony Jones1beeffe2007-08-20 13:46:20 -0700682 * hwmon_device_unregister() must be called when the device is no
Mark M. Hoffman12364412005-07-15 21:38:08 -0400683 * longer needed.
684 *
Tony Jones1beeffe2007-08-20 13:46:20 -0700685 * Returns the pointer to the new device.
Mark M. Hoffman12364412005-07-15 21:38:08 -0400686 */
Tony Jones1beeffe2007-08-20 13:46:20 -0700687struct device *hwmon_device_register(struct device *dev)
Mark M. Hoffman12364412005-07-15 21:38:08 -0400688{
Guenter Roeckbab22432013-07-06 13:57:23 -0700689 return hwmon_device_register_with_groups(dev, NULL, NULL, NULL);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400690}
Frans Meulenbroeks839a9ee2012-01-08 19:34:14 +0100691EXPORT_SYMBOL_GPL(hwmon_device_register);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400692
693/**
694 * hwmon_device_unregister - removes the previously registered class device
695 *
Tony Jones1beeffe2007-08-20 13:46:20 -0700696 * @dev: the class device to destroy
Mark M. Hoffman12364412005-07-15 21:38:08 -0400697 */
Tony Jones1beeffe2007-08-20 13:46:20 -0700698void hwmon_device_unregister(struct device *dev)
Mark M. Hoffman12364412005-07-15 21:38:08 -0400699{
700 int id;
701
Kay Sievers739cf3a2009-01-06 10:44:41 -0800702 if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
Tony Jones1beeffe2007-08-20 13:46:20 -0700703 device_unregister(dev);
Jonathan Cameron4ca5f462011-10-31 17:10:09 -0700704 ida_simple_remove(&hwmon_ida, id);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400705 } else
Tony Jones1beeffe2007-08-20 13:46:20 -0700706 dev_dbg(dev->parent,
Mark M. Hoffman12364412005-07-15 21:38:08 -0400707 "hwmon_device_unregister() failed: bad class ID!\n");
708}
Frans Meulenbroeks839a9ee2012-01-08 19:34:14 +0100709EXPORT_SYMBOL_GPL(hwmon_device_unregister);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400710
Guenter Roeck74188cb2013-07-11 20:00:12 -0700711static void devm_hwmon_release(struct device *dev, void *res)
712{
713 struct device *hwdev = *(struct device **)res;
714
715 hwmon_device_unregister(hwdev);
716}
717
718/**
719 * devm_hwmon_device_register_with_groups - register w/ hwmon
720 * @dev: the parent device
721 * @name: hwmon name attribute
722 * @drvdata: driver data to attach to created device
723 * @groups: List of attribute groups to create
724 *
725 * Returns the pointer to the new device. The new device is automatically
726 * unregistered with the parent device.
727 */
728struct device *
729devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
730 void *drvdata,
731 const struct attribute_group **groups)
732{
733 struct device **ptr, *hwdev;
734
735 if (!dev)
736 return ERR_PTR(-EINVAL);
737
738 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
739 if (!ptr)
740 return ERR_PTR(-ENOMEM);
741
742 hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
743 if (IS_ERR(hwdev))
744 goto error;
745
746 *ptr = hwdev;
747 devres_add(dev, ptr);
748 return hwdev;
749
750error:
751 devres_free(ptr);
752 return hwdev;
753}
754EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
755
Guenter Roeckd5601682015-08-26 19:38:11 -0700756/**
757 * devm_hwmon_device_register_with_info - register w/ hwmon
758 * @dev: the parent device
759 * @name: hwmon name attribute
760 * @drvdata: driver data to attach to created device
761 * @info: Pointer to hwmon chip information
762 * @groups - pointer to list of driver specific attribute groups
763 *
764 * Returns the pointer to the new device. The new device is automatically
765 * unregistered with the parent device.
766 */
767struct device *
768devm_hwmon_device_register_with_info(struct device *dev, const char *name,
769 void *drvdata,
770 const struct hwmon_chip_info *chip,
771 const struct attribute_group **groups)
772{
773 struct device **ptr, *hwdev;
774
775 if (!dev)
776 return ERR_PTR(-EINVAL);
777
778 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
779 if (!ptr)
780 return ERR_PTR(-ENOMEM);
781
782 hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
783 groups);
784 if (IS_ERR(hwdev))
785 goto error;
786
787 *ptr = hwdev;
788 devres_add(dev, ptr);
789
790 return hwdev;
791
792error:
793 devres_free(ptr);
794 return hwdev;
795}
796EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
797
Guenter Roeck74188cb2013-07-11 20:00:12 -0700798static int devm_hwmon_match(struct device *dev, void *res, void *data)
799{
800 struct device **hwdev = res;
801
802 return *hwdev == data;
803}
804
805/**
806 * devm_hwmon_device_unregister - removes a previously registered hwmon device
807 *
808 * @dev: the parent device of the device to unregister
809 */
810void devm_hwmon_device_unregister(struct device *dev)
811{
812 WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
813}
814EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
815
Jean Delvare2958b1e2009-06-15 18:39:50 +0200816static void __init hwmon_pci_quirks(void)
817{
818#if defined CONFIG_X86 && defined CONFIG_PCI
819 struct pci_dev *sb;
820 u16 base;
821 u8 enable;
822
823 /* Open access to 0x295-0x296 on MSI MS-7031 */
824 sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
Jean Delvared6dab7d2012-12-19 22:16:59 +0100825 if (sb) {
826 if (sb->subsystem_vendor == 0x1462 && /* MSI */
827 sb->subsystem_device == 0x0031) { /* MS-7031 */
828 pci_read_config_byte(sb, 0x48, &enable);
829 pci_read_config_word(sb, 0x64, &base);
Jean Delvare2958b1e2009-06-15 18:39:50 +0200830
Jean Delvared6dab7d2012-12-19 22:16:59 +0100831 if (base == 0 && !(enable & BIT(2))) {
832 dev_info(&sb->dev,
833 "Opening wide generic port at 0x295\n");
834 pci_write_config_word(sb, 0x64, 0x295);
835 pci_write_config_byte(sb, 0x48,
836 enable | BIT(2));
837 }
Jean Delvare2958b1e2009-06-15 18:39:50 +0200838 }
Jean Delvared6dab7d2012-12-19 22:16:59 +0100839 pci_dev_put(sb);
Jean Delvare2958b1e2009-06-15 18:39:50 +0200840 }
841#endif
842}
843
Mark M. Hoffman12364412005-07-15 21:38:08 -0400844static int __init hwmon_init(void)
845{
Guenter Roeckbab22432013-07-06 13:57:23 -0700846 int err;
847
Jean Delvare2958b1e2009-06-15 18:39:50 +0200848 hwmon_pci_quirks();
849
Guenter Roeckbab22432013-07-06 13:57:23 -0700850 err = class_register(&hwmon_class);
851 if (err) {
852 pr_err("couldn't register hwmon sysfs class\n");
853 return err;
Mark M. Hoffman12364412005-07-15 21:38:08 -0400854 }
855 return 0;
856}
857
858static void __exit hwmon_exit(void)
859{
Guenter Roeckbab22432013-07-06 13:57:23 -0700860 class_unregister(&hwmon_class);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400861}
862
David Brownell37f54ee2007-02-14 21:15:04 +0100863subsys_initcall(hwmon_init);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400864module_exit(hwmon_exit);
865
Mark M. Hoffman12364412005-07-15 21:38:08 -0400866MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
867MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
868MODULE_LICENSE("GPL");
869