12c6fcbb2SVadim Pasternak // SPDX-License-Identifier: GPL-2.0-or-later 22c6fcbb2SVadim Pasternak /* 32c6fcbb2SVadim Pasternak * Hardware monitoring driver for MPS Multi-phase Digital VR Controllers 42c6fcbb2SVadim Pasternak * 52c6fcbb2SVadim Pasternak * Copyright (C) 2020 Nvidia Technologies Ltd. 62c6fcbb2SVadim Pasternak */ 72c6fcbb2SVadim Pasternak 82c6fcbb2SVadim Pasternak #include <linux/err.h> 92c6fcbb2SVadim Pasternak #include <linux/i2c.h> 102c6fcbb2SVadim Pasternak #include <linux/init.h> 112c6fcbb2SVadim Pasternak #include <linux/kernel.h> 122c6fcbb2SVadim Pasternak #include <linux/module.h> 131f6f34d0SPatrick Rudolph #include <linux/of_device.h> 142c6fcbb2SVadim Pasternak #include "pmbus.h" 152c6fcbb2SVadim Pasternak 162c6fcbb2SVadim Pasternak /* Vendor specific registers. */ 172c6fcbb2SVadim Pasternak #define MP2975_MFR_APS_HYS_R2 0x0d 182c6fcbb2SVadim Pasternak #define MP2975_MFR_SLOPE_TRIM3 0x1d 192c6fcbb2SVadim Pasternak #define MP2975_MFR_VR_MULTI_CONFIG_R1 0x0d 202c6fcbb2SVadim Pasternak #define MP2975_MFR_VR_MULTI_CONFIG_R2 0x1d 212c6fcbb2SVadim Pasternak #define MP2975_MFR_APS_DECAY_ADV 0x56 222c6fcbb2SVadim Pasternak #define MP2975_MFR_DC_LOOP_CTRL 0x59 232c6fcbb2SVadim Pasternak #define MP2975_MFR_OCP_UCP_PHASE_SET 0x65 242c6fcbb2SVadim Pasternak #define MP2975_MFR_VR_CONFIG1 0x68 252c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_CS1_2 0x82 262c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_CS3_4 0x83 272c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_CS5_6 0x84 282c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_CS7_8 0x85 292c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_CS9_10 0x86 302c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_CS11_12 0x87 312c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_IOUT_PK 0x90 322c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_POUT_PK 0x91 332c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_VREF_R1 0xa1 342c6fcbb2SVadim Pasternak #define MP2975_MFR_READ_VREF_R2 0xa3 352c6fcbb2SVadim Pasternak #define MP2975_MFR_OVP_TH_SET 0xe5 362c6fcbb2SVadim Pasternak #define MP2975_MFR_UVP_SET 0xe6 372c6fcbb2SVadim Pasternak 38*5239277eSPatrick Rudolph #define MP2973_MFR_RESO_SET 0xc7 39*5239277eSPatrick Rudolph 402c6fcbb2SVadim Pasternak #define MP2975_VOUT_FORMAT BIT(15) 412c6fcbb2SVadim Pasternak #define MP2975_VID_STEP_SEL_R1 BIT(4) 422c6fcbb2SVadim Pasternak #define MP2975_IMVP9_EN_R1 BIT(13) 432c6fcbb2SVadim Pasternak #define MP2975_VID_STEP_SEL_R2 BIT(3) 442c6fcbb2SVadim Pasternak #define MP2975_IMVP9_EN_R2 BIT(12) 452c6fcbb2SVadim Pasternak #define MP2975_PRT_THRES_DIV_OV_EN BIT(14) 462c6fcbb2SVadim Pasternak #define MP2975_DRMOS_KCS GENMASK(13, 12) 472c6fcbb2SVadim Pasternak #define MP2975_PROT_DEV_OV_OFF 10 482c6fcbb2SVadim Pasternak #define MP2975_PROT_DEV_OV_ON 5 492c6fcbb2SVadim Pasternak #define MP2975_SENSE_AMPL BIT(11) 502c6fcbb2SVadim Pasternak #define MP2975_SENSE_AMPL_UNIT 1 512c6fcbb2SVadim Pasternak #define MP2975_SENSE_AMPL_HALF 2 522c6fcbb2SVadim Pasternak #define MP2975_VIN_UV_LIMIT_UNIT 8 532c6fcbb2SVadim Pasternak 54*5239277eSPatrick Rudolph #define MP2973_VOUT_FORMAT_R1 GENMASK(7, 6) 55*5239277eSPatrick Rudolph #define MP2973_VOUT_FORMAT_R2 GENMASK(4, 3) 56*5239277eSPatrick Rudolph #define MP2973_VOUT_FORMAT_DIRECT_R1 BIT(7) 57*5239277eSPatrick Rudolph #define MP2973_VOUT_FORMAT_LINEAR_R1 BIT(6) 58*5239277eSPatrick Rudolph #define MP2973_VOUT_FORMAT_DIRECT_R2 BIT(4) 59*5239277eSPatrick Rudolph #define MP2973_VOUT_FORMAT_LINEAR_R2 BIT(3) 60*5239277eSPatrick Rudolph 61*5239277eSPatrick Rudolph #define MP2973_MFR_VR_MULTI_CONFIG_R1 0x0d 62*5239277eSPatrick Rudolph #define MP2973_MFR_VR_MULTI_CONFIG_R2 0x1d 63*5239277eSPatrick Rudolph #define MP2973_VID_STEP_SEL_R1 BIT(4) 64*5239277eSPatrick Rudolph #define MP2973_IMVP9_EN_R1 BIT(14) 65*5239277eSPatrick Rudolph #define MP2973_VID_STEP_SEL_R2 BIT(3) 66*5239277eSPatrick Rudolph #define MP2973_IMVP9_EN_R2 BIT(13) 67*5239277eSPatrick Rudolph 68*5239277eSPatrick Rudolph #define MP2973_MFR_READ_IOUT_PK 0x90 69*5239277eSPatrick Rudolph #define MP2973_MFR_READ_POUT_PK 0x91 70*5239277eSPatrick Rudolph 712c6fcbb2SVadim Pasternak #define MP2975_MAX_PHASE_RAIL1 8 722c6fcbb2SVadim Pasternak #define MP2975_MAX_PHASE_RAIL2 4 73*5239277eSPatrick Rudolph 74*5239277eSPatrick Rudolph #define MP2973_MAX_PHASE_RAIL1 14 75*5239277eSPatrick Rudolph #define MP2973_MAX_PHASE_RAIL2 6 76*5239277eSPatrick Rudolph 77*5239277eSPatrick Rudolph #define MP2971_MAX_PHASE_RAIL1 8 78*5239277eSPatrick Rudolph #define MP2971_MAX_PHASE_RAIL2 3 79*5239277eSPatrick Rudolph 802c6fcbb2SVadim Pasternak #define MP2975_PAGE_NUM 2 812c6fcbb2SVadim Pasternak 822c6fcbb2SVadim Pasternak #define MP2975_RAIL2_FUNC (PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | \ 832c6fcbb2SVadim Pasternak PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT | \ 842292e2f6SVadim Pasternak PMBUS_HAVE_POUT | PMBUS_PHASE_VIRTUAL) 852c6fcbb2SVadim Pasternak 861f6f34d0SPatrick Rudolph enum chips { 87*5239277eSPatrick Rudolph mp2971, mp2973, mp2975 881f6f34d0SPatrick Rudolph }; 891f6f34d0SPatrick Rudolph 90e2c90b48SPatrick Rudolph static const int mp2975_max_phases[][MP2975_PAGE_NUM] = { 91e2c90b48SPatrick Rudolph [mp2975] = { MP2975_MAX_PHASE_RAIL1, MP2975_MAX_PHASE_RAIL2 }, 92*5239277eSPatrick Rudolph [mp2973] = { MP2973_MAX_PHASE_RAIL1, MP2973_MAX_PHASE_RAIL2 }, 93*5239277eSPatrick Rudolph [mp2971] = { MP2971_MAX_PHASE_RAIL1, MP2971_MAX_PHASE_RAIL2 }, 94e2c90b48SPatrick Rudolph }; 95e2c90b48SPatrick Rudolph 962c6fcbb2SVadim Pasternak struct mp2975_data { 972c6fcbb2SVadim Pasternak struct pmbus_driver_info info; 981f6f34d0SPatrick Rudolph enum chips chip_id; 992c6fcbb2SVadim Pasternak int vout_scale; 100e2c90b48SPatrick Rudolph int max_phases[MP2975_PAGE_NUM]; 1012c6fcbb2SVadim Pasternak int vid_step[MP2975_PAGE_NUM]; 1022c6fcbb2SVadim Pasternak int vref[MP2975_PAGE_NUM]; 1032c6fcbb2SVadim Pasternak int vref_off[MP2975_PAGE_NUM]; 1042c6fcbb2SVadim Pasternak int vout_max[MP2975_PAGE_NUM]; 1052c6fcbb2SVadim Pasternak int vout_ov_fixed[MP2975_PAGE_NUM]; 1062c6fcbb2SVadim Pasternak int curr_sense_gain[MP2975_PAGE_NUM]; 1072c6fcbb2SVadim Pasternak }; 1082c6fcbb2SVadim Pasternak 1091f6f34d0SPatrick Rudolph static const struct i2c_device_id mp2975_id[] = { 110*5239277eSPatrick Rudolph {"mp2971", mp2971}, 111*5239277eSPatrick Rudolph {"mp2973", mp2973}, 1121f6f34d0SPatrick Rudolph {"mp2975", mp2975}, 1131f6f34d0SPatrick Rudolph {} 1141f6f34d0SPatrick Rudolph }; 1151f6f34d0SPatrick Rudolph 1161f6f34d0SPatrick Rudolph MODULE_DEVICE_TABLE(i2c, mp2975_id); 1171f6f34d0SPatrick Rudolph 1182c6fcbb2SVadim Pasternak #define to_mp2975_data(x) container_of(x, struct mp2975_data, info) 1192c6fcbb2SVadim Pasternak 1202c6fcbb2SVadim Pasternak static int 1212c6fcbb2SVadim Pasternak mp2975_read_word_helper(struct i2c_client *client, int page, int phase, u8 reg, 1222c6fcbb2SVadim Pasternak u16 mask) 1232c6fcbb2SVadim Pasternak { 1242c6fcbb2SVadim Pasternak int ret = pmbus_read_word_data(client, page, phase, reg); 1252c6fcbb2SVadim Pasternak 1262c6fcbb2SVadim Pasternak return (ret > 0) ? ret & mask : ret; 1272c6fcbb2SVadim Pasternak } 1282c6fcbb2SVadim Pasternak 1292c6fcbb2SVadim Pasternak static int 1302c6fcbb2SVadim Pasternak mp2975_vid2direct(int vrf, int val) 1312c6fcbb2SVadim Pasternak { 1322c6fcbb2SVadim Pasternak switch (vrf) { 1332c6fcbb2SVadim Pasternak case vr12: 1342c6fcbb2SVadim Pasternak if (val >= 0x01) 1352c6fcbb2SVadim Pasternak return 250 + (val - 1) * 5; 1362c6fcbb2SVadim Pasternak break; 1372c6fcbb2SVadim Pasternak case vr13: 1382c6fcbb2SVadim Pasternak if (val >= 0x01) 1392c6fcbb2SVadim Pasternak return 500 + (val - 1) * 10; 1402c6fcbb2SVadim Pasternak break; 1412c6fcbb2SVadim Pasternak case imvp9: 1422c6fcbb2SVadim Pasternak if (val >= 0x01) 1432c6fcbb2SVadim Pasternak return 200 + (val - 1) * 10; 1442c6fcbb2SVadim Pasternak break; 1452c6fcbb2SVadim Pasternak default: 1462c6fcbb2SVadim Pasternak return -EINVAL; 1472c6fcbb2SVadim Pasternak } 1482c6fcbb2SVadim Pasternak return 0; 1492c6fcbb2SVadim Pasternak } 1502c6fcbb2SVadim Pasternak 1512c6fcbb2SVadim Pasternak static int 1522c6fcbb2SVadim Pasternak mp2975_read_phase(struct i2c_client *client, struct mp2975_data *data, 1532c6fcbb2SVadim Pasternak int page, int phase, u8 reg) 1542c6fcbb2SVadim Pasternak { 1552c6fcbb2SVadim Pasternak int ph_curr, ret; 1562c6fcbb2SVadim Pasternak 1572c6fcbb2SVadim Pasternak ret = pmbus_read_word_data(client, page, phase, reg); 1582c6fcbb2SVadim Pasternak if (ret < 0) 1592c6fcbb2SVadim Pasternak return ret; 1602c6fcbb2SVadim Pasternak 1612c6fcbb2SVadim Pasternak if (!((phase + 1) % MP2975_PAGE_NUM)) 1622c6fcbb2SVadim Pasternak ret >>= 8; 1632c6fcbb2SVadim Pasternak ret &= 0xff; 1642c6fcbb2SVadim Pasternak 1652c6fcbb2SVadim Pasternak /* 1662c6fcbb2SVadim Pasternak * Output value is calculated as: (READ_CSx / 80 – 1.23) / (Kcs * Rcs) 1672c6fcbb2SVadim Pasternak * where: 1682c6fcbb2SVadim Pasternak * - Kcs is the DrMOS current sense gain of power stage, which is 1692c6fcbb2SVadim Pasternak * obtained from the register MP2975_MFR_VR_CONFIG1, bits 13-12 with 1702c6fcbb2SVadim Pasternak * the following selection of DrMOS (data->curr_sense_gain[page]): 1712c6fcbb2SVadim Pasternak * 00b - 5µA/A, 01b - 8.5µA/A, 10b - 9.7µA/A, 11b - 10µA/A. 1722c6fcbb2SVadim Pasternak * - Rcs is the internal phase current sense resistor which is constant 1732c6fcbb2SVadim Pasternak * value 1kΩ. 1742c6fcbb2SVadim Pasternak */ 1752c6fcbb2SVadim Pasternak ph_curr = ret * 100 - 9800; 1762c6fcbb2SVadim Pasternak 1772c6fcbb2SVadim Pasternak /* 1782c6fcbb2SVadim Pasternak * Current phase sensing, providing by the device is not accurate 1792c6fcbb2SVadim Pasternak * for the light load. This because sampling of current occurrence of 1802c6fcbb2SVadim Pasternak * bit weight has a big deviation for light load. For handling such 1812c6fcbb2SVadim Pasternak * case phase current is represented as the maximum between the value 1822c6fcbb2SVadim Pasternak * calculated above and total rail current divided by number phases. 1832c6fcbb2SVadim Pasternak */ 1842c6fcbb2SVadim Pasternak ret = pmbus_read_word_data(client, page, phase, PMBUS_READ_IOUT); 1852c6fcbb2SVadim Pasternak if (ret < 0) 1862c6fcbb2SVadim Pasternak return ret; 1872c6fcbb2SVadim Pasternak 1882c6fcbb2SVadim Pasternak return max_t(int, DIV_ROUND_CLOSEST(ret, data->info.phases[page]), 1892c6fcbb2SVadim Pasternak DIV_ROUND_CLOSEST(ph_curr, data->curr_sense_gain[page])); 1902c6fcbb2SVadim Pasternak } 1912c6fcbb2SVadim Pasternak 1922c6fcbb2SVadim Pasternak static int 1932c6fcbb2SVadim Pasternak mp2975_read_phases(struct i2c_client *client, struct mp2975_data *data, 1942c6fcbb2SVadim Pasternak int page, int phase) 1952c6fcbb2SVadim Pasternak { 1962c6fcbb2SVadim Pasternak int ret; 1972c6fcbb2SVadim Pasternak 1982c6fcbb2SVadim Pasternak if (page) { 1992c6fcbb2SVadim Pasternak switch (phase) { 2002c6fcbb2SVadim Pasternak case 0 ... 1: 2012c6fcbb2SVadim Pasternak ret = mp2975_read_phase(client, data, page, phase, 2022c6fcbb2SVadim Pasternak MP2975_MFR_READ_CS7_8); 2032c6fcbb2SVadim Pasternak break; 2042c6fcbb2SVadim Pasternak case 2 ... 3: 2052c6fcbb2SVadim Pasternak ret = mp2975_read_phase(client, data, page, phase, 2062c6fcbb2SVadim Pasternak MP2975_MFR_READ_CS9_10); 2072c6fcbb2SVadim Pasternak break; 2082c6fcbb2SVadim Pasternak case 4 ... 5: 2092c6fcbb2SVadim Pasternak ret = mp2975_read_phase(client, data, page, phase, 2102c6fcbb2SVadim Pasternak MP2975_MFR_READ_CS11_12); 2112c6fcbb2SVadim Pasternak break; 2122c6fcbb2SVadim Pasternak default: 2132c6fcbb2SVadim Pasternak return -ENODATA; 2142c6fcbb2SVadim Pasternak } 2152c6fcbb2SVadim Pasternak } else { 2162c6fcbb2SVadim Pasternak switch (phase) { 2172c6fcbb2SVadim Pasternak case 0 ... 1: 2182c6fcbb2SVadim Pasternak ret = mp2975_read_phase(client, data, page, phase, 2192c6fcbb2SVadim Pasternak MP2975_MFR_READ_CS1_2); 2202c6fcbb2SVadim Pasternak break; 2212c6fcbb2SVadim Pasternak case 2 ... 3: 2222c6fcbb2SVadim Pasternak ret = mp2975_read_phase(client, data, page, phase, 2232c6fcbb2SVadim Pasternak MP2975_MFR_READ_CS3_4); 2242c6fcbb2SVadim Pasternak break; 2252c6fcbb2SVadim Pasternak case 4 ... 5: 2262c6fcbb2SVadim Pasternak ret = mp2975_read_phase(client, data, page, phase, 2272c6fcbb2SVadim Pasternak MP2975_MFR_READ_CS5_6); 2282c6fcbb2SVadim Pasternak break; 2292c6fcbb2SVadim Pasternak case 6 ... 7: 2302c6fcbb2SVadim Pasternak ret = mp2975_read_phase(client, data, page, phase, 2312c6fcbb2SVadim Pasternak MP2975_MFR_READ_CS7_8); 2322c6fcbb2SVadim Pasternak break; 2332c6fcbb2SVadim Pasternak case 8 ... 9: 2342c6fcbb2SVadim Pasternak ret = mp2975_read_phase(client, data, page, phase, 2352c6fcbb2SVadim Pasternak MP2975_MFR_READ_CS9_10); 2362c6fcbb2SVadim Pasternak break; 2372c6fcbb2SVadim Pasternak case 10 ... 11: 2382c6fcbb2SVadim Pasternak ret = mp2975_read_phase(client, data, page, phase, 2392c6fcbb2SVadim Pasternak MP2975_MFR_READ_CS11_12); 2402c6fcbb2SVadim Pasternak break; 2412c6fcbb2SVadim Pasternak default: 2422c6fcbb2SVadim Pasternak return -ENODATA; 2432c6fcbb2SVadim Pasternak } 2442c6fcbb2SVadim Pasternak } 2452c6fcbb2SVadim Pasternak return ret; 2462c6fcbb2SVadim Pasternak } 2472c6fcbb2SVadim Pasternak 248*5239277eSPatrick Rudolph static int mp2973_read_word_data(struct i2c_client *client, int page, 249*5239277eSPatrick Rudolph int phase, int reg) 250*5239277eSPatrick Rudolph { 251*5239277eSPatrick Rudolph const struct pmbus_driver_info *info = pmbus_get_driver_info(client); 252*5239277eSPatrick Rudolph struct mp2975_data *data = to_mp2975_data(info); 253*5239277eSPatrick Rudolph int ret; 254*5239277eSPatrick Rudolph 255*5239277eSPatrick Rudolph switch (reg) { 256*5239277eSPatrick Rudolph case PMBUS_OT_FAULT_LIMIT: 257*5239277eSPatrick Rudolph ret = mp2975_read_word_helper(client, page, phase, reg, 258*5239277eSPatrick Rudolph GENMASK(7, 0)); 259*5239277eSPatrick Rudolph break; 260*5239277eSPatrick Rudolph case PMBUS_VIN_OV_FAULT_LIMIT: 261*5239277eSPatrick Rudolph ret = mp2975_read_word_helper(client, page, phase, reg, 262*5239277eSPatrick Rudolph GENMASK(7, 0)); 263*5239277eSPatrick Rudolph if (ret < 0) 264*5239277eSPatrick Rudolph return ret; 265*5239277eSPatrick Rudolph 266*5239277eSPatrick Rudolph ret = DIV_ROUND_CLOSEST(ret, MP2975_VIN_UV_LIMIT_UNIT); 267*5239277eSPatrick Rudolph break; 268*5239277eSPatrick Rudolph case PMBUS_VOUT_OV_FAULT_LIMIT: 269*5239277eSPatrick Rudolph /* 270*5239277eSPatrick Rudolph * MP2971 and mp2973 only supports tracking (ovp1) mode. 271*5239277eSPatrick Rudolph */ 272*5239277eSPatrick Rudolph ret = mp2975_read_word_helper(client, page, phase, 273*5239277eSPatrick Rudolph MP2975_MFR_OVP_TH_SET, 274*5239277eSPatrick Rudolph GENMASK(2, 0)); 275*5239277eSPatrick Rudolph if (ret < 0) 276*5239277eSPatrick Rudolph return ret; 277*5239277eSPatrick Rudolph 278*5239277eSPatrick Rudolph ret = data->vout_max[page] + 50 * (ret + 1); 279*5239277eSPatrick Rudolph break; 280*5239277eSPatrick Rudolph case PMBUS_VOUT_UV_FAULT_LIMIT: 281*5239277eSPatrick Rudolph ret = mp2975_read_word_helper(client, page, phase, reg, 282*5239277eSPatrick Rudolph GENMASK(8, 0)); 283*5239277eSPatrick Rudolph if (ret < 0) 284*5239277eSPatrick Rudolph return ret; 285*5239277eSPatrick Rudolph ret = mp2975_vid2direct(info->vrm_version[page], ret); 286*5239277eSPatrick Rudolph break; 287*5239277eSPatrick Rudolph case PMBUS_VIRT_READ_POUT_MAX: 288*5239277eSPatrick Rudolph ret = pmbus_read_word_data(client, page, phase, 289*5239277eSPatrick Rudolph MP2973_MFR_READ_POUT_PK); 290*5239277eSPatrick Rudolph break; 291*5239277eSPatrick Rudolph case PMBUS_VIRT_READ_IOUT_MAX: 292*5239277eSPatrick Rudolph ret = pmbus_read_word_data(client, page, phase, 293*5239277eSPatrick Rudolph MP2973_MFR_READ_IOUT_PK); 294*5239277eSPatrick Rudolph break; 295*5239277eSPatrick Rudolph case PMBUS_UT_WARN_LIMIT: 296*5239277eSPatrick Rudolph case PMBUS_UT_FAULT_LIMIT: 297*5239277eSPatrick Rudolph case PMBUS_VIN_UV_WARN_LIMIT: 298*5239277eSPatrick Rudolph case PMBUS_VIN_UV_FAULT_LIMIT: 299*5239277eSPatrick Rudolph case PMBUS_VOUT_UV_WARN_LIMIT: 300*5239277eSPatrick Rudolph case PMBUS_VOUT_OV_WARN_LIMIT: 301*5239277eSPatrick Rudolph case PMBUS_VIN_OV_WARN_LIMIT: 302*5239277eSPatrick Rudolph case PMBUS_IIN_OC_FAULT_LIMIT: 303*5239277eSPatrick Rudolph case PMBUS_IOUT_OC_LV_FAULT_LIMIT: 304*5239277eSPatrick Rudolph case PMBUS_IOUT_OC_WARN_LIMIT: 305*5239277eSPatrick Rudolph case PMBUS_IOUT_OC_FAULT_LIMIT: 306*5239277eSPatrick Rudolph case PMBUS_IOUT_UC_FAULT_LIMIT: 307*5239277eSPatrick Rudolph case PMBUS_POUT_OP_FAULT_LIMIT: 308*5239277eSPatrick Rudolph case PMBUS_POUT_OP_WARN_LIMIT: 309*5239277eSPatrick Rudolph case PMBUS_PIN_OP_WARN_LIMIT: 310*5239277eSPatrick Rudolph return -ENXIO; 311*5239277eSPatrick Rudolph default: 312*5239277eSPatrick Rudolph return -ENODATA; 313*5239277eSPatrick Rudolph } 314*5239277eSPatrick Rudolph 315*5239277eSPatrick Rudolph return ret; 316*5239277eSPatrick Rudolph } 317*5239277eSPatrick Rudolph 3182c6fcbb2SVadim Pasternak static int mp2975_read_word_data(struct i2c_client *client, int page, 3192c6fcbb2SVadim Pasternak int phase, int reg) 3202c6fcbb2SVadim Pasternak { 3212c6fcbb2SVadim Pasternak const struct pmbus_driver_info *info = pmbus_get_driver_info(client); 3222c6fcbb2SVadim Pasternak struct mp2975_data *data = to_mp2975_data(info); 3232c6fcbb2SVadim Pasternak int ret; 3242c6fcbb2SVadim Pasternak 3252c6fcbb2SVadim Pasternak switch (reg) { 3262c6fcbb2SVadim Pasternak case PMBUS_OT_FAULT_LIMIT: 3272c6fcbb2SVadim Pasternak ret = mp2975_read_word_helper(client, page, phase, reg, 3282c6fcbb2SVadim Pasternak GENMASK(7, 0)); 3292c6fcbb2SVadim Pasternak break; 3302c6fcbb2SVadim Pasternak case PMBUS_VIN_OV_FAULT_LIMIT: 3312c6fcbb2SVadim Pasternak ret = mp2975_read_word_helper(client, page, phase, reg, 3322c6fcbb2SVadim Pasternak GENMASK(7, 0)); 3332c6fcbb2SVadim Pasternak if (ret < 0) 3342c6fcbb2SVadim Pasternak return ret; 3352c6fcbb2SVadim Pasternak 3362c6fcbb2SVadim Pasternak ret = DIV_ROUND_CLOSEST(ret, MP2975_VIN_UV_LIMIT_UNIT); 3372c6fcbb2SVadim Pasternak break; 3382c6fcbb2SVadim Pasternak case PMBUS_VOUT_OV_FAULT_LIMIT: 3392c6fcbb2SVadim Pasternak /* 3402c6fcbb2SVadim Pasternak * Register provides two values for over-voltage protection 3412c6fcbb2SVadim Pasternak * threshold for fixed (ovp2) and tracking (ovp1) modes. The 3422c6fcbb2SVadim Pasternak * minimum of these two values is provided as over-voltage 3432c6fcbb2SVadim Pasternak * fault alarm. 3442c6fcbb2SVadim Pasternak */ 3452c6fcbb2SVadim Pasternak ret = mp2975_read_word_helper(client, page, phase, 3462c6fcbb2SVadim Pasternak MP2975_MFR_OVP_TH_SET, 3472c6fcbb2SVadim Pasternak GENMASK(2, 0)); 3482c6fcbb2SVadim Pasternak if (ret < 0) 3492c6fcbb2SVadim Pasternak return ret; 3502c6fcbb2SVadim Pasternak 3512c6fcbb2SVadim Pasternak ret = min_t(int, data->vout_max[page] + 50 * (ret + 1), 3522c6fcbb2SVadim Pasternak data->vout_ov_fixed[page]); 3532c6fcbb2SVadim Pasternak break; 3542c6fcbb2SVadim Pasternak case PMBUS_VOUT_UV_FAULT_LIMIT: 3552c6fcbb2SVadim Pasternak ret = mp2975_read_word_helper(client, page, phase, 3562c6fcbb2SVadim Pasternak MP2975_MFR_UVP_SET, 3572c6fcbb2SVadim Pasternak GENMASK(2, 0)); 3582c6fcbb2SVadim Pasternak if (ret < 0) 3592c6fcbb2SVadim Pasternak return ret; 3602c6fcbb2SVadim Pasternak 3612c6fcbb2SVadim Pasternak ret = DIV_ROUND_CLOSEST(data->vref[page] * 10 - 50 * 3622c6fcbb2SVadim Pasternak (ret + 1) * data->vout_scale, 10); 3632c6fcbb2SVadim Pasternak break; 3642c6fcbb2SVadim Pasternak case PMBUS_VIRT_READ_POUT_MAX: 3652c6fcbb2SVadim Pasternak ret = mp2975_read_word_helper(client, page, phase, 3662c6fcbb2SVadim Pasternak MP2975_MFR_READ_POUT_PK, 3672c6fcbb2SVadim Pasternak GENMASK(12, 0)); 3682c6fcbb2SVadim Pasternak if (ret < 0) 3692c6fcbb2SVadim Pasternak return ret; 3702c6fcbb2SVadim Pasternak 3712c6fcbb2SVadim Pasternak ret = DIV_ROUND_CLOSEST(ret, 4); 3722c6fcbb2SVadim Pasternak break; 3732c6fcbb2SVadim Pasternak case PMBUS_VIRT_READ_IOUT_MAX: 3742c6fcbb2SVadim Pasternak ret = mp2975_read_word_helper(client, page, phase, 3752c6fcbb2SVadim Pasternak MP2975_MFR_READ_IOUT_PK, 3762c6fcbb2SVadim Pasternak GENMASK(12, 0)); 3772c6fcbb2SVadim Pasternak if (ret < 0) 3782c6fcbb2SVadim Pasternak return ret; 3792c6fcbb2SVadim Pasternak 3802c6fcbb2SVadim Pasternak ret = DIV_ROUND_CLOSEST(ret, 4); 3812c6fcbb2SVadim Pasternak break; 3822c6fcbb2SVadim Pasternak case PMBUS_READ_IOUT: 3832c6fcbb2SVadim Pasternak ret = mp2975_read_phases(client, data, page, phase); 3842c6fcbb2SVadim Pasternak if (ret < 0) 3852c6fcbb2SVadim Pasternak return ret; 3862c6fcbb2SVadim Pasternak 3872c6fcbb2SVadim Pasternak break; 3882c6fcbb2SVadim Pasternak case PMBUS_UT_WARN_LIMIT: 3892c6fcbb2SVadim Pasternak case PMBUS_UT_FAULT_LIMIT: 3902c6fcbb2SVadim Pasternak case PMBUS_VIN_UV_WARN_LIMIT: 3912c6fcbb2SVadim Pasternak case PMBUS_VIN_UV_FAULT_LIMIT: 3922c6fcbb2SVadim Pasternak case PMBUS_VOUT_UV_WARN_LIMIT: 3932c6fcbb2SVadim Pasternak case PMBUS_VOUT_OV_WARN_LIMIT: 3942c6fcbb2SVadim Pasternak case PMBUS_VIN_OV_WARN_LIMIT: 3952c6fcbb2SVadim Pasternak case PMBUS_IIN_OC_FAULT_LIMIT: 3962c6fcbb2SVadim Pasternak case PMBUS_IOUT_OC_LV_FAULT_LIMIT: 3972c6fcbb2SVadim Pasternak case PMBUS_IIN_OC_WARN_LIMIT: 3982c6fcbb2SVadim Pasternak case PMBUS_IOUT_OC_WARN_LIMIT: 3992c6fcbb2SVadim Pasternak case PMBUS_IOUT_OC_FAULT_LIMIT: 4002c6fcbb2SVadim Pasternak case PMBUS_IOUT_UC_FAULT_LIMIT: 4012c6fcbb2SVadim Pasternak case PMBUS_POUT_OP_FAULT_LIMIT: 4022c6fcbb2SVadim Pasternak case PMBUS_POUT_OP_WARN_LIMIT: 4032c6fcbb2SVadim Pasternak case PMBUS_PIN_OP_WARN_LIMIT: 4042c6fcbb2SVadim Pasternak return -ENXIO; 4052c6fcbb2SVadim Pasternak default: 4062c6fcbb2SVadim Pasternak return -ENODATA; 4072c6fcbb2SVadim Pasternak } 4082c6fcbb2SVadim Pasternak 4092c6fcbb2SVadim Pasternak return ret; 4102c6fcbb2SVadim Pasternak } 4112c6fcbb2SVadim Pasternak 412e2c90b48SPatrick Rudolph static int mp2975_identify_multiphase_rail2(struct i2c_client *client, 413e2c90b48SPatrick Rudolph struct mp2975_data *data) 4142c6fcbb2SVadim Pasternak { 4152c6fcbb2SVadim Pasternak int ret; 4162c6fcbb2SVadim Pasternak 4172c6fcbb2SVadim Pasternak /* 418e2c90b48SPatrick Rudolph * Identify multiphase for rail 2 - could be from 0 to data->max_phases[1]. 4192c6fcbb2SVadim Pasternak * In case phase number is zero – only page zero is supported 4202c6fcbb2SVadim Pasternak */ 4212c6fcbb2SVadim Pasternak ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2); 4222c6fcbb2SVadim Pasternak if (ret < 0) 4232c6fcbb2SVadim Pasternak return ret; 4242c6fcbb2SVadim Pasternak 4252c6fcbb2SVadim Pasternak ret = i2c_smbus_read_word_data(client, MP2975_MFR_VR_MULTI_CONFIG_R2); 4262c6fcbb2SVadim Pasternak if (ret < 0) 4272c6fcbb2SVadim Pasternak return ret; 4282c6fcbb2SVadim Pasternak 4292c6fcbb2SVadim Pasternak ret &= GENMASK(2, 0); 430e2c90b48SPatrick Rudolph return (ret >= data->max_phases[1]) ? data->max_phases[1] : ret; 4312c6fcbb2SVadim Pasternak } 4322c6fcbb2SVadim Pasternak 4332c6fcbb2SVadim Pasternak static void mp2975_set_phase_rail1(struct pmbus_driver_info *info) 4342c6fcbb2SVadim Pasternak { 4352c6fcbb2SVadim Pasternak int i; 4362c6fcbb2SVadim Pasternak 4372c6fcbb2SVadim Pasternak for (i = 0 ; i < info->phases[0]; i++) 4382c6fcbb2SVadim Pasternak info->pfunc[i] = PMBUS_HAVE_IOUT; 4392c6fcbb2SVadim Pasternak } 4402c6fcbb2SVadim Pasternak 4412c6fcbb2SVadim Pasternak static void 4422c6fcbb2SVadim Pasternak mp2975_set_phase_rail2(struct pmbus_driver_info *info, int num_phases) 4432c6fcbb2SVadim Pasternak { 4442c6fcbb2SVadim Pasternak int i; 4452c6fcbb2SVadim Pasternak 4462c6fcbb2SVadim Pasternak /* Set phases for rail 2 from upper to lower. */ 4472c6fcbb2SVadim Pasternak for (i = 1; i <= num_phases; i++) 4482c6fcbb2SVadim Pasternak info->pfunc[MP2975_MAX_PHASE_RAIL1 - i] = PMBUS_HAVE_IOUT; 4492c6fcbb2SVadim Pasternak } 4502c6fcbb2SVadim Pasternak 4512c6fcbb2SVadim Pasternak static int 4522c6fcbb2SVadim Pasternak mp2975_identify_multiphase(struct i2c_client *client, struct mp2975_data *data, 4532c6fcbb2SVadim Pasternak struct pmbus_driver_info *info) 4542c6fcbb2SVadim Pasternak { 4552c6fcbb2SVadim Pasternak int num_phases2, ret; 4562c6fcbb2SVadim Pasternak 4572c6fcbb2SVadim Pasternak ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2); 4582c6fcbb2SVadim Pasternak if (ret < 0) 4592c6fcbb2SVadim Pasternak return ret; 4602c6fcbb2SVadim Pasternak 461e2c90b48SPatrick Rudolph /* Identify multiphase for rail 1 - could be from 1 to data->max_phases[0]. */ 4622c6fcbb2SVadim Pasternak ret = i2c_smbus_read_word_data(client, MP2975_MFR_VR_MULTI_CONFIG_R1); 4632c6fcbb2SVadim Pasternak if (ret <= 0) 4642c6fcbb2SVadim Pasternak return ret; 4652c6fcbb2SVadim Pasternak 4662c6fcbb2SVadim Pasternak info->phases[0] = ret & GENMASK(3, 0); 4672c6fcbb2SVadim Pasternak 4682c6fcbb2SVadim Pasternak /* 469e2c90b48SPatrick Rudolph * The device provides a total of $n PWM pins, and can be configured 4702c6fcbb2SVadim Pasternak * to different phase count applications for rail 1 and rail 2. 471e2c90b48SPatrick Rudolph * Rail 1 can be set to $n phases, while rail 2 can be set to less than 472e2c90b48SPatrick Rudolph * that. When rail 1’s phase count is configured as 0, rail 4732c6fcbb2SVadim Pasternak * 1 operates with 1-phase DCM. When rail 2 phase count is configured 4742c6fcbb2SVadim Pasternak * as 0, rail 2 is disabled. 4752c6fcbb2SVadim Pasternak */ 476e2c90b48SPatrick Rudolph if (info->phases[0] > data->max_phases[0]) 4772c6fcbb2SVadim Pasternak return -EINVAL; 4782c6fcbb2SVadim Pasternak 4792c6fcbb2SVadim Pasternak mp2975_set_phase_rail1(info); 480e2c90b48SPatrick Rudolph num_phases2 = min(data->max_phases[0] - info->phases[0], 481e2c90b48SPatrick Rudolph data->max_phases[1]); 4822c6fcbb2SVadim Pasternak if (info->phases[1] && info->phases[1] <= num_phases2) 4832c6fcbb2SVadim Pasternak mp2975_set_phase_rail2(info, num_phases2); 4842c6fcbb2SVadim Pasternak 4852c6fcbb2SVadim Pasternak return 0; 4862c6fcbb2SVadim Pasternak } 4872c6fcbb2SVadim Pasternak 4882c6fcbb2SVadim Pasternak static int 4892c6fcbb2SVadim Pasternak mp2975_identify_vid(struct i2c_client *client, struct mp2975_data *data, 4902c6fcbb2SVadim Pasternak struct pmbus_driver_info *info, u32 reg, int page, 4912c6fcbb2SVadim Pasternak u32 imvp_bit, u32 vr_bit) 4922c6fcbb2SVadim Pasternak { 4932c6fcbb2SVadim Pasternak int ret; 4942c6fcbb2SVadim Pasternak 4952c6fcbb2SVadim Pasternak /* Identify VID mode and step selection. */ 4962c6fcbb2SVadim Pasternak ret = i2c_smbus_read_word_data(client, reg); 4972c6fcbb2SVadim Pasternak if (ret < 0) 4982c6fcbb2SVadim Pasternak return ret; 4992c6fcbb2SVadim Pasternak 5002c6fcbb2SVadim Pasternak if (ret & imvp_bit) { 5012c6fcbb2SVadim Pasternak info->vrm_version[page] = imvp9; 5022c6fcbb2SVadim Pasternak data->vid_step[page] = MP2975_PROT_DEV_OV_OFF; 5032c6fcbb2SVadim Pasternak } else if (ret & vr_bit) { 5042c6fcbb2SVadim Pasternak info->vrm_version[page] = vr12; 5052c6fcbb2SVadim Pasternak data->vid_step[page] = MP2975_PROT_DEV_OV_ON; 5062c6fcbb2SVadim Pasternak } else { 5072c6fcbb2SVadim Pasternak info->vrm_version[page] = vr13; 5082c6fcbb2SVadim Pasternak data->vid_step[page] = MP2975_PROT_DEV_OV_OFF; 5092c6fcbb2SVadim Pasternak } 5102c6fcbb2SVadim Pasternak 5112c6fcbb2SVadim Pasternak return 0; 5122c6fcbb2SVadim Pasternak } 5132c6fcbb2SVadim Pasternak 5142c6fcbb2SVadim Pasternak static int 5152c6fcbb2SVadim Pasternak mp2975_identify_rails_vid(struct i2c_client *client, struct mp2975_data *data, 5162c6fcbb2SVadim Pasternak struct pmbus_driver_info *info) 5172c6fcbb2SVadim Pasternak { 5182c6fcbb2SVadim Pasternak int ret; 5192c6fcbb2SVadim Pasternak 5202c6fcbb2SVadim Pasternak ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2); 5212c6fcbb2SVadim Pasternak if (ret < 0) 5222c6fcbb2SVadim Pasternak return ret; 5232c6fcbb2SVadim Pasternak 5242c6fcbb2SVadim Pasternak /* Identify VID mode for rail 1. */ 5252c6fcbb2SVadim Pasternak ret = mp2975_identify_vid(client, data, info, 5262c6fcbb2SVadim Pasternak MP2975_MFR_VR_MULTI_CONFIG_R1, 0, 5272c6fcbb2SVadim Pasternak MP2975_IMVP9_EN_R1, MP2975_VID_STEP_SEL_R1); 5282c6fcbb2SVadim Pasternak if (ret < 0) 5292c6fcbb2SVadim Pasternak return ret; 5302c6fcbb2SVadim Pasternak 5312c6fcbb2SVadim Pasternak /* Identify VID mode for rail 2, if connected. */ 5322c6fcbb2SVadim Pasternak if (info->phases[1]) 5332c6fcbb2SVadim Pasternak ret = mp2975_identify_vid(client, data, info, 5342c6fcbb2SVadim Pasternak MP2975_MFR_VR_MULTI_CONFIG_R2, 1, 5352c6fcbb2SVadim Pasternak MP2975_IMVP9_EN_R2, 5362c6fcbb2SVadim Pasternak MP2975_VID_STEP_SEL_R2); 537*5239277eSPatrick Rudolph 538*5239277eSPatrick Rudolph return ret; 539*5239277eSPatrick Rudolph } 540*5239277eSPatrick Rudolph 541*5239277eSPatrick Rudolph static int 542*5239277eSPatrick Rudolph mp2973_identify_rails_vid(struct i2c_client *client, struct mp2975_data *data, 543*5239277eSPatrick Rudolph struct pmbus_driver_info *info) 544*5239277eSPatrick Rudolph { 545*5239277eSPatrick Rudolph int ret; 546*5239277eSPatrick Rudolph 547*5239277eSPatrick Rudolph ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2); 548*5239277eSPatrick Rudolph if (ret < 0) 549*5239277eSPatrick Rudolph return ret; 550*5239277eSPatrick Rudolph 551*5239277eSPatrick Rudolph /* Identify VID mode for rail 1. */ 552*5239277eSPatrick Rudolph ret = mp2975_identify_vid(client, data, info, 553*5239277eSPatrick Rudolph MP2973_MFR_VR_MULTI_CONFIG_R1, 0, 554*5239277eSPatrick Rudolph MP2973_IMVP9_EN_R1, MP2973_VID_STEP_SEL_R1); 555*5239277eSPatrick Rudolph 556*5239277eSPatrick Rudolph if (ret < 0) 557*5239277eSPatrick Rudolph return ret; 558*5239277eSPatrick Rudolph 559*5239277eSPatrick Rudolph /* Identify VID mode for rail 2, if connected. */ 560*5239277eSPatrick Rudolph if (info->phases[1]) 561*5239277eSPatrick Rudolph ret = mp2975_identify_vid(client, data, info, 562*5239277eSPatrick Rudolph MP2973_MFR_VR_MULTI_CONFIG_R2, 1, 563*5239277eSPatrick Rudolph MP2973_IMVP9_EN_R2, 564*5239277eSPatrick Rudolph MP2973_VID_STEP_SEL_R2); 565*5239277eSPatrick Rudolph 5662c6fcbb2SVadim Pasternak return ret; 5672c6fcbb2SVadim Pasternak } 5682c6fcbb2SVadim Pasternak 5692c6fcbb2SVadim Pasternak static int 5702c6fcbb2SVadim Pasternak mp2975_current_sense_gain_get(struct i2c_client *client, 5712c6fcbb2SVadim Pasternak struct mp2975_data *data) 5722c6fcbb2SVadim Pasternak { 5732c6fcbb2SVadim Pasternak int i, ret; 5742c6fcbb2SVadim Pasternak 5752c6fcbb2SVadim Pasternak /* 5762c6fcbb2SVadim Pasternak * Obtain DrMOS current sense gain of power stage from the register 5772c6fcbb2SVadim Pasternak * MP2975_MFR_VR_CONFIG1, bits 13-12. The value is selected as below: 5782c6fcbb2SVadim Pasternak * 00b - 5µA/A, 01b - 8.5µA/A, 10b - 9.7µA/A, 11b - 10µA/A. Other 5792c6fcbb2SVadim Pasternak * values are invalid. 5802c6fcbb2SVadim Pasternak */ 5812c6fcbb2SVadim Pasternak for (i = 0 ; i < data->info.pages; i++) { 5822c6fcbb2SVadim Pasternak ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, i); 5832c6fcbb2SVadim Pasternak if (ret < 0) 5842c6fcbb2SVadim Pasternak return ret; 5852c6fcbb2SVadim Pasternak ret = i2c_smbus_read_word_data(client, 5862c6fcbb2SVadim Pasternak MP2975_MFR_VR_CONFIG1); 5872c6fcbb2SVadim Pasternak if (ret < 0) 5882c6fcbb2SVadim Pasternak return ret; 5892c6fcbb2SVadim Pasternak 5902c6fcbb2SVadim Pasternak switch ((ret & MP2975_DRMOS_KCS) >> 12) { 5912c6fcbb2SVadim Pasternak case 0: 5922c6fcbb2SVadim Pasternak data->curr_sense_gain[i] = 50; 5932c6fcbb2SVadim Pasternak break; 5942c6fcbb2SVadim Pasternak case 1: 5952c6fcbb2SVadim Pasternak data->curr_sense_gain[i] = 85; 5962c6fcbb2SVadim Pasternak break; 5972c6fcbb2SVadim Pasternak case 2: 5982c6fcbb2SVadim Pasternak data->curr_sense_gain[i] = 97; 5992c6fcbb2SVadim Pasternak break; 6002c6fcbb2SVadim Pasternak default: 6012c6fcbb2SVadim Pasternak data->curr_sense_gain[i] = 100; 6022c6fcbb2SVadim Pasternak break; 6032c6fcbb2SVadim Pasternak } 6042c6fcbb2SVadim Pasternak } 6052c6fcbb2SVadim Pasternak 6062c6fcbb2SVadim Pasternak return 0; 6072c6fcbb2SVadim Pasternak } 6082c6fcbb2SVadim Pasternak 6092c6fcbb2SVadim Pasternak static int 6102c6fcbb2SVadim Pasternak mp2975_vref_get(struct i2c_client *client, struct mp2975_data *data, 6112c6fcbb2SVadim Pasternak struct pmbus_driver_info *info) 6122c6fcbb2SVadim Pasternak { 6132c6fcbb2SVadim Pasternak int ret; 6142c6fcbb2SVadim Pasternak 6152c6fcbb2SVadim Pasternak ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 3); 6162c6fcbb2SVadim Pasternak if (ret < 0) 6172c6fcbb2SVadim Pasternak return ret; 6182c6fcbb2SVadim Pasternak 6192c6fcbb2SVadim Pasternak /* Get voltage reference value for rail 1. */ 6202c6fcbb2SVadim Pasternak ret = i2c_smbus_read_word_data(client, MP2975_MFR_READ_VREF_R1); 6212c6fcbb2SVadim Pasternak if (ret < 0) 6222c6fcbb2SVadim Pasternak return ret; 6232c6fcbb2SVadim Pasternak 6242c6fcbb2SVadim Pasternak data->vref[0] = ret * data->vid_step[0]; 6252c6fcbb2SVadim Pasternak 6262c6fcbb2SVadim Pasternak /* Get voltage reference value for rail 2, if connected. */ 6272c6fcbb2SVadim Pasternak if (data->info.pages == MP2975_PAGE_NUM) { 6282c6fcbb2SVadim Pasternak ret = i2c_smbus_read_word_data(client, MP2975_MFR_READ_VREF_R2); 6292c6fcbb2SVadim Pasternak if (ret < 0) 6302c6fcbb2SVadim Pasternak return ret; 6312c6fcbb2SVadim Pasternak 6322c6fcbb2SVadim Pasternak data->vref[1] = ret * data->vid_step[1]; 6332c6fcbb2SVadim Pasternak } 6342c6fcbb2SVadim Pasternak return 0; 6352c6fcbb2SVadim Pasternak } 6362c6fcbb2SVadim Pasternak 6372c6fcbb2SVadim Pasternak static int 6382c6fcbb2SVadim Pasternak mp2975_vref_offset_get(struct i2c_client *client, struct mp2975_data *data, 6392c6fcbb2SVadim Pasternak int page) 6402c6fcbb2SVadim Pasternak { 6412c6fcbb2SVadim Pasternak int ret; 6422c6fcbb2SVadim Pasternak 6432c6fcbb2SVadim Pasternak ret = i2c_smbus_read_word_data(client, MP2975_MFR_OVP_TH_SET); 6442c6fcbb2SVadim Pasternak if (ret < 0) 6452c6fcbb2SVadim Pasternak return ret; 6462c6fcbb2SVadim Pasternak 6472c6fcbb2SVadim Pasternak switch ((ret & GENMASK(5, 3)) >> 3) { 6482c6fcbb2SVadim Pasternak case 1: 6492c6fcbb2SVadim Pasternak data->vref_off[page] = 140; 6502c6fcbb2SVadim Pasternak break; 6512c6fcbb2SVadim Pasternak case 2: 6522c6fcbb2SVadim Pasternak data->vref_off[page] = 220; 6532c6fcbb2SVadim Pasternak break; 6542c6fcbb2SVadim Pasternak case 4: 6552c6fcbb2SVadim Pasternak data->vref_off[page] = 400; 6562c6fcbb2SVadim Pasternak break; 6572c6fcbb2SVadim Pasternak default: 6582c6fcbb2SVadim Pasternak return -EINVAL; 6592c6fcbb2SVadim Pasternak } 6602c6fcbb2SVadim Pasternak return 0; 6612c6fcbb2SVadim Pasternak } 6622c6fcbb2SVadim Pasternak 6632c6fcbb2SVadim Pasternak static int 6642c6fcbb2SVadim Pasternak mp2975_vout_max_get(struct i2c_client *client, struct mp2975_data *data, 6652c6fcbb2SVadim Pasternak struct pmbus_driver_info *info, int page) 6662c6fcbb2SVadim Pasternak { 6672c6fcbb2SVadim Pasternak int ret; 6682c6fcbb2SVadim Pasternak 6692c6fcbb2SVadim Pasternak /* Get maximum reference voltage of VID-DAC in VID format. */ 6702c6fcbb2SVadim Pasternak ret = i2c_smbus_read_word_data(client, PMBUS_VOUT_MAX); 6712c6fcbb2SVadim Pasternak if (ret < 0) 6722c6fcbb2SVadim Pasternak return ret; 6732c6fcbb2SVadim Pasternak 6742c6fcbb2SVadim Pasternak data->vout_max[page] = mp2975_vid2direct(info->vrm_version[page], ret & 6752c6fcbb2SVadim Pasternak GENMASK(8, 0)); 6762c6fcbb2SVadim Pasternak return 0; 6772c6fcbb2SVadim Pasternak } 6782c6fcbb2SVadim Pasternak 6792c6fcbb2SVadim Pasternak static int 6801feb31e8SPatrick Rudolph mp2975_set_vout_format(struct i2c_client *client, 6812c6fcbb2SVadim Pasternak struct mp2975_data *data, int page) 6822c6fcbb2SVadim Pasternak { 683*5239277eSPatrick Rudolph int ret, i; 6842c6fcbb2SVadim Pasternak 685*5239277eSPatrick Rudolph /* Enable DIRECT VOUT format 1mV/LSB */ 686*5239277eSPatrick Rudolph if (data->chip_id == mp2975) { 6872c6fcbb2SVadim Pasternak ret = i2c_smbus_read_word_data(client, MP2975_MFR_DC_LOOP_CTRL); 6882c6fcbb2SVadim Pasternak if (ret < 0) 6892c6fcbb2SVadim Pasternak return ret; 6901feb31e8SPatrick Rudolph if (ret & MP2975_VOUT_FORMAT) { 6911feb31e8SPatrick Rudolph ret &= ~MP2975_VOUT_FORMAT; 6921feb31e8SPatrick Rudolph ret = i2c_smbus_write_word_data(client, MP2975_MFR_DC_LOOP_CTRL, ret); 6931feb31e8SPatrick Rudolph } 694*5239277eSPatrick Rudolph } else { 695*5239277eSPatrick Rudolph ret = i2c_smbus_read_word_data(client, MP2973_MFR_RESO_SET); 696*5239277eSPatrick Rudolph if (ret < 0) 697*5239277eSPatrick Rudolph return ret; 698*5239277eSPatrick Rudolph i = ret; 699*5239277eSPatrick Rudolph 700*5239277eSPatrick Rudolph if (page == 0) { 701*5239277eSPatrick Rudolph i &= ~MP2973_VOUT_FORMAT_R1; 702*5239277eSPatrick Rudolph i |= MP2973_VOUT_FORMAT_DIRECT_R1; 703*5239277eSPatrick Rudolph } else { 704*5239277eSPatrick Rudolph i &= ~MP2973_VOUT_FORMAT_R2; 705*5239277eSPatrick Rudolph i |= MP2973_VOUT_FORMAT_DIRECT_R2; 706*5239277eSPatrick Rudolph } 707*5239277eSPatrick Rudolph if (i != ret) 708*5239277eSPatrick Rudolph ret = i2c_smbus_write_word_data(client, MP2973_MFR_RESO_SET, i); 709*5239277eSPatrick Rudolph } 7101feb31e8SPatrick Rudolph return ret; 7112c6fcbb2SVadim Pasternak } 7122c6fcbb2SVadim Pasternak 7132c6fcbb2SVadim Pasternak static int 7142c6fcbb2SVadim Pasternak mp2975_vout_ov_scale_get(struct i2c_client *client, struct mp2975_data *data, 7152c6fcbb2SVadim Pasternak struct pmbus_driver_info *info) 7162c6fcbb2SVadim Pasternak { 7172c6fcbb2SVadim Pasternak int thres_dev, sense_ampl, ret; 7182c6fcbb2SVadim Pasternak 7192c6fcbb2SVadim Pasternak ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 0); 7202c6fcbb2SVadim Pasternak if (ret < 0) 7212c6fcbb2SVadim Pasternak return ret; 7222c6fcbb2SVadim Pasternak 7232c6fcbb2SVadim Pasternak /* 7242c6fcbb2SVadim Pasternak * Get divider for over- and under-voltage protection thresholds 7252c6fcbb2SVadim Pasternak * configuration from the Advanced Options of Auto Phase Shedding and 7262c6fcbb2SVadim Pasternak * decay register. 7272c6fcbb2SVadim Pasternak */ 7282c6fcbb2SVadim Pasternak ret = i2c_smbus_read_word_data(client, MP2975_MFR_APS_DECAY_ADV); 7292c6fcbb2SVadim Pasternak if (ret < 0) 7302c6fcbb2SVadim Pasternak return ret; 7312c6fcbb2SVadim Pasternak thres_dev = ret & MP2975_PRT_THRES_DIV_OV_EN ? MP2975_PROT_DEV_OV_ON : 7322c6fcbb2SVadim Pasternak MP2975_PROT_DEV_OV_OFF; 7332c6fcbb2SVadim Pasternak 7342c6fcbb2SVadim Pasternak /* Select the gain of remote sense amplifier. */ 7352c6fcbb2SVadim Pasternak ret = i2c_smbus_read_word_data(client, PMBUS_VOUT_SCALE_LOOP); 7362c6fcbb2SVadim Pasternak if (ret < 0) 7372c6fcbb2SVadim Pasternak return ret; 7382c6fcbb2SVadim Pasternak sense_ampl = ret & MP2975_SENSE_AMPL ? MP2975_SENSE_AMPL_HALF : 7392c6fcbb2SVadim Pasternak MP2975_SENSE_AMPL_UNIT; 7402c6fcbb2SVadim Pasternak 7412c6fcbb2SVadim Pasternak data->vout_scale = sense_ampl * thres_dev; 7422c6fcbb2SVadim Pasternak 7432c6fcbb2SVadim Pasternak return 0; 7442c6fcbb2SVadim Pasternak } 7452c6fcbb2SVadim Pasternak 7462c6fcbb2SVadim Pasternak static int 7472c6fcbb2SVadim Pasternak mp2975_vout_per_rail_config_get(struct i2c_client *client, 7482c6fcbb2SVadim Pasternak struct mp2975_data *data, 7492c6fcbb2SVadim Pasternak struct pmbus_driver_info *info) 7502c6fcbb2SVadim Pasternak { 7512c6fcbb2SVadim Pasternak int i, ret; 7522c6fcbb2SVadim Pasternak 7532c6fcbb2SVadim Pasternak for (i = 0; i < data->info.pages; i++) { 7542c6fcbb2SVadim Pasternak ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, i); 7552c6fcbb2SVadim Pasternak if (ret < 0) 756*5239277eSPatrick Rudolph continue; 7572c6fcbb2SVadim Pasternak 758*5239277eSPatrick Rudolph /* Set VOUT format for READ_VOUT command : direct. */ 759*5239277eSPatrick Rudolph ret = mp2975_set_vout_format(client, data, i); 7602c6fcbb2SVadim Pasternak if (ret < 0) 7612c6fcbb2SVadim Pasternak return ret; 7622c6fcbb2SVadim Pasternak 7632c6fcbb2SVadim Pasternak /* Obtain maximum voltage values. */ 7642c6fcbb2SVadim Pasternak ret = mp2975_vout_max_get(client, data, info, i); 7652c6fcbb2SVadim Pasternak if (ret < 0) 7662c6fcbb2SVadim Pasternak return ret; 7672c6fcbb2SVadim Pasternak 768*5239277eSPatrick Rudolph /* Skip if reading Vref is unsupported */ 769*5239277eSPatrick Rudolph if (data->chip_id != mp2975) 770*5239277eSPatrick Rudolph continue; 771*5239277eSPatrick Rudolph 772*5239277eSPatrick Rudolph /* Obtain voltage reference offsets. */ 773*5239277eSPatrick Rudolph ret = mp2975_vref_offset_get(client, data, i); 7742c6fcbb2SVadim Pasternak if (ret < 0) 7752c6fcbb2SVadim Pasternak return ret; 7762c6fcbb2SVadim Pasternak 7772c6fcbb2SVadim Pasternak /* 7782c6fcbb2SVadim Pasternak * Set over-voltage fixed value. Thresholds are provided as 7792c6fcbb2SVadim Pasternak * fixed value, and tracking value. The minimum of them are 7802c6fcbb2SVadim Pasternak * exposed as over-voltage critical threshold. 7812c6fcbb2SVadim Pasternak */ 7822c6fcbb2SVadim Pasternak data->vout_ov_fixed[i] = data->vref[i] + 7832c6fcbb2SVadim Pasternak DIV_ROUND_CLOSEST(data->vref_off[i] * 7842c6fcbb2SVadim Pasternak data->vout_scale, 7852c6fcbb2SVadim Pasternak 10); 7862c6fcbb2SVadim Pasternak } 7872c6fcbb2SVadim Pasternak 7882c6fcbb2SVadim Pasternak return 0; 7892c6fcbb2SVadim Pasternak } 7902c6fcbb2SVadim Pasternak 7912c6fcbb2SVadim Pasternak static struct pmbus_driver_info mp2975_info = { 7922c6fcbb2SVadim Pasternak .pages = 1, 7932c6fcbb2SVadim Pasternak .format[PSC_VOLTAGE_IN] = linear, 7942c6fcbb2SVadim Pasternak .format[PSC_VOLTAGE_OUT] = direct, 7952c6fcbb2SVadim Pasternak .format[PSC_TEMPERATURE] = direct, 7962c6fcbb2SVadim Pasternak .format[PSC_CURRENT_IN] = linear, 7972c6fcbb2SVadim Pasternak .format[PSC_CURRENT_OUT] = direct, 7982c6fcbb2SVadim Pasternak .format[PSC_POWER] = direct, 7992c6fcbb2SVadim Pasternak .m[PSC_TEMPERATURE] = 1, 8002c6fcbb2SVadim Pasternak .m[PSC_VOLTAGE_OUT] = 1, 8012c6fcbb2SVadim Pasternak .R[PSC_VOLTAGE_OUT] = 3, 8022c6fcbb2SVadim Pasternak .m[PSC_CURRENT_OUT] = 1, 8032c6fcbb2SVadim Pasternak .m[PSC_POWER] = 1, 8042c6fcbb2SVadim Pasternak .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | 8052c6fcbb2SVadim Pasternak PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT | 8062c6fcbb2SVadim Pasternak PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP | PMBUS_HAVE_POUT | 8072c6fcbb2SVadim Pasternak PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT | PMBUS_PHASE_VIRTUAL, 8082c6fcbb2SVadim Pasternak .read_word_data = mp2975_read_word_data, 8092c6fcbb2SVadim Pasternak }; 8102c6fcbb2SVadim Pasternak 811*5239277eSPatrick Rudolph static struct pmbus_driver_info mp2973_info = { 812*5239277eSPatrick Rudolph .pages = 1, 813*5239277eSPatrick Rudolph .format[PSC_VOLTAGE_IN] = linear, 814*5239277eSPatrick Rudolph .format[PSC_VOLTAGE_OUT] = direct, 815*5239277eSPatrick Rudolph .format[PSC_TEMPERATURE] = linear, 816*5239277eSPatrick Rudolph .format[PSC_CURRENT_IN] = linear, 817*5239277eSPatrick Rudolph .format[PSC_CURRENT_OUT] = linear, 818*5239277eSPatrick Rudolph .format[PSC_POWER] = linear, 819*5239277eSPatrick Rudolph .m[PSC_VOLTAGE_OUT] = 1, 820*5239277eSPatrick Rudolph .R[PSC_VOLTAGE_OUT] = 3, 821*5239277eSPatrick Rudolph .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | 822*5239277eSPatrick Rudolph PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT | 823*5239277eSPatrick Rudolph PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP | PMBUS_HAVE_POUT | 824*5239277eSPatrick Rudolph PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT, 825*5239277eSPatrick Rudolph .read_word_data = mp2973_read_word_data, 826*5239277eSPatrick Rudolph }; 827*5239277eSPatrick Rudolph 8282c6fcbb2SVadim Pasternak static int mp2975_probe(struct i2c_client *client) 8292c6fcbb2SVadim Pasternak { 8302c6fcbb2SVadim Pasternak struct pmbus_driver_info *info; 8312c6fcbb2SVadim Pasternak struct mp2975_data *data; 8322c6fcbb2SVadim Pasternak int ret; 8332c6fcbb2SVadim Pasternak 8342c6fcbb2SVadim Pasternak data = devm_kzalloc(&client->dev, sizeof(struct mp2975_data), 8352c6fcbb2SVadim Pasternak GFP_KERNEL); 8362c6fcbb2SVadim Pasternak if (!data) 8372c6fcbb2SVadim Pasternak return -ENOMEM; 8382c6fcbb2SVadim Pasternak 8391f6f34d0SPatrick Rudolph if (client->dev.of_node) 8401f6f34d0SPatrick Rudolph data->chip_id = (enum chips)(unsigned long)of_device_get_match_data(&client->dev); 8411f6f34d0SPatrick Rudolph else 8421f6f34d0SPatrick Rudolph data->chip_id = i2c_match_id(mp2975_id, client)->driver_data; 8431f6f34d0SPatrick Rudolph 844e2c90b48SPatrick Rudolph memcpy(data->max_phases, mp2975_max_phases[data->chip_id], 845e2c90b48SPatrick Rudolph sizeof(data->max_phases)); 846e2c90b48SPatrick Rudolph 847*5239277eSPatrick Rudolph if (data->chip_id == mp2975) 848*5239277eSPatrick Rudolph memcpy(&data->info, &mp2975_info, sizeof(*info)); 849*5239277eSPatrick Rudolph else 850*5239277eSPatrick Rudolph memcpy(&data->info, &mp2973_info, sizeof(*info)); 851*5239277eSPatrick Rudolph 8522c6fcbb2SVadim Pasternak info = &data->info; 8532c6fcbb2SVadim Pasternak 8542c6fcbb2SVadim Pasternak /* Identify multiphase configuration for rail 2. */ 855e2c90b48SPatrick Rudolph ret = mp2975_identify_multiphase_rail2(client, data); 8562c6fcbb2SVadim Pasternak if (ret < 0) 8572c6fcbb2SVadim Pasternak return ret; 8582c6fcbb2SVadim Pasternak 8592c6fcbb2SVadim Pasternak if (ret) { 8602c6fcbb2SVadim Pasternak /* Two rails are connected. */ 8612c6fcbb2SVadim Pasternak data->info.pages = MP2975_PAGE_NUM; 8622c6fcbb2SVadim Pasternak data->info.phases[1] = ret; 8632c6fcbb2SVadim Pasternak data->info.func[1] = MP2975_RAIL2_FUNC; 8642c6fcbb2SVadim Pasternak } 8652c6fcbb2SVadim Pasternak 866*5239277eSPatrick Rudolph if (data->chip_id == mp2975) { 8672c6fcbb2SVadim Pasternak /* Identify multiphase configuration. */ 8682c6fcbb2SVadim Pasternak ret = mp2975_identify_multiphase(client, data, info); 8692c6fcbb2SVadim Pasternak if (ret) 8702c6fcbb2SVadim Pasternak return ret; 8712c6fcbb2SVadim Pasternak 8722c6fcbb2SVadim Pasternak /* Identify VID setting per rail. */ 8732c6fcbb2SVadim Pasternak ret = mp2975_identify_rails_vid(client, data, info); 8742c6fcbb2SVadim Pasternak if (ret < 0) 8752c6fcbb2SVadim Pasternak return ret; 8762c6fcbb2SVadim Pasternak 8772c6fcbb2SVadim Pasternak /* Obtain current sense gain of power stage. */ 8782c6fcbb2SVadim Pasternak ret = mp2975_current_sense_gain_get(client, data); 8792c6fcbb2SVadim Pasternak if (ret) 8802c6fcbb2SVadim Pasternak return ret; 8812c6fcbb2SVadim Pasternak 8822c6fcbb2SVadim Pasternak /* Obtain voltage reference values. */ 8832c6fcbb2SVadim Pasternak ret = mp2975_vref_get(client, data, info); 8842c6fcbb2SVadim Pasternak if (ret) 8852c6fcbb2SVadim Pasternak return ret; 8862c6fcbb2SVadim Pasternak 8872c6fcbb2SVadim Pasternak /* Obtain vout over-voltage scales. */ 8882c6fcbb2SVadim Pasternak ret = mp2975_vout_ov_scale_get(client, data, info); 8892c6fcbb2SVadim Pasternak if (ret < 0) 8902c6fcbb2SVadim Pasternak return ret; 891*5239277eSPatrick Rudolph } else { 892*5239277eSPatrick Rudolph /* Identify VID setting per rail. */ 893*5239277eSPatrick Rudolph ret = mp2973_identify_rails_vid(client, data, info); 894*5239277eSPatrick Rudolph if (ret < 0) 895*5239277eSPatrick Rudolph return ret; 896*5239277eSPatrick Rudolph } 8972c6fcbb2SVadim Pasternak 8982c6fcbb2SVadim Pasternak /* Obtain offsets, maximum and format for vout. */ 8992c6fcbb2SVadim Pasternak ret = mp2975_vout_per_rail_config_get(client, data, info); 9002c6fcbb2SVadim Pasternak if (ret) 9012c6fcbb2SVadim Pasternak return ret; 9022c6fcbb2SVadim Pasternak 9032c6fcbb2SVadim Pasternak return pmbus_do_probe(client, info); 9042c6fcbb2SVadim Pasternak } 9052c6fcbb2SVadim Pasternak 9062c6fcbb2SVadim Pasternak static const struct of_device_id __maybe_unused mp2975_of_match[] = { 907*5239277eSPatrick Rudolph {.compatible = "mps,mp2971", .data = (void *)mp2971}, 908*5239277eSPatrick Rudolph {.compatible = "mps,mp2973", .data = (void *)mp2973}, 9091f6f34d0SPatrick Rudolph {.compatible = "mps,mp2975", .data = (void *)mp2975}, 9102c6fcbb2SVadim Pasternak {} 9112c6fcbb2SVadim Pasternak }; 9122c6fcbb2SVadim Pasternak MODULE_DEVICE_TABLE(of, mp2975_of_match); 9132c6fcbb2SVadim Pasternak 9142c6fcbb2SVadim Pasternak static struct i2c_driver mp2975_driver = { 9152c6fcbb2SVadim Pasternak .driver = { 9162c6fcbb2SVadim Pasternak .name = "mp2975", 9172c6fcbb2SVadim Pasternak .of_match_table = of_match_ptr(mp2975_of_match), 9182c6fcbb2SVadim Pasternak }, 9191975d167SUwe Kleine-König .probe = mp2975_probe, 9202c6fcbb2SVadim Pasternak .id_table = mp2975_id, 9212c6fcbb2SVadim Pasternak }; 9222c6fcbb2SVadim Pasternak 9232c6fcbb2SVadim Pasternak module_i2c_driver(mp2975_driver); 9242c6fcbb2SVadim Pasternak 9252c6fcbb2SVadim Pasternak MODULE_AUTHOR("Vadim Pasternak <vadimp@nvidia.com>"); 9262c6fcbb2SVadim Pasternak MODULE_DESCRIPTION("PMBus driver for MPS MP2975 device"); 9272c6fcbb2SVadim Pasternak MODULE_LICENSE("GPL"); 928b94ca77eSGuenter Roeck MODULE_IMPORT_NS(PMBUS); 929