power: rk81x-battery: get charger type by usb notifier instead of polling
[firefly-linux-kernel-4.4.55.git] / drivers / power / rk29_charger_display.c
1          
2 #include <linux/module.h>
3 #include <linux/init.h>
4 #include <linux/slab.h>
5 #include <linux/kernel.h>
6 #include <linux/errno.h>
7 #include <linux/device.h>
8 #include <linux/delay.h>
9 #include <linux/power_supply.h>
10 #include <linux/workqueue.h>
11 #include <linux/reboot.h>
12 #include <asm/unaligned.h>
13 #include <asm/uaccess.h>
14 #include <linux/power_supply.h>
15 #include <linux/rockchip/common.h>
16
17 #if 0
18 #define DBG(x...)       printk(KERN_INFO x)
19 #else
20 #define DBG(x...)
21 #endif
22
23 static int pwr_on_thrsd = 5;          //power on capcity threshold
24
25 static int __init pwr_on_thrsd_setup(char *str)
26 {
27
28         pwr_on_thrsd = simple_strtol(str,NULL,10);
29         printk(KERN_INFO "power on threshold:%d",pwr_on_thrsd);
30         return 0;
31 }
32
33 __setup("pwr_on_thrsd=", pwr_on_thrsd_setup);
34
35 static int usb_status;
36 static int ac_status;
37 static int __rk_get_system_battery_status(struct device *dev, void *data)
38 {
39         union power_supply_propval val_status = {POWER_SUPPLY_STATUS_DISCHARGING};
40         struct power_supply *psy = dev_get_drvdata(dev);
41
42         psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &val_status);
43
44         if (val_status.intval != 0) {
45                 if (psy->type == POWER_SUPPLY_TYPE_USB)
46                         usb_status = POWER_SUPPLY_TYPE_USB;
47                 if (psy->type == POWER_SUPPLY_TYPE_MAINS)
48                         ac_status = POWER_SUPPLY_TYPE_MAINS;
49         }
50
51         return 0;
52 }
53
54 // POWER_SUPPLY_TYPE_BATTERY --- discharge
55 // POWER_SUPPLY_TYPE_USB     --- usb_charging
56 // POWER_SUPPLY_TYPE_MAINS   --- AC_charging
57 int rk_get_system_battery_status(void)
58 {
59         class_for_each_device(power_supply_class, NULL, NULL, __rk_get_system_battery_status);
60
61         if (ac_status == POWER_SUPPLY_TYPE_MAINS) {
62                 return POWER_SUPPLY_TYPE_MAINS;
63         } else if (usb_status == POWER_SUPPLY_TYPE_USB) {
64                 return POWER_SUPPLY_TYPE_USB;
65         }
66
67         return POWER_SUPPLY_TYPE_BATTERY;
68 }
69 EXPORT_SYMBOL(rk_get_system_battery_status);
70
71 static union power_supply_propval battery_capacity = { 100 };
72 static int __rk_get_system_battery_capacity(struct device *dev, void *data)
73 {
74         struct power_supply *psy = dev_get_drvdata(dev);
75
76         psy->get_property(psy, POWER_SUPPLY_PROP_CAPACITY, &battery_capacity);
77
78         return 0;
79 }
80
81 int rk_get_system_battery_capacity(void)
82 {
83         class_for_each_device(power_supply_class, NULL, NULL, __rk_get_system_battery_capacity);
84
85         return battery_capacity.intval;
86 }
87 EXPORT_SYMBOL(rk_get_system_battery_capacity);
88
89 #ifdef CONFIG_CHARGER_DISPLAY
90 static void add_bootmode_charger_to_cmdline(void)
91 {
92         char *pmode=" androidboot.mode=charger";
93         char *new_command_line = kzalloc(strlen(saved_command_line) + strlen(pmode) + 1, GFP_KERNEL);
94
95         sprintf(new_command_line, "%s%s", saved_command_line, pmode);
96         saved_command_line = new_command_line;
97
98         printk("Kernel command line: %s\n", saved_command_line);
99 }
100
101 static int  __init start_charge_logo_display(void)
102 {
103         union power_supply_propval val_status = {POWER_SUPPLY_STATUS_DISCHARGING};
104         union power_supply_propval val_capacity ={ 100} ;
105
106         printk("start_charge_logo_display\n");
107
108         if(rockchip_boot_mode() == BOOT_MODE_RECOVERY)  //recovery mode
109         {
110                 printk("recovery mode \n");
111                 return 0;
112         }
113         if (rk_get_system_battery_status() != POWER_SUPPLY_TYPE_BATTERY)
114                 val_status.intval = POWER_SUPPLY_STATUS_CHARGING;
115
116         val_capacity.intval = rk_get_system_battery_capacity();
117         // low power   and  discharging
118 #if 0
119         if((val_capacity.intval < pwr_on_thrsd )&&(val_status.intval != POWER_SUPPLY_STATUS_CHARGING))
120         {
121                 printk("low power\n");
122                 kernel_power_off();
123                 while(1);
124                 return 0;
125         }
126 #endif
127
128         if(val_status.intval == POWER_SUPPLY_STATUS_CHARGING)
129         {
130                 if (((rockchip_boot_mode() == BOOT_MODE_NORMAL) || (rockchip_boot_mode() == BOOT_MODE_CHARGE)) || (val_capacity.intval <= pwr_on_thrsd))
131             {                   
132                         add_bootmode_charger_to_cmdline();
133                         printk("power in charge mode %d %d  %d\n\n",rockchip_boot_mode(),val_capacity.intval,pwr_on_thrsd);
134            }
135         }
136
137         return 0;
138
139
140 late_initcall(start_charge_logo_display);
141 #endif