summaryrefslogtreecommitdiff
path: root/drivers/hwmon/pmbus/mp2888.c
blob: 50662ed8e3d58309a8ef428a19b766cb05a23fb9 (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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Hardware monitoring driver for MPS Multi-phase Digital VR Controllers
 *
 * Copyright (C) 2020 Nvidia Technologies Ltd.
 */

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

/* Vendor specific registers. */
#define MP2888_MFR_SYS_CONFIG	0x44
#define MP2888_MFR_READ_CS1_2	0x73
#define MP2888_MFR_READ_CS3_4	0x74
#define MP2888_MFR_READ_CS5_6	0x75
#define MP2888_MFR_READ_CS7_8	0x76
#define MP2888_MFR_READ_CS9_10	0x77
#define MP2888_MFR_VR_CONFIG1	0xe1

#define MP2888_TOTAL_CURRENT_RESOLUTION	BIT(3)
#define MP2888_PHASE_CURRENT_RESOLUTION	BIT(4)
#define MP2888_DRMOS_KCS		GENMASK(2, 0)
#define MP2888_TEMP_UNIT		10
#define MP2888_MAX_PHASE		10

struct mp2888_data {
	struct pmbus_driver_info info;
	int total_curr_resolution;
	int phase_curr_resolution;
	int curr_sense_gain;
};

#define to_mp2888_data(x)	container_of(x, struct mp2888_data, info)

static int mp2888_read_byte_data(struct i2c_client *client, int page, int reg)
{
	switch (reg) {
	case PMBUS_VOUT_MODE:
		/* Enforce VOUT direct format. */
		return PB_VOUT_MODE_DIRECT;
	default:
		return -ENODATA;
	}
}

static int
mp2888_current_sense_gain_and_resolution_get(struct i2c_client *client, struct mp2888_data *data)
{
	int ret;

	/*
	 * Obtain DrMOS current sense gain of power stage from the register
	 * , bits 0-2. The value is selected as below:
	 * 00b - 5µA/A, 01b - 8.5µA/A, 10b - 9.7µA/A, 11b - 10µA/A. Other
	 * values are reserved.
	 */
	ret = i2c_smbus_read_word_data(client, MP2888_MFR_SYS_CONFIG);
	if (ret < 0)
		return ret;

	switch (ret & MP2888_DRMOS_KCS) {
	case 0:
		data->curr_sense_gain = 85;
		break;
	case 1:
		data->curr_sense_gain = 97;
		break;
	case 2:
		data->curr_sense_gain = 100;
		break;
	case 3:
		data->curr_sense_gain = 50;
		break;
	default:
		return -EINVAL;
	}

	/*
	 * Obtain resolution selector for total and phase current report and protection.
	 * 0: original resolution; 1: half resolution (in such case phase current value should
	 * be doubled.
	 */
	data->total_curr_resolution = (ret & MP2888_TOTAL_CURRENT_RESOLUTION) >> 3;
	data->phase_curr_resolution = (ret & MP2888_PHASE_CURRENT_RESOLUTION) >> 4;

	return 0;
}

static int
mp2888_read_phase(struct i2c_client *client, struct mp2888_data *data, int page, int phase, u8 reg)
{
	int ret;

	ret = pmbus_read_word_data(client, page, phase, reg);
	if (ret < 0)
		return ret;

	if (!((phase + 1) % 2))
		ret >>= 8;
	ret &= 0xff;

	/*
	 * Output value is calculated as: (READ_CSx / 80 – 1.23) / (Kcs * Rcs)
	 * where:
	 * - Kcs is the DrMOS current sense gain of power stage, which is obtained from the
	 *   register MP2888_MFR_VR_CONFIG1, bits 13-12 with the following selection of DrMOS
	 *   (data->curr_sense_gain):
	 *   00b - 8.5µA/A, 01b - 9.7µA/A, 1b - 10µA/A, 11b - 5µA/A.
	 * - Rcs is the internal phase current sense resistor. This parameter depends on hardware
	 *   assembly. By default it is set to 1kΩ. In case of different assembly, user should
	 *   scale this parameter by dividing it by Rcs.
	 * If phase current resolution bit is set to 1, READ_CSx value should be doubled.
	 * Note, that current phase sensing, providing by the device is not accurate. This is
	 * because sampling of current occurrence of bit weight has a big deviation, especially for
	 * light load.
	 */
	ret = DIV_ROUND_CLOSEST(ret * 200 - 19600, data->curr_sense_gain);
	/* Scale according to total current resolution. */
	ret = (data->total_curr_resolution) ? ret * 2 : ret;
	return ret;
}

static int
mp2888_read_phases(struct i2c_client *client, struct mp2888_data *data, int page, int phase)
{
	int ret;

	switch (phase) {
	case 0 ... 1:
		ret = mp2888_read_phase(client, data, page, phase, MP2888_MFR_READ_CS1_2);
		break;
	case 2 ... 3:
		ret = mp2888_read_phase(client, data, page, phase, MP2888_MFR_READ_CS3_4);
		break;
	case 4 ... 5:
		ret = mp2888_read_phase(client, data, page, phase, MP2888_MFR_READ_CS5_6);
		break;
	case 6 ... 7:
		ret = mp2888_read_phase(client, data, page, phase, MP2888_MFR_READ_CS7_8);
		break;
	case 8 ... 9:
		ret = mp2888_read_phase(client, data, page, phase, MP2888_MFR_READ_CS9_10);
		break;
	default:
		return -ENODATA;
	}
	return ret;
}

static int mp2888_read_word_data(struct i2c_client *client, int page, int phase, int reg)
{
	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
	struct mp2888_data *data = to_mp2888_data(info);
	int ret;

	switch (reg) {
	case PMBUS_READ_VIN:
		ret = pmbus_read_word_data(client, page, phase, reg);
		if (ret <= 0)
			return ret;

		/*
		 * READ_VIN requires fixup to scale it to linear11 format. Register data format
		 * provides 10 bits for mantissa and 6 bits for exponent. Bits 15:10 are set with
		 * the fixed value 111011b.
		 */
		ret = (ret & GENMASK(9, 0)) | ((ret & GENMASK(31, 10)) << 1);
		break;
	case PMBUS_OT_WARN_LIMIT:
		ret = pmbus_read_word_data(client, page, phase, reg);
		if (ret < 0)
			return ret;
		/*
		 * Chip reports limits in degrees C, but the actual temperature in 10th of
		 * degrees C - scaling is needed to match both.
		 */
		ret *= MP2888_TEMP_UNIT;
		break;
	case PMBUS_READ_IOUT:
		if (phase != 0xff)
			return mp2888_read_phases(client, data, page, phase);

		ret = pmbus_read_word_data(client, page, phase, reg);
		if (ret < 0)
			return ret;
		/*
		 * READ_IOUT register has unused bits 15:12 with fixed value 1110b. Clear these
		 * bits and scale with total current resolution. Data is provided in direct format.
		 */
		ret &= GENMASK(11, 0);
		ret = data->total_curr_resolution ? ret * 2 : ret;
		break;
	case PMBUS_IOUT_OC_WARN_LIMIT:
		ret = pmbus_read_word_data(client, page, phase, reg);
		if (ret < 0)
			return ret;
		ret &= GENMASK(9, 0);
		/*
		 * Chip reports limits with resolution 1A or 2A, if total current resolution bit is
		 * set 1. Actual current is reported with 0.25A or respectively 0.5A resolution.
		 * Scaling is needed to match both.
		 */
		ret = data->total_curr_resolution ? ret * 8 : ret * 4;
		break;
	case PMBUS_READ_POUT:
	case PMBUS_READ_PIN:
		ret = pmbus_read_word_data(client, page, phase, reg);
		if (ret < 0)
			return ret;
		ret = data->total_curr_resolution ? ret : DIV_ROUND_CLOSEST(ret, 2);
		break;
	case PMBUS_POUT_OP_WARN_LIMIT:
		ret = pmbus_read_word_data(client, page, phase, reg);
		if (ret < 0)
			return ret;
		/*
		 * Chip reports limits with resolution 1W or 2W, if total current resolution bit is
		 * set 1. Actual power is reported with 0.5W or 1W respectively resolution. Scaling
		 * is needed to match both.
		 */
		ret = data->total_curr_resolution ? ret * 2 : ret;
		break;
	/*
	 * The below registers are not implemented by device or implemented not according to the
	 * spec. Skip all of them to avoid exposing non-relevant inputs to sysfs.
	 */
	case PMBUS_OT_FAULT_LIMIT:
	case PMBUS_UT_WARN_LIMIT:
	case PMBUS_UT_FAULT_LIMIT:
	case PMBUS_VIN_UV_FAULT_LIMIT:
	case PMBUS_VOUT_UV_WARN_LIMIT:
	case PMBUS_VOUT_OV_WARN_LIMIT:
	case PMBUS_VOUT_UV_FAULT_LIMIT:
	case PMBUS_VOUT_OV_FAULT_LIMIT:
	case PMBUS_VIN_OV_WARN_LIMIT:
	case PMBUS_IOUT_OC_LV_FAULT_LIMIT:
	case PMBUS_IOUT_OC_FAULT_LIMIT:
	case PMBUS_POUT_MAX:
	case PMBUS_IOUT_UC_FAULT_LIMIT:
	case PMBUS_POUT_OP_FAULT_LIMIT:
	case PMBUS_PIN_OP_WARN_LIMIT:
	case PMBUS_MFR_VIN_MIN:
	case PMBUS_MFR_VOUT_MIN:
	case PMBUS_MFR_VIN_MAX:
	case PMBUS_MFR_VOUT_MAX:
	case PMBUS_MFR_IIN_MAX:
	case PMBUS_MFR_IOUT_MAX:
	case PMBUS_MFR_PIN_MAX:
	case PMBUS_MFR_POUT_MAX:
	case PMBUS_MFR_MAX_TEMP_1:
		return -ENXIO;
	default:
		return -ENODATA;
	}

	return ret;
}

static int mp2888_write_word_data(struct i2c_client *client, int page, int reg, u16 word)
{
	const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
	struct mp2888_data *data = to_mp2888_data(info);

	switch (reg) {
	case PMBUS_OT_WARN_LIMIT:
		word = DIV_ROUND_CLOSEST(word, MP2888_TEMP_UNIT);
		/* Drop unused bits 15:8. */
		word = clamp_val(word, 0, GENMASK(7, 0));
		break;
	case PMBUS_IOUT_OC_WARN_LIMIT:
		/* Fix limit according to total curent resolution. */
		word = data->total_curr_resolution ? DIV_ROUND_CLOSEST(word, 8) :
		       DIV_ROUND_CLOSEST(word, 4);
		/* Drop unused bits 15:10. */
		word = clamp_val(word, 0, GENMASK(9, 0));
		break;
	case PMBUS_POUT_OP_WARN_LIMIT:
		/* Fix limit according to total curent resolution. */
		word = data->total_curr_resolution ? DIV_ROUND_CLOSEST(word, 4) :
		       DIV_ROUND_CLOSEST(word, 2);
		/* Drop unused bits 15:10. */
		word = clamp_val(word, 0, GENMASK(9, 0));
		break;
	default:
		return -ENODATA;
	}
	return pmbus_write_word_data(client, page, reg, word);
}

static int
mp2888_identify_multiphase(struct i2c_client *client, struct mp2888_data *data,
			   struct pmbus_driver_info *info)
{
	int ret;

	ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 0);
	if (ret < 0)
		return ret;

	/* Identify multiphase number - could be from 1 to 10. */
	ret = i2c_smbus_read_word_data(client, MP2888_MFR_VR_CONFIG1);
	if (ret <= 0)
		return ret;

	info->phases[0] = ret & GENMASK(3, 0);

	/*
	 * The device provides a total of 10 PWM pins, and can be configured to different phase
	 * count applications for rail.
	 */
	if (info->phases[0] > MP2888_MAX_PHASE)
		return -EINVAL;

	return 0;
}

