Merge tag 'fbdev-5.17-1' of git://git.kernel.org/pub/scm/linux/kernel/git/deller...
[linux-2.6-microblaze.git] / drivers / power / supply / bq25890_charger.c
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/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
25 #define BQ25895_ID                      7
26 #define BQ25896_ID                      0
27
28 enum bq25890_chip_version {
29         BQ25890,
30         BQ25892,
31         BQ25895,
32         BQ25896,
33 };
34
35 static const char *const bq25890_chip_name[] = {
36         "BQ25890",
37         "BQ25892",
38         "BQ25895",
39         "BQ25896",
40 };
41
42 enum 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 */
47         F_BAT_LOAD_EN, F_WD_RST, F_OTG_CFG, F_CHG_CFG, F_SYSVMIN,
48         F_MIN_VBAT_SEL,                                              /* Reg03 */
49         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 */
57         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 */
60         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 */
75 struct 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 */
86         u8 rbatcomp;    /* IBAT sense resistor value    */
87         u8 vclamp;      /* IBAT compensation voltage limit */
88 };
89
90 struct 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
99 struct 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
112         enum bq25890_chip_version chip_version;
113         struct bq25890_init_data init_data;
114         struct bq25890_state state;
115
116         struct mutex lock; /* protect state data */
117 };
118
119 static const struct regmap_range bq25890_readonly_reg_ranges[] = {
120         regmap_reg_range(0x0b, 0x0c),
121         regmap_reg_range(0x0e, 0x13),
122 };
123
124 static 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
129 static const struct regmap_range bq25890_volatile_reg_ranges[] = {
130         regmap_reg_range(0x00, 0x00),
131         regmap_reg_range(0x02, 0x02),
132         regmap_reg_range(0x09, 0x09),
133         regmap_reg_range(0x0b, 0x14),
134 };
135
136 static 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
141 static 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
152 static 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),
166         [F_HVDCP_EN]            = REG_FIELD(0x02, 3, 3),  // reserved on BQ25896
167         [F_MAXC_EN]             = REG_FIELD(0x02, 2, 2),  // reserved on BQ25896
168         [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),
176         [F_MIN_VBAT_SEL]        = REG_FIELD(0x03, 0, 0), // BQ25896 only
177         /* 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),
193         [F_JEITA_ISET]          = REG_FIELD(0x07, 0, 0), // reserved on BQ25895
194         /* REG08 */
195         [F_BATCMP]              = REG_FIELD(0x08, 5, 7),
196         [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),
202         [F_JEITA_VSET]          = REG_FIELD(0x09, 4, 4), // reserved on BQ25895
203         [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),
209         [F_BOOSTI]              = REG_FIELD(0x0A, 0, 2), // reserved on BQ25895
210         [F_PFM_OTG_DIS]         = REG_FIELD(0x0A, 3, 3), // BQ25896 only
211         /* 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),
215         [F_SDP_STAT]            = REG_FIELD(0x0B, 1, 1), // reserved on BQ25896
216         [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  */
255 enum bq25890_table_ids {
256         /* range tables */
257         TBL_ICHG,
258         TBL_ITERM,
259         TBL_IILIM,
260         TBL_VREG,
261         TBL_BOOSTV,
262         TBL_SYSVMIN,
263         TBL_VBATCOMP,
264         TBL_RBATCOMP,
265
266         /* lookup tables */
267         TBL_TREG,
268         TBL_BOOSTI,
269         TBL_TSPCT,
270 };
271
272 /* Thermal Regulation Threshold lookup table, in degrees Celsius */
273 static const u32 bq25890_treg_tbl[] = { 60, 80, 100, 120 };
274
275 #define BQ25890_TREG_TBL_SIZE           ARRAY_SIZE(bq25890_treg_tbl)
276
277 /* Boost mode current limit lookup table, in uA */
278 static const u32 bq25890_boosti_tbl[] = {
279         500000, 700000, 1100000, 1300000, 1600000, 1800000, 2100000, 2400000
280 };
281
282 #define BQ25890_BOOSTI_TBL_SIZE         ARRAY_SIZE(bq25890_boosti_tbl)
283
284 /* NTC 10K temperature lookup table in tenths of a degree */
285 static const u32 bq25890_tspct_tbl[] = {
286         850, 840, 830, 820, 810, 800, 790, 780,
287         770, 760, 750, 740, 730, 720, 710, 700,
288         690, 685, 680, 675, 670, 660, 650, 645,
289         640, 630, 620, 615, 610, 600, 590, 585,
290         580, 570, 565, 560, 550, 540, 535, 530,
291         520, 515, 510, 500, 495, 490, 480, 475,
292         470, 460, 455, 450, 440, 435, 430, 425,
293         420, 410, 405, 400, 390, 385, 380, 370,
294         365, 360, 355, 350, 340, 335, 330, 320,
295         310, 305, 300, 290, 285, 280, 275, 270,
296         260, 250, 245, 240, 230, 225, 220, 210,
297         205, 200, 190, 180, 175, 170, 160, 150,
298         145, 140, 130, 120, 115, 110, 100, 90,
299         80, 70, 60, 50, 40, 30, 20, 10,
300         0, -10, -20, -30, -40, -60, -70, -80,
301         -90, -10, -120, -140, -150, -170, -190, -210,
302 };
303
304 #define BQ25890_TSPCT_TBL_SIZE          ARRAY_SIZE(bq25890_tspct_tbl)
305
306 struct bq25890_range {
307         u32 min;
308         u32 max;
309         u32 step;
310 };
311
312 struct bq25890_lookup {
313         const u32 *tbl;
314         u32 size;
315 };
316
317 static const union {
318         struct bq25890_range  rt;
319         struct bq25890_lookup lt;
320 } bq25890_tables[] = {
321         /* range tables */
322         /* TODO: BQ25896 has max ICHG 3008 mA */
323         [TBL_ICHG] =    { .rt = {0,       5056000, 64000} },     /* uA */
324         [TBL_ITERM] =   { .rt = {64000,   1024000, 64000} },     /* uA */
325         [TBL_IILIM] =   { .rt = {100000,  3250000, 50000} },     /* uA */
326         [TBL_VREG] =    { .rt = {3840000, 4608000, 16000} },     /* uV */
327         [TBL_BOOSTV] =  { .rt = {4550000, 5510000, 64000} },     /* uV */
328         [TBL_SYSVMIN] = { .rt = {3000000, 3700000, 100000} },    /* uV */
329         [TBL_VBATCOMP] ={ .rt = {0,        224000, 32000} },     /* uV */
330         [TBL_RBATCOMP] ={ .rt = {0,        140000, 20000} },     /* uOhm */
331
332         /* lookup tables */
333         [TBL_TREG] =    { .lt = {bq25890_treg_tbl, BQ25890_TREG_TBL_SIZE} },
334         [TBL_BOOSTI] =  { .lt = {bq25890_boosti_tbl, BQ25890_BOOSTI_TBL_SIZE} },
335         [TBL_TSPCT] =   { .lt = {bq25890_tspct_tbl, BQ25890_TSPCT_TBL_SIZE} }
336 };
337
338 static int bq25890_field_read(struct bq25890_device *bq,
339                               enum bq25890_fields field_id)
340 {
341         int ret;
342         int val;
343
344         ret = regmap_field_read(bq->rmap_fields[field_id], &val);
345         if (ret < 0)
346                 return ret;
347
348         return val;
349 }
350
351 static int bq25890_field_write(struct bq25890_device *bq,
352                                enum bq25890_fields field_id, u8 val)
353 {
354         return regmap_field_write(bq->rmap_fields[field_id], val);
355 }
356
357 static u8 bq25890_find_idx(u32 value, enum bq25890_table_ids id)
358 {
359         u8 idx;
360
361         if (id >= TBL_TREG) {
362                 const u32 *tbl = bq25890_tables[id].lt.tbl;
363                 u32 tbl_size = bq25890_tables[id].lt.size;
364
365                 for (idx = 1; idx < tbl_size && tbl[idx] <= value; idx++)
366                         ;
367         } else {
368                 const struct bq25890_range *rtbl = &bq25890_tables[id].rt;
369                 u8 rtbl_size;
370
371                 rtbl_size = (rtbl->max - rtbl->min) / rtbl->step + 1;
372
373                 for (idx = 1;
374                      idx < rtbl_size && (idx * rtbl->step + rtbl->min <= value);
375                      idx++)
376                         ;
377         }
378
379         return idx - 1;
380 }
381
382 static u32 bq25890_find_val(u8 idx, enum bq25890_table_ids id)
383 {
384         const struct bq25890_range *rtbl;
385
386         /* lookup table? */
387         if (id >= TBL_TREG)
388                 return bq25890_tables[id].lt.tbl[idx];
389
390         /* range table */
391         rtbl = &bq25890_tables[id].rt;
392
393         return (rtbl->min + idx * rtbl->step);
394 }
395
396 enum bq25890_status {
397         STATUS_NOT_CHARGING,
398         STATUS_PRE_CHARGING,
399         STATUS_FAST_CHARGING,
400         STATUS_TERMINATION_DONE,
401 };
402
403 enum bq25890_chrg_fault {
404         CHRG_FAULT_NORMAL,
405         CHRG_FAULT_INPUT,
406         CHRG_FAULT_THERMAL_SHUTDOWN,
407         CHRG_FAULT_TIMER_EXPIRED,
408 };
409
410 static bool bq25890_is_adc_property(enum power_supply_property psp)
411 {
412         switch (psp) {
413         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
414         case POWER_SUPPLY_PROP_CURRENT_NOW:
415         case POWER_SUPPLY_PROP_TEMP:
416                 return true;
417
418         default:
419                 return false;
420         }
421 }
422
423 static irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq);
424
425 static int bq25890_power_supply_get_property(struct power_supply *psy,
426                                              enum power_supply_property psp,
427                                              union power_supply_propval *val)
428 {
429         struct bq25890_device *bq = power_supply_get_drvdata(psy);
430         struct bq25890_state state;
431         bool do_adc_conv;
432         int ret;
433
434         mutex_lock(&bq->lock);
435         /* update state in case we lost an interrupt */
436         __bq25890_handle_irq(bq);
437         state = bq->state;
438         do_adc_conv = !state.online && bq25890_is_adc_property(psp);
439         if (do_adc_conv)
440                 bq25890_field_write(bq, F_CONV_START, 1);
441         mutex_unlock(&bq->lock);
442
443         if (do_adc_conv)
444                 regmap_field_read_poll_timeout(bq->rmap_fields[F_CONV_START],
445                         ret, !ret, 25000, 1000000);
446
447         switch (psp) {
448         case POWER_SUPPLY_PROP_STATUS:
449                 if (!state.online)
450                         val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
451                 else if (state.chrg_status == STATUS_NOT_CHARGING)
452                         val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
453                 else if (state.chrg_status == STATUS_PRE_CHARGING ||
454                          state.chrg_status == STATUS_FAST_CHARGING)
455                         val->intval = POWER_SUPPLY_STATUS_CHARGING;
456                 else if (state.chrg_status == STATUS_TERMINATION_DONE)
457                         val->intval = POWER_SUPPLY_STATUS_FULL;
458                 else
459                         val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
460
461                 break;
462
463         case POWER_SUPPLY_PROP_CHARGE_TYPE:
464                 if (!state.online || state.chrg_status == STATUS_NOT_CHARGING ||
465                     state.chrg_status == STATUS_TERMINATION_DONE)
466                         val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
467                 else if (state.chrg_status == STATUS_PRE_CHARGING)
468                         val->intval = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
469                 else if (state.chrg_status == STATUS_FAST_CHARGING)
470                         val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
471                 else /* unreachable */
472                         val->intval = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
473                 break;
474
475         case POWER_SUPPLY_PROP_MANUFACTURER:
476                 val->strval = BQ25890_MANUFACTURER;
477                 break;
478
479         case POWER_SUPPLY_PROP_MODEL_NAME:
480                 val->strval = bq25890_chip_name[bq->chip_version];
481                 break;
482
483         case POWER_SUPPLY_PROP_ONLINE:
484                 val->intval = state.online;
485                 break;
486
487         case POWER_SUPPLY_PROP_HEALTH:
488                 if (!state.chrg_fault && !state.bat_fault && !state.boost_fault)
489                         val->intval = POWER_SUPPLY_HEALTH_GOOD;
490                 else if (state.bat_fault)
491                         val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
492                 else if (state.chrg_fault == CHRG_FAULT_TIMER_EXPIRED)
493                         val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
494                 else if (state.chrg_fault == CHRG_FAULT_THERMAL_SHUTDOWN)
495                         val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
496                 else
497                         val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
498                 break;
499
500         case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
501                 val->intval = bq25890_find_val(bq->init_data.ichg, TBL_ICHG);
502                 break;
503
504         case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
505                 if (!state.online) {
506                         val->intval = 0;
507                         break;
508                 }
509
510                 ret = bq25890_field_read(bq, F_BATV); /* read measured value */
511                 if (ret < 0)
512                         return ret;
513
514                 /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
515                 val->intval = 2304000 + ret * 20000;
516                 break;
517
518         case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
519                 val->intval = bq25890_find_val(bq->init_data.vreg, TBL_VREG);
520                 break;
521
522         case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
523                 val->intval = bq25890_find_val(bq->init_data.iprechg, TBL_ITERM);
524                 break;
525
526         case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
527                 val->intval = bq25890_find_val(bq->init_data.iterm, TBL_ITERM);
528                 break;
529
530         case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
531                 ret = bq25890_field_read(bq, F_IILIM);
532                 if (ret < 0)
533                         return ret;
534
535                 val->intval = bq25890_find_val(ret, TBL_IILIM);
536                 break;
537
538         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
539                 ret = bq25890_field_read(bq, F_SYSV); /* read measured value */
540                 if (ret < 0)
541                         return ret;
542
543                 /* converted_val = 2.304V + ADC_val * 20mV (table 10.3.15) */
544                 val->intval = 2304000 + ret * 20000;
545                 break;
546
547         case POWER_SUPPLY_PROP_CURRENT_NOW:
548                 ret = bq25890_field_read(bq, F_ICHGR); /* read measured value */
549                 if (ret < 0)
550                         return ret;
551
552                 /* converted_val = ADC_val * 50mA (table 10.3.19) */
553                 val->intval = ret * -50000;
554                 break;
555
556         case POWER_SUPPLY_PROP_TEMP:
557                 ret = bq25890_field_read(bq, F_TSPCT);
558                 if (ret < 0)
559                         return ret;
560
561                 /* convert TS percentage into rough temperature */
562                 val->intval = bq25890_find_val(ret, TBL_TSPCT);
563                 break;
564
565         default:
566                 return -EINVAL;
567         }
568
569         return 0;
570 }
571
572 static int bq25890_get_chip_state(struct bq25890_device *bq,
573                                   struct bq25890_state *state)
574 {
575         int i, ret;
576
577         struct {
578                 enum bq25890_fields id;
579                 u8 *data;
580         } state_fields[] = {
581                 {F_CHG_STAT,    &state->chrg_status},
582                 {F_PG_STAT,     &state->online},
583                 {F_VSYS_STAT,   &state->vsys_status},
584                 {F_BOOST_FAULT, &state->boost_fault},
585                 {F_BAT_FAULT,   &state->bat_fault},
586                 {F_CHG_FAULT,   &state->chrg_fault}
587         };
588
589         for (i = 0; i < ARRAY_SIZE(state_fields); i++) {
590                 ret = bq25890_field_read(bq, state_fields[i].id);
591                 if (ret < 0)
592                         return ret;
593
594                 *state_fields[i].data = ret;
595         }
596
597         dev_dbg(bq->dev, "S:CHG/PG/VSYS=%d/%d/%d, F:CHG/BOOST/BAT=%d/%d/%d\n",
598                 state->chrg_status, state->online, state->vsys_status,
599                 state->chrg_fault, state->boost_fault, state->bat_fault);
600
601         return 0;
602 }
603
604 static irqreturn_t __bq25890_handle_irq(struct bq25890_device *bq)
605 {
606         struct bq25890_state new_state;
607         int ret;
608
609         ret = bq25890_get_chip_state(bq, &new_state);
610         if (ret < 0)
611                 return IRQ_NONE;
612
613         if (!memcmp(&bq->state, &new_state, sizeof(new_state)))
614                 return IRQ_NONE;
615
616         if (!new_state.online && bq->state.online) {        /* power removed */
617                 /* disable ADC */
618                 ret = bq25890_field_write(bq, F_CONV_RATE, 0);
619                 if (ret < 0)
620                         goto error;
621         } else if (new_state.online && !bq->state.online) { /* power inserted */
622                 /* enable ADC, to have control of charge current/voltage */
623                 ret = bq25890_field_write(bq, F_CONV_RATE, 1);
624                 if (ret < 0)
625                         goto error;
626         }
627
628         bq->state = new_state;
629         power_supply_changed(bq->charger);
630
631         return IRQ_HANDLED;
632 error:
633         dev_err(bq->dev, "Error communicating with the chip: %pe\n",
634                 ERR_PTR(ret));
635         return IRQ_HANDLED;
636 }
637
638 static irqreturn_t bq25890_irq_handler_thread(int irq, void *private)
639 {
640         struct bq25890_device *bq = private;
641         irqreturn_t ret;
642
643         mutex_lock(&bq->lock);
644         ret = __bq25890_handle_irq(bq);
645         mutex_unlock(&bq->lock);
646
647         return ret;
648 }
649
650 static int bq25890_chip_reset(struct bq25890_device *bq)
651 {
652         int ret;
653         int rst_check_counter = 10;
654
655         ret = bq25890_field_write(bq, F_REG_RST, 1);
656         if (ret < 0)
657                 return ret;
658
659         do {
660                 ret = bq25890_field_read(bq, F_REG_RST);
661                 if (ret < 0)
662                         return ret;
663
664                 usleep_range(5, 10);
665         } while (ret == 1 && --rst_check_counter);
666
667         if (!rst_check_counter)
668                 return -ETIMEDOUT;
669
670         return 0;
671 }
672
673 static int bq25890_hw_init(struct bq25890_device *bq)
674 {
675         int ret;
676         int i;
677
678         const struct {
679                 enum bq25890_fields id;
680                 u32 value;
681         } init_data[] = {
682                 {F_ICHG,         bq->init_data.ichg},
683                 {F_VREG,         bq->init_data.vreg},
684                 {F_ITERM,        bq->init_data.iterm},
685                 {F_IPRECHG,      bq->init_data.iprechg},
686                 {F_SYSVMIN,      bq->init_data.sysvmin},
687                 {F_BOOSTV,       bq->init_data.boostv},
688                 {F_BOOSTI,       bq->init_data.boosti},
689                 {F_BOOSTF,       bq->init_data.boostf},
690                 {F_EN_ILIM,      bq->init_data.ilim_en},
691                 {F_TREG,         bq->init_data.treg},
692                 {F_BATCMP,       bq->init_data.rbatcomp},
693                 {F_VCLAMP,       bq->init_data.vclamp},
694         };
695
696         ret = bq25890_chip_reset(bq);
697         if (ret < 0) {
698                 dev_dbg(bq->dev, "Reset failed %d\n", ret);
699                 return ret;
700         }
701
702         /* disable watchdog */
703         ret = bq25890_field_write(bq, F_WD, 0);
704         if (ret < 0) {
705                 dev_dbg(bq->dev, "Disabling watchdog failed %d\n", ret);
706                 return ret;
707         }
708
709         /* initialize currents/voltages and other parameters */
710         for (i = 0; i < ARRAY_SIZE(init_data); i++) {
711                 ret = bq25890_field_write(bq, init_data[i].id,
712                                           init_data[i].value);
713                 if (ret < 0) {
714                         dev_dbg(bq->dev, "Writing init data failed %d\n", ret);
715                         return ret;
716                 }
717         }
718
719         ret = bq25890_get_chip_state(bq, &bq->state);
720         if (ret < 0) {
721                 dev_dbg(bq->dev, "Get state failed %d\n", ret);
722                 return ret;
723         }
724
725         /* Configure ADC for continuous conversions when charging */
726         ret = bq25890_field_write(bq, F_CONV_RATE, !!bq->state.online);
727         if (ret < 0) {
728                 dev_dbg(bq->dev, "Config ADC failed %d\n", ret);
729                 return ret;
730         }
731
732         return 0;
733 }
734
735 static const enum power_supply_property bq25890_power_supply_props[] = {
736         POWER_SUPPLY_PROP_MANUFACTURER,
737         POWER_SUPPLY_PROP_MODEL_NAME,
738         POWER_SUPPLY_PROP_STATUS,
739         POWER_SUPPLY_PROP_CHARGE_TYPE,
740         POWER_SUPPLY_PROP_ONLINE,
741         POWER_SUPPLY_PROP_HEALTH,
742         POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
743         POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
744         POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
745         POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
746         POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
747         POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
748         POWER_SUPPLY_PROP_VOLTAGE_NOW,
749         POWER_SUPPLY_PROP_CURRENT_NOW,
750         POWER_SUPPLY_PROP_TEMP,
751 };
752
753 static char *bq25890_charger_supplied_to[] = {
754         "main-battery",
755 };
756
757 static const struct power_supply_desc bq25890_power_supply_desc = {
758         .name = "bq25890-charger",
759         .type = POWER_SUPPLY_TYPE_USB,
760         .properties = bq25890_power_supply_props,
761         .num_properties = ARRAY_SIZE(bq25890_power_supply_props),
762         .get_property = bq25890_power_supply_get_property,
763 };
764
765 static int bq25890_power_supply_init(struct bq25890_device *bq)
766 {
767         struct power_supply_config psy_cfg = { .drv_data = bq, };
768
769         psy_cfg.supplied_to = bq25890_charger_supplied_to;
770         psy_cfg.num_supplicants = ARRAY_SIZE(bq25890_charger_supplied_to);
771
772         bq->charger = devm_power_supply_register(bq->dev,
773                                                  &bq25890_power_supply_desc,
774                                                  &psy_cfg);
775
776         return PTR_ERR_OR_ZERO(bq->charger);
777 }
778
779 static void bq25890_usb_work(struct work_struct *data)
780 {
781         int ret;
782         struct bq25890_device *bq =
783                         container_of(data, struct bq25890_device, usb_work);
784
785         switch (bq->usb_event) {
786         case USB_EVENT_ID:
787                 /* Enable boost mode */
788                 ret = bq25890_field_write(bq, F_OTG_CFG, 1);
789                 if (ret < 0)
790                         goto error;
791                 break;
792
793         case USB_EVENT_NONE:
794                 /* Disable boost mode */
795                 ret = bq25890_field_write(bq, F_OTG_CFG, 0);
796                 if (ret < 0)
797                         goto error;
798
799                 power_supply_changed(bq->charger);
800                 break;
801         }
802
803         return;
804
805 error:
806         dev_err(bq->dev, "Error switching to boost/charger mode.\n");
807 }
808
809 static int bq25890_usb_notifier(struct notifier_block *nb, unsigned long val,
810                                 void *priv)
811 {
812         struct bq25890_device *bq =
813                         container_of(nb, struct bq25890_device, usb_nb);
814
815         bq->usb_event = val;
816         queue_work(system_power_efficient_wq, &bq->usb_work);
817
818         return NOTIFY_OK;
819 }
820
821 static int bq25890_get_chip_version(struct bq25890_device *bq)
822 {
823         int id, rev;
824
825         id = bq25890_field_read(bq, F_PN);
826         if (id < 0) {
827                 dev_err(bq->dev, "Cannot read chip ID: %d\n", id);
828                 return id;
829         }
830
831         rev = bq25890_field_read(bq, F_DEV_REV);
832         if (rev < 0) {
833                 dev_err(bq->dev, "Cannot read chip revision: %d\n", rev);
834                 return rev;
835         }
836
837         switch (id) {
838         case BQ25890_ID:
839                 bq->chip_version = BQ25890;
840                 break;
841
842         /* BQ25892 and BQ25896 share same ID 0 */
843         case BQ25896_ID:
844                 switch (rev) {
845                 case 2:
846                         bq->chip_version = BQ25896;
847                         break;
848                 case 1:
849                         bq->chip_version = BQ25892;
850                         break;
851                 default:
852                         dev_err(bq->dev,
853                                 "Unknown device revision %d, assume BQ25892\n",
854                                 rev);
855                         bq->chip_version = BQ25892;
856                 }
857                 break;
858
859         case BQ25895_ID:
860                 bq->chip_version = BQ25895;
861                 break;
862
863         default:
864                 dev_err(bq->dev, "Unknown chip ID %d\n", id);
865                 return -ENODEV;
866         }
867
868         return 0;
869 }
870
871 static int bq25890_irq_probe(struct bq25890_device *bq)
872 {
873         struct gpio_desc *irq;
874
875         irq = devm_gpiod_get(bq->dev, BQ25890_IRQ_PIN, GPIOD_IN);
876         if (IS_ERR(irq))
877                 return dev_err_probe(bq->dev, PTR_ERR(irq),
878                                      "Could not probe irq pin.\n");
879
880         return gpiod_to_irq(irq);
881 }
882
883 static int bq25890_fw_read_u32_props(struct bq25890_device *bq)
884 {
885         int ret;
886         u32 property;
887         int i;
888         struct bq25890_init_data *init = &bq->init_data;
889         struct {
890                 char *name;
891                 bool optional;
892                 enum bq25890_table_ids tbl_id;
893                 u8 *conv_data; /* holds converted value from given property */
894         } props[] = {
895                 /* required properties */
896                 {"ti,charge-current", false, TBL_ICHG, &init->ichg},
897                 {"ti,battery-regulation-voltage", false, TBL_VREG, &init->vreg},
898                 {"ti,termination-current", false, TBL_ITERM, &init->iterm},
899                 {"ti,precharge-current", false, TBL_ITERM, &init->iprechg},
900                 {"ti,minimum-sys-voltage", false, TBL_SYSVMIN, &init->sysvmin},
901                 {"ti,boost-voltage", false, TBL_BOOSTV, &init->boostv},
902                 {"ti,boost-max-current", false, TBL_BOOSTI, &init->boosti},
903
904                 /* optional properties */
905                 {"ti,thermal-regulation-threshold", true, TBL_TREG, &init->treg},
906                 {"ti,ibatcomp-micro-ohms", true, TBL_RBATCOMP, &init->rbatcomp},
907                 {"ti,ibatcomp-clamp-microvolt", true, TBL_VBATCOMP, &init->vclamp},
908         };
909
910         /* initialize data for optional properties */
911         init->treg = 3; /* 120 degrees Celsius */
912         init->rbatcomp = init->vclamp = 0; /* IBAT compensation disabled */
913
914         for (i = 0; i < ARRAY_SIZE(props); i++) {
915                 ret = device_property_read_u32(bq->dev, props[i].name,
916                                                &property);
917                 if (ret < 0) {
918                         if (props[i].optional)
919                                 continue;
920
921                         dev_err(bq->dev, "Unable to read property %d %s\n", ret,
922                                 props[i].name);
923
924                         return ret;
925                 }
926
927                 *props[i].conv_data = bq25890_find_idx(property,
928                                                        props[i].tbl_id);
929         }
930
931         return 0;
932 }
933
934 static int bq25890_fw_probe(struct bq25890_device *bq)
935 {
936         int ret;
937         struct bq25890_init_data *init = &bq->init_data;
938
939         ret = bq25890_fw_read_u32_props(bq);
940         if (ret < 0)
941                 return ret;
942
943         init->ilim_en = device_property_read_bool(bq->dev, "ti,use-ilim-pin");
944         init->boostf = device_property_read_bool(bq->dev, "ti,boost-low-freq");
945
946         return 0;
947 }
948
949 static int bq25890_probe(struct i2c_client *client,
950                          const struct i2c_device_id *id)
951 {
952         struct device *dev = &client->dev;
953         struct bq25890_device *bq;
954         int ret;
955         int i;
956
957         bq = devm_kzalloc(dev, sizeof(*bq), GFP_KERNEL);
958         if (!bq)
959                 return -ENOMEM;
960
961         bq->client = client;
962         bq->dev = dev;
963
964         mutex_init(&bq->lock);
965
966         bq->rmap = devm_regmap_init_i2c(client, &bq25890_regmap_config);
967         if (IS_ERR(bq->rmap))
968                 return dev_err_probe(dev, PTR_ERR(bq->rmap),
969                                      "failed to allocate register map\n");
970
971         for (i = 0; i < ARRAY_SIZE(bq25890_reg_fields); i++) {
972                 const struct reg_field *reg_fields = bq25890_reg_fields;
973
974                 bq->rmap_fields[i] = devm_regmap_field_alloc(dev, bq->rmap,
975                                                              reg_fields[i]);
976                 if (IS_ERR(bq->rmap_fields[i]))
977                         return dev_err_probe(dev, PTR_ERR(bq->rmap_fields[i]),
978                                              "cannot allocate regmap field\n");
979         }
980
981         i2c_set_clientdata(client, bq);
982
983         ret = bq25890_get_chip_version(bq);
984         if (ret) {
985                 dev_err(dev, "Cannot read chip ID or unknown chip: %d\n", ret);
986                 return ret;
987         }
988
989         if (!dev->platform_data) {
990                 ret = bq25890_fw_probe(bq);
991                 if (ret < 0) {
992                         dev_err(dev, "Cannot read device properties: %d\n",
993                                 ret);
994                         return ret;
995                 }
996         } else {
997                 return -ENODEV;
998         }
999
1000         ret = bq25890_hw_init(bq);
1001         if (ret < 0) {
1002                 dev_err(dev, "Cannot initialize the chip: %d\n", ret);
1003                 return ret;
1004         }
1005
1006         if (client->irq <= 0)
1007                 client->irq = bq25890_irq_probe(bq);
1008
1009         if (client->irq < 0) {
1010                 dev_err(dev, "No irq resource found.\n");
1011                 return client->irq;
1012         }
1013
1014         /* OTG reporting */
1015         bq->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1016         if (!IS_ERR_OR_NULL(bq->usb_phy)) {
1017                 INIT_WORK(&bq->usb_work, bq25890_usb_work);
1018                 bq->usb_nb.notifier_call = bq25890_usb_notifier;
1019                 usb_register_notifier(bq->usb_phy, &bq->usb_nb);
1020         }
1021
1022         ret = bq25890_power_supply_init(bq);
1023         if (ret < 0) {
1024                 dev_err(dev, "Failed to register power supply\n");
1025                 goto err_unregister_usb_notifier;
1026         }
1027
1028         ret = devm_request_threaded_irq(dev, client->irq, NULL,
1029                                         bq25890_irq_handler_thread,
1030                                         IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1031                                         BQ25890_IRQ_PIN, bq);
1032         if (ret)
1033                 goto err_unregister_usb_notifier;
1034
1035         return 0;
1036
1037 err_unregister_usb_notifier:
1038         if (!IS_ERR_OR_NULL(bq->usb_phy))
1039                 usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
1040
1041         return ret;
1042 }
1043
1044 static int bq25890_remove(struct i2c_client *client)
1045 {
1046         struct bq25890_device *bq = i2c_get_clientdata(client);
1047
1048         if (!IS_ERR_OR_NULL(bq->usb_phy))
1049                 usb_unregister_notifier(bq->usb_phy, &bq->usb_nb);
1050
1051         /* reset all registers to default values */
1052         bq25890_chip_reset(bq);
1053
1054         return 0;
1055 }
1056
1057 #ifdef CONFIG_PM_SLEEP
1058 static int bq25890_suspend(struct device *dev)
1059 {
1060         struct bq25890_device *bq = dev_get_drvdata(dev);
1061
1062         /*
1063          * If charger is removed, while in suspend, make sure ADC is diabled
1064          * since it consumes slightly more power.
1065          */
1066         return bq25890_field_write(bq, F_CONV_RATE, 0);
1067 }
1068
1069 static int bq25890_resume(struct device *dev)
1070 {
1071         int ret;
1072         struct bq25890_device *bq = dev_get_drvdata(dev);
1073
1074         mutex_lock(&bq->lock);
1075
1076         ret = bq25890_get_chip_state(bq, &bq->state);
1077         if (ret < 0)
1078                 goto unlock;
1079
1080         /* Re-enable ADC only if charger is plugged in. */
1081         if (bq->state.online) {
1082                 ret = bq25890_field_write(bq, F_CONV_RATE, 1);
1083                 if (ret < 0)
1084                         goto unlock;
1085         }
1086
1087         /* signal userspace, maybe state changed while suspended */
1088         power_supply_changed(bq->charger);
1089
1090 unlock:
1091         mutex_unlock(&bq->lock);
1092
1093         return ret;
1094 }
1095 #endif
1096
1097 static const struct dev_pm_ops bq25890_pm = {
1098         SET_SYSTEM_SLEEP_PM_OPS(bq25890_suspend, bq25890_resume)
1099 };
1100
1101 static const struct i2c_device_id bq25890_i2c_ids[] = {
1102         { "bq25890", 0 },
1103         { "bq25892", 0 },
1104         { "bq25895", 0 },
1105         { "bq25896", 0 },
1106         {},
1107 };
1108 MODULE_DEVICE_TABLE(i2c, bq25890_i2c_ids);
1109
1110 static const struct of_device_id bq25890_of_match[] = {
1111         { .compatible = "ti,bq25890", },
1112         { .compatible = "ti,bq25892", },
1113         { .compatible = "ti,bq25895", },
1114         { .compatible = "ti,bq25896", },
1115         { },
1116 };
1117 MODULE_DEVICE_TABLE(of, bq25890_of_match);
1118
1119 #ifdef CONFIG_ACPI
1120 static const struct acpi_device_id bq25890_acpi_match[] = {
1121         {"BQ258900", 0},
1122         {},
1123 };
1124 MODULE_DEVICE_TABLE(acpi, bq25890_acpi_match);
1125 #endif
1126
1127 static struct i2c_driver bq25890_driver = {
1128         .driver = {
1129                 .name = "bq25890-charger",
1130                 .of_match_table = of_match_ptr(bq25890_of_match),
1131                 .acpi_match_table = ACPI_PTR(bq25890_acpi_match),
1132                 .pm = &bq25890_pm,
1133         },
1134         .probe = bq25890_probe,
1135         .remove = bq25890_remove,
1136         .id_table = bq25890_i2c_ids,
1137 };
1138 module_i2c_driver(bq25890_driver);
1139
1140 MODULE_AUTHOR("Laurentiu Palcu <laurentiu.palcu@intel.com>");
1141 MODULE_DESCRIPTION("bq25890 charger driver");
1142 MODULE_LICENSE("GPL");