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