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[] = {
60 static char *rtdef_chg_name = "rt-charger";
62 static char *rt_charger_supply_list[] = {
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,
79 static int rt_charger_get_property(struct power_supply *psy,
80 enum power_supply_property psp,
81 union power_supply_propval *val)
83 struct rt5036_charger_info *ci = dev_get_drvdata(psy->dev->parent);
88 case POWER_SUPPLY_PROP_ONLINE:
89 val->intval = ci->online;
91 case POWER_SUPPLY_PROP_STATUS:
92 regval = rt5036_reg_read(ci->i2c, RT5036_REG_CHGSTAT1);
96 regval &= RT5036_CHGSTAT_MASK;
97 regval >>= RT5036_CHGSTAT_SHIFT;
100 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
103 val->intval = POWER_SUPPLY_STATUS_CHARGING;
106 val->intval = POWER_SUPPLY_STATUS_FULL;
109 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
114 case POWER_SUPPLY_PROP_PRESENT:
115 regval = rt5036_reg_read(ci->i2c, RT5036_REG_CHGSTAT1);
119 if (regval & RT5036_CHGDIS_MASK)
125 case POWER_SUPPLY_PROP_CHARGE_NOW:
126 val->intval = ci->charge_cable;
128 case POWER_SUPPLY_PROP_CURRENT_MAX:
131 case POWER_SUPPLY_PROP_CURRENT_AVG:
132 regval = rt5036_reg_read(ci->i2c, RT5036_REG_CHGCTL1);
136 regval &= RT5036_CHGAICR_MASK;
137 regval >>= RT5036_CHGAICR_SHIFT;
166 case POWER_SUPPLY_PROP_CURRENT_NOW:
167 regval = rt5036_reg_read(ci->i2c, RT5036_REG_CHGSTAT1);
171 if (regval & RT5036_CHGOPASTAT_MASK) {
175 rt5036_reg_read(ci->i2c,
180 regval &= RT5036_CHGICC_MASK;
181 regval >>= RT5036_CHGICC_SHIFT;
182 val->intval = 500 + regval * 100;
187 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
188 regval = rt5036_reg_read(ci->i2c, RT5036_REG_CHGCTL2);
192 regval &= RT5036_CHGCV_MASK;
193 regval >>= RT5036_CHGCV_SHIFT;
194 val->intval = regval * 25 + 3650;
197 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
200 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
209 static int rt_charger_set_property(struct power_supply *psy,
210 enum power_supply_property psp,
211 const union power_supply_propval *val)
213 struct rt5036_charger_info *ci = dev_get_drvdata(psy->dev->parent);
217 RTINFO("prop = %d, val->intval = %d\n", psp, val->intval);
219 case POWER_SUPPLY_PROP_ONLINE:
220 ci->online = val->intval;
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);
227 pval.intval = POWER_SUPPLY_STATUS_CHARGING;
228 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
230 power_supply_changed(psy);
232 dev_err(ci->dev, "couldn't get RT battery\n");
235 #ifdef CONFIG_RT9420_FUELGAUGE
236 union power_supply_propval pval;
237 struct power_supply *psy =
238 power_supply_get_by_name("rt-fuelgauge");
240 pval.intval = POWER_SUPPLY_STATUS_CHARGING;
241 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
243 power_supply_changed(psy);
246 "couldn't get rt fuelgauge battery\n");
249 union power_supply_propval pval;
250 struct power_supply *psy =
251 power_supply_get_by_name("battery");
253 pval.intval = POWER_SUPPLY_STATUS_CHARGING;
254 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
256 power_supply_changed(psy);
258 dev_err(ci->dev, "couldn't get battery\n");
260 #endif /* #ifdef CONFIG_RT9420_FUELGAUGE */
261 #endif /* #ifdef CONFIG_RT_BATTERY */
264 rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL1,
265 RT5036_CHGTEEN_MASK);
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);
272 pval.intval = POWER_SUPPLY_STATUS_DISCHARGING;
273 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
275 power_supply_changed(psy);
277 dev_err(ci->dev, "couldn't get RT battery\n");
280 #ifdef CONFIG_RT9420_FUELGAUGE
281 union power_supply_propval pval;
282 struct power_supply *psy =
283 power_supply_get_by_name("rt-fuelgauge");
285 pval.intval = POWER_SUPPLY_STATUS_DISCHARGING;
286 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
288 power_supply_changed(psy);
291 "couldn't get rt fuelgauge battery\n");
294 union power_supply_propval pval;
295 struct power_supply *psy =
296 power_supply_get_by_name("battery");
298 pval.intval = POWER_SUPPLY_STATUS_DISCHARGING;
299 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
301 power_supply_changed(psy);
303 dev_err(ci->dev, "couldn't get battery\n");
305 #endif /* #ifdef CONFIG_RT9420_FUELGAUGE */
306 #endif /* #ifdef CONFIG_RT_BATTERY */
309 rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL1,
310 RT5036_CHGTEEN_MASK);
311 /* te rst workaround */
312 rt5036_set_bits(ci->i2c, 0x20,
314 rt5036_clr_bits(ci->i2c, 0x20,
319 case POWER_SUPPLY_PROP_PRESENT:
320 if (ci->online && val->intval) {
322 union power_supply_propval pval;
324 if (ci->charge_cable == POWER_SUPPLY_TYPE_MAINS)
326 else if (ci->charge_cable == POWER_SUPPLY_TYPE_USB_DCP)
327 icc = ci->usbtachg_icc;
329 icc = ci->usbchg_icc;
332 psy->set_property(psy,
333 POWER_SUPPLY_PROP_CURRENT_NOW,
336 dev_err(ci->dev, "set final icc fail\n");
339 case POWER_SUPPLY_PROP_CHARGE_NOW:
340 ci->charge_cable = val->intval;
342 case POWER_SUPPLY_PROP_CURRENT_AVG:
343 if (val->intval <= 0)
345 else if (val->intval <= 100)
347 else if (val->intval <= 500)
349 else if (val->intval <= 700)
351 else if (val->intval <= 900)
353 else if (val->intval <= 1000)
355 else if (val->intval <= 1500)
357 else if (val->intval <= 2000)
362 ret = rt5036_assign_bits(ci->i2c, RT5036_REG_CHGCTL1,
365 RT5036_CHGAICR_SHIFT);
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);
374 psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE, &pval);
375 power_supply_changed(psy);
377 if (ci->charge_cable != POWER_SUPPLY_TYPE_MAINS) {
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,
385 rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL1,
386 RT5036_CHGOPAMODE_MASK);
389 rt5036_clr_bits(ci->i2c, 0x23, 0x3);
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,
400 rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL1,
401 RT5036_CHGOPAMODE_MASK);
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)
410 rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL1,
411 RT5036_CHGOPAMODE_MASK);
414 rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL1,
415 RT5036_CHGOPAMODE_MASK);
416 #endif /* #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN */
418 } else if (val->intval < 500)
420 else if (val->intval > 2000)
423 regval = (val->intval - 500) / 100;
427 if (!ci->batabs && val->intval > 0)
429 rt5036_assign_bits(ci->i2c, RT5036_REG_CHGCTL5,
431 regval << RT5036_CHGICC_SHIFT);
432 rt5036_reg_write(ci->i2c, RT5036_REG_CHGCTL4,
435 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
436 if (val->intval < 3650)
438 else if (val->intval > 5225)
441 regval = (val->intval - 3650) / 25;
443 rt5036_assign_bits(ci->i2c, RT5036_REG_CHGCTL2,
445 regval << RT5036_CHGCV_SHIFT);
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:
457 static void rt5036_stat2alrt_irq_handler(void *info, int eventno)
459 struct rt5036_charger_info *ci = info;
460 unsigned char old_stat, new_stat;
463 old_stat = ci->stat2;
464 ret = rt5036_reg_read(ci->i2c, RT5036_REG_CHGSTAT2);
466 dev_err(ci->dev, "read stat io fail\n");
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);
479 psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE,
481 power_supply_changed(psy);
483 dev_err(ci->dev, "couldn't get RT usb\n");
485 #endif /* #ifndef CONFIG_RT_SUPPORT_ACUSB_DUALIN */
486 dev_info(ci->dev, "cable in\n");
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);
494 psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE,
496 power_supply_changed(psy);
498 dev_err(ci->dev, "couldn't get RT usb\n");
500 #endif /* #ifndef CONFIG_RT_SUPPORT_ACUSB_DUALIN */
501 dev_info(ci->dev, "cable out\n");
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);
515 dev_info(ci->dev, "normal temperature\n");
516 rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL7,
517 RT5036_CCJEITA_MASK);
519 ci->stat2 = new_stat;
522 static void rt5036_batabs_irq_handler(void *info, int eventno)
524 struct rt5036_charger_info *ci = info;
525 struct power_supply *psy = &ci->psy;
526 union power_supply_propval val;
528 /*disable battery detection*/
529 ret = rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL6, RT5036_BATDEN_MASK);
531 dev_err(ci->dev, "set battary detection disable fail\n");
534 ret = psy->set_property(psy, POWER_SUPPLY_PROP_CURRENT_AVG, &val);
536 dev_err(ci->dev, "set aicr to 2000 fail\n");
539 ret = psy->set_property(psy, POWER_SUPPLY_PROP_CURRENT_NOW, &val);
541 dev_err(ci->dev, "set icc to 2000 fail\n");
542 /*set charger offline*/
544 ret = psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE, &val);
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);
551 ret = psy->set_property(psy, POWER_SUPPLY_PROP_PRESENT, &val);
553 dev_err(ci->dev, "set battery not present fail\n");
555 dev_err(ci->dev, "couldn't get batt psy\n");
558 #ifdef CONFIG_RT9420_FUELGAUGE
559 psy = power_supply_get_by_name("rt-fuelgauge");
562 ret = psy->set_property(psy, POWER_SUPPLY_PROP_PRESENT, &val);
564 dev_err(ci->dev, "set battery not present fail\n");
566 dev_err(ci->dev, "couldn't get rt fuelgauge psy\n");
569 psy = power_supply_get_by_name("battery");
572 ret = psy->set_property(psy, POWER_SUPPLY_PROP_PRESENT, &val);
574 dev_err(ci->dev, "set battery not present fail\n");
576 dev_err(ci->dev, "couldn't get battery psy\n");
578 #endif /* #ifdef CONFIG_RT9420_FUELGAUGE */
579 #endif /* #ifdef CONFIG_RT_BATTERY */
583 static void rt5036_general_irq_handler(void *info, int eventno)
585 struct rt5036_charger_info *ci = info;
587 RTINFO("eventno=%02d\n", eventno);
589 case CHGEVENT_CHTMRFI:
590 rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL3,
591 RT5036_CHGOTGEN_MASK);
593 rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL3,
594 RT5036_CHGOTGEN_MASK);
596 case CHGEVENT_CHRCHGI:
598 dev_warn(ci->dev, "recharge false alarm\n");
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;
606 pval.intval = POWER_SUPPLY_STATUS_CHARGING;
607 /*set battery status*/
608 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
610 power_supply_changed(psy);
612 dev_err(ci->dev, "couldn't get batt psy\n");
615 #ifdef CONFIG_RT9420_FUELGAUGE
616 struct power_supply *psy =
617 power_supply_get_by_name("rt-fuelgauge");
618 union power_supply_propval pval;
621 pval.intval = POWER_SUPPLY_STATUS_CHARGING;
622 /*set battery status*/
623 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
625 power_supply_changed(psy);
628 "couldn't get rt fuelgauge psy\n");
631 struct power_supply *psy =
632 power_supply_get_by_name("battery");
633 union power_supply_propval pval;
636 pval.intval = POWER_SUPPLY_STATUS_CHARGING;
637 /*set battery status*/
638 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
640 power_supply_changed(psy);
642 dev_err(ci->dev, "couldn't get battery psy\n");
644 #endif /* #ifdef CONFIG_RT9420_FUELGAUGE */
645 #endif /* #ifdfef CONFIG_RT_BATTERY */
646 dev_info(ci->dev, "recharge occur\n");
651 dev_warn(ci->dev, "eoc false alarm\n");
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;
659 pval.intval = POWER_SUPPLY_STATUS_FULL;
660 /*set battery status*/
661 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
663 power_supply_changed(psy);
665 dev_err(ci->dev, "couldn't get batt psy\n");
668 #ifdef CONFIG_RT9420_FUELGAUGE
669 struct power_supply *psy =
670 power_supply_get_by_name("rt-fuelgauge");
671 union power_supply_propval pval;
674 /*set battery status*/
675 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
677 power_supply_changed(psy);
680 "couldn't get rt fuelgauge psy\n");
683 struct power_supply *psy =
684 power_supply_get_by_name("battery");
685 union power_supply_propval pval;
688 pval.intval = POWER_SUPPLY_STATUS_FULL;
689 /*set battery status*/
690 psy->set_property(psy, POWER_SUPPLY_PROP_STATUS,
692 power_supply_changed(psy);
694 dev_err(ci->dev, "couldn't get battery psy\n");
696 #endif /* #ifdef CONFIG_RT9420_FUELGAUGE */
697 #endif /* #ifdfef CONFIG_RT_BATTERY */
698 dev_info(ci->dev, "eoc really occur\n");
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,
725 void rt5036_charger_irq_handler(struct rt5036_charger_info *ci,
726 unsigned int irqevent)
729 unsigned int masked_irq_event =
730 (chg_init_regval[8] << 16) | (chg_init_regval[9] << 8) |
732 unsigned int final_irq_event = irqevent & (~masked_irq_event);
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);
739 EXPORT_SYMBOL(rt5036_charger_irq_handler);
741 #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN
742 static irqreturn_t rt5036_acdet_irq_handler(int irqno, void *param)
744 struct rt5036_charger_info *ci = param;
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);
750 if (RT5036_ACIN_LEVEL)
751 irq_set_irq_type(ci->acdet_irq, IRQF_TRIGGER_FALLING);
753 irq_set_irq_type(ci->acdet_irq, IRQF_TRIGGER_RISING);
756 rt5036_clr_bits(ci->i2c, RT5036_REG_CHGCTL1,
757 RT5036_CHGOPAMODE_MASK);
759 psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE, &pval);
760 power_supply_changed(psy);
762 dev_err(ci->dev, "couldn't get RT ac\n");
764 dev_info(ci->dev, "ac in\n");
766 union power_supply_propval pval;
767 struct power_supply *psy = power_supply_get_by_name(RT_AC_NAME);
769 if (RT5036_ACIN_LEVEL)
770 irq_set_irq_type(ci->acdet_irq, IRQF_TRIGGER_RISING);
772 irq_set_irq_type(ci->acdet_irq, IRQF_TRIGGER_FALLING);
775 psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE, &pval);
776 power_supply_changed(psy);
779 pval.intval = ci->otg_volt;
780 ci->psy.set_property(&ci->psy,
781 POWER_SUPPLY_PROP_VOLTAGE_NOW,
783 rt5036_set_bits(ci->i2c, RT5036_REG_CHGCTL1,
784 RT5036_CHGOPAMODE_MASK);
787 dev_err(ci->dev, "couldn't get RT ac\n");
789 dev_info(ci->dev, "ac out\n");
794 static irqreturn_t rt5036_usbdet_irq_handler(int irqno, void *param)
796 struct rt5036_charger_info *ci = param;
799 dev_info(ci->dev, "currently in otg mode\n");
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);
809 irq_set_irq_type(ci->usbdet_irq, IRQF_TRIGGER_RISING);
812 psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE, &pval);
813 power_supply_changed(psy);
815 dev_err(ci->dev, "couldn't get RT usb\n");
817 dev_info(ci->dev, "usb in\n");
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);
825 irq_set_irq_type(ci->usbdet_irq, IRQF_TRIGGER_FALLING);
828 psy->set_property(psy, POWER_SUPPLY_PROP_ONLINE, &pval);
829 power_supply_changed(psy);
831 dev_err(ci->dev, "couldn't get RT usb\n");
833 dev_info(ci->dev, "usb out\n");
839 static int rt5036_acusb_irqinit(struct rt5036_charger_info *ci)
843 if (gpio_is_valid(ci->acdet_gpio) && gpio_is_valid(ci->usbdet_gpio)) {
844 rc = gpio_request(ci->acdet_gpio, "rt5036_acdet_gpio");
846 dev_err(ci->dev, "request acdet gpio fail\n");
849 rc = gpio_request(ci->usbdet_gpio, "rt5036_usbdet_gpio");
851 dev_err(ci->dev, "request usbdet gpio fail\n");
854 gpio_direction_input(ci->acdet_gpio);
855 gpio_direction_input(ci->usbdet_gpio);
857 ci->acdet_irq = gpio_to_irq(ci->acdet_gpio);
858 ci->usbdet_irq = gpio_to_irq(ci->usbdet_gpio);
860 rc = devm_request_threaded_irq(ci->dev, ci->acdet_irq, NULL,
861 rt5036_acdet_irq_handler,
862 IRQF_TRIGGER_FALLING |
864 "rt5036_acdet_irq", ci);
866 dev_err(ci->dev, "request acdet irq fail\n");
869 rc = devm_request_threaded_irq(ci->dev, ci->usbdet_irq, NULL,
870 rt5036_usbdet_irq_handler,
871 IRQF_TRIGGER_FALLING |
873 "rt5036_usbdet_irq", ci);
875 dev_err(ci->dev, "request usbdet irq fail\n");
878 enable_irq_wake(ci->acdet_irq);
879 enable_irq_wake(ci->usbdet_irq);
887 static void rt5036_acusb_irqdeinit(struct rt5036_charger_info *ci)
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);
894 #endif /* #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN */
896 static void rt5036_chg_dwork_func(struct work_struct *work)
898 struct rt5036_charger_info *ci =
899 container_of(work, struct rt5036_charger_info,
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 */
908 static int rt5036_charger_reginit(struct i2c_client *client)
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);
929 static int rt_parse_dt(struct rt5036_charger_info *ci,
933 struct device_node *np = dev->of_node;
936 if (of_property_read_bool(np, "rt,te_en"))
939 if (of_property_read_u32(np, "rt,iprec", &val)) {
940 dev_info(dev, "no iprec property, use the default value\n");
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);
948 if (of_property_read_u32(np, "rt,ieoc", &val)) {
949 dev_info(dev, "no ieoc property, use the default value\n");
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;
957 if (of_property_read_u32(np, "rt,vprec", &val)) {
958 dev_info(dev, "no vprec property, use the default value\n");
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;
966 if (of_property_read_u32(np, "rt,batlv", &val)) {
967 dev_info(dev, "no batlv property, use the default value\n");
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;
975 if (of_property_read_u32(np, "rt,vrechg", &val)) {
976 dev_info(dev, "no vrechg property, use the default value\n");
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);
984 if (of_property_read_u32(np, "rt,chg_volt", &val)) {
986 "no chg_volt property, use 4200 as the default value\n");
992 if (of_property_read_u32(np, "rt,otg_volt", &val)) {
994 "no otg_volt property, use 5025 as the default value\n");
1000 if (of_property_read_u32(np, "rt,acchg_icc", &val)) {
1002 "no acchg_icc property, use 2000 as the default value\n");
1003 ci->acchg_icc = 2000;
1005 ci->acchg_icc = val;
1008 if (of_property_read_u32(np, "rt,usbtachg_icc", &val)) {
1010 "no usbtachg_icc property, use 2000 as the default value\n");
1011 ci->usbtachg_icc = 2000;
1013 ci->usbtachg_icc = val;
1016 if (of_property_read_u32(np, "rt,usbchg_icc", &val)) {
1018 "no usbchg_icc property, use 500 as the default value\n");
1019 ci->usbchg_icc = 500;
1021 ci->usbchg_icc = val;
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);
1034 static int rt_parse_pdata(struct rt5036_charger_info *ci,
1037 struct rt5036_chg_data *pdata = dev->platform_data;
1042 chg_init_regval[4] &= (~RT5036_CHGIPREC_MASK);
1043 chg_init_regval[4] |= (pdata->iprec << RT5036_CHGIPREC_SHIFT);
1045 chg_init_regval[4] &= (~RT5036_CHGIEOC_MASK);
1046 chg_init_regval[4] |= pdata->ieoc;
1048 chg_init_regval[5] &= (~RT5036_CHGVPREC_MASK);
1049 chg_init_regval[5] |= pdata->vprec;
1051 chg_init_regval[6] &= (~RT5036_CHGBATLV_MASK);
1052 chg_init_regval[6] |= pdata->batlv;
1054 chg_init_regval[7] &= (~RT5036_CHGVRECHG_MASK);
1055 chg_init_regval[7] |= (pdata->vrechg << RT5036_CHGVRECHG_SHIFT);
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);
1071 #ifdef CONFIG_RT_POWER
1072 static struct platform_device rt_power_dev = {
1076 #endif /* #ifdef CONFIG_RT_POWER */
1078 #ifdef CONFIG_RT_BATTERY
1079 static struct platform_device rt_battery_dev = {
1080 .name = "rt-battery",
1083 #endif /* #ifdef CONFIG_RT_BATTERY */
1085 static int rt5036_charger_probe(struct platform_device *pdev)
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;
1096 ci = devm_kzalloc(&pdev->dev, sizeof(*ci), GFP_KERNEL);
1100 ci->i2c = chip->i2c;
1102 rt_parse_dt(ci, &pdev->dev);
1105 dev_err(&pdev->dev, "platform data invalid\n");
1109 pdev->dev.platform_data = pdata->chg_pdata;
1110 rt_parse_pdata(ci, &pdev->dev);
1113 ci->dev = &pdev->dev;
1114 INIT_DELAYED_WORK(&ci->dwork, rt5036_chg_dwork_func);
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;
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);
1132 "couldn't create power supply for rt-charger\n");
1135 #ifdef CONFIG_RT_BATTERY
1136 rt_battery_dev.dev.parent = &pdev->dev;
1137 ret = platform_device_register(&rt_battery_dev);
1140 #endif /* #ifdef CONFIG_RT_BATTERY */
1142 #ifdef CONFIG_RT_POWER
1144 devm_kzalloc(&pdev->dev, sizeof(*rt_power_pdata), GFP_KERNEL);
1145 if (!rt_power_pdata) {
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;
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);
1159 #endif /* #ifdef CONFIG_RT_POWER */
1161 #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN
1162 ret = rt5036_acusb_irqinit(ci);
1165 #endif /* #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN */
1167 schedule_delayed_work(&ci->dwork, msecs_to_jiffies(100));
1168 chip->chg_info = ci;
1169 dev_info(&pdev->dev, "driver successfully loaded\n");
1171 #ifdef CONFIG_RT_SUPPORT_ACUSB_DUALIN
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
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);
1187 static int rt5036_charger_remove(struct platform_device *pdev)
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);
1203 static const struct of_device_id rt_match_table[] = {
1204 {.compatible = "rt,rt5036-charger",},
1208 static struct platform_driver rt5036_charger_driver = {
1210 .name = RT5036_DEV_NAME "-charger",
1211 .owner = THIS_MODULE,
1212 .of_match_table = rt_match_table,
1214 .probe = rt5036_charger_probe,
1215 .remove = rt5036_charger_remove,
1218 static int __init rt5036_charger_init(void)
1220 return platform_driver_register(&rt5036_charger_driver);
1222 subsys_initcall(rt5036_charger_init);
1224 static void __exit rt5036_charger_exit(void)
1226 platform_driver_unregister(&rt5036_charger_driver);
1228 module_exit(rt5036_charger_exit);
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);