1 // SPDX-License-Identifier: GPL-2.0+
3 // Fuel gauge driver for Maxim 17042 / 8966 / 8997
4 // Note that Maxim 8966 and 8997 are mfd and this is its subdevice.
6 // Copyright (C) 2011 Samsung Electronics
7 // MyungJoo Ham <myungjoo.ham@samsung.com>
9 // This driver is based on max17040_battery.c
11 #include <linux/acpi.h>
12 #include <linux/devm-helpers.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/i2c.h>
17 #include <linux/delay.h>
18 #include <linux/interrupt.h>
20 #include <linux/mod_devicetable.h>
21 #include <linux/power_supply.h>
22 #include <linux/power/max17042_battery.h>
24 #include <linux/regmap.h>
26 /* Status register bits */
27 #define STATUS_POR_BIT (1 << 1)
28 #define STATUS_BST_BIT (1 << 3)
29 #define STATUS_VMN_BIT (1 << 8)
30 #define STATUS_TMN_BIT (1 << 9)
31 #define STATUS_SMN_BIT (1 << 10)
32 #define STATUS_BI_BIT (1 << 11)
33 #define STATUS_VMX_BIT (1 << 12)
34 #define STATUS_TMX_BIT (1 << 13)
35 #define STATUS_SMX_BIT (1 << 14)
36 #define STATUS_BR_BIT (1 << 15)
38 /* Interrupt mask bits */
39 #define CFG_ALRT_BIT_ENBL (1 << 2)
41 #define VFSOC0_LOCK 0x0000
42 #define VFSOC0_UNLOCK 0x0080
43 #define MODEL_UNLOCK1 0X0059
44 #define MODEL_UNLOCK2 0X00C4
45 #define MODEL_LOCK1 0X0000
46 #define MODEL_LOCK2 0X0000
48 #define dQ_ACC_DIV 0x4
49 #define dP_ACC_100 0x1900
50 #define dP_ACC_200 0x3200
52 #define MAX17042_VMAX_TOLERANCE 50 /* 50 mV */
54 struct max17042_chip
{
55 struct i2c_client
*client
;
56 struct regmap
*regmap
;
57 struct power_supply
*battery
;
58 enum max170xx_chip_type chip_type
;
59 struct max17042_platform_data
*pdata
;
60 struct work_struct work
;
64 static enum power_supply_property max17042_battery_props
[] = {
65 POWER_SUPPLY_PROP_STATUS
,
66 POWER_SUPPLY_PROP_PRESENT
,
67 POWER_SUPPLY_PROP_TECHNOLOGY
,
68 POWER_SUPPLY_PROP_CYCLE_COUNT
,
69 POWER_SUPPLY_PROP_VOLTAGE_MAX
,
70 POWER_SUPPLY_PROP_VOLTAGE_MIN
,
71 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
72 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
73 POWER_SUPPLY_PROP_VOLTAGE_AVG
,
74 POWER_SUPPLY_PROP_VOLTAGE_OCV
,
75 POWER_SUPPLY_PROP_CAPACITY
,
76 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
77 POWER_SUPPLY_PROP_CHARGE_FULL
,
78 POWER_SUPPLY_PROP_CHARGE_NOW
,
79 POWER_SUPPLY_PROP_CHARGE_COUNTER
,
80 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT
,
81 POWER_SUPPLY_PROP_TEMP
,
82 POWER_SUPPLY_PROP_TEMP_ALERT_MIN
,
83 POWER_SUPPLY_PROP_TEMP_ALERT_MAX
,
84 POWER_SUPPLY_PROP_TEMP_MIN
,
85 POWER_SUPPLY_PROP_TEMP_MAX
,
86 POWER_SUPPLY_PROP_HEALTH
,
87 POWER_SUPPLY_PROP_SCOPE
,
88 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
,
89 // these two have to be at the end on the list
90 POWER_SUPPLY_PROP_CURRENT_NOW
,
91 POWER_SUPPLY_PROP_CURRENT_AVG
,
94 static int max17042_get_temperature(struct max17042_chip
*chip
, int *temp
)
98 struct regmap
*map
= chip
->regmap
;
100 ret
= regmap_read(map
, MAX17042_TEMP
, &data
);
104 *temp
= sign_extend32(data
, 15);
105 /* The value is converted into deci-centigrade scale */
106 /* Units of LSB = 1 / 256 degree Celsius */
107 *temp
= *temp
* 10 / 256;
111 static int max17042_get_status(struct max17042_chip
*chip
, int *status
)
113 int ret
, charge_full
, charge_now
;
117 ret
= power_supply_am_i_supplied(chip
->battery
);
119 *status
= POWER_SUPPLY_STATUS_UNKNOWN
;
123 *status
= POWER_SUPPLY_STATUS_DISCHARGING
;
128 * The MAX170xx has builtin end-of-charge detection and will update
129 * FullCAP to match RepCap when it detects end of charging.
131 * When this cycle the battery gets charged to a higher (calculated)
132 * capacity then the previous cycle then FullCAP will get updated
133 * continuously once end-of-charge detection kicks in, so allow the
137 ret
= regmap_read(chip
->regmap
, MAX17042_FullCAP
, &charge_full
);
141 ret
= regmap_read(chip
->regmap
, MAX17042_RepCap
, &charge_now
);
145 if ((charge_full
- charge_now
) <= MAX17042_FULL_THRESHOLD
) {
146 *status
= POWER_SUPPLY_STATUS_FULL
;
151 * Even though we are supplied, we may still be discharging if the
152 * supply is e.g. only delivering 5V 0.5A. Check current if available.
154 if (!chip
->pdata
->enable_current_sense
) {
155 *status
= POWER_SUPPLY_STATUS_CHARGING
;
159 ret
= regmap_read(chip
->regmap
, MAX17042_AvgCurrent
, &data
);
163 avg_current
= sign_extend32(data
, 15);
164 avg_current
*= 1562500 / chip
->pdata
->r_sns
;
167 *status
= POWER_SUPPLY_STATUS_CHARGING
;
169 *status
= POWER_SUPPLY_STATUS_DISCHARGING
;
174 static int max17042_get_battery_health(struct max17042_chip
*chip
, int *health
)
176 int temp
, vavg
, vbatt
, ret
;
179 ret
= regmap_read(chip
->regmap
, MAX17042_AvgVCELL
, &val
);
183 /* bits [0-3] unused */
184 vavg
= val
* 625 / 8;
185 /* Convert to millivolts */
188 ret
= regmap_read(chip
->regmap
, MAX17042_VCELL
, &val
);
192 /* bits [0-3] unused */
193 vbatt
= val
* 625 / 8;
194 /* Convert to millivolts */
197 if (vavg
< chip
->pdata
->vmin
) {
198 *health
= POWER_SUPPLY_HEALTH_DEAD
;
202 if (vbatt
> chip
->pdata
->vmax
+ MAX17042_VMAX_TOLERANCE
) {
203 *health
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
207 ret
= max17042_get_temperature(chip
, &temp
);
211 if (temp
< chip
->pdata
->temp_min
) {
212 *health
= POWER_SUPPLY_HEALTH_COLD
;
216 if (temp
> chip
->pdata
->temp_max
) {
217 *health
= POWER_SUPPLY_HEALTH_OVERHEAT
;
221 *health
= POWER_SUPPLY_HEALTH_GOOD
;
230 static int max17042_get_property(struct power_supply
*psy
,
231 enum power_supply_property psp
,
232 union power_supply_propval
*val
)
234 struct max17042_chip
*chip
= power_supply_get_drvdata(psy
);
235 struct regmap
*map
= chip
->regmap
;
240 if (!chip
->init_complete
)
244 case POWER_SUPPLY_PROP_STATUS
:
245 ret
= max17042_get_status(chip
, &val
->intval
);
249 case POWER_SUPPLY_PROP_PRESENT
:
250 ret
= regmap_read(map
, MAX17042_STATUS
, &data
);
254 if (data
& MAX17042_STATUS_BattAbsent
)
259 case POWER_SUPPLY_PROP_TECHNOLOGY
:
260 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LION
;
262 case POWER_SUPPLY_PROP_CYCLE_COUNT
:
263 ret
= regmap_read(map
, MAX17042_Cycles
, &data
);
269 case POWER_SUPPLY_PROP_VOLTAGE_MAX
:
270 ret
= regmap_read(map
, MAX17042_MinMaxVolt
, &data
);
274 val
->intval
= data
>> 8;
275 val
->intval
*= 20000; /* Units of LSB = 20mV */
277 case POWER_SUPPLY_PROP_VOLTAGE_MIN
:
278 ret
= regmap_read(map
, MAX17042_MinMaxVolt
, &data
);
282 val
->intval
= (data
& 0xff) * 20000; /* Units of 20mV */
284 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
285 if (chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17042
)
286 ret
= regmap_read(map
, MAX17042_V_empty
, &data
);
288 ret
= regmap_read(map
, MAX17047_V_empty
, &data
);
292 val
->intval
= data
>> 7;
293 val
->intval
*= 10000; /* Units of LSB = 10mV */
295 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
296 ret
= regmap_read(map
, MAX17042_VCELL
, &data
);
300 val
->intval
= data
* 625 / 8;
302 case POWER_SUPPLY_PROP_VOLTAGE_AVG
:
303 ret
= regmap_read(map
, MAX17042_AvgVCELL
, &data
);
307 val
->intval
= data
* 625 / 8;
309 case POWER_SUPPLY_PROP_VOLTAGE_OCV
:
310 ret
= regmap_read(map
, MAX17042_OCVInternal
, &data
);
314 val
->intval
= data
* 625 / 8;
316 case POWER_SUPPLY_PROP_CAPACITY
:
317 if (chip
->pdata
->enable_current_sense
)
318 ret
= regmap_read(map
, MAX17042_RepSOC
, &data
);
320 ret
= regmap_read(map
, MAX17042_VFSOC
, &data
);
324 val
->intval
= data
>> 8;
326 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
327 ret
= regmap_read(map
, MAX17042_DesignCap
, &data
);
331 data64
= data
* 5000000ll;
332 do_div(data64
, chip
->pdata
->r_sns
);
333 val
->intval
= data64
;
335 case POWER_SUPPLY_PROP_CHARGE_FULL
:
336 ret
= regmap_read(map
, MAX17042_FullCAP
, &data
);
340 data64
= data
* 5000000ll;
341 do_div(data64
, chip
->pdata
->r_sns
);
342 val
->intval
= data64
;
344 case POWER_SUPPLY_PROP_CHARGE_NOW
:
345 ret
= regmap_read(map
, MAX17042_RepCap
, &data
);
349 data64
= data
* 5000000ll;
350 do_div(data64
, chip
->pdata
->r_sns
);
351 val
->intval
= data64
;
353 case POWER_SUPPLY_PROP_CHARGE_COUNTER
:
354 ret
= regmap_read(map
, MAX17042_QH
, &data
);
358 data64
= sign_extend64(data
, 15) * 5000000ll;
359 val
->intval
= div_s64(data64
, chip
->pdata
->r_sns
);
361 case POWER_SUPPLY_PROP_TEMP
:
362 ret
= max17042_get_temperature(chip
, &val
->intval
);
366 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN
:
367 ret
= regmap_read(map
, MAX17042_TALRT_Th
, &data
);
370 /* LSB is Alert Minimum. In deci-centigrade */
371 val
->intval
= sign_extend32(data
& 0xff, 7) * 10;
373 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX
:
374 ret
= regmap_read(map
, MAX17042_TALRT_Th
, &data
);
377 /* MSB is Alert Maximum. In deci-centigrade */
378 val
->intval
= sign_extend32(data
>> 8, 7) * 10;
380 case POWER_SUPPLY_PROP_TEMP_MIN
:
381 val
->intval
= chip
->pdata
->temp_min
;
383 case POWER_SUPPLY_PROP_TEMP_MAX
:
384 val
->intval
= chip
->pdata
->temp_max
;
386 case POWER_SUPPLY_PROP_HEALTH
:
387 ret
= max17042_get_battery_health(chip
, &val
->intval
);
391 case POWER_SUPPLY_PROP_SCOPE
:
392 val
->intval
= POWER_SUPPLY_SCOPE_SYSTEM
;
394 case POWER_SUPPLY_PROP_CURRENT_NOW
:
395 if (chip
->pdata
->enable_current_sense
) {
396 ret
= regmap_read(map
, MAX17042_Current
, &data
);
400 data64
= sign_extend64(data
, 15) * 1562500ll;
401 val
->intval
= div_s64(data64
, chip
->pdata
->r_sns
);
406 case POWER_SUPPLY_PROP_CURRENT_AVG
:
407 if (chip
->pdata
->enable_current_sense
) {
408 ret
= regmap_read(map
, MAX17042_AvgCurrent
, &data
);
412 data64
= sign_extend64(data
, 15) * 1562500ll;
413 val
->intval
= div_s64(data64
, chip
->pdata
->r_sns
);
418 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT
:
419 ret
= regmap_read(map
, MAX17042_ICHGTerm
, &data
);
423 data64
= data
* 1562500ll;
424 val
->intval
= div_s64(data64
, chip
->pdata
->r_sns
);
426 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW
:
427 ret
= regmap_read(map
, MAX17042_TTE
, &data
);
431 val
->intval
= data
* 5625 / 1000;
439 static int max17042_set_property(struct power_supply
*psy
,
440 enum power_supply_property psp
,
441 const union power_supply_propval
*val
)
443 struct max17042_chip
*chip
= power_supply_get_drvdata(psy
);
444 struct regmap
*map
= chip
->regmap
;
450 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN
:
451 ret
= regmap_read(map
, MAX17042_TALRT_Th
, &data
);
455 /* Input in deci-centigrade, convert to centigrade */
456 temp
= val
->intval
/ 10;
457 /* force min < max */
458 if (temp
>= (int8_t)(data
>> 8))
459 temp
= (int8_t)(data
>> 8) - 1;
460 /* Write both MAX and MIN ALERT */
461 data
= (data
& 0xff00) + temp
;
462 ret
= regmap_write(map
, MAX17042_TALRT_Th
, data
);
464 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX
:
465 ret
= regmap_read(map
, MAX17042_TALRT_Th
, &data
);
469 /* Input in Deci-Centigrade, convert to centigrade */
470 temp
= val
->intval
/ 10;
471 /* force max > min */
472 if (temp
<= (int8_t)(data
& 0xff))
473 temp
= (int8_t)(data
& 0xff) + 1;
474 /* Write both MAX and MIN ALERT */
475 data
= (data
& 0xff) + (temp
<< 8);
476 ret
= regmap_write(map
, MAX17042_TALRT_Th
, data
);
485 static int max17042_property_is_writeable(struct power_supply
*psy
,
486 enum power_supply_property psp
)
491 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN
:
492 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX
:
502 static int max17042_write_verify_reg(struct regmap
*map
, u8 reg
, u32 value
)
509 ret
= regmap_write(map
, reg
, value
);
510 regmap_read(map
, reg
, &read_value
);
511 if (read_value
!= value
) {
515 } while (retries
&& read_value
!= value
);
518 pr_err("%s: err %d\n", __func__
, ret
);
523 static inline void max17042_override_por(struct regmap
*map
,
527 regmap_write(map
, reg
, value
);
530 static inline void max17042_unlock_model(struct max17042_chip
*chip
)
532 struct regmap
*map
= chip
->regmap
;
534 regmap_write(map
, MAX17042_MLOCKReg1
, MODEL_UNLOCK1
);
535 regmap_write(map
, MAX17042_MLOCKReg2
, MODEL_UNLOCK2
);
538 static inline void max17042_lock_model(struct max17042_chip
*chip
)
540 struct regmap
*map
= chip
->regmap
;
542 regmap_write(map
, MAX17042_MLOCKReg1
, MODEL_LOCK1
);
543 regmap_write(map
, MAX17042_MLOCKReg2
, MODEL_LOCK2
);
546 static inline void max17042_write_model_data(struct max17042_chip
*chip
,
549 struct regmap
*map
= chip
->regmap
;
552 for (i
= 0; i
< size
; i
++)
553 regmap_write(map
, addr
+ i
,
554 chip
->pdata
->config_data
->cell_char_tbl
[i
]);
557 static inline void max17042_read_model_data(struct max17042_chip
*chip
,
558 u8 addr
, u16
*data
, int size
)
560 struct regmap
*map
= chip
->regmap
;
564 for (i
= 0; i
< size
; i
++) {
565 regmap_read(map
, addr
+ i
, &tmp
);
570 static inline int max17042_model_data_compare(struct max17042_chip
*chip
,
571 u16
*data1
, u16
*data2
, int size
)
575 if (memcmp(data1
, data2
, size
)) {
576 dev_err(&chip
->client
->dev
, "%s compare failed\n", __func__
);
577 for (i
= 0; i
< size
; i
++)
578 dev_info(&chip
->client
->dev
, "0x%x, 0x%x",
580 dev_info(&chip
->client
->dev
, "\n");
586 static int max17042_init_model(struct max17042_chip
*chip
)
589 int table_size
= ARRAY_SIZE(chip
->pdata
->config_data
->cell_char_tbl
);
592 temp_data
= kcalloc(table_size
, sizeof(*temp_data
), GFP_KERNEL
);
596 max17042_unlock_model(chip
);
597 max17042_write_model_data(chip
, MAX17042_MODELChrTbl
,
599 max17042_read_model_data(chip
, MAX17042_MODELChrTbl
, temp_data
,
602 ret
= max17042_model_data_compare(
604 chip
->pdata
->config_data
->cell_char_tbl
,
608 max17042_lock_model(chip
);
614 static int max17042_verify_model_lock(struct max17042_chip
*chip
)
617 int table_size
= ARRAY_SIZE(chip
->pdata
->config_data
->cell_char_tbl
);
621 temp_data
= kcalloc(table_size
, sizeof(*temp_data
), GFP_KERNEL
);
625 max17042_read_model_data(chip
, MAX17042_MODELChrTbl
, temp_data
,
627 for (i
= 0; i
< table_size
; i
++)
635 static void max17042_write_config_regs(struct max17042_chip
*chip
)
637 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
638 struct regmap
*map
= chip
->regmap
;
640 regmap_write(map
, MAX17042_CONFIG
, config
->config
);
641 regmap_write(map
, MAX17042_LearnCFG
, config
->learn_cfg
);
642 regmap_write(map
, MAX17042_FilterCFG
,
644 regmap_write(map
, MAX17042_RelaxCFG
, config
->relax_cfg
);
645 if (chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17047
||
646 chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17050
||
647 chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17055
)
648 regmap_write(map
, MAX17047_FullSOCThr
,
649 config
->full_soc_thresh
);
652 static void max17042_write_custom_regs(struct max17042_chip
*chip
)
654 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
655 struct regmap
*map
= chip
->regmap
;
657 max17042_write_verify_reg(map
, MAX17042_RCOMP0
, config
->rcomp0
);
658 max17042_write_verify_reg(map
, MAX17042_TempCo
, config
->tcompc0
);
659 max17042_write_verify_reg(map
, MAX17042_ICHGTerm
, config
->ichgt_term
);
660 if (chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17042
) {
661 regmap_write(map
, MAX17042_EmptyTempCo
, config
->empty_tempco
);
662 max17042_write_verify_reg(map
, MAX17042_K_empty0
,
665 max17042_write_verify_reg(map
, MAX17047_QRTbl00
,
667 max17042_write_verify_reg(map
, MAX17047_QRTbl10
,
669 max17042_write_verify_reg(map
, MAX17047_QRTbl20
,
671 max17042_write_verify_reg(map
, MAX17047_QRTbl30
,
676 static void max17042_update_capacity_regs(struct max17042_chip
*chip
)
678 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
679 struct regmap
*map
= chip
->regmap
;
681 max17042_write_verify_reg(map
, MAX17042_FullCAP
,
683 regmap_write(map
, MAX17042_DesignCap
, config
->design_cap
);
684 max17042_write_verify_reg(map
, MAX17042_FullCAPNom
,
688 static void max17042_reset_vfsoc0_reg(struct max17042_chip
*chip
)
691 struct regmap
*map
= chip
->regmap
;
693 regmap_read(map
, MAX17042_VFSOC
, &vfSoc
);
694 regmap_write(map
, MAX17042_VFSOC0Enable
, VFSOC0_UNLOCK
);
695 max17042_write_verify_reg(map
, MAX17042_VFSOC0
, vfSoc
);
696 regmap_write(map
, MAX17042_VFSOC0Enable
, VFSOC0_LOCK
);
699 static void max17042_load_new_capacity_params(struct max17042_chip
*chip
)
701 u32 full_cap0
, rep_cap
, dq_acc
, vfSoc
;
704 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
705 struct regmap
*map
= chip
->regmap
;
707 regmap_read(map
, MAX17042_FullCAP0
, &full_cap0
);
708 regmap_read(map
, MAX17042_VFSOC
, &vfSoc
);
710 /* fg_vfSoc needs to shifted by 8 bits to get the
711 * perc in 1% accuracy, to get the right rem_cap multiply
712 * full_cap0, fg_vfSoc and devide by 100
714 rem_cap
= ((vfSoc
>> 8) * full_cap0
) / 100;
715 max17042_write_verify_reg(map
, MAX17042_RemCap
, rem_cap
);
718 max17042_write_verify_reg(map
, MAX17042_RepCap
, rep_cap
);
720 /* Write dQ_acc to 200% of Capacity and dP_acc to 200% */
721 dq_acc
= config
->fullcap
/ dQ_ACC_DIV
;
722 max17042_write_verify_reg(map
, MAX17042_dQacc
, dq_acc
);
723 max17042_write_verify_reg(map
, MAX17042_dPacc
, dP_ACC_200
);
725 max17042_write_verify_reg(map
, MAX17042_FullCAP
,
727 regmap_write(map
, MAX17042_DesignCap
,
729 max17042_write_verify_reg(map
, MAX17042_FullCAPNom
,
731 /* Update SOC register with new SOC */
732 regmap_write(map
, MAX17042_RepSOC
, vfSoc
);
736 * Block write all the override values coming from platform data.
737 * This function MUST be called before the POR initialization procedure
738 * specified by maxim.
740 static inline void max17042_override_por_values(struct max17042_chip
*chip
)
742 struct regmap
*map
= chip
->regmap
;
743 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
745 max17042_override_por(map
, MAX17042_TGAIN
, config
->tgain
);
746 max17042_override_por(map
, MAX17042_TOFF
, config
->toff
);
747 max17042_override_por(map
, MAX17042_CGAIN
, config
->cgain
);
748 max17042_override_por(map
, MAX17042_COFF
, config
->coff
);
750 max17042_override_por(map
, MAX17042_VALRT_Th
, config
->valrt_thresh
);
751 max17042_override_por(map
, MAX17042_TALRT_Th
, config
->talrt_thresh
);
752 max17042_override_por(map
, MAX17042_SALRT_Th
,
753 config
->soc_alrt_thresh
);
754 max17042_override_por(map
, MAX17042_CONFIG
, config
->config
);
755 max17042_override_por(map
, MAX17042_SHDNTIMER
, config
->shdntimer
);
757 max17042_override_por(map
, MAX17042_DesignCap
, config
->design_cap
);
758 max17042_override_por(map
, MAX17042_ICHGTerm
, config
->ichgt_term
);
760 max17042_override_por(map
, MAX17042_AtRate
, config
->at_rate
);
761 max17042_override_por(map
, MAX17042_LearnCFG
, config
->learn_cfg
);
762 max17042_override_por(map
, MAX17042_FilterCFG
, config
->filter_cfg
);
763 max17042_override_por(map
, MAX17042_RelaxCFG
, config
->relax_cfg
);
764 max17042_override_por(map
, MAX17042_MiscCFG
, config
->misc_cfg
);
766 max17042_override_por(map
, MAX17042_FullCAP
, config
->fullcap
);
767 max17042_override_por(map
, MAX17042_FullCAPNom
, config
->fullcapnom
);
768 max17042_override_por(map
, MAX17042_dQacc
, config
->dqacc
);
769 max17042_override_por(map
, MAX17042_dPacc
, config
->dpacc
);
771 max17042_override_por(map
, MAX17042_RCOMP0
, config
->rcomp0
);
772 max17042_override_por(map
, MAX17042_TempCo
, config
->tcompc0
);
774 if (chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17042
) {
775 max17042_override_por(map
, MAX17042_MaskSOC
, config
->masksoc
);
776 max17042_override_por(map
, MAX17042_SOC_empty
, config
->socempty
);
777 max17042_override_por(map
, MAX17042_V_empty
, config
->vempty
);
778 max17042_override_por(map
, MAX17042_EmptyTempCo
, config
->empty_tempco
);
779 max17042_override_por(map
, MAX17042_K_empty0
, config
->kempty0
);
782 if ((chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17042
) ||
783 (chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17047
) ||
784 (chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17050
)) {
785 max17042_override_por(map
, MAX17042_IAvg_empty
, config
->iavg_empty
);
786 max17042_override_por(map
, MAX17042_TempNom
, config
->temp_nom
);
787 max17042_override_por(map
, MAX17042_TempLim
, config
->temp_lim
);
788 max17042_override_por(map
, MAX17042_FCTC
, config
->fctc
);
791 if ((chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17047
) ||
792 (chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17050
) ||
793 (chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17055
)) {
794 max17042_override_por(map
, MAX17047_V_empty
, config
->vempty
);
798 static int max17042_init_chip(struct max17042_chip
*chip
)
800 struct regmap
*map
= chip
->regmap
;
803 max17042_override_por_values(chip
);
804 /* After Power up, the MAX17042 requires 500mS in order
805 * to perform signal debouncing and initial SOC reporting
809 /* Initialize configuration */
810 max17042_write_config_regs(chip
);
812 /* write cell characterization data */
813 ret
= max17042_init_model(chip
);
815 dev_err(&chip
->client
->dev
, "%s init failed\n",
820 ret
= max17042_verify_model_lock(chip
);
822 dev_err(&chip
->client
->dev
, "%s lock verify failed\n",
826 /* write custom parameters */
827 max17042_write_custom_regs(chip
);
829 /* update capacity params */
830 max17042_update_capacity_regs(chip
);
832 /* delay must be atleast 350mS to allow VFSOC
833 * to be calculated from the new configuration
837 /* reset vfsoc0 reg */
838 max17042_reset_vfsoc0_reg(chip
);
840 /* load new capacity params */
841 max17042_load_new_capacity_params(chip
);
843 /* Init complete, Clear the POR bit */
844 regmap_update_bits(map
, MAX17042_STATUS
, STATUS_POR_BIT
, 0x0);
848 static void max17042_set_soc_threshold(struct max17042_chip
*chip
, u16 off
)
850 struct regmap
*map
= chip
->regmap
;
853 /* program interrupt thresholds such that we should
854 * get interrupt for every 'off' perc change in the soc
856 if (chip
->pdata
->enable_current_sense
)
857 regmap_read(map
, MAX17042_RepSOC
, &soc
);
859 regmap_read(map
, MAX17042_VFSOC
, &soc
);
861 soc_tr
= (soc
+ off
) << 8;
864 regmap_write(map
, MAX17042_SALRT_Th
, soc_tr
);
867 static irqreturn_t
max17042_thread_handler(int id
, void *dev
)
869 struct max17042_chip
*chip
= dev
;
873 ret
= regmap_read(chip
->regmap
, MAX17042_STATUS
, &val
);
877 if ((val
& STATUS_SMN_BIT
) || (val
& STATUS_SMX_BIT
)) {
878 dev_dbg(&chip
->client
->dev
, "SOC threshold INTR\n");
879 max17042_set_soc_threshold(chip
, 1);
882 /* we implicitly handle all alerts via power_supply_changed */
883 regmap_clear_bits(chip
->regmap
, MAX17042_STATUS
,
884 0xFFFF & ~(STATUS_POR_BIT
| STATUS_BST_BIT
));
886 power_supply_changed(chip
->battery
);
890 static void max17042_init_worker(struct work_struct
*work
)
892 struct max17042_chip
*chip
= container_of(work
,
893 struct max17042_chip
, work
);
896 /* Initialize registers according to values from the platform data */
897 if (chip
->pdata
->enable_por_init
&& chip
->pdata
->config_data
) {
898 ret
= max17042_init_chip(chip
);
903 chip
->init_complete
= 1;
907 static struct max17042_platform_data
*
908 max17042_get_of_pdata(struct max17042_chip
*chip
)
910 struct device
*dev
= &chip
->client
->dev
;
911 struct device_node
*np
= dev
->of_node
;
913 struct max17042_platform_data
*pdata
;
915 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
920 * Require current sense resistor value to be specified for
921 * current-sense functionality to be enabled at all.
923 if (of_property_read_u32(np
, "maxim,rsns-microohm", &prop
) == 0) {
925 pdata
->enable_current_sense
= true;
928 if (of_property_read_s32(np
, "maxim,cold-temp", &pdata
->temp_min
))
929 pdata
->temp_min
= INT_MIN
;
930 if (of_property_read_s32(np
, "maxim,over-heat-temp", &pdata
->temp_max
))
931 pdata
->temp_max
= INT_MAX
;
932 if (of_property_read_s32(np
, "maxim,dead-volt", &pdata
->vmin
))
933 pdata
->vmin
= INT_MIN
;
934 if (of_property_read_s32(np
, "maxim,over-volt", &pdata
->vmax
))
935 pdata
->vmax
= INT_MAX
;
941 static struct max17042_reg_data max17047_default_pdata_init_regs
[] = {
943 * Some firmwares do not set FullSOCThr, Enable End-of-Charge Detection
944 * when the voltage FG reports 95%, as recommended in the datasheet.
946 { MAX17047_FullSOCThr
, MAX17042_BATTERY_FULL
<< 8 },
949 static struct max17042_platform_data
*
950 max17042_get_default_pdata(struct max17042_chip
*chip
)
952 struct device
*dev
= &chip
->client
->dev
;
953 struct max17042_platform_data
*pdata
;
957 * The MAX17047 gets used on x86 where we might not have pdata, assume
958 * the firmware will already have initialized the fuel-gauge and provide
959 * default values for the non init bits to make things work.
961 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
965 if ((chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17047
) ||
966 (chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17050
)) {
967 pdata
->init_data
= max17047_default_pdata_init_regs
;
968 pdata
->num_init_data
=
969 ARRAY_SIZE(max17047_default_pdata_init_regs
);
972 ret
= regmap_read(chip
->regmap
, MAX17042_MiscCFG
, &misc_cfg
);
976 /* If bits 0-1 are set to 3 then only Voltage readings are used */
977 if ((misc_cfg
& 0x3) == 0x3)
978 pdata
->enable_current_sense
= false;
980 pdata
->enable_current_sense
= true;
982 pdata
->vmin
= MAX17042_DEFAULT_VMIN
;
983 pdata
->vmax
= MAX17042_DEFAULT_VMAX
;
984 pdata
->temp_min
= MAX17042_DEFAULT_TEMP_MIN
;
985 pdata
->temp_max
= MAX17042_DEFAULT_TEMP_MAX
;
990 static struct max17042_platform_data
*
991 max17042_get_pdata(struct max17042_chip
*chip
)
993 struct device
*dev
= &chip
->client
->dev
;
997 return max17042_get_of_pdata(chip
);
999 if (dev
->platform_data
)
1000 return dev
->platform_data
;
1002 return max17042_get_default_pdata(chip
);
1005 static const struct regmap_config max17042_regmap_config
= {
1008 .val_format_endian
= REGMAP_ENDIAN_NATIVE
,
1011 static const struct power_supply_desc max17042_psy_desc
= {
1012 .name
= "max170xx_battery",
1013 .type
= POWER_SUPPLY_TYPE_BATTERY
,
1014 .get_property
= max17042_get_property
,
1015 .set_property
= max17042_set_property
,
1016 .property_is_writeable
= max17042_property_is_writeable
,
1017 .external_power_changed
= power_supply_changed
,
1018 .properties
= max17042_battery_props
,
1019 .num_properties
= ARRAY_SIZE(max17042_battery_props
),
1022 static const struct power_supply_desc max17042_no_current_sense_psy_desc
= {
1023 .name
= "max170xx_battery",
1024 .type
= POWER_SUPPLY_TYPE_BATTERY
,
1025 .get_property
= max17042_get_property
,
1026 .set_property
= max17042_set_property
,
1027 .property_is_writeable
= max17042_property_is_writeable
,
1028 .properties
= max17042_battery_props
,
1029 .num_properties
= ARRAY_SIZE(max17042_battery_props
) - 2,
1032 static int max17042_probe(struct i2c_client
*client
)
1034 const struct i2c_device_id
*id
= i2c_client_get_device_id(client
);
1035 struct i2c_adapter
*adapter
= client
->adapter
;
1036 const struct power_supply_desc
*max17042_desc
= &max17042_psy_desc
;
1037 struct power_supply_config psy_cfg
= {};
1038 const struct acpi_device_id
*acpi_id
= NULL
;
1039 struct device
*dev
= &client
->dev
;
1040 struct max17042_chip
*chip
;
1045 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_WORD_DATA
))
1048 chip
= devm_kzalloc(&client
->dev
, sizeof(*chip
), GFP_KERNEL
);
1052 chip
->client
= client
;
1054 chip
->chip_type
= id
->driver_data
;
1056 acpi_id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
1060 chip
->chip_type
= acpi_id
->driver_data
;
1062 chip
->regmap
= devm_regmap_init_i2c(client
, &max17042_regmap_config
);
1063 if (IS_ERR(chip
->regmap
)) {
1064 dev_err(&client
->dev
, "Failed to initialize regmap\n");
1068 chip
->pdata
= max17042_get_pdata(chip
);
1070 dev_err(&client
->dev
, "no platform data provided\n");
1074 i2c_set_clientdata(client
, chip
);
1075 psy_cfg
.drv_data
= chip
;
1076 psy_cfg
.of_node
= dev
->of_node
;
1078 /* When current is not measured,
1079 * CURRENT_NOW and CURRENT_AVG properties should be invisible. */
1080 if (!chip
->pdata
->enable_current_sense
)
1081 max17042_desc
= &max17042_no_current_sense_psy_desc
;
1083 if (chip
->pdata
->r_sns
== 0)
1084 chip
->pdata
->r_sns
= MAX17042_DEFAULT_SNS_RESISTOR
;
1086 if (chip
->pdata
->init_data
)
1087 for (i
= 0; i
< chip
->pdata
->num_init_data
; i
++)
1088 regmap_write(chip
->regmap
,
1089 chip
->pdata
->init_data
[i
].addr
,
1090 chip
->pdata
->init_data
[i
].data
);
1092 if (!chip
->pdata
->enable_current_sense
) {
1093 regmap_write(chip
->regmap
, MAX17042_CGAIN
, 0x0000);
1094 regmap_write(chip
->regmap
, MAX17042_MiscCFG
, 0x0003);
1095 regmap_write(chip
->regmap
, MAX17042_LearnCFG
, 0x0007);
1098 chip
->battery
= devm_power_supply_register(&client
->dev
, max17042_desc
,
1100 if (IS_ERR(chip
->battery
)) {
1101 dev_err(&client
->dev
, "failed: power supply register\n");
1102 return PTR_ERR(chip
->battery
);
1106 unsigned int flags
= IRQF_ONESHOT
;
1109 * On ACPI systems the IRQ may be handled by ACPI-event code,
1110 * so we need to share (if the ACPI code is willing to share).
1113 flags
|= IRQF_SHARED
| IRQF_PROBE_SHARED
;
1115 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1117 max17042_thread_handler
, flags
,
1118 chip
->battery
->desc
->name
,
1121 regmap_update_bits(chip
->regmap
, MAX17042_CONFIG
,
1124 max17042_set_soc_threshold(chip
, 1);
1128 dev_err(&client
->dev
, "Failed to get IRQ\n");
1131 /* Not able to update the charge threshold when exceeded? -> disable */
1133 regmap_write(chip
->regmap
, MAX17042_SALRT_Th
, 0xff00);
1135 regmap_read(chip
->regmap
, MAX17042_STATUS
, &val
);
1136 if (val
& STATUS_POR_BIT
) {
1137 ret
= devm_work_autocancel(&client
->dev
, &chip
->work
,
1138 max17042_init_worker
);
1141 schedule_work(&chip
->work
);
1143 chip
->init_complete
= 1;
1149 #ifdef CONFIG_PM_SLEEP
1150 static int max17042_suspend(struct device
*dev
)
1152 struct max17042_chip
*chip
= dev_get_drvdata(dev
);
1155 * disable the irq and enable irq_wake
1156 * capability to the interrupt line.
1158 if (chip
->client
->irq
) {
1159 disable_irq(chip
->client
->irq
);
1160 enable_irq_wake(chip
->client
->irq
);
1166 static int max17042_resume(struct device
*dev
)
1168 struct max17042_chip
*chip
= dev_get_drvdata(dev
);
1170 if (chip
->client
->irq
) {
1171 disable_irq_wake(chip
->client
->irq
);
1172 enable_irq(chip
->client
->irq
);
1173 /* re-program the SOC thresholds to 1% change */
1174 max17042_set_soc_threshold(chip
, 1);
1181 static SIMPLE_DEV_PM_OPS(max17042_pm_ops
, max17042_suspend
,
1185 static const struct acpi_device_id max17042_acpi_match
[] = {
1186 { "MAX17047", MAXIM_DEVICE_TYPE_MAX17047
},
1189 MODULE_DEVICE_TABLE(acpi
, max17042_acpi_match
);
1193 static const struct of_device_id max17042_dt_match
[] = {
1194 { .compatible
= "maxim,max17042" },
1195 { .compatible
= "maxim,max17047" },
1196 { .compatible
= "maxim,max17050" },
1197 { .compatible
= "maxim,max17055" },
1198 { .compatible
= "maxim,max77849-battery" },
1201 MODULE_DEVICE_TABLE(of
, max17042_dt_match
);
1204 static const struct i2c_device_id max17042_id
[] = {
1205 { "max17042", MAXIM_DEVICE_TYPE_MAX17042
},
1206 { "max17047", MAXIM_DEVICE_TYPE_MAX17047
},
1207 { "max17050", MAXIM_DEVICE_TYPE_MAX17050
},
1208 { "max17055", MAXIM_DEVICE_TYPE_MAX17055
},
1209 { "max77849-battery", MAXIM_DEVICE_TYPE_MAX17047
},
1212 MODULE_DEVICE_TABLE(i2c
, max17042_id
);
1214 static struct i2c_driver max17042_i2c_driver
= {
1217 .acpi_match_table
= ACPI_PTR(max17042_acpi_match
),
1218 .of_match_table
= of_match_ptr(max17042_dt_match
),
1219 .pm
= &max17042_pm_ops
,
1221 .probe
= max17042_probe
,
1222 .id_table
= max17042_id
,
1224 module_i2c_driver(max17042_i2c_driver
);
1226 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1227 MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
1228 MODULE_LICENSE("GPL");