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