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 MP2975_MFR_APS_HYS_R2 0x0d
17 #define MP2975_MFR_SLOPE_TRIM3 0x1d
18 #define MP2975_MFR_VR_MULTI_CONFIG_R1 0x0d
19 #define MP2975_MFR_VR_MULTI_CONFIG_R2 0x1d
20 #define MP2975_MFR_APS_DECAY_ADV 0x56
21 #define MP2975_MFR_DC_LOOP_CTRL 0x59
22 #define MP2975_MFR_OCP_UCP_PHASE_SET 0x65
23 #define MP2975_MFR_VR_CONFIG1 0x68
24 #define MP2975_MFR_READ_CS1_2 0x82
25 #define MP2975_MFR_READ_CS3_4 0x83
26 #define MP2975_MFR_READ_CS5_6 0x84
27 #define MP2975_MFR_READ_CS7_8 0x85
28 #define MP2975_MFR_READ_CS9_10 0x86
29 #define MP2975_MFR_READ_CS11_12 0x87
30 #define MP2975_MFR_READ_IOUT_PK 0x90
31 #define MP2975_MFR_READ_POUT_PK 0x91
32 #define MP2975_MFR_READ_VREF_R1 0xa1
33 #define MP2975_MFR_READ_VREF_R2 0xa3
34 #define MP2975_MFR_OVP_TH_SET 0xe5
35 #define MP2975_MFR_UVP_SET 0xe6
36
37 #define MP2975_VOUT_FORMAT BIT(15)
38 #define MP2975_VID_STEP_SEL_R1 BIT(4)
39 #define MP2975_IMVP9_EN_R1 BIT(13)
40 #define MP2975_VID_STEP_SEL_R2 BIT(3)
41 #define MP2975_IMVP9_EN_R2 BIT(12)
42 #define MP2975_PRT_THRES_DIV_OV_EN BIT(14)
43 #define MP2975_DRMOS_KCS GENMASK(13, 12)
44 #define MP2975_PROT_DEV_OV_OFF 10
45 #define MP2975_PROT_DEV_OV_ON 5
46 #define MP2975_SENSE_AMPL BIT(11)
47 #define MP2975_SENSE_AMPL_UNIT 1
48 #define MP2975_SENSE_AMPL_HALF 2
49 #define MP2975_VIN_UV_LIMIT_UNIT 8
50
51 #define MP2975_MAX_PHASE_RAIL1 8
52 #define MP2975_MAX_PHASE_RAIL2 4
53 #define MP2975_PAGE_NUM 2
54
55 #define MP2975_RAIL2_FUNC (PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | \
56 PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT | \
57 PMBUS_HAVE_POUT | PMBUS_PHASE_VIRTUAL)
58
59 struct mp2975_data {
60 struct pmbus_driver_info info;
61 int vout_scale;
62 int vid_step[MP2975_PAGE_NUM];
63 int vref[MP2975_PAGE_NUM];
64 int vref_off[MP2975_PAGE_NUM];
65 int vout_max[MP2975_PAGE_NUM];
66 int vout_ov_fixed[MP2975_PAGE_NUM];
67 int vout_format[MP2975_PAGE_NUM];
68 int curr_sense_gain[MP2975_PAGE_NUM];
69 };
70
71 #define to_mp2975_data(x) container_of(x, struct mp2975_data, info)
72
mp2975_read_byte_data(struct i2c_client * client,int page,int reg)73 static int mp2975_read_byte_data(struct i2c_client *client, int page, int reg)
74 {
75 switch (reg) {
76 case PMBUS_VOUT_MODE:
77 /*
78 * Enforce VOUT direct format, since device allows to set the
79 * different formats for the different rails. Conversion from
80 * VID to direct provided by driver internally, in case it is
81 * necessary.
82 */
83 return PB_VOUT_MODE_DIRECT;
84 default:
85 return -ENODATA;
86 }
87 }
88
89 static int
mp2975_read_word_helper(struct i2c_client * client,int page,int phase,u8 reg,u16 mask)90 mp2975_read_word_helper(struct i2c_client *client, int page, int phase, u8 reg,
91 u16 mask)
92 {
93 int ret = pmbus_read_word_data(client, page, phase, reg);
94
95 return (ret > 0) ? ret & mask : ret;
96 }
97
98 static int
mp2975_vid2direct(int vrf,int val)99 mp2975_vid2direct(int vrf, int val)
100 {
101 switch (vrf) {
102 case vr12:
103 if (val >= 0x01)
104 return 250 + (val - 1) * 5;
105 break;
106 case vr13:
107 if (val >= 0x01)
108 return 500 + (val - 1) * 10;
109 break;
110 case imvp9:
111 if (val >= 0x01)
112 return 200 + (val - 1) * 10;
113 break;
114 default:
115 return -EINVAL;
116 }
117 return 0;
118 }
119
120 static int
mp2975_read_phase(struct i2c_client * client,struct mp2975_data * data,int page,int phase,u8 reg)121 mp2975_read_phase(struct i2c_client *client, struct mp2975_data *data,
122 int page, int phase, u8 reg)
123 {
124 int ph_curr, ret;
125
126 ret = pmbus_read_word_data(client, page, phase, reg);
127 if (ret < 0)
128 return ret;
129
130 if (!((phase + 1) % MP2975_PAGE_NUM))
131 ret >>= 8;
132 ret &= 0xff;
133
134 /*
135 * Output value is calculated as: (READ_CSx / 80 – 1.23) / (Kcs * Rcs)
136 * where:
137 * - Kcs is the DrMOS current sense gain of power stage, which is
138 * obtained from the register MP2975_MFR_VR_CONFIG1, bits 13-12 with
139 * the following selection of DrMOS (data->curr_sense_gain[page]):
140 * 00b - 5µA/A, 01b - 8.5µA/A, 10b - 9.7µA/A, 11b - 10µA/A.
141 * - Rcs is the internal phase current sense resistor which is constant
142 * value 1kΩ.
143 */
144 ph_curr = ret * 100 - 9800;
145
146 /*
147 * Current phase sensing, providing by the device is not accurate
148 * for the light load. This because sampling of current occurrence of
149 * bit weight has a big deviation for light load. For handling such
150 * case phase current is represented as the maximum between the value
151 * calculated above and total rail current divided by number phases.
152 */
153 ret = pmbus_read_word_data(client, page, phase, PMBUS_READ_IOUT);
154 if (ret < 0)
155 return ret;
156
157 return max_t(int, DIV_ROUND_CLOSEST(ret, data->info.phases[page]),
158 DIV_ROUND_CLOSEST(ph_curr, data->curr_sense_gain[page]));
159 }
160
161 static int
mp2975_read_phases(struct i2c_client * client,struct mp2975_data * data,int page,int phase)162 mp2975_read_phases(struct i2c_client *client, struct mp2975_data *data,
163 int page, int phase)
164 {
165 int ret;
166
167 if (page) {
168 switch (phase) {
169 case 0 ... 1:
170 ret = mp2975_read_phase(client, data, page, phase,
171 MP2975_MFR_READ_CS7_8);
172 break;
173 case 2 ... 3:
174 ret = mp2975_read_phase(client, data, page, phase,
175 MP2975_MFR_READ_CS9_10);
176 break;
177 case 4 ... 5:
178 ret = mp2975_read_phase(client, data, page, phase,
179 MP2975_MFR_READ_CS11_12);
180 break;
181 default:
182 return -ENODATA;
183 }
184 } else {
185 switch (phase) {
186 case 0 ... 1:
187 ret = mp2975_read_phase(client, data, page, phase,
188 MP2975_MFR_READ_CS1_2);
189 break;
190 case 2 ... 3:
191 ret = mp2975_read_phase(client, data, page, phase,
192 MP2975_MFR_READ_CS3_4);
193 break;
194 case 4 ... 5:
195 ret = mp2975_read_phase(client, data, page, phase,
196 MP2975_MFR_READ_CS5_6);
197 break;
198 case 6 ... 7:
199 ret = mp2975_read_phase(client, data, page, phase,
200 MP2975_MFR_READ_CS7_8);
201 break;
202 case 8 ... 9:
203 ret = mp2975_read_phase(client, data, page, phase,
204 MP2975_MFR_READ_CS9_10);
205 break;
206 case 10 ... 11:
207 ret = mp2975_read_phase(client, data, page, phase,
208 MP2975_MFR_READ_CS11_12);
209 break;
210 default:
211 return -ENODATA;
212 }
213 }
214 return ret;
215 }
216
mp2975_read_word_data(struct i2c_client * client,int page,int phase,int reg)217 static int mp2975_read_word_data(struct i2c_client *client, int page,
218 int phase, int reg)
219 {
220 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
221 struct mp2975_data *data = to_mp2975_data(info);
222 int ret;
223
224 switch (reg) {
225 case PMBUS_OT_FAULT_LIMIT:
226 ret = mp2975_read_word_helper(client, page, phase, reg,
227 GENMASK(7, 0));
228 break;
229 case PMBUS_VIN_OV_FAULT_LIMIT:
230 ret = mp2975_read_word_helper(client, page, phase, reg,
231 GENMASK(7, 0));
232 if (ret < 0)
233 return ret;
234
235 ret = DIV_ROUND_CLOSEST(ret, MP2975_VIN_UV_LIMIT_UNIT);
236 break;
237 case PMBUS_VOUT_OV_FAULT_LIMIT:
238 /*
239 * Register provides two values for over-voltage protection
240 * threshold for fixed (ovp2) and tracking (ovp1) modes. The
241 * minimum of these two values is provided as over-voltage
242 * fault alarm.
243 */
244 ret = mp2975_read_word_helper(client, page, phase,
245 MP2975_MFR_OVP_TH_SET,
246 GENMASK(2, 0));
247 if (ret < 0)
248 return ret;
249
250 ret = min_t(int, data->vout_max[page] + 50 * (ret + 1),
251 data->vout_ov_fixed[page]);
252 break;
253 case PMBUS_VOUT_UV_FAULT_LIMIT:
254 ret = mp2975_read_word_helper(client, page, phase,
255 MP2975_MFR_UVP_SET,
256 GENMASK(2, 0));
257 if (ret < 0)
258 return ret;
259
260 ret = DIV_ROUND_CLOSEST(data->vref[page] * 10 - 50 *
261 (ret + 1) * data->vout_scale, 10);
262 break;
263 case PMBUS_READ_VOUT:
264 ret = mp2975_read_word_helper(client, page, phase, reg,
265 GENMASK(11, 0));
266 if (ret < 0)
267 return ret;
268
269 /*
270 * READ_VOUT can be provided in VID or direct format. The
271 * format type is specified by bit 15 of the register
272 * MP2975_MFR_DC_LOOP_CTRL. The driver enforces VOUT direct
273 * format, since device allows to set the different formats for
274 * the different rails and also all VOUT limits registers are
275 * provided in a direct format. In case format is VID - convert
276 * to direct.
277 */
278 if (data->vout_format[page] == vid)
279 ret = mp2975_vid2direct(info->vrm_version[page], ret);
280 break;
281 case PMBUS_VIRT_READ_POUT_MAX:
282 ret = mp2975_read_word_helper(client, page, phase,
283 MP2975_MFR_READ_POUT_PK,
284 GENMASK(12, 0));
285 if (ret < 0)
286 return ret;
287
288 ret = DIV_ROUND_CLOSEST(ret, 4);
289 break;
290 case PMBUS_VIRT_READ_IOUT_MAX:
291 ret = mp2975_read_word_helper(client, page, phase,
292 MP2975_MFR_READ_IOUT_PK,
293 GENMASK(12, 0));
294 if (ret < 0)
295 return ret;
296
297 ret = DIV_ROUND_CLOSEST(ret, 4);
298 break;
299 case PMBUS_READ_IOUT:
300 ret = mp2975_read_phases(client, data, page, phase);
301 if (ret < 0)
302 return ret;
303
304 break;
305 case PMBUS_UT_WARN_LIMIT:
306 case PMBUS_UT_FAULT_LIMIT:
307 case PMBUS_VIN_UV_WARN_LIMIT:
308 case PMBUS_VIN_UV_FAULT_LIMIT:
309 case PMBUS_VOUT_UV_WARN_LIMIT:
310 case PMBUS_VOUT_OV_WARN_LIMIT:
311 case PMBUS_VIN_OV_WARN_LIMIT:
312 case PMBUS_IIN_OC_FAULT_LIMIT:
313 case PMBUS_IOUT_OC_LV_FAULT_LIMIT:
314 case PMBUS_IIN_OC_WARN_LIMIT:
315 case PMBUS_IOUT_OC_WARN_LIMIT:
316 case PMBUS_IOUT_OC_FAULT_LIMIT:
317 case PMBUS_IOUT_UC_FAULT_LIMIT:
318 case PMBUS_POUT_OP_FAULT_LIMIT:
319 case PMBUS_POUT_OP_WARN_LIMIT:
320 case PMBUS_PIN_OP_WARN_LIMIT:
321 return -ENXIO;
322 default:
323 return -ENODATA;
324 }
325
326 return ret;
327 }
328
mp2975_identify_multiphase_rail2(struct i2c_client * client)329 static int mp2975_identify_multiphase_rail2(struct i2c_client *client)
330 {
331 int ret;
332
333 /*
334 * Identify multiphase for rail 2 - could be from 0 to 4.
335 * In case phase number is zero – only page zero is supported
336 */
337 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2);
338 if (ret < 0)
339 return ret;
340
341 /* Identify multiphase for rail 2 - could be from 0 to 4. */
342 ret = i2c_smbus_read_word_data(client, MP2975_MFR_VR_MULTI_CONFIG_R2);
343 if (ret < 0)
344 return ret;
345
346 ret &= GENMASK(2, 0);
347 return (ret >= 4) ? 4 : ret;
348 }
349
mp2975_set_phase_rail1(struct pmbus_driver_info * info)350 static void mp2975_set_phase_rail1(struct pmbus_driver_info *info)
351 {
352 int i;
353
354 for (i = 0 ; i < info->phases[0]; i++)
355 info->pfunc[i] = PMBUS_HAVE_IOUT;
356 }
357
358 static void
mp2975_set_phase_rail2(struct pmbus_driver_info * info,int num_phases)359 mp2975_set_phase_rail2(struct pmbus_driver_info *info, int num_phases)
360 {
361 int i;
362
363 /* Set phases for rail 2 from upper to lower. */
364 for (i = 1; i <= num_phases; i++)
365 info->pfunc[MP2975_MAX_PHASE_RAIL1 - i] = PMBUS_HAVE_IOUT;
366 }
367
368 static int
mp2975_identify_multiphase(struct i2c_client * client,struct mp2975_data * data,struct pmbus_driver_info * info)369 mp2975_identify_multiphase(struct i2c_client *client, struct mp2975_data *data,
370 struct pmbus_driver_info *info)
371 {
372 int num_phases2, ret;
373
374 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2);
375 if (ret < 0)
376 return ret;
377
378 /* Identify multiphase for rail 1 - could be from 1 to 8. */
379 ret = i2c_smbus_read_word_data(client, MP2975_MFR_VR_MULTI_CONFIG_R1);
380 if (ret <= 0)
381 return ret;
382
383 info->phases[0] = ret & GENMASK(3, 0);
384
385 /*
386 * The device provides a total of 8 PWM pins, and can be configured
387 * to different phase count applications for rail 1 and rail 2.
388 * Rail 1 can be set to 8 phases, while rail 2 can only be set to 4
389 * phases at most. When rail 1’s phase count is configured as 0, rail
390 * 1 operates with 1-phase DCM. When rail 2 phase count is configured
391 * as 0, rail 2 is disabled.
392 */
393 if (info->phases[0] > MP2975_MAX_PHASE_RAIL1)
394 return -EINVAL;
395
396 mp2975_set_phase_rail1(info);
397 num_phases2 = min(MP2975_MAX_PHASE_RAIL1 - info->phases[0],
398 MP2975_MAX_PHASE_RAIL2);
399 if (info->phases[1] && info->phases[1] <= num_phases2)
400 mp2975_set_phase_rail2(info, num_phases2);
401
402 return 0;
403 }
404
405 static int
mp2975_identify_vid(struct i2c_client * client,struct mp2975_data * data,struct pmbus_driver_info * info,u32 reg,int page,u32 imvp_bit,u32 vr_bit)406 mp2975_identify_vid(struct i2c_client *client, struct mp2975_data *data,
407 struct pmbus_driver_info *info, u32 reg, int page,
408 u32 imvp_bit, u32 vr_bit)
409 {
410 int ret;
411
412 /* Identify VID mode and step selection. */
413 ret = i2c_smbus_read_word_data(client, reg);
414 if (ret < 0)
415 return ret;
416
417 if (ret & imvp_bit) {
418 info->vrm_version[page] = imvp9;
419 data->vid_step[page] = MP2975_PROT_DEV_OV_OFF;
420 } else if (ret & vr_bit) {
421 info->vrm_version[page] = vr12;
422 data->vid_step[page] = MP2975_PROT_DEV_OV_ON;
423 } else {
424 info->vrm_version[page] = vr13;
425 data->vid_step[page] = MP2975_PROT_DEV_OV_OFF;
426 }
427
428 return 0;
429 }
430
431 static int
mp2975_identify_rails_vid(struct i2c_client * client,struct mp2975_data * data,struct pmbus_driver_info * info)432 mp2975_identify_rails_vid(struct i2c_client *client, struct mp2975_data *data,
433 struct pmbus_driver_info *info)
434 {
435 int ret;
436
437 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2);
438 if (ret < 0)
439 return ret;
440
441 /* Identify VID mode for rail 1. */
442 ret = mp2975_identify_vid(client, data, info,
443 MP2975_MFR_VR_MULTI_CONFIG_R1, 0,
444 MP2975_IMVP9_EN_R1, MP2975_VID_STEP_SEL_R1);
445 if (ret < 0)
446 return ret;
447
448 /* Identify VID mode for rail 2, if connected. */
449 if (info->phases[1])
450 ret = mp2975_identify_vid(client, data, info,
451 MP2975_MFR_VR_MULTI_CONFIG_R2, 1,
452 MP2975_IMVP9_EN_R2,
453 MP2975_VID_STEP_SEL_R2);
454 return ret;
455 }
456
457 static int
mp2975_current_sense_gain_get(struct i2c_client * client,struct mp2975_data * data)458 mp2975_current_sense_gain_get(struct i2c_client *client,
459 struct mp2975_data *data)
460 {
461 int i, ret;
462
463 /*
464 * Obtain DrMOS current sense gain of power stage from the register
465 * MP2975_MFR_VR_CONFIG1, bits 13-12. The value is selected as below:
466 * 00b - 5µA/A, 01b - 8.5µA/A, 10b - 9.7µA/A, 11b - 10µA/A. Other
467 * values are invalid.
468 */
469 for (i = 0 ; i < data->info.pages; i++) {
470 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, i);
471 if (ret < 0)
472 return ret;
473 ret = i2c_smbus_read_word_data(client,
474 MP2975_MFR_VR_CONFIG1);
475 if (ret < 0)
476 return ret;
477
478 switch ((ret & MP2975_DRMOS_KCS) >> 12) {
479 case 0:
480 data->curr_sense_gain[i] = 50;
481 break;
482 case 1:
483 data->curr_sense_gain[i] = 85;
484 break;
485 case 2:
486 data->curr_sense_gain[i] = 97;
487 break;
488 default:
489 data->curr_sense_gain[i] = 100;
490 break;
491 }
492 }
493
494 return 0;
495 }
496
497 static int
mp2975_vref_get(struct i2c_client * client,struct mp2975_data * data,struct pmbus_driver_info * info)498 mp2975_vref_get(struct i2c_client *client, struct mp2975_data *data,
499 struct pmbus_driver_info *info)
500 {
501 int ret;
502
503 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 3);
504 if (ret < 0)
505 return ret;
506
507 /* Get voltage reference value for rail 1. */
508 ret = i2c_smbus_read_word_data(client, MP2975_MFR_READ_VREF_R1);
509 if (ret < 0)
510 return ret;
511
512 data->vref[0] = ret * data->vid_step[0];
513
514 /* Get voltage reference value for rail 2, if connected. */
515 if (data->info.pages == MP2975_PAGE_NUM) {
516 ret = i2c_smbus_read_word_data(client, MP2975_MFR_READ_VREF_R2);
517 if (ret < 0)
518 return ret;
519
520 data->vref[1] = ret * data->vid_step[1];
521 }
522 return 0;
523 }
524
525 static int
mp2975_vref_offset_get(struct i2c_client * client,struct mp2975_data * data,int page)526 mp2975_vref_offset_get(struct i2c_client *client, struct mp2975_data *data,
527 int page)
528 {
529 int ret;
530
531 ret = i2c_smbus_read_word_data(client, MP2975_MFR_OVP_TH_SET);
532 if (ret < 0)
533 return ret;
534
535 switch ((ret & GENMASK(5, 3)) >> 3) {
536 case 1:
537 data->vref_off[page] = 140;
538 break;
539 case 2:
540 data->vref_off[page] = 220;
541 break;
542 case 4:
543 data->vref_off[page] = 400;
544 break;
545 default:
546 return -EINVAL;
547 }
548 return 0;
549 }
550
551 static int
mp2975_vout_max_get(struct i2c_client * client,struct mp2975_data * data,struct pmbus_driver_info * info,int page)552 mp2975_vout_max_get(struct i2c_client *client, struct mp2975_data *data,
553 struct pmbus_driver_info *info, int page)
554 {
555 int ret;
556
557 /* Get maximum reference voltage of VID-DAC in VID format. */
558 ret = i2c_smbus_read_word_data(client, PMBUS_VOUT_MAX);
559 if (ret < 0)
560 return ret;
561
562 data->vout_max[page] = mp2975_vid2direct(info->vrm_version[page], ret &
563 GENMASK(8, 0));
564 return 0;
565 }
566
567 static int
mp2975_identify_vout_format(struct i2c_client * client,struct mp2975_data * data,int page)568 mp2975_identify_vout_format(struct i2c_client *client,
569 struct mp2975_data *data, int page)
570 {
571 int ret;
572
573 ret = i2c_smbus_read_word_data(client, MP2975_MFR_DC_LOOP_CTRL);
574 if (ret < 0)
575 return ret;
576
577 if (ret & MP2975_VOUT_FORMAT)
578 data->vout_format[page] = vid;
579 else
580 data->vout_format[page] = direct;
581 return 0;
582 }
583
584 static int
mp2975_vout_ov_scale_get(struct i2c_client * client,struct mp2975_data * data,struct pmbus_driver_info * info)585 mp2975_vout_ov_scale_get(struct i2c_client *client, struct mp2975_data *data,
586 struct pmbus_driver_info *info)
587 {
588 int thres_dev, sense_ampl, ret;
589
590 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 0);
591 if (ret < 0)
592 return ret;
593
594 /*
595 * Get divider for over- and under-voltage protection thresholds
596 * configuration from the Advanced Options of Auto Phase Shedding and
597 * decay register.
598 */
599 ret = i2c_smbus_read_word_data(client, MP2975_MFR_APS_DECAY_ADV);
600 if (ret < 0)
601 return ret;
602 thres_dev = ret & MP2975_PRT_THRES_DIV_OV_EN ? MP2975_PROT_DEV_OV_ON :
603 MP2975_PROT_DEV_OV_OFF;
604
605 /* Select the gain of remote sense amplifier. */
606 ret = i2c_smbus_read_word_data(client, PMBUS_VOUT_SCALE_LOOP);
607 if (ret < 0)
608 return ret;
609 sense_ampl = ret & MP2975_SENSE_AMPL ? MP2975_SENSE_AMPL_HALF :
610 MP2975_SENSE_AMPL_UNIT;
611
612 data->vout_scale = sense_ampl * thres_dev;
613
614 return 0;
615 }
616
617 static int
mp2975_vout_per_rail_config_get(struct i2c_client * client,struct mp2975_data * data,struct pmbus_driver_info * info)618 mp2975_vout_per_rail_config_get(struct i2c_client *client,
619 struct mp2975_data *data,
620 struct pmbus_driver_info *info)
621 {
622 int i, ret;
623
624 for (i = 0; i < data->info.pages; i++) {
625 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, i);
626 if (ret < 0)
627 return ret;
628
629 /* Obtain voltage reference offsets. */
630 ret = mp2975_vref_offset_get(client, data, i);
631 if (ret < 0)
632 return ret;
633
634 /* Obtain maximum voltage values. */
635 ret = mp2975_vout_max_get(client, data, info, i);
636 if (ret < 0)
637 return ret;
638
639 /*
640 * Get VOUT format for READ_VOUT command : VID or direct.
641 * Pages on same device can be configured with different
642 * formats.
643 */
644 ret = mp2975_identify_vout_format(client, data, i);
645 if (ret < 0)
646 return ret;
647
648 /*
649 * Set over-voltage fixed value. Thresholds are provided as
650 * fixed value, and tracking value. The minimum of them are
651 * exposed as over-voltage critical threshold.
652 */
653 data->vout_ov_fixed[i] = data->vref[i] +
654 DIV_ROUND_CLOSEST(data->vref_off[i] *
655 data->vout_scale,
656 10);
657 }
658
659 return 0;
660 }
661
662 static struct pmbus_driver_info mp2975_info = {
663 .pages = 1,
664 .format[PSC_VOLTAGE_IN] = linear,
665 .format[PSC_VOLTAGE_OUT] = direct,
666 .format[PSC_TEMPERATURE] = direct,
667 .format[PSC_CURRENT_IN] = linear,
668 .format[PSC_CURRENT_OUT] = direct,
669 .format[PSC_POWER] = direct,
670 .m[PSC_TEMPERATURE] = 1,
671 .m[PSC_VOLTAGE_OUT] = 1,
672 .R[PSC_VOLTAGE_OUT] = 3,
673 .m[PSC_CURRENT_OUT] = 1,
674 .m[PSC_POWER] = 1,
675 .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
676 PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
677 PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP | PMBUS_HAVE_POUT |
678 PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT | PMBUS_PHASE_VIRTUAL,
679 .read_byte_data = mp2975_read_byte_data,
680 .read_word_data = mp2975_read_word_data,
681 };
682
mp2975_probe(struct i2c_client * client)683 static int mp2975_probe(struct i2c_client *client)
684 {
685 struct pmbus_driver_info *info;
686 struct mp2975_data *data;
687 int ret;
688
689 data = devm_kzalloc(&client->dev, sizeof(struct mp2975_data),
690 GFP_KERNEL);
691 if (!data)
692 return -ENOMEM;
693
694 memcpy(&data->info, &mp2975_info, sizeof(*info));
695 info = &data->info;
696
697 /* Identify multiphase configuration for rail 2. */
698 ret = mp2975_identify_multiphase_rail2(client);
699 if (ret < 0)
700 return ret;
701
702 if (ret) {
703 /* Two rails are connected. */
704 data->info.pages = MP2975_PAGE_NUM;
705 data->info.phases[1] = ret;
706 data->info.func[1] = MP2975_RAIL2_FUNC;
707 }
708
709 /* Identify multiphase configuration. */
710 ret = mp2975_identify_multiphase(client, data, info);
711 if (ret)
712 return ret;
713
714 /* Identify VID setting per rail. */
715 ret = mp2975_identify_rails_vid(client, data, info);
716 if (ret < 0)
717 return ret;
718
719 /* Obtain current sense gain of power stage. */
720 ret = mp2975_current_sense_gain_get(client, data);
721 if (ret)
722 return ret;
723
724 /* Obtain voltage reference values. */
725 ret = mp2975_vref_get(client, data, info);
726 if (ret)
727 return ret;
728
729 /* Obtain vout over-voltage scales. */
730 ret = mp2975_vout_ov_scale_get(client, data, info);
731 if (ret < 0)
732 return ret;
733
734 /* Obtain offsets, maximum and format for vout. */
735 ret = mp2975_vout_per_rail_config_get(client, data, info);
736 if (ret)
737 return ret;
738
739 return pmbus_do_probe(client, info);
740 }
741
742 static const struct i2c_device_id mp2975_id[] = {
743 {"mp2975", 0},
744 {}
745 };
746
747 MODULE_DEVICE_TABLE(i2c, mp2975_id);
748
749 static const struct of_device_id __maybe_unused mp2975_of_match[] = {
750 {.compatible = "mps,mp2975"},
751 {}
752 };
753 MODULE_DEVICE_TABLE(of, mp2975_of_match);
754
755 static struct i2c_driver mp2975_driver = {
756 .driver = {
757 .name = "mp2975",
758 .of_match_table = of_match_ptr(mp2975_of_match),
759 },
760 .probe_new = mp2975_probe,
761 .remove = pmbus_do_remove,
762 .id_table = mp2975_id,
763 };
764
765 module_i2c_driver(mp2975_driver);
766
767 MODULE_AUTHOR("Vadim Pasternak <vadimp@nvidia.com>");
768 MODULE_DESCRIPTION("PMBus driver for MPS MP2975 device");
769 MODULE_LICENSE("GPL");
770