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>
37 /* Status register bits */
38 #define STATUS_POR_BIT (1 << 1)
39 #define STATUS_BST_BIT (1 << 3)
40 #define STATUS_VMN_BIT (1 << 8)
41 #define STATUS_TMN_BIT (1 << 9)
42 #define STATUS_SMN_BIT (1 << 10)
43 #define STATUS_BI_BIT (1 << 11)
44 #define STATUS_VMX_BIT (1 << 12)
45 #define STATUS_TMX_BIT (1 << 13)
46 #define STATUS_SMX_BIT (1 << 14)
47 #define STATUS_BR_BIT (1 << 15)
49 /* Interrupt mask bits */
50 #define CONFIG_ALRT_BIT_ENBL (1 << 2)
51 #define STATUS_INTR_SOCMIN_BIT (1 << 10)
52 #define STATUS_INTR_SOCMAX_BIT (1 << 14)
54 #define VFSOC0_LOCK 0x0000
55 #define VFSOC0_UNLOCK 0x0080
56 #define MODEL_UNLOCK1 0X0059
57 #define MODEL_UNLOCK2 0X00C4
58 #define MODEL_LOCK1 0X0000
59 #define MODEL_LOCK2 0X0000
61 #define dQ_ACC_DIV 0x4
62 #define dP_ACC_100 0x1900
63 #define dP_ACC_200 0x3200
65 #define MAX17042_IC_VERSION 0x0092
66 #define MAX17047_IC_VERSION 0x00AC /* same for max17050 */
68 struct max17042_chip
{
69 struct i2c_client
*client
;
70 struct power_supply battery
;
71 enum max170xx_chip_type chip_type
;
72 struct max17042_platform_data
*pdata
;
73 struct work_struct work
;
77 static int max17042_write_reg(struct i2c_client
*client
, u8 reg
, u16 value
)
79 int ret
= i2c_smbus_write_word_data(client
, reg
, value
);
82 dev_err(&client
->dev
, "%s: err %d\n", __func__
, ret
);
87 static int max17042_read_reg(struct i2c_client
*client
, u8 reg
)
89 int ret
= i2c_smbus_read_word_data(client
, reg
);
92 dev_err(&client
->dev
, "%s: err %d\n", __func__
, ret
);
97 static void max17042_set_reg(struct i2c_client
*client
,
98 struct max17042_reg_data
*data
, int size
)
102 for (i
= 0; i
< size
; i
++)
103 max17042_write_reg(client
, data
[i
].addr
, data
[i
].data
);
106 static enum power_supply_property max17042_battery_props
[] = {
107 POWER_SUPPLY_PROP_PRESENT
,
108 POWER_SUPPLY_PROP_CYCLE_COUNT
,
109 POWER_SUPPLY_PROP_VOLTAGE_MAX
,
110 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
111 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
112 POWER_SUPPLY_PROP_VOLTAGE_AVG
,
113 POWER_SUPPLY_PROP_VOLTAGE_OCV
,
114 POWER_SUPPLY_PROP_CAPACITY
,
115 POWER_SUPPLY_PROP_CHARGE_FULL
,
116 POWER_SUPPLY_PROP_CHARGE_COUNTER
,
117 POWER_SUPPLY_PROP_TEMP
,
118 POWER_SUPPLY_PROP_CURRENT_NOW
,
119 POWER_SUPPLY_PROP_CURRENT_AVG
,
122 static int max17042_get_property(struct power_supply
*psy
,
123 enum power_supply_property psp
,
124 union power_supply_propval
*val
)
126 struct max17042_chip
*chip
= container_of(psy
,
127 struct max17042_chip
, battery
);
130 if (!chip
->init_complete
)
134 case POWER_SUPPLY_PROP_PRESENT
:
135 ret
= max17042_read_reg(chip
->client
, MAX17042_STATUS
);
139 if (ret
& MAX17042_STATUS_BattAbsent
)
144 case POWER_SUPPLY_PROP_CYCLE_COUNT
:
145 ret
= max17042_read_reg(chip
->client
, MAX17042_Cycles
);
151 case POWER_SUPPLY_PROP_VOLTAGE_MAX
:
152 ret
= max17042_read_reg(chip
->client
, MAX17042_MinMaxVolt
);
156 val
->intval
= ret
>> 8;
157 val
->intval
*= 20000; /* Units of LSB = 20mV */
159 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
160 if (chip
->chip_type
== MAX17042
)
161 ret
= max17042_read_reg(chip
->client
, MAX17042_V_empty
);
163 ret
= max17042_read_reg(chip
->client
, MAX17047_V_empty
);
167 val
->intval
= ret
>> 7;
168 val
->intval
*= 10000; /* Units of LSB = 10mV */
170 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
171 ret
= max17042_read_reg(chip
->client
, MAX17042_VCELL
);
175 val
->intval
= ret
* 625 / 8;
177 case POWER_SUPPLY_PROP_VOLTAGE_AVG
:
178 ret
= max17042_read_reg(chip
->client
, MAX17042_AvgVCELL
);
182 val
->intval
= ret
* 625 / 8;
184 case POWER_SUPPLY_PROP_VOLTAGE_OCV
:
185 ret
= max17042_read_reg(chip
->client
, MAX17042_OCVInternal
);
189 val
->intval
= ret
* 625 / 8;
191 case POWER_SUPPLY_PROP_CAPACITY
:
192 ret
= max17042_read_reg(chip
->client
, MAX17042_RepSOC
);
196 val
->intval
= ret
>> 8;
198 case POWER_SUPPLY_PROP_CHARGE_FULL
:
199 ret
= max17042_read_reg(chip
->client
, MAX17042_FullCAP
);
203 val
->intval
= ret
* 1000 / 2;
205 case POWER_SUPPLY_PROP_CHARGE_COUNTER
:
206 ret
= max17042_read_reg(chip
->client
, MAX17042_QH
);
210 val
->intval
= ret
* 1000 / 2;
212 case POWER_SUPPLY_PROP_TEMP
:
213 ret
= max17042_read_reg(chip
->client
, MAX17042_TEMP
);
218 /* The value is signed. */
219 if (val
->intval
& 0x8000) {
220 val
->intval
= (0x7fff & ~val
->intval
) + 1;
223 /* The value is converted into deci-centigrade scale */
224 /* Units of LSB = 1 / 256 degree Celsius */
225 val
->intval
= val
->intval
* 10 / 256;
227 case POWER_SUPPLY_PROP_CURRENT_NOW
:
228 if (chip
->pdata
->enable_current_sense
) {
229 ret
= max17042_read_reg(chip
->client
, MAX17042_Current
);
234 if (val
->intval
& 0x8000) {
236 val
->intval
= ~val
->intval
& 0x7fff;
240 val
->intval
*= 1562500 / chip
->pdata
->r_sns
;
245 case POWER_SUPPLY_PROP_CURRENT_AVG
:
246 if (chip
->pdata
->enable_current_sense
) {
247 ret
= max17042_read_reg(chip
->client
,
248 MAX17042_AvgCurrent
);
253 if (val
->intval
& 0x8000) {
255 val
->intval
= ~val
->intval
& 0x7fff;
259 val
->intval
*= 1562500 / chip
->pdata
->r_sns
;
270 static int max17042_write_verify_reg(struct i2c_client
*client
,
278 ret
= i2c_smbus_write_word_data(client
, reg
, value
);
279 read_value
= max17042_read_reg(client
, reg
);
280 if (read_value
!= value
) {
284 } while (retries
&& read_value
!= value
);
287 dev_err(&client
->dev
, "%s: err %d\n", __func__
, ret
);
292 static inline void max17042_override_por(
293 struct i2c_client
*client
, u8 reg
, u16 value
)
296 max17042_write_reg(client
, reg
, value
);
299 static inline void max10742_unlock_model(struct max17042_chip
*chip
)
301 struct i2c_client
*client
= chip
->client
;
302 max17042_write_reg(client
, MAX17042_MLOCKReg1
, MODEL_UNLOCK1
);
303 max17042_write_reg(client
, MAX17042_MLOCKReg2
, MODEL_UNLOCK2
);
306 static inline void max10742_lock_model(struct max17042_chip
*chip
)
308 struct i2c_client
*client
= chip
->client
;
309 max17042_write_reg(client
, MAX17042_MLOCKReg1
, MODEL_LOCK1
);
310 max17042_write_reg(client
, MAX17042_MLOCKReg2
, MODEL_LOCK2
);
313 static inline void max17042_write_model_data(struct max17042_chip
*chip
,
316 struct i2c_client
*client
= chip
->client
;
318 for (i
= 0; i
< size
; i
++)
319 max17042_write_reg(client
, addr
+ i
,
320 chip
->pdata
->config_data
->cell_char_tbl
[i
]);
323 static inline void max17042_read_model_data(struct max17042_chip
*chip
,
324 u8 addr
, u16
*data
, int size
)
326 struct i2c_client
*client
= chip
->client
;
329 for (i
= 0; i
< size
; i
++)
330 data
[i
] = max17042_read_reg(client
, addr
+ i
);
333 static inline int max17042_model_data_compare(struct max17042_chip
*chip
,
334 u16
*data1
, u16
*data2
, int size
)
338 if (memcmp(data1
, data2
, size
)) {
339 dev_err(&chip
->client
->dev
, "%s compare failed\n", __func__
);
340 for (i
= 0; i
< size
; i
++)
341 dev_info(&chip
->client
->dev
, "0x%x, 0x%x",
343 dev_info(&chip
->client
->dev
, "\n");
349 static int max17042_init_model(struct max17042_chip
*chip
)
352 int table_size
= ARRAY_SIZE(chip
->pdata
->config_data
->cell_char_tbl
);
355 temp_data
= kcalloc(table_size
, sizeof(*temp_data
), GFP_KERNEL
);
359 max10742_unlock_model(chip
);
360 max17042_write_model_data(chip
, MAX17042_MODELChrTbl
,
362 max17042_read_model_data(chip
, MAX17042_MODELChrTbl
, temp_data
,
365 ret
= max17042_model_data_compare(
367 chip
->pdata
->config_data
->cell_char_tbl
,
371 max10742_lock_model(chip
);
377 static int max17042_verify_model_lock(struct max17042_chip
*chip
)
380 int table_size
= ARRAY_SIZE(chip
->pdata
->config_data
->cell_char_tbl
);
384 temp_data
= kcalloc(table_size
, sizeof(*temp_data
), GFP_KERNEL
);
388 max17042_read_model_data(chip
, MAX17042_MODELChrTbl
, temp_data
,
390 for (i
= 0; i
< table_size
; i
++)
398 static void max17042_write_config_regs(struct max17042_chip
*chip
)
400 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
402 max17042_write_reg(chip
->client
, MAX17042_CONFIG
, config
->config
);
403 max17042_write_reg(chip
->client
, MAX17042_LearnCFG
, config
->learn_cfg
);
404 max17042_write_reg(chip
->client
, MAX17042_FilterCFG
,
406 max17042_write_reg(chip
->client
, MAX17042_RelaxCFG
, config
->relax_cfg
);
407 if (chip
->chip_type
== MAX17047
)
408 max17042_write_reg(chip
->client
, MAX17047_FullSOCThr
,
409 config
->full_soc_thresh
);
412 static void max17042_write_custom_regs(struct max17042_chip
*chip
)
414 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
416 max17042_write_verify_reg(chip
->client
, MAX17042_RCOMP0
,
418 max17042_write_verify_reg(chip
->client
, MAX17042_TempCo
,
420 max17042_write_verify_reg(chip
->client
, MAX17042_ICHGTerm
,
422 if (chip
->chip_type
== MAX17042
) {
423 max17042_write_reg(chip
->client
, MAX17042_EmptyTempCo
,
424 config
->empty_tempco
);
425 max17042_write_verify_reg(chip
->client
, MAX17042_K_empty0
,
428 max17042_write_verify_reg(chip
->client
, MAX17047_QRTbl00
,
430 max17042_write_verify_reg(chip
->client
, MAX17047_QRTbl10
,
432 max17042_write_verify_reg(chip
->client
, MAX17047_QRTbl20
,
434 max17042_write_verify_reg(chip
->client
, MAX17047_QRTbl30
,
439 static void max17042_update_capacity_regs(struct max17042_chip
*chip
)
441 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
443 max17042_write_verify_reg(chip
->client
, MAX17042_FullCAP
,
445 max17042_write_reg(chip
->client
, MAX17042_DesignCap
,
447 max17042_write_verify_reg(chip
->client
, MAX17042_FullCAPNom
,
451 static void max17042_reset_vfsoc0_reg(struct max17042_chip
*chip
)
455 vfSoc
= max17042_read_reg(chip
->client
, MAX17042_VFSOC
);
456 max17042_write_reg(chip
->client
, MAX17042_VFSOC0Enable
, VFSOC0_UNLOCK
);
457 max17042_write_verify_reg(chip
->client
, MAX17042_VFSOC0
, vfSoc
);
458 max17042_write_reg(chip
->client
, MAX17042_VFSOC0Enable
, VFSOC0_LOCK
);
461 static void max17042_load_new_capacity_params(struct max17042_chip
*chip
)
463 u16 full_cap0
, rep_cap
, dq_acc
, vfSoc
;
466 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
468 full_cap0
= max17042_read_reg(chip
->client
, MAX17042_FullCAP0
);
469 vfSoc
= max17042_read_reg(chip
->client
, MAX17042_VFSOC
);
471 /* fg_vfSoc needs to shifted by 8 bits to get the
472 * perc in 1% accuracy, to get the right rem_cap multiply
473 * full_cap0, fg_vfSoc and devide by 100
475 rem_cap
= ((vfSoc
>> 8) * full_cap0
) / 100;
476 max17042_write_verify_reg(chip
->client
, MAX17042_RemCap
, (u16
)rem_cap
);
478 rep_cap
= (u16
)rem_cap
;
479 max17042_write_verify_reg(chip
->client
, MAX17042_RepCap
, rep_cap
);
481 /* Write dQ_acc to 200% of Capacity and dP_acc to 200% */
482 dq_acc
= config
->fullcap
/ dQ_ACC_DIV
;
483 max17042_write_verify_reg(chip
->client
, MAX17042_dQacc
, dq_acc
);
484 max17042_write_verify_reg(chip
->client
, MAX17042_dPacc
, dP_ACC_200
);
486 max17042_write_verify_reg(chip
->client
, MAX17042_FullCAP
,
488 max17042_write_reg(chip
->client
, MAX17042_DesignCap
,
490 max17042_write_verify_reg(chip
->client
, MAX17042_FullCAPNom
,
492 /* Update SOC register with new SOC */
493 max17042_write_reg(chip
->client
, MAX17042_RepSOC
, vfSoc
);
497 * Block write all the override values coming from platform data.
498 * This function MUST be called before the POR initialization proceedure
499 * specified by maxim.
501 static inline void max17042_override_por_values(struct max17042_chip
*chip
)
503 struct i2c_client
*client
= chip
->client
;
504 struct max17042_config_data
*config
= chip
->pdata
->config_data
;
506 max17042_override_por(client
, MAX17042_TGAIN
, config
->tgain
);
507 max17042_override_por(client
, MAx17042_TOFF
, config
->toff
);
508 max17042_override_por(client
, MAX17042_CGAIN
, config
->cgain
);
509 max17042_override_por(client
, MAX17042_COFF
, config
->coff
);
511 max17042_override_por(client
, MAX17042_VALRT_Th
, config
->valrt_thresh
);
512 max17042_override_por(client
, MAX17042_TALRT_Th
, config
->talrt_thresh
);
513 max17042_override_por(client
, MAX17042_SALRT_Th
,
514 config
->soc_alrt_thresh
);
515 max17042_override_por(client
, MAX17042_CONFIG
, config
->config
);
516 max17042_override_por(client
, MAX17042_SHDNTIMER
, config
->shdntimer
);
518 max17042_override_por(client
, MAX17042_DesignCap
, config
->design_cap
);
519 max17042_override_por(client
, MAX17042_ICHGTerm
, config
->ichgt_term
);
521 max17042_override_por(client
, MAX17042_AtRate
, config
->at_rate
);
522 max17042_override_por(client
, MAX17042_LearnCFG
, config
->learn_cfg
);
523 max17042_override_por(client
, MAX17042_FilterCFG
, config
->filter_cfg
);
524 max17042_override_por(client
, MAX17042_RelaxCFG
, config
->relax_cfg
);
525 max17042_override_por(client
, MAX17042_MiscCFG
, config
->misc_cfg
);
526 max17042_override_por(client
, MAX17042_MaskSOC
, config
->masksoc
);
528 max17042_override_por(client
, MAX17042_FullCAP
, config
->fullcap
);
529 max17042_override_por(client
, MAX17042_FullCAPNom
, config
->fullcapnom
);
530 if (chip
->chip_type
== MAX17042
)
531 max17042_override_por(client
, MAX17042_SOC_empty
,
533 max17042_override_por(client
, MAX17042_LAvg_empty
, config
->lavg_empty
);
534 max17042_override_por(client
, MAX17042_dQacc
, config
->dqacc
);
535 max17042_override_por(client
, MAX17042_dPacc
, config
->dpacc
);
537 if (chip
->chip_type
== MAX17042
)
538 max17042_override_por(client
, MAX17042_V_empty
, config
->vempty
);
540 max17042_override_por(client
, MAX17047_V_empty
, config
->vempty
);
541 max17042_override_por(client
, MAX17042_TempNom
, config
->temp_nom
);
542 max17042_override_por(client
, MAX17042_TempLim
, config
->temp_lim
);
543 max17042_override_por(client
, MAX17042_FCTC
, config
->fctc
);
544 max17042_override_por(client
, MAX17042_RCOMP0
, config
->rcomp0
);
545 max17042_override_por(client
, MAX17042_TempCo
, config
->tcompc0
);
546 if (chip
->chip_type
) {
547 max17042_override_por(client
, MAX17042_EmptyTempCo
,
548 config
->empty_tempco
);
549 max17042_override_por(client
, MAX17042_K_empty0
,
554 static int max17042_init_chip(struct max17042_chip
*chip
)
559 max17042_override_por_values(chip
);
560 /* After Power up, the MAX17042 requires 500mS in order
561 * to perform signal debouncing and initial SOC reporting
565 /* Initialize configaration */
566 max17042_write_config_regs(chip
);
568 /* write cell characterization data */
569 ret
= max17042_init_model(chip
);
571 dev_err(&chip
->client
->dev
, "%s init failed\n",
576 ret
= max17042_verify_model_lock(chip
);
578 dev_err(&chip
->client
->dev
, "%s lock verify failed\n",
582 /* write custom parameters */
583 max17042_write_custom_regs(chip
);
585 /* update capacity params */
586 max17042_update_capacity_regs(chip
);
588 /* delay must be atleast 350mS to allow VFSOC
589 * to be calculated from the new configuration
593 /* reset vfsoc0 reg */
594 max17042_reset_vfsoc0_reg(chip
);
596 /* load new capacity params */
597 max17042_load_new_capacity_params(chip
);
599 /* Init complete, Clear the POR bit */
600 val
= max17042_read_reg(chip
->client
, MAX17042_STATUS
);
601 max17042_write_reg(chip
->client
, MAX17042_STATUS
,
602 val
& (~STATUS_POR_BIT
));
606 static void max17042_set_soc_threshold(struct max17042_chip
*chip
, u16 off
)
610 /* program interrupt thesholds such that we should
611 * get interrupt for every 'off' perc change in the soc
613 soc
= max17042_read_reg(chip
->client
, MAX17042_RepSOC
) >> 8;
614 soc_tr
= (soc
+ off
) << 8;
615 soc_tr
|= (soc
- off
);
616 max17042_write_reg(chip
->client
, MAX17042_SALRT_Th
, soc_tr
);
619 static irqreturn_t
max17042_thread_handler(int id
, void *dev
)
621 struct max17042_chip
*chip
= dev
;
624 val
= max17042_read_reg(chip
->client
, MAX17042_STATUS
);
625 if ((val
& STATUS_INTR_SOCMIN_BIT
) ||
626 (val
& STATUS_INTR_SOCMAX_BIT
)) {
627 dev_info(&chip
->client
->dev
, "SOC threshold INTR\n");
628 max17042_set_soc_threshold(chip
, 1);
631 power_supply_changed(&chip
->battery
);
635 static void max17042_init_worker(struct work_struct
*work
)
637 struct max17042_chip
*chip
= container_of(work
,
638 struct max17042_chip
, work
);
641 /* Initialize registers according to values from the platform data */
642 if (chip
->pdata
->enable_por_init
&& chip
->pdata
->config_data
) {
643 ret
= max17042_init_chip(chip
);
648 chip
->init_complete
= 1;
652 static struct max17042_platform_data
*
653 max17042_get_pdata(struct device
*dev
)
655 struct device_node
*np
= dev
->of_node
;
657 struct max17042_platform_data
*pdata
;
660 return dev
->platform_data
;
662 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
667 * Require current sense resistor value to be specified for
668 * current-sense functionality to be enabled at all.
670 if (of_property_read_u32(np
, "maxim,rsns-microohm", &prop
) == 0) {
672 pdata
->enable_current_sense
= true;
678 static struct max17042_platform_data
*
679 max17042_get_pdata(struct device
*dev
)
681 return dev
->platform_data
;
685 static int max17042_probe(struct i2c_client
*client
,
686 const struct i2c_device_id
*id
)
688 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
689 struct max17042_chip
*chip
;
693 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_WORD_DATA
))
696 chip
= devm_kzalloc(&client
->dev
, sizeof(*chip
), GFP_KERNEL
);
700 chip
->client
= client
;
701 chip
->pdata
= max17042_get_pdata(&client
->dev
);
703 dev_err(&client
->dev
, "no platform data provided\n");
707 i2c_set_clientdata(client
, chip
);
709 ret
= max17042_read_reg(chip
->client
, MAX17042_DevName
);
710 if (ret
== MAX17042_IC_VERSION
) {
711 dev_dbg(&client
->dev
, "chip type max17042 detected\n");
712 chip
->chip_type
= MAX17042
;
713 } else if (ret
== MAX17047_IC_VERSION
) {
714 dev_dbg(&client
->dev
, "chip type max17047/50 detected\n");
715 chip
->chip_type
= MAX17047
;
717 dev_err(&client
->dev
, "device version mismatch: %x\n", ret
);
721 chip
->battery
.name
= "max170xx_battery";
722 chip
->battery
.type
= POWER_SUPPLY_TYPE_BATTERY
;
723 chip
->battery
.get_property
= max17042_get_property
;
724 chip
->battery
.properties
= max17042_battery_props
;
725 chip
->battery
.num_properties
= ARRAY_SIZE(max17042_battery_props
);
727 /* When current is not measured,
728 * CURRENT_NOW and CURRENT_AVG properties should be invisible. */
729 if (!chip
->pdata
->enable_current_sense
)
730 chip
->battery
.num_properties
-= 2;
732 if (chip
->pdata
->r_sns
== 0)
733 chip
->pdata
->r_sns
= MAX17042_DEFAULT_SNS_RESISTOR
;
735 if (chip
->pdata
->init_data
)
736 max17042_set_reg(client
, chip
->pdata
->init_data
,
737 chip
->pdata
->num_init_data
);
739 if (!chip
->pdata
->enable_current_sense
) {
740 max17042_write_reg(client
, MAX17042_CGAIN
, 0x0000);
741 max17042_write_reg(client
, MAX17042_MiscCFG
, 0x0003);
742 max17042_write_reg(client
, MAX17042_LearnCFG
, 0x0007);
745 ret
= power_supply_register(&client
->dev
, &chip
->battery
);
747 dev_err(&client
->dev
, "failed: power supply register\n");
752 ret
= request_threaded_irq(client
->irq
, NULL
,
753 max17042_thread_handler
,
754 IRQF_TRIGGER_FALLING
,
755 chip
->battery
.name
, chip
);
757 reg
= max17042_read_reg(client
, MAX17042_CONFIG
);
758 reg
|= CONFIG_ALRT_BIT_ENBL
;
759 max17042_write_reg(client
, MAX17042_CONFIG
, reg
);
760 max17042_set_soc_threshold(chip
, 1);
763 dev_err(&client
->dev
, "%s(): cannot get IRQ\n",
768 reg
= max17042_read_reg(chip
->client
, MAX17042_STATUS
);
769 if (reg
& STATUS_POR_BIT
) {
770 INIT_WORK(&chip
->work
, max17042_init_worker
);
771 schedule_work(&chip
->work
);
773 chip
->init_complete
= 1;
779 static int max17042_remove(struct i2c_client
*client
)
781 struct max17042_chip
*chip
= i2c_get_clientdata(client
);
784 free_irq(client
->irq
, chip
);
785 power_supply_unregister(&chip
->battery
);
790 static int max17042_suspend(struct device
*dev
)
792 struct max17042_chip
*chip
= dev_get_drvdata(dev
);
795 * disable the irq and enable irq_wake
796 * capability to the interrupt line.
798 if (chip
->client
->irq
) {
799 disable_irq(chip
->client
->irq
);
800 enable_irq_wake(chip
->client
->irq
);
806 static int max17042_resume(struct device
*dev
)
808 struct max17042_chip
*chip
= dev_get_drvdata(dev
);
810 if (chip
->client
->irq
) {
811 disable_irq_wake(chip
->client
->irq
);
812 enable_irq(chip
->client
->irq
);
813 /* re-program the SOC thresholds to 1% change */
814 max17042_set_soc_threshold(chip
, 1);
820 static const struct dev_pm_ops max17042_pm_ops
= {
821 .suspend
= max17042_suspend
,
822 .resume
= max17042_resume
,
825 #define MAX17042_PM_OPS (&max17042_pm_ops)
827 #define MAX17042_PM_OPS NULL
831 static const struct of_device_id max17042_dt_match
[] = {
832 { .compatible
= "maxim,max17042" },
833 { .compatible
= "maxim,max17047" },
834 { .compatible
= "maxim,max17050" },
837 MODULE_DEVICE_TABLE(of
, max17042_dt_match
);
840 static const struct i2c_device_id max17042_id
[] = {
846 MODULE_DEVICE_TABLE(i2c
, max17042_id
);
848 static struct i2c_driver max17042_i2c_driver
= {
851 .of_match_table
= of_match_ptr(max17042_dt_match
),
852 .pm
= MAX17042_PM_OPS
,
854 .probe
= max17042_probe
,
855 .remove
= max17042_remove
,
856 .id_table
= max17042_id
,
858 module_i2c_driver(max17042_i2c_driver
);
860 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
861 MODULE_DESCRIPTION("MAX17042 Fuel Gauge");
862 MODULE_LICENSE("GPL");