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 {
272 /* Thermal Regulation Threshold lookup table, in degrees Celsius */
273 static const u32 bq25890_treg_tbl[] = { 60, 80, 100, 120 };
275 #define BQ25890_TREG_TBL_SIZE ARRAY_SIZE(bq25890_treg_tbl)
277 /* Boost mode current limit lookup table, in uA */
278 static const u32 bq25890_boosti_tbl[] = {
279 500000, 700000, 1100000, 1300000, 1600000, 1800000, 2100000, 2400000
282 #define BQ25890_BOOSTI_TBL_SIZE ARRAY_SIZE(bq25890_boosti_tbl)
284 /* NTC 10K temperature lookup table in tenths of a degree */
285 static const u32 bq25890_tspct_tbl[] = {
286 850, 840, 830, 820, 810, 800, 790, 780,
287 770, 760, 750, 740, 730, 720, 710, 700,
288 690, 685, 680, 675, 670, 660, 650, 645,
289 640, 630, 620, 615, 610, 600, 590, 585,
290 580, 570, 565, 560, 550, 540, 535, 530,
291 520, 515, 510, 500, 495, 490, 480, 475,
292 470, 460, 455, 450, 440, 435, 430, 425,
293 420, 410, 405, 400, 390, 385, 380, 370,
294 365, 360, 355, 350, 340, 335, 330, 320,
295 310, 305, 300, 290, 285, 280, 275, 270,
296 260, 250, 245, 240, 230, 225, 220, 210,
297 205, 200, 190, 180, 175, 170, 160, 150,
298 145, 140, 130, 120, 115, 110, 100, 90,
299 80, 70, 60, 50, 40, 30, 20, 10,
300 0, -10, -20, -30, -40, -60, -70, -80,
301 -90, -10, -120, -140, -150, -170, -190, -210,
304 #define BQ25890_TSPCT_TBL_SIZE ARRAY_SIZE(bq25890_tspct_tbl)
306 struct bq25890_range {
312 struct bq25890_lookup {
318 struct bq25890_range rt;
319 struct bq25890_lookup lt;
320 } bq25890_tables[] = {
322 /* TODO: BQ25896 has max ICHG 3008 mA */
323 [TBL_ICHG] = { .rt = {0, 5056000, 64000} }, /* uA */
324 [TBL_ITERM] = { .rt = {64000, 1024000, 64000} }, /* uA */
325 [TBL_IILIM] = { .rt = {100000, 3250000, 50000} }, /* uA */
326 [TBL_VREG] = { .rt = {3840000, 4608000, 16000} }, /* uV */
327 [TBL_BOOSTV] = { .rt = {4550000, 5510000, 64000} }, /* uV */
328 [TBL_SYSVMIN] = { .rt = {3000000, 3700000, 100000} }, /* uV */
329 [TBL_VBATCOMP] ={ .rt = {0, 224000, 32000} }, /* uV */
330 [TBL_RBATCOMP] ={ .rt = {0, 140000, 20000} }, /* uOhm */
333 [TBL_TREG] = { .lt = {bq25890_treg_tbl, BQ25890_TREG_TBL_SIZE} },
334 [TBL_BOOSTI] = { .lt = {bq25890_boosti_tbl, BQ25890_BOOSTI_TBL_SIZE} },
335 [TBL_TSPCT] = { .lt = {bq25890_tspct_tbl, BQ25890_TSPCT_TBL_SIZE} }
338 static int bq25890_field_read(struct bq25890_device *bq,
339 enum bq25890_fields field_id)
344 ret = regmap_field_read(bq->rmap_fields[field_id], &val);
351 static int bq25890_field_write(struct bq25890_device *bq,
352 enum bq25890_fields field_id, u8 val)
354 return regmap_field_write(bq->rmap_fields[field_id], val);
357 static u8 bq25890_find_idx(u32 value, enum bq25890_table_ids id)
361 if (id >= TBL_TREG) {
362 const u32 *tbl = bq25890_tables[id].lt.tbl;
363 u32 tbl_size = bq25890_tables[id].lt.size;
365 for (idx = 1; idx < tbl_size && tbl[idx] <= value; idx++)
368 const struct bq25890_range *rtbl = &bq25890_tables[id].rt;
371 rtbl_size = (rtbl->max - rtbl->min) / rtbl->step + 1;
374 idx < rtbl_size && (idx * rtbl->step + rtbl->min <= value);
382 static u32 bq25890_find_val(u8 idx, enum bq25890_table_ids id)
384 const struct bq25890_range *rtbl;
388 return bq25890_tables[id].lt.tbl[idx];
391 rtbl = &bq25890_tables[id].rt;
393 return (rtbl->min + idx * rtbl->step);
396 enum bq25890_status {
399 STATUS_FAST_CHARGING,
400 STATUS_TERMINATION_DONE,
403 enum bq25890_chrg_fault {
406 CHRG_FAULT_THERMAL_SHUTDOWN,
407 CHRG_FAULT_TIMER_EXPIRED,
410 static bool bq25890_is_adc_property(enum power_supply_property psp)
413 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
414 case POWER_SUPPLY_PROP_CURRENT_NOW:
415 case POWER_SUPPLY_PROP_TEMP:
423 static irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq);
425 static int bq25890_power_supply_get_property(struct power_supply *psy,
426 enum power_supply_property psp,
427 union power_supply_propval *val)
429 struct bq25890_device *bq = power_supply_get_drvdata(psy);
430 struct bq25890_state state;
434 mutex_lock(&bq->lock);
435 /* update state in case we lost an interrupt */
436 __bq25890_handle_irq(bq);
438 do_adc_conv = !state.online && bq25890_is_adc_property(psp);
440 bq25890_field_write(bq, F_CONV_START, 1);
441 mutex_unlock(&bq->lock);
444 regmap_field_read_poll_timeout(bq->rmap_fields[F_CONV_START],
445 ret, !ret, 25000, 1000000);
448 case POWER_SUPPLY_PROP_STATUS:
450 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
451 else if (state.chrg_status == STATUS_NOT_CHARGING)
452 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
453 else if (state.chrg_status == STATUS_PRE_CHARGING ||
454 state.chrg_status == STATUS_FAST_CHARGING)
455 val->intval = POWER_SUPPLY_STATUS_CHARGING;
456 else if (state.chrg_status == STATUS_TERMINATION_DONE)
457 val->intval = POWER_SUPPLY_STATUS_FULL;
459 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
463 case POWER_SUPPLY_PROP_CHARGE_TYPE:
464 if (!state.online || state.chrg_status == STATUS_NOT_CHARGING ||
465 state.chrg_status == STATUS_TERMINATION_DONE)
466 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
467 else if (state.chrg_status == STATUS_PRE_CHARGING)
468 val->intval = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
469 else if (state.chrg_status == STATUS_FAST_CHARGING)
470 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
471 else /* unreachable */
472 val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
475 case POWER_SUPPLY_PROP_MANUFACTURER:
476 val->strval = BQ25890_MANUFACTURER;
479 case POWER_SUPPLY_PROP_MODEL_NAME:
480 val->strval = bq25890_chip_name[bq->chip_version];
483 case POWER_SUPPLY_PROP_ONLINE:
484 val->intval = state.online;
487 case POWER_SUPPLY_PROP_HEALTH:
488 if (!state.chrg_fault && !state.bat_fault && !state.boost_fault)
489 val->intval = POWER_SUPPLY_HEALTH_GOOD;
490 else if (state.bat_fault)
491 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
492 else if (state.chrg_fault == CHRG_FAULT_TIMER_EXPIRED)
493 val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
494 else if (state.chrg_fault == CHRG_FAULT_THERMAL_SHUTDOWN)
495 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
497 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
500 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
501 val->intval = bq25890_find_val(bq->init_data.ichg, TBL_ICHG);
504 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
510 ret = bq25890_field_read(bq, F_BATV); /* read measured value */
514 /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
515 val->intval = 2304000 + ret * 20000;
518 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
519 val->intval = bq25890_find_val(bq->init_data.vreg, TBL_VREG);
522 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
523 val->intval = bq25890_find_val(bq->init_data.iprechg, TBL_ITERM);
526 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
527 val->intval = bq25890_find_val(bq->init_data.iterm, TBL_ITERM);
530 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
531 ret = bq25890_field_read(bq, F_IILIM);
535 val->intval = bq25890_find_val(ret, TBL_IILIM);
538 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
539 ret = bq25890_field_read(bq, F_SYSV); /* read measured value */
543 /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
544 val->intval = 2304000 + ret * 20000;
547 case POWER_SUPPLY_PROP_CURRENT_NOW:
548 ret = bq25890_field_read(bq, F_ICHGR); /* read measured value */
552 /* converted_val = ADC_val * 50mA (table 10.3.19) */
553 val->intval = ret * -50000;
556 case POWER_SUPPLY_PROP_TEMP:
557 ret = bq25890_field_read(bq, F_TSPCT);
561 /* convert TS percentage into rough temperature */
562 val->intval = bq25890_find_val(ret, TBL_TSPCT);
572 static int bq25890_get_chip_state(struct bq25890_device *bq,
573 struct bq25890_state *state)
578 enum bq25890_fields id;
581 {F_CHG_STAT, &state->chrg_status},
582 {F_PG_STAT, &state->online},
583 {F_VSYS_STAT, &state->vsys_status},
584 {F_BOOST_FAULT, &state->boost_fault},
585 {F_BAT_FAULT, &state->bat_fault},
586 {F_CHG_FAULT, &state->chrg_fault}
589 for (i = 0; i < ARRAY_SIZE(state_fields); i++) {
590 ret = bq25890_field_read(bq, state_fields[i].id);
594 *state_fields[i].data = ret;
597 dev_dbg(bq->dev, "S:CHG/PG/VSYS=%d/%d/%d, F:CHG/BOOST/BAT=%d/%d/%d\n",
598 state->chrg_status, state->online, state->vsys_status,
599 state->chrg_fault, state->boost_fault, state->bat_fault);
604 static irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq)
606 struct bq25890_state new_state;
609 ret = bq25890_get_chip_state(bq, &new_state);
613 if (!memcmp(&bq->state, &new_state, sizeof(new_state)))
616 if (!new_state.online && bq->state.online) { /* power removed */
618 ret = bq25890_field_write(bq, F_CONV_RATE, 0);
621 } else if (new_state.online && !bq->state.online) { /* power inserted */
622 /* enable ADC, to have control of charge current/voltage */
623 ret = bq25890_field_write(bq, F_CONV_RATE, 1);
628 bq->state = new_state;
629 power_supply_changed(bq->charger);
633 dev_err(bq->dev, "Error communicating with the chip: %pe\n",
638 static irqreturn_t bq25890_irq_handler_thread(int irq, void *private)
640 struct bq25890_device *bq = private;
643 mutex_lock(&bq->lock);
644 ret = __bq25890_handle_irq(bq);
645 mutex_unlock(&bq->lock);
650 static int bq25890_chip_reset(struct bq25890_device *bq)
653 int rst_check_counter = 10;
655 ret = bq25890_field_write(bq, F_REG_RST, 1);
660 ret = bq25890_field_read(bq, F_REG_RST);
665 } while (ret == 1 && --rst_check_counter);
667 if (!rst_check_counter)
673 static int bq25890_hw_init(struct bq25890_device *bq)
679 enum bq25890_fields id;
682 {F_ICHG, bq->init_data.ichg},
683 {F_VREG, bq->init_data.vreg},
684 {F_ITERM, bq->init_data.iterm},
685 {F_IPRECHG, bq->init_data.iprechg},
686 {F_SYSVMIN, bq->init_data.sysvmin},
687 {F_BOOSTV, bq->init_data.boostv},
688 {F_BOOSTI, bq->init_data.boosti},
689 {F_BOOSTF, bq->init_data.boostf},
690 {F_EN_ILIM, bq->init_data.ilim_en},
691 {F_TREG, bq->init_data.treg},
692 {F_BATCMP, bq->init_data.rbatcomp},
693 {F_VCLAMP, bq->init_data.vclamp},
696 ret = bq25890_chip_reset(bq);
698 dev_dbg(bq->dev, "Reset failed %d\n", ret);
702 /* disable watchdog */
703 ret = bq25890_field_write(bq, F_WD, 0);
705 dev_dbg(bq->dev, "Disabling watchdog failed %d\n", ret);
709 /* initialize currents/voltages and other parameters */
710 for (i = 0; i < ARRAY_SIZE(init_data); i++) {
711 ret = bq25890_field_write(bq, init_data[i].id,
714 dev_dbg(bq->dev, "Writing init data failed %d\n", ret);
719 ret = bq25890_get_chip_state(bq, &bq->state);
721 dev_dbg(bq->dev, "Get state failed %d\n", ret);
725 /* Configure ADC for continuous conversions when charging */
726 ret = bq25890_field_write(bq, F_CONV_RATE, !!bq->state.online);
728 dev_dbg(bq->dev, "Config ADC failed %d\n", ret);
735 static const enum power_supply_property bq25890_power_supply_props[] = {
736 POWER_SUPPLY_PROP_MANUFACTURER,
737 POWER_SUPPLY_PROP_MODEL_NAME,
738 POWER_SUPPLY_PROP_STATUS,
739 POWER_SUPPLY_PROP_CHARGE_TYPE,
740 POWER_SUPPLY_PROP_ONLINE,
741 POWER_SUPPLY_PROP_HEALTH,
742 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
743 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
744 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
745 POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
746 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
747 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
748 POWER_SUPPLY_PROP_VOLTAGE_NOW,
749 POWER_SUPPLY_PROP_CURRENT_NOW,
750 POWER_SUPPLY_PROP_TEMP,
753 static char *bq25890_charger_supplied_to[] = {
757 static const struct power_supply_desc bq25890_power_supply_desc = {
758 .name = "bq25890-charger",
759 .type = POWER_SUPPLY_TYPE_USB,
760 .properties = bq25890_power_supply_props,
761 .num_properties = ARRAY_SIZE(bq25890_power_supply_props),
762 .get_property = bq25890_power_supply_get_property,
765 static int bq25890_power_supply_init(struct bq25890_device *bq)
767 struct power_supply_config psy_cfg = { .drv_data = bq, };
769 psy_cfg.supplied_to = bq25890_charger_supplied_to;
770 psy_cfg.num_supplicants = ARRAY_SIZE(bq25890_charger_supplied_to);
772 bq->charger = devm_power_supply_register(bq->dev,
773 &bq25890_power_supply_desc,
776 return PTR_ERR_OR_ZERO(bq->charger);
779 static void bq25890_usb_work(struct work_struct *data)
782 struct bq25890_device *bq =
783 container_of(data, struct bq25890_device, usb_work);
785 switch (bq->usb_event) {
787 /* Enable boost mode */
788 ret = bq25890_field_write(bq, F_OTG_CFG, 1);
794 /* Disable boost mode */
795 ret = bq25890_field_write(bq, F_OTG_CFG, 0);
799 power_supply_changed(bq->charger);
806 dev_err(bq->dev, "Error switching to boost/charger mode.\n");
809 static int bq25890_usb_notifier(struct notifier_block *nb, unsigned long val,
812 struct bq25890_device *bq =
813 container_of(nb, struct bq25890_device, usb_nb);
816 queue_work(system_power_efficient_wq, &bq->usb_work);
821 static int bq25890_get_chip_version(struct bq25890_device *bq)
825 id = bq25890_field_read(bq, F_PN);
827 dev_err(bq->dev, "Cannot read chip ID: %d\n", id);
831 rev = bq25890_field_read(bq, F_DEV_REV);
833 dev_err(bq->dev, "Cannot read chip revision: %d\n", rev);
839 bq->chip_version = BQ25890;
842 /* BQ25892 and BQ25896 share same ID 0 */
846 bq->chip_version = BQ25896;
849 bq->chip_version = BQ25892;
853 "Unknown device revision %d, assume BQ25892\n",
855 bq->chip_version = BQ25892;
860 bq->chip_version = BQ25895;
864 dev_err(bq->dev, "Unknown chip ID %d\n", id);
871 static int bq25890_irq_probe(struct bq25890_device *bq)
873 struct gpio_desc *irq;
875 irq = devm_gpiod_get(bq->dev, BQ25890_IRQ_PIN, GPIOD_IN);
877 return dev_err_probe(bq->dev, PTR_ERR(irq),
878 "Could not probe irq pin.\n");
880 return gpiod_to_irq(irq);
883 static int bq25890_fw_read_u32_props(struct bq25890_device *bq)
888 struct bq25890_init_data *init = &bq->init_data;
892 enum bq25890_table_ids tbl_id;
893 u8 *conv_data; /* holds converted value from given property */
895 /* required properties */
896 {"ti,charge-current", false, TBL_ICHG, &init->ichg},
897 {"ti,battery-regulation-voltage", false, TBL_VREG, &init->vreg},
898 {"ti,termination-current", false, TBL_ITERM, &init->iterm},
899 {"ti,precharge-current", false, TBL_ITERM, &init->iprechg},
900 {"ti,minimum-sys-voltage", false, TBL_SYSVMIN, &init->sysvmin},
901 {"ti,boost-voltage", false, TBL_BOOSTV, &init->boostv},
902 {"ti,boost-max-current", false, TBL_BOOSTI, &init->boosti},
904 /* optional properties */
905 {"ti,thermal-regulation-threshold", true, TBL_TREG, &init->treg},
906 {"ti,ibatcomp-micro-ohms", true, TBL_RBATCOMP, &init->rbatcomp},
907 {"ti,ibatcomp-clamp-microvolt", true, TBL_VBATCOMP, &init->vclamp},
910 /* initialize data for optional properties */
911 init->treg = 3; /* 120 degrees Celsius */
912 init->rbatcomp = init->vclamp = 0; /* IBAT compensation disabled */
914 for (i = 0; i < ARRAY_SIZE(props); i++) {
915 ret = device_property_read_u32(bq->dev, props[i].name,
918 if (props[i].optional)
921 dev_err(bq->dev, "Unable to read property %d %s\n", ret,
927 *props[i].conv_data = bq25890_find_idx(property,
934 static int bq25890_fw_probe(struct bq25890_device *bq)
937 struct bq25890_init_data *init = &bq->init_data;
939 ret = bq25890_fw_read_u32_props(bq);
943 init->ilim_en = device_property_read_bool(bq->dev, "ti,use-ilim-pin");
944 init->boostf = device_property_read_bool(bq->dev, "ti,boost-low-freq");
949 static int bq25890_probe(struct i2c_client *client,
950 const struct i2c_device_id *id)
952 struct device *dev = &client->dev;
953 struct bq25890_device *bq;
957 bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL);
964 mutex_init(&bq->lock);
966 bq->rmap = devm_regmap_init_i2c(client, &bq25890_regmap_config);
967 if (IS_ERR(bq->rmap))
968 return dev_err_probe(dev, PTR_ERR(bq->rmap),
969 "failed to allocate register map\n");
971 for (i = 0; i < ARRAY_SIZE(bq25890_reg_fields); i++) {
972 const struct reg_field *reg_fields = bq25890_reg_fields;
974 bq->rmap_fields[i] = devm_regmap_field_alloc(dev, bq->rmap,
976 if (IS_ERR(bq->rmap_fields[i]))
977 return dev_err_probe(dev, PTR_ERR(bq->rmap_fields[i]),
978 "cannot allocate regmap field\n");
981 i2c_set_clientdata(client, bq);
983 ret = bq25890_get_chip_version(bq);
985 dev_err(dev, "Cannot read chip ID or unknown chip: %d\n", ret);
989 if (!dev->platform_data) {
990 ret = bq25890_fw_probe(bq);
992 dev_err(dev, "Cannot read device properties: %d\n",
1000 ret = bq25890_hw_init(bq);
1002 dev_err(dev, "Cannot initialize the chip: %d\n", ret);
1006 if (client->irq <= 0)
1007 client->irq = bq25890_irq_probe(bq);
1009 if (client->irq < 0) {
1010 dev_err(dev, "No irq resource found.\n");
1015 bq->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1016 if (!IS_ERR_OR_NULL(bq->usb_phy)) {
1017 INIT_WORK(&bq->usb_work, bq25890_usb_work);
1018 bq->usb_nb.notifier_call = bq25890_usb_notifier;
1019 usb_register_notifier(bq->usb_phy, &bq->usb_nb);
1022 ret = bq25890_power_supply_init(bq);
1024 dev_err(dev, "Failed to register power supply\n");
1025 goto err_unregister_usb_notifier;
1028 ret = devm_request_threaded_irq(dev, client->irq, NULL,
1029 bq25890_irq_handler_thread,
1030 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1031 BQ25890_IRQ_PIN, bq);
1033 goto err_unregister_usb_notifier;
1037 err_unregister_usb_notifier:
1038 if (!IS_ERR_OR_NULL(bq->usb_phy))
1039 usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
1044 static int bq25890_remove(struct i2c_client *client)
1046 struct bq25890_device *bq = i2c_get_clientdata(client);
1048 if (!IS_ERR_OR_NULL(bq->usb_phy))
1049 usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
1051 /* reset all registers to default values */
1052 bq25890_chip_reset(bq);
1057 #ifdef CONFIG_PM_SLEEP
1058 static int bq25890_suspend(struct device *dev)
1060 struct bq25890_device *bq = dev_get_drvdata(dev);
1063 * If charger is removed, while in suspend, make sure ADC is diabled
1064 * since it consumes slightly more power.
1066 return bq25890_field_write(bq, F_CONV_RATE, 0);
1069 static int bq25890_resume(struct device *dev)
1072 struct bq25890_device *bq = dev_get_drvdata(dev);
1074 mutex_lock(&bq->lock);
1076 ret = bq25890_get_chip_state(bq, &bq->state);
1080 /* Re-enable ADC only if charger is plugged in. */
1081 if (bq->state.online) {
1082 ret = bq25890_field_write(bq, F_CONV_RATE, 1);
1087 /* signal userspace, maybe state changed while suspended */
1088 power_supply_changed(bq->charger);
1091 mutex_unlock(&bq->lock);
1097 static const struct dev_pm_ops bq25890_pm = {
1098 SET_SYSTEM_SLEEP_PM_OPS(bq25890_suspend, bq25890_resume)
1101 static const struct i2c_device_id bq25890_i2c_ids[] = {
1108 MODULE_DEVICE_TABLE(i2c, bq25890_i2c_ids);
1110 static const struct of_device_id bq25890_of_match[] = {
1111 { .compatible = "ti,bq25890", },
1112 { .compatible = "ti,bq25892", },
1113 { .compatible = "ti,bq25895", },
1114 { .compatible = "ti,bq25896", },
1117 MODULE_DEVICE_TABLE(of, bq25890_of_match);
1120 static const struct acpi_device_id bq25890_acpi_match[] = {
1124 MODULE_DEVICE_TABLE(acpi, bq25890_acpi_match);
1127 static struct i2c_driver bq25890_driver = {
1129 .name = "bq25890-charger",
1130 .of_match_table = of_match_ptr(bq25890_of_match),
1131 .acpi_match_table = ACPI_PTR(bq25890_acpi_match),
1134 .probe = bq25890_probe,
1135 .remove = bq25890_remove,
1136 .id_table = bq25890_i2c_ids,
1138 module_i2c_driver(bq25890_driver);
1140 MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
1141 MODULE_DESCRIPTION("bq25890 charger driver");
1142 MODULE_LICENSE("GPL");