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