GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / power / supply / axp288_charger.c
1 /*
2  * axp288_charger.c - X-power AXP288 PMIC Charger driver
3  *
4  * Copyright (C) 2016-2017 Hans de Goede <hdegoede@redhat.com>
5  * Copyright (C) 2014 Intel Corporation
6  * Author: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17
18 #include <linux/acpi.h>
19 #include <linux/module.h>
20 #include <linux/device.h>
21 #include <linux/regmap.h>
22 #include <linux/workqueue.h>
23 #include <linux/delay.h>
24 #include <linux/platform_device.h>
25 #include <linux/usb/otg.h>
26 #include <linux/notifier.h>
27 #include <linux/power_supply.h>
28 #include <linux/property.h>
29 #include <linux/mfd/axp20x.h>
30 #include <linux/extcon.h>
31 #include <linux/dmi.h>
32
33 #define PS_STAT_VBUS_TRIGGER            (1 << 0)
34 #define PS_STAT_BAT_CHRG_DIR            (1 << 2)
35 #define PS_STAT_VBAT_ABOVE_VHOLD        (1 << 3)
36 #define PS_STAT_VBUS_VALID              (1 << 4)
37 #define PS_STAT_VBUS_PRESENT            (1 << 5)
38
39 #define CHRG_STAT_BAT_SAFE_MODE         (1 << 3)
40 #define CHRG_STAT_BAT_VALID             (1 << 4)
41 #define CHRG_STAT_BAT_PRESENT           (1 << 5)
42 #define CHRG_STAT_CHARGING              (1 << 6)
43 #define CHRG_STAT_PMIC_OTP              (1 << 7)
44
45 #define VBUS_ISPOUT_CUR_LIM_MASK        0x03
46 #define VBUS_ISPOUT_CUR_LIM_BIT_POS     0
47 #define VBUS_ISPOUT_CUR_LIM_900MA       0x0     /* 900mA */
48 #define VBUS_ISPOUT_CUR_LIM_1500MA      0x1     /* 1500mA */
49 #define VBUS_ISPOUT_CUR_LIM_2000MA      0x2     /* 2000mA */
50 #define VBUS_ISPOUT_CUR_NO_LIM          0x3     /* 2500mA */
51 #define VBUS_ISPOUT_VHOLD_SET_MASK      0x31
52 #define VBUS_ISPOUT_VHOLD_SET_BIT_POS   0x3
53 #define VBUS_ISPOUT_VHOLD_SET_OFFSET    4000    /* 4000mV */
54 #define VBUS_ISPOUT_VHOLD_SET_LSB_RES   100     /* 100mV */
55 #define VBUS_ISPOUT_VHOLD_SET_4300MV    0x3     /* 4300mV */
56 #define VBUS_ISPOUT_VBUS_PATH_DIS       (1 << 7)
57
58 #define CHRG_CCCV_CC_MASK               0xf             /* 4 bits */
59 #define CHRG_CCCV_CC_BIT_POS            0
60 #define CHRG_CCCV_CC_OFFSET             200             /* 200mA */
61 #define CHRG_CCCV_CC_LSB_RES            200             /* 200mA */
62 #define CHRG_CCCV_ITERM_20P             (1 << 4)        /* 20% of CC */
63 #define CHRG_CCCV_CV_MASK               0x60            /* 2 bits */
64 #define CHRG_CCCV_CV_BIT_POS            5
65 #define CHRG_CCCV_CV_4100MV             0x0             /* 4.10V */
66 #define CHRG_CCCV_CV_4150MV             0x1             /* 4.15V */
67 #define CHRG_CCCV_CV_4200MV             0x2             /* 4.20V */
68 #define CHRG_CCCV_CV_4350MV             0x3             /* 4.35V */
69 #define CHRG_CCCV_CHG_EN                (1 << 7)
70
71 #define CNTL2_CC_TIMEOUT_MASK           0x3     /* 2 bits */
72 #define CNTL2_CC_TIMEOUT_OFFSET         6       /* 6 Hrs */
73 #define CNTL2_CC_TIMEOUT_LSB_RES        2       /* 2 Hrs */
74 #define CNTL2_CC_TIMEOUT_12HRS          0x3     /* 12 Hrs */
75 #define CNTL2_CHGLED_TYPEB              (1 << 4)
76 #define CNTL2_CHG_OUT_TURNON            (1 << 5)
77 #define CNTL2_PC_TIMEOUT_MASK           0xC0
78 #define CNTL2_PC_TIMEOUT_OFFSET         40      /* 40 mins */
79 #define CNTL2_PC_TIMEOUT_LSB_RES        10      /* 10 mins */
80 #define CNTL2_PC_TIMEOUT_70MINS         0x3
81
82 #define CHRG_ILIM_TEMP_LOOP_EN          (1 << 3)
83 #define CHRG_VBUS_ILIM_MASK             0xf0
84 #define CHRG_VBUS_ILIM_BIT_POS          4
85 #define CHRG_VBUS_ILIM_100MA            0x0     /* 100mA */
86 #define CHRG_VBUS_ILIM_500MA            0x1     /* 500mA */
87 #define CHRG_VBUS_ILIM_900MA            0x2     /* 900mA */
88 #define CHRG_VBUS_ILIM_1500MA           0x3     /* 1500mA */
89 #define CHRG_VBUS_ILIM_2000MA           0x4     /* 2000mA */
90 #define CHRG_VBUS_ILIM_2500MA           0x5     /* 2500mA */
91 #define CHRG_VBUS_ILIM_3000MA           0x6     /* 3000mA */
92 #define CHRG_VBUS_ILIM_3500MA           0x7     /* 3500mA */
93 #define CHRG_VBUS_ILIM_4000MA           0x8     /* 4000mA */
94
95 #define CHRG_VLTFC_0C                   0xA5    /* 0 DegC */
96 #define CHRG_VHTFC_45C                  0x1F    /* 45 DegC */
97
98 #define FG_CNTL_OCV_ADJ_EN              (1 << 3)
99
100 #define CV_4100MV                       4100    /* 4100mV */
101 #define CV_4150MV                       4150    /* 4150mV */
102 #define CV_4200MV                       4200    /* 4200mV */
103 #define CV_4350MV                       4350    /* 4350mV */
104
105 #define AXP288_EXTCON_DEV_NAME          "axp288_extcon"
106 #define USB_HOST_EXTCON_HID             "INT3496"
107 #define USB_HOST_EXTCON_NAME            "INT3496:00"
108
109 enum {
110         VBUS_OV_IRQ = 0,
111         CHARGE_DONE_IRQ,
112         CHARGE_CHARGING_IRQ,
113         BAT_SAFE_QUIT_IRQ,
114         BAT_SAFE_ENTER_IRQ,
115         QCBTU_IRQ,
116         CBTU_IRQ,
117         QCBTO_IRQ,
118         CBTO_IRQ,
119         CHRG_INTR_END,
120 };
121
122 struct axp288_chrg_info {
123         struct platform_device *pdev;
124         struct regmap *regmap;
125         struct regmap_irq_chip_data *regmap_irqc;
126         int irq[CHRG_INTR_END];
127         struct power_supply *psy_usb;
128
129         /* OTG/Host mode */
130         struct {
131                 struct work_struct work;
132                 struct extcon_dev *cable;
133                 struct notifier_block id_nb;
134                 bool id_short;
135         } otg;
136
137         /* SDP/CDP/DCP USB charging cable notifications */
138         struct {
139                 struct extcon_dev *edev;
140                 struct notifier_block nb;
141                 struct work_struct work;
142         } cable;
143
144         int cc;
145         int cv;
146         int max_cc;
147         int max_cv;
148 };
149
150 static inline int axp288_charger_set_cc(struct axp288_chrg_info *info, int cc)
151 {
152         u8 reg_val;
153         int ret;
154
155         if (cc < CHRG_CCCV_CC_OFFSET)
156                 cc = CHRG_CCCV_CC_OFFSET;
157         else if (cc > info->max_cc)
158                 cc = info->max_cc;
159
160         reg_val = (cc - CHRG_CCCV_CC_OFFSET) / CHRG_CCCV_CC_LSB_RES;
161         cc = (reg_val * CHRG_CCCV_CC_LSB_RES) + CHRG_CCCV_CC_OFFSET;
162         reg_val = reg_val << CHRG_CCCV_CC_BIT_POS;
163
164         ret = regmap_update_bits(info->regmap,
165                                 AXP20X_CHRG_CTRL1,
166                                 CHRG_CCCV_CC_MASK, reg_val);
167         if (ret >= 0)
168                 info->cc = cc;
169
170         return ret;
171 }
172
173 static inline int axp288_charger_set_cv(struct axp288_chrg_info *info, int cv)
174 {
175         u8 reg_val;
176         int ret;
177
178         if (cv <= CV_4100MV) {
179                 reg_val = CHRG_CCCV_CV_4100MV;
180                 cv = CV_4100MV;
181         } else if (cv <= CV_4150MV) {
182                 reg_val = CHRG_CCCV_CV_4150MV;
183                 cv = CV_4150MV;
184         } else if (cv <= CV_4200MV) {
185                 reg_val = CHRG_CCCV_CV_4200MV;
186                 cv = CV_4200MV;
187         } else {
188                 reg_val = CHRG_CCCV_CV_4350MV;
189                 cv = CV_4350MV;
190         }
191
192         reg_val = reg_val << CHRG_CCCV_CV_BIT_POS;
193
194         ret = regmap_update_bits(info->regmap,
195                                 AXP20X_CHRG_CTRL1,
196                                 CHRG_CCCV_CV_MASK, reg_val);
197
198         if (ret >= 0)
199                 info->cv = cv;
200
201         return ret;
202 }
203
204 static int axp288_charger_get_vbus_inlmt(struct axp288_chrg_info *info)
205 {
206         unsigned int val;
207         int ret;
208
209         ret = regmap_read(info->regmap, AXP20X_CHRG_BAK_CTRL, &val);
210         if (ret < 0)
211                 return ret;
212
213         val >>= CHRG_VBUS_ILIM_BIT_POS;
214         switch (val) {
215         case CHRG_VBUS_ILIM_100MA:
216                 return 100000;
217         case CHRG_VBUS_ILIM_500MA:
218                 return 500000;
219         case CHRG_VBUS_ILIM_900MA:
220                 return 900000;
221         case CHRG_VBUS_ILIM_1500MA:
222                 return 1500000;
223         case CHRG_VBUS_ILIM_2000MA:
224                 return 2000000;
225         case CHRG_VBUS_ILIM_2500MA:
226                 return 2500000;
227         case CHRG_VBUS_ILIM_3000MA:
228                 return 3000000;
229         case CHRG_VBUS_ILIM_3500MA:
230                 return 3500000;
231         default:
232                 /* All b1xxx values map to 4000 mA */
233                 return 4000000;
234         }
235 }
236
237 static inline int axp288_charger_set_vbus_inlmt(struct axp288_chrg_info *info,
238                                            int inlmt)
239 {
240         int ret;
241         u8 reg_val;
242
243         if (inlmt >= 4000000)
244                 reg_val = CHRG_VBUS_ILIM_4000MA << CHRG_VBUS_ILIM_BIT_POS;
245         else if (inlmt >= 3500000)
246                 reg_val = CHRG_VBUS_ILIM_3500MA << CHRG_VBUS_ILIM_BIT_POS;
247         else if (inlmt >= 3000000)
248                 reg_val = CHRG_VBUS_ILIM_3000MA << CHRG_VBUS_ILIM_BIT_POS;
249         else if (inlmt >= 2500000)
250                 reg_val = CHRG_VBUS_ILIM_2500MA << CHRG_VBUS_ILIM_BIT_POS;
251         else if (inlmt >= 2000000)
252                 reg_val = CHRG_VBUS_ILIM_2000MA << CHRG_VBUS_ILIM_BIT_POS;
253         else if (inlmt >= 1500000)
254                 reg_val = CHRG_VBUS_ILIM_1500MA << CHRG_VBUS_ILIM_BIT_POS;
255         else if (inlmt >= 900000)
256                 reg_val = CHRG_VBUS_ILIM_900MA << CHRG_VBUS_ILIM_BIT_POS;
257         else if (inlmt >= 500000)
258                 reg_val = CHRG_VBUS_ILIM_500MA << CHRG_VBUS_ILIM_BIT_POS;
259         else
260                 reg_val = CHRG_VBUS_ILIM_100MA << CHRG_VBUS_ILIM_BIT_POS;
261
262         ret = regmap_update_bits(info->regmap, AXP20X_CHRG_BAK_CTRL,
263                                  CHRG_VBUS_ILIM_MASK, reg_val);
264         if (ret < 0)
265                 dev_err(&info->pdev->dev, "charger BAK control %d\n", ret);
266
267         return ret;
268 }
269
270 static int axp288_charger_vbus_path_select(struct axp288_chrg_info *info,
271                                                                 bool enable)
272 {
273         int ret;
274
275         if (enable)
276                 ret = regmap_update_bits(info->regmap, AXP20X_VBUS_IPSOUT_MGMT,
277                                         VBUS_ISPOUT_VBUS_PATH_DIS, 0);
278         else
279                 ret = regmap_update_bits(info->regmap, AXP20X_VBUS_IPSOUT_MGMT,
280                         VBUS_ISPOUT_VBUS_PATH_DIS, VBUS_ISPOUT_VBUS_PATH_DIS);
281
282         if (ret < 0)
283                 dev_err(&info->pdev->dev, "axp288 vbus path select %d\n", ret);
284
285         return ret;
286 }
287
288 static int axp288_charger_enable_charger(struct axp288_chrg_info *info,
289                                                                 bool enable)
290 {
291         int ret;
292
293         if (enable)
294                 ret = regmap_update_bits(info->regmap, AXP20X_CHRG_CTRL1,
295                                 CHRG_CCCV_CHG_EN, CHRG_CCCV_CHG_EN);
296         else
297                 ret = regmap_update_bits(info->regmap, AXP20X_CHRG_CTRL1,
298                                 CHRG_CCCV_CHG_EN, 0);
299         if (ret < 0)
300                 dev_err(&info->pdev->dev, "axp288 enable charger %d\n", ret);
301
302         return ret;
303 }
304
305 static int axp288_charger_is_present(struct axp288_chrg_info *info)
306 {
307         int ret, present = 0;
308         unsigned int val;
309
310         ret = regmap_read(info->regmap, AXP20X_PWR_INPUT_STATUS, &val);
311         if (ret < 0)
312                 return ret;
313
314         if (val & PS_STAT_VBUS_PRESENT)
315                 present = 1;
316         return present;
317 }
318
319 static int axp288_charger_is_online(struct axp288_chrg_info *info)
320 {
321         int ret, online = 0;
322         unsigned int val;
323
324         ret = regmap_read(info->regmap, AXP20X_PWR_INPUT_STATUS, &val);
325         if (ret < 0)
326                 return ret;
327
328         if (val & PS_STAT_VBUS_VALID)
329                 online = 1;
330         return online;
331 }
332
333 static int axp288_get_charger_health(struct axp288_chrg_info *info)
334 {
335         int ret, pwr_stat, chrg_stat;
336         int health = POWER_SUPPLY_HEALTH_UNKNOWN;
337         unsigned int val;
338
339         ret = regmap_read(info->regmap, AXP20X_PWR_INPUT_STATUS, &val);
340         if ((ret < 0) || !(val & PS_STAT_VBUS_PRESENT))
341                 goto health_read_fail;
342         else
343                 pwr_stat = val;
344
345         ret = regmap_read(info->regmap, AXP20X_PWR_OP_MODE, &val);
346         if (ret < 0)
347                 goto health_read_fail;
348         else
349                 chrg_stat = val;
350
351         if (!(pwr_stat & PS_STAT_VBUS_VALID))
352                 health = POWER_SUPPLY_HEALTH_DEAD;
353         else if (chrg_stat & CHRG_STAT_PMIC_OTP)
354                 health = POWER_SUPPLY_HEALTH_OVERHEAT;
355         else if (chrg_stat & CHRG_STAT_BAT_SAFE_MODE)
356                 health = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
357         else
358                 health = POWER_SUPPLY_HEALTH_GOOD;
359
360 health_read_fail:
361         return health;
362 }
363
364 static int axp288_charger_usb_set_property(struct power_supply *psy,
365                                     enum power_supply_property psp,
366                                     const union power_supply_propval *val)
367 {
368         struct axp288_chrg_info *info = power_supply_get_drvdata(psy);
369         int ret = 0;
370         int scaled_val;
371
372         switch (psp) {
373         case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
374                 scaled_val = min(val->intval, info->max_cc);
375                 scaled_val = DIV_ROUND_CLOSEST(scaled_val, 1000);
376                 ret = axp288_charger_set_cc(info, scaled_val);
377                 if (ret < 0)
378                         dev_warn(&info->pdev->dev, "set charge current failed\n");
379                 break;
380         case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
381                 scaled_val = min(val->intval, info->max_cv);
382                 scaled_val = DIV_ROUND_CLOSEST(scaled_val, 1000);
383                 ret = axp288_charger_set_cv(info, scaled_val);
384                 if (ret < 0)
385                         dev_warn(&info->pdev->dev, "set charge voltage failed\n");
386                 break;
387         case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
388                 ret = axp288_charger_set_vbus_inlmt(info, val->intval);
389                 if (ret < 0)
390                         dev_warn(&info->pdev->dev, "set input current limit failed\n");
391                 break;
392         default:
393                 ret = -EINVAL;
394         }
395
396         return ret;
397 }
398
399 static int axp288_charger_usb_get_property(struct power_supply *psy,
400                                     enum power_supply_property psp,
401                                     union power_supply_propval *val)
402 {
403         struct axp288_chrg_info *info = power_supply_get_drvdata(psy);
404         int ret;
405
406         switch (psp) {
407         case POWER_SUPPLY_PROP_PRESENT:
408                 /* Check for OTG case first */
409                 if (info->otg.id_short) {
410                         val->intval = 0;
411                         break;
412                 }
413                 ret = axp288_charger_is_present(info);
414                 if (ret < 0)
415                         return ret;
416                 val->intval = ret;
417                 break;
418         case POWER_SUPPLY_PROP_ONLINE:
419                 /* Check for OTG case first */
420                 if (info->otg.id_short) {
421                         val->intval = 0;
422                         break;
423                 }
424                 ret = axp288_charger_is_online(info);
425                 if (ret < 0)
426                         return ret;
427                 val->intval = ret;
428                 break;
429         case POWER_SUPPLY_PROP_HEALTH:
430                 val->intval = axp288_get_charger_health(info);
431                 break;
432         case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
433                 val->intval = info->cc * 1000;
434                 break;
435         case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
436                 val->intval = info->max_cc * 1000;
437                 break;
438         case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
439                 val->intval = info->cv * 1000;
440                 break;
441         case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
442                 val->intval = info->max_cv * 1000;
443                 break;
444         case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
445                 ret = axp288_charger_get_vbus_inlmt(info);
446                 if (ret < 0)
447                         return ret;
448                 val->intval = ret;
449                 break;
450         default:
451                 return -EINVAL;
452         }
453
454         return 0;
455 }
456
457 static int axp288_charger_property_is_writeable(struct power_supply *psy,
458                 enum power_supply_property psp)
459 {
460         int ret;
461
462         switch (psp) {
463         case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
464         case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
465         case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
466                 ret = 1;
467                 break;
468         default:
469                 ret = 0;
470         }
471
472         return ret;
473 }
474
475 static enum power_supply_property axp288_usb_props[] = {
476         POWER_SUPPLY_PROP_PRESENT,
477         POWER_SUPPLY_PROP_ONLINE,
478         POWER_SUPPLY_PROP_TYPE,
479         POWER_SUPPLY_PROP_HEALTH,
480         POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
481         POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
482         POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
483         POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
484         POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
485 };
486
487 static const struct power_supply_desc axp288_charger_desc = {
488         .name                   = "axp288_charger",
489         .type                   = POWER_SUPPLY_TYPE_USB,
490         .properties             = axp288_usb_props,
491         .num_properties         = ARRAY_SIZE(axp288_usb_props),
492         .get_property           = axp288_charger_usb_get_property,
493         .set_property           = axp288_charger_usb_set_property,
494         .property_is_writeable  = axp288_charger_property_is_writeable,
495 };
496
497 static irqreturn_t axp288_charger_irq_thread_handler(int irq, void *dev)
498 {
499         struct axp288_chrg_info *info = dev;
500         int i;
501
502         for (i = 0; i < CHRG_INTR_END; i++) {
503                 if (info->irq[i] == irq)
504                         break;
505         }
506
507         if (i >= CHRG_INTR_END) {
508                 dev_warn(&info->pdev->dev, "spurious interrupt!!\n");
509                 return IRQ_NONE;
510         }
511
512         switch (i) {
513         case VBUS_OV_IRQ:
514                 dev_dbg(&info->pdev->dev, "VBUS Over Voltage INTR\n");
515                 break;
516         case CHARGE_DONE_IRQ:
517                 dev_dbg(&info->pdev->dev, "Charging Done INTR\n");
518                 break;
519         case CHARGE_CHARGING_IRQ:
520                 dev_dbg(&info->pdev->dev, "Start Charging IRQ\n");
521                 break;
522         case BAT_SAFE_QUIT_IRQ:
523                 dev_dbg(&info->pdev->dev,
524                         "Quit Safe Mode(restart timer) Charging IRQ\n");
525                 break;
526         case BAT_SAFE_ENTER_IRQ:
527                 dev_dbg(&info->pdev->dev,
528                         "Enter Safe Mode(timer expire) Charging IRQ\n");
529                 break;
530         case QCBTU_IRQ:
531                 dev_dbg(&info->pdev->dev,
532                         "Quit Battery Under Temperature(CHRG) INTR\n");
533                 break;
534         case CBTU_IRQ:
535                 dev_dbg(&info->pdev->dev,
536                         "Hit Battery Under Temperature(CHRG) INTR\n");
537                 break;
538         case QCBTO_IRQ:
539                 dev_dbg(&info->pdev->dev,
540                         "Quit Battery Over Temperature(CHRG) INTR\n");
541                 break;
542         case CBTO_IRQ:
543                 dev_dbg(&info->pdev->dev,
544                         "Hit Battery Over Temperature(CHRG) INTR\n");
545                 break;
546         default:
547                 dev_warn(&info->pdev->dev, "Spurious Interrupt!!!\n");
548                 goto out;
549         }
550
551         power_supply_changed(info->psy_usb);
552 out:
553         return IRQ_HANDLED;
554 }
555
556 /*
557  * The HP Pavilion x2 10 series comes in a number of variants:
558  * Bay Trail SoC    + AXP288 PMIC, Micro-USB, DMI_BOARD_NAME: "8021"
559  * Bay Trail SoC    + AXP288 PMIC, Type-C,    DMI_BOARD_NAME: "815D"
560  * Cherry Trail SoC + AXP288 PMIC, Type-C,    DMI_BOARD_NAME: "813E"
561  * Cherry Trail SoC + TI PMIC,     Type-C,    DMI_BOARD_NAME: "827C" or "82F4"
562  *
563  * The variants with the AXP288 + Type-C connector are all kinds of special:
564  *
565  * 1. They use a Type-C connector which the AXP288 does not support, so when
566  * using a Type-C charger it is not recognized. Unlike most AXP288 devices,
567  * this model actually has mostly working ACPI AC / Battery code, the ACPI code
568  * "solves" this by simply setting the input_current_limit to 3A.
569  * There are still some issues with the ACPI code, so we use this native driver,
570  * and to solve the charging not working (500mA is not enough) issue we hardcode
571  * the 3A input_current_limit like the ACPI code does.
572  *
573  * 2. If no charger is connected the machine boots with the vbus-path disabled.
574  * Normally this is done when a 5V boost converter is active to avoid the PMIC
575  * trying to charge from the 5V boost converter's output. This is done when
576  * an OTG host cable is inserted and the ID pin on the micro-B receptacle is
577  * pulled low and the ID pin has an ACPI event handler associated with it
578  * which re-enables the vbus-path when the ID pin is pulled high when the
579  * OTG host cable is removed. The Type-C connector has no ID pin, there is
580  * no ID pin handler and there appears to be no 5V boost converter, so we
581  * end up not charging because the vbus-path is disabled, until we unplug
582  * the charger which automatically clears the vbus-path disable bit and then
583  * on the second plug-in of the adapter we start charging. To solve the not
584  * charging on first charger plugin we unconditionally enable the vbus-path at
585  * probe on this model, which is safe since there is no 5V boost converter.
586  */
587 static const struct dmi_system_id axp288_hp_x2_dmi_ids[] = {
588         {
589                 .matches = {
590                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
591                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP Pavilion x2 Detachable"),
592                         DMI_EXACT_MATCH(DMI_BOARD_NAME, "815D"),
593                 },
594         },
595         {
596                 .matches = {
597                         DMI_EXACT_MATCH(DMI_SYS_VENDOR, "HP"),
598                         DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP Pavilion x2 Detachable"),
599                         DMI_EXACT_MATCH(DMI_BOARD_NAME, "813E"),
600                 },
601         },
602         {} /* Terminating entry */
603 };
604
605 static void axp288_charger_extcon_evt_worker(struct work_struct *work)
606 {
607         struct axp288_chrg_info *info =
608             container_of(work, struct axp288_chrg_info, cable.work);
609         int ret, current_limit;
610         struct extcon_dev *edev = info->cable.edev;
611         unsigned int val;
612
613         ret = regmap_read(info->regmap, AXP20X_PWR_INPUT_STATUS, &val);
614         if (ret < 0) {
615                 dev_err(&info->pdev->dev, "Error reading status (%d)\n", ret);
616                 return;
617         }
618
619         /* Offline? Disable charging and bail */
620         if (!(val & PS_STAT_VBUS_VALID)) {
621                 dev_dbg(&info->pdev->dev, "USB charger disconnected\n");
622                 axp288_charger_enable_charger(info, false);
623                 power_supply_changed(info->psy_usb);
624                 return;
625         }
626
627         /* Determine cable/charger type */
628         if (dmi_check_system(axp288_hp_x2_dmi_ids)) {
629                 /* See comment above axp288_hp_x2_dmi_ids declaration */
630                 dev_dbg(&info->pdev->dev, "HP X2 with Type-C, setting inlmt to 3A\n");
631                 current_limit = 3000000;
632         } else if (extcon_get_state(edev, EXTCON_CHG_USB_SDP) > 0) {
633                 dev_dbg(&info->pdev->dev, "USB SDP charger is connected\n");
634                 current_limit = 500000;
635         } else if (extcon_get_state(edev, EXTCON_CHG_USB_CDP) > 0) {
636                 dev_dbg(&info->pdev->dev, "USB CDP charger is connected\n");
637                 current_limit = 1500000;
638         } else if (extcon_get_state(edev, EXTCON_CHG_USB_DCP) > 0) {
639                 dev_dbg(&info->pdev->dev, "USB DCP charger is connected\n");
640                 current_limit = 2000000;
641         } else {
642                 /* Charger type detection still in progress, bail. */
643                 return;
644         }
645
646         /* Set vbus current limit first, then enable charger */
647         ret = axp288_charger_set_vbus_inlmt(info, current_limit);
648         if (ret == 0)
649                 axp288_charger_enable_charger(info, true);
650         else
651                 dev_err(&info->pdev->dev,
652                         "error setting current limit (%d)\n", ret);
653
654         power_supply_changed(info->psy_usb);
655 }
656
657 static int axp288_charger_handle_cable_evt(struct notifier_block *nb,
658                                            unsigned long event, void *param)
659 {
660         struct axp288_chrg_info *info =
661                 container_of(nb, struct axp288_chrg_info, cable.nb);
662         schedule_work(&info->cable.work);
663         return NOTIFY_OK;
664 }
665
666 static void axp288_charger_otg_evt_worker(struct work_struct *work)
667 {
668         struct axp288_chrg_info *info =
669             container_of(work, struct axp288_chrg_info, otg.work);
670         struct extcon_dev *edev = info->otg.cable;
671         int ret, usb_host = extcon_get_state(edev, EXTCON_USB_HOST);
672
673         dev_dbg(&info->pdev->dev, "external connector USB-Host is %s\n",
674                                 usb_host ? "attached" : "detached");
675
676         /*
677          * Set usb_id_short flag to avoid running charger detection logic
678          * in case usb host.
679          */
680         info->otg.id_short = usb_host;
681
682         /* Disable VBUS path before enabling the 5V boost */
683         ret = axp288_charger_vbus_path_select(info, !info->otg.id_short);
684         if (ret < 0)
685                 dev_warn(&info->pdev->dev, "vbus path disable failed\n");
686 }
687
688 static int axp288_charger_handle_otg_evt(struct notifier_block *nb,
689                                    unsigned long event, void *param)
690 {
691         struct axp288_chrg_info *info =
692             container_of(nb, struct axp288_chrg_info, otg.id_nb);
693
694         schedule_work(&info->otg.work);
695
696         return NOTIFY_OK;
697 }
698
699 static int charger_init_hw_regs(struct axp288_chrg_info *info)
700 {
701         int ret, cc, cv;
702         unsigned int val;
703
704         /* Program temperature thresholds */
705         ret = regmap_write(info->regmap, AXP20X_V_LTF_CHRG, CHRG_VLTFC_0C);
706         if (ret < 0) {
707                 dev_err(&info->pdev->dev, "register(%x) write error(%d)\n",
708                                                         AXP20X_V_LTF_CHRG, ret);
709                 return ret;
710         }
711
712         ret = regmap_write(info->regmap, AXP20X_V_HTF_CHRG, CHRG_VHTFC_45C);
713         if (ret < 0) {
714                 dev_err(&info->pdev->dev, "register(%x) write error(%d)\n",
715                                                         AXP20X_V_HTF_CHRG, ret);
716                 return ret;
717         }
718
719         /* Do not turn-off charger o/p after charge cycle ends */
720         ret = regmap_update_bits(info->regmap,
721                                 AXP20X_CHRG_CTRL2,
722                                 CNTL2_CHG_OUT_TURNON, CNTL2_CHG_OUT_TURNON);
723         if (ret < 0) {
724                 dev_err(&info->pdev->dev, "register(%x) write error(%d)\n",
725                                                 AXP20X_CHRG_CTRL2, ret);
726                 return ret;
727         }
728
729         /* Setup ending condition for charging to be 10% of I(chrg) */
730         ret = regmap_update_bits(info->regmap,
731                                 AXP20X_CHRG_CTRL1,
732                                 CHRG_CCCV_ITERM_20P, 0);
733         if (ret < 0) {
734                 dev_err(&info->pdev->dev, "register(%x) write error(%d)\n",
735                                                 AXP20X_CHRG_CTRL1, ret);
736                 return ret;
737         }
738
739         /* Disable OCV-SOC curve calibration */
740         ret = regmap_update_bits(info->regmap,
741                                 AXP20X_CC_CTRL,
742                                 FG_CNTL_OCV_ADJ_EN, 0);
743         if (ret < 0) {
744                 dev_err(&info->pdev->dev, "register(%x) write error(%d)\n",
745                                                 AXP20X_CC_CTRL, ret);
746                 return ret;
747         }
748
749         if (dmi_check_system(axp288_hp_x2_dmi_ids)) {
750                 /* See comment above axp288_hp_x2_dmi_ids declaration */
751                 ret = axp288_charger_vbus_path_select(info, true);
752                 if (ret < 0)
753                         return ret;
754         }
755
756         /* Read current charge voltage and current limit */
757         ret = regmap_read(info->regmap, AXP20X_CHRG_CTRL1, &val);
758         if (ret < 0) {
759                 dev_err(&info->pdev->dev, "register(%x) read error(%d)\n",
760                         AXP20X_CHRG_CTRL1, ret);
761                 return ret;
762         }
763
764         /* Determine charge voltage */
765         cv = (val & CHRG_CCCV_CV_MASK) >> CHRG_CCCV_CV_BIT_POS;
766         switch (cv) {
767         case CHRG_CCCV_CV_4100MV:
768                 info->cv = CV_4100MV;
769                 break;
770         case CHRG_CCCV_CV_4150MV:
771                 info->cv = CV_4150MV;
772                 break;
773         case CHRG_CCCV_CV_4200MV:
774                 info->cv = CV_4200MV;
775                 break;
776         case CHRG_CCCV_CV_4350MV:
777                 info->cv = CV_4350MV;
778                 break;
779         }
780
781         /* Determine charge current limit */
782         cc = (val & CHRG_CCCV_CC_MASK) >> CHRG_CCCV_CC_BIT_POS;
783         cc = (cc * CHRG_CCCV_CC_LSB_RES) + CHRG_CCCV_CC_OFFSET;
784         info->cc = cc;
785
786         /*
787          * Do not allow the user to configure higher settings then those
788          * set by the firmware
789          */
790         info->max_cv = info->cv;
791         info->max_cc = info->cc;
792
793         return 0;
794 }
795
796 static void axp288_charger_cancel_work(void *data)
797 {
798         struct axp288_chrg_info *info = data;
799
800         cancel_work_sync(&info->otg.work);
801         cancel_work_sync(&info->cable.work);
802 }
803
804 static int axp288_charger_probe(struct platform_device *pdev)
805 {
806         int ret, i, pirq;
807         struct axp288_chrg_info *info;
808         struct device *dev = &pdev->dev;
809         struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
810         struct power_supply_config charger_cfg = {};
811         unsigned int val;
812
813         /*
814          * On some devices the fuelgauge and charger parts of the axp288 are
815          * not used, check that the fuelgauge is enabled (CC_CTRL != 0).
816          */
817         ret = regmap_read(axp20x->regmap, AXP20X_CC_CTRL, &val);
818         if (ret < 0)
819                 return ret;
820         if (val == 0)
821                 return -ENODEV;
822
823         info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
824         if (!info)
825                 return -ENOMEM;
826
827         info->pdev = pdev;
828         info->regmap = axp20x->regmap;
829         info->regmap_irqc = axp20x->regmap_irqc;
830
831         info->cable.edev = extcon_get_extcon_dev(AXP288_EXTCON_DEV_NAME);
832         if (info->cable.edev == NULL) {
833                 dev_dbg(&pdev->dev, "%s is not ready, probe deferred\n",
834                         AXP288_EXTCON_DEV_NAME);
835                 return -EPROBE_DEFER;
836         }
837
838         if (acpi_dev_present(USB_HOST_EXTCON_HID, NULL, -1)) {
839                 info->otg.cable = extcon_get_extcon_dev(USB_HOST_EXTCON_NAME);
840                 if (info->otg.cable == NULL) {
841                         dev_dbg(dev, "EXTCON_USB_HOST is not ready, probe deferred\n");
842                         return -EPROBE_DEFER;
843                 }
844                 dev_info(&pdev->dev,
845                          "Using " USB_HOST_EXTCON_HID " extcon for usb-id\n");
846         }
847
848         platform_set_drvdata(pdev, info);
849
850         ret = charger_init_hw_regs(info);
851         if (ret)
852                 return ret;
853
854         /* Register with power supply class */
855         charger_cfg.drv_data = info;
856         info->psy_usb = devm_power_supply_register(dev, &axp288_charger_desc,
857                                                    &charger_cfg);
858         if (IS_ERR(info->psy_usb)) {
859                 ret = PTR_ERR(info->psy_usb);
860                 dev_err(dev, "failed to register power supply: %d\n", ret);
861                 return ret;
862         }
863
864         /* Cancel our work on cleanup, register this before the notifiers */
865         ret = devm_add_action(dev, axp288_charger_cancel_work, info);
866         if (ret)
867                 return ret;
868
869         /* Register for extcon notification */
870         INIT_WORK(&info->cable.work, axp288_charger_extcon_evt_worker);
871         info->cable.nb.notifier_call = axp288_charger_handle_cable_evt;
872         ret = devm_extcon_register_notifier_all(dev, info->cable.edev,
873                                                 &info->cable.nb);
874         if (ret) {
875                 dev_err(dev, "failed to register cable extcon notifier\n");
876                 return ret;
877         }
878         schedule_work(&info->cable.work);
879
880         /* Register for OTG notification */
881         INIT_WORK(&info->otg.work, axp288_charger_otg_evt_worker);
882         info->otg.id_nb.notifier_call = axp288_charger_handle_otg_evt;
883         if (info->otg.cable) {
884                 ret = devm_extcon_register_notifier(&pdev->dev, info->otg.cable,
885                                         EXTCON_USB_HOST, &info->otg.id_nb);
886                 if (ret) {
887                         dev_err(dev, "failed to register EXTCON_USB_HOST notifier\n");
888                         return ret;
889                 }
890                 schedule_work(&info->otg.work);
891         }
892
893         /* Register charger interrupts */
894         for (i = 0; i < CHRG_INTR_END; i++) {
895                 pirq = platform_get_irq(info->pdev, i);
896                 if (pirq < 0) {
897                         dev_err(&pdev->dev, "Failed to get IRQ: %d\n", pirq);
898                         return pirq;
899                 }
900                 info->irq[i] = regmap_irq_get_virq(info->regmap_irqc, pirq);
901                 if (info->irq[i] < 0) {
902                         dev_warn(&info->pdev->dev,
903                                 "failed to get virtual interrupt=%d\n", pirq);
904                         return info->irq[i];
905                 }
906                 ret = devm_request_threaded_irq(&info->pdev->dev, info->irq[i],
907                                         NULL, axp288_charger_irq_thread_handler,
908                                         IRQF_ONESHOT, info->pdev->name, info);
909                 if (ret) {
910                         dev_err(&pdev->dev, "failed to request interrupt=%d\n",
911                                                                 info->irq[i]);
912                         return ret;
913                 }
914         }
915
916         return 0;
917 }
918
919 static const struct platform_device_id axp288_charger_id_table[] = {
920         { .name = "axp288_charger" },
921         {},
922 };
923 MODULE_DEVICE_TABLE(platform, axp288_charger_id_table);
924
925 static struct platform_driver axp288_charger_driver = {
926         .probe = axp288_charger_probe,
927         .id_table = axp288_charger_id_table,
928         .driver = {
929                 .name = "axp288_charger",
930         },
931 };
932
933 module_platform_driver(axp288_charger_driver);
934
935 MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>");
936 MODULE_DESCRIPTION("X-power AXP288 Charger Driver");
937 MODULE_LICENSE("GPL v2");