rk31:pmu:rt5025:updata drivers,modify charger & fg bug,support otg
[firefly-linux-kernel-4.4.55.git] / include / linux / mfd / rt5025.h
1 /*
2  *  include/linux/mfd/rt5025.h
3  *  Include header file for Richtek RT5025 Core file
4  *
5  *  Copyright (C) 2013 Richtek Electronics
6  *  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 #ifndef __LINUX_MFD_RT5025_H
14 #define __LINUX_MFD_RT5025_H
15
16 #include <linux/power_supply.h>
17 #include <linux/android_alarm.h>
18
19 #define RT5025_DEVICE_NAME "RT5025"
20 #define RT5025_DRV_VER     "1.0.4_R"
21
22 enum {
23         RT5025_RSTDELAY1_100MS,
24         RT5025_RSTDELAY1_500MS,
25         RT5025_RSTDELAY1_1S,
26         RT5025_RSTDELAY1_2S,
27 };
28
29 enum {
30         RT5025_RSTDELAY2_100MS,
31         RT5025_RSTDELAY2_500MS,
32         RT5025_RSTDELAY2_1S,
33         RT5025_RSTDELAY2_2S,
34 };
35
36 enum {
37         RT5025_VOFF_2P8V,
38         RT5025_VOFF_2P9V,
39         RT5025_VOFF_3P0V,
40         RT5025_VOFF_3P1V,
41         RT5025_VOFF_3P2V,
42         RT5025_VOFF_3P3V,
43         RT5025_VOFF_3P4V,
44         RT5025_VOFF_3P5V,
45 };
46
47 enum {
48         RT5025_STARTIME_100MS,
49         RT5025_STARTIME_1S,
50         RT5025_STARTIME_2S,
51         RT5025_STARTIME_3S,
52 };
53
54 enum {
55         RT5025_LPRESS_1S,
56         RT5025_LPRESS_1P5S,
57         RT5025_LPRESS_2S,
58         RT5025_LPRESS_2P5S,
59 };
60
61 enum {
62         RT5025_SHDNPRESS_4S,
63         RT5025_SHDNPRESS_6S,
64         RT5025_SHDNPRESS_8S,
65         RT5025_SHDNPRESS_10S,
66 };
67
68 enum {
69         RT5025_PGDLY_10MS,
70         RT5025_PGDLY_50MS,
71         RT5025_PGDLY_100MS,
72         RT5025_PGDLY_200MS,
73 };
74
75 enum {
76         RT5025_SHDNDLY_100MS,
77         RT5025_SHDNDLY_500MS,
78         RT5025_SHDNDLY_1S,
79         RT5025_SHDNDLY_2S,
80 };
81
82 enum {
83         RT5025_CCCHG_TO_4H,
84         RT5025_CCCHG_TO_6H,
85         RT5025_CCCHG_TO_8H,
86         RT5025_CCCHG_TO_10H,
87 };
88
89 enum {
90         RT5025_PRECHG_TO_30M,
91         RT5025_PRECHG_TO_40M,
92         RT5025_PRECHG_TO_50M,
93         RT5025_PRECHG_TO_60M,
94 };
95
96 enum {
97         RT5025_ICC_0P5A,
98         RT5025_ICC_0P6A,
99         RT5025_ICC_0P7A,
100         RT5025_ICC_0P8A,
101         RT5025_ICC_0P9A,
102         RT5025_ICC_1A,
103         RT5025_ICC_1P1A,
104         RT5025_ICC_1P2A,
105         RT5025_ICC_1P3A,
106         RT5025_ICC_1P4A,
107         RT5025_ICC_1P5A,
108         RT5025_ICC_1P6A,
109         RT5025_ICC_1P7A,
110         RT5025_ICC_1P8A,
111         RT5025_ICC_1P9A,
112         RT5025_ICC_2A,
113         RT5025_ICC_MAX,
114 };
115
116 enum {
117         RT5025_AICR_100MA,
118         RT5025_AICR_500MA,
119         RT5025_AICR_1A,
120         RT5025_AICR_NOLIMIT,
121 };
122
123 enum {
124         RT5025_DPM_4V,
125         RT5025_DPM_4P25V,
126         RT5025_DPM_4P5V,
127         RT5025_DPM_DIS,
128 };
129
130 enum {
131         RT5025_VPREC_2V,
132         RT5025_VPREC_2P2V,
133         RT5025_VPREC_2P4V,
134         RT5025_VPREC_2P6V,
135         RT5025_VPREC_2P8V,
136         RT5025_VPREC_3V,
137         RT5025_VPREC_3V_1,
138         RT5025_VPREC_3V_2,
139 };
140
141 enum {
142         RT5025_IEOC_10P,
143         RT5025_IEOC_20P,
144 };
145
146 enum {
147         RT5025_IPREC_10P,
148         RT5025_IPREC_20P,
149 };
150
151 enum {
152         RT5025_ID_DCDC1,
153         RT5025_ID_DCDC2,
154         RT5025_ID_DCDC3,
155         RT5025_ID_DCDC4,
156         RT5025_ID_LDO1,
157         RT5025_ID_LDO2,
158         RT5025_ID_LDO3,
159         RT5025_ID_LDO4,
160         RT5025_ID_LDO5,
161         RT5025_ID_LDO6,
162         RT5025_MAX_REGULATOR,
163 };
164
165 struct rt5025_power_data {
166         union {
167                 struct {
168                         unsigned char Resv1:1;
169                         unsigned char CHGBC_EN:1;
170                         unsigned char TE:1;
171                         unsigned char Resv2:1;
172                         unsigned char CCCHG_TIMEOUT:2;
173                         unsigned char PRECHG_TIMEOUT:2;
174                 }bitfield;
175                 unsigned char val;
176         }CHGControl2;
177         union {
178                 struct {
179                         unsigned char Resv:2;
180                         unsigned char VOREG:6;
181                 }bitfield;
182                 unsigned char val;
183         }CHGControl3;
184         union {
185                 struct {
186                         unsigned char AICR_CON:1;
187                         unsigned char AICR:2;
188                         unsigned char ICC:4;
189                         unsigned char CHG_RST:1;
190                 }bitfield;
191                 unsigned char val;
192         }CHGControl4;
193         union {
194                 struct {
195                         unsigned char Resv1:4;
196                         unsigned char DPM:2;
197                         unsigned char Resv2:2;
198                 }bitfield;
199                 unsigned char val;
200         }CHGControl5;
201         union {
202                 struct {
203                         unsigned char IPREC:1;
204                         unsigned char IEOC:1;
205                         unsigned char VPREC:3;
206                         unsigned char Resv:3;
207                 }bitfield;
208                 unsigned char val;
209         }CHGControl6;
210         union {
211                 struct {
212                         unsigned char Resv1:4;
213                         unsigned char CHGC_EN:1;
214                         unsigned char CHG_DCDC_MODE:1;
215                         unsigned char BATD_EN:1;
216                         unsigned char Resv2:1;
217                 }bitfield;
218                 unsigned char val;
219         }CHGControl7;
220 };
221
222 struct rt5025_gpio_data {
223         unsigned gpio_base;
224         unsigned irq_base;
225 };
226
227 struct rt5025_misc_data {
228         union {
229                 struct {
230                         unsigned char Action:2;
231                         unsigned char Delayed1:2;
232                         unsigned char Delayed2:2;
233                         unsigned char Resv:2;
234                 }bitfield;
235                 unsigned char val;
236         }RSTCtrl;
237         union {
238                 struct {
239                         unsigned char Resv:5;
240                         unsigned char VOFF:3;
241                 }bitfield;
242                 unsigned char val;
243         }VSYSCtrl;
244         union {
245                 struct {
246                         unsigned char PG_DLY:2;
247                         unsigned char SHDN_PRESS:2;
248                         unsigned char LPRESS_TIME:2;
249                         unsigned char START_TIME:2;
250                 }bitfield;
251                 unsigned char val;
252         }PwrOnCfg;
253         union {
254                 struct {
255                         unsigned char Resv:4;
256                         unsigned char SHDN_DLYTIME:2;
257                         unsigned char SHDN_TIMING:1;
258                         unsigned char SHDN_CTRL:1;
259                 }bitfield;
260                 unsigned char val;
261         }SHDNCtrl;
262         union {
263                 struct {
264                         unsigned char Resv:2;
265                         unsigned char OT_ENSHDN:1;
266                         unsigned char PWRON_ENSHDN:1;
267                         unsigned char DCDC3LV_ENSHDN:1;
268                         unsigned char DCDC2LV_ENSHDN:1;
269                         unsigned char DCDC1LV_ENSHDN:1;
270                         unsigned char SYSLV_ENSHDN:1;
271                 }bitfield;
272                 unsigned char val;
273         }PwrOffCond;
274 };
275
276 struct rt5025_irq_data {
277         union {
278                 struct {
279                         unsigned char BATABS:1;
280                         unsigned char Resv1:2;
281                         unsigned char INUSB_PLUGIN:1;
282                         unsigned char INUSBOVP:1;
283                         unsigned char Resv2:1;
284                         unsigned char INAC_PLUGIN:1;
285                         unsigned char INACOVP:1;
286                 }bitfield;
287                 unsigned char val;
288         }irq_enable1;
289         union {
290                 struct {
291                         unsigned char CHTERMI:1;
292                         unsigned char CHBATOVI:1;
293                         unsigned char CHGOODI_INUSB:1;
294                         unsigned char CHBADI_INUSB:1;
295                         unsigned char CHSLPI_INUSB:1;
296                         unsigned char CHGOODI_INAC:1;
297                         unsigned char CHBADI_INAC:1;
298                         unsigned char CHSLPI_INAC:1;
299                 }bitfield;
300                 unsigned char val;
301         }irq_enable2;
302         union {
303                 struct {
304                         unsigned char TIMEOUT_CC:1;
305                         unsigned char TIMEOUT_PC:1;
306                         unsigned char Resv:3;
307                         unsigned char CHVSREGI:1;
308                         unsigned char CHTREGI:1;
309                         unsigned char CHRCHGI:1;
310                 }bitfield;
311                 unsigned char val;
312         }irq_enable3;
313         union {
314                 struct {
315                         unsigned char SYSLV:1;
316                         unsigned char DCDC4LVHV:1;
317                         unsigned char PWRONLP:1;
318                         unsigned char PWRONSP:1;
319                         unsigned char DCDC3LV:1;
320                         unsigned char DCDC2LV:1;
321                         unsigned char DCDC1LV:1;
322                         unsigned char OT:1;
323                 }bitfield;
324                 unsigned char val;
325         }irq_enable4;
326         union {
327                 struct {
328                         unsigned char Resv:1;
329                         unsigned char GPIO0_IE:1;
330                         unsigned char GPIO1_IE:1;
331                         unsigned char GPIO2_IE:1;
332                         unsigned char RESETB:1;
333                         unsigned char PWRONF:1;
334                         unsigned char PWRONR:1;
335                         unsigned char KPSHDN:1;
336                 }bitfield;
337                 unsigned char val;
338         }irq_enable5;
339 };
340
341 enum {
342         JEITA_NO_CHARGE,
343         JEITA_NORMAL_USB,
344         JEITA_USB_TA,
345         JEITA_AC_ADAPTER,
346         JEITA_CHARGER_MAX,
347 };
348
349 struct rt5025_jeita_data {
350         int* temp;
351         u8* temp_scalar;
352         int (*temp_cc)[5];
353         int (*temp_cv)[5];
354 };
355
356 #define CHG_EVENT_INACOVP       (0x80<<16)
357 #define CHG_EVENT_INAC_PLUGIN   (0x40<<16)
358 #define CHG_EVENT_INUSBOVP      (0x10<<16)
359 #define CHG_EVENT_INUSB_PLUGIN  (0x08<<16)
360 #define CHG_EVENT_BAT_ABS       (0x01<<16)
361
362 #define CHG_EVENT_CHSLPI_INAC   (0x80<<8)
363 #define CHG_EVENT_CHBADI_INAC   (0x40<<8)
364 #define CHG_EVENT_CHGOODI_INAC  (0x20<<8)
365 #define CHG_EVENT_CHSLPI_INUSB  (0x10<<8)
366 #define CHG_EVENT_CHBADI_INUSB  (0x08<<8)
367 #define CHG_EVENT_CHGOODI_INUSB (0x04<<8)
368 #define CHG_EVENT_CHBATOVI      (0x02<<8)
369 #define CHG_EVENT_CHTERMI       (0x01<<8)
370
371 #define CHG_EVENT_CHRCHGI       (0x80<<0)
372 #define CHG_EVENT_CHTREGI       (0x40<<0)
373 #define CHG_EVENT_CHVSREGI      (0x20<<0)
374 #define CHG_EVENT_TIMEOUTPC     (0x02<<0)
375 #define CHG_EVENT_TIMEOUTCC     (0x01<<0)
376
377 #define CHARGER_DETECT_MASK     (CHG_EVENT_INAC_PLUGIN | CHG_EVENT_INUSB_PLUGIN | \
378                                  CHG_EVENT_CHSLPI_INAC | CHG_EVENT_CHSLPI_INUSB | \
379                                  CHG_EVENT_CHBADI_INAC | CHG_EVENT_CHBADI_INUSB | \
380                                  CHG_EVENT_CHTERMI | CHG_EVENT_CHRCHGI)
381  
382
383 #define PWR_EVENT_OTIQ          (0x80<<8)
384 #define PWR_EVENT_DCDC1LV       (0x40<<8)
385 #define PWR_EVENT_DCDC2LV       (0x20<<8)
386 #define PWR_EVENT_DCDC3LV       (0x10<<8)
387 #define PWR_EVENT_PWRONSP       (0x08<<8)
388 #define PWR_EVENT_PWRONLP       (0x04<<8)
389 #define PWR_EVENT_DCDC4LVHV     (0x02<<8)
390 #define PWR_EVENT_SYSLV         (0x01<<8)
391
392 #define PWR_EVENT_KPSHDN        (0x80<<0)
393 #define PWR_EVNET_PWRONR        (0x40<<0)
394 #define PWR_EVENT_PWRONF        (0x20<<0)
395 #define PWR_EVENT_RESETB        (0x10<<0)
396 #define PWR_EVENT_GPIO2IE       (0x08<<0)
397 #define PWR_EVENT_GPIO1IE       (0x04<<0)
398 #define PWR_EVENT_GPIO0IE       (0x02<<0)
399
400 struct rt5025_event_callback {
401         #if 1
402         void (*charger_event_callback)(uint32_t detected);
403         void (*power_event_callkback)(uint32_t detected);
404         #else
405         void (*over_temperature_callback)(uint8_t detected);
406         void (*charging_complete_callback)(void);
407         void (*over_voltage_callback)(uint8_t detected);
408         void (*under_voltage_callback)(uint8_t detected);
409         void (*charge_fault_callback)(uint8_t detected);
410         void (*charge_warning_callback)(uint8_t detected);
411         #endif
412 };
413
414 struct rt5025_power_info {
415         struct i2c_client       *i2c;
416         struct device           *dev;
417         struct rt5025_chip      *chip;
418         //struct rt5025_gauge_callbacks *event_callback;
419         struct power_supply     ac;
420         struct power_supply     usb;
421         struct mutex    var_lock;
422         struct delayed_work usb_detect_work;
423         int usb_cnt;
424         int chg_term;
425         int otg_en;
426         unsigned                ac_online:1;
427         unsigned                usb_online:1;
428         unsigned                chg_stat:3;
429 };
430
431 struct rt5025_swjeita_info {
432         struct i2c_client *i2c;
433         struct rt5025_chip *chip;
434         struct delayed_work thermal_reg_work;
435         int *temp;
436         u8 *temp_scalar;
437         int (*temp_cc)[5];
438         int (*temp_cv)[5];
439         int dec_current;
440         int cur_section;
441         int cur_therm_region;
442         int cur_cable;
443         int cur_temp;
444         int cur_inttemp;
445         int init_once;
446         int suspend;
447 };
448
449 struct rt5025_battery_info {
450         struct i2c_client *client;
451         struct rt5025_chip *chip;
452         //struct rt5025_gauge_callbacks cb;
453
454         struct power_supply     battery;
455         
456         struct delayed_work monitor_work;
457         struct wake_lock monitor_wake_lock;
458         struct wake_lock low_battery_wake_lock;
459         struct wake_lock status_wake_lock;
460 //#if RT5025_TEST_WAKE_LOCK
461         struct wake_lock test_wake_lock;
462 //#endif
463         struct alarm wakeup_alarm;
464         
465         bool temp_range_0_5;
466         bool temp_range_5_10;
467         bool temp_range_10_15;
468         bool temp_range_15_20;
469         bool temp_range_20_30;
470         bool temp_range_30_35;
471         bool temp_range_35_40;
472         bool temp_range_40_45;
473         bool temp_range_45_50;
474         
475         bool range_0_5_done;
476         bool range_5_10_done;
477         bool range_10_15_done;
478         bool range_15_20_done;
479         bool range_20_30_done;
480         bool range_30_35_done;
481         bool range_35_40_done;
482         bool range_40_45_done;
483         bool range_45_50_done;
484         
485         
486         
487         bool    suspend_poll;
488         ktime_t last_poll;
489 //      ktime_t last_event;
490   struct timespec last_event;
491
492   u16 update_time;
493   
494   /* previous battery voltage */
495   u16 pre_vcell;
496   /* previous battery current */
497   s16 pre_curr; 
498   /* battery voltage */
499   u16 vcell;
500   /* battery current */
501   s16 curr;
502   /* battery current offset */
503   u16 curr_offset;
504   /* AIN voltage */
505   u16 ain_volt;
506   /* battery internal temperature */
507   s16 int_temp;
508   /* battery external temperature */
509   s16 ext_temp;
510   /* charge coulomb counter */
511   u32 chg_cc;
512   u32 chg_cc_unuse;
513   /* discharge coulomb counter */
514   u32 dchg_cc;
515   u32 dchg_cc_unuse;
516   /* battery capacity */
517   u16 soc;
518   u16 temp_soc;
519   u16 pre_soc;
520   
521   u16 time_interval;
522   u16 pre_gauge_timer;
523     
524   u8 online;
525   u8 status;
526   u8 internal_status;
527   u8 health;
528   u8 present;
529
530   /* IRQ flag */
531   u8 irq_flag;
532    
533   /* max voltage IRQ flag */
534   bool max_volt_irq;
535   /* min voltage1 IRQ flag */
536   bool min_volt1_irq;  
537   /* min voltage2 IRQ flag */
538   bool min_volt2_irq;
539   /* max temperature IRQ flag */
540   bool max_temp_irq;
541   /* min temperature IRQ flag */
542   bool min_temp_irq;
543
544   bool min_volt2_alert;
545
546         u8 temp_high_cnt;
547         u8 temp_low_cnt;
548         u8 temp_recover_cnt;
549         
550         bool init_cap;
551         bool avg_flag;
552         
553   /* remain capacity */
554   u32 rm;
555   /* SOC permille  */
556   u16 permille;
557   /* full capccity */
558   u16 fcc_aging;
559   u16 fcc;
560   u16   dc;
561   s16 tempcmp;
562   #if 0
563   u32 time_to_empty;
564   u32 time_to_full;
565   #endif
566   
567   bool edv_flag;
568   bool edv_detection;
569   u8 edv_cnt;
570   
571   bool tp_flag;
572   u8 tp_cnt;
573   
574   u8 cycle_cnt;
575   u32 acc_dchg_cap;
576
577   bool smooth_flag;
578   
579   u16 gauge_timer;
580   s16 curr_raw;
581   u32 empty_edv;
582   u8  edv_region;
583
584   bool init_once;
585   bool device_suspend;
586   bool last_suspend;
587   bool last_tp_flag;
588   u32 cal_fcc;
589   u8 test_temp;
590 };
591
592 struct rt5025_chip {
593         struct i2c_client *i2c;
594         struct workqueue_struct *wq;
595         struct device *dev;
596         struct rt5025_power_info *power_info;
597         struct rt5025_swjeita_info *jeita_info;
598         struct rt5025_battery_info *battery_info;
599         int suspend;
600         int irq;
601         struct delayed_work delayed_work;
602         struct mutex io_lock;
603 };
604
605 struct rt5025_platform_data {
606         struct regulator_init_data* regulator[RT5025_MAX_REGULATOR];
607         struct rt5025_power_data* power_data;
608         struct rt5025_gpio_data* gpio_data;
609         struct rt5025_misc_data* misc_data;
610         struct rt5025_irq_data* irq_data;
611         struct rt5025_jeita_data* jeita_data;
612         struct rt5025_event_callback *cb;
613         int (*pre_init)(struct rt5025_chip *rt5025_chip);
614         /** Called after subdevices are set up */
615         int (*post_init)(void);
616         int (*set_otg_enable)(int);
617         int intr_pin;
618 };
619
620 #ifdef CONFIG_MFD_RT5025_MISC
621 extern void rt5025_power_off(void);
622 #endif /* CONFIG_MFD_RT5025_MISC */
623
624 #ifdef CONFIG_POWER_RT5025
625 extern int rt5025_ext_set_charging_buck(int);
626 extern int rt5025_set_charging_buck(struct i2c_client *, int);
627 extern int rt5025_set_charging_current_switch(struct i2c_client *, int);
628 extern void rt5025_gauge_set_status(struct rt5025_battery_info *, int);
629 extern void rt5025_gauge_set_online(struct rt5025_battery_info *, bool);
630 extern void rt5025_gauge_irq_handler(struct rt5025_battery_info *, u8);
631 extern int rt5025_power_charge_detect(struct rt5025_power_info *);
632 extern int rt5025_notify_charging_cable(struct rt5025_swjeita_info *, int);
633 extern int rt5025_swjeita_irq_handler(struct rt5025_swjeita_info *, unsigned char);
634 #endif /* CONFIG_POEWR_RT5025 */
635
636 extern int rt5025_reg_block_read(struct i2c_client *, int, int, void *);
637 extern int rt5025_reg_block_write(struct i2c_client *, int, int, void *);
638 extern int rt5025_reg_read(struct i2c_client *, int);
639 extern int rt5025_reg_write(struct i2c_client *, int, unsigned char);
640 extern int rt5025_assign_bits(struct i2c_client *, int, unsigned char, unsigned char);
641 extern int rt5025_set_bits(struct i2c_client *, int, unsigned char);
642 extern int rt5025_clr_bits(struct i2c_client *, int, unsigned char);
643
644 extern int rt5025_core_init(struct rt5025_chip *, struct rt5025_platform_data *);
645 extern int rt5025_core_deinit(struct rt5025_chip *);
646
647 #ifdef CONFIG_MFD_RT_SHOW_INFO
648 #define RTINFO(format, args...) \
649         printk(KERN_INFO "%s:%s() line-%d: " format, RT5025_DEVICE_NAME,__FUNCTION__,__LINE__, ##args)
650 #else
651 #define RTINFO(format,args...)
652 #endif /* CONFIG_MFD_RT_SHOW_INFO */
653
654 #endif /* __LINUX_MFD_RT5025_H */