1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Hardware monitoring driver for MPS Multi-phase Digital VR Controllers(MP2925) 4 */ 5 6 #include <linux/bitfield.h> 7 #include <linux/i2c.h> 8 #include <linux/module.h> 9 #include <linux/of_device.h> 10 #include "pmbus.h" 11 12 /* 13 * Vender specific register MFR_VR_MULTI_CONFIG(0x08). 14 * This register is used to obtain vid scale. 15 */ 16 #define MFR_VR_MULTI_CONFIG 0x08 17 18 #define MP2925_VOUT_DIV 512 19 #define MP2925_VOUT_OVUV_UINT 195 20 #define MP2925_VOUT_OVUV_DIV 100 21 22 #define MP2925_PAGE_NUM 2 23 24 #define MP2925_RAIL1_FUNC (PMBUS_HAVE_VIN | PMBUS_HAVE_PIN | \ 25 PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT | \ 26 PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP | \ 27 PMBUS_HAVE_STATUS_VOUT | \ 28 PMBUS_HAVE_STATUS_IOUT | \ 29 PMBUS_HAVE_STATUS_TEMP | \ 30 PMBUS_HAVE_STATUS_INPUT) 31 32 #define MP2925_RAIL2_FUNC (PMBUS_HAVE_PIN | PMBUS_HAVE_VOUT | \ 33 PMBUS_HAVE_IOUT | PMBUS_HAVE_POUT | \ 34 PMBUS_HAVE_TEMP | PMBUS_HAVE_IIN | \ 35 PMBUS_HAVE_STATUS_VOUT | \ 36 PMBUS_HAVE_STATUS_IOUT | \ 37 PMBUS_HAVE_STATUS_TEMP | \ 38 PMBUS_HAVE_STATUS_INPUT) 39 40 struct mp2925_data { 41 struct pmbus_driver_info info; 42 int vout_scale[MP2925_PAGE_NUM]; 43 }; 44 45 #define to_mp2925_data(x) container_of(x, struct mp2925_data, info) 46 47 static u16 mp2925_linear_exp_transfer(u16 word, u16 expect_exponent) 48 { 49 s16 exponent, mantissa, target_exponent; 50 51 exponent = ((s16)word) >> 11; 52 mantissa = ((s16)((word & 0x7ff) << 5)) >> 5; 53 target_exponent = (s16)((expect_exponent & 0x1f) << 11) >> 11; 54 55 if (exponent > target_exponent) 56 mantissa = mantissa << (exponent - target_exponent); 57 else 58 mantissa = mantissa >> (target_exponent - exponent); 59 60 return (mantissa & 0x7ff) | ((expect_exponent << 11) & 0xf800); 61 } 62 63 static int mp2925_read_byte_data(struct i2c_client *client, int page, int reg) 64 { 65 int ret; 66 67 switch (reg) { 68 case PMBUS_VOUT_MODE: 69 /* 70 * The MP2925 does not follow standard PMBus protocol completely, 71 * and the calculation of vout in this driver is based on direct 72 * format. As a result, the format of vout is enforced to direct. 73 */ 74 ret = PB_VOUT_MODE_DIRECT; 75 break; 76 default: 77 ret = -ENODATA; 78 break; 79 } 80 81 return ret; 82 } 83 84 static int mp2925_read_word_data(struct i2c_client *client, int page, int phase, 85 int reg) 86 { 87 const struct pmbus_driver_info *info = pmbus_get_driver_info(client); 88 struct mp2925_data *data = to_mp2925_data(info); 89 int ret; 90 91 switch (reg) { 92 case PMBUS_READ_VOUT: 93 ret = pmbus_read_word_data(client, page, phase, reg); 94 if (ret < 0) 95 return ret; 96 97 ret = DIV_ROUND_CLOSEST((ret & GENMASK(11, 0)) * data->vout_scale[page], 98 MP2925_VOUT_DIV); 99 break; 100 case PMBUS_VOUT_OV_FAULT_LIMIT: 101 case PMBUS_VOUT_UV_FAULT_LIMIT: 102 ret = pmbus_read_word_data(client, page, phase, reg); 103 if (ret < 0) 104 return ret; 105 106 ret = DIV_ROUND_CLOSEST((ret & GENMASK(11, 0)) * MP2925_VOUT_OVUV_UINT, 107 MP2925_VOUT_OVUV_DIV); 108 break; 109 case PMBUS_STATUS_WORD: 110 case PMBUS_READ_VIN: 111 case PMBUS_READ_IOUT: 112 case PMBUS_READ_POUT: 113 case PMBUS_READ_PIN: 114 case PMBUS_READ_IIN: 115 case PMBUS_READ_TEMPERATURE_1: 116 case PMBUS_VIN_OV_FAULT_LIMIT: 117 case PMBUS_VIN_OV_WARN_LIMIT: 118 case PMBUS_VIN_UV_WARN_LIMIT: 119 case PMBUS_VIN_UV_FAULT_LIMIT: 120 case PMBUS_IOUT_OC_FAULT_LIMIT: 121 case PMBUS_IOUT_OC_WARN_LIMIT: 122 case PMBUS_OT_FAULT_LIMIT: 123 case PMBUS_OT_WARN_LIMIT: 124 ret = -ENODATA; 125 break; 126 default: 127 ret = -EINVAL; 128 break; 129 } 130 131 return ret; 132 } 133 134 static int mp2925_write_word_data(struct i2c_client *client, int page, int reg, 135 u16 word) 136 { 137 int ret; 138 139 switch (reg) { 140 case PMBUS_VIN_OV_FAULT_LIMIT: 141 case PMBUS_VIN_OV_WARN_LIMIT: 142 case PMBUS_VIN_UV_WARN_LIMIT: 143 case PMBUS_VIN_UV_FAULT_LIMIT: 144 /* 145 * The PMBUS_VIN_OV_FAULT_LIMIT, PMBUS_VIN_OV_WARN_LIMIT, 146 * PMBUS_VIN_UV_WARN_LIMIT and PMBUS_VIN_UV_FAULT_LIMIT 147 * of MP2925 is linear11 format, and the exponent is a 148 * constant value(5'b11100), so the exponent of word 149 * parameter should be converted to 5'b11100(0x1C). 150 */ 151 ret = pmbus_write_word_data(client, page, reg, 152 mp2925_linear_exp_transfer(word, 0x1C)); 153 break; 154 case PMBUS_VOUT_OV_FAULT_LIMIT: 155 case PMBUS_VOUT_UV_FAULT_LIMIT: 156 /* 157 * The bit0-bit11 is the limit value, and bit12-bit15 158 * should not be changed. 159 */ 160 ret = pmbus_read_word_data(client, page, 0xff, reg); 161 if (ret < 0) 162 return ret; 163 164 ret = pmbus_write_word_data(client, page, reg, 165 (ret & ~GENMASK(11, 0)) | 166 FIELD_PREP(GENMASK(11, 0), 167 DIV_ROUND_CLOSEST(word * MP2925_VOUT_OVUV_DIV, 168 MP2925_VOUT_OVUV_UINT))); 169 break; 170 case PMBUS_OT_FAULT_LIMIT: 171 case PMBUS_OT_WARN_LIMIT: 172 /* 173 * The PMBUS_OT_FAULT_LIMIT and PMBUS_OT_WARN_LIMIT of 174 * MP2925 is linear11 format, and the exponent is a 175 * constant value(5'b00000), so the exponent of word 176 * parameter should be converted to 5'b00000. 177 */ 178 ret = pmbus_write_word_data(client, page, reg, 179 mp2925_linear_exp_transfer(word, 0x00)); 180 break; 181 case PMBUS_IOUT_OC_FAULT_LIMIT: 182 case PMBUS_IOUT_OC_WARN_LIMIT: 183 /* 184 * The PMBUS_IOUT_OC_FAULT_LIMIT and PMBUS_IOUT_OC_WARN_LIMIT 185 * of MP2925 is linear11 format, and the exponent can not be 186 * changed. 187 */ 188 ret = pmbus_read_word_data(client, page, 0xff, reg); 189 if (ret < 0) 190 return ret; 191 192 ret = pmbus_write_word_data(client, page, reg, 193 mp2925_linear_exp_transfer(word, 194 FIELD_GET(GENMASK(15, 11), 195 ret))); 196 break; 197 default: 198 ret = -EINVAL; 199 break; 200 } 201 202 return ret; 203 } 204 205 static int 206 mp2925_identify_vout_scale(struct i2c_client *client, struct pmbus_driver_info *info, 207 int page) 208 { 209 struct mp2925_data *data = to_mp2925_data(info); 210 int ret; 211 212 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page); 213 if (ret < 0) 214 return ret; 215 216 ret = i2c_smbus_read_byte_data(client, PMBUS_VOUT_MODE); 217 if (ret < 0) 218 return ret; 219 220 if (FIELD_GET(GENMASK(5, 5), ret)) { 221 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 222 page == 0 ? 3 : 4); 223 if (ret < 0) 224 return ret; 225 226 ret = i2c_smbus_read_word_data(client, MFR_VR_MULTI_CONFIG); 227 if (ret < 0) 228 return ret; 229 230 if (FIELD_GET(GENMASK(5, 5), ret)) 231 data->vout_scale[page] = 2560; 232 else 233 data->vout_scale[page] = 5120; 234 } else if (FIELD_GET(GENMASK(4, 4), ret)) { 235 data->vout_scale[page] = 1; 236 } else { 237 data->vout_scale[page] = 512; 238 } 239 240 return 0; 241 } 242 243 static int mp2925_identify(struct i2c_client *client, struct pmbus_driver_info *info) 244 { 245 int ret; 246 247 ret = mp2925_identify_vout_scale(client, info, 0); 248 if (ret < 0) 249 return ret; 250 251 return mp2925_identify_vout_scale(client, info, 1); 252 } 253 254 static const struct pmbus_driver_info mp2925_info = { 255 .pages = MP2925_PAGE_NUM, 256 .format[PSC_VOLTAGE_IN] = linear, 257 .format[PSC_CURRENT_IN] = linear, 258 .format[PSC_CURRENT_OUT] = linear, 259 .format[PSC_POWER] = linear, 260 .format[PSC_TEMPERATURE] = linear, 261 .format[PSC_VOLTAGE_OUT] = direct, 262 263 .m[PSC_VOLTAGE_OUT] = 1, 264 .R[PSC_VOLTAGE_OUT] = 3, 265 .b[PSC_VOLTAGE_OUT] = 0, 266 267 .func[0] = MP2925_RAIL1_FUNC, 268 .func[1] = MP2925_RAIL2_FUNC, 269 .read_word_data = mp2925_read_word_data, 270 .read_byte_data = mp2925_read_byte_data, 271 .write_word_data = mp2925_write_word_data, 272 .identify = mp2925_identify, 273 }; 274 275 static int mp2925_probe(struct i2c_client *client) 276 { 277 struct mp2925_data *data; 278 279 data = devm_kzalloc(&client->dev, sizeof(struct mp2925_data), GFP_KERNEL); 280 if (!data) 281 return -ENOMEM; 282 283 memcpy(&data->info, &mp2925_info, sizeof(mp2925_info)); 284 285 return pmbus_do_probe(client, &data->info); 286 } 287 288 static const struct i2c_device_id mp2925_id[] = { 289 {"mp2925"}, 290 {"mp2929"}, 291 {} 292 }; 293 MODULE_DEVICE_TABLE(i2c, mp2925_id); 294 295 static const struct of_device_id __maybe_unused mp2925_of_match[] = { 296 {.compatible = "mps,mp2925"}, 297 {.compatible = "mps,mp2929"}, 298 {} 299 }; 300 MODULE_DEVICE_TABLE(of, mp2925_of_match); 301 302 static struct i2c_driver mp2925_driver = { 303 .driver = { 304 .name = "mp2925", 305 .of_match_table = mp2925_of_match, 306 }, 307 .probe = mp2925_probe, 308 .id_table = mp2925_id, 309 }; 310 311 module_i2c_driver(mp2925_driver); 312 313 MODULE_AUTHOR("Wensheng Wang <wenswang@yeah.net>"); 314 MODULE_DESCRIPTION("PMBus driver for MPS MP2925"); 315 MODULE_LICENSE("GPL"); 316 MODULE_IMPORT_NS("PMBUS"); 317