1 // SPDX-License-Identifier: GPL-2.0
2 // BQ2515X Battery Charger Driver
3 // Copyright (C) 2020 Texas Instruments Incorporated - https://www.ti.com/
7 #include <linux/init.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/power_supply.h>
12 #include <linux/regmap.h>
13 #include <linux/types.h>
15 #define BQ2515X_MANUFACTURER "Texas Instruments"
17 #define BQ2515X_STAT0 0x00
18 #define BQ2515X_STAT1 0x01
19 #define BQ2515X_STAT2 0x02
20 #define BQ2515X_FLAG0 0x03
21 #define BQ2515X_FLAG1 0x04
22 #define BQ2515X_FLAG2 0x05
23 #define BQ2515X_FLAG3 0x06
24 #define BQ2515X_MASK0 0x07
25 #define BQ2515X_MASK1 0x08
26 #define BQ2515X_MASK2 0x09
27 #define BQ2515X_MASK3 0x0a
28 #define BQ2515X_VBAT_CTRL 0x12
29 #define BQ2515X_ICHG_CTRL 0x13
30 #define BQ2515X_PCHRGCTRL 0x14
31 #define BQ2515X_TERMCTRL 0x15
32 #define BQ2515X_BUVLO 0x16
33 #define BQ2515X_CHARGERCTRL0 0x17
34 #define BQ2515X_CHARGERCTRL1 0x18
35 #define BQ2515X_ILIMCTRL 0x19
36 #define BQ2515X_LDOCTRL 0x1d
37 #define BQ2515X_MRCTRL 0x30
38 #define BQ2515X_ICCTRL0 0x35
39 #define BQ2515X_ICCTRL1 0x36
40 #define BQ2515X_ICCTRL2 0x37
41 #define BQ2515X_ADCCTRL0 0x40
42 #define BQ2515X_ADCCTRL1 0x41
43 #define BQ2515X_ADC_VBAT_M 0x42
44 #define BQ2515X_ADC_VBAT_L 0x43
45 #define BQ2515X_ADC_TS_M 0x44
46 #define BQ2515X_ADC_TS_L 0x45
47 #define BQ2515X_ADC_ICHG_M 0x46
48 #define BQ2515X_ADC_ICHG_L 0x47
49 #define BQ2515X_ADC_ADCIN_M 0x48
50 #define BQ2515X_ADC_ADCIN_L 0x49
51 #define BQ2515X_ADC_VIN_M 0x4a
52 #define BQ2515X_ADC_VIN_L 0x4b
53 #define BQ2515X_ADC_PMID_M 0x4c
54 #define BQ2515X_ADC_PMID_L 0x4d
55 #define BQ2515X_ADC_IIN_M 0x4e
56 #define BQ2515X_ADC_IIN_L 0x4f
57 #define BQ2515X_ADC_COMP1_M 0x52
58 #define BQ2515X_ADC_COMP1_L 0X53
59 #define BQ2515X_ADC_COMP2_M 0X54
60 #define BQ2515X_ADC_COMP2_L 0x55
61 #define BQ2515X_ADC_COMP3_M 0x56
62 #define BQ2515X_ADC_COMP3_L 0x57
63 #define BQ2515X_ADC_READ_EN 0x58
64 #define BQ2515X_TS_FASTCHGCTRL 0x61
65 #define BQ2515X_TS_COLD 0x62
66 #define BQ2515X_TS_COOL 0x63
67 #define BQ2515X_TS_WARM 0x64
68 #define BQ2515X_TS_HOT 0x65
69 #define BQ2515X_DEVICE_ID 0x6f
71 #define BQ2515X_DEFAULT_ICHG_UA 10000
72 #define BQ25150_DEFAULT_ILIM_UA 100000
73 #define BQ25155_DEFAULT_ILIM_UA 500000
74 #define BQ2515X_DEFAULT_VBAT_REG_UV 4200000
75 #define BQ2515X_DEFAULT_IPRECHARGE_UA 2500
77 #define BQ2515X_DIVISOR 65536
78 #define BQ2515X_VBAT_BASE_VOLT 3600000
79 #define BQ2515X_VBAT_REG_MAX 4600000
80 #define BQ2515X_VBAT_REG_MIN 3600000
81 #define BQ2515X_VBAT_STEP_UV 10000
82 #define BQ2515X_UV_FACTOR 1000000
83 #define BQ2515X_VBAT_MULTIPLIER 6
84 #define BQ2515X_ICHG_DIVISOR 52429
85 #define BQ2515X_ICHG_CURR_STEP_THRESH_UA 318750
86 #define BQ2515X_ICHG_MIN_UA 0
87 #define BQ2515X_ICHG_MAX_UA 500000
88 #define BQ2515X_ICHG_RNG_1B0_UA 1250
89 #define BQ2515X_ICHG_RNG_1B1_UA 2500
90 #define BQ2515X_VLOWV_SEL_1B0_UV 3000000
91 #define BQ2515X_VLOWV_SEL_1B1_UV 2800000
92 #define BQ2515X_PRECHRG_ICHRG_RNGE_1875_UA 18750
93 #define BQ2515X_PRECHRG_ICHRG_RNGE_3750_UA 37500
94 #define BQ2515X_TWAKE2_MIN_US 1700000
95 #define BQ2515X_TWAKE2_MAX_US 2300000
97 #define BQ2515X_ILIM_150MA 0x2
98 #define BQ2515X_ILIM_MASK 0x7
99 #define BQ2515X_ILIM_MIN 50000
100 #define BQ2515X_ILIM_MAX 600000
101 #define BQ2515X_HEALTH_MASK 0xf
102 #define BQ2515X_ICHGRNG_MASK 0x80
103 #define BQ2515X_STAT0_MASK 0x0f
104 #define BQ2515X_STAT1_MASK 0x1f
105 #define BQ2515X_PRECHARGE_MASK 0x1f
107 #define BQ2515X_TS_HOT_STAT BIT(0)
108 #define BQ2515X_TS_WARM_STAT BIT(1)
109 #define BQ2515X_TS_COOL_STAT BIT(2)
110 #define BQ2515X_TS_COLD_STAT BIT(3)
111 #define BQ2515X_SAFETY_TIMER_EXP BIT(5)
113 #define BQ2515X_EN_VBAT_READ BIT(3)
114 #define BQ2515X_EN_ICHG_READ BIT(5)
116 #define BQ2515X_VIN_GOOD BIT(0)
117 #define BQ2515X_CHRG_DONE BIT(5)
118 #define BQ2515X_CV_CHRG_MODE BIT(6)
120 #define BQ2515X_VIN_OVP_FAULT_STAT BIT(7)
122 #define BQ2515X_WATCHDOG_DISABLE BIT(4)
124 #define BQ2515X_ICHARGE_RANGE BIT(7)
126 #define BQ2515X_VLOWV_SEL BIT(5)
128 #define BQ2515X_CHARGER_DISABLE BIT(0)
130 #define BQ2515X_HWRESET_14S_WD BIT(1)
132 static const int bq2515x_ilim_lvl_values
[] = {
133 50000, 100000, 150000, 200000, 300000, 400000, 500000, 600000
137 * struct bq2515x_init_data -
138 * @ilim: input current limit
139 * @ichg: fast charge current
140 * @vbatreg: battery regulation voltage
141 * @iprechg: precharge current
143 struct bq2515x_init_data
{
156 * struct bq2515x_device -
157 * @mains: mains properties
158 * @battery: battery properties
159 * @regmap: register map structure
160 * @dev: device structure
162 * @reset_gpio: manual reset (MR) pin
163 * @powerdown_gpio: low power mode pin
164 * @ac_detect_gpio: power good (PG) pin
165 * @ce_gpio: charge enable (CE) pin
167 * @model_name: string value describing device model
168 * @device_id: value of device_id
169 * @mains_online: boolean value indicating power supply online
171 * @init_data: charger initialization data structure
173 struct bq2515x_device
{
174 struct power_supply
*mains
;
175 struct power_supply
*battery
;
176 struct regmap
*regmap
;
179 struct gpio_desc
*reset_gpio
;
180 struct gpio_desc
*powerdown_gpio
;
181 struct gpio_desc
*ac_detect_gpio
;
182 struct gpio_desc
*ce_gpio
;
184 char model_name
[I2C_NAME_SIZE
];
188 struct bq2515x_init_data init_data
;
191 static const struct reg_default bq25150_reg_defaults
[] = {
192 {BQ2515X_FLAG0
, 0x0},
193 {BQ2515X_FLAG1
, 0x0},
194 {BQ2515X_FLAG2
, 0x0},
195 {BQ2515X_FLAG3
, 0x0},
196 {BQ2515X_MASK0
, 0x0},
197 {BQ2515X_MASK1
, 0x0},
198 {BQ2515X_MASK2
, 0x71},
199 {BQ2515X_MASK3
, 0x0},
200 {BQ2515X_VBAT_CTRL
, 0x3C},
201 {BQ2515X_ICHG_CTRL
, 0x8},
202 {BQ2515X_PCHRGCTRL
, 0x2},
203 {BQ2515X_TERMCTRL
, 0x14},
204 {BQ2515X_BUVLO
, 0x0},
205 {BQ2515X_CHARGERCTRL0
, 0x82},
206 {BQ2515X_CHARGERCTRL1
, 0x42},
207 {BQ2515X_ILIMCTRL
, 0x1},
208 {BQ2515X_LDOCTRL
, 0xB0},
209 {BQ2515X_MRCTRL
, 0x2A},
210 {BQ2515X_ICCTRL0
, 0x10},
211 {BQ2515X_ICCTRL1
, 0x0},
212 {BQ2515X_ICCTRL2
, 0x0},
213 {BQ2515X_ADCCTRL0
, 0x2},
214 {BQ2515X_ADCCTRL1
, 0x40},
215 {BQ2515X_ADC_COMP1_M
, 0x23},
216 {BQ2515X_ADC_COMP1_L
, 0x20},
217 {BQ2515X_ADC_COMP2_M
, 0x38},
218 {BQ2515X_ADC_COMP2_L
, 0x90},
219 {BQ2515X_ADC_COMP3_M
, 0x0},
220 {BQ2515X_ADC_COMP3_L
, 0x0},
221 {BQ2515X_ADC_READ_EN
, 0x0},
222 {BQ2515X_TS_FASTCHGCTRL
, 0x34},
223 {BQ2515X_TS_COLD
, 0x7C},
224 {BQ2515X_TS_COOL
, 0x6D},
225 {BQ2515X_TS_WARM
, 0x38},
226 {BQ2515X_TS_HOT
, 0x27},
227 {BQ2515X_DEVICE_ID
, 0x20},
230 static const struct reg_default bq25155_reg_defaults
[] = {
231 {BQ2515X_FLAG0
, 0x0},
232 {BQ2515X_FLAG1
, 0x0},
233 {BQ2515X_FLAG2
, 0x0},
234 {BQ2515X_FLAG3
, 0x0},
235 {BQ2515X_MASK0
, 0x0},
236 {BQ2515X_MASK1
, 0x0},
237 {BQ2515X_MASK2
, 0x71},
238 {BQ2515X_MASK3
, 0x0},
239 {BQ2515X_VBAT_CTRL
, 0x3C},
240 {BQ2515X_ICHG_CTRL
, 0x8},
241 {BQ2515X_PCHRGCTRL
, 0x2},
242 {BQ2515X_TERMCTRL
, 0x14},
243 {BQ2515X_BUVLO
, 0x0},
244 {BQ2515X_CHARGERCTRL0
, 0x82},
245 {BQ2515X_CHARGERCTRL1
, 0xC2},
246 {BQ2515X_ILIMCTRL
, 0x6},
247 {BQ2515X_LDOCTRL
, 0xB0},
248 {BQ2515X_MRCTRL
, 0x2A},
249 {BQ2515X_ICCTRL0
, 0x10},
250 {BQ2515X_ICCTRL1
, 0x0},
251 {BQ2515X_ICCTRL2
, 0x40},
252 {BQ2515X_ADCCTRL0
, 0x2},
253 {BQ2515X_ADCCTRL1
, 0x40},
254 {BQ2515X_ADC_COMP1_M
, 0x23},
255 {BQ2515X_ADC_COMP1_L
, 0x20},
256 {BQ2515X_ADC_COMP2_M
, 0x38},
257 {BQ2515X_ADC_COMP2_L
, 0x90},
258 {BQ2515X_ADC_COMP3_M
, 0x0},
259 {BQ2515X_ADC_COMP3_L
, 0x0},
260 {BQ2515X_ADC_READ_EN
, 0x0},
261 {BQ2515X_TS_FASTCHGCTRL
, 0x34},
262 {BQ2515X_TS_COLD
, 0x7C},
263 {BQ2515X_TS_COOL
, 0x6D},
264 {BQ2515X_TS_WARM
, 0x38},
265 {BQ2515X_TS_HOT
, 0x27},
266 {BQ2515X_DEVICE_ID
, 0x35},
269 static int bq2515x_wake_up(struct bq2515x_device
*bq2515x
)
274 /* Read the STAT register if we can read it then the device is out
275 * of ship mode. If the register cannot be read then attempt to wake
276 * it up and enable the ADC.
278 ret
= regmap_read(bq2515x
->regmap
, BQ2515X_STAT0
, &val
);
282 /* Need to toggle LP and bring device out of ship mode. The device
283 * will exit the ship mode when the MR pin is held low for at least
284 * t_WAKE2 as shown in section 8.3.7.1 of the datasheet.
286 gpiod_set_value_cansleep(bq2515x
->powerdown_gpio
, 0);
288 gpiod_set_value_cansleep(bq2515x
->reset_gpio
, 0);
289 usleep_range(BQ2515X_TWAKE2_MIN_US
, BQ2515X_TWAKE2_MAX_US
);
290 gpiod_set_value_cansleep(bq2515x
->reset_gpio
, 1);
292 return regmap_write(bq2515x
->regmap
, BQ2515X_ADC_READ_EN
,
293 (BQ2515X_EN_VBAT_READ
| BQ2515X_EN_ICHG_READ
));
296 static int bq2515x_update_ps_status(struct bq2515x_device
*bq2515x
)
302 if (bq2515x
->ac_detect_gpio
)
303 val
= gpiod_get_value_cansleep(bq2515x
->ac_detect_gpio
);
305 ret
= regmap_read(bq2515x
->regmap
, BQ2515X_STAT0
, &val
);
310 dc
= val
& BQ2515X_VIN_GOOD
;
312 ret
= bq2515x
->mains_online
!= dc
;
314 bq2515x
->mains_online
= dc
;
319 static int bq2515x_disable_watchdog_timers(struct bq2515x_device
*bq2515x
)
323 ret
= regmap_update_bits(bq2515x
->regmap
, BQ2515X_CHARGERCTRL0
,
324 BQ2515X_WATCHDOG_DISABLE
, BQ2515X_WATCHDOG_DISABLE
);
328 return regmap_update_bits(bq2515x
->regmap
, BQ2515X_ICCTRL2
,
329 BQ2515X_HWRESET_14S_WD
, 0);
332 static int bq2515x_get_battery_voltage_now(struct bq2515x_device
*bq2515x
)
337 uint32_t vbat_measurement
;
339 if (!bq2515x
->mains_online
)
340 bq2515x_wake_up(bq2515x
);
342 ret
= regmap_read(bq2515x
->regmap
, BQ2515X_ADC_VBAT_M
, &vbat_msb
);
346 ret
= regmap_read(bq2515x
->regmap
, BQ2515X_ADC_VBAT_L
, &vbat_lsb
);
350 vbat_measurement
= (vbat_msb
<< 8) | vbat_lsb
;
352 return vbat_measurement
* (BQ2515X_UV_FACTOR
/ BQ2515X_DIVISOR
) *
353 BQ2515X_VBAT_MULTIPLIER
;
356 static int bq2515x_get_battery_current_now(struct bq2515x_device
*bq2515x
)
361 uint32_t ichg_measurement
;
362 u16 ichg_multiplier
= BQ2515X_ICHG_RNG_1B0_UA
;
363 unsigned int ichg_reg_code
, reg_code
;
364 unsigned int icharge_range
= 0, pchrgctrl
;
365 unsigned int buvlo
, vlowv_sel
, vlowv
= BQ2515X_VLOWV_SEL_1B0_UV
;
367 if (!bq2515x
->mains_online
)
370 ret
= regmap_read(bq2515x
->regmap
, BQ2515X_ADC_ICHG_M
, &ichg_msb
);
374 ret
= regmap_read(bq2515x
->regmap
, BQ2515X_ADC_ICHG_L
, &ichg_lsb
);
378 ichg_measurement
= (ichg_msb
<< 8) | ichg_lsb
;
380 ret
= regmap_read(bq2515x
->regmap
, BQ2515X_BUVLO
, &buvlo
);
384 vlowv_sel
= buvlo
& BQ2515X_VLOWV_SEL
;
387 vlowv
= BQ2515X_VLOWV_SEL_1B1_UV
;
389 if (bq2515x_get_battery_voltage_now(bq2515x
) < vlowv
) {
390 ret
= regmap_read(bq2515x
->regmap
, BQ2515X_PCHRGCTRL
,
395 reg_code
= pchrgctrl
& BQ2515X_PRECHARGE_MASK
;
397 ret
= regmap_read(bq2515x
->regmap
, BQ2515X_ICHG_CTRL
,
402 reg_code
= ichg_reg_code
;
405 ret
= regmap_read(bq2515x
->regmap
, BQ2515X_PCHRGCTRL
, &pchrgctrl
);
409 icharge_range
= pchrgctrl
& BQ2515X_ICHARGE_RANGE
;
412 ichg_multiplier
= BQ2515X_ICHG_RNG_1B1_UA
;
414 return reg_code
* (ichg_multiplier
* ichg_measurement
/
415 BQ2515X_ICHG_DIVISOR
);
418 static bool bq2515x_get_charge_disable(struct bq2515x_device
*bq2515x
)
425 ce_pin
= gpiod_get_value_cansleep(bq2515x
->ce_gpio
);
427 ret
= regmap_read(bq2515x
->regmap
, BQ2515X_ICCTRL2
, &icctrl2
);
431 charger_disable
= icctrl2
& BQ2515X_CHARGER_DISABLE
;
433 if (charger_disable
|| ce_pin
)
439 static int bq2515x_set_charge_disable(struct bq2515x_device
*bq2515x
, int val
)
441 gpiod_set_value_cansleep(bq2515x
->ce_gpio
, val
);
443 return regmap_update_bits(bq2515x
->regmap
, BQ2515X_ICCTRL2
,
444 BQ2515X_CHARGER_DISABLE
, val
);
447 static int bq2515x_get_const_charge_current(struct bq2515x_device
*bq2515x
)
450 u16 ichg_multiplier
= BQ2515X_ICHG_RNG_1B0_UA
;
451 unsigned int ichg_reg_code
;
452 unsigned int pchrgctrl
;
453 unsigned int icharge_range
;
455 ret
= regmap_read(bq2515x
->regmap
, BQ2515X_ICHG_CTRL
, &ichg_reg_code
);
459 ret
= regmap_read(bq2515x
->regmap
, BQ2515X_PCHRGCTRL
, &pchrgctrl
);
463 icharge_range
= pchrgctrl
& BQ2515X_ICHARGE_RANGE
;
466 ichg_multiplier
= BQ2515X_ICHG_RNG_1B1_UA
;
468 return ichg_reg_code
* ichg_multiplier
;
471 static int bq2515x_set_const_charge_current(struct bq2515x_device
*bq2515x
,
475 unsigned int ichg_reg_code
;
476 u16 ichg_multiplier
= BQ2515X_ICHG_RNG_1B0_UA
;
477 unsigned int icharge_range
= 0;
479 if (val
> BQ2515X_ICHG_MAX_UA
|| val
< BQ2515X_ICHG_MIN_UA
)
482 if (val
> BQ2515X_ICHG_CURR_STEP_THRESH_UA
) {
483 ichg_multiplier
= BQ2515X_ICHG_RNG_1B1_UA
;
484 icharge_range
= BQ2515X_ICHARGE_RANGE
;
487 bq2515x_set_charge_disable(bq2515x
, 1);
489 ret
= regmap_update_bits(bq2515x
->regmap
, BQ2515X_PCHRGCTRL
,
490 BQ2515X_ICHARGE_RANGE
, icharge_range
);
494 ichg_reg_code
= val
/ ichg_multiplier
;
496 ret
= regmap_write(bq2515x
->regmap
, BQ2515X_ICHG_CTRL
, ichg_reg_code
);
500 return bq2515x_set_charge_disable(bq2515x
, 0);
503 static int bq2515x_get_precharge_current(struct bq2515x_device
*bq2515x
)
506 unsigned int pchrgctrl
;
507 unsigned int icharge_range
;
508 u16 precharge_multiplier
= BQ2515X_ICHG_RNG_1B0_UA
;
509 unsigned int precharge_reg_code
;
511 ret
= regmap_read(bq2515x
->regmap
, BQ2515X_PCHRGCTRL
, &pchrgctrl
);
515 icharge_range
= pchrgctrl
& BQ2515X_ICHARGE_RANGE
;
518 precharge_multiplier
= BQ2515X_ICHG_RNG_1B1_UA
;
520 precharge_reg_code
= pchrgctrl
& BQ2515X_PRECHARGE_MASK
;
522 return precharge_reg_code
* precharge_multiplier
;
525 static int bq2515x_set_precharge_current(struct bq2515x_device
*bq2515x
,
529 unsigned int pchrgctrl
;
530 unsigned int icharge_range
;
531 unsigned int precharge_reg_code
;
532 unsigned int precharge_multiplier
= BQ2515X_ICHG_RNG_1B0_UA
;
533 unsigned int precharge_max_ua
= BQ2515X_PRECHRG_ICHRG_RNGE_1875_UA
;
535 ret
= regmap_read(bq2515x
->regmap
, BQ2515X_PCHRGCTRL
, &pchrgctrl
);
539 icharge_range
= pchrgctrl
& BQ2515X_ICHARGE_RANGE
;
542 precharge_max_ua
= BQ2515X_PRECHRG_ICHRG_RNGE_3750_UA
;
543 precharge_multiplier
= BQ2515X_ICHG_RNG_1B1_UA
;
545 precharge_max_ua
= BQ2515X_PRECHRG_ICHRG_RNGE_1875_UA
;
546 precharge_multiplier
= BQ2515X_ICHG_RNG_1B0_UA
;
548 if (val
> precharge_max_ua
|| val
< BQ2515X_ICHG_MIN_UA
)
551 precharge_reg_code
= val
/ precharge_multiplier
;
553 ret
= bq2515x_set_charge_disable(bq2515x
, 1);
557 ret
= regmap_update_bits(bq2515x
->regmap
, BQ2515X_PCHRGCTRL
,
558 BQ2515X_PRECHARGE_MASK
, precharge_reg_code
);
562 return bq2515x_set_charge_disable(bq2515x
, 0);
565 static int bq2515x_charging_status(struct bq2515x_device
*bq2515x
,
566 union power_supply_propval
*val
)
568 bool status0_no_fault
;
569 bool status1_no_fault
;
575 if (!bq2515x
->mains_online
) {
576 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
580 ret
= regmap_read(bq2515x
->regmap
, BQ2515X_STAT0
, &status
);
585 * The code block below is used to determine if any faults from the
586 * STAT0 register are disbaling charging or if the charge has completed
587 * according to the CHARGE_DONE_STAT bit.
589 if (((status
& BQ2515X_STAT0_MASK
) == true) &
590 ((status
& BQ2515X_CHRG_DONE
) == false)) {
591 status0_no_fault
= true;
593 } else if (status
& BQ2515X_CHRG_DONE
) {
595 status0_no_fault
= false;
597 status0_no_fault
= false;
601 ret
= regmap_read(bq2515x
->regmap
, BQ2515X_STAT1
, &status
);
605 * The code block below is used to determine if any faults from the
606 * STAT1 register are disbaling charging
608 if ((status
& BQ2515X_STAT1_MASK
) == false)
609 status1_no_fault
= true;
611 status1_no_fault
= false;
613 ce_status
= (!bq2515x_get_charge_disable(bq2515x
));
616 * If there are no faults and charging is enabled, then status is
617 * charging. Otherwise, if charging is complete, then status is full.
618 * Otherwise, if a fault exists or charging is disabled, then status is
621 if (status0_no_fault
& status1_no_fault
& ce_status
)
622 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
623 else if (charge_done
)
624 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
625 else if (!(status0_no_fault
& status1_no_fault
& ce_status
))
626 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
631 static int bq2515x_get_batt_reg(struct bq2515x_device
*bq2515x
)
636 ret
= regmap_read(bq2515x
->regmap
, BQ2515X_VBAT_CTRL
, &vbat_reg_code
);
640 return BQ2515X_VBAT_BASE_VOLT
+ vbat_reg_code
* BQ2515X_VBAT_STEP_UV
;
643 static int bq2515x_set_batt_reg(struct bq2515x_device
*bq2515x
, int val
)
647 if (val
> BQ2515X_VBAT_REG_MAX
|| val
< BQ2515X_VBAT_REG_MIN
)
650 vbat_reg_code
= (val
- BQ2515X_VBAT_BASE_VOLT
) / BQ2515X_VBAT_STEP_UV
;
652 return regmap_write(bq2515x
->regmap
, BQ2515X_VBAT_CTRL
, vbat_reg_code
);
655 static int bq2515x_get_ilim_lvl(struct bq2515x_device
*bq2515x
)
660 ret
= regmap_read(bq2515x
->regmap
, BQ2515X_ILIMCTRL
, &ilimctrl
);
664 return bq2515x_ilim_lvl_values
[ilimctrl
& BQ2515X_ILIM_MASK
];
667 static int bq2515x_set_ilim_lvl(struct bq2515x_device
*bq2515x
, int val
)
670 unsigned int array_size
= ARRAY_SIZE(bq2515x_ilim_lvl_values
);
672 for (i
= array_size
- 1; i
> 0; i
--) {
673 if (val
>= bq2515x_ilim_lvl_values
[i
])
676 return regmap_write(bq2515x
->regmap
, BQ2515X_ILIMCTRL
, i
);
679 static int bq2515x_power_supply_property_is_writeable(struct power_supply
*psy
,
680 enum power_supply_property prop
)
683 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
684 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
685 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
686 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT
:
693 static int bq2515x_charger_get_health(struct bq2515x_device
*bq2515x
,
694 union power_supply_propval
*val
)
696 int health
= POWER_SUPPLY_HEALTH_GOOD
;
701 if (!bq2515x
->mains_online
)
702 bq2515x_wake_up(bq2515x
);
704 ret
= regmap_read(bq2515x
->regmap
, BQ2515X_FLAG3
, &flag3
);
708 ret
= regmap_read(bq2515x
->regmap
, BQ2515X_STAT1
, &stat1
);
712 if (stat1
& BQ2515X_HEALTH_MASK
) {
713 switch (stat1
& BQ2515X_HEALTH_MASK
) {
714 case BQ2515X_TS_HOT_STAT
:
715 health
= POWER_SUPPLY_HEALTH_HOT
;
717 case BQ2515X_TS_WARM_STAT
:
718 health
= POWER_SUPPLY_HEALTH_WARM
;
720 case BQ2515X_TS_COOL_STAT
:
721 health
= POWER_SUPPLY_HEALTH_COOL
;
723 case BQ2515X_TS_COLD_STAT
:
724 health
= POWER_SUPPLY_HEALTH_COLD
;
727 health
= POWER_SUPPLY_HEALTH_UNKNOWN
;
732 if (stat1
& BQ2515X_VIN_OVP_FAULT_STAT
)
733 health
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
735 if (flag3
& BQ2515X_SAFETY_TIMER_EXP
)
736 health
= POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE
;
738 val
->intval
= health
;
742 static int bq2515x_mains_set_property(struct power_supply
*psy
,
743 enum power_supply_property prop
,
744 const union power_supply_propval
*val
)
746 struct bq2515x_device
*bq2515x
= power_supply_get_drvdata(psy
);
750 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
751 ret
= bq2515x_set_batt_reg(bq2515x
, val
->intval
);
754 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
755 ret
= bq2515x_set_const_charge_current(bq2515x
, val
->intval
);
758 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
759 ret
= bq2515x_set_ilim_lvl(bq2515x
, val
->intval
);
762 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT
:
763 ret
= bq2515x_set_precharge_current(bq2515x
, val
->intval
);
773 static int bq2515x_mains_get_property(struct power_supply
*psy
,
774 enum power_supply_property prop
,
775 union power_supply_propval
*val
)
777 struct bq2515x_device
*bq2515x
= power_supply_get_drvdata(psy
);
782 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
783 ret
= bq2515x_get_const_charge_current(bq2515x
);
790 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
791 ret
= bq2515x_get_batt_reg(bq2515x
);
797 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT
:
798 ret
= bq2515x_get_precharge_current(bq2515x
);
804 case POWER_SUPPLY_PROP_ONLINE
:
805 val
->intval
= bq2515x
->mains_online
;
808 case POWER_SUPPLY_PROP_HEALTH
:
809 ret
= bq2515x_charger_get_health(bq2515x
, val
);
811 val
->intval
= POWER_SUPPLY_HEALTH_UNKNOWN
;
814 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
:
815 ret
= bq2515x_get_ilim_lvl(bq2515x
);
821 case POWER_SUPPLY_PROP_MODEL_NAME
:
822 val
->strval
= bq2515x
->model_name
;
825 case POWER_SUPPLY_PROP_MANUFACTURER
:
826 val
->strval
= BQ2515X_MANUFACTURER
;
829 case POWER_SUPPLY_PROP_STATUS
:
830 ret
= bq2515x_charging_status(bq2515x
, val
);
842 static int bq2515x_battery_get_property(struct power_supply
*psy
,
843 enum power_supply_property prop
,
844 union power_supply_propval
*val
)
846 struct bq2515x_device
*bq2515x
= power_supply_get_drvdata(psy
);
849 ret
= bq2515x_update_ps_status(bq2515x
);
855 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
:
856 ret
= bq2515x
->init_data
.vbatreg
;
862 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
:
863 ret
= bq2515x
->init_data
.ichg
;
869 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
870 ret
= bq2515x_get_battery_voltage_now(bq2515x
);
876 case POWER_SUPPLY_PROP_CURRENT_NOW
:
877 ret
= bq2515x_get_battery_current_now(bq2515x
);
889 static const enum power_supply_property bq2515x_battery_properties
[] = {
890 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
891 POWER_SUPPLY_PROP_CURRENT_NOW
,
892 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
,
893 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
,
896 static const enum power_supply_property bq2515x_mains_properties
[] = {
897 POWER_SUPPLY_PROP_ONLINE
,
898 POWER_SUPPLY_PROP_STATUS
,
899 POWER_SUPPLY_PROP_HEALTH
,
900 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT
,
901 POWER_SUPPLY_PROP_MODEL_NAME
,
902 POWER_SUPPLY_PROP_MANUFACTURER
,
903 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
,
904 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
,
905 POWER_SUPPLY_PROP_PRECHARGE_CURRENT
,
908 static const struct power_supply_desc bq2515x_mains_desc
= {
909 .name
= "bq2515x-mains",
910 .type
= POWER_SUPPLY_TYPE_MAINS
,
911 .get_property
= bq2515x_mains_get_property
,
912 .set_property
= bq2515x_mains_set_property
,
913 .properties
= bq2515x_mains_properties
,
914 .num_properties
= ARRAY_SIZE(bq2515x_mains_properties
),
915 .property_is_writeable
= bq2515x_power_supply_property_is_writeable
,
918 static const struct power_supply_desc bq2515x_battery_desc
= {
919 .name
= "bq2515x-battery",
920 .type
= POWER_SUPPLY_TYPE_BATTERY
,
921 .get_property
= bq2515x_battery_get_property
,
922 .properties
= bq2515x_battery_properties
,
923 .num_properties
= ARRAY_SIZE(bq2515x_battery_properties
),
924 .property_is_writeable
= bq2515x_power_supply_property_is_writeable
,
927 static int bq2515x_power_supply_register(struct bq2515x_device
*bq2515x
,
928 struct device
*dev
, struct power_supply_config psy_cfg
)
930 bq2515x
->mains
= devm_power_supply_register(bq2515x
->dev
,
933 if (IS_ERR(bq2515x
->mains
))
936 bq2515x
->battery
= devm_power_supply_register(bq2515x
->dev
,
937 &bq2515x_battery_desc
,
939 if (IS_ERR(bq2515x
->battery
))
945 static int bq2515x_hw_init(struct bq2515x_device
*bq2515x
)
948 struct power_supply_battery_info bat_info
= { };
950 ret
= bq2515x_disable_watchdog_timers(bq2515x
);
954 if (bq2515x
->init_data
.ilim
) {
955 ret
= bq2515x_set_ilim_lvl(bq2515x
, bq2515x
->init_data
.ilim
);
960 ret
= power_supply_get_battery_info(bq2515x
->mains
, &bat_info
);
962 dev_warn(bq2515x
->dev
, "battery info missing, default values will be applied\n");
964 bq2515x
->init_data
.ichg
= BQ2515X_DEFAULT_ICHG_UA
;
966 bq2515x
->init_data
.vbatreg
= BQ2515X_DEFAULT_VBAT_REG_UV
;
968 bq2515x
->init_data
.iprechg
= BQ2515X_DEFAULT_IPRECHARGE_UA
;
971 bq2515x
->init_data
.ichg
=
972 bat_info
.constant_charge_current_max_ua
;
974 bq2515x
->init_data
.vbatreg
=
975 bat_info
.constant_charge_voltage_max_uv
;
977 bq2515x
->init_data
.iprechg
=
978 bat_info
.precharge_current_ua
;
981 ret
= bq2515x_set_const_charge_current(bq2515x
,
982 bq2515x
->init_data
.ichg
);
986 ret
= bq2515x_set_batt_reg(bq2515x
, bq2515x
->init_data
.vbatreg
);
990 return bq2515x_set_precharge_current(bq2515x
,
991 bq2515x
->init_data
.iprechg
);
994 static int bq2515x_read_properties(struct bq2515x_device
*bq2515x
)
998 ret
= device_property_read_u32(bq2515x
->dev
,
999 "input-current-limit-microamp",
1000 &bq2515x
->init_data
.ilim
);
1002 switch (bq2515x
->device_id
) {
1004 bq2515x
->init_data
.ilim
= BQ25150_DEFAULT_ILIM_UA
;
1007 bq2515x
->init_data
.ilim
= BQ25155_DEFAULT_ILIM_UA
;
1012 bq2515x
->ac_detect_gpio
= devm_gpiod_get_optional(bq2515x
->dev
,
1013 "ac-detect", GPIOD_IN
);
1014 if (IS_ERR(bq2515x
->ac_detect_gpio
)) {
1015 ret
= PTR_ERR(bq2515x
->ac_detect_gpio
);
1016 dev_err(bq2515x
->dev
, "Failed to get ac detect");
1020 bq2515x
->reset_gpio
= devm_gpiod_get_optional(bq2515x
->dev
,
1021 "reset", GPIOD_OUT_LOW
);
1022 if (IS_ERR(bq2515x
->reset_gpio
)) {
1023 ret
= PTR_ERR(bq2515x
->reset_gpio
);
1024 dev_err(bq2515x
->dev
, "Failed to get reset");
1028 bq2515x
->powerdown_gpio
= devm_gpiod_get_optional(bq2515x
->dev
,
1029 "powerdown", GPIOD_OUT_LOW
);
1030 if (IS_ERR(bq2515x
->powerdown_gpio
)) {
1031 ret
= PTR_ERR(bq2515x
->powerdown_gpio
);
1032 dev_err(bq2515x
->dev
, "Failed to get powerdown");
1036 bq2515x
->ce_gpio
= devm_gpiod_get_optional(bq2515x
->dev
,
1039 if (IS_ERR(bq2515x
->ce_gpio
)) {
1040 ret
= PTR_ERR(bq2515x
->ce_gpio
);
1041 dev_err(bq2515x
->dev
, "Failed to get ce");
1048 static bool bq2515x_volatile_register(struct device
*dev
, unsigned int reg
)
1051 case BQ2515X_STAT0
... BQ2515X_FLAG3
:
1052 case BQ2515X_ADC_VBAT_M
... BQ2515X_ADC_IIN_L
:
1059 static const struct regmap_config bq25150_regmap_config
= {
1063 .max_register
= BQ2515X_DEVICE_ID
,
1064 .reg_defaults
= bq25150_reg_defaults
,
1065 .num_reg_defaults
= ARRAY_SIZE(bq25150_reg_defaults
),
1066 .cache_type
= REGCACHE_RBTREE
,
1067 .volatile_reg
= bq2515x_volatile_register
,
1070 static const struct regmap_config bq25155_regmap_config
= {
1074 .max_register
= BQ2515X_DEVICE_ID
,
1075 .reg_defaults
= bq25155_reg_defaults
,
1076 .num_reg_defaults
= ARRAY_SIZE(bq25155_reg_defaults
),
1077 .cache_type
= REGCACHE_RBTREE
,
1078 .volatile_reg
= bq2515x_volatile_register
,
1081 static int bq2515x_probe(struct i2c_client
*client
,
1082 const struct i2c_device_id
*id
)
1084 struct device
*dev
= &client
->dev
;
1085 struct bq2515x_device
*bq2515x
;
1086 struct power_supply_config charger_cfg
= {};
1089 bq2515x
= devm_kzalloc(dev
, sizeof(*bq2515x
), GFP_KERNEL
);
1095 strncpy(bq2515x
->model_name
, id
->name
, I2C_NAME_SIZE
);
1097 bq2515x
->device_id
= id
->driver_data
;
1099 switch (bq2515x
->device_id
) {
1101 bq2515x
->regmap
= devm_regmap_init_i2c(client
,
1102 &bq25150_regmap_config
);
1105 bq2515x
->regmap
= devm_regmap_init_i2c(client
,
1106 &bq25155_regmap_config
);
1110 if (IS_ERR(bq2515x
->regmap
)) {
1111 dev_err(dev
, "failed to allocate register map\n");
1112 return PTR_ERR(bq2515x
->regmap
);
1115 i2c_set_clientdata(client
, bq2515x
);
1117 charger_cfg
.drv_data
= bq2515x
;
1118 charger_cfg
.of_node
= dev
->of_node
;
1120 ret
= bq2515x_read_properties(bq2515x
);
1122 dev_err(dev
, "Failed to read device tree properties %d\n",
1127 ret
= bq2515x_power_supply_register(bq2515x
, dev
, charger_cfg
);
1129 dev_err(dev
, "failed to register power supply\n");
1133 ret
= bq2515x_hw_init(bq2515x
);
1135 dev_err(dev
, "Cannot initialize the chip\n");
1142 static const struct i2c_device_id bq2515x_i2c_ids
[] = {
1143 { "bq25150", BQ25150
, },
1144 { "bq25155", BQ25155
, },
1147 MODULE_DEVICE_TABLE(i2c
, bq2515x_i2c_ids
);
1149 static const struct of_device_id bq2515x_of_match
[] = {
1150 { .compatible
= "ti,bq25150", },
1151 { .compatible
= "ti,bq25155", },
1154 MODULE_DEVICE_TABLE(of
, bq2515x_of_match
);
1156 static struct i2c_driver bq2515x_driver
= {
1158 .name
= "bq2515x-charger",
1159 .of_match_table
= bq2515x_of_match
,
1161 .probe
= bq2515x_probe
,
1162 .id_table
= bq2515x_i2c_ids
,
1164 module_i2c_driver(bq2515x_driver
);
1166 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
1167 MODULE_AUTHOR("Ricardo Rivera-Matos <r-rivera-matos@ti.com>");
1168 MODULE_DESCRIPTION("BQ2515X charger driver");
1169 MODULE_LICENSE("GPL v2");