aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/power/max17042_battery.c
diff options
context:
space:
mode:
authorDonggeun Kim <dg77.kim@samsung.com>2011-06-30 05:07:41 -0400
committerAnton Vorontsov <cbouatmailru@gmail.com>2011-07-08 09:01:58 -0400
commit086ef502c202503a19a491838098157cdab6238e (patch)
treed43b45b7572123ef908d5df55495a854e339db49 /drivers/power/max17042_battery.c
parent464f29a21792e0e506d936d147beea72a5f8e904 (diff)
power_supply: MAX17042: Support additional properties
This patch supports additional properties (PRESENT, CYCLE_COUNT, VOLTAGE_MAX, VOLTAGE_MIN_DESIGN, CURRENT_NOW, CURRENT_AVG, CHARGE_FULL, and TEMP). Plus, initialization code for registers is added. Signed-off-by: Donggeun Kim <dg77.kim@samsung.com> Signed-off-by: MyungJoo Ham <myungjoo.ham@samsung.com> Signed-off-by: KyungMin Park <kyungmin.park@samsung.com> Signed-off-by: Anton Vorontsov <cbouatmailru@gmail.com>
Diffstat (limited to 'drivers/power/max17042_battery.c')
-rw-r--r--drivers/power/max17042_battery.c173
1 files changed, 105 insertions, 68 deletions
diff --git a/drivers/power/max17042_battery.c b/drivers/power/max17042_battery.c
index 09f7496a6d8c..98bfab35b8e9 100644
--- a/drivers/power/max17042_battery.c
+++ b/drivers/power/max17042_battery.c
@@ -29,74 +29,6 @@
29#include <linux/power_supply.h> 29#include <linux/power_supply.h>
30#include <linux/power/max17042_battery.h> 30#include <linux/power/max17042_battery.h>
31 31
32enum max17042_register {
33 MAX17042_STATUS = 0x00,
34 MAX17042_VALRT_Th = 0x01,
35 MAX17042_TALRT_Th = 0x02,
36 MAX17042_SALRT_Th = 0x03,
37 MAX17042_AtRate = 0x04,
38 MAX17042_RepCap = 0x05,
39 MAX17042_RepSOC = 0x06,
40 MAX17042_Age = 0x07,
41 MAX17042_TEMP = 0x08,
42 MAX17042_VCELL = 0x09,
43 MAX17042_Current = 0x0A,
44 MAX17042_AvgCurrent = 0x0B,
45 MAX17042_Qresidual = 0x0C,
46 MAX17042_SOC = 0x0D,
47 MAX17042_AvSOC = 0x0E,
48 MAX17042_RemCap = 0x0F,
49 MAX17402_FullCAP = 0x10,
50 MAX17042_TTE = 0x11,
51 MAX17042_V_empty = 0x12,
52
53 MAX17042_RSLOW = 0x14,
54
55 MAX17042_AvgTA = 0x16,
56 MAX17042_Cycles = 0x17,
57 MAX17042_DesignCap = 0x18,
58 MAX17042_AvgVCELL = 0x19,
59 MAX17042_MinMaxTemp = 0x1A,
60 MAX17042_MinMaxVolt = 0x1B,
61 MAX17042_MinMaxCurr = 0x1C,
62 MAX17042_CONFIG = 0x1D,
63 MAX17042_ICHGTerm = 0x1E,
64 MAX17042_AvCap = 0x1F,
65 MAX17042_ManName = 0x20,
66 MAX17042_DevName = 0x21,
67 MAX17042_DevChem = 0x22,
68
69 MAX17042_TempNom = 0x24,
70 MAX17042_TempCold = 0x25,
71 MAX17042_TempHot = 0x26,
72 MAX17042_AIN = 0x27,
73 MAX17042_LearnCFG = 0x28,
74 MAX17042_SHFTCFG = 0x29,
75 MAX17042_RelaxCFG = 0x2A,
76 MAX17042_MiscCFG = 0x2B,
77 MAX17042_TGAIN = 0x2C,
78 MAx17042_TOFF = 0x2D,
79 MAX17042_CGAIN = 0x2E,
80 MAX17042_COFF = 0x2F,
81
82 MAX17042_Q_empty = 0x33,
83 MAX17042_T_empty = 0x34,
84
85 MAX17042_RCOMP0 = 0x38,
86 MAX17042_TempCo = 0x39,
87 MAX17042_Rx = 0x3A,
88 MAX17042_T_empty0 = 0x3B,
89 MAX17042_TaskPeriod = 0x3C,
90 MAX17042_FSTAT = 0x3D,
91
92 MAX17042_SHDNTIMER = 0x3F,
93
94 MAX17042_VFRemCap = 0x4A,
95
96 MAX17042_QH = 0x4D,
97 MAX17042_QL = 0x4E,
98};
99
100struct max17042_chip { 32struct max17042_chip {
101 struct i2c_client *client; 33 struct i2c_client *client;
102 struct power_supply battery; 34 struct power_supply battery;
@@ -123,10 +55,27 @@ static int max17042_read_reg(struct i2c_client *client, u8 reg)
123 return ret; 55 return ret;
124} 56}
125 57
58static void max17042_set_reg(struct i2c_client *client,
59 struct max17042_reg_data *data, int size)
60{
61 int i;
62
63 for (i = 0; i < size; i++)
64 max17042_write_reg(client, data[i].addr, data[i].data);
65}
66
126static enum power_supply_property max17042_battery_props[] = { 67static enum power_supply_property max17042_battery_props[] = {
68 POWER_SUPPLY_PROP_PRESENT,
69 POWER_SUPPLY_PROP_CYCLE_COUNT,
70 POWER_SUPPLY_PROP_VOLTAGE_MAX,
71 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
127 POWER_SUPPLY_PROP_VOLTAGE_NOW, 72 POWER_SUPPLY_PROP_VOLTAGE_NOW,
128 POWER_SUPPLY_PROP_VOLTAGE_AVG, 73 POWER_SUPPLY_PROP_VOLTAGE_AVG,
129 POWER_SUPPLY_PROP_CAPACITY, 74 POWER_SUPPLY_PROP_CAPACITY,
75 POWER_SUPPLY_PROP_CHARGE_FULL,
76 POWER_SUPPLY_PROP_TEMP,
77 POWER_SUPPLY_PROP_CURRENT_NOW,
78 POWER_SUPPLY_PROP_CURRENT_AVG,
130}; 79};
131 80
132static int max17042_get_property(struct power_supply *psy, 81static int max17042_get_property(struct power_supply *psy,
@@ -137,6 +86,30 @@ static int max17042_get_property(struct power_supply *psy,
137 struct max17042_chip, battery); 86 struct max17042_chip, battery);
138 87
139 switch (psp) { 88 switch (psp) {
89 case POWER_SUPPLY_PROP_PRESENT:
90 val->intval = max17042_read_reg(chip->client,
91 MAX17042_STATUS);
92 if (val->intval & MAX17042_STATUS_BattAbsent)
93 val->intval = 0;
94 else
95 val->intval = 1;
96 break;
97 case POWER_SUPPLY_PROP_CYCLE_COUNT:
98 val->intval = max17042_read_reg(chip->client,
99 MAX17042_Cycles);
100 break;
101 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
102 val->intval = max17042_read_reg(chip->client,
103 MAX17042_MinMaxVolt);
104 val->intval >>= 8;
105 val->intval *= 20000; /* Units of LSB = 20mV */
106 break;
107 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
108 val->intval = max17042_read_reg(chip->client,
109 MAX17042_V_empty);
110 val->intval >>= 7;
111 val->intval *= 10000; /* Units of LSB = 10mV */
112 break;
140 case POWER_SUPPLY_PROP_VOLTAGE_NOW: 113 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
141 val->intval = max17042_read_reg(chip->client, 114 val->intval = max17042_read_reg(chip->client,
142 MAX17042_VCELL) * 83; /* 1000 / 12 = 83 */ 115 MAX17042_VCELL) * 83; /* 1000 / 12 = 83 */
@@ -149,6 +122,57 @@ static int max17042_get_property(struct power_supply *psy,
149 val->intval = max17042_read_reg(chip->client, 122 val->intval = max17042_read_reg(chip->client,
150 MAX17042_SOC) / 256; 123 MAX17042_SOC) / 256;
151 break; 124 break;
125 case POWER_SUPPLY_PROP_CHARGE_FULL:
126 val->intval = max17042_read_reg(chip->client,
127 MAX17042_RepSOC);
128 if ((val->intval / 256) >= MAX17042_BATTERY_FULL)
129 val->intval = 1;
130 else if (val->intval >= 0)
131 val->intval = 0;
132 break;
133 case POWER_SUPPLY_PROP_TEMP:
134 val->intval = max17042_read_reg(chip->client,
135 MAX17042_TEMP);
136 /* The value is signed. */
137 if (val->intval & 0x8000) {
138 val->intval = (0x7fff & ~val->intval) + 1;
139 val->intval *= -1;
140 }
141 /* The value is converted into deci-centigrade scale */
142 /* Units of LSB = 1 / 256 degree Celsius */
143 val->intval = val->intval * 10 / 256;
144 break;
145 case POWER_SUPPLY_PROP_CURRENT_NOW:
146 if (chip->pdata->enable_current_sense) {
147 val->intval = max17042_read_reg(chip->client,
148 MAX17042_Current);
149 if (val->intval & 0x8000) {
150 /* Negative */
151 val->intval = ~val->intval & 0x7fff;
152 val->intval++;
153 val->intval *= -1;
154 }
155 val->intval >>= 4;
156 val->intval *= 1000000 * 25 / chip->pdata->r_sns;
157 } else {
158 return -EINVAL;
159 }
160 break;
161 case POWER_SUPPLY_PROP_CURRENT_AVG:
162 if (chip->pdata->enable_current_sense) {
163 val->intval = max17042_read_reg(chip->client,
164 MAX17042_AvgCurrent);
165 if (val->intval & 0x8000) {
166 /* Negative */
167 val->intval = ~val->intval & 0x7fff;
168 val->intval++;
169 val->intval *= -1;
170 }
171 val->intval *= 1562500 / chip->pdata->r_sns;
172 } else {
173 return -EINVAL;
174 }
175 break;
152 default: 176 default:
153 return -EINVAL; 177 return -EINVAL;
154 } 178 }
@@ -180,6 +204,11 @@ static int __devinit max17042_probe(struct i2c_client *client,
180 chip->battery.properties = max17042_battery_props; 204 chip->battery.properties = max17042_battery_props;
181 chip->battery.num_properties = ARRAY_SIZE(max17042_battery_props); 205 chip->battery.num_properties = ARRAY_SIZE(max17042_battery_props);
182 206
207 /* When current is not measured,
208 * CURRENT_NOW and CURRENT_AVG properties should be invisible. */
209 if (!chip->pdata->enable_current_sense)
210 chip->battery.num_properties -= 2;
211
183 ret = power_supply_register(&client->dev, &chip->battery); 212 ret = power_supply_register(&client->dev, &chip->battery);
184 if (ret) { 213 if (ret) {
185 dev_err(&client->dev, "failed: power supply register\n"); 214 dev_err(&client->dev, "failed: power supply register\n");
@@ -187,10 +216,18 @@ static int __devinit max17042_probe(struct i2c_client *client,
187 return ret; 216 return ret;
188 } 217 }
189 218
219 /* Initialize registers according to values from the platform data */
220 if (chip->pdata->init_data)
221 max17042_set_reg(client, chip->pdata->init_data,
222 chip->pdata->num_init_data);
223
190 if (!chip->pdata->enable_current_sense) { 224 if (!chip->pdata->enable_current_sense) {
191 max17042_write_reg(client, MAX17042_CGAIN, 0x0000); 225 max17042_write_reg(client, MAX17042_CGAIN, 0x0000);
192 max17042_write_reg(client, MAX17042_MiscCFG, 0x0003); 226 max17042_write_reg(client, MAX17042_MiscCFG, 0x0003);
193 max17042_write_reg(client, MAX17042_LearnCFG, 0x0007); 227 max17042_write_reg(client, MAX17042_LearnCFG, 0x0007);
228 } else {
229 if (chip->pdata->r_sns == 0)
230 chip->pdata->r_sns = MAX17042_DEFAULT_SNS_RESISTOR;
194 } 231 }
195 232
196 return 0; 233 return 0;