UPSTREAM: PCI: rockchip: remove the pointer to L1 substate cap
[firefly-linux-kernel-4.4.55.git] / drivers / power / rt5036-charger.c
1 /*
2  *  drivers/power/rt5036-charger.c
3  *  Driver for Richtek RT5036 PMIC Charger driver
4  *
5  *  Copyright (C) 2014 Richtek Technology Corp.
6  *  cy_huang <cy_huang@richtek.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  */
13
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/version.h>
18 #include <linux/err.h>
19 #include <linux/platform_device.h>
20 #include <linux/i2c.h>
21 #include <linux/workqueue.h>
22 #include <linux/of.h>
23 #include <linux/power_supply.h>
24 #include <linux/delay.h>
25 #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN
26 #include <linux/of_gpio.h>
27 #include <linux/irq.h>
28 #include <linux/interrupt.h>
29 #endif /* #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN */
30
31 #include <linux/mfd/rt5036/rt5036.h>
32 #include <linux/power/rt5036-charger.h>
33 #ifdef CONFIG_RT_POWER
34 #include <linux/power/rt-power.h>
35 #endif /* #ifdef CONFIG_RT_POWER */
36 #ifdef CONFIG_RT_BATTERY
37 #include <linux/power/rt-battery.h>
38 #endif /* #ifdef CONFIG_RT_BATTERY */
39
40 #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN
41 #define RT5036_ACIN_LEVEL       0
42 #define RT5036_USBIN_LEVEL      1
43 #endif /* #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN */
44
45 static unsigned char chg_init_regval[] = {
46         0xb0,                   /*REG 0x01*/
47 #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI
48         0xDC,                   /*REG 0x02*/
49 #else
50         0x58,                   /*REG 0x02*/
51 #endif /* #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI */
52         0x00,                   /*REG 0x03*/
53         0xFE,                   /*REG 0x04*/
54         0x93,                   /*REG 0x05*/
55         0xAD,                   /*REG 0x06*/
56 #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI
57         0x94,                   /*REG 0x07*/
58         #else
59         0xB4,                   /*REG 0x07*/
60 #endif /* #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI */
61         0x01,                   /*REG 0x08*/
62         0x0C,                   /*REG 0x13*/
63         0x80,                   /*REG 0x14*/
64         0x00,                   /*REG 0x15*/
65         0x70,                   /*REG 0x18*/
66 };
67
68 static char *rtdef_chg_name = "rt-charger";
69
70 static char *rt_charger_supply_list[] = {
71         "none",
72 };
73
74 static enum power_supply_property rt_charger_props[] = {
75         POWER_SUPPLY_PROP_STATUS,
76         POWER_SUPPLY_PROP_ONLINE,
77         POWER_SUPPLY_PROP_PRESENT,
78         POWER_SUPPLY_PROP_CHARGE_NOW,
79         POWER_SUPPLY_PROP_CURRENT_MAX,
80         POWER_SUPPLY_PROP_CURRENT_AVG,
81         POWER_SUPPLY_PROP_CURRENT_NOW,
82         POWER_SUPPLY_PROP_VOLTAGE_NOW,
83         POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
84         POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
85 };
86
87 static int rt_charger_get_property(struct power_supply *psy,
88                                    enum power_supply_property psp,
89                                    union power_supply_propval *val)
90 {
91         struct rt5036_charger_info *ci = dev_get_drvdata(psy->dev->parent);
92         int ret = 0;
93         int regval = 0;
94
95         switch (psp) {
96         case POWER_SUPPLY_PROP_ONLINE:
97                 val->intval = ci->online;
98                 break;
99         case POWER_SUPPLY_PROP_STATUS:
100                 regval = rt5036_reg_read(ci->i2c, RT5036_REG_CHGSTAT1);
101                 if (regval < 0) {
102                         ret = -EINVAL;
103                 } else {
104                         regval &= RT5036_CHGSTAT_MASK;
105                         regval >>= RT5036_CHGSTAT_SHIFT;
106                         switch (regval) {
107                         case 0:
108                                 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
109                                 break;
110                         case 1:
111                                 val->intval = POWER_SUPPLY_STATUS_CHARGING;
112                                 break;
113                         case 2:
114                                 val->intval = POWER_SUPPLY_STATUS_FULL;
115                                 break;
116                         case 3:
117                                 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
118                                 break;
119                         }
120                 }
121                 break;
122         case POWER_SUPPLY_PROP_PRESENT:
123                 regval = rt5036_reg_read(ci->i2c, RT5036_REG_CHGSTAT1);
124                 if (regval < 0) {
125                         ret = -EINVAL;
126                 } else {
127                         if (regval & RT5036_CHGDIS_MASK)
128                                 val->intval = 0;
129                         else
130                                 val->intval = 1;
131                 }
132                 break;
133         case POWER_SUPPLY_PROP_CHARGE_NOW:
134                 val->intval = ci->charge_cable;
135                 break;
136         case POWER_SUPPLY_PROP_CURRENT_MAX:
137                 val->intval = 2000;
138                 break;
139         case POWER_SUPPLY_PROP_CURRENT_AVG:
140                 regval = rt5036_reg_read(ci->i2c, RT5036_REG_CHGCTL1);
141                 if (regval < 0) {
142                         ret = -EINVAL;
143                 } else {
144                         regval &= RT5036_CHGAICR_MASK;
145                         regval >>= RT5036_CHGAICR_SHIFT;
146                         switch (regval) {
147                         case 0:
148                                 val->intval = 0;
149                                 break;
150                         case 1:
151                                 val->intval = 100;
152                                 break;
153                         case 2:
154                                 val->intval = 500;
155                                 break;
156                         case 3:
157                                 val->intval = 700;
158                                 break;
159                         case 4:
160                                 val->intval = 900;
161                                 break;
162                         case 5:
163                                 val->intval = 1000;
164                                 break;
165                         case 6:
166                                 val->intval = 1500;
167                                 break;
168                         case 7:
169                                 val->intval = 2000;
170                                 break;
171                         }
172                 }
173                 break;
174         case POWER_SUPPLY_PROP_CURRENT_NOW:
175                 regval = rt5036_reg_read(ci->i2c, RT5036_REG_CHGSTAT1);
176                 if (regval < 0) {
177                         ret = -EINVAL;
178                 } else {
179                         if (regval & RT5036_CHGOPASTAT_MASK) {
180                                 val->intval = -1;
181                         } else {
182                                 regval =
183                                     rt5036_reg_read(ci->i2c,
184                                                     RT5036_REG_CHGCTL5);
185                                 if (regval < 0) {
186                                         ret = -EINVAL;
187                                 } else {
188                                         regval &= RT5036_CHGICC_MASK;
189                                         regval >>= RT5036_CHGICC_SHIFT;
190                                         val->intval = 500 + regval * 100;
191                                 }
192                         }
193                 }
194                 break;
195         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
196                 regval = rt5036_reg_read(ci->i2c, RT5036_REG_CHGCTL2);
197                 if (regval < 0) {
198                         ret = -EINVAL;
199                 } else {
200                         regval &= RT5036_CHGCV_MASK;
201                         regval >>= RT5036_CHGCV_SHIFT;
202                         val->intval = regval * 25 + 3650;
203                 }
204                 break;
205         case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
206                 val->intval = 3650;
207                 break;
208         case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
209                 val->intval = 4400;
210                 break;
211         default:
212                 ret = -EINVAL;
213         }
214         return ret;
215 }
216
217 static int rt_charger_set_property(struct power_supply *psy,
218                                    enum power_supply_property psp,
219                                    const union power_supply_propval *val)
220 {
221         struct rt5036_charger_info *ci = dev_get_drvdata(psy->dev->parent);
222         int ret = 0;
223         int regval = 0;
224
225         RTINFO("prop = %d, val->intval = %d\n", psp, val->intval);
226         switch (psp) {
227         case POWER_SUPPLY_PROP_ONLINE:
228                 ci->online = val->intval;
229                 if (ci->online) {
230 #ifdef CONFIG_RT_BATTERY
231                         union power_supply_propval pval;
232                         struct power_supply *psy =
233                             power_supply_get_by_name(RT_BATT_NAME);
234                         if (psy) {
235                                 pval.intval = POWER_SUPPLY_STATUS_CHARGING;
236                                 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
237                                                   &pval);
238                                 power_supply_changed(psy);
239                         } else {
240                                 dev_err(ci->dev, "couldn't get RT battery\n");
241                         }
242 #else
243 #ifdef CONFIG_RT9420_FUELGAUGE
244                         union power_supply_propval pval;
245                         struct power_supply *psy =
246                             power_supply_get_by_name("rt-fuelgauge");
247                         if (psy) {
248                                 pval.intval = POWER_SUPPLY_STATUS_CHARGING;
249                                 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
250                                                   &pval);
251                                 power_supply_changed(psy);
252                         } else {
253                                 dev_err(ci->dev,
254                                         "couldn't get rt fuelgauge battery\n");
255                                 }
256 #else
257                         union power_supply_propval pval;
258                         struct power_supply *psy =
259                             power_supply_get_by_name("battery");
260                         if (psy) {
261                                 pval.intval = POWER_SUPPLY_STATUS_CHARGING;
262                                 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
263                                                   &pval);
264                                 power_supply_changed(psy);
265                         } else {
266                                 dev_err(ci->dev, "couldn't get battery\n");
267                                 }
268 #endif /* #ifdef CONFIG_RT9420_FUELGAUGE */
269 #endif /* #ifdef CONFIG_RT_BATTERY */
270                         if (ci->te_en)
271                                 ret =
272                                     rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL1,
273                                                     RT5036_CHGTEEN_MASK);
274                 } else {
275 #ifdef CONFIG_RT_BATTERY
276                         union power_supply_propval pval;
277                         struct power_supply *psy =
278                             power_supply_get_by_name(RT_BATT_NAME);
279                         if (psy) {
280                                 pval.intval = POWER_SUPPLY_STATUS_DISCHARGING;
281                                 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
282                                                   &pval);
283                                 power_supply_changed(psy);
284                         } else {
285                                 dev_err(ci->dev, "couldn't get RT battery\n");
286                         }
287 #else
288 #ifdef CONFIG_RT9420_FUELGAUGE
289                         union power_supply_propval pval;
290                         struct power_supply *psy =
291                             power_supply_get_by_name("rt-fuelgauge");
292                         if (psy) {
293                                 pval.intval = POWER_SUPPLY_STATUS_DISCHARGING;
294                                 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
295                                                   &pval);
296                                 power_supply_changed(psy);
297                         } else {
298                                 dev_err(ci->dev,
299                                         "couldn't get rt fuelgauge battery\n");
300                         }
301 #else
302                         union power_supply_propval pval;
303                         struct power_supply *psy =
304                             power_supply_get_by_name("battery");
305                         if (psy) {
306                                 pval.intval = POWER_SUPPLY_STATUS_DISCHARGING;
307                                 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
308                                                   &pval);
309                                 power_supply_changed(psy);
310                         } else {
311                                 dev_err(ci->dev, "couldn't get battery\n");
312                         }
313 #endif /* #ifdef CONFIG_RT9420_FUELGAUGE */
314 #endif /* #ifdef CONFIG_RT_BATTERY */
315                         if (ci->te_en) {
316                                 ret =
317                                     rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL1,
318                                                     RT5036_CHGTEEN_MASK);
319                                 /* te rst workaround */
320                                 rt5036_set_bits(ci->i2c, 0x20,
321                                                 RT5036_TERST_MASK);
322                                 rt5036_clr_bits(ci->i2c, 0x20,
323                                                 RT5036_TERST_MASK);
324                         }
325                 }
326                 break;
327         case POWER_SUPPLY_PROP_PRESENT:
328                 if (ci->online && val->intval) {
329                         int icc = 0;
330                         union power_supply_propval pval;
331
332                         if (ci->charge_cable == POWER_SUPPLY_TYPE_MAINS)
333                                 icc = ci->acchg_icc;
334                         else if (ci->charge_cable == POWER_SUPPLY_TYPE_USB_DCP)
335                                 icc = ci->usbtachg_icc;
336                         else
337                                 icc = ci->usbchg_icc;
338                         pval.intval = icc;
339                         ret =
340                             psy->set_property(psy,
341                                               POWER_SUPPLY_PROP_CURRENT_NOW,
342                                               &pval);
343                         if (ret < 0)
344                                 dev_err(ci->dev, "set final icc fail\n");
345                 }
346                 break;
347         case POWER_SUPPLY_PROP_CHARGE_NOW:
348                 ci->charge_cable = val->intval;
349                 break;
350         case POWER_SUPPLY_PROP_CURRENT_AVG:
351                 if (val->intval <= 0)
352                         regval = 0;
353                 else if (val->intval <= 100)
354                         regval = 1;
355                 else if (val->intval <= 500)
356                         regval = 2;
357                 else if (val->intval <= 700)
358                         regval = 3;
359                 else if (val->intval <= 900)
360                         regval = 4;
361                 else if (val->intval <= 1000)
362                         regval = 5;
363                 else if (val->intval <= 1500)
364                         regval = 6;
365                 else if (val->intval <= 2000)
366                         regval = 7;
367                 else
368                         regval = 0;
369                 if (!ci->batabs)
370                         ret = rt5036_assign_bits(ci->i2c, RT5036_REG_CHGCTL1,
371                                                  RT5036_CHGAICR_MASK,
372                                                  regval <<
373                                                  RT5036_CHGAICR_SHIFT);
374                 break;
375         case POWER_SUPPLY_PROP_CURRENT_NOW:
376                 if (val->intval < 0) {
377                         union power_supply_propval pval;
378 #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN
379                         struct power_supply *psy =
380                             power_supply_get_by_name(RT_USB_NAME);
381                         pval.intval = 0;
382                         psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE, &pval);
383                         power_supply_changed(psy);
384                         ci->otg_en = 1;
385                         if (ci->charge_cable != POWER_SUPPLY_TYPE_MAINS) {
386                                 /* otg drop fix */
387                                 rt5036_set_bits(ci->i2c, 0x23, 0x3);
388                                 pval.intval = ci->otg_volt;
389                                 ci->psy.set_property(&ci->psy,
390                                         POWER_SUPPLY_PROP_VOLTAGE_NOW,
391                                         &pval);
392 #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI
393                                 dev_info(ci->dev, "set UUG on\n");
394                                 ret = rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL6, 0x20);
395                                 rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL1,
396                                             RT5036_CHGOPAMODE_MASK);
397 #else
398                                 ret =
399                                     rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL1,
400                                                     RT5036_CHGOPAMODE_MASK);
401 #endif /* #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI */
402                                 /* otg drop fix */
403                                 mdelay(10);
404                                 rt5036_clr_bits(ci->i2c, 0x23, 0x3);
405                         }
406 #else
407                         ci->otg_en = 1;
408                         /* otg drop fix */
409                         rt5036_set_bits(ci->i2c, 0x23, 0x3);
410                         pval.intval = ci->otg_volt;
411                         ci->psy.set_property(&ci->psy,
412                                              POWER_SUPPLY_PROP_VOLTAGE_NOW,
413                                              &pval);
414 #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI
415                         ret = rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL6, 0x20);
416 #else
417                         ret =
418                             rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL1,
419                                             RT5036_CHGOPAMODE_MASK);
420 #endif /* #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI */
421                         /* otg drop fix */
422                         mdelay(10);
423                         rt5036_clr_bits(ci->i2c, 0x23, 0x3);
424 #endif /* #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN */
425                 } else if (val->intval == 0) {
426 #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN
427                         if (ci->charge_cable != POWER_SUPPLY_TYPE_MAINS) {
428 #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI
429                                 dev_info(ci->dev, "set UUG off\n");
430                                 ret = rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL6, 0x20);
431                                 rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL1,
432                                             RT5036_CHGOPAMODE_MASK);
433 #else
434                                 ret =
435                                     rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL1,
436                                                     RT5036_CHGOPAMODE_MASK);
437 #endif /* #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI */
438                         }
439 #else
440 #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI
441                         ret = rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL6, 0x20);
442 #else
443                         ret =
444                             rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL1,
445                                             RT5036_CHGOPAMODE_MASK);
446 #endif /* #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI */
447 #endif /* #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN */
448                         ci->otg_en = 0;
449                 } else if (val->intval < 500)
450                         regval = 0;
451                 else if (val->intval > 2000)
452                         regval = 15;
453                 else
454                         regval = (val->intval - 500) / 100;
455                 regval += 5;
456                 if (regval > 15)
457                         regval = 15;
458                 if (!ci->batabs && val->intval > 0)
459                         ret =
460                             rt5036_assign_bits(ci->i2c, RT5036_REG_CHGCTL5,
461                                                RT5036_CHGICC_MASK,
462                                                regval << RT5036_CHGICC_SHIFT);
463                 rt5036_reg_write(ci->i2c, RT5036_REG_CHGCTL4,
464                                  chg_init_regval[4]);
465                 break;
466         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
467                 if (val->intval < 3650)
468                         regval = 0;
469                 else if (val->intval > 5225)
470                         regval = 0x3F;
471                 else
472                         regval = (val->intval - 3650) / 25;
473                 ret =
474                     rt5036_assign_bits(ci->i2c, RT5036_REG_CHGCTL2,
475                                        RT5036_CHGCV_MASK,
476                                        regval << RT5036_CHGCV_SHIFT);
477                 break;
478         case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
479         case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
480         case POWER_SUPPLY_PROP_CURRENT_MAX:
481         case POWER_SUPPLY_PROP_STATUS:
482         default:
483                 ret = -EINVAL;
484         }
485         return ret;
486 }
487
488 static void rt5036_stat2alrt_irq_handler(void *info, int eventno)
489 {
490         struct rt5036_charger_info *ci = info;
491         unsigned char old_stat, new_stat;
492         int ret;
493
494         old_stat = ci->stat2;
495         ret = rt5036_reg_read(ci->i2c, RT5036_REG_CHGSTAT2);
496         if (ret < 0) {
497                 dev_err(ci->dev, "read stat io fail\n");
498                 return;
499         }
500         new_stat = ret;
501         /*cablein status change*/
502         if ((old_stat ^ new_stat) & RT5036_PWRRDY_MASK) {
503                 if (new_stat & RT5036_PWRRDY_MASK) {
504 #ifndef CONFIG_RT_SUPPORT_ACUSB_DUALIN
505                         union power_supply_propval pval;
506                         struct power_supply *psy =
507                             power_supply_get_by_name(RT_USB_NAME);
508                         if (psy) {
509 #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI
510                                 rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL1,
511                                             RT5036_CHGOPAMODE_MASK);
512                                 rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL6, 0x20);
513 #endif /* #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI */
514                                 pval.intval = 1;
515                                 psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE,
516                                                   &pval);
517                                 power_supply_changed(psy);
518                         } else {
519                                 dev_err(ci->dev, "couldn't get RT usb\n");
520                         }
521 #endif /* #ifndef CONFIG_RT_SUPPORT_ACUSB_DUALIN */
522                         dev_info(ci->dev, "cable in\n");
523                 } else {
524 #ifndef CONFIG_RT_SUPPORT_ACUSB_DUALIN
525                         union power_supply_propval pval;
526                         struct power_supply *psy =
527                             power_supply_get_by_name(RT_USB_NAME);
528                         if (psy) {
529                                 pval.intval = 0;
530                                 psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE,
531                                                   &pval);
532                                 power_supply_changed(psy);
533 #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI
534                                 rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL6, 0x20);
535                                 pval.intval = ci->otg_volt;
536                                 ci->psy.set_property(&ci->psy,
537                                                      POWER_SUPPLY_PROP_VOLTAGE_NOW,
538                                                      &pval);
539                                 rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL1,
540                                             RT5036_CHGOPAMODE_MASK);
541 #endif /* #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI */
542                         } else
543                                 dev_err(ci->dev, "couldn't get RT usb\n");
544                         }
545 #endif /* #ifndef CONFIG_RT_SUPPORT_ACUSB_DUALIN */
546                         dev_info(ci->dev, "cable out\n");
547                 }
548         /*jeita status change*/
549         old_stat = new_stat & RT5036_TSEVENT_MASK;
550         if (old_stat & RT5036_TSWC_MASK) {
551                 dev_info(ci->dev, "warm or cool parameter\n");
552                 rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL7,
553                                 RT5036_CCJEITA_MASK);
554         } else if (old_stat & RT5036_TSHC_MASK) {
555                 dev_info(ci->dev, "hot or cold temperature\n");
556                 rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL7,
557                                 RT5036_CCJEITA_MASK);
558         } else {
559                 dev_info(ci->dev, "normal temperature\n");
560                 rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL7,
561                                 RT5036_CCJEITA_MASK);
562         }
563         ci->stat2 = new_stat;
564 }
565
566 static void rt5036_batabs_irq_handler(void *info, int eventno)
567 {
568         struct rt5036_charger_info *ci = info;
569         struct power_supply *psy = &ci->psy;
570         union power_supply_propval val;
571         int ret;
572         /*disable battery detection*/
573         ret = rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL6, RT5036_BATDEN_MASK);
574         if (ret < 0)
575                 dev_err(ci->dev, "set battary detection disable fail\n");
576         /*set aicr to 2000*/
577         val.intval = 2000;
578         ret = psy->set_property(psy, POWER_SUPPLY_PROP_CURRENT_AVG, &val);
579         if (ret < 0)
580                 dev_err(ci->dev, "set aicr to 2000 fail\n");
581         /*set icc to 2000*/
582         val.intval = 2000;
583         ret = psy->set_property(psy, POWER_SUPPLY_PROP_CURRENT_NOW, &val);
584         if (ret < 0)
585                 dev_err(ci->dev, "set icc to 2000 fail\n");
586         /*set charger offline*/
587         val.intval = 0;
588         ret = psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE, &val);
589         if (ret < 0)
590                 dev_err(ci->dev, "set charger offline fail\n");
591 #ifdef CONFIG_RT_BATTERY
592         psy = power_supply_get_by_name(RT_BATT_NAME);
593         if (psy) {
594                 val.intval = 0;
595                 ret = psy->set_property(psy, POWER_SUPPLY_PROP_PRESENT, &val);
596                 if (ret < 0)
597                         dev_err(ci->dev, "set battery not present fail\n");
598         } else {
599                 dev_err(ci->dev, "couldn't get batt psy\n");
600                 }
601 #else
602 #ifdef CONFIG_RT9420_FUELGAUGE
603         psy = power_supply_get_by_name("rt-fuelgauge");
604         if (psy) {
605                 val.intval = 0;
606                 ret = psy->set_property(psy, POWER_SUPPLY_PROP_PRESENT, &val);
607                 if (ret < 0)
608                         dev_err(ci->dev, "set battery not present fail\n");
609         } else {
610                 dev_err(ci->dev, "couldn't get rt fuelgauge psy\n");
611                 }
612 #else
613         psy = power_supply_get_by_name("battery");
614         if (psy) {
615                 val.intval = 0;
616                 ret = psy->set_property(psy, POWER_SUPPLY_PROP_PRESENT, &val);
617                 if (ret < 0)
618                         dev_err(ci->dev, "set battery not present fail\n");
619         } else {
620                 dev_err(ci->dev, "couldn't get battery psy\n");
621                 }
622 #endif /* #ifdef CONFIG_RT9420_FUELGAUGE */
623 #endif /* #ifdef CONFIG_RT_BATTERY */
624         ci->batabs = 1;
625 }
626
627 static void rt5036_general_irq_handler(void *info, int eventno)
628 {
629         struct rt5036_charger_info *ci = info;
630
631         RTINFO("eventno=%02d\n", eventno);
632         switch (eventno) {
633         case CHGEVENT_CHTMRFI:
634                 rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL3,
635                                 RT5036_CHGOTGEN_MASK);
636                 msleep(5);
637                 rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL3,
638                                 RT5036_CHGOTGEN_MASK);
639                 break;
640         case CHGEVENT_CHRCHGI:
641                 if (!ci->online) {
642                         dev_warn(ci->dev, "recharge false alarm\n");
643                 } else {
644 #ifdef CONFIG_RT_BATTERY
645                         struct power_supply *psy =
646                             power_supply_get_by_name(RT_BATT_NAME);
647                         union power_supply_propval pval;
648
649                         if (psy) {
650                                 pval.intval = POWER_SUPPLY_STATUS_CHARGING;
651                                 /*set battery status*/
652                                 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
653                                                   &pval);
654                                 power_supply_changed(psy);
655                         } else {
656                                 dev_err(ci->dev, "couldn't get batt psy\n");
657                                 }
658 #else
659 #ifdef CONFIG_RT9420_FUELGAUGE
660                         struct power_supply *psy =
661                             power_supply_get_by_name("rt-fuelgauge");
662                         union power_supply_propval pval;
663
664                         if (psy) {
665                                 pval.intval = POWER_SUPPLY_STATUS_CHARGING;
666                                 /*set battery status*/
667                                 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
668                                                   &pval);
669                                 power_supply_changed(psy);
670                         } else {
671                                 dev_err(ci->dev,
672                                         "couldn't get rt fuelgauge psy\n");
673                                 }
674 #else
675                         struct power_supply *psy =
676                             power_supply_get_by_name("battery");
677                         union power_supply_propval pval;
678
679                         if (psy) {
680                                 pval.intval = POWER_SUPPLY_STATUS_CHARGING;
681                                 /*set battery status*/
682                                 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
683                                                   &pval);
684                                 power_supply_changed(psy);
685                         } else {
686                                 dev_err(ci->dev, "couldn't get battery psy\n");
687                                 }
688 #endif /* #ifdef CONFIG_RT9420_FUELGAUGE */
689 #endif /* #ifdfef CONFIG_RT_BATTERY */
690                         dev_info(ci->dev, "recharge occur\n");
691                 }
692                 break;
693         case CHGEVENT_IEOCI:
694                 if (!ci->online) {
695                         dev_warn(ci->dev, "eoc false alarm\n");
696                 } else {
697 #ifdef CONFIG_RT_BATTERY
698                         struct power_supply *psy =
699                             power_supply_get_by_name(RT_BATT_NAME);
700                         union power_supply_propval pval;
701
702                         if (psy) {
703                                 pval.intval = POWER_SUPPLY_STATUS_FULL;
704                                 /*set battery status*/
705                                 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
706                                                   &pval);
707                                 power_supply_changed(psy);
708                         } else {
709                                 dev_err(ci->dev, "couldn't get batt psy\n");
710                                 }
711 #else
712 #ifdef CONFIG_RT9420_FUELGAUGE
713                         struct power_supply *psy =
714                             power_supply_get_by_name("rt-fuelgauge");
715                         union power_supply_propval pval;
716
717                         if (psy) {
718                                 /*set battery status*/
719                                 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
720                                                   &pval);
721                                 power_supply_changed(psy);
722                         } else {
723                                 dev_err(ci->dev,
724                                         "couldn't get rt fuelgauge psy\n");
725                                 }
726 #else
727                         struct power_supply *psy =
728                             power_supply_get_by_name("battery");
729                         union power_supply_propval pval;
730
731                         if (psy) {
732                                 pval.intval = POWER_SUPPLY_STATUS_FULL;
733                                 /*set battery status*/
734                                 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
735                                                   &pval);
736                                 power_supply_changed(psy);
737                         } else {
738                                 dev_err(ci->dev, "couldn't get battery psy\n");
739                                 }
740 #endif /* #ifdef CONFIG_RT9420_FUELGAUGE */
741 #endif /* #ifdfef CONFIG_RT_BATTERY */
742                         dev_info(ci->dev, "eoc really occur\n");
743                 }
744                 break;
745         default:
746                 break;
747         }
748 }
749
750 static rt_irq_handler rt_chgirq_handler[CHGEVENT_MAX] = {
751         [CHGEVENT_STAT2ALT] = rt5036_stat2alrt_irq_handler,
752         [CHGEVENT_CHBSTLOWVI] = rt5036_general_irq_handler,
753         [CHGEVENT_BSTOLI] = rt5036_general_irq_handler,
754         [CHGEVENT_BSTVIMIDOVP] = rt5036_general_irq_handler,
755         [CHGEVENT_CHTMRFI] = rt5036_general_irq_handler,
756         [CHGEVENT_CHRCHGI] = rt5036_general_irq_handler,
757         [CHGEVENT_CHTERMI] = rt5036_general_irq_handler,
758         [CHGEVENT_CHBATOVI] = rt5036_general_irq_handler,
759         [CHGEVENT_CHRVPI] = rt5036_general_irq_handler,
760         [CHGEVENT_BATABSENSE] = rt5036_batabs_irq_handler,
761         [CHGEVENT_CHBADADPI] = rt5036_general_irq_handler,
762         [CHGEVENT_VINCHGPLUGOUT] = rt5036_general_irq_handler,
763         [CHGEVENT_VINCHGPLUGIN] = rt5036_general_irq_handler,
764         [CHGEVENT_PPBATLVI] = rt5036_general_irq_handler,
765         [CHGEVENT_IEOCI] = rt5036_general_irq_handler,
766         [CHGEVENT_VINOVPI] = rt5036_general_irq_handler,
767 };
768
769 void rt5036_charger_irq_handler(struct rt5036_charger_info *ci,
770                                 unsigned int irqevent)
771 {
772         int i;
773         unsigned int masked_irq_event =
774             (chg_init_regval[8] << 16) | (chg_init_regval[9] << 8) |
775             chg_init_regval[10];
776         unsigned int final_irq_event = irqevent & (~masked_irq_event);
777
778         for (i = 0; i < CHGEVENT_MAX; i++) {
779                 if ((final_irq_event & (1 << i)) && rt_chgirq_handler[i])
780                         rt_chgirq_handler[i] (ci, i);
781         }
782 }
783 EXPORT_SYMBOL(rt5036_charger_irq_handler);
784
785 #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN
786 static irqreturn_t rt5036_acdet_irq_handler(int irqno, void *param)
787 {
788         struct rt5036_charger_info *ci = param;
789
790         if (RT5036_ACIN_LEVEL == gpio_get_value(ci->acdet_gpio)) {
791                 union power_supply_propval pval;
792                 struct power_supply *psy = power_supply_get_by_name(RT_AC_NAME);
793
794                 if (RT5036_ACIN_LEVEL)
795                         irq_set_irq_type(ci->acdet_irq, IRQF_TRIGGER_FALLING);
796                 else
797                         irq_set_irq_type(ci->acdet_irq, IRQF_TRIGGER_RISING);
798                 if (psy) {
799 #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI
800                         rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL1,
801                                     RT5036_CHGOPAMODE_MASK);
802                         rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL6, 0x20);
803 #else
804                         if (ci->otg_en)
805                                 rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL1,
806                                                 RT5036_CHGOPAMODE_MASK);
807 #endif /* #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI */
808                         pval.intval = 1;
809                         psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE, &pval);
810                         power_supply_changed(psy);
811                 } else {
812                         dev_err(ci->dev, "couldn't get RT ac\n");
813                         }
814                 dev_info(ci->dev, "ac in\n");
815         } else {
816                 union power_supply_propval pval;
817                 struct power_supply *psy = power_supply_get_by_name(RT_AC_NAME);
818
819                 if (RT5036_ACIN_LEVEL)
820                         irq_set_irq_type(ci->acdet_irq, IRQF_TRIGGER_RISING);
821                 else
822                         irq_set_irq_type(ci->acdet_irq, IRQF_TRIGGER_FALLING);
823                 if (psy) {
824                         pval.intval = 0;
825                         psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE, &pval);
826                         power_supply_changed(psy);
827 #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI
828                         if (ci->charge_cable == 0) {
829                                 if (ci->otg_en)
830                                         rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL6, 0x20);
831                                 else
832                                         rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL6, 0x20);
833                                 pval.intval = ci->otg_volt;
834                                 ci->psy.set_property(&ci->psy,
835                                                      POWER_SUPPLY_PROP_VOLTAGE_NOW,
836                                                      &pval);
837                                 rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL1,
838                                             RT5036_CHGOPAMODE_MASK);
839                         }
840 #else
841                         if (ci->otg_en) {
842                                 /*set otg voltage*/
843                                 pval.intval = ci->otg_volt;
844                                 ci->psy.set_property(&ci->psy,
845                                         POWER_SUPPLY_PROP_VOLTAGE_NOW,
846                                         &pval);
847                                 rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL1,
848                                                 RT5036_CHGOPAMODE_MASK);
849                         }
850 #endif /* #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI */
851                 } else
852                         dev_err(ci->dev, "couldn't get RT ac\n");
853                         }
854                 dev_info(ci->dev, "ac out\n");
855         }
856         return IRQ_HANDLED;
857 }
858
859 static irqreturn_t rt5036_usbdet_irq_handler(int irqno, void *param)
860 {
861         struct rt5036_charger_info *ci = param;
862
863         if (ci->otg_en) {
864                 dev_info(ci->dev, "currently in otg mode\n");
865                 goto usb_out;
866         }
867         if (RT5036_USBIN_LEVEL == gpio_get_value(ci->usbdet_gpio)) {
868                 union power_supply_propval pval;
869                 struct power_supply *psy =
870                     power_supply_get_by_name(RT_USB_NAME);
871                 if (RT5036_USBIN_LEVEL)
872                         irq_set_irq_type(ci->usbdet_irq, IRQF_TRIGGER_FALLING);
873                 else
874                         irq_set_irq_type(ci->usbdet_irq, IRQF_TRIGGER_RISING);
875                 if (psy) {
876 #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI
877                         rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL1,
878                                     RT5036_CHGOPAMODE_MASK);
879                         rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL6, 0x20);
880 #endif /* #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI */
881                         pval.intval = 1;
882                         psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE, &pval);
883                         power_supply_changed(psy);
884                 } else {
885                         dev_err(ci->dev, "couldn't get RT usb\n");
886                         }
887                 dev_info(ci->dev, "usb in\n");
888         } else {
889                 union power_supply_propval pval;
890                 struct power_supply *psy =
891                     power_supply_get_by_name(RT_USB_NAME);
892                 if (RT5036_USBIN_LEVEL)
893                         irq_set_irq_type(ci->usbdet_irq, IRQF_TRIGGER_RISING);
894                 else
895                         irq_set_irq_type(ci->usbdet_irq, IRQF_TRIGGER_FALLING);
896                 if (psy) {
897                         pval.intval = 0;
898                         psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE, &pval);
899                         power_supply_changed(psy);
900 #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI
901                         if (ci->charge_cable == 0) {
902                                 rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL6, 0x20);
903                                 pval.intval = ci->otg_volt;
904                                 ci->psy.set_property(&ci->psy,
905                                                      POWER_SUPPLY_PROP_VOLTAGE_NOW,
906                                                      &pval);
907                                 rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL1,
908                                             RT5036_CHGOPAMODE_MASK);
909                         }
910 #endif /* #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI */
911                 } else
912                         dev_err(ci->dev, "couldn't get RT usb\n");
913                         }
914                 dev_info(ci->dev, "usb out\n");
915         }
916 usb_out:
917         return IRQ_HANDLED;
918 }
919
920 static int rt5036_acusb_irqinit(struct rt5036_charger_info *ci)
921 {
922         int rc = 0;
923
924         if (gpio_is_valid(ci->acdet_gpio) && gpio_is_valid(ci->usbdet_gpio)) {
925                 rc = gpio_request(ci->acdet_gpio, "rt5036_acdet_gpio");
926                 if (rc < 0) {
927                         dev_err(ci->dev, "request acdet gpio fail\n");
928                         goto irq_out;
929                 }
930                 rc = gpio_request(ci->usbdet_gpio, "rt5036_usbdet_gpio");
931                 if (rc < 0) {
932                         dev_err(ci->dev, "request usbdet gpio fail\n");
933                         goto irq_out;
934                 }
935                 gpio_direction_input(ci->acdet_gpio);
936                 gpio_direction_input(ci->usbdet_gpio);
937
938                 ci->acdet_irq = gpio_to_irq(ci->acdet_gpio);
939                 ci->usbdet_irq = gpio_to_irq(ci->usbdet_gpio);
940
941                 rc = devm_request_threaded_irq(ci->dev, ci->acdet_irq, NULL,
942                                                rt5036_acdet_irq_handler,
943                                                IRQF_TRIGGER_FALLING |
944                                                IRQF_DISABLED,
945                                                "rt5036_acdet_irq", ci);
946                 if (rc < 0) {
947                         dev_err(ci->dev, "request acdet irq fail\n");
948                         goto irq_out;
949                 }
950                 rc = devm_request_threaded_irq(ci->dev, ci->usbdet_irq, NULL,
951                                                rt5036_usbdet_irq_handler,
952                                                IRQF_TRIGGER_FALLING |
953                                                IRQF_DISABLED,
954                                                "rt5036_usbdet_irq", ci);
955                 if (rc < 0) {
956                         dev_err(ci->dev, "request usbdet irq fail\n");
957                         goto irq_out;
958                 }
959                 enable_irq_wake(ci->acdet_irq);
960                 enable_irq_wake(ci->usbdet_irq);
961         } else {
962                 rc = -EINVAL;
963                 }
964 irq_out:
965         return rc;
966 }
967
968 static void rt5036_acusb_irqdeinit(struct rt5036_charger_info *ci)
969 {
970         devm_free_irq(ci->dev, ci->acdet_irq, ci);
971         devm_free_irq(ci->dev, ci->usbdet_irq, ci);
972         gpio_free(ci->acdet_gpio);
973         gpio_free(ci->usbdet_gpio);
974 }
975 #endif /* #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN */
976
977 static void rt5036_chg_dwork_func(struct work_struct *work)
978 {
979         struct rt5036_charger_info *ci =
980             container_of(work, struct rt5036_charger_info,
981                          dwork.work);
982         rt5036_stat2alrt_irq_handler(ci, 0);
983 #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN
984         rt5036_acdet_irq_handler(ci->acdet_irq, ci);
985         rt5036_usbdet_irq_handler(ci->usbdet_irq, ci);
986 #endif /* #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN */
987 }
988
989 static int rt5036_charger_reginit(struct i2c_client *client)
990 {
991         /*thermal HGM*/
992         rt5036_set_bits(client, 0x20, 0x40);
993         /*charger fix in rev D IC*/
994 #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI
995         rt5036_reg_write(client, 0x22, 0x60);
996 #else
997         rt5036_reg_write(client, 0x22, 0xE0);
998 #endif /* #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI */
999         /*write charger init val*/
1000         rt5036_reg_block_write(client, RT5036_REG_CHGCTL1, 8, chg_init_regval);
1001         rt5036_reg_block_write(client, RT5036_REG_CHGIRQMASK1, 3,
1002                                &chg_init_regval[8]);
1003         rt5036_reg_write(client, RT5036_REG_CHGSTAT2MASK, chg_init_regval[11]);
1004         /*always read at first time*/
1005         rt5036_reg_read(client, RT5036_REG_CHGIRQ1);
1006         rt5036_reg_read(client, RT5036_REG_CHGIRQ2);
1007         rt5036_reg_read(client, RT5036_REG_CHGIRQ3);
1008         rt5036_set_bits(client, 0x20, RT5036_TERST_MASK);
1009         rt5036_clr_bits(client, 0x20, RT5036_TERST_MASK);
1010 #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI
1011         rt5036_set_bits(client, RT5036_REG_CHGCTL1, RT5036_CHGOPAMODE_MASK);
1012 #endif /* #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI */
1013         RTINFO("\n");
1014         return 0;
1015 }
1016
1017 static int rt_parse_dt(struct rt5036_charger_info *ci,
1018                                  struct device *dev)
1019 {
1020 #ifdef CONFIG_OF
1021         struct device_node *np = dev->of_node;
1022         u32 val;
1023
1024         if (of_property_read_bool(np, "rt,te_en"))
1025                 ci->te_en = 1;
1026
1027         if (of_property_read_u32(np, "rt,iprec", &val)) {
1028                 dev_info(dev, "no iprec property, use the default value\n");
1029         } else {
1030                 if (val > RT5036_IPREC_MAX)
1031                         val = RT5036_IPREC_MAX;
1032                 chg_init_regval[4] &= (~RT5036_CHGIPREC_MASK);
1033                 chg_init_regval[4] |= (val << RT5036_CHGIPREC_SHIFT);
1034         }
1035
1036         if (of_property_read_u32(np, "rt,ieoc", &val)) {
1037                 dev_info(dev, "no ieoc property, use the default value\n");
1038         } else {
1039                 if (val > RT5036_IEOC_MAX)
1040                         val = RT5036_IEOC_MAX;
1041                 chg_init_regval[4] &= (~RT5036_CHGIEOC_MASK);
1042                 chg_init_regval[4] |= val;
1043         }
1044
1045         if (of_property_read_u32(np, "rt,vprec", &val)) {
1046                 dev_info(dev, "no vprec property, use the default value\n");
1047         } else {
1048                 if (val > RT5036_VPREC_MAX)
1049                         val = RT5036_VPREC_MAX;
1050                 chg_init_regval[5] &= (~RT5036_CHGVPREC_MASK);
1051                 chg_init_regval[5] |= val;
1052         }
1053
1054         if (of_property_read_u32(np, "rt,batlv", &val)) {
1055                 dev_info(dev, "no batlv property, use the default value\n");
1056         } else {
1057                 if (val > RT5036_BATLV_MAX)
1058                         val = RT5036_BATLV_MAX;
1059                 chg_init_regval[6] &= (~RT5036_CHGBATLV_MASK);
1060                 chg_init_regval[6] |= val;
1061         }
1062
1063         if (of_property_read_u32(np, "rt,vrechg", &val)) {
1064                 dev_info(dev, "no vrechg property, use the default value\n");
1065         } else {
1066                 if (val > RT5036_VRECHG_MAX)
1067                         val = RT5036_VRECHG_MAX;
1068                 chg_init_regval[7] &= (~RT5036_CHGVRECHG_MASK);
1069                 chg_init_regval[7] |= (val << RT5036_CHGVRECHG_SHIFT);
1070         }
1071
1072         if (of_property_read_u32(np, "rt,chg_volt", &val)) {
1073                 dev_info(dev,
1074                          "no chg_volt property, use 4200 as the default value\n");
1075                 ci->chg_volt = 4200;
1076         } else {
1077                 ci->chg_volt = val;
1078                 }
1079
1080         if (of_property_read_u32(np, "rt,otg_volt", &val)) {
1081                 dev_info(dev,
1082                          "no otg_volt property, use 5025 as the default value\n");
1083                 ci->otg_volt = 5025;
1084         } else {
1085                 ci->otg_volt = val;
1086                 }
1087
1088         if (of_property_read_u32(np, "rt,acchg_icc", &val)) {
1089                 dev_info(dev,
1090                          "no acchg_icc property, use 2000 as the default value\n");
1091                 ci->acchg_icc = 2000;
1092         } else {
1093                 ci->acchg_icc = val;
1094                 }
1095
1096         if (of_property_read_u32(np, "rt,usbtachg_icc", &val)) {
1097                 dev_info(dev,
1098                          "no usbtachg_icc property, use 2000 as the default value\n");
1099                 ci->usbtachg_icc = 2000;
1100         } else {
1101                 ci->usbtachg_icc = val;
1102                 }
1103
1104         if (of_property_read_u32(np, "rt,usbchg_icc", &val)) {
1105                 dev_info(dev,
1106                          "no usbchg_icc property, use 500 as the default value\n");
1107                 ci->usbchg_icc = 500;
1108         } else {
1109                 ci->usbchg_icc = val;
1110                 }
1111
1112 #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN
1113         ci->acdet_gpio = of_get_named_gpio(np, "rt,acdet_gpio", 0);
1114         ci->usbdet_gpio = of_get_named_gpio(np, "rt,usbdet_gpio", 0);
1115 #endif /* #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN */
1116 #endif /* #ifdef CONFIG_OF */
1117         rt5036_charger_reginit(ci->i2c);
1118         RTINFO("\n");
1119         return 0;
1120 }
1121
1122 static int rt_parse_pdata(struct rt5036_charger_info *ci,
1123                                     struct device *dev)
1124 {
1125         struct rt5036_chg_data *pdata = dev->platform_data;
1126
1127         if (pdata->te_en)
1128                 ci->te_en = 1;
1129
1130         chg_init_regval[4] &= (~RT5036_CHGIPREC_MASK);
1131         chg_init_regval[4] |= (pdata->iprec << RT5036_CHGIPREC_SHIFT);
1132
1133         chg_init_regval[4] &= (~RT5036_CHGIEOC_MASK);
1134         chg_init_regval[4] |= pdata->ieoc;
1135
1136         chg_init_regval[5] &= (~RT5036_CHGVPREC_MASK);
1137         chg_init_regval[5] |= pdata->vprec;
1138
1139         chg_init_regval[6] &= (~RT5036_CHGBATLV_MASK);
1140         chg_init_regval[6] |= pdata->batlv;
1141
1142         chg_init_regval[7] &= (~RT5036_CHGVRECHG_MASK);
1143         chg_init_regval[7] |= (pdata->vrechg << RT5036_CHGVRECHG_SHIFT);
1144
1145         ci->chg_volt = pdata->chg_volt;
1146         ci->otg_volt = pdata->otg_volt;
1147         ci->acchg_icc = pdata->acchg_icc;
1148         ci->usbtachg_icc = pdata->usbtachg_icc;
1149         ci->usbchg_icc = pdata->usbchg_icc;
1150 #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN
1151         ci->acdet_gpio = pdata->acdet_gpio;
1152         ci->usbdet_gpio = pdata->usbdet_gpio;
1153 #endif /* #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN */
1154         rt5036_charger_reginit(ci->i2c);
1155         RTINFO("\n");
1156         return 0;
1157 }
1158
1159 #ifdef CONFIG_RT_POWER
1160 static struct platform_device rt_power_dev = {
1161         .name = "rt-power",
1162         .id = -1,
1163 };
1164 #endif /* #ifdef CONFIG_RT_POWER */
1165
1166 #ifdef CONFIG_RT_BATTERY
1167 static struct platform_device rt_battery_dev = {
1168         .name = "rt-battery",
1169         .id = -1,
1170 };
1171 #endif /* #ifdef CONFIG_RT_BATTERY */
1172
1173 static int rt5036_charger_probe(struct platform_device *pdev)
1174 {
1175         struct rt5036_chip *chip = dev_get_drvdata(pdev->dev.parent);
1176         struct rt5036_platform_data *pdata = (pdev->dev.parent)->platform_data;
1177 #ifdef CONFIG_RT_POWER
1178         struct rt_power_data *rt_power_pdata;
1179 #endif /* #ifdef CONFIG_RT_POWER */
1180         struct rt5036_charger_info *ci;
1181         bool use_dt = pdev->dev.of_node;
1182         int ret = 0;
1183
1184         ci = devm_kzalloc(&pdev->dev, sizeof(*ci), GFP_KERNEL);
1185         if (!ci)
1186                 return -ENOMEM;
1187
1188         ci->i2c = chip->i2c;
1189         if (use_dt) {
1190                 rt_parse_dt(ci, &pdev->dev);
1191         } else {
1192                 if (!pdata) {
1193                         dev_err(&pdev->dev, "platform data invalid\n");
1194                         ret = -EINVAL;
1195                         goto out_dev;
1196                 }
1197                 pdev->dev.platform_data = pdata->chg_pdata;
1198                 rt_parse_pdata(ci, &pdev->dev);
1199         }
1200
1201         ci->dev = &pdev->dev;
1202         INIT_DELAYED_WORK(&ci->dwork, rt5036_chg_dwork_func);
1203
1204         platform_set_drvdata(pdev, ci);
1205         /*power supply register*/
1206         ci->psy.name = rtdef_chg_name;
1207 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 3, 0))
1208         ci->psy.type = POWER_SUPPLY_TYPE_UNKNOWN;
1209 #else
1210         ci->psy.type = -1;
1211 #endif /* #ifdef (LINUX_VERSION_CODE */
1212         ci->psy.supplied_to = rt_charger_supply_list;
1213         ci->psy.properties = rt_charger_props;
1214         ci->psy.num_properties = ARRAY_SIZE(rt_charger_props);
1215         ci->psy.get_property = rt_charger_get_property;
1216         ci->psy.set_property = rt_charger_set_property;
1217         ret = power_supply_register(&pdev->dev, &ci->psy);
1218         if (ret < 0) {
1219                 dev_err(&pdev->dev,
1220                         "couldn't create power supply for rt-charger\n");
1221                 goto out_dev;
1222         }
1223 #ifdef CONFIG_RT_BATTERY
1224         rt_battery_dev.dev.parent = &pdev->dev;
1225         ret = platform_device_register(&rt_battery_dev);
1226         if (ret < 0)
1227                 goto out_dev;
1228 #endif /* #ifdef CONFIG_RT_BATTERY */
1229
1230 #ifdef CONFIG_RT_POWER
1231         rt_power_pdata =
1232             devm_kzalloc(&pdev->dev, sizeof(*rt_power_pdata), GFP_KERNEL);
1233         if (!rt_power_pdata) {
1234                 ret = -ENOMEM;
1235                 goto out_psy;
1236         }
1237         rt_power_pdata->chg_volt = ci->chg_volt;
1238         rt_power_pdata->acchg_icc = ci->acchg_icc;
1239         rt_power_pdata->usbtachg_icc = ci->usbtachg_icc;
1240         rt_power_pdata->usbchg_icc = ci->usbchg_icc;
1241
1242         rt_power_dev.dev.platform_data = rt_power_pdata;
1243         rt_power_dev.dev.parent = &pdev->dev;
1244         ret = platform_device_register(&rt_power_dev);
1245         if (ret < 0)
1246                 goto out_psy;
1247 #endif /* #ifdef CONFIG_RT_POWER */
1248
1249 #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN
1250         ret = rt5036_acusb_irqinit(ci);
1251         if (ret < 0)
1252                 goto out_all;
1253 #endif /* #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN */
1254
1255         schedule_delayed_work(&ci->dwork, msecs_to_jiffies(100));
1256         chip->chg_info = ci;
1257         dev_info(&pdev->dev, "driver successfully loaded\n");
1258         return 0;
1259 #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN
1260 out_all:
1261         platform_device_unregister(&rt_power_dev);
1262         devm_kfree(&pdev->dev, rt_power_pdata);
1263 #endif /* #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN */
1264 #ifdef CONFIG_RT_POWER
1265 out_psy:
1266 #endif /* #ifdef CONFIG_RT_POEWR */
1267 #ifdef CONFIG_RT_BATTERY
1268         platform_device_unregister(&rt_battery_dev);
1269 #endif /* #ifdef CONFIG_RT_BATTERY */
1270         power_supply_unregister(&ci->psy);
1271 out_dev:
1272         return ret;
1273 }
1274
1275 static int rt5036_charger_remove(struct platform_device *pdev)
1276 {
1277         struct rt5036_charger_info *ci = platform_get_drvdata(pdev);
1278 #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN
1279         rt5036_acusb_irqdeinit(ci);
1280 #endif /* #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN */
1281 #ifdef CONFIG_RT_POWER
1282         platform_device_unregister(&rt_power_dev);
1283 #endif /* #ifdef CONFIG_RT_POWER */
1284 #ifdef CONFIG_RT_BATTERY
1285         platform_device_unregister(&rt_battery_dev);
1286 #endif /* #ifdef CONFIG_RT_BATTERY */
1287         power_supply_unregister(&ci->psy);
1288         return 0;
1289 }
1290
1291 static const struct of_device_id rt_match_table[] = {
1292         {.compatible = "rt,rt5036-charger",},
1293         {},
1294 };
1295
1296 static struct platform_driver rt5036_charger_driver = {
1297         .driver = {
1298                    .name = RT5036_DEV_NAME "-charger",
1299                    .owner = THIS_MODULE,
1300                    .of_match_table = rt_match_table,
1301                    },
1302         .probe = rt5036_charger_probe,
1303         .remove = rt5036_charger_remove,
1304 };
1305
1306 static int __init rt5036_charger_init(void)
1307 {
1308         return platform_driver_register(&rt5036_charger_driver);
1309 }
1310 subsys_initcall(rt5036_charger_init);
1311
1312 static void __exit rt5036_charger_exit(void)
1313 {
1314         platform_driver_unregister(&rt5036_charger_driver);
1315 }
1316 module_exit(rt5036_charger_exit);
1317
1318 MODULE_LICENSE("GPL");
1319 MODULE_AUTHOR("CY Huang <cy_huang@richtek.com");
1320 MODULE_DESCRIPTION("Charger driver for RT5036");
1321 MODULE_ALIAS("platform:" RT5036_DEV_NAME "-charger");
1322 MODULE_VERSION(RT5036_DRV_VER);