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