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