1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Richtek RT9455WSC battery charger.
5 * Copyright (C) 2015 Intel Corporation
8 #include <linux/module.h>
9 #include <linux/interrupt.h>
10 #include <linux/delay.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/power_supply.h>
14 #include <linux/i2c.h>
15 #include <linux/acpi.h>
16 #include <linux/usb/phy.h>
17 #include <linux/regmap.h>
19 #define RT9455_MANUFACTURER "Richtek"
20 #define RT9455_MODEL_NAME "RT9455"
21 #define RT9455_DRIVER_NAME "rt9455-charger"
23 #define RT9455_IRQ_NAME "interrupt"
25 #define RT9455_PWR_RDY_DELAY 1 /* 1 second */
26 #define RT9455_MAX_CHARGING_TIME 21600 /* 6 hrs */
27 #define RT9455_BATT_PRESENCE_DELAY 60 /* 60 seconds */
29 #define RT9455_CHARGE_MODE 0x00
30 #define RT9455_BOOST_MODE 0x01
32 #define RT9455_FAULT 0x03
34 #define RT9455_IAICR_100MA 0x00
35 #define RT9455_IAICR_500MA 0x01
36 #define RT9455_IAICR_NO_LIMIT 0x03
38 #define RT9455_CHARGE_DISABLE 0x00
39 #define RT9455_CHARGE_ENABLE 0x01
41 #define RT9455_PWR_FAULT 0x00
42 #define RT9455_PWR_GOOD 0x01
44 #define RT9455_REG_CTRL1 0x00 /* CTRL1 reg address */
45 #define RT9455_REG_CTRL2 0x01 /* CTRL2 reg address */
46 #define RT9455_REG_CTRL3 0x02 /* CTRL3 reg address */
47 #define RT9455_REG_DEV_ID 0x03 /* DEV_ID reg address */
48 #define RT9455_REG_CTRL4 0x04 /* CTRL4 reg address */
49 #define RT9455_REG_CTRL5 0x05 /* CTRL5 reg address */
50 #define RT9455_REG_CTRL6 0x06 /* CTRL6 reg address */
51 #define RT9455_REG_CTRL7 0x07 /* CTRL7 reg address */
52 #define RT9455_REG_IRQ1 0x08 /* IRQ1 reg address */
53 #define RT9455_REG_IRQ2 0x09 /* IRQ2 reg address */
54 #define RT9455_REG_IRQ3 0x0A /* IRQ3 reg address */
55 #define RT9455_REG_MASK1 0x0B /* MASK1 reg address */
56 #define RT9455_REG_MASK2 0x0C /* MASK2 reg address */
57 #define RT9455_REG_MASK3 0x0D /* MASK3 reg address */
60 F_STAT
, F_BOOST
, F_PWR_RDY
, F_OTG_PIN_POLARITY
, /* CTRL1 reg fields */
62 F_IAICR
, F_TE_SHDN_EN
, F_HIGHER_OCP
, F_TE
, F_IAICR_INT
, F_HIZ
,
63 F_OPA_MODE
, /* CTRL2 reg fields */
65 F_VOREG
, F_OTG_PL
, F_OTG_EN
, /* CTRL3 reg fields */
67 F_VENDOR_ID
, F_CHIP_REV
, /* DEV_ID reg fields */
69 F_RST
, /* CTRL4 reg fields */
71 F_TMR_EN
, F_MIVR
, F_IPREC
, F_IEOC_PERCENTAGE
, /* CTRL5 reg fields*/
73 F_IAICR_SEL
, F_ICHRG
, F_VPREC
, /* CTRL6 reg fields */
75 F_BATD_EN
, F_CHG_EN
, F_VMREG
, /* CTRL7 reg fields */
77 F_TSDI
, F_VINOVPI
, F_BATAB
, /* IRQ1 reg fields */
79 F_CHRVPI
, F_CHBATOVI
, F_CHTERMI
, F_CHRCHGI
, F_CH32MI
, F_CHTREGI
,
80 F_CHMIVRI
, /* IRQ2 reg fields */
82 F_BSTBUSOVI
, F_BSTOLI
, F_BSTLOWVI
, F_BST32SI
, /* IRQ3 reg fields */
84 F_TSDM
, F_VINOVPIM
, F_BATABM
, /* MASK1 reg fields */
86 F_CHRVPIM
, F_CHBATOVIM
, F_CHTERMIM
, F_CHRCHGIM
, F_CH32MIM
, F_CHTREGIM
,
87 F_CHMIVRIM
, /* MASK2 reg fields */
89 F_BSTVINOVIM
, F_BSTOLIM
, F_BSTLOWVIM
, F_BST32SIM
, /* MASK3 reg fields */
94 static const struct reg_field rt9455_reg_fields
[] = {
95 [F_STAT
] = REG_FIELD(RT9455_REG_CTRL1
, 4, 5),
96 [F_BOOST
] = REG_FIELD(RT9455_REG_CTRL1
, 3, 3),
97 [F_PWR_RDY
] = REG_FIELD(RT9455_REG_CTRL1
, 2, 2),
98 [F_OTG_PIN_POLARITY
] = REG_FIELD(RT9455_REG_CTRL1
, 1, 1),
100 [F_IAICR
] = REG_FIELD(RT9455_REG_CTRL2
, 6, 7),
101 [F_TE_SHDN_EN
] = REG_FIELD(RT9455_REG_CTRL2
, 5, 5),
102 [F_HIGHER_OCP
] = REG_FIELD(RT9455_REG_CTRL2
, 4, 4),
103 [F_TE
] = REG_FIELD(RT9455_REG_CTRL2
, 3, 3),
104 [F_IAICR_INT
] = REG_FIELD(RT9455_REG_CTRL2
, 2, 2),
105 [F_HIZ
] = REG_FIELD(RT9455_REG_CTRL2
, 1, 1),
106 [F_OPA_MODE
] = REG_FIELD(RT9455_REG_CTRL2
, 0, 0),
108 [F_VOREG
] = REG_FIELD(RT9455_REG_CTRL3
, 2, 7),
109 [F_OTG_PL
] = REG_FIELD(RT9455_REG_CTRL3
, 1, 1),
110 [F_OTG_EN
] = REG_FIELD(RT9455_REG_CTRL3
, 0, 0),
112 [F_VENDOR_ID
] = REG_FIELD(RT9455_REG_DEV_ID
, 4, 7),
113 [F_CHIP_REV
] = REG_FIELD(RT9455_REG_DEV_ID
, 0, 3),
115 [F_RST
] = REG_FIELD(RT9455_REG_CTRL4
, 7, 7),
117 [F_TMR_EN
] = REG_FIELD(RT9455_REG_CTRL5
, 7, 7),
118 [F_MIVR
] = REG_FIELD(RT9455_REG_CTRL5
, 4, 5),
119 [F_IPREC
] = REG_FIELD(RT9455_REG_CTRL5
, 2, 3),
120 [F_IEOC_PERCENTAGE
] = REG_FIELD(RT9455_REG_CTRL5
, 0, 1),
122 [F_IAICR_SEL
] = REG_FIELD(RT9455_REG_CTRL6
, 7, 7),
123 [F_ICHRG
] = REG_FIELD(RT9455_REG_CTRL6
, 4, 6),
124 [F_VPREC
] = REG_FIELD(RT9455_REG_CTRL6
, 0, 2),
126 [F_BATD_EN
] = REG_FIELD(RT9455_REG_CTRL7
, 6, 6),
127 [F_CHG_EN
] = REG_FIELD(RT9455_REG_CTRL7
, 4, 4),
128 [F_VMREG
] = REG_FIELD(RT9455_REG_CTRL7
, 0, 3),
130 [F_TSDI
] = REG_FIELD(RT9455_REG_IRQ1
, 7, 7),
131 [F_VINOVPI
] = REG_FIELD(RT9455_REG_IRQ1
, 6, 6),
132 [F_BATAB
] = REG_FIELD(RT9455_REG_IRQ1
, 0, 0),
134 [F_CHRVPI
] = REG_FIELD(RT9455_REG_IRQ2
, 7, 7),
135 [F_CHBATOVI
] = REG_FIELD(RT9455_REG_IRQ2
, 5, 5),
136 [F_CHTERMI
] = REG_FIELD(RT9455_REG_IRQ2
, 4, 4),
137 [F_CHRCHGI
] = REG_FIELD(RT9455_REG_IRQ2
, 3, 3),
138 [F_CH32MI
] = REG_FIELD(RT9455_REG_IRQ2
, 2, 2),
139 [F_CHTREGI
] = REG_FIELD(RT9455_REG_IRQ2
, 1, 1),
140 [F_CHMIVRI
] = REG_FIELD(RT9455_REG_IRQ2
, 0, 0),
142 [F_BSTBUSOVI
] = REG_FIELD(RT9455_REG_IRQ3
, 7, 7),
143 [F_BSTOLI
] = REG_FIELD(RT9455_REG_IRQ3
, 6, 6),
144 [F_BSTLOWVI
] = REG_FIELD(RT9455_REG_IRQ3
, 5, 5),
145 [F_BST32SI
] = REG_FIELD(RT9455_REG_IRQ3
, 3, 3),
147 [F_TSDM
] = REG_FIELD(RT9455_REG_MASK1
, 7, 7),
148 [F_VINOVPIM
] = REG_FIELD(RT9455_REG_MASK1
, 6, 6),
149 [F_BATABM
] = REG_FIELD(RT9455_REG_MASK1
, 0, 0),
151 [F_CHRVPIM
] = REG_FIELD(RT9455_REG_MASK2
, 7, 7),
152 [F_CHBATOVIM
] = REG_FIELD(RT9455_REG_MASK2
, 5, 5),
153 [F_CHTERMIM
] = REG_FIELD(RT9455_REG_MASK2
, 4, 4),
154 [F_CHRCHGIM
] = REG_FIELD(RT9455_REG_MASK2
, 3, 3),
155 [F_CH32MIM
] = REG_FIELD(RT9455_REG_MASK2
, 2, 2),
156 [F_CHTREGIM
] = REG_FIELD(RT9455_REG_MASK2
, 1, 1),
157 [F_CHMIVRIM
] = REG_FIELD(RT9455_REG_MASK2
, 0, 0),
159 [F_BSTVINOVIM
] = REG_FIELD(RT9455_REG_MASK3
, 7, 7),
160 [F_BSTOLIM
] = REG_FIELD(RT9455_REG_MASK3
, 6, 6),
161 [F_BSTLOWVIM
] = REG_FIELD(RT9455_REG_MASK3
, 5, 5),
162 [F_BST32SIM
] = REG_FIELD(RT9455_REG_MASK3
, 3, 3),
165 #define GET_MASK(fid) (BIT(rt9455_reg_fields[fid].msb + 1) - \
166 BIT(rt9455_reg_fields[fid].lsb))
169 * Each array initialised below shows the possible real-world values for a
170 * group of bits belonging to RT9455 registers. The arrays are sorted in
171 * ascending order. The index of each real-world value represents the value
172 * that is encoded in the group of bits belonging to RT9455 registers.
174 /* REG06[6:4] (ICHRG) in uAh */
175 static const int rt9455_ichrg_values
[] = {
176 500000, 650000, 800000, 950000, 1100000, 1250000, 1400000, 1550000
180 * When the charger is in charge mode, REG02[7:2] represent battery regulation
183 /* REG02[7:2] (VOREG) in uV */
184 static const int rt9455_voreg_values
[] = {
185 3500000, 3520000, 3540000, 3560000, 3580000, 3600000, 3620000, 3640000,
186 3660000, 3680000, 3700000, 3720000, 3740000, 3760000, 3780000, 3800000,
187 3820000, 3840000, 3860000, 3880000, 3900000, 3920000, 3940000, 3960000,
188 3980000, 4000000, 4020000, 4040000, 4060000, 4080000, 4100000, 4120000,
189 4140000, 4160000, 4180000, 4200000, 4220000, 4240000, 4260000, 4280000,
190 4300000, 4330000, 4350000, 4370000, 4390000, 4410000, 4430000, 4450000,
191 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000,
192 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000
195 #if IS_ENABLED(CONFIG_USB_PHY)
197 * When the charger is in boost mode, REG02[7:2] represent boost output
200 /* REG02[7:2] (Boost output voltage) in uV */
201 static const int rt9455_boost_voltage_values
[] = {
202 4425000, 4450000, 4475000, 4500000, 4525000, 4550000, 4575000, 4600000,
203 4625000, 4650000, 4675000, 4700000, 4725000, 4750000, 4775000, 4800000,
204 4825000, 4850000, 4875000, 4900000, 4925000, 4950000, 4975000, 5000000,
205 5025000, 5050000, 5075000, 5100000, 5125000, 5150000, 5175000, 5200000,
206 5225000, 5250000, 5275000, 5300000, 5325000, 5350000, 5375000, 5400000,
207 5425000, 5450000, 5475000, 5500000, 5525000, 5550000, 5575000, 5600000,
208 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000,
209 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000,
213 /* REG07[3:0] (VMREG) in uV */
214 static const int rt9455_vmreg_values
[] = {
215 4200000, 4220000, 4240000, 4260000, 4280000, 4300000, 4320000, 4340000,
216 4360000, 4380000, 4400000, 4430000, 4450000, 4450000, 4450000, 4450000
219 /* REG05[5:4] (IEOC_PERCENTAGE) */
220 static const int rt9455_ieoc_percentage_values
[] = {
224 /* REG05[1:0] (MIVR) in uV */
225 static const int rt9455_mivr_values
[] = {
226 4000000, 4250000, 4500000, 5000000
229 /* REG05[1:0] (IAICR) in uA */
230 static const int rt9455_iaicr_values
[] = {
231 100000, 500000, 1000000, 2000000
235 struct i2c_client
*client
;
236 struct regmap
*regmap
;
237 struct regmap_field
*regmap_fields
[F_MAX_FIELDS
];
238 struct power_supply
*charger
;
239 #if IS_ENABLED(CONFIG_USB_PHY)
240 struct usb_phy
*usb_phy
;
241 struct notifier_block nb
;
243 struct delayed_work pwr_rdy_work
;
244 struct delayed_work max_charging_time_work
;
245 struct delayed_work batt_presence_work
;
251 * Iterate through each element of the 'tbl' array until an element whose value
252 * is greater than v is found. Return the index of the respective element,
253 * or the index of the last element in the array, if no such element is found.
255 static unsigned int rt9455_find_idx(const int tbl
[], int tbl_size
, int v
)
260 * No need to iterate until the last index in the table because
261 * if no element greater than v is found in the table,
262 * or if only the last element is greater than v,
263 * function returns the index of the last element.
265 for (i
= 0; i
< tbl_size
- 1; i
++)
269 return (tbl_size
- 1);
272 static int rt9455_get_field_val(struct rt9455_info
*info
,
273 enum rt9455_fields field
,
274 const int tbl
[], int tbl_size
, int *val
)
279 ret
= regmap_field_read(info
->regmap_fields
[field
], &v
);
283 v
= (v
>= tbl_size
) ? (tbl_size
- 1) : v
;
289 static int rt9455_set_field_val(struct rt9455_info
*info
,
290 enum rt9455_fields field
,
291 const int tbl
[], int tbl_size
, int val
)
293 unsigned int idx
= rt9455_find_idx(tbl
, tbl_size
, val
);
295 return regmap_field_write(info
->regmap_fields
[field
], idx
);
298 static int rt9455_register_reset(struct rt9455_info
*info
)
300 struct device
*dev
= &info
->client
->dev
;
302 int ret
, limit
= 100;
304 ret
= regmap_field_write(info
->regmap_fields
[F_RST
], 0x01);
306 dev_err(dev
, "Failed to set RST bit\n");
311 * To make sure that reset operation has finished, loop until RST bit
315 ret
= regmap_field_read(info
->regmap_fields
[F_RST
], &v
);
317 dev_err(dev
, "Failed to read RST bit\n");
324 usleep_range(10, 100);
333 /* Charger power supply property routines */
334 static enum power_supply_property rt9455_charger_properties
[] = {
335 POWER_SUPPLY_PROP_STATUS
,
336 POWER_SUPPLY_PROP_HEALTH
,
337 POWER_SUPPLY_PROP_PRESENT
,
338 POWER_SUPPLY_PROP_ONLINE
,
339 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
,
340 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
,
341 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
,
342 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
,
343 POWER_SUPPLY_PROP_SCOPE
,
344 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT
,
345 POWER_SUPPLY_PROP_MODEL_NAME
,
346 POWER_SUPPLY_PROP_MANUFACTURER
,
349 static char *rt9455_charger_supplied_to
[] = {
353 static int rt9455_charger_get_status(struct rt9455_info
*info
,
354 union power_supply_propval
*val
)
356 unsigned int v
, pwr_rdy
;
359 ret
= regmap_field_read(info
->regmap_fields
[F_PWR_RDY
],
362 dev_err(&info
->client
->dev
, "Failed to read PWR_RDY bit\n");
367 * If PWR_RDY bit is unset, the battery is discharging. Otherwise,
368 * STAT bits value must be checked.
371 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
375 ret
= regmap_field_read(info
->regmap_fields
[F_STAT
], &v
);
377 dev_err(&info
->client
->dev
, "Failed to read STAT bits\n");
384 * If PWR_RDY bit is set, but STAT bits value is 0, the charger
385 * may be in one of the following cases:
386 * 1. CHG_EN bit is 0.
387 * 2. CHG_EN bit is 1 but the battery is not connected.
388 * In any of these cases, POWER_SUPPLY_STATUS_NOT_CHARGING is
391 val
->intval
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
394 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
397 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
400 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
405 static int rt9455_charger_get_health(struct rt9455_info
*info
,
406 union power_supply_propval
*val
)
408 struct device
*dev
= &info
->client
->dev
;
412 val
->intval
= POWER_SUPPLY_HEALTH_GOOD
;
414 ret
= regmap_read(info
->regmap
, RT9455_REG_IRQ1
, &v
);
416 dev_err(dev
, "Failed to read IRQ1 register\n");
420 if (v
& GET_MASK(F_TSDI
)) {
421 val
->intval
= POWER_SUPPLY_HEALTH_OVERHEAT
;
424 if (v
& GET_MASK(F_VINOVPI
)) {
425 val
->intval
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
428 if (v
& GET_MASK(F_BATAB
)) {
429 val
->intval
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
433 ret
= regmap_read(info
->regmap
, RT9455_REG_IRQ2
, &v
);
435 dev_err(dev
, "Failed to read IRQ2 register\n");
439 if (v
& GET_MASK(F_CHBATOVI
)) {
440 val
->intval
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
443 if (v
& GET_MASK(F_CH32MI
)) {
444 val
->intval
= POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE
;
448 ret
= regmap_read(info
->regmap
, RT9455_REG_IRQ3
, &v
);
450 dev_err(dev
, "Failed to read IRQ3 register\n");
454 if (v
& GET_MASK(F_BSTBUSOVI
)) {
455 val
->intval
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
458 if (v
& GET_MASK(F_BSTOLI
)) {
459 val
->intval
= POWER_SUPPLY_HEALTH_OVERVOLTAGE
;
462 if (v
& GET_MASK(F_BSTLOWVI
)) {
463 val
->intval
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
466 if (v
& GET_MASK(F_BST32SI
)) {
467 val
->intval
= POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE
;
471 ret
= regmap_field_read(info
->regmap_fields
[F_STAT
], &v
);
473 dev_err(dev
, "Failed to read STAT bits\n");
477 if (v
== RT9455_FAULT
) {
478 val
->intval
= POWER_SUPPLY_HEALTH_UNSPEC_FAILURE
;
485 static int rt9455_charger_get_battery_presence(struct rt9455_info
*info
,
486 union power_supply_propval
*val
)
491 ret
= regmap_field_read(info
->regmap_fields
[F_BATAB
], &v
);
493 dev_err(&info
->client
->dev
, "Failed to read BATAB bit\n");
498 * Since BATAB is 1 when battery is NOT present and 0 otherwise,
499 * !BATAB is returned.
506 static int rt9455_charger_get_online(struct rt9455_info
*info
,
507 union power_supply_propval
*val
)
512 ret
= regmap_field_read(info
->regmap_fields
[F_PWR_RDY
], &v
);
514 dev_err(&info
->client
->dev
, "Failed to read PWR_RDY bit\n");
518 val
->intval
= (int)v
;
523 static int rt9455_charger_get_current(struct rt9455_info
*info
,
524 union power_supply_propval
*val
)
529 ret
= rt9455_get_field_val(info
, F_ICHRG
,
531 ARRAY_SIZE(rt9455_ichrg_values
),
534 dev_err(&info
->client
->dev
, "Failed to read ICHRG value\n");
543 static int rt9455_charger_get_current_max(struct rt9455_info
*info
,
544 union power_supply_propval
*val
)
546 int idx
= ARRAY_SIZE(rt9455_ichrg_values
) - 1;
548 val
->intval
= rt9455_ichrg_values
[idx
];
553 static int rt9455_charger_get_voltage(struct rt9455_info
*info
,
554 union power_supply_propval
*val
)
559 ret
= rt9455_get_field_val(info
, F_VOREG
,
561 ARRAY_SIZE(rt9455_voreg_values
),
564 dev_err(&info
->client
->dev
, "Failed to read VOREG value\n");
568 val
->intval
= voltage
;
573 static int rt9455_charger_get_voltage_max(struct rt9455_info
*info
,
574 union power_supply_propval
*val
)
576 int idx
= ARRAY_SIZE(rt9455_vmreg_values
) - 1;
578 val
->intval
= rt9455_vmreg_values
[idx
];
583 static int rt9455_charger_get_term_current(struct rt9455_info
*info
,
584 union power_supply_propval
*val
)
586 struct device
*dev
= &info
->client
->dev
;
587 int ichrg
, ieoc_percentage
, ret
;
589 ret
= rt9455_get_field_val(info
, F_ICHRG
,
591 ARRAY_SIZE(rt9455_ichrg_values
),
594 dev_err(dev
, "Failed to read ICHRG value\n");
598 ret
= rt9455_get_field_val(info
, F_IEOC_PERCENTAGE
,
599 rt9455_ieoc_percentage_values
,
600 ARRAY_SIZE(rt9455_ieoc_percentage_values
),
603 dev_err(dev
, "Failed to read IEOC value\n");
607 val
->intval
= ichrg
* ieoc_percentage
/ 100;
612 static int rt9455_charger_get_property(struct power_supply
*psy
,
613 enum power_supply_property psp
,
614 union power_supply_propval
*val
)
616 struct rt9455_info
*info
= power_supply_get_drvdata(psy
);
619 case POWER_SUPPLY_PROP_STATUS
:
620 return rt9455_charger_get_status(info
, val
);
621 case POWER_SUPPLY_PROP_HEALTH
:
622 return rt9455_charger_get_health(info
, val
);
623 case POWER_SUPPLY_PROP_PRESENT
:
624 return rt9455_charger_get_battery_presence(info
, val
);
625 case POWER_SUPPLY_PROP_ONLINE
:
626 return rt9455_charger_get_online(info
, val
);
627 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT
:
628 return rt9455_charger_get_current(info
, val
);
629 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX
:
630 return rt9455_charger_get_current_max(info
, val
);
631 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE
:
632 return rt9455_charger_get_voltage(info
, val
);
633 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX
:
634 return rt9455_charger_get_voltage_max(info
, val
);
635 case POWER_SUPPLY_PROP_SCOPE
:
636 val
->intval
= POWER_SUPPLY_SCOPE_SYSTEM
;
638 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT
:
639 return rt9455_charger_get_term_current(info
, val
);
640 case POWER_SUPPLY_PROP_MODEL_NAME
:
641 val
->strval
= RT9455_MODEL_NAME
;
643 case POWER_SUPPLY_PROP_MANUFACTURER
:
644 val
->strval
= RT9455_MANUFACTURER
;
651 static int rt9455_hw_init(struct rt9455_info
*info
, u32 ichrg
,
655 struct device
*dev
= &info
->client
->dev
;
658 ret
= rt9455_register_reset(info
);
660 dev_err(dev
, "Power On Reset failed\n");
664 /* Set TE bit in order to enable end of charge detection */
665 ret
= regmap_field_write(info
->regmap_fields
[F_TE
], 1);
667 dev_err(dev
, "Failed to set TE bit\n");
671 /* Set TE_SHDN_EN bit in order to enable end of charge detection */
672 ret
= regmap_field_write(info
->regmap_fields
[F_TE_SHDN_EN
], 1);
674 dev_err(dev
, "Failed to set TE_SHDN_EN bit\n");
679 * Set BATD_EN bit in order to enable battery detection
680 * when charging is done
682 ret
= regmap_field_write(info
->regmap_fields
[F_BATD_EN
], 1);
684 dev_err(dev
, "Failed to set BATD_EN bit\n");
689 * Disable Safety Timer. In charge mode, this timer terminates charging
690 * if no read or write via I2C is done within 32 minutes. This timer
691 * avoids overcharging the baterry when the OS is not loaded and the
692 * charger is connected to a power source.
693 * In boost mode, this timer triggers BST32SI interrupt if no read or
694 * write via I2C is done within 32 seconds.
695 * When the OS is loaded and the charger driver is inserted, it is used
696 * delayed_work, named max_charging_time_work, to avoid overcharging
699 ret
= regmap_field_write(info
->regmap_fields
[F_TMR_EN
], 0x00);
701 dev_err(dev
, "Failed to disable Safety Timer\n");
705 /* Set ICHRG to value retrieved from device-specific data */
706 ret
= rt9455_set_field_val(info
, F_ICHRG
,
708 ARRAY_SIZE(rt9455_ichrg_values
), ichrg
);
710 dev_err(dev
, "Failed to set ICHRG value\n");
714 /* Set IEOC Percentage to value retrieved from device-specific data */
715 ret
= rt9455_set_field_val(info
, F_IEOC_PERCENTAGE
,
716 rt9455_ieoc_percentage_values
,
717 ARRAY_SIZE(rt9455_ieoc_percentage_values
),
720 dev_err(dev
, "Failed to set IEOC Percentage value\n");
724 /* Set VOREG to value retrieved from device-specific data */
725 ret
= rt9455_set_field_val(info
, F_VOREG
,
727 ARRAY_SIZE(rt9455_voreg_values
),
730 dev_err(dev
, "Failed to set VOREG value\n");
734 /* Set VMREG value to maximum (4.45V). */
735 idx
= ARRAY_SIZE(rt9455_vmreg_values
) - 1;
736 ret
= rt9455_set_field_val(info
, F_VMREG
,
738 ARRAY_SIZE(rt9455_vmreg_values
),
739 rt9455_vmreg_values
[idx
]);
741 dev_err(dev
, "Failed to set VMREG value\n");
746 * Set MIVR to value retrieved from device-specific data.
747 * If no value is specified, default value for MIVR is 4.5V.
752 ret
= rt9455_set_field_val(info
, F_MIVR
,
754 ARRAY_SIZE(rt9455_mivr_values
), mivr
);
756 dev_err(dev
, "Failed to set MIVR value\n");
761 * Set IAICR to value retrieved from device-specific data.
762 * If no value is specified, default value for IAICR is 500 mA.
767 ret
= rt9455_set_field_val(info
, F_IAICR
,
769 ARRAY_SIZE(rt9455_iaicr_values
), iaicr
);
771 dev_err(dev
, "Failed to set IAICR value\n");
776 * Set IAICR_INT bit so that IAICR value is determined by IAICR bits
777 * and not by OTG pin.
779 ret
= regmap_field_write(info
->regmap_fields
[F_IAICR_INT
], 0x01);
781 dev_err(dev
, "Failed to set IAICR_INT bit\n");
786 * Disable CHMIVRI interrupt. Because the driver sets MIVR value,
787 * CHMIVRI is triggered, but there is no action to be taken by the
788 * driver when CHMIVRI is triggered.
790 ret
= regmap_field_write(info
->regmap_fields
[F_CHMIVRIM
], 0x01);
792 dev_err(dev
, "Failed to mask CHMIVRI interrupt\n");
799 #if IS_ENABLED(CONFIG_USB_PHY)
801 * Before setting the charger into boost mode, boost output voltage is
802 * set. This is needed because boost output voltage may differ from battery
803 * regulation voltage. F_VOREG bits represent either battery regulation voltage
804 * or boost output voltage, depending on the mode the charger is. Both battery
805 * regulation voltage and boost output voltage are read from DT/ACPI during
808 static int rt9455_set_boost_voltage_before_boost_mode(struct rt9455_info
*info
)
810 struct device
*dev
= &info
->client
->dev
;
813 ret
= rt9455_set_field_val(info
, F_VOREG
,
814 rt9455_boost_voltage_values
,
815 ARRAY_SIZE(rt9455_boost_voltage_values
),
816 info
->boost_voltage
);
818 dev_err(dev
, "Failed to set boost output voltage value\n");
827 * Before setting the charger into charge mode, battery regulation voltage is
828 * set. This is needed because boost output voltage may differ from battery
829 * regulation voltage. F_VOREG bits represent either battery regulation voltage
830 * or boost output voltage, depending on the mode the charger is. Both battery
831 * regulation voltage and boost output voltage are read from DT/ACPI during
834 static int rt9455_set_voreg_before_charge_mode(struct rt9455_info
*info
)
836 struct device
*dev
= &info
->client
->dev
;
839 ret
= rt9455_set_field_val(info
, F_VOREG
,
841 ARRAY_SIZE(rt9455_voreg_values
),
844 dev_err(dev
, "Failed to set VOREG value\n");
851 static int rt9455_irq_handler_check_irq1_register(struct rt9455_info
*info
,
852 bool *_is_battery_absent
,
853 bool *_alert_userspace
)
855 unsigned int irq1
, mask1
, mask2
;
856 struct device
*dev
= &info
->client
->dev
;
857 bool is_battery_absent
= false;
858 bool alert_userspace
= false;
861 ret
= regmap_read(info
->regmap
, RT9455_REG_IRQ1
, &irq1
);
863 dev_err(dev
, "Failed to read IRQ1 register\n");
867 ret
= regmap_read(info
->regmap
, RT9455_REG_MASK1
, &mask1
);
869 dev_err(dev
, "Failed to read MASK1 register\n");
873 if (irq1
& GET_MASK(F_TSDI
)) {
874 dev_err(dev
, "Thermal shutdown fault occurred\n");
875 alert_userspace
= true;
878 if (irq1
& GET_MASK(F_VINOVPI
)) {
879 dev_err(dev
, "Overvoltage input occurred\n");
880 alert_userspace
= true;
883 if (irq1
& GET_MASK(F_BATAB
)) {
884 dev_err(dev
, "Battery absence occurred\n");
885 is_battery_absent
= true;
886 alert_userspace
= true;
888 if ((mask1
& GET_MASK(F_BATABM
)) == 0) {
889 ret
= regmap_field_write(info
->regmap_fields
[F_BATABM
],
892 dev_err(dev
, "Failed to mask BATAB interrupt\n");
897 ret
= regmap_read(info
->regmap
, RT9455_REG_MASK2
, &mask2
);
899 dev_err(dev
, "Failed to read MASK2 register\n");
903 if (mask2
& GET_MASK(F_CHTERMIM
)) {
904 ret
= regmap_field_write(
905 info
->regmap_fields
[F_CHTERMIM
], 0x00);
907 dev_err(dev
, "Failed to unmask CHTERMI interrupt\n");
912 if (mask2
& GET_MASK(F_CHRCHGIM
)) {
913 ret
= regmap_field_write(
914 info
->regmap_fields
[F_CHRCHGIM
], 0x00);
916 dev_err(dev
, "Failed to unmask CHRCHGI interrupt\n");
922 * When the battery is absent, max_charging_time_work is
923 * cancelled, since no charging is done.
925 cancel_delayed_work_sync(&info
->max_charging_time_work
);
927 * Since no interrupt is triggered when the battery is
928 * reconnected, max_charging_time_work is not rescheduled.
929 * Therefore, batt_presence_work is scheduled to check whether
930 * the battery is still absent or not.
932 queue_delayed_work(system_power_efficient_wq
,
933 &info
->batt_presence_work
,
934 RT9455_BATT_PRESENCE_DELAY
* HZ
);
937 *_is_battery_absent
= is_battery_absent
;
940 *_alert_userspace
= alert_userspace
;
945 static int rt9455_irq_handler_check_irq2_register(struct rt9455_info
*info
,
946 bool is_battery_absent
,
947 bool *_alert_userspace
)
949 unsigned int irq2
, mask2
;
950 struct device
*dev
= &info
->client
->dev
;
951 bool alert_userspace
= false;
954 ret
= regmap_read(info
->regmap
, RT9455_REG_IRQ2
, &irq2
);
956 dev_err(dev
, "Failed to read IRQ2 register\n");
960 ret
= regmap_read(info
->regmap
, RT9455_REG_MASK2
, &mask2
);
962 dev_err(dev
, "Failed to read MASK2 register\n");
966 if (irq2
& GET_MASK(F_CHRVPI
)) {
967 dev_dbg(dev
, "Charger fault occurred\n");
969 * CHRVPI bit is set in 2 cases:
970 * 1. when the power source is connected to the charger.
971 * 2. when the power source is disconnected from the charger.
972 * To identify the case, PWR_RDY bit is checked. Because
973 * PWR_RDY bit is set / cleared after CHRVPI interrupt is
974 * triggered, it is used delayed_work to later read PWR_RDY bit.
975 * Also, do not set to true alert_userspace, because there is no
976 * need to notify userspace when CHRVPI interrupt has occurred.
977 * Userspace will be notified after PWR_RDY bit is read.
979 queue_delayed_work(system_power_efficient_wq
,
981 RT9455_PWR_RDY_DELAY
* HZ
);
983 if (irq2
& GET_MASK(F_CHBATOVI
)) {
984 dev_err(dev
, "Battery OVP occurred\n");
985 alert_userspace
= true;
987 if (irq2
& GET_MASK(F_CHTERMI
)) {
988 dev_dbg(dev
, "Charge terminated\n");
989 if (!is_battery_absent
) {
990 if ((mask2
& GET_MASK(F_CHTERMIM
)) == 0) {
991 ret
= regmap_field_write(
992 info
->regmap_fields
[F_CHTERMIM
], 0x01);
994 dev_err(dev
, "Failed to mask CHTERMI interrupt\n");
998 * Update MASK2 value, since CHTERMIM bit is
1001 mask2
= mask2
| GET_MASK(F_CHTERMIM
);
1003 cancel_delayed_work_sync(&info
->max_charging_time_work
);
1004 alert_userspace
= true;
1007 if (irq2
& GET_MASK(F_CHRCHGI
)) {
1008 dev_dbg(dev
, "Recharge request\n");
1009 ret
= regmap_field_write(info
->regmap_fields
[F_CHG_EN
],
1010 RT9455_CHARGE_ENABLE
);
1012 dev_err(dev
, "Failed to enable charging\n");
1015 if (mask2
& GET_MASK(F_CHTERMIM
)) {
1016 ret
= regmap_field_write(
1017 info
->regmap_fields
[F_CHTERMIM
], 0x00);
1019 dev_err(dev
, "Failed to unmask CHTERMI interrupt\n");
1022 /* Update MASK2 value, since CHTERMIM bit is cleared. */
1023 mask2
= mask2
& ~GET_MASK(F_CHTERMIM
);
1025 if (!is_battery_absent
) {
1027 * No need to check whether the charger is connected to
1028 * power source when CHRCHGI is received, since CHRCHGI
1029 * is not triggered if the charger is not connected to
1032 queue_delayed_work(system_power_efficient_wq
,
1033 &info
->max_charging_time_work
,
1034 RT9455_MAX_CHARGING_TIME
* HZ
);
1035 alert_userspace
= true;
1038 if (irq2
& GET_MASK(F_CH32MI
)) {
1039 dev_err(dev
, "Charger fault. 32 mins timeout occurred\n");
1040 alert_userspace
= true;
1042 if (irq2
& GET_MASK(F_CHTREGI
)) {
1044 "Charger warning. Thermal regulation loop active\n");
1045 alert_userspace
= true;
1047 if (irq2
& GET_MASK(F_CHMIVRI
)) {
1049 "Charger warning. Input voltage MIVR loop active\n");
1052 if (alert_userspace
)
1053 *_alert_userspace
= alert_userspace
;
1058 static int rt9455_irq_handler_check_irq3_register(struct rt9455_info
*info
,
1059 bool *_alert_userspace
)
1061 unsigned int irq3
, mask3
;
1062 struct device
*dev
= &info
->client
->dev
;
1063 bool alert_userspace
= false;
1066 ret
= regmap_read(info
->regmap
, RT9455_REG_IRQ3
, &irq3
);
1068 dev_err(dev
, "Failed to read IRQ3 register\n");
1072 ret
= regmap_read(info
->regmap
, RT9455_REG_MASK3
, &mask3
);
1074 dev_err(dev
, "Failed to read MASK3 register\n");
1078 if (irq3
& GET_MASK(F_BSTBUSOVI
)) {
1079 dev_err(dev
, "Boost fault. Overvoltage input occurred\n");
1080 alert_userspace
= true;
1082 if (irq3
& GET_MASK(F_BSTOLI
)) {
1083 dev_err(dev
, "Boost fault. Overload\n");
1084 alert_userspace
= true;
1086 if (irq3
& GET_MASK(F_BSTLOWVI
)) {
1087 dev_err(dev
, "Boost fault. Battery voltage too low\n");
1088 alert_userspace
= true;
1090 if (irq3
& GET_MASK(F_BST32SI
)) {
1091 dev_err(dev
, "Boost fault. 32 seconds timeout occurred.\n");
1092 alert_userspace
= true;
1095 if (alert_userspace
) {
1096 dev_info(dev
, "Boost fault occurred, therefore the charger goes into charge mode\n");
1097 ret
= rt9455_set_voreg_before_charge_mode(info
);
1099 dev_err(dev
, "Failed to set VOREG before entering charge mode\n");
1102 ret
= regmap_field_write(info
->regmap_fields
[F_OPA_MODE
],
1103 RT9455_CHARGE_MODE
);
1105 dev_err(dev
, "Failed to set charger in charge mode\n");
1108 *_alert_userspace
= alert_userspace
;
1114 static irqreturn_t
rt9455_irq_handler_thread(int irq
, void *data
)
1116 struct rt9455_info
*info
= data
;
1118 bool alert_userspace
= false;
1119 bool is_battery_absent
= false;
1120 unsigned int status
;
1126 dev
= &info
->client
->dev
;
1128 if (irq
!= info
->client
->irq
) {
1129 dev_err(dev
, "Interrupt is not for RT9455 charger\n");
1133 ret
= regmap_field_read(info
->regmap_fields
[F_STAT
], &status
);
1135 dev_err(dev
, "Failed to read STAT bits\n");
1138 dev_dbg(dev
, "Charger status is %d\n", status
);
1141 * Each function that processes an IRQ register receives as output
1142 * parameter alert_userspace pointer. alert_userspace is set to true
1143 * in such a function only if an interrupt has occurred in the
1144 * respective interrupt register. This way, it is avoided the following
1145 * case: interrupt occurs only in IRQ1 register,
1146 * rt9455_irq_handler_check_irq1_register() function sets to true
1147 * alert_userspace, but rt9455_irq_handler_check_irq2_register()
1148 * and rt9455_irq_handler_check_irq3_register() functions set to false
1149 * alert_userspace and power_supply_changed() is never called.
1151 ret
= rt9455_irq_handler_check_irq1_register(info
, &is_battery_absent
,
1154 dev_err(dev
, "Failed to handle IRQ1 register\n");
1158 ret
= rt9455_irq_handler_check_irq2_register(info
, is_battery_absent
,
1161 dev_err(dev
, "Failed to handle IRQ2 register\n");
1165 ret
= rt9455_irq_handler_check_irq3_register(info
, &alert_userspace
);
1167 dev_err(dev
, "Failed to handle IRQ3 register\n");
1171 if (alert_userspace
) {
1173 * Sometimes, an interrupt occurs while rt9455_probe() function
1174 * is executing and power_supply_register() is not yet called.
1175 * Do not call power_supply_changed() in this case.
1178 power_supply_changed(info
->charger
);
1184 static int rt9455_discover_charger(struct rt9455_info
*info
, u32
*ichrg
,
1185 u32
*ieoc_percentage
,
1186 u32
*mivr
, u32
*iaicr
)
1188 struct device
*dev
= &info
->client
->dev
;
1191 if (!dev
->of_node
&& !ACPI_HANDLE(dev
)) {
1192 dev_err(dev
, "No support for either device tree or ACPI\n");
1196 * ICHRG, IEOC_PERCENTAGE, VOREG and boost output voltage are mandatory
1199 ret
= device_property_read_u32(dev
, "richtek,output-charge-current",
1202 dev_err(dev
, "Error: missing \"output-charge-current\" property\n");
1206 ret
= device_property_read_u32(dev
, "richtek,end-of-charge-percentage",
1209 dev_err(dev
, "Error: missing \"end-of-charge-percentage\" property\n");
1213 ret
= device_property_read_u32(dev
,
1214 "richtek,battery-regulation-voltage",
1217 dev_err(dev
, "Error: missing \"battery-regulation-voltage\" property\n");
1221 ret
= device_property_read_u32(dev
, "richtek,boost-output-voltage",
1222 &info
->boost_voltage
);
1224 dev_err(dev
, "Error: missing \"boost-output-voltage\" property\n");
1229 * MIVR and IAICR are optional parameters. Do not return error if one of
1230 * them is not present in ACPI table or device tree specification.
1232 device_property_read_u32(dev
, "richtek,min-input-voltage-regulation",
1234 device_property_read_u32(dev
, "richtek,avg-input-current-regulation",
1240 #if IS_ENABLED(CONFIG_USB_PHY)
1241 static int rt9455_usb_event_none(struct rt9455_info
*info
,
1242 u8 opa_mode
, u8 iaicr
)
1244 struct device
*dev
= &info
->client
->dev
;
1247 if (opa_mode
== RT9455_BOOST_MODE
) {
1248 ret
= rt9455_set_voreg_before_charge_mode(info
);
1250 dev_err(dev
, "Failed to set VOREG before entering charge mode\n");
1254 * If the charger is in boost mode, and it has received
1255 * USB_EVENT_NONE, this means the consumer device powered by the
1256 * charger is not connected anymore.
1257 * In this case, the charger goes into charge mode.
1259 dev_dbg(dev
, "USB_EVENT_NONE received, therefore the charger goes into charge mode\n");
1260 ret
= regmap_field_write(info
->regmap_fields
[F_OPA_MODE
],
1261 RT9455_CHARGE_MODE
);
1263 dev_err(dev
, "Failed to set charger in charge mode\n");
1268 dev_dbg(dev
, "USB_EVENT_NONE received, therefore IAICR is set to its minimum value\n");
1269 if (iaicr
!= RT9455_IAICR_100MA
) {
1270 ret
= regmap_field_write(info
->regmap_fields
[F_IAICR
],
1271 RT9455_IAICR_100MA
);
1273 dev_err(dev
, "Failed to set IAICR value\n");
1281 static int rt9455_usb_event_vbus(struct rt9455_info
*info
,
1282 u8 opa_mode
, u8 iaicr
)
1284 struct device
*dev
= &info
->client
->dev
;
1287 if (opa_mode
== RT9455_BOOST_MODE
) {
1288 ret
= rt9455_set_voreg_before_charge_mode(info
);
1290 dev_err(dev
, "Failed to set VOREG before entering charge mode\n");
1294 * If the charger is in boost mode, and it has received
1295 * USB_EVENT_VBUS, this means the consumer device powered by the
1296 * charger is not connected anymore.
1297 * In this case, the charger goes into charge mode.
1299 dev_dbg(dev
, "USB_EVENT_VBUS received, therefore the charger goes into charge mode\n");
1300 ret
= regmap_field_write(info
->regmap_fields
[F_OPA_MODE
],
1301 RT9455_CHARGE_MODE
);
1303 dev_err(dev
, "Failed to set charger in charge mode\n");
1308 dev_dbg(dev
, "USB_EVENT_VBUS received, therefore IAICR is set to 500 mA\n");
1309 if (iaicr
!= RT9455_IAICR_500MA
) {
1310 ret
= regmap_field_write(info
->regmap_fields
[F_IAICR
],
1311 RT9455_IAICR_500MA
);
1313 dev_err(dev
, "Failed to set IAICR value\n");
1321 static int rt9455_usb_event_id(struct rt9455_info
*info
,
1322 u8 opa_mode
, u8 iaicr
)
1324 struct device
*dev
= &info
->client
->dev
;
1327 if (opa_mode
== RT9455_CHARGE_MODE
) {
1328 ret
= rt9455_set_boost_voltage_before_boost_mode(info
);
1330 dev_err(dev
, "Failed to set boost output voltage before entering boost mode\n");
1334 * If the charger is in charge mode, and it has received
1335 * USB_EVENT_ID, this means a consumer device is connected and
1336 * it should be powered by the charger.
1337 * In this case, the charger goes into boost mode.
1339 dev_dbg(dev
, "USB_EVENT_ID received, therefore the charger goes into boost mode\n");
1340 ret
= regmap_field_write(info
->regmap_fields
[F_OPA_MODE
],
1343 dev_err(dev
, "Failed to set charger in boost mode\n");
1348 dev_dbg(dev
, "USB_EVENT_ID received, therefore IAICR is set to its minimum value\n");
1349 if (iaicr
!= RT9455_IAICR_100MA
) {
1350 ret
= regmap_field_write(info
->regmap_fields
[F_IAICR
],
1351 RT9455_IAICR_100MA
);
1353 dev_err(dev
, "Failed to set IAICR value\n");
1361 static int rt9455_usb_event_charger(struct rt9455_info
*info
,
1362 u8 opa_mode
, u8 iaicr
)
1364 struct device
*dev
= &info
->client
->dev
;
1367 if (opa_mode
== RT9455_BOOST_MODE
) {
1368 ret
= rt9455_set_voreg_before_charge_mode(info
);
1370 dev_err(dev
, "Failed to set VOREG before entering charge mode\n");
1374 * If the charger is in boost mode, and it has received
1375 * USB_EVENT_CHARGER, this means the consumer device powered by
1376 * the charger is not connected anymore.
1377 * In this case, the charger goes into charge mode.
1379 dev_dbg(dev
, "USB_EVENT_CHARGER received, therefore the charger goes into charge mode\n");
1380 ret
= regmap_field_write(info
->regmap_fields
[F_OPA_MODE
],
1381 RT9455_CHARGE_MODE
);
1383 dev_err(dev
, "Failed to set charger in charge mode\n");
1388 dev_dbg(dev
, "USB_EVENT_CHARGER received, therefore IAICR is set to no current limit\n");
1389 if (iaicr
!= RT9455_IAICR_NO_LIMIT
) {
1390 ret
= regmap_field_write(info
->regmap_fields
[F_IAICR
],
1391 RT9455_IAICR_NO_LIMIT
);
1393 dev_err(dev
, "Failed to set IAICR value\n");
1401 static int rt9455_usb_event(struct notifier_block
*nb
,
1402 unsigned long event
, void *power
)
1404 struct rt9455_info
*info
= container_of(nb
, struct rt9455_info
, nb
);
1405 struct device
*dev
= &info
->client
->dev
;
1406 unsigned int opa_mode
, iaicr
;
1410 * Determine whether the charger is in charge mode
1413 ret
= regmap_field_read(info
->regmap_fields
[F_OPA_MODE
],
1416 dev_err(dev
, "Failed to read OPA_MODE value\n");
1420 ret
= regmap_field_read(info
->regmap_fields
[F_IAICR
],
1423 dev_err(dev
, "Failed to read IAICR value\n");
1427 dev_dbg(dev
, "Received USB event %lu\n", event
);
1429 case USB_EVENT_NONE
:
1430 return rt9455_usb_event_none(info
, opa_mode
, iaicr
);
1431 case USB_EVENT_VBUS
:
1432 return rt9455_usb_event_vbus(info
, opa_mode
, iaicr
);
1434 return rt9455_usb_event_id(info
, opa_mode
, iaicr
);
1435 case USB_EVENT_CHARGER
:
1436 return rt9455_usb_event_charger(info
, opa_mode
, iaicr
);
1438 dev_err(dev
, "Unknown USB event\n");
1444 static void rt9455_pwr_rdy_work_callback(struct work_struct
*work
)
1446 struct rt9455_info
*info
= container_of(work
, struct rt9455_info
,
1448 struct device
*dev
= &info
->client
->dev
;
1449 unsigned int pwr_rdy
;
1452 ret
= regmap_field_read(info
->regmap_fields
[F_PWR_RDY
], &pwr_rdy
);
1454 dev_err(dev
, "Failed to read PWR_RDY bit\n");
1458 case RT9455_PWR_FAULT
:
1459 dev_dbg(dev
, "Charger disconnected from power source\n");
1460 cancel_delayed_work_sync(&info
->max_charging_time_work
);
1462 case RT9455_PWR_GOOD
:
1463 dev_dbg(dev
, "Charger connected to power source\n");
1464 ret
= regmap_field_write(info
->regmap_fields
[F_CHG_EN
],
1465 RT9455_CHARGE_ENABLE
);
1467 dev_err(dev
, "Failed to enable charging\n");
1470 queue_delayed_work(system_power_efficient_wq
,
1471 &info
->max_charging_time_work
,
1472 RT9455_MAX_CHARGING_TIME
* HZ
);
1476 * Notify userspace that the charger has been either connected to or
1477 * disconnected from the power source.
1479 power_supply_changed(info
->charger
);
1482 static void rt9455_max_charging_time_work_callback(struct work_struct
*work
)
1484 struct rt9455_info
*info
= container_of(work
, struct rt9455_info
,
1485 max_charging_time_work
.work
);
1486 struct device
*dev
= &info
->client
->dev
;
1489 dev_err(dev
, "Battery has been charging for at least 6 hours and is not yet fully charged. Battery is dead, therefore charging is disabled.\n");
1490 ret
= regmap_field_write(info
->regmap_fields
[F_CHG_EN
],
1491 RT9455_CHARGE_DISABLE
);
1493 dev_err(dev
, "Failed to disable charging\n");
1496 static void rt9455_batt_presence_work_callback(struct work_struct
*work
)
1498 struct rt9455_info
*info
= container_of(work
, struct rt9455_info
,
1499 batt_presence_work
.work
);
1500 struct device
*dev
= &info
->client
->dev
;
1501 unsigned int irq1
, mask1
;
1504 ret
= regmap_read(info
->regmap
, RT9455_REG_IRQ1
, &irq1
);
1506 dev_err(dev
, "Failed to read IRQ1 register\n");
1511 * If the battery is still absent, batt_presence_work is rescheduled.
1512 * Otherwise, max_charging_time is scheduled.
1514 if (irq1
& GET_MASK(F_BATAB
)) {
1515 queue_delayed_work(system_power_efficient_wq
,
1516 &info
->batt_presence_work
,
1517 RT9455_BATT_PRESENCE_DELAY
* HZ
);
1519 queue_delayed_work(system_power_efficient_wq
,
1520 &info
->max_charging_time_work
,
1521 RT9455_MAX_CHARGING_TIME
* HZ
);
1523 ret
= regmap_read(info
->regmap
, RT9455_REG_MASK1
, &mask1
);
1525 dev_err(dev
, "Failed to read MASK1 register\n");
1529 if (mask1
& GET_MASK(F_BATABM
)) {
1530 ret
= regmap_field_write(info
->regmap_fields
[F_BATABM
],
1533 dev_err(dev
, "Failed to unmask BATAB interrupt\n");
1536 * Notify userspace that the battery is now connected to the
1539 power_supply_changed(info
->charger
);
1543 static const struct power_supply_desc rt9455_charger_desc
= {
1544 .name
= RT9455_DRIVER_NAME
,
1545 .type
= POWER_SUPPLY_TYPE_USB
,
1546 .properties
= rt9455_charger_properties
,
1547 .num_properties
= ARRAY_SIZE(rt9455_charger_properties
),
1548 .get_property
= rt9455_charger_get_property
,
1551 static bool rt9455_is_writeable_reg(struct device
*dev
, unsigned int reg
)
1554 case RT9455_REG_DEV_ID
:
1555 case RT9455_REG_IRQ1
:
1556 case RT9455_REG_IRQ2
:
1557 case RT9455_REG_IRQ3
:
1564 static bool rt9455_is_volatile_reg(struct device
*dev
, unsigned int reg
)
1567 case RT9455_REG_DEV_ID
:
1568 case RT9455_REG_CTRL5
:
1569 case RT9455_REG_CTRL6
:
1576 static const struct regmap_config rt9455_regmap_config
= {
1579 .writeable_reg
= rt9455_is_writeable_reg
,
1580 .volatile_reg
= rt9455_is_volatile_reg
,
1581 .max_register
= RT9455_REG_MASK3
,
1582 .cache_type
= REGCACHE_RBTREE
,
1585 static int rt9455_probe(struct i2c_client
*client
)
1587 struct i2c_adapter
*adapter
= client
->adapter
;
1588 struct device
*dev
= &client
->dev
;
1589 struct rt9455_info
*info
;
1590 struct power_supply_config rt9455_charger_config
= {};
1592 * Mandatory device-specific data values. Also, VOREG and boost output
1593 * voltage are mandatory values, but they are stored in rt9455_info
1596 u32 ichrg
, ieoc_percentage
;
1597 /* Optional device-specific data values. */
1598 u32 mivr
= -1, iaicr
= -1;
1601 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)) {
1602 dev_err(dev
, "No support for SMBUS_BYTE_DATA\n");
1605 info
= devm_kzalloc(dev
, sizeof(*info
), GFP_KERNEL
);
1609 info
->client
= client
;
1610 i2c_set_clientdata(client
, info
);
1612 info
->regmap
= devm_regmap_init_i2c(client
,
1613 &rt9455_regmap_config
);
1614 if (IS_ERR(info
->regmap
)) {
1615 dev_err(dev
, "Failed to initialize register map\n");
1619 for (i
= 0; i
< F_MAX_FIELDS
; i
++) {
1620 info
->regmap_fields
[i
] =
1621 devm_regmap_field_alloc(dev
, info
->regmap
,
1622 rt9455_reg_fields
[i
]);
1623 if (IS_ERR(info
->regmap_fields
[i
])) {
1625 "Failed to allocate regmap field = %d\n", i
);
1626 return PTR_ERR(info
->regmap_fields
[i
]);
1630 ret
= rt9455_discover_charger(info
, &ichrg
, &ieoc_percentage
,
1633 dev_err(dev
, "Failed to discover charger\n");
1637 #if IS_ENABLED(CONFIG_USB_PHY)
1638 info
->usb_phy
= devm_usb_get_phy(dev
, USB_PHY_TYPE_USB2
);
1639 if (IS_ERR(info
->usb_phy
)) {
1640 dev_err(dev
, "Failed to get USB transceiver\n");
1642 info
->nb
.notifier_call
= rt9455_usb_event
;
1643 ret
= usb_register_notifier(info
->usb_phy
, &info
->nb
);
1645 dev_err(dev
, "Failed to register USB notifier\n");
1647 * If usb_register_notifier() fails, set notifier_call
1648 * to NULL, to avoid calling usb_unregister_notifier().
1650 info
->nb
.notifier_call
= NULL
;
1655 INIT_DEFERRABLE_WORK(&info
->pwr_rdy_work
, rt9455_pwr_rdy_work_callback
);
1656 INIT_DEFERRABLE_WORK(&info
->max_charging_time_work
,
1657 rt9455_max_charging_time_work_callback
);
1658 INIT_DEFERRABLE_WORK(&info
->batt_presence_work
,
1659 rt9455_batt_presence_work_callback
);
1661 rt9455_charger_config
.of_node
= dev
->of_node
;
1662 rt9455_charger_config
.drv_data
= info
;
1663 rt9455_charger_config
.supplied_to
= rt9455_charger_supplied_to
;
1664 rt9455_charger_config
.num_supplicants
=
1665 ARRAY_SIZE(rt9455_charger_supplied_to
);
1666 ret
= devm_request_threaded_irq(dev
, client
->irq
, NULL
,
1667 rt9455_irq_handler_thread
,
1668 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
1669 RT9455_DRIVER_NAME
, info
);
1671 dev_err(dev
, "Failed to register IRQ handler\n");
1672 goto put_usb_notifier
;
1675 ret
= rt9455_hw_init(info
, ichrg
, ieoc_percentage
, mivr
, iaicr
);
1677 dev_err(dev
, "Failed to set charger to its default values\n");
1678 goto put_usb_notifier
;
1681 info
->charger
= devm_power_supply_register(dev
, &rt9455_charger_desc
,
1682 &rt9455_charger_config
);
1683 if (IS_ERR(info
->charger
)) {
1684 dev_err(dev
, "Failed to register charger\n");
1685 ret
= PTR_ERR(info
->charger
);
1686 goto put_usb_notifier
;
1692 #if IS_ENABLED(CONFIG_USB_PHY)
1693 if (info
->nb
.notifier_call
) {
1694 usb_unregister_notifier(info
->usb_phy
, &info
->nb
);
1695 info
->nb
.notifier_call
= NULL
;
1701 static void rt9455_remove(struct i2c_client
*client
)
1704 struct rt9455_info
*info
= i2c_get_clientdata(client
);
1706 ret
= rt9455_register_reset(info
);
1708 dev_err(&info
->client
->dev
, "Failed to set charger to its default values\n");
1710 #if IS_ENABLED(CONFIG_USB_PHY)
1711 if (info
->nb
.notifier_call
)
1712 usb_unregister_notifier(info
->usb_phy
, &info
->nb
);
1715 cancel_delayed_work_sync(&info
->pwr_rdy_work
);
1716 cancel_delayed_work_sync(&info
->max_charging_time_work
);
1717 cancel_delayed_work_sync(&info
->batt_presence_work
);
1720 static const struct i2c_device_id rt9455_i2c_id_table
[] = {
1721 { RT9455_DRIVER_NAME
},
1724 MODULE_DEVICE_TABLE(i2c
, rt9455_i2c_id_table
);
1726 static const struct of_device_id rt9455_of_match
[] __maybe_unused
= {
1727 { .compatible
= "richtek,rt9455", },
1730 MODULE_DEVICE_TABLE(of
, rt9455_of_match
);
1733 static const struct acpi_device_id rt9455_i2c_acpi_match
[] = {
1737 MODULE_DEVICE_TABLE(acpi
, rt9455_i2c_acpi_match
);
1740 static struct i2c_driver rt9455_driver
= {
1741 .probe
= rt9455_probe
,
1742 .remove
= rt9455_remove
,
1743 .id_table
= rt9455_i2c_id_table
,
1745 .name
= RT9455_DRIVER_NAME
,
1746 .of_match_table
= of_match_ptr(rt9455_of_match
),
1747 .acpi_match_table
= ACPI_PTR(rt9455_i2c_acpi_match
),
1750 module_i2c_driver(rt9455_driver
);
1752 MODULE_LICENSE("GPL");
1753 MODULE_AUTHOR("Anda-Maria Nicolae <anda-maria.nicolae@intel.com>");
1754 MODULE_DESCRIPTION("Richtek RT9455 Charger Driver");