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/init.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/i2c.h>
16 #include <linux/delay.h>
17 #include <linux/interrupt.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/power_supply.h>
21 #include <linux/power/max17042_battery.h>
23 #include <linux/regmap.h>
25 /* Status register bits */
26 #define STATUS_POR_BIT (1 << 1)
27 #define STATUS_BST_BIT (1 << 3)
28 #define STATUS_VMN_BIT (1 << 8)
29 #define STATUS_TMN_BIT (1 << 9)
30 #define STATUS_SMN_BIT (1 << 10)
31 #define STATUS_BI_BIT (1 << 11)
32 #define STATUS_VMX_BIT (1 << 12)
33 #define STATUS_TMX_BIT (1 << 13)
34 #define STATUS_SMX_BIT (1 << 14)
35 #define STATUS_BR_BIT (1 << 15)
37 /* Interrupt mask bits */
38 #define CONFIG_ALRT_BIT_ENBL (1 << 2)
39 #define STATUS_INTR_SOCMIN_BIT (1 << 10)
40 #define STATUS_INTR_SOCMAX_BIT (1 << 14)
42 #define VFSOC0_LOCK 0x0000
43 #define VFSOC0_UNLOCK 0x0080
44 #define MODEL_UNLOCK1 0X0059
45 #define MODEL_UNLOCK2 0X00C4
46 #define MODEL_LOCK1 0X0000
47 #define MODEL_LOCK2 0X0000
49 #define dQ_ACC_DIV 0x4
50 #define dP_ACC_100 0x1900
51 #define dP_ACC_200 0x3200
53 #define MAX17042_VMAX_TOLERANCE 50 /* 50 mV */
55 struct max17042_chip
{
56 struct i2c_client
*client
;
57 struct regmap
*regmap
;
58 struct power_supply
*battery
;
59 enum max170xx_chip_type chip_type
;
60 struct max17042_platform_data
*pdata
;
61 struct work_struct work
;
65 static enum power_supply_property max17042_battery_props
[] = {
66 POWER_SUPPLY_PROP_STATUS
,
67 POWER_SUPPLY_PROP_PRESENT
,
68 POWER_SUPPLY_PROP_TECHNOLOGY
,
69 POWER_SUPPLY_PROP_CYCLE_COUNT
,
70 POWER_SUPPLY_PROP_VOLTAGE_MAX
,
71 POWER_SUPPLY_PROP_VOLTAGE_MIN
,
72 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
73 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
74 POWER_SUPPLY_PROP_VOLTAGE_AVG
,
75 POWER_SUPPLY_PROP_VOLTAGE_OCV
,
76 POWER_SUPPLY_PROP_CAPACITY
,
77 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
78 POWER_SUPPLY_PROP_CHARGE_FULL
,
79 POWER_SUPPLY_PROP_CHARGE_NOW
,
80 POWER_SUPPLY_PROP_CHARGE_COUNTER
,
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_CURRENT_NOW
,
89 POWER_SUPPLY_PROP_CURRENT_AVG
,
92 static int max17042_get_temperature(struct max17042_chip
*chip
, int *temp
)
96 struct regmap
*map
= chip
->regmap
;
98 ret
= regmap_read(map
, MAX17042_TEMP
, &data
);
102 *temp
= sign_extend32(data
, 15);
103 /* The value is converted into deci-centigrade scale */
104 /* Units of LSB = 1 / 256 degree Celsius */
105 *temp
= *temp
* 10 / 256;
109 static int max17042_get_status(struct max17042_chip
*chip
, int *status
)
111 int ret
, charge_full
, charge_now
;
115 ret
= power_supply_am_i_supplied(chip
->battery
);
117 *status
= POWER_SUPPLY_STATUS_UNKNOWN
;
121 *status
= POWER_SUPPLY_STATUS_DISCHARGING
;
126 * The MAX170xx has builtin end-of-charge detection and will update
127 * FullCAP to match RepCap when it detects end of charging.
129 * When this cycle the battery gets charged to a higher (calculated)
130 * capacity then the previous cycle then FullCAP will get updated
131 * contineously once end-of-charge detection kicks in, so allow the
135 ret
= regmap_read(chip
->regmap
, MAX17042_FullCAP
, &charge_full
);
139 ret
= regmap_read(chip
->regmap
, MAX17042_RepCap
, &charge_now
);
143 if ((charge_full
- charge_now
) <= MAX17042_FULL_THRESHOLD
) {
144 *status
= POWER_SUPPLY_STATUS_FULL
;
149 * Even though we are supplied, we may still be discharging if the
150 * supply is e.g. only delivering 5V 0.5A. Check current if available.
152 if (!chip
->pdata
->enable_current_sense
) {
153 *status
= POWER_SUPPLY_STATUS_CHARGING
;
157 ret
= regmap_read(chip
->regmap
, MAX17042_AvgCurrent
, &data
);
161 avg_current
= sign_extend32(data
, 15);
162 avg_current
*= 1562500 / chip
->pdata
->r_sns
;
165 *status
= POWER_SUPPLY_STATUS_CHARGING
;
167 *status
= POWER_SUPPLY_STATUS_DISCHARGING
;
172 static int max17042_get_battery_health(struct max17042_chip
*chip
, int *health
)
174 int temp
, vavg
, vbatt
, ret
;
177 ret
= regmap_read(chip
->regmap
, MAX17042_AvgVCELL
, &val
);
181 /* bits [0-3] unused */
182 vavg
= val
* 625 / 8;
183 /* Convert to millivolts */
186 ret
= regmap_read(chip
->regmap
, MAX17042_VCELL
, &val
);
190 /* bits [0-3] unused */
191 vbatt
= val
* 625 / 8;
192 /* Convert to millivolts */
195 if (vavg
< chip
->pdata
->vmin
) {
196 *health
= POWER_SUPPLY_HEALTH_DEAD
;
200 if (vbatt
> chip
->pdata
->vmax
+ MAX17042_VMAX_TOLERANCE
) {
201 *health
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
205 ret
= max17042_get_temperature(chip
, &temp
);
209 if (temp
< chip
->pdata
->temp_min
) {
210 *health
= POWER_SUPPLY_HEALTH_COLD
;
214 if (temp
> chip
->pdata
->temp_max
) {
215 *health
= POWER_SUPPLY_HEALTH_OVERHEAT
;
219 *health
= POWER_SUPPLY_HEALTH_GOOD
;
228 static int max17042_get_property(struct power_supply
*psy
,
229 enum power_supply_property psp
,
230 union power_supply_propval
*val
)
232 struct max17042_chip
*chip
= power_supply_get_drvdata(psy
);
233 struct regmap
*map
= chip
->regmap
;
238 if (!chip
->init_complete
)
242 case POWER_SUPPLY_PROP_STATUS
:
243 ret
= max17042_get_status(chip
, &val
->intval
);
247 case POWER_SUPPLY_PROP_PRESENT
:
248 ret
= regmap_read(map
, MAX17042_STATUS
, &data
);
252 if (data
& MAX17042_STATUS_BattAbsent
)
257 case POWER_SUPPLY_PROP_TECHNOLOGY
:
258 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LION
;
260 case POWER_SUPPLY_PROP_CYCLE_COUNT
:
261 ret
= regmap_read(map
, MAX17042_Cycles
, &data
);
267 case POWER_SUPPLY_PROP_VOLTAGE_MAX
:
268 ret
= regmap_read(map
, MAX17042_MinMaxVolt
, &data
);
272 val
->intval
= data
>> 8;
273 val
->intval
*= 20000; /* Units of LSB = 20mV */
275 case POWER_SUPPLY_PROP_VOLTAGE_MIN
:
276 ret
= regmap_read(map
, MAX17042_MinMaxVolt
, &data
);
280 val
->intval
= (data
& 0xff) * 20000; /* Units of 20mV */
282 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
283 if (chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17042
)
284 ret
= regmap_read(map
, MAX17042_V_empty
, &data
);
286 ret
= regmap_read(map
, MAX17047_V_empty
, &data
);
290 val
->intval
= data
>> 7;
291 val
->intval
*= 10000; /* Units of LSB = 10mV */
293 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
294 ret
= regmap_read(map
, MAX17042_VCELL
, &data
);
298 val
->intval
= data
* 625 / 8;
300 case POWER_SUPPLY_PROP_VOLTAGE_AVG
:
301 ret
= regmap_read(map
, MAX17042_AvgVCELL
, &data
);
305 val
->intval
= data
* 625 / 8;
307 case POWER_SUPPLY_PROP_VOLTAGE_OCV
:
308 ret
= regmap_read(map
, MAX17042_OCVInternal
, &data
);
312 val
->intval
= data
* 625 / 8;
314 case POWER_SUPPLY_PROP_CAPACITY
:
315 ret
= regmap_read(map
, MAX17042_RepSOC
, &data
);
319 val
->intval
= data
>> 8;
321 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
322 ret
= regmap_read(map
, MAX17042_DesignCap
, &data
);
326 data64
= data
* 5000000ll;
327 do_div(data64
, chip
->pdata
->r_sns
);
328 val
->intval
= data64
;
330 case POWER_SUPPLY_PROP_CHARGE_FULL
:
331 ret
= regmap_read(map
, MAX17042_FullCAP
, &data
);
335 data64
= data
* 5000000ll;
336 do_div(data64
, chip
->pdata
->r_sns
);
337 val
->intval
= data64
;
339 case POWER_SUPPLY_PROP_CHARGE_NOW
:
340 ret
= regmap_read(map
, MAX17042_RepCap
, &data
);
344 data64
= data
* 5000000ll;
345 do_div(data64
, chip
->pdata
->r_sns
);
346 val
->intval
= data64
;
348 case POWER_SUPPLY_PROP_CHARGE_COUNTER
:
349 ret
= regmap_read(map
, MAX17042_QH
, &data
);
353 val
->intval
= data
* 1000 / 2;
355 case POWER_SUPPLY_PROP_TEMP
:
356 ret
= max17042_get_temperature(chip
, &val
->intval
);
360 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN
:
361 ret
= regmap_read(map
, MAX17042_TALRT_Th
, &data
);
364 /* LSB is Alert Minimum. In deci-centigrade */
365 val
->intval
= sign_extend32(data
& 0xff, 7) * 10;
367 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX
:
368 ret
= regmap_read(map
, MAX17042_TALRT_Th
, &data
);
371 /* MSB is Alert Maximum. In deci-centigrade */
372 val
->intval
= sign_extend32(data
>> 8, 7) * 10;
374 case POWER_SUPPLY_PROP_TEMP_MIN
:
375 val
->intval
= chip
->pdata
->temp_min
;
377 case POWER_SUPPLY_PROP_TEMP_MAX
:
378 val
->intval
= chip
->pdata
->temp_max
;
380 case POWER_SUPPLY_PROP_HEALTH
:
381 ret
= max17042_get_battery_health(chip
, &val
->intval
);
385 case POWER_SUPPLY_PROP_SCOPE
:
386 val
->intval
= POWER_SUPPLY_SCOPE_SYSTEM
;
388 case POWER_SUPPLY_PROP_CURRENT_NOW
:
389 if (chip
->pdata
->enable_current_sense
) {
390 ret
= regmap_read(map
, MAX17042_Current
, &data
);
394 val
->intval
= sign_extend32(data
, 15);
395 val
->intval
*= 1562500 / chip
->pdata
->r_sns
;
400 case POWER_SUPPLY_PROP_CURRENT_AVG
:
401 if (chip
->pdata
->enable_current_sense
) {
402 ret
= regmap_read(map
, MAX17042_AvgCurrent
, &data
);
406 val
->intval
= sign_extend32(data
, 15);
407 val
->intval
*= 1562500 / chip
->pdata
->r_sns
;
418 static int max17042_set_property(struct power_supply
*psy
,
419 enum power_supply_property psp
,
420 const union power_supply_propval
*val
)
422 struct max17042_chip
*chip
= power_supply_get_drvdata(psy
);
423 struct regmap
*map
= chip
->regmap
;
429 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN
:
430 ret
= regmap_read(map
, MAX17042_TALRT_Th
, &data
);
434 /* Input in deci-centigrade, convert to centigrade */
435 temp
= val
->intval
/ 10;
436 /* force min < max */
437 if (temp
>= (int8_t)(data
>> 8))
438 temp
= (int8_t)(data
>> 8) - 1;
439 /* Write both MAX and MIN ALERT */
440 data
= (data
& 0xff00) + temp
;
441 ret
= regmap_write(map
, MAX17042_TALRT_Th
, data
);
443 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX
:
444 ret
= regmap_read(map
, MAX17042_TALRT_Th
, &data
);
448 /* Input in Deci-Centigrade, convert to centigrade */
449 temp
= val
->intval
/ 10;
450 /* force max > min */
451 if (temp
<= (int8_t)(data
& 0xff))
452 temp
= (int8_t)(data
& 0xff) + 1;
453 /* Write both MAX and MIN ALERT */
454 data
= (data
& 0xff) + (temp
<< 8);
455 ret
= regmap_write(map
, MAX17042_TALRT_Th
, data
);
464 static int max17042_property_is_writeable(struct power_supply
*psy
,
465 enum power_supply_property psp
)
470 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN
:
471 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX
:
481 static void max17042_external_power_changed(struct power_supply
*psy
)
483 power_supply_changed(psy
);
486 static int max17042_write_verify_reg(struct regmap
*map
, u8 reg
, u32 value
)
493 ret
= regmap_write(map
, reg
, value
);
494 regmap_read(map
, reg
, &read_value
);
495 if (read_value
!= value
) {
499 } while (retries
&& read_value
!= value
);
502 pr_err("%s: err %d\n", __func__
, ret
);
507 static inline void max17042_override_por(struct regmap
*map
,
511 regmap_write(map
, reg
, value
);
514 static inline void max17042_unlock_model(struct max17042_chip
*chip
)
516 struct regmap
*map
= chip
->regmap
;
518 regmap_write(map
, MAX17042_MLOCKReg1
, MODEL_UNLOCK1
);
519 regmap_write(map
, MAX17042_MLOCKReg2
, MODEL_UNLOCK2
);
522 static inline void max17042_lock_model(struct max17042_chip
*chip
)
524 struct regmap
*map
= chip
->regmap
;
526 regmap_write(map
, MAX17042_MLOCKReg1
, MODEL_LOCK1
);
527 regmap_write(map
, MAX17042_MLOCKReg2
, MODEL_LOCK2
);
530 static inline void max17042_write_model_data(struct max17042_chip
*chip
,
533 struct regmap
*map
= chip
->regmap
;
536 for (i
= 0; i
< size
; i
++)
537 regmap_write(map
, addr
+ i
,
538 chip
->pdata
->config_data
->cell_char_tbl
[i
]);
541 static inline void max17042_read_model_data(struct max17042_chip
*chip
,
542 u8 addr
, u16
*data
, int size
)
544 struct regmap
*map
= chip
->regmap
;
548 for (i
= 0; i
< size
; i
++) {
549 regmap_read(map
, addr
+ i
, &tmp
);
554 static inline int max17042_model_data_compare(struct max17042_chip
*chip
,
555 u16
*data1
, u16
*data2
, int size
)
559 if (memcmp(data1
, data2
, size
)) {
560 dev_err(&chip
->client
->dev
, "%s compare failed\n", __func__
);
561 for (i
= 0; i
< size
; i
++)
562 dev_info(&chip
->client
->dev
, "0x%x, 0x%x",
564 dev_info(&chip
->client
->dev
, "\n");
570 static int max17042_init_model(struct max17042_chip
*chip
)
573 int table_size
= ARRAY_SIZE(chip
->pdata
->config_data
->cell_char_tbl
);
576 temp_data
= kcalloc(table_size
, sizeof(*temp_data
), GFP_KERNEL
);
580 max17042_unlock_model(chip
);
581 max17042_write_model_data(chip
, MAX17042_MODELChrTbl
,
583 max17042_read_model_data(chip
, MAX17042_MODELChrTbl
, temp_data
,
586 ret
= max17042_model_data_compare(
588 chip
->pdata
->config_data
->cell_char_tbl
,
592 max17042_lock_model(chip
);
598 static int max17042_verify_model_lock(struct max17042_chip
*chip
)
601 int table_size
= ARRAY_SIZE(chip
->pdata
->config_data
->cell_char_tbl
);
605 temp_data
= kcalloc(table_size
, sizeof(*temp_data
), GFP_KERNEL
);
609 max17042_read_model_data(chip
, MAX17042_MODELChrTbl
, temp_data
,
611 for (i
= 0; i
< table_size
; i
++)
619 static void max17042_write_config_regs(struct max17042_chip
*chip
)
621 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
622 struct regmap
*map
= chip
->regmap
;
624 regmap_write(map
, MAX17042_CONFIG
, config
->config
);
625 regmap_write(map
, MAX17042_LearnCFG
, config
->learn_cfg
);
626 regmap_write(map
, MAX17042_FilterCFG
,
628 regmap_write(map
, MAX17042_RelaxCFG
, config
->relax_cfg
);
629 if (chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17047
||
630 chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17050
)
631 regmap_write(map
, MAX17047_FullSOCThr
,
632 config
->full_soc_thresh
);
635 static void max17042_write_custom_regs(struct max17042_chip
*chip
)
637 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
638 struct regmap
*map
= chip
->regmap
;
640 max17042_write_verify_reg(map
, MAX17042_RCOMP0
, config
->rcomp0
);
641 max17042_write_verify_reg(map
, MAX17042_TempCo
, config
->tcompc0
);
642 max17042_write_verify_reg(map
, MAX17042_ICHGTerm
, config
->ichgt_term
);
643 if (chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17042
) {
644 regmap_write(map
, MAX17042_EmptyTempCo
, config
->empty_tempco
);
645 max17042_write_verify_reg(map
, MAX17042_K_empty0
,
648 max17042_write_verify_reg(map
, MAX17047_QRTbl00
,
650 max17042_write_verify_reg(map
, MAX17047_QRTbl10
,
652 max17042_write_verify_reg(map
, MAX17047_QRTbl20
,
654 max17042_write_verify_reg(map
, MAX17047_QRTbl30
,
659 static void max17042_update_capacity_regs(struct max17042_chip
*chip
)
661 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
662 struct regmap
*map
= chip
->regmap
;
664 max17042_write_verify_reg(map
, MAX17042_FullCAP
,
666 regmap_write(map
, MAX17042_DesignCap
, config
->design_cap
);
667 max17042_write_verify_reg(map
, MAX17042_FullCAPNom
,
671 static void max17042_reset_vfsoc0_reg(struct max17042_chip
*chip
)
674 struct regmap
*map
= chip
->regmap
;
676 regmap_read(map
, MAX17042_VFSOC
, &vfSoc
);
677 regmap_write(map
, MAX17042_VFSOC0Enable
, VFSOC0_UNLOCK
);
678 max17042_write_verify_reg(map
, MAX17042_VFSOC0
, vfSoc
);
679 regmap_write(map
, MAX17042_VFSOC0Enable
, VFSOC0_LOCK
);
682 static void max17042_load_new_capacity_params(struct max17042_chip
*chip
)
684 u32 full_cap0
, rep_cap
, dq_acc
, vfSoc
;
687 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
688 struct regmap
*map
= chip
->regmap
;
690 regmap_read(map
, MAX17042_FullCAP0
, &full_cap0
);
691 regmap_read(map
, MAX17042_VFSOC
, &vfSoc
);
693 /* fg_vfSoc needs to shifted by 8 bits to get the
694 * perc in 1% accuracy, to get the right rem_cap multiply
695 * full_cap0, fg_vfSoc and devide by 100
697 rem_cap
= ((vfSoc
>> 8) * full_cap0
) / 100;
698 max17042_write_verify_reg(map
, MAX17042_RemCap
, rem_cap
);
701 max17042_write_verify_reg(map
, MAX17042_RepCap
, rep_cap
);
703 /* Write dQ_acc to 200% of Capacity and dP_acc to 200% */
704 dq_acc
= config
->fullcap
/ dQ_ACC_DIV
;
705 max17042_write_verify_reg(map
, MAX17042_dQacc
, dq_acc
);
706 max17042_write_verify_reg(map
, MAX17042_dPacc
, dP_ACC_200
);
708 max17042_write_verify_reg(map
, MAX17042_FullCAP
,
710 regmap_write(map
, MAX17042_DesignCap
,
712 max17042_write_verify_reg(map
, MAX17042_FullCAPNom
,
714 /* Update SOC register with new SOC */
715 regmap_write(map
, MAX17042_RepSOC
, vfSoc
);
719 * Block write all the override values coming from platform data.
720 * This function MUST be called before the POR initialization proceedure
721 * specified by maxim.
723 static inline void max17042_override_por_values(struct max17042_chip
*chip
)
725 struct regmap
*map
= chip
->regmap
;
726 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
728 max17042_override_por(map
, MAX17042_TGAIN
, config
->tgain
);
729 max17042_override_por(map
, MAx17042_TOFF
, config
->toff
);
730 max17042_override_por(map
, MAX17042_CGAIN
, config
->cgain
);
731 max17042_override_por(map
, MAX17042_COFF
, config
->coff
);
733 max17042_override_por(map
, MAX17042_VALRT_Th
, config
->valrt_thresh
);
734 max17042_override_por(map
, MAX17042_TALRT_Th
, config
->talrt_thresh
);
735 max17042_override_por(map
, MAX17042_SALRT_Th
,
736 config
->soc_alrt_thresh
);
737 max17042_override_por(map
, MAX17042_CONFIG
, config
->config
);
738 max17042_override_por(map
, MAX17042_SHDNTIMER
, config
->shdntimer
);
740 max17042_override_por(map
, MAX17042_DesignCap
, config
->design_cap
);
741 max17042_override_por(map
, MAX17042_ICHGTerm
, config
->ichgt_term
);
743 max17042_override_por(map
, MAX17042_AtRate
, config
->at_rate
);
744 max17042_override_por(map
, MAX17042_LearnCFG
, config
->learn_cfg
);
745 max17042_override_por(map
, MAX17042_FilterCFG
, config
->filter_cfg
);
746 max17042_override_por(map
, MAX17042_RelaxCFG
, config
->relax_cfg
);
747 max17042_override_por(map
, MAX17042_MiscCFG
, config
->misc_cfg
);
748 max17042_override_por(map
, MAX17042_MaskSOC
, config
->masksoc
);
750 max17042_override_por(map
, MAX17042_FullCAP
, config
->fullcap
);
751 max17042_override_por(map
, MAX17042_FullCAPNom
, config
->fullcapnom
);
752 if (chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17042
)
753 max17042_override_por(map
, MAX17042_SOC_empty
,
755 max17042_override_por(map
, MAX17042_LAvg_empty
, config
->lavg_empty
);
756 max17042_override_por(map
, MAX17042_dQacc
, config
->dqacc
);
757 max17042_override_por(map
, MAX17042_dPacc
, config
->dpacc
);
759 if (chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17042
)
760 max17042_override_por(map
, MAX17042_V_empty
, config
->vempty
);
762 max17042_override_por(map
, MAX17047_V_empty
, config
->vempty
);
763 max17042_override_por(map
, MAX17042_TempNom
, config
->temp_nom
);
764 max17042_override_por(map
, MAX17042_TempLim
, config
->temp_lim
);
765 max17042_override_por(map
, MAX17042_FCTC
, config
->fctc
);
766 max17042_override_por(map
, MAX17042_RCOMP0
, config
->rcomp0
);
767 max17042_override_por(map
, MAX17042_TempCo
, config
->tcompc0
);
768 if (chip
->chip_type
) {
769 max17042_override_por(map
, MAX17042_EmptyTempCo
,
770 config
->empty_tempco
);
771 max17042_override_por(map
, MAX17042_K_empty0
,
776 static int max17042_init_chip(struct max17042_chip
*chip
)
778 struct regmap
*map
= chip
->regmap
;
781 max17042_override_por_values(chip
);
782 /* After Power up, the MAX17042 requires 500mS in order
783 * to perform signal debouncing and initial SOC reporting
787 /* Initialize configaration */
788 max17042_write_config_regs(chip
);
790 /* write cell characterization data */
791 ret
= max17042_init_model(chip
);
793 dev_err(&chip
->client
->dev
, "%s init failed\n",
798 ret
= max17042_verify_model_lock(chip
);
800 dev_err(&chip
->client
->dev
, "%s lock verify failed\n",
804 /* write custom parameters */
805 max17042_write_custom_regs(chip
);
807 /* update capacity params */
808 max17042_update_capacity_regs(chip
);
810 /* delay must be atleast 350mS to allow VFSOC
811 * to be calculated from the new configuration
815 /* reset vfsoc0 reg */
816 max17042_reset_vfsoc0_reg(chip
);
818 /* load new capacity params */
819 max17042_load_new_capacity_params(chip
);
821 /* Init complete, Clear the POR bit */
822 regmap_update_bits(map
, MAX17042_STATUS
, STATUS_POR_BIT
, 0x0);
826 static void max17042_set_soc_threshold(struct max17042_chip
*chip
, u16 off
)
828 struct regmap
*map
= chip
->regmap
;
831 /* program interrupt thesholds such that we should
832 * get interrupt for every 'off' perc change in the soc
834 regmap_read(map
, MAX17042_RepSOC
, &soc
);
836 soc_tr
= (soc
+ off
) << 8;
837 soc_tr
|= (soc
- off
);
838 regmap_write(map
, MAX17042_SALRT_Th
, soc_tr
);
841 static irqreturn_t
max17042_thread_handler(int id
, void *dev
)
843 struct max17042_chip
*chip
= dev
;
846 regmap_read(chip
->regmap
, MAX17042_STATUS
, &val
);
847 if ((val
& STATUS_INTR_SOCMIN_BIT
) ||
848 (val
& STATUS_INTR_SOCMAX_BIT
)) {
849 dev_info(&chip
->client
->dev
, "SOC threshold INTR\n");
850 max17042_set_soc_threshold(chip
, 1);
853 power_supply_changed(chip
->battery
);
857 static void max17042_init_worker(struct work_struct
*work
)
859 struct max17042_chip
*chip
= container_of(work
,
860 struct max17042_chip
, work
);
863 /* Initialize registers according to values from the platform data */
864 if (chip
->pdata
->enable_por_init
&& chip
->pdata
->config_data
) {
865 ret
= max17042_init_chip(chip
);
870 chip
->init_complete
= 1;
874 static struct max17042_platform_data
*
875 max17042_get_of_pdata(struct max17042_chip
*chip
)
877 struct device
*dev
= &chip
->client
->dev
;
878 struct device_node
*np
= dev
->of_node
;
880 struct max17042_platform_data
*pdata
;
882 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
887 * Require current sense resistor value to be specified for
888 * current-sense functionality to be enabled at all.
890 if (of_property_read_u32(np
, "maxim,rsns-microohm", &prop
) == 0) {
892 pdata
->enable_current_sense
= true;
895 if (of_property_read_s32(np
, "maxim,cold-temp", &pdata
->temp_min
))
896 pdata
->temp_min
= INT_MIN
;
897 if (of_property_read_s32(np
, "maxim,over-heat-temp", &pdata
->temp_max
))
898 pdata
->temp_max
= INT_MAX
;
899 if (of_property_read_s32(np
, "maxim,dead-volt", &pdata
->vmin
))
900 pdata
->vmin
= INT_MIN
;
901 if (of_property_read_s32(np
, "maxim,over-volt", &pdata
->vmax
))
902 pdata
->vmax
= INT_MAX
;
908 static struct max17042_reg_data max17047_default_pdata_init_regs
[] = {
910 * Some firmwares do not set FullSOCThr, Enable End-of-Charge Detection
911 * when the voltage FG reports 95%, as recommended in the datasheet.
913 { MAX17047_FullSOCThr
, MAX17042_BATTERY_FULL
<< 8 },
916 static struct max17042_platform_data
*
917 max17042_get_default_pdata(struct max17042_chip
*chip
)
919 struct device
*dev
= &chip
->client
->dev
;
920 struct max17042_platform_data
*pdata
;
924 * The MAX17047 gets used on x86 where we might not have pdata, assume
925 * the firmware will already have initialized the fuel-gauge and provide
926 * default values for the non init bits to make things work.
928 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
932 if (chip
->chip_type
!= MAXIM_DEVICE_TYPE_MAX17042
) {
933 pdata
->init_data
= max17047_default_pdata_init_regs
;
934 pdata
->num_init_data
=
935 ARRAY_SIZE(max17047_default_pdata_init_regs
);
938 ret
= regmap_read(chip
->regmap
, MAX17042_MiscCFG
, &misc_cfg
);
942 /* If bits 0-1 are set to 3 then only Voltage readings are used */
943 if ((misc_cfg
& 0x3) == 0x3)
944 pdata
->enable_current_sense
= false;
946 pdata
->enable_current_sense
= true;
948 pdata
->vmin
= MAX17042_DEFAULT_VMIN
;
949 pdata
->vmax
= MAX17042_DEFAULT_VMAX
;
950 pdata
->temp_min
= MAX17042_DEFAULT_TEMP_MIN
;
951 pdata
->temp_max
= MAX17042_DEFAULT_TEMP_MAX
;
956 static struct max17042_platform_data
*
957 max17042_get_pdata(struct max17042_chip
*chip
)
959 struct device
*dev
= &chip
->client
->dev
;
963 return max17042_get_of_pdata(chip
);
965 if (dev
->platform_data
)
966 return dev
->platform_data
;
968 return max17042_get_default_pdata(chip
);
971 static const struct regmap_config max17042_regmap_config
= {
974 .val_format_endian
= REGMAP_ENDIAN_NATIVE
,
977 static const struct power_supply_desc max17042_psy_desc
= {
978 .name
= "max170xx_battery",
979 .type
= POWER_SUPPLY_TYPE_BATTERY
,
980 .get_property
= max17042_get_property
,
981 .set_property
= max17042_set_property
,
982 .property_is_writeable
= max17042_property_is_writeable
,
983 .external_power_changed
= max17042_external_power_changed
,
984 .properties
= max17042_battery_props
,
985 .num_properties
= ARRAY_SIZE(max17042_battery_props
),
988 static const struct power_supply_desc max17042_no_current_sense_psy_desc
= {
989 .name
= "max170xx_battery",
990 .type
= POWER_SUPPLY_TYPE_BATTERY
,
991 .get_property
= max17042_get_property
,
992 .set_property
= max17042_set_property
,
993 .property_is_writeable
= max17042_property_is_writeable
,
994 .properties
= max17042_battery_props
,
995 .num_properties
= ARRAY_SIZE(max17042_battery_props
) - 2,
998 static void max17042_stop_work(void *data
)
1000 struct max17042_chip
*chip
= data
;
1002 cancel_work_sync(&chip
->work
);
1005 static int max17042_probe(struct i2c_client
*client
,
1006 const struct i2c_device_id
*id
)
1008 struct i2c_adapter
*adapter
= client
->adapter
;
1009 const struct power_supply_desc
*max17042_desc
= &max17042_psy_desc
;
1010 struct power_supply_config psy_cfg
= {};
1011 const struct acpi_device_id
*acpi_id
= NULL
;
1012 struct device
*dev
= &client
->dev
;
1013 struct max17042_chip
*chip
;
1018 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_WORD_DATA
))
1021 chip
= devm_kzalloc(&client
->dev
, sizeof(*chip
), GFP_KERNEL
);
1025 chip
->client
= client
;
1027 chip
->chip_type
= id
->driver_data
;
1029 acpi_id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
1033 chip
->chip_type
= acpi_id
->driver_data
;
1035 chip
->regmap
= devm_regmap_init_i2c(client
, &max17042_regmap_config
);
1036 if (IS_ERR(chip
->regmap
)) {
1037 dev_err(&client
->dev
, "Failed to initialize regmap\n");
1041 chip
->pdata
= max17042_get_pdata(chip
);
1043 dev_err(&client
->dev
, "no platform data provided\n");
1047 i2c_set_clientdata(client
, chip
);
1048 psy_cfg
.drv_data
= chip
;
1049 psy_cfg
.of_node
= dev
->of_node
;
1051 /* When current is not measured,
1052 * CURRENT_NOW and CURRENT_AVG properties should be invisible. */
1053 if (!chip
->pdata
->enable_current_sense
)
1054 max17042_desc
= &max17042_no_current_sense_psy_desc
;
1056 if (chip
->pdata
->r_sns
== 0)
1057 chip
->pdata
->r_sns
= MAX17042_DEFAULT_SNS_RESISTOR
;
1059 if (chip
->pdata
->init_data
)
1060 for (i
= 0; i
< chip
->pdata
->num_init_data
; i
++)
1061 regmap_write(chip
->regmap
,
1062 chip
->pdata
->init_data
[i
].addr
,
1063 chip
->pdata
->init_data
[i
].data
);
1065 if (!chip
->pdata
->enable_current_sense
) {
1066 regmap_write(chip
->regmap
, MAX17042_CGAIN
, 0x0000);
1067 regmap_write(chip
->regmap
, MAX17042_MiscCFG
, 0x0003);
1068 regmap_write(chip
->regmap
, MAX17042_LearnCFG
, 0x0007);
1071 chip
->battery
= devm_power_supply_register(&client
->dev
, max17042_desc
,
1073 if (IS_ERR(chip
->battery
)) {
1074 dev_err(&client
->dev
, "failed: power supply register\n");
1075 return PTR_ERR(chip
->battery
);
1079 unsigned int flags
= IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
;
1082 * On ACPI systems the IRQ may be handled by ACPI-event code,
1083 * so we need to share (if the ACPI code is willing to share).
1086 flags
|= IRQF_SHARED
| IRQF_PROBE_SHARED
;
1088 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1090 max17042_thread_handler
, flags
,
1091 chip
->battery
->desc
->name
,
1094 regmap_update_bits(chip
->regmap
, MAX17042_CONFIG
,
1095 CONFIG_ALRT_BIT_ENBL
,
1096 CONFIG_ALRT_BIT_ENBL
);
1097 max17042_set_soc_threshold(chip
, 1);
1101 dev_err(&client
->dev
, "Failed to get IRQ\n");
1104 /* Not able to update the charge threshold when exceeded? -> disable */
1106 regmap_write(chip
->regmap
, MAX17042_SALRT_Th
, 0xff00);
1108 regmap_read(chip
->regmap
, MAX17042_STATUS
, &val
);
1109 if (val
& STATUS_POR_BIT
) {
1110 INIT_WORK(&chip
->work
, max17042_init_worker
);
1111 ret
= devm_add_action(&client
->dev
, max17042_stop_work
, chip
);
1114 schedule_work(&chip
->work
);
1116 chip
->init_complete
= 1;
1122 #ifdef CONFIG_PM_SLEEP
1123 static int max17042_suspend(struct device
*dev
)
1125 struct max17042_chip
*chip
= dev_get_drvdata(dev
);
1128 * disable the irq and enable irq_wake
1129 * capability to the interrupt line.
1131 if (chip
->client
->irq
) {
1132 disable_irq(chip
->client
->irq
);
1133 enable_irq_wake(chip
->client
->irq
);
1139 static int max17042_resume(struct device
*dev
)
1141 struct max17042_chip
*chip
= dev_get_drvdata(dev
);
1143 if (chip
->client
->irq
) {
1144 disable_irq_wake(chip
->client
->irq
);
1145 enable_irq(chip
->client
->irq
);
1146 /* re-program the SOC thresholds to 1% change */
1147 max17042_set_soc_threshold(chip
, 1);
1154 static SIMPLE_DEV_PM_OPS(max17042_pm_ops
, max17042_suspend
,
1158 static const struct acpi_device_id max17042_acpi_match
[] = {
1159 { "MAX17047", MAXIM_DEVICE_TYPE_MAX17047
},
1162 MODULE_DEVICE_TABLE(acpi
, max17042_acpi_match
);
1166 static const struct of_device_id max17042_dt_match
[] = {
1167 { .compatible
= "maxim,max17042" },
1168 { .compatible
= "maxim,max17047" },
1169 { .compatible
= "maxim,max17050" },
1172 MODULE_DEVICE_TABLE(of
, max17042_dt_match
);
1175 static const struct i2c_device_id max17042_id
[] = {
1176 { "max17042", MAXIM_DEVICE_TYPE_MAX17042
},
1177 { "max17047", MAXIM_DEVICE_TYPE_MAX17047
},
1178 { "max17050", MAXIM_DEVICE_TYPE_MAX17050
},
1181 MODULE_DEVICE_TABLE(i2c
, max17042_id
);
1183 static struct i2c_driver max17042_i2c_driver
= {
1186 .acpi_match_table
= ACPI_PTR(max17042_acpi_match
),
1187 .of_match_table
= of_match_ptr(max17042_dt_match
),
1188 .pm
= &max17042_pm_ops
,
1190 .probe
= max17042_probe
,
1191 .id_table
= max17042_id
,
1193 module_i2c_driver(max17042_i2c_driver
);
1195 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1196 MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
1197 MODULE_LICENSE("GPL");