summaryrefslogtreecommitdiffstats
path: root/drivers/net/ethernet/netronome/nfp/nfp_hwmon.c
blob: 0d6c59d6d4aed048b322b589f41b7256f4924e75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
/* Copyright (C) 2017 Netronome Systems, Inc. */

#include <linux/kernel.h>
#include <linux/bitops.h>
#include <linux/hwmon.h>

#include "nfpcore/nfp_cpp.h"
#include "nfpcore/nfp_nsp.h"
#include "nfp_main.h"

#define NFP_TEMP_MAX		(95 * 1000)
#define NFP_TEMP_CRIT		(105 * 1000)

#define NFP_POWER_MAX		(25 * 1000 * 1000)

static int nfp_hwmon_sensor_id(enum hwmon_sensor_types type, int channel)
{
	if (type == hwmon_temp)
		return NFP_SENSOR_CHIP_TEMPERATURE;
	if (type == hwmon_power)
		return NFP_SENSOR_ASSEMBLY_POWER + channel;
	return -EINVAL;
}

static int
nfp_hwmon_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
	       int channel, long *val)
{
	static const struct {
		enum hwmon_sensor_types type;
		u32 attr;
		long val;
	} const_vals[] = {
		{ hwmon_temp,	hwmon_temp_max,		NFP_TEMP_MAX },
		{ hwmon_temp,	hwmon_temp_crit,	NFP_TEMP_CRIT },
		{ hwmon_power,	hwmon_power_max,	NFP_POWER_MAX },
	};
	struct nfp_pf *pf = dev_get_drvdata(dev);
	enum nfp_nsp_sensor_id id;
	int err, i;

	for (i = 0; i < ARRAY_SIZE(const_vals); i++)
		if (const_vals[i].type == type && const_vals[i].attr == attr) {
			*val = const_vals[i].val;
			return 0;
		}

	err = nfp_hwmon_sensor_id(type, channel);
	if (err < 0)
		return err;
	id = err;

	if (!(pf->nspi->sensor_mask & BIT(id)))
		return -EOPNOTSUPP;

	if (type == hwmon_temp && attr == hwmon_temp_input)
		return nfp_hwmon_read_sensor(pf->cpp, id, val);
	if (type == hwmon_power && attr == hwmon_power_input)
		return nfp_hwmon_read_sensor(pf->cpp, id, val);

	return -EINVAL;
}

static umode_t
nfp_hwmon_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr,
		     int channel)
{
	if (type == hwmon_temp) {
		switch (attr) {
		case hwmon_temp_input:
		case hwmon_temp_crit:
		case hwmon_temp_max:
			return 0444;
		}
	} else if (type == hwmon_power) {
		switch (attr) {
		case hwmon_power_input:
		case hwmon_power_max:
			return 0444;
		}
	}
	return 0;
}

static u32 nfp_chip_config[] = {
	HWMON_C_REGISTER_TZ,
	0
};

static const struct hwmon_channel_info nfp_chip = {
	.type = hwmon_chip,
	.config = nfp_chip_config,
};

static u32 nfp_temp_config[] = {
	HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_CRIT,
	0
};

static const struct hwmon_channel_info nfp_temp = {
	.type = hwmon_temp,
	.config = nfp_temp_config,
};

static u32 nfp_power_config[] = {
	HWMON_P_INPUT | HWMON_P_MAX,
	HWMON_P_INPUT,
	HWMON_P_INPUT,
	0
};

static const struct hwmon_channel_info nfp_power = {
	.type = hwmon_power,
	.config = nfp_power_config,
};

static const struct hwmon_channel_info * const nfp_hwmon_info[] = {
	&nfp_chip,
	&nfp_temp,
	&nfp_power,
	NULL
};

static const struct hwmon_ops nfp_hwmon_ops = {
	.is_visible = nfp_hwmon_is_visible,
	.read = nfp_hwmon_read,
};

static const struct hwmon_chip_info nfp_chip_info = {
	.ops = &nfp_hwmon_ops,
	.info = nfp_hwmon_info,
};

int nfp_hwmon_register(struct nfp_pf *pf)
{
	if (!IS_REACHABLE(CONFIG_HWMON))
		return 0;

	if (!pf->nspi) {
		nfp_warn(pf->cpp, "not registering HWMON (no NSP info)\n");
		return 0;
	}
	if (!pf->nspi->sensor_mask) {
		nfp_info(pf->cpp,
			 "not registering HWMON (NSP doesn't report sensors)\n");
		return 0;
	}

	pf->hwmon_dev = hwmon_device_register_with_info(&pf->pdev->dev, "nfp",
							pf, &nfp_chip_info,
							NULL);
	return PTR_ERR_OR_ZERO(pf->hwmon_dev);
}

void nfp_hwmon_unregister(struct nfp_pf *pf)
{
	if (!IS_REACHABLE(CONFIG_HWMON) || !pf->hwmon_dev)
		return;

	hwmon_device_unregister(pf->hwmon_dev);
}