1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for the TI bq24190 battery charger.
5 * Author: Mark A. Greer <mgreer@animalcreek.com>
8 #include <linux/mod_devicetable.h>
9 #include <linux/module.h>
10 #include <linux/interrupt.h>
11 #include <linux/delay.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/power_supply.h>
14 #include <linux/power/bq24190_charger.h>
15 #include <linux/regulator/driver.h>
16 #include <linux/regulator/machine.h>
17 #include <linux/workqueue.h>
18 #include <linux/i2c.h>
19 #include <linux/extcon-provider.h>
21 #define BQ24190_MANUFACTURER "Texas Instruments"
23 #define BQ24190_REG_ISC 0x00 /* Input Source Control */
24 #define BQ24190_REG_ISC_EN_HIZ_MASK BIT(7)
25 #define BQ24190_REG_ISC_EN_HIZ_SHIFT 7
26 #define BQ24190_REG_ISC_VINDPM_MASK (BIT(6) | BIT(5) | BIT(4) | \
28 #define BQ24190_REG_ISC_VINDPM_SHIFT 3
29 #define BQ24190_REG_ISC_IINLIM_MASK (BIT(2) | BIT(1) | BIT(0))
30 #define BQ24190_REG_ISC_IINLIM_SHIFT 0
32 #define BQ24190_REG_POC 0x01 /* Power-On Configuration */
33 #define BQ24190_REG_POC_RESET_MASK BIT(7)
34 #define BQ24190_REG_POC_RESET_SHIFT 7
35 #define BQ24190_REG_POC_WDT_RESET_MASK BIT(6)
36 #define BQ24190_REG_POC_WDT_RESET_SHIFT 6
37 #define BQ24190_REG_POC_CHG_CONFIG_MASK (BIT(5) | BIT(4))
38 #define BQ24190_REG_POC_CHG_CONFIG_SHIFT 4
39 #define BQ24190_REG_POC_CHG_CONFIG_DISABLE 0x0
40 #define BQ24190_REG_POC_CHG_CONFIG_CHARGE 0x1
41 #define BQ24190_REG_POC_CHG_CONFIG_OTG 0x2
42 #define BQ24190_REG_POC_CHG_CONFIG_OTG_ALT 0x3
43 #define BQ24190_REG_POC_SYS_MIN_MASK (BIT(3) | BIT(2) | BIT(1))
44 #define BQ24190_REG_POC_SYS_MIN_SHIFT 1
45 #define BQ24190_REG_POC_SYS_MIN_MIN 3000
46 #define BQ24190_REG_POC_SYS_MIN_MAX 3700
47 #define BQ24190_REG_POC_BOOST_LIM_MASK BIT(0)
48 #define BQ24190_REG_POC_BOOST_LIM_SHIFT 0
50 #define BQ24190_REG_CCC 0x02 /* Charge Current Control */
51 #define BQ24190_REG_CCC_ICHG_MASK (BIT(7) | BIT(6) | BIT(5) | \
52 BIT(4) | BIT(3) | BIT(2))
53 #define BQ24190_REG_CCC_ICHG_SHIFT 2
54 #define BQ24190_REG_CCC_FORCE_20PCT_MASK BIT(0)
55 #define BQ24190_REG_CCC_FORCE_20PCT_SHIFT 0
57 #define BQ24190_REG_PCTCC 0x03 /* Pre-charge/Termination Current Cntl */
58 #define BQ24190_REG_PCTCC_IPRECHG_MASK (BIT(7) | BIT(6) | BIT(5) | \
60 #define BQ24190_REG_PCTCC_IPRECHG_SHIFT 4
61 #define BQ24190_REG_PCTCC_IPRECHG_MIN 128
62 #define BQ24190_REG_PCTCC_IPRECHG_MAX 2048
63 #define BQ24190_REG_PCTCC_ITERM_MASK (BIT(3) | BIT(2) | BIT(1) | \
65 #define BQ24190_REG_PCTCC_ITERM_SHIFT 0
66 #define BQ24190_REG_PCTCC_ITERM_MIN 128
67 #define BQ24190_REG_PCTCC_ITERM_MAX 2048
69 #define BQ24190_REG_CVC 0x04 /* Charge Voltage Control */
70 #define BQ24190_REG_CVC_VREG_MASK (BIT(7) | BIT(6) | BIT(5) | \
71 BIT(4) | BIT(3) | BIT(2))
72 #define BQ24190_REG_CVC_VREG_SHIFT 2
73 #define BQ24190_REG_CVC_BATLOWV_MASK BIT(1)
74 #define BQ24190_REG_CVC_BATLOWV_SHIFT 1
75 #define BQ24190_REG_CVC_VRECHG_MASK BIT(0)
76 #define BQ24190_REG_CVC_VRECHG_SHIFT 0
78 #define BQ24190_REG_CTTC 0x05 /* Charge Term/Timer Control */
79 #define BQ24190_REG_CTTC_EN_TERM_MASK BIT(7)
80 #define BQ24190_REG_CTTC_EN_TERM_SHIFT 7
81 #define BQ24190_REG_CTTC_TERM_STAT_MASK BIT(6)
82 #define BQ24190_REG_CTTC_TERM_STAT_SHIFT 6
83 #define BQ24190_REG_CTTC_WATCHDOG_MASK (BIT(5) | BIT(4))
84 #define BQ24190_REG_CTTC_WATCHDOG_SHIFT 4
85 #define BQ24190_REG_CTTC_EN_TIMER_MASK BIT(3)
86 #define BQ24190_REG_CTTC_EN_TIMER_SHIFT 3
87 #define BQ24190_REG_CTTC_CHG_TIMER_MASK (BIT(2) | BIT(1))
88 #define BQ24190_REG_CTTC_CHG_TIMER_SHIFT 1
89 #define BQ24190_REG_CTTC_JEITA_ISET_MASK BIT(0)
90 #define BQ24190_REG_CTTC_JEITA_ISET_SHIFT 0
92 #define BQ24190_REG_ICTRC 0x06 /* IR Comp/Thermal Regulation Control */
93 #define BQ24190_REG_ICTRC_BAT_COMP_MASK (BIT(7) | BIT(6) | BIT(5))
94 #define BQ24190_REG_ICTRC_BAT_COMP_SHIFT 5
95 #define BQ24190_REG_ICTRC_VCLAMP_MASK (BIT(4) | BIT(3) | BIT(2))
96 #define BQ24190_REG_ICTRC_VCLAMP_SHIFT 2
97 #define BQ24190_REG_ICTRC_TREG_MASK (BIT(1) | BIT(0))
98 #define BQ24190_REG_ICTRC_TREG_SHIFT 0
100 #define BQ24190_REG_MOC 0x07 /* Misc. Operation Control */
101 #define BQ24190_REG_MOC_DPDM_EN_MASK BIT(7)
102 #define BQ24190_REG_MOC_DPDM_EN_SHIFT 7
103 #define BQ24190_REG_MOC_TMR2X_EN_MASK BIT(6)
104 #define BQ24190_REG_MOC_TMR2X_EN_SHIFT 6
105 #define BQ24190_REG_MOC_BATFET_DISABLE_MASK BIT(5)
106 #define BQ24190_REG_MOC_BATFET_DISABLE_SHIFT 5
107 #define BQ24190_REG_MOC_JEITA_VSET_MASK BIT(4)
108 #define BQ24190_REG_MOC_JEITA_VSET_SHIFT 4
109 #define BQ24190_REG_MOC_INT_MASK_MASK (BIT(1) | BIT(0))
110 #define BQ24190_REG_MOC_INT_MASK_SHIFT 0
112 #define BQ24190_REG_SS 0x08 /* System Status */
113 #define BQ24190_REG_SS_VBUS_STAT_MASK (BIT(7) | BIT(6))
114 #define BQ24190_REG_SS_VBUS_STAT_SHIFT 6
115 #define BQ24190_REG_SS_CHRG_STAT_MASK (BIT(5) | BIT(4))
116 #define BQ24190_REG_SS_CHRG_STAT_SHIFT 4
117 #define BQ24190_REG_SS_DPM_STAT_MASK BIT(3)
118 #define BQ24190_REG_SS_DPM_STAT_SHIFT 3
119 #define BQ24190_REG_SS_PG_STAT_MASK BIT(2)
120 #define BQ24190_REG_SS_PG_STAT_SHIFT 2
121 #define BQ24190_REG_SS_THERM_STAT_MASK BIT(1)
122 #define BQ24190_REG_SS_THERM_STAT_SHIFT 1
123 #define BQ24190_REG_SS_VSYS_STAT_MASK BIT(0)
124 #define BQ24190_REG_SS_VSYS_STAT_SHIFT 0
126 #define BQ24190_REG_F 0x09 /* Fault */
127 #define BQ24190_REG_F_WATCHDOG_FAULT_MASK BIT(7)
128 #define BQ24190_REG_F_WATCHDOG_FAULT_SHIFT 7
129 #define BQ24190_REG_F_BOOST_FAULT_MASK BIT(6)
130 #define BQ24190_REG_F_BOOST_FAULT_SHIFT 6
131 #define BQ24190_REG_F_CHRG_FAULT_MASK (BIT(5) | BIT(4))
132 #define BQ24190_REG_F_CHRG_FAULT_SHIFT 4
133 #define BQ24190_REG_F_BAT_FAULT_MASK BIT(3)
134 #define BQ24190_REG_F_BAT_FAULT_SHIFT 3
135 #define BQ24190_REG_F_NTC_FAULT_MASK (BIT(2) | BIT(1) | BIT(0))
136 #define BQ24190_REG_F_NTC_FAULT_SHIFT 0
138 #define BQ24190_REG_VPRS 0x0A /* Vendor/Part/Revision Status */
139 #define BQ24190_REG_VPRS_PN_MASK (BIT(5) | BIT(4) | BIT(3))
140 #define BQ24190_REG_VPRS_PN_SHIFT 3
141 #define BQ24190_REG_VPRS_PN_24190 0x4
142 #define BQ24190_REG_VPRS_PN_24192 0x5 /* Also 24193, 24196 */
143 #define BQ24190_REG_VPRS_PN_24192I 0x3
144 #define BQ24190_REG_VPRS_TS_PROFILE_MASK BIT(2)
145 #define BQ24190_REG_VPRS_TS_PROFILE_SHIFT 2
146 #define BQ24190_REG_VPRS_DEV_REG_MASK (BIT(1) | BIT(0))
147 #define BQ24190_REG_VPRS_DEV_REG_SHIFT 0
150 * The FAULT register is latched by the bq24190 (except for NTC_FAULT)
151 * so the first read after a fault returns the latched value and subsequent
152 * reads return the current value. In order to return the fault status
153 * to the user, have the interrupt handler save the reg's value and retrieve
154 * it in the appropriate health/status routine.
156 struct bq24190_dev_info {
157 struct i2c_client *client;
159 struct extcon_dev *edev;
160 struct power_supply *charger;
161 struct power_supply *battery;
162 struct delayed_work input_current_limit_work;
163 char model_name[I2C_NAME_SIZE];
166 bool otg_vbus_enabled;
175 struct mutex f_reg_lock;
181 static int bq24190_charger_set_charge_type(struct bq24190_dev_info *bdi,
182 const union power_supply_propval *val);
184 static const unsigned int bq24190_usb_extcon_cable[] = {
190 * The tables below provide a 2-way mapping for the value that goes in
191 * the register field and the real-world value that it represents.
192 * The index of the array is the value that goes in the register; the
193 * number at that index in the array is the real-world value that it
197 /* REG00[2:0] (IINLIM) in uAh */
198 static const int bq24190_isc_iinlim_values[] = {
199 100000, 150000, 500000, 900000, 1200000, 1500000, 2000000, 3000000
202 /* REG02[7:2] (ICHG) in uAh */
203 static const int bq24190_ccc_ichg_values[] = {
204 512000, 576000, 640000, 704000, 768000, 832000, 896000, 960000,
205 1024000, 1088000, 1152000, 1216000, 1280000, 1344000, 1408000, 1472000,
206 1536000, 1600000, 1664000, 1728000, 1792000, 1856000, 1920000, 1984000,
207 2048000, 2112000, 2176000, 2240000, 2304000, 2368000, 2432000, 2496000,
208 2560000, 2624000, 2688000, 2752000, 2816000, 2880000, 2944000, 3008000,
209 3072000, 3136000, 3200000, 3264000, 3328000, 3392000, 3456000, 3520000,
210 3584000, 3648000, 3712000, 3776000, 3840000, 3904000, 3968000, 4032000,
211 4096000, 4160000, 4224000, 4288000, 4352000, 4416000, 4480000, 4544000
214 /* REG04[7:2] (VREG) in uV */
215 static const int bq24190_cvc_vreg_values[] = {
216 3504000, 3520000, 3536000, 3552000, 3568000, 3584000, 3600000, 3616000,
217 3632000, 3648000, 3664000, 3680000, 3696000, 3712000, 3728000, 3744000,
218 3760000, 3776000, 3792000, 3808000, 3824000, 3840000, 3856000, 3872000,
219 3888000, 3904000, 3920000, 3936000, 3952000, 3968000, 3984000, 4000000,
220 4016000, 4032000, 4048000, 4064000, 4080000, 4096000, 4112000, 4128000,
221 4144000, 4160000, 4176000, 4192000, 4208000, 4224000, 4240000, 4256000,
222 4272000, 4288000, 4304000, 4320000, 4336000, 4352000, 4368000, 4384000,
226 /* REG06[1:0] (TREG) in tenths of degrees Celsius */
227 static const int bq24190_ictrc_treg_values[] = {
232 * Return the index in 'tbl' of greatest value that is less than or equal to
233 * 'val'. The index range returned is 0 to 'tbl_size' - 1. Assumes that
234 * the values in 'tbl' are sorted from smallest to largest and 'tbl_size'
237 static u8 bq24190_find_idx(const int tbl[], int tbl_size, int v)
241 for (i = 1; i < tbl_size; i++)
248 /* Basic driver I/O routines */
250 static int bq24190_read(struct bq24190_dev_info *bdi, u8 reg, u8 *data)
254 ret = i2c_smbus_read_byte_data(bdi->client, reg);
262 static int bq24190_write(struct bq24190_dev_info *bdi, u8 reg, u8 data)
264 return i2c_smbus_write_byte_data(bdi->client, reg, data);
267 static int bq24190_read_mask(struct bq24190_dev_info *bdi, u8 reg,
268 u8 mask, u8 shift, u8 *data)
273 ret = bq24190_read(bdi, reg, &v);
284 static int bq24190_write_mask(struct bq24190_dev_info *bdi, u8 reg,
285 u8 mask, u8 shift, u8 data)
290 ret = bq24190_read(bdi, reg, &v);
295 v |= ((data << shift) & mask);
297 return bq24190_write(bdi, reg, v);
300 static int bq24190_get_field_val(struct bq24190_dev_info *bdi,
301 u8 reg, u8 mask, u8 shift,
302 const int tbl[], int tbl_size,
308 ret = bq24190_read_mask(bdi, reg, mask, shift, &v);
312 v = (v >= tbl_size) ? (tbl_size - 1) : v;
318 static int bq24190_set_field_val(struct bq24190_dev_info *bdi,
319 u8 reg, u8 mask, u8 shift,
320 const int tbl[], int tbl_size,
325 idx = bq24190_find_idx(tbl, tbl_size, val);
327 return bq24190_write_mask(bdi, reg, mask, shift, idx);
332 * There are a numerous options that are configurable on the bq24190
333 * that go well beyond what the power_supply properties provide access to.
334 * Provide sysfs access to them so they can be examined and possibly modified
335 * on the fly. They will be provided for the charger power_supply object only
336 * and will be prefixed by 'f_' to make them easier to recognize.
339 #define BQ24190_SYSFS_FIELD(_name, r, f, m, store) \
341 .attr = __ATTR(f_##_name, m, bq24190_sysfs_show, store), \
342 .reg = BQ24190_REG_##r, \
343 .mask = BQ24190_REG_##r##_##f##_MASK, \
344 .shift = BQ24190_REG_##r##_##f##_SHIFT, \
347 #define BQ24190_SYSFS_FIELD_RW(_name, r, f) \
348 BQ24190_SYSFS_FIELD(_name, r, f, S_IWUSR | S_IRUGO, \
351 #define BQ24190_SYSFS_FIELD_RO(_name, r, f) \
352 BQ24190_SYSFS_FIELD(_name, r, f, S_IRUGO, NULL)
354 static ssize_t bq24190_sysfs_show(struct device *dev,
355 struct device_attribute *attr, char *buf);
356 static ssize_t bq24190_sysfs_store(struct device *dev,
357 struct device_attribute *attr, const char *buf, size_t count);
359 struct bq24190_sysfs_field_info {
360 struct device_attribute attr;
366 /* On i386 ptrace-abi.h defines SS that breaks the macro calls below. */
369 static struct bq24190_sysfs_field_info bq24190_sysfs_field_tbl[] = {
370 /* sysfs name reg field in reg */
371 BQ24190_SYSFS_FIELD_RW(en_hiz, ISC, EN_HIZ),
372 BQ24190_SYSFS_FIELD_RW(vindpm, ISC, VINDPM),
373 BQ24190_SYSFS_FIELD_RW(iinlim, ISC, IINLIM),
374 BQ24190_SYSFS_FIELD_RW(chg_config, POC, CHG_CONFIG),
375 BQ24190_SYSFS_FIELD_RW(sys_min, POC, SYS_MIN),
376 BQ24190_SYSFS_FIELD_RW(boost_lim, POC, BOOST_LIM),
377 BQ24190_SYSFS_FIELD_RW(ichg, CCC, ICHG),
378 BQ24190_SYSFS_FIELD_RW(force_20_pct, CCC, FORCE_20PCT),
379 BQ24190_SYSFS_FIELD_RW(iprechg, PCTCC, IPRECHG),
380 BQ24190_SYSFS_FIELD_RW(iterm, PCTCC, ITERM),
381 BQ24190_SYSFS_FIELD_RW(vreg, CVC, VREG),
382 BQ24190_SYSFS_FIELD_RW(batlowv, CVC, BATLOWV),
383 BQ24190_SYSFS_FIELD_RW(vrechg, CVC, VRECHG),
384 BQ24190_SYSFS_FIELD_RW(en_term, CTTC, EN_TERM),
385 BQ24190_SYSFS_FIELD_RW(term_stat, CTTC, TERM_STAT),
386 BQ24190_SYSFS_FIELD_RO(watchdog, CTTC, WATCHDOG),
387 BQ24190_SYSFS_FIELD_RW(en_timer, CTTC, EN_TIMER),
388 BQ24190_SYSFS_FIELD_RW(chg_timer, CTTC, CHG_TIMER),
389 BQ24190_SYSFS_FIELD_RW(jeta_iset, CTTC, JEITA_ISET),
390 BQ24190_SYSFS_FIELD_RW(bat_comp, ICTRC, BAT_COMP),
391 BQ24190_SYSFS_FIELD_RW(vclamp, ICTRC, VCLAMP),
392 BQ24190_SYSFS_FIELD_RW(treg, ICTRC, TREG),
393 BQ24190_SYSFS_FIELD_RW(dpdm_en, MOC, DPDM_EN),
394 BQ24190_SYSFS_FIELD_RW(tmr2x_en, MOC, TMR2X_EN),
395 BQ24190_SYSFS_FIELD_RW(batfet_disable, MOC, BATFET_DISABLE),
396 BQ24190_SYSFS_FIELD_RW(jeita_vset, MOC, JEITA_VSET),
397 BQ24190_SYSFS_FIELD_RO(int_mask, MOC, INT_MASK),
398 BQ24190_SYSFS_FIELD_RO(vbus_stat, SS, VBUS_STAT),
399 BQ24190_SYSFS_FIELD_RO(chrg_stat, SS, CHRG_STAT),
400 BQ24190_SYSFS_FIELD_RO(dpm_stat, SS, DPM_STAT),
401 BQ24190_SYSFS_FIELD_RO(pg_stat, SS, PG_STAT),
402 BQ24190_SYSFS_FIELD_RO(therm_stat, SS, THERM_STAT),
403 BQ24190_SYSFS_FIELD_RO(vsys_stat, SS, VSYS_STAT),
404 BQ24190_SYSFS_FIELD_RO(watchdog_fault, F, WATCHDOG_FAULT),
405 BQ24190_SYSFS_FIELD_RO(boost_fault, F, BOOST_FAULT),
406 BQ24190_SYSFS_FIELD_RO(chrg_fault, F, CHRG_FAULT),
407 BQ24190_SYSFS_FIELD_RO(bat_fault, F, BAT_FAULT),
408 BQ24190_SYSFS_FIELD_RO(ntc_fault, F, NTC_FAULT),
409 BQ24190_SYSFS_FIELD_RO(pn, VPRS, PN),
410 BQ24190_SYSFS_FIELD_RO(ts_profile, VPRS, TS_PROFILE),
411 BQ24190_SYSFS_FIELD_RO(dev_reg, VPRS, DEV_REG),
414 static struct attribute *
415 bq24190_sysfs_attrs[ARRAY_SIZE(bq24190_sysfs_field_tbl) + 1];
417 ATTRIBUTE_GROUPS(bq24190_sysfs);
419 static void bq24190_sysfs_init_attrs(void)
421 int i, limit = ARRAY_SIZE(bq24190_sysfs_field_tbl);
423 for (i = 0; i < limit; i++)
424 bq24190_sysfs_attrs[i] = &bq24190_sysfs_field_tbl[i].attr.attr;
426 bq24190_sysfs_attrs[limit] = NULL; /* Has additional entry for this */
429 static struct bq24190_sysfs_field_info *bq24190_sysfs_field_lookup(
432 int i, limit = ARRAY_SIZE(bq24190_sysfs_field_tbl);
434 for (i = 0; i < limit; i++)
435 if (!strcmp(name, bq24190_sysfs_field_tbl[i].attr.attr.name))
441 return &bq24190_sysfs_field_tbl[i];
444 static ssize_t bq24190_sysfs_show(struct device *dev,
445 struct device_attribute *attr, char *buf)
447 struct power_supply *psy = dev_get_drvdata(dev);
448 struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy);
449 struct bq24190_sysfs_field_info *info;
454 info = bq24190_sysfs_field_lookup(attr->attr.name);
458 ret = pm_runtime_resume_and_get(bdi->dev);
462 ret = bq24190_read_mask(bdi, info->reg, info->mask, info->shift, &v);
466 count = scnprintf(buf, PAGE_SIZE, "%hhx\n", v);
468 pm_runtime_mark_last_busy(bdi->dev);
469 pm_runtime_put_autosuspend(bdi->dev);
474 static ssize_t bq24190_sysfs_store(struct device *dev,
475 struct device_attribute *attr, const char *buf, size_t count)
477 struct power_supply *psy = dev_get_drvdata(dev);
478 struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy);
479 struct bq24190_sysfs_field_info *info;
483 info = bq24190_sysfs_field_lookup(attr->attr.name);
487 ret = kstrtou8(buf, 0, &v);
491 ret = pm_runtime_resume_and_get(bdi->dev);
495 ret = bq24190_write_mask(bdi, info->reg, info->mask, info->shift, v);
499 pm_runtime_mark_last_busy(bdi->dev);
500 pm_runtime_put_autosuspend(bdi->dev);
506 static int bq24190_set_otg_vbus(struct bq24190_dev_info *bdi, bool enable)
508 union power_supply_propval val = { .intval = bdi->charge_type };
511 ret = pm_runtime_resume_and_get(bdi->dev);
513 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", ret);
517 bdi->otg_vbus_enabled = enable;
519 ret = bq24190_write_mask(bdi, BQ24190_REG_POC,
520 BQ24190_REG_POC_CHG_CONFIG_MASK,
521 BQ24190_REG_POC_CHG_CONFIG_SHIFT,
522 BQ24190_REG_POC_CHG_CONFIG_OTG);
524 ret = bq24190_charger_set_charge_type(bdi, &val);
526 pm_runtime_mark_last_busy(bdi->dev);
527 pm_runtime_put_autosuspend(bdi->dev);
532 #ifdef CONFIG_REGULATOR
533 static int bq24190_vbus_enable(struct regulator_dev *dev)
535 return bq24190_set_otg_vbus(rdev_get_drvdata(dev), true);
538 static int bq24190_vbus_disable(struct regulator_dev *dev)
540 return bq24190_set_otg_vbus(rdev_get_drvdata(dev), false);
543 static int bq24190_vbus_is_enabled(struct regulator_dev *dev)
545 struct bq24190_dev_info *bdi = rdev_get_drvdata(dev);
549 ret = pm_runtime_resume_and_get(bdi->dev);
551 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", ret);
555 ret = bq24190_read_mask(bdi, BQ24190_REG_POC,
556 BQ24190_REG_POC_CHG_CONFIG_MASK,
557 BQ24190_REG_POC_CHG_CONFIG_SHIFT, &val);
559 pm_runtime_mark_last_busy(bdi->dev);
560 pm_runtime_put_autosuspend(bdi->dev);
565 bdi->otg_vbus_enabled = (val == BQ24190_REG_POC_CHG_CONFIG_OTG ||
566 val == BQ24190_REG_POC_CHG_CONFIG_OTG_ALT);
567 return bdi->otg_vbus_enabled;
570 static const struct regulator_ops bq24190_vbus_ops = {
571 .enable = bq24190_vbus_enable,
572 .disable = bq24190_vbus_disable,
573 .is_enabled = bq24190_vbus_is_enabled,
576 static const struct regulator_desc bq24190_vbus_desc = {
577 .name = "usb_otg_vbus",
578 .of_match = "usb-otg-vbus",
579 .type = REGULATOR_VOLTAGE,
580 .owner = THIS_MODULE,
581 .ops = &bq24190_vbus_ops,
586 static const struct regulator_init_data bq24190_vbus_init_data = {
588 .valid_ops_mask = REGULATOR_CHANGE_STATUS,
592 static int bq24190_register_vbus_regulator(struct bq24190_dev_info *bdi)
594 struct bq24190_platform_data *pdata = bdi->dev->platform_data;
595 struct regulator_config cfg = { };
596 struct regulator_dev *reg;
600 if (pdata && pdata->regulator_init_data)
601 cfg.init_data = pdata->regulator_init_data;
603 cfg.init_data = &bq24190_vbus_init_data;
604 cfg.driver_data = bdi;
605 reg = devm_regulator_register(bdi->dev, &bq24190_vbus_desc, &cfg);
608 dev_err(bdi->dev, "Can't register regulator: %d\n", ret);
614 static int bq24190_register_vbus_regulator(struct bq24190_dev_info *bdi)
620 static int bq24190_set_config(struct bq24190_dev_info *bdi)
625 ret = bq24190_read(bdi, BQ24190_REG_CTTC, &v);
629 bdi->watchdog = ((v & BQ24190_REG_CTTC_WATCHDOG_MASK) >>
630 BQ24190_REG_CTTC_WATCHDOG_SHIFT);
633 * According to the "Host Mode and default Mode" section of the
634 * manual, a write to any register causes the bq24190 to switch
635 * from default mode to host mode. It will switch back to default
636 * mode after a WDT timeout unless the WDT is turned off as well.
637 * So, by simply turning off the WDT, we accomplish both with the
640 v &= ~BQ24190_REG_CTTC_WATCHDOG_MASK;
642 ret = bq24190_write(bdi, BQ24190_REG_CTTC, v);
647 v = bdi->sys_min / 100 - 30; // manual section 9.5.1.2, table 9
648 ret = bq24190_write_mask(bdi, BQ24190_REG_POC,
649 BQ24190_REG_POC_SYS_MIN_MASK,
650 BQ24190_REG_POC_SYS_MIN_SHIFT,
657 v = bdi->iprechg / 128 - 1; // manual section 9.5.1.4, table 11
658 ret = bq24190_write_mask(bdi, BQ24190_REG_PCTCC,
659 BQ24190_REG_PCTCC_IPRECHG_MASK,
660 BQ24190_REG_PCTCC_IPRECHG_SHIFT,
667 v = bdi->iterm / 128 - 1; // manual section 9.5.1.4, table 11
668 ret = bq24190_write_mask(bdi, BQ24190_REG_PCTCC,
669 BQ24190_REG_PCTCC_ITERM_MASK,
670 BQ24190_REG_PCTCC_ITERM_SHIFT,
677 ret = bq24190_set_field_val(bdi, BQ24190_REG_CCC,
678 BQ24190_REG_CCC_ICHG_MASK,
679 BQ24190_REG_CCC_ICHG_SHIFT,
680 bq24190_ccc_ichg_values,
681 ARRAY_SIZE(bq24190_ccc_ichg_values),
688 ret = bq24190_set_field_val(bdi, BQ24190_REG_CVC,
689 BQ24190_REG_CVC_VREG_MASK,
690 BQ24190_REG_CVC_VREG_SHIFT,
691 bq24190_cvc_vreg_values,
692 ARRAY_SIZE(bq24190_cvc_vreg_values),
701 static int bq24190_register_reset(struct bq24190_dev_info *bdi)
703 int ret, limit = 100;
707 * This prop. can be passed on device instantiation from platform code:
708 * struct property_entry pe[] =
709 * { PROPERTY_ENTRY_BOOL("disable-reset"), ... };
710 * struct i2c_board_info bi =
711 * { .type = "bq24190", .addr = 0x6b, .properties = pe, .irq = irq };
712 * struct i2c_adapter ad = { ... };
713 * i2c_add_adapter(&ad);
714 * i2c_new_client_device(&ad, &bi);
716 if (device_property_read_bool(bdi->dev, "disable-reset"))
719 /* Reset the registers */
720 ret = bq24190_write_mask(bdi, BQ24190_REG_POC,
721 BQ24190_REG_POC_RESET_MASK,
722 BQ24190_REG_POC_RESET_SHIFT,
727 /* Reset bit will be cleared by hardware so poll until it is */
729 ret = bq24190_read_mask(bdi, BQ24190_REG_POC,
730 BQ24190_REG_POC_RESET_MASK,
731 BQ24190_REG_POC_RESET_SHIFT,
739 usleep_range(100, 200);
745 /* Charger power supply property routines */
747 static int bq24190_charger_get_charge_type(struct bq24190_dev_info *bdi,
748 union power_supply_propval *val)
753 ret = bq24190_read_mask(bdi, BQ24190_REG_POC,
754 BQ24190_REG_POC_CHG_CONFIG_MASK,
755 BQ24190_REG_POC_CHG_CONFIG_SHIFT,
760 /* If POC[CHG_CONFIG] (REG01[5:4]) == 0, charge is disabled */
762 type = POWER_SUPPLY_CHARGE_TYPE_NONE;
764 ret = bq24190_read_mask(bdi, BQ24190_REG_CCC,
765 BQ24190_REG_CCC_FORCE_20PCT_MASK,
766 BQ24190_REG_CCC_FORCE_20PCT_SHIFT,
771 type = (v) ? POWER_SUPPLY_CHARGE_TYPE_TRICKLE :
772 POWER_SUPPLY_CHARGE_TYPE_FAST;
780 static int bq24190_charger_set_charge_type(struct bq24190_dev_info *bdi,
781 const union power_supply_propval *val)
783 u8 chg_config, force_20pct, en_term;
787 * According to the "Termination when REG02[0] = 1" section of
788 * the bq24190 manual, the trickle charge could be less than the
789 * termination current so it recommends turning off the termination
792 * Note: AFAICT from the datasheet, the user will have to manually
793 * turn off the charging when in 20% mode. If its not turned off,
794 * there could be battery damage. So, use this mode at your own risk.
796 switch (val->intval) {
797 case POWER_SUPPLY_CHARGE_TYPE_NONE:
800 case POWER_SUPPLY_CHARGE_TYPE_TRICKLE:
805 case POWER_SUPPLY_CHARGE_TYPE_FAST:
814 bdi->charge_type = val->intval;
816 * If the 5V Vbus boost regulator is enabled delay setting
817 * the charge-type until its gets disabled.
819 if (bdi->otg_vbus_enabled)
822 if (chg_config) { /* Enabling the charger */
823 ret = bq24190_write_mask(bdi, BQ24190_REG_CCC,
824 BQ24190_REG_CCC_FORCE_20PCT_MASK,
825 BQ24190_REG_CCC_FORCE_20PCT_SHIFT,
830 ret = bq24190_write_mask(bdi, BQ24190_REG_CTTC,
831 BQ24190_REG_CTTC_EN_TERM_MASK,
832 BQ24190_REG_CTTC_EN_TERM_SHIFT,
838 return bq24190_write_mask(bdi, BQ24190_REG_POC,
839 BQ24190_REG_POC_CHG_CONFIG_MASK,
840 BQ24190_REG_POC_CHG_CONFIG_SHIFT, chg_config);
843 static int bq24190_charger_get_health(struct bq24190_dev_info *bdi,
844 union power_supply_propval *val)
849 mutex_lock(&bdi->f_reg_lock);
851 mutex_unlock(&bdi->f_reg_lock);
853 if (v & BQ24190_REG_F_NTC_FAULT_MASK) {
854 switch (v >> BQ24190_REG_F_NTC_FAULT_SHIFT & 0x7) {
855 case 0x1: /* TS1 Cold */
856 case 0x3: /* TS2 Cold */
857 case 0x5: /* Both Cold */
858 health = POWER_SUPPLY_HEALTH_COLD;
860 case 0x2: /* TS1 Hot */
861 case 0x4: /* TS2 Hot */
862 case 0x6: /* Both Hot */
863 health = POWER_SUPPLY_HEALTH_OVERHEAT;
866 health = POWER_SUPPLY_HEALTH_UNKNOWN;
868 } else if (v & BQ24190_REG_F_BAT_FAULT_MASK) {
869 health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
870 } else if (v & BQ24190_REG_F_CHRG_FAULT_MASK) {
871 switch (v >> BQ24190_REG_F_CHRG_FAULT_SHIFT & 0x3) {
872 case 0x1: /* Input Fault (VBUS OVP or VBAT<VBUS<3.8V) */
874 * This could be over-voltage or under-voltage
875 * and there's no way to tell which. Instead
876 * of looking foolish and returning 'OVERVOLTAGE'
877 * when its really under-voltage, just return
880 health = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
882 case 0x2: /* Thermal Shutdown */
883 health = POWER_SUPPLY_HEALTH_OVERHEAT;
885 case 0x3: /* Charge Safety Timer Expiration */
886 health = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
888 default: /* prevent compiler warning */
891 } else if (v & BQ24190_REG_F_BOOST_FAULT_MASK) {
893 * This could be over-current or over-voltage but there's
894 * no way to tell which. Return 'OVERVOLTAGE' since there
895 * isn't an 'OVERCURRENT' value defined that we can return
896 * even if it was over-current.
898 health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
900 health = POWER_SUPPLY_HEALTH_GOOD;
903 val->intval = health;
908 static int bq24190_charger_get_online(struct bq24190_dev_info *bdi,
909 union power_supply_propval *val)
911 u8 pg_stat, batfet_disable;
914 ret = bq24190_read_mask(bdi, BQ24190_REG_SS,
915 BQ24190_REG_SS_PG_STAT_MASK,
916 BQ24190_REG_SS_PG_STAT_SHIFT, &pg_stat);
920 ret = bq24190_read_mask(bdi, BQ24190_REG_MOC,
921 BQ24190_REG_MOC_BATFET_DISABLE_MASK,
922 BQ24190_REG_MOC_BATFET_DISABLE_SHIFT, &batfet_disable);
926 val->intval = pg_stat && !batfet_disable;
931 static int bq24190_battery_set_online(struct bq24190_dev_info *bdi,
932 const union power_supply_propval *val);
933 static int bq24190_battery_get_status(struct bq24190_dev_info *bdi,
934 union power_supply_propval *val);
935 static int bq24190_battery_get_temp_alert_max(struct bq24190_dev_info *bdi,
936 union power_supply_propval *val);
937 static int bq24190_battery_set_temp_alert_max(struct bq24190_dev_info *bdi,
938 const union power_supply_propval *val);
940 static int bq24190_charger_set_online(struct bq24190_dev_info *bdi,
941 const union power_supply_propval *val)
943 return bq24190_battery_set_online(bdi, val);
946 static int bq24190_charger_get_status(struct bq24190_dev_info *bdi,
947 union power_supply_propval *val)
949 return bq24190_battery_get_status(bdi, val);
952 static int bq24190_charger_get_temp_alert_max(struct bq24190_dev_info *bdi,
953 union power_supply_propval *val)
955 return bq24190_battery_get_temp_alert_max(bdi, val);
958 static int bq24190_charger_set_temp_alert_max(struct bq24190_dev_info *bdi,
959 const union power_supply_propval *val)
961 return bq24190_battery_set_temp_alert_max(bdi, val);
964 static int bq24190_charger_get_precharge(struct bq24190_dev_info *bdi,
965 union power_supply_propval *val)
970 ret = bq24190_read_mask(bdi, BQ24190_REG_PCTCC,
971 BQ24190_REG_PCTCC_IPRECHG_MASK,
972 BQ24190_REG_PCTCC_IPRECHG_SHIFT, &v);
976 val->intval = ++v * 128 * 1000;
980 static int bq24190_charger_get_charge_term(struct bq24190_dev_info *bdi,
981 union power_supply_propval *val)
986 ret = bq24190_read_mask(bdi, BQ24190_REG_PCTCC,
987 BQ24190_REG_PCTCC_ITERM_MASK,
988 BQ24190_REG_PCTCC_ITERM_SHIFT, &v);
992 val->intval = ++v * 128 * 1000;
996 static int bq24190_charger_get_current(struct bq24190_dev_info *bdi,
997 union power_supply_propval *val)
1002 ret = bq24190_get_field_val(bdi, BQ24190_REG_CCC,
1003 BQ24190_REG_CCC_ICHG_MASK, BQ24190_REG_CCC_ICHG_SHIFT,
1004 bq24190_ccc_ichg_values,
1005 ARRAY_SIZE(bq24190_ccc_ichg_values), &curr);
1009 ret = bq24190_read_mask(bdi, BQ24190_REG_CCC,
1010 BQ24190_REG_CCC_FORCE_20PCT_MASK,
1011 BQ24190_REG_CCC_FORCE_20PCT_SHIFT, &v);
1015 /* If FORCE_20PCT is enabled, then current is 20% of ICHG value */
1023 static int bq24190_charger_set_current(struct bq24190_dev_info *bdi,
1024 const union power_supply_propval *val)
1027 int ret, curr = val->intval;
1029 ret = bq24190_read_mask(bdi, BQ24190_REG_CCC,
1030 BQ24190_REG_CCC_FORCE_20PCT_MASK,
1031 BQ24190_REG_CCC_FORCE_20PCT_SHIFT, &v);
1035 /* If FORCE_20PCT is enabled, have to multiply value passed in by 5 */
1039 if (curr > bdi->ichg_max)
1042 ret = bq24190_set_field_val(bdi, BQ24190_REG_CCC,
1043 BQ24190_REG_CCC_ICHG_MASK, BQ24190_REG_CCC_ICHG_SHIFT,
1044 bq24190_ccc_ichg_values,
1045 ARRAY_SIZE(bq24190_ccc_ichg_values), curr);
1054 static int bq24190_charger_get_voltage(struct bq24190_dev_info *bdi,
1055 union power_supply_propval *val)
1059 ret = bq24190_get_field_val(bdi, BQ24190_REG_CVC,
1060 BQ24190_REG_CVC_VREG_MASK, BQ24190_REG_CVC_VREG_SHIFT,
1061 bq24190_cvc_vreg_values,
1062 ARRAY_SIZE(bq24190_cvc_vreg_values), &voltage);
1066 val->intval = voltage;
1070 static int bq24190_charger_set_voltage(struct bq24190_dev_info *bdi,
1071 const union power_supply_propval *val)
1075 if (val->intval > bdi->vreg_max)
1078 ret = bq24190_set_field_val(bdi, BQ24190_REG_CVC,
1079 BQ24190_REG_CVC_VREG_MASK, BQ24190_REG_CVC_VREG_SHIFT,
1080 bq24190_cvc_vreg_values,
1081 ARRAY_SIZE(bq24190_cvc_vreg_values), val->intval);
1085 bdi->vreg = val->intval;
1090 static int bq24190_charger_get_iinlimit(struct bq24190_dev_info *bdi,
1091 union power_supply_propval *val)
1095 ret = bq24190_get_field_val(bdi, BQ24190_REG_ISC,
1096 BQ24190_REG_ISC_IINLIM_MASK,
1097 BQ24190_REG_ISC_IINLIM_SHIFT,
1098 bq24190_isc_iinlim_values,
1099 ARRAY_SIZE(bq24190_isc_iinlim_values), &iinlimit);
1103 val->intval = iinlimit;
1107 static int bq24190_charger_set_iinlimit(struct bq24190_dev_info *bdi,
1108 const union power_supply_propval *val)
1110 return bq24190_set_field_val(bdi, BQ24190_REG_ISC,
1111 BQ24190_REG_ISC_IINLIM_MASK,
1112 BQ24190_REG_ISC_IINLIM_SHIFT,
1113 bq24190_isc_iinlim_values,
1114 ARRAY_SIZE(bq24190_isc_iinlim_values), val->intval);
1117 static int bq24190_charger_get_property(struct power_supply *psy,
1118 enum power_supply_property psp, union power_supply_propval *val)
1120 struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy);
1123 dev_dbg(bdi->dev, "prop: %d\n", psp);
1125 ret = pm_runtime_resume_and_get(bdi->dev);
1130 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1131 ret = bq24190_charger_get_charge_type(bdi, val);
1133 case POWER_SUPPLY_PROP_HEALTH:
1134 ret = bq24190_charger_get_health(bdi, val);
1136 case POWER_SUPPLY_PROP_ONLINE:
1137 ret = bq24190_charger_get_online(bdi, val);
1139 case POWER_SUPPLY_PROP_STATUS:
1140 ret = bq24190_charger_get_status(bdi, val);
1142 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
1143 ret = bq24190_charger_get_temp_alert_max(bdi, val);
1145 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
1146 ret = bq24190_charger_get_precharge(bdi, val);
1148 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
1149 ret = bq24190_charger_get_charge_term(bdi, val);
1151 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
1152 ret = bq24190_charger_get_current(bdi, val);
1154 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
1155 val->intval = bdi->ichg_max;
1158 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
1159 ret = bq24190_charger_get_voltage(bdi, val);
1161 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
1162 val->intval = bdi->vreg_max;
1165 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
1166 ret = bq24190_charger_get_iinlimit(bdi, val);
1168 case POWER_SUPPLY_PROP_SCOPE:
1169 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
1172 case POWER_SUPPLY_PROP_MODEL_NAME:
1173 val->strval = bdi->model_name;
1176 case POWER_SUPPLY_PROP_MANUFACTURER:
1177 val->strval = BQ24190_MANUFACTURER;
1184 pm_runtime_mark_last_busy(bdi->dev);
1185 pm_runtime_put_autosuspend(bdi->dev);
1190 static int bq24190_charger_set_property(struct power_supply *psy,
1191 enum power_supply_property psp,
1192 const union power_supply_propval *val)
1194 struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy);
1197 dev_dbg(bdi->dev, "prop: %d\n", psp);
1199 ret = pm_runtime_resume_and_get(bdi->dev);
1204 case POWER_SUPPLY_PROP_ONLINE:
1205 ret = bq24190_charger_set_online(bdi, val);
1207 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
1208 ret = bq24190_charger_set_temp_alert_max(bdi, val);
1210 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1211 ret = bq24190_charger_set_charge_type(bdi, val);
1213 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
1214 ret = bq24190_charger_set_current(bdi, val);
1216 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
1217 ret = bq24190_charger_set_voltage(bdi, val);
1219 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
1220 ret = bq24190_charger_set_iinlimit(bdi, val);
1226 pm_runtime_mark_last_busy(bdi->dev);
1227 pm_runtime_put_autosuspend(bdi->dev);
1232 static int bq24190_charger_property_is_writeable(struct power_supply *psy,
1233 enum power_supply_property psp)
1236 case POWER_SUPPLY_PROP_ONLINE:
1237 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
1238 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1239 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
1240 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
1241 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
1248 static void bq24190_input_current_limit_work(struct work_struct *work)
1250 struct bq24190_dev_info *bdi =
1251 container_of(work, struct bq24190_dev_info,
1252 input_current_limit_work.work);
1253 union power_supply_propval val;
1256 ret = power_supply_get_property_from_supplier(bdi->charger,
1257 POWER_SUPPLY_PROP_CURRENT_MAX,
1262 bq24190_charger_set_property(bdi->charger,
1263 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
1267 /* Sync the input-current-limit with our parent supply (if we have one) */
1268 static void bq24190_charger_external_power_changed(struct power_supply *psy)
1270 struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy);
1273 * The Power-Good detection may take up to 220ms, sometimes
1274 * the external charger detection is quicker, and the bq24190 will
1275 * reset to iinlim based on its own charger detection (which is not
1276 * hooked up when using external charger detection) resulting in a
1277 * too low default 500mA iinlim. Delay setting the input-current-limit
1278 * for 300ms to avoid this.
1280 queue_delayed_work(system_wq, &bdi->input_current_limit_work,
1281 msecs_to_jiffies(300));
1284 static enum power_supply_property bq24190_charger_properties[] = {
1285 POWER_SUPPLY_PROP_CHARGE_TYPE,
1286 POWER_SUPPLY_PROP_HEALTH,
1287 POWER_SUPPLY_PROP_ONLINE,
1288 POWER_SUPPLY_PROP_STATUS,
1289 POWER_SUPPLY_PROP_TEMP_ALERT_MAX,
1290 POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
1291 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
1292 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
1293 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
1294 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
1295 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
1296 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
1297 POWER_SUPPLY_PROP_SCOPE,
1298 POWER_SUPPLY_PROP_MODEL_NAME,
1299 POWER_SUPPLY_PROP_MANUFACTURER,
1302 static char *bq24190_charger_supplied_to[] = {
1306 static const struct power_supply_desc bq24190_charger_desc = {
1307 .name = "bq24190-charger",
1308 .type = POWER_SUPPLY_TYPE_USB,
1309 .properties = bq24190_charger_properties,
1310 .num_properties = ARRAY_SIZE(bq24190_charger_properties),
1311 .get_property = bq24190_charger_get_property,
1312 .set_property = bq24190_charger_set_property,
1313 .property_is_writeable = bq24190_charger_property_is_writeable,
1314 .external_power_changed = bq24190_charger_external_power_changed,
1317 /* Battery power supply property routines */
1319 static int bq24190_battery_get_status(struct bq24190_dev_info *bdi,
1320 union power_supply_propval *val)
1322 u8 ss_reg, chrg_fault;
1325 mutex_lock(&bdi->f_reg_lock);
1326 chrg_fault = bdi->f_reg;
1327 mutex_unlock(&bdi->f_reg_lock);
1329 chrg_fault &= BQ24190_REG_F_CHRG_FAULT_MASK;
1330 chrg_fault >>= BQ24190_REG_F_CHRG_FAULT_SHIFT;
1332 ret = bq24190_read(bdi, BQ24190_REG_SS, &ss_reg);
1337 * The battery must be discharging when any of these are true:
1338 * - there is no good power source;
1339 * - there is a charge fault.
1340 * Could also be discharging when in "supplement mode" but
1341 * there is no way to tell when its in that mode.
1343 if (!(ss_reg & BQ24190_REG_SS_PG_STAT_MASK) || chrg_fault) {
1344 status = POWER_SUPPLY_STATUS_DISCHARGING;
1346 ss_reg &= BQ24190_REG_SS_CHRG_STAT_MASK;
1347 ss_reg >>= BQ24190_REG_SS_CHRG_STAT_SHIFT;
1350 case 0x0: /* Not Charging */
1351 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1353 case 0x1: /* Pre-charge */
1354 case 0x2: /* Fast Charging */
1355 status = POWER_SUPPLY_STATUS_CHARGING;
1357 case 0x3: /* Charge Termination Done */
1358 status = POWER_SUPPLY_STATUS_FULL;
1366 val->intval = status;
1371 static int bq24190_battery_get_health(struct bq24190_dev_info *bdi,
1372 union power_supply_propval *val)
1377 mutex_lock(&bdi->f_reg_lock);
1379 mutex_unlock(&bdi->f_reg_lock);
1381 if (v & BQ24190_REG_F_BAT_FAULT_MASK) {
1382 health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
1384 v &= BQ24190_REG_F_NTC_FAULT_MASK;
1385 v >>= BQ24190_REG_F_NTC_FAULT_SHIFT;
1388 case 0x0: /* Normal */
1389 health = POWER_SUPPLY_HEALTH_GOOD;
1391 case 0x1: /* TS1 Cold */
1392 case 0x3: /* TS2 Cold */
1393 case 0x5: /* Both Cold */
1394 health = POWER_SUPPLY_HEALTH_COLD;
1396 case 0x2: /* TS1 Hot */
1397 case 0x4: /* TS2 Hot */
1398 case 0x6: /* Both Hot */
1399 health = POWER_SUPPLY_HEALTH_OVERHEAT;
1402 health = POWER_SUPPLY_HEALTH_UNKNOWN;
1406 val->intval = health;
1410 static int bq24190_battery_get_online(struct bq24190_dev_info *bdi,
1411 union power_supply_propval *val)
1416 ret = bq24190_read_mask(bdi, BQ24190_REG_MOC,
1417 BQ24190_REG_MOC_BATFET_DISABLE_MASK,
1418 BQ24190_REG_MOC_BATFET_DISABLE_SHIFT, &batfet_disable);
1422 val->intval = !batfet_disable;
1426 static int bq24190_battery_set_online(struct bq24190_dev_info *bdi,
1427 const union power_supply_propval *val)
1429 return bq24190_write_mask(bdi, BQ24190_REG_MOC,
1430 BQ24190_REG_MOC_BATFET_DISABLE_MASK,
1431 BQ24190_REG_MOC_BATFET_DISABLE_SHIFT, !val->intval);
1434 static int bq24190_battery_get_temp_alert_max(struct bq24190_dev_info *bdi,
1435 union power_supply_propval *val)
1439 ret = bq24190_get_field_val(bdi, BQ24190_REG_ICTRC,
1440 BQ24190_REG_ICTRC_TREG_MASK,
1441 BQ24190_REG_ICTRC_TREG_SHIFT,
1442 bq24190_ictrc_treg_values,
1443 ARRAY_SIZE(bq24190_ictrc_treg_values), &temp);
1451 static int bq24190_battery_set_temp_alert_max(struct bq24190_dev_info *bdi,
1452 const union power_supply_propval *val)
1454 return bq24190_set_field_val(bdi, BQ24190_REG_ICTRC,
1455 BQ24190_REG_ICTRC_TREG_MASK,
1456 BQ24190_REG_ICTRC_TREG_SHIFT,
1457 bq24190_ictrc_treg_values,
1458 ARRAY_SIZE(bq24190_ictrc_treg_values), val->intval);
1461 static int bq24190_battery_get_property(struct power_supply *psy,
1462 enum power_supply_property psp, union power_supply_propval *val)
1464 struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy);
1467 dev_warn(bdi->dev, "warning: /sys/class/power_supply/bq24190-battery is deprecated\n");
1468 dev_dbg(bdi->dev, "prop: %d\n", psp);
1470 ret = pm_runtime_resume_and_get(bdi->dev);
1475 case POWER_SUPPLY_PROP_STATUS:
1476 ret = bq24190_battery_get_status(bdi, val);
1478 case POWER_SUPPLY_PROP_HEALTH:
1479 ret = bq24190_battery_get_health(bdi, val);
1481 case POWER_SUPPLY_PROP_ONLINE:
1482 ret = bq24190_battery_get_online(bdi, val);
1484 case POWER_SUPPLY_PROP_TECHNOLOGY:
1485 /* Could be Li-on or Li-polymer but no way to tell which */
1486 val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
1489 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
1490 ret = bq24190_battery_get_temp_alert_max(bdi, val);
1492 case POWER_SUPPLY_PROP_SCOPE:
1493 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
1500 pm_runtime_mark_last_busy(bdi->dev);
1501 pm_runtime_put_autosuspend(bdi->dev);
1506 static int bq24190_battery_set_property(struct power_supply *psy,
1507 enum power_supply_property psp,
1508 const union power_supply_propval *val)
1510 struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy);
1513 dev_warn(bdi->dev, "warning: /sys/class/power_supply/bq24190-battery is deprecated\n");
1514 dev_dbg(bdi->dev, "prop: %d\n", psp);
1516 ret = pm_runtime_resume_and_get(bdi->dev);
1521 case POWER_SUPPLY_PROP_ONLINE:
1522 ret = bq24190_battery_set_online(bdi, val);
1524 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
1525 ret = bq24190_battery_set_temp_alert_max(bdi, val);
1531 pm_runtime_mark_last_busy(bdi->dev);
1532 pm_runtime_put_autosuspend(bdi->dev);
1537 static int bq24190_battery_property_is_writeable(struct power_supply *psy,
1538 enum power_supply_property psp)
1543 case POWER_SUPPLY_PROP_ONLINE:
1544 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
1554 static enum power_supply_property bq24190_battery_properties[] = {
1555 POWER_SUPPLY_PROP_STATUS,
1556 POWER_SUPPLY_PROP_HEALTH,
1557 POWER_SUPPLY_PROP_ONLINE,
1558 POWER_SUPPLY_PROP_TECHNOLOGY,
1559 POWER_SUPPLY_PROP_TEMP_ALERT_MAX,
1560 POWER_SUPPLY_PROP_SCOPE,
1563 static const struct power_supply_desc bq24190_battery_desc = {
1564 .name = "bq24190-battery",
1565 .type = POWER_SUPPLY_TYPE_BATTERY,
1566 .properties = bq24190_battery_properties,
1567 .num_properties = ARRAY_SIZE(bq24190_battery_properties),
1568 .get_property = bq24190_battery_get_property,
1569 .set_property = bq24190_battery_set_property,
1570 .property_is_writeable = bq24190_battery_property_is_writeable,
1573 static int bq24190_configure_usb_otg(struct bq24190_dev_info *bdi, u8 ss_reg)
1578 otg_enabled = !!(ss_reg & BQ24190_REG_SS_VBUS_STAT_MASK);
1579 ret = extcon_set_state_sync(bdi->edev, EXTCON_USB, otg_enabled);
1581 dev_err(bdi->dev, "Can't set extcon state to %d: %d\n",
1587 static void bq24190_check_status(struct bq24190_dev_info *bdi)
1589 const u8 battery_mask_ss = BQ24190_REG_SS_CHRG_STAT_MASK;
1590 const u8 battery_mask_f = BQ24190_REG_F_BAT_FAULT_MASK
1591 | BQ24190_REG_F_NTC_FAULT_MASK;
1592 bool alert_charger = false, alert_battery = false;
1593 u8 ss_reg = 0, f_reg = 0;
1596 ret = bq24190_read(bdi, BQ24190_REG_SS, &ss_reg);
1598 dev_err(bdi->dev, "Can't read SS reg: %d\n", ret);
1604 ret = bq24190_read(bdi, BQ24190_REG_F, &f_reg);
1606 dev_err(bdi->dev, "Can't read F reg: %d\n", ret);
1609 } while (f_reg && ++i < 2);
1611 /* ignore over/under voltage fault after disconnect */
1612 if (f_reg == (1 << BQ24190_REG_F_CHRG_FAULT_SHIFT) &&
1613 !(ss_reg & BQ24190_REG_SS_PG_STAT_MASK))
1616 if (f_reg != bdi->f_reg) {
1618 "Fault: boost %d, charge %d, battery %d, ntc %d\n",
1619 !!(f_reg & BQ24190_REG_F_BOOST_FAULT_MASK),
1620 !!(f_reg & BQ24190_REG_F_CHRG_FAULT_MASK),
1621 !!(f_reg & BQ24190_REG_F_BAT_FAULT_MASK),
1622 !!(f_reg & BQ24190_REG_F_NTC_FAULT_MASK));
1624 mutex_lock(&bdi->f_reg_lock);
1625 if ((bdi->f_reg & battery_mask_f) != (f_reg & battery_mask_f))
1626 alert_battery = true;
1627 if ((bdi->f_reg & ~battery_mask_f) != (f_reg & ~battery_mask_f))
1628 alert_charger = true;
1630 mutex_unlock(&bdi->f_reg_lock);
1633 if (ss_reg != bdi->ss_reg) {
1635 * The device is in host mode so when PG_STAT goes from 1->0
1636 * (i.e., power removed) HIZ needs to be disabled.
1638 if ((bdi->ss_reg & BQ24190_REG_SS_PG_STAT_MASK) &&
1639 !(ss_reg & BQ24190_REG_SS_PG_STAT_MASK)) {
1640 ret = bq24190_write_mask(bdi, BQ24190_REG_ISC,
1641 BQ24190_REG_ISC_EN_HIZ_MASK,
1642 BQ24190_REG_ISC_EN_HIZ_SHIFT,
1645 dev_err(bdi->dev, "Can't access ISC reg: %d\n",
1649 if ((bdi->ss_reg & battery_mask_ss) != (ss_reg & battery_mask_ss))
1650 alert_battery = true;
1651 if ((bdi->ss_reg & ~battery_mask_ss) != (ss_reg & ~battery_mask_ss))
1652 alert_charger = true;
1653 bdi->ss_reg = ss_reg;
1656 if (alert_charger || alert_battery) {
1657 power_supply_changed(bdi->charger);
1658 bq24190_configure_usb_otg(bdi, ss_reg);
1660 if (alert_battery && bdi->battery)
1661 power_supply_changed(bdi->battery);
1663 dev_dbg(bdi->dev, "ss_reg: 0x%02x, f_reg: 0x%02x\n", ss_reg, f_reg);
1666 static irqreturn_t bq24190_irq_handler_thread(int irq, void *data)
1668 struct bq24190_dev_info *bdi = data;
1671 bdi->irq_event = true;
1672 error = pm_runtime_resume_and_get(bdi->dev);
1674 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", error);
1677 bq24190_check_status(bdi);
1678 pm_runtime_mark_last_busy(bdi->dev);
1679 pm_runtime_put_autosuspend(bdi->dev);
1680 bdi->irq_event = false;
1685 static int bq24190_hw_init(struct bq24190_dev_info *bdi)
1690 /* First check that the device really is what its supposed to be */
1691 ret = bq24190_read_mask(bdi, BQ24190_REG_VPRS,
1692 BQ24190_REG_VPRS_PN_MASK,
1693 BQ24190_REG_VPRS_PN_SHIFT,
1699 case BQ24190_REG_VPRS_PN_24190:
1700 case BQ24190_REG_VPRS_PN_24192:
1701 case BQ24190_REG_VPRS_PN_24192I:
1704 dev_err(bdi->dev, "Error unknown model: 0x%02x\n", v);
1708 ret = bq24190_register_reset(bdi);
1712 ret = bq24190_set_config(bdi);
1716 return bq24190_read(bdi, BQ24190_REG_SS, &bdi->ss_reg);
1719 static int bq24190_get_config(struct bq24190_dev_info *bdi)
1721 const char * const s = "ti,system-minimum-microvolt";
1722 struct power_supply_battery_info *info;
1725 idx = ARRAY_SIZE(bq24190_ccc_ichg_values) - 1;
1726 bdi->ichg_max = bq24190_ccc_ichg_values[idx];
1728 idx = ARRAY_SIZE(bq24190_cvc_vreg_values) - 1;
1729 bdi->vreg_max = bq24190_cvc_vreg_values[idx];
1731 if (device_property_read_u32(bdi->dev, s, &v) == 0) {
1733 if (v >= BQ24190_REG_POC_SYS_MIN_MIN
1734 && v <= BQ24190_REG_POC_SYS_MIN_MAX)
1737 dev_warn(bdi->dev, "invalid value for %s: %u\n", s, v);
1740 if (!power_supply_get_battery_info(bdi->charger, &info)) {
1741 v = info->precharge_current_ua / 1000;
1742 if (v >= BQ24190_REG_PCTCC_IPRECHG_MIN
1743 && v <= BQ24190_REG_PCTCC_IPRECHG_MAX)
1746 dev_warn(bdi->dev, "invalid value for battery:precharge-current-microamp: %d\n",
1749 v = info->charge_term_current_ua / 1000;
1750 if (v >= BQ24190_REG_PCTCC_ITERM_MIN
1751 && v <= BQ24190_REG_PCTCC_ITERM_MAX)
1754 dev_warn(bdi->dev, "invalid value for battery:charge-term-current-microamp: %d\n",
1757 /* These are optional, so no warning when not set */
1758 v = info->constant_charge_current_max_ua;
1759 if (v >= bq24190_ccc_ichg_values[0] && v <= bdi->ichg_max)
1760 bdi->ichg = bdi->ichg_max = v;
1762 v = info->constant_charge_voltage_max_uv;
1763 if (v >= bq24190_cvc_vreg_values[0] && v <= bdi->vreg_max)
1764 bdi->vreg = bdi->vreg_max = v;
1770 static int bq24190_probe(struct i2c_client *client,
1771 const struct i2c_device_id *id)
1773 struct i2c_adapter *adapter = client->adapter;
1774 struct device *dev = &client->dev;
1775 struct power_supply_config charger_cfg = {}, battery_cfg = {};
1776 struct bq24190_dev_info *bdi;
1779 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1780 dev_err(dev, "No support for SMBUS_BYTE_DATA\n");
1784 bdi = devm_kzalloc(dev, sizeof(*bdi), GFP_KERNEL);
1786 dev_err(dev, "Can't alloc bdi struct\n");
1790 bdi->client = client;
1792 strncpy(bdi->model_name, id->name, I2C_NAME_SIZE);
1793 mutex_init(&bdi->f_reg_lock);
1794 bdi->charge_type = POWER_SUPPLY_CHARGE_TYPE_FAST;
1796 bdi->ss_reg = BQ24190_REG_SS_VBUS_STAT_MASK; /* impossible state */
1797 INIT_DELAYED_WORK(&bdi->input_current_limit_work,
1798 bq24190_input_current_limit_work);
1800 i2c_set_clientdata(client, bdi);
1802 if (client->irq <= 0) {
1803 dev_err(dev, "Can't get irq info\n");
1807 bdi->edev = devm_extcon_dev_allocate(dev, bq24190_usb_extcon_cable);
1808 if (IS_ERR(bdi->edev))
1809 return PTR_ERR(bdi->edev);
1811 ret = devm_extcon_dev_register(dev, bdi->edev);
1815 pm_runtime_enable(dev);
1816 pm_runtime_use_autosuspend(dev);
1817 pm_runtime_set_autosuspend_delay(dev, 600);
1818 ret = pm_runtime_get_sync(dev);
1820 dev_err(dev, "pm_runtime_get failed: %i\n", ret);
1825 bq24190_sysfs_init_attrs();
1826 charger_cfg.attr_grp = bq24190_sysfs_groups;
1829 charger_cfg.drv_data = bdi;
1830 charger_cfg.of_node = dev->of_node;
1831 charger_cfg.supplied_to = bq24190_charger_supplied_to;
1832 charger_cfg.num_supplicants = ARRAY_SIZE(bq24190_charger_supplied_to);
1833 bdi->charger = power_supply_register(dev, &bq24190_charger_desc,
1835 if (IS_ERR(bdi->charger)) {
1836 dev_err(dev, "Can't register charger\n");
1837 ret = PTR_ERR(bdi->charger);
1841 /* the battery class is deprecated and will be removed. */
1842 /* in the interim, this property hides it. */
1843 if (!device_property_read_bool(dev, "omit-battery-class")) {
1844 battery_cfg.drv_data = bdi;
1845 bdi->battery = power_supply_register(dev, &bq24190_battery_desc,
1847 if (IS_ERR(bdi->battery)) {
1848 dev_err(dev, "Can't register battery\n");
1849 ret = PTR_ERR(bdi->battery);
1854 ret = bq24190_get_config(bdi);
1856 dev_err(dev, "Can't get devicetree config\n");
1860 ret = bq24190_hw_init(bdi);
1862 dev_err(dev, "Hardware init failed\n");
1866 ret = bq24190_configure_usb_otg(bdi, bdi->ss_reg);
1870 bdi->initialized = true;
1872 ret = devm_request_threaded_irq(dev, client->irq, NULL,
1873 bq24190_irq_handler_thread,
1874 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1875 "bq24190-charger", bdi);
1877 dev_err(dev, "Can't set up irq handler\n");
1881 ret = bq24190_register_vbus_regulator(bdi);
1885 enable_irq_wake(client->irq);
1887 pm_runtime_mark_last_busy(dev);
1888 pm_runtime_put_autosuspend(dev);
1893 if (!IS_ERR_OR_NULL(bdi->battery))
1894 power_supply_unregister(bdi->battery);
1895 power_supply_unregister(bdi->charger);
1898 pm_runtime_put_sync(dev);
1899 pm_runtime_dont_use_autosuspend(dev);
1900 pm_runtime_disable(dev);
1904 static void bq24190_remove(struct i2c_client *client)
1906 struct bq24190_dev_info *bdi = i2c_get_clientdata(client);
1909 error = pm_runtime_resume_and_get(bdi->dev);
1911 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", error);
1913 bq24190_register_reset(bdi);
1915 power_supply_unregister(bdi->battery);
1916 power_supply_unregister(bdi->charger);
1918 pm_runtime_put_sync(bdi->dev);
1919 pm_runtime_dont_use_autosuspend(bdi->dev);
1920 pm_runtime_disable(bdi->dev);
1923 static void bq24190_shutdown(struct i2c_client *client)
1925 struct bq24190_dev_info *bdi = i2c_get_clientdata(client);
1927 /* Turn off 5V boost regulator on shutdown */
1928 bq24190_set_otg_vbus(bdi, false);
1931 static __maybe_unused int bq24190_runtime_suspend(struct device *dev)
1933 struct i2c_client *client = to_i2c_client(dev);
1934 struct bq24190_dev_info *bdi = i2c_get_clientdata(client);
1936 if (!bdi->initialized)
1939 dev_dbg(bdi->dev, "%s\n", __func__);
1944 static __maybe_unused int bq24190_runtime_resume(struct device *dev)
1946 struct i2c_client *client = to_i2c_client(dev);
1947 struct bq24190_dev_info *bdi = i2c_get_clientdata(client);
1949 if (!bdi->initialized)
1952 if (!bdi->irq_event) {
1953 dev_dbg(bdi->dev, "checking events on possible wakeirq\n");
1954 bq24190_check_status(bdi);
1960 static __maybe_unused int bq24190_pm_suspend(struct device *dev)
1962 struct i2c_client *client = to_i2c_client(dev);
1963 struct bq24190_dev_info *bdi = i2c_get_clientdata(client);
1966 error = pm_runtime_resume_and_get(bdi->dev);
1968 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", error);
1970 bq24190_register_reset(bdi);
1973 pm_runtime_mark_last_busy(bdi->dev);
1974 pm_runtime_put_autosuspend(bdi->dev);
1980 static __maybe_unused int bq24190_pm_resume(struct device *dev)
1982 struct i2c_client *client = to_i2c_client(dev);
1983 struct bq24190_dev_info *bdi = i2c_get_clientdata(client);
1987 bdi->ss_reg = BQ24190_REG_SS_VBUS_STAT_MASK; /* impossible state */
1989 error = pm_runtime_resume_and_get(bdi->dev);
1991 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", error);
1993 bq24190_register_reset(bdi);
1994 bq24190_set_config(bdi);
1995 bq24190_read(bdi, BQ24190_REG_SS, &bdi->ss_reg);
1998 pm_runtime_mark_last_busy(bdi->dev);
1999 pm_runtime_put_autosuspend(bdi->dev);
2002 /* Things may have changed while suspended so alert upper layer */
2003 power_supply_changed(bdi->charger);
2005 power_supply_changed(bdi->battery);
2010 static const struct dev_pm_ops bq24190_pm_ops = {
2011 SET_RUNTIME_PM_OPS(bq24190_runtime_suspend, bq24190_runtime_resume,
2013 SET_SYSTEM_SLEEP_PM_OPS(bq24190_pm_suspend, bq24190_pm_resume)
2016 static const struct i2c_device_id bq24190_i2c_ids[] = {
2023 MODULE_DEVICE_TABLE(i2c, bq24190_i2c_ids);
2025 static const struct of_device_id bq24190_of_match[] = {
2026 { .compatible = "ti,bq24190", },
2027 { .compatible = "ti,bq24192", },
2028 { .compatible = "ti,bq24192i", },
2029 { .compatible = "ti,bq24196", },
2032 MODULE_DEVICE_TABLE(of, bq24190_of_match);
2034 static struct i2c_driver bq24190_driver = {
2035 .probe = bq24190_probe,
2036 .remove = bq24190_remove,
2037 .shutdown = bq24190_shutdown,
2038 .id_table = bq24190_i2c_ids,
2040 .name = "bq24190-charger",
2041 .pm = &bq24190_pm_ops,
2042 .of_match_table = bq24190_of_match,
2045 module_i2c_driver(bq24190_driver);
2047 MODULE_LICENSE("GPL");
2048 MODULE_AUTHOR("Mark A. Greer <mgreer@animalcreek.com>");
2049 MODULE_DESCRIPTION("TI BQ24190 Charger Driver");