blob: b7eac5428083801cd4edc67b8140fe4d1e882b78 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +03002/*
3 * TI BQ25890 charger driver
4 *
5 * Copyright (C) 2015 Intel Corporation
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +03006 */
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/types.h>
13#include <linux/gpio/consumer.h>
14#include <linux/interrupt.h>
15#include <linux/delay.h>
16#include <linux/usb/phy.h>
17
18#include <linux/acpi.h>
19#include <linux/of.h>
20
21#define BQ25890_MANUFACTURER "Texas Instruments"
22#define BQ25890_IRQ_PIN "bq25890_irq"
23
24#define BQ25890_ID 3
Angus Ainslie (Purism)5c35ba92019-07-05 05:37:51 -060025#define BQ25895_ID 7
Angus Ainslie (Purism)2e1a2dd2018-07-31 11:49:08 -060026#define BQ25896_ID 0
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +030027
Yauhen Kharuzhyd20267c2020-01-02 01:46:25 +030028enum bq25890_chip_version {
29 BQ25890,
30 BQ25892,
31 BQ25895,
32 BQ25896,
33};
34
Michał Mirosław5956fca2020-05-03 17:21:10 +020035static const char *const bq25890_chip_name[] = {
36 "BQ25890",
37 "BQ25892",
38 "BQ25895",
39 "BQ25896",
40};
41
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +030042enum bq25890_fields {
43 F_EN_HIZ, F_EN_ILIM, F_IILIM, /* Reg00 */
44 F_BHOT, F_BCOLD, F_VINDPM_OFS, /* Reg01 */
45 F_CONV_START, F_CONV_RATE, F_BOOSTF, F_ICO_EN,
46 F_HVDCP_EN, F_MAXC_EN, F_FORCE_DPM, F_AUTO_DPDM_EN, /* Reg02 */
Yauhen Kharuzhyd20267c2020-01-02 01:46:25 +030047 F_BAT_LOAD_EN, F_WD_RST, F_OTG_CFG, F_CHG_CFG, F_SYSVMIN,
48 F_MIN_VBAT_SEL, /* Reg03 */
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +030049 F_PUMPX_EN, F_ICHG, /* Reg04 */
50 F_IPRECHG, F_ITERM, /* Reg05 */
51 F_VREG, F_BATLOWV, F_VRECHG, /* Reg06 */
52 F_TERM_EN, F_STAT_DIS, F_WD, F_TMR_EN, F_CHG_TMR,
53 F_JEITA_ISET, /* Reg07 */
54 F_BATCMP, F_VCLAMP, F_TREG, /* Reg08 */
55 F_FORCE_ICO, F_TMR2X_EN, F_BATFET_DIS, F_JEITA_VSET,
56 F_BATFET_DLY, F_BATFET_RST_EN, F_PUMPX_UP, F_PUMPX_DN, /* Reg09 */
Yauhen Kharuzhyd20267c2020-01-02 01:46:25 +030057 F_BOOSTV, F_PFM_OTG_DIS, F_BOOSTI, /* Reg0A */
58 F_VBUS_STAT, F_CHG_STAT, F_PG_STAT, F_SDP_STAT, F_0B_RSVD,
59 F_VSYS_STAT, /* Reg0B */
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +030060 F_WD_FAULT, F_BOOST_FAULT, F_CHG_FAULT, F_BAT_FAULT,
61 F_NTC_FAULT, /* Reg0C */
62 F_FORCE_VINDPM, F_VINDPM, /* Reg0D */
63 F_THERM_STAT, F_BATV, /* Reg0E */
64 F_SYSV, /* Reg0F */
65 F_TSPCT, /* Reg10 */
66 F_VBUS_GD, F_VBUSV, /* Reg11 */
67 F_ICHGR, /* Reg12 */
68 F_VDPM_STAT, F_IDPM_STAT, F_IDPM_LIM, /* Reg13 */
69 F_REG_RST, F_ICO_OPTIMIZED, F_PN, F_TS_PROFILE, F_DEV_REV, /* Reg14 */
70
71 F_MAX_FIELDS
72};
73
74/* initial field values, converted to register values */
75struct bq25890_init_data {
76 u8 ichg; /* charge current */
77 u8 vreg; /* regulation voltage */
78 u8 iterm; /* termination current */
79 u8 iprechg; /* precharge current */
80 u8 sysvmin; /* minimum system voltage limit */
81 u8 boostv; /* boost regulation voltage */
82 u8 boosti; /* boost current limit */
83 u8 boostf; /* boost frequency */
84 u8 ilim_en; /* enable ILIM pin */
85 u8 treg; /* thermal regulation threshold */
Michał Mirosław72408322020-09-26 21:05:34 +020086 u8 rbatcomp; /* IBAT sense resistor value */
87 u8 vclamp; /* IBAT compensation voltage limit */
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +030088};
89
90struct bq25890_state {
91 u8 online;
92 u8 chrg_status;
93 u8 chrg_fault;
94 u8 vsys_status;
95 u8 boost_fault;
96 u8 bat_fault;
97};
98
99struct bq25890_device {
100 struct i2c_client *client;
101 struct device *dev;
102 struct power_supply *charger;
103
104 struct usb_phy *usb_phy;
105 struct notifier_block usb_nb;
106 struct work_struct usb_work;
107 unsigned long usb_event;
108
109 struct regmap *rmap;
110 struct regmap_field *rmap_fields[F_MAX_FIELDS];
111
Yauhen Kharuzhyd20267c2020-01-02 01:46:25 +0300112 enum bq25890_chip_version chip_version;
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300113 struct bq25890_init_data init_data;
114 struct bq25890_state state;
115
116 struct mutex lock; /* protect state data */
117};
118
119static const struct regmap_range bq25890_readonly_reg_ranges[] = {
120 regmap_reg_range(0x0b, 0x0c),
121 regmap_reg_range(0x0e, 0x13),
122};
123
124static const struct regmap_access_table bq25890_writeable_regs = {
125 .no_ranges = bq25890_readonly_reg_ranges,
126 .n_no_ranges = ARRAY_SIZE(bq25890_readonly_reg_ranges),
127};
128
129static const struct regmap_range bq25890_volatile_reg_ranges[] = {
130 regmap_reg_range(0x00, 0x00),
Michał Mirosław21d90ed2020-05-04 21:47:45 +0200131 regmap_reg_range(0x02, 0x02),
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300132 regmap_reg_range(0x09, 0x09),
Yauhen Kharuzhyd20267c2020-01-02 01:46:25 +0300133 regmap_reg_range(0x0b, 0x14),
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300134};
135
136static const struct regmap_access_table bq25890_volatile_regs = {
137 .yes_ranges = bq25890_volatile_reg_ranges,
138 .n_yes_ranges = ARRAY_SIZE(bq25890_volatile_reg_ranges),
139};
140
141static const struct regmap_config bq25890_regmap_config = {
142 .reg_bits = 8,
143 .val_bits = 8,
144
145 .max_register = 0x14,
146 .cache_type = REGCACHE_RBTREE,
147
148 .wr_table = &bq25890_writeable_regs,
149 .volatile_table = &bq25890_volatile_regs,
150};
151
152static const struct reg_field bq25890_reg_fields[] = {
153 /* REG00 */
154 [F_EN_HIZ] = REG_FIELD(0x00, 7, 7),
155 [F_EN_ILIM] = REG_FIELD(0x00, 6, 6),
156 [F_IILIM] = REG_FIELD(0x00, 0, 5),
157 /* REG01 */
158 [F_BHOT] = REG_FIELD(0x01, 6, 7),
159 [F_BCOLD] = REG_FIELD(0x01, 5, 5),
160 [F_VINDPM_OFS] = REG_FIELD(0x01, 0, 4),
161 /* REG02 */
162 [F_CONV_START] = REG_FIELD(0x02, 7, 7),
163 [F_CONV_RATE] = REG_FIELD(0x02, 6, 6),
164 [F_BOOSTF] = REG_FIELD(0x02, 5, 5),
165 [F_ICO_EN] = REG_FIELD(0x02, 4, 4),
Angus Ainslie (Purism)2e1a2dd2018-07-31 11:49:08 -0600166 [F_HVDCP_EN] = REG_FIELD(0x02, 3, 3), // reserved on BQ25896
167 [F_MAXC_EN] = REG_FIELD(0x02, 2, 2), // reserved on BQ25896
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300168 [F_FORCE_DPM] = REG_FIELD(0x02, 1, 1),
169 [F_AUTO_DPDM_EN] = REG_FIELD(0x02, 0, 0),
170 /* REG03 */
171 [F_BAT_LOAD_EN] = REG_FIELD(0x03, 7, 7),
172 [F_WD_RST] = REG_FIELD(0x03, 6, 6),
173 [F_OTG_CFG] = REG_FIELD(0x03, 5, 5),
174 [F_CHG_CFG] = REG_FIELD(0x03, 4, 4),
175 [F_SYSVMIN] = REG_FIELD(0x03, 1, 3),
Yauhen Kharuzhyd20267c2020-01-02 01:46:25 +0300176 [F_MIN_VBAT_SEL] = REG_FIELD(0x03, 0, 0), // BQ25896 only
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300177 /* REG04 */
178 [F_PUMPX_EN] = REG_FIELD(0x04, 7, 7),
179 [F_ICHG] = REG_FIELD(0x04, 0, 6),
180 /* REG05 */
181 [F_IPRECHG] = REG_FIELD(0x05, 4, 7),
182 [F_ITERM] = REG_FIELD(0x05, 0, 3),
183 /* REG06 */
184 [F_VREG] = REG_FIELD(0x06, 2, 7),
185 [F_BATLOWV] = REG_FIELD(0x06, 1, 1),
186 [F_VRECHG] = REG_FIELD(0x06, 0, 0),
187 /* REG07 */
188 [F_TERM_EN] = REG_FIELD(0x07, 7, 7),
189 [F_STAT_DIS] = REG_FIELD(0x07, 6, 6),
190 [F_WD] = REG_FIELD(0x07, 4, 5),
191 [F_TMR_EN] = REG_FIELD(0x07, 3, 3),
192 [F_CHG_TMR] = REG_FIELD(0x07, 1, 2),
Angus Ainslie (Purism)5c35ba92019-07-05 05:37:51 -0600193 [F_JEITA_ISET] = REG_FIELD(0x07, 0, 0), // reserved on BQ25895
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300194 /* REG08 */
Michał Mirosław95809132018-12-17 20:28:15 +0100195 [F_BATCMP] = REG_FIELD(0x08, 5, 7),
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300196 [F_VCLAMP] = REG_FIELD(0x08, 2, 4),
197 [F_TREG] = REG_FIELD(0x08, 0, 1),
198 /* REG09 */
199 [F_FORCE_ICO] = REG_FIELD(0x09, 7, 7),
200 [F_TMR2X_EN] = REG_FIELD(0x09, 6, 6),
201 [F_BATFET_DIS] = REG_FIELD(0x09, 5, 5),
Angus Ainslie (Purism)5c35ba92019-07-05 05:37:51 -0600202 [F_JEITA_VSET] = REG_FIELD(0x09, 4, 4), // reserved on BQ25895
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300203 [F_BATFET_DLY] = REG_FIELD(0x09, 3, 3),
204 [F_BATFET_RST_EN] = REG_FIELD(0x09, 2, 2),
205 [F_PUMPX_UP] = REG_FIELD(0x09, 1, 1),
206 [F_PUMPX_DN] = REG_FIELD(0x09, 0, 0),
207 /* REG0A */
208 [F_BOOSTV] = REG_FIELD(0x0A, 4, 7),
Angus Ainslie (Purism)5c35ba92019-07-05 05:37:51 -0600209 [F_BOOSTI] = REG_FIELD(0x0A, 0, 2), // reserved on BQ25895
Yauhen Kharuzhyd20267c2020-01-02 01:46:25 +0300210 [F_PFM_OTG_DIS] = REG_FIELD(0x0A, 3, 3), // BQ25896 only
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300211 /* REG0B */
212 [F_VBUS_STAT] = REG_FIELD(0x0B, 5, 7),
213 [F_CHG_STAT] = REG_FIELD(0x0B, 3, 4),
214 [F_PG_STAT] = REG_FIELD(0x0B, 2, 2),
Angus Ainslie (Purism)2e1a2dd2018-07-31 11:49:08 -0600215 [F_SDP_STAT] = REG_FIELD(0x0B, 1, 1), // reserved on BQ25896
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300216 [F_VSYS_STAT] = REG_FIELD(0x0B, 0, 0),
217 /* REG0C */
218 [F_WD_FAULT] = REG_FIELD(0x0C, 7, 7),
219 [F_BOOST_FAULT] = REG_FIELD(0x0C, 6, 6),
220 [F_CHG_FAULT] = REG_FIELD(0x0C, 4, 5),
221 [F_BAT_FAULT] = REG_FIELD(0x0C, 3, 3),
222 [F_NTC_FAULT] = REG_FIELD(0x0C, 0, 2),
223 /* REG0D */
224 [F_FORCE_VINDPM] = REG_FIELD(0x0D, 7, 7),
225 [F_VINDPM] = REG_FIELD(0x0D, 0, 6),
226 /* REG0E */
227 [F_THERM_STAT] = REG_FIELD(0x0E, 7, 7),
228 [F_BATV] = REG_FIELD(0x0E, 0, 6),
229 /* REG0F */
230 [F_SYSV] = REG_FIELD(0x0F, 0, 6),
231 /* REG10 */
232 [F_TSPCT] = REG_FIELD(0x10, 0, 6),
233 /* REG11 */
234 [F_VBUS_GD] = REG_FIELD(0x11, 7, 7),
235 [F_VBUSV] = REG_FIELD(0x11, 0, 6),
236 /* REG12 */
237 [F_ICHGR] = REG_FIELD(0x12, 0, 6),
238 /* REG13 */
239 [F_VDPM_STAT] = REG_FIELD(0x13, 7, 7),
240 [F_IDPM_STAT] = REG_FIELD(0x13, 6, 6),
241 [F_IDPM_LIM] = REG_FIELD(0x13, 0, 5),
242 /* REG14 */
243 [F_REG_RST] = REG_FIELD(0x14, 7, 7),
244 [F_ICO_OPTIMIZED] = REG_FIELD(0x14, 6, 6),
245 [F_PN] = REG_FIELD(0x14, 3, 5),
246 [F_TS_PROFILE] = REG_FIELD(0x14, 2, 2),
247 [F_DEV_REV] = REG_FIELD(0x14, 0, 1)
248};
249
250/*
251 * Most of the val -> idx conversions can be computed, given the minimum,
252 * maximum and the step between values. For the rest of conversions, we use
253 * lookup tables.
254 */
255enum bq25890_table_ids {
256 /* range tables */
257 TBL_ICHG,
258 TBL_ITERM,
Michał Mirosław478efc72020-05-04 21:47:47 +0200259 TBL_IILIM,
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300260 TBL_VREG,
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300261 TBL_BOOSTV,
262 TBL_SYSVMIN,
Michał Mirosław72408322020-09-26 21:05:34 +0200263 TBL_VBATCOMP,
264 TBL_RBATCOMP,
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300265
266 /* lookup tables */
267 TBL_TREG,
268 TBL_BOOSTI,
269};
270
271/* Thermal Regulation Threshold lookup table, in degrees Celsius */
272static const u32 bq25890_treg_tbl[] = { 60, 80, 100, 120 };
273
274#define BQ25890_TREG_TBL_SIZE ARRAY_SIZE(bq25890_treg_tbl)
275
276/* Boost mode current limit lookup table, in uA */
277static const u32 bq25890_boosti_tbl[] = {
278 500000, 700000, 1100000, 1300000, 1600000, 1800000, 2100000, 2400000
279};
280
281#define BQ25890_BOOSTI_TBL_SIZE ARRAY_SIZE(bq25890_boosti_tbl)
282
283struct bq25890_range {
284 u32 min;
285 u32 max;
286 u32 step;
287};
288
289struct bq25890_lookup {
290 const u32 *tbl;
291 u32 size;
292};
293
294static const union {
295 struct bq25890_range rt;
296 struct bq25890_lookup lt;
297} bq25890_tables[] = {
298 /* range tables */
Yauhen Kharuzhyd20267c2020-01-02 01:46:25 +0300299 /* TODO: BQ25896 has max ICHG 3008 mA */
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300300 [TBL_ICHG] = { .rt = {0, 5056000, 64000} }, /* uA */
301 [TBL_ITERM] = { .rt = {64000, 1024000, 64000} }, /* uA */
Sebastian Krzyszkowiakd4e9e7b2020-11-25 04:48:05 +0100302 [TBL_IILIM] = { .rt = {100000, 3250000, 50000} }, /* uA */
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300303 [TBL_VREG] = { .rt = {3840000, 4608000, 16000} }, /* uV */
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300304 [TBL_BOOSTV] = { .rt = {4550000, 5510000, 64000} }, /* uV */
305 [TBL_SYSVMIN] = { .rt = {3000000, 3700000, 100000} }, /* uV */
Michał Mirosław72408322020-09-26 21:05:34 +0200306 [TBL_VBATCOMP] ={ .rt = {0, 224000, 32000} }, /* uV */
307 [TBL_RBATCOMP] ={ .rt = {0, 140000, 20000} }, /* uOhm */
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300308
309 /* lookup tables */
310 [TBL_TREG] = { .lt = {bq25890_treg_tbl, BQ25890_TREG_TBL_SIZE} },
311 [TBL_BOOSTI] = { .lt = {bq25890_boosti_tbl, BQ25890_BOOSTI_TBL_SIZE} }
312};
313
314static int bq25890_field_read(struct bq25890_device *bq,
315 enum bq25890_fields field_id)
316{
317 int ret;
318 int val;
319
320 ret = regmap_field_read(bq->rmap_fields[field_id], &val);
321 if (ret < 0)
322 return ret;
323
324 return val;
325}
326
327static int bq25890_field_write(struct bq25890_device *bq,
328 enum bq25890_fields field_id, u8 val)
329{
330 return regmap_field_write(bq->rmap_fields[field_id], val);
331}
332
333static u8 bq25890_find_idx(u32 value, enum bq25890_table_ids id)
334{
335 u8 idx;
336
337 if (id >= TBL_TREG) {
338 const u32 *tbl = bq25890_tables[id].lt.tbl;
339 u32 tbl_size = bq25890_tables[id].lt.size;
340
341 for (idx = 1; idx < tbl_size && tbl[idx] <= value; idx++)
342 ;
343 } else {
344 const struct bq25890_range *rtbl = &bq25890_tables[id].rt;
345 u8 rtbl_size;
346
347 rtbl_size = (rtbl->max - rtbl->min) / rtbl->step + 1;
348
349 for (idx = 1;
350 idx < rtbl_size && (idx * rtbl->step + rtbl->min <= value);
351 idx++)
352 ;
353 }
354
355 return idx - 1;
356}
357
358static u32 bq25890_find_val(u8 idx, enum bq25890_table_ids id)
359{
360 const struct bq25890_range *rtbl;
361
362 /* lookup table? */
363 if (id >= TBL_TREG)
364 return bq25890_tables[id].lt.tbl[idx];
365
366 /* range table */
367 rtbl = &bq25890_tables[id].rt;
368
369 return (rtbl->min + idx * rtbl->step);
370}
371
372enum bq25890_status {
373 STATUS_NOT_CHARGING,
374 STATUS_PRE_CHARGING,
375 STATUS_FAST_CHARGING,
376 STATUS_TERMINATION_DONE,
377};
378
379enum bq25890_chrg_fault {
380 CHRG_FAULT_NORMAL,
381 CHRG_FAULT_INPUT,
382 CHRG_FAULT_THERMAL_SHUTDOWN,
383 CHRG_FAULT_TIMER_EXPIRED,
384};
385
Michał Mirosław21d90ed2020-05-04 21:47:45 +0200386static bool bq25890_is_adc_property(enum power_supply_property psp)
387{
388 switch (psp) {
389 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
390 case POWER_SUPPLY_PROP_CURRENT_NOW:
391 return true;
392
393 default:
394 return false;
395 }
396}
397
Michał Mirosław3b4df572020-05-04 21:47:45 +0200398static irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq);
399
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300400static int bq25890_power_supply_get_property(struct power_supply *psy,
401 enum power_supply_property psp,
402 union power_supply_propval *val)
403{
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300404 struct bq25890_device *bq = power_supply_get_drvdata(psy);
405 struct bq25890_state state;
Michał Mirosław21d90ed2020-05-04 21:47:45 +0200406 bool do_adc_conv;
407 int ret;
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300408
409 mutex_lock(&bq->lock);
Michał Mirosław3b4df572020-05-04 21:47:45 +0200410 /* update state in case we lost an interrupt */
411 __bq25890_handle_irq(bq);
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300412 state = bq->state;
Michał Mirosław21d90ed2020-05-04 21:47:45 +0200413 do_adc_conv = !state.online && bq25890_is_adc_property(psp);
414 if (do_adc_conv)
415 bq25890_field_write(bq, F_CONV_START, 1);
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300416 mutex_unlock(&bq->lock);
417
Michał Mirosław21d90ed2020-05-04 21:47:45 +0200418 if (do_adc_conv)
419 regmap_field_read_poll_timeout(bq->rmap_fields[F_CONV_START],
420 ret, !ret, 25000, 1000000);
421
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300422 switch (psp) {
423 case POWER_SUPPLY_PROP_STATUS:
424 if (!state.online)
425 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
426 else if (state.chrg_status == STATUS_NOT_CHARGING)
427 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
428 else if (state.chrg_status == STATUS_PRE_CHARGING ||
429 state.chrg_status == STATUS_FAST_CHARGING)
430 val->intval = POWER_SUPPLY_STATUS_CHARGING;
431 else if (state.chrg_status == STATUS_TERMINATION_DONE)
432 val->intval = POWER_SUPPLY_STATUS_FULL;
433 else
434 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
435
436 break;
437
Michał Mirosławb302a0a2020-05-04 21:47:46 +0200438 case POWER_SUPPLY_PROP_CHARGE_TYPE:
439 if (!state.online || state.chrg_status == STATUS_NOT_CHARGING ||
440 state.chrg_status == STATUS_TERMINATION_DONE)
441 val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
442 else if (state.chrg_status == STATUS_PRE_CHARGING)
443 val->intval = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
444 else if (state.chrg_status == STATUS_FAST_CHARGING)
445 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
446 else /* unreachable */
447 val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
448 break;
449
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300450 case POWER_SUPPLY_PROP_MANUFACTURER:
451 val->strval = BQ25890_MANUFACTURER;
452 break;
453
Angus Ainslie (Purism)2e1a2dd2018-07-31 11:49:08 -0600454 case POWER_SUPPLY_PROP_MODEL_NAME:
Michał Mirosław5956fca2020-05-03 17:21:10 +0200455 val->strval = bq25890_chip_name[bq->chip_version];
Angus Ainslie (Purism)2e1a2dd2018-07-31 11:49:08 -0600456 break;
457
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300458 case POWER_SUPPLY_PROP_ONLINE:
459 val->intval = state.online;
460 break;
461
462 case POWER_SUPPLY_PROP_HEALTH:
463 if (!state.chrg_fault && !state.bat_fault && !state.boost_fault)
464 val->intval = POWER_SUPPLY_HEALTH_GOOD;
465 else if (state.bat_fault)
466 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
467 else if (state.chrg_fault == CHRG_FAULT_TIMER_EXPIRED)
468 val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
469 else if (state.chrg_fault == CHRG_FAULT_THERMAL_SHUTDOWN)
470 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
471 else
472 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
473 break;
474
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300475 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
Michał Mirosławf83a6ec2018-12-17 20:28:16 +0100476 val->intval = bq25890_find_val(bq->init_data.ichg, TBL_ICHG);
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300477 break;
478
479 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
480 if (!state.online) {
481 val->intval = 0;
482 break;
483 }
484
485 ret = bq25890_field_read(bq, F_BATV); /* read measured value */
486 if (ret < 0)
487 return ret;
488
489 /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
490 val->intval = 2304000 + ret * 20000;
491 break;
492
493 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
Michał Mirosławf83a6ec2018-12-17 20:28:16 +0100494 val->intval = bq25890_find_val(bq->init_data.vreg, TBL_VREG);
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300495 break;
496
Michał Mirosławc9426562020-05-04 21:47:46 +0200497 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
498 val->intval = bq25890_find_val(bq->init_data.iprechg, TBL_ITERM);
499 break;
500
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300501 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
502 val->intval = bq25890_find_val(bq->init_data.iterm, TBL_ITERM);
503 break;
504
Michał Mirosław478efc72020-05-04 21:47:47 +0200505 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
506 ret = bq25890_field_read(bq, F_IILIM);
507 if (ret < 0)
508 return ret;
509
510 val->intval = bq25890_find_val(ret, TBL_IILIM);
511 break;
512
Angus Ainslie (Purism)ae6fe7a2018-07-31 11:49:09 -0600513 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
514 ret = bq25890_field_read(bq, F_SYSV); /* read measured value */
515 if (ret < 0)
516 return ret;
517
518 /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
519 val->intval = 2304000 + ret * 20000;
520 break;
521
Michał Mirosław1e4724d2020-05-04 21:47:30 +0200522 case POWER_SUPPLY_PROP_CURRENT_NOW:
523 ret = bq25890_field_read(bq, F_ICHGR); /* read measured value */
524 if (ret < 0)
525 return ret;
526
527 /* converted_val = ADC_val * 50mA (table 10.3.19) */
528 val->intval = ret * -50000;
529 break;
530
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300531 default:
532 return -EINVAL;
533 }
534
535 return 0;
536}
537
538static int bq25890_get_chip_state(struct bq25890_device *bq,
539 struct bq25890_state *state)
540{
541 int i, ret;
542
543 struct {
544 enum bq25890_fields id;
545 u8 *data;
546 } state_fields[] = {
547 {F_CHG_STAT, &state->chrg_status},
548 {F_PG_STAT, &state->online},
549 {F_VSYS_STAT, &state->vsys_status},
550 {F_BOOST_FAULT, &state->boost_fault},
551 {F_BAT_FAULT, &state->bat_fault},
552 {F_CHG_FAULT, &state->chrg_fault}
553 };
554
555 for (i = 0; i < ARRAY_SIZE(state_fields); i++) {
556 ret = bq25890_field_read(bq, state_fields[i].id);
557 if (ret < 0)
558 return ret;
559
560 *state_fields[i].data = ret;
561 }
562
563 dev_dbg(bq->dev, "S:CHG/PG/VSYS=%d/%d/%d, F:CHG/BOOST/BAT=%d/%d/%d\n",
564 state->chrg_status, state->online, state->vsys_status,
565 state->chrg_fault, state->boost_fault, state->bat_fault);
566
567 return 0;
568}
569
Michał Mirosław72d9cd92020-05-03 17:21:11 +0200570static irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq)
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300571{
Michał Mirosław72d9cd92020-05-03 17:21:11 +0200572 struct bq25890_state new_state;
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300573 int ret;
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300574
Michał Mirosław72d9cd92020-05-03 17:21:11 +0200575 ret = bq25890_get_chip_state(bq, &new_state);
576 if (ret < 0)
577 return IRQ_NONE;
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300578
Michał Mirosław72d9cd92020-05-03 17:21:11 +0200579 if (!memcmp(&bq->state, &new_state, sizeof(new_state)))
580 return IRQ_NONE;
581
582 if (!new_state.online && bq->state.online) { /* power removed */
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300583 /* disable ADC */
Yauhen Kharuzhy80211be2021-11-07 23:20:01 +0300584 ret = bq25890_field_write(bq, F_CONV_RATE, 0);
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300585 if (ret < 0)
586 goto error;
Michał Mirosław72d9cd92020-05-03 17:21:11 +0200587 } else if (new_state.online && !bq->state.online) { /* power inserted */
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300588 /* enable ADC, to have control of charge current/voltage */
Yauhen Kharuzhy80211be2021-11-07 23:20:01 +0300589 ret = bq25890_field_write(bq, F_CONV_RATE, 1);
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300590 if (ret < 0)
591 goto error;
592 }
593
Michał Mirosław72d9cd92020-05-03 17:21:11 +0200594 bq->state = new_state;
595 power_supply_changed(bq->charger);
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300596
Michał Mirosław72d9cd92020-05-03 17:21:11 +0200597 return IRQ_HANDLED;
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300598error:
Michał Mirosław72d9cd92020-05-03 17:21:11 +0200599 dev_err(bq->dev, "Error communicating with the chip: %pe\n",
600 ERR_PTR(ret));
601 return IRQ_HANDLED;
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300602}
603
604static irqreturn_t bq25890_irq_handler_thread(int irq, void *private)
605{
606 struct bq25890_device *bq = private;
Michał Mirosław72d9cd92020-05-03 17:21:11 +0200607 irqreturn_t ret;
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300608
609 mutex_lock(&bq->lock);
Michał Mirosław72d9cd92020-05-03 17:21:11 +0200610 ret = __bq25890_handle_irq(bq);
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300611 mutex_unlock(&bq->lock);
612
Michał Mirosław72d9cd92020-05-03 17:21:11 +0200613 return ret;
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300614}
615
616static int bq25890_chip_reset(struct bq25890_device *bq)
617{
618 int ret;
619 int rst_check_counter = 10;
620
621 ret = bq25890_field_write(bq, F_REG_RST, 1);
622 if (ret < 0)
623 return ret;
624
625 do {
626 ret = bq25890_field_read(bq, F_REG_RST);
627 if (ret < 0)
628 return ret;
629
630 usleep_range(5, 10);
631 } while (ret == 1 && --rst_check_counter);
632
633 if (!rst_check_counter)
634 return -ETIMEDOUT;
635
636 return 0;
637}
638
639static int bq25890_hw_init(struct bq25890_device *bq)
640{
641 int ret;
642 int i;
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300643
644 const struct {
645 enum bq25890_fields id;
646 u32 value;
647 } init_data[] = {
648 {F_ICHG, bq->init_data.ichg},
649 {F_VREG, bq->init_data.vreg},
650 {F_ITERM, bq->init_data.iterm},
651 {F_IPRECHG, bq->init_data.iprechg},
652 {F_SYSVMIN, bq->init_data.sysvmin},
653 {F_BOOSTV, bq->init_data.boostv},
654 {F_BOOSTI, bq->init_data.boosti},
655 {F_BOOSTF, bq->init_data.boostf},
656 {F_EN_ILIM, bq->init_data.ilim_en},
Michał Mirosław72408322020-09-26 21:05:34 +0200657 {F_TREG, bq->init_data.treg},
658 {F_BATCMP, bq->init_data.rbatcomp},
659 {F_VCLAMP, bq->init_data.vclamp},
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300660 };
661
662 ret = bq25890_chip_reset(bq);
Angus Ainslie (Purism)9d9ae342018-07-31 11:49:06 -0600663 if (ret < 0) {
664 dev_dbg(bq->dev, "Reset failed %d\n", ret);
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300665 return ret;
kbuild test robotad1570d2018-09-17 10:23:20 +0800666 }
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300667
668 /* disable watchdog */
669 ret = bq25890_field_write(bq, F_WD, 0);
Angus Ainslie (Purism)9d9ae342018-07-31 11:49:06 -0600670 if (ret < 0) {
671 dev_dbg(bq->dev, "Disabling watchdog failed %d\n", ret);
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300672 return ret;
kbuild test robotad1570d2018-09-17 10:23:20 +0800673 }
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300674
675 /* initialize currents/voltages and other parameters */
676 for (i = 0; i < ARRAY_SIZE(init_data); i++) {
677 ret = bq25890_field_write(bq, init_data[i].id,
678 init_data[i].value);
Angus Ainslie (Purism)9d9ae342018-07-31 11:49:06 -0600679 if (ret < 0) {
680 dev_dbg(bq->dev, "Writing init data failed %d\n", ret);
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300681 return ret;
kbuild test robotad1570d2018-09-17 10:23:20 +0800682 }
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300683 }
684
Hans de Goede22ad4f92021-10-30 20:28:04 +0200685 ret = bq25890_get_chip_state(bq, &bq->state);
686 if (ret < 0) {
687 dev_dbg(bq->dev, "Get state failed %d\n", ret);
688 return ret;
689 }
690
Michał Mirosław21d90ed2020-05-04 21:47:45 +0200691 /* Configure ADC for continuous conversions when charging */
692 ret = bq25890_field_write(bq, F_CONV_RATE, !!bq->state.online);
Angus Ainslie (Purism)9d9ae342018-07-31 11:49:06 -0600693 if (ret < 0) {
694 dev_dbg(bq->dev, "Config ADC failed %d\n", ret);
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300695 return ret;
kbuild test robotad1570d2018-09-17 10:23:20 +0800696 }
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300697
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300698 return 0;
699}
700
Michał Mirosława6a48fa2020-05-03 17:21:10 +0200701static const enum power_supply_property bq25890_power_supply_props[] = {
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300702 POWER_SUPPLY_PROP_MANUFACTURER,
Angus Ainslie (Purism)2e1a2dd2018-07-31 11:49:08 -0600703 POWER_SUPPLY_PROP_MODEL_NAME,
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300704 POWER_SUPPLY_PROP_STATUS,
Michał Mirosławb302a0a2020-05-04 21:47:46 +0200705 POWER_SUPPLY_PROP_CHARGE_TYPE,
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300706 POWER_SUPPLY_PROP_ONLINE,
707 POWER_SUPPLY_PROP_HEALTH,
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300708 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
709 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
710 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
Michał Mirosławc9426562020-05-04 21:47:46 +0200711 POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300712 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
Michał Mirosław478efc72020-05-04 21:47:47 +0200713 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
Angus Ainslie (Purism)ae6fe7a2018-07-31 11:49:09 -0600714 POWER_SUPPLY_PROP_VOLTAGE_NOW,
Michał Mirosław1e4724d2020-05-04 21:47:30 +0200715 POWER_SUPPLY_PROP_CURRENT_NOW,
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300716};
717
718static char *bq25890_charger_supplied_to[] = {
719 "main-battery",
720};
721
722static const struct power_supply_desc bq25890_power_supply_desc = {
723 .name = "bq25890-charger",
724 .type = POWER_SUPPLY_TYPE_USB,
725 .properties = bq25890_power_supply_props,
726 .num_properties = ARRAY_SIZE(bq25890_power_supply_props),
727 .get_property = bq25890_power_supply_get_property,
728};
729
730static int bq25890_power_supply_init(struct bq25890_device *bq)
731{
732 struct power_supply_config psy_cfg = { .drv_data = bq, };
733
734 psy_cfg.supplied_to = bq25890_charger_supplied_to;
735 psy_cfg.num_supplicants = ARRAY_SIZE(bq25890_charger_supplied_to);
736
Hans de Goeded01363da2021-10-30 20:28:03 +0200737 bq->charger = devm_power_supply_register(bq->dev,
738 &bq25890_power_supply_desc,
739 &psy_cfg);
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300740
741 return PTR_ERR_OR_ZERO(bq->charger);
742}
743
744static void bq25890_usb_work(struct work_struct *data)
745{
746 int ret;
747 struct bq25890_device *bq =
748 container_of(data, struct bq25890_device, usb_work);
749
750 switch (bq->usb_event) {
751 case USB_EVENT_ID:
752 /* Enable boost mode */
753 ret = bq25890_field_write(bq, F_OTG_CFG, 1);
754 if (ret < 0)
755 goto error;
756 break;
757
758 case USB_EVENT_NONE:
759 /* Disable boost mode */
760 ret = bq25890_field_write(bq, F_OTG_CFG, 0);
761 if (ret < 0)
762 goto error;
763
764 power_supply_changed(bq->charger);
765 break;
766 }
767
768 return;
769
770error:
771 dev_err(bq->dev, "Error switching to boost/charger mode.\n");
772}
773
774static int bq25890_usb_notifier(struct notifier_block *nb, unsigned long val,
775 void *priv)
776{
777 struct bq25890_device *bq =
778 container_of(nb, struct bq25890_device, usb_nb);
779
780 bq->usb_event = val;
781 queue_work(system_power_efficient_wq, &bq->usb_work);
782
783 return NOTIFY_OK;
784}
785
Yauhen Kharuzhyd20267c2020-01-02 01:46:25 +0300786static int bq25890_get_chip_version(struct bq25890_device *bq)
787{
788 int id, rev;
789
790 id = bq25890_field_read(bq, F_PN);
791 if (id < 0) {
Martin Kepplinger172d0cc2021-10-14 11:45:33 +0200792 dev_err(bq->dev, "Cannot read chip ID: %d\n", id);
Yauhen Kharuzhyd20267c2020-01-02 01:46:25 +0300793 return id;
794 }
795
796 rev = bq25890_field_read(bq, F_DEV_REV);
797 if (rev < 0) {
Martin Kepplinger172d0cc2021-10-14 11:45:33 +0200798 dev_err(bq->dev, "Cannot read chip revision: %d\n", rev);
Colin Ian Kingcb619e82020-01-16 17:09:00 +0000799 return rev;
Yauhen Kharuzhyd20267c2020-01-02 01:46:25 +0300800 }
801
802 switch (id) {
803 case BQ25890_ID:
804 bq->chip_version = BQ25890;
805 break;
806
807 /* BQ25892 and BQ25896 share same ID 0 */
808 case BQ25896_ID:
809 switch (rev) {
810 case 2:
811 bq->chip_version = BQ25896;
812 break;
813 case 1:
814 bq->chip_version = BQ25892;
815 break;
816 default:
817 dev_err(bq->dev,
818 "Unknown device revision %d, assume BQ25892\n",
819 rev);
820 bq->chip_version = BQ25892;
821 }
822 break;
823
824 case BQ25895_ID:
825 bq->chip_version = BQ25895;
826 break;
827
828 default:
829 dev_err(bq->dev, "Unknown chip ID %d\n", id);
830 return -ENODEV;
831 }
832
833 return 0;
834}
835
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300836static int bq25890_irq_probe(struct bq25890_device *bq)
837{
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300838 struct gpio_desc *irq;
839
Andy Shevchenko86775872017-02-20 19:12:23 +0200840 irq = devm_gpiod_get(bq->dev, BQ25890_IRQ_PIN, GPIOD_IN);
Martin Kepplinger172d0cc2021-10-14 11:45:33 +0200841 if (IS_ERR(irq))
842 return dev_err_probe(bq->dev, PTR_ERR(irq),
843 "Could not probe irq pin.\n");
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300844
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300845 return gpiod_to_irq(irq);
846}
847
848static int bq25890_fw_read_u32_props(struct bq25890_device *bq)
849{
850 int ret;
851 u32 property;
852 int i;
853 struct bq25890_init_data *init = &bq->init_data;
854 struct {
855 char *name;
856 bool optional;
857 enum bq25890_table_ids tbl_id;
858 u8 *conv_data; /* holds converted value from given property */
859 } props[] = {
860 /* required properties */
861 {"ti,charge-current", false, TBL_ICHG, &init->ichg},
862 {"ti,battery-regulation-voltage", false, TBL_VREG, &init->vreg},
863 {"ti,termination-current", false, TBL_ITERM, &init->iterm},
864 {"ti,precharge-current", false, TBL_ITERM, &init->iprechg},
865 {"ti,minimum-sys-voltage", false, TBL_SYSVMIN, &init->sysvmin},
866 {"ti,boost-voltage", false, TBL_BOOSTV, &init->boostv},
867 {"ti,boost-max-current", false, TBL_BOOSTI, &init->boosti},
868
869 /* optional properties */
Michał Mirosław72408322020-09-26 21:05:34 +0200870 {"ti,thermal-regulation-threshold", true, TBL_TREG, &init->treg},
871 {"ti,ibatcomp-micro-ohms", true, TBL_RBATCOMP, &init->rbatcomp},
872 {"ti,ibatcomp-clamp-microvolt", true, TBL_VBATCOMP, &init->vclamp},
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300873 };
874
875 /* initialize data for optional properties */
876 init->treg = 3; /* 120 degrees Celsius */
Michał Mirosław72408322020-09-26 21:05:34 +0200877 init->rbatcomp = init->vclamp = 0; /* IBAT compensation disabled */
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300878
879 for (i = 0; i < ARRAY_SIZE(props); i++) {
880 ret = device_property_read_u32(bq->dev, props[i].name,
881 &property);
882 if (ret < 0) {
883 if (props[i].optional)
884 continue;
885
Angus Ainslie (Purism)9d9ae342018-07-31 11:49:06 -0600886 dev_err(bq->dev, "Unable to read property %d %s\n", ret,
887 props[i].name);
888
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300889 return ret;
890 }
891
892 *props[i].conv_data = bq25890_find_idx(property,
893 props[i].tbl_id);
894 }
895
896 return 0;
897}
898
899static int bq25890_fw_probe(struct bq25890_device *bq)
900{
901 int ret;
902 struct bq25890_init_data *init = &bq->init_data;
903
904 ret = bq25890_fw_read_u32_props(bq);
905 if (ret < 0)
906 return ret;
907
908 init->ilim_en = device_property_read_bool(bq->dev, "ti,use-ilim-pin");
909 init->boostf = device_property_read_bool(bq->dev, "ti,boost-low-freq");
910
911 return 0;
912}
913
914static int bq25890_probe(struct i2c_client *client,
915 const struct i2c_device_id *id)
916{
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300917 struct device *dev = &client->dev;
918 struct bq25890_device *bq;
919 int ret;
920 int i;
921
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300922 bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL);
923 if (!bq)
924 return -ENOMEM;
925
926 bq->client = client;
927 bq->dev = dev;
928
929 mutex_init(&bq->lock);
930
931 bq->rmap = devm_regmap_init_i2c(client, &bq25890_regmap_config);
Martin Kepplinger172d0cc2021-10-14 11:45:33 +0200932 if (IS_ERR(bq->rmap))
933 return dev_err_probe(dev, PTR_ERR(bq->rmap),
934 "failed to allocate register map\n");
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300935
936 for (i = 0; i < ARRAY_SIZE(bq25890_reg_fields); i++) {
937 const struct reg_field *reg_fields = bq25890_reg_fields;
938
939 bq->rmap_fields[i] = devm_regmap_field_alloc(dev, bq->rmap,
940 reg_fields[i]);
Martin Kepplinger172d0cc2021-10-14 11:45:33 +0200941 if (IS_ERR(bq->rmap_fields[i]))
942 return dev_err_probe(dev, PTR_ERR(bq->rmap_fields[i]),
943 "cannot allocate regmap field\n");
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300944 }
945
946 i2c_set_clientdata(client, bq);
947
Yauhen Kharuzhyd20267c2020-01-02 01:46:25 +0300948 ret = bq25890_get_chip_version(bq);
949 if (ret) {
Martin Kepplinger172d0cc2021-10-14 11:45:33 +0200950 dev_err(dev, "Cannot read chip ID or unknown chip: %d\n", ret);
Yauhen Kharuzhyd20267c2020-01-02 01:46:25 +0300951 return ret;
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300952 }
953
954 if (!dev->platform_data) {
955 ret = bq25890_fw_probe(bq);
956 if (ret < 0) {
Martin Kepplinger172d0cc2021-10-14 11:45:33 +0200957 dev_err(dev, "Cannot read device properties: %d\n",
958 ret);
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300959 return ret;
960 }
961 } else {
962 return -ENODEV;
963 }
964
965 ret = bq25890_hw_init(bq);
966 if (ret < 0) {
Martin Kepplinger172d0cc2021-10-14 11:45:33 +0200967 dev_err(dev, "Cannot initialize the chip: %d\n", ret);
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300968 return ret;
969 }
970
971 if (client->irq <= 0)
972 client->irq = bq25890_irq_probe(bq);
973
974 if (client->irq < 0) {
975 dev_err(dev, "No irq resource found.\n");
976 return client->irq;
977 }
978
979 /* OTG reporting */
980 bq->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
981 if (!IS_ERR_OR_NULL(bq->usb_phy)) {
982 INIT_WORK(&bq->usb_work, bq25890_usb_work);
983 bq->usb_nb.notifier_call = bq25890_usb_notifier;
984 usb_register_notifier(bq->usb_phy, &bq->usb_nb);
985 }
986
Hans de Goeded01363da2021-10-30 20:28:03 +0200987 ret = bq25890_power_supply_init(bq);
988 if (ret < 0) {
989 dev_err(dev, "Failed to register power supply\n");
990 goto err_unregister_usb_notifier;
991 }
992
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300993 ret = devm_request_threaded_irq(dev, client->irq, NULL,
994 bq25890_irq_handler_thread,
995 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
996 BQ25890_IRQ_PIN, bq);
997 if (ret)
Hans de Goeded01363da2021-10-30 20:28:03 +0200998 goto err_unregister_usb_notifier;
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +0300999
1000 return 0;
1001
Hans de Goeded01363da2021-10-30 20:28:03 +02001002err_unregister_usb_notifier:
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +03001003 if (!IS_ERR_OR_NULL(bq->usb_phy))
1004 usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
1005
1006 return ret;
1007}
1008
1009static int bq25890_remove(struct i2c_client *client)
1010{
1011 struct bq25890_device *bq = i2c_get_clientdata(client);
1012
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +03001013 if (!IS_ERR_OR_NULL(bq->usb_phy))
1014 usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
1015
1016 /* reset all registers to default values */
1017 bq25890_chip_reset(bq);
1018
1019 return 0;
1020}
1021
1022#ifdef CONFIG_PM_SLEEP
1023static int bq25890_suspend(struct device *dev)
1024{
1025 struct bq25890_device *bq = dev_get_drvdata(dev);
1026
1027 /*
1028 * If charger is removed, while in suspend, make sure ADC is diabled
1029 * since it consumes slightly more power.
1030 */
Michał Mirosław21d90ed2020-05-04 21:47:45 +02001031 return bq25890_field_write(bq, F_CONV_RATE, 0);
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +03001032}
1033
1034static int bq25890_resume(struct device *dev)
1035{
1036 int ret;
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +03001037 struct bq25890_device *bq = dev_get_drvdata(dev);
1038
Michał Mirosław72d9cd92020-05-03 17:21:11 +02001039 mutex_lock(&bq->lock);
1040
1041 ret = bq25890_get_chip_state(bq, &bq->state);
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +03001042 if (ret < 0)
Dan Carpentercf5701b2020-05-06 13:11:16 +03001043 goto unlock;
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +03001044
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +03001045 /* Re-enable ADC only if charger is plugged in. */
Michał Mirosław72d9cd92020-05-03 17:21:11 +02001046 if (bq->state.online) {
Michał Mirosław21d90ed2020-05-04 21:47:45 +02001047 ret = bq25890_field_write(bq, F_CONV_RATE, 1);
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +03001048 if (ret < 0)
Dan Carpentercf5701b2020-05-06 13:11:16 +03001049 goto unlock;
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +03001050 }
1051
1052 /* signal userspace, maybe state changed while suspended */
1053 power_supply_changed(bq->charger);
1054
Dan Carpentercf5701b2020-05-06 13:11:16 +03001055unlock:
Michał Mirosław72d9cd92020-05-03 17:21:11 +02001056 mutex_unlock(&bq->lock);
1057
Dan Carpentercf5701b2020-05-06 13:11:16 +03001058 return ret;
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +03001059}
1060#endif
1061
1062static const struct dev_pm_ops bq25890_pm = {
1063 SET_SYSTEM_SLEEP_PM_OPS(bq25890_suspend, bq25890_resume)
1064};
1065
1066static const struct i2c_device_id bq25890_i2c_ids[] = {
1067 { "bq25890", 0 },
Yauhen Kharuzhy46aa27e2020-01-02 01:46:26 +03001068 { "bq25892", 0 },
1069 { "bq25895", 0 },
1070 { "bq25896", 0 },
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +03001071 {},
1072};
1073MODULE_DEVICE_TABLE(i2c, bq25890_i2c_ids);
1074
1075static const struct of_device_id bq25890_of_match[] = {
1076 { .compatible = "ti,bq25890", },
Yauhen Kharuzhy46aa27e2020-01-02 01:46:26 +03001077 { .compatible = "ti,bq25892", },
1078 { .compatible = "ti,bq25895", },
1079 { .compatible = "ti,bq25896", },
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +03001080 { },
1081};
1082MODULE_DEVICE_TABLE(of, bq25890_of_match);
1083
Krzysztof Kozlowski02067dc2020-09-11 18:27:25 +02001084#ifdef CONFIG_ACPI
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +03001085static const struct acpi_device_id bq25890_acpi_match[] = {
1086 {"BQ258900", 0},
1087 {},
1088};
1089MODULE_DEVICE_TABLE(acpi, bq25890_acpi_match);
Krzysztof Kozlowski02067dc2020-09-11 18:27:25 +02001090#endif
Laurentiu Palcu4aeae9c2015-05-19 16:24:40 +03001091
1092static struct i2c_driver bq25890_driver = {
1093 .driver = {
1094 .name = "bq25890-charger",
1095 .of_match_table = of_match_ptr(bq25890_of_match),
1096 .acpi_match_table = ACPI_PTR(bq25890_acpi_match),
1097 .pm = &bq25890_pm,
1098 },
1099 .probe = bq25890_probe,
1100 .remove = bq25890_remove,
1101 .id_table = bq25890_i2c_ids,
1102};
1103module_i2c_driver(bq25890_driver);
1104
1105MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
1106MODULE_DESCRIPTION("bq25890 charger driver");
1107MODULE_LICENSE("GPL");