summaryrefslogtreecommitdiff
path: root/drivers/hwmon/pmbus/vt7505.c
blob: 6a66f06eddb86b18cece11ac30d73b789954e1fb (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Hardware monitoring driver for Analog Devices MAX16545/MAX16550 and
 * Volterra VT7505 PMBus controllers.
 *
 * Copyright 2026 Hewlett Packard Enterprise Development LP
 */

#include <linux/bitfield.h>
#include <linux/i2c.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/pmbus.h>

#include "pmbus.h"

#define VT7505_MFR_CONFIG		0xd0
#define VT7505_CFG_OCP_S_FILT_MASK	GENMASK(15, 14)

#define VT7505_MFR_PEAK_VIN		0xd1
#define VT7505_MFR_PEAK_IOUT		0xd2
#define VT7505_MFR_PEAK_PIN		0xd3
#define VT7505_MFR_PEAK_TEMP		0xd4
#define VT7505_MFR_CLEAR_PEAKS		0xd5
#define VT7505_MFR_PEAK_VOUT		0xfd

#define VT7505_RLOAD_DEFAULT		4750
/* Largest RLOAD for which coeff * rload / 1000 still fits in s32. */
#define VT7505_RLOAD_MAX		283383959U

struct vt7505_chip_data {
	int temp_m;
	int temp_b;
	bool has_ocp_filter;
};

static const struct vt7505_chip_data max16545_data = {
	.temp_m = 205,
	.temp_b = 6545,
};

static const struct vt7505_chip_data max16550_data = {
	.temp_m = 199,
	.temp_b = 7046,
	.has_ocp_filter = true,
};

static const struct vt7505_chip_data vt7505_data = {
	.temp_m = 205,
	.temp_b = 6545,
	.has_ocp_filter = true,
};

static int vt7505_read_word_data(struct i2c_client *client, int page,
				 int phase, int reg)
{
	switch (reg) {
	case PMBUS_VIRT_READ_VIN_MAX:
		return pmbus_read_word_data(client, page, phase,
					    VT7505_MFR_PEAK_VIN);
	case PMBUS_VIRT_READ_IOUT_MAX:
		return pmbus_read_word_data(client, page, phase,
					    VT7505_MFR_PEAK_IOUT);
	case PMBUS_VIRT_READ_PIN_MAX:
		return pmbus_read_word_data(client, page, phase,
					    VT7505_MFR_PEAK_PIN);
	case PMBUS_VIRT_READ_TEMP_MAX:
		return pmbus_read_word_data(client, page, phase,
					    VT7505_MFR_PEAK_TEMP);
	case PMBUS_VIRT_READ_VOUT_MAX:
		return pmbus_read_word_data(client, page, phase,
					    VT7505_MFR_PEAK_VOUT);
	case PMBUS_VIRT_RESET_VIN_HISTORY:
	case PMBUS_VIRT_RESET_IOUT_HISTORY:
	case PMBUS_VIRT_RESET_PIN_HISTORY:
	case PMBUS_VIRT_RESET_TEMP_HISTORY:
	case PMBUS_VIRT_RESET_VOUT_HISTORY:
		return 0;
	default:
		return -ENODATA;
	}
}

static int vt7505_write_word_data(struct i2c_client *client, int page,
				  int reg, u16 word)
{
	switch (reg) {
	/*
	 * A single reset command clears all peak values. CLEAR_PEAKS is a
	 * send-byte command; the device NAKs a word or byte-data write to it.
	 */
	case PMBUS_VIRT_RESET_VIN_HISTORY:
	case PMBUS_VIRT_RESET_IOUT_HISTORY:
	case PMBUS_VIRT_RESET_PIN_HISTORY:
	case PMBUS_VIRT_RESET_TEMP_HISTORY:
	case PMBUS_VIRT_RESET_VOUT_HISTORY:
		return pmbus_write_byte(client, page,
					VT7505_MFR_CLEAR_PEAKS);
	default:
		return -ENODATA;
	}
}

/*
 * None of these controllers implement the standard PMBus WRITE_PROTECT
 * (0x10) register, so tell the core not to access it.
 */
static struct pmbus_platform_data vt7505_pdata = {
	.flags = PMBUS_NO_WRITE_PROTECT,
};

static int vt7505_set_ocp_filter(struct i2c_client *client)
{
	u32 ocp_us;
	u8 field;
	int ret;
	u16 word;

	if (of_property_read_u32(client->dev.of_node, "adi,ocp-severe-filter-us",
				 &ocp_us))
		return 0;

	switch (ocp_us) {
	case 0:
		field = 0;
		break;
	case 1:
		field = 1;
		break;
	case 2:
		field = 2;
		break;
	case 10:
		field = 3;
		break;
	default:
		return dev_err_probe(&client->dev, -EINVAL,
				     "invalid adi,ocp-severe-filter-us value %u\n",
				     ocp_us);
	}

	ret = i2c_smbus_read_word_data(client, VT7505_MFR_CONFIG);
	if (ret < 0)
		return dev_err_probe(&client->dev, ret,
				     "failed to read MFR_CONFIG\n");

	word = ret & ~VT7505_CFG_OCP_S_FILT_MASK;
	word |= FIELD_PREP(VT7505_CFG_OCP_S_FILT_MASK, field);

	ret = i2c_smbus_write_word_data(client, VT7505_MFR_CONFIG, word);
	if (ret < 0)
		return dev_err_probe(&client->dev, ret,
				     "failed to write MFR_CONFIG\n");

	return 0;
}

static void vt7505_set_m(int *m, u32 rload)
{
	u64 val = (u64)*m * rload;

	/* rload is range-checked in probe, so the result fits in int. */
	*m = DIV_ROUND_CLOSEST_ULL(val, 1000);
}

static int vt7505_probe(struct i2c_client *client)
{
	struct device *dev = &client->dev;
	const struct vt7505_chip_data *chip;
	struct pmbus_driver_info *info;
	u32 rload;
	int ret;

	chip = i2c_get_match_data(client);
	if (!chip)
		return -ENODEV;

	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
	if (!info)
		return -ENOMEM;

	dev->platform_data = &vt7505_pdata;

	/*
	 * The m coefficient used in the direct-format current and power
	 * calculations depends on RLOAD, the external current-report resistor
	 * connected between the ILOAD pin and ground. Use the default value if
	 * none is specified.
	 */
	if (of_property_read_u32(dev->of_node, "adi,rload-ohms", &rload))
		rload = VT7505_RLOAD_DEFAULT;

	if (!rload || rload > VT7505_RLOAD_MAX)
		return dev_err_probe(dev, -EINVAL,
				     "adi,rload-ohms must be 1-%u\n",
				     VT7505_RLOAD_MAX);

	info->pages = 1;
	info->read_word_data = vt7505_read_word_data;
	info->write_word_data = vt7505_write_word_data;

	info->format[PSC_VOLTAGE_IN] = direct;
	info->format[PSC_VOLTAGE_OUT] = direct;
	info->format[PSC_CURRENT_IN] = direct;
	info->format[PSC_CURRENT_OUT] = direct;
	info->format[PSC_POWER] = direct;
	info->format[PSC_TEMPERATURE] = direct;

	/*
	 * Direct data format coefficients from the device datasheet ("PMBus
	 * Equation Parameters"). The current and power m coefficients scale
	 * with RLOAD; vt7505_set_m() applies the 1/1000 factor below, giving
	 * m = 3.824 * RLOAD for current and 0.895 * RLOAD for power. The
	 * temperature coefficients are chip specific (see the chip data).
	 */
	info->m[PSC_VOLTAGE_IN] = 7578;
	info->R[PSC_VOLTAGE_IN] = -2;
	info->m[PSC_VOLTAGE_OUT] = 7578;
	info->R[PSC_VOLTAGE_OUT] = -2;
	info->m[PSC_CURRENT_IN] = 3824;
	info->b[PSC_CURRENT_IN] = -4300;
	info->R[PSC_CURRENT_IN] = -3;
	info->m[PSC_CURRENT_OUT] = 3824;
	info->b[PSC_CURRENT_OUT] = -4300;
	info->R[PSC_CURRENT_OUT] = -3;
	info->m[PSC_POWER] = 895;
	info->b[PSC_POWER] = -9100;
	info->R[PSC_POWER] = -2;

	vt7505_set_m(&info->m[PSC_CURRENT_IN], rload);
	vt7505_set_m(&info->m[PSC_CURRENT_OUT], rload);
	vt7505_set_m(&info->m[PSC_POWER], rload);

	info->m[PSC_TEMPERATURE] = chip->temp_m;
	info->b[PSC_TEMPERATURE] = chip->temp_b;
	info->R[PSC_TEMPERATURE] = -2;

	info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT |
			PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
			PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
			PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
			PMBUS_HAVE_IIN | PMBUS_HAVE_PIN;

	/*
	 * The severe OCP deglitch filter is programmable on the MAX16550 and
	 * the VT7505, but fixed on the MAX16545.
	 */
	if (chip->has_ocp_filter) {
		ret = vt7505_set_ocp_filter(client);
		if (ret)
			return ret;
	}

	return pmbus_do_probe(client, info);
}

static const struct i2c_device_id vt7505_id[] = {
	{ .name = "max16545", .driver_data = (kernel_ulong_t)&max16545_data },
	{ .name = "max16550", .driver_data = (kernel_ulong_t)&max16550_data },
	{ .name = "vt7505", .driver_data = (kernel_ulong_t)&vt7505_data },
	{ }
};
MODULE_DEVICE_TABLE(i2c, vt7505_id);

static const struct of_device_id vt7505_of_match[] = {
	{ .compatible = "adi,max16545", .data = &max16545_data },
	{ .compatible = "adi,max16550", .data = &max16550_data },
	{ .compatible = "adi,vt7505", .data = &vt7505_data },
	{ }
};
MODULE_DEVICE_TABLE(of, vt7505_of_match);

static struct i2c_driver vt7505_driver = {
	.driver = {
		.name = "vt7505",
		.of_match_table = vt7505_of_match,
	},
	.probe = vt7505_probe,
	.id_table = vt7505_id,
};
module_i2c_driver(vt7505_driver);

MODULE_AUTHOR("Georgi Vlaev <gvlaev@juniper.net>");
MODULE_DESCRIPTION("PMBus driver for Analog Devices MAX16545/MAX16550 and Volterra VT7505");
MODULE_LICENSE("GPL");
MODULE_IMPORT_NS("PMBUS");