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