1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * TI BQ25890 charger driver
5 * Copyright (C) 2015 Intel Corporation
8 #include <linux/module.h>
10 #include <linux/power_supply.h>
11 #include <linux/regmap.h>
12 #include <linux/types.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/interrupt.h>
15 #include <linux/delay.h>
16 #include <linux/usb/phy.h>
18 #include <linux/acpi.h>
21 #define BQ25890_MANUFACTURER "Texas Instruments"
22 #define BQ25890_IRQ_PIN "bq25890_irq"
28 enum bq25890_chip_version
{
35 static const char *const bq25890_chip_name
[] = {
43 F_EN_HIZ
, F_EN_ILIM
, F_IILIM
, /* Reg00 */
44 F_BHOT
, F_BCOLD
, F_VINDPM_OFS
, /* Reg01 */
45 F_CONV_START
, F_CONV_RATE
, F_BOOSTF
, F_ICO_EN
,
46 F_HVDCP_EN
, F_MAXC_EN
, F_FORCE_DPM
, F_AUTO_DPDM_EN
, /* Reg02 */
47 F_BAT_LOAD_EN
, F_WD_RST
, F_OTG_CFG
, F_CHG_CFG
, F_SYSVMIN
,
48 F_MIN_VBAT_SEL
, /* Reg03 */
49 F_PUMPX_EN
, F_ICHG
, /* Reg04 */
50 F_IPRECHG
, F_ITERM
, /* Reg05 */
51 F_VREG
, F_BATLOWV
, F_VRECHG
, /* Reg06 */
52 F_TERM_EN
, F_STAT_DIS
, F_WD
, F_TMR_EN
, F_CHG_TMR
,
53 F_JEITA_ISET
, /* Reg07 */
54 F_BATCMP
, F_VCLAMP
, F_TREG
, /* Reg08 */
55 F_FORCE_ICO
, F_TMR2X_EN
, F_BATFET_DIS
, F_JEITA_VSET
,
56 F_BATFET_DLY
, F_BATFET_RST_EN
, F_PUMPX_UP
, F_PUMPX_DN
, /* Reg09 */
57 F_BOOSTV
, F_PFM_OTG_DIS
, F_BOOSTI
, /* Reg0A */
58 F_VBUS_STAT
, F_CHG_STAT
, F_PG_STAT
, F_SDP_STAT
, F_0B_RSVD
,
59 F_VSYS_STAT
, /* Reg0B */
60 F_WD_FAULT
, F_BOOST_FAULT
, F_CHG_FAULT
, F_BAT_FAULT
,
61 F_NTC_FAULT
, /* Reg0C */
62 F_FORCE_VINDPM
, F_VINDPM
, /* Reg0D */
63 F_THERM_STAT
, F_BATV
, /* Reg0E */
66 F_VBUS_GD
, F_VBUSV
, /* Reg11 */
68 F_VDPM_STAT
, F_IDPM_STAT
, F_IDPM_LIM
, /* Reg13 */
69 F_REG_RST
, F_ICO_OPTIMIZED
, F_PN
, F_TS_PROFILE
, F_DEV_REV
, /* Reg14 */
74 /* initial field values, converted to register values */
75 struct bq25890_init_data
{
76 u8 ichg
; /* charge current */
77 u8 vreg
; /* regulation voltage */
78 u8 iterm
; /* termination current */
79 u8 iprechg
; /* precharge current */
80 u8 sysvmin
; /* minimum system voltage limit */
81 u8 boostv
; /* boost regulation voltage */
82 u8 boosti
; /* boost current limit */
83 u8 boostf
; /* boost frequency */
84 u8 ilim_en
; /* enable ILIM pin */
85 u8 treg
; /* thermal regulation threshold */
86 u8 rbatcomp
; /* IBAT sense resistor value */
87 u8 vclamp
; /* IBAT compensation voltage limit */
90 struct bq25890_state
{
99 struct bq25890_device
{
100 struct i2c_client
*client
;
102 struct power_supply
*charger
;
104 struct usb_phy
*usb_phy
;
105 struct notifier_block usb_nb
;
106 struct work_struct usb_work
;
107 unsigned long usb_event
;
110 struct regmap_field
*rmap_fields
[F_MAX_FIELDS
];
112 enum bq25890_chip_version chip_version
;
113 struct bq25890_init_data init_data
;
114 struct bq25890_state state
;
116 struct mutex lock
; /* protect state data */
119 static const struct regmap_range bq25890_readonly_reg_ranges
[] = {
120 regmap_reg_range(0x0b, 0x0c),
121 regmap_reg_range(0x0e, 0x13),
124 static const struct regmap_access_table bq25890_writeable_regs
= {
125 .no_ranges
= bq25890_readonly_reg_ranges
,
126 .n_no_ranges
= ARRAY_SIZE(bq25890_readonly_reg_ranges
),
129 static const struct regmap_range bq25890_volatile_reg_ranges
[] = {
130 regmap_reg_range(0x00, 0x00),
131 regmap_reg_range(0x02, 0x02),
132 regmap_reg_range(0x09, 0x09),
133 regmap_reg_range(0x0b, 0x14),
136 static const struct regmap_access_table bq25890_volatile_regs
= {
137 .yes_ranges
= bq25890_volatile_reg_ranges
,
138 .n_yes_ranges
= ARRAY_SIZE(bq25890_volatile_reg_ranges
),
141 static const struct regmap_config bq25890_regmap_config
= {
145 .max_register
= 0x14,
146 .cache_type
= REGCACHE_RBTREE
,
148 .wr_table
= &bq25890_writeable_regs
,
149 .volatile_table
= &bq25890_volatile_regs
,
152 static const struct reg_field bq25890_reg_fields
[] = {
154 [F_EN_HIZ
] = REG_FIELD(0x00, 7, 7),
155 [F_EN_ILIM
] = REG_FIELD(0x00, 6, 6),
156 [F_IILIM
] = REG_FIELD(0x00, 0, 5),
158 [F_BHOT
] = REG_FIELD(0x01, 6, 7),
159 [F_BCOLD
] = REG_FIELD(0x01, 5, 5),
160 [F_VINDPM_OFS
] = REG_FIELD(0x01, 0, 4),
162 [F_CONV_START
] = REG_FIELD(0x02, 7, 7),
163 [F_CONV_RATE
] = REG_FIELD(0x02, 6, 6),
164 [F_BOOSTF
] = REG_FIELD(0x02, 5, 5),
165 [F_ICO_EN
] = REG_FIELD(0x02, 4, 4),
166 [F_HVDCP_EN
] = REG_FIELD(0x02, 3, 3), // reserved on BQ25896
167 [F_MAXC_EN
] = REG_FIELD(0x02, 2, 2), // reserved on BQ25896
168 [F_FORCE_DPM
] = REG_FIELD(0x02, 1, 1),
169 [F_AUTO_DPDM_EN
] = REG_FIELD(0x02, 0, 0),
171 [F_BAT_LOAD_EN
] = REG_FIELD(0x03, 7, 7),
172 [F_WD_RST
] = REG_FIELD(0x03, 6, 6),
173 [F_OTG_CFG
] = REG_FIELD(0x03, 5, 5),
174 [F_CHG_CFG
] = REG_FIELD(0x03, 4, 4),
175 [F_SYSVMIN
] = REG_FIELD(0x03, 1, 3),
176 [F_MIN_VBAT_SEL
] = REG_FIELD(0x03, 0, 0), // BQ25896 only
178 [F_PUMPX_EN
] = REG_FIELD(0x04, 7, 7),
179 [F_ICHG
] = REG_FIELD(0x04, 0, 6),
181 [F_IPRECHG
] = REG_FIELD(0x05, 4, 7),
182 [F_ITERM
] = REG_FIELD(0x05, 0, 3),
184 [F_VREG
] = REG_FIELD(0x06, 2, 7),
185 [F_BATLOWV
] = REG_FIELD(0x06, 1, 1),
186 [F_VRECHG
] = REG_FIELD(0x06, 0, 0),
188 [F_TERM_EN
] = REG_FIELD(0x07, 7, 7),
189 [F_STAT_DIS
] = REG_FIELD(0x07, 6, 6),
190 [F_WD
] = REG_FIELD(0x07, 4, 5),
191 [F_TMR_EN
] = REG_FIELD(0x07, 3, 3),
192 [F_CHG_TMR
] = REG_FIELD(0x07, 1, 2),
193 [F_JEITA_ISET
] = REG_FIELD(0x07, 0, 0), // reserved on BQ25895
195 [F_BATCMP
] = REG_FIELD(0x08, 5, 7),
196 [F_VCLAMP
] = REG_FIELD(0x08, 2, 4),
197 [F_TREG
] = REG_FIELD(0x08, 0, 1),
199 [F_FORCE_ICO
] = REG_FIELD(0x09, 7, 7),
200 [F_TMR2X_EN
] = REG_FIELD(0x09, 6, 6),
201 [F_BATFET_DIS
] = REG_FIELD(0x09, 5, 5),
202 [F_JEITA_VSET
] = REG_FIELD(0x09, 4, 4), // reserved on BQ25895
203 [F_BATFET_DLY
] = REG_FIELD(0x09, 3, 3),
204 [F_BATFET_RST_EN
] = REG_FIELD(0x09, 2, 2),
205 [F_PUMPX_UP
] = REG_FIELD(0x09, 1, 1),
206 [F_PUMPX_DN
] = REG_FIELD(0x09, 0, 0),
208 [F_BOOSTV
] = REG_FIELD(0x0A, 4, 7),
209 [F_BOOSTI
] = REG_FIELD(0x0A, 0, 2), // reserved on BQ25895
210 [F_PFM_OTG_DIS
] = REG_FIELD(0x0A, 3, 3), // BQ25896 only
212 [F_VBUS_STAT
] = REG_FIELD(0x0B, 5, 7),
213 [F_CHG_STAT
] = REG_FIELD(0x0B, 3, 4),
214 [F_PG_STAT
] = REG_FIELD(0x0B, 2, 2),
215 [F_SDP_STAT
] = REG_FIELD(0x0B, 1, 1), // reserved on BQ25896
216 [F_VSYS_STAT
] = REG_FIELD(0x0B, 0, 0),
218 [F_WD_FAULT
] = REG_FIELD(0x0C, 7, 7),
219 [F_BOOST_FAULT
] = REG_FIELD(0x0C, 6, 6),
220 [F_CHG_FAULT
] = REG_FIELD(0x0C, 4, 5),
221 [F_BAT_FAULT
] = REG_FIELD(0x0C, 3, 3),
222 [F_NTC_FAULT
] = REG_FIELD(0x0C, 0, 2),
224 [F_FORCE_VINDPM
] = REG_FIELD(0x0D, 7, 7),
225 [F_VINDPM
] = REG_FIELD(0x0D, 0, 6),
227 [F_THERM_STAT
] = REG_FIELD(0x0E, 7, 7),
228 [F_BATV
] = REG_FIELD(0x0E, 0, 6),
230 [F_SYSV
] = REG_FIELD(0x0F, 0, 6),
232 [F_TSPCT
] = REG_FIELD(0x10, 0, 6),
234 [F_VBUS_GD
] = REG_FIELD(0x11, 7, 7),
235 [F_VBUSV
] = REG_FIELD(0x11, 0, 6),
237 [F_ICHGR
] = REG_FIELD(0x12, 0, 6),
239 [F_VDPM_STAT
] = REG_FIELD(0x13, 7, 7),
240 [F_IDPM_STAT
] = REG_FIELD(0x13, 6, 6),
241 [F_IDPM_LIM
] = REG_FIELD(0x13, 0, 5),
243 [F_REG_RST
] = REG_FIELD(0x14, 7, 7),
244 [F_ICO_OPTIMIZED
] = REG_FIELD(0x14, 6, 6),
245 [F_PN
] = REG_FIELD(0x14, 3, 5),
246 [F_TS_PROFILE
] = REG_FIELD(0x14, 2, 2),
247 [F_DEV_REV
] = REG_FIELD(0x14, 0, 1)
251 * Most of the val -> idx conversions can be computed, given the minimum,
252 * maximum and the step between values. For the rest of conversions, we use
255 enum bq25890_table_ids
{
271 /* Thermal Regulation Threshold lookup table, in degrees Celsius */
272 static const u32 bq25890_treg_tbl
[] = { 60, 80, 100, 120 };
274 #define BQ25890_TREG_TBL_SIZE ARRAY_SIZE(bq25890_treg_tbl)
276 /* Boost mode current limit lookup table, in uA */
277 static const u32 bq25890_boosti_tbl
[] = {
278 500000, 700000, 1100000, 1300000, 1600000, 1800000, 2100000, 2400000
281 #define BQ25890_BOOSTI_TBL_SIZE ARRAY_SIZE(bq25890_boosti_tbl)
283 struct bq25890_range
{
289 struct bq25890_lookup
{
295 struct bq25890_range rt
;
296 struct bq25890_lookup lt
;
297 } bq25890_tables
[] = {
299 /* TODO: BQ25896 has max ICHG 3008 mA */
300 [TBL_ICHG
] = { .rt
= {0, 5056000, 64000} }, /* uA */
301 [TBL_ITERM
] = { .rt
= {64000, 1024000, 64000} }, /* uA */
302 [TBL_IILIM
] = { .rt
= {100000, 3250000, 50000} }, /* uA */
303 [TBL_VREG
] = { .rt
= {3840000, 4608000, 16000} }, /* uV */
304 [TBL_BOOSTV
] = { .rt
= {4550000, 5510000, 64000} }, /* uV */
305 [TBL_SYSVMIN
] = { .rt
= {3000000, 3700000, 100000} }, /* uV */
306 [TBL_VBATCOMP
] ={ .rt
= {0, 224000, 32000} }, /* uV */
307 [TBL_RBATCOMP
] ={ .rt
= {0, 140000, 20000} }, /* uOhm */
310 [TBL_TREG
] = { .lt
= {bq25890_treg_tbl
, BQ25890_TREG_TBL_SIZE
} },
311 [TBL_BOOSTI
] = { .lt
= {bq25890_boosti_tbl
, BQ25890_BOOSTI_TBL_SIZE
} }
314 static int bq25890_field_read(struct bq25890_device
*bq
,
315 enum bq25890_fields field_id
)
320 ret
= regmap_field_read(bq
->rmap_fields
[field_id
], &val
);
327 static int bq25890_field_write(struct bq25890_device
*bq
,
328 enum bq25890_fields field_id
, u8 val
)
330 return regmap_field_write(bq
->rmap_fields
[field_id
], val
);
333 static u8
bq25890_find_idx(u32 value
, enum bq25890_table_ids id
)
337 if (id
>= TBL_TREG
) {
338 const u32
*tbl
= bq25890_tables
[id
].lt
.tbl
;
339 u32 tbl_size
= bq25890_tables
[id
].lt
.size
;
341 for (idx
= 1; idx
< tbl_size
&& tbl
[idx
] <= value
; idx
++)
344 const struct bq25890_range
*rtbl
= &bq25890_tables
[id
].rt
;
347 rtbl_size
= (rtbl
->max
- rtbl
->min
) / rtbl
->step
+ 1;
350 idx
< rtbl_size
&& (idx
* rtbl
->step
+ rtbl
->min
<= value
);
358 static u32
bq25890_find_val(u8 idx
, enum bq25890_table_ids id
)
360 const struct bq25890_range
*rtbl
;
364 return bq25890_tables
[id
].lt
.tbl
[idx
];
367 rtbl
= &bq25890_tables
[id
].rt
;
369 return (rtbl
->min
+ idx
* rtbl
->step
);
372 enum bq25890_status
{
375 STATUS_FAST_CHARGING
,
376 STATUS_TERMINATION_DONE
,
379 enum bq25890_chrg_fault
{
382 CHRG_FAULT_THERMAL_SHUTDOWN
,
383 CHRG_FAULT_TIMER_EXPIRED
,
386 static bool bq25890_is_adc_property(enum power_supply_property psp
)
389 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
390 case POWER_SUPPLY_PROP_CURRENT_NOW
:
398 static irqreturn_t
__bq25890_handle_irq(struct bq25890_device
*bq
);
400 static int bq25890_power_supply_get_property(struct power_supply
*psy
,
401 enum power_supply_property psp
,
402 union power_supply_propval
*val
)
404 struct bq25890_device
*bq
= power_supply_get_drvdata(psy
);
405 struct bq25890_state state
;
409 mutex_lock(&bq
->lock
);
410 /* update state in case we lost an interrupt */
411 __bq25890_handle_irq(bq
);
413 do_adc_conv
= !state
.online
&& bq25890_is_adc_property(psp
);
415 bq25890_field_write(bq
, F_CONV_START
, 1);
416 mutex_unlock(&bq
->lock
);
419 regmap_field_read_poll_timeout(bq
->rmap_fields
[F_CONV_START
],
420 ret
, !ret
, 25000, 1000000);
423 case POWER_SUPPLY_PROP_STATUS
:
425 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
426 else if (state
.chrg_status
== STATUS_NOT_CHARGING
)
427 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
428 else if (state
.chrg_status
== STATUS_PRE_CHARGING
||
429 state
.chrg_status
== STATUS_FAST_CHARGING
)
430 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
431 else if (state
.chrg_status
== STATUS_TERMINATION_DONE
)
432 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
434 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
438 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
439 if (!state
.online
|| state
.chrg_status
== STATUS_NOT_CHARGING
||
440 state
.chrg_status
== STATUS_TERMINATION_DONE
)
441 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_NONE
;
442 else if (state
.chrg_status
== STATUS_PRE_CHARGING
)
443 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_STANDARD
;
444 else if (state
.chrg_status
== STATUS_FAST_CHARGING
)
445 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_FAST
;
446 else /* unreachable */
447 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_UNKNOWN
;
450 case POWER_SUPPLY_PROP_MANUFACTURER
:
451 val
->strval
= BQ25890_MANUFACTURER
;
454 case POWER_SUPPLY_PROP_MODEL_NAME
:
455 val
->strval
= bq25890_chip_name
[bq
->chip_version
];
458 case POWER_SUPPLY_PROP_ONLINE
:
459 val
->intval
= state
.online
;
462 case POWER_SUPPLY_PROP_HEALTH
:
463 if (!state
.chrg_fault
&& !state
.bat_fault
&& !state
.boost_fault
)
464 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
465 else if (state
.bat_fault
)
466 val
->intval
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
467 else if (state
.chrg_fault
== CHRG_FAULT_TIMER_EXPIRED
)
468 val
->intval
= POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE
;
469 else if (state
.chrg_fault
== CHRG_FAULT_THERMAL_SHUTDOWN
)
470 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
472 val
->intval
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
475 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
:
476 val
->intval
= bq25890_find_val(bq
->init_data
.ichg
, TBL_ICHG
);
479 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
485 ret
= bq25890_field_read(bq
, F_BATV
); /* read measured value */
489 /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
490 val
->intval
= 2304000 + ret
* 20000;
493 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
:
494 val
->intval
= bq25890_find_val(bq
->init_data
.vreg
, TBL_VREG
);
497 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT
:
498 val
->intval
= bq25890_find_val(bq
->init_data
.iprechg
, TBL_ITERM
);
501 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT
:
502 val
->intval
= bq25890_find_val(bq
->init_data
.iterm
, TBL_ITERM
);
505 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
506 ret
= bq25890_field_read(bq
, F_IILIM
);
510 val
->intval
= bq25890_find_val(ret
, TBL_IILIM
);
513 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
514 ret
= bq25890_field_read(bq
, F_SYSV
); /* read measured value */
518 /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
519 val
->intval
= 2304000 + ret
* 20000;
522 case POWER_SUPPLY_PROP_CURRENT_NOW
:
523 ret
= bq25890_field_read(bq
, F_ICHGR
); /* read measured value */
527 /* converted_val = ADC_val * 50mA (table 10.3.19) */
528 val
->intval
= ret
* -50000;
538 static int bq25890_get_chip_state(struct bq25890_device
*bq
,
539 struct bq25890_state
*state
)
544 enum bq25890_fields id
;
547 {F_CHG_STAT
, &state
->chrg_status
},
548 {F_PG_STAT
, &state
->online
},
549 {F_VSYS_STAT
, &state
->vsys_status
},
550 {F_BOOST_FAULT
, &state
->boost_fault
},
551 {F_BAT_FAULT
, &state
->bat_fault
},
552 {F_CHG_FAULT
, &state
->chrg_fault
}
555 for (i
= 0; i
< ARRAY_SIZE(state_fields
); i
++) {
556 ret
= bq25890_field_read(bq
, state_fields
[i
].id
);
560 *state_fields
[i
].data
= ret
;
563 dev_dbg(bq
->dev
, "S:CHG/PG/VSYS=%d/%d/%d, F:CHG/BOOST/BAT=%d/%d/%d\n",
564 state
->chrg_status
, state
->online
, state
->vsys_status
,
565 state
->chrg_fault
, state
->boost_fault
, state
->bat_fault
);
570 static irqreturn_t
__bq25890_handle_irq(struct bq25890_device
*bq
)
572 struct bq25890_state new_state
;
575 ret
= bq25890_get_chip_state(bq
, &new_state
);
579 if (!memcmp(&bq
->state
, &new_state
, sizeof(new_state
)))
582 if (!new_state
.online
&& bq
->state
.online
) { /* power removed */
584 ret
= bq25890_field_write(bq
, F_CONV_START
, 0);
587 } else if (new_state
.online
&& !bq
->state
.online
) { /* power inserted */
588 /* enable ADC, to have control of charge current/voltage */
589 ret
= bq25890_field_write(bq
, F_CONV_START
, 1);
594 bq
->state
= new_state
;
595 power_supply_changed(bq
->charger
);
599 dev_err(bq
->dev
, "Error communicating with the chip: %pe\n",
604 static irqreturn_t
bq25890_irq_handler_thread(int irq
, void *private)
606 struct bq25890_device
*bq
= private;
609 mutex_lock(&bq
->lock
);
610 ret
= __bq25890_handle_irq(bq
);
611 mutex_unlock(&bq
->lock
);
616 static int bq25890_chip_reset(struct bq25890_device
*bq
)
619 int rst_check_counter
= 10;
621 ret
= bq25890_field_write(bq
, F_REG_RST
, 1);
626 ret
= bq25890_field_read(bq
, F_REG_RST
);
631 } while (ret
== 1 && --rst_check_counter
);
633 if (!rst_check_counter
)
639 static int bq25890_hw_init(struct bq25890_device
*bq
)
645 enum bq25890_fields id
;
648 {F_ICHG
, bq
->init_data
.ichg
},
649 {F_VREG
, bq
->init_data
.vreg
},
650 {F_ITERM
, bq
->init_data
.iterm
},
651 {F_IPRECHG
, bq
->init_data
.iprechg
},
652 {F_SYSVMIN
, bq
->init_data
.sysvmin
},
653 {F_BOOSTV
, bq
->init_data
.boostv
},
654 {F_BOOSTI
, bq
->init_data
.boosti
},
655 {F_BOOSTF
, bq
->init_data
.boostf
},
656 {F_EN_ILIM
, bq
->init_data
.ilim_en
},
657 {F_TREG
, bq
->init_data
.treg
},
658 {F_BATCMP
, bq
->init_data
.rbatcomp
},
659 {F_VCLAMP
, bq
->init_data
.vclamp
},
662 ret
= bq25890_chip_reset(bq
);
664 dev_dbg(bq
->dev
, "Reset failed %d\n", ret
);
668 /* disable watchdog */
669 ret
= bq25890_field_write(bq
, F_WD
, 0);
671 dev_dbg(bq
->dev
, "Disabling watchdog failed %d\n", ret
);
675 /* initialize currents/voltages and other parameters */
676 for (i
= 0; i
< ARRAY_SIZE(init_data
); i
++) {
677 ret
= bq25890_field_write(bq
, init_data
[i
].id
,
680 dev_dbg(bq
->dev
, "Writing init data failed %d\n", ret
);
685 /* Configure ADC for continuous conversions when charging */
686 ret
= bq25890_field_write(bq
, F_CONV_RATE
, !!bq
->state
.online
);
688 dev_dbg(bq
->dev
, "Config ADC failed %d\n", ret
);
692 ret
= bq25890_get_chip_state(bq
, &bq
->state
);
694 dev_dbg(bq
->dev
, "Get state failed %d\n", ret
);
701 static const enum power_supply_property bq25890_power_supply_props
[] = {
702 POWER_SUPPLY_PROP_MANUFACTURER
,
703 POWER_SUPPLY_PROP_MODEL_NAME
,
704 POWER_SUPPLY_PROP_STATUS
,
705 POWER_SUPPLY_PROP_CHARGE_TYPE
,
706 POWER_SUPPLY_PROP_ONLINE
,
707 POWER_SUPPLY_PROP_HEALTH
,
708 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
,
709 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
,
710 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
,
711 POWER_SUPPLY_PROP_PRECHARGE_CURRENT
,
712 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT
,
713 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
,
714 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
715 POWER_SUPPLY_PROP_CURRENT_NOW
,
718 static char *bq25890_charger_supplied_to
[] = {
722 static const struct power_supply_desc bq25890_power_supply_desc
= {
723 .name
= "bq25890-charger",
724 .type
= POWER_SUPPLY_TYPE_USB
,
725 .properties
= bq25890_power_supply_props
,
726 .num_properties
= ARRAY_SIZE(bq25890_power_supply_props
),
727 .get_property
= bq25890_power_supply_get_property
,
730 static int bq25890_power_supply_init(struct bq25890_device
*bq
)
732 struct power_supply_config psy_cfg
= { .drv_data
= bq
, };
734 psy_cfg
.supplied_to
= bq25890_charger_supplied_to
;
735 psy_cfg
.num_supplicants
= ARRAY_SIZE(bq25890_charger_supplied_to
);
737 bq
->charger
= power_supply_register(bq
->dev
, &bq25890_power_supply_desc
,
740 return PTR_ERR_OR_ZERO(bq
->charger
);
743 static void bq25890_usb_work(struct work_struct
*data
)
746 struct bq25890_device
*bq
=
747 container_of(data
, struct bq25890_device
, usb_work
);
749 switch (bq
->usb_event
) {
751 /* Enable boost mode */
752 ret
= bq25890_field_write(bq
, F_OTG_CFG
, 1);
758 /* Disable boost mode */
759 ret
= bq25890_field_write(bq
, F_OTG_CFG
, 0);
763 power_supply_changed(bq
->charger
);
770 dev_err(bq
->dev
, "Error switching to boost/charger mode.\n");
773 static int bq25890_usb_notifier(struct notifier_block
*nb
, unsigned long val
,
776 struct bq25890_device
*bq
=
777 container_of(nb
, struct bq25890_device
, usb_nb
);
780 queue_work(system_power_efficient_wq
, &bq
->usb_work
);
785 static int bq25890_get_chip_version(struct bq25890_device
*bq
)
789 id
= bq25890_field_read(bq
, F_PN
);
791 dev_err(bq
->dev
, "Cannot read chip ID.\n");
795 rev
= bq25890_field_read(bq
, F_DEV_REV
);
797 dev_err(bq
->dev
, "Cannot read chip revision.\n");
803 bq
->chip_version
= BQ25890
;
806 /* BQ25892 and BQ25896 share same ID 0 */
810 bq
->chip_version
= BQ25896
;
813 bq
->chip_version
= BQ25892
;
817 "Unknown device revision %d, assume BQ25892\n",
819 bq
->chip_version
= BQ25892
;
824 bq
->chip_version
= BQ25895
;
828 dev_err(bq
->dev
, "Unknown chip ID %d\n", id
);
835 static int bq25890_irq_probe(struct bq25890_device
*bq
)
837 struct gpio_desc
*irq
;
839 irq
= devm_gpiod_get(bq
->dev
, BQ25890_IRQ_PIN
, GPIOD_IN
);
841 dev_err(bq
->dev
, "Could not probe irq pin.\n");
845 return gpiod_to_irq(irq
);
848 static int bq25890_fw_read_u32_props(struct bq25890_device
*bq
)
853 struct bq25890_init_data
*init
= &bq
->init_data
;
857 enum bq25890_table_ids tbl_id
;
858 u8
*conv_data
; /* holds converted value from given property */
860 /* required properties */
861 {"ti,charge-current", false, TBL_ICHG
, &init
->ichg
},
862 {"ti,battery-regulation-voltage", false, TBL_VREG
, &init
->vreg
},
863 {"ti,termination-current", false, TBL_ITERM
, &init
->iterm
},
864 {"ti,precharge-current", false, TBL_ITERM
, &init
->iprechg
},
865 {"ti,minimum-sys-voltage", false, TBL_SYSVMIN
, &init
->sysvmin
},
866 {"ti,boost-voltage", false, TBL_BOOSTV
, &init
->boostv
},
867 {"ti,boost-max-current", false, TBL_BOOSTI
, &init
->boosti
},
869 /* optional properties */
870 {"ti,thermal-regulation-threshold", true, TBL_TREG
, &init
->treg
},
871 {"ti,ibatcomp-micro-ohms", true, TBL_RBATCOMP
, &init
->rbatcomp
},
872 {"ti,ibatcomp-clamp-microvolt", true, TBL_VBATCOMP
, &init
->vclamp
},
875 /* initialize data for optional properties */
876 init
->treg
= 3; /* 120 degrees Celsius */
877 init
->rbatcomp
= init
->vclamp
= 0; /* IBAT compensation disabled */
879 for (i
= 0; i
< ARRAY_SIZE(props
); i
++) {
880 ret
= device_property_read_u32(bq
->dev
, props
[i
].name
,
883 if (props
[i
].optional
)
886 dev_err(bq
->dev
, "Unable to read property %d %s\n", ret
,
892 *props
[i
].conv_data
= bq25890_find_idx(property
,
899 static int bq25890_fw_probe(struct bq25890_device
*bq
)
902 struct bq25890_init_data
*init
= &bq
->init_data
;
904 ret
= bq25890_fw_read_u32_props(bq
);
908 init
->ilim_en
= device_property_read_bool(bq
->dev
, "ti,use-ilim-pin");
909 init
->boostf
= device_property_read_bool(bq
->dev
, "ti,boost-low-freq");
914 static int bq25890_probe(struct i2c_client
*client
,
915 const struct i2c_device_id
*id
)
917 struct device
*dev
= &client
->dev
;
918 struct bq25890_device
*bq
;
922 bq
= devm_kzalloc(dev
, sizeof(*bq
), GFP_KERNEL
);
929 mutex_init(&bq
->lock
);
931 bq
->rmap
= devm_regmap_init_i2c(client
, &bq25890_regmap_config
);
932 if (IS_ERR(bq
->rmap
)) {
933 dev_err(dev
, "failed to allocate register map\n");
934 return PTR_ERR(bq
->rmap
);
937 for (i
= 0; i
< ARRAY_SIZE(bq25890_reg_fields
); i
++) {
938 const struct reg_field
*reg_fields
= bq25890_reg_fields
;
940 bq
->rmap_fields
[i
] = devm_regmap_field_alloc(dev
, bq
->rmap
,
942 if (IS_ERR(bq
->rmap_fields
[i
])) {
943 dev_err(dev
, "cannot allocate regmap field\n");
944 return PTR_ERR(bq
->rmap_fields
[i
]);
948 i2c_set_clientdata(client
, bq
);
950 ret
= bq25890_get_chip_version(bq
);
952 dev_err(dev
, "Cannot read chip ID or unknown chip.\n");
956 if (!dev
->platform_data
) {
957 ret
= bq25890_fw_probe(bq
);
959 dev_err(dev
, "Cannot read device properties.\n");
966 ret
= bq25890_hw_init(bq
);
968 dev_err(dev
, "Cannot initialize the chip.\n");
972 if (client
->irq
<= 0)
973 client
->irq
= bq25890_irq_probe(bq
);
975 if (client
->irq
< 0) {
976 dev_err(dev
, "No irq resource found.\n");
981 bq
->usb_phy
= devm_usb_get_phy(dev
, USB_PHY_TYPE_USB2
);
982 if (!IS_ERR_OR_NULL(bq
->usb_phy
)) {
983 INIT_WORK(&bq
->usb_work
, bq25890_usb_work
);
984 bq
->usb_nb
.notifier_call
= bq25890_usb_notifier
;
985 usb_register_notifier(bq
->usb_phy
, &bq
->usb_nb
);
988 ret
= devm_request_threaded_irq(dev
, client
->irq
, NULL
,
989 bq25890_irq_handler_thread
,
990 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
991 BQ25890_IRQ_PIN
, bq
);
995 ret
= bq25890_power_supply_init(bq
);
997 dev_err(dev
, "Failed to register power supply\n");
1004 if (!IS_ERR_OR_NULL(bq
->usb_phy
))
1005 usb_unregister_notifier(bq
->usb_phy
, &bq
->usb_nb
);
1010 static int bq25890_remove(struct i2c_client
*client
)
1012 struct bq25890_device
*bq
= i2c_get_clientdata(client
);
1014 power_supply_unregister(bq
->charger
);
1016 if (!IS_ERR_OR_NULL(bq
->usb_phy
))
1017 usb_unregister_notifier(bq
->usb_phy
, &bq
->usb_nb
);
1019 /* reset all registers to default values */
1020 bq25890_chip_reset(bq
);
1025 #ifdef CONFIG_PM_SLEEP
1026 static int bq25890_suspend(struct device
*dev
)
1028 struct bq25890_device
*bq
= dev_get_drvdata(dev
);
1031 * If charger is removed, while in suspend, make sure ADC is diabled
1032 * since it consumes slightly more power.
1034 return bq25890_field_write(bq
, F_CONV_RATE
, 0);
1037 static int bq25890_resume(struct device
*dev
)
1040 struct bq25890_device
*bq
= dev_get_drvdata(dev
);
1042 mutex_lock(&bq
->lock
);
1044 ret
= bq25890_get_chip_state(bq
, &bq
->state
);
1048 /* Re-enable ADC only if charger is plugged in. */
1049 if (bq
->state
.online
) {
1050 ret
= bq25890_field_write(bq
, F_CONV_RATE
, 1);
1055 /* signal userspace, maybe state changed while suspended */
1056 power_supply_changed(bq
->charger
);
1059 mutex_unlock(&bq
->lock
);
1065 static const struct dev_pm_ops bq25890_pm
= {
1066 SET_SYSTEM_SLEEP_PM_OPS(bq25890_suspend
, bq25890_resume
)
1069 static const struct i2c_device_id bq25890_i2c_ids
[] = {
1076 MODULE_DEVICE_TABLE(i2c
, bq25890_i2c_ids
);
1078 static const struct of_device_id bq25890_of_match
[] = {
1079 { .compatible
= "ti,bq25890", },
1080 { .compatible
= "ti,bq25892", },
1081 { .compatible
= "ti,bq25895", },
1082 { .compatible
= "ti,bq25896", },
1085 MODULE_DEVICE_TABLE(of
, bq25890_of_match
);
1088 static const struct acpi_device_id bq25890_acpi_match
[] = {
1092 MODULE_DEVICE_TABLE(acpi
, bq25890_acpi_match
);
1095 static struct i2c_driver bq25890_driver
= {
1097 .name
= "bq25890-charger",
1098 .of_match_table
= of_match_ptr(bq25890_of_match
),
1099 .acpi_match_table
= ACPI_PTR(bq25890_acpi_match
),
1102 .probe
= bq25890_probe
,
1103 .remove
= bq25890_remove
,
1104 .id_table
= bq25890_i2c_ids
,
1106 module_i2c_driver(bq25890_driver
);
1108 MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
1109 MODULE_DESCRIPTION("bq25890 charger driver");
1110 MODULE_LICENSE("GPL");