Commit | Line | Data |
---|---|---|
359ab9f5 MH |
1 | /* |
2 | * Fuel gauge driver for Maxim 17042 / 8966 / 8997 | |
3 | * Note that Maxim 8966 and 8997 are mfd and this is its subdevice. | |
4 | * | |
5 | * Copyright (C) 2011 Samsung Electronics | |
6 | * MyungJoo Ham <myungjoo.ham@samsung.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | * | |
22 | * This driver is based on max17040_battery.c | |
23 | */ | |
24 | ||
25 | #include <linux/init.h> | |
7e6d62db | 26 | #include <linux/module.h> |
359ab9f5 MH |
27 | #include <linux/slab.h> |
28 | #include <linux/i2c.h> | |
f3a71a6e | 29 | #include <linux/delay.h> |
359ab9f5 MH |
30 | #include <linux/mod_devicetable.h> |
31 | #include <linux/power_supply.h> | |
32 | #include <linux/power/max17042_battery.h> | |
33 | ||
f3a71a6e RP |
34 | /* Status register bits */ |
35 | #define STATUS_POR_BIT (1 << 1) | |
36 | #define STATUS_BST_BIT (1 << 3) | |
37 | #define STATUS_VMN_BIT (1 << 8) | |
38 | #define STATUS_TMN_BIT (1 << 9) | |
39 | #define STATUS_SMN_BIT (1 << 10) | |
40 | #define STATUS_BI_BIT (1 << 11) | |
41 | #define STATUS_VMX_BIT (1 << 12) | |
42 | #define STATUS_TMX_BIT (1 << 13) | |
43 | #define STATUS_SMX_BIT (1 << 14) | |
44 | #define STATUS_BR_BIT (1 << 15) | |
45 | ||
46 | #define VFSOC0_LOCK 0x0000 | |
47 | #define VFSOC0_UNLOCK 0x0080 | |
48 | #define MODEL_UNLOCK1 0X0059 | |
49 | #define MODEL_UNLOCK2 0X00C4 | |
50 | #define MODEL_LOCK1 0X0000 | |
51 | #define MODEL_LOCK2 0X0000 | |
52 | ||
53 | #define dQ_ACC_DIV 0x4 | |
54 | #define dP_ACC_100 0x1900 | |
55 | #define dP_ACC_200 0x3200 | |
56 | ||
359ab9f5 MH |
57 | struct max17042_chip { |
58 | struct i2c_client *client; | |
59 | struct power_supply battery; | |
60 | struct max17042_platform_data *pdata; | |
f3a71a6e RP |
61 | struct work_struct work; |
62 | int init_complete; | |
359ab9f5 MH |
63 | }; |
64 | ||
65 | static int max17042_write_reg(struct i2c_client *client, u8 reg, u16 value) | |
66 | { | |
67 | int ret = i2c_smbus_write_word_data(client, reg, value); | |
68 | ||
69 | if (ret < 0) | |
70 | dev_err(&client->dev, "%s: err %d\n", __func__, ret); | |
71 | ||
72 | return ret; | |
73 | } | |
74 | ||
75 | static int max17042_read_reg(struct i2c_client *client, u8 reg) | |
76 | { | |
77 | int ret = i2c_smbus_read_word_data(client, reg); | |
78 | ||
79 | if (ret < 0) | |
80 | dev_err(&client->dev, "%s: err %d\n", __func__, ret); | |
81 | ||
82 | return ret; | |
83 | } | |
84 | ||
086ef502 DK |
85 | static void max17042_set_reg(struct i2c_client *client, |
86 | struct max17042_reg_data *data, int size) | |
87 | { | |
88 | int i; | |
89 | ||
90 | for (i = 0; i < size; i++) | |
91 | max17042_write_reg(client, data[i].addr, data[i].data); | |
92 | } | |
93 | ||
359ab9f5 | 94 | static enum power_supply_property max17042_battery_props[] = { |
086ef502 DK |
95 | POWER_SUPPLY_PROP_PRESENT, |
96 | POWER_SUPPLY_PROP_CYCLE_COUNT, | |
97 | POWER_SUPPLY_PROP_VOLTAGE_MAX, | |
98 | POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN, | |
359ab9f5 MH |
99 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
100 | POWER_SUPPLY_PROP_VOLTAGE_AVG, | |
101 | POWER_SUPPLY_PROP_CAPACITY, | |
086ef502 DK |
102 | POWER_SUPPLY_PROP_CHARGE_FULL, |
103 | POWER_SUPPLY_PROP_TEMP, | |
104 | POWER_SUPPLY_PROP_CURRENT_NOW, | |
105 | POWER_SUPPLY_PROP_CURRENT_AVG, | |
359ab9f5 MH |
106 | }; |
107 | ||
108 | static int max17042_get_property(struct power_supply *psy, | |
109 | enum power_supply_property psp, | |
110 | union power_supply_propval *val) | |
111 | { | |
112 | struct max17042_chip *chip = container_of(psy, | |
113 | struct max17042_chip, battery); | |
60a1f6e4 | 114 | int ret; |
359ab9f5 | 115 | |
f3a71a6e RP |
116 | if (!chip->init_complete) |
117 | return -EAGAIN; | |
118 | ||
359ab9f5 | 119 | switch (psp) { |
086ef502 | 120 | case POWER_SUPPLY_PROP_PRESENT: |
60a1f6e4 RP |
121 | ret = max17042_read_reg(chip->client, MAX17042_STATUS); |
122 | if (ret < 0) | |
123 | return ret; | |
124 | ||
125 | if (ret & MAX17042_STATUS_BattAbsent) | |
086ef502 DK |
126 | val->intval = 0; |
127 | else | |
128 | val->intval = 1; | |
129 | break; | |
130 | case POWER_SUPPLY_PROP_CYCLE_COUNT: | |
60a1f6e4 RP |
131 | ret = max17042_read_reg(chip->client, MAX17042_Cycles); |
132 | if (ret < 0) | |
133 | return ret; | |
134 | ||
135 | val->intval = ret; | |
086ef502 DK |
136 | break; |
137 | case POWER_SUPPLY_PROP_VOLTAGE_MAX: | |
60a1f6e4 RP |
138 | ret = max17042_read_reg(chip->client, MAX17042_MinMaxVolt); |
139 | if (ret < 0) | |
140 | return ret; | |
141 | ||
142 | val->intval = ret >> 8; | |
086ef502 DK |
143 | val->intval *= 20000; /* Units of LSB = 20mV */ |
144 | break; | |
145 | case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN: | |
60a1f6e4 RP |
146 | ret = max17042_read_reg(chip->client, MAX17042_V_empty); |
147 | if (ret < 0) | |
148 | return ret; | |
149 | ||
150 | val->intval = ret >> 7; | |
086ef502 DK |
151 | val->intval *= 10000; /* Units of LSB = 10mV */ |
152 | break; | |
359ab9f5 | 153 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
60a1f6e4 RP |
154 | ret = max17042_read_reg(chip->client, MAX17042_VCELL); |
155 | if (ret < 0) | |
156 | return ret; | |
157 | ||
158 | val->intval = ret * 625 / 8; | |
359ab9f5 MH |
159 | break; |
160 | case POWER_SUPPLY_PROP_VOLTAGE_AVG: | |
60a1f6e4 RP |
161 | ret = max17042_read_reg(chip->client, MAX17042_AvgVCELL); |
162 | if (ret < 0) | |
163 | return ret; | |
164 | ||
165 | val->intval = ret * 625 / 8; | |
359ab9f5 MH |
166 | break; |
167 | case POWER_SUPPLY_PROP_CAPACITY: | |
60a1f6e4 RP |
168 | ret = max17042_read_reg(chip->client, MAX17042_SOC); |
169 | if (ret < 0) | |
170 | return ret; | |
171 | ||
172 | val->intval = ret >> 8; | |
359ab9f5 | 173 | break; |
086ef502 | 174 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
60a1f6e4 RP |
175 | ret = max17042_read_reg(chip->client, MAX17042_RepSOC); |
176 | if (ret < 0) | |
177 | return ret; | |
178 | ||
179 | if ((ret >> 8) >= MAX17042_BATTERY_FULL) | |
086ef502 | 180 | val->intval = 1; |
60a1f6e4 | 181 | else if (ret >= 0) |
086ef502 DK |
182 | val->intval = 0; |
183 | break; | |
184 | case POWER_SUPPLY_PROP_TEMP: | |
60a1f6e4 RP |
185 | ret = max17042_read_reg(chip->client, MAX17042_TEMP); |
186 | if (ret < 0) | |
187 | return ret; | |
188 | ||
189 | val->intval = ret; | |
086ef502 DK |
190 | /* The value is signed. */ |
191 | if (val->intval & 0x8000) { | |
192 | val->intval = (0x7fff & ~val->intval) + 1; | |
193 | val->intval *= -1; | |
194 | } | |
195 | /* The value is converted into deci-centigrade scale */ | |
196 | /* Units of LSB = 1 / 256 degree Celsius */ | |
197 | val->intval = val->intval * 10 / 256; | |
198 | break; | |
199 | case POWER_SUPPLY_PROP_CURRENT_NOW: | |
200 | if (chip->pdata->enable_current_sense) { | |
60a1f6e4 RP |
201 | ret = max17042_read_reg(chip->client, MAX17042_Current); |
202 | if (ret < 0) | |
203 | return ret; | |
204 | ||
205 | val->intval = ret; | |
086ef502 DK |
206 | if (val->intval & 0x8000) { |
207 | /* Negative */ | |
208 | val->intval = ~val->intval & 0x7fff; | |
209 | val->intval++; | |
210 | val->intval *= -1; | |
211 | } | |
91d8b0d6 | 212 | val->intval *= 1562500 / chip->pdata->r_sns; |
086ef502 DK |
213 | } else { |
214 | return -EINVAL; | |
215 | } | |
216 | break; | |
217 | case POWER_SUPPLY_PROP_CURRENT_AVG: | |
218 | if (chip->pdata->enable_current_sense) { | |
60a1f6e4 RP |
219 | ret = max17042_read_reg(chip->client, |
220 | MAX17042_AvgCurrent); | |
221 | if (ret < 0) | |
222 | return ret; | |
223 | ||
224 | val->intval = ret; | |
086ef502 DK |
225 | if (val->intval & 0x8000) { |
226 | /* Negative */ | |
227 | val->intval = ~val->intval & 0x7fff; | |
228 | val->intval++; | |
229 | val->intval *= -1; | |
230 | } | |
231 | val->intval *= 1562500 / chip->pdata->r_sns; | |
232 | } else { | |
233 | return -EINVAL; | |
234 | } | |
235 | break; | |
359ab9f5 MH |
236 | default: |
237 | return -EINVAL; | |
238 | } | |
239 | return 0; | |
240 | } | |
241 | ||
f3a71a6e RP |
242 | static int max17042_write_verify_reg(struct i2c_client *client, |
243 | u8 reg, u16 value) | |
244 | { | |
245 | int retries = 8; | |
246 | int ret; | |
247 | u16 read_value; | |
248 | ||
249 | do { | |
250 | ret = i2c_smbus_write_word_data(client, reg, value); | |
251 | read_value = max17042_read_reg(client, reg); | |
252 | if (read_value != value) { | |
253 | ret = -EIO; | |
254 | retries--; | |
255 | } | |
256 | } while (retries && read_value != value); | |
257 | ||
258 | if (ret < 0) | |
259 | dev_err(&client->dev, "%s: err %d\n", __func__, ret); | |
260 | ||
261 | return ret; | |
262 | } | |
263 | ||
264 | static inline void max17042_override_por( | |
265 | struct i2c_client *client, u8 reg, u16 value) | |
266 | { | |
267 | if (value) | |
268 | max17042_write_reg(client, reg, value); | |
269 | } | |
270 | ||
271 | static inline void max10742_unlock_model(struct max17042_chip *chip) | |
272 | { | |
273 | struct i2c_client *client = chip->client; | |
274 | max17042_write_reg(client, MAX17042_MLOCKReg1, MODEL_UNLOCK1); | |
275 | max17042_write_reg(client, MAX17042_MLOCKReg2, MODEL_UNLOCK2); | |
276 | } | |
277 | ||
278 | static inline void max10742_lock_model(struct max17042_chip *chip) | |
279 | { | |
280 | struct i2c_client *client = chip->client; | |
281 | max17042_write_reg(client, MAX17042_MLOCKReg1, MODEL_LOCK1); | |
282 | max17042_write_reg(client, MAX17042_MLOCKReg2, MODEL_LOCK2); | |
283 | } | |
284 | ||
285 | static inline void max17042_write_model_data(struct max17042_chip *chip, | |
286 | u8 addr, int size) | |
287 | { | |
288 | struct i2c_client *client = chip->client; | |
289 | int i; | |
290 | for (i = 0; i < size; i++) | |
291 | max17042_write_reg(client, addr + i, | |
292 | chip->pdata->config_data->cell_char_tbl[i]); | |
293 | } | |
294 | ||
295 | static inline void max17042_read_model_data(struct max17042_chip *chip, | |
296 | u8 addr, u16 *data, int size) | |
297 | { | |
298 | struct i2c_client *client = chip->client; | |
299 | int i; | |
300 | ||
301 | for (i = 0; i < size; i++) | |
302 | data[i] = max17042_read_reg(client, addr + i); | |
303 | } | |
304 | ||
305 | static inline int max17042_model_data_compare(struct max17042_chip *chip, | |
306 | u16 *data1, u16 *data2, int size) | |
307 | { | |
308 | int i; | |
309 | ||
310 | if (memcmp(data1, data2, size)) { | |
311 | dev_err(&chip->client->dev, "%s compare failed\n", __func__); | |
312 | for (i = 0; i < size; i++) | |
313 | dev_info(&chip->client->dev, "0x%x, 0x%x", | |
314 | data1[i], data2[i]); | |
315 | dev_info(&chip->client->dev, "\n"); | |
316 | return -EINVAL; | |
317 | } | |
318 | return 0; | |
319 | } | |
320 | ||
321 | static int max17042_init_model(struct max17042_chip *chip) | |
322 | { | |
323 | int ret; | |
324 | int table_size = | |
325 | sizeof(chip->pdata->config_data->cell_char_tbl)/sizeof(u16); | |
326 | u16 *temp_data; | |
327 | ||
328 | temp_data = kzalloc(table_size, GFP_KERNEL); | |
329 | if (!temp_data) | |
330 | return -ENOMEM; | |
331 | ||
332 | max10742_unlock_model(chip); | |
333 | max17042_write_model_data(chip, MAX17042_MODELChrTbl, | |
334 | table_size); | |
335 | max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data, | |
336 | table_size); | |
337 | ||
338 | ret = max17042_model_data_compare( | |
339 | chip, | |
340 | chip->pdata->config_data->cell_char_tbl, | |
341 | temp_data, | |
342 | table_size); | |
343 | ||
344 | max10742_lock_model(chip); | |
345 | kfree(temp_data); | |
346 | ||
347 | return ret; | |
348 | } | |
349 | ||
350 | static int max17042_verify_model_lock(struct max17042_chip *chip) | |
351 | { | |
352 | int i; | |
353 | int table_size = | |
354 | sizeof(chip->pdata->config_data->cell_char_tbl); | |
355 | u16 *temp_data; | |
356 | int ret = 0; | |
357 | ||
358 | temp_data = kzalloc(table_size, GFP_KERNEL); | |
359 | if (!temp_data) | |
360 | return -ENOMEM; | |
361 | ||
362 | max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data, | |
363 | table_size); | |
364 | for (i = 0; i < table_size; i++) | |
365 | if (temp_data[i]) | |
366 | ret = -EINVAL; | |
367 | ||
368 | kfree(temp_data); | |
369 | return ret; | |
370 | } | |
371 | ||
372 | static void max17042_write_config_regs(struct max17042_chip *chip) | |
373 | { | |
374 | struct max17042_config_data *config = chip->pdata->config_data; | |
375 | ||
376 | max17042_write_reg(chip->client, MAX17042_CONFIG, config->config); | |
377 | max17042_write_reg(chip->client, MAX17042_LearnCFG, config->learn_cfg); | |
378 | max17042_write_reg(chip->client, MAX17042_FilterCFG, | |
379 | config->filter_cfg); | |
380 | max17042_write_reg(chip->client, MAX17042_RelaxCFG, config->relax_cfg); | |
381 | } | |
382 | ||
383 | static void max17042_write_custom_regs(struct max17042_chip *chip) | |
384 | { | |
385 | struct max17042_config_data *config = chip->pdata->config_data; | |
386 | ||
387 | max17042_write_verify_reg(chip->client, MAX17042_RCOMP0, | |
388 | config->rcomp0); | |
389 | max17042_write_verify_reg(chip->client, MAX17042_TempCo, | |
390 | config->tcompc0); | |
391 | max17042_write_reg(chip->client, MAX17042_EmptyTempCo, | |
392 | config->empty_tempco); | |
393 | max17042_write_verify_reg(chip->client, MAX17042_K_empty0, | |
394 | config->kempty0); | |
395 | max17042_write_verify_reg(chip->client, MAX17042_ICHGTerm, | |
396 | config->ichgt_term); | |
397 | } | |
398 | ||
399 | static void max17042_update_capacity_regs(struct max17042_chip *chip) | |
400 | { | |
401 | struct max17042_config_data *config = chip->pdata->config_data; | |
402 | ||
403 | max17042_write_verify_reg(chip->client, MAX17042_FullCAP, | |
404 | config->fullcap); | |
405 | max17042_write_reg(chip->client, MAX17042_DesignCap, | |
406 | config->design_cap); | |
407 | max17042_write_verify_reg(chip->client, MAX17042_FullCAPNom, | |
408 | config->fullcapnom); | |
409 | } | |
410 | ||
411 | static void max17042_reset_vfsoc0_reg(struct max17042_chip *chip) | |
412 | { | |
413 | u16 vfSoc; | |
414 | ||
415 | vfSoc = max17042_read_reg(chip->client, MAX17042_VFSOC); | |
416 | max17042_write_reg(chip->client, MAX17042_VFSOC0Enable, VFSOC0_UNLOCK); | |
417 | max17042_write_verify_reg(chip->client, MAX17042_VFSOC0, vfSoc); | |
418 | max17042_write_reg(chip->client, MAX17042_VFSOC0Enable, VFSOC0_LOCK); | |
419 | } | |
420 | ||
421 | static void max17042_load_new_capacity_params(struct max17042_chip *chip) | |
422 | { | |
423 | u16 full_cap0, rep_cap, dq_acc, vfSoc; | |
424 | u32 rem_cap; | |
425 | ||
426 | struct max17042_config_data *config = chip->pdata->config_data; | |
427 | ||
428 | full_cap0 = max17042_read_reg(chip->client, MAX17042_FullCAP0); | |
429 | vfSoc = max17042_read_reg(chip->client, MAX17042_VFSOC); | |
430 | ||
431 | /* fg_vfSoc needs to shifted by 8 bits to get the | |
432 | * perc in 1% accuracy, to get the right rem_cap multiply | |
433 | * full_cap0, fg_vfSoc and devide by 100 | |
434 | */ | |
435 | rem_cap = ((vfSoc >> 8) * full_cap0) / 100; | |
436 | max17042_write_verify_reg(chip->client, MAX17042_RemCap, (u16)rem_cap); | |
437 | ||
438 | rep_cap = (u16)rem_cap; | |
439 | max17042_write_verify_reg(chip->client, MAX17042_RepCap, rep_cap); | |
440 | ||
441 | /* Write dQ_acc to 200% of Capacity and dP_acc to 200% */ | |
442 | dq_acc = config->fullcap / dQ_ACC_DIV; | |
443 | max17042_write_verify_reg(chip->client, MAX17042_dQacc, dq_acc); | |
444 | max17042_write_verify_reg(chip->client, MAX17042_dPacc, dP_ACC_200); | |
445 | ||
446 | max17042_write_verify_reg(chip->client, MAX17042_FullCAP, | |
447 | config->fullcap); | |
448 | max17042_write_reg(chip->client, MAX17042_DesignCap, | |
449 | config->design_cap); | |
450 | max17042_write_verify_reg(chip->client, MAX17042_FullCAPNom, | |
451 | config->fullcapnom); | |
452 | } | |
453 | ||
454 | /* | |
455 | * Block write all the override values coming from platform data. | |
456 | * This function MUST be called before the POR initialization proceedure | |
457 | * specified by maxim. | |
458 | */ | |
459 | static inline void max17042_override_por_values(struct max17042_chip *chip) | |
460 | { | |
461 | struct i2c_client *client = chip->client; | |
462 | struct max17042_config_data *config = chip->pdata->config_data; | |
463 | ||
464 | max17042_override_por(client, MAX17042_TGAIN, config->tgain); | |
465 | max17042_override_por(client, MAx17042_TOFF, config->toff); | |
466 | max17042_override_por(client, MAX17042_CGAIN, config->cgain); | |
467 | max17042_override_por(client, MAX17042_COFF, config->coff); | |
468 | ||
469 | max17042_override_por(client, MAX17042_VALRT_Th, config->valrt_thresh); | |
470 | max17042_override_por(client, MAX17042_TALRT_Th, config->talrt_thresh); | |
471 | max17042_override_por(client, MAX17042_SALRT_Th, | |
472 | config->soc_alrt_thresh); | |
473 | max17042_override_por(client, MAX17042_CONFIG, config->config); | |
474 | max17042_override_por(client, MAX17042_SHDNTIMER, config->shdntimer); | |
475 | ||
476 | max17042_override_por(client, MAX17042_DesignCap, config->design_cap); | |
477 | max17042_override_por(client, MAX17042_ICHGTerm, config->ichgt_term); | |
478 | ||
479 | max17042_override_por(client, MAX17042_AtRate, config->at_rate); | |
480 | max17042_override_por(client, MAX17042_LearnCFG, config->learn_cfg); | |
481 | max17042_override_por(client, MAX17042_FilterCFG, config->filter_cfg); | |
482 | max17042_override_por(client, MAX17042_RelaxCFG, config->relax_cfg); | |
483 | max17042_override_por(client, MAX17042_MiscCFG, config->misc_cfg); | |
484 | max17042_override_por(client, MAX17042_MaskSOC, config->masksoc); | |
485 | ||
486 | max17042_override_por(client, MAX17042_FullCAP, config->fullcap); | |
487 | max17042_override_por(client, MAX17042_FullCAPNom, config->fullcapnom); | |
488 | max17042_override_por(client, MAX17042_SOC_empty, config->socempty); | |
489 | max17042_override_por(client, MAX17042_LAvg_empty, config->lavg_empty); | |
490 | max17042_override_por(client, MAX17042_dQacc, config->dqacc); | |
491 | max17042_override_por(client, MAX17042_dPacc, config->dpacc); | |
492 | ||
493 | max17042_override_por(client, MAX17042_V_empty, config->vempty); | |
494 | max17042_override_por(client, MAX17042_TempNom, config->temp_nom); | |
495 | max17042_override_por(client, MAX17042_TempLim, config->temp_lim); | |
496 | max17042_override_por(client, MAX17042_FCTC, config->fctc); | |
497 | max17042_override_por(client, MAX17042_RCOMP0, config->rcomp0); | |
498 | max17042_override_por(client, MAX17042_TempCo, config->tcompc0); | |
499 | max17042_override_por(client, MAX17042_EmptyTempCo, | |
500 | config->empty_tempco); | |
501 | max17042_override_por(client, MAX17042_K_empty0, config->kempty0); | |
502 | } | |
503 | ||
504 | static int max17042_init_chip(struct max17042_chip *chip) | |
505 | { | |
506 | int ret; | |
507 | int val; | |
508 | ||
509 | max17042_override_por_values(chip); | |
510 | /* After Power up, the MAX17042 requires 500mS in order | |
511 | * to perform signal debouncing and initial SOC reporting | |
512 | */ | |
513 | msleep(500); | |
514 | ||
515 | /* Initialize configaration */ | |
516 | max17042_write_config_regs(chip); | |
517 | ||
518 | /* write cell characterization data */ | |
519 | ret = max17042_init_model(chip); | |
520 | if (ret) { | |
521 | dev_err(&chip->client->dev, "%s init failed\n", | |
522 | __func__); | |
523 | return -EIO; | |
524 | } | |
525 | max17042_verify_model_lock(chip); | |
526 | if (ret) { | |
527 | dev_err(&chip->client->dev, "%s lock verify failed\n", | |
528 | __func__); | |
529 | return -EIO; | |
530 | } | |
531 | /* write custom parameters */ | |
532 | max17042_write_custom_regs(chip); | |
533 | ||
534 | /* update capacity params */ | |
535 | max17042_update_capacity_regs(chip); | |
536 | ||
537 | /* delay must be atleast 350mS to allow VFSOC | |
538 | * to be calculated from the new configuration | |
539 | */ | |
540 | msleep(350); | |
541 | ||
542 | /* reset vfsoc0 reg */ | |
543 | max17042_reset_vfsoc0_reg(chip); | |
544 | ||
545 | /* load new capacity params */ | |
546 | max17042_load_new_capacity_params(chip); | |
547 | ||
548 | /* Init complete, Clear the POR bit */ | |
549 | val = max17042_read_reg(chip->client, MAX17042_STATUS); | |
550 | max17042_write_reg(chip->client, MAX17042_STATUS, | |
551 | val & (~STATUS_POR_BIT)); | |
552 | return 0; | |
553 | } | |
554 | ||
555 | ||
556 | static void max17042_init_worker(struct work_struct *work) | |
557 | { | |
558 | struct max17042_chip *chip = container_of(work, | |
559 | struct max17042_chip, work); | |
560 | int ret; | |
561 | ||
562 | /* Initialize registers according to values from the platform data */ | |
563 | if (chip->pdata->enable_por_init && chip->pdata->config_data) { | |
564 | ret = max17042_init_chip(chip); | |
565 | if (ret) | |
566 | return; | |
567 | } | |
568 | ||
569 | chip->init_complete = 1; | |
570 | } | |
571 | ||
359ab9f5 MH |
572 | static int __devinit max17042_probe(struct i2c_client *client, |
573 | const struct i2c_device_id *id) | |
574 | { | |
575 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); | |
576 | struct max17042_chip *chip; | |
577 | int ret; | |
f3a71a6e | 578 | int reg; |
359ab9f5 MH |
579 | |
580 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) | |
581 | return -EIO; | |
582 | ||
583 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); | |
584 | if (!chip) | |
585 | return -ENOMEM; | |
586 | ||
587 | chip->client = client; | |
588 | chip->pdata = client->dev.platform_data; | |
589 | ||
590 | i2c_set_clientdata(client, chip); | |
591 | ||
592 | chip->battery.name = "max17042_battery"; | |
593 | chip->battery.type = POWER_SUPPLY_TYPE_BATTERY; | |
594 | chip->battery.get_property = max17042_get_property; | |
595 | chip->battery.properties = max17042_battery_props; | |
596 | chip->battery.num_properties = ARRAY_SIZE(max17042_battery_props); | |
597 | ||
086ef502 DK |
598 | /* When current is not measured, |
599 | * CURRENT_NOW and CURRENT_AVG properties should be invisible. */ | |
600 | if (!chip->pdata->enable_current_sense) | |
601 | chip->battery.num_properties -= 2; | |
602 | ||
4cfa892c PR |
603 | if (chip->pdata->r_sns == 0) |
604 | chip->pdata->r_sns = MAX17042_DEFAULT_SNS_RESISTOR; | |
605 | ||
086ef502 DK |
606 | if (chip->pdata->init_data) |
607 | max17042_set_reg(client, chip->pdata->init_data, | |
f3a71a6e | 608 | chip->pdata->num_init_data); |
086ef502 | 609 | |
359ab9f5 MH |
610 | if (!chip->pdata->enable_current_sense) { |
611 | max17042_write_reg(client, MAX17042_CGAIN, 0x0000); | |
612 | max17042_write_reg(client, MAX17042_MiscCFG, 0x0003); | |
613 | max17042_write_reg(client, MAX17042_LearnCFG, 0x0007); | |
614 | } | |
615 | ||
f3a71a6e RP |
616 | reg = max17042_read_reg(chip->client, MAX17042_STATUS); |
617 | ||
618 | if (reg & STATUS_POR_BIT) { | |
619 | INIT_WORK(&chip->work, max17042_init_worker); | |
620 | schedule_work(&chip->work); | |
621 | } else { | |
622 | chip->init_complete = 1; | |
623 | } | |
624 | ||
625 | ret = power_supply_register(&client->dev, &chip->battery); | |
626 | if (ret) { | |
627 | dev_err(&client->dev, "failed: power supply register\n"); | |
628 | kfree(chip); | |
629 | } | |
630 | return ret; | |
359ab9f5 MH |
631 | } |
632 | ||
633 | static int __devexit max17042_remove(struct i2c_client *client) | |
634 | { | |
635 | struct max17042_chip *chip = i2c_get_clientdata(client); | |
636 | ||
637 | power_supply_unregister(&chip->battery); | |
359ab9f5 MH |
638 | kfree(chip); |
639 | return 0; | |
640 | } | |
641 | ||
642 | static const struct i2c_device_id max17042_id[] = { | |
643 | { "max17042", 0 }, | |
644 | { } | |
645 | }; | |
646 | MODULE_DEVICE_TABLE(i2c, max17042_id); | |
647 | ||
648 | static struct i2c_driver max17042_i2c_driver = { | |
649 | .driver = { | |
650 | .name = "max17042", | |
651 | }, | |
652 | .probe = max17042_probe, | |
653 | .remove = __devexit_p(max17042_remove), | |
654 | .id_table = max17042_id, | |
655 | }; | |
656 | ||
657 | static int __init max17042_init(void) | |
658 | { | |
659 | return i2c_add_driver(&max17042_i2c_driver); | |
660 | } | |
661 | module_init(max17042_init); | |
662 | ||
663 | static void __exit max17042_exit(void) | |
664 | { | |
665 | i2c_del_driver(&max17042_i2c_driver); | |
666 | } | |
667 | module_exit(max17042_exit); | |
668 | ||
669 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); | |
670 | MODULE_DESCRIPTION("MAX17042 Fuel Gauge"); | |
671 | MODULE_LICENSE("GPL"); |