treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / power / supply / rt9455_charger.c
blob29161ae9024562b18da8ff82f7f92b8bec4f92f9
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Driver for Richtek RT9455WSC battery charger.
5 * Copyright (C) 2015 Intel Corporation
6 */
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 */
60 enum rt9455_fields {
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 */
92 F_MAX_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
182 * voltage.
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
197 * When the charger is in boost mode, REG02[7:2] represent boost output
198 * voltage.
200 /* REG02[7:2] (Boost output voltage) in uV */
201 static const int rt9455_boost_voltage_values[] = {
202 4425000, 4450000, 4475000, 4500000, 4525000, 4550000, 4575000, 4600000,
203 4625000, 4650000, 4675000, 4700000, 4725000, 4750000, 4775000, 4800000,
204 4825000, 4850000, 4875000, 4900000, 4925000, 4950000, 4975000, 5000000,
205 5025000, 5050000, 5075000, 5100000, 5125000, 5150000, 5175000, 5200000,
206 5225000, 5250000, 5275000, 5300000, 5325000, 5350000, 5375000, 5400000,
207 5425000, 5450000, 5475000, 5500000, 5525000, 5550000, 5575000, 5600000,
208 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000,
209 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000, 5600000,
212 /* REG07[3:0] (VMREG) in uV */
213 static const int rt9455_vmreg_values[] = {
214 4200000, 4220000, 4240000, 4260000, 4280000, 4300000, 4320000, 4340000,
215 4360000, 4380000, 4400000, 4430000, 4450000, 4450000, 4450000, 4450000
218 /* REG05[5:4] (IEOC_PERCENTAGE) */
219 static const int rt9455_ieoc_percentage_values[] = {
220 10, 30, 20, 30
223 /* REG05[1:0] (MIVR) in uV */
224 static const int rt9455_mivr_values[] = {
225 4000000, 4250000, 4500000, 5000000
228 /* REG05[1:0] (IAICR) in uA */
229 static const int rt9455_iaicr_values[] = {
230 100000, 500000, 1000000, 2000000
233 struct rt9455_info {
234 struct i2c_client *client;
235 struct regmap *regmap;
236 struct regmap_field *regmap_fields[F_MAX_FIELDS];
237 struct power_supply *charger;
238 #if IS_ENABLED(CONFIG_USB_PHY)
239 struct usb_phy *usb_phy;
240 struct notifier_block nb;
241 #endif
242 struct delayed_work pwr_rdy_work;
243 struct delayed_work max_charging_time_work;
244 struct delayed_work batt_presence_work;
245 u32 voreg;
246 u32 boost_voltage;
250 * Iterate through each element of the 'tbl' array until an element whose value
251 * is greater than v is found. Return the index of the respective element,
252 * or the index of the last element in the array, if no such element is found.
254 static unsigned int rt9455_find_idx(const int tbl[], int tbl_size, int v)
256 int i;
259 * No need to iterate until the last index in the table because
260 * if no element greater than v is found in the table,
261 * or if only the last element is greater than v,
262 * function returns the index of the last element.
264 for (i = 0; i < tbl_size - 1; i++)
265 if (v <= tbl[i])
266 return i;
268 return (tbl_size - 1);
271 static int rt9455_get_field_val(struct rt9455_info *info,
272 enum rt9455_fields field,
273 const int tbl[], int tbl_size, int *val)
275 unsigned int v;
276 int ret;
278 ret = regmap_field_read(info->regmap_fields[field], &v);
279 if (ret)
280 return ret;
282 v = (v >= tbl_size) ? (tbl_size - 1) : v;
283 *val = tbl[v];
285 return 0;
288 static int rt9455_set_field_val(struct rt9455_info *info,
289 enum rt9455_fields field,
290 const int tbl[], int tbl_size, int val)
292 unsigned int idx = rt9455_find_idx(tbl, tbl_size, val);
294 return regmap_field_write(info->regmap_fields[field], idx);
297 static int rt9455_register_reset(struct rt9455_info *info)
299 struct device *dev = &info->client->dev;
300 unsigned int v;
301 int ret, limit = 100;
303 ret = regmap_field_write(info->regmap_fields[F_RST], 0x01);
304 if (ret) {
305 dev_err(dev, "Failed to set RST bit\n");
306 return ret;
310 * To make sure that reset operation has finished, loop until RST bit
311 * is set to 0.
313 do {
314 ret = regmap_field_read(info->regmap_fields[F_RST], &v);
315 if (ret) {
316 dev_err(dev, "Failed to read RST bit\n");
317 return ret;
320 if (!v)
321 break;
323 usleep_range(10, 100);
324 } while (--limit);
326 if (!limit)
327 return -EIO;
329 return 0;
332 /* Charger power supply property routines */
333 static enum power_supply_property rt9455_charger_properties[] = {
334 POWER_SUPPLY_PROP_STATUS,
335 POWER_SUPPLY_PROP_HEALTH,
336 POWER_SUPPLY_PROP_PRESENT,
337 POWER_SUPPLY_PROP_ONLINE,
338 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
339 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
340 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
341 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
342 POWER_SUPPLY_PROP_SCOPE,
343 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
344 POWER_SUPPLY_PROP_MODEL_NAME,
345 POWER_SUPPLY_PROP_MANUFACTURER,
348 static char *rt9455_charger_supplied_to[] = {
349 "main-battery",
352 static int rt9455_charger_get_status(struct rt9455_info *info,
353 union power_supply_propval *val)
355 unsigned int v, pwr_rdy;
356 int ret;
358 ret = regmap_field_read(info->regmap_fields[F_PWR_RDY],
359 &pwr_rdy);
360 if (ret) {
361 dev_err(&info->client->dev, "Failed to read PWR_RDY bit\n");
362 return ret;
366 * If PWR_RDY bit is unset, the battery is discharging. Otherwise,
367 * STAT bits value must be checked.
369 if (!pwr_rdy) {
370 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
371 return 0;
374 ret = regmap_field_read(info->regmap_fields[F_STAT], &v);
375 if (ret) {
376 dev_err(&info->client->dev, "Failed to read STAT bits\n");
377 return ret;
380 switch (v) {
381 case 0:
383 * If PWR_RDY bit is set, but STAT bits value is 0, the charger
384 * may be in one of the following cases:
385 * 1. CHG_EN bit is 0.
386 * 2. CHG_EN bit is 1 but the battery is not connected.
387 * In any of these cases, POWER_SUPPLY_STATUS_NOT_CHARGING is
388 * returned.
390 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
391 return 0;
392 case 1:
393 val->intval = POWER_SUPPLY_STATUS_CHARGING;
394 return 0;
395 case 2:
396 val->intval = POWER_SUPPLY_STATUS_FULL;
397 return 0;
398 default:
399 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
400 return 0;
404 static int rt9455_charger_get_health(struct rt9455_info *info,
405 union power_supply_propval *val)
407 struct device *dev = &info->client->dev;
408 unsigned int v;
409 int ret;
411 val->intval = POWER_SUPPLY_HEALTH_GOOD;
413 ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &v);
414 if (ret) {
415 dev_err(dev, "Failed to read IRQ1 register\n");
416 return ret;
419 if (v & GET_MASK(F_TSDI)) {
420 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
421 return 0;
423 if (v & GET_MASK(F_VINOVPI)) {
424 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
425 return 0;
427 if (v & GET_MASK(F_BATAB)) {
428 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
429 return 0;
432 ret = regmap_read(info->regmap, RT9455_REG_IRQ2, &v);
433 if (ret) {
434 dev_err(dev, "Failed to read IRQ2 register\n");
435 return ret;
438 if (v & GET_MASK(F_CHBATOVI)) {
439 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
440 return 0;
442 if (v & GET_MASK(F_CH32MI)) {
443 val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
444 return 0;
447 ret = regmap_read(info->regmap, RT9455_REG_IRQ3, &v);
448 if (ret) {
449 dev_err(dev, "Failed to read IRQ3 register\n");
450 return ret;
453 if (v & GET_MASK(F_BSTBUSOVI)) {
454 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
455 return 0;
457 if (v & GET_MASK(F_BSTOLI)) {
458 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
459 return 0;
461 if (v & GET_MASK(F_BSTLOWVI)) {
462 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
463 return 0;
465 if (v & GET_MASK(F_BST32SI)) {
466 val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
467 return 0;
470 ret = regmap_field_read(info->regmap_fields[F_STAT], &v);
471 if (ret) {
472 dev_err(dev, "Failed to read STAT bits\n");
473 return ret;
476 if (v == RT9455_FAULT) {
477 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
478 return 0;
481 return 0;
484 static int rt9455_charger_get_battery_presence(struct rt9455_info *info,
485 union power_supply_propval *val)
487 unsigned int v;
488 int ret;
490 ret = regmap_field_read(info->regmap_fields[F_BATAB], &v);
491 if (ret) {
492 dev_err(&info->client->dev, "Failed to read BATAB bit\n");
493 return ret;
497 * Since BATAB is 1 when battery is NOT present and 0 otherwise,
498 * !BATAB is returned.
500 val->intval = !v;
502 return 0;
505 static int rt9455_charger_get_online(struct rt9455_info *info,
506 union power_supply_propval *val)
508 unsigned int v;
509 int ret;
511 ret = regmap_field_read(info->regmap_fields[F_PWR_RDY], &v);
512 if (ret) {
513 dev_err(&info->client->dev, "Failed to read PWR_RDY bit\n");
514 return ret;
517 val->intval = (int)v;
519 return 0;
522 static int rt9455_charger_get_current(struct rt9455_info *info,
523 union power_supply_propval *val)
525 int curr;
526 int ret;
528 ret = rt9455_get_field_val(info, F_ICHRG,
529 rt9455_ichrg_values,
530 ARRAY_SIZE(rt9455_ichrg_values),
531 &curr);
532 if (ret) {
533 dev_err(&info->client->dev, "Failed to read ICHRG value\n");
534 return ret;
537 val->intval = curr;
539 return 0;
542 static int rt9455_charger_get_current_max(struct rt9455_info *info,
543 union power_supply_propval *val)
545 int idx = ARRAY_SIZE(rt9455_ichrg_values) - 1;
547 val->intval = rt9455_ichrg_values[idx];
549 return 0;
552 static int rt9455_charger_get_voltage(struct rt9455_info *info,
553 union power_supply_propval *val)
555 int voltage;
556 int ret;
558 ret = rt9455_get_field_val(info, F_VOREG,
559 rt9455_voreg_values,
560 ARRAY_SIZE(rt9455_voreg_values),
561 &voltage);
562 if (ret) {
563 dev_err(&info->client->dev, "Failed to read VOREG value\n");
564 return ret;
567 val->intval = voltage;
569 return 0;
572 static int rt9455_charger_get_voltage_max(struct rt9455_info *info,
573 union power_supply_propval *val)
575 int idx = ARRAY_SIZE(rt9455_vmreg_values) - 1;
577 val->intval = rt9455_vmreg_values[idx];
579 return 0;
582 static int rt9455_charger_get_term_current(struct rt9455_info *info,
583 union power_supply_propval *val)
585 struct device *dev = &info->client->dev;
586 int ichrg, ieoc_percentage, ret;
588 ret = rt9455_get_field_val(info, F_ICHRG,
589 rt9455_ichrg_values,
590 ARRAY_SIZE(rt9455_ichrg_values),
591 &ichrg);
592 if (ret) {
593 dev_err(dev, "Failed to read ICHRG value\n");
594 return ret;
597 ret = rt9455_get_field_val(info, F_IEOC_PERCENTAGE,
598 rt9455_ieoc_percentage_values,
599 ARRAY_SIZE(rt9455_ieoc_percentage_values),
600 &ieoc_percentage);
601 if (ret) {
602 dev_err(dev, "Failed to read IEOC value\n");
603 return ret;
606 val->intval = ichrg * ieoc_percentage / 100;
608 return 0;
611 static int rt9455_charger_get_property(struct power_supply *psy,
612 enum power_supply_property psp,
613 union power_supply_propval *val)
615 struct rt9455_info *info = power_supply_get_drvdata(psy);
617 switch (psp) {
618 case POWER_SUPPLY_PROP_STATUS:
619 return rt9455_charger_get_status(info, val);
620 case POWER_SUPPLY_PROP_HEALTH:
621 return rt9455_charger_get_health(info, val);
622 case POWER_SUPPLY_PROP_PRESENT:
623 return rt9455_charger_get_battery_presence(info, val);
624 case POWER_SUPPLY_PROP_ONLINE:
625 return rt9455_charger_get_online(info, val);
626 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
627 return rt9455_charger_get_current(info, val);
628 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
629 return rt9455_charger_get_current_max(info, val);
630 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
631 return rt9455_charger_get_voltage(info, val);
632 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
633 return rt9455_charger_get_voltage_max(info, val);
634 case POWER_SUPPLY_PROP_SCOPE:
635 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
636 return 0;
637 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
638 return rt9455_charger_get_term_current(info, val);
639 case POWER_SUPPLY_PROP_MODEL_NAME:
640 val->strval = RT9455_MODEL_NAME;
641 return 0;
642 case POWER_SUPPLY_PROP_MANUFACTURER:
643 val->strval = RT9455_MANUFACTURER;
644 return 0;
645 default:
646 return -ENODATA;
650 static int rt9455_hw_init(struct rt9455_info *info, u32 ichrg,
651 u32 ieoc_percentage,
652 u32 mivr, u32 iaicr)
654 struct device *dev = &info->client->dev;
655 int idx, ret;
657 ret = rt9455_register_reset(info);
658 if (ret) {
659 dev_err(dev, "Power On Reset failed\n");
660 return ret;
663 /* Set TE bit in order to enable end of charge detection */
664 ret = regmap_field_write(info->regmap_fields[F_TE], 1);
665 if (ret) {
666 dev_err(dev, "Failed to set TE bit\n");
667 return ret;
670 /* Set TE_SHDN_EN bit in order to enable end of charge detection */
671 ret = regmap_field_write(info->regmap_fields[F_TE_SHDN_EN], 1);
672 if (ret) {
673 dev_err(dev, "Failed to set TE_SHDN_EN bit\n");
674 return ret;
678 * Set BATD_EN bit in order to enable battery detection
679 * when charging is done
681 ret = regmap_field_write(info->regmap_fields[F_BATD_EN], 1);
682 if (ret) {
683 dev_err(dev, "Failed to set BATD_EN bit\n");
684 return ret;
688 * Disable Safety Timer. In charge mode, this timer terminates charging
689 * if no read or write via I2C is done within 32 minutes. This timer
690 * avoids overcharging the baterry when the OS is not loaded and the
691 * charger is connected to a power source.
692 * In boost mode, this timer triggers BST32SI interrupt if no read or
693 * write via I2C is done within 32 seconds.
694 * When the OS is loaded and the charger driver is inserted, it is used
695 * delayed_work, named max_charging_time_work, to avoid overcharging
696 * the battery.
698 ret = regmap_field_write(info->regmap_fields[F_TMR_EN], 0x00);
699 if (ret) {
700 dev_err(dev, "Failed to disable Safety Timer\n");
701 return ret;
704 /* Set ICHRG to value retrieved from device-specific data */
705 ret = rt9455_set_field_val(info, F_ICHRG,
706 rt9455_ichrg_values,
707 ARRAY_SIZE(rt9455_ichrg_values), ichrg);
708 if (ret) {
709 dev_err(dev, "Failed to set ICHRG value\n");
710 return ret;
713 /* Set IEOC Percentage to value retrieved from device-specific data */
714 ret = rt9455_set_field_val(info, F_IEOC_PERCENTAGE,
715 rt9455_ieoc_percentage_values,
716 ARRAY_SIZE(rt9455_ieoc_percentage_values),
717 ieoc_percentage);
718 if (ret) {
719 dev_err(dev, "Failed to set IEOC Percentage value\n");
720 return ret;
723 /* Set VOREG to value retrieved from device-specific data */
724 ret = rt9455_set_field_val(info, F_VOREG,
725 rt9455_voreg_values,
726 ARRAY_SIZE(rt9455_voreg_values),
727 info->voreg);
728 if (ret) {
729 dev_err(dev, "Failed to set VOREG value\n");
730 return ret;
733 /* Set VMREG value to maximum (4.45V). */
734 idx = ARRAY_SIZE(rt9455_vmreg_values) - 1;
735 ret = rt9455_set_field_val(info, F_VMREG,
736 rt9455_vmreg_values,
737 ARRAY_SIZE(rt9455_vmreg_values),
738 rt9455_vmreg_values[idx]);
739 if (ret) {
740 dev_err(dev, "Failed to set VMREG value\n");
741 return ret;
745 * Set MIVR to value retrieved from device-specific data.
746 * If no value is specified, default value for MIVR is 4.5V.
748 if (mivr == -1)
749 mivr = 4500000;
751 ret = rt9455_set_field_val(info, F_MIVR,
752 rt9455_mivr_values,
753 ARRAY_SIZE(rt9455_mivr_values), mivr);
754 if (ret) {
755 dev_err(dev, "Failed to set MIVR value\n");
756 return ret;
760 * Set IAICR to value retrieved from device-specific data.
761 * If no value is specified, default value for IAICR is 500 mA.
763 if (iaicr == -1)
764 iaicr = 500000;
766 ret = rt9455_set_field_val(info, F_IAICR,
767 rt9455_iaicr_values,
768 ARRAY_SIZE(rt9455_iaicr_values), iaicr);
769 if (ret) {
770 dev_err(dev, "Failed to set IAICR value\n");
771 return ret;
775 * Set IAICR_INT bit so that IAICR value is determined by IAICR bits
776 * and not by OTG pin.
778 ret = regmap_field_write(info->regmap_fields[F_IAICR_INT], 0x01);
779 if (ret) {
780 dev_err(dev, "Failed to set IAICR_INT bit\n");
781 return ret;
785 * Disable CHMIVRI interrupt. Because the driver sets MIVR value,
786 * CHMIVRI is triggered, but there is no action to be taken by the
787 * driver when CHMIVRI is triggered.
789 ret = regmap_field_write(info->regmap_fields[F_CHMIVRIM], 0x01);
790 if (ret) {
791 dev_err(dev, "Failed to mask CHMIVRI interrupt\n");
792 return ret;
795 return 0;
798 #if IS_ENABLED(CONFIG_USB_PHY)
800 * Before setting the charger into boost mode, boost output voltage is
801 * set. This is needed because boost output voltage may differ from battery
802 * regulation voltage. F_VOREG bits represent either battery regulation voltage
803 * or boost output voltage, depending on the mode the charger is. Both battery
804 * regulation voltage and boost output voltage are read from DT/ACPI during
805 * probe.
807 static int rt9455_set_boost_voltage_before_boost_mode(struct rt9455_info *info)
809 struct device *dev = &info->client->dev;
810 int ret;
812 ret = rt9455_set_field_val(info, F_VOREG,
813 rt9455_boost_voltage_values,
814 ARRAY_SIZE(rt9455_boost_voltage_values),
815 info->boost_voltage);
816 if (ret) {
817 dev_err(dev, "Failed to set boost output voltage value\n");
818 return ret;
821 return 0;
823 #endif
826 * Before setting the charger into charge mode, battery regulation voltage is
827 * set. This is needed because boost output voltage may differ from battery
828 * regulation voltage. F_VOREG bits represent either battery regulation voltage
829 * or boost output voltage, depending on the mode the charger is. Both battery
830 * regulation voltage and boost output voltage are read from DT/ACPI during
831 * probe.
833 static int rt9455_set_voreg_before_charge_mode(struct rt9455_info *info)
835 struct device *dev = &info->client->dev;
836 int ret;
838 ret = rt9455_set_field_val(info, F_VOREG,
839 rt9455_voreg_values,
840 ARRAY_SIZE(rt9455_voreg_values),
841 info->voreg);
842 if (ret) {
843 dev_err(dev, "Failed to set VOREG value\n");
844 return ret;
847 return 0;
850 static int rt9455_irq_handler_check_irq1_register(struct rt9455_info *info,
851 bool *_is_battery_absent,
852 bool *_alert_userspace)
854 unsigned int irq1, mask1, mask2;
855 struct device *dev = &info->client->dev;
856 bool is_battery_absent = false;
857 bool alert_userspace = false;
858 int ret;
860 ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &irq1);
861 if (ret) {
862 dev_err(dev, "Failed to read IRQ1 register\n");
863 return ret;
866 ret = regmap_read(info->regmap, RT9455_REG_MASK1, &mask1);
867 if (ret) {
868 dev_err(dev, "Failed to read MASK1 register\n");
869 return ret;
872 if (irq1 & GET_MASK(F_TSDI)) {
873 dev_err(dev, "Thermal shutdown fault occurred\n");
874 alert_userspace = true;
877 if (irq1 & GET_MASK(F_VINOVPI)) {
878 dev_err(dev, "Overvoltage input occurred\n");
879 alert_userspace = true;
882 if (irq1 & GET_MASK(F_BATAB)) {
883 dev_err(dev, "Battery absence occurred\n");
884 is_battery_absent = true;
885 alert_userspace = true;
887 if ((mask1 & GET_MASK(F_BATABM)) == 0) {
888 ret = regmap_field_write(info->regmap_fields[F_BATABM],
889 0x01);
890 if (ret) {
891 dev_err(dev, "Failed to mask BATAB interrupt\n");
892 return ret;
896 ret = regmap_read(info->regmap, RT9455_REG_MASK2, &mask2);
897 if (ret) {
898 dev_err(dev, "Failed to read MASK2 register\n");
899 return ret;
902 if (mask2 & GET_MASK(F_CHTERMIM)) {
903 ret = regmap_field_write(
904 info->regmap_fields[F_CHTERMIM], 0x00);
905 if (ret) {
906 dev_err(dev, "Failed to unmask CHTERMI interrupt\n");
907 return ret;
911 if (mask2 & GET_MASK(F_CHRCHGIM)) {
912 ret = regmap_field_write(
913 info->regmap_fields[F_CHRCHGIM], 0x00);
914 if (ret) {
915 dev_err(dev, "Failed to unmask CHRCHGI interrupt\n");
916 return ret;
921 * When the battery is absent, max_charging_time_work is
922 * cancelled, since no charging is done.
924 cancel_delayed_work_sync(&info->max_charging_time_work);
926 * Since no interrupt is triggered when the battery is
927 * reconnected, max_charging_time_work is not rescheduled.
928 * Therefore, batt_presence_work is scheduled to check whether
929 * the battery is still absent or not.
931 queue_delayed_work(system_power_efficient_wq,
932 &info->batt_presence_work,
933 RT9455_BATT_PRESENCE_DELAY * HZ);
936 *_is_battery_absent = is_battery_absent;
938 if (alert_userspace)
939 *_alert_userspace = alert_userspace;
941 return 0;
944 static int rt9455_irq_handler_check_irq2_register(struct rt9455_info *info,
945 bool is_battery_absent,
946 bool *_alert_userspace)
948 unsigned int irq2, mask2;
949 struct device *dev = &info->client->dev;
950 bool alert_userspace = false;
951 int ret;
953 ret = regmap_read(info->regmap, RT9455_REG_IRQ2, &irq2);
954 if (ret) {
955 dev_err(dev, "Failed to read IRQ2 register\n");
956 return ret;
959 ret = regmap_read(info->regmap, RT9455_REG_MASK2, &mask2);
960 if (ret) {
961 dev_err(dev, "Failed to read MASK2 register\n");
962 return ret;
965 if (irq2 & GET_MASK(F_CHRVPI)) {
966 dev_dbg(dev, "Charger fault occurred\n");
968 * CHRVPI bit is set in 2 cases:
969 * 1. when the power source is connected to the charger.
970 * 2. when the power source is disconnected from the charger.
971 * To identify the case, PWR_RDY bit is checked. Because
972 * PWR_RDY bit is set / cleared after CHRVPI interrupt is
973 * triggered, it is used delayed_work to later read PWR_RDY bit.
974 * Also, do not set to true alert_userspace, because there is no
975 * need to notify userspace when CHRVPI interrupt has occurred.
976 * Userspace will be notified after PWR_RDY bit is read.
978 queue_delayed_work(system_power_efficient_wq,
979 &info->pwr_rdy_work,
980 RT9455_PWR_RDY_DELAY * HZ);
982 if (irq2 & GET_MASK(F_CHBATOVI)) {
983 dev_err(dev, "Battery OVP occurred\n");
984 alert_userspace = true;
986 if (irq2 & GET_MASK(F_CHTERMI)) {
987 dev_dbg(dev, "Charge terminated\n");
988 if (!is_battery_absent) {
989 if ((mask2 & GET_MASK(F_CHTERMIM)) == 0) {
990 ret = regmap_field_write(
991 info->regmap_fields[F_CHTERMIM], 0x01);
992 if (ret) {
993 dev_err(dev, "Failed to mask CHTERMI interrupt\n");
994 return ret;
997 * Update MASK2 value, since CHTERMIM bit is
998 * set.
1000 mask2 = mask2 | GET_MASK(F_CHTERMIM);
1002 cancel_delayed_work_sync(&info->max_charging_time_work);
1003 alert_userspace = true;
1006 if (irq2 & GET_MASK(F_CHRCHGI)) {
1007 dev_dbg(dev, "Recharge request\n");
1008 ret = regmap_field_write(info->regmap_fields[F_CHG_EN],
1009 RT9455_CHARGE_ENABLE);
1010 if (ret) {
1011 dev_err(dev, "Failed to enable charging\n");
1012 return ret;
1014 if (mask2 & GET_MASK(F_CHTERMIM)) {
1015 ret = regmap_field_write(
1016 info->regmap_fields[F_CHTERMIM], 0x00);
1017 if (ret) {
1018 dev_err(dev, "Failed to unmask CHTERMI interrupt\n");
1019 return ret;
1021 /* Update MASK2 value, since CHTERMIM bit is cleared. */
1022 mask2 = mask2 & ~GET_MASK(F_CHTERMIM);
1024 if (!is_battery_absent) {
1026 * No need to check whether the charger is connected to
1027 * power source when CHRCHGI is received, since CHRCHGI
1028 * is not triggered if the charger is not connected to
1029 * the power source.
1031 queue_delayed_work(system_power_efficient_wq,
1032 &info->max_charging_time_work,
1033 RT9455_MAX_CHARGING_TIME * HZ);
1034 alert_userspace = true;
1037 if (irq2 & GET_MASK(F_CH32MI)) {
1038 dev_err(dev, "Charger fault. 32 mins timeout occurred\n");
1039 alert_userspace = true;
1041 if (irq2 & GET_MASK(F_CHTREGI)) {
1042 dev_warn(dev,
1043 "Charger warning. Thermal regulation loop active\n");
1044 alert_userspace = true;
1046 if (irq2 & GET_MASK(F_CHMIVRI)) {
1047 dev_dbg(dev,
1048 "Charger warning. Input voltage MIVR loop active\n");
1051 if (alert_userspace)
1052 *_alert_userspace = alert_userspace;
1054 return 0;
1057 static int rt9455_irq_handler_check_irq3_register(struct rt9455_info *info,
1058 bool *_alert_userspace)
1060 unsigned int irq3, mask3;
1061 struct device *dev = &info->client->dev;
1062 bool alert_userspace = false;
1063 int ret;
1065 ret = regmap_read(info->regmap, RT9455_REG_IRQ3, &irq3);
1066 if (ret) {
1067 dev_err(dev, "Failed to read IRQ3 register\n");
1068 return ret;
1071 ret = regmap_read(info->regmap, RT9455_REG_MASK3, &mask3);
1072 if (ret) {
1073 dev_err(dev, "Failed to read MASK3 register\n");
1074 return ret;
1077 if (irq3 & GET_MASK(F_BSTBUSOVI)) {
1078 dev_err(dev, "Boost fault. Overvoltage input occurred\n");
1079 alert_userspace = true;
1081 if (irq3 & GET_MASK(F_BSTOLI)) {
1082 dev_err(dev, "Boost fault. Overload\n");
1083 alert_userspace = true;
1085 if (irq3 & GET_MASK(F_BSTLOWVI)) {
1086 dev_err(dev, "Boost fault. Battery voltage too low\n");
1087 alert_userspace = true;
1089 if (irq3 & GET_MASK(F_BST32SI)) {
1090 dev_err(dev, "Boost fault. 32 seconds timeout occurred.\n");
1091 alert_userspace = true;
1094 if (alert_userspace) {
1095 dev_info(dev, "Boost fault occurred, therefore the charger goes into charge mode\n");
1096 ret = rt9455_set_voreg_before_charge_mode(info);
1097 if (ret) {
1098 dev_err(dev, "Failed to set VOREG before entering charge mode\n");
1099 return ret;
1101 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1102 RT9455_CHARGE_MODE);
1103 if (ret) {
1104 dev_err(dev, "Failed to set charger in charge mode\n");
1105 return ret;
1107 *_alert_userspace = alert_userspace;
1110 return 0;
1113 static irqreturn_t rt9455_irq_handler_thread(int irq, void *data)
1115 struct rt9455_info *info = data;
1116 struct device *dev;
1117 bool alert_userspace = false;
1118 bool is_battery_absent = false;
1119 unsigned int status;
1120 int ret;
1122 if (!info)
1123 return IRQ_NONE;
1125 dev = &info->client->dev;
1127 if (irq != info->client->irq) {
1128 dev_err(dev, "Interrupt is not for RT9455 charger\n");
1129 return IRQ_NONE;
1132 ret = regmap_field_read(info->regmap_fields[F_STAT], &status);
1133 if (ret) {
1134 dev_err(dev, "Failed to read STAT bits\n");
1135 return IRQ_HANDLED;
1137 dev_dbg(dev, "Charger status is %d\n", status);
1140 * Each function that processes an IRQ register receives as output
1141 * parameter alert_userspace pointer. alert_userspace is set to true
1142 * in such a function only if an interrupt has occurred in the
1143 * respective interrupt register. This way, it is avoided the following
1144 * case: interrupt occurs only in IRQ1 register,
1145 * rt9455_irq_handler_check_irq1_register() function sets to true
1146 * alert_userspace, but rt9455_irq_handler_check_irq2_register()
1147 * and rt9455_irq_handler_check_irq3_register() functions set to false
1148 * alert_userspace and power_supply_changed() is never called.
1150 ret = rt9455_irq_handler_check_irq1_register(info, &is_battery_absent,
1151 &alert_userspace);
1152 if (ret) {
1153 dev_err(dev, "Failed to handle IRQ1 register\n");
1154 return IRQ_HANDLED;
1157 ret = rt9455_irq_handler_check_irq2_register(info, is_battery_absent,
1158 &alert_userspace);
1159 if (ret) {
1160 dev_err(dev, "Failed to handle IRQ2 register\n");
1161 return IRQ_HANDLED;
1164 ret = rt9455_irq_handler_check_irq3_register(info, &alert_userspace);
1165 if (ret) {
1166 dev_err(dev, "Failed to handle IRQ3 register\n");
1167 return IRQ_HANDLED;
1170 if (alert_userspace) {
1172 * Sometimes, an interrupt occurs while rt9455_probe() function
1173 * is executing and power_supply_register() is not yet called.
1174 * Do not call power_supply_changed() in this case.
1176 if (info->charger)
1177 power_supply_changed(info->charger);
1180 return IRQ_HANDLED;
1183 static int rt9455_discover_charger(struct rt9455_info *info, u32 *ichrg,
1184 u32 *ieoc_percentage,
1185 u32 *mivr, u32 *iaicr)
1187 struct device *dev = &info->client->dev;
1188 int ret;
1190 if (!dev->of_node && !ACPI_HANDLE(dev)) {
1191 dev_err(dev, "No support for either device tree or ACPI\n");
1192 return -EINVAL;
1195 * ICHRG, IEOC_PERCENTAGE, VOREG and boost output voltage are mandatory
1196 * parameters.
1198 ret = device_property_read_u32(dev, "richtek,output-charge-current",
1199 ichrg);
1200 if (ret) {
1201 dev_err(dev, "Error: missing \"output-charge-current\" property\n");
1202 return ret;
1205 ret = device_property_read_u32(dev, "richtek,end-of-charge-percentage",
1206 ieoc_percentage);
1207 if (ret) {
1208 dev_err(dev, "Error: missing \"end-of-charge-percentage\" property\n");
1209 return ret;
1212 ret = device_property_read_u32(dev,
1213 "richtek,battery-regulation-voltage",
1214 &info->voreg);
1215 if (ret) {
1216 dev_err(dev, "Error: missing \"battery-regulation-voltage\" property\n");
1217 return ret;
1220 ret = device_property_read_u32(dev, "richtek,boost-output-voltage",
1221 &info->boost_voltage);
1222 if (ret) {
1223 dev_err(dev, "Error: missing \"boost-output-voltage\" property\n");
1224 return ret;
1228 * MIVR and IAICR are optional parameters. Do not return error if one of
1229 * them is not present in ACPI table or device tree specification.
1231 device_property_read_u32(dev, "richtek,min-input-voltage-regulation",
1232 mivr);
1233 device_property_read_u32(dev, "richtek,avg-input-current-regulation",
1234 iaicr);
1236 return 0;
1239 #if IS_ENABLED(CONFIG_USB_PHY)
1240 static int rt9455_usb_event_none(struct rt9455_info *info,
1241 u8 opa_mode, u8 iaicr)
1243 struct device *dev = &info->client->dev;
1244 int ret;
1246 if (opa_mode == RT9455_BOOST_MODE) {
1247 ret = rt9455_set_voreg_before_charge_mode(info);
1248 if (ret) {
1249 dev_err(dev, "Failed to set VOREG before entering charge mode\n");
1250 return ret;
1253 * If the charger is in boost mode, and it has received
1254 * USB_EVENT_NONE, this means the consumer device powered by the
1255 * charger is not connected anymore.
1256 * In this case, the charger goes into charge mode.
1258 dev_dbg(dev, "USB_EVENT_NONE received, therefore the charger goes into charge mode\n");
1259 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1260 RT9455_CHARGE_MODE);
1261 if (ret) {
1262 dev_err(dev, "Failed to set charger in charge mode\n");
1263 return NOTIFY_DONE;
1267 dev_dbg(dev, "USB_EVENT_NONE received, therefore IAICR is set to its minimum value\n");
1268 if (iaicr != RT9455_IAICR_100MA) {
1269 ret = regmap_field_write(info->regmap_fields[F_IAICR],
1270 RT9455_IAICR_100MA);
1271 if (ret) {
1272 dev_err(dev, "Failed to set IAICR value\n");
1273 return NOTIFY_DONE;
1277 return NOTIFY_OK;
1280 static int rt9455_usb_event_vbus(struct rt9455_info *info,
1281 u8 opa_mode, u8 iaicr)
1283 struct device *dev = &info->client->dev;
1284 int ret;
1286 if (opa_mode == RT9455_BOOST_MODE) {
1287 ret = rt9455_set_voreg_before_charge_mode(info);
1288 if (ret) {
1289 dev_err(dev, "Failed to set VOREG before entering charge mode\n");
1290 return ret;
1293 * If the charger is in boost mode, and it has received
1294 * USB_EVENT_VBUS, this means the consumer device powered by the
1295 * charger is not connected anymore.
1296 * In this case, the charger goes into charge mode.
1298 dev_dbg(dev, "USB_EVENT_VBUS received, therefore the charger goes into charge mode\n");
1299 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1300 RT9455_CHARGE_MODE);
1301 if (ret) {
1302 dev_err(dev, "Failed to set charger in charge mode\n");
1303 return NOTIFY_DONE;
1307 dev_dbg(dev, "USB_EVENT_VBUS received, therefore IAICR is set to 500 mA\n");
1308 if (iaicr != RT9455_IAICR_500MA) {
1309 ret = regmap_field_write(info->regmap_fields[F_IAICR],
1310 RT9455_IAICR_500MA);
1311 if (ret) {
1312 dev_err(dev, "Failed to set IAICR value\n");
1313 return NOTIFY_DONE;
1317 return NOTIFY_OK;
1320 static int rt9455_usb_event_id(struct rt9455_info *info,
1321 u8 opa_mode, u8 iaicr)
1323 struct device *dev = &info->client->dev;
1324 int ret;
1326 if (opa_mode == RT9455_CHARGE_MODE) {
1327 ret = rt9455_set_boost_voltage_before_boost_mode(info);
1328 if (ret) {
1329 dev_err(dev, "Failed to set boost output voltage before entering boost mode\n");
1330 return ret;
1333 * If the charger is in charge mode, and it has received
1334 * USB_EVENT_ID, this means a consumer device is connected and
1335 * it should be powered by the charger.
1336 * In this case, the charger goes into boost mode.
1338 dev_dbg(dev, "USB_EVENT_ID received, therefore the charger goes into boost mode\n");
1339 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1340 RT9455_BOOST_MODE);
1341 if (ret) {
1342 dev_err(dev, "Failed to set charger in boost mode\n");
1343 return NOTIFY_DONE;
1347 dev_dbg(dev, "USB_EVENT_ID received, therefore IAICR is set to its minimum value\n");
1348 if (iaicr != RT9455_IAICR_100MA) {
1349 ret = regmap_field_write(info->regmap_fields[F_IAICR],
1350 RT9455_IAICR_100MA);
1351 if (ret) {
1352 dev_err(dev, "Failed to set IAICR value\n");
1353 return NOTIFY_DONE;
1357 return NOTIFY_OK;
1360 static int rt9455_usb_event_charger(struct rt9455_info *info,
1361 u8 opa_mode, u8 iaicr)
1363 struct device *dev = &info->client->dev;
1364 int ret;
1366 if (opa_mode == RT9455_BOOST_MODE) {
1367 ret = rt9455_set_voreg_before_charge_mode(info);
1368 if (ret) {
1369 dev_err(dev, "Failed to set VOREG before entering charge mode\n");
1370 return ret;
1373 * If the charger is in boost mode, and it has received
1374 * USB_EVENT_CHARGER, this means the consumer device powered by
1375 * the charger is not connected anymore.
1376 * In this case, the charger goes into charge mode.
1378 dev_dbg(dev, "USB_EVENT_CHARGER received, therefore the charger goes into charge mode\n");
1379 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1380 RT9455_CHARGE_MODE);
1381 if (ret) {
1382 dev_err(dev, "Failed to set charger in charge mode\n");
1383 return NOTIFY_DONE;
1387 dev_dbg(dev, "USB_EVENT_CHARGER received, therefore IAICR is set to no current limit\n");
1388 if (iaicr != RT9455_IAICR_NO_LIMIT) {
1389 ret = regmap_field_write(info->regmap_fields[F_IAICR],
1390 RT9455_IAICR_NO_LIMIT);
1391 if (ret) {
1392 dev_err(dev, "Failed to set IAICR value\n");
1393 return NOTIFY_DONE;
1397 return NOTIFY_OK;
1400 static int rt9455_usb_event(struct notifier_block *nb,
1401 unsigned long event, void *power)
1403 struct rt9455_info *info = container_of(nb, struct rt9455_info, nb);
1404 struct device *dev = &info->client->dev;
1405 unsigned int opa_mode, iaicr;
1406 int ret;
1409 * Determine whether the charger is in charge mode
1410 * or in boost mode.
1412 ret = regmap_field_read(info->regmap_fields[F_OPA_MODE],
1413 &opa_mode);
1414 if (ret) {
1415 dev_err(dev, "Failed to read OPA_MODE value\n");
1416 return NOTIFY_DONE;
1419 ret = regmap_field_read(info->regmap_fields[F_IAICR],
1420 &iaicr);
1421 if (ret) {
1422 dev_err(dev, "Failed to read IAICR value\n");
1423 return NOTIFY_DONE;
1426 dev_dbg(dev, "Received USB event %lu\n", event);
1427 switch (event) {
1428 case USB_EVENT_NONE:
1429 return rt9455_usb_event_none(info, opa_mode, iaicr);
1430 case USB_EVENT_VBUS:
1431 return rt9455_usb_event_vbus(info, opa_mode, iaicr);
1432 case USB_EVENT_ID:
1433 return rt9455_usb_event_id(info, opa_mode, iaicr);
1434 case USB_EVENT_CHARGER:
1435 return rt9455_usb_event_charger(info, opa_mode, iaicr);
1436 default:
1437 dev_err(dev, "Unknown USB event\n");
1439 return NOTIFY_DONE;
1441 #endif
1443 static void rt9455_pwr_rdy_work_callback(struct work_struct *work)
1445 struct rt9455_info *info = container_of(work, struct rt9455_info,
1446 pwr_rdy_work.work);
1447 struct device *dev = &info->client->dev;
1448 unsigned int pwr_rdy;
1449 int ret;
1451 ret = regmap_field_read(info->regmap_fields[F_PWR_RDY], &pwr_rdy);
1452 if (ret) {
1453 dev_err(dev, "Failed to read PWR_RDY bit\n");
1454 return;
1456 switch (pwr_rdy) {
1457 case RT9455_PWR_FAULT:
1458 dev_dbg(dev, "Charger disconnected from power source\n");
1459 cancel_delayed_work_sync(&info->max_charging_time_work);
1460 break;
1461 case RT9455_PWR_GOOD:
1462 dev_dbg(dev, "Charger connected to power source\n");
1463 ret = regmap_field_write(info->regmap_fields[F_CHG_EN],
1464 RT9455_CHARGE_ENABLE);
1465 if (ret) {
1466 dev_err(dev, "Failed to enable charging\n");
1467 return;
1469 queue_delayed_work(system_power_efficient_wq,
1470 &info->max_charging_time_work,
1471 RT9455_MAX_CHARGING_TIME * HZ);
1472 break;
1475 * Notify userspace that the charger has been either connected to or
1476 * disconnected from the power source.
1478 power_supply_changed(info->charger);
1481 static void rt9455_max_charging_time_work_callback(struct work_struct *work)
1483 struct rt9455_info *info = container_of(work, struct rt9455_info,
1484 max_charging_time_work.work);
1485 struct device *dev = &info->client->dev;
1486 int ret;
1488 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");
1489 ret = regmap_field_write(info->regmap_fields[F_CHG_EN],
1490 RT9455_CHARGE_DISABLE);
1491 if (ret)
1492 dev_err(dev, "Failed to disable charging\n");
1495 static void rt9455_batt_presence_work_callback(struct work_struct *work)
1497 struct rt9455_info *info = container_of(work, struct rt9455_info,
1498 batt_presence_work.work);
1499 struct device *dev = &info->client->dev;
1500 unsigned int irq1, mask1;
1501 int ret;
1503 ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &irq1);
1504 if (ret) {
1505 dev_err(dev, "Failed to read IRQ1 register\n");
1506 return;
1510 * If the battery is still absent, batt_presence_work is rescheduled.
1511 * Otherwise, max_charging_time is scheduled.
1513 if (irq1 & GET_MASK(F_BATAB)) {
1514 queue_delayed_work(system_power_efficient_wq,
1515 &info->batt_presence_work,
1516 RT9455_BATT_PRESENCE_DELAY * HZ);
1517 } else {
1518 queue_delayed_work(system_power_efficient_wq,
1519 &info->max_charging_time_work,
1520 RT9455_MAX_CHARGING_TIME * HZ);
1522 ret = regmap_read(info->regmap, RT9455_REG_MASK1, &mask1);
1523 if (ret) {
1524 dev_err(dev, "Failed to read MASK1 register\n");
1525 return;
1528 if (mask1 & GET_MASK(F_BATABM)) {
1529 ret = regmap_field_write(info->regmap_fields[F_BATABM],
1530 0x00);
1531 if (ret)
1532 dev_err(dev, "Failed to unmask BATAB interrupt\n");
1535 * Notify userspace that the battery is now connected to the
1536 * charger.
1538 power_supply_changed(info->charger);
1542 static const struct power_supply_desc rt9455_charger_desc = {
1543 .name = RT9455_DRIVER_NAME,
1544 .type = POWER_SUPPLY_TYPE_USB,
1545 .properties = rt9455_charger_properties,
1546 .num_properties = ARRAY_SIZE(rt9455_charger_properties),
1547 .get_property = rt9455_charger_get_property,
1550 static bool rt9455_is_writeable_reg(struct device *dev, unsigned int reg)
1552 switch (reg) {
1553 case RT9455_REG_DEV_ID:
1554 case RT9455_REG_IRQ1:
1555 case RT9455_REG_IRQ2:
1556 case RT9455_REG_IRQ3:
1557 return false;
1558 default:
1559 return true;
1563 static bool rt9455_is_volatile_reg(struct device *dev, unsigned int reg)
1565 switch (reg) {
1566 case RT9455_REG_DEV_ID:
1567 case RT9455_REG_CTRL5:
1568 case RT9455_REG_CTRL6:
1569 return false;
1570 default:
1571 return true;
1575 static const struct regmap_config rt9455_regmap_config = {
1576 .reg_bits = 8,
1577 .val_bits = 8,
1578 .writeable_reg = rt9455_is_writeable_reg,
1579 .volatile_reg = rt9455_is_volatile_reg,
1580 .max_register = RT9455_REG_MASK3,
1581 .cache_type = REGCACHE_RBTREE,
1584 static int rt9455_probe(struct i2c_client *client,
1585 const struct i2c_device_id *id)
1587 struct i2c_adapter *adapter = client->adapter;
1588 struct device *dev = &client->dev;
1589 struct rt9455_info *info;
1590 struct power_supply_config rt9455_charger_config = {};
1592 * Mandatory device-specific data values. Also, VOREG and boost output
1593 * voltage are mandatory values, but they are stored in rt9455_info
1594 * structure.
1596 u32 ichrg, ieoc_percentage;
1597 /* Optional device-specific data values. */
1598 u32 mivr = -1, iaicr = -1;
1599 int i, ret;
1601 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1602 dev_err(dev, "No support for SMBUS_BYTE_DATA\n");
1603 return -ENODEV;
1605 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
1606 if (!info)
1607 return -ENOMEM;
1609 info->client = client;
1610 i2c_set_clientdata(client, info);
1612 info->regmap = devm_regmap_init_i2c(client,
1613 &rt9455_regmap_config);
1614 if (IS_ERR(info->regmap)) {
1615 dev_err(dev, "Failed to initialize register map\n");
1616 return -EINVAL;
1619 for (i = 0; i < F_MAX_FIELDS; i++) {
1620 info->regmap_fields[i] =
1621 devm_regmap_field_alloc(dev, info->regmap,
1622 rt9455_reg_fields[i]);
1623 if (IS_ERR(info->regmap_fields[i])) {
1624 dev_err(dev,
1625 "Failed to allocate regmap field = %d\n", i);
1626 return PTR_ERR(info->regmap_fields[i]);
1630 ret = rt9455_discover_charger(info, &ichrg, &ieoc_percentage,
1631 &mivr, &iaicr);
1632 if (ret) {
1633 dev_err(dev, "Failed to discover charger\n");
1634 return ret;
1637 #if IS_ENABLED(CONFIG_USB_PHY)
1638 info->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
1639 if (IS_ERR(info->usb_phy)) {
1640 dev_err(dev, "Failed to get USB transceiver\n");
1641 } else {
1642 info->nb.notifier_call = rt9455_usb_event;
1643 ret = usb_register_notifier(info->usb_phy, &info->nb);
1644 if (ret) {
1645 dev_err(dev, "Failed to register USB notifier\n");
1647 * If usb_register_notifier() fails, set notifier_call
1648 * to NULL, to avoid calling usb_unregister_notifier().
1650 info->nb.notifier_call = NULL;
1653 #endif
1655 INIT_DEFERRABLE_WORK(&info->pwr_rdy_work, rt9455_pwr_rdy_work_callback);
1656 INIT_DEFERRABLE_WORK(&info->max_charging_time_work,
1657 rt9455_max_charging_time_work_callback);
1658 INIT_DEFERRABLE_WORK(&info->batt_presence_work,
1659 rt9455_batt_presence_work_callback);
1661 rt9455_charger_config.of_node = dev->of_node;
1662 rt9455_charger_config.drv_data = info;
1663 rt9455_charger_config.supplied_to = rt9455_charger_supplied_to;
1664 rt9455_charger_config.num_supplicants =
1665 ARRAY_SIZE(rt9455_charger_supplied_to);
1666 ret = devm_request_threaded_irq(dev, client->irq, NULL,
1667 rt9455_irq_handler_thread,
1668 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1669 RT9455_DRIVER_NAME, info);
1670 if (ret) {
1671 dev_err(dev, "Failed to register IRQ handler\n");
1672 goto put_usb_notifier;
1675 ret = rt9455_hw_init(info, ichrg, ieoc_percentage, mivr, iaicr);
1676 if (ret) {
1677 dev_err(dev, "Failed to set charger to its default values\n");
1678 goto put_usb_notifier;
1681 info->charger = devm_power_supply_register(dev, &rt9455_charger_desc,
1682 &rt9455_charger_config);
1683 if (IS_ERR(info->charger)) {
1684 dev_err(dev, "Failed to register charger\n");
1685 ret = PTR_ERR(info->charger);
1686 goto put_usb_notifier;
1689 return 0;
1691 put_usb_notifier:
1692 #if IS_ENABLED(CONFIG_USB_PHY)
1693 if (info->nb.notifier_call) {
1694 usb_unregister_notifier(info->usb_phy, &info->nb);
1695 info->nb.notifier_call = NULL;
1697 #endif
1698 return ret;
1701 static int rt9455_remove(struct i2c_client *client)
1703 int ret;
1704 struct rt9455_info *info = i2c_get_clientdata(client);
1706 ret = rt9455_register_reset(info);
1707 if (ret)
1708 dev_err(&info->client->dev, "Failed to set charger to its default values\n");
1710 #if IS_ENABLED(CONFIG_USB_PHY)
1711 if (info->nb.notifier_call)
1712 usb_unregister_notifier(info->usb_phy, &info->nb);
1713 #endif
1715 cancel_delayed_work_sync(&info->pwr_rdy_work);
1716 cancel_delayed_work_sync(&info->max_charging_time_work);
1717 cancel_delayed_work_sync(&info->batt_presence_work);
1719 return ret;
1722 static const struct i2c_device_id rt9455_i2c_id_table[] = {
1723 { RT9455_DRIVER_NAME, 0 },
1724 { },
1726 MODULE_DEVICE_TABLE(i2c, rt9455_i2c_id_table);
1728 static const struct of_device_id rt9455_of_match[] = {
1729 { .compatible = "richtek,rt9455", },
1730 { },
1732 MODULE_DEVICE_TABLE(of, rt9455_of_match);
1734 static const struct acpi_device_id rt9455_i2c_acpi_match[] = {
1735 { "RT945500", 0 },
1738 MODULE_DEVICE_TABLE(acpi, rt9455_i2c_acpi_match);
1740 static struct i2c_driver rt9455_driver = {
1741 .probe = rt9455_probe,
1742 .remove = rt9455_remove,
1743 .id_table = rt9455_i2c_id_table,
1744 .driver = {
1745 .name = RT9455_DRIVER_NAME,
1746 .of_match_table = of_match_ptr(rt9455_of_match),
1747 .acpi_match_table = ACPI_PTR(rt9455_i2c_acpi_match),
1750 module_i2c_driver(rt9455_driver);
1752 MODULE_LICENSE("GPL");
1753 MODULE_AUTHOR("Anda-Maria Nicolae <anda-maria.nicolae@intel.com>");
1754 MODULE_DESCRIPTION("Richtek RT9455 Charger Driver");