blob: 93fd13c9be68a1350d3945fe6c9c9a80be1b96c2 [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,
108 POWER_SUPPLY_PROP_CAPACITY,
Donggeun Kim086ef502011-06-30 18:07:41 +0900109 POWER_SUPPLY_PROP_CHARGE_FULL,
110 POWER_SUPPLY_PROP_TEMP,
111 POWER_SUPPLY_PROP_CURRENT_NOW,
112 POWER_SUPPLY_PROP_CURRENT_AVG,
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900113};
114
115static int max17042_get_property(struct power_supply *psy,
116 enum power_supply_property psp,
117 union power_supply_propval *val)
118{
119 struct max17042_chip *chip = container_of(psy,
120 struct max17042_chip, battery);
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400121 int ret;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900122
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400123 if (!chip->init_complete)
124 return -EAGAIN;
125
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900126 switch (psp) {
Donggeun Kim086ef502011-06-30 18:07:41 +0900127 case POWER_SUPPLY_PROP_PRESENT:
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400128 ret = max17042_read_reg(chip->client, MAX17042_STATUS);
129 if (ret < 0)
130 return ret;
131
132 if (ret & MAX17042_STATUS_BattAbsent)
Donggeun Kim086ef502011-06-30 18:07:41 +0900133 val->intval = 0;
134 else
135 val->intval = 1;
136 break;
137 case POWER_SUPPLY_PROP_CYCLE_COUNT:
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400138 ret = max17042_read_reg(chip->client, MAX17042_Cycles);
139 if (ret < 0)
140 return ret;
141
142 val->intval = ret;
Donggeun Kim086ef502011-06-30 18:07:41 +0900143 break;
144 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400145 ret = max17042_read_reg(chip->client, MAX17042_MinMaxVolt);
146 if (ret < 0)
147 return ret;
148
149 val->intval = ret >> 8;
Donggeun Kim086ef502011-06-30 18:07:41 +0900150 val->intval *= 20000; /* Units of LSB = 20mV */
151 break;
152 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400153 ret = max17042_read_reg(chip->client, MAX17042_V_empty);
154 if (ret < 0)
155 return ret;
156
157 val->intval = ret >> 7;
Donggeun Kim086ef502011-06-30 18:07:41 +0900158 val->intval *= 10000; /* Units of LSB = 10mV */
159 break;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900160 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400161 ret = max17042_read_reg(chip->client, MAX17042_VCELL);
162 if (ret < 0)
163 return ret;
164
165 val->intval = ret * 625 / 8;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900166 break;
167 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400168 ret = max17042_read_reg(chip->client, MAX17042_AvgVCELL);
169 if (ret < 0)
170 return ret;
171
172 val->intval = ret * 625 / 8;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900173 break;
174 case POWER_SUPPLY_PROP_CAPACITY:
Dirk Brandewie13e0aa42012-01-24 09:26:08 -0800175 ret = max17042_read_reg(chip->client, MAX17042_RepSOC);
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400176 if (ret < 0)
177 return ret;
178
179 val->intval = ret >> 8;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900180 break;
Donggeun Kim086ef502011-06-30 18:07:41 +0900181 case POWER_SUPPLY_PROP_CHARGE_FULL:
MyungJoo Ham6e0e60c2012-03-14 03:00:01 +0100182 ret = max17042_read_reg(chip->client, MAX17042_FullCAP);
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400183 if (ret < 0)
184 return ret;
185
MyungJoo Ham6e0e60c2012-03-14 03:00:01 +0100186 val->intval = ret * 1000 / 2;
Donggeun Kim086ef502011-06-30 18:07:41 +0900187 break;
188 case POWER_SUPPLY_PROP_TEMP:
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400189 ret = max17042_read_reg(chip->client, MAX17042_TEMP);
190 if (ret < 0)
191 return ret;
192
193 val->intval = ret;
Donggeun Kim086ef502011-06-30 18:07:41 +0900194 /* The value is signed. */
195 if (val->intval & 0x8000) {
196 val->intval = (0x7fff & ~val->intval) + 1;
197 val->intval *= -1;
198 }
199 /* The value is converted into deci-centigrade scale */
200 /* Units of LSB = 1 / 256 degree Celsius */
201 val->intval = val->intval * 10 / 256;
202 break;
203 case POWER_SUPPLY_PROP_CURRENT_NOW:
204 if (chip->pdata->enable_current_sense) {
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400205 ret = max17042_read_reg(chip->client, MAX17042_Current);
206 if (ret < 0)
207 return ret;
208
209 val->intval = ret;
Donggeun Kim086ef502011-06-30 18:07:41 +0900210 if (val->intval & 0x8000) {
211 /* Negative */
212 val->intval = ~val->intval & 0x7fff;
213 val->intval++;
214 val->intval *= -1;
215 }
Philip Rakity91d8b0d2011-08-12 21:19:57 -0700216 val->intval *= 1562500 / chip->pdata->r_sns;
Donggeun Kim086ef502011-06-30 18:07:41 +0900217 } else {
218 return -EINVAL;
219 }
220 break;
221 case POWER_SUPPLY_PROP_CURRENT_AVG:
222 if (chip->pdata->enable_current_sense) {
Ramakrishna Pallala60a1f6e2011-11-26 04:11:15 +0400223 ret = max17042_read_reg(chip->client,
224 MAX17042_AvgCurrent);
225 if (ret < 0)
226 return ret;
227
228 val->intval = ret;
Donggeun Kim086ef502011-06-30 18:07:41 +0900229 if (val->intval & 0x8000) {
230 /* Negative */
231 val->intval = ~val->intval & 0x7fff;
232 val->intval++;
233 val->intval *= -1;
234 }
235 val->intval *= 1562500 / chip->pdata->r_sns;
236 } else {
237 return -EINVAL;
238 }
239 break;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900240 default:
241 return -EINVAL;
242 }
243 return 0;
244}
245
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400246static int max17042_write_verify_reg(struct i2c_client *client,
247 u8 reg, u16 value)
248{
249 int retries = 8;
250 int ret;
251 u16 read_value;
252
253 do {
254 ret = i2c_smbus_write_word_data(client, reg, value);
255 read_value = max17042_read_reg(client, reg);
256 if (read_value != value) {
257 ret = -EIO;
258 retries--;
259 }
260 } while (retries && read_value != value);
261
262 if (ret < 0)
263 dev_err(&client->dev, "%s: err %d\n", __func__, ret);
264
265 return ret;
266}
267
268static inline void max17042_override_por(
269 struct i2c_client *client, u8 reg, u16 value)
270{
271 if (value)
272 max17042_write_reg(client, reg, value);
273}
274
275static inline void max10742_unlock_model(struct max17042_chip *chip)
276{
277 struct i2c_client *client = chip->client;
278 max17042_write_reg(client, MAX17042_MLOCKReg1, MODEL_UNLOCK1);
279 max17042_write_reg(client, MAX17042_MLOCKReg2, MODEL_UNLOCK2);
280}
281
282static inline void max10742_lock_model(struct max17042_chip *chip)
283{
284 struct i2c_client *client = chip->client;
285 max17042_write_reg(client, MAX17042_MLOCKReg1, MODEL_LOCK1);
286 max17042_write_reg(client, MAX17042_MLOCKReg2, MODEL_LOCK2);
287}
288
289static inline void max17042_write_model_data(struct max17042_chip *chip,
290 u8 addr, int size)
291{
292 struct i2c_client *client = chip->client;
293 int i;
294 for (i = 0; i < size; i++)
295 max17042_write_reg(client, addr + i,
296 chip->pdata->config_data->cell_char_tbl[i]);
297}
298
299static inline void max17042_read_model_data(struct max17042_chip *chip,
300 u8 addr, u16 *data, int size)
301{
302 struct i2c_client *client = chip->client;
303 int i;
304
305 for (i = 0; i < size; i++)
306 data[i] = max17042_read_reg(client, addr + i);
307}
308
309static inline int max17042_model_data_compare(struct max17042_chip *chip,
310 u16 *data1, u16 *data2, int size)
311{
312 int i;
313
314 if (memcmp(data1, data2, size)) {
315 dev_err(&chip->client->dev, "%s compare failed\n", __func__);
316 for (i = 0; i < size; i++)
317 dev_info(&chip->client->dev, "0x%x, 0x%x",
318 data1[i], data2[i]);
319 dev_info(&chip->client->dev, "\n");
320 return -EINVAL;
321 }
322 return 0;
323}
324
325static int max17042_init_model(struct max17042_chip *chip)
326{
327 int ret;
Dan Carpenter1ef3d8f2012-03-15 14:37:32 +0300328 int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl);
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400329 u16 *temp_data;
330
Dan Carpenter1ef3d8f2012-03-15 14:37:32 +0300331 temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL);
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400332 if (!temp_data)
333 return -ENOMEM;
334
335 max10742_unlock_model(chip);
336 max17042_write_model_data(chip, MAX17042_MODELChrTbl,
337 table_size);
338 max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data,
339 table_size);
340
341 ret = max17042_model_data_compare(
342 chip,
343 chip->pdata->config_data->cell_char_tbl,
344 temp_data,
345 table_size);
346
347 max10742_lock_model(chip);
348 kfree(temp_data);
349
350 return ret;
351}
352
353static int max17042_verify_model_lock(struct max17042_chip *chip)
354{
355 int i;
Dan Carpenter1ef3d8f2012-03-15 14:37:32 +0300356 int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl);
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400357 u16 *temp_data;
358 int ret = 0;
359
Dan Carpenter1ef3d8f2012-03-15 14:37:32 +0300360 temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL);
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400361 if (!temp_data)
362 return -ENOMEM;
363
364 max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data,
365 table_size);
366 for (i = 0; i < table_size; i++)
367 if (temp_data[i])
368 ret = -EINVAL;
369
370 kfree(temp_data);
371 return ret;
372}
373
374static void max17042_write_config_regs(struct max17042_chip *chip)
375{
376 struct max17042_config_data *config = chip->pdata->config_data;
377
378 max17042_write_reg(chip->client, MAX17042_CONFIG, config->config);
379 max17042_write_reg(chip->client, MAX17042_LearnCFG, config->learn_cfg);
380 max17042_write_reg(chip->client, MAX17042_FilterCFG,
381 config->filter_cfg);
382 max17042_write_reg(chip->client, MAX17042_RelaxCFG, config->relax_cfg);
383}
384
385static void max17042_write_custom_regs(struct max17042_chip *chip)
386{
387 struct max17042_config_data *config = chip->pdata->config_data;
388
389 max17042_write_verify_reg(chip->client, MAX17042_RCOMP0,
390 config->rcomp0);
391 max17042_write_verify_reg(chip->client, MAX17042_TempCo,
392 config->tcompc0);
393 max17042_write_reg(chip->client, MAX17042_EmptyTempCo,
394 config->empty_tempco);
395 max17042_write_verify_reg(chip->client, MAX17042_K_empty0,
396 config->kempty0);
397 max17042_write_verify_reg(chip->client, MAX17042_ICHGTerm,
398 config->ichgt_term);
399}
400
401static void max17042_update_capacity_regs(struct max17042_chip *chip)
402{
403 struct max17042_config_data *config = chip->pdata->config_data;
404
405 max17042_write_verify_reg(chip->client, MAX17042_FullCAP,
406 config->fullcap);
407 max17042_write_reg(chip->client, MAX17042_DesignCap,
408 config->design_cap);
409 max17042_write_verify_reg(chip->client, MAX17042_FullCAPNom,
410 config->fullcapnom);
411}
412
413static void max17042_reset_vfsoc0_reg(struct max17042_chip *chip)
414{
415 u16 vfSoc;
416
417 vfSoc = max17042_read_reg(chip->client, MAX17042_VFSOC);
418 max17042_write_reg(chip->client, MAX17042_VFSOC0Enable, VFSOC0_UNLOCK);
419 max17042_write_verify_reg(chip->client, MAX17042_VFSOC0, vfSoc);
420 max17042_write_reg(chip->client, MAX17042_VFSOC0Enable, VFSOC0_LOCK);
421}
422
423static void max17042_load_new_capacity_params(struct max17042_chip *chip)
424{
425 u16 full_cap0, rep_cap, dq_acc, vfSoc;
426 u32 rem_cap;
427
428 struct max17042_config_data *config = chip->pdata->config_data;
429
430 full_cap0 = max17042_read_reg(chip->client, MAX17042_FullCAP0);
431 vfSoc = max17042_read_reg(chip->client, MAX17042_VFSOC);
432
433 /* fg_vfSoc needs to shifted by 8 bits to get the
434 * perc in 1% accuracy, to get the right rem_cap multiply
435 * full_cap0, fg_vfSoc and devide by 100
436 */
437 rem_cap = ((vfSoc >> 8) * full_cap0) / 100;
438 max17042_write_verify_reg(chip->client, MAX17042_RemCap, (u16)rem_cap);
439
440 rep_cap = (u16)rem_cap;
441 max17042_write_verify_reg(chip->client, MAX17042_RepCap, rep_cap);
442
443 /* Write dQ_acc to 200% of Capacity and dP_acc to 200% */
444 dq_acc = config->fullcap / dQ_ACC_DIV;
445 max17042_write_verify_reg(chip->client, MAX17042_dQacc, dq_acc);
446 max17042_write_verify_reg(chip->client, MAX17042_dPacc, dP_ACC_200);
447
448 max17042_write_verify_reg(chip->client, MAX17042_FullCAP,
449 config->fullcap);
450 max17042_write_reg(chip->client, MAX17042_DesignCap,
451 config->design_cap);
452 max17042_write_verify_reg(chip->client, MAX17042_FullCAPNom,
453 config->fullcapnom);
454}
455
456/*
457 * Block write all the override values coming from platform data.
458 * This function MUST be called before the POR initialization proceedure
459 * specified by maxim.
460 */
461static inline void max17042_override_por_values(struct max17042_chip *chip)
462{
463 struct i2c_client *client = chip->client;
464 struct max17042_config_data *config = chip->pdata->config_data;
465
466 max17042_override_por(client, MAX17042_TGAIN, config->tgain);
467 max17042_override_por(client, MAx17042_TOFF, config->toff);
468 max17042_override_por(client, MAX17042_CGAIN, config->cgain);
469 max17042_override_por(client, MAX17042_COFF, config->coff);
470
471 max17042_override_por(client, MAX17042_VALRT_Th, config->valrt_thresh);
472 max17042_override_por(client, MAX17042_TALRT_Th, config->talrt_thresh);
473 max17042_override_por(client, MAX17042_SALRT_Th,
474 config->soc_alrt_thresh);
475 max17042_override_por(client, MAX17042_CONFIG, config->config);
476 max17042_override_por(client, MAX17042_SHDNTIMER, config->shdntimer);
477
478 max17042_override_por(client, MAX17042_DesignCap, config->design_cap);
479 max17042_override_por(client, MAX17042_ICHGTerm, config->ichgt_term);
480
481 max17042_override_por(client, MAX17042_AtRate, config->at_rate);
482 max17042_override_por(client, MAX17042_LearnCFG, config->learn_cfg);
483 max17042_override_por(client, MAX17042_FilterCFG, config->filter_cfg);
484 max17042_override_por(client, MAX17042_RelaxCFG, config->relax_cfg);
485 max17042_override_por(client, MAX17042_MiscCFG, config->misc_cfg);
486 max17042_override_por(client, MAX17042_MaskSOC, config->masksoc);
487
488 max17042_override_por(client, MAX17042_FullCAP, config->fullcap);
489 max17042_override_por(client, MAX17042_FullCAPNom, config->fullcapnom);
490 max17042_override_por(client, MAX17042_SOC_empty, config->socempty);
491 max17042_override_por(client, MAX17042_LAvg_empty, config->lavg_empty);
492 max17042_override_por(client, MAX17042_dQacc, config->dqacc);
493 max17042_override_por(client, MAX17042_dPacc, config->dpacc);
494
495 max17042_override_por(client, MAX17042_V_empty, config->vempty);
496 max17042_override_por(client, MAX17042_TempNom, config->temp_nom);
497 max17042_override_por(client, MAX17042_TempLim, config->temp_lim);
498 max17042_override_por(client, MAX17042_FCTC, config->fctc);
499 max17042_override_por(client, MAX17042_RCOMP0, config->rcomp0);
500 max17042_override_por(client, MAX17042_TempCo, config->tcompc0);
501 max17042_override_por(client, MAX17042_EmptyTempCo,
502 config->empty_tempco);
503 max17042_override_por(client, MAX17042_K_empty0, config->kempty0);
504}
505
506static int max17042_init_chip(struct max17042_chip *chip)
507{
508 int ret;
509 int val;
510
511 max17042_override_por_values(chip);
512 /* After Power up, the MAX17042 requires 500mS in order
513 * to perform signal debouncing and initial SOC reporting
514 */
515 msleep(500);
516
517 /* Initialize configaration */
518 max17042_write_config_regs(chip);
519
520 /* write cell characterization data */
521 ret = max17042_init_model(chip);
522 if (ret) {
523 dev_err(&chip->client->dev, "%s init failed\n",
524 __func__);
525 return -EIO;
526 }
527 max17042_verify_model_lock(chip);
528 if (ret) {
529 dev_err(&chip->client->dev, "%s lock verify failed\n",
530 __func__);
531 return -EIO;
532 }
533 /* write custom parameters */
534 max17042_write_custom_regs(chip);
535
536 /* update capacity params */
537 max17042_update_capacity_regs(chip);
538
539 /* delay must be atleast 350mS to allow VFSOC
540 * to be calculated from the new configuration
541 */
542 msleep(350);
543
544 /* reset vfsoc0 reg */
545 max17042_reset_vfsoc0_reg(chip);
546
547 /* load new capacity params */
548 max17042_load_new_capacity_params(chip);
549
550 /* Init complete, Clear the POR bit */
551 val = max17042_read_reg(chip->client, MAX17042_STATUS);
552 max17042_write_reg(chip->client, MAX17042_STATUS,
553 val & (~STATUS_POR_BIT));
554 return 0;
555}
556
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -0800557static void max17042_set_soc_threshold(struct max17042_chip *chip, u16 off)
558{
559 u16 soc, soc_tr;
560
561 /* program interrupt thesholds such that we should
562 * get interrupt for every 'off' perc change in the soc
563 */
564 soc = max17042_read_reg(chip->client, MAX17042_RepSOC) >> 8;
565 soc_tr = (soc + off) << 8;
566 soc_tr |= (soc - off);
567 max17042_write_reg(chip->client, MAX17042_SALRT_Th, soc_tr);
568}
569
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -0800570static irqreturn_t max17042_thread_handler(int id, void *dev)
571{
572 struct max17042_chip *chip = dev;
573 u16 val;
574
575 val = max17042_read_reg(chip->client, MAX17042_STATUS);
Ramakrishna Pallala5cdd4d72012-03-21 03:03:16 +0530576 if ((val & STATUS_INTR_SOCMIN_BIT) ||
577 (val & STATUS_INTR_SOCMAX_BIT)) {
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -0800578 dev_info(&chip->client->dev, "SOC threshold INTR\n");
579 max17042_set_soc_threshold(chip, 1);
580 }
581
582 power_supply_changed(&chip->battery);
583 return IRQ_HANDLED;
584}
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400585
586static void max17042_init_worker(struct work_struct *work)
587{
588 struct max17042_chip *chip = container_of(work,
589 struct max17042_chip, work);
590 int ret;
591
592 /* Initialize registers according to values from the platform data */
593 if (chip->pdata->enable_por_init && chip->pdata->config_data) {
594 ret = max17042_init_chip(chip);
595 if (ret)
596 return;
597 }
598
599 chip->init_complete = 1;
600}
601
Karol Lewandowski38322462012-02-22 19:06:22 +0100602#ifdef CONFIG_OF
603static struct max17042_platform_data *
604max17042_get_pdata(struct device *dev)
605{
606 struct device_node *np = dev->of_node;
607 u32 prop;
608 struct max17042_platform_data *pdata;
609
610 if (!np)
611 return dev->platform_data;
612
613 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
614 if (!pdata)
615 return NULL;
616
617 /*
618 * Require current sense resistor value to be specified for
619 * current-sense functionality to be enabled at all.
620 */
621 if (of_property_read_u32(np, "maxim,rsns-microohm", &prop) == 0) {
622 pdata->r_sns = prop;
623 pdata->enable_current_sense = true;
624 }
625
626 return pdata;
627}
628#else
629static struct max17042_platform_data *
630max17042_get_pdata(struct device *dev)
631{
632 return dev->platform_data;
633}
634#endif
635
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900636static int __devinit max17042_probe(struct i2c_client *client,
637 const struct i2c_device_id *id)
638{
639 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
640 struct max17042_chip *chip;
641 int ret;
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400642 int reg;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900643
644 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
645 return -EIO;
646
Karol Lewandowski2f3b4342012-02-22 19:06:20 +0100647 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900648 if (!chip)
649 return -ENOMEM;
650
651 chip->client = client;
Karol Lewandowski38322462012-02-22 19:06:22 +0100652 chip->pdata = max17042_get_pdata(&client->dev);
653 if (!chip->pdata) {
654 dev_err(&client->dev, "no platform data provided\n");
655 return -EINVAL;
656 }
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900657
658 i2c_set_clientdata(client, chip);
659
660 chip->battery.name = "max17042_battery";
661 chip->battery.type = POWER_SUPPLY_TYPE_BATTERY;
662 chip->battery.get_property = max17042_get_property;
663 chip->battery.properties = max17042_battery_props;
664 chip->battery.num_properties = ARRAY_SIZE(max17042_battery_props);
665
Donggeun Kim086ef502011-06-30 18:07:41 +0900666 /* When current is not measured,
667 * CURRENT_NOW and CURRENT_AVG properties should be invisible. */
668 if (!chip->pdata->enable_current_sense)
669 chip->battery.num_properties -= 2;
670
Philip Rakity4cfa8922011-08-12 21:18:18 -0700671 if (chip->pdata->r_sns == 0)
672 chip->pdata->r_sns = MAX17042_DEFAULT_SNS_RESISTOR;
673
Donggeun Kim086ef502011-06-30 18:07:41 +0900674 if (chip->pdata->init_data)
675 max17042_set_reg(client, chip->pdata->init_data,
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400676 chip->pdata->num_init_data);
Donggeun Kim086ef502011-06-30 18:07:41 +0900677
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900678 if (!chip->pdata->enable_current_sense) {
679 max17042_write_reg(client, MAX17042_CGAIN, 0x0000);
680 max17042_write_reg(client, MAX17042_MiscCFG, 0x0003);
681 max17042_write_reg(client, MAX17042_LearnCFG, 0x0007);
682 }
683
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -0800684 if (client->irq) {
Ramakrishna Pallala5cdd4d72012-03-21 03:03:16 +0530685 ret = request_threaded_irq(client->irq, NULL,
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -0800686 max17042_thread_handler,
Ramakrishna Pallala5cdd4d72012-03-21 03:03:16 +0530687 IRQF_TRIGGER_FALLING,
688 chip->battery.name, chip);
Ramakrishna Pallalae5f38722012-01-24 09:26:06 -0800689 if (!ret) {
690 reg = max17042_read_reg(client, MAX17042_CONFIG);
691 reg |= CONFIG_ALRT_BIT_ENBL;
692 max17042_write_reg(client, MAX17042_CONFIG, reg);
693 max17042_set_soc_threshold(chip, 1);
694 } else
695 dev_err(&client->dev, "%s(): cannot get IRQ\n",
696 __func__);
697 }
698
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400699 reg = max17042_read_reg(chip->client, MAX17042_STATUS);
700
701 if (reg & STATUS_POR_BIT) {
702 INIT_WORK(&chip->work, max17042_init_worker);
703 schedule_work(&chip->work);
704 } else {
705 chip->init_complete = 1;
706 }
707
708 ret = power_supply_register(&client->dev, &chip->battery);
Karol Lewandowski2f3b4342012-02-22 19:06:20 +0100709 if (ret)
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400710 dev_err(&client->dev, "failed: power supply register\n");
Ramakrishna Pallalaf3a71a62012-03-13 22:03:52 +0400711 return ret;
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900712}
713
714static int __devexit max17042_remove(struct i2c_client *client)
715{
716 struct max17042_chip *chip = i2c_get_clientdata(client);
717
Ramakrishna Pallalabb28da92012-03-26 15:38:26 +0530718 if (client->irq)
719 free_irq(client->irq, chip);
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900720 power_supply_unregister(&chip->battery);
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900721 return 0;
722}
723
Karol Lewandowski38322462012-02-22 19:06:22 +0100724#ifdef CONFIG_OF
725static const struct of_device_id max17042_dt_match[] = {
726 { .compatible = "maxim,max17042" },
727 { },
728};
729MODULE_DEVICE_TABLE(of, max17042_dt_match);
730#endif
731
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900732static const struct i2c_device_id max17042_id[] = {
733 { "max17042", 0 },
734 { }
735};
736MODULE_DEVICE_TABLE(i2c, max17042_id);
737
738static struct i2c_driver max17042_i2c_driver = {
739 .driver = {
740 .name = "max17042",
Karol Lewandowski38322462012-02-22 19:06:22 +0100741 .of_match_table = of_match_ptr(max17042_dt_match),
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900742 },
743 .probe = max17042_probe,
744 .remove = __devexit_p(max17042_remove),
745 .id_table = max17042_id,
746};
Axel Lin5ff92e72012-01-21 14:42:54 +0800747module_i2c_driver(max17042_i2c_driver);
MyungJoo Ham359ab9f2011-01-14 14:46:11 +0900748
749MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
750MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
751MODULE_LICENSE("GPL");