0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/err.h>
0009 #include <linux/i2c.h>
0010 #include <linux/init.h>
0011 #include <linux/kernel.h>
0012 #include <linux/module.h>
0013 #include <linux/regulator/driver.h>
0014
0015 #include "pmbus.h"
0016
0017 #define XDPE122_PROT_VR12_5MV 0x01
0018 #define XDPE122_PROT_VR12_5_10MV 0x02
0019 #define XDPE122_PROT_IMVP9_10MV 0x03
0020 #define XDPE122_AMD_625MV 0x10
0021 #define XDPE122_PAGE_NUM 2
0022
0023 static int xdpe122_read_word_data(struct i2c_client *client, int page,
0024 int phase, int reg)
0025 {
0026 const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
0027 long val;
0028 s16 exponent;
0029 s32 mantissa;
0030 int ret;
0031
0032 switch (reg) {
0033 case PMBUS_VOUT_OV_FAULT_LIMIT:
0034 case PMBUS_VOUT_UV_FAULT_LIMIT:
0035 ret = pmbus_read_word_data(client, page, phase, reg);
0036 if (ret < 0)
0037 return ret;
0038
0039
0040 exponent = ((s16)ret) >> 11;
0041 mantissa = ((s16)((ret & GENMASK(10, 0)) << 5)) >> 5;
0042 val = mantissa * 1000L;
0043 if (exponent >= 0)
0044 val <<= exponent;
0045 else
0046 val >>= -exponent;
0047
0048
0049 switch (info->vrm_version[page]) {
0050 case vr13:
0051 if (val >= 500)
0052 return 1 + DIV_ROUND_CLOSEST(val - 500, 10);
0053 return 0;
0054 case vr12:
0055 if (val >= 250)
0056 return 1 + DIV_ROUND_CLOSEST(val - 250, 5);
0057 return 0;
0058 case imvp9:
0059 if (val >= 200)
0060 return 1 + DIV_ROUND_CLOSEST(val - 200, 10);
0061 return 0;
0062 case amd625mv:
0063 if (val >= 200 && val <= 1550)
0064 return DIV_ROUND_CLOSEST((1550 - val) * 100,
0065 625);
0066 return 0;
0067 default:
0068 return -EINVAL;
0069 }
0070 default:
0071 return -ENODATA;
0072 }
0073
0074 return 0;
0075 }
0076
0077 static int xdpe122_identify(struct i2c_client *client,
0078 struct pmbus_driver_info *info)
0079 {
0080 u8 vout_params;
0081 int i, ret, vout_mode;
0082
0083 vout_mode = pmbus_read_byte_data(client, 0, PMBUS_VOUT_MODE);
0084 if (vout_mode >= 0 && vout_mode != 0xff) {
0085 switch (vout_mode >> 5) {
0086 case 0:
0087 info->format[PSC_VOLTAGE_OUT] = linear;
0088 return 0;
0089 case 1:
0090 info->format[PSC_VOLTAGE_OUT] = vid;
0091 info->read_word_data = xdpe122_read_word_data;
0092 break;
0093 default:
0094 return -ENODEV;
0095 }
0096 }
0097
0098 for (i = 0; i < XDPE122_PAGE_NUM; i++) {
0099
0100 ret = pmbus_read_byte_data(client, i, PMBUS_VOUT_MODE);
0101 if (ret < 0)
0102 return ret;
0103
0104 vout_params = ret & GENMASK(4, 0);
0105
0106 switch (vout_params) {
0107 case XDPE122_PROT_VR12_5_10MV:
0108 info->vrm_version[i] = vr13;
0109 break;
0110 case XDPE122_PROT_VR12_5MV:
0111 info->vrm_version[i] = vr12;
0112 break;
0113 case XDPE122_PROT_IMVP9_10MV:
0114 info->vrm_version[i] = imvp9;
0115 break;
0116 case XDPE122_AMD_625MV:
0117 info->vrm_version[i] = amd625mv;
0118 break;
0119 default:
0120 return -EINVAL;
0121 }
0122 }
0123
0124 return 0;
0125 }
0126
0127 static const struct regulator_desc __maybe_unused xdpe122_reg_desc[] = {
0128 PMBUS_REGULATOR("vout", 0),
0129 PMBUS_REGULATOR("vout", 1),
0130 };
0131
0132 static struct pmbus_driver_info xdpe122_info = {
0133 .pages = XDPE122_PAGE_NUM,
0134 .format[PSC_VOLTAGE_IN] = linear,
0135 .format[PSC_TEMPERATURE] = linear,
0136 .format[PSC_CURRENT_IN] = linear,
0137 .format[PSC_CURRENT_OUT] = linear,
0138 .format[PSC_POWER] = linear,
0139 .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
0140 PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
0141 PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
0142 PMBUS_HAVE_POUT | PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT,
0143 .func[1] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
0144 PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
0145 PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
0146 PMBUS_HAVE_POUT | PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT,
0147 .identify = xdpe122_identify,
0148 #if IS_ENABLED(CONFIG_SENSORS_XDPE122_REGULATOR)
0149 .num_regulators = 2,
0150 .reg_desc = xdpe122_reg_desc,
0151 #endif
0152 };
0153
0154 static int xdpe122_probe(struct i2c_client *client)
0155 {
0156 struct pmbus_driver_info *info;
0157
0158 info = devm_kmemdup(&client->dev, &xdpe122_info, sizeof(*info),
0159 GFP_KERNEL);
0160 if (!info)
0161 return -ENOMEM;
0162
0163 return pmbus_do_probe(client, info);
0164 }
0165
0166 static const struct i2c_device_id xdpe122_id[] = {
0167 {"xdpe11280", 0},
0168 {"xdpe12254", 0},
0169 {"xdpe12284", 0},
0170 {}
0171 };
0172
0173 MODULE_DEVICE_TABLE(i2c, xdpe122_id);
0174
0175 static const struct of_device_id __maybe_unused xdpe122_of_match[] = {
0176 {.compatible = "infineon,xdpe11280"},
0177 {.compatible = "infineon,xdpe12254"},
0178 {.compatible = "infineon,xdpe12284"},
0179 {}
0180 };
0181 MODULE_DEVICE_TABLE(of, xdpe122_of_match);
0182
0183 static struct i2c_driver xdpe122_driver = {
0184 .driver = {
0185 .name = "xdpe12284",
0186 .of_match_table = of_match_ptr(xdpe122_of_match),
0187 },
0188 .probe_new = xdpe122_probe,
0189 .id_table = xdpe122_id,
0190 };
0191
0192 module_i2c_driver(xdpe122_driver);
0193
0194 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
0195 MODULE_DESCRIPTION("PMBus driver for Infineon XDPE122 family");
0196 MODULE_LICENSE("GPL");
0197 MODULE_IMPORT_NS(PMBUS);