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"
29 F_EN_HIZ
, F_EN_ILIM
, F_IILIM
, /* Reg00 */
30 F_BHOT
, F_BCOLD
, F_VINDPM_OFS
, /* Reg01 */
31 F_CONV_START
, F_CONV_RATE
, F_BOOSTF
, F_ICO_EN
,
32 F_HVDCP_EN
, F_MAXC_EN
, F_FORCE_DPM
, F_AUTO_DPDM_EN
, /* Reg02 */
33 F_BAT_LOAD_EN
, F_WD_RST
, F_OTG_CFG
, F_CHG_CFG
, F_SYSVMIN
, /* Reg03 */
34 F_PUMPX_EN
, F_ICHG
, /* Reg04 */
35 F_IPRECHG
, F_ITERM
, /* Reg05 */
36 F_VREG
, F_BATLOWV
, F_VRECHG
, /* Reg06 */
37 F_TERM_EN
, F_STAT_DIS
, F_WD
, F_TMR_EN
, F_CHG_TMR
,
38 F_JEITA_ISET
, /* Reg07 */
39 F_BATCMP
, F_VCLAMP
, F_TREG
, /* Reg08 */
40 F_FORCE_ICO
, F_TMR2X_EN
, F_BATFET_DIS
, F_JEITA_VSET
,
41 F_BATFET_DLY
, F_BATFET_RST_EN
, F_PUMPX_UP
, F_PUMPX_DN
, /* Reg09 */
42 F_BOOSTV
, F_BOOSTI
, /* Reg0A */
43 F_VBUS_STAT
, F_CHG_STAT
, F_PG_STAT
, F_SDP_STAT
, F_VSYS_STAT
, /* Reg0B */
44 F_WD_FAULT
, F_BOOST_FAULT
, F_CHG_FAULT
, F_BAT_FAULT
,
45 F_NTC_FAULT
, /* Reg0C */
46 F_FORCE_VINDPM
, F_VINDPM
, /* Reg0D */
47 F_THERM_STAT
, F_BATV
, /* Reg0E */
50 F_VBUS_GD
, F_VBUSV
, /* Reg11 */
52 F_VDPM_STAT
, F_IDPM_STAT
, F_IDPM_LIM
, /* Reg13 */
53 F_REG_RST
, F_ICO_OPTIMIZED
, F_PN
, F_TS_PROFILE
, F_DEV_REV
, /* Reg14 */
58 /* initial field values, converted to register values */
59 struct bq25890_init_data
{
60 u8 ichg
; /* charge current */
61 u8 vreg
; /* regulation voltage */
62 u8 iterm
; /* termination current */
63 u8 iprechg
; /* precharge current */
64 u8 sysvmin
; /* minimum system voltage limit */
65 u8 boostv
; /* boost regulation voltage */
66 u8 boosti
; /* boost current limit */
67 u8 boostf
; /* boost frequency */
68 u8 ilim_en
; /* enable ILIM pin */
69 u8 treg
; /* thermal regulation threshold */
72 struct bq25890_state
{
81 struct bq25890_device
{
82 struct i2c_client
*client
;
84 struct power_supply
*charger
;
86 struct usb_phy
*usb_phy
;
87 struct notifier_block usb_nb
;
88 struct work_struct usb_work
;
89 unsigned long usb_event
;
92 struct regmap_field
*rmap_fields
[F_MAX_FIELDS
];
95 struct bq25890_init_data init_data
;
96 struct bq25890_state state
;
98 struct mutex lock
; /* protect state data */
101 static const struct regmap_range bq25890_readonly_reg_ranges
[] = {
102 regmap_reg_range(0x0b, 0x0c),
103 regmap_reg_range(0x0e, 0x13),
106 static const struct regmap_access_table bq25890_writeable_regs
= {
107 .no_ranges
= bq25890_readonly_reg_ranges
,
108 .n_no_ranges
= ARRAY_SIZE(bq25890_readonly_reg_ranges
),
111 static const struct regmap_range bq25890_volatile_reg_ranges
[] = {
112 regmap_reg_range(0x00, 0x00),
113 regmap_reg_range(0x09, 0x09),
114 regmap_reg_range(0x0b, 0x0c),
115 regmap_reg_range(0x0e, 0x14),
118 static const struct regmap_access_table bq25890_volatile_regs
= {
119 .yes_ranges
= bq25890_volatile_reg_ranges
,
120 .n_yes_ranges
= ARRAY_SIZE(bq25890_volatile_reg_ranges
),
123 static const struct regmap_config bq25890_regmap_config
= {
127 .max_register
= 0x14,
128 .cache_type
= REGCACHE_RBTREE
,
130 .wr_table
= &bq25890_writeable_regs
,
131 .volatile_table
= &bq25890_volatile_regs
,
134 static const struct reg_field bq25890_reg_fields
[] = {
136 [F_EN_HIZ
] = REG_FIELD(0x00, 7, 7),
137 [F_EN_ILIM
] = REG_FIELD(0x00, 6, 6),
138 [F_IILIM
] = REG_FIELD(0x00, 0, 5),
140 [F_BHOT
] = REG_FIELD(0x01, 6, 7),
141 [F_BCOLD
] = REG_FIELD(0x01, 5, 5),
142 [F_VINDPM_OFS
] = REG_FIELD(0x01, 0, 4),
144 [F_CONV_START
] = REG_FIELD(0x02, 7, 7),
145 [F_CONV_RATE
] = REG_FIELD(0x02, 6, 6),
146 [F_BOOSTF
] = REG_FIELD(0x02, 5, 5),
147 [F_ICO_EN
] = REG_FIELD(0x02, 4, 4),
148 [F_HVDCP_EN
] = REG_FIELD(0x02, 3, 3), // reserved on BQ25896
149 [F_MAXC_EN
] = REG_FIELD(0x02, 2, 2), // reserved on BQ25896
150 [F_FORCE_DPM
] = REG_FIELD(0x02, 1, 1),
151 [F_AUTO_DPDM_EN
] = REG_FIELD(0x02, 0, 0),
153 [F_BAT_LOAD_EN
] = REG_FIELD(0x03, 7, 7),
154 [F_WD_RST
] = REG_FIELD(0x03, 6, 6),
155 [F_OTG_CFG
] = REG_FIELD(0x03, 5, 5),
156 [F_CHG_CFG
] = REG_FIELD(0x03, 4, 4),
157 [F_SYSVMIN
] = REG_FIELD(0x03, 1, 3),
158 /* MIN_VBAT_SEL on BQ25896 */
160 [F_PUMPX_EN
] = REG_FIELD(0x04, 7, 7),
161 [F_ICHG
] = REG_FIELD(0x04, 0, 6),
163 [F_IPRECHG
] = REG_FIELD(0x05, 4, 7),
164 [F_ITERM
] = REG_FIELD(0x05, 0, 3),
166 [F_VREG
] = REG_FIELD(0x06, 2, 7),
167 [F_BATLOWV
] = REG_FIELD(0x06, 1, 1),
168 [F_VRECHG
] = REG_FIELD(0x06, 0, 0),
170 [F_TERM_EN
] = REG_FIELD(0x07, 7, 7),
171 [F_STAT_DIS
] = REG_FIELD(0x07, 6, 6),
172 [F_WD
] = REG_FIELD(0x07, 4, 5),
173 [F_TMR_EN
] = REG_FIELD(0x07, 3, 3),
174 [F_CHG_TMR
] = REG_FIELD(0x07, 1, 2),
175 [F_JEITA_ISET
] = REG_FIELD(0x07, 0, 0), // reserved on BQ25895
177 [F_BATCMP
] = REG_FIELD(0x08, 5, 7),
178 [F_VCLAMP
] = REG_FIELD(0x08, 2, 4),
179 [F_TREG
] = REG_FIELD(0x08, 0, 1),
181 [F_FORCE_ICO
] = REG_FIELD(0x09, 7, 7),
182 [F_TMR2X_EN
] = REG_FIELD(0x09, 6, 6),
183 [F_BATFET_DIS
] = REG_FIELD(0x09, 5, 5),
184 [F_JEITA_VSET
] = REG_FIELD(0x09, 4, 4), // reserved on BQ25895
185 [F_BATFET_DLY
] = REG_FIELD(0x09, 3, 3),
186 [F_BATFET_RST_EN
] = REG_FIELD(0x09, 2, 2),
187 [F_PUMPX_UP
] = REG_FIELD(0x09, 1, 1),
188 [F_PUMPX_DN
] = REG_FIELD(0x09, 0, 0),
190 [F_BOOSTV
] = REG_FIELD(0x0A, 4, 7),
191 /* PFM_OTG_DIS 3 on BQ25896 */
192 [F_BOOSTI
] = REG_FIELD(0x0A, 0, 2), // reserved on BQ25895
194 [F_VBUS_STAT
] = REG_FIELD(0x0B, 5, 7),
195 [F_CHG_STAT
] = REG_FIELD(0x0B, 3, 4),
196 [F_PG_STAT
] = REG_FIELD(0x0B, 2, 2),
197 [F_SDP_STAT
] = REG_FIELD(0x0B, 1, 1), // reserved on BQ25896
198 [F_VSYS_STAT
] = REG_FIELD(0x0B, 0, 0),
200 [F_WD_FAULT
] = REG_FIELD(0x0C, 7, 7),
201 [F_BOOST_FAULT
] = REG_FIELD(0x0C, 6, 6),
202 [F_CHG_FAULT
] = REG_FIELD(0x0C, 4, 5),
203 [F_BAT_FAULT
] = REG_FIELD(0x0C, 3, 3),
204 [F_NTC_FAULT
] = REG_FIELD(0x0C, 0, 2),
206 [F_FORCE_VINDPM
] = REG_FIELD(0x0D, 7, 7),
207 [F_VINDPM
] = REG_FIELD(0x0D, 0, 6),
209 [F_THERM_STAT
] = REG_FIELD(0x0E, 7, 7),
210 [F_BATV
] = REG_FIELD(0x0E, 0, 6),
212 [F_SYSV
] = REG_FIELD(0x0F, 0, 6),
214 [F_TSPCT
] = REG_FIELD(0x10, 0, 6),
216 [F_VBUS_GD
] = REG_FIELD(0x11, 7, 7),
217 [F_VBUSV
] = REG_FIELD(0x11, 0, 6),
219 [F_ICHGR
] = REG_FIELD(0x12, 0, 6),
221 [F_VDPM_STAT
] = REG_FIELD(0x13, 7, 7),
222 [F_IDPM_STAT
] = REG_FIELD(0x13, 6, 6),
223 [F_IDPM_LIM
] = REG_FIELD(0x13, 0, 5),
225 [F_REG_RST
] = REG_FIELD(0x14, 7, 7),
226 [F_ICO_OPTIMIZED
] = REG_FIELD(0x14, 6, 6),
227 [F_PN
] = REG_FIELD(0x14, 3, 5),
228 [F_TS_PROFILE
] = REG_FIELD(0x14, 2, 2),
229 [F_DEV_REV
] = REG_FIELD(0x14, 0, 1)
233 * Most of the val -> idx conversions can be computed, given the minimum,
234 * maximum and the step between values. For the rest of conversions, we use
237 enum bq25890_table_ids
{
250 /* Thermal Regulation Threshold lookup table, in degrees Celsius */
251 static const u32 bq25890_treg_tbl
[] = { 60, 80, 100, 120 };
253 #define BQ25890_TREG_TBL_SIZE ARRAY_SIZE(bq25890_treg_tbl)
255 /* Boost mode current limit lookup table, in uA */
256 static const u32 bq25890_boosti_tbl
[] = {
257 500000, 700000, 1100000, 1300000, 1600000, 1800000, 2100000, 2400000
260 #define BQ25890_BOOSTI_TBL_SIZE ARRAY_SIZE(bq25890_boosti_tbl)
262 struct bq25890_range
{
268 struct bq25890_lookup
{
274 struct bq25890_range rt
;
275 struct bq25890_lookup lt
;
276 } bq25890_tables
[] = {
278 [TBL_ICHG
] = { .rt
= {0, 5056000, 64000} }, /* uA */
279 [TBL_ITERM
] = { .rt
= {64000, 1024000, 64000} }, /* uA */
280 [TBL_VREG
] = { .rt
= {3840000, 4608000, 16000} }, /* uV */
281 [TBL_BOOSTV
] = { .rt
= {4550000, 5510000, 64000} }, /* uV */
282 [TBL_SYSVMIN
] = { .rt
= {3000000, 3700000, 100000} }, /* uV */
285 [TBL_TREG
] = { .lt
= {bq25890_treg_tbl
, BQ25890_TREG_TBL_SIZE
} },
286 [TBL_BOOSTI
] = { .lt
= {bq25890_boosti_tbl
, BQ25890_BOOSTI_TBL_SIZE
} }
289 static int bq25890_field_read(struct bq25890_device
*bq
,
290 enum bq25890_fields field_id
)
295 ret
= regmap_field_read(bq
->rmap_fields
[field_id
], &val
);
302 static int bq25890_field_write(struct bq25890_device
*bq
,
303 enum bq25890_fields field_id
, u8 val
)
305 return regmap_field_write(bq
->rmap_fields
[field_id
], val
);
308 static u8
bq25890_find_idx(u32 value
, enum bq25890_table_ids id
)
312 if (id
>= TBL_TREG
) {
313 const u32
*tbl
= bq25890_tables
[id
].lt
.tbl
;
314 u32 tbl_size
= bq25890_tables
[id
].lt
.size
;
316 for (idx
= 1; idx
< tbl_size
&& tbl
[idx
] <= value
; idx
++)
319 const struct bq25890_range
*rtbl
= &bq25890_tables
[id
].rt
;
322 rtbl_size
= (rtbl
->max
- rtbl
->min
) / rtbl
->step
+ 1;
325 idx
< rtbl_size
&& (idx
* rtbl
->step
+ rtbl
->min
<= value
);
333 static u32
bq25890_find_val(u8 idx
, enum bq25890_table_ids id
)
335 const struct bq25890_range
*rtbl
;
339 return bq25890_tables
[id
].lt
.tbl
[idx
];
342 rtbl
= &bq25890_tables
[id
].rt
;
344 return (rtbl
->min
+ idx
* rtbl
->step
);
347 enum bq25890_status
{
350 STATUS_FAST_CHARGING
,
351 STATUS_TERMINATION_DONE
,
354 enum bq25890_chrg_fault
{
357 CHRG_FAULT_THERMAL_SHUTDOWN
,
358 CHRG_FAULT_TIMER_EXPIRED
,
361 static int bq25890_power_supply_get_property(struct power_supply
*psy
,
362 enum power_supply_property psp
,
363 union power_supply_propval
*val
)
366 struct bq25890_device
*bq
= power_supply_get_drvdata(psy
);
367 struct bq25890_state state
;
369 mutex_lock(&bq
->lock
);
371 mutex_unlock(&bq
->lock
);
374 case POWER_SUPPLY_PROP_STATUS
:
376 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
377 else if (state
.chrg_status
== STATUS_NOT_CHARGING
)
378 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
379 else if (state
.chrg_status
== STATUS_PRE_CHARGING
||
380 state
.chrg_status
== STATUS_FAST_CHARGING
)
381 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
382 else if (state
.chrg_status
== STATUS_TERMINATION_DONE
)
383 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
385 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
389 case POWER_SUPPLY_PROP_MANUFACTURER
:
390 val
->strval
= BQ25890_MANUFACTURER
;
393 case POWER_SUPPLY_PROP_MODEL_NAME
:
394 if (bq
->chip_id
== BQ25890_ID
)
395 val
->strval
= "BQ25890";
396 else if (bq
->chip_id
== BQ25895_ID
)
397 val
->strval
= "BQ25895";
398 else if (bq
->chip_id
== BQ25896_ID
)
399 val
->strval
= "BQ25896";
401 val
->strval
= "UNKNOWN";
405 case POWER_SUPPLY_PROP_ONLINE
:
406 val
->intval
= state
.online
;
409 case POWER_SUPPLY_PROP_HEALTH
:
410 if (!state
.chrg_fault
&& !state
.bat_fault
&& !state
.boost_fault
)
411 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
412 else if (state
.bat_fault
)
413 val
->intval
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
414 else if (state
.chrg_fault
== CHRG_FAULT_TIMER_EXPIRED
)
415 val
->intval
= POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE
;
416 else if (state
.chrg_fault
== CHRG_FAULT_THERMAL_SHUTDOWN
)
417 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
419 val
->intval
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
422 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
423 ret
= bq25890_field_read(bq
, F_ICHGR
); /* read measured value */
427 /* converted_val = ADC_val * 50mA (table 10.3.19) */
428 val
->intval
= ret
* 50000;
431 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
:
432 val
->intval
= bq25890_find_val(bq
->init_data
.ichg
, TBL_ICHG
);
435 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
441 ret
= bq25890_field_read(bq
, F_BATV
); /* read measured value */
445 /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
446 val
->intval
= 2304000 + ret
* 20000;
449 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
:
450 val
->intval
= bq25890_find_val(bq
->init_data
.vreg
, TBL_VREG
);
453 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT
:
454 val
->intval
= bq25890_find_val(bq
->init_data
.iterm
, TBL_ITERM
);
457 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
458 ret
= bq25890_field_read(bq
, F_SYSV
); /* read measured value */
462 /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
463 val
->intval
= 2304000 + ret
* 20000;
473 static int bq25890_get_chip_state(struct bq25890_device
*bq
,
474 struct bq25890_state
*state
)
479 enum bq25890_fields id
;
482 {F_CHG_STAT
, &state
->chrg_status
},
483 {F_PG_STAT
, &state
->online
},
484 {F_VSYS_STAT
, &state
->vsys_status
},
485 {F_BOOST_FAULT
, &state
->boost_fault
},
486 {F_BAT_FAULT
, &state
->bat_fault
},
487 {F_CHG_FAULT
, &state
->chrg_fault
}
490 for (i
= 0; i
< ARRAY_SIZE(state_fields
); i
++) {
491 ret
= bq25890_field_read(bq
, state_fields
[i
].id
);
495 *state_fields
[i
].data
= ret
;
498 dev_dbg(bq
->dev
, "S:CHG/PG/VSYS=%d/%d/%d, F:CHG/BOOST/BAT=%d/%d/%d\n",
499 state
->chrg_status
, state
->online
, state
->vsys_status
,
500 state
->chrg_fault
, state
->boost_fault
, state
->bat_fault
);
505 static bool bq25890_state_changed(struct bq25890_device
*bq
,
506 struct bq25890_state
*new_state
)
508 struct bq25890_state old_state
;
510 mutex_lock(&bq
->lock
);
511 old_state
= bq
->state
;
512 mutex_unlock(&bq
->lock
);
514 return (old_state
.chrg_status
!= new_state
->chrg_status
||
515 old_state
.chrg_fault
!= new_state
->chrg_fault
||
516 old_state
.online
!= new_state
->online
||
517 old_state
.bat_fault
!= new_state
->bat_fault
||
518 old_state
.boost_fault
!= new_state
->boost_fault
||
519 old_state
.vsys_status
!= new_state
->vsys_status
);
522 static void bq25890_handle_state_change(struct bq25890_device
*bq
,
523 struct bq25890_state
*new_state
)
526 struct bq25890_state old_state
;
528 mutex_lock(&bq
->lock
);
529 old_state
= bq
->state
;
530 mutex_unlock(&bq
->lock
);
532 if (!new_state
->online
) { /* power removed */
534 ret
= bq25890_field_write(bq
, F_CONV_START
, 0);
537 } else if (!old_state
.online
) { /* power inserted */
538 /* enable ADC, to have control of charge current/voltage */
539 ret
= bq25890_field_write(bq
, F_CONV_START
, 1);
547 dev_err(bq
->dev
, "Error communicating with the chip.\n");
550 static irqreturn_t
bq25890_irq_handler_thread(int irq
, void *private)
552 struct bq25890_device
*bq
= private;
554 struct bq25890_state state
;
556 ret
= bq25890_get_chip_state(bq
, &state
);
560 if (!bq25890_state_changed(bq
, &state
))
563 bq25890_handle_state_change(bq
, &state
);
565 mutex_lock(&bq
->lock
);
567 mutex_unlock(&bq
->lock
);
569 power_supply_changed(bq
->charger
);
575 static int bq25890_chip_reset(struct bq25890_device
*bq
)
578 int rst_check_counter
= 10;
580 ret
= bq25890_field_write(bq
, F_REG_RST
, 1);
585 ret
= bq25890_field_read(bq
, F_REG_RST
);
590 } while (ret
== 1 && --rst_check_counter
);
592 if (!rst_check_counter
)
598 static int bq25890_hw_init(struct bq25890_device
*bq
)
602 struct bq25890_state state
;
605 enum bq25890_fields id
;
608 {F_ICHG
, bq
->init_data
.ichg
},
609 {F_VREG
, bq
->init_data
.vreg
},
610 {F_ITERM
, bq
->init_data
.iterm
},
611 {F_IPRECHG
, bq
->init_data
.iprechg
},
612 {F_SYSVMIN
, bq
->init_data
.sysvmin
},
613 {F_BOOSTV
, bq
->init_data
.boostv
},
614 {F_BOOSTI
, bq
->init_data
.boosti
},
615 {F_BOOSTF
, bq
->init_data
.boostf
},
616 {F_EN_ILIM
, bq
->init_data
.ilim_en
},
617 {F_TREG
, bq
->init_data
.treg
}
620 ret
= bq25890_chip_reset(bq
);
622 dev_dbg(bq
->dev
, "Reset failed %d\n", ret
);
626 /* disable watchdog */
627 ret
= bq25890_field_write(bq
, F_WD
, 0);
629 dev_dbg(bq
->dev
, "Disabling watchdog failed %d\n", ret
);
633 /* initialize currents/voltages and other parameters */
634 for (i
= 0; i
< ARRAY_SIZE(init_data
); i
++) {
635 ret
= bq25890_field_write(bq
, init_data
[i
].id
,
638 dev_dbg(bq
->dev
, "Writing init data failed %d\n", ret
);
643 /* Configure ADC for continuous conversions. This does not enable it. */
644 ret
= bq25890_field_write(bq
, F_CONV_RATE
, 1);
646 dev_dbg(bq
->dev
, "Config ADC failed %d\n", ret
);
650 ret
= bq25890_get_chip_state(bq
, &state
);
652 dev_dbg(bq
->dev
, "Get state failed %d\n", ret
);
656 mutex_lock(&bq
->lock
);
658 mutex_unlock(&bq
->lock
);
663 static enum power_supply_property bq25890_power_supply_props
[] = {
664 POWER_SUPPLY_PROP_MANUFACTURER
,
665 POWER_SUPPLY_PROP_MODEL_NAME
,
666 POWER_SUPPLY_PROP_STATUS
,
667 POWER_SUPPLY_PROP_ONLINE
,
668 POWER_SUPPLY_PROP_HEALTH
,
669 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
,
670 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
,
671 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
,
672 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
,
673 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT
,
674 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
677 static char *bq25890_charger_supplied_to
[] = {
681 static const struct power_supply_desc bq25890_power_supply_desc
= {
682 .name
= "bq25890-charger",
683 .type
= POWER_SUPPLY_TYPE_USB
,
684 .properties
= bq25890_power_supply_props
,
685 .num_properties
= ARRAY_SIZE(bq25890_power_supply_props
),
686 .get_property
= bq25890_power_supply_get_property
,
689 static int bq25890_power_supply_init(struct bq25890_device
*bq
)
691 struct power_supply_config psy_cfg
= { .drv_data
= bq
, };
693 psy_cfg
.supplied_to
= bq25890_charger_supplied_to
;
694 psy_cfg
.num_supplicants
= ARRAY_SIZE(bq25890_charger_supplied_to
);
696 bq
->charger
= power_supply_register(bq
->dev
, &bq25890_power_supply_desc
,
699 return PTR_ERR_OR_ZERO(bq
->charger
);
702 static void bq25890_usb_work(struct work_struct
*data
)
705 struct bq25890_device
*bq
=
706 container_of(data
, struct bq25890_device
, usb_work
);
708 switch (bq
->usb_event
) {
710 /* Enable boost mode */
711 ret
= bq25890_field_write(bq
, F_OTG_CFG
, 1);
717 /* Disable boost mode */
718 ret
= bq25890_field_write(bq
, F_OTG_CFG
, 0);
722 power_supply_changed(bq
->charger
);
729 dev_err(bq
->dev
, "Error switching to boost/charger mode.\n");
732 static int bq25890_usb_notifier(struct notifier_block
*nb
, unsigned long val
,
735 struct bq25890_device
*bq
=
736 container_of(nb
, struct bq25890_device
, usb_nb
);
739 queue_work(system_power_efficient_wq
, &bq
->usb_work
);
744 static int bq25890_irq_probe(struct bq25890_device
*bq
)
746 struct gpio_desc
*irq
;
748 irq
= devm_gpiod_get(bq
->dev
, BQ25890_IRQ_PIN
, GPIOD_IN
);
750 dev_err(bq
->dev
, "Could not probe irq pin.\n");
754 return gpiod_to_irq(irq
);
757 static int bq25890_fw_read_u32_props(struct bq25890_device
*bq
)
762 struct bq25890_init_data
*init
= &bq
->init_data
;
766 enum bq25890_table_ids tbl_id
;
767 u8
*conv_data
; /* holds converted value from given property */
769 /* required properties */
770 {"ti,charge-current", false, TBL_ICHG
, &init
->ichg
},
771 {"ti,battery-regulation-voltage", false, TBL_VREG
, &init
->vreg
},
772 {"ti,termination-current", false, TBL_ITERM
, &init
->iterm
},
773 {"ti,precharge-current", false, TBL_ITERM
, &init
->iprechg
},
774 {"ti,minimum-sys-voltage", false, TBL_SYSVMIN
, &init
->sysvmin
},
775 {"ti,boost-voltage", false, TBL_BOOSTV
, &init
->boostv
},
776 {"ti,boost-max-current", false, TBL_BOOSTI
, &init
->boosti
},
778 /* optional properties */
779 {"ti,thermal-regulation-threshold", true, TBL_TREG
, &init
->treg
}
782 /* initialize data for optional properties */
783 init
->treg
= 3; /* 120 degrees Celsius */
785 for (i
= 0; i
< ARRAY_SIZE(props
); i
++) {
786 ret
= device_property_read_u32(bq
->dev
, props
[i
].name
,
789 if (props
[i
].optional
)
792 dev_err(bq
->dev
, "Unable to read property %d %s\n", ret
,
798 *props
[i
].conv_data
= bq25890_find_idx(property
,
805 static int bq25890_fw_probe(struct bq25890_device
*bq
)
808 struct bq25890_init_data
*init
= &bq
->init_data
;
810 ret
= bq25890_fw_read_u32_props(bq
);
814 init
->ilim_en
= device_property_read_bool(bq
->dev
, "ti,use-ilim-pin");
815 init
->boostf
= device_property_read_bool(bq
->dev
, "ti,boost-low-freq");
820 static int bq25890_probe(struct i2c_client
*client
,
821 const struct i2c_device_id
*id
)
823 struct i2c_adapter
*adapter
= client
->adapter
;
824 struct device
*dev
= &client
->dev
;
825 struct bq25890_device
*bq
;
829 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)) {
830 dev_err(dev
, "No support for SMBUS_BYTE_DATA\n");
834 bq
= devm_kzalloc(dev
, sizeof(*bq
), GFP_KERNEL
);
841 mutex_init(&bq
->lock
);
843 bq
->rmap
= devm_regmap_init_i2c(client
, &bq25890_regmap_config
);
844 if (IS_ERR(bq
->rmap
)) {
845 dev_err(dev
, "failed to allocate register map\n");
846 return PTR_ERR(bq
->rmap
);
849 for (i
= 0; i
< ARRAY_SIZE(bq25890_reg_fields
); i
++) {
850 const struct reg_field
*reg_fields
= bq25890_reg_fields
;
852 bq
->rmap_fields
[i
] = devm_regmap_field_alloc(dev
, bq
->rmap
,
854 if (IS_ERR(bq
->rmap_fields
[i
])) {
855 dev_err(dev
, "cannot allocate regmap field\n");
856 return PTR_ERR(bq
->rmap_fields
[i
]);
860 i2c_set_clientdata(client
, bq
);
862 bq
->chip_id
= bq25890_field_read(bq
, F_PN
);
863 if (bq
->chip_id
< 0) {
864 dev_err(dev
, "Cannot read chip ID.\n");
868 if ((bq
->chip_id
!= BQ25890_ID
) && (bq
->chip_id
!= BQ25895_ID
)
869 && (bq
->chip_id
!= BQ25896_ID
)) {
870 dev_err(dev
, "Chip with ID=%d, not supported!\n", bq
->chip_id
);
874 if (!dev
->platform_data
) {
875 ret
= bq25890_fw_probe(bq
);
877 dev_err(dev
, "Cannot read device properties.\n");
884 ret
= bq25890_hw_init(bq
);
886 dev_err(dev
, "Cannot initialize the chip.\n");
890 if (client
->irq
<= 0)
891 client
->irq
= bq25890_irq_probe(bq
);
893 if (client
->irq
< 0) {
894 dev_err(dev
, "No irq resource found.\n");
899 bq
->usb_phy
= devm_usb_get_phy(dev
, USB_PHY_TYPE_USB2
);
900 if (!IS_ERR_OR_NULL(bq
->usb_phy
)) {
901 INIT_WORK(&bq
->usb_work
, bq25890_usb_work
);
902 bq
->usb_nb
.notifier_call
= bq25890_usb_notifier
;
903 usb_register_notifier(bq
->usb_phy
, &bq
->usb_nb
);
906 ret
= devm_request_threaded_irq(dev
, client
->irq
, NULL
,
907 bq25890_irq_handler_thread
,
908 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
909 BQ25890_IRQ_PIN
, bq
);
913 ret
= bq25890_power_supply_init(bq
);
915 dev_err(dev
, "Failed to register power supply\n");
922 if (!IS_ERR_OR_NULL(bq
->usb_phy
))
923 usb_unregister_notifier(bq
->usb_phy
, &bq
->usb_nb
);
928 static int bq25890_remove(struct i2c_client
*client
)
930 struct bq25890_device
*bq
= i2c_get_clientdata(client
);
932 power_supply_unregister(bq
->charger
);
934 if (!IS_ERR_OR_NULL(bq
->usb_phy
))
935 usb_unregister_notifier(bq
->usb_phy
, &bq
->usb_nb
);
937 /* reset all registers to default values */
938 bq25890_chip_reset(bq
);
943 #ifdef CONFIG_PM_SLEEP
944 static int bq25890_suspend(struct device
*dev
)
946 struct bq25890_device
*bq
= dev_get_drvdata(dev
);
949 * If charger is removed, while in suspend, make sure ADC is diabled
950 * since it consumes slightly more power.
952 return bq25890_field_write(bq
, F_CONV_START
, 0);
955 static int bq25890_resume(struct device
*dev
)
958 struct bq25890_state state
;
959 struct bq25890_device
*bq
= dev_get_drvdata(dev
);
961 ret
= bq25890_get_chip_state(bq
, &state
);
965 mutex_lock(&bq
->lock
);
967 mutex_unlock(&bq
->lock
);
969 /* Re-enable ADC only if charger is plugged in. */
971 ret
= bq25890_field_write(bq
, F_CONV_START
, 1);
976 /* signal userspace, maybe state changed while suspended */
977 power_supply_changed(bq
->charger
);
983 static const struct dev_pm_ops bq25890_pm
= {
984 SET_SYSTEM_SLEEP_PM_OPS(bq25890_suspend
, bq25890_resume
)
987 static const struct i2c_device_id bq25890_i2c_ids
[] = {
991 MODULE_DEVICE_TABLE(i2c
, bq25890_i2c_ids
);
993 static const struct of_device_id bq25890_of_match
[] = {
994 { .compatible
= "ti,bq25890", },
997 MODULE_DEVICE_TABLE(of
, bq25890_of_match
);
999 static const struct acpi_device_id bq25890_acpi_match
[] = {
1003 MODULE_DEVICE_TABLE(acpi
, bq25890_acpi_match
);
1005 static struct i2c_driver bq25890_driver
= {
1007 .name
= "bq25890-charger",
1008 .of_match_table
= of_match_ptr(bq25890_of_match
),
1009 .acpi_match_table
= ACPI_PTR(bq25890_acpi_match
),
1012 .probe
= bq25890_probe
,
1013 .remove
= bq25890_remove
,
1014 .id_table
= bq25890_i2c_ids
,
1016 module_i2c_driver(bq25890_driver
);
1018 MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
1019 MODULE_DESCRIPTION("bq25890 charger driver");
1020 MODULE_LICENSE("GPL");