treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / power / supply / bq25890_charger.c
blobaebd1253dbc932796fab992df11c8e350dac2701
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * TI BQ25890 charger driver
5 * Copyright (C) 2015 Intel Corporation
6 */
8 #include <linux/module.h>
9 #include <linux/i2c.h>
10 #include <linux/power_supply.h>
11 #include <linux/regmap.h>
12 #include <linux/types.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/interrupt.h>
15 #include <linux/delay.h>
16 #include <linux/usb/phy.h>
18 #include <linux/acpi.h>
19 #include <linux/of.h>
21 #define BQ25890_MANUFACTURER "Texas Instruments"
22 #define BQ25890_IRQ_PIN "bq25890_irq"
24 #define BQ25890_ID 3
25 #define BQ25895_ID 7
26 #define BQ25896_ID 0
28 enum bq25890_chip_version {
29 BQ25890,
30 BQ25892,
31 BQ25895,
32 BQ25896,
35 enum bq25890_fields {
36 F_EN_HIZ, F_EN_ILIM, F_IILIM, /* Reg00 */
37 F_BHOT, F_BCOLD, F_VINDPM_OFS, /* Reg01 */
38 F_CONV_START, F_CONV_RATE, F_BOOSTF, F_ICO_EN,
39 F_HVDCP_EN, F_MAXC_EN, F_FORCE_DPM, F_AUTO_DPDM_EN, /* Reg02 */
40 F_BAT_LOAD_EN, F_WD_RST, F_OTG_CFG, F_CHG_CFG, F_SYSVMIN,
41 F_MIN_VBAT_SEL, /* Reg03 */
42 F_PUMPX_EN, F_ICHG, /* Reg04 */
43 F_IPRECHG, F_ITERM, /* Reg05 */
44 F_VREG, F_BATLOWV, F_VRECHG, /* Reg06 */
45 F_TERM_EN, F_STAT_DIS, F_WD, F_TMR_EN, F_CHG_TMR,
46 F_JEITA_ISET, /* Reg07 */
47 F_BATCMP, F_VCLAMP, F_TREG, /* Reg08 */
48 F_FORCE_ICO, F_TMR2X_EN, F_BATFET_DIS, F_JEITA_VSET,
49 F_BATFET_DLY, F_BATFET_RST_EN, F_PUMPX_UP, F_PUMPX_DN, /* Reg09 */
50 F_BOOSTV, F_PFM_OTG_DIS, F_BOOSTI, /* Reg0A */
51 F_VBUS_STAT, F_CHG_STAT, F_PG_STAT, F_SDP_STAT, F_0B_RSVD,
52 F_VSYS_STAT, /* Reg0B */
53 F_WD_FAULT, F_BOOST_FAULT, F_CHG_FAULT, F_BAT_FAULT,
54 F_NTC_FAULT, /* Reg0C */
55 F_FORCE_VINDPM, F_VINDPM, /* Reg0D */
56 F_THERM_STAT, F_BATV, /* Reg0E */
57 F_SYSV, /* Reg0F */
58 F_TSPCT, /* Reg10 */
59 F_VBUS_GD, F_VBUSV, /* Reg11 */
60 F_ICHGR, /* Reg12 */
61 F_VDPM_STAT, F_IDPM_STAT, F_IDPM_LIM, /* Reg13 */
62 F_REG_RST, F_ICO_OPTIMIZED, F_PN, F_TS_PROFILE, F_DEV_REV, /* Reg14 */
64 F_MAX_FIELDS
67 /* initial field values, converted to register values */
68 struct bq25890_init_data {
69 u8 ichg; /* charge current */
70 u8 vreg; /* regulation voltage */
71 u8 iterm; /* termination current */
72 u8 iprechg; /* precharge current */
73 u8 sysvmin; /* minimum system voltage limit */
74 u8 boostv; /* boost regulation voltage */
75 u8 boosti; /* boost current limit */
76 u8 boostf; /* boost frequency */
77 u8 ilim_en; /* enable ILIM pin */
78 u8 treg; /* thermal regulation threshold */
81 struct bq25890_state {
82 u8 online;
83 u8 chrg_status;
84 u8 chrg_fault;
85 u8 vsys_status;
86 u8 boost_fault;
87 u8 bat_fault;
90 struct bq25890_device {
91 struct i2c_client *client;
92 struct device *dev;
93 struct power_supply *charger;
95 struct usb_phy *usb_phy;
96 struct notifier_block usb_nb;
97 struct work_struct usb_work;
98 unsigned long usb_event;
100 struct regmap *rmap;
101 struct regmap_field *rmap_fields[F_MAX_FIELDS];
103 enum bq25890_chip_version chip_version;
104 struct bq25890_init_data init_data;
105 struct bq25890_state state;
107 struct mutex lock; /* protect state data */
110 static const struct regmap_range bq25890_readonly_reg_ranges[] = {
111 regmap_reg_range(0x0b, 0x0c),
112 regmap_reg_range(0x0e, 0x13),
115 static const struct regmap_access_table bq25890_writeable_regs = {
116 .no_ranges = bq25890_readonly_reg_ranges,
117 .n_no_ranges = ARRAY_SIZE(bq25890_readonly_reg_ranges),
120 static const struct regmap_range bq25890_volatile_reg_ranges[] = {
121 regmap_reg_range(0x00, 0x00),
122 regmap_reg_range(0x09, 0x09),
123 regmap_reg_range(0x0b, 0x14),
126 static const struct regmap_access_table bq25890_volatile_regs = {
127 .yes_ranges = bq25890_volatile_reg_ranges,
128 .n_yes_ranges = ARRAY_SIZE(bq25890_volatile_reg_ranges),
131 static const struct regmap_config bq25890_regmap_config = {
132 .reg_bits = 8,
133 .val_bits = 8,
135 .max_register = 0x14,
136 .cache_type = REGCACHE_RBTREE,
138 .wr_table = &bq25890_writeable_regs,
139 .volatile_table = &bq25890_volatile_regs,
142 static const struct reg_field bq25890_reg_fields[] = {
143 /* REG00 */
144 [F_EN_HIZ] = REG_FIELD(0x00, 7, 7),
145 [F_EN_ILIM] = REG_FIELD(0x00, 6, 6),
146 [F_IILIM] = REG_FIELD(0x00, 0, 5),
147 /* REG01 */
148 [F_BHOT] = REG_FIELD(0x01, 6, 7),
149 [F_BCOLD] = REG_FIELD(0x01, 5, 5),
150 [F_VINDPM_OFS] = REG_FIELD(0x01, 0, 4),
151 /* REG02 */
152 [F_CONV_START] = REG_FIELD(0x02, 7, 7),
153 [F_CONV_RATE] = REG_FIELD(0x02, 6, 6),
154 [F_BOOSTF] = REG_FIELD(0x02, 5, 5),
155 [F_ICO_EN] = REG_FIELD(0x02, 4, 4),
156 [F_HVDCP_EN] = REG_FIELD(0x02, 3, 3), // reserved on BQ25896
157 [F_MAXC_EN] = REG_FIELD(0x02, 2, 2), // reserved on BQ25896
158 [F_FORCE_DPM] = REG_FIELD(0x02, 1, 1),
159 [F_AUTO_DPDM_EN] = REG_FIELD(0x02, 0, 0),
160 /* REG03 */
161 [F_BAT_LOAD_EN] = REG_FIELD(0x03, 7, 7),
162 [F_WD_RST] = REG_FIELD(0x03, 6, 6),
163 [F_OTG_CFG] = REG_FIELD(0x03, 5, 5),
164 [F_CHG_CFG] = REG_FIELD(0x03, 4, 4),
165 [F_SYSVMIN] = REG_FIELD(0x03, 1, 3),
166 [F_MIN_VBAT_SEL] = REG_FIELD(0x03, 0, 0), // BQ25896 only
167 /* REG04 */
168 [F_PUMPX_EN] = REG_FIELD(0x04, 7, 7),
169 [F_ICHG] = REG_FIELD(0x04, 0, 6),
170 /* REG05 */
171 [F_IPRECHG] = REG_FIELD(0x05, 4, 7),
172 [F_ITERM] = REG_FIELD(0x05, 0, 3),
173 /* REG06 */
174 [F_VREG] = REG_FIELD(0x06, 2, 7),
175 [F_BATLOWV] = REG_FIELD(0x06, 1, 1),
176 [F_VRECHG] = REG_FIELD(0x06, 0, 0),
177 /* REG07 */
178 [F_TERM_EN] = REG_FIELD(0x07, 7, 7),
179 [F_STAT_DIS] = REG_FIELD(0x07, 6, 6),
180 [F_WD] = REG_FIELD(0x07, 4, 5),
181 [F_TMR_EN] = REG_FIELD(0x07, 3, 3),
182 [F_CHG_TMR] = REG_FIELD(0x07, 1, 2),
183 [F_JEITA_ISET] = REG_FIELD(0x07, 0, 0), // reserved on BQ25895
184 /* REG08 */
185 [F_BATCMP] = REG_FIELD(0x08, 5, 7),
186 [F_VCLAMP] = REG_FIELD(0x08, 2, 4),
187 [F_TREG] = REG_FIELD(0x08, 0, 1),
188 /* REG09 */
189 [F_FORCE_ICO] = REG_FIELD(0x09, 7, 7),
190 [F_TMR2X_EN] = REG_FIELD(0x09, 6, 6),
191 [F_BATFET_DIS] = REG_FIELD(0x09, 5, 5),
192 [F_JEITA_VSET] = REG_FIELD(0x09, 4, 4), // reserved on BQ25895
193 [F_BATFET_DLY] = REG_FIELD(0x09, 3, 3),
194 [F_BATFET_RST_EN] = REG_FIELD(0x09, 2, 2),
195 [F_PUMPX_UP] = REG_FIELD(0x09, 1, 1),
196 [F_PUMPX_DN] = REG_FIELD(0x09, 0, 0),
197 /* REG0A */
198 [F_BOOSTV] = REG_FIELD(0x0A, 4, 7),
199 [F_BOOSTI] = REG_FIELD(0x0A, 0, 2), // reserved on BQ25895
200 [F_PFM_OTG_DIS] = REG_FIELD(0x0A, 3, 3), // BQ25896 only
201 /* REG0B */
202 [F_VBUS_STAT] = REG_FIELD(0x0B, 5, 7),
203 [F_CHG_STAT] = REG_FIELD(0x0B, 3, 4),
204 [F_PG_STAT] = REG_FIELD(0x0B, 2, 2),
205 [F_SDP_STAT] = REG_FIELD(0x0B, 1, 1), // reserved on BQ25896
206 [F_VSYS_STAT] = REG_FIELD(0x0B, 0, 0),
207 /* REG0C */
208 [F_WD_FAULT] = REG_FIELD(0x0C, 7, 7),
209 [F_BOOST_FAULT] = REG_FIELD(0x0C, 6, 6),
210 [F_CHG_FAULT] = REG_FIELD(0x0C, 4, 5),
211 [F_BAT_FAULT] = REG_FIELD(0x0C, 3, 3),
212 [F_NTC_FAULT] = REG_FIELD(0x0C, 0, 2),
213 /* REG0D */
214 [F_FORCE_VINDPM] = REG_FIELD(0x0D, 7, 7),
215 [F_VINDPM] = REG_FIELD(0x0D, 0, 6),
216 /* REG0E */
217 [F_THERM_STAT] = REG_FIELD(0x0E, 7, 7),
218 [F_BATV] = REG_FIELD(0x0E, 0, 6),
219 /* REG0F */
220 [F_SYSV] = REG_FIELD(0x0F, 0, 6),
221 /* REG10 */
222 [F_TSPCT] = REG_FIELD(0x10, 0, 6),
223 /* REG11 */
224 [F_VBUS_GD] = REG_FIELD(0x11, 7, 7),
225 [F_VBUSV] = REG_FIELD(0x11, 0, 6),
226 /* REG12 */
227 [F_ICHGR] = REG_FIELD(0x12, 0, 6),
228 /* REG13 */
229 [F_VDPM_STAT] = REG_FIELD(0x13, 7, 7),
230 [F_IDPM_STAT] = REG_FIELD(0x13, 6, 6),
231 [F_IDPM_LIM] = REG_FIELD(0x13, 0, 5),
232 /* REG14 */
233 [F_REG_RST] = REG_FIELD(0x14, 7, 7),
234 [F_ICO_OPTIMIZED] = REG_FIELD(0x14, 6, 6),
235 [F_PN] = REG_FIELD(0x14, 3, 5),
236 [F_TS_PROFILE] = REG_FIELD(0x14, 2, 2),
237 [F_DEV_REV] = REG_FIELD(0x14, 0, 1)
241 * Most of the val -> idx conversions can be computed, given the minimum,
242 * maximum and the step between values. For the rest of conversions, we use
243 * lookup tables.
245 enum bq25890_table_ids {
246 /* range tables */
247 TBL_ICHG,
248 TBL_ITERM,
249 TBL_VREG,
250 TBL_BOOSTV,
251 TBL_SYSVMIN,
253 /* lookup tables */
254 TBL_TREG,
255 TBL_BOOSTI,
258 /* Thermal Regulation Threshold lookup table, in degrees Celsius */
259 static const u32 bq25890_treg_tbl[] = { 60, 80, 100, 120 };
261 #define BQ25890_TREG_TBL_SIZE ARRAY_SIZE(bq25890_treg_tbl)
263 /* Boost mode current limit lookup table, in uA */
264 static const u32 bq25890_boosti_tbl[] = {
265 500000, 700000, 1100000, 1300000, 1600000, 1800000, 2100000, 2400000
268 #define BQ25890_BOOSTI_TBL_SIZE ARRAY_SIZE(bq25890_boosti_tbl)
270 struct bq25890_range {
271 u32 min;
272 u32 max;
273 u32 step;
276 struct bq25890_lookup {
277 const u32 *tbl;
278 u32 size;
281 static const union {
282 struct bq25890_range rt;
283 struct bq25890_lookup lt;
284 } bq25890_tables[] = {
285 /* range tables */
286 /* TODO: BQ25896 has max ICHG 3008 mA */
287 [TBL_ICHG] = { .rt = {0, 5056000, 64000} }, /* uA */
288 [TBL_ITERM] = { .rt = {64000, 1024000, 64000} }, /* uA */
289 [TBL_VREG] = { .rt = {3840000, 4608000, 16000} }, /* uV */
290 [TBL_BOOSTV] = { .rt = {4550000, 5510000, 64000} }, /* uV */
291 [TBL_SYSVMIN] = { .rt = {3000000, 3700000, 100000} }, /* uV */
293 /* lookup tables */
294 [TBL_TREG] = { .lt = {bq25890_treg_tbl, BQ25890_TREG_TBL_SIZE} },
295 [TBL_BOOSTI] = { .lt = {bq25890_boosti_tbl, BQ25890_BOOSTI_TBL_SIZE} }
298 static int bq25890_field_read(struct bq25890_device *bq,
299 enum bq25890_fields field_id)
301 int ret;
302 int val;
304 ret = regmap_field_read(bq->rmap_fields[field_id], &val);
305 if (ret < 0)
306 return ret;
308 return val;
311 static int bq25890_field_write(struct bq25890_device *bq,
312 enum bq25890_fields field_id, u8 val)
314 return regmap_field_write(bq->rmap_fields[field_id], val);
317 static u8 bq25890_find_idx(u32 value, enum bq25890_table_ids id)
319 u8 idx;
321 if (id >= TBL_TREG) {
322 const u32 *tbl = bq25890_tables[id].lt.tbl;
323 u32 tbl_size = bq25890_tables[id].lt.size;
325 for (idx = 1; idx < tbl_size && tbl[idx] <= value; idx++)
327 } else {
328 const struct bq25890_range *rtbl = &bq25890_tables[id].rt;
329 u8 rtbl_size;
331 rtbl_size = (rtbl->max - rtbl->min) / rtbl->step + 1;
333 for (idx = 1;
334 idx < rtbl_size && (idx * rtbl->step + rtbl->min <= value);
335 idx++)
339 return idx - 1;
342 static u32 bq25890_find_val(u8 idx, enum bq25890_table_ids id)
344 const struct bq25890_range *rtbl;
346 /* lookup table? */
347 if (id >= TBL_TREG)
348 return bq25890_tables[id].lt.tbl[idx];
350 /* range table */
351 rtbl = &bq25890_tables[id].rt;
353 return (rtbl->min + idx * rtbl->step);
356 enum bq25890_status {
357 STATUS_NOT_CHARGING,
358 STATUS_PRE_CHARGING,
359 STATUS_FAST_CHARGING,
360 STATUS_TERMINATION_DONE,
363 enum bq25890_chrg_fault {
364 CHRG_FAULT_NORMAL,
365 CHRG_FAULT_INPUT,
366 CHRG_FAULT_THERMAL_SHUTDOWN,
367 CHRG_FAULT_TIMER_EXPIRED,
370 static int bq25890_power_supply_get_property(struct power_supply *psy,
371 enum power_supply_property psp,
372 union power_supply_propval *val)
374 int ret;
375 struct bq25890_device *bq = power_supply_get_drvdata(psy);
376 struct bq25890_state state;
378 mutex_lock(&bq->lock);
379 state = bq->state;
380 mutex_unlock(&bq->lock);
382 switch (psp) {
383 case POWER_SUPPLY_PROP_STATUS:
384 if (!state.online)
385 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
386 else if (state.chrg_status == STATUS_NOT_CHARGING)
387 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
388 else if (state.chrg_status == STATUS_PRE_CHARGING ||
389 state.chrg_status == STATUS_FAST_CHARGING)
390 val->intval = POWER_SUPPLY_STATUS_CHARGING;
391 else if (state.chrg_status == STATUS_TERMINATION_DONE)
392 val->intval = POWER_SUPPLY_STATUS_FULL;
393 else
394 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
396 break;
398 case POWER_SUPPLY_PROP_MANUFACTURER:
399 val->strval = BQ25890_MANUFACTURER;
400 break;
402 case POWER_SUPPLY_PROP_MODEL_NAME:
403 if (bq->chip_version == BQ25890)
404 val->strval = "BQ25890";
405 else if (bq->chip_version == BQ25892)
406 val->strval = "BQ25892";
407 else if (bq->chip_version == BQ25895)
408 val->strval = "BQ25895";
409 else if (bq->chip_version == BQ25896)
410 val->strval = "BQ25896";
411 else
412 val->strval = "UNKNOWN";
414 break;
416 case POWER_SUPPLY_PROP_ONLINE:
417 val->intval = state.online;
418 break;
420 case POWER_SUPPLY_PROP_HEALTH:
421 if (!state.chrg_fault && !state.bat_fault && !state.boost_fault)
422 val->intval = POWER_SUPPLY_HEALTH_GOOD;
423 else if (state.bat_fault)
424 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
425 else if (state.chrg_fault == CHRG_FAULT_TIMER_EXPIRED)
426 val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
427 else if (state.chrg_fault == CHRG_FAULT_THERMAL_SHUTDOWN)
428 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
429 else
430 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
431 break;
433 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
434 ret = bq25890_field_read(bq, F_ICHGR); /* read measured value */
435 if (ret < 0)
436 return ret;
438 /* converted_val = ADC_val * 50mA (table 10.3.19) */
439 val->intval = ret * 50000;
440 break;
442 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
443 val->intval = bq25890_find_val(bq->init_data.ichg, TBL_ICHG);
444 break;
446 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
447 if (!state.online) {
448 val->intval = 0;
449 break;
452 ret = bq25890_field_read(bq, F_BATV); /* read measured value */
453 if (ret < 0)
454 return ret;
456 /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
457 val->intval = 2304000 + ret * 20000;
458 break;
460 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
461 val->intval = bq25890_find_val(bq->init_data.vreg, TBL_VREG);
462 break;
464 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
465 val->intval = bq25890_find_val(bq->init_data.iterm, TBL_ITERM);
466 break;
468 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
469 ret = bq25890_field_read(bq, F_SYSV); /* read measured value */
470 if (ret < 0)
471 return ret;
473 /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
474 val->intval = 2304000 + ret * 20000;
475 break;
477 default:
478 return -EINVAL;
481 return 0;
484 static int bq25890_get_chip_state(struct bq25890_device *bq,
485 struct bq25890_state *state)
487 int i, ret;
489 struct {
490 enum bq25890_fields id;
491 u8 *data;
492 } state_fields[] = {
493 {F_CHG_STAT, &state->chrg_status},
494 {F_PG_STAT, &state->online},
495 {F_VSYS_STAT, &state->vsys_status},
496 {F_BOOST_FAULT, &state->boost_fault},
497 {F_BAT_FAULT, &state->bat_fault},
498 {F_CHG_FAULT, &state->chrg_fault}
501 for (i = 0; i < ARRAY_SIZE(state_fields); i++) {
502 ret = bq25890_field_read(bq, state_fields[i].id);
503 if (ret < 0)
504 return ret;
506 *state_fields[i].data = ret;
509 dev_dbg(bq->dev, "S:CHG/PG/VSYS=%d/%d/%d, F:CHG/BOOST/BAT=%d/%d/%d\n",
510 state->chrg_status, state->online, state->vsys_status,
511 state->chrg_fault, state->boost_fault, state->bat_fault);
513 return 0;
516 static bool bq25890_state_changed(struct bq25890_device *bq,
517 struct bq25890_state *new_state)
519 struct bq25890_state old_state;
521 mutex_lock(&bq->lock);
522 old_state = bq->state;
523 mutex_unlock(&bq->lock);
525 return (old_state.chrg_status != new_state->chrg_status ||
526 old_state.chrg_fault != new_state->chrg_fault ||
527 old_state.online != new_state->online ||
528 old_state.bat_fault != new_state->bat_fault ||
529 old_state.boost_fault != new_state->boost_fault ||
530 old_state.vsys_status != new_state->vsys_status);
533 static void bq25890_handle_state_change(struct bq25890_device *bq,
534 struct bq25890_state *new_state)
536 int ret;
537 struct bq25890_state old_state;
539 mutex_lock(&bq->lock);
540 old_state = bq->state;
541 mutex_unlock(&bq->lock);
543 if (!new_state->online) { /* power removed */
544 /* disable ADC */
545 ret = bq25890_field_write(bq, F_CONV_START, 0);
546 if (ret < 0)
547 goto error;
548 } else if (!old_state.online) { /* power inserted */
549 /* enable ADC, to have control of charge current/voltage */
550 ret = bq25890_field_write(bq, F_CONV_START, 1);
551 if (ret < 0)
552 goto error;
555 return;
557 error:
558 dev_err(bq->dev, "Error communicating with the chip.\n");
561 static irqreturn_t bq25890_irq_handler_thread(int irq, void *private)
563 struct bq25890_device *bq = private;
564 int ret;
565 struct bq25890_state state;
567 ret = bq25890_get_chip_state(bq, &state);
568 if (ret < 0)
569 goto handled;
571 if (!bq25890_state_changed(bq, &state))
572 goto handled;
574 bq25890_handle_state_change(bq, &state);
576 mutex_lock(&bq->lock);
577 bq->state = state;
578 mutex_unlock(&bq->lock);
580 power_supply_changed(bq->charger);
582 handled:
583 return IRQ_HANDLED;
586 static int bq25890_chip_reset(struct bq25890_device *bq)
588 int ret;
589 int rst_check_counter = 10;
591 ret = bq25890_field_write(bq, F_REG_RST, 1);
592 if (ret < 0)
593 return ret;
595 do {
596 ret = bq25890_field_read(bq, F_REG_RST);
597 if (ret < 0)
598 return ret;
600 usleep_range(5, 10);
601 } while (ret == 1 && --rst_check_counter);
603 if (!rst_check_counter)
604 return -ETIMEDOUT;
606 return 0;
609 static int bq25890_hw_init(struct bq25890_device *bq)
611 int ret;
612 int i;
613 struct bq25890_state state;
615 const struct {
616 enum bq25890_fields id;
617 u32 value;
618 } init_data[] = {
619 {F_ICHG, bq->init_data.ichg},
620 {F_VREG, bq->init_data.vreg},
621 {F_ITERM, bq->init_data.iterm},
622 {F_IPRECHG, bq->init_data.iprechg},
623 {F_SYSVMIN, bq->init_data.sysvmin},
624 {F_BOOSTV, bq->init_data.boostv},
625 {F_BOOSTI, bq->init_data.boosti},
626 {F_BOOSTF, bq->init_data.boostf},
627 {F_EN_ILIM, bq->init_data.ilim_en},
628 {F_TREG, bq->init_data.treg}
631 ret = bq25890_chip_reset(bq);
632 if (ret < 0) {
633 dev_dbg(bq->dev, "Reset failed %d\n", ret);
634 return ret;
637 /* disable watchdog */
638 ret = bq25890_field_write(bq, F_WD, 0);
639 if (ret < 0) {
640 dev_dbg(bq->dev, "Disabling watchdog failed %d\n", ret);
641 return ret;
644 /* initialize currents/voltages and other parameters */
645 for (i = 0; i < ARRAY_SIZE(init_data); i++) {
646 ret = bq25890_field_write(bq, init_data[i].id,
647 init_data[i].value);
648 if (ret < 0) {
649 dev_dbg(bq->dev, "Writing init data failed %d\n", ret);
650 return ret;
654 /* Configure ADC for continuous conversions. This does not enable it. */
655 ret = bq25890_field_write(bq, F_CONV_RATE, 1);
656 if (ret < 0) {
657 dev_dbg(bq->dev, "Config ADC failed %d\n", ret);
658 return ret;
661 ret = bq25890_get_chip_state(bq, &state);
662 if (ret < 0) {
663 dev_dbg(bq->dev, "Get state failed %d\n", ret);
664 return ret;
667 mutex_lock(&bq->lock);
668 bq->state = state;
669 mutex_unlock(&bq->lock);
671 return 0;
674 static enum power_supply_property bq25890_power_supply_props[] = {
675 POWER_SUPPLY_PROP_MANUFACTURER,
676 POWER_SUPPLY_PROP_MODEL_NAME,
677 POWER_SUPPLY_PROP_STATUS,
678 POWER_SUPPLY_PROP_ONLINE,
679 POWER_SUPPLY_PROP_HEALTH,
680 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
681 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
682 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
683 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
684 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
685 POWER_SUPPLY_PROP_VOLTAGE_NOW,
688 static char *bq25890_charger_supplied_to[] = {
689 "main-battery",
692 static const struct power_supply_desc bq25890_power_supply_desc = {
693 .name = "bq25890-charger",
694 .type = POWER_SUPPLY_TYPE_USB,
695 .properties = bq25890_power_supply_props,
696 .num_properties = ARRAY_SIZE(bq25890_power_supply_props),
697 .get_property = bq25890_power_supply_get_property,
700 static int bq25890_power_supply_init(struct bq25890_device *bq)
702 struct power_supply_config psy_cfg = { .drv_data = bq, };
704 psy_cfg.supplied_to = bq25890_charger_supplied_to;
705 psy_cfg.num_supplicants = ARRAY_SIZE(bq25890_charger_supplied_to);
707 bq->charger = power_supply_register(bq->dev, &bq25890_power_supply_desc,
708 &psy_cfg);
710 return PTR_ERR_OR_ZERO(bq->charger);
713 static void bq25890_usb_work(struct work_struct *data)
715 int ret;
716 struct bq25890_device *bq =
717 container_of(data, struct bq25890_device, usb_work);
719 switch (bq->usb_event) {
720 case USB_EVENT_ID:
721 /* Enable boost mode */
722 ret = bq25890_field_write(bq, F_OTG_CFG, 1);
723 if (ret < 0)
724 goto error;
725 break;
727 case USB_EVENT_NONE:
728 /* Disable boost mode */
729 ret = bq25890_field_write(bq, F_OTG_CFG, 0);
730 if (ret < 0)
731 goto error;
733 power_supply_changed(bq->charger);
734 break;
737 return;
739 error:
740 dev_err(bq->dev, "Error switching to boost/charger mode.\n");
743 static int bq25890_usb_notifier(struct notifier_block *nb, unsigned long val,
744 void *priv)
746 struct bq25890_device *bq =
747 container_of(nb, struct bq25890_device, usb_nb);
749 bq->usb_event = val;
750 queue_work(system_power_efficient_wq, &bq->usb_work);
752 return NOTIFY_OK;
755 static int bq25890_get_chip_version(struct bq25890_device *bq)
757 int id, rev;
759 id = bq25890_field_read(bq, F_PN);
760 if (id < 0) {
761 dev_err(bq->dev, "Cannot read chip ID.\n");
762 return id;
765 rev = bq25890_field_read(bq, F_DEV_REV);
766 if (rev < 0) {
767 dev_err(bq->dev, "Cannot read chip revision.\n");
768 return rev;
771 switch (id) {
772 case BQ25890_ID:
773 bq->chip_version = BQ25890;
774 break;
776 /* BQ25892 and BQ25896 share same ID 0 */
777 case BQ25896_ID:
778 switch (rev) {
779 case 2:
780 bq->chip_version = BQ25896;
781 break;
782 case 1:
783 bq->chip_version = BQ25892;
784 break;
785 default:
786 dev_err(bq->dev,
787 "Unknown device revision %d, assume BQ25892\n",
788 rev);
789 bq->chip_version = BQ25892;
791 break;
793 case BQ25895_ID:
794 bq->chip_version = BQ25895;
795 break;
797 default:
798 dev_err(bq->dev, "Unknown chip ID %d\n", id);
799 return -ENODEV;
802 return 0;
805 static int bq25890_irq_probe(struct bq25890_device *bq)
807 struct gpio_desc *irq;
809 irq = devm_gpiod_get(bq->dev, BQ25890_IRQ_PIN, GPIOD_IN);
810 if (IS_ERR(irq)) {
811 dev_err(bq->dev, "Could not probe irq pin.\n");
812 return PTR_ERR(irq);
815 return gpiod_to_irq(irq);
818 static int bq25890_fw_read_u32_props(struct bq25890_device *bq)
820 int ret;
821 u32 property;
822 int i;
823 struct bq25890_init_data *init = &bq->init_data;
824 struct {
825 char *name;
826 bool optional;
827 enum bq25890_table_ids tbl_id;
828 u8 *conv_data; /* holds converted value from given property */
829 } props[] = {
830 /* required properties */
831 {"ti,charge-current", false, TBL_ICHG, &init->ichg},
832 {"ti,battery-regulation-voltage", false, TBL_VREG, &init->vreg},
833 {"ti,termination-current", false, TBL_ITERM, &init->iterm},
834 {"ti,precharge-current", false, TBL_ITERM, &init->iprechg},
835 {"ti,minimum-sys-voltage", false, TBL_SYSVMIN, &init->sysvmin},
836 {"ti,boost-voltage", false, TBL_BOOSTV, &init->boostv},
837 {"ti,boost-max-current", false, TBL_BOOSTI, &init->boosti},
839 /* optional properties */
840 {"ti,thermal-regulation-threshold", true, TBL_TREG, &init->treg}
843 /* initialize data for optional properties */
844 init->treg = 3; /* 120 degrees Celsius */
846 for (i = 0; i < ARRAY_SIZE(props); i++) {
847 ret = device_property_read_u32(bq->dev, props[i].name,
848 &property);
849 if (ret < 0) {
850 if (props[i].optional)
851 continue;
853 dev_err(bq->dev, "Unable to read property %d %s\n", ret,
854 props[i].name);
856 return ret;
859 *props[i].conv_data = bq25890_find_idx(property,
860 props[i].tbl_id);
863 return 0;
866 static int bq25890_fw_probe(struct bq25890_device *bq)
868 int ret;
869 struct bq25890_init_data *init = &bq->init_data;
871 ret = bq25890_fw_read_u32_props(bq);
872 if (ret < 0)
873 return ret;
875 init->ilim_en = device_property_read_bool(bq->dev, "ti,use-ilim-pin");
876 init->boostf = device_property_read_bool(bq->dev, "ti,boost-low-freq");
878 return 0;
881 static int bq25890_probe(struct i2c_client *client,
882 const struct i2c_device_id *id)
884 struct i2c_adapter *adapter = client->adapter;
885 struct device *dev = &client->dev;
886 struct bq25890_device *bq;
887 int ret;
888 int i;
890 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
891 dev_err(dev, "No support for SMBUS_BYTE_DATA\n");
892 return -ENODEV;
895 bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL);
896 if (!bq)
897 return -ENOMEM;
899 bq->client = client;
900 bq->dev = dev;
902 mutex_init(&bq->lock);
904 bq->rmap = devm_regmap_init_i2c(client, &bq25890_regmap_config);
905 if (IS_ERR(bq->rmap)) {
906 dev_err(dev, "failed to allocate register map\n");
907 return PTR_ERR(bq->rmap);
910 for (i = 0; i < ARRAY_SIZE(bq25890_reg_fields); i++) {
911 const struct reg_field *reg_fields = bq25890_reg_fields;
913 bq->rmap_fields[i] = devm_regmap_field_alloc(dev, bq->rmap,
914 reg_fields[i]);
915 if (IS_ERR(bq->rmap_fields[i])) {
916 dev_err(dev, "cannot allocate regmap field\n");
917 return PTR_ERR(bq->rmap_fields[i]);
921 i2c_set_clientdata(client, bq);
923 ret = bq25890_get_chip_version(bq);
924 if (ret) {
925 dev_err(dev, "Cannot read chip ID or unknown chip.\n");
926 return ret;
929 if (!dev->platform_data) {
930 ret = bq25890_fw_probe(bq);
931 if (ret < 0) {
932 dev_err(dev, "Cannot read device properties.\n");
933 return ret;
935 } else {
936 return -ENODEV;
939 ret = bq25890_hw_init(bq);
940 if (ret < 0) {
941 dev_err(dev, "Cannot initialize the chip.\n");
942 return ret;
945 if (client->irq <= 0)
946 client->irq = bq25890_irq_probe(bq);
948 if (client->irq < 0) {
949 dev_err(dev, "No irq resource found.\n");
950 return client->irq;
953 /* OTG reporting */
954 bq->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
955 if (!IS_ERR_OR_NULL(bq->usb_phy)) {
956 INIT_WORK(&bq->usb_work, bq25890_usb_work);
957 bq->usb_nb.notifier_call = bq25890_usb_notifier;
958 usb_register_notifier(bq->usb_phy, &bq->usb_nb);
961 ret = devm_request_threaded_irq(dev, client->irq, NULL,
962 bq25890_irq_handler_thread,
963 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
964 BQ25890_IRQ_PIN, bq);
965 if (ret)
966 goto irq_fail;
968 ret = bq25890_power_supply_init(bq);
969 if (ret < 0) {
970 dev_err(dev, "Failed to register power supply\n");
971 goto irq_fail;
974 return 0;
976 irq_fail:
977 if (!IS_ERR_OR_NULL(bq->usb_phy))
978 usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
980 return ret;
983 static int bq25890_remove(struct i2c_client *client)
985 struct bq25890_device *bq = i2c_get_clientdata(client);
987 power_supply_unregister(bq->charger);
989 if (!IS_ERR_OR_NULL(bq->usb_phy))
990 usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
992 /* reset all registers to default values */
993 bq25890_chip_reset(bq);
995 return 0;
998 #ifdef CONFIG_PM_SLEEP
999 static int bq25890_suspend(struct device *dev)
1001 struct bq25890_device *bq = dev_get_drvdata(dev);
1004 * If charger is removed, while in suspend, make sure ADC is diabled
1005 * since it consumes slightly more power.
1007 return bq25890_field_write(bq, F_CONV_START, 0);
1010 static int bq25890_resume(struct device *dev)
1012 int ret;
1013 struct bq25890_state state;
1014 struct bq25890_device *bq = dev_get_drvdata(dev);
1016 ret = bq25890_get_chip_state(bq, &state);
1017 if (ret < 0)
1018 return ret;
1020 mutex_lock(&bq->lock);
1021 bq->state = state;
1022 mutex_unlock(&bq->lock);
1024 /* Re-enable ADC only if charger is plugged in. */
1025 if (state.online) {
1026 ret = bq25890_field_write(bq, F_CONV_START, 1);
1027 if (ret < 0)
1028 return ret;
1031 /* signal userspace, maybe state changed while suspended */
1032 power_supply_changed(bq->charger);
1034 return 0;
1036 #endif
1038 static const struct dev_pm_ops bq25890_pm = {
1039 SET_SYSTEM_SLEEP_PM_OPS(bq25890_suspend, bq25890_resume)
1042 static const struct i2c_device_id bq25890_i2c_ids[] = {
1043 { "bq25890", 0 },
1044 { "bq25892", 0 },
1045 { "bq25895", 0 },
1046 { "bq25896", 0 },
1049 MODULE_DEVICE_TABLE(i2c, bq25890_i2c_ids);
1051 static const struct of_device_id bq25890_of_match[] = {
1052 { .compatible = "ti,bq25890", },
1053 { .compatible = "ti,bq25892", },
1054 { .compatible = "ti,bq25895", },
1055 { .compatible = "ti,bq25896", },
1056 { },
1058 MODULE_DEVICE_TABLE(of, bq25890_of_match);
1060 static const struct acpi_device_id bq25890_acpi_match[] = {
1061 {"BQ258900", 0},
1064 MODULE_DEVICE_TABLE(acpi, bq25890_acpi_match);
1066 static struct i2c_driver bq25890_driver = {
1067 .driver = {
1068 .name = "bq25890-charger",
1069 .of_match_table = of_match_ptr(bq25890_of_match),
1070 .acpi_match_table = ACPI_PTR(bq25890_acpi_match),
1071 .pm = &bq25890_pm,
1073 .probe = bq25890_probe,
1074 .remove = bq25890_remove,
1075 .id_table = bq25890_i2c_ids,
1077 module_i2c_driver(bq25890_driver);
1079 MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
1080 MODULE_DESCRIPTION("bq25890 charger driver");
1081 MODULE_LICENSE("GPL");