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/acpi.h>
26 #include <linux/init.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/i2c.h>
30 #include <linux/delay.h>
31 #include <linux/interrupt.h>
33 #include <linux/mod_devicetable.h>
34 #include <linux/power_supply.h>
35 #include <linux/power/max17042_battery.h>
37 #include <linux/regmap.h>
39 /* Status register bits */
40 #define STATUS_POR_BIT (1 << 1)
41 #define STATUS_BST_BIT (1 << 3)
42 #define STATUS_VMN_BIT (1 << 8)
43 #define STATUS_TMN_BIT (1 << 9)
44 #define STATUS_SMN_BIT (1 << 10)
45 #define STATUS_BI_BIT (1 << 11)
46 #define STATUS_VMX_BIT (1 << 12)
47 #define STATUS_TMX_BIT (1 << 13)
48 #define STATUS_SMX_BIT (1 << 14)
49 #define STATUS_BR_BIT (1 << 15)
51 /* Interrupt mask bits */
52 #define CONFIG_ALRT_BIT_ENBL (1 << 2)
53 #define STATUS_INTR_SOCMIN_BIT (1 << 10)
54 #define STATUS_INTR_SOCMAX_BIT (1 << 14)
56 #define VFSOC0_LOCK 0x0000
57 #define VFSOC0_UNLOCK 0x0080
58 #define MODEL_UNLOCK1 0X0059
59 #define MODEL_UNLOCK2 0X00C4
60 #define MODEL_LOCK1 0X0000
61 #define MODEL_LOCK2 0X0000
63 #define dQ_ACC_DIV 0x4
64 #define dP_ACC_100 0x1900
65 #define dP_ACC_200 0x3200
67 #define MAX17042_VMAX_TOLERANCE 50 /* 50 mV */
69 struct 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
;
79 static enum power_supply_property max17042_battery_props
[] = {
80 POWER_SUPPLY_PROP_STATUS
,
81 POWER_SUPPLY_PROP_PRESENT
,
82 POWER_SUPPLY_PROP_TECHNOLOGY
,
83 POWER_SUPPLY_PROP_CYCLE_COUNT
,
84 POWER_SUPPLY_PROP_VOLTAGE_MAX
,
85 POWER_SUPPLY_PROP_VOLTAGE_MIN
,
86 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
87 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
88 POWER_SUPPLY_PROP_VOLTAGE_AVG
,
89 POWER_SUPPLY_PROP_VOLTAGE_OCV
,
90 POWER_SUPPLY_PROP_CAPACITY
,
91 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
92 POWER_SUPPLY_PROP_CHARGE_FULL
,
93 POWER_SUPPLY_PROP_CHARGE_NOW
,
94 POWER_SUPPLY_PROP_CHARGE_COUNTER
,
95 POWER_SUPPLY_PROP_TEMP
,
96 POWER_SUPPLY_PROP_TEMP_ALERT_MIN
,
97 POWER_SUPPLY_PROP_TEMP_ALERT_MAX
,
98 POWER_SUPPLY_PROP_TEMP_MIN
,
99 POWER_SUPPLY_PROP_TEMP_MAX
,
100 POWER_SUPPLY_PROP_HEALTH
,
101 POWER_SUPPLY_PROP_SCOPE
,
102 POWER_SUPPLY_PROP_CURRENT_NOW
,
103 POWER_SUPPLY_PROP_CURRENT_AVG
,
106 static int max17042_get_temperature(struct max17042_chip
*chip
, int *temp
)
110 struct regmap
*map
= chip
->regmap
;
112 ret
= regmap_read(map
, MAX17042_TEMP
, &data
);
116 *temp
= sign_extend32(data
, 15);
117 /* The value is converted into deci-centigrade scale */
118 /* Units of LSB = 1 / 256 degree Celsius */
119 *temp
= *temp
* 10 / 256;
123 static int max17042_get_status(struct max17042_chip
*chip
, int *status
)
125 int ret
, charge_full
, charge_now
;
127 ret
= power_supply_am_i_supplied(chip
->battery
);
129 *status
= POWER_SUPPLY_STATUS_UNKNOWN
;
133 *status
= POWER_SUPPLY_STATUS_DISCHARGING
;
138 * The MAX170xx has builtin end-of-charge detection and will update
139 * FullCAP to match RepCap when it detects end of charging.
141 * When this cycle the battery gets charged to a higher (calculated)
142 * capacity then the previous cycle then FullCAP will get updated
143 * contineously once end-of-charge detection kicks in, so allow the
147 ret
= regmap_read(chip
->regmap
, MAX17042_FullCAP
, &charge_full
);
151 ret
= regmap_read(chip
->regmap
, MAX17042_RepCap
, &charge_now
);
155 if ((charge_full
- charge_now
) <= MAX17042_FULL_THRESHOLD
)
156 *status
= POWER_SUPPLY_STATUS_FULL
;
158 *status
= POWER_SUPPLY_STATUS_CHARGING
;
163 static int max17042_get_battery_health(struct max17042_chip
*chip
, int *health
)
165 int temp
, vavg
, vbatt
, ret
;
168 ret
= regmap_read(chip
->regmap
, MAX17042_AvgVCELL
, &val
);
172 /* bits [0-3] unused */
173 vavg
= val
* 625 / 8;
174 /* Convert to millivolts */
177 ret
= regmap_read(chip
->regmap
, MAX17042_VCELL
, &val
);
181 /* bits [0-3] unused */
182 vbatt
= val
* 625 / 8;
183 /* Convert to millivolts */
186 if (vavg
< chip
->pdata
->vmin
) {
187 *health
= POWER_SUPPLY_HEALTH_DEAD
;
191 if (vbatt
> chip
->pdata
->vmax
+ MAX17042_VMAX_TOLERANCE
) {
192 *health
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
196 ret
= max17042_get_temperature(chip
, &temp
);
200 if (temp
< chip
->pdata
->temp_min
) {
201 *health
= POWER_SUPPLY_HEALTH_COLD
;
205 if (temp
> chip
->pdata
->temp_max
) {
206 *health
= POWER_SUPPLY_HEALTH_OVERHEAT
;
210 *health
= POWER_SUPPLY_HEALTH_GOOD
;
219 static int max17042_get_property(struct power_supply
*psy
,
220 enum power_supply_property psp
,
221 union power_supply_propval
*val
)
223 struct max17042_chip
*chip
= power_supply_get_drvdata(psy
);
224 struct regmap
*map
= chip
->regmap
;
229 if (!chip
->init_complete
)
233 case POWER_SUPPLY_PROP_STATUS
:
234 ret
= max17042_get_status(chip
, &val
->intval
);
238 case POWER_SUPPLY_PROP_PRESENT
:
239 ret
= regmap_read(map
, MAX17042_STATUS
, &data
);
243 if (data
& MAX17042_STATUS_BattAbsent
)
248 case POWER_SUPPLY_PROP_TECHNOLOGY
:
249 val
->intval
= POWER_SUPPLY_TECHNOLOGY_LION
;
251 case POWER_SUPPLY_PROP_CYCLE_COUNT
:
252 ret
= regmap_read(map
, MAX17042_Cycles
, &data
);
258 case POWER_SUPPLY_PROP_VOLTAGE_MAX
:
259 ret
= regmap_read(map
, MAX17042_MinMaxVolt
, &data
);
263 val
->intval
= data
>> 8;
264 val
->intval
*= 20000; /* Units of LSB = 20mV */
266 case POWER_SUPPLY_PROP_VOLTAGE_MIN
:
267 ret
= regmap_read(map
, MAX17042_MinMaxVolt
, &data
);
271 val
->intval
= (data
& 0xff) * 20000; /* Units of 20mV */
273 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
274 if (chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17042
)
275 ret
= regmap_read(map
, MAX17042_V_empty
, &data
);
277 ret
= regmap_read(map
, MAX17047_V_empty
, &data
);
281 val
->intval
= data
>> 7;
282 val
->intval
*= 10000; /* Units of LSB = 10mV */
284 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
285 ret
= regmap_read(map
, MAX17042_VCELL
, &data
);
289 val
->intval
= data
* 625 / 8;
291 case POWER_SUPPLY_PROP_VOLTAGE_AVG
:
292 ret
= regmap_read(map
, MAX17042_AvgVCELL
, &data
);
296 val
->intval
= data
* 625 / 8;
298 case POWER_SUPPLY_PROP_VOLTAGE_OCV
:
299 ret
= regmap_read(map
, MAX17042_OCVInternal
, &data
);
303 val
->intval
= data
* 625 / 8;
305 case POWER_SUPPLY_PROP_CAPACITY
:
306 ret
= regmap_read(map
, MAX17042_RepSOC
, &data
);
310 val
->intval
= data
>> 8;
312 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
313 ret
= regmap_read(map
, MAX17042_DesignCap
, &data
);
317 data64
= data
* 5000000ll;
318 do_div(data64
, chip
->pdata
->r_sns
);
319 val
->intval
= data64
;
321 case POWER_SUPPLY_PROP_CHARGE_FULL
:
322 ret
= regmap_read(map
, MAX17042_FullCAP
, &data
);
326 data64
= data
* 5000000ll;
327 do_div(data64
, chip
->pdata
->r_sns
);
328 val
->intval
= data64
;
330 case POWER_SUPPLY_PROP_CHARGE_NOW
:
331 ret
= regmap_read(map
, MAX17042_RepCap
, &data
);
335 data64
= data
* 5000000ll;
336 do_div(data64
, chip
->pdata
->r_sns
);
337 val
->intval
= data64
;
339 case POWER_SUPPLY_PROP_CHARGE_COUNTER
:
340 ret
= regmap_read(map
, MAX17042_QH
, &data
);
344 val
->intval
= data
* 1000 / 2;
346 case POWER_SUPPLY_PROP_TEMP
:
347 ret
= max17042_get_temperature(chip
, &val
->intval
);
351 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN
:
352 ret
= regmap_read(map
, MAX17042_TALRT_Th
, &data
);
355 /* LSB is Alert Minimum. In deci-centigrade */
356 val
->intval
= sign_extend32(data
& 0xff, 7) * 10;
358 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX
:
359 ret
= regmap_read(map
, MAX17042_TALRT_Th
, &data
);
362 /* MSB is Alert Maximum. In deci-centigrade */
363 val
->intval
= sign_extend32(data
>> 8, 7) * 10;
365 case POWER_SUPPLY_PROP_TEMP_MIN
:
366 val
->intval
= chip
->pdata
->temp_min
;
368 case POWER_SUPPLY_PROP_TEMP_MAX
:
369 val
->intval
= chip
->pdata
->temp_max
;
371 case POWER_SUPPLY_PROP_HEALTH
:
372 ret
= max17042_get_battery_health(chip
, &val
->intval
);
376 case POWER_SUPPLY_PROP_SCOPE
:
377 val
->intval
= POWER_SUPPLY_SCOPE_SYSTEM
;
379 case POWER_SUPPLY_PROP_CURRENT_NOW
:
380 if (chip
->pdata
->enable_current_sense
) {
381 ret
= regmap_read(map
, MAX17042_Current
, &data
);
385 val
->intval
= sign_extend32(data
, 15);
386 val
->intval
*= 1562500 / chip
->pdata
->r_sns
;
391 case POWER_SUPPLY_PROP_CURRENT_AVG
:
392 if (chip
->pdata
->enable_current_sense
) {
393 ret
= regmap_read(map
, MAX17042_AvgCurrent
, &data
);
397 val
->intval
= sign_extend32(data
, 15);
398 val
->intval
*= 1562500 / chip
->pdata
->r_sns
;
409 static int max17042_set_property(struct power_supply
*psy
,
410 enum power_supply_property psp
,
411 const union power_supply_propval
*val
)
413 struct max17042_chip
*chip
= power_supply_get_drvdata(psy
);
414 struct regmap
*map
= chip
->regmap
;
420 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN
:
421 ret
= regmap_read(map
, MAX17042_TALRT_Th
, &data
);
425 /* Input in deci-centigrade, convert to centigrade */
426 temp
= val
->intval
/ 10;
427 /* force min < max */
428 if (temp
>= (int8_t)(data
>> 8))
429 temp
= (int8_t)(data
>> 8) - 1;
430 /* Write both MAX and MIN ALERT */
431 data
= (data
& 0xff00) + temp
;
432 ret
= regmap_write(map
, MAX17042_TALRT_Th
, data
);
434 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX
:
435 ret
= regmap_read(map
, MAX17042_TALRT_Th
, &data
);
439 /* Input in Deci-Centigrade, convert to centigrade */
440 temp
= val
->intval
/ 10;
441 /* force max > min */
442 if (temp
<= (int8_t)(data
& 0xff))
443 temp
= (int8_t)(data
& 0xff) + 1;
444 /* Write both MAX and MIN ALERT */
445 data
= (data
& 0xff) + (temp
<< 8);
446 ret
= regmap_write(map
, MAX17042_TALRT_Th
, data
);
455 static int max17042_property_is_writeable(struct power_supply
*psy
,
456 enum power_supply_property psp
)
461 case POWER_SUPPLY_PROP_TEMP_ALERT_MIN
:
462 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX
:
472 static void max17042_external_power_changed(struct power_supply
*psy
)
474 power_supply_changed(psy
);
477 static int max17042_write_verify_reg(struct regmap
*map
, u8 reg
, u32 value
)
484 ret
= regmap_write(map
, reg
, value
);
485 regmap_read(map
, reg
, &read_value
);
486 if (read_value
!= value
) {
490 } while (retries
&& read_value
!= value
);
493 pr_err("%s: err %d\n", __func__
, ret
);
498 static inline void max17042_override_por(struct regmap
*map
,
502 regmap_write(map
, reg
, value
);
505 static inline void max10742_unlock_model(struct max17042_chip
*chip
)
507 struct regmap
*map
= chip
->regmap
;
509 regmap_write(map
, MAX17042_MLOCKReg1
, MODEL_UNLOCK1
);
510 regmap_write(map
, MAX17042_MLOCKReg2
, MODEL_UNLOCK2
);
513 static inline void max10742_lock_model(struct max17042_chip
*chip
)
515 struct regmap
*map
= chip
->regmap
;
517 regmap_write(map
, MAX17042_MLOCKReg1
, MODEL_LOCK1
);
518 regmap_write(map
, MAX17042_MLOCKReg2
, MODEL_LOCK2
);
521 static inline void max17042_write_model_data(struct max17042_chip
*chip
,
524 struct regmap
*map
= chip
->regmap
;
527 for (i
= 0; i
< size
; i
++)
528 regmap_write(map
, addr
+ i
,
529 chip
->pdata
->config_data
->cell_char_tbl
[i
]);
532 static inline void max17042_read_model_data(struct max17042_chip
*chip
,
533 u8 addr
, u16
*data
, int size
)
535 struct regmap
*map
= chip
->regmap
;
539 for (i
= 0; i
< size
; i
++) {
540 regmap_read(map
, addr
+ i
, &tmp
);
545 static inline int max17042_model_data_compare(struct max17042_chip
*chip
,
546 u16
*data1
, u16
*data2
, int size
)
550 if (memcmp(data1
, data2
, size
)) {
551 dev_err(&chip
->client
->dev
, "%s compare failed\n", __func__
);
552 for (i
= 0; i
< size
; i
++)
553 dev_info(&chip
->client
->dev
, "0x%x, 0x%x",
555 dev_info(&chip
->client
->dev
, "\n");
561 static int max17042_init_model(struct max17042_chip
*chip
)
564 int table_size
= ARRAY_SIZE(chip
->pdata
->config_data
->cell_char_tbl
);
567 temp_data
= kcalloc(table_size
, sizeof(*temp_data
), GFP_KERNEL
);
571 max10742_unlock_model(chip
);
572 max17042_write_model_data(chip
, MAX17042_MODELChrTbl
,
574 max17042_read_model_data(chip
, MAX17042_MODELChrTbl
, temp_data
,
577 ret
= max17042_model_data_compare(
579 chip
->pdata
->config_data
->cell_char_tbl
,
583 max10742_lock_model(chip
);
589 static int max17042_verify_model_lock(struct max17042_chip
*chip
)
592 int table_size
= ARRAY_SIZE(chip
->pdata
->config_data
->cell_char_tbl
);
596 temp_data
= kcalloc(table_size
, sizeof(*temp_data
), GFP_KERNEL
);
600 max17042_read_model_data(chip
, MAX17042_MODELChrTbl
, temp_data
,
602 for (i
= 0; i
< table_size
; i
++)
610 static void max17042_write_config_regs(struct max17042_chip
*chip
)
612 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
613 struct regmap
*map
= chip
->regmap
;
615 regmap_write(map
, MAX17042_CONFIG
, config
->config
);
616 regmap_write(map
, MAX17042_LearnCFG
, config
->learn_cfg
);
617 regmap_write(map
, MAX17042_FilterCFG
,
619 regmap_write(map
, MAX17042_RelaxCFG
, config
->relax_cfg
);
620 if (chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17047
||
621 chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17050
)
622 regmap_write(map
, MAX17047_FullSOCThr
,
623 config
->full_soc_thresh
);
626 static void max17042_write_custom_regs(struct max17042_chip
*chip
)
628 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
629 struct regmap
*map
= chip
->regmap
;
631 max17042_write_verify_reg(map
, MAX17042_RCOMP0
, config
->rcomp0
);
632 max17042_write_verify_reg(map
, MAX17042_TempCo
, config
->tcompc0
);
633 max17042_write_verify_reg(map
, MAX17042_ICHGTerm
, config
->ichgt_term
);
634 if (chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17042
) {
635 regmap_write(map
, MAX17042_EmptyTempCo
, config
->empty_tempco
);
636 max17042_write_verify_reg(map
, MAX17042_K_empty0
,
639 max17042_write_verify_reg(map
, MAX17047_QRTbl00
,
641 max17042_write_verify_reg(map
, MAX17047_QRTbl10
,
643 max17042_write_verify_reg(map
, MAX17047_QRTbl20
,
645 max17042_write_verify_reg(map
, MAX17047_QRTbl30
,
650 static void max17042_update_capacity_regs(struct max17042_chip
*chip
)
652 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
653 struct regmap
*map
= chip
->regmap
;
655 max17042_write_verify_reg(map
, MAX17042_FullCAP
,
657 regmap_write(map
, MAX17042_DesignCap
, config
->design_cap
);
658 max17042_write_verify_reg(map
, MAX17042_FullCAPNom
,
662 static void max17042_reset_vfsoc0_reg(struct max17042_chip
*chip
)
665 struct regmap
*map
= chip
->regmap
;
667 regmap_read(map
, MAX17042_VFSOC
, &vfSoc
);
668 regmap_write(map
, MAX17042_VFSOC0Enable
, VFSOC0_UNLOCK
);
669 max17042_write_verify_reg(map
, MAX17042_VFSOC0
, vfSoc
);
670 regmap_write(map
, MAX17042_VFSOC0Enable
, VFSOC0_LOCK
);
673 static void max17042_load_new_capacity_params(struct max17042_chip
*chip
)
675 u32 full_cap0
, rep_cap
, dq_acc
, vfSoc
;
678 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
679 struct regmap
*map
= chip
->regmap
;
681 regmap_read(map
, MAX17042_FullCAP0
, &full_cap0
);
682 regmap_read(map
, MAX17042_VFSOC
, &vfSoc
);
684 /* fg_vfSoc needs to shifted by 8 bits to get the
685 * perc in 1% accuracy, to get the right rem_cap multiply
686 * full_cap0, fg_vfSoc and devide by 100
688 rem_cap
= ((vfSoc
>> 8) * full_cap0
) / 100;
689 max17042_write_verify_reg(map
, MAX17042_RemCap
, rem_cap
);
692 max17042_write_verify_reg(map
, MAX17042_RepCap
, rep_cap
);
694 /* Write dQ_acc to 200% of Capacity and dP_acc to 200% */
695 dq_acc
= config
->fullcap
/ dQ_ACC_DIV
;
696 max17042_write_verify_reg(map
, MAX17042_dQacc
, dq_acc
);
697 max17042_write_verify_reg(map
, MAX17042_dPacc
, dP_ACC_200
);
699 max17042_write_verify_reg(map
, MAX17042_FullCAP
,
701 regmap_write(map
, MAX17042_DesignCap
,
703 max17042_write_verify_reg(map
, MAX17042_FullCAPNom
,
705 /* Update SOC register with new SOC */
706 regmap_write(map
, MAX17042_RepSOC
, vfSoc
);
710 * Block write all the override values coming from platform data.
711 * This function MUST be called before the POR initialization proceedure
712 * specified by maxim.
714 static inline void max17042_override_por_values(struct max17042_chip
*chip
)
716 struct regmap
*map
= chip
->regmap
;
717 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
719 max17042_override_por(map
, MAX17042_TGAIN
, config
->tgain
);
720 max17042_override_por(map
, MAx17042_TOFF
, config
->toff
);
721 max17042_override_por(map
, MAX17042_CGAIN
, config
->cgain
);
722 max17042_override_por(map
, MAX17042_COFF
, config
->coff
);
724 max17042_override_por(map
, MAX17042_VALRT_Th
, config
->valrt_thresh
);
725 max17042_override_por(map
, MAX17042_TALRT_Th
, config
->talrt_thresh
);
726 max17042_override_por(map
, MAX17042_SALRT_Th
,
727 config
->soc_alrt_thresh
);
728 max17042_override_por(map
, MAX17042_CONFIG
, config
->config
);
729 max17042_override_por(map
, MAX17042_SHDNTIMER
, config
->shdntimer
);
731 max17042_override_por(map
, MAX17042_DesignCap
, config
->design_cap
);
732 max17042_override_por(map
, MAX17042_ICHGTerm
, config
->ichgt_term
);
734 max17042_override_por(map
, MAX17042_AtRate
, config
->at_rate
);
735 max17042_override_por(map
, MAX17042_LearnCFG
, config
->learn_cfg
);
736 max17042_override_por(map
, MAX17042_FilterCFG
, config
->filter_cfg
);
737 max17042_override_por(map
, MAX17042_RelaxCFG
, config
->relax_cfg
);
738 max17042_override_por(map
, MAX17042_MiscCFG
, config
->misc_cfg
);
739 max17042_override_por(map
, MAX17042_MaskSOC
, config
->masksoc
);
741 max17042_override_por(map
, MAX17042_FullCAP
, config
->fullcap
);
742 max17042_override_por(map
, MAX17042_FullCAPNom
, config
->fullcapnom
);
743 if (chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17042
)
744 max17042_override_por(map
, MAX17042_SOC_empty
,
746 max17042_override_por(map
, MAX17042_LAvg_empty
, config
->lavg_empty
);
747 max17042_override_por(map
, MAX17042_dQacc
, config
->dqacc
);
748 max17042_override_por(map
, MAX17042_dPacc
, config
->dpacc
);
750 if (chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17042
)
751 max17042_override_por(map
, MAX17042_V_empty
, config
->vempty
);
753 max17042_override_por(map
, MAX17047_V_empty
, config
->vempty
);
754 max17042_override_por(map
, MAX17042_TempNom
, config
->temp_nom
);
755 max17042_override_por(map
, MAX17042_TempLim
, config
->temp_lim
);
756 max17042_override_por(map
, MAX17042_FCTC
, config
->fctc
);
757 max17042_override_por(map
, MAX17042_RCOMP0
, config
->rcomp0
);
758 max17042_override_por(map
, MAX17042_TempCo
, config
->tcompc0
);
759 if (chip
->chip_type
) {
760 max17042_override_por(map
, MAX17042_EmptyTempCo
,
761 config
->empty_tempco
);
762 max17042_override_por(map
, MAX17042_K_empty0
,
767 static int max17042_init_chip(struct max17042_chip
*chip
)
769 struct regmap
*map
= chip
->regmap
;
772 max17042_override_por_values(chip
);
773 /* After Power up, the MAX17042 requires 500mS in order
774 * to perform signal debouncing and initial SOC reporting
778 /* Initialize configaration */
779 max17042_write_config_regs(chip
);
781 /* write cell characterization data */
782 ret
= max17042_init_model(chip
);
784 dev_err(&chip
->client
->dev
, "%s init failed\n",
789 ret
= max17042_verify_model_lock(chip
);
791 dev_err(&chip
->client
->dev
, "%s lock verify failed\n",
795 /* write custom parameters */
796 max17042_write_custom_regs(chip
);
798 /* update capacity params */
799 max17042_update_capacity_regs(chip
);
801 /* delay must be atleast 350mS to allow VFSOC
802 * to be calculated from the new configuration
806 /* reset vfsoc0 reg */
807 max17042_reset_vfsoc0_reg(chip
);
809 /* load new capacity params */
810 max17042_load_new_capacity_params(chip
);
812 /* Init complete, Clear the POR bit */
813 regmap_update_bits(map
, MAX17042_STATUS
, STATUS_POR_BIT
, 0x0);
817 static void max17042_set_soc_threshold(struct max17042_chip
*chip
, u16 off
)
819 struct regmap
*map
= chip
->regmap
;
822 /* program interrupt thesholds such that we should
823 * get interrupt for every 'off' perc change in the soc
825 regmap_read(map
, MAX17042_RepSOC
, &soc
);
827 soc_tr
= (soc
+ off
) << 8;
828 soc_tr
|= (soc
- off
);
829 regmap_write(map
, MAX17042_SALRT_Th
, soc_tr
);
832 static irqreturn_t
max17042_thread_handler(int id
, void *dev
)
834 struct max17042_chip
*chip
= dev
;
837 regmap_read(chip
->regmap
, MAX17042_STATUS
, &val
);
838 if ((val
& STATUS_INTR_SOCMIN_BIT
) ||
839 (val
& STATUS_INTR_SOCMAX_BIT
)) {
840 dev_info(&chip
->client
->dev
, "SOC threshold INTR\n");
841 max17042_set_soc_threshold(chip
, 1);
844 power_supply_changed(chip
->battery
);
848 static void max17042_init_worker(struct work_struct
*work
)
850 struct max17042_chip
*chip
= container_of(work
,
851 struct max17042_chip
, work
);
854 /* Initialize registers according to values from the platform data */
855 if (chip
->pdata
->enable_por_init
&& chip
->pdata
->config_data
) {
856 ret
= max17042_init_chip(chip
);
861 chip
->init_complete
= 1;
865 static struct max17042_platform_data
*
866 max17042_get_pdata(struct max17042_chip
*chip
)
868 struct device
*dev
= &chip
->client
->dev
;
869 struct device_node
*np
= dev
->of_node
;
871 struct max17042_platform_data
*pdata
;
874 return dev
->platform_data
;
876 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
881 * Require current sense resistor value to be specified for
882 * current-sense functionality to be enabled at all.
884 if (of_property_read_u32(np
, "maxim,rsns-microohm", &prop
) == 0) {
886 pdata
->enable_current_sense
= true;
889 if (of_property_read_s32(np
, "maxim,cold-temp", &pdata
->temp_min
))
890 pdata
->temp_min
= INT_MIN
;
891 if (of_property_read_s32(np
, "maxim,over-heat-temp", &pdata
->temp_max
))
892 pdata
->temp_max
= INT_MAX
;
893 if (of_property_read_s32(np
, "maxim,dead-volt", &pdata
->vmin
))
894 pdata
->vmin
= INT_MIN
;
895 if (of_property_read_s32(np
, "maxim,over-volt", &pdata
->vmax
))
896 pdata
->vmax
= INT_MAX
;
901 static struct max17042_reg_data max17047_default_pdata_init_regs
[] = {
903 * Some firmwares do not set FullSOCThr, Enable End-of-Charge Detection
904 * when the voltage FG reports 95%, as recommended in the datasheet.
906 { MAX17047_FullSOCThr
, MAX17042_BATTERY_FULL
<< 8 },
909 static struct max17042_platform_data
*
910 max17042_get_pdata(struct max17042_chip
*chip
)
912 struct device
*dev
= &chip
->client
->dev
;
913 struct max17042_platform_data
*pdata
;
916 if (dev
->platform_data
)
917 return dev
->platform_data
;
920 * The MAX17047 gets used on x86 where we might not have pdata, assume
921 * the firmware will already have initialized the fuel-gauge and provide
922 * default values for the non init bits to make things work.
924 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
928 if (chip
->chip_type
!= MAXIM_DEVICE_TYPE_MAX17042
) {
929 pdata
->init_data
= max17047_default_pdata_init_regs
;
930 pdata
->num_init_data
=
931 ARRAY_SIZE(max17047_default_pdata_init_regs
);
934 ret
= regmap_read(chip
->regmap
, MAX17042_MiscCFG
, &misc_cfg
);
938 /* If bits 0-1 are set to 3 then only Voltage readings are used */
939 if ((misc_cfg
& 0x3) == 0x3)
940 pdata
->enable_current_sense
= false;
942 pdata
->enable_current_sense
= true;
944 pdata
->vmin
= MAX17042_DEFAULT_VMIN
;
945 pdata
->vmax
= MAX17042_DEFAULT_VMAX
;
946 pdata
->temp_min
= MAX17042_DEFAULT_TEMP_MIN
;
947 pdata
->temp_max
= MAX17042_DEFAULT_TEMP_MAX
;
953 static const struct regmap_config max17042_regmap_config
= {
956 .val_format_endian
= REGMAP_ENDIAN_NATIVE
,
959 static const struct power_supply_desc max17042_psy_desc
= {
960 .name
= "max170xx_battery",
961 .type
= POWER_SUPPLY_TYPE_BATTERY
,
962 .get_property
= max17042_get_property
,
963 .set_property
= max17042_set_property
,
964 .property_is_writeable
= max17042_property_is_writeable
,
965 .external_power_changed
= max17042_external_power_changed
,
966 .properties
= max17042_battery_props
,
967 .num_properties
= ARRAY_SIZE(max17042_battery_props
),
970 static const struct power_supply_desc max17042_no_current_sense_psy_desc
= {
971 .name
= "max170xx_battery",
972 .type
= POWER_SUPPLY_TYPE_BATTERY
,
973 .get_property
= max17042_get_property
,
974 .set_property
= max17042_set_property
,
975 .property_is_writeable
= max17042_property_is_writeable
,
976 .properties
= max17042_battery_props
,
977 .num_properties
= ARRAY_SIZE(max17042_battery_props
) - 2,
980 static int max17042_probe(struct i2c_client
*client
,
981 const struct i2c_device_id
*id
)
983 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
984 const struct power_supply_desc
*max17042_desc
= &max17042_psy_desc
;
985 struct power_supply_config psy_cfg
= {};
986 const struct acpi_device_id
*acpi_id
= NULL
;
987 struct device
*dev
= &client
->dev
;
988 struct max17042_chip
*chip
;
993 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_WORD_DATA
))
996 chip
= devm_kzalloc(&client
->dev
, sizeof(*chip
), GFP_KERNEL
);
1000 chip
->client
= client
;
1002 chip
->chip_type
= id
->driver_data
;
1004 acpi_id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
1008 chip
->chip_type
= acpi_id
->driver_data
;
1010 chip
->regmap
= devm_regmap_init_i2c(client
, &max17042_regmap_config
);
1011 if (IS_ERR(chip
->regmap
)) {
1012 dev_err(&client
->dev
, "Failed to initialize regmap\n");
1016 chip
->pdata
= max17042_get_pdata(chip
);
1018 dev_err(&client
->dev
, "no platform data provided\n");
1022 i2c_set_clientdata(client
, chip
);
1023 psy_cfg
.drv_data
= chip
;
1024 psy_cfg
.of_node
= dev
->of_node
;
1026 /* When current is not measured,
1027 * CURRENT_NOW and CURRENT_AVG properties should be invisible. */
1028 if (!chip
->pdata
->enable_current_sense
)
1029 max17042_desc
= &max17042_no_current_sense_psy_desc
;
1031 if (chip
->pdata
->r_sns
== 0)
1032 chip
->pdata
->r_sns
= MAX17042_DEFAULT_SNS_RESISTOR
;
1034 if (chip
->pdata
->init_data
)
1035 for (i
= 0; i
< chip
->pdata
->num_init_data
; i
++)
1036 regmap_write(chip
->regmap
,
1037 chip
->pdata
->init_data
[i
].addr
,
1038 chip
->pdata
->init_data
[i
].data
);
1040 if (!chip
->pdata
->enable_current_sense
) {
1041 regmap_write(chip
->regmap
, MAX17042_CGAIN
, 0x0000);
1042 regmap_write(chip
->regmap
, MAX17042_MiscCFG
, 0x0003);
1043 regmap_write(chip
->regmap
, MAX17042_LearnCFG
, 0x0007);
1046 chip
->battery
= devm_power_supply_register(&client
->dev
, max17042_desc
,
1048 if (IS_ERR(chip
->battery
)) {
1049 dev_err(&client
->dev
, "failed: power supply register\n");
1050 return PTR_ERR(chip
->battery
);
1054 unsigned int flags
= IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
;
1057 * On ACPI systems the IRQ may be handled by ACPI-event code,
1058 * so we need to share (if the ACPI code is willing to share).
1061 flags
|= IRQF_SHARED
| IRQF_PROBE_SHARED
;
1063 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
1065 max17042_thread_handler
, flags
,
1066 chip
->battery
->desc
->name
,
1069 regmap_update_bits(chip
->regmap
, MAX17042_CONFIG
,
1070 CONFIG_ALRT_BIT_ENBL
,
1071 CONFIG_ALRT_BIT_ENBL
);
1072 max17042_set_soc_threshold(chip
, 1);
1076 dev_err(&client
->dev
, "Failed to get IRQ\n");
1079 /* Not able to update the charge threshold when exceeded? -> disable */
1081 regmap_write(chip
->regmap
, MAX17042_SALRT_Th
, 0xff00);
1083 regmap_read(chip
->regmap
, MAX17042_STATUS
, &val
);
1084 if (val
& STATUS_POR_BIT
) {
1085 INIT_WORK(&chip
->work
, max17042_init_worker
);
1086 schedule_work(&chip
->work
);
1088 chip
->init_complete
= 1;
1094 #ifdef CONFIG_PM_SLEEP
1095 static int max17042_suspend(struct device
*dev
)
1097 struct max17042_chip
*chip
= dev_get_drvdata(dev
);
1100 * disable the irq and enable irq_wake
1101 * capability to the interrupt line.
1103 if (chip
->client
->irq
) {
1104 disable_irq(chip
->client
->irq
);
1105 enable_irq_wake(chip
->client
->irq
);
1111 static int max17042_resume(struct device
*dev
)
1113 struct max17042_chip
*chip
= dev_get_drvdata(dev
);
1115 if (chip
->client
->irq
) {
1116 disable_irq_wake(chip
->client
->irq
);
1117 enable_irq(chip
->client
->irq
);
1118 /* re-program the SOC thresholds to 1% change */
1119 max17042_set_soc_threshold(chip
, 1);
1126 static SIMPLE_DEV_PM_OPS(max17042_pm_ops
, max17042_suspend
,
1130 static const struct acpi_device_id max17042_acpi_match
[] = {
1131 { "MAX17047", MAXIM_DEVICE_TYPE_MAX17047
},
1134 MODULE_DEVICE_TABLE(acpi
, max17042_acpi_match
);
1138 static const struct of_device_id max17042_dt_match
[] = {
1139 { .compatible
= "maxim,max17042" },
1140 { .compatible
= "maxim,max17047" },
1141 { .compatible
= "maxim,max17050" },
1144 MODULE_DEVICE_TABLE(of
, max17042_dt_match
);
1147 static const struct i2c_device_id max17042_id
[] = {
1148 { "max17042", MAXIM_DEVICE_TYPE_MAX17042
},
1149 { "max17047", MAXIM_DEVICE_TYPE_MAX17047
},
1150 { "max17050", MAXIM_DEVICE_TYPE_MAX17050
},
1153 MODULE_DEVICE_TABLE(i2c
, max17042_id
);
1155 static struct i2c_driver max17042_i2c_driver
= {
1158 .acpi_match_table
= ACPI_PTR(max17042_acpi_match
),
1159 .of_match_table
= of_match_ptr(max17042_dt_match
),
1160 .pm
= &max17042_pm_ops
,
1162 .probe
= max17042_probe
,
1163 .id_table
= max17042_id
,
1165 module_i2c_driver(max17042_i2c_driver
);
1167 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1168 MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
1169 MODULE_LICENSE("GPL");