2 * Fuel gauge driver for Maxim 17042 / 8966 / 8997
3 * Note that Maxim 8966 and 8997 are mfd and this is its subdevice.
5 * Copyright (C) 2011 Samsung Electronics
6 * MyungJoo Ham <myungjoo.ham@samsung.com>
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.
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.
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
22 * This driver is based on max17040_battery.c
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>
32 #include <linux/mod_devicetable.h>
33 #include <linux/power_supply.h>
34 #include <linux/power/max17042_battery.h>
36 #include <linux/regmap.h>
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)
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)
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
62 #define dQ_ACC_DIV 0x4
63 #define dP_ACC_100 0x1900
64 #define dP_ACC_200 0x3200
66 #define MAX17042_VMAX_TOLERANCE 50 /* 50 mV */
68 struct 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
;
78 static 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
,
99 static int max17042_get_temperature(struct max17042_chip
*chip
, int *temp
)
103 struct regmap
*map
= chip
->regmap
;
105 ret
= regmap_read(map
, MAX17042_TEMP
, &data
);
110 /* The value is signed. */
111 if (*temp
& 0x8000) {
112 *temp
= (0x7fff & ~*temp
) + 1;
116 /* The value is converted into deci-centigrade scale */
117 /* Units of LSB = 1 / 256 degree Celsius */
118 *temp
= *temp
* 10 / 256;
122 static int max17042_get_battery_health(struct max17042_chip
*chip
, int *health
)
124 int temp
, vavg
, vbatt
, ret
;
127 ret
= regmap_read(chip
->regmap
, MAX17042_AvgVCELL
, &val
);
131 /* bits [0-3] unused */
132 vavg
= val
* 625 / 8;
133 /* Convert to millivolts */
136 ret
= regmap_read(chip
->regmap
, MAX17042_VCELL
, &val
);
140 /* bits [0-3] unused */
141 vbatt
= val
* 625 / 8;
142 /* Convert to millivolts */
145 if (vavg
< chip
->pdata
->vmin
) {
146 *health
= POWER_SUPPLY_HEALTH_DEAD
;
150 if (vbatt
> chip
->pdata
->vmax
+ MAX17042_VMAX_TOLERANCE
) {
151 *health
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
155 ret
= max17042_get_temperature(chip
, &temp
);
159 if (temp
<= chip
->pdata
->temp_min
) {
160 *health
= POWER_SUPPLY_HEALTH_COLD
;
164 if (temp
>= chip
->pdata
->temp_max
) {
165 *health
= POWER_SUPPLY_HEALTH_OVERHEAT
;
169 *health
= POWER_SUPPLY_HEALTH_GOOD
;
178 static int max17042_get_property(struct power_supply
*psy
,
179 enum power_supply_property psp
,
180 union power_supply_propval
*val
)
182 struct max17042_chip
*chip
= power_supply_get_drvdata(psy
);
183 struct regmap
*map
= chip
->regmap
;
187 if (!chip
->init_complete
)
191 case POWER_SUPPLY_PROP_PRESENT
:
192 ret
= regmap_read(map
, MAX17042_STATUS
, &data
);
196 if (data
& MAX17042_STATUS_BattAbsent
)
201 case POWER_SUPPLY_PROP_CYCLE_COUNT
:
202 ret
= regmap_read(map
, MAX17042_Cycles
, &data
);
208 case POWER_SUPPLY_PROP_VOLTAGE_MAX
:
209 ret
= regmap_read(map
, MAX17042_MinMaxVolt
, &data
);
213 val
->intval
= data
>> 8;
214 val
->intval
*= 20000; /* Units of LSB = 20mV */
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
);
220 ret
= regmap_read(map
, MAX17047_V_empty
, &data
);
224 val
->intval
= data
>> 7;
225 val
->intval
*= 10000; /* Units of LSB = 10mV */
227 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
228 ret
= regmap_read(map
, MAX17042_VCELL
, &data
);
232 val
->intval
= data
* 625 / 8;
234 case POWER_SUPPLY_PROP_VOLTAGE_AVG
:
235 ret
= regmap_read(map
, MAX17042_AvgVCELL
, &data
);
239 val
->intval
= data
* 625 / 8;
241 case POWER_SUPPLY_PROP_VOLTAGE_OCV
:
242 ret
= regmap_read(map
, MAX17042_OCVInternal
, &data
);
246 val
->intval
= data
* 625 / 8;
248 case POWER_SUPPLY_PROP_CAPACITY
:
249 ret
= regmap_read(map
, MAX17042_RepSOC
, &data
);
253 val
->intval
= data
>> 8;
255 case POWER_SUPPLY_PROP_CHARGE_FULL
:
256 ret
= regmap_read(map
, MAX17042_FullCAP
, &data
);
260 val
->intval
= data
* 1000 / 2;
262 case POWER_SUPPLY_PROP_CHARGE_COUNTER
:
263 ret
= regmap_read(map
, MAX17042_QH
, &data
);
267 val
->intval
= data
* 1000 / 2;
269 case POWER_SUPPLY_PROP_TEMP
:
270 ret
= max17042_get_temperature(chip
, &val
->intval
);
274 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN
:
275 ret
= regmap_read(map
, MAX17042_TALRT_Th
, &data
);
278 /* LSB is Alert Minimum. In deci-centigrade */
279 val
->intval
= (data
& 0xff) * 10;
281 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX
:
282 ret
= regmap_read(map
, MAX17042_TALRT_Th
, &data
);
285 /* MSB is Alert Maximum. In deci-centigrade */
286 val
->intval
= (data
>> 8) * 10;
288 case POWER_SUPPLY_PROP_TEMP_MIN
:
289 val
->intval
= chip
->pdata
->temp_min
;
291 case POWER_SUPPLY_PROP_TEMP_MAX
:
292 val
->intval
= chip
->pdata
->temp_max
;
294 case POWER_SUPPLY_PROP_HEALTH
:
295 ret
= max17042_get_battery_health(chip
, &val
->intval
);
299 case POWER_SUPPLY_PROP_CURRENT_NOW
:
300 if (chip
->pdata
->enable_current_sense
) {
301 ret
= regmap_read(map
, MAX17042_Current
, &data
);
306 if (val
->intval
& 0x8000) {
308 val
->intval
= ~val
->intval
& 0x7fff;
312 val
->intval
*= 1562500 / chip
->pdata
->r_sns
;
317 case POWER_SUPPLY_PROP_CURRENT_AVG
:
318 if (chip
->pdata
->enable_current_sense
) {
319 ret
= regmap_read(map
, MAX17042_AvgCurrent
, &data
);
324 if (val
->intval
& 0x8000) {
326 val
->intval
= ~val
->intval
& 0x7fff;
330 val
->intval
*= 1562500 / chip
->pdata
->r_sns
;
341 static int max17042_set_property(struct power_supply
*psy
,
342 enum power_supply_property psp
,
343 const union power_supply_propval
*val
)
345 struct max17042_chip
*chip
= power_supply_get_drvdata(psy
);
346 struct regmap
*map
= chip
->regmap
;
352 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN
:
353 ret
= regmap_read(map
, MAX17042_TALRT_Th
, &data
);
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
);
366 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX
:
367 ret
= regmap_read(map
, MAX17042_TALRT_Th
, &data
);
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
);
387 static int max17042_property_is_writeable(struct power_supply
*psy
,
388 enum power_supply_property psp
)
393 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN
:
394 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX
:
404 static int max17042_write_verify_reg(struct regmap
*map
, u8 reg
, u32 value
)
411 ret
= regmap_write(map
, reg
, value
);
412 regmap_read(map
, reg
, &read_value
);
413 if (read_value
!= value
) {
417 } while (retries
&& read_value
!= value
);
420 pr_err("%s: err %d\n", __func__
, ret
);
425 static inline void max17042_override_por(struct regmap
*map
,
429 regmap_write(map
, reg
, value
);
432 static inline void max10742_unlock_model(struct max17042_chip
*chip
)
434 struct regmap
*map
= chip
->regmap
;
436 regmap_write(map
, MAX17042_MLOCKReg1
, MODEL_UNLOCK1
);
437 regmap_write(map
, MAX17042_MLOCKReg2
, MODEL_UNLOCK2
);
440 static inline void max10742_lock_model(struct max17042_chip
*chip
)
442 struct regmap
*map
= chip
->regmap
;
444 regmap_write(map
, MAX17042_MLOCKReg1
, MODEL_LOCK1
);
445 regmap_write(map
, MAX17042_MLOCKReg2
, MODEL_LOCK2
);
448 static inline void max17042_write_model_data(struct max17042_chip
*chip
,
451 struct regmap
*map
= chip
->regmap
;
454 for (i
= 0; i
< size
; i
++)
455 regmap_write(map
, addr
+ i
,
456 chip
->pdata
->config_data
->cell_char_tbl
[i
]);
459 static inline void max17042_read_model_data(struct max17042_chip
*chip
,
460 u8 addr
, u32
*data
, int size
)
462 struct regmap
*map
= chip
->regmap
;
465 for (i
= 0; i
< size
; i
++)
466 regmap_read(map
, addr
+ i
, &data
[i
]);
469 static inline int max17042_model_data_compare(struct max17042_chip
*chip
,
470 u16
*data1
, u16
*data2
, int size
)
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",
479 dev_info(&chip
->client
->dev
, "\n");
485 static int max17042_init_model(struct max17042_chip
*chip
)
488 int table_size
= ARRAY_SIZE(chip
->pdata
->config_data
->cell_char_tbl
);
491 temp_data
= kcalloc(table_size
, sizeof(*temp_data
), GFP_KERNEL
);
495 max10742_unlock_model(chip
);
496 max17042_write_model_data(chip
, MAX17042_MODELChrTbl
,
498 max17042_read_model_data(chip
, MAX17042_MODELChrTbl
, temp_data
,
501 ret
= max17042_model_data_compare(
503 chip
->pdata
->config_data
->cell_char_tbl
,
507 max10742_lock_model(chip
);
513 static int max17042_verify_model_lock(struct max17042_chip
*chip
)
516 int table_size
= ARRAY_SIZE(chip
->pdata
->config_data
->cell_char_tbl
);
520 temp_data
= kcalloc(table_size
, sizeof(*temp_data
), GFP_KERNEL
);
524 max17042_read_model_data(chip
, MAX17042_MODELChrTbl
, temp_data
,
526 for (i
= 0; i
< table_size
; i
++)
534 static void max17042_write_config_regs(struct max17042_chip
*chip
)
536 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
537 struct regmap
*map
= chip
->regmap
;
539 regmap_write(map
, MAX17042_CONFIG
, config
->config
);
540 regmap_write(map
, MAX17042_LearnCFG
, config
->learn_cfg
);
541 regmap_write(map
, MAX17042_FilterCFG
,
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
);
550 static void max17042_write_custom_regs(struct max17042_chip
*chip
)
552 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
553 struct regmap
*map
= chip
->regmap
;
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
,
563 max17042_write_verify_reg(map
, MAX17047_QRTbl00
,
565 max17042_write_verify_reg(map
, MAX17047_QRTbl10
,
567 max17042_write_verify_reg(map
, MAX17047_QRTbl20
,
569 max17042_write_verify_reg(map
, MAX17047_QRTbl30
,
574 static void max17042_update_capacity_regs(struct max17042_chip
*chip
)
576 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
577 struct regmap
*map
= chip
->regmap
;
579 max17042_write_verify_reg(map
, MAX17042_FullCAP
,
581 regmap_write(map
, MAX17042_DesignCap
, config
->design_cap
);
582 max17042_write_verify_reg(map
, MAX17042_FullCAPNom
,
586 static void max17042_reset_vfsoc0_reg(struct max17042_chip
*chip
)
589 struct regmap
*map
= chip
->regmap
;
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
);
597 static void max17042_load_new_capacity_params(struct max17042_chip
*chip
)
599 u32 full_cap0
, rep_cap
, dq_acc
, vfSoc
;
602 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
603 struct regmap
*map
= chip
->regmap
;
605 regmap_read(map
, MAX17042_FullCAP0
, &full_cap0
);
606 regmap_read(map
, MAX17042_VFSOC
, &vfSoc
);
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
612 rem_cap
= ((vfSoc
>> 8) * full_cap0
) / 100;
613 max17042_write_verify_reg(map
, MAX17042_RemCap
, rem_cap
);
616 max17042_write_verify_reg(map
, MAX17042_RepCap
, rep_cap
);
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
);
623 max17042_write_verify_reg(map
, MAX17042_FullCAP
,
625 regmap_write(map
, MAX17042_DesignCap
,
627 max17042_write_verify_reg(map
, MAX17042_FullCAPNom
,
629 /* Update SOC register with new SOC */
630 regmap_write(map
, MAX17042_RepSOC
, vfSoc
);
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.
638 static inline void max17042_override_por_values(struct max17042_chip
*chip
)
640 struct regmap
*map
= chip
->regmap
;
641 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
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
);
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
);
655 max17042_override_por(map
, MAX17042_DesignCap
, config
->design_cap
);
656 max17042_override_por(map
, MAX17042_ICHGTerm
, config
->ichgt_term
);
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
);
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
,
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
);
674 if (chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17042
)
675 max17042_override_por(map
, MAX17042_V_empty
, config
->vempty
);
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
,
691 static int max17042_init_chip(struct max17042_chip
*chip
)
693 struct regmap
*map
= chip
->regmap
;
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
702 /* Initialize configaration */
703 max17042_write_config_regs(chip
);
705 /* write cell characterization data */
706 ret
= max17042_init_model(chip
);
708 dev_err(&chip
->client
->dev
, "%s init failed\n",
713 ret
= max17042_verify_model_lock(chip
);
715 dev_err(&chip
->client
->dev
, "%s lock verify failed\n",
719 /* write custom parameters */
720 max17042_write_custom_regs(chip
);
722 /* update capacity params */
723 max17042_update_capacity_regs(chip
);
725 /* delay must be atleast 350mS to allow VFSOC
726 * to be calculated from the new configuration
730 /* reset vfsoc0 reg */
731 max17042_reset_vfsoc0_reg(chip
);
733 /* load new capacity params */
734 max17042_load_new_capacity_params(chip
);
736 /* Init complete, Clear the POR bit */
737 regmap_update_bits(map
, MAX17042_STATUS
, STATUS_POR_BIT
, 0x0);
741 static void max17042_set_soc_threshold(struct max17042_chip
*chip
, u16 off
)
743 struct regmap
*map
= chip
->regmap
;
746 /* program interrupt thesholds such that we should
747 * get interrupt for every 'off' perc change in the soc
749 regmap_read(map
, MAX17042_RepSOC
, &soc
);
751 soc_tr
= (soc
+ off
) << 8;
752 soc_tr
|= (soc
- off
);
753 regmap_write(map
, MAX17042_SALRT_Th
, soc_tr
);
756 static irqreturn_t
max17042_thread_handler(int id
, void *dev
)
758 struct max17042_chip
*chip
= dev
;
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);
768 power_supply_changed(chip
->battery
);
772 static void max17042_init_worker(struct work_struct
*work
)
774 struct max17042_chip
*chip
= container_of(work
,
775 struct max17042_chip
, work
);
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
);
785 chip
->init_complete
= 1;
789 static struct max17042_platform_data
*
790 max17042_get_pdata(struct device
*dev
)
792 struct device_node
*np
= dev
->of_node
;
794 struct max17042_platform_data
*pdata
;
797 return dev
->platform_data
;
799 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
804 * Require current sense resistor value to be specified for
805 * current-sense functionality to be enabled at all.
807 if (of_property_read_u32(np
, "maxim,rsns-microohm", &prop
) == 0) {
809 pdata
->enable_current_sense
= true;
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
;
824 static struct max17042_platform_data
*
825 max17042_get_pdata(struct device
*dev
)
827 return dev
->platform_data
;
831 static const struct regmap_config max17042_regmap_config
= {
834 .val_format_endian
= REGMAP_ENDIAN_NATIVE
,
837 static 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
),
847 static 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,
857 static int max17042_probe(struct i2c_client
*client
,
858 const struct i2c_device_id
*id
)
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
;
868 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_WORD_DATA
))
871 chip
= devm_kzalloc(&client
->dev
, sizeof(*chip
), GFP_KERNEL
);
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");
882 chip
->pdata
= max17042_get_pdata(&client
->dev
);
884 dev_err(&client
->dev
, "no platform data provided\n");
888 i2c_set_clientdata(client
, chip
);
889 chip
->chip_type
= id
->driver_data
;
890 psy_cfg
.drv_data
= chip
;
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
;
897 if (chip
->pdata
->r_sns
== 0)
898 chip
->pdata
->r_sns
= MAX17042_DEFAULT_SNS_RESISTOR
;
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
);
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);
912 chip
->battery
= devm_power_supply_register(&client
->dev
, max17042_desc
,
914 if (IS_ERR(chip
->battery
)) {
915 dev_err(&client
->dev
, "failed: power supply register\n");
916 return PTR_ERR(chip
->battery
);
920 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
922 max17042_thread_handler
,
923 IRQF_TRIGGER_FALLING
|
925 chip
->battery
->desc
->name
,
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);
934 dev_err(&client
->dev
, "%s(): cannot get IRQ\n",
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
);
944 chip
->init_complete
= 1;
950 #ifdef CONFIG_PM_SLEEP
951 static int max17042_suspend(struct device
*dev
)
953 struct max17042_chip
*chip
= dev_get_drvdata(dev
);
956 * disable the irq and enable irq_wake
957 * capability to the interrupt line.
959 if (chip
->client
->irq
) {
960 disable_irq(chip
->client
->irq
);
961 enable_irq_wake(chip
->client
->irq
);
967 static int max17042_resume(struct device
*dev
)
969 struct max17042_chip
*chip
= dev_get_drvdata(dev
);
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);
982 static SIMPLE_DEV_PM_OPS(max17042_pm_ops
, max17042_suspend
,
986 static const struct of_device_id max17042_dt_match
[] = {
987 { .compatible
= "maxim,max17042" },
988 { .compatible
= "maxim,max17047" },
989 { .compatible
= "maxim,max17050" },
992 MODULE_DEVICE_TABLE(of
, max17042_dt_match
);
995 static 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
},
1001 MODULE_DEVICE_TABLE(i2c
, max17042_id
);
1003 static struct i2c_driver max17042_i2c_driver
= {
1006 .of_match_table
= of_match_ptr(max17042_dt_match
),
1007 .pm
= &max17042_pm_ops
,
1009 .probe
= max17042_probe
,
1010 .id_table
= max17042_id
,
1012 module_i2c_driver(max17042_i2c_driver
);
1014 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1015 MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
1016 MODULE_LICENSE("GPL");