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 struct max17042_chip
{
67 struct i2c_client
*client
;
68 struct regmap
*regmap
;
69 struct power_supply
*battery
;
70 enum max170xx_chip_type chip_type
;
71 struct max17042_platform_data
*pdata
;
72 struct work_struct work
;
76 static enum power_supply_property max17042_battery_props
[] = {
77 POWER_SUPPLY_PROP_PRESENT
,
78 POWER_SUPPLY_PROP_CYCLE_COUNT
,
79 POWER_SUPPLY_PROP_VOLTAGE_MAX
,
80 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
81 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
82 POWER_SUPPLY_PROP_VOLTAGE_AVG
,
83 POWER_SUPPLY_PROP_VOLTAGE_OCV
,
84 POWER_SUPPLY_PROP_CAPACITY
,
85 POWER_SUPPLY_PROP_CHARGE_FULL
,
86 POWER_SUPPLY_PROP_CHARGE_COUNTER
,
87 POWER_SUPPLY_PROP_TEMP
,
88 POWER_SUPPLY_PROP_CURRENT_NOW
,
89 POWER_SUPPLY_PROP_CURRENT_AVG
,
92 static int max17042_get_property(struct power_supply
*psy
,
93 enum power_supply_property psp
,
94 union power_supply_propval
*val
)
96 struct max17042_chip
*chip
= power_supply_get_drvdata(psy
);
97 struct regmap
*map
= chip
->regmap
;
101 if (!chip
->init_complete
)
105 case POWER_SUPPLY_PROP_PRESENT
:
106 ret
= regmap_read(map
, MAX17042_STATUS
, &data
);
110 if (data
& MAX17042_STATUS_BattAbsent
)
115 case POWER_SUPPLY_PROP_CYCLE_COUNT
:
116 ret
= regmap_read(map
, MAX17042_Cycles
, &data
);
122 case POWER_SUPPLY_PROP_VOLTAGE_MAX
:
123 ret
= regmap_read(map
, MAX17042_MinMaxVolt
, &data
);
127 val
->intval
= data
>> 8;
128 val
->intval
*= 20000; /* Units of LSB = 20mV */
130 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
131 if (chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17042
)
132 ret
= regmap_read(map
, MAX17042_V_empty
, &data
);
134 ret
= regmap_read(map
, MAX17047_V_empty
, &data
);
138 val
->intval
= data
>> 7;
139 val
->intval
*= 10000; /* Units of LSB = 10mV */
141 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
142 ret
= regmap_read(map
, MAX17042_VCELL
, &data
);
146 val
->intval
= data
* 625 / 8;
148 case POWER_SUPPLY_PROP_VOLTAGE_AVG
:
149 ret
= regmap_read(map
, MAX17042_AvgVCELL
, &data
);
153 val
->intval
= data
* 625 / 8;
155 case POWER_SUPPLY_PROP_VOLTAGE_OCV
:
156 ret
= regmap_read(map
, MAX17042_OCVInternal
, &data
);
160 val
->intval
= data
* 625 / 8;
162 case POWER_SUPPLY_PROP_CAPACITY
:
163 ret
= regmap_read(map
, MAX17042_RepSOC
, &data
);
167 val
->intval
= data
>> 8;
169 case POWER_SUPPLY_PROP_CHARGE_FULL
:
170 ret
= regmap_read(map
, MAX17042_FullCAP
, &data
);
174 val
->intval
= data
* 1000 / 2;
176 case POWER_SUPPLY_PROP_CHARGE_COUNTER
:
177 ret
= regmap_read(map
, MAX17042_QH
, &data
);
181 val
->intval
= data
* 1000 / 2;
183 case POWER_SUPPLY_PROP_TEMP
:
184 ret
= regmap_read(map
, MAX17042_TEMP
, &data
);
189 /* The value is signed. */
190 if (val
->intval
& 0x8000) {
191 val
->intval
= (0x7fff & ~val
->intval
) + 1;
194 /* The value is converted into deci-centigrade scale */
195 /* Units of LSB = 1 / 256 degree Celsius */
196 val
->intval
= val
->intval
* 10 / 256;
198 case POWER_SUPPLY_PROP_CURRENT_NOW
:
199 if (chip
->pdata
->enable_current_sense
) {
200 ret
= regmap_read(map
, MAX17042_Current
, &data
);
205 if (val
->intval
& 0x8000) {
207 val
->intval
= ~val
->intval
& 0x7fff;
211 val
->intval
*= 1562500 / chip
->pdata
->r_sns
;
216 case POWER_SUPPLY_PROP_CURRENT_AVG
:
217 if (chip
->pdata
->enable_current_sense
) {
218 ret
= regmap_read(map
, MAX17042_AvgCurrent
, &data
);
223 if (val
->intval
& 0x8000) {
225 val
->intval
= ~val
->intval
& 0x7fff;
229 val
->intval
*= 1562500 / chip
->pdata
->r_sns
;
240 static int max17042_write_verify_reg(struct regmap
*map
, u8 reg
, u32 value
)
247 ret
= regmap_write(map
, reg
, value
);
248 regmap_read(map
, reg
, &read_value
);
249 if (read_value
!= value
) {
253 } while (retries
&& read_value
!= value
);
256 pr_err("%s: err %d\n", __func__
, ret
);
261 static inline void max17042_override_por(struct regmap
*map
,
265 regmap_write(map
, reg
, value
);
268 static inline void max10742_unlock_model(struct max17042_chip
*chip
)
270 struct regmap
*map
= chip
->regmap
;
272 regmap_write(map
, MAX17042_MLOCKReg1
, MODEL_UNLOCK1
);
273 regmap_write(map
, MAX17042_MLOCKReg2
, MODEL_UNLOCK2
);
276 static inline void max10742_lock_model(struct max17042_chip
*chip
)
278 struct regmap
*map
= chip
->regmap
;
280 regmap_write(map
, MAX17042_MLOCKReg1
, MODEL_LOCK1
);
281 regmap_write(map
, MAX17042_MLOCKReg2
, MODEL_LOCK2
);
284 static inline void max17042_write_model_data(struct max17042_chip
*chip
,
287 struct regmap
*map
= chip
->regmap
;
290 for (i
= 0; i
< size
; i
++)
291 regmap_write(map
, addr
+ i
,
292 chip
->pdata
->config_data
->cell_char_tbl
[i
]);
295 static inline void max17042_read_model_data(struct max17042_chip
*chip
,
296 u8 addr
, u32
*data
, int size
)
298 struct regmap
*map
= chip
->regmap
;
301 for (i
= 0; i
< size
; i
++)
302 regmap_read(map
, addr
+ i
, &data
[i
]);
305 static inline int max17042_model_data_compare(struct max17042_chip
*chip
,
306 u16
*data1
, u16
*data2
, int size
)
310 if (memcmp(data1
, data2
, size
)) {
311 dev_err(&chip
->client
->dev
, "%s compare failed\n", __func__
);
312 for (i
= 0; i
< size
; i
++)
313 dev_info(&chip
->client
->dev
, "0x%x, 0x%x",
315 dev_info(&chip
->client
->dev
, "\n");
321 static int max17042_init_model(struct max17042_chip
*chip
)
324 int table_size
= ARRAY_SIZE(chip
->pdata
->config_data
->cell_char_tbl
);
327 temp_data
= kcalloc(table_size
, sizeof(*temp_data
), GFP_KERNEL
);
331 max10742_unlock_model(chip
);
332 max17042_write_model_data(chip
, MAX17042_MODELChrTbl
,
334 max17042_read_model_data(chip
, MAX17042_MODELChrTbl
, temp_data
,
337 ret
= max17042_model_data_compare(
339 chip
->pdata
->config_data
->cell_char_tbl
,
343 max10742_lock_model(chip
);
349 static int max17042_verify_model_lock(struct max17042_chip
*chip
)
352 int table_size
= ARRAY_SIZE(chip
->pdata
->config_data
->cell_char_tbl
);
356 temp_data
= kcalloc(table_size
, sizeof(*temp_data
), GFP_KERNEL
);
360 max17042_read_model_data(chip
, MAX17042_MODELChrTbl
, temp_data
,
362 for (i
= 0; i
< table_size
; i
++)
370 static void max17042_write_config_regs(struct max17042_chip
*chip
)
372 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
373 struct regmap
*map
= chip
->regmap
;
375 regmap_write(map
, MAX17042_CONFIG
, config
->config
);
376 regmap_write(map
, MAX17042_LearnCFG
, config
->learn_cfg
);
377 regmap_write(map
, MAX17042_FilterCFG
,
379 regmap_write(map
, MAX17042_RelaxCFG
, config
->relax_cfg
);
380 if (chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17047
||
381 chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17050
)
382 regmap_write(map
, MAX17047_FullSOCThr
,
383 config
->full_soc_thresh
);
386 static void max17042_write_custom_regs(struct max17042_chip
*chip
)
388 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
389 struct regmap
*map
= chip
->regmap
;
391 max17042_write_verify_reg(map
, MAX17042_RCOMP0
, config
->rcomp0
);
392 max17042_write_verify_reg(map
, MAX17042_TempCo
, config
->tcompc0
);
393 max17042_write_verify_reg(map
, MAX17042_ICHGTerm
, config
->ichgt_term
);
394 if (chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17042
) {
395 regmap_write(map
, MAX17042_EmptyTempCo
, config
->empty_tempco
);
396 max17042_write_verify_reg(map
, MAX17042_K_empty0
,
399 max17042_write_verify_reg(map
, MAX17047_QRTbl00
,
401 max17042_write_verify_reg(map
, MAX17047_QRTbl10
,
403 max17042_write_verify_reg(map
, MAX17047_QRTbl20
,
405 max17042_write_verify_reg(map
, MAX17047_QRTbl30
,
410 static void max17042_update_capacity_regs(struct max17042_chip
*chip
)
412 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
413 struct regmap
*map
= chip
->regmap
;
415 max17042_write_verify_reg(map
, MAX17042_FullCAP
,
417 regmap_write(map
, MAX17042_DesignCap
, config
->design_cap
);
418 max17042_write_verify_reg(map
, MAX17042_FullCAPNom
,
422 static void max17042_reset_vfsoc0_reg(struct max17042_chip
*chip
)
425 struct regmap
*map
= chip
->regmap
;
427 regmap_read(map
, MAX17042_VFSOC
, &vfSoc
);
428 regmap_write(map
, MAX17042_VFSOC0Enable
, VFSOC0_UNLOCK
);
429 max17042_write_verify_reg(map
, MAX17042_VFSOC0
, vfSoc
);
430 regmap_write(map
, MAX17042_VFSOC0Enable
, VFSOC0_LOCK
);
433 static void max17042_load_new_capacity_params(struct max17042_chip
*chip
)
435 u32 full_cap0
, rep_cap
, dq_acc
, vfSoc
;
438 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
439 struct regmap
*map
= chip
->regmap
;
441 regmap_read(map
, MAX17042_FullCAP0
, &full_cap0
);
442 regmap_read(map
, MAX17042_VFSOC
, &vfSoc
);
444 /* fg_vfSoc needs to shifted by 8 bits to get the
445 * perc in 1% accuracy, to get the right rem_cap multiply
446 * full_cap0, fg_vfSoc and devide by 100
448 rem_cap
= ((vfSoc
>> 8) * full_cap0
) / 100;
449 max17042_write_verify_reg(map
, MAX17042_RemCap
, rem_cap
);
452 max17042_write_verify_reg(map
, MAX17042_RepCap
, rep_cap
);
454 /* Write dQ_acc to 200% of Capacity and dP_acc to 200% */
455 dq_acc
= config
->fullcap
/ dQ_ACC_DIV
;
456 max17042_write_verify_reg(map
, MAX17042_dQacc
, dq_acc
);
457 max17042_write_verify_reg(map
, MAX17042_dPacc
, dP_ACC_200
);
459 max17042_write_verify_reg(map
, MAX17042_FullCAP
,
461 regmap_write(map
, MAX17042_DesignCap
,
463 max17042_write_verify_reg(map
, MAX17042_FullCAPNom
,
465 /* Update SOC register with new SOC */
466 regmap_write(map
, MAX17042_RepSOC
, vfSoc
);
470 * Block write all the override values coming from platform data.
471 * This function MUST be called before the POR initialization proceedure
472 * specified by maxim.
474 static inline void max17042_override_por_values(struct max17042_chip
*chip
)
476 struct regmap
*map
= chip
->regmap
;
477 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
479 max17042_override_por(map
, MAX17042_TGAIN
, config
->tgain
);
480 max17042_override_por(map
, MAx17042_TOFF
, config
->toff
);
481 max17042_override_por(map
, MAX17042_CGAIN
, config
->cgain
);
482 max17042_override_por(map
, MAX17042_COFF
, config
->coff
);
484 max17042_override_por(map
, MAX17042_VALRT_Th
, config
->valrt_thresh
);
485 max17042_override_por(map
, MAX17042_TALRT_Th
, config
->talrt_thresh
);
486 max17042_override_por(map
, MAX17042_SALRT_Th
,
487 config
->soc_alrt_thresh
);
488 max17042_override_por(map
, MAX17042_CONFIG
, config
->config
);
489 max17042_override_por(map
, MAX17042_SHDNTIMER
, config
->shdntimer
);
491 max17042_override_por(map
, MAX17042_DesignCap
, config
->design_cap
);
492 max17042_override_por(map
, MAX17042_ICHGTerm
, config
->ichgt_term
);
494 max17042_override_por(map
, MAX17042_AtRate
, config
->at_rate
);
495 max17042_override_por(map
, MAX17042_LearnCFG
, config
->learn_cfg
);
496 max17042_override_por(map
, MAX17042_FilterCFG
, config
->filter_cfg
);
497 max17042_override_por(map
, MAX17042_RelaxCFG
, config
->relax_cfg
);
498 max17042_override_por(map
, MAX17042_MiscCFG
, config
->misc_cfg
);
499 max17042_override_por(map
, MAX17042_MaskSOC
, config
->masksoc
);
501 max17042_override_por(map
, MAX17042_FullCAP
, config
->fullcap
);
502 max17042_override_por(map
, MAX17042_FullCAPNom
, config
->fullcapnom
);
503 if (chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17042
)
504 max17042_override_por(map
, MAX17042_SOC_empty
,
506 max17042_override_por(map
, MAX17042_LAvg_empty
, config
->lavg_empty
);
507 max17042_override_por(map
, MAX17042_dQacc
, config
->dqacc
);
508 max17042_override_por(map
, MAX17042_dPacc
, config
->dpacc
);
510 if (chip
->chip_type
== MAXIM_DEVICE_TYPE_MAX17042
)
511 max17042_override_por(map
, MAX17042_V_empty
, config
->vempty
);
513 max17042_override_por(map
, MAX17047_V_empty
, config
->vempty
);
514 max17042_override_por(map
, MAX17042_TempNom
, config
->temp_nom
);
515 max17042_override_por(map
, MAX17042_TempLim
, config
->temp_lim
);
516 max17042_override_por(map
, MAX17042_FCTC
, config
->fctc
);
517 max17042_override_por(map
, MAX17042_RCOMP0
, config
->rcomp0
);
518 max17042_override_por(map
, MAX17042_TempCo
, config
->tcompc0
);
519 if (chip
->chip_type
) {
520 max17042_override_por(map
, MAX17042_EmptyTempCo
,
521 config
->empty_tempco
);
522 max17042_override_por(map
, MAX17042_K_empty0
,
527 static int max17042_init_chip(struct max17042_chip
*chip
)
529 struct regmap
*map
= chip
->regmap
;
532 max17042_override_por_values(chip
);
533 /* After Power up, the MAX17042 requires 500mS in order
534 * to perform signal debouncing and initial SOC reporting
538 /* Initialize configaration */
539 max17042_write_config_regs(chip
);
541 /* write cell characterization data */
542 ret
= max17042_init_model(chip
);
544 dev_err(&chip
->client
->dev
, "%s init failed\n",
549 ret
= max17042_verify_model_lock(chip
);
551 dev_err(&chip
->client
->dev
, "%s lock verify failed\n",
555 /* write custom parameters */
556 max17042_write_custom_regs(chip
);
558 /* update capacity params */
559 max17042_update_capacity_regs(chip
);
561 /* delay must be atleast 350mS to allow VFSOC
562 * to be calculated from the new configuration
566 /* reset vfsoc0 reg */
567 max17042_reset_vfsoc0_reg(chip
);
569 /* load new capacity params */
570 max17042_load_new_capacity_params(chip
);
572 /* Init complete, Clear the POR bit */
573 regmap_update_bits(map
, MAX17042_STATUS
, STATUS_POR_BIT
, 0x0);
577 static void max17042_set_soc_threshold(struct max17042_chip
*chip
, u16 off
)
579 struct regmap
*map
= chip
->regmap
;
582 /* program interrupt thesholds such that we should
583 * get interrupt for every 'off' perc change in the soc
585 regmap_read(map
, MAX17042_RepSOC
, &soc
);
587 soc_tr
= (soc
+ off
) << 8;
588 soc_tr
|= (soc
- off
);
589 regmap_write(map
, MAX17042_SALRT_Th
, soc_tr
);
592 static irqreturn_t
max17042_thread_handler(int id
, void *dev
)
594 struct max17042_chip
*chip
= dev
;
597 regmap_read(chip
->regmap
, MAX17042_STATUS
, &val
);
598 if ((val
& STATUS_INTR_SOCMIN_BIT
) ||
599 (val
& STATUS_INTR_SOCMAX_BIT
)) {
600 dev_info(&chip
->client
->dev
, "SOC threshold INTR\n");
601 max17042_set_soc_threshold(chip
, 1);
604 power_supply_changed(chip
->battery
);
608 static void max17042_init_worker(struct work_struct
*work
)
610 struct max17042_chip
*chip
= container_of(work
,
611 struct max17042_chip
, work
);
614 /* Initialize registers according to values from the platform data */
615 if (chip
->pdata
->enable_por_init
&& chip
->pdata
->config_data
) {
616 ret
= max17042_init_chip(chip
);
621 chip
->init_complete
= 1;
625 static struct max17042_platform_data
*
626 max17042_get_pdata(struct device
*dev
)
628 struct device_node
*np
= dev
->of_node
;
630 struct max17042_platform_data
*pdata
;
633 return dev
->platform_data
;
635 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
640 * Require current sense resistor value to be specified for
641 * current-sense functionality to be enabled at all.
643 if (of_property_read_u32(np
, "maxim,rsns-microohm", &prop
) == 0) {
645 pdata
->enable_current_sense
= true;
651 static struct max17042_platform_data
*
652 max17042_get_pdata(struct device
*dev
)
654 return dev
->platform_data
;
658 static const struct regmap_config max17042_regmap_config
= {
661 .val_format_endian
= REGMAP_ENDIAN_NATIVE
,
664 static const struct power_supply_desc max17042_psy_desc
= {
665 .name
= "max170xx_battery",
666 .type
= POWER_SUPPLY_TYPE_BATTERY
,
667 .get_property
= max17042_get_property
,
668 .properties
= max17042_battery_props
,
669 .num_properties
= ARRAY_SIZE(max17042_battery_props
),
672 static const struct power_supply_desc max17042_no_current_sense_psy_desc
= {
673 .name
= "max170xx_battery",
674 .type
= POWER_SUPPLY_TYPE_BATTERY
,
675 .get_property
= max17042_get_property
,
676 .properties
= max17042_battery_props
,
677 .num_properties
= ARRAY_SIZE(max17042_battery_props
) - 2,
680 static int max17042_probe(struct i2c_client
*client
,
681 const struct i2c_device_id
*id
)
683 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
684 const struct power_supply_desc
*max17042_desc
= &max17042_psy_desc
;
685 struct power_supply_config psy_cfg
= {};
686 struct max17042_chip
*chip
;
691 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_WORD_DATA
))
694 chip
= devm_kzalloc(&client
->dev
, sizeof(*chip
), GFP_KERNEL
);
698 chip
->client
= client
;
699 chip
->regmap
= devm_regmap_init_i2c(client
, &max17042_regmap_config
);
700 if (IS_ERR(chip
->regmap
)) {
701 dev_err(&client
->dev
, "Failed to initialize regmap\n");
705 chip
->pdata
= max17042_get_pdata(&client
->dev
);
707 dev_err(&client
->dev
, "no platform data provided\n");
711 i2c_set_clientdata(client
, chip
);
712 chip
->chip_type
= id
->driver_data
;
713 psy_cfg
.drv_data
= chip
;
715 /* When current is not measured,
716 * CURRENT_NOW and CURRENT_AVG properties should be invisible. */
717 if (!chip
->pdata
->enable_current_sense
)
718 max17042_desc
= &max17042_no_current_sense_psy_desc
;
720 if (chip
->pdata
->r_sns
== 0)
721 chip
->pdata
->r_sns
= MAX17042_DEFAULT_SNS_RESISTOR
;
723 if (chip
->pdata
->init_data
)
724 for (i
= 0; i
< chip
->pdata
->num_init_data
; i
++)
725 regmap_write(chip
->regmap
,
726 chip
->pdata
->init_data
[i
].addr
,
727 chip
->pdata
->init_data
[i
].data
);
729 if (!chip
->pdata
->enable_current_sense
) {
730 regmap_write(chip
->regmap
, MAX17042_CGAIN
, 0x0000);
731 regmap_write(chip
->regmap
, MAX17042_MiscCFG
, 0x0003);
732 regmap_write(chip
->regmap
, MAX17042_LearnCFG
, 0x0007);
735 chip
->battery
= power_supply_register(&client
->dev
, max17042_desc
,
737 if (IS_ERR(chip
->battery
)) {
738 dev_err(&client
->dev
, "failed: power supply register\n");
739 return PTR_ERR(chip
->battery
);
743 ret
= request_threaded_irq(client
->irq
, NULL
,
744 max17042_thread_handler
,
745 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
746 chip
->battery
->desc
->name
, chip
);
748 regmap_update_bits(chip
->regmap
, MAX17042_CONFIG
,
749 CONFIG_ALRT_BIT_ENBL
,
750 CONFIG_ALRT_BIT_ENBL
);
751 max17042_set_soc_threshold(chip
, 1);
754 dev_err(&client
->dev
, "%s(): cannot get IRQ\n",
759 regmap_read(chip
->regmap
, MAX17042_STATUS
, &val
);
760 if (val
& STATUS_POR_BIT
) {
761 INIT_WORK(&chip
->work
, max17042_init_worker
);
762 schedule_work(&chip
->work
);
764 chip
->init_complete
= 1;
770 static int max17042_remove(struct i2c_client
*client
)
772 struct max17042_chip
*chip
= i2c_get_clientdata(client
);
775 free_irq(client
->irq
, chip
);
776 power_supply_unregister(chip
->battery
);
780 #ifdef CONFIG_PM_SLEEP
781 static int max17042_suspend(struct device
*dev
)
783 struct max17042_chip
*chip
= dev_get_drvdata(dev
);
786 * disable the irq and enable irq_wake
787 * capability to the interrupt line.
789 if (chip
->client
->irq
) {
790 disable_irq(chip
->client
->irq
);
791 enable_irq_wake(chip
->client
->irq
);
797 static int max17042_resume(struct device
*dev
)
799 struct max17042_chip
*chip
= dev_get_drvdata(dev
);
801 if (chip
->client
->irq
) {
802 disable_irq_wake(chip
->client
->irq
);
803 enable_irq(chip
->client
->irq
);
804 /* re-program the SOC thresholds to 1% change */
805 max17042_set_soc_threshold(chip
, 1);
812 static SIMPLE_DEV_PM_OPS(max17042_pm_ops
, max17042_suspend
,
816 static const struct of_device_id max17042_dt_match
[] = {
817 { .compatible
= "maxim,max17042" },
818 { .compatible
= "maxim,max17047" },
819 { .compatible
= "maxim,max17050" },
822 MODULE_DEVICE_TABLE(of
, max17042_dt_match
);
825 static const struct i2c_device_id max17042_id
[] = {
826 { "max17042", MAXIM_DEVICE_TYPE_MAX17042
},
827 { "max17047", MAXIM_DEVICE_TYPE_MAX17047
},
828 { "max17050", MAXIM_DEVICE_TYPE_MAX17050
},
831 MODULE_DEVICE_TABLE(i2c
, max17042_id
);
833 static struct i2c_driver max17042_i2c_driver
= {
836 .of_match_table
= of_match_ptr(max17042_dt_match
),
837 .pm
= &max17042_pm_ops
,
839 .probe
= max17042_probe
,
840 .remove
= max17042_remove
,
841 .id_table
= max17042_id
,
843 module_i2c_driver(max17042_i2c_driver
);
845 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
846 MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
847 MODULE_LICENSE("GPL");