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