Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
277 changes: 35 additions & 242 deletions drivers/hwmon/pmbus/mp2869.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define MP2869_MFR_VOUT_SCALE_LOOP 0x29
#define MP2869_IIN_SCALE_BIT_R1 0x53
#define MP2869_IOUT_SCALE_BIT 0x67
#define MP2869_MFR_PWR_RPT_R1 0x68
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is this new parameter is used?

#define MP2869_MFR_VOUT_LOOP_CTRL_R2 0xBD

#define MP2869_MAX_PHASE_RAIL1 16
Expand Down Expand Up @@ -46,6 +47,7 @@ struct mp2869_data {
int vout_gain[MP2869_PAGE_NUM];
int curr_iin_gain[MP2869_PAGE_NUM];
int curr_iout_gain[MP2869_PAGE_NUM];
int power_gain[MP2869_PAGE_NUM];
int max_phases[MP2869_PAGE_NUM];
enum chips chip_id;
};
Expand All @@ -54,79 +56,7 @@ struct mp2869_data {
#define MAX_LIN_MANTISSA (1023 * 1000)
#define MIN_LIN_MANTISSA (511 * 1000)

/*
static int bit_x_get(int val,int high,int low )
{
int tmp_num;
int bit_num = high-low+1;

int tmp_val = val & GENMASK(high,low);

//zsj add
printk("val =%d,tmp_val =%d\n",val,tmp_val);

tmp_val >= low;
//zsj add
printk("tmp_val >=low(%d) = %d\n",tmp_val,low);

tmp_num = 2<<(bit_num-1);
if(tmp_val > = tmp_num)
{
tmp_num = 2<<bit_num;
tmp_val -= tmp_num;
}

//zsj add
printk("bit_x_get =(%d)\n",tmp_val);

return tmp_val;
}

static u16 val2linear11(s64 val)
{
s16 exponent = 0, mantissa;
bool negative = false;

if (val == 0)
return 0;

if (val < 0) {
negative = true;
val = -val;
}

// Reduce large mantissa until it fits into 10 bit
while (val >= MAX_LIN_MANTISSA && exponent < 15) {
exponent++;
val >>= 1;
}
// Increase small mantissa to improve precision
while (val < MIN_LIN_MANTISSA && exponent > -15) {
exponent--;
val <<= 1;
}

// Convert mantissa from milli-units to units
mantissa = clamp_val(DIV_ROUND_CLOSEST_ULL(val, 1000), 0, 0x3ff);

// restore sign
if (negative)
mantissa = -mantissa;

// Convert to 5 bit exponent, 11 bit mantissa
return (mantissa & 0x7ff) | ((exponent << 11) & 0xf800);
}

static int
mp2869_read_word_helper(struct i2c_client *client, int page, int phase, u8 reg,
u16 mask)
{
int ret = pmbus_read_word_data(client, page, phase, reg);

return (ret > 0) ? ret & mask : ret;
}

*/

// some values are SMBus LINEAR11 data which need a conversion
static int corsairpsu_linear11_to_int(int val)
Expand All @@ -147,48 +77,12 @@ mp2869_read_vout(struct i2c_client *client, struct mp2869_data *data, int page,
ret = pmbus_read_word_data(client, page, phase, reg);

/* convert vout result to direct format */

if(page == 0)
{
ret = (ret*1000) / data->vout_gain[page];
}
else if(page == 1)
{
ret *= data->vout_gain[page];
}
ret = (ret*1000) / data->vout_gain[page];

return ret;
}

static int
mp2869_read_iout(struct i2c_client *client, struct mp2869_data *data, int page,
int phase, u8 reg)
{
int ret;

ret = pmbus_read_word_data(client, page, phase, reg);

ret = corsairpsu_linear11_to_int(ret);

/* convert vout result to direct format */
ret = (ret*1000) / data->curr_iout_gain[page];

return ret;
}

static int
mp2869_read_iin(struct i2c_client *client, struct mp2869_data *data, int page,
int phase, u8 reg)
{
int ret;

ret = pmbus_read_word_data(client, page, phase, reg);

/* convert vout result to direct format */
ret = (ret*1000) / data->curr_iin_gain[page];

return ret;
}

static int
mp2869_read_word_data(struct i2c_client *client, int page,
Expand All @@ -202,12 +96,7 @@ mp2869_read_word_data(struct i2c_client *client, int page,
case PMBUS_READ_VOUT:
ret = mp2869_read_vout(client, data, page, phase, reg);
break;
case PMBUS_READ_IOUT:
ret = mp2869_read_iout(client, data, page, phase, reg);
break;
case PMBUS_READ_IIN:
ret = mp2869_read_iin(client, data, page, phase, reg);
break;

default:
return -ENODATA;
}
Expand All @@ -227,134 +116,63 @@ mp2869_read_byte_data(struct i2c_client *client, int page, int reg)
}
}



