2 * Fuel gauge driver for Maxim 17042 / 8966 / 8997
3 * Note that Maxim 8966 and 8997 are mfd and this is its subdevice.
5 * Copyright (C) 2011 Samsung Electronics
6 * MyungJoo Ham <myungjoo.ham@samsung.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * This driver is based on max17040_battery.c
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/i2c.h>
29 #include <linux/delay.h>
30 #include <linux/interrupt.h>
32 #include <linux/mod_devicetable.h>
33 #include <linux/power_supply.h>
34 #include <linux/power/max17042_battery.h>
36 #include <linux/regmap.h>
38 /* Status register bits */
39 #define STATUS_POR_BIT (1 << 1)
40 #define STATUS_BST_BIT (1 << 3)
41 #define STATUS_VMN_BIT (1 << 8)
42 #define STATUS_TMN_BIT (1 << 9)
43 #define STATUS_SMN_BIT (1 << 10)
44 #define STATUS_BI_BIT (1 << 11)
45 #define STATUS_VMX_BIT (1 << 12)
46 #define STATUS_TMX_BIT (1 << 13)
47 #define STATUS_SMX_BIT (1 << 14)
48 #define STATUS_BR_BIT (1 << 15)
50 /* Interrupt mask bits */
51 #define CONFIG_ALRT_BIT_ENBL (1 << 2)
52 #define STATUS_INTR_SOCMIN_BIT (1 << 10)
53 #define STATUS_INTR_SOCMAX_BIT (1 << 14)
55 #define VFSOC0_LOCK 0x0000
56 #define VFSOC0_UNLOCK 0x0080
57 #define MODEL_UNLOCK1 0X0059
58 #define MODEL_UNLOCK2 0X00C4
59 #define MODEL_LOCK1 0X0000
60 #define MODEL_LOCK2 0X0000
62 #define dQ_ACC_DIV 0x4
63 #define dP_ACC_100 0x1900
64 #define dP_ACC_200 0x3200
66 #define MAX17042_IC_VERSION 0x0092
67 #define MAX17047_IC_VERSION 0x00AC /* same for max17050 */
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_PRESENT
,
81 POWER_SUPPLY_PROP_CYCLE_COUNT
,
82 POWER_SUPPLY_PROP_VOLTAGE_MAX
,
83 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
84 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
85 POWER_SUPPLY_PROP_VOLTAGE_AVG
,
86 POWER_SUPPLY_PROP_VOLTAGE_OCV
,
87 POWER_SUPPLY_PROP_CAPACITY
,
88 POWER_SUPPLY_PROP_CHARGE_FULL
,
89 POWER_SUPPLY_PROP_CHARGE_COUNTER
,
90 POWER_SUPPLY_PROP_TEMP
,
91 POWER_SUPPLY_PROP_CURRENT_NOW
,
92 POWER_SUPPLY_PROP_CURRENT_AVG
,
95 static int max17042_get_property(struct power_supply
*psy
,
96 enum power_supply_property psp
,
97 union power_supply_propval
*val
)
99 struct max17042_chip
*chip
= container_of(psy
,
100 struct max17042_chip
, battery
);
101 struct regmap
*map
= chip
->regmap
;
105 if (!chip
->init_complete
)
109 case POWER_SUPPLY_PROP_PRESENT
:
110 ret
= regmap_read(map
, MAX17042_STATUS
, &data
);
114 if (data
& MAX17042_STATUS_BattAbsent
)
119 case POWER_SUPPLY_PROP_CYCLE_COUNT
:
120 ret
= regmap_read(map
, MAX17042_Cycles
, &data
);
126 case POWER_SUPPLY_PROP_VOLTAGE_MAX
:
127 ret
= regmap_read(map
, MAX17042_MinMaxVolt
, &data
);
131 val
->intval
= data
>> 8;
132 val
->intval
*= 20000; /* Units of LSB = 20mV */
134 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
135 if (chip
->chip_type
== MAX17042
)
136 ret
= regmap_read(map
, MAX17042_V_empty
, &data
);
138 ret
= regmap_read(map
, MAX17047_V_empty
, &data
);
142 val
->intval
= data
>> 7;
143 val
->intval
*= 10000; /* Units of LSB = 10mV */
145 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
146 ret
= regmap_read(map
, MAX17042_VCELL
, &data
);
150 val
->intval
= data
* 625 / 8;
152 case POWER_SUPPLY_PROP_VOLTAGE_AVG
:
153 ret
= regmap_read(map
, MAX17042_AvgVCELL
, &data
);
157 val
->intval
= data
* 625 / 8;
159 case POWER_SUPPLY_PROP_VOLTAGE_OCV
:
160 ret
= regmap_read(map
, MAX17042_OCVInternal
, &data
);
164 val
->intval
= data
* 625 / 8;
166 case POWER_SUPPLY_PROP_CAPACITY
:
167 ret
= regmap_read(map
, MAX17042_RepSOC
, &data
);
171 val
->intval
= data
>> 8;
173 case POWER_SUPPLY_PROP_CHARGE_FULL
:
174 ret
= regmap_read(map
, MAX17042_FullCAP
, &data
);
178 val
->intval
= data
* 1000 / 2;
180 case POWER_SUPPLY_PROP_CHARGE_COUNTER
:
181 ret
= regmap_read(map
, MAX17042_QH
, &data
);
185 val
->intval
= data
* 1000 / 2;
187 case POWER_SUPPLY_PROP_TEMP
:
188 ret
= regmap_read(map
, MAX17042_TEMP
, &data
);
193 /* The value is signed. */
194 if (val
->intval
& 0x8000) {
195 val
->intval
= (0x7fff & ~val
->intval
) + 1;
198 /* The value is converted into deci-centigrade scale */
199 /* Units of LSB = 1 / 256 degree Celsius */
200 val
->intval
= val
->intval
* 10 / 256;
202 case POWER_SUPPLY_PROP_CURRENT_NOW
:
203 if (chip
->pdata
->enable_current_sense
) {
204 ret
= regmap_read(map
, MAX17042_Current
, &data
);
209 if (val
->intval
& 0x8000) {
211 val
->intval
= ~val
->intval
& 0x7fff;
215 val
->intval
*= 1562500 / chip
->pdata
->r_sns
;
220 case POWER_SUPPLY_PROP_CURRENT_AVG
:
221 if (chip
->pdata
->enable_current_sense
) {
222 ret
= regmap_read(map
, MAX17042_AvgCurrent
, &data
);
227 if (val
->intval
& 0x8000) {
229 val
->intval
= ~val
->intval
& 0x7fff;
233 val
->intval
*= 1562500 / chip
->pdata
->r_sns
;
244 static int max17042_write_verify_reg(struct regmap
*map
, u8 reg
, u32 value
)
251 ret
= regmap_write(map
, reg
, value
);
252 regmap_read(map
, reg
, &read_value
);
253 if (read_value
!= value
) {
257 } while (retries
&& read_value
!= value
);
260 pr_err("%s: err %d\n", __func__
, ret
);
265 static inline void max17042_override_por(struct regmap
*map
,
269 regmap_write(map
, reg
, value
);
272 static inline void max10742_unlock_model(struct max17042_chip
*chip
)
274 struct regmap
*map
= chip
->regmap
;
275 regmap_write(map
, MAX17042_MLOCKReg1
, MODEL_UNLOCK1
);
276 regmap_write(map
, MAX17042_MLOCKReg2
, MODEL_UNLOCK2
);
279 static inline void max10742_lock_model(struct max17042_chip
*chip
)
281 struct regmap
*map
= chip
->regmap
;
283 regmap_write(map
, MAX17042_MLOCKReg1
, MODEL_LOCK1
);
284 regmap_write(map
, MAX17042_MLOCKReg2
, MODEL_LOCK2
);
287 static inline void max17042_write_model_data(struct max17042_chip
*chip
,
290 struct regmap
*map
= chip
->regmap
;
292 for (i
= 0; i
< size
; i
++)
293 regmap_write(map
, addr
+ i
,
294 chip
->pdata
->config_data
->cell_char_tbl
[i
]);
297 static inline void max17042_read_model_data(struct max17042_chip
*chip
,
298 u8 addr
, u32
*data
, int size
)
300 struct regmap
*map
= chip
->regmap
;
303 for (i
= 0; i
< size
; i
++)
304 regmap_read(map
, addr
+ i
, &data
[i
]);
307 static inline int max17042_model_data_compare(struct max17042_chip
*chip
,
308 u16
*data1
, u16
*data2
, int size
)
312 if (memcmp(data1
, data2
, size
)) {
313 dev_err(&chip
->client
->dev
, "%s compare failed\n", __func__
);
314 for (i
= 0; i
< size
; i
++)
315 dev_info(&chip
->client
->dev
, "0x%x, 0x%x",
317 dev_info(&chip
->client
->dev
, "\n");
323 static int max17042_init_model(struct max17042_chip
*chip
)
326 int table_size
= ARRAY_SIZE(chip
->pdata
->config_data
->cell_char_tbl
);
329 temp_data
= kcalloc(table_size
, sizeof(*temp_data
), GFP_KERNEL
);
333 max10742_unlock_model(chip
);
334 max17042_write_model_data(chip
, MAX17042_MODELChrTbl
,
336 max17042_read_model_data(chip
, MAX17042_MODELChrTbl
, temp_data
,
339 ret
= max17042_model_data_compare(
341 chip
->pdata
->config_data
->cell_char_tbl
,
345 max10742_lock_model(chip
);
351 static int max17042_verify_model_lock(struct max17042_chip
*chip
)
354 int table_size
= ARRAY_SIZE(chip
->pdata
->config_data
->cell_char_tbl
);
358 temp_data
= kcalloc(table_size
, sizeof(*temp_data
), GFP_KERNEL
);
362 max17042_read_model_data(chip
, MAX17042_MODELChrTbl
, temp_data
,
364 for (i
= 0; i
< table_size
; i
++)
372 static void max17042_write_config_regs(struct max17042_chip
*chip
)
374 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
375 struct regmap
*map
= chip
->regmap
;
377 regmap_write(map
, MAX17042_CONFIG
, config
->config
);
378 regmap_write(map
, MAX17042_LearnCFG
, config
->learn_cfg
);
379 regmap_write(map
, MAX17042_FilterCFG
,
381 regmap_write(map
, MAX17042_RelaxCFG
, config
->relax_cfg
);
382 if (chip
->chip_type
== MAX17047
)
383 regmap_write(map
, MAX17047_FullSOCThr
,
384 config
->full_soc_thresh
);
387 static void max17042_write_custom_regs(struct max17042_chip
*chip
)
389 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
390 struct regmap
*map
= chip
->regmap
;
392 max17042_write_verify_reg(map
, MAX17042_RCOMP0
, config
->rcomp0
);
393 max17042_write_verify_reg(map
, MAX17042_TempCo
, config
->tcompc0
);
394 max17042_write_verify_reg(map
, MAX17042_ICHGTerm
, config
->ichgt_term
);
395 if (chip
->chip_type
== MAX17042
) {
396 regmap_write(map
, MAX17042_EmptyTempCo
, config
->empty_tempco
);
397 max17042_write_verify_reg(map
, MAX17042_K_empty0
,
400 max17042_write_verify_reg(map
, MAX17047_QRTbl00
,
402 max17042_write_verify_reg(map
, MAX17047_QRTbl10
,
404 max17042_write_verify_reg(map
, MAX17047_QRTbl20
,
406 max17042_write_verify_reg(map
, MAX17047_QRTbl30
,
411 static void max17042_update_capacity_regs(struct max17042_chip
*chip
)
413 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
414 struct regmap
*map
= chip
->regmap
;
416 max17042_write_verify_reg(map
, MAX17042_FullCAP
,
418 regmap_write(map
, MAX17042_DesignCap
, config
->design_cap
);
419 max17042_write_verify_reg(map
, MAX17042_FullCAPNom
,
423 static void max17042_reset_vfsoc0_reg(struct max17042_chip
*chip
)
426 struct regmap
*map
= chip
->regmap
;
428 regmap_read(map
, MAX17042_VFSOC
, &vfSoc
);
429 regmap_write(map
, MAX17042_VFSOC0Enable
, VFSOC0_UNLOCK
);
430 max17042_write_verify_reg(map
, MAX17042_VFSOC0
, vfSoc
);
431 regmap_write(map
, MAX17042_VFSOC0Enable
, VFSOC0_LOCK
);
434 static void max17042_load_new_capacity_params(struct max17042_chip
*chip
)
436 u32 full_cap0
, rep_cap
, dq_acc
, vfSoc
;
439 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
440 struct regmap
*map
= chip
->regmap
;
442 regmap_read(map
, MAX17042_FullCAP0
, &full_cap0
);
443 regmap_read(map
, MAX17042_VFSOC
, &vfSoc
);
445 /* fg_vfSoc needs to shifted by 8 bits to get the
446 * perc in 1% accuracy, to get the right rem_cap multiply
447 * full_cap0, fg_vfSoc and devide by 100
449 rem_cap
= ((vfSoc
>> 8) * full_cap0
) / 100;
450 max17042_write_verify_reg(map
, MAX17042_RemCap
, rem_cap
);
453 max17042_write_verify_reg(map
, MAX17042_RepCap
, rep_cap
);
455 /* Write dQ_acc to 200% of Capacity and dP_acc to 200% */
456 dq_acc
= config
->fullcap
/ dQ_ACC_DIV
;
457 max17042_write_verify_reg(map
, MAX17042_dQacc
, dq_acc
);
458 max17042_write_verify_reg(map
, MAX17042_dPacc
, dP_ACC_200
);
460 max17042_write_verify_reg(map
, MAX17042_FullCAP
,
462 regmap_write(map
, MAX17042_DesignCap
,
464 max17042_write_verify_reg(map
, MAX17042_FullCAPNom
,
466 /* Update SOC register with new SOC */
467 regmap_write(map
, MAX17042_RepSOC
, vfSoc
);
471 * Block write all the override values coming from platform data.
472 * This function MUST be called before the POR initialization proceedure
473 * specified by maxim.
475 static inline void max17042_override_por_values(struct max17042_chip
*chip
)
477 struct regmap
*map
= chip
->regmap
;
478 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
480 max17042_override_por(map
, MAX17042_TGAIN
, config
->tgain
);
481 max17042_override_por(map
, MAx17042_TOFF
, config
->toff
);
482 max17042_override_por(map
, MAX17042_CGAIN
, config
->cgain
);
483 max17042_override_por(map
, MAX17042_COFF
, config
->coff
);
485 max17042_override_por(map
, MAX17042_VALRT_Th
, config
->valrt_thresh
);
486 max17042_override_por(map
, MAX17042_TALRT_Th
, config
->talrt_thresh
);
487 max17042_override_por(map
, MAX17042_SALRT_Th
,
488 config
->soc_alrt_thresh
);
489 max17042_override_por(map
, MAX17042_CONFIG
, config
->config
);
490 max17042_override_por(map
, MAX17042_SHDNTIMER
, config
->shdntimer
);
492 max17042_override_por(map
, MAX17042_DesignCap
, config
->design_cap
);
493 max17042_override_por(map
, MAX17042_ICHGTerm
, config
->ichgt_term
);
495 max17042_override_por(map
, MAX17042_AtRate
, config
->at_rate
);
496 max17042_override_por(map
, MAX17042_LearnCFG
, config
->learn_cfg
);
497 max17042_override_por(map
, MAX17042_FilterCFG
, config
->filter_cfg
);
498 max17042_override_por(map
, MAX17042_RelaxCFG
, config
->relax_cfg
);
499 max17042_override_por(map
, MAX17042_MiscCFG
, config
->misc_cfg
);
500 max17042_override_por(map
, MAX17042_MaskSOC
, config
->masksoc
);
502 max17042_override_por(map
, MAX17042_FullCAP
, config
->fullcap
);
503 max17042_override_por(map
, MAX17042_FullCAPNom
, config
->fullcapnom
);
504 if (chip
->chip_type
== MAX17042
)
505 max17042_override_por(map
, MAX17042_SOC_empty
,
507 max17042_override_por(map
, MAX17042_LAvg_empty
, config
->lavg_empty
);
508 max17042_override_por(map
, MAX17042_dQacc
, config
->dqacc
);
509 max17042_override_por(map
, MAX17042_dPacc
, config
->dpacc
);
511 if (chip
->chip_type
== MAX17042
)
512 max17042_override_por(map
, MAX17042_V_empty
, config
->vempty
);
514 max17042_override_por(map
, MAX17047_V_empty
, config
->vempty
);
515 max17042_override_por(map
, MAX17042_TempNom
, config
->temp_nom
);
516 max17042_override_por(map
, MAX17042_TempLim
, config
->temp_lim
);
517 max17042_override_por(map
, MAX17042_FCTC
, config
->fctc
);
518 max17042_override_por(map
, MAX17042_RCOMP0
, config
->rcomp0
);
519 max17042_override_por(map
, MAX17042_TempCo
, config
->tcompc0
);
520 if (chip
->chip_type
) {
521 max17042_override_por(map
, MAX17042_EmptyTempCo
,
522 config
->empty_tempco
);
523 max17042_override_por(map
, MAX17042_K_empty0
,
528 static int max17042_init_chip(struct max17042_chip
*chip
)
530 struct regmap
*map
= chip
->regmap
;
534 max17042_override_por_values(chip
);
535 /* After Power up, the MAX17042 requires 500mS in order
536 * to perform signal debouncing and initial SOC reporting
540 /* Initialize configaration */
541 max17042_write_config_regs(chip
);
543 /* write cell characterization data */
544 ret
= max17042_init_model(chip
);
546 dev_err(&chip
->client
->dev
, "%s init failed\n",
551 ret
= max17042_verify_model_lock(chip
);
553 dev_err(&chip
->client
->dev
, "%s lock verify failed\n",
557 /* write custom parameters */
558 max17042_write_custom_regs(chip
);
560 /* update capacity params */
561 max17042_update_capacity_regs(chip
);
563 /* delay must be atleast 350mS to allow VFSOC
564 * to be calculated from the new configuration
568 /* reset vfsoc0 reg */
569 max17042_reset_vfsoc0_reg(chip
);
571 /* load new capacity params */
572 max17042_load_new_capacity_params(chip
);
574 /* Init complete, Clear the POR bit */
575 regmap_read(map
, MAX17042_STATUS
, &val
);
576 regmap_write(map
, MAX17042_STATUS
, val
& (~STATUS_POR_BIT
));
580 static void max17042_set_soc_threshold(struct max17042_chip
*chip
, u16 off
)
582 struct regmap
*map
= chip
->regmap
;
585 /* program interrupt thesholds such that we should
586 * get interrupt for every 'off' perc change in the soc
588 regmap_read(map
, MAX17042_RepSOC
, &soc
);
590 soc_tr
= (soc
+ off
) << 8;
591 soc_tr
|= (soc
- off
);
592 regmap_write(map
, MAX17042_SALRT_Th
, soc_tr
);
595 static irqreturn_t
max17042_thread_handler(int id
, void *dev
)
597 struct max17042_chip
*chip
= dev
;
600 regmap_read(chip
->regmap
, MAX17042_STATUS
, &val
);
601 if ((val
& STATUS_INTR_SOCMIN_BIT
) ||
602 (val
& STATUS_INTR_SOCMAX_BIT
)) {
603 dev_info(&chip
->client
->dev
, "SOC threshold INTR\n");
604 max17042_set_soc_threshold(chip
, 1);
607 power_supply_changed(&chip
->battery
);
611 static void max17042_init_worker(struct work_struct
*work
)
613 struct max17042_chip
*chip
= container_of(work
,
614 struct max17042_chip
, work
);
617 /* Initialize registers according to values from the platform data */
618 if (chip
->pdata
->enable_por_init
&& chip
->pdata
->config_data
) {
619 ret
= max17042_init_chip(chip
);
624 chip
->init_complete
= 1;
628 static struct max17042_platform_data
*
629 max17042_get_pdata(struct device
*dev
)
631 struct device_node
*np
= dev
->of_node
;
633 struct max17042_platform_data
*pdata
;
636 return dev
->platform_data
;
638 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
643 * Require current sense resistor value to be specified for
644 * current-sense functionality to be enabled at all.
646 if (of_property_read_u32(np
, "maxim,rsns-microohm", &prop
) == 0) {
648 pdata
->enable_current_sense
= true;
654 static struct max17042_platform_data
*
655 max17042_get_pdata(struct device
*dev
)
657 return dev
->platform_data
;
661 static struct regmap_config max17042_regmap_config
= {
664 .val_format_endian
= REGMAP_ENDIAN_NATIVE
,
667 static int max17042_probe(struct i2c_client
*client
,
668 const struct i2c_device_id
*id
)
670 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
671 struct max17042_chip
*chip
;
676 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_WORD_DATA
))
679 chip
= devm_kzalloc(&client
->dev
, sizeof(*chip
), GFP_KERNEL
);
683 chip
->client
= client
;
684 chip
->regmap
= devm_regmap_init_i2c(client
, &max17042_regmap_config
);
685 if (IS_ERR(chip
->regmap
)) {
686 dev_err(&client
->dev
, "Failed to initialize regmap\n");
690 chip
->pdata
= max17042_get_pdata(&client
->dev
);
692 dev_err(&client
->dev
, "no platform data provided\n");
696 i2c_set_clientdata(client
, chip
);
698 regmap_read(chip
->regmap
, MAX17042_DevName
, &val
);
699 if (val
== MAX17042_IC_VERSION
) {
700 dev_dbg(&client
->dev
, "chip type max17042 detected\n");
701 chip
->chip_type
= MAX17042
;
702 } else if (val
== MAX17047_IC_VERSION
) {
703 dev_dbg(&client
->dev
, "chip type max17047/50 detected\n");
704 chip
->chip_type
= MAX17047
;
706 dev_err(&client
->dev
, "device version mismatch: %x\n", val
);
710 chip
->battery
.name
= "max170xx_battery";
711 chip
->battery
.type
= POWER_SUPPLY_TYPE_BATTERY
;
712 chip
->battery
.get_property
= max17042_get_property
;
713 chip
->battery
.properties
= max17042_battery_props
;
714 chip
->battery
.num_properties
= ARRAY_SIZE(max17042_battery_props
);
716 /* When current is not measured,
717 * CURRENT_NOW and CURRENT_AVG properties should be invisible. */
718 if (!chip
->pdata
->enable_current_sense
)
719 chip
->battery
.num_properties
-= 2;
721 if (chip
->pdata
->r_sns
== 0)
722 chip
->pdata
->r_sns
= MAX17042_DEFAULT_SNS_RESISTOR
;
724 if (chip
->pdata
->init_data
)
725 for (i
= 0; i
< chip
->pdata
->num_init_data
; i
++)
726 regmap_write(chip
->regmap
,
727 chip
->pdata
->init_data
[i
].addr
,
728 chip
->pdata
->init_data
[i
].data
);
730 if (!chip
->pdata
->enable_current_sense
) {
731 regmap_write(chip
->regmap
, MAX17042_CGAIN
, 0x0000);
732 regmap_write(chip
->regmap
, MAX17042_MiscCFG
, 0x0003);
733 regmap_write(chip
->regmap
, MAX17042_LearnCFG
, 0x0007);
736 ret
= power_supply_register(&client
->dev
, &chip
->battery
);
738 dev_err(&client
->dev
, "failed: power supply register\n");
743 ret
= request_threaded_irq(client
->irq
, NULL
,
744 max17042_thread_handler
,
745 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
746 chip
->battery
.name
, chip
);
748 regmap_read(chip
->regmap
, MAX17042_CONFIG
, &val
);
749 val
|= CONFIG_ALRT_BIT_ENBL
;
750 regmap_write(chip
->regmap
, MAX17042_CONFIG
, val
);
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
[] = {
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");