Merge remote-tracking branch 'stable/linux-3.0.y' into develop-3.0
[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/delay.h>
8 #include <linux/power_supply.h>
9 #include <linux/workqueue.h>
10 #include <linux/reboot.h>
11 #include <asm/unaligned.h>
12 #include <mach/gpio.h>
13 #include <mach/iomux.h>
14 #include <mach/board.h>
15 #include <asm/uaccess.h>
16 #include <linux/power_supply.h>
17
18
19 #if 0
20 #define DBG(x...)       printk(KERN_INFO x)
21 #else
22 #define DBG(x...)
23 #endif
24
25 //#define RK29_PLAY_ON_PIN RK29_PIN6_PA7
26 //#define MAX_PRE_CNT 2
27 //#define DET_CNT   5
28 #define PWR_ON_THRESHD 5       //power on threshd of capacity
29 //unsigned int   pre_cnt = 0;   //for long press counter 
30 //int charge_disp_mode = 0;
31 static int pwr_on_thrsd = 5;          //power on capcity threshold
32
33 //extern int boot_mode_init(char * s);
34
35 static int __init pwr_on_thrsd_setup(char *str)
36 {
37
38         pwr_on_thrsd = simple_strtol(str,NULL,10);
39         printk(KERN_INFO "power on threshold:%d",pwr_on_thrsd);
40         return 0;
41 }
42
43 __setup("pwr_on_thrsd=", pwr_on_thrsd_setup);
44
45 static int usb_status;
46 static int ac_status;
47 static int __rk_get_system_battery_status(struct device *dev, void *data)
48 {
49         union power_supply_propval val_status = {POWER_SUPPLY_STATUS_DISCHARGING};
50         struct power_supply *psy = dev_get_drvdata(dev);
51
52         psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &val_status);
53
54         if (val_status.intval != 0) {
55                 if (psy->type == POWER_SUPPLY_TYPE_USB)
56                         usb_status = POWER_SUPPLY_TYPE_USB;
57                 if (psy->type == POWER_SUPPLY_TYPE_MAINS)
58                         ac_status = POWER_SUPPLY_TYPE_MAINS;
59         }
60
61         return 0;
62 }
63
64 // POWER_SUPPLY_TYPE_BATTERY --- discharge
65 // POWER_SUPPLY_TYPE_USB     --- usb_charging
66 // POWER_SUPPLY_TYPE_MAINS   --- AC_charging
67 int rk_get_system_battery_status(void)
68 {
69         class_for_each_device(power_supply_class, NULL, NULL, __rk_get_system_battery_status);
70
71         if (ac_status == POWER_SUPPLY_TYPE_MAINS) {
72                 return POWER_SUPPLY_TYPE_MAINS;
73         } else if (usb_status == POWER_SUPPLY_TYPE_USB) {
74                 return POWER_SUPPLY_TYPE_USB;
75         }
76
77         return POWER_SUPPLY_TYPE_BATTERY;
78 }
79 EXPORT_SYMBOL(rk_get_system_battery_status);
80
81 static union power_supply_propval battery_capacity = { 100 };
82 static int __rk_get_system_battery_capacity(struct device *dev, void *data)
83 {
84         struct power_supply *psy = dev_get_drvdata(dev);
85
86         psy->get_property(psy, POWER_SUPPLY_PROP_CAPACITY, &battery_capacity);
87
88         return 0;
89 }
90
91 int rk_get_system_battery_capacity(void)
92 {
93         class_for_each_device(power_supply_class, NULL, NULL, __rk_get_system_battery_capacity);
94
95         return battery_capacity.intval;
96 }
97 EXPORT_SYMBOL(rk_get_system_battery_capacity);
98
99 #ifdef CONFIG_POWER_ON_CHARGER_DISPLAY
100 //int charger_mode=0;           //1:charge,0:not charge
101 static void add_bootmode_charger_to_cmdline(void)
102 {
103         char *pmode=" androidboot.mode=charger";
104         //int off = strlen(saved_command_line);
105         char *new_command_line = kzalloc(strlen(saved_command_line) + strlen(pmode) + 1, GFP_KERNEL);
106         sprintf(new_command_line, "%s%s", saved_command_line, pmode);
107         saved_command_line = new_command_line;
108         //strcpy(saved_command_line+off,pmode);
109
110         //int off = strlen(boot_command_line);
111         //strcpy(boot_command_line+off,pmode);
112
113         printk("Kernel command line: %s\n", saved_command_line);
114 }
115
116 //display charger logo in kernel CAPACITY
117
118
119 static int  __init start_charge_logo_display(void)
120 {
121         union power_supply_propval val_status = {POWER_SUPPLY_STATUS_DISCHARGING};
122         union power_supply_propval val_capacity ={ 100} ;
123
124         printk("start_charge_logo_display\n");
125
126         if(board_boot_mode() == BOOT_MODE_RECOVERY)  //recovery mode
127         {
128                 printk("recovery mode \n");
129                 return 0;
130
131         }
132
133         if (rk_get_system_battery_status() != POWER_SUPPLY_TYPE_BATTERY)
134                 val_status.intval = POWER_SUPPLY_STATUS_CHARGING;
135
136         val_capacity.intval = rk_get_system_battery_capacity();
137
138         // low power   and  discharging
139 #if 0
140         if((val_capacity.intval < pwr_on_thrsd )&&(val_status.intval != POWER_SUPPLY_STATUS_CHARGING))
141         {
142                 printk("low power\n");
143                 kernel_power_off();
144                 while(1);
145                 return 0;
146         }
147 #endif
148
149
150         //low power and charging
151 #if 0
152         if((val_capacity.intval < pwr_on_thrsd )&&(val_status.intval == POWER_SUPPLY_STATUS_CHARGING))
153         {
154                 while((val_capacity.intval < pwr_on_thrsd ))
155                 {
156                         list_for_each_entry(psy, &rk_psy_head, rk_psy_node)
157                         {
158                                 psy->get_property(psy,POWER_SUPPLY_PROP_CAPACITY,&val_capacity); 
159                         }
160
161                         //printk("charging ... \n");
162                 }
163         }
164
165 #endif
166
167         if(val_status.intval == POWER_SUPPLY_STATUS_CHARGING)
168         {
169                 if ((board_boot_mode() != BOOT_MODE_REBOOT) || (val_capacity.intval <= pwr_on_thrsd))  //do not enter power on charge mode when soft  reset
170             {                   
171                         add_bootmode_charger_to_cmdline();
172                         //boot_mode_init("charge");
173                         printk("power in charge mode\n");
174                 }
175         }
176
177         return 0;
178
179
180 //subsys_initcall_sync(start_charge_logo_display);
181 module_init(start_charge_logo_display);
182 #endif