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