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
196 * When the charger is in boost mode, REG02[7:2] represent boost output
199 /* REG02[7:2] (Boost output voltage) in uV */
200 static const int rt9455_boost_voltage_values[] = {
201 4425000, 4450000, 4475000, 4500000, 4525000, 4550000, 4575000, 4600000,
202 4625000, 4650000, 4675000, 4700000, 4725000, 4750000, 4775000, 4800000,
203 4825000, 4850000, 4875000, 4900000, 4925000, 4950000, 4975000, 5000000,
204 5025000, 5050000, 5075000, 5100000, 5125000, 5150000, 5175000, 5200000,
205 5225000, 5250000, 5275000, 5300000, 5325000, 5350000, 5375000, 5400000,
206 5425000, 5450000, 5475000, 5500000, 5525000, 5550000, 5575000, 5600000,
207 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000,
208 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000,
211 /* REG07[3:0] (VMREG) in uV */
212 static const int rt9455_vmreg_values[] = {
213 4200000, 4220000, 4240000, 4260000, 4280000, 4300000, 4320000, 4340000,
214 4360000, 4380000, 4400000, 4430000, 4450000, 4450000, 4450000, 4450000
217 /* REG05[5:4] (IEOC_PERCENTAGE) */
218 static const int rt9455_ieoc_percentage_values[] = {
222 /* REG05[1:0] (MIVR) in uV */
223 static const int rt9455_mivr_values[] = {
224 4000000, 4250000, 4500000, 5000000
227 /* REG05[1:0] (IAICR) in uA */
228 static const int rt9455_iaicr_values[] = {
229 100000, 500000, 1000000, 2000000
233 struct i2c_client *client;
234 struct regmap *regmap;
235 struct regmap_field *regmap_fields[F_MAX_FIELDS];
236 struct power_supply *charger;
237 #if IS_ENABLED(CONFIG_USB_PHY)
238 struct usb_phy *usb_phy;
239 struct notifier_block nb;
241 struct delayed_work pwr_rdy_work;
242 struct delayed_work max_charging_time_work;
243 struct delayed_work batt_presence_work;
249 * Iterate through each element of the 'tbl' array until an element whose value
250 * is greater than v is found. Return the index of the respective element,
251 * or the index of the last element in the array, if no such element is found.
253 static unsigned int rt9455_find_idx(const int tbl[], int tbl_size, int v)
258 * No need to iterate until the last index in the table because
259 * if no element greater than v is found in the table,
260 * or if only the last element is greater than v,
261 * function returns the index of the last element.
263 for (i = 0; i < tbl_size - 1; i++)
267 return (tbl_size - 1);
270 static int rt9455_get_field_val(struct rt9455_info *info,
271 enum rt9455_fields field,
272 const int tbl[], int tbl_size, int *val)
277 ret = regmap_field_read(info->regmap_fields[field], &v);
281 v = (v >= tbl_size) ? (tbl_size - 1) : v;
287 static int rt9455_set_field_val(struct rt9455_info *info,
288 enum rt9455_fields field,
289 const int tbl[], int tbl_size, int val)
291 unsigned int idx = rt9455_find_idx(tbl, tbl_size, val);
293 return regmap_field_write(info->regmap_fields[field], idx);
296 static int rt9455_register_reset(struct rt9455_info *info)
298 struct device *dev = &info->client->dev;
300 int ret, limit = 100;
302 ret = regmap_field_write(info->regmap_fields[F_RST], 0x01);
304 dev_err(dev, "Failed to set RST bit\n");
309 * To make sure that reset operation has finished, loop until RST bit
313 ret = regmap_field_read(info->regmap_fields[F_RST], &v);
315 dev_err(dev, "Failed to read RST bit\n");
322 usleep_range(10, 100);
331 /* Charger power supply property routines */
332 static enum power_supply_property rt9455_charger_properties[] = {
333 POWER_SUPPLY_PROP_STATUS,
334 POWER_SUPPLY_PROP_HEALTH,
335 POWER_SUPPLY_PROP_PRESENT,
336 POWER_SUPPLY_PROP_ONLINE,
337 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
338 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
339 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
340 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
341 POWER_SUPPLY_PROP_SCOPE,
342 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
343 POWER_SUPPLY_PROP_MODEL_NAME,
344 POWER_SUPPLY_PROP_MANUFACTURER,
347 static char *rt9455_charger_supplied_to[] = {
351 static int rt9455_charger_get_status(struct rt9455_info *info,
352 union power_supply_propval *val)
354 unsigned int v, pwr_rdy;
357 ret = regmap_field_read(info->regmap_fields[F_PWR_RDY],
360 dev_err(&info->client->dev, "Failed to read PWR_RDY bit\n");
365 * If PWR_RDY bit is unset, the battery is discharging. Otherwise,
366 * STAT bits value must be checked.
369 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
373 ret = regmap_field_read(info->regmap_fields[F_STAT], &v);
375 dev_err(&info->client->dev, "Failed to read STAT bits\n");
382 * If PWR_RDY bit is set, but STAT bits value is 0, the charger
383 * may be in one of the following cases:
384 * 1. CHG_EN bit is 0.
385 * 2. CHG_EN bit is 1 but the battery is not connected.
386 * In any of these cases, POWER_SUPPLY_STATUS_NOT_CHARGING is
389 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
392 val->intval = POWER_SUPPLY_STATUS_CHARGING;
395 val->intval = POWER_SUPPLY_STATUS_FULL;
398 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
403 static int rt9455_charger_get_health(struct rt9455_info *info,
404 union power_supply_propval *val)
406 struct device *dev = &info->client->dev;
410 val->intval = POWER_SUPPLY_HEALTH_GOOD;
412 ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &v);
414 dev_err(dev, "Failed to read IRQ1 register\n");
418 if (v & GET_MASK(F_TSDI)) {
419 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
422 if (v & GET_MASK(F_VINOVPI)) {
423 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
426 if (v & GET_MASK(F_BATAB)) {
427 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
431 ret = regmap_read(info->regmap, RT9455_REG_IRQ2, &v);
433 dev_err(dev, "Failed to read IRQ2 register\n");
437 if (v & GET_MASK(F_CHBATOVI)) {
438 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
441 if (v & GET_MASK(F_CH32MI)) {
442 val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
446 ret = regmap_read(info->regmap, RT9455_REG_IRQ3, &v);
448 dev_err(dev, "Failed to read IRQ3 register\n");
452 if (v & GET_MASK(F_BSTBUSOVI)) {
453 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
456 if (v & GET_MASK(F_BSTOLI)) {
457 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
460 if (v & GET_MASK(F_BSTLOWVI)) {
461 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
464 if (v & GET_MASK(F_BST32SI)) {
465 val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
469 ret = regmap_field_read(info->regmap_fields[F_STAT], &v);
471 dev_err(dev, "Failed to read STAT bits\n");
475 if (v == RT9455_FAULT) {
476 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
483 static int rt9455_charger_get_battery_presence(struct rt9455_info *info,
484 union power_supply_propval *val)
489 ret = regmap_field_read(info->regmap_fields[F_BATAB], &v);
491 dev_err(&info->client->dev, "Failed to read BATAB bit\n");
496 * Since BATAB is 1 when battery is NOT present and 0 otherwise,
497 * !BATAB is returned.
504 static int rt9455_charger_get_online(struct rt9455_info *info,
505 union power_supply_propval *val)
510 ret = regmap_field_read(info->regmap_fields[F_PWR_RDY], &v);
512 dev_err(&info->client->dev, "Failed to read PWR_RDY bit\n");
516 val->intval = (int)v;
521 static int rt9455_charger_get_current(struct rt9455_info *info,
522 union power_supply_propval *val)
527 ret = rt9455_get_field_val(info, F_ICHRG,
529 ARRAY_SIZE(rt9455_ichrg_values),
532 dev_err(&info->client->dev, "Failed to read ICHRG value\n");
541 static int rt9455_charger_get_current_max(struct rt9455_info *info,
542 union power_supply_propval *val)
544 int idx = ARRAY_SIZE(rt9455_ichrg_values) - 1;
546 val->intval = rt9455_ichrg_values[idx];
551 static int rt9455_charger_get_voltage(struct rt9455_info *info,
552 union power_supply_propval *val)
557 ret = rt9455_get_field_val(info, F_VOREG,
559 ARRAY_SIZE(rt9455_voreg_values),
562 dev_err(&info->client->dev, "Failed to read VOREG value\n");
566 val->intval = voltage;
571 static int rt9455_charger_get_voltage_max(struct rt9455_info *info,
572 union power_supply_propval *val)
574 int idx = ARRAY_SIZE(rt9455_vmreg_values) - 1;
576 val->intval = rt9455_vmreg_values[idx];
581 static int rt9455_charger_get_term_current(struct rt9455_info *info,
582 union power_supply_propval *val)
584 struct device *dev = &info->client->dev;
585 int ichrg, ieoc_percentage, ret;
587 ret = rt9455_get_field_val(info, F_ICHRG,
589 ARRAY_SIZE(rt9455_ichrg_values),
592 dev_err(dev, "Failed to read ICHRG value\n");
596 ret = rt9455_get_field_val(info, F_IEOC_PERCENTAGE,
597 rt9455_ieoc_percentage_values,
598 ARRAY_SIZE(rt9455_ieoc_percentage_values),
601 dev_err(dev, "Failed to read IEOC value\n");
605 val->intval = ichrg * ieoc_percentage / 100;
610 static int rt9455_charger_get_property(struct power_supply *psy,
611 enum power_supply_property psp,
612 union power_supply_propval *val)
614 struct rt9455_info *info = power_supply_get_drvdata(psy);
617 case POWER_SUPPLY_PROP_STATUS:
618 return rt9455_charger_get_status(info, val);
619 case POWER_SUPPLY_PROP_HEALTH:
620 return rt9455_charger_get_health(info, val);
621 case POWER_SUPPLY_PROP_PRESENT:
622 return rt9455_charger_get_battery_presence(info, val);
623 case POWER_SUPPLY_PROP_ONLINE:
624 return rt9455_charger_get_online(info, val);
625 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
626 return rt9455_charger_get_current(info, val);
627 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
628 return rt9455_charger_get_current_max(info, val);
629 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
630 return rt9455_charger_get_voltage(info, val);
631 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
632 return rt9455_charger_get_voltage_max(info, val);
633 case POWER_SUPPLY_PROP_SCOPE:
634 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
636 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
637 return rt9455_charger_get_term_current(info, val);
638 case POWER_SUPPLY_PROP_MODEL_NAME:
639 val->strval = RT9455_MODEL_NAME;
641 case POWER_SUPPLY_PROP_MANUFACTURER:
642 val->strval = RT9455_MANUFACTURER;
649 static int rt9455_hw_init(struct rt9455_info *info, u32 ichrg,
653 struct device *dev = &info->client->dev;
656 ret = rt9455_register_reset(info);
658 dev_err(dev, "Power On Reset failed\n");
662 /* Set TE bit in order to enable end of charge detection */
663 ret = regmap_field_write(info->regmap_fields[F_TE], 1);
665 dev_err(dev, "Failed to set TE bit\n");
669 /* Set TE_SHDN_EN bit in order to enable end of charge detection */
670 ret = regmap_field_write(info->regmap_fields[F_TE_SHDN_EN], 1);
672 dev_err(dev, "Failed to set TE_SHDN_EN bit\n");
677 * Set BATD_EN bit in order to enable battery detection
678 * when charging is done
680 ret = regmap_field_write(info->regmap_fields[F_BATD_EN], 1);
682 dev_err(dev, "Failed to set BATD_EN bit\n");
687 * Disable Safety Timer. In charge mode, this timer terminates charging
688 * if no read or write via I2C is done within 32 minutes. This timer
689 * avoids overcharging the baterry when the OS is not loaded and the
690 * charger is connected to a power source.
691 * In boost mode, this timer triggers BST32SI interrupt if no read or
692 * write via I2C is done within 32 seconds.
693 * When the OS is loaded and the charger driver is inserted, it is used
694 * delayed_work, named max_charging_time_work, to avoid overcharging
697 ret = regmap_field_write(info->regmap_fields[F_TMR_EN], 0x00);
699 dev_err(dev, "Failed to disable Safety Timer\n");
703 /* Set ICHRG to value retrieved from device-specific data */
704 ret = rt9455_set_field_val(info, F_ICHRG,
706 ARRAY_SIZE(rt9455_ichrg_values), ichrg);
708 dev_err(dev, "Failed to set ICHRG value\n");
712 /* Set IEOC Percentage to value retrieved from device-specific data */
713 ret = rt9455_set_field_val(info, F_IEOC_PERCENTAGE,
714 rt9455_ieoc_percentage_values,
715 ARRAY_SIZE(rt9455_ieoc_percentage_values),
718 dev_err(dev, "Failed to set IEOC Percentage value\n");
722 /* Set VOREG to value retrieved from device-specific data */
723 ret = rt9455_set_field_val(info, F_VOREG,
725 ARRAY_SIZE(rt9455_voreg_values),
728 dev_err(dev, "Failed to set VOREG value\n");
732 /* Set VMREG value to maximum (4.45V). */
733 idx = ARRAY_SIZE(rt9455_vmreg_values) - 1;
734 ret = rt9455_set_field_val(info, F_VMREG,
736 ARRAY_SIZE(rt9455_vmreg_values),
737 rt9455_vmreg_values[idx]);
739 dev_err(dev, "Failed to set VMREG value\n");
744 * Set MIVR to value retrieved from device-specific data.
745 * If no value is specified, default value for MIVR is 4.5V.
750 ret = rt9455_set_field_val(info, F_MIVR,
752 ARRAY_SIZE(rt9455_mivr_values), mivr);
754 dev_err(dev, "Failed to set MIVR value\n");
759 * Set IAICR to value retrieved from device-specific data.
760 * If no value is specified, default value for IAICR is 500 mA.
765 ret = rt9455_set_field_val(info, F_IAICR,
767 ARRAY_SIZE(rt9455_iaicr_values), iaicr);
769 dev_err(dev, "Failed to set IAICR value\n");
774 * Set IAICR_INT bit so that IAICR value is determined by IAICR bits
775 * and not by OTG pin.
777 ret = regmap_field_write(info->regmap_fields[F_IAICR_INT], 0x01);
779 dev_err(dev, "Failed to set IAICR_INT bit\n");
784 * Disable CHMIVRI interrupt. Because the driver sets MIVR value,
785 * CHMIVRI is triggered, but there is no action to be taken by the
786 * driver when CHMIVRI is triggered.
788 ret = regmap_field_write(info->regmap_fields[F_CHMIVRIM], 0x01);
790 dev_err(dev, "Failed to mask CHMIVRI interrupt\n");
797 #if IS_ENABLED(CONFIG_USB_PHY)
799 * Before setting the charger into boost mode, boost output voltage is
800 * set. This is needed because boost output voltage may differ from battery
801 * regulation voltage. F_VOREG bits represent either battery regulation voltage
802 * or boost output voltage, depending on the mode the charger is. Both battery
803 * regulation voltage and boost output voltage are read from DT/ACPI during
806 static int rt9455_set_boost_voltage_before_boost_mode(struct rt9455_info *info)
808 struct device *dev = &info->client->dev;
811 ret = rt9455_set_field_val(info, F_VOREG,
812 rt9455_boost_voltage_values,
813 ARRAY_SIZE(rt9455_boost_voltage_values),
814 info->boost_voltage);
816 dev_err(dev, "Failed to set boost output voltage value\n");
825 * Before setting the charger into charge mode, battery regulation voltage is
826 * set. This is needed because boost output voltage may differ from battery
827 * regulation voltage. F_VOREG bits represent either battery regulation voltage
828 * or boost output voltage, depending on the mode the charger is. Both battery
829 * regulation voltage and boost output voltage are read from DT/ACPI during
832 static int rt9455_set_voreg_before_charge_mode(struct rt9455_info *info)
834 struct device *dev = &info->client->dev;
837 ret = rt9455_set_field_val(info, F_VOREG,
839 ARRAY_SIZE(rt9455_voreg_values),
842 dev_err(dev, "Failed to set VOREG value\n");
849 static int rt9455_irq_handler_check_irq1_register(struct rt9455_info *info,
850 bool *_is_battery_absent,
851 bool *_alert_userspace)
853 unsigned int irq1, mask1, mask2;
854 struct device *dev = &info->client->dev;
855 bool is_battery_absent = false;
856 bool alert_userspace = false;
859 ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &irq1);
861 dev_err(dev, "Failed to read IRQ1 register\n");
865 ret = regmap_read(info->regmap, RT9455_REG_MASK1, &mask1);
867 dev_err(dev, "Failed to read MASK1 register\n");
871 if (irq1 & GET_MASK(F_TSDI)) {
872 dev_err(dev, "Thermal shutdown fault occurred\n");
873 alert_userspace = true;
876 if (irq1 & GET_MASK(F_VINOVPI)) {
877 dev_err(dev, "Overvoltage input occurred\n");
878 alert_userspace = true;
881 if (irq1 & GET_MASK(F_BATAB)) {
882 dev_err(dev, "Battery absence occurred\n");
883 is_battery_absent = true;
884 alert_userspace = true;
886 if ((mask1 & GET_MASK(F_BATABM)) == 0) {
887 ret = regmap_field_write(info->regmap_fields[F_BATABM],
890 dev_err(dev, "Failed to mask BATAB interrupt\n");
895 ret = regmap_read(info->regmap, RT9455_REG_MASK2, &mask2);
897 dev_err(dev, "Failed to read MASK2 register\n");
901 if (mask2 & GET_MASK(F_CHTERMIM)) {
902 ret = regmap_field_write(
903 info->regmap_fields[F_CHTERMIM], 0x00);
905 dev_err(dev, "Failed to unmask CHTERMI interrupt\n");
910 if (mask2 & GET_MASK(F_CHRCHGIM)) {
911 ret = regmap_field_write(
912 info->regmap_fields[F_CHRCHGIM], 0x00);
914 dev_err(dev, "Failed to unmask CHRCHGI interrupt\n");
920 * When the battery is absent, max_charging_time_work is
921 * cancelled, since no charging is done.
923 cancel_delayed_work_sync(&info->max_charging_time_work);
925 * Since no interrupt is triggered when the battery is
926 * reconnected, max_charging_time_work is not rescheduled.
927 * Therefore, batt_presence_work is scheduled to check whether
928 * the battery is still absent or not.
930 queue_delayed_work(system_power_efficient_wq,
931 &info->batt_presence_work,
932 RT9455_BATT_PRESENCE_DELAY * HZ);
935 *_is_battery_absent = is_battery_absent;
938 *_alert_userspace = alert_userspace;
943 static int rt9455_irq_handler_check_irq2_register(struct rt9455_info *info,
944 bool is_battery_absent,
945 bool *_alert_userspace)
947 unsigned int irq2, mask2;
948 struct device *dev = &info->client->dev;
949 bool alert_userspace = false;
952 ret = regmap_read(info->regmap, RT9455_REG_IRQ2, &irq2);
954 dev_err(dev, "Failed to read IRQ2 register\n");
958 ret = regmap_read(info->regmap, RT9455_REG_MASK2, &mask2);
960 dev_err(dev, "Failed to read MASK2 register\n");
964 if (irq2 & GET_MASK(F_CHRVPI)) {
965 dev_dbg(dev, "Charger fault occurred\n");
967 * CHRVPI bit is set in 2 cases:
968 * 1. when the power source is connected to the charger.
969 * 2. when the power source is disconnected from the charger.
970 * To identify the case, PWR_RDY bit is checked. Because
971 * PWR_RDY bit is set / cleared after CHRVPI interrupt is
972 * triggered, it is used delayed_work to later read PWR_RDY bit.
973 * Also, do not set to true alert_userspace, because there is no
974 * need to notify userspace when CHRVPI interrupt has occurred.
975 * Userspace will be notified after PWR_RDY bit is read.
977 queue_delayed_work(system_power_efficient_wq,
979 RT9455_PWR_RDY_DELAY * HZ);
981 if (irq2 & GET_MASK(F_CHBATOVI)) {
982 dev_err(dev, "Battery OVP occurred\n");
983 alert_userspace = true;
985 if (irq2 & GET_MASK(F_CHTERMI)) {
986 dev_dbg(dev, "Charge terminated\n");
987 if (!is_battery_absent) {
988 if ((mask2 & GET_MASK(F_CHTERMIM)) == 0) {
989 ret = regmap_field_write(
990 info->regmap_fields[F_CHTERMIM], 0x01);
992 dev_err(dev, "Failed to mask CHTERMI interrupt\n");
996 * Update MASK2 value, since CHTERMIM bit is
999 mask2 = mask2 | GET_MASK(F_CHTERMIM);
1001 cancel_delayed_work_sync(&info->max_charging_time_work);
1002 alert_userspace = true;
1005 if (irq2 & GET_MASK(F_CHRCHGI)) {
1006 dev_dbg(dev, "Recharge request\n");
1007 ret = regmap_field_write(info->regmap_fields[F_CHG_EN],
1008 RT9455_CHARGE_ENABLE);
1010 dev_err(dev, "Failed to enable charging\n");
1013 if (mask2 & GET_MASK(F_CHTERMIM)) {
1014 ret = regmap_field_write(
1015 info->regmap_fields[F_CHTERMIM], 0x00);
1017 dev_err(dev, "Failed to unmask CHTERMI interrupt\n");
1020 /* Update MASK2 value, since CHTERMIM bit is cleared. */
1021 mask2 = mask2 & ~GET_MASK(F_CHTERMIM);
1023 if (!is_battery_absent) {
1025 * No need to check whether the charger is connected to
1026 * power source when CHRCHGI is received, since CHRCHGI
1027 * is not triggered if the charger is not connected to
1030 queue_delayed_work(system_power_efficient_wq,
1031 &info->max_charging_time_work,
1032 RT9455_MAX_CHARGING_TIME * HZ);
1033 alert_userspace = true;
1036 if (irq2 & GET_MASK(F_CH32MI)) {
1037 dev_err(dev, "Charger fault. 32 mins timeout occurred\n");
1038 alert_userspace = true;
1040 if (irq2 & GET_MASK(F_CHTREGI)) {
1042 "Charger warning. Thermal regulation loop active\n");
1043 alert_userspace = true;
1045 if (irq2 & GET_MASK(F_CHMIVRI)) {
1047 "Charger warning. Input voltage MIVR loop active\n");
1050 if (alert_userspace)
1051 *_alert_userspace = alert_userspace;
1056 static int rt9455_irq_handler_check_irq3_register(struct rt9455_info *info,
1057 bool *_alert_userspace)
1059 unsigned int irq3, mask3;
1060 struct device *dev = &info->client->dev;
1061 bool alert_userspace = false;
1064 ret = regmap_read(info->regmap, RT9455_REG_IRQ3, &irq3);
1066 dev_err(dev, "Failed to read IRQ3 register\n");
1070 ret = regmap_read(info->regmap, RT9455_REG_MASK3, &mask3);
1072 dev_err(dev, "Failed to read MASK3 register\n");
1076 if (irq3 & GET_MASK(F_BSTBUSOVI)) {
1077 dev_err(dev, "Boost fault. Overvoltage input occurred\n");
1078 alert_userspace = true;
1080 if (irq3 & GET_MASK(F_BSTOLI)) {
1081 dev_err(dev, "Boost fault. Overload\n");
1082 alert_userspace = true;
1084 if (irq3 & GET_MASK(F_BSTLOWVI)) {
1085 dev_err(dev, "Boost fault. Battery voltage too low\n");
1086 alert_userspace = true;
1088 if (irq3 & GET_MASK(F_BST32SI)) {
1089 dev_err(dev, "Boost fault. 32 seconds timeout occurred.\n");
1090 alert_userspace = true;
1093 if (alert_userspace) {
1094 dev_info(dev, "Boost fault occurred, therefore the charger goes into charge mode\n");
1095 ret = rt9455_set_voreg_before_charge_mode(info);
1097 dev_err(dev, "Failed to set VOREG before entering charge mode\n");
1100 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1101 RT9455_CHARGE_MODE);
1103 dev_err(dev, "Failed to set charger in charge mode\n");
1106 *_alert_userspace = alert_userspace;
1112 static irqreturn_t rt9455_irq_handler_thread(int irq, void *data)
1114 struct rt9455_info *info = data;
1116 bool alert_userspace = false;
1117 bool is_battery_absent = false;
1118 unsigned int status;
1124 dev = &info->client->dev;
1126 if (irq != info->client->irq) {
1127 dev_err(dev, "Interrupt is not for RT9455 charger\n");
1131 ret = regmap_field_read(info->regmap_fields[F_STAT], &status);
1133 dev_err(dev, "Failed to read STAT bits\n");
1136 dev_dbg(dev, "Charger status is %d\n", status);
1139 * Each function that processes an IRQ register receives as output
1140 * parameter alert_userspace pointer. alert_userspace is set to true
1141 * in such a function only if an interrupt has occurred in the
1142 * respective interrupt register. This way, it is avoided the following
1143 * case: interrupt occurs only in IRQ1 register,
1144 * rt9455_irq_handler_check_irq1_register() function sets to true
1145 * alert_userspace, but rt9455_irq_handler_check_irq2_register()
1146 * and rt9455_irq_handler_check_irq3_register() functions set to false
1147 * alert_userspace and power_supply_changed() is never called.
1149 ret = rt9455_irq_handler_check_irq1_register(info, &is_battery_absent,
1152 dev_err(dev, "Failed to handle IRQ1 register\n");
1156 ret = rt9455_irq_handler_check_irq2_register(info, is_battery_absent,
1159 dev_err(dev, "Failed to handle IRQ2 register\n");
1163 ret = rt9455_irq_handler_check_irq3_register(info, &alert_userspace);
1165 dev_err(dev, "Failed to handle IRQ3 register\n");
1169 if (alert_userspace) {
1171 * Sometimes, an interrupt occurs while rt9455_probe() function
1172 * is executing and power_supply_register() is not yet called.
1173 * Do not call power_supply_changed() in this case.
1176 power_supply_changed(info->charger);
1182 static int rt9455_discover_charger(struct rt9455_info *info, u32 *ichrg,
1183 u32 *ieoc_percentage,
1184 u32 *mivr, u32 *iaicr)
1186 struct device *dev = &info->client->dev;
1189 if (!dev->of_node && !ACPI_HANDLE(dev)) {
1190 dev_err(dev, "No support for either device tree or ACPI\n");
1194 * ICHRG, IEOC_PERCENTAGE, VOREG and boost output voltage are mandatory
1197 ret = device_property_read_u32(dev, "richtek,output-charge-current",
1200 dev_err(dev, "Error: missing \"output-charge-current\" property\n");
1204 ret = device_property_read_u32(dev, "richtek,end-of-charge-percentage",
1207 dev_err(dev, "Error: missing \"end-of-charge-percentage\" property\n");
1211 ret = device_property_read_u32(dev,
1212 "richtek,battery-regulation-voltage",
1215 dev_err(dev, "Error: missing \"battery-regulation-voltage\" property\n");
1219 ret = device_property_read_u32(dev, "richtek,boost-output-voltage",
1220 &info->boost_voltage);
1222 dev_err(dev, "Error: missing \"boost-output-voltage\" property\n");
1227 * MIVR and IAICR are optional parameters. Do not return error if one of
1228 * them is not present in ACPI table or device tree specification.
1230 device_property_read_u32(dev, "richtek,min-input-voltage-regulation",
1232 device_property_read_u32(dev, "richtek,avg-input-current-regulation",
1238 #if IS_ENABLED(CONFIG_USB_PHY)
1239 static int rt9455_usb_event_none(struct rt9455_info *info,
1240 u8 opa_mode, u8 iaicr)
1242 struct device *dev = &info->client->dev;
1245 if (opa_mode == RT9455_BOOST_MODE) {
1246 ret = rt9455_set_voreg_before_charge_mode(info);
1248 dev_err(dev, "Failed to set VOREG before entering charge mode\n");
1252 * If the charger is in boost mode, and it has received
1253 * USB_EVENT_NONE, this means the consumer device powered by the
1254 * charger is not connected anymore.
1255 * In this case, the charger goes into charge mode.
1257 dev_dbg(dev, "USB_EVENT_NONE received, therefore the charger goes into charge mode\n");
1258 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1259 RT9455_CHARGE_MODE);
1261 dev_err(dev, "Failed to set charger in charge mode\n");
1266 dev_dbg(dev, "USB_EVENT_NONE received, therefore IAICR is set to its minimum value\n");
1267 if (iaicr != RT9455_IAICR_100MA) {
1268 ret = regmap_field_write(info->regmap_fields[F_IAICR],
1269 RT9455_IAICR_100MA);
1271 dev_err(dev, "Failed to set IAICR value\n");
1279 static int rt9455_usb_event_vbus(struct rt9455_info *info,
1280 u8 opa_mode, u8 iaicr)
1282 struct device *dev = &info->client->dev;
1285 if (opa_mode == RT9455_BOOST_MODE) {
1286 ret = rt9455_set_voreg_before_charge_mode(info);
1288 dev_err(dev, "Failed to set VOREG before entering charge mode\n");
1292 * If the charger is in boost mode, and it has received
1293 * USB_EVENT_VBUS, this means the consumer device powered by the
1294 * charger is not connected anymore.
1295 * In this case, the charger goes into charge mode.
1297 dev_dbg(dev, "USB_EVENT_VBUS received, therefore the charger goes into charge mode\n");
1298 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1299 RT9455_CHARGE_MODE);
1301 dev_err(dev, "Failed to set charger in charge mode\n");
1306 dev_dbg(dev, "USB_EVENT_VBUS received, therefore IAICR is set to 500 mA\n");
1307 if (iaicr != RT9455_IAICR_500MA) {
1308 ret = regmap_field_write(info->regmap_fields[F_IAICR],
1309 RT9455_IAICR_500MA);
1311 dev_err(dev, "Failed to set IAICR value\n");
1319 static int rt9455_usb_event_id(struct rt9455_info *info,
1320 u8 opa_mode, u8 iaicr)
1322 struct device *dev = &info->client->dev;
1325 if (opa_mode == RT9455_CHARGE_MODE) {
1326 ret = rt9455_set_boost_voltage_before_boost_mode(info);
1328 dev_err(dev, "Failed to set boost output voltage before entering boost mode\n");
1332 * If the charger is in charge mode, and it has received
1333 * USB_EVENT_ID, this means a consumer device is connected and
1334 * it should be powered by the charger.
1335 * In this case, the charger goes into boost mode.
1337 dev_dbg(dev, "USB_EVENT_ID received, therefore the charger goes into boost mode\n");
1338 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1341 dev_err(dev, "Failed to set charger in boost mode\n");
1346 dev_dbg(dev, "USB_EVENT_ID received, therefore IAICR is set to its minimum value\n");
1347 if (iaicr != RT9455_IAICR_100MA) {
1348 ret = regmap_field_write(info->regmap_fields[F_IAICR],
1349 RT9455_IAICR_100MA);
1351 dev_err(dev, "Failed to set IAICR value\n");
1359 static int rt9455_usb_event_charger(struct rt9455_info *info,
1360 u8 opa_mode, u8 iaicr)
1362 struct device *dev = &info->client->dev;
1365 if (opa_mode == RT9455_BOOST_MODE) {
1366 ret = rt9455_set_voreg_before_charge_mode(info);
1368 dev_err(dev, "Failed to set VOREG before entering charge mode\n");
1372 * If the charger is in boost mode, and it has received
1373 * USB_EVENT_CHARGER, this means the consumer device powered by
1374 * the charger is not connected anymore.
1375 * In this case, the charger goes into charge mode.
1377 dev_dbg(dev, "USB_EVENT_CHARGER received, therefore the charger goes into charge mode\n");
1378 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1379 RT9455_CHARGE_MODE);
1381 dev_err(dev, "Failed to set charger in charge mode\n");
1386 dev_dbg(dev, "USB_EVENT_CHARGER received, therefore IAICR is set to no current limit\n");
1387 if (iaicr != RT9455_IAICR_NO_LIMIT) {
1388 ret = regmap_field_write(info->regmap_fields[F_IAICR],
1389 RT9455_IAICR_NO_LIMIT);
1391 dev_err(dev, "Failed to set IAICR value\n");
1399 static int rt9455_usb_event(struct notifier_block *nb,
1400 unsigned long event, void *power)
1402 struct rt9455_info *info = container_of(nb, struct rt9455_info, nb);
1403 struct device *dev = &info->client->dev;
1404 unsigned int opa_mode, iaicr;
1408 * Determine whether the charger is in charge mode
1411 ret = regmap_field_read(info->regmap_fields[F_OPA_MODE],
1414 dev_err(dev, "Failed to read OPA_MODE value\n");
1418 ret = regmap_field_read(info->regmap_fields[F_IAICR],
1421 dev_err(dev, "Failed to read IAICR value\n");
1425 dev_dbg(dev, "Received USB event %lu\n", event);
1427 case USB_EVENT_NONE:
1428 return rt9455_usb_event_none(info, opa_mode, iaicr);
1429 case USB_EVENT_VBUS:
1430 return rt9455_usb_event_vbus(info, opa_mode, iaicr);
1432 return rt9455_usb_event_id(info, opa_mode, iaicr);
1433 case USB_EVENT_CHARGER:
1434 return rt9455_usb_event_charger(info, opa_mode, iaicr);
1436 dev_err(dev, "Unknown USB event\n");
1442 static void rt9455_pwr_rdy_work_callback(struct work_struct *work)
1444 struct rt9455_info *info = container_of(work, struct rt9455_info,
1446 struct device *dev = &info->client->dev;
1447 unsigned int pwr_rdy;
1450 ret = regmap_field_read(info->regmap_fields[F_PWR_RDY], &pwr_rdy);
1452 dev_err(dev, "Failed to read PWR_RDY bit\n");
1456 case RT9455_PWR_FAULT:
1457 dev_dbg(dev, "Charger disconnected from power source\n");
1458 cancel_delayed_work_sync(&info->max_charging_time_work);
1460 case RT9455_PWR_GOOD:
1461 dev_dbg(dev, "Charger connected to power source\n");
1462 ret = regmap_field_write(info->regmap_fields[F_CHG_EN],
1463 RT9455_CHARGE_ENABLE);
1465 dev_err(dev, "Failed to enable charging\n");
1468 queue_delayed_work(system_power_efficient_wq,
1469 &info->max_charging_time_work,
1470 RT9455_MAX_CHARGING_TIME * HZ);
1474 * Notify userspace that the charger has been either connected to or
1475 * disconnected from the power source.
1477 power_supply_changed(info->charger);
1480 static void rt9455_max_charging_time_work_callback(struct work_struct *work)
1482 struct rt9455_info *info = container_of(work, struct rt9455_info,
1483 max_charging_time_work.work);
1484 struct device *dev = &info->client->dev;
1487 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");
1488 ret = regmap_field_write(info->regmap_fields[F_CHG_EN],
1489 RT9455_CHARGE_DISABLE);
1491 dev_err(dev, "Failed to disable charging\n");
1494 static void rt9455_batt_presence_work_callback(struct work_struct *work)
1496 struct rt9455_info *info = container_of(work, struct rt9455_info,
1497 batt_presence_work.work);
1498 struct device *dev = &info->client->dev;
1499 unsigned int irq1, mask1;
1502 ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &irq1);
1504 dev_err(dev, "Failed to read IRQ1 register\n");
1509 * If the battery is still absent, batt_presence_work is rescheduled.
1510 * Otherwise, max_charging_time is scheduled.
1512 if (irq1 & GET_MASK(F_BATAB)) {
1513 queue_delayed_work(system_power_efficient_wq,
1514 &info->batt_presence_work,
1515 RT9455_BATT_PRESENCE_DELAY * HZ);
1517 queue_delayed_work(system_power_efficient_wq,
1518 &info->max_charging_time_work,
1519 RT9455_MAX_CHARGING_TIME * HZ);
1521 ret = regmap_read(info->regmap, RT9455_REG_MASK1, &mask1);
1523 dev_err(dev, "Failed to read MASK1 register\n");
1527 if (mask1 & GET_MASK(F_BATABM)) {
1528 ret = regmap_field_write(info->regmap_fields[F_BATABM],
1531 dev_err(dev, "Failed to unmask BATAB interrupt\n");
1534 * Notify userspace that the battery is now connected to the
1537 power_supply_changed(info->charger);
1541 static const struct power_supply_desc rt9455_charger_desc = {
1542 .name = RT9455_DRIVER_NAME,
1543 .type = POWER_SUPPLY_TYPE_USB,
1544 .properties = rt9455_charger_properties,
1545 .num_properties = ARRAY_SIZE(rt9455_charger_properties),
1546 .get_property = rt9455_charger_get_property,
1549 static bool rt9455_is_writeable_reg(struct device *dev, unsigned int reg)
1552 case RT9455_REG_DEV_ID:
1553 case RT9455_REG_IRQ1:
1554 case RT9455_REG_IRQ2:
1555 case RT9455_REG_IRQ3:
1562 static bool rt9455_is_volatile_reg(struct device *dev, unsigned int reg)
1565 case RT9455_REG_DEV_ID:
1566 case RT9455_REG_CTRL5:
1567 case RT9455_REG_CTRL6:
1574 static const struct regmap_config rt9455_regmap_config = {
1577 .writeable_reg = rt9455_is_writeable_reg,
1578 .volatile_reg = rt9455_is_volatile_reg,
1579 .max_register = RT9455_REG_MASK3,
1580 .cache_type = REGCACHE_RBTREE,
1583 static int rt9455_probe(struct i2c_client *client)
1585 struct i2c_adapter *adapter = client->adapter;
1586 struct device *dev = &client->dev;
1587 struct rt9455_info *info;
1588 struct power_supply_config rt9455_charger_config = {};
1590 * Mandatory device-specific data values. Also, VOREG and boost output
1591 * voltage are mandatory values, but they are stored in rt9455_info
1594 u32 ichrg, ieoc_percentage;
1595 /* Optional device-specific data values. */
1596 u32 mivr = -1, iaicr = -1;
1599 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1600 dev_err(dev, "No support for SMBUS_BYTE_DATA\n");
1603 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
1607 info->client = client;
1608 i2c_set_clientdata(client, info);
1610 info->regmap = devm_regmap_init_i2c(client,
1611 &rt9455_regmap_config);
1612 if (IS_ERR(info->regmap)) {
1613 dev_err(dev, "Failed to initialize register map\n");
1617 for (i = 0; i < F_MAX_FIELDS; i++) {
1618 info->regmap_fields[i] =
1619 devm_regmap_field_alloc(dev, info->regmap,
1620 rt9455_reg_fields[i]);
1621 if (IS_ERR(info->regmap_fields[i])) {
1623 "Failed to allocate regmap field = %d\n", i);
1624 return PTR_ERR(info->regmap_fields[i]);
1628 ret = rt9455_discover_charger(info, &ichrg, &ieoc_percentage,
1631 dev_err(dev, "Failed to discover charger\n");
1635 #if IS_ENABLED(CONFIG_USB_PHY)
1636 info->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1637 if (IS_ERR(info->usb_phy)) {
1638 dev_err(dev, "Failed to get USB transceiver\n");
1640 info->nb.notifier_call = rt9455_usb_event;
1641 ret = usb_register_notifier(info->usb_phy, &info->nb);
1643 dev_err(dev, "Failed to register USB notifier\n");
1645 * If usb_register_notifier() fails, set notifier_call
1646 * to NULL, to avoid calling usb_unregister_notifier().
1648 info->nb.notifier_call = NULL;
1653 INIT_DEFERRABLE_WORK(&info->pwr_rdy_work, rt9455_pwr_rdy_work_callback);
1654 INIT_DEFERRABLE_WORK(&info->max_charging_time_work,
1655 rt9455_max_charging_time_work_callback);
1656 INIT_DEFERRABLE_WORK(&info->batt_presence_work,
1657 rt9455_batt_presence_work_callback);
1659 rt9455_charger_config.of_node = dev->of_node;
1660 rt9455_charger_config.drv_data = info;
1661 rt9455_charger_config.supplied_to = rt9455_charger_supplied_to;
1662 rt9455_charger_config.num_supplicants =
1663 ARRAY_SIZE(rt9455_charger_supplied_to);
1664 ret = devm_request_threaded_irq(dev, client->irq, NULL,
1665 rt9455_irq_handler_thread,
1666 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1667 RT9455_DRIVER_NAME, info);
1669 dev_err(dev, "Failed to register IRQ handler\n");
1670 goto put_usb_notifier;
1673 ret = rt9455_hw_init(info, ichrg, ieoc_percentage, mivr, iaicr);
1675 dev_err(dev, "Failed to set charger to its default values\n");
1676 goto put_usb_notifier;
1679 info->charger = devm_power_supply_register(dev, &rt9455_charger_desc,
1680 &rt9455_charger_config);
1681 if (IS_ERR(info->charger)) {
1682 dev_err(dev, "Failed to register charger\n");
1683 ret = PTR_ERR(info->charger);
1684 goto put_usb_notifier;
1690 #if IS_ENABLED(CONFIG_USB_PHY)
1691 if (info->nb.notifier_call) {
1692 usb_unregister_notifier(info->usb_phy, &info->nb);
1693 info->nb.notifier_call = NULL;
1699 static void rt9455_remove(struct i2c_client *client)
1702 struct rt9455_info *info = i2c_get_clientdata(client);
1704 ret = rt9455_register_reset(info);
1706 dev_err(&info->client->dev, "Failed to set charger to its default values\n");
1708 #if IS_ENABLED(CONFIG_USB_PHY)
1709 if (info->nb.notifier_call)
1710 usb_unregister_notifier(info->usb_phy, &info->nb);
1713 cancel_delayed_work_sync(&info->pwr_rdy_work);
1714 cancel_delayed_work_sync(&info->max_charging_time_work);
1715 cancel_delayed_work_sync(&info->batt_presence_work);
1718 static const struct i2c_device_id rt9455_i2c_id_table[] = {
1719 { RT9455_DRIVER_NAME, 0 },
1722 MODULE_DEVICE_TABLE(i2c, rt9455_i2c_id_table);
1724 static const struct of_device_id rt9455_of_match[] __maybe_unused = {
1725 { .compatible = "richtek,rt9455", },
1728 MODULE_DEVICE_TABLE(of, rt9455_of_match);
1731 static const struct acpi_device_id rt9455_i2c_acpi_match[] = {
1735 MODULE_DEVICE_TABLE(acpi, rt9455_i2c_acpi_match);
1738 static struct i2c_driver rt9455_driver = {
1739 .probe = rt9455_probe,
1740 .remove = rt9455_remove,
1741 .id_table = rt9455_i2c_id_table,
1743 .name = RT9455_DRIVER_NAME,
1744 .of_match_table = of_match_ptr(rt9455_of_match),
1745 .acpi_match_table = ACPI_PTR(rt9455_i2c_acpi_match),
1748 module_i2c_driver(rt9455_driver);
1750 MODULE_LICENSE("GPL");
1751 MODULE_AUTHOR("Anda-Maria Nicolae <anda-maria.nicolae@intel.com>");
1752 MODULE_DESCRIPTION("Richtek RT9455 Charger Driver");