Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
34enum bq25890_chip_version {
35 BQ25890,
36 BQ25892,
37 BQ25895,
38 BQ25896,
39};
40
41static const char *const bq25890_chip_name[] = {
42 "BQ25890",
43 "BQ25892",
44 "BQ25895",
45 "BQ25896",
46};
47
48enum 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 */
81struct 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
96struct bq25890_state {
97 u8 online;
98 u8 hiz;
99 u8 chrg_status;
100 u8 chrg_fault;
101 u8 vsys_status;
102 u8 boost_fault;
103 u8 bat_fault;
104 u8 ntc_fault;
105};
106
107struct bq25890_device {
108 struct i2c_client *client;
109 struct device *dev;
110 struct power_supply *charger;
111 struct power_supply *secondary_chrg;
112 struct power_supply_desc desc;
113 char name[28]; /* "bq25890-charger-%d" */
114 int id;
115
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;
121
122 struct regmap *rmap;
123 struct regmap_field *rmap_fields[F_MAX_FIELDS];
124
125 bool skip_reset;
126 bool read_back_init_data;
127 bool force_hiz;
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;
133
134 struct mutex lock; /* protect state data */
135};
136
137static DEFINE_IDR(bq25890_id);
138static DEFINE_MUTEX(bq25890_id_mutex);
139
140static const struct regmap_range bq25890_readonly_reg_ranges[] = {
141 regmap_reg_range(0x0b, 0x0c),
142 regmap_reg_range(0x0e, 0x13),
143};
144
145static 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),
148};
149
150static 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),
155};
156
157static 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),
160};
161
162static const struct regmap_config bq25890_regmap_config = {
163 .reg_bits = 8,
164 .val_bits = 8,
165
166 .max_register = 0x14,
167 .cache_type = REGCACHE_RBTREE,
168
169 .wr_table = &bq25890_writeable_regs,
170 .volatile_table = &bq25890_volatile_regs,
171};
172
173static const struct reg_field bq25890_reg_fields[] = {
174 /* REG00 */
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),
178 /* REG01 */
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),
182 /* REG02 */
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),
191 /* REG03 */
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
198 /* REG04 */
199 [F_PUMPX_EN] = REG_FIELD(0x04, 7, 7),
200 [F_ICHG] = REG_FIELD(0x04, 0, 6),
201 /* REG05 */
202 [F_IPRECHG] = REG_FIELD(0x05, 4, 7),
203 [F_ITERM] = REG_FIELD(0x05, 0, 3),
204 /* REG06 */
205 [F_VREG] = REG_FIELD(0x06, 2, 7),
206 [F_BATLOWV] = REG_FIELD(0x06, 1, 1),
207 [F_VRECHG] = REG_FIELD(0x06, 0, 0),
208 /* REG07 */
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
215 /* REG08 */
216 [F_BATCMP] = REG_FIELD(0x08, 5, 7),
217 [F_VCLAMP] = REG_FIELD(0x08, 2, 4),
218 [F_TREG] = REG_FIELD(0x08, 0, 1),
219 /* REG09 */
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),
228 /* REG0A */
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
232 /* REG0B */
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),
238 /* REG0C */
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),
244 /* REG0D */
245 [F_FORCE_VINDPM] = REG_FIELD(0x0D, 7, 7),
246 [F_VINDPM] = REG_FIELD(0x0D, 0, 6),
247 /* REG0E */
248 [F_THERM_STAT] = REG_FIELD(0x0E, 7, 7),
249 [F_BATV] = REG_FIELD(0x0E, 0, 6),
250 /* REG0F */
251 [F_SYSV] = REG_FIELD(0x0F, 0, 6),
252 /* REG10 */
253 [F_TSPCT] = REG_FIELD(0x10, 0, 6),
254 /* REG11 */
255 [F_VBUS_GD] = REG_FIELD(0x11, 7, 7),
256 [F_VBUSV] = REG_FIELD(0x11, 0, 6),
257 /* REG12 */
258 [F_ICHGR] = REG_FIELD(0x12, 0, 6),
259 /* REG13 */
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),
263 /* REG14 */
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)
269};
270
271/*
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
274 * lookup tables.
275 */
276enum bq25890_table_ids {
277 /* range tables */
278 TBL_ICHG,
279 TBL_ITERM,
280 TBL_IINLIM,
281 TBL_VREG,
282 TBL_BOOSTV,
283 TBL_SYSVMIN,
284 TBL_VBUSV,
285 TBL_VBATCOMP,
286 TBL_RBATCOMP,
287
288 /* lookup tables */
289 TBL_TREG,
290 TBL_BOOSTI,
291 TBL_TSPCT,
292};
293
294/* Thermal Regulation Threshold lookup table, in degrees Celsius */
295static const u32 bq25890_treg_tbl[] = { 60, 80, 100, 120 };
296
297#define BQ25890_TREG_TBL_SIZE ARRAY_SIZE(bq25890_treg_tbl)
298
299/* Boost mode current limit lookup table, in uA */
300static const u32 bq25890_boosti_tbl[] = {
301 500000, 700000, 1100000, 1300000, 1600000, 1800000, 2100000, 2400000
302};
303
304#define BQ25890_BOOSTI_TBL_SIZE ARRAY_SIZE(bq25890_boosti_tbl)
305
306/* NTC 10K temperature lookup table in tenths of a degree */
307static 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,
324};
325
326#define BQ25890_TSPCT_TBL_SIZE ARRAY_SIZE(bq25890_tspct_tbl)
327
328struct bq25890_range {
329 u32 min;
330 u32 max;
331 u32 step;
332};
333
334struct bq25890_lookup {
335 const u32 *tbl;
336 u32 size;
337};
338
339static const union {
340 struct bq25890_range rt;
341 struct bq25890_lookup lt;
342} bq25890_tables[] = {
343 /* range 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 */
354
355 /* lookup tables */
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} }
359};
360
361static int bq25890_field_read(struct bq25890_device *bq,
362 enum bq25890_fields field_id)
363{
364 int ret;
365 int val;
366
367 ret = regmap_field_read(bq->rmap_fields[field_id], &val);
368 if (ret < 0)
369 return ret;
370
371 return val;
372}
373
374static int bq25890_field_write(struct bq25890_device *bq,
375 enum bq25890_fields field_id, u8 val)
376{
377 return regmap_field_write(bq->rmap_fields[field_id], val);
378}
379
380static u8 bq25890_find_idx(u32 value, enum bq25890_table_ids id)
381{
382 u8 idx;
383
384 if (id >= TBL_TREG) {
385 const u32 *tbl = bq25890_tables[id].lt.tbl;
386 u32 tbl_size = bq25890_tables[id].lt.size;
387
388 for (idx = 1; idx < tbl_size && tbl[idx] <= value; idx++)
389 ;
390 } else {
391 const struct bq25890_range *rtbl = &bq25890_tables[id].rt;
392 u8 rtbl_size;
393
394 rtbl_size = (rtbl->max - rtbl->min) / rtbl->step + 1;
395
396 for (idx = 1;
397 idx < rtbl_size && (idx * rtbl->step + rtbl->min <= value);
398 idx++)
399 ;
400 }
401
402 return idx - 1;
403}
404
405static u32 bq25890_find_val(u8 idx, enum bq25890_table_ids id)
406{
407 const struct bq25890_range *rtbl;
408
409 /* lookup table? */
410 if (id >= TBL_TREG)
411 return bq25890_tables[id].lt.tbl[idx];
412
413 /* range table */
414 rtbl = &bq25890_tables[id].rt;
415
416 return (rtbl->min + idx * rtbl->step);
417}
418
419enum bq25890_status {
420 STATUS_NOT_CHARGING,
421 STATUS_PRE_CHARGING,
422 STATUS_FAST_CHARGING,
423 STATUS_TERMINATION_DONE,
424};
425
426enum bq25890_chrg_fault {
427 CHRG_FAULT_NORMAL,
428 CHRG_FAULT_INPUT,
429 CHRG_FAULT_THERMAL_SHUTDOWN,
430 CHRG_FAULT_TIMER_EXPIRED,
431};
432
433enum bq25890_ntc_fault {
434 NTC_FAULT_NORMAL = 0,
435 NTC_FAULT_WARM = 2,
436 NTC_FAULT_COOL = 3,
437 NTC_FAULT_COLD = 5,
438 NTC_FAULT_HOT = 6,
439};
440
441static bool bq25890_is_adc_property(enum power_supply_property psp)
442{
443 switch (psp) {
444 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
445 case POWER_SUPPLY_PROP_CURRENT_NOW:
446 case POWER_SUPPLY_PROP_TEMP:
447 return true;
448
449 default:
450 return false;
451 }
452}
453
454static irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq);
455
456static int bq25890_get_vbus_voltage(struct bq25890_device *bq)
457{
458 int ret;
459
460 ret = bq25890_field_read(bq, F_VBUSV);
461 if (ret < 0)
462 return ret;
463
464 return bq25890_find_val(ret, TBL_VBUSV);
465}
466
467static void bq25890_update_state(struct bq25890_device *bq,
468 enum power_supply_property psp,
469 struct bq25890_state *state)
470{
471 bool do_adc_conv;
472 int ret;
473
474 mutex_lock(&bq->lock);
475 /* update state in case we lost an interrupt */
476 __bq25890_handle_irq(bq);
477 *state = bq->state;
478 do_adc_conv = (!state->online || state->hiz) && bq25890_is_adc_property(psp);
479 if (do_adc_conv)
480 bq25890_field_write(bq, F_CONV_START, 1);
481 mutex_unlock(&bq->lock);
482
483 if (do_adc_conv)
484 regmap_field_read_poll_timeout(bq->rmap_fields[F_CONV_START],
485 ret, !ret, 25000, 1000000);
486}
487
488static int bq25890_power_supply_get_property(struct power_supply *psy,
489 enum power_supply_property psp,
490 union power_supply_propval *val)
491{
492 struct bq25890_device *bq = power_supply_get_drvdata(psy);
493 struct bq25890_state state;
494 int ret;
495
496 bq25890_update_state(bq, psp, &state);
497
498 switch (psp) {
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;
509 else
510 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
511
512 break;
513
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;
525 break;
526
527 case POWER_SUPPLY_PROP_MANUFACTURER:
528 val->strval = BQ25890_MANUFACTURER;
529 break;
530
531 case POWER_SUPPLY_PROP_MODEL_NAME:
532 val->strval = bq25890_chip_name[bq->chip_version];
533 break;
534
535 case POWER_SUPPLY_PROP_ONLINE:
536 val->intval = state.online && !state.hiz;
537 break;
538
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;
548 else
549 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
550 break;
551
552 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
553 val->intval = bq25890_find_val(bq->init_data.iprechg, TBL_ITERM);
554 break;
555
556 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
557 val->intval = bq25890_find_val(bq->init_data.iterm, TBL_ITERM);
558 break;
559
560 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
561 ret = bq25890_field_read(bq, F_IINLIM);
562 if (ret < 0)
563 return ret;
564
565 val->intval = bq25890_find_val(ret, TBL_IINLIM);
566 break;
567
568 case POWER_SUPPLY_PROP_CURRENT_NOW: /* I_BAT now */
569 /*
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
575 */
576 ret = bq25890_field_read(bq, F_ICHGR); /* read measured value */
577 if (ret < 0)
578 return ret;
579
580 /* converted_val = ADC_val * 50mA (table 10.3.19) */
581 val->intval = ret * -50000;
582 break;
583
584 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: /* I_BAT user limit */
585 /*
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.
589 *
590 * This value reflects the current hardware setting.
591 *
592 * The POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX is the
593 * maximum value of this property.
594 */
595 ret = bq25890_field_read(bq, F_ICHG);
596 if (ret < 0)
597 return ret;
598 val->intval = bq25890_find_val(ret, TBL_ICHG);
599
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);
603 if (ret < 0)
604 return ret;
605
606 if (ret)
607 val->intval /= 5;
608 else
609 val->intval /= 2;
610 }
611 break;
612
613 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: /* I_BAT max */
614 /*
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.
618 *
619 * This value is constant for each battery and set from DT.
620 */
621 val->intval = bq25890_find_val(bq->init_data.ichg, TBL_ICHG);
622 break;
623
624 case POWER_SUPPLY_PROP_VOLTAGE_NOW: /* V_BAT now */
625 /*
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
631 */
632 ret = bq25890_field_read(bq, F_BATV); /* read measured value */
633 if (ret < 0)
634 return ret;
635
636 /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
637 val->intval = 2304000 + ret * 20000;
638 break;
639
640 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: /* V_BAT user limit */
641 /*
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.
645 *
646 * This value reflects the current hardware setting.
647 *
648 * The POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX is the
649 * maximum value of this property.
650 */
651 ret = bq25890_field_read(bq, F_VREG);
652 if (ret < 0)
653 return ret;
654
655 val->intval = bq25890_find_val(ret, TBL_VREG);
656 break;
657
658 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: /* V_BAT max */
659 /*
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.
663 *
664 * This value is constant for each battery and set from DT.
665 */
666 val->intval = bq25890_find_val(bq->init_data.vreg, TBL_VREG);
667 break;
668
669 case POWER_SUPPLY_PROP_TEMP:
670 ret = bq25890_field_read(bq, F_TSPCT);
671 if (ret < 0)
672 return ret;
673
674 /* convert TS percentage into rough temperature */
675 val->intval = bq25890_find_val(ret, TBL_TSPCT);
676 break;
677
678 default:
679 return -EINVAL;
680 }
681
682 return 0;
683}
684
685static int bq25890_power_supply_set_property(struct power_supply *psy,
686 enum power_supply_property psp,
687 const union power_supply_propval *val)
688{
689 struct bq25890_device *bq = power_supply_get_drvdata(psy);
690 struct bq25890_state state;
691 int maxval, ret;
692 u8 lval;
693
694 switch (psp) {
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);
708 if (!ret)
709 bq->force_hiz = !val->intval;
710 bq25890_update_state(bq, psp, &state);
711 return ret;
712 default:
713 return -EINVAL;
714 }
715}
716
717static int bq25890_power_supply_property_is_writeable(struct power_supply *psy,
718 enum power_supply_property psp)
719{
720 switch (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:
725 return true;
726 default:
727 return false;
728 }
729}
730
731/*
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.
735 */
736static int bq25890_charger_get_scaled_iinlim_regval(struct bq25890_device *bq,
737 int iinlim_ua)
738{
739 iinlim_ua = iinlim_ua * bq->iinlim_percentage / 100;
740 return bq25890_find_idx(iinlim_ua, TBL_IINLIM);
741}
742
743/* On the BQ25892 try to get charger-type info from our supplier */
744static void bq25890_charger_external_power_changed(struct power_supply *psy)
745{
746 struct bq25890_device *bq = power_supply_get_drvdata(psy);
747 union power_supply_propval val;
748 int input_current_limit, ret;
749
750 if (bq->chip_version != BQ25892)
751 return;
752
753 ret = power_supply_get_property_from_supplier(bq->charger,
754 POWER_SUPPLY_PROP_USB_TYPE,
755 &val);
756 if (ret)
757 return;
758
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);
766 }
767 break;
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);
771 break;
772 case POWER_SUPPLY_USB_TYPE_SDP:
773 default:
774 input_current_limit = bq25890_charger_get_scaled_iinlim_regval(bq, 500000);
775 }
776
777 bq25890_field_write(bq, F_IINLIM, input_current_limit);
778}
779
780static int bq25890_get_chip_state(struct bq25890_device *bq,
781 struct bq25890_state *state)
782{
783 int i, ret;
784
785 struct {
786 enum bq25890_fields id;
787 u8 *data;
788 } state_fields[] = {
789 {F_CHG_STAT, &state->chrg_status},
790 {F_PG_STAT, &state->online},
791 {F_EN_HIZ, &state->hiz},
792 {F_VSYS_STAT, &state->vsys_status},
793 {F_BOOST_FAULT, &state->boost_fault},
794 {F_BAT_FAULT, &state->bat_fault},
795 {F_CHG_FAULT, &state->chrg_fault},
796 {F_NTC_FAULT, &state->ntc_fault}
797 };
798
799 for (i = 0; i < ARRAY_SIZE(state_fields); i++) {
800 ret = bq25890_field_read(bq, state_fields[i].id);
801 if (ret < 0)
802 return ret;
803
804 *state_fields[i].data = ret;
805 }
806
807 dev_dbg(bq->dev, "S:CHG/PG/HIZ/VSYS=%d/%d/%d/%d, F:CHG/BOOST/BAT/NTC=%d/%d/%d/%d\n",
808 state->chrg_status, state->online,
809 state->hiz, state->vsys_status,
810 state->chrg_fault, state->boost_fault,
811 state->bat_fault, state->ntc_fault);
812
813 return 0;
814}
815
816static irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq)
817{
818 bool adc_conv_rate, new_adc_conv_rate;
819 struct bq25890_state new_state;
820 int ret;
821
822 ret = bq25890_get_chip_state(bq, &new_state);
823 if (ret < 0)
824 return IRQ_NONE;
825
826 if (!memcmp(&bq->state, &new_state, sizeof(new_state)))
827 return IRQ_NONE;
828
829 /*
830 * Restore HiZ bit in case it was set by user. The chip does not retain
831 * this bit on cable replug, hence the bit must be reset manually here.
832 */
833 if (new_state.online && !bq->state.online && bq->force_hiz) {
834 ret = bq25890_field_write(bq, F_EN_HIZ, bq->force_hiz);
835 if (ret < 0)
836 goto error;
837 new_state.hiz = 1;
838 }
839
840 /* Should period ADC sampling be enabled? */
841 adc_conv_rate = bq->state.online && !bq->state.hiz;
842 new_adc_conv_rate = new_state.online && !new_state.hiz;
843
844 if (new_adc_conv_rate != adc_conv_rate) {
845 ret = bq25890_field_write(bq, F_CONV_RATE, new_adc_conv_rate);
846 if (ret < 0)
847 goto error;
848 }
849
850 bq->state = new_state;
851 power_supply_changed(bq->charger);
852
853 return IRQ_HANDLED;
854error:
855 dev_err(bq->dev, "Error communicating with the chip: %pe\n",
856 ERR_PTR(ret));
857 return IRQ_HANDLED;
858}
859
860static irqreturn_t bq25890_irq_handler_thread(int irq, void *private)
861{
862 struct bq25890_device *bq = private;
863 irqreturn_t ret;
864
865 mutex_lock(&bq->lock);
866 ret = __bq25890_handle_irq(bq);
867 mutex_unlock(&bq->lock);
868
869 return ret;
870}
871
872static int bq25890_chip_reset(struct bq25890_device *bq)
873{
874 int ret;
875 int rst_check_counter = 10;
876
877 ret = bq25890_field_write(bq, F_REG_RST, 1);
878 if (ret < 0)
879 return ret;
880
881 do {
882 ret = bq25890_field_read(bq, F_REG_RST);
883 if (ret < 0)
884 return ret;
885
886 usleep_range(5, 10);
887 } while (ret == 1 && --rst_check_counter);
888
889 if (!rst_check_counter)
890 return -ETIMEDOUT;
891
892 return 0;
893}
894
895static int bq25890_rw_init_data(struct bq25890_device *bq)
896{
897 bool write = !bq->read_back_init_data;
898 int ret;
899 int i;
900
901 const struct {
902 enum bq25890_fields id;
903 u8 *value;
904 } init_data[] = {
905 {F_ICHG, &bq->init_data.ichg},
906 {F_VREG, &bq->init_data.vreg},
907 {F_ITERM, &bq->init_data.iterm},
908 {F_IPRECHG, &bq->init_data.iprechg},
909 {F_SYSVMIN, &bq->init_data.sysvmin},
910 {F_BOOSTV, &bq->init_data.boostv},
911 {F_BOOSTI, &bq->init_data.boosti},
912 {F_BOOSTF, &bq->init_data.boostf},
913 {F_EN_ILIM, &bq->init_data.ilim_en},
914 {F_TREG, &bq->init_data.treg},
915 {F_BATCMP, &bq->init_data.rbatcomp},
916 {F_VCLAMP, &bq->init_data.vclamp},
917 };
918
919 for (i = 0; i < ARRAY_SIZE(init_data); i++) {
920 if (write) {
921 ret = bq25890_field_write(bq, init_data[i].id,
922 *init_data[i].value);
923 } else {
924 ret = bq25890_field_read(bq, init_data[i].id);
925 if (ret >= 0)
926 *init_data[i].value = ret;
927 }
928 if (ret < 0) {
929 dev_dbg(bq->dev, "Accessing init data failed %d\n", ret);
930 return ret;
931 }
932 }
933
934 return 0;
935}
936
937static int bq25890_hw_init(struct bq25890_device *bq)
938{
939 int ret;
940
941 if (!bq->skip_reset) {
942 ret = bq25890_chip_reset(bq);
943 if (ret < 0) {
944 dev_dbg(bq->dev, "Reset failed %d\n", ret);
945 return ret;
946 }
947 } else {
948 /*
949 * Ensure charging is enabled, on some boards where the fw
950 * takes care of initalizition F_CHG_CFG is set to 0 before
951 * handing control over to the OS.
952 */
953 ret = bq25890_field_write(bq, F_CHG_CFG, 1);
954 if (ret < 0) {
955 dev_dbg(bq->dev, "Enabling charging failed %d\n", ret);
956 return ret;
957 }
958 }
959
960 /* disable watchdog */
961 ret = bq25890_field_write(bq, F_WD, 0);
962 if (ret < 0) {
963 dev_dbg(bq->dev, "Disabling watchdog failed %d\n", ret);
964 return ret;
965 }
966
967 /* initialize currents/voltages and other parameters */
968 ret = bq25890_rw_init_data(bq);
969 if (ret)
970 return ret;
971
972 ret = bq25890_get_chip_state(bq, &bq->state);
973 if (ret < 0) {
974 dev_dbg(bq->dev, "Get state failed %d\n", ret);
975 return ret;
976 }
977
978 /* Configure ADC for continuous conversions when charging */
979 ret = bq25890_field_write(bq, F_CONV_RATE, bq->state.online && !bq->state.hiz);
980 if (ret < 0) {
981 dev_dbg(bq->dev, "Config ADC failed %d\n", ret);
982 return ret;
983 }
984
985 return 0;
986}
987
988static const enum power_supply_property bq25890_power_supply_props[] = {
989 POWER_SUPPLY_PROP_MANUFACTURER,
990 POWER_SUPPLY_PROP_MODEL_NAME,
991 POWER_SUPPLY_PROP_STATUS,
992 POWER_SUPPLY_PROP_CHARGE_TYPE,
993 POWER_SUPPLY_PROP_ONLINE,
994 POWER_SUPPLY_PROP_HEALTH,
995 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
996 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
997 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
998 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
999 POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
1000 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
1001 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
1002 POWER_SUPPLY_PROP_VOLTAGE_NOW,
1003 POWER_SUPPLY_PROP_CURRENT_NOW,
1004 POWER_SUPPLY_PROP_TEMP,
1005};
1006
1007static char *bq25890_charger_supplied_to[] = {
1008 "main-battery",
1009};
1010
1011static const struct power_supply_desc bq25890_power_supply_desc = {
1012 .type = POWER_SUPPLY_TYPE_USB,
1013 .properties = bq25890_power_supply_props,
1014 .num_properties = ARRAY_SIZE(bq25890_power_supply_props),
1015 .get_property = bq25890_power_supply_get_property,
1016 .set_property = bq25890_power_supply_set_property,
1017 .property_is_writeable = bq25890_power_supply_property_is_writeable,
1018 .external_power_changed = bq25890_charger_external_power_changed,
1019};
1020
1021static int bq25890_power_supply_init(struct bq25890_device *bq)
1022{
1023 struct power_supply_config psy_cfg = { .drv_data = bq, };
1024
1025 /* Get ID for the device */
1026 mutex_lock(&bq25890_id_mutex);
1027 bq->id = idr_alloc(&bq25890_id, bq, 0, 0, GFP_KERNEL);
1028 mutex_unlock(&bq25890_id_mutex);
1029 if (bq->id < 0)
1030 return bq->id;
1031
1032 snprintf(bq->name, sizeof(bq->name), "bq25890-charger-%d", bq->id);
1033 bq->desc = bq25890_power_supply_desc;
1034 bq->desc.name = bq->name;
1035
1036 psy_cfg.supplied_to = bq25890_charger_supplied_to;
1037 psy_cfg.num_supplicants = ARRAY_SIZE(bq25890_charger_supplied_to);
1038
1039 bq->charger = devm_power_supply_register(bq->dev, &bq->desc, &psy_cfg);
1040
1041 return PTR_ERR_OR_ZERO(bq->charger);
1042}
1043
1044static int bq25890_set_otg_cfg(struct bq25890_device *bq, u8 val)
1045{
1046 int ret;
1047
1048 ret = bq25890_field_write(bq, F_OTG_CFG, val);
1049 if (ret < 0)
1050 dev_err(bq->dev, "Error switching to boost/charger mode: %d\n", ret);
1051
1052 return ret;
1053}
1054
1055static void bq25890_pump_express_work(struct work_struct *data)
1056{
1057 struct bq25890_device *bq =
1058 container_of(data, struct bq25890_device, pump_express_work.work);
1059 union power_supply_propval value;
1060 int voltage, i, ret;
1061
1062 dev_dbg(bq->dev, "Start to request input voltage increasing\n");
1063
1064 /* If there is a second charger put in Hi-Z mode */
1065 if (bq->secondary_chrg) {
1066 value.intval = 0;
1067 power_supply_set_property(bq->secondary_chrg, POWER_SUPPLY_PROP_ONLINE, &value);
1068 }
1069
1070 /* Enable current pulse voltage control protocol */
1071 ret = bq25890_field_write(bq, F_PUMPX_EN, 1);
1072 if (ret < 0)
1073 goto error_print;
1074
1075 for (i = 0; i < PUMP_EXPRESS_MAX_TRIES; i++) {
1076 voltage = bq25890_get_vbus_voltage(bq);
1077 if (voltage < 0)
1078 goto error_print;
1079 dev_dbg(bq->dev, "input voltage = %d uV\n", voltage);
1080
1081 if ((voltage + PUMP_EXPRESS_VBUS_MARGIN_uV) >
1082 bq->pump_express_vbus_max)
1083 break;
1084
1085 ret = bq25890_field_write(bq, F_PUMPX_UP, 1);
1086 if (ret < 0)
1087 goto error_print;
1088
1089 /* Note a single PUMPX up pulse-sequence takes 2.1s */
1090 ret = regmap_field_read_poll_timeout(bq->rmap_fields[F_PUMPX_UP],
1091 ret, !ret, 100000, 3000000);
1092 if (ret < 0)
1093 goto error_print;
1094
1095 /* Make sure ADC has sampled Vbus before checking again */
1096 msleep(1000);
1097 }
1098
1099 bq25890_field_write(bq, F_PUMPX_EN, 0);
1100
1101 if (bq->secondary_chrg) {
1102 value.intval = 1;
1103 power_supply_set_property(bq->secondary_chrg, POWER_SUPPLY_PROP_ONLINE, &value);
1104 }
1105
1106 dev_info(bq->dev, "Hi-voltage charging requested, input voltage is %d mV\n",
1107 voltage);
1108
1109 return;
1110error_print:
1111 bq25890_field_write(bq, F_PUMPX_EN, 0);
1112 dev_err(bq->dev, "Failed to request hi-voltage charging\n");
1113}
1114
1115static void bq25890_usb_work(struct work_struct *data)
1116{
1117 int ret;
1118 struct bq25890_device *bq =
1119 container_of(data, struct bq25890_device, usb_work);
1120
1121 switch (bq->usb_event) {
1122 case USB_EVENT_ID:
1123 /* Enable boost mode */
1124 bq25890_set_otg_cfg(bq, 1);
1125 break;
1126
1127 case USB_EVENT_NONE:
1128 /* Disable boost mode */
1129 ret = bq25890_set_otg_cfg(bq, 0);
1130 if (ret == 0)
1131 power_supply_changed(bq->charger);
1132 break;
1133 }
1134}
1135
1136static int bq25890_usb_notifier(struct notifier_block *nb, unsigned long val,
1137 void *priv)
1138{
1139 struct bq25890_device *bq =
1140 container_of(nb, struct bq25890_device, usb_nb);
1141
1142 bq->usb_event = val;
1143 queue_work(system_power_efficient_wq, &bq->usb_work);
1144
1145 return NOTIFY_OK;
1146}
1147
1148#ifdef CONFIG_REGULATOR
1149static int bq25890_vbus_enable(struct regulator_dev *rdev)
1150{
1151 struct bq25890_device *bq = rdev_get_drvdata(rdev);
1152 union power_supply_propval val = {
1153 .intval = 0,
1154 };
1155
1156 /*
1157 * When enabling 5V boost / Vbus output, we need to put the secondary
1158 * charger in Hi-Z mode to avoid it trying to charge the secondary
1159 * battery from the 5V boost output.
1160 */
1161 if (bq->secondary_chrg)
1162 power_supply_set_property(bq->secondary_chrg, POWER_SUPPLY_PROP_ONLINE, &val);
1163
1164 return bq25890_set_otg_cfg(bq, 1);
1165}
1166
1167static int bq25890_vbus_disable(struct regulator_dev *rdev)
1168{
1169 struct bq25890_device *bq = rdev_get_drvdata(rdev);
1170 union power_supply_propval val = {
1171 .intval = 1,
1172 };
1173 int ret;
1174
1175 ret = bq25890_set_otg_cfg(bq, 0);
1176 if (ret)
1177 return ret;
1178
1179 if (bq->secondary_chrg)
1180 power_supply_set_property(bq->secondary_chrg, POWER_SUPPLY_PROP_ONLINE, &val);
1181
1182 return 0;
1183}
1184
1185static int bq25890_vbus_is_enabled(struct regulator_dev *rdev)
1186{
1187 struct bq25890_device *bq = rdev_get_drvdata(rdev);
1188
1189 return bq25890_field_read(bq, F_OTG_CFG);
1190}
1191
1192static int bq25890_vbus_get_voltage(struct regulator_dev *rdev)
1193{
1194 struct bq25890_device *bq = rdev_get_drvdata(rdev);
1195
1196 return bq25890_get_vbus_voltage(bq);
1197}
1198
1199static int bq25890_vsys_get_voltage(struct regulator_dev *rdev)
1200{
1201 struct bq25890_device *bq = rdev_get_drvdata(rdev);
1202 int ret;
1203
1204 /* Should be some output voltage ? */
1205 ret = bq25890_field_read(bq, F_SYSV); /* read measured value */
1206 if (ret < 0)
1207 return ret;
1208
1209 /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
1210 return 2304000 + ret * 20000;
1211}
1212
1213static const struct regulator_ops bq25890_vbus_ops = {
1214 .enable = bq25890_vbus_enable,
1215 .disable = bq25890_vbus_disable,
1216 .is_enabled = bq25890_vbus_is_enabled,
1217 .get_voltage = bq25890_vbus_get_voltage,
1218};
1219
1220static const struct regulator_desc bq25890_vbus_desc = {
1221 .name = "usb_otg_vbus",
1222 .of_match = "usb-otg-vbus",
1223 .type = REGULATOR_VOLTAGE,
1224 .owner = THIS_MODULE,
1225 .ops = &bq25890_vbus_ops,
1226};
1227
1228static const struct regulator_ops bq25890_vsys_ops = {
1229 .get_voltage = bq25890_vsys_get_voltage,
1230};
1231
1232static const struct regulator_desc bq25890_vsys_desc = {
1233 .name = "vsys",
1234 .of_match = "vsys",
1235 .type = REGULATOR_VOLTAGE,
1236 .owner = THIS_MODULE,
1237 .ops = &bq25890_vsys_ops,
1238};
1239
1240static int bq25890_register_regulator(struct bq25890_device *bq)
1241{
1242 struct bq25890_platform_data *pdata = dev_get_platdata(bq->dev);
1243 struct regulator_config cfg = {
1244 .dev = bq->dev,
1245 .driver_data = bq,
1246 };
1247 struct regulator_dev *reg;
1248
1249 if (pdata)
1250 cfg.init_data = pdata->regulator_init_data;
1251
1252 reg = devm_regulator_register(bq->dev, &bq25890_vbus_desc, &cfg);
1253 if (IS_ERR(reg)) {
1254 return dev_err_probe(bq->dev, PTR_ERR(reg),
1255 "registering vbus regulator");
1256 }
1257
1258 /* pdata->regulator_init_data is for vbus only */
1259 cfg.init_data = NULL;
1260 reg = devm_regulator_register(bq->dev, &bq25890_vsys_desc, &cfg);
1261 if (IS_ERR(reg)) {
1262 return dev_err_probe(bq->dev, PTR_ERR(reg),
1263 "registering vsys regulator");
1264 }
1265
1266 return 0;
1267}
1268#else
1269static inline int
1270bq25890_register_regulator(struct bq25890_device *bq)
1271{
1272 return 0;
1273}
1274#endif
1275
1276static int bq25890_get_chip_version(struct bq25890_device *bq)
1277{
1278 int id, rev;
1279
1280 id = bq25890_field_read(bq, F_PN);
1281 if (id < 0) {
1282 dev_err(bq->dev, "Cannot read chip ID: %d\n", id);
1283 return id;
1284 }
1285
1286 rev = bq25890_field_read(bq, F_DEV_REV);
1287 if (rev < 0) {
1288 dev_err(bq->dev, "Cannot read chip revision: %d\n", rev);
1289 return rev;
1290 }
1291
1292 switch (id) {
1293 case BQ25890_ID:
1294 bq->chip_version = BQ25890;
1295 break;
1296
1297 /* BQ25892 and BQ25896 share same ID 0 */
1298 case BQ25896_ID:
1299 switch (rev) {
1300 case 2:
1301 bq->chip_version = BQ25896;
1302 break;
1303 case 1:
1304 bq->chip_version = BQ25892;
1305 break;
1306 default:
1307 dev_err(bq->dev,
1308 "Unknown device revision %d, assume BQ25892\n",
1309 rev);
1310 bq->chip_version = BQ25892;
1311 }
1312 break;
1313
1314 case BQ25895_ID:
1315 bq->chip_version = BQ25895;
1316 break;
1317
1318 default:
1319 dev_err(bq->dev, "Unknown chip ID %d\n", id);
1320 return -ENODEV;
1321 }
1322
1323 return 0;
1324}
1325
1326static int bq25890_irq_probe(struct bq25890_device *bq)
1327{
1328 struct gpio_desc *irq;
1329
1330 irq = devm_gpiod_get(bq->dev, BQ25890_IRQ_PIN, GPIOD_IN);
1331 if (IS_ERR(irq))
1332 return dev_err_probe(bq->dev, PTR_ERR(irq),
1333 "Could not probe irq pin.\n");
1334
1335 return gpiod_to_irq(irq);
1336}
1337
1338static int bq25890_fw_read_u32_props(struct bq25890_device *bq)
1339{
1340 int ret;
1341 u32 property;
1342 int i;
1343 struct bq25890_init_data *init = &bq->init_data;
1344 struct {
1345 char *name;
1346 bool optional;
1347 enum bq25890_table_ids tbl_id;
1348 u8 *conv_data; /* holds converted value from given property */
1349 } props[] = {
1350 /* required properties */
1351 {"ti,charge-current", false, TBL_ICHG, &init->ichg},
1352 {"ti,battery-regulation-voltage", false, TBL_VREG, &init->vreg},
1353 {"ti,termination-current", false, TBL_ITERM, &init->iterm},
1354 {"ti,precharge-current", false, TBL_ITERM, &init->iprechg},
1355 {"ti,minimum-sys-voltage", false, TBL_SYSVMIN, &init->sysvmin},
1356 {"ti,boost-voltage", false, TBL_BOOSTV, &init->boostv},
1357 {"ti,boost-max-current", false, TBL_BOOSTI, &init->boosti},
1358
1359 /* optional properties */
1360 {"ti,thermal-regulation-threshold", true, TBL_TREG, &init->treg},
1361 {"ti,ibatcomp-micro-ohms", true, TBL_RBATCOMP, &init->rbatcomp},
1362 {"ti,ibatcomp-clamp-microvolt", true, TBL_VBATCOMP, &init->vclamp},
1363 };
1364
1365 /* initialize data for optional properties */
1366 init->treg = 3; /* 120 degrees Celsius */
1367 init->rbatcomp = init->vclamp = 0; /* IBAT compensation disabled */
1368
1369 for (i = 0; i < ARRAY_SIZE(props); i++) {
1370 ret = device_property_read_u32(bq->dev, props[i].name,
1371 &property);
1372 if (ret < 0) {
1373 if (props[i].optional)
1374 continue;
1375
1376 dev_err(bq->dev, "Unable to read property %d %s\n", ret,
1377 props[i].name);
1378
1379 return ret;
1380 }
1381
1382 *props[i].conv_data = bq25890_find_idx(property,
1383 props[i].tbl_id);
1384 }
1385
1386 return 0;
1387}
1388
1389static int bq25890_fw_probe(struct bq25890_device *bq)
1390{
1391 int ret;
1392 struct bq25890_init_data *init = &bq->init_data;
1393 const char *str;
1394 u32 val;
1395
1396 ret = device_property_read_string(bq->dev, "linux,secondary-charger-name", &str);
1397 if (ret == 0) {
1398 bq->secondary_chrg = power_supply_get_by_name(str);
1399 if (!bq->secondary_chrg)
1400 return -EPROBE_DEFER;
1401 }
1402
1403 /* Optional, left at 0 if property is not present */
1404 device_property_read_u32(bq->dev, "linux,pump-express-vbus-max",
1405 &bq->pump_express_vbus_max);
1406
1407 ret = device_property_read_u32(bq->dev, "linux,iinlim-percentage", &val);
1408 if (ret == 0) {
1409 if (val > 100) {
1410 dev_err(bq->dev, "Error linux,iinlim-percentage %u > 100\n", val);
1411 return -EINVAL;
1412 }
1413 bq->iinlim_percentage = val;
1414 } else {
1415 bq->iinlim_percentage = 100;
1416 }
1417
1418 bq->skip_reset = device_property_read_bool(bq->dev, "linux,skip-reset");
1419 bq->read_back_init_data = device_property_read_bool(bq->dev,
1420 "linux,read-back-settings");
1421 if (bq->read_back_init_data)
1422 return 0;
1423
1424 ret = bq25890_fw_read_u32_props(bq);
1425 if (ret < 0)
1426 return ret;
1427
1428 init->ilim_en = device_property_read_bool(bq->dev, "ti,use-ilim-pin");
1429 init->boostf = device_property_read_bool(bq->dev, "ti,boost-low-freq");
1430
1431 return 0;
1432}
1433
1434static void bq25890_non_devm_cleanup(void *data)
1435{
1436 struct bq25890_device *bq = data;
1437
1438 cancel_delayed_work_sync(&bq->pump_express_work);
1439
1440 if (bq->id >= 0) {
1441 mutex_lock(&bq25890_id_mutex);
1442 idr_remove(&bq25890_id, bq->id);
1443 mutex_unlock(&bq25890_id_mutex);
1444 }
1445}
1446
1447static int bq25890_probe(struct i2c_client *client)
1448{
1449 struct device *dev = &client->dev;
1450 struct bq25890_device *bq;
1451 int ret;
1452
1453 bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL);
1454 if (!bq)
1455 return -ENOMEM;
1456
1457 bq->client = client;
1458 bq->dev = dev;
1459 bq->id = -1;
1460
1461 mutex_init(&bq->lock);
1462 INIT_DELAYED_WORK(&bq->pump_express_work, bq25890_pump_express_work);
1463
1464 bq->rmap = devm_regmap_init_i2c(client, &bq25890_regmap_config);
1465 if (IS_ERR(bq->rmap))
1466 return dev_err_probe(dev, PTR_ERR(bq->rmap),
1467 "failed to allocate register map\n");
1468
1469 ret = devm_regmap_field_bulk_alloc(dev, bq->rmap, bq->rmap_fields,
1470 bq25890_reg_fields, F_MAX_FIELDS);
1471 if (ret)
1472 return ret;
1473
1474 i2c_set_clientdata(client, bq);
1475
1476 ret = bq25890_get_chip_version(bq);
1477 if (ret) {
1478 dev_err(dev, "Cannot read chip ID or unknown chip: %d\n", ret);
1479 return ret;
1480 }
1481
1482 ret = bq25890_fw_probe(bq);
1483 if (ret < 0)
1484 return dev_err_probe(dev, ret, "reading device properties\n");
1485
1486 ret = bq25890_hw_init(bq);
1487 if (ret < 0) {
1488 dev_err(dev, "Cannot initialize the chip: %d\n", ret);
1489 return ret;
1490 }
1491
1492 if (client->irq <= 0)
1493 client->irq = bq25890_irq_probe(bq);
1494
1495 if (client->irq < 0) {
1496 dev_err(dev, "No irq resource found.\n");
1497 return client->irq;
1498 }
1499
1500 /* OTG reporting */
1501 bq->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1502
1503 /*
1504 * This must be before bq25890_power_supply_init(), so that it runs
1505 * after devm unregisters the power_supply.
1506 */
1507 ret = devm_add_action_or_reset(dev, bq25890_non_devm_cleanup, bq);
1508 if (ret)
1509 return ret;
1510
1511 ret = bq25890_register_regulator(bq);
1512 if (ret)
1513 return ret;
1514
1515 ret = bq25890_power_supply_init(bq);
1516 if (ret < 0)
1517 return dev_err_probe(dev, ret, "registering power supply\n");
1518
1519 ret = devm_request_threaded_irq(dev, client->irq, NULL,
1520 bq25890_irq_handler_thread,
1521 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1522 BQ25890_IRQ_PIN, bq);
1523 if (ret)
1524 return ret;
1525
1526 if (!IS_ERR_OR_NULL(bq->usb_phy)) {
1527 INIT_WORK(&bq->usb_work, bq25890_usb_work);
1528 bq->usb_nb.notifier_call = bq25890_usb_notifier;
1529 usb_register_notifier(bq->usb_phy, &bq->usb_nb);
1530 }
1531
1532 return 0;
1533}
1534
1535static void bq25890_remove(struct i2c_client *client)
1536{
1537 struct bq25890_device *bq = i2c_get_clientdata(client);
1538
1539 if (!IS_ERR_OR_NULL(bq->usb_phy)) {
1540 usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
1541 cancel_work_sync(&bq->usb_work);
1542 }
1543
1544 if (!bq->skip_reset) {
1545 /* reset all registers to default values */
1546 bq25890_chip_reset(bq);
1547 }
1548}
1549
1550static void bq25890_shutdown(struct i2c_client *client)
1551{
1552 struct bq25890_device *bq = i2c_get_clientdata(client);
1553
1554 /*
1555 * TODO this if + return should probably be removed, but that would
1556 * introduce a function change for boards using the usb-phy framework.
1557 * This needs to be tested on such a board before making this change.
1558 */
1559 if (!IS_ERR_OR_NULL(bq->usb_phy))
1560 return;
1561
1562 /*
1563 * Turn off the 5v Boost regulator which outputs Vbus to the device's
1564 * Micro-USB or Type-C USB port. Leaving this on drains power and
1565 * this avoids the PMIC on some device-models seeing this as Vbus
1566 * getting inserted after shutdown, causing the device to immediately
1567 * power-up again.
1568 */
1569 bq25890_set_otg_cfg(bq, 0);
1570}
1571
1572#ifdef CONFIG_PM_SLEEP
1573static int bq25890_suspend(struct device *dev)
1574{
1575 struct bq25890_device *bq = dev_get_drvdata(dev);
1576
1577 /*
1578 * If charger is removed, while in suspend, make sure ADC is diabled
1579 * since it consumes slightly more power.
1580 */
1581 return bq25890_field_write(bq, F_CONV_RATE, 0);
1582}
1583
1584static int bq25890_resume(struct device *dev)
1585{
1586 int ret;
1587 struct bq25890_device *bq = dev_get_drvdata(dev);
1588
1589 mutex_lock(&bq->lock);
1590
1591 ret = bq25890_get_chip_state(bq, &bq->state);
1592 if (ret < 0)
1593 goto unlock;
1594
1595 /* Re-enable ADC only if charger is plugged in. */
1596 if (bq->state.online) {
1597 ret = bq25890_field_write(bq, F_CONV_RATE, 1);
1598 if (ret < 0)
1599 goto unlock;
1600 }
1601
1602 /* signal userspace, maybe state changed while suspended */
1603 power_supply_changed(bq->charger);
1604
1605unlock:
1606 mutex_unlock(&bq->lock);
1607
1608 return ret;
1609}
1610#endif
1611
1612static const struct dev_pm_ops bq25890_pm = {
1613 SET_SYSTEM_SLEEP_PM_OPS(bq25890_suspend, bq25890_resume)
1614};
1615
1616static const struct i2c_device_id bq25890_i2c_ids[] = {
1617 { "bq25890", 0 },
1618 { "bq25892", 0 },
1619 { "bq25895", 0 },
1620 { "bq25896", 0 },
1621 {},
1622};
1623MODULE_DEVICE_TABLE(i2c, bq25890_i2c_ids);
1624
1625static const struct of_device_id bq25890_of_match[] = {
1626 { .compatible = "ti,bq25890", },
1627 { .compatible = "ti,bq25892", },
1628 { .compatible = "ti,bq25895", },
1629 { .compatible = "ti,bq25896", },
1630 { },
1631};
1632MODULE_DEVICE_TABLE(of, bq25890_of_match);
1633
1634#ifdef CONFIG_ACPI
1635static const struct acpi_device_id bq25890_acpi_match[] = {
1636 {"BQ258900", 0},
1637 {},
1638};
1639MODULE_DEVICE_TABLE(acpi, bq25890_acpi_match);
1640#endif
1641
1642static struct i2c_driver bq25890_driver = {
1643 .driver = {
1644 .name = "bq25890-charger",
1645 .of_match_table = of_match_ptr(bq25890_of_match),
1646 .acpi_match_table = ACPI_PTR(bq25890_acpi_match),
1647 .pm = &bq25890_pm,
1648 },
1649 .probe_new = bq25890_probe,
1650 .remove = bq25890_remove,
1651 .shutdown = bq25890_shutdown,
1652 .id_table = bq25890_i2c_ids,
1653};
1654module_i2c_driver(bq25890_driver);
1655
1656MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
1657MODULE_DESCRIPTION("bq25890 charger driver");
1658MODULE_LICENSE("GPL");