1
0
Fork 0
mirror of https://github.com/CTCaer/hekate.git synced 2024-11-22 18:06:40 +00:00

power: Refactor max17050 driver

This commit is contained in:
CTCaer 2020-06-14 02:03:43 +03:00
parent b0c4cef077
commit 416db840ec
2 changed files with 56 additions and 44 deletions

View file

@ -49,6 +49,15 @@ u32 max17050_get_cached_batt_volt()
return battery_voltage; return battery_voltage;
} }
static u16 max17050_get_reg(u8 reg)
{
u16 data = 0;
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, reg);
return data;
}
int max17050_get_property(enum MAX17050_reg reg, int *value) int max17050_get_property(enum MAX17050_reg reg, int *value)
{ {
u16 data; u16 data;
@ -56,67 +65,64 @@ int max17050_get_property(enum MAX17050_reg reg, int *value)
switch (reg) switch (reg)
{ {
case MAX17050_Age: // Age (percent). Based on 100% x (FullCAP Register/DesignCap). case MAX17050_Age: // Age (percent). Based on 100% x (FullCAP Register/DesignCap).
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_Age); data = max17050_get_reg(MAX17050_Age);
*value = data >> 8; /* Show MSB. 1% increments */ *value = data >> 8; /* Show MSB. 1% increments */
break; break;
case MAX17050_Cycles: // Cycle count. case MAX17050_Cycles: // Cycle count.
i2c_recv_buf_small((u8 *)value, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_Cycles); *value = max17050_get_reg(MAX17050_Cycles);
break; break;
case MAX17050_MinVolt: // Voltage max/min case MAX17050_MinVolt: // Voltage max/min
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_MinMaxVolt); data = max17050_get_reg(MAX17050_MinMaxVolt);
*value = (data & 0xff) * 20; /* Voltage MIN. Units of 20mV */ *value = (data & 0xff) * 20; /* Voltage MIN. Units of 20mV */
break; break;
case MAX17050_MaxVolt: // Voltage max/min case MAX17050_MaxVolt: // Voltage max/min
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_MinMaxVolt); data = max17050_get_reg(MAX17050_MinMaxVolt);
*value = (data >> 8) * 20; /* Voltage MAX. Units of LSB = 20mV */ *value = (data >> 8) * 20; /* Voltage MAX. Units of LSB = 20mV */
break; break;
case MAX17050_V_empty: // Voltage min design. case MAX17050_V_empty: // Voltage min design.
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_V_empty); data = max17050_get_reg(MAX17050_V_empty);
*value = (data >> 7) * 10; /* Units of LSB = 10mV */ *value = (data >> 7) * 10; /* Units of LSB = 10mV */
break; break;
case MAX17050_VCELL: // Voltage now. case MAX17050_VCELL: // Voltage now.
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_VCELL); data = max17050_get_reg(MAX17050_VCELL);
*value = data * 625 / 8 / 1000; *value = data * 625 / 8 / 1000;
battery_voltage = *value; battery_voltage = *value;
break; break;
case MAX17050_AvgVCELL: // Voltage avg. case MAX17050_AvgVCELL: // Voltage avg.
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_AvgVCELL); data = max17050_get_reg(MAX17050_AvgVCELL);
*value = data * 625 / 8 / 1000; *value = data * 625 / 8 / 1000;
break; break;
case MAX17050_OCVInternal: // Voltage ocv. case MAX17050_OCVInternal: // Voltage ocv.
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_OCVInternal); data = max17050_get_reg(MAX17050_OCVInternal);
*value = data * 625 / 8 / 1000; *value = data * 625 / 8 / 1000;
break; break;
case MAX17050_RepSOC: // Capacity %. case MAX17050_RepSOC: // Capacity %.
i2c_recv_buf_small((u8 *)value, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_RepSOC); *value = max17050_get_reg(MAX17050_RepSOC);
break; break;
case MAX17050_DesignCap: // Charge full design. case MAX17050_DesignCap: // Charge full design.
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_DesignCap); data = max17050_get_reg(MAX17050_DesignCap);
data = data * 5 / 10; *value = data * 5 / 10;
*value = data;
break; break;
case MAX17050_FullCAP: // Charge full. case MAX17050_FullCAP: // Charge full.
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_FullCAP); data = max17050_get_reg(MAX17050_FullCAP);
data = data * 5 / 10; *value = data * 5 / 10;
*value = data;
break; break;
case MAX17050_RepCap: // Charge now. case MAX17050_RepCap: // Charge now.
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_RepCap); data = max17050_get_reg(MAX17050_RepCap);
data = data * 5 / 10; *value = data * 5 / 10;
*value = data;
break; break;
case MAX17050_TEMP: // Temp. case MAX17050_TEMP: // Temp.
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_TEMP); data = max17050_get_reg(MAX17050_TEMP);
*value = (s16)data; *value = (s16)data;
*value = *value * 10 / 256; *value = *value * 10 / 256;
break; break;
case MAX17050_Current: // Current now. case MAX17050_Current: // Current now.
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_Current); data = max17050_get_reg(MAX17050_Current);
*value = (s16)data; *value = (s16)data;
*value *= 1562500 / MAX17050_DEFAULT_SNS_RESISTOR; *value *= 1562500 / MAX17050_DEFAULT_SNS_RESISTOR;
break; break;
case MAX17050_AvgCurrent: // Current avg. case MAX17050_AvgCurrent: // Current avg.
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_AvgCurrent); data = max17050_get_reg(MAX17050_AvgCurrent);
*value = (s16)data; *value = (s16)data;
*value *= 1562500 / MAX17050_DEFAULT_SNS_RESISTOR; *value *= 1562500 / MAX17050_DEFAULT_SNS_RESISTOR;
break; break;
@ -135,7 +141,7 @@ static int _max17050_write_verify_reg(u8 reg, u16 value)
do do
{ {
ret = i2c_send_buf_small(I2C_1, MAXIM17050_I2C_ADDR, reg, (u8 *)&value, 2); ret = i2c_send_buf_small(I2C_1, MAXIM17050_I2C_ADDR, reg, (u8 *)&value, 2);
i2c_recv_buf_small((u8 *)&read_value, 2, I2C_1, MAXIM17050_I2C_ADDR, reg); read_value = max17050_get_reg(reg);
if (read_value != value) if (read_value != value)
{ {
ret = -1; ret = -1;

View file

@ -49,6 +49,15 @@ u32 max17050_get_cached_batt_volt()
return battery_voltage; return battery_voltage;
} }
static u16 max17050_get_reg(u8 reg)
{
u16 data = 0;
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, reg);
return data;
}
int max17050_get_property(enum MAX17050_reg reg, int *value) int max17050_get_property(enum MAX17050_reg reg, int *value)
{ {
u16 data; u16 data;
@ -56,67 +65,64 @@ int max17050_get_property(enum MAX17050_reg reg, int *value)
switch (reg) switch (reg)
{ {
case MAX17050_Age: // Age (percent). Based on 100% x (FullCAP Register/DesignCap). case MAX17050_Age: // Age (percent). Based on 100% x (FullCAP Register/DesignCap).
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_Age); data = max17050_get_reg(MAX17050_Age);
*value = data >> 8; /* Show MSB. 1% increments */ *value = data >> 8; /* Show MSB. 1% increments */
break; break;
case MAX17050_Cycles: // Cycle count. case MAX17050_Cycles: // Cycle count.
i2c_recv_buf_small((u8 *)value, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_Cycles); *value = max17050_get_reg(MAX17050_Cycles);
break; break;
case MAX17050_MinVolt: // Voltage max/min case MAX17050_MinVolt: // Voltage max/min
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_MinMaxVolt); data = max17050_get_reg(MAX17050_MinMaxVolt);
*value = (data & 0xff) * 20; /* Voltage MIN. Units of 20mV */ *value = (data & 0xff) * 20; /* Voltage MIN. Units of 20mV */
break; break;
case MAX17050_MaxVolt: // Voltage max/min case MAX17050_MaxVolt: // Voltage max/min
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_MinMaxVolt); data = max17050_get_reg(MAX17050_MinMaxVolt);
*value = (data >> 8) * 20; /* Voltage MAX. Units of LSB = 20mV */ *value = (data >> 8) * 20; /* Voltage MAX. Units of LSB = 20mV */
break; break;
case MAX17050_V_empty: // Voltage min design. case MAX17050_V_empty: // Voltage min design.
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_V_empty); data = max17050_get_reg(MAX17050_V_empty);
*value = (data >> 7) * 10; /* Units of LSB = 10mV */ *value = (data >> 7) * 10; /* Units of LSB = 10mV */
break; break;
case MAX17050_VCELL: // Voltage now. case MAX17050_VCELL: // Voltage now.
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_VCELL); data = max17050_get_reg(MAX17050_VCELL);
*value = data * 625 / 8 / 1000; *value = data * 625 / 8 / 1000;
battery_voltage = *value; battery_voltage = *value;
break; break;
case MAX17050_AvgVCELL: // Voltage avg. case MAX17050_AvgVCELL: // Voltage avg.
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_AvgVCELL); data = max17050_get_reg(MAX17050_AvgVCELL);
*value = data * 625 / 8 / 1000; *value = data * 625 / 8 / 1000;
break; break;
case MAX17050_OCVInternal: // Voltage ocv. case MAX17050_OCVInternal: // Voltage ocv.
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_OCVInternal); data = max17050_get_reg(MAX17050_OCVInternal);
*value = data * 625 / 8 / 1000; *value = data * 625 / 8 / 1000;
break; break;
case MAX17050_RepSOC: // Capacity %. case MAX17050_RepSOC: // Capacity %.
i2c_recv_buf_small((u8 *)value, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_RepSOC); *value = max17050_get_reg(MAX17050_RepSOC);
break; break;
case MAX17050_DesignCap: // Charge full design. case MAX17050_DesignCap: // Charge full design.
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_DesignCap); data = max17050_get_reg(MAX17050_DesignCap);
data = data * 5 / 10; *value = data * 5 / 10;
*value = data;
break; break;
case MAX17050_FullCAP: // Charge full. case MAX17050_FullCAP: // Charge full.
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_FullCAP); data = max17050_get_reg(MAX17050_FullCAP);
data = data * 5 / 10; *value = data * 5 / 10;
*value = data;
break; break;
case MAX17050_RepCap: // Charge now. case MAX17050_RepCap: // Charge now.
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_RepCap); data = max17050_get_reg(MAX17050_RepCap);
data = data * 5 / 10; *value = data * 5 / 10;
*value = data;
break; break;
case MAX17050_TEMP: // Temp. case MAX17050_TEMP: // Temp.
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_TEMP); data = max17050_get_reg(MAX17050_TEMP);
*value = (s16)data; *value = (s16)data;
*value = *value * 10 / 256; *value = *value * 10 / 256;
break; break;
case MAX17050_Current: // Current now. case MAX17050_Current: // Current now.
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_Current); data = max17050_get_reg(MAX17050_Current);
*value = (s16)data; *value = (s16)data;
*value *= 1562500 / MAX17050_DEFAULT_SNS_RESISTOR; *value *= 1562500 / MAX17050_DEFAULT_SNS_RESISTOR;
break; break;
case MAX17050_AvgCurrent: // Current avg. case MAX17050_AvgCurrent: // Current avg.
i2c_recv_buf_small((u8 *)&data, 2, I2C_1, MAXIM17050_I2C_ADDR, MAX17050_AvgCurrent); data = max17050_get_reg(MAX17050_AvgCurrent);
*value = (s16)data; *value = (s16)data;
*value *= 1562500 / MAX17050_DEFAULT_SNS_RESISTOR; *value *= 1562500 / MAX17050_DEFAULT_SNS_RESISTOR;
break; break;
@ -135,7 +141,7 @@ static int _max17050_write_verify_reg(u8 reg, u16 value)
do do
{ {
ret = i2c_send_buf_small(I2C_1, MAXIM17050_I2C_ADDR, reg, (u8 *)&value, 2); ret = i2c_send_buf_small(I2C_1, MAXIM17050_I2C_ADDR, reg, (u8 *)&value, 2);
i2c_recv_buf_small((u8 *)&read_value, 2, I2C_1, MAXIM17050_I2C_ADDR, reg); read_value = max17050_get_reg(reg);
if (read_value != value) if (read_value != value)
{ {
ret = -1; ret = -1;