2 * drivers/power/ricoh619-battery.c
4 * Charger driver for RICOH RC5T619 power management chip.
6 * Copyright (C) 2012-2013 RICOH COMPANY,LTD
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.
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
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/>.
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
26 #include <linux/mutex.h>
27 #include <linux/string.h>
28 #include <linux/power_supply.h>
29 #include <linux/mfd/ricoh619.h>
30 #include <linux/power/ricoh619_battery.h>
31 #include <linux/power/ricoh61x_battery_init.h>
32 #include <linux/delay.h>
33 #include <linux/workqueue.h>
34 #include <linux/delay.h>
35 #include <mach/board.h>
38 #include <linux/interrupt.h>
39 #include <linux/irq.h>
42 /* define for function */
43 #define ENABLE_FUEL_GAUGE_FUNCTION
44 #define ENABLE_LOW_BATTERY_DETECTION
45 #define ENABLE_FACTORY_MODE
46 #define DISABLE_CHARGER_TIMER
47 /* #define ENABLE_FG_KEEP_ON_MODE */
48 #define ENABLE_OCV_TABLE_CALIB
53 #define RICOH619_REL1_SEL_VALUE 64
54 #define RICOH619_REL2_SEL_VALUE 0
64 //for debug #ifdef ENABLE_FUEL_GAUGE_FUNCTION
65 /* define for FG delayed time */
66 #define RICOH619_MONITOR_START_TIME 15
67 #define RICOH619_FG_RESET_TIME 6
68 #define RICOH619_FG_STABLE_TIME 120
69 #define RICOH619_DISPLAY_UPDATE_TIME 15
70 #define RICOH619_LOW_VOL_DOWN_TIME 10
71 #define RICOH619_CHARGE_MONITOR_TIME 20
72 #define RICOH619_CHARGE_RESUME_TIME 1
73 #define RICOH619_CHARGE_CALC_TIME 1
74 #define RICOH619_JEITA_UPDATE_TIME 60
75 #define RICOH619_DELAY_TIME 60
76 /* define for FG parameter */
77 #define RICOH619_MAX_RESET_SOC_DIFF 5
78 #define RICOH619_GET_CHARGE_NUM 10
79 #define RICOH619_UPDATE_COUNT_DISP 4
80 #define RICOH619_UPDATE_COUNT_FULL 4
81 #define RICOH619_CHARGE_UPDATE_TIME 3
82 #define RICOH619_FULL_WAIT_TIME 2
83 #define RE_CAP_GO_DOWN 10 /* 40 */
84 #define RICOH619_ENTER_LOW_VOL 70
85 #define RICOH619_TAH_SEL2 5
86 #define RICOH619_TAL_SEL2 6
87 /* define for FG status */
90 RICOH619_SOCA_UNSTABLE,
91 RICOH619_SOCA_FG_RESET,
96 RICOH619_SOCA_LOW_VOL,
100 #ifdef ENABLE_LOW_BATTERY_DETECTION
101 #define LOW_BATTERY_DETECTION_TIME 10
104 struct ricoh619_soca_info {
107 int ocv_table_def[11];
109 int soc; /* Latest FG SOC value */
112 int status; /* SOCA status 0: Not initial; 5: Finished */
114 int chg_status; /* chg_status */
115 int soc_delta; /* soc delta for status3(DISP) */
126 int Vbat[RICOH619_GET_CHARGE_NUM];
127 int Vsys[RICOH619_GET_CHARGE_NUM];
128 int Ibat[RICOH619_GET_CHARGE_NUM];
134 /* for LOW VOL state */
147 struct ricoh619_battery_info {
149 struct power_supply battery;
150 struct delayed_work monitor_work;
151 struct delayed_work displayed_work;
152 struct delayed_work charge_stable_work;
153 struct delayed_work changed_work;
154 #ifdef ENABLE_LOW_BATTERY_DETECTION
155 struct delayed_work low_battery_work;
157 struct delayed_work charge_monitor_work;
158 struct delayed_work get_charge_work;
159 struct delayed_work jeita_work;
161 struct work_struct irq_work; /* for Charging & VUSB/VADP */
162 struct work_struct usb_irq_work; /* for ADC_VUSB */
164 struct workqueue_struct *monitor_wqueue;
165 struct workqueue_struct *workqueue; /* for Charging & VUSB/VADP */
166 struct workqueue_struct *usb_workqueue; /* for ADC_VUSB */
168 #ifdef ENABLE_FACTORY_MODE
169 struct delayed_work factory_mode_work;
170 struct workqueue_struct *factory_mode_wqueue;
174 unsigned long monitor_time;
190 struct ricoh619_soca_info *soca;
192 bool entry_factory_mode;
215 /* this value is for mfd fucntion */
218 extern int dwc_vbus_status(void);
219 /*This is for full state*/
221 static int BatteryTableFlagDef=0;
222 static int BatteryTypeDef=0;
223 static void ricoh619_scaling_OCV_table(struct ricoh619_battery_info *info, int cutoff_vol, int full_vol, int *start_per, int *end_per);
224 static int Battery_Type(void)
226 return BatteryTypeDef;
229 static int Battery_Table(void)
231 return BatteryTableFlagDef;
234 static void ricoh619_battery_work(struct work_struct *work)
236 struct ricoh619_battery_info *info = container_of(work,
237 struct ricoh619_battery_info, monitor_work.work);
239 RICOH_FG_DBG("PMU: %s\n", __func__);
240 power_supply_changed(&info->battery);
241 queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
245 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
246 static int measure_vbatt_FG(struct ricoh619_battery_info *info, int *data);
247 static int measure_Ibatt_FG(struct ricoh619_battery_info *info, int *data);
248 static int calc_capacity(struct ricoh619_battery_info *info);
249 static int calc_capacity_2(struct ricoh619_battery_info *info);
250 static int get_OCV_init_Data(struct ricoh619_battery_info *info, int index);
251 static int get_OCV_voltage(struct ricoh619_battery_info *info, int index);
252 static int get_check_fuel_gauge_reg(struct ricoh619_battery_info *info,
253 int Reg_h, int Reg_l, int enable_bit);
254 static int calc_capacity_in_period(struct ricoh619_battery_info *info,
255 int *cc_cap, bool *is_charging, bool cc_rst);
256 static int get_charge_priority(struct ricoh619_battery_info *info, bool *data);
257 static int set_charge_priority(struct ricoh619_battery_info *info, bool *data);
258 static int get_power_supply_status(struct ricoh619_battery_info *info);
259 static int measure_vsys_ADC(struct ricoh619_battery_info *info, int *data);
260 static int Calc_Linear_Interpolation(int x0, int y0, int x1, int y1, int y);
261 static int get_battery_temp(struct ricoh619_battery_info *info);
262 static int get_battery_temp_2(struct ricoh619_battery_info *info);
263 static int check_jeita_status(struct ricoh619_battery_info *info, bool *is_jeita_updated);
265 static int calc_ocv(struct ricoh619_battery_info *info)
272 ret = measure_vbatt_FG(info, &Vbat);
273 ret = measure_Ibatt_FG(info, &Ibat);
275 ocv = Vbat - Ibat * info->soca->Rbat;
280 static int check_charge_status_2(struct ricoh619_battery_info *info, int displayed_soc_temp)
282 if (displayed_soc_temp < 0)
283 displayed_soc_temp = 0;
285 get_power_supply_status(info);
287 if (POWER_SUPPLY_STATUS_FULL == info->soca->chg_status) {
289 info->soca->displayed_soc = 100*100;
291 if (info->soca->Ibat_ave >= 0) { /* for issue 3 */
292 if (g_full_flag == 1) {
293 info->soca->displayed_soc = 100*100;
295 if (info->soca->displayed_soc/100 < 99) {
296 info->soca->displayed_soc = displayed_soc_temp;
298 info->soca->displayed_soc = 99 * 100;
302 if (info->soca->Ibat_ave < 0) {
303 if (g_full_flag == 1) {
304 if ((calc_ocv(info) < (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*3/10) )
305 && (info->soca->soc/100 <= 98) ) {
307 info->soca->displayed_soc = 100*100;
309 info->soca->displayed_soc = 100*100;
313 info->soca->displayed_soc = displayed_soc_temp;
317 return info->soca->displayed_soc;
321 * Calculate Capacity in a period
322 * - read CC_SUM & FA_CAP from Coulom Counter
323 * - and calculate Capacity.
324 * @cc_cap: capacity in a period, unit 0.01%
325 * @is_charging: Flag of charging current direction
326 * TRUE : charging (plus)
327 * FALSE: discharging (minus)
328 * @cc_rst: reset CC_SUM or not
332 static int calc_capacity_in_period(struct ricoh619_battery_info *info,
333 int *cc_cap, bool *is_charging, bool cc_rst)
336 uint8_t cc_sum_reg[4];
337 uint8_t cc_clr[4] = {0, 0, 0, 0};
338 uint8_t fa_cap_reg[2];
343 uint8_t charge_state;
346 *is_charging = true; /* currrent state initialize -> charging */
348 if (info->entry_factory_mode)
351 //check need charge stop or not
352 /* get power supply status */
353 err = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
356 charge_state = (status & 0x1F);
357 Ocv = calc_ocv(info);
358 if (charge_state == CHG_STATE_CHG_COMPLETE) {
359 /* Check CHG status is complete or not */
361 } else if (calc_capacity(info) == 100) {
362 /* Check HW soc is 100 or not */
364 } else if (Ocv/1000 < get_OCV_voltage(info, 9)) {
365 /* Check VBAT is high level or not */
371 if (cc_stop_flag == 1)
373 /* Disable Charging/Completion Interrupt */
374 err = ricoh619_set_bits(info->dev->parent,
375 RICOH619_INT_MSK_CHGSTS1, 0x01);
379 /* disable charging */
380 err = ricoh619_clr_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x03);
386 err = ricoh619_write(info->dev->parent, CC_CTRL_REG, 0x01);
391 err = ricoh619_bulk_reads(info->dev->parent,
392 CC_SUMREG3_REG, 4, cc_sum_reg);
396 if (cc_rst == true) {
398 err = ricoh619_bulk_writes(info->dev->parent,
399 CC_SUMREG3_REG, 4, cc_clr);
405 err = ricoh619_write(info->dev->parent, CC_CTRL_REG, 0);
408 if (cc_stop_flag == 1)
411 /* Enable charging */
412 err = ricoh619_set_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x03);
418 /* Clear Charging Interrupt status */
419 err = ricoh619_clr_bits(info->dev->parent,
420 RICOH619_INT_IR_CHGSTS1, 0x01);
424 /* ricoh619_read(info->dev->parent, RICOH619_INT_IR_CHGSTS1, &val);
425 RICOH_FG_DBG("INT_IR_CHGSTS1 = 0x%x\n",val); */
427 /* Enable Charging Interrupt */
428 err = ricoh619_clr_bits(info->dev->parent,
429 RICOH619_INT_MSK_CHGSTS1, 0x01);
434 err = ricoh619_bulk_reads(info->dev->parent,
435 FA_CAP_H_REG, 2, fa_cap_reg);
439 /* fa_cap = *(uint16_t*)fa_cap_reg & 0x7fff; */
440 fa_cap = (fa_cap_reg[0] << 8 | fa_cap_reg[1]) & 0x7fff;
442 /* cc_sum = *(uint32_t*)cc_sum_reg; */
443 cc_sum = cc_sum_reg[0] << 24 | cc_sum_reg[1] << 16 |
444 cc_sum_reg[2] << 8 | cc_sum_reg[3];
446 /* calculation two's complement of CC_SUM */
447 if (cc_sum & 0x80000000) {
448 cc_sum = (cc_sum^0xffffffff)+0x01;
449 *is_charging = false; /* discharge */
452 *cc_cap = cc_sum*25/9/fa_cap; /* CC_SUM/3600/FA_CAP */
456 dev_err(info->dev, "Error !!-----\n");
460 * Calculate target using capacity
462 static int get_target_use_cap(struct ricoh619_battery_info *info)
467 int Target_Cutoff_Vol;
477 /* get const value */
478 Ibat_min = -1 * info->soca->target_ibat;
479 if (info->soca->Ibat_ave > Ibat_min) /* I bat is minus */
483 Ibat_now = info->soca->Ibat_ave;
485 fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
487 use_cap = fa_cap - info->soca->re_cap_old;
489 Ocv_ZeroPer_now = info->soca->target_vsys * 1000 - Ibat_now * info->soca->Rsys;
491 RICOH_FG_DBG("PMU: ------- Rsys= %d: cutoff_ocv= %d: Ocv_ZeroPer_now= %d =======\n",
492 info->soca->Rsys, info->soca->cutoff_ocv, Ocv_ZeroPer_now);
496 for (i = 0; i <= 10; i = i+1) {
497 temp = (battery_init_para[info->num][i*2]<<8)
498 | (battery_init_para[info->num][i*2+1]);
499 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
500 temp = ((temp * 50000 * 10 / 4095) + 5) / 10;
503 for (i = 1; i < 11; i++) {
504 if (ocv_table[i] >= Ocv_ZeroPer_now / 100) {
506 start_per = Calc_Linear_Interpolation(
507 (i-1)*1000, ocv_table[i-1], i*1000,
508 ocv_table[i], (Ocv_ZeroPer_now / 100));
513 FA_CAP_now = fa_cap * ((10000 - start_per) / 100 ) / 100;
515 RICOH_FG_DBG("PMU: ------- Target_Cutoff_Vol= %d: Ocv_ZeroPer_now= %d: start_per= %d =======\n",
516 Target_Cutoff_Vol, Ocv_ZeroPer_now, start_per);
519 RE_CAP_now = FA_CAP_now - use_cap;
521 if (RE_CAP_now < RE_CAP_GO_DOWN || info->soca->Vsys_ave < info->soca->target_vsys*1000) {
522 info->soca->hurry_up_flg = 1;
524 info->soca->hurry_up_flg = 0;
525 /* get CC_OnePer_step */
526 if (info->soca->displayed_soc > 0) { /* avoid divide-by-0 */
527 CC_OnePer_step = RE_CAP_now / (info->soca->displayed_soc / 100);
531 /* get info->soca->target_use_cap */
532 info->soca->target_use_cap = use_cap + CC_OnePer_step;
535 RICOH_FG_DBG("PMU: ------- FA_CAP_now= %d: RE_CAP_now= %d: target_use_cap= %d: hurry_up_flg= %d -------\n",
536 FA_CAP_now, RE_CAP_now, info->soca->target_use_cap, info->soca->hurry_up_flg);
540 #ifdef ENABLE_OCV_TABLE_CALIB
542 * Calibration OCV Table
543 * - Update the value of VBAT on 100% in OCV table
544 * if battery is Full charged.
545 * - int vbat_ocv <- unit is uV
547 static int calib_ocvTable(struct ricoh619_battery_info *info, int vbat_ocv)
553 int start_per,end_per;
555 RICOH_FG_DBG("PMU: %s\n", __func__);
557 if (info->soca->Ibat_ave > RICOH619_REL1_SEL_VALUE) {
558 RICOH_FG_DBG("PMU: %s IBAT > 64mA -- Not Calibration --\n", __func__);
562 if (vbat_ocv < info->soca->OCV100_max) {
563 if (vbat_ocv < info->soca->OCV100_min)
564 ocv100_new = info->soca->OCV100_min;
566 ocv100_new = vbat_ocv;
568 ocv100_new = info->soca->OCV100_max;
570 RICOH_FG_DBG("PMU : %s :max %d min %d current %d\n",__func__,info->soca->OCV100_max,info->soca->OCV100_min,vbat_ocv);
571 RICOH_FG_DBG("PMU : %s : New OCV 100% = 0x%x\n",__func__,ocv100_new);
574 ret = ricoh619_clr_bits(info->dev->parent, FG_CTRL_REG, 0x01);
576 dev_err("PMU: %s Error in FG_En OFF\n", __func__);
580 info->soca->ready_fg = 0;
582 //cutoff_ocv = (battery_init_para[info->num][0]<<8) | (battery_init_para[info->num][1]);
583 cutoff_ocv = get_OCV_voltage(info, 0);
585 info->soca->ocv_table_def[10] = info->soca->OCV100_max;
587 ricoh619_scaling_OCV_table(info, cutoff_ocv/1000, ocv100_new/1000, &start_per, &end_per);
589 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
590 BAT_INIT_TOP_REG, 22, battery_init_para[info->num]);
592 dev_err(info->dev, "batterry initialize error\n");
596 for (i = 0; i <= 10; i = i+1) {
597 info->soca->ocv_table[i] = get_OCV_voltage(info, i);
598 RICOH_FG_DBG("PMU: %s : * %d0%% voltage = %d uV\n",
599 __func__, i, info->soca->ocv_table[i]);
602 /* FG_En on & Reset*/
603 ret = ricoh619_write(info->dev->parent, FG_CTRL_REG, 0x51);
605 dev_err("PMU: %s Error in FG_En On & Reset\n", __func__);
609 RICOH_FG_DBG("PMU: %s Exit \n", __func__);
617 static void ricoh619_displayed_work(struct work_struct *work)
625 int displayed_soc_temp;
628 bool is_charging = true;
630 int re_cap,fa_cap,use_cap;
631 bool is_jeita_updated;
640 struct ricoh619_battery_info *info = container_of(work,
641 struct ricoh619_battery_info, displayed_work.work);
643 if (info->entry_factory_mode) {
644 info->soca->status = RICOH619_SOCA_STABLE;
645 info->soca->displayed_soc = -EINVAL;
646 info->soca->ready_fg = 0;
650 mutex_lock(&info->lock);
652 is_jeita_updated = false;
654 if ((RICOH619_SOCA_START == info->soca->status)
655 || (RICOH619_SOCA_STABLE == info->soca->status)
656 || (RICOH619_SOCA_FULL == info->soca->status))
657 info->soca->ready_fg = 1;
659 /* judege Full state or Moni Vsys state */
660 if ((RICOH619_SOCA_DISP == info->soca->status)
661 || (RICOH619_SOCA_STABLE == info->soca->status))
664 temp_ocv = get_OCV_voltage(info, 10) -
665 (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))/2;
667 if ((POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
668 && (calc_ocv(info) > temp_ocv)) {
669 info->soca->status = RICOH619_SOCA_FULL;
670 info->soca->update_count = 0;
672 /* for issue 1 solution start*/
673 if(g_full_flag == 1){
674 info->soca->status = RICOH619_SOCA_FULL;
675 info->soca->update_count = 0;
677 /* for issue1 solution end */
678 /* check Full state or not*/
679 if (info->soca->Ibat_ave >= -20) {
680 if ((calc_ocv(info) > (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*7/10))
681 || (info->soca->displayed_soc/100 >= 99))
684 info->soca->status = RICOH619_SOCA_FULL;
685 info->soca->update_count = 0;
686 } else if ((calc_ocv(info) > (get_OCV_voltage(info, 9)))
687 && (info->soca->Ibat_ave < 300))
690 info->soca->status = RICOH619_SOCA_FULL;
691 info->soca->update_count = 0;
693 } else { /* dis-charging */
694 if (info->soca->displayed_soc/100 < RICOH619_ENTER_LOW_VOL) {
695 info->soca->target_use_cap = 0;
696 info->soca->status = RICOH619_SOCA_LOW_VOL;
701 if (RICOH619_SOCA_STABLE == info->soca->status) {
702 info->soca->soc = calc_capacity_2(info);
703 soc_round = info->soca->soc / 100;
704 last_soc_round = info->soca->last_soc / 100;
706 info->soca->soc_delta = info->soca->soc - info->soca->last_soc;
709 if (info->soca->chg_status == POWER_SUPPLY_STATUS_CHARGING) {
710 if (soc_round >= 90) {
712 if (info->soca->reset_flg_90 == 0) {
713 err = ricoh619_write(info->dev->parent,
716 dev_err(info->dev, "Error in writing the control register\n");
718 info->soca->ready_fg = 0;
720 for (i = 0; i < 3; i = i+1) {
721 info->soca->reset_soc[i] = 0;
723 info->soca->stable_count = 0;
725 info->soca->status = RICOH619_SOCA_FG_RESET;
726 /* Delay for addition Reset Time (6s) */
727 info->soca->stable_count = 1;
728 queue_delayed_work(info->monitor_wqueue,
729 &info->charge_stable_work,
730 RICOH619_FG_RESET_TIME*HZ);
732 info->soca->reset_flg_90 = 1;
735 } else if (soc_round < 100) {
736 if (info->soca->reset_flg_95 == 0) {
737 err = ricoh619_write(info->dev->parent,
740 dev_err(info->dev, "Error in writing the control register\n");
742 info->soca->ready_fg = 0;
744 for (i = 0; i < 3; i = i+1) {
745 info->soca->reset_soc[i] = 0;
748 info->soca->stable_count = 0;
749 info->soca->status = RICOH619_SOCA_FG_RESET;
750 info->soca->stable_count = 1;
751 queue_delayed_work(info->monitor_wqueue,
752 &info->charge_stable_work,
753 RICOH619_FG_RESET_TIME*HZ);
755 info->soca->reset_flg_95 = 1;
760 info->soca->reset_flg_90 = 0;
761 info->soca->reset_flg_95 = 0;
765 if (info->soca->soc_delta >= -100 && info->soca->soc_delta <= 100) {
766 info->soca->displayed_soc = info->soca->soc;
768 info->soca->status = RICOH619_SOCA_DISP;
770 info->soca->last_soc = info->soca->soc;
771 info->soca->soc_delta = 0;
772 } else if (RICOH619_SOCA_FULL == info->soca->status) {
773 err = check_jeita_status(info, &is_jeita_updated);
775 dev_err(info->dev, "Error in updating JEITA %d\n", err);
778 info->soca->soc = calc_capacity(info) * 100;
779 info->soca->last_soc = calc_capacity_2(info); /* for DISP */
781 if (info->soca->Ibat_ave >= -20) { /* charging */
782 if (0 == info->soca->jt_limit) {
783 if (g_full_flag == 1) {
785 //temp_ocv = get_OCV_voltage(info, 10) -
786 // (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))/2;
787 temp_ocv = get_OCV_voltage(info, 9);
788 info->soca->update_count++;
789 info->soca->chg_cmp_times = 0;
790 if (info->soca->update_count >= RICOH619_UPDATE_COUNT_FULL) {
791 if (calc_ocv(info) < temp_ocv) {
792 if(info->soca->displayed_soc > info->soca->soc) {
793 info->soca->displayed_soc = info->soca->displayed_soc - 1;
795 info->soca->displayed_soc = info->soca->soc;
798 info->soca->displayed_soc = 100*100;
800 info->soca->update_count = 0;
803 if (calc_ocv(info) < (get_OCV_voltage(info, 8))) { /* fail safe*/
805 info->soca->status = RICOH619_SOCA_DISP;
806 info->soca->soc_delta = 0;
807 } else if (POWER_SUPPLY_STATUS_FULL == info->soca->chg_status) {
808 if(info->soca->chg_cmp_times >= RICOH619_FULL_WAIT_TIME) {
809 info->soca->displayed_soc = 100*100;
810 info->soca->update_count = 0;
812 #ifdef ENABLE_OCV_TABLE_CALIB
813 err = calib_ocvTable(info,calc_ocv(info));
815 dev_err(info->dev, "Calibration OCV Error !!\n");
818 info->soca->chg_cmp_times++;
821 info->soca->chg_cmp_times = 0;
822 if (info->soca->displayed_soc/100 < 99) {
823 info->soca->update_count++;
824 if (info->soca->update_count
825 >= RICOH619_UPDATE_COUNT_FULL) {
826 info->soca->displayed_soc = info->soca->displayed_soc + 100;
827 info->soca->update_count = 0;
830 info->soca->displayed_soc = 99 * 100;
831 info->soca->update_count = 0;
836 info->soca->update_count = 0;
838 } else { /* discharging */
839 if (g_full_flag == 1) {
840 if (info->soca->Ibat_ave <= -1 * RICOH619_REL1_SEL_VALUE) {
841 if ((calc_ocv(info) < (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*3/10))
842 && (info->soca->soc/100 <= 98)) {
844 info->soca->displayed_soc = 100 * 100;
845 info->soca->status = RICOH619_SOCA_DISP;
846 info->soca->soc_delta = 0;
848 info->soca->displayed_soc = 100 * 100;
850 } else { /* into relaxation state */
851 ricoh619_read(info->dev->parent, CHGSTATE_REG, ®_val);
852 if (reg_val & 0xc0) {
853 info->soca->displayed_soc = 100 * 100;
856 info->soca->displayed_soc = 100 * 100;
857 info->soca->status = RICOH619_SOCA_DISP;
858 info->soca->soc_delta = 0;
863 info->soca->status = RICOH619_SOCA_DISP;
864 info->soca->soc_delta = 0;
867 } else if (RICOH619_SOCA_LOW_VOL == info->soca->status) {
868 if(info->soca->Ibat_ave >= 0) {
869 info->soca->soc = calc_capacity(info) * 100;
870 info->soca->status = RICOH619_SOCA_DISP;
871 info->soca->last_soc = info->soca->soc;
872 info->soca->soc_delta = 0;
874 re_cap = get_check_fuel_gauge_reg(info, RE_CAP_H_REG, RE_CAP_L_REG,
876 fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
878 use_cap = fa_cap - re_cap;
880 if (info->soca->target_use_cap == 0) {
881 info->soca->re_cap_old = re_cap;
882 get_target_use_cap(info);
885 if(use_cap >= info->soca->target_use_cap) {
886 info->soca->displayed_soc = info->soca->displayed_soc - 100;
887 info->soca->displayed_soc = max(0, info->soca->displayed_soc);
888 info->soca->re_cap_old = re_cap;
889 } else if (info->soca->hurry_up_flg == 1) {
890 info->soca->displayed_soc = info->soca->displayed_soc - 100;
891 info->soca->displayed_soc = max(0, info->soca->displayed_soc);
892 info->soca->re_cap_old = re_cap;
894 get_target_use_cap(info);
895 info->soca->soc = calc_capacity(info) * 100;
898 if (RICOH619_SOCA_DISP == info->soca->status) {
900 info->soca->soc = calc_capacity_2(info);
902 soc_round = info->soca->soc / 100;
903 last_soc_round = info->soca->last_soc / 100;
904 last_disp_round = (info->soca->displayed_soc + 50) / 100;
906 info->soca->soc_delta =
907 info->soca->soc_delta + (info->soca->soc - info->soca->last_soc);
909 info->soca->last_soc = info->soca->soc;
911 if (last_disp_round == soc_round) {
912 /* if SOC == DISPLAY move to stable */
913 info->soca->displayed_soc = info->soca->soc ;
914 info->soca->status = RICOH619_SOCA_STABLE;
916 } else if (info->soca->Ibat_ave > 0) {
917 if ((0 == info->soca->jt_limit) ||
918 (POWER_SUPPLY_STATUS_FULL != info->soca->chg_status)) {
920 if (last_disp_round < soc_round) {
921 /* Case 1 : Charge, Display < SOC */
922 if (info->soca->soc_delta >= 100) {
923 info->soca->displayed_soc
924 = last_disp_round * 100 + 50;
925 info->soca->soc_delta -= 100;
926 if (info->soca->soc_delta >= 100)
929 info->soca->displayed_soc += 25;
930 disp_dec = info->soca->displayed_soc % 100;
931 if ((50 <= disp_dec) && (disp_dec <= 74))
932 info->soca->soc_delta = 0;
934 if (info->soca->displayed_soc/100
936 info->soca->displayed_soc
939 = RICOH619_SOCA_STABLE;
942 } else if (last_disp_round > soc_round) {
943 /* Case 2 : Charge, Display > SOC */
944 if (info->soca->soc_delta >= 300) {
945 info->soca->displayed_soc += 100;
946 info->soca->soc_delta -= 300;
948 if (info->soca->displayed_soc/100
950 info->soca->displayed_soc
953 = RICOH619_SOCA_STABLE;
958 info->soca->soc_delta = 0;
962 if (last_disp_round > soc_round) {
963 /* Case 3 : Dis-Charge, Display > SOC */
964 if (info->soca->soc_delta <= -100) {
965 info->soca->displayed_soc
966 = last_disp_round * 100 - 75;
967 info->soca->soc_delta += 100;
968 if (info->soca->soc_delta <= -100)
971 info->soca->displayed_soc -= 25;
972 disp_dec = info->soca->displayed_soc % 100;
973 if ((25 <= disp_dec) && (disp_dec <= 49))
974 info->soca->soc_delta = 0;
976 if (info->soca->displayed_soc/100
978 info->soca->displayed_soc
981 = RICOH619_SOCA_STABLE;
984 } else if (last_disp_round < soc_round) {
985 /* Case 4 : Dis-Charge, Display < SOC */
986 if (info->soca->soc_delta <= -300) {
987 info->soca->displayed_soc -= 100;
988 info->soca->soc_delta += 300;
990 if (info->soca->displayed_soc/100
992 info->soca->displayed_soc
995 = RICOH619_SOCA_STABLE;
1000 } else if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
1001 err = ricoh619_read(info->dev->parent, PSWR_REG, &val);
1003 info->soca->soc = val * 100;
1006 "Error in reading PSWR_REG %d\n", err);
1008 = calc_capacity(info) * 100;
1011 err = calc_capacity_in_period(info, &cc_cap,
1012 &is_charging, false);
1014 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1016 info->soca->cc_delta
1017 = (is_charging == true) ? cc_cap : -cc_cap;
1020 = info->soca->soc + info->soca->cc_delta;
1021 if (displayed_soc_temp < 0)
1022 displayed_soc_temp = 0;
1024 = min(10000, displayed_soc_temp);
1025 displayed_soc_temp = max(0, displayed_soc_temp);
1027 if (0 == info->soca->jt_limit) {
1028 check_charge_status_2(info, displayed_soc_temp);
1030 info->soca->displayed_soc = displayed_soc_temp;
1032 } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
1034 } else if (RICOH619_SOCA_START == info->soca->status) {
1035 err = check_jeita_status(info, &is_jeita_updated);
1036 is_jeita_updated = false;
1038 dev_err(info->dev, "Error in updating JEITA %d\n", err);
1040 err = ricoh619_read(info->dev->parent, PSWR_REG, &val);
1042 ////debug for Zero state///
1043 ret = measure_vbatt_FG(info, &Vbat);
1044 ret = measure_Ibatt_FG(info, &Ibat);
1045 ocv = Vbat - Ibat * info->soca->Rbat;
1046 RICOH_FG_DBG("Check ZERO state : Vbat %d, Ibat %d, ocv %d Rbat %d %d\n",Vbat,Ibat,ocv,info->soca->Rbat);
1047 info->soca->soc = calc_capacity(info) * 100;
1048 RICOH_FG_DBG("Check ZERO state : first_pwon %d, RSOC is %d OCV is %d\n",info->first_pwon,info->soca->soc/100,calc_ocv(info));
1049 RICOH_FG_DBG("Check ZERO state : g_fg_on_mode %d, get ocv voltage %d\n",g_fg_on_mode,get_OCV_voltage(info, 0));
1050 ///////////////////////////
1051 if (info->first_pwon) {
1052 info->soca->soc = calc_capacity(info) * 100;
1053 val = (info->soca->soc + 50)/100;
1055 err = ricoh619_write(info->dev->parent, PSWR_REG, val);
1057 dev_err(info->dev, "Error in writing PSWR_REG\n");
1060 if ((info->soca->soc == 0) && (calc_ocv(info)
1061 < get_OCV_voltage(info, 0))) {
1062 info->soca->displayed_soc = 0;
1063 info->soca->status = RICOH619_SOCA_ZERO;
1065 if (0 == info->soca->jt_limit) {
1066 check_charge_status_2(info, info->soca->soc);
1068 info->soca->displayed_soc = info->soca->soc;
1070 info->soca->status = RICOH619_SOCA_UNSTABLE;
1072 } else if (g_fg_on_mode && (val == 0x7f)) {
1073 info->soca->soc = calc_capacity(info) * 100;
1074 if ((info->soca->soc == 0) && (calc_ocv(info)
1075 < get_OCV_voltage(info, 0))) {
1076 info->soca->displayed_soc = 0;
1077 info->soca->status = RICOH619_SOCA_ZERO;
1079 if (0 == info->soca->jt_limit) {
1080 check_charge_status_2(info, info->soca->soc);
1082 info->soca->displayed_soc = info->soca->soc;
1084 info->soca->last_soc = info->soca->soc;
1085 info->soca->status = RICOH619_SOCA_STABLE;
1088 info->soca->soc = val * 100;
1091 "Error in reading PSWR_REG %d\n", err);
1093 = calc_capacity(info) * 100;
1096 err = calc_capacity_in_period(info, &cc_cap,
1097 &is_charging, false);
1099 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1101 info->soca->cc_delta
1102 = (is_charging == true) ? cc_cap : -cc_cap;
1103 if (calc_ocv(info) < get_OCV_voltage(info, 0)) {
1104 info->soca->displayed_soc = 0;
1105 info->soca->status = RICOH619_SOCA_ZERO;
1108 = info->soca->soc + info->soca->cc_delta;
1109 if (displayed_soc_temp < 0)
1110 displayed_soc_temp = 0;
1112 = min(10000, displayed_soc_temp);
1113 displayed_soc_temp = max(0, displayed_soc_temp);
1114 if (0 == info->soca->jt_limit) {
1115 check_charge_status_2(info, displayed_soc_temp);
1117 info->soca->displayed_soc = displayed_soc_temp;
1119 info->soca->last_soc = calc_capacity(info) * 100;
1120 info->soca->status = RICOH619_SOCA_UNSTABLE;
1123 } else if (RICOH619_SOCA_ZERO == info->soca->status) {
1124 if (calc_ocv(info) > get_OCV_voltage(info, 0)) {
1125 err = ricoh619_write(info->dev->parent,
1128 dev_err(info->dev, "Error in writing the control register\n");
1129 info->soca->last_soc = calc_capacity_2(info);
1130 info->soca->status = RICOH619_SOCA_STABLE;
1131 info->soca->ready_fg = 0;
1133 info->soca->displayed_soc = 0;
1137 && (info->soca->status == RICOH619_SOCA_STABLE)) {
1138 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
1140 dev_err(info->dev, "Error in writing PSWR_REG\n");
1142 } else if (RICOH619_SOCA_UNSTABLE != info->soca->status) {
1143 if (info->soca->displayed_soc <= 0) {
1146 val = (info->soca->displayed_soc + 50)/100;
1149 err = ricoh619_write(info->dev->parent, PSWR_REG, val);
1151 dev_err(info->dev, "Error in writing PSWR_REG\n");
1155 err = calc_capacity_in_period(info, &cc_cap,
1156 &is_charging, true);
1158 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1161 RICOH_FG_DBG("PMU: ------- STATUS= %d: IBAT= %d: VSYS= %d: VBAT= %d: DSOC= %d: RSOC= %d: -------\n",
1162 info->soca->status, info->soca->Ibat_ave, info->soca->Vsys_ave, info->soca->Vbat_ave,
1163 info->soca->displayed_soc, info->soca->soc);
1165 #ifdef DISABLE_CHARGER_TIMER
1166 /* clear charger timer */
1167 if ( info->soca->chg_status == POWER_SUPPLY_STATUS_CHARGING ) {
1168 err = ricoh619_read(info->dev->parent, TIMSET_REG, &val);
1171 "Error in read TIMSET_REG%d\n", err);
1172 /* to check bit 0-1 */
1176 /* set rapid timer 240 -> 300 */
1177 err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x03);
1179 dev_err(info->dev, "Error in writing the control register\n");
1182 /* set rapid timer 300 -> 240 */
1183 err = ricoh619_clr_bits(info->dev->parent, TIMSET_REG, 0x01);
1184 err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x02);
1186 dev_err(info->dev, "Error in writing the control register\n");
1192 if (0 == info->soca->ready_fg)
1193 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1194 RICOH619_FG_RESET_TIME * HZ);
1195 else if (delay_flag == 1)
1196 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1197 RICOH619_DELAY_TIME * HZ);
1198 else if (RICOH619_SOCA_DISP == info->soca->status)
1199 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1200 RICOH619_DISPLAY_UPDATE_TIME * HZ);
1201 else if (info->soca->hurry_up_flg == 1)
1202 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1203 RICOH619_LOW_VOL_DOWN_TIME * HZ);
1205 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1206 RICOH619_DISPLAY_UPDATE_TIME * HZ);
1208 mutex_unlock(&info->lock);
1210 if(true == is_jeita_updated)
1211 power_supply_changed(&info->battery);
1216 static void ricoh619_stable_charge_countdown_work(struct work_struct *work)
1222 struct ricoh619_battery_info *info = container_of(work,
1223 struct ricoh619_battery_info, charge_stable_work.work);
1225 if (info->entry_factory_mode)
1228 mutex_lock(&info->lock);
1229 if (RICOH619_SOCA_FG_RESET == info->soca->status)
1230 info->soca->ready_fg = 1;
1232 if (2 <= info->soca->stable_count) {
1233 if (3 == info->soca->stable_count
1234 && RICOH619_SOCA_FG_RESET == info->soca->status) {
1235 ret = ricoh619_write(info->dev->parent,
1238 dev_err(info->dev, "Error in writing the control register\n");
1239 info->soca->ready_fg = 0;
1241 info->soca->stable_count = info->soca->stable_count - 1;
1242 queue_delayed_work(info->monitor_wqueue,
1243 &info->charge_stable_work,
1244 RICOH619_FG_STABLE_TIME * HZ / 10);
1245 } else if (0 >= info->soca->stable_count) {
1246 /* Finished queue, ignore */
1247 } else if (1 == info->soca->stable_count) {
1248 if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
1249 /* Judge if FG need reset or Not */
1250 info->soca->soc = calc_capacity(info) * 100;
1251 if (info->chg_ctr != 0) {
1252 queue_delayed_work(info->monitor_wqueue,
1253 &info->charge_stable_work,
1254 RICOH619_FG_STABLE_TIME * HZ / 10);
1255 mutex_unlock(&info->lock);
1258 /* Do reset setting */
1259 ret = ricoh619_write(info->dev->parent,
1262 dev_err(info->dev, "Error in writing the control register\n");
1264 info->soca->ready_fg = 0;
1265 info->soca->status = RICOH619_SOCA_FG_RESET;
1267 /* Delay for addition Reset Time (6s) */
1268 queue_delayed_work(info->monitor_wqueue,
1269 &info->charge_stable_work,
1270 RICOH619_FG_RESET_TIME*HZ);
1271 } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
1272 info->soca->reset_soc[2] = info->soca->reset_soc[1];
1273 info->soca->reset_soc[1] = info->soca->reset_soc[0];
1274 info->soca->reset_soc[0] = calc_capacity(info) * 100;
1275 info->soca->reset_count++;
1277 if (info->soca->reset_count > 10) {
1278 /* Reset finished; */
1279 info->soca->soc = info->soca->reset_soc[0];
1280 info->soca->stable_count = 0;
1284 for (i = 0; i < 3; i++) {
1285 if (max < info->soca->reset_soc[i]/100)
1286 max = info->soca->reset_soc[i]/100;
1287 if (min > info->soca->reset_soc[i]/100)
1288 min = info->soca->reset_soc[i]/100;
1291 if ((info->soca->reset_count > 3) && ((max - min)
1292 < RICOH619_MAX_RESET_SOC_DIFF)) {
1293 /* Reset finished; */
1294 info->soca->soc = info->soca->reset_soc[0];
1295 info->soca->stable_count = 0;
1298 /* Do reset setting */
1299 ret = ricoh619_write(info->dev->parent,
1302 dev_err(info->dev, "Error in writing the control register\n");
1304 info->soca->ready_fg = 0;
1306 /* Delay for addition Reset Time (6s) */
1307 queue_delayed_work(info->monitor_wqueue,
1308 &info->charge_stable_work,
1309 RICOH619_FG_RESET_TIME*HZ);
1311 /* Finished queue From now, select FG as result; */
1312 } else if (RICOH619_SOCA_START == info->soca->status) {
1313 /* Normal condition */
1314 } else { /* other state ZERO/DISP/STABLE */
1315 info->soca->stable_count = 0;
1318 mutex_unlock(&info->lock);
1322 info->soca->last_soc = info->soca->soc;
1323 info->soca->status = RICOH619_SOCA_DISP;
1324 info->soca->soc_delta = 0;
1327 mutex_unlock(&info->lock);
1331 static void ricoh619_charge_monitor_work(struct work_struct *work)
1333 struct ricoh619_battery_info *info = container_of(work,
1334 struct ricoh619_battery_info, charge_monitor_work.work);
1336 get_power_supply_status(info);
1338 if (POWER_SUPPLY_STATUS_DISCHARGING == info->soca->chg_status
1339 || POWER_SUPPLY_STATUS_NOT_CHARGING == info->soca->chg_status) {
1340 switch (info->soca->dischg_state) {
1342 info->soca->dischg_state = 1;
1345 info->soca->dischg_state = 2;
1353 info->soca->dischg_state = 0;
1356 queue_delayed_work(info->monitor_wqueue, &info->charge_monitor_work,
1357 RICOH619_CHARGE_MONITOR_TIME * HZ);
1362 static void ricoh619_get_charge_work(struct work_struct *work)
1364 struct ricoh619_battery_info *info = container_of(work,
1365 struct ricoh619_battery_info, get_charge_work.work);
1367 int Vbat_temp, Vsys_temp, Ibat_temp;
1368 int Vbat_sort[RICOH619_GET_CHARGE_NUM];
1369 int Vsys_sort[RICOH619_GET_CHARGE_NUM];
1370 int Ibat_sort[RICOH619_GET_CHARGE_NUM];
1374 mutex_lock(&info->lock);
1376 for (i = RICOH619_GET_CHARGE_NUM-1; i > 0; i--) {
1377 if (0 == info->soca->chg_count) {
1378 info->soca->Vbat[i] = 0;
1379 info->soca->Vsys[i] = 0;
1380 info->soca->Ibat[i] = 0;
1382 info->soca->Vbat[i] = info->soca->Vbat[i-1];
1383 info->soca->Vsys[i] = info->soca->Vsys[i-1];
1384 info->soca->Ibat[i] = info->soca->Ibat[i-1];
1388 ret = measure_vbatt_FG(info, &info->soca->Vbat[0]);
1389 ret = measure_vsys_ADC(info, &info->soca->Vsys[0]);
1390 ret = measure_Ibatt_FG(info, &info->soca->Ibat[0]);
1392 info->soca->chg_count++;
1394 if (RICOH619_GET_CHARGE_NUM != info->soca->chg_count) {
1395 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1396 RICOH619_CHARGE_CALC_TIME * HZ);
1397 mutex_unlock(&info->lock);
1401 for (i = 0; i < RICOH619_GET_CHARGE_NUM; i++) {
1402 Vbat_sort[i] = info->soca->Vbat[i];
1403 Vsys_sort[i] = info->soca->Vsys[i];
1404 Ibat_sort[i] = info->soca->Ibat[i];
1410 for (i = 0; i < RICOH619_GET_CHARGE_NUM - 1; i++) {
1411 for (j = RICOH619_GET_CHARGE_NUM - 1; j > i; j--) {
1412 if (Vbat_sort[j - 1] > Vbat_sort[j]) {
1413 Vbat_temp = Vbat_sort[j];
1414 Vbat_sort[j] = Vbat_sort[j - 1];
1415 Vbat_sort[j - 1] = Vbat_temp;
1417 if (Vsys_sort[j - 1] > Vsys_sort[j]) {
1418 Vsys_temp = Vsys_sort[j];
1419 Vsys_sort[j] = Vsys_sort[j - 1];
1420 Vsys_sort[j - 1] = Vsys_temp;
1422 if (Ibat_sort[j - 1] > Ibat_sort[j]) {
1423 Ibat_temp = Ibat_sort[j];
1424 Ibat_sort[j] = Ibat_sort[j - 1];
1425 Ibat_sort[j - 1] = Ibat_temp;
1433 for (i = 3; i < RICOH619_GET_CHARGE_NUM-3; i++) {
1434 Vbat_temp = Vbat_temp + Vbat_sort[i];
1435 Vsys_temp = Vsys_temp + Vsys_sort[i];
1436 Ibat_temp = Ibat_temp + Ibat_sort[i];
1438 Vbat_temp = Vbat_temp / (RICOH619_GET_CHARGE_NUM - 6);
1439 Vsys_temp = Vsys_temp / (RICOH619_GET_CHARGE_NUM - 6);
1440 Ibat_temp = Ibat_temp / (RICOH619_GET_CHARGE_NUM - 6);
1442 if (0 == info->soca->chg_count) {
1443 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1444 RICOH619_CHARGE_UPDATE_TIME * HZ);
1445 mutex_unlock(&info->lock);
1448 info->soca->Vbat_ave = Vbat_temp;
1449 info->soca->Vsys_ave = Vsys_temp;
1450 info->soca->Ibat_ave = Ibat_temp;
1453 info->soca->chg_count = 0;
1454 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1455 RICOH619_CHARGE_UPDATE_TIME * HZ);
1456 mutex_unlock(&info->lock);
1460 /* Initial setting of FuelGauge SOCA function */
1461 static int ricoh619_init_fgsoca(struct ricoh619_battery_info *info)
1467 for (i = 0; i <= 10; i = i+1) {
1468 info->soca->ocv_table[i] = get_OCV_voltage(info, i);
1469 RICOH_FG_DBG("PMU: %s : * %d0%% voltage = %d uV\n",
1470 __func__, i, info->soca->ocv_table[i]);
1473 for (i = 0; i < 3; i = i+1)
1474 info->soca->reset_soc[i] = 0;
1475 info->soca->reset_count = 0;
1477 if (info->first_pwon) {
1479 err = ricoh619_read(info->dev->parent, CHGISET_REG, &val);
1482 "Error in read CHGISET_REG%d\n", err);
1484 err = ricoh619_write(info->dev->parent, CHGISET_REG, 0);
1487 "Error in writing CHGISET_REG%d\n", err);
1490 if (!info->entry_factory_mode) {
1491 err = ricoh619_write(info->dev->parent,
1494 dev_err(info->dev, "Error in writing the control register\n");
1499 err = ricoh619_write(info->dev->parent, CHGISET_REG, val);
1502 "Error in writing CHGISET_REG%d\n", err);
1505 /* Rbat : Transfer */
1506 info->soca->Rbat = get_OCV_init_Data(info, 12) * 1000 / 512
1508 info->soca->n_cap = get_OCV_init_Data(info, 11);
1510 info->soca->f_chg_margin = (get_OCV_voltage(info, 10) -
1511 get_OCV_voltage(info, 9)) / 10;
1513 info->soca->displayed_soc = 0;
1514 info->soca->suspend_soc = 0;
1515 info->soca->ready_fg = 0;
1516 info->soca->soc_delta = 0;
1517 info->soca->update_count = 0;
1518 info->soca->status = RICOH619_SOCA_START;
1519 /* stable count down 11->2, 1: reset; 0: Finished; */
1520 info->soca->stable_count = 11;
1521 info->soca->chg_cmp_times = 0;
1522 info->soca->dischg_state = 0;
1523 info->soca->Vbat_ave = 0;
1524 info->soca->Vsys_ave = 0;
1525 info->soca->Ibat_ave = 0;
1526 info->soca->chg_count = 0;
1527 info->soca->target_use_cap = 0;
1528 info->soca->hurry_up_flg = 0;
1529 info->soca->re_cap_old = 0;
1530 info->soca->jt_limit = 0;
1532 for (i = 0; i < RICOH619_GET_CHARGE_NUM; i++) {
1533 info->soca->Vbat[i] = 0;
1534 info->soca->Vsys[i] = 0;
1535 info->soca->Ibat[i] = 0;
1540 #ifdef ENABLE_FG_KEEP_ON_MODE
1546 /* Start first Display job */
1547 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1548 RICOH619_FG_RESET_TIME*HZ);
1550 /* Start first Waiting stable job */
1551 queue_delayed_work(info->monitor_wqueue, &info->charge_stable_work,
1552 RICOH619_FG_STABLE_TIME*HZ/10);
1554 queue_delayed_work(info->monitor_wqueue, &info->charge_monitor_work,
1555 RICOH619_CHARGE_MONITOR_TIME * HZ);
1557 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1558 RICOH619_CHARGE_MONITOR_TIME * HZ);
1560 if (info->jt_hw_sw) {
1561 /* Enable JEITA function supported by H/W */
1562 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x04);
1564 dev_err(info->dev, "Error in writing the control register\n");
1566 /* Disable JEITA function supported by H/W */
1567 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x04);
1569 dev_err(info->dev, "Error in writing the control register\n");
1570 queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
1571 RICOH619_FG_RESET_TIME * HZ);
1574 /* Disable JEITA function supported by H/W */
1575 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x04);
1577 dev_err(info->dev, "Error in writing the control register\n");
1580 RICOH_FG_DBG("PMU: %s : * Rbat = %d mOhm n_cap = %d mAH\n",
1581 __func__, info->soca->Rbat, info->soca->n_cap);
1586 static void ricoh619_changed_work(struct work_struct *work)
1588 struct ricoh619_battery_info *info = container_of(work,
1589 struct ricoh619_battery_info, changed_work.work);
1591 RICOH_FG_DBG("PMU: %s\n", __func__);
1592 power_supply_changed(&info->battery);
1597 static int check_jeita_status(struct ricoh619_battery_info *info, bool *is_jeita_updated)
1598 /* JEITA Parameter settings
1602 * jt_vfchg_h~+~~~~~~~~~~~~~~~~~~~+
1604 * jt_vfchg_l-| - - - - - - - - - +~~~~~~~~~~+
1606 * -------0--+-------------------+----------+--- Temp
1610 * jt_ichg_h-+ - -+~~~~~~~~~~~~~~+~~~~~~~~~~+
1612 * jt_ichg_l-+~~~~+ Charge area |
1614 * 0--+----+--------------+----------+--- Temp
1615 * 0 jt_temp_l jt_temp_h 55
1622 uint8_t chgiset_org;
1623 uint8_t batset2_org;
1624 uint8_t set_vchg_h, set_vchg_l;
1625 uint8_t set_ichg_h, set_ichg_l;
1627 *is_jeita_updated = false;
1629 /* No execute if JEITA disabled */
1630 if (!info->jt_en || info->jt_hw_sw)
1633 /* Check FG Reset */
1634 if (info->soca->ready_fg) {
1635 temp = get_battery_temp_2(info) / 10;
1637 RICOH_FG_DBG(KERN_INFO "JEITA: %s *** cannot update by resetting FG ******\n", __func__);
1642 err = ricoh619_read(info->dev->parent, BATSET2_REG, &batset2_org);
1644 dev_err(info->dev, "Error in readng the battery setting register\n");
1647 vfchg = (batset2_org & 0x70) >> 4;
1648 batset2_org &= 0x8F;
1651 err = ricoh619_read(info->dev->parent, CHGISET_REG, &chgiset_org);
1653 dev_err(info->dev, "Error in readng the chrage setting register\n");
1656 chgiset_org &= 0xC0;
1658 set_ichg_h = (uint8_t)(chgiset_org | info->jt_ichg_h);
1659 set_ichg_l = (uint8_t)(chgiset_org | info->jt_ichg_l);
1661 set_vchg_h = (uint8_t)((info->jt_vfchg_h << 4) | batset2_org);
1662 set_vchg_l = (uint8_t)((info->jt_vfchg_l << 4) | batset2_org);
1664 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Temperature: %d, vfchg: %d, SW status: %d, chg_status: %d ******\n",
1665 __func__, temp, vfchg, info->soca->status, info->soca->chg_status);
1667 if (temp <= 0 || 55 <= temp) {
1668 /* 1st and 5th temperature ranges (~0, 55~) */
1669 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Temp(%d) is out of 0-55 ******\n", __func__, temp);
1670 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
1672 dev_err(info->dev, "Error in writing the control register\n");
1675 info->soca->jt_limit = 0;
1676 *is_jeita_updated = true;
1677 } else if (temp < info->jt_temp_l) {
1678 /* 2nd temperature range (0~12) */
1679 if (vfchg != info->jt_vfchg_h) {
1680 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 0<Temp<12, update to vfchg=%d ******\n",
1681 __func__, info->jt_vfchg_h);
1682 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
1684 dev_err(info->dev, "Error in writing the control register\n");
1688 /* set VFCHG/VRCHG */
1689 err = ricoh619_write(info->dev->parent,
1690 BATSET2_REG, set_vchg_h);
1692 dev_err(info->dev, "Error in writing the battery setting register\n");
1695 info->soca->jt_limit = 0;
1696 *is_jeita_updated = true;
1698 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 0<Temp<50, already set vfchg=%d, so no need to update ******\n",
1699 __func__, info->jt_vfchg_h);
1702 err = ricoh619_write(info->dev->parent, CHGISET_REG, set_ichg_l);
1704 dev_err(info->dev, "Error in writing the battery setting register\n");
1707 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x03);
1709 dev_err(info->dev, "Error in writing the control register\n");
1712 } else if (temp < info->jt_temp_h) {
1713 /* 3rd temperature range (12~50) */
1714 if (vfchg != info->jt_vfchg_h) {
1715 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 12<Temp<50, update to vfchg==%d ******\n", __func__, info->jt_vfchg_h);
1717 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
1719 dev_err(info->dev, "Error in writing the control register\n");
1722 /* set VFCHG/VRCHG */
1723 err = ricoh619_write(info->dev->parent,
1724 BATSET2_REG, set_vchg_h);
1726 dev_err(info->dev, "Error in writing the battery setting register\n");
1729 info->soca->jt_limit = 0;
1731 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 12<Temp<50, already set vfchg==%d, so no need to update ******\n",
1732 __func__, info->jt_vfchg_h);
1735 err = ricoh619_write(info->dev->parent, CHGISET_REG, set_ichg_h);
1737 dev_err(info->dev, "Error in writing the battery setting register\n");
1740 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x03);
1742 dev_err(info->dev, "Error in writing the control register\n");
1745 } else if (temp < 55) {
1746 /* 4th temperature range (50~55) */
1747 if (vfchg != info->jt_vfchg_l) {
1748 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 50<Temp<55, update to vfchg==%d ******\n", __func__, info->jt_vfchg_l);
1750 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
1752 dev_err(info->dev, "Error in writing the control register\n");
1755 /* set VFCHG/VRCHG */
1756 err = ricoh619_write(info->dev->parent,
1757 BATSET2_REG, set_vchg_l);
1759 dev_err(info->dev, "Error in writing the battery setting register\n");
1762 info->soca->jt_limit = 1;
1763 *is_jeita_updated = true;
1765 RICOH_FG_DBG(KERN_INFO "JEITA: %s *** 50<Temp<55, already set vfchg==%d, so no need to update ******\n",
1766 __func__, info->jt_vfchg_l);
1769 err = ricoh619_write(info->dev->parent, CHGISET_REG, set_ichg_h);
1771 dev_err(info->dev, "Error in writing the battery setting register\n");
1774 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x03);
1776 dev_err(info->dev, "Error in writing the control register\n");
1781 get_power_supply_status(info);
1782 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Hope updating value in this timing after checking jeita, chg_status: %d, is_jeita_updated: %d ******\n",
1783 __func__, info->soca->chg_status, *is_jeita_updated);
1788 RICOH_FG_DBG(KERN_INFO "PMU: %s ERROR ******\n", __func__);
1792 static void ricoh619_jeita_work(struct work_struct *work)
1795 bool is_jeita_updated = false;
1796 struct ricoh619_battery_info *info = container_of(work,
1797 struct ricoh619_battery_info, jeita_work.work);
1799 mutex_lock(&info->lock);
1801 ret = check_jeita_status(info, &is_jeita_updated);
1803 queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
1804 RICOH619_JEITA_UPDATE_TIME * HZ);
1806 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Call check_jeita_status() in jeita_work, err:%d ******\n",
1808 queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
1809 RICOH619_FG_RESET_TIME * HZ);
1812 mutex_unlock(&info->lock);
1814 if(true == is_jeita_updated)
1815 power_supply_changed(&info->battery);
1820 #ifdef ENABLE_FACTORY_MODE
1821 /*------------------------------------------------------*/
1823 /* Check Battery exist or not */
1824 /* If not, disabled Rapid to Complete State change */
1825 /*------------------------------------------------------*/
1826 static int ricoh619_factory_mode(struct ricoh619_battery_info *info)
1831 ret = ricoh619_read(info->dev->parent, RICOH619_INT_MON_CHGCTR, &val);
1833 dev_err(info->dev, "Error in reading the control register\n");
1836 if (!(val & 0x01)) /* No Adapter connected */
1839 /* Rapid to Complete State change disable */
1840 ret = ricoh619_set_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x40);
1842 dev_err(info->dev, "Error in writing the control register\n");
1846 /* Wait 1s for checking Charging State */
1847 queue_delayed_work(info->factory_mode_wqueue, &info->factory_mode_work,
1853 static void check_charging_state_work(struct work_struct *work)
1855 struct ricoh619_battery_info *info = container_of(work,
1856 struct ricoh619_battery_info, factory_mode_work.work);
1860 int chargeCurrent = 0;
1862 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &val);
1864 dev_err(info->dev, "Error in reading the control register\n");
1869 chargeCurrent = get_check_fuel_gauge_reg(info, CC_AVERAGE1_REG,
1870 CC_AVERAGE0_REG, 0x3fff);
1871 if (chargeCurrent < 0) {
1872 dev_err(info->dev, "Error in reading the FG register\n");
1876 /* Repid State && Charge Current about 0mA */
1877 if (((chargeCurrent >= 0x3ffc && chargeCurrent <= 0x3fff)
1878 || chargeCurrent < 0x05) && val == 0x43) {
1879 RICOH_FG_DBG("PMU:%s --- No battery !! Enter Factory mode ---\n"
1881 info->entry_factory_mode = true;
1882 /* clear FG_ACC bit */
1883 ret = ricoh619_clr_bits(info->dev->parent, RICOH619_FG_CTRL, 0x10);
1885 dev_err(info->dev, "Error in writing FG_CTRL\n");
1887 return; /* Factory Mode */
1890 /* Return Normal Mode --> Rapid to Complete State change enable */
1891 ret = ricoh619_clr_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x40);
1893 dev_err(info->dev, "Error in writing the control register\n");
1896 RICOH_FG_DBG("PMU:%s --- Battery exist !! Return Normal mode ---0x%2x\n"
1901 #endif /* ENABLE_FACTORY_MODE */
1903 static int Calc_Linear_Interpolation(int x0, int y0, int x1, int y1, int y)
1908 alpha = (y - y0)*100 / (y1 - y0);
1910 x = ((100 - alpha) * x0 + alpha * x1) / 100;
1915 static void ricoh619_scaling_OCV_table(struct ricoh619_battery_info *info, int cutoff_vol, int full_vol, int *start_per, int *end_per)
1921 int OCV_percent_new[11];
1923 /* get ocv table. this table is calculated by Apprication */
1924 //RICOH_FG_DBG("PMU : %s : original table\n",__func__);
1925 for (i = 0; i <= 10; i = i+1) {
1926 RICOH_FG_DBG(KERN_INFO "PMU: %s : %d0%% voltage = %d uV\n",
1927 __func__, i, info->soca->ocv_table_def[i]);
1929 //RICOH_FG_DBG("PMU: %s : cutoff_vol %d full_vol %d\n",
1930 // __func__, cutoff_vol,full_vol);
1933 for (i = 1; i < 11; i++) {
1934 if (info->soca->ocv_table_def[i] >= cutoff_vol * 1000) {
1935 /* unit is 0.001% */
1936 *start_per = Calc_Linear_Interpolation(
1937 (i-1)*1000, info->soca->ocv_table_def[i-1], i*1000,
1938 info->soca->ocv_table_def[i], (cutoff_vol * 1000));
1944 for (i = 1; i < 11; i++) {
1945 if (info->soca->ocv_table_def[i] >= full_vol * 1000) {
1946 /* unit is 0.001% */
1947 *end_per = Calc_Linear_Interpolation(
1948 (i-1)*1000, info->soca->ocv_table_def[i-1], i*1000,
1949 info->soca->ocv_table_def[i], (full_vol * 1000));
1954 /* calc new ocv percent */
1955 percent_step = ( *end_per - *start_per) / 10;
1956 //RICOH_FG_DBG("PMU : %s : percent_step is %d end per is %d start per is %d\n",__func__, percent_step, *end_per, *start_per);
1958 for (i = 0; i < 11; i++) {
1960 = *start_per + percent_step*(i - 0);
1963 /* calc new ocv voltage */
1964 for (i = 0; i < 11; i++) {
1965 for (j = 1; j < 11; j++) {
1966 if (1000*j >= OCV_percent_new[i]) {
1967 temp = Calc_Linear_Interpolation(
1968 info->soca->ocv_table_def[j-1], (j-1)*1000,
1969 info->soca->ocv_table_def[j] , j*1000,
1970 OCV_percent_new[i]);
1972 temp = ( (temp/1000) * 4095 ) / 5000;
1974 battery_init_para[info->num][i*2 + 1] = temp;
1975 battery_init_para[info->num][i*2] = temp >> 8;
1981 RICOH_FG_DBG("PMU : %s : new table\n",__func__);
1982 for (i = 0; i <= 10; i = i+1) {
1983 temp = (battery_init_para[info->num][i*2]<<8)
1984 | (battery_init_para[info->num][i*2+1]);
1985 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
1986 temp = ((temp * 50000 * 10 / 4095) + 5) / 10;
1987 RICOH_FG_DBG("PMU : %s : ocv_table %d is %d v\n",__func__, i, temp);
1992 static int ricoh619_set_OCV_table(struct ricoh619_battery_info *info)
1995 int OCV_set_table[11];
1999 int available_cap_ori;
2008 int OCV_percent_new[11];
2017 for (i = 0; i <= 10; i = i+1) {
2018 info->soca->ocv_table_def[i] = get_OCV_voltage(info, i);
2019 RICOH_FG_DBG(KERN_INFO "PMU: %s : %d0%% voltage = %d uV\n",
2020 __func__, i, info->soca->ocv_table_def[i]);
2023 info->soca->target_vsys = info->fg_target_vsys;
2024 info->soca->target_ibat = info->fg_target_ibat;
2027 RICOH_FG_DBG("PMU : %s : target_vsys is %d target_ibat is %d\n",__func__,info->soca->target_vsys,info->soca->target_ibat);
2029 if ((info->soca->target_ibat == 0) || (info->soca->target_vsys == 0)) { /* normal version */
2030 } else { /*Slice cutoff voltage version. */
2031 temp = (battery_init_para[info->num][24]<<8) | (battery_init_para[info->num][25]);
2032 Rbat = temp * 1000 / 512 * 5000 / 4095;
2034 Ibat_min = -1 * info->soca->target_ibat;
2035 info->soca->Rsys = Rbat + 55;
2036 info->soca->cutoff_ocv = info->soca->target_vsys - Ibat_min * info->soca->Rsys / 1000;
2038 full_ocv = (battery_init_para[info->num][20]<<8) | (battery_init_para[info->num][21]);
2039 full_ocv = full_ocv * 5000 / 4095;
2041 ricoh619_scaling_OCV_table(info, info->soca->cutoff_ocv, full_ocv, &start_per, &end_per);
2043 /* calc available capacity */
2044 /* get avilable capacity */
2045 /* battery_init_para23-24 is designe capacity */
2046 available_cap = (battery_init_para[info->num][22]<<8)
2047 | (battery_init_para[info->num][23]);
2049 available_cap = available_cap
2050 * ((10000 - start_per) / 100) / 100 ;
2053 battery_init_para[info->num][23] = available_cap;
2054 battery_init_para[info->num][22] = available_cap >> 8;
2057 ret = ricoh619_clr_bits(info->dev->parent, FG_CTRL_REG, 0x01);
2059 dev_err(info->dev, "error in FG_En off\n");
2062 ////////////////////////////////
2063 ////////////////////////////////
2064 //check OCV calibration or not
2065 /* get ocv table from setting */
2066 for (i = 0; i < 11; i++){
2067 ret = ricoh619_read_bank1(info->dev->parent, (0xBC + i*2), &val);
2069 dev_err(info->dev, "batterry initialize error\n");
2072 ret = ricoh619_read_bank1(info->dev->parent, (0xBD + i*2), &val2);
2074 dev_err(info->dev, "batterry initialize error\n");
2078 OCV_set_table[i] = val2 + (val << 8);
2079 RICOH_FG_DBG("PMU : %s : no %d OCV_set_table %x\n", __func__, i, OCV_set_table[i]);
2083 OCV_100_def = (battery_init_para[info->num][20]<<8)
2084 | (battery_init_para[info->num][21]);
2086 OCV_0_def = (battery_init_para[info->num][0]<<8)
2087 | (battery_init_para[info->num][1]);
2089 if(OCV_100_def < OCV_set_table[10]) {
2090 OCV_100_diff = OCV_set_table[10] - OCV_100_def;
2092 OCV_100_diff = OCV_100_def - OCV_set_table[10];
2095 if(OCV_0_def < OCV_set_table[0]) {
2096 OCV_0_diff = OCV_set_table[0] - OCV_0_def;
2098 OCV_0_diff = OCV_0_def - OCV_set_table[0];
2100 RICOH_FG_DBG("PMU : %s : def OCV 100 %x OCV 0 %x \n", __func__, OCV_100_def, OCV_0_def);
2101 RICOH_FG_DBG("PMU : %s : OCV 100 %x OCV 0 %x \n", __func__, OCV_set_table[10], OCV_set_table[0]);
2102 RICOH_FG_DBG("PMU : %s : diff OCV 100 %x OCV 0 %x \n", __func__, OCV_100_diff, OCV_0_diff);
2104 if ( (OCV_100_diff > 5)
2106 {/* keep setting OCV table */
2107 for (i = 0; i < 11; i++) {
2108 RICOH_FG_DBG("PMU : 1\n");
2109 battery_init_para[info->num][i*2+1] = OCV_set_table[i];
2110 battery_init_para[info->num][i*2] = OCV_set_table[i] >> 8;
2114 /////////////////////////////////
2115 ret = ricoh619_read_bank1(info->dev->parent, 0xDC, &val);
2117 dev_err(info->dev, "batterry initialize error\n");
2122 val &= 0x0F; //clear bit 4-7
2123 val |= 0x10; //set bit 4
2125 ret = ricoh619_write_bank1(info->dev->parent, 0xDC, val);
2127 dev_err(info->dev, "batterry initialize error\n");
2131 ret = ricoh619_read_bank1(info->dev->parent, 0xDC, &val2);
2133 dev_err(info->dev, "batterry initialize error\n");
2137 ret = ricoh619_write_bank1(info->dev->parent, 0xDC, val_temp);
2139 dev_err(info->dev, "batterry initialize error\n");
2143 RICOH_FG_DBG("PMU : %s : original 0x%x, before 0x%x, after 0x%x\n",__func__, val_temp, val, val2);
2146 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
2147 BAT_INIT_TOP_REG, 30, battery_init_para[info->num]);
2149 dev_err(info->dev, "batterry initialize error\n");
2153 ret = ricoh619_read_bank1(info->dev->parent, 0xD2, &val);
2155 dev_err(info->dev, "batterry initialize error\n");
2159 ret = ricoh619_read_bank1(info->dev->parent, 0xD3, &val2);
2161 dev_err(info->dev, "batterry initialize error\n");
2165 available_cap_ori = val2 + (val << 8);
2166 available_cap = battery_init_para[info->num][23]
2167 + (battery_init_para[info->num][22] << 8);
2169 if (available_cap_ori == available_cap) {
2170 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
2171 BAT_INIT_TOP_REG, 22, battery_init_para[info->num]);
2173 dev_err(info->dev, "batterry initialize error\n");
2177 for (i = 0; i < 6; i++) {
2178 ret = ricoh619_write_bank1(info->dev->parent, 0xD4+i, battery_init_para[info->num][23+i]);
2180 dev_err(info->dev, "batterry initialize error\n");
2185 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
2186 BAT_INIT_TOP_REG, 30, battery_init_para[info->num]);
2188 dev_err(info->dev, "batterry initialize error\n");
2194 ////////////////////////////////
2201 /* Initial setting of battery */
2202 static int ricoh619_init_battery(struct ricoh619_battery_info *info)
2207 /* Need to implement initial setting of batery and error */
2208 /* -------------------------- */
2209 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
2211 /* set kanwa state */
2212 if (RICOH619_REL1_SEL_VALUE > 240)
2215 val = RICOH619_REL1_SEL_VALUE / 16 ;
2217 /* set kanwa state */
2218 if (RICOH619_REL2_SEL_VALUE > 120)
2221 val2 = RICOH619_REL2_SEL_VALUE / 8 ;
2223 val = val + (val2 << 4);
2225 ret = ricoh619_write_bank1(info->dev->parent, BAT_REL_SEL_REG, val);
2227 dev_err(info->dev, "Error in writing BAT_REL_SEL_REG\n");
2231 ret = ricoh619_read_bank1(info->dev->parent, BAT_REL_SEL_REG, &val);
2232 RICOH_FG_DBG("PMU: ------- BAT_REL_SEL= %xh: =======\n",
2235 ret = ricoh619_write_bank1(info->dev->parent, BAT_TA_SEL_REG, 0x00);
2237 dev_err(info->dev, "Error in writing BAT_TA_SEL_REG\n");
2241 // ret = ricoh619_read(info->dev->parent, FG_CTRL_REG, &val);
2243 // dev_err(info->dev, "Error in reading the control register\n");
2247 // val = (val & 0x10) >> 4;
2248 // info->first_pwon = (val == 0) ? 1 : 0;
2249 ret = ricoh619_read(info->dev->parent, PSWR_REG, &val);
2251 dev_err(info->dev,"Error in reading PSWR_REG %d\n", ret);
2254 info->first_pwon = (val == 0) ? 1 : 0;
2257 ret = ricoh619_set_OCV_table(info);
2259 dev_err(info->dev, "Error in writing the OCV Tabler\n");
2263 ret = ricoh619_write(info->dev->parent, FG_CTRL_REG, 0x11);
2265 dev_err(info->dev, "Error in writing the control register\n");
2271 ret = ricoh619_write(info->dev->parent, VINDAC_REG, 0x01);
2273 dev_err(info->dev, "Error in writing the control register\n");
2277 if (info->alarm_vol_mv < 2700 || info->alarm_vol_mv > 3400) {
2278 dev_err(info->dev, "alarm_vol_mv is out of range!\n");
2285 /* Initial setting of charger */
2286 static int ricoh619_init_charger(struct ricoh619_battery_info *info)
2300 info->chg_stat1 = 0;
2302 err = ricoh619_set_bits(info->dev->parent, RICOH619_PWR_FUNC, 0x20);
2304 dev_err(info->dev, "Error in writing the PWR FUNC register\n");
2308 charge_status = get_power_supply_status(info);
2310 if (charge_status != POWER_SUPPLY_STATUS_FULL)
2312 /* Disable charging */
2313 err = ricoh619_clr_bits(info->dev->parent,CHGCTL1_REG, 0x03);
2315 dev_err(info->dev, "Error in writing the control register\n");
2321 err = ricoh619_read(info->dev->parent, REGISET1_REG,&val);
2322 RICOH_FG_DBG("PMU : %s : before REGISET1_REG (0x%x) is 0x%x info->ch_ilim_adp is 0x%x\n",__func__,REGISET1_REG,val,info->ch_ilim_adp);
2324 /* REGISET1:(0xB6) setting */
2325 if ((info->ch_ilim_adp != 0xFF) || (info->ch_ilim_adp <= 0x1D)) {
2326 val = info->ch_ilim_adp;
2328 err = ricoh619_write(info->dev->parent, REGISET1_REG,val);
2330 dev_err(info->dev, "Error in writing REGISET1_REG %d\n",
2337 err = ricoh619_read(info->dev->parent, REGISET1_REG,&val);
2338 RICOH_FG_DBG("PMU : %s : after REGISET1_REG (0x%x) is 0x%x info->ch_ilim_adp is 0x%x\n",__func__,REGISET1_REG,val,info->ch_ilim_adp);
2341 err = ricoh619_read(info->dev->parent, REGISET2_REG,&val);
2342 RICOH_FG_DBG("PMU : %s : before REGISET2_REG (0x%x) is 0x%x info->ch_ilim_usb is 0x%x\n",__func__,REGISET2_REG,val,info->ch_ilim_usb);
2344 /* REGISET2:(0xB7) setting */
2345 err = ricoh619_read(info->dev->parent, REGISET2_REG, &val);
2348 "Error in read REGISET2_REG %d\n", err);
2352 if ((info->ch_ilim_usb != 0xFF) || (info->ch_ilim_usb <= 0x1D)) {
2353 val2 = info->ch_ilim_usb;
2354 } else {/* Keep OTP value */
2355 val2 = (val & 0x1F);
2363 err = ricoh619_write(info->dev->parent, REGISET2_REG,val);
2365 dev_err(info->dev, "Error in writing REGISET2_REG %d\n",
2371 err = ricoh619_read(info->dev->parent, REGISET2_REG,&val);
2372 RICOH_FG_DBG("PMU : %s : after REGISET2_REG (0x%x) is 0x%x info->ch_ilim_usb is 0x%x\n",__func__,REGISET2_REG,val,info->ch_ilim_usb);
2374 /* CHGISET_REG(0xB8) setting */
2376 err = ricoh619_read(info->dev->parent, CHGISET_REG,&val);
2377 RICOH_FG_DBG("PMU : %s : before CHGISET_REG (0x%x) is 0x%x info->ch_ichg is 0x%x info->ch_icchg is 0x%x\n",__func__,CHGISET_REG,val,info->ch_ichg,info->ch_icchg);
2379 err = ricoh619_read(info->dev->parent, CHGISET_REG, &val);
2382 "Error in read CHGISET_REG %d\n", err);
2386 /* Define Current settings value for charging (bit 4~0)*/
2387 if ((info->ch_ichg != 0xFF) || (info->ch_ichg <= 0x1D)) {
2388 val2 = info->ch_ichg;
2389 } else { /* Keep OTP value */
2390 val2 = (val & 0x1F);
2393 /* Define Current settings at the charge completion (bit 7~6)*/
2394 if ((info->ch_icchg != 0xFF) || (info->ch_icchg <= 0x03)) {
2395 val3 = info->ch_icchg << 6;
2396 } else { /* Keep OTP value */
2402 err = ricoh619_write(info->dev->parent, CHGISET_REG, val);
2404 dev_err(info->dev, "Error in writing CHGISET_REG %d\n",
2410 err = ricoh619_read(info->dev->parent, CHGISET_REG,&val);
2411 RICOH_FG_DBG("PMU : %s : after CHGISET_REG (0x%x) is 0x%x info->ch_ichg is 0x%x info->ch_icchg is 0x%x\n",__func__,CHGISET_REG,val,info->ch_ichg,info->ch_icchg);
2414 err = ricoh619_read(info->dev->parent, BATSET1_REG,&val);
2415 RICOH_FG_DBG("PMU : %s : before BATSET1_REG (0x%x) is 0x%x info->ch_vbatovset is 0x%x\n",__func__,BATSET1_REG,val,info->ch_vbatovset);
2417 /* BATSET1_REG(0xBA) setting */
2418 err = ricoh619_read(info->dev->parent, BATSET1_REG, &val);
2421 "Error in read BATSET1 register %d\n", err);
2425 /* Define Battery overvoltage (bit 4)*/
2426 if ((info->ch_vbatovset != 0xFF) || (info->ch_vbatovset <= 0x1)) {
2427 val2 = info->ch_vbatovset;
2429 } else { /* Keep OTP value */
2430 val2 = (val & 0x10);
2433 /* keep bit 0-3 and bit 5-7 */
2438 err = ricoh619_write(info->dev->parent, BATSET1_REG, val);
2440 dev_err(info->dev, "Error in writing BAT1_REG %d\n",
2445 err = ricoh619_read(info->dev->parent, BATSET1_REG,&val);
2446 RICOH_FG_DBG("PMU : %s : after BATSET1_REG (0x%x) is 0x%x info->ch_vbatovset is 0x%x\n",__func__,BATSET1_REG,val,info->ch_vbatovset);
2449 err = ricoh619_read(info->dev->parent, BATSET2_REG,&val);
2450 RICOH_FG_DBG("PMU : %s : before BATSET2_REG (0x%x) is 0x%x info->ch_vrchg is 0x%x info->ch_vfchg is 0x%x \n",__func__,BATSET2_REG,val,info->ch_vrchg,info->ch_vfchg);
2453 /* BATSET2_REG(0xBB) setting */
2454 err = ricoh619_read(info->dev->parent, BATSET2_REG, &val);
2457 "Error in read BATSET2 register %d\n", err);
2461 /* Define Re-charging voltage (bit 2~0)*/
2462 if ((info->ch_vrchg != 0xFF) || (info->ch_vrchg <= 0x04)) {
2463 val2 = info->ch_vrchg;
2464 } else { /* Keep OTP value */
2465 val2 = (val & 0x07);
2468 /* Define FULL charging voltage (bit 6~4)*/
2469 if ((info->ch_vfchg != 0xFF) || (info->ch_vfchg <= 0x04)) {
2470 val3 = info->ch_vfchg;
2472 } else { /* Keep OTP value */
2473 val3 = (val & 0x70);
2476 /* keep bit 3 and bit 7 */
2479 val = val + val2 + val3;
2481 err = ricoh619_write(info->dev->parent, BATSET2_REG, val);
2483 dev_err(info->dev, "Error in writing RICOH619_RE_CHARGE_VOLTAGE %d\n",
2489 err = ricoh619_read(info->dev->parent, BATSET2_REG,&val);
2490 RICOH_FG_DBG("PMU : %s : after BATSET2_REG (0x%x) is 0x%x info->ch_vrchg is 0x%x info->ch_vfchg is 0x%x \n",__func__,BATSET2_REG,val,info->ch_vrchg,info->ch_vfchg);
2492 /* Set rising edge setting ([1:0]=01b)for INT in charging */
2493 /* and rising edge setting ([3:2]=01b)for charge completion */
2494 err = ricoh619_read(info->dev->parent, RICOH619_CHG_STAT_DETMOD1, &val);
2496 dev_err(info->dev, "Error in reading CHG_STAT_DETMOD1 %d\n",
2502 err = ricoh619_write(info->dev->parent, RICOH619_CHG_STAT_DETMOD1, val);
2504 dev_err(info->dev, "Error in writing CHG_STAT_DETMOD1 %d\n",
2509 /* Unmask In charging/charge completion */
2510 err = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGSTS1, 0xfc);
2512 dev_err(info->dev, "Error in writing INT_MSK_CHGSTS1 %d\n",
2517 /* Set both edge for VUSB([3:2]=11b)/VADP([1:0]=11b) detect */
2518 err = ricoh619_read(info->dev->parent, RICOH619_CHG_CTRL_DETMOD1, &val);
2520 dev_err(info->dev, "Error in reading CHG_CTRL_DETMOD1 %d\n",
2526 err = ricoh619_write(info->dev->parent, RICOH619_CHG_CTRL_DETMOD1, val);
2528 dev_err(info->dev, "Error in writing CHG_CTRL_DETMOD1 %d\n",
2533 /* Unmask In VUSB/VADP completion */
2534 err = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGCTR, 0xfc);
2536 dev_err(info->dev, "Error in writing INT_MSK_CHGSTS1 %d\n",
2541 if (charge_status != POWER_SUPPLY_STATUS_FULL)
2543 /* Enable charging */
2544 err = ricoh619_set_bits(info->dev->parent,CHGCTL1_REG, 0x03);
2546 dev_err(info->dev, "Error in writing the control register\n");
2550 /* get OCV100_min, OCV100_min*/
2551 temp = (battery_init_para[info->num][24]<<8) | (battery_init_para[info->num][25]);
2552 rbat = temp * 1000 / 512 * 5000 / 4095;
2554 /* get vfchg value */
2555 err = ricoh619_read(info->dev->parent, BATSET2_REG, &val);
2557 dev_err(info->dev, "Error in reading the batset2reg\n");
2563 vfchg_val = 4050 + val2 * 50;
2567 RICOH_FG_DBG("PMU : %s : test test val %d, val2 %d vfchg %d\n", __func__, val, val2, vfchg_val);
2570 err = ricoh619_read(info->dev->parent, CHGISET_REG, &val);
2572 dev_err(info->dev, "Error in reading the chgisetreg\n");
2577 icchg_val = 50 + val2 * 50;
2578 RICOH_FG_DBG("PMU : %s : test test val %d, val2 %d icchg %d\n", __func__, val, val2, icchg_val);
2580 info->soca->OCV100_min = ( vfchg_val * 99 / 100 - (icchg_val * (rbat +20))/1000 - 20 ) * 1000;
2581 info->soca->OCV100_max = ( vfchg_val * 101 / 100 - (icchg_val * (rbat +20))/1000 + 20 ) * 1000;
2583 RICOH_FG_DBG("PMU : %s : 100 min %d, 100 max %d vfchg %d icchg %d rbat %d\n",__func__,
2584 info->soca->OCV100_min,info->soca->OCV100_max,vfchg_val,icchg_val,rbat);
2586 #ifdef ENABLE_LOW_BATTERY_DETECTION
2587 /* Set ADRQ=00 to stop ADC */
2588 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT3, 0x0);
2589 /* Enable VSYS threshold Low interrupt */
2590 ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
2591 /* Set ADC auto conversion interval 250ms */
2592 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT2, 0x0);
2593 /* Enable VSYS pin conversion in auto-ADC */
2594 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT1, 0x10);
2595 /* Set VSYS threshold low voltage value = (voltage(V)*255)/(3*2.5) */
2596 val = info->alarm_vol_mv * 255 / 7500;
2597 ricoh619_write(info->dev->parent, RICOH619_ADC_VSYS_THL, val);
2598 /* Start auto-mode & average 4-time conversion mode for ADC */
2599 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT3, 0x28);
2600 /* Enable master ADC INT */
2601 ricoh619_set_bits(info->dev->parent, RICOH619_INTC_INTEN, ADC_INT);
2609 static int get_power_supply_status(struct ricoh619_battery_info *info)
2612 uint8_t supply_state;
2613 uint8_t charge_state;
2616 /* get power supply status */
2617 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
2619 dev_err(info->dev, "Error in reading the control register\n");
2623 charge_state = (status & 0x1F);
2624 supply_state = ((status & 0xC0) >> 6);
2626 if (info->entry_factory_mode)
2627 return POWER_SUPPLY_STATUS_NOT_CHARGING;
2629 if (supply_state == SUPPLY_STATE_BAT) {
2630 info->soca->chg_status = POWER_SUPPLY_STATUS_DISCHARGING;
2632 switch (charge_state) {
2633 case CHG_STATE_CHG_OFF:
2634 info->soca->chg_status
2635 = POWER_SUPPLY_STATUS_DISCHARGING;
2637 case CHG_STATE_CHG_READY_VADP:
2638 info->soca->chg_status
2639 = POWER_SUPPLY_STATUS_NOT_CHARGING;
2641 case CHG_STATE_CHG_TRICKLE:
2642 info->soca->chg_status
2643 = POWER_SUPPLY_STATUS_CHARGING;
2645 case CHG_STATE_CHG_RAPID:
2646 info->soca->chg_status
2647 = POWER_SUPPLY_STATUS_CHARGING;
2649 case CHG_STATE_CHG_COMPLETE:
2650 info->soca->chg_status
2651 = POWER_SUPPLY_STATUS_FULL;
2653 case CHG_STATE_SUSPEND:
2654 info->soca->chg_status
2655 = POWER_SUPPLY_STATUS_DISCHARGING;
2657 case CHG_STATE_VCHG_OVER_VOL:
2658 info->soca->chg_status
2659 = POWER_SUPPLY_STATUS_DISCHARGING;
2661 case CHG_STATE_BAT_ERROR:
2662 info->soca->chg_status
2663 = POWER_SUPPLY_STATUS_NOT_CHARGING;
2665 case CHG_STATE_NO_BAT:
2666 info->soca->chg_status
2667 = POWER_SUPPLY_STATUS_NOT_CHARGING;
2669 case CHG_STATE_BAT_OVER_VOL:
2670 info->soca->chg_status
2671 = POWER_SUPPLY_STATUS_NOT_CHARGING;
2673 case CHG_STATE_BAT_TEMP_ERR:
2674 info->soca->chg_status
2675 = POWER_SUPPLY_STATUS_NOT_CHARGING;
2677 case CHG_STATE_DIE_ERR:
2678 info->soca->chg_status
2679 = POWER_SUPPLY_STATUS_NOT_CHARGING;
2681 case CHG_STATE_DIE_SHUTDOWN:
2682 info->soca->chg_status
2683 = POWER_SUPPLY_STATUS_DISCHARGING;
2685 case CHG_STATE_NO_BAT2:
2686 info->soca->chg_status
2687 = POWER_SUPPLY_STATUS_NOT_CHARGING;
2689 case CHG_STATE_CHG_READY_VUSB:
2690 info->soca->chg_status
2691 = POWER_SUPPLY_STATUS_NOT_CHARGING;
2694 info->soca->chg_status
2695 = POWER_SUPPLY_STATUS_UNKNOWN;
2700 return info->soca->chg_status;
2703 static void charger_irq_work(struct work_struct *work)
2705 struct ricoh619_battery_info *info
2706 = container_of(work, struct ricoh619_battery_info, irq_work);
2709 RICOH_FG_DBG("PMU:%s In\n", __func__);
2711 power_supply_changed(&info->battery);
2713 mutex_lock(&info->lock);
2715 if (info->chg_stat1 & 0x01) {
2716 ricoh619_read(info->dev->parent, CHGSTATE_REG, ®_val);
2717 if (reg_val & 0x40) { /* USE ADP */
2718 /* set adp limit current 2A */
2719 ricoh619_write(info->dev->parent, REGISET1_REG, 0x13);
2720 /* set charge current 2A */
2721 ricoh619_write(info->dev->parent, CHGISET_REG, 0xD3);
2723 else if (reg_val & 0x80) { /* USE USB */
2724 queue_work(info->usb_workqueue, &info->usb_irq_work);
2728 info->chg_stat1 = 0;
2730 /* Enable Interrupt for VADP/USB */
2731 ret = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGCTR, 0xfc);
2734 "%s(): Error in enable charger mask INT %d\n",
2737 /* Enable Interrupt for Charging & complete */
2738 ret = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGSTS1, 0xfc);
2741 "%s(): Error in enable charger mask INT %d\n",
2744 mutex_unlock(&info->lock);
2745 RICOH_FG_DBG("PMU:%s Out\n", __func__);
2748 #ifdef ENABLE_LOW_BATTERY_DETECTION
2749 static void low_battery_irq_work(struct work_struct *work)
2751 struct ricoh619_battery_info *info = container_of(work,
2752 struct ricoh619_battery_info, low_battery_work.work);
2756 RICOH_FG_DBG("PMU:%s In\n", __func__);
2758 power_supply_changed(&info->battery);
2760 /* Enable VADP threshold Low interrupt */
2761 ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
2764 "%s(): Error in enable adc mask INT %d\n",
2770 static void ricoh619_usb_charge_det(void)
2772 struct ricoh619 *ricoh619 = g_ricoh619;
2773 ricoh619_set_bits(ricoh619->dev,REGISET2_REG,(1 << 7)); //set usb limit current when SDP or other mode
2774 if(2 == dwc_vbus_status()){
2775 ricoh619_write(ricoh619->dev,REGISET2_REG,0x13); //set usb limit current 2A
2776 ricoh619_write(ricoh619->dev,CHGISET_REG,0xD3); //set charge current 2A
2778 else if(1 == dwc_vbus_status()){
2779 ricoh619_write(ricoh619->dev,REGISET2_REG,0x04); //set usb limit current 500ma
2780 ricoh619_write(ricoh619->dev,CHGISET_REG,0xC4); //set charge current 500ma
2784 static void usb_det_irq_work(struct work_struct *work)
2786 struct ricoh619_battery_info *info = container_of(work,
2787 struct ricoh619_battery_info, usb_irq_work);
2792 RICOH_FG_DBG("PMU:%s In\n", __func__);
2794 power_supply_changed(&info->battery);
2796 mutex_lock(&info->lock);
2798 /* Enable Interrupt for VUSB */
2799 ret = ricoh619_clr_bits(info->dev->parent,
2800 RICOH619_INT_MSK_CHGCTR, 0x02);
2803 "%s(): Error in enable charger mask INT %d\n",
2806 mutex_unlock(&info->lock);
2807 ret = ricoh619_read(info->dev->parent, RICOH619_INT_MON_CHGCTR, &sts);
2809 dev_err(info->dev, "Error in reading the control register\n");
2815 //ricoh619_usb_charge_det();
2821 /*********************/
2823 /*********************/
2826 RICOH_FG_DBG("PMU:%s Out\n", __func__);
2829 extern void rk28_send_wakeup_key(void);
2830 static irqreturn_t charger_in_isr(int irq, void *battery_info)
2832 struct ricoh619_battery_info *info = battery_info;
2833 struct ricoh619 *ricoh619 = g_ricoh619;
2835 RICOH_FG_DBG("PMU:%s\n", __func__);
2837 info->chg_stat1 |= 0x01;
2839 queue_work(info->workqueue, &info->irq_work);
2840 // rk28_send_wakeup_key();
2845 static irqreturn_t charger_complete_isr(int irq, void *battery_info)
2847 struct ricoh619_battery_info *info = battery_info;
2848 RICOH_FG_DBG("PMU:%s\n", __func__);
2850 info->chg_stat1 |= 0x02;
2851 queue_work(info->workqueue, &info->irq_work);
2852 rk28_send_wakeup_key();
2857 static irqreturn_t charger_usb_isr(int irq, void *battery_info)
2859 struct ricoh619_battery_info *info = battery_info;
2860 RICOH_FG_DBG("PMU:%s\n", __func__);
2862 info->chg_ctr |= 0x02;
2864 queue_work(info->workqueue, &info->irq_work);
2866 info->soca->dischg_state = 0;
2867 info->soca->chg_count = 0;
2869 // queue_work(info->usb_workqueue, &info->usb_irq_work);
2870 // rk28_send_wakeup_key();
2872 if (RICOH619_SOCA_UNSTABLE == info->soca->status
2873 || RICOH619_SOCA_FG_RESET == info->soca->status)
2874 info->soca->stable_count = 11;
2879 static irqreturn_t charger_adp_isr(int irq, void *battery_info)
2881 struct ricoh619_battery_info *info = battery_info;
2882 struct ricoh619 *ricoh619 = g_ricoh619;
2883 RICOH_FG_DBG("PMU:%s\n", __func__);
2885 info->chg_ctr |= 0x01;
2886 queue_work(info->workqueue, &info->irq_work);
2887 rk28_send_wakeup_key();
2889 info->soca->dischg_state = 0;
2890 info->soca->chg_count = 0;
2891 if (RICOH619_SOCA_UNSTABLE == info->soca->status
2892 || RICOH619_SOCA_FG_RESET == info->soca->status)
2893 info->soca->stable_count = 11;
2899 #ifdef ENABLE_LOW_BATTERY_DETECTION
2900 /*************************************************************/
2901 /* for Detecting Low Battery */
2902 /*************************************************************/
2904 static irqreturn_t adc_vsysl_isr(int irq, void *battery_info)
2907 struct ricoh619_battery_info *info = battery_info;
2910 RICOH_FG_DBG("PMU:%s\n", __func__);
2912 queue_delayed_work(info->monitor_wqueue, &info->low_battery_work,
2913 LOW_BATTERY_DETECTION_TIME*HZ);
2917 RICOH_FG_DBG("PMU:%s\n", __func__);
2918 // ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
2919 // rk28_send_wakeup_key();
2926 * Get Charger Priority
2927 * - get higher-priority between VADP and VUSB
2928 * @ data: higher-priority is stored
2932 static int get_charge_priority(struct ricoh619_battery_info *info, bool *data)
2937 ret = ricoh619_read(info->dev->parent, CHGCTL1_REG, &val);
2945 * Set Charger Priority
2946 * - set higher-priority between VADP and VUSB
2947 * - data: higher-priority is stored
2951 static int set_charge_priority(struct ricoh619_battery_info *info, bool *data)
2959 ret = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, val);
2963 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
2964 static int get_check_fuel_gauge_reg(struct ricoh619_battery_info *info,
2965 int Reg_h, int Reg_l, int enable_bit)
2967 uint8_t get_data_h, get_data_l;
2968 int old_data, current_data;
2974 for (i = 0; i < 5 ; i++) {
2975 ret = ricoh619_read(info->dev->parent, Reg_h, &get_data_h);
2977 dev_err(info->dev, "Error in reading the control register\n");
2981 ret = ricoh619_read(info->dev->parent, Reg_l, &get_data_l);
2983 dev_err(info->dev, "Error in reading the control register\n");
2987 current_data = ((get_data_h & 0xff) << 8) | (get_data_l & 0xff);
2988 current_data = (current_data & enable_bit);
2990 if (current_data == old_data)
2991 return current_data;
2993 old_data = current_data;
2996 return current_data;
2999 static int calc_capacity(struct ricoh619_battery_info *info)
3007 temperature = get_battery_temp_2(info) / 10; /* unit 0.1 degree -> 1 degree */
3009 if (temperature >= 25) {
3011 } else if (temperature >= 5) {
3012 nt = (25 - temperature) * RICOH619_TAH_SEL2 * 625 / 100;
3014 nt = (625 + (5 - temperature) * RICOH619_TAL_SEL2 * 625 / 100);
3017 /* get remaining battery capacity from fuel gauge */
3018 ret = ricoh619_read(info->dev->parent, SOC_REG, &capacity);
3020 dev_err(info->dev, "Error in reading the control register\n");
3024 temp = capacity * 100 * 100 / (10000 - nt);
3026 return temp; /* Unit is 1% */
3029 static int calc_capacity_2(struct ricoh619_battery_info *info)
3039 temperature = get_battery_temp_2(info) / 10; /* unit 0.1 degree -> 1 degree */
3041 if (temperature >= 25) {
3043 } else if (temperature >= 5) {
3044 nt = (25 - temperature) * RICOH619_TAH_SEL2 * 625 / 100;
3046 nt = (625 + (5 - temperature) * RICOH619_TAL_SEL2 * 625 / 100);
3049 re_cap = get_check_fuel_gauge_reg(info, RE_CAP_H_REG, RE_CAP_L_REG,
3051 fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
3055 capacity = ((long)re_cap * 100 * 100 / fa_cap) + 50;
3056 capacity = min(10000, capacity);
3057 capacity = max(0, capacity);
3059 ret = ricoh619_read(info->dev->parent, SOC_REG, &val);
3061 dev_err(info->dev, "Error in reading the control register\n");
3064 capacity = (long)val * 100;
3068 temp = (int)(capacity * 100 * 100 / (10000 - nt));
3070 return temp; /* Unit is 0.01% */
3073 static int get_battery_temp(struct ricoh619_battery_info *info)
3078 ret = get_check_fuel_gauge_reg(info, TEMP_1_REG, TEMP_2_REG, 0x0fff);
3080 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
3084 /* bit3 of 0xED(TEMP_1) is sign_bit */
3085 sign_bit = ((ret & 0x0800) >> 11);
3087 ret = (ret & 0x07ff);
3089 if (sign_bit == 0) /* positive value part */
3090 /* conversion unit */
3091 /* 1 unit is 0.0625 degree and retun unit
3092 * should be 0.1 degree,
3094 ret = ret * 625 / 1000;
3095 else { /*negative value part */
3096 ret = (~ret + 1) & 0x7ff;
3097 ret = -1 * ret * 625 / 1000;
3103 static int get_battery_temp_2(struct ricoh619_battery_info *info)
3105 uint8_t reg_buff[2];
3106 long temp, temp_off, temp_gain;
3107 bool temp_sign, temp_off_sign, temp_gain_sign;
3116 /* Calculate TEMP */
3117 ret = get_check_fuel_gauge_reg(info, TEMP_1_REG, TEMP_2_REG, 0x0fff);
3119 dev_err(info->dev, "Error in reading the fuel gauge register\n");
3124 temp_sign = (reg_val & 0x0800) >> 11;
3125 reg_val = (reg_val & 0x07ff);
3127 if (temp_sign == 0) /* positive value part */
3128 /* the unit is 0.0001 degree */
3129 temp = (long)reg_val * 625;
3130 else { /*negative value part */
3131 reg_val = (~reg_val + 1) & 0x7ff;
3132 temp = -1 * (long)reg_val * 625;
3135 /* Calculate TEMP_OFF */
3136 ret = ricoh619_bulk_reads_bank1(info->dev->parent,
3137 TEMP_OFF_H_REG, 2, reg_buff);
3139 dev_err(info->dev, "Error in reading the fuel gauge register\n");
3143 reg_val = reg_buff[0] << 8 | reg_buff[1];
3144 temp_off_sign = (reg_val & 0x0800) >> 11;
3145 reg_val = (reg_val & 0x07ff);
3147 if (temp_off_sign == 0) /* positive value part */
3148 /* the unit is 0.0001 degree */
3149 temp_off = (long)reg_val * 625;
3150 else { /*negative value part */
3151 reg_val = (~reg_val + 1) & 0x7ff;
3152 temp_off = -1 * (long)reg_val * 625;
3155 /* Calculate TEMP_GAIN */
3156 ret = ricoh619_bulk_reads_bank1(info->dev->parent,
3157 TEMP_GAIN_H_REG, 2, reg_buff);
3159 dev_err(info->dev, "Error in reading the fuel gauge register\n");
3163 reg_val = reg_buff[0] << 8 | reg_buff[1];
3164 temp_gain_sign = (reg_val & 0x0800) >> 11;
3165 reg_val = (reg_val & 0x07ff);
3167 if (temp_gain_sign == 0) /* positive value part */
3168 /* 1 unit is 0.000488281. the result is 0.01 */
3169 temp_gain = (long)reg_val * 488281 / 100000;
3170 else { /*negative value part */
3171 reg_val = (~reg_val + 1) & 0x7ff;
3172 temp_gain = -1 * (long)reg_val * 488281 / 100000;
3175 /* Calculate VTHM */
3177 Vthm = (int)((temp - temp_off) / 4095 * 2500 / temp_gain);
3179 RICOH_FG_DBG("PMU %s Skip to compensate temperature\n", __func__);
3183 ret = measure_Ibatt_FG(info, &Iout);
3184 Vsns = Iout * 2 / 100;
3186 if (temp < -120000) {
3187 /* Low Temperature */
3188 if (0 != (2500 - Vthm)) {
3189 Rthm = 10 * 10 * (Vthm - Vsns) / (2500 - Vthm);
3191 RICOH_FG_DBG("PMU %s Skip to compensate temperature\n", __func__);
3196 R_ln2 = (R_ln1 * R_ln1 * R_ln1 * R_ln1 * R_ln1 / 100000
3197 - R_ln1 * R_ln1 * R_ln1 * R_ln1 * 2 / 100
3198 + R_ln1 * R_ln1 * R_ln1 * 11
3199 - R_ln1 * R_ln1 * 2980
3203 /* the unit of new_temp is 0.1 degree */
3204 new_temp = (int)((100 * 1000 * B_VALUE / (R_ln2 + B_VALUE * 100 * 1000 / 29815) - 27315) / 10);
3205 RICOH_FG_DBG("PMU %s low temperature %d\n", __func__, new_temp/10);
3206 } else if (temp > 520000) {
3207 /* High Temperature */
3208 if (0 != (2500 - Vthm)) {
3209 Rthm = 100 * 10 * (Vthm - Vsns) / (2500 - Vthm);
3211 RICOH_FG_DBG("PMU %s Skip to compensate temperature\n", __func__);
3214 RICOH_FG_DBG("PMU %s [Rthm] Rthm %d[ohm]\n", __func__, Rthm);
3217 R_ln2 = (R_ln1 * R_ln1 * R_ln1 * R_ln1 * R_ln1 / 100000 * 15652 / 100
3218 - R_ln1 * R_ln1 * R_ln1 * R_ln1 / 1000 * 23103 / 100
3219 + R_ln1 * R_ln1 * R_ln1 * 1298 / 100
3220 - R_ln1 * R_ln1 * 35089 / 100
3221 + R_ln1 * 50334 / 10
3223 /* the unit of new_temp is 0.1 degree */
3224 new_temp = (int)((100 * 100 * B_VALUE / (R_ln2 + B_VALUE * 100 * 100 / 29815) - 27315) / 10);
3225 RICOH_FG_DBG("PMU %s high temperature %d\n", __func__, new_temp/10);
3227 /* the unit of new_temp is 0.1 degree */
3228 new_temp = temp / 1000;
3234 new_temp = get_battery_temp(info);
3238 static int get_time_to_empty(struct ricoh619_battery_info *info)
3242 ret = get_check_fuel_gauge_reg(info, TT_EMPTY_H_REG, TT_EMPTY_L_REG,
3245 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
3249 /* conversion unit */
3250 /* 1unit is 1miniute and return nnit should be 1 second */
3256 static int get_time_to_full(struct ricoh619_battery_info *info)
3260 ret = get_check_fuel_gauge_reg(info, TT_FULL_H_REG, TT_FULL_L_REG,
3263 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
3272 /* battery voltage is get from Fuel gauge */
3273 static int measure_vbatt_FG(struct ricoh619_battery_info *info, int *data)
3277 ret = get_check_fuel_gauge_reg(info, VOLTAGE_1_REG, VOLTAGE_2_REG,
3280 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
3285 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
3286 *data = *data * 50000 / 4095;
3287 /* return unit should be 1uV */
3288 *data = *data * 100;
3293 static int measure_Ibatt_FG(struct ricoh619_battery_info *info, int *data)
3297 ret = get_check_fuel_gauge_reg(info, CC_AVERAGE1_REG,
3298 CC_AVERAGE0_REG, 0x3fff);
3300 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
3304 *data = (ret > 0x1fff) ? (ret - 0x4000) : ret;
3308 static int get_OCV_init_Data(struct ricoh619_battery_info *info, int index)
3311 ret = (battery_init_para[info->num][index*2]<<8) | (battery_init_para[info->num][index*2+1]);
3315 static int get_OCV_voltage(struct ricoh619_battery_info *info, int index)
3318 ret = get_OCV_init_Data(info, index);
3319 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
3320 ret = ret * 50000 / 4095;
3321 /* return unit should be 1uV */
3327 /* battery voltage is get from ADC */
3328 static int measure_vbatt_ADC(struct ricoh619_battery_info *info, int *data)
3331 uint8_t data_l = 0, data_h = 0;
3334 /* ADC interrupt enable */
3335 ret = ricoh619_set_bits(info->dev->parent, INTEN_REG, 0x08);
3337 dev_err(info->dev, "Error in setting the control register bit\n");
3341 /* enable interrupt request of single mode */
3342 ret = ricoh619_set_bits(info->dev->parent, EN_ADCIR3_REG, 0x01);
3344 dev_err(info->dev, "Error in setting the control register bit\n");
3348 /* single request */
3349 ret = ricoh619_write(info->dev->parent, ADCCNT3_REG, 0x10);
3351 dev_err(info->dev, "Error in writing the control register\n");
3355 for (i = 0; i < 5; i++) {
3357 RICOH_FG_DBG("ADC conversion times: %d\n", i);
3358 /* read completed flag of ADC */
3359 ret = ricoh619_read(info->dev->parent, EN_ADCIR3_REG, &data_h);
3361 dev_err(info->dev, "Error in reading the control register\n");
3369 dev_err(info->dev, "ADC conversion too long!\n");
3373 ret = ricoh619_read(info->dev->parent, VBATDATAH_REG, &data_h);
3375 dev_err(info->dev, "Error in reading the control register\n");
3379 ret = ricoh619_read(info->dev->parent, VBATDATAL_REG, &data_l);
3381 dev_err(info->dev, "Error in reading the control register\n");
3385 *data = ((data_h & 0xff) << 4) | (data_l & 0x0f);
3386 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
3387 *data = *data * 5000 / 4095;
3388 /* return unit should be 1uV */
3389 *data = *data * 1000;
3398 static int measure_vsys_ADC(struct ricoh619_battery_info *info, int *data)
3400 uint8_t data_l = 0, data_h = 0;
3403 ret = ricoh619_read(info->dev->parent, VSYSDATAH_REG, &data_h);
3405 dev_err(info->dev, "Error in reading the control register\n");
3408 ret = ricoh619_read(info->dev->parent, VSYSDATAL_REG, &data_l);
3410 dev_err(info->dev, "Error in reading the control register\n");
3413 *data = ((data_h & 0xff) << 4) | (data_l & 0x0f);
3414 *data = *data * 1000 * 3 * 5 / 2 / 4095;
3415 /* return unit should be 1uV */
3416 *data = *data * 1000;
3421 static void ricoh619_external_power_changed(struct power_supply *psy)
3423 struct ricoh619_battery_info *info;
3425 info = container_of(psy, struct ricoh619_battery_info, battery);
3426 queue_delayed_work(info->monitor_wqueue,
3427 &info->changed_work, HZ / 2);
3432 static int ricoh619_batt_get_prop(struct power_supply *psy,
3433 enum power_supply_property psp,
3434 union power_supply_propval *val)
3436 struct ricoh619_battery_info *info = dev_get_drvdata(psy->dev->parent);
3441 mutex_lock(&info->lock);
3445 case POWER_SUPPLY_PROP_ONLINE:
3446 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
3448 dev_err(info->dev, "Error in reading the control register\n");
3449 mutex_unlock(&info->lock);
3452 if (psy->type == POWER_SUPPLY_TYPE_MAINS)
3453 val->intval = (status & 0x40 ? 1 : 0);
3454 else if (psy->type == POWER_SUPPLY_TYPE_USB)
3455 val->intval = (status & 0x80 ? 1 : 0);
3458 /* this setting is same as battery driver of 584 */
3459 case POWER_SUPPLY_PROP_STATUS:
3460 ret = get_power_supply_status(info);
3463 /* RICOH_FG_DBG("Power Supply Status is %d\n",
3467 /* this setting is same as battery driver of 584 */
3468 case POWER_SUPPLY_PROP_PRESENT:
3469 val->intval = info->present;
3472 /* current voltage is get from fuel gauge */
3473 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
3474 /* return real vbatt Voltage */
3475 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
3476 if (info->soca->ready_fg)
3477 ret = measure_vbatt_FG(info, &data);
3479 //val->intval = -EINVAL;
3480 data = info->cur_voltage * 1000;
3481 /* RICOH_FG_DBG( "battery voltage is not ready\n"); */
3484 ret = measure_vbatt_ADC(info, &data);
3487 /* convert unit uV -> mV */
3488 info->cur_voltage = data / 1000;
3490 RICOH_FG_DBG( "battery voltage is %d mV\n",
3494 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
3495 /* current battery capacity is get from fuel gauge */
3496 case POWER_SUPPLY_PROP_CAPACITY:
3497 if (info->entry_factory_mode){
3499 info->capacity = 100;
3500 } else if (info->soca->displayed_soc <= 0) {
3504 val->intval = (info->soca->displayed_soc + 50)/100;
3505 info->capacity = (info->soca->displayed_soc + 50)/100;
3507 /* RICOH_FG_DBG("battery capacity is %d%%\n",
3511 /* current temperature of battery */
3512 case POWER_SUPPLY_PROP_TEMP:
3513 if (info->soca->ready_fg) {
3515 val->intval = get_battery_temp_2(info);
3516 info->battery_temp = val->intval/10;
3517 RICOH_FG_DBG( "battery temperature is %d degree\n", info->battery_temp);
3519 val->intval = info->battery_temp * 10;
3520 /* RICOH_FG_DBG("battery temperature is not ready\n"); */
3524 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
3525 if (info->soca->ready_fg) {
3526 ret = get_time_to_empty(info);
3528 info->time_to_empty = ret/60;
3529 RICOH_FG_DBG("time of empty battery is %d minutes\n", info->time_to_empty);
3531 //val->intval = -EINVAL;
3532 val->intval = info->time_to_empty * 60;
3533 RICOH_FG_DBG("time of empty battery is %d minutes\n", info->time_to_empty);
3534 /* RICOH_FG_DBG( "time of empty battery is not ready\n"); */
3538 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
3539 if (info->soca->ready_fg) {
3540 ret = get_time_to_full(info);
3542 info->time_to_full = ret/60;
3543 RICOH_FG_DBG( "time of full battery is %d minutes\n", info->time_to_full);
3545 //val->intval = -EINVAL;
3546 val->intval = info->time_to_full * 60;
3547 RICOH_FG_DBG( "time of full battery is %d minutes\n", info->time_to_full);
3548 /* RICOH_FG_DBG("time of full battery is not ready\n"); */
3552 case POWER_SUPPLY_PROP_TECHNOLOGY:
3553 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
3557 case POWER_SUPPLY_PROP_HEALTH:
3558 val->intval = POWER_SUPPLY_HEALTH_GOOD;
3561 case POWER_SUPPLY_PROP_CURRENT_AVG:
3562 measure_Ibatt_FG(info, &data);
3563 //RICOH_FG_DBG("average current xxxxxxxxxxxxxx %d \n", data);
3566 mutex_unlock(&info->lock);
3570 mutex_unlock(&info->lock);
3575 static enum power_supply_property ricoh619_batt_props[] = {
3576 POWER_SUPPLY_PROP_STATUS,
3577 POWER_SUPPLY_PROP_PRESENT,
3578 POWER_SUPPLY_PROP_VOLTAGE_NOW,
3579 POWER_SUPPLY_PROP_CURRENT_AVG,
3581 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
3582 POWER_SUPPLY_PROP_CAPACITY,
3583 POWER_SUPPLY_PROP_TEMP,
3584 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
3585 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
3587 POWER_SUPPLY_PROP_TECHNOLOGY,
3588 POWER_SUPPLY_PROP_HEALTH,
3591 static enum power_supply_property ricoh619_power_props[] = {
3592 POWER_SUPPLY_PROP_ONLINE,
3595 struct power_supply powerac = {
3597 .type = POWER_SUPPLY_TYPE_MAINS,
3598 .properties = ricoh619_power_props,
3599 .num_properties = ARRAY_SIZE(ricoh619_power_props),
3600 .get_property = ricoh619_batt_get_prop,
3603 struct power_supply powerusb = {
3605 .type = POWER_SUPPLY_TYPE_USB,
3606 .properties = ricoh619_power_props,
3607 .num_properties = ARRAY_SIZE(ricoh619_power_props),
3608 .get_property = ricoh619_batt_get_prop,
3611 static __devinit int ricoh619_battery_probe(struct platform_device *pdev)
3613 struct ricoh619_battery_info *info;
3614 struct ricoh619_battery_platform_data *pdata;
3618 RICOH_FG_DBG("PMU:2013.8.3 %s\n", __func__);
3620 info = kzalloc(sizeof(struct ricoh619_battery_info), GFP_KERNEL);
3623 info->soca = kzalloc(sizeof(struct ricoh619_soca_info), GFP_KERNEL);
3627 info->dev = &pdev->dev;
3628 info->status = POWER_SUPPLY_STATUS_CHARGING;
3629 pdata = pdev->dev.platform_data;
3630 info->monitor_time = pdata->monitor_time * HZ;
3631 info->alarm_vol_mv = pdata->alarm_vol_mv;
3633 type_n = Battery_Type();
3634 info->num = Battery_Table();
3635 temp = sizeof(battery_init_para)/(sizeof(uint8_t)*32);
3636 RICOH_FG_DBG("%s temp=%d\n", __func__, temp);
3637 if(info->num >= (sizeof(battery_init_para)/(sizeof(uint8_t)*32)))
3639 RICOH_FG_DBG("%s type_n=%d\n", __func__, type_n);
3640 RICOH_FG_DBG("%s info->num=%d\n", __func__, info->num);
3641 /* these valuse are set in platform */
3644 info->ch_vfchg = pdata->ch_vfchg;
3645 info->ch_vrchg = pdata->ch_vrchg;
3646 info->ch_vbatovset = pdata->ch_vbatovset;
3647 info->ch_ichg = pdata->ch_ichg;
3648 info->ch_ilim_adp = pdata->ch_ilim_adp;
3649 info->ch_ilim_usb = pdata->ch_ilim_usb;
3650 info->ch_icchg = pdata->ch_icchg;
3651 info->fg_target_vsys = pdata->fg_target_vsys;
3652 info->fg_target_ibat = pdata->fg_target_ibat;
3653 info->jt_en = pdata->jt_en;
3654 info->jt_hw_sw = pdata->jt_hw_sw;
3655 info->jt_temp_h = pdata->jt_temp_h;
3656 info->jt_temp_l = pdata->jt_temp_l;
3657 info->jt_vfchg_h = pdata->jt_vfchg_h;
3658 info->jt_vfchg_l = pdata->jt_vfchg_l;
3659 info->jt_ichg_h = pdata->jt_ichg_h;
3660 info->jt_ichg_l = pdata->jt_ichg_l;
3663 info->adc_vdd_mv = ADC_VDD_MV; /* 2800; */
3664 info->min_voltage = MIN_VOLTAGE; /* 3100; */
3665 info->max_voltage = MAX_VOLTAGE; /* 4200; */
3667 info->entry_factory_mode = false;
3669 mutex_init(&info->lock);
3670 platform_set_drvdata(pdev, info);
3672 info->battery.name = "battery";
3673 info->battery.type = POWER_SUPPLY_TYPE_BATTERY;
3674 info->battery.properties = ricoh619_batt_props;
3675 info->battery.num_properties = ARRAY_SIZE(ricoh619_batt_props);
3676 info->battery.get_property = ricoh619_batt_get_prop;
3677 info->battery.set_property = NULL;
3678 /* info->battery.external_power_changed
3679 = ricoh619_external_power_changed; */
3681 /* Disable Charger/ADC interrupt */
3682 ret = ricoh619_clr_bits(info->dev->parent, RICOH619_INTC_INTEN,
3687 ret = ricoh619_init_battery(info);
3691 #ifdef ENABLE_FACTORY_MODE
3692 info->factory_mode_wqueue
3693 = create_singlethread_workqueue("ricoh619_factory_mode");
3694 INIT_DELAYED_WORK_DEFERRABLE(&info->factory_mode_work,
3695 check_charging_state_work);
3697 ret = ricoh619_factory_mode(info);
3703 ret = power_supply_register(&pdev->dev, &info->battery);
3706 info->battery.dev->parent = &pdev->dev;
3708 ret = power_supply_register(&pdev->dev, &powerac);
3709 ret = power_supply_register(&pdev->dev, &powerusb);
3711 info->monitor_wqueue
3712 = create_singlethread_workqueue("ricoh619_battery_monitor");
3714 info->workqueue = create_singlethread_workqueue("rc5t619_charger_in");
3715 INIT_WORK(&info->irq_work, charger_irq_work);
3718 = create_singlethread_workqueue("rc5t619_usb_det");
3719 INIT_WORK(&info->usb_irq_work, usb_det_irq_work);
3721 INIT_DELAYED_WORK_DEFERRABLE(&info->monitor_work,
3722 ricoh619_battery_work);
3723 INIT_DELAYED_WORK_DEFERRABLE(&info->displayed_work,
3724 ricoh619_displayed_work);
3725 INIT_DELAYED_WORK_DEFERRABLE(&info->charge_stable_work,
3726 ricoh619_stable_charge_countdown_work);
3727 INIT_DELAYED_WORK_DEFERRABLE(&info->charge_monitor_work,
3728 ricoh619_charge_monitor_work);
3729 INIT_DELAYED_WORK_DEFERRABLE(&info->get_charge_work,
3730 ricoh619_get_charge_work);
3731 INIT_DELAYED_WORK_DEFERRABLE(&info->jeita_work, ricoh619_jeita_work);
3732 INIT_DELAYED_WORK(&info->changed_work, ricoh619_changed_work);
3734 /* Charger IRQ workqueue settings */
3735 charger_irq = pdata->irq;
3737 ret = request_threaded_irq(charger_irq + RICOH619_IRQ_FONCHGINT,
3738 NULL, charger_in_isr, IRQF_ONESHOT,
3739 "rc5t619_charger_in", info);
3741 dev_err(&pdev->dev, "Can't get CHG_INT IRQ for chrager: %d\n",
3746 ret = request_threaded_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT,
3747 NULL, charger_complete_isr,
3748 IRQF_ONESHOT, "rc5t619_charger_comp",
3751 dev_err(&pdev->dev, "Can't get CHG_COMP IRQ for chrager: %d\n",
3756 ret = request_threaded_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT,
3757 NULL, charger_usb_isr, IRQF_ONESHOT,
3758 "rc5t619_usb_det", info);
3760 dev_err(&pdev->dev, "Can't get USB_DET IRQ for chrager: %d\n",
3765 ret = request_threaded_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT,
3766 NULL, charger_adp_isr, IRQF_ONESHOT,
3767 "rc5t619_adp_det", info);
3770 "Can't get ADP_DET IRQ for chrager: %d\n", ret);
3774 #ifdef ENABLE_LOW_BATTERY_DETECTION
3775 ret = request_threaded_irq(charger_irq + RICOH619_IRQ_VSYSLIR,
3776 NULL, adc_vsysl_isr, IRQF_ONESHOT,
3777 "rc5t619_adc_vsysl", info);
3780 "Can't get ADC_VSYSL IRQ for chrager: %d\n", ret);
3783 INIT_DELAYED_WORK_DEFERRABLE(&info->low_battery_work,
3784 low_battery_irq_work);
3787 /* Charger init and IRQ setting */
3788 ret = ricoh619_init_charger(info);
3792 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
3793 ret = ricoh619_init_fgsoca(info);
3795 queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
3796 RICOH619_MONITOR_START_TIME*HZ);
3798 /* Enable Charger interrupt */
3799 ricoh619_set_bits(info->dev->parent, RICOH619_INTC_INTEN, CHG_INT);
3808 static int __devexit ricoh619_battery_remove(struct platform_device *pdev)
3810 struct ricoh619_battery_info *info = platform_get_drvdata(pdev);
3815 bool is_charging = true;
3816 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
3818 && (info->soca->status == RICOH619_SOCA_STABLE)) {
3819 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
3821 dev_err(info->dev, "Error in writing PSWR_REG\n");
3823 } else if (info->soca->status != RICOH619_SOCA_START
3824 && info->soca->status != RICOH619_SOCA_UNSTABLE) {
3825 if (info->soca->displayed_soc <= 0) {
3828 val = (info->soca->displayed_soc + 50)/100;
3831 ret = ricoh619_write(info->dev->parent, PSWR_REG, val);
3833 dev_err(info->dev, "Error in writing PSWR_REG\n");
3837 ret = calc_capacity_in_period(info, &cc_cap,
3838 &is_charging, true);
3840 dev_err(info->dev, "Read cc_sum Error !!-----\n");
3843 if (g_fg_on_mode == 0) {
3844 ret = ricoh619_clr_bits(info->dev->parent,
3847 dev_err(info->dev, "Error in clr FG EN\n");
3850 /* set rapid timer 300 min */
3851 err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x03);
3853 dev_err(info->dev, "Error in writing the control register\n");
3856 free_irq(charger_irq + RICOH619_IRQ_FONCHGINT, &info);
3857 free_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT, &info);
3858 free_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT, &info);
3859 free_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT, &info);
3860 #ifdef ENABLE_LOW_BATTERY_DETECTION
3861 free_irq(charger_irq + RICOH619_IRQ_VSYSLIR, &info);
3864 cancel_delayed_work(&info->monitor_work);
3865 cancel_delayed_work(&info->charge_stable_work);
3866 cancel_delayed_work(&info->charge_monitor_work);
3867 cancel_delayed_work(&info->get_charge_work);
3868 cancel_delayed_work(&info->displayed_work);
3870 cancel_delayed_work(&info->changed_work);
3871 #ifdef ENABLE_LOW_BATTERY_DETECTION
3872 cancel_delayed_work(&info->low_battery_work);
3874 cancel_delayed_work(&info->factory_mode_work);
3875 cancel_delayed_work(&info->jeita_work);
3877 cancel_work_sync(&info->irq_work);
3878 cancel_work_sync(&info->usb_irq_work);
3880 flush_workqueue(info->monitor_wqueue);
3881 flush_workqueue(info->workqueue);
3882 flush_workqueue(info->usb_workqueue);
3883 flush_workqueue(info->factory_mode_wqueue);
3885 destroy_workqueue(info->monitor_wqueue);
3886 destroy_workqueue(info->workqueue);
3887 destroy_workqueue(info->usb_workqueue);
3888 destroy_workqueue(info->factory_mode_wqueue);
3890 power_supply_unregister(&info->battery);
3892 platform_set_drvdata(pdev, NULL);
3897 static int ricoh619_battery_suspend(struct device *dev)
3899 struct ricoh619_battery_info *info = dev_get_drvdata(dev);
3904 bool is_charging = true;
3907 && (info->soca->status == RICOH619_SOCA_STABLE)) {
3908 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
3910 dev_err(info->dev, "Error in writing PSWR_REG\n");
3912 info->soca->suspend_soc = info->soca->displayed_soc;
3913 } else if (info->soca->status != RICOH619_SOCA_START
3914 && info->soca->status != RICOH619_SOCA_UNSTABLE) {
3915 if (info->soca->displayed_soc <= 0) {
3918 val = (info->soca->displayed_soc + 50)/100;
3921 ret = ricoh619_write(info->dev->parent, PSWR_REG, val);
3923 dev_err(info->dev, "Error in writing PSWR_REG\n");
3927 info->soca->suspend_soc = info->soca->displayed_soc;
3929 ret = calc_capacity_in_period(info, &cc_cap,
3930 &is_charging, true);
3932 dev_err(info->dev, "Read cc_sum Error !!-----\n");
3933 } else if (info->soca->status == RICOH619_SOCA_START
3934 || info->soca->status == RICOH619_SOCA_UNSTABLE) {
3936 ret = ricoh619_read(info->dev->parent, PSWR_REG, &val);
3938 dev_err(info->dev, "Error in reading the pswr register\n");
3941 info->soca->suspend_soc = val * 100;
3944 if (info->soca->status == RICOH619_SOCA_DISP
3945 || info->soca->status == RICOH619_SOCA_STABLE
3946 || info->soca->status == RICOH619_SOCA_FULL) {
3947 info->soca->soc = calc_capacity_2(info);
3948 info->soca->soc_delta =
3949 info->soca->soc_delta + (info->soca->soc - info->soca->last_soc);
3952 info->soca->soc_delta = 0;
3955 if (info->soca->status == RICOH619_SOCA_STABLE
3956 || info->soca->status == RICOH619_SOCA_FULL)
3957 info->soca->status = RICOH619_SOCA_DISP;
3958 /* set rapid timer 300 min */
3959 err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x03);
3961 dev_err(info->dev, "Error in writing the control register\n");
3964 // disable_irq(charger_irq + RICOH619_IRQ_FONCHGINT);
3965 // disable_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT);
3966 // disable_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT);
3967 // disable_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT);
3968 #ifdef ENABLE_LOW_BATTERY_DETECTION
3969 // disable_irq(charger_irq + RICOH619_IRQ_VSYSLIR);
3972 flush_delayed_work(&info->monitor_work);
3973 flush_delayed_work(&info->displayed_work);
3974 flush_delayed_work(&info->charge_stable_work);
3975 flush_delayed_work(&info->charge_monitor_work);
3976 flush_delayed_work(&info->get_charge_work);
3977 flush_delayed_work(&info->changed_work);
3978 #ifdef ENABLE_LOW_BATTERY_DETECTION
3979 flush_delayed_work(&info->low_battery_work);
3981 flush_delayed_work(&info->factory_mode_work);
3982 flush_delayed_work(&info->jeita_work);
3984 // flush_work(&info->irq_work);
3985 // flush_work(&info->usb_irq_work);
3991 static int ricoh619_battery_resume(struct device *dev)
3993 struct ricoh619_battery_info *info = dev_get_drvdata(dev);
3996 int displayed_soc_temp;
3998 bool is_charging = true;
3999 bool is_jeita_updated;
4002 RICOH_FG_DBG(KERN_INFO "PMU: %s: \n", __func__);
4004 ret = check_jeita_status(info, &is_jeita_updated);
4006 dev_err(info->dev, "Error in updating JEITA %d\n", ret);
4009 if (info->entry_factory_mode) {
4010 info->soca->displayed_soc = -EINVAL;
4011 } else if (RICOH619_SOCA_ZERO == info->soca->status) {
4012 if (calc_ocv(info) > get_OCV_voltage(info, 0)) {
4013 ret = ricoh619_read(info->dev->parent, PSWR_REG, &val);
4015 info->soca->soc = val * 100;
4018 "Error in reading PSWR_REG %d\n", ret);
4020 = calc_capacity(info) * 100;
4023 ret = calc_capacity_in_period(info, &cc_cap,
4024 &is_charging, true);
4026 dev_err(info->dev, "Read cc_sum Error !!-----\n");
4028 info->soca->cc_delta
4029 = (is_charging == true) ? cc_cap : -cc_cap;
4032 = info->soca->soc + info->soca->cc_delta;
4033 if (displayed_soc_temp < 0)
4034 displayed_soc_temp = 0;
4035 displayed_soc_temp = min(10000, displayed_soc_temp);
4036 displayed_soc_temp = max(0, displayed_soc_temp);
4037 info->soca->displayed_soc = displayed_soc_temp;
4039 ret = ricoh619_write(info->dev->parent,
4042 dev_err(info->dev, "Error in writing the control register\n");
4043 info->soca->ready_fg = 0;
4044 info->soca->status = RICOH619_SOCA_FG_RESET;
4047 info->soca->displayed_soc = 0;
4049 info->soca->soc = info->soca->suspend_soc;
4051 if (RICOH619_SOCA_START == info->soca->status
4052 || RICOH619_SOCA_UNSTABLE == info->soca->status) {
4053 ret = calc_capacity_in_period(info, &cc_cap,
4054 &is_charging, false);
4056 ret = calc_capacity_in_period(info, &cc_cap,
4057 &is_charging, true);
4061 dev_err(info->dev, "Read cc_sum Error !!-----\n");
4063 info->soca->cc_delta = (is_charging == true) ? cc_cap : -cc_cap;
4065 displayed_soc_temp = info->soca->soc + info->soca->cc_delta;
4066 if (displayed_soc_temp < 0)
4067 displayed_soc_temp = 0;
4068 displayed_soc_temp = min(10000, displayed_soc_temp);
4069 displayed_soc_temp = max(0, displayed_soc_temp);
4071 if (0 == info->soca->jt_limit) {
4072 check_charge_status_2(info, displayed_soc_temp);
4074 info->soca->displayed_soc = displayed_soc_temp;
4077 if (RICOH619_SOCA_DISP == info->soca->status) {
4078 info->soca->last_soc = calc_capacity_2(info);
4081 info->soca->update_count = 0;
4083 ret = measure_vbatt_FG(info, &info->soca->Vbat_ave);
4084 ret = measure_vsys_ADC(info, &info->soca->Vsys_ave);
4085 ret = measure_Ibatt_FG(info, &info->soca->Ibat_ave);
4087 power_supply_changed(&info->battery);
4088 queue_delayed_work(info->monitor_wqueue, &info->displayed_work, HZ);
4090 if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
4091 info->soca->stable_count = 10;
4092 queue_delayed_work(info->monitor_wqueue,
4093 &info->charge_stable_work,
4094 RICOH619_FG_STABLE_TIME*HZ/10);
4095 } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
4096 info->soca->stable_count = 1;
4098 for (i = 0; i < 3; i = i+1)
4099 info->soca->reset_soc[i] = 0;
4100 info->soca->reset_count = 0;
4102 queue_delayed_work(info->monitor_wqueue,
4103 &info->charge_stable_work,
4104 RICOH619_FG_RESET_TIME*HZ);
4107 queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
4108 info->monitor_time);
4110 queue_delayed_work(info->monitor_wqueue, &info->charge_monitor_work,
4111 RICOH619_CHARGE_RESUME_TIME * HZ);
4113 info->soca->chg_count = 0;
4114 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
4115 RICOH619_CHARGE_RESUME_TIME * HZ);
4117 if (!info->jt_hw_sw) {
4118 queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
4119 RICOH619_JEITA_UPDATE_TIME * HZ);
4122 // ricoh619_write(info->dev->parent, 0x9d, 0x00);
4123 // enable_irq(charger_irq + RICOH619_IRQ_FONCHGINT);
4124 // enable_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT);
4125 // enable_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT);
4126 // enable_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT);
4127 #ifdef ENABLE_LOW_BATTERY_DETECTION
4128 // enable_irq(charger_irq + RICOH619_IRQ_VSYSLIR);
4130 ricoh619_write(info->dev->parent, 0x9d, 0x4d);
4134 static const struct dev_pm_ops ricoh619_battery_pm_ops = {
4135 .suspend = ricoh619_battery_suspend,
4136 .resume = ricoh619_battery_resume,
4140 static struct platform_driver ricoh619_battery_driver = {
4142 .name = "ricoh619-battery",
4143 .owner = THIS_MODULE,
4145 .pm = &ricoh619_battery_pm_ops,
4148 .probe = ricoh619_battery_probe,
4149 .remove = __devexit_p(ricoh619_battery_remove),
4152 static int __init ricoh619_battery_init(void)
4154 RICOH_FG_DBG("PMU: %s\n", __func__);
4155 return platform_driver_register(&ricoh619_battery_driver);
4157 subsys_initcall_sync(ricoh619_battery_init);
4159 static void __exit ricoh619_battery_exit(void)
4161 platform_driver_unregister(&ricoh619_battery_driver);
4163 module_exit(ricoh619_battery_exit);
4165 MODULE_DESCRIPTION("RICOH619 Battery driver");
4166 MODULE_ALIAS("platform:ricoh619-battery");
4167 MODULE_LICENSE("GPL");