summaryrefstree
path: root/kernel/drivers/net/ethernet/aquantia/atlantic-fwd/atl_hwmon.c
blob: 103d2552de22069c9a244c3b30c0915f5b966541 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// SPDX-License-Identifier: GPL-2.0-only
/* Atlantic Network Driver
 *
 * Copyright (C) 2018 aQuantia Corporation
 * Copyright (C) 2019-2020 Marvell International Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include "atl_common.h"
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>

#if LINUX_VERSION_CODE > KERNEL_VERSION(4,10,0)

static ssize_t atl_hwmon_set_flag(struct device *dev,
	struct device_attribute *attr, const char *buf, size_t size)
{
	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
	struct atl_hw *hw = dev_get_drvdata(dev);
	bool val;
	int ret;

	if (strtobool(buf, &val) < 0)
		return -EINVAL;

	ret = atl_update_thermal_flag(hw, sattr->index, val);

	return ret ?: size;
}

static ssize_t atl_hwmon_show_flag(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	struct sensor_device_attribute *sattr = to_sensor_dev_attr(attr);
	struct atl_hw *hw = dev_get_drvdata(dev);

	return snprintf(buf, PAGE_SIZE, "%d\n",
		!!(hw->thermal.flags & BIT(sattr->index)));
}

#define ATL_HWMON_BIT_ATTR(_name, _bit) \
	SENSOR_DEVICE_ATTR(_name, S_IRUGO | S_IWUSR, atl_hwmon_show_flag, \
		atl_hwmon_set_flag, _bit)

static ATL_HWMON_BIT_ATTR(monitor, atl_thermal_monitor_shift);
static ATL_HWMON_BIT_ATTR(throttle, atl_thermal_throttle_shift);
static ATL_HWMON_BIT_ATTR(ignore_lims, atl_thermal_ignore_lims_shift);

static struct attribute *atl_hwmon_attrs[] = {
	&sensor_dev_attr_monitor.dev_attr.attr,
	&sensor_dev_attr_throttle.dev_attr.attr,
	&sensor_dev_attr_ignore_lims.dev_attr.attr,
	NULL,
};
ATTRIBUTE_GROUPS(atl_hwmon);

static char *atl_hwmon_labels[] = {
	"PHY Temperature",
};

static const uint32_t atl_hwmon_temp_config[] = {
	HWMON_T_INPUT | HWMON_T_LABEL | HWMON_T_CRIT | HWMON_T_MAX |
	HWMON_T_MAX_HYST | HWMON_T_MAX_ALARM,
	0,
};

static const struct hwmon_channel_info atl_hwmon_temp = {
	.type = hwmon_temp,
	.config = atl_hwmon_temp_config,
};

static const struct hwmon_channel_info *atl_hwmon_info[] = {
	&atl_hwmon_temp,
	NULL,
};

static umode_t atl_hwmon_is_visible(const void *p,
	enum hwmon_sensor_types type, uint32_t attr, int channel)
{
	if (type != hwmon_temp)
		return 0;

	switch (attr) {
	case hwmon_temp_input: case hwmon_temp_max_alarm: case hwmon_temp_label:
		return S_IRUGO;

	case hwmon_temp_crit: case hwmon_temp_max: case hwmon_temp_max_hyst:
		return S_IRUGO | S_IWUSR;
	}

	return 0;
}

static int atl_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
	uint32_t attr, int channel, long *val)
{
	struct atl_hw *hw = dev_get_drvdata(dev);
	int temp, ret;

	if (type != hwmon_temp)
		return -EINVAL;

	switch (attr) {
	case hwmon_temp_input:
		ret = hw->mcp.ops->get_phy_temperature(hw, &temp);
		if (ret)
			return ret;

		*val = temp;
		break;

	case hwmon_temp_crit:
		*val = hw->thermal.crit * 1000;
		break;

	case hwmon_temp_max:
		*val = hw->thermal.high * 1000;
		break;

	case hwmon_temp_max_hyst:
		*val = hw->thermal.low * 1000;
		break;

	case hwmon_temp_max_alarm:
		*val = hw->link_state.thermal_throttled;
		break;

	default:
		return -EINVAL;
	}
	return 0;
}

static int atl_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
	uint32_t attr, int channel, long val)
{
	struct atl_hw *hw = dev_get_drvdata(dev);
	uint8_t *ptr, old;
	int ret;

	if (type != hwmon_temp)
		return -EINVAL;

	switch (attr) {
	case hwmon_temp_crit:
		ptr = &hw->thermal.crit;
		break;

	case hwmon_temp_max:
		ptr = &hw->thermal.high;
		break;

	case hwmon_temp_max_hyst:
		ptr = &hw->thermal.low;
		break;

	default:
		return -EINVAL;
	}

	old = *ptr;
	*ptr = val / 1000;

	ret = atl_update_thermal(hw);
	if (ret)
		*ptr = old;

	return ret;
}

static int atl_hwmon_read_string(struct device *dev,
	enum hwmon_sensor_types type, u32 attr, int channel, const char **str)
{
	if (type != hwmon_temp || attr != hwmon_temp_label)
		return -EINVAL;

	*str = atl_hwmon_labels[channel];
	return 0;
}

static const struct hwmon_ops atl_hwmon_ops = {
	.is_visible = atl_hwmon_is_visible,
	.read = atl_hwmon_read,
	.read_string = atl_hwmon_read_string,
	.write = atl_hwmon_write,
};

static const struct hwmon_chip_info atl_hwmon = {
	.ops = &atl_hwmon_ops,
	.info = atl_hwmon_info,
};

int atl_hwmon_init(struct atl_nic *nic)
{
	struct device *hwmon_dev;
	struct atl_hw *hw = &nic->hw;

	hwmon_dev = devm_hwmon_device_register_with_info(&hw->pdev->dev,
		nic->ndev->name, hw, &atl_hwmon, atl_hwmon_groups);

	return PTR_ERR_OR_ZERO(hwmon_dev);
}

#else

int atl_hwmon_init(struct atl_nic *nic)
{
	return 0;
}

#endif