static struct pmbus_driver_info mp2888_info = {
	.pages = 1,
	.format[PSC_VOLTAGE_IN] = linear,
	.format[PSC_VOLTAGE_OUT] = direct,
	.format[PSC_TEMPERATURE] = direct,
	.format[PSC_CURRENT_IN] = linear,
	.format[PSC_CURRENT_OUT] = direct,
	.format[PSC_POWER] = direct,
	.m[PSC_TEMPERATURE] = 1,
	.R[PSC_TEMPERATURE] = 1,
	.m[PSC_VOLTAGE_OUT] = 1,
	.R[PSC_VOLTAGE_OUT] = 3,
	.m[PSC_CURRENT_OUT] = 4,
	.m[PSC_POWER] = 1,
	.func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_IOUT |
		   PMBUS_HAVE_STATUS_IOUT | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
		   PMBUS_HAVE_POUT | PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT |
		   PMBUS_PHASE_VIRTUAL,
	.pfunc[0] = PMBUS_HAVE_IOUT,
	.pfunc[1] = PMBUS_HAVE_IOUT,
	.pfunc[2] = PMBUS_HAVE_IOUT,
	.pfunc[3] = PMBUS_HAVE_IOUT,
	.pfunc[4] = PMBUS_HAVE_IOUT,
	.pfunc[5] = PMBUS_HAVE_IOUT,
	.pfunc[6] = PMBUS_HAVE_IOUT,
	.pfunc[7] = PMBUS_HAVE_IOUT,
	.pfunc[8] = PMBUS_HAVE_IOUT,
	.pfunc[9] = PMBUS_HAVE_IOUT,
	.read_byte_data = mp2888_read_byte_data,
	.read_word_data = mp2888_read_word_data,
	.write_word_data = mp2888_write_word_data,
};

