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 #define RICOH619_BATTERY_VERSION "RICOH619_BATTERY_VERSION: 2014.05.06"
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/platform_device.h>
28 #include <linux/slab.h>
29 #include <linux/mutex.h>
30 #include <linux/string.h>
31 #include <linux/power_supply.h>
32 #include <linux/mfd/ricoh619.h>
33 #include <linux/power/ricoh619_battery.h>
34 #include <linux/power/ricoh61x_battery_init.h>
35 #include <linux/delay.h>
36 #include <linux/workqueue.h>
40 #include <linux/interrupt.h>
41 #include <linux/irq.h>
42 #include <linux/irqdomain.h>
45 /* define for function */
46 #define ENABLE_FUEL_GAUGE_FUNCTION
47 #define ENABLE_LOW_BATTERY_DETECTION
48 #define ENABLE_FACTORY_MODE
49 #define DISABLE_CHARGER_TIMER
50 /* #define ENABLE_FG_KEEP_ON_MODE */
51 /* #define ENABLE_OCV_TABLE_CALIB */
52 //#define SUPPORT_USB_CONNECT_TO_ADP
56 #define RICOH619_REL1_SEL_VALUE 64
57 #define RICOH619_REL2_SEL_VALUE 0
67 //for debug #ifdef ENABLE_FUEL_GAUGE_FUNCTION
68 /* define for FG delayed time */
69 #define RICOH619_MONITOR_START_TIME 15
70 #define RICOH619_FG_RESET_TIME 6
71 #define RICOH619_FG_STABLE_TIME 120
72 #define RICOH619_DISPLAY_UPDATE_TIME 15
73 #define RICOH619_LOW_VOL_DOWN_TIME 10
74 #define RICOH619_CHARGE_MONITOR_TIME 20
75 #define RICOH619_CHARGE_RESUME_TIME 1
76 #define RICOH619_CHARGE_CALC_TIME 1
77 #define RICOH619_JEITA_UPDATE_TIME 60
78 #define RICOH619_DELAY_TIME 60
79 /* define for FG parameter */
80 #define RICOH619_MAX_RESET_SOC_DIFF 5
81 #define RICOH619_GET_CHARGE_NUM 10
82 #define RICOH619_UPDATE_COUNT_DISP 4
83 #define RICOH619_UPDATE_COUNT_FULL 4
84 #define RICOH619_UPDATE_COUNT_FULL_RESET 7
85 #define RICOH619_CHARGE_UPDATE_TIME 3
86 #define RICOH619_FULL_WAIT_TIME 4
87 #define RE_CAP_GO_DOWN 10 /* 40 */
88 #define RICOH619_ENTER_LOW_VOL 70
89 #define RICOH619_TAH_SEL2 5
90 #define RICOH619_TAL_SEL2 6
92 #define RICOH619_OCV_OFFSET_BOUND 3
93 #define RICOH619_OCV_OFFSET_RATIO 2
95 #define RICOH619_VADP_DROP_WORK 1
96 #define RICOH619_TIME_CHG_STEP (1*HZ)// unit:secound
97 #define RICOH619_TIME_CHG_COUNT 15*60//only for test //15*60
99 /* define for FG status */
102 RICOH619_SOCA_UNSTABLE,
103 RICOH619_SOCA_FG_RESET,
105 RICOH619_SOCA_STABLE,
108 RICOH619_SOCA_LOW_VOL,
112 #ifdef ENABLE_LOW_BATTERY_DETECTION
113 #define LOW_BATTERY_DETECTION_TIME 10
116 struct ricoh619_soca_info {
119 int ocv_table_def[11];
121 int ocv_table_low[11];
122 int soc; /* Latest FG SOC value */
125 int status; /* SOCA status 0: Not initial; 5: Finished */
127 int chg_status; /* chg_status */
128 int soc_delta; /* soc delta for status3(DISP) */
132 int last_displayed_soc;
138 int Vbat[RICOH619_GET_CHARGE_NUM];
139 int Vsys[RICOH619_GET_CHARGE_NUM];
140 int Ibat[RICOH619_GET_CHARGE_NUM];
146 int full_reset_count;
149 /* for LOW VOL state */
167 struct ricoh619_battery_info {
169 struct power_supply battery;
170 struct delayed_work monitor_work;
171 struct delayed_work displayed_work;
172 struct delayed_work charge_stable_work;
173 struct delayed_work changed_work;
174 #ifdef ENABLE_LOW_BATTERY_DETECTION
175 struct delayed_work low_battery_work;
177 struct delayed_work charge_monitor_work;
178 struct delayed_work get_charge_work;
179 struct delayed_work jeita_work;
180 struct delayed_work charge_complete_ready;
182 struct work_struct irq_work; /* for Charging & VUSB/VADP */
183 struct work_struct usb_irq_work; /* for ADC_VUSB */
184 #ifdef RICOH619_VADP_DROP_WORK
185 struct delayed_work vadp_drop_work;
187 struct workqueue_struct *monitor_wqueue;
188 struct workqueue_struct *workqueue; /* for Charging & VUSB/VADP */
189 struct workqueue_struct *usb_workqueue; /* for ADC_VUSB */
191 #ifdef ENABLE_FACTORY_MODE
192 struct delayed_work factory_mode_work;
193 struct workqueue_struct *factory_mode_wqueue;
197 unsigned long monitor_time;
213 struct ricoh619_soca_info *soca;
215 bool entry_factory_mode;
235 int chg_complete_rd_flag;
236 int chg_complete_rd_cnt;
237 int chg_complete_tm_ov_flag;
238 int chg_complete_sleep_flag;
244 struct power_supply powerac;
245 struct power_supply powerusb;
249 /* this value is for mfd fucntion */
253 extern int dwc_otg_check_dpdm(bool wait);
254 /*This is for full state*/
255 static int BatteryTableFlagDef=0;
256 static int BatteryTypeDef=0;
257 static int Battery_Type(void)
259 return BatteryTypeDef;
262 static int Battery_Table(void)
264 return BatteryTableFlagDef;
267 static void ricoh619_battery_work(struct work_struct *work)
269 struct ricoh619_battery_info *info = container_of(work,
270 struct ricoh619_battery_info, monitor_work.work);
272 RICOH_FG_DBG("PMU: %s\n", __func__);
273 power_supply_changed(&info->battery);
274 queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
278 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
279 static int measure_vbatt_FG(struct ricoh619_battery_info *info, int *data);
280 static int measure_Ibatt_FG(struct ricoh619_battery_info *info, int *data);
281 static int calc_capacity(struct ricoh619_battery_info *info);
282 static int calc_capacity_2(struct ricoh619_battery_info *info);
283 static int get_OCV_init_Data(struct ricoh619_battery_info *info, int index);
284 static int get_OCV_voltage(struct ricoh619_battery_info *info, int index);
285 static int get_check_fuel_gauge_reg(struct ricoh619_battery_info *info,
286 int Reg_h, int Reg_l, int enable_bit);
287 static int calc_capacity_in_period(struct ricoh619_battery_info *info,
288 int *cc_cap, bool *is_charging, bool cc_rst);
289 //static int get_charge_priority(struct ricoh619_battery_info *info, bool *data);
290 //static int set_charge_priority(struct ricoh619_battery_info *info, bool *data);
291 static int get_power_supply_status(struct ricoh619_battery_info *info);
292 static int get_power_supply_Android_status(struct ricoh619_battery_info *info);
293 static int measure_vsys_ADC(struct ricoh619_battery_info *info, int *data);
294 static int Calc_Linear_Interpolation(int x0, int y0, int x1, int y1, int y);
295 static int get_battery_temp(struct ricoh619_battery_info *info);
296 static int get_battery_temp_2(struct ricoh619_battery_info *info);
297 static int check_jeita_status(struct ricoh619_battery_info *info, bool *is_jeita_updated);
298 static void ricoh619_scaling_OCV_table(struct ricoh619_battery_info *info, int cutoff_vol, int full_vol, int *start_per, int *end_per);
299 static int ricoh619_Check_OCV_Offset(struct ricoh619_battery_info *info);
301 static int calc_ocv(struct ricoh619_battery_info *info)
308 ret = measure_vbatt_FG(info, &Vbat);
309 ret = measure_Ibatt_FG(info, &Ibat);
311 ocv = Vbat - Ibat * info->soca->Rbat;
317 static int set_Rlow(struct ricoh619_battery_info *info)
325 if (info->soca->Rbat == 0)
326 info->soca->Rbat = get_OCV_init_Data(info, 12) * 1000 / 512
329 Vocv = calc_ocv(info);
330 Rbat_low_max = info->soca->Rbat * 1.5;
332 if (Vocv < get_OCV_voltage(info,3))
334 info->soca->R_low = Calc_Linear_Interpolation(info->soca->Rbat,get_OCV_voltage(info,3),
335 Rbat_low_max, get_OCV_voltage(info,0), Vocv);
336 RICOH_FG_DBG("PMU: Modify RBAT from %d to %d ", info->soca->Rbat, info->soca->R_low);
337 temp = info->soca->R_low *4095/5000*512/1000;
339 val = info->soca->R_low>>8;
340 err = ricoh619_write_bank1(info->dev->parent, 0xD4, val);
342 dev_err(info->dev, "batterry initialize error\n");
346 val = info->soca->R_low & 0xff;
347 err = ricoh619_write_bank1(info->dev->parent, 0xD5, val);
349 dev_err(info->dev, "batterry initialize error\n");
353 else info->soca->R_low = 0;
359 static int Set_back_ocv_table(struct ricoh619_battery_info *info)
365 uint8_t debug_disp[22];
367 /* Modify back ocv table */
369 if (0 != info->soca->ocv_table_low[0])
371 for (i = 0 ; i < 11; i++){
372 battery_init_para[info->num][i*2 + 1] = info->soca->ocv_table_low[i];
373 battery_init_para[info->num][i*2] = info->soca->ocv_table_low[i] >> 8;
375 err = ricoh619_clr_bits(info->dev->parent, FG_CTRL_REG, 0x01);
377 err = ricoh619_bulk_writes_bank1(info->dev->parent,
378 BAT_INIT_TOP_REG, 22, battery_init_para[info->num]);
380 err = ricoh619_set_bits(info->dev->parent, FG_CTRL_REG, 0x01);
382 /* debug comment start*/
383 err = ricoh619_bulk_reads_bank1(info->dev->parent,
384 BAT_INIT_TOP_REG, 22, debug_disp);
385 for (i = 0; i < 11; i++){
386 RICOH_FG_DBG("PMU : %s : after OCV table %d 0x%x\n",__func__, i * 10, (debug_disp[i*2] << 8 | debug_disp[i*2+1]));
390 for(i = 0; i < 11; i++)
392 info->soca->ocv_table_low[i] = 0;
396 /* Modify back Rbat */
397 if (0!=info->soca->R_low)
399 RICOH_FG_DBG("PMU: Modify back RBAT from %d to %d ", info->soca->R_low,info->soca->Rbat);
400 temp = info->soca->Rbat*4095/5000*512/1000;
402 val = info->soca->R_low>>8;
403 err = ricoh619_write_bank1(info->dev->parent, 0xD4, val);
405 dev_err(info->dev, "batterry initialize error\n");
409 val = info->soca->R_low & 0xff;
410 err = ricoh619_write_bank1(info->dev->parent, 0xD5, val);
412 dev_err(info->dev, "batterry initialize error\n");
416 info->soca->R_low = 0;
423 static int ricoh619_Check_OCV_Offset(struct ricoh619_battery_info *info)
425 int ocv_table[11]; // HEX value
429 uint8_t debug_disp[22];
432 RICOH_FG_DBG("PMU : %s : calc ocv %d get OCV %d\n",__func__,calc_ocv(info),get_OCV_voltage(info, RICOH619_OCV_OFFSET_BOUND));
434 /* check adp/usb status */
435 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &val);
437 dev_err(info->dev, "Error in reading the control register\n");
441 val = (val & 0xC0) >> 6;
443 if (val != 0){ /* connect adp or usb */
444 if (calc_ocv(info) < get_OCV_voltage(info, RICOH619_OCV_OFFSET_BOUND) )
446 if(0 == info->soca->ocv_table_low[0]){
447 for (i = 0 ; i < 11; i++){
448 ocv_table[i] = (battery_init_para[info->num][i*2]<<8) | (battery_init_para[info->num][i*2+1]);
449 RICOH_FG_DBG("PMU : %s : OCV table %d 0x%x\n",__func__,i * 10, ocv_table[i]);
450 info->soca->ocv_table_low[i] = ocv_table[i];
453 for (i = 0 ; i < 11; i++){
454 temp = ocv_table[i] * (100 + RICOH619_OCV_OFFSET_RATIO) / 100;
456 battery_init_para[info->num][i*2 + 1] = temp;
457 battery_init_para[info->num][i*2] = temp >> 8;
459 ret = ricoh619_clr_bits(info->dev->parent, FG_CTRL_REG, 0x01);
461 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
462 BAT_INIT_TOP_REG, 22, battery_init_para[info->num]);
464 ret = ricoh619_set_bits(info->dev->parent, FG_CTRL_REG, 0x01);
466 /* debug comment start*/
467 ret = ricoh619_bulk_reads_bank1(info->dev->parent,
468 BAT_INIT_TOP_REG, 22, debug_disp);
469 for (i = 0; i < 11; i++){
470 RICOH_FG_DBG("PMU : %s : after OCV table %d 0x%x\n",__func__, i * 10, (debug_disp[i*2] << 8 | debug_disp[i*2+1]));
480 static int reset_FG_process(struct ricoh619_battery_info *info)
484 //err = set_Rlow(info);
485 //err = ricoh619_Check_OCV_Offset(info);
486 err = ricoh619_write(info->dev->parent,
488 info->soca->ready_fg = 0;
493 static int check_charge_status_2(struct ricoh619_battery_info *info, int displayed_soc_temp)
495 if (displayed_soc_temp < 0)
496 displayed_soc_temp = 0;
498 get_power_supply_status(info);
499 info->soca->soc = calc_capacity(info) * 100;
501 if (POWER_SUPPLY_STATUS_FULL == info->soca->chg_status) {
502 if ((info->first_pwon == 1)
503 && (RICOH619_SOCA_START == info->soca->status)) {
505 info->soca->soc_full = info->soca->soc;
506 info->soca->displayed_soc = 100*100;
507 info->soca->full_reset_count = 0;
509 if ( (displayed_soc_temp > 97*100)
510 && (calc_ocv(info) > (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*7/10) )){
512 info->soca->soc_full = info->soca->soc;
513 info->soca->displayed_soc = 100*100;
514 info->soca->full_reset_count = 0;
517 info->soca->displayed_soc = displayed_soc_temp;
522 if (info->soca->Ibat_ave >= 0) {
523 if (g_full_flag == 1) {
524 info->soca->displayed_soc = 100*100;
526 if (info->soca->displayed_soc/100 < 99) {
527 info->soca->displayed_soc = displayed_soc_temp;
529 info->soca->displayed_soc = 99 * 100;
533 if (info->soca->Ibat_ave < 0) {
534 if (g_full_flag == 1) {
535 if (calc_ocv(info) < (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*7/10) ) {
537 //info->soca->displayed_soc = 100*100;
538 info->soca->displayed_soc = displayed_soc_temp;
540 info->soca->displayed_soc = 100*100;
544 info->soca->displayed_soc = displayed_soc_temp;
548 return info->soca->displayed_soc;
552 * Calculate Capacity in a period
553 * - read CC_SUM & FA_CAP from Coulom Counter
554 * - and calculate Capacity.
555 * @cc_cap: capacity in a period, unit 0.01%
556 * @is_charging: Flag of charging current direction
557 * TRUE : charging (plus)
558 * FALSE: discharging (minus)
559 * @cc_rst: reset CC_SUM or not
563 static int calc_capacity_in_period(struct ricoh619_battery_info *info,
564 int *cc_cap, bool *is_charging, bool cc_rst)
567 uint8_t cc_sum_reg[4];
568 uint8_t cc_clr[4] = {0, 0, 0, 0};
569 uint8_t fa_cap_reg[2];
574 uint8_t charge_state;
576 uint32_t cc_cap_temp;
580 *is_charging = true; /* currrent state initialize -> charging */
582 if (info->entry_factory_mode)
585 //check need charge stop or not
586 /* get power supply status */
587 err = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
590 charge_state = (status & 0x1F);
591 Ocv = calc_ocv(info);
592 if (charge_state == CHG_STATE_CHG_COMPLETE) {
593 /* Check CHG status is complete or not */
595 } else if (calc_capacity(info) == 100) {
596 /* Check HW soc is 100 or not */
598 } else if (Ocv/1000 < get_OCV_voltage(info, 9)) {
599 /* Check VBAT is high level or not */
605 if (cc_stop_flag == 1)
607 /* Disable Charging/Completion Interrupt */
608 err = ricoh619_set_bits(info->dev->parent,
609 RICOH619_INT_MSK_CHGSTS1, 0x01);
613 /* disable charging */
614 err = ricoh619_clr_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x03);
620 err = ricoh619_write(info->dev->parent, CC_CTRL_REG, 0x01);
625 err = ricoh619_bulk_reads(info->dev->parent,
626 CC_SUMREG3_REG, 4, cc_sum_reg);
630 if (cc_rst == true) {
632 err = ricoh619_bulk_writes(info->dev->parent,
633 CC_SUMREG3_REG, 4, cc_clr);
639 err = ricoh619_write(info->dev->parent, CC_CTRL_REG, 0);
642 if (cc_stop_flag == 1)
645 /* Enable charging */
646 err = ricoh619_set_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x03);
652 /* Clear Charging Interrupt status */
653 err = ricoh619_clr_bits(info->dev->parent,
654 RICOH619_INT_IR_CHGSTS1, 0x01);
658 /* ricoh619_read(info->dev->parent, RICOH619_INT_IR_CHGSTS1, &val);
659 RICOH_FG_DBG("INT_IR_CHGSTS1 = 0x%x\n",val); */
661 /* Enable Charging Interrupt */
662 err = ricoh619_clr_bits(info->dev->parent,
663 RICOH619_INT_MSK_CHGSTS1, 0x01);
668 err = ricoh619_bulk_reads(info->dev->parent,
669 FA_CAP_H_REG, 2, fa_cap_reg);
673 /* fa_cap = *(uint16_t*)fa_cap_reg & 0x7fff; */
674 fa_cap = (fa_cap_reg[0] << 8 | fa_cap_reg[1]) & 0x7fff;
676 /* cc_sum = *(uint32_t*)cc_sum_reg; */
677 cc_sum = cc_sum_reg[0] << 24 | cc_sum_reg[1] << 16 |
678 cc_sum_reg[2] << 8 | cc_sum_reg[3];
680 /* calculation two's complement of CC_SUM */
681 if (cc_sum & 0x80000000) {
682 cc_sum = (cc_sum^0xffffffff)+0x01;
683 *is_charging = false; /* discharge */
685 /* (CC_SUM x 10000)/3600/FA_CAP */
686 *cc_cap = cc_sum*25/9/fa_cap; /* unit is 0.01% */
688 //////////////////////////////////////////////////////////////////
689 cc_cap_min = fa_cap*3600/100/100/100; /* Unit is 0.0001% */
690 cc_cap_temp = cc_sum / cc_cap_min;
692 cc_cap_res = cc_cap_temp % 100;
694 RICOH_FG_DBG("PMU: cc_sum = %d: cc_cap_res= %d: \n", cc_sum, cc_cap_res);
698 info->soca->cc_cap_offset += cc_cap_res;
699 if (info->soca->cc_cap_offset >= 100) {
701 info->soca->cc_cap_offset %= 100;
704 info->soca->cc_cap_offset -= cc_cap_res;
705 if (info->soca->cc_cap_offset <= -100) {
707 info->soca->cc_cap_offset %= 100;
710 RICOH_FG_DBG("PMU: cc_cap_offset= %d: \n", info->soca->cc_cap_offset);
712 //////////////////////////////////////////////////////////////////
715 dev_err(info->dev, "Error !!-----\n");
719 * Calculate target using capacity
721 static int get_target_use_cap(struct ricoh619_battery_info *info)
726 int Target_Cutoff_Vol = 0;
742 /* get const value */
743 Ibat_min = -1 * info->soca->target_ibat;
744 if (info->soca->Ibat_ave > Ibat_min) /* I bat is minus */
748 Ibat_now = info->soca->Ibat_ave;
750 fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
752 use_cap = fa_cap - info->soca->re_cap_old;
754 /* get OCV table % */
755 for (i = 0; i <= 10; i = i+1) {
756 temp = (battery_init_para[info->num][i*2]<<8)
757 | (battery_init_para[info->num][i*2+1]);
758 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
759 temp = ((temp * 50000 * 10 / 4095) + 5) / 10;
761 RICOH_FG_DBG("PMU : %s : ocv_table %d is %d v\n",__func__, i, ocv_table[i]);
764 /* Find out Current OCV */
765 i = info->soca->soc/1000;
766 j = info->soca->soc - info->soca->soc/1000*1000;
767 Ocv_now_table = ocv_table[i]*100+(ocv_table[i+1]-ocv_table[i])*j/10;
769 Rsys_now = (info->soca->Vsys_ave - Ocv_now_table) / info->soca->Ibat_ave;
771 Ocv_ZeroPer_now = info->soca->target_vsys * 1000 - Ibat_now * Rsys_now;
773 RICOH_FG_DBG("PMU: ------- Ocv_now_table= %d: Rsys_now= %d =======\n",
774 Ocv_now_table, Rsys_now);
776 RICOH_FG_DBG("PMU: ------- Rsys= %d: cutoff_ocv= %d: Ocv_ZeroPer_now= %d =======\n",
777 info->soca->Rsys, info->soca->cutoff_ocv, Ocv_ZeroPer_now);
782 for (i = 1; i < 11; i++) {
783 RICOH_FG_DBG("PMU : %s : ocv_table %d is %d v Ocv_ZerPernow is %d\n",__func__, i, ocv_table[i],(Ocv_ZeroPer_now / 100));
784 if (ocv_table[i] >= Ocv_ZeroPer_now / 100) {
786 start_per = Calc_Linear_Interpolation(
787 (i-1)*1000, ocv_table[i-1], i*1000,
788 ocv_table[i], (Ocv_ZeroPer_now / 100));
793 start_per = max(0, start_per);
795 FA_CAP_now = fa_cap * ((10000 - start_per) / 100 ) / 100;
797 RICOH_FG_DBG("PMU: ------- Target_Cutoff_Vol= %d: Ocv_ZeroPer_now= %d: start_per= %d =======\n",
798 Target_Cutoff_Vol, Ocv_ZeroPer_now, start_per);
801 RE_CAP_now = FA_CAP_now - use_cap;
803 if (RE_CAP_now < RE_CAP_GO_DOWN) {
804 info->soca->hurry_up_flg = 1;
805 } else if (info->soca->Vsys_ave < info->soca->target_vsys*1000) {
806 info->soca->hurry_up_flg = 1;
807 } else if (info->fg_poff_vbat != 0) {
808 if (info->soca->Vbat_ave < info->fg_poff_vbat*1000) {
809 info->soca->hurry_up_flg = 1;
811 info->soca->hurry_up_flg = 0;
814 info->soca->hurry_up_flg = 0;
817 /* get CC_OnePer_step */
818 if (info->soca->displayed_soc > 0) { /* avoid divide-by-0 */
819 CC_OnePer_step = RE_CAP_now / (info->soca->displayed_soc / 100 + 1);
823 /* get info->soca->target_use_cap */
824 info->soca->target_use_cap = use_cap + CC_OnePer_step;
826 RICOH_FG_DBG("PMU: ------- FA_CAP_now= %d: RE_CAP_now= %d: CC_OnePer_step= %d: target_use_cap= %d: hurry_up_flg= %d -------\n",
827 FA_CAP_now, RE_CAP_now, CC_OnePer_step, info->soca->target_use_cap, info->soca->hurry_up_flg);
831 #ifdef ENABLE_OCV_TABLE_CALIB
833 * Calibration OCV Table
834 * - Update the value of VBAT on 100% in OCV table
835 * if battery is Full charged.
836 * - int vbat_ocv <- unit is uV
838 static int calib_ocvTable(struct ricoh619_battery_info *info, int vbat_ocv)
847 RICOH_FG_DBG("PMU: %s\n", __func__);
849 if (info->soca->Ibat_ave > RICOH619_REL1_SEL_VALUE) {
850 RICOH_FG_DBG("PMU: %s IBAT > 64mA -- Not Calibration --\n", __func__);
854 if (vbat_ocv < info->soca->OCV100_max) {
855 if (vbat_ocv < info->soca->OCV100_min)
856 ocv100_new = info->soca->OCV100_min;
858 ocv100_new = vbat_ocv;
860 ocv100_new = info->soca->OCV100_max;
862 RICOH_FG_DBG("PMU : %s :max %d min %d current %d\n",__func__,info->soca->OCV100_max,info->soca->OCV100_min,vbat_ocv);
863 RICOH_FG_DBG("PMU : %s : New OCV 100% = 0x%x\n",__func__,ocv100_new);
866 ret = ricoh619_clr_bits(info->dev->parent, FG_CTRL_REG, 0x01);
868 dev_err("PMU: %s Error in FG_En OFF\n", __func__);
873 //cutoff_ocv = (battery_init_para[info->num][0]<<8) | (battery_init_para[info->num][1]);
874 cutoff_ocv = get_OCV_voltage(info, 0);
876 info->soca->ocv_table_def[10] = info->soca->OCV100_max;
878 ricoh619_scaling_OCV_table(info, cutoff_ocv/1000, ocv100_new/1000, &start_per, &end_per);
880 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
881 BAT_INIT_TOP_REG, 22, battery_init_para[info->num]);
883 dev_err(info->dev, "batterry initialize error\n");
887 for (i = 0; i <= 10; i = i+1) {
888 info->soca->ocv_table[i] = get_OCV_voltage(info, i);
889 RICOH_FG_DBG("PMU: %s : * %d0%% voltage = %d uV\n",
890 __func__, i, info->soca->ocv_table[i]);
893 /* FG_En on & Reset*/
894 ret = reset_FG_process(info);
896 dev_err("PMU: %s Error in FG_En On & Reset\n", __func__);
900 RICOH_FG_DBG("PMU: %s Exit \n", __func__);
908 static void ricoh619_displayed_work(struct work_struct *work)
916 int displayed_soc_temp;
919 bool is_charging = true;
920 int re_cap,fa_cap,use_cap;
921 bool is_jeita_updated;
930 struct ricoh619_battery_info *info = container_of(work,
931 struct ricoh619_battery_info, displayed_work.work);
933 if (info->entry_factory_mode) {
934 info->soca->status = RICOH619_SOCA_STABLE;
935 info->soca->displayed_soc = -EINVAL;
936 info->soca->ready_fg = 0;
940 mutex_lock(&info->lock);
942 is_jeita_updated = false;
944 if ((RICOH619_SOCA_START == info->soca->status)
945 || (RICOH619_SOCA_STABLE == info->soca->status)
946 || (RICOH619_SOCA_FULL == info->soca->status))
947 info->soca->ready_fg = 1;
949 /* judge Full state or Moni Vsys state */
950 if ((RICOH619_SOCA_DISP == info->soca->status)
951 || (RICOH619_SOCA_STABLE == info->soca->status))
954 temp_ocv = get_OCV_voltage(info, 10) -
955 (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))/2;
957 if(g_full_flag == 1){ /* for issue 1 solution start*/
958 info->soca->status = RICOH619_SOCA_FULL;
959 }else if ((POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
960 && (calc_ocv(info) > temp_ocv)) {
961 info->soca->status = RICOH619_SOCA_FULL;
963 } else if (info->soca->Ibat_ave >= -20) {
964 /* for issue1 solution end */
965 /* check Full state or not*/
966 if ((calc_ocv(info) > (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*7/10))
967 || (POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
968 || (info->soca->displayed_soc > 9850))
970 info->soca->status = RICOH619_SOCA_FULL;
972 } else if ((calc_ocv(info) > (get_OCV_voltage(info, 9)))
973 && (info->soca->Ibat_ave < 300))
975 info->soca->status = RICOH619_SOCA_FULL;
978 } else { /* dis-charging */
979 if (info->soca->displayed_soc/100 < RICOH619_ENTER_LOW_VOL) {
980 info->soca->target_use_cap = 0;
981 info->soca->status = RICOH619_SOCA_LOW_VOL;
986 if (RICOH619_SOCA_STABLE == info->soca->status) {
987 info->soca->soc = calc_capacity_2(info);
988 info->soca->soc_delta = info->soca->soc - info->soca->last_soc;
990 if (info->soca->soc_delta >= -100 && info->soca->soc_delta <= 100) {
991 info->soca->displayed_soc = info->soca->soc;
993 info->soca->status = RICOH619_SOCA_DISP;
995 info->soca->last_soc = info->soca->soc;
996 info->soca->soc_delta = 0;
997 } else if (RICOH619_SOCA_FULL == info->soca->status) {
998 err = check_jeita_status(info, &is_jeita_updated);
1000 dev_err(info->dev, "Error in updating JEITA %d\n", err);
1003 info->soca->soc = calc_capacity(info) * 100;
1004 info->soca->last_soc = calc_capacity_2(info); /* for DISP */
1006 if (info->soca->Ibat_ave >= -20) { /* charging */
1007 if (0 == info->soca->jt_limit) {
1008 if (g_full_flag == 1) {
1010 if (POWER_SUPPLY_STATUS_FULL == info->soca->chg_status) {
1011 if(info->soca->full_reset_count < RICOH619_UPDATE_COUNT_FULL_RESET) {
1012 info->soca->full_reset_count++;
1013 } else if (info->soca->full_reset_count < (RICOH619_UPDATE_COUNT_FULL_RESET + 1)) {
1014 err = reset_FG_process(info);
1016 dev_err(info->dev, "Error in writing the control register\n");
1017 info->soca->full_reset_count++;
1019 } else if(info->soca->full_reset_count < (RICOH619_UPDATE_COUNT_FULL_RESET + 2)) {
1020 info->soca->full_reset_count++;
1021 info->soca->fc_cap = 0;
1022 info->soca->soc_full = info->soca->soc;
1025 if(info->soca->fc_cap < -1 * 200) {
1027 info->soca->displayed_soc = 99 * 100;
1029 info->soca->full_reset_count = 0;
1033 info->soca->chg_cmp_times = 0;
1034 err = calc_capacity_in_period(info, &cc_cap, &is_charging, true);
1036 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1038 fc_delta = (is_charging == true) ? cc_cap : -cc_cap;
1040 info->soca->fc_cap = info->soca->fc_cap + fc_delta;
1042 if (g_full_flag == 1){
1043 info->soca->displayed_soc = 100*100;
1046 if (calc_ocv(info) < (get_OCV_voltage(info, 8))) { /* fail safe*/
1048 info->soca->status = RICOH619_SOCA_DISP;
1049 info->soca->soc_delta = 0;
1050 } else if ((POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
1051 && (info->soca->displayed_soc >= 9890)){
1052 if(info->soca->chg_cmp_times > RICOH619_FULL_WAIT_TIME) {
1053 info->soca->displayed_soc = 100*100;
1055 info->soca->full_reset_count = 0;
1056 info->soca->soc_full = info->soca->soc;
1057 info->soca->fc_cap = 0;
1058 #ifdef ENABLE_OCV_TABLE_CALIB
1059 err = calib_ocvTable(info,calc_ocv(info));
1061 dev_err(info->dev, "Calibration OCV Error !!\n");
1064 info->soca->chg_cmp_times++;
1067 fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
1070 if (info->soca->displayed_soc >= 9950) {
1071 if((info->soca->soc_full - info->soca->soc) < 200) {
1075 info->soca->chg_cmp_times = 0;
1077 err = calc_capacity_in_period(info, &cc_cap, &is_charging, true);
1079 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1080 info->soca->cc_delta
1081 = (is_charging == true) ? cc_cap : -cc_cap;
1083 if((POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
1084 // || (info->soca->Ibat_ave > 200))
1085 || (info->soca->Ibat_ave < info->ch_icchg*50 + 100) || (info->soca->displayed_soc<9700))
1087 info->soca->displayed_soc += 13 * 3000 / fa_cap;
1090 info->soca->displayed_soc
1091 = info->soca->displayed_soc + info->soca->cc_delta*8/10;
1094 info->soca->displayed_soc
1095 = min(10000, info->soca->displayed_soc);
1096 info->soca->displayed_soc = max(0, info->soca->displayed_soc);
1098 if (info->soca->displayed_soc >= 9890) {
1099 info->soca->displayed_soc = 99 * 100;
1104 info->soca->full_reset_count = 0;
1106 } else { /* discharging */
1107 if (info->soca->displayed_soc >= 9950) {
1108 if (info->soca->Ibat_ave <= -1 * RICOH619_REL1_SEL_VALUE) {
1109 if ((calc_ocv(info) < (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*3/10))
1110 || ((info->soca->soc_full - info->soca->soc) > 200)) {
1113 info->soca->full_reset_count = 0;
1114 info->soca->displayed_soc = 100 * 100;
1115 info->soca->status = RICOH619_SOCA_DISP;
1116 info->soca->last_soc = info->soca->soc;
1117 info->soca->soc_delta = 0;
1119 info->soca->displayed_soc = 100 * 100;
1121 } else { /* into relaxation state */
1122 ricoh619_read(info->dev->parent, CHGSTATE_REG, ®_val);
1123 if (reg_val & 0xc0) {
1124 info->soca->displayed_soc = 100 * 100;
1127 info->soca->full_reset_count = 0;
1128 info->soca->displayed_soc = 100 * 100;
1129 info->soca->status = RICOH619_SOCA_DISP;
1130 info->soca->last_soc = info->soca->soc;
1131 info->soca->soc_delta = 0;
1136 info->soca->status = RICOH619_SOCA_DISP;
1137 info->soca->soc_delta = 0;
1138 info->soca->full_reset_count = 0;
1139 info->soca->last_soc = info->soca->soc;
1142 } else if (RICOH619_SOCA_LOW_VOL == info->soca->status) {
1143 if(info->soca->Ibat_ave >= 0) {
1144 info->soca->soc = calc_capacity(info) * 100;
1145 info->soca->status = RICOH619_SOCA_DISP;
1146 info->soca->last_soc = info->soca->soc;
1147 info->soca->soc_delta = 0;
1149 re_cap = get_check_fuel_gauge_reg(info, RE_CAP_H_REG, RE_CAP_L_REG,
1151 fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
1153 use_cap = fa_cap - re_cap;
1155 if (info->soca->target_use_cap == 0) {
1156 info->soca->re_cap_old = re_cap;
1157 get_target_use_cap(info);
1160 if(use_cap >= info->soca->target_use_cap) {
1161 info->soca->displayed_soc = info->soca->displayed_soc - 100;
1162 info->soca->displayed_soc = max(0, info->soca->displayed_soc);
1163 info->soca->re_cap_old = re_cap;
1164 } else if (info->soca->hurry_up_flg == 1) {
1165 info->soca->displayed_soc = info->soca->displayed_soc - 100;
1166 info->soca->displayed_soc = max(0, info->soca->displayed_soc);
1167 info->soca->re_cap_old = re_cap;
1169 get_target_use_cap(info);
1170 info->soca->soc = calc_capacity(info) * 100;
1173 if (RICOH619_SOCA_DISP == info->soca->status) {
1175 info->soca->soc = calc_capacity_2(info);
1177 soc_round = (info->soca->soc + 50) / 100;
1178 last_soc_round = (info->soca->last_soc + 50) / 100;
1179 last_disp_round = (info->soca->displayed_soc + 50) / 100;
1181 info->soca->soc_delta =
1182 info->soca->soc_delta + (info->soca->soc - info->soca->last_soc);
1184 info->soca->last_soc = info->soca->soc;
1186 if (last_disp_round == soc_round) {
1187 /* if SOC == DISPLAY move to stable */
1188 info->soca->displayed_soc = info->soca->soc ;
1189 info->soca->status = RICOH619_SOCA_STABLE;
1191 } else if (info->soca->Ibat_ave > 0) {
1192 if ((0 == info->soca->jt_limit) ||
1193 (POWER_SUPPLY_STATUS_FULL != info->soca->chg_status)) {
1195 if (last_disp_round < soc_round) {
1196 /* Case 1 : Charge, Display < SOC */
1197 if (info->soca->soc_delta >= 100) {
1198 info->soca->displayed_soc
1199 = last_disp_round * 100 + 50;
1200 info->soca->soc_delta -= 100;
1201 if (info->soca->soc_delta >= 100)
1204 info->soca->displayed_soc += 25;
1205 disp_dec = info->soca->displayed_soc % 100;
1206 if ((50 <= disp_dec) && (disp_dec <= 74))
1207 info->soca->soc_delta = 0;
1209 if ((info->soca->displayed_soc + 50)/100
1211 info->soca->displayed_soc
1214 = RICOH619_SOCA_STABLE;
1217 } else if (last_disp_round > soc_round) {
1218 /* Case 2 : Charge, Display > SOC */
1219 if (info->soca->soc_delta >= 300) {
1220 info->soca->displayed_soc += 100;
1221 info->soca->soc_delta -= 300;
1223 if ((info->soca->displayed_soc + 50)/100
1225 info->soca->displayed_soc
1228 = RICOH619_SOCA_STABLE;
1233 info->soca->soc_delta = 0;
1237 if (last_disp_round > soc_round) {
1238 /* Case 3 : Dis-Charge, Display > SOC */
1239 if (info->soca->soc_delta <= -100) {
1240 info->soca->displayed_soc
1241 = last_disp_round * 100 - 75;
1242 info->soca->soc_delta += 100;
1243 if (info->soca->soc_delta <= -100)
1246 info->soca->displayed_soc -= 25;
1247 disp_dec = info->soca->displayed_soc % 100;
1248 if ((25 <= disp_dec) && (disp_dec <= 49))
1249 info->soca->soc_delta = 0;
1251 if ((info->soca->displayed_soc + 50)/100
1253 info->soca->displayed_soc
1256 = RICOH619_SOCA_STABLE;
1259 } else if (last_disp_round < soc_round) {
1260 /* Case 4 : Dis-Charge, Display < SOC */
1261 if (info->soca->soc_delta <= -300) {
1262 info->soca->displayed_soc -= 100;
1263 info->soca->soc_delta += 300;
1265 if ((info->soca->displayed_soc + 50)/100
1267 info->soca->displayed_soc
1270 = RICOH619_SOCA_STABLE;
1275 } else if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
1277 temp_ocv = get_OCV_voltage(info, 10) -
1278 (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))/2;
1280 if(g_full_flag == 1){ /* for issue 1 solution start*/
1281 info->soca->status = RICOH619_SOCA_FULL;
1282 err = reset_FG_process(info);
1284 dev_err(info->dev, "Error in writing the control register\n");
1287 }else if ((POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
1288 && (calc_ocv(info) > temp_ocv)) {
1289 info->soca->status = RICOH619_SOCA_FULL;
1291 err = reset_FG_process(info);
1293 dev_err(info->dev, "Error in writing the control register\n");
1295 } else if (info->soca->Ibat_ave >= -20) {
1296 /* for issue1 solution end */
1297 /* check Full state or not*/
1298 if ((calc_ocv(info) > (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*7/10))
1299 || (POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
1300 || (info->soca->displayed_soc > 9850))
1302 info->soca->status = RICOH619_SOCA_FULL;
1304 err = reset_FG_process(info);
1306 dev_err(info->dev, "Error in writing the control register\n");
1308 } else if ((calc_ocv(info) > (get_OCV_voltage(info, 9)))
1309 && (info->soca->Ibat_ave < 300))
1311 info->soca->status = RICOH619_SOCA_FULL;
1313 err = reset_FG_process(info);
1315 dev_err(info->dev, "Error in writing the control register\n");
1320 err = ricoh619_read(info->dev->parent, PSWR_REG, &val);
1322 info->soca->soc = val * 100;
1325 "Error in reading PSWR_REG %d\n", err);
1327 = calc_capacity(info) * 100;
1330 err = calc_capacity_in_period(info, &cc_cap,
1331 &is_charging, false);
1333 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1335 info->soca->cc_delta
1336 = (is_charging == true) ? cc_cap : -cc_cap;
1339 = info->soca->soc + info->soca->cc_delta;
1340 if (displayed_soc_temp < 0)
1341 displayed_soc_temp = 0;
1343 = min(9850, displayed_soc_temp);
1344 displayed_soc_temp = max(0, displayed_soc_temp);
1346 info->soca->displayed_soc = displayed_soc_temp;
1348 } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
1350 } else if (RICOH619_SOCA_START == info->soca->status) {
1352 err = measure_Ibatt_FG(info, &Ibat);
1353 err = measure_vbatt_FG(info, &Vbat);
1354 err = measure_vsys_ADC(info, &Vsys);
1356 info->soca->Ibat_ave = Ibat;
1357 info->soca->Vbat_ave = Vbat;
1358 info->soca->Vsys_ave = Vsys;
1360 err = check_jeita_status(info, &is_jeita_updated);
1361 is_jeita_updated = false;
1363 dev_err(info->dev, "Error in updating JEITA %d\n", err);
1365 err = ricoh619_read(info->dev->parent, PSWR_REG, &val);
1367 if (info->first_pwon) {
1368 info->soca->soc = calc_capacity(info) * 100;
1369 val = (info->soca->soc + 50)/100;
1371 err = ricoh619_write(info->dev->parent, PSWR_REG, val);
1373 dev_err(info->dev, "Error in writing PSWR_REG\n");
1376 if ((info->soca->soc == 0) && (calc_ocv(info)
1377 < get_OCV_voltage(info, 0))) {
1378 info->soca->displayed_soc = 0;
1379 info->soca->status = RICOH619_SOCA_ZERO;
1381 if (0 == info->soca->jt_limit) {
1382 check_charge_status_2(info, info->soca->soc);
1384 info->soca->displayed_soc = info->soca->soc;
1387 if (info->soca->displayed_soc < 300) {
1388 info->soca->target_use_cap = 0;
1389 info->soca->status = RICOH619_SOCA_LOW_VOL;
1391 if ((info->fg_poff_vbat != 0)
1392 && (Vbat < info->fg_poff_vbat * 1000) ){
1393 info->soca->target_use_cap = 0;
1394 info->soca->status = RICOH619_SOCA_LOW_VOL;
1396 info->soca->status = RICOH619_SOCA_UNSTABLE;
1400 info->soca->status = RICOH619_SOCA_UNSTABLE;
1403 } else if (g_fg_on_mode && (val == 0x7f)) {
1404 info->soca->soc = calc_capacity(info) * 100;
1405 if ((info->soca->soc == 0) && (calc_ocv(info)
1406 < get_OCV_voltage(info, 0))) {
1407 info->soca->displayed_soc = 0;
1408 info->soca->status = RICOH619_SOCA_ZERO;
1410 if (0 == info->soca->jt_limit) {
1411 check_charge_status_2(info, info->soca->soc);
1413 info->soca->displayed_soc = info->soca->soc;
1415 info->soca->last_soc = info->soca->soc;
1416 info->soca->status = RICOH619_SOCA_STABLE;
1419 info->soca->soc = val * 100;
1422 "Error in reading PSWR_REG %d\n", err);
1424 = calc_capacity(info) * 100;
1427 err = calc_capacity_in_period(info, &cc_cap,
1428 &is_charging, false);
1430 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1432 info->soca->cc_delta
1433 = (is_charging == true) ? cc_cap : -cc_cap;
1434 if (calc_ocv(info) < get_OCV_voltage(info, 0)) {
1435 info->soca->displayed_soc = 0;
1436 info->soca->status = RICOH619_SOCA_ZERO;
1439 = info->soca->soc + info->soca->cc_delta;
1440 if (displayed_soc_temp < 0)
1441 displayed_soc_temp = 0;
1443 = min(10000, displayed_soc_temp);
1444 displayed_soc_temp = max(0, displayed_soc_temp);
1445 if (0 == info->soca->jt_limit) {
1446 check_charge_status_2(info, displayed_soc_temp);
1448 info->soca->displayed_soc = displayed_soc_temp;
1450 info->soca->last_soc = calc_capacity(info) * 100;
1452 if (info->soca->displayed_soc < 300) {
1453 info->soca->target_use_cap = 0;
1454 info->soca->status = RICOH619_SOCA_LOW_VOL;
1456 if ((info->fg_poff_vbat != 0)
1457 && (Vbat < info->fg_poff_vbat * 1000)){
1458 info->soca->target_use_cap = 0;
1459 info->soca->status = RICOH619_SOCA_LOW_VOL;
1461 info->soca->status = RICOH619_SOCA_UNSTABLE;
1465 if(info->soca->displayed_soc >= 9850)
1467 info->soca->displayed_soc = 10000;
1468 info->chg_complete_tm_ov_flag = 1;
1470 info->soca->status = RICOH619_SOCA_UNSTABLE;
1474 } else if (RICOH619_SOCA_ZERO == info->soca->status) {
1475 if (calc_ocv(info) > get_OCV_voltage(info, 0)) {
1476 err = reset_FG_process(info);
1478 dev_err(info->dev, "Error in writing the control register\n");
1479 info->soca->last_soc = calc_capacity_2(info);
1480 info->soca->status = RICOH619_SOCA_STABLE;
1482 info->soca->displayed_soc = 0;
1485 /* keep DSOC = 1 when Vbat is over 3.4V*/
1486 if( info->fg_poff_vbat != 0) {
1487 if (info->soca->zero_flg == 1) {
1488 if(info->soca->Ibat_ave >= 0) {
1489 info->soca->zero_flg = 0;
1491 info->soca->displayed_soc = 0;
1492 } else if (info->soca->displayed_soc < 50) {
1493 if (info->soca->Vbat_ave < 2000*1000) { /* error value */
1494 info->soca->displayed_soc = 100;
1495 } else if (info->soca->Vbat_ave < info->fg_poff_vbat*1000) {
1496 info->soca->displayed_soc = 0;
1497 info->soca->zero_flg = 1;
1499 info->soca->displayed_soc = 100;
1505 && (info->soca->status == RICOH619_SOCA_STABLE)) {
1506 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
1508 dev_err(info->dev, "Error in writing PSWR_REG\n");
1510 err = calc_capacity_in_period(info, &cc_cap,
1511 &is_charging, true);
1513 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1515 } else if (RICOH619_SOCA_UNSTABLE != info->soca->status) {
1516 if ((info->soca->displayed_soc + 50) / 100 <= 1) {
1519 val = (info->soca->displayed_soc + 50)/100;
1522 err = ricoh619_write(info->dev->parent, PSWR_REG, val);
1524 dev_err(info->dev, "Error in writing PSWR_REG\n");
1528 err = calc_capacity_in_period(info, &cc_cap,
1529 &is_charging, true);
1531 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1534 RICOH_FG_DBG("PMU: ------- STATUS= %d: IBAT= %d: VSYS= %d: VBAT= %d: DSOC= %d: RSOC= %d: -------\n",
1535 info->soca->status, info->soca->Ibat_ave, info->soca->Vsys_ave, info->soca->Vbat_ave,
1536 info->soca->displayed_soc, info->soca->soc);
1538 #ifdef DISABLE_CHARGER_TIMER
1539 /* clear charger timer */
1540 if ( info->soca->chg_status == POWER_SUPPLY_STATUS_CHARGING ) {
1541 err = ricoh619_read(info->dev->parent, TIMSET_REG, &val);
1544 "Error in read TIMSET_REG%d\n", err);
1545 /* to check bit 0-1 */
1549 /* set rapid timer 240 -> 300 */
1550 err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x03);
1552 dev_err(info->dev, "Error in writing the control register\n");
1555 /* set rapid timer 300 -> 240 */
1556 err = ricoh619_clr_bits(info->dev->parent, TIMSET_REG, 0x01);
1557 err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x02);
1559 dev_err(info->dev, "Error in writing the control register\n");
1565 if (0 == info->soca->ready_fg)
1566 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1567 RICOH619_FG_RESET_TIME * HZ);
1568 else if (delay_flag == 1)
1569 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1570 RICOH619_DELAY_TIME * HZ);
1571 else if (RICOH619_SOCA_DISP == info->soca->status)
1572 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1573 RICOH619_DISPLAY_UPDATE_TIME * HZ);
1574 else if (info->soca->hurry_up_flg == 1)
1575 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1576 RICOH619_LOW_VOL_DOWN_TIME * HZ);
1578 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1579 RICOH619_DISPLAY_UPDATE_TIME * HZ);
1581 mutex_unlock(&info->lock);
1583 if((true == is_jeita_updated)
1584 || (info->soca->last_displayed_soc/100 != (info->soca->displayed_soc+50)/100))
1585 power_supply_changed(&info->battery);
1587 info->soca->last_displayed_soc = info->soca->displayed_soc+50;
1589 if((info->soca->displayed_soc >= 9850) && (info->soca->Ibat_ave > -20) && (info->capacity < 100))
1591 if(info->chg_complete_rd_flag == 0)
1593 info->chg_complete_rd_flag = 1;
1594 info->chg_complete_rd_cnt = 0;
1595 queue_delayed_work(info->monitor_wqueue, &info->charge_complete_ready, 0);
1600 info->chg_complete_rd_flag = 0;
1603 if(info->chg_complete_tm_ov_flag == 1)
1605 if(info->soca->displayed_soc < 9850 || info->soca->Ibat_ave < -20)
1607 info->chg_complete_tm_ov_flag = 0;
1608 power_supply_changed(&info->battery);
1614 static void ricoh619_stable_charge_countdown_work(struct work_struct *work)
1620 struct ricoh619_battery_info *info = container_of(work,
1621 struct ricoh619_battery_info, charge_stable_work.work);
1623 if (info->entry_factory_mode)
1626 mutex_lock(&info->lock);
1627 if (RICOH619_SOCA_FG_RESET == info->soca->status)
1628 info->soca->ready_fg = 1;
1630 if (2 <= info->soca->stable_count) {
1631 if (3 == info->soca->stable_count
1632 && RICOH619_SOCA_FG_RESET == info->soca->status) {
1633 ret = reset_FG_process(info);
1635 dev_err(info->dev, "Error in writing the control register\n");
1637 info->soca->stable_count = info->soca->stable_count - 1;
1638 queue_delayed_work(info->monitor_wqueue,
1639 &info->charge_stable_work,
1640 RICOH619_FG_STABLE_TIME * HZ / 10);
1641 } else if (0 >= info->soca->stable_count) {
1642 /* Finished queue, ignore */
1643 } else if (1 == info->soca->stable_count) {
1644 if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
1645 /* Judge if FG need reset or Not */
1646 info->soca->soc = calc_capacity(info) * 100;
1647 if (info->chg_ctr != 0) {
1648 queue_delayed_work(info->monitor_wqueue,
1649 &info->charge_stable_work,
1650 RICOH619_FG_STABLE_TIME * HZ / 10);
1651 mutex_unlock(&info->lock);
1654 /* Do reset setting */
1655 ret = reset_FG_process(info);
1657 dev_err(info->dev, "Error in writing the control register\n");
1659 info->soca->status = RICOH619_SOCA_FG_RESET;
1661 /* Delay for addition Reset Time (6s) */
1662 queue_delayed_work(info->monitor_wqueue,
1663 &info->charge_stable_work,
1664 RICOH619_FG_RESET_TIME*HZ);
1665 } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
1666 info->soca->reset_soc[2] = info->soca->reset_soc[1];
1667 info->soca->reset_soc[1] = info->soca->reset_soc[0];
1668 info->soca->reset_soc[0] = calc_capacity(info) * 100;
1669 info->soca->reset_count++;
1671 if (info->soca->reset_count > 10) {
1672 /* Reset finished; */
1673 info->soca->soc = info->soca->reset_soc[0];
1674 info->soca->stable_count = 0;
1678 for (i = 0; i < 3; i++) {
1679 if (max < info->soca->reset_soc[i]/100)
1680 max = info->soca->reset_soc[i]/100;
1681 if (min > info->soca->reset_soc[i]/100)
1682 min = info->soca->reset_soc[i]/100;
1685 if ((info->soca->reset_count > 3) && ((max - min)
1686 < RICOH619_MAX_RESET_SOC_DIFF)) {
1687 /* Reset finished; */
1688 info->soca->soc = info->soca->reset_soc[0];
1689 info->soca->stable_count = 0;
1692 /* Do reset setting */
1693 ret = reset_FG_process(info);
1695 dev_err(info->dev, "Error in writing the control register\n");
1697 /* Delay for addition Reset Time (6s) */
1698 queue_delayed_work(info->monitor_wqueue,
1699 &info->charge_stable_work,
1700 RICOH619_FG_RESET_TIME*HZ);
1702 /* Finished queue From now, select FG as result; */
1703 } else if (RICOH619_SOCA_START == info->soca->status) {
1704 /* Normal condition */
1705 } else { /* other state ZERO/DISP/STABLE */
1706 info->soca->stable_count = 0;
1709 mutex_unlock(&info->lock);
1713 info->soca->last_soc = info->soca->soc;
1714 info->soca->status = RICOH619_SOCA_DISP;
1715 info->soca->soc_delta = 0;
1718 mutex_unlock(&info->lock);
1722 static void ricoh619_charge_monitor_work(struct work_struct *work)
1724 struct ricoh619_battery_info *info = container_of(work,
1725 struct ricoh619_battery_info, charge_monitor_work.work);
1727 get_power_supply_status(info);
1729 if (POWER_SUPPLY_STATUS_DISCHARGING == info->soca->chg_status
1730 || POWER_SUPPLY_STATUS_NOT_CHARGING == info->soca->chg_status) {
1731 switch (info->soca->dischg_state) {
1733 info->soca->dischg_state = 1;
1736 info->soca->dischg_state = 2;
1744 info->soca->dischg_state = 0;
1747 queue_delayed_work(info->monitor_wqueue, &info->charge_monitor_work,
1748 RICOH619_CHARGE_MONITOR_TIME * HZ);
1753 static void ricoh619_get_charge_work(struct work_struct *work)
1755 struct ricoh619_battery_info *info = container_of(work,
1756 struct ricoh619_battery_info, get_charge_work.work);
1758 int Vbat_temp, Vsys_temp, Ibat_temp;
1759 int Vbat_sort[RICOH619_GET_CHARGE_NUM];
1760 int Vsys_sort[RICOH619_GET_CHARGE_NUM];
1761 int Ibat_sort[RICOH619_GET_CHARGE_NUM];
1765 mutex_lock(&info->lock);
1767 for (i = RICOH619_GET_CHARGE_NUM-1; i > 0; i--) {
1768 if (0 == info->soca->chg_count) {
1769 info->soca->Vbat[i] = 0;
1770 info->soca->Vsys[i] = 0;
1771 info->soca->Ibat[i] = 0;
1773 info->soca->Vbat[i] = info->soca->Vbat[i-1];
1774 info->soca->Vsys[i] = info->soca->Vsys[i-1];
1775 info->soca->Ibat[i] = info->soca->Ibat[i-1];
1779 ret = measure_vbatt_FG(info, &info->soca->Vbat[0]);
1780 ret = measure_vsys_ADC(info, &info->soca->Vsys[0]);
1781 ret = measure_Ibatt_FG(info, &info->soca->Ibat[0]);
1783 info->soca->chg_count++;
1785 if (RICOH619_GET_CHARGE_NUM != info->soca->chg_count) {
1786 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1787 RICOH619_CHARGE_CALC_TIME * HZ);
1788 mutex_unlock(&info->lock);
1792 for (i = 0; i < RICOH619_GET_CHARGE_NUM; i++) {
1793 Vbat_sort[i] = info->soca->Vbat[i];
1794 Vsys_sort[i] = info->soca->Vsys[i];
1795 Ibat_sort[i] = info->soca->Ibat[i];
1801 for (i = 0; i < RICOH619_GET_CHARGE_NUM - 1; i++) {
1802 for (j = RICOH619_GET_CHARGE_NUM - 1; j > i; j--) {
1803 if (Vbat_sort[j - 1] > Vbat_sort[j]) {
1804 Vbat_temp = Vbat_sort[j];
1805 Vbat_sort[j] = Vbat_sort[j - 1];
1806 Vbat_sort[j - 1] = Vbat_temp;
1808 if (Vsys_sort[j - 1] > Vsys_sort[j]) {
1809 Vsys_temp = Vsys_sort[j];
1810 Vsys_sort[j] = Vsys_sort[j - 1];
1811 Vsys_sort[j - 1] = Vsys_temp;
1813 if (Ibat_sort[j - 1] > Ibat_sort[j]) {
1814 Ibat_temp = Ibat_sort[j];
1815 Ibat_sort[j] = Ibat_sort[j - 1];
1816 Ibat_sort[j - 1] = Ibat_temp;
1824 for (i = 3; i < RICOH619_GET_CHARGE_NUM-3; i++) {
1825 Vbat_temp = Vbat_temp + Vbat_sort[i];
1826 Vsys_temp = Vsys_temp + Vsys_sort[i];
1827 Ibat_temp = Ibat_temp + Ibat_sort[i];
1829 Vbat_temp = Vbat_temp / (RICOH619_GET_CHARGE_NUM - 6);
1830 Vsys_temp = Vsys_temp / (RICOH619_GET_CHARGE_NUM - 6);
1831 Ibat_temp = Ibat_temp / (RICOH619_GET_CHARGE_NUM - 6);
1833 if (0 == info->soca->chg_count) {
1834 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1835 RICOH619_CHARGE_UPDATE_TIME * HZ);
1836 mutex_unlock(&info->lock);
1839 info->soca->Vbat_ave = Vbat_temp;
1840 info->soca->Vsys_ave = Vsys_temp;
1841 info->soca->Ibat_ave = Ibat_temp;
1844 info->soca->chg_count = 0;
1845 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1846 RICOH619_CHARGE_UPDATE_TIME * HZ);
1847 mutex_unlock(&info->lock);
1851 /* Initial setting of FuelGauge SOCA function */
1852 static int ricoh619_init_fgsoca(struct ricoh619_battery_info *info)
1858 bool is_charging = true;
1860 for (i = 0; i <= 10; i = i+1) {
1861 info->soca->ocv_table[i] = get_OCV_voltage(info, i);
1862 RICOH_FG_DBG("PMU: %s : * %d0%% voltage = %d uV\n",
1863 __func__, i, info->soca->ocv_table[i]);
1866 for (i = 0; i < 3; i = i+1)
1867 info->soca->reset_soc[i] = 0;
1868 info->soca->reset_count = 0;
1870 if (info->first_pwon) {
1872 err = ricoh619_read(info->dev->parent, CHGISET_REG, &val);
1875 "Error in read CHGISET_REG%d\n", err);
1877 err = ricoh619_write(info->dev->parent, CHGISET_REG, 0);
1880 "Error in writing CHGISET_REG%d\n", err);
1883 if (!info->entry_factory_mode) {
1884 err = ricoh619_write(info->dev->parent,
1887 dev_err(info->dev, "Error in writing the control register\n");
1890 err = calc_capacity_in_period(info, &cc_cap, &is_charging, true);
1894 err = ricoh619_write(info->dev->parent, CHGISET_REG, val);
1897 "Error in writing CHGISET_REG%d\n", err);
1900 /* Rbat : Transfer */
1901 info->soca->Rbat = get_OCV_init_Data(info, 12) * 1000 / 512
1903 info->soca->n_cap = get_OCV_init_Data(info, 11);
1906 info->soca->displayed_soc = 0;
1907 info->soca->last_displayed_soc = 0;
1908 info->soca->suspend_soc = 0;
1909 info->soca->ready_fg = 0;
1910 info->soca->soc_delta = 0;
1911 info->soca->full_reset_count = 0;
1912 info->soca->soc_full = 0;
1913 info->soca->fc_cap = 0;
1914 info->soca->status = RICOH619_SOCA_START;
1915 /* stable count down 11->2, 1: reset; 0: Finished; */
1916 info->soca->stable_count = 11;
1917 info->soca->chg_cmp_times = 0;
1918 info->soca->dischg_state = 0;
1919 info->soca->Vbat_ave = 0;
1920 info->soca->Vbat_old = 0;
1921 info->soca->Vsys_ave = 0;
1922 info->soca->Ibat_ave = 0;
1923 info->soca->chg_count = 0;
1924 info->soca->target_use_cap = 0;
1925 info->soca->hurry_up_flg = 0;
1926 info->soca->re_cap_old = 0;
1927 info->soca->jt_limit = 0;
1928 info->soca->zero_flg = 0;
1929 info->soca->cc_cap_offset = 0;
1931 for (i = 0; i < 11; i++) {
1932 info->soca->ocv_table_low[i] = 0;
1935 for (i = 0; i < RICOH619_GET_CHARGE_NUM; i++) {
1936 info->soca->Vbat[i] = 0;
1937 info->soca->Vsys[i] = 0;
1938 info->soca->Ibat[i] = 0;
1943 #ifdef ENABLE_FG_KEEP_ON_MODE
1949 /* Start first Display job */
1950 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1951 RICOH619_FG_RESET_TIME*HZ);
1953 /* Start first Waiting stable job */
1954 queue_delayed_work(info->monitor_wqueue, &info->charge_stable_work,
1955 RICOH619_FG_STABLE_TIME*HZ/10);
1957 queue_delayed_work(info->monitor_wqueue, &info->charge_monitor_work,
1958 RICOH619_CHARGE_MONITOR_TIME * HZ);
1960 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1961 RICOH619_CHARGE_MONITOR_TIME * HZ);
1963 if (info->jt_hw_sw) {
1964 /* Enable JEITA function supported by H/W */
1965 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x04);
1967 dev_err(info->dev, "Error in writing the control register\n");
1969 /* Disable JEITA function supported by H/W */
1970 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x04);
1972 dev_err(info->dev, "Error in writing the control register\n");
1973 queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
1974 RICOH619_FG_RESET_TIME * HZ);
1977 /* Disable JEITA function supported by H/W */
1978 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x04);
1980 dev_err(info->dev, "Error in writing the control register\n");
1983 RICOH_FG_DBG("PMU: %s : * Rbat = %d mOhm n_cap = %d mAH\n",
1984 __func__, info->soca->Rbat, info->soca->n_cap);
1989 static void ricoh619_charging_complete_work(struct work_struct *work)
1991 struct ricoh619_battery_info *info = container_of(work,
1992 struct ricoh619_battery_info, charge_complete_ready.work);
1994 uint8_t time_ov_flag;
1995 RICOH_FG_DBG("PMU: %s\n", __func__);
1996 RICOH_FG_DBG("info->chg_complete_rd_cnt = %d\n", info->chg_complete_rd_cnt);
1997 RICOH_FG_DBG("info->chg_complete_rd_flag = %d\n", info->chg_complete_rd_flag);
1998 RICOH_FG_DBG("info->chg_complete_tm_ov_flag = %d\n", info->chg_complete_tm_ov_flag);
1999 RICOH_FG_DBG("time_ov_flag = %d\n", time_ov_flag);
2001 if(info->chg_complete_rd_flag == 1)
2003 // start chg 99per to 100per timer
2005 info->chg_complete_rd_flag = 2;
2006 info->chg_complete_tm_ov_flag = 0;
2010 if(info->capacity == 100)
2012 // battery arriver to 100% earlier than time ov
2014 info->chg_complete_rd_cnt = 0;
2015 info->chg_complete_tm_ov_flag = 1;
2017 else if(info->chg_complete_rd_cnt > RICOH619_TIME_CHG_COUNT)
2019 // chg timer ov before cap arrive to 100%
2021 info->chg_complete_tm_ov_flag = 1;
2022 info->chg_complete_rd_cnt = 0;
2023 info->soca->status = RICOH619_SOCA_FULL;
2024 power_supply_changed(&info->battery);
2029 info->chg_complete_tm_ov_flag = 0;
2033 if(time_ov_flag == 0)
2035 info->chg_complete_rd_cnt++;
2036 queue_delayed_work(info->monitor_wqueue, &info->charge_complete_ready,
2037 RICOH619_TIME_CHG_STEP);
2044 RICOH_FG_DBG("PMU2: %s return\n", __func__);
2045 RICOH_FG_DBG("info->chg_complete_rd_cnt = %d\n", info->chg_complete_rd_cnt);
2046 RICOH_FG_DBG("info->chg_complete_rd_flag = %d\n", info->chg_complete_rd_flag);
2047 RICOH_FG_DBG("info->chg_complete_tm_ov_flag = %d\n", info->chg_complete_tm_ov_flag);
2048 RICOH_FG_DBG("time_ov_flag = %d\n", time_ov_flag);
2051 static void ricoh619_changed_work(struct work_struct *work)
2053 struct ricoh619_battery_info *info = container_of(work,
2054 struct ricoh619_battery_info, changed_work.work);
2056 RICOH_FG_DBG("PMU: %s\n", __func__);
2057 power_supply_changed(&info->battery);
2062 static int check_jeita_status(struct ricoh619_battery_info *info, bool *is_jeita_updated)
2063 /* JEITA Parameter settings
2067 * jt_vfchg_h~+~~~~~~~~~~~~~~~~~~~+
2069 * jt_vfchg_l-| - - - - - - - - - +~~~~~~~~~~+
2071 * -------0--+-------------------+----------+--- Temp
2075 * jt_ichg_h-+ - -+~~~~~~~~~~~~~~+~~~~~~~~~~+
2077 * jt_ichg_l-+~~~~+ Charge area |
2079 * 0--+----+--------------+----------+--- Temp
2080 * 0 jt_temp_l jt_temp_h 55
2086 uint8_t chgiset_org;
2087 uint8_t batset2_org;
2088 uint8_t set_vchg_h, set_vchg_l;
2089 uint8_t set_ichg_h, set_ichg_l;
2091 *is_jeita_updated = false;
2093 /* No execute if JEITA disabled */
2094 if (!info->jt_en || info->jt_hw_sw)
2097 /* Check FG Reset */
2098 if (info->soca->ready_fg) {
2099 temp = get_battery_temp_2(info) / 10;
2101 RICOH_FG_DBG(KERN_INFO "JEITA: %s *** cannot update by resetting FG ******\n", __func__);
2106 err = ricoh619_read(info->dev->parent, BATSET2_REG, &batset2_org);
2108 dev_err(info->dev, "Error in readng the battery setting register\n");
2111 vfchg = (batset2_org & 0x70) >> 4;
2112 batset2_org &= 0x8F;
2115 err = ricoh619_read(info->dev->parent, CHGISET_REG, &chgiset_org);
2117 dev_err(info->dev, "Error in readng the chrage setting register\n");
2120 chgiset_org &= 0xC0;
2122 set_ichg_h = (uint8_t)(chgiset_org | info->jt_ichg_h);
2123 set_ichg_l = (uint8_t)(chgiset_org | info->jt_ichg_l);
2125 set_vchg_h = (uint8_t)((info->jt_vfchg_h << 4) | batset2_org);
2126 set_vchg_l = (uint8_t)((info->jt_vfchg_l << 4) | batset2_org);
2128 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Temperature: %d, vfchg: %d, SW status: %d, chg_status: %d ******\n",
2129 __func__, temp, vfchg, info->soca->status, info->soca->chg_status);
2131 if (temp <= 0 || 55 <= temp) {
2132 /* 1st and 5th temperature ranges (~0, 55~) */
2133 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Temp(%d) is out of 0-55 ******\n", __func__, temp);
2134 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2136 dev_err(info->dev, "Error in writing the control register\n");
2139 info->soca->jt_limit = 0;
2140 *is_jeita_updated = true;
2141 } else if (temp < info->jt_temp_l) {
2142 /* 2nd temperature range (0~12) */
2143 if (vfchg != info->jt_vfchg_h) {
2144 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 0<Temp<12, update to vfchg=%d ******\n",
2145 __func__, info->jt_vfchg_h);
2146 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2148 dev_err(info->dev, "Error in writing the control register\n");
2152 /* set VFCHG/VRCHG */
2153 err = ricoh619_write(info->dev->parent,
2154 BATSET2_REG, set_vchg_h);
2156 dev_err(info->dev, "Error in writing the battery setting register\n");
2159 info->soca->jt_limit = 0;
2160 *is_jeita_updated = true;
2162 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 0<Temp<50, already set vfchg=%d, so no need to update ******\n",
2163 __func__, info->jt_vfchg_h);
2166 err = ricoh619_write(info->dev->parent, CHGISET_REG, set_ichg_l);
2168 dev_err(info->dev, "Error in writing the battery setting register\n");
2171 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2173 dev_err(info->dev, "Error in writing the control register\n");
2176 } else if (temp < info->jt_temp_h) {
2177 /* 3rd temperature range (12~50) */
2178 if (vfchg != info->jt_vfchg_h) {
2179 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 12<Temp<50, update to vfchg==%d ******\n", __func__, info->jt_vfchg_h);
2181 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2183 dev_err(info->dev, "Error in writing the control register\n");
2186 /* set VFCHG/VRCHG */
2187 err = ricoh619_write(info->dev->parent,
2188 BATSET2_REG, set_vchg_h);
2190 dev_err(info->dev, "Error in writing the battery setting register\n");
2193 info->soca->jt_limit = 0;
2194 *is_jeita_updated = true;
2196 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 12<Temp<50, already set vfchg==%d, so no need to update ******\n",
2197 __func__, info->jt_vfchg_h);
2200 err = ricoh619_write(info->dev->parent, CHGISET_REG, set_ichg_h);
2202 dev_err(info->dev, "Error in writing the battery setting register\n");
2205 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2207 dev_err(info->dev, "Error in writing the control register\n");
2210 } else if (temp < 55) {
2211 /* 4th temperature range (50~55) */
2212 if (vfchg != info->jt_vfchg_l) {
2213 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 50<Temp<55, update to vfchg==%d ******\n", __func__, info->jt_vfchg_l);
2215 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2217 dev_err(info->dev, "Error in writing the control register\n");
2220 /* set VFCHG/VRCHG */
2221 err = ricoh619_write(info->dev->parent,
2222 BATSET2_REG, set_vchg_l);
2224 dev_err(info->dev, "Error in writing the battery setting register\n");
2227 info->soca->jt_limit = 1;
2228 *is_jeita_updated = true;
2230 RICOH_FG_DBG(KERN_INFO "JEITA: %s *** 50<Temp<55, already set vfchg==%d, so no need to update ******\n",
2231 __func__, info->jt_vfchg_l);
2234 err = ricoh619_write(info->dev->parent, CHGISET_REG, set_ichg_h);
2236 dev_err(info->dev, "Error in writing the battery setting register\n");
2239 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2241 dev_err(info->dev, "Error in writing the control register\n");
2246 get_power_supply_status(info);
2247 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Hope updating value in this timing after checking jeita, chg_status: %d, is_jeita_updated: %d ******\n",
2248 __func__, info->soca->chg_status, *is_jeita_updated);
2253 RICOH_FG_DBG(KERN_INFO "PMU: %s ERROR ******\n", __func__);
2257 static void ricoh619_jeita_work(struct work_struct *work)
2260 bool is_jeita_updated = false;
2261 struct ricoh619_battery_info *info = container_of(work,
2262 struct ricoh619_battery_info, jeita_work.work);
2264 mutex_lock(&info->lock);
2266 ret = check_jeita_status(info, &is_jeita_updated);
2268 queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
2269 RICOH619_JEITA_UPDATE_TIME * HZ);
2271 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Call check_jeita_status() in jeita_work, err:%d ******\n",
2273 queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
2274 RICOH619_FG_RESET_TIME * HZ);
2277 mutex_unlock(&info->lock);
2279 if(true == is_jeita_updated)
2280 power_supply_changed(&info->battery);
2285 #ifdef ENABLE_FACTORY_MODE
2286 /*------------------------------------------------------*/
2288 /* Check Battery exist or not */
2289 /* If not, disabled Rapid to Complete State change */
2290 /*------------------------------------------------------*/
2291 static int ricoh619_factory_mode(struct ricoh619_battery_info *info)
2296 ret = ricoh619_read(info->dev->parent, RICOH619_INT_MON_CHGCTR, &val);
2298 dev_err(info->dev, "Error in reading the control register\n");
2301 if (!(val & 0x01)) /* No Adapter connected */
2304 /* Rapid to Complete State change disable */
2305 ret = ricoh619_set_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x40);
2307 dev_err(info->dev, "Error in writing the control register\n");
2311 /* Wait 1s for checking Charging State */
2312 queue_delayed_work(info->factory_mode_wqueue, &info->factory_mode_work,
2318 static void check_charging_state_work(struct work_struct *work)
2320 struct ricoh619_battery_info *info = container_of(work,
2321 struct ricoh619_battery_info, factory_mode_work.work);
2325 int chargeCurrent = 0;
2327 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &val);
2329 dev_err(info->dev, "Error in reading the control register\n");
2334 chargeCurrent = get_check_fuel_gauge_reg(info, CC_AVERAGE1_REG,
2335 CC_AVERAGE0_REG, 0x3fff);
2336 if (chargeCurrent < 0) {
2337 dev_err(info->dev, "Error in reading the FG register\n");
2341 /* Repid State && Charge Current about 0mA */
2342 if (((chargeCurrent >= 0x3ffc && chargeCurrent <= 0x3fff)
2343 || chargeCurrent < 0x05) && val == 0x43) {
2344 RICOH_FG_DBG("PMU:%s --- No battery !! Enter Factory mode ---\n"
2346 info->entry_factory_mode = true;
2347 /* clear FG_ACC bit */
2348 ret = ricoh619_clr_bits(info->dev->parent, RICOH619_FG_CTRL, 0x10);
2350 dev_err(info->dev, "Error in writing FG_CTRL\n");
2352 return; /* Factory Mode */
2355 /* Return Normal Mode --> Rapid to Complete State change enable */
2356 ret = ricoh619_clr_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x40);
2358 dev_err(info->dev, "Error in writing the control register\n");
2361 RICOH_FG_DBG("PMU:%s --- Battery exist !! Return Normal mode ---0x%2x\n"
2366 #endif /* ENABLE_FACTORY_MODE */
2368 static int Calc_Linear_Interpolation(int x0, int y0, int x1, int y1, int y)
2373 alpha = (y - y0)*100 / (y1 - y0);
2375 x = ((100 - alpha) * x0 + alpha * x1) / 100;
2380 static void ricoh619_scaling_OCV_table(struct ricoh619_battery_info *info, int cutoff_vol, int full_vol, int *start_per, int *end_per)
2385 int OCV_percent_new[11];
2387 /* get ocv table. this table is calculated by Apprication */
2388 //RICOH_FG_DBG("PMU : %s : original table\n",__func__);
2389 for (i = 0; i <= 10; i = i+1) {
2390 RICOH_FG_DBG(KERN_INFO "PMU: %s : %d0%% voltage = %d uV\n",
2391 __func__, i, info->soca->ocv_table_def[i]);
2393 //RICOH_FG_DBG("PMU: %s : cutoff_vol %d full_vol %d\n",
2394 // __func__, cutoff_vol,full_vol);
2397 if (info->soca->ocv_table_def[0] > cutoff_vol * 1000) {
2399 RICOH_FG_DBG("PMU : %s : setting value of cuttoff_vol(%d) is out of range(%d) \n",__func__, cutoff_vol, info->soca->ocv_table_def[0]);
2401 for (i = 1; i < 11; i++) {
2402 if (info->soca->ocv_table_def[i] >= cutoff_vol * 1000) {
2403 /* unit is 0.001% */
2404 *start_per = Calc_Linear_Interpolation(
2405 (i-1)*1000, info->soca->ocv_table_def[i-1], i*1000,
2406 info->soca->ocv_table_def[i], (cutoff_vol * 1000));
2413 for (i = 1; i < 11; i++) {
2414 if (info->soca->ocv_table_def[i] >= full_vol * 1000) {
2415 /* unit is 0.001% */
2416 *end_per = Calc_Linear_Interpolation(
2417 (i-1)*1000, info->soca->ocv_table_def[i-1], i*1000,
2418 info->soca->ocv_table_def[i], (full_vol * 1000));
2423 /* calc new ocv percent */
2424 percent_step = ( *end_per - *start_per) / 10;
2425 //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);
2427 for (i = 0; i < 11; i++) {
2429 = *start_per + percent_step*(i - 0);
2432 /* calc new ocv voltage */
2433 for (i = 0; i < 11; i++) {
2434 for (j = 1; j < 11; j++) {
2435 if (1000*j >= OCV_percent_new[i]) {
2436 temp = Calc_Linear_Interpolation(
2437 info->soca->ocv_table_def[j-1], (j-1)*1000,
2438 info->soca->ocv_table_def[j] , j*1000,
2439 OCV_percent_new[i]);
2441 temp = ( (temp/1000) * 4095 ) / 5000;
2443 battery_init_para[info->num][i*2 + 1] = temp;
2444 battery_init_para[info->num][i*2] = temp >> 8;
2450 RICOH_FG_DBG("PMU : %s : new table\n",__func__);
2451 for (i = 0; i <= 10; i = i+1) {
2452 temp = (battery_init_para[info->num][i*2]<<8)
2453 | (battery_init_para[info->num][i*2+1]);
2454 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
2455 temp = ((temp * 50000 * 10 / 4095) + 5) / 10;
2456 RICOH_FG_DBG("PMU : %s : ocv_table %d is %d v\n",__func__, i, temp);
2461 static int ricoh619_set_OCV_table(struct ricoh619_battery_info *info)
2467 int available_cap_ori;
2480 for (i = 0; i <= 10; i = i+1) {
2481 info->soca->ocv_table_def[i] = get_OCV_voltage(info, i);
2482 RICOH_FG_DBG(KERN_INFO "PMU: %s : %d0%% voltage = %d uV\n",
2483 __func__, i, info->soca->ocv_table_def[i]);
2486 temp = (battery_init_para[info->num][24]<<8) | (battery_init_para[info->num][25]);
2487 Rbat = temp * 1000 / 512 * 5000 / 4095;
2488 info->soca->Rsys = Rbat + 55;
2490 if ((info->fg_target_ibat == 0) || (info->fg_target_vsys == 0)) { /* normal version */
2492 temp = (battery_init_para[info->num][22]<<8) | (battery_init_para[info->num][23]);
2493 //fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
2496 info->soca->target_ibat = temp*2/10; /* calc 0.2C*/
2497 temp1 = (battery_init_para[info->num][0]<<8) | (battery_init_para[info->num][1]);
2499 info->soca->target_vsys = temp1 + ( info->soca->target_ibat * info->soca->Rsys ) / 1000;
2503 info->soca->target_ibat = info->fg_target_ibat;
2504 /* calc min vsys value */
2505 temp1 = (battery_init_para[info->num][0]<<8) | (battery_init_para[info->num][1]);
2506 temp = temp1 + ( info->soca->target_ibat * info->soca->Rsys ) / 1000;
2507 if( temp < info->fg_target_vsys) {
2508 info->soca->target_vsys = info->fg_target_vsys;
2510 info->soca->target_vsys = temp;
2511 RICOH_FG_DBG("PMU : %s : setting value of target vsys(%d) is out of range(%d)\n",__func__, info->fg_target_vsys, temp);
2516 RICOH_FG_DBG("PMU : %s : target_vsys is %d target_ibat is %d\n",__func__,info->soca->target_vsys,info->soca->target_ibat);
2518 if ((info->soca->target_ibat == 0) || (info->soca->target_vsys == 0)) { /* normal version */
2519 } else { /*Slice cutoff voltage version. */
2521 Ibat_min = -1 * info->soca->target_ibat;
2522 info->soca->cutoff_ocv = info->soca->target_vsys - Ibat_min * info->soca->Rsys / 1000;
2524 full_ocv = (battery_init_para[info->num][20]<<8) | (battery_init_para[info->num][21]);
2525 full_ocv = full_ocv * 5000 / 4095;
2527 ricoh619_scaling_OCV_table(info, info->soca->cutoff_ocv, full_ocv, &start_per, &end_per);
2529 /* calc available capacity */
2530 /* get avilable capacity */
2531 /* battery_init_para23-24 is designe capacity */
2532 available_cap = (battery_init_para[info->num][22]<<8)
2533 | (battery_init_para[info->num][23]);
2535 available_cap = available_cap
2536 * ((10000 - start_per) / 100) / 100 ;
2539 battery_init_para[info->num][23] = available_cap;
2540 battery_init_para[info->num][22] = available_cap >> 8;
2543 ret = ricoh619_clr_bits(info->dev->parent, FG_CTRL_REG, 0x01);
2545 dev_err(info->dev, "error in FG_En off\n");
2548 /////////////////////////////////
2549 ret = ricoh619_read_bank1(info->dev->parent, 0xDC, &val);
2551 dev_err(info->dev, "batterry initialize error\n");
2556 val &= 0x0F; //clear bit 4-7
2557 val |= 0x10; //set bit 4
2559 ret = ricoh619_write_bank1(info->dev->parent, 0xDC, val);
2561 dev_err(info->dev, "batterry initialize error\n");
2565 ret = ricoh619_read_bank1(info->dev->parent, 0xDC, &val2);
2567 dev_err(info->dev, "batterry initialize error\n");
2571 ret = ricoh619_write_bank1(info->dev->parent, 0xDC, val_temp);
2573 dev_err(info->dev, "batterry initialize error\n");
2577 RICOH_FG_DBG("PMU : %s : original 0x%x, before 0x%x, after 0x%x\n",__func__, val_temp, val, val2);
2580 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
2581 BAT_INIT_TOP_REG, 30, battery_init_para[info->num]);
2583 dev_err(info->dev, "batterry initialize error\n");
2587 ret = ricoh619_read_bank1(info->dev->parent, 0xD2, &val);
2589 dev_err(info->dev, "batterry initialize error\n");
2593 ret = ricoh619_read_bank1(info->dev->parent, 0xD3, &val2);
2595 dev_err(info->dev, "batterry initialize error\n");
2599 available_cap_ori = val2 + (val << 8);
2600 available_cap = battery_init_para[info->num][23]
2601 + (battery_init_para[info->num][22] << 8);
2603 if (available_cap_ori == available_cap) {
2604 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
2605 BAT_INIT_TOP_REG, 22, battery_init_para[info->num]);
2607 dev_err(info->dev, "batterry initialize error\n");
2611 for (i = 0; i < 6; i++) {
2612 ret = ricoh619_write_bank1(info->dev->parent, 0xD4+i, battery_init_para[info->num][24+i]);
2614 dev_err(info->dev, "batterry initialize error\n");
2619 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
2620 BAT_INIT_TOP_REG, 30, battery_init_para[info->num]);
2622 dev_err(info->dev, "batterry initialize error\n");
2628 ////////////////////////////////
2635 /* Initial setting of battery */
2636 static int ricoh619_init_battery(struct ricoh619_battery_info *info)
2641 /* Need to implement initial setting of batery and error */
2642 /* -------------------------- */
2643 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
2645 /* set relaxation state */
2646 if (RICOH619_REL1_SEL_VALUE > 240)
2649 val = RICOH619_REL1_SEL_VALUE / 16 ;
2651 /* set relaxation state */
2652 if (RICOH619_REL2_SEL_VALUE > 120)
2655 val2 = RICOH619_REL2_SEL_VALUE / 8 ;
2657 val = val + (val2 << 4);
2659 ret = ricoh619_write_bank1(info->dev->parent, BAT_REL_SEL_REG, val);
2661 dev_err(info->dev, "Error in writing BAT_REL_SEL_REG\n");
2665 ret = ricoh619_read_bank1(info->dev->parent, BAT_REL_SEL_REG, &val);
2666 RICOH_FG_DBG("PMU: ------- BAT_REL_SEL= %xh: =======\n",
2669 ret = ricoh619_write_bank1(info->dev->parent, BAT_TA_SEL_REG, 0x00);
2671 dev_err(info->dev, "Error in writing BAT_TA_SEL_REG\n");
2675 // ret = ricoh619_read(info->dev->parent, FG_CTRL_REG, &val);
2677 // dev_err(info->dev, "Error in reading the control register\n");
2681 // val = (val & 0x10) >> 4;
2682 // info->first_pwon = (val == 0) ? 1 : 0;
2683 ret = ricoh619_read(info->dev->parent, PSWR_REG, &val);
2685 dev_err(info->dev,"Error in reading PSWR_REG %d\n", ret);
2688 info->first_pwon = (val == 0) ? 1 : 0;
2691 ret = ricoh619_set_OCV_table(info);
2693 dev_err(info->dev, "Error in writing the OCV Tabler\n");
2697 ret = ricoh619_write(info->dev->parent, FG_CTRL_REG, 0x11);
2699 dev_err(info->dev, "Error in writing the control register\n");
2705 ret = ricoh619_write(info->dev->parent, VINDAC_REG, 0x03);
2707 dev_err(info->dev, "Error in writing the control register\n");
2711 if (info->alarm_vol_mv < 2700 || info->alarm_vol_mv > 3400) {
2712 dev_err(info->dev, "alarm_vol_mv is out of range!\n");
2719 /* Initial setting of charger */
2720 static int ricoh619_init_charger(struct ricoh619_battery_info *info)
2733 info->chg_stat1 = 0;
2735 err = ricoh619_set_bits(info->dev->parent, RICOH619_PWR_FUNC, 0x20);
2737 dev_err(info->dev, "Error in writing the PWR FUNC register\n");
2741 charge_status = get_power_supply_status(info);
2743 if (charge_status != POWER_SUPPLY_STATUS_FULL)
2745 /* Disable charging */
2746 err = ricoh619_clr_bits(info->dev->parent,CHGCTL1_REG, 0x03);
2748 dev_err(info->dev, "Error in writing the control register\n");
2754 err = ricoh619_read(info->dev->parent, REGISET1_REG,&val);
2755 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);
2757 /* REGISET1:(0xB6) setting */
2758 if ((info->ch_ilim_adp != 0xFF) || (info->ch_ilim_adp <= 0x1D)) {
2759 val = info->ch_ilim_adp;
2761 err = ricoh619_write(info->dev->parent, REGISET1_REG,val);
2763 dev_err(info->dev, "Error in writing REGISET1_REG %d\n",
2770 err = ricoh619_read(info->dev->parent, REGISET1_REG,&val);
2771 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);
2774 err = ricoh619_read(info->dev->parent, REGISET2_REG,&val);
2775 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);
2777 /* REGISET2:(0xB7) setting */
2778 err = ricoh619_read(info->dev->parent, REGISET2_REG, &val);
2781 "Error in read REGISET2_REG %d\n", err);
2785 if ((info->ch_ilim_usb != 0xFF) || (info->ch_ilim_usb <= 0x1D)) {
2786 val2 = info->ch_ilim_usb;
2787 } else {/* Keep OTP value */
2788 val2 = (val & 0x1F);
2796 err = ricoh619_write(info->dev->parent, REGISET2_REG,val);
2798 dev_err(info->dev, "Error in writing REGISET2_REG %d\n",
2804 err = ricoh619_read(info->dev->parent, REGISET2_REG,&val);
2805 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);
2807 /* CHGISET_REG(0xB8) setting */
2809 err = ricoh619_read(info->dev->parent, CHGISET_REG,&val);
2810 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);
2812 err = ricoh619_read(info->dev->parent, CHGISET_REG, &val);
2815 "Error in read CHGISET_REG %d\n", err);
2819 /* Define Current settings value for charging (bit 4~0)*/
2820 if ((info->ch_ichg != 0xFF) || (info->ch_ichg <= 0x1D)) {
2821 val2 = info->ch_ichg;
2822 } else { /* Keep OTP value */
2823 val2 = (val & 0x1F);
2826 /* Define Current settings at the charge completion (bit 7~6)*/
2827 if ((info->ch_icchg != 0xFF) || (info->ch_icchg <= 0x03)) {
2828 val3 = info->ch_icchg << 6;
2829 } else { /* Keep OTP value */
2830 val3 = (val & 0xC0);
2835 err = ricoh619_write(info->dev->parent, CHGISET_REG, val);
2837 dev_err(info->dev, "Error in writing CHGISET_REG %d\n",
2843 err = ricoh619_read(info->dev->parent, CHGISET_REG,&val);
2844 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);
2847 err = ricoh619_read(info->dev->parent, BATSET1_REG,&val);
2848 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);
2850 /* BATSET1_REG(0xBA) setting */
2851 err = ricoh619_read(info->dev->parent, BATSET1_REG, &val);
2854 "Error in read BATSET1 register %d\n", err);
2858 /* Define Battery overvoltage (bit 4)*/
2859 if ((info->ch_vbatovset != 0xFF) || (info->ch_vbatovset <= 0x1)) {
2860 val2 = info->ch_vbatovset;
2862 } else { /* Keep OTP value */
2863 val2 = (val & 0x10);
2866 /* keep bit 0-3 and bit 5-7 */
2871 err = ricoh619_write(info->dev->parent, BATSET1_REG, val);
2873 dev_err(info->dev, "Error in writing BAT1_REG %d\n",
2878 err = ricoh619_read(info->dev->parent, BATSET1_REG,&val);
2879 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);
2882 err = ricoh619_read(info->dev->parent, BATSET2_REG,&val);
2883 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);
2886 /* BATSET2_REG(0xBB) setting */
2887 err = ricoh619_read(info->dev->parent, BATSET2_REG, &val);
2890 "Error in read BATSET2 register %d\n", err);
2894 /* Define Re-charging voltage (bit 2~0)*/
2895 if ((info->ch_vrchg != 0xFF) || (info->ch_vrchg <= 0x04)) {
2896 val2 = info->ch_vrchg;
2897 } else { /* Keep OTP value */
2898 val2 = (val & 0x07);
2901 /* Define FULL charging voltage (bit 6~4)*/
2902 if ((info->ch_vfchg != 0xFF) || (info->ch_vfchg <= 0x04)) {
2903 val3 = info->ch_vfchg;
2905 } else { /* Keep OTP value */
2906 val3 = (val & 0x70);
2909 /* keep bit 3 and bit 7 */
2912 val = val + val2 + val3;
2914 err = ricoh619_write(info->dev->parent, BATSET2_REG, val);
2916 dev_err(info->dev, "Error in writing RICOH619_RE_CHARGE_VOLTAGE %d\n",
2922 err = ricoh619_read(info->dev->parent, BATSET2_REG,&val);
2923 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);
2925 /* Set rising edge setting ([1:0]=01b)for INT in charging */
2926 /* and rising edge setting ([3:2]=01b)for charge completion */
2927 err = ricoh619_read(info->dev->parent, RICOH619_CHG_STAT_DETMOD1, &val);
2929 dev_err(info->dev, "Error in reading CHG_STAT_DETMOD1 %d\n",
2935 err = ricoh619_write(info->dev->parent, RICOH619_CHG_STAT_DETMOD1, val);
2937 dev_err(info->dev, "Error in writing CHG_STAT_DETMOD1 %d\n",
2942 /* Unmask In charging/charge completion */
2943 err = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGSTS1, 0xfc);
2945 dev_err(info->dev, "Error in writing INT_MSK_CHGSTS1 %d\n",
2950 /* Set both edge for VUSB([3:2]=11b)/VADP([1:0]=11b) detect */
2951 err = ricoh619_read(info->dev->parent, RICOH619_CHG_CTRL_DETMOD1, &val);
2953 dev_err(info->dev, "Error in reading CHG_CTRL_DETMOD1 %d\n",
2959 err = ricoh619_write(info->dev->parent, RICOH619_CHG_CTRL_DETMOD1, val);
2961 dev_err(info->dev, "Error in writing CHG_CTRL_DETMOD1 %d\n",
2966 /* Unmask In VUSB/VADP completion */
2967 err = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGCTR, 0xfc);
2969 dev_err(info->dev, "Error in writing INT_MSK_CHGSTS1 %d\n",
2974 if (charge_status != POWER_SUPPLY_STATUS_FULL)
2976 /* Enable charging */
2977 err = ricoh619_set_bits(info->dev->parent,CHGCTL1_REG, 0x03);
2979 dev_err(info->dev, "Error in writing the control register\n");
2983 /* get OCV100_min, OCV100_min*/
2984 temp = (battery_init_para[info->num][24]<<8) | (battery_init_para[info->num][25]);
2985 rbat = temp * 1000 / 512 * 5000 / 4095;
2987 /* get vfchg value */
2988 err = ricoh619_read(info->dev->parent, BATSET2_REG, &val);
2990 dev_err(info->dev, "Error in reading the batset2reg\n");
2996 vfchg_val = 4050 + val2 * 50;
3000 RICOH_FG_DBG("PMU : %s : test test val %d, val2 %d vfchg %d\n", __func__, val, val2, vfchg_val);
3003 err = ricoh619_read(info->dev->parent, CHGISET_REG, &val);
3005 dev_err(info->dev, "Error in reading the chgisetreg\n");
3010 icchg_val = 50 + val2 * 50;
3011 RICOH_FG_DBG("PMU : %s : test test val %d, val2 %d icchg %d\n", __func__, val, val2, icchg_val);
3013 info->soca->OCV100_min = ( vfchg_val * 99 / 100 - (icchg_val * (rbat +20))/1000 - 20 ) * 1000;
3014 info->soca->OCV100_max = ( vfchg_val * 101 / 100 - (icchg_val * (rbat +20))/1000 + 20 ) * 1000;
3016 RICOH_FG_DBG("PMU : %s : 100 min %d, 100 max %d vfchg %d icchg %d rbat %d\n",__func__,
3017 info->soca->OCV100_min,info->soca->OCV100_max,vfchg_val,icchg_val,rbat);
3019 #ifdef ENABLE_LOW_BATTERY_DETECTION
3020 /* Set ADRQ=00 to stop ADC */
3021 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT3, 0x0);
3022 /* Enable VSYS threshold Low interrupt */
3023 ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
3024 /* Set ADC auto conversion interval 250ms */
3025 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT2, 0x0);
3026 /* Enable VSYS pin conversion in auto-ADC */
3027 // ricoh619_write(info->dev->parent, RICOH619_ADC_CNT1, 0x10);
3028 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT1, 0x16);
3029 /* Set VSYS threshold low voltage value = (voltage(V)*255)/(3*2.5) */
3030 val = info->alarm_vol_mv * 255 / 7500;
3031 ricoh619_write(info->dev->parent, RICOH619_ADC_VSYS_THL, val);
3032 /* Start auto-mode & average 4-time conversion mode for ADC */
3033 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT3, 0x28);
3034 /* Enable master ADC INT */
3035 ricoh619_set_bits(info->dev->parent, RICOH619_INTC_INTEN, ADC_INT);
3043 static int get_power_supply_status(struct ricoh619_battery_info *info)
3046 uint8_t supply_state;
3047 uint8_t charge_state;
3050 /* get power supply status */
3051 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
3053 dev_err(info->dev, "Error in reading the control register\n");
3057 charge_state = (status & 0x1F);
3058 supply_state = ((status & 0xC0) >> 6);
3060 if (info->entry_factory_mode)
3061 return POWER_SUPPLY_STATUS_NOT_CHARGING;
3063 if (supply_state == SUPPLY_STATE_BAT) {
3064 info->soca->chg_status = POWER_SUPPLY_STATUS_DISCHARGING;
3066 switch (charge_state) {
3067 case CHG_STATE_CHG_OFF:
3068 info->soca->chg_status
3069 = POWER_SUPPLY_STATUS_DISCHARGING;
3071 case CHG_STATE_CHG_READY_VADP:
3072 info->soca->chg_status
3073 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3075 case CHG_STATE_CHG_TRICKLE:
3076 info->soca->chg_status
3077 = POWER_SUPPLY_STATUS_CHARGING;
3079 case CHG_STATE_CHG_RAPID:
3080 info->soca->chg_status
3081 = POWER_SUPPLY_STATUS_CHARGING;
3083 case CHG_STATE_CHG_COMPLETE:
3084 info->soca->chg_status
3085 = POWER_SUPPLY_STATUS_FULL;
3087 case CHG_STATE_SUSPEND:
3088 info->soca->chg_status
3089 = POWER_SUPPLY_STATUS_DISCHARGING;
3091 case CHG_STATE_VCHG_OVER_VOL:
3092 info->soca->chg_status
3093 = POWER_SUPPLY_STATUS_DISCHARGING;
3095 case CHG_STATE_BAT_ERROR:
3096 info->soca->chg_status
3097 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3099 case CHG_STATE_NO_BAT:
3100 info->soca->chg_status
3101 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3103 case CHG_STATE_BAT_OVER_VOL:
3104 info->soca->chg_status
3105 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3107 case CHG_STATE_BAT_TEMP_ERR:
3108 info->soca->chg_status
3109 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3111 case CHG_STATE_DIE_ERR:
3112 info->soca->chg_status
3113 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3115 case CHG_STATE_DIE_SHUTDOWN:
3116 info->soca->chg_status
3117 = POWER_SUPPLY_STATUS_DISCHARGING;
3119 case CHG_STATE_NO_BAT2:
3120 info->soca->chg_status
3121 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3123 case CHG_STATE_CHG_READY_VUSB:
3124 info->soca->chg_status
3125 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3128 info->soca->chg_status
3129 = POWER_SUPPLY_STATUS_UNKNOWN;
3134 return info->soca->chg_status;
3137 static int get_power_supply_Android_status(struct ricoh619_battery_info *info)
3140 get_power_supply_status(info);
3142 /* get power supply status */
3143 if (info->entry_factory_mode)
3144 return POWER_SUPPLY_STATUS_NOT_CHARGING;
3146 switch (info->soca->chg_status) {
3147 case POWER_SUPPLY_STATUS_UNKNOWN:
3148 return POWER_SUPPLY_STATUS_UNKNOWN;
3151 case POWER_SUPPLY_STATUS_NOT_CHARGING:
3152 return POWER_SUPPLY_STATUS_NOT_CHARGING;
3155 case POWER_SUPPLY_STATUS_DISCHARGING:
3156 return POWER_SUPPLY_STATUS_DISCHARGING;
3159 case POWER_SUPPLY_STATUS_CHARGING:
3160 return POWER_SUPPLY_STATUS_CHARGING;
3163 case POWER_SUPPLY_STATUS_FULL:
3164 if(info->soca->displayed_soc == 100 * 100) {
3165 return POWER_SUPPLY_STATUS_FULL;
3167 return POWER_SUPPLY_STATUS_CHARGING;
3171 return POWER_SUPPLY_STATUS_UNKNOWN;
3175 return POWER_SUPPLY_STATUS_UNKNOWN;
3177 extern struct ricoh619 *g_ricoh619;
3178 static void charger_irq_work(struct work_struct *work)
3180 struct ricoh619_battery_info *info
3181 = container_of(work, struct ricoh619_battery_info, irq_work);
3184 RICOH_FG_DBG("PMU:%s In\n", __func__);
3186 power_supply_changed(&info->battery);
3187 power_supply_changed(&powerac);
3188 power_supply_changed(&powerusb);
3190 // mutex_lock(&info->lock);
3192 if (info->chg_stat1 & 0x01) {
3193 ricoh619_read(info->dev->parent, CHGSTATE_REG, ®_val);
3194 if (reg_val & 0x40) { /* USE ADP */
3195 #ifdef SUPPORT_USB_CONNECT_TO_ADP
3197 RICOH_FG_DBG("PMU:%s usb det dwc_otg_check_dpdm =%d\n", __func__,dwc_otg_check_dpdm(0));
3198 if(2 == dwc_otg_check_dpdm(0)){
3199 /* set adp limit current 2A */
3200 ricoh619_write(info->dev->parent, REGISET1_REG, 0x16);
3201 /* set charge current 2A */
3202 ricoh619_write(info->dev->parent, CHGISET_REG, 0xD3);
3205 /* set adp limit current 500ma */
3206 ricoh619_write(info->dev->parent, REGISET1_REG, 0x04);
3207 /* set charge current 500ma */
3208 ricoh619_write(info->dev->parent, CHGISET_REG, 0xc4);
3211 power_supply_changed(&info->battery);
3212 power_supply_changed(&powerac);
3213 power_supply_changed(&powerusb);
3216 #else //support adp and usb chag
3217 if (gpio_is_valid(g_ricoh619->dc_det)){
3218 ret = gpio_request(g_ricoh619->dc_det, "ricoh619_dc_det");
3220 RICOH_FG_DBG("Failed to request gpio %d with ret:""%d\n",g_ricoh619->dc_det, ret);
3222 gpio_direction_input(g_ricoh619->dc_det);
3223 ret = gpio_get_value(g_ricoh619->dc_det);
3226 /* set adp limit current 2A */
3227 ricoh619_write(info->dev->parent, REGISET1_REG, 0x16);
3228 /* set charge current 2A */
3229 ricoh619_write(info->dev->parent, CHGISET_REG, 0xD3);
3232 /* set adp limit current 500ma */
3233 ricoh619_write(info->dev->parent, REGISET1_REG, 0x04);
3234 /* set charge current 500ma */
3235 ricoh619_write(info->dev->parent, CHGISET_REG, 0xc4);
3237 gpio_free(g_ricoh619->dc_det);
3240 /* set adp limit current 2A */
3241 ricoh619_write(info->dev->parent, REGISET1_REG, 0x16);
3242 /* set charge current 2A */
3243 ricoh619_write(info->dev->parent, CHGISET_REG, 0xD3);
3247 else if (reg_val & 0x80) { /* USE USB */
3248 queue_work(info->usb_workqueue, &info->usb_irq_work);
3252 info->chg_stat1 = 0;
3254 /* Enable Interrupt for VADP/USB */
3255 ret = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGCTR, 0xfc);
3258 "%s(): Error in enable charger mask INT %d\n",
3261 /* Enable Interrupt for Charging & complete */
3262 ret = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGSTS1, 0xfc);
3265 "%s(): Error in enable charger mask INT %d\n",
3268 // mutex_unlock(&info->lock);
3269 RICOH_FG_DBG("PMU:%s Out\n", __func__);
3272 #ifdef ENABLE_LOW_BATTERY_DETECTION
3273 static void low_battery_irq_work(struct work_struct *work)
3275 struct ricoh619_battery_info *info = container_of(work,
3276 struct ricoh619_battery_info, low_battery_work.work);
3280 RICOH_FG_DBG("PMU:%s In\n", __func__);
3282 power_supply_changed(&info->battery);
3284 /* Enable VADP threshold Low interrupt */
3285 ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
3288 "%s(): Error in enable adc mask INT %d\n",
3294 static void ricoh619_usb_charge_det(void)
3296 struct ricoh619 *ricoh619 = g_ricoh619;
3297 ricoh619_set_bits(ricoh619->dev,REGISET2_REG,(1 << 7)); //set usb limit current when SDP or other mode
3298 RICOH_FG_DBG("PMU:%s usb det dwc_otg_check_dpdm =%d\n", __func__,dwc_otg_check_dpdm(0));
3299 if(2 == dwc_otg_check_dpdm(0)){
3300 ricoh619_write(ricoh619->dev,REGISET2_REG,0x16); //set usb limit current 2A
3301 ricoh619_write(ricoh619->dev,CHGISET_REG,0xD3); //set charge current 2A
3304 ricoh619_write(ricoh619->dev,REGISET2_REG,0x04); //set usb limit current 500ma
3305 ricoh619_write(ricoh619->dev,CHGISET_REG,0xC4); //set charge current 500ma
3307 power_supply_changed(&powerac);
3308 power_supply_changed(&powerusb);
3311 static void usb_det_irq_work(struct work_struct *work)
3313 struct ricoh619_battery_info *info = container_of(work,
3314 struct ricoh619_battery_info, usb_irq_work);
3318 RICOH_FG_DBG("PMU:%s In\n", __func__);
3320 power_supply_changed(&info->battery);
3321 power_supply_changed(&powerac);
3322 power_supply_changed(&powerusb);
3324 mutex_lock(&info->lock);
3326 /* Enable Interrupt for VUSB */
3327 ret = ricoh619_clr_bits(info->dev->parent,
3328 RICOH619_INT_MSK_CHGCTR, 0x02);
3331 "%s(): Error in enable charger mask INT %d\n",
3334 mutex_unlock(&info->lock);
3335 ret = ricoh619_read(info->dev->parent, RICOH619_INT_MON_CHGCTR, &sts);
3337 dev_err(info->dev, "Error in reading the control register\n");
3342 ricoh619_usb_charge_det();
3346 /*********************/
3348 /*********************/
3351 RICOH_FG_DBG("PMU:%s Out\n", __func__);
3354 extern void rk_send_wakeup_key(void);
3355 static irqreturn_t charger_in_isr(int irq, void *battery_info)
3357 struct ricoh619_battery_info *info = battery_info;
3358 RICOH_FG_DBG("PMU:%s\n", __func__);
3360 info->chg_stat1 |= 0x01;
3362 queue_work(info->workqueue, &info->irq_work);
3363 // rk_send_wakeup_key();
3368 static irqreturn_t charger_complete_isr(int irq, void *battery_info)
3370 struct ricoh619_battery_info *info = battery_info;
3371 RICOH_FG_DBG("PMU:%s\n", __func__);
3373 info->chg_stat1 |= 0x02;
3374 queue_work(info->workqueue, &info->irq_work);
3375 // rk_send_wakeup_key();
3380 static irqreturn_t charger_usb_isr(int irq, void *battery_info)
3382 struct ricoh619_battery_info *info = battery_info;
3383 RICOH_FG_DBG("PMU:%s\n", __func__);
3385 info->chg_ctr |= 0x02;
3387 queue_work(info->workqueue, &info->irq_work);
3389 info->soca->dischg_state = 0;
3390 info->soca->chg_count = 0;
3392 // queue_work(info->usb_workqueue, &info->usb_irq_work);
3393 rk_send_wakeup_key();
3395 if (RICOH619_SOCA_UNSTABLE == info->soca->status
3396 || RICOH619_SOCA_FG_RESET == info->soca->status)
3397 info->soca->stable_count = 11;
3402 static irqreturn_t charger_adp_isr(int irq, void *battery_info)
3404 struct ricoh619_battery_info *info = battery_info;
3405 RICOH_FG_DBG("PMU:%s\n", __func__);
3407 info->chg_ctr |= 0x01;
3408 queue_work(info->workqueue, &info->irq_work);
3409 rk_send_wakeup_key();
3411 info->soca->dischg_state = 0;
3412 info->soca->chg_count = 0;
3413 if (RICOH619_SOCA_UNSTABLE == info->soca->status
3414 || RICOH619_SOCA_FG_RESET == info->soca->status)
3415 info->soca->stable_count = 11;
3421 #ifdef ENABLE_LOW_BATTERY_DETECTION
3422 /*************************************************************/
3423 /* for Detecting Low Battery */
3424 /*************************************************************/
3426 static irqreturn_t adc_vsysl_isr(int irq, void *battery_info)
3429 struct ricoh619_battery_info *info = battery_info;
3432 RICOH_FG_DBG("PMU:%s\n", __func__);
3434 queue_delayed_work(info->monitor_wqueue, &info->low_battery_work,
3435 LOW_BATTERY_DETECTION_TIME*HZ);
3439 RICOH_FG_DBG("PMU:%s\n", __func__);
3440 // ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
3441 rk_send_wakeup_key();
3446 #ifdef RICOH619_VADP_DROP_WORK
3447 static void vadp_drop_irq_work(struct work_struct *work)
3449 struct ricoh619_battery_info *info = container_of(work,
3450 struct ricoh619_battery_info, vadp_drop_work.work);
3456 RICOH_FG_DBG("PMU vadp_drop_work:%s In\n", __func__);
3457 mutex_lock(&info->lock);
3458 ret = ricoh619_read(info->dev->parent, 0x6a, &data[0]);
3459 ret = ricoh619_read(info->dev->parent, 0x6b, &data[1]);
3460 ret = ricoh619_read(info->dev->parent, 0x6c, &data[2]);
3461 ret = ricoh619_read(info->dev->parent, 0x6d, &data[3]);
3462 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG,&data[4]);
3463 reg[0]= (data[0]<<4) |data[1];
3464 reg[1]= (data[2]<<4) |data[3];
3466 // printk("PMU vadp_drop:%s In %08x %08x %08x %08x %08x %08x %d\n", __func__,data[0],data[1],data[2],data[3],reg[0],reg[1],ret);
3467 if ((2*(reg[0] +82)) > 3*reg[1]){
3468 ricoh619_write(info->dev->parent, 0xb3, 0x28);
3469 // printk("PMU vadp_drop charger disable:%s In %08x %08x\n", __func__,reg[0],reg[1]);
3471 else if(data[4] & 0xc0){
3472 ret = ricoh619_read(info->dev->parent, 0xb3, &data[5]);
3473 // printk("PMU charger is disabled:%s data[4]= %08x data[5]=%08x\n", __func__,data[4],data[5]);
3474 if(((data[5] & 0x03) ==0)|| ((data[5] & 0x08)==0)){
3475 ricoh619_write(info->dev->parent, 0xb3, 0x23);
3476 ret = ricoh619_read(info->dev->parent, 0xb3, &data[5]);
3477 // printk("PMU charger enable:%s data[4]= %08x data[5]=%08x\n", __func__,data[4],data[5]);
3480 power_supply_changed(&info->battery);
3481 power_supply_changed(&powerac);
3482 power_supply_changed(&powerusb);
3483 mutex_unlock(&info->lock);
3484 queue_delayed_work(info->monitor_wqueue, &info->vadp_drop_work,3*HZ);
3489 * Get Charger Priority
3490 * - get higher-priority between VADP and VUSB
3491 * @ data: higher-priority is stored
3496 static int get_charge_priority(struct ricoh619_battery_info *info, bool *data)
3501 ret = ricoh619_read(info->dev->parent, CHGCTL1_REG, &val);
3510 * Set Charger Priority
3511 * - set higher-priority between VADP and VUSB
3512 * - data: higher-priority is stored
3517 static int set_charge_priority(struct ricoh619_battery_info *info, bool *data)
3523 ret = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, val);
3525 ret = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, val);
3530 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
3531 static int get_check_fuel_gauge_reg(struct ricoh619_battery_info *info,
3532 int Reg_h, int Reg_l, int enable_bit)
3534 uint8_t get_data_h, get_data_l;
3535 int old_data, current_data;
3541 for (i = 0; i < 5 ; i++) {
3542 ret = ricoh619_read(info->dev->parent, Reg_h, &get_data_h);
3544 dev_err(info->dev, "Error in reading the control register\n");
3548 ret = ricoh619_read(info->dev->parent, Reg_l, &get_data_l);
3550 dev_err(info->dev, "Error in reading the control register\n");
3554 current_data = ((get_data_h & 0xff) << 8) | (get_data_l & 0xff);
3555 current_data = (current_data & enable_bit);
3557 if (current_data == old_data)
3558 return current_data;
3560 old_data = current_data;
3563 return current_data;
3566 static int calc_capacity(struct ricoh619_battery_info *info)
3574 temperature = get_battery_temp_2(info) / 10; /* unit 0.1 degree -> 1 degree */
3576 if (temperature >= 25) {
3578 } else if (temperature >= 5) {
3579 nt = (25 - temperature) * RICOH619_TAH_SEL2 * 625 / 100;
3581 nt = (625 + (5 - temperature) * RICOH619_TAL_SEL2 * 625 / 100);
3584 /* get remaining battery capacity from fuel gauge */
3585 ret = ricoh619_read(info->dev->parent, SOC_REG, &capacity);
3587 dev_err(info->dev, "Error in reading the control register\n");
3591 temp = capacity * 100 * 100 / (10000 - nt);
3593 temp = min(100, temp);
3594 temp = max(0, temp);
3596 return temp; /* Unit is 1% */
3599 static int calc_capacity_2(struct ricoh619_battery_info *info)
3609 temperature = get_battery_temp_2(info) / 10; /* unit 0.1 degree -> 1 degree */
3611 if (temperature >= 25) {
3613 } else if (temperature >= 5) {
3614 nt = (25 - temperature) * RICOH619_TAH_SEL2 * 625 / 100;
3616 nt = (625 + (5 - temperature) * RICOH619_TAL_SEL2 * 625 / 100);
3619 re_cap = get_check_fuel_gauge_reg(info, RE_CAP_H_REG, RE_CAP_L_REG,
3621 fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
3625 capacity = ((long)re_cap * 100 * 100 / fa_cap);
3626 capacity = (long)(min(10000, (int)capacity));
3627 capacity = (long)(max(0, (int)capacity));
3629 ret = ricoh619_read(info->dev->parent, SOC_REG, &val);
3631 dev_err(info->dev, "Error in reading the control register\n");
3634 capacity = (long)val * 100;
3638 temp = (int)(capacity * 100 * 100 / (10000 - nt));
3640 temp = min(10000, temp);
3641 temp = max(0, temp);
3643 return temp; /* Unit is 0.01% */
3646 static int get_battery_temp(struct ricoh619_battery_info *info)
3651 ret = get_check_fuel_gauge_reg(info, TEMP_1_REG, TEMP_2_REG, 0x0fff);
3653 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
3657 /* bit3 of 0xED(TEMP_1) is sign_bit */
3658 sign_bit = ((ret & 0x0800) >> 11);
3660 ret = (ret & 0x07ff);
3662 if (sign_bit == 0) /* positive value part */
3663 /* conversion unit */
3664 /* 1 unit is 0.0625 degree and retun unit
3665 * should be 0.1 degree,
3667 ret = ret * 625 / 1000;
3668 else { /*negative value part */
3669 ret = (~ret + 1) & 0x7ff;
3670 ret = -1 * ret * 625 / 1000;
3676 static int get_battery_temp_2(struct ricoh619_battery_info *info)
3678 uint8_t reg_buff[2];
3679 long temp, temp_off, temp_gain;
3680 bool temp_sign, temp_off_sign, temp_gain_sign;
3689 /* Calculate TEMP */
3690 ret = get_check_fuel_gauge_reg(info, TEMP_1_REG, TEMP_2_REG, 0x0fff);
3692 dev_err(info->dev, "Error in reading the fuel gauge register\n");
3697 temp_sign = (reg_val & 0x0800) >> 11;
3698 reg_val = (reg_val & 0x07ff);
3700 if (temp_sign == 0) /* positive value part */
3701 /* the unit is 0.0001 degree */
3702 temp = (long)reg_val * 625;
3703 else { /*negative value part */
3704 reg_val = (~reg_val + 1) & 0x7ff;
3705 temp = -1 * (long)reg_val * 625;
3708 /* Calculate TEMP_OFF */
3709 ret = ricoh619_bulk_reads_bank1(info->dev->parent,
3710 TEMP_OFF_H_REG, 2, reg_buff);
3712 dev_err(info->dev, "Error in reading the fuel gauge register\n");
3716 reg_val = reg_buff[0] << 8 | reg_buff[1];
3717 temp_off_sign = (reg_val & 0x0800) >> 11;
3718 reg_val = (reg_val & 0x07ff);
3720 if (temp_off_sign == 0) /* positive value part */
3721 /* the unit is 0.0001 degree */
3722 temp_off = (long)reg_val * 625;
3723 else { /*negative value part */
3724 reg_val = (~reg_val + 1) & 0x7ff;
3725 temp_off = -1 * (long)reg_val * 625;
3728 /* Calculate TEMP_GAIN */
3729 ret = ricoh619_bulk_reads_bank1(info->dev->parent,
3730 TEMP_GAIN_H_REG, 2, reg_buff);
3732 dev_err(info->dev, "Error in reading the fuel gauge register\n");
3736 reg_val = reg_buff[0] << 8 | reg_buff[1];
3737 temp_gain_sign = (reg_val & 0x0800) >> 11;
3738 reg_val = (reg_val & 0x07ff);
3740 if (temp_gain_sign == 0) /* positive value part */
3741 /* 1 unit is 0.000488281. the result is 0.01 */
3742 temp_gain = (long)reg_val * 488281 / 100000;
3743 else { /*negative value part */
3744 reg_val = (~reg_val + 1) & 0x7ff;
3745 temp_gain = -1 * (long)reg_val * 488281 / 100000;
3748 /* Calculate VTHM */
3750 Vthm = (int)((temp - temp_off) / 4095 * 2500 / temp_gain);
3752 RICOH_FG_DBG("PMU %s Skip to compensate temperature\n", __func__);
3756 ret = measure_Ibatt_FG(info, &Iout);
3757 Vsns = Iout * 2 / 100;
3759 if (temp < -120000) {
3760 /* Low Temperature */
3761 if (0 != (2500 - Vthm)) {
3762 Rthm = 10 * 10 * (Vthm - Vsns) / (2500 - Vthm);
3764 RICOH_FG_DBG("PMU %s Skip to compensate temperature\n", __func__);
3769 R_ln2 = (R_ln1 * R_ln1 * R_ln1 * R_ln1 * R_ln1 / 100000
3770 - R_ln1 * R_ln1 * R_ln1 * R_ln1 * 2 / 100
3771 + R_ln1 * R_ln1 * R_ln1 * 11
3772 - R_ln1 * R_ln1 * 2980
3776 /* the unit of new_temp is 0.1 degree */
3777 new_temp = (int)((100 * 1000 * B_VALUE / (R_ln2 + B_VALUE * 100 * 1000 / 29815) - 27315) / 10);
3778 RICOH_FG_DBG("PMU %s low temperature %d\n", __func__, new_temp/10);
3779 } else if (temp > 520000) {
3780 /* High Temperature */
3781 if (0 != (2500 - Vthm)) {
3782 Rthm = 100 * 10 * (Vthm - Vsns) / (2500 - Vthm);
3784 RICOH_FG_DBG("PMU %s Skip to compensate temperature\n", __func__);
3787 RICOH_FG_DBG("PMU %s [Rthm] Rthm %d[ohm]\n", __func__, Rthm);
3790 R_ln2 = (R_ln1 * R_ln1 * R_ln1 * R_ln1 * R_ln1 / 100000 * 15652 / 100
3791 - R_ln1 * R_ln1 * R_ln1 * R_ln1 / 1000 * 23103 / 100
3792 + R_ln1 * R_ln1 * R_ln1 * 1298 / 100
3793 - R_ln1 * R_ln1 * 35089 / 100
3794 + R_ln1 * 50334 / 10
3796 /* the unit of new_temp is 0.1 degree */
3797 new_temp = (int)((100 * 100 * B_VALUE / (R_ln2 + B_VALUE * 100 * 100 / 29815) - 27315) / 10);
3798 RICOH_FG_DBG("PMU %s high temperature %d\n", __func__, new_temp/10);
3800 /* the unit of new_temp is 0.1 degree */
3801 new_temp = temp / 1000;
3807 new_temp = get_battery_temp(info);
3811 static int get_time_to_empty(struct ricoh619_battery_info *info)
3815 ret = get_check_fuel_gauge_reg(info, TT_EMPTY_H_REG, TT_EMPTY_L_REG,
3818 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
3822 /* conversion unit */
3823 /* 1unit is 1miniute and return nnit should be 1 second */
3829 static int get_time_to_full(struct ricoh619_battery_info *info)
3833 ret = get_check_fuel_gauge_reg(info, TT_FULL_H_REG, TT_FULL_L_REG,
3836 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
3845 /* battery voltage is get from Fuel gauge */
3846 static int measure_vbatt_FG(struct ricoh619_battery_info *info, int *data)
3850 if(info->soca->ready_fg == 1) {
3851 ret = get_check_fuel_gauge_reg(info, VOLTAGE_1_REG, VOLTAGE_2_REG,
3854 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
3859 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
3860 *data = *data * 50000 / 4095;
3861 /* return unit should be 1uV */
3862 *data = *data * 100;
3863 info->soca->Vbat_old = *data;
3865 *data = info->soca->Vbat_old;
3871 static int measure_Ibatt_FG(struct ricoh619_battery_info *info, int *data)
3875 ret = get_check_fuel_gauge_reg(info, CC_AVERAGE1_REG,
3876 CC_AVERAGE0_REG, 0x3fff);
3878 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
3882 *data = (ret > 0x1fff) ? (ret - 0x4000) : ret;
3886 static int get_OCV_init_Data(struct ricoh619_battery_info *info, int index)
3889 ret = (battery_init_para[info->num][index*2]<<8) | (battery_init_para[info->num][index*2+1]);
3893 static int get_OCV_voltage(struct ricoh619_battery_info *info, int index)
3896 ret = get_OCV_init_Data(info, index);
3897 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
3898 ret = ret * 50000 / 4095;
3899 /* return unit should be 1uV */
3905 /* battery voltage is get from ADC */
3906 static int measure_vbatt_ADC(struct ricoh619_battery_info *info, int *data)
3909 uint8_t data_l = 0, data_h = 0;
3912 /* ADC interrupt enable */
3913 ret = ricoh619_set_bits(info->dev->parent, INTEN_REG, 0x08);
3915 dev_err(info->dev, "Error in setting the control register bit\n");
3919 /* enable interrupt request of single mode */
3920 ret = ricoh619_set_bits(info->dev->parent, EN_ADCIR3_REG, 0x01);
3922 dev_err(info->dev, "Error in setting the control register bit\n");
3926 /* single request */
3927 ret = ricoh619_write(info->dev->parent, ADCCNT3_REG, 0x10);
3929 dev_err(info->dev, "Error in writing the control register\n");
3933 for (i = 0; i < 5; i++) {
3935 RICOH_FG_DBG("ADC conversion times: %d\n", i);
3936 /* read completed flag of ADC */
3937 ret = ricoh619_read(info->dev->parent, EN_ADCIR3_REG, &data_h);
3939 dev_err(info->dev, "Error in reading the control register\n");
3947 dev_err(info->dev, "ADC conversion too long!\n");
3951 ret = ricoh619_read(info->dev->parent, VBATDATAH_REG, &data_h);
3953 dev_err(info->dev, "Error in reading the control register\n");
3957 ret = ricoh619_read(info->dev->parent, VBATDATAL_REG, &data_l);
3959 dev_err(info->dev, "Error in reading the control register\n");
3963 *data = ((data_h & 0xff) << 4) | (data_l & 0x0f);
3964 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
3965 *data = *data * 5000 / 4095;
3966 /* return unit should be 1uV */
3967 *data = *data * 1000;
3976 static int measure_vsys_ADC(struct ricoh619_battery_info *info, int *data)
3978 uint8_t data_l = 0, data_h = 0;
3981 ret = ricoh619_read(info->dev->parent, VSYSDATAH_REG, &data_h);
3983 dev_err(info->dev, "Error in reading the control register\n");
3986 ret = ricoh619_read(info->dev->parent, VSYSDATAL_REG, &data_l);
3988 dev_err(info->dev, "Error in reading the control register\n");
3991 *data = ((data_h & 0xff) << 4) | (data_l & 0x0f);
3992 *data = *data * 1000 * 3 * 5 / 2 / 4095;
3993 /* return unit should be 1uV */
3994 *data = *data * 1000;
3999 static void ricoh619_external_power_changed(struct power_supply *psy)
4001 struct ricoh619_battery_info *info;
4003 info = container_of(psy, struct ricoh619_battery_info, battery);
4004 queue_delayed_work(info->monitor_wqueue,
4005 &info->changed_work, HZ / 2);
4010 static int ricoh619_batt_get_prop(struct power_supply *psy,
4011 enum power_supply_property psp,
4012 union power_supply_propval *val)
4014 struct ricoh619_battery_info *info = dev_get_drvdata(psy->dev->parent);
4019 mutex_lock(&info->lock);
4023 case POWER_SUPPLY_PROP_ONLINE:
4024 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
4026 dev_err(info->dev, "Error in reading the control register\n");
4027 mutex_unlock(&info->lock);
4030 #ifdef SUPPORT_USB_CONNECT_TO_ADP
4031 if (psy->type == POWER_SUPPLY_TYPE_MAINS)
4032 if((2 == dwc_otg_check_dpdm(0)) && (status & 0x40))
4036 else if (psy->type == POWER_SUPPLY_TYPE_USB)
4037 if((1 == dwc_otg_check_dpdm(0)) && (status & 0x40))
4042 if (psy->type == POWER_SUPPLY_TYPE_MAINS)
4043 val->intval = (status & 0x40 ? 1 : 0);
4044 else if (psy->type == POWER_SUPPLY_TYPE_USB)
4045 val->intval = (status & 0x80 ? 1 : 0);
4048 /* this setting is same as battery driver of 584 */
4049 case POWER_SUPPLY_PROP_STATUS:
4050 if(info->chg_complete_tm_ov_flag == 0)
4052 ret = get_power_supply_Android_status(info);
4055 /* RICOH_FG_DBG("Power Supply Status is %d\n",
4060 val->intval = POWER_SUPPLY_STATUS_FULL;
4064 /* this setting is same as battery driver of 584 */
4065 case POWER_SUPPLY_PROP_PRESENT:
4066 // val->intval = info->present;
4070 /* current voltage is get from fuel gauge */
4071 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
4072 /* return real vbatt Voltage */
4073 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
4074 if (info->soca->ready_fg)
4075 ret = measure_vbatt_FG(info, &data);
4077 //val->intval = -EINVAL;
4078 data = info->cur_voltage * 1000;
4079 RICOH_FG_DBG( "battery voltage is not ready\n");
4082 ret = measure_vbatt_ADC(info, &data);
4085 /* convert unit uV -> mV */
4086 info->cur_voltage = data / 1000;
4088 RICOH_FG_DBG( "battery voltage is %d mV\n",
4092 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
4093 /* current battery capacity is get from fuel gauge */
4094 case POWER_SUPPLY_PROP_CAPACITY:
4095 if (info->entry_factory_mode){
4097 info->capacity = 100;
4098 } else if (info->soca->displayed_soc < 0) {
4100 info->capacity = 10;
4102 if(info->chg_complete_tm_ov_flag == 1)
4104 info->capacity = 100;
4105 val->intval = info->capacity;
4109 info->capacity = (info->soca->displayed_soc + 50)/100;
4110 val->intval = info->capacity;
4113 RICOH_FG_DBG("battery capacity is %d%%\n", info->capacity);
4116 /* current temperature of battery */
4117 case POWER_SUPPLY_PROP_TEMP:
4118 if (info->soca->ready_fg) {
4120 val->intval = get_battery_temp_2(info);
4121 info->battery_temp = val->intval/10;
4122 RICOH_FG_DBG( "battery temperature is %d degree\n", info->battery_temp);
4124 val->intval = info->battery_temp * 10;
4125 /* RICOH_FG_DBG("battery temperature is not ready\n"); */
4130 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
4131 if (info->soca->ready_fg) {
4132 ret = get_time_to_empty(info);
4134 info->time_to_empty = ret/60;
4135 RICOH_FG_DBG("time of empty battery is %d minutes\n", info->time_to_empty);
4137 //val->intval = -EINVAL;
4138 val->intval = info->time_to_empty * 60;
4139 RICOH_FG_DBG("time of empty battery is %d minutes\n", info->time_to_empty);
4140 /* RICOH_FG_DBG( "time of empty battery is not ready\n"); */
4144 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
4145 if (info->soca->ready_fg) {
4146 ret = get_time_to_full(info);
4148 info->time_to_full = ret/60;
4149 RICOH_FG_DBG( "time of full battery is %d minutes\n", info->time_to_full);
4151 //val->intval = -EINVAL;
4152 val->intval = info->time_to_full * 60;
4153 RICOH_FG_DBG( "time of full battery is %d minutes\n", info->time_to_full);
4154 /* RICOH_FG_DBG("time of full battery is not ready\n"); */
4160 case POWER_SUPPLY_PROP_TECHNOLOGY:
4161 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
4165 case POWER_SUPPLY_PROP_HEALTH:
4166 val->intval = POWER_SUPPLY_HEALTH_GOOD;
4169 case POWER_SUPPLY_PROP_CURRENT_AVG:
4170 measure_Ibatt_FG(info, &data);
4171 //RICOH_FG_DBG("average current xxxxxxxxxxxxxx %d \n", data);
4174 mutex_unlock(&info->lock);
4178 mutex_unlock(&info->lock);
4183 static enum power_supply_property ricoh619_batt_props[] = {
4184 POWER_SUPPLY_PROP_STATUS,
4185 POWER_SUPPLY_PROP_PRESENT,
4186 POWER_SUPPLY_PROP_VOLTAGE_NOW,
4187 POWER_SUPPLY_PROP_CURRENT_AVG,
4189 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
4190 POWER_SUPPLY_PROP_CAPACITY,
4191 POWER_SUPPLY_PROP_TEMP,
4192 //POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
4193 //POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
4195 POWER_SUPPLY_PROP_TECHNOLOGY,
4196 POWER_SUPPLY_PROP_HEALTH,
4199 static enum power_supply_property ricoh619_power_props[] = {
4200 POWER_SUPPLY_PROP_ONLINE,
4203 struct power_supply powerac = {
4205 .type = POWER_SUPPLY_TYPE_MAINS,
4206 .properties = ricoh619_power_props,
4207 .num_properties = ARRAY_SIZE(ricoh619_power_props),
4208 .get_property = ricoh619_batt_get_prop,
4211 struct power_supply powerusb = {
4213 .type = POWER_SUPPLY_TYPE_USB,
4214 .properties = ricoh619_power_props,
4215 .num_properties = ARRAY_SIZE(ricoh619_power_props),
4216 .get_property = ricoh619_batt_get_prop,
4220 static struct ricoh619_battery_platform_data *
4221 ricoh619_battery_dt_init(struct platform_device *pdev)
4223 struct device_node *nproot = pdev->dev.parent->of_node;
4224 struct device_node *np;
4225 struct ricoh619_battery_platform_data *pdata;
4229 return pdev->dev.platform_data;
4231 np = of_find_node_by_name(nproot, "battery");
4233 dev_err(&pdev->dev, "failed to find battery node\n");
4237 pdata = devm_kzalloc(&pdev->dev,
4238 sizeof(struct ricoh619_battery_platform_data),
4241 of_property_read_u32(np, "ricoh,monitor-time", &pdata->monitor_time);
4242 of_property_read_u32(np, "ricoh,alarm-vol-mv", &pdata->alarm_vol_mv);
4244 /* check rage of b,.attery type */
4245 type_n = Battery_Type();
4246 RICOH_FG_DBG("%s type_n=%d,temp is %d\n", __func__, type_n,temp);
4250 of_property_read_u32(np, "ricoh,ch-vfchg", &pdata->type[0].ch_vfchg);
4251 of_property_read_u32(np, "ricoh,ch-vrchg", &pdata->type[0].ch_vrchg);
4252 of_property_read_u32(np, "ricoh,ch-vbatovset", &pdata->type[0].ch_vbatovset);
4253 of_property_read_u32(np, "ricoh,ch-ichg", &pdata->type[0].ch_ichg);
4254 of_property_read_u32(np, "ricoh,ch-ilim-adp", &pdata->type[0].ch_ilim_adp);
4255 of_property_read_u32(np, "ricoh,ch-ilim-usb", &pdata->type[0].ch_ilim_usb);
4256 of_property_read_u32(np, "ricoh,ch-icchg", &pdata->type[0].ch_icchg);
4257 of_property_read_u32(np, "ricoh,fg-target-vsys", &pdata->type[0].fg_target_vsys);
4258 of_property_read_u32(np, "ricoh,fg-target-ibat", &pdata->type[0].fg_target_ibat);
4259 of_property_read_u32(np, "ricoh,fg-poff-vbat", &pdata->type[0].fg_poff_vbat);
4260 of_property_read_u32(np, "ricoh,jt-en", &pdata->type[0].jt_en);
4261 of_property_read_u32(np, "ricoh,jt-hw-sw", &pdata->type[0].jt_hw_sw);
4262 of_property_read_u32(np, "ricoh,jt-temp-h", &pdata->type[0].jt_temp_h);
4263 of_property_read_u32(np, "ricoh,jt-temp-l", &pdata->type[0].jt_temp_l);
4264 of_property_read_u32(np, "ricoh,jt-vfchg-h", &pdata->type[0].jt_vfchg_h);
4265 of_property_read_u32(np, "ricoh,jt-vfchg-l", &pdata->type[0].jt_vfchg_l);
4266 of_property_read_u32(np, "ricoh,jt-ichg-h", &pdata->type[0].jt_ichg_h);
4267 of_property_read_u32(np, "ricoh,jt-ichg-l", &pdata->type[0].jt_ichg_l);
4271 of_property_read_u32(np, "ricoh,ch-vfchg-1", &pdata->type[1].ch_vfchg);
4272 of_property_read_u32(np, "ricoh,ch-vrchg-1", &pdata->type[1].ch_vrchg);
4273 of_property_read_u32(np, "ricoh,ch-vbatovset-1", &pdata->type[1].ch_vbatovset);
4274 of_property_read_u32(np, "ricoh,ch-ichg-1", &pdata->type[1].ch_ichg);
4275 of_property_read_u32(np, "ricoh,ch-ilim-adp-1", &pdata->type[1].ch_ilim_adp);
4276 of_property_read_u32(np, "ricoh,ch-ilim-usb-1", &pdata->type[1].ch_ilim_usb);
4277 of_property_read_u32(np, "ricoh,ch-icchg-1", &pdata->type[1].ch_icchg);
4278 of_property_read_u32(np, "ricoh,fg-target-vsys-1", &pdata->type[1].fg_target_vsys);
4279 of_property_read_u32(np, "ricoh,fg-target-ibat-1", &pdata->type[1].fg_target_ibat);
4280 of_property_read_u32(np, "ricoh,fg-poff-vbat-1", &pdata->type[1].fg_poff_vbat);
4281 of_property_read_u32(np, "ricoh,jt-en-1", &pdata->type[1].jt_en);
4282 of_property_read_u32(np, "ricoh,jt-hw-sw-1", &pdata->type[1].jt_hw_sw);
4283 of_property_read_u32(np, "ricoh,jt-temp-h-1", &pdata->type[1].jt_temp_h);
4284 of_property_read_u32(np, "ricoh,jt-temp-l-1", &pdata->type[1].jt_temp_l);
4285 of_property_read_u32(np, "ricoh,jt-vfchg-h-1", &pdata->type[1].jt_vfchg_h);
4286 of_property_read_u32(np, "ricoh,jt-vfchg-l-1", &pdata->type[1].jt_vfchg_l);
4287 of_property_read_u32(np, "ricoh,jt-ichg-h-1", &pdata->type[1].jt_ichg_h);
4288 of_property_read_u32(np, "ricoh,jt-ichg-l-1", &pdata->type[1].jt_ichg_l);
4301 static struct ricoh619_battery_platform_data *
4302 ricoh619_battery_dt_init(struct platform_device *pdev)
4304 return pdev->dev.platform_data;
4308 static int ricoh619_battery_probe(struct platform_device *pdev)
4310 struct ricoh619_battery_info *info;
4311 struct ricoh619_battery_platform_data *pdata;
4312 struct ricoh619 *ricoh619 = dev_get_drvdata(pdev->dev.parent);
4315 RICOH_FG_DBG(KERN_INFO "PMU: %s : version is %s\n", __func__,RICOH619_BATTERY_VERSION);
4317 pdata = ricoh619_battery_dt_init(pdev);
4319 dev_err(&pdev->dev, "platform data isn't assigned to "
4323 info = devm_kzalloc(ricoh619->dev,sizeof(struct ricoh619_battery_info), GFP_KERNEL);
4326 info->soca = devm_kzalloc(ricoh619->dev,sizeof(struct ricoh619_soca_info), GFP_KERNEL);
4330 info->dev = &pdev->dev;
4331 info->status = POWER_SUPPLY_STATUS_CHARGING;
4332 info->monitor_time = pdata->monitor_time * HZ;
4333 info->alarm_vol_mv = pdata->alarm_vol_mv;
4335 /* check rage of battery num */
4336 info->num = Battery_Table();
4337 temp = sizeof(battery_init_para)/(sizeof(uint8_t)*32);
4338 if(info->num >= (sizeof(battery_init_para)/(sizeof(uint8_t)*32)))
4340 RICOH_FG_DBG("%s : Battery num is out of range\n", __func__);
4343 RICOH_FG_DBG("%s info->num=%d,temp is %d\n", __func__, info->num,temp);
4345 /* these valuse are set in platform */
4346 info->ch_vfchg = pdata->type[type_n].ch_vfchg;
4347 info->ch_vrchg = pdata->type[type_n].ch_vrchg;
4348 info->ch_vbatovset = pdata->type[type_n].ch_vbatovset;
4349 info->ch_ichg = pdata->type[type_n].ch_ichg;
4350 info->ch_ilim_adp = pdata->type[type_n].ch_ilim_adp;
4351 info->ch_ilim_usb = pdata->type[type_n].ch_ilim_usb;
4352 info->ch_icchg = pdata->type[type_n].ch_icchg;
4353 info->fg_target_vsys = pdata->type[type_n].fg_target_vsys;
4354 info->fg_target_ibat = pdata->type[type_n].fg_target_ibat;
4355 info->fg_poff_vbat = pdata->type[type_n].fg_poff_vbat;
4356 info->jt_en = pdata->type[type_n].jt_en;
4357 info->jt_hw_sw = pdata->type[type_n].jt_hw_sw;
4358 info->jt_temp_h = pdata->type[type_n].jt_temp_h;
4359 info->jt_temp_l = pdata->type[type_n].jt_temp_l;
4360 info->jt_vfchg_h = pdata->type[type_n].jt_vfchg_h;
4361 info->jt_vfchg_l = pdata->type[type_n].jt_vfchg_l;
4362 info->jt_ichg_h = pdata->type[type_n].jt_ichg_h;
4363 info->jt_ichg_l = pdata->type[type_n].jt_ichg_l;
4365 info->adc_vdd_mv = ADC_VDD_MV; /* 2800; */
4366 info->min_voltage = MIN_VOLTAGE; /* 3100; */
4367 info->max_voltage = MAX_VOLTAGE; /* 4200; */
4369 info->entry_factory_mode = false;
4371 info->chg_complete_rd_flag = 0;
4372 info->chg_complete_rd_cnt = 0;
4373 info->chg_complete_tm_ov_flag = 0;
4374 info->chg_complete_sleep_flag = 0;
4376 mutex_init(&info->lock);
4377 platform_set_drvdata(pdev, info);
4379 info->battery.name = "battery";
4380 info->battery.type = POWER_SUPPLY_TYPE_BATTERY;
4381 info->battery.properties = ricoh619_batt_props;
4382 info->battery.num_properties = ARRAY_SIZE(ricoh619_batt_props);
4383 info->battery.get_property = ricoh619_batt_get_prop;
4384 info->battery.set_property = NULL;
4385 /* info->battery.external_power_changed
4386 = ricoh619_external_power_changed; */
4388 /* Disable Charger/ADC interrupt */
4389 ret = ricoh619_clr_bits(info->dev->parent, RICOH619_INTC_INTEN,
4394 ret = ricoh619_init_battery(info);
4398 #ifdef ENABLE_FACTORY_MODE
4399 info->factory_mode_wqueue
4400 = create_singlethread_workqueue("ricoh619_factory_mode");
4401 INIT_DEFERRABLE_WORK(&info->factory_mode_work,
4402 check_charging_state_work);
4404 ret = ricoh619_factory_mode(info);
4410 ret = power_supply_register(&pdev->dev, &info->battery);
4413 info->battery.dev->parent = &pdev->dev;
4415 ret = power_supply_register(&pdev->dev, &powerac);
4416 ret = power_supply_register(&pdev->dev, &powerusb);
4418 info->monitor_wqueue
4419 = create_singlethread_workqueue("ricoh619_battery_monitor");
4421 info->workqueue = create_singlethread_workqueue("rc5t619_charger_in");
4422 INIT_WORK(&info->irq_work, charger_irq_work);
4425 = create_singlethread_workqueue("rc5t619_usb_det");
4426 INIT_WORK(&info->usb_irq_work, usb_det_irq_work);
4428 INIT_DEFERRABLE_WORK(&info->monitor_work,
4429 ricoh619_battery_work);
4430 INIT_DEFERRABLE_WORK(&info->displayed_work,
4431 ricoh619_displayed_work);
4432 INIT_DEFERRABLE_WORK(&info->charge_stable_work,
4433 ricoh619_stable_charge_countdown_work);
4434 INIT_DEFERRABLE_WORK(&info->charge_monitor_work,
4435 ricoh619_charge_monitor_work);
4436 INIT_DEFERRABLE_WORK(&info->get_charge_work,
4437 ricoh619_get_charge_work);
4438 INIT_DEFERRABLE_WORK(&info->jeita_work, ricoh619_jeita_work);
4439 INIT_DELAYED_WORK(&info->changed_work, ricoh619_changed_work);
4441 INIT_DELAYED_WORK(&info->charge_complete_ready, ricoh619_charging_complete_work);
4443 /* Charger IRQ workqueue settings */
4445 ret = request_threaded_irq( irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FONCHGINT),NULL, charger_in_isr, IRQF_ONESHOT,
4446 "rc5t619_charger_in", info);
4448 dev_err(&pdev->dev, "Can't get CHG_INT IRQ for chrager: %d\n",ret);
4452 ret = request_threaded_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FCHGCMPINT),NULL, charger_complete_isr,
4453 IRQF_ONESHOT, "rc5t619_charger_comp",info);
4455 dev_err(&pdev->dev, "Can't get CHG_COMP IRQ for chrager: %d\n",ret);
4459 ret = request_threaded_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FVUSBDETSINT) ,NULL, charger_usb_isr, IRQF_ONESHOT,
4460 "rc5t619_usb_det", info);
4462 dev_err(&pdev->dev, "Can't get USB_DET IRQ for chrager: %d\n",ret);
4466 ret = request_threaded_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FVADPDETSINT),NULL, charger_adp_isr, IRQF_ONESHOT,
4467 "rc5t619_adp_det", info);
4470 "Can't get ADP_DET IRQ for chrager: %d\n", ret);
4474 #ifdef ENABLE_LOW_BATTERY_DETECTION
4475 ret = request_threaded_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_VSYSLIR) ,NULL, adc_vsysl_isr, IRQF_ONESHOT,
4476 "rc5t619_adc_vsysl", info);
4479 "Can't get ADC_VSYSL IRQ for chrager: %d\n", ret);
4482 INIT_DEFERRABLE_WORK(&info->low_battery_work,
4483 low_battery_irq_work);
4485 #ifdef RICOH619_VADP_DROP_WORK
4486 INIT_DEFERRABLE_WORK(&info->vadp_drop_work,vadp_drop_irq_work);
4487 queue_delayed_work(info->monitor_wqueue, &info->vadp_drop_work,0);
4489 /* Charger init and IRQ setting */
4490 ret = ricoh619_init_charger(info);
4494 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
4495 ret = ricoh619_init_fgsoca(info);
4497 queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
4498 RICOH619_MONITOR_START_TIME*HZ);
4500 /* Enable Charger/ADC interrupt */
4501 ricoh619_set_bits(info->dev->parent, RICOH619_INTC_INTEN, CHG_INT | ADC_INT);
4510 static int ricoh619_battery_remove(struct platform_device *pdev)
4512 struct ricoh619_battery_info *info = platform_get_drvdata(pdev);
4513 struct ricoh619 *ricoh619 = dev_get_drvdata(pdev->dev.parent);
4518 bool is_charging = true;
4519 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
4521 && (info->soca->status == RICOH619_SOCA_STABLE)) {
4522 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
4524 dev_err(info->dev, "Error in writing PSWR_REG\n");
4526 } else if (info->soca->status != RICOH619_SOCA_START
4527 && info->soca->status != RICOH619_SOCA_UNSTABLE) {
4528 if (info->soca->displayed_soc <= 0) {
4531 val = (info->soca->displayed_soc + 50)/100;
4534 ret = ricoh619_write(info->dev->parent, PSWR_REG, val);
4536 dev_err(info->dev, "Error in writing PSWR_REG\n");
4540 ret = calc_capacity_in_period(info, &cc_cap,
4541 &is_charging, true);
4543 dev_err(info->dev, "Read cc_sum Error !!-----\n");
4546 if (g_fg_on_mode == 0) {
4547 ret = ricoh619_clr_bits(info->dev->parent,
4550 dev_err(info->dev, "Error in clr FG EN\n");
4553 /* set rapid timer 300 min */
4554 err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x03);
4556 dev_err(info->dev, "Error in writing the control register\n");
4559 if(info->capacity == 100)
4561 ret = ricoh619_write(info->dev->parent, PSWR_REG, 100);
4563 dev_err(info->dev, "Error in writing PSWR_REG\n");
4566 free_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FONCHGINT), &info);
4567 free_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FCHGCMPINT), &info);
4568 free_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FVUSBDETSINT), &info);
4569 free_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FVADPDETSINT) , &info);
4570 #ifdef ENABLE_LOW_BATTERY_DETECTION
4571 free_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_VSYSLIR), &info);
4574 cancel_delayed_work(&info->monitor_work);
4575 cancel_delayed_work(&info->charge_stable_work);
4576 cancel_delayed_work(&info->charge_monitor_work);
4577 cancel_delayed_work(&info->get_charge_work);
4578 cancel_delayed_work(&info->displayed_work);
4580 cancel_delayed_work(&info->changed_work);
4581 #ifdef ENABLE_LOW_BATTERY_DETECTION
4582 cancel_delayed_work(&info->low_battery_work);
4584 #ifdef RICOH619_VADP_DROP_WORK
4585 cancel_delayed_work(&info->vadp_drop_work);
4587 cancel_delayed_work(&info->factory_mode_work);
4588 cancel_delayed_work(&info->jeita_work);
4589 cancel_delayed_work(&info->charge_complete_ready);
4591 cancel_work_sync(&info->irq_work);
4592 cancel_work_sync(&info->usb_irq_work);
4594 flush_workqueue(info->monitor_wqueue);
4595 flush_workqueue(info->workqueue);
4596 flush_workqueue(info->usb_workqueue);
4597 flush_workqueue(info->factory_mode_wqueue);
4599 destroy_workqueue(info->monitor_wqueue);
4600 destroy_workqueue(info->workqueue);
4601 destroy_workqueue(info->usb_workqueue);
4602 destroy_workqueue(info->factory_mode_wqueue);
4604 power_supply_unregister(&info->battery);
4606 platform_set_drvdata(pdev, NULL);
4611 static int ricoh619_battery_suspend(struct device *dev)
4613 struct ricoh619_battery_info *info = dev_get_drvdata(dev);
4618 bool is_charging = true;
4619 int displayed_soc_temp;
4622 && (info->soca->status == RICOH619_SOCA_STABLE)) {
4623 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
4625 dev_err(info->dev, "Error in writing PSWR_REG\n");
4627 info->soca->suspend_soc = info->soca->displayed_soc;
4628 ret = calc_capacity_in_period(info, &cc_cap,
4629 &is_charging, true);
4631 dev_err(info->dev, "Read cc_sum Error !!-----\n");
4633 } else if (info->soca->status != RICOH619_SOCA_START
4634 && info->soca->status != RICOH619_SOCA_UNSTABLE) {
4635 if (info->soca->displayed_soc <= 0) {
4638 val = (info->soca->displayed_soc + 50)/100;
4641 ret = ricoh619_write(info->dev->parent, PSWR_REG, val);
4643 dev_err(info->dev, "Error in writing PSWR_REG\n");
4647 ret = calc_capacity_in_period(info, &cc_cap,
4648 &is_charging, true);
4650 dev_err(info->dev, "Read cc_sum Error !!-----\n");
4652 if (info->soca->status != RICOH619_SOCA_STABLE) {
4653 info->soca->cc_delta
4654 = (is_charging == true) ? cc_cap : -cc_cap;
4657 = info->soca->displayed_soc + info->soca->cc_delta;
4658 displayed_soc_temp = min(10000, displayed_soc_temp);
4659 displayed_soc_temp = max(0, displayed_soc_temp);
4660 info->soca->displayed_soc = displayed_soc_temp;
4662 info->soca->suspend_soc = info->soca->displayed_soc;
4664 } else if (info->soca->status == RICOH619_SOCA_START
4665 || info->soca->status == RICOH619_SOCA_UNSTABLE) {
4667 ret = ricoh619_read(info->dev->parent, PSWR_REG, &val);
4669 dev_err(info->dev, "Error in reading the pswr register\n");
4672 info->soca->suspend_soc = val * 100;
4675 if (info->soca->status == RICOH619_SOCA_DISP
4676 || info->soca->status == RICOH619_SOCA_STABLE
4677 || info->soca->status == RICOH619_SOCA_FULL) {
4678 info->soca->soc = calc_capacity_2(info);
4679 info->soca->soc_delta =
4680 info->soca->soc_delta + (info->soca->soc - info->soca->last_soc);
4683 info->soca->soc_delta = 0;
4686 if (info->soca->status == RICOH619_SOCA_STABLE
4687 || info->soca->status == RICOH619_SOCA_FULL)
4688 info->soca->status = RICOH619_SOCA_DISP;
4689 /* set rapid timer 300 min */
4690 err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x03);
4692 dev_err(info->dev, "Error in writing the control register\n");
4695 // disable_irq(charger_irq + RICOH619_IRQ_FONCHGINT);
4696 // disable_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT);
4697 // disable_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT);
4698 // disable_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT);
4699 #ifdef ENABLE_LOW_BATTERY_DETECTION
4700 // disable_irq(charger_irq + RICOH619_IRQ_VSYSLIR);
4703 flush_delayed_work(&info->monitor_work);
4704 flush_delayed_work(&info->displayed_work);
4705 flush_delayed_work(&info->charge_stable_work);
4706 flush_delayed_work(&info->charge_monitor_work);
4707 flush_delayed_work(&info->get_charge_work);
4708 flush_delayed_work(&info->changed_work);
4709 #ifdef ENABLE_LOW_BATTERY_DETECTION
4710 flush_delayed_work(&info->low_battery_work);
4712 flush_delayed_work(&info->factory_mode_work);
4713 flush_delayed_work(&info->jeita_work);
4714 #ifdef RICOH619_VADP_DROP_WORK
4715 flush_delayed_work(&info->vadp_drop_work);
4718 // flush_work(&info->irq_work);
4719 // flush_work(&info->usb_irq_work);
4721 cancel_delayed_work(&info->monitor_work);
4722 cancel_delayed_work(&info->displayed_work);
4723 cancel_delayed_work(&info->charge_stable_work);
4724 cancel_delayed_work(&info->charge_monitor_work);
4725 cancel_delayed_work(&info->get_charge_work);
4726 cancel_delayed_work(&info->changed_work);
4727 #ifdef ENABLE_LOW_BATTERY_DETECTION
4728 cancel_delayed_work(&info->low_battery_work);
4730 cancel_delayed_work(&info->charge_complete_ready);
4731 cancel_delayed_work(&info->factory_mode_work);
4732 cancel_delayed_work(&info->jeita_work);
4733 #ifdef RICOH619_VADP_DROP_WORK
4734 cancel_delayed_work(&info->vadp_drop_work);
4736 info->chg_complete_rd_cnt = 0;
4737 info->chg_complete_rd_flag = 0;
4739 if(info->capacity == 100)
4741 ret = ricoh619_write(info->dev->parent, PSWR_REG, 100);
4743 dev_err(info->dev, "Error in writing PSWR_REG\n");
4744 if(info->chg_complete_tm_ov_flag != 1)
4746 info->chg_complete_tm_ov_flag = 0;
4747 info->chg_complete_sleep_flag = 1;
4750 // flush_work(&info->irq_work);
4751 // flush_work(&info->usb_irq_work);
4757 static int ricoh619_battery_resume(struct device *dev)
4759 struct ricoh619_battery_info *info = dev_get_drvdata(dev);
4762 int displayed_soc_temp;
4764 bool is_charging = true;
4765 bool is_jeita_updated;
4768 RICOH_FG_DBG(KERN_INFO "PMU: %s: \n", __func__);
4770 ret = check_jeita_status(info, &is_jeita_updated);
4772 dev_err(info->dev, "Error in updating JEITA %d\n", ret);
4775 if (info->entry_factory_mode) {
4776 info->soca->displayed_soc = -EINVAL;
4777 } else if (RICOH619_SOCA_ZERO == info->soca->status) {
4778 if (calc_ocv(info) > get_OCV_voltage(info, 0)) {
4779 ret = ricoh619_read(info->dev->parent, PSWR_REG, &val);
4781 info->soca->soc = val * 100;
4784 "Error in reading PSWR_REG %d\n", ret);
4786 = calc_capacity(info) * 100;
4789 ret = calc_capacity_in_period(info, &cc_cap,
4790 &is_charging, true);
4792 dev_err(info->dev, "Read cc_sum Error !!-----\n");
4794 info->soca->cc_delta
4795 = (is_charging == true) ? cc_cap : -cc_cap;
4798 = info->soca->soc + info->soca->cc_delta;
4799 if (displayed_soc_temp < 0)
4800 displayed_soc_temp = 0;
4801 displayed_soc_temp = min(10000, displayed_soc_temp);
4802 displayed_soc_temp = max(0, displayed_soc_temp);
4803 info->soca->displayed_soc = displayed_soc_temp;
4805 ret = reset_FG_process(info);
4808 dev_err(info->dev, "Error in writing the control register\n");
4809 info->soca->status = RICOH619_SOCA_FG_RESET;
4812 info->soca->displayed_soc = 0;
4814 info->soca->soc = info->soca->suspend_soc;
4816 if (RICOH619_SOCA_START == info->soca->status
4817 || RICOH619_SOCA_UNSTABLE == info->soca->status) {
4818 ret = calc_capacity_in_period(info, &cc_cap,
4819 &is_charging, false);
4821 ret = calc_capacity_in_period(info, &cc_cap,
4822 &is_charging, true);
4826 dev_err(info->dev, "Read cc_sum Error !!-----\n");
4828 info->soca->cc_delta = (is_charging == true) ? cc_cap : -cc_cap;
4830 displayed_soc_temp = info->soca->soc + info->soca->cc_delta;
4831 if (info->soca->zero_flg == 1) {
4832 if((info->soca->Ibat_ave >= 0)
4833 || (displayed_soc_temp >= 100)){
4834 info->soca->zero_flg = 0;
4836 displayed_soc_temp = 0;
4838 } else if (displayed_soc_temp < 100) {
4839 /* keep DSOC = 1 when Vbat is over 3.4V*/
4840 if( info->fg_poff_vbat != 0) {
4841 if (info->soca->Vbat_ave < 2000*1000) { /* error value */
4842 displayed_soc_temp = 100;
4843 } else if (info->soca->Vbat_ave < info->fg_poff_vbat*1000) {
4844 displayed_soc_temp = 0;
4845 info->soca->zero_flg = 1;
4847 displayed_soc_temp = 100;
4851 displayed_soc_temp = min(10000, displayed_soc_temp);
4852 displayed_soc_temp = max(0, displayed_soc_temp);
4854 if (0 == info->soca->jt_limit) {
4855 check_charge_status_2(info, displayed_soc_temp);
4857 info->soca->displayed_soc = displayed_soc_temp;
4860 if (RICOH619_SOCA_DISP == info->soca->status) {
4861 info->soca->last_soc = calc_capacity_2(info);
4865 ret = measure_vbatt_FG(info, &info->soca->Vbat_ave);
4866 ret = measure_vsys_ADC(info, &info->soca->Vsys_ave);
4867 ret = measure_Ibatt_FG(info, &info->soca->Ibat_ave);
4869 if(info->chg_complete_sleep_flag == 1)
4871 info->chg_complete_tm_ov_flag = 1;
4872 info->chg_complete_sleep_flag = 0;
4875 power_supply_changed(&info->battery);
4876 queue_delayed_work(info->monitor_wqueue, &info->displayed_work, HZ);
4878 if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
4879 info->soca->stable_count = 10;
4880 queue_delayed_work(info->monitor_wqueue,
4881 &info->charge_stable_work,
4882 RICOH619_FG_STABLE_TIME*HZ/10);
4883 } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
4884 info->soca->stable_count = 1;
4886 for (i = 0; i < 3; i = i+1)
4887 info->soca->reset_soc[i] = 0;
4888 info->soca->reset_count = 0;
4890 queue_delayed_work(info->monitor_wqueue,
4891 &info->charge_stable_work,
4892 RICOH619_FG_RESET_TIME*HZ);
4895 queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
4896 info->monitor_time);
4898 queue_delayed_work(info->monitor_wqueue, &info->charge_monitor_work,
4899 RICOH619_CHARGE_RESUME_TIME * HZ);
4901 info->soca->chg_count = 0;
4902 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
4903 RICOH619_CHARGE_RESUME_TIME * HZ);
4904 #ifdef RICOH619_VADP_DROP_WORK
4905 queue_delayed_work(info->monitor_wqueue, &info->vadp_drop_work,1 * HZ);
4908 if (!info->jt_hw_sw) {
4909 queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
4910 RICOH619_JEITA_UPDATE_TIME * HZ);
4913 // ricoh619_write(info->dev->parent, 0x9d, 0x00);
4914 // enable_irq(charger_irq + RICOH619_IRQ_FONCHGINT);
4915 // enable_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT);
4916 // enable_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT);
4917 // enable_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT);
4918 #ifdef ENABLE_LOW_BATTERY_DETECTION
4919 // enable_irq(charger_irq + RICOH619_IRQ_VSYSLIR);
4921 ricoh619_write(info->dev->parent, 0x9d, 0x4d);
4926 static const struct of_device_id ricoh619_battery_dt_match[] = {
4927 { .compatible = "ricoh,ricoh619-battery", },
4930 MODULE_DEVICE_TABLE(of, ricoh619_battery_dt_match);
4934 static const struct dev_pm_ops ricoh619_battery_pm_ops = {
4935 .suspend = ricoh619_battery_suspend,
4936 .resume = ricoh619_battery_resume,
4940 static struct platform_driver ricoh619_battery_driver = {
4942 .name = "ricoh619-battery",
4943 .owner = THIS_MODULE,
4944 .of_match_table = of_match_ptr(ricoh619_battery_dt_match),
4946 .pm = &ricoh619_battery_pm_ops,
4949 .probe = ricoh619_battery_probe,
4950 .remove = ricoh619_battery_remove,
4953 static int __init ricoh619_battery_init(void)
4955 RICOH_FG_DBG("PMU: %s\n", __func__);
4956 return platform_driver_register(&ricoh619_battery_driver);
4958 fs_initcall_sync(ricoh619_battery_init);
4960 static void __exit ricoh619_battery_exit(void)
4962 platform_driver_unregister(&ricoh619_battery_driver);
4964 module_exit(ricoh619_battery_exit);
4966 MODULE_DESCRIPTION("RICOH619 Battery driver");
4967 MODULE_ALIAS("platform:ricoh619-battery");
4968 MODULE_LICENSE("GPL");