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>
11 #include <linux/of_irq.h>
12 #include <linux/of_device.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/power_supply.h>
15 #include <linux/i2c.h>
16 #include <linux/acpi.h>
17 #include <linux/usb/phy.h>
18 #include <linux/regmap.h>
20 #define RT9455_MANUFACTURER "Richtek"
21 #define RT9455_MODEL_NAME "RT9455"
22 #define RT9455_DRIVER_NAME "rt9455-charger"
24 #define RT9455_IRQ_NAME "interrupt"
26 #define RT9455_PWR_RDY_DELAY 1 /* 1 second */
27 #define RT9455_MAX_CHARGING_TIME 21600 /* 6 hrs */
28 #define RT9455_BATT_PRESENCE_DELAY 60 /* 60 seconds */
30 #define RT9455_CHARGE_MODE 0x00
31 #define RT9455_BOOST_MODE 0x01
33 #define RT9455_FAULT 0x03
35 #define RT9455_IAICR_100MA 0x00
36 #define RT9455_IAICR_500MA 0x01
37 #define RT9455_IAICR_NO_LIMIT 0x03
39 #define RT9455_CHARGE_DISABLE 0x00
40 #define RT9455_CHARGE_ENABLE 0x01
42 #define RT9455_PWR_FAULT 0x00
43 #define RT9455_PWR_GOOD 0x01
45 #define RT9455_REG_CTRL1 0x00 /* CTRL1 reg address */
46 #define RT9455_REG_CTRL2 0x01 /* CTRL2 reg address */
47 #define RT9455_REG_CTRL3 0x02 /* CTRL3 reg address */
48 #define RT9455_REG_DEV_ID 0x03 /* DEV_ID reg address */
49 #define RT9455_REG_CTRL4 0x04 /* CTRL4 reg address */
50 #define RT9455_REG_CTRL5 0x05 /* CTRL5 reg address */
51 #define RT9455_REG_CTRL6 0x06 /* CTRL6 reg address */
52 #define RT9455_REG_CTRL7 0x07 /* CTRL7 reg address */
53 #define RT9455_REG_IRQ1 0x08 /* IRQ1 reg address */
54 #define RT9455_REG_IRQ2 0x09 /* IRQ2 reg address */
55 #define RT9455_REG_IRQ3 0x0A /* IRQ3 reg address */
56 #define RT9455_REG_MASK1 0x0B /* MASK1 reg address */
57 #define RT9455_REG_MASK2 0x0C /* MASK2 reg address */
58 #define RT9455_REG_MASK3 0x0D /* MASK3 reg address */
61 F_STAT, F_BOOST, F_PWR_RDY, F_OTG_PIN_POLARITY, /* CTRL1 reg fields */
63 F_IAICR, F_TE_SHDN_EN, F_HIGHER_OCP, F_TE, F_IAICR_INT, F_HIZ,
64 F_OPA_MODE, /* CTRL2 reg fields */
66 F_VOREG, F_OTG_PL, F_OTG_EN, /* CTRL3 reg fields */
68 F_VENDOR_ID, F_CHIP_REV, /* DEV_ID reg fields */
70 F_RST, /* CTRL4 reg fields */
72 F_TMR_EN, F_MIVR, F_IPREC, F_IEOC_PERCENTAGE, /* CTRL5 reg fields*/
74 F_IAICR_SEL, F_ICHRG, F_VPREC, /* CTRL6 reg fields */
76 F_BATD_EN, F_CHG_EN, F_VMREG, /* CTRL7 reg fields */
78 F_TSDI, F_VINOVPI, F_BATAB, /* IRQ1 reg fields */
80 F_CHRVPI, F_CHBATOVI, F_CHTERMI, F_CHRCHGI, F_CH32MI, F_CHTREGI,
81 F_CHMIVRI, /* IRQ2 reg fields */
83 F_BSTBUSOVI, F_BSTOLI, F_BSTLOWVI, F_BST32SI, /* IRQ3 reg fields */
85 F_TSDM, F_VINOVPIM, F_BATABM, /* MASK1 reg fields */
87 F_CHRVPIM, F_CHBATOVIM, F_CHTERMIM, F_CHRCHGIM, F_CH32MIM, F_CHTREGIM,
88 F_CHMIVRIM, /* MASK2 reg fields */
90 F_BSTVINOVIM, F_BSTOLIM, F_BSTLOWVIM, F_BST32SIM, /* MASK3 reg fields */
95 static const struct reg_field rt9455_reg_fields[] = {
96 [F_STAT] = REG_FIELD(RT9455_REG_CTRL1, 4, 5),
97 [F_BOOST] = REG_FIELD(RT9455_REG_CTRL1, 3, 3),
98 [F_PWR_RDY] = REG_FIELD(RT9455_REG_CTRL1, 2, 2),
99 [F_OTG_PIN_POLARITY] = REG_FIELD(RT9455_REG_CTRL1, 1, 1),
101 [F_IAICR] = REG_FIELD(RT9455_REG_CTRL2, 6, 7),
102 [F_TE_SHDN_EN] = REG_FIELD(RT9455_REG_CTRL2, 5, 5),
103 [F_HIGHER_OCP] = REG_FIELD(RT9455_REG_CTRL2, 4, 4),
104 [F_TE] = REG_FIELD(RT9455_REG_CTRL2, 3, 3),
105 [F_IAICR_INT] = REG_FIELD(RT9455_REG_CTRL2, 2, 2),
106 [F_HIZ] = REG_FIELD(RT9455_REG_CTRL2, 1, 1),
107 [F_OPA_MODE] = REG_FIELD(RT9455_REG_CTRL2, 0, 0),
109 [F_VOREG] = REG_FIELD(RT9455_REG_CTRL3, 2, 7),
110 [F_OTG_PL] = REG_FIELD(RT9455_REG_CTRL3, 1, 1),
111 [F_OTG_EN] = REG_FIELD(RT9455_REG_CTRL3, 0, 0),
113 [F_VENDOR_ID] = REG_FIELD(RT9455_REG_DEV_ID, 4, 7),
114 [F_CHIP_REV] = REG_FIELD(RT9455_REG_DEV_ID, 0, 3),
116 [F_RST] = REG_FIELD(RT9455_REG_CTRL4, 7, 7),
118 [F_TMR_EN] = REG_FIELD(RT9455_REG_CTRL5, 7, 7),
119 [F_MIVR] = REG_FIELD(RT9455_REG_CTRL5, 4, 5),
120 [F_IPREC] = REG_FIELD(RT9455_REG_CTRL5, 2, 3),
121 [F_IEOC_PERCENTAGE] = REG_FIELD(RT9455_REG_CTRL5, 0, 1),
123 [F_IAICR_SEL] = REG_FIELD(RT9455_REG_CTRL6, 7, 7),
124 [F_ICHRG] = REG_FIELD(RT9455_REG_CTRL6, 4, 6),
125 [F_VPREC] = REG_FIELD(RT9455_REG_CTRL6, 0, 2),
127 [F_BATD_EN] = REG_FIELD(RT9455_REG_CTRL7, 6, 6),
128 [F_CHG_EN] = REG_FIELD(RT9455_REG_CTRL7, 4, 4),
129 [F_VMREG] = REG_FIELD(RT9455_REG_CTRL7, 0, 3),
131 [F_TSDI] = REG_FIELD(RT9455_REG_IRQ1, 7, 7),
132 [F_VINOVPI] = REG_FIELD(RT9455_REG_IRQ1, 6, 6),
133 [F_BATAB] = REG_FIELD(RT9455_REG_IRQ1, 0, 0),
135 [F_CHRVPI] = REG_FIELD(RT9455_REG_IRQ2, 7, 7),
136 [F_CHBATOVI] = REG_FIELD(RT9455_REG_IRQ2, 5, 5),
137 [F_CHTERMI] = REG_FIELD(RT9455_REG_IRQ2, 4, 4),
138 [F_CHRCHGI] = REG_FIELD(RT9455_REG_IRQ2, 3, 3),
139 [F_CH32MI] = REG_FIELD(RT9455_REG_IRQ2, 2, 2),
140 [F_CHTREGI] = REG_FIELD(RT9455_REG_IRQ2, 1, 1),
141 [F_CHMIVRI] = REG_FIELD(RT9455_REG_IRQ2, 0, 0),
143 [F_BSTBUSOVI] = REG_FIELD(RT9455_REG_IRQ3, 7, 7),
144 [F_BSTOLI] = REG_FIELD(RT9455_REG_IRQ3, 6, 6),
145 [F_BSTLOWVI] = REG_FIELD(RT9455_REG_IRQ3, 5, 5),
146 [F_BST32SI] = REG_FIELD(RT9455_REG_IRQ3, 3, 3),
148 [F_TSDM] = REG_FIELD(RT9455_REG_MASK1, 7, 7),
149 [F_VINOVPIM] = REG_FIELD(RT9455_REG_MASK1, 6, 6),
150 [F_BATABM] = REG_FIELD(RT9455_REG_MASK1, 0, 0),
152 [F_CHRVPIM] = REG_FIELD(RT9455_REG_MASK2, 7, 7),
153 [F_CHBATOVIM] = REG_FIELD(RT9455_REG_MASK2, 5, 5),
154 [F_CHTERMIM] = REG_FIELD(RT9455_REG_MASK2, 4, 4),
155 [F_CHRCHGIM] = REG_FIELD(RT9455_REG_MASK2, 3, 3),
156 [F_CH32MIM] = REG_FIELD(RT9455_REG_MASK2, 2, 2),
157 [F_CHTREGIM] = REG_FIELD(RT9455_REG_MASK2, 1, 1),
158 [F_CHMIVRIM] = REG_FIELD(RT9455_REG_MASK2, 0, 0),
160 [F_BSTVINOVIM] = REG_FIELD(RT9455_REG_MASK3, 7, 7),
161 [F_BSTOLIM] = REG_FIELD(RT9455_REG_MASK3, 6, 6),
162 [F_BSTLOWVIM] = REG_FIELD(RT9455_REG_MASK3, 5, 5),
163 [F_BST32SIM] = REG_FIELD(RT9455_REG_MASK3, 3, 3),
166 #define GET_MASK(fid) (BIT(rt9455_reg_fields[fid].msb + 1) - \
167 BIT(rt9455_reg_fields[fid].lsb))
170 * Each array initialised below shows the possible real-world values for a
171 * group of bits belonging to RT9455 registers. The arrays are sorted in
172 * ascending order. The index of each real-world value represents the value
173 * that is encoded in the group of bits belonging to RT9455 registers.
175 /* REG06[6:4] (ICHRG) in uAh */
176 static const int rt9455_ichrg_values[] = {
177 500000, 650000, 800000, 950000, 1100000, 1250000, 1400000, 1550000
181 * When the charger is in charge mode, REG02[7:2] represent battery regulation
184 /* REG02[7:2] (VOREG) in uV */
185 static const int rt9455_voreg_values[] = {
186 3500000, 3520000, 3540000, 3560000, 3580000, 3600000, 3620000, 3640000,
187 3660000, 3680000, 3700000, 3720000, 3740000, 3760000, 3780000, 3800000,
188 3820000, 3840000, 3860000, 3880000, 3900000, 3920000, 3940000, 3960000,
189 3980000, 4000000, 4020000, 4040000, 4060000, 4080000, 4100000, 4120000,
190 4140000, 4160000, 4180000, 4200000, 4220000, 4240000, 4260000, 4280000,
191 4300000, 4330000, 4350000, 4370000, 4390000, 4410000, 4430000, 4450000,
192 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000,
193 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000
196 #if IS_ENABLED(CONFIG_USB_PHY)
198 * When the charger is in boost mode, REG02[7:2] represent boost output
201 /* REG02[7:2] (Boost output voltage) in uV */
202 static const int rt9455_boost_voltage_values[] = {
203 4425000, 4450000, 4475000, 4500000, 4525000, 4550000, 4575000, 4600000,
204 4625000, 4650000, 4675000, 4700000, 4725000, 4750000, 4775000, 4800000,
205 4825000, 4850000, 4875000, 4900000, 4925000, 4950000, 4975000, 5000000,
206 5025000, 5050000, 5075000, 5100000, 5125000, 5150000, 5175000, 5200000,
207 5225000, 5250000, 5275000, 5300000, 5325000, 5350000, 5375000, 5400000,
208 5425000, 5450000, 5475000, 5500000, 5525000, 5550000, 5575000, 5600000,
209 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000,
210 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000,
214 /* REG07[3:0] (VMREG) in uV */
215 static const int rt9455_vmreg_values[] = {
216 4200000, 4220000, 4240000, 4260000, 4280000, 4300000, 4320000, 4340000,
217 4360000, 4380000, 4400000, 4430000, 4450000, 4450000, 4450000, 4450000
220 /* REG05[5:4] (IEOC_PERCENTAGE) */
221 static const int rt9455_ieoc_percentage_values[] = {
225 /* REG05[1:0] (MIVR) in uV */
226 static const int rt9455_mivr_values[] = {
227 4000000, 4250000, 4500000, 5000000
230 /* REG05[1:0] (IAICR) in uA */
231 static const int rt9455_iaicr_values[] = {
232 100000, 500000, 1000000, 2000000
236 struct i2c_client *client;
237 struct regmap *regmap;
238 struct regmap_field *regmap_fields[F_MAX_FIELDS];
239 struct power_supply *charger;
240 #if IS_ENABLED(CONFIG_USB_PHY)
241 struct usb_phy *usb_phy;
242 struct notifier_block nb;
244 struct delayed_work pwr_rdy_work;
245 struct delayed_work max_charging_time_work;
246 struct delayed_work batt_presence_work;
252 * Iterate through each element of the 'tbl' array until an element whose value
253 * is greater than v is found. Return the index of the respective element,
254 * or the index of the last element in the array, if no such element is found.
256 static unsigned int rt9455_find_idx(const int tbl[], int tbl_size, int v)
261 * No need to iterate until the last index in the table because
262 * if no element greater than v is found in the table,
263 * or if only the last element is greater than v,
264 * function returns the index of the last element.
266 for (i = 0; i < tbl_size - 1; i++)
270 return (tbl_size - 1);
273 static int rt9455_get_field_val(struct rt9455_info *info,
274 enum rt9455_fields field,
275 const int tbl[], int tbl_size, int *val)
280 ret = regmap_field_read(info->regmap_fields[field], &v);
284 v = (v >= tbl_size) ? (tbl_size - 1) : v;
290 static int rt9455_set_field_val(struct rt9455_info *info,
291 enum rt9455_fields field,
292 const int tbl[], int tbl_size, int val)
294 unsigned int idx = rt9455_find_idx(tbl, tbl_size, val);
296 return regmap_field_write(info->regmap_fields[field], idx);
299 static int rt9455_register_reset(struct rt9455_info *info)
301 struct device *dev = &info->client->dev;
303 int ret, limit = 100;
305 ret = regmap_field_write(info->regmap_fields[F_RST], 0x01);
307 dev_err(dev, "Failed to set RST bit\n");
312 * To make sure that reset operation has finished, loop until RST bit
316 ret = regmap_field_read(info->regmap_fields[F_RST], &v);
318 dev_err(dev, "Failed to read RST bit\n");
325 usleep_range(10, 100);
334 /* Charger power supply property routines */
335 static enum power_supply_property rt9455_charger_properties[] = {
336 POWER_SUPPLY_PROP_STATUS,
337 POWER_SUPPLY_PROP_HEALTH,
338 POWER_SUPPLY_PROP_PRESENT,
339 POWER_SUPPLY_PROP_ONLINE,
340 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
341 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
342 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
343 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
344 POWER_SUPPLY_PROP_SCOPE,
345 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
346 POWER_SUPPLY_PROP_MODEL_NAME,
347 POWER_SUPPLY_PROP_MANUFACTURER,
350 static char *rt9455_charger_supplied_to[] = {
354 static int rt9455_charger_get_status(struct rt9455_info *info,
355 union power_supply_propval *val)
357 unsigned int v, pwr_rdy;
360 ret = regmap_field_read(info->regmap_fields[F_PWR_RDY],
363 dev_err(&info->client->dev, "Failed to read PWR_RDY bit\n");
368 * If PWR_RDY bit is unset, the battery is discharging. Otherwise,
369 * STAT bits value must be checked.
372 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
376 ret = regmap_field_read(info->regmap_fields[F_STAT], &v);
378 dev_err(&info->client->dev, "Failed to read STAT bits\n");
385 * If PWR_RDY bit is set, but STAT bits value is 0, the charger
386 * may be in one of the following cases:
387 * 1. CHG_EN bit is 0.
388 * 2. CHG_EN bit is 1 but the battery is not connected.
389 * In any of these cases, POWER_SUPPLY_STATUS_NOT_CHARGING is
392 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
395 val->intval = POWER_SUPPLY_STATUS_CHARGING;
398 val->intval = POWER_SUPPLY_STATUS_FULL;
401 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
406 static int rt9455_charger_get_health(struct rt9455_info *info,
407 union power_supply_propval *val)
409 struct device *dev = &info->client->dev;
413 val->intval = POWER_SUPPLY_HEALTH_GOOD;
415 ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &v);
417 dev_err(dev, "Failed to read IRQ1 register\n");
421 if (v & GET_MASK(F_TSDI)) {
422 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
425 if (v & GET_MASK(F_VINOVPI)) {
426 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
429 if (v & GET_MASK(F_BATAB)) {
430 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
434 ret = regmap_read(info->regmap, RT9455_REG_IRQ2, &v);
436 dev_err(dev, "Failed to read IRQ2 register\n");
440 if (v & GET_MASK(F_CHBATOVI)) {
441 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
444 if (v & GET_MASK(F_CH32MI)) {
445 val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
449 ret = regmap_read(info->regmap, RT9455_REG_IRQ3, &v);
451 dev_err(dev, "Failed to read IRQ3 register\n");
455 if (v & GET_MASK(F_BSTBUSOVI)) {
456 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
459 if (v & GET_MASK(F_BSTOLI)) {
460 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
463 if (v & GET_MASK(F_BSTLOWVI)) {
464 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
467 if (v & GET_MASK(F_BST32SI)) {
468 val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
472 ret = regmap_field_read(info->regmap_fields[F_STAT], &v);
474 dev_err(dev, "Failed to read STAT bits\n");
478 if (v == RT9455_FAULT) {
479 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
486 static int rt9455_charger_get_battery_presence(struct rt9455_info *info,
487 union power_supply_propval *val)
492 ret = regmap_field_read(info->regmap_fields[F_BATAB], &v);
494 dev_err(&info->client->dev, "Failed to read BATAB bit\n");
499 * Since BATAB is 1 when battery is NOT present and 0 otherwise,
500 * !BATAB is returned.
507 static int rt9455_charger_get_online(struct rt9455_info *info,
508 union power_supply_propval *val)
513 ret = regmap_field_read(info->regmap_fields[F_PWR_RDY], &v);
515 dev_err(&info->client->dev, "Failed to read PWR_RDY bit\n");
519 val->intval = (int)v;
524 static int rt9455_charger_get_current(struct rt9455_info *info,
525 union power_supply_propval *val)
530 ret = rt9455_get_field_val(info, F_ICHRG,
532 ARRAY_SIZE(rt9455_ichrg_values),
535 dev_err(&info->client->dev, "Failed to read ICHRG value\n");
544 static int rt9455_charger_get_current_max(struct rt9455_info *info,
545 union power_supply_propval *val)
547 int idx = ARRAY_SIZE(rt9455_ichrg_values) - 1;
549 val->intval = rt9455_ichrg_values[idx];
554 static int rt9455_charger_get_voltage(struct rt9455_info *info,
555 union power_supply_propval *val)
560 ret = rt9455_get_field_val(info, F_VOREG,
562 ARRAY_SIZE(rt9455_voreg_values),
565 dev_err(&info->client->dev, "Failed to read VOREG value\n");
569 val->intval = voltage;
574 static int rt9455_charger_get_voltage_max(struct rt9455_info *info,
575 union power_supply_propval *val)
577 int idx = ARRAY_SIZE(rt9455_vmreg_values) - 1;
579 val->intval = rt9455_vmreg_values[idx];
584 static int rt9455_charger_get_term_current(struct rt9455_info *info,
585 union power_supply_propval *val)
587 struct device *dev = &info->client->dev;
588 int ichrg, ieoc_percentage, ret;
590 ret = rt9455_get_field_val(info, F_ICHRG,
592 ARRAY_SIZE(rt9455_ichrg_values),
595 dev_err(dev, "Failed to read ICHRG value\n");
599 ret = rt9455_get_field_val(info, F_IEOC_PERCENTAGE,
600 rt9455_ieoc_percentage_values,
601 ARRAY_SIZE(rt9455_ieoc_percentage_values),
604 dev_err(dev, "Failed to read IEOC value\n");
608 val->intval = ichrg * ieoc_percentage / 100;
613 static int rt9455_charger_get_property(struct power_supply *psy,
614 enum power_supply_property psp,
615 union power_supply_propval *val)
617 struct rt9455_info *info = power_supply_get_drvdata(psy);
620 case POWER_SUPPLY_PROP_STATUS:
621 return rt9455_charger_get_status(info, val);
622 case POWER_SUPPLY_PROP_HEALTH:
623 return rt9455_charger_get_health(info, val);
624 case POWER_SUPPLY_PROP_PRESENT:
625 return rt9455_charger_get_battery_presence(info, val);
626 case POWER_SUPPLY_PROP_ONLINE:
627 return rt9455_charger_get_online(info, val);
628 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
629 return rt9455_charger_get_current(info, val);
630 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
631 return rt9455_charger_get_current_max(info, val);
632 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
633 return rt9455_charger_get_voltage(info, val);
634 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
635 return rt9455_charger_get_voltage_max(info, val);
636 case POWER_SUPPLY_PROP_SCOPE:
637 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
639 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
640 return rt9455_charger_get_term_current(info, val);
641 case POWER_SUPPLY_PROP_MODEL_NAME:
642 val->strval = RT9455_MODEL_NAME;
644 case POWER_SUPPLY_PROP_MANUFACTURER:
645 val->strval = RT9455_MANUFACTURER;
652 static int rt9455_hw_init(struct rt9455_info *info, u32 ichrg,
656 struct device *dev = &info->client->dev;
659 ret = rt9455_register_reset(info);
661 dev_err(dev, "Power On Reset failed\n");
665 /* Set TE bit in order to enable end of charge detection */
666 ret = regmap_field_write(info->regmap_fields[F_TE], 1);
668 dev_err(dev, "Failed to set TE bit\n");
672 /* Set TE_SHDN_EN bit in order to enable end of charge detection */
673 ret = regmap_field_write(info->regmap_fields[F_TE_SHDN_EN], 1);
675 dev_err(dev, "Failed to set TE_SHDN_EN bit\n");
680 * Set BATD_EN bit in order to enable battery detection
681 * when charging is done
683 ret = regmap_field_write(info->regmap_fields[F_BATD_EN], 1);
685 dev_err(dev, "Failed to set BATD_EN bit\n");
690 * Disable Safety Timer. In charge mode, this timer terminates charging
691 * if no read or write via I2C is done within 32 minutes. This timer
692 * avoids overcharging the baterry when the OS is not loaded and the
693 * charger is connected to a power source.
694 * In boost mode, this timer triggers BST32SI interrupt if no read or
695 * write via I2C is done within 32 seconds.
696 * When the OS is loaded and the charger driver is inserted, it is used
697 * delayed_work, named max_charging_time_work, to avoid overcharging
700 ret = regmap_field_write(info->regmap_fields[F_TMR_EN], 0x00);
702 dev_err(dev, "Failed to disable Safety Timer\n");
706 /* Set ICHRG to value retrieved from device-specific data */
707 ret = rt9455_set_field_val(info, F_ICHRG,
709 ARRAY_SIZE(rt9455_ichrg_values), ichrg);
711 dev_err(dev, "Failed to set ICHRG value\n");
715 /* Set IEOC Percentage to value retrieved from device-specific data */
716 ret = rt9455_set_field_val(info, F_IEOC_PERCENTAGE,
717 rt9455_ieoc_percentage_values,
718 ARRAY_SIZE(rt9455_ieoc_percentage_values),
721 dev_err(dev, "Failed to set IEOC Percentage value\n");
725 /* Set VOREG to value retrieved from device-specific data */
726 ret = rt9455_set_field_val(info, F_VOREG,
728 ARRAY_SIZE(rt9455_voreg_values),
731 dev_err(dev, "Failed to set VOREG value\n");
735 /* Set VMREG value to maximum (4.45V). */
736 idx = ARRAY_SIZE(rt9455_vmreg_values) - 1;
737 ret = rt9455_set_field_val(info, F_VMREG,
739 ARRAY_SIZE(rt9455_vmreg_values),
740 rt9455_vmreg_values[idx]);
742 dev_err(dev, "Failed to set VMREG value\n");
747 * Set MIVR to value retrieved from device-specific data.
748 * If no value is specified, default value for MIVR is 4.5V.
753 ret = rt9455_set_field_val(info, F_MIVR,
755 ARRAY_SIZE(rt9455_mivr_values), mivr);
757 dev_err(dev, "Failed to set MIVR value\n");
762 * Set IAICR to value retrieved from device-specific data.
763 * If no value is specified, default value for IAICR is 500 mA.
768 ret = rt9455_set_field_val(info, F_IAICR,
770 ARRAY_SIZE(rt9455_iaicr_values), iaicr);
772 dev_err(dev, "Failed to set IAICR value\n");
777 * Set IAICR_INT bit so that IAICR value is determined by IAICR bits
778 * and not by OTG pin.
780 ret = regmap_field_write(info->regmap_fields[F_IAICR_INT], 0x01);
782 dev_err(dev, "Failed to set IAICR_INT bit\n");
787 * Disable CHMIVRI interrupt. Because the driver sets MIVR value,
788 * CHMIVRI is triggered, but there is no action to be taken by the
789 * driver when CHMIVRI is triggered.
791 ret = regmap_field_write(info->regmap_fields[F_CHMIVRIM], 0x01);
793 dev_err(dev, "Failed to mask CHMIVRI interrupt\n");
800 #if IS_ENABLED(CONFIG_USB_PHY)
802 * Before setting the charger into boost mode, boost output voltage is
803 * set. This is needed because boost output voltage may differ from battery
804 * regulation voltage. F_VOREG bits represent either battery regulation voltage
805 * or boost output voltage, depending on the mode the charger is. Both battery
806 * regulation voltage and boost output voltage are read from DT/ACPI during
809 static int rt9455_set_boost_voltage_before_boost_mode(struct rt9455_info *info)
811 struct device *dev = &info->client->dev;
814 ret = rt9455_set_field_val(info, F_VOREG,
815 rt9455_boost_voltage_values,
816 ARRAY_SIZE(rt9455_boost_voltage_values),
817 info->boost_voltage);
819 dev_err(dev, "Failed to set boost output voltage value\n");
828 * Before setting the charger into charge mode, battery regulation voltage is
829 * set. This is needed because boost output voltage may differ from battery
830 * regulation voltage. F_VOREG bits represent either battery regulation voltage
831 * or boost output voltage, depending on the mode the charger is. Both battery
832 * regulation voltage and boost output voltage are read from DT/ACPI during
835 static int rt9455_set_voreg_before_charge_mode(struct rt9455_info *info)
837 struct device *dev = &info->client->dev;
840 ret = rt9455_set_field_val(info, F_VOREG,
842 ARRAY_SIZE(rt9455_voreg_values),
845 dev_err(dev, "Failed to set VOREG value\n");
852 static int rt9455_irq_handler_check_irq1_register(struct rt9455_info *info,
853 bool *_is_battery_absent,
854 bool *_alert_userspace)
856 unsigned int irq1, mask1, mask2;
857 struct device *dev = &info->client->dev;
858 bool is_battery_absent = false;
859 bool alert_userspace = false;
862 ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &irq1);
864 dev_err(dev, "Failed to read IRQ1 register\n");
868 ret = regmap_read(info->regmap, RT9455_REG_MASK1, &mask1);
870 dev_err(dev, "Failed to read MASK1 register\n");
874 if (irq1 & GET_MASK(F_TSDI)) {
875 dev_err(dev, "Thermal shutdown fault occurred\n");
876 alert_userspace = true;
879 if (irq1 & GET_MASK(F_VINOVPI)) {
880 dev_err(dev, "Overvoltage input occurred\n");
881 alert_userspace = true;
884 if (irq1 & GET_MASK(F_BATAB)) {
885 dev_err(dev, "Battery absence occurred\n");
886 is_battery_absent = true;
887 alert_userspace = true;
889 if ((mask1 & GET_MASK(F_BATABM)) == 0) {
890 ret = regmap_field_write(info->regmap_fields[F_BATABM],
893 dev_err(dev, "Failed to mask BATAB interrupt\n");
898 ret = regmap_read(info->regmap, RT9455_REG_MASK2, &mask2);
900 dev_err(dev, "Failed to read MASK2 register\n");
904 if (mask2 & GET_MASK(F_CHTERMIM)) {
905 ret = regmap_field_write(
906 info->regmap_fields[F_CHTERMIM], 0x00);
908 dev_err(dev, "Failed to unmask CHTERMI interrupt\n");
913 if (mask2 & GET_MASK(F_CHRCHGIM)) {
914 ret = regmap_field_write(
915 info->regmap_fields[F_CHRCHGIM], 0x00);
917 dev_err(dev, "Failed to unmask CHRCHGI interrupt\n");
923 * When the battery is absent, max_charging_time_work is
924 * cancelled, since no charging is done.
926 cancel_delayed_work_sync(&info->max_charging_time_work);
928 * Since no interrupt is triggered when the battery is
929 * reconnected, max_charging_time_work is not rescheduled.
930 * Therefore, batt_presence_work is scheduled to check whether
931 * the battery is still absent or not.
933 queue_delayed_work(system_power_efficient_wq,
934 &info->batt_presence_work,
935 RT9455_BATT_PRESENCE_DELAY * HZ);
938 *_is_battery_absent = is_battery_absent;
941 *_alert_userspace = alert_userspace;
946 static int rt9455_irq_handler_check_irq2_register(struct rt9455_info *info,
947 bool is_battery_absent,
948 bool *_alert_userspace)
950 unsigned int irq2, mask2;
951 struct device *dev = &info->client->dev;
952 bool alert_userspace = false;
955 ret = regmap_read(info->regmap, RT9455_REG_IRQ2, &irq2);
957 dev_err(dev, "Failed to read IRQ2 register\n");
961 ret = regmap_read(info->regmap, RT9455_REG_MASK2, &mask2);
963 dev_err(dev, "Failed to read MASK2 register\n");
967 if (irq2 & GET_MASK(F_CHRVPI)) {
968 dev_dbg(dev, "Charger fault occurred\n");
970 * CHRVPI bit is set in 2 cases:
971 * 1. when the power source is connected to the charger.
972 * 2. when the power source is disconnected from the charger.
973 * To identify the case, PWR_RDY bit is checked. Because
974 * PWR_RDY bit is set / cleared after CHRVPI interrupt is
975 * triggered, it is used delayed_work to later read PWR_RDY bit.
976 * Also, do not set to true alert_userspace, because there is no
977 * need to notify userspace when CHRVPI interrupt has occurred.
978 * Userspace will be notified after PWR_RDY bit is read.
980 queue_delayed_work(system_power_efficient_wq,
982 RT9455_PWR_RDY_DELAY * HZ);
984 if (irq2 & GET_MASK(F_CHBATOVI)) {
985 dev_err(dev, "Battery OVP occurred\n");
986 alert_userspace = true;
988 if (irq2 & GET_MASK(F_CHTERMI)) {
989 dev_dbg(dev, "Charge terminated\n");
990 if (!is_battery_absent) {
991 if ((mask2 & GET_MASK(F_CHTERMIM)) == 0) {
992 ret = regmap_field_write(
993 info->regmap_fields[F_CHTERMIM], 0x01);
995 dev_err(dev, "Failed to mask CHTERMI interrupt\n");
999 * Update MASK2 value, since CHTERMIM bit is
1002 mask2 = mask2 | GET_MASK(F_CHTERMIM);
1004 cancel_delayed_work_sync(&info->max_charging_time_work);
1005 alert_userspace = true;
1008 if (irq2 & GET_MASK(F_CHRCHGI)) {
1009 dev_dbg(dev, "Recharge request\n");
1010 ret = regmap_field_write(info->regmap_fields[F_CHG_EN],
1011 RT9455_CHARGE_ENABLE);
1013 dev_err(dev, "Failed to enable charging\n");
1016 if (mask2 & GET_MASK(F_CHTERMIM)) {
1017 ret = regmap_field_write(
1018 info->regmap_fields[F_CHTERMIM], 0x00);
1020 dev_err(dev, "Failed to unmask CHTERMI interrupt\n");
1023 /* Update MASK2 value, since CHTERMIM bit is cleared. */
1024 mask2 = mask2 & ~GET_MASK(F_CHTERMIM);
1026 if (!is_battery_absent) {
1028 * No need to check whether the charger is connected to
1029 * power source when CHRCHGI is received, since CHRCHGI
1030 * is not triggered if the charger is not connected to
1033 queue_delayed_work(system_power_efficient_wq,
1034 &info->max_charging_time_work,
1035 RT9455_MAX_CHARGING_TIME * HZ);
1036 alert_userspace = true;
1039 if (irq2 & GET_MASK(F_CH32MI)) {
1040 dev_err(dev, "Charger fault. 32 mins timeout occurred\n");
1041 alert_userspace = true;
1043 if (irq2 & GET_MASK(F_CHTREGI)) {
1045 "Charger warning. Thermal regulation loop active\n");
1046 alert_userspace = true;
1048 if (irq2 & GET_MASK(F_CHMIVRI)) {
1050 "Charger warning. Input voltage MIVR loop active\n");
1053 if (alert_userspace)
1054 *_alert_userspace = alert_userspace;
1059 static int rt9455_irq_handler_check_irq3_register(struct rt9455_info *info,
1060 bool *_alert_userspace)
1062 unsigned int irq3, mask3;
1063 struct device *dev = &info->client->dev;
1064 bool alert_userspace = false;
1067 ret = regmap_read(info->regmap, RT9455_REG_IRQ3, &irq3);
1069 dev_err(dev, "Failed to read IRQ3 register\n");
1073 ret = regmap_read(info->regmap, RT9455_REG_MASK3, &mask3);
1075 dev_err(dev, "Failed to read MASK3 register\n");
1079 if (irq3 & GET_MASK(F_BSTBUSOVI)) {
1080 dev_err(dev, "Boost fault. Overvoltage input occurred\n");
1081 alert_userspace = true;
1083 if (irq3 & GET_MASK(F_BSTOLI)) {
1084 dev_err(dev, "Boost fault. Overload\n");
1085 alert_userspace = true;
1087 if (irq3 & GET_MASK(F_BSTLOWVI)) {
1088 dev_err(dev, "Boost fault. Battery voltage too low\n");
1089 alert_userspace = true;
1091 if (irq3 & GET_MASK(F_BST32SI)) {
1092 dev_err(dev, "Boost fault. 32 seconds timeout occurred.\n");
1093 alert_userspace = true;
1096 if (alert_userspace) {
1097 dev_info(dev, "Boost fault occurred, therefore the charger goes into charge mode\n");
1098 ret = rt9455_set_voreg_before_charge_mode(info);
1100 dev_err(dev, "Failed to set VOREG before entering charge mode\n");
1103 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1104 RT9455_CHARGE_MODE);
1106 dev_err(dev, "Failed to set charger in charge mode\n");
1109 *_alert_userspace = alert_userspace;
1115 static irqreturn_t rt9455_irq_handler_thread(int irq, void *data)
1117 struct rt9455_info *info = data;
1119 bool alert_userspace = false;
1120 bool is_battery_absent = false;
1121 unsigned int status;
1127 dev = &info->client->dev;
1129 if (irq != info->client->irq) {
1130 dev_err(dev, "Interrupt is not for RT9455 charger\n");
1134 ret = regmap_field_read(info->regmap_fields[F_STAT], &status);
1136 dev_err(dev, "Failed to read STAT bits\n");
1139 dev_dbg(dev, "Charger status is %d\n", status);
1142 * Each function that processes an IRQ register receives as output
1143 * parameter alert_userspace pointer. alert_userspace is set to true
1144 * in such a function only if an interrupt has occurred in the
1145 * respective interrupt register. This way, it is avoided the following
1146 * case: interrupt occurs only in IRQ1 register,
1147 * rt9455_irq_handler_check_irq1_register() function sets to true
1148 * alert_userspace, but rt9455_irq_handler_check_irq2_register()
1149 * and rt9455_irq_handler_check_irq3_register() functions set to false
1150 * alert_userspace and power_supply_changed() is never called.
1152 ret = rt9455_irq_handler_check_irq1_register(info, &is_battery_absent,
1155 dev_err(dev, "Failed to handle IRQ1 register\n");
1159 ret = rt9455_irq_handler_check_irq2_register(info, is_battery_absent,
1162 dev_err(dev, "Failed to handle IRQ2 register\n");
1166 ret = rt9455_irq_handler_check_irq3_register(info, &alert_userspace);
1168 dev_err(dev, "Failed to handle IRQ3 register\n");
1172 if (alert_userspace) {
1174 * Sometimes, an interrupt occurs while rt9455_probe() function
1175 * is executing and power_supply_register() is not yet called.
1176 * Do not call power_supply_changed() in this case.
1179 power_supply_changed(info->charger);
1185 static int rt9455_discover_charger(struct rt9455_info *info, u32 *ichrg,
1186 u32 *ieoc_percentage,
1187 u32 *mivr, u32 *iaicr)
1189 struct device *dev = &info->client->dev;
1192 if (!dev->of_node && !ACPI_HANDLE(dev)) {
1193 dev_err(dev, "No support for either device tree or ACPI\n");
1197 * ICHRG, IEOC_PERCENTAGE, VOREG and boost output voltage are mandatory
1200 ret = device_property_read_u32(dev, "richtek,output-charge-current",
1203 dev_err(dev, "Error: missing \"output-charge-current\" property\n");
1207 ret = device_property_read_u32(dev, "richtek,end-of-charge-percentage",
1210 dev_err(dev, "Error: missing \"end-of-charge-percentage\" property\n");
1214 ret = device_property_read_u32(dev,
1215 "richtek,battery-regulation-voltage",
1218 dev_err(dev, "Error: missing \"battery-regulation-voltage\" property\n");
1222 ret = device_property_read_u32(dev, "richtek,boost-output-voltage",
1223 &info->boost_voltage);
1225 dev_err(dev, "Error: missing \"boost-output-voltage\" property\n");
1230 * MIVR and IAICR are optional parameters. Do not return error if one of
1231 * them is not present in ACPI table or device tree specification.
1233 device_property_read_u32(dev, "richtek,min-input-voltage-regulation",
1235 device_property_read_u32(dev, "richtek,avg-input-current-regulation",
1241 #if IS_ENABLED(CONFIG_USB_PHY)
1242 static int rt9455_usb_event_none(struct rt9455_info *info,
1243 u8 opa_mode, u8 iaicr)
1245 struct device *dev = &info->client->dev;
1248 if (opa_mode == RT9455_BOOST_MODE) {
1249 ret = rt9455_set_voreg_before_charge_mode(info);
1251 dev_err(dev, "Failed to set VOREG before entering charge mode\n");
1255 * If the charger is in boost mode, and it has received
1256 * USB_EVENT_NONE, this means the consumer device powered by the
1257 * charger is not connected anymore.
1258 * In this case, the charger goes into charge mode.
1260 dev_dbg(dev, "USB_EVENT_NONE received, therefore the charger goes into charge mode\n");
1261 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1262 RT9455_CHARGE_MODE);
1264 dev_err(dev, "Failed to set charger in charge mode\n");
1269 dev_dbg(dev, "USB_EVENT_NONE received, therefore IAICR is set to its minimum value\n");
1270 if (iaicr != RT9455_IAICR_100MA) {
1271 ret = regmap_field_write(info->regmap_fields[F_IAICR],
1272 RT9455_IAICR_100MA);
1274 dev_err(dev, "Failed to set IAICR value\n");
1282 static int rt9455_usb_event_vbus(struct rt9455_info *info,
1283 u8 opa_mode, u8 iaicr)
1285 struct device *dev = &info->client->dev;
1288 if (opa_mode == RT9455_BOOST_MODE) {
1289 ret = rt9455_set_voreg_before_charge_mode(info);
1291 dev_err(dev, "Failed to set VOREG before entering charge mode\n");
1295 * If the charger is in boost mode, and it has received
1296 * USB_EVENT_VBUS, this means the consumer device powered by the
1297 * charger is not connected anymore.
1298 * In this case, the charger goes into charge mode.
1300 dev_dbg(dev, "USB_EVENT_VBUS received, therefore the charger goes into charge mode\n");
1301 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1302 RT9455_CHARGE_MODE);
1304 dev_err(dev, "Failed to set charger in charge mode\n");
1309 dev_dbg(dev, "USB_EVENT_VBUS received, therefore IAICR is set to 500 mA\n");
1310 if (iaicr != RT9455_IAICR_500MA) {
1311 ret = regmap_field_write(info->regmap_fields[F_IAICR],
1312 RT9455_IAICR_500MA);
1314 dev_err(dev, "Failed to set IAICR value\n");
1322 static int rt9455_usb_event_id(struct rt9455_info *info,
1323 u8 opa_mode, u8 iaicr)
1325 struct device *dev = &info->client->dev;
1328 if (opa_mode == RT9455_CHARGE_MODE) {
1329 ret = rt9455_set_boost_voltage_before_boost_mode(info);
1331 dev_err(dev, "Failed to set boost output voltage before entering boost mode\n");
1335 * If the charger is in charge mode, and it has received
1336 * USB_EVENT_ID, this means a consumer device is connected and
1337 * it should be powered by the charger.
1338 * In this case, the charger goes into boost mode.
1340 dev_dbg(dev, "USB_EVENT_ID received, therefore the charger goes into boost mode\n");
1341 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1344 dev_err(dev, "Failed to set charger in boost mode\n");
1349 dev_dbg(dev, "USB_EVENT_ID received, therefore IAICR is set to its minimum value\n");
1350 if (iaicr != RT9455_IAICR_100MA) {
1351 ret = regmap_field_write(info->regmap_fields[F_IAICR],
1352 RT9455_IAICR_100MA);
1354 dev_err(dev, "Failed to set IAICR value\n");
1362 static int rt9455_usb_event_charger(struct rt9455_info *info,
1363 u8 opa_mode, u8 iaicr)
1365 struct device *dev = &info->client->dev;
1368 if (opa_mode == RT9455_BOOST_MODE) {
1369 ret = rt9455_set_voreg_before_charge_mode(info);
1371 dev_err(dev, "Failed to set VOREG before entering charge mode\n");
1375 * If the charger is in boost mode, and it has received
1376 * USB_EVENT_CHARGER, this means the consumer device powered by
1377 * the charger is not connected anymore.
1378 * In this case, the charger goes into charge mode.
1380 dev_dbg(dev, "USB_EVENT_CHARGER received, therefore the charger goes into charge mode\n");
1381 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1382 RT9455_CHARGE_MODE);
1384 dev_err(dev, "Failed to set charger in charge mode\n");
1389 dev_dbg(dev, "USB_EVENT_CHARGER received, therefore IAICR is set to no current limit\n");
1390 if (iaicr != RT9455_IAICR_NO_LIMIT) {
1391 ret = regmap_field_write(info->regmap_fields[F_IAICR],
1392 RT9455_IAICR_NO_LIMIT);
1394 dev_err(dev, "Failed to set IAICR value\n");
1402 static int rt9455_usb_event(struct notifier_block *nb,
1403 unsigned long event, void *power)
1405 struct rt9455_info *info = container_of(nb, struct rt9455_info, nb);
1406 struct device *dev = &info->client->dev;
1407 unsigned int opa_mode, iaicr;
1411 * Determine whether the charger is in charge mode
1414 ret = regmap_field_read(info->regmap_fields[F_OPA_MODE],
1417 dev_err(dev, "Failed to read OPA_MODE value\n");
1421 ret = regmap_field_read(info->regmap_fields[F_IAICR],
1424 dev_err(dev, "Failed to read IAICR value\n");
1428 dev_dbg(dev, "Received USB event %lu\n", event);
1430 case USB_EVENT_NONE:
1431 return rt9455_usb_event_none(info, opa_mode, iaicr);
1432 case USB_EVENT_VBUS:
1433 return rt9455_usb_event_vbus(info, opa_mode, iaicr);
1435 return rt9455_usb_event_id(info, opa_mode, iaicr);
1436 case USB_EVENT_CHARGER:
1437 return rt9455_usb_event_charger(info, opa_mode, iaicr);
1439 dev_err(dev, "Unknown USB event\n");
1445 static void rt9455_pwr_rdy_work_callback(struct work_struct *work)
1447 struct rt9455_info *info = container_of(work, struct rt9455_info,
1449 struct device *dev = &info->client->dev;
1450 unsigned int pwr_rdy;
1453 ret = regmap_field_read(info->regmap_fields[F_PWR_RDY], &pwr_rdy);
1455 dev_err(dev, "Failed to read PWR_RDY bit\n");
1459 case RT9455_PWR_FAULT:
1460 dev_dbg(dev, "Charger disconnected from power source\n");
1461 cancel_delayed_work_sync(&info->max_charging_time_work);
1463 case RT9455_PWR_GOOD:
1464 dev_dbg(dev, "Charger connected to power source\n");
1465 ret = regmap_field_write(info->regmap_fields[F_CHG_EN],
1466 RT9455_CHARGE_ENABLE);
1468 dev_err(dev, "Failed to enable charging\n");
1471 queue_delayed_work(system_power_efficient_wq,
1472 &info->max_charging_time_work,
1473 RT9455_MAX_CHARGING_TIME * HZ);
1477 * Notify userspace that the charger has been either connected to or
1478 * disconnected from the power source.
1480 power_supply_changed(info->charger);
1483 static void rt9455_max_charging_time_work_callback(struct work_struct *work)
1485 struct rt9455_info *info = container_of(work, struct rt9455_info,
1486 max_charging_time_work.work);
1487 struct device *dev = &info->client->dev;
1490 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");
1491 ret = regmap_field_write(info->regmap_fields[F_CHG_EN],
1492 RT9455_CHARGE_DISABLE);
1494 dev_err(dev, "Failed to disable charging\n");
1497 static void rt9455_batt_presence_work_callback(struct work_struct *work)
1499 struct rt9455_info *info = container_of(work, struct rt9455_info,
1500 batt_presence_work.work);
1501 struct device *dev = &info->client->dev;
1502 unsigned int irq1, mask1;
1505 ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &irq1);
1507 dev_err(dev, "Failed to read IRQ1 register\n");
1512 * If the battery is still absent, batt_presence_work is rescheduled.
1513 * Otherwise, max_charging_time is scheduled.
1515 if (irq1 & GET_MASK(F_BATAB)) {
1516 queue_delayed_work(system_power_efficient_wq,
1517 &info->batt_presence_work,
1518 RT9455_BATT_PRESENCE_DELAY * HZ);
1520 queue_delayed_work(system_power_efficient_wq,
1521 &info->max_charging_time_work,
1522 RT9455_MAX_CHARGING_TIME * HZ);
1524 ret = regmap_read(info->regmap, RT9455_REG_MASK1, &mask1);
1526 dev_err(dev, "Failed to read MASK1 register\n");
1530 if (mask1 & GET_MASK(F_BATABM)) {
1531 ret = regmap_field_write(info->regmap_fields[F_BATABM],
1534 dev_err(dev, "Failed to unmask BATAB interrupt\n");
1537 * Notify userspace that the battery is now connected to the
1540 power_supply_changed(info->charger);
1544 static const struct power_supply_desc rt9455_charger_desc = {
1545 .name = RT9455_DRIVER_NAME,
1546 .type = POWER_SUPPLY_TYPE_USB,
1547 .properties = rt9455_charger_properties,
1548 .num_properties = ARRAY_SIZE(rt9455_charger_properties),
1549 .get_property = rt9455_charger_get_property,
1552 static bool rt9455_is_writeable_reg(struct device *dev, unsigned int reg)
1555 case RT9455_REG_DEV_ID:
1556 case RT9455_REG_IRQ1:
1557 case RT9455_REG_IRQ2:
1558 case RT9455_REG_IRQ3:
1565 static bool rt9455_is_volatile_reg(struct device *dev, unsigned int reg)
1568 case RT9455_REG_DEV_ID:
1569 case RT9455_REG_CTRL5:
1570 case RT9455_REG_CTRL6:
1577 static const struct regmap_config rt9455_regmap_config = {
1580 .writeable_reg = rt9455_is_writeable_reg,
1581 .volatile_reg = rt9455_is_volatile_reg,
1582 .max_register = RT9455_REG_MASK3,
1583 .cache_type = REGCACHE_RBTREE,
1586 static int rt9455_probe(struct i2c_client *client,
1587 const struct i2c_device_id *id)
1589 struct i2c_adapter *adapter = client->adapter;
1590 struct device *dev = &client->dev;
1591 struct rt9455_info *info;
1592 struct power_supply_config rt9455_charger_config = {};
1594 * Mandatory device-specific data values. Also, VOREG and boost output
1595 * voltage are mandatory values, but they are stored in rt9455_info
1598 u32 ichrg, ieoc_percentage;
1599 /* Optional device-specific data values. */
1600 u32 mivr = -1, iaicr = -1;
1603 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1604 dev_err(dev, "No support for SMBUS_BYTE_DATA\n");
1607 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
1611 info->client = client;
1612 i2c_set_clientdata(client, info);
1614 info->regmap = devm_regmap_init_i2c(client,
1615 &rt9455_regmap_config);
1616 if (IS_ERR(info->regmap)) {
1617 dev_err(dev, "Failed to initialize register map\n");
1621 for (i = 0; i < F_MAX_FIELDS; i++) {
1622 info->regmap_fields[i] =
1623 devm_regmap_field_alloc(dev, info->regmap,
1624 rt9455_reg_fields[i]);
1625 if (IS_ERR(info->regmap_fields[i])) {
1627 "Failed to allocate regmap field = %d\n", i);
1628 return PTR_ERR(info->regmap_fields[i]);
1632 ret = rt9455_discover_charger(info, &ichrg, &ieoc_percentage,
1635 dev_err(dev, "Failed to discover charger\n");
1639 #if IS_ENABLED(CONFIG_USB_PHY)
1640 info->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1641 if (IS_ERR(info->usb_phy)) {
1642 dev_err(dev, "Failed to get USB transceiver\n");
1644 info->nb.notifier_call = rt9455_usb_event;
1645 ret = usb_register_notifier(info->usb_phy, &info->nb);
1647 dev_err(dev, "Failed to register USB notifier\n");
1649 * If usb_register_notifier() fails, set notifier_call
1650 * to NULL, to avoid calling usb_unregister_notifier().
1652 info->nb.notifier_call = NULL;
1657 INIT_DEFERRABLE_WORK(&info->pwr_rdy_work, rt9455_pwr_rdy_work_callback);
1658 INIT_DEFERRABLE_WORK(&info->max_charging_time_work,
1659 rt9455_max_charging_time_work_callback);
1660 INIT_DEFERRABLE_WORK(&info->batt_presence_work,
1661 rt9455_batt_presence_work_callback);
1663 rt9455_charger_config.of_node = dev->of_node;
1664 rt9455_charger_config.drv_data = info;
1665 rt9455_charger_config.supplied_to = rt9455_charger_supplied_to;
1666 rt9455_charger_config.num_supplicants =
1667 ARRAY_SIZE(rt9455_charger_supplied_to);
1668 ret = devm_request_threaded_irq(dev, client->irq, NULL,
1669 rt9455_irq_handler_thread,
1670 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1671 RT9455_DRIVER_NAME, info);
1673 dev_err(dev, "Failed to register IRQ handler\n");
1674 goto put_usb_notifier;
1677 ret = rt9455_hw_init(info, ichrg, ieoc_percentage, mivr, iaicr);
1679 dev_err(dev, "Failed to set charger to its default values\n");
1680 goto put_usb_notifier;
1683 info->charger = devm_power_supply_register(dev, &rt9455_charger_desc,
1684 &rt9455_charger_config);
1685 if (IS_ERR(info->charger)) {
1686 dev_err(dev, "Failed to register charger\n");
1687 ret = PTR_ERR(info->charger);
1688 goto put_usb_notifier;
1694 #if IS_ENABLED(CONFIG_USB_PHY)
1695 if (info->nb.notifier_call) {
1696 usb_unregister_notifier(info->usb_phy, &info->nb);
1697 info->nb.notifier_call = NULL;
1703 static int rt9455_remove(struct i2c_client *client)
1706 struct rt9455_info *info = i2c_get_clientdata(client);
1708 ret = rt9455_register_reset(info);
1710 dev_err(&info->client->dev, "Failed to set charger to its default values\n");
1712 #if IS_ENABLED(CONFIG_USB_PHY)
1713 if (info->nb.notifier_call)
1714 usb_unregister_notifier(info->usb_phy, &info->nb);
1717 cancel_delayed_work_sync(&info->pwr_rdy_work);
1718 cancel_delayed_work_sync(&info->max_charging_time_work);
1719 cancel_delayed_work_sync(&info->batt_presence_work);
1724 static const struct i2c_device_id rt9455_i2c_id_table[] = {
1725 { RT9455_DRIVER_NAME, 0 },
1728 MODULE_DEVICE_TABLE(i2c, rt9455_i2c_id_table);
1730 static const struct of_device_id rt9455_of_match[] = {
1731 { .compatible = "richtek,rt9455", },
1734 MODULE_DEVICE_TABLE(of, rt9455_of_match);
1737 static const struct acpi_device_id rt9455_i2c_acpi_match[] = {
1741 MODULE_DEVICE_TABLE(acpi, rt9455_i2c_acpi_match);
1744 static struct i2c_driver rt9455_driver = {
1745 .probe = rt9455_probe,
1746 .remove = rt9455_remove,
1747 .id_table = rt9455_i2c_id_table,
1749 .name = RT9455_DRIVER_NAME,
1750 .of_match_table = of_match_ptr(rt9455_of_match),
1751 .acpi_match_table = ACPI_PTR(rt9455_i2c_acpi_match),
1754 module_i2c_driver(rt9455_driver);
1756 MODULE_LICENSE("GPL");
1757 MODULE_AUTHOR("Anda-Maria Nicolae <anda-maria.nicolae@intel.com>");
1758 MODULE_DESCRIPTION("Richtek RT9455 Charger Driver");