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 {
151 * struct bq2515x_info -
152 * @regmap_config: register map config
153 * @ilim: input current limit
155 struct bq2515x_info {
156 const struct regmap_config *regmap_config;
161 * struct bq2515x_device -
162 * @mains: mains properties
163 * @battery: battery properties
164 * @regmap: register map structure
165 * @dev: device structure
167 * @reset_gpio: manual reset (MR) pin
168 * @powerdown_gpio: low power mode pin
169 * @ac_detect_gpio: power good (PG) pin
170 * @ce_gpio: charge enable (CE) pin
173 * @model_name: string value describing device model
174 * @mains_online: boolean value indicating power supply online
176 * @init_data: charger initialization data structure
178 struct bq2515x_device {
179 struct power_supply *mains;
180 struct power_supply *battery;
181 struct regmap *regmap;
184 struct gpio_desc *reset_gpio;
185 struct gpio_desc *powerdown_gpio;
186 struct gpio_desc *ac_detect_gpio;
187 struct gpio_desc *ce_gpio;
189 const struct bq2515x_info *info;
190 char model_name[I2C_NAME_SIZE];
193 struct bq2515x_init_data init_data;
196 static const struct reg_default bq25150_reg_defaults[] = {
197 {BQ2515X_FLAG0, 0x0},
198 {BQ2515X_FLAG1, 0x0},
199 {BQ2515X_FLAG2, 0x0},
200 {BQ2515X_FLAG3, 0x0},
201 {BQ2515X_MASK0, 0x0},
202 {BQ2515X_MASK1, 0x0},
203 {BQ2515X_MASK2, 0x71},
204 {BQ2515X_MASK3, 0x0},
205 {BQ2515X_VBAT_CTRL, 0x3C},
206 {BQ2515X_ICHG_CTRL, 0x8},
207 {BQ2515X_PCHRGCTRL, 0x2},
208 {BQ2515X_TERMCTRL, 0x14},
209 {BQ2515X_BUVLO, 0x0},
210 {BQ2515X_CHARGERCTRL0, 0x82},
211 {BQ2515X_CHARGERCTRL1, 0x42},
212 {BQ2515X_ILIMCTRL, 0x1},
213 {BQ2515X_LDOCTRL, 0xB0},
214 {BQ2515X_MRCTRL, 0x2A},
215 {BQ2515X_ICCTRL0, 0x10},
216 {BQ2515X_ICCTRL1, 0x0},
217 {BQ2515X_ICCTRL2, 0x0},
218 {BQ2515X_ADCCTRL0, 0x2},
219 {BQ2515X_ADCCTRL1, 0x40},
220 {BQ2515X_ADC_COMP1_M, 0x23},
221 {BQ2515X_ADC_COMP1_L, 0x20},
222 {BQ2515X_ADC_COMP2_M, 0x38},
223 {BQ2515X_ADC_COMP2_L, 0x90},
224 {BQ2515X_ADC_COMP3_M, 0x0},
225 {BQ2515X_ADC_COMP3_L, 0x0},
226 {BQ2515X_ADC_READ_EN, 0x0},
227 {BQ2515X_TS_FASTCHGCTRL, 0x34},
228 {BQ2515X_TS_COLD, 0x7C},
229 {BQ2515X_TS_COOL, 0x6D},
230 {BQ2515X_TS_WARM, 0x38},
231 {BQ2515X_TS_HOT, 0x27},
232 {BQ2515X_DEVICE_ID, 0x20},
235 static const struct reg_default bq25155_reg_defaults[] = {
236 {BQ2515X_FLAG0, 0x0},
237 {BQ2515X_FLAG1, 0x0},
238 {BQ2515X_FLAG2, 0x0},
239 {BQ2515X_FLAG3, 0x0},
240 {BQ2515X_MASK0, 0x0},
241 {BQ2515X_MASK1, 0x0},
242 {BQ2515X_MASK2, 0x71},
243 {BQ2515X_MASK3, 0x0},
244 {BQ2515X_VBAT_CTRL, 0x3C},
245 {BQ2515X_ICHG_CTRL, 0x8},
246 {BQ2515X_PCHRGCTRL, 0x2},
247 {BQ2515X_TERMCTRL, 0x14},
248 {BQ2515X_BUVLO, 0x0},
249 {BQ2515X_CHARGERCTRL0, 0x82},
250 {BQ2515X_CHARGERCTRL1, 0xC2},
251 {BQ2515X_ILIMCTRL, 0x6},
252 {BQ2515X_LDOCTRL, 0xB0},
253 {BQ2515X_MRCTRL, 0x2A},
254 {BQ2515X_ICCTRL0, 0x10},
255 {BQ2515X_ICCTRL1, 0x0},
256 {BQ2515X_ICCTRL2, 0x40},
257 {BQ2515X_ADCCTRL0, 0x2},
258 {BQ2515X_ADCCTRL1, 0x40},
259 {BQ2515X_ADC_COMP1_M, 0x23},
260 {BQ2515X_ADC_COMP1_L, 0x20},
261 {BQ2515X_ADC_COMP2_M, 0x38},
262 {BQ2515X_ADC_COMP2_L, 0x90},
263 {BQ2515X_ADC_COMP3_M, 0x0},
264 {BQ2515X_ADC_COMP3_L, 0x0},
265 {BQ2515X_ADC_READ_EN, 0x0},
266 {BQ2515X_TS_FASTCHGCTRL, 0x34},
267 {BQ2515X_TS_COLD, 0x7C},
268 {BQ2515X_TS_COOL, 0x6D},
269 {BQ2515X_TS_WARM, 0x38},
270 {BQ2515X_TS_HOT, 0x27},
271 {BQ2515X_DEVICE_ID, 0x35},
274 static int bq2515x_wake_up(struct bq2515x_device *bq2515x)
279 /* Read the STAT register if we can read it then the device is out
280 * of ship mode. If the register cannot be read then attempt to wake
281 * it up and enable the ADC.
283 ret = regmap_read(bq2515x->regmap, BQ2515X_STAT0, &val);
287 /* Need to toggle LP and bring device out of ship mode. The device
288 * will exit the ship mode when the MR pin is held low for at least
289 * t_WAKE2 as shown in section 8.3.7.1 of the datasheet.
291 gpiod_set_value_cansleep(bq2515x->powerdown_gpio, 0);
293 gpiod_set_value_cansleep(bq2515x->reset_gpio, 0);
294 usleep_range(BQ2515X_TWAKE2_MIN_US, BQ2515X_TWAKE2_MAX_US);
295 gpiod_set_value_cansleep(bq2515x->reset_gpio, 1);
297 return regmap_write(bq2515x->regmap, BQ2515X_ADC_READ_EN,
298 (BQ2515X_EN_VBAT_READ | BQ2515X_EN_ICHG_READ));
301 static int bq2515x_update_ps_status(struct bq2515x_device *bq2515x)
307 if (bq2515x->ac_detect_gpio)
308 val = gpiod_get_value_cansleep(bq2515x->ac_detect_gpio);
310 ret = regmap_read(bq2515x->regmap, BQ2515X_STAT0, &val);
315 dc = val & BQ2515X_VIN_GOOD;
317 ret = bq2515x->mains_online != dc;
319 bq2515x->mains_online = dc;
324 static int bq2515x_disable_watchdog_timers(struct bq2515x_device *bq2515x)
328 ret = regmap_update_bits(bq2515x->regmap, BQ2515X_CHARGERCTRL0,
329 BQ2515X_WATCHDOG_DISABLE, BQ2515X_WATCHDOG_DISABLE);
333 return regmap_update_bits(bq2515x->regmap, BQ2515X_ICCTRL2,
334 BQ2515X_HWRESET_14S_WD, 0);
337 static int bq2515x_get_battery_voltage_now(struct bq2515x_device *bq2515x)
342 uint32_t vbat_measurement;
344 if (!bq2515x->mains_online)
345 bq2515x_wake_up(bq2515x);
347 ret = regmap_read(bq2515x->regmap, BQ2515X_ADC_VBAT_M, &vbat_msb);
351 ret = regmap_read(bq2515x->regmap, BQ2515X_ADC_VBAT_L, &vbat_lsb);
355 vbat_measurement = (vbat_msb << 8) | vbat_lsb;
357 return vbat_measurement * (BQ2515X_UV_FACTOR / BQ2515X_DIVISOR) *
358 BQ2515X_VBAT_MULTIPLIER;
361 static int bq2515x_get_battery_current_now(struct bq2515x_device *bq2515x)
366 uint32_t ichg_measurement;
367 u16 ichg_multiplier = BQ2515X_ICHG_RNG_1B0_UA;
368 unsigned int ichg_reg_code, reg_code;
369 unsigned int icharge_range = 0, pchrgctrl;
370 unsigned int buvlo, vlowv_sel, vlowv = BQ2515X_VLOWV_SEL_1B0_UV;
372 if (!bq2515x->mains_online)
375 ret = regmap_read(bq2515x->regmap, BQ2515X_ADC_ICHG_M, &ichg_msb);
379 ret = regmap_read(bq2515x->regmap, BQ2515X_ADC_ICHG_L, &ichg_lsb);
383 ichg_measurement = (ichg_msb << 8) | ichg_lsb;
385 ret = regmap_read(bq2515x->regmap, BQ2515X_BUVLO, &buvlo);
389 vlowv_sel = buvlo & BQ2515X_VLOWV_SEL;
392 vlowv = BQ2515X_VLOWV_SEL_1B1_UV;
394 if (bq2515x_get_battery_voltage_now(bq2515x) < vlowv) {
395 ret = regmap_read(bq2515x->regmap, BQ2515X_PCHRGCTRL,
400 reg_code = pchrgctrl & BQ2515X_PRECHARGE_MASK;
402 ret = regmap_read(bq2515x->regmap, BQ2515X_ICHG_CTRL,
407 reg_code = ichg_reg_code;
410 ret = regmap_read(bq2515x->regmap, BQ2515X_PCHRGCTRL, &pchrgctrl);
414 icharge_range = pchrgctrl & BQ2515X_ICHARGE_RANGE;
417 ichg_multiplier = BQ2515X_ICHG_RNG_1B1_UA;
419 return reg_code * (ichg_multiplier * ichg_measurement /
420 BQ2515X_ICHG_DIVISOR);
423 static bool bq2515x_get_charge_disable(struct bq2515x_device *bq2515x)
430 ce_pin = gpiod_get_value_cansleep(bq2515x->ce_gpio);
432 ret = regmap_read(bq2515x->regmap, BQ2515X_ICCTRL2, &icctrl2);
436 charger_disable = icctrl2 & BQ2515X_CHARGER_DISABLE;
438 if (charger_disable || ce_pin)
444 static int bq2515x_set_charge_disable(struct bq2515x_device *bq2515x, int val)
446 gpiod_set_value_cansleep(bq2515x->ce_gpio, val);
448 return regmap_update_bits(bq2515x->regmap, BQ2515X_ICCTRL2,
449 BQ2515X_CHARGER_DISABLE, val);
452 static int bq2515x_get_const_charge_current(struct bq2515x_device *bq2515x)
455 u16 ichg_multiplier = BQ2515X_ICHG_RNG_1B0_UA;
456 unsigned int ichg_reg_code;
457 unsigned int pchrgctrl;
458 unsigned int icharge_range;
460 ret = regmap_read(bq2515x->regmap, BQ2515X_ICHG_CTRL, &ichg_reg_code);
464 ret = regmap_read(bq2515x->regmap, BQ2515X_PCHRGCTRL, &pchrgctrl);
468 icharge_range = pchrgctrl & BQ2515X_ICHARGE_RANGE;
471 ichg_multiplier = BQ2515X_ICHG_RNG_1B1_UA;
473 return ichg_reg_code * ichg_multiplier;
476 static int bq2515x_set_const_charge_current(struct bq2515x_device *bq2515x,
480 unsigned int ichg_reg_code;
481 u16 ichg_multiplier = BQ2515X_ICHG_RNG_1B0_UA;
482 unsigned int icharge_range = 0;
484 if (val > BQ2515X_ICHG_MAX_UA || val < BQ2515X_ICHG_MIN_UA)
487 if (val > BQ2515X_ICHG_CURR_STEP_THRESH_UA) {
488 ichg_multiplier = BQ2515X_ICHG_RNG_1B1_UA;
489 icharge_range = BQ2515X_ICHARGE_RANGE;
492 bq2515x_set_charge_disable(bq2515x, 1);
494 ret = regmap_update_bits(bq2515x->regmap, BQ2515X_PCHRGCTRL,
495 BQ2515X_ICHARGE_RANGE, icharge_range);
499 ichg_reg_code = val / ichg_multiplier;
501 ret = regmap_write(bq2515x->regmap, BQ2515X_ICHG_CTRL, ichg_reg_code);
505 return bq2515x_set_charge_disable(bq2515x, 0);
508 static int bq2515x_get_precharge_current(struct bq2515x_device *bq2515x)
511 unsigned int pchrgctrl;
512 unsigned int icharge_range;
513 u16 precharge_multiplier = BQ2515X_ICHG_RNG_1B0_UA;
514 unsigned int precharge_reg_code;
516 ret = regmap_read(bq2515x->regmap, BQ2515X_PCHRGCTRL, &pchrgctrl);
520 icharge_range = pchrgctrl & BQ2515X_ICHARGE_RANGE;
523 precharge_multiplier = BQ2515X_ICHG_RNG_1B1_UA;
525 precharge_reg_code = pchrgctrl & BQ2515X_PRECHARGE_MASK;
527 return precharge_reg_code * precharge_multiplier;
530 static int bq2515x_set_precharge_current(struct bq2515x_device *bq2515x,
534 unsigned int pchrgctrl;
535 unsigned int icharge_range;
536 unsigned int precharge_reg_code;
537 unsigned int precharge_multiplier = BQ2515X_ICHG_RNG_1B0_UA;
538 unsigned int precharge_max_ua = BQ2515X_PRECHRG_ICHRG_RNGE_1875_UA;
540 ret = regmap_read(bq2515x->regmap, BQ2515X_PCHRGCTRL, &pchrgctrl);
544 icharge_range = pchrgctrl & BQ2515X_ICHARGE_RANGE;
547 precharge_max_ua = BQ2515X_PRECHRG_ICHRG_RNGE_3750_UA;
548 precharge_multiplier = BQ2515X_ICHG_RNG_1B1_UA;
550 precharge_max_ua = BQ2515X_PRECHRG_ICHRG_RNGE_1875_UA;
551 precharge_multiplier = BQ2515X_ICHG_RNG_1B0_UA;
553 if (val > precharge_max_ua || val < BQ2515X_ICHG_MIN_UA)
556 precharge_reg_code = val / precharge_multiplier;
558 ret = bq2515x_set_charge_disable(bq2515x, 1);
562 ret = regmap_update_bits(bq2515x->regmap, BQ2515X_PCHRGCTRL,
563 BQ2515X_PRECHARGE_MASK, precharge_reg_code);
567 return bq2515x_set_charge_disable(bq2515x, 0);
570 static int bq2515x_charging_status(struct bq2515x_device *bq2515x,
571 union power_supply_propval *val)
573 bool status0_no_fault;
574 bool status1_no_fault;
580 if (!bq2515x->mains_online) {
581 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
585 ret = regmap_read(bq2515x->regmap, BQ2515X_STAT0, &status);
590 * The code block below is used to determine if any faults from the
591 * STAT0 register are disbaling charging or if the charge has completed
592 * according to the CHARGE_DONE_STAT bit.
594 if (((status & BQ2515X_STAT0_MASK) == true) &
595 ((status & BQ2515X_CHRG_DONE) == false)) {
596 status0_no_fault = true;
598 } else if (status & BQ2515X_CHRG_DONE) {
600 status0_no_fault = false;
602 status0_no_fault = false;
606 ret = regmap_read(bq2515x->regmap, BQ2515X_STAT1, &status);
610 * The code block below is used to determine if any faults from the
611 * STAT1 register are disbaling charging
613 if ((status & BQ2515X_STAT1_MASK) == false)
614 status1_no_fault = true;
616 status1_no_fault = false;
618 ce_status = (!bq2515x_get_charge_disable(bq2515x));
621 * If there are no faults and charging is enabled, then status is
622 * charging. Otherwise, if charging is complete, then status is full.
623 * Otherwise, if a fault exists or charging is disabled, then status is
626 if (status0_no_fault & status1_no_fault & ce_status)
627 val->intval = POWER_SUPPLY_STATUS_CHARGING;
628 else if (charge_done)
629 val->intval = POWER_SUPPLY_STATUS_FULL;
630 else if (!(status0_no_fault & status1_no_fault & ce_status))
631 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
636 static int bq2515x_get_batt_reg(struct bq2515x_device *bq2515x)
641 ret = regmap_read(bq2515x->regmap, BQ2515X_VBAT_CTRL, &vbat_reg_code);
645 return BQ2515X_VBAT_BASE_VOLT + vbat_reg_code * BQ2515X_VBAT_STEP_UV;
648 static int bq2515x_set_batt_reg(struct bq2515x_device *bq2515x, int val)
652 if (val > BQ2515X_VBAT_REG_MAX || val < BQ2515X_VBAT_REG_MIN)
655 vbat_reg_code = (val - BQ2515X_VBAT_BASE_VOLT) / BQ2515X_VBAT_STEP_UV;
657 return regmap_write(bq2515x->regmap, BQ2515X_VBAT_CTRL, vbat_reg_code);
660 static int bq2515x_get_ilim_lvl(struct bq2515x_device *bq2515x)
665 ret = regmap_read(bq2515x->regmap, BQ2515X_ILIMCTRL, &ilimctrl);
669 return bq2515x_ilim_lvl_values[ilimctrl & BQ2515X_ILIM_MASK];
672 static int bq2515x_set_ilim_lvl(struct bq2515x_device *bq2515x, int val)
675 unsigned int array_size = ARRAY_SIZE(bq2515x_ilim_lvl_values);
677 for (i = array_size - 1; i > 0; i--) {
678 if (val >= bq2515x_ilim_lvl_values[i])
681 return regmap_write(bq2515x->regmap, BQ2515X_ILIMCTRL, i);
684 static int bq2515x_power_supply_property_is_writeable(struct power_supply *psy,
685 enum power_supply_property prop)
688 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
689 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
690 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
691 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
698 static int bq2515x_charger_get_health(struct bq2515x_device *bq2515x,
699 union power_supply_propval *val)
701 int health = POWER_SUPPLY_HEALTH_GOOD;
706 if (!bq2515x->mains_online)
707 bq2515x_wake_up(bq2515x);
709 ret = regmap_read(bq2515x->regmap, BQ2515X_FLAG3, &flag3);
713 ret = regmap_read(bq2515x->regmap, BQ2515X_STAT1, &stat1);
717 if (stat1 & BQ2515X_HEALTH_MASK) {
718 switch (stat1 & BQ2515X_HEALTH_MASK) {
719 case BQ2515X_TS_HOT_STAT:
720 health = POWER_SUPPLY_HEALTH_HOT;
722 case BQ2515X_TS_WARM_STAT:
723 health = POWER_SUPPLY_HEALTH_WARM;
725 case BQ2515X_TS_COOL_STAT:
726 health = POWER_SUPPLY_HEALTH_COOL;
728 case BQ2515X_TS_COLD_STAT:
729 health = POWER_SUPPLY_HEALTH_COLD;
732 health = POWER_SUPPLY_HEALTH_UNKNOWN;
737 if (stat1 & BQ2515X_VIN_OVP_FAULT_STAT)
738 health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
740 if (flag3 & BQ2515X_SAFETY_TIMER_EXP)
741 health = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
743 val->intval = health;
747 static int bq2515x_mains_set_property(struct power_supply *psy,
748 enum power_supply_property prop,
749 const union power_supply_propval *val)
751 struct bq2515x_device *bq2515x = power_supply_get_drvdata(psy);
755 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
756 ret = bq2515x_set_batt_reg(bq2515x, val->intval);
759 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
760 ret = bq2515x_set_const_charge_current(bq2515x, val->intval);
763 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
764 ret = bq2515x_set_ilim_lvl(bq2515x, val->intval);
767 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
768 ret = bq2515x_set_precharge_current(bq2515x, val->intval);
778 static int bq2515x_mains_get_property(struct power_supply *psy,
779 enum power_supply_property prop,
780 union power_supply_propval *val)
782 struct bq2515x_device *bq2515x = power_supply_get_drvdata(psy);
787 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
788 ret = bq2515x_get_const_charge_current(bq2515x);
795 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
796 ret = bq2515x_get_batt_reg(bq2515x);
802 case POWER_SUPPLY_PROP_PRECHARGE_CURRENT:
803 ret = bq2515x_get_precharge_current(bq2515x);
809 case POWER_SUPPLY_PROP_ONLINE:
810 val->intval = bq2515x->mains_online;
813 case POWER_SUPPLY_PROP_HEALTH:
814 ret = bq2515x_charger_get_health(bq2515x, val);
816 val->intval = POWER_SUPPLY_HEALTH_UNKNOWN;
819 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
820 ret = bq2515x_get_ilim_lvl(bq2515x);
826 case POWER_SUPPLY_PROP_MODEL_NAME:
827 val->strval = bq2515x->model_name;
830 case POWER_SUPPLY_PROP_MANUFACTURER:
831 val->strval = BQ2515X_MANUFACTURER;
834 case POWER_SUPPLY_PROP_STATUS:
835 ret = bq2515x_charging_status(bq2515x, val);
847 static int bq2515x_battery_get_property(struct power_supply *psy,
848 enum power_supply_property prop,
849 union power_supply_propval *val)
851 struct bq2515x_device *bq2515x = power_supply_get_drvdata(psy);
854 ret = bq2515x_update_ps_status(bq2515x);
860 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
861 ret = bq2515x->init_data.vbatreg;
867 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
868 ret = bq2515x->init_data.ichg;
874 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
875 ret = bq2515x_get_battery_voltage_now(bq2515x);
881 case POWER_SUPPLY_PROP_CURRENT_NOW:
882 ret = bq2515x_get_battery_current_now(bq2515x);
894 static const enum power_supply_property bq2515x_battery_properties[] = {
895 POWER_SUPPLY_PROP_VOLTAGE_NOW,
896 POWER_SUPPLY_PROP_CURRENT_NOW,
897 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
898 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
901 static const enum power_supply_property bq2515x_mains_properties[] = {
902 POWER_SUPPLY_PROP_ONLINE,
903 POWER_SUPPLY_PROP_STATUS,
904 POWER_SUPPLY_PROP_HEALTH,
905 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
906 POWER_SUPPLY_PROP_MODEL_NAME,
907 POWER_SUPPLY_PROP_MANUFACTURER,
908 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
909 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
910 POWER_SUPPLY_PROP_PRECHARGE_CURRENT,
913 static const struct power_supply_desc bq2515x_mains_desc = {
914 .name = "bq2515x-mains",
915 .type = POWER_SUPPLY_TYPE_MAINS,
916 .get_property = bq2515x_mains_get_property,
917 .set_property = bq2515x_mains_set_property,
918 .properties = bq2515x_mains_properties,
919 .num_properties = ARRAY_SIZE(bq2515x_mains_properties),
920 .property_is_writeable = bq2515x_power_supply_property_is_writeable,
923 static const struct power_supply_desc bq2515x_battery_desc = {
924 .name = "bq2515x-battery",
925 .type = POWER_SUPPLY_TYPE_BATTERY,
926 .get_property = bq2515x_battery_get_property,
927 .properties = bq2515x_battery_properties,
928 .num_properties = ARRAY_SIZE(bq2515x_battery_properties),
929 .property_is_writeable = bq2515x_power_supply_property_is_writeable,
932 static int bq2515x_power_supply_register(struct bq2515x_device *bq2515x,
933 struct device *dev, struct power_supply_config psy_cfg)
935 bq2515x->mains = devm_power_supply_register(bq2515x->dev,
938 if (IS_ERR(bq2515x->mains))
941 bq2515x->battery = devm_power_supply_register(bq2515x->dev,
942 &bq2515x_battery_desc,
944 if (IS_ERR(bq2515x->battery))
950 static int bq2515x_hw_init(struct bq2515x_device *bq2515x)
953 struct power_supply_battery_info *bat_info;
955 ret = bq2515x_disable_watchdog_timers(bq2515x);
959 if (bq2515x->init_data.ilim) {
960 ret = bq2515x_set_ilim_lvl(bq2515x, bq2515x->init_data.ilim);
965 ret = power_supply_get_battery_info(bq2515x->mains, &bat_info);
967 dev_warn(bq2515x->dev, "battery info missing, default values will be applied\n");
969 bq2515x->init_data.ichg = BQ2515X_DEFAULT_ICHG_UA;
971 bq2515x->init_data.vbatreg = BQ2515X_DEFAULT_VBAT_REG_UV;
973 bq2515x->init_data.iprechg = BQ2515X_DEFAULT_IPRECHARGE_UA;
976 bq2515x->init_data.ichg =
977 bat_info->constant_charge_current_max_ua;
979 bq2515x->init_data.vbatreg =
980 bat_info->constant_charge_voltage_max_uv;
982 bq2515x->init_data.iprechg =
983 bat_info->precharge_current_ua;
986 ret = bq2515x_set_const_charge_current(bq2515x,
987 bq2515x->init_data.ichg);
991 ret = bq2515x_set_batt_reg(bq2515x, bq2515x->init_data.vbatreg);
995 return bq2515x_set_precharge_current(bq2515x,
996 bq2515x->init_data.iprechg);
999 static int bq2515x_read_properties(struct bq2515x_device *bq2515x)
1003 ret = device_property_read_u32(bq2515x->dev,
1004 "input-current-limit-microamp",
1005 &bq2515x->init_data.ilim);
1007 bq2515x->init_data.ilim = bq2515x->info->ilim;
1009 bq2515x->ac_detect_gpio = devm_gpiod_get_optional(bq2515x->dev,
1010 "ac-detect", GPIOD_IN);
1011 if (IS_ERR(bq2515x->ac_detect_gpio)) {
1012 ret = PTR_ERR(bq2515x->ac_detect_gpio);
1013 dev_err(bq2515x->dev, "Failed to get ac detect");
1017 bq2515x->reset_gpio = devm_gpiod_get_optional(bq2515x->dev,
1018 "reset", GPIOD_OUT_LOW);
1019 if (IS_ERR(bq2515x->reset_gpio)) {
1020 ret = PTR_ERR(bq2515x->reset_gpio);
1021 dev_err(bq2515x->dev, "Failed to get reset");
1025 bq2515x->powerdown_gpio = devm_gpiod_get_optional(bq2515x->dev,
1026 "powerdown", GPIOD_OUT_LOW);
1027 if (IS_ERR(bq2515x->powerdown_gpio)) {
1028 ret = PTR_ERR(bq2515x->powerdown_gpio);
1029 dev_err(bq2515x->dev, "Failed to get powerdown");
1033 bq2515x->ce_gpio = devm_gpiod_get_optional(bq2515x->dev,
1036 if (IS_ERR(bq2515x->ce_gpio)) {
1037 ret = PTR_ERR(bq2515x->ce_gpio);
1038 dev_err(bq2515x->dev, "Failed to get ce");
1045 static bool bq2515x_volatile_register(struct device *dev, unsigned int reg)
1048 case BQ2515X_STAT0 ... BQ2515X_FLAG3:
1049 case BQ2515X_ADC_VBAT_M ... BQ2515X_ADC_IIN_L:
1056 static const struct regmap_config bq25150_regmap_config = {
1060 .max_register = BQ2515X_DEVICE_ID,
1061 .reg_defaults = bq25150_reg_defaults,
1062 .num_reg_defaults = ARRAY_SIZE(bq25150_reg_defaults),
1063 .cache_type = REGCACHE_RBTREE,
1064 .volatile_reg = bq2515x_volatile_register,
1067 static const struct regmap_config bq25155_regmap_config = {
1071 .max_register = BQ2515X_DEVICE_ID,
1072 .reg_defaults = bq25155_reg_defaults,
1073 .num_reg_defaults = ARRAY_SIZE(bq25155_reg_defaults),
1074 .cache_type = REGCACHE_RBTREE,
1075 .volatile_reg = bq2515x_volatile_register,
1078 static int bq2515x_probe(struct i2c_client *client)
1080 const struct i2c_device_id *id = i2c_client_get_device_id(client);
1081 struct device *dev = &client->dev;
1082 struct bq2515x_device *bq2515x;
1083 struct power_supply_config charger_cfg = {};
1086 bq2515x = devm_kzalloc(dev, sizeof(*bq2515x), GFP_KERNEL);
1092 strscpy(bq2515x->model_name, id->name, sizeof(bq2515x->model_name));
1094 bq2515x->info = i2c_get_match_data(client);
1095 bq2515x->regmap = devm_regmap_init_i2c(client,
1096 bq2515x->info->regmap_config);
1097 if (IS_ERR(bq2515x->regmap)) {
1098 dev_err(dev, "failed to allocate register map\n");
1099 return PTR_ERR(bq2515x->regmap);
1102 i2c_set_clientdata(client, bq2515x);
1104 charger_cfg.drv_data = bq2515x;
1105 charger_cfg.of_node = dev->of_node;
1107 ret = bq2515x_read_properties(bq2515x);
1109 dev_err(dev, "Failed to read device tree properties %d\n",
1114 ret = bq2515x_power_supply_register(bq2515x, dev, charger_cfg);
1116 dev_err(dev, "failed to register power supply\n");
1120 ret = bq2515x_hw_init(bq2515x);
1122 dev_err(dev, "Cannot initialize the chip\n");
1129 static const struct bq2515x_info bq25150 = {
1130 .regmap_config = &bq25150_regmap_config,
1131 .ilim = BQ25150_DEFAULT_ILIM_UA,
1134 static const struct bq2515x_info bq25155 = {
1135 .regmap_config = &bq25155_regmap_config,
1136 .ilim = BQ25155_DEFAULT_ILIM_UA,
1139 static const struct i2c_device_id bq2515x_i2c_ids[] = {
1140 { "bq25150", (kernel_ulong_t)&bq25150 },
1141 { "bq25155", (kernel_ulong_t)&bq25155 },
1144 MODULE_DEVICE_TABLE(i2c, bq2515x_i2c_ids);
1146 static const struct of_device_id bq2515x_of_match[] = {
1147 { .compatible = "ti,bq25150", .data = &bq25150 },
1148 { .compatible = "ti,bq25155", .data = &bq25155 },
1151 MODULE_DEVICE_TABLE(of, bq2515x_of_match);
1153 static struct i2c_driver bq2515x_driver = {
1155 .name = "bq2515x-charger",
1156 .of_match_table = bq2515x_of_match,
1158 .probe = bq2515x_probe,
1159 .id_table = bq2515x_i2c_ids,
1161 module_i2c_driver(bq2515x_driver);
1163 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
1164 MODULE_AUTHOR("Ricardo Rivera-Matos <r-rivera-matos@ti.com>");
1165 MODULE_DESCRIPTION("BQ2515X charger driver");
1166 MODULE_LICENSE("GPL v2");