Merge remote-tracking branch 'origin/develop-3.10' into develop-3.10-next
[firefly-linux-kernel-4.4.55.git] / drivers / power / ricoh619-battery.c
1 /*
2  * drivers/power/ricoh619-battery.c
3  *
4  * Charger driver for RICOH RC5T619 power management chip.
5  *
6  * Copyright (C) 2012-2013 RICOH COMPANY,LTD
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 as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 #define RICOH619_BATTERY_VERSION "RICOH619_BATTERY_VERSION: 2014.05.06"
23
24
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/platform_device.h>
28 #include <linux/slab.h>
29 #include <linux/mutex.h>
30 #include <linux/string.h>
31 #include <linux/power_supply.h>
32 #include <linux/mfd/ricoh619.h>
33 #include <linux/power/ricoh619_battery.h>
34 #include <linux/power/ricoh61x_battery_init.h>
35 #include <linux/delay.h>
36 #include <linux/workqueue.h>
37 #include <linux/of.h>
38
39
40 #include <linux/interrupt.h>
41 #include <linux/irq.h>
42 #include <linux/irqdomain.h>
43
44
45 /* define for function */
46 #define ENABLE_FUEL_GAUGE_FUNCTION
47 #define ENABLE_LOW_BATTERY_DETECTION
48 //#define ENABLE_FACTORY_MODE
49 #define DISABLE_CHARGER_TIMER
50 /* #define ENABLE_FG_KEEP_ON_MODE */
51 /* #define ENABLE_OCV_TABLE_CALIB */
52 //#define SUPPORT_USB_CONNECT_TO_ADP
53
54
55 /* FG setting */
56 #define RICOH619_REL1_SEL_VALUE         64
57 #define RICOH619_REL2_SEL_VALUE         0
58
59 enum int_type {
60         SYS_INT  = 0x01,
61         DCDC_INT = 0x02,
62         ADC_INT  = 0x08,
63         GPIO_INT = 0x10,
64         CHG_INT  = 0x40,
65 };
66
67 //for debug   #ifdef ENABLE_FUEL_GAUGE_FUNCTION
68 /* define for FG delayed time */
69 #define RICOH619_MONITOR_START_TIME             15
70 #define RICOH619_FG_RESET_TIME                  6
71 #define RICOH619_FG_STABLE_TIME         120
72 #define RICOH619_DISPLAY_UPDATE_TIME            15
73 #define RICOH619_LOW_VOL_DOWN_TIME              10
74 #define RICOH619_CHARGE_MONITOR_TIME            20
75 #define RICOH619_CHARGE_RESUME_TIME             1
76 #define RICOH619_CHARGE_CALC_TIME               1
77 #define RICOH619_JEITA_UPDATE_TIME              60
78 #define RICOH619_DELAY_TIME                             60
79 /* define for FG parameter */
80 #define RICOH619_MAX_RESET_SOC_DIFF             5
81 #define RICOH619_GET_CHARGE_NUM         10
82 #define RICOH619_UPDATE_COUNT_DISP              4
83 #define RICOH619_UPDATE_COUNT_FULL              4
84 #define RICOH619_UPDATE_COUNT_FULL_RESET        7
85 #define RICOH619_CHARGE_UPDATE_TIME             3
86 #define RICOH619_FULL_WAIT_TIME                 4
87 #define RE_CAP_GO_DOWN                          10      /* 40 */
88 #define RICOH619_ENTER_LOW_VOL                  70
89 #define RICOH619_TAH_SEL2                       5
90 #define RICOH619_TAL_SEL2                       6
91
92 #define RICOH619_OCV_OFFSET_BOUND       3
93 #define RICOH619_OCV_OFFSET_RATIO       2
94
95 #define RICOH619_VADP_DROP_WORK 1
96 #define RICOH619_TIME_CHG_STEP  (1*HZ)// unit:secound
97 #define RICOH619_TIME_CHG_COUNT 15*60//only for test //15*60 
98
99 /* define for FG status */
100 enum {
101         RICOH619_SOCA_START,
102         RICOH619_SOCA_UNSTABLE,
103         RICOH619_SOCA_FG_RESET,
104         RICOH619_SOCA_DISP,
105         RICOH619_SOCA_STABLE,
106         RICOH619_SOCA_ZERO,
107         RICOH619_SOCA_FULL,
108         RICOH619_SOCA_LOW_VOL,
109 };
110 //#endif
111
112 #ifdef ENABLE_LOW_BATTERY_DETECTION
113 #define LOW_BATTERY_DETECTION_TIME              10
114 #endif
115
116 struct ricoh619_soca_info {
117         int Rbat;
118         int n_cap;
119         int ocv_table_def[11];
120         int ocv_table[11];
121         int ocv_table_low[11];
122         int soc;                /* Latest FG SOC value */
123         int displayed_soc;
124         int suspend_soc;
125         int status;             /* SOCA status 0: Not initial; 5: Finished */
126         int stable_count;
127         int chg_status;         /* chg_status */
128         int soc_delta;          /* soc delta for status3(DISP) */
129         int cc_delta;
130         int cc_cap_offset;
131         int last_soc;
132         int last_displayed_soc;
133         int ready_fg;
134         int reset_count;
135         int reset_soc[3];
136         int chg_cmp_times;
137         int dischg_state;
138         int Vbat[RICOH619_GET_CHARGE_NUM];
139         int Vsys[RICOH619_GET_CHARGE_NUM];
140         int Ibat[RICOH619_GET_CHARGE_NUM];
141         int Vbat_ave;
142         int Vbat_old;
143         int Vsys_ave;
144         int Ibat_ave;
145         int chg_count;
146         int full_reset_count;
147         int soc_full;
148         int fc_cap;
149         /* for LOW VOL state */
150         int target_use_cap;
151         int hurry_up_flg;
152         int zero_flg;
153         int re_cap_old;
154         int cutoff_ocv;
155         int Rsys;
156         int target_vsys;
157         int target_ibat;
158         int jt_limit;
159         int OCV100_min;
160         int OCV100_max;
161         int R_low;
162         int rsoc_ready_flag;
163         int init_pswr;
164         int last_cc_sum;
165 };
166
167 struct ricoh619_battery_info {
168         struct device      *dev;
169         struct power_supply     battery;
170         struct delayed_work     monitor_work;
171         struct delayed_work     displayed_work;
172         struct delayed_work     charge_stable_work;
173         struct delayed_work     changed_work;
174 #ifdef ENABLE_LOW_BATTERY_DETECTION
175         struct delayed_work     low_battery_work;
176 #endif
177         struct delayed_work     charge_monitor_work;
178         struct delayed_work     get_charge_work;
179         struct delayed_work     jeita_work;
180         struct delayed_work     charge_complete_ready;
181
182         struct work_struct      irq_work;       /* for Charging & VUSB/VADP */
183         struct work_struct      usb_irq_work;   /* for ADC_VUSB */
184         #ifdef RICOH619_VADP_DROP_WORK
185         struct delayed_work     vadp_drop_work;
186         #endif
187         struct workqueue_struct *monitor_wqueue;
188         struct workqueue_struct *workqueue;     /* for Charging & VUSB/VADP */
189         struct workqueue_struct *usb_workqueue; /* for ADC_VUSB */
190
191 #ifdef ENABLE_FACTORY_MODE
192         struct delayed_work     factory_mode_work;
193         struct workqueue_struct *factory_mode_wqueue;
194 #endif
195
196         struct mutex            lock;
197         unsigned long           monitor_time;
198         int             adc_vdd_mv;
199         int             multiple;
200         int             alarm_vol_mv;
201         int             status;
202         int             min_voltage;
203         int             max_voltage;
204         int             cur_voltage;
205         int             capacity;
206         int             battery_temp;
207         int             time_to_empty;
208         int             time_to_full;
209         int             chg_ctr;
210         int             chg_stat1;
211         unsigned        present:1;
212         u16             delay;
213         struct          ricoh619_soca_info *soca;
214         int             first_pwon;
215         bool            entry_factory_mode;
216         int             ch_vfchg;
217         int             ch_vrchg;
218         int             ch_vbatovset;
219         int             ch_ichg;
220         int             ch_ilim_adp;
221         int             ch_ilim_usb;
222         int             ch_icchg;
223         int             fg_target_vsys;
224         int             fg_target_ibat;
225         int             fg_poff_vbat;
226         int             jt_en;
227         int             jt_hw_sw;
228         int             jt_temp_h;
229         int             jt_temp_l;
230         int             jt_vfchg_h;
231         int             jt_vfchg_l;
232         int             jt_ichg_h;
233         int             jt_ichg_l;
234
235         int     chg_complete_rd_flag;
236         int     chg_complete_rd_cnt;
237         int             chg_complete_tm_ov_flag;
238         int             chg_complete_sleep_flag;
239         int             chg_old_dsoc;
240
241         int             num;
242         };
243
244 struct power_supply powerac;
245 struct power_supply powerusb;
246
247 int g_full_flag;
248 int charger_irq;
249 /* this value is for mfd fucntion */
250 int g_soc;
251 int g_fg_on_mode;
252 int type_n;
253 extern int dwc_otg_check_dpdm(bool wait);
254 /*This is for full state*/
255 static int BatteryTableFlagDef=0;
256 static int BatteryTypeDef=0;
257 static int Battery_Type(void)
258 {
259         return BatteryTypeDef;
260 }
261
262 static int Battery_Table(void)
263 {
264         return BatteryTableFlagDef;
265 }
266
267 static void ricoh619_battery_work(struct work_struct *work)
268 {
269         struct ricoh619_battery_info *info = container_of(work,
270                 struct ricoh619_battery_info, monitor_work.work);
271
272         RICOH_FG_DBG("PMU: %s\n", __func__);
273         power_supply_changed(&info->battery);
274         queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
275                            info->monitor_time);
276 }
277
278 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
279 static int measure_vbatt_FG(struct ricoh619_battery_info *info, int *data);
280 static int measure_Ibatt_FG(struct ricoh619_battery_info *info, int *data);
281 static int calc_capacity(struct ricoh619_battery_info *info);
282 static int calc_capacity_2(struct ricoh619_battery_info *info);
283 static int get_OCV_init_Data(struct ricoh619_battery_info *info, int index);
284 static int get_OCV_voltage(struct ricoh619_battery_info *info, int index);
285 static int get_check_fuel_gauge_reg(struct ricoh619_battery_info *info,
286                                          int Reg_h, int Reg_l, int enable_bit);
287 static int calc_capacity_in_period(struct ricoh619_battery_info *info,
288                                  int *cc_cap, bool *is_charging, bool cc_rst);
289 //static int get_charge_priority(struct ricoh619_battery_info *info, bool *data);
290 //static int set_charge_priority(struct ricoh619_battery_info *info, bool *data);
291 static int get_power_supply_status(struct ricoh619_battery_info *info);
292 static int get_power_supply_Android_status(struct ricoh619_battery_info *info);
293 static int measure_vsys_ADC(struct ricoh619_battery_info *info, int *data);
294 static int Calc_Linear_Interpolation(int x0, int y0, int x1, int y1, int y);
295 static int get_battery_temp(struct ricoh619_battery_info *info);
296 static int get_battery_temp_2(struct ricoh619_battery_info *info);
297 static int check_jeita_status(struct ricoh619_battery_info *info, bool *is_jeita_updated);
298 static void ricoh619_scaling_OCV_table(struct ricoh619_battery_info *info, int cutoff_vol, int full_vol, int *start_per, int *end_per);
299 //static int ricoh619_Check_OCV_Offset(struct ricoh619_battery_info *info);
300
301 static int calc_ocv(struct ricoh619_battery_info *info)
302 {
303         int Vbat = 0;
304         int Ibat = 0;
305         int ret;
306         int ocv;
307
308         ret = measure_vbatt_FG(info, &Vbat);
309         ret = measure_Ibatt_FG(info, &Ibat);
310
311         ocv = Vbat - Ibat * info->soca->Rbat;
312
313         return ocv;
314 }
315
316 #if 0
317 static int set_Rlow(struct ricoh619_battery_info *info)
318 {
319         int err;
320         int Rbat_low_max;
321         uint8_t val;
322         int Vocv;
323         int temp;
324
325         if (info->soca->Rbat == 0)
326                         info->soca->Rbat = get_OCV_init_Data(info, 12) * 1000 / 512
327                                                          * 5000 / 4095;
328         
329         Vocv = calc_ocv(info);
330         Rbat_low_max = info->soca->Rbat * 1.5;
331
332         if (Vocv < get_OCV_voltage(info,3))
333         {
334                 info->soca->R_low = Calc_Linear_Interpolation(info->soca->Rbat,get_OCV_voltage(info,3),
335                         Rbat_low_max, get_OCV_voltage(info,0), Vocv);
336                 RICOH_FG_DBG("PMU: Modify RBAT from %d to %d ", info->soca->Rbat, info->soca->R_low);
337                 temp = info->soca->R_low *4095/5000*512/1000;
338                 
339                 val = info->soca->R_low>>8;
340                 err = ricoh619_write_bank1(info->dev->parent, 0xD4, val);
341                 if (err < 0) {
342                         dev_err(info->dev, "batterry initialize error\n");
343                         return err;
344                 }
345
346                 val = info->soca->R_low & 0xff;
347                 err = ricoh619_write_bank1(info->dev->parent, 0xD5, val);
348                 if (err < 0) {
349                         dev_err(info->dev, "batterry initialize error\n");
350                         return err;
351                 }
352         }
353         else  info->soca->R_low = 0;
354                 
355
356         return err;
357 }
358
359 static int Set_back_ocv_table(struct ricoh619_battery_info *info)
360 {
361         int err;
362         uint8_t val;
363         int temp;
364         int i;
365         uint8_t debug_disp[22];
366
367         /* Modify back ocv table */
368
369         if (0 != info->soca->ocv_table_low[0])
370         {
371                 for (i = 0 ; i < 11; i++){
372                         battery_init_para[info->num][i*2 + 1] = info->soca->ocv_table_low[i];
373                         battery_init_para[info->num][i*2] = info->soca->ocv_table_low[i] >> 8;
374                 }
375                 err = ricoh619_clr_bits(info->dev->parent, FG_CTRL_REG, 0x01);
376
377                 err = ricoh619_bulk_writes_bank1(info->dev->parent,
378                         BAT_INIT_TOP_REG, 22, battery_init_para[info->num]);
379
380                 err = ricoh619_set_bits(info->dev->parent, FG_CTRL_REG, 0x01);
381
382                 /* debug comment start*/
383                 err = ricoh619_bulk_reads_bank1(info->dev->parent,
384                         BAT_INIT_TOP_REG, 22, debug_disp);
385                 for (i = 0; i < 11; i++){
386                         RICOH_FG_DBG("PMU : %s : after OCV table %d 0x%x\n",__func__, i * 10, (debug_disp[i*2] << 8 | debug_disp[i*2+1]));
387                 }
388                 /* end */
389                 /* clear table*/
390                 for(i = 0; i < 11; i++)
391                 {
392                         info->soca->ocv_table_low[i] = 0;
393                 }
394         }
395         
396         /* Modify back Rbat */
397         if (0!=info->soca->R_low)
398         {               
399                 RICOH_FG_DBG("PMU: Modify back RBAT from %d to %d ",  info->soca->R_low,info->soca->Rbat);
400                 temp = info->soca->Rbat*4095/5000*512/1000;
401                 
402                 val = info->soca->R_low>>8;
403                 err = ricoh619_write_bank1(info->dev->parent, 0xD4, val);
404                 if (err < 0) {
405                         dev_err(info->dev, "batterry initialize error\n");
406                         return err;
407                 }
408
409                 val = info->soca->R_low & 0xff;
410                 err = ricoh619_write_bank1(info->dev->parent, 0xD5, val);
411                 if (err < 0) {
412                         dev_err(info->dev, "batterry initialize error\n");
413                         return err;
414                 }
415
416                 info->soca->R_low = 0;
417         }
418         return 0;
419 }
420
421 /**
422 **/
423
424 static int ricoh619_Check_OCV_Offset(struct ricoh619_battery_info *info)
425 {
426         int ocv_table[11]; // HEX value
427         int i;
428         int temp;
429         int ret;
430         uint8_t debug_disp[22];
431         uint8_t val = 0;
432
433         RICOH_FG_DBG("PMU : %s : calc ocv %d get OCV %d\n",__func__,calc_ocv(info),get_OCV_voltage(info, RICOH619_OCV_OFFSET_BOUND));
434
435         /* check adp/usb status */
436         ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &val);
437         if (ret < 0) {
438                 dev_err(info->dev, "Error in reading the control register\n");
439                 return ret;
440         }
441
442         val = (val & 0xC0) >> 6;
443
444         if (val != 0){ /* connect adp or usb */
445                 if (calc_ocv(info) < get_OCV_voltage(info, RICOH619_OCV_OFFSET_BOUND) )
446                 {
447                         if(0 == info->soca->ocv_table_low[0]){
448                                 for (i = 0 ; i < 11; i++){
449                                 ocv_table[i] = (battery_init_para[info->num][i*2]<<8) | (battery_init_para[info->num][i*2+1]);
450                                 RICOH_FG_DBG("PMU : %s : OCV table %d 0x%x\n",__func__,i * 10, ocv_table[i]);
451                                 info->soca->ocv_table_low[i] = ocv_table[i];
452                                 }
453
454                                 for (i = 0 ; i < 11; i++){
455                                         temp = ocv_table[i] * (100 + RICOH619_OCV_OFFSET_RATIO) / 100;
456
457                                         battery_init_para[info->num][i*2 + 1] = temp;
458                                         battery_init_para[info->num][i*2] = temp >> 8;
459                                 }
460                                 ret = ricoh619_clr_bits(info->dev->parent, FG_CTRL_REG, 0x01);
461
462                                 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
463                                         BAT_INIT_TOP_REG, 22, battery_init_para[info->num]);
464
465                                 ret = ricoh619_set_bits(info->dev->parent, FG_CTRL_REG, 0x01);
466
467                                 /* debug comment start*/
468                                 ret = ricoh619_bulk_reads_bank1(info->dev->parent,
469                                         BAT_INIT_TOP_REG, 22, debug_disp);
470                                 for (i = 0; i < 11; i++){
471                                         RICOH_FG_DBG("PMU : %s : after OCV table %d 0x%x\n",__func__, i * 10, (debug_disp[i*2] << 8 | debug_disp[i*2+1]));
472                                 }
473                                 /* end */
474                         }
475                 }
476         }
477         
478         return 0;
479 }
480 #endif
481 static int reset_FG_process(struct ricoh619_battery_info *info)
482 {
483         int err;
484
485         //err = set_Rlow(info);
486         //err = ricoh619_Check_OCV_Offset(info);
487         err = ricoh619_write(info->dev->parent,
488                                          FG_CTRL_REG, 0x51);
489         info->soca->ready_fg = 0;
490         return err;
491 }
492
493
494 static int check_charge_status_2(struct ricoh619_battery_info *info, int displayed_soc_temp)
495 {
496         if (displayed_soc_temp < 0)
497                         displayed_soc_temp = 0;
498         
499         get_power_supply_status(info);
500         info->soca->soc = calc_capacity(info) * 100;
501
502         if (POWER_SUPPLY_STATUS_FULL == info->soca->chg_status) {
503                 if ((info->first_pwon == 1)
504                         && (RICOH619_SOCA_START == info->soca->status)) {
505                                 g_full_flag = 1;
506                                 info->soca->soc_full = info->soca->soc;
507                                 info->soca->displayed_soc = 100*100;
508                                 info->soca->full_reset_count = 0;
509                 } else {
510                         if ( (displayed_soc_temp > 97*100)
511                                 && (calc_ocv(info) > (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*7/10)  )){
512                                 g_full_flag = 1;
513                                 info->soca->soc_full = info->soca->soc;
514                                 info->soca->displayed_soc = 100*100;
515                                 info->soca->full_reset_count = 0;
516                         } else {
517                                 g_full_flag = 0;
518                                 info->soca->displayed_soc = displayed_soc_temp;
519                         }
520
521                 }
522         }
523         if (info->soca->Ibat_ave >= 0) {
524                 if (g_full_flag == 1) {
525                         info->soca->displayed_soc = 100*100;
526                 } else {
527                         if (info->soca->displayed_soc/100 < 99) {
528                                 info->soca->displayed_soc = displayed_soc_temp;
529                         } else {
530                                 info->soca->displayed_soc = 99 * 100;
531                         }
532                 }
533         }
534         if (info->soca->Ibat_ave < 0) {
535                 if (g_full_flag == 1) {
536                         if (calc_ocv(info) < (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*7/10)  ) {
537                                 g_full_flag = 0;
538                                 //info->soca->displayed_soc = 100*100;
539                                 info->soca->displayed_soc = displayed_soc_temp;
540                         } else {
541                                 info->soca->displayed_soc = 100*100;
542                         }
543                 } else {
544                         g_full_flag = 0;
545                         info->soca->displayed_soc = displayed_soc_temp;
546                 }
547         }
548
549         return info->soca->displayed_soc;
550 }
551
552 /**
553 * Calculate Capacity in a period
554 * - read CC_SUM & FA_CAP from Coulom Counter
555 * -  and calculate Capacity.
556 * @cc_cap: capacity in a period, unit 0.01%
557 * @is_charging: Flag of charging current direction
558 *               TRUE : charging (plus)
559 *               FALSE: discharging (minus)
560 * @cc_rst: reset CC_SUM or not
561 *               TRUE : reset
562 *               FALSE: not reset
563 **/
564 static int calc_capacity_in_period(struct ricoh619_battery_info *info,
565                                  int *cc_cap, bool *is_charging, bool cc_rst)
566 {
567         int err;
568         uint8_t         cc_sum_reg[4];
569         uint8_t         cc_clr[4] = {0, 0, 0, 0};
570         uint8_t         fa_cap_reg[2];
571         uint16_t        fa_cap;
572         uint32_t        cc_sum;
573         int             cc_stop_flag;
574         uint8_t         status;
575         uint8_t         charge_state;
576         int             Ocv;
577         uint32_t        cc_cap_temp;
578         uint32_t        cc_cap_min;
579         int             cc_cap_res;
580
581         *is_charging = true;    /* currrent state initialize -> charging */
582
583         if (info->entry_factory_mode)
584                 return 0;
585
586         //check need charge stop or not
587         /* get  power supply status */
588         err = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
589         if (err < 0)
590                 goto out;
591         charge_state = (status & 0x1F);
592         Ocv = calc_ocv(info);
593         if (charge_state == CHG_STATE_CHG_COMPLETE) {
594                 /* Check CHG status is complete or not */
595                 cc_stop_flag = 0;
596         } else if (calc_capacity(info) == 100) {
597                 /* Check HW soc is 100 or not */
598                 cc_stop_flag = 0;
599         } else if (Ocv/1000 < get_OCV_voltage(info, 9)) {
600                 /* Check VBAT is high level or not */
601                 cc_stop_flag = 0;
602         } else {
603                 cc_stop_flag = 1;
604         }
605
606         if (cc_stop_flag == 1)
607         {
608                 /* Disable Charging/Completion Interrupt */
609                 err = ricoh619_set_bits(info->dev->parent,
610                                                 RICOH619_INT_MSK_CHGSTS1, 0x01);
611                 if (err < 0)
612                         goto out;
613
614                 /* disable charging */
615                 err = ricoh619_clr_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x03);
616                 if (err < 0)
617                         goto out;
618         }
619
620         /* CC_pause enter */
621         err = ricoh619_write(info->dev->parent, CC_CTRL_REG, 0x01);
622         if (err < 0)
623                 goto out;
624
625         /* Read CC_SUM */
626         err = ricoh619_bulk_reads(info->dev->parent,
627                                         CC_SUMREG3_REG, 4, cc_sum_reg);
628         if (err < 0)
629                 goto out;
630
631         if (cc_rst == true) {
632                 /* CC_SUM <- 0 */
633                 err = ricoh619_bulk_writes(info->dev->parent,
634                                                 CC_SUMREG3_REG, 4, cc_clr);
635                 if (err < 0)
636                         goto out;
637         }
638
639         /* CC_pause exist */
640         err = ricoh619_write(info->dev->parent, CC_CTRL_REG, 0);
641         if (err < 0)
642                 goto out;
643         if (cc_stop_flag == 1)
644         {
645         
646                 /* Enable charging */
647                 err = ricoh619_set_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x03);
648                 if (err < 0)
649                         goto out;
650
651                 udelay(1000);
652
653                 /* Clear Charging Interrupt status */
654                 err = ricoh619_clr_bits(info->dev->parent,
655                                         RICOH619_INT_IR_CHGSTS1, 0x01);
656                 if (err < 0)
657                         goto out;
658
659                 /* ricoh619_read(info->dev->parent, RICOH619_INT_IR_CHGSTS1, &val);
660                 RICOH_FG_DBG("INT_IR_CHGSTS1 = 0x%x\n",val); */
661
662                 /* Enable Charging Interrupt */
663                 err = ricoh619_clr_bits(info->dev->parent,
664                                                 RICOH619_INT_MSK_CHGSTS1, 0x01);
665                 if (err < 0)
666                         goto out;
667         }
668         /* Read FA_CAP */
669         err = ricoh619_bulk_reads(info->dev->parent,
670                                  FA_CAP_H_REG, 2, fa_cap_reg);
671         if (err < 0)
672                 goto out;
673
674         /* fa_cap = *(uint16_t*)fa_cap_reg & 0x7fff; */
675         fa_cap = (fa_cap_reg[0] << 8 | fa_cap_reg[1]) & 0x7fff;
676
677         /* cc_sum = *(uint32_t*)cc_sum_reg; */
678         cc_sum = cc_sum_reg[0] << 24 | cc_sum_reg[1] << 16 |
679                                 cc_sum_reg[2] << 8 | cc_sum_reg[3];
680
681         /* calculation  two's complement of CC_SUM */
682         if (cc_sum & 0x80000000) {
683                 cc_sum = (cc_sum^0xffffffff)+0x01;
684                 *is_charging = false;           /* discharge */
685         }
686         /* (CC_SUM x 10000)/3600/FA_CAP */
687         *cc_cap = cc_sum*25/9/fa_cap;           /* unit is 0.01% */
688
689         //////////////////////////////////////////////////////////////////      
690         cc_cap_min = fa_cap*3600/100/100/100;   /* Unit is 0.0001% */
691         cc_cap_temp = cc_sum / cc_cap_min;
692                 
693         cc_cap_res = cc_cap_temp % 100;
694         
695         RICOH_FG_DBG("PMU: cc_sum = %d: cc_cap_res= %d: \n", cc_sum, cc_cap_res);
696         
697         
698         if(*is_charging) {
699                 info->soca->cc_cap_offset += cc_cap_res;
700                 if (info->soca->cc_cap_offset >= 100) {
701                         *cc_cap += 1;
702                         info->soca->cc_cap_offset %= 100;
703                 }
704         } else {
705                 info->soca->cc_cap_offset -= cc_cap_res;
706                 if (info->soca->cc_cap_offset <= -100) {
707                         *cc_cap += 1;
708                         info->soca->cc_cap_offset %= 100;
709                 }
710         }
711         RICOH_FG_DBG("PMU: cc_cap_offset= %d: \n", info->soca->cc_cap_offset);
712         
713         //////////////////////////////////////////////////////////////////
714         return 0;
715 out:
716         dev_err(info->dev, "Error !!-----\n");
717         return err;
718 }
719 /**
720 * Calculate target using capacity
721 **/
722 static int get_target_use_cap(struct ricoh619_battery_info *info)
723 {
724         int i,j;
725         int ocv_table[11];
726         int temp;
727 //      int Target_Cutoff_Vol = 0;
728         int Ocv_ZeroPer_now;
729         int Ibat_now;
730         int fa_cap,use_cap;
731         int FA_CAP_now;
732         int start_per = 0;
733         int RE_CAP_now;
734         int CC_OnePer_step;
735         int Ibat_min;
736
737 //      int Ocv_now;
738         int Ocv_now_table;
739 //      int soc_per;
740 //      int use_cap_now;
741         int Rsys_now;
742
743         /* get const value */
744         Ibat_min = -1 * info->soca->target_ibat;
745         if (info->soca->Ibat_ave > Ibat_min) /* I bat is minus */
746         {
747                 Ibat_now = Ibat_min;
748         } else {
749                 Ibat_now = info->soca->Ibat_ave;
750         }
751         fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
752                                                                 0x7fff);
753         use_cap = fa_cap - info->soca->re_cap_old;
754
755         /* get OCV table % */
756         for (i = 0; i <= 10; i = i+1) {
757                 temp = (battery_init_para[info->num][i*2]<<8)
758                          | (battery_init_para[info->num][i*2+1]);
759                 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
760                 temp = ((temp * 50000 * 10 / 4095) + 5) / 10;
761                 ocv_table[i] = temp;
762                 RICOH_FG_DBG("PMU : %s : ocv_table %d is %d v\n",__func__, i, ocv_table[i]);
763         }
764
765         /* Find out Current OCV */
766         i = info->soca->soc/1000;
767         j = info->soca->soc - info->soca->soc/1000*1000;
768         Ocv_now_table = ocv_table[i]*100+(ocv_table[i+1]-ocv_table[i])*j/10;
769
770                 Rsys_now = (info->soca->Vsys_ave - Ocv_now_table) / info->soca->Ibat_ave;       
771
772         Ocv_ZeroPer_now = info->soca->target_vsys * 1000 - Ibat_now * Rsys_now;
773
774         RICOH_FG_DBG("PMU: -------  Ocv_now_table= %d: Rsys_now= %d =======\n",
775                Ocv_now_table, Rsys_now);
776
777         RICOH_FG_DBG("PMU: -------  Rsys= %d: cutoff_ocv= %d: Ocv_ZeroPer_now= %d =======\n",
778                info->soca->Rsys, info->soca->cutoff_ocv, Ocv_ZeroPer_now);
779
780         /* get FA_CAP_now */
781
782         
783         for (i = 1; i < 11; i++) {
784                 RICOH_FG_DBG("PMU : %s : ocv_table %d is %d v Ocv_ZerPernow is %d\n",__func__, i, ocv_table[i],(Ocv_ZeroPer_now / 100));
785                 if (ocv_table[i] >= Ocv_ZeroPer_now / 100) {
786                         /* unit is 0.001% */
787                         start_per = Calc_Linear_Interpolation(
788                                 (i-1)*1000, ocv_table[i-1], i*1000,
789                                  ocv_table[i], (Ocv_ZeroPer_now / 100));
790                         i = 11;
791                 }
792         }
793
794         start_per = max(0, start_per);
795
796         FA_CAP_now = fa_cap * ((10000 - start_per) / 100 ) / 100;
797
798         RICOH_FG_DBG("PMU: -------  Target_Cutoff_Vol= %d: Ocv_ZeroPer_now= %d: start_per= %d =======\n",
799                Target_Cutoff_Vol, Ocv_ZeroPer_now, start_per);
800
801         /* get RE_CAP_now */
802         RE_CAP_now = FA_CAP_now - use_cap;
803         
804         if (RE_CAP_now < RE_CAP_GO_DOWN) {
805                 info->soca->hurry_up_flg = 1;
806         } else if (info->soca->Vsys_ave < info->soca->target_vsys*1000) {
807                 info->soca->hurry_up_flg = 1;
808         } else if (info->fg_poff_vbat != 0) {
809                 if (info->soca->Vbat_ave < info->fg_poff_vbat*1000) {
810                         info->soca->hurry_up_flg = 1;
811                 } else {
812                         info->soca->hurry_up_flg = 0;
813                 }
814         } else {
815                 info->soca->hurry_up_flg = 0;
816         }
817
818         /* get CC_OnePer_step */
819         if (info->soca->displayed_soc > 0) { /* avoid divide-by-0 */
820                 CC_OnePer_step = RE_CAP_now / (info->soca->displayed_soc / 100 + 1);
821         } else {
822                 CC_OnePer_step = 0;
823         }
824         /* get info->soca->target_use_cap */
825         info->soca->target_use_cap = use_cap + CC_OnePer_step;
826         
827         RICOH_FG_DBG("PMU: ------- FA_CAP_now= %d: RE_CAP_now= %d: CC_OnePer_step= %d: target_use_cap= %d: hurry_up_flg= %d -------\n",
828                FA_CAP_now, RE_CAP_now, CC_OnePer_step, info->soca->target_use_cap, info->soca->hurry_up_flg);
829         
830         return 0;
831 }
832 #ifdef ENABLE_OCV_TABLE_CALIB
833 /**
834 * Calibration OCV Table
835 * - Update the value of VBAT on 100% in OCV table 
836 *    if battery is Full charged.
837 * - int vbat_ocv <- unit is uV
838 **/
839 static int calib_ocvTable(struct ricoh619_battery_info *info, int vbat_ocv)
840 {
841         int ret;
842         int cutoff_ocv;
843         int i;
844         int ocv100_new;
845         int start_per = 0;
846         int end_per = 0;
847         
848         RICOH_FG_DBG("PMU: %s\n", __func__);
849         
850         if (info->soca->Ibat_ave > RICOH619_REL1_SEL_VALUE) {
851                 RICOH_FG_DBG("PMU: %s IBAT > 64mA -- Not Calibration --\n", __func__);
852                 return 0;
853         }
854         
855         if (vbat_ocv < info->soca->OCV100_max) {
856                 if (vbat_ocv < info->soca->OCV100_min)
857                         ocv100_new = info->soca->OCV100_min;
858                 else
859                         ocv100_new = vbat_ocv;
860         } else {
861                 ocv100_new = info->soca->OCV100_max;
862         }
863         RICOH_FG_DBG("PMU : %s :max %d min %d current %d\n",__func__,info->soca->OCV100_max,info->soca->OCV100_min,vbat_ocv);
864         RICOH_FG_DBG("PMU : %s : New OCV 100% = 0x%x\n",__func__,ocv100_new);
865         
866         /* FG_En Off */
867         ret = ricoh619_clr_bits(info->dev->parent, FG_CTRL_REG, 0x01);
868         if (ret < 0) {
869                 dev_err("PMU: %s Error in FG_En OFF\n", __func__);
870                 goto err;
871         }
872
873
874         //cutoff_ocv = (battery_init_para[info->num][0]<<8) | (battery_init_para[info->num][1]);
875         cutoff_ocv = get_OCV_voltage(info, 0);
876
877         info->soca->ocv_table_def[10] = info->soca->OCV100_max;
878
879         ricoh619_scaling_OCV_table(info, cutoff_ocv/1000, ocv100_new/1000, &start_per, &end_per);
880
881         ret = ricoh619_bulk_writes_bank1(info->dev->parent,
882                                 BAT_INIT_TOP_REG, 22, battery_init_para[info->num]);
883         if (ret < 0) {
884                 dev_err(info->dev, "batterry initialize error\n");
885                 goto err;
886         }
887
888         for (i = 0; i <= 10; i = i+1) {
889                 info->soca->ocv_table[i] = get_OCV_voltage(info, i);
890                 RICOH_FG_DBG("PMU: %s : * %d0%% voltage = %d uV\n",
891                                  __func__, i, info->soca->ocv_table[i]);
892         }
893         
894         /* FG_En on & Reset*/
895         ret = reset_FG_process(info);
896         if (ret < 0) {
897                 dev_err("PMU: %s Error in FG_En On & Reset\n", __func__);
898                 goto err;
899         }
900
901         RICOH_FG_DBG("PMU: %s Exit \n", __func__);
902         return 0;
903 err:
904         return ret;
905
906 }
907 #endif
908
909 static void ricoh619_displayed_work(struct work_struct *work)
910 {
911         int err;
912         uint8_t val;
913         uint8_t val2;
914         int soc_round;
915         int last_soc_round;
916         int last_disp_round;
917         int displayed_soc_temp;
918         int disp_dec;
919         int cc_cap = 0;
920         bool is_charging = true;
921         int re_cap,fa_cap,use_cap;
922         bool is_jeita_updated;
923         uint8_t reg_val;
924         int delay_flag = 0;
925         int Vbat = 0;
926         int Ibat = 0;
927         int Vsys = 0;
928         int temp_ocv;
929         int fc_delta = 0;
930
931         struct ricoh619_battery_info *info = container_of(work,
932         struct ricoh619_battery_info, displayed_work.work);
933
934         if (info->entry_factory_mode) {
935                 info->soca->status = RICOH619_SOCA_STABLE;
936                 info->soca->displayed_soc = -EINVAL;
937                 info->soca->ready_fg = 0;
938                 return;
939         }
940
941         mutex_lock(&info->lock);
942         
943         is_jeita_updated = false;
944
945         if ((RICOH619_SOCA_START == info->soca->status)
946                  || (RICOH619_SOCA_STABLE == info->soca->status)
947                  || (RICOH619_SOCA_FULL == info->soca->status))
948                 info->soca->ready_fg = 1;
949
950         /* judge Full state or Moni Vsys state */
951         if ((RICOH619_SOCA_DISP == info->soca->status)
952                  || (RICOH619_SOCA_STABLE == info->soca->status))
953         {
954                 /* caluc 95% ocv */
955                 temp_ocv = get_OCV_voltage(info, 10) -
956                                         (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))/2;
957                 
958                 if(g_full_flag == 1){   /* for issue 1 solution start*/
959                         info->soca->status = RICOH619_SOCA_FULL;
960                 }else if ((POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
961                         && (calc_ocv(info) > temp_ocv)) {
962                         info->soca->status = RICOH619_SOCA_FULL;
963                         g_full_flag = 0;
964                 } else if (info->soca->Ibat_ave >= -20) {
965                         /* for issue1 solution end */
966                         /* check Full state or not*/
967                         if ((calc_ocv(info) > (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*7/10))
968                                 || (POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
969                                 || (info->soca->displayed_soc > 9850))
970                         {
971                                 info->soca->status = RICOH619_SOCA_FULL;
972                                 g_full_flag = 0;
973                         } else if ((calc_ocv(info) > (get_OCV_voltage(info, 9)))
974                                 && (info->soca->Ibat_ave < 300))
975                         {
976                                 info->soca->status = RICOH619_SOCA_FULL;
977                                 g_full_flag = 0;
978                         }
979                 } else { /* dis-charging */
980                         if (info->soca->displayed_soc/100 < RICOH619_ENTER_LOW_VOL) {
981                                 info->soca->target_use_cap = 0;
982                                 info->soca->status = RICOH619_SOCA_LOW_VOL;
983                         }
984                 }
985         }
986
987         if (RICOH619_SOCA_STABLE == info->soca->status) {
988                 info->soca->soc = calc_capacity_2(info);
989                 info->soca->soc_delta = info->soca->soc - info->soca->last_soc;
990
991                 if (info->soca->soc_delta >= -100 && info->soca->soc_delta <= 100) {
992                         info->soca->displayed_soc = info->soca->soc;
993                 } else {
994                         info->soca->status = RICOH619_SOCA_DISP;
995                 }
996                 info->soca->last_soc = info->soca->soc;
997                 info->soca->soc_delta = 0;
998         } else if (RICOH619_SOCA_FULL == info->soca->status) {
999                 err = check_jeita_status(info, &is_jeita_updated);
1000                 if (err < 0) {
1001                         dev_err(info->dev, "Error in updating JEITA %d\n", err);
1002                         goto end_flow;
1003                 }
1004                 info->soca->soc = calc_capacity(info) * 100;
1005                 info->soca->last_soc = calc_capacity_2(info);   /* for DISP */
1006
1007                 if (info->soca->Ibat_ave >= -20) { /* charging */
1008                         if (0 == info->soca->jt_limit) {
1009                                 if (g_full_flag == 1) {
1010                                         
1011                                         if (POWER_SUPPLY_STATUS_FULL == info->soca->chg_status) {
1012                                                 if(info->soca->full_reset_count < RICOH619_UPDATE_COUNT_FULL_RESET) {
1013                                                         info->soca->full_reset_count++;
1014                                                 } else if (info->soca->full_reset_count < (RICOH619_UPDATE_COUNT_FULL_RESET + 1)) {
1015                                                         err = reset_FG_process(info);
1016                                                         if (err < 0)
1017                                                                 dev_err(info->dev, "Error in writing the control register\n");
1018                                                         info->soca->full_reset_count++;
1019                                                         goto end_flow;
1020                                                 } else if(info->soca->full_reset_count < (RICOH619_UPDATE_COUNT_FULL_RESET + 2)) {
1021                                                         info->soca->full_reset_count++;
1022                                                         info->soca->fc_cap = 0;
1023                                                         info->soca->soc_full = info->soca->soc;
1024                                                 }
1025                                         } else {
1026                                                 if(info->soca->fc_cap < -1 * 200) {
1027                                                         g_full_flag = 0;
1028                                                         info->soca->displayed_soc = 99 * 100;
1029                                                 }
1030                                                 info->soca->full_reset_count = 0;
1031                                         }
1032                                         
1033
1034                                         info->soca->chg_cmp_times = 0;
1035                                         err = calc_capacity_in_period(info, &cc_cap, &is_charging, true);
1036                                         if (err < 0)
1037                                         dev_err(info->dev, "Read cc_sum Error !!-----\n");
1038
1039                                         fc_delta = (is_charging == true) ? cc_cap : -cc_cap;
1040
1041                                         info->soca->fc_cap = info->soca->fc_cap + fc_delta;
1042
1043                                         if (g_full_flag == 1){
1044                                                 info->soca->displayed_soc = 100*100;
1045                                         }
1046                                 } else {
1047                                         if (calc_ocv(info) < (get_OCV_voltage(info, 8))) { /* fail safe*/
1048                                                 g_full_flag = 0;
1049                                                 info->soca->status = RICOH619_SOCA_DISP;
1050                                                 info->soca->soc_delta = 0;
1051                                         } else if ((POWER_SUPPLY_STATUS_FULL == info->soca->chg_status) 
1052                                                 && (info->soca->displayed_soc >= 9890)){
1053                                                 if(info->soca->chg_cmp_times > RICOH619_FULL_WAIT_TIME) {
1054                                                         info->soca->displayed_soc = 100*100;
1055                                                         g_full_flag = 1;
1056                                                         info->soca->full_reset_count = 0;
1057                                                         info->soca->soc_full = info->soca->soc;
1058                                                         info->soca->fc_cap = 0;
1059 #ifdef ENABLE_OCV_TABLE_CALIB
1060                                                         err = calib_ocvTable(info,calc_ocv(info));
1061                                                         if (err < 0)
1062                                                                 dev_err(info->dev, "Calibration OCV Error !!\n");
1063 #endif
1064                                                 } else {
1065                                                         info->soca->chg_cmp_times++;
1066                                                 }
1067                                         } else {
1068                                                 fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
1069                                                         0x7fff);
1070                                                 
1071                                                 if (info->soca->displayed_soc >= 9950) {
1072                                                         if((info->soca->soc_full - info->soca->soc) < 200) {
1073                                                                 goto end_flow;
1074                                                         }
1075                                                 }
1076                                                 info->soca->chg_cmp_times = 0;
1077
1078                                                 err = calc_capacity_in_period(info, &cc_cap, &is_charging, true);
1079                                                 if (err < 0)
1080                                                 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1081                                                 info->soca->cc_delta
1082                                                          = (is_charging == true) ? cc_cap : -cc_cap;
1083
1084                                                 if((POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
1085                                                 //      || (info->soca->Ibat_ave > 200))
1086                                                 || (info->soca->Ibat_ave < info->ch_icchg*50 + 100) || (info->soca->displayed_soc<9700))
1087                                                 {
1088                                                         info->soca->displayed_soc += 13 * 3000 / fa_cap;
1089                                                 }
1090                                                 else {
1091                                                         info->soca->displayed_soc
1092                                                        = info->soca->displayed_soc + info->soca->cc_delta*8/10;
1093                                                 }
1094                                                 
1095                                                 info->soca->displayed_soc
1096                                                          = min(10000, info->soca->displayed_soc);
1097                                                 info->soca->displayed_soc = max(0, info->soca->displayed_soc);
1098
1099                                                 if (info->soca->displayed_soc >= 9890) {
1100                                                         info->soca->displayed_soc = 99 * 100;
1101                                                 }
1102                                         }
1103                                 }
1104                         } else {
1105                                 info->soca->full_reset_count = 0;
1106                         }
1107                 } else { /* discharging */
1108                         if (info->soca->displayed_soc >= 9950) {
1109                                 if (info->soca->Ibat_ave <= -1 * RICOH619_REL1_SEL_VALUE) {
1110                                         if ((calc_ocv(info) < (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*3/10))
1111                                                 || ((info->soca->soc_full - info->soca->soc) > 200)) {
1112
1113                                                 g_full_flag = 0;
1114                                                 info->soca->full_reset_count = 0;
1115                                                 info->soca->displayed_soc = 100 * 100;
1116                                                 info->soca->status = RICOH619_SOCA_DISP;
1117                                                 info->soca->last_soc = info->soca->soc;
1118                                                 info->soca->soc_delta = 0;
1119                                         } else {
1120                                                 info->soca->displayed_soc = 100 * 100;
1121                                         }
1122                                 } else { /* into relaxation state */
1123                                         ricoh619_read(info->dev->parent, CHGSTATE_REG, &reg_val);
1124                                         if (reg_val & 0xc0) {
1125                                                 info->soca->displayed_soc = 100 * 100;
1126                                         } else {
1127                                                 g_full_flag = 0;
1128                                                 info->soca->full_reset_count = 0;
1129                                                 info->soca->displayed_soc = 100 * 100;
1130                                                 info->soca->status = RICOH619_SOCA_DISP;
1131                                                 info->soca->last_soc = info->soca->soc;
1132                                                 info->soca->soc_delta = 0;
1133                                         }
1134                                 }
1135                         } else {
1136                                 g_full_flag = 0;
1137                                 info->soca->status = RICOH619_SOCA_DISP;
1138                                 info->soca->soc_delta = 0;
1139                                 info->soca->full_reset_count = 0;
1140                                 info->soca->last_soc = info->soca->soc;
1141                         }
1142                 }
1143         } else if (RICOH619_SOCA_LOW_VOL == info->soca->status) {
1144                 if(info->soca->Ibat_ave >= 0) {
1145                         info->soca->soc = calc_capacity(info) * 100;
1146                         info->soca->status = RICOH619_SOCA_DISP;
1147                         info->soca->last_soc = info->soca->soc;
1148                         info->soca->soc_delta = 0;
1149                 } else {
1150                         re_cap = get_check_fuel_gauge_reg(info, RE_CAP_H_REG, RE_CAP_L_REG,
1151                                                                 0x7fff);
1152                         fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
1153                                                                 0x7fff);
1154                         use_cap = fa_cap - re_cap;
1155                         
1156                         if (info->soca->target_use_cap == 0) {
1157                                 info->soca->re_cap_old = re_cap;
1158                                 get_target_use_cap(info);
1159                         }
1160                         
1161                         if(use_cap >= info->soca->target_use_cap) {
1162                                 info->soca->displayed_soc = info->soca->displayed_soc - 100;
1163                                 info->soca->displayed_soc = max(0, info->soca->displayed_soc);
1164                                 info->soca->re_cap_old = re_cap;
1165                         } else if (info->soca->hurry_up_flg == 1) {
1166                                 info->soca->displayed_soc = info->soca->displayed_soc - 100;
1167                                 info->soca->displayed_soc = max(0, info->soca->displayed_soc);
1168                                 info->soca->re_cap_old = re_cap;
1169                         }
1170                         get_target_use_cap(info);
1171                         info->soca->soc = calc_capacity(info) * 100;
1172                 }
1173         }
1174         else if (RICOH619_SOCA_DISP == info->soca->status) {
1175
1176                 info->soca->soc = calc_capacity_2(info);
1177
1178                 soc_round = (info->soca->soc + 50) / 100;
1179                 last_soc_round = (info->soca->last_soc + 50) / 100;
1180                 last_disp_round = (info->soca->displayed_soc + 50) / 100;
1181
1182                 info->soca->soc_delta =
1183                         info->soca->soc_delta + (info->soca->soc - info->soca->last_soc);
1184
1185                 info->soca->last_soc = info->soca->soc;
1186                 /* six case */
1187                 if (last_disp_round == soc_round) {
1188                         /* if SOC == DISPLAY move to stable */
1189                         info->soca->displayed_soc = info->soca->soc ;
1190                         info->soca->status = RICOH619_SOCA_STABLE;
1191                         delay_flag = 1;
1192                 } else if (info->soca->Ibat_ave > 0) {
1193                         if ((0 == info->soca->jt_limit) || 
1194                         (POWER_SUPPLY_STATUS_FULL != info->soca->chg_status)) {
1195                                 /* Charge */
1196                                 if (last_disp_round < soc_round) {
1197                                         /* Case 1 : Charge, Display < SOC */
1198                                         if (info->soca->soc_delta >= 100) {
1199                                                 info->soca->displayed_soc
1200                                                         = last_disp_round * 100 + 50;
1201                                                 info->soca->soc_delta -= 100;
1202                                                 if (info->soca->soc_delta >= 100)
1203                                                         delay_flag = 1;
1204                                         } else {
1205                                                 info->soca->displayed_soc += 25;
1206                                                 disp_dec = info->soca->displayed_soc % 100;
1207                                                 if ((50 <= disp_dec) && (disp_dec <= 74))
1208                                                         info->soca->soc_delta = 0;
1209                                         }
1210                                         if ((info->soca->displayed_soc + 50)/100
1211                                                                  >= soc_round) {
1212                                                 info->soca->displayed_soc
1213                                                         = info->soca->soc ;
1214                                                 info->soca->status
1215                                                         = RICOH619_SOCA_STABLE;
1216                                                 delay_flag = 1;
1217                                         }
1218                                 } else if (last_disp_round > soc_round) {
1219                                         /* Case 2 : Charge, Display > SOC */
1220                                         if (info->soca->soc_delta >= 300) {
1221                                                 info->soca->displayed_soc += 100;
1222                                                 info->soca->soc_delta -= 300;
1223                                         }
1224                                         if ((info->soca->displayed_soc + 50)/100
1225                                                                  <= soc_round) {
1226                                                 info->soca->displayed_soc
1227                                                         = info->soca->soc ;
1228                                                 info->soca->status
1229                                                 = RICOH619_SOCA_STABLE;
1230                                                 delay_flag = 1;
1231                                         }
1232                                 }
1233                         } else {
1234                                 info->soca->soc_delta = 0;
1235                         }
1236                 } else {
1237                         /* Dis-Charge */
1238                         if (last_disp_round > soc_round) {
1239                                 /* Case 3 : Dis-Charge, Display > SOC */
1240                                 if (info->soca->soc_delta <= -100) {
1241                                         info->soca->displayed_soc
1242                                                 = last_disp_round * 100 - 75;
1243                                         info->soca->soc_delta += 100;
1244                                         if (info->soca->soc_delta <= -100)
1245                                                 delay_flag = 1;
1246                                 } else {
1247                                         info->soca->displayed_soc -= 25;
1248                                         disp_dec = info->soca->displayed_soc % 100;
1249                                         if ((25 <= disp_dec) && (disp_dec <= 49))
1250                                                 info->soca->soc_delta = 0;
1251                                 }
1252                                 if ((info->soca->displayed_soc + 50)/100
1253                                                          <= soc_round) {
1254                                         info->soca->displayed_soc
1255                                                 = info->soca->soc ;
1256                                         info->soca->status
1257                                                 = RICOH619_SOCA_STABLE;
1258                                         delay_flag = 1;
1259                                 }
1260                         } else if (last_disp_round < soc_round) {
1261                                 /* Case 4 : Dis-Charge, Display < SOC */
1262                                 if (info->soca->soc_delta <= -300) {
1263                                         info->soca->displayed_soc -= 100;
1264                                         info->soca->soc_delta += 300;
1265                                 }
1266                                 if ((info->soca->displayed_soc + 50)/100
1267                                                          >= soc_round) {
1268                                         info->soca->displayed_soc
1269                                                 = info->soca->soc ;
1270                                         info->soca->status
1271                                                 = RICOH619_SOCA_STABLE;
1272                                         delay_flag = 1;
1273                                 }
1274                         }
1275                 }
1276         } else if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
1277                 /* caluc 95% ocv */
1278                 temp_ocv = get_OCV_voltage(info, 10) -
1279                                         (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))/2;
1280                 
1281                 if(g_full_flag == 1){   /* for issue 1 solution start*/
1282                         info->soca->status = RICOH619_SOCA_FULL;
1283                         err = reset_FG_process(info);
1284                         if (err < 0)
1285                                 dev_err(info->dev, "Error in writing the control register\n");
1286                         
1287                         goto end_flow;
1288                 }else if ((POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
1289                         && (calc_ocv(info) > temp_ocv)) {
1290                         info->soca->status = RICOH619_SOCA_FULL;
1291                         g_full_flag = 0;
1292                         err = reset_FG_process(info);
1293                         if (err < 0)
1294                                 dev_err(info->dev, "Error in writing the control register\n");
1295                         goto end_flow;
1296                 } else if (info->soca->Ibat_ave >= -20) {
1297                         /* for issue1 solution end */
1298                         /* check Full state or not*/
1299                         if ((calc_ocv(info) > (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*7/10))
1300                                 || (POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
1301                                 || (info->soca->displayed_soc > 9850))
1302                         {
1303                                 info->soca->status = RICOH619_SOCA_FULL;
1304                                 g_full_flag = 0;
1305                                 err = reset_FG_process(info);
1306                                 if (err < 0)
1307                                         dev_err(info->dev, "Error in writing the control register\n");
1308                                 goto end_flow;
1309                         } else if ((calc_ocv(info) > (get_OCV_voltage(info, 9)))
1310                                 && (info->soca->Ibat_ave < 300))
1311                         {
1312                                 info->soca->status = RICOH619_SOCA_FULL;
1313                                 g_full_flag = 0;
1314                                 err = reset_FG_process(info);
1315                                 if (err < 0)
1316                                         dev_err(info->dev, "Error in writing the control register\n");                          
1317                                 goto end_flow;
1318                         }
1319                 }
1320
1321                 err = ricoh619_read(info->dev->parent, PSWR_REG, &val);
1322                 val &= 0x7f;
1323                 info->soca->soc = val * 100;
1324                 if (err < 0) {
1325                         dev_err(info->dev,
1326                                  "Error in reading PSWR_REG %d\n", err);
1327                         info->soca->soc
1328                                  = calc_capacity(info) * 100;
1329                 }
1330
1331                 err = calc_capacity_in_period(info, &cc_cap,
1332                                                  &is_charging, false);
1333                 if (err < 0)
1334                         dev_err(info->dev, "Read cc_sum Error !!-----\n");
1335
1336                 info->soca->cc_delta
1337                          = (is_charging == true) ? cc_cap : -cc_cap;
1338
1339                 displayed_soc_temp
1340                        = info->soca->soc + info->soca->cc_delta;
1341                 if (displayed_soc_temp < 0)
1342                         displayed_soc_temp = 0;
1343                 displayed_soc_temp
1344                          = min(9850, displayed_soc_temp);
1345                 displayed_soc_temp = max(0, displayed_soc_temp);
1346
1347                 info->soca->displayed_soc = displayed_soc_temp;
1348
1349         } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
1350                 /* No update */
1351         } else if (RICOH619_SOCA_START == info->soca->status) {
1352
1353                 err = measure_Ibatt_FG(info, &Ibat);
1354                 err = measure_vbatt_FG(info, &Vbat);
1355                 err = measure_vsys_ADC(info, &Vsys);
1356
1357                 info->soca->Ibat_ave = Ibat;
1358                 info->soca->Vbat_ave = Vbat;
1359                 info->soca->Vsys_ave = Vsys;
1360
1361                 err = check_jeita_status(info, &is_jeita_updated);
1362                 is_jeita_updated = false;
1363                 if (err < 0) {
1364                         dev_err(info->dev, "Error in updating JEITA %d\n", err);
1365                 }
1366                 err = ricoh619_read(info->dev->parent, PSWR_REG, &val);
1367                 val &= 0x7f;
1368                 if (info->first_pwon) {
1369                         info->soca->soc = calc_capacity(info) * 100;
1370                         val = (info->soca->soc + 50)/100;
1371                         val &= 0x7f;
1372                         err = ricoh619_write(info->dev->parent, PSWR_REG, val);
1373                         if (err < 0)
1374                                 dev_err(info->dev, "Error in writing PSWR_REG\n");
1375                         g_soc = val;
1376
1377                         if ((info->soca->soc == 0) && (calc_ocv(info)
1378                                         < get_OCV_voltage(info, 0))) {
1379                                 info->soca->displayed_soc = 0;
1380                                 info->soca->status = RICOH619_SOCA_ZERO;
1381                         } else {
1382                                 if (0 == info->soca->jt_limit) {
1383                                         check_charge_status_2(info, info->soca->soc);
1384                                 } else {
1385                                         info->soca->displayed_soc = info->soca->soc;
1386                                 }
1387                                 if (Ibat < 0) {
1388                                         if (info->soca->displayed_soc < 300) {
1389                                                 info->soca->target_use_cap = 0;
1390                                                 info->soca->status = RICOH619_SOCA_LOW_VOL;
1391                                         } else {
1392                                                 if ((info->fg_poff_vbat != 0)
1393                                                       && (Vbat < info->fg_poff_vbat * 1000) ){
1394                                                           info->soca->target_use_cap = 0;
1395                                                           info->soca->status = RICOH619_SOCA_LOW_VOL;
1396                                                   } else { 
1397                                                           info->soca->status = RICOH619_SOCA_UNSTABLE;
1398                                                   }
1399                                         }
1400                                 } else {
1401                                         info->soca->status = RICOH619_SOCA_UNSTABLE;
1402                                 }
1403                         }
1404                 } else if (g_fg_on_mode && (val == 0x7f)) {
1405                         info->soca->soc = calc_capacity(info) * 100;
1406                         if ((info->soca->soc == 0) && (calc_ocv(info)
1407                                         < get_OCV_voltage(info, 0))) {
1408                                 info->soca->displayed_soc = 0;
1409                                 info->soca->status = RICOH619_SOCA_ZERO;
1410                         } else {
1411                                 if (0 == info->soca->jt_limit) {
1412                                         check_charge_status_2(info, info->soca->soc);
1413                                 } else {
1414                                         info->soca->displayed_soc = info->soca->soc;
1415                                 }
1416                                 info->soca->last_soc = info->soca->soc;
1417                                 info->soca->status = RICOH619_SOCA_STABLE;
1418                         }
1419                 } else {
1420                         info->soca->soc = val * 100;
1421                         if (err < 0) {
1422                                 dev_err(info->dev,
1423                                          "Error in reading PSWR_REG %d\n", err);
1424                                 info->soca->soc
1425                                          = calc_capacity(info) * 100;
1426                         }
1427
1428                         err = calc_capacity_in_period(info, &cc_cap,
1429                                                          &is_charging, false);
1430                         if (err < 0)
1431                                 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1432
1433                         info->soca->cc_delta
1434                                  = (is_charging == true) ? cc_cap : -cc_cap;
1435                         if (calc_ocv(info) < get_OCV_voltage(info, 0)) {
1436                                 info->soca->displayed_soc = 0;
1437                                 info->soca->status = RICOH619_SOCA_ZERO;
1438                         } else {
1439                                 displayed_soc_temp
1440                                        = info->soca->soc + info->soca->cc_delta;
1441                                 if (displayed_soc_temp < 0)
1442                                         displayed_soc_temp = 0;
1443                                 displayed_soc_temp
1444                                          = min(10000, displayed_soc_temp);
1445                                 displayed_soc_temp = max(0, displayed_soc_temp);
1446                                 if (0 == info->soca->jt_limit) {
1447                                         check_charge_status_2(info, displayed_soc_temp);
1448                                 } else {
1449                                         info->soca->displayed_soc = displayed_soc_temp;
1450                                 }
1451                                 info->soca->last_soc = calc_capacity(info) * 100;
1452                                 if (Ibat < 0) {
1453                                         if (info->soca->displayed_soc < 300) {
1454                                                 info->soca->target_use_cap = 0;
1455                                                 info->soca->status = RICOH619_SOCA_LOW_VOL;
1456                                         } else {
1457                                                 if ((info->fg_poff_vbat != 0)
1458                                                       && (Vbat < info->fg_poff_vbat * 1000)){
1459                                                           info->soca->target_use_cap = 0;
1460                                                           info->soca->status = RICOH619_SOCA_LOW_VOL;
1461                                                   } else { 
1462                                                           info->soca->status = RICOH619_SOCA_UNSTABLE;
1463                                                   }
1464                                         }
1465                                 } else {
1466                                         if(info->soca->displayed_soc >= 9850)
1467                                         {
1468                                                 info->soca->displayed_soc = 10000;
1469                                                 info->chg_complete_tm_ov_flag = 1;
1470                                         }
1471                                         info->soca->status = RICOH619_SOCA_UNSTABLE;
1472                                 }
1473                         }
1474                 }
1475         } else if (RICOH619_SOCA_ZERO == info->soca->status) {
1476                 if (calc_ocv(info) > get_OCV_voltage(info, 0)) {
1477                         err = reset_FG_process(info);
1478                         if (err < 0)
1479                                 dev_err(info->dev, "Error in writing the control register\n");
1480                         info->soca->last_soc = calc_capacity_2(info);
1481                         info->soca->status = RICOH619_SOCA_STABLE;
1482                 }
1483                 info->soca->displayed_soc = 0;
1484         }
1485 end_flow:
1486         /* keep DSOC = 1 when Vbat is over 3.4V*/
1487         if( info->fg_poff_vbat != 0) {
1488                 if (info->soca->zero_flg == 1) {
1489                         if(info->soca->Ibat_ave >= 0) {
1490                                 info->soca->zero_flg = 0;
1491                         }
1492                         info->soca->displayed_soc = 0;
1493                 } else if (info->soca->displayed_soc < 50) {
1494                         if (info->soca->Vbat_ave < 2000*1000) { /* error value */
1495                                 info->soca->displayed_soc = 100;
1496                         } else if (info->soca->Vbat_ave < info->fg_poff_vbat*1000) {
1497                                 info->soca->displayed_soc = 0;
1498                                 info->soca->zero_flg = 1;
1499                         } else {
1500                                 info->soca->displayed_soc = 100;
1501                         }
1502                 }
1503         }
1504
1505         if (g_fg_on_mode
1506                  && (info->soca->status == RICOH619_SOCA_STABLE)) {
1507                 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
1508                 if (err < 0)
1509                         dev_err(info->dev, "Error in writing PSWR_REG\n");
1510                 g_soc = 0x7F;
1511                 err = calc_capacity_in_period(info, &cc_cap,
1512                                                         &is_charging, true);
1513                 if (err < 0)
1514                         dev_err(info->dev, "Read cc_sum Error !!-----\n");
1515
1516         } else if (RICOH619_SOCA_UNSTABLE != info->soca->status) {
1517                 if ((info->soca->displayed_soc + 50) / 100 <= 1) {
1518                         val = 1;
1519                 } else {
1520                         val = (info->soca->displayed_soc + 50)/100;
1521                         val &= 0x7f;
1522                 }
1523                 err = ricoh619_write(info->dev->parent, PSWR_REG, val);
1524                 if (err < 0)
1525                         dev_err(info->dev, "Error in writing PSWR_REG\n");
1526
1527                 g_soc = val;
1528
1529                 err = calc_capacity_in_period(info, &cc_cap,
1530                                                          &is_charging, true);
1531                 if (err < 0)
1532                         dev_err(info->dev, "Read cc_sum Error !!-----\n");
1533         }
1534         
1535         RICOH_FG_DBG("PMU: ------- STATUS= %d: IBAT= %d: VSYS= %d: VBAT= %d: DSOC= %d: RSOC= %d: -------\n",
1536                info->soca->status, info->soca->Ibat_ave, info->soca->Vsys_ave, info->soca->Vbat_ave,
1537         info->soca->displayed_soc, info->soca->soc);
1538
1539 #ifdef DISABLE_CHARGER_TIMER
1540         /* clear charger timer */
1541         if ( info->soca->chg_status == POWER_SUPPLY_STATUS_CHARGING ) {
1542                 err = ricoh619_read(info->dev->parent, TIMSET_REG, &val);
1543                 if (err < 0)
1544                         dev_err(info->dev,
1545                         "Error in read TIMSET_REG%d\n", err);
1546                 /* to check bit 0-1 */
1547                 val2 = val & 0x03;
1548
1549                 if (val2 == 0x02){
1550                         /* set rapid timer 240 -> 300 */
1551                         err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x03);
1552                         if (err < 0) {
1553                                 dev_err(info->dev, "Error in writing the control register\n");
1554                         }
1555                 } else {
1556                         /* set rapid timer 300 -> 240 */
1557                         err = ricoh619_clr_bits(info->dev->parent, TIMSET_REG, 0x01);
1558                         err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x02);
1559                         if (err < 0) {
1560                                 dev_err(info->dev, "Error in writing the control register\n");
1561                         }
1562                 }
1563         }
1564 #endif
1565
1566         if (0 == info->soca->ready_fg)
1567                 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1568                                          RICOH619_FG_RESET_TIME * HZ);
1569         else if (delay_flag == 1)
1570                 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1571                                          RICOH619_DELAY_TIME * HZ);
1572         else if (RICOH619_SOCA_DISP == info->soca->status)
1573                 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1574                                          RICOH619_DISPLAY_UPDATE_TIME * HZ);
1575         else if (info->soca->hurry_up_flg == 1)
1576                 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1577                                          RICOH619_LOW_VOL_DOWN_TIME * HZ);
1578         else
1579                 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1580                                          RICOH619_DISPLAY_UPDATE_TIME * HZ);
1581
1582         mutex_unlock(&info->lock);
1583
1584         if((true == is_jeita_updated)
1585         || (info->soca->last_displayed_soc/100 != (info->soca->displayed_soc+50)/100))
1586                 power_supply_changed(&info->battery);
1587
1588         info->soca->last_displayed_soc = info->soca->displayed_soc+50;
1589
1590         if((info->soca->displayed_soc >= 9850) && (info->soca->Ibat_ave > -20) && (info->capacity < 100))
1591         {
1592                 if(info->chg_complete_rd_flag == 0)
1593                 {
1594                         info->chg_complete_rd_flag = 1;
1595                         info->chg_complete_rd_cnt = 0;
1596                         queue_delayed_work(info->monitor_wqueue, &info->charge_complete_ready, 0);
1597                 }
1598         }
1599         else
1600         {
1601                 info->chg_complete_rd_flag = 0;
1602         }
1603
1604         if(info->chg_complete_tm_ov_flag == 1)
1605         {
1606                 if(info->soca->displayed_soc < 9850 || info->soca->Ibat_ave < -20)
1607                 {
1608                         info->chg_complete_tm_ov_flag = 0;
1609                         power_supply_changed(&info->battery);
1610                 }
1611         }
1612         return;
1613 }
1614
1615 static void ricoh619_stable_charge_countdown_work(struct work_struct *work)
1616 {
1617         int ret;
1618         int max = 0;
1619         int min = 100;
1620         int i;
1621         struct ricoh619_battery_info *info = container_of(work,
1622                 struct ricoh619_battery_info, charge_stable_work.work);
1623
1624         if (info->entry_factory_mode)
1625                 return;
1626
1627         mutex_lock(&info->lock);
1628         if (RICOH619_SOCA_FG_RESET == info->soca->status)
1629                 info->soca->ready_fg = 1;
1630
1631         if (2 <= info->soca->stable_count) {
1632                 if (3 == info->soca->stable_count
1633                         && RICOH619_SOCA_FG_RESET == info->soca->status) {
1634                         ret = reset_FG_process(info);
1635                         if (ret < 0)
1636                                 dev_err(info->dev, "Error in writing the control register\n");
1637                 }
1638                 info->soca->stable_count = info->soca->stable_count - 1;
1639                 queue_delayed_work(info->monitor_wqueue,
1640                                          &info->charge_stable_work,
1641                                          RICOH619_FG_STABLE_TIME * HZ / 10);
1642         } else if (0 >= info->soca->stable_count) {
1643                 /* Finished queue, ignore */
1644         } else if (1 == info->soca->stable_count) {
1645                 if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
1646                         /* Judge if FG need reset or Not */
1647                         info->soca->soc = calc_capacity(info) * 100;
1648                         if (info->chg_ctr != 0) {
1649                                 queue_delayed_work(info->monitor_wqueue,
1650                                          &info->charge_stable_work,
1651                                          RICOH619_FG_STABLE_TIME * HZ / 10);
1652                                 mutex_unlock(&info->lock);
1653                                 return;
1654                         }
1655                         /* Do reset setting */
1656                         ret = reset_FG_process(info);
1657                         if (ret < 0)
1658                                 dev_err(info->dev, "Error in writing the control register\n");
1659
1660                         info->soca->status = RICOH619_SOCA_FG_RESET;
1661
1662                         /* Delay for addition Reset Time (6s) */
1663                         queue_delayed_work(info->monitor_wqueue,
1664                                          &info->charge_stable_work,
1665                                          RICOH619_FG_RESET_TIME*HZ);
1666                 } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
1667                         info->soca->reset_soc[2] = info->soca->reset_soc[1];
1668                         info->soca->reset_soc[1] = info->soca->reset_soc[0];
1669                         info->soca->reset_soc[0] = calc_capacity(info) * 100;
1670                         info->soca->reset_count++;
1671
1672                         if (info->soca->reset_count > 10) {
1673                                 /* Reset finished; */
1674                                 info->soca->soc = info->soca->reset_soc[0];
1675                                 info->soca->stable_count = 0;
1676                                 goto adjust;
1677                         }
1678
1679                         for (i = 0; i < 3; i++) {
1680                                 if (max < info->soca->reset_soc[i]/100)
1681                                         max = info->soca->reset_soc[i]/100;
1682                                 if (min > info->soca->reset_soc[i]/100)
1683                                         min = info->soca->reset_soc[i]/100;
1684                         }
1685
1686                         if ((info->soca->reset_count > 3) && ((max - min)
1687                                         < RICOH619_MAX_RESET_SOC_DIFF)) {
1688                                 /* Reset finished; */
1689                                 info->soca->soc = info->soca->reset_soc[0];
1690                                 info->soca->stable_count = 0;
1691                                 goto adjust;
1692                         } else {
1693                                 /* Do reset setting */
1694                                 ret = reset_FG_process(info);
1695                                 if (ret < 0)
1696                                         dev_err(info->dev, "Error in writing the control register\n");
1697
1698                                 /* Delay for addition Reset Time (6s) */
1699                                 queue_delayed_work(info->monitor_wqueue,
1700                                                  &info->charge_stable_work,
1701                                                  RICOH619_FG_RESET_TIME*HZ);
1702                         }
1703                 /* Finished queue From now, select FG as result; */
1704                 } else if (RICOH619_SOCA_START == info->soca->status) {
1705                         /* Normal condition */
1706                 } else { /* other state ZERO/DISP/STABLE */
1707                         info->soca->stable_count = 0;
1708                 }
1709
1710                 mutex_unlock(&info->lock);
1711                 return;
1712
1713 adjust:
1714                 info->soca->last_soc = info->soca->soc;
1715                 info->soca->status = RICOH619_SOCA_DISP;
1716                 info->soca->soc_delta = 0;
1717
1718         }
1719         mutex_unlock(&info->lock);
1720         return;
1721 }
1722
1723 static void ricoh619_charge_monitor_work(struct work_struct *work)
1724 {
1725         struct ricoh619_battery_info *info = container_of(work,
1726                 struct ricoh619_battery_info, charge_monitor_work.work);
1727
1728         get_power_supply_status(info);
1729
1730         if (POWER_SUPPLY_STATUS_DISCHARGING == info->soca->chg_status
1731                 || POWER_SUPPLY_STATUS_NOT_CHARGING == info->soca->chg_status) {
1732                 switch (info->soca->dischg_state) {
1733                 case    0:
1734                         info->soca->dischg_state = 1;
1735                         break;
1736                 case    1:
1737                         info->soca->dischg_state = 2;
1738                         break;
1739         
1740                 case    2:
1741                 default:
1742                         break;
1743                 }
1744         } else {
1745                 info->soca->dischg_state = 0;
1746         }
1747
1748         queue_delayed_work(info->monitor_wqueue, &info->charge_monitor_work,
1749                                          RICOH619_CHARGE_MONITOR_TIME * HZ);
1750
1751         return;
1752 }
1753
1754 static void ricoh619_get_charge_work(struct work_struct *work)
1755 {
1756         struct ricoh619_battery_info *info = container_of(work,
1757                 struct ricoh619_battery_info, get_charge_work.work);
1758
1759         int Vbat_temp, Vsys_temp, Ibat_temp;
1760         int Vbat_sort[RICOH619_GET_CHARGE_NUM];
1761         int Vsys_sort[RICOH619_GET_CHARGE_NUM];
1762         int Ibat_sort[RICOH619_GET_CHARGE_NUM];
1763         int i, j;
1764         int ret;
1765
1766         mutex_lock(&info->lock);
1767
1768         for (i = RICOH619_GET_CHARGE_NUM-1; i > 0; i--) {
1769                 if (0 == info->soca->chg_count) {
1770                         info->soca->Vbat[i] = 0;
1771                         info->soca->Vsys[i] = 0;
1772                         info->soca->Ibat[i] = 0;
1773                 } else {
1774                         info->soca->Vbat[i] = info->soca->Vbat[i-1];
1775                         info->soca->Vsys[i] = info->soca->Vsys[i-1];
1776                         info->soca->Ibat[i] = info->soca->Ibat[i-1];
1777                 }
1778         }
1779
1780         ret = measure_vbatt_FG(info, &info->soca->Vbat[0]);
1781         ret = measure_vsys_ADC(info, &info->soca->Vsys[0]);
1782         ret = measure_Ibatt_FG(info, &info->soca->Ibat[0]);
1783
1784         info->soca->chg_count++;
1785
1786         if (RICOH619_GET_CHARGE_NUM != info->soca->chg_count) {
1787                 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1788                                          RICOH619_CHARGE_CALC_TIME * HZ);
1789                 mutex_unlock(&info->lock);
1790                 return ;
1791         }
1792
1793         for (i = 0; i < RICOH619_GET_CHARGE_NUM; i++) {
1794                 Vbat_sort[i] = info->soca->Vbat[i];
1795                 Vsys_sort[i] = info->soca->Vsys[i];
1796                 Ibat_sort[i] = info->soca->Ibat[i];
1797         }
1798
1799         Vbat_temp = 0;
1800         Vsys_temp = 0;
1801         Ibat_temp = 0;
1802         for (i = 0; i < RICOH619_GET_CHARGE_NUM - 1; i++) {
1803                 for (j = RICOH619_GET_CHARGE_NUM - 1; j > i; j--) {
1804                         if (Vbat_sort[j - 1] > Vbat_sort[j]) {
1805                                 Vbat_temp = Vbat_sort[j];
1806                                 Vbat_sort[j] = Vbat_sort[j - 1];
1807                                 Vbat_sort[j - 1] = Vbat_temp;
1808                         }
1809                         if (Vsys_sort[j - 1] > Vsys_sort[j]) {
1810                                 Vsys_temp = Vsys_sort[j];
1811                                 Vsys_sort[j] = Vsys_sort[j - 1];
1812                                 Vsys_sort[j - 1] = Vsys_temp;
1813                         }
1814                         if (Ibat_sort[j - 1] > Ibat_sort[j]) {
1815                                 Ibat_temp = Ibat_sort[j];
1816                                 Ibat_sort[j] = Ibat_sort[j - 1];
1817                                 Ibat_sort[j - 1] = Ibat_temp;
1818                         }
1819                 }
1820         }
1821
1822         Vbat_temp = 0;
1823         Vsys_temp = 0;
1824         Ibat_temp = 0;
1825         for (i = 3; i < RICOH619_GET_CHARGE_NUM-3; i++) {
1826                 Vbat_temp = Vbat_temp + Vbat_sort[i];
1827                 Vsys_temp = Vsys_temp + Vsys_sort[i];
1828                 Ibat_temp = Ibat_temp + Ibat_sort[i];
1829         }
1830         Vbat_temp = Vbat_temp / (RICOH619_GET_CHARGE_NUM - 6);
1831         Vsys_temp = Vsys_temp / (RICOH619_GET_CHARGE_NUM - 6);
1832         Ibat_temp = Ibat_temp / (RICOH619_GET_CHARGE_NUM - 6);
1833
1834         if (0 == info->soca->chg_count) {
1835                 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1836                                  RICOH619_CHARGE_UPDATE_TIME * HZ);
1837                 mutex_unlock(&info->lock);
1838                 return;
1839         } else {
1840                 info->soca->Vbat_ave = Vbat_temp;
1841                 info->soca->Vsys_ave = Vsys_temp;
1842                 info->soca->Ibat_ave = Ibat_temp;
1843         }
1844
1845         info->soca->chg_count = 0;
1846         queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1847                                  RICOH619_CHARGE_UPDATE_TIME * HZ);
1848         mutex_unlock(&info->lock);
1849         return;
1850 }
1851
1852 /* Initial setting of FuelGauge SOCA function */
1853 static int ricoh619_init_fgsoca(struct ricoh619_battery_info *info)
1854 {
1855         int i;
1856         int err;
1857         uint8_t val;
1858         int cc_cap = 0;
1859         bool is_charging = true;
1860
1861         for (i = 0; i <= 10; i = i+1) {
1862                 info->soca->ocv_table[i] = get_OCV_voltage(info, i);
1863                 RICOH_FG_DBG("PMU: %s : * %d0%% voltage = %d uV\n",
1864                                  __func__, i, info->soca->ocv_table[i]);
1865         }
1866
1867         for (i = 0; i < 3; i = i+1)
1868                 info->soca->reset_soc[i] = 0;
1869         info->soca->reset_count = 0;
1870
1871         if (info->first_pwon) {
1872
1873                 err = ricoh619_read(info->dev->parent, CHGISET_REG, &val);
1874                 if (err < 0)
1875                         dev_err(info->dev,
1876                         "Error in read CHGISET_REG%d\n", err);
1877
1878                 err = ricoh619_write(info->dev->parent, CHGISET_REG, 0);
1879                 if (err < 0)
1880                         dev_err(info->dev,
1881                         "Error in writing CHGISET_REG%d\n", err);
1882                 msleep(1000);
1883
1884                 if (!info->entry_factory_mode) {
1885                         err = ricoh619_write(info->dev->parent,
1886                                                         FG_CTRL_REG, 0x51);
1887                         if (err < 0)
1888                                 dev_err(info->dev, "Error in writing the control register\n");
1889                 }
1890
1891                 err = calc_capacity_in_period(info, &cc_cap, &is_charging, true);
1892
1893                 msleep(6000);
1894
1895                 err = ricoh619_write(info->dev->parent, CHGISET_REG, val);
1896                 if (err < 0)
1897                         dev_err(info->dev,
1898                         "Error in writing CHGISET_REG%d\n", err);
1899         }
1900         
1901         /* Rbat : Transfer */
1902         info->soca->Rbat = get_OCV_init_Data(info, 12) * 1000 / 512
1903                                                          * 5000 / 4095;
1904         info->soca->n_cap = get_OCV_init_Data(info, 11);
1905
1906
1907         info->soca->displayed_soc = 0;
1908         info->soca->last_displayed_soc = 0;
1909         info->soca->suspend_soc = 0;
1910         info->soca->ready_fg = 0;
1911         info->soca->soc_delta = 0;
1912         info->soca->full_reset_count = 0;
1913         info->soca->soc_full = 0;
1914         info->soca->fc_cap = 0;
1915         info->soca->status = RICOH619_SOCA_START;
1916         /* stable count down 11->2, 1: reset; 0: Finished; */
1917         info->soca->stable_count = 11;
1918         info->soca->chg_cmp_times = 0;
1919         info->soca->dischg_state = 0;
1920         info->soca->Vbat_ave = 0;
1921         info->soca->Vbat_old = 0;
1922         info->soca->Vsys_ave = 0;
1923         info->soca->Ibat_ave = 0;
1924         info->soca->chg_count = 0;
1925         info->soca->target_use_cap = 0;
1926         info->soca->hurry_up_flg = 0;
1927         info->soca->re_cap_old = 0;
1928         info->soca->jt_limit = 0;
1929         info->soca->zero_flg = 0;
1930         info->soca->cc_cap_offset = 0;
1931
1932         for (i = 0; i < 11; i++) {
1933                 info->soca->ocv_table_low[i] = 0;
1934         }
1935
1936         for (i = 0; i < RICOH619_GET_CHARGE_NUM; i++) {
1937                 info->soca->Vbat[i] = 0;
1938                 info->soca->Vsys[i] = 0;
1939                 info->soca->Ibat[i] = 0;
1940         }
1941
1942         g_full_flag = 0;
1943         
1944 #ifdef ENABLE_FG_KEEP_ON_MODE
1945         g_fg_on_mode = 1;
1946 #else
1947         g_fg_on_mode = 0;
1948 #endif
1949
1950         /* Start first Display job */
1951         queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1952                                                    RICOH619_FG_RESET_TIME*HZ);
1953
1954         /* Start first Waiting stable job */
1955         queue_delayed_work(info->monitor_wqueue, &info->charge_stable_work,
1956                    RICOH619_FG_STABLE_TIME*HZ/10);
1957
1958         queue_delayed_work(info->monitor_wqueue, &info->charge_monitor_work,
1959                                          RICOH619_CHARGE_MONITOR_TIME * HZ);
1960
1961         queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1962                                          RICOH619_CHARGE_MONITOR_TIME * HZ);
1963         if (info->jt_en) {
1964                 if (info->jt_hw_sw) {
1965                         /* Enable JEITA function supported by H/W */
1966                         err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x04);
1967                         if (err < 0)
1968                                 dev_err(info->dev, "Error in writing the control register\n");
1969                 } else {
1970                         /* Disable JEITA function supported by H/W */
1971                         err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x04);
1972                         if (err < 0)
1973                                 dev_err(info->dev, "Error in writing the control register\n");
1974                         queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
1975                                                          RICOH619_FG_RESET_TIME * HZ);
1976                 }
1977         } else {
1978                 /* Disable JEITA function supported by H/W */
1979                 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x04);
1980                 if (err < 0)
1981                         dev_err(info->dev, "Error in writing the control register\n");
1982         }
1983
1984         RICOH_FG_DBG("PMU: %s : * Rbat = %d mOhm   n_cap = %d mAH\n",
1985                          __func__, info->soca->Rbat, info->soca->n_cap);
1986         return 1;
1987 }
1988 #endif
1989
1990 static void ricoh619_charging_complete_work(struct work_struct *work)
1991 {
1992         struct ricoh619_battery_info *info = container_of(work,
1993                 struct ricoh619_battery_info, charge_complete_ready.work);
1994
1995         uint8_t time_ov_flag;
1996         RICOH_FG_DBG("PMU: %s\n", __func__);
1997         RICOH_FG_DBG("info->chg_complete_rd_cnt = %d\n", info->chg_complete_rd_cnt);
1998         RICOH_FG_DBG("info->chg_complete_rd_flag = %d\n", info->chg_complete_rd_flag);
1999         RICOH_FG_DBG("info->chg_complete_tm_ov_flag = %d\n", info->chg_complete_tm_ov_flag);
2000         RICOH_FG_DBG("time_ov_flag = %d\n", time_ov_flag);
2001         
2002         if(info->chg_complete_rd_flag == 1)
2003         {
2004                 // start chg 99per to 100per timer
2005                 time_ov_flag = 0;
2006                 info->chg_complete_rd_flag = 2;
2007                 info->chg_complete_tm_ov_flag = 0;
2008         }
2009         else
2010         {
2011                 if(info->capacity == 100)
2012                 {
2013                         // battery arriver to 100% earlier than time ov
2014                         time_ov_flag = 1;
2015                         info->chg_complete_rd_cnt = 0;
2016                         info->chg_complete_tm_ov_flag = 1;
2017                 }
2018                 else if(info->chg_complete_rd_cnt > RICOH619_TIME_CHG_COUNT)
2019                 {
2020                         // chg timer ov before cap arrive to 100%
2021                         time_ov_flag = 1;
2022                         info->chg_complete_tm_ov_flag = 1;
2023                         info->chg_complete_rd_cnt = 0;
2024                         info->soca->status = RICOH619_SOCA_FULL;
2025                         power_supply_changed(&info->battery);
2026                 }
2027                 else
2028                 {
2029                         time_ov_flag = 0;
2030                         info->chg_complete_tm_ov_flag = 0;
2031                 }
2032         }
2033
2034         if(time_ov_flag == 0)
2035         {
2036                 info->chg_complete_rd_cnt++;
2037                 queue_delayed_work(info->monitor_wqueue, &info->charge_complete_ready, 
2038                         RICOH619_TIME_CHG_STEP);
2039         }
2040         else
2041         {
2042                 time_ov_flag = 0;
2043         }
2044
2045         RICOH_FG_DBG("PMU2: %s return\n", __func__);
2046         RICOH_FG_DBG("info->chg_complete_rd_cnt = %d\n", info->chg_complete_rd_cnt);
2047         RICOH_FG_DBG("info->chg_complete_rd_flag = %d\n", info->chg_complete_rd_flag);
2048         RICOH_FG_DBG("info->chg_complete_tm_ov_flag = %d\n", info->chg_complete_tm_ov_flag);
2049         RICOH_FG_DBG("time_ov_flag = %d\n", time_ov_flag);
2050
2051 }
2052 static void ricoh619_changed_work(struct work_struct *work)
2053 {
2054         struct ricoh619_battery_info *info = container_of(work,
2055                 struct ricoh619_battery_info, changed_work.work);
2056
2057         RICOH_FG_DBG("PMU: %s\n", __func__);
2058         power_supply_changed(&info->battery);
2059
2060         return;
2061 }
2062
2063 static int check_jeita_status(struct ricoh619_battery_info *info, bool *is_jeita_updated)
2064 /*  JEITA Parameter settings
2065 *
2066 *          VCHG  
2067 *            |     
2068 * jt_vfchg_h~+~~~~~~~~~~~~~~~~~~~+
2069 *            |                   |
2070 * jt_vfchg_l-| - - - - - - - - - +~~~~~~~~~~+
2071 *            |    Charge area    +          |               
2072 *  -------0--+-------------------+----------+--- Temp
2073 *            !                   +
2074 *          ICHG     
2075 *            |                   +
2076 *  jt_ichg_h-+ - -+~~~~~~~~~~~~~~+~~~~~~~~~~+
2077 *            +    |              +          |
2078 *  jt_ichg_l-+~~~~+   Charge area           |
2079 *            |    +              +          |
2080 *         0--+----+--------------+----------+--- Temp
2081 *            0   jt_temp_l      jt_temp_h   55
2082 */
2083 {
2084         int temp;
2085         int err = 0;
2086         int vfchg;
2087         uint8_t chgiset_org;
2088         uint8_t batset2_org;
2089         uint8_t set_vchg_h, set_vchg_l;
2090         uint8_t set_ichg_h, set_ichg_l;
2091
2092         *is_jeita_updated = false;
2093         
2094         /* No execute if JEITA disabled */
2095         if (!info->jt_en || info->jt_hw_sw)
2096                 return 0;
2097
2098         /* Check FG Reset */
2099         if (info->soca->ready_fg) {
2100                 temp = get_battery_temp_2(info) / 10;
2101         } else {
2102                 RICOH_FG_DBG(KERN_INFO "JEITA: %s *** cannot update by resetting FG ******\n", __func__);
2103                 goto out;
2104         }
2105
2106         /* Read BATSET2 */
2107         err = ricoh619_read(info->dev->parent, BATSET2_REG, &batset2_org);
2108         if (err < 0) {
2109                 dev_err(info->dev, "Error in readng the battery setting register\n");
2110                 goto out;
2111         }
2112         vfchg = (batset2_org & 0x70) >> 4;
2113         batset2_org &= 0x8F;
2114         
2115         /* Read CHGISET */
2116         err = ricoh619_read(info->dev->parent, CHGISET_REG, &chgiset_org);
2117         if (err < 0) {
2118                 dev_err(info->dev, "Error in readng the chrage setting register\n");
2119                 goto out;
2120         }
2121         chgiset_org &= 0xC0;
2122
2123         set_ichg_h = (uint8_t)(chgiset_org | info->jt_ichg_h);
2124         set_ichg_l = (uint8_t)(chgiset_org | info->jt_ichg_l);
2125                 
2126         set_vchg_h = (uint8_t)((info->jt_vfchg_h << 4) | batset2_org);
2127         set_vchg_l = (uint8_t)((info->jt_vfchg_l << 4) | batset2_org);
2128
2129         RICOH_FG_DBG(KERN_INFO "PMU: %s *** Temperature: %d, vfchg: %d, SW status: %d, chg_status: %d ******\n",
2130                  __func__, temp, vfchg, info->soca->status, info->soca->chg_status);
2131
2132         if (temp <= 0 || 55 <= temp) {
2133                 /* 1st and 5th temperature ranges (~0, 55~) */
2134                 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Temp(%d) is out of 0-55 ******\n", __func__, temp);
2135                 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2136                 if (err < 0) {
2137                         dev_err(info->dev, "Error in writing the control register\n");
2138                         goto out;
2139                 }
2140                 info->soca->jt_limit = 0;
2141                 *is_jeita_updated = true;
2142         } else if (temp < info->jt_temp_l) {
2143                 /* 2nd temperature range (0~12) */
2144                 if (vfchg != info->jt_vfchg_h) {
2145                         RICOH_FG_DBG(KERN_INFO "PMU: %s *** 0<Temp<12, update to vfchg=%d ******\n", 
2146                                                                         __func__, info->jt_vfchg_h);
2147                         err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2148                         if (err < 0) {
2149                                 dev_err(info->dev, "Error in writing the control register\n");
2150                                 goto out;
2151                         }
2152
2153                         /* set VFCHG/VRCHG */
2154                         err = ricoh619_write(info->dev->parent,
2155                                                          BATSET2_REG, set_vchg_h);
2156                         if (err < 0) {
2157                                 dev_err(info->dev, "Error in writing the battery setting register\n");
2158                                 goto out;
2159                         }
2160                         info->soca->jt_limit = 0;
2161                         *is_jeita_updated = true;
2162                 } else
2163                         RICOH_FG_DBG(KERN_INFO "PMU: %s *** 0<Temp<50, already set vfchg=%d, so no need to update ******\n",
2164                                         __func__, info->jt_vfchg_h);
2165
2166                 /* set ICHG */
2167                 err = ricoh619_write(info->dev->parent, CHGISET_REG, set_ichg_l);
2168                 if (err < 0) {
2169                         dev_err(info->dev, "Error in writing the battery setting register\n");
2170                         goto out;
2171                 }
2172                 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2173                 if (err < 0) {
2174                         dev_err(info->dev, "Error in writing the control register\n");
2175                         goto out;
2176                 }
2177         } else if (temp < info->jt_temp_h) {
2178                 /* 3rd temperature range (12~50) */
2179                 if (vfchg != info->jt_vfchg_h) {
2180                         RICOH_FG_DBG(KERN_INFO "PMU: %s *** 12<Temp<50, update to vfchg==%d ******\n", __func__, info->jt_vfchg_h);
2181
2182                         err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2183                         if (err < 0) {
2184                                 dev_err(info->dev, "Error in writing the control register\n");
2185                                 goto out;
2186                         }
2187                         /* set VFCHG/VRCHG */
2188                         err = ricoh619_write(info->dev->parent,
2189                                                          BATSET2_REG, set_vchg_h);
2190                         if (err < 0) {
2191                                 dev_err(info->dev, "Error in writing the battery setting register\n");
2192                                 goto out;
2193                         }
2194                         info->soca->jt_limit = 0;
2195                         *is_jeita_updated = true;
2196                 } else
2197                         RICOH_FG_DBG(KERN_INFO "PMU: %s *** 12<Temp<50, already set vfchg==%d, so no need to update ******\n", 
2198                                         __func__, info->jt_vfchg_h);
2199                 
2200                 /* set ICHG */
2201                 err = ricoh619_write(info->dev->parent, CHGISET_REG, set_ichg_h);
2202                 if (err < 0) {
2203                         dev_err(info->dev, "Error in writing the battery setting register\n");
2204                         goto out;
2205                 }
2206                 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2207                 if (err < 0) {
2208                         dev_err(info->dev, "Error in writing the control register\n");
2209                         goto out;
2210                 }
2211         } else if (temp < 55) {
2212                 /* 4th temperature range (50~55) */
2213                 if (vfchg != info->jt_vfchg_l) {
2214                         RICOH_FG_DBG(KERN_INFO "PMU: %s *** 50<Temp<55, update to vfchg==%d ******\n", __func__, info->jt_vfchg_l);
2215                         
2216                         err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2217                         if (err < 0) {
2218                                 dev_err(info->dev, "Error in writing the control register\n");
2219                                 goto out;
2220                         }
2221                         /* set VFCHG/VRCHG */
2222                         err = ricoh619_write(info->dev->parent,
2223                                                          BATSET2_REG, set_vchg_l);
2224                         if (err < 0) {
2225                                 dev_err(info->dev, "Error in writing the battery setting register\n");
2226                                 goto out;
2227                         }
2228                         info->soca->jt_limit = 1;
2229                         *is_jeita_updated = true;
2230                 } else
2231                         RICOH_FG_DBG(KERN_INFO "JEITA: %s *** 50<Temp<55, already set vfchg==%d, so no need to update ******\n", 
2232                                         __func__, info->jt_vfchg_l);
2233
2234                 /* set ICHG */
2235                 err = ricoh619_write(info->dev->parent, CHGISET_REG, set_ichg_h);
2236                 if (err < 0) {
2237                         dev_err(info->dev, "Error in writing the battery setting register\n");
2238                         goto out;
2239                 }
2240                 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2241                 if (err < 0) {
2242                         dev_err(info->dev, "Error in writing the control register\n");
2243                         goto out;
2244                 }
2245         }
2246
2247         get_power_supply_status(info);
2248         RICOH_FG_DBG(KERN_INFO "PMU: %s *** Hope updating value in this timing after checking jeita, chg_status: %d, is_jeita_updated: %d ******\n",
2249                  __func__, info->soca->chg_status, *is_jeita_updated);
2250
2251         return 0;
2252         
2253 out:
2254         RICOH_FG_DBG(KERN_INFO "PMU: %s ERROR ******\n", __func__);
2255         return err;
2256 }
2257
2258 static void ricoh619_jeita_work(struct work_struct *work)
2259 {
2260         int ret;
2261         bool is_jeita_updated = false;
2262         struct ricoh619_battery_info *info = container_of(work,
2263                 struct ricoh619_battery_info, jeita_work.work);
2264
2265         mutex_lock(&info->lock);
2266
2267         ret = check_jeita_status(info, &is_jeita_updated);
2268         if (0 == ret) {
2269                 queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
2270                                          RICOH619_JEITA_UPDATE_TIME * HZ);
2271         } else {
2272                 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Call check_jeita_status() in jeita_work, err:%d ******\n", 
2273                                                         __func__, ret);
2274                 queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
2275                                          RICOH619_FG_RESET_TIME * HZ);
2276         }
2277
2278         mutex_unlock(&info->lock);
2279
2280         if(true == is_jeita_updated)
2281                 power_supply_changed(&info->battery);
2282
2283         return;
2284 }
2285
2286 #ifdef ENABLE_FACTORY_MODE
2287 /*------------------------------------------------------*/
2288 /* Factory Mode                                         */
2289 /*    Check Battery exist or not                        */
2290 /*    If not, disabled Rapid to Complete State change   */
2291 /*------------------------------------------------------*/
2292 static int ricoh619_factory_mode(struct ricoh619_battery_info *info)
2293 {
2294         int ret = 0;
2295         uint8_t val = 0;
2296
2297         ret = ricoh619_read(info->dev->parent, RICOH619_INT_MON_CHGCTR, &val);
2298         if (ret < 0) {
2299                 dev_err(info->dev, "Error in reading the control register\n");
2300                 return ret;
2301         }
2302         if (!(val & 0x01)) /* No Adapter connected */
2303                 return ret;
2304
2305         /* Rapid to Complete State change disable */
2306         ret = ricoh619_set_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x40);
2307         if (ret < 0) {
2308                 dev_err(info->dev, "Error in writing the control register\n");
2309                 return ret;
2310         }
2311
2312         /* Wait 1s for checking Charging State */
2313         queue_delayed_work(info->factory_mode_wqueue, &info->factory_mode_work,
2314                          1*HZ);
2315
2316         return ret;
2317 }
2318
2319 static void check_charging_state_work(struct work_struct *work)
2320 {
2321         struct ricoh619_battery_info *info = container_of(work,
2322                 struct ricoh619_battery_info, factory_mode_work.work);
2323
2324         int ret = 0;
2325         uint8_t val = 0;
2326         int chargeCurrent = 0;
2327
2328         ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &val);
2329         if (ret < 0) {
2330                 dev_err(info->dev, "Error in reading the control register\n");
2331                 return;
2332         }
2333
2334
2335         chargeCurrent = get_check_fuel_gauge_reg(info, CC_AVERAGE1_REG,
2336                                                  CC_AVERAGE0_REG, 0x3fff);
2337         if (chargeCurrent < 0) {
2338                 dev_err(info->dev, "Error in reading the FG register\n");
2339                 return;
2340         }
2341
2342         /* Repid State && Charge Current about 0mA */
2343         if (((chargeCurrent >= 0x3ffc && chargeCurrent <= 0x3fff)
2344                 || chargeCurrent < 0x05) && val == 0x43) {
2345                 RICOH_FG_DBG("PMU:%s --- No battery !! Enter Factory mode ---\n"
2346                                 , __func__);
2347                 info->entry_factory_mode = true;
2348                 /* clear FG_ACC bit */
2349                 ret = ricoh619_clr_bits(info->dev->parent, RICOH619_FG_CTRL, 0x10);
2350                 if (ret < 0)
2351                         dev_err(info->dev, "Error in writing FG_CTRL\n");
2352                 
2353                 return; /* Factory Mode */
2354         }
2355
2356         /* Return Normal Mode --> Rapid to Complete State change enable */
2357         ret = ricoh619_clr_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x40);
2358         if (ret < 0) {
2359                 dev_err(info->dev, "Error in writing the control register\n");
2360                 return;
2361         }
2362         RICOH_FG_DBG("PMU:%s --- Battery exist !! Return Normal mode ---0x%2x\n"
2363                         , __func__, val);
2364
2365         return;
2366 }
2367 #endif /* ENABLE_FACTORY_MODE */
2368
2369 static int Calc_Linear_Interpolation(int x0, int y0, int x1, int y1, int y)
2370 {
2371         int     alpha;
2372         int x;
2373
2374         alpha = (y - y0)*100 / (y1 - y0);
2375
2376         x = ((100 - alpha) * x0 + alpha * x1) / 100;
2377
2378         return x;
2379 }
2380
2381 static void ricoh619_scaling_OCV_table(struct ricoh619_battery_info *info, int cutoff_vol, int full_vol, int *start_per, int *end_per)
2382 {
2383         int             i, j;
2384         int             temp;
2385         int             percent_step;
2386         int             OCV_percent_new[11];
2387
2388         /* get ocv table. this table is calculated by Apprication */
2389         //RICOH_FG_DBG("PMU : %s : original table\n",__func__);
2390         for (i = 0; i <= 10; i = i+1) {
2391                 RICOH_FG_DBG(KERN_INFO "PMU: %s : %d0%% voltage = %d uV\n",
2392                                  __func__, i, info->soca->ocv_table_def[i]);
2393         }
2394         //RICOH_FG_DBG("PMU: %s : cutoff_vol %d full_vol %d\n",
2395         //                       __func__, cutoff_vol,full_vol);
2396
2397         /* Check Start % */
2398         if (info->soca->ocv_table_def[0] > cutoff_vol * 1000) {
2399                 *start_per = 0;
2400                 RICOH_FG_DBG("PMU : %s : setting value of cuttoff_vol(%d) is out of range(%d) \n",__func__, cutoff_vol, info->soca->ocv_table_def[0]);
2401         } else {
2402                 for (i = 1; i < 11; i++) {
2403                         if (info->soca->ocv_table_def[i] >= cutoff_vol * 1000) {
2404                                 /* unit is 0.001% */
2405                                 *start_per = Calc_Linear_Interpolation(
2406                                         (i-1)*1000, info->soca->ocv_table_def[i-1], i*1000,
2407                                         info->soca->ocv_table_def[i], (cutoff_vol * 1000));
2408                                 break;
2409                         }
2410                 }
2411         }
2412
2413         /* Check End % */
2414         for (i = 1; i < 11; i++) {
2415                 if (info->soca->ocv_table_def[i] >= full_vol * 1000) {
2416                         /* unit is 0.001% */
2417                         *end_per = Calc_Linear_Interpolation(
2418                                 (i-1)*1000, info->soca->ocv_table_def[i-1], i*1000,
2419                                  info->soca->ocv_table_def[i], (full_vol * 1000));
2420                         break;
2421                 }
2422         }
2423
2424         /* calc new ocv percent */
2425         percent_step = ( *end_per - *start_per) / 10;
2426         //RICOH_FG_DBG("PMU : %s : percent_step is %d end per is %d start per is %d\n",__func__, percent_step, *end_per, *start_per);
2427
2428         for (i = 0; i < 11; i++) {
2429                 OCV_percent_new[i]
2430                          = *start_per + percent_step*(i - 0);
2431         }
2432
2433         /* calc new ocv voltage */
2434         for (i = 0; i < 11; i++) {
2435                 for (j = 1; j < 11; j++) {
2436                         if (1000*j >= OCV_percent_new[i]) {
2437                                 temp = Calc_Linear_Interpolation(
2438                                         info->soca->ocv_table_def[j-1], (j-1)*1000,
2439                                         info->soca->ocv_table_def[j] , j*1000,
2440                                          OCV_percent_new[i]);
2441
2442                                 temp = ( (temp/1000) * 4095 ) / 5000;
2443
2444                                 battery_init_para[info->num][i*2 + 1] = temp;
2445                                 battery_init_para[info->num][i*2] = temp >> 8;
2446
2447                                 break;
2448                         }
2449                 }
2450         }
2451         RICOH_FG_DBG("PMU : %s : new table\n",__func__);
2452         for (i = 0; i <= 10; i = i+1) {
2453                 temp = (battery_init_para[info->num][i*2]<<8)
2454                          | (battery_init_para[info->num][i*2+1]);
2455                 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
2456                 temp = ((temp * 50000 * 10 / 4095) + 5) / 10;
2457                 RICOH_FG_DBG("PMU : %s : ocv_table %d is %d v\n",__func__, i, temp);
2458         }
2459
2460 }
2461
2462 static int ricoh619_set_OCV_table(struct ricoh619_battery_info *info)
2463 {
2464         int             ret = 0;
2465         int             i;
2466         int             full_ocv;
2467         int             available_cap;
2468         int             available_cap_ori;
2469         int             temp;
2470         int             temp1;
2471         int             start_per = 0;
2472         int             end_per = 0;
2473         int             Rbat;
2474         int             Ibat_min;
2475         uint8_t val;
2476         uint8_t val2;
2477         uint8_t val_temp;
2478
2479
2480         //get ocv table 
2481         for (i = 0; i <= 10; i = i+1) {
2482                 info->soca->ocv_table_def[i] = get_OCV_voltage(info, i);
2483                 RICOH_FG_DBG(KERN_INFO "PMU: %s : %d0%% voltage = %d uV\n",
2484                          __func__, i, info->soca->ocv_table_def[i]);
2485         }
2486
2487         temp =  (battery_init_para[info->num][24]<<8) | (battery_init_para[info->num][25]);
2488         Rbat = temp * 1000 / 512 * 5000 / 4095;
2489         info->soca->Rsys = Rbat + 55;
2490
2491         if ((info->fg_target_ibat == 0) || (info->fg_target_vsys == 0)) {       /* normal version */
2492
2493                 temp =  (battery_init_para[info->num][22]<<8) | (battery_init_para[info->num][23]);
2494                 //fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
2495                 //                              0x7fff);
2496
2497                 info->soca->target_ibat = temp*2/10; /* calc 0.2C*/
2498                 temp1 =  (battery_init_para[info->num][0]<<8) | (battery_init_para[info->num][1]);
2499
2500                 info->soca->target_vsys = temp1 + ( info->soca->target_ibat * info->soca->Rsys ) / 1000;
2501                 
2502
2503         } else {
2504                 info->soca->target_ibat = info->fg_target_ibat;
2505                 /* calc min vsys value */
2506                 temp1 =  (battery_init_para[info->num][0]<<8) | (battery_init_para[info->num][1]);
2507                 temp = temp1 + ( info->soca->target_ibat * info->soca->Rsys ) / 1000;
2508                 if( temp < info->fg_target_vsys) {
2509                         info->soca->target_vsys = info->fg_target_vsys;
2510                 } else {
2511                         info->soca->target_vsys = temp;
2512                         RICOH_FG_DBG("PMU : %s : setting value of target vsys(%d) is out of range(%d)\n",__func__, info->fg_target_vsys, temp);
2513                 }
2514         }
2515
2516         //for debug
2517         RICOH_FG_DBG("PMU : %s : target_vsys is %d target_ibat is %d\n",__func__,info->soca->target_vsys,info->soca->target_ibat);
2518         
2519         if ((info->soca->target_ibat == 0) || (info->soca->target_vsys == 0)) { /* normal version */
2520         } else {        /*Slice cutoff voltage version. */
2521
2522                 Ibat_min = -1 * info->soca->target_ibat;
2523                 info->soca->cutoff_ocv = info->soca->target_vsys - Ibat_min * info->soca->Rsys / 1000;
2524                 
2525                 full_ocv = (battery_init_para[info->num][20]<<8) | (battery_init_para[info->num][21]);
2526                 full_ocv = full_ocv * 5000 / 4095;
2527
2528                 ricoh619_scaling_OCV_table(info, info->soca->cutoff_ocv, full_ocv, &start_per, &end_per);
2529
2530                 /* calc available capacity */
2531                 /* get avilable capacity */
2532                 /* battery_init_para23-24 is designe capacity */
2533                 available_cap = (battery_init_para[info->num][22]<<8)
2534                                          | (battery_init_para[info->num][23]);
2535
2536                 available_cap = available_cap
2537                          * ((10000 - start_per) / 100) / 100 ;
2538
2539
2540                 battery_init_para[info->num][23] =  available_cap;
2541                 battery_init_para[info->num][22] =  available_cap >> 8;
2542
2543         }
2544         ret = ricoh619_clr_bits(info->dev->parent, FG_CTRL_REG, 0x01);
2545         if (ret < 0) {
2546                 dev_err(info->dev, "error in FG_En off\n");
2547                 goto err;
2548         }
2549         /////////////////////////////////
2550         ret = ricoh619_read_bank1(info->dev->parent, 0xDC, &val);
2551         if (ret < 0) {
2552                 dev_err(info->dev, "batterry initialize error\n");
2553                 goto err;
2554         }
2555
2556         val_temp = val;
2557         val     &= 0x0F; //clear bit 4-7
2558         val     |= 0x10; //set bit 4
2559         
2560         ret = ricoh619_write_bank1(info->dev->parent, 0xDC, val);
2561         if (ret < 0) {
2562                 dev_err(info->dev, "batterry initialize error\n");
2563                 goto err;
2564         }
2565         
2566         ret = ricoh619_read_bank1(info->dev->parent, 0xDC, &val2);
2567         if (ret < 0) {
2568                 dev_err(info->dev, "batterry initialize error\n");
2569                 goto err;
2570         }
2571
2572         ret = ricoh619_write_bank1(info->dev->parent, 0xDC, val_temp);
2573         if (ret < 0) {
2574                 dev_err(info->dev, "batterry initialize error\n");
2575                 goto err;
2576         }
2577
2578         RICOH_FG_DBG("PMU : %s : original 0x%x, before 0x%x, after 0x%x\n",__func__, val_temp, val, val2);
2579         
2580         if (val != val2) {
2581                 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
2582                                 BAT_INIT_TOP_REG, 30, battery_init_para[info->num]);
2583                 if (ret < 0) {
2584                         dev_err(info->dev, "batterry initialize error\n");
2585                         goto err;
2586                 }
2587         } else {
2588                 ret = ricoh619_read_bank1(info->dev->parent, 0xD2, &val);
2589                 if (ret < 0) {
2590                 dev_err(info->dev, "batterry initialize error\n");
2591                 goto err;
2592                 }
2593         
2594                 ret = ricoh619_read_bank1(info->dev->parent, 0xD3, &val2);
2595                 if (ret < 0) {
2596                         dev_err(info->dev, "batterry initialize error\n");
2597                         goto err;
2598                 }
2599                 
2600                 available_cap_ori = val2 + (val << 8);
2601                 available_cap = battery_init_para[info->num][23]
2602                                                 + (battery_init_para[info->num][22] << 8);
2603
2604                 if (available_cap_ori == available_cap) {
2605                         ret = ricoh619_bulk_writes_bank1(info->dev->parent,
2606                                 BAT_INIT_TOP_REG, 22, battery_init_para[info->num]);
2607                         if (ret < 0) {
2608                                 dev_err(info->dev, "batterry initialize error\n");
2609                                 return ret;
2610                         }
2611                         
2612                         for (i = 0; i < 6; i++) {
2613                                 ret = ricoh619_write_bank1(info->dev->parent, 0xD4+i, battery_init_para[info->num][24+i]);
2614                                 if (ret < 0) {
2615                                         dev_err(info->dev, "batterry initialize error\n");
2616                                         return ret;
2617                                 }
2618                         }
2619                 } else {
2620                         ret = ricoh619_bulk_writes_bank1(info->dev->parent,
2621                                 BAT_INIT_TOP_REG, 30, battery_init_para[info->num]);
2622                         if (ret < 0) {
2623                                 dev_err(info->dev, "batterry initialize error\n");
2624                                 goto err;
2625                         }
2626                 }
2627         }
2628
2629         ////////////////////////////////
2630
2631         return 0;
2632 err:
2633         return ret;
2634 }
2635
2636 /* Initial setting of battery */
2637 static int ricoh619_init_battery(struct ricoh619_battery_info *info)
2638 {
2639         int ret = 0;
2640         uint8_t val;
2641         uint8_t val2;
2642         /* Need to implement initial setting of batery and error */
2643         /* -------------------------- */
2644 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
2645
2646         /* set relaxation state */
2647         if (RICOH619_REL1_SEL_VALUE > 240)
2648                 val = 0x0F;
2649         else
2650                 val = RICOH619_REL1_SEL_VALUE / 16 ;
2651
2652         /* set relaxation state */
2653         if (RICOH619_REL2_SEL_VALUE > 120)
2654                 val2 = 0x0F;
2655         else
2656                 val2 = RICOH619_REL2_SEL_VALUE / 8 ;
2657
2658         val =  val + (val2 << 4);
2659
2660         ret = ricoh619_write_bank1(info->dev->parent, BAT_REL_SEL_REG, val);
2661         if (ret < 0) {
2662                 dev_err(info->dev, "Error in writing BAT_REL_SEL_REG\n");
2663                 return ret;
2664         }
2665
2666         ret = ricoh619_read_bank1(info->dev->parent, BAT_REL_SEL_REG, &val);
2667         RICOH_FG_DBG("PMU: -------  BAT_REL_SEL= %xh: =======\n",
2668                 val);
2669
2670         ret = ricoh619_write_bank1(info->dev->parent, BAT_TA_SEL_REG, 0x00);
2671         if (ret < 0) {
2672                 dev_err(info->dev, "Error in writing BAT_TA_SEL_REG\n");
2673                 return ret;
2674         }
2675
2676 //      ret = ricoh619_read(info->dev->parent, FG_CTRL_REG, &val);
2677 //      if (ret < 0) {
2678 //              dev_err(info->dev, "Error in reading the control register\n");
2679 //              return ret;
2680 //      }
2681
2682 //      val = (val & 0x10) >> 4;
2683 //      info->first_pwon = (val == 0) ? 1 : 0;
2684         ret = ricoh619_read(info->dev->parent, PSWR_REG, &val);
2685         if (ret < 0) {
2686                 dev_err(info->dev,"Error in reading PSWR_REG %d\n", ret);
2687                 return ret;
2688         }
2689         info->first_pwon = (val == 0) ? 1 : 0;
2690         g_soc = val & 0x7f;
2691         
2692         ret = ricoh619_set_OCV_table(info);
2693         if (ret < 0) {
2694                 dev_err(info->dev, "Error in writing the OCV Tabler\n");
2695                 return ret;
2696         }
2697
2698         ret = ricoh619_write(info->dev->parent, FG_CTRL_REG, 0x11);
2699         if (ret < 0) {
2700                 dev_err(info->dev, "Error in writing the control register\n");
2701                 return ret;
2702         }
2703
2704 #endif
2705
2706         ret = ricoh619_write(info->dev->parent, VINDAC_REG, 0x03);
2707         if (ret < 0) {
2708                 dev_err(info->dev, "Error in writing the control register\n");
2709                 return ret;
2710         }
2711
2712         if (info->alarm_vol_mv < 2700 || info->alarm_vol_mv > 3400) {
2713                 dev_err(info->dev, "alarm_vol_mv is out of range!\n");
2714                 return -1;
2715         }
2716
2717         return ret;
2718 }
2719
2720 /* Initial setting of charger */
2721 static int ricoh619_init_charger(struct ricoh619_battery_info *info)
2722 {
2723         int err;
2724         uint8_t val;
2725         uint8_t val2;
2726         uint8_t val3;
2727         int charge_status;
2728         int     vfchg_val;
2729         int     icchg_val;
2730         int     rbat;
2731         int     temp;
2732
2733         info->chg_ctr = 0;
2734         info->chg_stat1 = 0;
2735
2736         err = ricoh619_set_bits(info->dev->parent, RICOH619_PWR_FUNC, 0x20);
2737         if (err < 0) {
2738                 dev_err(info->dev, "Error in writing the PWR FUNC register\n");
2739                 goto free_device;
2740         }
2741
2742         charge_status = get_power_supply_status(info);
2743
2744         if (charge_status != POWER_SUPPLY_STATUS_FULL)
2745         {
2746                 /* Disable charging */
2747                 err = ricoh619_clr_bits(info->dev->parent,CHGCTL1_REG, 0x03);
2748                 if (err < 0) {
2749                         dev_err(info->dev, "Error in writing the control register\n");
2750                         goto free_device;
2751                 }
2752         }
2753
2754         //debug messeage
2755         err = ricoh619_read(info->dev->parent, REGISET1_REG,&val);
2756         RICOH_FG_DBG("PMU : %s : before REGISET1_REG (0x%x) is 0x%x info->ch_ilim_adp is 0x%x\n",__func__,REGISET1_REG,val,info->ch_ilim_adp);
2757
2758         /* REGISET1:(0xB6) setting */
2759         if ((info->ch_ilim_adp != 0xFF) || (info->ch_ilim_adp <= 0x1D)) {
2760                 val = info->ch_ilim_adp;
2761
2762                 err = ricoh619_write(info->dev->parent, REGISET1_REG,val);
2763                 if (err < 0) {
2764                         dev_err(info->dev, "Error in writing REGISET1_REG %d\n",
2765                                                                                  err);
2766                         goto free_device;
2767                 }
2768         }
2769
2770         //debug messeage
2771         err = ricoh619_read(info->dev->parent, REGISET1_REG,&val);
2772         RICOH_FG_DBG("PMU : %s : after REGISET1_REG (0x%x) is 0x%x info->ch_ilim_adp is 0x%x\n",__func__,REGISET1_REG,val,info->ch_ilim_adp);
2773         
2774                 //debug messeage
2775         err = ricoh619_read(info->dev->parent, REGISET2_REG,&val);
2776         RICOH_FG_DBG("PMU : %s : before REGISET2_REG (0x%x) is 0x%x info->ch_ilim_usb is 0x%x\n",__func__,REGISET2_REG,val,info->ch_ilim_usb);
2777
2778         /* REGISET2:(0xB7) setting */
2779         err = ricoh619_read(info->dev->parent, REGISET2_REG, &val);
2780         if (err < 0) {
2781                 dev_err(info->dev,
2782                 "Error in read REGISET2_REG %d\n", err);
2783                 goto free_device;
2784         }
2785         
2786         if ((info->ch_ilim_usb != 0xFF) || (info->ch_ilim_usb <= 0x1D)) {
2787                 val2 = info->ch_ilim_usb;
2788         } else {/* Keep OTP value */
2789                 val2 = (val & 0x1F);
2790         }
2791
2792                 /* keep bit 5-7 */
2793         val &= 0xE0;
2794         
2795         val = val + val2;
2796         
2797         err = ricoh619_write(info->dev->parent, REGISET2_REG,val);
2798         if (err < 0) {
2799                 dev_err(info->dev, "Error in writing REGISET2_REG %d\n",
2800                                                                          err);
2801                 goto free_device;
2802         }
2803
2804                 //debug messeage
2805         err = ricoh619_read(info->dev->parent, REGISET2_REG,&val);
2806         RICOH_FG_DBG("PMU : %s : after REGISET2_REG (0x%x) is 0x%x info->ch_ilim_usb is 0x%x\n",__func__,REGISET2_REG,val,info->ch_ilim_usb);
2807
2808         /* CHGISET_REG(0xB8) setting */
2809                 //debug messeage
2810         err = ricoh619_read(info->dev->parent, CHGISET_REG,&val);
2811         RICOH_FG_DBG("PMU : %s : before CHGISET_REG (0x%x) is 0x%x info->ch_ichg is 0x%x info->ch_icchg is 0x%x\n",__func__,CHGISET_REG,val,info->ch_ichg,info->ch_icchg);
2812
2813         err = ricoh619_read(info->dev->parent, CHGISET_REG, &val);
2814         if (err < 0) {
2815                 dev_err(info->dev,
2816                 "Error in read CHGISET_REG %d\n", err);
2817                 goto free_device;
2818         }
2819
2820                 /* Define Current settings value for charging (bit 4~0)*/
2821         if ((info->ch_ichg != 0xFF) || (info->ch_ichg <= 0x1D)) {
2822                 val2 = info->ch_ichg;
2823         } else { /* Keep OTP value */
2824                 val2 = (val & 0x1F);
2825         }
2826
2827                 /* Define Current settings at the charge completion (bit 7~6)*/
2828         if ((info->ch_icchg != 0xFF) || (info->ch_icchg <= 0x03)) {
2829                 val3 = info->ch_icchg << 6;
2830         } else { /* Keep OTP value */
2831                 val3 = (val & 0xC0);
2832         }
2833
2834         val = val2 + val3;
2835
2836         err = ricoh619_write(info->dev->parent, CHGISET_REG, val);
2837         if (err < 0) {
2838                 dev_err(info->dev, "Error in writing CHGISET_REG %d\n",
2839                                                                          err);
2840                 goto free_device;
2841         }
2842
2843                 //debug messeage
2844         err = ricoh619_read(info->dev->parent, CHGISET_REG,&val);
2845         RICOH_FG_DBG("PMU : %s : after CHGISET_REG (0x%x) is 0x%x info->ch_ichg is 0x%x info->ch_icchg is 0x%x\n",__func__,CHGISET_REG,val,info->ch_ichg,info->ch_icchg);
2846
2847                 //debug messeage
2848         err = ricoh619_read(info->dev->parent, BATSET1_REG,&val);
2849         RICOH_FG_DBG("PMU : %s : before BATSET1_REG (0x%x) is 0x%x info->ch_vbatovset is 0x%x\n",__func__,BATSET1_REG,val,info->ch_vbatovset);
2850         
2851         /* BATSET1_REG(0xBA) setting */
2852         err = ricoh619_read(info->dev->parent, BATSET1_REG, &val);
2853         if (err < 0) {
2854                 dev_err(info->dev,
2855                 "Error in read BATSET1 register %d\n", err);
2856                 goto free_device;
2857         }
2858
2859                 /* Define Battery overvoltage  (bit 4)*/
2860         if ((info->ch_vbatovset != 0xFF) || (info->ch_vbatovset <= 0x1)) {
2861                 val2 = info->ch_vbatovset;
2862                 val2 = val2 << 4;
2863         } else { /* Keep OTP value */
2864                 val2 = (val & 0x10);
2865         }
2866         
2867                 /* keep bit 0-3 and bit 5-7 */
2868         val = (val & 0xEF);
2869         
2870         val = val + val2;
2871
2872         err = ricoh619_write(info->dev->parent, BATSET1_REG, val);
2873         if (err < 0) {
2874                 dev_err(info->dev, "Error in writing BAT1_REG %d\n",
2875                                                                          err);
2876                 goto free_device;
2877         }
2878                 //debug messeage
2879         err = ricoh619_read(info->dev->parent, BATSET1_REG,&val);
2880         RICOH_FG_DBG("PMU : %s : after BATSET1_REG (0x%x) is 0x%x info->ch_vbatovset is 0x%x\n",__func__,BATSET1_REG,val,info->ch_vbatovset);
2881         
2882                 //debug messeage
2883         err = ricoh619_read(info->dev->parent, BATSET2_REG,&val);
2884         RICOH_FG_DBG("PMU : %s : before BATSET2_REG (0x%x) is 0x%x info->ch_vrchg is 0x%x info->ch_vfchg is 0x%x \n",__func__,BATSET2_REG,val,info->ch_vrchg,info->ch_vfchg);
2885
2886         
2887         /* BATSET2_REG(0xBB) setting */
2888         err = ricoh619_read(info->dev->parent, BATSET2_REG, &val);
2889         if (err < 0) {
2890                 dev_err(info->dev,
2891                 "Error in read BATSET2 register %d\n", err);
2892                 goto free_device;
2893         }
2894
2895                 /* Define Re-charging voltage (bit 2~0)*/
2896         if ((info->ch_vrchg != 0xFF) || (info->ch_vrchg <= 0x04)) {
2897                 val2 = info->ch_vrchg;
2898         } else { /* Keep OTP value */
2899                 val2 = (val & 0x07);
2900         }
2901
2902                 /* Define FULL charging voltage (bit 6~4)*/
2903         if ((info->ch_vfchg != 0xFF) || (info->ch_vfchg <= 0x04)) {
2904                 val3 = info->ch_vfchg;
2905                 val3 = val3 << 4;
2906         } else {        /* Keep OTP value */
2907                 val3 = (val & 0x70);
2908         }
2909
2910                 /* keep bit 3 and bit 7 */
2911         val = (val & 0x88);
2912         
2913         val = val + val2 + val3;
2914
2915         err = ricoh619_write(info->dev->parent, BATSET2_REG, val);
2916         if (err < 0) {
2917                 dev_err(info->dev, "Error in writing RICOH619_RE_CHARGE_VOLTAGE %d\n",
2918                                                                          err);
2919                 goto free_device;
2920         }
2921
2922                 //debug messeage
2923         err = ricoh619_read(info->dev->parent, BATSET2_REG,&val);
2924         RICOH_FG_DBG("PMU : %s : after BATSET2_REG (0x%x) is 0x%x info->ch_vrchg is 0x%x info->ch_vfchg is 0x%x  \n",__func__,BATSET2_REG,val,info->ch_vrchg,info->ch_vfchg);
2925
2926         /* Set rising edge setting ([1:0]=01b)for INT in charging */
2927         /*  and rising edge setting ([3:2]=01b)for charge completion */
2928         err = ricoh619_read(info->dev->parent, RICOH619_CHG_STAT_DETMOD1, &val);
2929         if (err < 0) {
2930                 dev_err(info->dev, "Error in reading CHG_STAT_DETMOD1 %d\n",
2931                                                                  err);
2932                 goto free_device;
2933         }
2934         val &= 0xf0;
2935         val |= 0x05;
2936         err = ricoh619_write(info->dev->parent, RICOH619_CHG_STAT_DETMOD1, val);
2937         if (err < 0) {
2938                 dev_err(info->dev, "Error in writing CHG_STAT_DETMOD1 %d\n",
2939                                                                  err);
2940                 goto free_device;
2941         }
2942
2943         /* Unmask In charging/charge completion */
2944         err = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGSTS1, 0xfc);
2945         if (err < 0) {
2946                 dev_err(info->dev, "Error in writing INT_MSK_CHGSTS1 %d\n",
2947                                                                  err);
2948                 goto free_device;
2949         }
2950
2951         /* Set both edge for VUSB([3:2]=11b)/VADP([1:0]=11b) detect */
2952         err = ricoh619_read(info->dev->parent, RICOH619_CHG_CTRL_DETMOD1, &val);
2953         if (err < 0) {
2954                 dev_err(info->dev, "Error in reading CHG_CTRL_DETMOD1 %d\n",
2955                                                                  err);
2956                 goto free_device;
2957         }
2958         val &= 0xf0;
2959         val |= 0x0f;
2960         err = ricoh619_write(info->dev->parent, RICOH619_CHG_CTRL_DETMOD1, val);
2961         if (err < 0) {
2962                 dev_err(info->dev, "Error in writing CHG_CTRL_DETMOD1 %d\n",
2963                                                                  err);
2964                 goto free_device;
2965         }
2966
2967         /* Unmask In VUSB/VADP completion */
2968         err = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGCTR, 0xfc);
2969         if (err < 0) {
2970                 dev_err(info->dev, "Error in writing INT_MSK_CHGSTS1 %d\n",
2971                                                                          err);
2972                 goto free_device;
2973         }
2974         
2975         if (charge_status != POWER_SUPPLY_STATUS_FULL)
2976         {
2977                 /* Enable charging */
2978                 err = ricoh619_set_bits(info->dev->parent,CHGCTL1_REG, 0x03);
2979                 if (err < 0) {
2980                         dev_err(info->dev, "Error in writing the control register\n");
2981                         goto free_device;
2982                 }
2983         }
2984         /* get OCV100_min, OCV100_min*/
2985         temp = (battery_init_para[info->num][24]<<8) | (battery_init_para[info->num][25]);
2986         rbat = temp * 1000 / 512 * 5000 / 4095;
2987         
2988         /* get vfchg value */
2989         err = ricoh619_read(info->dev->parent, BATSET2_REG, &val);
2990         if (err < 0) {
2991                 dev_err(info->dev, "Error in reading the batset2reg\n");
2992                 goto free_device;
2993         }
2994         val &= 0x70;
2995         val2 = val >> 4;
2996         if (val2 <= 3) {
2997                 vfchg_val = 4050 + val2 * 50;
2998         } else {
2999                 vfchg_val = 4350;
3000         }
3001         RICOH_FG_DBG("PMU : %s : test test val %d, val2 %d vfchg %d\n", __func__, val, val2, vfchg_val);
3002
3003         /* get  value */
3004         err = ricoh619_read(info->dev->parent, CHGISET_REG, &val);
3005         if (err < 0) {
3006                 dev_err(info->dev, "Error in reading the chgisetreg\n");
3007                 goto free_device;
3008         }
3009         val &= 0xC0;
3010         val2 = val >> 6;
3011         icchg_val = 50 + val2 * 50;
3012         RICOH_FG_DBG("PMU : %s : test test val %d, val2 %d icchg %d\n", __func__, val, val2, icchg_val);
3013
3014         info->soca->OCV100_min = ( vfchg_val * 99 / 100 - (icchg_val * (rbat +20))/1000 - 20 ) * 1000;
3015         info->soca->OCV100_max = ( vfchg_val * 101 / 100 - (icchg_val * (rbat +20))/1000 + 20 ) * 1000;
3016         
3017         RICOH_FG_DBG("PMU : %s : 100 min %d, 100 max %d vfchg %d icchg %d rbat %d\n",__func__,
3018         info->soca->OCV100_min,info->soca->OCV100_max,vfchg_val,icchg_val,rbat);
3019
3020 #ifdef ENABLE_LOW_BATTERY_DETECTION
3021         /* Set ADRQ=00 to stop ADC */
3022         ricoh619_write(info->dev->parent, RICOH619_ADC_CNT3, 0x0);
3023         /* Enable VSYS threshold Low interrupt */
3024         ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
3025         /* Set ADC auto conversion interval 250ms */
3026         ricoh619_write(info->dev->parent, RICOH619_ADC_CNT2, 0x0);
3027         /* Enable VSYS pin conversion in auto-ADC */
3028 //      ricoh619_write(info->dev->parent, RICOH619_ADC_CNT1, 0x10);
3029         ricoh619_write(info->dev->parent, RICOH619_ADC_CNT1, 0x16);
3030         /* Set VSYS threshold low voltage value = (voltage(V)*255)/(3*2.5) */
3031         val = info->alarm_vol_mv * 255 / 7500;
3032         ricoh619_write(info->dev->parent, RICOH619_ADC_VSYS_THL, val);
3033         /* Start auto-mode & average 4-time conversion mode for ADC */
3034         ricoh619_write(info->dev->parent, RICOH619_ADC_CNT3, 0x28);
3035         /* Enable master ADC INT */
3036         ricoh619_set_bits(info->dev->parent, RICOH619_INTC_INTEN, ADC_INT);
3037 #endif
3038
3039 free_device:
3040         return err;
3041 }
3042
3043
3044 static int get_power_supply_status(struct ricoh619_battery_info *info)
3045 {
3046         uint8_t status;
3047         uint8_t supply_state;
3048         uint8_t charge_state;
3049         int ret = 0;
3050
3051         /* get  power supply status */
3052         ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
3053         if (ret < 0) {
3054                 dev_err(info->dev, "Error in reading the control register\n");
3055                 return ret;
3056         }
3057
3058         charge_state = (status & 0x1F);
3059         supply_state = ((status & 0xC0) >> 6);
3060
3061         if (info->entry_factory_mode)
3062                         return POWER_SUPPLY_STATUS_NOT_CHARGING;
3063
3064         if (supply_state == SUPPLY_STATE_BAT) {
3065                 info->soca->chg_status = POWER_SUPPLY_STATUS_DISCHARGING;
3066         } else {
3067                 switch (charge_state) {
3068                 case    CHG_STATE_CHG_OFF:
3069                                 info->soca->chg_status
3070                                         = POWER_SUPPLY_STATUS_DISCHARGING;
3071                                 break;
3072                 case    CHG_STATE_CHG_READY_VADP:
3073                                 info->soca->chg_status
3074                                         = POWER_SUPPLY_STATUS_NOT_CHARGING;
3075                                 break;
3076                 case    CHG_STATE_CHG_TRICKLE:
3077                                 info->soca->chg_status
3078                                         = POWER_SUPPLY_STATUS_CHARGING;
3079                                 break;
3080                 case    CHG_STATE_CHG_RAPID:
3081                                 info->soca->chg_status
3082                                         = POWER_SUPPLY_STATUS_CHARGING;
3083                                 break;
3084                 case    CHG_STATE_CHG_COMPLETE:
3085                                 info->soca->chg_status
3086                                         = POWER_SUPPLY_STATUS_FULL;
3087                                 break;
3088                 case    CHG_STATE_SUSPEND:
3089                                 info->soca->chg_status
3090                                         = POWER_SUPPLY_STATUS_DISCHARGING;
3091                                 break;
3092                 case    CHG_STATE_VCHG_OVER_VOL:
3093                                 info->soca->chg_status
3094                                         = POWER_SUPPLY_STATUS_DISCHARGING;
3095                                 break;
3096                 case    CHG_STATE_BAT_ERROR:
3097                                 info->soca->chg_status
3098                                         = POWER_SUPPLY_STATUS_NOT_CHARGING;
3099                                 break;
3100                 case    CHG_STATE_NO_BAT:
3101                                 info->soca->chg_status
3102                                         = POWER_SUPPLY_STATUS_NOT_CHARGING;
3103                                 break;
3104                 case    CHG_STATE_BAT_OVER_VOL:
3105                                 info->soca->chg_status
3106                                         = POWER_SUPPLY_STATUS_NOT_CHARGING;
3107                                 break;
3108                 case    CHG_STATE_BAT_TEMP_ERR:
3109                                 info->soca->chg_status
3110                                         = POWER_SUPPLY_STATUS_NOT_CHARGING;
3111                                 break;
3112                 case    CHG_STATE_DIE_ERR:
3113                                 info->soca->chg_status
3114                                         = POWER_SUPPLY_STATUS_NOT_CHARGING;
3115                                 break;
3116                 case    CHG_STATE_DIE_SHUTDOWN:
3117                                 info->soca->chg_status
3118                                         = POWER_SUPPLY_STATUS_DISCHARGING;
3119                                 break;
3120                 case    CHG_STATE_NO_BAT2:
3121                                 info->soca->chg_status
3122                                         = POWER_SUPPLY_STATUS_NOT_CHARGING;
3123                                 break;
3124                 case    CHG_STATE_CHG_READY_VUSB:
3125                                 info->soca->chg_status
3126                                         = POWER_SUPPLY_STATUS_NOT_CHARGING;
3127                                 break;
3128                 default:
3129                                 info->soca->chg_status
3130                                         = POWER_SUPPLY_STATUS_UNKNOWN;
3131                                 break;
3132                 }
3133         }
3134
3135         return info->soca->chg_status;
3136 }
3137
3138 static int get_power_supply_Android_status(struct ricoh619_battery_info *info)
3139 {
3140
3141         get_power_supply_status(info);
3142
3143         /* get  power supply status */
3144         if (info->entry_factory_mode)
3145                         return POWER_SUPPLY_STATUS_NOT_CHARGING;
3146
3147         switch (info->soca->chg_status) {
3148                 case    POWER_SUPPLY_STATUS_UNKNOWN:
3149                                 return POWER_SUPPLY_STATUS_UNKNOWN;
3150                                 break;
3151
3152                 case    POWER_SUPPLY_STATUS_NOT_CHARGING:
3153                                 return POWER_SUPPLY_STATUS_NOT_CHARGING;
3154                                 break;
3155
3156                 case    POWER_SUPPLY_STATUS_DISCHARGING:
3157                                 return POWER_SUPPLY_STATUS_DISCHARGING;
3158                                 break;
3159
3160                 case    POWER_SUPPLY_STATUS_CHARGING:
3161                                 return POWER_SUPPLY_STATUS_CHARGING;
3162                                 break;
3163
3164                 case    POWER_SUPPLY_STATUS_FULL:
3165                                 if(info->soca->displayed_soc == 100 * 100) {
3166                                         return POWER_SUPPLY_STATUS_FULL;
3167                                 } else {
3168                                         return POWER_SUPPLY_STATUS_CHARGING;
3169                                 }
3170                                 break;
3171                 default:
3172                                 return POWER_SUPPLY_STATUS_UNKNOWN;
3173                                 break;
3174         }
3175
3176         return POWER_SUPPLY_STATUS_UNKNOWN;
3177 }
3178 extern struct ricoh619 *g_ricoh619;
3179 static void charger_irq_work(struct work_struct *work)
3180 {
3181         struct ricoh619_battery_info *info
3182                  = container_of(work, struct ricoh619_battery_info, irq_work);
3183         int ret = 0;
3184         uint8_t reg_val;
3185         RICOH_FG_DBG("PMU:%s In\n", __func__);
3186
3187         power_supply_changed(&info->battery);
3188         power_supply_changed(&powerac);
3189         power_supply_changed(&powerusb);
3190
3191 //      mutex_lock(&info->lock);
3192         
3193         if (info->chg_stat1 & 0x01) {
3194                 ricoh619_read(info->dev->parent, CHGSTATE_REG, &reg_val);
3195                 if (reg_val & 0x40) { /* USE ADP */     
3196                         #ifdef SUPPORT_USB_CONNECT_TO_ADP
3197                                 int i;
3198                                 for(i =0;i<60;i++){
3199                                 RICOH_FG_DBG("PMU:%s usb det dwc_otg_check_dpdm =%d\n", __func__,dwc_otg_check_dpdm(0));
3200                                 if(2 == dwc_otg_check_dpdm(0)){
3201                                 /* set adp limit current 2A */
3202                                 ricoh619_write(info->dev->parent, REGISET1_REG, 0x16);
3203                                 /* set charge current 2A */
3204                                 ricoh619_write(info->dev->parent, CHGISET_REG, 0xD3); 
3205                                 }
3206                                 else {
3207                                 /* set adp limit current 500ma */
3208                                 ricoh619_write(info->dev->parent, REGISET1_REG, 0x04);
3209                                 /* set charge current 500ma */
3210                                 ricoh619_write(info->dev->parent, CHGISET_REG, 0xc4); 
3211                                 }
3212                                 
3213                                 power_supply_changed(&info->battery);
3214                                 power_supply_changed(&powerac);
3215                                 power_supply_changed(&powerusb);
3216                                 msleep(100);
3217                                 }
3218                         #else //support adp and usb chag
3219                         if (gpio_is_valid(g_ricoh619->dc_det)){
3220                                 ret = gpio_request(g_ricoh619->dc_det, "ricoh619_dc_det");
3221                                 if (ret < 0) {
3222                                         RICOH_FG_DBG("Failed to request gpio %d with ret:""%d\n",g_ricoh619->dc_det, ret);
3223                                 }
3224                                 gpio_direction_input(g_ricoh619->dc_det);
3225                                 ret = gpio_get_value(g_ricoh619->dc_det);
3226
3227                                 if (ret ==0){
3228                                         /* set adp limit current 2A */
3229                                         ricoh619_write(info->dev->parent, REGISET1_REG, 0x16);
3230                                         /* set charge current 2A */
3231                                         ricoh619_write(info->dev->parent, CHGISET_REG, 0xD3);
3232                                 }
3233                                 else {
3234                                         /* set adp limit current 500ma */
3235                                         ricoh619_write(info->dev->parent, REGISET1_REG, 0x04);
3236                                         /* set charge current 500ma */
3237                                         ricoh619_write(info->dev->parent, CHGISET_REG, 0xc4); 
3238                                 }
3239                                 gpio_free(g_ricoh619->dc_det);
3240                         }
3241                         else{
3242                                 /* set adp limit current 2A */
3243                                 ricoh619_write(info->dev->parent, REGISET1_REG, 0x16);
3244                                 /* set charge current 2A */
3245                                 ricoh619_write(info->dev->parent, CHGISET_REG, 0xD3); 
3246                         }
3247                         #endif
3248                 }       
3249                 else if (reg_val & 0x80) { /* USE USB */
3250                         queue_work(info->usb_workqueue, &info->usb_irq_work);
3251                 }
3252         }
3253         info->chg_ctr = 0;
3254         info->chg_stat1 = 0;
3255         
3256         /* Enable Interrupt for VADP/USB */
3257         ret = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGCTR, 0xfc);
3258         if (ret < 0)
3259                 dev_err(info->dev,
3260                          "%s(): Error in enable charger mask INT %d\n",
3261                          __func__, ret);
3262
3263         /* Enable Interrupt for Charging & complete */
3264         ret = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGSTS1, 0xfc);
3265         if (ret < 0)
3266                 dev_err(info->dev,
3267                          "%s(): Error in enable charger mask INT %d\n",
3268                          __func__, ret);
3269
3270 //      mutex_unlock(&info->lock);
3271         RICOH_FG_DBG("PMU:%s Out\n", __func__);
3272 }
3273
3274 #ifdef ENABLE_LOW_BATTERY_DETECTION
3275 static void low_battery_irq_work(struct work_struct *work)
3276 {
3277         struct ricoh619_battery_info *info = container_of(work,
3278                  struct ricoh619_battery_info, low_battery_work.work);
3279
3280         int ret = 0;
3281
3282         RICOH_FG_DBG("PMU:%s In\n", __func__);
3283
3284         power_supply_changed(&info->battery);
3285
3286         /* Enable VADP threshold Low interrupt */
3287         ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
3288         if (ret < 0)
3289                 dev_err(info->dev,
3290                          "%s(): Error in enable adc mask INT %d\n",
3291                          __func__, ret);
3292 }
3293 #endif
3294
3295
3296 static void ricoh619_usb_charge_det(void)
3297 {
3298         struct ricoh619 *ricoh619 = g_ricoh619;
3299         ricoh619_set_bits(ricoh619->dev,REGISET2_REG,(1 << 7));  //set usb limit current  when SDP or other mode
3300         RICOH_FG_DBG("PMU:%s usb det dwc_otg_check_dpdm =%d\n", __func__,dwc_otg_check_dpdm(0));
3301         if(2 == dwc_otg_check_dpdm(0)){
3302         ricoh619_write(ricoh619->dev,REGISET2_REG,0x16);  //set usb limit current  2A
3303         ricoh619_write(ricoh619->dev,CHGISET_REG,0xD3);  //set charge current  2A
3304         }
3305         else {
3306         ricoh619_write(ricoh619->dev,REGISET2_REG,0x04);  //set usb limit current  500ma
3307         ricoh619_write(ricoh619->dev,CHGISET_REG,0xC4);  //set charge current   500ma
3308         }
3309         power_supply_changed(&powerac);
3310         power_supply_changed(&powerusb);
3311 }
3312
3313 static void usb_det_irq_work(struct work_struct *work)
3314 {
3315         struct ricoh619_battery_info *info = container_of(work,
3316                  struct ricoh619_battery_info, usb_irq_work);
3317         int ret = 0,i;
3318         uint8_t sts;
3319
3320         RICOH_FG_DBG("PMU:%s In\n", __func__);
3321
3322         power_supply_changed(&info->battery);
3323         power_supply_changed(&powerac);
3324         power_supply_changed(&powerusb);
3325
3326         mutex_lock(&info->lock);
3327
3328         /* Enable Interrupt for VUSB */
3329         ret = ricoh619_clr_bits(info->dev->parent,
3330                                          RICOH619_INT_MSK_CHGCTR, 0x02);
3331         if (ret < 0)
3332                 dev_err(info->dev,
3333                          "%s(): Error in enable charger mask INT %d\n",
3334                          __func__, ret);
3335
3336         mutex_unlock(&info->lock);
3337         ret = ricoh619_read(info->dev->parent, RICOH619_INT_MON_CHGCTR, &sts);
3338         if (ret < 0)
3339                 dev_err(info->dev, "Error in reading the control register\n");
3340
3341         sts &= 0x02;
3342         if (sts) {
3343                 for(i =0;i<60;i++){
3344                 ricoh619_usb_charge_det();
3345                 mdelay(10);
3346                 }
3347         } else {
3348                 /*********************/
3349                 /* No process ??     */
3350                 /*********************/
3351         }
3352         
3353         RICOH_FG_DBG("PMU:%s Out\n", __func__);
3354 }
3355
3356 extern void rk_send_wakeup_key(void);
3357 static irqreturn_t charger_in_isr(int irq, void *battery_info)
3358 {
3359         struct ricoh619_battery_info *info = battery_info;
3360         RICOH_FG_DBG("PMU:%s\n", __func__); 
3361
3362         info->chg_stat1 |= 0x01;
3363
3364         queue_work(info->workqueue, &info->irq_work);
3365 //      rk_send_wakeup_key();
3366
3367         return IRQ_HANDLED;
3368 }
3369
3370 static irqreturn_t charger_complete_isr(int irq, void *battery_info)
3371 {
3372         struct ricoh619_battery_info *info = battery_info;
3373         RICOH_FG_DBG("PMU:%s\n", __func__);
3374
3375         info->chg_stat1 |= 0x02;
3376         queue_work(info->workqueue, &info->irq_work);
3377 //      rk_send_wakeup_key();
3378         
3379         return IRQ_HANDLED;
3380 }
3381
3382 static irqreturn_t charger_usb_isr(int irq, void *battery_info)
3383 {
3384         struct ricoh619_battery_info *info = battery_info;
3385         RICOH_FG_DBG("PMU:%s\n", __func__);
3386
3387         info->chg_ctr |= 0x02;
3388         
3389         queue_work(info->workqueue, &info->irq_work);
3390         
3391         info->soca->dischg_state = 0;
3392         info->soca->chg_count = 0;
3393
3394 //      queue_work(info->usb_workqueue, &info->usb_irq_work);
3395         rk_send_wakeup_key(); 
3396          
3397         if (RICOH619_SOCA_UNSTABLE == info->soca->status
3398                 || RICOH619_SOCA_FG_RESET == info->soca->status)
3399                 info->soca->stable_count = 11;
3400         
3401         return IRQ_HANDLED;
3402 }
3403
3404 static irqreturn_t charger_adp_isr(int irq, void *battery_info)
3405 {
3406         struct ricoh619_battery_info *info = battery_info;
3407         RICOH_FG_DBG("PMU:%s\n", __func__);
3408
3409         info->chg_ctr |= 0x01;
3410         queue_work(info->workqueue, &info->irq_work);
3411         rk_send_wakeup_key(); 
3412
3413         info->soca->dischg_state = 0;
3414         info->soca->chg_count = 0;
3415         if (RICOH619_SOCA_UNSTABLE == info->soca->status
3416                 || RICOH619_SOCA_FG_RESET == info->soca->status)
3417                 info->soca->stable_count = 11;
3418
3419         return IRQ_HANDLED;
3420 }
3421
3422
3423 #ifdef ENABLE_LOW_BATTERY_DETECTION
3424 /*************************************************************/
3425 /* for Detecting Low Battery                                 */
3426 /*************************************************************/
3427
3428 static irqreturn_t adc_vsysl_isr(int irq, void *battery_info)
3429 {
3430
3431         struct ricoh619_battery_info *info = battery_info;
3432
3433 #if 1
3434         RICOH_FG_DBG("PMU:%s\n", __func__);
3435
3436         queue_delayed_work(info->monitor_wqueue, &info->low_battery_work,
3437                                         LOW_BATTERY_DETECTION_TIME*HZ);
3438
3439 #endif
3440
3441         RICOH_FG_DBG("PMU:%s\n", __func__);
3442 //      ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
3443         rk_send_wakeup_key(); 
3444
3445         return IRQ_HANDLED;
3446 }
3447 #endif
3448 #ifdef RICOH619_VADP_DROP_WORK
3449 static void vadp_drop_irq_work(struct work_struct *work)
3450 {
3451         struct ricoh619_battery_info *info = container_of(work,
3452                  struct ricoh619_battery_info, vadp_drop_work.work);
3453
3454         int ret = 0;
3455         uint8_t data[6];
3456         u16 reg[2];
3457
3458         RICOH_FG_DBG("PMU vadp_drop_work:%s In\n", __func__);
3459         mutex_lock(&info->lock);        
3460         ret = ricoh619_read(info->dev->parent, 0x6a, &data[0]);
3461         ret = ricoh619_read(info->dev->parent, 0x6b, &data[1]);
3462         ret = ricoh619_read(info->dev->parent, 0x6c, &data[2]);
3463         ret = ricoh619_read(info->dev->parent, 0x6d, &data[3]);
3464         ret = ricoh619_read(info->dev->parent, CHGSTATE_REG,&data[4]);
3465         reg[0]= (data[0]<<4) |data[1];
3466         reg[1]= (data[2]<<4) |data[3];
3467
3468 //      printk("PMU vadp_drop:%s In %08x %08x %08x %08x %08x %08x %d\n", __func__,data[0],data[1],data[2],data[3],reg[0],reg[1],ret);   
3469         if ((2*(reg[0] +82)) > 3*reg[1]){
3470                 ricoh619_write(info->dev->parent, 0xb3, 0x28);
3471 //              printk("PMU vadp_drop charger disable:%s In  %08x %08x\n", __func__,reg[0],reg[1]); 
3472         }
3473         else if(data[4] & 0xc0){
3474                 ret = ricoh619_read(info->dev->parent, 0xb3, &data[5]);
3475 //               printk("PMU charger is disabled:%s data[4]= %08x data[5]=%08x\n", __func__,data[4],data[5]);
3476                 if(((data[5] & 0x03) ==0)|| ((data[5] & 0x08)==0)){
3477                         ricoh619_write(info->dev->parent, 0xb3, 0x23);
3478                          ret = ricoh619_read(info->dev->parent, 0xb3, &data[5]);
3479 //                      printk("PMU charger enable:%s data[4]= %08x data[5]=%08x\n", __func__,data[4],data[5]);
3480                 }
3481         }
3482         power_supply_changed(&info->battery);
3483         power_supply_changed(&powerac);
3484         power_supply_changed(&powerusb);
3485         mutex_unlock(&info->lock);
3486         queue_delayed_work(info->monitor_wqueue, &info->vadp_drop_work,3*HZ);
3487
3488 }
3489 #endif
3490 /*
3491  * Get Charger Priority
3492  * - get higher-priority between VADP and VUSB
3493  * @ data: higher-priority is stored
3494  *         true : VUSB
3495  *         false: VADP
3496  */
3497  /*
3498 static int get_charge_priority(struct ricoh619_battery_info *info, bool *data)
3499 {
3500         int ret = 0;
3501         uint8_t val = 0;
3502
3503         ret = ricoh619_read(info->dev->parent, CHGCTL1_REG, &val);
3504         val = val >> 7;
3505         *data = (bool)val;
3506
3507         return ret;
3508 }
3509 */
3510
3511 /*
3512  * Set Charger Priority
3513  * - set higher-priority between VADP and VUSB
3514  * - data: higher-priority is stored
3515  *         true : VUSB
3516  *         false: VADP
3517  */
3518  /*
3519 static int set_charge_priority(struct ricoh619_battery_info *info, bool *data)
3520 {
3521         int ret = 0;
3522         uint8_t val = 0x80;
3523
3524         if (*data == 1)
3525                 ret = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, val);
3526         else
3527                 ret = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, val);
3528
3529         return ret;
3530 }
3531 */
3532 #ifdef  ENABLE_FUEL_GAUGE_FUNCTION
3533 static int get_check_fuel_gauge_reg(struct ricoh619_battery_info *info,
3534                                          int Reg_h, int Reg_l, int enable_bit)
3535 {
3536         uint8_t get_data_h, get_data_l;
3537         int old_data, current_data;
3538         int i;
3539         int ret = 0;
3540
3541         old_data = 0;
3542
3543         for (i = 0; i < 5 ; i++) {
3544                 ret = ricoh619_read(info->dev->parent, Reg_h, &get_data_h);
3545                 if (ret < 0) {
3546                         dev_err(info->dev, "Error in reading the control register\n");
3547                         return ret;
3548                 }
3549
3550                 ret = ricoh619_read(info->dev->parent, Reg_l, &get_data_l);
3551                 if (ret < 0) {
3552                         dev_err(info->dev, "Error in reading the control register\n");
3553                         return ret;
3554                 }
3555
3556                 current_data = ((get_data_h & 0xff) << 8) | (get_data_l & 0xff);
3557                 current_data = (current_data & enable_bit);
3558
3559                 if (current_data == old_data)
3560                         return current_data;
3561                 else
3562                         old_data = current_data;
3563         }
3564
3565         return current_data;
3566 }
3567
3568 static int calc_capacity(struct ricoh619_battery_info *info)
3569 {
3570         uint8_t capacity;
3571         int temp;
3572         int ret = 0;
3573         int nt;
3574         int temperature;
3575
3576         temperature = get_battery_temp_2(info) / 10; /* unit 0.1 degree -> 1 degree */
3577
3578         if (temperature >= 25) {
3579                 nt = 0;
3580         } else if (temperature >= 5) {
3581                 nt = (25 - temperature) * RICOH619_TAH_SEL2 * 625 / 100;
3582         } else {
3583                 nt = (625  + (5 - temperature) * RICOH619_TAL_SEL2 * 625 / 100);
3584         }
3585
3586         /* get remaining battery capacity from fuel gauge */
3587         ret = ricoh619_read(info->dev->parent, SOC_REG, &capacity);
3588         if (ret < 0) {
3589                 dev_err(info->dev, "Error in reading the control register\n");
3590                 return ret;
3591         }
3592
3593         temp = capacity * 100 * 100 / (10000 - nt);
3594
3595         temp = min(100, temp);
3596         temp = max(0, temp);
3597         
3598         return temp;            /* Unit is 1% */
3599 }
3600
3601 static int calc_capacity_2(struct ricoh619_battery_info *info)
3602 {
3603         uint8_t val;
3604         long capacity;
3605         int re_cap, fa_cap;
3606         int temp;
3607         int ret = 0;
3608         int nt;
3609         int temperature;
3610
3611         temperature = get_battery_temp_2(info) / 10; /* unit 0.1 degree -> 1 degree */
3612
3613         if (temperature >= 25) {
3614                 nt = 0;
3615         } else if (temperature >= 5) {
3616                 nt = (25 - temperature) * RICOH619_TAH_SEL2 * 625 / 100;
3617         } else {
3618                 nt = (625  + (5 - temperature) * RICOH619_TAL_SEL2 * 625 / 100);
3619         }
3620
3621         re_cap = get_check_fuel_gauge_reg(info, RE_CAP_H_REG, RE_CAP_L_REG,
3622                                                 0x7fff);
3623         fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
3624                                                 0x7fff);
3625
3626         if (fa_cap != 0) {
3627                 capacity = ((long)re_cap * 100 * 100 / fa_cap);
3628                 capacity = (long)(min(10000, (int)capacity));
3629                 capacity = (long)(max(0, (int)capacity));
3630         } else {
3631                 ret = ricoh619_read(info->dev->parent, SOC_REG, &val);
3632                 if (ret < 0) {
3633                         dev_err(info->dev, "Error in reading the control register\n");
3634                         return ret;
3635                 }
3636                 capacity = (long)val * 100;
3637         }
3638         
3639
3640         temp = (int)(capacity * 100 * 100 / (10000 - nt));
3641
3642         temp = min(10000, temp);
3643         temp = max(0, temp);
3644
3645         return temp;            /* Unit is 0.01% */
3646 }
3647
3648 static int get_battery_temp(struct ricoh619_battery_info *info)
3649 {
3650         int ret = 0;
3651         int sign_bit;
3652
3653         ret = get_check_fuel_gauge_reg(info, TEMP_1_REG, TEMP_2_REG, 0x0fff);
3654         if (ret < 0) {
3655                 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
3656                 return ret;
3657         }
3658
3659         /* bit3 of 0xED(TEMP_1) is sign_bit */
3660         sign_bit = ((ret & 0x0800) >> 11);
3661
3662         ret = (ret & 0x07ff);
3663
3664         if (sign_bit == 0)      /* positive value part */
3665                 /* conversion unit */
3666                 /* 1 unit is 0.0625 degree and retun unit
3667                  * should be 0.1 degree,
3668                  */
3669                 ret = ret * 625  / 1000;
3670         else {  /*negative value part */
3671                 ret = (~ret + 1) & 0x7ff;
3672                 ret = -1 * ret * 625 / 1000;
3673         }
3674
3675         return ret;
3676 }
3677
3678 static int get_battery_temp_2(struct ricoh619_battery_info *info)
3679 {
3680         uint8_t reg_buff[2];
3681         long temp, temp_off, temp_gain;
3682         bool temp_sign, temp_off_sign, temp_gain_sign;
3683         int Vsns = 0;
3684         int Iout = 0;
3685         int Vthm, Rthm;
3686         int reg_val = 0;
3687         int new_temp;
3688         long R_ln1, R_ln2;
3689         int ret = 0;
3690
3691         /* Calculate TEMP */
3692         ret = get_check_fuel_gauge_reg(info, TEMP_1_REG, TEMP_2_REG, 0x0fff);
3693         if (ret < 0) {
3694                 dev_err(info->dev, "Error in reading the fuel gauge register\n");
3695                 goto out;
3696         }
3697
3698         reg_val = ret;
3699         temp_sign = (reg_val & 0x0800) >> 11;
3700         reg_val = (reg_val & 0x07ff);
3701
3702         if (temp_sign == 0)     /* positive value part */
3703                 /* the unit is 0.0001 degree */
3704                 temp = (long)reg_val * 625;
3705         else {  /*negative value part */
3706                 reg_val = (~reg_val + 1) & 0x7ff;
3707                 temp = -1 * (long)reg_val * 625;
3708         }
3709
3710         /* Calculate TEMP_OFF */
3711         ret = ricoh619_bulk_reads_bank1(info->dev->parent,
3712                                         TEMP_OFF_H_REG, 2, reg_buff);
3713         if (ret < 0) {
3714                 dev_err(info->dev, "Error in reading the fuel gauge register\n");
3715                 goto out;
3716         }
3717
3718         reg_val = reg_buff[0] << 8 | reg_buff[1];
3719         temp_off_sign = (reg_val & 0x0800) >> 11;
3720         reg_val = (reg_val & 0x07ff);
3721
3722         if (temp_off_sign == 0) /* positive value part */
3723                 /* the unit is 0.0001 degree */
3724                 temp_off = (long)reg_val * 625;
3725         else {  /*negative value part */
3726                 reg_val = (~reg_val + 1) & 0x7ff;
3727                 temp_off = -1 * (long)reg_val * 625;
3728         }
3729
3730         /* Calculate TEMP_GAIN */
3731         ret = ricoh619_bulk_reads_bank1(info->dev->parent,
3732                                         TEMP_GAIN_H_REG, 2, reg_buff);
3733         if (ret < 0) {
3734                 dev_err(info->dev, "Error in reading the fuel gauge register\n");
3735                 goto out;
3736         }
3737
3738         reg_val = reg_buff[0] << 8 | reg_buff[1];
3739         temp_gain_sign = (reg_val & 0x0800) >> 11;
3740         reg_val = (reg_val & 0x07ff);
3741
3742         if (temp_gain_sign == 0)        /* positive value part */
3743                 /* 1 unit is 0.000488281. the result is 0.01 */
3744                 temp_gain = (long)reg_val * 488281 / 100000;
3745         else {  /*negative value part */
3746                 reg_val = (~reg_val + 1) & 0x7ff;
3747                 temp_gain = -1 * (long)reg_val * 488281 / 100000;
3748         }
3749
3750         /* Calculate VTHM */
3751         if (0 != temp_gain)
3752                 Vthm = (int)((temp - temp_off) / 4095 * 2500 / temp_gain);
3753         else {
3754                 RICOH_FG_DBG("PMU %s Skip to compensate temperature\n", __func__);
3755                 goto out;
3756         }
3757
3758         ret = measure_Ibatt_FG(info, &Iout);
3759         Vsns = Iout * 2 / 100;
3760
3761         if (temp < -120000) {
3762                 /* Low Temperature */
3763                 if (0 != (2500 - Vthm)) {
3764                         Rthm = 10 * 10 * (Vthm - Vsns) / (2500 - Vthm);
3765                 } else {
3766                         RICOH_FG_DBG("PMU %s Skip to compensate temperature\n", __func__);
3767                         goto out;
3768                 }
3769
3770                 R_ln1 = Rthm / 10;
3771                 R_ln2 =  (R_ln1 * R_ln1 * R_ln1 * R_ln1 * R_ln1 / 100000
3772                         - R_ln1 * R_ln1 * R_ln1 * R_ln1 * 2 / 100
3773                         + R_ln1 * R_ln1 * R_ln1 * 11
3774                         - R_ln1 * R_ln1 * 2980
3775                         + R_ln1 * 449800
3776                         - 784000) / 10000;
3777
3778                 /* the unit of new_temp is 0.1 degree */
3779                 new_temp = (int)((100 * 1000 * B_VALUE / (R_ln2 + B_VALUE * 100 * 1000 / 29815) - 27315) / 10);
3780                 RICOH_FG_DBG("PMU %s low temperature %d\n", __func__, new_temp/10);  
3781         } else if (temp > 520000) {
3782                 /* High Temperature */
3783                 if (0 != (2500 - Vthm)) {
3784                         Rthm = 100 * 10 * (Vthm - Vsns) / (2500 - Vthm);
3785                 } else {
3786                         RICOH_FG_DBG("PMU %s Skip to compensate temperature\n", __func__);
3787                         goto out;
3788                 }
3789                 RICOH_FG_DBG("PMU %s [Rthm] Rthm %d[ohm]\n", __func__, Rthm);
3790
3791                 R_ln1 = Rthm / 10;
3792                 R_ln2 =  (R_ln1 * R_ln1 * R_ln1 * R_ln1 * R_ln1 / 100000 * 15652 / 100
3793                         - R_ln1 * R_ln1 * R_ln1 * R_ln1 / 1000 * 23103 / 100
3794                         + R_ln1 * R_ln1 * R_ln1 * 1298 / 100
3795                         - R_ln1 * R_ln1 * 35089 / 100
3796                         + R_ln1 * 50334 / 10
3797                         - 48569) / 100;
3798                 /* the unit of new_temp is 0.1 degree */
3799                 new_temp = (int)((100 * 100 * B_VALUE / (R_ln2 + B_VALUE * 100 * 100 / 29815) - 27315) / 10);
3800                 RICOH_FG_DBG("PMU %s high temperature %d\n", __func__, new_temp/10);  
3801         } else {
3802                 /* the unit of new_temp is 0.1 degree */
3803                 new_temp = temp / 1000;
3804         }
3805
3806         return new_temp;
3807
3808 out:
3809         new_temp = get_battery_temp(info);
3810         return new_temp;
3811 }
3812 #if 0
3813 static int get_time_to_empty(struct ricoh619_battery_info *info)
3814 {
3815         int ret = 0;
3816
3817         ret = get_check_fuel_gauge_reg(info, TT_EMPTY_H_REG, TT_EMPTY_L_REG,
3818                                                                 0xffff);
3819         if (ret < 0) {
3820                 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
3821                 return ret;
3822         }
3823
3824         /* conversion unit */
3825         /* 1unit is 1miniute and return nnit should be 1 second */
3826         ret = ret * 60;
3827
3828         return ret;
3829 }
3830
3831 static int get_time_to_full(struct ricoh619_battery_info *info)
3832 {
3833         int ret = 0;
3834
3835         ret = get_check_fuel_gauge_reg(info, TT_FULL_H_REG, TT_FULL_L_REG,
3836                                                                 0xffff);
3837         if (ret < 0) {
3838                 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
3839                 return ret;
3840         }
3841
3842         ret = ret * 60;
3843
3844         return  ret;
3845 }
3846 #endif
3847 /* battery voltage is get from Fuel gauge */
3848 static int measure_vbatt_FG(struct ricoh619_battery_info *info, int *data)
3849 {
3850         int ret = 0;
3851
3852         if(info->soca->ready_fg == 1) {
3853                 ret = get_check_fuel_gauge_reg(info, VOLTAGE_1_REG, VOLTAGE_2_REG,
3854                                                                         0x0fff);
3855                 if (ret < 0) {
3856                         dev_err(info->dev, "Error in reading the fuel gauge control register\n");
3857                         return ret;
3858                 }
3859
3860                 *data = ret;
3861                 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
3862                 *data = *data * 50000 / 4095;
3863                 /* return unit should be 1uV */
3864                 *data = *data * 100;
3865                 info->soca->Vbat_old = *data;
3866         } else {
3867                 *data = info->soca->Vbat_old;
3868         }
3869
3870         return ret;
3871 }
3872
3873 static int measure_Ibatt_FG(struct ricoh619_battery_info *info, int *data)
3874 {
3875         int ret = 0;
3876
3877         ret =  get_check_fuel_gauge_reg(info, CC_AVERAGE1_REG,
3878                                                  CC_AVERAGE0_REG, 0x3fff);
3879         if (ret < 0) {
3880                 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
3881                 return ret;
3882         }
3883
3884         *data = (ret > 0x1fff) ? (ret - 0x4000) : ret;
3885         return ret;
3886 }
3887
3888 static int get_OCV_init_Data(struct ricoh619_battery_info *info, int index)
3889 {
3890         int ret = 0;
3891         ret =  (battery_init_para[info->num][index*2]<<8) | (battery_init_para[info->num][index*2+1]);
3892         return ret;
3893 }
3894
3895 static int get_OCV_voltage(struct ricoh619_battery_info *info, int index)
3896 {
3897         int ret = 0;
3898         ret =  get_OCV_init_Data(info, index);
3899         /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
3900         ret = ret * 50000 / 4095;
3901         /* return unit should be 1uV */
3902         ret = ret * 100;
3903         return ret;
3904 }
3905
3906 #else
3907 /* battery voltage is get from ADC */
3908 static int measure_vbatt_ADC(struct ricoh619_battery_info *info, int *data)
3909 {
3910         int     i;
3911         uint8_t data_l = 0, data_h = 0;
3912         int ret;
3913
3914         /* ADC interrupt enable */
3915         ret = ricoh619_set_bits(info->dev->parent, INTEN_REG, 0x08);
3916         if (ret < 0) {
3917                 dev_err(info->dev, "Error in setting the control register bit\n");
3918                 goto err;
3919         }
3920
3921         /* enable interrupt request of single mode */
3922         ret = ricoh619_set_bits(info->dev->parent, EN_ADCIR3_REG, 0x01);
3923         if (ret < 0) {
3924                 dev_err(info->dev, "Error in setting the control register bit\n");
3925                 goto err;
3926         }
3927
3928         /* single request */
3929         ret = ricoh619_write(info->dev->parent, ADCCNT3_REG, 0x10);
3930         if (ret < 0) {
3931                 dev_err(info->dev, "Error in writing the control register\n");
3932                 goto err;
3933         }
3934
3935         for (i = 0; i < 5; i++) {
3936         usleep(1000);
3937                 RICOH_FG_DBG("ADC conversion times: %d\n", i);
3938                 /* read completed flag of ADC */
3939                 ret = ricoh619_read(info->dev->parent, EN_ADCIR3_REG, &data_h);
3940                 if (ret < 0) {
3941                         dev_err(info->dev, "Error in reading the control register\n");
3942                         goto err;
3943                 }
3944
3945                 if (data_h & 0x01)
3946                         goto    done;
3947         }
3948
3949         dev_err(info->dev, "ADC conversion too long!\n");
3950         goto err;
3951
3952 done:
3953         ret = ricoh619_read(info->dev->parent, VBATDATAH_REG, &data_h);
3954         if (ret < 0) {
3955                 dev_err(info->dev, "Error in reading the control register\n");
3956                 goto err;
3957         }
3958
3959         ret = ricoh619_read(info->dev->parent, VBATDATAL_REG, &data_l);
3960         if (ret < 0) {
3961                 dev_err(info->dev, "Error in reading the control register\n");
3962                 goto err;
3963         }
3964
3965         *data = ((data_h & 0xff) << 4) | (data_l & 0x0f);
3966         /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
3967         *data = *data * 5000 / 4095;
3968         /* return unit should be 1uV */
3969         *data = *data * 1000;
3970
3971         return 0;
3972
3973 err:
3974         return -1;
3975
3976 #endif
3977
3978 static int measure_vsys_ADC(struct ricoh619_battery_info *info, int *data)
3979 {
3980         uint8_t data_l = 0, data_h = 0;
3981         int ret;
3982
3983         ret = ricoh619_read(info->dev->parent, VSYSDATAH_REG, &data_h);
3984         if (ret < 0) {
3985                 dev_err(info->dev, "Error in reading the control register\n");
3986         }
3987
3988         ret = ricoh619_read(info->dev->parent, VSYSDATAL_REG, &data_l);
3989         if (ret < 0) {
3990                 dev_err(info->dev, "Error in reading the control register\n");
3991         }
3992
3993         *data = ((data_h & 0xff) << 4) | (data_l & 0x0f);
3994         *data = *data * 1000 * 3 * 5 / 2 / 4095;
3995         /* return unit should be 1uV */
3996         *data = *data * 1000;
3997
3998         return 0;
3999 }
4000 /*
4001 static void ricoh619_external_power_changed(struct power_supply *psy)
4002 {
4003         struct ricoh619_battery_info *info;
4004
4005         info = container_of(psy, struct ricoh619_battery_info, battery);
4006         queue_delayed_work(info->monitor_wqueue,
4007                            &info->changed_work, HZ / 2);
4008         return;
4009 }
4010 */
4011
4012 static int ricoh619_batt_get_prop(struct power_supply *psy,
4013                                 enum power_supply_property psp,
4014                                 union power_supply_propval *val)
4015 {
4016         struct ricoh619_battery_info *info = dev_get_drvdata(psy->dev->parent);
4017         int data = 0;
4018         int ret = 0;
4019         uint8_t status;
4020
4021         mutex_lock(&info->lock);
4022
4023         switch (psp) {
4024
4025         case POWER_SUPPLY_PROP_ONLINE:
4026                 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
4027                 if (ret < 0) {
4028                         dev_err(info->dev, "Error in reading the control register\n");
4029                         mutex_unlock(&info->lock);
4030                         return ret;
4031                 }
4032                 #ifdef SUPPORT_USB_CONNECT_TO_ADP
4033                         if (psy->type == POWER_SUPPLY_TYPE_MAINS){
4034                                 if((2 == dwc_otg_check_dpdm(0)) && (status & 0x40))
4035                                         val->intval =1;
4036                                 else 
4037                                         val->intval =0;
4038                         }
4039                         else if (psy->type == POWER_SUPPLY_TYPE_USB){
4040                                 if((1 == dwc_otg_check_dpdm(0)) && (status & 0x40))
4041                                         val->intval =1;
4042                                 else 
4043                                         val->intval =0;
4044                         }
4045                 #else
4046                         if (psy->type == POWER_SUPPLY_TYPE_MAINS)
4047                                 val->intval = (status & 0x40 ? 1 : 0);
4048                         else if (psy->type == POWER_SUPPLY_TYPE_USB)
4049                                 val->intval = (status & 0x80 ? 1 : 0);
4050                 #endif
4051                 break;
4052         /* this setting is same as battery driver of 584 */
4053         case POWER_SUPPLY_PROP_STATUS:
4054                 if(info->chg_complete_tm_ov_flag == 0)
4055                 {
4056                         ret = get_power_supply_Android_status(info);
4057                         val->intval = ret;
4058                         info->status = ret;
4059                         /* RICOH_FG_DBG("Power Supply Status is %d\n",
4060                                                         info->status); */
4061                 }
4062                 else
4063                 {
4064                         val->intval = POWER_SUPPLY_STATUS_FULL;
4065                 }
4066                 break;
4067
4068         /* this setting is same as battery driver of 584 */
4069         case POWER_SUPPLY_PROP_PRESENT:
4070         //      val->intval = info->present;
4071                 val->intval = 1;
4072                 break;
4073
4074         /* current voltage is get from fuel gauge */
4075         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
4076                 /* return real vbatt Voltage */
4077 #ifdef  ENABLE_FUEL_GAUGE_FUNCTION
4078                 if (info->soca->ready_fg)
4079                         ret = measure_vbatt_FG(info, &data);
4080                 else {
4081                         //val->intval = -EINVAL;
4082                         data = info->cur_voltage * 1000;
4083                          RICOH_FG_DBG( "battery voltage is not ready\n"); 
4084                 }
4085 #else
4086                 ret = measure_vbatt_ADC(info, &data);
4087 #endif
4088                 val->intval = data;
4089                 /* convert unit uV -> mV */
4090                 info->cur_voltage = data / 1000;
4091                 
4092                 RICOH_FG_DBG( "battery voltage is %d mV\n",
4093                                                 info->cur_voltage);
4094                 break;
4095
4096 #ifdef  ENABLE_FUEL_GAUGE_FUNCTION
4097         /* current battery capacity is get from fuel gauge */
4098         case POWER_SUPPLY_PROP_CAPACITY:
4099                 if (info->entry_factory_mode){
4100                         val->intval = 100;
4101                         info->capacity = 100;
4102                 } else if ((info->soca->displayed_soc < 0) || (info->cur_voltage == 0)) {
4103                         val->intval = 50;
4104                         info->capacity = 50;
4105                 } else {
4106                         if(info->chg_complete_tm_ov_flag == 1)
4107                         {
4108                                 info->capacity = 100;
4109                                 val->intval = info->capacity;
4110                         }
4111                         else
4112                         {
4113                                 info->capacity = (info->soca->displayed_soc + 50)/100;
4114                                 val->intval = info->capacity;
4115                         }
4116                 }
4117                 RICOH_FG_DBG("battery capacity is %d%%\n", info->capacity); 
4118                 break;
4119
4120         /* current temperature of battery */
4121         case POWER_SUPPLY_PROP_TEMP:
4122                 if (info->soca->ready_fg) {
4123                         ret = 0;
4124                         val->intval = get_battery_temp_2(info);
4125                         info->battery_temp = val->intval/10;
4126                         RICOH_FG_DBG( "battery temperature is %d degree\n", info->battery_temp);
4127                 } else {
4128                         val->intval = info->battery_temp * 10;
4129                         /* RICOH_FG_DBG("battery temperature is not ready\n"); */
4130                 }
4131                 break;
4132
4133         #if 0
4134         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
4135                 if (info->soca->ready_fg) {
4136                         ret = get_time_to_empty(info);
4137                         val->intval = ret;
4138                         info->time_to_empty = ret/60;
4139                         RICOH_FG_DBG("time of empty battery is %d minutes\n", info->time_to_empty);
4140                 } else {
4141                         //val->intval = -EINVAL;
4142                         val->intval = info->time_to_empty * 60;
4143                         RICOH_FG_DBG("time of empty battery is %d minutes\n", info->time_to_empty);
4144                         /* RICOH_FG_DBG( "time of empty battery is not ready\n"); */
4145                 }
4146                 break;
4147
4148          case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
4149                 if (info->soca->ready_fg) {
4150                         ret = get_time_to_full(info);
4151                         val->intval = ret;
4152                         info->time_to_full = ret/60;
4153                         RICOH_FG_DBG( "time of full battery is %d minutes\n", info->time_to_full);
4154                 } else {
4155                         //val->intval = -EINVAL;
4156                         val->intval = info->time_to_full * 60;
4157                         RICOH_FG_DBG( "time of full battery is %d minutes\n", info->time_to_full);
4158                         /* RICOH_FG_DBG("time of full battery is not ready\n"); */
4159                 }
4160                 break;
4161
4162         #endif
4163 #endif
4164          case POWER_SUPPLY_PROP_TECHNOLOGY:
4165                 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
4166                 ret = 0;
4167                 break;
4168
4169         case POWER_SUPPLY_PROP_HEALTH:
4170                 val->intval = POWER_SUPPLY_HEALTH_GOOD;
4171                 ret = 0;
4172                 break;
4173         case POWER_SUPPLY_PROP_CURRENT_AVG:
4174                 measure_Ibatt_FG(info, &data);
4175                 //RICOH_FG_DBG("average current xxxxxxxxxxxxxx %d \n", data);
4176                 break;
4177         default:
4178                 mutex_unlock(&info->lock);
4179                 return -ENODEV;
4180         }
4181
4182         mutex_unlock(&info->lock);
4183
4184         return ret;
4185 }
4186
4187 static enum power_supply_property ricoh619_batt_props[] = {
4188         POWER_SUPPLY_PROP_STATUS,
4189         POWER_SUPPLY_PROP_PRESENT,
4190         POWER_SUPPLY_PROP_VOLTAGE_NOW,
4191         POWER_SUPPLY_PROP_CURRENT_AVG,
4192
4193 #ifdef  ENABLE_FUEL_GAUGE_FUNCTION
4194         POWER_SUPPLY_PROP_CAPACITY,
4195         POWER_SUPPLY_PROP_TEMP,
4196         //POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
4197         //POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
4198 #endif
4199         POWER_SUPPLY_PROP_TECHNOLOGY,
4200         POWER_SUPPLY_PROP_HEALTH,
4201 };
4202
4203 static enum power_supply_property ricoh619_power_props[] = {
4204         POWER_SUPPLY_PROP_ONLINE,
4205 };
4206
4207 struct power_supply     powerac = {
4208                 .name = "acpwr",
4209                 .type = POWER_SUPPLY_TYPE_MAINS,
4210                 .properties = ricoh619_power_props,
4211                 .num_properties = ARRAY_SIZE(ricoh619_power_props),
4212                 .get_property = ricoh619_batt_get_prop,
4213 };
4214
4215 struct power_supply     powerusb = {
4216                 .name = "usbpwr",
4217                 .type = POWER_SUPPLY_TYPE_USB,
4218                 .properties = ricoh619_power_props,
4219                 .num_properties = ARRAY_SIZE(ricoh619_power_props),
4220                 .get_property = ricoh619_batt_get_prop,
4221 };
4222
4223 #ifdef CONFIG_OF
4224 static struct ricoh619_battery_platform_data *
4225 ricoh619_battery_dt_init(struct platform_device *pdev)
4226 {
4227         struct device_node *nproot = pdev->dev.parent->of_node;
4228         struct device_node *np;
4229         struct ricoh619_battery_platform_data *pdata;
4230
4231         if (!nproot)
4232                 return pdev->dev.platform_data;
4233
4234         np = of_find_node_by_name(nproot, "battery");
4235         if (!np) {
4236                 dev_err(&pdev->dev, "failed to find battery node\n");
4237                 return NULL;
4238         }
4239
4240         pdata = devm_kzalloc(&pdev->dev,
4241                         sizeof(struct ricoh619_battery_platform_data),
4242                         GFP_KERNEL);
4243
4244         of_property_read_u32(np, "ricoh,monitor-time", &pdata->monitor_time);
4245         of_property_read_u32(np, "ricoh,alarm-vol-mv", &pdata->alarm_vol_mv);
4246
4247         /* check rage of b,.attery type */
4248         type_n = Battery_Type();
4249         RICOH_FG_DBG("%s type_n=%d\n", __func__, type_n);
4250
4251         switch (type_n) {
4252         case (0):
4253                 of_property_read_u32(np, "ricoh,ch-vfchg", &pdata->type[0].ch_vfchg);
4254                 of_property_read_u32(np, "ricoh,ch-vrchg", &pdata->type[0].ch_vrchg);
4255                 of_property_read_u32(np, "ricoh,ch-vbatovset", &pdata->type[0].ch_vbatovset);
4256                 of_property_read_u32(np, "ricoh,ch-ichg", &pdata->type[0].ch_ichg);
4257                 of_property_read_u32(np, "ricoh,ch-ilim-adp", &pdata->type[0].ch_ilim_adp);
4258                 of_property_read_u32(np, "ricoh,ch-ilim-usb", &pdata->type[0].ch_ilim_usb);
4259                 of_property_read_u32(np, "ricoh,ch-icchg", &pdata->type[0].ch_icchg);
4260                 of_property_read_u32(np, "ricoh,fg-target-vsys", &pdata->type[0].fg_target_vsys);
4261                 of_property_read_u32(np, "ricoh,fg-target-ibat", &pdata->type[0].fg_target_ibat);
4262                 of_property_read_u32(np, "ricoh,fg-poff-vbat", &pdata->type[0].fg_poff_vbat);
4263                 of_property_read_u32(np, "ricoh,jt-en", &pdata->type[0].jt_en);
4264                 of_property_read_u32(np, "ricoh,jt-hw-sw", &pdata->type[0].jt_hw_sw);
4265                 of_property_read_u32(np, "ricoh,jt-temp-h", &pdata->type[0].jt_temp_h);
4266                 of_property_read_u32(np, "ricoh,jt-temp-l", &pdata->type[0].jt_temp_l);
4267                 of_property_read_u32(np, "ricoh,jt-vfchg-h", &pdata->type[0].jt_vfchg_h);
4268                 of_property_read_u32(np, "ricoh,jt-vfchg-l", &pdata->type[0].jt_vfchg_l);
4269                 of_property_read_u32(np, "ricoh,jt-ichg-h", &pdata->type[0].jt_ichg_h);
4270                 of_property_read_u32(np, "ricoh,jt-ichg-l", &pdata->type[0].jt_ichg_l);
4271                 break;
4272 #if 0
4273         case (1):
4274                 of_property_read_u32(np, "ricoh,ch-vfchg-1", &pdata->type[1].ch_vfchg);
4275                 of_property_read_u32(np, "ricoh,ch-vrchg-1", &pdata->type[1].ch_vrchg);
4276                 of_property_read_u32(np, "ricoh,ch-vbatovset-1", &pdata->type[1].ch_vbatovset);
4277                 of_property_read_u32(np, "ricoh,ch-ichg-1", &pdata->type[1].ch_ichg);
4278                 of_property_read_u32(np, "ricoh,ch-ilim-adp-1", &pdata->type[1].ch_ilim_adp);
4279                 of_property_read_u32(np, "ricoh,ch-ilim-usb-1", &pdata->type[1].ch_ilim_usb);
4280                 of_property_read_u32(np, "ricoh,ch-icchg-1", &pdata->type[1].ch_icchg);
4281                 of_property_read_u32(np, "ricoh,fg-target-vsys-1", &pdata->type[1].fg_target_vsys);
4282                 of_property_read_u32(np, "ricoh,fg-target-ibat-1", &pdata->type[1].fg_target_ibat);
4283                 of_property_read_u32(np, "ricoh,fg-poff-vbat-1", &pdata->type[1].fg_poff_vbat);
4284                 of_property_read_u32(np, "ricoh,jt-en-1", &pdata->type[1].jt_en);
4285                 of_property_read_u32(np, "ricoh,jt-hw-sw-1", &pdata->type[1].jt_hw_sw);
4286                 of_property_read_u32(np, "ricoh,jt-temp-h-1", &pdata->type[1].jt_temp_h);
4287                 of_property_read_u32(np, "ricoh,jt-temp-l-1", &pdata->type[1].jt_temp_l);
4288                 of_property_read_u32(np, "ricoh,jt-vfchg-h-1", &pdata->type[1].jt_vfchg_h);
4289                 of_property_read_u32(np, "ricoh,jt-vfchg-l-1", &pdata->type[1].jt_vfchg_l);
4290                 of_property_read_u32(np, "ricoh,jt-ichg-h-1", &pdata->type[1].jt_ichg_h);
4291                 of_property_read_u32(np, "ricoh,jt-ichg-l-1", &pdata->type[1].jt_ichg_l);
4292                 break;
4293 #endif
4294         default:
4295                 of_node_put(np);
4296                 return 0;
4297         }
4298
4299         of_node_put(np);
4300
4301         return pdata;
4302 }
4303 #else
4304 static struct ricoh619_battery_platform_data *
4305 ricoh619_battery_dt_init(struct platform_device *pdev)
4306 {
4307         return pdev->dev.platform_data;
4308 }
4309 #endif
4310
4311 static int ricoh619_battery_probe(struct platform_device *pdev)
4312 {
4313         struct ricoh619_battery_info *info;
4314         struct ricoh619_battery_platform_data *pdata;
4315         struct ricoh619 *ricoh619 = dev_get_drvdata(pdev->dev.parent);
4316         int ret, temp;
4317
4318         RICOH_FG_DBG(KERN_INFO "PMU: %s : version is %s\n", __func__,RICOH619_BATTERY_VERSION);
4319
4320         pdata = ricoh619_battery_dt_init(pdev);
4321         if (!pdata) {
4322                 dev_err(&pdev->dev, "platform data isn't assigned to "
4323                         "power supply\n");
4324                 return -EINVAL;
4325         }
4326         info = devm_kzalloc(ricoh619->dev,sizeof(struct ricoh619_battery_info), GFP_KERNEL);
4327         if (!info)
4328                 return -ENOMEM;
4329         info->soca = devm_kzalloc(ricoh619->dev,sizeof(struct ricoh619_soca_info), GFP_KERNEL);
4330                 if (!info->soca)
4331                         return -ENOMEM;
4332
4333         info->dev = &pdev->dev;
4334         info->status = POWER_SUPPLY_STATUS_CHARGING;
4335         info->monitor_time = pdata->monitor_time * HZ;
4336         info->alarm_vol_mv = pdata->alarm_vol_mv;
4337
4338         /* check rage of battery num */
4339         info->num = Battery_Table();
4340         temp = sizeof(battery_init_para)/(sizeof(uint8_t)*32);
4341         if(info->num >= (sizeof(battery_init_para)/(sizeof(uint8_t)*32)))
4342         {
4343                 RICOH_FG_DBG("%s : Battery num is out of range\n", __func__);
4344                 info->num = 0;
4345         }
4346         RICOH_FG_DBG("%s info->num=%d,temp is %d\n", __func__, info->num,temp);
4347
4348         /* these valuse are set in platform */
4349         info->ch_vfchg = pdata->type[type_n].ch_vfchg;
4350         info->ch_vrchg = pdata->type[type_n].ch_vrchg;
4351         info->ch_vbatovset = pdata->type[type_n].ch_vbatovset;
4352         info->ch_ichg = pdata->type[type_n].ch_ichg;
4353         info->ch_ilim_adp = pdata->type[type_n].ch_ilim_adp;
4354         info->ch_ilim_usb = pdata->type[type_n].ch_ilim_usb;
4355         info->ch_icchg = pdata->type[type_n].ch_icchg;
4356         info->fg_target_vsys = pdata->type[type_n].fg_target_vsys;
4357         info->fg_target_ibat = pdata->type[type_n].fg_target_ibat;
4358         info->fg_poff_vbat = pdata->type[type_n].fg_poff_vbat;
4359         info->jt_en = pdata->type[type_n].jt_en;
4360         info->jt_hw_sw = pdata->type[type_n].jt_hw_sw;
4361         info->jt_temp_h = pdata->type[type_n].jt_temp_h;
4362         info->jt_temp_l = pdata->type[type_n].jt_temp_l;
4363         info->jt_vfchg_h = pdata->type[type_n].jt_vfchg_h;
4364         info->jt_vfchg_l = pdata->type[type_n].jt_vfchg_l;
4365         info->jt_ichg_h = pdata->type[type_n].jt_ichg_h;
4366         info->jt_ichg_l = pdata->type[type_n].jt_ichg_l;
4367
4368         info->adc_vdd_mv = ADC_VDD_MV;          /* 2800; */
4369         info->min_voltage = MIN_VOLTAGE;        /* 3100; */
4370         info->max_voltage = MAX_VOLTAGE;        /* 4200; */
4371         info->delay = 500;
4372         info->entry_factory_mode = false;
4373
4374         info->chg_complete_rd_flag = 0;
4375         info->chg_complete_rd_cnt = 0;
4376         info->chg_complete_tm_ov_flag = 0;
4377         info->chg_complete_sleep_flag = 0;
4378
4379         mutex_init(&info->lock);
4380         platform_set_drvdata(pdev, info);
4381
4382         info->battery.name = "battery";
4383         info->battery.type = POWER_SUPPLY_TYPE_BATTERY;
4384         info->battery.properties = ricoh619_batt_props;
4385         info->battery.num_properties = ARRAY_SIZE(ricoh619_batt_props);
4386         info->battery.get_property = ricoh619_batt_get_prop;
4387         info->battery.set_property = NULL;
4388 /*      info->battery.external_power_changed
4389                  = ricoh619_external_power_changed; */
4390
4391         /* Disable Charger/ADC interrupt */
4392         ret = ricoh619_clr_bits(info->dev->parent, RICOH619_INTC_INTEN,
4393                                                          CHG_INT | ADC_INT);
4394         if (ret<0)
4395                 goto out;
4396
4397         ret = ricoh619_init_battery(info);
4398         if (ret<0)
4399                 goto out;
4400 /*
4401 #ifdef ENABLE_FACTORY_MODE
4402         info->factory_mode_wqueue
4403                 = create_singlethread_workqueue("ricoh619_factory_mode");
4404         INIT_DEFERRABLE_WORK(&info->factory_mode_work,
4405                                          check_charging_state_work);
4406
4407         ret = ricoh619_factory_mode(info);
4408         if (ret<0)
4409                 goto out;
4410
4411 #endif
4412 */
4413         ret = power_supply_register(&pdev->dev, &info->battery);
4414
4415         if (ret<0)
4416                 info->battery.dev->parent = &pdev->dev;
4417
4418         ret = power_supply_register(&pdev->dev, &powerac);
4419         ret = power_supply_register(&pdev->dev, &powerusb);
4420
4421         info->monitor_wqueue
4422                 = create_singlethread_workqueue("ricoh619_battery_monitor");
4423
4424         info->workqueue = create_singlethread_workqueue("rc5t619_charger_in");
4425         INIT_WORK(&info->irq_work, charger_irq_work);
4426
4427         info->usb_workqueue
4428                 = create_singlethread_workqueue("rc5t619_usb_det");
4429         INIT_WORK(&info->usb_irq_work, usb_det_irq_work);
4430
4431         INIT_DEFERRABLE_WORK(&info->monitor_work,
4432                                          ricoh619_battery_work);
4433         INIT_DEFERRABLE_WORK(&info->displayed_work,
4434                                          ricoh619_displayed_work);
4435         INIT_DEFERRABLE_WORK(&info->charge_stable_work,
4436                                          ricoh619_stable_charge_countdown_work);
4437         INIT_DEFERRABLE_WORK(&info->charge_monitor_work,
4438                                          ricoh619_charge_monitor_work);
4439         INIT_DEFERRABLE_WORK(&info->get_charge_work,
4440                                          ricoh619_get_charge_work);
4441         INIT_DEFERRABLE_WORK(&info->jeita_work, ricoh619_jeita_work);
4442         INIT_DELAYED_WORK(&info->changed_work, ricoh619_changed_work);
4443
4444         INIT_DELAYED_WORK(&info->charge_complete_ready, ricoh619_charging_complete_work);
4445
4446         /* Charger IRQ workqueue settings */
4447
4448         ret = request_threaded_irq( irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FONCHGINT),NULL, charger_in_isr, IRQF_ONESHOT,
4449                                                 "rc5t619_charger_in", info);
4450         if (ret < 0) {
4451                 dev_err(&pdev->dev, "Can't get CHG_INT IRQ for chrager: %d\n",ret);
4452                 goto out;
4453         }
4454
4455         ret = request_threaded_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FCHGCMPINT),NULL, charger_complete_isr,
4456                                         IRQF_ONESHOT, "rc5t619_charger_comp",info);
4457         if (ret < 0) {
4458                 dev_err(&pdev->dev, "Can't get CHG_COMP IRQ for chrager: %d\n",ret);
4459                 goto out;
4460         }
4461
4462         ret = request_threaded_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FVUSBDETSINT) ,NULL, charger_usb_isr, IRQF_ONESHOT,
4463                                                 "rc5t619_usb_det", info);
4464         if (ret < 0) {
4465                 dev_err(&pdev->dev, "Can't get USB_DET IRQ for chrager: %d\n",ret);
4466                 goto out;
4467         }
4468
4469         ret = request_threaded_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FVADPDETSINT),NULL, charger_adp_isr, IRQF_ONESHOT,
4470                                                 "rc5t619_adp_det", info);
4471         if (ret < 0) {
4472                 dev_err(&pdev->dev,
4473                         "Can't get ADP_DET IRQ for chrager: %d\n", ret);
4474                 goto out;
4475         }
4476
4477 #ifdef ENABLE_LOW_BATTERY_DETECTION
4478         ret = request_threaded_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_VSYSLIR) ,NULL, adc_vsysl_isr, IRQF_ONESHOT,
4479                                                 "rc5t619_adc_vsysl", info);
4480         if (ret < 0) {
4481                 dev_err(&pdev->dev,
4482                         "Can't get ADC_VSYSL IRQ for chrager: %d\n", ret);
4483                 goto out;
4484         }
4485         INIT_DEFERRABLE_WORK(&info->low_battery_work,
4486                                          low_battery_irq_work);
4487 #endif
4488 #ifdef RICOH619_VADP_DROP_WORK
4489         INIT_DEFERRABLE_WORK(&info->vadp_drop_work,vadp_drop_irq_work);
4490         queue_delayed_work(info->monitor_wqueue, &info->vadp_drop_work,0);
4491 #endif
4492         /* Charger init and IRQ setting */
4493         ret = ricoh619_init_charger(info);
4494         if (ret<0)
4495                 goto out;
4496
4497 #ifdef  ENABLE_FUEL_GAUGE_FUNCTION
4498         ret = ricoh619_init_fgsoca(info);
4499 #endif
4500         queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
4501                                         RICOH619_MONITOR_START_TIME*HZ);
4502
4503         /* Enable Charger/ADC interrupt */
4504         ricoh619_set_bits(info->dev->parent, RICOH619_INTC_INTEN, CHG_INT | ADC_INT);
4505
4506         return 0;
4507
4508 out:
4509         kfree(info);
4510         return ret;
4511 }
4512
4513 static int ricoh619_battery_remove(struct platform_device *pdev)
4514 {
4515         struct ricoh619_battery_info *info = platform_get_drvdata(pdev);
4516         struct ricoh619 *ricoh619 = dev_get_drvdata(pdev->dev.parent);
4517         uint8_t val;
4518         int ret;
4519         int err;
4520         int cc_cap = 0;
4521         bool is_charging = true;
4522 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
4523         if (g_fg_on_mode
4524                  && (info->soca->status == RICOH619_SOCA_STABLE)) {
4525                 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
4526                 if (err < 0)
4527                         dev_err(info->dev, "Error in writing PSWR_REG\n");
4528                 g_soc = 0x7f;
4529         } else if (info->soca->status != RICOH619_SOCA_START
4530                 && info->soca->status != RICOH619_SOCA_UNSTABLE) {
4531                 if (info->soca->displayed_soc <= 0) {
4532                         val = 1;
4533                 } else {
4534                         val = (info->soca->displayed_soc + 50)/100;
4535                         val &= 0x7f;
4536                 }
4537                 ret = ricoh619_write(info->dev->parent, PSWR_REG, val);
4538                 if (ret < 0)
4539                         dev_err(info->dev, "Error in writing PSWR_REG\n");
4540
4541                 g_soc = val;
4542
4543                 ret = calc_capacity_in_period(info, &cc_cap,
4544                                                          &is_charging, true);
4545                 if (ret < 0)
4546                         dev_err(info->dev, "Read cc_sum Error !!-----\n");
4547         }
4548
4549         if (g_fg_on_mode == 0) {
4550                 ret = ricoh619_clr_bits(info->dev->parent,
4551                                          FG_CTRL_REG, 0x01);
4552                 if (ret < 0)
4553                         dev_err(info->dev, "Error in clr FG EN\n");
4554         }
4555         
4556         /* set rapid timer 300 min */
4557         err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x03);
4558         if (err < 0) {
4559                 dev_err(info->dev, "Error in writing the control register\n");
4560         }
4561
4562         if(info->capacity == 100)
4563         {
4564                 ret = ricoh619_write(info->dev->parent, PSWR_REG, 100);
4565                 if (ret < 0)
4566                         dev_err(info->dev, "Error in writing PSWR_REG\n");
4567         }
4568         
4569         free_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FONCHGINT), &info);
4570         free_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FCHGCMPINT), &info);
4571         free_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FVUSBDETSINT), &info);
4572         free_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FVADPDETSINT) , &info);
4573 #ifdef ENABLE_LOW_BATTERY_DETECTION
4574         free_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_VSYSLIR), &info);
4575 #endif
4576
4577         cancel_delayed_work(&info->monitor_work);
4578         cancel_delayed_work(&info->charge_stable_work);
4579         cancel_delayed_work(&info->charge_monitor_work);
4580         cancel_delayed_work(&info->get_charge_work);
4581         cancel_delayed_work(&info->displayed_work);
4582 #endif
4583         cancel_delayed_work(&info->changed_work);
4584 #ifdef ENABLE_LOW_BATTERY_DETECTION
4585         cancel_delayed_work(&info->low_battery_work);
4586 #endif
4587 #ifdef RICOH619_VADP_DROP_WORK
4588         cancel_delayed_work(&info->vadp_drop_work);
4589 #endif
4590 #ifdef ENABLE_FACTORY_MODE
4591         cancel_delayed_work(&info->factory_mode_work);
4592 #endif
4593         cancel_delayed_work(&info->jeita_work);
4594         cancel_delayed_work(&info->charge_complete_ready);
4595         
4596         cancel_work_sync(&info->irq_work);
4597         cancel_work_sync(&info->usb_irq_work);
4598
4599         flush_workqueue(info->monitor_wqueue);
4600         flush_workqueue(info->workqueue);
4601         flush_workqueue(info->usb_workqueue);
4602 #ifdef ENABLE_FACTORY_MODE
4603         flush_workqueue(info->factory_mode_wqueue);
4604 #endif
4605         destroy_workqueue(info->monitor_wqueue);
4606         destroy_workqueue(info->workqueue);
4607         destroy_workqueue(info->usb_workqueue);
4608 #ifdef ENABLE_FACTORY_MODE
4609         destroy_workqueue(info->factory_mode_wqueue);
4610 #endif
4611
4612         power_supply_unregister(&info->battery);
4613         kfree(info);
4614         platform_set_drvdata(pdev, NULL);
4615         return 0;
4616 }
4617
4618 #ifdef CONFIG_PM
4619 static int ricoh619_battery_suspend(struct device *dev)
4620 {
4621         struct ricoh619_battery_info *info = dev_get_drvdata(dev);
4622         uint8_t val;
4623         int ret;
4624         int err;
4625         int cc_cap = 0;
4626         bool is_charging = true;
4627         int displayed_soc_temp;
4628
4629         if (g_fg_on_mode
4630                  && (info->soca->status == RICOH619_SOCA_STABLE)) {
4631                 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
4632                 if (err < 0)
4633                         dev_err(info->dev, "Error in writing PSWR_REG\n");
4634                  g_soc = 0x7F;
4635                 info->soca->suspend_soc = info->soca->displayed_soc;
4636                 ret = calc_capacity_in_period(info, &cc_cap,
4637                                                          &is_charging, true);
4638                 if (ret < 0)
4639                         dev_err(info->dev, "Read cc_sum Error !!-----\n");
4640
4641         } else if (info->soca->status != RICOH619_SOCA_START
4642                 && info->soca->status != RICOH619_SOCA_UNSTABLE) {
4643                 if (info->soca->displayed_soc <= 0) {
4644                         val = 1;
4645                 } else {
4646                         val = (info->soca->displayed_soc + 50)/100;
4647                         val &= 0x7f;
4648                 }
4649                 ret = ricoh619_write(info->dev->parent, PSWR_REG, val);
4650                 if (ret < 0)
4651                         dev_err(info->dev, "Error in writing PSWR_REG\n");
4652
4653                 g_soc = val;
4654
4655                 ret = calc_capacity_in_period(info, &cc_cap,
4656                                                          &is_charging, true);
4657                 if (ret < 0)
4658                         dev_err(info->dev, "Read cc_sum Error !!-----\n");
4659                         
4660                 if (info->soca->status != RICOH619_SOCA_STABLE) {
4661                         info->soca->cc_delta
4662                                  = (is_charging == true) ? cc_cap : -cc_cap;
4663
4664                         displayed_soc_temp
4665                                = info->soca->displayed_soc + info->soca->cc_delta;
4666                         displayed_soc_temp = min(10000, displayed_soc_temp);
4667                         displayed_soc_temp = max(0, displayed_soc_temp);
4668                         info->soca->displayed_soc = displayed_soc_temp;
4669                 }
4670                 info->soca->suspend_soc = info->soca->displayed_soc;
4671                                         
4672         } else if (info->soca->status == RICOH619_SOCA_START
4673                 || info->soca->status == RICOH619_SOCA_UNSTABLE) {
4674
4675                 ret = ricoh619_read(info->dev->parent, PSWR_REG, &val);
4676                 if (ret < 0)
4677                         dev_err(info->dev, "Error in reading the pswr register\n");
4678                 val &= 0x7f;
4679
4680                 info->soca->suspend_soc = val * 100;
4681         }
4682
4683         if (info->soca->status == RICOH619_SOCA_DISP
4684                 || info->soca->status == RICOH619_SOCA_STABLE
4685                 || info->soca->status == RICOH619_SOCA_FULL) {
4686                 info->soca->soc = calc_capacity_2(info);
4687                 info->soca->soc_delta =
4688                         info->soca->soc_delta + (info->soca->soc - info->soca->last_soc);
4689
4690         } else {
4691                 info->soca->soc_delta = 0;
4692         }
4693
4694         if (info->soca->status == RICOH619_SOCA_STABLE
4695                 || info->soca->status == RICOH619_SOCA_FULL)
4696                 info->soca->status = RICOH619_SOCA_DISP;
4697         /* set rapid timer 300 min */
4698         err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x03);
4699         if (err < 0) {
4700                 dev_err(info->dev, "Error in writing the control register\n");
4701         }
4702
4703 //      disable_irq(charger_irq + RICOH619_IRQ_FONCHGINT);
4704 //      disable_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT);
4705 //      disable_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT);
4706 //      disable_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT);
4707 #ifdef ENABLE_LOW_BATTERY_DETECTION
4708 //      disable_irq(charger_irq + RICOH619_IRQ_VSYSLIR);
4709 #endif
4710 #if 0
4711         flush_delayed_work(&info->monitor_work);
4712         flush_delayed_work(&info->displayed_work);
4713         flush_delayed_work(&info->charge_stable_work);
4714         flush_delayed_work(&info->charge_monitor_work);
4715         flush_delayed_work(&info->get_charge_work);
4716         flush_delayed_work(&info->changed_work);
4717 #ifdef ENABLE_LOW_BATTERY_DETECTION
4718         flush_delayed_work(&info->low_battery_work);
4719 #endif
4720         flush_delayed_work(&info->factory_mode_work);
4721         flush_delayed_work(&info->jeita_work);
4722 #ifdef RICOH619_VADP_DROP_WORK
4723         flush_delayed_work(&info->vadp_drop_work);
4724 #endif
4725         
4726 //      flush_work(&info->irq_work);
4727 //      flush_work(&info->usb_irq_work);
4728 #else
4729         cancel_delayed_work(&info->monitor_work);
4730         cancel_delayed_work(&info->displayed_work);
4731         cancel_delayed_work(&info->charge_stable_work);
4732         cancel_delayed_work(&info->charge_monitor_work);
4733         cancel_delayed_work(&info->get_charge_work);
4734         cancel_delayed_work(&info->changed_work);
4735 #ifdef ENABLE_LOW_BATTERY_DETECTION
4736         cancel_delayed_work(&info->low_battery_work);
4737 #endif
4738         cancel_delayed_work(&info->charge_complete_ready);
4739 #ifdef ENABLE_FACTORY_MODE
4740         cancel_delayed_work(&info->factory_mode_work);
4741 #endif
4742         cancel_delayed_work(&info->jeita_work);
4743 #ifdef RICOH619_VADP_DROP_WORK
4744         cancel_delayed_work(&info->vadp_drop_work);
4745 #endif
4746         info->chg_complete_rd_cnt = 0;
4747         info->chg_complete_rd_flag = 0;
4748
4749         if(info->capacity == 100)
4750         {
4751                 ret = ricoh619_write(info->dev->parent, PSWR_REG, 100);
4752                 if (ret < 0)
4753                         dev_err(info->dev, "Error in writing PSWR_REG\n");
4754                 if(info->chg_complete_tm_ov_flag != 1)
4755                 {
4756                         info->chg_complete_tm_ov_flag = 0;
4757                         info->chg_complete_sleep_flag = 1;
4758                 }
4759         }
4760 //      flush_work(&info->irq_work);
4761 //      flush_work(&info->usb_irq_work);
4762 #endif
4763
4764         return 0;
4765 }
4766
4767 static int ricoh619_battery_resume(struct device *dev)
4768 {
4769         struct ricoh619_battery_info *info = dev_get_drvdata(dev);
4770         uint8_t val;
4771         int ret;
4772         int displayed_soc_temp;
4773         int cc_cap = 0;
4774         bool is_charging = true;
4775         bool is_jeita_updated;
4776         int i;
4777
4778         RICOH_FG_DBG(KERN_INFO "PMU: %s: \n", __func__);
4779
4780         ret = check_jeita_status(info, &is_jeita_updated);
4781         if (ret < 0) {
4782                 dev_err(info->dev, "Error in updating JEITA %d\n", ret);
4783         }
4784
4785         if (info->entry_factory_mode) {
4786                 info->soca->displayed_soc = -EINVAL;
4787         } else if (RICOH619_SOCA_ZERO == info->soca->status) {
4788                 if (calc_ocv(info) > get_OCV_voltage(info, 0)) {
4789                         ret = ricoh619_read(info->dev->parent, PSWR_REG, &val);
4790                         val &= 0x7f;
4791                         info->soca->soc = val * 100;
4792                         if (ret < 0) {
4793                                 dev_err(info->dev,
4794                                          "Error in reading PSWR_REG %d\n", ret);
4795                                 info->soca->soc
4796                                          = calc_capacity(info) * 100;
4797                         }
4798
4799                         ret = calc_capacity_in_period(info, &cc_cap,
4800                                                          &is_charging, true);
4801                         if (ret < 0)
4802                                 dev_err(info->dev, "Read cc_sum Error !!-----\n");
4803
4804                         info->soca->cc_delta
4805                                  = (is_charging == true) ? cc_cap : -cc_cap;
4806
4807                         displayed_soc_temp
4808                                  = info->soca->soc + info->soca->cc_delta;
4809                         if (displayed_soc_temp < 0)
4810                                 displayed_soc_temp = 0;
4811                         displayed_soc_temp = min(10000, displayed_soc_temp);
4812                         displayed_soc_temp = max(0, displayed_soc_temp);
4813                         info->soca->displayed_soc = displayed_soc_temp;
4814
4815                         ret = reset_FG_process(info);
4816
4817                         if (ret < 0)
4818                                 dev_err(info->dev, "Error in writing the control register\n");
4819                         info->soca->status = RICOH619_SOCA_FG_RESET;
4820
4821                 } else
4822                         info->soca->displayed_soc = 0;
4823         } else {
4824                 info->soca->soc = info->soca->suspend_soc;
4825
4826                 if (RICOH619_SOCA_START == info->soca->status
4827                         || RICOH619_SOCA_UNSTABLE == info->soca->status) {
4828                         ret = calc_capacity_in_period(info, &cc_cap,
4829                                                          &is_charging, false);
4830                 } else { 
4831                         ret = calc_capacity_in_period(info, &cc_cap,
4832                                                          &is_charging, true);
4833                 }
4834
4835                 if (ret < 0)
4836                         dev_err(info->dev, "Read cc_sum Error !!-----\n");
4837
4838                 info->soca->cc_delta = (is_charging == true) ? cc_cap : -cc_cap;
4839
4840                 displayed_soc_temp = info->soca->soc + info->soca->cc_delta;
4841                 if (info->soca->zero_flg == 1) {
4842                         if((info->soca->Ibat_ave >= 0) 
4843                         || (displayed_soc_temp >= 100)){
4844                                 info->soca->zero_flg = 0;
4845                         } else {
4846                                 displayed_soc_temp = 0;
4847                         }
4848                 } else if (displayed_soc_temp < 100) {
4849                         /* keep DSOC = 1 when Vbat is over 3.4V*/
4850                         if( info->fg_poff_vbat != 0) {
4851                                 if (info->soca->Vbat_ave < 2000*1000) { /* error value */
4852                                         displayed_soc_temp = 100;
4853                                 } else if (info->soca->Vbat_ave < info->fg_poff_vbat*1000) {
4854                                         displayed_soc_temp = 0;
4855                                         info->soca->zero_flg = 1;
4856                                 } else {
4857                                         displayed_soc_temp = 100;
4858                                 }
4859                         }
4860                 }
4861                 displayed_soc_temp = min(10000, displayed_soc_temp);
4862                 displayed_soc_temp = max(0, displayed_soc_temp);
4863
4864                 if (0 == info->soca->jt_limit) {
4865                         check_charge_status_2(info, displayed_soc_temp);
4866                 } else {
4867                         info->soca->displayed_soc = displayed_soc_temp;
4868                 }
4869
4870                 if (RICOH619_SOCA_DISP == info->soca->status) {
4871                         info->soca->last_soc = calc_capacity_2(info);
4872                 }
4873         }
4874
4875         ret = measure_vbatt_FG(info, &info->soca->Vbat_ave);
4876         ret = measure_vsys_ADC(info, &info->soca->Vsys_ave);
4877         ret = measure_Ibatt_FG(info, &info->soca->Ibat_ave);
4878
4879         if(info->chg_complete_sleep_flag == 1)
4880         {
4881                 info->chg_complete_tm_ov_flag = 1;
4882                 info->chg_complete_sleep_flag = 0;
4883         }
4884
4885         power_supply_changed(&info->battery);
4886         queue_delayed_work(info->monitor_wqueue, &info->displayed_work, HZ);
4887
4888         if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
4889                 info->soca->stable_count = 10;
4890                 queue_delayed_work(info->monitor_wqueue,
4891                                          &info->charge_stable_work,
4892                                          RICOH619_FG_STABLE_TIME*HZ/10);
4893         } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
4894                 info->soca->stable_count = 1;
4895
4896                 for (i = 0; i < 3; i = i+1)
4897                         info->soca->reset_soc[i] = 0;
4898                 info->soca->reset_count = 0;
4899
4900                 queue_delayed_work(info->monitor_wqueue,
4901                                          &info->charge_stable_work,
4902                                          RICOH619_FG_RESET_TIME*HZ);
4903         }
4904
4905         queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
4906                                                  info->monitor_time);
4907
4908         queue_delayed_work(info->monitor_wqueue, &info->charge_monitor_work,
4909                                          RICOH619_CHARGE_RESUME_TIME * HZ);
4910
4911         info->soca->chg_count = 0;
4912         queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
4913                                          RICOH619_CHARGE_RESUME_TIME * HZ);
4914         #ifdef RICOH619_VADP_DROP_WORK
4915         queue_delayed_work(info->monitor_wqueue, &info->vadp_drop_work,1 * HZ);
4916         #endif
4917         if (info->jt_en) {
4918                 if (!info->jt_hw_sw) {
4919                         queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
4920                                          RICOH619_JEITA_UPDATE_TIME * HZ);
4921                 }
4922         }
4923 //      ricoh619_write(info->dev->parent, 0x9d, 0x00);
4924 //      enable_irq(charger_irq + RICOH619_IRQ_FONCHGINT);
4925 //      enable_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT);
4926 //      enable_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT);
4927 //      enable_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT);
4928 #ifdef ENABLE_LOW_BATTERY_DETECTION
4929 //      enable_irq(charger_irq + RICOH619_IRQ_VSYSLIR);
4930 #endif
4931         ricoh619_write(info->dev->parent, 0x9d, 0x4d);
4932         return 0;
4933 }
4934
4935 #ifdef CONFIG_OF
4936 static const struct of_device_id ricoh619_battery_dt_match[] = {
4937         { .compatible = "ricoh,ricoh619-battery", },
4938         {},
4939 };
4940 MODULE_DEVICE_TABLE(of, ricoh619_battery_dt_match);
4941 #endif
4942
4943
4944 static const struct dev_pm_ops ricoh619_battery_pm_ops = {
4945         .suspend        = ricoh619_battery_suspend,
4946         .resume         = ricoh619_battery_resume,
4947 };
4948 #endif
4949
4950 static struct platform_driver ricoh619_battery_driver = {
4951         .driver = {
4952                                 .name   = "ricoh619-battery",
4953                                 .owner  = THIS_MODULE,
4954                                 .of_match_table = of_match_ptr(ricoh619_battery_dt_match),
4955 #ifdef CONFIG_PM
4956                                 .pm     = &ricoh619_battery_pm_ops,
4957 #endif
4958         },
4959         .probe  = ricoh619_battery_probe,
4960         .remove = ricoh619_battery_remove,
4961 };
4962
4963 static int __init ricoh619_battery_init(void)
4964 {
4965         RICOH_FG_DBG("PMU: %s\n", __func__);
4966         return platform_driver_register(&ricoh619_battery_driver);
4967 }
4968 fs_initcall_sync(ricoh619_battery_init);
4969
4970 static void __exit ricoh619_battery_exit(void)
4971 {
4972         platform_driver_unregister(&ricoh619_battery_driver);
4973 }
4974 module_exit(ricoh619_battery_exit);
4975
4976 MODULE_DESCRIPTION("RICOH619 Battery driver");
4977 MODULE_ALIAS("platform:ricoh619-battery");
4978 MODULE_LICENSE("GPL");