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