summaryrefslogtreecommitdiffstats
path: root/drivers/hwmon/pmbus/fsp-3y.c
blob: 284b73aaed46700517ea8e9840ade24df23527d1 (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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Hardware monitoring driver for FSP 3Y-Power PSUs
 *
 * Copyright (c) 2021 Václav Kubernát, CESNET
 *
 * This driver is mostly reverse engineered with the help of a tool called pmbus_peek written by
 * David Brownell (and later adopted by Jan Kundrát). The device has some sort of a timing issue
 * when switching pages, details are explained in the code. The driver support is limited. It
 * exposes only the values, that have been tested to work correctly. Unsupported values either
 * aren't supported by the devices or their encondings are unknown.
 */

#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include "pmbus.h"

#define YM2151_PAGE_12V_LOG	0x00
#define YM2151_PAGE_12V_REAL	0x00
#define YM2151_PAGE_5VSB_LOG	0x01
#define YM2151_PAGE_5VSB_REAL	0x20
#define YH5151E_PAGE_12V_LOG	0x00
#define YH5151E_PAGE_12V_REAL	0x00
#define YH5151E_PAGE_5V_LOG	0x01
#define YH5151E_PAGE_5V_REAL	0x10
#define YH5151E_PAGE_3V3_LOG	0x02
#define YH5151E_PAGE_3V3_REAL	0x11

enum chips {
	ym2151e,
	yh5151e
};

struct fsp3y_data {
	struct pmbus_driver_info info;
	int chip;
	int page;
};

#define to_fsp3y_data(x) container_of(x, struct fsp3y_data, info)

static int page_log_to_page_real(int page_log, enum chips chip)
{
	switch (chip) {
	case ym2151e:
		switch (page_log) {
		case YM2151_PAGE_12V_LOG:
			return YM2151_PAGE_12V_REAL;
		case YM2151_PAGE_5VSB_LOG:
			return YM2151_PAGE_5VSB_REAL;
		}
		return -EINVAL;
	case yh5151e:
		switch (page_log) {
		case YH5151E_PAGE_12V_LOG:
			return YH5151E_PAGE_12V_REAL;
		case YH5151E_PAGE_5V_LOG:
			return YH5151E_PAGE_5V_LOG;
		case YH5151E_PAGE_3V3_LOG:
			return YH5151E_PAGE_3V3_REAL;
		}
		return -EINVAL;
	}

	return -EINVAL;
}

static int set_page(struct i2c_client *client, int page_log)
{
	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
	struct fsp3y_data *data = to_fsp3y_data(info);
	int rv;
	int page_real;

	if (page_log < 0)
		return 0;

	page_real = page_log_to_page_real(page_log, data->chip);
	if (page_real < 0)
		return page_real;

	if (data->page != page_real) {
		rv = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page_real);
		if (rv < 0)
			return rv;

		data->page = page_real;

		/*
		 * Testing showed that the device has a timing issue. After
		 * setting a page, it takes a while, before the device actually
		 * gives the correct values from the correct page. 20 ms was
		 * tested to be enough to not give wrong values (15 ms wasn't
		 * enough).
		 */
		usleep_range(20000, 30000);
	}

	return 0;
}

static int fsp3y_read_byte_data(struct i2c_client *client, int page, int reg)
{
	int rv;

	rv = set_page(client, page);
	if (rv < 0)
		return rv;

	return i2c_smbus_read_byte_data(client, reg);
}

