262 lines · c
1// SPDX-License-Identifier: GPL-2.0-or-later2/*3 * Hardware monitoring driver for MPS Multi-phase Digital VR Controllers(MP2993)4 */5 6#include <linux/i2c.h>7#include <linux/module.h>8#include <linux/of_device.h>9#include "pmbus.h"10 11#define MP2993_VOUT_OVUV_UINT 12512#define MP2993_VOUT_OVUV_DIV 6413#define MP2993_VIN_LIMIT_UINT 114#define MP2993_VIN_LIMIT_DIV 815#define MP2993_READ_VIN_UINT 116#define MP2993_READ_VIN_DIV 3217 18#define MP2993_PAGE_NUM 219 20#define MP2993_RAIL1_FUNC (PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT | \21 PMBUS_HAVE_IOUT | PMBUS_HAVE_POUT | \22 PMBUS_HAVE_TEMP | PMBUS_HAVE_PIN | \23 PMBUS_HAVE_IIN | \24 PMBUS_HAVE_STATUS_VOUT | \25 PMBUS_HAVE_STATUS_IOUT | \26 PMBUS_HAVE_STATUS_TEMP | \27 PMBUS_HAVE_STATUS_INPUT)28 29#define MP2993_RAIL2_FUNC (PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT | \30 PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP | \31 PMBUS_HAVE_STATUS_VOUT | \32 PMBUS_HAVE_STATUS_IOUT | \33 PMBUS_HAVE_STATUS_TEMP | \34 PMBUS_HAVE_STATUS_INPUT)35 36/* Converts a linear11 data exponent to a specified value */37static u16 mp2993_linear11_exponent_transfer(u16 word, u16 expect_exponent)38{39 s16 exponent, mantissa, target_exponent;40 41 exponent = ((s16)word) >> 11;42 mantissa = ((s16)((word & 0x7ff) << 5)) >> 5;43 target_exponent = (s16)((expect_exponent & 0x1f) << 11) >> 11;44 45 if (exponent > target_exponent)46 mantissa = mantissa << (exponent - target_exponent);47 else48 mantissa = mantissa >> (target_exponent - exponent);49 50 return (mantissa & 0x7ff) | ((expect_exponent << 11) & 0xf800);51}52 53static int54mp2993_set_vout_format(struct i2c_client *client, int page, int format)55{56 int ret;57 58 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, page);59 if (ret < 0)60 return ret;61 62 return i2c_smbus_write_byte_data(client, PMBUS_VOUT_MODE, format);63}64 65static int mp2993_identify(struct i2c_client *client, struct pmbus_driver_info *info)66{67 int ret;68 69 /* Set vout to direct format for rail1. */70 ret = mp2993_set_vout_format(client, 0, PB_VOUT_MODE_DIRECT);71 if (ret < 0)72 return ret;73 74 /* Set vout to direct format for rail2. */75 return mp2993_set_vout_format(client, 1, PB_VOUT_MODE_DIRECT);76}77 78static int mp2993_read_word_data(struct i2c_client *client, int page, int phase,79 int reg)80{81 int ret;82 83 switch (reg) {84 case PMBUS_VOUT_OV_FAULT_LIMIT:85 case PMBUS_VOUT_UV_FAULT_LIMIT:86 ret = pmbus_read_word_data(client, page, phase, reg);87 if (ret < 0)88 return ret;89 90 ret = DIV_ROUND_CLOSEST(ret * MP2993_VOUT_OVUV_UINT, MP2993_VOUT_OVUV_DIV);91 break;92 case PMBUS_OT_FAULT_LIMIT:93 case PMBUS_OT_WARN_LIMIT:94 /*95 * The MP2993 ot fault limit value and ot warn limit value96 * per rail are always the same, so only PMBUS_OT_FAULT_LIMIT97 * and PMBUS_OT_WARN_LIMIT register in page 0 are defined to98 * indicates the limit value.99 */100 ret = pmbus_read_word_data(client, 0, phase, reg);101 break;102 case PMBUS_READ_VIN:103 /* The MP2993 vin scale is (1/32V)/Lsb */104 ret = pmbus_read_word_data(client, page, phase, reg);105 if (ret < 0)106 return ret;107 108 ret = DIV_ROUND_CLOSEST((ret & GENMASK(9, 0)) * MP2993_READ_VIN_UINT,109 MP2993_READ_VIN_DIV);110 break;111 case PMBUS_VIN_OV_FAULT_LIMIT:112 case PMBUS_VIN_OV_WARN_LIMIT:113 case PMBUS_VIN_UV_WARN_LIMIT:114 case PMBUS_VIN_UV_FAULT_LIMIT:115 /* The MP2993 vin limit scale is (1/8V)/Lsb */116 ret = pmbus_read_word_data(client, page, phase, reg);117 if (ret < 0)118 return ret;119 120 ret = DIV_ROUND_CLOSEST((ret & GENMASK(7, 0)) * MP2993_VIN_LIMIT_UINT,121 MP2993_VIN_LIMIT_DIV);122 break;123 case PMBUS_READ_IOUT:124 case PMBUS_READ_IIN:125 case PMBUS_IIN_OC_WARN_LIMIT:126 case PMBUS_IOUT_OC_FAULT_LIMIT:127 case PMBUS_IOUT_OC_WARN_LIMIT:128 case PMBUS_READ_VOUT:129 case PMBUS_READ_PIN:130 case PMBUS_READ_POUT:131 case PMBUS_READ_TEMPERATURE_1:132 ret = -ENODATA;133 break;134 default:135 ret = -EINVAL;136 break;137 }138 139 return ret;140}141 142static int mp2993_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_VOUT_OV_FAULT_LIMIT:149 case PMBUS_VOUT_UV_FAULT_LIMIT:150 ret = DIV_ROUND_CLOSEST(word * MP2993_VOUT_OVUV_DIV, MP2993_VOUT_OVUV_UINT);151 ret = pmbus_write_word_data(client, 0, reg, ret);152 break;153 case PMBUS_OT_FAULT_LIMIT:154 case PMBUS_OT_WARN_LIMIT:155 /*156 * The MP2993 ot fault limit value and ot warn limit value157 * per rail are always the same, so only PMBUS_OT_FAULT_LIMIT158 * and PMBUS_OT_WARN_LIMIT register in page 0 are defined to159 * config the ot limit value.160 */161 ret = pmbus_write_word_data(client, 0, reg, word);162 break;163 case PMBUS_VIN_OV_FAULT_LIMIT:164 case PMBUS_VIN_OV_WARN_LIMIT:165 case PMBUS_VIN_UV_WARN_LIMIT:166 case PMBUS_VIN_UV_FAULT_LIMIT:167 /* The MP2993 vin limit scale is (1/8V)/Lsb */168 ret = pmbus_write_word_data(client, 0, reg,169 DIV_ROUND_CLOSEST(word * MP2993_VIN_LIMIT_DIV,170 MP2993_VIN_LIMIT_UINT));171 break;172 case PMBUS_IIN_OC_WARN_LIMIT:173 /*174 * The PMBUS_IIN_OC_WARN_LIMIT of MP2993 is linear11 format,175 * and the exponent is a constant value(5'b00000), so the176 * exponent of word parameter should be converted to 5'b00000.177 */178 ret = pmbus_write_word_data(client, page, reg,179 mp2993_linear11_exponent_transfer(word, 0x00));180 break;181 //182 case PMBUS_IOUT_OC_FAULT_LIMIT:183 case PMBUS_IOUT_OC_WARN_LIMIT:184 /*185 * The PMBUS_IOUT_OC_FAULT_LIMIT and PMBUS_IOUT_OC_WARN_LIMIT186 * of MP2993 can be regarded as linear11 format, and the187 * exponent is a 5'b00001 or 5'b00000. To ensure a larger188 * range of limit value, so the exponent of word parameter189 * should be converted to 5'b00001.190 */191 ret = pmbus_write_word_data(client, page, reg,192 mp2993_linear11_exponent_transfer(word, 0x01));193 break;194 default:195 ret = -EINVAL;196 break;197 }198 199 return ret;200}201 202static struct pmbus_driver_info mp2993_info = {203 .pages = MP2993_PAGE_NUM,204 .format[PSC_VOLTAGE_IN] = direct,205 .format[PSC_CURRENT_IN] = linear,206 .format[PSC_CURRENT_OUT] = linear,207 .format[PSC_TEMPERATURE] = direct,208 .format[PSC_POWER] = linear,209 .format[PSC_VOLTAGE_OUT] = direct,210 211 .m[PSC_VOLTAGE_OUT] = 1,212 .R[PSC_VOLTAGE_OUT] = 3,213 .b[PSC_VOLTAGE_OUT] = 0,214 215 .m[PSC_VOLTAGE_IN] = 1,216 .R[PSC_VOLTAGE_IN] = 0,217 .b[PSC_VOLTAGE_IN] = 0,218 219 .m[PSC_TEMPERATURE] = 1,220 .R[PSC_TEMPERATURE] = 0,221 .b[PSC_TEMPERATURE] = 0,222 223 .func[0] = MP2993_RAIL1_FUNC,224 .func[1] = MP2993_RAIL2_FUNC,225 .read_word_data = mp2993_read_word_data,226 .write_word_data = mp2993_write_word_data,227 .identify = mp2993_identify,228};229 230static int mp2993_probe(struct i2c_client *client)231{232 return pmbus_do_probe(client, &mp2993_info);233}234 235static const struct i2c_device_id mp2993_id[] = {236 {"mp2993", 0},237 {}238};239MODULE_DEVICE_TABLE(i2c, mp2993_id);240 241static const struct of_device_id __maybe_unused mp2993_of_match[] = {242 {.compatible = "mps,mp2993"},243 {}244};245MODULE_DEVICE_TABLE(of, mp2993_of_match);246 247static struct i2c_driver mp2993_driver = {248 .driver = {249 .name = "mp2993",250 .of_match_table = mp2993_of_match,251 },252 .probe = mp2993_probe,253 .id_table = mp2993_id,254};255 256module_i2c_driver(mp2993_driver);257 258MODULE_AUTHOR("Noah Wang <noahwang.wang@outlook.com>");259MODULE_DESCRIPTION("PMBus driver for MPS MP2993");260MODULE_LICENSE("GPL");261MODULE_IMPORT_NS(PMBUS);262