ricoh619:support pmic ricoh619
[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 static void usb_det_irq_work(struct work_struct *work)
1374 {
1375         struct ricoh619_battery_info *info = container_of(work,
1376                  struct ricoh619_battery_info, usb_irq_work);
1377         int ret = 0;
1378         uint8_t sts;
1379         int time =0;
1380
1381         RICOH_FG_DBG("PMU:%s In\n", __func__);
1382
1383         power_supply_changed(&info->battery);
1384
1385         mutex_lock(&info->lock);
1386
1387         /* Enable Interrupt for VUSB */
1388         ret = ricoh619_clr_bits(info->dev->parent,
1389                                          RICOH619_INT_MSK_CHGCTR, 0x02);
1390         if (ret < 0)
1391                 dev_err(info->dev,
1392                          "%s(): Error in enable charger mask INT %d\n",
1393                          __func__, ret);
1394
1395         mutex_unlock(&info->lock);
1396
1397         ret = ricoh619_read(info->dev->parent, RICOH619_INT_MON_CHGCTR, &sts);
1398         if (ret < 0)
1399                 dev_err(info->dev, "Error in reading the control register\n");
1400
1401         sts &= 0x02;
1402         if (sts) {
1403         
1404         } else {
1405                 /*********************/
1406                 /* No process ??     */
1407                 /*********************/
1408         }
1409         
1410         RICOH_FG_DBG("PMU:%s Out\n", __func__);
1411 }
1412
1413 extern void rk28_send_wakeup_key(void); 
1414 static irqreturn_t charger_in_isr(int irq, void *battery_info)
1415 {
1416         struct ricoh619_battery_info *info = battery_info;
1417         printk("PMU:%s\n", __func__);
1418
1419         info->chg_stat1 |= 0x01;
1420         queue_work(info->workqueue, &info->irq_work);
1421          rk28_send_wakeup_key(); 
1422         return IRQ_HANDLED;
1423 }
1424
1425 static irqreturn_t charger_complete_isr(int irq, void *battery_info)
1426 {
1427         struct ricoh619_battery_info *info = battery_info;
1428         printk("PMU:%s\n", __func__);
1429
1430         info->chg_stat1 |= 0x02;
1431         queue_work(info->workqueue, &info->irq_work);
1432
1433         return IRQ_HANDLED;
1434 }
1435
1436 static irqreturn_t charger_usb_isr(int irq, void *battery_info)
1437 {
1438         struct ricoh619_battery_info *info = battery_info;
1439         printk("PMU:%s\n", __func__);
1440
1441         info->chg_ctr |= 0x02;
1442         queue_work(info->usb_workqueue, &info->usb_irq_work);
1443          rk28_send_wakeup_key(); 
1444
1445         if (RICOH619_SOCA_UNSTABLE == info->soca->status
1446                 || RICOH619_SOCA_FG_RESET == info->soca->status)
1447                 info->soca->stable_count = 11;
1448         
1449         return IRQ_HANDLED;
1450 }
1451
1452 static irqreturn_t charger_adp_isr(int irq, void *battery_info)
1453 {
1454         struct ricoh619_battery_info *info = battery_info;
1455         struct ricoh619 *ricoh619 = g_ricoh619;
1456         printk("PMU:%s\n", __func__);
1457
1458         info->chg_ctr |= 0x01;
1459         queue_work(info->workqueue, &info->irq_work);
1460         rk28_send_wakeup_key(); 
1461         /* clr usb det irq */
1462         ricoh619_clr_bits(ricoh619->dev, RICOH619_INT_IR_CHGCTR,
1463                                                          info->chg_ctr);
1464
1465         /* set adp limit current 2A */
1466         ricoh619_write(ricoh619->dev, 0xb6, 0x13);
1467         /* set charge current 2A */
1468         ricoh619_write(ricoh619->dev, 0xb8, 0x13);
1469
1470         if (RICOH619_SOCA_UNSTABLE == info->soca->status
1471                 || RICOH619_SOCA_FG_RESET == info->soca->status)
1472                 info->soca->stable_count = 11;
1473
1474         return IRQ_HANDLED;
1475 }
1476
1477
1478 #ifdef ENABLE_LOW_BATTERY_DETECTION
1479 /*************************************************************/
1480 /* for Detecting Low Battery                                 */
1481 /*************************************************************/
1482
1483 static irqreturn_t adc_vsysl_isr(int irq, void *battery_info)
1484 {
1485
1486         struct ricoh619_battery_info *info = battery_info;
1487
1488 #if 0
1489         RICOH_FG_DBG("PMU:%s\n", __func__);
1490
1491         queue_delayed_work(info->monitor_wqueue, &info->low_battery_work,
1492                                         LOW_BATTERY_DETECTION_TIME*HZ);
1493
1494 #endif
1495
1496         RICOH_FG_DBG("PMU:%s\n", __func__);
1497         ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
1498         rk28_send_wakeup_key(); 
1499
1500         return IRQ_HANDLED;
1501 }
1502 #endif
1503
1504
1505 #ifdef  ENABLE_FUEL_GAUGE_FUNCTION
1506 static int get_check_fuel_gauge_reg(struct ricoh619_battery_info *info,
1507                                          int Reg_h, int Reg_l, int enable_bit)
1508 {
1509         uint8_t get_data_h, get_data_l;
1510         int old_data, current_data;
1511         int i;
1512         int ret = 0;
1513
1514         old_data = 0;
1515
1516         for (i = 0; i < 5 ; i++) {
1517                 ret = ricoh619_read(info->dev->parent, Reg_h, &get_data_h);
1518                 if (ret < 0) {
1519                         dev_err(info->dev, "Error in reading the control register\n");
1520                         return ret;
1521                 }
1522
1523                 ret = ricoh619_read(info->dev->parent, Reg_l, &get_data_l);
1524                 if (ret < 0) {
1525                         dev_err(info->dev, "Error in reading the control register\n");
1526                         return ret;
1527                 }
1528
1529                 current_data = ((get_data_h & 0xff) << 8) | (get_data_l & 0xff);
1530                 current_data = (current_data & enable_bit);
1531
1532                 if (current_data == old_data)
1533                         return current_data;
1534                 else
1535                         old_data = current_data;
1536         }
1537
1538         return current_data;
1539 }
1540
1541 static int calc_capacity(struct ricoh619_battery_info *info)
1542 {
1543         uint8_t capacity;
1544         int temp;
1545         int ret = 0;
1546
1547         /* get remaining battery capacity from fuel gauge */
1548         ret = ricoh619_read(info->dev->parent, SOC_REG, &capacity);
1549         if (ret < 0) {
1550                 dev_err(info->dev, "Error in reading the control register\n");
1551                 return ret;
1552         }
1553
1554         temp = capacity;
1555
1556         return temp;            /* Unit is 1% */
1557 }
1558
1559 static int get_battery_temp(struct ricoh619_battery_info *info)
1560 {
1561         int ret = 0;
1562         int sign_bit;
1563
1564         ret = get_check_fuel_gauge_reg(info, TEMP_1_REG, TEMP_2_REG, 0x0fff);
1565         if (ret < 0) {
1566                 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
1567                 return ret;
1568         }
1569
1570         /* bit3 of 0xED(TEMP_1) is sign_bit */
1571         sign_bit = ((ret & 0x0800) >> 11);
1572
1573         ret = (ret & 0x07ff);
1574
1575         if (sign_bit == 0)      /* positive value part */
1576                 /* conversion unit */
1577                 /* 1 unit is 0.0625 degree and retun unit
1578                  * should be 0.1 degree,
1579                  */
1580                 ret = ret * 625  / 1000;
1581         else    /*negative value part */
1582                 ret = -1 * ret * 625 / 1000;
1583
1584         return ret;
1585 }
1586
1587 static int get_time_to_empty(struct ricoh619_battery_info *info)
1588 {
1589         int ret = 0;
1590
1591         ret = get_check_fuel_gauge_reg(info, TT_EMPTY_H_REG, TT_EMPTY_L_REG,
1592                                                                 0xffff);
1593         if (ret < 0) {
1594                 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
1595                 return ret;
1596         }
1597
1598         /* conversion unit */
1599         /* 1unit is 1miniute and return nnit should be 1 second */
1600         ret = ret * 60;
1601
1602         return ret;
1603 }
1604
1605 static int get_time_to_full(struct ricoh619_battery_info *info)
1606 {
1607         int ret = 0;
1608
1609         ret = get_check_fuel_gauge_reg(info, TT_FULL_H_REG, TT_FULL_L_REG,
1610                                                                 0xffff);
1611         if (ret < 0) {
1612                 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
1613                 return ret;
1614         }
1615
1616         ret = ret * 60;
1617
1618         return  ret;
1619 }
1620
1621 /* battery voltage is get from Fuel gauge */
1622 static int measure_vbatt_FG(struct ricoh619_battery_info *info, int *data)
1623 {
1624         int ret = 0;
1625
1626         ret = get_check_fuel_gauge_reg(info, VOLTAGE_1_REG, VOLTAGE_2_REG,
1627                                                                 0x0fff);
1628         if (ret < 0) {
1629                 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
1630                 return ret;
1631         }
1632
1633         *data = ret;
1634         /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
1635         *data = *data * 50000 / 4095;
1636         /* return unit should be 1uV */
1637         *data = *data * 100;
1638
1639         return ret;
1640 }
1641
1642 static int measure_Ibatt_FG(struct ricoh619_battery_info *info, int *data)
1643 {
1644         int ret = 0;
1645
1646         ret =  get_check_fuel_gauge_reg(info, CC_AVERAGE1_REG,
1647                                                  CC_AVERAGE0_REG, 0x3fff);
1648         if (ret < 0) {
1649                 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
1650                 return ret;
1651         }
1652
1653         *data = (ret > 0x1fff) ? (ret - 0x4000) : ret;
1654         return ret;
1655 }
1656
1657 static int get_OCV_init_Data(struct ricoh619_battery_info *info, int index)
1658 {
1659         int ret = 0;
1660         ret =  (battery_init_para[index*2]<<8) | (battery_init_para[index*2+1]);
1661         return ret;
1662 }
1663
1664 static int get_OCV_voltage(struct ricoh619_battery_info *info, int index)
1665 {
1666         int ret = 0;
1667         ret =  get_OCV_init_Data(info, index);
1668         /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
1669         ret = ret * 50000 / 4095;
1670         /* return unit should be 1uV */
1671         ret = ret * 100;
1672         return ret;
1673 }
1674
1675 #else
1676 /* battery voltage is get from ADC */
1677 static int measure_vbatt_ADC(struct ricoh619_battery_info *info, int *data)
1678 {
1679         int     i;
1680         uint8_t data_l = 0, data_h = 0;
1681         int ret;
1682
1683         /* ADC interrupt enable */
1684         ret = ricoh619_set_bits(info->dev->parent, INTEN_REG, 0x08);
1685         if (ret < 0) {
1686                 dev_err(info->dev, "Error in setting the control register bit\n");
1687                 goto err;
1688         }
1689
1690         /* enable interrupt request of single mode */
1691         ret = ricoh619_set_bits(info->dev->parent, EN_ADCIR3_REG, 0x01);
1692         if (ret < 0) {
1693                 dev_err(info->dev, "Error in setting the control register bit\n");
1694                 goto err;
1695         }
1696
1697         /* single request */
1698         ret = ricoh619_write(info->dev->parent, ADCCNT3_REG, 0x10);
1699         if (ret < 0) {
1700                 dev_err(info->dev, "Error in writing the control register\n");
1701                 goto err;
1702         }
1703
1704         for (i = 0; i < 5; i++) {
1705                 usleep(1000);
1706                 RICOH_FG_DBG("ADC conversion times: %d\n", i);
1707                 /* read completed flag of ADC */
1708                 ret = ricoh619_read(info->dev->parent, EN_ADCIR3_REG, &data_h);
1709                 if (ret < 0) {
1710                         dev_err(info->dev, "Error in reading the control register\n");
1711                         goto err;
1712                 }
1713
1714                 if (data_h & 0x01)
1715                         goto    done;
1716         }
1717
1718         dev_err(info->dev, "ADC conversion too long!\n");
1719         goto err;
1720
1721 done:
1722         ret = ricoh619_read(info->dev->parent, VBATDATAH_REG, &data_h);
1723         if (ret < 0) {
1724                 dev_err(info->dev, "Error in reading the control register\n");
1725                 goto err;
1726         }
1727
1728         ret = ricoh619_read(info->dev->parent, VBATDATAL_REG, &data_l);
1729         if (ret < 0) {
1730                 dev_err(info->dev, "Error in reading the control register\n");
1731                 goto err;
1732         }
1733
1734         *data = ((data_h & 0xff) << 4) | (data_l & 0x0f);
1735         /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
1736         *data = *data * 5000 / 4095;
1737         /* return unit should be 1uV */
1738         *data = *data * 1000;
1739
1740         return 0;
1741
1742 err:
1743         return -1;
1744 }
1745 #endif
1746
1747 /*
1748 static void ricoh619_external_power_changed(struct power_supply *psy)
1749 {
1750         struct ricoh619_battery_info *info;
1751
1752         info = container_of(psy, struct ricoh619_battery_info, battery);
1753         queue_delayed_work(info->monitor_wqueue,
1754                            &info->changed_work, HZ / 2);
1755         return;
1756 }
1757 */
1758
1759 static int ricoh619_batt_get_prop(struct power_supply *psy,
1760                                 enum power_supply_property psp,
1761                                 union power_supply_propval *val)
1762 {
1763         struct ricoh619_battery_info *info = dev_get_drvdata(psy->dev->parent);
1764         int data = 0;
1765         int ret = 0;
1766         uint8_t status;
1767
1768         mutex_lock(&info->lock);
1769
1770         switch (psp) {
1771         case POWER_SUPPLY_PROP_ONLINE:
1772                 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
1773                 if (ret < 0) {
1774                         dev_err(info->dev, "Error in reading the control register\n");
1775                         mutex_unlock(&info->lock);
1776                         return ret;
1777                 }
1778                 if (psy->type == POWER_SUPPLY_TYPE_MAINS)
1779                         val->intval = (status & 0x40 ? 1 : 0);
1780                 else if (psy->type == POWER_SUPPLY_TYPE_USB)
1781                         val->intval = (status & 0x80 ? 1 : 0);
1782                 break;
1783         /* this setting is same as battery driver of 584 */
1784         case POWER_SUPPLY_PROP_STATUS:
1785                 ret = get_power_supply_status(info);
1786                 val->intval = ret;
1787                 info->status = ret;
1788                 /* RICOH_FG_DBG("Power Supply Status is %d\n",
1789                                                         info->status); */
1790                 break;
1791
1792         /* this setting is same as battery driver of 584 */
1793         case POWER_SUPPLY_PROP_PRESENT:
1794                 val->intval = info->present;
1795                 break;
1796
1797         /* current voltage is get from fuel gauge */
1798         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
1799                 /* return real vbatt Voltage */
1800 #ifdef  ENABLE_FUEL_GAUGE_FUNCTION
1801                 if (info->soca->ready_fg)
1802                         ret = measure_vbatt_FG(info, &data);
1803                 else {
1804                         val->intval = -EINVAL;
1805                         RICOH_FG_DBG( "battery voltage is not ready\n");
1806                         break;
1807                 }
1808 #else
1809                 ret = measure_vbatt_ADC(info, &data);
1810 #endif
1811                 val->intval = data;
1812                 /* convert unit uV -> mV */
1813                 info->cur_voltage = data / 1000;
1814                 RICOH_FG_DBG( "battery voltage is %d mV\n",
1815                                                 info->cur_voltage);
1816                 break;
1817
1818 #ifdef  ENABLE_FUEL_GAUGE_FUNCTION
1819         /* current battery capacity is get from fuel gauge */
1820         case POWER_SUPPLY_PROP_CAPACITY:
1821                 val->intval = (info->soca->displayed_soc + 50)/100;
1822                 info->capacity = (info->soca->displayed_soc + 50)/100;
1823                 /* RICOH_FG_DBG("battery capacity is %d%%\n",
1824                                                         info->capacity); */
1825                 break;
1826
1827         /* current temperature of battery */
1828         case POWER_SUPPLY_PROP_TEMP:
1829                 if (info->soca->ready_fg) {
1830                         ret = get_battery_temp(info);
1831                         val->intval = ret;
1832                         info->battery_temp = ret/10;
1833                         RICOH_FG_DBG( "battery temperature is %d degree\n", info->battery_temp);
1834                 } else {
1835                         val->intval = -EINVAL;
1836                         RICOH_FG_DBG("battery temperature is not ready\n");
1837                 }
1838                 break;
1839
1840         case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
1841                 if (info->soca->ready_fg) {
1842                         ret = get_time_to_empty(info);
1843                         val->intval = ret;
1844                         info->time_to_empty = ret/60;
1845                         RICOH_FG_DBG("time of empty battery is %d minutes\n", info->time_to_empty);
1846                 } else {
1847                         val->intval = -EINVAL;
1848                         RICOH_FG_DBG( "time of empty battery is not ready\n");
1849                 }
1850                 break;
1851
1852          case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
1853                 if (info->soca->ready_fg) {
1854                         ret = get_time_to_full(info);
1855                         val->intval = ret;
1856                         info->time_to_full = ret/60;
1857                         RICOH_FG_DBG( "time of full battery is %d minutes\n", info->time_to_full);
1858                 } else {
1859                         val->intval = -EINVAL;
1860                         RICOH_FG_DBG("time of full battery is not ready\n");
1861                 }
1862                 break;
1863 #endif
1864          case POWER_SUPPLY_PROP_TECHNOLOGY:
1865                 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
1866                 ret = 0;
1867                 break;
1868
1869         case POWER_SUPPLY_PROP_HEALTH:
1870                 val->intval = POWER_SUPPLY_HEALTH_GOOD;
1871                 ret = 0;
1872                 break;
1873
1874         default:
1875                 mutex_unlock(&info->lock);
1876                 return -ENODEV;
1877         }
1878
1879         mutex_unlock(&info->lock);
1880
1881         return ret;
1882 }
1883
1884 static enum power_supply_property ricoh619_batt_props[] = {
1885         POWER_SUPPLY_PROP_STATUS,
1886         POWER_SUPPLY_PROP_PRESENT,
1887         POWER_SUPPLY_PROP_VOLTAGE_NOW,
1888
1889 #ifdef  ENABLE_FUEL_GAUGE_FUNCTION
1890         POWER_SUPPLY_PROP_CAPACITY,
1891         POWER_SUPPLY_PROP_TEMP,
1892         POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
1893         POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
1894 #endif
1895         POWER_SUPPLY_PROP_TECHNOLOGY,
1896         POWER_SUPPLY_PROP_HEALTH,
1897 };
1898
1899 static enum power_supply_property ricoh619_power_props[] = {
1900         POWER_SUPPLY_PROP_ONLINE,
1901 };
1902
1903 struct power_supply     powerac = {
1904                 .name = "acpwr",
1905                 .type = POWER_SUPPLY_TYPE_MAINS,
1906                 .properties = ricoh619_power_props,
1907                 .num_properties = ARRAY_SIZE(ricoh619_power_props),
1908                 .get_property = ricoh619_batt_get_prop,
1909 };
1910
1911 struct power_supply     powerusb = {
1912                 .name = "usbpwr",
1913                 .type = POWER_SUPPLY_TYPE_USB,
1914                 .properties = ricoh619_power_props,
1915                 .num_properties = ARRAY_SIZE(ricoh619_power_props),
1916                 .get_property = ricoh619_batt_get_prop,
1917 };
1918
1919 static __devinit int ricoh619_battery_probe(struct platform_device *pdev)
1920 {
1921         struct ricoh619_battery_info *info;
1922         struct ricoh619_battery_platform_data *pdata;
1923         int ret;
1924
1925         RICOH_FG_DBG("PMU: %s\n", __func__);
1926
1927         info = kzalloc(sizeof(struct ricoh619_battery_info), GFP_KERNEL);
1928         if (!info)
1929                 return -ENOMEM;
1930         info->soca = kzalloc(sizeof(struct ricoh619_soca_info), GFP_KERNEL);
1931                 if (!info->soca)
1932                         return -ENOMEM;
1933
1934         info->dev = &pdev->dev;
1935         info->status = POWER_SUPPLY_STATUS_CHARGING;
1936         pdata = pdev->dev.platform_data;
1937         info->monitor_time = pdata->monitor_time * HZ;
1938         info->alarm_vol_mv = pdata->alarm_vol_mv;
1939         info->adc_vdd_mv = ADC_VDD_MV;          /* 2800; */
1940         info->min_voltage = MIN_VOLTAGE;        /* 3100; */
1941         info->max_voltage = MAX_VOLTAGE;        /* 4200; */
1942         info->delay = 500;
1943         info->entry_factory_mode = false;
1944
1945         mutex_init(&info->lock);
1946         platform_set_drvdata(pdev, info);
1947
1948         info->battery.name = "battery";
1949         info->battery.type = POWER_SUPPLY_TYPE_BATTERY;
1950         info->battery.properties = ricoh619_batt_props;
1951         info->battery.num_properties = ARRAY_SIZE(ricoh619_batt_props);
1952         info->battery.get_property = ricoh619_batt_get_prop;
1953         info->battery.set_property = NULL;
1954 /*      info->battery.external_power_changed
1955                  = ricoh619_external_power_changed; */
1956
1957         /* Disable Charger/ADC interrupt */
1958         ret = ricoh619_clr_bits(info->dev->parent, RICOH619_INTC_INTEN,
1959                                                          CHG_INT | ADC_INT);
1960         if (ret)
1961                 goto out;
1962
1963         ret = ricoh619_init_battery(info);
1964         if (ret)
1965                 goto out;
1966
1967 #ifdef ENABLE_FACTORY_MODE
1968         info->factory_mode_wqueue
1969                 = create_singlethread_workqueue("ricoh619_factory_mode");
1970         INIT_DELAYED_WORK_DEFERRABLE(&info->factory_mode_work,
1971                                          check_charging_state_work);
1972
1973         ret = ricoh619_factory_mode(info);
1974         if (ret)
1975                 goto out;
1976
1977 #endif
1978
1979         ret = power_supply_register(&pdev->dev, &info->battery);
1980
1981         if (ret)
1982                 info->battery.dev->parent = &pdev->dev;
1983
1984         ret = power_supply_register(&pdev->dev, &powerac);
1985         ret = power_supply_register(&pdev->dev, &powerusb);
1986
1987         info->monitor_wqueue
1988                 = create_singlethread_workqueue("ricoh619_battery_monitor");
1989         INIT_DELAYED_WORK_DEFERRABLE(&info->monitor_work,
1990                                          ricoh619_battery_work);
1991         INIT_DELAYED_WORK_DEFERRABLE(&info->displayed_work,
1992                                          ricoh619_displayed_work);
1993         INIT_DELAYED_WORK_DEFERRABLE(&info->charge_stable_work,
1994                                          ricoh619_stable_charge_countdown_work);
1995         INIT_DELAYED_WORK(&info->changed_work, ricoh619_changed_work);
1996         queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
1997                                         RICOH619_MONITOR_START_TIME*HZ);
1998
1999
2000         /* Charger IRQ workqueue settings */
2001         charger_irq = pdata->irq;
2002
2003         info->workqueue = create_singlethread_workqueue("rc5t619_charger_in");
2004         INIT_WORK(&info->irq_work, charger_irq_work);
2005
2006         ret = request_threaded_irq(charger_irq + RICOH619_IRQ_FONCHGINT,
2007                                         NULL, charger_in_isr, IRQF_ONESHOT,
2008                                                 "rc5t619_charger_in", info);
2009         if (ret < 0) {
2010                 dev_err(&pdev->dev, "Can't get CHG_INT IRQ for chrager: %d\n",
2011                                                                         ret);
2012                 goto out;
2013         }
2014         info->workqueue = create_singlethread_workqueue("rc5t619_charger_in");
2015         INIT_WORK(&info->irq_work, charger_irq_work);
2016
2017         ret = request_threaded_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT,
2018                                                 NULL, charger_complete_isr,
2019                                         IRQF_ONESHOT, "rc5t619_charger_comp",
2020                                                                 info);
2021         if (ret < 0) {
2022                 dev_err(&pdev->dev, "Can't get CHG_COMP IRQ for chrager: %d\n",
2023                                                                          ret);
2024                 goto out;
2025         }
2026
2027         ret = request_threaded_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT,
2028                                         NULL, charger_usb_isr, IRQF_ONESHOT,
2029                                                 "rc5t619_usb_det", info);
2030         if (ret < 0) {
2031                 dev_err(&pdev->dev, "Can't get USB_DET IRQ for chrager: %d\n",
2032                                                                          ret);
2033                 goto out;
2034         }
2035
2036         ret = request_threaded_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT,
2037                                         NULL, charger_adp_isr, IRQF_ONESHOT,
2038                                                 "rc5t619_adp_det", info);
2039         if (ret < 0) {
2040                 dev_err(&pdev->dev,
2041                         "Can't get ADP_DET IRQ for chrager: %d\n", ret);
2042                 goto out;
2043         }
2044
2045         info->usb_workqueue
2046                 = create_singlethread_workqueue("rc5t619_usb_det");
2047         INIT_WORK(&info->usb_irq_work, usb_det_irq_work);
2048
2049
2050 #ifdef ENABLE_LOW_BATTERY_DETECTION
2051         ret = request_threaded_irq(charger_irq + RICOH619_IRQ_VSYSLIR,
2052                                         NULL, adc_vsysl_isr, IRQF_ONESHOT,
2053                                                 "rc5t619_adc_vsysl", info);
2054         if (ret < 0) {
2055                 dev_err(&pdev->dev,
2056                         "Can't get ADC_VSYSL IRQ for chrager: %d\n", ret);
2057                 goto out;
2058         }
2059         INIT_DELAYED_WORK_DEFERRABLE(&info->low_battery_work,
2060                                          low_battery_irq_work);
2061 #endif
2062
2063         /* Charger init and IRQ setting */
2064         ret = ricoh619_init_charger(info);
2065         if (ret)
2066                 goto out;
2067
2068 #ifdef  ENABLE_FUEL_GAUGE_FUNCTION
2069         ret = ricoh619_init_fgsoca(info);
2070 #endif
2071
2072         /* Enable Charger interrupt */
2073         ricoh619_set_bits(info->dev->parent, RICOH619_INTC_INTEN, CHG_INT);
2074
2075         return 0;
2076
2077 out:
2078         kfree(info);
2079         return ret;
2080 }
2081
2082 static int __devexit ricoh619_battery_remove(struct platform_device *pdev)
2083 {
2084         struct ricoh619_battery_info *info = platform_get_drvdata(pdev);
2085         uint8_t val;
2086         int ret;
2087         int err;
2088         int cc_cap = 0;
2089         bool is_charging = true;
2090
2091         if (g_fg_on_mode
2092                  && (info->soca->status == RICOH619_SOCA_STABLE)) {
2093                 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
2094                 if (err < 0)
2095                         dev_err(info->dev, "Error in writing PSWR_REG\n");
2096                 g_soc = 0x7f;
2097         } else {
2098                 val = (info->soca->displayed_soc + 50)/100;
2099                 val &= 0x7f;
2100                 ret = ricoh619_write(info->dev->parent, PSWR_REG, val);
2101                 if (ret < 0)
2102                         dev_err(info->dev, "Error in writing PSWR_REG\n");
2103
2104                 g_soc = (info->soca->displayed_soc + 50)/100;
2105
2106                 ret = calc_capacity_in_period(info, &cc_cap, &is_charging);
2107                 if (ret < 0)
2108                         dev_err(info->dev, "Read cc_sum Error !!-----\n");
2109         }
2110
2111         if (g_fg_on_mode == 0) {
2112                 ret = ricoh619_clr_bits(info->dev->parent,
2113                                          FG_CTRL_REG, 0x01);
2114                 if (ret < 0)
2115                         dev_err(info->dev, "Error in clr FG EN\n");
2116         }
2117
2118         cancel_delayed_work(&info->monitor_work);
2119         cancel_delayed_work(&info->charge_stable_work);
2120         cancel_delayed_work(&info->changed_work);
2121 #ifdef ENABLE_LOW_BATTERY_DETECTION
2122         cancel_delayed_work(&info->low_battery_work);
2123 #endif
2124
2125         flush_work(&info->irq_work);
2126         flush_work(&info->usb_irq_work);
2127
2128         flush_workqueue(info->monitor_wqueue);
2129         flush_workqueue(info->workqueue);
2130         flush_workqueue(info->usb_workqueue);
2131
2132         destroy_workqueue(info->monitor_wqueue);
2133         destroy_workqueue(info->workqueue);
2134         destroy_workqueue(info->usb_workqueue);
2135
2136         power_supply_unregister(&info->battery);
2137         kfree(info);
2138         platform_set_drvdata(pdev, NULL);
2139         return 0;
2140 }
2141
2142 #ifdef CONFIG_PM
2143 static int ricoh619_battery_suspend(struct device *dev)
2144 {
2145         struct ricoh619_battery_info *info = dev_get_drvdata(dev);
2146         uint8_t val;
2147         int ret;
2148         int err;
2149         int cc_cap = 0;
2150         bool is_charging = true;
2151
2152         if (g_fg_on_mode
2153                  && (info->soca->status == RICOH619_SOCA_STABLE)) {
2154                 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
2155                 if (err < 0)
2156                         dev_err(info->dev, "Error in writing PSWR_REG\n");
2157                  g_soc = 0x7F;
2158         } else {
2159                 val = (info->soca->displayed_soc + 50)/100;
2160                 val &= 0x7f;
2161                 ret = ricoh619_write(info->dev->parent, PSWR_REG, val);
2162                 if (ret < 0)
2163                         dev_err(info->dev, "Error in writing PSWR_REG\n");
2164
2165                 g_soc = (info->soca->displayed_soc + 50)/100;
2166
2167                 ret = calc_capacity_in_period(info, &cc_cap, &is_charging);
2168                 if (ret < 0)
2169                         dev_err(info->dev, "Read cc_sum Error !!-----\n");
2170         }
2171
2172         disable_irq(charger_irq + RICOH619_IRQ_FONCHGINT);
2173         disable_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT);
2174         disable_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT);
2175         disable_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT);
2176 #ifdef ENABLE_LOW_BATTERY_DETECTION
2177         disable_irq(charger_irq + RICOH619_IRQ_VSYSLIR);
2178 #endif
2179
2180         cancel_delayed_work_sync(&info->monitor_work);
2181         cancel_delayed_work_sync(&info->displayed_work);
2182         cancel_delayed_work_sync(&info->charge_stable_work);
2183         cancel_delayed_work_sync(&info->changed_work);
2184 #ifdef ENABLE_LOW_BATTERY_DETECTION
2185         cancel_delayed_work_sync(&info->low_battery_work);
2186 #endif
2187         cancel_work_sync(&info->irq_work);
2188         cancel_work_sync(&info->usb_irq_work);
2189
2190         flush_workqueue(info->monitor_wqueue);
2191         flush_workqueue(info->workqueue);
2192         flush_workqueue(info->usb_workqueue);
2193
2194         return 0;
2195 }
2196
2197 static int ricoh619_battery_resume(struct device *dev)
2198 {
2199         struct ricoh619_battery_info *info = dev_get_drvdata(dev);
2200         uint8_t val;
2201         int ret;
2202         int displayed_soc_temp;
2203         int cc_cap = 0;
2204         bool is_charging = true;
2205         int i;
2206
2207         if (info->entry_factory_mode) {
2208                 info->soca->displayed_soc = -EINVAL;
2209         } else if (RICOH619_SOCA_STABLE == info->soca->status) {
2210                 info->soca->soc = calc_capacity(info) * 100;
2211                 info->soca->displayed_soc = info->soca->soc;
2212         } else if (RICOH619_SOCA_ZERO == info->soca->status) {
2213                 if (calc_ocv(info) > get_OCV_voltage(info, 0)) {
2214                         ret = ricoh619_read(info->dev->parent, PSWR_REG, &val);
2215                         val &= 0x7f;
2216                         info->soca->soc = val * 100;
2217                         if ((ret < 0) || (val == 0)) {
2218                                 dev_err(info->dev,
2219                                          "Error in reading PSWR_REG %d\n", ret);
2220                                 info->soca->soc
2221                                          = calc_capacity(info) * 100 + 50;
2222                         }
2223
2224                         ret = calc_capacity_in_period(info, &cc_cap,
2225                                                                  &is_charging);
2226                         if (ret < 0)
2227                                 dev_err(info->dev, "Read cc_sum Error !!-----\n");
2228
2229                         info->soca->cc_delta
2230                                  = (is_charging == true) ? cc_cap : -cc_cap;
2231
2232                         displayed_soc_temp
2233                                  = info->soca->soc + info->soca->cc_delta;
2234                         displayed_soc_temp = min(10000, displayed_soc_temp);
2235                         displayed_soc_temp = max(0, displayed_soc_temp);
2236                         info->soca->displayed_soc = displayed_soc_temp;
2237
2238                         ret = ricoh619_write(info->dev->parent,
2239                                                          FG_CTRL_REG, 0x51);
2240                         if (ret < 0)
2241                                 dev_err(info->dev, "Error in writing the control register\n");
2242                         info->soca->ready_fg = 0;
2243                         info->soca->status = RICOH619_SOCA_FG_RESET;
2244
2245                 } else
2246                         info->soca->displayed_soc = 0;
2247         } else {
2248                 ret = ricoh619_read(info->dev->parent, PSWR_REG, &val);
2249                 val &= 0x7f;
2250                 info->soca->soc = val * 100;
2251                 if ((ret < 0) || (val == 0)) {
2252                         dev_err(info->dev,
2253                                  "Error in reading PSWR_REG %d\n", ret);
2254                         info->soca->soc
2255                                  = calc_capacity(info) * 100 + 50;
2256                 }
2257
2258                 ret = calc_capacity_in_period(info, &cc_cap, &is_charging);
2259                 if (ret < 0)
2260                         dev_err(info->dev, "Read cc_sum Error !!-----\n");
2261
2262                 info->soca->cc_delta = (is_charging == true) ? cc_cap : -cc_cap;
2263
2264                 displayed_soc_temp = info->soca->soc + info->soca->cc_delta;
2265                 displayed_soc_temp = min(10000, displayed_soc_temp);
2266                 displayed_soc_temp = max(0, displayed_soc_temp);
2267                 info->soca->displayed_soc = displayed_soc_temp;
2268                 if (RICOH619_SOCA_DISP == info->soca->status)
2269                         info->soca->last_soc = calc_capacity(info) * 100;
2270         }
2271
2272         power_supply_changed(&info->battery);
2273         queue_delayed_work(info->monitor_wqueue, &info->displayed_work, HZ);
2274
2275         if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
2276                 info->soca->stable_count = 10;
2277                 queue_delayed_work(info->monitor_wqueue,
2278                                          &info->charge_stable_work,
2279                                          RICOH619_FG_STABLE_TIME*HZ/10);
2280         } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
2281                 info->soca->stable_count = 1;
2282
2283                 for (i = 0; i < 3; i = i+1)
2284                         info->soca->reset_soc[i] = 0;
2285                 info->soca->reset_count = 0;
2286
2287                 queue_delayed_work(info->monitor_wqueue,
2288                                          &info->charge_stable_work,
2289                                          RICOH619_FG_RESET_TIME*HZ);
2290         }
2291
2292         queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
2293                                                  info->monitor_time);
2294
2295         enable_irq(charger_irq + RICOH619_IRQ_FONCHGINT);
2296         enable_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT);
2297         enable_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT);
2298         enable_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT);
2299 #ifdef ENABLE_LOW_BATTERY_DETECTION
2300         enable_irq(charger_irq + RICOH619_IRQ_VSYSLIR);
2301 #endif
2302         return 0;
2303 }
2304
2305 static const struct dev_pm_ops ricoh619_battery_pm_ops = {
2306         .suspend        = ricoh619_battery_suspend,
2307         .resume         = ricoh619_battery_resume,
2308 };
2309 #endif
2310
2311 static struct platform_driver ricoh619_battery_driver = {
2312         .driver = {
2313                                 .name   = "ricoh619-battery",
2314                                 .owner  = THIS_MODULE,
2315 #ifdef CONFIG_PM
2316                                 .pm     = &ricoh619_battery_pm_ops,
2317 #endif
2318         },
2319         .probe  = ricoh619_battery_probe,
2320         .remove = __devexit_p(ricoh619_battery_remove),
2321 };
2322
2323 static int __init ricoh619_battery_init(void)
2324 {
2325         RICOH_FG_DBG("PMU: %s\n", __func__);
2326         return platform_driver_register(&ricoh619_battery_driver);
2327 }
2328 subsys_initcall_sync(ricoh619_battery_init);
2329
2330 static void __exit ricoh619_battery_exit(void)
2331 {
2332         platform_driver_unregister(&ricoh619_battery_driver);
2333 }
2334 module_exit(ricoh619_battery_exit);
2335
2336 MODULE_DESCRIPTION("RICOH619 Battery driver");
2337 MODULE_ALIAS("platform:ricoh619-battery");
2338 MODULE_LICENSE("GPL");