1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Driver for Richtek RT9455WSC battery charger.
4 *
5 * Copyright (C) 2015 Intel Corporation
6 */
7
8 #include <linux/module.h>
9 #include <linux/interrupt.h>
10 #include <linux/delay.h>
11 #include <linux/of.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/power_supply.h>
14 #include <linux/i2c.h>
15 #include <linux/acpi.h>
16 #include <linux/usb/phy.h>
17 #include <linux/regmap.h>
18
19 #define RT9455_MANUFACTURER "Richtek"
20 #define RT9455_MODEL_NAME "RT9455"
21 #define RT9455_DRIVER_NAME "rt9455-charger"
22
23 #define RT9455_IRQ_NAME "interrupt"
24
25 #define RT9455_PWR_RDY_DELAY 1 /* 1 second */
26 #define RT9455_MAX_CHARGING_TIME 21600 /* 6 hrs */
27 #define RT9455_BATT_PRESENCE_DELAY 60 /* 60 seconds */
28
29 #define RT9455_CHARGE_MODE 0x00
30 #define RT9455_BOOST_MODE 0x01
31
32 #define RT9455_FAULT 0x03
33
34 #define RT9455_IAICR_100MA 0x00
35 #define RT9455_IAICR_500MA 0x01
36 #define RT9455_IAICR_NO_LIMIT 0x03
37
38 #define RT9455_CHARGE_DISABLE 0x00
39 #define RT9455_CHARGE_ENABLE 0x01
40
41 #define RT9455_PWR_FAULT 0x00
42 #define RT9455_PWR_GOOD 0x01
43
44 #define RT9455_REG_CTRL1 0x00 /* CTRL1 reg address */
45 #define RT9455_REG_CTRL2 0x01 /* CTRL2 reg address */
46 #define RT9455_REG_CTRL3 0x02 /* CTRL3 reg address */
47 #define RT9455_REG_DEV_ID 0x03 /* DEV_ID reg address */
48 #define RT9455_REG_CTRL4 0x04 /* CTRL4 reg address */
49 #define RT9455_REG_CTRL5 0x05 /* CTRL5 reg address */
50 #define RT9455_REG_CTRL6 0x06 /* CTRL6 reg address */
51 #define RT9455_REG_CTRL7 0x07 /* CTRL7 reg address */
52 #define RT9455_REG_IRQ1 0x08 /* IRQ1 reg address */
53 #define RT9455_REG_IRQ2 0x09 /* IRQ2 reg address */
54 #define RT9455_REG_IRQ3 0x0A /* IRQ3 reg address */
55 #define RT9455_REG_MASK1 0x0B /* MASK1 reg address */
56 #define RT9455_REG_MASK2 0x0C /* MASK2 reg address */
57 #define RT9455_REG_MASK3 0x0D /* MASK3 reg address */
58
59 enum rt9455_fields {
60 F_STAT, F_BOOST, F_PWR_RDY, F_OTG_PIN_POLARITY, /* CTRL1 reg fields */
61
62 F_IAICR, F_TE_SHDN_EN, F_HIGHER_OCP, F_TE, F_IAICR_INT, F_HIZ,
63 F_OPA_MODE, /* CTRL2 reg fields */
64
65 F_VOREG, F_OTG_PL, F_OTG_EN, /* CTRL3 reg fields */
66
67 F_VENDOR_ID, F_CHIP_REV, /* DEV_ID reg fields */
68
69 F_RST, /* CTRL4 reg fields */
70
71 F_TMR_EN, F_MIVR, F_IPREC, F_IEOC_PERCENTAGE, /* CTRL5 reg fields*/
72
73 F_IAICR_SEL, F_ICHRG, F_VPREC, /* CTRL6 reg fields */
74
75 F_BATD_EN, F_CHG_EN, F_VMREG, /* CTRL7 reg fields */
76
77 F_TSDI, F_VINOVPI, F_BATAB, /* IRQ1 reg fields */
78
79 F_CHRVPI, F_CHBATOVI, F_CHTERMI, F_CHRCHGI, F_CH32MI, F_CHTREGI,
80 F_CHMIVRI, /* IRQ2 reg fields */
81
82 F_BSTBUSOVI, F_BSTOLI, F_BSTLOWVI, F_BST32SI, /* IRQ3 reg fields */
83
84 F_TSDM, F_VINOVPIM, F_BATABM, /* MASK1 reg fields */
85
86 F_CHRVPIM, F_CHBATOVIM, F_CHTERMIM, F_CHRCHGIM, F_CH32MIM, F_CHTREGIM,
87 F_CHMIVRIM, /* MASK2 reg fields */
88
89 F_BSTVINOVIM, F_BSTOLIM, F_BSTLOWVIM, F_BST32SIM, /* MASK3 reg fields */
90
91 F_MAX_FIELDS
92 };
93
94 static const struct reg_field rt9455_reg_fields[] = {
95 [F_STAT] = REG_FIELD(RT9455_REG_CTRL1, 4, 5),
96 [F_BOOST] = REG_FIELD(RT9455_REG_CTRL1, 3, 3),
97 [F_PWR_RDY] = REG_FIELD(RT9455_REG_CTRL1, 2, 2),
98 [F_OTG_PIN_POLARITY] = REG_FIELD(RT9455_REG_CTRL1, 1, 1),
99
100 [F_IAICR] = REG_FIELD(RT9455_REG_CTRL2, 6, 7),
101 [F_TE_SHDN_EN] = REG_FIELD(RT9455_REG_CTRL2, 5, 5),
102 [F_HIGHER_OCP] = REG_FIELD(RT9455_REG_CTRL2, 4, 4),
103 [F_TE] = REG_FIELD(RT9455_REG_CTRL2, 3, 3),
104 [F_IAICR_INT] = REG_FIELD(RT9455_REG_CTRL2, 2, 2),
105 [F_HIZ] = REG_FIELD(RT9455_REG_CTRL2, 1, 1),
106 [F_OPA_MODE] = REG_FIELD(RT9455_REG_CTRL2, 0, 0),
107
108 [F_VOREG] = REG_FIELD(RT9455_REG_CTRL3, 2, 7),
109 [F_OTG_PL] = REG_FIELD(RT9455_REG_CTRL3, 1, 1),
110 [F_OTG_EN] = REG_FIELD(RT9455_REG_CTRL3, 0, 0),
111
112 [F_VENDOR_ID] = REG_FIELD(RT9455_REG_DEV_ID, 4, 7),
113 [F_CHIP_REV] = REG_FIELD(RT9455_REG_DEV_ID, 0, 3),
114
115 [F_RST] = REG_FIELD(RT9455_REG_CTRL4, 7, 7),
116
117 [F_TMR_EN] = REG_FIELD(RT9455_REG_CTRL5, 7, 7),
118 [F_MIVR] = REG_FIELD(RT9455_REG_CTRL5, 4, 5),
119 [F_IPREC] = REG_FIELD(RT9455_REG_CTRL5, 2, 3),
120 [F_IEOC_PERCENTAGE] = REG_FIELD(RT9455_REG_CTRL5, 0, 1),
121
122 [F_IAICR_SEL] = REG_FIELD(RT9455_REG_CTRL6, 7, 7),
123 [F_ICHRG] = REG_FIELD(RT9455_REG_CTRL6, 4, 6),
124 [F_VPREC] = REG_FIELD(RT9455_REG_CTRL6, 0, 2),
125
126 [F_BATD_EN] = REG_FIELD(RT9455_REG_CTRL7, 6, 6),
127 [F_CHG_EN] = REG_FIELD(RT9455_REG_CTRL7, 4, 4),
128 [F_VMREG] = REG_FIELD(RT9455_REG_CTRL7, 0, 3),
129
130 [F_TSDI] = REG_FIELD(RT9455_REG_IRQ1, 7, 7),
131 [F_VINOVPI] = REG_FIELD(RT9455_REG_IRQ1, 6, 6),
132 [F_BATAB] = REG_FIELD(RT9455_REG_IRQ1, 0, 0),
133
134 [F_CHRVPI] = REG_FIELD(RT9455_REG_IRQ2, 7, 7),
135 [F_CHBATOVI] = REG_FIELD(RT9455_REG_IRQ2, 5, 5),
136 [F_CHTERMI] = REG_FIELD(RT9455_REG_IRQ2, 4, 4),
137 [F_CHRCHGI] = REG_FIELD(RT9455_REG_IRQ2, 3, 3),
138 [F_CH32MI] = REG_FIELD(RT9455_REG_IRQ2, 2, 2),
139 [F_CHTREGI] = REG_FIELD(RT9455_REG_IRQ2, 1, 1),
140 [F_CHMIVRI] = REG_FIELD(RT9455_REG_IRQ2, 0, 0),
141
142 [F_BSTBUSOVI] = REG_FIELD(RT9455_REG_IRQ3, 7, 7),
143 [F_BSTOLI] = REG_FIELD(RT9455_REG_IRQ3, 6, 6),
144 [F_BSTLOWVI] = REG_FIELD(RT9455_REG_IRQ3, 5, 5),
145 [F_BST32SI] = REG_FIELD(RT9455_REG_IRQ3, 3, 3),
146
147 [F_TSDM] = REG_FIELD(RT9455_REG_MASK1, 7, 7),
148 [F_VINOVPIM] = REG_FIELD(RT9455_REG_MASK1, 6, 6),
149 [F_BATABM] = REG_FIELD(RT9455_REG_MASK1, 0, 0),
150
151 [F_CHRVPIM] = REG_FIELD(RT9455_REG_MASK2, 7, 7),
152 [F_CHBATOVIM] = REG_FIELD(RT9455_REG_MASK2, 5, 5),
153 [F_CHTERMIM] = REG_FIELD(RT9455_REG_MASK2, 4, 4),
154 [F_CHRCHGIM] = REG_FIELD(RT9455_REG_MASK2, 3, 3),
155 [F_CH32MIM] = REG_FIELD(RT9455_REG_MASK2, 2, 2),
156 [F_CHTREGIM] = REG_FIELD(RT9455_REG_MASK2, 1, 1),
157 [F_CHMIVRIM] = REG_FIELD(RT9455_REG_MASK2, 0, 0),
158
159 [F_BSTVINOVIM] = REG_FIELD(RT9455_REG_MASK3, 7, 7),
160 [F_BSTOLIM] = REG_FIELD(RT9455_REG_MASK3, 6, 6),
161 [F_BSTLOWVIM] = REG_FIELD(RT9455_REG_MASK3, 5, 5),
162 [F_BST32SIM] = REG_FIELD(RT9455_REG_MASK3, 3, 3),
163 };
164
165 #define GET_MASK(fid) (BIT(rt9455_reg_fields[fid].msb + 1) - \
166 BIT(rt9455_reg_fields[fid].lsb))
167
168 /*
169 * Each array initialised below shows the possible real-world values for a
170 * group of bits belonging to RT9455 registers. The arrays are sorted in
171 * ascending order. The index of each real-world value represents the value
172 * that is encoded in the group of bits belonging to RT9455 registers.
173 */
174 /* REG06[6:4] (ICHRG) in uAh */
175 static const int rt9455_ichrg_values[] = {
176 500000, 650000, 800000, 950000, 1100000, 1250000, 1400000, 1550000
177 };
178
179 /*
180 * When the charger is in charge mode, REG02[7:2] represent battery regulation
181 * voltage.
182 */
183 /* REG02[7:2] (VOREG) in uV */
184 static const int rt9455_voreg_values[] = {
185 3500000, 3520000, 3540000, 3560000, 3580000, 3600000, 3620000, 3640000,
186 3660000, 3680000, 3700000, 3720000, 3740000, 3760000, 3780000, 3800000,
187 3820000, 3840000, 3860000, 3880000, 3900000, 3920000, 3940000, 3960000,
188 3980000, 4000000, 4020000, 4040000, 4060000, 4080000, 4100000, 4120000,
189 4140000, 4160000, 4180000, 4200000, 4220000, 4240000, 4260000, 4280000,
190 4300000, 4330000, 4350000, 4370000, 4390000, 4410000, 4430000, 4450000,
191 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000,
192 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000, 4450000
193 };
194
195 #if IS_ENABLED(CONFIG_USB_PHY)
196 /*
197 * When the charger is in boost mode, REG02[7:2] represent boost output
198 * voltage.
199 */
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,
210 };
211 #endif
212
213 /* REG07[3:0] (VMREG) in uV */
214 static const int rt9455_vmreg_values[] = {
215 4200000, 4220000, 4240000, 4260000, 4280000, 4300000, 4320000, 4340000,
216 4360000, 4380000, 4400000, 4430000, 4450000, 4450000, 4450000, 4450000
217 };
218
219 /* REG05[5:4] (IEOC_PERCENTAGE) */
220 static const int rt9455_ieoc_percentage_values[] = {
221 10, 30, 20, 30
222 };
223
224 /* REG05[1:0] (MIVR) in uV */
225 static const int rt9455_mivr_values[] = {
226 4000000, 4250000, 4500000, 5000000
227 };
228
229 /* REG05[1:0] (IAICR) in uA */
230 static const int rt9455_iaicr_values[] = {
231 100000, 500000, 1000000, 2000000
232 };
233
234 struct rt9455_info {
235 struct i2c_client *client;
236 struct regmap *regmap;
237 struct regmap_field *regmap_fields[F_MAX_FIELDS];
238 struct power_supply *charger;
239 #if IS_ENABLED(CONFIG_USB_PHY)
240 struct usb_phy *usb_phy;
241 struct notifier_block nb;
242 #endif
243 struct delayed_work pwr_rdy_work;
244 struct delayed_work max_charging_time_work;
245 struct delayed_work batt_presence_work;
246 u32 voreg;
247 u32 boost_voltage;
248 };
249
250 /*
251 * Iterate through each element of the 'tbl' array until an element whose value
252 * is greater than v is found. Return the index of the respective element,
253 * or the index of the last element in the array, if no such element is found.
254 */
rt9455_find_idx(const int tbl[],int tbl_size,int v)255 static unsigned int rt9455_find_idx(const int tbl[], int tbl_size, int v)
256 {
257 int i;
258
259 /*
260 * No need to iterate until the last index in the table because
261 * if no element greater than v is found in the table,
262 * or if only the last element is greater than v,
263 * function returns the index of the last element.
264 */
265 for (i = 0; i < tbl_size - 1; i++)
266 if (v <= tbl[i])
267 return i;
268
269 return (tbl_size - 1);
270 }
271
rt9455_get_field_val(struct rt9455_info * info,enum rt9455_fields field,const int tbl[],int tbl_size,int * val)272 static int rt9455_get_field_val(struct rt9455_info *info,
273 enum rt9455_fields field,
274 const int tbl[], int tbl_size, int *val)
275 {
276 unsigned int v;
277 int ret;
278
279 ret = regmap_field_read(info->regmap_fields[field], &v);
280 if (ret)
281 return ret;
282
283 v = (v >= tbl_size) ? (tbl_size - 1) : v;
284 *val = tbl[v];
285
286 return 0;
287 }
288
rt9455_set_field_val(struct rt9455_info * info,enum rt9455_fields field,const int tbl[],int tbl_size,int val)289 static int rt9455_set_field_val(struct rt9455_info *info,
290 enum rt9455_fields field,
291 const int tbl[], int tbl_size, int val)
292 {
293 unsigned int idx = rt9455_find_idx(tbl, tbl_size, val);
294
295 return regmap_field_write(info->regmap_fields[field], idx);
296 }
297
rt9455_register_reset(struct rt9455_info * info)298 static int rt9455_register_reset(struct rt9455_info *info)
299 {
300 struct device *dev = &info->client->dev;
301 unsigned int v;
302 int ret, limit = 100;
303
304 ret = regmap_field_write(info->regmap_fields[F_RST], 0x01);
305 if (ret) {
306 dev_err(dev, "Failed to set RST bit\n");
307 return ret;
308 }
309
310 /*
311 * To make sure that reset operation has finished, loop until RST bit
312 * is set to 0.
313 */
314 do {
315 ret = regmap_field_read(info->regmap_fields[F_RST], &v);
316 if (ret) {
317 dev_err(dev, "Failed to read RST bit\n");
318 return ret;
319 }
320
321 if (!v)
322 break;
323
324 usleep_range(10, 100);
325 } while (--limit);
326
327 if (!limit)
328 return -EIO;
329
330 return 0;
331 }
332
333 /* Charger power supply property routines */
334 static enum power_supply_property rt9455_charger_properties[] = {
335 POWER_SUPPLY_PROP_STATUS,
336 POWER_SUPPLY_PROP_HEALTH,
337 POWER_SUPPLY_PROP_PRESENT,
338 POWER_SUPPLY_PROP_ONLINE,
339 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
340 POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
341 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
342 POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
343 POWER_SUPPLY_PROP_SCOPE,
344 POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
345 POWER_SUPPLY_PROP_MODEL_NAME,
346 POWER_SUPPLY_PROP_MANUFACTURER,
347 };
348
349 static char *rt9455_charger_supplied_to[] = {
350 "main-battery",
351 };
352
rt9455_charger_get_status(struct rt9455_info * info,union power_supply_propval * val)353 static int rt9455_charger_get_status(struct rt9455_info *info,
354 union power_supply_propval *val)
355 {
356 unsigned int v, pwr_rdy;
357 int ret;
358
359 ret = regmap_field_read(info->regmap_fields[F_PWR_RDY],
360 &pwr_rdy);
361 if (ret) {
362 dev_err(&info->client->dev, "Failed to read PWR_RDY bit\n");
363 return ret;
364 }
365
366 /*
367 * If PWR_RDY bit is unset, the battery is discharging. Otherwise,
368 * STAT bits value must be checked.
369 */
370 if (!pwr_rdy) {
371 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
372 return 0;
373 }
374
375 ret = regmap_field_read(info->regmap_fields[F_STAT], &v);
376 if (ret) {
377 dev_err(&info->client->dev, "Failed to read STAT bits\n");
378 return ret;
379 }
380
381 switch (v) {
382 case 0:
383 /*
384 * If PWR_RDY bit is set, but STAT bits value is 0, the charger
385 * may be in one of the following cases:
386 * 1. CHG_EN bit is 0.
387 * 2. CHG_EN bit is 1 but the battery is not connected.
388 * In any of these cases, POWER_SUPPLY_STATUS_NOT_CHARGING is
389 * returned.
390 */
391 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
392 return 0;
393 case 1:
394 val->intval = POWER_SUPPLY_STATUS_CHARGING;
395 return 0;
396 case 2:
397 val->intval = POWER_SUPPLY_STATUS_FULL;
398 return 0;
399 default:
400 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
401 return 0;
402 }
403 }
404
rt9455_charger_get_health(struct rt9455_info * info,union power_supply_propval * val)405 static int rt9455_charger_get_health(struct rt9455_info *info,
406 union power_supply_propval *val)
407 {
408 struct device *dev = &info->client->dev;
409 unsigned int v;
410 int ret;
411
412 val->intval = POWER_SUPPLY_HEALTH_GOOD;
413
414 ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &v);
415 if (ret) {
416 dev_err(dev, "Failed to read IRQ1 register\n");
417 return ret;
418 }
419
420 if (v & GET_MASK(F_TSDI)) {
421 val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
422 return 0;
423 }
424 if (v & GET_MASK(F_VINOVPI)) {
425 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
426 return 0;
427 }
428 if (v & GET_MASK(F_BATAB)) {
429 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
430 return 0;
431 }
432
433 ret = regmap_read(info->regmap, RT9455_REG_IRQ2, &v);
434 if (ret) {
435 dev_err(dev, "Failed to read IRQ2 register\n");
436 return ret;
437 }
438
439 if (v & GET_MASK(F_CHBATOVI)) {
440 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
441 return 0;
442 }
443 if (v & GET_MASK(F_CH32MI)) {
444 val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
445 return 0;
446 }
447
448 ret = regmap_read(info->regmap, RT9455_REG_IRQ3, &v);
449 if (ret) {
450 dev_err(dev, "Failed to read IRQ3 register\n");
451 return ret;
452 }
453
454 if (v & GET_MASK(F_BSTBUSOVI)) {
455 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
456 return 0;
457 }
458 if (v & GET_MASK(F_BSTOLI)) {
459 val->intval = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
460 return 0;
461 }
462 if (v & GET_MASK(F_BSTLOWVI)) {
463 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
464 return 0;
465 }
466 if (v & GET_MASK(F_BST32SI)) {
467 val->intval = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
468 return 0;
469 }
470
471 ret = regmap_field_read(info->regmap_fields[F_STAT], &v);
472 if (ret) {
473 dev_err(dev, "Failed to read STAT bits\n");
474 return ret;
475 }
476
477 if (v == RT9455_FAULT) {
478 val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
479 return 0;
480 }
481
482 return 0;
483 }
484
rt9455_charger_get_battery_presence(struct rt9455_info * info,union power_supply_propval * val)485 static int rt9455_charger_get_battery_presence(struct rt9455_info *info,
486 union power_supply_propval *val)
487 {
488 unsigned int v;
489 int ret;
490
491 ret = regmap_field_read(info->regmap_fields[F_BATAB], &v);
492 if (ret) {
493 dev_err(&info->client->dev, "Failed to read BATAB bit\n");
494 return ret;
495 }
496
497 /*
498 * Since BATAB is 1 when battery is NOT present and 0 otherwise,
499 * !BATAB is returned.
500 */
501 val->intval = !v;
502
503 return 0;
504 }
505
rt9455_charger_get_online(struct rt9455_info * info,union power_supply_propval * val)506 static int rt9455_charger_get_online(struct rt9455_info *info,
507 union power_supply_propval *val)
508 {
509 unsigned int v;
510 int ret;
511
512 ret = regmap_field_read(info->regmap_fields[F_PWR_RDY], &v);
513 if (ret) {
514 dev_err(&info->client->dev, "Failed to read PWR_RDY bit\n");
515 return ret;
516 }
517
518 val->intval = (int)v;
519
520 return 0;
521 }
522
rt9455_charger_get_current(struct rt9455_info * info,union power_supply_propval * val)523 static int rt9455_charger_get_current(struct rt9455_info *info,
524 union power_supply_propval *val)
525 {
526 int curr;
527 int ret;
528
529 ret = rt9455_get_field_val(info, F_ICHRG,
530 rt9455_ichrg_values,
531 ARRAY_SIZE(rt9455_ichrg_values),
532 &curr);
533 if (ret) {
534 dev_err(&info->client->dev, "Failed to read ICHRG value\n");
535 return ret;
536 }
537
538 val->intval = curr;
539
540 return 0;
541 }
542
rt9455_charger_get_current_max(struct rt9455_info * info,union power_supply_propval * val)543 static int rt9455_charger_get_current_max(struct rt9455_info *info,
544 union power_supply_propval *val)
545 {
546 int idx = ARRAY_SIZE(rt9455_ichrg_values) - 1;
547
548 val->intval = rt9455_ichrg_values[idx];
549
550 return 0;
551 }
552
rt9455_charger_get_voltage(struct rt9455_info * info,union power_supply_propval * val)553 static int rt9455_charger_get_voltage(struct rt9455_info *info,
554 union power_supply_propval *val)
555 {
556 int voltage;
557 int ret;
558
559 ret = rt9455_get_field_val(info, F_VOREG,
560 rt9455_voreg_values,
561 ARRAY_SIZE(rt9455_voreg_values),
562 &voltage);
563 if (ret) {
564 dev_err(&info->client->dev, "Failed to read VOREG value\n");
565 return ret;
566 }
567
568 val->intval = voltage;
569
570 return 0;
571 }
572
rt9455_charger_get_voltage_max(struct rt9455_info * info,union power_supply_propval * val)573 static int rt9455_charger_get_voltage_max(struct rt9455_info *info,
574 union power_supply_propval *val)
575 {
576 int idx = ARRAY_SIZE(rt9455_vmreg_values) - 1;
577
578 val->intval = rt9455_vmreg_values[idx];
579
580 return 0;
581 }
582
rt9455_charger_get_term_current(struct rt9455_info * info,union power_supply_propval * val)583 static int rt9455_charger_get_term_current(struct rt9455_info *info,
584 union power_supply_propval *val)
585 {
586 struct device *dev = &info->client->dev;
587 int ichrg, ieoc_percentage, ret;
588
589 ret = rt9455_get_field_val(info, F_ICHRG,
590 rt9455_ichrg_values,
591 ARRAY_SIZE(rt9455_ichrg_values),
592 &ichrg);
593 if (ret) {
594 dev_err(dev, "Failed to read ICHRG value\n");
595 return ret;
596 }
597
598 ret = rt9455_get_field_val(info, F_IEOC_PERCENTAGE,
599 rt9455_ieoc_percentage_values,
600 ARRAY_SIZE(rt9455_ieoc_percentage_values),
601 &ieoc_percentage);
602 if (ret) {
603 dev_err(dev, "Failed to read IEOC value\n");
604 return ret;
605 }
606
607 val->intval = ichrg * ieoc_percentage / 100;
608
609 return 0;
610 }
611
rt9455_charger_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)612 static int rt9455_charger_get_property(struct power_supply *psy,
613 enum power_supply_property psp,
614 union power_supply_propval *val)
615 {
616 struct rt9455_info *info = power_supply_get_drvdata(psy);
617
618 switch (psp) {
619 case POWER_SUPPLY_PROP_STATUS:
620 return rt9455_charger_get_status(info, val);
621 case POWER_SUPPLY_PROP_HEALTH:
622 return rt9455_charger_get_health(info, val);
623 case POWER_SUPPLY_PROP_PRESENT:
624 return rt9455_charger_get_battery_presence(info, val);
625 case POWER_SUPPLY_PROP_ONLINE:
626 return rt9455_charger_get_online(info, val);
627 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
628 return rt9455_charger_get_current(info, val);
629 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
630 return rt9455_charger_get_current_max(info, val);
631 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
632 return rt9455_charger_get_voltage(info, val);
633 case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
634 return rt9455_charger_get_voltage_max(info, val);
635 case POWER_SUPPLY_PROP_SCOPE:
636 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
637 return 0;
638 case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
639 return rt9455_charger_get_term_current(info, val);
640 case POWER_SUPPLY_PROP_MODEL_NAME:
641 val->strval = RT9455_MODEL_NAME;
642 return 0;
643 case POWER_SUPPLY_PROP_MANUFACTURER:
644 val->strval = RT9455_MANUFACTURER;
645 return 0;
646 default:
647 return -ENODATA;
648 }
649 }
650
rt9455_hw_init(struct rt9455_info * info,u32 ichrg,u32 ieoc_percentage,u32 mivr,u32 iaicr)651 static int rt9455_hw_init(struct rt9455_info *info, u32 ichrg,
652 u32 ieoc_percentage,
653 u32 mivr, u32 iaicr)
654 {
655 struct device *dev = &info->client->dev;
656 int idx, ret;
657
658 ret = rt9455_register_reset(info);
659 if (ret) {
660 dev_err(dev, "Power On Reset failed\n");
661 return ret;
662 }
663
664 /* Set TE bit in order to enable end of charge detection */
665 ret = regmap_field_write(info->regmap_fields[F_TE], 1);
666 if (ret) {
667 dev_err(dev, "Failed to set TE bit\n");
668 return ret;
669 }
670
671 /* Set TE_SHDN_EN bit in order to enable end of charge detection */
672 ret = regmap_field_write(info->regmap_fields[F_TE_SHDN_EN], 1);
673 if (ret) {
674 dev_err(dev, "Failed to set TE_SHDN_EN bit\n");
675 return ret;
676 }
677
678 /*
679 * Set BATD_EN bit in order to enable battery detection
680 * when charging is done
681 */
682 ret = regmap_field_write(info->regmap_fields[F_BATD_EN], 1);
683 if (ret) {
684 dev_err(dev, "Failed to set BATD_EN bit\n");
685 return ret;
686 }
687
688 /*
689 * Disable Safety Timer. In charge mode, this timer terminates charging
690 * if no read or write via I2C is done within 32 minutes. This timer
691 * avoids overcharging the baterry when the OS is not loaded and the
692 * charger is connected to a power source.
693 * In boost mode, this timer triggers BST32SI interrupt if no read or
694 * write via I2C is done within 32 seconds.
695 * When the OS is loaded and the charger driver is inserted, it is used
696 * delayed_work, named max_charging_time_work, to avoid overcharging
697 * the battery.
698 */
699 ret = regmap_field_write(info->regmap_fields[F_TMR_EN], 0x00);
700 if (ret) {
701 dev_err(dev, "Failed to disable Safety Timer\n");
702 return ret;
703 }
704
705 /* Set ICHRG to value retrieved from device-specific data */
706 ret = rt9455_set_field_val(info, F_ICHRG,
707 rt9455_ichrg_values,
708 ARRAY_SIZE(rt9455_ichrg_values), ichrg);
709 if (ret) {
710 dev_err(dev, "Failed to set ICHRG value\n");
711 return ret;
712 }
713
714 /* Set IEOC Percentage to value retrieved from device-specific data */
715 ret = rt9455_set_field_val(info, F_IEOC_PERCENTAGE,
716 rt9455_ieoc_percentage_values,
717 ARRAY_SIZE(rt9455_ieoc_percentage_values),
718 ieoc_percentage);
719 if (ret) {
720 dev_err(dev, "Failed to set IEOC Percentage value\n");
721 return ret;
722 }
723
724 /* Set VOREG to value retrieved from device-specific data */
725 ret = rt9455_set_field_val(info, F_VOREG,
726 rt9455_voreg_values,
727 ARRAY_SIZE(rt9455_voreg_values),
728 info->voreg);
729 if (ret) {
730 dev_err(dev, "Failed to set VOREG value\n");
731 return ret;
732 }
733
734 /* Set VMREG value to maximum (4.45V). */
735 idx = ARRAY_SIZE(rt9455_vmreg_values) - 1;
736 ret = rt9455_set_field_val(info, F_VMREG,
737 rt9455_vmreg_values,
738 ARRAY_SIZE(rt9455_vmreg_values),
739 rt9455_vmreg_values[idx]);
740 if (ret) {
741 dev_err(dev, "Failed to set VMREG value\n");
742 return ret;
743 }
744
745 /*
746 * Set MIVR to value retrieved from device-specific data.
747 * If no value is specified, default value for MIVR is 4.5V.
748 */
749 if (mivr == -1)
750 mivr = 4500000;
751
752 ret = rt9455_set_field_val(info, F_MIVR,
753 rt9455_mivr_values,
754 ARRAY_SIZE(rt9455_mivr_values), mivr);
755 if (ret) {
756 dev_err(dev, "Failed to set MIVR value\n");
757 return ret;
758 }
759
760 /*
761 * Set IAICR to value retrieved from device-specific data.
762 * If no value is specified, default value for IAICR is 500 mA.
763 */
764 if (iaicr == -1)
765 iaicr = 500000;
766
767 ret = rt9455_set_field_val(info, F_IAICR,
768 rt9455_iaicr_values,
769 ARRAY_SIZE(rt9455_iaicr_values), iaicr);
770 if (ret) {
771 dev_err(dev, "Failed to set IAICR value\n");
772 return ret;
773 }
774
775 /*
776 * Set IAICR_INT bit so that IAICR value is determined by IAICR bits
777 * and not by OTG pin.
778 */
779 ret = regmap_field_write(info->regmap_fields[F_IAICR_INT], 0x01);
780 if (ret) {
781 dev_err(dev, "Failed to set IAICR_INT bit\n");
782 return ret;
783 }
784
785 /*
786 * Disable CHMIVRI interrupt. Because the driver sets MIVR value,
787 * CHMIVRI is triggered, but there is no action to be taken by the
788 * driver when CHMIVRI is triggered.
789 */
790 ret = regmap_field_write(info->regmap_fields[F_CHMIVRIM], 0x01);
791 if (ret) {
792 dev_err(dev, "Failed to mask CHMIVRI interrupt\n");
793 return ret;
794 }
795
796 return 0;
797 }
798
799 #if IS_ENABLED(CONFIG_USB_PHY)
800 /*
801 * Before setting the charger into boost mode, boost output voltage is
802 * set. This is needed because boost output voltage may differ from battery
803 * regulation voltage. F_VOREG bits represent either battery regulation voltage
804 * or boost output voltage, depending on the mode the charger is. Both battery
805 * regulation voltage and boost output voltage are read from DT/ACPI during
806 * probe.
807 */
rt9455_set_boost_voltage_before_boost_mode(struct rt9455_info * info)808 static int rt9455_set_boost_voltage_before_boost_mode(struct rt9455_info *info)
809 {
810 struct device *dev = &info->client->dev;
811 int ret;
812
813 ret = rt9455_set_field_val(info, F_VOREG,
814 rt9455_boost_voltage_values,
815 ARRAY_SIZE(rt9455_boost_voltage_values),
816 info->boost_voltage);
817 if (ret) {
818 dev_err(dev, "Failed to set boost output voltage value\n");
819 return ret;
820 }
821
822 return 0;
823 }
824 #endif
825
826 /*
827 * Before setting the charger into charge mode, battery regulation voltage is
828 * set. This is needed because boost output voltage may differ from battery
829 * regulation voltage. F_VOREG bits represent either battery regulation voltage
830 * or boost output voltage, depending on the mode the charger is. Both battery
831 * regulation voltage and boost output voltage are read from DT/ACPI during
832 * probe.
833 */
rt9455_set_voreg_before_charge_mode(struct rt9455_info * info)834 static int rt9455_set_voreg_before_charge_mode(struct rt9455_info *info)
835 {
836 struct device *dev = &info->client->dev;
837 int ret;
838
839 ret = rt9455_set_field_val(info, F_VOREG,
840 rt9455_voreg_values,
841 ARRAY_SIZE(rt9455_voreg_values),
842 info->voreg);
843 if (ret) {
844 dev_err(dev, "Failed to set VOREG value\n");
845 return ret;
846 }
847
848 return 0;
849 }
850
rt9455_irq_handler_check_irq1_register(struct rt9455_info * info,bool * _is_battery_absent,bool * _alert_userspace)851 static int rt9455_irq_handler_check_irq1_register(struct rt9455_info *info,
852 bool *_is_battery_absent,
853 bool *_alert_userspace)
854 {
855 unsigned int irq1, mask1, mask2;
856 struct device *dev = &info->client->dev;
857 bool is_battery_absent = false;
858 bool alert_userspace = false;
859 int ret;
860
861 ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &irq1);
862 if (ret) {
863 dev_err(dev, "Failed to read IRQ1 register\n");
864 return ret;
865 }
866
867 ret = regmap_read(info->regmap, RT9455_REG_MASK1, &mask1);
868 if (ret) {
869 dev_err(dev, "Failed to read MASK1 register\n");
870 return ret;
871 }
872
873 if (irq1 & GET_MASK(F_TSDI)) {
874 dev_err(dev, "Thermal shutdown fault occurred\n");
875 alert_userspace = true;
876 }
877
878 if (irq1 & GET_MASK(F_VINOVPI)) {
879 dev_err(dev, "Overvoltage input occurred\n");
880 alert_userspace = true;
881 }
882
883 if (irq1 & GET_MASK(F_BATAB)) {
884 dev_err(dev, "Battery absence occurred\n");
885 is_battery_absent = true;
886 alert_userspace = true;
887
888 if ((mask1 & GET_MASK(F_BATABM)) == 0) {
889 ret = regmap_field_write(info->regmap_fields[F_BATABM],
890 0x01);
891 if (ret) {
892 dev_err(dev, "Failed to mask BATAB interrupt\n");
893 return ret;
894 }
895 }
896
897 ret = regmap_read(info->regmap, RT9455_REG_MASK2, &mask2);
898 if (ret) {
899 dev_err(dev, "Failed to read MASK2 register\n");
900 return ret;
901 }
902
903 if (mask2 & GET_MASK(F_CHTERMIM)) {
904 ret = regmap_field_write(
905 info->regmap_fields[F_CHTERMIM], 0x00);
906 if (ret) {
907 dev_err(dev, "Failed to unmask CHTERMI interrupt\n");
908 return ret;
909 }
910 }
911
912 if (mask2 & GET_MASK(F_CHRCHGIM)) {
913 ret = regmap_field_write(
914 info->regmap_fields[F_CHRCHGIM], 0x00);
915 if (ret) {
916 dev_err(dev, "Failed to unmask CHRCHGI interrupt\n");
917 return ret;
918 }
919 }
920
921 /*
922 * When the battery is absent, max_charging_time_work is
923 * cancelled, since no charging is done.
924 */
925 cancel_delayed_work_sync(&info->max_charging_time_work);
926 /*
927 * Since no interrupt is triggered when the battery is
928 * reconnected, max_charging_time_work is not rescheduled.
929 * Therefore, batt_presence_work is scheduled to check whether
930 * the battery is still absent or not.
931 */
932 queue_delayed_work(system_power_efficient_wq,
933 &info->batt_presence_work,
934 RT9455_BATT_PRESENCE_DELAY * HZ);
935 }
936
937 *_is_battery_absent = is_battery_absent;
938
939 if (alert_userspace)
940 *_alert_userspace = alert_userspace;
941
942 return 0;
943 }
944
rt9455_irq_handler_check_irq2_register(struct rt9455_info * info,bool is_battery_absent,bool * _alert_userspace)945 static int rt9455_irq_handler_check_irq2_register(struct rt9455_info *info,
946 bool is_battery_absent,
947 bool *_alert_userspace)
948 {
949 unsigned int irq2, mask2;
950 struct device *dev = &info->client->dev;
951 bool alert_userspace = false;
952 int ret;
953
954 ret = regmap_read(info->regmap, RT9455_REG_IRQ2, &irq2);
955 if (ret) {
956 dev_err(dev, "Failed to read IRQ2 register\n");
957 return ret;
958 }
959
960 ret = regmap_read(info->regmap, RT9455_REG_MASK2, &mask2);
961 if (ret) {
962 dev_err(dev, "Failed to read MASK2 register\n");
963 return ret;
964 }
965
966 if (irq2 & GET_MASK(F_CHRVPI)) {
967 dev_dbg(dev, "Charger fault occurred\n");
968 /*
969 * CHRVPI bit is set in 2 cases:
970 * 1. when the power source is connected to the charger.
971 * 2. when the power source is disconnected from the charger.
972 * To identify the case, PWR_RDY bit is checked. Because
973 * PWR_RDY bit is set / cleared after CHRVPI interrupt is
974 * triggered, it is used delayed_work to later read PWR_RDY bit.
975 * Also, do not set to true alert_userspace, because there is no
976 * need to notify userspace when CHRVPI interrupt has occurred.
977 * Userspace will be notified after PWR_RDY bit is read.
978 */
979 queue_delayed_work(system_power_efficient_wq,
980 &info->pwr_rdy_work,
981 RT9455_PWR_RDY_DELAY * HZ);
982 }
983 if (irq2 & GET_MASK(F_CHBATOVI)) {
984 dev_err(dev, "Battery OVP occurred\n");
985 alert_userspace = true;
986 }
987 if (irq2 & GET_MASK(F_CHTERMI)) {
988 dev_dbg(dev, "Charge terminated\n");
989 if (!is_battery_absent) {
990 if ((mask2 & GET_MASK(F_CHTERMIM)) == 0) {
991 ret = regmap_field_write(
992 info->regmap_fields[F_CHTERMIM], 0x01);
993 if (ret) {
994 dev_err(dev, "Failed to mask CHTERMI interrupt\n");
995 return ret;
996 }
997 /*
998 * Update MASK2 value, since CHTERMIM bit is
999 * set.
1000 */
1001 mask2 = mask2 | GET_MASK(F_CHTERMIM);
1002 }
1003 cancel_delayed_work_sync(&info->max_charging_time_work);
1004 alert_userspace = true;
1005 }
1006 }
1007 if (irq2 & GET_MASK(F_CHRCHGI)) {
1008 dev_dbg(dev, "Recharge request\n");
1009 ret = regmap_field_write(info->regmap_fields[F_CHG_EN],
1010 RT9455_CHARGE_ENABLE);
1011 if (ret) {
1012 dev_err(dev, "Failed to enable charging\n");
1013 return ret;
1014 }
1015 if (mask2 & GET_MASK(F_CHTERMIM)) {
1016 ret = regmap_field_write(
1017 info->regmap_fields[F_CHTERMIM], 0x00);
1018 if (ret) {
1019 dev_err(dev, "Failed to unmask CHTERMI interrupt\n");
1020 return ret;
1021 }
1022 /* Update MASK2 value, since CHTERMIM bit is cleared. */
1023 mask2 = mask2 & ~GET_MASK(F_CHTERMIM);
1024 }
1025 if (!is_battery_absent) {
1026 /*
1027 * No need to check whether the charger is connected to
1028 * power source when CHRCHGI is received, since CHRCHGI
1029 * is not triggered if the charger is not connected to
1030 * the power source.
1031 */
1032 queue_delayed_work(system_power_efficient_wq,
1033 &info->max_charging_time_work,
1034 RT9455_MAX_CHARGING_TIME * HZ);
1035 alert_userspace = true;
1036 }
1037 }
1038 if (irq2 & GET_MASK(F_CH32MI)) {
1039 dev_err(dev, "Charger fault. 32 mins timeout occurred\n");
1040 alert_userspace = true;
1041 }
1042 if (irq2 & GET_MASK(F_CHTREGI)) {
1043 dev_warn(dev,
1044 "Charger warning. Thermal regulation loop active\n");
1045 alert_userspace = true;
1046 }
1047 if (irq2 & GET_MASK(F_CHMIVRI)) {
1048 dev_dbg(dev,
1049 "Charger warning. Input voltage MIVR loop active\n");
1050 }
1051
1052 if (alert_userspace)
1053 *_alert_userspace = alert_userspace;
1054
1055 return 0;
1056 }
1057
rt9455_irq_handler_check_irq3_register(struct rt9455_info * info,bool * _alert_userspace)1058 static int rt9455_irq_handler_check_irq3_register(struct rt9455_info *info,
1059 bool *_alert_userspace)
1060 {
1061 unsigned int irq3, mask3;
1062 struct device *dev = &info->client->dev;
1063 bool alert_userspace = false;
1064 int ret;
1065
1066 ret = regmap_read(info->regmap, RT9455_REG_IRQ3, &irq3);
1067 if (ret) {
1068 dev_err(dev, "Failed to read IRQ3 register\n");
1069 return ret;
1070 }
1071
1072 ret = regmap_read(info->regmap, RT9455_REG_MASK3, &mask3);
1073 if (ret) {
1074 dev_err(dev, "Failed to read MASK3 register\n");
1075 return ret;
1076 }
1077
1078 if (irq3 & GET_MASK(F_BSTBUSOVI)) {
1079 dev_err(dev, "Boost fault. Overvoltage input occurred\n");
1080 alert_userspace = true;
1081 }
1082 if (irq3 & GET_MASK(F_BSTOLI)) {
1083 dev_err(dev, "Boost fault. Overload\n");
1084 alert_userspace = true;
1085 }
1086 if (irq3 & GET_MASK(F_BSTLOWVI)) {
1087 dev_err(dev, "Boost fault. Battery voltage too low\n");
1088 alert_userspace = true;
1089 }
1090 if (irq3 & GET_MASK(F_BST32SI)) {
1091 dev_err(dev, "Boost fault. 32 seconds timeout occurred.\n");
1092 alert_userspace = true;
1093 }
1094
1095 if (alert_userspace) {
1096 dev_info(dev, "Boost fault occurred, therefore the charger goes into charge mode\n");
1097 ret = rt9455_set_voreg_before_charge_mode(info);
1098 if (ret) {
1099 dev_err(dev, "Failed to set VOREG before entering charge mode\n");
1100 return ret;
1101 }
1102 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1103 RT9455_CHARGE_MODE);
1104 if (ret) {
1105 dev_err(dev, "Failed to set charger in charge mode\n");
1106 return ret;
1107 }
1108 *_alert_userspace = alert_userspace;
1109 }
1110
1111 return 0;
1112 }
1113
rt9455_irq_handler_thread(int irq,void * data)1114 static irqreturn_t rt9455_irq_handler_thread(int irq, void *data)
1115 {
1116 struct rt9455_info *info = data;
1117 struct device *dev;
1118 bool alert_userspace = false;
1119 bool is_battery_absent = false;
1120 unsigned int status;
1121 int ret;
1122
1123 if (!info)
1124 return IRQ_NONE;
1125
1126 dev = &info->client->dev;
1127
1128 if (irq != info->client->irq) {
1129 dev_err(dev, "Interrupt is not for RT9455 charger\n");
1130 return IRQ_NONE;
1131 }
1132
1133 ret = regmap_field_read(info->regmap_fields[F_STAT], &status);
1134 if (ret) {
1135 dev_err(dev, "Failed to read STAT bits\n");
1136 return IRQ_HANDLED;
1137 }
1138 dev_dbg(dev, "Charger status is %d\n", status);
1139
1140 /*
1141 * Each function that processes an IRQ register receives as output
1142 * parameter alert_userspace pointer. alert_userspace is set to true
1143 * in such a function only if an interrupt has occurred in the
1144 * respective interrupt register. This way, it is avoided the following
1145 * case: interrupt occurs only in IRQ1 register,
1146 * rt9455_irq_handler_check_irq1_register() function sets to true
1147 * alert_userspace, but rt9455_irq_handler_check_irq2_register()
1148 * and rt9455_irq_handler_check_irq3_register() functions set to false
1149 * alert_userspace and power_supply_changed() is never called.
1150 */
1151 ret = rt9455_irq_handler_check_irq1_register(info, &is_battery_absent,
1152 &alert_userspace);
1153 if (ret) {
1154 dev_err(dev, "Failed to handle IRQ1 register\n");
1155 return IRQ_HANDLED;
1156 }
1157
1158 ret = rt9455_irq_handler_check_irq2_register(info, is_battery_absent,
1159 &alert_userspace);
1160 if (ret) {
1161 dev_err(dev, "Failed to handle IRQ2 register\n");
1162 return IRQ_HANDLED;
1163 }
1164
1165 ret = rt9455_irq_handler_check_irq3_register(info, &alert_userspace);
1166 if (ret) {
1167 dev_err(dev, "Failed to handle IRQ3 register\n");
1168 return IRQ_HANDLED;
1169 }
1170
1171 if (alert_userspace) {
1172 /*
1173 * Sometimes, an interrupt occurs while rt9455_probe() function
1174 * is executing and power_supply_register() is not yet called.
1175 * Do not call power_supply_changed() in this case.
1176 */
1177 if (info->charger)
1178 power_supply_changed(info->charger);
1179 }
1180
1181 return IRQ_HANDLED;
1182 }
1183
rt9455_discover_charger(struct rt9455_info * info,u32 * ichrg,u32 * ieoc_percentage,u32 * mivr,u32 * iaicr)1184 static int rt9455_discover_charger(struct rt9455_info *info, u32 *ichrg,
1185 u32 *ieoc_percentage,
1186 u32 *mivr, u32 *iaicr)
1187 {
1188 struct device *dev = &info->client->dev;
1189 int ret;
1190
1191 if (!dev->of_node && !ACPI_HANDLE(dev)) {
1192 dev_err(dev, "No support for either device tree or ACPI\n");
1193 return -EINVAL;
1194 }
1195 /*
1196 * ICHRG, IEOC_PERCENTAGE, VOREG and boost output voltage are mandatory
1197 * parameters.
1198 */
1199 ret = device_property_read_u32(dev, "richtek,output-charge-current",
1200 ichrg);
1201 if (ret) {
1202 dev_err(dev, "Error: missing \"output-charge-current\" property\n");
1203 return ret;
1204 }
1205
1206 ret = device_property_read_u32(dev, "richtek,end-of-charge-percentage",
1207 ieoc_percentage);
1208 if (ret) {
1209 dev_err(dev, "Error: missing \"end-of-charge-percentage\" property\n");
1210 return ret;
1211 }
1212
1213 ret = device_property_read_u32(dev,
1214 "richtek,battery-regulation-voltage",
1215 &info->voreg);
1216 if (ret) {
1217 dev_err(dev, "Error: missing \"battery-regulation-voltage\" property\n");
1218 return ret;
1219 }
1220
1221 ret = device_property_read_u32(dev, "richtek,boost-output-voltage",
1222 &info->boost_voltage);
1223 if (ret) {
1224 dev_err(dev, "Error: missing \"boost-output-voltage\" property\n");
1225 return ret;
1226 }
1227
1228 /*
1229 * MIVR and IAICR are optional parameters. Do not return error if one of
1230 * them is not present in ACPI table or device tree specification.
1231 */
1232 device_property_read_u32(dev, "richtek,min-input-voltage-regulation",
1233 mivr);
1234 device_property_read_u32(dev, "richtek,avg-input-current-regulation",
1235 iaicr);
1236
1237 return 0;
1238 }
1239
1240 #if IS_ENABLED(CONFIG_USB_PHY)
rt9455_usb_event_none(struct rt9455_info * info,u8 opa_mode,u8 iaicr)1241 static int rt9455_usb_event_none(struct rt9455_info *info,
1242 u8 opa_mode, u8 iaicr)
1243 {
1244 struct device *dev = &info->client->dev;
1245 int ret;
1246
1247 if (opa_mode == RT9455_BOOST_MODE) {
1248 ret = rt9455_set_voreg_before_charge_mode(info);
1249 if (ret) {
1250 dev_err(dev, "Failed to set VOREG before entering charge mode\n");
1251 return ret;
1252 }
1253 /*
1254 * If the charger is in boost mode, and it has received
1255 * USB_EVENT_NONE, this means the consumer device powered by the
1256 * charger is not connected anymore.
1257 * In this case, the charger goes into charge mode.
1258 */
1259 dev_dbg(dev, "USB_EVENT_NONE received, therefore the charger goes into charge mode\n");
1260 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1261 RT9455_CHARGE_MODE);
1262 if (ret) {
1263 dev_err(dev, "Failed to set charger in charge mode\n");
1264 return NOTIFY_DONE;
1265 }
1266 }
1267
1268 dev_dbg(dev, "USB_EVENT_NONE received, therefore IAICR is set to its minimum value\n");
1269 if (iaicr != RT9455_IAICR_100MA) {
1270 ret = regmap_field_write(info->regmap_fields[F_IAICR],
1271 RT9455_IAICR_100MA);
1272 if (ret) {
1273 dev_err(dev, "Failed to set IAICR value\n");
1274 return NOTIFY_DONE;
1275 }
1276 }
1277
1278 return NOTIFY_OK;
1279 }
1280
rt9455_usb_event_vbus(struct rt9455_info * info,u8 opa_mode,u8 iaicr)1281 static int rt9455_usb_event_vbus(struct rt9455_info *info,
1282 u8 opa_mode, u8 iaicr)
1283 {
1284 struct device *dev = &info->client->dev;
1285 int ret;
1286
1287 if (opa_mode == RT9455_BOOST_MODE) {
1288 ret = rt9455_set_voreg_before_charge_mode(info);
1289 if (ret) {
1290 dev_err(dev, "Failed to set VOREG before entering charge mode\n");
1291 return ret;
1292 }
1293 /*
1294 * If the charger is in boost mode, and it has received
1295 * USB_EVENT_VBUS, this means the consumer device powered by the
1296 * charger is not connected anymore.
1297 * In this case, the charger goes into charge mode.
1298 */
1299 dev_dbg(dev, "USB_EVENT_VBUS received, therefore the charger goes into charge mode\n");
1300 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1301 RT9455_CHARGE_MODE);
1302 if (ret) {
1303 dev_err(dev, "Failed to set charger in charge mode\n");
1304 return NOTIFY_DONE;
1305 }
1306 }
1307
1308 dev_dbg(dev, "USB_EVENT_VBUS received, therefore IAICR is set to 500 mA\n");
1309 if (iaicr != RT9455_IAICR_500MA) {
1310 ret = regmap_field_write(info->regmap_fields[F_IAICR],
1311 RT9455_IAICR_500MA);
1312 if (ret) {
1313 dev_err(dev, "Failed to set IAICR value\n");
1314 return NOTIFY_DONE;
1315 }
1316 }
1317
1318 return NOTIFY_OK;
1319 }
1320
rt9455_usb_event_id(struct rt9455_info * info,u8 opa_mode,u8 iaicr)1321 static int rt9455_usb_event_id(struct rt9455_info *info,
1322 u8 opa_mode, u8 iaicr)
1323 {
1324 struct device *dev = &info->client->dev;
1325 int ret;
1326
1327 if (opa_mode == RT9455_CHARGE_MODE) {
1328 ret = rt9455_set_boost_voltage_before_boost_mode(info);
1329 if (ret) {
1330 dev_err(dev, "Failed to set boost output voltage before entering boost mode\n");
1331 return ret;
1332 }
1333 /*
1334 * If the charger is in charge mode, and it has received
1335 * USB_EVENT_ID, this means a consumer device is connected and
1336 * it should be powered by the charger.
1337 * In this case, the charger goes into boost mode.
1338 */
1339 dev_dbg(dev, "USB_EVENT_ID received, therefore the charger goes into boost mode\n");
1340 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1341 RT9455_BOOST_MODE);
1342 if (ret) {
1343 dev_err(dev, "Failed to set charger in boost mode\n");
1344 return NOTIFY_DONE;
1345 }
1346 }
1347
1348 dev_dbg(dev, "USB_EVENT_ID received, therefore IAICR is set to its minimum value\n");
1349 if (iaicr != RT9455_IAICR_100MA) {
1350 ret = regmap_field_write(info->regmap_fields[F_IAICR],
1351 RT9455_IAICR_100MA);
1352 if (ret) {
1353 dev_err(dev, "Failed to set IAICR value\n");
1354 return NOTIFY_DONE;
1355 }
1356 }
1357
1358 return NOTIFY_OK;
1359 }
1360
rt9455_usb_event_charger(struct rt9455_info * info,u8 opa_mode,u8 iaicr)1361 static int rt9455_usb_event_charger(struct rt9455_info *info,
1362 u8 opa_mode, u8 iaicr)
1363 {
1364 struct device *dev = &info->client->dev;
1365 int ret;
1366
1367 if (opa_mode == RT9455_BOOST_MODE) {
1368 ret = rt9455_set_voreg_before_charge_mode(info);
1369 if (ret) {
1370 dev_err(dev, "Failed to set VOREG before entering charge mode\n");
1371 return ret;
1372 }
1373 /*
1374 * If the charger is in boost mode, and it has received
1375 * USB_EVENT_CHARGER, this means the consumer device powered by
1376 * the charger is not connected anymore.
1377 * In this case, the charger goes into charge mode.
1378 */
1379 dev_dbg(dev, "USB_EVENT_CHARGER received, therefore the charger goes into charge mode\n");
1380 ret = regmap_field_write(info->regmap_fields[F_OPA_MODE],
1381 RT9455_CHARGE_MODE);
1382 if (ret) {
1383 dev_err(dev, "Failed to set charger in charge mode\n");
1384 return NOTIFY_DONE;
1385 }
1386 }
1387
1388 dev_dbg(dev, "USB_EVENT_CHARGER received, therefore IAICR is set to no current limit\n");
1389 if (iaicr != RT9455_IAICR_NO_LIMIT) {
1390 ret = regmap_field_write(info->regmap_fields[F_IAICR],
1391 RT9455_IAICR_NO_LIMIT);
1392 if (ret) {
1393 dev_err(dev, "Failed to set IAICR value\n");
1394 return NOTIFY_DONE;
1395 }
1396 }
1397
1398 return NOTIFY_OK;
1399 }
1400
rt9455_usb_event(struct notifier_block * nb,unsigned long event,void * power)1401 static int rt9455_usb_event(struct notifier_block *nb,
1402 unsigned long event, void *power)
1403 {
1404 struct rt9455_info *info = container_of(nb, struct rt9455_info, nb);
1405 struct device *dev = &info->client->dev;
1406 unsigned int opa_mode, iaicr;
1407 int ret;
1408
1409 /*
1410 * Determine whether the charger is in charge mode
1411 * or in boost mode.
1412 */
1413 ret = regmap_field_read(info->regmap_fields[F_OPA_MODE],
1414 &opa_mode);
1415 if (ret) {
1416 dev_err(dev, "Failed to read OPA_MODE value\n");
1417 return NOTIFY_DONE;
1418 }
1419
1420 ret = regmap_field_read(info->regmap_fields[F_IAICR],
1421 &iaicr);
1422 if (ret) {
1423 dev_err(dev, "Failed to read IAICR value\n");
1424 return NOTIFY_DONE;
1425 }
1426
1427 dev_dbg(dev, "Received USB event %lu\n", event);
1428 switch (event) {
1429 case USB_EVENT_NONE:
1430 return rt9455_usb_event_none(info, opa_mode, iaicr);
1431 case USB_EVENT_VBUS:
1432 return rt9455_usb_event_vbus(info, opa_mode, iaicr);
1433 case USB_EVENT_ID:
1434 return rt9455_usb_event_id(info, opa_mode, iaicr);
1435 case USB_EVENT_CHARGER:
1436 return rt9455_usb_event_charger(info, opa_mode, iaicr);
1437 default:
1438 dev_err(dev, "Unknown USB event\n");
1439 }
1440 return NOTIFY_DONE;
1441 }
1442 #endif
1443
rt9455_pwr_rdy_work_callback(struct work_struct * work)1444 static void rt9455_pwr_rdy_work_callback(struct work_struct *work)
1445 {
1446 struct rt9455_info *info = container_of(work, struct rt9455_info,
1447 pwr_rdy_work.work);
1448 struct device *dev = &info->client->dev;
1449 unsigned int pwr_rdy;
1450 int ret;
1451
1452 ret = regmap_field_read(info->regmap_fields[F_PWR_RDY], &pwr_rdy);
1453 if (ret) {
1454 dev_err(dev, "Failed to read PWR_RDY bit\n");
1455 return;
1456 }
1457 switch (pwr_rdy) {
1458 case RT9455_PWR_FAULT:
1459 dev_dbg(dev, "Charger disconnected from power source\n");
1460 cancel_delayed_work_sync(&info->max_charging_time_work);
1461 break;
1462 case RT9455_PWR_GOOD:
1463 dev_dbg(dev, "Charger connected to power source\n");
1464 ret = regmap_field_write(info->regmap_fields[F_CHG_EN],
1465 RT9455_CHARGE_ENABLE);
1466 if (ret) {
1467 dev_err(dev, "Failed to enable charging\n");
1468 return;
1469 }
1470 queue_delayed_work(system_power_efficient_wq,
1471 &info->max_charging_time_work,
1472 RT9455_MAX_CHARGING_TIME * HZ);
1473 break;
1474 }
1475 /*
1476 * Notify userspace that the charger has been either connected to or
1477 * disconnected from the power source.
1478 */
1479 power_supply_changed(info->charger);
1480 }
1481
rt9455_max_charging_time_work_callback(struct work_struct * work)1482 static void rt9455_max_charging_time_work_callback(struct work_struct *work)
1483 {
1484 struct rt9455_info *info = container_of(work, struct rt9455_info,
1485 max_charging_time_work.work);
1486 struct device *dev = &info->client->dev;
1487 int ret;
1488
1489 dev_err(dev, "Battery has been charging for at least 6 hours and is not yet fully charged. Battery is dead, therefore charging is disabled.\n");
1490 ret = regmap_field_write(info->regmap_fields[F_CHG_EN],
1491 RT9455_CHARGE_DISABLE);
1492 if (ret)
1493 dev_err(dev, "Failed to disable charging\n");
1494 }
1495
rt9455_batt_presence_work_callback(struct work_struct * work)1496 static void rt9455_batt_presence_work_callback(struct work_struct *work)
1497 {
1498 struct rt9455_info *info = container_of(work, struct rt9455_info,
1499 batt_presence_work.work);
1500 struct device *dev = &info->client->dev;
1501 unsigned int irq1, mask1;
1502 int ret;
1503
1504 ret = regmap_read(info->regmap, RT9455_REG_IRQ1, &irq1);
1505 if (ret) {
1506 dev_err(dev, "Failed to read IRQ1 register\n");
1507 return;
1508 }
1509
1510 /*
1511 * If the battery is still absent, batt_presence_work is rescheduled.
1512 * Otherwise, max_charging_time is scheduled.
1513 */
1514 if (irq1 & GET_MASK(F_BATAB)) {
1515 queue_delayed_work(system_power_efficient_wq,
1516 &info->batt_presence_work,
1517 RT9455_BATT_PRESENCE_DELAY * HZ);
1518 } else {
1519 queue_delayed_work(system_power_efficient_wq,
1520 &info->max_charging_time_work,
1521 RT9455_MAX_CHARGING_TIME * HZ);
1522
1523 ret = regmap_read(info->regmap, RT9455_REG_MASK1, &mask1);
1524 if (ret) {
1525 dev_err(dev, "Failed to read MASK1 register\n");
1526 return;
1527 }
1528
1529 if (mask1 & GET_MASK(F_BATABM)) {
1530 ret = regmap_field_write(info->regmap_fields[F_BATABM],
1531 0x00);
1532 if (ret)
1533 dev_err(dev, "Failed to unmask BATAB interrupt\n");
1534 }
1535 /*
1536 * Notify userspace that the battery is now connected to the
1537 * charger.
1538 */
1539 power_supply_changed(info->charger);
1540 }
1541 }
1542
1543 static const struct power_supply_desc rt9455_charger_desc = {
1544 .name = RT9455_DRIVER_NAME,
1545 .type = POWER_SUPPLY_TYPE_USB,
1546 .properties = rt9455_charger_properties,
1547 .num_properties = ARRAY_SIZE(rt9455_charger_properties),
1548 .get_property = rt9455_charger_get_property,
1549 };
1550
rt9455_is_writeable_reg(struct device * dev,unsigned int reg)1551 static bool rt9455_is_writeable_reg(struct device *dev, unsigned int reg)
1552 {
1553 switch (reg) {
1554 case RT9455_REG_DEV_ID:
1555 case RT9455_REG_IRQ1:
1556 case RT9455_REG_IRQ2:
1557 case RT9455_REG_IRQ3:
1558 return false;
1559 default:
1560 return true;
1561 }
1562 }
1563
rt9455_is_volatile_reg(struct device * dev,unsigned int reg)1564 static bool rt9455_is_volatile_reg(struct device *dev, unsigned int reg)
1565 {
1566 switch (reg) {
1567 case RT9455_REG_DEV_ID:
1568 case RT9455_REG_CTRL5:
1569 case RT9455_REG_CTRL6:
1570 return false;
1571 default:
1572 return true;
1573 }
1574 }
1575
1576 static const struct regmap_config rt9455_regmap_config = {
1577 .reg_bits = 8,
1578 .val_bits = 8,
1579 .writeable_reg = rt9455_is_writeable_reg,
1580 .volatile_reg = rt9455_is_volatile_reg,
1581 .max_register = RT9455_REG_MASK3,
1582 .cache_type = REGCACHE_RBTREE,
1583 };
1584
rt9455_probe(struct i2c_client * client)1585 static int rt9455_probe(struct i2c_client *client)
1586 {
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 = {};
1591 /*
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.
1595 */
1596 u32 ichrg, ieoc_percentage;
1597 /* Optional device-specific data values. */
1598 u32 mivr = -1, iaicr = -1;
1599 int i, ret;
1600
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;
1604 }
1605 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
1606 if (!info)
1607 return -ENOMEM;
1608
1609 info->client = client;
1610 i2c_set_clientdata(client, info);
1611
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;
1617 }
1618
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]);
1627 }
1628 }
1629
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;
1635 }
1636
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");
1646 /*
1647 * If usb_register_notifier() fails, set notifier_call
1648 * to NULL, to avoid calling usb_unregister_notifier().
1649 */
1650 info->nb.notifier_call = NULL;
1651 }
1652 }
1653 #endif
1654
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);
1660
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;
1673 }
1674
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;
1679 }
1680
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;
1687 }
1688
1689 return 0;
1690
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;
1696 }
1697 #endif
1698 return ret;
1699 }
1700
rt9455_remove(struct i2c_client * client)1701 static void rt9455_remove(struct i2c_client *client)
1702 {
1703 int ret;
1704 struct rt9455_info *info = i2c_get_clientdata(client);
1705
1706 ret = rt9455_register_reset(info);
1707 if (ret)
1708 dev_err(&info->client->dev, "Failed to set charger to its default values\n");
1709
1710 #if IS_ENABLED(CONFIG_USB_PHY)
1711 if (info->nb.notifier_call)
1712 usb_unregister_notifier(info->usb_phy, &info->nb);
1713 #endif
1714
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);
1718 }
1719
1720 static const struct i2c_device_id rt9455_i2c_id_table[] = {
1721 { RT9455_DRIVER_NAME, 0 },
1722 { },
1723 };
1724 MODULE_DEVICE_TABLE(i2c, rt9455_i2c_id_table);
1725
1726 static const struct of_device_id rt9455_of_match[] __maybe_unused = {
1727 { .compatible = "richtek,rt9455", },
1728 { },
1729 };
1730 MODULE_DEVICE_TABLE(of, rt9455_of_match);
1731
1732 #ifdef CONFIG_ACPI
1733 static const struct acpi_device_id rt9455_i2c_acpi_match[] = {
1734 { "RT945500", 0 },
1735 { }
1736 };
1737 MODULE_DEVICE_TABLE(acpi, rt9455_i2c_acpi_match);
1738 #endif
1739
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),
1748 },
1749 };
1750 module_i2c_driver(rt9455_driver);
1751
1752 MODULE_LICENSE("GPL");
1753 MODULE_AUTHOR("Anda-Maria Nicolae <anda-maria.nicolae@intel.com>");
1754 MODULE_DESCRIPTION("Richtek RT9455 Charger Driver");
1755