| 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 |
int vid_offset[MP2925_PAGE_NUM]; |
| 44 |
}; |
| 45 |
|
| 46 |
#define to_mp2925_data(x) container_of(x, struct mp2925_data, info) |
| 47 |
|
| 48 |
static u16 mp2925_linear_exp_transfer(u16 word, u16 expect_exponent) |
| 49 |
{ |
| 50 |
s16 exponent, mantissa, target_exponent; |
| 51 |
|
| 52 |
exponent = ((s16)word) >> 11; |
| 53 |
mantissa = ((s16)((word & 0x7ff) << 5)) >> 5; |
| 54 |
target_exponent = (s16)((expect_exponent & 0x1f) << 11) >> 11; |
| 55 |
|
| 56 |
if (exponent > target_exponent) |
| 57 |
mantissa = mantissa << (exponent - target_exponent); |
| 58 |
else |
| 59 |
mantissa = mantissa >> (target_exponent - exponent); |
| 60 |
|
| 61 |
return (mantissa & 0x7ff) | ((expect_exponent << 11) & 0xf800); |
| 62 |
} |
| 63 |
|
| 64 |
static int mp2925_read_byte_data(struct i2c_client *client, int page, int reg) |
| 65 |
{ |
| 66 |
int ret; |
| 67 |
|
| 68 |
switch (reg) { |
| 69 |
case PMBUS_VOUT_MODE: |
| 70 |
/* |
| 71 |
* The MP2925 does not follow standard PMBus protocol completely, |
| 72 |
* and the calculation of vout in this driver is based on direct |
| 73 |
* format. As a result, the format of vout is enforced to direct. |
| 74 |
*/ |
| 75 |
ret = PB_VOUT_MODE_DIRECT; |
| 76 |
break; |
| 77 |
default: |
| 78 |
ret = -ENODATA; |
| 79 |
break; |
| 80 |
} |
| 81 |
|
| 82 |
return ret; |
| 83 |
} |
| 84 |
|
| 85 |
static int mp2925_read_word_data(struct i2c_client *client, int page, int phase, |
| 86 |
int reg) |
| 87 |
{ |
| 88 |
const struct pmbus_driver_info *info = pmbus_get_driver_info(client); |
| 89 |
struct mp2925_data *data = to_mp2925_data(info); |
| 90 |
int ret; |
| 91 |
|
| 92 |
switch (reg) { |
| 93 |
case PMBUS_READ_VOUT: |
| 94 |
ret = pmbus_read_word_data(client, page, phase, reg); |
| 95 |
if (ret < 0) |
| 96 |
return ret; |
| 97 |
|
| 98 |
/* |
| 99 |
* In vid mode, the MP2925 vout telemetry has 49 vid step offset, but |
| 100 |
* PMBUS_VOUT_OV_FAULT_LIMIT and PMBUS_VOUT_UV_FAULT_LIMIT do not take |
| 101 |
* this into consideration, its resolution is 1.95mV/LSB, as a result, |
| 102 |
* format[PSC_VOLTAGE_OUT] can not be set to vid directly. Adding extra |
| 103 |
* vid_offset variable for vout telemetry. |
| 104 |
*/ |
| 105 |
ret = DIV_ROUND_CLOSEST(((ret & GENMASK(11, 0)) + data->vid_offset[page]) * |
| 106 |
data->vout_scale[page], MP2925_VOUT_DIV); |
| 107 |
break; |
| 108 |
case PMBUS_VOUT_OV_FAULT_LIMIT: |
| 109 |
case PMBUS_VOUT_UV_FAULT_LIMIT: |
| 110 |
ret = pmbus_read_word_data(client, page, phase, reg); |
| 111 |
if (ret < 0) |
| 112 |
return ret; |
| 113 |
|
| 114 |
ret = DIV_ROUND_CLOSEST((ret & GENMASK(11, 0)) * MP2925_VOUT_OVUV_UINT, |
| 115 |
MP2925_VOUT_OVUV_DIV); |
| 116 |
break; |
| 117 |
case PMBUS_STATUS_WORD: |
| 118 |
case PMBUS_READ_VIN: |
| 119 |
case PMBUS_READ_IOUT: |
| 120 |
case PMBUS_READ_POUT: |
| 121 |
case PMBUS_READ_PIN: |
| 122 |
case PMBUS_READ_IIN: |
| 123 |
case PMBUS_READ_TEMPERATURE_1: |
| 124 |
case PMBUS_VIN_OV_FAULT_LIMIT: |
| 125 |
case PMBUS_VIN_OV_WARN_LIMIT: |
| 126 |
case PMBUS_VIN_UV_WARN_LIMIT: |
| 127 |
case PMBUS_VIN_UV_FAULT_LIMIT: |
| 128 |
case PMBUS_IOUT_OC_FAULT_LIMIT: |
| 129 |
case PMBUS_IOUT_OC_WARN_LIMIT: |
| 130 |
case PMBUS_OT_FAULT_LIMIT: |
| 131 |
case PMBUS_OT_WARN_LIMIT: |
| 132 |
ret = -ENODATA; |
| 133 |
break; |
| 134 |
default: |
| 135 |
ret = -EINVAL; |
| 136 |
break; |
| 137 |
} |
| 138 |
|
| 139 |
return ret; |
| 140 |
} |
| 141 |
|
| 142 |
static int mp2925_write_word_data(struct i2c_client *client, int page, int reg, |
| 143 |
u16 word) |
| 144 |
{ |
| 145 |
int ret; |
| 146 |
|
| 147 |
switch (reg) { |
| 148 |
case PMBUS_VIN_OV_FAULT_LIMIT: |
| 149 |
case PMBUS_VIN_OV_WARN_LIMIT: |
| 150 |
case PMBUS_VIN_UV_WARN_LIMIT: |
| 151 |
case PMBUS_VIN_UV_FAULT_LIMIT: |
| 152 |
/* |
| 153 |
* The PMBUS_VIN_OV_FAULT_LIMIT, PMBUS_VIN_OV_WARN_LIMIT, |
| 154 |
* PMBUS_VIN_UV_WARN_LIMIT and PMBUS_VIN_UV_FAULT_LIMIT |
| 155 |
* of MP2925 is linear11 format, and the exponent is a |
| 156 |
* constant value(5'b11100), so the exponent of word |
| 157 |
* parameter should be converted to 5'b11100(0x1C). |
| 158 |
*/ |
| 159 |
ret = pmbus_write_word_data(client, page, reg, |
| 160 |
mp2925_linear_exp_transfer(word, 0x1C)); |
| 161 |
break; |
| 162 |
case PMBUS_VOUT_OV_FAULT_LIMIT: |
| 163 |
case PMBUS_VOUT_UV_FAULT_LIMIT: |
| 164 |
/* |
| 165 |
* The bit0-bit11 is the limit value, and bit12-bit15 |
| 166 |
* should not be changed. |
| 167 |
*/ |
| 168 |
ret = pmbus_read_word_data(client, page, 0xff, reg); |
| 169 |
if (ret < 0) |
| 170 |
return ret; |
| 171 |
|
| 172 |
ret = pmbus_write_word_data(client, page, reg, |
| 173 |
(ret & ~GENMASK(11, 0)) | |
| 174 |
FIELD_PREP(GENMASK(11, 0), |
| 175 |
DIV_ROUND_CLOSEST(word * MP2925_VOUT_OVUV_DIV, |
| 176 |
MP2925_VOUT_OVUV_UINT))); |
| 177 |
break; |
| 178 |
case PMBUS_OT_FAULT_LIMIT: |
| 179 |
case PMBUS_OT_WARN_LIMIT: |
| 180 |
/* |
| 181 |
* The PMBUS_OT_FAULT_LIMIT and PMBUS_OT_WARN_LIMIT of |
| 182 |
* MP2925 is linear11 format, and the exponent is a |
| 183 |
* constant value(5'b00000), so the exponent of word |
| 184 |
* parameter should be converted to 5'b00000. |
| 185 |
*/ |
| 186 |
ret = pmbus_write_word_data(client, page, reg, |
| 187 |
mp2925_linear_exp_transfer(word, 0x00)); |
| 188 |
break; |
| 189 |
case PMBUS_IOUT_OC_FAULT_LIMIT: |
| 190 |
case PMBUS_IOUT_OC_WARN_LIMIT: |
| 191 |
/* |
| 192 |
* The PMBUS_IOUT_OC_FAULT_LIMIT and PMBUS_IOUT_OC_WARN_LIMIT |
| 193 |
* of MP2925 is linear11 format, and the exponent can not be |
| 194 |
* changed. |
| 195 |
*/ |
| 196 |
ret = pmbus_read_word_data(client, page, 0xff, reg); |
| 197 |
if (ret < 0) |
| 198 |
return ret; |
| 199 |
|
| 200 |
ret = pmbus_write_word_data(client, page, reg, |
| 201 |
mp2925_linear_exp_transfer(word, |
| 202 |
FIELD_GET(GENMASK(15, 11), |
| 203 |
ret))); |
| 204 |
break; |
| 205 |
default: |
| 206 |
ret = -EINVAL; |
| 207 |
break; |
| 208 |
} |
| 209 |
|
| 210 |
return ret; |
| 211 |
} |
| 212 |
|
| 213 |
static int |
| 214 |
mp2925_identify_vout_scale(struct i2c_client *client, struct pmbus_driver_info *info, |
| 215 |
int page) |
| 216 |
{ |
| 217 |
struct mp2925_data *data = to_mp2925_data(info); |
| 218 |
int ret; |
| 219 |
|
| 220 |
ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page); |
| 221 |
if (ret < 0) |
| 222 |
return ret; |
| 223 |
|
| 224 |
ret = i2c_smbus_read_byte_data(client, PMBUS_VOUT_MODE); |
| 225 |
if (ret < 0) |
| 226 |
return ret; |
| 227 |
|
| 228 |
if (FIELD_GET(GENMASK(5, 5), ret)) { |
| 229 |
ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, |
| 230 |
page == 0 ? 3 : 4); |
| 231 |
if (ret < 0) |
| 232 |
return ret; |
| 233 |
|
| 234 |
ret = i2c_smbus_read_word_data(client, MFR_VR_MULTI_CONFIG); |
| 235 |
if (ret < 0) |
| 236 |
return ret; |
| 237 |
|
| 238 |
if (FIELD_GET(GENMASK(5, 5), ret)) |
| 239 |
data->vout_scale[page] = 2560; |
| 240 |
else |
| 241 |
data->vout_scale[page] = 5120; |
| 242 |
|
| 243 |
/* |
| 244 |
* In vid mode, the MP2925 vout telemetry has 49 vid step offset, but |
| 245 |
* PMBUS_VOUT_OV_FAULT_LIMIT and PMBUS_VOUT_UV_FAULT_LIMIT do not take |
| 246 |
* this into consideration, its resolution is 1.95mV/LSB, as a result, |
| 247 |
* format[PSC_VOLTAGE_OUT] can not be set to vid directly. Adding extra |
| 248 |
* vid_offset variable for vout telemetry. |
| 249 |
*/ |
| 250 |
data->vid_offset[page] = 49; |
| 251 |
} else if (FIELD_GET(GENMASK(4, 4), ret)) { |
| 252 |
data->vout_scale[page] = 1; |
| 253 |
data->vid_offset[page] = 0; |
| 254 |
} else { |
| 255 |
data->vout_scale[page] = 512; |
| 256 |
data->vid_offset[page] = 0; |
| 257 |
} |
| 258 |
|
| 259 |
return 0; |
| 260 |
} |
| 261 |
|
| 262 |
static int mp2925_identify(struct i2c_client *client, struct pmbus_driver_info *info) |
| 263 |
{ |
| 264 |
int ret; |
| 265 |
|
| 266 |
ret = mp2925_identify_vout_scale(client, info, 0); |
| 267 |
if (ret < 0) |
| 268 |
return ret; |
| 269 |
|
| 270 |
return mp2925_identify_vout_scale(client, info, 1); |
| 271 |
} |
| 272 |
|
| 273 |
static const struct pmbus_driver_info mp2925_info = { |
| 274 |
.pages = MP2925_PAGE_NUM, |
| 275 |
.format[PSC_VOLTAGE_IN] = linear, |
| 276 |
.format[PSC_CURRENT_IN] = linear, |
| 277 |
.format[PSC_CURRENT_OUT] = linear, |
| 278 |
.format[PSC_POWER] = linear, |
| 279 |
.format[PSC_TEMPERATURE] = linear, |
| 280 |
.format[PSC_VOLTAGE_OUT] = direct, |
| 281 |
|
| 282 |
.m[PSC_VOLTAGE_OUT] = 1, |
| 283 |
.R[PSC_VOLTAGE_OUT] = 3, |
| 284 |
.b[PSC_VOLTAGE_OUT] = 0, |
| 285 |
|
| 286 |
.func[0] = MP2925_RAIL1_FUNC, |
| 287 |
.func[1] = MP2925_RAIL2_FUNC, |
| 288 |
.read_word_data = mp2925_read_word_data, |
| 289 |
.read_byte_data = mp2925_read_byte_data, |
| 290 |
.write_word_data = mp2925_write_word_data, |
| 291 |
.identify = mp2925_identify, |
| 292 |
}; |
| 293 |
|
| 294 |
static int mp2925_probe(struct i2c_client *client) |
| 295 |
{ |
| 296 |
struct mp2925_data *data; |
| 297 |
|
| 298 |
data = devm_kzalloc(&client->dev, sizeof(struct mp2925_data), GFP_KERNEL); |
| 299 |
if (!data) |
| 300 |
return -ENOMEM; |
| 301 |
|
| 302 |
memcpy(&data->info, &mp2925_info, sizeof(mp2925_info)); |
| 303 |
|
| 304 |
return pmbus_do_probe(client, &data->info); |
| 305 |
} |
| 306 |
|
| 307 |
static const struct i2c_device_id mp2925_id[] = { |
| 308 |
{"mp2925"}, |
| 309 |
{"mp2929"}, |
| 310 |
{} |
| 311 |
}; |
| 312 |
MODULE_DEVICE_TABLE(i2c, mp2925_id); |
| 313 |
|
| 314 |
static const struct of_device_id __maybe_unused mp2925_of_match[] = { |
| 315 |
{.compatible = "mps,mp2925"}, |
| 316 |
{.compatible = "mps,mp2929"}, |
| 317 |
{} |
| 318 |
}; |
| 319 |
MODULE_DEVICE_TABLE(of, mp2925_of_match); |
| 320 |
|
| 321 |
static struct i2c_driver mp2925_driver = { |
| 322 |
.driver = { |
| 323 |
.name = "mp2925", |
| 324 |
.of_match_table = mp2925_of_match, |
| 325 |
}, |
| 326 |
.probe = mp2925_probe, |
| 327 |
.id_table = mp2925_id, |
| 328 |
}; |
| 329 |
|
| 330 |
module_i2c_driver(mp2925_driver); |
| 331 |
|
| 332 |
MODULE_AUTHOR("Wensheng Wang <[email protected]>"); |
| 333 |
MODULE_DESCRIPTION("PMBus driver for MPS MP2925"); |
| 334 |
MODULE_LICENSE("GPL"); |
| 335 |
MODULE_IMPORT_NS("PMBUS"); |