clk: rockchip: support setting ddr clock via SCPI APIs
[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/rk_keys.h>
39 #include <linux/rtc.h>
40
41 #include <linux/interrupt.h>
42 #include <linux/irq.h>
43 #include <linux/irqdomain.h>
44
45
46 /* define for function */
47 #define ENABLE_FUEL_GAUGE_FUNCTION
48 #define ENABLE_LOW_BATTERY_DETECTION
49 //#define ENABLE_FACTORY_MODE
50 #define DISABLE_CHARGER_TIMER
51 /* #define ENABLE_FG_KEEP_ON_MODE */
52 /* #define ENABLE_OCV_TABLE_CALIB */
53 //#define SUPPORT_USB_CONNECT_TO_ADP
54
55
56 /* FG setting */
57 #define RICOH619_REL1_SEL_VALUE         64
58 #define RICOH619_REL2_SEL_VALUE         0
59
60 enum int_type {
61         SYS_INT  = 0x01,
62         DCDC_INT = 0x02,
63         ADC_INT  = 0x08,
64         GPIO_INT = 0x10,
65         CHG_INT  = 0x40,
66 };
67
68 //for debug   #ifdef ENABLE_FUEL_GAUGE_FUNCTION
69 /* define for FG delayed time */
70 #define RICOH619_MONITOR_START_TIME             15
71 #define RICOH619_FG_RESET_TIME                  6
72 #define RICOH619_FG_STABLE_TIME         120
73 #define RICOH619_DISPLAY_UPDATE_TIME            15
74 #define RICOH619_LOW_VOL_DOWN_TIME              10
75 #define RICOH619_CHARGE_MONITOR_TIME            20
76 #define RICOH619_CHARGE_RESUME_TIME             1
77 #define RICOH619_CHARGE_CALC_TIME               1
78 #define RICOH619_JEITA_UPDATE_TIME              60
79 #define RICOH619_DELAY_TIME                             60
80 /* define for FG parameter */
81 #define RICOH619_MAX_RESET_SOC_DIFF             5
82 #define RICOH619_GET_CHARGE_NUM         10
83 #define RICOH619_UPDATE_COUNT_DISP              4
84 #define RICOH619_UPDATE_COUNT_FULL              4
85 #define RICOH619_UPDATE_COUNT_FULL_RESET        7
86 #define RICOH619_CHARGE_UPDATE_TIME             3
87 #define RICOH619_FULL_WAIT_TIME                 4
88 #define RE_CAP_GO_DOWN                          10      /* 40 */
89 #define RICOH619_ENTER_LOW_VOL                  70
90 #define RICOH619_TAH_SEL2                       5
91 #define RICOH619_TAL_SEL2                       6
92
93 #define RICOH619_OCV_OFFSET_BOUND       3
94 #define RICOH619_OCV_OFFSET_RATIO       2
95
96 #define RICOH619_VADP_DROP_WORK
97 #define RICOH619_TIME_CHG_STEP  (1*HZ)// unit:secound
98 #define RICOH619_TIME_CHG_COUNT 15*60//only for test //15*60 
99
100 /* define for FG status */
101 enum {
102         RICOH619_SOCA_START,
103         RICOH619_SOCA_UNSTABLE,
104         RICOH619_SOCA_FG_RESET,
105         RICOH619_SOCA_DISP,
106         RICOH619_SOCA_STABLE,
107         RICOH619_SOCA_ZERO,
108         RICOH619_SOCA_FULL,
109         RICOH619_SOCA_LOW_VOL,
110 };
111 //#endif
112
113 #ifdef ENABLE_LOW_BATTERY_DETECTION
114 #define LOW_BATTERY_DETECTION_TIME              10
115 #endif
116
117 struct ricoh619_soca_info {
118         int Rbat;
119         int n_cap;
120         int ocv_table_def[11];
121         int ocv_table[11];
122         int ocv_table_low[11];
123         int soc;                /* Latest FG SOC value */
124         int displayed_soc;
125         int suspend_soc;
126         int status;             /* SOCA status 0: Not initial; 5: Finished */
127         int stable_count;
128         int chg_status;         /* chg_status */
129         int soc_delta;          /* soc delta for status3(DISP) */
130         int cc_delta;
131         int cc_cap_offset;
132         int last_soc;
133         int last_displayed_soc;
134         int ready_fg;
135         int reset_count;
136         int reset_soc[3];
137         int chg_cmp_times;
138         int dischg_state;
139         int Vbat[RICOH619_GET_CHARGE_NUM];
140         int Vsys[RICOH619_GET_CHARGE_NUM];
141         int Ibat[RICOH619_GET_CHARGE_NUM];
142         int Vbat_ave;
143         int Vbat_old;
144         int Vsys_ave;
145         int Ibat_ave;
146         int chg_count;
147         int full_reset_count;
148         int soc_full;
149         int fc_cap;
150         /* for LOW VOL state */
151         int target_use_cap;
152         int hurry_up_flg;
153         int zero_flg;
154         int re_cap_old;
155         int cutoff_ocv;
156         int Rsys;
157         int target_vsys;
158         int target_ibat;
159         int jt_limit;
160         int OCV100_min;
161         int OCV100_max;
162         int R_low;
163         int rsoc_ready_flag;
164         int init_pswr;
165         int last_cc_sum;
166 };
167
168 struct ricoh619_battery_info {
169         struct device      *dev;
170         struct power_supply     battery;
171         struct delayed_work     monitor_work;
172         struct delayed_work     displayed_work;
173         struct delayed_work     charge_stable_work;
174         struct delayed_work     changed_work;
175 #ifdef ENABLE_LOW_BATTERY_DETECTION
176         struct delayed_work     low_battery_work;
177 #endif
178         struct delayed_work     charge_monitor_work;
179         struct delayed_work     get_charge_work;
180         struct delayed_work     jeita_work;
181         struct delayed_work     charge_complete_ready;
182
183         struct work_struct      irq_work;       /* for Charging & VUSB/VADP */
184         struct work_struct      usb_irq_work;   /* for ADC_VUSB */
185         #ifdef RICOH619_VADP_DROP_WORK
186         struct delayed_work     vadp_drop_work;
187         #endif
188         struct workqueue_struct *monitor_wqueue;
189         struct workqueue_struct *workqueue;     /* for Charging & VUSB/VADP */
190         struct workqueue_struct *usb_workqueue; /* for ADC_VUSB */
191
192 #ifdef ENABLE_FACTORY_MODE
193         struct delayed_work     factory_mode_work;
194         struct workqueue_struct *factory_mode_wqueue;
195 #endif
196
197         struct mutex            lock;
198         unsigned long           monitor_time;
199         int             adc_vdd_mv;
200         int             multiple;
201         int             alarm_vol_mv;
202         int             status;
203         int             min_voltage;
204         int             max_voltage;
205         int             cur_voltage;
206         int             capacity;
207         int             battery_temp;
208         int             time_to_empty;
209         int             time_to_full;
210         int             chg_ctr;
211         int             chg_stat1;
212         unsigned        present:1;
213         u16             delay;
214         struct          ricoh619_soca_info *soca;
215         int             first_pwon;
216         bool            entry_factory_mode;
217         int             ch_vfchg;
218         int             ch_vrchg;
219         int             ch_vbatovset;
220         int             ch_ichg;
221         int             ch_ilim_adp;
222         int             ch_ilim_usb;
223         int             ch_icchg;
224         int             fg_target_vsys;
225         int             fg_target_ibat;
226         int             fg_poff_vbat;
227         int             jt_en;
228         int             jt_hw_sw;
229         int             jt_temp_h;
230         int             jt_temp_l;
231         int             jt_vfchg_h;
232         int             jt_vfchg_l;
233         int             jt_ichg_h;
234         int             jt_ichg_l;
235
236         int     chg_complete_rd_flag;
237         int     chg_complete_rd_cnt;
238         int             chg_complete_tm_ov_flag;
239         int             chg_complete_sleep_flag;
240         int             chg_old_dsoc;
241
242         int             num;
243         };
244
245 struct power_supply powerac;
246 struct power_supply powerusb;
247
248 int g_full_flag;
249 int charger_irq;
250 /* this value is for mfd fucntion */
251 int g_soc;
252 int g_fg_on_mode;
253 int type_n;
254 extern int dwc_otg_check_dpdm(bool wait);
255 /*This is for full state*/
256 static int BatteryTableFlagDef=0;
257 static int BatteryTypeDef=0;
258 static int Battery_Type(void)
259 {
260         return BatteryTypeDef;
261 }
262
263 static int Battery_Table(void)
264 {
265         return BatteryTableFlagDef;
266 }
267
268 static void ricoh619_battery_work(struct work_struct *work)
269 {
270         struct ricoh619_battery_info *info = container_of(work,
271                 struct ricoh619_battery_info, monitor_work.work);
272
273         RICOH_FG_DBG("PMU: %s\n", __func__);
274         power_supply_changed(&info->battery);
275         queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
276                            info->monitor_time);
277 }
278
279 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
280 static int measure_vbatt_FG(struct ricoh619_battery_info *info, int *data);
281 static int measure_Ibatt_FG(struct ricoh619_battery_info *info, int *data);
282 static int calc_capacity(struct ricoh619_battery_info *info);
283 static int calc_capacity_2(struct ricoh619_battery_info *info);
284 static int get_OCV_init_Data(struct ricoh619_battery_info *info, int index);
285 static int get_OCV_voltage(struct ricoh619_battery_info *info, int index);
286 static int get_check_fuel_gauge_reg(struct ricoh619_battery_info *info,
287                                          int Reg_h, int Reg_l, int enable_bit);
288 static int calc_capacity_in_period(struct ricoh619_battery_info *info,
289                                  int *cc_cap, bool *is_charging, bool cc_rst);
290 //static int get_charge_priority(struct ricoh619_battery_info *info, bool *data);
291 //static int set_charge_priority(struct ricoh619_battery_info *info, bool *data);
292 static int get_power_supply_status(struct ricoh619_battery_info *info);
293 static int get_power_supply_Android_status(struct ricoh619_battery_info *info);
294 static int measure_vsys_ADC(struct ricoh619_battery_info *info, int *data);
295 static int Calc_Linear_Interpolation(int x0, int y0, int x1, int y1, int y);
296 static int get_battery_temp(struct ricoh619_battery_info *info);
297 static int get_battery_temp_2(struct ricoh619_battery_info *info);
298 static int check_jeita_status(struct ricoh619_battery_info *info, bool *is_jeita_updated);
299 static void ricoh619_scaling_OCV_table(struct ricoh619_battery_info *info, int cutoff_vol, int full_vol, int *start_per, int *end_per);
300 //static int ricoh619_Check_OCV_Offset(struct ricoh619_battery_info *info);
301
302 static int calc_ocv(struct ricoh619_battery_info *info)
303 {
304         int Vbat = 0;
305         int Ibat = 0;
306         int ret;
307         int ocv;
308
309         ret = measure_vbatt_FG(info, &Vbat);
310         ret = measure_Ibatt_FG(info, &Ibat);
311
312         ocv = Vbat - Ibat * info->soca->Rbat;
313
314         return ocv;
315 }
316
317 #if 0
318 static int set_Rlow(struct ricoh619_battery_info *info)
319 {
320         int err;
321         int Rbat_low_max;
322         uint8_t val;
323         int Vocv;
324         int temp;
325
326         if (info->soca->Rbat == 0)
327                         info->soca->Rbat = get_OCV_init_Data(info, 12) * 1000 / 512
328                                                          * 5000 / 4095;
329         
330         Vocv = calc_ocv(info);
331         Rbat_low_max = info->soca->Rbat * 1.5;
332
333         if (Vocv < get_OCV_voltage(info,3))
334         {
335                 info->soca->R_low = Calc_Linear_Interpolation(info->soca->Rbat,get_OCV_voltage(info,3),
336                         Rbat_low_max, get_OCV_voltage(info,0), Vocv);
337                 RICOH_FG_DBG("PMU: Modify RBAT from %d to %d ", info->soca->Rbat, info->soca->R_low);
338                 temp = info->soca->R_low *4095/5000*512/1000;
339                 
340                 val = info->soca->R_low>>8;
341                 err = ricoh619_write_bank1(info->dev->parent, 0xD4, val);
342                 if (err < 0) {
343                         dev_err(info->dev, "batterry initialize error\n");
344                         return err;
345                 }
346
347                 val = info->soca->R_low & 0xff;
348                 err = ricoh619_write_bank1(info->dev->parent, 0xD5, val);
349                 if (err < 0) {
350                         dev_err(info->dev, "batterry initialize error\n");
351                         return err;
352                 }
353         }
354         else  info->soca->R_low = 0;
355                 
356
357         return err;
358 }
359
360 static int Set_back_ocv_table(struct ricoh619_battery_info *info)
361 {
362         int err;
363         uint8_t val;
364         int temp;
365         int i;
366         uint8_t debug_disp[22];
367
368         /* Modify back ocv table */
369
370         if (0 != info->soca->ocv_table_low[0])
371         {
372                 for (i = 0 ; i < 11; i++){
373                         battery_init_para[info->num][i*2 + 1] = info->soca->ocv_table_low[i];
374                         battery_init_para[info->num][i*2] = info->soca->ocv_table_low[i] >> 8;
375                 }
376                 err = ricoh619_clr_bits(info->dev->parent, FG_CTRL_REG, 0x01);
377
378                 err = ricoh619_bulk_writes_bank1(info->dev->parent,
379                         BAT_INIT_TOP_REG, 22, battery_init_para[info->num]);
380
381                 err = ricoh619_set_bits(info->dev->parent, FG_CTRL_REG, 0x01);
382
383                 /* debug comment start*/
384                 err = ricoh619_bulk_reads_bank1(info->dev->parent,
385                         BAT_INIT_TOP_REG, 22, debug_disp);
386                 for (i = 0; i < 11; i++){
387                         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]));
388                 }
389                 /* end */
390                 /* clear table*/
391                 for(i = 0; i < 11; i++)
392                 {
393                         info->soca->ocv_table_low[i] = 0;
394                 }
395         }
396         
397         /* Modify back Rbat */
398         if (0!=info->soca->R_low)
399         {               
400                 RICOH_FG_DBG("PMU: Modify back RBAT from %d to %d ",  info->soca->R_low,info->soca->Rbat);
401                 temp = info->soca->Rbat*4095/5000*512/1000;
402                 
403                 val = info->soca->R_low>>8;
404                 err = ricoh619_write_bank1(info->dev->parent, 0xD4, val);
405                 if (err < 0) {
406                         dev_err(info->dev, "batterry initialize error\n");
407                         return err;
408                 }
409
410                 val = info->soca->R_low & 0xff;
411                 err = ricoh619_write_bank1(info->dev->parent, 0xD5, val);
412                 if (err < 0) {
413                         dev_err(info->dev, "batterry initialize error\n");
414                         return err;
415                 }
416
417                 info->soca->R_low = 0;
418         }
419         return 0;
420 }
421
422 /**
423 **/
424
425 static int ricoh619_Check_OCV_Offset(struct ricoh619_battery_info *info)
426 {
427         int ocv_table[11]; // HEX value
428         int i;
429         int temp;
430         int ret;
431         uint8_t debug_disp[22];
432         uint8_t val = 0;
433
434         RICOH_FG_DBG("PMU : %s : calc ocv %d get OCV %d\n",__func__,calc_ocv(info),get_OCV_voltage(info, RICOH619_OCV_OFFSET_BOUND));
435
436         /* check adp/usb status */
437         ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &val);
438         if (ret < 0) {
439                 dev_err(info->dev, "Error in reading the control register\n");
440                 return ret;
441         }
442
443         val = (val & 0xC0) >> 6;
444
445         if (val != 0){ /* connect adp or usb */
446                 if (calc_ocv(info) < get_OCV_voltage(info, RICOH619_OCV_OFFSET_BOUND) )
447                 {
448                         if(0 == info->soca->ocv_table_low[0]){
449                                 for (i = 0 ; i < 11; i++){
450                                 ocv_table[i] = (battery_init_para[info->num][i*2]<<8) | (battery_init_para[info->num][i*2+1]);
451                                 RICOH_FG_DBG("PMU : %s : OCV table %d 0x%x\n",__func__,i * 10, ocv_table[i]);
452                                 info->soca->ocv_table_low[i] = ocv_table[i];
453                                 }
454
455                                 for (i = 0 ; i < 11; i++){
456                                         temp = ocv_table[i] * (100 + RICOH619_OCV_OFFSET_RATIO) / 100;
457
458                                         battery_init_para[info->num][i*2 + 1] = temp;
459                                         battery_init_para[info->num][i*2] = temp >> 8;
460                                 }
461                                 ret = ricoh619_clr_bits(info->dev->parent, FG_CTRL_REG, 0x01);
462
463                                 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
464                                         BAT_INIT_TOP_REG, 22, battery_init_para[info->num]);
465
466                                 ret = ricoh619_set_bits(info->dev->parent, FG_CTRL_REG, 0x01);
467
468                                 /* debug comment start*/
469                                 ret = ricoh619_bulk_reads_bank1(info->dev->parent,
470                                         BAT_INIT_TOP_REG, 22, debug_disp);
471                                 for (i = 0; i < 11; i++){
472                                         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]));
473                                 }
474                                 /* end */
475                         }
476                 }
477         }
478         
479         return 0;
480 }
481 #endif
482 static int reset_FG_process(struct ricoh619_battery_info *info)
483 {
484         int err;
485
486         //err = set_Rlow(info);
487         //err = ricoh619_Check_OCV_Offset(info);
488         err = ricoh619_write(info->dev->parent,
489                                          FG_CTRL_REG, 0x51);
490         info->soca->ready_fg = 0;
491         return err;
492 }
493
494
495 static int check_charge_status_2(struct ricoh619_battery_info *info, int displayed_soc_temp)
496 {
497         if (displayed_soc_temp < 0)
498                         displayed_soc_temp = 0;
499         
500         get_power_supply_status(info);
501         info->soca->soc = calc_capacity(info) * 100;
502
503         if (POWER_SUPPLY_STATUS_FULL == info->soca->chg_status) {
504                 if ((info->first_pwon == 1)
505                         && (RICOH619_SOCA_START == info->soca->status)) {
506                                 g_full_flag = 1;
507                                 info->soca->soc_full = info->soca->soc;
508                                 info->soca->displayed_soc = 100*100;
509                                 info->soca->full_reset_count = 0;
510                 } else {
511                         if ( (displayed_soc_temp > 97*100)
512                                 && (calc_ocv(info) > (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*7/10)  )){
513                                 g_full_flag = 1;
514                                 info->soca->soc_full = info->soca->soc;
515                                 info->soca->displayed_soc = 100*100;
516                                 info->soca->full_reset_count = 0;
517                         } else {
518                                 g_full_flag = 0;
519                                 info->soca->displayed_soc = displayed_soc_temp;
520                         }
521
522                 }
523         }
524         if (info->soca->Ibat_ave >= 0) {
525                 if (g_full_flag == 1) {
526                         info->soca->displayed_soc = 100*100;
527                 } else {
528                         if (info->soca->displayed_soc/100 < 99) {
529                                 info->soca->displayed_soc = displayed_soc_temp;
530                         } else {
531                                 info->soca->displayed_soc = 99 * 100;
532                         }
533                 }
534         }
535         if (info->soca->Ibat_ave < 0) {
536                 if (g_full_flag == 1) {
537                         if (calc_ocv(info) < (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*7/10)  ) {
538                                 g_full_flag = 0;
539                                 //info->soca->displayed_soc = 100*100;
540                                 info->soca->displayed_soc = displayed_soc_temp;
541                         } else {
542                                 info->soca->displayed_soc = 100*100;
543                         }
544                 } else {
545                         g_full_flag = 0;
546                         info->soca->displayed_soc = displayed_soc_temp;
547                 }
548         }
549
550         return info->soca->displayed_soc;
551 }
552
553 /**
554 * Calculate Capacity in a period
555 * - read CC_SUM & FA_CAP from Coulom Counter
556 * -  and calculate Capacity.
557 * @cc_cap: capacity in a period, unit 0.01%
558 * @is_charging: Flag of charging current direction
559 *               TRUE : charging (plus)
560 *               FALSE: discharging (minus)
561 * @cc_rst: reset CC_SUM or not
562 *               TRUE : reset
563 *               FALSE: not reset
564 **/
565 static int calc_capacity_in_period(struct ricoh619_battery_info *info,
566                                  int *cc_cap, bool *is_charging, bool cc_rst)
567 {
568         int err;
569         uint8_t         cc_sum_reg[4];
570         uint8_t         cc_clr[4] = {0, 0, 0, 0};
571         uint8_t         fa_cap_reg[2];
572         uint16_t        fa_cap;
573         uint32_t        cc_sum;
574         int             cc_stop_flag;
575         uint8_t         status;
576         uint8_t         charge_state;
577         int             Ocv;
578         uint32_t        cc_cap_temp;
579         uint32_t        cc_cap_min;
580         int             cc_cap_res;
581
582         *is_charging = true;    /* currrent state initialize -> charging */
583
584         if (info->entry_factory_mode)
585                 return 0;
586
587         //check need charge stop or not
588         /* get  power supply status */
589         err = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
590         if (err < 0)
591                 goto out;
592         charge_state = (status & 0x1F);
593         Ocv = calc_ocv(info);
594         if (charge_state == CHG_STATE_CHG_COMPLETE) {
595                 /* Check CHG status is complete or not */
596                 cc_stop_flag = 0;
597         } else if (calc_capacity(info) == 100) {
598                 /* Check HW soc is 100 or not */
599                 cc_stop_flag = 0;
600         } else if (Ocv/1000 < get_OCV_voltage(info, 9)) {
601                 /* Check VBAT is high level or not */
602                 cc_stop_flag = 0;
603         } else {
604                 cc_stop_flag = 1;
605         }
606
607         if (cc_stop_flag == 1)
608         {
609                 /* Disable Charging/Completion Interrupt */
610                 err = ricoh619_set_bits(info->dev->parent,
611                                                 RICOH619_INT_MSK_CHGSTS1, 0x01);
612                 if (err < 0)
613                         goto out;
614
615                 /* disable charging */
616                 err = ricoh619_clr_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x03);
617                 if (err < 0)
618                         goto out;
619         }
620
621         /* CC_pause enter */
622         err = ricoh619_write(info->dev->parent, CC_CTRL_REG, 0x01);
623         if (err < 0)
624                 goto out;
625
626         /* Read CC_SUM */
627         err = ricoh619_bulk_reads(info->dev->parent,
628                                         CC_SUMREG3_REG, 4, cc_sum_reg);
629         if (err < 0)
630                 goto out;
631
632         if (cc_rst == true) {
633                 /* CC_SUM <- 0 */
634                 err = ricoh619_bulk_writes(info->dev->parent,
635                                                 CC_SUMREG3_REG, 4, cc_clr);
636                 if (err < 0)
637                         goto out;
638         }
639
640         /* CC_pause exist */
641         err = ricoh619_write(info->dev->parent, CC_CTRL_REG, 0);
642         if (err < 0)
643                 goto out;
644         if (cc_stop_flag == 1)
645         {
646         
647                 /* Enable charging */
648                 err = ricoh619_set_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x03);
649                 if (err < 0)
650                         goto out;
651
652                 udelay(1000);
653
654                 /* Clear Charging Interrupt status */
655                 err = ricoh619_clr_bits(info->dev->parent,
656                                         RICOH619_INT_IR_CHGSTS1, 0x01);
657                 if (err < 0)
658                         goto out;
659
660                 /* ricoh619_read(info->dev->parent, RICOH619_INT_IR_CHGSTS1, &val);
661                 RICOH_FG_DBG("INT_IR_CHGSTS1 = 0x%x\n",val); */
662
663                 /* Enable Charging Interrupt */
664                 err = ricoh619_clr_bits(info->dev->parent,
665                                                 RICOH619_INT_MSK_CHGSTS1, 0x01);
666                 if (err < 0)
667                         goto out;
668         }
669         /* Read FA_CAP */
670         err = ricoh619_bulk_reads(info->dev->parent,
671                                  FA_CAP_H_REG, 2, fa_cap_reg);
672         if (err < 0)
673                 goto out;
674
675         /* fa_cap = *(uint16_t*)fa_cap_reg & 0x7fff; */
676         fa_cap = (fa_cap_reg[0] << 8 | fa_cap_reg[1]) & 0x7fff;
677
678         /* cc_sum = *(uint32_t*)cc_sum_reg; */
679         cc_sum = cc_sum_reg[0] << 24 | cc_sum_reg[1] << 16 |
680                                 cc_sum_reg[2] << 8 | cc_sum_reg[3];
681
682         /* calculation  two's complement of CC_SUM */
683         if (cc_sum & 0x80000000) {
684                 cc_sum = (cc_sum^0xffffffff)+0x01;
685                 *is_charging = false;           /* discharge */
686         }
687         /* (CC_SUM x 10000)/3600/FA_CAP */
688         *cc_cap = cc_sum*25/9/fa_cap;           /* unit is 0.01% */
689
690         //////////////////////////////////////////////////////////////////      
691         cc_cap_min = fa_cap*3600/100/100/100;   /* Unit is 0.0001% */
692         cc_cap_temp = cc_sum / cc_cap_min;
693                 
694         cc_cap_res = cc_cap_temp % 100;
695         
696         RICOH_FG_DBG("PMU: cc_sum = %d: cc_cap_res= %d: \n", cc_sum, cc_cap_res);
697         
698         
699         if(*is_charging) {
700                 info->soca->cc_cap_offset += cc_cap_res;
701                 if (info->soca->cc_cap_offset >= 100) {
702                         *cc_cap += 1;
703                         info->soca->cc_cap_offset %= 100;
704                 }
705         } else {
706                 info->soca->cc_cap_offset -= cc_cap_res;
707                 if (info->soca->cc_cap_offset <= -100) {
708                         *cc_cap += 1;
709                         info->soca->cc_cap_offset %= 100;
710                 }
711         }
712         RICOH_FG_DBG("PMU: cc_cap_offset= %d: \n", info->soca->cc_cap_offset);
713         
714         //////////////////////////////////////////////////////////////////
715         return 0;
716 out:
717         dev_err(info->dev, "Error !!-----\n");
718         return err;
719 }
720 /**
721 * Calculate target using capacity
722 **/
723 static int get_target_use_cap(struct ricoh619_battery_info *info)
724 {
725         int i,j;
726         int ocv_table[11];
727         int temp;
728 //      int Target_Cutoff_Vol = 0;
729         int Ocv_ZeroPer_now;
730         int Ibat_now;
731         int fa_cap,use_cap;
732         int FA_CAP_now;
733         int start_per = 0;
734         int RE_CAP_now;
735         int CC_OnePer_step;
736         int Ibat_min;
737
738 //      int Ocv_now;
739         int Ocv_now_table;
740 //      int soc_per;
741 //      int use_cap_now;
742         int Rsys_now;
743
744         /* get const value */
745         Ibat_min = -1 * info->soca->target_ibat;
746         if (info->soca->Ibat_ave > Ibat_min) /* I bat is minus */
747         {
748                 Ibat_now = Ibat_min;
749         } else {
750                 Ibat_now = info->soca->Ibat_ave;
751         }
752         fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
753                                                                 0x7fff);
754         use_cap = fa_cap - info->soca->re_cap_old;
755
756         /* get OCV table % */
757         for (i = 0; i <= 10; i = i+1) {
758                 temp = (battery_init_para[info->num][i*2]<<8)
759                          | (battery_init_para[info->num][i*2+1]);
760                 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
761                 temp = ((temp * 50000 * 10 / 4095) + 5) / 10;
762                 ocv_table[i] = temp;
763                 RICOH_FG_DBG("PMU : %s : ocv_table %d is %d v\n",__func__, i, ocv_table[i]);
764         }
765
766         /* Find out Current OCV */
767         i = info->soca->soc/1000;
768         j = info->soca->soc - info->soca->soc/1000*1000;
769         Ocv_now_table = ocv_table[i]*100+(ocv_table[i+1]-ocv_table[i])*j/10;
770
771         Rsys_now = (info->soca->Vsys_ave - Ocv_now_table) / info->soca->Ibat_ave;
772         if (((abs(info->soca->soc - info->soca->displayed_soc)) > 10)
773                 && (info->soca->Ibat_ave > -250)) {
774                 if (Rsys_now < 0)
775                         Rsys_now = max(-info->soca->Rbat, Rsys_now);
776                 else
777                         Rsys_now = min(info->soca->Rbat, Rsys_now);
778         }
779
780         Ocv_ZeroPer_now = info->soca->target_vsys * 1000 - Ibat_now * Rsys_now;
781
782         RICOH_FG_DBG("PMU: -------  Ocv_now_table= %d: Rsys_now= %d =======\n",
783                Ocv_now_table, Rsys_now);
784
785         RICOH_FG_DBG("PMU: -------  Rsys= %d: cutoff_ocv= %d: Ocv_ZeroPer_now= %d =======\n",
786                info->soca->Rsys, info->soca->cutoff_ocv, Ocv_ZeroPer_now);
787
788         /* get FA_CAP_now */
789
790         
791         for (i = 1; i < 11; i++) {
792                 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));
793                 if (ocv_table[i] >= Ocv_ZeroPer_now / 100) {
794                         /* unit is 0.001% */
795                         start_per = Calc_Linear_Interpolation(
796                                 (i-1)*1000, ocv_table[i-1], i*1000,
797                                  ocv_table[i], (Ocv_ZeroPer_now / 100));
798                         i = 11;
799                 }
800         }
801
802         start_per = max(0, start_per);
803
804         FA_CAP_now = fa_cap * ((10000 - start_per) / 100 ) / 100;
805
806         RICOH_FG_DBG("PMU: -------Ocv_ZeroPer_now= %d: start_per= %d =======\n",
807                 Ocv_ZeroPer_now, start_per);
808
809         /* get RE_CAP_now */
810         RE_CAP_now = FA_CAP_now - use_cap;
811         
812         if (RE_CAP_now < RE_CAP_GO_DOWN) {
813                 info->soca->hurry_up_flg = 1;
814         } else if (info->soca->Vsys_ave < info->soca->target_vsys*1000) {
815                 info->soca->hurry_up_flg = 1;
816         } else if (info->fg_poff_vbat != 0) {
817                 if (info->soca->Vbat_ave < info->fg_poff_vbat*1000) {
818                         info->soca->hurry_up_flg = 1;
819                 } else {
820                         info->soca->hurry_up_flg = 0;
821                 }
822         } else {
823                 info->soca->hurry_up_flg = 0;
824         }
825
826         /* get CC_OnePer_step */
827         if (info->soca->displayed_soc > 0) { /* avoid divide-by-0 */
828                 CC_OnePer_step = RE_CAP_now / (info->soca->displayed_soc / 100 + 1);
829         } else {
830                 CC_OnePer_step = 0;
831         }
832         /* get info->soca->target_use_cap */
833         info->soca->target_use_cap = use_cap + CC_OnePer_step;
834         
835         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",
836                FA_CAP_now, RE_CAP_now, CC_OnePer_step, info->soca->target_use_cap, info->soca->hurry_up_flg);
837         
838         return 0;
839 }
840 #ifdef ENABLE_OCV_TABLE_CALIB
841 /**
842 * Calibration OCV Table
843 * - Update the value of VBAT on 100% in OCV table 
844 *    if battery is Full charged.
845 * - int vbat_ocv <- unit is uV
846 **/
847 static int calib_ocvTable(struct ricoh619_battery_info *info, int vbat_ocv)
848 {
849         int ret;
850         int cutoff_ocv;
851         int i;
852         int ocv100_new;
853         int start_per = 0;
854         int end_per = 0;
855         
856         RICOH_FG_DBG("PMU: %s\n", __func__);
857         
858         if (info->soca->Ibat_ave > RICOH619_REL1_SEL_VALUE) {
859                 RICOH_FG_DBG("PMU: %s IBAT > 64mA -- Not Calibration --\n", __func__);
860                 return 0;
861         }
862         
863         if (vbat_ocv < info->soca->OCV100_max) {
864                 if (vbat_ocv < info->soca->OCV100_min)
865                         ocv100_new = info->soca->OCV100_min;
866                 else
867                         ocv100_new = vbat_ocv;
868         } else {
869                 ocv100_new = info->soca->OCV100_max;
870         }
871         RICOH_FG_DBG("PMU : %s :max %d min %d current %d\n",__func__,info->soca->OCV100_max,info->soca->OCV100_min,vbat_ocv);
872         RICOH_FG_DBG("PMU : %s : New OCV 100% = 0x%x\n",__func__,ocv100_new);
873         
874         /* FG_En Off */
875         ret = ricoh619_clr_bits(info->dev->parent, FG_CTRL_REG, 0x01);
876         if (ret < 0) {
877                 dev_err("PMU: %s Error in FG_En OFF\n", __func__);
878                 goto err;
879         }
880
881
882         //cutoff_ocv = (battery_init_para[info->num][0]<<8) | (battery_init_para[info->num][1]);
883         cutoff_ocv = get_OCV_voltage(info, 0);
884
885         info->soca->ocv_table_def[10] = info->soca->OCV100_max;
886
887         ricoh619_scaling_OCV_table(info, cutoff_ocv/1000, ocv100_new/1000, &start_per, &end_per);
888
889         ret = ricoh619_bulk_writes_bank1(info->dev->parent,
890                                 BAT_INIT_TOP_REG, 22, battery_init_para[info->num]);
891         if (ret < 0) {
892                 dev_err(info->dev, "batterry initialize error\n");
893                 goto err;
894         }
895
896         for (i = 0; i <= 10; i = i+1) {
897                 info->soca->ocv_table[i] = get_OCV_voltage(info, i);
898                 RICOH_FG_DBG("PMU: %s : * %d0%% voltage = %d uV\n",
899                                  __func__, i, info->soca->ocv_table[i]);
900         }
901         
902         /* FG_En on & Reset*/
903         ret = reset_FG_process(info);
904         if (ret < 0) {
905                 dev_err("PMU: %s Error in FG_En On & Reset\n", __func__);
906                 goto err;
907         }
908
909         RICOH_FG_DBG("PMU: %s Exit \n", __func__);
910         return 0;
911 err:
912         return ret;
913
914 }
915 #endif
916
917 static void ricoh619_displayed_work(struct work_struct *work)
918 {
919         int err;
920         uint8_t val;
921         uint8_t val2;
922         int soc_round;
923         int last_soc_round;
924         int last_disp_round;
925         int displayed_soc_temp;
926         int disp_dec;
927         int cc_cap = 0;
928         bool is_charging = true;
929         int re_cap,fa_cap,use_cap;
930         bool is_jeita_updated;
931         uint8_t reg_val;
932         int delay_flag = 0;
933         int Vbat = 0;
934         int Ibat = 0;
935         int Vsys = 0;
936         int temp_ocv;
937         int fc_delta = 0;
938
939         struct ricoh619_battery_info *info = container_of(work,
940         struct ricoh619_battery_info, displayed_work.work);
941
942         if (info->entry_factory_mode) {
943                 info->soca->status = RICOH619_SOCA_STABLE;
944                 info->soca->displayed_soc = -EINVAL;
945                 info->soca->ready_fg = 0;
946                 return;
947         }
948
949         mutex_lock(&info->lock);
950         
951         is_jeita_updated = false;
952
953         if ((RICOH619_SOCA_START == info->soca->status)
954                  || (RICOH619_SOCA_STABLE == info->soca->status)
955                  || (RICOH619_SOCA_FULL == info->soca->status))
956                 info->soca->ready_fg = 1;
957
958         /* judge Full state or Moni Vsys state */
959         if ((RICOH619_SOCA_DISP == info->soca->status)
960                  || (RICOH619_SOCA_STABLE == info->soca->status))
961         {
962                 /* caluc 95% ocv */
963                 temp_ocv = get_OCV_voltage(info, 10) -
964                                         (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))/2;
965                 
966                 if(g_full_flag == 1){   /* for issue 1 solution start*/
967                         info->soca->status = RICOH619_SOCA_FULL;
968                 }else if ((POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
969                         && (calc_ocv(info) > temp_ocv)) {
970                         info->soca->status = RICOH619_SOCA_FULL;
971                         g_full_flag = 0;
972                 } else if (info->soca->Ibat_ave >= -20) {
973                         /* for issue1 solution end */
974                         /* check Full state or not*/
975                         if ((calc_ocv(info) > (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*7/10))
976                                 || (POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
977                                 || (info->soca->displayed_soc > 9850))
978                         {
979                                 info->soca->status = RICOH619_SOCA_FULL;
980                                 g_full_flag = 0;
981                         } else if ((calc_ocv(info) > (get_OCV_voltage(info, 9)))
982                                 && (info->soca->Ibat_ave < 300))
983                         {
984                                 info->soca->status = RICOH619_SOCA_FULL;
985                                 g_full_flag = 0;
986                         }
987                 } else { /* dis-charging */
988                         if (info->soca->displayed_soc/100 < RICOH619_ENTER_LOW_VOL) {
989                                 info->soca->target_use_cap = 0;
990                                 info->soca->status = RICOH619_SOCA_LOW_VOL;
991                         }
992                 }
993         }
994
995         if (RICOH619_SOCA_STABLE == info->soca->status) {
996                 info->soca->soc = calc_capacity_2(info);
997                 info->soca->soc_delta = info->soca->soc - info->soca->last_soc;
998
999                 if (info->soca->soc_delta >= -100 && info->soca->soc_delta <= 100) {
1000                         info->soca->displayed_soc = info->soca->soc;
1001                 } else {
1002                         info->soca->status = RICOH619_SOCA_DISP;
1003                 }
1004                 info->soca->last_soc = info->soca->soc;
1005                 info->soca->soc_delta = 0;
1006         } else if (RICOH619_SOCA_FULL == info->soca->status) {
1007                 err = check_jeita_status(info, &is_jeita_updated);
1008                 if (err < 0) {
1009                         dev_err(info->dev, "Error in updating JEITA %d\n", err);
1010                         goto end_flow;
1011                 }
1012                 info->soca->soc = calc_capacity(info) * 100;
1013                 info->soca->last_soc = calc_capacity_2(info);   /* for DISP */
1014
1015                 if (info->soca->Ibat_ave >= -20) { /* charging */
1016                         if (0 == info->soca->jt_limit) {
1017                                 if (g_full_flag == 1) {
1018                                         
1019                                         if (POWER_SUPPLY_STATUS_FULL == info->soca->chg_status) {
1020                                                 if(info->soca->full_reset_count < RICOH619_UPDATE_COUNT_FULL_RESET) {
1021                                                         info->soca->full_reset_count++;
1022                                                 } else if (info->soca->full_reset_count < (RICOH619_UPDATE_COUNT_FULL_RESET + 1)) {
1023                                                         err = reset_FG_process(info);
1024                                                         if (err < 0)
1025                                                                 dev_err(info->dev, "Error in writing the control register\n");
1026                                                         info->soca->full_reset_count++;
1027                                                         goto end_flow;
1028                                                 } else if(info->soca->full_reset_count < (RICOH619_UPDATE_COUNT_FULL_RESET + 2)) {
1029                                                         info->soca->full_reset_count++;
1030                                                         info->soca->fc_cap = 0;
1031                                                         info->soca->soc_full = info->soca->soc;
1032                                                 }
1033                                         } else {
1034                                                 if(info->soca->fc_cap < -1 * 200) {
1035                                                         g_full_flag = 0;
1036                                                         info->soca->displayed_soc = 99 * 100;
1037                                                 }
1038                                                 info->soca->full_reset_count = 0;
1039                                         }
1040                                         
1041
1042                                         info->soca->chg_cmp_times = 0;
1043                                         err = calc_capacity_in_period(info, &cc_cap, &is_charging, true);
1044                                         if (err < 0)
1045                                         dev_err(info->dev, "Read cc_sum Error !!-----\n");
1046
1047                                         fc_delta = (is_charging == true) ? cc_cap : -cc_cap;
1048
1049                                         info->soca->fc_cap = info->soca->fc_cap + fc_delta;
1050
1051                                         if (g_full_flag == 1){
1052                                                 info->soca->displayed_soc = 100*100;
1053                                         }
1054                                 } else {
1055                                         if (calc_ocv(info) < (get_OCV_voltage(info, 8))) { /* fail safe*/
1056                                                 g_full_flag = 0;
1057                                                 info->soca->status = RICOH619_SOCA_DISP;
1058                                                 info->soca->soc_delta = 0;
1059                                         } else if ((POWER_SUPPLY_STATUS_FULL == info->soca->chg_status) 
1060                                                 && (info->soca->displayed_soc >= 9890)){
1061                                                 if(info->soca->chg_cmp_times > RICOH619_FULL_WAIT_TIME) {
1062                                                         info->soca->displayed_soc = 100*100;
1063                                                         g_full_flag = 1;
1064                                                         info->soca->full_reset_count = 0;
1065                                                         info->soca->soc_full = info->soca->soc;
1066                                                         info->soca->fc_cap = 0;
1067 #ifdef ENABLE_OCV_TABLE_CALIB
1068                                                         err = calib_ocvTable(info,calc_ocv(info));
1069                                                         if (err < 0)
1070                                                                 dev_err(info->dev, "Calibration OCV Error !!\n");
1071 #endif
1072                                                 } else {
1073                                                         info->soca->chg_cmp_times++;
1074                                                 }
1075                                         } else {
1076                                                 fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
1077                                                         0x7fff);
1078                                                 
1079                                                 if (info->soca->displayed_soc >= 9950) {
1080                                                         if((info->soca->soc_full - info->soca->soc) < 200) {
1081                                                                 goto end_flow;
1082                                                         }
1083                                                 }
1084                                                 info->soca->chg_cmp_times = 0;
1085
1086                                                 err = calc_capacity_in_period(info, &cc_cap, &is_charging, true);
1087                                                 if (err < 0)
1088                                                 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1089                                                 info->soca->cc_delta
1090                                                          = (is_charging == true) ? cc_cap : -cc_cap;
1091
1092                                                 if((POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
1093                                                 //      || (info->soca->Ibat_ave > 200))
1094                                                 || (info->soca->Ibat_ave < info->ch_icchg*50 + 100) || (info->soca->displayed_soc<9700))
1095                                                 {
1096                                                         info->soca->displayed_soc += 13 * 3000 / fa_cap;
1097                                                 }
1098                                                 else {
1099                                                         info->soca->displayed_soc
1100                                                        = info->soca->displayed_soc + info->soca->cc_delta*8/10;
1101                                                 }
1102                                                 
1103                                                 info->soca->displayed_soc
1104                                                          = min(10000, info->soca->displayed_soc);
1105                                                 info->soca->displayed_soc = max(0, info->soca->displayed_soc);
1106
1107                                                 if (info->soca->displayed_soc >= 9890) {
1108                                                         info->soca->displayed_soc = 99 * 100;
1109                                                 }
1110                                         }
1111                                 }
1112                         } else {
1113                                 info->soca->full_reset_count = 0;
1114                         }
1115                 } else { /* discharging */
1116                         if (info->soca->displayed_soc >= 9950) {
1117                                 if (info->soca->Ibat_ave <= -1 * RICOH619_REL1_SEL_VALUE) {
1118                                         if ((calc_ocv(info) < (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*3/10))
1119                                                 || ((info->soca->soc_full - info->soca->soc) > 200)) {
1120
1121                                                 g_full_flag = 0;
1122                                                 info->soca->full_reset_count = 0;
1123                                                 info->soca->displayed_soc = 100 * 100;
1124                                                 info->soca->status = RICOH619_SOCA_DISP;
1125                                                 info->soca->last_soc = info->soca->soc;
1126                                                 info->soca->soc_delta = 0;
1127                                         } else {
1128                                                 info->soca->displayed_soc = 100 * 100;
1129                                         }
1130                                 } else { /* into relaxation state */
1131                                         ricoh619_read(info->dev->parent, CHGSTATE_REG, &reg_val);
1132                                         if (reg_val & 0xc0) {
1133                                                 info->soca->displayed_soc = 100 * 100;
1134                                         } else {
1135                                                 g_full_flag = 0;
1136                                                 info->soca->full_reset_count = 0;
1137                                                 info->soca->displayed_soc = 100 * 100;
1138                                                 info->soca->status = RICOH619_SOCA_DISP;
1139                                                 info->soca->last_soc = info->soca->soc;
1140                                                 info->soca->soc_delta = 0;
1141                                         }
1142                                 }
1143                         } else {
1144                                 g_full_flag = 0;
1145                                 info->soca->status = RICOH619_SOCA_DISP;
1146                                 info->soca->soc_delta = 0;
1147                                 info->soca->full_reset_count = 0;
1148                                 info->soca->last_soc = info->soca->soc;
1149                         }
1150                 }
1151         } else if (RICOH619_SOCA_LOW_VOL == info->soca->status) {
1152                 if(info->soca->Ibat_ave >= 0) {
1153                         info->soca->soc = calc_capacity(info) * 100;
1154                         info->soca->status = RICOH619_SOCA_DISP;
1155                         info->soca->last_soc = info->soca->soc;
1156                         info->soca->soc_delta = 0;
1157                 } else {
1158                         re_cap = get_check_fuel_gauge_reg(info, RE_CAP_H_REG, RE_CAP_L_REG,
1159                                                                 0x7fff);
1160                         fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
1161                                                                 0x7fff);
1162                         use_cap = fa_cap - re_cap;
1163                         
1164                         if (info->soca->target_use_cap == 0) {
1165                                 info->soca->re_cap_old = re_cap;
1166                                 get_target_use_cap(info);
1167                         }
1168                         
1169                         if(use_cap >= info->soca->target_use_cap) {
1170                                 info->soca->displayed_soc = info->soca->displayed_soc - 100;
1171                                 info->soca->displayed_soc = max(0, info->soca->displayed_soc);
1172                                 info->soca->re_cap_old = re_cap;
1173                         } else if (info->soca->hurry_up_flg == 1) {
1174                                 info->soca->displayed_soc = info->soca->displayed_soc - 100;
1175                                 info->soca->displayed_soc = max(0, info->soca->displayed_soc);
1176                                 info->soca->re_cap_old = re_cap;
1177                         }
1178                         get_target_use_cap(info);
1179                         info->soca->soc = calc_capacity(info) * 100;
1180                 }
1181         }
1182         else if (RICOH619_SOCA_DISP == info->soca->status) {
1183
1184                 info->soca->soc = calc_capacity_2(info);
1185
1186                 soc_round = (info->soca->soc + 50) / 100;
1187                 last_soc_round = (info->soca->last_soc + 50) / 100;
1188                 last_disp_round = (info->soca->displayed_soc + 50) / 100;
1189
1190                 info->soca->soc_delta =
1191                         info->soca->soc_delta + (info->soca->soc - info->soca->last_soc);
1192
1193                 info->soca->last_soc = info->soca->soc;
1194                 /* six case */
1195                 if (last_disp_round == soc_round) {
1196                         /* if SOC == DISPLAY move to stable */
1197                         info->soca->displayed_soc = info->soca->soc ;
1198                         info->soca->status = RICOH619_SOCA_STABLE;
1199                         delay_flag = 1;
1200                 } else if (info->soca->Ibat_ave > 0) {
1201                         if ((0 == info->soca->jt_limit) || 
1202                         (POWER_SUPPLY_STATUS_FULL != info->soca->chg_status)) {
1203                                 /* Charge */
1204                                 if (last_disp_round < soc_round) {
1205                                         /* Case 1 : Charge, Display < SOC */
1206                                         if (info->soca->soc_delta >= 100) {
1207                                                 info->soca->displayed_soc
1208                                                         = last_disp_round * 100 + 50;
1209                                                 info->soca->soc_delta -= 100;
1210                                                 if (info->soca->soc_delta >= 100)
1211                                                         delay_flag = 1;
1212                                         } else {
1213                                                 info->soca->displayed_soc += 25;
1214                                                 disp_dec = info->soca->displayed_soc % 100;
1215                                                 if ((50 <= disp_dec) && (disp_dec <= 74))
1216                                                         info->soca->soc_delta = 0;
1217                                         }
1218                                         if ((info->soca->displayed_soc + 50)/100
1219                                                                  >= soc_round) {
1220                                                 info->soca->displayed_soc
1221                                                         = info->soca->soc ;
1222                                                 info->soca->status
1223                                                         = RICOH619_SOCA_STABLE;
1224                                                 delay_flag = 1;
1225                                         }
1226                                 } else if (last_disp_round > soc_round) {
1227                                         /* Case 2 : Charge, Display > SOC */
1228                                         if (info->soca->soc_delta >= 300) {
1229                                                 info->soca->displayed_soc += 100;
1230                                                 info->soca->soc_delta -= 300;
1231                                         }
1232                                         if ((info->soca->displayed_soc + 50)/100
1233                                                                  <= soc_round) {
1234                                                 info->soca->displayed_soc
1235                                                         = info->soca->soc ;
1236                                                 info->soca->status
1237                                                 = RICOH619_SOCA_STABLE;
1238                                                 delay_flag = 1;
1239                                         }
1240                                 }
1241                         } else {
1242                                 info->soca->soc_delta = 0;
1243                         }
1244                 } else {
1245                         /* Dis-Charge */
1246                         if (last_disp_round > soc_round) {
1247                                 /* Case 3 : Dis-Charge, Display > SOC */
1248                                 if (info->soca->soc_delta <= -100) {
1249                                         info->soca->displayed_soc
1250                                                 = last_disp_round * 100 - 75;
1251                                         info->soca->soc_delta += 100;
1252                                         if (info->soca->soc_delta <= -100)
1253                                                 delay_flag = 1;
1254                                 } else {
1255                                         info->soca->displayed_soc -= 25;
1256                                         disp_dec = info->soca->displayed_soc % 100;
1257                                         if ((25 <= disp_dec) && (disp_dec <= 49))
1258                                                 info->soca->soc_delta = 0;
1259                                 }
1260                                 if ((info->soca->displayed_soc + 50)/100
1261                                                          <= soc_round) {
1262                                         info->soca->displayed_soc
1263                                                 = info->soca->soc ;
1264                                         info->soca->status
1265                                                 = RICOH619_SOCA_STABLE;
1266                                         delay_flag = 1;
1267                                 }
1268                         } else if (last_disp_round < soc_round) {
1269                                 /* Case 4 : Dis-Charge, Display < SOC */
1270                                 if (info->soca->soc_delta <= -300) {
1271                                         info->soca->displayed_soc -= 100;
1272                                         info->soca->soc_delta += 300;
1273                                 }
1274                                 if ((info->soca->displayed_soc + 50)/100
1275                                                          >= soc_round) {
1276                                         info->soca->displayed_soc
1277                                                 = info->soca->soc ;
1278                                         info->soca->status
1279                                                 = RICOH619_SOCA_STABLE;
1280                                         delay_flag = 1;
1281                                 }
1282                         }
1283                 }
1284         } else if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
1285                 /* caluc 95% ocv */
1286                 temp_ocv = get_OCV_voltage(info, 10) -
1287                                         (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))/2;
1288                 
1289                 if(g_full_flag == 1){   /* for issue 1 solution start*/
1290                         info->soca->status = RICOH619_SOCA_FULL;
1291                         err = reset_FG_process(info);
1292                         if (err < 0)
1293                                 dev_err(info->dev, "Error in writing the control register\n");
1294                         
1295                         goto end_flow;
1296                 }else if ((POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
1297                         && (calc_ocv(info) > temp_ocv)) {
1298                         info->soca->status = RICOH619_SOCA_FULL;
1299                         g_full_flag = 0;
1300                         err = reset_FG_process(info);
1301                         if (err < 0)
1302                                 dev_err(info->dev, "Error in writing the control register\n");
1303                         goto end_flow;
1304                 } else if (info->soca->Ibat_ave >= -20) {
1305                         /* for issue1 solution end */
1306                         /* check Full state or not*/
1307                         if ((calc_ocv(info) > (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*7/10))
1308                                 || (POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
1309                                 || (info->soca->displayed_soc > 9850))
1310                         {
1311                                 info->soca->status = RICOH619_SOCA_FULL;
1312                                 g_full_flag = 0;
1313                                 err = reset_FG_process(info);
1314                                 if (err < 0)
1315                                         dev_err(info->dev, "Error in writing the control register\n");
1316                                 goto end_flow;
1317                         } else if ((calc_ocv(info) > (get_OCV_voltage(info, 9)))
1318                                 && (info->soca->Ibat_ave < 300))
1319                         {
1320                                 info->soca->status = RICOH619_SOCA_FULL;
1321                                 g_full_flag = 0;
1322                                 err = reset_FG_process(info);
1323                                 if (err < 0)
1324                                         dev_err(info->dev, "Error in writing the control register\n");                          
1325                                 goto end_flow;
1326                         }
1327                 }
1328
1329                 err = ricoh619_read(info->dev->parent, PSWR_REG, &val);
1330                 val &= 0x7f;
1331                 info->soca->soc = val * 100;
1332                 if (err < 0) {
1333                         dev_err(info->dev,
1334                                  "Error in reading PSWR_REG %d\n", err);
1335                         info->soca->soc
1336                                  = calc_capacity(info) * 100;
1337                 }
1338
1339                 err = calc_capacity_in_period(info, &cc_cap,
1340                                                  &is_charging, false);
1341                 if (err < 0)
1342                         dev_err(info->dev, "Read cc_sum Error !!-----\n");
1343
1344                 info->soca->cc_delta
1345                          = (is_charging == true) ? cc_cap : -cc_cap;
1346
1347                 displayed_soc_temp
1348                        = info->soca->soc + info->soca->cc_delta;
1349                 if (displayed_soc_temp < 0)
1350                         displayed_soc_temp = 0;
1351                 displayed_soc_temp
1352                          = min(9850, displayed_soc_temp);
1353                 displayed_soc_temp = max(0, displayed_soc_temp);
1354
1355                 info->soca->displayed_soc = displayed_soc_temp;
1356
1357         } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
1358                 /* No update */
1359         } else if (RICOH619_SOCA_START == info->soca->status) {
1360
1361                 err = measure_Ibatt_FG(info, &Ibat);
1362                 err = measure_vbatt_FG(info, &Vbat);
1363                 err = measure_vsys_ADC(info, &Vsys);
1364
1365                 info->soca->Ibat_ave = Ibat;
1366                 info->soca->Vbat_ave = Vbat;
1367                 info->soca->Vsys_ave = Vsys;
1368
1369                 err = check_jeita_status(info, &is_jeita_updated);
1370                 is_jeita_updated = false;
1371                 if (err < 0) {
1372                         dev_err(info->dev, "Error in updating JEITA %d\n", err);
1373                 }
1374                 err = ricoh619_read(info->dev->parent, PSWR_REG, &val);
1375                 val &= 0x7f;
1376                 if (info->first_pwon) {
1377                         info->soca->soc = calc_capacity(info) * 100;
1378                         val = (info->soca->soc + 50)/100;
1379                         val &= 0x7f;
1380                         err = ricoh619_write(info->dev->parent, PSWR_REG, val);
1381                         if (err < 0)
1382                                 dev_err(info->dev, "Error in writing PSWR_REG\n");
1383                         g_soc = val;
1384
1385                         if ((info->soca->soc == 0) && (calc_ocv(info)
1386                                         < get_OCV_voltage(info, 0))) {
1387                                 info->soca->displayed_soc = 0;
1388                                 info->soca->status = RICOH619_SOCA_ZERO;
1389                         } else {
1390                                 if (0 == info->soca->jt_limit) {
1391                                         check_charge_status_2(info, info->soca->soc);
1392                                 } else {
1393                                         info->soca->displayed_soc = info->soca->soc;
1394                                 }
1395                                 if (Ibat < 0) {
1396                                         if (info->soca->displayed_soc < 300) {
1397                                                 info->soca->target_use_cap = 0;
1398                                                 info->soca->status = RICOH619_SOCA_LOW_VOL;
1399                                         } else {
1400                                                 if ((info->fg_poff_vbat != 0)
1401                                                       && (Vbat < info->fg_poff_vbat * 1000) ){
1402                                                           info->soca->target_use_cap = 0;
1403                                                           info->soca->status = RICOH619_SOCA_LOW_VOL;
1404                                                   } else { 
1405                                                           info->soca->status = RICOH619_SOCA_UNSTABLE;
1406                                                   }
1407                                         }
1408                                 } else {
1409                                         info->soca->status = RICOH619_SOCA_UNSTABLE;
1410                                 }
1411                         }
1412                 } else if (g_fg_on_mode && (val == 0x7f)) {
1413                         info->soca->soc = calc_capacity(info) * 100;
1414                         if ((info->soca->soc == 0) && (calc_ocv(info)
1415                                         < get_OCV_voltage(info, 0))) {
1416                                 info->soca->displayed_soc = 0;
1417                                 info->soca->status = RICOH619_SOCA_ZERO;
1418                         } else {
1419                                 if (0 == info->soca->jt_limit) {
1420                                         check_charge_status_2(info, info->soca->soc);
1421                                 } else {
1422                                         info->soca->displayed_soc = info->soca->soc;
1423                                 }
1424                                 info->soca->last_soc = info->soca->soc;
1425                                 info->soca->status = RICOH619_SOCA_STABLE;
1426                         }
1427                 } else {
1428                         info->soca->soc = val * 100;
1429                         if (err < 0) {
1430                                 dev_err(info->dev,
1431                                          "Error in reading PSWR_REG %d\n", err);
1432                                 info->soca->soc
1433                                          = calc_capacity(info) * 100;
1434                         }
1435
1436                         err = calc_capacity_in_period(info, &cc_cap,
1437                                                          &is_charging, false);
1438                         if (err < 0)
1439                                 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1440
1441                         info->soca->cc_delta
1442                                  = (is_charging == true) ? cc_cap : -cc_cap;
1443                         if (calc_ocv(info) < get_OCV_voltage(info, 0)) {
1444                                 info->soca->displayed_soc = 0;
1445                                 info->soca->status = RICOH619_SOCA_ZERO;
1446                         } else {
1447                                 displayed_soc_temp
1448                                        = info->soca->soc + info->soca->cc_delta;
1449                                 if (displayed_soc_temp < 0)
1450                                         displayed_soc_temp = 0;
1451                                 displayed_soc_temp
1452                                          = min(10000, displayed_soc_temp);
1453                                 displayed_soc_temp = max(0, displayed_soc_temp);
1454                                 if (0 == info->soca->jt_limit) {
1455                                         check_charge_status_2(info, displayed_soc_temp);
1456                                 } else {
1457                                         info->soca->displayed_soc = displayed_soc_temp;
1458                                 }
1459                                 info->soca->last_soc = calc_capacity(info) * 100;
1460                                 if (Ibat < 0) {
1461                                         if (info->soca->displayed_soc < 300) {
1462                                                 info->soca->target_use_cap = 0;
1463                                                 info->soca->status = RICOH619_SOCA_LOW_VOL;
1464                                         } else {
1465                                                 if ((info->fg_poff_vbat != 0)
1466                                                       && (Vbat < info->fg_poff_vbat * 1000)){
1467                                                           info->soca->target_use_cap = 0;
1468                                                           info->soca->status = RICOH619_SOCA_LOW_VOL;
1469                                                   } else { 
1470                                                           info->soca->status = RICOH619_SOCA_UNSTABLE;
1471                                                   }
1472                                         }
1473                                 } else {
1474                                         if(info->soca->displayed_soc >= 9850)
1475                                         {
1476                                                 info->soca->displayed_soc = 10000;
1477                                                 info->chg_complete_tm_ov_flag = 1;
1478                                         }
1479                                         info->soca->status = RICOH619_SOCA_UNSTABLE;
1480                                 }
1481                         }
1482                 }
1483         } else if (RICOH619_SOCA_ZERO == info->soca->status) {
1484                 if (calc_ocv(info) > get_OCV_voltage(info, 0)) {
1485                         err = reset_FG_process(info);
1486                         if (err < 0)
1487                                 dev_err(info->dev, "Error in writing the control register\n");
1488                         info->soca->last_soc = calc_capacity_2(info);
1489                         info->soca->status = RICOH619_SOCA_STABLE;
1490                 }
1491                 info->soca->displayed_soc = 0;
1492         }
1493 end_flow:
1494         /* keep DSOC = 1 when Vbat is over 3.4V*/
1495         if( info->fg_poff_vbat != 0) {
1496                 if (info->soca->zero_flg == 1) {
1497                         if(info->soca->Ibat_ave >= 0) {
1498                                 info->soca->zero_flg = 0;
1499                         }
1500                         info->soca->displayed_soc = 0;
1501                 } else if (info->soca->displayed_soc < 50) {
1502                         if (info->soca->Vbat_ave < 2000*1000) { /* error value */
1503                                 info->soca->displayed_soc = 100;
1504                         } else if (info->soca->Vbat_ave < info->fg_poff_vbat*1000) {
1505                                 info->soca->displayed_soc = 0;
1506                                 info->soca->zero_flg = 1;
1507                         } else {
1508                                 info->soca->displayed_soc = 100;
1509                         }
1510                 }
1511         }
1512
1513         if (g_fg_on_mode
1514                  && (info->soca->status == RICOH619_SOCA_STABLE)) {
1515                 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
1516                 if (err < 0)
1517                         dev_err(info->dev, "Error in writing PSWR_REG\n");
1518                 g_soc = 0x7F;
1519                 err = calc_capacity_in_period(info, &cc_cap,
1520                                                         &is_charging, true);
1521                 if (err < 0)
1522                         dev_err(info->dev, "Read cc_sum Error !!-----\n");
1523
1524         } else if (RICOH619_SOCA_UNSTABLE != info->soca->status) {
1525                 if ((info->soca->displayed_soc + 50) / 100 <= 1) {
1526                         val = 1;
1527                 } else {
1528                         val = (info->soca->displayed_soc + 50)/100;
1529                         val &= 0x7f;
1530                 }
1531                 err = ricoh619_write(info->dev->parent, PSWR_REG, val);
1532                 if (err < 0)
1533                         dev_err(info->dev, "Error in writing PSWR_REG\n");
1534
1535                 g_soc = val;
1536
1537                 err = calc_capacity_in_period(info, &cc_cap,
1538                                                          &is_charging, true);
1539                 if (err < 0)
1540                         dev_err(info->dev, "Read cc_sum Error !!-----\n");
1541         }
1542         
1543         RICOH_FG_DBG("PMU: ------- STATUS= %d: IBAT= %d: VSYS= %d: VBAT= %d: DSOC= %d: RSOC= %d: -------\n",
1544                info->soca->status, info->soca->Ibat_ave, info->soca->Vsys_ave, info->soca->Vbat_ave,
1545         info->soca->displayed_soc, info->soca->soc);
1546
1547 #ifdef DISABLE_CHARGER_TIMER
1548         /* clear charger timer */
1549         if ( info->soca->chg_status == POWER_SUPPLY_STATUS_CHARGING ) {
1550                 err = ricoh619_read(info->dev->parent, TIMSET_REG, &val);
1551                 if (err < 0)
1552                         dev_err(info->dev,
1553                         "Error in read TIMSET_REG%d\n", err);
1554                 /* to check bit 0-1 */
1555                 val2 = val & 0x03;
1556
1557                 if (val2 == 0x02){
1558                         /* set rapid timer 240 -> 300 */
1559                         err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x03);
1560                         if (err < 0) {
1561                                 dev_err(info->dev, "Error in writing the control register\n");
1562                         }
1563                 } else {
1564                         /* set rapid timer 300 -> 240 */
1565                         err = ricoh619_clr_bits(info->dev->parent, TIMSET_REG, 0x01);
1566                         err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x02);
1567                         if (err < 0) {
1568                                 dev_err(info->dev, "Error in writing the control register\n");
1569                         }
1570                 }
1571         }
1572 #endif
1573
1574         if (0 == info->soca->ready_fg)
1575                 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1576                                          RICOH619_FG_RESET_TIME * HZ);
1577         else if (delay_flag == 1)
1578                 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1579                                          RICOH619_DELAY_TIME * HZ);
1580         else if (RICOH619_SOCA_DISP == info->soca->status)
1581                 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1582                                          RICOH619_DISPLAY_UPDATE_TIME * HZ);
1583         else if (info->soca->hurry_up_flg == 1)
1584                 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1585                                          RICOH619_LOW_VOL_DOWN_TIME * HZ);
1586         else
1587                 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1588                                          RICOH619_DISPLAY_UPDATE_TIME * HZ);
1589
1590         mutex_unlock(&info->lock);
1591
1592         if((true == is_jeita_updated)
1593         || (info->soca->last_displayed_soc/100 != (info->soca->displayed_soc+50)/100))
1594                 power_supply_changed(&info->battery);
1595
1596         info->soca->last_displayed_soc = info->soca->displayed_soc+50;
1597
1598         if ((info->soca->displayed_soc >= 9850) && (info->soca->Ibat_ave > -20) && (info->capacity < 100)
1599                 && (info->soca->chg_status == POWER_SUPPLY_STATUS_CHARGING))
1600         {
1601                 if(info->chg_complete_rd_flag == 0)
1602                 {
1603                         info->chg_complete_rd_flag = 1;
1604                         info->chg_complete_rd_cnt = 0;
1605                         queue_delayed_work(info->monitor_wqueue, &info->charge_complete_ready, 0);
1606                 }
1607         }
1608         else
1609         {
1610                 info->chg_complete_rd_flag = 0;
1611         }
1612
1613         if(info->chg_complete_tm_ov_flag == 1)
1614         {
1615                 if(info->soca->displayed_soc < 9850 || info->soca->Ibat_ave < -20)
1616                 {
1617                         info->chg_complete_tm_ov_flag = 0;
1618                         power_supply_changed(&info->battery);
1619                 }
1620         }
1621         return;
1622 }
1623
1624 static void ricoh619_stable_charge_countdown_work(struct work_struct *work)
1625 {
1626         int ret;
1627         int max = 0;
1628         int min = 100;
1629         int i;
1630         struct ricoh619_battery_info *info = container_of(work,
1631                 struct ricoh619_battery_info, charge_stable_work.work);
1632
1633         if (info->entry_factory_mode)
1634                 return;
1635
1636         mutex_lock(&info->lock);
1637         if (RICOH619_SOCA_FG_RESET == info->soca->status)
1638                 info->soca->ready_fg = 1;
1639
1640         if (2 <= info->soca->stable_count) {
1641                 if (3 == info->soca->stable_count
1642                         && RICOH619_SOCA_FG_RESET == info->soca->status) {
1643                         ret = reset_FG_process(info);
1644                         if (ret < 0)
1645                                 dev_err(info->dev, "Error in writing the control register\n");
1646                 }
1647                 info->soca->stable_count = info->soca->stable_count - 1;
1648                 queue_delayed_work(info->monitor_wqueue,
1649                                          &info->charge_stable_work,
1650                                          RICOH619_FG_STABLE_TIME * HZ / 10);
1651         } else if (0 >= info->soca->stable_count) {
1652                 /* Finished queue, ignore */
1653         } else if (1 == info->soca->stable_count) {
1654                 if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
1655                         /* Judge if FG need reset or Not */
1656                         info->soca->soc = calc_capacity(info) * 100;
1657                         if (info->chg_ctr != 0) {
1658                                 queue_delayed_work(info->monitor_wqueue,
1659                                          &info->charge_stable_work,
1660                                          RICOH619_FG_STABLE_TIME * HZ / 10);
1661                                 mutex_unlock(&info->lock);
1662                                 return;
1663                         }
1664                         /* Do reset setting */
1665                         ret = reset_FG_process(info);
1666                         if (ret < 0)
1667                                 dev_err(info->dev, "Error in writing the control register\n");
1668
1669                         info->soca->status = RICOH619_SOCA_FG_RESET;
1670
1671                         /* Delay for addition Reset Time (6s) */
1672                         queue_delayed_work(info->monitor_wqueue,
1673                                          &info->charge_stable_work,
1674                                          RICOH619_FG_RESET_TIME*HZ);
1675                 } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
1676                         info->soca->reset_soc[2] = info->soca->reset_soc[1];
1677                         info->soca->reset_soc[1] = info->soca->reset_soc[0];
1678                         info->soca->reset_soc[0] = calc_capacity(info) * 100;
1679                         info->soca->reset_count++;
1680
1681                         if (info->soca->reset_count > 10) {
1682                                 /* Reset finished; */
1683                                 info->soca->soc = info->soca->reset_soc[0];
1684                                 info->soca->stable_count = 0;
1685                                 goto adjust;
1686                         }
1687
1688                         for (i = 0; i < 3; i++) {
1689                                 if (max < info->soca->reset_soc[i]/100)
1690                                         max = info->soca->reset_soc[i]/100;
1691                                 if (min > info->soca->reset_soc[i]/100)
1692                                         min = info->soca->reset_soc[i]/100;
1693                         }
1694
1695                         if ((info->soca->reset_count > 3) && ((max - min)
1696                                         < RICOH619_MAX_RESET_SOC_DIFF)) {
1697                                 /* Reset finished; */
1698                                 info->soca->soc = info->soca->reset_soc[0];
1699                                 info->soca->stable_count = 0;
1700                                 goto adjust;
1701                         } else {
1702                                 /* Do reset setting */
1703                                 ret = reset_FG_process(info);
1704                                 if (ret < 0)
1705                                         dev_err(info->dev, "Error in writing the control register\n");
1706
1707                                 /* Delay for addition Reset Time (6s) */
1708                                 queue_delayed_work(info->monitor_wqueue,
1709                                                  &info->charge_stable_work,
1710                                                  RICOH619_FG_RESET_TIME*HZ);
1711                         }
1712                 /* Finished queue From now, select FG as result; */
1713                 } else if (RICOH619_SOCA_START == info->soca->status) {
1714                         /* Normal condition */
1715                 } else { /* other state ZERO/DISP/STABLE */
1716                         info->soca->stable_count = 0;
1717                 }
1718
1719                 mutex_unlock(&info->lock);
1720                 return;
1721
1722 adjust:
1723                 info->soca->last_soc = info->soca->soc;
1724                 info->soca->status = RICOH619_SOCA_DISP;
1725                 info->soca->soc_delta = 0;
1726
1727         }
1728         mutex_unlock(&info->lock);
1729         return;
1730 }
1731
1732 static void ricoh619_charge_monitor_work(struct work_struct *work)
1733 {
1734         struct ricoh619_battery_info *info = container_of(work,
1735                 struct ricoh619_battery_info, charge_monitor_work.work);
1736
1737         get_power_supply_status(info);
1738
1739         if (POWER_SUPPLY_STATUS_DISCHARGING == info->soca->chg_status
1740                 || POWER_SUPPLY_STATUS_NOT_CHARGING == info->soca->chg_status) {
1741                 switch (info->soca->dischg_state) {
1742                 case    0:
1743                         info->soca->dischg_state = 1;
1744                         break;
1745                 case    1:
1746                         info->soca->dischg_state = 2;
1747                         break;
1748         
1749                 case    2:
1750                 default:
1751                         break;
1752                 }
1753         } else {
1754                 info->soca->dischg_state = 0;
1755         }
1756
1757         queue_delayed_work(info->monitor_wqueue, &info->charge_monitor_work,
1758                                          RICOH619_CHARGE_MONITOR_TIME * HZ);
1759
1760         return;
1761 }
1762
1763 static void ricoh619_get_charge_work(struct work_struct *work)
1764 {
1765         struct ricoh619_battery_info *info = container_of(work,
1766                 struct ricoh619_battery_info, get_charge_work.work);
1767
1768         int Vbat_temp, Vsys_temp, Ibat_temp;
1769         int Vbat_sort[RICOH619_GET_CHARGE_NUM];
1770         int Vsys_sort[RICOH619_GET_CHARGE_NUM];
1771         int Ibat_sort[RICOH619_GET_CHARGE_NUM];
1772         int i, j;
1773         int ret;
1774
1775         mutex_lock(&info->lock);
1776
1777         for (i = RICOH619_GET_CHARGE_NUM-1; i > 0; i--) {
1778                 if (0 == info->soca->chg_count) {
1779                         info->soca->Vbat[i] = 0;
1780                         info->soca->Vsys[i] = 0;
1781                         info->soca->Ibat[i] = 0;
1782                 } else {
1783                         info->soca->Vbat[i] = info->soca->Vbat[i-1];
1784                         info->soca->Vsys[i] = info->soca->Vsys[i-1];
1785                         info->soca->Ibat[i] = info->soca->Ibat[i-1];
1786                 }
1787         }
1788
1789         ret = measure_vbatt_FG(info, &info->soca->Vbat[0]);
1790         ret = measure_vsys_ADC(info, &info->soca->Vsys[0]);
1791         ret = measure_Ibatt_FG(info, &info->soca->Ibat[0]);
1792
1793         info->soca->chg_count++;
1794
1795         if (RICOH619_GET_CHARGE_NUM != info->soca->chg_count) {
1796                 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1797                                          RICOH619_CHARGE_CALC_TIME * HZ);
1798                 mutex_unlock(&info->lock);
1799                 return ;
1800         }
1801
1802         for (i = 0; i < RICOH619_GET_CHARGE_NUM; i++) {
1803                 Vbat_sort[i] = info->soca->Vbat[i];
1804                 Vsys_sort[i] = info->soca->Vsys[i];
1805                 Ibat_sort[i] = info->soca->Ibat[i];
1806         }
1807
1808         Vbat_temp = 0;
1809         Vsys_temp = 0;
1810         Ibat_temp = 0;
1811         for (i = 0; i < RICOH619_GET_CHARGE_NUM - 1; i++) {
1812                 for (j = RICOH619_GET_CHARGE_NUM - 1; j > i; j--) {
1813                         if (Vbat_sort[j - 1] > Vbat_sort[j]) {
1814                                 Vbat_temp = Vbat_sort[j];
1815                                 Vbat_sort[j] = Vbat_sort[j - 1];
1816                                 Vbat_sort[j - 1] = Vbat_temp;
1817                         }
1818                         if (Vsys_sort[j - 1] > Vsys_sort[j]) {
1819                                 Vsys_temp = Vsys_sort[j];
1820                                 Vsys_sort[j] = Vsys_sort[j - 1];
1821                                 Vsys_sort[j - 1] = Vsys_temp;
1822                         }
1823                         if (Ibat_sort[j - 1] > Ibat_sort[j]) {
1824                                 Ibat_temp = Ibat_sort[j];
1825                                 Ibat_sort[j] = Ibat_sort[j - 1];
1826                                 Ibat_sort[j - 1] = Ibat_temp;
1827                         }
1828                 }
1829         }
1830
1831         Vbat_temp = 0;
1832         Vsys_temp = 0;
1833         Ibat_temp = 0;
1834         for (i = 3; i < RICOH619_GET_CHARGE_NUM-3; i++) {
1835                 Vbat_temp = Vbat_temp + Vbat_sort[i];
1836                 Vsys_temp = Vsys_temp + Vsys_sort[i];
1837                 Ibat_temp = Ibat_temp + Ibat_sort[i];
1838         }
1839         Vbat_temp = Vbat_temp / (RICOH619_GET_CHARGE_NUM - 6);
1840         Vsys_temp = Vsys_temp / (RICOH619_GET_CHARGE_NUM - 6);
1841         Ibat_temp = Ibat_temp / (RICOH619_GET_CHARGE_NUM - 6);
1842
1843         if (0 == info->soca->chg_count) {
1844                 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1845                                  RICOH619_CHARGE_UPDATE_TIME * HZ);
1846                 mutex_unlock(&info->lock);
1847                 return;
1848         } else {
1849                 info->soca->Vbat_ave = Vbat_temp;
1850                 info->soca->Vsys_ave = Vsys_temp;
1851                 info->soca->Ibat_ave = Ibat_temp;
1852         }
1853
1854         info->soca->chg_count = 0;
1855         queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1856                                  RICOH619_CHARGE_UPDATE_TIME * HZ);
1857         mutex_unlock(&info->lock);
1858         return;
1859 }
1860
1861 /* Initial setting of FuelGauge SOCA function */
1862 static int ricoh619_init_fgsoca(struct ricoh619_battery_info *info)
1863 {
1864         int i;
1865         int err;
1866         uint8_t val;
1867         int cc_cap = 0;
1868         bool is_charging = true;
1869
1870         for (i = 0; i <= 10; i = i+1) {
1871                 info->soca->ocv_table[i] = get_OCV_voltage(info, i);
1872                 RICOH_FG_DBG("PMU: %s : * %d0%% voltage = %d uV\n",
1873                                  __func__, i, info->soca->ocv_table[i]);
1874         }
1875
1876         for (i = 0; i < 3; i = i+1)
1877                 info->soca->reset_soc[i] = 0;
1878         info->soca->reset_count = 0;
1879
1880         if (info->first_pwon) {
1881
1882                 err = ricoh619_read(info->dev->parent, CHGISET_REG, &val);
1883                 if (err < 0)
1884                         dev_err(info->dev,
1885                         "Error in read CHGISET_REG%d\n", err);
1886
1887                 err = ricoh619_write(info->dev->parent, CHGISET_REG, 0);
1888                 if (err < 0)
1889                         dev_err(info->dev,
1890                         "Error in writing CHGISET_REG%d\n", err);
1891                 msleep(1000);
1892
1893                 if (!info->entry_factory_mode) {
1894                         err = ricoh619_write(info->dev->parent,
1895                                                         FG_CTRL_REG, 0x51);
1896                         if (err < 0)
1897                                 dev_err(info->dev, "Error in writing the control register\n");
1898                 }
1899
1900                 err = calc_capacity_in_period(info, &cc_cap, &is_charging, true);
1901
1902                 msleep(6000);
1903
1904                 err = ricoh619_write(info->dev->parent, CHGISET_REG, val);
1905                 if (err < 0)
1906                         dev_err(info->dev,
1907                         "Error in writing CHGISET_REG%d\n", err);
1908         }
1909         
1910         /* Rbat : Transfer */
1911         info->soca->Rbat = get_OCV_init_Data(info, 12) * 1000 / 512
1912                                                          * 5000 / 4095;
1913         info->soca->n_cap = get_OCV_init_Data(info, 11);
1914
1915
1916         info->soca->displayed_soc = 0;
1917         info->soca->last_displayed_soc = 0;
1918         info->soca->suspend_soc = 0;
1919         info->soca->ready_fg = 0;
1920         info->soca->soc_delta = 0;
1921         info->soca->full_reset_count = 0;
1922         info->soca->soc_full = 0;
1923         info->soca->fc_cap = 0;
1924         info->soca->status = RICOH619_SOCA_START;
1925         /* stable count down 11->2, 1: reset; 0: Finished; */
1926         info->soca->stable_count = 11;
1927         info->soca->chg_cmp_times = 0;
1928         info->soca->dischg_state = 0;
1929         info->soca->Vbat_ave = 0;
1930         info->soca->Vbat_old = 0;
1931         info->soca->Vsys_ave = 0;
1932         info->soca->Ibat_ave = 0;
1933         info->soca->chg_count = 0;
1934         info->soca->target_use_cap = 0;
1935         info->soca->hurry_up_flg = 0;
1936         info->soca->re_cap_old = 0;
1937         info->soca->jt_limit = 0;
1938         info->soca->zero_flg = 0;
1939         info->soca->cc_cap_offset = 0;
1940
1941         for (i = 0; i < 11; i++) {
1942                 info->soca->ocv_table_low[i] = 0;
1943         }
1944
1945         for (i = 0; i < RICOH619_GET_CHARGE_NUM; i++) {
1946                 info->soca->Vbat[i] = 0;
1947                 info->soca->Vsys[i] = 0;
1948                 info->soca->Ibat[i] = 0;
1949         }
1950
1951         g_full_flag = 0;
1952         
1953 #ifdef ENABLE_FG_KEEP_ON_MODE
1954         g_fg_on_mode = 1;
1955 #else
1956         g_fg_on_mode = 0;
1957 #endif
1958
1959         /* Start first Display job */
1960         queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1961                                                    RICOH619_FG_RESET_TIME*HZ);
1962
1963         /* Start first Waiting stable job */
1964         queue_delayed_work(info->monitor_wqueue, &info->charge_stable_work,
1965                    RICOH619_FG_STABLE_TIME*HZ/10);
1966
1967         queue_delayed_work(info->monitor_wqueue, &info->charge_monitor_work,
1968                                          RICOH619_CHARGE_MONITOR_TIME * HZ);
1969
1970         queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1971                                          RICOH619_CHARGE_MONITOR_TIME * HZ);
1972         if (info->jt_en) {
1973                 if (info->jt_hw_sw) {
1974                         /* Enable JEITA function supported by H/W */
1975                         err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x04);
1976                         if (err < 0)
1977                                 dev_err(info->dev, "Error in writing the control register\n");
1978                 } else {
1979                         /* Disable JEITA function supported by H/W */
1980                         err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x04);
1981                         if (err < 0)
1982                                 dev_err(info->dev, "Error in writing the control register\n");
1983                         queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
1984                                                          RICOH619_FG_RESET_TIME * HZ);
1985                 }
1986         } else {
1987                 /* Disable JEITA function supported by H/W */
1988                 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x04);
1989                 if (err < 0)
1990                         dev_err(info->dev, "Error in writing the control register\n");
1991         }
1992
1993         RICOH_FG_DBG("PMU: %s : * Rbat = %d mOhm   n_cap = %d mAH\n",
1994                          __func__, info->soca->Rbat, info->soca->n_cap);
1995         return 1;
1996 }
1997 #endif
1998
1999 static void ricoh619_charging_complete_work(struct work_struct *work)
2000 {
2001         struct ricoh619_battery_info *info = container_of(work,
2002                 struct ricoh619_battery_info, charge_complete_ready.work);
2003
2004         uint8_t time_ov_flag;
2005         RICOH_FG_DBG("PMU: %s\n", __func__);
2006         RICOH_FG_DBG("info->chg_complete_rd_cnt = %d\n", info->chg_complete_rd_cnt);
2007         RICOH_FG_DBG("info->chg_complete_rd_flag = %d\n", info->chg_complete_rd_flag);
2008         RICOH_FG_DBG("info->chg_complete_tm_ov_flag = %d\n", info->chg_complete_tm_ov_flag);
2009         
2010         if(info->chg_complete_rd_flag == 1)
2011         {
2012                 // start chg 99per to 100per timer
2013                 time_ov_flag = 0;
2014                 info->chg_complete_rd_flag = 2;
2015                 info->chg_complete_tm_ov_flag = 0;
2016         }
2017         else
2018         {
2019                 if(info->capacity == 100)
2020                 {
2021                         // battery arriver to 100% earlier than time ov
2022                         time_ov_flag = 1;
2023                         info->chg_complete_rd_cnt = 0;
2024                         info->chg_complete_tm_ov_flag = 1;
2025                 }
2026                 else if(info->chg_complete_rd_cnt > RICOH619_TIME_CHG_COUNT)
2027                 {
2028                         // chg timer ov before cap arrive to 100%
2029                         time_ov_flag = 1;
2030                         info->chg_complete_tm_ov_flag = 1;
2031                         info->chg_complete_rd_cnt = 0;
2032                         info->soca->status = RICOH619_SOCA_FULL;
2033                         power_supply_changed(&info->battery);
2034                 }
2035                 else
2036                 {
2037                         time_ov_flag = 0;
2038                         info->chg_complete_tm_ov_flag = 0;
2039                 }
2040         }
2041
2042         if ((time_ov_flag == 0) && (info->soca->chg_status == POWER_SUPPLY_STATUS_CHARGING))
2043         {
2044                 info->chg_complete_rd_cnt++;
2045                 queue_delayed_work(info->monitor_wqueue, &info->charge_complete_ready, 
2046                         RICOH619_TIME_CHG_STEP);
2047         }
2048         else
2049         {
2050                 info->chg_complete_rd_flag = 0;
2051         }
2052
2053         RICOH_FG_DBG("PMU2: %s return\n", __func__);
2054         RICOH_FG_DBG("info->chg_complete_rd_cnt = %d\n", info->chg_complete_rd_cnt);
2055         RICOH_FG_DBG("info->chg_complete_rd_flag = %d\n", info->chg_complete_rd_flag);
2056         RICOH_FG_DBG("info->chg_complete_tm_ov_flag = %d\n", info->chg_complete_tm_ov_flag);
2057         RICOH_FG_DBG("time_ov_flag = %d\n", time_ov_flag);
2058
2059 }
2060 static void ricoh619_changed_work(struct work_struct *work)
2061 {
2062         struct ricoh619_battery_info *info = container_of(work,
2063                 struct ricoh619_battery_info, changed_work.work);
2064
2065         RICOH_FG_DBG("PMU: %s\n", __func__);
2066         power_supply_changed(&info->battery);
2067
2068         return;
2069 }
2070
2071 static int check_jeita_status(struct ricoh619_battery_info *info, bool *is_jeita_updated)
2072 /*  JEITA Parameter settings
2073 *
2074 *          VCHG  
2075 *            |     
2076 * jt_vfchg_h~+~~~~~~~~~~~~~~~~~~~+
2077 *            |                   |
2078 * jt_vfchg_l-| - - - - - - - - - +~~~~~~~~~~+
2079 *            |    Charge area    +          |               
2080 *  -------0--+-------------------+----------+--- Temp
2081 *            !                   +
2082 *          ICHG     
2083 *            |                   +
2084 *  jt_ichg_h-+ - -+~~~~~~~~~~~~~~+~~~~~~~~~~+
2085 *            +    |              +          |
2086 *  jt_ichg_l-+~~~~+   Charge area           |
2087 *            |    +              +          |
2088 *         0--+----+--------------+----------+--- Temp
2089 *            0   jt_temp_l      jt_temp_h   55
2090 */
2091 {
2092         int temp;
2093         int err = 0;
2094         int vfchg;
2095         uint8_t chgiset_org;
2096         uint8_t batset2_org;
2097         uint8_t set_vchg_h, set_vchg_l;
2098         uint8_t set_ichg_h, set_ichg_l;
2099
2100         *is_jeita_updated = false;
2101         
2102         /* No execute if JEITA disabled */
2103         if (!info->jt_en || info->jt_hw_sw)
2104                 return 0;
2105
2106         /* Check FG Reset */
2107         if (info->soca->ready_fg) {
2108                 temp = get_battery_temp_2(info) / 10;
2109         } else {
2110                 RICOH_FG_DBG(KERN_INFO "JEITA: %s *** cannot update by resetting FG ******\n", __func__);
2111                 goto out;
2112         }
2113
2114         /* Read BATSET2 */
2115         err = ricoh619_read(info->dev->parent, BATSET2_REG, &batset2_org);
2116         if (err < 0) {
2117                 dev_err(info->dev, "Error in readng the battery setting register\n");
2118                 goto out;
2119         }
2120         vfchg = (batset2_org & 0x70) >> 4;
2121         batset2_org &= 0x8F;
2122         
2123         /* Read CHGISET */
2124         err = ricoh619_read(info->dev->parent, CHGISET_REG, &chgiset_org);
2125         if (err < 0) {
2126                 dev_err(info->dev, "Error in readng the chrage setting register\n");
2127                 goto out;
2128         }
2129         chgiset_org &= 0xC0;
2130
2131         set_ichg_h = (uint8_t)(chgiset_org | info->jt_ichg_h);
2132         set_ichg_l = (uint8_t)(chgiset_org | info->jt_ichg_l);
2133                 
2134         set_vchg_h = (uint8_t)((info->jt_vfchg_h << 4) | batset2_org);
2135         set_vchg_l = (uint8_t)((info->jt_vfchg_l << 4) | batset2_org);
2136
2137         RICOH_FG_DBG(KERN_INFO "PMU: %s *** Temperature: %d, vfchg: %d, SW status: %d, chg_status: %d ******\n",
2138                  __func__, temp, vfchg, info->soca->status, info->soca->chg_status);
2139
2140         if (temp <= 0 || 55 <= temp) {
2141                 /* 1st and 5th temperature ranges (~0, 55~) */
2142                 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Temp(%d) is out of 0-55 ******\n", __func__, temp);
2143                 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2144                 if (err < 0) {
2145                         dev_err(info->dev, "Error in writing the control register\n");
2146                         goto out;
2147                 }
2148                 info->soca->jt_limit = 0;
2149                 *is_jeita_updated = true;
2150         } else if (temp < info->jt_temp_l) {
2151                 /* 2nd temperature range (0~12) */
2152                 if (vfchg != info->jt_vfchg_h) {
2153                         RICOH_FG_DBG(KERN_INFO "PMU: %s *** 0<Temp<12, update to vfchg=%d ******\n", 
2154                                                                         __func__, info->jt_vfchg_h);
2155                         err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2156                         if (err < 0) {
2157                                 dev_err(info->dev, "Error in writing the control register\n");
2158                                 goto out;
2159                         }
2160
2161                         /* set VFCHG/VRCHG */
2162                         err = ricoh619_write(info->dev->parent,
2163                                                          BATSET2_REG, set_vchg_h);
2164                         if (err < 0) {
2165                                 dev_err(info->dev, "Error in writing the battery setting register\n");
2166                                 goto out;
2167                         }
2168                         info->soca->jt_limit = 0;
2169                         *is_jeita_updated = true;
2170                 } else
2171                         RICOH_FG_DBG(KERN_INFO "PMU: %s *** 0<Temp<50, already set vfchg=%d, so no need to update ******\n",
2172                                         __func__, info->jt_vfchg_h);
2173
2174                 /* set ICHG */
2175                 err = ricoh619_write(info->dev->parent, CHGISET_REG, set_ichg_l);
2176                 if (err < 0) {
2177                         dev_err(info->dev, "Error in writing the battery setting register\n");
2178                         goto out;
2179                 }
2180                 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2181                 if (err < 0) {
2182                         dev_err(info->dev, "Error in writing the control register\n");
2183                         goto out;
2184                 }
2185         } else if (temp < info->jt_temp_h) {
2186                 /* 3rd temperature range (12~50) */
2187                 if (vfchg != info->jt_vfchg_h) {
2188                         RICOH_FG_DBG(KERN_INFO "PMU: %s *** 12<Temp<50, update to vfchg==%d ******\n", __func__, info->jt_vfchg_h);
2189
2190                         err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2191                         if (err < 0) {
2192                                 dev_err(info->dev, "Error in writing the control register\n");
2193                                 goto out;
2194                         }
2195                         /* set VFCHG/VRCHG */
2196                         err = ricoh619_write(info->dev->parent,
2197                                                          BATSET2_REG, set_vchg_h);
2198                         if (err < 0) {
2199                                 dev_err(info->dev, "Error in writing the battery setting register\n");
2200                                 goto out;
2201                         }
2202                         info->soca->jt_limit = 0;
2203                         *is_jeita_updated = true;
2204                 } else
2205                         RICOH_FG_DBG(KERN_INFO "PMU: %s *** 12<Temp<50, already set vfchg==%d, so no need to update ******\n", 
2206                                         __func__, info->jt_vfchg_h);
2207                 
2208                 /* set ICHG */
2209                 err = ricoh619_write(info->dev->parent, CHGISET_REG, set_ichg_h);
2210                 if (err < 0) {
2211                         dev_err(info->dev, "Error in writing the battery setting register\n");
2212                         goto out;
2213                 }
2214                 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2215                 if (err < 0) {
2216                         dev_err(info->dev, "Error in writing the control register\n");
2217                         goto out;
2218                 }
2219         } else if (temp < 55) {
2220                 /* 4th temperature range (50~55) */
2221                 if (vfchg != info->jt_vfchg_l) {
2222                         RICOH_FG_DBG(KERN_INFO "PMU: %s *** 50<Temp<55, update to vfchg==%d ******\n", __func__, info->jt_vfchg_l);
2223                         
2224                         err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2225                         if (err < 0) {
2226                                 dev_err(info->dev, "Error in writing the control register\n");
2227                                 goto out;
2228                         }
2229                         /* set VFCHG/VRCHG */
2230                         err = ricoh619_write(info->dev->parent,
2231                                                          BATSET2_REG, set_vchg_l);
2232                         if (err < 0) {
2233                                 dev_err(info->dev, "Error in writing the battery setting register\n");
2234                                 goto out;
2235                         }
2236                         info->soca->jt_limit = 1;
2237                         *is_jeita_updated = true;
2238                 } else
2239                         RICOH_FG_DBG(KERN_INFO "JEITA: %s *** 50<Temp<55, already set vfchg==%d, so no need to update ******\n", 
2240                                         __func__, info->jt_vfchg_l);
2241
2242                 /* set ICHG */
2243                 err = ricoh619_write(info->dev->parent, CHGISET_REG, set_ichg_h);
2244                 if (err < 0) {
2245                         dev_err(info->dev, "Error in writing the battery setting register\n");
2246                         goto out;
2247                 }
2248                 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2249                 if (err < 0) {
2250                         dev_err(info->dev, "Error in writing the control register\n");
2251                         goto out;
2252                 }
2253         }
2254
2255         get_power_supply_status(info);
2256         RICOH_FG_DBG(KERN_INFO "PMU: %s *** Hope updating value in this timing after checking jeita, chg_status: %d, is_jeita_updated: %d ******\n",
2257                  __func__, info->soca->chg_status, *is_jeita_updated);
2258
2259         return 0;
2260         
2261 out:
2262         RICOH_FG_DBG(KERN_INFO "PMU: %s ERROR ******\n", __func__);
2263         return err;
2264 }
2265
2266 static void ricoh619_jeita_work(struct work_struct *work)
2267 {
2268         int ret;
2269         bool is_jeita_updated = false;
2270         struct ricoh619_battery_info *info = container_of(work,
2271                 struct ricoh619_battery_info, jeita_work.work);
2272
2273         mutex_lock(&info->lock);
2274
2275         ret = check_jeita_status(info, &is_jeita_updated);
2276         if (0 == ret) {
2277                 queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
2278                                          RICOH619_JEITA_UPDATE_TIME * HZ);
2279         } else {
2280                 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Call check_jeita_status() in jeita_work, err:%d ******\n", 
2281                                                         __func__, ret);
2282                 queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
2283                                          RICOH619_FG_RESET_TIME * HZ);
2284         }
2285
2286         mutex_unlock(&info->lock);
2287
2288         if(true == is_jeita_updated)
2289                 power_supply_changed(&info->battery);
2290
2291         return;
2292 }
2293
2294 #ifdef ENABLE_FACTORY_MODE
2295 /*------------------------------------------------------*/
2296 /* Factory Mode                                         */
2297 /*    Check Battery exist or not                        */
2298 /*    If not, disabled Rapid to Complete State change   */
2299 /*------------------------------------------------------*/
2300 static int ricoh619_factory_mode(struct ricoh619_battery_info *info)
2301 {
2302         int ret = 0;
2303         uint8_t val = 0;
2304
2305         ret = ricoh619_read(info->dev->parent, RICOH619_INT_MON_CHGCTR, &val);
2306         if (ret < 0) {
2307                 dev_err(info->dev, "Error in reading the control register\n");
2308                 return ret;
2309         }
2310         if (!(val & 0x01)) /* No Adapter connected */
2311                 return ret;
2312
2313         /* Rapid to Complete State change disable */
2314         ret = ricoh619_set_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x40);
2315         if (ret < 0) {
2316                 dev_err(info->dev, "Error in writing the control register\n");
2317                 return ret;
2318         }
2319
2320         /* Wait 1s for checking Charging State */
2321         queue_delayed_work(info->factory_mode_wqueue, &info->factory_mode_work,
2322                          1*HZ);
2323
2324         return ret;
2325 }
2326
2327 static void check_charging_state_work(struct work_struct *work)
2328 {
2329         struct ricoh619_battery_info *info = container_of(work,
2330                 struct ricoh619_battery_info, factory_mode_work.work);
2331
2332         int ret = 0;
2333         uint8_t val = 0;
2334         int chargeCurrent = 0;
2335
2336         ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &val);
2337         if (ret < 0) {
2338                 dev_err(info->dev, "Error in reading the control register\n");
2339                 return;
2340         }
2341
2342
2343         chargeCurrent = get_check_fuel_gauge_reg(info, CC_AVERAGE1_REG,
2344                                                  CC_AVERAGE0_REG, 0x3fff);
2345         if (chargeCurrent < 0) {
2346                 dev_err(info->dev, "Error in reading the FG register\n");
2347                 return;
2348         }
2349
2350         /* Repid State && Charge Current about 0mA */
2351         if (((chargeCurrent >= 0x3ffc && chargeCurrent <= 0x3fff)
2352                 || chargeCurrent < 0x05) && val == 0x43) {
2353                 RICOH_FG_DBG("PMU:%s --- No battery !! Enter Factory mode ---\n"
2354                                 , __func__);
2355                 info->entry_factory_mode = true;
2356                 /* clear FG_ACC bit */
2357                 ret = ricoh619_clr_bits(info->dev->parent, RICOH619_FG_CTRL, 0x10);
2358                 if (ret < 0)
2359                         dev_err(info->dev, "Error in writing FG_CTRL\n");
2360                 
2361                 return; /* Factory Mode */
2362         }
2363
2364         /* Return Normal Mode --> Rapid to Complete State change enable */
2365         ret = ricoh619_clr_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x40);
2366         if (ret < 0) {
2367                 dev_err(info->dev, "Error in writing the control register\n");
2368                 return;
2369         }
2370         RICOH_FG_DBG("PMU:%s --- Battery exist !! Return Normal mode ---0x%2x\n"
2371                         , __func__, val);
2372
2373         return;
2374 }
2375 #endif /* ENABLE_FACTORY_MODE */
2376
2377 static int Calc_Linear_Interpolation(int x0, int y0, int x1, int y1, int y)
2378 {
2379         int     alpha;
2380         int x;
2381
2382         alpha = (y - y0)*100 / (y1 - y0);
2383
2384         x = ((100 - alpha) * x0 + alpha * x1) / 100;
2385
2386         return x;
2387 }
2388
2389 static void ricoh619_scaling_OCV_table(struct ricoh619_battery_info *info, int cutoff_vol, int full_vol, int *start_per, int *end_per)
2390 {
2391         int             i, j;
2392         int             temp;
2393         int             percent_step;
2394         int             OCV_percent_new[11];
2395
2396         /* get ocv table. this table is calculated by Apprication */
2397         //RICOH_FG_DBG("PMU : %s : original table\n",__func__);
2398         for (i = 0; i <= 10; i = i+1) {
2399                 RICOH_FG_DBG(KERN_INFO "PMU: %s : %d0%% voltage = %d uV\n",
2400                                  __func__, i, info->soca->ocv_table_def[i]);
2401         }
2402         //RICOH_FG_DBG("PMU: %s : cutoff_vol %d full_vol %d\n",
2403         //                       __func__, cutoff_vol,full_vol);
2404
2405         /* Check Start % */
2406         if (info->soca->ocv_table_def[0] > cutoff_vol * 1000) {
2407                 *start_per = 0;
2408                 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]);
2409         } else {
2410                 for (i = 1; i < 11; i++) {
2411                         if (info->soca->ocv_table_def[i] >= cutoff_vol * 1000) {
2412                                 /* unit is 0.001% */
2413                                 *start_per = Calc_Linear_Interpolation(
2414                                         (i-1)*1000, info->soca->ocv_table_def[i-1], i*1000,
2415                                         info->soca->ocv_table_def[i], (cutoff_vol * 1000));
2416                                 break;
2417                         }
2418                 }
2419         }
2420
2421         /* Check End % */
2422         for (i = 1; i < 11; i++) {
2423                 if (info->soca->ocv_table_def[i] >= full_vol * 1000) {
2424                         /* unit is 0.001% */
2425                         *end_per = Calc_Linear_Interpolation(
2426                                 (i-1)*1000, info->soca->ocv_table_def[i-1], i*1000,
2427                                  info->soca->ocv_table_def[i], (full_vol * 1000));
2428                         break;
2429                 }
2430         }
2431
2432         /* calc new ocv percent */
2433         percent_step = ( *end_per - *start_per) / 10;
2434         //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);
2435
2436         for (i = 0; i < 11; i++) {
2437                 OCV_percent_new[i]
2438                          = *start_per + percent_step*(i - 0);
2439         }
2440
2441         /* calc new ocv voltage */
2442         for (i = 0; i < 11; i++) {
2443                 for (j = 1; j < 11; j++) {
2444                         if (1000*j >= OCV_percent_new[i]) {
2445                                 temp = Calc_Linear_Interpolation(
2446                                         info->soca->ocv_table_def[j-1], (j-1)*1000,
2447                                         info->soca->ocv_table_def[j] , j*1000,
2448                                          OCV_percent_new[i]);
2449
2450                                 temp = ( (temp/1000) * 4095 ) / 5000;
2451
2452                                 battery_init_para[info->num][i*2 + 1] = temp;
2453                                 battery_init_para[info->num][i*2] = temp >> 8;
2454
2455                                 break;
2456                         }
2457                 }
2458         }
2459         RICOH_FG_DBG("PMU : %s : new table\n",__func__);
2460         for (i = 0; i <= 10; i = i+1) {
2461                 temp = (battery_init_para[info->num][i*2]<<8)
2462                          | (battery_init_para[info->num][i*2+1]);
2463                 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
2464                 temp = ((temp * 50000 * 10 / 4095) + 5) / 10;
2465                 RICOH_FG_DBG("PMU : %s : ocv_table %d is %d v\n",__func__, i, temp);
2466         }
2467
2468 }
2469
2470 static int ricoh619_set_OCV_table(struct ricoh619_battery_info *info)
2471 {
2472         int             ret = 0;
2473         int             i;
2474         int             full_ocv;
2475         int             available_cap;
2476         int             available_cap_ori;
2477         int             temp;
2478         int             temp1;
2479         int             start_per = 0;
2480         int             end_per = 0;
2481         int             Rbat;
2482         int             Ibat_min;
2483         uint8_t val;
2484         uint8_t val2;
2485         uint8_t val_temp;
2486
2487
2488         //get ocv table 
2489         for (i = 0; i <= 10; i = i+1) {
2490                 info->soca->ocv_table_def[i] = get_OCV_voltage(info, i);
2491                 RICOH_FG_DBG(KERN_INFO "PMU: %s : %d0%% voltage = %d uV\n",
2492                          __func__, i, info->soca->ocv_table_def[i]);
2493         }
2494
2495         temp =  (battery_init_para[info->num][24]<<8) | (battery_init_para[info->num][25]);
2496         Rbat = temp * 1000 / 512 * 5000 / 4095;
2497         info->soca->Rsys = Rbat + 55;
2498
2499         if ((info->fg_target_ibat == 0) || (info->fg_target_vsys == 0)) {       /* normal version */
2500
2501                 temp =  (battery_init_para[info->num][22]<<8) | (battery_init_para[info->num][23]);
2502                 //fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
2503                 //                              0x7fff);
2504
2505                 info->soca->target_ibat = temp*2/10; /* calc 0.2C*/
2506                 temp1 =  (battery_init_para[info->num][0]<<8) | (battery_init_para[info->num][1]);
2507
2508                 info->soca->target_vsys = temp1 + ( info->soca->target_ibat * info->soca->Rsys ) / 1000;
2509                 
2510
2511         } else {
2512                 info->soca->target_ibat = info->fg_target_ibat;
2513                 /* calc min vsys value */
2514                 temp1 =  (battery_init_para[info->num][0]<<8) | (battery_init_para[info->num][1]);
2515                 temp = temp1 + ( info->soca->target_ibat * info->soca->Rsys ) / 1000;
2516                 if( temp < info->fg_target_vsys) {
2517                         info->soca->target_vsys = info->fg_target_vsys;
2518                 } else {
2519                         info->soca->target_vsys = temp;
2520                         RICOH_FG_DBG("PMU : %s : setting value of target vsys(%d) is out of range(%d)\n",__func__, info->fg_target_vsys, temp);
2521                 }
2522         }
2523
2524         //for debug
2525         RICOH_FG_DBG("PMU : %s : target_vsys is %d target_ibat is %d\n",__func__,info->soca->target_vsys,info->soca->target_ibat);
2526         
2527         if ((info->soca->target_ibat == 0) || (info->soca->target_vsys == 0)) { /* normal version */
2528         } else {        /*Slice cutoff voltage version. */
2529
2530                 Ibat_min = -1 * info->soca->target_ibat;
2531                 info->soca->cutoff_ocv = info->soca->target_vsys - Ibat_min * info->soca->Rsys / 1000;
2532                 
2533                 full_ocv = (battery_init_para[info->num][20]<<8) | (battery_init_para[info->num][21]);
2534                 full_ocv = full_ocv * 5000 / 4095;
2535
2536                 ricoh619_scaling_OCV_table(info, info->soca->cutoff_ocv, full_ocv, &start_per, &end_per);
2537
2538                 /* calc available capacity */
2539                 /* get avilable capacity */
2540                 /* battery_init_para23-24 is designe capacity */
2541                 available_cap = (battery_init_para[info->num][22]<<8)
2542                                          | (battery_init_para[info->num][23]);
2543
2544                 available_cap = available_cap
2545                          * ((10000 - start_per) / 100) / 100 ;
2546
2547
2548                 battery_init_para[info->num][23] =  available_cap;
2549                 battery_init_para[info->num][22] =  available_cap >> 8;
2550
2551         }
2552         ret = ricoh619_clr_bits(info->dev->parent, FG_CTRL_REG, 0x01);
2553         if (ret < 0) {
2554                 dev_err(info->dev, "error in FG_En off\n");
2555                 goto err;
2556         }
2557         /////////////////////////////////
2558         ret = ricoh619_read_bank1(info->dev->parent, 0xDC, &val);
2559         if (ret < 0) {
2560                 dev_err(info->dev, "batterry initialize error\n");
2561                 goto err;
2562         }
2563
2564         val_temp = val;
2565         val     &= 0x0F; //clear bit 4-7
2566         val     |= 0x10; //set bit 4
2567         
2568         ret = ricoh619_write_bank1(info->dev->parent, 0xDC, val);
2569         if (ret < 0) {
2570                 dev_err(info->dev, "batterry initialize error\n");
2571                 goto err;
2572         }
2573         
2574         ret = ricoh619_read_bank1(info->dev->parent, 0xDC, &val2);
2575         if (ret < 0) {
2576                 dev_err(info->dev, "batterry initialize error\n");
2577                 goto err;
2578         }
2579
2580         ret = ricoh619_write_bank1(info->dev->parent, 0xDC, val_temp);
2581         if (ret < 0) {
2582                 dev_err(info->dev, "batterry initialize error\n");
2583                 goto err;
2584         }
2585
2586         RICOH_FG_DBG("PMU : %s : original 0x%x, before 0x%x, after 0x%x\n",__func__, val_temp, val, val2);
2587         
2588         if (val != val2) {
2589                 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
2590                                 BAT_INIT_TOP_REG, 30, battery_init_para[info->num]);
2591                 if (ret < 0) {
2592                         dev_err(info->dev, "batterry initialize error\n");
2593                         goto err;
2594                 }
2595         } else {
2596                 ret = ricoh619_read_bank1(info->dev->parent, 0xD2, &val);
2597                 if (ret < 0) {
2598                 dev_err(info->dev, "batterry initialize error\n");
2599                 goto err;
2600                 }
2601         
2602                 ret = ricoh619_read_bank1(info->dev->parent, 0xD3, &val2);
2603                 if (ret < 0) {
2604                         dev_err(info->dev, "batterry initialize error\n");
2605                         goto err;
2606                 }
2607                 
2608                 available_cap_ori = val2 + (val << 8);
2609                 available_cap = battery_init_para[info->num][23]
2610                                                 + (battery_init_para[info->num][22] << 8);
2611
2612                 if (available_cap_ori == available_cap) {
2613                         ret = ricoh619_bulk_writes_bank1(info->dev->parent,
2614                                 BAT_INIT_TOP_REG, 22, battery_init_para[info->num]);
2615                         if (ret < 0) {
2616                                 dev_err(info->dev, "batterry initialize error\n");
2617                                 return ret;
2618                         }
2619                         
2620                         for (i = 0; i < 6; i++) {
2621                                 ret = ricoh619_write_bank1(info->dev->parent, 0xD4+i, battery_init_para[info->num][24+i]);
2622                                 if (ret < 0) {
2623                                         dev_err(info->dev, "batterry initialize error\n");
2624                                         return ret;
2625                                 }
2626                         }
2627                 } else {
2628                         ret = ricoh619_bulk_writes_bank1(info->dev->parent,
2629                                 BAT_INIT_TOP_REG, 30, battery_init_para[info->num]);
2630                         if (ret < 0) {
2631                                 dev_err(info->dev, "batterry initialize error\n");
2632                                 goto err;
2633                         }
2634                 }
2635         }
2636
2637         ////////////////////////////////
2638
2639         return 0;
2640 err:
2641         return ret;
2642 }
2643
2644 /* Initial setting of battery */
2645 static int ricoh619_init_battery(struct ricoh619_battery_info *info)
2646 {
2647         int ret = 0;
2648         uint8_t val;
2649         uint8_t val2;
2650         /* Need to implement initial setting of batery and error */
2651         /* -------------------------- */
2652 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
2653
2654         /* set relaxation state */
2655         if (RICOH619_REL1_SEL_VALUE > 240)
2656                 val = 0x0F;
2657         else
2658                 val = RICOH619_REL1_SEL_VALUE / 16 ;
2659
2660         /* set relaxation state */
2661         if (RICOH619_REL2_SEL_VALUE > 120)
2662                 val2 = 0x0F;
2663         else
2664                 val2 = RICOH619_REL2_SEL_VALUE / 8 ;
2665
2666         val =  val + (val2 << 4);
2667
2668         ret = ricoh619_write_bank1(info->dev->parent, BAT_REL_SEL_REG, val);
2669         if (ret < 0) {
2670                 dev_err(info->dev, "Error in writing BAT_REL_SEL_REG\n");
2671                 return ret;
2672         }
2673
2674         ret = ricoh619_read_bank1(info->dev->parent, BAT_REL_SEL_REG, &val);
2675         RICOH_FG_DBG("PMU: -------  BAT_REL_SEL= %xh: =======\n",
2676                 val);
2677
2678         ret = ricoh619_write_bank1(info->dev->parent, BAT_TA_SEL_REG, 0x00);
2679         if (ret < 0) {
2680                 dev_err(info->dev, "Error in writing BAT_TA_SEL_REG\n");
2681                 return ret;
2682         }
2683
2684 //      ret = ricoh619_read(info->dev->parent, FG_CTRL_REG, &val);
2685 //      if (ret < 0) {
2686 //              dev_err(info->dev, "Error in reading the control register\n");
2687 //              return ret;
2688 //      }
2689
2690 //      val = (val & 0x10) >> 4;
2691 //      info->first_pwon = (val == 0) ? 1 : 0;
2692         ret = ricoh619_read(info->dev->parent, PSWR_REG, &val);
2693         if (ret < 0) {
2694                 dev_err(info->dev,"Error in reading PSWR_REG %d\n", ret);
2695                 return ret;
2696         }
2697         info->first_pwon = (val == 0) ? 1 : 0;
2698         g_soc = val & 0x7f;
2699         
2700         ret = ricoh619_set_OCV_table(info);
2701         if (ret < 0) {
2702                 dev_err(info->dev, "Error in writing the OCV Tabler\n");
2703                 return ret;
2704         }
2705
2706         ret = ricoh619_write(info->dev->parent, FG_CTRL_REG, 0x11);
2707         if (ret < 0) {
2708                 dev_err(info->dev, "Error in writing the control register\n");
2709                 return ret;
2710         }
2711
2712 #endif
2713
2714         ret = ricoh619_write(info->dev->parent, VINDAC_REG, 0x03);
2715         if (ret < 0) {
2716                 dev_err(info->dev, "Error in writing the control register\n");
2717                 return ret;
2718         }
2719
2720         if (info->alarm_vol_mv < 2700 || info->alarm_vol_mv > 3400) {
2721                 dev_err(info->dev, "alarm_vol_mv is out of range!\n");
2722                 return -1;
2723         }
2724
2725         return ret;
2726 }
2727
2728 /* Initial setting of charger */
2729 static int ricoh619_init_charger(struct ricoh619_battery_info *info)
2730 {
2731         int err;
2732         uint8_t val;
2733         uint8_t val2;
2734         uint8_t val3;
2735         int charge_status;
2736         int     vfchg_val;
2737         int     icchg_val;
2738         int     rbat;
2739         int     temp;
2740
2741         info->chg_ctr = 0;
2742         info->chg_stat1 = 0;
2743
2744         err = ricoh619_set_bits(info->dev->parent, RICOH619_PWR_FUNC, 0x20);
2745         if (err < 0) {
2746                 dev_err(info->dev, "Error in writing the PWR FUNC register\n");
2747                 goto free_device;
2748         }
2749
2750         charge_status = get_power_supply_status(info);
2751
2752         if (charge_status != POWER_SUPPLY_STATUS_FULL)
2753         {
2754                 /* Disable charging */
2755                 err = ricoh619_clr_bits(info->dev->parent,CHGCTL1_REG, 0x03);
2756                 if (err < 0) {
2757                         dev_err(info->dev, "Error in writing the control register\n");
2758                         goto free_device;
2759                 }
2760         }
2761
2762         //debug messeage
2763         err = ricoh619_read(info->dev->parent, REGISET1_REG,&val);
2764         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);
2765
2766         /* REGISET1:(0xB6) setting */
2767         if ((info->ch_ilim_adp != 0xFF) || (info->ch_ilim_adp <= 0x1D)) {
2768                 val = info->ch_ilim_adp;
2769
2770                 err = ricoh619_write(info->dev->parent, REGISET1_REG,val);
2771                 if (err < 0) {
2772                         dev_err(info->dev, "Error in writing REGISET1_REG %d\n",
2773                                                                                  err);
2774                         goto free_device;
2775                 }
2776         }
2777
2778         //debug messeage
2779         err = ricoh619_read(info->dev->parent, REGISET1_REG,&val);
2780         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);
2781         
2782                 //debug messeage
2783         err = ricoh619_read(info->dev->parent, REGISET2_REG,&val);
2784         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);
2785
2786         /* REGISET2:(0xB7) setting */
2787         err = ricoh619_read(info->dev->parent, REGISET2_REG, &val);
2788         if (err < 0) {
2789                 dev_err(info->dev,
2790                 "Error in read REGISET2_REG %d\n", err);
2791                 goto free_device;
2792         }
2793         
2794         if ((info->ch_ilim_usb != 0xFF) || (info->ch_ilim_usb <= 0x1D)) {
2795                 val2 = info->ch_ilim_usb;
2796         } else {/* Keep OTP value */
2797                 val2 = (val & 0x1F);
2798         }
2799
2800                 /* keep bit 5-7 */
2801         val &= 0xE0;
2802         
2803         val = val + val2;
2804         
2805         err = ricoh619_write(info->dev->parent, REGISET2_REG,val);
2806         if (err < 0) {
2807                 dev_err(info->dev, "Error in writing REGISET2_REG %d\n",
2808                                                                          err);
2809                 goto free_device;
2810         }
2811
2812                 //debug messeage
2813         err = ricoh619_read(info->dev->parent, REGISET2_REG,&val);
2814         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);
2815
2816         /* CHGISET_REG(0xB8) setting */
2817                 //debug messeage
2818         err = ricoh619_read(info->dev->parent, CHGISET_REG,&val);
2819         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);
2820
2821         err = ricoh619_read(info->dev->parent, CHGISET_REG, &val);
2822         if (err < 0) {
2823                 dev_err(info->dev,
2824                 "Error in read CHGISET_REG %d\n", err);
2825                 goto free_device;
2826         }
2827
2828                 /* Define Current settings value for charging (bit 4~0)*/
2829         if ((info->ch_ichg != 0xFF) || (info->ch_ichg <= 0x1D)) {
2830                 val2 = info->ch_ichg;
2831         } else { /* Keep OTP value */
2832                 val2 = (val & 0x1F);
2833         }
2834
2835                 /* Define Current settings at the charge completion (bit 7~6)*/
2836         if ((info->ch_icchg != 0xFF) || (info->ch_icchg <= 0x03)) {
2837                 val3 = info->ch_icchg << 6;
2838         } else { /* Keep OTP value */
2839                 val3 = (val & 0xC0);
2840         }
2841
2842         val = val2 + val3;
2843
2844         err = ricoh619_write(info->dev->parent, CHGISET_REG, val);
2845         if (err < 0) {
2846                 dev_err(info->dev, "Error in writing CHGISET_REG %d\n",
2847                                                                          err);
2848                 goto free_device;
2849         }
2850
2851                 //debug messeage
2852         err = ricoh619_read(info->dev->parent, CHGISET_REG,&val);
2853         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);
2854
2855                 //debug messeage
2856         err = ricoh619_read(info->dev->parent, BATSET1_REG,&val);
2857         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);
2858         
2859         /* BATSET1_REG(0xBA) setting */
2860         err = ricoh619_read(info->dev->parent, BATSET1_REG, &val);
2861         if (err < 0) {
2862                 dev_err(info->dev,
2863                 "Error in read BATSET1 register %d\n", err);
2864                 goto free_device;
2865         }
2866
2867                 /* Define Battery overvoltage  (bit 4)*/
2868         if ((info->ch_vbatovset != 0xFF) || (info->ch_vbatovset <= 0x1)) {
2869                 val2 = info->ch_vbatovset;
2870                 val2 = val2 << 4;
2871         } else { /* Keep OTP value */
2872                 val2 = (val & 0x10);
2873         }
2874         
2875                 /* keep bit 0-3 and bit 5-7 */
2876         val = (val & 0xEF);
2877         
2878         val = val + val2;
2879
2880         err = ricoh619_write(info->dev->parent, BATSET1_REG, val);
2881         if (err < 0) {
2882                 dev_err(info->dev, "Error in writing BAT1_REG %d\n",
2883                                                                          err);
2884                 goto free_device;
2885         }
2886                 //debug messeage
2887         err = ricoh619_read(info->dev->parent, BATSET1_REG,&val);
2888         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);
2889         
2890                 //debug messeage
2891         err = ricoh619_read(info->dev->parent, BATSET2_REG,&val);
2892         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);
2893
2894         
2895         /* BATSET2_REG(0xBB) setting */
2896         err = ricoh619_read(info->dev->parent, BATSET2_REG, &val);
2897         if (err < 0) {
2898                 dev_err(info->dev,
2899                 "Error in read BATSET2 register %d\n", err);
2900                 goto free_device;
2901         }
2902
2903                 /* Define Re-charging voltage (bit 2~0)*/
2904         if ((info->ch_vrchg != 0xFF) || (info->ch_vrchg <= 0x04)) {
2905                 val2 = info->ch_vrchg;
2906         } else { /* Keep OTP value */
2907                 val2 = (val & 0x07);
2908         }
2909
2910                 /* Define FULL charging voltage (bit 6~4)*/
2911         if ((info->ch_vfchg != 0xFF) || (info->ch_vfchg <= 0x04)) {
2912                 val3 = info->ch_vfchg;
2913                 val3 = val3 << 4;
2914         } else {        /* Keep OTP value */
2915                 val3 = (val & 0x70);
2916         }
2917
2918                 /* keep bit 3 and bit 7 */
2919         val = (val & 0x88);
2920         
2921         val = val + val2 + val3;
2922
2923         err = ricoh619_write(info->dev->parent, BATSET2_REG, val);
2924         if (err < 0) {
2925                 dev_err(info->dev, "Error in writing RICOH619_RE_CHARGE_VOLTAGE %d\n",
2926                                                                          err);
2927                 goto free_device;
2928         }
2929
2930                 //debug messeage
2931         err = ricoh619_read(info->dev->parent, BATSET2_REG,&val);
2932         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);
2933
2934         /* Set rising edge setting ([1:0]=01b)for INT in charging */
2935         /*  and rising edge setting ([3:2]=01b)for charge completion */
2936         err = ricoh619_read(info->dev->parent, RICOH619_CHG_STAT_DETMOD1, &val);
2937         if (err < 0) {
2938                 dev_err(info->dev, "Error in reading CHG_STAT_DETMOD1 %d\n",
2939                                                                  err);
2940                 goto free_device;
2941         }
2942         val &= 0xf0;
2943         val |= 0x05;
2944         err = ricoh619_write(info->dev->parent, RICOH619_CHG_STAT_DETMOD1, val);
2945         if (err < 0) {
2946                 dev_err(info->dev, "Error in writing CHG_STAT_DETMOD1 %d\n",
2947                                                                  err);
2948                 goto free_device;
2949         }
2950
2951         /* Unmask In charging/charge completion */
2952         err = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGSTS1, 0xfc);
2953         if (err < 0) {
2954                 dev_err(info->dev, "Error in writing INT_MSK_CHGSTS1 %d\n",
2955                                                                  err);
2956                 goto free_device;
2957         }
2958
2959         /* Set both edge for VUSB([3:2]=11b)/VADP([1:0]=11b) detect */
2960         err = ricoh619_read(info->dev->parent, RICOH619_CHG_CTRL_DETMOD1, &val);
2961         if (err < 0) {
2962                 dev_err(info->dev, "Error in reading CHG_CTRL_DETMOD1 %d\n",
2963                                                                  err);
2964                 goto free_device;
2965         }
2966         val &= 0xf0;
2967         val |= 0x0f;
2968         err = ricoh619_write(info->dev->parent, RICOH619_CHG_CTRL_DETMOD1, val);
2969         if (err < 0) {
2970                 dev_err(info->dev, "Error in writing CHG_CTRL_DETMOD1 %d\n",
2971                                                                  err);
2972                 goto free_device;
2973         }
2974
2975         /* Unmask In VUSB/VADP completion */
2976         err = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGCTR, 0xfc);
2977         if (err < 0) {
2978                 dev_err(info->dev, "Error in writing INT_MSK_CHGSTS1 %d\n",
2979                                                                          err);
2980                 goto free_device;
2981         }
2982         
2983         if (charge_status != POWER_SUPPLY_STATUS_FULL)
2984         {
2985                 /* Enable charging */
2986                 err = ricoh619_set_bits(info->dev->parent,CHGCTL1_REG, 0x03);
2987                 if (err < 0) {
2988                         dev_err(info->dev, "Error in writing the control register\n");
2989                         goto free_device;
2990                 }
2991         }
2992         /* get OCV100_min, OCV100_min*/
2993         temp = (battery_init_para[info->num][24]<<8) | (battery_init_para[info->num][25]);
2994         rbat = temp * 1000 / 512 * 5000 / 4095;
2995         
2996         /* get vfchg value */
2997         err = ricoh619_read(info->dev->parent, BATSET2_REG, &val);
2998         if (err < 0) {
2999                 dev_err(info->dev, "Error in reading the batset2reg\n");
3000                 goto free_device;
3001         }
3002         val &= 0x70;
3003         val2 = val >> 4;
3004         if (val2 <= 3) {
3005                 vfchg_val = 4050 + val2 * 50;
3006         } else {
3007                 vfchg_val = 4350;
3008         }
3009         RICOH_FG_DBG("PMU : %s : test test val %d, val2 %d vfchg %d\n", __func__, val, val2, vfchg_val);
3010
3011         /* get  value */
3012         err = ricoh619_read(info->dev->parent, CHGISET_REG, &val);
3013         if (err < 0) {
3014                 dev_err(info->dev, "Error in reading the chgisetreg\n");
3015                 goto free_device;
3016         }
3017         val &= 0xC0;
3018         val2 = val >> 6;
3019         icchg_val = 50 + val2 * 50;
3020         RICOH_FG_DBG("PMU : %s : test test val %d, val2 %d icchg %d\n", __func__, val, val2, icchg_val);
3021
3022         info->soca->OCV100_min = ( vfchg_val * 99 / 100 - (icchg_val * (rbat +20))/1000 - 20 ) * 1000;
3023         info->soca->OCV100_max = ( vfchg_val * 101 / 100 - (icchg_val * (rbat +20))/1000 + 20 ) * 1000;
3024         
3025         RICOH_FG_DBG("PMU : %s : 100 min %d, 100 max %d vfchg %d icchg %d rbat %d\n",__func__,
3026         info->soca->OCV100_min,info->soca->OCV100_max,vfchg_val,icchg_val,rbat);
3027
3028 #ifdef ENABLE_LOW_BATTERY_DETECTION
3029         /* Set ADRQ=00 to stop ADC */
3030         ricoh619_write(info->dev->parent, RICOH619_ADC_CNT3, 0x0);
3031         /* Enable VSYS threshold Low interrupt */
3032         ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
3033         /* Set ADC auto conversion interval 250ms */
3034         ricoh619_write(info->dev->parent, RICOH619_ADC_CNT2, 0x0);
3035         /* Enable VSYS pin conversion in auto-ADC */
3036 //      ricoh619_write(info->dev->parent, RICOH619_ADC_CNT1, 0x10);
3037         ricoh619_write(info->dev->parent, RICOH619_ADC_CNT1, 0x16);
3038         /* Set VSYS threshold low voltage value = (voltage(V)*255)/(3*2.5) */
3039         val = info->alarm_vol_mv * 255 / 7500;
3040         ricoh619_write(info->dev->parent, RICOH619_ADC_VSYS_THL, val);
3041         /* Start auto-mode & average 4-time conversion mode for ADC */
3042         ricoh619_write(info->dev->parent, RICOH619_ADC_CNT3, 0x28);
3043         /* Enable master ADC INT */
3044         ricoh619_set_bits(info->dev->parent, RICOH619_INTC_INTEN, ADC_INT);
3045 #endif
3046
3047 free_device:
3048         return err;
3049 }
3050
3051
3052 static int get_power_supply_status(struct ricoh619_battery_info *info)
3053 {
3054         uint8_t status;
3055         uint8_t supply_state;
3056         uint8_t charge_state;
3057         int ret = 0;
3058
3059         /* get  power supply status */
3060         ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
3061         if (ret < 0) {
3062                 dev_err(info->dev, "Error in reading the control register\n");
3063                 return ret;
3064         }
3065
3066         charge_state = (status & 0x1F);
3067         supply_state = ((status & 0xC0) >> 6);
3068
3069         if (info->entry_factory_mode)
3070                         return POWER_SUPPLY_STATUS_NOT_CHARGING;
3071
3072         if (supply_state == SUPPLY_STATE_BAT) {
3073                 info->soca->chg_status = POWER_SUPPLY_STATUS_DISCHARGING;
3074         } else {
3075                 switch (charge_state) {
3076                 case    CHG_STATE_CHG_OFF:
3077                                 info->soca->chg_status
3078                                         = POWER_SUPPLY_STATUS_DISCHARGING;
3079                                 break;
3080                 case    CHG_STATE_CHG_READY_VADP:
3081                                 info->soca->chg_status
3082                                         = POWER_SUPPLY_STATUS_NOT_CHARGING;
3083                                 break;
3084                 case    CHG_STATE_CHG_TRICKLE:
3085                                 info->soca->chg_status
3086                                         = POWER_SUPPLY_STATUS_CHARGING;
3087                                 break;
3088                 case    CHG_STATE_CHG_RAPID:
3089                                 info->soca->chg_status
3090                                         = POWER_SUPPLY_STATUS_CHARGING;
3091                                 break;
3092                 case    CHG_STATE_CHG_COMPLETE:
3093                                 info->soca->chg_status
3094                                         = POWER_SUPPLY_STATUS_FULL;
3095                                 break;
3096                 case    CHG_STATE_SUSPEND:
3097                                 info->soca->chg_status
3098                                         = POWER_SUPPLY_STATUS_DISCHARGING;
3099                                 break;
3100                 case    CHG_STATE_VCHG_OVER_VOL:
3101                                 info->soca->chg_status
3102                                         = POWER_SUPPLY_STATUS_DISCHARGING;
3103                                 break;
3104                 case    CHG_STATE_BAT_ERROR:
3105                                 info->soca->chg_status
3106                                         = POWER_SUPPLY_STATUS_NOT_CHARGING;
3107                                 break;
3108                 case    CHG_STATE_NO_BAT:
3109                                 info->soca->chg_status
3110                                         = POWER_SUPPLY_STATUS_NOT_CHARGING;
3111                                 break;
3112                 case    CHG_STATE_BAT_OVER_VOL:
3113                                 info->soca->chg_status
3114                                         = POWER_SUPPLY_STATUS_NOT_CHARGING;
3115                                 break;
3116                 case    CHG_STATE_BAT_TEMP_ERR:
3117                                 info->soca->chg_status
3118                                         = POWER_SUPPLY_STATUS_NOT_CHARGING;
3119                                 break;
3120                 case    CHG_STATE_DIE_ERR:
3121                                 info->soca->chg_status
3122                                         = POWER_SUPPLY_STATUS_NOT_CHARGING;
3123                                 break;
3124                 case    CHG_STATE_DIE_SHUTDOWN:
3125                                 info->soca->chg_status
3126                                         = POWER_SUPPLY_STATUS_DISCHARGING;
3127                                 break;
3128                 case    CHG_STATE_NO_BAT2:
3129                                 info->soca->chg_status
3130                                         = POWER_SUPPLY_STATUS_NOT_CHARGING;
3131                                 break;
3132                 case    CHG_STATE_CHG_READY_VUSB:
3133                                 info->soca->chg_status
3134                                         = POWER_SUPPLY_STATUS_NOT_CHARGING;
3135                                 break;
3136                 default:
3137                                 info->soca->chg_status
3138                                         = POWER_SUPPLY_STATUS_UNKNOWN;
3139                                 break;
3140                 }
3141         }
3142
3143         return info->soca->chg_status;
3144 }
3145
3146 static int get_power_supply_Android_status(struct ricoh619_battery_info *info)
3147 {
3148
3149         get_power_supply_status(info);
3150
3151         /* get  power supply status */
3152         if (info->entry_factory_mode)
3153                         return POWER_SUPPLY_STATUS_NOT_CHARGING;
3154
3155         switch (info->soca->chg_status) {
3156                 case    POWER_SUPPLY_STATUS_UNKNOWN:
3157                                 return POWER_SUPPLY_STATUS_UNKNOWN;
3158                                 break;
3159
3160                 case    POWER_SUPPLY_STATUS_NOT_CHARGING:
3161                                 return POWER_SUPPLY_STATUS_NOT_CHARGING;
3162                                 break;
3163
3164                 case    POWER_SUPPLY_STATUS_DISCHARGING:
3165                                 return POWER_SUPPLY_STATUS_DISCHARGING;
3166                                 break;
3167
3168                 case    POWER_SUPPLY_STATUS_CHARGING:
3169                                 return POWER_SUPPLY_STATUS_CHARGING;
3170                                 break;
3171
3172                 case    POWER_SUPPLY_STATUS_FULL:
3173                                 if(info->soca->displayed_soc == 100 * 100) {
3174                                         return POWER_SUPPLY_STATUS_FULL;
3175                                 } else {
3176                                         return POWER_SUPPLY_STATUS_CHARGING;
3177                                 }
3178                                 break;
3179                 default:
3180                                 return POWER_SUPPLY_STATUS_UNKNOWN;
3181                                 break;
3182         }
3183
3184         return POWER_SUPPLY_STATUS_UNKNOWN;
3185 }
3186 extern struct ricoh619 *g_ricoh619;
3187 static void charger_irq_work(struct work_struct *work)
3188 {
3189         struct ricoh619_battery_info *info
3190                  = container_of(work, struct ricoh619_battery_info, irq_work);
3191         int ret = 0;
3192         uint8_t reg_val;
3193         RICOH_FG_DBG("PMU:%s In\n", __func__);
3194
3195         power_supply_changed(&info->battery);
3196         power_supply_changed(&powerac);
3197         power_supply_changed(&powerusb);
3198
3199 //      mutex_lock(&info->lock);
3200         
3201         if (info->chg_stat1 & 0x01) {
3202                 ricoh619_read(info->dev->parent, CHGSTATE_REG, &reg_val);
3203                 if (reg_val & 0x40) { /* USE ADP */     
3204                         #ifdef SUPPORT_USB_CONNECT_TO_ADP
3205                                 int i;
3206                                 for(i =0;i<60;i++){
3207                                 RICOH_FG_DBG("PMU:%s usb det dwc_otg_check_dpdm =%d\n", __func__,dwc_otg_check_dpdm(0));
3208                                 if(2 == dwc_otg_check_dpdm(0)){
3209                                 /* set adp limit current 2A */
3210                                 ricoh619_write(info->dev->parent, REGISET1_REG, 0x16);
3211                                 /* set charge current 2A */
3212                                 ricoh619_write(info->dev->parent, CHGISET_REG, 0xD3); 
3213                                 }
3214                                 else {
3215                                 /* set adp limit current 500ma */
3216                                 ricoh619_write(info->dev->parent, REGISET1_REG, 0x04);
3217                                 /* set charge current 500ma */
3218                                 ricoh619_write(info->dev->parent, CHGISET_REG, 0xc4); 
3219                                 }
3220                                 
3221                                 power_supply_changed(&info->battery);
3222                                 power_supply_changed(&powerac);
3223                                 power_supply_changed(&powerusb);
3224                                 msleep(100);
3225                                 }
3226                         #else //support adp and usb chag
3227                         if (gpio_is_valid(g_ricoh619->dc_det)){
3228                                 ret = gpio_request(g_ricoh619->dc_det, "ricoh619_dc_det");
3229                                 if (ret < 0) {
3230                                         RICOH_FG_DBG("Failed to request gpio %d with ret:""%d\n",g_ricoh619->dc_det, ret);
3231                                 }
3232                                 gpio_direction_input(g_ricoh619->dc_det);
3233                                 ret = gpio_get_value(g_ricoh619->dc_det);
3234
3235                                 if (ret ==0){
3236                                         /* set adp limit current 2A */
3237                                         ricoh619_write(info->dev->parent, REGISET1_REG, 0x16);
3238                                         /* set charge current 2A */
3239                                         ricoh619_write(info->dev->parent, CHGISET_REG, 0xD3);
3240                                 }
3241                                 else {
3242                                         /* set adp limit current 500ma */
3243                                         ricoh619_write(info->dev->parent, REGISET1_REG, 0x04);
3244                                         /* set charge current 500ma */
3245                                         ricoh619_write(info->dev->parent, CHGISET_REG, 0xc4); 
3246                                 }
3247                                 gpio_free(g_ricoh619->dc_det);
3248                         }
3249                         else{
3250                                 /* set adp limit current 2A */
3251                                 ricoh619_write(info->dev->parent, REGISET1_REG, 0x16);
3252                                 /* set charge current 2A */
3253                                 ricoh619_write(info->dev->parent, CHGISET_REG, 0xD3); 
3254                         }
3255                         #endif
3256                 } else if (reg_val & 0x80) { /* USE ONLY USB */
3257                         queue_work(info->usb_workqueue, &info->usb_irq_work);
3258                 }
3259         }
3260         info->chg_ctr = 0;
3261         info->chg_stat1 = 0;
3262         
3263         /* Enable Interrupt for VADP/USB */
3264         ret = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGCTR, 0xfc);
3265         if (ret < 0)
3266                 dev_err(info->dev,
3267                          "%s(): Error in enable charger mask INT %d\n",
3268                          __func__, ret);
3269
3270         /* Enable Interrupt for Charging & complete */
3271         ret = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGSTS1, 0xfc);
3272         if (ret < 0)
3273                 dev_err(info->dev,
3274                          "%s(): Error in enable charger mask INT %d\n",
3275                          __func__, ret);
3276
3277 //      mutex_unlock(&info->lock);
3278         RICOH_FG_DBG("PMU:%s Out\n", __func__);
3279 }
3280
3281 #ifdef ENABLE_LOW_BATTERY_DETECTION
3282 static void low_battery_irq_work(struct work_struct *work)
3283 {
3284         struct ricoh619_battery_info *info = container_of(work,
3285                  struct ricoh619_battery_info, low_battery_work.work);
3286
3287         int ret = 0;
3288
3289         RICOH_FG_DBG("PMU:%s In\n", __func__);
3290
3291         power_supply_changed(&info->battery);
3292
3293         /* Enable VADP threshold Low interrupt */
3294         ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
3295         if (ret < 0)
3296                 dev_err(info->dev,
3297                          "%s(): Error in enable adc mask INT %d\n",
3298                          __func__, ret);
3299 }
3300 #endif
3301
3302
3303 static void ricoh619_usb_charge_det(void)
3304 {
3305         struct ricoh619 *ricoh619 = g_ricoh619;
3306         ricoh619_set_bits(ricoh619->dev,REGISET2_REG,(1 << 7));  //set usb limit current  when SDP or other mode
3307         RICOH_FG_DBG("PMU:%s usb det dwc_otg_check_dpdm =%d\n", __func__,dwc_otg_check_dpdm(0));
3308         if(2 == dwc_otg_check_dpdm(0)){
3309         ricoh619_write(ricoh619->dev,REGISET2_REG,0x16);  //set usb limit current  2A
3310         ricoh619_write(ricoh619->dev,CHGISET_REG,0xD3);  //set charge current  2A
3311         }
3312         else {
3313         ricoh619_write(ricoh619->dev,REGISET2_REG,0x04);  //set usb limit current  500ma
3314         ricoh619_write(ricoh619->dev,CHGISET_REG,0xC4);  //set charge current   500ma
3315         }
3316         power_supply_changed(&powerac);
3317         power_supply_changed(&powerusb);
3318 }
3319
3320 static void usb_det_irq_work(struct work_struct *work)
3321 {
3322         struct ricoh619_battery_info *info = container_of(work,
3323                  struct ricoh619_battery_info, usb_irq_work);
3324         int ret = 0;
3325         uint8_t sts;
3326
3327         RICOH_FG_DBG("PMU:%s In\n", __func__);
3328
3329         power_supply_changed(&info->battery);
3330         power_supply_changed(&powerac);
3331         power_supply_changed(&powerusb);
3332
3333         mutex_lock(&info->lock);
3334
3335         /* Enable Interrupt for VUSB */
3336         ret = ricoh619_clr_bits(info->dev->parent,
3337                                          RICOH619_INT_MSK_CHGCTR, 0x02);
3338         if (ret < 0)
3339                 dev_err(info->dev,
3340                          "%s(): Error in enable charger mask INT %d\n",
3341                          __func__, ret);
3342
3343         mutex_unlock(&info->lock);
3344         ret = ricoh619_read(info->dev->parent, RICOH619_INT_MON_CHGCTR, &sts);
3345         if (ret < 0)
3346                 dev_err(info->dev, "Error in reading the control register\n");
3347
3348         sts &= 0x02;
3349         if (sts)
3350                 ricoh619_usb_charge_det();
3351         
3352         RICOH_FG_DBG("PMU:%s Out\n", __func__);
3353 }
3354
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");