static int
mp2869_current_iin_gain_get(struct i2c_client *client,
mp2869_voltage_vout_gain_get(struct i2c_client *client,
struct mp2869_data *data)
{
int curr_gain, ret;

//Curr gain IIN1
int vout_gain, ret;
//Voltage gain VOUT1
ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 0);
if(ret < 0)
return ret;

ret = i2c_smbus_read_word_data(client,MP2869_IIN_SCALE_BIT_R1);

printk("mp2869_current_iin_gain_get MP2869_IIN_SCALE_BIT_R1 ret =%d\n",ret);
//IIN1 scale bit is 53h page0 bit[10:8]
ret = ret & GENMASK(10,8);
ret = ret >> 8;
ret = i2c_smbus_read_word_data(client,MP2869_MFR_VOUT_SCALE_LOOP);
printk("mp2869_voltage_vout_gain_get MP2869_MFR_VOUT_SCALE_LOOP ret =%d\n",ret);

//VOUT1 scale bit is 29h page0 bit[12:10]
ret = ret & GENMASK(12,10);
ret = ret >> 10;

switch (ret) {
case 0:
curr_gain = 8;
vout_gain = 160;
break;
case 1:
curr_gain = 256;
vout_gain = 200;
break;
case 2:
curr_gain = 128;
vout_gain = 400;
break;
case 3:
curr_gain = 64;
vout_gain = 500;
break;
case 4:
curr_gain = 32;
vout_gain = 1000;
break;
case 5:
curr_gain = 16;
vout_gain = 256;
break;
case 6:
curr_gain = 8;
vout_gain = 512;
break;
case 7:
curr_gain = 4;
break;
default:
printk("error get iin_gain in MP2869_IIN_SCALE_BIT_R1\n");
break;
}

data->curr_iin_gain[0] = curr_gain;
data->curr_iin_gain[0] = 1;
//zsjadd test
for(int i=0;i <2;i++)
{
printk("data->curr_iin_gain[%d]=%d\n",i,data->curr_iin_gain[i]);
}

return 0;
}


static int
mp2869_current_iout_gain_get(struct i2c_client *client,
struct mp2869_data *data)
{
int curr_gain, ret;

//Curr gain IOUT1
ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 0);
if(ret < 0)
return ret;

//IOUT1 scale bit is 67h page0 bit[2:0]
ret = i2c_smbus_read_word_data(client,MP2869_IOUT_SCALE_BIT);

ret = ret & GENMASK(2,0);

switch (ret) {
case 0:
curr_gain = 1;
break;
case 1:
curr_gain = 32;
break;
case 2:
curr_gain = 16;
break;
case 3:
curr_gain = 8;
break;
case 4:
curr_gain = 4;
break;
case 5:
curr_gain = 2;
vout_gain = 1024;
break;
case 6:
curr_gain = 1;
break;
default:
printk("error get iin_gain in MP2869_IIN_SCALE_BIT_R1\n");
printk("error get vout_gain in MP2869_MFR_VOUT_SCALE_LOOP\n");
break;
}
data->vout_gain[0] = vout_gain;


data->curr_iout_gain[0] = curr_gain;

data->curr_iout_gain[1] =1;
//zsjadd test
for(int i=0;i <2;i++)
{
printk("data->curr_iout_gain[%d]=%d\n",i,data->curr_iout_gain[i]);
}

