1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2007 - 2018 Intel Corporation. */
5 #include "e1000_82575.h"
8 #include <linux/module.h>
9 #include <linux/types.h>
10 #include <linux/sysfs.h>
11 #include <linux/kobject.h>
12 #include <linux/device.h>
13 #include <linux/netdevice.h>
14 #include <linux/hwmon.h>
15 #include <linux/pci.h>
17 #ifdef CONFIG_IGB_HWMON
18 static struct i2c_board_info i350_sensor_info
= {
19 I2C_BOARD_INFO("i350bb", (0Xf8 >> 1)),
22 /* hwmon callback functions */
23 static ssize_t
igb_hwmon_show_location(struct device
*dev
,
24 struct device_attribute
*attr
,
27 struct hwmon_attr
*igb_attr
= container_of(attr
, struct hwmon_attr
,
29 return sprintf(buf
, "loc%u\n",
30 igb_attr
->sensor
->location
);
33 static ssize_t
igb_hwmon_show_temp(struct device
*dev
,
34 struct device_attribute
*attr
,
37 struct hwmon_attr
*igb_attr
= container_of(attr
, struct hwmon_attr
,
41 /* reset the temp field */
42 igb_attr
->hw
->mac
.ops
.get_thermal_sensor_data(igb_attr
->hw
);
44 value
= igb_attr
->sensor
->temp
;
46 /* display millidegree */
49 return sprintf(buf
, "%u\n", value
);
52 static ssize_t
igb_hwmon_show_cautionthresh(struct device
*dev
,
53 struct device_attribute
*attr
,
56 struct hwmon_attr
*igb_attr
= container_of(attr
, struct hwmon_attr
,
58 unsigned int value
= igb_attr
->sensor
->caution_thresh
;
60 /* display millidegree */
63 return sprintf(buf
, "%u\n", value
);
66 static ssize_t
igb_hwmon_show_maxopthresh(struct device
*dev
,
67 struct device_attribute
*attr
,
70 struct hwmon_attr
*igb_attr
= container_of(attr
, struct hwmon_attr
,
72 unsigned int value
= igb_attr
->sensor
->max_op_thresh
;
74 /* display millidegree */
77 return sprintf(buf
, "%u\n", value
);
80 /* igb_add_hwmon_attr - Create hwmon attr table for a hwmon sysfs file.
81 * @ adapter: pointer to the adapter structure
82 * @ offset: offset in the eeprom sensor data table
83 * @ type: type of sensor data to display
85 * For each file we want in hwmon's sysfs interface we need a device_attribute
86 * This is included in our hwmon_attr struct that contains the references to
87 * the data structures we need to get the data to display.
89 static int igb_add_hwmon_attr(struct igb_adapter
*adapter
,
90 unsigned int offset
, int type
)
94 struct hwmon_attr
*igb_attr
;
96 n_attr
= adapter
->igb_hwmon_buff
->n_hwmon
;
97 igb_attr
= &adapter
->igb_hwmon_buff
->hwmon_list
[n_attr
];
100 case IGB_HWMON_TYPE_LOC
:
101 igb_attr
->dev_attr
.show
= igb_hwmon_show_location
;
102 snprintf(igb_attr
->name
, sizeof(igb_attr
->name
),
103 "temp%u_label", offset
+ 1);
105 case IGB_HWMON_TYPE_TEMP
:
106 igb_attr
->dev_attr
.show
= igb_hwmon_show_temp
;
107 snprintf(igb_attr
->name
, sizeof(igb_attr
->name
),
108 "temp%u_input", offset
+ 1);
110 case IGB_HWMON_TYPE_CAUTION
:
111 igb_attr
->dev_attr
.show
= igb_hwmon_show_cautionthresh
;
112 snprintf(igb_attr
->name
, sizeof(igb_attr
->name
),
113 "temp%u_max", offset
+ 1);
115 case IGB_HWMON_TYPE_MAX
:
116 igb_attr
->dev_attr
.show
= igb_hwmon_show_maxopthresh
;
117 snprintf(igb_attr
->name
, sizeof(igb_attr
->name
),
118 "temp%u_crit", offset
+ 1);
125 /* These always the same regardless of type */
127 &adapter
->hw
.mac
.thermal_sensor_data
.sensor
[offset
];
128 igb_attr
->hw
= &adapter
->hw
;
129 igb_attr
->dev_attr
.store
= NULL
;
130 igb_attr
->dev_attr
.attr
.mode
= 0444;
131 igb_attr
->dev_attr
.attr
.name
= igb_attr
->name
;
132 sysfs_attr_init(&igb_attr
->dev_attr
.attr
);
134 adapter
->igb_hwmon_buff
->attrs
[n_attr
] = &igb_attr
->dev_attr
.attr
;
136 ++adapter
->igb_hwmon_buff
->n_hwmon
;
141 static void igb_sysfs_del_adapter(struct igb_adapter
*adapter
)
145 /* called from igb_main.c */
146 void igb_sysfs_exit(struct igb_adapter
*adapter
)
148 igb_sysfs_del_adapter(adapter
);
151 /* called from igb_main.c */
152 int igb_sysfs_init(struct igb_adapter
*adapter
)
154 struct hwmon_buff
*igb_hwmon
;
155 struct i2c_client
*client
;
156 struct device
*hwmon_dev
;
160 /* If this method isn't defined we don't support thermals */
161 if (adapter
->hw
.mac
.ops
.init_thermal_sensor_thresh
== NULL
)
164 /* Don't create thermal hwmon interface if no sensors present */
165 rc
= (adapter
->hw
.mac
.ops
.init_thermal_sensor_thresh(&adapter
->hw
));
169 igb_hwmon
= devm_kzalloc(&adapter
->pdev
->dev
, sizeof(*igb_hwmon
),
175 adapter
->igb_hwmon_buff
= igb_hwmon
;
177 for (i
= 0; i
< E1000_MAX_SENSORS
; i
++) {
179 /* Only create hwmon sysfs entries for sensors that have
182 if (adapter
->hw
.mac
.thermal_sensor_data
.sensor
[i
].location
== 0)
185 /* Bail if any hwmon attr struct fails to initialize */
186 rc
= igb_add_hwmon_attr(adapter
, i
, IGB_HWMON_TYPE_CAUTION
);
189 rc
= igb_add_hwmon_attr(adapter
, i
, IGB_HWMON_TYPE_LOC
);
192 rc
= igb_add_hwmon_attr(adapter
, i
, IGB_HWMON_TYPE_TEMP
);
195 rc
= igb_add_hwmon_attr(adapter
, i
, IGB_HWMON_TYPE_MAX
);
200 /* init i2c_client */
201 client
= i2c_new_device(&adapter
->i2c_adap
, &i350_sensor_info
);
202 if (client
== NULL
) {
203 dev_info(&adapter
->pdev
->dev
,
204 "Failed to create new i2c device.\n");
208 adapter
->i2c_client
= client
;
210 igb_hwmon
->groups
[0] = &igb_hwmon
->group
;
211 igb_hwmon
->group
.attrs
= igb_hwmon
->attrs
;
213 hwmon_dev
= devm_hwmon_device_register_with_groups(&adapter
->pdev
->dev
,
217 if (IS_ERR(hwmon_dev
)) {
218 rc
= PTR_ERR(hwmon_dev
);
225 igb_sysfs_del_adapter(adapter
);