1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Hardware monitoring driver for Infineon Multi-phase Digital VR Controllers 4 * 5 * Copyright (c) 2020 Mellanox Technologies. All rights reserved. 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 #define XDPE122_PROT_VR12_5MV 0x01 /* VR12.0 mode, 5-mV DAC */ 16 #define XDPE122_PROT_VR12_5_10MV 0x02 /* VR12.5 mode, 10-mV DAC */ 17 #define XDPE122_PROT_IMVP9_10MV 0x03 /* IMVP9 mode, 10-mV DAC */ 18 #define XDPE122_AMD_625MV 0x10 /* AMD mode 6.25mV */ 19 #define XDPE122_PAGE_NUM 2 20 21 static int xdpe122_read_word_data(struct i2c_client *client, int page, int reg) 22 { 23 const struct pmbus_driver_info *info = pmbus_get_driver_info(client); 24 long val; 25 s16 exponent; 26 s32 mantissa; 27 int ret; 28 29 switch (reg) { 30 case PMBUS_VOUT_OV_FAULT_LIMIT: 31 case PMBUS_VOUT_UV_FAULT_LIMIT: 32 ret = pmbus_read_word_data(client, page, reg); 33 if (ret < 0) 34 return ret; 35 36 /* Convert register value to LINEAR11 data. */ 37 exponent = ((s16)ret) >> 11; 38 mantissa = ((s16)((ret & GENMASK(10, 0)) << 5)) >> 5; 39 val = mantissa * 1000L; 40 if (exponent >= 0) 41 val <<= exponent; 42 else 43 val >>= -exponent; 44 45 /* Convert data to VID register. */ 46 switch (info->vrm_version[page]) { 47 case vr13: 48 if (val >= 500) 49 return 1 + DIV_ROUND_CLOSEST(val - 500, 10); 50 return 0; 51 case vr12: 52 if (val >= 250) 53 return 1 + DIV_ROUND_CLOSEST(val - 250, 5); 54 return 0; 55 case imvp9: 56 if (val >= 200) 57 return 1 + DIV_ROUND_CLOSEST(val - 200, 10); 58 return 0; 59 case amd625mv: 60 if (val >= 200 && val <= 1550) 61 return DIV_ROUND_CLOSEST((1550 - val) * 100, 62 625); 63 return 0; 64 default: 65 return -EINVAL; 66 } 67 default: 68 return -ENODATA; 69 } 70 71 return 0; 72 } 73 74 static int xdpe122_identify(struct i2c_client *client, 75 struct pmbus_driver_info *info) 76 { 77 u8 vout_params; 78 int i, ret; 79 80 for (i = 0; i < XDPE122_PAGE_NUM; i++) { 81 /* Read the register with VOUT scaling value.*/ 82 ret = pmbus_read_byte_data(client, i, PMBUS_VOUT_MODE); 83 if (ret < 0) 84 return ret; 85 86 vout_params = ret & GENMASK(4, 0); 87 88 switch (vout_params) { 89 case XDPE122_PROT_VR12_5_10MV: 90 info->vrm_version[i] = vr13; 91 break; 92 case XDPE122_PROT_VR12_5MV: 93 info->vrm_version[i] = vr12; 94 break; 95 case XDPE122_PROT_IMVP9_10MV: 96 info->vrm_version[i] = imvp9; 97 break; 98 case XDPE122_AMD_625MV: 99 info->vrm_version[i] = amd625mv; 100 break; 101 default: 102 return -EINVAL; 103 } 104 } 105 106 return 0; 107 } 108 109 static struct pmbus_driver_info xdpe122_info = { 110 .pages = XDPE122_PAGE_NUM, 111 .format[PSC_VOLTAGE_IN] = linear, 112 .format[PSC_VOLTAGE_OUT] = vid, 113 .format[PSC_TEMPERATURE] = linear, 114 .format[PSC_CURRENT_IN] = linear, 115 .format[PSC_CURRENT_OUT] = linear, 116 .format[PSC_POWER] = linear, 117 .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | 118 PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT | 119 PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP | 120 PMBUS_HAVE_POUT | PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT, 121 .func[1] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT | 122 PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT | 123 PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP | 124 PMBUS_HAVE_POUT | PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT, 125 .identify = xdpe122_identify, 126 .read_word_data = xdpe122_read_word_data, 127 }; 128 129 static int xdpe122_probe(struct i2c_client *client, 130 const struct i2c_device_id *id) 131 { 132 struct pmbus_driver_info *info; 133 134 info = devm_kmemdup(&client->dev, &xdpe122_info, sizeof(*info), 135 GFP_KERNEL); 136 if (!info) 137 return -ENOMEM; 138 139 return pmbus_do_probe(client, id, info); 140 } 141 142 static const struct i2c_device_id xdpe122_id[] = { 143 {"xdpe12254", 0}, 144 {"xdpe12284", 0}, 145 {} 146 }; 147 148 MODULE_DEVICE_TABLE(i2c, xdpe122_id); 149 150 static const struct of_device_id __maybe_unused xdpe122_of_match[] = { 151 {.compatible = "infineon,xdpe12254"}, 152 {.compatible = "infineon,xdpe12284"}, 153 {} 154 }; 155 MODULE_DEVICE_TABLE(of, xdpe122_of_match); 156 157 static struct i2c_driver xdpe122_driver = { 158 .driver = { 159 .name = "xdpe12284", 160 .of_match_table = of_match_ptr(xdpe122_of_match), 161 }, 162 .probe = xdpe122_probe, 163 .remove = pmbus_do_remove, 164 .id_table = xdpe122_id, 165 }; 166 167 module_i2c_driver(xdpe122_driver); 168 169 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>"); 170 MODULE_DESCRIPTION("PMBus driver for Infineon XDPE122 family"); 171 MODULE_LICENSE("GPL"); 172