static int mp2888_probe(struct i2c_client *client)
{
	struct pmbus_driver_info *info;
	struct mp2888_data *data;
	int ret;

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

	memcpy(&data->info, &mp2888_info, sizeof(*info));
	info = &data->info;

	/* Identify multiphase configuration. */
	ret = mp2888_identify_multiphase(client, data, info);
	if (ret)
		return ret;

	/* Obtain current sense gain of power stage and current resolution. */
	ret = mp2888_current_sense_gain_and_resolution_get(client, data);
	if (ret)
		return ret;

	return pmbus_do_probe(client, info);
}

static const struct i2c_device_id mp2888_id[] = {
	{"mp2888", 0},
	{}
};

MODULE_DEVICE_TABLE(i2c, mp2888_id);

static const struct of_device_id __maybe_unused mp2888_of_match[] = {
	{.compatible = "mps,mp2888"},
	{}
};
MODULE_DEVICE_TABLE(of, mp2888_of_match);

static struct i2c_driver mp2888_driver = {
	.driver = {
		.name = "mp2888",
		.of_match_table = of_match_ptr(mp2888_of_match),
	},
	.probe = mp2888_probe,
	.id_table = mp2888_id,
};

module_i2c_driver(mp2888_driver);

MODULE_AUTHOR("Vadim Pasternak <vadimp@nvidia.com>");
MODULE_DESCRIPTION("PMBus driver for MPS MP2888 device");
MODULE_LICENSE("GPL");
MODULE_IMPORT_NS(PMBUS);