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/power/bq25890_charger.h>
12 #include <linux/regmap.h>
13 #include <linux/regulator/driver.h>
14 #include <linux/types.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/interrupt.h>
17 #include <linux/delay.h>
18 #include <linux/usb/phy.h>
20 #include <linux/acpi.h>
23 #define BQ25890_MANUFACTURER "Texas Instruments"
24 #define BQ25890_IRQ_PIN "bq25890_irq"
30 #define PUMP_EXPRESS_START_DELAY (5 * HZ)
31 #define PUMP_EXPRESS_MAX_TRIES 6
32 #define PUMP_EXPRESS_VBUS_MARGIN_uV 1000000
34 enum bq25890_chip_version
{
41 static const char *const bq25890_chip_name
[] = {
49 F_EN_HIZ
, F_EN_ILIM
, F_IINLIM
, /* Reg00 */
50 F_BHOT
, F_BCOLD
, F_VINDPM_OFS
, /* Reg01 */
51 F_CONV_START
, F_CONV_RATE
, F_BOOSTF
, F_ICO_EN
,
52 F_HVDCP_EN
, F_MAXC_EN
, F_FORCE_DPM
, F_AUTO_DPDM_EN
, /* Reg02 */
53 F_BAT_LOAD_EN
, F_WD_RST
, F_OTG_CFG
, F_CHG_CFG
, F_SYSVMIN
,
54 F_MIN_VBAT_SEL
, /* Reg03 */
55 F_PUMPX_EN
, F_ICHG
, /* Reg04 */
56 F_IPRECHG
, F_ITERM
, /* Reg05 */
57 F_VREG
, F_BATLOWV
, F_VRECHG
, /* Reg06 */
58 F_TERM_EN
, F_STAT_DIS
, F_WD
, F_TMR_EN
, F_CHG_TMR
,
59 F_JEITA_ISET
, /* Reg07 */
60 F_BATCMP
, F_VCLAMP
, F_TREG
, /* Reg08 */
61 F_FORCE_ICO
, F_TMR2X_EN
, F_BATFET_DIS
, F_JEITA_VSET
,
62 F_BATFET_DLY
, F_BATFET_RST_EN
, F_PUMPX_UP
, F_PUMPX_DN
, /* Reg09 */
63 F_BOOSTV
, F_PFM_OTG_DIS
, F_BOOSTI
, /* Reg0A */
64 F_VBUS_STAT
, F_CHG_STAT
, F_PG_STAT
, F_SDP_STAT
, F_0B_RSVD
,
65 F_VSYS_STAT
, /* Reg0B */
66 F_WD_FAULT
, F_BOOST_FAULT
, F_CHG_FAULT
, F_BAT_FAULT
,
67 F_NTC_FAULT
, /* Reg0C */
68 F_FORCE_VINDPM
, F_VINDPM
, /* Reg0D */
69 F_THERM_STAT
, F_BATV
, /* Reg0E */
72 F_VBUS_GD
, F_VBUSV
, /* Reg11 */
74 F_VDPM_STAT
, F_IDPM_STAT
, F_IDPM_LIM
, /* Reg13 */
75 F_REG_RST
, F_ICO_OPTIMIZED
, F_PN
, F_TS_PROFILE
, F_DEV_REV
, /* Reg14 */
80 /* initial field values, converted to register values */
81 struct bq25890_init_data
{
82 u8 ichg
; /* charge current */
83 u8 vreg
; /* regulation voltage */
84 u8 iterm
; /* termination current */
85 u8 iprechg
; /* precharge current */
86 u8 sysvmin
; /* minimum system voltage limit */
87 u8 boostv
; /* boost regulation voltage */
88 u8 boosti
; /* boost current limit */
89 u8 boostf
; /* boost frequency */
90 u8 ilim_en
; /* enable ILIM pin */
91 u8 treg
; /* thermal regulation threshold */
92 u8 rbatcomp
; /* IBAT sense resistor value */
93 u8 vclamp
; /* IBAT compensation voltage limit */
96 struct bq25890_state
{
107 struct bq25890_device
{
108 struct i2c_client
*client
;
110 struct power_supply
*charger
;
111 struct power_supply
*secondary_chrg
;
112 struct power_supply_desc desc
;
113 char name
[28]; /* "bq25890-charger-%d" */
116 struct usb_phy
*usb_phy
;
117 struct notifier_block usb_nb
;
118 struct work_struct usb_work
;
119 struct delayed_work pump_express_work
;
120 unsigned long usb_event
;
123 struct regmap_field
*rmap_fields
[F_MAX_FIELDS
];
126 bool read_back_init_data
;
128 u32 pump_express_vbus_max
;
129 u32 iinlim_percentage
;
130 enum bq25890_chip_version chip_version
;
131 struct bq25890_init_data init_data
;
132 struct bq25890_state state
;
134 struct mutex lock
; /* protect state data */
137 static DEFINE_IDR(bq25890_id
);
138 static DEFINE_MUTEX(bq25890_id_mutex
);
140 static const struct regmap_range bq25890_readonly_reg_ranges
[] = {
141 regmap_reg_range(0x0b, 0x0c),
142 regmap_reg_range(0x0e, 0x13),
145 static const struct regmap_access_table bq25890_writeable_regs
= {
146 .no_ranges
= bq25890_readonly_reg_ranges
,
147 .n_no_ranges
= ARRAY_SIZE(bq25890_readonly_reg_ranges
),
150 static const struct regmap_range bq25890_volatile_reg_ranges
[] = {
151 regmap_reg_range(0x00, 0x00),
152 regmap_reg_range(0x02, 0x02),
153 regmap_reg_range(0x09, 0x09),
154 regmap_reg_range(0x0b, 0x14),
157 static const struct regmap_access_table bq25890_volatile_regs
= {
158 .yes_ranges
= bq25890_volatile_reg_ranges
,
159 .n_yes_ranges
= ARRAY_SIZE(bq25890_volatile_reg_ranges
),
162 static const struct regmap_config bq25890_regmap_config
= {
166 .max_register
= 0x14,
167 .cache_type
= REGCACHE_RBTREE
,
169 .wr_table
= &bq25890_writeable_regs
,
170 .volatile_table
= &bq25890_volatile_regs
,
173 static const struct reg_field bq25890_reg_fields
[] = {
175 [F_EN_HIZ
] = REG_FIELD(0x00, 7, 7),
176 [F_EN_ILIM
] = REG_FIELD(0x00, 6, 6),
177 [F_IINLIM
] = REG_FIELD(0x00, 0, 5),
179 [F_BHOT
] = REG_FIELD(0x01, 6, 7),
180 [F_BCOLD
] = REG_FIELD(0x01, 5, 5),
181 [F_VINDPM_OFS
] = REG_FIELD(0x01, 0, 4),
183 [F_CONV_START
] = REG_FIELD(0x02, 7, 7),
184 [F_CONV_RATE
] = REG_FIELD(0x02, 6, 6),
185 [F_BOOSTF
] = REG_FIELD(0x02, 5, 5),
186 [F_ICO_EN
] = REG_FIELD(0x02, 4, 4),
187 [F_HVDCP_EN
] = REG_FIELD(0x02, 3, 3), // reserved on BQ25896
188 [F_MAXC_EN
] = REG_FIELD(0x02, 2, 2), // reserved on BQ25896
189 [F_FORCE_DPM
] = REG_FIELD(0x02, 1, 1),
190 [F_AUTO_DPDM_EN
] = REG_FIELD(0x02, 0, 0),
192 [F_BAT_LOAD_EN
] = REG_FIELD(0x03, 7, 7),
193 [F_WD_RST
] = REG_FIELD(0x03, 6, 6),
194 [F_OTG_CFG
] = REG_FIELD(0x03, 5, 5),
195 [F_CHG_CFG
] = REG_FIELD(0x03, 4, 4),
196 [F_SYSVMIN
] = REG_FIELD(0x03, 1, 3),
197 [F_MIN_VBAT_SEL
] = REG_FIELD(0x03, 0, 0), // BQ25896 only
199 [F_PUMPX_EN
] = REG_FIELD(0x04, 7, 7),
200 [F_ICHG
] = REG_FIELD(0x04, 0, 6),
202 [F_IPRECHG
] = REG_FIELD(0x05, 4, 7),
203 [F_ITERM
] = REG_FIELD(0x05, 0, 3),
205 [F_VREG
] = REG_FIELD(0x06, 2, 7),
206 [F_BATLOWV
] = REG_FIELD(0x06, 1, 1),
207 [F_VRECHG
] = REG_FIELD(0x06, 0, 0),
209 [F_TERM_EN
] = REG_FIELD(0x07, 7, 7),
210 [F_STAT_DIS
] = REG_FIELD(0x07, 6, 6),
211 [F_WD
] = REG_FIELD(0x07, 4, 5),
212 [F_TMR_EN
] = REG_FIELD(0x07, 3, 3),
213 [F_CHG_TMR
] = REG_FIELD(0x07, 1, 2),
214 [F_JEITA_ISET
] = REG_FIELD(0x07, 0, 0), // reserved on BQ25895
216 [F_BATCMP
] = REG_FIELD(0x08, 5, 7),
217 [F_VCLAMP
] = REG_FIELD(0x08, 2, 4),
218 [F_TREG
] = REG_FIELD(0x08, 0, 1),
220 [F_FORCE_ICO
] = REG_FIELD(0x09, 7, 7),
221 [F_TMR2X_EN
] = REG_FIELD(0x09, 6, 6),
222 [F_BATFET_DIS
] = REG_FIELD(0x09, 5, 5),
223 [F_JEITA_VSET
] = REG_FIELD(0x09, 4, 4), // reserved on BQ25895
224 [F_BATFET_DLY
] = REG_FIELD(0x09, 3, 3),
225 [F_BATFET_RST_EN
] = REG_FIELD(0x09, 2, 2),
226 [F_PUMPX_UP
] = REG_FIELD(0x09, 1, 1),
227 [F_PUMPX_DN
] = REG_FIELD(0x09, 0, 0),
229 [F_BOOSTV
] = REG_FIELD(0x0A, 4, 7),
230 [F_BOOSTI
] = REG_FIELD(0x0A, 0, 2), // reserved on BQ25895
231 [F_PFM_OTG_DIS
] = REG_FIELD(0x0A, 3, 3), // BQ25896 only
233 [F_VBUS_STAT
] = REG_FIELD(0x0B, 5, 7),
234 [F_CHG_STAT
] = REG_FIELD(0x0B, 3, 4),
235 [F_PG_STAT
] = REG_FIELD(0x0B, 2, 2),
236 [F_SDP_STAT
] = REG_FIELD(0x0B, 1, 1), // reserved on BQ25896
237 [F_VSYS_STAT
] = REG_FIELD(0x0B, 0, 0),
239 [F_WD_FAULT
] = REG_FIELD(0x0C, 7, 7),
240 [F_BOOST_FAULT
] = REG_FIELD(0x0C, 6, 6),
241 [F_CHG_FAULT
] = REG_FIELD(0x0C, 4, 5),
242 [F_BAT_FAULT
] = REG_FIELD(0x0C, 3, 3),
243 [F_NTC_FAULT
] = REG_FIELD(0x0C, 0, 2),
245 [F_FORCE_VINDPM
] = REG_FIELD(0x0D, 7, 7),
246 [F_VINDPM
] = REG_FIELD(0x0D, 0, 6),
248 [F_THERM_STAT
] = REG_FIELD(0x0E, 7, 7),
249 [F_BATV
] = REG_FIELD(0x0E, 0, 6),
251 [F_SYSV
] = REG_FIELD(0x0F, 0, 6),
253 [F_TSPCT
] = REG_FIELD(0x10, 0, 6),
255 [F_VBUS_GD
] = REG_FIELD(0x11, 7, 7),
256 [F_VBUSV
] = REG_FIELD(0x11, 0, 6),
258 [F_ICHGR
] = REG_FIELD(0x12, 0, 6),
260 [F_VDPM_STAT
] = REG_FIELD(0x13, 7, 7),
261 [F_IDPM_STAT
] = REG_FIELD(0x13, 6, 6),
262 [F_IDPM_LIM
] = REG_FIELD(0x13, 0, 5),
264 [F_REG_RST
] = REG_FIELD(0x14, 7, 7),
265 [F_ICO_OPTIMIZED
] = REG_FIELD(0x14, 6, 6),
266 [F_PN
] = REG_FIELD(0x14, 3, 5),
267 [F_TS_PROFILE
] = REG_FIELD(0x14, 2, 2),
268 [F_DEV_REV
] = REG_FIELD(0x14, 0, 1)
272 * Most of the val -> idx conversions can be computed, given the minimum,
273 * maximum and the step between values. For the rest of conversions, we use
276 enum bq25890_table_ids
{
294 /* Thermal Regulation Threshold lookup table, in degrees Celsius */
295 static const u32 bq25890_treg_tbl
[] = { 60, 80, 100, 120 };
297 #define BQ25890_TREG_TBL_SIZE ARRAY_SIZE(bq25890_treg_tbl)
299 /* Boost mode current limit lookup table, in uA */
300 static const u32 bq25890_boosti_tbl
[] = {
301 500000, 700000, 1100000, 1300000, 1600000, 1800000, 2100000, 2400000
304 #define BQ25890_BOOSTI_TBL_SIZE ARRAY_SIZE(bq25890_boosti_tbl)
306 /* NTC 10K temperature lookup table in tenths of a degree */
307 static const u32 bq25890_tspct_tbl
[] = {
308 850, 840, 830, 820, 810, 800, 790, 780,
309 770, 760, 750, 740, 730, 720, 710, 700,
310 690, 685, 680, 675, 670, 660, 650, 645,
311 640, 630, 620, 615, 610, 600, 590, 585,
312 580, 570, 565, 560, 550, 540, 535, 530,
313 520, 515, 510, 500, 495, 490, 480, 475,
314 470, 460, 455, 450, 440, 435, 430, 425,
315 420, 410, 405, 400, 390, 385, 380, 370,
316 365, 360, 355, 350, 340, 335, 330, 320,
317 310, 305, 300, 290, 285, 280, 275, 270,
318 260, 250, 245, 240, 230, 225, 220, 210,
319 205, 200, 190, 180, 175, 170, 160, 150,
320 145, 140, 130, 120, 115, 110, 100, 90,
321 80, 70, 60, 50, 40, 30, 20, 10,
322 0, -10, -20, -30, -40, -60, -70, -80,
323 -90, -10, -120, -140, -150, -170, -190, -210,
326 #define BQ25890_TSPCT_TBL_SIZE ARRAY_SIZE(bq25890_tspct_tbl)
328 struct bq25890_range
{
334 struct bq25890_lookup
{
340 struct bq25890_range rt
;
341 struct bq25890_lookup lt
;
342 } bq25890_tables
[] = {
344 /* TODO: BQ25896 has max ICHG 3008 mA */
345 [TBL_ICHG
] = { .rt
= {0, 5056000, 64000} }, /* uA */
346 [TBL_ITERM
] = { .rt
= {64000, 1024000, 64000} }, /* uA */
347 [TBL_IINLIM
] = { .rt
= {100000, 3250000, 50000} }, /* uA */
348 [TBL_VREG
] = { .rt
= {3840000, 4608000, 16000} }, /* uV */
349 [TBL_BOOSTV
] = { .rt
= {4550000, 5510000, 64000} }, /* uV */
350 [TBL_SYSVMIN
] = { .rt
= {3000000, 3700000, 100000} }, /* uV */
351 [TBL_VBUSV
] = { .rt
= {2600000, 15300000, 100000} }, /* uV */
352 [TBL_VBATCOMP
] = { .rt
= {0, 224000, 32000} }, /* uV */
353 [TBL_RBATCOMP
] = { .rt
= {0, 140000, 20000} }, /* uOhm */
356 [TBL_TREG
] = { .lt
= {bq25890_treg_tbl
, BQ25890_TREG_TBL_SIZE
} },
357 [TBL_BOOSTI
] = { .lt
= {bq25890_boosti_tbl
, BQ25890_BOOSTI_TBL_SIZE
} },
358 [TBL_TSPCT
] = { .lt
= {bq25890_tspct_tbl
, BQ25890_TSPCT_TBL_SIZE
} }
361 static int bq25890_field_read(struct bq25890_device
*bq
,
362 enum bq25890_fields field_id
)
367 ret
= regmap_field_read(bq
->rmap_fields
[field_id
], &val
);
374 static int bq25890_field_write(struct bq25890_device
*bq
,
375 enum bq25890_fields field_id
, u8 val
)
377 return regmap_field_write(bq
->rmap_fields
[field_id
], val
);
380 static u8
bq25890_find_idx(u32 value
, enum bq25890_table_ids id
)
384 if (id
>= TBL_TREG
) {
385 const u32
*tbl
= bq25890_tables
[id
].lt
.tbl
;
386 u32 tbl_size
= bq25890_tables
[id
].lt
.size
;
388 for (idx
= 1; idx
< tbl_size
&& tbl
[idx
] <= value
; idx
++)
391 const struct bq25890_range
*rtbl
= &bq25890_tables
[id
].rt
;
394 rtbl_size
= (rtbl
->max
- rtbl
->min
) / rtbl
->step
+ 1;
397 idx
< rtbl_size
&& (idx
* rtbl
->step
+ rtbl
->min
<= value
);
405 static u32
bq25890_find_val(u8 idx
, enum bq25890_table_ids id
)
407 const struct bq25890_range
*rtbl
;
411 return bq25890_tables
[id
].lt
.tbl
[idx
];
414 rtbl
= &bq25890_tables
[id
].rt
;
416 return (rtbl
->min
+ idx
* rtbl
->step
);
419 enum bq25890_status
{
422 STATUS_FAST_CHARGING
,
423 STATUS_TERMINATION_DONE
,
426 enum bq25890_chrg_fault
{
429 CHRG_FAULT_THERMAL_SHUTDOWN
,
430 CHRG_FAULT_TIMER_EXPIRED
,
433 enum bq25890_ntc_fault
{
434 NTC_FAULT_NORMAL
= 0,
441 static bool bq25890_is_adc_property(enum power_supply_property psp
)
444 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
445 case POWER_SUPPLY_PROP_CURRENT_NOW
:
446 case POWER_SUPPLY_PROP_TEMP
:
454 static irqreturn_t
__bq25890_handle_irq(struct bq25890_device
*bq
);
456 static int bq25890_get_vbus_voltage(struct bq25890_device
*bq
)
460 ret
= bq25890_field_read(bq
, F_VBUSV
);
464 return bq25890_find_val(ret
, TBL_VBUSV
);
467 static void bq25890_update_state(struct bq25890_device
*bq
,
468 enum power_supply_property psp
,
469 struct bq25890_state
*state
)
474 mutex_lock(&bq
->lock
);
475 /* update state in case we lost an interrupt */
476 __bq25890_handle_irq(bq
);
478 do_adc_conv
= (!state
->online
|| state
->hiz
) && bq25890_is_adc_property(psp
);
480 bq25890_field_write(bq
, F_CONV_START
, 1);
481 mutex_unlock(&bq
->lock
);
484 regmap_field_read_poll_timeout(bq
->rmap_fields
[F_CONV_START
],
485 ret
, !ret
, 25000, 1000000);
488 static int bq25890_power_supply_get_property(struct power_supply
*psy
,
489 enum power_supply_property psp
,
490 union power_supply_propval
*val
)
492 struct bq25890_device
*bq
= power_supply_get_drvdata(psy
);
493 struct bq25890_state state
;
496 bq25890_update_state(bq
, psp
, &state
);
499 case POWER_SUPPLY_PROP_STATUS
:
500 if (!state
.online
|| state
.hiz
)
501 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
502 else if (state
.chrg_status
== STATUS_NOT_CHARGING
)
503 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
504 else if (state
.chrg_status
== STATUS_PRE_CHARGING
||
505 state
.chrg_status
== STATUS_FAST_CHARGING
)
506 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
507 else if (state
.chrg_status
== STATUS_TERMINATION_DONE
)
508 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
510 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
514 case POWER_SUPPLY_PROP_CHARGE_TYPE
:
515 if (!state
.online
|| state
.hiz
||
516 state
.chrg_status
== STATUS_NOT_CHARGING
||
517 state
.chrg_status
== STATUS_TERMINATION_DONE
)
518 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_NONE
;
519 else if (state
.chrg_status
== STATUS_PRE_CHARGING
)
520 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_STANDARD
;
521 else if (state
.chrg_status
== STATUS_FAST_CHARGING
)
522 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_FAST
;
523 else /* unreachable */
524 val
->intval
= POWER_SUPPLY_CHARGE_TYPE_UNKNOWN
;
527 case POWER_SUPPLY_PROP_MANUFACTURER
:
528 val
->strval
= BQ25890_MANUFACTURER
;
531 case POWER_SUPPLY_PROP_MODEL_NAME
:
532 val
->strval
= bq25890_chip_name
[bq
->chip_version
];
535 case POWER_SUPPLY_PROP_ONLINE
:
536 val
->intval
= state
.online
&& !state
.hiz
;
539 case POWER_SUPPLY_PROP_HEALTH
:
540 if (!state
.chrg_fault
&& !state
.bat_fault
&& !state
.boost_fault
)
541 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
542 else if (state
.bat_fault
)
543 val
->intval
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
544 else if (state
.chrg_fault
== CHRG_FAULT_TIMER_EXPIRED
)
545 val
->intval
= POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE
;
546 else if (state
.chrg_fault
== CHRG_FAULT_THERMAL_SHUTDOWN
)
547 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
549 val
->intval
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
552 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT
:
553 val
->intval
= bq25890_find_val(bq
->init_data
.iprechg
, TBL_ITERM
);
556 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT
:
557 val
->intval
= bq25890_find_val(bq
->init_data
.iterm
, TBL_ITERM
);
560 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
561 ret
= bq25890_field_read(bq
, F_IINLIM
);
565 val
->intval
= bq25890_find_val(ret
, TBL_IINLIM
);
568 case POWER_SUPPLY_PROP_CURRENT_NOW
: /* I_BAT now */
570 * This is ADC-sampled immediate charge current supplied
571 * from charger to battery. The property name is confusing,
572 * for clarification refer to:
573 * Documentation/ABI/testing/sysfs-class-power
574 * /sys/class/power_supply/<supply_name>/current_now
576 ret
= bq25890_field_read(bq
, F_ICHGR
); /* read measured value */
580 /* converted_val = ADC_val * 50mA (table 10.3.19) */
581 val
->intval
= ret
* -50000;
584 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
: /* I_BAT user limit */
586 * This is user-configured constant charge current supplied
587 * from charger to battery in first phase of charging, when
588 * battery voltage is below constant charge voltage.
590 * This value reflects the current hardware setting.
592 * The POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX is the
593 * maximum value of this property.
595 ret
= bq25890_field_read(bq
, F_ICHG
);
598 val
->intval
= bq25890_find_val(ret
, TBL_ICHG
);
600 /* When temperature is too low, charge current is decreased */
601 if (bq
->state
.ntc_fault
== NTC_FAULT_COOL
) {
602 ret
= bq25890_field_read(bq
, F_JEITA_ISET
);
613 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
: /* I_BAT max */
615 * This is maximum allowed constant charge current supplied
616 * from charger to battery in first phase of charging, when
617 * battery voltage is below constant charge voltage.
619 * This value is constant for each battery and set from DT.
621 val
->intval
= bq25890_find_val(bq
->init_data
.ichg
, TBL_ICHG
);
624 case POWER_SUPPLY_PROP_VOLTAGE_NOW
: /* V_BAT now */
626 * This is ADC-sampled immediate charge voltage supplied
627 * from charger to battery. The property name is confusing,
628 * for clarification refer to:
629 * Documentation/ABI/testing/sysfs-class-power
630 * /sys/class/power_supply/<supply_name>/voltage_now
632 ret
= bq25890_field_read(bq
, F_BATV
); /* read measured value */
636 /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
637 val
->intval
= 2304000 + ret
* 20000;
640 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
: /* V_BAT user limit */
642 * This is user-configured constant charge voltage supplied
643 * from charger to battery in second phase of charging, when
644 * battery voltage reached constant charge voltage.
646 * This value reflects the current hardware setting.
648 * The POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX is the
649 * maximum value of this property.
651 ret
= bq25890_field_read(bq
, F_VREG
);
655 val
->intval
= bq25890_find_val(ret
, TBL_VREG
);
658 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
: /* V_BAT max */
660 * This is maximum allowed constant charge voltage supplied
661 * from charger to battery in second phase of charging, when
662 * battery voltage reached constant charge voltage.
664 * This value is constant for each battery and set from DT.
666 val
->intval
= bq25890_find_val(bq
->init_data
.vreg
, TBL_VREG
);
669 case POWER_SUPPLY_PROP_TEMP
:
670 ret
= bq25890_field_read(bq
, F_TSPCT
);
674 /* convert TS percentage into rough temperature */
675 val
->intval
= bq25890_find_val(ret
, TBL_TSPCT
);
685 static int bq25890_power_supply_set_property(struct power_supply
*psy
,
686 enum power_supply_property psp
,
687 const union power_supply_propval
*val
)
689 struct bq25890_device
*bq
= power_supply_get_drvdata(psy
);
690 struct bq25890_state state
;
695 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
696 maxval
= bq25890_find_val(bq
->init_data
.ichg
, TBL_ICHG
);
697 lval
= bq25890_find_idx(min(val
->intval
, maxval
), TBL_ICHG
);
698 return bq25890_field_write(bq
, F_ICHG
, lval
);
699 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
700 maxval
= bq25890_find_val(bq
->init_data
.vreg
, TBL_VREG
);
701 lval
= bq25890_find_idx(min(val
->intval
, maxval
), TBL_VREG
);
702 return bq25890_field_write(bq
, F_VREG
, lval
);
703 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
704 lval
= bq25890_find_idx(val
->intval
, TBL_IINLIM
);
705 return bq25890_field_write(bq
, F_IINLIM
, lval
);
706 case POWER_SUPPLY_PROP_ONLINE
:
707 ret
= bq25890_field_write(bq
, F_EN_HIZ
, !val
->intval
);
709 bq
->force_hiz
= !val
->intval
;
710 bq25890_update_state(bq
, psp
, &state
);
717 static int bq25890_power_supply_property_is_writeable(struct power_supply
*psy
,
718 enum power_supply_property psp
)
721 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
722 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
723 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
724 case POWER_SUPPLY_PROP_ONLINE
:
732 * If there are multiple chargers the maximum current the external power-supply
733 * can deliver needs to be divided over the chargers. This is done according
734 * to the bq->iinlim_percentage setting.
736 static int bq25890_charger_get_scaled_iinlim_regval(struct bq25890_device
*bq
,
739 iinlim_ua
= iinlim_ua
* bq
->iinlim_percentage
/ 100;
740 return bq25890_find_idx(iinlim_ua
, TBL_IINLIM
);
743 /* On the BQ25892 try to get charger-type info from our supplier */
744 static void bq25890_charger_external_power_changed(struct power_supply
*psy
)
746 struct bq25890_device
*bq
= power_supply_get_drvdata(psy
);
747 union power_supply_propval val
;
748 int input_current_limit
, ret
;
750 if (bq
->chip_version
!= BQ25892
)
753 ret
= power_supply_get_property_from_supplier(psy
,
754 POWER_SUPPLY_PROP_USB_TYPE
,
759 switch (val
.intval
) {
760 case POWER_SUPPLY_USB_TYPE_DCP
:
761 input_current_limit
= bq25890_charger_get_scaled_iinlim_regval(bq
, 2000000);
762 if (bq
->pump_express_vbus_max
) {
763 queue_delayed_work(system_power_efficient_wq
,
764 &bq
->pump_express_work
,
765 PUMP_EXPRESS_START_DELAY
);
768 case POWER_SUPPLY_USB_TYPE_CDP
:
769 case POWER_SUPPLY_USB_TYPE_ACA
:
770 input_current_limit
= bq25890_charger_get_scaled_iinlim_regval(bq
, 1500000);
772 case POWER_SUPPLY_USB_TYPE_SDP
:
774 input_current_limit
= bq25890_charger_get_scaled_iinlim_regval(bq
, 500000);
777 bq25890_field_write(bq
, F_IINLIM
, input_current_limit
);
778 power_supply_changed(psy
);
781 static int bq25890_get_chip_state(struct bq25890_device
*bq
,
782 struct bq25890_state
*state
)
787 enum bq25890_fields id
;
790 {F_CHG_STAT
, &state
->chrg_status
},
791 {F_PG_STAT
, &state
->online
},
792 {F_EN_HIZ
, &state
->hiz
},
793 {F_VSYS_STAT
, &state
->vsys_status
},
794 {F_BOOST_FAULT
, &state
->boost_fault
},
795 {F_BAT_FAULT
, &state
->bat_fault
},
796 {F_CHG_FAULT
, &state
->chrg_fault
},
797 {F_NTC_FAULT
, &state
->ntc_fault
}
800 for (i
= 0; i
< ARRAY_SIZE(state_fields
); i
++) {
801 ret
= bq25890_field_read(bq
, state_fields
[i
].id
);
805 *state_fields
[i
].data
= ret
;
808 dev_dbg(bq
->dev
, "S:CHG/PG/HIZ/VSYS=%d/%d/%d/%d, F:CHG/BOOST/BAT/NTC=%d/%d/%d/%d\n",
809 state
->chrg_status
, state
->online
,
810 state
->hiz
, state
->vsys_status
,
811 state
->chrg_fault
, state
->boost_fault
,
812 state
->bat_fault
, state
->ntc_fault
);
817 static irqreturn_t
__bq25890_handle_irq(struct bq25890_device
*bq
)
819 bool adc_conv_rate
, new_adc_conv_rate
;
820 struct bq25890_state new_state
;
823 ret
= bq25890_get_chip_state(bq
, &new_state
);
827 if (!memcmp(&bq
->state
, &new_state
, sizeof(new_state
)))
831 * Restore HiZ bit in case it was set by user. The chip does not retain
832 * this bit on cable replug, hence the bit must be reset manually here.
834 if (new_state
.online
&& !bq
->state
.online
&& bq
->force_hiz
) {
835 ret
= bq25890_field_write(bq
, F_EN_HIZ
, bq
->force_hiz
);
841 /* Should period ADC sampling be enabled? */
842 adc_conv_rate
= bq
->state
.online
&& !bq
->state
.hiz
;
843 new_adc_conv_rate
= new_state
.online
&& !new_state
.hiz
;
845 if (new_adc_conv_rate
!= adc_conv_rate
) {
846 ret
= bq25890_field_write(bq
, F_CONV_RATE
, new_adc_conv_rate
);
851 bq
->state
= new_state
;
852 power_supply_changed(bq
->charger
);
856 dev_err(bq
->dev
, "Error communicating with the chip: %pe\n",
861 static irqreturn_t
bq25890_irq_handler_thread(int irq
, void *private)
863 struct bq25890_device
*bq
= private;
866 mutex_lock(&bq
->lock
);
867 ret
= __bq25890_handle_irq(bq
);
868 mutex_unlock(&bq
->lock
);
873 static int bq25890_chip_reset(struct bq25890_device
*bq
)
876 int rst_check_counter
= 10;
878 ret
= bq25890_field_write(bq
, F_REG_RST
, 1);
883 ret
= bq25890_field_read(bq
, F_REG_RST
);
888 } while (ret
== 1 && --rst_check_counter
);
890 if (!rst_check_counter
)
896 static int bq25890_rw_init_data(struct bq25890_device
*bq
)
898 bool write
= !bq
->read_back_init_data
;
903 enum bq25890_fields id
;
906 {F_ICHG
, &bq
->init_data
.ichg
},
907 {F_VREG
, &bq
->init_data
.vreg
},
908 {F_ITERM
, &bq
->init_data
.iterm
},
909 {F_IPRECHG
, &bq
->init_data
.iprechg
},
910 {F_SYSVMIN
, &bq
->init_data
.sysvmin
},
911 {F_BOOSTV
, &bq
->init_data
.boostv
},
912 {F_BOOSTI
, &bq
->init_data
.boosti
},
913 {F_BOOSTF
, &bq
->init_data
.boostf
},
914 {F_EN_ILIM
, &bq
->init_data
.ilim_en
},
915 {F_TREG
, &bq
->init_data
.treg
},
916 {F_BATCMP
, &bq
->init_data
.rbatcomp
},
917 {F_VCLAMP
, &bq
->init_data
.vclamp
},
920 for (i
= 0; i
< ARRAY_SIZE(init_data
); i
++) {
922 ret
= bq25890_field_write(bq
, init_data
[i
].id
,
923 *init_data
[i
].value
);
925 ret
= bq25890_field_read(bq
, init_data
[i
].id
);
927 *init_data
[i
].value
= ret
;
930 dev_dbg(bq
->dev
, "Accessing init data failed %d\n", ret
);
938 static int bq25890_hw_init(struct bq25890_device
*bq
)
942 if (!bq
->skip_reset
) {
943 ret
= bq25890_chip_reset(bq
);
945 dev_dbg(bq
->dev
, "Reset failed %d\n", ret
);
950 * Ensure charging is enabled, on some boards where the fw
951 * takes care of initalizition F_CHG_CFG is set to 0 before
952 * handing control over to the OS.
954 ret
= bq25890_field_write(bq
, F_CHG_CFG
, 1);
956 dev_dbg(bq
->dev
, "Enabling charging failed %d\n", ret
);
961 /* disable watchdog */
962 ret
= bq25890_field_write(bq
, F_WD
, 0);
964 dev_dbg(bq
->dev
, "Disabling watchdog failed %d\n", ret
);
968 /* initialize currents/voltages and other parameters */
969 ret
= bq25890_rw_init_data(bq
);
973 ret
= bq25890_get_chip_state(bq
, &bq
->state
);
975 dev_dbg(bq
->dev
, "Get state failed %d\n", ret
);
979 /* Configure ADC for continuous conversions when charging */
980 ret
= bq25890_field_write(bq
, F_CONV_RATE
, bq
->state
.online
&& !bq
->state
.hiz
);
982 dev_dbg(bq
->dev
, "Config ADC failed %d\n", ret
);
989 static const enum power_supply_property bq25890_power_supply_props
[] = {
990 POWER_SUPPLY_PROP_MANUFACTURER
,
991 POWER_SUPPLY_PROP_MODEL_NAME
,
992 POWER_SUPPLY_PROP_STATUS
,
993 POWER_SUPPLY_PROP_CHARGE_TYPE
,
994 POWER_SUPPLY_PROP_ONLINE
,
995 POWER_SUPPLY_PROP_HEALTH
,
996 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
,
997 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
,
998 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
,
999 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
,
1000 POWER_SUPPLY_PROP_PRECHARGE_CURRENT
,
1001 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT
,
1002 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
,
1003 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
1004 POWER_SUPPLY_PROP_CURRENT_NOW
,
1005 POWER_SUPPLY_PROP_TEMP
,
1008 static char *bq25890_charger_supplied_to
[] = {
1012 static const struct power_supply_desc bq25890_power_supply_desc
= {
1013 .type
= POWER_SUPPLY_TYPE_USB
,
1014 .properties
= bq25890_power_supply_props
,
1015 .num_properties
= ARRAY_SIZE(bq25890_power_supply_props
),
1016 .get_property
= bq25890_power_supply_get_property
,
1017 .set_property
= bq25890_power_supply_set_property
,
1018 .property_is_writeable
= bq25890_power_supply_property_is_writeable
,
1019 .external_power_changed
= bq25890_charger_external_power_changed
,
1022 static int bq25890_power_supply_init(struct bq25890_device
*bq
)
1024 struct power_supply_config psy_cfg
= { .drv_data
= bq
, };
1026 /* Get ID for the device */
1027 mutex_lock(&bq25890_id_mutex
);
1028 bq
->id
= idr_alloc(&bq25890_id
, bq
, 0, 0, GFP_KERNEL
);
1029 mutex_unlock(&bq25890_id_mutex
);
1033 snprintf(bq
->name
, sizeof(bq
->name
), "bq25890-charger-%d", bq
->id
);
1034 bq
->desc
= bq25890_power_supply_desc
;
1035 bq
->desc
.name
= bq
->name
;
1037 psy_cfg
.supplied_to
= bq25890_charger_supplied_to
;
1038 psy_cfg
.num_supplicants
= ARRAY_SIZE(bq25890_charger_supplied_to
);
1040 bq
->charger
= devm_power_supply_register(bq
->dev
, &bq
->desc
, &psy_cfg
);
1042 return PTR_ERR_OR_ZERO(bq
->charger
);
1045 static int bq25890_set_otg_cfg(struct bq25890_device
*bq
, u8 val
)
1049 ret
= bq25890_field_write(bq
, F_OTG_CFG
, val
);
1051 dev_err(bq
->dev
, "Error switching to boost/charger mode: %d\n", ret
);
1056 static void bq25890_pump_express_work(struct work_struct
*data
)
1058 struct bq25890_device
*bq
=
1059 container_of(data
, struct bq25890_device
, pump_express_work
.work
);
1060 union power_supply_propval value
;
1061 int voltage
, i
, ret
;
1063 dev_dbg(bq
->dev
, "Start to request input voltage increasing\n");
1065 /* If there is a second charger put in Hi-Z mode */
1066 if (bq
->secondary_chrg
) {
1068 power_supply_set_property(bq
->secondary_chrg
, POWER_SUPPLY_PROP_ONLINE
, &value
);
1071 /* Enable current pulse voltage control protocol */
1072 ret
= bq25890_field_write(bq
, F_PUMPX_EN
, 1);
1076 for (i
= 0; i
< PUMP_EXPRESS_MAX_TRIES
; i
++) {
1077 voltage
= bq25890_get_vbus_voltage(bq
);
1080 dev_dbg(bq
->dev
, "input voltage = %d uV\n", voltage
);
1082 if ((voltage
+ PUMP_EXPRESS_VBUS_MARGIN_uV
) >
1083 bq
->pump_express_vbus_max
)
1086 ret
= bq25890_field_write(bq
, F_PUMPX_UP
, 1);
1090 /* Note a single PUMPX up pulse-sequence takes 2.1s */
1091 ret
= regmap_field_read_poll_timeout(bq
->rmap_fields
[F_PUMPX_UP
],
1092 ret
, !ret
, 100000, 3000000);
1096 /* Make sure ADC has sampled Vbus before checking again */
1100 bq25890_field_write(bq
, F_PUMPX_EN
, 0);
1102 if (bq
->secondary_chrg
) {
1104 power_supply_set_property(bq
->secondary_chrg
, POWER_SUPPLY_PROP_ONLINE
, &value
);
1107 dev_info(bq
->dev
, "Hi-voltage charging requested, input voltage is %d mV\n",
1110 power_supply_changed(bq
->charger
);
1114 bq25890_field_write(bq
, F_PUMPX_EN
, 0);
1115 dev_err(bq
->dev
, "Failed to request hi-voltage charging\n");
1118 static void bq25890_usb_work(struct work_struct
*data
)
1121 struct bq25890_device
*bq
=
1122 container_of(data
, struct bq25890_device
, usb_work
);
1124 switch (bq
->usb_event
) {
1126 /* Enable boost mode */
1127 bq25890_set_otg_cfg(bq
, 1);
1130 case USB_EVENT_NONE
:
1131 /* Disable boost mode */
1132 ret
= bq25890_set_otg_cfg(bq
, 0);
1134 power_supply_changed(bq
->charger
);
1139 static int bq25890_usb_notifier(struct notifier_block
*nb
, unsigned long val
,
1142 struct bq25890_device
*bq
=
1143 container_of(nb
, struct bq25890_device
, usb_nb
);
1145 bq
->usb_event
= val
;
1146 queue_work(system_power_efficient_wq
, &bq
->usb_work
);
1151 #ifdef CONFIG_REGULATOR
1152 static int bq25890_vbus_enable(struct regulator_dev
*rdev
)
1154 struct bq25890_device
*bq
= rdev_get_drvdata(rdev
);
1155 union power_supply_propval val
= {
1160 * When enabling 5V boost / Vbus output, we need to put the secondary
1161 * charger in Hi-Z mode to avoid it trying to charge the secondary
1162 * battery from the 5V boost output.
1164 if (bq
->secondary_chrg
)
1165 power_supply_set_property(bq
->secondary_chrg
, POWER_SUPPLY_PROP_ONLINE
, &val
);
1167 return bq25890_set_otg_cfg(bq
, 1);
1170 static int bq25890_vbus_disable(struct regulator_dev
*rdev
)
1172 struct bq25890_device
*bq
= rdev_get_drvdata(rdev
);
1173 union power_supply_propval val
= {
1178 ret
= bq25890_set_otg_cfg(bq
, 0);
1182 if (bq
->secondary_chrg
)
1183 power_supply_set_property(bq
->secondary_chrg
, POWER_SUPPLY_PROP_ONLINE
, &val
);
1188 static int bq25890_vbus_is_enabled(struct regulator_dev
*rdev
)
1190 struct bq25890_device
*bq
= rdev_get_drvdata(rdev
);
1192 return bq25890_field_read(bq
, F_OTG_CFG
);
1195 static int bq25890_vbus_get_voltage(struct regulator_dev
*rdev
)
1197 struct bq25890_device
*bq
= rdev_get_drvdata(rdev
);
1199 return bq25890_get_vbus_voltage(bq
);
1202 static int bq25890_vsys_get_voltage(struct regulator_dev
*rdev
)
1204 struct bq25890_device
*bq
= rdev_get_drvdata(rdev
);
1207 /* Should be some output voltage ? */
1208 ret
= bq25890_field_read(bq
, F_SYSV
); /* read measured value */
1212 /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
1213 return 2304000 + ret
* 20000;
1216 static const struct regulator_ops bq25890_vbus_ops
= {
1217 .enable
= bq25890_vbus_enable
,
1218 .disable
= bq25890_vbus_disable
,
1219 .is_enabled
= bq25890_vbus_is_enabled
,
1220 .get_voltage
= bq25890_vbus_get_voltage
,
1223 static const struct regulator_desc bq25890_vbus_desc
= {
1224 .name
= "usb_otg_vbus",
1225 .of_match
= "usb-otg-vbus",
1226 .type
= REGULATOR_VOLTAGE
,
1227 .owner
= THIS_MODULE
,
1228 .ops
= &bq25890_vbus_ops
,
1231 static const struct regulator_ops bq25890_vsys_ops
= {
1232 .get_voltage
= bq25890_vsys_get_voltage
,
1235 static const struct regulator_desc bq25890_vsys_desc
= {
1238 .type
= REGULATOR_VOLTAGE
,
1239 .owner
= THIS_MODULE
,
1240 .ops
= &bq25890_vsys_ops
,
1243 static int bq25890_register_regulator(struct bq25890_device
*bq
)
1245 struct bq25890_platform_data
*pdata
= dev_get_platdata(bq
->dev
);
1246 struct regulator_config cfg
= {
1250 struct regulator_dev
*reg
;
1253 cfg
.init_data
= pdata
->regulator_init_data
;
1255 reg
= devm_regulator_register(bq
->dev
, &bq25890_vbus_desc
, &cfg
);
1257 return dev_err_probe(bq
->dev
, PTR_ERR(reg
),
1258 "registering vbus regulator");
1261 /* pdata->regulator_init_data is for vbus only */
1262 cfg
.init_data
= NULL
;
1263 reg
= devm_regulator_register(bq
->dev
, &bq25890_vsys_desc
, &cfg
);
1265 return dev_err_probe(bq
->dev
, PTR_ERR(reg
),
1266 "registering vsys regulator");
1273 bq25890_register_regulator(struct bq25890_device
*bq
)
1279 static int bq25890_get_chip_version(struct bq25890_device
*bq
)
1283 id
= bq25890_field_read(bq
, F_PN
);
1285 dev_err(bq
->dev
, "Cannot read chip ID: %d\n", id
);
1289 rev
= bq25890_field_read(bq
, F_DEV_REV
);
1291 dev_err(bq
->dev
, "Cannot read chip revision: %d\n", rev
);
1297 bq
->chip_version
= BQ25890
;
1300 /* BQ25892 and BQ25896 share same ID 0 */
1304 bq
->chip_version
= BQ25896
;
1307 bq
->chip_version
= BQ25892
;
1311 "Unknown device revision %d, assume BQ25892\n",
1313 bq
->chip_version
= BQ25892
;
1318 bq
->chip_version
= BQ25895
;
1322 dev_err(bq
->dev
, "Unknown chip ID %d\n", id
);
1329 static int bq25890_irq_probe(struct bq25890_device
*bq
)
1331 struct gpio_desc
*irq
;
1333 irq
= devm_gpiod_get(bq
->dev
, BQ25890_IRQ_PIN
, GPIOD_IN
);
1335 return dev_err_probe(bq
->dev
, PTR_ERR(irq
),
1336 "Could not probe irq pin.\n");
1338 return gpiod_to_irq(irq
);
1341 static int bq25890_fw_read_u32_props(struct bq25890_device
*bq
)
1346 struct bq25890_init_data
*init
= &bq
->init_data
;
1350 enum bq25890_table_ids tbl_id
;
1351 u8
*conv_data
; /* holds converted value from given property */
1353 /* required properties */
1354 {"ti,charge-current", false, TBL_ICHG
, &init
->ichg
},
1355 {"ti,battery-regulation-voltage", false, TBL_VREG
, &init
->vreg
},
1356 {"ti,termination-current", false, TBL_ITERM
, &init
->iterm
},
1357 {"ti,precharge-current", false, TBL_ITERM
, &init
->iprechg
},
1358 {"ti,minimum-sys-voltage", false, TBL_SYSVMIN
, &init
->sysvmin
},
1359 {"ti,boost-voltage", false, TBL_BOOSTV
, &init
->boostv
},
1360 {"ti,boost-max-current", false, TBL_BOOSTI
, &init
->boosti
},
1362 /* optional properties */
1363 {"ti,thermal-regulation-threshold", true, TBL_TREG
, &init
->treg
},
1364 {"ti,ibatcomp-micro-ohms", true, TBL_RBATCOMP
, &init
->rbatcomp
},
1365 {"ti,ibatcomp-clamp-microvolt", true, TBL_VBATCOMP
, &init
->vclamp
},
1368 /* initialize data for optional properties */
1369 init
->treg
= 3; /* 120 degrees Celsius */
1370 init
->rbatcomp
= init
->vclamp
= 0; /* IBAT compensation disabled */
1372 for (i
= 0; i
< ARRAY_SIZE(props
); i
++) {
1373 ret
= device_property_read_u32(bq
->dev
, props
[i
].name
,
1376 if (props
[i
].optional
)
1379 dev_err(bq
->dev
, "Unable to read property %d %s\n", ret
,
1385 *props
[i
].conv_data
= bq25890_find_idx(property
,
1392 static int bq25890_fw_probe(struct bq25890_device
*bq
)
1395 struct bq25890_init_data
*init
= &bq
->init_data
;
1399 ret
= device_property_read_string(bq
->dev
, "linux,secondary-charger-name", &str
);
1401 bq
->secondary_chrg
= power_supply_get_by_name(str
);
1402 if (!bq
->secondary_chrg
)
1403 return -EPROBE_DEFER
;
1406 /* Optional, left at 0 if property is not present */
1407 device_property_read_u32(bq
->dev
, "linux,pump-express-vbus-max",
1408 &bq
->pump_express_vbus_max
);
1410 ret
= device_property_read_u32(bq
->dev
, "linux,iinlim-percentage", &val
);
1413 dev_err(bq
->dev
, "Error linux,iinlim-percentage %u > 100\n", val
);
1416 bq
->iinlim_percentage
= val
;
1418 bq
->iinlim_percentage
= 100;
1421 bq
->skip_reset
= device_property_read_bool(bq
->dev
, "linux,skip-reset");
1422 bq
->read_back_init_data
= device_property_read_bool(bq
->dev
,
1423 "linux,read-back-settings");
1424 if (bq
->read_back_init_data
)
1427 ret
= bq25890_fw_read_u32_props(bq
);
1431 init
->ilim_en
= device_property_read_bool(bq
->dev
, "ti,use-ilim-pin");
1432 init
->boostf
= device_property_read_bool(bq
->dev
, "ti,boost-low-freq");
1437 static void bq25890_non_devm_cleanup(void *data
)
1439 struct bq25890_device
*bq
= data
;
1441 cancel_delayed_work_sync(&bq
->pump_express_work
);
1444 mutex_lock(&bq25890_id_mutex
);
1445 idr_remove(&bq25890_id
, bq
->id
);
1446 mutex_unlock(&bq25890_id_mutex
);
1450 static int bq25890_probe(struct i2c_client
*client
)
1452 struct device
*dev
= &client
->dev
;
1453 struct bq25890_device
*bq
;
1456 bq
= devm_kzalloc(dev
, sizeof(*bq
), GFP_KERNEL
);
1460 bq
->client
= client
;
1464 mutex_init(&bq
->lock
);
1465 INIT_DELAYED_WORK(&bq
->pump_express_work
, bq25890_pump_express_work
);
1467 bq
->rmap
= devm_regmap_init_i2c(client
, &bq25890_regmap_config
);
1468 if (IS_ERR(bq
->rmap
))
1469 return dev_err_probe(dev
, PTR_ERR(bq
->rmap
),
1470 "failed to allocate register map\n");
1472 ret
= devm_regmap_field_bulk_alloc(dev
, bq
->rmap
, bq
->rmap_fields
,
1473 bq25890_reg_fields
, F_MAX_FIELDS
);
1477 i2c_set_clientdata(client
, bq
);
1479 ret
= bq25890_get_chip_version(bq
);
1481 dev_err(dev
, "Cannot read chip ID or unknown chip: %d\n", ret
);
1485 ret
= bq25890_fw_probe(bq
);
1487 return dev_err_probe(dev
, ret
, "reading device properties\n");
1489 ret
= bq25890_hw_init(bq
);
1491 dev_err(dev
, "Cannot initialize the chip: %d\n", ret
);
1495 if (client
->irq
<= 0)
1496 client
->irq
= bq25890_irq_probe(bq
);
1498 if (client
->irq
< 0) {
1499 dev_err(dev
, "No irq resource found.\n");
1504 bq
->usb_phy
= devm_usb_get_phy(dev
, USB_PHY_TYPE_USB2
);
1507 * This must be before bq25890_power_supply_init(), so that it runs
1508 * after devm unregisters the power_supply.
1510 ret
= devm_add_action_or_reset(dev
, bq25890_non_devm_cleanup
, bq
);
1514 ret
= bq25890_register_regulator(bq
);
1518 ret
= bq25890_power_supply_init(bq
);
1520 return dev_err_probe(dev
, ret
, "registering power supply\n");
1522 ret
= devm_request_threaded_irq(dev
, client
->irq
, NULL
,
1523 bq25890_irq_handler_thread
,
1524 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
1525 BQ25890_IRQ_PIN
, bq
);
1529 if (!IS_ERR_OR_NULL(bq
->usb_phy
)) {
1530 INIT_WORK(&bq
->usb_work
, bq25890_usb_work
);
1531 bq
->usb_nb
.notifier_call
= bq25890_usb_notifier
;
1532 usb_register_notifier(bq
->usb_phy
, &bq
->usb_nb
);
1538 static void bq25890_remove(struct i2c_client
*client
)
1540 struct bq25890_device
*bq
= i2c_get_clientdata(client
);
1542 if (!IS_ERR_OR_NULL(bq
->usb_phy
)) {
1543 usb_unregister_notifier(bq
->usb_phy
, &bq
->usb_nb
);
1544 cancel_work_sync(&bq
->usb_work
);
1547 if (!bq
->skip_reset
) {
1548 /* reset all registers to default values */
1549 bq25890_chip_reset(bq
);
1553 static void bq25890_shutdown(struct i2c_client
*client
)
1555 struct bq25890_device
*bq
= i2c_get_clientdata(client
);
1558 * TODO this if + return should probably be removed, but that would
1559 * introduce a function change for boards using the usb-phy framework.
1560 * This needs to be tested on such a board before making this change.
1562 if (!IS_ERR_OR_NULL(bq
->usb_phy
))
1566 * Turn off the 5v Boost regulator which outputs Vbus to the device's
1567 * Micro-USB or Type-C USB port. Leaving this on drains power and
1568 * this avoids the PMIC on some device-models seeing this as Vbus
1569 * getting inserted after shutdown, causing the device to immediately
1572 bq25890_set_otg_cfg(bq
, 0);
1575 #ifdef CONFIG_PM_SLEEP
1576 static int bq25890_suspend(struct device
*dev
)
1578 struct bq25890_device
*bq
= dev_get_drvdata(dev
);
1581 * If charger is removed, while in suspend, make sure ADC is diabled
1582 * since it consumes slightly more power.
1584 return bq25890_field_write(bq
, F_CONV_RATE
, 0);
1587 static int bq25890_resume(struct device
*dev
)
1590 struct bq25890_device
*bq
= dev_get_drvdata(dev
);
1592 mutex_lock(&bq
->lock
);
1594 ret
= bq25890_get_chip_state(bq
, &bq
->state
);
1598 /* Re-enable ADC only if charger is plugged in. */
1599 if (bq
->state
.online
) {
1600 ret
= bq25890_field_write(bq
, F_CONV_RATE
, 1);
1605 /* signal userspace, maybe state changed while suspended */
1606 power_supply_changed(bq
->charger
);
1609 mutex_unlock(&bq
->lock
);
1615 static const struct dev_pm_ops bq25890_pm
= {
1616 SET_SYSTEM_SLEEP_PM_OPS(bq25890_suspend
, bq25890_resume
)
1619 static const struct i2c_device_id bq25890_i2c_ids
[] = {
1626 MODULE_DEVICE_TABLE(i2c
, bq25890_i2c_ids
);
1628 static const struct of_device_id bq25890_of_match
[] __maybe_unused
= {
1629 { .compatible
= "ti,bq25890", },
1630 { .compatible
= "ti,bq25892", },
1631 { .compatible
= "ti,bq25895", },
1632 { .compatible
= "ti,bq25896", },
1635 MODULE_DEVICE_TABLE(of
, bq25890_of_match
);
1638 static const struct acpi_device_id bq25890_acpi_match
[] = {
1642 MODULE_DEVICE_TABLE(acpi
, bq25890_acpi_match
);
1645 static struct i2c_driver bq25890_driver
= {
1647 .name
= "bq25890-charger",
1648 .of_match_table
= of_match_ptr(bq25890_of_match
),
1649 .acpi_match_table
= ACPI_PTR(bq25890_acpi_match
),
1652 .probe
= bq25890_probe
,
1653 .remove
= bq25890_remove
,
1654 .shutdown
= bq25890_shutdown
,
1655 .id_table
= bq25890_i2c_ids
,
1657 module_i2c_driver(bq25890_driver
);
1659 MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
1660 MODULE_DESCRIPTION("bq25890 charger driver");
1661 MODULE_LICENSE("GPL");