2 * drivers/power/rt5036-charger.c
3 * Driver for Richtek RT5036 PMIC Charger driver
5 * Copyright (C) 2014 Richtek Technology Corp.
6 * cy_huang <cy_huang@richtek.com>
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.
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>
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 */
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 */
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 */
45 static unsigned char chg_init_regval[] = {
47 #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI
51 #endif /* #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI */
56 #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI
60 #endif /* #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI */
68 static char *rtdef_chg_name = "rt-charger";
70 static char *rt_charger_supply_list[] = {
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,
87 static int rt_charger_get_property(struct power_supply *psy,
88 enum power_supply_property psp,
89 union power_supply_propval *val)
91 struct rt5036_charger_info *ci = dev_get_drvdata(psy->dev->parent);
96 case POWER_SUPPLY_PROP_ONLINE:
97 val->intval = ci->online;
99 case POWER_SUPPLY_PROP_STATUS:
100 regval = rt5036_reg_read(ci->i2c, RT5036_REG_CHGSTAT1);
104 regval &= RT5036_CHGSTAT_MASK;
105 regval >>= RT5036_CHGSTAT_SHIFT;
108 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
111 val->intval = POWER_SUPPLY_STATUS_CHARGING;
114 val->intval = POWER_SUPPLY_STATUS_FULL;
117 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
122 case POWER_SUPPLY_PROP_PRESENT:
123 regval = rt5036_reg_read(ci->i2c, RT5036_REG_CHGSTAT1);
127 if (regval & RT5036_CHGDIS_MASK)
133 case POWER_SUPPLY_PROP_CHARGE_NOW:
134 val->intval = ci->charge_cable;
136 case POWER_SUPPLY_PROP_CURRENT_MAX:
139 case POWER_SUPPLY_PROP_CURRENT_AVG:
140 regval = rt5036_reg_read(ci->i2c, RT5036_REG_CHGCTL1);
144 regval &= RT5036_CHGAICR_MASK;
145 regval >>= RT5036_CHGAICR_SHIFT;
174 case POWER_SUPPLY_PROP_CURRENT_NOW:
175 regval = rt5036_reg_read(ci->i2c, RT5036_REG_CHGSTAT1);
179 if (regval & RT5036_CHGOPASTAT_MASK) {
183 rt5036_reg_read(ci->i2c,
188 regval &= RT5036_CHGICC_MASK;
189 regval >>= RT5036_CHGICC_SHIFT;
190 val->intval = 500 + regval * 100;
195 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
196 regval = rt5036_reg_read(ci->i2c, RT5036_REG_CHGCTL2);
200 regval &= RT5036_CHGCV_MASK;
201 regval >>= RT5036_CHGCV_SHIFT;
202 val->intval = regval * 25 + 3650;
205 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
208 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
217 static int rt_charger_set_property(struct power_supply *psy,
218 enum power_supply_property psp,
219 const union power_supply_propval *val)
221 struct rt5036_charger_info *ci = dev_get_drvdata(psy->dev->parent);
225 RTINFO("prop = %d, val->intval = %d\n", psp, val->intval);
227 case POWER_SUPPLY_PROP_ONLINE:
228 ci->online = val->intval;
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);
235 pval.intval = POWER_SUPPLY_STATUS_CHARGING;
236 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
238 power_supply_changed(psy);
240 dev_err(ci->dev, "couldn't get RT battery\n");
243 #ifdef CONFIG_RT9420_FUELGAUGE
244 union power_supply_propval pval;
245 struct power_supply *psy =
246 power_supply_get_by_name("rt-fuelgauge");
248 pval.intval = POWER_SUPPLY_STATUS_CHARGING;
249 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
251 power_supply_changed(psy);
254 "couldn't get rt fuelgauge battery\n");
257 union power_supply_propval pval;
258 struct power_supply *psy =
259 power_supply_get_by_name("battery");
261 pval.intval = POWER_SUPPLY_STATUS_CHARGING;
262 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
264 power_supply_changed(psy);
266 dev_err(ci->dev, "couldn't get battery\n");
268 #endif /* #ifdef CONFIG_RT9420_FUELGAUGE */
269 #endif /* #ifdef CONFIG_RT_BATTERY */
272 rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL1,
273 RT5036_CHGTEEN_MASK);
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);
280 pval.intval = POWER_SUPPLY_STATUS_DISCHARGING;
281 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
283 power_supply_changed(psy);
285 dev_err(ci->dev, "couldn't get RT battery\n");
288 #ifdef CONFIG_RT9420_FUELGAUGE
289 union power_supply_propval pval;
290 struct power_supply *psy =
291 power_supply_get_by_name("rt-fuelgauge");
293 pval.intval = POWER_SUPPLY_STATUS_DISCHARGING;
294 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
296 power_supply_changed(psy);
299 "couldn't get rt fuelgauge battery\n");
302 union power_supply_propval pval;
303 struct power_supply *psy =
304 power_supply_get_by_name("battery");
306 pval.intval = POWER_SUPPLY_STATUS_DISCHARGING;
307 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
309 power_supply_changed(psy);
311 dev_err(ci->dev, "couldn't get battery\n");
313 #endif /* #ifdef CONFIG_RT9420_FUELGAUGE */
314 #endif /* #ifdef CONFIG_RT_BATTERY */
317 rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL1,
318 RT5036_CHGTEEN_MASK);
319 /* te rst workaround */
320 rt5036_set_bits(ci->i2c, 0x20,
322 rt5036_clr_bits(ci->i2c, 0x20,
327 case POWER_SUPPLY_PROP_PRESENT:
328 if (ci->online && val->intval) {
330 union power_supply_propval pval;
332 if (ci->charge_cable == POWER_SUPPLY_TYPE_MAINS)
334 else if (ci->charge_cable == POWER_SUPPLY_TYPE_USB_DCP)
335 icc = ci->usbtachg_icc;
337 icc = ci->usbchg_icc;
340 psy->set_property(psy,
341 POWER_SUPPLY_PROP_CURRENT_NOW,
344 dev_err(ci->dev, "set final icc fail\n");
347 case POWER_SUPPLY_PROP_CHARGE_NOW:
348 ci->charge_cable = val->intval;
350 case POWER_SUPPLY_PROP_CURRENT_AVG:
351 if (val->intval <= 0)
353 else if (val->intval <= 100)
355 else if (val->intval <= 500)
357 else if (val->intval <= 700)
359 else if (val->intval <= 900)
361 else if (val->intval <= 1000)
363 else if (val->intval <= 1500)
365 else if (val->intval <= 2000)
370 ret = rt5036_assign_bits(ci->i2c, RT5036_REG_CHGCTL1,
373 RT5036_CHGAICR_SHIFT);
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);
382 psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE, &pval);
383 power_supply_changed(psy);
385 if (ci->charge_cable != POWER_SUPPLY_TYPE_MAINS) {
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,
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);
399 rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL1,
400 RT5036_CHGOPAMODE_MASK);
401 #endif /* #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI */
404 rt5036_clr_bits(ci->i2c, 0x23, 0x3);
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,
414 #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI
415 ret = rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL6, 0x20);
418 rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL1,
419 RT5036_CHGOPAMODE_MASK);
420 #endif /* #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI */
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);
435 rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL1,
436 RT5036_CHGOPAMODE_MASK);
437 #endif /* #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI */
440 #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI
441 ret = rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL6, 0x20);
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 */
449 } else if (val->intval < 500)
451 else if (val->intval > 2000)
454 regval = (val->intval - 500) / 100;
458 if (!ci->batabs && val->intval > 0)
460 rt5036_assign_bits(ci->i2c, RT5036_REG_CHGCTL5,
462 regval << RT5036_CHGICC_SHIFT);
463 rt5036_reg_write(ci->i2c, RT5036_REG_CHGCTL4,
466 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
467 if (val->intval < 3650)
469 else if (val->intval > 5225)
472 regval = (val->intval - 3650) / 25;
474 rt5036_assign_bits(ci->i2c, RT5036_REG_CHGCTL2,
476 regval << RT5036_CHGCV_SHIFT);
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:
488 static void rt5036_stat2alrt_irq_handler(void *info, int eventno)
490 struct rt5036_charger_info *ci = info;
491 unsigned char old_stat, new_stat;
494 old_stat = ci->stat2;
495 ret = rt5036_reg_read(ci->i2c, RT5036_REG_CHGSTAT2);
497 dev_err(ci->dev, "read stat io fail\n");
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);
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 */
515 psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE,
517 power_supply_changed(psy);
519 dev_err(ci->dev, "couldn't get RT usb\n");
521 #endif /* #ifndef CONFIG_RT_SUPPORT_ACUSB_DUALIN */
522 dev_info(ci->dev, "cable in\n");
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);
530 psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE,
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,
539 rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL1,
540 RT5036_CHGOPAMODE_MASK);
541 #endif /* #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI */
543 dev_err(ci->dev, "couldn't get RT usb\n");
545 #endif /* #ifndef CONFIG_RT_SUPPORT_ACUSB_DUALIN */
546 dev_info(ci->dev, "cable out\n");
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);
559 dev_info(ci->dev, "normal temperature\n");
560 rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL7,
561 RT5036_CCJEITA_MASK);
563 ci->stat2 = new_stat;
566 static void rt5036_batabs_irq_handler(void *info, int eventno)
568 struct rt5036_charger_info *ci = info;
569 struct power_supply *psy = &ci->psy;
570 union power_supply_propval val;
572 /*disable battery detection*/
573 ret = rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL6, RT5036_BATDEN_MASK);
575 dev_err(ci->dev, "set battary detection disable fail\n");
578 ret = psy->set_property(psy, POWER_SUPPLY_PROP_CURRENT_AVG, &val);
580 dev_err(ci->dev, "set aicr to 2000 fail\n");
583 ret = psy->set_property(psy, POWER_SUPPLY_PROP_CURRENT_NOW, &val);
585 dev_err(ci->dev, "set icc to 2000 fail\n");
586 /*set charger offline*/
588 ret = psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE, &val);
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);
595 ret = psy->set_property(psy, POWER_SUPPLY_PROP_PRESENT, &val);
597 dev_err(ci->dev, "set battery not present fail\n");
599 dev_err(ci->dev, "couldn't get batt psy\n");
602 #ifdef CONFIG_RT9420_FUELGAUGE
603 psy = power_supply_get_by_name("rt-fuelgauge");
606 ret = psy->set_property(psy, POWER_SUPPLY_PROP_PRESENT, &val);
608 dev_err(ci->dev, "set battery not present fail\n");
610 dev_err(ci->dev, "couldn't get rt fuelgauge psy\n");
613 psy = power_supply_get_by_name("battery");
616 ret = psy->set_property(psy, POWER_SUPPLY_PROP_PRESENT, &val);
618 dev_err(ci->dev, "set battery not present fail\n");
620 dev_err(ci->dev, "couldn't get battery psy\n");
622 #endif /* #ifdef CONFIG_RT9420_FUELGAUGE */
623 #endif /* #ifdef CONFIG_RT_BATTERY */
627 static void rt5036_general_irq_handler(void *info, int eventno)
629 struct rt5036_charger_info *ci = info;
631 RTINFO("eventno=%02d\n", eventno);
633 case CHGEVENT_CHTMRFI:
634 rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL3,
635 RT5036_CHGOTGEN_MASK);
637 rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL3,
638 RT5036_CHGOTGEN_MASK);
640 case CHGEVENT_CHRCHGI:
642 dev_warn(ci->dev, "recharge false alarm\n");
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;
650 pval.intval = POWER_SUPPLY_STATUS_CHARGING;
651 /*set battery status*/
652 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
654 power_supply_changed(psy);
656 dev_err(ci->dev, "couldn't get batt psy\n");
659 #ifdef CONFIG_RT9420_FUELGAUGE
660 struct power_supply *psy =
661 power_supply_get_by_name("rt-fuelgauge");
662 union power_supply_propval pval;
665 pval.intval = POWER_SUPPLY_STATUS_CHARGING;
666 /*set battery status*/
667 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
669 power_supply_changed(psy);
672 "couldn't get rt fuelgauge psy\n");
675 struct power_supply *psy =
676 power_supply_get_by_name("battery");
677 union power_supply_propval pval;
680 pval.intval = POWER_SUPPLY_STATUS_CHARGING;
681 /*set battery status*/
682 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
684 power_supply_changed(psy);
686 dev_err(ci->dev, "couldn't get battery psy\n");
688 #endif /* #ifdef CONFIG_RT9420_FUELGAUGE */
689 #endif /* #ifdfef CONFIG_RT_BATTERY */
690 dev_info(ci->dev, "recharge occur\n");
695 dev_warn(ci->dev, "eoc false alarm\n");
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;
703 pval.intval = POWER_SUPPLY_STATUS_FULL;
704 /*set battery status*/
705 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
707 power_supply_changed(psy);
709 dev_err(ci->dev, "couldn't get batt psy\n");
712 #ifdef CONFIG_RT9420_FUELGAUGE
713 struct power_supply *psy =
714 power_supply_get_by_name("rt-fuelgauge");
715 union power_supply_propval pval;
718 /*set battery status*/
719 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
721 power_supply_changed(psy);
724 "couldn't get rt fuelgauge psy\n");
727 struct power_supply *psy =
728 power_supply_get_by_name("battery");
729 union power_supply_propval pval;
732 pval.intval = POWER_SUPPLY_STATUS_FULL;
733 /*set battery status*/
734 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
736 power_supply_changed(psy);
738 dev_err(ci->dev, "couldn't get battery psy\n");
740 #endif /* #ifdef CONFIG_RT9420_FUELGAUGE */
741 #endif /* #ifdfef CONFIG_RT_BATTERY */
742 dev_info(ci->dev, "eoc really occur\n");
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,
769 void rt5036_charger_irq_handler(struct rt5036_charger_info *ci,
770 unsigned int irqevent)
773 unsigned int masked_irq_event =
774 (chg_init_regval[8] << 16) | (chg_init_regval[9] << 8) |
776 unsigned int final_irq_event = irqevent & (~masked_irq_event);
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);
783 EXPORT_SYMBOL(rt5036_charger_irq_handler);
785 #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN
786 static irqreturn_t rt5036_acdet_irq_handler(int irqno, void *param)
788 struct rt5036_charger_info *ci = param;
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);
794 if (RT5036_ACIN_LEVEL)
795 irq_set_irq_type(ci->acdet_irq, IRQF_TRIGGER_FALLING);
797 irq_set_irq_type(ci->acdet_irq, IRQF_TRIGGER_RISING);
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);
805 rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL1,
806 RT5036_CHGOPAMODE_MASK);
807 #endif /* #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI */
809 psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE, &pval);
810 power_supply_changed(psy);
812 dev_err(ci->dev, "couldn't get RT ac\n");
814 dev_info(ci->dev, "ac in\n");
816 union power_supply_propval pval;
817 struct power_supply *psy = power_supply_get_by_name(RT_AC_NAME);
819 if (RT5036_ACIN_LEVEL)
820 irq_set_irq_type(ci->acdet_irq, IRQF_TRIGGER_RISING);
822 irq_set_irq_type(ci->acdet_irq, IRQF_TRIGGER_FALLING);
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) {
830 rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL6, 0x20);
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,
837 rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL1,
838 RT5036_CHGOPAMODE_MASK);
843 pval.intval = ci->otg_volt;
844 ci->psy.set_property(&ci->psy,
845 POWER_SUPPLY_PROP_VOLTAGE_NOW,
847 rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL1,
848 RT5036_CHGOPAMODE_MASK);
850 #endif /* #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI */
852 dev_err(ci->dev, "couldn't get RT ac\n");
854 dev_info(ci->dev, "ac out\n");
859 static irqreturn_t rt5036_usbdet_irq_handler(int irqno, void *param)
861 struct rt5036_charger_info *ci = param;
864 dev_info(ci->dev, "currently in otg mode\n");
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);
874 irq_set_irq_type(ci->usbdet_irq, IRQF_TRIGGER_RISING);
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 */
882 psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE, &pval);
883 power_supply_changed(psy);
885 dev_err(ci->dev, "couldn't get RT usb\n");
887 dev_info(ci->dev, "usb in\n");
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);
895 irq_set_irq_type(ci->usbdet_irq, IRQF_TRIGGER_FALLING);
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,
907 rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL1,
908 RT5036_CHGOPAMODE_MASK);
910 #endif /* #ifdef CONFIG_CHARGER_RT5036_VMID_HDMI */
912 dev_err(ci->dev, "couldn't get RT usb\n");
914 dev_info(ci->dev, "usb out\n");
920 static int rt5036_acusb_irqinit(struct rt5036_charger_info *ci)
924 if (gpio_is_valid(ci->acdet_gpio) && gpio_is_valid(ci->usbdet_gpio)) {
925 rc = gpio_request(ci->acdet_gpio, "rt5036_acdet_gpio");
927 dev_err(ci->dev, "request acdet gpio fail\n");
930 rc = gpio_request(ci->usbdet_gpio, "rt5036_usbdet_gpio");
932 dev_err(ci->dev, "request usbdet gpio fail\n");
935 gpio_direction_input(ci->acdet_gpio);
936 gpio_direction_input(ci->usbdet_gpio);
938 ci->acdet_irq = gpio_to_irq(ci->acdet_gpio);
939 ci->usbdet_irq = gpio_to_irq(ci->usbdet_gpio);
941 rc = devm_request_threaded_irq(ci->dev, ci->acdet_irq, NULL,
942 rt5036_acdet_irq_handler,
943 IRQF_TRIGGER_FALLING |
945 "rt5036_acdet_irq", ci);
947 dev_err(ci->dev, "request acdet irq fail\n");
950 rc = devm_request_threaded_irq(ci->dev, ci->usbdet_irq, NULL,
951 rt5036_usbdet_irq_handler,
952 IRQF_TRIGGER_FALLING |
954 "rt5036_usbdet_irq", ci);
956 dev_err(ci->dev, "request usbdet irq fail\n");
959 enable_irq_wake(ci->acdet_irq);
960 enable_irq_wake(ci->usbdet_irq);
968 static void rt5036_acusb_irqdeinit(struct rt5036_charger_info *ci)
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);
975 #endif /* #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN */
977 static void rt5036_chg_dwork_func(struct work_struct *work)
979 struct rt5036_charger_info *ci =
980 container_of(work, struct rt5036_charger_info,
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 */
989 static int rt5036_charger_reginit(struct i2c_client *client)
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);
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 */
1017 static int rt_parse_dt(struct rt5036_charger_info *ci,
1021 struct device_node *np = dev->of_node;
1024 if (of_property_read_bool(np, "rt,te_en"))
1027 if (of_property_read_u32(np, "rt,iprec", &val)) {
1028 dev_info(dev, "no iprec property, use the default value\n");
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);
1036 if (of_property_read_u32(np, "rt,ieoc", &val)) {
1037 dev_info(dev, "no ieoc property, use the default value\n");
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;
1045 if (of_property_read_u32(np, "rt,vprec", &val)) {
1046 dev_info(dev, "no vprec property, use the default value\n");
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;
1054 if (of_property_read_u32(np, "rt,batlv", &val)) {
1055 dev_info(dev, "no batlv property, use the default value\n");
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;
1063 if (of_property_read_u32(np, "rt,vrechg", &val)) {
1064 dev_info(dev, "no vrechg property, use the default value\n");
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);
1072 if (of_property_read_u32(np, "rt,chg_volt", &val)) {
1074 "no chg_volt property, use 4200 as the default value\n");
1075 ci->chg_volt = 4200;
1080 if (of_property_read_u32(np, "rt,otg_volt", &val)) {
1082 "no otg_volt property, use 5025 as the default value\n");
1083 ci->otg_volt = 5025;
1088 if (of_property_read_u32(np, "rt,acchg_icc", &val)) {
1090 "no acchg_icc property, use 2000 as the default value\n");
1091 ci->acchg_icc = 2000;
1093 ci->acchg_icc = val;
1096 if (of_property_read_u32(np, "rt,usbtachg_icc", &val)) {
1098 "no usbtachg_icc property, use 2000 as the default value\n");
1099 ci->usbtachg_icc = 2000;
1101 ci->usbtachg_icc = val;
1104 if (of_property_read_u32(np, "rt,usbchg_icc", &val)) {
1106 "no usbchg_icc property, use 500 as the default value\n");
1107 ci->usbchg_icc = 500;
1109 ci->usbchg_icc = val;
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);
1122 static int rt_parse_pdata(struct rt5036_charger_info *ci,
1125 struct rt5036_chg_data *pdata = dev->platform_data;
1130 chg_init_regval[4] &= (~RT5036_CHGIPREC_MASK);
1131 chg_init_regval[4] |= (pdata->iprec << RT5036_CHGIPREC_SHIFT);
1133 chg_init_regval[4] &= (~RT5036_CHGIEOC_MASK);
1134 chg_init_regval[4] |= pdata->ieoc;
1136 chg_init_regval[5] &= (~RT5036_CHGVPREC_MASK);
1137 chg_init_regval[5] |= pdata->vprec;
1139 chg_init_regval[6] &= (~RT5036_CHGBATLV_MASK);
1140 chg_init_regval[6] |= pdata->batlv;
1142 chg_init_regval[7] &= (~RT5036_CHGVRECHG_MASK);
1143 chg_init_regval[7] |= (pdata->vrechg << RT5036_CHGVRECHG_SHIFT);
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);
1159 #ifdef CONFIG_RT_POWER
1160 static struct platform_device rt_power_dev = {
1164 #endif /* #ifdef CONFIG_RT_POWER */
1166 #ifdef CONFIG_RT_BATTERY
1167 static struct platform_device rt_battery_dev = {
1168 .name = "rt-battery",
1171 #endif /* #ifdef CONFIG_RT_BATTERY */
1173 static int rt5036_charger_probe(struct platform_device *pdev)
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;
1184 ci = devm_kzalloc(&pdev->dev, sizeof(*ci), GFP_KERNEL);
1188 ci->i2c = chip->i2c;
1190 rt_parse_dt(ci, &pdev->dev);
1193 dev_err(&pdev->dev, "platform data invalid\n");
1197 pdev->dev.platform_data = pdata->chg_pdata;
1198 rt_parse_pdata(ci, &pdev->dev);
1201 ci->dev = &pdev->dev;
1202 INIT_DELAYED_WORK(&ci->dwork, rt5036_chg_dwork_func);
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;
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);
1220 "couldn't create power supply for rt-charger\n");
1223 #ifdef CONFIG_RT_BATTERY
1224 rt_battery_dev.dev.parent = &pdev->dev;
1225 ret = platform_device_register(&rt_battery_dev);
1228 #endif /* #ifdef CONFIG_RT_BATTERY */
1230 #ifdef CONFIG_RT_POWER
1232 devm_kzalloc(&pdev->dev, sizeof(*rt_power_pdata), GFP_KERNEL);
1233 if (!rt_power_pdata) {
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;
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);
1247 #endif /* #ifdef CONFIG_RT_POWER */
1249 #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN
1250 ret = rt5036_acusb_irqinit(ci);
1253 #endif /* #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN */
1255 schedule_delayed_work(&ci->dwork, msecs_to_jiffies(100));
1256 chip->chg_info = ci;
1257 dev_info(&pdev->dev, "driver successfully loaded\n");
1259 #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN
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
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);
1275 static int rt5036_charger_remove(struct platform_device *pdev)
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);
1291 static const struct of_device_id rt_match_table[] = {
1292 {.compatible = "rt,rt5036-charger",},
1296 static struct platform_driver rt5036_charger_driver = {
1298 .name = RT5036_DEV_NAME "-charger",
1299 .owner = THIS_MODULE,
1300 .of_match_table = rt_match_table,
1302 .probe = rt5036_charger_probe,
1303 .remove = rt5036_charger_remove,
1306 static int __init rt5036_charger_init(void)
1308 return platform_driver_register(&rt5036_charger_driver);
1310 subsys_initcall(rt5036_charger_init);
1312 static void __exit rt5036_charger_exit(void)
1314 platform_driver_unregister(&rt5036_charger_driver);
1316 module_exit(rt5036_charger_exit);
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);