power/reset: at91: big endian fixes for atsama5d3x
[linux-2.6-block.git] / drivers / power / max17042_battery.c
CommitLineData
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>
e5f3872d 30#include <linux/interrupt.h>
48bc1774 31#include <linux/pm.h>
359ab9f5
MH
32#include <linux/mod_devicetable.h>
33#include <linux/power_supply.h>
34#include <linux/power/max17042_battery.h>
3832246d 35#include <linux/of.h>
39e7213e 36#include <linux/regmap.h>
359ab9f5 37
f3a71a6e
RP
38/* Status register bits */
39#define STATUS_POR_BIT (1 << 1)
40#define STATUS_BST_BIT (1 << 3)
41#define STATUS_VMN_BIT (1 << 8)
42#define STATUS_TMN_BIT (1 << 9)
43#define STATUS_SMN_BIT (1 << 10)
44#define STATUS_BI_BIT (1 << 11)
45#define STATUS_VMX_BIT (1 << 12)
46#define STATUS_TMX_BIT (1 << 13)
47#define STATUS_SMX_BIT (1 << 14)
48#define STATUS_BR_BIT (1 << 15)
49
e5f3872d
RP
50/* Interrupt mask bits */
51#define CONFIG_ALRT_BIT_ENBL (1 << 2)
5cdd4d7f
RP
52#define STATUS_INTR_SOCMIN_BIT (1 << 10)
53#define STATUS_INTR_SOCMAX_BIT (1 << 14)
e5f3872d 54
f3a71a6e
RP
55#define VFSOC0_LOCK 0x0000
56#define VFSOC0_UNLOCK 0x0080
57#define MODEL_UNLOCK1 0X0059
58#define MODEL_UNLOCK2 0X00C4
59#define MODEL_LOCK1 0X0000
60#define MODEL_LOCK2 0X0000
61
62#define dQ_ACC_DIV 0x4
63#define dP_ACC_100 0x1900
64#define dP_ACC_200 0x3200
65
9a8422d2
RP
66#define MAX17042_IC_VERSION 0x0092
67#define MAX17047_IC_VERSION 0x00AC /* same for max17050 */
68
359ab9f5
MH
69struct max17042_chip {
70 struct i2c_client *client;
39e7213e 71 struct regmap *regmap;
297d716f 72 struct power_supply *battery;
9a8422d2 73 enum max170xx_chip_type chip_type;
359ab9f5 74 struct max17042_platform_data *pdata;
f3a71a6e
RP
75 struct work_struct work;
76 int init_complete;
359ab9f5
MH
77};
78
359ab9f5 79static enum power_supply_property max17042_battery_props[] = {
086ef502
DK
80 POWER_SUPPLY_PROP_PRESENT,
81 POWER_SUPPLY_PROP_CYCLE_COUNT,
82 POWER_SUPPLY_PROP_VOLTAGE_MAX,
83 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
359ab9f5
MH
84 POWER_SUPPLY_PROP_VOLTAGE_NOW,
85 POWER_SUPPLY_PROP_VOLTAGE_AVG,
a2ebfe2f 86 POWER_SUPPLY_PROP_VOLTAGE_OCV,
359ab9f5 87 POWER_SUPPLY_PROP_CAPACITY,
086ef502 88 POWER_SUPPLY_PROP_CHARGE_FULL,
5fc55bc8 89 POWER_SUPPLY_PROP_CHARGE_COUNTER,
086ef502
DK
90 POWER_SUPPLY_PROP_TEMP,
91 POWER_SUPPLY_PROP_CURRENT_NOW,
92 POWER_SUPPLY_PROP_CURRENT_AVG,
359ab9f5
MH
93};
94
95static int max17042_get_property(struct power_supply *psy,
96 enum power_supply_property psp,
97 union power_supply_propval *val)
98{
297d716f 99 struct max17042_chip *chip = power_supply_get_drvdata(psy);
39e7213e 100 struct regmap *map = chip->regmap;
60a1f6e4 101 int ret;
39e7213e 102 u32 data;
359ab9f5 103
f3a71a6e
RP
104 if (!chip->init_complete)
105 return -EAGAIN;
106
359ab9f5 107 switch (psp) {
086ef502 108 case POWER_SUPPLY_PROP_PRESENT:
39e7213e 109 ret = regmap_read(map, MAX17042_STATUS, &data);
60a1f6e4
RP
110 if (ret < 0)
111 return ret;
112
39e7213e 113 if (data & MAX17042_STATUS_BattAbsent)
086ef502
DK
114 val->intval = 0;
115 else
116 val->intval = 1;
117 break;
118 case POWER_SUPPLY_PROP_CYCLE_COUNT:
39e7213e 119 ret = regmap_read(map, MAX17042_Cycles, &data);
60a1f6e4
RP
120 if (ret < 0)
121 return ret;
122
39e7213e 123 val->intval = data;
086ef502
DK
124 break;
125 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
39e7213e 126 ret = regmap_read(map, MAX17042_MinMaxVolt, &data);
60a1f6e4
RP
127 if (ret < 0)
128 return ret;
129
39e7213e 130 val->intval = data >> 8;
086ef502
DK
131 val->intval *= 20000; /* Units of LSB = 20mV */
132 break;
133 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
9a8422d2 134 if (chip->chip_type == MAX17042)
39e7213e 135 ret = regmap_read(map, MAX17042_V_empty, &data);
9a8422d2 136 else
39e7213e 137 ret = regmap_read(map, MAX17047_V_empty, &data);
60a1f6e4
RP
138 if (ret < 0)
139 return ret;
140
39e7213e 141 val->intval = data >> 7;
086ef502
DK
142 val->intval *= 10000; /* Units of LSB = 10mV */
143 break;
359ab9f5 144 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
39e7213e 145 ret = regmap_read(map, MAX17042_VCELL, &data);
60a1f6e4
RP
146 if (ret < 0)
147 return ret;
148
39e7213e 149 val->intval = data * 625 / 8;
359ab9f5
MH
150 break;
151 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
39e7213e 152 ret = regmap_read(map, MAX17042_AvgVCELL, &data);
60a1f6e4
RP
153 if (ret < 0)
154 return ret;
155
39e7213e 156 val->intval = data * 625 / 8;
a2ebfe2f
RP
157 break;
158 case POWER_SUPPLY_PROP_VOLTAGE_OCV:
39e7213e 159 ret = regmap_read(map, MAX17042_OCVInternal, &data);
a2ebfe2f
RP
160 if (ret < 0)
161 return ret;
162
39e7213e 163 val->intval = data * 625 / 8;
359ab9f5
MH
164 break;
165 case POWER_SUPPLY_PROP_CAPACITY:
39e7213e 166 ret = regmap_read(map, MAX17042_RepSOC, &data);
60a1f6e4
RP
167 if (ret < 0)
168 return ret;
169
39e7213e 170 val->intval = data >> 8;
359ab9f5 171 break;
086ef502 172 case POWER_SUPPLY_PROP_CHARGE_FULL:
39e7213e 173 ret = regmap_read(map, MAX17042_FullCAP, &data);
60a1f6e4
RP
174 if (ret < 0)
175 return ret;
176
39e7213e 177 val->intval = data * 1000 / 2;
5fc55bc8
RP
178 break;
179 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
39e7213e 180 ret = regmap_read(map, MAX17042_QH, &data);
5fc55bc8
RP
181 if (ret < 0)
182 return ret;
183
39e7213e 184 val->intval = data * 1000 / 2;
086ef502
DK
185 break;
186 case POWER_SUPPLY_PROP_TEMP:
39e7213e 187 ret = regmap_read(map, MAX17042_TEMP, &data);
60a1f6e4
RP
188 if (ret < 0)
189 return ret;
190
39e7213e 191 val->intval = data;
086ef502
DK
192 /* The value is signed. */
193 if (val->intval & 0x8000) {
194 val->intval = (0x7fff & ~val->intval) + 1;
195 val->intval *= -1;
196 }
197 /* The value is converted into deci-centigrade scale */
198 /* Units of LSB = 1 / 256 degree Celsius */
199 val->intval = val->intval * 10 / 256;
200 break;
201 case POWER_SUPPLY_PROP_CURRENT_NOW:
202 if (chip->pdata->enable_current_sense) {
39e7213e 203 ret = regmap_read(map, MAX17042_Current, &data);
60a1f6e4
RP
204 if (ret < 0)
205 return ret;
206
39e7213e 207 val->intval = data;
086ef502
DK
208 if (val->intval & 0x8000) {
209 /* Negative */
210 val->intval = ~val->intval & 0x7fff;
211 val->intval++;
212 val->intval *= -1;
213 }
91d8b0d6 214 val->intval *= 1562500 / chip->pdata->r_sns;
086ef502
DK
215 } else {
216 return -EINVAL;
217 }
218 break;
219 case POWER_SUPPLY_PROP_CURRENT_AVG:
220 if (chip->pdata->enable_current_sense) {
39e7213e 221 ret = regmap_read(map, MAX17042_AvgCurrent, &data);
60a1f6e4
RP
222 if (ret < 0)
223 return ret;
224
39e7213e 225 val->intval = data;
086ef502
DK
226 if (val->intval & 0x8000) {
227 /* Negative */
228 val->intval = ~val->intval & 0x7fff;
229 val->intval++;
230 val->intval *= -1;
231 }
232 val->intval *= 1562500 / chip->pdata->r_sns;
233 } else {
234 return -EINVAL;
235 }
236 break;
359ab9f5
MH
237 default:
238 return -EINVAL;
239 }
240 return 0;
241}
242
39e7213e 243static int max17042_write_verify_reg(struct regmap *map, u8 reg, u32 value)
f3a71a6e
RP
244{
245 int retries = 8;
246 int ret;
39e7213e 247 u32 read_value;
f3a71a6e
RP
248
249 do {
39e7213e
JL
250 ret = regmap_write(map, reg, value);
251 regmap_read(map, reg, &read_value);
f3a71a6e
RP
252 if (read_value != value) {
253 ret = -EIO;
254 retries--;
255 }
256 } while (retries && read_value != value);
257
258 if (ret < 0)
39e7213e 259 pr_err("%s: err %d\n", __func__, ret);
f3a71a6e
RP
260
261 return ret;
262}
263
39e7213e
JL
264static inline void max17042_override_por(struct regmap *map,
265 u8 reg, u16 value)
f3a71a6e
RP
266{
267 if (value)
39e7213e 268 regmap_write(map, reg, value);
f3a71a6e
RP
269}
270
271static inline void max10742_unlock_model(struct max17042_chip *chip)
272{
39e7213e
JL
273 struct regmap *map = chip->regmap;
274 regmap_write(map, MAX17042_MLOCKReg1, MODEL_UNLOCK1);
275 regmap_write(map, MAX17042_MLOCKReg2, MODEL_UNLOCK2);
f3a71a6e
RP
276}
277
278static inline void max10742_lock_model(struct max17042_chip *chip)
279{
39e7213e
JL
280 struct regmap *map = chip->regmap;
281
282 regmap_write(map, MAX17042_MLOCKReg1, MODEL_LOCK1);
283 regmap_write(map, MAX17042_MLOCKReg2, MODEL_LOCK2);
f3a71a6e
RP
284}
285
286static inline void max17042_write_model_data(struct max17042_chip *chip,
287 u8 addr, int size)
288{
39e7213e 289 struct regmap *map = chip->regmap;
f3a71a6e
RP
290 int i;
291 for (i = 0; i < size; i++)
39e7213e
JL
292 regmap_write(map, addr + i,
293 chip->pdata->config_data->cell_char_tbl[i]);
f3a71a6e
RP
294}
295
296static inline void max17042_read_model_data(struct max17042_chip *chip,
39e7213e 297 u8 addr, u32 *data, int size)
f3a71a6e 298{
39e7213e 299 struct regmap *map = chip->regmap;
f3a71a6e
RP
300 int i;
301
302 for (i = 0; i < size; i++)
39e7213e 303 regmap_read(map, addr + i, &data[i]);
f3a71a6e
RP
304}
305
306static inline int max17042_model_data_compare(struct max17042_chip *chip,
307 u16 *data1, u16 *data2, int size)
308{
309 int i;
310
311 if (memcmp(data1, data2, size)) {
312 dev_err(&chip->client->dev, "%s compare failed\n", __func__);
313 for (i = 0; i < size; i++)
314 dev_info(&chip->client->dev, "0x%x, 0x%x",
315 data1[i], data2[i]);
316 dev_info(&chip->client->dev, "\n");
317 return -EINVAL;
318 }
319 return 0;
320}
321
322static int max17042_init_model(struct max17042_chip *chip)
323{
324 int ret;
1ef3d8fb 325 int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl);
39e7213e 326 u32 *temp_data;
f3a71a6e 327
1ef3d8fb 328 temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL);
f3a71a6e
RP
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,
39e7213e 341 (u16 *)temp_data,
f3a71a6e
RP
342 table_size);
343
344 max10742_lock_model(chip);
345 kfree(temp_data);
346
347 return ret;
348}
349
350static int max17042_verify_model_lock(struct max17042_chip *chip)
351{
352 int i;
1ef3d8fb 353 int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl);
39e7213e 354 u32 *temp_data;
f3a71a6e
RP
355 int ret = 0;
356
1ef3d8fb 357 temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL);
f3a71a6e
RP
358 if (!temp_data)
359 return -ENOMEM;
360
361 max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data,
362 table_size);
363 for (i = 0; i < table_size; i++)
364 if (temp_data[i])
365 ret = -EINVAL;
366
367 kfree(temp_data);
368 return ret;
369}
370
371static void max17042_write_config_regs(struct max17042_chip *chip)
372{
373 struct max17042_config_data *config = chip->pdata->config_data;
39e7213e 374 struct regmap *map = chip->regmap;
f3a71a6e 375
39e7213e
JL
376 regmap_write(map, MAX17042_CONFIG, config->config);
377 regmap_write(map, MAX17042_LearnCFG, config->learn_cfg);
378 regmap_write(map, MAX17042_FilterCFG,
f3a71a6e 379 config->filter_cfg);
39e7213e 380 regmap_write(map, MAX17042_RelaxCFG, config->relax_cfg);
9a8422d2 381 if (chip->chip_type == MAX17047)
39e7213e 382 regmap_write(map, MAX17047_FullSOCThr,
9a8422d2 383 config->full_soc_thresh);
f3a71a6e
RP
384}
385
386static void max17042_write_custom_regs(struct max17042_chip *chip)
387{
388 struct max17042_config_data *config = chip->pdata->config_data;
39e7213e 389 struct regmap *map = chip->regmap;
f3a71a6e 390
39e7213e
JL
391 max17042_write_verify_reg(map, MAX17042_RCOMP0, config->rcomp0);
392 max17042_write_verify_reg(map, MAX17042_TempCo, config->tcompc0);
393 max17042_write_verify_reg(map, MAX17042_ICHGTerm, config->ichgt_term);
9a8422d2 394 if (chip->chip_type == MAX17042) {
39e7213e
JL
395 regmap_write(map, MAX17042_EmptyTempCo, config->empty_tempco);
396 max17042_write_verify_reg(map, MAX17042_K_empty0,
9a8422d2
RP
397 config->kempty0);
398 } else {
39e7213e 399 max17042_write_verify_reg(map, MAX17047_QRTbl00,
9a8422d2 400 config->qrtbl00);
39e7213e 401 max17042_write_verify_reg(map, MAX17047_QRTbl10,
9a8422d2 402 config->qrtbl10);
39e7213e 403 max17042_write_verify_reg(map, MAX17047_QRTbl20,
9a8422d2 404 config->qrtbl20);
39e7213e 405 max17042_write_verify_reg(map, MAX17047_QRTbl30,
9a8422d2
RP
406 config->qrtbl30);
407 }
f3a71a6e
RP
408}
409
410static void max17042_update_capacity_regs(struct max17042_chip *chip)
411{
412 struct max17042_config_data *config = chip->pdata->config_data;
39e7213e 413 struct regmap *map = chip->regmap;
f3a71a6e 414
39e7213e 415 max17042_write_verify_reg(map, MAX17042_FullCAP,
f3a71a6e 416 config->fullcap);
39e7213e
JL
417 regmap_write(map, MAX17042_DesignCap, config->design_cap);
418 max17042_write_verify_reg(map, MAX17042_FullCAPNom,
f3a71a6e
RP
419 config->fullcapnom);
420}
421
422static void max17042_reset_vfsoc0_reg(struct max17042_chip *chip)
423{
39e7213e
JL
424 unsigned int vfSoc;
425 struct regmap *map = chip->regmap;
f3a71a6e 426
39e7213e
JL
427 regmap_read(map, MAX17042_VFSOC, &vfSoc);
428 regmap_write(map, MAX17042_VFSOC0Enable, VFSOC0_UNLOCK);
429 max17042_write_verify_reg(map, MAX17042_VFSOC0, vfSoc);
430 regmap_write(map, MAX17042_VFSOC0Enable, VFSOC0_LOCK);
f3a71a6e
RP
431}
432
433static void max17042_load_new_capacity_params(struct max17042_chip *chip)
434{
39e7213e 435 u32 full_cap0, rep_cap, dq_acc, vfSoc;
f3a71a6e
RP
436 u32 rem_cap;
437
438 struct max17042_config_data *config = chip->pdata->config_data;
39e7213e 439 struct regmap *map = chip->regmap;
f3a71a6e 440
39e7213e
JL
441 regmap_read(map, MAX17042_FullCAP0, &full_cap0);
442 regmap_read(map, MAX17042_VFSOC, &vfSoc);
f3a71a6e
RP
443
444 /* fg_vfSoc needs to shifted by 8 bits to get the
445 * perc in 1% accuracy, to get the right rem_cap multiply
446 * full_cap0, fg_vfSoc and devide by 100
447 */
448 rem_cap = ((vfSoc >> 8) * full_cap0) / 100;
39e7213e 449 max17042_write_verify_reg(map, MAX17042_RemCap, rem_cap);
f3a71a6e 450
39e7213e
JL
451 rep_cap = rem_cap;
452 max17042_write_verify_reg(map, MAX17042_RepCap, rep_cap);
f3a71a6e
RP
453
454 /* Write dQ_acc to 200% of Capacity and dP_acc to 200% */
455 dq_acc = config->fullcap / dQ_ACC_DIV;
39e7213e
JL
456 max17042_write_verify_reg(map, MAX17042_dQacc, dq_acc);
457 max17042_write_verify_reg(map, MAX17042_dPacc, dP_ACC_200);
f3a71a6e 458
39e7213e 459 max17042_write_verify_reg(map, MAX17042_FullCAP,
f3a71a6e 460 config->fullcap);
39e7213e 461 regmap_write(map, MAX17042_DesignCap,
f3a71a6e 462 config->design_cap);
39e7213e 463 max17042_write_verify_reg(map, MAX17042_FullCAPNom,
f3a71a6e 464 config->fullcapnom);
9a8422d2 465 /* Update SOC register with new SOC */
39e7213e 466 regmap_write(map, MAX17042_RepSOC, vfSoc);
f3a71a6e
RP
467}
468
469/*
470 * Block write all the override values coming from platform data.
471 * This function MUST be called before the POR initialization proceedure
472 * specified by maxim.
473 */
474static inline void max17042_override_por_values(struct max17042_chip *chip)
475{
39e7213e 476 struct regmap *map = chip->regmap;
f3a71a6e
RP
477 struct max17042_config_data *config = chip->pdata->config_data;
478
39e7213e
JL
479 max17042_override_por(map, MAX17042_TGAIN, config->tgain);
480 max17042_override_por(map, MAx17042_TOFF, config->toff);
481 max17042_override_por(map, MAX17042_CGAIN, config->cgain);
482 max17042_override_por(map, MAX17042_COFF, config->coff);
483
484 max17042_override_por(map, MAX17042_VALRT_Th, config->valrt_thresh);
485 max17042_override_por(map, MAX17042_TALRT_Th, config->talrt_thresh);
486 max17042_override_por(map, MAX17042_SALRT_Th,
487 config->soc_alrt_thresh);
488 max17042_override_por(map, MAX17042_CONFIG, config->config);
489 max17042_override_por(map, MAX17042_SHDNTIMER, config->shdntimer);
490
491 max17042_override_por(map, MAX17042_DesignCap, config->design_cap);
492 max17042_override_por(map, MAX17042_ICHGTerm, config->ichgt_term);
493
494 max17042_override_por(map, MAX17042_AtRate, config->at_rate);
495 max17042_override_por(map, MAX17042_LearnCFG, config->learn_cfg);
496 max17042_override_por(map, MAX17042_FilterCFG, config->filter_cfg);
497 max17042_override_por(map, MAX17042_RelaxCFG, config->relax_cfg);
498 max17042_override_por(map, MAX17042_MiscCFG, config->misc_cfg);
499 max17042_override_por(map, MAX17042_MaskSOC, config->masksoc);
500
501 max17042_override_por(map, MAX17042_FullCAP, config->fullcap);
502 max17042_override_por(map, MAX17042_FullCAPNom, config->fullcapnom);
9a8422d2 503 if (chip->chip_type == MAX17042)
39e7213e 504 max17042_override_por(map, MAX17042_SOC_empty,
9a8422d2 505 config->socempty);
39e7213e
JL
506 max17042_override_por(map, MAX17042_LAvg_empty, config->lavg_empty);
507 max17042_override_por(map, MAX17042_dQacc, config->dqacc);
508 max17042_override_por(map, MAX17042_dPacc, config->dpacc);
f3a71a6e 509
9a8422d2 510 if (chip->chip_type == MAX17042)
39e7213e 511 max17042_override_por(map, MAX17042_V_empty, config->vempty);
9a8422d2 512 else
39e7213e
JL
513 max17042_override_por(map, MAX17047_V_empty, config->vempty);
514 max17042_override_por(map, MAX17042_TempNom, config->temp_nom);
515 max17042_override_por(map, MAX17042_TempLim, config->temp_lim);
516 max17042_override_por(map, MAX17042_FCTC, config->fctc);
517 max17042_override_por(map, MAX17042_RCOMP0, config->rcomp0);
518 max17042_override_por(map, MAX17042_TempCo, config->tcompc0);
9a8422d2 519 if (chip->chip_type) {
39e7213e
JL
520 max17042_override_por(map, MAX17042_EmptyTempCo,
521 config->empty_tempco);
522 max17042_override_por(map, MAX17042_K_empty0,
523 config->kempty0);
9a8422d2 524 }
f3a71a6e
RP
525}
526
527static int max17042_init_chip(struct max17042_chip *chip)
528{
39e7213e 529 struct regmap *map = chip->regmap;
f3a71a6e 530 int ret;
f3a71a6e
RP
531
532 max17042_override_por_values(chip);
533 /* After Power up, the MAX17042 requires 500mS in order
534 * to perform signal debouncing and initial SOC reporting
535 */
536 msleep(500);
537
538 /* Initialize configaration */
539 max17042_write_config_regs(chip);
540
541 /* write cell characterization data */
542 ret = max17042_init_model(chip);
543 if (ret) {
544 dev_err(&chip->client->dev, "%s init failed\n",
545 __func__);
546 return -EIO;
547 }
a879f19f
AC
548
549 ret = max17042_verify_model_lock(chip);
f3a71a6e
RP
550 if (ret) {
551 dev_err(&chip->client->dev, "%s lock verify failed\n",
552 __func__);
553 return -EIO;
554 }
555 /* write custom parameters */
556 max17042_write_custom_regs(chip);
557
558 /* update capacity params */
559 max17042_update_capacity_regs(chip);
560
561 /* delay must be atleast 350mS to allow VFSOC
562 * to be calculated from the new configuration
563 */
564 msleep(350);
565
566 /* reset vfsoc0 reg */
567 max17042_reset_vfsoc0_reg(chip);
568
569 /* load new capacity params */
570 max17042_load_new_capacity_params(chip);
571
572 /* Init complete, Clear the POR bit */
bc352686 573 regmap_update_bits(map, MAX17042_STATUS, STATUS_POR_BIT, 0x0);
f3a71a6e
RP
574 return 0;
575}
576
e5f3872d
RP
577static void max17042_set_soc_threshold(struct max17042_chip *chip, u16 off)
578{
39e7213e
JL
579 struct regmap *map = chip->regmap;
580 u32 soc, soc_tr;
e5f3872d
RP
581
582 /* program interrupt thesholds such that we should
583 * get interrupt for every 'off' perc change in the soc
584 */
39e7213e
JL
585 regmap_read(map, MAX17042_RepSOC, &soc);
586 soc >>= 8;
e5f3872d
RP
587 soc_tr = (soc + off) << 8;
588 soc_tr |= (soc - off);
39e7213e 589 regmap_write(map, MAX17042_SALRT_Th, soc_tr);
e5f3872d
RP
590}
591
e5f3872d
RP
592static irqreturn_t max17042_thread_handler(int id, void *dev)
593{
594 struct max17042_chip *chip = dev;
39e7213e 595 u32 val;
e5f3872d 596
39e7213e 597 regmap_read(chip->regmap, MAX17042_STATUS, &val);
5cdd4d7f
RP
598 if ((val & STATUS_INTR_SOCMIN_BIT) ||
599 (val & STATUS_INTR_SOCMAX_BIT)) {
e5f3872d
RP
600 dev_info(&chip->client->dev, "SOC threshold INTR\n");
601 max17042_set_soc_threshold(chip, 1);
602 }
603
297d716f 604 power_supply_changed(chip->battery);
e5f3872d
RP
605 return IRQ_HANDLED;
606}
f3a71a6e
RP
607
608static void max17042_init_worker(struct work_struct *work)
609{
610 struct max17042_chip *chip = container_of(work,
611 struct max17042_chip, work);
612 int ret;
613
614 /* Initialize registers according to values from the platform data */
615 if (chip->pdata->enable_por_init && chip->pdata->config_data) {
616 ret = max17042_init_chip(chip);
617 if (ret)
618 return;
619 }
620
621 chip->init_complete = 1;
622}
623
3832246d
KL
624#ifdef CONFIG_OF
625static struct max17042_platform_data *
626max17042_get_pdata(struct device *dev)
627{
628 struct device_node *np = dev->of_node;
629 u32 prop;
630 struct max17042_platform_data *pdata;
631
632 if (!np)
633 return dev->platform_data;
634
635 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
636 if (!pdata)
637 return NULL;
638
639 /*
640 * Require current sense resistor value to be specified for
641 * current-sense functionality to be enabled at all.
642 */
643 if (of_property_read_u32(np, "maxim,rsns-microohm", &prop) == 0) {
644 pdata->r_sns = prop;
645 pdata->enable_current_sense = true;
646 }
647
648 return pdata;
649}
650#else
651static struct max17042_platform_data *
652max17042_get_pdata(struct device *dev)
653{
654 return dev->platform_data;
655}
656#endif
657
e0291285 658static const struct regmap_config max17042_regmap_config = {
39e7213e
JL
659 .reg_bits = 8,
660 .val_bits = 16,
661 .val_format_endian = REGMAP_ENDIAN_NATIVE,
662};
663
297d716f
KK
664static const struct power_supply_desc max17042_psy_desc = {
665 .name = "max170xx_battery",
666 .type = POWER_SUPPLY_TYPE_BATTERY,
667 .get_property = max17042_get_property,
668 .properties = max17042_battery_props,
669 .num_properties = ARRAY_SIZE(max17042_battery_props),
670};
671
672static const struct power_supply_desc max17042_no_current_sense_psy_desc = {
673 .name = "max170xx_battery",
674 .type = POWER_SUPPLY_TYPE_BATTERY,
675 .get_property = max17042_get_property,
676 .properties = max17042_battery_props,
677 .num_properties = ARRAY_SIZE(max17042_battery_props) - 2,
678};
679
c8afa640 680static int max17042_probe(struct i2c_client *client,
359ab9f5
MH
681 const struct i2c_device_id *id)
682{
683 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
297d716f
KK
684 const struct power_supply_desc *max17042_desc = &max17042_psy_desc;
685 struct power_supply_config psy_cfg = {};
359ab9f5
MH
686 struct max17042_chip *chip;
687 int ret;
39e7213e
JL
688 int i;
689 u32 val;
359ab9f5
MH
690
691 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
692 return -EIO;
693
2f3b4342 694 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
359ab9f5
MH
695 if (!chip)
696 return -ENOMEM;
697
698 chip->client = client;
39e7213e
JL
699 chip->regmap = devm_regmap_init_i2c(client, &max17042_regmap_config);
700 if (IS_ERR(chip->regmap)) {
701 dev_err(&client->dev, "Failed to initialize regmap\n");
702 return -EINVAL;
703 }
704
3832246d
KL
705 chip->pdata = max17042_get_pdata(&client->dev);
706 if (!chip->pdata) {
707 dev_err(&client->dev, "no platform data provided\n");
708 return -EINVAL;
709 }
359ab9f5
MH
710
711 i2c_set_clientdata(client, chip);
297d716f 712 psy_cfg.drv_data = chip;
359ab9f5 713
39e7213e
JL
714 regmap_read(chip->regmap, MAX17042_DevName, &val);
715 if (val == MAX17042_IC_VERSION) {
9a8422d2
RP
716 dev_dbg(&client->dev, "chip type max17042 detected\n");
717 chip->chip_type = MAX17042;
39e7213e 718 } else if (val == MAX17047_IC_VERSION) {
9a8422d2
RP
719 dev_dbg(&client->dev, "chip type max17047/50 detected\n");
720 chip->chip_type = MAX17047;
721 } else {
39e7213e 722 dev_err(&client->dev, "device version mismatch: %x\n", val);
9a8422d2
RP
723 return -EIO;
724 }
725
086ef502
DK
726 /* When current is not measured,
727 * CURRENT_NOW and CURRENT_AVG properties should be invisible. */
728 if (!chip->pdata->enable_current_sense)
297d716f 729 max17042_desc = &max17042_no_current_sense_psy_desc;
086ef502 730
4cfa892c
PR
731 if (chip->pdata->r_sns == 0)
732 chip->pdata->r_sns = MAX17042_DEFAULT_SNS_RESISTOR;
733
086ef502 734 if (chip->pdata->init_data)
39e7213e
JL
735 for (i = 0; i < chip->pdata->num_init_data; i++)
736 regmap_write(chip->regmap,
737 chip->pdata->init_data[i].addr,
738 chip->pdata->init_data[i].data);
086ef502 739
359ab9f5 740 if (!chip->pdata->enable_current_sense) {
39e7213e
JL
741 regmap_write(chip->regmap, MAX17042_CGAIN, 0x0000);
742 regmap_write(chip->regmap, MAX17042_MiscCFG, 0x0003);
743 regmap_write(chip->regmap, MAX17042_LearnCFG, 0x0007);
359ab9f5
MH
744 }
745
297d716f
KK
746 chip->battery = power_supply_register(&client->dev, max17042_desc,
747 &psy_cfg);
748 if (IS_ERR(chip->battery)) {
243e3527 749 dev_err(&client->dev, "failed: power supply register\n");
297d716f 750 return PTR_ERR(chip->battery);
243e3527
RP
751 }
752
e5f3872d 753 if (client->irq) {
5cdd4d7f 754 ret = request_threaded_irq(client->irq, NULL,
26740dbb
JL
755 max17042_thread_handler,
756 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
297d716f 757 chip->battery->desc->name, chip);
e5f3872d 758 if (!ret) {
bc352686
KK
759 regmap_update_bits(chip->regmap, MAX17042_CONFIG,
760 CONFIG_ALRT_BIT_ENBL,
761 CONFIG_ALRT_BIT_ENBL);
e5f3872d 762 max17042_set_soc_threshold(chip, 1);
e5ba50bc
RP
763 } else {
764 client->irq = 0;
e5f3872d
RP
765 dev_err(&client->dev, "%s(): cannot get IRQ\n",
766 __func__);
e5ba50bc 767 }
e5f3872d
RP
768 }
769
39e7213e
JL
770 regmap_read(chip->regmap, MAX17042_STATUS, &val);
771 if (val & STATUS_POR_BIT) {
f3a71a6e
RP
772 INIT_WORK(&chip->work, max17042_init_worker);
773 schedule_work(&chip->work);
774 } else {
775 chip->init_complete = 1;
776 }
777
243e3527 778 return 0;
359ab9f5
MH
779}
780
415ec69f 781static int max17042_remove(struct i2c_client *client)
359ab9f5
MH
782{
783 struct max17042_chip *chip = i2c_get_clientdata(client);
784
bb28da90
RP
785 if (client->irq)
786 free_irq(client->irq, chip);
297d716f 787 power_supply_unregister(chip->battery);
359ab9f5
MH
788 return 0;
789}
790
3d23c7f4 791#ifdef CONFIG_PM_SLEEP
48bc1774
RP
792static int max17042_suspend(struct device *dev)
793{
794 struct max17042_chip *chip = dev_get_drvdata(dev);
795
48e41c70
AV
796 /*
797 * disable the irq and enable irq_wake
48bc1774
RP
798 * capability to the interrupt line.
799 */
800 if (chip->client->irq) {
801 disable_irq(chip->client->irq);
802 enable_irq_wake(chip->client->irq);
803 }
804
805 return 0;
806}
807
808static int max17042_resume(struct device *dev)
809{
810 struct max17042_chip *chip = dev_get_drvdata(dev);
811
812 if (chip->client->irq) {
813 disable_irq_wake(chip->client->irq);
814 enable_irq(chip->client->irq);
815 /* re-program the SOC thresholds to 1% change */
816 max17042_set_soc_threshold(chip, 1);
817 }
818
819 return 0;
820}
48bc1774
RP
821#endif
822
3d23c7f4
MB
823static SIMPLE_DEV_PM_OPS(max17042_pm_ops, max17042_suspend,
824 max17042_resume);
825
3832246d
KL
826#ifdef CONFIG_OF
827static const struct of_device_id max17042_dt_match[] = {
828 { .compatible = "maxim,max17042" },
9a8422d2
RP
829 { .compatible = "maxim,max17047" },
830 { .compatible = "maxim,max17050" },
3832246d
KL
831 { },
832};
833MODULE_DEVICE_TABLE(of, max17042_dt_match);
834#endif
835
359ab9f5
MH
836static const struct i2c_device_id max17042_id[] = {
837 { "max17042", 0 },
9a8422d2
RP
838 { "max17047", 1 },
839 { "max17050", 2 },
359ab9f5
MH
840 { }
841};
842MODULE_DEVICE_TABLE(i2c, max17042_id);
843
844static struct i2c_driver max17042_i2c_driver = {
845 .driver = {
846 .name = "max17042",
3832246d 847 .of_match_table = of_match_ptr(max17042_dt_match),
3d23c7f4 848 .pm = &max17042_pm_ops,
359ab9f5
MH
849 },
850 .probe = max17042_probe,
28ea73f4 851 .remove = max17042_remove,
359ab9f5
MH
852 .id_table = max17042_id,
853};
5ff92e7a 854module_i2c_driver(max17042_i2c_driver);
359ab9f5
MH
855
856MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
857MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
858MODULE_LICENSE("GPL");