xref: /openbmc/linux/drivers/hwmon/hwmon.c (revision dd21bfa4)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * hwmon.c - part of lm_sensors, Linux kernel modules for hardware monitoring
4  *
5  * This file defines the sysfs class "hwmon", for use by sensors drivers.
6  *
7  * Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com>
8  */
9 
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 
12 #include <linux/bitops.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/gfp.h>
16 #include <linux/hwmon.h>
17 #include <linux/idr.h>
18 #include <linux/list.h>
19 #include <linux/module.h>
20 #include <linux/pci.h>
21 #include <linux/slab.h>
22 #include <linux/string.h>
23 #include <linux/thermal.h>
24 
25 #define CREATE_TRACE_POINTS
26 #include <trace/events/hwmon.h>
27 
28 #define HWMON_ID_PREFIX "hwmon"
29 #define HWMON_ID_FORMAT HWMON_ID_PREFIX "%d"
30 
31 struct hwmon_device {
32 	const char *name;
33 	struct device dev;
34 	const struct hwmon_chip_info *chip;
35 	struct list_head tzdata;
36 	struct attribute_group group;
37 	const struct attribute_group **groups;
38 };
39 
40 #define to_hwmon_device(d) container_of(d, struct hwmon_device, dev)
41 
42 #define MAX_SYSFS_ATTR_NAME_LENGTH	32
43 
44 struct hwmon_device_attribute {
45 	struct device_attribute dev_attr;
46 	const struct hwmon_ops *ops;
47 	enum hwmon_sensor_types type;
48 	u32 attr;
49 	int index;
50 	char name[MAX_SYSFS_ATTR_NAME_LENGTH];
51 };
52 
53 #define to_hwmon_attr(d) \
54 	container_of(d, struct hwmon_device_attribute, dev_attr)
55 #define to_dev_attr(a) container_of(a, struct device_attribute, attr)
56 
57 /*
58  * Thermal zone information
59  */
60 struct hwmon_thermal_data {
61 	struct list_head node;		/* hwmon tzdata list entry */
62 	struct device *dev;		/* Reference to hwmon device */
63 	int index;			/* sensor index */
64 	struct thermal_zone_device *tzd;/* thermal zone device */
65 };
66 
67 static ssize_t
68 name_show(struct device *dev, struct device_attribute *attr, char *buf)
69 {
70 	return sprintf(buf, "%s\n", to_hwmon_device(dev)->name);
71 }
72 static DEVICE_ATTR_RO(name);
73 
74 static struct attribute *hwmon_dev_attrs[] = {
75 	&dev_attr_name.attr,
76 	NULL
77 };
78 
79 static umode_t hwmon_dev_name_is_visible(struct kobject *kobj,
80 					 struct attribute *attr, int n)
81 {
82 	struct device *dev = kobj_to_dev(kobj);
83 
84 	if (to_hwmon_device(dev)->name == NULL)
85 		return 0;
86 
87 	return attr->mode;
88 }
89 
90 static const struct attribute_group hwmon_dev_attr_group = {
91 	.attrs		= hwmon_dev_attrs,
92 	.is_visible	= hwmon_dev_name_is_visible,
93 };
94 
95 static const struct attribute_group *hwmon_dev_attr_groups[] = {
96 	&hwmon_dev_attr_group,
97 	NULL
98 };
99 
100 static void hwmon_free_attrs(struct attribute **attrs)
101 {
102 	int i;
103 
104 	for (i = 0; attrs[i]; i++) {
105 		struct device_attribute *dattr = to_dev_attr(attrs[i]);
106 		struct hwmon_device_attribute *hattr = to_hwmon_attr(dattr);
107 
108 		kfree(hattr);
109 	}
110 	kfree(attrs);
111 }
112 
113 static void hwmon_dev_release(struct device *dev)
114 {
115 	struct hwmon_device *hwdev = to_hwmon_device(dev);
116 
117 	if (hwdev->group.attrs)
118 		hwmon_free_attrs(hwdev->group.attrs);
119 	kfree(hwdev->groups);
120 	kfree(hwdev);
121 }
122 
123 static struct class hwmon_class = {
124 	.name = "hwmon",
125 	.owner = THIS_MODULE,
126 	.dev_groups = hwmon_dev_attr_groups,
127 	.dev_release = hwmon_dev_release,
128 };
129 
130 static DEFINE_IDA(hwmon_ida);
131 
132 /* Thermal zone handling */
133 
134 /*
135  * The complex conditional is necessary to avoid a cyclic dependency
136  * between hwmon and thermal_sys modules.
137  */
138 #ifdef CONFIG_THERMAL_OF
139 static int hwmon_thermal_get_temp(void *data, int *temp)
140 {
141 	struct hwmon_thermal_data *tdata = data;
142 	struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
143 	int ret;
144 	long t;
145 
146 	ret = hwdev->chip->ops->read(tdata->dev, hwmon_temp, hwmon_temp_input,
147 				     tdata->index, &t);
148 	if (ret < 0)
149 		return ret;
150 
151 	*temp = t;
152 
153 	return 0;
154 }
155 
156 static int hwmon_thermal_set_trips(void *data, int low, int high)
157 {
158 	struct hwmon_thermal_data *tdata = data;
159 	struct hwmon_device *hwdev = to_hwmon_device(tdata->dev);
160 	const struct hwmon_chip_info *chip = hwdev->chip;
161 	const struct hwmon_channel_info **info = chip->info;
162 	unsigned int i;
163 	int err;
164 
165 	if (!chip->ops->write)
166 		return 0;
167 
168 	for (i = 0; info[i] && info[i]->type != hwmon_temp; i++)
169 		continue;
170 
171 	if (!info[i])
172 		return 0;
173 
174 	if (info[i]->config[tdata->index] & HWMON_T_MIN) {
175 		err = chip->ops->write(tdata->dev, hwmon_temp,
176 				       hwmon_temp_min, tdata->index, low);
177 		if (err && err != -EOPNOTSUPP)
178 			return err;
179 	}
180 
181 	if (info[i]->config[tdata->index] & HWMON_T_MAX) {
182 		err = chip->ops->write(tdata->dev, hwmon_temp,
183 				       hwmon_temp_max, tdata->index, high);
184 		if (err && err != -EOPNOTSUPP)
185 			return err;
186 	}
187 
188 	return 0;
189 }
190 
191 static const struct thermal_zone_of_device_ops hwmon_thermal_ops = {
192 	.get_temp = hwmon_thermal_get_temp,
193 	.set_trips = hwmon_thermal_set_trips,
194 };
195 
196 static void hwmon_thermal_remove_sensor(void *data)
197 {
198 	list_del(data);
199 }
200 
201 static int hwmon_thermal_add_sensor(struct device *dev, int index)
202 {
203 	struct hwmon_device *hwdev = to_hwmon_device(dev);
204 	struct hwmon_thermal_data *tdata;
205 	struct thermal_zone_device *tzd;
206 	int err;
207 
208 	tdata = devm_kzalloc(dev, sizeof(*tdata), GFP_KERNEL);
209 	if (!tdata)
210 		return -ENOMEM;
211 
212 	tdata->dev = dev;
213 	tdata->index = index;
214 
215 	tzd = devm_thermal_zone_of_sensor_register(dev, index, tdata,
216 						   &hwmon_thermal_ops);
217 	if (IS_ERR(tzd)) {
218 		if (PTR_ERR(tzd) != -ENODEV)
219 			return PTR_ERR(tzd);
220 		dev_info(dev, "temp%d_input not attached to any thermal zone\n",
221 			 index + 1);
222 		devm_kfree(dev, tdata);
223 		return 0;
224 	}
225 
226 	err = devm_add_action(dev, hwmon_thermal_remove_sensor, &tdata->node);
227 	if (err)
228 		return err;
229 
230 	tdata->tzd = tzd;
231 	list_add(&tdata->node, &hwdev->tzdata);
232 
233 	return 0;
234 }
235 
236 static int hwmon_thermal_register_sensors(struct device *dev)
237 {
238 	struct hwmon_device *hwdev = to_hwmon_device(dev);
239 	const struct hwmon_chip_info *chip = hwdev->chip;
240 	const struct hwmon_channel_info **info = chip->info;
241 	void *drvdata = dev_get_drvdata(dev);
242 	int i;
243 
244 	for (i = 1; info[i]; i++) {
245 		int j;
246 
247 		if (info[i]->type != hwmon_temp)
248 			continue;
249 
250 		for (j = 0; info[i]->config[j]; j++) {
251 			int err;
252 
253 			if (!(info[i]->config[j] & HWMON_T_INPUT) ||
254 			    !chip->ops->is_visible(drvdata, hwmon_temp,
255 						   hwmon_temp_input, j))
256 				continue;
257 
258 			err = hwmon_thermal_add_sensor(dev, j);
259 			if (err)
260 				return err;
261 		}
262 	}
263 
264 	return 0;
265 }
266 
267 static void hwmon_thermal_notify(struct device *dev, int index)
268 {
269 	struct hwmon_device *hwdev = to_hwmon_device(dev);
270 	struct hwmon_thermal_data *tzdata;
271 
272 	list_for_each_entry(tzdata, &hwdev->tzdata, node) {
273 		if (tzdata->index == index) {
274 			thermal_zone_device_update(tzdata->tzd,
275 						   THERMAL_EVENT_UNSPECIFIED);
276 		}
277 	}
278 }
279 
280 #else
281 static int hwmon_thermal_register_sensors(struct device *dev)
282 {
283 	return 0;
284 }
285 
286 static void hwmon_thermal_notify(struct device *dev, int index) { }
287 
288 #endif /* IS_REACHABLE(CONFIG_THERMAL) && ... */
289 
290 static int hwmon_attr_base(enum hwmon_sensor_types type)
291 {
292 	if (type == hwmon_in || type == hwmon_intrusion)
293 		return 0;
294 	return 1;
295 }
296 
297 /* sysfs attribute management */
298 
299 static ssize_t hwmon_attr_show(struct device *dev,
300 			       struct device_attribute *devattr, char *buf)
301 {
302 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
303 	long val;
304 	int ret;
305 
306 	ret = hattr->ops->read(dev, hattr->type, hattr->attr, hattr->index,
307 			       &val);
308 	if (ret < 0)
309 		return ret;
310 
311 	trace_hwmon_attr_show(hattr->index + hwmon_attr_base(hattr->type),
312 			      hattr->name, val);
313 
314 	return sprintf(buf, "%ld\n", val);
315 }
316 
317 static ssize_t hwmon_attr_show_string(struct device *dev,
318 				      struct device_attribute *devattr,
319 				      char *buf)
320 {
321 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
322 	enum hwmon_sensor_types type = hattr->type;
323 	const char *s;
324 	int ret;
325 
326 	ret = hattr->ops->read_string(dev, hattr->type, hattr->attr,
327 				      hattr->index, &s);
328 	if (ret < 0)
329 		return ret;
330 
331 	trace_hwmon_attr_show_string(hattr->index + hwmon_attr_base(type),
332 				     hattr->name, s);
333 
334 	return sprintf(buf, "%s\n", s);
335 }
336 
337 static ssize_t hwmon_attr_store(struct device *dev,
338 				struct device_attribute *devattr,
339 				const char *buf, size_t count)
340 {
341 	struct hwmon_device_attribute *hattr = to_hwmon_attr(devattr);
342 	long val;
343 	int ret;
344 
345 	ret = kstrtol(buf, 10, &val);
346 	if (ret < 0)
347 		return ret;
348 
349 	ret = hattr->ops->write(dev, hattr->type, hattr->attr, hattr->index,
350 				val);
351 	if (ret < 0)
352 		return ret;
353 
354 	trace_hwmon_attr_store(hattr->index + hwmon_attr_base(hattr->type),
355 			       hattr->name, val);
356 
357 	return count;
358 }
359 
360 static bool is_string_attr(enum hwmon_sensor_types type, u32 attr)
361 {
362 	return (type == hwmon_temp && attr == hwmon_temp_label) ||
363 	       (type == hwmon_in && attr == hwmon_in_label) ||
364 	       (type == hwmon_curr && attr == hwmon_curr_label) ||
365 	       (type == hwmon_power && attr == hwmon_power_label) ||
366 	       (type == hwmon_energy && attr == hwmon_energy_label) ||
367 	       (type == hwmon_humidity && attr == hwmon_humidity_label) ||
368 	       (type == hwmon_fan && attr == hwmon_fan_label);
369 }
370 
371 static struct attribute *hwmon_genattr(const void *drvdata,
372 				       enum hwmon_sensor_types type,
373 				       u32 attr,
374 				       int index,
375 				       const char *template,
376 				       const struct hwmon_ops *ops)
377 {
378 	struct hwmon_device_attribute *hattr;
379 	struct device_attribute *dattr;
380 	struct attribute *a;
381 	umode_t mode;
382 	const char *name;
383 	bool is_string = is_string_attr(type, attr);
384 
385 	/* The attribute is invisible if there is no template string */
386 	if (!template)
387 		return ERR_PTR(-ENOENT);
388 
389 	mode = ops->is_visible(drvdata, type, attr, index);
390 	if (!mode)
391 		return ERR_PTR(-ENOENT);
392 
393 	if ((mode & 0444) && ((is_string && !ops->read_string) ||
394 				 (!is_string && !ops->read)))
395 		return ERR_PTR(-EINVAL);
396 	if ((mode & 0222) && !ops->write)
397 		return ERR_PTR(-EINVAL);
398 
399 	hattr = kzalloc(sizeof(*hattr), GFP_KERNEL);
400 	if (!hattr)
401 		return ERR_PTR(-ENOMEM);
402 
403 	if (type == hwmon_chip) {
404 		name = template;
405 	} else {
406 		scnprintf(hattr->name, sizeof(hattr->name), template,
407 			  index + hwmon_attr_base(type));
408 		name = hattr->name;
409 	}
410 
411 	hattr->type = type;
412 	hattr->attr = attr;
413 	hattr->index = index;
414 	hattr->ops = ops;
415 
416 	dattr = &hattr->dev_attr;
417 	dattr->show = is_string ? hwmon_attr_show_string : hwmon_attr_show;
418 	dattr->store = hwmon_attr_store;
419 
420 	a = &dattr->attr;
421 	sysfs_attr_init(a);
422 	a->name = name;
423 	a->mode = mode;
424 
425 	return a;
426 }
427 
428 /*
429  * Chip attributes are not attribute templates but actual sysfs attributes.
430  * See hwmon_genattr() for special handling.
431  */
432 static const char * const hwmon_chip_attrs[] = {
433 	[hwmon_chip_temp_reset_history] = "temp_reset_history",
434 	[hwmon_chip_in_reset_history] = "in_reset_history",
435 	[hwmon_chip_curr_reset_history] = "curr_reset_history",
436 	[hwmon_chip_power_reset_history] = "power_reset_history",
437 	[hwmon_chip_update_interval] = "update_interval",
438 	[hwmon_chip_alarms] = "alarms",
439 	[hwmon_chip_samples] = "samples",
440 	[hwmon_chip_curr_samples] = "curr_samples",
441 	[hwmon_chip_in_samples] = "in_samples",
442 	[hwmon_chip_power_samples] = "power_samples",
443 	[hwmon_chip_temp_samples] = "temp_samples",
444 };
445 
446 static const char * const hwmon_temp_attr_templates[] = {
447 	[hwmon_temp_enable] = "temp%d_enable",
448 	[hwmon_temp_input] = "temp%d_input",
449 	[hwmon_temp_type] = "temp%d_type",
450 	[hwmon_temp_lcrit] = "temp%d_lcrit",
451 	[hwmon_temp_lcrit_hyst] = "temp%d_lcrit_hyst",
452 	[hwmon_temp_min] = "temp%d_min",
453 	[hwmon_temp_min_hyst] = "temp%d_min_hyst",
454 	[hwmon_temp_max] = "temp%d_max",
455 	[hwmon_temp_max_hyst] = "temp%d_max_hyst",
456 	[hwmon_temp_crit] = "temp%d_crit",
457 	[hwmon_temp_crit_hyst] = "temp%d_crit_hyst",
458 	[hwmon_temp_emergency] = "temp%d_emergency",
459 	[hwmon_temp_emergency_hyst] = "temp%d_emergency_hyst",
460 	[hwmon_temp_alarm] = "temp%d_alarm",
461 	[hwmon_temp_lcrit_alarm] = "temp%d_lcrit_alarm",
462 	[hwmon_temp_min_alarm] = "temp%d_min_alarm",
463 	[hwmon_temp_max_alarm] = "temp%d_max_alarm",
464 	[hwmon_temp_crit_alarm] = "temp%d_crit_alarm",
465 	[hwmon_temp_emergency_alarm] = "temp%d_emergency_alarm",
466 	[hwmon_temp_fault] = "temp%d_fault",
467 	[hwmon_temp_offset] = "temp%d_offset",
468 	[hwmon_temp_label] = "temp%d_label",
469 	[hwmon_temp_lowest] = "temp%d_lowest",
470 	[hwmon_temp_highest] = "temp%d_highest",
471 	[hwmon_temp_reset_history] = "temp%d_reset_history",
472 	[hwmon_temp_rated_min] = "temp%d_rated_min",
473 	[hwmon_temp_rated_max] = "temp%d_rated_max",
474 };
475 
476 static const char * const hwmon_in_attr_templates[] = {
477 	[hwmon_in_enable] = "in%d_enable",
478 	[hwmon_in_input] = "in%d_input",
479 	[hwmon_in_min] = "in%d_min",
480 	[hwmon_in_max] = "in%d_max",
481 	[hwmon_in_lcrit] = "in%d_lcrit",
482 	[hwmon_in_crit] = "in%d_crit",
483 	[hwmon_in_average] = "in%d_average",
484 	[hwmon_in_lowest] = "in%d_lowest",
485 	[hwmon_in_highest] = "in%d_highest",
486 	[hwmon_in_reset_history] = "in%d_reset_history",
487 	[hwmon_in_label] = "in%d_label",
488 	[hwmon_in_alarm] = "in%d_alarm",
489 	[hwmon_in_min_alarm] = "in%d_min_alarm",
490 	[hwmon_in_max_alarm] = "in%d_max_alarm",
491 	[hwmon_in_lcrit_alarm] = "in%d_lcrit_alarm",
492 	[hwmon_in_crit_alarm] = "in%d_crit_alarm",
493 	[hwmon_in_rated_min] = "in%d_rated_min",
494 	[hwmon_in_rated_max] = "in%d_rated_max",
495 };
496 
497 static const char * const hwmon_curr_attr_templates[] = {
498 	[hwmon_curr_enable] = "curr%d_enable",
499 	[hwmon_curr_input] = "curr%d_input",
500 	[hwmon_curr_min] = "curr%d_min",
501 	[hwmon_curr_max] = "curr%d_max",
502 	[hwmon_curr_lcrit] = "curr%d_lcrit",
503 	[hwmon_curr_crit] = "curr%d_crit",
504 	[hwmon_curr_average] = "curr%d_average",
505 	[hwmon_curr_lowest] = "curr%d_lowest",
506 	[hwmon_curr_highest] = "curr%d_highest",
507 	[hwmon_curr_reset_history] = "curr%d_reset_history",
508 	[hwmon_curr_label] = "curr%d_label",
509 	[hwmon_curr_alarm] = "curr%d_alarm",
510 	[hwmon_curr_min_alarm] = "curr%d_min_alarm",
511 	[hwmon_curr_max_alarm] = "curr%d_max_alarm",
512 	[hwmon_curr_lcrit_alarm] = "curr%d_lcrit_alarm",
513 	[hwmon_curr_crit_alarm] = "curr%d_crit_alarm",
514 	[hwmon_curr_rated_min] = "curr%d_rated_min",
515 	[hwmon_curr_rated_max] = "curr%d_rated_max",
516 };
517 
518 static const char * const hwmon_power_attr_templates[] = {
519 	[hwmon_power_enable] = "power%d_enable",
520 	[hwmon_power_average] = "power%d_average",
521 	[hwmon_power_average_interval] = "power%d_average_interval",
522 	[hwmon_power_average_interval_max] = "power%d_interval_max",
523 	[hwmon_power_average_interval_min] = "power%d_interval_min",
524 	[hwmon_power_average_highest] = "power%d_average_highest",
525 	[hwmon_power_average_lowest] = "power%d_average_lowest",
526 	[hwmon_power_average_max] = "power%d_average_max",
527 	[hwmon_power_average_min] = "power%d_average_min",
528 	[hwmon_power_input] = "power%d_input",
529 	[hwmon_power_input_highest] = "power%d_input_highest",
530 	[hwmon_power_input_lowest] = "power%d_input_lowest",
531 	[hwmon_power_reset_history] = "power%d_reset_history",
532 	[hwmon_power_accuracy] = "power%d_accuracy",
533 	[hwmon_power_cap] = "power%d_cap",
534 	[hwmon_power_cap_hyst] = "power%d_cap_hyst",
535 	[hwmon_power_cap_max] = "power%d_cap_max",
536 	[hwmon_power_cap_min] = "power%d_cap_min",
537 	[hwmon_power_min] = "power%d_min",
538 	[hwmon_power_max] = "power%d_max",
539 	[hwmon_power_lcrit] = "power%d_lcrit",
540 	[hwmon_power_crit] = "power%d_crit",
541 	[hwmon_power_label] = "power%d_label",
542 	[hwmon_power_alarm] = "power%d_alarm",
543 	[hwmon_power_cap_alarm] = "power%d_cap_alarm",
544 	[hwmon_power_min_alarm] = "power%d_min_alarm",
545 	[hwmon_power_max_alarm] = "power%d_max_alarm",
546 	[hwmon_power_lcrit_alarm] = "power%d_lcrit_alarm",
547 	[hwmon_power_crit_alarm] = "power%d_crit_alarm",
548 	[hwmon_power_rated_min] = "power%d_rated_min",
549 	[hwmon_power_rated_max] = "power%d_rated_max",
550 };
551 
552 static const char * const hwmon_energy_attr_templates[] = {
553 	[hwmon_energy_enable] = "energy%d_enable",
554 	[hwmon_energy_input] = "energy%d_input",
555 	[hwmon_energy_label] = "energy%d_label",
556 };
557 
558 static const char * const hwmon_humidity_attr_templates[] = {
559 	[hwmon_humidity_enable] = "humidity%d_enable",
560 	[hwmon_humidity_input] = "humidity%d_input",
561 	[hwmon_humidity_label] = "humidity%d_label",
562 	[hwmon_humidity_min] = "humidity%d_min",
563 	[hwmon_humidity_min_hyst] = "humidity%d_min_hyst",
564 	[hwmon_humidity_max] = "humidity%d_max",
565 	[hwmon_humidity_max_hyst] = "humidity%d_max_hyst",
566 	[hwmon_humidity_alarm] = "humidity%d_alarm",
567 	[hwmon_humidity_fault] = "humidity%d_fault",
568 	[hwmon_humidity_rated_min] = "humidity%d_rated_min",
569 	[hwmon_humidity_rated_max] = "humidity%d_rated_max",
570 };
571 
572 static const char * const hwmon_fan_attr_templates[] = {
573 	[hwmon_fan_enable] = "fan%d_enable",
574 	[hwmon_fan_input] = "fan%d_input",
575 	[hwmon_fan_label] = "fan%d_label",
576 	[hwmon_fan_min] = "fan%d_min",
577 	[hwmon_fan_max] = "fan%d_max",
578 	[hwmon_fan_div] = "fan%d_div",
579 	[hwmon_fan_pulses] = "fan%d_pulses",
580 	[hwmon_fan_target] = "fan%d_target",
581 	[hwmon_fan_alarm] = "fan%d_alarm",
582 	[hwmon_fan_min_alarm] = "fan%d_min_alarm",
583 	[hwmon_fan_max_alarm] = "fan%d_max_alarm",
584 	[hwmon_fan_fault] = "fan%d_fault",
585 };
586 
587 static const char * const hwmon_pwm_attr_templates[] = {
588 	[hwmon_pwm_input] = "pwm%d",
589 	[hwmon_pwm_enable] = "pwm%d_enable",
590 	[hwmon_pwm_mode] = "pwm%d_mode",
591 	[hwmon_pwm_freq] = "pwm%d_freq",
592 };
593 
594 static const char * const hwmon_intrusion_attr_templates[] = {
595 	[hwmon_intrusion_alarm] = "intrusion%d_alarm",
596 	[hwmon_intrusion_beep]  = "intrusion%d_beep",
597 };
598 
599 static const char * const *__templates[] = {
600 	[hwmon_chip] = hwmon_chip_attrs,
601 	[hwmon_temp] = hwmon_temp_attr_templates,
602 	[hwmon_in] = hwmon_in_attr_templates,
603 	[hwmon_curr] = hwmon_curr_attr_templates,
604 	[hwmon_power] = hwmon_power_attr_templates,
605 	[hwmon_energy] = hwmon_energy_attr_templates,
606 	[hwmon_humidity] = hwmon_humidity_attr_templates,
607 	[hwmon_fan] = hwmon_fan_attr_templates,
608 	[hwmon_pwm] = hwmon_pwm_attr_templates,
609 	[hwmon_intrusion] = hwmon_intrusion_attr_templates,
610 };
611 
612 static const int __templates_size[] = {
613 	[hwmon_chip] = ARRAY_SIZE(hwmon_chip_attrs),
614 	[hwmon_temp] = ARRAY_SIZE(hwmon_temp_attr_templates),
615 	[hwmon_in] = ARRAY_SIZE(hwmon_in_attr_templates),
616 	[hwmon_curr] = ARRAY_SIZE(hwmon_curr_attr_templates),
617 	[hwmon_power] = ARRAY_SIZE(hwmon_power_attr_templates),
618 	[hwmon_energy] = ARRAY_SIZE(hwmon_energy_attr_templates),
619 	[hwmon_humidity] = ARRAY_SIZE(hwmon_humidity_attr_templates),
620 	[hwmon_fan] = ARRAY_SIZE(hwmon_fan_attr_templates),
621 	[hwmon_pwm] = ARRAY_SIZE(hwmon_pwm_attr_templates),
622 	[hwmon_intrusion] = ARRAY_SIZE(hwmon_intrusion_attr_templates),
623 };
624 
625 int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type,
626 		       u32 attr, int channel)
627 {
628 	char sattr[MAX_SYSFS_ATTR_NAME_LENGTH];
629 	const char * const *templates;
630 	const char *template;
631 	int base;
632 
633 	if (type >= ARRAY_SIZE(__templates))
634 		return -EINVAL;
635 	if (attr >= __templates_size[type])
636 		return -EINVAL;
637 
638 	templates = __templates[type];
639 	template = templates[attr];
640 
641 	base = hwmon_attr_base(type);
642 
643 	scnprintf(sattr, MAX_SYSFS_ATTR_NAME_LENGTH, template, base + channel);
644 	sysfs_notify(&dev->kobj, NULL, sattr);
645 	kobject_uevent(&dev->kobj, KOBJ_CHANGE);
646 
647 	if (type == hwmon_temp)
648 		hwmon_thermal_notify(dev, channel);
649 
650 	return 0;
651 }
652 EXPORT_SYMBOL_GPL(hwmon_notify_event);
653 
654 static int hwmon_num_channel_attrs(const struct hwmon_channel_info *info)
655 {
656 	int i, n;
657 
658 	for (i = n = 0; info->config[i]; i++)
659 		n += hweight32(info->config[i]);
660 
661 	return n;
662 }
663 
664 static int hwmon_genattrs(const void *drvdata,
665 			  struct attribute **attrs,
666 			  const struct hwmon_ops *ops,
667 			  const struct hwmon_channel_info *info)
668 {
669 	const char * const *templates;
670 	int template_size;
671 	int i, aindex = 0;
672 
673 	if (info->type >= ARRAY_SIZE(__templates))
674 		return -EINVAL;
675 
676 	templates = __templates[info->type];
677 	template_size = __templates_size[info->type];
678 
679 	for (i = 0; info->config[i]; i++) {
680 		u32 attr_mask = info->config[i];
681 		u32 attr;
682 
683 		while (attr_mask) {
684 			struct attribute *a;
685 
686 			attr = __ffs(attr_mask);
687 			attr_mask &= ~BIT(attr);
688 			if (attr >= template_size)
689 				return -EINVAL;
690 			a = hwmon_genattr(drvdata, info->type, attr, i,
691 					  templates[attr], ops);
692 			if (IS_ERR(a)) {
693 				if (PTR_ERR(a) != -ENOENT)
694 					return PTR_ERR(a);
695 				continue;
696 			}
697 			attrs[aindex++] = a;
698 		}
699 	}
700 	return aindex;
701 }
702 
703 static struct attribute **
704 __hwmon_create_attrs(const void *drvdata, const struct hwmon_chip_info *chip)
705 {
706 	int ret, i, aindex = 0, nattrs = 0;
707 	struct attribute **attrs;
708 
709 	for (i = 0; chip->info[i]; i++)
710 		nattrs += hwmon_num_channel_attrs(chip->info[i]);
711 
712 	if (nattrs == 0)
713 		return ERR_PTR(-EINVAL);
714 
715 	attrs = kcalloc(nattrs + 1, sizeof(*attrs), GFP_KERNEL);
716 	if (!attrs)
717 		return ERR_PTR(-ENOMEM);
718 
719 	for (i = 0; chip->info[i]; i++) {
720 		ret = hwmon_genattrs(drvdata, &attrs[aindex], chip->ops,
721 				     chip->info[i]);
722 		if (ret < 0) {
723 			hwmon_free_attrs(attrs);
724 			return ERR_PTR(ret);
725 		}
726 		aindex += ret;
727 	}
728 
729 	return attrs;
730 }
731 
732 static struct device *
733 __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
734 			const struct hwmon_chip_info *chip,
735 			const struct attribute_group **groups)
736 {
737 	struct hwmon_device *hwdev;
738 	struct device *hdev;
739 	int i, err, id;
740 
741 	/* Complain about invalid characters in hwmon name attribute */
742 	if (name && (!strlen(name) || strpbrk(name, "-* \t\n")))
743 		dev_warn(dev,
744 			 "hwmon: '%s' is not a valid name attribute, please fix\n",
745 			 name);
746 
747 	id = ida_simple_get(&hwmon_ida, 0, 0, GFP_KERNEL);
748 	if (id < 0)
749 		return ERR_PTR(id);
750 
751 	hwdev = kzalloc(sizeof(*hwdev), GFP_KERNEL);
752 	if (hwdev == NULL) {
753 		err = -ENOMEM;
754 		goto ida_remove;
755 	}
756 
757 	hdev = &hwdev->dev;
758 
759 	if (chip) {
760 		struct attribute **attrs;
761 		int ngroups = 2; /* terminating NULL plus &hwdev->groups */
762 
763 		if (groups)
764 			for (i = 0; groups[i]; i++)
765 				ngroups++;
766 
767 		hwdev->groups = kcalloc(ngroups, sizeof(*groups), GFP_KERNEL);
768 		if (!hwdev->groups) {
769 			err = -ENOMEM;
770 			goto free_hwmon;
771 		}
772 
773 		attrs = __hwmon_create_attrs(drvdata, chip);
774 		if (IS_ERR(attrs)) {
775 			err = PTR_ERR(attrs);
776 			goto free_hwmon;
777 		}
778 
779 		hwdev->group.attrs = attrs;
780 		ngroups = 0;
781 		hwdev->groups[ngroups++] = &hwdev->group;
782 
783 		if (groups) {
784 			for (i = 0; groups[i]; i++)
785 				hwdev->groups[ngroups++] = groups[i];
786 		}
787 
788 		hdev->groups = hwdev->groups;
789 	} else {
790 		hdev->groups = groups;
791 	}
792 
793 	hwdev->name = name;
794 	hdev->class = &hwmon_class;
795 	hdev->parent = dev;
796 	hdev->of_node = dev ? dev->of_node : NULL;
797 	hwdev->chip = chip;
798 	dev_set_drvdata(hdev, drvdata);
799 	dev_set_name(hdev, HWMON_ID_FORMAT, id);
800 	err = device_register(hdev);
801 	if (err) {
802 		put_device(hdev);
803 		goto ida_remove;
804 	}
805 
806 	INIT_LIST_HEAD(&hwdev->tzdata);
807 
808 	if (dev && dev->of_node && chip && chip->ops->read &&
809 	    chip->info[0]->type == hwmon_chip &&
810 	    (chip->info[0]->config[0] & HWMON_C_REGISTER_TZ)) {
811 		err = hwmon_thermal_register_sensors(hdev);
812 		if (err) {
813 			device_unregister(hdev);
814 			/*
815 			 * Don't worry about hwdev; hwmon_dev_release(), called
816 			 * from device_unregister(), will free it.
817 			 */
818 			goto ida_remove;
819 		}
820 	}
821 
822 	return hdev;
823 
824 free_hwmon:
825 	hwmon_dev_release(hdev);
826 ida_remove:
827 	ida_simple_remove(&hwmon_ida, id);
828 	return ERR_PTR(err);
829 }
830 
831 /**
832  * hwmon_device_register_with_groups - register w/ hwmon
833  * @dev: the parent device
834  * @name: hwmon name attribute
835  * @drvdata: driver data to attach to created device
836  * @groups: List of attribute groups to create
837  *
838  * hwmon_device_unregister() must be called when the device is no
839  * longer needed.
840  *
841  * Returns the pointer to the new device.
842  */
843 struct device *
844 hwmon_device_register_with_groups(struct device *dev, const char *name,
845 				  void *drvdata,
846 				  const struct attribute_group **groups)
847 {
848 	if (!name)
849 		return ERR_PTR(-EINVAL);
850 
851 	return __hwmon_device_register(dev, name, drvdata, NULL, groups);
852 }
853 EXPORT_SYMBOL_GPL(hwmon_device_register_with_groups);
854 
855 /**
856  * hwmon_device_register_with_info - register w/ hwmon
857  * @dev: the parent device
858  * @name: hwmon name attribute
859  * @drvdata: driver data to attach to created device
860  * @chip: pointer to hwmon chip information
861  * @extra_groups: pointer to list of additional non-standard attribute groups
862  *
863  * hwmon_device_unregister() must be called when the device is no
864  * longer needed.
865  *
866  * Returns the pointer to the new device.
867  */
868 struct device *
869 hwmon_device_register_with_info(struct device *dev, const char *name,
870 				void *drvdata,
871 				const struct hwmon_chip_info *chip,
872 				const struct attribute_group **extra_groups)
873 {
874 	if (!name)
875 		return ERR_PTR(-EINVAL);
876 
877 	if (chip && (!chip->ops || !chip->ops->is_visible || !chip->info))
878 		return ERR_PTR(-EINVAL);
879 
880 	if (chip && !dev)
881 		return ERR_PTR(-EINVAL);
882 
883 	return __hwmon_device_register(dev, name, drvdata, chip, extra_groups);
884 }
885 EXPORT_SYMBOL_GPL(hwmon_device_register_with_info);
886 
887 /**
888  * hwmon_device_register - register w/ hwmon
889  * @dev: the device to register
890  *
891  * hwmon_device_unregister() must be called when the device is no
892  * longer needed.
893  *
894  * Returns the pointer to the new device.
895  */
896 struct device *hwmon_device_register(struct device *dev)
897 {
898 	dev_warn(dev,
899 		 "hwmon_device_register() is deprecated. Please convert the driver to use hwmon_device_register_with_info().\n");
900 
901 	return __hwmon_device_register(dev, NULL, NULL, NULL, NULL);
902 }
903 EXPORT_SYMBOL_GPL(hwmon_device_register);
904 
905 /**
906  * hwmon_device_unregister - removes the previously registered class device
907  *
908  * @dev: the class device to destroy
909  */
910 void hwmon_device_unregister(struct device *dev)
911 {
912 	int id;
913 
914 	if (likely(sscanf(dev_name(dev), HWMON_ID_FORMAT, &id) == 1)) {
915 		device_unregister(dev);
916 		ida_simple_remove(&hwmon_ida, id);
917 	} else
918 		dev_dbg(dev->parent,
919 			"hwmon_device_unregister() failed: bad class ID!\n");
920 }
921 EXPORT_SYMBOL_GPL(hwmon_device_unregister);
922 
923 static void devm_hwmon_release(struct device *dev, void *res)
924 {
925 	struct device *hwdev = *(struct device **)res;
926 
927 	hwmon_device_unregister(hwdev);
928 }
929 
930 /**
931  * devm_hwmon_device_register_with_groups - register w/ hwmon
932  * @dev: the parent device
933  * @name: hwmon name attribute
934  * @drvdata: driver data to attach to created device
935  * @groups: List of attribute groups to create
936  *
937  * Returns the pointer to the new device. The new device is automatically
938  * unregistered with the parent device.
939  */
940 struct device *
941 devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
942 				       void *drvdata,
943 				       const struct attribute_group **groups)
944 {
945 	struct device **ptr, *hwdev;
946 
947 	if (!dev)
948 		return ERR_PTR(-EINVAL);
949 
950 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
951 	if (!ptr)
952 		return ERR_PTR(-ENOMEM);
953 
954 	hwdev = hwmon_device_register_with_groups(dev, name, drvdata, groups);
955 	if (IS_ERR(hwdev))
956 		goto error;
957 
958 	*ptr = hwdev;
959 	devres_add(dev, ptr);
960 	return hwdev;
961 
962 error:
963 	devres_free(ptr);
964 	return hwdev;
965 }
966 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_groups);
967 
968 /**
969  * devm_hwmon_device_register_with_info - register w/ hwmon
970  * @dev:	the parent device
971  * @name:	hwmon name attribute
972  * @drvdata:	driver data to attach to created device
973  * @chip:	pointer to hwmon chip information
974  * @groups:	pointer to list of driver specific attribute groups
975  *
976  * Returns the pointer to the new device. The new device is automatically
977  * unregistered with the parent device.
978  */
979 struct device *
980 devm_hwmon_device_register_with_info(struct device *dev, const char *name,
981 				     void *drvdata,
982 				     const struct hwmon_chip_info *chip,
983 				     const struct attribute_group **groups)
984 {
985 	struct device **ptr, *hwdev;
986 
987 	if (!dev)
988 		return ERR_PTR(-EINVAL);
989 
990 	ptr = devres_alloc(devm_hwmon_release, sizeof(*ptr), GFP_KERNEL);
991 	if (!ptr)
992 		return ERR_PTR(-ENOMEM);
993 
994 	hwdev = hwmon_device_register_with_info(dev, name, drvdata, chip,
995 						groups);
996 	if (IS_ERR(hwdev))
997 		goto error;
998 
999 	*ptr = hwdev;
1000 	devres_add(dev, ptr);
1001 
1002 	return hwdev;
1003 
1004 error:
1005 	devres_free(ptr);
1006 	return hwdev;
1007 }
1008 EXPORT_SYMBOL_GPL(devm_hwmon_device_register_with_info);
1009 
1010 static int devm_hwmon_match(struct device *dev, void *res, void *data)
1011 {
1012 	struct device **hwdev = res;
1013 
1014 	return *hwdev == data;
1015 }
1016 
1017 /**
1018  * devm_hwmon_device_unregister - removes a previously registered hwmon device
1019  *
1020  * @dev: the parent device of the device to unregister
1021  */
1022 void devm_hwmon_device_unregister(struct device *dev)
1023 {
1024 	WARN_ON(devres_release(dev, devm_hwmon_release, devm_hwmon_match, dev));
1025 }
1026 EXPORT_SYMBOL_GPL(devm_hwmon_device_unregister);
1027 
1028 static void __init hwmon_pci_quirks(void)
1029 {
1030 #if defined CONFIG_X86 && defined CONFIG_PCI
1031 	struct pci_dev *sb;
1032 	u16 base;
1033 	u8 enable;
1034 
1035 	/* Open access to 0x295-0x296 on MSI MS-7031 */
1036 	sb = pci_get_device(PCI_VENDOR_ID_ATI, 0x436c, NULL);
1037 	if (sb) {
1038 		if (sb->subsystem_vendor == 0x1462 &&	/* MSI */
1039 		    sb->subsystem_device == 0x0031) {	/* MS-7031 */
1040 			pci_read_config_byte(sb, 0x48, &enable);
1041 			pci_read_config_word(sb, 0x64, &base);
1042 
1043 			if (base == 0 && !(enable & BIT(2))) {
1044 				dev_info(&sb->dev,
1045 					 "Opening wide generic port at 0x295\n");
1046 				pci_write_config_word(sb, 0x64, 0x295);
1047 				pci_write_config_byte(sb, 0x48,
1048 						      enable | BIT(2));
1049 			}
1050 		}
1051 		pci_dev_put(sb);
1052 	}
1053 #endif
1054 }
1055 
1056 static int __init hwmon_init(void)
1057 {
1058 	int err;
1059 
1060 	hwmon_pci_quirks();
1061 
1062 	err = class_register(&hwmon_class);
1063 	if (err) {
1064 		pr_err("couldn't register hwmon sysfs class\n");
1065 		return err;
1066 	}
1067 	return 0;
1068 }
1069 
1070 static void __exit hwmon_exit(void)
1071 {
1072 	class_unregister(&hwmon_class);
1073 }
1074 
1075 subsys_initcall(hwmon_init);
1076 module_exit(hwmon_exit);
1077 
1078 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
1079 MODULE_DESCRIPTION("hardware monitoring sysfs/class support");
1080 MODULE_LICENSE("GPL");
1081 
1082