xref: /linux/drivers/net/ethernet/intel/igb/igb_hwmon.c (revision ae06c70b135886d7d6252f3090146f01a3f3b80c)
1*ae06c70bSJeff Kirsher // SPDX-License-Identifier: GPL-2.0
2e52c0f96SCarolyn Wyborny /* Intel(R) Gigabit Ethernet Linux driver
3e52c0f96SCarolyn Wyborny  * Copyright(c) 2007-2014 Intel Corporation.
4e52c0f96SCarolyn Wyborny  *
5e52c0f96SCarolyn Wyborny  * This program is free software; you can redistribute it and/or modify it
6e52c0f96SCarolyn Wyborny  * under the terms and conditions of the GNU General Public License,
7e52c0f96SCarolyn Wyborny  * version 2, as published by the Free Software Foundation.
8e52c0f96SCarolyn Wyborny  *
9e52c0f96SCarolyn Wyborny  * This program is distributed in the hope it will be useful, but WITHOUT
10e52c0f96SCarolyn Wyborny  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11e52c0f96SCarolyn Wyborny  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12e52c0f96SCarolyn Wyborny  * more details.
13e52c0f96SCarolyn Wyborny  *
14e52c0f96SCarolyn Wyborny  * You should have received a copy of the GNU General Public License along with
15e52c0f96SCarolyn Wyborny  * this program; if not, see <http://www.gnu.org/licenses/>.
16e52c0f96SCarolyn Wyborny  *
17e52c0f96SCarolyn Wyborny  * The full GNU General Public License is included in this distribution in
18e52c0f96SCarolyn Wyborny  * the file called "COPYING".
19e52c0f96SCarolyn Wyborny  *
20e52c0f96SCarolyn Wyborny  * Contact Information:
21e52c0f96SCarolyn Wyborny  * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
22e52c0f96SCarolyn Wyborny  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
23e52c0f96SCarolyn Wyborny  */
24e428893bSCarolyn Wyborny 
25e428893bSCarolyn Wyborny #include "igb.h"
26e428893bSCarolyn Wyborny #include "e1000_82575.h"
27e428893bSCarolyn Wyborny #include "e1000_hw.h"
28e428893bSCarolyn Wyborny 
29e428893bSCarolyn Wyborny #include <linux/module.h>
30e428893bSCarolyn Wyborny #include <linux/types.h>
31e428893bSCarolyn Wyborny #include <linux/sysfs.h>
32e428893bSCarolyn Wyborny #include <linux/kobject.h>
33e428893bSCarolyn Wyborny #include <linux/device.h>
34e428893bSCarolyn Wyborny #include <linux/netdevice.h>
35e428893bSCarolyn Wyborny #include <linux/hwmon.h>
36e428893bSCarolyn Wyborny #include <linux/pci.h>
37e428893bSCarolyn Wyborny 
38e428893bSCarolyn Wyborny #ifdef CONFIG_IGB_HWMON
3905ec29e8SStephen Hemminger static struct i2c_board_info i350_sensor_info = {
40603e86faSCarolyn Wyborny 	I2C_BOARD_INFO("i350bb", (0Xf8 >> 1)),
41603e86faSCarolyn Wyborny };
42603e86faSCarolyn Wyborny 
43e428893bSCarolyn Wyborny /* hwmon callback functions */
44e428893bSCarolyn Wyborny static ssize_t igb_hwmon_show_location(struct device *dev,
45e428893bSCarolyn Wyborny 				       struct device_attribute *attr,
46e428893bSCarolyn Wyborny 				       char *buf)
47e428893bSCarolyn Wyborny {
48e428893bSCarolyn Wyborny 	struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr,
49e428893bSCarolyn Wyborny 						   dev_attr);
50e428893bSCarolyn Wyborny 	return sprintf(buf, "loc%u\n",
51e428893bSCarolyn Wyborny 		       igb_attr->sensor->location);
52e428893bSCarolyn Wyborny }
53e428893bSCarolyn Wyborny 
54e428893bSCarolyn Wyborny static ssize_t igb_hwmon_show_temp(struct device *dev,
55e428893bSCarolyn Wyborny 				   struct device_attribute *attr,
56e428893bSCarolyn Wyborny 				   char *buf)
57e428893bSCarolyn Wyborny {
58e428893bSCarolyn Wyborny 	struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr,
59e428893bSCarolyn Wyborny 						   dev_attr);
60e428893bSCarolyn Wyborny 	unsigned int value;
61e428893bSCarolyn Wyborny 
62e428893bSCarolyn Wyborny 	/* reset the temp field */
63e428893bSCarolyn Wyborny 	igb_attr->hw->mac.ops.get_thermal_sensor_data(igb_attr->hw);
64e428893bSCarolyn Wyborny 
65e428893bSCarolyn Wyborny 	value = igb_attr->sensor->temp;
66e428893bSCarolyn Wyborny 
67e428893bSCarolyn Wyborny 	/* display millidegree */
68e428893bSCarolyn Wyborny 	value *= 1000;
69e428893bSCarolyn Wyborny 
70e428893bSCarolyn Wyborny 	return sprintf(buf, "%u\n", value);
71e428893bSCarolyn Wyborny }
72e428893bSCarolyn Wyborny 
73e428893bSCarolyn Wyborny static ssize_t igb_hwmon_show_cautionthresh(struct device *dev,
74e428893bSCarolyn Wyborny 					    struct device_attribute *attr,
75e428893bSCarolyn Wyborny 					    char *buf)
76e428893bSCarolyn Wyborny {
77e428893bSCarolyn Wyborny 	struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr,
78e428893bSCarolyn Wyborny 						   dev_attr);
79e428893bSCarolyn Wyborny 	unsigned int value = igb_attr->sensor->caution_thresh;
80e428893bSCarolyn Wyborny 
81e428893bSCarolyn Wyborny 	/* display millidegree */
82e428893bSCarolyn Wyborny 	value *= 1000;
83e428893bSCarolyn Wyborny 
84e428893bSCarolyn Wyborny 	return sprintf(buf, "%u\n", value);
85e428893bSCarolyn Wyborny }
86e428893bSCarolyn Wyborny 
87e428893bSCarolyn Wyborny static ssize_t igb_hwmon_show_maxopthresh(struct device *dev,
88e428893bSCarolyn Wyborny 					  struct device_attribute *attr,
89e428893bSCarolyn Wyborny 					  char *buf)
90e428893bSCarolyn Wyborny {
91e428893bSCarolyn Wyborny 	struct hwmon_attr *igb_attr = container_of(attr, struct hwmon_attr,
92e428893bSCarolyn Wyborny 						   dev_attr);
93e428893bSCarolyn Wyborny 	unsigned int value = igb_attr->sensor->max_op_thresh;
94e428893bSCarolyn Wyborny 
95e428893bSCarolyn Wyborny 	/* display millidegree */
96e428893bSCarolyn Wyborny 	value *= 1000;
97e428893bSCarolyn Wyborny 
98e428893bSCarolyn Wyborny 	return sprintf(buf, "%u\n", value);
99e428893bSCarolyn Wyborny }
100e428893bSCarolyn Wyborny 
101e428893bSCarolyn Wyborny /* igb_add_hwmon_attr - Create hwmon attr table for a hwmon sysfs file.
102e428893bSCarolyn Wyborny  * @ adapter: pointer to the adapter structure
103e428893bSCarolyn Wyborny  * @ offset: offset in the eeprom sensor data table
104e428893bSCarolyn Wyborny  * @ type: type of sensor data to display
105e428893bSCarolyn Wyborny  *
106e428893bSCarolyn Wyborny  * For each file we want in hwmon's sysfs interface we need a device_attribute
107e428893bSCarolyn Wyborny  * This is included in our hwmon_attr struct that contains the references to
108e428893bSCarolyn Wyborny  * the data structures we need to get the data to display.
109e428893bSCarolyn Wyborny  */
110e428893bSCarolyn Wyborny static int igb_add_hwmon_attr(struct igb_adapter *adapter,
111b980ac18SJeff Kirsher 			      unsigned int offset, int type)
112b980ac18SJeff Kirsher {
113e428893bSCarolyn Wyborny 	int rc;
114e428893bSCarolyn Wyborny 	unsigned int n_attr;
115e428893bSCarolyn Wyborny 	struct hwmon_attr *igb_attr;
116e428893bSCarolyn Wyborny 
117e3670b81SGuenter Roeck 	n_attr = adapter->igb_hwmon_buff->n_hwmon;
118e3670b81SGuenter Roeck 	igb_attr = &adapter->igb_hwmon_buff->hwmon_list[n_attr];
119e428893bSCarolyn Wyborny 
120e428893bSCarolyn Wyborny 	switch (type) {
121e428893bSCarolyn Wyborny 	case IGB_HWMON_TYPE_LOC:
122e428893bSCarolyn Wyborny 		igb_attr->dev_attr.show = igb_hwmon_show_location;
123e428893bSCarolyn Wyborny 		snprintf(igb_attr->name, sizeof(igb_attr->name),
124e6e25bbaSGuenter Roeck 			 "temp%u_label", offset + 1);
125e428893bSCarolyn Wyborny 		break;
126e428893bSCarolyn Wyborny 	case IGB_HWMON_TYPE_TEMP:
127e428893bSCarolyn Wyborny 		igb_attr->dev_attr.show = igb_hwmon_show_temp;
128e428893bSCarolyn Wyborny 		snprintf(igb_attr->name, sizeof(igb_attr->name),
129e6e25bbaSGuenter Roeck 			 "temp%u_input", offset + 1);
130e428893bSCarolyn Wyborny 		break;
131e428893bSCarolyn Wyborny 	case IGB_HWMON_TYPE_CAUTION:
132e428893bSCarolyn Wyborny 		igb_attr->dev_attr.show = igb_hwmon_show_cautionthresh;
133e428893bSCarolyn Wyborny 		snprintf(igb_attr->name, sizeof(igb_attr->name),
134e6e25bbaSGuenter Roeck 			 "temp%u_max", offset + 1);
135e428893bSCarolyn Wyborny 		break;
136e428893bSCarolyn Wyborny 	case IGB_HWMON_TYPE_MAX:
137e428893bSCarolyn Wyborny 		igb_attr->dev_attr.show = igb_hwmon_show_maxopthresh;
138e428893bSCarolyn Wyborny 		snprintf(igb_attr->name, sizeof(igb_attr->name),
139e6e25bbaSGuenter Roeck 			 "temp%u_crit", offset + 1);
140e428893bSCarolyn Wyborny 		break;
141e428893bSCarolyn Wyborny 	default:
142e428893bSCarolyn Wyborny 		rc = -EPERM;
143e428893bSCarolyn Wyborny 		return rc;
144e428893bSCarolyn Wyborny 	}
145e428893bSCarolyn Wyborny 
146e428893bSCarolyn Wyborny 	/* These always the same regardless of type */
147e428893bSCarolyn Wyborny 	igb_attr->sensor =
148e428893bSCarolyn Wyborny 		&adapter->hw.mac.thermal_sensor_data.sensor[offset];
149e428893bSCarolyn Wyborny 	igb_attr->hw = &adapter->hw;
150e428893bSCarolyn Wyborny 	igb_attr->dev_attr.store = NULL;
151e428893bSCarolyn Wyborny 	igb_attr->dev_attr.attr.mode = S_IRUGO;
152e428893bSCarolyn Wyborny 	igb_attr->dev_attr.attr.name = igb_attr->name;
153e428893bSCarolyn Wyborny 	sysfs_attr_init(&igb_attr->dev_attr.attr);
154e428893bSCarolyn Wyborny 
155e3670b81SGuenter Roeck 	adapter->igb_hwmon_buff->attrs[n_attr] = &igb_attr->dev_attr.attr;
156e3670b81SGuenter Roeck 
157e3670b81SGuenter Roeck 	++adapter->igb_hwmon_buff->n_hwmon;
158e3670b81SGuenter Roeck 
159e3670b81SGuenter Roeck 	return 0;
160e428893bSCarolyn Wyborny }
161e428893bSCarolyn Wyborny 
162e428893bSCarolyn Wyborny static void igb_sysfs_del_adapter(struct igb_adapter *adapter)
163e428893bSCarolyn Wyborny {
164e428893bSCarolyn Wyborny }
165e428893bSCarolyn Wyborny 
166e428893bSCarolyn Wyborny /* called from igb_main.c */
167e428893bSCarolyn Wyborny void igb_sysfs_exit(struct igb_adapter *adapter)
168e428893bSCarolyn Wyborny {
169e428893bSCarolyn Wyborny 	igb_sysfs_del_adapter(adapter);
170e428893bSCarolyn Wyborny }
171e428893bSCarolyn Wyborny 
172e428893bSCarolyn Wyborny /* called from igb_main.c */
173e428893bSCarolyn Wyborny int igb_sysfs_init(struct igb_adapter *adapter)
174e428893bSCarolyn Wyborny {
175e3670b81SGuenter Roeck 	struct hwmon_buff *igb_hwmon;
176e3670b81SGuenter Roeck 	struct i2c_client *client;
177e3670b81SGuenter Roeck 	struct device *hwmon_dev;
178e428893bSCarolyn Wyborny 	unsigned int i;
179e428893bSCarolyn Wyborny 	int rc = 0;
180e428893bSCarolyn Wyborny 
181e428893bSCarolyn Wyborny 	/* If this method isn't defined we don't support thermals */
182e428893bSCarolyn Wyborny 	if (adapter->hw.mac.ops.init_thermal_sensor_thresh == NULL)
183e428893bSCarolyn Wyborny 		goto exit;
184e428893bSCarolyn Wyborny 
185e428893bSCarolyn Wyborny 	/* Don't create thermal hwmon interface if no sensors present */
186e428893bSCarolyn Wyborny 	rc = (adapter->hw.mac.ops.init_thermal_sensor_thresh(&adapter->hw));
187e428893bSCarolyn Wyborny 	if (rc)
188e428893bSCarolyn Wyborny 		goto exit;
189e428893bSCarolyn Wyborny 
190e3670b81SGuenter Roeck 	igb_hwmon = devm_kzalloc(&adapter->pdev->dev, sizeof(*igb_hwmon),
191e3670b81SGuenter Roeck 				 GFP_KERNEL);
192e3670b81SGuenter Roeck 	if (!igb_hwmon) {
193e3670b81SGuenter Roeck 		rc = -ENOMEM;
194603e86faSCarolyn Wyborny 		goto exit;
195603e86faSCarolyn Wyborny 	}
196e3670b81SGuenter Roeck 	adapter->igb_hwmon_buff = igb_hwmon;
197e428893bSCarolyn Wyborny 
198e428893bSCarolyn Wyborny 	for (i = 0; i < E1000_MAX_SENSORS; i++) {
199e428893bSCarolyn Wyborny 
200e428893bSCarolyn Wyborny 		/* Only create hwmon sysfs entries for sensors that have
201e428893bSCarolyn Wyborny 		 * meaningful data.
202e428893bSCarolyn Wyborny 		 */
203e428893bSCarolyn Wyborny 		if (adapter->hw.mac.thermal_sensor_data.sensor[i].location == 0)
204e428893bSCarolyn Wyborny 			continue;
205e428893bSCarolyn Wyborny 
206e428893bSCarolyn Wyborny 		/* Bail if any hwmon attr struct fails to initialize */
207e428893bSCarolyn Wyborny 		rc = igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_CAUTION);
208e428893bSCarolyn Wyborny 		if (rc)
209e3670b81SGuenter Roeck 			goto exit;
210e3670b81SGuenter Roeck 		rc = igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_LOC);
211e3670b81SGuenter Roeck 		if (rc)
212e3670b81SGuenter Roeck 			goto exit;
213e3670b81SGuenter Roeck 		rc = igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_TEMP);
214e3670b81SGuenter Roeck 		if (rc)
215e3670b81SGuenter Roeck 			goto exit;
216e3670b81SGuenter Roeck 		rc = igb_add_hwmon_attr(adapter, i, IGB_HWMON_TYPE_MAX);
217e3670b81SGuenter Roeck 		if (rc)
218e3670b81SGuenter Roeck 			goto exit;
219e3670b81SGuenter Roeck 	}
220e3670b81SGuenter Roeck 
221e3670b81SGuenter Roeck 	/* init i2c_client */
222e3670b81SGuenter Roeck 	client = i2c_new_device(&adapter->i2c_adap, &i350_sensor_info);
223e3670b81SGuenter Roeck 	if (client == NULL) {
224e3670b81SGuenter Roeck 		dev_info(&adapter->pdev->dev,
225e3670b81SGuenter Roeck 			 "Failed to create new i2c device.\n");
226e3670b81SGuenter Roeck 		rc = -ENODEV;
227e3670b81SGuenter Roeck 		goto exit;
228e3670b81SGuenter Roeck 	}
229e3670b81SGuenter Roeck 	adapter->i2c_client = client;
230e3670b81SGuenter Roeck 
231e3670b81SGuenter Roeck 	igb_hwmon->groups[0] = &igb_hwmon->group;
232e3670b81SGuenter Roeck 	igb_hwmon->group.attrs = igb_hwmon->attrs;
233e3670b81SGuenter Roeck 
234e3670b81SGuenter Roeck 	hwmon_dev = devm_hwmon_device_register_with_groups(&adapter->pdev->dev,
235e3670b81SGuenter Roeck 							   client->name,
236e3670b81SGuenter Roeck 							   igb_hwmon,
237e3670b81SGuenter Roeck 							   igb_hwmon->groups);
238e3670b81SGuenter Roeck 	if (IS_ERR(hwmon_dev)) {
239e3670b81SGuenter Roeck 		rc = PTR_ERR(hwmon_dev);
240e428893bSCarolyn Wyborny 		goto err;
241e428893bSCarolyn Wyborny 	}
242e428893bSCarolyn Wyborny 
243e428893bSCarolyn Wyborny 	goto exit;
244e428893bSCarolyn Wyborny 
245e428893bSCarolyn Wyborny err:
246e428893bSCarolyn Wyborny 	igb_sysfs_del_adapter(adapter);
247e428893bSCarolyn Wyborny exit:
248e428893bSCarolyn Wyborny 	return rc;
249e428893bSCarolyn Wyborny }
250e428893bSCarolyn Wyborny #endif
251