1 // SPDX-License-Identifier: GPL-2.0-only
3 * Ampere Computing SoC's SMPro Hardware Monitoring Driver
5 * Copyright (c) 2022, Ampere Computing LLC
7 #include <linux/bitfield.h>
8 #include <linux/bitops.h>
9 #include <linux/hwmon.h>
10 #include <linux/hwmon-sysfs.h>
11 #include <linux/kernel.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/property.h>
16 #include <linux/regmap.h>
18 /* Logical Power Sensor Registers */
20 #define SOC_VRD_TEMP 0x11
21 #define DIMM_VRD_TEMP 0x12
22 #define CORE_VRD_TEMP 0x13
23 #define CH0_DIMM_TEMP 0x14
24 #define CH1_DIMM_TEMP 0x15
25 #define CH2_DIMM_TEMP 0x16
26 #define CH3_DIMM_TEMP 0x17
27 #define CH4_DIMM_TEMP 0x18
28 #define CH5_DIMM_TEMP 0x19
29 #define CH6_DIMM_TEMP 0x1A
30 #define CH7_DIMM_TEMP 0x1B
31 #define RCA_VRD_TEMP 0x1C
33 #define CORE_VRD_PWR 0x20
35 #define DIMM_VRD1_PWR 0x22
36 #define DIMM_VRD2_PWR 0x23
37 #define CORE_VRD_PWR_MW 0x26
38 #define SOC_PWR_MW 0x27
39 #define DIMM_VRD1_PWR_MW 0x28
40 #define DIMM_VRD2_PWR_MW 0x29
41 #define RCA_VRD_PWR 0x2A
42 #define RCA_VRD_PWR_MW 0x2B
44 #define MEM_HOT_THRESHOLD 0x32
45 #define SOC_VR_HOT_THRESHOLD 0x33
46 #define CORE_VRD_VOLT 0x34
47 #define SOC_VRD_VOLT 0x35
48 #define DIMM_VRD1_VOLT 0x36
49 #define DIMM_VRD2_VOLT 0x37
50 #define RCA_VRD_VOLT 0x38
52 #define CORE_VRD_CURR 0x39
53 #define SOC_VRD_CURR 0x3A
54 #define DIMM_VRD1_CURR 0x3B
55 #define DIMM_VRD2_CURR 0x3C
56 #define RCA_VRD_CURR 0x3D
59 struct regmap
*regmap
;
68 static const struct smpro_sensor temperature
[] = {
75 .reg_ext
= SOC_VR_HOT_THRESHOLD
,
76 .label
= "temp2 SoC VRD"
80 .label
= "temp3 DIMM VRD"
84 .label
= "temp4 CORE VRD"
88 .reg_ext
= MEM_HOT_THRESHOLD
,
89 .label
= "temp5 CH0 DIMM"
93 .reg_ext
= MEM_HOT_THRESHOLD
,
94 .label
= "temp6 CH1 DIMM"
98 .reg_ext
= MEM_HOT_THRESHOLD
,
99 .label
= "temp7 CH2 DIMM"
102 .reg
= CH3_DIMM_TEMP
,
103 .reg_ext
= MEM_HOT_THRESHOLD
,
104 .label
= "temp8 CH3 DIMM"
107 .reg
= CH4_DIMM_TEMP
,
108 .reg_ext
= MEM_HOT_THRESHOLD
,
109 .label
= "temp9 CH4 DIMM"
112 .reg
= CH5_DIMM_TEMP
,
113 .reg_ext
= MEM_HOT_THRESHOLD
,
114 .label
= "temp10 CH5 DIMM"
117 .reg
= CH6_DIMM_TEMP
,
118 .reg_ext
= MEM_HOT_THRESHOLD
,
119 .label
= "temp11 CH6 DIMM"
122 .reg
= CH7_DIMM_TEMP
,
123 .reg_ext
= MEM_HOT_THRESHOLD
,
124 .label
= "temp12 CH7 DIMM"
128 .label
= "temp13 RCA VRD"
132 static const struct smpro_sensor voltage
[] = {
134 .reg
= CORE_VRD_VOLT
,
135 .label
= "vout0 CORE VRD"
139 .label
= "vout1 SoC VRD"
142 .reg
= DIMM_VRD1_VOLT
,
143 .label
= "vout2 DIMM VRD1"
146 .reg
= DIMM_VRD2_VOLT
,
147 .label
= "vout3 DIMM VRD2"
151 .label
= "vout4 RCA VRD"
155 static const struct smpro_sensor curr_sensor
[] = {
157 .reg
= CORE_VRD_CURR
,
158 .label
= "iout1 CORE VRD"
162 .label
= "iout2 SoC VRD"
165 .reg
= DIMM_VRD1_CURR
,
166 .label
= "iout3 DIMM VRD1"
169 .reg
= DIMM_VRD2_CURR
,
170 .label
= "iout4 DIMM VRD2"
174 .label
= "iout5 RCA VRD"
178 static const struct smpro_sensor power
[] = {
181 .reg_ext
= CORE_VRD_PWR_MW
,
182 .label
= "power1 CORE VRD"
186 .reg_ext
= SOC_PWR_MW
,
187 .label
= "power2 SoC"
190 .reg
= DIMM_VRD1_PWR
,
191 .reg_ext
= DIMM_VRD1_PWR_MW
,
192 .label
= "power3 DIMM VRD1"
195 .reg
= DIMM_VRD2_PWR
,
196 .reg_ext
= DIMM_VRD2_PWR_MW
,
197 .label
= "power4 DIMM VRD2"
201 .reg_ext
= RCA_VRD_PWR_MW
,
202 .label
= "power5 RCA VRD"
206 static int smpro_read_temp(struct device
*dev
, u32 attr
, int channel
, long *val
)
208 struct smpro_hwmon
*hwmon
= dev_get_drvdata(dev
);
213 case hwmon_temp_input
:
214 ret
= regmap_read(hwmon
->regmap
, temperature
[channel
].reg
, &value
);
218 case hwmon_temp_crit
:
219 ret
= regmap_read(hwmon
->regmap
, temperature
[channel
].reg_ext
, &value
);
227 *val
= sign_extend32(value
, 8) * 1000;
231 static int smpro_read_in(struct device
*dev
, u32 attr
, int channel
, long *val
)
233 struct smpro_hwmon
*hwmon
= dev_get_drvdata(dev
);
239 ret
= regmap_read(hwmon
->regmap
, voltage
[channel
].reg
, &value
);
242 /* 15-bit value in 1mV */
243 *val
= value
& 0x7fff;
250 static int smpro_read_curr(struct device
*dev
, u32 attr
, int channel
, long *val
)
252 struct smpro_hwmon
*hwmon
= dev_get_drvdata(dev
);
257 case hwmon_curr_input
:
258 ret
= regmap_read(hwmon
->regmap
, curr_sensor
[channel
].reg
, &value
);
261 /* Scale reported by the hardware is 1mA */
262 *val
= value
& 0x7fff;
269 static int smpro_read_power(struct device
*dev
, u32 attr
, int channel
, long *val_pwr
)
271 struct smpro_hwmon
*hwmon
= dev_get_drvdata(dev
);
272 unsigned int val
= 0, val_mw
= 0;
276 case hwmon_power_input
:
277 ret
= regmap_read(hwmon
->regmap
, power
[channel
].reg
, &val
);
281 ret
= regmap_read(hwmon
->regmap
, power
[channel
].reg_ext
, &val_mw
);
285 *val_pwr
= (val
& 0x3ff) * 1000000 + (val_mw
& 0x3ff) * 1000;
293 static int smpro_read(struct device
*dev
, enum hwmon_sensor_types type
,
294 u32 attr
, int channel
, long *val
)
298 return smpro_read_temp(dev
, attr
, channel
, val
);
300 return smpro_read_in(dev
, attr
, channel
, val
);
302 return smpro_read_power(dev
, attr
, channel
, val
);
304 return smpro_read_curr(dev
, attr
, channel
, val
);
310 static int smpro_read_string(struct device
*dev
, enum hwmon_sensor_types type
,
311 u32 attr
, int channel
, const char **str
)
316 case hwmon_temp_label
:
317 *str
= temperature
[channel
].label
;
327 *str
= voltage
[channel
].label
;
336 case hwmon_curr_label
:
337 *str
= curr_sensor
[channel
].label
;
346 case hwmon_power_label
:
347 *str
= power
[channel
].label
;
360 static umode_t
smpro_is_visible(const void *data
, enum hwmon_sensor_types type
,
361 u32 attr
, int channel
)
363 const struct smpro_hwmon
*hwmon
= data
;
370 case hwmon_temp_input
:
371 case hwmon_temp_label
:
372 case hwmon_temp_crit
:
373 ret
= regmap_read(hwmon
->regmap
, temperature
[channel
].reg
, &value
);
374 if (ret
|| value
== 0xFFFF)
388 static const struct hwmon_channel_info
* const smpro_info
[] = {
389 HWMON_CHANNEL_INFO(temp
,
390 HWMON_T_INPUT
| HWMON_T_LABEL
,
391 HWMON_T_INPUT
| HWMON_T_LABEL
| HWMON_T_CRIT
,
392 HWMON_T_INPUT
| HWMON_T_LABEL
,
393 HWMON_T_INPUT
| HWMON_T_LABEL
,
394 HWMON_T_INPUT
| HWMON_T_LABEL
| HWMON_T_CRIT
,
395 HWMON_T_INPUT
| HWMON_T_LABEL
| HWMON_T_CRIT
,
396 HWMON_T_INPUT
| HWMON_T_LABEL
| HWMON_T_CRIT
,
397 HWMON_T_INPUT
| HWMON_T_LABEL
| HWMON_T_CRIT
,
398 HWMON_T_INPUT
| HWMON_T_LABEL
| HWMON_T_CRIT
,
399 HWMON_T_INPUT
| HWMON_T_LABEL
| HWMON_T_CRIT
,
400 HWMON_T_INPUT
| HWMON_T_LABEL
| HWMON_T_CRIT
,
401 HWMON_T_INPUT
| HWMON_T_LABEL
| HWMON_T_CRIT
,
402 HWMON_T_INPUT
| HWMON_T_LABEL
),
403 HWMON_CHANNEL_INFO(in
,
404 HWMON_I_INPUT
| HWMON_I_LABEL
,
405 HWMON_I_INPUT
| HWMON_I_LABEL
,
406 HWMON_I_INPUT
| HWMON_I_LABEL
,
407 HWMON_I_INPUT
| HWMON_I_LABEL
,
408 HWMON_I_INPUT
| HWMON_I_LABEL
),
409 HWMON_CHANNEL_INFO(power
,
410 HWMON_P_INPUT
| HWMON_P_LABEL
,
411 HWMON_P_INPUT
| HWMON_P_LABEL
,
412 HWMON_P_INPUT
| HWMON_P_LABEL
,
413 HWMON_P_INPUT
| HWMON_P_LABEL
,
414 HWMON_P_INPUT
| HWMON_P_LABEL
),
415 HWMON_CHANNEL_INFO(curr
,
416 HWMON_C_INPUT
| HWMON_C_LABEL
,
417 HWMON_C_INPUT
| HWMON_C_LABEL
,
418 HWMON_C_INPUT
| HWMON_C_LABEL
,
419 HWMON_C_INPUT
| HWMON_C_LABEL
,
420 HWMON_C_INPUT
| HWMON_C_LABEL
),
424 static const struct hwmon_ops smpro_hwmon_ops
= {
425 .is_visible
= smpro_is_visible
,
427 .read_string
= smpro_read_string
,
430 static const struct hwmon_chip_info smpro_chip_info
= {
431 .ops
= &smpro_hwmon_ops
,
435 static int smpro_hwmon_probe(struct platform_device
*pdev
)
437 struct smpro_hwmon
*hwmon
;
438 struct device
*hwmon_dev
;
440 hwmon
= devm_kzalloc(&pdev
->dev
, sizeof(struct smpro_hwmon
), GFP_KERNEL
);
444 hwmon
->regmap
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
448 hwmon_dev
= devm_hwmon_device_register_with_info(&pdev
->dev
, "smpro_hwmon",
449 hwmon
, &smpro_chip_info
, NULL
);
451 return PTR_ERR_OR_ZERO(hwmon_dev
);
454 static struct platform_driver smpro_hwmon_driver
= {
455 .probe
= smpro_hwmon_probe
,
457 .name
= "smpro-hwmon",
461 module_platform_driver(smpro_hwmon_driver
);
463 MODULE_AUTHOR("Thu Nguyen <thu@os.amperecomputing.com>");
464 MODULE_AUTHOR("Quan Nguyen <quan@os.amperecomputing.com>");
465 MODULE_DESCRIPTION("Ampere Altra SMPro hwmon driver");
466 MODULE_LICENSE("GPL");