static int fsp3y_read_word_data(struct i2c_client *client, int page, int phase, int reg)
{
	int rv;

	/*
	 * This masks commands which weren't tested to work correctly. Some of
	 * the masked commands return 0xFFFF. These would probably get tagged as
	 * invalid by pmbus_core. Other ones do return values which might be
	 * useful (that is, they are not 0xFFFF), but their encoding is unknown,
	 * and so they are unsupported.
	 */
	switch (reg) {
	case PMBUS_READ_FAN_SPEED_1:
	case PMBUS_READ_IIN:
	case PMBUS_READ_IOUT:
	case PMBUS_READ_PIN:
	case PMBUS_READ_POUT:
	case PMBUS_READ_TEMPERATURE_1:
	case PMBUS_READ_TEMPERATURE_2:
	case PMBUS_READ_TEMPERATURE_3:
	case PMBUS_READ_VIN:
	case PMBUS_READ_VOUT:
	case PMBUS_STATUS_WORD:
		break;
	default:
		return -ENXIO;
	}

	rv = set_page(client, page);
	if (rv < 0)
		return rv;

	return i2c_smbus_read_word_data(client, reg);
}

static struct pmbus_driver_info fsp3y_info[] = {
	[ym2151e] = {
		.pages = 2,
		.func[YM2151_PAGE_12V_LOG] =
			PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
			PMBUS_HAVE_PIN | PMBUS_HAVE_POUT  |
			PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 |
			PMBUS_HAVE_VIN | PMBUS_HAVE_IIN |
			PMBUS_HAVE_FAN12,
		.func[YM2151_PAGE_5VSB_LOG] =
			PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT,
			PMBUS_HAVE_IIN,
		.read_word_data = fsp3y_read_word_data,
		.read_byte_data = fsp3y_read_byte_data,
	},
	[yh5151e] = {
		.pages = 3,
		.func[YH5151E_PAGE_12V_LOG] =
			PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
			PMBUS_HAVE_POUT  |
			PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_TEMP3,
		.func[YH5151E_PAGE_5V_LOG] =
			PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
			PMBUS_HAVE_POUT,
		.func[YH5151E_PAGE_3V3_LOG] =
			PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT |
			PMBUS_HAVE_POUT,
		.read_word_data = fsp3y_read_word_data,
		.read_byte_data = fsp3y_read_byte_data,
	}
};

static int fsp3y_detect(struct i2c_client *client)
{
	int rv;
	u8 buf[I2C_SMBUS_BLOCK_MAX + 1];

	rv = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, buf);
	if (rv < 0)
		return rv;

	buf[rv] = '\0';

	if (rv == 8) {
		if (!strcmp(buf, "YM-2151E"))
			return ym2151e;
		else if (!strcmp(buf, "YH-5151E"))
			return yh5151e;
	}

	dev_err(&client->dev, "Unsupported model %.*s\n", rv, buf);
	return -ENODEV;
}

static const struct i2c_device_id fsp3y_id[] = {
	{"ym2151e", ym2151e},
	{"yh5151e", yh5151e},
	{ }
};

static int fsp3y_probe(struct i2c_client *client)
{
	struct fsp3y_data *data;
	const struct i2c_device_id *id;
	int rv;

	data = devm_kzalloc(&client->dev, sizeof(struct fsp3y_data), GFP_KERNEL);
	if (!data)
		return -ENOMEM;

	data->chip = fsp3y_detect(client);
	if (data->chip < 0)
		return data->chip;

	id = i2c_match_id(fsp3y_id, client);
	if (data->chip != id->driver_data)
		dev_warn(&client->dev, "Device mismatch: Configured %s (%d), detected %d\n",
			 id->name, (int)id->driver_data, data->chip);

	rv = i2c_smbus_read_byte_data(client, PMBUS_PAGE);
	if (rv < 0)
		return rv;
	data->page = rv;

	data->info = fsp3y_info[data->chip];

	return pmbus_do_probe(client, &data->info);
}

MODULE_DEVICE_TABLE(i2c, fsp3y_id);

static struct i2c_driver fsp3y_driver = {
	.driver = {
		   .name = "fsp3y",
		   },
	.probe_new = fsp3y_probe,
	.id_table = fsp3y_id
};

module_i2c_driver(fsp3y_driver);

MODULE_AUTHOR("Václav Kubernát");
MODULE_DESCRIPTION("PMBus driver for FSP/3Y-Power power supplies");
MODULE_LICENSE("GPL");