]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/hwmon/pmbus/mp2888.c
Merge tag 'imx-fixes-5.15-3' of git://git.kernel.org/pub/scm/linux/kernel/git/shawngu...
[mirror_ubuntu-jammy-kernel.git] / drivers / hwmon / pmbus / mp2888.c
CommitLineData
e4db7719
VP
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Hardware monitoring driver for MPS Multi-phase Digital VR Controllers
4 *
5 * Copyright (C) 2020 Nvidia Technologies Ltd.
6 */
7
8#include <linux/err.h>
9#include <linux/i2c.h>
10#include <linux/init.h>
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include "pmbus.h"
14
15/* Vendor specific registers. */
16#define MP2888_MFR_SYS_CONFIG 0x44
17#define MP2888_MFR_READ_CS1_2 0x73
18#define MP2888_MFR_READ_CS3_4 0x74
19#define MP2888_MFR_READ_CS5_6 0x75
20#define MP2888_MFR_READ_CS7_8 0x76
21#define MP2888_MFR_READ_CS9_10 0x77
22#define MP2888_MFR_VR_CONFIG1 0xe1
23
24#define MP2888_TOTAL_CURRENT_RESOLUTION BIT(3)
25#define MP2888_PHASE_CURRENT_RESOLUTION BIT(4)
26#define MP2888_DRMOS_KCS GENMASK(2, 0)
27#define MP2888_TEMP_UNIT 10
28#define MP2888_MAX_PHASE 10
29
30struct mp2888_data {
31 struct pmbus_driver_info info;
32 int total_curr_resolution;
33 int phase_curr_resolution;
34 int curr_sense_gain;
35};
36
37#define to_mp2888_data(x) container_of(x, struct mp2888_data, info)
38
39static int mp2888_read_byte_data(struct i2c_client *client, int page, int reg)
40{
41 switch (reg) {
42 case PMBUS_VOUT_MODE:
43 /* Enforce VOUT direct format. */
44 return PB_VOUT_MODE_DIRECT;
45 default:
46 return -ENODATA;
47 }
48}
49
50static int
51mp2888_current_sense_gain_and_resolution_get(struct i2c_client *client, struct mp2888_data *data)
52{
53 int ret;
54
55 /*
56 * Obtain DrMOS current sense gain of power stage from the register
57 * , bits 0-2. The value is selected as below:
58 * 00b - 5µA/A, 01b - 8.5µA/A, 10b - 9.7µA/A, 11b - 10µA/A. Other
59 * values are reserved.
60 */
61 ret = i2c_smbus_read_word_data(client, MP2888_MFR_SYS_CONFIG);
62 if (ret < 0)
63 return ret;
64
65 switch (ret & MP2888_DRMOS_KCS) {
66 case 0:
67 data->curr_sense_gain = 85;
68 break;
69 case 1:
70 data->curr_sense_gain = 97;
71 break;
72 case 2:
73 data->curr_sense_gain = 100;
74 break;
75 case 3:
76 data->curr_sense_gain = 50;
77 break;
78 default:
79 return -EINVAL;
80 }
81
82 /*
83 * Obtain resolution selector for total and phase current report and protection.
84 * 0: original resolution; 1: half resolution (in such case phase current value should
85 * be doubled.
86 */
87 data->total_curr_resolution = (ret & MP2888_TOTAL_CURRENT_RESOLUTION) >> 3;
88 data->phase_curr_resolution = (ret & MP2888_PHASE_CURRENT_RESOLUTION) >> 4;
89
90 return 0;
91}
92
93static int
94mp2888_read_phase(struct i2c_client *client, struct mp2888_data *data, int page, int phase, u8 reg)
95{
96 int ret;
97
98 ret = pmbus_read_word_data(client, page, phase, reg);
99 if (ret < 0)
100 return ret;
101
102 if (!((phase + 1) % 2))
103 ret >>= 8;
104 ret &= 0xff;
105
106 /*
107 * Output value is calculated as: (READ_CSx / 80 – 1.23) / (Kcs * Rcs)
108 * where:
109 * - Kcs is the DrMOS current sense gain of power stage, which is obtained from the
110 * register MP2888_MFR_VR_CONFIG1, bits 13-12 with the following selection of DrMOS
111 * (data->curr_sense_gain):
112 * 00b - 5µA/A, 01b - 8.5µA/A, 10b - 9.7µA/A, 11b - 10µA/A.
113 * - Rcs is the internal phase current sense resistor. This parameter depends on hardware
114 * assembly. By default it is set to 1kΩ. In case of different assembly, user should
115 * scale this parameter by dividing it by Rcs.
116 * If phase current resolution bit is set to 1, READ_CSx value should be doubled.
117 * Note, that current phase sensing, providing by the device is not accurate. This is
118 * because sampling of current occurrence of bit weight has a big deviation, especially for
119 * light load.
120 */
121 ret = DIV_ROUND_CLOSEST(ret * 100 - 9800, data->curr_sense_gain);
122 ret = (data->phase_curr_resolution) ? ret * 2 : ret;
123 /* Scale according to total current resolution. */
124 ret = (data->total_curr_resolution) ? ret * 8 : ret * 4;
125 return ret;
126}
127
128static int
129mp2888_read_phases(struct i2c_client *client, struct mp2888_data *data, int page, int phase)
130{
131 int ret;
132
133 switch (phase) {
134 case 0 ... 1:
135 ret = mp2888_read_phase(client, data, page, phase, MP2888_MFR_READ_CS1_2);
136 break;
137 case 2 ... 3:
138 ret = mp2888_read_phase(client, data, page, phase, MP2888_MFR_READ_CS3_4);
139 break;
140 case 4 ... 5:
141 ret = mp2888_read_phase(client, data, page, phase, MP2888_MFR_READ_CS5_6);
142 break;
143 case 6 ... 7:
144 ret = mp2888_read_phase(client, data, page, phase, MP2888_MFR_READ_CS7_8);
145 break;
146 case 8 ... 9:
147 ret = mp2888_read_phase(client, data, page, phase, MP2888_MFR_READ_CS9_10);
148 break;
149 default:
150 return -ENODATA;
151 }
152 return ret;
153}
154
155static int mp2888_read_word_data(struct i2c_client *client, int page, int phase, int reg)
156{
157 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
158 struct mp2888_data *data = to_mp2888_data(info);
159 int ret;
160
161 switch (reg) {
162 case PMBUS_READ_VIN:
163 ret = pmbus_read_word_data(client, page, phase, reg);
164 if (ret <= 0)
165 return ret;
166
167 /*
168 * READ_VIN requires fixup to scale it to linear11 format. Register data format
169 * provides 10 bits for mantissa and 6 bits for exponent. Bits 15:10 are set with
170 * the fixed value 111011b.
171 */
172 ret = (ret & GENMASK(9, 0)) | ((ret & GENMASK(31, 10)) << 1);
173 break;
174 case PMBUS_OT_WARN_LIMIT:
175 ret = pmbus_read_word_data(client, page, phase, reg);
176 if (ret < 0)
177 return ret;
178 /*
179 * Chip reports limits in degrees C, but the actual temperature in 10th of
180 * degrees C - scaling is needed to match both.
181 */
182 ret *= MP2888_TEMP_UNIT;
183 break;
184 case PMBUS_READ_IOUT:
185 if (phase != 0xff)
186 return mp2888_read_phases(client, data, page, phase);
187
188 ret = pmbus_read_word_data(client, page, phase, reg);
189 if (ret < 0)
190 return ret;
191 /*
192 * READ_IOUT register has unused bits 15:12 with fixed value 1110b. Clear these
193 * bits and scale with total current resolution. Data is provided in direct format.
194 */
195 ret &= GENMASK(11, 0);
196 ret = data->total_curr_resolution ? ret * 2 : ret;
197 break;
198 case PMBUS_IOUT_OC_WARN_LIMIT:
199 ret = pmbus_read_word_data(client, page, phase, reg);
200 if (ret < 0)
201 return ret;
202 ret &= GENMASK(9, 0);
203 /*
204 * Chip reports limits with resolution 1A or 2A, if total current resolution bit is
205 * set 1. Actual current is reported with 0.25A or respectively 0.5A resolution.
206 * Scaling is needed to match both.
207 */
208 ret = data->total_curr_resolution ? ret * 8 : ret * 4;
209 break;
210 case PMBUS_READ_POUT:
211 case PMBUS_READ_PIN:
212 ret = pmbus_read_word_data(client, page, phase, reg);
213 if (ret < 0)
214 return ret;
215 ret = data->total_curr_resolution ? ret * 2 : ret;
216 break;
217 case PMBUS_POUT_OP_WARN_LIMIT:
218 ret = pmbus_read_word_data(client, page, phase, reg);
219 if (ret < 0)
220 return ret;
221 /*
222 * Chip reports limits with resolution 1W or 2W, if total current resolution bit is
223 * set 1. Actual power is reported with 0.5W or 1W respectively resolution. Scaling
224 * is needed to match both.
225 */
226 ret = data->total_curr_resolution ? ret * 4 : ret * 2;
227 break;
228 /*
229 * The below registers are not implemented by device or implemented not according to the
230 * spec. Skip all of them to avoid exposing non-relevant inputs to sysfs.
231 */
232 case PMBUS_OT_FAULT_LIMIT:
233 case PMBUS_UT_WARN_LIMIT:
234 case PMBUS_UT_FAULT_LIMIT:
235 case PMBUS_VIN_UV_FAULT_LIMIT:
236 case PMBUS_VOUT_UV_WARN_LIMIT:
237 case PMBUS_VOUT_OV_WARN_LIMIT:
238 case PMBUS_VOUT_UV_FAULT_LIMIT:
239 case PMBUS_VOUT_OV_FAULT_LIMIT:
240 case PMBUS_VIN_OV_WARN_LIMIT:
241 case PMBUS_IOUT_OC_LV_FAULT_LIMIT:
242 case PMBUS_IOUT_OC_FAULT_LIMIT:
243 case PMBUS_POUT_MAX:
244 case PMBUS_IOUT_UC_FAULT_LIMIT:
245 case PMBUS_POUT_OP_FAULT_LIMIT:
246 case PMBUS_PIN_OP_WARN_LIMIT:
247 case PMBUS_MFR_VIN_MIN:
248 case PMBUS_MFR_VOUT_MIN:
249 case PMBUS_MFR_VIN_MAX:
250 case PMBUS_MFR_VOUT_MAX:
251 case PMBUS_MFR_IIN_MAX:
252 case PMBUS_MFR_IOUT_MAX:
253 case PMBUS_MFR_PIN_MAX:
254 case PMBUS_MFR_POUT_MAX:
255 case PMBUS_MFR_MAX_TEMP_1:
256 return -ENXIO;
257 default:
258 return -ENODATA;
259 }
260
261 return ret;
262}
263
264static int mp2888_write_word_data(struct i2c_client *client, int page, int reg, u16 word)
265{
266 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
267 struct mp2888_data *data = to_mp2888_data(info);
268
269 switch (reg) {
270 case PMBUS_OT_WARN_LIMIT:
271 word = DIV_ROUND_CLOSEST(word, MP2888_TEMP_UNIT);
272 /* Drop unused bits 15:8. */
273 word = clamp_val(word, 0, GENMASK(7, 0));
274 break;
275 case PMBUS_IOUT_OC_WARN_LIMIT:
276 /* Fix limit according to total curent resolution. */
277 word = data->total_curr_resolution ? DIV_ROUND_CLOSEST(word, 8) :
278 DIV_ROUND_CLOSEST(word, 4);
279 /* Drop unused bits 15:10. */
280 word = clamp_val(word, 0, GENMASK(9, 0));
281 break;
282 case PMBUS_POUT_OP_WARN_LIMIT:
283 /* Fix limit according to total curent resolution. */
284 word = data->total_curr_resolution ? DIV_ROUND_CLOSEST(word, 4) :
285 DIV_ROUND_CLOSEST(word, 2);
286 /* Drop unused bits 15:10. */
287 word = clamp_val(word, 0, GENMASK(9, 0));
288 break;
289 default:
290 return -ENODATA;
291 }
292 return pmbus_write_word_data(client, page, reg, word);
293}
294
295static int
296mp2888_identify_multiphase(struct i2c_client *client, struct mp2888_data *data,
297 struct pmbus_driver_info *info)
298{
299 int ret;
300
301 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 0);
302 if (ret < 0)
303 return ret;
304
305 /* Identify multiphase number - could be from 1 to 10. */
306 ret = i2c_smbus_read_word_data(client, MP2888_MFR_VR_CONFIG1);
307 if (ret <= 0)
308 return ret;
309
310 info->phases[0] = ret & GENMASK(3, 0);
311
312 /*
313 * The device provides a total of 10 PWM pins, and can be configured to different phase
314 * count applications for rail.
315 */
316 if (info->phases[0] > MP2888_MAX_PHASE)
317 return -EINVAL;
318
319 return 0;
320}
321
322static struct pmbus_driver_info mp2888_info = {
323 .pages = 1,
324 .format[PSC_VOLTAGE_IN] = linear,
325 .format[PSC_VOLTAGE_OUT] = direct,
326 .format[PSC_TEMPERATURE] = direct,
327 .format[PSC_CURRENT_IN] = linear,
328 .format[PSC_CURRENT_OUT] = direct,
329 .format[PSC_POWER] = direct,
330 .m[PSC_TEMPERATURE] = 1,
331 .R[PSC_TEMPERATURE] = 1,
332 .m[PSC_VOLTAGE_OUT] = 1,
333 .R[PSC_VOLTAGE_OUT] = 3,
334 .m[PSC_CURRENT_OUT] = 4,
335 .m[PSC_POWER] = 1,
336 .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_IOUT |
337 PMBUS_HAVE_STATUS_IOUT | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
338 PMBUS_HAVE_POUT | PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT |
339 PMBUS_PHASE_VIRTUAL,
340 .pfunc[0] = PMBUS_HAVE_IOUT,
341 .pfunc[1] = PMBUS_HAVE_IOUT,
342 .pfunc[2] = PMBUS_HAVE_IOUT,
343 .pfunc[3] = PMBUS_HAVE_IOUT,
344 .pfunc[4] = PMBUS_HAVE_IOUT,
345 .pfunc[5] = PMBUS_HAVE_IOUT,
346 .pfunc[6] = PMBUS_HAVE_IOUT,
347 .pfunc[7] = PMBUS_HAVE_IOUT,
348 .pfunc[8] = PMBUS_HAVE_IOUT,
349 .pfunc[9] = PMBUS_HAVE_IOUT,
350 .read_byte_data = mp2888_read_byte_data,
351 .read_word_data = mp2888_read_word_data,
352 .write_word_data = mp2888_write_word_data,
353};
354
355static int mp2888_probe(struct i2c_client *client)
356{
357 struct pmbus_driver_info *info;
358 struct mp2888_data *data;
359 int ret;
360
361 data = devm_kzalloc(&client->dev, sizeof(struct mp2888_data), GFP_KERNEL);
362 if (!data)
363 return -ENOMEM;
364
365 memcpy(&data->info, &mp2888_info, sizeof(*info));
366 info = &data->info;
367
368 /* Identify multiphase configuration. */
369 ret = mp2888_identify_multiphase(client, data, info);
370 if (ret)
371 return ret;
372
373 /* Obtain current sense gain of power stage and current resolution. */
374 ret = mp2888_current_sense_gain_and_resolution_get(client, data);
375 if (ret)
376 return ret;
377
378 return pmbus_do_probe(client, info);
379}
380
381static const struct i2c_device_id mp2888_id[] = {
382 {"mp2888", 0},
383 {}
384};
385
386MODULE_DEVICE_TABLE(i2c, mp2888_id);
387
388static const struct of_device_id __maybe_unused mp2888_of_match[] = {
389 {.compatible = "mps,mp2888"},
390 {}
391};
392MODULE_DEVICE_TABLE(of, mp2888_of_match);
393
394static struct i2c_driver mp2888_driver = {
395 .driver = {
396 .name = "mp2888",
397 .of_match_table = of_match_ptr(mp2888_of_match),
398 },
399 .probe_new = mp2888_probe,
400 .id_table = mp2888_id,
401};
402
403module_i2c_driver(mp2888_driver);
404
405MODULE_AUTHOR("Vadim Pasternak <vadimp@nvidia.com>");
406MODULE_DESCRIPTION("PMBus driver for MPS MP2888 device");
407MODULE_LICENSE("GPL");
408MODULE_IMPORT_NS(PMBUS);