1 /* Intel(R) Gigabit Ethernet Linux driver
2 * Copyright(c) 2007-2014 Intel Corporation.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, see <http://www.gnu.org/licenses/>.
16 * The full GNU General Public License is included in this distribution in
17 * the file called "COPYING".
19 * Contact Information:
20 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
21 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25 #include "e1000_82575.h"
28 #include <linux/module.h>
29 #include <linux/types.h>
30 #include <linux/sysfs.h>
31 #include <linux/kobject.h>
32 #include <linux/device.h>
33 #include <linux/netdevice.h>
34 #include <linux/hwmon.h>
35 #include <linux/pci.h>
37 #ifdef CONFIG_IGB_HWMON
38 static struct i2c_board_info i350_sensor_info
= {
39 I2C_BOARD_INFO("i350bb", (0Xf8 >> 1)),
42 /* hwmon callback functions */
43 static ssize_t
igb_hwmon_show_location(struct device
*dev
,
44 struct device_attribute
*attr
,
47 struct hwmon_attr
*igb_attr
= container_of(attr
, struct hwmon_attr
,
49 return sprintf(buf
, "loc%u\n",
50 igb_attr
->sensor
->location
);
53 static ssize_t
igb_hwmon_show_temp(struct device
*dev
,
54 struct device_attribute
*attr
,
57 struct hwmon_attr
*igb_attr
= container_of(attr
, struct hwmon_attr
,
61 /* reset the temp field */
62 igb_attr
->hw
->mac
.ops
.get_thermal_sensor_data(igb_attr
->hw
);
64 value
= igb_attr
->sensor
->temp
;
66 /* display millidegree */
69 return sprintf(buf
, "%u\n", value
);
72 static ssize_t
igb_hwmon_show_cautionthresh(struct device
*dev
,
73 struct device_attribute
*attr
,
76 struct hwmon_attr
*igb_attr
= container_of(attr
, struct hwmon_attr
,
78 unsigned int value
= igb_attr
->sensor
->caution_thresh
;
80 /* display millidegree */
83 return sprintf(buf
, "%u\n", value
);
86 static ssize_t
igb_hwmon_show_maxopthresh(struct device
*dev
,
87 struct device_attribute
*attr
,
90 struct hwmon_attr
*igb_attr
= container_of(attr
, struct hwmon_attr
,
92 unsigned int value
= igb_attr
->sensor
->max_op_thresh
;
94 /* display millidegree */
97 return sprintf(buf
, "%u\n", value
);
100 /* igb_add_hwmon_attr - Create hwmon attr table for a hwmon sysfs file.
101 * @ adapter: pointer to the adapter structure
102 * @ offset: offset in the eeprom sensor data table
103 * @ type: type of sensor data to display
105 * For each file we want in hwmon's sysfs interface we need a device_attribute
106 * This is included in our hwmon_attr struct that contains the references to
107 * the data structures we need to get the data to display.
109 static int igb_add_hwmon_attr(struct igb_adapter
*adapter
,
110 unsigned int offset
, int type
)
114 struct hwmon_attr
*igb_attr
;
116 n_attr
= adapter
->igb_hwmon_buff
->n_hwmon
;
117 igb_attr
= &adapter
->igb_hwmon_buff
->hwmon_list
[n_attr
];
120 case IGB_HWMON_TYPE_LOC
:
121 igb_attr
->dev_attr
.show
= igb_hwmon_show_location
;
122 snprintf(igb_attr
->name
, sizeof(igb_attr
->name
),
123 "temp%u_label", offset
+ 1);
125 case IGB_HWMON_TYPE_TEMP
:
126 igb_attr
->dev_attr
.show
= igb_hwmon_show_temp
;
127 snprintf(igb_attr
->name
, sizeof(igb_attr
->name
),
128 "temp%u_input", offset
+ 1);
130 case IGB_HWMON_TYPE_CAUTION
:
131 igb_attr
->dev_attr
.show
= igb_hwmon_show_cautionthresh
;
132 snprintf(igb_attr
->name
, sizeof(igb_attr
->name
),
133 "temp%u_max", offset
+ 1);
135 case IGB_HWMON_TYPE_MAX
:
136 igb_attr
->dev_attr
.show
= igb_hwmon_show_maxopthresh
;
137 snprintf(igb_attr
->name
, sizeof(igb_attr
->name
),
138 "temp%u_crit", offset
+ 1);
145 /* These always the same regardless of type */
147 &adapter
->hw
.mac
.thermal_sensor_data
.sensor
[offset
];
148 igb_attr
->hw
= &adapter
->hw
;
149 igb_attr
->dev_attr
.store
= NULL
;
150 igb_attr
->dev_attr
.attr
.mode
= S_IRUGO
;
151 igb_attr
->dev_attr
.attr
.name
= igb_attr
->name
;
152 sysfs_attr_init(&igb_attr
->dev_attr
.attr
);
154 adapter
->igb_hwmon_buff
->attrs
[n_attr
] = &igb_attr
->dev_attr
.attr
;
156 ++adapter
->igb_hwmon_buff
->n_hwmon
;
161 static void igb_sysfs_del_adapter(struct igb_adapter
*adapter
)
165 /* called from igb_main.c */
166 void igb_sysfs_exit(struct igb_adapter
*adapter
)
168 igb_sysfs_del_adapter(adapter
);
171 /* called from igb_main.c */
172 int igb_sysfs_init(struct igb_adapter
*adapter
)
174 struct hwmon_buff
*igb_hwmon
;
175 struct i2c_client
*client
;
176 struct device
*hwmon_dev
;
180 /* If this method isn't defined we don't support thermals */
181 if (adapter
->hw
.mac
.ops
.init_thermal_sensor_thresh
== NULL
)
184 /* Don't create thermal hwmon interface if no sensors present */
185 rc
= (adapter
->hw
.mac
.ops
.init_thermal_sensor_thresh(&adapter
->hw
));
189 igb_hwmon
= devm_kzalloc(&adapter
->pdev
->dev
, sizeof(*igb_hwmon
),
195 adapter
->igb_hwmon_buff
= igb_hwmon
;
197 for (i
= 0; i
< E1000_MAX_SENSORS
; i
++) {
199 /* Only create hwmon sysfs entries for sensors that have
202 if (adapter
->hw
.mac
.thermal_sensor_data
.sensor
[i
].location
== 0)
205 /* Bail if any hwmon attr struct fails to initialize */
206 rc
= igb_add_hwmon_attr(adapter
, i
, IGB_HWMON_TYPE_CAUTION
);
209 rc
= igb_add_hwmon_attr(adapter
, i
, IGB_HWMON_TYPE_LOC
);
212 rc
= igb_add_hwmon_attr(adapter
, i
, IGB_HWMON_TYPE_TEMP
);
215 rc
= igb_add_hwmon_attr(adapter
, i
, IGB_HWMON_TYPE_MAX
);
220 /* init i2c_client */
221 client
= i2c_new_device(&adapter
->i2c_adap
, &i350_sensor_info
);
222 if (client
== NULL
) {
223 dev_info(&adapter
->pdev
->dev
,
224 "Failed to create new i2c device.\n");
228 adapter
->i2c_client
= client
;
230 igb_hwmon
->groups
[0] = &igb_hwmon
->group
;
231 igb_hwmon
->group
.attrs
= igb_hwmon
->attrs
;
233 hwmon_dev
= devm_hwmon_device_register_with_groups(&adapter
->pdev
->dev
,
237 if (IS_ERR(hwmon_dev
)) {
238 rc
= PTR_ERR(hwmon_dev
);
245 igb_sysfs_del_adapter(adapter
);