Loading...
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>
26#include <linux/module.h>
27#include <linux/slab.h>
28#include <linux/i2c.h>
29#include <linux/delay.h>
30#include <linux/interrupt.h>
31#include <linux/pm.h>
32#include <linux/mod_devicetable.h>
33#include <linux/power_supply.h>
34#include <linux/power/max17042_battery.h>
35#include <linux/of.h>
36#include <linux/regmap.h>
37
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
50/* Interrupt mask bits */
51#define CONFIG_ALRT_BIT_ENBL (1 << 2)
52#define STATUS_INTR_SOCMIN_BIT (1 << 10)
53#define STATUS_INTR_SOCMAX_BIT (1 << 14)
54
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
66#define MAX17042_IC_VERSION 0x0092
67#define MAX17047_IC_VERSION 0x00AC /* same for max17050 */
68
69struct max17042_chip {
70 struct i2c_client *client;
71 struct regmap *regmap;
72 struct power_supply battery;
73 enum max170xx_chip_type chip_type;
74 struct max17042_platform_data *pdata;
75 struct work_struct work;
76 int init_complete;
77};
78
79static enum power_supply_property max17042_battery_props[] = {
80 POWER_SUPPLY_PROP_PRESENT,
81 POWER_SUPPLY_PROP_CYCLE_COUNT,
82 POWER_SUPPLY_PROP_VOLTAGE_MAX,
83 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
84 POWER_SUPPLY_PROP_VOLTAGE_NOW,
85 POWER_SUPPLY_PROP_VOLTAGE_AVG,
86 POWER_SUPPLY_PROP_VOLTAGE_OCV,
87 POWER_SUPPLY_PROP_CAPACITY,
88 POWER_SUPPLY_PROP_CHARGE_FULL,
89 POWER_SUPPLY_PROP_CHARGE_COUNTER,
90 POWER_SUPPLY_PROP_TEMP,
91 POWER_SUPPLY_PROP_CURRENT_NOW,
92 POWER_SUPPLY_PROP_CURRENT_AVG,
93};
94
95static int max17042_get_property(struct power_supply *psy,
96 enum power_supply_property psp,
97 union power_supply_propval *val)
98{
99 struct max17042_chip *chip = container_of(psy,
100 struct max17042_chip, battery);
101 struct regmap *map = chip->regmap;
102 int ret;
103 u32 data;
104
105 if (!chip->init_complete)
106 return -EAGAIN;
107
108 switch (psp) {
109 case POWER_SUPPLY_PROP_PRESENT:
110 ret = regmap_read(map, MAX17042_STATUS, &data);
111 if (ret < 0)
112 return ret;
113
114 if (data & MAX17042_STATUS_BattAbsent)
115 val->intval = 0;
116 else
117 val->intval = 1;
118 break;
119 case POWER_SUPPLY_PROP_CYCLE_COUNT:
120 ret = regmap_read(map, MAX17042_Cycles, &data);
121 if (ret < 0)
122 return ret;
123
124 val->intval = data;
125 break;
126 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
127 ret = regmap_read(map, MAX17042_MinMaxVolt, &data);
128 if (ret < 0)
129 return ret;
130
131 val->intval = data >> 8;
132 val->intval *= 20000; /* Units of LSB = 20mV */
133 break;
134 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
135 if (chip->chip_type == MAX17042)
136 ret = regmap_read(map, MAX17042_V_empty, &data);
137 else
138 ret = regmap_read(map, MAX17047_V_empty, &data);
139 if (ret < 0)
140 return ret;
141
142 val->intval = data >> 7;
143 val->intval *= 10000; /* Units of LSB = 10mV */
144 break;
145 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
146 ret = regmap_read(map, MAX17042_VCELL, &data);
147 if (ret < 0)
148 return ret;
149
150 val->intval = data * 625 / 8;
151 break;
152 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
153 ret = regmap_read(map, MAX17042_AvgVCELL, &data);
154 if (ret < 0)
155 return ret;
156
157 val->intval = data * 625 / 8;
158 break;
159 case POWER_SUPPLY_PROP_VOLTAGE_OCV:
160 ret = regmap_read(map, MAX17042_OCVInternal, &data);
161 if (ret < 0)
162 return ret;
163
164 val->intval = data * 625 / 8;
165 break;
166 case POWER_SUPPLY_PROP_CAPACITY:
167 ret = regmap_read(map, MAX17042_RepSOC, &data);
168 if (ret < 0)
169 return ret;
170
171 val->intval = data >> 8;
172 break;
173 case POWER_SUPPLY_PROP_CHARGE_FULL:
174 ret = regmap_read(map, MAX17042_FullCAP, &data);
175 if (ret < 0)
176 return ret;
177
178 val->intval = data * 1000 / 2;
179 break;
180 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
181 ret = regmap_read(map, MAX17042_QH, &data);
182 if (ret < 0)
183 return ret;
184
185 val->intval = data * 1000 / 2;
186 break;
187 case POWER_SUPPLY_PROP_TEMP:
188 ret = regmap_read(map, MAX17042_TEMP, &data);
189 if (ret < 0)
190 return ret;
191
192 val->intval = data;
193 /* The value is signed. */
194 if (val->intval & 0x8000) {
195 val->intval = (0x7fff & ~val->intval) + 1;
196 val->intval *= -1;
197 }
198 /* The value is converted into deci-centigrade scale */
199 /* Units of LSB = 1 / 256 degree Celsius */
200 val->intval = val->intval * 10 / 256;
201 break;
202 case POWER_SUPPLY_PROP_CURRENT_NOW:
203 if (chip->pdata->enable_current_sense) {
204 ret = regmap_read(map, MAX17042_Current, &data);
205 if (ret < 0)
206 return ret;
207
208 val->intval = data;
209 if (val->intval & 0x8000) {
210 /* Negative */
211 val->intval = ~val->intval & 0x7fff;
212 val->intval++;
213 val->intval *= -1;
214 }
215 val->intval *= 1562500 / chip->pdata->r_sns;
216 } else {
217 return -EINVAL;
218 }
219 break;
220 case POWER_SUPPLY_PROP_CURRENT_AVG:
221 if (chip->pdata->enable_current_sense) {
222 ret = regmap_read(map, MAX17042_AvgCurrent, &data);
223 if (ret < 0)
224 return ret;
225
226 val->intval = data;
227 if (val->intval & 0x8000) {
228 /* Negative */
229 val->intval = ~val->intval & 0x7fff;
230 val->intval++;
231 val->intval *= -1;
232 }
233 val->intval *= 1562500 / chip->pdata->r_sns;
234 } else {
235 return -EINVAL;
236 }
237 break;
238 default:
239 return -EINVAL;
240 }
241 return 0;
242}
243
244static int max17042_write_verify_reg(struct regmap *map, u8 reg, u32 value)
245{
246 int retries = 8;
247 int ret;
248 u32 read_value;
249
250 do {
251 ret = regmap_write(map, reg, value);
252 regmap_read(map, reg, &read_value);
253 if (read_value != value) {
254 ret = -EIO;
255 retries--;
256 }
257 } while (retries && read_value != value);
258
259 if (ret < 0)
260 pr_err("%s: err %d\n", __func__, ret);
261
262 return ret;
263}
264
265static inline void max17042_override_por(struct regmap *map,
266 u8 reg, u16 value)
267{
268 if (value)
269 regmap_write(map, reg, value);
270}
271
272static inline void max10742_unlock_model(struct max17042_chip *chip)
273{
274 struct regmap *map = chip->regmap;
275 regmap_write(map, MAX17042_MLOCKReg1, MODEL_UNLOCK1);
276 regmap_write(map, MAX17042_MLOCKReg2, MODEL_UNLOCK2);
277}
278
279static inline void max10742_lock_model(struct max17042_chip *chip)
280{
281 struct regmap *map = chip->regmap;
282
283 regmap_write(map, MAX17042_MLOCKReg1, MODEL_LOCK1);
284 regmap_write(map, MAX17042_MLOCKReg2, MODEL_LOCK2);
285}
286
287static inline void max17042_write_model_data(struct max17042_chip *chip,
288 u8 addr, int size)
289{
290 struct regmap *map = chip->regmap;
291 int i;
292 for (i = 0; i < size; i++)
293 regmap_write(map, addr + i,
294 chip->pdata->config_data->cell_char_tbl[i]);
295}
296
297static inline void max17042_read_model_data(struct max17042_chip *chip,
298 u8 addr, u32 *data, int size)
299{
300 struct regmap *map = chip->regmap;
301 int i;
302
303 for (i = 0; i < size; i++)
304 regmap_read(map, addr + i, &data[i]);
305}
306
307static inline int max17042_model_data_compare(struct max17042_chip *chip,
308 u16 *data1, u16 *data2, int size)
309{
310 int i;
311
312 if (memcmp(data1, data2, size)) {
313 dev_err(&chip->client->dev, "%s compare failed\n", __func__);
314 for (i = 0; i < size; i++)
315 dev_info(&chip->client->dev, "0x%x, 0x%x",
316 data1[i], data2[i]);
317 dev_info(&chip->client->dev, "\n");
318 return -EINVAL;
319 }
320 return 0;
321}
322
323static int max17042_init_model(struct max17042_chip *chip)
324{
325 int ret;
326 int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl);
327 u32 *temp_data;
328
329 temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL);
330 if (!temp_data)
331 return -ENOMEM;
332
333 max10742_unlock_model(chip);
334 max17042_write_model_data(chip, MAX17042_MODELChrTbl,
335 table_size);
336 max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data,
337 table_size);
338
339 ret = max17042_model_data_compare(
340 chip,
341 chip->pdata->config_data->cell_char_tbl,
342 (u16 *)temp_data,
343 table_size);
344
345 max10742_lock_model(chip);
346 kfree(temp_data);
347
348 return ret;
349}
350
351static int max17042_verify_model_lock(struct max17042_chip *chip)
352{
353 int i;
354 int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl);
355 u32 *temp_data;
356 int ret = 0;
357
358 temp_data = kcalloc(table_size, sizeof(*temp_data), 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
372static void max17042_write_config_regs(struct max17042_chip *chip)
373{
374 struct max17042_config_data *config = chip->pdata->config_data;
375 struct regmap *map = chip->regmap;
376
377 regmap_write(map, MAX17042_CONFIG, config->config);
378 regmap_write(map, MAX17042_LearnCFG, config->learn_cfg);
379 regmap_write(map, MAX17042_FilterCFG,
380 config->filter_cfg);
381 regmap_write(map, MAX17042_RelaxCFG, config->relax_cfg);
382 if (chip->chip_type == MAX17047)
383 regmap_write(map, MAX17047_FullSOCThr,
384 config->full_soc_thresh);
385}
386
387static void max17042_write_custom_regs(struct max17042_chip *chip)
388{
389 struct max17042_config_data *config = chip->pdata->config_data;
390 struct regmap *map = chip->regmap;
391
392 max17042_write_verify_reg(map, MAX17042_RCOMP0, config->rcomp0);
393 max17042_write_verify_reg(map, MAX17042_TempCo, config->tcompc0);
394 max17042_write_verify_reg(map, MAX17042_ICHGTerm, config->ichgt_term);
395 if (chip->chip_type == MAX17042) {
396 regmap_write(map, MAX17042_EmptyTempCo, config->empty_tempco);
397 max17042_write_verify_reg(map, MAX17042_K_empty0,
398 config->kempty0);
399 } else {
400 max17042_write_verify_reg(map, MAX17047_QRTbl00,
401 config->qrtbl00);
402 max17042_write_verify_reg(map, MAX17047_QRTbl10,
403 config->qrtbl10);
404 max17042_write_verify_reg(map, MAX17047_QRTbl20,
405 config->qrtbl20);
406 max17042_write_verify_reg(map, MAX17047_QRTbl30,
407 config->qrtbl30);
408 }
409}
410
411static void max17042_update_capacity_regs(struct max17042_chip *chip)
412{
413 struct max17042_config_data *config = chip->pdata->config_data;
414 struct regmap *map = chip->regmap;
415
416 max17042_write_verify_reg(map, MAX17042_FullCAP,
417 config->fullcap);
418 regmap_write(map, MAX17042_DesignCap, config->design_cap);
419 max17042_write_verify_reg(map, MAX17042_FullCAPNom,
420 config->fullcapnom);
421}
422
423static void max17042_reset_vfsoc0_reg(struct max17042_chip *chip)
424{
425 unsigned int vfSoc;
426 struct regmap *map = chip->regmap;
427
428 regmap_read(map, MAX17042_VFSOC, &vfSoc);
429 regmap_write(map, MAX17042_VFSOC0Enable, VFSOC0_UNLOCK);
430 max17042_write_verify_reg(map, MAX17042_VFSOC0, vfSoc);
431 regmap_write(map, MAX17042_VFSOC0Enable, VFSOC0_LOCK);
432}
433
434static void max17042_load_new_capacity_params(struct max17042_chip *chip)
435{
436 u32 full_cap0, rep_cap, dq_acc, vfSoc;
437 u32 rem_cap;
438
439 struct max17042_config_data *config = chip->pdata->config_data;
440 struct regmap *map = chip->regmap;
441
442 regmap_read(map, MAX17042_FullCAP0, &full_cap0);
443 regmap_read(map, MAX17042_VFSOC, &vfSoc);
444
445 /* fg_vfSoc needs to shifted by 8 bits to get the
446 * perc in 1% accuracy, to get the right rem_cap multiply
447 * full_cap0, fg_vfSoc and devide by 100
448 */
449 rem_cap = ((vfSoc >> 8) * full_cap0) / 100;
450 max17042_write_verify_reg(map, MAX17042_RemCap, rem_cap);
451
452 rep_cap = rem_cap;
453 max17042_write_verify_reg(map, MAX17042_RepCap, rep_cap);
454
455 /* Write dQ_acc to 200% of Capacity and dP_acc to 200% */
456 dq_acc = config->fullcap / dQ_ACC_DIV;
457 max17042_write_verify_reg(map, MAX17042_dQacc, dq_acc);
458 max17042_write_verify_reg(map, MAX17042_dPacc, dP_ACC_200);
459
460 max17042_write_verify_reg(map, MAX17042_FullCAP,
461 config->fullcap);
462 regmap_write(map, MAX17042_DesignCap,
463 config->design_cap);
464 max17042_write_verify_reg(map, MAX17042_FullCAPNom,
465 config->fullcapnom);
466 /* Update SOC register with new SOC */
467 regmap_write(map, MAX17042_RepSOC, vfSoc);
468}
469
470/*
471 * Block write all the override values coming from platform data.
472 * This function MUST be called before the POR initialization proceedure
473 * specified by maxim.
474 */
475static inline void max17042_override_por_values(struct max17042_chip *chip)
476{
477 struct regmap *map = chip->regmap;
478 struct max17042_config_data *config = chip->pdata->config_data;
479
480 max17042_override_por(map, MAX17042_TGAIN, config->tgain);
481 max17042_override_por(map, MAx17042_TOFF, config->toff);
482 max17042_override_por(map, MAX17042_CGAIN, config->cgain);
483 max17042_override_por(map, MAX17042_COFF, config->coff);
484
485 max17042_override_por(map, MAX17042_VALRT_Th, config->valrt_thresh);
486 max17042_override_por(map, MAX17042_TALRT_Th, config->talrt_thresh);
487 max17042_override_por(map, MAX17042_SALRT_Th,
488 config->soc_alrt_thresh);
489 max17042_override_por(map, MAX17042_CONFIG, config->config);
490 max17042_override_por(map, MAX17042_SHDNTIMER, config->shdntimer);
491
492 max17042_override_por(map, MAX17042_DesignCap, config->design_cap);
493 max17042_override_por(map, MAX17042_ICHGTerm, config->ichgt_term);
494
495 max17042_override_por(map, MAX17042_AtRate, config->at_rate);
496 max17042_override_por(map, MAX17042_LearnCFG, config->learn_cfg);
497 max17042_override_por(map, MAX17042_FilterCFG, config->filter_cfg);
498 max17042_override_por(map, MAX17042_RelaxCFG, config->relax_cfg);
499 max17042_override_por(map, MAX17042_MiscCFG, config->misc_cfg);
500 max17042_override_por(map, MAX17042_MaskSOC, config->masksoc);
501
502 max17042_override_por(map, MAX17042_FullCAP, config->fullcap);
503 max17042_override_por(map, MAX17042_FullCAPNom, config->fullcapnom);
504 if (chip->chip_type == MAX17042)
505 max17042_override_por(map, MAX17042_SOC_empty,
506 config->socempty);
507 max17042_override_por(map, MAX17042_LAvg_empty, config->lavg_empty);
508 max17042_override_por(map, MAX17042_dQacc, config->dqacc);
509 max17042_override_por(map, MAX17042_dPacc, config->dpacc);
510
511 if (chip->chip_type == MAX17042)
512 max17042_override_por(map, MAX17042_V_empty, config->vempty);
513 else
514 max17042_override_por(map, MAX17047_V_empty, config->vempty);
515 max17042_override_por(map, MAX17042_TempNom, config->temp_nom);
516 max17042_override_por(map, MAX17042_TempLim, config->temp_lim);
517 max17042_override_por(map, MAX17042_FCTC, config->fctc);
518 max17042_override_por(map, MAX17042_RCOMP0, config->rcomp0);
519 max17042_override_por(map, MAX17042_TempCo, config->tcompc0);
520 if (chip->chip_type) {
521 max17042_override_por(map, MAX17042_EmptyTempCo,
522 config->empty_tempco);
523 max17042_override_por(map, MAX17042_K_empty0,
524 config->kempty0);
525 }
526}
527
528static int max17042_init_chip(struct max17042_chip *chip)
529{
530 struct regmap *map = chip->regmap;
531 int ret;
532 int val;
533
534 max17042_override_por_values(chip);
535 /* After Power up, the MAX17042 requires 500mS in order
536 * to perform signal debouncing and initial SOC reporting
537 */
538 msleep(500);
539
540 /* Initialize configaration */
541 max17042_write_config_regs(chip);
542
543 /* write cell characterization data */
544 ret = max17042_init_model(chip);
545 if (ret) {
546 dev_err(&chip->client->dev, "%s init failed\n",
547 __func__);
548 return -EIO;
549 }
550
551 ret = max17042_verify_model_lock(chip);
552 if (ret) {
553 dev_err(&chip->client->dev, "%s lock verify failed\n",
554 __func__);
555 return -EIO;
556 }
557 /* write custom parameters */
558 max17042_write_custom_regs(chip);
559
560 /* update capacity params */
561 max17042_update_capacity_regs(chip);
562
563 /* delay must be atleast 350mS to allow VFSOC
564 * to be calculated from the new configuration
565 */
566 msleep(350);
567
568 /* reset vfsoc0 reg */
569 max17042_reset_vfsoc0_reg(chip);
570
571 /* load new capacity params */
572 max17042_load_new_capacity_params(chip);
573
574 /* Init complete, Clear the POR bit */
575 regmap_read(map, MAX17042_STATUS, &val);
576 regmap_write(map, MAX17042_STATUS, val & (~STATUS_POR_BIT));
577 return 0;
578}
579
580static void max17042_set_soc_threshold(struct max17042_chip *chip, u16 off)
581{
582 struct regmap *map = chip->regmap;
583 u32 soc, soc_tr;
584
585 /* program interrupt thesholds such that we should
586 * get interrupt for every 'off' perc change in the soc
587 */
588 regmap_read(map, MAX17042_RepSOC, &soc);
589 soc >>= 8;
590 soc_tr = (soc + off) << 8;
591 soc_tr |= (soc - off);
592 regmap_write(map, MAX17042_SALRT_Th, soc_tr);
593}
594
595static irqreturn_t max17042_thread_handler(int id, void *dev)
596{
597 struct max17042_chip *chip = dev;
598 u32 val;
599
600 regmap_read(chip->regmap, MAX17042_STATUS, &val);
601 if ((val & STATUS_INTR_SOCMIN_BIT) ||
602 (val & STATUS_INTR_SOCMAX_BIT)) {
603 dev_info(&chip->client->dev, "SOC threshold INTR\n");
604 max17042_set_soc_threshold(chip, 1);
605 }
606
607 power_supply_changed(&chip->battery);
608 return IRQ_HANDLED;
609}
610
611static void max17042_init_worker(struct work_struct *work)
612{
613 struct max17042_chip *chip = container_of(work,
614 struct max17042_chip, work);
615 int ret;
616
617 /* Initialize registers according to values from the platform data */
618 if (chip->pdata->enable_por_init && chip->pdata->config_data) {
619 ret = max17042_init_chip(chip);
620 if (ret)
621 return;
622 }
623
624 chip->init_complete = 1;
625}
626
627#ifdef CONFIG_OF
628static struct max17042_platform_data *
629max17042_get_pdata(struct device *dev)
630{
631 struct device_node *np = dev->of_node;
632 u32 prop;
633 struct max17042_platform_data *pdata;
634
635 if (!np)
636 return dev->platform_data;
637
638 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
639 if (!pdata)
640 return NULL;
641
642 /*
643 * Require current sense resistor value to be specified for
644 * current-sense functionality to be enabled at all.
645 */
646 if (of_property_read_u32(np, "maxim,rsns-microohm", &prop) == 0) {
647 pdata->r_sns = prop;
648 pdata->enable_current_sense = true;
649 }
650
651 return pdata;
652}
653#else
654static struct max17042_platform_data *
655max17042_get_pdata(struct device *dev)
656{
657 return dev->platform_data;
658}
659#endif
660
661static struct regmap_config max17042_regmap_config = {
662 .reg_bits = 8,
663 .val_bits = 16,
664 .val_format_endian = REGMAP_ENDIAN_NATIVE,
665};
666
667static int max17042_probe(struct i2c_client *client,
668 const struct i2c_device_id *id)
669{
670 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
671 struct max17042_chip *chip;
672 int ret;
673 int i;
674 u32 val;
675
676 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
677 return -EIO;
678
679 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
680 if (!chip)
681 return -ENOMEM;
682
683 chip->client = client;
684 chip->regmap = devm_regmap_init_i2c(client, &max17042_regmap_config);
685 if (IS_ERR(chip->regmap)) {
686 dev_err(&client->dev, "Failed to initialize regmap\n");
687 return -EINVAL;
688 }
689
690 chip->pdata = max17042_get_pdata(&client->dev);
691 if (!chip->pdata) {
692 dev_err(&client->dev, "no platform data provided\n");
693 return -EINVAL;
694 }
695
696 i2c_set_clientdata(client, chip);
697
698 regmap_read(chip->regmap, MAX17042_DevName, &val);
699 if (val == MAX17042_IC_VERSION) {
700 dev_dbg(&client->dev, "chip type max17042 detected\n");
701 chip->chip_type = MAX17042;
702 } else if (val == MAX17047_IC_VERSION) {
703 dev_dbg(&client->dev, "chip type max17047/50 detected\n");
704 chip->chip_type = MAX17047;
705 } else {
706 dev_err(&client->dev, "device version mismatch: %x\n", val);
707 return -EIO;
708 }
709
710 chip->battery.name = "max170xx_battery";
711 chip->battery.type = POWER_SUPPLY_TYPE_BATTERY;
712 chip->battery.get_property = max17042_get_property;
713 chip->battery.properties = max17042_battery_props;
714 chip->battery.num_properties = ARRAY_SIZE(max17042_battery_props);
715
716 /* When current is not measured,
717 * CURRENT_NOW and CURRENT_AVG properties should be invisible. */
718 if (!chip->pdata->enable_current_sense)
719 chip->battery.num_properties -= 2;
720
721 if (chip->pdata->r_sns == 0)
722 chip->pdata->r_sns = MAX17042_DEFAULT_SNS_RESISTOR;
723
724 if (chip->pdata->init_data)
725 for (i = 0; i < chip->pdata->num_init_data; i++)
726 regmap_write(chip->regmap,
727 chip->pdata->init_data[i].addr,
728 chip->pdata->init_data[i].data);
729
730 if (!chip->pdata->enable_current_sense) {
731 regmap_write(chip->regmap, MAX17042_CGAIN, 0x0000);
732 regmap_write(chip->regmap, MAX17042_MiscCFG, 0x0003);
733 regmap_write(chip->regmap, MAX17042_LearnCFG, 0x0007);
734 }
735
736 ret = power_supply_register(&client->dev, &chip->battery);
737 if (ret) {
738 dev_err(&client->dev, "failed: power supply register\n");
739 return ret;
740 }
741
742 if (client->irq) {
743 ret = request_threaded_irq(client->irq, NULL,
744 max17042_thread_handler,
745 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
746 chip->battery.name, chip);
747 if (!ret) {
748 regmap_read(chip->regmap, MAX17042_CONFIG, &val);
749 val |= CONFIG_ALRT_BIT_ENBL;
750 regmap_write(chip->regmap, MAX17042_CONFIG, val);
751 max17042_set_soc_threshold(chip, 1);
752 } else {
753 client->irq = 0;
754 dev_err(&client->dev, "%s(): cannot get IRQ\n",
755 __func__);
756 }
757 }
758
759 regmap_read(chip->regmap, MAX17042_STATUS, &val);
760 if (val & STATUS_POR_BIT) {
761 INIT_WORK(&chip->work, max17042_init_worker);
762 schedule_work(&chip->work);
763 } else {
764 chip->init_complete = 1;
765 }
766
767 return 0;
768}
769
770static int max17042_remove(struct i2c_client *client)
771{
772 struct max17042_chip *chip = i2c_get_clientdata(client);
773
774 if (client->irq)
775 free_irq(client->irq, chip);
776 power_supply_unregister(&chip->battery);
777 return 0;
778}
779
780#ifdef CONFIG_PM_SLEEP
781static int max17042_suspend(struct device *dev)
782{
783 struct max17042_chip *chip = dev_get_drvdata(dev);
784
785 /*
786 * disable the irq and enable irq_wake
787 * capability to the interrupt line.
788 */
789 if (chip->client->irq) {
790 disable_irq(chip->client->irq);
791 enable_irq_wake(chip->client->irq);
792 }
793
794 return 0;
795}
796
797static int max17042_resume(struct device *dev)
798{
799 struct max17042_chip *chip = dev_get_drvdata(dev);
800
801 if (chip->client->irq) {
802 disable_irq_wake(chip->client->irq);
803 enable_irq(chip->client->irq);
804 /* re-program the SOC thresholds to 1% change */
805 max17042_set_soc_threshold(chip, 1);
806 }
807
808 return 0;
809}
810#endif
811
812static SIMPLE_DEV_PM_OPS(max17042_pm_ops, max17042_suspend,
813 max17042_resume);
814
815#ifdef CONFIG_OF
816static const struct of_device_id max17042_dt_match[] = {
817 { .compatible = "maxim,max17042" },
818 { .compatible = "maxim,max17047" },
819 { .compatible = "maxim,max17050" },
820 { },
821};
822MODULE_DEVICE_TABLE(of, max17042_dt_match);
823#endif
824
825static const struct i2c_device_id max17042_id[] = {
826 { "max17042", 0 },
827 { "max17047", 1 },
828 { "max17050", 2 },
829 { }
830};
831MODULE_DEVICE_TABLE(i2c, max17042_id);
832
833static struct i2c_driver max17042_i2c_driver = {
834 .driver = {
835 .name = "max17042",
836 .of_match_table = of_match_ptr(max17042_dt_match),
837 .pm = &max17042_pm_ops,
838 },
839 .probe = max17042_probe,
840 .remove = max17042_remove,
841 .id_table = max17042_id,
842};
843module_i2c_driver(max17042_i2c_driver);
844
845MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
846MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
847MODULE_LICENSE("GPL");
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>
26#include <linux/module.h>
27#include <linux/slab.h>
28#include <linux/i2c.h>
29#include <linux/delay.h>
30#include <linux/interrupt.h>
31#include <linux/pm.h>
32#include <linux/mod_devicetable.h>
33#include <linux/power_supply.h>
34#include <linux/power/max17042_battery.h>
35#include <linux/of.h>
36#include <linux/regmap.h>
37
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
50/* Interrupt mask bits */
51#define CONFIG_ALRT_BIT_ENBL (1 << 2)
52#define STATUS_INTR_SOCMIN_BIT (1 << 10)
53#define STATUS_INTR_SOCMAX_BIT (1 << 14)
54
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
66#define MAX17042_VMAX_TOLERANCE 50 /* 50 mV */
67
68struct max17042_chip {
69 struct i2c_client *client;
70 struct regmap *regmap;
71 struct power_supply *battery;
72 enum max170xx_chip_type chip_type;
73 struct max17042_platform_data *pdata;
74 struct work_struct work;
75 int init_complete;
76};
77
78static enum power_supply_property max17042_battery_props[] = {
79 POWER_SUPPLY_PROP_PRESENT,
80 POWER_SUPPLY_PROP_CYCLE_COUNT,
81 POWER_SUPPLY_PROP_VOLTAGE_MAX,
82 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
83 POWER_SUPPLY_PROP_VOLTAGE_NOW,
84 POWER_SUPPLY_PROP_VOLTAGE_AVG,
85 POWER_SUPPLY_PROP_VOLTAGE_OCV,
86 POWER_SUPPLY_PROP_CAPACITY,
87 POWER_SUPPLY_PROP_CHARGE_FULL,
88 POWER_SUPPLY_PROP_CHARGE_COUNTER,
89 POWER_SUPPLY_PROP_TEMP,
90 POWER_SUPPLY_PROP_TEMP_ALERT_MIN,
91 POWER_SUPPLY_PROP_TEMP_ALERT_MAX,
92 POWER_SUPPLY_PROP_TEMP_MIN,
93 POWER_SUPPLY_PROP_TEMP_MAX,
94 POWER_SUPPLY_PROP_HEALTH,
95 POWER_SUPPLY_PROP_CURRENT_NOW,
96 POWER_SUPPLY_PROP_CURRENT_AVG,
97};
98
99static int max17042_get_temperature(struct max17042_chip *chip, int *temp)
100{
101 int ret;
102 u32 data;
103 struct regmap *map = chip->regmap;
104
105 ret = regmap_read(map, MAX17042_TEMP, &data);
106 if (ret < 0)
107 return ret;
108
109 *temp = data;
110 /* The value is signed. */
111 if (*temp & 0x8000) {
112 *temp = (0x7fff & ~*temp) + 1;
113 *temp *= -1;
114 }
115
116 /* The value is converted into deci-centigrade scale */
117 /* Units of LSB = 1 / 256 degree Celsius */
118 *temp = *temp * 10 / 256;
119 return 0;
120}
121
122static int max17042_get_battery_health(struct max17042_chip *chip, int *health)
123{
124 int temp, vavg, vbatt, ret;
125 u32 val;
126
127 ret = regmap_read(chip->regmap, MAX17042_AvgVCELL, &val);
128 if (ret < 0)
129 goto health_error;
130
131 /* bits [0-3] unused */
132 vavg = val * 625 / 8;
133 /* Convert to millivolts */
134 vavg /= 1000;
135
136 ret = regmap_read(chip->regmap, MAX17042_VCELL, &val);
137 if (ret < 0)
138 goto health_error;
139
140 /* bits [0-3] unused */
141 vbatt = val * 625 / 8;
142 /* Convert to millivolts */
143 vbatt /= 1000;
144
145 if (vavg < chip->pdata->vmin) {
146 *health = POWER_SUPPLY_HEALTH_DEAD;
147 goto out;
148 }
149
150 if (vbatt > chip->pdata->vmax + MAX17042_VMAX_TOLERANCE) {
151 *health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
152 goto out;
153 }
154
155 ret = max17042_get_temperature(chip, &temp);
156 if (ret < 0)
157 goto health_error;
158
159 if (temp <= chip->pdata->temp_min) {
160 *health = POWER_SUPPLY_HEALTH_COLD;
161 goto out;
162 }
163
164 if (temp >= chip->pdata->temp_max) {
165 *health = POWER_SUPPLY_HEALTH_OVERHEAT;
166 goto out;
167 }
168
169 *health = POWER_SUPPLY_HEALTH_GOOD;
170
171out:
172 return 0;
173
174health_error:
175 return ret;
176}
177
178static int max17042_get_property(struct power_supply *psy,
179 enum power_supply_property psp,
180 union power_supply_propval *val)
181{
182 struct max17042_chip *chip = power_supply_get_drvdata(psy);
183 struct regmap *map = chip->regmap;
184 int ret;
185 u32 data;
186
187 if (!chip->init_complete)
188 return -EAGAIN;
189
190 switch (psp) {
191 case POWER_SUPPLY_PROP_PRESENT:
192 ret = regmap_read(map, MAX17042_STATUS, &data);
193 if (ret < 0)
194 return ret;
195
196 if (data & MAX17042_STATUS_BattAbsent)
197 val->intval = 0;
198 else
199 val->intval = 1;
200 break;
201 case POWER_SUPPLY_PROP_CYCLE_COUNT:
202 ret = regmap_read(map, MAX17042_Cycles, &data);
203 if (ret < 0)
204 return ret;
205
206 val->intval = data;
207 break;
208 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
209 ret = regmap_read(map, MAX17042_MinMaxVolt, &data);
210 if (ret < 0)
211 return ret;
212
213 val->intval = data >> 8;
214 val->intval *= 20000; /* Units of LSB = 20mV */
215 break;
216 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
217 if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042)
218 ret = regmap_read(map, MAX17042_V_empty, &data);
219 else
220 ret = regmap_read(map, MAX17047_V_empty, &data);
221 if (ret < 0)
222 return ret;
223
224 val->intval = data >> 7;
225 val->intval *= 10000; /* Units of LSB = 10mV */
226 break;
227 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
228 ret = regmap_read(map, MAX17042_VCELL, &data);
229 if (ret < 0)
230 return ret;
231
232 val->intval = data * 625 / 8;
233 break;
234 case POWER_SUPPLY_PROP_VOLTAGE_AVG:
235 ret = regmap_read(map, MAX17042_AvgVCELL, &data);
236 if (ret < 0)
237 return ret;
238
239 val->intval = data * 625 / 8;
240 break;
241 case POWER_SUPPLY_PROP_VOLTAGE_OCV:
242 ret = regmap_read(map, MAX17042_OCVInternal, &data);
243 if (ret < 0)
244 return ret;
245
246 val->intval = data * 625 / 8;
247 break;
248 case POWER_SUPPLY_PROP_CAPACITY:
249 ret = regmap_read(map, MAX17042_RepSOC, &data);
250 if (ret < 0)
251 return ret;
252
253 val->intval = data >> 8;
254 break;
255 case POWER_SUPPLY_PROP_CHARGE_FULL:
256 ret = regmap_read(map, MAX17042_FullCAP, &data);
257 if (ret < 0)
258 return ret;
259
260 val->intval = data * 1000 / 2;
261 break;
262 case POWER_SUPPLY_PROP_CHARGE_COUNTER:
263 ret = regmap_read(map, MAX17042_QH, &data);
264 if (ret < 0)
265 return ret;
266
267 val->intval = data * 1000 / 2;
268 break;
269 case POWER_SUPPLY_PROP_TEMP:
270 ret = max17042_get_temperature(chip, &val->intval);
271 if (ret < 0)
272 return ret;
273 break;
274 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
275 ret = regmap_read(map, MAX17042_TALRT_Th, &data);
276 if (ret < 0)
277 return ret;
278 /* LSB is Alert Minimum. In deci-centigrade */
279 val->intval = (data & 0xff) * 10;
280 break;
281 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
282 ret = regmap_read(map, MAX17042_TALRT_Th, &data);
283 if (ret < 0)
284 return ret;
285 /* MSB is Alert Maximum. In deci-centigrade */
286 val->intval = (data >> 8) * 10;
287 break;
288 case POWER_SUPPLY_PROP_TEMP_MIN:
289 val->intval = chip->pdata->temp_min;
290 break;
291 case POWER_SUPPLY_PROP_TEMP_MAX:
292 val->intval = chip->pdata->temp_max;
293 break;
294 case POWER_SUPPLY_PROP_HEALTH:
295 ret = max17042_get_battery_health(chip, &val->intval);
296 if (ret < 0)
297 return ret;
298 break;
299 case POWER_SUPPLY_PROP_CURRENT_NOW:
300 if (chip->pdata->enable_current_sense) {
301 ret = regmap_read(map, MAX17042_Current, &data);
302 if (ret < 0)
303 return ret;
304
305 val->intval = data;
306 if (val->intval & 0x8000) {
307 /* Negative */
308 val->intval = ~val->intval & 0x7fff;
309 val->intval++;
310 val->intval *= -1;
311 }
312 val->intval *= 1562500 / chip->pdata->r_sns;
313 } else {
314 return -EINVAL;
315 }
316 break;
317 case POWER_SUPPLY_PROP_CURRENT_AVG:
318 if (chip->pdata->enable_current_sense) {
319 ret = regmap_read(map, MAX17042_AvgCurrent, &data);
320 if (ret < 0)
321 return ret;
322
323 val->intval = data;
324 if (val->intval & 0x8000) {
325 /* Negative */
326 val->intval = ~val->intval & 0x7fff;
327 val->intval++;
328 val->intval *= -1;
329 }
330 val->intval *= 1562500 / chip->pdata->r_sns;
331 } else {
332 return -EINVAL;
333 }
334 break;
335 default:
336 return -EINVAL;
337 }
338 return 0;
339}
340
341static int max17042_set_property(struct power_supply *psy,
342 enum power_supply_property psp,
343 const union power_supply_propval *val)
344{
345 struct max17042_chip *chip = power_supply_get_drvdata(psy);
346 struct regmap *map = chip->regmap;
347 int ret = 0;
348 u32 data;
349 int8_t temp;
350
351 switch (psp) {
352 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
353 ret = regmap_read(map, MAX17042_TALRT_Th, &data);
354 if (ret < 0)
355 return ret;
356
357 /* Input in deci-centigrade, convert to centigrade */
358 temp = val->intval / 10;
359 /* force min < max */
360 if (temp >= (int8_t)(data >> 8))
361 temp = (int8_t)(data >> 8) - 1;
362 /* Write both MAX and MIN ALERT */
363 data = (data & 0xff00) + temp;
364 ret = regmap_write(map, MAX17042_TALRT_Th, data);
365 break;
366 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
367 ret = regmap_read(map, MAX17042_TALRT_Th, &data);
368 if (ret < 0)
369 return ret;
370
371 /* Input in Deci-Centigrade, convert to centigrade */
372 temp = val->intval / 10;
373 /* force max > min */
374 if (temp <= (int8_t)(data & 0xff))
375 temp = (int8_t)(data & 0xff) + 1;
376 /* Write both MAX and MIN ALERT */
377 data = (data & 0xff) + (temp << 8);
378 ret = regmap_write(map, MAX17042_TALRT_Th, data);
379 break;
380 default:
381 ret = -EINVAL;
382 }
383
384 return ret;
385}
386
387static int max17042_property_is_writeable(struct power_supply *psy,
388 enum power_supply_property psp)
389{
390 int ret;
391
392 switch (psp) {
393 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
394 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
395 ret = 1;
396 break;
397 default:
398 ret = 0;
399 }
400
401 return ret;
402}
403
404static int max17042_write_verify_reg(struct regmap *map, u8 reg, u32 value)
405{
406 int retries = 8;
407 int ret;
408 u32 read_value;
409
410 do {
411 ret = regmap_write(map, reg, value);
412 regmap_read(map, reg, &read_value);
413 if (read_value != value) {
414 ret = -EIO;
415 retries--;
416 }
417 } while (retries && read_value != value);
418
419 if (ret < 0)
420 pr_err("%s: err %d\n", __func__, ret);
421
422 return ret;
423}
424
425static inline void max17042_override_por(struct regmap *map,
426 u8 reg, u16 value)
427{
428 if (value)
429 regmap_write(map, reg, value);
430}
431
432static inline void max10742_unlock_model(struct max17042_chip *chip)
433{
434 struct regmap *map = chip->regmap;
435
436 regmap_write(map, MAX17042_MLOCKReg1, MODEL_UNLOCK1);
437 regmap_write(map, MAX17042_MLOCKReg2, MODEL_UNLOCK2);
438}
439
440static inline void max10742_lock_model(struct max17042_chip *chip)
441{
442 struct regmap *map = chip->regmap;
443
444 regmap_write(map, MAX17042_MLOCKReg1, MODEL_LOCK1);
445 regmap_write(map, MAX17042_MLOCKReg2, MODEL_LOCK2);
446}
447
448static inline void max17042_write_model_data(struct max17042_chip *chip,
449 u8 addr, int size)
450{
451 struct regmap *map = chip->regmap;
452 int i;
453
454 for (i = 0; i < size; i++)
455 regmap_write(map, addr + i,
456 chip->pdata->config_data->cell_char_tbl[i]);
457}
458
459static inline void max17042_read_model_data(struct max17042_chip *chip,
460 u8 addr, u32 *data, int size)
461{
462 struct regmap *map = chip->regmap;
463 int i;
464
465 for (i = 0; i < size; i++)
466 regmap_read(map, addr + i, &data[i]);
467}
468
469static inline int max17042_model_data_compare(struct max17042_chip *chip,
470 u16 *data1, u16 *data2, int size)
471{
472 int i;
473
474 if (memcmp(data1, data2, size)) {
475 dev_err(&chip->client->dev, "%s compare failed\n", __func__);
476 for (i = 0; i < size; i++)
477 dev_info(&chip->client->dev, "0x%x, 0x%x",
478 data1[i], data2[i]);
479 dev_info(&chip->client->dev, "\n");
480 return -EINVAL;
481 }
482 return 0;
483}
484
485static int max17042_init_model(struct max17042_chip *chip)
486{
487 int ret;
488 int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl);
489 u32 *temp_data;
490
491 temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL);
492 if (!temp_data)
493 return -ENOMEM;
494
495 max10742_unlock_model(chip);
496 max17042_write_model_data(chip, MAX17042_MODELChrTbl,
497 table_size);
498 max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data,
499 table_size);
500
501 ret = max17042_model_data_compare(
502 chip,
503 chip->pdata->config_data->cell_char_tbl,
504 (u16 *)temp_data,
505 table_size);
506
507 max10742_lock_model(chip);
508 kfree(temp_data);
509
510 return ret;
511}
512
513static int max17042_verify_model_lock(struct max17042_chip *chip)
514{
515 int i;
516 int table_size = ARRAY_SIZE(chip->pdata->config_data->cell_char_tbl);
517 u32 *temp_data;
518 int ret = 0;
519
520 temp_data = kcalloc(table_size, sizeof(*temp_data), GFP_KERNEL);
521 if (!temp_data)
522 return -ENOMEM;
523
524 max17042_read_model_data(chip, MAX17042_MODELChrTbl, temp_data,
525 table_size);
526 for (i = 0; i < table_size; i++)
527 if (temp_data[i])
528 ret = -EINVAL;
529
530 kfree(temp_data);
531 return ret;
532}
533
534static void max17042_write_config_regs(struct max17042_chip *chip)
535{
536 struct max17042_config_data *config = chip->pdata->config_data;
537 struct regmap *map = chip->regmap;
538
539 regmap_write(map, MAX17042_CONFIG, config->config);
540 regmap_write(map, MAX17042_LearnCFG, config->learn_cfg);
541 regmap_write(map, MAX17042_FilterCFG,
542 config->filter_cfg);
543 regmap_write(map, MAX17042_RelaxCFG, config->relax_cfg);
544 if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17047 ||
545 chip->chip_type == MAXIM_DEVICE_TYPE_MAX17050)
546 regmap_write(map, MAX17047_FullSOCThr,
547 config->full_soc_thresh);
548}
549
550static void max17042_write_custom_regs(struct max17042_chip *chip)
551{
552 struct max17042_config_data *config = chip->pdata->config_data;
553 struct regmap *map = chip->regmap;
554
555 max17042_write_verify_reg(map, MAX17042_RCOMP0, config->rcomp0);
556 max17042_write_verify_reg(map, MAX17042_TempCo, config->tcompc0);
557 max17042_write_verify_reg(map, MAX17042_ICHGTerm, config->ichgt_term);
558 if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042) {
559 regmap_write(map, MAX17042_EmptyTempCo, config->empty_tempco);
560 max17042_write_verify_reg(map, MAX17042_K_empty0,
561 config->kempty0);
562 } else {
563 max17042_write_verify_reg(map, MAX17047_QRTbl00,
564 config->qrtbl00);
565 max17042_write_verify_reg(map, MAX17047_QRTbl10,
566 config->qrtbl10);
567 max17042_write_verify_reg(map, MAX17047_QRTbl20,
568 config->qrtbl20);
569 max17042_write_verify_reg(map, MAX17047_QRTbl30,
570 config->qrtbl30);
571 }
572}
573
574static void max17042_update_capacity_regs(struct max17042_chip *chip)
575{
576 struct max17042_config_data *config = chip->pdata->config_data;
577 struct regmap *map = chip->regmap;
578
579 max17042_write_verify_reg(map, MAX17042_FullCAP,
580 config->fullcap);
581 regmap_write(map, MAX17042_DesignCap, config->design_cap);
582 max17042_write_verify_reg(map, MAX17042_FullCAPNom,
583 config->fullcapnom);
584}
585
586static void max17042_reset_vfsoc0_reg(struct max17042_chip *chip)
587{
588 unsigned int vfSoc;
589 struct regmap *map = chip->regmap;
590
591 regmap_read(map, MAX17042_VFSOC, &vfSoc);
592 regmap_write(map, MAX17042_VFSOC0Enable, VFSOC0_UNLOCK);
593 max17042_write_verify_reg(map, MAX17042_VFSOC0, vfSoc);
594 regmap_write(map, MAX17042_VFSOC0Enable, VFSOC0_LOCK);
595}
596
597static void max17042_load_new_capacity_params(struct max17042_chip *chip)
598{
599 u32 full_cap0, rep_cap, dq_acc, vfSoc;
600 u32 rem_cap;
601
602 struct max17042_config_data *config = chip->pdata->config_data;
603 struct regmap *map = chip->regmap;
604
605 regmap_read(map, MAX17042_FullCAP0, &full_cap0);
606 regmap_read(map, MAX17042_VFSOC, &vfSoc);
607
608 /* fg_vfSoc needs to shifted by 8 bits to get the
609 * perc in 1% accuracy, to get the right rem_cap multiply
610 * full_cap0, fg_vfSoc and devide by 100
611 */
612 rem_cap = ((vfSoc >> 8) * full_cap0) / 100;
613 max17042_write_verify_reg(map, MAX17042_RemCap, rem_cap);
614
615 rep_cap = rem_cap;
616 max17042_write_verify_reg(map, MAX17042_RepCap, rep_cap);
617
618 /* Write dQ_acc to 200% of Capacity and dP_acc to 200% */
619 dq_acc = config->fullcap / dQ_ACC_DIV;
620 max17042_write_verify_reg(map, MAX17042_dQacc, dq_acc);
621 max17042_write_verify_reg(map, MAX17042_dPacc, dP_ACC_200);
622
623 max17042_write_verify_reg(map, MAX17042_FullCAP,
624 config->fullcap);
625 regmap_write(map, MAX17042_DesignCap,
626 config->design_cap);
627 max17042_write_verify_reg(map, MAX17042_FullCAPNom,
628 config->fullcapnom);
629 /* Update SOC register with new SOC */
630 regmap_write(map, MAX17042_RepSOC, vfSoc);
631}
632
633/*
634 * Block write all the override values coming from platform data.
635 * This function MUST be called before the POR initialization proceedure
636 * specified by maxim.
637 */
638static inline void max17042_override_por_values(struct max17042_chip *chip)
639{
640 struct regmap *map = chip->regmap;
641 struct max17042_config_data *config = chip->pdata->config_data;
642
643 max17042_override_por(map, MAX17042_TGAIN, config->tgain);
644 max17042_override_por(map, MAx17042_TOFF, config->toff);
645 max17042_override_por(map, MAX17042_CGAIN, config->cgain);
646 max17042_override_por(map, MAX17042_COFF, config->coff);
647
648 max17042_override_por(map, MAX17042_VALRT_Th, config->valrt_thresh);
649 max17042_override_por(map, MAX17042_TALRT_Th, config->talrt_thresh);
650 max17042_override_por(map, MAX17042_SALRT_Th,
651 config->soc_alrt_thresh);
652 max17042_override_por(map, MAX17042_CONFIG, config->config);
653 max17042_override_por(map, MAX17042_SHDNTIMER, config->shdntimer);
654
655 max17042_override_por(map, MAX17042_DesignCap, config->design_cap);
656 max17042_override_por(map, MAX17042_ICHGTerm, config->ichgt_term);
657
658 max17042_override_por(map, MAX17042_AtRate, config->at_rate);
659 max17042_override_por(map, MAX17042_LearnCFG, config->learn_cfg);
660 max17042_override_por(map, MAX17042_FilterCFG, config->filter_cfg);
661 max17042_override_por(map, MAX17042_RelaxCFG, config->relax_cfg);
662 max17042_override_por(map, MAX17042_MiscCFG, config->misc_cfg);
663 max17042_override_por(map, MAX17042_MaskSOC, config->masksoc);
664
665 max17042_override_por(map, MAX17042_FullCAP, config->fullcap);
666 max17042_override_por(map, MAX17042_FullCAPNom, config->fullcapnom);
667 if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042)
668 max17042_override_por(map, MAX17042_SOC_empty,
669 config->socempty);
670 max17042_override_por(map, MAX17042_LAvg_empty, config->lavg_empty);
671 max17042_override_por(map, MAX17042_dQacc, config->dqacc);
672 max17042_override_por(map, MAX17042_dPacc, config->dpacc);
673
674 if (chip->chip_type == MAXIM_DEVICE_TYPE_MAX17042)
675 max17042_override_por(map, MAX17042_V_empty, config->vempty);
676 else
677 max17042_override_por(map, MAX17047_V_empty, config->vempty);
678 max17042_override_por(map, MAX17042_TempNom, config->temp_nom);
679 max17042_override_por(map, MAX17042_TempLim, config->temp_lim);
680 max17042_override_por(map, MAX17042_FCTC, config->fctc);
681 max17042_override_por(map, MAX17042_RCOMP0, config->rcomp0);
682 max17042_override_por(map, MAX17042_TempCo, config->tcompc0);
683 if (chip->chip_type) {
684 max17042_override_por(map, MAX17042_EmptyTempCo,
685 config->empty_tempco);
686 max17042_override_por(map, MAX17042_K_empty0,
687 config->kempty0);
688 }
689}
690
691static int max17042_init_chip(struct max17042_chip *chip)
692{
693 struct regmap *map = chip->regmap;
694 int ret;
695
696 max17042_override_por_values(chip);
697 /* After Power up, the MAX17042 requires 500mS in order
698 * to perform signal debouncing and initial SOC reporting
699 */
700 msleep(500);
701
702 /* Initialize configaration */
703 max17042_write_config_regs(chip);
704
705 /* write cell characterization data */
706 ret = max17042_init_model(chip);
707 if (ret) {
708 dev_err(&chip->client->dev, "%s init failed\n",
709 __func__);
710 return -EIO;
711 }
712
713 ret = max17042_verify_model_lock(chip);
714 if (ret) {
715 dev_err(&chip->client->dev, "%s lock verify failed\n",
716 __func__);
717 return -EIO;
718 }
719 /* write custom parameters */
720 max17042_write_custom_regs(chip);
721
722 /* update capacity params */
723 max17042_update_capacity_regs(chip);
724
725 /* delay must be atleast 350mS to allow VFSOC
726 * to be calculated from the new configuration
727 */
728 msleep(350);
729
730 /* reset vfsoc0 reg */
731 max17042_reset_vfsoc0_reg(chip);
732
733 /* load new capacity params */
734 max17042_load_new_capacity_params(chip);
735
736 /* Init complete, Clear the POR bit */
737 regmap_update_bits(map, MAX17042_STATUS, STATUS_POR_BIT, 0x0);
738 return 0;
739}
740
741static void max17042_set_soc_threshold(struct max17042_chip *chip, u16 off)
742{
743 struct regmap *map = chip->regmap;
744 u32 soc, soc_tr;
745
746 /* program interrupt thesholds such that we should
747 * get interrupt for every 'off' perc change in the soc
748 */
749 regmap_read(map, MAX17042_RepSOC, &soc);
750 soc >>= 8;
751 soc_tr = (soc + off) << 8;
752 soc_tr |= (soc - off);
753 regmap_write(map, MAX17042_SALRT_Th, soc_tr);
754}
755
756static irqreturn_t max17042_thread_handler(int id, void *dev)
757{
758 struct max17042_chip *chip = dev;
759 u32 val;
760
761 regmap_read(chip->regmap, MAX17042_STATUS, &val);
762 if ((val & STATUS_INTR_SOCMIN_BIT) ||
763 (val & STATUS_INTR_SOCMAX_BIT)) {
764 dev_info(&chip->client->dev, "SOC threshold INTR\n");
765 max17042_set_soc_threshold(chip, 1);
766 }
767
768 power_supply_changed(chip->battery);
769 return IRQ_HANDLED;
770}
771
772static void max17042_init_worker(struct work_struct *work)
773{
774 struct max17042_chip *chip = container_of(work,
775 struct max17042_chip, work);
776 int ret;
777
778 /* Initialize registers according to values from the platform data */
779 if (chip->pdata->enable_por_init && chip->pdata->config_data) {
780 ret = max17042_init_chip(chip);
781 if (ret)
782 return;
783 }
784
785 chip->init_complete = 1;
786}
787
788#ifdef CONFIG_OF
789static struct max17042_platform_data *
790max17042_get_pdata(struct device *dev)
791{
792 struct device_node *np = dev->of_node;
793 u32 prop;
794 struct max17042_platform_data *pdata;
795
796 if (!np)
797 return dev->platform_data;
798
799 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
800 if (!pdata)
801 return NULL;
802
803 /*
804 * Require current sense resistor value to be specified for
805 * current-sense functionality to be enabled at all.
806 */
807 if (of_property_read_u32(np, "maxim,rsns-microohm", &prop) == 0) {
808 pdata->r_sns = prop;
809 pdata->enable_current_sense = true;
810 }
811
812 if (of_property_read_s32(np, "maxim,cold-temp", &pdata->temp_min))
813 pdata->temp_min = INT_MIN;
814 if (of_property_read_s32(np, "maxim,over-heat-temp", &pdata->temp_max))
815 pdata->temp_max = INT_MAX;
816 if (of_property_read_s32(np, "maxim,dead-volt", &pdata->vmin))
817 pdata->vmin = INT_MIN;
818 if (of_property_read_s32(np, "maxim,over-volt", &pdata->vmax))
819 pdata->vmax = INT_MAX;
820
821 return pdata;
822}
823#else
824static struct max17042_platform_data *
825max17042_get_pdata(struct device *dev)
826{
827 return dev->platform_data;
828}
829#endif
830
831static const struct regmap_config max17042_regmap_config = {
832 .reg_bits = 8,
833 .val_bits = 16,
834 .val_format_endian = REGMAP_ENDIAN_NATIVE,
835};
836
837static const struct power_supply_desc max17042_psy_desc = {
838 .name = "max170xx_battery",
839 .type = POWER_SUPPLY_TYPE_BATTERY,
840 .get_property = max17042_get_property,
841 .set_property = max17042_set_property,
842 .property_is_writeable = max17042_property_is_writeable,
843 .properties = max17042_battery_props,
844 .num_properties = ARRAY_SIZE(max17042_battery_props),
845};
846
847static const struct power_supply_desc max17042_no_current_sense_psy_desc = {
848 .name = "max170xx_battery",
849 .type = POWER_SUPPLY_TYPE_BATTERY,
850 .get_property = max17042_get_property,
851 .set_property = max17042_set_property,
852 .property_is_writeable = max17042_property_is_writeable,
853 .properties = max17042_battery_props,
854 .num_properties = ARRAY_SIZE(max17042_battery_props) - 2,
855};
856
857static int max17042_probe(struct i2c_client *client,
858 const struct i2c_device_id *id)
859{
860 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
861 const struct power_supply_desc *max17042_desc = &max17042_psy_desc;
862 struct power_supply_config psy_cfg = {};
863 struct max17042_chip *chip;
864 int ret;
865 int i;
866 u32 val;
867
868 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
869 return -EIO;
870
871 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
872 if (!chip)
873 return -ENOMEM;
874
875 chip->client = client;
876 chip->regmap = devm_regmap_init_i2c(client, &max17042_regmap_config);
877 if (IS_ERR(chip->regmap)) {
878 dev_err(&client->dev, "Failed to initialize regmap\n");
879 return -EINVAL;
880 }
881
882 chip->pdata = max17042_get_pdata(&client->dev);
883 if (!chip->pdata) {
884 dev_err(&client->dev, "no platform data provided\n");
885 return -EINVAL;
886 }
887
888 i2c_set_clientdata(client, chip);
889 chip->chip_type = id->driver_data;
890 psy_cfg.drv_data = chip;
891
892 /* When current is not measured,
893 * CURRENT_NOW and CURRENT_AVG properties should be invisible. */
894 if (!chip->pdata->enable_current_sense)
895 max17042_desc = &max17042_no_current_sense_psy_desc;
896
897 if (chip->pdata->r_sns == 0)
898 chip->pdata->r_sns = MAX17042_DEFAULT_SNS_RESISTOR;
899
900 if (chip->pdata->init_data)
901 for (i = 0; i < chip->pdata->num_init_data; i++)
902 regmap_write(chip->regmap,
903 chip->pdata->init_data[i].addr,
904 chip->pdata->init_data[i].data);
905
906 if (!chip->pdata->enable_current_sense) {
907 regmap_write(chip->regmap, MAX17042_CGAIN, 0x0000);
908 regmap_write(chip->regmap, MAX17042_MiscCFG, 0x0003);
909 regmap_write(chip->regmap, MAX17042_LearnCFG, 0x0007);
910 }
911
912 chip->battery = devm_power_supply_register(&client->dev, max17042_desc,
913 &psy_cfg);
914 if (IS_ERR(chip->battery)) {
915 dev_err(&client->dev, "failed: power supply register\n");
916 return PTR_ERR(chip->battery);
917 }
918
919 if (client->irq) {
920 ret = devm_request_threaded_irq(&client->dev, client->irq,
921 NULL,
922 max17042_thread_handler,
923 IRQF_TRIGGER_FALLING |
924 IRQF_ONESHOT,
925 chip->battery->desc->name,
926 chip);
927 if (!ret) {
928 regmap_update_bits(chip->regmap, MAX17042_CONFIG,
929 CONFIG_ALRT_BIT_ENBL,
930 CONFIG_ALRT_BIT_ENBL);
931 max17042_set_soc_threshold(chip, 1);
932 } else {
933 client->irq = 0;
934 dev_err(&client->dev, "%s(): cannot get IRQ\n",
935 __func__);
936 }
937 }
938
939 regmap_read(chip->regmap, MAX17042_STATUS, &val);
940 if (val & STATUS_POR_BIT) {
941 INIT_WORK(&chip->work, max17042_init_worker);
942 schedule_work(&chip->work);
943 } else {
944 chip->init_complete = 1;
945 }
946
947 return 0;
948}
949
950#ifdef CONFIG_PM_SLEEP
951static int max17042_suspend(struct device *dev)
952{
953 struct max17042_chip *chip = dev_get_drvdata(dev);
954
955 /*
956 * disable the irq and enable irq_wake
957 * capability to the interrupt line.
958 */
959 if (chip->client->irq) {
960 disable_irq(chip->client->irq);
961 enable_irq_wake(chip->client->irq);
962 }
963
964 return 0;
965}
966
967static int max17042_resume(struct device *dev)
968{
969 struct max17042_chip *chip = dev_get_drvdata(dev);
970
971 if (chip->client->irq) {
972 disable_irq_wake(chip->client->irq);
973 enable_irq(chip->client->irq);
974 /* re-program the SOC thresholds to 1% change */
975 max17042_set_soc_threshold(chip, 1);
976 }
977
978 return 0;
979}
980#endif
981
982static SIMPLE_DEV_PM_OPS(max17042_pm_ops, max17042_suspend,
983 max17042_resume);
984
985#ifdef CONFIG_OF
986static const struct of_device_id max17042_dt_match[] = {
987 { .compatible = "maxim,max17042" },
988 { .compatible = "maxim,max17047" },
989 { .compatible = "maxim,max17050" },
990 { },
991};
992MODULE_DEVICE_TABLE(of, max17042_dt_match);
993#endif
994
995static const struct i2c_device_id max17042_id[] = {
996 { "max17042", MAXIM_DEVICE_TYPE_MAX17042 },
997 { "max17047", MAXIM_DEVICE_TYPE_MAX17047 },
998 { "max17050", MAXIM_DEVICE_TYPE_MAX17050 },
999 { }
1000};
1001MODULE_DEVICE_TABLE(i2c, max17042_id);
1002
1003static struct i2c_driver max17042_i2c_driver = {
1004 .driver = {
1005 .name = "max17042",
1006 .of_match_table = of_match_ptr(max17042_dt_match),
1007 .pm = &max17042_pm_ops,
1008 },
1009 .probe = max17042_probe,
1010 .id_table = max17042_id,
1011};
1012module_i2c_driver(max17042_i2c_driver);
1013
1014MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1015MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
1016MODULE_LICENSE("GPL");