rk3188:pmu:rt5025:updata rt5025 drivers, modify some fg and charger bug
[firefly-linux-kernel-4.4.55.git] / drivers / power / rt5025-power.c
1 /* drivers/power/rt5025-power.c
2  * I2C Driver for Richtek RT5025 PMIC
3  * Multi function device - multi functional baseband PMIC Power part
4  *
5  * Copyright (C) 2013
6  * Author: 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.
11  */
12
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/i2c.h>
17 #include <linux/power_supply.h>
18 #include <linux/platform_device.h>
19 #include <linux/err.h>
20 #include <linux/version.h>
21 #include <linux/slab.h>
22 #include <linux/workqueue.h>
23 #include <linux/mfd/rt5025.h>
24 #include <linux/power/rt5025-power.h>
25 #include <linux/delay.h>
26
27
28 static enum power_supply_property rt5025_adap_props[] = {
29         POWER_SUPPLY_PROP_ONLINE,
30 };
31
32 static char *rt5025_supply_list[] = {
33         "rt5025-battery",
34 };
35
36
37 #if 0
38 static int rt5025_set_charging_current_switch (struct i2c_client *i2c, int onoff)
39 {
40         int ret;
41         if (onoff)
42                 ret = rt5025_set_bits(i2c, RT5025_REG_CHGCTL7, RT5025_CHGCEN_MASK);
43         else
44                 ret = rt5025_clr_bits(i2c, RT5025_REG_CHGCTL7, RT5025_CHGCEN_MASK);
45         return ret;
46 }
47
48 static int rt5025_set_charging_buck(struct i2c_client *i2c, int onoff)
49 {
50         int ret;
51         if (onoff)
52                 ret = rt5025_set_bits(i2c, RT5025_REG_CHGCTL2, RT5025_CHGBUCKEN_MASK);
53         else
54                 ret = rt5025_clr_bits(i2c, RT5025_REG_CHGCTL2, RT5025_CHGBUCKEN_MASK);
55         return ret;
56 }
57 #endif
58
59 static int rt5025_set_charging_current(struct i2c_client *i2c, int cur_value)
60 {
61         int ret = 0;
62         u8 data = 0;
63
64         //ICC Setting
65         #if 0
66         if (cur_value > 2000)
67                 data |= 0x0f<<3;
68         else if (cur_value >= 500 && cur_value <= 2000)
69         {
70                 data = (cur_value-500)/100;
71                 data<<=3;
72         }
73         #endif
74
75         //AICR Setting
76         if (cur_value > 1000)
77                 data |= 0x03<<1;
78         else if (cur_value > 500 && cur_value <= 1000)
79                 data |= 0x02<<1;
80         else if (cur_value > 100 && cur_value >= 500)
81                 data |= 0x01<<1;
82
83         rt5025_assign_bits(i2c, RT5025_REG_CHGCTL4, RT5025_CHGAICR_MASK, data);
84         return ret;
85 }
86
87 static int rt5025_chgstat_changed(struct rt5025_power_info *info, unsigned new_val)
88 {
89         int ret = 0;
90         switch (new_val)
91         {
92                 case 0x00:
93                         #if 0
94                         rt5025_set_charging_current_switch(info->i2c, 1);
95                         rt5025_set_charging_buck(info->i2c, 1);
96                         #endif
97                         info->chg_stat = 0x00;
98                         #if 1
99                         if (info->chip->battery_info)
100                         {
101                                 if (info->chg_term == 0)
102                                         rt5025_gauge_set_status(info->chip->battery_info, POWER_SUPPLY_STATUS_CHARGING);
103                                 else if (info->chg_term > 0)
104                                 {
105                                         rt5025_gauge_set_status(info->chip->battery_info, POWER_SUPPLY_STATUS_FULL);
106                                         info->chg_term = 0;
107                                 }
108                                 
109                         }
110                         #else
111                         if (info->event_callback)
112                                 info->event_callback->rt5025_gauge_set_status(POWER_SUPPLY_STATUS_CHARGING);
113                         #endif
114                         break;
115                 case 0x01:
116                         //rt5025_set_charging_current_switch(info->i2c, 1);
117                         info->chg_stat = 0x01;
118                         #if 1
119                         if (info->chip->battery_info)
120                         {
121                                 rt5025_gauge_set_status(info->chip->battery_info, POWER_SUPPLY_STATUS_CHARGING);
122                                 info->chg_term = 0;
123                         }
124                         #else
125                         if (info->event_callback)
126                                 info->event_callback->rt5025_gauge_set_status(POWER_SUPPLY_STATUS_CHARGING);
127                         #endif
128                         break;
129                 case 0x02:
130                         #if 0
131                         rt5025_set_charging_current_switch(info->i2c, 0);
132                         #endif
133                         info->chg_stat = 0x02;
134                         #if 1
135                         if (info->chip->battery_info)
136                         {
137                                 rt5025_gauge_set_status(info->chip->battery_info, POWER_SUPPLY_STATUS_FULL);
138                                 info->chg_term = 0;
139                         }
140                         #else
141                         if (info->event_callback)
142                                 info->event_callback->rt5025_gauge_set_status(POWER_SUPPLY_STATUS_FULL);
143                         #endif
144                         break;
145                 case 0x03:
146                         #if 0
147                         rt5025_set_charging_buck(info->i2c, 0);
148                         rt5025_set_charging_current_switch(info->i2c, 0);
149                         #endif
150                         info->chg_stat = 0x03;
151                         #if 1
152                         if (info->chip->battery_info)
153                         {
154                                 if (info->chg_term == 0)
155                                         rt5025_gauge_set_status(info->chip->battery_info, POWER_SUPPLY_STATUS_CHARGING);
156                                 else if (info->chg_term > 1)
157                                 {
158                                         rt5025_gauge_set_status(info->chip->battery_info, POWER_SUPPLY_STATUS_FULL);
159                                         info->chg_term = 0;
160                                 }
161                         }
162                         #else
163                         if (info->event_callback)
164                                 info->event_callback->rt5025_gauge_set_status(POWER_SUPPLY_STATUS_DISCHARGING);
165                         #endif
166                         break;
167                 default:
168                         break;
169         }
170         return ret;
171 }
172
173 #if 0
174 int rt5025_power_passirq_to_gauge(struct rt5025_power_info *info)
175 {
176         if (info->event_callback)
177                 info->event_callback->rt5025_gauge_irq_handler();
178         return 0;
179 }
180 EXPORT_SYMBOL(rt5025_power_passirq_to_gauge);
181 #endif
182
183 int rt5025_power_charge_detect(struct rt5025_power_info *info)
184 {
185         int ret = 0;
186         unsigned char chgstatval = 0;
187         unsigned old_usbval, old_acval, old_chgval, new_usbval, new_acval, new_chgval;
188
189         old_acval = info->ac_online;
190         old_usbval = info->usb_online;
191         old_chgval = info->chg_stat;
192
193         mdelay(10);
194         
195         ret = rt5025_reg_read(info->i2c, RT5025_REG_CHGSTAT);
196         if (ret<0)
197         {
198                 dev_err(info->dev, "read chg stat reg fail\n");
199                 return ret;
200         }
201         chgstatval = ret;
202
203         new_acval = (chgstatval&RT5025_CHG_ACONLINE)>>RT5025_CHG_ACSHIFT;
204         if (old_acval != new_acval)
205         {
206                 info->ac_online = new_acval;
207                 power_supply_changed(&info->ac);
208         }
209         new_usbval = (chgstatval&RT5025_CHG_USBONLINE)>>RT5025_CHG_USBSHIFT;
210         if (old_usbval != new_usbval)
211         {
212                 info->usb_online = new_usbval;
213                 power_supply_changed(&info->usb);
214         }
215
216         if (old_acval != new_acval || old_usbval != new_usbval)
217                 schedule_delayed_work(&info->usb_detect_work, 0); //no delay
218
219         new_chgval = (chgstatval&RT5025_CHGSTAT_MASK)>>RT5025_CHGSTAT_SHIFT;
220         
221         if (new_acval || new_usbval)
222         {
223                 //if (old_chgval != new_chgval)
224                 //{
225                         ret = rt5025_chgstat_changed(info, new_chgval);
226                 //}
227         }
228         else
229         {
230                 #if 0
231                 rt5025_set_charging_buck(info->i2c, 0);
232                 rt5025_set_charging_current_switch(info->i2c, 0);
233                 #endif
234                 info->chg_stat = RT5025_CHGSTAT_UNKNOWN;
235                 if (info->chip->jeita_info)
236                         rt5025_notify_charging_cable(info->chip->jeita_info, JEITA_NO_CHARGE);
237                 #if 1
238                 if (info->chip->battery_info)
239                         rt5025_gauge_set_status(info->chip->battery_info, POWER_SUPPLY_STATUS_DISCHARGING);
240                 #else
241                 if (info->event_callback)
242                         info->event_callback->rt5025_gauge_set_status(POWER_SUPPLY_STATUS_NOT_CHARGING);
243                 #endif
244         }
245
246         return ret;
247 }
248 EXPORT_SYMBOL(rt5025_power_charge_detect);
249
250 static int rt5025_adap_get_props(struct power_supply *psy,
251                                 enum power_supply_property psp,
252                                 union power_supply_propval *val)
253 {
254         struct rt5025_power_info *info = dev_get_drvdata(psy->dev->parent);
255         switch(psp)
256         {
257                 case POWER_SUPPLY_PROP_ONLINE:
258                         if (psy->type == POWER_SUPPLY_TYPE_MAINS)
259                                 val->intval = info->ac_online;
260                         else if (psy->type == POWER_SUPPLY_TYPE_USB)
261                                 val->intval = info->usb_online;
262                         else
263                                 return -EINVAL;
264                         break;
265                 default:
266                         return -EINVAL;
267         }
268         return 0;
269 }
270
271
272 extern int dwc_vbus_status(void);
273
274
275 static void usb_detect_work_func(struct work_struct *work)
276 {
277         struct delayed_work *delayed_work = (struct delayed_work *)container_of(work, struct delayed_work, work);
278         struct rt5025_power_info *pi = (struct rt5025_power_info *)container_of(delayed_work, struct rt5025_power_info, usb_detect_work);
279         
280         RTINFO("rt5025: %s ++", __func__);
281
282         mutex_lock(&pi->var_lock);
283         if (pi->ac_online)
284         {
285                 rt5025_set_charging_current(pi->i2c, 1000);
286                 rt5025_notify_charging_cable(pi->chip->jeita_info, JEITA_AC_ADAPTER);
287                 pi->usb_cnt = 0;
288         }
289         else if (pi->usb_online)
290         {
291                 RTINFO("%s: usb_cnt %d\n", __func__, pi->usb_cnt);
292                 switch(dwc_vbus_status())
293                 {
294                         case 2: // USB Wall charger
295                                 rt5025_set_charging_current(pi->i2c, 1000);
296                                 rt5025_notify_charging_cable(pi->chip->jeita_info, JEITA_USB_TA);
297                                 RTINFO("rt5025: detect usb wall charger\n");
298                                 break;
299                         case 1: //normal USB
300                         default:
301                                 rt5025_set_charging_current(pi->i2c, 500);
302                                 rt5025_notify_charging_cable(pi->chip->jeita_info, JEITA_NORMAL_USB);
303                                 RTINFO("rt5025: detect normal usb\n");
304                                 break;
305                 }
306                 if (pi->usb_cnt++ < 60)
307                         schedule_delayed_work(&pi->usb_detect_work, 1*HZ);
308         }
309         else
310         {
311                 //default to prevent over current charging
312                 rt5025_set_charging_current(pi->i2c, 500);
313                 rt5025_notify_charging_cable(pi->chip->jeita_info, JEITA_NO_CHARGE);
314                 //reset usb_cnt;
315                 pi->usb_cnt = 0;
316         }
317         mutex_unlock(&pi->var_lock);
318
319         RTINFO("rt5025: %s --", __func__);
320 }
321
322 static int __devinit rt5025_init_charger(struct rt5025_power_info *info, struct rt5025_power_data* pd)
323 {
324         //unsigned char data;
325         info->ac_online = 0;
326         info->usb_online =0;
327         //init charger buckck & charger current en to disable stat
328         info->chg_stat = RT5025_CHGSTAT_UNKNOWN;
329         #if 0
330         if (info->event_callback)
331                 info->event_callback->rt5025_gauge_set_status(POWER_SUPPLY_STATUS_DISCHARGING);
332         #endif
333         //rt5025_set_bits(info->i2c, RT5025_REG_CHGCTL4, RT5025_CHGRST_MASK);
334         //udelay(200);
335         //init register setting
336         rt5025_reg_write(info->i2c, RT5025_REG_CHGCTL2, pd->CHGControl2.val);
337         rt5025_reg_write(info->i2c, RT5025_REG_CHGCTL3, pd->CHGControl3.val);
338         rt5025_reg_write(info->i2c, RT5025_REG_CHGCTL4, pd->CHGControl4.val);
339         rt5025_reg_write(info->i2c, RT5025_REG_CHGCTL5, pd->CHGControl5.val);
340         rt5025_reg_write(info->i2c, RT5025_REG_CHGCTL6, pd->CHGControl6.val);
341         //rt5025_reg_write(info->i2c, RT5025_REG_CHGCTL7, pd->CHGControl7.val);
342         rt5025_assign_bits(info->i2c, RT5025_REG_CHGCTL7, 0xEF, pd->CHGControl7.val);
343         rt5025_reg_write(info->i2c, 0xA9, 0x60 );
344         //Special buck setting
345         #if 0
346         //Buck 1
347         data = rt5025_reg_read(info->i2c, 0x47);
348         data ^=0xc2;
349         rt5025_reg_write(info->i2c, 0x47, data);
350         //Buck 2
351         data = rt5025_reg_read(info->i2c, 0x48);
352         data ^=0xc2;
353         rt5025_reg_write(info->i2c, 0x48, data);
354         //Buck 3
355         data = rt5025_reg_read(info->i2c, 0x49);
356         data ^=0xc2;
357         rt5025_reg_write(info->i2c, 0x49, data);
358         #endif  //#if 0
359         
360         rt5025_power_charge_detect(info);
361
362         return 0;
363 }
364
365 static int __devinit rt5025_power_probe(struct platform_device *pdev)
366 {
367         struct rt5025_chip *chip = dev_get_drvdata(pdev->dev.parent);
368         struct rt5025_platform_data *pdata = chip->dev->platform_data;
369         struct rt5025_power_info *pi;
370         int ret = 0;
371         
372         pi = kzalloc(sizeof(*pi), GFP_KERNEL);
373         if (!pi)
374                 return -ENOMEM;
375
376         pi->i2c = chip->i2c;
377         pi->dev = &pdev->dev;
378         pi->chip = chip;
379         mutex_init(&pi->var_lock);
380         INIT_DELAYED_WORK(&pi->usb_detect_work, usb_detect_work_func);
381
382         #if 0
383         ret = rt5025_gauge_init(pi);
384         if (ret)
385                 goto out;
386         #endif
387
388         platform_set_drvdata(pdev, pi);
389
390         pi->ac.name = "rt5025-dc";
391         pi->ac.type = POWER_SUPPLY_TYPE_MAINS;
392         pi->ac.supplied_to = rt5025_supply_list;
393         pi->ac.properties = rt5025_adap_props;
394         pi->ac.num_properties = ARRAY_SIZE(rt5025_adap_props);
395         pi->ac.get_property = rt5025_adap_get_props;
396         ret = power_supply_register(&pdev->dev, &pi->ac);
397         if (ret)
398                 goto out;
399
400         pi->usb.name = "rt5025-usb";
401         pi->usb.type = POWER_SUPPLY_TYPE_USB;
402         pi->ac.supplied_to = rt5025_supply_list;
403         pi->usb.properties = rt5025_adap_props;
404         pi->usb.num_properties = ARRAY_SIZE(rt5025_adap_props);
405         pi->usb.get_property = rt5025_adap_get_props;
406         ret = power_supply_register(&pdev->dev, &pi->usb);
407         if (ret)
408                 goto out_usb;
409
410         rt5025_init_charger(pi, pdata->power_data);
411         chip->power_info = pi;
412
413         return ret;
414 out_usb:
415         power_supply_unregister(&pi->ac);
416 out:
417         kfree(pi);
418
419         return ret;
420 }
421
422 static int rt5025_power_suspend(struct platform_device *pdev, pm_message_t state)
423 {
424         #if 0
425         struct rt5025_power_info *pi = platform_get_drvdata(pdev);
426
427         if (pi->event_callback)
428                 pi->event_callback->rt5025_gauge_suspend();
429         #endif
430         RTINFO("\n");
431         return 0;
432 }
433
434 static int rt5025_power_resume(struct platform_device *pdev)
435 {
436         #if 0
437         struct rt5025_power_info *pi = platform_get_drvdata(pdev);
438
439         if (pi->event_callback)
440                 pi->event_callback->rt5025_gauge_resume();
441         #endif
442         RTINFO("\n");
443         return 0;
444 }
445
446 static int __devexit rt5025_power_remove(struct platform_device *pdev)
447 {
448         struct rt5025_power_info *pi = platform_get_drvdata(pdev);
449         struct rt5025_chip *chip = dev_get_drvdata(pdev->dev.parent);
450
451         #if 0
452         if (pi->event_callback)
453                 pi->event_callback->rt5025_gauge_remove();
454         #endif
455         power_supply_unregister(&pi->usb);
456         power_supply_unregister(&pi->ac);
457         chip->power_info = NULL;
458         kfree(pi);
459
460         return 0;
461 }
462
463 static struct platform_driver rt5025_power_driver = 
464 {
465         .driver = {
466                 .name = RT5025_DEVICE_NAME "-power",
467                 .owner = THIS_MODULE,
468         },
469         .probe = rt5025_power_probe,
470         .remove = __devexit_p(rt5025_power_remove),
471         .suspend = rt5025_power_suspend,
472         .resume = rt5025_power_resume,
473 };
474
475 static int __init rt5025_power_init(void)
476 {
477         return platform_driver_register(&rt5025_power_driver);
478 }
479 late_initcall_sync(rt5025_power_init);
480
481 static void __exit rt5025_power_exit(void)
482 {
483         platform_driver_unregister(&rt5025_power_driver);
484 }
485 module_exit(rt5025_power_exit);
486
487
488 MODULE_LICENSE("GPL v2");
489 MODULE_AUTHOR("CY Huang <cy_huang@richtek.com");
490 MODULE_DESCRIPTION("Power/Gauge driver for RT5025");
491 MODULE_ALIAS("platform:" RT5025_DEVICE_NAME "-power");
492 MODULE_VERSION(RT5025_DRV_VER);