blob: 915767dd0962b49d74d49f7a8c78dbc21f07679f [file] [log] [blame]
MyungJoo Ham359ab9f2011-01-14 14:46:11 +09001/*
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>
Paul Gortmaker7e6d62d2011-07-03 15:28:29 -040026#include <linux/module.h>
MyungJoo Ham359ab9f2011-01-14 14:46:11 +090027#include <linux/slab.h>
28#include <linux/i2c.h>
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +040029#include <linux/delay.h>
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -080030#include <linux/interrupt.h>
MyungJoo Ham359ab9f2011-01-14 14:46:11 +090031#include <linux/mod_devicetable.h>
32#include <linux/power_supply.h>
33#include <linux/power/max17042_battery.h>
Karol Lewandowski38322462012-02-22 19:06:22 +010034#include <linux/of.h>
MyungJoo Ham359ab9f2011-01-14 14:46:11 +090035
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +040036/* Status register bits */
37#define STATUS_POR_BIT (1 << 1)
38#define STATUS_BST_BIT (1 << 3)
39#define STATUS_VMN_BIT (1 << 8)
40#define STATUS_TMN_BIT (1 << 9)
41#define STATUS_SMN_BIT (1 << 10)
42#define STATUS_BI_BIT (1 << 11)
43#define STATUS_VMX_BIT (1 << 12)
44#define STATUS_TMX_BIT (1 << 13)
45#define STATUS_SMX_BIT (1 << 14)
46#define STATUS_BR_BIT (1 << 15)
47
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -080048/* Interrupt mask bits */
49#define CONFIG_ALRT_BIT_ENBL (1 << 2)
Ramakrishna Pallala5cdd4d72012-03-21 03:03:16 +053050#define STATUS_INTR_SOCMIN_BIT (1 << 10)
51#define STATUS_INTR_SOCMAX_BIT (1 << 14)
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -080052
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +040053#define VFSOC0_LOCK 0x0000
54#define VFSOC0_UNLOCK 0x0080
55#define MODEL_UNLOCK1 0X0059
56#define MODEL_UNLOCK2 0X00C4
57#define MODEL_LOCK1 0X0000
58#define MODEL_LOCK2 0X0000
59
60#define dQ_ACC_DIV 0x4
61#define dP_ACC_100 0x1900
62#define dP_ACC_200 0x3200
63
MyungJoo Ham359ab9f2011-01-14 14:46:11 +090064struct max17042_chip {
65 struct i2c_client *client;
66 struct power_supply battery;
67 struct max17042_platform_data *pdata;
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +040068 struct work_struct work;
69 int init_complete;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +090070};
71
72static int max17042_write_reg(struct i2c_client *client, u8 reg, u16 value)
73{
74 int ret = i2c_smbus_write_word_data(client, reg, value);
75
76 if (ret < 0)
77 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
78
79 return ret;
80}
81
82static int max17042_read_reg(struct i2c_client *client, u8 reg)
83{
84 int ret = i2c_smbus_read_word_data(client, reg);
85
86 if (ret < 0)
87 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
88
89 return ret;
90}
91
Donggeun Kim086ef502011-06-30 18:07:41 +090092static void max17042_set_reg(struct i2c_client *client,
93 struct max17042_reg_data *data, int size)
94{
95 int i;
96
97 for (i = 0; i < size; i++)
98 max17042_write_reg(client, data[i].addr, data[i].data);
99}
100
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900101static enum power_supply_property max17042_battery_props[] = {
Donggeun Kim086ef502011-06-30 18:07:41 +0900102 POWER_SUPPLY_PROP_PRESENT,
103 POWER_SUPPLY_PROP_CYCLE_COUNT,
104 POWER_SUPPLY_PROP_VOLTAGE_MAX,
105 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900106 POWER_SUPPLY_PROP_VOLTAGE_NOW,
107 POWER_SUPPLY_PROP_VOLTAGE_AVG,
Ramakrishna Pallaladc7ef2b2012-04-10 16:21:20 +0530108 POWER_SUPPLY_PROP_VOLTAGE_OCV,
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900109 POWER_SUPPLY_PROP_CAPACITY,
Donggeun Kim086ef502011-06-30 18:07:41 +0900110 POWER_SUPPLY_PROP_CHARGE_FULL,
111 POWER_SUPPLY_PROP_TEMP,
112 POWER_SUPPLY_PROP_CURRENT_NOW,
113 POWER_SUPPLY_PROP_CURRENT_AVG,
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900114};
115
116static int max17042_get_property(struct power_supply *psy,
117 enum power_supply_property psp,
118 union power_supply_propval *val)
119{
120 struct max17042_chip *chip = container_of(psy,
121 struct max17042_chip, battery);
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400122 int ret;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900123
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400124 if (!chip->init_complete)
125 return -EAGAIN;
126
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900127 switch (psp) {
Donggeun Kim086ef502011-06-30 18:07:41 +0900128 case POWER_SUPPLY_PROP_PRESENT:
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400129 ret = max17042_read_reg(chip->client, MAX17042_STATUS);
130 if (ret < 0)
131 return ret;
132
133 if (ret & MAX17042_STATUS_BattAbsent)
Donggeun Kim086ef502011-06-30 18:07:41 +0900134 val->intval = 0;
135 else
136 val->intval = 1;
137 break;
138 case POWER_SUPPLY_PROP_CYCLE_COUNT:
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400139 ret = max17042_read_reg(chip->client, MAX17042_Cycles);
140 if (ret < 0)
141 return ret;
142
143 val->intval = ret;
Donggeun Kim086ef502011-06-30 18:07:41 +0900144 break;
145 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400146 ret = max17042_read_reg(chip->client, MAX17042_MinMaxVolt);
147 if (ret < 0)
148 return ret;
149
150 val->intval = ret >> 8;
Donggeun Kim086ef502011-06-30 18:07:41 +0900151 val->intval *= 20000; /* Units of LSB = 20mV */
152 break;
153 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400154 ret = max17042_read_reg(chip->client, MAX17042_V_empty);
155 if (ret < 0)
156 return ret;
157
158 val->intval = ret >> 7;
Donggeun Kim086ef502011-06-30 18:07:41 +0900159 val->intval *= 10000; /* Units of LSB = 10mV */
160 break;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900161 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400162 ret = max17042_read_reg(chip->client, MAX17042_VCELL);
163 if (ret < 0)
164 return ret;
165
166 val->intval = ret * 625 / 8;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900167 break;
168 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400169 ret = max17042_read_reg(chip->client, MAX17042_AvgVCELL);
170 if (ret < 0)
171 return ret;
172
173 val->intval = ret * 625 / 8;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900174 break;
Ramakrishna Pallaladc7ef2b2012-04-10 16:21:20 +0530175 case POWER_SUPPLY_PROP_VOLTAGE_OCV:
176 ret = max17042_read_reg(chip->client, MAX17042_OCVInternal);
177 if (ret < 0)
178 return ret;
179
180 val->intval = ret * 625 / 8;
181 break;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900182 case POWER_SUPPLY_PROP_CAPACITY:
Dirk Brandewie13e0aa42012-01-24 09:26:08 -0800183 ret = max17042_read_reg(chip->client, MAX17042_RepSOC);
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400184 if (ret < 0)
185 return ret;
186
187 val->intval = ret >> 8;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900188 break;
Donggeun Kim086ef502011-06-30 18:07:41 +0900189 case POWER_SUPPLY_PROP_CHARGE_FULL:
MyungJoo Ham6e0e60c2012-03-14 03:00:01 +0100190 ret = max17042_read_reg(chip->client, MAX17042_FullCAP);
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400191 if (ret < 0)
192 return ret;
193
MyungJoo Ham6e0e60c2012-03-14 03:00:01 +0100194 val->intval = ret * 1000 / 2;
Donggeun Kim086ef502011-06-30 18:07:41 +0900195 break;
196 case POWER_SUPPLY_PROP_TEMP:
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400197 ret = max17042_read_reg(chip->client, MAX17042_TEMP);
198 if (ret < 0)
199 return ret;
200
201 val->intval = ret;
Donggeun Kim086ef502011-06-30 18:07:41 +0900202 /* The value is signed. */
203 if (val->intval & 0x8000) {
204 val->intval = (0x7fff & ~val->intval) + 1;
205 val->intval *= -1;
206 }
207 /* The value is converted into deci-centigrade scale */
208 /* Units of LSB = 1 / 256 degree Celsius */
209 val->intval = val->intval * 10 / 256;
210 break;
211 case POWER_SUPPLY_PROP_CURRENT_NOW:
212 if (chip->pdata->enable_current_sense) {
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400213 ret = max17042_read_reg(chip->client, MAX17042_Current);
214 if (ret < 0)
215 return ret;
216
217 val->intval = ret;
Donggeun Kim086ef502011-06-30 18:07:41 +0900218 if (val->intval & 0x8000) {
219 /* Negative */
220 val->intval = ~val->intval & 0x7fff;
221 val->intval++;
222 val->intval *= -1;
223 }
Philip Rakity91d8b0d2011-08-12 21:19:57 -0700224 val->intval *= 1562500 / chip->pdata->r_sns;
Donggeun Kim086ef502011-06-30 18:07:41 +0900225 } else {
226 return -EINVAL;
227 }
228 break;
229 case POWER_SUPPLY_PROP_CURRENT_AVG:
230 if (chip->pdata->enable_current_sense) {
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400231 ret = max17042_read_reg(chip->client,
232 MAX17042_AvgCurrent);
233 if (ret < 0)
234 return ret;
235
236 val->intval = ret;
Donggeun Kim086ef502011-06-30 18:07:41 +0900237 if (val->intval & 0x8000) {
238 /* Negative */
239 val->intval = ~val->intval & 0x7fff;
240 val->intval++;
241 val->intval *= -1;
242 }
243 val->intval *= 1562500 / chip->pdata->r_sns;
244 } else {
245 return -EINVAL;
246 }
247 break;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900248 default:
249 return -EINVAL;
250 }
251 return 0;
252}
253
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400254static int max17042_write_verify_reg(struct i2c_client *client,
255 u8 reg, u16 value)
256{
257 int retries = 8;
258 int ret;
259 u16 read_value;
260
261 do {
262 ret = i2c_smbus_write_word_data(client, reg, value);
263 read_value = max17042_read_reg(client, reg);
264 if (read_value != value) {
265 ret = -EIO;
266 retries--;
267 }
268 } while (retries && read_value != value);
269
270 if (ret < 0)
271 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
272
273 return ret;
274}
275
276static inline void max17042_override_por(
277 struct i2c_client *client, u8 reg, u16 value)
278{
279 if (value)
280 max17042_write_reg(client, reg, value);
281}
282
283static inline void max10742_unlock_model(struct max17042_chip *chip)
284{
285 struct i2c_client *client = chip->client;
286 max17042_write_reg(client, MAX17042_MLOCKReg1, MODEL_UNLOCK1);
287 max17042_write_reg(client, MAX17042_MLOCKReg2, MODEL_UNLOCK2);
288}
289
290static inline void max10742_lock_model(struct max17042_chip *chip)
291{
292 struct i2c_client *client = chip->client;
293 max17042_write_reg(client, MAX17042_MLOCKReg1, MODEL_LOCK1);
294 max17042_write_reg(client, MAX17042_MLOCKReg2, MODEL_LOCK2);
295}
296
297static inline void max17042_write_model_data(struct max17042_chip *chip,
298 u8 addr, int size)
299{
300 struct i2c_client *client = chip->client;
301 int i;
302 for (i = 0; i < size; i++)
303 max17042_write_reg(client, addr + i,
304 chip->pdata->config_data->cell_char_tbl[i]);
305}
306
307static inline void max17042_read_model_data(struct max17042_chip *chip,
308 u8 addr, u16 *data, int size)
309{
310 struct i2c_client *client = chip->client;
311 int i;
312
313 for (i = 0; i < size; i++)
314 data[i] = max17042_read_reg(client, addr + i);
315}
316
317static inline int max17042_model_data_compare(struct max17042_chip *chip,
318 u16 *data1, u16 *data2, int size)
319{
320 int i;
321
322 if (memcmp(data1, data2, size)) {
323 dev_err(&chip->client->dev, "%s compare failed\n", __func__);
324 for (i = 0; i < size; i++)
325 dev_info(&chip->client->dev, "0x%x, 0x%x",
326 data1[i], data2[i]);
327 dev_info(&chip->client->dev, "\n");
328 return -EINVAL;
329 }
330 return 0;
331}
332
333static int max17042_init_model(struct max17042_chip *chip)
334{
335 int ret;
336 int table_size =
337 sizeof(chip->pdata->config_data->cell_char_tbl)/sizeof(u16);
338 u16 *temp_data;
339
340 temp_data = kzalloc(table_size, GFP_KERNEL);
341 if (!temp_data)
342 return -ENOMEM;
343
344 max10742_unlock_model(chip);
345 max17042_write_model_data(chip, MAX17042_MODELChrTbl,
346 table_size);
347 max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data,
348 table_size);
349
350 ret = max17042_model_data_compare(
351 chip,
352 chip->pdata->config_data->cell_char_tbl,
353 temp_data,
354 table_size);
355
356 max10742_lock_model(chip);
357 kfree(temp_data);
358
359 return ret;
360}
361
362static int max17042_verify_model_lock(struct max17042_chip *chip)
363{
364 int i;
365 int table_size =
366 sizeof(chip->pdata->config_data->cell_char_tbl);
367 u16 *temp_data;
368 int ret = 0;
369
370 temp_data = kzalloc(table_size, GFP_KERNEL);
371 if (!temp_data)
372 return -ENOMEM;
373
374 max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data,
375 table_size);
376 for (i = 0; i < table_size; i++)
377 if (temp_data[i])
378 ret = -EINVAL;
379
380 kfree(temp_data);
381 return ret;
382}
383
384static void max17042_write_config_regs(struct max17042_chip *chip)
385{
386 struct max17042_config_data *config = chip->pdata->config_data;
387
388 max17042_write_reg(chip->client, MAX17042_CONFIG, config->config);
389 max17042_write_reg(chip->client, MAX17042_LearnCFG, config->learn_cfg);
390 max17042_write_reg(chip->client, MAX17042_FilterCFG,
391 config->filter_cfg);
392 max17042_write_reg(chip->client, MAX17042_RelaxCFG, config->relax_cfg);
393}
394
395static void max17042_write_custom_regs(struct max17042_chip *chip)
396{
397 struct max17042_config_data *config = chip->pdata->config_data;
398
399 max17042_write_verify_reg(chip->client, MAX17042_RCOMP0,
400 config->rcomp0);
401 max17042_write_verify_reg(chip->client, MAX17042_TempCo,
402 config->tcompc0);
403 max17042_write_reg(chip->client, MAX17042_EmptyTempCo,
404 config->empty_tempco);
405 max17042_write_verify_reg(chip->client, MAX17042_K_empty0,
406 config->kempty0);
407 max17042_write_verify_reg(chip->client, MAX17042_ICHGTerm,
408 config->ichgt_term);
409}
410
411static void max17042_update_capacity_regs(struct max17042_chip *chip)
412{
413 struct max17042_config_data *config = chip->pdata->config_data;
414
415 max17042_write_verify_reg(chip->client, MAX17042_FullCAP,
416 config->fullcap);
417 max17042_write_reg(chip->client, MAX17042_DesignCap,
418 config->design_cap);
419 max17042_write_verify_reg(chip->client, MAX17042_FullCAPNom,
420 config->fullcapnom);
421}
422
423static void max17042_reset_vfsoc0_reg(struct max17042_chip *chip)
424{
425 u16 vfSoc;
426
427 vfSoc = max17042_read_reg(chip->client, MAX17042_VFSOC);
428 max17042_write_reg(chip->client, MAX17042_VFSOC0Enable, VFSOC0_UNLOCK);
429 max17042_write_verify_reg(chip->client, MAX17042_VFSOC0, vfSoc);
430 max17042_write_reg(chip->client, MAX17042_VFSOC0Enable, VFSOC0_LOCK);
431}
432
433static void max17042_load_new_capacity_params(struct max17042_chip *chip)
434{
435 u16 full_cap0, rep_cap, dq_acc, vfSoc;
436 u32 rem_cap;
437
438 struct max17042_config_data *config = chip->pdata->config_data;
439
440 full_cap0 = max17042_read_reg(chip->client, MAX17042_FullCAP0);
441 vfSoc = max17042_read_reg(chip->client, MAX17042_VFSOC);
442
443 /* fg_vfSoc needs to shifted by 8 bits to get the
444 * perc in 1% accuracy, to get the right rem_cap multiply
445 * full_cap0, fg_vfSoc and devide by 100
446 */
447 rem_cap = ((vfSoc >> 8) * full_cap0) / 100;
448 max17042_write_verify_reg(chip->client, MAX17042_RemCap, (u16)rem_cap);
449
450 rep_cap = (u16)rem_cap;
451 max17042_write_verify_reg(chip->client, MAX17042_RepCap, rep_cap);
452
453 /* Write dQ_acc to 200% of Capacity and dP_acc to 200% */
454 dq_acc = config->fullcap / dQ_ACC_DIV;
455 max17042_write_verify_reg(chip->client, MAX17042_dQacc, dq_acc);
456 max17042_write_verify_reg(chip->client, MAX17042_dPacc, dP_ACC_200);
457
458 max17042_write_verify_reg(chip->client, MAX17042_FullCAP,
459 config->fullcap);
460 max17042_write_reg(chip->client, MAX17042_DesignCap,
461 config->design_cap);
462 max17042_write_verify_reg(chip->client, MAX17042_FullCAPNom,
463 config->fullcapnom);
464}
465
466/*
467 * Block write all the override values coming from platform data.
468 * This function MUST be called before the POR initialization proceedure
469 * specified by maxim.
470 */
471static inline void max17042_override_por_values(struct max17042_chip *chip)
472{
473 struct i2c_client *client = chip->client;
474 struct max17042_config_data *config = chip->pdata->config_data;
475
476 max17042_override_por(client, MAX17042_TGAIN, config->tgain);
477 max17042_override_por(client, MAx17042_TOFF, config->toff);
478 max17042_override_por(client, MAX17042_CGAIN, config->cgain);
479 max17042_override_por(client, MAX17042_COFF, config->coff);
480
481 max17042_override_por(client, MAX17042_VALRT_Th, config->valrt_thresh);
482 max17042_override_por(client, MAX17042_TALRT_Th, config->talrt_thresh);
483 max17042_override_por(client, MAX17042_SALRT_Th,
484 config->soc_alrt_thresh);
485 max17042_override_por(client, MAX17042_CONFIG, config->config);
486 max17042_override_por(client, MAX17042_SHDNTIMER, config->shdntimer);
487
488 max17042_override_por(client, MAX17042_DesignCap, config->design_cap);
489 max17042_override_por(client, MAX17042_ICHGTerm, config->ichgt_term);
490
491 max17042_override_por(client, MAX17042_AtRate, config->at_rate);
492 max17042_override_por(client, MAX17042_LearnCFG, config->learn_cfg);
493 max17042_override_por(client, MAX17042_FilterCFG, config->filter_cfg);
494 max17042_override_por(client, MAX17042_RelaxCFG, config->relax_cfg);
495 max17042_override_por(client, MAX17042_MiscCFG, config->misc_cfg);
496 max17042_override_por(client, MAX17042_MaskSOC, config->masksoc);
497
498 max17042_override_por(client, MAX17042_FullCAP, config->fullcap);
499 max17042_override_por(client, MAX17042_FullCAPNom, config->fullcapnom);
500 max17042_override_por(client, MAX17042_SOC_empty, config->socempty);
501 max17042_override_por(client, MAX17042_LAvg_empty, config->lavg_empty);
502 max17042_override_por(client, MAX17042_dQacc, config->dqacc);
503 max17042_override_por(client, MAX17042_dPacc, config->dpacc);
504
505 max17042_override_por(client, MAX17042_V_empty, config->vempty);
506 max17042_override_por(client, MAX17042_TempNom, config->temp_nom);
507 max17042_override_por(client, MAX17042_TempLim, config->temp_lim);
508 max17042_override_por(client, MAX17042_FCTC, config->fctc);
509 max17042_override_por(client, MAX17042_RCOMP0, config->rcomp0);
510 max17042_override_por(client, MAX17042_TempCo, config->tcompc0);
511 max17042_override_por(client, MAX17042_EmptyTempCo,
512 config->empty_tempco);
513 max17042_override_por(client, MAX17042_K_empty0, config->kempty0);
514}
515
516static int max17042_init_chip(struct max17042_chip *chip)
517{
518 int ret;
519 int val;
520
521 max17042_override_por_values(chip);
522 /* After Power up, the MAX17042 requires 500mS in order
523 * to perform signal debouncing and initial SOC reporting
524 */
525 msleep(500);
526
527 /* Initialize configaration */
528 max17042_write_config_regs(chip);
529
530 /* write cell characterization data */
531 ret = max17042_init_model(chip);
532 if (ret) {
533 dev_err(&chip->client->dev, "%s init failed\n",
534 __func__);
535 return -EIO;
536 }
537 max17042_verify_model_lock(chip);
538 if (ret) {
539 dev_err(&chip->client->dev, "%s lock verify failed\n",
540 __func__);
541 return -EIO;
542 }
543 /* write custom parameters */
544 max17042_write_custom_regs(chip);
545
546 /* update capacity params */
547 max17042_update_capacity_regs(chip);
548
549 /* delay must be atleast 350mS to allow VFSOC
550 * to be calculated from the new configuration
551 */
552 msleep(350);
553
554 /* reset vfsoc0 reg */
555 max17042_reset_vfsoc0_reg(chip);
556
557 /* load new capacity params */
558 max17042_load_new_capacity_params(chip);
559
560 /* Init complete, Clear the POR bit */
561 val = max17042_read_reg(chip->client, MAX17042_STATUS);
562 max17042_write_reg(chip->client, MAX17042_STATUS,
563 val & (~STATUS_POR_BIT));
564 return 0;
565}
566
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -0800567static void max17042_set_soc_threshold(struct max17042_chip *chip, u16 off)
568{
569 u16 soc, soc_tr;
570
571 /* program interrupt thesholds such that we should
572 * get interrupt for every 'off' perc change in the soc
573 */
574 soc = max17042_read_reg(chip->client, MAX17042_RepSOC) >> 8;
575 soc_tr = (soc + off) << 8;
576 soc_tr |= (soc - off);
577 max17042_write_reg(chip->client, MAX17042_SALRT_Th, soc_tr);
578}
579
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -0800580static irqreturn_t max17042_thread_handler(int id, void *dev)
581{
582 struct max17042_chip *chip = dev;
583 u16 val;
584
585 val = max17042_read_reg(chip->client, MAX17042_STATUS);
Ramakrishna Pallala5cdd4d72012-03-21 03:03:16 +0530586 if ((val & STATUS_INTR_SOCMIN_BIT) ||
587 (val & STATUS_INTR_SOCMAX_BIT)) {
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -0800588 dev_info(&chip->client->dev, "SOC threshold INTR\n");
589 max17042_set_soc_threshold(chip, 1);
590 }
591
592 power_supply_changed(&chip->battery);
593 return IRQ_HANDLED;
594}
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400595
596static void max17042_init_worker(struct work_struct *work)
597{
598 struct max17042_chip *chip = container_of(work,
599 struct max17042_chip, work);
600 int ret;
601
602 /* Initialize registers according to values from the platform data */
603 if (chip->pdata->enable_por_init && chip->pdata->config_data) {
604 ret = max17042_init_chip(chip);
605 if (ret)
606 return;
607 }
608
609 chip->init_complete = 1;
610}
611
Karol Lewandowski38322462012-02-22 19:06:22 +0100612#ifdef CONFIG_OF
613static struct max17042_platform_data *
614max17042_get_pdata(struct device *dev)
615{
616 struct device_node *np = dev->of_node;
617 u32 prop;
618 struct max17042_platform_data *pdata;
619
620 if (!np)
621 return dev->platform_data;
622
623 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
624 if (!pdata)
625 return NULL;
626
627 /*
628 * Require current sense resistor value to be specified for
629 * current-sense functionality to be enabled at all.
630 */
631 if (of_property_read_u32(np, "maxim,rsns-microohm", &prop) == 0) {
632 pdata->r_sns = prop;
633 pdata->enable_current_sense = true;
634 }
635
636 return pdata;
637}
638#else
639static struct max17042_platform_data *
640max17042_get_pdata(struct device *dev)
641{
642 return dev->platform_data;
643}
644#endif
645
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900646static int __devinit max17042_probe(struct i2c_client *client,
647 const struct i2c_device_id *id)
648{
649 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
650 struct max17042_chip *chip;
651 int ret;
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400652 int reg;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900653
654 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
655 return -EIO;
656
Karol Lewandowski2f3b4342012-02-22 19:06:20 +0100657 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900658 if (!chip)
659 return -ENOMEM;
660
661 chip->client = client;
Karol Lewandowski38322462012-02-22 19:06:22 +0100662 chip->pdata = max17042_get_pdata(&client->dev);
663 if (!chip->pdata) {
664 dev_err(&client->dev, "no platform data provided\n");
665 return -EINVAL;
666 }
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900667
668 i2c_set_clientdata(client, chip);
669
670 chip->battery.name = "max17042_battery";
671 chip->battery.type = POWER_SUPPLY_TYPE_BATTERY;
672 chip->battery.get_property = max17042_get_property;
673 chip->battery.properties = max17042_battery_props;
674 chip->battery.num_properties = ARRAY_SIZE(max17042_battery_props);
675
Donggeun Kim086ef502011-06-30 18:07:41 +0900676 /* When current is not measured,
677 * CURRENT_NOW and CURRENT_AVG properties should be invisible. */
678 if (!chip->pdata->enable_current_sense)
679 chip->battery.num_properties -= 2;
680
Philip Rakity4cfa8922011-08-12 21:18:18 -0700681 if (chip->pdata->r_sns == 0)
682 chip->pdata->r_sns = MAX17042_DEFAULT_SNS_RESISTOR;
683
Donggeun Kim086ef502011-06-30 18:07:41 +0900684 if (chip->pdata->init_data)
685 max17042_set_reg(client, chip->pdata->init_data,
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400686 chip->pdata->num_init_data);
Donggeun Kim086ef502011-06-30 18:07:41 +0900687
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900688 if (!chip->pdata->enable_current_sense) {
689 max17042_write_reg(client, MAX17042_CGAIN, 0x0000);
690 max17042_write_reg(client, MAX17042_MiscCFG, 0x0003);
691 max17042_write_reg(client, MAX17042_LearnCFG, 0x0007);
692 }
693
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -0800694 if (client->irq) {
Ramakrishna Pallala5cdd4d72012-03-21 03:03:16 +0530695 ret = request_threaded_irq(client->irq, NULL,
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -0800696 max17042_thread_handler,
Ramakrishna Pallala5cdd4d72012-03-21 03:03:16 +0530697 IRQF_TRIGGER_FALLING,
698 chip->battery.name, chip);
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -0800699 if (!ret) {
700 reg = max17042_read_reg(client, MAX17042_CONFIG);
701 reg |= CONFIG_ALRT_BIT_ENBL;
702 max17042_write_reg(client, MAX17042_CONFIG, reg);
703 max17042_set_soc_threshold(chip, 1);
704 } else
705 dev_err(&client->dev, "%s(): cannot get IRQ\n",
706 __func__);
707 }
708
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400709 reg = max17042_read_reg(chip->client, MAX17042_STATUS);
710
711 if (reg & STATUS_POR_BIT) {
712 INIT_WORK(&chip->work, max17042_init_worker);
713 schedule_work(&chip->work);
714 } else {
715 chip->init_complete = 1;
716 }
717
718 ret = power_supply_register(&client->dev, &chip->battery);
Karol Lewandowski2f3b4342012-02-22 19:06:20 +0100719 if (ret)
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400720 dev_err(&client->dev, "failed: power supply register\n");
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400721 return ret;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900722}
723
724static int __devexit max17042_remove(struct i2c_client *client)
725{
726 struct max17042_chip *chip = i2c_get_clientdata(client);
727
728 power_supply_unregister(&chip->battery);
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900729 return 0;
730}
731
Karol Lewandowski38322462012-02-22 19:06:22 +0100732#ifdef CONFIG_OF
733static const struct of_device_id max17042_dt_match[] = {
734 { .compatible = "maxim,max17042" },
735 { },
736};
737MODULE_DEVICE_TABLE(of, max17042_dt_match);
738#endif
739
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900740static const struct i2c_device_id max17042_id[] = {
741 { "max17042", 0 },
742 { }
743};
744MODULE_DEVICE_TABLE(i2c, max17042_id);
745
746static struct i2c_driver max17042_i2c_driver = {
747 .driver = {
748 .name = "max17042",
Karol Lewandowski38322462012-02-22 19:06:22 +0100749 .of_match_table = of_match_ptr(max17042_dt_match),
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900750 },
751 .probe = max17042_probe,
752 .remove = __devexit_p(max17042_remove),
753 .id_table = max17042_id,
754};
Axel Lin5ff92e72012-01-21 14:42:54 +0800755module_i2c_driver(max17042_i2c_driver);
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900756
757MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
758MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
759MODULE_LICENSE("GPL");