1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * TI BQ25890 charger driver
4 *
5 * Copyright (C) 2015 Intel Corporation
6 */
7
8 #include <linux/module.h>
9 #include <linux/i2c.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>
19
20 #include <linux/acpi.h>
21 #include <linux/of.h>
22
23 #define BQ25890_MANUFACTURER "Texas Instruments"
24 #define BQ25890_IRQ_PIN "bq25890_irq"
25
26 #define BQ25890_ID 3
27 #define BQ25895_ID 7
28 #define BQ25896_ID 0
29
30 #define PUMP_EXPRESS_START_DELAY (5 * HZ)
31 #define PUMP_EXPRESS_MAX_TRIES 6
32 #define PUMP_EXPRESS_VBUS_MARGIN_uV 1000000
33
34 enum bq25890_chip_version {
35 BQ25890,
36 BQ25892,
37 BQ25895,
38 BQ25896,
39 };
40
41 static const char *const bq25890_chip_name[] = {
42 "BQ25890",
43 "BQ25892",
44 "BQ25895",
45 "BQ25896",
46 };
47
48 enum bq25890_fields {
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 */
70 F_SYSV, /* Reg0F */
71 F_TSPCT, /* Reg10 */
72 F_VBUS_GD, F_VBUSV, /* Reg11 */
73 F_ICHGR, /* Reg12 */
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 */
76
77 F_MAX_FIELDS
78 };
79
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 */
94 };
95
96 struct bq25890_state {
97 u8 online;
98 u8 chrg_status;
99 u8 chrg_fault;
100 u8 vsys_status;
101 u8 boost_fault;
102 u8 bat_fault;
103 u8 ntc_fault;
104 };
105
106 struct bq25890_device {
107 struct i2c_client *client;
108 struct device *dev;
109 struct power_supply *charger;
110
111 struct usb_phy *usb_phy;
112 struct notifier_block usb_nb;
113 struct work_struct usb_work;
114 struct delayed_work pump_express_work;
115 unsigned long usb_event;
116
117 struct regmap *rmap;
118 struct regmap_field *rmap_fields[F_MAX_FIELDS];
119
120 bool skip_reset;
121 bool read_back_init_data;
122 u32 pump_express_vbus_max;
123 enum bq25890_chip_version chip_version;
124 struct bq25890_init_data init_data;
125 struct bq25890_state state;
126
127 struct mutex lock; /* protect state data */
128 };
129
130 static const struct regmap_range bq25890_readonly_reg_ranges[] = {
131 regmap_reg_range(0x0b, 0x0c),
132 regmap_reg_range(0x0e, 0x13),
133 };
134
135 static const struct regmap_access_table bq25890_writeable_regs = {
136 .no_ranges = bq25890_readonly_reg_ranges,
137 .n_no_ranges = ARRAY_SIZE(bq25890_readonly_reg_ranges),
138 };
139
140 static const struct regmap_range bq25890_volatile_reg_ranges[] = {
141 regmap_reg_range(0x00, 0x00),
142 regmap_reg_range(0x02, 0x02),
143 regmap_reg_range(0x09, 0x09),
144 regmap_reg_range(0x0b, 0x14),
145 };
146
147 static const struct regmap_access_table bq25890_volatile_regs = {
148 .yes_ranges = bq25890_volatile_reg_ranges,
149 .n_yes_ranges = ARRAY_SIZE(bq25890_volatile_reg_ranges),
150 };
151
152 static const struct regmap_config bq25890_regmap_config = {
153 .reg_bits = 8,
154 .val_bits = 8,
155
156 .max_register = 0x14,
157 .cache_type = REGCACHE_RBTREE,
158
159 .wr_table = &bq25890_writeable_regs,
160 .volatile_table = &bq25890_volatile_regs,
161 };
162
163 static const struct reg_field bq25890_reg_fields[] = {
164 /* REG00 */
165 [F_EN_HIZ] = REG_FIELD(0x00, 7, 7),
166 [F_EN_ILIM] = REG_FIELD(0x00, 6, 6),
167 [F_IINLIM] = REG_FIELD(0x00, 0, 5),
168 /* REG01 */
169 [F_BHOT] = REG_FIELD(0x01, 6, 7),
170 [F_BCOLD] = REG_FIELD(0x01, 5, 5),
171 [F_VINDPM_OFS] = REG_FIELD(0x01, 0, 4),
172 /* REG02 */
173 [F_CONV_START] = REG_FIELD(0x02, 7, 7),
174 [F_CONV_RATE] = REG_FIELD(0x02, 6, 6),
175 [F_BOOSTF] = REG_FIELD(0x02, 5, 5),
176 [F_ICO_EN] = REG_FIELD(0x02, 4, 4),
177 [F_HVDCP_EN] = REG_FIELD(0x02, 3, 3), // reserved on BQ25896
178 [F_MAXC_EN] = REG_FIELD(0x02, 2, 2), // reserved on BQ25896
179 [F_FORCE_DPM] = REG_FIELD(0x02, 1, 1),
180 [F_AUTO_DPDM_EN] = REG_FIELD(0x02, 0, 0),
181 /* REG03 */
182 [F_BAT_LOAD_EN] = REG_FIELD(0x03, 7, 7),
183 [F_WD_RST] = REG_FIELD(0x03, 6, 6),
184 [F_OTG_CFG] = REG_FIELD(0x03, 5, 5),
185 [F_CHG_CFG] = REG_FIELD(0x03, 4, 4),
186 [F_SYSVMIN] = REG_FIELD(0x03, 1, 3),
187 [F_MIN_VBAT_SEL] = REG_FIELD(0x03, 0, 0), // BQ25896 only
188 /* REG04 */
189 [F_PUMPX_EN] = REG_FIELD(0x04, 7, 7),
190 [F_ICHG] = REG_FIELD(0x04, 0, 6),
191 /* REG05 */
192 [F_IPRECHG] = REG_FIELD(0x05, 4, 7),
193 [F_ITERM] = REG_FIELD(0x05, 0, 3),
194 /* REG06 */
195 [F_VREG] = REG_FIELD(0x06, 2, 7),
196 [F_BATLOWV] = REG_FIELD(0x06, 1, 1),
197 [F_VRECHG] = REG_FIELD(0x06, 0, 0),
198 /* REG07 */
199 [F_TERM_EN] = REG_FIELD(0x07, 7, 7),
200 [F_STAT_DIS] = REG_FIELD(0x07, 6, 6),
201 [F_WD] = REG_FIELD(0x07, 4, 5),
202 [F_TMR_EN] = REG_FIELD(0x07, 3, 3),
203 [F_CHG_TMR] = REG_FIELD(0x07, 1, 2),
204 [F_JEITA_ISET] = REG_FIELD(0x07, 0, 0), // reserved on BQ25895
205 /* REG08 */
206 [F_BATCMP] = REG_FIELD(0x08, 5, 7),
207 [F_VCLAMP] = REG_FIELD(0x08, 2, 4),
208 [F_TREG] = REG_FIELD(0x08, 0, 1),
209 /* REG09 */
210 [F_FORCE_ICO] = REG_FIELD(0x09, 7, 7),
211 [F_TMR2X_EN] = REG_FIELD(0x09, 6, 6),
212 [F_BATFET_DIS] = REG_FIELD(0x09, 5, 5),
213 [F_JEITA_VSET] = REG_FIELD(0x09, 4, 4), // reserved on BQ25895
214 [F_BATFET_DLY] = REG_FIELD(0x09, 3, 3),
215 [F_BATFET_RST_EN] = REG_FIELD(0x09, 2, 2),
216 [F_PUMPX_UP] = REG_FIELD(0x09, 1, 1),
217 [F_PUMPX_DN] = REG_FIELD(0x09, 0, 0),
218 /* REG0A */
219 [F_BOOSTV] = REG_FIELD(0x0A, 4, 7),
220 [F_BOOSTI] = REG_FIELD(0x0A, 0, 2), // reserved on BQ25895
221 [F_PFM_OTG_DIS] = REG_FIELD(0x0A, 3, 3), // BQ25896 only
222 /* REG0B */
223 [F_VBUS_STAT] = REG_FIELD(0x0B, 5, 7),
224 [F_CHG_STAT] = REG_FIELD(0x0B, 3, 4),
225 [F_PG_STAT] = REG_FIELD(0x0B, 2, 2),
226 [F_SDP_STAT] = REG_FIELD(0x0B, 1, 1), // reserved on BQ25896
227 [F_VSYS_STAT] = REG_FIELD(0x0B, 0, 0),
228 /* REG0C */
229 [F_WD_FAULT] = REG_FIELD(0x0C, 7, 7),
230 [F_BOOST_FAULT] = REG_FIELD(0x0C, 6, 6),
231 [F_CHG_FAULT] = REG_FIELD(0x0C, 4, 5),
232 [F_BAT_FAULT] = REG_FIELD(0x0C, 3, 3),
233 [F_NTC_FAULT] = REG_FIELD(0x0C, 0, 2),
234 /* REG0D */
235 [F_FORCE_VINDPM] = REG_FIELD(0x0D, 7, 7),
236 [F_VINDPM] = REG_FIELD(0x0D, 0, 6),
237 /* REG0E */
238 [F_THERM_STAT] = REG_FIELD(0x0E, 7, 7),
239 [F_BATV] = REG_FIELD(0x0E, 0, 6),
240 /* REG0F */
241 [F_SYSV] = REG_FIELD(0x0F, 0, 6),
242 /* REG10 */
243 [F_TSPCT] = REG_FIELD(0x10, 0, 6),
244 /* REG11 */
245 [F_VBUS_GD] = REG_FIELD(0x11, 7, 7),
246 [F_VBUSV] = REG_FIELD(0x11, 0, 6),
247 /* REG12 */
248 [F_ICHGR] = REG_FIELD(0x12, 0, 6),
249 /* REG13 */
250 [F_VDPM_STAT] = REG_FIELD(0x13, 7, 7),
251 [F_IDPM_STAT] = REG_FIELD(0x13, 6, 6),
252 [F_IDPM_LIM] = REG_FIELD(0x13, 0, 5),
253 /* REG14 */
254 [F_REG_RST] = REG_FIELD(0x14, 7, 7),
255 [F_ICO_OPTIMIZED] = REG_FIELD(0x14, 6, 6),
256 [F_PN] = REG_FIELD(0x14, 3, 5),
257 [F_TS_PROFILE] = REG_FIELD(0x14, 2, 2),
258 [F_DEV_REV] = REG_FIELD(0x14, 0, 1)
259 };
260
261 /*
262 * Most of the val -> idx conversions can be computed, given the minimum,
263 * maximum and the step between values. For the rest of conversions, we use
264 * lookup tables.
265 */
266 enum bq25890_table_ids {
267 /* range tables */
268 TBL_ICHG,
269 TBL_ITERM,
270 TBL_IINLIM,
271 TBL_VREG,
272 TBL_BOOSTV,
273 TBL_SYSVMIN,
274 TBL_VBUSV,
275 TBL_VBATCOMP,
276 TBL_RBATCOMP,
277
278 /* lookup tables */
279 TBL_TREG,
280 TBL_BOOSTI,
281 TBL_TSPCT,
282 };
283
284 /* Thermal Regulation Threshold lookup table, in degrees Celsius */
285 static const u32 bq25890_treg_tbl[] = { 60, 80, 100, 120 };
286
287 #define BQ25890_TREG_TBL_SIZE ARRAY_SIZE(bq25890_treg_tbl)
288
289 /* Boost mode current limit lookup table, in uA */
290 static const u32 bq25890_boosti_tbl[] = {
291 500000, 700000, 1100000, 1300000, 1600000, 1800000, 2100000, 2400000
292 };
293
294 #define BQ25890_BOOSTI_TBL_SIZE ARRAY_SIZE(bq25890_boosti_tbl)
295
296 /* NTC 10K temperature lookup table in tenths of a degree */
297 static const u32 bq25890_tspct_tbl[] = {
298 850, 840, 830, 820, 810, 800, 790, 780,
299 770, 760, 750, 740, 730, 720, 710, 700,
300 690, 685, 680, 675, 670, 660, 650, 645,
301 640, 630, 620, 615, 610, 600, 590, 585,
302 580, 570, 565, 560, 550, 540, 535, 530,
303 520, 515, 510, 500, 495, 490, 480, 475,
304 470, 460, 455, 450, 440, 435, 430, 425,
305 420, 410, 405, 400, 390, 385, 380, 370,
306 365, 360, 355, 350, 340, 335, 330, 320,
307 310, 305, 300, 290, 285, 280, 275, 270,
308 260, 250, 245, 240, 230, 225, 220, 210,
309 205, 200, 190, 180, 175, 170, 160, 150,
310 145, 140, 130, 120, 115, 110, 100, 90,
311 80, 70, 60, 50, 40, 30, 20, 10,
312 0, -10, -20, -30, -40, -60, -70, -80,
313 -90, -10, -120, -140, -150, -170, -190, -210,
314 };
315
316 #define BQ25890_TSPCT_TBL_SIZE ARRAY_SIZE(bq25890_tspct_tbl)
317
318 struct bq25890_range {
319 u32 min;
320 u32 max;
321 u32 step;
322 };
323
324 struct bq25890_lookup {
325 const u32 *tbl;
326 u32 size;
327 };
328
329 static const union {
330 struct bq25890_range rt;
331 struct bq25890_lookup lt;
332 } bq25890_tables[] = {
333 /* range tables */
334 /* TODO: BQ25896 has max ICHG 3008 mA */
335 [TBL_ICHG] = { .rt = {0, 5056000, 64000} }, /* uA */
336 [TBL_ITERM] = { .rt = {64000, 1024000, 64000} }, /* uA */
337 [TBL_IINLIM] = { .rt = {100000, 3250000, 50000} }, /* uA */
338 [TBL_VREG] = { .rt = {3840000, 4608000, 16000} }, /* uV */
339 [TBL_BOOSTV] = { .rt = {4550000, 5510000, 64000} }, /* uV */
340 [TBL_SYSVMIN] = { .rt = {3000000, 3700000, 100000} }, /* uV */
341 [TBL_VBUSV] = { .rt = {2600000, 15300000, 100000} }, /* uV */
342 [TBL_VBATCOMP] = { .rt = {0, 224000, 32000} }, /* uV */
343 [TBL_RBATCOMP] = { .rt = {0, 140000, 20000} }, /* uOhm */
344
345 /* lookup tables */
346 [TBL_TREG] = { .lt = {bq25890_treg_tbl, BQ25890_TREG_TBL_SIZE} },
347 [TBL_BOOSTI] = { .lt = {bq25890_boosti_tbl, BQ25890_BOOSTI_TBL_SIZE} },
348 [TBL_TSPCT] = { .lt = {bq25890_tspct_tbl, BQ25890_TSPCT_TBL_SIZE} }
349 };
350
bq25890_field_read(struct bq25890_device * bq,enum bq25890_fields field_id)351 static int bq25890_field_read(struct bq25890_device *bq,
352 enum bq25890_fields field_id)
353 {
354 int ret;
355 int val;
356
357 ret = regmap_field_read(bq->rmap_fields[field_id], &val);
358 if (ret < 0)
359 return ret;
360
361 return val;
362 }
363
bq25890_field_write(struct bq25890_device * bq,enum bq25890_fields field_id,u8 val)364 static int bq25890_field_write(struct bq25890_device *bq,
365 enum bq25890_fields field_id, u8 val)
366 {
367 return regmap_field_write(bq->rmap_fields[field_id], val);
368 }
369
bq25890_find_idx(u32 value,enum bq25890_table_ids id)370 static u8 bq25890_find_idx(u32 value, enum bq25890_table_ids id)
371 {
372 u8 idx;
373
374 if (id >= TBL_TREG) {
375 const u32 *tbl = bq25890_tables[id].lt.tbl;
376 u32 tbl_size = bq25890_tables[id].lt.size;
377
378 for (idx = 1; idx < tbl_size && tbl[idx] <= value; idx++)
379 ;
380 } else {
381 const struct bq25890_range *rtbl = &bq25890_tables[id].rt;
382 u8 rtbl_size;
383
384 rtbl_size = (rtbl->max - rtbl->min) / rtbl->step + 1;
385
386 for (idx = 1;
387 idx < rtbl_size && (idx * rtbl->step + rtbl->min <= value);
388 idx++)
389 ;
390 }
391
392 return idx - 1;
393 }
394
bq25890_find_val(u8 idx,enum bq25890_table_ids id)395 static u32 bq25890_find_val(u8 idx, enum bq25890_table_ids id)
396 {
397 const struct bq25890_range *rtbl;
398
399 /* lookup table? */
400 if (id >= TBL_TREG)
401 return bq25890_tables[id].lt.tbl[idx];
402
403 /* range table */
404 rtbl = &bq25890_tables[id].rt;
405
406 return (rtbl->min + idx * rtbl->step);
407 }
408
409 enum bq25890_status {
410 STATUS_NOT_CHARGING,
411 STATUS_PRE_CHARGING,
412 STATUS_FAST_CHARGING,
413 STATUS_TERMINATION_DONE,
414 };
415
416 enum bq25890_chrg_fault {
417 CHRG_FAULT_NORMAL,
418 CHRG_FAULT_INPUT,
419 CHRG_FAULT_THERMAL_SHUTDOWN,
420 CHRG_FAULT_TIMER_EXPIRED,
421 };
422
423 enum bq25890_ntc_fault {
424 NTC_FAULT_NORMAL = 0,
425 NTC_FAULT_WARM = 2,
426 NTC_FAULT_COOL = 3,
427 NTC_FAULT_COLD = 5,
428 NTC_FAULT_HOT = 6,
429 };
430
bq25890_is_adc_property(enum power_supply_property psp)431 static bool bq25890_is_adc_property(enum power_supply_property psp)
432 {
433 switch (psp) {
434 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
435 case POWER_SUPPLY_PROP_CURRENT_NOW:
436 case POWER_SUPPLY_PROP_TEMP:
437 return true;
438
439 default:
440 return false;
441 }
442 }
443
444 static irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq);
445
bq25890_get_vbus_voltage(struct bq25890_device * bq)446 static int bq25890_get_vbus_voltage(struct bq25890_device *bq)
447 {
448 int ret;
449
450 ret = bq25890_field_read(bq, F_VBUSV);
451 if (ret < 0)
452 return ret;
453
454 return bq25890_find_val(ret, TBL_VBUSV);
455 }
456
bq25890_power_supply_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)457 static int bq25890_power_supply_get_property(struct power_supply *psy,
458 enum power_supply_property psp,
459 union power_supply_propval *val)
460 {
461 struct bq25890_device *bq = power_supply_get_drvdata(psy);
462 struct bq25890_state state;
463 bool do_adc_conv;
464 int ret;
465
466 mutex_lock(&bq->lock);
467 /* update state in case we lost an interrupt */
468 __bq25890_handle_irq(bq);
469 state = bq->state;
470 do_adc_conv = !state.online && bq25890_is_adc_property(psp);
471 if (do_adc_conv)
472 bq25890_field_write(bq, F_CONV_START, 1);
473 mutex_unlock(&bq->lock);
474
475 if (do_adc_conv)
476 regmap_field_read_poll_timeout(bq->rmap_fields[F_CONV_START],
477 ret, !ret, 25000, 1000000);
478
479 switch (psp) {
480 case POWER_SUPPLY_PROP_STATUS:
481 if (!state.online)
482 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
483 else if (state.chrg_status == STATUS_NOT_CHARGING)
484 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
485 else if (state.chrg_status == STATUS_PRE_CHARGING ||
486 state.chrg_status == STATUS_FAST_CHARGING)
487 val->intval = POWER_SUPPLY_STATUS_CHARGING;
488 else if (state.chrg_status == STATUS_TERMINATION_DONE)
489 val->intval = POWER_SUPPLY_STATUS_FULL;
490 else
491 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
492
493 break;
494
495 case POWER_SUPPLY_PROP_CHARGE_TYPE:
496 if (!state.online || state.chrg_status == STATUS_NOT_CHARGING ||
497 state.chrg_status == STATUS_TERMINATION_DONE)
498 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
499 else if (state.chrg_status == STATUS_PRE_CHARGING)
500 val->intval = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
501 else if (state.chrg_status == STATUS_FAST_CHARGING)
502 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
503 else /* unreachable */
504 val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
505 break;
506
507 case POWER_SUPPLY_PROP_MANUFACTURER:
508 val->strval = BQ25890_MANUFACTURER;
509 break;
510
511 case POWER_SUPPLY_PROP_MODEL_NAME:
512 val->strval = bq25890_chip_name[bq->chip_version];
513 break;
514
515 case POWER_SUPPLY_PROP_ONLINE:
516 val->intval = state.online;
517 break;
518
519 case POWER_SUPPLY_PROP_HEALTH:
520 if (!state.chrg_fault && !state.bat_fault && !state.boost_fault)
521 val->intval = POWER_SUPPLY_HEALTH_GOOD;
522 else if (state.bat_fault)
523 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
524 else if (state.chrg_fault == CHRG_FAULT_TIMER_EXPIRED)
525 val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
526 else if (state.chrg_fault == CHRG_FAULT_THERMAL_SHUTDOWN)
527 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
528 else
529 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
530 break;
531
532 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
533 val->intval = bq25890_find_val(bq->init_data.ichg, TBL_ICHG);
534
535 /* When temperature is too low, charge current is decreased */
536 if (bq->state.ntc_fault == NTC_FAULT_COOL) {
537 ret = bq25890_field_read(bq, F_JEITA_ISET);
538 if (ret < 0)
539 return ret;
540
541 if (ret)
542 val->intval /= 5;
543 else
544 val->intval /= 2;
545 }
546 break;
547
548 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
549 if (!state.online) {
550 val->intval = 0;
551 break;
552 }
553
554 ret = bq25890_field_read(bq, F_BATV); /* read measured value */
555 if (ret < 0)
556 return ret;
557
558 /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
559 val->intval = 2304000 + ret * 20000;
560 break;
561
562 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
563 val->intval = bq25890_find_val(bq->init_data.vreg, TBL_VREG);
564 break;
565
566 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
567 val->intval = bq25890_find_val(bq->init_data.iprechg, TBL_ITERM);
568 break;
569
570 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
571 val->intval = bq25890_find_val(bq->init_data.iterm, TBL_ITERM);
572 break;
573
574 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
575 ret = bq25890_field_read(bq, F_IINLIM);
576 if (ret < 0)
577 return ret;
578
579 val->intval = bq25890_find_val(ret, TBL_IINLIM);
580 break;
581
582 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
583 ret = bq25890_field_read(bq, F_SYSV); /* read measured value */
584 if (ret < 0)
585 return ret;
586
587 /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
588 val->intval = 2304000 + ret * 20000;
589 break;
590
591 case POWER_SUPPLY_PROP_CURRENT_NOW:
592 ret = bq25890_field_read(bq, F_ICHGR); /* read measured value */
593 if (ret < 0)
594 return ret;
595
596 /* converted_val = ADC_val * 50mA (table 10.3.19) */
597 val->intval = ret * -50000;
598 break;
599
600 case POWER_SUPPLY_PROP_TEMP:
601 ret = bq25890_field_read(bq, F_TSPCT);
602 if (ret < 0)
603 return ret;
604
605 /* convert TS percentage into rough temperature */
606 val->intval = bq25890_find_val(ret, TBL_TSPCT);
607 break;
608
609 default:
610 return -EINVAL;
611 }
612
613 return 0;
614 }
615
bq25890_power_supply_set_property(struct power_supply * psy,enum power_supply_property psp,const union power_supply_propval * val)616 static int bq25890_power_supply_set_property(struct power_supply *psy,
617 enum power_supply_property psp,
618 const union power_supply_propval *val)
619 {
620 struct bq25890_device *bq = power_supply_get_drvdata(psy);
621 u8 lval;
622
623 switch (psp) {
624 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
625 lval = bq25890_find_idx(val->intval, TBL_IINLIM);
626 return bq25890_field_write(bq, F_IINLIM, lval);
627 default:
628 return -EINVAL;
629 }
630 }
631
bq25890_power_supply_property_is_writeable(struct power_supply * psy,enum power_supply_property psp)632 static int bq25890_power_supply_property_is_writeable(struct power_supply *psy,
633 enum power_supply_property psp)
634 {
635 switch (psp) {
636 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
637 return true;
638 default:
639 return false;
640 }
641 }
642
643 /* On the BQ25892 try to get charger-type info from our supplier */
bq25890_charger_external_power_changed(struct power_supply * psy)644 static void bq25890_charger_external_power_changed(struct power_supply *psy)
645 {
646 struct bq25890_device *bq = power_supply_get_drvdata(psy);
647 union power_supply_propval val;
648 int input_current_limit, ret;
649
650 if (bq->chip_version != BQ25892)
651 return;
652
653 ret = power_supply_get_property_from_supplier(bq->charger,
654 POWER_SUPPLY_PROP_USB_TYPE,
655 &val);
656 if (ret)
657 return;
658
659 switch (val.intval) {
660 case POWER_SUPPLY_USB_TYPE_DCP:
661 input_current_limit = bq25890_find_idx(2000000, TBL_IINLIM);
662 if (bq->pump_express_vbus_max) {
663 queue_delayed_work(system_power_efficient_wq,
664 &bq->pump_express_work,
665 PUMP_EXPRESS_START_DELAY);
666 }
667 break;
668 case POWER_SUPPLY_USB_TYPE_CDP:
669 case POWER_SUPPLY_USB_TYPE_ACA:
670 input_current_limit = bq25890_find_idx(1500000, TBL_IINLIM);
671 break;
672 case POWER_SUPPLY_USB_TYPE_SDP:
673 default:
674 input_current_limit = bq25890_find_idx(500000, TBL_IINLIM);
675 }
676
677 bq25890_field_write(bq, F_IINLIM, input_current_limit);
678 }
679
bq25890_get_chip_state(struct bq25890_device * bq,struct bq25890_state * state)680 static int bq25890_get_chip_state(struct bq25890_device *bq,
681 struct bq25890_state *state)
682 {
683 int i, ret;
684
685 struct {
686 enum bq25890_fields id;
687 u8 *data;
688 } state_fields[] = {
689 {F_CHG_STAT, &state->chrg_status},
690 {F_PG_STAT, &state->online},
691 {F_VSYS_STAT, &state->vsys_status},
692 {F_BOOST_FAULT, &state->boost_fault},
693 {F_BAT_FAULT, &state->bat_fault},
694 {F_CHG_FAULT, &state->chrg_fault},
695 {F_NTC_FAULT, &state->ntc_fault}
696 };
697
698 for (i = 0; i < ARRAY_SIZE(state_fields); i++) {
699 ret = bq25890_field_read(bq, state_fields[i].id);
700 if (ret < 0)
701 return ret;
702
703 *state_fields[i].data = ret;
704 }
705
706 dev_dbg(bq->dev, "S:CHG/PG/VSYS=%d/%d/%d, F:CHG/BOOST/BAT/NTC=%d/%d/%d/%d\n",
707 state->chrg_status, state->online, state->vsys_status,
708 state->chrg_fault, state->boost_fault, state->bat_fault,
709 state->ntc_fault);
710
711 return 0;
712 }
713
__bq25890_handle_irq(struct bq25890_device * bq)714 static irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq)
715 {
716 struct bq25890_state new_state;
717 int ret;
718
719 ret = bq25890_get_chip_state(bq, &new_state);
720 if (ret < 0)
721 return IRQ_NONE;
722
723 if (!memcmp(&bq->state, &new_state, sizeof(new_state)))
724 return IRQ_NONE;
725
726 if (!new_state.online && bq->state.online) { /* power removed */
727 /* disable ADC */
728 ret = bq25890_field_write(bq, F_CONV_RATE, 0);
729 if (ret < 0)
730 goto error;
731 } else if (new_state.online && !bq->state.online) { /* power inserted */
732 /* enable ADC, to have control of charge current/voltage */
733 ret = bq25890_field_write(bq, F_CONV_RATE, 1);
734 if (ret < 0)
735 goto error;
736 }
737
738 bq->state = new_state;
739 power_supply_changed(bq->charger);
740
741 return IRQ_HANDLED;
742 error:
743 dev_err(bq->dev, "Error communicating with the chip: %pe\n",
744 ERR_PTR(ret));
745 return IRQ_HANDLED;
746 }
747
bq25890_irq_handler_thread(int irq,void * private)748 static irqreturn_t bq25890_irq_handler_thread(int irq, void *private)
749 {
750 struct bq25890_device *bq = private;
751 irqreturn_t ret;
752
753 mutex_lock(&bq->lock);
754 ret = __bq25890_handle_irq(bq);
755 mutex_unlock(&bq->lock);
756
757 return ret;
758 }
759
bq25890_chip_reset(struct bq25890_device * bq)760 static int bq25890_chip_reset(struct bq25890_device *bq)
761 {
762 int ret;
763 int rst_check_counter = 10;
764
765 ret = bq25890_field_write(bq, F_REG_RST, 1);
766 if (ret < 0)
767 return ret;
768
769 do {
770 ret = bq25890_field_read(bq, F_REG_RST);
771 if (ret < 0)
772 return ret;
773
774 usleep_range(5, 10);
775 } while (ret == 1 && --rst_check_counter);
776
777 if (!rst_check_counter)
778 return -ETIMEDOUT;
779
780 return 0;
781 }
782
bq25890_rw_init_data(struct bq25890_device * bq)783 static int bq25890_rw_init_data(struct bq25890_device *bq)
784 {
785 bool write = !bq->read_back_init_data;
786 int ret;
787 int i;
788
789 const struct {
790 enum bq25890_fields id;
791 u8 *value;
792 } init_data[] = {
793 {F_ICHG, &bq->init_data.ichg},
794 {F_VREG, &bq->init_data.vreg},
795 {F_ITERM, &bq->init_data.iterm},
796 {F_IPRECHG, &bq->init_data.iprechg},
797 {F_SYSVMIN, &bq->init_data.sysvmin},
798 {F_BOOSTV, &bq->init_data.boostv},
799 {F_BOOSTI, &bq->init_data.boosti},
800 {F_BOOSTF, &bq->init_data.boostf},
801 {F_EN_ILIM, &bq->init_data.ilim_en},
802 {F_TREG, &bq->init_data.treg},
803 {F_BATCMP, &bq->init_data.rbatcomp},
804 {F_VCLAMP, &bq->init_data.vclamp},
805 };
806
807 for (i = 0; i < ARRAY_SIZE(init_data); i++) {
808 if (write) {
809 ret = bq25890_field_write(bq, init_data[i].id,
810 *init_data[i].value);
811 } else {
812 ret = bq25890_field_read(bq, init_data[i].id);
813 if (ret >= 0)
814 *init_data[i].value = ret;
815 }
816 if (ret < 0) {
817 dev_dbg(bq->dev, "Accessing init data failed %d\n", ret);
818 return ret;
819 }
820 }
821
822 return 0;
823 }
824
bq25890_hw_init(struct bq25890_device * bq)825 static int bq25890_hw_init(struct bq25890_device *bq)
826 {
827 int ret;
828
829 if (!bq->skip_reset) {
830 ret = bq25890_chip_reset(bq);
831 if (ret < 0) {
832 dev_dbg(bq->dev, "Reset failed %d\n", ret);
833 return ret;
834 }
835 } else {
836 /*
837 * Ensure charging is enabled, on some boards where the fw
838 * takes care of initalizition F_CHG_CFG is set to 0 before
839 * handing control over to the OS.
840 */
841 ret = bq25890_field_write(bq, F_CHG_CFG, 1);
842 if (ret < 0) {
843 dev_dbg(bq->dev, "Enabling charging failed %d\n", ret);
844 return ret;
845 }
846 }
847
848 /* disable watchdog */
849 ret = bq25890_field_write(bq, F_WD, 0);
850 if (ret < 0) {
851 dev_dbg(bq->dev, "Disabling watchdog failed %d\n", ret);
852 return ret;
853 }
854
855 /* initialize currents/voltages and other parameters */
856 ret = bq25890_rw_init_data(bq);
857 if (ret)
858 return ret;
859
860 ret = bq25890_get_chip_state(bq, &bq->state);
861 if (ret < 0) {
862 dev_dbg(bq->dev, "Get state failed %d\n", ret);
863 return ret;
864 }
865
866 /* Configure ADC for continuous conversions when charging */
867 ret = bq25890_field_write(bq, F_CONV_RATE, !!bq->state.online);
868 if (ret < 0) {
869 dev_dbg(bq->dev, "Config ADC failed %d\n", ret);
870 return ret;
871 }
872
873 return 0;
874 }
875
876 static const enum power_supply_property bq25890_power_supply_props[] = {
877 POWER_SUPPLY_PROP_MANUFACTURER,
878 POWER_SUPPLY_PROP_MODEL_NAME,
879 POWER_SUPPLY_PROP_STATUS,
880 POWER_SUPPLY_PROP_CHARGE_TYPE,
881 POWER_SUPPLY_PROP_ONLINE,
882 POWER_SUPPLY_PROP_HEALTH,
883 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
884 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
885 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
886 POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
887 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
888 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
889 POWER_SUPPLY_PROP_VOLTAGE_NOW,
890 POWER_SUPPLY_PROP_CURRENT_NOW,
891 POWER_SUPPLY_PROP_TEMP,
892 };
893
894 static char *bq25890_charger_supplied_to[] = {
895 "main-battery",
896 };
897
898 static const struct power_supply_desc bq25890_power_supply_desc = {
899 .name = "bq25890-charger",
900 .type = POWER_SUPPLY_TYPE_USB,
901 .properties = bq25890_power_supply_props,
902 .num_properties = ARRAY_SIZE(bq25890_power_supply_props),
903 .get_property = bq25890_power_supply_get_property,
904 .set_property = bq25890_power_supply_set_property,
905 .property_is_writeable = bq25890_power_supply_property_is_writeable,
906 .external_power_changed = bq25890_charger_external_power_changed,
907 };
908
bq25890_power_supply_init(struct bq25890_device * bq)909 static int bq25890_power_supply_init(struct bq25890_device *bq)
910 {
911 struct power_supply_config psy_cfg = { .drv_data = bq, };
912
913 psy_cfg.supplied_to = bq25890_charger_supplied_to;
914 psy_cfg.num_supplicants = ARRAY_SIZE(bq25890_charger_supplied_to);
915
916 bq->charger = devm_power_supply_register(bq->dev,
917 &bq25890_power_supply_desc,
918 &psy_cfg);
919
920 return PTR_ERR_OR_ZERO(bq->charger);
921 }
922
bq25890_set_otg_cfg(struct bq25890_device * bq,u8 val)923 static int bq25890_set_otg_cfg(struct bq25890_device *bq, u8 val)
924 {
925 int ret;
926
927 ret = bq25890_field_write(bq, F_OTG_CFG, val);
928 if (ret < 0)
929 dev_err(bq->dev, "Error switching to boost/charger mode: %d\n", ret);
930
931 return ret;
932 }
933
bq25890_pump_express_work(struct work_struct * data)934 static void bq25890_pump_express_work(struct work_struct *data)
935 {
936 struct bq25890_device *bq =
937 container_of(data, struct bq25890_device, pump_express_work.work);
938 int voltage, i, ret;
939
940 dev_dbg(bq->dev, "Start to request input voltage increasing\n");
941
942 /* Enable current pulse voltage control protocol */
943 ret = bq25890_field_write(bq, F_PUMPX_EN, 1);
944 if (ret < 0)
945 goto error_print;
946
947 for (i = 0; i < PUMP_EXPRESS_MAX_TRIES; i++) {
948 voltage = bq25890_get_vbus_voltage(bq);
949 if (voltage < 0)
950 goto error_print;
951 dev_dbg(bq->dev, "input voltage = %d uV\n", voltage);
952
953 if ((voltage + PUMP_EXPRESS_VBUS_MARGIN_uV) >
954 bq->pump_express_vbus_max)
955 break;
956
957 ret = bq25890_field_write(bq, F_PUMPX_UP, 1);
958 if (ret < 0)
959 goto error_print;
960
961 /* Note a single PUMPX up pulse-sequence takes 2.1s */
962 ret = regmap_field_read_poll_timeout(bq->rmap_fields[F_PUMPX_UP],
963 ret, !ret, 100000, 3000000);
964 if (ret < 0)
965 goto error_print;
966
967 /* Make sure ADC has sampled Vbus before checking again */
968 msleep(1000);
969 }
970
971 bq25890_field_write(bq, F_PUMPX_EN, 0);
972
973 dev_info(bq->dev, "Hi-voltage charging requested, input voltage is %d mV\n",
974 voltage);
975
976 return;
977 error_print:
978 bq25890_field_write(bq, F_PUMPX_EN, 0);
979 dev_err(bq->dev, "Failed to request hi-voltage charging\n");
980 }
981
bq25890_usb_work(struct work_struct * data)982 static void bq25890_usb_work(struct work_struct *data)
983 {
984 int ret;
985 struct bq25890_device *bq =
986 container_of(data, struct bq25890_device, usb_work);
987
988 switch (bq->usb_event) {
989 case USB_EVENT_ID:
990 /* Enable boost mode */
991 bq25890_set_otg_cfg(bq, 1);
992 break;
993
994 case USB_EVENT_NONE:
995 /* Disable boost mode */
996 ret = bq25890_set_otg_cfg(bq, 0);
997 if (ret == 0)
998 power_supply_changed(bq->charger);
999 break;
1000 }
1001 }
1002
bq25890_usb_notifier(struct notifier_block * nb,unsigned long val,void * priv)1003 static int bq25890_usb_notifier(struct notifier_block *nb, unsigned long val,
1004 void *priv)
1005 {
1006 struct bq25890_device *bq =
1007 container_of(nb, struct bq25890_device, usb_nb);
1008
1009 bq->usb_event = val;
1010 queue_work(system_power_efficient_wq, &bq->usb_work);
1011
1012 return NOTIFY_OK;
1013 }
1014
1015 #ifdef CONFIG_REGULATOR
bq25890_vbus_enable(struct regulator_dev * rdev)1016 static int bq25890_vbus_enable(struct regulator_dev *rdev)
1017 {
1018 struct bq25890_device *bq = rdev_get_drvdata(rdev);
1019
1020 return bq25890_set_otg_cfg(bq, 1);
1021 }
1022
bq25890_vbus_disable(struct regulator_dev * rdev)1023 static int bq25890_vbus_disable(struct regulator_dev *rdev)
1024 {
1025 struct bq25890_device *bq = rdev_get_drvdata(rdev);
1026
1027 return bq25890_set_otg_cfg(bq, 0);
1028 }
1029
bq25890_vbus_is_enabled(struct regulator_dev * rdev)1030 static int bq25890_vbus_is_enabled(struct regulator_dev *rdev)
1031 {
1032 struct bq25890_device *bq = rdev_get_drvdata(rdev);
1033
1034 return bq25890_field_read(bq, F_OTG_CFG);
1035 }
1036
1037 static const struct regulator_ops bq25890_vbus_ops = {
1038 .enable = bq25890_vbus_enable,
1039 .disable = bq25890_vbus_disable,
1040 .is_enabled = bq25890_vbus_is_enabled,
1041 };
1042
1043 static const struct regulator_desc bq25890_vbus_desc = {
1044 .name = "usb_otg_vbus",
1045 .of_match = "usb-otg-vbus",
1046 .type = REGULATOR_VOLTAGE,
1047 .owner = THIS_MODULE,
1048 .ops = &bq25890_vbus_ops,
1049 .fixed_uV = 5000000,
1050 .n_voltages = 1,
1051 };
1052 #endif
1053
bq25890_get_chip_version(struct bq25890_device * bq)1054 static int bq25890_get_chip_version(struct bq25890_device *bq)
1055 {
1056 int id, rev;
1057
1058 id = bq25890_field_read(bq, F_PN);
1059 if (id < 0) {
1060 dev_err(bq->dev, "Cannot read chip ID: %d\n", id);
1061 return id;
1062 }
1063
1064 rev = bq25890_field_read(bq, F_DEV_REV);
1065 if (rev < 0) {
1066 dev_err(bq->dev, "Cannot read chip revision: %d\n", rev);
1067 return rev;
1068 }
1069
1070 switch (id) {
1071 case BQ25890_ID:
1072 bq->chip_version = BQ25890;
1073 break;
1074
1075 /* BQ25892 and BQ25896 share same ID 0 */
1076 case BQ25896_ID:
1077 switch (rev) {
1078 case 2:
1079 bq->chip_version = BQ25896;
1080 break;
1081 case 1:
1082 bq->chip_version = BQ25892;
1083 break;
1084 default:
1085 dev_err(bq->dev,
1086 "Unknown device revision %d, assume BQ25892\n",
1087 rev);
1088 bq->chip_version = BQ25892;
1089 }
1090 break;
1091
1092 case BQ25895_ID:
1093 bq->chip_version = BQ25895;
1094 break;
1095
1096 default:
1097 dev_err(bq->dev, "Unknown chip ID %d\n", id);
1098 return -ENODEV;
1099 }
1100
1101 return 0;
1102 }
1103
bq25890_irq_probe(struct bq25890_device * bq)1104 static int bq25890_irq_probe(struct bq25890_device *bq)
1105 {
1106 struct gpio_desc *irq;
1107
1108 irq = devm_gpiod_get(bq->dev, BQ25890_IRQ_PIN, GPIOD_IN);
1109 if (IS_ERR(irq))
1110 return dev_err_probe(bq->dev, PTR_ERR(irq),
1111 "Could not probe irq pin.\n");
1112
1113 return gpiod_to_irq(irq);
1114 }
1115
bq25890_fw_read_u32_props(struct bq25890_device * bq)1116 static int bq25890_fw_read_u32_props(struct bq25890_device *bq)
1117 {
1118 int ret;
1119 u32 property;
1120 int i;
1121 struct bq25890_init_data *init = &bq->init_data;
1122 struct {
1123 char *name;
1124 bool optional;
1125 enum bq25890_table_ids tbl_id;
1126 u8 *conv_data; /* holds converted value from given property */
1127 } props[] = {
1128 /* required properties */
1129 {"ti,charge-current", false, TBL_ICHG, &init->ichg},
1130 {"ti,battery-regulation-voltage", false, TBL_VREG, &init->vreg},
1131 {"ti,termination-current", false, TBL_ITERM, &init->iterm},
1132 {"ti,precharge-current", false, TBL_ITERM, &init->iprechg},
1133 {"ti,minimum-sys-voltage", false, TBL_SYSVMIN, &init->sysvmin},
1134 {"ti,boost-voltage", false, TBL_BOOSTV, &init->boostv},
1135 {"ti,boost-max-current", false, TBL_BOOSTI, &init->boosti},
1136
1137 /* optional properties */
1138 {"ti,thermal-regulation-threshold", true, TBL_TREG, &init->treg},
1139 {"ti,ibatcomp-micro-ohms", true, TBL_RBATCOMP, &init->rbatcomp},
1140 {"ti,ibatcomp-clamp-microvolt", true, TBL_VBATCOMP, &init->vclamp},
1141 };
1142
1143 /* initialize data for optional properties */
1144 init->treg = 3; /* 120 degrees Celsius */
1145 init->rbatcomp = init->vclamp = 0; /* IBAT compensation disabled */
1146
1147 for (i = 0; i < ARRAY_SIZE(props); i++) {
1148 ret = device_property_read_u32(bq->dev, props[i].name,
1149 &property);
1150 if (ret < 0) {
1151 if (props[i].optional)
1152 continue;
1153
1154 dev_err(bq->dev, "Unable to read property %d %s\n", ret,
1155 props[i].name);
1156
1157 return ret;
1158 }
1159
1160 *props[i].conv_data = bq25890_find_idx(property,
1161 props[i].tbl_id);
1162 }
1163
1164 return 0;
1165 }
1166
bq25890_fw_probe(struct bq25890_device * bq)1167 static int bq25890_fw_probe(struct bq25890_device *bq)
1168 {
1169 int ret;
1170 struct bq25890_init_data *init = &bq->init_data;
1171
1172 /* Optional, left at 0 if property is not present */
1173 device_property_read_u32(bq->dev, "linux,pump-express-vbus-max",
1174 &bq->pump_express_vbus_max);
1175
1176 bq->skip_reset = device_property_read_bool(bq->dev, "linux,skip-reset");
1177 bq->read_back_init_data = device_property_read_bool(bq->dev,
1178 "linux,read-back-settings");
1179 if (bq->read_back_init_data)
1180 return 0;
1181
1182 ret = bq25890_fw_read_u32_props(bq);
1183 if (ret < 0)
1184 return ret;
1185
1186 init->ilim_en = device_property_read_bool(bq->dev, "ti,use-ilim-pin");
1187 init->boostf = device_property_read_bool(bq->dev, "ti,boost-low-freq");
1188
1189 return 0;
1190 }
1191
bq25890_probe(struct i2c_client * client,const struct i2c_device_id * id)1192 static int bq25890_probe(struct i2c_client *client,
1193 const struct i2c_device_id *id)
1194 {
1195 struct device *dev = &client->dev;
1196 struct bq25890_device *bq;
1197 int ret;
1198
1199 bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL);
1200 if (!bq)
1201 return -ENOMEM;
1202
1203 bq->client = client;
1204 bq->dev = dev;
1205
1206 mutex_init(&bq->lock);
1207 INIT_DELAYED_WORK(&bq->pump_express_work, bq25890_pump_express_work);
1208
1209 bq->rmap = devm_regmap_init_i2c(client, &bq25890_regmap_config);
1210 if (IS_ERR(bq->rmap))
1211 return dev_err_probe(dev, PTR_ERR(bq->rmap),
1212 "failed to allocate register map\n");
1213
1214 ret = devm_regmap_field_bulk_alloc(dev, bq->rmap, bq->rmap_fields,
1215 bq25890_reg_fields, F_MAX_FIELDS);
1216 if (ret)
1217 return ret;
1218
1219 i2c_set_clientdata(client, bq);
1220
1221 ret = bq25890_get_chip_version(bq);
1222 if (ret) {
1223 dev_err(dev, "Cannot read chip ID or unknown chip: %d\n", ret);
1224 return ret;
1225 }
1226
1227 ret = bq25890_fw_probe(bq);
1228 if (ret < 0)
1229 return dev_err_probe(dev, ret, "reading device properties\n");
1230
1231 ret = bq25890_hw_init(bq);
1232 if (ret < 0) {
1233 dev_err(dev, "Cannot initialize the chip: %d\n", ret);
1234 return ret;
1235 }
1236
1237 if (client->irq <= 0)
1238 client->irq = bq25890_irq_probe(bq);
1239
1240 if (client->irq < 0) {
1241 dev_err(dev, "No irq resource found.\n");
1242 return client->irq;
1243 }
1244
1245 /* OTG reporting */
1246 bq->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1247 if (!IS_ERR_OR_NULL(bq->usb_phy)) {
1248 INIT_WORK(&bq->usb_work, bq25890_usb_work);
1249 bq->usb_nb.notifier_call = bq25890_usb_notifier;
1250 usb_register_notifier(bq->usb_phy, &bq->usb_nb);
1251 }
1252 #ifdef CONFIG_REGULATOR
1253 else {
1254 struct bq25890_platform_data *pdata = dev_get_platdata(dev);
1255 struct regulator_config cfg = { };
1256 struct regulator_dev *reg;
1257
1258 cfg.dev = dev;
1259 cfg.driver_data = bq;
1260 if (pdata)
1261 cfg.init_data = pdata->regulator_init_data;
1262
1263 reg = devm_regulator_register(dev, &bq25890_vbus_desc, &cfg);
1264 if (IS_ERR(reg))
1265 return dev_err_probe(dev, PTR_ERR(reg), "registering regulator");
1266 }
1267 #endif
1268
1269 ret = bq25890_power_supply_init(bq);
1270 if (ret < 0) {
1271 dev_err(dev, "Failed to register power supply\n");
1272 goto err_unregister_usb_notifier;
1273 }
1274
1275 ret = devm_request_threaded_irq(dev, client->irq, NULL,
1276 bq25890_irq_handler_thread,
1277 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1278 BQ25890_IRQ_PIN, bq);
1279 if (ret)
1280 goto err_unregister_usb_notifier;
1281
1282 return 0;
1283
1284 err_unregister_usb_notifier:
1285 if (!IS_ERR_OR_NULL(bq->usb_phy))
1286 usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
1287
1288 return ret;
1289 }
1290
bq25890_remove(struct i2c_client * client)1291 static void bq25890_remove(struct i2c_client *client)
1292 {
1293 struct bq25890_device *bq = i2c_get_clientdata(client);
1294
1295 if (!IS_ERR_OR_NULL(bq->usb_phy))
1296 usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
1297
1298 if (!bq->skip_reset) {
1299 /* reset all registers to default values */
1300 bq25890_chip_reset(bq);
1301 }
1302 }
1303
bq25890_shutdown(struct i2c_client * client)1304 static void bq25890_shutdown(struct i2c_client *client)
1305 {
1306 struct bq25890_device *bq = i2c_get_clientdata(client);
1307
1308 /*
1309 * TODO this if + return should probably be removed, but that would
1310 * introduce a function change for boards using the usb-phy framework.
1311 * This needs to be tested on such a board before making this change.
1312 */
1313 if (!IS_ERR_OR_NULL(bq->usb_phy))
1314 return;
1315
1316 /*
1317 * Turn off the 5v Boost regulator which outputs Vbus to the device's
1318 * Micro-USB or Type-C USB port. Leaving this on drains power and
1319 * this avoids the PMIC on some device-models seeing this as Vbus
1320 * getting inserted after shutdown, causing the device to immediately
1321 * power-up again.
1322 */
1323 bq25890_set_otg_cfg(bq, 0);
1324 }
1325
1326 #ifdef CONFIG_PM_SLEEP
bq25890_suspend(struct device * dev)1327 static int bq25890_suspend(struct device *dev)
1328 {
1329 struct bq25890_device *bq = dev_get_drvdata(dev);
1330
1331 /*
1332 * If charger is removed, while in suspend, make sure ADC is diabled
1333 * since it consumes slightly more power.
1334 */
1335 return bq25890_field_write(bq, F_CONV_RATE, 0);
1336 }
1337
bq25890_resume(struct device * dev)1338 static int bq25890_resume(struct device *dev)
1339 {
1340 int ret;
1341 struct bq25890_device *bq = dev_get_drvdata(dev);
1342
1343 mutex_lock(&bq->lock);
1344
1345 ret = bq25890_get_chip_state(bq, &bq->state);
1346 if (ret < 0)
1347 goto unlock;
1348
1349 /* Re-enable ADC only if charger is plugged in. */
1350 if (bq->state.online) {
1351 ret = bq25890_field_write(bq, F_CONV_RATE, 1);
1352 if (ret < 0)
1353 goto unlock;
1354 }
1355
1356 /* signal userspace, maybe state changed while suspended */
1357 power_supply_changed(bq->charger);
1358
1359 unlock:
1360 mutex_unlock(&bq->lock);
1361
1362 return ret;
1363 }
1364 #endif
1365
1366 static const struct dev_pm_ops bq25890_pm = {
1367 SET_SYSTEM_SLEEP_PM_OPS(bq25890_suspend, bq25890_resume)
1368 };
1369
1370 static const struct i2c_device_id bq25890_i2c_ids[] = {
1371 { "bq25890", 0 },
1372 { "bq25892", 0 },
1373 { "bq25895", 0 },
1374 { "bq25896", 0 },
1375 {},
1376 };
1377 MODULE_DEVICE_TABLE(i2c, bq25890_i2c_ids);
1378
1379 static const struct of_device_id bq25890_of_match[] = {
1380 { .compatible = "ti,bq25890", },
1381 { .compatible = "ti,bq25892", },
1382 { .compatible = "ti,bq25895", },
1383 { .compatible = "ti,bq25896", },
1384 { },
1385 };
1386 MODULE_DEVICE_TABLE(of, bq25890_of_match);
1387
1388 #ifdef CONFIG_ACPI
1389 static const struct acpi_device_id bq25890_acpi_match[] = {
1390 {"BQ258900", 0},
1391 {},
1392 };
1393 MODULE_DEVICE_TABLE(acpi, bq25890_acpi_match);
1394 #endif
1395
1396 static struct i2c_driver bq25890_driver = {
1397 .driver = {
1398 .name = "bq25890-charger",
1399 .of_match_table = of_match_ptr(bq25890_of_match),
1400 .acpi_match_table = ACPI_PTR(bq25890_acpi_match),
1401 .pm = &bq25890_pm,
1402 },
1403 .probe = bq25890_probe,
1404 .remove = bq25890_remove,
1405 .shutdown = bq25890_shutdown,
1406 .id_table = bq25890_i2c_ids,
1407 };
1408 module_i2c_driver(bq25890_driver);
1409
1410 MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
1411 MODULE_DESCRIPTION("bq25890 charger driver");
1412 MODULE_LICENSE("GPL");
1413