2 * Driver for Richtek RT9455WSC battery charger.
4 * Copyright (C) 2015 Intel Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/module.h>
18 #include <linux/interrupt.h>
19 #include <linux/delay.h>
20 #include <linux/of_irq.h>
21 #include <linux/of_device.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/power_supply.h>
24 #include <linux/i2c.h>
25 #include <linux/acpi.h>
26 #include <linux/usb/phy.h>
27 #include <linux/regmap.h>
29 #define RT9455_MANUFACTURER "Richtek"
30 #define RT9455_MODEL_NAME "RT9455"
31 #define RT9455_DRIVER_NAME "rt9455-charger"
33 #define RT9455_IRQ_NAME "interrupt"
35 #define RT9455_PWR_RDY_DELAY 1 /* 1 second */
36 #define RT9455_MAX_CHARGING_TIME 21600 /* 6 hrs */
37 #define RT9455_BATT_PRESENCE_DELAY 60 /* 60 seconds */
39 #define RT9455_CHARGE_MODE 0x00
40 #define RT9455_BOOST_MODE 0x01
42 #define RT9455_FAULT 0x03
44 #define RT9455_IAICR_100MA 0x00
45 #define RT9455_IAICR_500MA 0x01
46 #define RT9455_IAICR_NO_LIMIT 0x03
48 #define RT9455_CHARGE_DISABLE 0x00
49 #define RT9455_CHARGE_ENABLE 0x01
51 #define RT9455_PWR_FAULT 0x00
52 #define RT9455_PWR_GOOD 0x01
54 #define RT9455_REG_CTRL1 0x00 /* CTRL1 reg address */
55 #define RT9455_REG_CTRL2 0x01 /* CTRL2 reg address */
56 #define RT9455_REG_CTRL3 0x02 /* CTRL3 reg address */
57 #define RT9455_REG_DEV_ID 0x03 /* DEV_ID reg address */
58 #define RT9455_REG_CTRL4 0x04 /* CTRL4 reg address */
59 #define RT9455_REG_CTRL5 0x05 /* CTRL5 reg address */
60 #define RT9455_REG_CTRL6 0x06 /* CTRL6 reg address */
61 #define RT9455_REG_CTRL7 0x07 /* CTRL7 reg address */
62 #define RT9455_REG_IRQ1 0x08 /* IRQ1 reg address */
63 #define RT9455_REG_IRQ2 0x09 /* IRQ2 reg address */
64 #define RT9455_REG_IRQ3 0x0A /* IRQ3 reg address */
65 #define RT9455_REG_MASK1 0x0B /* MASK1 reg address */
66 #define RT9455_REG_MASK2 0x0C /* MASK2 reg address */
67 #define RT9455_REG_MASK3 0x0D /* MASK3 reg address */
70 F_STAT, F_BOOST, F_PWR_RDY, F_OTG_PIN_POLARITY, /* CTRL1 reg fields */
72 F_IAICR, F_TE_SHDN_EN, F_HIGHER_OCP, F_TE, F_IAICR_INT, F_HIZ,
73 F_OPA_MODE, /* CTRL2 reg fields */
75 F_VOREG, F_OTG_PL, F_OTG_EN, /* CTRL3 reg fields */
77 F_VENDOR_ID, F_CHIP_REV, /* DEV_ID reg fields */
79 F_RST, /* CTRL4 reg fields */
81 F_TMR_EN, F_MIVR, F_IPREC, F_IEOC_PERCENTAGE, /* CTRL5 reg fields*/
83 F_IAICR_SEL, F_ICHRG, F_VPREC, /* CTRL6 reg fields */
85 F_BATD_EN, F_CHG_EN, F_VMREG, /* CTRL7 reg fields */
87 F_TSDI, F_VINOVPI, F_BATAB, /* IRQ1 reg fields */
89 F_CHRVPI, F_CHBATOVI, F_CHTERMI, F_CHRCHGI, F_CH32MI, F_CHTREGI,
90 F_CHMIVRI, /* IRQ2 reg fields */
92 F_BSTBUSOVI, F_BSTOLI, F_BSTLOWVI, F_BST32SI, /* IRQ3 reg fields */
94 F_TSDM, F_VINOVPIM, F_BATABM, /* MASK1 reg fields */
96 F_CHRVPIM, F_CHBATOVIM, F_CHTERMIM, F_CHRCHGIM, F_CH32MIM, F_CHTREGIM,
97 F_CHMIVRIM, /* MASK2 reg fields */
99 F_BSTVINOVIM, F_BSTOLIM, F_BSTLOWVIM, F_BST32SIM, /* MASK3 reg fields */
104 static const struct reg_field rt9455_reg_fields[] = {
105 [F_STAT] = REG_FIELD(RT9455_REG_CTRL1, 4, 5),
106 [F_BOOST] = REG_FIELD(RT9455_REG_CTRL1, 3, 3),
107 [F_PWR_RDY] = REG_FIELD(RT9455_REG_CTRL1, 2, 2),
108 [F_OTG_PIN_POLARITY] = REG_FIELD(RT9455_REG_CTRL1, 1, 1),
110 [F_IAICR] = REG_FIELD(RT9455_REG_CTRL2, 6, 7),
111 [F_TE_SHDN_EN] = REG_FIELD(RT9455_REG_CTRL2, 5, 5),
112 [F_HIGHER_OCP] = REG_FIELD(RT9455_REG_CTRL2, 4, 4),
113 [F_TE] = REG_FIELD(RT9455_REG_CTRL2, 3, 3),
114 [F_IAICR_INT] = REG_FIELD(RT9455_REG_CTRL2, 2, 2),
115 [F_HIZ] = REG_FIELD(RT9455_REG_CTRL2, 1, 1),
116 [F_OPA_MODE] = REG_FIELD(RT9455_REG_CTRL2, 0, 0),
118 [F_VOREG] = REG_FIELD(RT9455_REG_CTRL3, 2, 7),
119 [F_OTG_PL] = REG_FIELD(RT9455_REG_CTRL3, 1, 1),
120 [F_OTG_EN] = REG_FIELD(RT9455_REG_CTRL3, 0, 0),
122 [F_VENDOR_ID] = REG_FIELD(RT9455_REG_DEV_ID, 4, 7),
123 [F_CHIP_REV] = REG_FIELD(RT9455_REG_DEV_ID, 0, 3),
125 [F_RST] = REG_FIELD(RT9455_REG_CTRL4, 7, 7),
127 [F_TMR_EN] = REG_FIELD(RT9455_REG_CTRL5, 7, 7),
128 [F_MIVR] = REG_FIELD(RT9455_REG_CTRL5, 4, 5),
129 [F_IPREC] = REG_FIELD(RT9455_REG_CTRL5, 2, 3),
130 [F_IEOC_PERCENTAGE] = REG_FIELD(RT9455_REG_CTRL5, 0, 1),
132 [F_IAICR_SEL] = REG_FIELD(RT9455_REG_CTRL6, 7, 7),
133 [F_ICHRG] = REG_FIELD(RT9455_REG_CTRL6, 4, 6),
134 [F_VPREC] = REG_FIELD(RT9455_REG_CTRL6, 0, 2),
136 [F_BATD_EN] = REG_FIELD(RT9455_REG_CTRL7, 6, 6),
137 [F_CHG_EN] = REG_FIELD(RT9455_REG_CTRL7, 4, 4),
138 [F_VMREG] = REG_FIELD(RT9455_REG_CTRL7, 0, 3),
140 [F_TSDI] = REG_FIELD(RT9455_REG_IRQ1, 7, 7),
141 [F_VINOVPI] = REG_FIELD(RT9455_REG_IRQ1, 6, 6),
142 [F_BATAB] = REG_FIELD(RT9455_REG_IRQ1, 0, 0),
144 [F_CHRVPI] = REG_FIELD(RT9455_REG_IRQ2, 7, 7),
145 [F_CHBATOVI] = REG_FIELD(RT9455_REG_IRQ2, 5, 5),
146 [F_CHTERMI] = REG_FIELD(RT9455_REG_IRQ2, 4, 4),
147 [F_CHRCHGI] = REG_FIELD(RT9455_REG_IRQ2, 3, 3),
148 [F_CH32MI] = REG_FIELD(RT9455_REG_IRQ2, 2, 2),
149 [F_CHTREGI] = REG_FIELD(RT9455_REG_IRQ2, 1, 1),
150 [F_CHMIVRI] = REG_FIELD(RT9455_REG_IRQ2, 0, 0),
152 [F_BSTBUSOVI] = REG_FIELD(RT9455_REG_IRQ3, 7, 7),
153 [F_BSTOLI] = REG_FIELD(RT9455_REG_IRQ3, 6, 6),
154 [F_BSTLOWVI] = REG_FIELD(RT9455_REG_IRQ3, 5, 5),
155 [F_BST32SI] = REG_FIELD(RT9455_REG_IRQ3, 3, 3),
157 [F_TSDM] = REG_FIELD(RT9455_REG_MASK1, 7, 7),
158 [F_VINOVPIM] = REG_FIELD(RT9455_REG_MASK1, 6, 6),
159 [F_BATABM] = REG_FIELD(RT9455_REG_MASK1, 0, 0),
161 [F_CHRVPIM] = REG_FIELD(RT9455_REG_MASK2, 7, 7),
162 [F_CHBATOVIM] = REG_FIELD(RT9455_REG_MASK2, 5, 5),
163 [F_CHTERMIM] = REG_FIELD(RT9455_REG_MASK2, 4, 4),
164 [F_CHRCHGIM] = REG_FIELD(RT9455_REG_MASK2, 3, 3),
165 [F_CH32MIM] = REG_FIELD(RT9455_REG_MASK2, 2, 2),
166 [F_CHTREGIM] = REG_FIELD(RT9455_REG_MASK2, 1, 1),
167 [F_CHMIVRIM] = REG_FIELD(RT9455_REG_MASK2, 0, 0),
169 [F_BSTVINOVIM] = REG_FIELD(RT9455_REG_MASK3, 7, 7),
170 [F_BSTOLIM] = REG_FIELD(RT9455_REG_MASK3, 6, 6),
171 [F_BSTLOWVIM] = REG_FIELD(RT9455_REG_MASK3, 5, 5),
172 [F_BST32SIM] = REG_FIELD(RT9455_REG_MASK3, 3, 3),
175 #define GET_MASK(fid) (BIT(rt9455_reg_fields[fid].msb + 1) - \
176 BIT(rt9455_reg_fields[fid].lsb))
179 * Each array initialised below shows the possible real-world values for a
180 * group of bits belonging to RT9455 registers. The arrays are sorted in
181 * ascending order. The index of each real-world value represents the value
182 * that is encoded in the group of bits belonging to RT9455 registers.
184 /* REG06[6:4] (ICHRG) in uAh */
185 static const int rt9455_ichrg_values[] = {
186 500000, 650000, 800000, 950000, 1100000, 1250000, 1400000, 1550000
190 * When the charger is in charge mode, REG02[7:2] represent battery regulation
193 /* REG02[7:2] (VOREG) in uV */
194 static const int rt9455_voreg_values[] = {
195 3500000, 3520000, 3540000, 3560000, 3580000, 3600000, 3620000, 3640000,
196 3660000, 3680000, 3700000, 3720000, 3740000, 3760000, 3780000, 3800000,
197 3820000, 3840000, 3860000, 3880000, 3900000, 3920000, 3940000, 3960000,
198 3980000, 4000000, 4020000, 4040000, 4060000, 4080000, 4100000, 4120000,
199 4140000, 4160000, 4180000, 4200000, 4220000, 4240000, 4260000, 4280000,
200 4300000, 4330000, 4350000, 4370000, 4390000, 4410000, 4430000, 4450000,
201 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000,
202 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000
205 #if IS_ENABLED(CONFIG_USB_PHY)
207 * When the charger is in boost mode, REG02[7:2] represent boost output
210 /* REG02[7:2] (Boost output voltage) in uV */
211 static const int rt9455_boost_voltage_values[] = {
212 4425000, 4450000, 4475000, 4500000, 4525000, 4550000, 4575000, 4600000,
213 4625000, 4650000, 4675000, 4700000, 4725000, 4750000, 4775000, 4800000,
214 4825000, 4850000, 4875000, 4900000, 4925000, 4950000, 4975000, 5000000,
215 5025000, 5050000, 5075000, 5100000, 5125000, 5150000, 5175000, 5200000,
216 5225000, 5250000, 5275000, 5300000, 5325000, 5350000, 5375000, 5400000,
217 5425000, 5450000, 5475000, 5500000, 5525000, 5550000, 5575000, 5600000,
218 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000,
219 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000,
223 /* REG07[3:0] (VMREG) in uV */
224 static const int rt9455_vmreg_values[] = {
225 4200000, 4220000, 4240000, 4260000, 4280000, 4300000, 4320000, 4340000,
226 4360000, 4380000, 4400000, 4430000, 4450000, 4450000, 4450000, 4450000
229 /* REG05[5:4] (IEOC_PERCENTAGE) */
230 static const int rt9455_ieoc_percentage_values[] = {
234 /* REG05[1:0] (MIVR) in uV */
235 static const int rt9455_mivr_values[] = {
236 4000000, 4250000, 4500000, 5000000
239 /* REG05[1:0] (IAICR) in uA */
240 static const int rt9455_iaicr_values[] = {
241 100000, 500000, 1000000, 2000000
245 struct i2c_client *client;
246 struct regmap *regmap;
247 struct regmap_field *regmap_fields[F_MAX_FIELDS];
248 struct power_supply *charger;
249 #if IS_ENABLED(CONFIG_USB_PHY)
250 struct usb_phy *usb_phy;
251 struct notifier_block nb;
253 struct delayed_work pwr_rdy_work;
254 struct delayed_work max_charging_time_work;
255 struct delayed_work batt_presence_work;
261 * Iterate through each element of the 'tbl' array until an element whose value
262 * is greater than v is found. Return the index of the respective element,
263 * or the index of the last element in the array, if no such element is found.
265 static unsigned int rt9455_find_idx(const int tbl[], int tbl_size, int v)
270 * No need to iterate until the last index in the table because
271 * if no element greater than v is found in the table,
272 * or if only the last element is greater than v,
273 * function returns the index of the last element.
275 for (i = 0; i < tbl_size - 1; i++)
279 return (tbl_size - 1);
282 static int rt9455_get_field_val(struct rt9455_info *info,
283 enum rt9455_fields field,
284 const int tbl[], int tbl_size, int *val)
289 ret = regmap_field_read(info->regmap_fields[field], &v);
293 v = (v >= tbl_size) ? (tbl_size - 1) : v;
299 static int rt9455_set_field_val(struct rt9455_info *info,
300 enum rt9455_fields field,
301 const int tbl[], int tbl_size, int val)
303 unsigned int idx = rt9455_find_idx(tbl, tbl_size, val);
305 return regmap_field_write(info->regmap_fields[field], idx);
308 static int rt9455_register_reset(struct rt9455_info *info)
310 struct device *dev = &info->client->dev;
312 int ret, limit = 100;
314 ret = regmap_field_write(info->regmap_fields[F_RST], 0x01);
316 dev_err(dev, "Failed to set RST bit\n");
321 * To make sure that reset operation has finished, loop until RST bit
325 ret = regmap_field_read(info->regmap_fields[F_RST], &v);
327 dev_err(dev, "Failed to read RST bit\n");
334 usleep_range(10, 100);
343 /* Charger power supply property routines */
344 static enum power_supply_property rt9455_charger_properties[] = {
345 POWER_SUPPLY_PROP_STATUS,
346 POWER_SUPPLY_PROP_HEALTH,
347 POWER_SUPPLY_PROP_PRESENT,
348 POWER_SUPPLY_PROP_ONLINE,
349 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
350 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
351 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
352 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
353 POWER_SUPPLY_PROP_SCOPE,
354 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
355 POWER_SUPPLY_PROP_MODEL_NAME,
356 POWER_SUPPLY_PROP_MANUFACTURER,
359 static char *rt9455_charger_supplied_to[] = {
363 static int rt9455_charger_get_status(struct rt9455_info *info,
364 union power_supply_propval *val)
366 unsigned int v, pwr_rdy;
369 ret = regmap_field_read(info->regmap_fields[F_PWR_RDY],
372 dev_err(&info->client->dev, "Failed to read PWR_RDY bit\n");
377 * If PWR_RDY bit is unset, the battery is discharging. Otherwise,
378 * STAT bits value must be checked.
381 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
385 ret = regmap_field_read(info->regmap_fields[F_STAT], &v);
387 dev_err(&info->client->dev, "Failed to read STAT bits\n");
394 * If PWR_RDY bit is set, but STAT bits value is 0, the charger
395 * may be in one of the following cases:
396 * 1. CHG_EN bit is 0.
397 * 2. CHG_EN bit is 1 but the battery is not connected.
398 * In any of these cases, POWER_SUPPLY_STATUS_NOT_CHARGING is
401 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
404 val->intval = POWER_SUPPLY_STATUS_CHARGING;
407 val->intval = POWER_SUPPLY_STATUS_FULL;
410 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
415 static int rt9455_charger_get_health(struct rt9455_info *info,
416 union power_supply_propval *val)
418 struct device *dev = &info->client->dev;
422 val->intval = POWER_SUPPLY_HEALTH_GOOD;
424 ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &v);
426 dev_err(dev, "Failed to read IRQ1 register\n");
430 if (v & GET_MASK(F_TSDI)) {
431 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
434 if (v & GET_MASK(F_VINOVPI)) {
435 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
438 if (v & GET_MASK(F_BATAB)) {
439 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
443 ret = regmap_read(info->regmap, RT9455_REG_IRQ2, &v);
445 dev_err(dev, "Failed to read IRQ2 register\n");
449 if (v & GET_MASK(F_CHBATOVI)) {
450 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
453 if (v & GET_MASK(F_CH32MI)) {
454 val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
458 ret = regmap_read(info->regmap, RT9455_REG_IRQ3, &v);
460 dev_err(dev, "Failed to read IRQ3 register\n");
464 if (v & GET_MASK(F_BSTBUSOVI)) {
465 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
468 if (v & GET_MASK(F_BSTOLI)) {
469 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
472 if (v & GET_MASK(F_BSTLOWVI)) {
473 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
476 if (v & GET_MASK(F_BST32SI)) {
477 val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
481 ret = regmap_field_read(info->regmap_fields[F_STAT], &v);
483 dev_err(dev, "Failed to read STAT bits\n");
487 if (v == RT9455_FAULT) {
488 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
495 static int rt9455_charger_get_battery_presence(struct rt9455_info *info,
496 union power_supply_propval *val)
501 ret = regmap_field_read(info->regmap_fields[F_BATAB], &v);
503 dev_err(&info->client->dev, "Failed to read BATAB bit\n");
508 * Since BATAB is 1 when battery is NOT present and 0 otherwise,
509 * !BATAB is returned.
516 static int rt9455_charger_get_online(struct rt9455_info *info,
517 union power_supply_propval *val)
522 ret = regmap_field_read(info->regmap_fields[F_PWR_RDY], &v);
524 dev_err(&info->client->dev, "Failed to read PWR_RDY bit\n");
528 val->intval = (int)v;
533 static int rt9455_charger_get_current(struct rt9455_info *info,
534 union power_supply_propval *val)
539 ret = rt9455_get_field_val(info, F_ICHRG,
541 ARRAY_SIZE(rt9455_ichrg_values),
544 dev_err(&info->client->dev, "Failed to read ICHRG value\n");
553 static int rt9455_charger_get_current_max(struct rt9455_info *info,
554 union power_supply_propval *val)
556 int idx = ARRAY_SIZE(rt9455_ichrg_values) - 1;
558 val->intval = rt9455_ichrg_values[idx];
563 static int rt9455_charger_get_voltage(struct rt9455_info *info,
564 union power_supply_propval *val)
569 ret = rt9455_get_field_val(info, F_VOREG,
571 ARRAY_SIZE(rt9455_voreg_values),
574 dev_err(&info->client->dev, "Failed to read VOREG value\n");
578 val->intval = voltage;
583 static int rt9455_charger_get_voltage_max(struct rt9455_info *info,
584 union power_supply_propval *val)
586 int idx = ARRAY_SIZE(rt9455_vmreg_values) - 1;
588 val->intval = rt9455_vmreg_values[idx];
593 static int rt9455_charger_get_term_current(struct rt9455_info *info,
594 union power_supply_propval *val)
596 struct device *dev = &info->client->dev;
597 int ichrg, ieoc_percentage, ret;
599 ret = rt9455_get_field_val(info, F_ICHRG,
601 ARRAY_SIZE(rt9455_ichrg_values),
604 dev_err(dev, "Failed to read ICHRG value\n");
608 ret = rt9455_get_field_val(info, F_IEOC_PERCENTAGE,
609 rt9455_ieoc_percentage_values,
610 ARRAY_SIZE(rt9455_ieoc_percentage_values),
613 dev_err(dev, "Failed to read IEOC value\n");
617 val->intval = ichrg * ieoc_percentage / 100;
622 static int rt9455_charger_get_property(struct power_supply *psy,
623 enum power_supply_property psp,
624 union power_supply_propval *val)
626 struct rt9455_info *info = power_supply_get_drvdata(psy);
629 case POWER_SUPPLY_PROP_STATUS:
630 return rt9455_charger_get_status(info, val);
631 case POWER_SUPPLY_PROP_HEALTH:
632 return rt9455_charger_get_health(info, val);
633 case POWER_SUPPLY_PROP_PRESENT:
634 return rt9455_charger_get_battery_presence(info, val);
635 case POWER_SUPPLY_PROP_ONLINE:
636 return rt9455_charger_get_online(info, val);
637 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
638 return rt9455_charger_get_current(info, val);
639 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
640 return rt9455_charger_get_current_max(info, val);
641 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
642 return rt9455_charger_get_voltage(info, val);
643 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
644 return rt9455_charger_get_voltage_max(info, val);
645 case POWER_SUPPLY_PROP_SCOPE:
646 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
648 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
649 return rt9455_charger_get_term_current(info, val);
650 case POWER_SUPPLY_PROP_MODEL_NAME:
651 val->strval = RT9455_MODEL_NAME;
653 case POWER_SUPPLY_PROP_MANUFACTURER:
654 val->strval = RT9455_MANUFACTURER;
661 static int rt9455_hw_init(struct rt9455_info *info, u32 ichrg,
665 struct device *dev = &info->client->dev;
668 ret = rt9455_register_reset(info);
670 dev_err(dev, "Power On Reset failed\n");
674 /* Set TE bit in order to enable end of charge detection */
675 ret = regmap_field_write(info->regmap_fields[F_TE], 1);
677 dev_err(dev, "Failed to set TE bit\n");
681 /* Set TE_SHDN_EN bit in order to enable end of charge detection */
682 ret = regmap_field_write(info->regmap_fields[F_TE_SHDN_EN], 1);
684 dev_err(dev, "Failed to set TE_SHDN_EN bit\n");
689 * Set BATD_EN bit in order to enable battery detection
690 * when charging is done
692 ret = regmap_field_write(info->regmap_fields[F_BATD_EN], 1);
694 dev_err(dev, "Failed to set BATD_EN bit\n");
699 * Disable Safety Timer. In charge mode, this timer terminates charging
700 * if no read or write via I2C is done within 32 minutes. This timer
701 * avoids overcharging the baterry when the OS is not loaded and the
702 * charger is connected to a power source.
703 * In boost mode, this timer triggers BST32SI interrupt if no read or
704 * write via I2C is done within 32 seconds.
705 * When the OS is loaded and the charger driver is inserted, it is used
706 * delayed_work, named max_charging_time_work, to avoid overcharging
709 ret = regmap_field_write(info->regmap_fields[F_TMR_EN], 0x00);
711 dev_err(dev, "Failed to disable Safety Timer\n");
715 /* Set ICHRG to value retrieved from device-specific data */
716 ret = rt9455_set_field_val(info, F_ICHRG,
718 ARRAY_SIZE(rt9455_ichrg_values), ichrg);
720 dev_err(dev, "Failed to set ICHRG value\n");
724 /* Set IEOC Percentage to value retrieved from device-specific data */
725 ret = rt9455_set_field_val(info, F_IEOC_PERCENTAGE,
726 rt9455_ieoc_percentage_values,
727 ARRAY_SIZE(rt9455_ieoc_percentage_values),
730 dev_err(dev, "Failed to set IEOC Percentage value\n");
734 /* Set VOREG to value retrieved from device-specific data */
735 ret = rt9455_set_field_val(info, F_VOREG,
737 ARRAY_SIZE(rt9455_voreg_values),
740 dev_err(dev, "Failed to set VOREG value\n");
744 /* Set VMREG value to maximum (4.45V). */
745 idx = ARRAY_SIZE(rt9455_vmreg_values) - 1;
746 ret = rt9455_set_field_val(info, F_VMREG,
748 ARRAY_SIZE(rt9455_vmreg_values),
749 rt9455_vmreg_values[idx]);
751 dev_err(dev, "Failed to set VMREG value\n");
756 * Set MIVR to value retrieved from device-specific data.
757 * If no value is specified, default value for MIVR is 4.5V.
762 ret = rt9455_set_field_val(info, F_MIVR,
764 ARRAY_SIZE(rt9455_mivr_values), mivr);
766 dev_err(dev, "Failed to set MIVR value\n");
771 * Set IAICR to value retrieved from device-specific data.
772 * If no value is specified, default value for IAICR is 500 mA.
777 ret = rt9455_set_field_val(info, F_IAICR,
779 ARRAY_SIZE(rt9455_iaicr_values), iaicr);
781 dev_err(dev, "Failed to set IAICR value\n");
786 * Set IAICR_INT bit so that IAICR value is determined by IAICR bits
787 * and not by OTG pin.
789 ret = regmap_field_write(info->regmap_fields[F_IAICR_INT], 0x01);
791 dev_err(dev, "Failed to set IAICR_INT bit\n");
796 * Disable CHMIVRI interrupt. Because the driver sets MIVR value,
797 * CHMIVRI is triggered, but there is no action to be taken by the
798 * driver when CHMIVRI is triggered.
800 ret = regmap_field_write(info->regmap_fields[F_CHMIVRIM], 0x01);
802 dev_err(dev, "Failed to mask CHMIVRI interrupt\n");
809 #if IS_ENABLED(CONFIG_USB_PHY)
811 * Before setting the charger into boost mode, boost output voltage is
812 * set. This is needed because boost output voltage may differ from battery
813 * regulation voltage. F_VOREG bits represent either battery regulation voltage
814 * or boost output voltage, depending on the mode the charger is. Both battery
815 * regulation voltage and boost output voltage are read from DT/ACPI during
818 static int rt9455_set_boost_voltage_before_boost_mode(struct rt9455_info *info)
820 struct device *dev = &info->client->dev;
823 ret = rt9455_set_field_val(info, F_VOREG,
824 rt9455_boost_voltage_values,
825 ARRAY_SIZE(rt9455_boost_voltage_values),
826 info->boost_voltage);
828 dev_err(dev, "Failed to set boost output voltage value\n");
837 * Before setting the charger into charge mode, battery regulation voltage is
838 * set. This is needed because boost output voltage may differ from battery
839 * regulation voltage. F_VOREG bits represent either battery regulation voltage
840 * or boost output voltage, depending on the mode the charger is. Both battery
841 * regulation voltage and boost output voltage are read from DT/ACPI during
844 static int rt9455_set_voreg_before_charge_mode(struct rt9455_info *info)
846 struct device *dev = &info->client->dev;
849 ret = rt9455_set_field_val(info, F_VOREG,
851 ARRAY_SIZE(rt9455_voreg_values),
854 dev_err(dev, "Failed to set VOREG value\n");
861 static int rt9455_irq_handler_check_irq1_register(struct rt9455_info *info,
862 bool *_is_battery_absent,
863 bool *_alert_userspace)
865 unsigned int irq1, mask1, mask2;
866 struct device *dev = &info->client->dev;
867 bool is_battery_absent = false;
868 bool alert_userspace = false;
871 ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &irq1);
873 dev_err(dev, "Failed to read IRQ1 register\n");
877 ret = regmap_read(info->regmap, RT9455_REG_MASK1, &mask1);
879 dev_err(dev, "Failed to read MASK1 register\n");
883 if (irq1 & GET_MASK(F_TSDI)) {
884 dev_err(dev, "Thermal shutdown fault occurred\n");
885 alert_userspace = true;
888 if (irq1 & GET_MASK(F_VINOVPI)) {
889 dev_err(dev, "Overvoltage input occurred\n");
890 alert_userspace = true;
893 if (irq1 & GET_MASK(F_BATAB)) {
894 dev_err(dev, "Battery absence occurred\n");
895 is_battery_absent = true;
896 alert_userspace = true;
898 if ((mask1 & GET_MASK(F_BATABM)) == 0) {
899 ret = regmap_field_write(info->regmap_fields[F_BATABM],
902 dev_err(dev, "Failed to mask BATAB interrupt\n");
907 ret = regmap_read(info->regmap, RT9455_REG_MASK2, &mask2);
909 dev_err(dev, "Failed to read MASK2 register\n");
913 if (mask2 & GET_MASK(F_CHTERMIM)) {
914 ret = regmap_field_write(
915 info->regmap_fields[F_CHTERMIM], 0x00);
917 dev_err(dev, "Failed to unmask CHTERMI interrupt\n");
922 if (mask2 & GET_MASK(F_CHRCHGIM)) {
923 ret = regmap_field_write(
924 info->regmap_fields[F_CHRCHGIM], 0x00);
926 dev_err(dev, "Failed to unmask CHRCHGI interrupt\n");
932 * When the battery is absent, max_charging_time_work is
933 * cancelled, since no charging is done.
935 cancel_delayed_work_sync(&info->max_charging_time_work);
937 * Since no interrupt is triggered when the battery is
938 * reconnected, max_charging_time_work is not rescheduled.
939 * Therefore, batt_presence_work is scheduled to check whether
940 * the battery is still absent or not.
942 queue_delayed_work(system_power_efficient_wq,
943 &info->batt_presence_work,
944 RT9455_BATT_PRESENCE_DELAY * HZ);
947 *_is_battery_absent = is_battery_absent;
950 *_alert_userspace = alert_userspace;
955 static int rt9455_irq_handler_check_irq2_register(struct rt9455_info *info,
956 bool is_battery_absent,
957 bool *_alert_userspace)
959 unsigned int irq2, mask2;
960 struct device *dev = &info->client->dev;
961 bool alert_userspace = false;
964 ret = regmap_read(info->regmap, RT9455_REG_IRQ2, &irq2);
966 dev_err(dev, "Failed to read IRQ2 register\n");
970 ret = regmap_read(info->regmap, RT9455_REG_MASK2, &mask2);
972 dev_err(dev, "Failed to read MASK2 register\n");
976 if (irq2 & GET_MASK(F_CHRVPI)) {
977 dev_dbg(dev, "Charger fault occurred\n");
979 * CHRVPI bit is set in 2 cases:
980 * 1. when the power source is connected to the charger.
981 * 2. when the power source is disconnected from the charger.
982 * To identify the case, PWR_RDY bit is checked. Because
983 * PWR_RDY bit is set / cleared after CHRVPI interrupt is
984 * triggered, it is used delayed_work to later read PWR_RDY bit.
985 * Also, do not set to true alert_userspace, because there is no
986 * need to notify userspace when CHRVPI interrupt has occurred.
987 * Userspace will be notified after PWR_RDY bit is read.
989 queue_delayed_work(system_power_efficient_wq,
991 RT9455_PWR_RDY_DELAY * HZ);
993 if (irq2 & GET_MASK(F_CHBATOVI)) {
994 dev_err(dev, "Battery OVP occurred\n");
995 alert_userspace = true;
997 if (irq2 & GET_MASK(F_CHTERMI)) {
998 dev_dbg(dev, "Charge terminated\n");
999 if (!is_battery_absent) {
1000 if ((mask2 & GET_MASK(F_CHTERMIM)) == 0) {
1001 ret = regmap_field_write(
1002 info->regmap_fields[F_CHTERMIM], 0x01);
1004 dev_err(dev, "Failed to mask CHTERMI interrupt\n");
1008 * Update MASK2 value, since CHTERMIM bit is
1011 mask2 = mask2 | GET_MASK(F_CHTERMIM);
1013 cancel_delayed_work_sync(&info->max_charging_time_work);
1014 alert_userspace = true;
1017 if (irq2 & GET_MASK(F_CHRCHGI)) {
1018 dev_dbg(dev, "Recharge request\n");
1019 ret = regmap_field_write(info->regmap_fields[F_CHG_EN],
1020 RT9455_CHARGE_ENABLE);
1022 dev_err(dev, "Failed to enable charging\n");
1025 if (mask2 & GET_MASK(F_CHTERMIM)) {
1026 ret = regmap_field_write(
1027 info->regmap_fields[F_CHTERMIM], 0x00);
1029 dev_err(dev, "Failed to unmask CHTERMI interrupt\n");
1032 /* Update MASK2 value, since CHTERMIM bit is cleared. */
1033 mask2 = mask2 & ~GET_MASK(F_CHTERMIM);
1035 if (!is_battery_absent) {
1037 * No need to check whether the charger is connected to
1038 * power source when CHRCHGI is received, since CHRCHGI
1039 * is not triggered if the charger is not connected to
1042 queue_delayed_work(system_power_efficient_wq,
1043 &info->max_charging_time_work,
1044 RT9455_MAX_CHARGING_TIME * HZ);
1045 alert_userspace = true;
1048 if (irq2 & GET_MASK(F_CH32MI)) {
1049 dev_err(dev, "Charger fault. 32 mins timeout occurred\n");
1050 alert_userspace = true;
1052 if (irq2 & GET_MASK(F_CHTREGI)) {
1054 "Charger warning. Thermal regulation loop active\n");
1055 alert_userspace = true;
1057 if (irq2 & GET_MASK(F_CHMIVRI)) {
1059 "Charger warning. Input voltage MIVR loop active\n");
1062 if (alert_userspace)
1063 *_alert_userspace = alert_userspace;
1068 static int rt9455_irq_handler_check_irq3_register(struct rt9455_info *info,
1069 bool *_alert_userspace)
1071 unsigned int irq3, mask3;
1072 struct device *dev = &info->client->dev;
1073 bool alert_userspace = false;
1076 ret = regmap_read(info->regmap, RT9455_REG_IRQ3, &irq3);
1078 dev_err(dev, "Failed to read IRQ3 register\n");
1082 ret = regmap_read(info->regmap, RT9455_REG_MASK3, &mask3);
1084 dev_err(dev, "Failed to read MASK3 register\n");
1088 if (irq3 & GET_MASK(F_BSTBUSOVI)) {
1089 dev_err(dev, "Boost fault. Overvoltage input occurred\n");
1090 alert_userspace = true;
1092 if (irq3 & GET_MASK(F_BSTOLI)) {
1093 dev_err(dev, "Boost fault. Overload\n");
1094 alert_userspace = true;
1096 if (irq3 & GET_MASK(F_BSTLOWVI)) {
1097 dev_err(dev, "Boost fault. Battery voltage too low\n");
1098 alert_userspace = true;
1100 if (irq3 & GET_MASK(F_BST32SI)) {
1101 dev_err(dev, "Boost fault. 32 seconds timeout occurred.\n");
1102 alert_userspace = true;
1105 if (alert_userspace) {
1106 dev_info(dev, "Boost fault occurred, therefore the charger goes into charge mode\n");
1107 ret = rt9455_set_voreg_before_charge_mode(info);
1109 dev_err(dev, "Failed to set VOREG before entering charge mode\n");
1112 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1113 RT9455_CHARGE_MODE);
1115 dev_err(dev, "Failed to set charger in charge mode\n");
1118 *_alert_userspace = alert_userspace;
1124 static irqreturn_t rt9455_irq_handler_thread(int irq, void *data)
1126 struct rt9455_info *info = data;
1128 bool alert_userspace = false;
1129 bool is_battery_absent = false;
1130 unsigned int status;
1136 dev = &info->client->dev;
1138 if (irq != info->client->irq) {
1139 dev_err(dev, "Interrupt is not for RT9455 charger\n");
1143 ret = regmap_field_read(info->regmap_fields[F_STAT], &status);
1145 dev_err(dev, "Failed to read STAT bits\n");
1148 dev_dbg(dev, "Charger status is %d\n", status);
1151 * Each function that processes an IRQ register receives as output
1152 * parameter alert_userspace pointer. alert_userspace is set to true
1153 * in such a function only if an interrupt has occurred in the
1154 * respective interrupt register. This way, it is avoided the following
1155 * case: interrupt occurs only in IRQ1 register,
1156 * rt9455_irq_handler_check_irq1_register() function sets to true
1157 * alert_userspace, but rt9455_irq_handler_check_irq2_register()
1158 * and rt9455_irq_handler_check_irq3_register() functions set to false
1159 * alert_userspace and power_supply_changed() is never called.
1161 ret = rt9455_irq_handler_check_irq1_register(info, &is_battery_absent,
1164 dev_err(dev, "Failed to handle IRQ1 register\n");
1168 ret = rt9455_irq_handler_check_irq2_register(info, is_battery_absent,
1171 dev_err(dev, "Failed to handle IRQ2 register\n");
1175 ret = rt9455_irq_handler_check_irq3_register(info, &alert_userspace);
1177 dev_err(dev, "Failed to handle IRQ3 register\n");
1181 if (alert_userspace) {
1183 * Sometimes, an interrupt occurs while rt9455_probe() function
1184 * is executing and power_supply_register() is not yet called.
1185 * Do not call power_supply_changed() in this case.
1188 power_supply_changed(info->charger);
1194 static int rt9455_discover_charger(struct rt9455_info *info, u32 *ichrg,
1195 u32 *ieoc_percentage,
1196 u32 *mivr, u32 *iaicr)
1198 struct device *dev = &info->client->dev;
1201 if (!dev->of_node && !ACPI_HANDLE(dev)) {
1202 dev_err(dev, "No support for either device tree or ACPI\n");
1206 * ICHRG, IEOC_PERCENTAGE, VOREG and boost output voltage are mandatory
1209 ret = device_property_read_u32(dev, "richtek,output-charge-current",
1212 dev_err(dev, "Error: missing \"output-charge-current\" property\n");
1216 ret = device_property_read_u32(dev, "richtek,end-of-charge-percentage",
1219 dev_err(dev, "Error: missing \"end-of-charge-percentage\" property\n");
1223 ret = device_property_read_u32(dev,
1224 "richtek,battery-regulation-voltage",
1227 dev_err(dev, "Error: missing \"battery-regulation-voltage\" property\n");
1231 ret = device_property_read_u32(dev, "richtek,boost-output-voltage",
1232 &info->boost_voltage);
1234 dev_err(dev, "Error: missing \"boost-output-voltage\" property\n");
1239 * MIVR and IAICR are optional parameters. Do not return error if one of
1240 * them is not present in ACPI table or device tree specification.
1242 device_property_read_u32(dev, "richtek,min-input-voltage-regulation",
1244 device_property_read_u32(dev, "richtek,avg-input-current-regulation",
1250 #if IS_ENABLED(CONFIG_USB_PHY)
1251 static int rt9455_usb_event_none(struct rt9455_info *info,
1252 u8 opa_mode, u8 iaicr)
1254 struct device *dev = &info->client->dev;
1257 if (opa_mode == RT9455_BOOST_MODE) {
1258 ret = rt9455_set_voreg_before_charge_mode(info);
1260 dev_err(dev, "Failed to set VOREG before entering charge mode\n");
1264 * If the charger is in boost mode, and it has received
1265 * USB_EVENT_NONE, this means the consumer device powered by the
1266 * charger is not connected anymore.
1267 * In this case, the charger goes into charge mode.
1269 dev_dbg(dev, "USB_EVENT_NONE received, therefore the charger goes into charge mode\n");
1270 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1271 RT9455_CHARGE_MODE);
1273 dev_err(dev, "Failed to set charger in charge mode\n");
1278 dev_dbg(dev, "USB_EVENT_NONE received, therefore IAICR is set to its minimum value\n");
1279 if (iaicr != RT9455_IAICR_100MA) {
1280 ret = regmap_field_write(info->regmap_fields[F_IAICR],
1281 RT9455_IAICR_100MA);
1283 dev_err(dev, "Failed to set IAICR value\n");
1291 static int rt9455_usb_event_vbus(struct rt9455_info *info,
1292 u8 opa_mode, u8 iaicr)
1294 struct device *dev = &info->client->dev;
1297 if (opa_mode == RT9455_BOOST_MODE) {
1298 ret = rt9455_set_voreg_before_charge_mode(info);
1300 dev_err(dev, "Failed to set VOREG before entering charge mode\n");
1304 * If the charger is in boost mode, and it has received
1305 * USB_EVENT_VBUS, this means the consumer device powered by the
1306 * charger is not connected anymore.
1307 * In this case, the charger goes into charge mode.
1309 dev_dbg(dev, "USB_EVENT_VBUS received, therefore the charger goes into charge mode\n");
1310 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1311 RT9455_CHARGE_MODE);
1313 dev_err(dev, "Failed to set charger in charge mode\n");
1318 dev_dbg(dev, "USB_EVENT_VBUS received, therefore IAICR is set to 500 mA\n");
1319 if (iaicr != RT9455_IAICR_500MA) {
1320 ret = regmap_field_write(info->regmap_fields[F_IAICR],
1321 RT9455_IAICR_500MA);
1323 dev_err(dev, "Failed to set IAICR value\n");
1331 static int rt9455_usb_event_id(struct rt9455_info *info,
1332 u8 opa_mode, u8 iaicr)
1334 struct device *dev = &info->client->dev;
1337 if (opa_mode == RT9455_CHARGE_MODE) {
1338 ret = rt9455_set_boost_voltage_before_boost_mode(info);
1340 dev_err(dev, "Failed to set boost output voltage before entering boost mode\n");
1344 * If the charger is in charge mode, and it has received
1345 * USB_EVENT_ID, this means a consumer device is connected and
1346 * it should be powered by the charger.
1347 * In this case, the charger goes into boost mode.
1349 dev_dbg(dev, "USB_EVENT_ID received, therefore the charger goes into boost mode\n");
1350 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1353 dev_err(dev, "Failed to set charger in boost mode\n");
1358 dev_dbg(dev, "USB_EVENT_ID received, therefore IAICR is set to its minimum value\n");
1359 if (iaicr != RT9455_IAICR_100MA) {
1360 ret = regmap_field_write(info->regmap_fields[F_IAICR],
1361 RT9455_IAICR_100MA);
1363 dev_err(dev, "Failed to set IAICR value\n");
1371 static int rt9455_usb_event_charger(struct rt9455_info *info,
1372 u8 opa_mode, u8 iaicr)
1374 struct device *dev = &info->client->dev;
1377 if (opa_mode == RT9455_BOOST_MODE) {
1378 ret = rt9455_set_voreg_before_charge_mode(info);
1380 dev_err(dev, "Failed to set VOREG before entering charge mode\n");
1384 * If the charger is in boost mode, and it has received
1385 * USB_EVENT_CHARGER, this means the consumer device powered by
1386 * the charger is not connected anymore.
1387 * In this case, the charger goes into charge mode.
1389 dev_dbg(dev, "USB_EVENT_CHARGER received, therefore the charger goes into charge mode\n");
1390 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1391 RT9455_CHARGE_MODE);
1393 dev_err(dev, "Failed to set charger in charge mode\n");
1398 dev_dbg(dev, "USB_EVENT_CHARGER received, therefore IAICR is set to no current limit\n");
1399 if (iaicr != RT9455_IAICR_NO_LIMIT) {
1400 ret = regmap_field_write(info->regmap_fields[F_IAICR],
1401 RT9455_IAICR_NO_LIMIT);
1403 dev_err(dev, "Failed to set IAICR value\n");
1411 static int rt9455_usb_event(struct notifier_block *nb,
1412 unsigned long event, void *power)
1414 struct rt9455_info *info = container_of(nb, struct rt9455_info, nb);
1415 struct device *dev = &info->client->dev;
1416 unsigned int opa_mode, iaicr;
1420 * Determine whether the charger is in charge mode
1423 ret = regmap_field_read(info->regmap_fields[F_OPA_MODE],
1426 dev_err(dev, "Failed to read OPA_MODE value\n");
1430 ret = regmap_field_read(info->regmap_fields[F_IAICR],
1433 dev_err(dev, "Failed to read IAICR value\n");
1437 dev_dbg(dev, "Received USB event %lu\n", event);
1439 case USB_EVENT_NONE:
1440 return rt9455_usb_event_none(info, opa_mode, iaicr);
1441 case USB_EVENT_VBUS:
1442 return rt9455_usb_event_vbus(info, opa_mode, iaicr);
1444 return rt9455_usb_event_id(info, opa_mode, iaicr);
1445 case USB_EVENT_CHARGER:
1446 return rt9455_usb_event_charger(info, opa_mode, iaicr);
1448 dev_err(dev, "Unknown USB event\n");
1454 static void rt9455_pwr_rdy_work_callback(struct work_struct *work)
1456 struct rt9455_info *info = container_of(work, struct rt9455_info,
1458 struct device *dev = &info->client->dev;
1459 unsigned int pwr_rdy;
1462 ret = regmap_field_read(info->regmap_fields[F_PWR_RDY], &pwr_rdy);
1464 dev_err(dev, "Failed to read PWR_RDY bit\n");
1468 case RT9455_PWR_FAULT:
1469 dev_dbg(dev, "Charger disconnected from power source\n");
1470 cancel_delayed_work_sync(&info->max_charging_time_work);
1472 case RT9455_PWR_GOOD:
1473 dev_dbg(dev, "Charger connected to power source\n");
1474 ret = regmap_field_write(info->regmap_fields[F_CHG_EN],
1475 RT9455_CHARGE_ENABLE);
1477 dev_err(dev, "Failed to enable charging\n");
1480 queue_delayed_work(system_power_efficient_wq,
1481 &info->max_charging_time_work,
1482 RT9455_MAX_CHARGING_TIME * HZ);
1486 * Notify userspace that the charger has been either connected to or
1487 * disconnected from the power source.
1489 power_supply_changed(info->charger);
1492 static void rt9455_max_charging_time_work_callback(struct work_struct *work)
1494 struct rt9455_info *info = container_of(work, struct rt9455_info,
1495 max_charging_time_work.work);
1496 struct device *dev = &info->client->dev;
1499 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");
1500 ret = regmap_field_write(info->regmap_fields[F_CHG_EN],
1501 RT9455_CHARGE_DISABLE);
1503 dev_err(dev, "Failed to disable charging\n");
1506 static void rt9455_batt_presence_work_callback(struct work_struct *work)
1508 struct rt9455_info *info = container_of(work, struct rt9455_info,
1509 batt_presence_work.work);
1510 struct device *dev = &info->client->dev;
1511 unsigned int irq1, mask1;
1514 ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &irq1);
1516 dev_err(dev, "Failed to read IRQ1 register\n");
1521 * If the battery is still absent, batt_presence_work is rescheduled.
1522 * Otherwise, max_charging_time is scheduled.
1524 if (irq1 & GET_MASK(F_BATAB)) {
1525 queue_delayed_work(system_power_efficient_wq,
1526 &info->batt_presence_work,
1527 RT9455_BATT_PRESENCE_DELAY * HZ);
1529 queue_delayed_work(system_power_efficient_wq,
1530 &info->max_charging_time_work,
1531 RT9455_MAX_CHARGING_TIME * HZ);
1533 ret = regmap_read(info->regmap, RT9455_REG_MASK1, &mask1);
1535 dev_err(dev, "Failed to read MASK1 register\n");
1539 if (mask1 & GET_MASK(F_BATABM)) {
1540 ret = regmap_field_write(info->regmap_fields[F_BATABM],
1543 dev_err(dev, "Failed to unmask BATAB interrupt\n");
1546 * Notify userspace that the battery is now connected to the
1549 power_supply_changed(info->charger);
1553 static const struct power_supply_desc rt9455_charger_desc = {
1554 .name = RT9455_DRIVER_NAME,
1555 .type = POWER_SUPPLY_TYPE_USB,
1556 .properties = rt9455_charger_properties,
1557 .num_properties = ARRAY_SIZE(rt9455_charger_properties),
1558 .get_property = rt9455_charger_get_property,
1561 static bool rt9455_is_writeable_reg(struct device *dev, unsigned int reg)
1564 case RT9455_REG_DEV_ID:
1565 case RT9455_REG_IRQ1:
1566 case RT9455_REG_IRQ2:
1567 case RT9455_REG_IRQ3:
1574 static bool rt9455_is_volatile_reg(struct device *dev, unsigned int reg)
1577 case RT9455_REG_DEV_ID:
1578 case RT9455_REG_CTRL5:
1579 case RT9455_REG_CTRL6:
1586 static const struct regmap_config rt9455_regmap_config = {
1589 .writeable_reg = rt9455_is_writeable_reg,
1590 .volatile_reg = rt9455_is_volatile_reg,
1591 .max_register = RT9455_REG_MASK3,
1592 .cache_type = REGCACHE_RBTREE,
1595 static int rt9455_probe(struct i2c_client *client,
1596 const struct i2c_device_id *id)
1598 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
1599 struct device *dev = &client->dev;
1600 struct rt9455_info *info;
1601 struct power_supply_config rt9455_charger_config = {};
1603 * Mandatory device-specific data values. Also, VOREG and boost output
1604 * voltage are mandatory values, but they are stored in rt9455_info
1607 u32 ichrg, ieoc_percentage;
1608 /* Optional device-specific data values. */
1609 u32 mivr = -1, iaicr = -1;
1612 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1613 dev_err(dev, "No support for SMBUS_BYTE_DATA\n");
1616 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
1620 info->client = client;
1621 i2c_set_clientdata(client, info);
1623 info->regmap = devm_regmap_init_i2c(client,
1624 &rt9455_regmap_config);
1625 if (IS_ERR(info->regmap)) {
1626 dev_err(dev, "Failed to initialize register map\n");
1630 for (i = 0; i < F_MAX_FIELDS; i++) {
1631 info->regmap_fields[i] =
1632 devm_regmap_field_alloc(dev, info->regmap,
1633 rt9455_reg_fields[i]);
1634 if (IS_ERR(info->regmap_fields[i])) {
1636 "Failed to allocate regmap field = %d\n", i);
1637 return PTR_ERR(info->regmap_fields[i]);
1641 ret = rt9455_discover_charger(info, &ichrg, &ieoc_percentage,
1644 dev_err(dev, "Failed to discover charger\n");
1648 #if IS_ENABLED(CONFIG_USB_PHY)
1649 info->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1650 if (IS_ERR(info->usb_phy)) {
1651 dev_err(dev, "Failed to get USB transceiver\n");
1653 info->nb.notifier_call = rt9455_usb_event;
1654 ret = usb_register_notifier(info->usb_phy, &info->nb);
1656 dev_err(dev, "Failed to register USB notifier\n");
1658 * If usb_register_notifier() fails, set notifier_call
1659 * to NULL, to avoid calling usb_unregister_notifier().
1661 info->nb.notifier_call = NULL;
1666 INIT_DEFERRABLE_WORK(&info->pwr_rdy_work, rt9455_pwr_rdy_work_callback);
1667 INIT_DEFERRABLE_WORK(&info->max_charging_time_work,
1668 rt9455_max_charging_time_work_callback);
1669 INIT_DEFERRABLE_WORK(&info->batt_presence_work,
1670 rt9455_batt_presence_work_callback);
1672 rt9455_charger_config.of_node = dev->of_node;
1673 rt9455_charger_config.drv_data = info;
1674 rt9455_charger_config.supplied_to = rt9455_charger_supplied_to;
1675 rt9455_charger_config.num_supplicants =
1676 ARRAY_SIZE(rt9455_charger_supplied_to);
1677 ret = devm_request_threaded_irq(dev, client->irq, NULL,
1678 rt9455_irq_handler_thread,
1679 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1680 RT9455_DRIVER_NAME, info);
1682 dev_err(dev, "Failed to register IRQ handler\n");
1683 goto put_usb_notifier;
1686 ret = rt9455_hw_init(info, ichrg, ieoc_percentage, mivr, iaicr);
1688 dev_err(dev, "Failed to set charger to its default values\n");
1689 goto put_usb_notifier;
1692 info->charger = devm_power_supply_register(dev, &rt9455_charger_desc,
1693 &rt9455_charger_config);
1694 if (IS_ERR(info->charger)) {
1695 dev_err(dev, "Failed to register charger\n");
1696 ret = PTR_ERR(info->charger);
1697 goto put_usb_notifier;
1703 #if IS_ENABLED(CONFIG_USB_PHY)
1704 if (info->nb.notifier_call) {
1705 usb_unregister_notifier(info->usb_phy, &info->nb);
1706 info->nb.notifier_call = NULL;
1712 static int rt9455_remove(struct i2c_client *client)
1715 struct rt9455_info *info = i2c_get_clientdata(client);
1717 ret = rt9455_register_reset(info);
1719 dev_err(&info->client->dev, "Failed to set charger to its default values\n");
1721 #if IS_ENABLED(CONFIG_USB_PHY)
1722 if (info->nb.notifier_call)
1723 usb_unregister_notifier(info->usb_phy, &info->nb);
1726 cancel_delayed_work_sync(&info->pwr_rdy_work);
1727 cancel_delayed_work_sync(&info->max_charging_time_work);
1728 cancel_delayed_work_sync(&info->batt_presence_work);
1733 static const struct i2c_device_id rt9455_i2c_id_table[] = {
1734 { RT9455_DRIVER_NAME, 0 },
1737 MODULE_DEVICE_TABLE(i2c, rt9455_i2c_id_table);
1739 static const struct of_device_id rt9455_of_match[] = {
1740 { .compatible = "richtek,rt9455", },
1743 MODULE_DEVICE_TABLE(of, rt9455_of_match);
1745 static const struct acpi_device_id rt9455_i2c_acpi_match[] = {
1749 MODULE_DEVICE_TABLE(acpi, rt9455_i2c_acpi_match);
1751 static struct i2c_driver rt9455_driver = {
1752 .probe = rt9455_probe,
1753 .remove = rt9455_remove,
1754 .id_table = rt9455_i2c_id_table,
1756 .name = RT9455_DRIVER_NAME,
1757 .of_match_table = of_match_ptr(rt9455_of_match),
1758 .acpi_match_table = ACPI_PTR(rt9455_i2c_acpi_match),
1761 module_i2c_driver(rt9455_driver);
1763 MODULE_LICENSE("GPL");
1764 MODULE_AUTHOR("Anda-Maria Nicolae <anda-maria.nicolae@intel.com>");
1765 MODULE_DESCRIPTION("Richtek RT9455 Charger Driver");