ricoh619:modify charge limit current
[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
36 #include <linux/interrupt.h>
37 #include <linux/irq.h>
38
39
40 /* define for function */
41 #define ENABLE_FUEL_GAUGE_FUNCTION
42 #define ENABLE_LOW_BATTERY_DETECTION
43 #define ENABLE_FACTORY_MODE
44 #define ENABLE_FG_KEEP_ON_MODE
45
46 /* define for current limit. unit is mA */
47 /* if value is "0", these settings set by OTP */
48 #define RICOH619_MAX_CHARGE_CURRENT     0       /* mA */
49 #define RICOH619_MAX_ADP_CURRENT        0       /* mA */
50 #define RICOH619_MAX_USB_CURRENT        0       /* mA */
51 #define RICOH619_CHARGE_COMPLETION_CURRENT      200     /* mA Range 50~200
52                                                          * (Step 50) */
53 #define RICOH619_FULL_CHARGING_VOLTAGE          0       /* mv can set 4050,
54                                                          * 4100, 4150, 4200,
55                                                          * 4350(default 4100) */
56 #define RICOH619_RE_CHARGING_VOLTAGE            0       /* mv can set 3850,
57                                                          * 3900, 3950, 4000,
58                                                          * 4100(default 3900) */
59
60 /* FG setting */
61 #define CUTOFF_VOL                              0       /* mV "0" means cutoff
62                                                          * voltage = original
63                                                          * OCV table value */
64 #define RICOH619_REL1_SEL_VALUE                 64      /* mv Range 0~240
65                                                          * (Step 16) */
66
67 enum int_type {
68         SYS_INT  = 0x01,
69         DCDC_INT = 0x02,
70         ADC_INT  = 0x08,
71         GPIO_INT = 0x10,
72         CHG_INT  = 0x40,
73 };
74
75 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
76 /* define for FG parameter */
77 #define RICOH619_MONITOR_START_TIME             15
78 #define RICOH619_FG_RESET_TIME                  6
79 #define RICOH619_FG_STABLE_TIME                 120
80 #define RICOH619_DISPLAY_UPDATE_TIME            60
81 #define RICOH619_SOCA_DISP_UPDATE_TIME          60
82 #define RICOH619_MAX_RESET_SOC_DIFF             5
83
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 };
93 #endif
94
95 #ifdef ENABLE_LOW_BATTERY_DETECTION
96 #define LOW_BATTERY_DETECTION_TIME              10
97 #endif
98
99 struct ricoh619_soca_info {
100         int Rbat;
101         int n_cap;
102         int ocv_table[11];
103         int soc;                /* Latest FG SOC value */
104         int displayed_soc;
105         int status;             /* SOCA status 0: Not initial; 5: Finished */
106         int stable_count;
107         int chg_status;         /* chg_status */
108         int soc_delta;          /* soc delta for status3(DISP) */
109         int cc_delta;
110         int last_soc;
111         int ready_fg;
112         int reset_count;
113         int reset_soc[3];
114 };
115
116 struct ricoh619_battery_info {
117         struct device      *dev;
118         struct power_supply     battery;
119         struct delayed_work     monitor_work;
120         struct delayed_work     usb_work;
121         struct delayed_work     displayed_work;
122         struct delayed_work     charge_stable_work;
123         struct delayed_work     changed_work;
124 #ifdef ENABLE_LOW_BATTERY_DETECTION
125         struct delayed_work     low_battery_work;
126 #endif
127
128         struct work_struct      irq_work;       /* for Charging & VUSB/VADP */
129         struct work_struct      usb_irq_work;   /* for ADC_VUSB */
130
131         struct workqueue_struct *monitor_wqueue;
132         struct workqueue_struct *workqueue;     /* for Charging & VUSB/VADP */
133         struct workqueue_struct *usb_workqueue; /* for ADC_VUSB */
134
135 #ifdef ENABLE_FACTORY_MODE
136         struct delayed_work     factory_mode_work;
137         struct workqueue_struct *factory_mode_wqueue;
138 #endif
139
140         struct mutex            lock;
141         unsigned long           monitor_time;
142         int             adc_vdd_mv;
143         int             multiple;
144         int             alarm_vol_mv;
145         int             status;
146         int             min_voltage;
147         int             max_voltage;
148         int             cur_voltage;
149         int             capacity;
150         int             battery_temp;
151         int             time_to_empty;
152         int             time_to_full;
153         int             chg_ctr;
154         int             chg_stat1;
155         unsigned        present:1;
156         u16             delay;
157         struct          ricoh619_soca_info *soca;
158         int             first_pwon;
159         bool            entry_factory_mode;
160 };
161
162 int charger_irq;
163 /* this value is for mfd fucntion */
164 int g_soc;
165 int g_fg_on_mode;
166
167 static void ricoh619_battery_work(struct work_struct *work)
168 {
169         struct ricoh619_battery_info *info = container_of(work,
170                 struct ricoh619_battery_info, monitor_work.work);
171
172         RICOH_FG_DBG("PMU: %s\n", __func__);
173         power_supply_changed(&info->battery);
174         queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
175                            info->monitor_time);
176 }
177
178 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
179 static int measure_vbatt_FG(struct ricoh619_battery_info *info, int *data);
180 static int measure_Ibatt_FG(struct ricoh619_battery_info *info, int *data);
181 static int calc_capacity(struct ricoh619_battery_info *info);
182 static int get_OCV_init_Data(struct ricoh619_battery_info *info, int index);
183 static int get_OCV_voltage(struct ricoh619_battery_info *info, int index);
184 static int get_check_fuel_gauge_reg(struct ricoh619_battery_info *info,
185                                          int Reg_h, int Reg_l, int enable_bit);
186 static int calc_capacity_in_period(struct ricoh619_battery_info *info,
187                                          int *cc_cap, bool *is_charging);
188
189 /* check charge status.
190  * if CHG not Complete && SOC == 100 -> Stop charge
191  * if CHG Complete && SOC =! 100     -> SOC reset
192  */
193 static int check_charge_status(struct ricoh619_battery_info *info)
194 {
195         uint8_t status;
196         uint8_t supply_state;
197         uint8_t charge_state;
198         int ret = 0;
199         int current_SOC;
200
201         /* get  power supply status */
202         ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
203         if (ret < 0) {
204                 dev_err(info->dev,
205                          "Error in reading the control register\n");
206                 return ret;
207         }
208
209
210         charge_state = (status & 0x1F);
211         supply_state = ((status & 0xC0) >> 6);
212
213         current_SOC = (info->soca->displayed_soc + 50)/100;
214
215         if (charge_state == CHG_STATE_CHG_COMPLETE) {
216                 /* check SOC */
217                 if (current_SOC != 100) {
218                         ret = ricoh619_write(info->dev->parent,
219                                                  FG_CTRL_REG, 0x51);
220                         if (ret < 0) {
221                                 dev_err(info->dev, "Error in writing the control register\n");
222                                 return ret;
223                         }
224                         info->soca->ready_fg = 0;
225
226                         info->soca->displayed_soc = 100 * 100;
227
228                         info->soca->status = RICOH619_SOCA_STABLE;
229                 }
230
231         } else {        /* chg not complete */
232                 if (current_SOC == 100) {
233                         ret = ricoh619_clr_bits(info->dev->parent,
234                                                  CHGCTL1_REG, 0x03);
235                         if (ret < 0) {
236                                 dev_err(info->dev, "Error in writing the control register\n");
237                                 return ret;
238                         }
239                         info->soca->status = RICOH619_SOCA_STABLE;
240                 } else {
241                         ret = ricoh619_set_bits(info->dev->parent,
242                                                  CHGCTL1_REG, 0x03);
243                         if (ret < 0) {
244                                 dev_err(info->dev, "Error in writing the control register\n");
245                                 return ret;
246                         }
247                 }
248         }
249         return ret;
250 }
251
252 static int calc_ocv(struct ricoh619_battery_info *info)
253 {
254         int Vbat = 0;
255         int Ibat = 0;
256         int ret;
257         int ocv;
258
259         ret = measure_vbatt_FG(info, &Vbat);
260         ret = measure_Ibatt_FG(info, &Ibat);
261
262         ocv = Vbat - Ibat * info->soca->Rbat;
263
264         return ocv;
265 }
266
267 /**
268 * Calculate Capacity in a period
269 * - read CC_SUM & FA_CAP from Coulom Counter
270 * -  and calculate Capacity.
271 * @cc_cap: capacity in a period, unit 0.01%
272 * @is_charging: Flag of charging current direction
273 *               TRUE : charging (plus)
274 *               FALSE: discharging (minus)
275 **/
276 static int calc_capacity_in_period(struct ricoh619_battery_info *info,
277                                          int *cc_cap, bool *is_charging)
278 {
279         int err;
280         uint8_t cc_sum_reg[4];
281         uint8_t cc_clr[4] = {0, 0, 0, 0};
282         uint8_t fa_cap_reg[2];
283         uint16_t fa_cap;
284         uint32_t cc_sum;
285
286         *is_charging = true;    /* currrent state initialize -> charging */
287
288         if (info->entry_factory_mode)
289                 return 0;
290
291         /* Disable Charging/Completion Interrupt */
292         err = ricoh619_set_bits(info->dev->parent,
293                                         RICOH619_INT_MSK_CHGSTS1, 0x01);
294         if (err < 0)
295                 goto out;
296
297         /* In suspend - disable charging */
298         err = ricoh619_set_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x08);
299         if (err < 0)
300                 goto out;
301         /* CC_pause enter */
302         err = ricoh619_write(info->dev->parent, CC_CTRL_REG, 0x01);
303         if (err < 0)
304                 goto out;
305         /* Read CC_SUM */
306         err = ricoh619_bulk_reads(info->dev->parent,
307                                         CC_SUMREG3_REG, 4, cc_sum_reg);
308         if (err < 0)
309                 goto out;
310
311         /* CC_SUM <- 0 */
312         err = ricoh619_bulk_writes(info->dev->parent,
313                                         CC_SUMREG3_REG, 4, cc_clr);
314         if (err < 0)
315                 goto out;
316
317         /* CC_pause exist */
318         err = ricoh619_write(info->dev->parent, CC_CTRL_REG, 0);
319         if (err < 0)
320                 goto out;
321         /* out suspend - enable charging */
322         err = ricoh619_clr_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x08);
323         if (err < 0)
324                 goto out;
325
326         udelay(1000);
327
328         /* Clear Charging Interrupt status */
329         err = ricoh619_clr_bits(info->dev->parent,
330                                         RICOH619_INT_IR_CHGSTS1, 0x01);
331         if (err < 0)
332                 goto out;
333
334         /* ricoh619_read(info->dev->parent, RICOH619_INT_IR_CHGSTS1, &val);
335         RICOH_FG_DBG("INT_IR_CHGSTS1 = 0x%x\n",val); */
336
337         /* Enable Charging Interrupt */
338         err = ricoh619_clr_bits(info->dev->parent,
339                                         RICOH619_INT_MSK_CHGSTS1, 0x01);
340         if (err < 0)
341                 goto out;
342
343         /* Read FA_CAP */
344         err = ricoh619_bulk_reads(info->dev->parent,
345                                  FA_CAP_H_REG, 2, fa_cap_reg);
346         if (err < 0)
347                 goto out;
348
349         /* fa_cap = *(uint16_t*)fa_cap_reg & 0x7fff; */
350         fa_cap = (fa_cap_reg[0] << 8 | fa_cap_reg[1]) & 0x7fff;
351
352         /* calculation  two's complement of CC_SUM */
353         cc_sum = cc_sum_reg[0] << 24 | cc_sum_reg[1] << 16 |
354                                 cc_sum_reg[2] << 8 | cc_sum_reg[3];
355
356         /* cc_sum = *(uint32_t*)cc_sum_reg; */
357         if (cc_sum & 0x80000000) {
358                 cc_sum = (cc_sum^0xffffffff)+0x01;
359                 *is_charging = false;           /* discharge */
360         }
361
362         *cc_cap = cc_sum*25/9/fa_cap;   /* CC_SUM/3600/FA_CAP */
363
364         return 0;
365 out:
366         dev_err(info->dev, "Error !!-----\n");
367         return err;
368 }
369
370 static void ricoh619_displayed_work(struct work_struct *work)
371 {
372         int err;
373         uint8_t val;
374         int soc_round;
375         int last_soc_round;
376         int last_disp_round;
377         int displayed_soc_temp;
378         int cc_cap = 0;
379         bool is_charging = true;
380
381         struct ricoh619_battery_info *info = container_of(work,
382         struct ricoh619_battery_info, displayed_work.work);
383
384         if (info->entry_factory_mode) {
385                 info->soca->status = RICOH619_SOCA_STABLE;
386                 info->soca->displayed_soc = -EINVAL;
387                 info->soca->ready_fg = 0;
388                 return;
389         }
390
391         mutex_lock(&info->lock);
392
393         if ((RICOH619_SOCA_START == info->soca->status)
394                  || (RICOH619_SOCA_STABLE == info->soca->status))
395                 info->soca->ready_fg = 1;
396
397         if (RICOH619_SOCA_STABLE == info->soca->status) {
398                 info->soca->soc = calc_capacity(info) * 100;
399                 info->soca->displayed_soc = info->soca->soc;
400         } else if (RICOH619_SOCA_DISP == info->soca->status) {
401
402                 info->soca->soc = calc_capacity(info) * 100;
403
404                 soc_round = info->soca->soc / 100;
405                 last_soc_round = info->soca->last_soc / 100;
406                 last_disp_round = (info->soca->displayed_soc + 50) / 100;
407
408                 info->soca->soc_delta =
409                         info->soca->soc_delta + (soc_round - last_soc_round);
410
411                 info->soca->last_soc = info->soca->soc;
412
413                 /* six case */
414                 if (last_disp_round == soc_round) {
415                         /* if SOC == DISPLAY move to stable */
416                         info->soca->displayed_soc = info->soca->soc ;
417                         info->soca->status = RICOH619_SOCA_STABLE;
418
419                 } else if ((soc_round == 100) || (soc_round == 0)) {
420                         /* if SOC is 0% or 100% , finish display state*/
421                         info->soca->displayed_soc = info->soca->soc ;
422                         info->soca->status = RICOH619_SOCA_STABLE;
423
424                 } else if ((info->soca->chg_status) ==
425                                 (POWER_SUPPLY_STATUS_CHARGING)) {
426                         /* Charge */
427                         if (last_disp_round < soc_round) {
428                                 /* Case 1 : Charge, Display < SOC */
429                                 if (info->soca->soc_delta >= 1) {
430                                         info->soca->displayed_soc
431                                                 = (last_disp_round
432                                                 + info->soca->soc_delta)*100;
433                                         info->soca->soc_delta = 0;
434                                 } else {
435                                         info->soca->displayed_soc
436                                                 = (last_disp_round + 1)*100;
437                                 }
438
439                                 if (last_disp_round >= soc_round) {
440                                         info->soca->displayed_soc
441                                                 = info->soca->soc ;
442                                         info->soca->status
443                                                 = RICOH619_SOCA_STABLE;
444                                 }
445                         } else if (last_disp_round > soc_round) {
446                                 /* Case 2 : Charge, Display > SOC */
447                                 if (info->soca->soc_delta >= 3) {
448                                         info->soca->displayed_soc =
449                                                 (last_disp_round + 1)*100;
450                                         info->soca->soc_delta = 0;
451                                 }
452                                 if (last_disp_round <= soc_round) {
453                                         info->soca->displayed_soc
454                                                 = info->soca->soc ;
455                                         info->soca->status
456                                         = RICOH619_SOCA_STABLE;
457                                 }
458                         }
459                 } else {
460                         /* Dis-Charge */
461                         if (last_disp_round > soc_round) {
462                                 /* Case 3 : Dis-Charge, Display > SOC */
463                                 if (info->soca->soc_delta <= -1) {
464                                         info->soca->displayed_soc
465                                                 = (last_disp_round
466                                                 + info->soca->soc_delta)*100;
467                                         info->soca->soc_delta = 0;
468                                 } else {
469                                         info->soca->displayed_soc
470                                                 = (last_disp_round - 1)*100;
471                                 }
472                                 if (last_disp_round <= soc_round) {
473                                         info->soca->displayed_soc
474                                                 = info->soca->soc ;
475                                         info->soca->status
476                                                 = RICOH619_SOCA_STABLE;
477                                 }
478                         } else if (last_disp_round < soc_round) {
479                                 /* dis Charge, Display < SOC */
480                                 if (info->soca->soc_delta <= -3) {
481                                         info->soca->displayed_soc
482                                                 = (last_disp_round - 1)*100;
483                                         info->soca->soc_delta = 0;
484                                 }
485                                 if (last_disp_round >= soc_round) {
486                                         info->soca->displayed_soc
487                                                 = info->soca->soc ;
488                                         info->soca->status
489                                                 = RICOH619_SOCA_STABLE;
490                                 }
491                         }
492                 }
493         } else if (RICOH619_SOCA_UNSTABLE == info->soca->status
494                  || RICOH619_SOCA_FG_RESET == info->soca->status) {
495                 /* No update */
496         } else if (RICOH619_SOCA_START == info->soca->status) {
497                 err = ricoh619_read(info->dev->parent, PSWR_REG, &val);
498                 val &= 0x7f;
499                 if (info->first_pwon) {
500                         info->soca->soc = calc_capacity(info) * 100;
501                         if ((info->soca->soc == 0) && (calc_ocv(info)
502                                         < get_OCV_voltage(info, 0))) {
503                                 info->soca->displayed_soc = 0;
504                                 info->soca->status = RICOH619_SOCA_ZERO;
505                         } else {
506                                 info->soca->displayed_soc = info->soca->soc;
507                                 info->soca->status = RICOH619_SOCA_UNSTABLE;
508                         }
509                 } else if (g_fg_on_mode && (val == 0x7f)) {
510                         info->soca->soc = calc_capacity(info) * 100;
511                         if ((info->soca->soc == 0) && (calc_ocv(info)
512                                         < get_OCV_voltage(info, 0))) {
513                                 info->soca->displayed_soc = 0;
514                                 info->soca->status = RICOH619_SOCA_ZERO;
515                         } else {
516                                 info->soca->displayed_soc = info->soca->soc;
517                                 info->soca->status = RICOH619_SOCA_STABLE;
518                         }
519                 } else {
520                         info->soca->soc = val * 100;
521                         if ((err < 0) || (val == 0)) {
522                                 dev_err(info->dev,
523                                          "Error in reading PSWR_REG %d\n", err);
524                                 info->soca->soc
525                                          = calc_capacity(info) * 100 + 50;
526                         }
527
528                         err = calc_capacity_in_period(info, &cc_cap,
529                                                                  &is_charging);
530                         if (err < 0)
531                                 dev_err(info->dev, "Read cc_sum Error !!-----\n");
532
533                         info->soca->cc_delta
534                                  = (is_charging == true) ? cc_cap : -cc_cap;
535                         if (calc_ocv(info) < get_OCV_voltage(info, 0)) {
536                                 info->soca->displayed_soc = 0;
537                                 info->soca->status = RICOH619_SOCA_ZERO;
538                         } else {
539                                 displayed_soc_temp
540                                        = info->soca->soc + info->soca->cc_delta;
541                                 displayed_soc_temp
542                                          = min(10000, displayed_soc_temp);
543                                 displayed_soc_temp = max(0, displayed_soc_temp);
544                                 info->soca->displayed_soc = displayed_soc_temp;
545                                 info->soca->status = RICOH619_SOCA_UNSTABLE;
546                         }
547                 }
548         } else if (RICOH619_SOCA_ZERO == info->soca->status) {
549                 if (calc_ocv(info) > get_OCV_voltage(info, 0)) {
550                         err = ricoh619_write(info->dev->parent,
551                                                          FG_CTRL_REG, 0x51);
552                         if (err < 0)
553                                 dev_err(info->dev, "Error in writing the control register\n");
554                         info->soca->status = RICOH619_SOCA_STABLE;
555                         info->soca->ready_fg = 0;
556                 }
557                 info->soca->displayed_soc = 0;
558         }
559
560         /* Ceck charge status */
561         err = check_charge_status(info);
562         if (err < 0)
563                 dev_err(info->dev, "Error in writing the control register\n");
564
565         if (g_fg_on_mode
566                  && (info->soca->status == RICOH619_SOCA_STABLE)) {
567                 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
568                 if (err < 0)
569                         dev_err(info->dev, "Error in writing PSWR_REG\n");
570                 g_soc = 0x7F;
571         } else {
572                 val = (info->soca->displayed_soc + 50)/100;
573                 val &= 0x7f;
574                 err = ricoh619_write(info->dev->parent, PSWR_REG, val);
575                 if (err < 0)
576                         dev_err(info->dev, "Error in writing PSWR_REG\n");
577
578                 g_soc = (info->soca->displayed_soc + 50)/100;
579
580                 err = calc_capacity_in_period(info, &cc_cap, &is_charging);
581                 if (err < 0)
582                         dev_err(info->dev, "Read cc_sum Error !!-----\n");
583         }
584
585         if (0 == info->soca->ready_fg)
586                 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
587                                          RICOH619_FG_RESET_TIME * HZ);
588         else if (RICOH619_SOCA_DISP == info->soca->status)
589                 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
590                                          RICOH619_SOCA_DISP_UPDATE_TIME * HZ);
591         else
592                 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
593                                          RICOH619_DISPLAY_UPDATE_TIME * HZ);
594
595         mutex_unlock(&info->lock);
596
597         return;
598 }
599
600 static void ricoh619_stable_charge_countdown_work(struct work_struct *work)
601 {
602         int ret;
603         int max = 0;
604         int min = 100;
605         int i;
606         struct ricoh619_battery_info *info = container_of(work,
607                 struct ricoh619_battery_info, charge_stable_work.work);
608
609         if (info->entry_factory_mode)
610                 return;
611
612         mutex_lock(&info->lock);
613         if (RICOH619_SOCA_FG_RESET == info->soca->status)
614                 info->soca->ready_fg = 1;
615
616         if (2 <= info->soca->stable_count) {
617                 if (3 == info->soca->stable_count
618                         && RICOH619_SOCA_FG_RESET == info->soca->status) {
619                         ret = ricoh619_write(info->dev->parent,
620                                                          FG_CTRL_REG, 0x51);
621                         if (ret < 0)
622                                 dev_err(info->dev, "Error in writing the control register\n");
623                         info->soca->ready_fg = 0;
624                 }
625                 info->soca->stable_count = info->soca->stable_count - 1;
626                 queue_delayed_work(info->monitor_wqueue,
627                                          &info->charge_stable_work,
628                                          RICOH619_FG_STABLE_TIME * HZ / 10);
629         } else if (0 >= info->soca->stable_count) {
630                 /* Finished queue, ignore */
631         } else if (1 == info->soca->stable_count) {
632                 if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
633                         /* Judge if FG need reset or Not */
634                         info->soca->soc = calc_capacity(info) * 100;
635                         if (info->chg_ctr != 0) {
636                                 queue_delayed_work(info->monitor_wqueue,
637                                          &info->charge_stable_work,
638                                          RICOH619_FG_STABLE_TIME * HZ / 10);
639                                 mutex_unlock(&info->lock);
640                                 return;
641                         }
642                         /* Do reset setting */
643                         ret = ricoh619_write(info->dev->parent,
644                                                  FG_CTRL_REG, 0x51);
645                         if (ret < 0)
646                                 dev_err(info->dev, "Error in writing the control register\n");
647
648                         info->soca->ready_fg = 0;
649                         info->soca->status = RICOH619_SOCA_FG_RESET;
650
651                         /* Delay for addition Reset Time (6s) */
652                         queue_delayed_work(info->monitor_wqueue,
653                                          &info->charge_stable_work,
654                                          RICOH619_FG_RESET_TIME*HZ);
655                 } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
656                         info->soca->reset_soc[2] = info->soca->reset_soc[1];
657                         info->soca->reset_soc[1] = info->soca->reset_soc[0];
658                         info->soca->reset_soc[0] = calc_capacity(info) * 100;
659                         info->soca->reset_count++;
660
661                         if (info->soca->reset_count > 10) {
662                                 /* Reset finished; */
663                                 info->soca->soc = info->soca->reset_soc[0];
664                                 info->soca->stable_count = 0;
665                                 goto adjust;
666                         }
667
668                         for (i = 0; i < 3; i++) {
669                                 if (max < info->soca->reset_soc[i]/100)
670                                         max = info->soca->reset_soc[i]/100;
671                                 if (min > info->soca->reset_soc[i]/100)
672                                         min = info->soca->reset_soc[i]/100;
673                         }
674
675                         if ((info->soca->reset_count > 3) && ((max - min)
676                                         < RICOH619_MAX_RESET_SOC_DIFF)) {
677                                 /* Reset finished; */
678                                 info->soca->soc = info->soca->reset_soc[0];
679                                 info->soca->stable_count = 0;
680                                 goto adjust;
681                         } else {
682                                 /* Do reset setting */
683                                 ret = ricoh619_write(info->dev->parent,
684                                                          FG_CTRL_REG, 0x51);
685                                 if (ret < 0)
686                                         dev_err(info->dev, "Error in writing the control register\n");
687
688                                 info->soca->ready_fg = 0;
689
690                                 /* Delay for addition Reset Time (6s) */
691                                 queue_delayed_work(info->monitor_wqueue,
692                                                  &info->charge_stable_work,
693                                                  RICOH619_FG_RESET_TIME*HZ);
694                         }
695                 /* Finished queue From now, select FG as result; */
696                 } else if (RICOH619_SOCA_START == info->soca->status) {
697                         /* Normal condition */
698                 } else { /* other state ZERO/DISP/STABLE */
699                         info->soca->stable_count = 0;
700                 }
701
702                 mutex_unlock(&info->lock);
703                 return;
704
705 adjust:
706                 info->soca->last_soc = info->soca->soc;
707                 info->soca->status = RICOH619_SOCA_DISP;
708
709         }
710         mutex_unlock(&info->lock);
711         return;
712 }
713
714 /* Initial setting of FuelGauge SOCA function */
715 static int ricoh619_init_fgsoca(struct ricoh619_battery_info *info)
716 {
717         int i;
718         int err;
719         uint8_t val;
720         uint8_t val2;
721
722         for (i = 0; i <= 10; i = i+1) {
723                 info->soca->ocv_table[i] = get_OCV_voltage(info, i);
724                 RICOH_FG_DBG("PMU: %s : * %d0%% voltage = %d uV\n",
725                                  __func__, i, info->soca->ocv_table[i]);
726         }
727
728         for (i = 0; i < 3; i = i+1)
729                 info->soca->reset_soc[i] = 0;
730         info->soca->reset_count = 0;
731
732         if (info->first_pwon) {
733
734                 err = ricoh619_read(info->dev->parent, CHGISET_REG, &val);
735                 if (err < 0)
736                         dev_err(info->dev,
737                         "Error in read CHGISET_REG%d\n", err);
738
739                 err = ricoh619_write(info->dev->parent, CHGISET_REG, 0);
740                 if (err < 0)
741                         dev_err(info->dev,
742                         "Error in writing CHGISET_REG%d\n", err);
743                 msleep(1000);
744
745
746                 err = ricoh619_write(info->dev->parent,
747                                                  FG_CTRL_REG, 0x51);
748                 if (err < 0)
749                         dev_err(info->dev, "Error in writing the control register\n");
750
751                 msleep(6000);
752
753                 err = ricoh619_write(info->dev->parent, CHGISET_REG, val);
754                 if (err < 0)
755                         dev_err(info->dev,
756                         "Error in writing CHGISET_REG%d\n", err);
757         }
758         
759         /* Rbat : Transfer */
760         info->soca->Rbat = get_OCV_init_Data(info, 12) * 1000 / 512
761                                                          * 5000 / 4095;
762         info->soca->n_cap = get_OCV_init_Data(info, 11);
763
764         info->soca->displayed_soc = 0;
765         info->soca->ready_fg = 0;
766         info->soca->soc_delta = 0;
767         info->soca->status = RICOH619_SOCA_START;
768         /* stable count down 11->2, 1: reset; 0: Finished; */
769         info->soca->stable_count = 11;
770
771 #ifdef ENABLE_FG_KEEP_ON_MODE
772         g_fg_on_mode = 1;
773 #else
774         g_fg_on_mode = 0;
775 #endif
776
777         /* Start first Display job */
778         queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
779                                                    RICOH619_FG_RESET_TIME*HZ);
780
781         /* Start first Waiting stable job */
782         queue_delayed_work(info->monitor_wqueue, &info->charge_stable_work,
783                    RICOH619_FG_STABLE_TIME*HZ/10);
784
785         RICOH_FG_DBG("PMU: %s : * Rbat = %d mOhm   n_cap = %d mAH\n",
786                          __func__, info->soca->Rbat, info->soca->n_cap);
787         return 1;
788 }
789 #endif
790
791 static void ricoh619_changed_work(struct work_struct *work)
792 {
793         struct ricoh619_battery_info *info = container_of(work,
794                 struct ricoh619_battery_info, changed_work.work);
795
796         RICOH_FG_DBG("PMU: %s\n", __func__);
797         power_supply_changed(&info->battery);
798
799         return;
800 }
801
802 #ifdef ENABLE_FACTORY_MODE
803 /*------------------------------------------------------*/
804 /* Factory Mode                                         */
805 /*    Check Battery exist or not                        */
806 /*    If not, disabled Rapid to Complete State change   */
807 /*------------------------------------------------------*/
808 static int ricoh619_factory_mode(struct ricoh619_battery_info *info)
809 {
810         int ret = 0;
811         uint8_t val = 0;
812
813         ret = ricoh619_read(info->dev->parent, RICOH619_INT_MON_CHGCTR, &val);
814         if (ret < 0) {
815                 dev_err(info->dev, "Error in reading the control register\n");
816                 return ret;
817         }
818         if (!(val & 0x01)) /* No Adapter connected */
819                 return ret;
820
821         /* Rapid to Complete State change disable */
822         ret = ricoh619_write(info->dev->parent, RICOH619_CHG_CTL1, 0xe3);
823         if (ret < 0) {
824                 dev_err(info->dev, "Error in writing the control register\n");
825                 return ret;
826         }
827
828         /* Wait 1s for checking Charging State */
829         queue_delayed_work(info->factory_mode_wqueue, &info->factory_mode_work,
830                          1*HZ);
831
832         return ret;
833 }
834
835 static void check_charging_state_work(struct work_struct *work)
836 {
837         struct ricoh619_battery_info *info = container_of(work,
838                 struct ricoh619_battery_info, factory_mode_work.work);
839
840         int ret = 0;
841         uint8_t val = 0;
842         int chargeCurrent = 0;
843
844         ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &val);
845         if (ret < 0) {
846                 dev_err(info->dev, "Error in reading the control register\n");
847                 return;
848         }
849
850
851         chargeCurrent = get_check_fuel_gauge_reg(info, CC_AVERAGE1_REG,
852                                                  CC_AVERAGE0_REG, 0x3fff);
853         if (chargeCurrent < 0) {
854                 dev_err(info->dev, "Error in reading the FG register\n");
855                 return;
856         }
857
858         /* Repid State && Charge Current about 0mA */
859         if (((chargeCurrent > 0x3ffc && chargeCurrent < 0x3fff)
860                 || chargeCurrent < 0x05) && val == 0x43) {
861                 RICOH_FG_DBG("PMU:%s --- No battery !! Enter Factory mode ---\n"
862                                 , __func__);
863                 info->entry_factory_mode = true;
864                 return; /* Factory Mode */
865         }
866
867         /* Return Normal Mode --> Rapid to Complete State change enable */
868         ret = ricoh619_write(info->dev->parent, RICOH619_CHG_CTL1, 0xa3);
869         if (ret < 0) {
870                 dev_err(info->dev, "Error in writing the control register\n");
871                 return;
872         }
873         RICOH_FG_DBG("PMU:%s --- Battery exist !! Return Normal mode ---0x%2x\n"
874                         , __func__, val);
875
876         return;
877 }
878 #endif /* ENABLE_FACTORY_MODE */
879
880 static int Calc_Linear_Interpolation(int x0, int y0, int x1, int y1, int y)
881 {
882         int     alpha;
883         int x;
884
885         alpha = (y - y0)*100 / (y1 - y0);
886
887         x = ((100 - alpha) * x0 + alpha * x1) / 100;
888
889         return x;
890 }
891
892 static int ricoh619_set_OCV_table(struct ricoh619_battery_info *info)
893 {
894         int             ret = 0;
895         int             ocv_table[11];
896         int             i, j;
897         int             available_cap;
898         int             temp;
899         int             start_par;
900         int             percent_step;
901         int             OCV_percent_new[11];
902
903         if (CUTOFF_VOL == 0) {  /* normal version */
904         } else {        /*Slice cutoff voltage version. */
905
906                 /* get ocv table. this table is calculated by Apprication */
907                 for (i = 0; i <= 10; i = i+1) {
908                         temp = (battery_init_para[i*2]<<8)
909                                  | (battery_init_para[i*2+1]);
910                         /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
911                         temp = ((temp * 50000 * 10 / 4095) + 5) / 10;
912                         ocv_table[i] = temp;
913                 }
914
915
916                 /* Check Start % */
917                 for (i = 1; i < 11; i++) {
918                         if (ocv_table[i] >= CUTOFF_VOL * 10) {
919                                 /* unit is 0.001% */
920                                 start_par = Calc_Linear_Interpolation(
921                                         (i-1)*1000, ocv_table[i-1], i*1000,
922                                          ocv_table[i], (CUTOFF_VOL * 10));
923                                 i = 11;
924                         }
925                 }
926                 /* calc new ocv percent */
927                 percent_step = (10000 - start_par) / 10;
928
929                 for (i = 0; i < 11; i++) {
930                         OCV_percent_new[i]
931                                  = start_par + percent_step*(i - 0);
932                 }
933
934                 /* calc new ocv voltage */
935                 for (i = 0; i < 11; i++) {
936                         for (j = 1; j < 11; j++) {
937                                 if (1000*j >= OCV_percent_new[i]) {
938                                         temp = Calc_Linear_Interpolation(
939                                                 ocv_table[j-1], (j-1)*1000,
940                                                  ocv_table[j] , j*1000,
941                                                  OCV_percent_new[i]);
942
943                                         temp = temp * 4095 / 50000;
944
945                                         battery_init_para[i*2 + 1] = temp;
946                                         battery_init_para[i*2] = temp >> 8;
947
948                                         j = 11;
949                                 }
950                         }
951                 }
952
953                 /* calc available capacity */
954                 /* get avilable capacity */
955                 /* battery_init_para23-24 is designe capacity */
956                 available_cap = (battery_init_para[22]<<8)
957                                          | (battery_init_para[23]);
958
959                 available_cap = available_cap
960                          * ((10000 - start_par) / 100) / 100 ;
961
962
963                 battery_init_para[23] =  available_cap;
964                 battery_init_para[22] =  available_cap >> 8;
965
966         }
967         ret = ricoh619_bulk_writes_bank1(info->dev->parent,
968                          BAT_INIT_TOP_REG, 32, battery_init_para);
969         if (ret < 0) {
970                 dev_err(info->dev, "batterry initialize error\n");
971                 return ret;
972         }
973
974         return 1;
975 }
976
977 /* Initial setting of battery */
978 static int ricoh619_init_battery(struct ricoh619_battery_info *info)
979 {
980         int ret = 0;
981         uint8_t val;
982         /* Need to implement initial setting of batery and error */
983         /* -------------------------- */
984 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
985
986         /* set kanwa state */
987         if (RICOH619_REL1_SEL_VALUE > 240)
988                 val = 0x0F;
989         else
990                 val = RICOH619_REL1_SEL_VALUE / 16 ;
991
992         val = 0x20 + val;
993
994         ret = ricoh619_write_bank1(info->dev->parent, BAT_REL_SEL_REG, val);
995         if (ret < 0) {
996                 dev_err(info->dev, "Error in writing the OCV Tabler\n");
997                 return ret;
998         }
999
1000         ret = ricoh619_read(info->dev->parent, FG_CTRL_REG, &val);
1001         if (ret < 0) {
1002                 dev_err(info->dev, "Error in reading the control register\n");
1003                 return ret;
1004         }
1005
1006         val = (val & 0x10) >> 4;
1007         info->first_pwon = (val == 0) ? 1 : 0;
1008
1009         ret = ricoh619_set_OCV_table(info);
1010         if (ret < 0) {
1011                 dev_err(info->dev, "Error in writing the OCV Tabler\n");
1012                 return ret;
1013         }
1014
1015         ret = ricoh619_write(info->dev->parent, FG_CTRL_REG, 0x11);
1016         if (ret < 0) {
1017                 dev_err(info->dev, "Error in writing the control register\n");
1018                 return ret;
1019         }
1020
1021 #endif
1022
1023         if (info->alarm_vol_mv < 2700 || info->alarm_vol_mv > 3400) {
1024                 dev_err(info->dev, "alarm_vol_mv is out of range!\n");
1025                 return -1;
1026         }
1027
1028         return ret;
1029 }
1030
1031 /* Initial setting of charger */
1032 static int ricoh619_init_charger(struct ricoh619_battery_info *info)
1033 {
1034         int err;
1035         uint8_t val;
1036         uint8_t val2;
1037
1038         info->chg_ctr = 0;
1039         info->chg_stat1 = 0;
1040
1041         /* In suspend - disable charging */
1042         err = ricoh619_clr_bits(info->dev->parent,CHGCTL1_REG, 0x03);
1043         if (err < 0) {
1044                 dev_err(info->dev, "Error in writing the control register\n");
1045                 goto free_device;
1046         }
1047         
1048         if (RICOH619_MAX_ADP_CURRENT != 0) {
1049                 /* Change ADP Current to 2.5A. */
1050                 err = ricoh619_write(info->dev->parent, 0xb6,
1051                                                  (RICOH619_MAX_ADP_CURRENT-1)/100);
1052                 if (err < 0) {
1053                         dev_err(info->dev, "Error in writing INT_MSK_CHGSTS1 %d\n",
1054                                                                                  err);
1055                         goto free_device;
1056                 }
1057         }
1058
1059         if (RICOH619_MAX_USB_CURRENT != 0) {
1060                 /* Set Max Change USB Current (0xB7) */
1061                 err = ricoh619_write(info->dev->parent, REGISET2_REG,
1062                                                  (RICOH619_MAX_USB_CURRENT-1)/100);
1063                 if (err < 0) {
1064                         dev_err(info->dev,
1065                          "Error in writing RICOH619_MAX_USB_CURRENT %d\n", err);
1066                         goto free_device;
1067                 }
1068         }
1069
1070         /* Set Charge competion current    (0xB8) */
1071         /* this value for bit 4-0 */
1072         if (RICOH619_MAX_CHARGE_CURRENT != 0) {
1073                 val = (RICOH619_MAX_CHARGE_CURRENT-1)/100;
1074         } else {
1075                 err = ricoh619_read(info->dev->parent, CHGISET_REG, &val);
1076                 if (err < 0) {
1077                         dev_err(info->dev,
1078                         "Error in read RICOH619_MAX_CHARGE_CURRENT %d\n", err);
1079                         goto free_device;
1080                 }
1081                 val &= 0x3F;
1082         }
1083         
1084         /* Set Charge competion current    (0xB8) */
1085         /* this value for bit 7-6 */
1086         if(RICOH619_CHARGE_COMPLETION_CURRENT != 0) {
1087                 val2 = (RICOH619_CHARGE_COMPLETION_CURRENT - 50) / 50;
1088         } else {
1089                 err = ricoh619_read(info->dev->parent, CHGISET_REG, &val2);
1090                 if (err < 0) {
1091                         dev_err(info->dev,
1092                         "Error in read RICOH619_MAX_CHARGE_CURRENT %d\n", err);
1093                         goto free_device;
1094                 }
1095                 val2 &= 0xC0;
1096         }
1097         val = val + (val2 << 6);
1098         err = ricoh619_write(info->dev->parent, CHGISET_REG, val);
1099         if (err < 0) {
1100                 dev_err(info->dev,
1101                  "Error in writing RICOH619_MAX_CHARGE_CURRENT %d\n", err);
1102                 goto free_device;
1103         }
1104
1105         /* Change Charger Voltege to 4.2V. Recharge Point to 4.1V */
1106         /* for define FULL charging voltage (bit 6~4)*/
1107         if (RICOH619_FULL_CHARGING_VOLTAGE != 0) {
1108                 if (RICOH619_FULL_CHARGING_VOLTAGE < 4050)
1109                         val2 = 0x00;
1110                 else if (RICOH619_FULL_CHARGING_VOLTAGE > 4200)
1111                         val2 = 0x04;
1112                 else
1113                         val2 = (RICOH619_FULL_CHARGING_VOLTAGE - 4050) / 50;
1114         } else {
1115                 err = ricoh619_read(info->dev->parent, BATSET2_REG, &val2);
1116                 if (err < 0) {
1117                         dev_err(info->dev,
1118                         "Error in read RICOH619_FULL_CHARGE_VOLTAGE %d\n", err);
1119                         goto free_device;
1120                 }
1121                 val2 &= 0x70;
1122         }
1123
1124         /* for define re-charging voltage (bit 2~0)*/
1125         if (RICOH619_RE_CHARGING_VOLTAGE != 0) {
1126                 if (RICOH619_RE_CHARGING_VOLTAGE < 3850)
1127                         val = 0x00;
1128                 else if (RICOH619_RE_CHARGING_VOLTAGE > 4000)
1129                         val = 0x04;
1130                 else
1131                         val = (RICOH619_RE_CHARGING_VOLTAGE - 3850) / 50;
1132         } else {
1133                 err = ricoh619_read(info->dev->parent, BATSET2_REG, &val);
1134                 if (err < 0) {
1135                         dev_err(info->dev,
1136                         "Error in read RICOH619_RE_CHARGE_VOLTAGE %d\n", err);
1137                         goto free_device;
1138                 }
1139                 val &= 0x07;
1140         }
1141
1142         val = val + (val2 << 4);
1143
1144         err = ricoh619_write(info->dev->parent, BATSET2_REG, val);
1145         if (err < 0) {
1146                 dev_err(info->dev, "Error in writing RICOH619_RE_CHARGE_VOLTAGE %d\n",
1147                                                                          err);
1148                 goto free_device;
1149         }
1150
1151         /* out suspend - enable charging */
1152         err = ricoh619_set_bits(info->dev->parent,CHGCTL1_REG, 0x03);
1153         if (err < 0) {
1154                 dev_err(info->dev, "Error in writing the control register\n");
1155                 goto free_device;
1156         }
1157
1158         /* Set rising edge setting ([1:0]=01b)for INT in charging */
1159         /*  and rising edge setting ([3:2]=01b)for charge completion */
1160         err = ricoh619_read(info->dev->parent, RICOH619_CHG_STAT_DETMOD1, &val);
1161         if (err < 0) {
1162                 dev_err(info->dev, "Error in reading CHG_STAT_DETMOD1 %d\n",
1163                                                                  err);
1164                 goto free_device;
1165         }
1166         val &= 0xf0;
1167         val |= 0x05;
1168         err = ricoh619_write(info->dev->parent, RICOH619_CHG_STAT_DETMOD1, val);
1169         if (err < 0) {
1170                 dev_err(info->dev, "Error in writing CHG_STAT_DETMOD1 %d\n",
1171                                                                  err);
1172                 goto free_device;
1173         }
1174
1175         /* Unmask In charging/charge completion */
1176         err = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGSTS1, 0xfc);
1177         if (err < 0) {
1178                 dev_err(info->dev, "Error in writing INT_MSK_CHGSTS1 %d\n",
1179                                                                  err);
1180                 goto free_device;
1181         }
1182
1183         /* Set both edge for VUSB([3:2]=11b)/VADP([1:0]=11b) detect */
1184         err = ricoh619_read(info->dev->parent, RICOH619_CHG_CTRL_DETMOD1, &val);
1185         if (err < 0) {
1186                 dev_err(info->dev, "Error in reading CHG_CTRL_DETMOD1 %d\n",
1187                                                                  err);
1188                 goto free_device;
1189         }
1190         val &= 0xf0;
1191         val |= 0x0f;
1192         err = ricoh619_write(info->dev->parent, RICOH619_CHG_CTRL_DETMOD1, val);
1193         if (err < 0) {
1194                 dev_err(info->dev, "Error in writing CHG_CTRL_DETMOD1 %d\n",
1195                                                                  err);
1196                 goto free_device;
1197         }
1198
1199         /* Unmask In VUSB/VADP completion */
1200         err = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGCTR, 0xfc);
1201         if (err < 0) {
1202                 dev_err(info->dev, "Error in writing INT_MSK_CHGSTS1 %d\n",
1203                                                                          err);
1204                 goto free_device;
1205         }
1206
1207 #ifdef ENABLE_LOW_BATTERY_DETECTION
1208         /* Set ADRQ=00 to stop ADC */
1209         ricoh619_write(info->dev->parent, RICOH619_ADC_CNT3, 0x0);
1210         /* Enable VSYS threshold Low interrupt */
1211         ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
1212         /* Set ADC auto conversion interval 250ms */
1213         ricoh619_write(info->dev->parent, RICOH619_ADC_CNT2, 0x0);
1214         /* Enable VSYS pin conversion in auto-ADC */
1215         ricoh619_write(info->dev->parent, RICOH619_ADC_CNT1, 0x10);
1216         /* Set VSYS threshold low voltage = 3.50v */
1217         ricoh619_write(info->dev->parent, RICOH619_ADC_VSYS_THL, 0x77);
1218         /* Start auto-mode & average 4-time conversion mode for ADC */
1219         ricoh619_write(info->dev->parent, RICOH619_ADC_CNT3, 0x28);
1220         /* Enable master ADC INT */
1221         ricoh619_set_bits(info->dev->parent, RICOH619_INTC_INTEN, ADC_INT);
1222 #endif
1223
1224 free_device:
1225         return err;
1226 }
1227
1228
1229 static int get_power_supply_status(struct ricoh619_battery_info *info)
1230 {
1231         uint8_t status;
1232         uint8_t supply_state;
1233         uint8_t charge_state;
1234         int ret = 0;
1235
1236         /* get  power supply status */
1237         ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
1238         if (ret < 0) {
1239                 dev_err(info->dev, "Error in reading the control register\n");
1240                 return ret;
1241         }
1242
1243         charge_state = (status & 0x1F);
1244         supply_state = ((status & 0xC0) >> 6);
1245
1246         if (supply_state == SUPPLY_STATE_BAT) {
1247                 info->soca->chg_status = POWER_SUPPLY_STATUS_DISCHARGING;
1248         } else {
1249                 switch (charge_state) {
1250                 case    CHG_STATE_CHG_OFF:
1251                                 info->soca->chg_status
1252                                         = POWER_SUPPLY_STATUS_DISCHARGING;
1253                                 break;
1254                 case    CHG_STATE_CHG_READY_VADP:
1255                                 info->soca->chg_status
1256                                         = POWER_SUPPLY_STATUS_NOT_CHARGING;
1257                                 break;
1258                 case    CHG_STATE_CHG_TRICKLE:
1259                                 info->soca->chg_status
1260                                         = POWER_SUPPLY_STATUS_CHARGING;
1261                                 break;
1262                 case    CHG_STATE_CHG_RAPID:
1263                                 info->soca->chg_status
1264                                         = POWER_SUPPLY_STATUS_CHARGING;
1265                                 break;
1266                 case    CHG_STATE_CHG_COMPLETE:
1267                                 info->soca->chg_status
1268                                         = POWER_SUPPLY_STATUS_FULL;
1269                                 break;
1270                 case    CHG_STATE_SUSPEND:
1271                                 info->soca->chg_status
1272                                         = POWER_SUPPLY_STATUS_DISCHARGING;
1273                                 break;
1274                 case    CHG_STATE_VCHG_OVER_VOL:
1275                                 info->soca->chg_status
1276                                         = POWER_SUPPLY_STATUS_DISCHARGING;
1277                                 break;
1278                 case    CHG_STATE_BAT_ERROR:
1279                                 info->soca->chg_status
1280                                         = POWER_SUPPLY_STATUS_NOT_CHARGING;
1281                                 break;
1282                 case    CHG_STATE_NO_BAT:
1283                                 info->soca->chg_status
1284                                         = POWER_SUPPLY_STATUS_NOT_CHARGING;
1285                                 break;
1286                 case    CHG_STATE_BAT_OVER_VOL:
1287                                 info->soca->chg_status
1288                                         = POWER_SUPPLY_STATUS_NOT_CHARGING;
1289                                 break;
1290                 case    CHG_STATE_BAT_TEMP_ERR:
1291                                 info->soca->chg_status
1292                                         = POWER_SUPPLY_STATUS_NOT_CHARGING;
1293                                 break;
1294                 case    CHG_STATE_DIE_ERR:
1295                                 info->soca->chg_status
1296                                         = POWER_SUPPLY_STATUS_NOT_CHARGING;
1297                                 break;
1298                 case    CHG_STATE_DIE_SHUTDOWN:
1299                                 info->soca->chg_status
1300                                         = POWER_SUPPLY_STATUS_DISCHARGING;
1301                                 break;
1302                 case    CHG_STATE_NO_BAT2:
1303                                 info->soca->chg_status
1304                                         = POWER_SUPPLY_STATUS_NOT_CHARGING;
1305                                 break;
1306                 case    CHG_STATE_CHG_READY_VUSB:
1307                                 info->soca->chg_status
1308                                         = POWER_SUPPLY_STATUS_NOT_CHARGING;
1309                                 break;
1310                 default:
1311                                 info->soca->chg_status
1312                                         = POWER_SUPPLY_STATUS_UNKNOWN;
1313                                 break;
1314                 }
1315         }
1316
1317         return info->soca->chg_status;
1318 }
1319
1320 static void charger_irq_work(struct work_struct *work)
1321 {
1322         struct ricoh619_battery_info *info
1323                  = container_of(work, struct ricoh619_battery_info, irq_work);
1324         int ret = 0;
1325         RICOH_FG_DBG("PMU:%s In\n", __func__);
1326
1327         power_supply_changed(&info->battery);
1328
1329         mutex_lock(&info->lock);
1330         info->chg_ctr = 0;
1331         info->chg_stat1 = 0;
1332
1333         /* Enable Interrupt for VADP */
1334         ret = ricoh619_clr_bits(info->dev->parent,
1335                                          RICOH619_INT_MSK_CHGCTR, 0x01);
1336         if (ret < 0)
1337                 dev_err(info->dev,
1338                          "%s(): Error in enable charger mask INT %d\n",
1339                          __func__, ret);
1340
1341         /* Enable Interrupt for Charging & complete */
1342         ret = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGSTS1, 0xfc);
1343         if (ret < 0)
1344                 dev_err(info->dev,
1345                          "%s(): Error in enable charger mask INT %d\n",
1346                          __func__, ret);
1347
1348         mutex_unlock(&info->lock);
1349         RICOH_FG_DBG("PMU:%s Out\n", __func__);
1350 }
1351
1352 #ifdef ENABLE_LOW_BATTERY_DETECTION
1353 static void low_battery_irq_work(struct work_struct *work)
1354 {
1355         struct ricoh619_battery_info *info = container_of(work,
1356                  struct ricoh619_battery_info, low_battery_work.work);
1357
1358         int ret = 0;
1359
1360         RICOH_FG_DBG("PMU:%s In\n", __func__);
1361
1362         power_supply_changed(&info->battery);
1363
1364         /* Enable VADP threshold Low interrupt */
1365         ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
1366         if (ret < 0)
1367                 dev_err(info->dev,
1368                          "%s(): Error in enable adc mask INT %d\n",
1369                          __func__, ret);
1370 }
1371 #endif
1372
1373 extern int dwc_vbus_status(void);
1374 static void ricoh619_usb_charge_det(void)
1375 {
1376         struct ricoh619 *ricoh619 = g_ricoh619;
1377         ricoh619_set_bits(ricoh619->dev,REGISET2_REG,(1 << 7));  //set usb limit current  when SDP or other mode
1378         if(2 == dwc_vbus_status()){
1379         ricoh619_write(ricoh619->dev,REGISET2_REG,0x13);  //set usb limit current  2A
1380         ricoh619_write(ricoh619->dev,CHGISET_REG,0x13);  //set charge current  2A
1381         }
1382         else if(1 == dwc_vbus_status()){
1383         ricoh619_write(ricoh619->dev,REGISET2_REG,0x04);  //set usb limit current  500ma
1384         ricoh619_write(ricoh619->dev,CHGISET_REG,0x04);  //set charge current   500ma
1385         }
1386 }
1387
1388 static void usb_det_irq_work(struct work_struct *work)
1389 {
1390         struct ricoh619_battery_info *info = container_of(work,
1391                  struct ricoh619_battery_info, usb_irq_work);
1392         int ret = 0;
1393         uint8_t sts;
1394         int time =0;
1395
1396         RICOH_FG_DBG("PMU:%s In\n", __func__);
1397
1398         power_supply_changed(&info->battery);
1399
1400         mutex_lock(&info->lock);
1401
1402         /* Enable Interrupt for VUSB */
1403         ret = ricoh619_clr_bits(info->dev->parent,
1404                                          RICOH619_INT_MSK_CHGCTR, 0x02);
1405         if (ret < 0)
1406                 dev_err(info->dev,
1407                          "%s(): Error in enable charger mask INT %d\n",
1408                          __func__, ret);
1409
1410         mutex_unlock(&info->lock);
1411
1412         ret = ricoh619_read(info->dev->parent, RICOH619_INT_MON_CHGCTR, &sts);
1413         if (ret < 0)
1414                 dev_err(info->dev, "Error in reading the control register\n");
1415
1416         sts &= 0x02;
1417         if (sts) {
1418                 time = 60;
1419                 do {
1420                 ricoh619_usb_charge_det();
1421                 time --;
1422                 mdelay(1000);
1423                 }while(time >0);
1424         
1425         } else {
1426                 /*********************/
1427                 /* No process ??     */
1428                 /*********************/
1429         }
1430         
1431         RICOH_FG_DBG("PMU:%s Out\n", __func__);
1432 }
1433
1434 extern void rk28_send_wakeup_key(void); 
1435 static irqreturn_t charger_in_isr(int irq, void *battery_info)
1436 {
1437         struct ricoh619_battery_info *info = battery_info;
1438         struct ricoh619 *ricoh619 = g_ricoh619;
1439         uint8_t reg_val;
1440         int ret;
1441         printk("PMU:%s\n", __func__); 
1442
1443         info->chg_stat1 |= 0x01;
1444         queue_work(info->workqueue, &info->irq_work);
1445         rk28_send_wakeup_key();
1446          
1447          ricoh619_read(ricoh619->dev, 0xbd, &reg_val);
1448         if ( reg_val & 0x40)
1449         {
1450         /* set adp limit current 2A */
1451         ricoh619_write(ricoh619->dev, 0xb6, 0x13);
1452         /* set charge current 2A */
1453         ricoh619_write(ricoh619->dev, 0xb8, 0x13);
1454         }
1455         else if (reg_val & 0x80)
1456         {
1457         queue_work(info->usb_workqueue, &info->usb_irq_work);
1458         }
1459         return IRQ_HANDLED;
1460 }
1461
1462 static irqreturn_t charger_complete_isr(int irq, void *battery_info)
1463 {
1464         struct ricoh619_battery_info *info = battery_info;
1465         printk("PMU:%s\n", __func__);
1466
1467         info->chg_stat1 |= 0x02;
1468         queue_work(info->workqueue, &info->irq_work);
1469
1470         return IRQ_HANDLED;
1471 }
1472
1473 static irqreturn_t charger_usb_isr(int irq, void *battery_info)
1474 {
1475         struct ricoh619_battery_info *info = battery_info;
1476         printk("PMU:%s\n", __func__);
1477
1478         info->chg_ctr |= 0x02;
1479 //      queue_work(info->usb_workqueue, &info->usb_irq_work);
1480         rk28_send_wakeup_key(); 
1481          
1482         if (RICOH619_SOCA_UNSTABLE == info->soca->status
1483                 || RICOH619_SOCA_FG_RESET == info->soca->status)
1484                 info->soca->stable_count = 11;
1485         
1486         return IRQ_HANDLED;
1487 }
1488
1489 static irqreturn_t charger_adp_isr(int irq, void *battery_info)
1490 {
1491         struct ricoh619_battery_info *info = battery_info;
1492         struct ricoh619 *ricoh619 = g_ricoh619;
1493         printk("PMU:%s\n", __func__);
1494
1495         info->chg_ctr |= 0x01;
1496         queue_work(info->workqueue, &info->irq_work);
1497         /* clr usb det irq */
1498         ricoh619_clr_bits(ricoh619->dev, RICOH619_INT_IR_CHGCTR,
1499                                                          info->chg_ctr);
1500         /* set adp limit current 2A */
1501 //      ricoh619_write(ricoh619->dev, 0xb6, 0x13);
1502         /* set charge current 2A */
1503 //      ricoh619_write(ricoh619->dev, 0xb8, 0x13);
1504         rk28_send_wakeup_key(); 
1505         if (RICOH619_SOCA_UNSTABLE == info->soca->status
1506                 || RICOH619_SOCA_FG_RESET == info->soca->status)
1507                 info->soca->stable_count = 11;
1508
1509         return IRQ_HANDLED;
1510 }
1511
1512
1513 #ifdef ENABLE_LOW_BATTERY_DETECTION
1514 /*************************************************************/
1515 /* for Detecting Low Battery                                 */
1516 /*************************************************************/
1517
1518 static irqreturn_t adc_vsysl_isr(int irq, void *battery_info)
1519 {
1520
1521         struct ricoh619_battery_info *info = battery_info;
1522
1523 #if 0
1524         RICOH_FG_DBG("PMU:%s\n", __func__);
1525
1526         queue_delayed_work(info->monitor_wqueue, &info->low_battery_work,
1527                                         LOW_BATTERY_DETECTION_TIME*HZ);
1528
1529 #endif
1530
1531         RICOH_FG_DBG("PMU:%s\n", __func__);
1532         ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
1533         rk28_send_wakeup_key(); 
1534
1535         return IRQ_HANDLED;
1536 }
1537 #endif
1538
1539
1540 #ifdef  ENABLE_FUEL_GAUGE_FUNCTION
1541 static int get_check_fuel_gauge_reg(struct ricoh619_battery_info *info,
1542                                          int Reg_h, int Reg_l, int enable_bit)
1543 {
1544         uint8_t get_data_h, get_data_l;
1545         int old_data, current_data;
1546         int i;
1547         int ret = 0;
1548
1549         old_data = 0;
1550
1551         for (i = 0; i < 5 ; i++) {
1552                 ret = ricoh619_read(info->dev->parent, Reg_h, &get_data_h);
1553                 if (ret < 0) {
1554                         dev_err(info->dev, "Error in reading the control register\n");
1555                         return ret;
1556                 }
1557
1558                 ret = ricoh619_read(info->dev->parent, Reg_l, &get_data_l);
1559                 if (ret < 0) {
1560                         dev_err(info->dev, "Error in reading the control register\n");
1561                         return ret;
1562                 }
1563
1564                 current_data = ((get_data_h & 0xff) << 8) | (get_data_l & 0xff);
1565                 current_data = (current_data & enable_bit);
1566
1567                 if (current_data == old_data)
1568                         return current_data;
1569                 else
1570                         old_data = current_data;
1571         }
1572
1573         return current_data;
1574 }
1575
1576 static int calc_capacity(struct ricoh619_battery_info *info)
1577 {
1578         uint8_t capacity;
1579         int temp;
1580         int ret = 0;
1581
1582         /* get remaining battery capacity from fuel gauge */
1583         ret = ricoh619_read(info->dev->parent, SOC_REG, &capacity);
1584         if (ret < 0) {
1585                 dev_err(info->dev, "Error in reading the control register\n");
1586                 return ret;
1587         }
1588
1589         temp = capacity;
1590
1591         return temp;            /* Unit is 1% */
1592 }
1593
1594 static int get_battery_temp(struct ricoh619_battery_info *info)
1595 {
1596         int ret = 0;
1597         int sign_bit;
1598
1599         ret = get_check_fuel_gauge_reg(info, TEMP_1_REG, TEMP_2_REG, 0x0fff);
1600         if (ret < 0) {
1601                 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
1602                 return ret;
1603         }
1604
1605         /* bit3 of 0xED(TEMP_1) is sign_bit */
1606         sign_bit = ((ret & 0x0800) >> 11);
1607
1608         ret = (ret & 0x07ff);
1609
1610         if (sign_bit == 0)      /* positive value part */
1611                 /* conversion unit */
1612                 /* 1 unit is 0.0625 degree and retun unit
1613                  * should be 0.1 degree,
1614                  */
1615                 ret = ret * 625  / 1000;
1616         else    /*negative value part */
1617                 ret = -1 * ret * 625 / 1000;
1618
1619         return ret;
1620 }
1621
1622 static int get_time_to_empty(struct ricoh619_battery_info *info)
1623 {
1624         int ret = 0;
1625
1626         ret = get_check_fuel_gauge_reg(info, TT_EMPTY_H_REG, TT_EMPTY_L_REG,
1627                                                                 0xffff);
1628         if (ret < 0) {
1629                 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
1630                 return ret;
1631         }
1632
1633         /* conversion unit */
1634         /* 1unit is 1miniute and return nnit should be 1 second */
1635         ret = ret * 60;
1636
1637         return ret;
1638 }
1639
1640 static int get_time_to_full(struct ricoh619_battery_info *info)
1641 {
1642         int ret = 0;
1643
1644         ret = get_check_fuel_gauge_reg(info, TT_FULL_H_REG, TT_FULL_L_REG,
1645                                                                 0xffff);
1646         if (ret < 0) {
1647                 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
1648                 return ret;
1649         }
1650
1651         ret = ret * 60;
1652
1653         return  ret;
1654 }
1655
1656 /* battery voltage is get from Fuel gauge */
1657 static int measure_vbatt_FG(struct ricoh619_battery_info *info, int *data)
1658 {
1659         int ret = 0;
1660
1661         ret = get_check_fuel_gauge_reg(info, VOLTAGE_1_REG, VOLTAGE_2_REG,
1662                                                                 0x0fff);
1663         if (ret < 0) {
1664                 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
1665                 return ret;
1666         }
1667
1668         *data = ret;
1669         /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
1670         *data = *data * 50000 / 4095;
1671         /* return unit should be 1uV */
1672         *data = *data * 100;
1673
1674         return ret;
1675 }
1676
1677 static int measure_Ibatt_FG(struct ricoh619_battery_info *info, int *data)
1678 {
1679         int ret = 0;
1680
1681         ret =  get_check_fuel_gauge_reg(info, CC_AVERAGE1_REG,
1682                                                  CC_AVERAGE0_REG, 0x3fff);
1683         if (ret < 0) {
1684                 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
1685                 return ret;
1686         }
1687
1688         *data = (ret > 0x1fff) ? (ret - 0x4000) : ret;
1689         return ret;
1690 }
1691
1692 static int get_OCV_init_Data(struct ricoh619_battery_info *info, int index)
1693 {
1694         int ret = 0;
1695         ret =  (battery_init_para[index*2]<<8) | (battery_init_para[index*2+1]);
1696         return ret;
1697 }
1698
1699 static int get_OCV_voltage(struct ricoh619_battery_info *info, int index)
1700 {
1701         int ret = 0;
1702         ret =  get_OCV_init_Data(info, index);
1703         /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
1704         ret = ret * 50000 / 4095;
1705         /* return unit should be 1uV */
1706         ret = ret * 100;
1707         return ret;
1708 }
1709
1710 #else
1711 /* battery voltage is get from ADC */
1712 static int measure_vbatt_ADC(struct ricoh619_battery_info *info, int *data)
1713 {
1714         int     i;
1715         uint8_t data_l = 0, data_h = 0;
1716         int ret;
1717
1718         /* ADC interrupt enable */
1719         ret = ricoh619_set_bits(info->dev->parent, INTEN_REG, 0x08);
1720         if (ret < 0) {
1721                 dev_err(info->dev, "Error in setting the control register bit\n");
1722                 goto err;
1723         }
1724
1725         /* enable interrupt request of single mode */
1726         ret = ricoh619_set_bits(info->dev->parent, EN_ADCIR3_REG, 0x01);
1727         if (ret < 0) {
1728                 dev_err(info->dev, "Error in setting the control register bit\n");
1729                 goto err;
1730         }
1731
1732         /* single request */
1733         ret = ricoh619_write(info->dev->parent, ADCCNT3_REG, 0x10);
1734         if (ret < 0) {
1735                 dev_err(info->dev, "Error in writing the control register\n");
1736                 goto err;
1737         }
1738
1739         for (i = 0; i < 5; i++) {
1740                 usleep(1000);
1741                 RICOH_FG_DBG("ADC conversion times: %d\n", i);
1742                 /* read completed flag of ADC */
1743                 ret = ricoh619_read(info->dev->parent, EN_ADCIR3_REG, &data_h);
1744                 if (ret < 0) {
1745                         dev_err(info->dev, "Error in reading the control register\n");
1746                         goto err;
1747                 }
1748
1749                 if (data_h & 0x01)
1750                         goto    done;
1751         }
1752
1753         dev_err(info->dev, "ADC conversion too long!\n");
1754         goto err;
1755
1756 done:
1757         ret = ricoh619_read(info->dev->parent, VBATDATAH_REG, &data_h);
1758         if (ret < 0) {
1759                 dev_err(info->dev, "Error in reading the control register\n");
1760                 goto err;
1761         }
1762
1763         ret = ricoh619_read(info->dev->parent, VBATDATAL_REG, &data_l);
1764         if (ret < 0) {
1765                 dev_err(info->dev, "Error in reading the control register\n");
1766                 goto err;
1767         }
1768
1769         *data = ((data_h & 0xff) << 4) | (data_l & 0x0f);
1770         /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
1771         *data = *data * 5000 / 4095;
1772         /* return unit should be 1uV */
1773         *data = *data * 1000;
1774
1775         return 0;
1776
1777 err:
1778         return -1;
1779 }
1780 #endif
1781
1782 /*
1783 static void ricoh619_external_power_changed(struct power_supply *psy)
1784 {
1785         struct ricoh619_battery_info *info;
1786
1787         info = container_of(psy, struct ricoh619_battery_info, battery);
1788         queue_delayed_work(info->monitor_wqueue,
1789                            &info->changed_work, HZ / 2);
1790         return;
1791 }
1792 */
1793
1794 static int ricoh619_batt_get_prop(struct power_supply *psy,
1795                                 enum power_supply_property psp,
1796                                 union power_supply_propval *val)
1797 {
1798         struct ricoh619_battery_info *info = dev_get_drvdata(psy->dev->parent);
1799         int data = 0;
1800         int ret = 0;
1801         uint8_t status;
1802
1803         mutex_lock(&info->lock);
1804
1805         switch (psp) {
1806         case POWER_SUPPLY_PROP_ONLINE:
1807                 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
1808                 if (ret < 0) {
1809                         dev_err(info->dev, "Error in reading the control register\n");
1810                         mutex_unlock(&info->lock);
1811                         return ret;
1812                 }
1813                 if (psy->type == POWER_SUPPLY_TYPE_MAINS)
1814                         val->intval = (status & 0x40 ? 1 : 0);
1815                 else if (psy->type == POWER_SUPPLY_TYPE_USB)
1816                         val->intval = (status & 0x80 ? 1 : 0);
1817                 break;
1818         /* this setting is same as battery driver of 584 */
1819         case POWER_SUPPLY_PROP_STATUS:
1820                 ret = get_power_supply_status(info);
1821                 val->intval = ret;
1822                 info->status = ret;
1823                 /* RICOH_FG_DBG("Power Supply Status is %d\n",
1824                                                         info->status); */
1825                 break;
1826
1827         /* this setting is same as battery driver of 584 */
1828         case POWER_SUPPLY_PROP_PRESENT:
1829                 val->intval = info->present;
1830                 break;
1831
1832         /* current voltage is get from fuel gauge */
1833         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1834                 /* return real vbatt Voltage */
1835 #ifdef  ENABLE_FUEL_GAUGE_FUNCTION
1836                 if (info->soca->ready_fg)
1837                         ret = measure_vbatt_FG(info, &data);
1838                 else {
1839                         val->intval = -EINVAL;
1840                         RICOH_FG_DBG( "battery voltage is not ready\n");
1841                         break;
1842                 }
1843 #else
1844                 ret = measure_vbatt_ADC(info, &data);
1845 #endif
1846                 val->intval = data;
1847                 /* convert unit uV -> mV */
1848                 info->cur_voltage = data / 1000;
1849                 RICOH_FG_DBG( "battery voltage is %d mV\n",
1850                                                 info->cur_voltage);
1851                 break;
1852
1853 #ifdef  ENABLE_FUEL_GAUGE_FUNCTION
1854         /* current battery capacity is get from fuel gauge */
1855         case POWER_SUPPLY_PROP_CAPACITY:
1856                 val->intval = (info->soca->displayed_soc + 50)/100;
1857                 info->capacity = (info->soca->displayed_soc + 50)/100;
1858                 /* RICOH_FG_DBG("battery capacity is %d%%\n",
1859                                                         info->capacity); */
1860                 break;
1861
1862         /* current temperature of battery */
1863         case POWER_SUPPLY_PROP_TEMP:
1864                 if (info->soca->ready_fg) {
1865                         ret = get_battery_temp(info);
1866                         val->intval = ret;
1867                         info->battery_temp = ret/10;
1868                         RICOH_FG_DBG( "battery temperature is %d degree\n", info->battery_temp);
1869                 } else {
1870                         val->intval = -EINVAL;
1871                         RICOH_FG_DBG("battery temperature is not ready\n");
1872                 }
1873                 break;
1874
1875         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
1876                 if (info->soca->ready_fg) {
1877                         ret = get_time_to_empty(info);
1878                         val->intval = ret;
1879                         info->time_to_empty = ret/60;
1880                         RICOH_FG_DBG("time of empty battery is %d minutes\n", info->time_to_empty);
1881                 } else {
1882                         val->intval = -EINVAL;
1883                         RICOH_FG_DBG( "time of empty battery is not ready\n");
1884                 }
1885                 break;
1886
1887          case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
1888                 if (info->soca->ready_fg) {
1889                         ret = get_time_to_full(info);
1890                         val->intval = ret;
1891                         info->time_to_full = ret/60;
1892                         RICOH_FG_DBG( "time of full battery is %d minutes\n", info->time_to_full);
1893                 } else {
1894                         val->intval = -EINVAL;
1895                         RICOH_FG_DBG("time of full battery is not ready\n");
1896                 }
1897                 break;
1898 #endif
1899          case POWER_SUPPLY_PROP_TECHNOLOGY:
1900                 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
1901                 ret = 0;
1902                 break;
1903
1904         case POWER_SUPPLY_PROP_HEALTH:
1905                 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1906                 ret = 0;
1907                 break;
1908
1909         default:
1910                 mutex_unlock(&info->lock);
1911                 return -ENODEV;
1912         }
1913
1914         mutex_unlock(&info->lock);
1915
1916         return ret;
1917 }
1918
1919 static enum power_supply_property ricoh619_batt_props[] = {
1920         POWER_SUPPLY_PROP_STATUS,
1921         POWER_SUPPLY_PROP_PRESENT,
1922         POWER_SUPPLY_PROP_VOLTAGE_NOW,
1923
1924 #ifdef  ENABLE_FUEL_GAUGE_FUNCTION
1925         POWER_SUPPLY_PROP_CAPACITY,
1926         POWER_SUPPLY_PROP_TEMP,
1927         POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
1928         POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
1929 #endif
1930         POWER_SUPPLY_PROP_TECHNOLOGY,
1931         POWER_SUPPLY_PROP_HEALTH,
1932 };
1933
1934 static enum power_supply_property ricoh619_power_props[] = {
1935         POWER_SUPPLY_PROP_ONLINE,
1936 };
1937
1938 struct power_supply     powerac = {
1939                 .name = "acpwr",
1940                 .type = POWER_SUPPLY_TYPE_MAINS,
1941                 .properties = ricoh619_power_props,
1942                 .num_properties = ARRAY_SIZE(ricoh619_power_props),
1943                 .get_property = ricoh619_batt_get_prop,
1944 };
1945
1946 struct power_supply     powerusb = {
1947                 .name = "usbpwr",
1948                 .type = POWER_SUPPLY_TYPE_USB,
1949                 .properties = ricoh619_power_props,
1950                 .num_properties = ARRAY_SIZE(ricoh619_power_props),
1951                 .get_property = ricoh619_batt_get_prop,
1952 };
1953
1954 static __devinit int ricoh619_battery_probe(struct platform_device *pdev)
1955 {
1956         struct ricoh619_battery_info *info;
1957         struct ricoh619_battery_platform_data *pdata;
1958         int ret;
1959
1960         RICOH_FG_DBG("PMU: %s\n", __func__);
1961
1962         info = kzalloc(sizeof(struct ricoh619_battery_info), GFP_KERNEL);
1963         if (!info)
1964                 return -ENOMEM;
1965         info->soca = kzalloc(sizeof(struct ricoh619_soca_info), GFP_KERNEL);
1966                 if (!info->soca)
1967                         return -ENOMEM;
1968
1969         info->dev = &pdev->dev;
1970         info->status = POWER_SUPPLY_STATUS_CHARGING;
1971         pdata = pdev->dev.platform_data;
1972         info->monitor_time = pdata->monitor_time * HZ;
1973         info->alarm_vol_mv = pdata->alarm_vol_mv;
1974         info->adc_vdd_mv = ADC_VDD_MV;          /* 2800; */
1975         info->min_voltage = MIN_VOLTAGE;        /* 3100; */
1976         info->max_voltage = MAX_VOLTAGE;        /* 4200; */
1977         info->delay = 500;
1978         info->entry_factory_mode = false;
1979
1980         mutex_init(&info->lock);
1981         platform_set_drvdata(pdev, info);
1982
1983         info->battery.name = "battery";
1984         info->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1985         info->battery.properties = ricoh619_batt_props;
1986         info->battery.num_properties = ARRAY_SIZE(ricoh619_batt_props);
1987         info->battery.get_property = ricoh619_batt_get_prop;
1988         info->battery.set_property = NULL;
1989 /*      info->battery.external_power_changed
1990                  = ricoh619_external_power_changed; */
1991
1992         /* Disable Charger/ADC interrupt */
1993         ret = ricoh619_clr_bits(info->dev->parent, RICOH619_INTC_INTEN,
1994                                                          CHG_INT | ADC_INT);
1995         if (ret)
1996                 goto out;
1997
1998         ret = ricoh619_init_battery(info);
1999         if (ret)
2000                 goto out;
2001
2002 #ifdef ENABLE_FACTORY_MODE
2003         info->factory_mode_wqueue
2004                 = create_singlethread_workqueue("ricoh619_factory_mode");
2005         INIT_DELAYED_WORK_DEFERRABLE(&info->factory_mode_work,
2006                                          check_charging_state_work);
2007
2008         ret = ricoh619_factory_mode(info);
2009         if (ret)
2010                 goto out;
2011
2012 #endif
2013
2014         ret = power_supply_register(&pdev->dev, &info->battery);
2015
2016         if (ret)
2017                 info->battery.dev->parent = &pdev->dev;
2018
2019         ret = power_supply_register(&pdev->dev, &powerac);
2020         ret = power_supply_register(&pdev->dev, &powerusb);
2021
2022         info->monitor_wqueue
2023                 = create_singlethread_workqueue("ricoh619_battery_monitor");
2024         INIT_DELAYED_WORK_DEFERRABLE(&info->monitor_work,
2025                                          ricoh619_battery_work);
2026         INIT_DELAYED_WORK_DEFERRABLE(&info->displayed_work,
2027                                          ricoh619_displayed_work);
2028         INIT_DELAYED_WORK_DEFERRABLE(&info->charge_stable_work,
2029                                          ricoh619_stable_charge_countdown_work);
2030         INIT_DELAYED_WORK(&info->changed_work, ricoh619_changed_work);
2031         queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
2032                                         RICOH619_MONITOR_START_TIME*HZ);
2033
2034
2035         /* Charger IRQ workqueue settings */
2036         charger_irq = pdata->irq;
2037
2038         info->workqueue = create_singlethread_workqueue("rc5t619_charger_in");
2039         INIT_WORK(&info->irq_work, charger_irq_work);
2040
2041         ret = request_threaded_irq(charger_irq + RICOH619_IRQ_FONCHGINT,
2042                                         NULL, charger_in_isr, IRQF_ONESHOT,
2043                                                 "rc5t619_charger_in", info);
2044         if (ret < 0) {
2045                 dev_err(&pdev->dev, "Can't get CHG_INT IRQ for chrager: %d\n",
2046                                                                         ret);
2047                 goto out;
2048         }
2049         info->workqueue = create_singlethread_workqueue("rc5t619_charger_in");
2050         INIT_WORK(&info->irq_work, charger_irq_work);
2051
2052         ret = request_threaded_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT,
2053                                                 NULL, charger_complete_isr,
2054                                         IRQF_ONESHOT, "rc5t619_charger_comp",
2055                                                                 info);
2056         if (ret < 0) {
2057                 dev_err(&pdev->dev, "Can't get CHG_COMP IRQ for chrager: %d\n",
2058                                                                          ret);
2059                 goto out;
2060         }
2061
2062         ret = request_threaded_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT,
2063                                         NULL, charger_usb_isr, IRQF_ONESHOT,
2064                                                 "rc5t619_usb_det", info);
2065         if (ret < 0) {
2066                 dev_err(&pdev->dev, "Can't get USB_DET IRQ for chrager: %d\n",
2067                                                                          ret);
2068                 goto out;
2069         }
2070
2071         ret = request_threaded_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT,
2072                                         NULL, charger_adp_isr, IRQF_ONESHOT,
2073                                                 "rc5t619_adp_det", info);
2074         if (ret < 0) {
2075                 dev_err(&pdev->dev,
2076                         "Can't get ADP_DET IRQ for chrager: %d\n", ret);
2077                 goto out;
2078         }
2079
2080         info->usb_workqueue
2081                 = create_singlethread_workqueue("rc5t619_usb_det");
2082         INIT_WORK(&info->usb_irq_work, usb_det_irq_work);
2083
2084
2085 #ifdef ENABLE_LOW_BATTERY_DETECTION
2086         ret = request_threaded_irq(charger_irq + RICOH619_IRQ_VSYSLIR,
2087                                         NULL, adc_vsysl_isr, IRQF_ONESHOT,
2088                                                 "rc5t619_adc_vsysl", info);
2089         if (ret < 0) {
2090                 dev_err(&pdev->dev,
2091                         "Can't get ADC_VSYSL IRQ for chrager: %d\n", ret);
2092                 goto out;
2093         }
2094         INIT_DELAYED_WORK_DEFERRABLE(&info->low_battery_work,
2095                                          low_battery_irq_work);
2096 #endif
2097
2098         /* Charger init and IRQ setting */
2099         ret = ricoh619_init_charger(info);
2100         if (ret)
2101                 goto out;
2102
2103 #ifdef  ENABLE_FUEL_GAUGE_FUNCTION
2104         ret = ricoh619_init_fgsoca(info);
2105 #endif
2106
2107         /* Enable Charger interrupt */
2108         ricoh619_set_bits(info->dev->parent, RICOH619_INTC_INTEN, CHG_INT);
2109
2110         return 0;
2111
2112 out:
2113         kfree(info);
2114         return ret;
2115 }
2116
2117 static int __devexit ricoh619_battery_remove(struct platform_device *pdev)
2118 {
2119         struct ricoh619_battery_info *info = platform_get_drvdata(pdev);
2120         uint8_t val;
2121         int ret;
2122         int err;
2123         int cc_cap = 0;
2124         bool is_charging = true;
2125
2126         if (g_fg_on_mode
2127                  && (info->soca->status == RICOH619_SOCA_STABLE)) {
2128                 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
2129                 if (err < 0)
2130                         dev_err(info->dev, "Error in writing PSWR_REG\n");
2131                 g_soc = 0x7f;
2132         } else {
2133                 val = (info->soca->displayed_soc + 50)/100;
2134                 val &= 0x7f;
2135                 ret = ricoh619_write(info->dev->parent, PSWR_REG, val);
2136                 if (ret < 0)
2137                         dev_err(info->dev, "Error in writing PSWR_REG\n");
2138
2139                 g_soc = (info->soca->displayed_soc + 50)/100;
2140
2141                 ret = calc_capacity_in_period(info, &cc_cap, &is_charging);
2142                 if (ret < 0)
2143                         dev_err(info->dev, "Read cc_sum Error !!-----\n");
2144         }
2145
2146         if (g_fg_on_mode == 0) {
2147                 ret = ricoh619_clr_bits(info->dev->parent,
2148                                          FG_CTRL_REG, 0x01);
2149                 if (ret < 0)
2150                         dev_err(info->dev, "Error in clr FG EN\n");
2151         }
2152
2153         cancel_delayed_work(&info->monitor_work);
2154         cancel_delayed_work(&info->charge_stable_work);
2155         cancel_delayed_work(&info->changed_work);
2156 #ifdef ENABLE_LOW_BATTERY_DETECTION
2157         cancel_delayed_work(&info->low_battery_work);
2158 #endif
2159
2160         flush_work(&info->irq_work);
2161         flush_work(&info->usb_irq_work);
2162
2163         flush_workqueue(info->monitor_wqueue);
2164         flush_workqueue(info->workqueue);
2165         flush_workqueue(info->usb_workqueue);
2166
2167         destroy_workqueue(info->monitor_wqueue);
2168         destroy_workqueue(info->workqueue);
2169         destroy_workqueue(info->usb_workqueue);
2170
2171         power_supply_unregister(&info->battery);
2172         kfree(info);
2173         platform_set_drvdata(pdev, NULL);
2174         return 0;
2175 }
2176
2177 #ifdef CONFIG_PM
2178 static int ricoh619_battery_suspend(struct device *dev)
2179 {
2180         struct ricoh619_battery_info *info = dev_get_drvdata(dev);
2181         uint8_t val;
2182         int ret;
2183         int err;
2184         int cc_cap = 0;
2185         bool is_charging = true;
2186
2187         if (g_fg_on_mode
2188                  && (info->soca->status == RICOH619_SOCA_STABLE)) {
2189                 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
2190                 if (err < 0)
2191                         dev_err(info->dev, "Error in writing PSWR_REG\n");
2192                  g_soc = 0x7F;
2193         } else {
2194                 val = (info->soca->displayed_soc + 50)/100;
2195                 val &= 0x7f;
2196                 ret = ricoh619_write(info->dev->parent, PSWR_REG, val);
2197                 if (ret < 0)
2198                         dev_err(info->dev, "Error in writing PSWR_REG\n");
2199
2200                 g_soc = (info->soca->displayed_soc + 50)/100;
2201
2202                 ret = calc_capacity_in_period(info, &cc_cap, &is_charging);
2203                 if (ret < 0)
2204                         dev_err(info->dev, "Read cc_sum Error !!-----\n");
2205         }
2206
2207         disable_irq(charger_irq + RICOH619_IRQ_FONCHGINT);
2208         disable_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT);
2209         disable_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT);
2210         disable_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT);
2211 #ifdef ENABLE_LOW_BATTERY_DETECTION
2212         disable_irq(charger_irq + RICOH619_IRQ_VSYSLIR);
2213 #endif
2214
2215         cancel_delayed_work_sync(&info->monitor_work);
2216         cancel_delayed_work_sync(&info->displayed_work);
2217         cancel_delayed_work_sync(&info->charge_stable_work);
2218         cancel_delayed_work_sync(&info->changed_work);
2219 #ifdef ENABLE_LOW_BATTERY_DETECTION
2220         cancel_delayed_work_sync(&info->low_battery_work);
2221 #endif
2222         cancel_work_sync(&info->irq_work);
2223         cancel_work_sync(&info->usb_irq_work);
2224
2225         flush_workqueue(info->monitor_wqueue);
2226         flush_workqueue(info->workqueue);
2227         flush_workqueue(info->usb_workqueue);
2228
2229         return 0;
2230 }
2231
2232 static int ricoh619_battery_resume(struct device *dev)
2233 {
2234         struct ricoh619_battery_info *info = dev_get_drvdata(dev);
2235         uint8_t val;
2236         int ret;
2237         int displayed_soc_temp;
2238         int cc_cap = 0;
2239         bool is_charging = true;
2240         int i;
2241
2242         if (info->entry_factory_mode) {
2243                 info->soca->displayed_soc = -EINVAL;
2244         } else if (RICOH619_SOCA_STABLE == info->soca->status) {
2245                 info->soca->soc = calc_capacity(info) * 100;
2246                 info->soca->displayed_soc = info->soca->soc;
2247         } else if (RICOH619_SOCA_ZERO == info->soca->status) {
2248                 if (calc_ocv(info) > get_OCV_voltage(info, 0)) {
2249                         ret = ricoh619_read(info->dev->parent, PSWR_REG, &val);
2250                         val &= 0x7f;
2251                         info->soca->soc = val * 100;
2252                         if ((ret < 0) || (val == 0)) {
2253                                 dev_err(info->dev,
2254                                          "Error in reading PSWR_REG %d\n", ret);
2255                                 info->soca->soc
2256                                          = calc_capacity(info) * 100 + 50;
2257                         }
2258
2259                         ret = calc_capacity_in_period(info, &cc_cap,
2260                                                                  &is_charging);
2261                         if (ret < 0)
2262                                 dev_err(info->dev, "Read cc_sum Error !!-----\n");
2263
2264                         info->soca->cc_delta
2265                                  = (is_charging == true) ? cc_cap : -cc_cap;
2266
2267                         displayed_soc_temp
2268                                  = info->soca->soc + info->soca->cc_delta;
2269                         displayed_soc_temp = min(10000, displayed_soc_temp);
2270                         displayed_soc_temp = max(0, displayed_soc_temp);
2271                         info->soca->displayed_soc = displayed_soc_temp;
2272
2273                         ret = ricoh619_write(info->dev->parent,
2274                                                          FG_CTRL_REG, 0x51);
2275                         if (ret < 0)
2276                                 dev_err(info->dev, "Error in writing the control register\n");
2277                         info->soca->ready_fg = 0;
2278                         info->soca->status = RICOH619_SOCA_FG_RESET;
2279
2280                 } else
2281                         info->soca->displayed_soc = 0;
2282         } else {
2283                 ret = ricoh619_read(info->dev->parent, PSWR_REG, &val);
2284                 val &= 0x7f;
2285                 info->soca->soc = val * 100;
2286                 if ((ret < 0) || (val == 0)) {
2287                         dev_err(info->dev,
2288                                  "Error in reading PSWR_REG %d\n", ret);
2289                         info->soca->soc
2290                                  = calc_capacity(info) * 100 + 50;
2291                 }
2292
2293                 ret = calc_capacity_in_period(info, &cc_cap, &is_charging);
2294                 if (ret < 0)
2295                         dev_err(info->dev, "Read cc_sum Error !!-----\n");
2296
2297                 info->soca->cc_delta = (is_charging == true) ? cc_cap : -cc_cap;
2298
2299                 displayed_soc_temp = info->soca->soc + info->soca->cc_delta;
2300                 displayed_soc_temp = min(10000, displayed_soc_temp);
2301                 displayed_soc_temp = max(0, displayed_soc_temp);
2302                 info->soca->displayed_soc = displayed_soc_temp;
2303                 if (RICOH619_SOCA_DISP == info->soca->status)
2304                         info->soca->last_soc = calc_capacity(info) * 100;
2305         }
2306
2307         power_supply_changed(&info->battery);
2308         queue_delayed_work(info->monitor_wqueue, &info->displayed_work, HZ);
2309
2310         if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
2311                 info->soca->stable_count = 10;
2312                 queue_delayed_work(info->monitor_wqueue,
2313                                          &info->charge_stable_work,
2314                                          RICOH619_FG_STABLE_TIME*HZ/10);
2315         } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
2316                 info->soca->stable_count = 1;
2317
2318                 for (i = 0; i < 3; i = i+1)
2319                         info->soca->reset_soc[i] = 0;
2320                 info->soca->reset_count = 0;
2321
2322                 queue_delayed_work(info->monitor_wqueue,
2323                                          &info->charge_stable_work,
2324                                          RICOH619_FG_RESET_TIME*HZ);
2325         }
2326
2327         queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
2328                                                  info->monitor_time);
2329
2330         enable_irq(charger_irq + RICOH619_IRQ_FONCHGINT);
2331         enable_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT);
2332         enable_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT);
2333         enable_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT);
2334 #ifdef ENABLE_LOW_BATTERY_DETECTION
2335         enable_irq(charger_irq + RICOH619_IRQ_VSYSLIR);
2336 #endif
2337         return 0;
2338 }
2339
2340 static const struct dev_pm_ops ricoh619_battery_pm_ops = {
2341         .suspend        = ricoh619_battery_suspend,
2342         .resume         = ricoh619_battery_resume,
2343 };
2344 #endif
2345
2346 static struct platform_driver ricoh619_battery_driver = {
2347         .driver = {
2348                                 .name   = "ricoh619-battery",
2349                                 .owner  = THIS_MODULE,
2350 #ifdef CONFIG_PM
2351                                 .pm     = &ricoh619_battery_pm_ops,
2352 #endif
2353         },
2354         .probe  = ricoh619_battery_probe,
2355         .remove = __devexit_p(ricoh619_battery_remove),
2356 };
2357
2358 static int __init ricoh619_battery_init(void)
2359 {
2360         RICOH_FG_DBG("PMU: %s\n", __func__);
2361         return platform_driver_register(&ricoh619_battery_driver);
2362 }
2363 subsys_initcall_sync(ricoh619_battery_init);
2364
2365 static void __exit ricoh619_battery_exit(void)
2366 {
2367         platform_driver_unregister(&ricoh619_battery_driver);
2368 }
2369 module_exit(ricoh619_battery_exit);
2370
2371 MODULE_DESCRIPTION("RICOH619 Battery driver");
2372 MODULE_ALIAS("platform:ricoh619-battery");
2373 MODULE_LICENSE("GPL");