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