1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Intel MAX 10 BMC HWMON Driver
4  *
5  * Copyright (C) 2018-2020 Intel Corporation. All rights reserved.
6  *
7  */
8 #include <linux/device.h>
9 #include <linux/hwmon.h>
10 #include <linux/mfd/intel-m10-bmc.h>
11 #include <linux/module.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/platform_device.h>
14 
15 struct m10bmc_sdata {
16 	unsigned int reg_input;
17 	unsigned int reg_max;
18 	unsigned int reg_crit;
19 	unsigned int reg_hyst;
20 	unsigned int reg_min;
21 	unsigned int multiplier;
22 	const char *label;
23 };
24 
25 struct m10bmc_hwmon_board_data {
26 	const struct m10bmc_sdata *tables[hwmon_max];
27 	const struct hwmon_channel_info **hinfo;
28 };
29 
30 struct m10bmc_hwmon {
31 	struct device *dev;
32 	struct hwmon_chip_info chip;
33 	char *hw_name;
34 	struct intel_m10bmc *m10bmc;
35 	const struct m10bmc_hwmon_board_data *bdata;
36 };
37 
38 static const struct m10bmc_sdata n3000bmc_temp_tbl[] = {
39 	{ 0x100, 0x104, 0x108, 0x10c, 0x0, 500, "Board Temperature" },
40 	{ 0x110, 0x114, 0x118, 0x0, 0x0, 500, "FPGA Die Temperature" },
41 	{ 0x11c, 0x124, 0x120, 0x0, 0x0, 500, "QSFP0 Temperature" },
42 	{ 0x12c, 0x134, 0x130, 0x0, 0x0, 500, "QSFP1 Temperature" },
43 	{ 0x168, 0x0, 0x0, 0x0, 0x0, 500, "Retimer A Temperature" },
44 	{ 0x16c, 0x0, 0x0, 0x0, 0x0, 500, "Retimer A SerDes Temperature" },
45 	{ 0x170, 0x0, 0x0, 0x0, 0x0, 500, "Retimer B Temperature" },
46 	{ 0x174, 0x0, 0x0, 0x0, 0x0, 500, "Retimer B SerDes Temperature" },
47 };
48 
49 static const struct m10bmc_sdata n3000bmc_in_tbl[] = {
50 	{ 0x128, 0x0, 0x0, 0x0, 0x0, 1, "QSFP0 Supply Voltage" },
51 	{ 0x138, 0x0, 0x0, 0x0, 0x0, 1, "QSFP1 Supply Voltage" },
52 	{ 0x13c, 0x0, 0x0, 0x0, 0x0, 1, "FPGA Core Voltage" },
53 	{ 0x144, 0x0, 0x0, 0x0, 0x0, 1, "12V Backplane Voltage" },
54 	{ 0x14c, 0x0, 0x0, 0x0, 0x0, 1, "1.2V Voltage" },
55 	{ 0x150, 0x0, 0x0, 0x0, 0x0, 1, "12V AUX Voltage" },
56 	{ 0x158, 0x0, 0x0, 0x0, 0x0, 1, "1.8V Voltage" },
57 	{ 0x15c, 0x0, 0x0, 0x0, 0x0, 1, "3.3V Voltage" },
58 };
59 
60 static const struct m10bmc_sdata n3000bmc_curr_tbl[] = {
61 	{ 0x140, 0x0, 0x0, 0x0, 0x0, 1, "FPGA Core Current" },
62 	{ 0x148, 0x0, 0x0, 0x0, 0x0, 1, "12V Backplane Current" },
63 	{ 0x154, 0x0, 0x0, 0x0, 0x0, 1, "12V AUX Current" },
64 };
65 
66 static const struct m10bmc_sdata n3000bmc_power_tbl[] = {
67 	{ 0x160, 0x0, 0x0, 0x0, 0x0, 1000, "Board Power" },
68 };
69 
70 static const struct hwmon_channel_info *n3000bmc_hinfo[] = {
71 	HWMON_CHANNEL_INFO(temp,
72 			   HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST |
73 			   HWMON_T_CRIT | HWMON_T_CRIT_HYST | HWMON_T_LABEL,
74 			   HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT |
75 			   HWMON_T_LABEL,
76 			   HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT |
77 			   HWMON_T_LABEL,
78 			   HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT |
79 			   HWMON_T_LABEL,
80 			   HWMON_T_INPUT | HWMON_T_LABEL,
81 			   HWMON_T_INPUT | HWMON_T_LABEL,
82 			   HWMON_T_INPUT | HWMON_T_LABEL,
83 			   HWMON_T_INPUT | HWMON_T_LABEL),
84 	HWMON_CHANNEL_INFO(in,
85 			   HWMON_I_INPUT | HWMON_I_LABEL,
86 			   HWMON_I_INPUT | HWMON_I_LABEL,
87 			   HWMON_I_INPUT | HWMON_I_LABEL,
88 			   HWMON_I_INPUT | HWMON_I_LABEL,
89 			   HWMON_I_INPUT | HWMON_I_LABEL,
90 			   HWMON_I_INPUT | HWMON_I_LABEL,
91 			   HWMON_I_INPUT | HWMON_I_LABEL,
92 			   HWMON_I_INPUT | HWMON_I_LABEL),
93 	HWMON_CHANNEL_INFO(curr,
94 			   HWMON_C_INPUT | HWMON_C_LABEL,
95 			   HWMON_C_INPUT | HWMON_C_LABEL,
96 			   HWMON_C_INPUT | HWMON_C_LABEL),
97 	HWMON_CHANNEL_INFO(power,
98 			   HWMON_P_INPUT | HWMON_P_LABEL),
99 	NULL
100 };
101 
102 static const struct m10bmc_hwmon_board_data n3000bmc_hwmon_bdata = {
103 	.tables = {
104 		[hwmon_temp] = n3000bmc_temp_tbl,
105 		[hwmon_in] = n3000bmc_in_tbl,
106 		[hwmon_curr] = n3000bmc_curr_tbl,
107 		[hwmon_power] = n3000bmc_power_tbl,
108 	},
109 
110 	.hinfo = n3000bmc_hinfo,
111 };
112 
113 static umode_t
114 m10bmc_hwmon_is_visible(const void *data, enum hwmon_sensor_types type,
115 			u32 attr, int channel)
116 {
117 	return 0444;
118 }
119 
120 static const struct m10bmc_sdata *
121 find_sensor_data(struct m10bmc_hwmon *hw, enum hwmon_sensor_types type,
122 		 int channel)
123 {
124 	const struct m10bmc_sdata *tbl;
125 
126 	tbl = hw->bdata->tables[type];
127 	if (!tbl)
128 		return ERR_PTR(-EOPNOTSUPP);
129 
130 	return &tbl[channel];
131 }
132 
133 static int do_sensor_read(struct m10bmc_hwmon *hw,
134 			  const struct m10bmc_sdata *data,
135 			  unsigned int regoff, long *val)
136 {
137 	unsigned int regval;
138 	int ret;
139 
140 	ret = m10bmc_sys_read(hw->m10bmc, regoff, &regval);
141 	if (ret)
142 		return ret;
143 
144 	/*
145 	 * BMC Firmware will return 0xdeadbeef if the sensor value is invalid
146 	 * at that time. This usually happens on sensor channels which connect
147 	 * to external pluggable modules, e.g. QSFP temperature and voltage.
148 	 * When the QSFP is unplugged from cage, driver will get 0xdeadbeef
149 	 * from their registers.
150 	 */
151 	if (regval == 0xdeadbeef)
152 		return -ENODATA;
153 
154 	*val = regval * data->multiplier;
155 
156 	return 0;
157 }
158 
159 static int m10bmc_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
160 			     u32 attr, int channel, long *val)
161 {
162 	struct m10bmc_hwmon *hw = dev_get_drvdata(dev);
163 	unsigned int reg = 0, reg_hyst = 0;
164 	const struct m10bmc_sdata *data;
165 	long hyst, value;
166 	int ret;
167 
168 	data = find_sensor_data(hw, type, channel);
169 	if (IS_ERR(data))
170 		return PTR_ERR(data);
171 
172 	switch (type) {
173 	case hwmon_temp:
174 		switch (attr) {
175 		case hwmon_temp_input:
176 			reg = data->reg_input;
177 			break;
178 		case hwmon_temp_max_hyst:
179 			reg_hyst = data->reg_hyst;
180 			fallthrough;
181 		case hwmon_temp_max:
182 			reg = data->reg_max;
183 			break;
184 		case hwmon_temp_crit_hyst:
185 			reg_hyst = data->reg_hyst;
186 			fallthrough;
187 		case hwmon_temp_crit:
188 			reg = data->reg_crit;
189 			break;
190 		default:
191 			return -EOPNOTSUPP;
192 		}
193 		break;
194 	case hwmon_in:
195 		switch (attr) {
196 		case hwmon_in_input:
197 			reg = data->reg_input;
198 			break;
199 		case hwmon_in_max:
200 			reg = data->reg_max;
201 			break;
202 		case hwmon_in_crit:
203 			reg = data->reg_crit;
204 			break;
205 		case hwmon_in_min:
206 			reg = data->reg_min;
207 			break;
208 		default:
209 			return -EOPNOTSUPP;
210 		}
211 		break;
212 	case hwmon_curr:
213 		switch (attr) {
214 		case hwmon_curr_input:
215 			reg = data->reg_input;
216 			break;
217 		case hwmon_curr_max:
218 			reg = data->reg_max;
219 			break;
220 		case hwmon_curr_crit:
221 			reg = data->reg_crit;
222 			break;
223 		default:
224 			return -EOPNOTSUPP;
225 		}
226 		break;
227 	case hwmon_power:
228 		switch (attr) {
229 		case hwmon_power_input:
230 			reg = data->reg_input;
231 			break;
232 		default:
233 			return -EOPNOTSUPP;
234 		}
235 		break;
236 	default:
237 		return -EOPNOTSUPP;
238 	}
239 
240 	if (!reg)
241 		return -EOPNOTSUPP;
242 
243 	ret = do_sensor_read(hw, data, reg, &value);
244 	if (ret)
245 		return ret;
246 
247 	if (reg_hyst) {
248 		ret = do_sensor_read(hw, data, reg_hyst, &hyst);
249 		if (ret)
250 			return ret;
251 
252 		value -= hyst;
253 	}
254 
255 	*val = value;
256 
257 	return 0;
258 }
259 
260 static int m10bmc_hwmon_read_string(struct device *dev,
261 				    enum hwmon_sensor_types type,
262 				    u32 attr, int channel, const char **str)
263 {
264 	struct m10bmc_hwmon *hw = dev_get_drvdata(dev);
265 	const struct m10bmc_sdata *data;
266 
267 	data = find_sensor_data(hw, type, channel);
268 	if (IS_ERR(data))
269 		return PTR_ERR(data);
270 
271 	*str = data->label;
272 
273 	return 0;
274 }
275 
276 static const struct hwmon_ops m10bmc_hwmon_ops = {
277 	.is_visible = m10bmc_hwmon_is_visible,
278 	.read = m10bmc_hwmon_read,
279 	.read_string = m10bmc_hwmon_read_string,
280 };
281 
282 static int m10bmc_hwmon_probe(struct platform_device *pdev)
283 {
284 	const struct platform_device_id *id = platform_get_device_id(pdev);
285 	struct intel_m10bmc *m10bmc = dev_get_drvdata(pdev->dev.parent);
286 	struct device *hwmon_dev, *dev = &pdev->dev;
287 	struct m10bmc_hwmon *hw;
288 	int i;
289 
290 	hw = devm_kzalloc(dev, sizeof(*hw), GFP_KERNEL);
291 	if (!hw)
292 		return -ENOMEM;
293 
294 	hw->dev = dev;
295 	hw->m10bmc = m10bmc;
296 	hw->bdata = (const struct m10bmc_hwmon_board_data *)id->driver_data;
297 
298 	hw->chip.info = hw->bdata->hinfo;
299 	hw->chip.ops = &m10bmc_hwmon_ops;
300 
301 	hw->hw_name = devm_kstrdup(dev, id->name, GFP_KERNEL);
302 	if (!hw->hw_name)
303 		return -ENOMEM;
304 
305 	for (i = 0; hw->hw_name[i]; i++)
306 		if (hwmon_is_bad_char(hw->hw_name[i]))
307 			hw->hw_name[i] = '_';
308 
309 	hwmon_dev = devm_hwmon_device_register_with_info(dev, hw->hw_name,
310 							 hw, &hw->chip, NULL);
311 	return PTR_ERR_OR_ZERO(hwmon_dev);
312 }
313 
314 static const struct platform_device_id intel_m10bmc_hwmon_ids[] = {
315 	{
316 		.name = "n3000bmc-hwmon",
317 		.driver_data = (unsigned long)&n3000bmc_hwmon_bdata,
318 	},
319 	{ }
320 };
321 
322 static struct platform_driver intel_m10bmc_hwmon_driver = {
323 	.probe = m10bmc_hwmon_probe,
324 	.driver = {
325 		.name = "intel-m10-bmc-hwmon",
326 	},
327 	.id_table = intel_m10bmc_hwmon_ids,
328 };
329 module_platform_driver(intel_m10bmc_hwmon_driver);
330 
331 MODULE_DEVICE_TABLE(platform, intel_m10bmc_hwmon_ids);
332 MODULE_AUTHOR("Intel Corporation");
333 MODULE_DESCRIPTION("Intel MAX 10 BMC hardware monitor");
334 MODULE_LICENSE("GPL");
335