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 = sysfs_emit(buf, "%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 curr = ++v * 128 * 1000;
978 ret = bq24190_read_mask(bdi, BQ24190_REG_CCC,
979 BQ24190_REG_CCC_FORCE_20PCT_MASK,
980 BQ24190_REG_CCC_FORCE_20PCT_SHIFT, &v);
984 /* If FORCE_20PCT is enabled, then current is 50% of IPRECHG value */
993 static int bq24190_charger_get_charge_term(struct bq24190_dev_info *bdi,
994 union power_supply_propval *val)
999 ret = bq24190_read_mask(bdi, BQ24190_REG_PCTCC,
1000 BQ24190_REG_PCTCC_ITERM_MASK,
1001 BQ24190_REG_PCTCC_ITERM_SHIFT, &v);
1005 val->intval = ++v * 128 * 1000;
1009 static int bq24190_charger_get_current(struct bq24190_dev_info *bdi,
1010 union power_supply_propval *val)
1015 ret = bq24190_get_field_val(bdi, BQ24190_REG_CCC,
1016 BQ24190_REG_CCC_ICHG_MASK, BQ24190_REG_CCC_ICHG_SHIFT,
1017 bq24190_ccc_ichg_values,
1018 ARRAY_SIZE(bq24190_ccc_ichg_values), &curr);
1022 ret = bq24190_read_mask(bdi, BQ24190_REG_CCC,
1023 BQ24190_REG_CCC_FORCE_20PCT_MASK,
1024 BQ24190_REG_CCC_FORCE_20PCT_SHIFT, &v);
1028 /* If FORCE_20PCT is enabled, then current is 20% of ICHG value */
1036 static int bq24190_charger_set_current(struct bq24190_dev_info *bdi,
1037 const union power_supply_propval *val)
1040 int ret, curr = val->intval;
1042 ret = bq24190_read_mask(bdi, BQ24190_REG_CCC,
1043 BQ24190_REG_CCC_FORCE_20PCT_MASK,
1044 BQ24190_REG_CCC_FORCE_20PCT_SHIFT, &v);
1048 /* If FORCE_20PCT is enabled, have to multiply value passed in by 5 */
1052 if (curr > bdi->ichg_max)
1055 ret = bq24190_set_field_val(bdi, BQ24190_REG_CCC,
1056 BQ24190_REG_CCC_ICHG_MASK, BQ24190_REG_CCC_ICHG_SHIFT,
1057 bq24190_ccc_ichg_values,
1058 ARRAY_SIZE(bq24190_ccc_ichg_values), curr);
1067 static int bq24190_charger_get_voltage(struct bq24190_dev_info *bdi,
1068 union power_supply_propval *val)
1072 ret = bq24190_get_field_val(bdi, BQ24190_REG_CVC,
1073 BQ24190_REG_CVC_VREG_MASK, BQ24190_REG_CVC_VREG_SHIFT,
1074 bq24190_cvc_vreg_values,
1075 ARRAY_SIZE(bq24190_cvc_vreg_values), &voltage);
1079 val->intval = voltage;
1083 static int bq24190_charger_set_voltage(struct bq24190_dev_info *bdi,
1084 const union power_supply_propval *val)
1088 if (val->intval > bdi->vreg_max)
1091 ret = bq24190_set_field_val(bdi, BQ24190_REG_CVC,
1092 BQ24190_REG_CVC_VREG_MASK, BQ24190_REG_CVC_VREG_SHIFT,
1093 bq24190_cvc_vreg_values,
1094 ARRAY_SIZE(bq24190_cvc_vreg_values), val->intval);
1098 bdi->vreg = val->intval;
1103 static int bq24190_charger_get_iinlimit(struct bq24190_dev_info *bdi,
1104 union power_supply_propval *val)
1108 ret = bq24190_get_field_val(bdi, BQ24190_REG_ISC,
1109 BQ24190_REG_ISC_IINLIM_MASK,
1110 BQ24190_REG_ISC_IINLIM_SHIFT,
1111 bq24190_isc_iinlim_values,
1112 ARRAY_SIZE(bq24190_isc_iinlim_values), &iinlimit);
1116 val->intval = iinlimit;
1120 static int bq24190_charger_set_iinlimit(struct bq24190_dev_info *bdi,
1121 const union power_supply_propval *val)
1123 return bq24190_set_field_val(bdi, BQ24190_REG_ISC,
1124 BQ24190_REG_ISC_IINLIM_MASK,
1125 BQ24190_REG_ISC_IINLIM_SHIFT,
1126 bq24190_isc_iinlim_values,
1127 ARRAY_SIZE(bq24190_isc_iinlim_values), val->intval);
1130 static int bq24190_charger_get_property(struct power_supply *psy,
1131 enum power_supply_property psp, union power_supply_propval *val)
1133 struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy);
1136 dev_dbg(bdi->dev, "prop: %d\n", psp);
1138 ret = pm_runtime_resume_and_get(bdi->dev);
1143 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1144 ret = bq24190_charger_get_charge_type(bdi, val);
1146 case POWER_SUPPLY_PROP_HEALTH:
1147 ret = bq24190_charger_get_health(bdi, val);
1149 case POWER_SUPPLY_PROP_ONLINE:
1150 ret = bq24190_charger_get_online(bdi, val);
1152 case POWER_SUPPLY_PROP_STATUS:
1153 ret = bq24190_charger_get_status(bdi, val);
1155 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
1156 ret = bq24190_charger_get_temp_alert_max(bdi, val);
1158 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
1159 ret = bq24190_charger_get_precharge(bdi, val);
1161 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
1162 ret = bq24190_charger_get_charge_term(bdi, val);
1164 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
1165 ret = bq24190_charger_get_current(bdi, val);
1167 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
1168 val->intval = bdi->ichg_max;
1171 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
1172 ret = bq24190_charger_get_voltage(bdi, val);
1174 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
1175 val->intval = bdi->vreg_max;
1178 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
1179 ret = bq24190_charger_get_iinlimit(bdi, val);
1181 case POWER_SUPPLY_PROP_SCOPE:
1182 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
1185 case POWER_SUPPLY_PROP_MODEL_NAME:
1186 val->strval = bdi->model_name;
1189 case POWER_SUPPLY_PROP_MANUFACTURER:
1190 val->strval = BQ24190_MANUFACTURER;
1197 pm_runtime_mark_last_busy(bdi->dev);
1198 pm_runtime_put_autosuspend(bdi->dev);
1203 static int bq24190_charger_set_property(struct power_supply *psy,
1204 enum power_supply_property psp,
1205 const union power_supply_propval *val)
1207 struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy);
1210 dev_dbg(bdi->dev, "prop: %d\n", psp);
1212 ret = pm_runtime_resume_and_get(bdi->dev);
1217 case POWER_SUPPLY_PROP_ONLINE:
1218 ret = bq24190_charger_set_online(bdi, val);
1220 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
1221 ret = bq24190_charger_set_temp_alert_max(bdi, val);
1223 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1224 ret = bq24190_charger_set_charge_type(bdi, val);
1226 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
1227 ret = bq24190_charger_set_current(bdi, val);
1229 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
1230 ret = bq24190_charger_set_voltage(bdi, val);
1232 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
1233 ret = bq24190_charger_set_iinlimit(bdi, val);
1239 pm_runtime_mark_last_busy(bdi->dev);
1240 pm_runtime_put_autosuspend(bdi->dev);
1245 static int bq24190_charger_property_is_writeable(struct power_supply *psy,
1246 enum power_supply_property psp)
1249 case POWER_SUPPLY_PROP_ONLINE:
1250 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
1251 case POWER_SUPPLY_PROP_CHARGE_TYPE:
1252 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
1253 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
1254 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
1261 static void bq24190_input_current_limit_work(struct work_struct *work)
1263 struct bq24190_dev_info *bdi =
1264 container_of(work, struct bq24190_dev_info,
1265 input_current_limit_work.work);
1266 union power_supply_propval val;
1269 ret = power_supply_get_property_from_supplier(bdi->charger,
1270 POWER_SUPPLY_PROP_CURRENT_MAX,
1275 bq24190_charger_set_property(bdi->charger,
1276 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
1278 power_supply_changed(bdi->charger);
1281 /* Sync the input-current-limit with our parent supply (if we have one) */
1282 static void bq24190_charger_external_power_changed(struct power_supply *psy)
1284 struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy);
1287 * The Power-Good detection may take up to 220ms, sometimes
1288 * the external charger detection is quicker, and the bq24190 will
1289 * reset to iinlim based on its own charger detection (which is not
1290 * hooked up when using external charger detection) resulting in a
1291 * too low default 500mA iinlim. Delay setting the input-current-limit
1292 * for 300ms to avoid this.
1294 queue_delayed_work(system_wq, &bdi->input_current_limit_work,
1295 msecs_to_jiffies(300));
1298 static enum power_supply_property bq24190_charger_properties[] = {
1299 POWER_SUPPLY_PROP_CHARGE_TYPE,
1300 POWER_SUPPLY_PROP_HEALTH,
1301 POWER_SUPPLY_PROP_ONLINE,
1302 POWER_SUPPLY_PROP_STATUS,
1303 POWER_SUPPLY_PROP_TEMP_ALERT_MAX,
1304 POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
1305 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
1306 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
1307 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
1308 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
1309 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
1310 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
1311 POWER_SUPPLY_PROP_SCOPE,
1312 POWER_SUPPLY_PROP_MODEL_NAME,
1313 POWER_SUPPLY_PROP_MANUFACTURER,
1316 static char *bq24190_charger_supplied_to[] = {
1320 static const struct power_supply_desc bq24190_charger_desc = {
1321 .name = "bq24190-charger",
1322 .type = POWER_SUPPLY_TYPE_USB,
1323 .properties = bq24190_charger_properties,
1324 .num_properties = ARRAY_SIZE(bq24190_charger_properties),
1325 .get_property = bq24190_charger_get_property,
1326 .set_property = bq24190_charger_set_property,
1327 .property_is_writeable = bq24190_charger_property_is_writeable,
1328 .external_power_changed = bq24190_charger_external_power_changed,
1331 /* Battery power supply property routines */
1333 static int bq24190_battery_get_status(struct bq24190_dev_info *bdi,
1334 union power_supply_propval *val)
1336 u8 ss_reg, chrg_fault;
1339 mutex_lock(&bdi->f_reg_lock);
1340 chrg_fault = bdi->f_reg;
1341 mutex_unlock(&bdi->f_reg_lock);
1343 chrg_fault &= BQ24190_REG_F_CHRG_FAULT_MASK;
1344 chrg_fault >>= BQ24190_REG_F_CHRG_FAULT_SHIFT;
1346 ret = bq24190_read(bdi, BQ24190_REG_SS, &ss_reg);
1351 * The battery must be discharging when any of these are true:
1352 * - there is no good power source;
1353 * - there is a charge fault.
1354 * Could also be discharging when in "supplement mode" but
1355 * there is no way to tell when its in that mode.
1357 if (!(ss_reg & BQ24190_REG_SS_PG_STAT_MASK) || chrg_fault) {
1358 status = POWER_SUPPLY_STATUS_DISCHARGING;
1360 ss_reg &= BQ24190_REG_SS_CHRG_STAT_MASK;
1361 ss_reg >>= BQ24190_REG_SS_CHRG_STAT_SHIFT;
1364 case 0x0: /* Not Charging */
1365 status = POWER_SUPPLY_STATUS_NOT_CHARGING;
1367 case 0x1: /* Pre-charge */
1368 case 0x2: /* Fast Charging */
1369 status = POWER_SUPPLY_STATUS_CHARGING;
1371 case 0x3: /* Charge Termination Done */
1372 status = POWER_SUPPLY_STATUS_FULL;
1380 val->intval = status;
1385 static int bq24190_battery_get_health(struct bq24190_dev_info *bdi,
1386 union power_supply_propval *val)
1391 mutex_lock(&bdi->f_reg_lock);
1393 mutex_unlock(&bdi->f_reg_lock);
1395 if (v & BQ24190_REG_F_BAT_FAULT_MASK) {
1396 health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
1398 v &= BQ24190_REG_F_NTC_FAULT_MASK;
1399 v >>= BQ24190_REG_F_NTC_FAULT_SHIFT;
1402 case 0x0: /* Normal */
1403 health = POWER_SUPPLY_HEALTH_GOOD;
1405 case 0x1: /* TS1 Cold */
1406 case 0x3: /* TS2 Cold */
1407 case 0x5: /* Both Cold */
1408 health = POWER_SUPPLY_HEALTH_COLD;
1410 case 0x2: /* TS1 Hot */
1411 case 0x4: /* TS2 Hot */
1412 case 0x6: /* Both Hot */
1413 health = POWER_SUPPLY_HEALTH_OVERHEAT;
1416 health = POWER_SUPPLY_HEALTH_UNKNOWN;
1420 val->intval = health;
1424 static int bq24190_battery_get_online(struct bq24190_dev_info *bdi,
1425 union power_supply_propval *val)
1430 ret = bq24190_read_mask(bdi, BQ24190_REG_MOC,
1431 BQ24190_REG_MOC_BATFET_DISABLE_MASK,
1432 BQ24190_REG_MOC_BATFET_DISABLE_SHIFT, &batfet_disable);
1436 val->intval = !batfet_disable;
1440 static int bq24190_battery_set_online(struct bq24190_dev_info *bdi,
1441 const union power_supply_propval *val)
1443 return bq24190_write_mask(bdi, BQ24190_REG_MOC,
1444 BQ24190_REG_MOC_BATFET_DISABLE_MASK,
1445 BQ24190_REG_MOC_BATFET_DISABLE_SHIFT, !val->intval);
1448 static int bq24190_battery_get_temp_alert_max(struct bq24190_dev_info *bdi,
1449 union power_supply_propval *val)
1453 ret = bq24190_get_field_val(bdi, BQ24190_REG_ICTRC,
1454 BQ24190_REG_ICTRC_TREG_MASK,
1455 BQ24190_REG_ICTRC_TREG_SHIFT,
1456 bq24190_ictrc_treg_values,
1457 ARRAY_SIZE(bq24190_ictrc_treg_values), &temp);
1465 static int bq24190_battery_set_temp_alert_max(struct bq24190_dev_info *bdi,
1466 const union power_supply_propval *val)
1468 return bq24190_set_field_val(bdi, BQ24190_REG_ICTRC,
1469 BQ24190_REG_ICTRC_TREG_MASK,
1470 BQ24190_REG_ICTRC_TREG_SHIFT,
1471 bq24190_ictrc_treg_values,
1472 ARRAY_SIZE(bq24190_ictrc_treg_values), val->intval);
1475 static int bq24190_battery_get_property(struct power_supply *psy,
1476 enum power_supply_property psp, union power_supply_propval *val)
1478 struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy);
1481 dev_warn(bdi->dev, "warning: /sys/class/power_supply/bq24190-battery is deprecated\n");
1482 dev_dbg(bdi->dev, "prop: %d\n", psp);
1484 ret = pm_runtime_resume_and_get(bdi->dev);
1489 case POWER_SUPPLY_PROP_STATUS:
1490 ret = bq24190_battery_get_status(bdi, val);
1492 case POWER_SUPPLY_PROP_HEALTH:
1493 ret = bq24190_battery_get_health(bdi, val);
1495 case POWER_SUPPLY_PROP_ONLINE:
1496 ret = bq24190_battery_get_online(bdi, val);
1498 case POWER_SUPPLY_PROP_TECHNOLOGY:
1499 /* Could be Li-on or Li-polymer but no way to tell which */
1500 val->intval = POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
1503 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
1504 ret = bq24190_battery_get_temp_alert_max(bdi, val);
1506 case POWER_SUPPLY_PROP_SCOPE:
1507 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
1514 pm_runtime_mark_last_busy(bdi->dev);
1515 pm_runtime_put_autosuspend(bdi->dev);
1520 static int bq24190_battery_set_property(struct power_supply *psy,
1521 enum power_supply_property psp,
1522 const union power_supply_propval *val)
1524 struct bq24190_dev_info *bdi = power_supply_get_drvdata(psy);
1527 dev_warn(bdi->dev, "warning: /sys/class/power_supply/bq24190-battery is deprecated\n");
1528 dev_dbg(bdi->dev, "prop: %d\n", psp);
1530 ret = pm_runtime_resume_and_get(bdi->dev);
1535 case POWER_SUPPLY_PROP_ONLINE:
1536 ret = bq24190_battery_set_online(bdi, val);
1538 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
1539 ret = bq24190_battery_set_temp_alert_max(bdi, val);
1545 pm_runtime_mark_last_busy(bdi->dev);
1546 pm_runtime_put_autosuspend(bdi->dev);
1551 static int bq24190_battery_property_is_writeable(struct power_supply *psy,
1552 enum power_supply_property psp)
1557 case POWER_SUPPLY_PROP_ONLINE:
1558 case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
1568 static enum power_supply_property bq24190_battery_properties[] = {
1569 POWER_SUPPLY_PROP_STATUS,
1570 POWER_SUPPLY_PROP_HEALTH,
1571 POWER_SUPPLY_PROP_ONLINE,
1572 POWER_SUPPLY_PROP_TECHNOLOGY,
1573 POWER_SUPPLY_PROP_TEMP_ALERT_MAX,
1574 POWER_SUPPLY_PROP_SCOPE,
1577 static const struct power_supply_desc bq24190_battery_desc = {
1578 .name = "bq24190-battery",
1579 .type = POWER_SUPPLY_TYPE_BATTERY,
1580 .properties = bq24190_battery_properties,
1581 .num_properties = ARRAY_SIZE(bq24190_battery_properties),
1582 .get_property = bq24190_battery_get_property,
1583 .set_property = bq24190_battery_set_property,
1584 .property_is_writeable = bq24190_battery_property_is_writeable,
1587 static int bq24190_configure_usb_otg(struct bq24190_dev_info *bdi, u8 ss_reg)
1592 otg_enabled = !!(ss_reg & BQ24190_REG_SS_VBUS_STAT_MASK);
1593 ret = extcon_set_state_sync(bdi->edev, EXTCON_USB, otg_enabled);
1595 dev_err(bdi->dev, "Can't set extcon state to %d: %d\n",
1601 static void bq24190_check_status(struct bq24190_dev_info *bdi)
1603 const u8 battery_mask_ss = BQ24190_REG_SS_CHRG_STAT_MASK;
1604 const u8 battery_mask_f = BQ24190_REG_F_BAT_FAULT_MASK
1605 | BQ24190_REG_F_NTC_FAULT_MASK;
1606 bool alert_charger = false, alert_battery = false;
1607 u8 ss_reg = 0, f_reg = 0;
1610 ret = bq24190_read(bdi, BQ24190_REG_SS, &ss_reg);
1612 dev_err(bdi->dev, "Can't read SS reg: %d\n", ret);
1618 ret = bq24190_read(bdi, BQ24190_REG_F, &f_reg);
1620 dev_err(bdi->dev, "Can't read F reg: %d\n", ret);
1623 } while (f_reg && ++i < 2);
1625 /* ignore over/under voltage fault after disconnect */
1626 if (f_reg == (1 << BQ24190_REG_F_CHRG_FAULT_SHIFT) &&
1627 !(ss_reg & BQ24190_REG_SS_PG_STAT_MASK))
1630 if (f_reg != bdi->f_reg) {
1632 "Fault: boost %d, charge %d, battery %d, ntc %d\n",
1633 !!(f_reg & BQ24190_REG_F_BOOST_FAULT_MASK),
1634 !!(f_reg & BQ24190_REG_F_CHRG_FAULT_MASK),
1635 !!(f_reg & BQ24190_REG_F_BAT_FAULT_MASK),
1636 !!(f_reg & BQ24190_REG_F_NTC_FAULT_MASK));
1638 mutex_lock(&bdi->f_reg_lock);
1639 if ((bdi->f_reg & battery_mask_f) != (f_reg & battery_mask_f))
1640 alert_battery = true;
1641 if ((bdi->f_reg & ~battery_mask_f) != (f_reg & ~battery_mask_f))
1642 alert_charger = true;
1644 mutex_unlock(&bdi->f_reg_lock);
1647 if (ss_reg != bdi->ss_reg) {
1649 * The device is in host mode so when PG_STAT goes from 1->0
1650 * (i.e., power removed) HIZ needs to be disabled.
1652 if ((bdi->ss_reg & BQ24190_REG_SS_PG_STAT_MASK) &&
1653 !(ss_reg & BQ24190_REG_SS_PG_STAT_MASK)) {
1654 ret = bq24190_write_mask(bdi, BQ24190_REG_ISC,
1655 BQ24190_REG_ISC_EN_HIZ_MASK,
1656 BQ24190_REG_ISC_EN_HIZ_SHIFT,
1659 dev_err(bdi->dev, "Can't access ISC reg: %d\n",
1663 if ((bdi->ss_reg & battery_mask_ss) != (ss_reg & battery_mask_ss))
1664 alert_battery = true;
1665 if ((bdi->ss_reg & ~battery_mask_ss) != (ss_reg & ~battery_mask_ss))
1666 alert_charger = true;
1667 bdi->ss_reg = ss_reg;
1670 if (alert_charger || alert_battery) {
1671 power_supply_changed(bdi->charger);
1672 bq24190_configure_usb_otg(bdi, ss_reg);
1674 if (alert_battery && bdi->battery)
1675 power_supply_changed(bdi->battery);
1677 dev_dbg(bdi->dev, "ss_reg: 0x%02x, f_reg: 0x%02x\n", ss_reg, f_reg);
1680 static irqreturn_t bq24190_irq_handler_thread(int irq, void *data)
1682 struct bq24190_dev_info *bdi = data;
1685 bdi->irq_event = true;
1686 error = pm_runtime_resume_and_get(bdi->dev);
1688 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", error);
1691 bq24190_check_status(bdi);
1692 pm_runtime_mark_last_busy(bdi->dev);
1693 pm_runtime_put_autosuspend(bdi->dev);
1694 bdi->irq_event = false;
1699 static int bq24190_hw_init(struct bq24190_dev_info *bdi)
1704 /* First check that the device really is what its supposed to be */
1705 ret = bq24190_read_mask(bdi, BQ24190_REG_VPRS,
1706 BQ24190_REG_VPRS_PN_MASK,
1707 BQ24190_REG_VPRS_PN_SHIFT,
1713 case BQ24190_REG_VPRS_PN_24190:
1714 case BQ24190_REG_VPRS_PN_24192:
1715 case BQ24190_REG_VPRS_PN_24192I:
1718 dev_err(bdi->dev, "Error unknown model: 0x%02x\n", v);
1722 ret = bq24190_register_reset(bdi);
1726 ret = bq24190_set_config(bdi);
1730 return bq24190_read(bdi, BQ24190_REG_SS, &bdi->ss_reg);
1733 static int bq24190_get_config(struct bq24190_dev_info *bdi)
1735 const char * const s = "ti,system-minimum-microvolt";
1736 struct power_supply_battery_info *info;
1739 idx = ARRAY_SIZE(bq24190_ccc_ichg_values) - 1;
1740 bdi->ichg_max = bq24190_ccc_ichg_values[idx];
1742 idx = ARRAY_SIZE(bq24190_cvc_vreg_values) - 1;
1743 bdi->vreg_max = bq24190_cvc_vreg_values[idx];
1745 if (device_property_read_u32(bdi->dev, s, &v) == 0) {
1747 if (v >= BQ24190_REG_POC_SYS_MIN_MIN
1748 && v <= BQ24190_REG_POC_SYS_MIN_MAX)
1751 dev_warn(bdi->dev, "invalid value for %s: %u\n", s, v);
1754 if (!power_supply_get_battery_info(bdi->charger, &info)) {
1755 v = info->precharge_current_ua / 1000;
1756 if (v >= BQ24190_REG_PCTCC_IPRECHG_MIN
1757 && v <= BQ24190_REG_PCTCC_IPRECHG_MAX)
1760 dev_warn(bdi->dev, "invalid value for battery:precharge-current-microamp: %d\n",
1763 v = info->charge_term_current_ua / 1000;
1764 if (v >= BQ24190_REG_PCTCC_ITERM_MIN
1765 && v <= BQ24190_REG_PCTCC_ITERM_MAX)
1768 dev_warn(bdi->dev, "invalid value for battery:charge-term-current-microamp: %d\n",
1771 /* These are optional, so no warning when not set */
1772 v = info->constant_charge_current_max_ua;
1773 if (v >= bq24190_ccc_ichg_values[0] && v <= bdi->ichg_max)
1774 bdi->ichg = bdi->ichg_max = v;
1776 v = info->constant_charge_voltage_max_uv;
1777 if (v >= bq24190_cvc_vreg_values[0] && v <= bdi->vreg_max)
1778 bdi->vreg = bdi->vreg_max = v;
1784 static int bq24190_probe(struct i2c_client *client)
1786 const struct i2c_device_id *id = i2c_client_get_device_id(client);
1787 struct i2c_adapter *adapter = client->adapter;
1788 struct device *dev = &client->dev;
1789 struct power_supply_config charger_cfg = {}, battery_cfg = {};
1790 struct bq24190_dev_info *bdi;
1793 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1794 dev_err(dev, "No support for SMBUS_BYTE_DATA\n");
1798 bdi = devm_kzalloc(dev, sizeof(*bdi), GFP_KERNEL);
1800 dev_err(dev, "Can't alloc bdi struct\n");
1804 bdi->client = client;
1806 strscpy(bdi->model_name, id->name, sizeof(bdi->model_name));
1807 mutex_init(&bdi->f_reg_lock);
1808 bdi->charge_type = POWER_SUPPLY_CHARGE_TYPE_FAST;
1810 bdi->ss_reg = BQ24190_REG_SS_VBUS_STAT_MASK; /* impossible state */
1811 INIT_DELAYED_WORK(&bdi->input_current_limit_work,
1812 bq24190_input_current_limit_work);
1814 i2c_set_clientdata(client, bdi);
1816 if (client->irq <= 0) {
1817 dev_err(dev, "Can't get irq info\n");
1821 bdi->edev = devm_extcon_dev_allocate(dev, bq24190_usb_extcon_cable);
1822 if (IS_ERR(bdi->edev))
1823 return PTR_ERR(bdi->edev);
1825 ret = devm_extcon_dev_register(dev, bdi->edev);
1829 pm_runtime_enable(dev);
1830 pm_runtime_use_autosuspend(dev);
1831 pm_runtime_set_autosuspend_delay(dev, 600);
1832 ret = pm_runtime_get_sync(dev);
1834 dev_err(dev, "pm_runtime_get failed: %i\n", ret);
1839 bq24190_sysfs_init_attrs();
1840 charger_cfg.attr_grp = bq24190_sysfs_groups;
1843 charger_cfg.drv_data = bdi;
1844 charger_cfg.of_node = dev->of_node;
1845 charger_cfg.supplied_to = bq24190_charger_supplied_to;
1846 charger_cfg.num_supplicants = ARRAY_SIZE(bq24190_charger_supplied_to);
1847 bdi->charger = power_supply_register(dev, &bq24190_charger_desc,
1849 if (IS_ERR(bdi->charger)) {
1850 dev_err(dev, "Can't register charger\n");
1851 ret = PTR_ERR(bdi->charger);
1855 /* the battery class is deprecated and will be removed. */
1856 /* in the interim, this property hides it. */
1857 if (!device_property_read_bool(dev, "omit-battery-class")) {
1858 battery_cfg.drv_data = bdi;
1859 bdi->battery = power_supply_register(dev, &bq24190_battery_desc,
1861 if (IS_ERR(bdi->battery)) {
1862 dev_err(dev, "Can't register battery\n");
1863 ret = PTR_ERR(bdi->battery);
1868 ret = bq24190_get_config(bdi);
1870 dev_err(dev, "Can't get devicetree config\n");
1874 ret = bq24190_hw_init(bdi);
1876 dev_err(dev, "Hardware init failed\n");
1880 ret = bq24190_configure_usb_otg(bdi, bdi->ss_reg);
1884 bdi->initialized = true;
1886 ret = devm_request_threaded_irq(dev, client->irq, NULL,
1887 bq24190_irq_handler_thread,
1888 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1889 "bq24190-charger", bdi);
1891 dev_err(dev, "Can't set up irq handler\n");
1895 ret = bq24190_register_vbus_regulator(bdi);
1899 enable_irq_wake(client->irq);
1901 pm_runtime_mark_last_busy(dev);
1902 pm_runtime_put_autosuspend(dev);
1907 if (!IS_ERR_OR_NULL(bdi->battery))
1908 power_supply_unregister(bdi->battery);
1909 power_supply_unregister(bdi->charger);
1912 pm_runtime_put_sync(dev);
1913 pm_runtime_dont_use_autosuspend(dev);
1914 pm_runtime_disable(dev);
1918 static void bq24190_remove(struct i2c_client *client)
1920 struct bq24190_dev_info *bdi = i2c_get_clientdata(client);
1923 cancel_delayed_work_sync(&bdi->input_current_limit_work);
1924 error = pm_runtime_resume_and_get(bdi->dev);
1926 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", error);
1928 bq24190_register_reset(bdi);
1930 power_supply_unregister(bdi->battery);
1931 power_supply_unregister(bdi->charger);
1933 pm_runtime_put_sync(bdi->dev);
1934 pm_runtime_dont_use_autosuspend(bdi->dev);
1935 pm_runtime_disable(bdi->dev);
1938 static void bq24190_shutdown(struct i2c_client *client)
1940 struct bq24190_dev_info *bdi = i2c_get_clientdata(client);
1942 /* Turn off 5V boost regulator on shutdown */
1943 bq24190_set_otg_vbus(bdi, false);
1946 static __maybe_unused int bq24190_runtime_suspend(struct device *dev)
1948 struct i2c_client *client = to_i2c_client(dev);
1949 struct bq24190_dev_info *bdi = i2c_get_clientdata(client);
1951 if (!bdi->initialized)
1954 dev_dbg(bdi->dev, "%s\n", __func__);
1959 static __maybe_unused int bq24190_runtime_resume(struct device *dev)
1961 struct i2c_client *client = to_i2c_client(dev);
1962 struct bq24190_dev_info *bdi = i2c_get_clientdata(client);
1964 if (!bdi->initialized)
1967 if (!bdi->irq_event) {
1968 dev_dbg(bdi->dev, "checking events on possible wakeirq\n");
1969 bq24190_check_status(bdi);
1975 static __maybe_unused int bq24190_pm_suspend(struct device *dev)
1977 struct i2c_client *client = to_i2c_client(dev);
1978 struct bq24190_dev_info *bdi = i2c_get_clientdata(client);
1981 error = pm_runtime_resume_and_get(bdi->dev);
1983 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", error);
1985 bq24190_register_reset(bdi);
1988 pm_runtime_mark_last_busy(bdi->dev);
1989 pm_runtime_put_autosuspend(bdi->dev);
1995 static __maybe_unused int bq24190_pm_resume(struct device *dev)
1997 struct i2c_client *client = to_i2c_client(dev);
1998 struct bq24190_dev_info *bdi = i2c_get_clientdata(client);
2002 bdi->ss_reg = BQ24190_REG_SS_VBUS_STAT_MASK; /* impossible state */
2004 error = pm_runtime_resume_and_get(bdi->dev);
2006 dev_warn(bdi->dev, "pm_runtime_get failed: %i\n", error);
2008 bq24190_register_reset(bdi);
2009 bq24190_set_config(bdi);
2010 bq24190_read(bdi, BQ24190_REG_SS, &bdi->ss_reg);
2013 pm_runtime_mark_last_busy(bdi->dev);
2014 pm_runtime_put_autosuspend(bdi->dev);
2017 /* Things may have changed while suspended so alert upper layer */
2018 power_supply_changed(bdi->charger);
2020 power_supply_changed(bdi->battery);
2025 static const struct dev_pm_ops bq24190_pm_ops = {
2026 SET_RUNTIME_PM_OPS(bq24190_runtime_suspend, bq24190_runtime_resume,
2028 SET_SYSTEM_SLEEP_PM_OPS(bq24190_pm_suspend, bq24190_pm_resume)
2031 static const struct i2c_device_id bq24190_i2c_ids[] = {
2038 MODULE_DEVICE_TABLE(i2c, bq24190_i2c_ids);
2040 static const struct of_device_id bq24190_of_match[] = {
2041 { .compatible = "ti,bq24190", },
2042 { .compatible = "ti,bq24192", },
2043 { .compatible = "ti,bq24192i", },
2044 { .compatible = "ti,bq24196", },
2047 MODULE_DEVICE_TABLE(of, bq24190_of_match);
2049 static struct i2c_driver bq24190_driver = {
2050 .probe = bq24190_probe,
2051 .remove = bq24190_remove,
2052 .shutdown = bq24190_shutdown,
2053 .id_table = bq24190_i2c_ids,
2055 .name = "bq24190-charger",
2056 .pm = &bq24190_pm_ops,
2057 .of_match_table = bq24190_of_match,
2060 module_i2c_driver(bq24190_driver);
2062 MODULE_LICENSE("GPL");
2063 MODULE_AUTHOR("Mark A. Greer <mgreer@animalcreek.com>");
2064 MODULE_DESCRIPTION("TI BQ24190 Charger Driver");