return 0;
}

static int
mp2869_voltage_vout_gain_get(struct i2c_client *client,
struct mp2869_data *data)
{
int vout_gain, ret;
//Voltage gain VOUT1
ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 0);
//Voltage gain VOUT2
ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 1);
if(ret < 0)
return ret;
return ret;

ret = i2c_smbus_read_word_data(client,MP2869_MFR_VOUT_SCALE_LOOP);
printk("mp2869_voltage_vout_gain_get MP2869_MFR_VOUT_SCALE_LOOP ret =%d\n",ret);
printk("mp2869_voltage_vout_gain_get MP2869_MFR_VOUT_SCALE_LOOP_R2 ret =%d\n",ret);

//VOUT1 scale bit is 29h page0 bit[12:10]
ret = ret & GENMASK(12,10);
Expand Down Expand Up @@ -386,23 +204,9 @@ mp2869_voltage_vout_gain_get(struct i2c_client *client,
vout_gain = 1024;
break;
default:
printk("error get iin_gain in MP2869_MFR_VOUT_SCALE_LOOP\n");
printk("error get vout_gain in MP2869_MFR_VOUT_SCALE_LOOP\n");
break;
}
data->vout_gain[0] = vout_gain;

//Voltage gain VOUT2
ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 2);
if(ret < 0)
return ret;

ret = i2c_smbus_read_word_data(client,MP2869_MFR_VOUT_LOOP_CTRL_R2);
printk("mp2869_voltage_vout_gain_get MP2869_MFR_VOUT_LOOP_CTRL_R2 ret =%d\n",ret);

//VOUT2 scale bit is BDh page2 bit[15:14]
vout_gain = ret & GENMASK(15,14);
vout_gain = vout_gain>>14;

data->vout_gain[1] = vout_gain;

//zsjadd test
Expand All @@ -421,8 +225,8 @@ static struct pmbus_driver_info mp2869_info = {
.format[PSC_VOLTAGE_IN] = linear,
.format[PSC_VOLTAGE_OUT] = direct,
.format[PSC_TEMPERATURE] = linear,
.format[PSC_CURRENT_IN] = direct,
.format[PSC_CURRENT_OUT] = direct,
.format[PSC_CURRENT_IN] = linear,
.format[PSC_CURRENT_OUT] = linear,
.format[PSC_POWER] = linear,
.m[PSC_VOLTAGE_OUT] = 1,
.R[PSC_VOLTAGE_OUT] = 3,
Expand All @@ -432,8 +236,7 @@ static struct pmbus_driver_info mp2869_info = {
PMBUS_HAVE_PIN ,
.func[1] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT |
PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT |
PMBUS_HAVE_TEMP | PMBUS_HAVE_POUT |
PMBUS_HAVE_PIN ,
PMBUS_HAVE_TEMP | PMBUS_HAVE_POUT ,
.read_byte_data = mp2869_read_byte_data,
.read_word_data = mp2869_read_word_data,
};
Expand All @@ -457,20 +260,10 @@ static int mp2869_probe(struct i2c_client *client)
memcpy(&data->info, &mp2869_info, sizeof(*info));
info = &data->info;

/* Get IIN current stage. */
ret = mp2869_current_iin_gain_get(client, data);
if (ret)
return ret;

/* Get IOUT current stage. */
ret = mp2869_current_iout_gain_get(client, data);
if (ret)
return ret;

/* Get Vout stage. */
ret = mp2869_voltage_vout_gain_get(client, data);
if (ret)
return ret;
return ret;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: please remove the extra space


i2c_smbus_write_byte_data(client, PMBUS_PAGE, 0);

Expand Down Expand Up @@ -498,4 +291,4 @@ module_i2c_driver(mp2869_driver);
MODULE_AUTHOR("Shaojie Zhang");
MODULE_DESCRIPTION("PMBus driver for MPS MP2869/MP29608 device");
MODULE_LICENSE("GPL");
MODULE_IMPORT_NS(PMBUS);
MODULE_IMPORT_NS(PMBUS);