blob: c22dc1e079113f9174fe3b6a5bb1b0f6a4c9e724 [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 */
121#if IS_REACHABLE(CONFIG_THERMAL) && defined(CONFIG_THERMAL_OF) && \
122 (!defined(CONFIG_THERMAL_HWMON) || \
123 !(defined(MODULE) && IS_MODULE(CONFIG_THERMAL)))
Guenter Roeckd5601682015-08-26 19:38:11 -0700124static int hwmon_thermal_get_temp(void *data, int *temp)
125{
126 struct hwmon_thermal_data *tdata = data;
127 struct hwmon_device *hwdev = tdata->hwdev;
128 int ret;
129 long t;
130
131 ret = hwdev->chip->ops->read(&hwdev->dev, hwmon_temp, hwmon_temp_input,
132 tdata->index, &t);
133 if (ret < 0)
134 return ret;
135
136 *temp = t;
137
138 return 0;
139}
140
Julia Lawallc9920652017-08-08 17:09:02 +0200141static const struct thermal_zone_of_device_ops hwmon_thermal_ops = {
Guenter Roeckd5601682015-08-26 19:38:11 -0700142 .get_temp = hwmon_thermal_get_temp,
143};
144
145static int hwmon_thermal_add_sensor(struct device *dev,
146 struct hwmon_device *hwdev, int index)
147{
148 struct hwmon_thermal_data *tdata;
Linus Walleij47c332d2017-12-05 09:36:14 +0100149 struct thermal_zone_device *tzd;
Guenter Roeckd5601682015-08-26 19:38:11 -0700150
151 tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
152 if (!tdata)
153 return -ENOMEM;
154
155 tdata->hwdev = hwdev;
156 tdata->index = index;
157
Linus Walleij47c332d2017-12-05 09:36:14 +0100158 tzd = devm_thermal_zone_of_sensor_register(&hwdev->dev, index, tdata,
159 &hwmon_thermal_ops);
160 /*
161 * If CONFIG_THERMAL_OF is disabled, this returns -ENODEV,
162 * so ignore that error but forward any other error.
163 */
164 if (IS_ERR(tzd) && (PTR_ERR(tzd) != -ENODEV))
165 return PTR_ERR(tzd);
Guenter Roeckd5601682015-08-26 19:38:11 -0700166
167 return 0;
168}
169#else
170static int hwmon_thermal_add_sensor(struct device *dev,
171 struct hwmon_device *hwdev, int index)
172{
173 return 0;
174}
Guenter Roeck86430c12016-08-12 06:28:15 -0700175#endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
Guenter Roeckd5601682015-08-26 19:38:11 -0700176
Nicolin Chen61b8ab22018-10-09 14:42:19 -0700177static int hwmon_attr_base(enum hwmon_sensor_types type)
178{
179 if (type == hwmon_in)
180 return 0;
181 return 1;
182}
183
Guenter Roeckd5601682015-08-26 19:38:11 -0700184/* sysfs attribute management */
185
186static ssize_t hwmon_attr_show(struct device *dev,
187 struct device_attribute *devattr, char *buf)
188{
189 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
190 long val;
191 int ret;
192
193 ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
194 &val);
195 if (ret < 0)
196 return ret;
197
Nicolin Chen61b8ab22018-10-09 14:42:19 -0700198 trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type),
199 hattr->name, val);
200
Guenter Roeckd5601682015-08-26 19:38:11 -0700201 return sprintf(buf, "%ld\n", val);
202}
203
Guenter Roecke159ab52016-08-07 20:51:25 -0700204static ssize_t hwmon_attr_show_string(struct device *dev,
205 struct device_attribute *devattr,
206 char *buf)
207{
208 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
Nicolin Chen61b8ab22018-10-09 14:42:19 -0700209 enum hwmon_sensor_types type = hattr->type;
Jean Delvare5ba6bcb2017-03-07 06:15:15 -0800210 const char *s;
Guenter Roecke159ab52016-08-07 20:51:25 -0700211 int ret;
212
213 ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
214 hattr->index, &s);
215 if (ret < 0)
216 return ret;
217
Nicolin Chen61b8ab22018-10-09 14:42:19 -0700218 trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type),
219 hattr->name, s);
220
Guenter Roecke159ab52016-08-07 20:51:25 -0700221 return sprintf(buf, "%s\n", s);
222}
223
Guenter Roeckd5601682015-08-26 19:38:11 -0700224static ssize_t hwmon_attr_store(struct device *dev,
225 struct device_attribute *devattr,
226 const char *buf, size_t count)
227{
228 struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
229 long val;
230 int ret;
231
232 ret = kstrtol(buf, 10, &val);
233 if (ret < 0)
234 return ret;
235
236 ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
237 val);
238 if (ret < 0)
239 return ret;
240
Nicolin Chen61b8ab22018-10-09 14:42:19 -0700241 trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type),
242 hattr->name, val);
Guenter Roeckd5601682015-08-26 19:38:11 -0700243
Nicolin Chen61b8ab22018-10-09 14:42:19 -0700244 return count;
Guenter Roeckd5601682015-08-26 19:38:11 -0700245}
246
Guenter Roecke159ab52016-08-07 20:51:25 -0700247static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
248{
249 return (type == hwmon_temp && attr == hwmon_temp_label) ||
250 (type == hwmon_in && attr == hwmon_in_label) ||
251 (type == hwmon_curr && attr == hwmon_curr_label) ||
252 (type == hwmon_power && attr == hwmon_power_label) ||
253 (type == hwmon_energy && attr == hwmon_energy_label) ||
254 (type == hwmon_humidity && attr == hwmon_humidity_label) ||
255 (type == hwmon_fan && attr == hwmon_fan_label);
256}
257
Guenter Roeckd5601682015-08-26 19:38:11 -0700258static struct attribute *hwmon_genattr(struct device *dev,
259 const void *drvdata,
260 enum hwmon_sensor_types type,
261 u32 attr,
262 int index,
263 const char *template,
264 const struct hwmon_ops *ops)
265{
266 struct hwmon_device_attribute *hattr;
267 struct device_attribute *dattr;
268 struct attribute *a;
269 umode_t mode;
Rasmus Villemoes3b443de2018-10-27 00:30:59 +0200270 const char *name;
Guenter Roecke159ab52016-08-07 20:51:25 -0700271 bool is_string = is_string_attr(type, attr);
Guenter Roeckd5601682015-08-26 19:38:11 -0700272
273 /* The attribute is invisible if there is no template string */
274 if (!template)
275 return ERR_PTR(-ENOENT);
276
277 mode = ops->is_visible(drvdata, type, attr, index);
278 if (!mode)
279 return ERR_PTR(-ENOENT);
280
Guenter Roeck0d871162018-12-10 14:02:08 -0800281 if ((mode & 0444) && ((is_string && !ops->read_string) ||
Guenter Roecke159ab52016-08-07 20:51:25 -0700282 (!is_string && !ops->read)))
Guenter Roeckd5601682015-08-26 19:38:11 -0700283 return ERR_PTR(-EINVAL);
Guenter Roeck0d871162018-12-10 14:02:08 -0800284 if ((mode & 0222) && !ops->write)
Guenter Roeckd5601682015-08-26 19:38:11 -0700285 return ERR_PTR(-EINVAL);
286
Guenter Roeckd5601682015-08-26 19:38:11 -0700287 hattr = devm_kzalloc(dev, sizeof(*hattr), GFP_KERNEL);
288 if (!hattr)
289 return ERR_PTR(-ENOMEM);
290
Guenter Roeck3a412d52016-10-16 10:52:04 -0700291 if (type == hwmon_chip) {
Rasmus Villemoes3b443de2018-10-27 00:30:59 +0200292 name = template;
Guenter Roeck3a412d52016-10-16 10:52:04 -0700293 } else {
294 scnprintf(hattr->name, sizeof(hattr->name), template,
295 index + hwmon_attr_base(type));
296 name = hattr->name;
297 }
298
Guenter Roeckd5601682015-08-26 19:38:11 -0700299 hattr->type = type;
300 hattr->attr = attr;
301 hattr->index = index;
302 hattr->ops = ops;
303
304 dattr = &hattr->dev_attr;
Guenter Roecke159ab52016-08-07 20:51:25 -0700305 dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
Guenter Roeckd5601682015-08-26 19:38:11 -0700306 dattr->store = hwmon_attr_store;
307
308 a = &dattr->attr;
309 sysfs_attr_init(a);
310 a->name = name;
311 a->mode = mode;
312
313 return a;
314}
315
Guenter Roeckf4d325d2016-10-16 10:38:52 -0700316/*
317 * Chip attributes are not attribute templates but actual sysfs attributes.
318 * See hwmon_genattr() for special handling.
319 */
320static const char * const hwmon_chip_attrs[] = {
Guenter Roeckd5601682015-08-26 19:38:11 -0700321 [hwmon_chip_temp_reset_history] = "temp_reset_history",
Guenter Roeck00d616c2016-06-20 11:01:57 -0700322 [hwmon_chip_in_reset_history] = "in_reset_history",
Guenter Roeck9b269472016-06-20 11:10:33 -0700323 [hwmon_chip_curr_reset_history] = "curr_reset_history",
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700324 [hwmon_chip_power_reset_history] = "power_reset_history",
Guenter Roeckd5601682015-08-26 19:38:11 -0700325 [hwmon_chip_update_interval] = "update_interval",
326 [hwmon_chip_alarms] = "alarms",
327};
328
329static const char * const hwmon_temp_attr_templates[] = {
330 [hwmon_temp_input] = "temp%d_input",
331 [hwmon_temp_type] = "temp%d_type",
332 [hwmon_temp_lcrit] = "temp%d_lcrit",
333 [hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
334 [hwmon_temp_min] = "temp%d_min",
335 [hwmon_temp_min_hyst] = "temp%d_min_hyst",
336 [hwmon_temp_max] = "temp%d_max",
337 [hwmon_temp_max_hyst] = "temp%d_max_hyst",
338 [hwmon_temp_crit] = "temp%d_crit",
339 [hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
340 [hwmon_temp_emergency] = "temp%d_emergency",
341 [hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
342 [hwmon_temp_alarm] = "temp%d_alarm",
343 [hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
344 [hwmon_temp_min_alarm] = "temp%d_min_alarm",
345 [hwmon_temp_max_alarm] = "temp%d_max_alarm",
346 [hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
347 [hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
348 [hwmon_temp_fault] = "temp%d_fault",
349 [hwmon_temp_offset] = "temp%d_offset",
350 [hwmon_temp_label] = "temp%d_label",
351 [hwmon_temp_lowest] = "temp%d_lowest",
352 [hwmon_temp_highest] = "temp%d_highest",
353 [hwmon_temp_reset_history] = "temp%d_reset_history",
354};
355
Guenter Roeck00d616c2016-06-20 11:01:57 -0700356static const char * const hwmon_in_attr_templates[] = {
357 [hwmon_in_input] = "in%d_input",
358 [hwmon_in_min] = "in%d_min",
359 [hwmon_in_max] = "in%d_max",
360 [hwmon_in_lcrit] = "in%d_lcrit",
361 [hwmon_in_crit] = "in%d_crit",
362 [hwmon_in_average] = "in%d_average",
363 [hwmon_in_lowest] = "in%d_lowest",
364 [hwmon_in_highest] = "in%d_highest",
365 [hwmon_in_reset_history] = "in%d_reset_history",
366 [hwmon_in_label] = "in%d_label",
367 [hwmon_in_alarm] = "in%d_alarm",
368 [hwmon_in_min_alarm] = "in%d_min_alarm",
369 [hwmon_in_max_alarm] = "in%d_max_alarm",
370 [hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
371 [hwmon_in_crit_alarm] = "in%d_crit_alarm",
Nicolin Chen68c0d692018-10-05 16:59:04 -0700372 [hwmon_in_enable] = "in%d_enable",
Guenter Roeck00d616c2016-06-20 11:01:57 -0700373};
374
Guenter Roeck9b269472016-06-20 11:10:33 -0700375static const char * const hwmon_curr_attr_templates[] = {
376 [hwmon_curr_input] = "curr%d_input",
377 [hwmon_curr_min] = "curr%d_min",
378 [hwmon_curr_max] = "curr%d_max",
379 [hwmon_curr_lcrit] = "curr%d_lcrit",
380 [hwmon_curr_crit] = "curr%d_crit",
381 [hwmon_curr_average] = "curr%d_average",
382 [hwmon_curr_lowest] = "curr%d_lowest",
383 [hwmon_curr_highest] = "curr%d_highest",
384 [hwmon_curr_reset_history] = "curr%d_reset_history",
385 [hwmon_curr_label] = "curr%d_label",
386 [hwmon_curr_alarm] = "curr%d_alarm",
387 [hwmon_curr_min_alarm] = "curr%d_min_alarm",
388 [hwmon_curr_max_alarm] = "curr%d_max_alarm",
389 [hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
390 [hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
391};
392
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700393static const char * const hwmon_power_attr_templates[] = {
394 [hwmon_power_average] = "power%d_average",
395 [hwmon_power_average_interval] = "power%d_average_interval",
396 [hwmon_power_average_interval_max] = "power%d_interval_max",
397 [hwmon_power_average_interval_min] = "power%d_interval_min",
398 [hwmon_power_average_highest] = "power%d_average_highest",
399 [hwmon_power_average_lowest] = "power%d_average_lowest",
400 [hwmon_power_average_max] = "power%d_average_max",
401 [hwmon_power_average_min] = "power%d_average_min",
402 [hwmon_power_input] = "power%d_input",
403 [hwmon_power_input_highest] = "power%d_input_highest",
404 [hwmon_power_input_lowest] = "power%d_input_lowest",
405 [hwmon_power_reset_history] = "power%d_reset_history",
406 [hwmon_power_accuracy] = "power%d_accuracy",
407 [hwmon_power_cap] = "power%d_cap",
408 [hwmon_power_cap_hyst] = "power%d_cap_hyst",
409 [hwmon_power_cap_max] = "power%d_cap_max",
410 [hwmon_power_cap_min] = "power%d_cap_min",
Andrew Lunnaa7f29b2018-07-17 21:48:11 +0200411 [hwmon_power_min] = "power%d_min",
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700412 [hwmon_power_max] = "power%d_max",
Andrew Lunnaa7f29b2018-07-17 21:48:11 +0200413 [hwmon_power_lcrit] = "power%d_lcrit",
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700414 [hwmon_power_crit] = "power%d_crit",
415 [hwmon_power_label] = "power%d_label",
416 [hwmon_power_alarm] = "power%d_alarm",
417 [hwmon_power_cap_alarm] = "power%d_cap_alarm",
Andrew Lunnaa7f29b2018-07-17 21:48:11 +0200418 [hwmon_power_min_alarm] = "power%d_min_alarm",
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700419 [hwmon_power_max_alarm] = "power%d_max_alarm",
Andrew Lunnaa7f29b2018-07-17 21:48:11 +0200420 [hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700421 [hwmon_power_crit_alarm] = "power%d_crit_alarm",
422};
423
Guenter Roeck6bfcca42016-06-20 11:38:37 -0700424static const char * const hwmon_energy_attr_templates[] = {
425 [hwmon_energy_input] = "energy%d_input",
426 [hwmon_energy_label] = "energy%d_label",
427};
428
429static const char * const hwmon_humidity_attr_templates[] = {
430 [hwmon_humidity_input] = "humidity%d_input",
431 [hwmon_humidity_label] = "humidity%d_label",
432 [hwmon_humidity_min] = "humidity%d_min",
433 [hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
434 [hwmon_humidity_max] = "humidity%d_max",
435 [hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
436 [hwmon_humidity_alarm] = "humidity%d_alarm",
437 [hwmon_humidity_fault] = "humidity%d_fault",
438};
439
Guenter Roeck8faee732016-06-25 19:52:13 -0700440static const char * const hwmon_fan_attr_templates[] = {
441 [hwmon_fan_input] = "fan%d_input",
442 [hwmon_fan_label] = "fan%d_label",
443 [hwmon_fan_min] = "fan%d_min",
444 [hwmon_fan_max] = "fan%d_max",
445 [hwmon_fan_div] = "fan%d_div",
446 [hwmon_fan_pulses] = "fan%d_pulses",
447 [hwmon_fan_target] = "fan%d_target",
448 [hwmon_fan_alarm] = "fan%d_alarm",
449 [hwmon_fan_min_alarm] = "fan%d_min_alarm",
450 [hwmon_fan_max_alarm] = "fan%d_max_alarm",
451 [hwmon_fan_fault] = "fan%d_fault",
452};
453
Guenter Roeckf9f7bb32016-06-26 12:20:46 -0700454static const char * const hwmon_pwm_attr_templates[] = {
455 [hwmon_pwm_input] = "pwm%d",
456 [hwmon_pwm_enable] = "pwm%d_enable",
457 [hwmon_pwm_mode] = "pwm%d_mode",
458 [hwmon_pwm_freq] = "pwm%d_freq",
459};
460
Guenter Roeckd5601682015-08-26 19:38:11 -0700461static const char * const *__templates[] = {
Guenter Roeckf4d325d2016-10-16 10:38:52 -0700462 [hwmon_chip] = hwmon_chip_attrs,
Guenter Roeckd5601682015-08-26 19:38:11 -0700463 [hwmon_temp] = hwmon_temp_attr_templates,
Guenter Roeck00d616c2016-06-20 11:01:57 -0700464 [hwmon_in] = hwmon_in_attr_templates,
Guenter Roeck9b269472016-06-20 11:10:33 -0700465 [hwmon_curr] = hwmon_curr_attr_templates,
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700466 [hwmon_power] = hwmon_power_attr_templates,
Guenter Roeck6bfcca42016-06-20 11:38:37 -0700467 [hwmon_energy] = hwmon_energy_attr_templates,
468 [hwmon_humidity] = hwmon_humidity_attr_templates,
Guenter Roeck8faee732016-06-25 19:52:13 -0700469 [hwmon_fan] = hwmon_fan_attr_templates,
Guenter Roeckf9f7bb32016-06-26 12:20:46 -0700470 [hwmon_pwm] = hwmon_pwm_attr_templates,
Guenter Roeckd5601682015-08-26 19:38:11 -0700471};
472
473static const int __templates_size[] = {
Guenter Roeckf4d325d2016-10-16 10:38:52 -0700474 [hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
Guenter Roeckd5601682015-08-26 19:38:11 -0700475 [hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
Guenter Roeck00d616c2016-06-20 11:01:57 -0700476 [hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
Guenter Roeck9b269472016-06-20 11:10:33 -0700477 [hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
Guenter Roeckb308f5c2016-06-20 11:27:36 -0700478 [hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
Guenter Roeck6bfcca42016-06-20 11:38:37 -0700479 [hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
480 [hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
Guenter Roeck8faee732016-06-25 19:52:13 -0700481 [hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
Guenter Roeckf9f7bb32016-06-26 12:20:46 -0700482 [hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
Guenter Roeckd5601682015-08-26 19:38:11 -0700483};
484
485static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
486{
487 int i, n;
488
489 for (i = n = 0; info->config[i]; i++)
490 n += hweight32(info->config[i]);
491
492 return n;
493}
494
495static int hwmon_genattrs(struct device *dev,
496 const void *drvdata,
497 struct attribute **attrs,
498 const struct hwmon_ops *ops,
499 const struct hwmon_channel_info *info)
500{
501 const char * const *templates;
502 int template_size;
503 int i, aindex = 0;
504
505 if (info->type >= ARRAY_SIZE(__templates))
506 return -EINVAL;
507
508 templates = __templates[info->type];
509 template_size = __templates_size[info->type];
510
511 for (i = 0; info->config[i]; i++) {
512 u32 attr_mask = info->config[i];
513 u32 attr;
514
515 while (attr_mask) {
516 struct attribute *a;
517
518 attr = __ffs(attr_mask);
519 attr_mask &= ~BIT(attr);
520 if (attr >= template_size)
521 return -EINVAL;
522 a = hwmon_genattr(dev, drvdata, info->type, attr, i,
523 templates[attr], ops);
524 if (IS_ERR(a)) {
525 if (PTR_ERR(a) != -ENOENT)
526 return PTR_ERR(a);
527 continue;
528 }
529 attrs[aindex++] = a;
530 }
531 }
532 return aindex;
533}
534
535static struct attribute **
536__hwmon_create_attrs(struct device *dev, const void *drvdata,
537 const struct hwmon_chip_info *chip)
538{
539 int ret, i, aindex = 0, nattrs = 0;
540 struct attribute **attrs;
541
542 for (i = 0; chip->info[i]; i++)
543 nattrs += hwmon_num_channel_attrs(chip->info[i]);
544
545 if (nattrs == 0)
546 return ERR_PTR(-EINVAL);
547
548 attrs = devm_kcalloc(dev, nattrs + 1, sizeof(*attrs), GFP_KERNEL);
549 if (!attrs)
550 return ERR_PTR(-ENOMEM);
551
552 for (i = 0; chip->info[i]; i++) {
553 ret = hwmon_genattrs(dev, drvdata, &attrs[aindex], chip->ops,
554 chip->info[i]);
555 if (ret < 0)
556 return ERR_PTR(ret);
557 aindex += ret;
558 }
559
560 return attrs;
561}
562
563static struct device *
564__hwmon_device_register(struct device *dev, const char *name, void *drvdata,
565 const struct hwmon_chip_info *chip,
566 const struct attribute_group **groups)
567{
568 struct hwmon_device *hwdev;
569 struct device *hdev;
570 int i, j, err, id;
571
Guenter Roeck74d3b642017-01-27 19:35:57 -0800572 /* Complain about invalid characters in hwmon name attribute */
Guenter Roeckd5601682015-08-26 19:38:11 -0700573 if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
Guenter Roeck74d3b642017-01-27 19:35:57 -0800574 dev_warn(dev,
575 "hwmon: '%s' is not a valid name attribute, please fix\n",
576 name);
Guenter Roeckd5601682015-08-26 19:38:11 -0700577
578 id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
579 if (id < 0)
580 return ERR_PTR(id);
581
582 hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
583 if (hwdev == NULL) {
584 err = -ENOMEM;
585 goto ida_remove;
586 }
587
588 hdev = &hwdev->dev;
589
Guenter Roeck239552f2016-10-16 17:06:20 -0700590 if (chip) {
Guenter Roeckd5601682015-08-26 19:38:11 -0700591 struct attribute **attrs;
Guenter Roeckb2a4cc32016-10-16 17:11:52 -0700592 int ngroups = 2; /* terminating NULL plus &hwdev->groups */
Guenter Roeckd5601682015-08-26 19:38:11 -0700593
594 if (groups)
595 for (i = 0; groups[i]; i++)
596 ngroups++;
597
598 hwdev->groups = devm_kcalloc(dev, ngroups, sizeof(*groups),
599 GFP_KERNEL);
Colin Ian King38d8ed62016-10-23 21:56:08 +0100600 if (!hwdev->groups) {
601 err = -ENOMEM;
602 goto free_hwmon;
603 }
Guenter Roeckd5601682015-08-26 19:38:11 -0700604
605 attrs = __hwmon_create_attrs(dev, drvdata, chip);
606 if (IS_ERR(attrs)) {
607 err = PTR_ERR(attrs);
608 goto free_hwmon;
609 }
610
611 hwdev->group.attrs = attrs;
612 ngroups = 0;
613 hwdev->groups[ngroups++] = &hwdev->group;
614
615 if (groups) {
616 for (i = 0; groups[i]; i++)
617 hwdev->groups[ngroups++] = groups[i];
618 }
619
620 hdev->groups = hwdev->groups;
621 } else {
622 hdev->groups = groups;
623 }
624
625 hwdev->name = name;
626 hdev->class = &hwmon_class;
627 hdev->parent = dev;
628 hdev->of_node = dev ? dev->of_node : NULL;
629 hwdev->chip = chip;
630 dev_set_drvdata(hdev, drvdata);
631 dev_set_name(hdev, HWMON_ID_FORMAT, id);
632 err = device_register(hdev);
633 if (err)
634 goto free_hwmon;
635
Guenter Roeck319fe152017-01-31 03:56:08 -0800636 if (dev && chip && chip->ops->read &&
Guenter Roeckd5601682015-08-26 19:38:11 -0700637 chip->info[0]->type == hwmon_chip &&
638 (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
639 const struct hwmon_channel_info **info = chip->info;
640
641 for (i = 1; info[i]; i++) {
642 if (info[i]->type != hwmon_temp)
643 continue;
644
645 for (j = 0; info[i]->config[j]; j++) {
646 if (!chip->ops->is_visible(drvdata, hwmon_temp,
647 hwmon_temp_input, j))
648 continue;
Linus Walleij47c332d2017-12-05 09:36:14 +0100649 if (info[i]->config[j] & HWMON_T_INPUT) {
650 err = hwmon_thermal_add_sensor(dev,
651 hwdev, j);
Dmitry Osipenko74e35122018-10-24 22:37:13 +0300652 if (err) {
653 device_unregister(hdev);
654 goto ida_remove;
655 }
Linus Walleij47c332d2017-12-05 09:36:14 +0100656 }
Guenter Roeckd5601682015-08-26 19:38:11 -0700657 }
658 }
659 }
660
661 return hdev;
662
663free_hwmon:
664 kfree(hwdev);
665ida_remove:
666 ida_simple_remove(&hwmon_ida, id);
667 return ERR_PTR(err);
668}
669
Mark M. Hoffman12364412005-07-15 21:38:08 -0400670/**
Guenter Roeckbab22432013-07-06 13:57:23 -0700671 * hwmon_device_register_with_groups - register w/ hwmon
672 * @dev: the parent device
673 * @name: hwmon name attribute
674 * @drvdata: driver data to attach to created device
675 * @groups: List of attribute groups to create
676 *
677 * hwmon_device_unregister() must be called when the device is no
678 * longer needed.
679 *
680 * Returns the pointer to the new device.
681 */
682struct device *
683hwmon_device_register_with_groups(struct device *dev, const char *name,
684 void *drvdata,
685 const struct attribute_group **groups)
686{
Guenter Roeck83538632017-01-24 06:32:57 -0800687 if (!name)
688 return ERR_PTR(-EINVAL);
689
Guenter Roeckd5601682015-08-26 19:38:11 -0700690 return __hwmon_device_register(dev, name, drvdata, NULL, groups);
Guenter Roeckbab22432013-07-06 13:57:23 -0700691}
692EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
693
694/**
Guenter Roeckd5601682015-08-26 19:38:11 -0700695 * hwmon_device_register_with_info - register w/ hwmon
696 * @dev: the parent device
697 * @name: hwmon name attribute
698 * @drvdata: driver data to attach to created device
Guenter Roeck38709452017-12-03 15:15:00 -0800699 * @chip: pointer to hwmon chip information
Guenter Roeck848ba0a2016-10-16 17:20:43 -0700700 * @extra_groups: pointer to list of additional non-standard attribute groups
Guenter Roeckd5601682015-08-26 19:38:11 -0700701 *
702 * hwmon_device_unregister() must be called when the device is no
703 * longer needed.
704 *
705 * Returns the pointer to the new device.
706 */
707struct device *
708hwmon_device_register_with_info(struct device *dev, const char *name,
709 void *drvdata,
710 const struct hwmon_chip_info *chip,
Guenter Roeck848ba0a2016-10-16 17:20:43 -0700711 const struct attribute_group **extra_groups)
Guenter Roeckd5601682015-08-26 19:38:11 -0700712{
Guenter Roeck83538632017-01-24 06:32:57 -0800713 if (!name)
714 return ERR_PTR(-EINVAL);
715
Guenter Roeck239552f2016-10-16 17:06:20 -0700716 if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info))
Guenter Roeckd5601682015-08-26 19:38:11 -0700717 return ERR_PTR(-EINVAL);
718
Lucas Magasweran59df4f42018-05-08 04:43:33 -0700719 if (chip && !dev)
720 return ERR_PTR(-EINVAL);
721
Guenter Roeck848ba0a2016-10-16 17:20:43 -0700722 return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
Guenter Roeckd5601682015-08-26 19:38:11 -0700723}
724EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
725
726/**
Tony Jones1beeffe2007-08-20 13:46:20 -0700727 * hwmon_device_register - register w/ hwmon
Mark M. Hoffman12364412005-07-15 21:38:08 -0400728 * @dev: the device to register
729 *
Tony Jones1beeffe2007-08-20 13:46:20 -0700730 * hwmon_device_unregister() must be called when the device is no
Mark M. Hoffman12364412005-07-15 21:38:08 -0400731 * longer needed.
732 *
Tony Jones1beeffe2007-08-20 13:46:20 -0700733 * Returns the pointer to the new device.
Mark M. Hoffman12364412005-07-15 21:38:08 -0400734 */
Tony Jones1beeffe2007-08-20 13:46:20 -0700735struct device *hwmon_device_register(struct device *dev)
Mark M. Hoffman12364412005-07-15 21:38:08 -0400736{
Guenter Roeckaf1bd362016-10-16 11:31:08 -0700737 dev_warn(dev,
738 "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
739
Guenter Roeck83538632017-01-24 06:32:57 -0800740 return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400741}
Frans Meulenbroeks839a9ee2012-01-08 19:34:14 +0100742EXPORT_SYMBOL_GPL(hwmon_device_register);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400743
744/**
745 * hwmon_device_unregister - removes the previously registered class device
746 *
Tony Jones1beeffe2007-08-20 13:46:20 -0700747 * @dev: the class device to destroy
Mark M. Hoffman12364412005-07-15 21:38:08 -0400748 */
Tony Jones1beeffe2007-08-20 13:46:20 -0700749void hwmon_device_unregister(struct device *dev)
Mark M. Hoffman12364412005-07-15 21:38:08 -0400750{
751 int id;
752
Kay Sievers739cf3a2009-01-06 10:44:41 -0800753 if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
Tony Jones1beeffe2007-08-20 13:46:20 -0700754 device_unregister(dev);
Jonathan Cameron4ca5f462011-10-31 17:10:09 -0700755 ida_simple_remove(&hwmon_ida, id);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400756 } else
Tony Jones1beeffe2007-08-20 13:46:20 -0700757 dev_dbg(dev->parent,
Mark M. Hoffman12364412005-07-15 21:38:08 -0400758 "hwmon_device_unregister() failed: bad class ID!\n");
759}
Frans Meulenbroeks839a9ee2012-01-08 19:34:14 +0100760EXPORT_SYMBOL_GPL(hwmon_device_unregister);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400761
Guenter Roeck74188cb2013-07-11 20:00:12 -0700762static void devm_hwmon_release(struct device *dev, void *res)
763{
764 struct device *hwdev = *(struct device **)res;
765
766 hwmon_device_unregister(hwdev);
767}
768
769/**
770 * devm_hwmon_device_register_with_groups - register w/ hwmon
771 * @dev: the parent device
772 * @name: hwmon name attribute
773 * @drvdata: driver data to attach to created device
774 * @groups: List of attribute groups to create
775 *
776 * Returns the pointer to the new device. The new device is automatically
777 * unregistered with the parent device.
778 */
779struct device *
780devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
781 void *drvdata,
782 const struct attribute_group **groups)
783{
784 struct device **ptr, *hwdev;
785
786 if (!dev)
787 return ERR_PTR(-EINVAL);
788
789 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
790 if (!ptr)
791 return ERR_PTR(-ENOMEM);
792
793 hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
794 if (IS_ERR(hwdev))
795 goto error;
796
797 *ptr = hwdev;
798 devres_add(dev, ptr);
799 return hwdev;
800
801error:
802 devres_free(ptr);
803 return hwdev;
804}
805EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
806
Guenter Roeckd5601682015-08-26 19:38:11 -0700807/**
808 * devm_hwmon_device_register_with_info - register w/ hwmon
Guenter Roeck38709452017-12-03 15:15:00 -0800809 * @dev: the parent device
810 * @name: hwmon name attribute
811 * @drvdata: driver data to attach to created device
812 * @chip: pointer to hwmon chip information
813 * @groups: pointer to list of driver specific attribute groups
Guenter Roeckd5601682015-08-26 19:38:11 -0700814 *
815 * Returns the pointer to the new device. The new device is automatically
816 * unregistered with the parent device.
817 */
818struct device *
819devm_hwmon_device_register_with_info(struct device *dev, const char *name,
820 void *drvdata,
821 const struct hwmon_chip_info *chip,
822 const struct attribute_group **groups)
823{
824 struct device **ptr, *hwdev;
825
826 if (!dev)
827 return ERR_PTR(-EINVAL);
828
829 ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
830 if (!ptr)
831 return ERR_PTR(-ENOMEM);
832
833 hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
834 groups);
835 if (IS_ERR(hwdev))
836 goto error;
837
838 *ptr = hwdev;
839 devres_add(dev, ptr);
840
841 return hwdev;
842
843error:
844 devres_free(ptr);
845 return hwdev;
846}
847EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
848
Guenter Roeck74188cb2013-07-11 20:00:12 -0700849static int devm_hwmon_match(struct device *dev, void *res, void *data)
850{
851 struct device **hwdev = res;
852
853 return *hwdev == data;
854}
855
856/**
857 * devm_hwmon_device_unregister - removes a previously registered hwmon device
858 *
859 * @dev: the parent device of the device to unregister
860 */
861void devm_hwmon_device_unregister(struct device *dev)
862{
863 WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
864}
865EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
866
Jean Delvare2958b1e2009-06-15 18:39:50 +0200867static void __init hwmon_pci_quirks(void)
868{
869#if defined CONFIG_X86 && defined CONFIG_PCI
870 struct pci_dev *sb;
871 u16 base;
872 u8 enable;
873
874 /* Open access to 0x295-0x296 on MSI MS-7031 */
875 sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
Jean Delvared6dab7d2012-12-19 22:16:59 +0100876 if (sb) {
877 if (sb->subsystem_vendor == 0x1462 && /* MSI */
878 sb->subsystem_device == 0x0031) { /* MS-7031 */
879 pci_read_config_byte(sb, 0x48, &enable);
880 pci_read_config_word(sb, 0x64, &base);
Jean Delvare2958b1e2009-06-15 18:39:50 +0200881
Jean Delvared6dab7d2012-12-19 22:16:59 +0100882 if (base == 0 && !(enable & BIT(2))) {
883 dev_info(&sb->dev,
884 "Opening wide generic port at 0x295\n");
885 pci_write_config_word(sb, 0x64, 0x295);
886 pci_write_config_byte(sb, 0x48,
887 enable | BIT(2));
888 }
Jean Delvare2958b1e2009-06-15 18:39:50 +0200889 }
Jean Delvared6dab7d2012-12-19 22:16:59 +0100890 pci_dev_put(sb);
Jean Delvare2958b1e2009-06-15 18:39:50 +0200891 }
892#endif
893}
894
Mark M. Hoffman12364412005-07-15 21:38:08 -0400895static int __init hwmon_init(void)
896{
Guenter Roeckbab22432013-07-06 13:57:23 -0700897 int err;
898
Jean Delvare2958b1e2009-06-15 18:39:50 +0200899 hwmon_pci_quirks();
900
Guenter Roeckbab22432013-07-06 13:57:23 -0700901 err = class_register(&hwmon_class);
902 if (err) {
903 pr_err("couldn't register hwmon sysfs class\n");
904 return err;
Mark M. Hoffman12364412005-07-15 21:38:08 -0400905 }
906 return 0;
907}
908
909static void __exit hwmon_exit(void)
910{
Guenter Roeckbab22432013-07-06 13:57:23 -0700911 class_unregister(&hwmon_class);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400912}
913
David Brownell37f54ee2007-02-14 21:15:04 +0100914subsys_initcall(hwmon_init);
Mark M. Hoffman12364412005-07-15 21:38:08 -0400915module_exit(hwmon_exit);
916
Mark M. Hoffman12364412005-07-15 21:38:08 -0400917MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
918MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
919MODULE_LICENSE("GPL");
920