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;
424 static int ricoh619_Check_OCV_Offset(struct ricoh619_battery_info *info)
426 int ocv_table[11]; // HEX value
430 uint8_t debug_disp[22];
433 RICOH_FG_DBG("PMU : %s : calc ocv %d get OCV %d\n",__func__,calc_ocv(info),get_OCV_voltage(info, RICOH619_OCV_OFFSET_BOUND));
435 /* check adp/usb status */
436 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &val);
438 dev_err(info->dev, "Error in reading the control register\n");
442 val = (val & 0xC0) >> 6;
444 if (val != 0){ /* connect adp or usb */
445 if (calc_ocv(info) < get_OCV_voltage(info, RICOH619_OCV_OFFSET_BOUND) )
447 if(0 == info->soca->ocv_table_low[0]){
448 for (i = 0 ; i < 11; i++){
449 ocv_table[i] = (battery_init_para[info->num][i*2]<<8) | (battery_init_para[info->num][i*2+1]);
450 RICOH_FG_DBG("PMU : %s : OCV table %d 0x%x\n",__func__,i * 10, ocv_table[i]);
451 info->soca->ocv_table_low[i] = ocv_table[i];
454 for (i = 0 ; i < 11; i++){
455 temp = ocv_table[i] * (100 + RICOH619_OCV_OFFSET_RATIO) / 100;
457 battery_init_para[info->num][i*2 + 1] = temp;
458 battery_init_para[info->num][i*2] = temp >> 8;
460 ret = ricoh619_clr_bits(info->dev->parent, FG_CTRL_REG, 0x01);
462 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
463 BAT_INIT_TOP_REG, 22, battery_init_para[info->num]);
465 ret = ricoh619_set_bits(info->dev->parent, FG_CTRL_REG, 0x01);
467 /* debug comment start*/
468 ret = ricoh619_bulk_reads_bank1(info->dev->parent,
469 BAT_INIT_TOP_REG, 22, debug_disp);
470 for (i = 0; i < 11; i++){
471 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]));
481 static int reset_FG_process(struct ricoh619_battery_info *info)
485 //err = set_Rlow(info);
486 //err = ricoh619_Check_OCV_Offset(info);
487 err = ricoh619_write(info->dev->parent,
489 info->soca->ready_fg = 0;
494 static int check_charge_status_2(struct ricoh619_battery_info *info, int displayed_soc_temp)
496 if (displayed_soc_temp < 0)
497 displayed_soc_temp = 0;
499 get_power_supply_status(info);
500 info->soca->soc = calc_capacity(info) * 100;
502 if (POWER_SUPPLY_STATUS_FULL == info->soca->chg_status) {
503 if ((info->first_pwon == 1)
504 && (RICOH619_SOCA_START == info->soca->status)) {
506 info->soca->soc_full = info->soca->soc;
507 info->soca->displayed_soc = 100*100;
508 info->soca->full_reset_count = 0;
510 if ( (displayed_soc_temp > 97*100)
511 && (calc_ocv(info) > (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*7/10) )){
513 info->soca->soc_full = info->soca->soc;
514 info->soca->displayed_soc = 100*100;
515 info->soca->full_reset_count = 0;
518 info->soca->displayed_soc = displayed_soc_temp;
523 if (info->soca->Ibat_ave >= 0) {
524 if (g_full_flag == 1) {
525 info->soca->displayed_soc = 100*100;
527 if (info->soca->displayed_soc/100 < 99) {
528 info->soca->displayed_soc = displayed_soc_temp;
530 info->soca->displayed_soc = 99 * 100;
534 if (info->soca->Ibat_ave < 0) {
535 if (g_full_flag == 1) {
536 if (calc_ocv(info) < (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*7/10) ) {
538 //info->soca->displayed_soc = 100*100;
539 info->soca->displayed_soc = displayed_soc_temp;
541 info->soca->displayed_soc = 100*100;
545 info->soca->displayed_soc = displayed_soc_temp;
549 return info->soca->displayed_soc;
553 * Calculate Capacity in a period
554 * - read CC_SUM & FA_CAP from Coulom Counter
555 * - and calculate Capacity.
556 * @cc_cap: capacity in a period, unit 0.01%
557 * @is_charging: Flag of charging current direction
558 * TRUE : charging (plus)
559 * FALSE: discharging (minus)
560 * @cc_rst: reset CC_SUM or not
564 static int calc_capacity_in_period(struct ricoh619_battery_info *info,
565 int *cc_cap, bool *is_charging, bool cc_rst)
568 uint8_t cc_sum_reg[4];
569 uint8_t cc_clr[4] = {0, 0, 0, 0};
570 uint8_t fa_cap_reg[2];
575 uint8_t charge_state;
577 uint32_t cc_cap_temp;
581 *is_charging = true; /* currrent state initialize -> charging */
583 if (info->entry_factory_mode)
586 //check need charge stop or not
587 /* get power supply status */
588 err = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
591 charge_state = (status & 0x1F);
592 Ocv = calc_ocv(info);
593 if (charge_state == CHG_STATE_CHG_COMPLETE) {
594 /* Check CHG status is complete or not */
596 } else if (calc_capacity(info) == 100) {
597 /* Check HW soc is 100 or not */
599 } else if (Ocv/1000 < get_OCV_voltage(info, 9)) {
600 /* Check VBAT is high level or not */
606 if (cc_stop_flag == 1)
608 /* Disable Charging/Completion Interrupt */
609 err = ricoh619_set_bits(info->dev->parent,
610 RICOH619_INT_MSK_CHGSTS1, 0x01);
614 /* disable charging */
615 err = ricoh619_clr_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x03);
621 err = ricoh619_write(info->dev->parent, CC_CTRL_REG, 0x01);
626 err = ricoh619_bulk_reads(info->dev->parent,
627 CC_SUMREG3_REG, 4, cc_sum_reg);
631 if (cc_rst == true) {
633 err = ricoh619_bulk_writes(info->dev->parent,
634 CC_SUMREG3_REG, 4, cc_clr);
640 err = ricoh619_write(info->dev->parent, CC_CTRL_REG, 0);
643 if (cc_stop_flag == 1)
646 /* Enable charging */
647 err = ricoh619_set_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x03);
653 /* Clear Charging Interrupt status */
654 err = ricoh619_clr_bits(info->dev->parent,
655 RICOH619_INT_IR_CHGSTS1, 0x01);
659 /* ricoh619_read(info->dev->parent, RICOH619_INT_IR_CHGSTS1, &val);
660 RICOH_FG_DBG("INT_IR_CHGSTS1 = 0x%x\n",val); */
662 /* Enable Charging Interrupt */
663 err = ricoh619_clr_bits(info->dev->parent,
664 RICOH619_INT_MSK_CHGSTS1, 0x01);
669 err = ricoh619_bulk_reads(info->dev->parent,
670 FA_CAP_H_REG, 2, fa_cap_reg);
674 /* fa_cap = *(uint16_t*)fa_cap_reg & 0x7fff; */
675 fa_cap = (fa_cap_reg[0] << 8 | fa_cap_reg[1]) & 0x7fff;
677 /* cc_sum = *(uint32_t*)cc_sum_reg; */
678 cc_sum = cc_sum_reg[0] << 24 | cc_sum_reg[1] << 16 |
679 cc_sum_reg[2] << 8 | cc_sum_reg[3];
681 /* calculation two's complement of CC_SUM */
682 if (cc_sum & 0x80000000) {
683 cc_sum = (cc_sum^0xffffffff)+0x01;
684 *is_charging = false; /* discharge */
686 /* (CC_SUM x 10000)/3600/FA_CAP */
687 *cc_cap = cc_sum*25/9/fa_cap; /* unit is 0.01% */
689 //////////////////////////////////////////////////////////////////
690 cc_cap_min = fa_cap*3600/100/100/100; /* Unit is 0.0001% */
691 cc_cap_temp = cc_sum / cc_cap_min;
693 cc_cap_res = cc_cap_temp % 100;
695 RICOH_FG_DBG("PMU: cc_sum = %d: cc_cap_res= %d: \n", cc_sum, cc_cap_res);
699 info->soca->cc_cap_offset += cc_cap_res;
700 if (info->soca->cc_cap_offset >= 100) {
702 info->soca->cc_cap_offset %= 100;
705 info->soca->cc_cap_offset -= cc_cap_res;
706 if (info->soca->cc_cap_offset <= -100) {
708 info->soca->cc_cap_offset %= 100;
711 RICOH_FG_DBG("PMU: cc_cap_offset= %d: \n", info->soca->cc_cap_offset);
713 //////////////////////////////////////////////////////////////////
716 dev_err(info->dev, "Error !!-----\n");
720 * Calculate target using capacity
722 static int get_target_use_cap(struct ricoh619_battery_info *info)
727 // int Target_Cutoff_Vol = 0;
743 /* get const value */
744 Ibat_min = -1 * info->soca->target_ibat;
745 if (info->soca->Ibat_ave > Ibat_min) /* I bat is minus */
749 Ibat_now = info->soca->Ibat_ave;
751 fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
753 use_cap = fa_cap - info->soca->re_cap_old;
755 /* get OCV table % */
756 for (i = 0; i <= 10; i = i+1) {
757 temp = (battery_init_para[info->num][i*2]<<8)
758 | (battery_init_para[info->num][i*2+1]);
759 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
760 temp = ((temp * 50000 * 10 / 4095) + 5) / 10;
762 RICOH_FG_DBG("PMU : %s : ocv_table %d is %d v\n",__func__, i, ocv_table[i]);
765 /* Find out Current OCV */
766 i = info->soca->soc/1000;
767 j = info->soca->soc - info->soca->soc/1000*1000;
768 Ocv_now_table = ocv_table[i]*100+(ocv_table[i+1]-ocv_table[i])*j/10;
770 Rsys_now = (info->soca->Vsys_ave - Ocv_now_table) / info->soca->Ibat_ave;
772 Ocv_ZeroPer_now = info->soca->target_vsys * 1000 - Ibat_now * Rsys_now;
774 RICOH_FG_DBG("PMU: ------- Ocv_now_table= %d: Rsys_now= %d =======\n",
775 Ocv_now_table, Rsys_now);
777 RICOH_FG_DBG("PMU: ------- Rsys= %d: cutoff_ocv= %d: Ocv_ZeroPer_now= %d =======\n",
778 info->soca->Rsys, info->soca->cutoff_ocv, Ocv_ZeroPer_now);
783 for (i = 1; i < 11; i++) {
784 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));
785 if (ocv_table[i] >= Ocv_ZeroPer_now / 100) {
787 start_per = Calc_Linear_Interpolation(
788 (i-1)*1000, ocv_table[i-1], i*1000,
789 ocv_table[i], (Ocv_ZeroPer_now / 100));
794 start_per = max(0, start_per);
796 FA_CAP_now = fa_cap * ((10000 - start_per) / 100 ) / 100;
798 RICOH_FG_DBG("PMU: ------- Target_Cutoff_Vol= %d: Ocv_ZeroPer_now= %d: start_per= %d =======\n",
799 Target_Cutoff_Vol, Ocv_ZeroPer_now, start_per);
802 RE_CAP_now = FA_CAP_now - use_cap;
804 if (RE_CAP_now < RE_CAP_GO_DOWN) {
805 info->soca->hurry_up_flg = 1;
806 } else if (info->soca->Vsys_ave < info->soca->target_vsys*1000) {
807 info->soca->hurry_up_flg = 1;
808 } else if (info->fg_poff_vbat != 0) {
809 if (info->soca->Vbat_ave < info->fg_poff_vbat*1000) {
810 info->soca->hurry_up_flg = 1;
812 info->soca->hurry_up_flg = 0;
815 info->soca->hurry_up_flg = 0;
818 /* get CC_OnePer_step */
819 if (info->soca->displayed_soc > 0) { /* avoid divide-by-0 */
820 CC_OnePer_step = RE_CAP_now / (info->soca->displayed_soc / 100 + 1);
824 /* get info->soca->target_use_cap */
825 info->soca->target_use_cap = use_cap + CC_OnePer_step;
827 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",
828 FA_CAP_now, RE_CAP_now, CC_OnePer_step, info->soca->target_use_cap, info->soca->hurry_up_flg);
832 #ifdef ENABLE_OCV_TABLE_CALIB
834 * Calibration OCV Table
835 * - Update the value of VBAT on 100% in OCV table
836 * if battery is Full charged.
837 * - int vbat_ocv <- unit is uV
839 static int calib_ocvTable(struct ricoh619_battery_info *info, int vbat_ocv)
848 RICOH_FG_DBG("PMU: %s\n", __func__);
850 if (info->soca->Ibat_ave > RICOH619_REL1_SEL_VALUE) {
851 RICOH_FG_DBG("PMU: %s IBAT > 64mA -- Not Calibration --\n", __func__);
855 if (vbat_ocv < info->soca->OCV100_max) {
856 if (vbat_ocv < info->soca->OCV100_min)
857 ocv100_new = info->soca->OCV100_min;
859 ocv100_new = vbat_ocv;
861 ocv100_new = info->soca->OCV100_max;
863 RICOH_FG_DBG("PMU : %s :max %d min %d current %d\n",__func__,info->soca->OCV100_max,info->soca->OCV100_min,vbat_ocv);
864 RICOH_FG_DBG("PMU : %s : New OCV 100% = 0x%x\n",__func__,ocv100_new);
867 ret = ricoh619_clr_bits(info->dev->parent, FG_CTRL_REG, 0x01);
869 dev_err("PMU: %s Error in FG_En OFF\n", __func__);
874 //cutoff_ocv = (battery_init_para[info->num][0]<<8) | (battery_init_para[info->num][1]);
875 cutoff_ocv = get_OCV_voltage(info, 0);
877 info->soca->ocv_table_def[10] = info->soca->OCV100_max;
879 ricoh619_scaling_OCV_table(info, cutoff_ocv/1000, ocv100_new/1000, &start_per, &end_per);
881 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
882 BAT_INIT_TOP_REG, 22, battery_init_para[info->num]);
884 dev_err(info->dev, "batterry initialize error\n");
888 for (i = 0; i <= 10; i = i+1) {
889 info->soca->ocv_table[i] = get_OCV_voltage(info, i);
890 RICOH_FG_DBG("PMU: %s : * %d0%% voltage = %d uV\n",
891 __func__, i, info->soca->ocv_table[i]);
894 /* FG_En on & Reset*/
895 ret = reset_FG_process(info);
897 dev_err("PMU: %s Error in FG_En On & Reset\n", __func__);
901 RICOH_FG_DBG("PMU: %s Exit \n", __func__);
909 static void ricoh619_displayed_work(struct work_struct *work)
917 int displayed_soc_temp;
920 bool is_charging = true;
921 int re_cap,fa_cap,use_cap;
922 bool is_jeita_updated;
931 struct ricoh619_battery_info *info = container_of(work,
932 struct ricoh619_battery_info, displayed_work.work);
934 if (info->entry_factory_mode) {
935 info->soca->status = RICOH619_SOCA_STABLE;
936 info->soca->displayed_soc = -EINVAL;
937 info->soca->ready_fg = 0;
941 mutex_lock(&info->lock);
943 is_jeita_updated = false;
945 if ((RICOH619_SOCA_START == info->soca->status)
946 || (RICOH619_SOCA_STABLE == info->soca->status)
947 || (RICOH619_SOCA_FULL == info->soca->status))
948 info->soca->ready_fg = 1;
950 /* judge Full state or Moni Vsys state */
951 if ((RICOH619_SOCA_DISP == info->soca->status)
952 || (RICOH619_SOCA_STABLE == info->soca->status))
955 temp_ocv = get_OCV_voltage(info, 10) -
956 (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))/2;
958 if(g_full_flag == 1){ /* for issue 1 solution start*/
959 info->soca->status = RICOH619_SOCA_FULL;
960 }else if ((POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
961 && (calc_ocv(info) > temp_ocv)) {
962 info->soca->status = RICOH619_SOCA_FULL;
964 } else if (info->soca->Ibat_ave >= -20) {
965 /* for issue1 solution end */
966 /* check Full state or not*/
967 if ((calc_ocv(info) > (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*7/10))
968 || (POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
969 || (info->soca->displayed_soc > 9850))
971 info->soca->status = RICOH619_SOCA_FULL;
973 } else if ((calc_ocv(info) > (get_OCV_voltage(info, 9)))
974 && (info->soca->Ibat_ave < 300))
976 info->soca->status = RICOH619_SOCA_FULL;
979 } else { /* dis-charging */
980 if (info->soca->displayed_soc/100 < RICOH619_ENTER_LOW_VOL) {
981 info->soca->target_use_cap = 0;
982 info->soca->status = RICOH619_SOCA_LOW_VOL;
987 if (RICOH619_SOCA_STABLE == info->soca->status) {
988 info->soca->soc = calc_capacity_2(info);
989 info->soca->soc_delta = info->soca->soc - info->soca->last_soc;
991 if (info->soca->soc_delta >= -100 && info->soca->soc_delta <= 100) {
992 info->soca->displayed_soc = info->soca->soc;
994 info->soca->status = RICOH619_SOCA_DISP;
996 info->soca->last_soc = info->soca->soc;
997 info->soca->soc_delta = 0;
998 } else if (RICOH619_SOCA_FULL == info->soca->status) {
999 err = check_jeita_status(info, &is_jeita_updated);
1001 dev_err(info->dev, "Error in updating JEITA %d\n", err);
1004 info->soca->soc = calc_capacity(info) * 100;
1005 info->soca->last_soc = calc_capacity_2(info); /* for DISP */
1007 if (info->soca->Ibat_ave >= -20) { /* charging */
1008 if (0 == info->soca->jt_limit) {
1009 if (g_full_flag == 1) {
1011 if (POWER_SUPPLY_STATUS_FULL == info->soca->chg_status) {
1012 if(info->soca->full_reset_count < RICOH619_UPDATE_COUNT_FULL_RESET) {
1013 info->soca->full_reset_count++;
1014 } else if (info->soca->full_reset_count < (RICOH619_UPDATE_COUNT_FULL_RESET + 1)) {
1015 err = reset_FG_process(info);
1017 dev_err(info->dev, "Error in writing the control register\n");
1018 info->soca->full_reset_count++;
1020 } else if(info->soca->full_reset_count < (RICOH619_UPDATE_COUNT_FULL_RESET + 2)) {
1021 info->soca->full_reset_count++;
1022 info->soca->fc_cap = 0;
1023 info->soca->soc_full = info->soca->soc;
1026 if(info->soca->fc_cap < -1 * 200) {
1028 info->soca->displayed_soc = 99 * 100;
1030 info->soca->full_reset_count = 0;
1034 info->soca->chg_cmp_times = 0;
1035 err = calc_capacity_in_period(info, &cc_cap, &is_charging, true);
1037 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1039 fc_delta = (is_charging == true) ? cc_cap : -cc_cap;
1041 info->soca->fc_cap = info->soca->fc_cap + fc_delta;
1043 if (g_full_flag == 1){
1044 info->soca->displayed_soc = 100*100;
1047 if (calc_ocv(info) < (get_OCV_voltage(info, 8))) { /* fail safe*/
1049 info->soca->status = RICOH619_SOCA_DISP;
1050 info->soca->soc_delta = 0;
1051 } else if ((POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
1052 && (info->soca->displayed_soc >= 9890)){
1053 if(info->soca->chg_cmp_times > RICOH619_FULL_WAIT_TIME) {
1054 info->soca->displayed_soc = 100*100;
1056 info->soca->full_reset_count = 0;
1057 info->soca->soc_full = info->soca->soc;
1058 info->soca->fc_cap = 0;
1059 #ifdef ENABLE_OCV_TABLE_CALIB
1060 err = calib_ocvTable(info,calc_ocv(info));
1062 dev_err(info->dev, "Calibration OCV Error !!\n");
1065 info->soca->chg_cmp_times++;
1068 fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
1071 if (info->soca->displayed_soc >= 9950) {
1072 if((info->soca->soc_full - info->soca->soc) < 200) {
1076 info->soca->chg_cmp_times = 0;
1078 err = calc_capacity_in_period(info, &cc_cap, &is_charging, true);
1080 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1081 info->soca->cc_delta
1082 = (is_charging == true) ? cc_cap : -cc_cap;
1084 if((POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
1085 // || (info->soca->Ibat_ave > 200))
1086 || (info->soca->Ibat_ave < info->ch_icchg*50 + 100) || (info->soca->displayed_soc<9700))
1088 info->soca->displayed_soc += 13 * 3000 / fa_cap;
1091 info->soca->displayed_soc
1092 = info->soca->displayed_soc + info->soca->cc_delta*8/10;
1095 info->soca->displayed_soc
1096 = min(10000, info->soca->displayed_soc);
1097 info->soca->displayed_soc = max(0, info->soca->displayed_soc);
1099 if (info->soca->displayed_soc >= 9890) {
1100 info->soca->displayed_soc = 99 * 100;
1105 info->soca->full_reset_count = 0;
1107 } else { /* discharging */
1108 if (info->soca->displayed_soc >= 9950) {
1109 if (info->soca->Ibat_ave <= -1 * RICOH619_REL1_SEL_VALUE) {
1110 if ((calc_ocv(info) < (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*3/10))
1111 || ((info->soca->soc_full - info->soca->soc) > 200)) {
1114 info->soca->full_reset_count = 0;
1115 info->soca->displayed_soc = 100 * 100;
1116 info->soca->status = RICOH619_SOCA_DISP;
1117 info->soca->last_soc = info->soca->soc;
1118 info->soca->soc_delta = 0;
1120 info->soca->displayed_soc = 100 * 100;
1122 } else { /* into relaxation state */
1123 ricoh619_read(info->dev->parent, CHGSTATE_REG, ®_val);
1124 if (reg_val & 0xc0) {
1125 info->soca->displayed_soc = 100 * 100;
1128 info->soca->full_reset_count = 0;
1129 info->soca->displayed_soc = 100 * 100;
1130 info->soca->status = RICOH619_SOCA_DISP;
1131 info->soca->last_soc = info->soca->soc;
1132 info->soca->soc_delta = 0;
1137 info->soca->status = RICOH619_SOCA_DISP;
1138 info->soca->soc_delta = 0;
1139 info->soca->full_reset_count = 0;
1140 info->soca->last_soc = info->soca->soc;
1143 } else if (RICOH619_SOCA_LOW_VOL == info->soca->status) {
1144 if(info->soca->Ibat_ave >= 0) {
1145 info->soca->soc = calc_capacity(info) * 100;
1146 info->soca->status = RICOH619_SOCA_DISP;
1147 info->soca->last_soc = info->soca->soc;
1148 info->soca->soc_delta = 0;
1150 re_cap = get_check_fuel_gauge_reg(info, RE_CAP_H_REG, RE_CAP_L_REG,
1152 fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
1154 use_cap = fa_cap - re_cap;
1156 if (info->soca->target_use_cap == 0) {
1157 info->soca->re_cap_old = re_cap;
1158 get_target_use_cap(info);
1161 if(use_cap >= info->soca->target_use_cap) {
1162 info->soca->displayed_soc = info->soca->displayed_soc - 100;
1163 info->soca->displayed_soc = max(0, info->soca->displayed_soc);
1164 info->soca->re_cap_old = re_cap;
1165 } else if (info->soca->hurry_up_flg == 1) {
1166 info->soca->displayed_soc = info->soca->displayed_soc - 100;
1167 info->soca->displayed_soc = max(0, info->soca->displayed_soc);
1168 info->soca->re_cap_old = re_cap;
1170 get_target_use_cap(info);
1171 info->soca->soc = calc_capacity(info) * 100;
1174 if (RICOH619_SOCA_DISP == info->soca->status) {
1176 info->soca->soc = calc_capacity_2(info);
1178 soc_round = (info->soca->soc + 50) / 100;
1179 last_soc_round = (info->soca->last_soc + 50) / 100;
1180 last_disp_round = (info->soca->displayed_soc + 50) / 100;
1182 info->soca->soc_delta =
1183 info->soca->soc_delta + (info->soca->soc - info->soca->last_soc);
1185 info->soca->last_soc = info->soca->soc;
1187 if (last_disp_round == soc_round) {
1188 /* if SOC == DISPLAY move to stable */
1189 info->soca->displayed_soc = info->soca->soc ;
1190 info->soca->status = RICOH619_SOCA_STABLE;
1192 } else if (info->soca->Ibat_ave > 0) {
1193 if ((0 == info->soca->jt_limit) ||
1194 (POWER_SUPPLY_STATUS_FULL != info->soca->chg_status)) {
1196 if (last_disp_round < soc_round) {
1197 /* Case 1 : Charge, Display < SOC */
1198 if (info->soca->soc_delta >= 100) {
1199 info->soca->displayed_soc
1200 = last_disp_round * 100 + 50;
1201 info->soca->soc_delta -= 100;
1202 if (info->soca->soc_delta >= 100)
1205 info->soca->displayed_soc += 25;
1206 disp_dec = info->soca->displayed_soc % 100;
1207 if ((50 <= disp_dec) && (disp_dec <= 74))
1208 info->soca->soc_delta = 0;
1210 if ((info->soca->displayed_soc + 50)/100
1212 info->soca->displayed_soc
1215 = RICOH619_SOCA_STABLE;
1218 } else if (last_disp_round > soc_round) {
1219 /* Case 2 : Charge, Display > SOC */
1220 if (info->soca->soc_delta >= 300) {
1221 info->soca->displayed_soc += 100;
1222 info->soca->soc_delta -= 300;
1224 if ((info->soca->displayed_soc + 50)/100
1226 info->soca->displayed_soc
1229 = RICOH619_SOCA_STABLE;
1234 info->soca->soc_delta = 0;
1238 if (last_disp_round > soc_round) {
1239 /* Case 3 : Dis-Charge, Display > SOC */
1240 if (info->soca->soc_delta <= -100) {
1241 info->soca->displayed_soc
1242 = last_disp_round * 100 - 75;
1243 info->soca->soc_delta += 100;
1244 if (info->soca->soc_delta <= -100)
1247 info->soca->displayed_soc -= 25;
1248 disp_dec = info->soca->displayed_soc % 100;
1249 if ((25 <= disp_dec) && (disp_dec <= 49))
1250 info->soca->soc_delta = 0;
1252 if ((info->soca->displayed_soc + 50)/100
1254 info->soca->displayed_soc
1257 = RICOH619_SOCA_STABLE;
1260 } else if (last_disp_round < soc_round) {
1261 /* Case 4 : Dis-Charge, Display < SOC */
1262 if (info->soca->soc_delta <= -300) {
1263 info->soca->displayed_soc -= 100;
1264 info->soca->soc_delta += 300;
1266 if ((info->soca->displayed_soc + 50)/100
1268 info->soca->displayed_soc
1271 = RICOH619_SOCA_STABLE;
1276 } else if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
1278 temp_ocv = get_OCV_voltage(info, 10) -
1279 (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))/2;
1281 if(g_full_flag == 1){ /* for issue 1 solution start*/
1282 info->soca->status = RICOH619_SOCA_FULL;
1283 err = reset_FG_process(info);
1285 dev_err(info->dev, "Error in writing the control register\n");
1288 }else if ((POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
1289 && (calc_ocv(info) > temp_ocv)) {
1290 info->soca->status = RICOH619_SOCA_FULL;
1292 err = reset_FG_process(info);
1294 dev_err(info->dev, "Error in writing the control register\n");
1296 } else if (info->soca->Ibat_ave >= -20) {
1297 /* for issue1 solution end */
1298 /* check Full state or not*/
1299 if ((calc_ocv(info) > (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*7/10))
1300 || (POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
1301 || (info->soca->displayed_soc > 9850))
1303 info->soca->status = RICOH619_SOCA_FULL;
1305 err = reset_FG_process(info);
1307 dev_err(info->dev, "Error in writing the control register\n");
1309 } else if ((calc_ocv(info) > (get_OCV_voltage(info, 9)))
1310 && (info->soca->Ibat_ave < 300))
1312 info->soca->status = RICOH619_SOCA_FULL;
1314 err = reset_FG_process(info);
1316 dev_err(info->dev, "Error in writing the control register\n");
1321 err = ricoh619_read(info->dev->parent, PSWR_REG, &val);
1323 info->soca->soc = val * 100;
1326 "Error in reading PSWR_REG %d\n", err);
1328 = calc_capacity(info) * 100;
1331 err = calc_capacity_in_period(info, &cc_cap,
1332 &is_charging, false);
1334 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1336 info->soca->cc_delta
1337 = (is_charging == true) ? cc_cap : -cc_cap;
1340 = info->soca->soc + info->soca->cc_delta;
1341 if (displayed_soc_temp < 0)
1342 displayed_soc_temp = 0;
1344 = min(9850, displayed_soc_temp);
1345 displayed_soc_temp = max(0, displayed_soc_temp);
1347 info->soca->displayed_soc = displayed_soc_temp;
1349 } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
1351 } else if (RICOH619_SOCA_START == info->soca->status) {
1353 err = measure_Ibatt_FG(info, &Ibat);
1354 err = measure_vbatt_FG(info, &Vbat);
1355 err = measure_vsys_ADC(info, &Vsys);
1357 info->soca->Ibat_ave = Ibat;
1358 info->soca->Vbat_ave = Vbat;
1359 info->soca->Vsys_ave = Vsys;
1361 err = check_jeita_status(info, &is_jeita_updated);
1362 is_jeita_updated = false;
1364 dev_err(info->dev, "Error in updating JEITA %d\n", err);
1366 err = ricoh619_read(info->dev->parent, PSWR_REG, &val);
1368 if (info->first_pwon) {
1369 info->soca->soc = calc_capacity(info) * 100;
1370 val = (info->soca->soc + 50)/100;
1372 err = ricoh619_write(info->dev->parent, PSWR_REG, val);
1374 dev_err(info->dev, "Error in writing PSWR_REG\n");
1377 if ((info->soca->soc == 0) && (calc_ocv(info)
1378 < get_OCV_voltage(info, 0))) {
1379 info->soca->displayed_soc = 0;
1380 info->soca->status = RICOH619_SOCA_ZERO;
1382 if (0 == info->soca->jt_limit) {
1383 check_charge_status_2(info, info->soca->soc);
1385 info->soca->displayed_soc = info->soca->soc;
1388 if (info->soca->displayed_soc < 300) {
1389 info->soca->target_use_cap = 0;
1390 info->soca->status = RICOH619_SOCA_LOW_VOL;
1392 if ((info->fg_poff_vbat != 0)
1393 && (Vbat < info->fg_poff_vbat * 1000) ){
1394 info->soca->target_use_cap = 0;
1395 info->soca->status = RICOH619_SOCA_LOW_VOL;
1397 info->soca->status = RICOH619_SOCA_UNSTABLE;
1401 info->soca->status = RICOH619_SOCA_UNSTABLE;
1404 } else if (g_fg_on_mode && (val == 0x7f)) {
1405 info->soca->soc = calc_capacity(info) * 100;
1406 if ((info->soca->soc == 0) && (calc_ocv(info)
1407 < get_OCV_voltage(info, 0))) {
1408 info->soca->displayed_soc = 0;
1409 info->soca->status = RICOH619_SOCA_ZERO;
1411 if (0 == info->soca->jt_limit) {
1412 check_charge_status_2(info, info->soca->soc);
1414 info->soca->displayed_soc = info->soca->soc;
1416 info->soca->last_soc = info->soca->soc;
1417 info->soca->status = RICOH619_SOCA_STABLE;
1420 info->soca->soc = val * 100;
1423 "Error in reading PSWR_REG %d\n", err);
1425 = calc_capacity(info) * 100;
1428 err = calc_capacity_in_period(info, &cc_cap,
1429 &is_charging, false);
1431 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1433 info->soca->cc_delta
1434 = (is_charging == true) ? cc_cap : -cc_cap;
1435 if (calc_ocv(info) < get_OCV_voltage(info, 0)) {
1436 info->soca->displayed_soc = 0;
1437 info->soca->status = RICOH619_SOCA_ZERO;
1440 = info->soca->soc + info->soca->cc_delta;
1441 if (displayed_soc_temp < 0)
1442 displayed_soc_temp = 0;
1444 = min(10000, displayed_soc_temp);
1445 displayed_soc_temp = max(0, displayed_soc_temp);
1446 if (0 == info->soca->jt_limit) {
1447 check_charge_status_2(info, displayed_soc_temp);
1449 info->soca->displayed_soc = displayed_soc_temp;
1451 info->soca->last_soc = calc_capacity(info) * 100;
1453 if (info->soca->displayed_soc < 300) {
1454 info->soca->target_use_cap = 0;
1455 info->soca->status = RICOH619_SOCA_LOW_VOL;
1457 if ((info->fg_poff_vbat != 0)
1458 && (Vbat < info->fg_poff_vbat * 1000)){
1459 info->soca->target_use_cap = 0;
1460 info->soca->status = RICOH619_SOCA_LOW_VOL;
1462 info->soca->status = RICOH619_SOCA_UNSTABLE;
1466 if(info->soca->displayed_soc >= 9850)
1468 info->soca->displayed_soc = 10000;
1469 info->chg_complete_tm_ov_flag = 1;
1471 info->soca->status = RICOH619_SOCA_UNSTABLE;
1475 } else if (RICOH619_SOCA_ZERO == info->soca->status) {
1476 if (calc_ocv(info) > get_OCV_voltage(info, 0)) {
1477 err = reset_FG_process(info);
1479 dev_err(info->dev, "Error in writing the control register\n");
1480 info->soca->last_soc = calc_capacity_2(info);
1481 info->soca->status = RICOH619_SOCA_STABLE;
1483 info->soca->displayed_soc = 0;
1486 /* keep DSOC = 1 when Vbat is over 3.4V*/
1487 if( info->fg_poff_vbat != 0) {
1488 if (info->soca->zero_flg == 1) {
1489 if(info->soca->Ibat_ave >= 0) {
1490 info->soca->zero_flg = 0;
1492 info->soca->displayed_soc = 0;
1493 } else if (info->soca->displayed_soc < 50) {
1494 if (info->soca->Vbat_ave < 2000*1000) { /* error value */
1495 info->soca->displayed_soc = 100;
1496 } else if (info->soca->Vbat_ave < info->fg_poff_vbat*1000) {
1497 info->soca->displayed_soc = 0;
1498 info->soca->zero_flg = 1;
1500 info->soca->displayed_soc = 100;
1506 && (info->soca->status == RICOH619_SOCA_STABLE)) {
1507 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
1509 dev_err(info->dev, "Error in writing PSWR_REG\n");
1511 err = calc_capacity_in_period(info, &cc_cap,
1512 &is_charging, true);
1514 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1516 } else if (RICOH619_SOCA_UNSTABLE != info->soca->status) {
1517 if ((info->soca->displayed_soc + 50) / 100 <= 1) {
1520 val = (info->soca->displayed_soc + 50)/100;
1523 err = ricoh619_write(info->dev->parent, PSWR_REG, val);
1525 dev_err(info->dev, "Error in writing PSWR_REG\n");
1529 err = calc_capacity_in_period(info, &cc_cap,
1530 &is_charging, true);
1532 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1535 RICOH_FG_DBG("PMU: ------- STATUS= %d: IBAT= %d: VSYS= %d: VBAT= %d: DSOC= %d: RSOC= %d: -------\n",
1536 info->soca->status, info->soca->Ibat_ave, info->soca->Vsys_ave, info->soca->Vbat_ave,
1537 info->soca->displayed_soc, info->soca->soc);
1539 #ifdef DISABLE_CHARGER_TIMER
1540 /* clear charger timer */
1541 if ( info->soca->chg_status == POWER_SUPPLY_STATUS_CHARGING ) {
1542 err = ricoh619_read(info->dev->parent, TIMSET_REG, &val);
1545 "Error in read TIMSET_REG%d\n", err);
1546 /* to check bit 0-1 */
1550 /* set rapid timer 240 -> 300 */
1551 err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x03);
1553 dev_err(info->dev, "Error in writing the control register\n");
1556 /* set rapid timer 300 -> 240 */
1557 err = ricoh619_clr_bits(info->dev->parent, TIMSET_REG, 0x01);
1558 err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x02);
1560 dev_err(info->dev, "Error in writing the control register\n");
1566 if (0 == info->soca->ready_fg)
1567 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1568 RICOH619_FG_RESET_TIME * HZ);
1569 else if (delay_flag == 1)
1570 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1571 RICOH619_DELAY_TIME * HZ);
1572 else if (RICOH619_SOCA_DISP == info->soca->status)
1573 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1574 RICOH619_DISPLAY_UPDATE_TIME * HZ);
1575 else if (info->soca->hurry_up_flg == 1)
1576 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1577 RICOH619_LOW_VOL_DOWN_TIME * HZ);
1579 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1580 RICOH619_DISPLAY_UPDATE_TIME * HZ);
1582 mutex_unlock(&info->lock);
1584 if((true == is_jeita_updated)
1585 || (info->soca->last_displayed_soc/100 != (info->soca->displayed_soc+50)/100))
1586 power_supply_changed(&info->battery);
1588 info->soca->last_displayed_soc = info->soca->displayed_soc+50;
1590 if((info->soca->displayed_soc >= 9850) && (info->soca->Ibat_ave > -20) && (info->capacity < 100))
1592 if(info->chg_complete_rd_flag == 0)
1594 info->chg_complete_rd_flag = 1;
1595 info->chg_complete_rd_cnt = 0;
1596 queue_delayed_work(info->monitor_wqueue, &info->charge_complete_ready, 0);
1601 info->chg_complete_rd_flag = 0;
1604 if(info->chg_complete_tm_ov_flag == 1)
1606 if(info->soca->displayed_soc < 9850 || info->soca->Ibat_ave < -20)
1608 info->chg_complete_tm_ov_flag = 0;
1609 power_supply_changed(&info->battery);
1615 static void ricoh619_stable_charge_countdown_work(struct work_struct *work)
1621 struct ricoh619_battery_info *info = container_of(work,
1622 struct ricoh619_battery_info, charge_stable_work.work);
1624 if (info->entry_factory_mode)
1627 mutex_lock(&info->lock);
1628 if (RICOH619_SOCA_FG_RESET == info->soca->status)
1629 info->soca->ready_fg = 1;
1631 if (2 <= info->soca->stable_count) {
1632 if (3 == info->soca->stable_count
1633 && RICOH619_SOCA_FG_RESET == info->soca->status) {
1634 ret = reset_FG_process(info);
1636 dev_err(info->dev, "Error in writing the control register\n");
1638 info->soca->stable_count = info->soca->stable_count - 1;
1639 queue_delayed_work(info->monitor_wqueue,
1640 &info->charge_stable_work,
1641 RICOH619_FG_STABLE_TIME * HZ / 10);
1642 } else if (0 >= info->soca->stable_count) {
1643 /* Finished queue, ignore */
1644 } else if (1 == info->soca->stable_count) {
1645 if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
1646 /* Judge if FG need reset or Not */
1647 info->soca->soc = calc_capacity(info) * 100;
1648 if (info->chg_ctr != 0) {
1649 queue_delayed_work(info->monitor_wqueue,
1650 &info->charge_stable_work,
1651 RICOH619_FG_STABLE_TIME * HZ / 10);
1652 mutex_unlock(&info->lock);
1655 /* Do reset setting */
1656 ret = reset_FG_process(info);
1658 dev_err(info->dev, "Error in writing the control register\n");
1660 info->soca->status = RICOH619_SOCA_FG_RESET;
1662 /* Delay for addition Reset Time (6s) */
1663 queue_delayed_work(info->monitor_wqueue,
1664 &info->charge_stable_work,
1665 RICOH619_FG_RESET_TIME*HZ);
1666 } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
1667 info->soca->reset_soc[2] = info->soca->reset_soc[1];
1668 info->soca->reset_soc[1] = info->soca->reset_soc[0];
1669 info->soca->reset_soc[0] = calc_capacity(info) * 100;
1670 info->soca->reset_count++;
1672 if (info->soca->reset_count > 10) {
1673 /* Reset finished; */
1674 info->soca->soc = info->soca->reset_soc[0];
1675 info->soca->stable_count = 0;
1679 for (i = 0; i < 3; i++) {
1680 if (max < info->soca->reset_soc[i]/100)
1681 max = info->soca->reset_soc[i]/100;
1682 if (min > info->soca->reset_soc[i]/100)
1683 min = info->soca->reset_soc[i]/100;
1686 if ((info->soca->reset_count > 3) && ((max - min)
1687 < RICOH619_MAX_RESET_SOC_DIFF)) {
1688 /* Reset finished; */
1689 info->soca->soc = info->soca->reset_soc[0];
1690 info->soca->stable_count = 0;
1693 /* Do reset setting */
1694 ret = reset_FG_process(info);
1696 dev_err(info->dev, "Error in writing the control register\n");
1698 /* Delay for addition Reset Time (6s) */
1699 queue_delayed_work(info->monitor_wqueue,
1700 &info->charge_stable_work,
1701 RICOH619_FG_RESET_TIME*HZ);
1703 /* Finished queue From now, select FG as result; */
1704 } else if (RICOH619_SOCA_START == info->soca->status) {
1705 /* Normal condition */
1706 } else { /* other state ZERO/DISP/STABLE */
1707 info->soca->stable_count = 0;
1710 mutex_unlock(&info->lock);
1714 info->soca->last_soc = info->soca->soc;
1715 info->soca->status = RICOH619_SOCA_DISP;
1716 info->soca->soc_delta = 0;
1719 mutex_unlock(&info->lock);
1723 static void ricoh619_charge_monitor_work(struct work_struct *work)
1725 struct ricoh619_battery_info *info = container_of(work,
1726 struct ricoh619_battery_info, charge_monitor_work.work);
1728 get_power_supply_status(info);
1730 if (POWER_SUPPLY_STATUS_DISCHARGING == info->soca->chg_status
1731 || POWER_SUPPLY_STATUS_NOT_CHARGING == info->soca->chg_status) {
1732 switch (info->soca->dischg_state) {
1734 info->soca->dischg_state = 1;
1737 info->soca->dischg_state = 2;
1745 info->soca->dischg_state = 0;
1748 queue_delayed_work(info->monitor_wqueue, &info->charge_monitor_work,
1749 RICOH619_CHARGE_MONITOR_TIME * HZ);
1754 static void ricoh619_get_charge_work(struct work_struct *work)
1756 struct ricoh619_battery_info *info = container_of(work,
1757 struct ricoh619_battery_info, get_charge_work.work);
1759 int Vbat_temp, Vsys_temp, Ibat_temp;
1760 int Vbat_sort[RICOH619_GET_CHARGE_NUM];
1761 int Vsys_sort[RICOH619_GET_CHARGE_NUM];
1762 int Ibat_sort[RICOH619_GET_CHARGE_NUM];
1766 mutex_lock(&info->lock);
1768 for (i = RICOH619_GET_CHARGE_NUM-1; i > 0; i--) {
1769 if (0 == info->soca->chg_count) {
1770 info->soca->Vbat[i] = 0;
1771 info->soca->Vsys[i] = 0;
1772 info->soca->Ibat[i] = 0;
1774 info->soca->Vbat[i] = info->soca->Vbat[i-1];
1775 info->soca->Vsys[i] = info->soca->Vsys[i-1];
1776 info->soca->Ibat[i] = info->soca->Ibat[i-1];
1780 ret = measure_vbatt_FG(info, &info->soca->Vbat[0]);
1781 ret = measure_vsys_ADC(info, &info->soca->Vsys[0]);
1782 ret = measure_Ibatt_FG(info, &info->soca->Ibat[0]);
1784 info->soca->chg_count++;
1786 if (RICOH619_GET_CHARGE_NUM != info->soca->chg_count) {
1787 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1788 RICOH619_CHARGE_CALC_TIME * HZ);
1789 mutex_unlock(&info->lock);
1793 for (i = 0; i < RICOH619_GET_CHARGE_NUM; i++) {
1794 Vbat_sort[i] = info->soca->Vbat[i];
1795 Vsys_sort[i] = info->soca->Vsys[i];
1796 Ibat_sort[i] = info->soca->Ibat[i];
1802 for (i = 0; i < RICOH619_GET_CHARGE_NUM - 1; i++) {
1803 for (j = RICOH619_GET_CHARGE_NUM - 1; j > i; j--) {
1804 if (Vbat_sort[j - 1] > Vbat_sort[j]) {
1805 Vbat_temp = Vbat_sort[j];
1806 Vbat_sort[j] = Vbat_sort[j - 1];
1807 Vbat_sort[j - 1] = Vbat_temp;
1809 if (Vsys_sort[j - 1] > Vsys_sort[j]) {
1810 Vsys_temp = Vsys_sort[j];
1811 Vsys_sort[j] = Vsys_sort[j - 1];
1812 Vsys_sort[j - 1] = Vsys_temp;
1814 if (Ibat_sort[j - 1] > Ibat_sort[j]) {
1815 Ibat_temp = Ibat_sort[j];
1816 Ibat_sort[j] = Ibat_sort[j - 1];
1817 Ibat_sort[j - 1] = Ibat_temp;
1825 for (i = 3; i < RICOH619_GET_CHARGE_NUM-3; i++) {
1826 Vbat_temp = Vbat_temp + Vbat_sort[i];
1827 Vsys_temp = Vsys_temp + Vsys_sort[i];
1828 Ibat_temp = Ibat_temp + Ibat_sort[i];
1830 Vbat_temp = Vbat_temp / (RICOH619_GET_CHARGE_NUM - 6);
1831 Vsys_temp = Vsys_temp / (RICOH619_GET_CHARGE_NUM - 6);
1832 Ibat_temp = Ibat_temp / (RICOH619_GET_CHARGE_NUM - 6);
1834 if (0 == info->soca->chg_count) {
1835 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1836 RICOH619_CHARGE_UPDATE_TIME * HZ);
1837 mutex_unlock(&info->lock);
1840 info->soca->Vbat_ave = Vbat_temp;
1841 info->soca->Vsys_ave = Vsys_temp;
1842 info->soca->Ibat_ave = Ibat_temp;
1845 info->soca->chg_count = 0;
1846 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1847 RICOH619_CHARGE_UPDATE_TIME * HZ);
1848 mutex_unlock(&info->lock);
1852 /* Initial setting of FuelGauge SOCA function */
1853 static int ricoh619_init_fgsoca(struct ricoh619_battery_info *info)
1859 bool is_charging = true;
1861 for (i = 0; i <= 10; i = i+1) {
1862 info->soca->ocv_table[i] = get_OCV_voltage(info, i);
1863 RICOH_FG_DBG("PMU: %s : * %d0%% voltage = %d uV\n",
1864 __func__, i, info->soca->ocv_table[i]);
1867 for (i = 0; i < 3; i = i+1)
1868 info->soca->reset_soc[i] = 0;
1869 info->soca->reset_count = 0;
1871 if (info->first_pwon) {
1873 err = ricoh619_read(info->dev->parent, CHGISET_REG, &val);
1876 "Error in read CHGISET_REG%d\n", err);
1878 err = ricoh619_write(info->dev->parent, CHGISET_REG, 0);
1881 "Error in writing CHGISET_REG%d\n", err);
1884 if (!info->entry_factory_mode) {
1885 err = ricoh619_write(info->dev->parent,
1888 dev_err(info->dev, "Error in writing the control register\n");
1891 err = calc_capacity_in_period(info, &cc_cap, &is_charging, true);
1895 err = ricoh619_write(info->dev->parent, CHGISET_REG, val);
1898 "Error in writing CHGISET_REG%d\n", err);
1901 /* Rbat : Transfer */
1902 info->soca->Rbat = get_OCV_init_Data(info, 12) * 1000 / 512
1904 info->soca->n_cap = get_OCV_init_Data(info, 11);
1907 info->soca->displayed_soc = 0;
1908 info->soca->last_displayed_soc = 0;
1909 info->soca->suspend_soc = 0;
1910 info->soca->ready_fg = 0;
1911 info->soca->soc_delta = 0;
1912 info->soca->full_reset_count = 0;
1913 info->soca->soc_full = 0;
1914 info->soca->fc_cap = 0;
1915 info->soca->status = RICOH619_SOCA_START;
1916 /* stable count down 11->2, 1: reset; 0: Finished; */
1917 info->soca->stable_count = 11;
1918 info->soca->chg_cmp_times = 0;
1919 info->soca->dischg_state = 0;
1920 info->soca->Vbat_ave = 0;
1921 info->soca->Vbat_old = 0;
1922 info->soca->Vsys_ave = 0;
1923 info->soca->Ibat_ave = 0;
1924 info->soca->chg_count = 0;
1925 info->soca->target_use_cap = 0;
1926 info->soca->hurry_up_flg = 0;
1927 info->soca->re_cap_old = 0;
1928 info->soca->jt_limit = 0;
1929 info->soca->zero_flg = 0;
1930 info->soca->cc_cap_offset = 0;
1932 for (i = 0; i < 11; i++) {
1933 info->soca->ocv_table_low[i] = 0;
1936 for (i = 0; i < RICOH619_GET_CHARGE_NUM; i++) {
1937 info->soca->Vbat[i] = 0;
1938 info->soca->Vsys[i] = 0;
1939 info->soca->Ibat[i] = 0;
1944 #ifdef ENABLE_FG_KEEP_ON_MODE
1950 /* Start first Display job */
1951 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1952 RICOH619_FG_RESET_TIME*HZ);
1954 /* Start first Waiting stable job */
1955 queue_delayed_work(info->monitor_wqueue, &info->charge_stable_work,
1956 RICOH619_FG_STABLE_TIME*HZ/10);
1958 queue_delayed_work(info->monitor_wqueue, &info->charge_monitor_work,
1959 RICOH619_CHARGE_MONITOR_TIME * HZ);
1961 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1962 RICOH619_CHARGE_MONITOR_TIME * HZ);
1964 if (info->jt_hw_sw) {
1965 /* Enable JEITA function supported by H/W */
1966 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x04);
1968 dev_err(info->dev, "Error in writing the control register\n");
1970 /* Disable JEITA function supported by H/W */
1971 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x04);
1973 dev_err(info->dev, "Error in writing the control register\n");
1974 queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
1975 RICOH619_FG_RESET_TIME * HZ);
1978 /* Disable JEITA function supported by H/W */
1979 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x04);
1981 dev_err(info->dev, "Error in writing the control register\n");
1984 RICOH_FG_DBG("PMU: %s : * Rbat = %d mOhm n_cap = %d mAH\n",
1985 __func__, info->soca->Rbat, info->soca->n_cap);
1990 static void ricoh619_charging_complete_work(struct work_struct *work)
1992 struct ricoh619_battery_info *info = container_of(work,
1993 struct ricoh619_battery_info, charge_complete_ready.work);
1995 uint8_t time_ov_flag;
1996 RICOH_FG_DBG("PMU: %s\n", __func__);
1997 RICOH_FG_DBG("info->chg_complete_rd_cnt = %d\n", info->chg_complete_rd_cnt);
1998 RICOH_FG_DBG("info->chg_complete_rd_flag = %d\n", info->chg_complete_rd_flag);
1999 RICOH_FG_DBG("info->chg_complete_tm_ov_flag = %d\n", info->chg_complete_tm_ov_flag);
2000 RICOH_FG_DBG("time_ov_flag = %d\n", time_ov_flag);
2002 if(info->chg_complete_rd_flag == 1)
2004 // start chg 99per to 100per timer
2006 info->chg_complete_rd_flag = 2;
2007 info->chg_complete_tm_ov_flag = 0;
2011 if(info->capacity == 100)
2013 // battery arriver to 100% earlier than time ov
2015 info->chg_complete_rd_cnt = 0;
2016 info->chg_complete_tm_ov_flag = 1;
2018 else if(info->chg_complete_rd_cnt > RICOH619_TIME_CHG_COUNT)
2020 // chg timer ov before cap arrive to 100%
2022 info->chg_complete_tm_ov_flag = 1;
2023 info->chg_complete_rd_cnt = 0;
2024 info->soca->status = RICOH619_SOCA_FULL;
2025 power_supply_changed(&info->battery);
2030 info->chg_complete_tm_ov_flag = 0;
2034 if(time_ov_flag == 0)
2036 info->chg_complete_rd_cnt++;
2037 queue_delayed_work(info->monitor_wqueue, &info->charge_complete_ready,
2038 RICOH619_TIME_CHG_STEP);
2045 RICOH_FG_DBG("PMU2: %s return\n", __func__);
2046 RICOH_FG_DBG("info->chg_complete_rd_cnt = %d\n", info->chg_complete_rd_cnt);
2047 RICOH_FG_DBG("info->chg_complete_rd_flag = %d\n", info->chg_complete_rd_flag);
2048 RICOH_FG_DBG("info->chg_complete_tm_ov_flag = %d\n", info->chg_complete_tm_ov_flag);
2049 RICOH_FG_DBG("time_ov_flag = %d\n", time_ov_flag);
2052 static void ricoh619_changed_work(struct work_struct *work)
2054 struct ricoh619_battery_info *info = container_of(work,
2055 struct ricoh619_battery_info, changed_work.work);
2057 RICOH_FG_DBG("PMU: %s\n", __func__);
2058 power_supply_changed(&info->battery);
2063 static int check_jeita_status(struct ricoh619_battery_info *info, bool *is_jeita_updated)
2064 /* JEITA Parameter settings
2068 * jt_vfchg_h~+~~~~~~~~~~~~~~~~~~~+
2070 * jt_vfchg_l-| - - - - - - - - - +~~~~~~~~~~+
2072 * -------0--+-------------------+----------+--- Temp
2076 * jt_ichg_h-+ - -+~~~~~~~~~~~~~~+~~~~~~~~~~+
2078 * jt_ichg_l-+~~~~+ Charge area |
2080 * 0--+----+--------------+----------+--- Temp
2081 * 0 jt_temp_l jt_temp_h 55
2087 uint8_t chgiset_org;
2088 uint8_t batset2_org;
2089 uint8_t set_vchg_h, set_vchg_l;
2090 uint8_t set_ichg_h, set_ichg_l;
2092 *is_jeita_updated = false;
2094 /* No execute if JEITA disabled */
2095 if (!info->jt_en || info->jt_hw_sw)
2098 /* Check FG Reset */
2099 if (info->soca->ready_fg) {
2100 temp = get_battery_temp_2(info) / 10;
2102 RICOH_FG_DBG(KERN_INFO "JEITA: %s *** cannot update by resetting FG ******\n", __func__);
2107 err = ricoh619_read(info->dev->parent, BATSET2_REG, &batset2_org);
2109 dev_err(info->dev, "Error in readng the battery setting register\n");
2112 vfchg = (batset2_org & 0x70) >> 4;
2113 batset2_org &= 0x8F;
2116 err = ricoh619_read(info->dev->parent, CHGISET_REG, &chgiset_org);
2118 dev_err(info->dev, "Error in readng the chrage setting register\n");
2121 chgiset_org &= 0xC0;
2123 set_ichg_h = (uint8_t)(chgiset_org | info->jt_ichg_h);
2124 set_ichg_l = (uint8_t)(chgiset_org | info->jt_ichg_l);
2126 set_vchg_h = (uint8_t)((info->jt_vfchg_h << 4) | batset2_org);
2127 set_vchg_l = (uint8_t)((info->jt_vfchg_l << 4) | batset2_org);
2129 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Temperature: %d, vfchg: %d, SW status: %d, chg_status: %d ******\n",
2130 __func__, temp, vfchg, info->soca->status, info->soca->chg_status);
2132 if (temp <= 0 || 55 <= temp) {
2133 /* 1st and 5th temperature ranges (~0, 55~) */
2134 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Temp(%d) is out of 0-55 ******\n", __func__, temp);
2135 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2137 dev_err(info->dev, "Error in writing the control register\n");
2140 info->soca->jt_limit = 0;
2141 *is_jeita_updated = true;
2142 } else if (temp < info->jt_temp_l) {
2143 /* 2nd temperature range (0~12) */
2144 if (vfchg != info->jt_vfchg_h) {
2145 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 0<Temp<12, update to vfchg=%d ******\n",
2146 __func__, info->jt_vfchg_h);
2147 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2149 dev_err(info->dev, "Error in writing the control register\n");
2153 /* set VFCHG/VRCHG */
2154 err = ricoh619_write(info->dev->parent,
2155 BATSET2_REG, set_vchg_h);
2157 dev_err(info->dev, "Error in writing the battery setting register\n");
2160 info->soca->jt_limit = 0;
2161 *is_jeita_updated = true;
2163 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 0<Temp<50, already set vfchg=%d, so no need to update ******\n",
2164 __func__, info->jt_vfchg_h);
2167 err = ricoh619_write(info->dev->parent, CHGISET_REG, set_ichg_l);
2169 dev_err(info->dev, "Error in writing the battery setting register\n");
2172 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2174 dev_err(info->dev, "Error in writing the control register\n");
2177 } else if (temp < info->jt_temp_h) {
2178 /* 3rd temperature range (12~50) */
2179 if (vfchg != info->jt_vfchg_h) {
2180 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 12<Temp<50, update to vfchg==%d ******\n", __func__, info->jt_vfchg_h);
2182 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2184 dev_err(info->dev, "Error in writing the control register\n");
2187 /* set VFCHG/VRCHG */
2188 err = ricoh619_write(info->dev->parent,
2189 BATSET2_REG, set_vchg_h);
2191 dev_err(info->dev, "Error in writing the battery setting register\n");
2194 info->soca->jt_limit = 0;
2195 *is_jeita_updated = true;
2197 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 12<Temp<50, already set vfchg==%d, so no need to update ******\n",
2198 __func__, info->jt_vfchg_h);
2201 err = ricoh619_write(info->dev->parent, CHGISET_REG, set_ichg_h);
2203 dev_err(info->dev, "Error in writing the battery setting register\n");
2206 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2208 dev_err(info->dev, "Error in writing the control register\n");
2211 } else if (temp < 55) {
2212 /* 4th temperature range (50~55) */
2213 if (vfchg != info->jt_vfchg_l) {
2214 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 50<Temp<55, update to vfchg==%d ******\n", __func__, info->jt_vfchg_l);
2216 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2218 dev_err(info->dev, "Error in writing the control register\n");
2221 /* set VFCHG/VRCHG */
2222 err = ricoh619_write(info->dev->parent,
2223 BATSET2_REG, set_vchg_l);
2225 dev_err(info->dev, "Error in writing the battery setting register\n");
2228 info->soca->jt_limit = 1;
2229 *is_jeita_updated = true;
2231 RICOH_FG_DBG(KERN_INFO "JEITA: %s *** 50<Temp<55, already set vfchg==%d, so no need to update ******\n",
2232 __func__, info->jt_vfchg_l);
2235 err = ricoh619_write(info->dev->parent, CHGISET_REG, set_ichg_h);
2237 dev_err(info->dev, "Error in writing the battery setting register\n");
2240 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2242 dev_err(info->dev, "Error in writing the control register\n");
2247 get_power_supply_status(info);
2248 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Hope updating value in this timing after checking jeita, chg_status: %d, is_jeita_updated: %d ******\n",
2249 __func__, info->soca->chg_status, *is_jeita_updated);
2254 RICOH_FG_DBG(KERN_INFO "PMU: %s ERROR ******\n", __func__);
2258 static void ricoh619_jeita_work(struct work_struct *work)
2261 bool is_jeita_updated = false;
2262 struct ricoh619_battery_info *info = container_of(work,
2263 struct ricoh619_battery_info, jeita_work.work);
2265 mutex_lock(&info->lock);
2267 ret = check_jeita_status(info, &is_jeita_updated);
2269 queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
2270 RICOH619_JEITA_UPDATE_TIME * HZ);
2272 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Call check_jeita_status() in jeita_work, err:%d ******\n",
2274 queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
2275 RICOH619_FG_RESET_TIME * HZ);
2278 mutex_unlock(&info->lock);
2280 if(true == is_jeita_updated)
2281 power_supply_changed(&info->battery);
2286 #ifdef ENABLE_FACTORY_MODE
2287 /*------------------------------------------------------*/
2289 /* Check Battery exist or not */
2290 /* If not, disabled Rapid to Complete State change */
2291 /*------------------------------------------------------*/
2292 static int ricoh619_factory_mode(struct ricoh619_battery_info *info)
2297 ret = ricoh619_read(info->dev->parent, RICOH619_INT_MON_CHGCTR, &val);
2299 dev_err(info->dev, "Error in reading the control register\n");
2302 if (!(val & 0x01)) /* No Adapter connected */
2305 /* Rapid to Complete State change disable */
2306 ret = ricoh619_set_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x40);
2308 dev_err(info->dev, "Error in writing the control register\n");
2312 /* Wait 1s for checking Charging State */
2313 queue_delayed_work(info->factory_mode_wqueue, &info->factory_mode_work,
2319 static void check_charging_state_work(struct work_struct *work)
2321 struct ricoh619_battery_info *info = container_of(work,
2322 struct ricoh619_battery_info, factory_mode_work.work);
2326 int chargeCurrent = 0;
2328 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &val);
2330 dev_err(info->dev, "Error in reading the control register\n");
2335 chargeCurrent = get_check_fuel_gauge_reg(info, CC_AVERAGE1_REG,
2336 CC_AVERAGE0_REG, 0x3fff);
2337 if (chargeCurrent < 0) {
2338 dev_err(info->dev, "Error in reading the FG register\n");
2342 /* Repid State && Charge Current about 0mA */
2343 if (((chargeCurrent >= 0x3ffc && chargeCurrent <= 0x3fff)
2344 || chargeCurrent < 0x05) && val == 0x43) {
2345 RICOH_FG_DBG("PMU:%s --- No battery !! Enter Factory mode ---\n"
2347 info->entry_factory_mode = true;
2348 /* clear FG_ACC bit */
2349 ret = ricoh619_clr_bits(info->dev->parent, RICOH619_FG_CTRL, 0x10);
2351 dev_err(info->dev, "Error in writing FG_CTRL\n");
2353 return; /* Factory Mode */
2356 /* Return Normal Mode --> Rapid to Complete State change enable */
2357 ret = ricoh619_clr_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x40);
2359 dev_err(info->dev, "Error in writing the control register\n");
2362 RICOH_FG_DBG("PMU:%s --- Battery exist !! Return Normal mode ---0x%2x\n"
2367 #endif /* ENABLE_FACTORY_MODE */
2369 static int Calc_Linear_Interpolation(int x0, int y0, int x1, int y1, int y)
2374 alpha = (y - y0)*100 / (y1 - y0);
2376 x = ((100 - alpha) * x0 + alpha * x1) / 100;
2381 static void ricoh619_scaling_OCV_table(struct ricoh619_battery_info *info, int cutoff_vol, int full_vol, int *start_per, int *end_per)
2386 int OCV_percent_new[11];
2388 /* get ocv table. this table is calculated by Apprication */
2389 //RICOH_FG_DBG("PMU : %s : original table\n",__func__);
2390 for (i = 0; i <= 10; i = i+1) {
2391 RICOH_FG_DBG(KERN_INFO "PMU: %s : %d0%% voltage = %d uV\n",
2392 __func__, i, info->soca->ocv_table_def[i]);
2394 //RICOH_FG_DBG("PMU: %s : cutoff_vol %d full_vol %d\n",
2395 // __func__, cutoff_vol,full_vol);
2398 if (info->soca->ocv_table_def[0] > cutoff_vol * 1000) {
2400 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]);
2402 for (i = 1; i < 11; i++) {
2403 if (info->soca->ocv_table_def[i] >= cutoff_vol * 1000) {
2404 /* unit is 0.001% */
2405 *start_per = Calc_Linear_Interpolation(
2406 (i-1)*1000, info->soca->ocv_table_def[i-1], i*1000,
2407 info->soca->ocv_table_def[i], (cutoff_vol * 1000));
2414 for (i = 1; i < 11; i++) {
2415 if (info->soca->ocv_table_def[i] >= full_vol * 1000) {
2416 /* unit is 0.001% */
2417 *end_per = Calc_Linear_Interpolation(
2418 (i-1)*1000, info->soca->ocv_table_def[i-1], i*1000,
2419 info->soca->ocv_table_def[i], (full_vol * 1000));
2424 /* calc new ocv percent */
2425 percent_step = ( *end_per - *start_per) / 10;
2426 //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);
2428 for (i = 0; i < 11; i++) {
2430 = *start_per + percent_step*(i - 0);
2433 /* calc new ocv voltage */
2434 for (i = 0; i < 11; i++) {
2435 for (j = 1; j < 11; j++) {
2436 if (1000*j >= OCV_percent_new[i]) {
2437 temp = Calc_Linear_Interpolation(
2438 info->soca->ocv_table_def[j-1], (j-1)*1000,
2439 info->soca->ocv_table_def[j] , j*1000,
2440 OCV_percent_new[i]);
2442 temp = ( (temp/1000) * 4095 ) / 5000;
2444 battery_init_para[info->num][i*2 + 1] = temp;
2445 battery_init_para[info->num][i*2] = temp >> 8;
2451 RICOH_FG_DBG("PMU : %s : new table\n",__func__);
2452 for (i = 0; i <= 10; i = i+1) {
2453 temp = (battery_init_para[info->num][i*2]<<8)
2454 | (battery_init_para[info->num][i*2+1]);
2455 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
2456 temp = ((temp * 50000 * 10 / 4095) + 5) / 10;
2457 RICOH_FG_DBG("PMU : %s : ocv_table %d is %d v\n",__func__, i, temp);
2462 static int ricoh619_set_OCV_table(struct ricoh619_battery_info *info)
2468 int available_cap_ori;
2481 for (i = 0; i <= 10; i = i+1) {
2482 info->soca->ocv_table_def[i] = get_OCV_voltage(info, i);
2483 RICOH_FG_DBG(KERN_INFO "PMU: %s : %d0%% voltage = %d uV\n",
2484 __func__, i, info->soca->ocv_table_def[i]);
2487 temp = (battery_init_para[info->num][24]<<8) | (battery_init_para[info->num][25]);
2488 Rbat = temp * 1000 / 512 * 5000 / 4095;
2489 info->soca->Rsys = Rbat + 55;
2491 if ((info->fg_target_ibat == 0) || (info->fg_target_vsys == 0)) { /* normal version */
2493 temp = (battery_init_para[info->num][22]<<8) | (battery_init_para[info->num][23]);
2494 //fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
2497 info->soca->target_ibat = temp*2/10; /* calc 0.2C*/
2498 temp1 = (battery_init_para[info->num][0]<<8) | (battery_init_para[info->num][1]);
2500 info->soca->target_vsys = temp1 + ( info->soca->target_ibat * info->soca->Rsys ) / 1000;
2504 info->soca->target_ibat = info->fg_target_ibat;
2505 /* calc min vsys value */
2506 temp1 = (battery_init_para[info->num][0]<<8) | (battery_init_para[info->num][1]);
2507 temp = temp1 + ( info->soca->target_ibat * info->soca->Rsys ) / 1000;
2508 if( temp < info->fg_target_vsys) {
2509 info->soca->target_vsys = info->fg_target_vsys;
2511 info->soca->target_vsys = temp;
2512 RICOH_FG_DBG("PMU : %s : setting value of target vsys(%d) is out of range(%d)\n",__func__, info->fg_target_vsys, temp);
2517 RICOH_FG_DBG("PMU : %s : target_vsys is %d target_ibat is %d\n",__func__,info->soca->target_vsys,info->soca->target_ibat);
2519 if ((info->soca->target_ibat == 0) || (info->soca->target_vsys == 0)) { /* normal version */
2520 } else { /*Slice cutoff voltage version. */
2522 Ibat_min = -1 * info->soca->target_ibat;
2523 info->soca->cutoff_ocv = info->soca->target_vsys - Ibat_min * info->soca->Rsys / 1000;
2525 full_ocv = (battery_init_para[info->num][20]<<8) | (battery_init_para[info->num][21]);
2526 full_ocv = full_ocv * 5000 / 4095;
2528 ricoh619_scaling_OCV_table(info, info->soca->cutoff_ocv, full_ocv, &start_per, &end_per);
2530 /* calc available capacity */
2531 /* get avilable capacity */
2532 /* battery_init_para23-24 is designe capacity */
2533 available_cap = (battery_init_para[info->num][22]<<8)
2534 | (battery_init_para[info->num][23]);
2536 available_cap = available_cap
2537 * ((10000 - start_per) / 100) / 100 ;
2540 battery_init_para[info->num][23] = available_cap;
2541 battery_init_para[info->num][22] = available_cap >> 8;
2544 ret = ricoh619_clr_bits(info->dev->parent, FG_CTRL_REG, 0x01);
2546 dev_err(info->dev, "error in FG_En off\n");
2549 /////////////////////////////////
2550 ret = ricoh619_read_bank1(info->dev->parent, 0xDC, &val);
2552 dev_err(info->dev, "batterry initialize error\n");
2557 val &= 0x0F; //clear bit 4-7
2558 val |= 0x10; //set bit 4
2560 ret = ricoh619_write_bank1(info->dev->parent, 0xDC, val);
2562 dev_err(info->dev, "batterry initialize error\n");
2566 ret = ricoh619_read_bank1(info->dev->parent, 0xDC, &val2);
2568 dev_err(info->dev, "batterry initialize error\n");
2572 ret = ricoh619_write_bank1(info->dev->parent, 0xDC, val_temp);
2574 dev_err(info->dev, "batterry initialize error\n");
2578 RICOH_FG_DBG("PMU : %s : original 0x%x, before 0x%x, after 0x%x\n",__func__, val_temp, val, val2);
2581 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
2582 BAT_INIT_TOP_REG, 30, battery_init_para[info->num]);
2584 dev_err(info->dev, "batterry initialize error\n");
2588 ret = ricoh619_read_bank1(info->dev->parent, 0xD2, &val);
2590 dev_err(info->dev, "batterry initialize error\n");
2594 ret = ricoh619_read_bank1(info->dev->parent, 0xD3, &val2);
2596 dev_err(info->dev, "batterry initialize error\n");
2600 available_cap_ori = val2 + (val << 8);
2601 available_cap = battery_init_para[info->num][23]
2602 + (battery_init_para[info->num][22] << 8);
2604 if (available_cap_ori == available_cap) {
2605 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
2606 BAT_INIT_TOP_REG, 22, battery_init_para[info->num]);
2608 dev_err(info->dev, "batterry initialize error\n");
2612 for (i = 0; i < 6; i++) {
2613 ret = ricoh619_write_bank1(info->dev->parent, 0xD4+i, battery_init_para[info->num][24+i]);
2615 dev_err(info->dev, "batterry initialize error\n");
2620 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
2621 BAT_INIT_TOP_REG, 30, battery_init_para[info->num]);
2623 dev_err(info->dev, "batterry initialize error\n");
2629 ////////////////////////////////
2636 /* Initial setting of battery */
2637 static int ricoh619_init_battery(struct ricoh619_battery_info *info)
2642 /* Need to implement initial setting of batery and error */
2643 /* -------------------------- */
2644 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
2646 /* set relaxation state */
2647 if (RICOH619_REL1_SEL_VALUE > 240)
2650 val = RICOH619_REL1_SEL_VALUE / 16 ;
2652 /* set relaxation state */
2653 if (RICOH619_REL2_SEL_VALUE > 120)
2656 val2 = RICOH619_REL2_SEL_VALUE / 8 ;
2658 val = val + (val2 << 4);
2660 ret = ricoh619_write_bank1(info->dev->parent, BAT_REL_SEL_REG, val);
2662 dev_err(info->dev, "Error in writing BAT_REL_SEL_REG\n");
2666 ret = ricoh619_read_bank1(info->dev->parent, BAT_REL_SEL_REG, &val);
2667 RICOH_FG_DBG("PMU: ------- BAT_REL_SEL= %xh: =======\n",
2670 ret = ricoh619_write_bank1(info->dev->parent, BAT_TA_SEL_REG, 0x00);
2672 dev_err(info->dev, "Error in writing BAT_TA_SEL_REG\n");
2676 // ret = ricoh619_read(info->dev->parent, FG_CTRL_REG, &val);
2678 // dev_err(info->dev, "Error in reading the control register\n");
2682 // val = (val & 0x10) >> 4;
2683 // info->first_pwon = (val == 0) ? 1 : 0;
2684 ret = ricoh619_read(info->dev->parent, PSWR_REG, &val);
2686 dev_err(info->dev,"Error in reading PSWR_REG %d\n", ret);
2689 info->first_pwon = (val == 0) ? 1 : 0;
2692 ret = ricoh619_set_OCV_table(info);
2694 dev_err(info->dev, "Error in writing the OCV Tabler\n");
2698 ret = ricoh619_write(info->dev->parent, FG_CTRL_REG, 0x11);
2700 dev_err(info->dev, "Error in writing the control register\n");
2706 ret = ricoh619_write(info->dev->parent, VINDAC_REG, 0x03);
2708 dev_err(info->dev, "Error in writing the control register\n");
2712 if (info->alarm_vol_mv < 2700 || info->alarm_vol_mv > 3400) {
2713 dev_err(info->dev, "alarm_vol_mv is out of range!\n");
2720 /* Initial setting of charger */
2721 static int ricoh619_init_charger(struct ricoh619_battery_info *info)
2734 info->chg_stat1 = 0;
2736 err = ricoh619_set_bits(info->dev->parent, RICOH619_PWR_FUNC, 0x20);
2738 dev_err(info->dev, "Error in writing the PWR FUNC register\n");
2742 charge_status = get_power_supply_status(info);
2744 if (charge_status != POWER_SUPPLY_STATUS_FULL)
2746 /* Disable charging */
2747 err = ricoh619_clr_bits(info->dev->parent,CHGCTL1_REG, 0x03);
2749 dev_err(info->dev, "Error in writing the control register\n");
2755 err = ricoh619_read(info->dev->parent, REGISET1_REG,&val);
2756 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);
2758 /* REGISET1:(0xB6) setting */
2759 if ((info->ch_ilim_adp != 0xFF) || (info->ch_ilim_adp <= 0x1D)) {
2760 val = info->ch_ilim_adp;
2762 err = ricoh619_write(info->dev->parent, REGISET1_REG,val);
2764 dev_err(info->dev, "Error in writing REGISET1_REG %d\n",
2771 err = ricoh619_read(info->dev->parent, REGISET1_REG,&val);
2772 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);
2775 err = ricoh619_read(info->dev->parent, REGISET2_REG,&val);
2776 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);
2778 /* REGISET2:(0xB7) setting */
2779 err = ricoh619_read(info->dev->parent, REGISET2_REG, &val);
2782 "Error in read REGISET2_REG %d\n", err);
2786 if ((info->ch_ilim_usb != 0xFF) || (info->ch_ilim_usb <= 0x1D)) {
2787 val2 = info->ch_ilim_usb;
2788 } else {/* Keep OTP value */
2789 val2 = (val & 0x1F);
2797 err = ricoh619_write(info->dev->parent, REGISET2_REG,val);
2799 dev_err(info->dev, "Error in writing REGISET2_REG %d\n",
2805 err = ricoh619_read(info->dev->parent, REGISET2_REG,&val);
2806 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);
2808 /* CHGISET_REG(0xB8) setting */
2810 err = ricoh619_read(info->dev->parent, CHGISET_REG,&val);
2811 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);
2813 err = ricoh619_read(info->dev->parent, CHGISET_REG, &val);
2816 "Error in read CHGISET_REG %d\n", err);
2820 /* Define Current settings value for charging (bit 4~0)*/
2821 if ((info->ch_ichg != 0xFF) || (info->ch_ichg <= 0x1D)) {
2822 val2 = info->ch_ichg;
2823 } else { /* Keep OTP value */
2824 val2 = (val & 0x1F);
2827 /* Define Current settings at the charge completion (bit 7~6)*/
2828 if ((info->ch_icchg != 0xFF) || (info->ch_icchg <= 0x03)) {
2829 val3 = info->ch_icchg << 6;
2830 } else { /* Keep OTP value */
2831 val3 = (val & 0xC0);
2836 err = ricoh619_write(info->dev->parent, CHGISET_REG, val);
2838 dev_err(info->dev, "Error in writing CHGISET_REG %d\n",
2844 err = ricoh619_read(info->dev->parent, CHGISET_REG,&val);
2845 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);
2848 err = ricoh619_read(info->dev->parent, BATSET1_REG,&val);
2849 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);
2851 /* BATSET1_REG(0xBA) setting */
2852 err = ricoh619_read(info->dev->parent, BATSET1_REG, &val);
2855 "Error in read BATSET1 register %d\n", err);
2859 /* Define Battery overvoltage (bit 4)*/
2860 if ((info->ch_vbatovset != 0xFF) || (info->ch_vbatovset <= 0x1)) {
2861 val2 = info->ch_vbatovset;
2863 } else { /* Keep OTP value */
2864 val2 = (val & 0x10);
2867 /* keep bit 0-3 and bit 5-7 */
2872 err = ricoh619_write(info->dev->parent, BATSET1_REG, val);
2874 dev_err(info->dev, "Error in writing BAT1_REG %d\n",
2879 err = ricoh619_read(info->dev->parent, BATSET1_REG,&val);
2880 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);
2883 err = ricoh619_read(info->dev->parent, BATSET2_REG,&val);
2884 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);
2887 /* BATSET2_REG(0xBB) setting */
2888 err = ricoh619_read(info->dev->parent, BATSET2_REG, &val);
2891 "Error in read BATSET2 register %d\n", err);
2895 /* Define Re-charging voltage (bit 2~0)*/
2896 if ((info->ch_vrchg != 0xFF) || (info->ch_vrchg <= 0x04)) {
2897 val2 = info->ch_vrchg;
2898 } else { /* Keep OTP value */
2899 val2 = (val & 0x07);
2902 /* Define FULL charging voltage (bit 6~4)*/
2903 if ((info->ch_vfchg != 0xFF) || (info->ch_vfchg <= 0x04)) {
2904 val3 = info->ch_vfchg;
2906 } else { /* Keep OTP value */
2907 val3 = (val & 0x70);
2910 /* keep bit 3 and bit 7 */
2913 val = val + val2 + val3;
2915 err = ricoh619_write(info->dev->parent, BATSET2_REG, val);
2917 dev_err(info->dev, "Error in writing RICOH619_RE_CHARGE_VOLTAGE %d\n",
2923 err = ricoh619_read(info->dev->parent, BATSET2_REG,&val);
2924 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);
2926 /* Set rising edge setting ([1:0]=01b)for INT in charging */
2927 /* and rising edge setting ([3:2]=01b)for charge completion */
2928 err = ricoh619_read(info->dev->parent, RICOH619_CHG_STAT_DETMOD1, &val);
2930 dev_err(info->dev, "Error in reading CHG_STAT_DETMOD1 %d\n",
2936 err = ricoh619_write(info->dev->parent, RICOH619_CHG_STAT_DETMOD1, val);
2938 dev_err(info->dev, "Error in writing CHG_STAT_DETMOD1 %d\n",
2943 /* Unmask In charging/charge completion */
2944 err = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGSTS1, 0xfc);
2946 dev_err(info->dev, "Error in writing INT_MSK_CHGSTS1 %d\n",
2951 /* Set both edge for VUSB([3:2]=11b)/VADP([1:0]=11b) detect */
2952 err = ricoh619_read(info->dev->parent, RICOH619_CHG_CTRL_DETMOD1, &val);
2954 dev_err(info->dev, "Error in reading CHG_CTRL_DETMOD1 %d\n",
2960 err = ricoh619_write(info->dev->parent, RICOH619_CHG_CTRL_DETMOD1, val);
2962 dev_err(info->dev, "Error in writing CHG_CTRL_DETMOD1 %d\n",
2967 /* Unmask In VUSB/VADP completion */
2968 err = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGCTR, 0xfc);
2970 dev_err(info->dev, "Error in writing INT_MSK_CHGSTS1 %d\n",
2975 if (charge_status != POWER_SUPPLY_STATUS_FULL)
2977 /* Enable charging */
2978 err = ricoh619_set_bits(info->dev->parent,CHGCTL1_REG, 0x03);
2980 dev_err(info->dev, "Error in writing the control register\n");
2984 /* get OCV100_min, OCV100_min*/
2985 temp = (battery_init_para[info->num][24]<<8) | (battery_init_para[info->num][25]);
2986 rbat = temp * 1000 / 512 * 5000 / 4095;
2988 /* get vfchg value */
2989 err = ricoh619_read(info->dev->parent, BATSET2_REG, &val);
2991 dev_err(info->dev, "Error in reading the batset2reg\n");
2997 vfchg_val = 4050 + val2 * 50;
3001 RICOH_FG_DBG("PMU : %s : test test val %d, val2 %d vfchg %d\n", __func__, val, val2, vfchg_val);
3004 err = ricoh619_read(info->dev->parent, CHGISET_REG, &val);
3006 dev_err(info->dev, "Error in reading the chgisetreg\n");
3011 icchg_val = 50 + val2 * 50;
3012 RICOH_FG_DBG("PMU : %s : test test val %d, val2 %d icchg %d\n", __func__, val, val2, icchg_val);
3014 info->soca->OCV100_min = ( vfchg_val * 99 / 100 - (icchg_val * (rbat +20))/1000 - 20 ) * 1000;
3015 info->soca->OCV100_max = ( vfchg_val * 101 / 100 - (icchg_val * (rbat +20))/1000 + 20 ) * 1000;
3017 RICOH_FG_DBG("PMU : %s : 100 min %d, 100 max %d vfchg %d icchg %d rbat %d\n",__func__,
3018 info->soca->OCV100_min,info->soca->OCV100_max,vfchg_val,icchg_val,rbat);
3020 #ifdef ENABLE_LOW_BATTERY_DETECTION
3021 /* Set ADRQ=00 to stop ADC */
3022 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT3, 0x0);
3023 /* Enable VSYS threshold Low interrupt */
3024 ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
3025 /* Set ADC auto conversion interval 250ms */
3026 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT2, 0x0);
3027 /* Enable VSYS pin conversion in auto-ADC */
3028 // ricoh619_write(info->dev->parent, RICOH619_ADC_CNT1, 0x10);
3029 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT1, 0x16);
3030 /* Set VSYS threshold low voltage value = (voltage(V)*255)/(3*2.5) */
3031 val = info->alarm_vol_mv * 255 / 7500;
3032 ricoh619_write(info->dev->parent, RICOH619_ADC_VSYS_THL, val);
3033 /* Start auto-mode & average 4-time conversion mode for ADC */
3034 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT3, 0x28);
3035 /* Enable master ADC INT */
3036 ricoh619_set_bits(info->dev->parent, RICOH619_INTC_INTEN, ADC_INT);
3044 static int get_power_supply_status(struct ricoh619_battery_info *info)
3047 uint8_t supply_state;
3048 uint8_t charge_state;
3051 /* get power supply status */
3052 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
3054 dev_err(info->dev, "Error in reading the control register\n");
3058 charge_state = (status & 0x1F);
3059 supply_state = ((status & 0xC0) >> 6);
3061 if (info->entry_factory_mode)
3062 return POWER_SUPPLY_STATUS_NOT_CHARGING;
3064 if (supply_state == SUPPLY_STATE_BAT) {
3065 info->soca->chg_status = POWER_SUPPLY_STATUS_DISCHARGING;
3067 switch (charge_state) {
3068 case CHG_STATE_CHG_OFF:
3069 info->soca->chg_status
3070 = POWER_SUPPLY_STATUS_DISCHARGING;
3072 case CHG_STATE_CHG_READY_VADP:
3073 info->soca->chg_status
3074 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3076 case CHG_STATE_CHG_TRICKLE:
3077 info->soca->chg_status
3078 = POWER_SUPPLY_STATUS_CHARGING;
3080 case CHG_STATE_CHG_RAPID:
3081 info->soca->chg_status
3082 = POWER_SUPPLY_STATUS_CHARGING;
3084 case CHG_STATE_CHG_COMPLETE:
3085 info->soca->chg_status
3086 = POWER_SUPPLY_STATUS_FULL;
3088 case CHG_STATE_SUSPEND:
3089 info->soca->chg_status
3090 = POWER_SUPPLY_STATUS_DISCHARGING;
3092 case CHG_STATE_VCHG_OVER_VOL:
3093 info->soca->chg_status
3094 = POWER_SUPPLY_STATUS_DISCHARGING;
3096 case CHG_STATE_BAT_ERROR:
3097 info->soca->chg_status
3098 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3100 case CHG_STATE_NO_BAT:
3101 info->soca->chg_status
3102 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3104 case CHG_STATE_BAT_OVER_VOL:
3105 info->soca->chg_status
3106 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3108 case CHG_STATE_BAT_TEMP_ERR:
3109 info->soca->chg_status
3110 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3112 case CHG_STATE_DIE_ERR:
3113 info->soca->chg_status
3114 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3116 case CHG_STATE_DIE_SHUTDOWN:
3117 info->soca->chg_status
3118 = POWER_SUPPLY_STATUS_DISCHARGING;
3120 case CHG_STATE_NO_BAT2:
3121 info->soca->chg_status
3122 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3124 case CHG_STATE_CHG_READY_VUSB:
3125 info->soca->chg_status
3126 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3129 info->soca->chg_status
3130 = POWER_SUPPLY_STATUS_UNKNOWN;
3135 return info->soca->chg_status;
3138 static int get_power_supply_Android_status(struct ricoh619_battery_info *info)
3141 get_power_supply_status(info);
3143 /* get power supply status */
3144 if (info->entry_factory_mode)
3145 return POWER_SUPPLY_STATUS_NOT_CHARGING;
3147 switch (info->soca->chg_status) {
3148 case POWER_SUPPLY_STATUS_UNKNOWN:
3149 return POWER_SUPPLY_STATUS_UNKNOWN;
3152 case POWER_SUPPLY_STATUS_NOT_CHARGING:
3153 return POWER_SUPPLY_STATUS_NOT_CHARGING;
3156 case POWER_SUPPLY_STATUS_DISCHARGING:
3157 return POWER_SUPPLY_STATUS_DISCHARGING;
3160 case POWER_SUPPLY_STATUS_CHARGING:
3161 return POWER_SUPPLY_STATUS_CHARGING;
3164 case POWER_SUPPLY_STATUS_FULL:
3165 if(info->soca->displayed_soc == 100 * 100) {
3166 return POWER_SUPPLY_STATUS_FULL;
3168 return POWER_SUPPLY_STATUS_CHARGING;
3172 return POWER_SUPPLY_STATUS_UNKNOWN;
3176 return POWER_SUPPLY_STATUS_UNKNOWN;
3178 extern struct ricoh619 *g_ricoh619;
3179 static void charger_irq_work(struct work_struct *work)
3181 struct ricoh619_battery_info *info
3182 = container_of(work, struct ricoh619_battery_info, irq_work);
3185 RICOH_FG_DBG("PMU:%s In\n", __func__);
3187 power_supply_changed(&info->battery);
3188 power_supply_changed(&powerac);
3189 power_supply_changed(&powerusb);
3191 // mutex_lock(&info->lock);
3193 if (info->chg_stat1 & 0x01) {
3194 ricoh619_read(info->dev->parent, CHGSTATE_REG, ®_val);
3195 if (reg_val & 0x40) { /* USE ADP */
3196 #ifdef SUPPORT_USB_CONNECT_TO_ADP
3199 RICOH_FG_DBG("PMU:%s usb det dwc_otg_check_dpdm =%d\n", __func__,dwc_otg_check_dpdm(0));
3200 if(2 == dwc_otg_check_dpdm(0)){
3201 /* set adp limit current 2A */
3202 ricoh619_write(info->dev->parent, REGISET1_REG, 0x16);
3203 /* set charge current 2A */
3204 ricoh619_write(info->dev->parent, CHGISET_REG, 0xD3);
3207 /* set adp limit current 500ma */
3208 ricoh619_write(info->dev->parent, REGISET1_REG, 0x04);
3209 /* set charge current 500ma */
3210 ricoh619_write(info->dev->parent, CHGISET_REG, 0xc4);
3213 power_supply_changed(&info->battery);
3214 power_supply_changed(&powerac);
3215 power_supply_changed(&powerusb);
3218 #else //support adp and usb chag
3219 if (gpio_is_valid(g_ricoh619->dc_det)){
3220 ret = gpio_request(g_ricoh619->dc_det, "ricoh619_dc_det");
3222 RICOH_FG_DBG("Failed to request gpio %d with ret:""%d\n",g_ricoh619->dc_det, ret);
3224 gpio_direction_input(g_ricoh619->dc_det);
3225 ret = gpio_get_value(g_ricoh619->dc_det);
3228 /* set adp limit current 2A */
3229 ricoh619_write(info->dev->parent, REGISET1_REG, 0x16);
3230 /* set charge current 2A */
3231 ricoh619_write(info->dev->parent, CHGISET_REG, 0xD3);
3234 /* set adp limit current 500ma */
3235 ricoh619_write(info->dev->parent, REGISET1_REG, 0x04);
3236 /* set charge current 500ma */
3237 ricoh619_write(info->dev->parent, CHGISET_REG, 0xc4);
3239 gpio_free(g_ricoh619->dc_det);
3242 /* set adp limit current 2A */
3243 ricoh619_write(info->dev->parent, REGISET1_REG, 0x16);
3244 /* set charge current 2A */
3245 ricoh619_write(info->dev->parent, CHGISET_REG, 0xD3);
3249 else if (reg_val & 0x80) { /* USE USB */
3250 queue_work(info->usb_workqueue, &info->usb_irq_work);
3254 info->chg_stat1 = 0;
3256 /* Enable Interrupt for VADP/USB */
3257 ret = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGCTR, 0xfc);
3260 "%s(): Error in enable charger mask INT %d\n",
3263 /* Enable Interrupt for Charging & complete */
3264 ret = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGSTS1, 0xfc);
3267 "%s(): Error in enable charger mask INT %d\n",
3270 // mutex_unlock(&info->lock);
3271 RICOH_FG_DBG("PMU:%s Out\n", __func__);
3274 #ifdef ENABLE_LOW_BATTERY_DETECTION
3275 static void low_battery_irq_work(struct work_struct *work)
3277 struct ricoh619_battery_info *info = container_of(work,
3278 struct ricoh619_battery_info, low_battery_work.work);
3282 RICOH_FG_DBG("PMU:%s In\n", __func__);
3284 power_supply_changed(&info->battery);
3286 /* Enable VADP threshold Low interrupt */
3287 ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
3290 "%s(): Error in enable adc mask INT %d\n",
3296 static void ricoh619_usb_charge_det(void)
3298 struct ricoh619 *ricoh619 = g_ricoh619;
3299 ricoh619_set_bits(ricoh619->dev,REGISET2_REG,(1 << 7)); //set usb limit current when SDP or other mode
3300 RICOH_FG_DBG("PMU:%s usb det dwc_otg_check_dpdm =%d\n", __func__,dwc_otg_check_dpdm(0));
3301 if(2 == dwc_otg_check_dpdm(0)){
3302 ricoh619_write(ricoh619->dev,REGISET2_REG,0x16); //set usb limit current 2A
3303 ricoh619_write(ricoh619->dev,CHGISET_REG,0xD3); //set charge current 2A
3306 ricoh619_write(ricoh619->dev,REGISET2_REG,0x04); //set usb limit current 500ma
3307 ricoh619_write(ricoh619->dev,CHGISET_REG,0xC4); //set charge current 500ma
3309 power_supply_changed(&powerac);
3310 power_supply_changed(&powerusb);
3313 static void usb_det_irq_work(struct work_struct *work)
3315 struct ricoh619_battery_info *info = container_of(work,
3316 struct ricoh619_battery_info, usb_irq_work);
3320 RICOH_FG_DBG("PMU:%s In\n", __func__);
3322 power_supply_changed(&info->battery);
3323 power_supply_changed(&powerac);
3324 power_supply_changed(&powerusb);
3326 mutex_lock(&info->lock);
3328 /* Enable Interrupt for VUSB */
3329 ret = ricoh619_clr_bits(info->dev->parent,
3330 RICOH619_INT_MSK_CHGCTR, 0x02);
3333 "%s(): Error in enable charger mask INT %d\n",
3336 mutex_unlock(&info->lock);
3337 ret = ricoh619_read(info->dev->parent, RICOH619_INT_MON_CHGCTR, &sts);
3339 dev_err(info->dev, "Error in reading the control register\n");
3344 ricoh619_usb_charge_det();
3348 /*********************/
3350 /*********************/
3353 RICOH_FG_DBG("PMU:%s Out\n", __func__);
3356 extern void rk_send_wakeup_key(void);
3357 static irqreturn_t charger_in_isr(int irq, void *battery_info)
3359 struct ricoh619_battery_info *info = battery_info;
3360 RICOH_FG_DBG("PMU:%s\n", __func__);
3362 info->chg_stat1 |= 0x01;
3364 queue_work(info->workqueue, &info->irq_work);
3365 // rk_send_wakeup_key();
3370 static irqreturn_t charger_complete_isr(int irq, void *battery_info)
3372 struct ricoh619_battery_info *info = battery_info;
3373 RICOH_FG_DBG("PMU:%s\n", __func__);
3375 info->chg_stat1 |= 0x02;
3376 queue_work(info->workqueue, &info->irq_work);
3377 // rk_send_wakeup_key();
3382 static irqreturn_t charger_usb_isr(int irq, void *battery_info)
3384 struct ricoh619_battery_info *info = battery_info;
3385 RICOH_FG_DBG("PMU:%s\n", __func__);
3387 info->chg_ctr |= 0x02;
3389 queue_work(info->workqueue, &info->irq_work);
3391 info->soca->dischg_state = 0;
3392 info->soca->chg_count = 0;
3394 // queue_work(info->usb_workqueue, &info->usb_irq_work);
3395 rk_send_wakeup_key();
3397 if (RICOH619_SOCA_UNSTABLE == info->soca->status
3398 || RICOH619_SOCA_FG_RESET == info->soca->status)
3399 info->soca->stable_count = 11;
3404 static irqreturn_t charger_adp_isr(int irq, void *battery_info)
3406 struct ricoh619_battery_info *info = battery_info;
3407 RICOH_FG_DBG("PMU:%s\n", __func__);
3409 info->chg_ctr |= 0x01;
3410 queue_work(info->workqueue, &info->irq_work);
3411 rk_send_wakeup_key();
3413 info->soca->dischg_state = 0;
3414 info->soca->chg_count = 0;
3415 if (RICOH619_SOCA_UNSTABLE == info->soca->status
3416 || RICOH619_SOCA_FG_RESET == info->soca->status)
3417 info->soca->stable_count = 11;
3423 #ifdef ENABLE_LOW_BATTERY_DETECTION
3424 /*************************************************************/
3425 /* for Detecting Low Battery */
3426 /*************************************************************/
3428 static irqreturn_t adc_vsysl_isr(int irq, void *battery_info)
3431 struct ricoh619_battery_info *info = battery_info;
3434 RICOH_FG_DBG("PMU:%s\n", __func__);
3436 queue_delayed_work(info->monitor_wqueue, &info->low_battery_work,
3437 LOW_BATTERY_DETECTION_TIME*HZ);
3441 RICOH_FG_DBG("PMU:%s\n", __func__);
3442 // ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
3443 rk_send_wakeup_key();
3448 #ifdef RICOH619_VADP_DROP_WORK
3449 static void vadp_drop_irq_work(struct work_struct *work)
3451 struct ricoh619_battery_info *info = container_of(work,
3452 struct ricoh619_battery_info, vadp_drop_work.work);
3458 RICOH_FG_DBG("PMU vadp_drop_work:%s In\n", __func__);
3459 mutex_lock(&info->lock);
3460 ret = ricoh619_read(info->dev->parent, 0x6a, &data[0]);
3461 ret = ricoh619_read(info->dev->parent, 0x6b, &data[1]);
3462 ret = ricoh619_read(info->dev->parent, 0x6c, &data[2]);
3463 ret = ricoh619_read(info->dev->parent, 0x6d, &data[3]);
3464 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG,&data[4]);
3465 reg[0]= (data[0]<<4) |data[1];
3466 reg[1]= (data[2]<<4) |data[3];
3468 // 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);
3469 if ((2*(reg[0] +82)) > 3*reg[1]){
3470 ricoh619_write(info->dev->parent, 0xb3, 0x28);
3471 // printk("PMU vadp_drop charger disable:%s In %08x %08x\n", __func__,reg[0],reg[1]);
3473 else if(data[4] & 0xc0){
3474 ret = ricoh619_read(info->dev->parent, 0xb3, &data[5]);
3475 // printk("PMU charger is disabled:%s data[4]= %08x data[5]=%08x\n", __func__,data[4],data[5]);
3476 if(((data[5] & 0x03) ==0)|| ((data[5] & 0x08)==0)){
3477 ricoh619_write(info->dev->parent, 0xb3, 0x23);
3478 ret = ricoh619_read(info->dev->parent, 0xb3, &data[5]);
3479 // printk("PMU charger enable:%s data[4]= %08x data[5]=%08x\n", __func__,data[4],data[5]);
3482 power_supply_changed(&info->battery);
3483 power_supply_changed(&powerac);
3484 power_supply_changed(&powerusb);
3485 mutex_unlock(&info->lock);
3486 queue_delayed_work(info->monitor_wqueue, &info->vadp_drop_work,3*HZ);
3491 * Get Charger Priority
3492 * - get higher-priority between VADP and VUSB
3493 * @ data: higher-priority is stored
3498 static int get_charge_priority(struct ricoh619_battery_info *info, bool *data)
3503 ret = ricoh619_read(info->dev->parent, CHGCTL1_REG, &val);
3512 * Set Charger Priority
3513 * - set higher-priority between VADP and VUSB
3514 * - data: higher-priority is stored
3519 static int set_charge_priority(struct ricoh619_battery_info *info, bool *data)
3525 ret = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, val);
3527 ret = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, val);
3532 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
3533 static int get_check_fuel_gauge_reg(struct ricoh619_battery_info *info,
3534 int Reg_h, int Reg_l, int enable_bit)
3536 uint8_t get_data_h, get_data_l;
3537 int old_data, current_data;
3543 for (i = 0; i < 5 ; i++) {
3544 ret = ricoh619_read(info->dev->parent, Reg_h, &get_data_h);
3546 dev_err(info->dev, "Error in reading the control register\n");
3550 ret = ricoh619_read(info->dev->parent, Reg_l, &get_data_l);
3552 dev_err(info->dev, "Error in reading the control register\n");
3556 current_data = ((get_data_h & 0xff) << 8) | (get_data_l & 0xff);
3557 current_data = (current_data & enable_bit);
3559 if (current_data == old_data)
3560 return current_data;
3562 old_data = current_data;
3565 return current_data;
3568 static int calc_capacity(struct ricoh619_battery_info *info)
3576 temperature = get_battery_temp_2(info) / 10; /* unit 0.1 degree -> 1 degree */
3578 if (temperature >= 25) {
3580 } else if (temperature >= 5) {
3581 nt = (25 - temperature) * RICOH619_TAH_SEL2 * 625 / 100;
3583 nt = (625 + (5 - temperature) * RICOH619_TAL_SEL2 * 625 / 100);
3586 /* get remaining battery capacity from fuel gauge */
3587 ret = ricoh619_read(info->dev->parent, SOC_REG, &capacity);
3589 dev_err(info->dev, "Error in reading the control register\n");
3593 temp = capacity * 100 * 100 / (10000 - nt);
3595 temp = min(100, temp);
3596 temp = max(0, temp);
3598 return temp; /* Unit is 1% */
3601 static int calc_capacity_2(struct ricoh619_battery_info *info)
3611 temperature = get_battery_temp_2(info) / 10; /* unit 0.1 degree -> 1 degree */
3613 if (temperature >= 25) {
3615 } else if (temperature >= 5) {
3616 nt = (25 - temperature) * RICOH619_TAH_SEL2 * 625 / 100;
3618 nt = (625 + (5 - temperature) * RICOH619_TAL_SEL2 * 625 / 100);
3621 re_cap = get_check_fuel_gauge_reg(info, RE_CAP_H_REG, RE_CAP_L_REG,
3623 fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
3627 capacity = ((long)re_cap * 100 * 100 / fa_cap);
3628 capacity = (long)(min(10000, (int)capacity));
3629 capacity = (long)(max(0, (int)capacity));
3631 ret = ricoh619_read(info->dev->parent, SOC_REG, &val);
3633 dev_err(info->dev, "Error in reading the control register\n");
3636 capacity = (long)val * 100;
3640 temp = (int)(capacity * 100 * 100 / (10000 - nt));
3642 temp = min(10000, temp);
3643 temp = max(0, temp);
3645 return temp; /* Unit is 0.01% */
3648 static int get_battery_temp(struct ricoh619_battery_info *info)
3653 ret = get_check_fuel_gauge_reg(info, TEMP_1_REG, TEMP_2_REG, 0x0fff);
3655 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
3659 /* bit3 of 0xED(TEMP_1) is sign_bit */
3660 sign_bit = ((ret & 0x0800) >> 11);
3662 ret = (ret & 0x07ff);
3664 if (sign_bit == 0) /* positive value part */
3665 /* conversion unit */
3666 /* 1 unit is 0.0625 degree and retun unit
3667 * should be 0.1 degree,
3669 ret = ret * 625 / 1000;
3670 else { /*negative value part */
3671 ret = (~ret + 1) & 0x7ff;
3672 ret = -1 * ret * 625 / 1000;
3678 static int get_battery_temp_2(struct ricoh619_battery_info *info)
3680 uint8_t reg_buff[2];
3681 long temp, temp_off, temp_gain;
3682 bool temp_sign, temp_off_sign, temp_gain_sign;
3691 /* Calculate TEMP */
3692 ret = get_check_fuel_gauge_reg(info, TEMP_1_REG, TEMP_2_REG, 0x0fff);
3694 dev_err(info->dev, "Error in reading the fuel gauge register\n");
3699 temp_sign = (reg_val & 0x0800) >> 11;
3700 reg_val = (reg_val & 0x07ff);
3702 if (temp_sign == 0) /* positive value part */
3703 /* the unit is 0.0001 degree */
3704 temp = (long)reg_val * 625;
3705 else { /*negative value part */
3706 reg_val = (~reg_val + 1) & 0x7ff;
3707 temp = -1 * (long)reg_val * 625;
3710 /* Calculate TEMP_OFF */
3711 ret = ricoh619_bulk_reads_bank1(info->dev->parent,
3712 TEMP_OFF_H_REG, 2, reg_buff);
3714 dev_err(info->dev, "Error in reading the fuel gauge register\n");
3718 reg_val = reg_buff[0] << 8 | reg_buff[1];
3719 temp_off_sign = (reg_val & 0x0800) >> 11;
3720 reg_val = (reg_val & 0x07ff);
3722 if (temp_off_sign == 0) /* positive value part */
3723 /* the unit is 0.0001 degree */
3724 temp_off = (long)reg_val * 625;
3725 else { /*negative value part */
3726 reg_val = (~reg_val + 1) & 0x7ff;
3727 temp_off = -1 * (long)reg_val * 625;
3730 /* Calculate TEMP_GAIN */
3731 ret = ricoh619_bulk_reads_bank1(info->dev->parent,
3732 TEMP_GAIN_H_REG, 2, reg_buff);
3734 dev_err(info->dev, "Error in reading the fuel gauge register\n");
3738 reg_val = reg_buff[0] << 8 | reg_buff[1];
3739 temp_gain_sign = (reg_val & 0x0800) >> 11;
3740 reg_val = (reg_val & 0x07ff);
3742 if (temp_gain_sign == 0) /* positive value part */
3743 /* 1 unit is 0.000488281. the result is 0.01 */
3744 temp_gain = (long)reg_val * 488281 / 100000;
3745 else { /*negative value part */
3746 reg_val = (~reg_val + 1) & 0x7ff;
3747 temp_gain = -1 * (long)reg_val * 488281 / 100000;
3750 /* Calculate VTHM */
3752 Vthm = (int)((temp - temp_off) / 4095 * 2500 / temp_gain);
3754 RICOH_FG_DBG("PMU %s Skip to compensate temperature\n", __func__);
3758 ret = measure_Ibatt_FG(info, &Iout);
3759 Vsns = Iout * 2 / 100;
3761 if (temp < -120000) {
3762 /* Low Temperature */
3763 if (0 != (2500 - Vthm)) {
3764 Rthm = 10 * 10 * (Vthm - Vsns) / (2500 - Vthm);
3766 RICOH_FG_DBG("PMU %s Skip to compensate temperature\n", __func__);
3771 R_ln2 = (R_ln1 * R_ln1 * R_ln1 * R_ln1 * R_ln1 / 100000
3772 - R_ln1 * R_ln1 * R_ln1 * R_ln1 * 2 / 100
3773 + R_ln1 * R_ln1 * R_ln1 * 11
3774 - R_ln1 * R_ln1 * 2980
3778 /* the unit of new_temp is 0.1 degree */
3779 new_temp = (int)((100 * 1000 * B_VALUE / (R_ln2 + B_VALUE * 100 * 1000 / 29815) - 27315) / 10);
3780 RICOH_FG_DBG("PMU %s low temperature %d\n", __func__, new_temp/10);
3781 } else if (temp > 520000) {
3782 /* High Temperature */
3783 if (0 != (2500 - Vthm)) {
3784 Rthm = 100 * 10 * (Vthm - Vsns) / (2500 - Vthm);
3786 RICOH_FG_DBG("PMU %s Skip to compensate temperature\n", __func__);
3789 RICOH_FG_DBG("PMU %s [Rthm] Rthm %d[ohm]\n", __func__, Rthm);
3792 R_ln2 = (R_ln1 * R_ln1 * R_ln1 * R_ln1 * R_ln1 / 100000 * 15652 / 100
3793 - R_ln1 * R_ln1 * R_ln1 * R_ln1 / 1000 * 23103 / 100
3794 + R_ln1 * R_ln1 * R_ln1 * 1298 / 100
3795 - R_ln1 * R_ln1 * 35089 / 100
3796 + R_ln1 * 50334 / 10
3798 /* the unit of new_temp is 0.1 degree */
3799 new_temp = (int)((100 * 100 * B_VALUE / (R_ln2 + B_VALUE * 100 * 100 / 29815) - 27315) / 10);
3800 RICOH_FG_DBG("PMU %s high temperature %d\n", __func__, new_temp/10);
3802 /* the unit of new_temp is 0.1 degree */
3803 new_temp = temp / 1000;
3809 new_temp = get_battery_temp(info);
3813 static int get_time_to_empty(struct ricoh619_battery_info *info)
3817 ret = get_check_fuel_gauge_reg(info, TT_EMPTY_H_REG, TT_EMPTY_L_REG,
3820 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
3824 /* conversion unit */
3825 /* 1unit is 1miniute and return nnit should be 1 second */
3831 static int get_time_to_full(struct ricoh619_battery_info *info)
3835 ret = get_check_fuel_gauge_reg(info, TT_FULL_H_REG, TT_FULL_L_REG,
3838 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
3847 /* battery voltage is get from Fuel gauge */
3848 static int measure_vbatt_FG(struct ricoh619_battery_info *info, int *data)
3852 if(info->soca->ready_fg == 1) {
3853 ret = get_check_fuel_gauge_reg(info, VOLTAGE_1_REG, VOLTAGE_2_REG,
3856 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
3861 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
3862 *data = *data * 50000 / 4095;
3863 /* return unit should be 1uV */
3864 *data = *data * 100;
3865 info->soca->Vbat_old = *data;
3867 *data = info->soca->Vbat_old;
3873 static int measure_Ibatt_FG(struct ricoh619_battery_info *info, int *data)
3877 ret = get_check_fuel_gauge_reg(info, CC_AVERAGE1_REG,
3878 CC_AVERAGE0_REG, 0x3fff);
3880 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
3884 *data = (ret > 0x1fff) ? (ret - 0x4000) : ret;
3888 static int get_OCV_init_Data(struct ricoh619_battery_info *info, int index)
3891 ret = (battery_init_para[info->num][index*2]<<8) | (battery_init_para[info->num][index*2+1]);
3895 static int get_OCV_voltage(struct ricoh619_battery_info *info, int index)
3898 ret = get_OCV_init_Data(info, index);
3899 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
3900 ret = ret * 50000 / 4095;
3901 /* return unit should be 1uV */
3907 /* battery voltage is get from ADC */
3908 static int measure_vbatt_ADC(struct ricoh619_battery_info *info, int *data)
3911 uint8_t data_l = 0, data_h = 0;
3914 /* ADC interrupt enable */
3915 ret = ricoh619_set_bits(info->dev->parent, INTEN_REG, 0x08);
3917 dev_err(info->dev, "Error in setting the control register bit\n");
3921 /* enable interrupt request of single mode */
3922 ret = ricoh619_set_bits(info->dev->parent, EN_ADCIR3_REG, 0x01);
3924 dev_err(info->dev, "Error in setting the control register bit\n");
3928 /* single request */
3929 ret = ricoh619_write(info->dev->parent, ADCCNT3_REG, 0x10);
3931 dev_err(info->dev, "Error in writing the control register\n");
3935 for (i = 0; i < 5; i++) {
3937 RICOH_FG_DBG("ADC conversion times: %d\n", i);
3938 /* read completed flag of ADC */
3939 ret = ricoh619_read(info->dev->parent, EN_ADCIR3_REG, &data_h);
3941 dev_err(info->dev, "Error in reading the control register\n");
3949 dev_err(info->dev, "ADC conversion too long!\n");
3953 ret = ricoh619_read(info->dev->parent, VBATDATAH_REG, &data_h);
3955 dev_err(info->dev, "Error in reading the control register\n");
3959 ret = ricoh619_read(info->dev->parent, VBATDATAL_REG, &data_l);
3961 dev_err(info->dev, "Error in reading the control register\n");
3965 *data = ((data_h & 0xff) << 4) | (data_l & 0x0f);
3966 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
3967 *data = *data * 5000 / 4095;
3968 /* return unit should be 1uV */
3969 *data = *data * 1000;
3978 static int measure_vsys_ADC(struct ricoh619_battery_info *info, int *data)
3980 uint8_t data_l = 0, data_h = 0;
3983 ret = ricoh619_read(info->dev->parent, VSYSDATAH_REG, &data_h);
3985 dev_err(info->dev, "Error in reading the control register\n");
3988 ret = ricoh619_read(info->dev->parent, VSYSDATAL_REG, &data_l);
3990 dev_err(info->dev, "Error in reading the control register\n");
3993 *data = ((data_h & 0xff) << 4) | (data_l & 0x0f);
3994 *data = *data * 1000 * 3 * 5 / 2 / 4095;
3995 /* return unit should be 1uV */
3996 *data = *data * 1000;
4001 static void ricoh619_external_power_changed(struct power_supply *psy)
4003 struct ricoh619_battery_info *info;
4005 info = container_of(psy, struct ricoh619_battery_info, battery);
4006 queue_delayed_work(info->monitor_wqueue,
4007 &info->changed_work, HZ / 2);
4012 static int ricoh619_batt_get_prop(struct power_supply *psy,
4013 enum power_supply_property psp,
4014 union power_supply_propval *val)
4016 struct ricoh619_battery_info *info = dev_get_drvdata(psy->dev->parent);
4021 mutex_lock(&info->lock);
4025 case POWER_SUPPLY_PROP_ONLINE:
4026 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
4028 dev_err(info->dev, "Error in reading the control register\n");
4029 mutex_unlock(&info->lock);
4032 #ifdef SUPPORT_USB_CONNECT_TO_ADP
4033 if (psy->type == POWER_SUPPLY_TYPE_MAINS){
4034 if((2 == dwc_otg_check_dpdm(0)) && (status & 0x40))
4039 else if (psy->type == POWER_SUPPLY_TYPE_USB){
4040 if((1 == dwc_otg_check_dpdm(0)) && (status & 0x40))
4046 if (psy->type == POWER_SUPPLY_TYPE_MAINS)
4047 val->intval = (status & 0x40 ? 1 : 0);
4048 else if (psy->type == POWER_SUPPLY_TYPE_USB)
4049 val->intval = (status & 0x80 ? 1 : 0);
4052 /* this setting is same as battery driver of 584 */
4053 case POWER_SUPPLY_PROP_STATUS:
4054 if(info->chg_complete_tm_ov_flag == 0)
4056 ret = get_power_supply_Android_status(info);
4059 /* RICOH_FG_DBG("Power Supply Status is %d\n",
4064 val->intval = POWER_SUPPLY_STATUS_FULL;
4068 /* this setting is same as battery driver of 584 */
4069 case POWER_SUPPLY_PROP_PRESENT:
4070 // val->intval = info->present;
4074 /* current voltage is get from fuel gauge */
4075 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
4076 /* return real vbatt Voltage */
4077 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
4078 if (info->soca->ready_fg)
4079 ret = measure_vbatt_FG(info, &data);
4081 //val->intval = -EINVAL;
4082 data = info->cur_voltage * 1000;
4083 RICOH_FG_DBG( "battery voltage is not ready\n");
4086 ret = measure_vbatt_ADC(info, &data);
4089 /* convert unit uV -> mV */
4090 info->cur_voltage = data / 1000;
4092 RICOH_FG_DBG( "battery voltage is %d mV\n",
4096 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
4097 /* current battery capacity is get from fuel gauge */
4098 case POWER_SUPPLY_PROP_CAPACITY:
4099 if (info->entry_factory_mode){
4101 info->capacity = 100;
4102 } else if (info->soca->displayed_soc < 0) {
4104 info->capacity = 10;
4106 if(info->chg_complete_tm_ov_flag == 1)
4108 info->capacity = 100;
4109 val->intval = info->capacity;
4113 info->capacity = (info->soca->displayed_soc + 50)/100;
4114 val->intval = info->capacity;
4117 RICOH_FG_DBG("battery capacity is %d%%\n", info->capacity);
4120 /* current temperature of battery */
4121 case POWER_SUPPLY_PROP_TEMP:
4122 if (info->soca->ready_fg) {
4124 val->intval = get_battery_temp_2(info);
4125 info->battery_temp = val->intval/10;
4126 RICOH_FG_DBG( "battery temperature is %d degree\n", info->battery_temp);
4128 val->intval = info->battery_temp * 10;
4129 /* RICOH_FG_DBG("battery temperature is not ready\n"); */
4134 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
4135 if (info->soca->ready_fg) {
4136 ret = get_time_to_empty(info);
4138 info->time_to_empty = ret/60;
4139 RICOH_FG_DBG("time of empty battery is %d minutes\n", info->time_to_empty);
4141 //val->intval = -EINVAL;
4142 val->intval = info->time_to_empty * 60;
4143 RICOH_FG_DBG("time of empty battery is %d minutes\n", info->time_to_empty);
4144 /* RICOH_FG_DBG( "time of empty battery is not ready\n"); */
4148 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
4149 if (info->soca->ready_fg) {
4150 ret = get_time_to_full(info);
4152 info->time_to_full = ret/60;
4153 RICOH_FG_DBG( "time of full battery is %d minutes\n", info->time_to_full);
4155 //val->intval = -EINVAL;
4156 val->intval = info->time_to_full * 60;
4157 RICOH_FG_DBG( "time of full battery is %d minutes\n", info->time_to_full);
4158 /* RICOH_FG_DBG("time of full battery is not ready\n"); */
4164 case POWER_SUPPLY_PROP_TECHNOLOGY:
4165 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
4169 case POWER_SUPPLY_PROP_HEALTH:
4170 val->intval = POWER_SUPPLY_HEALTH_GOOD;
4173 case POWER_SUPPLY_PROP_CURRENT_AVG:
4174 measure_Ibatt_FG(info, &data);
4175 //RICOH_FG_DBG("average current xxxxxxxxxxxxxx %d \n", data);
4178 mutex_unlock(&info->lock);
4182 mutex_unlock(&info->lock);
4187 static enum power_supply_property ricoh619_batt_props[] = {
4188 POWER_SUPPLY_PROP_STATUS,
4189 POWER_SUPPLY_PROP_PRESENT,
4190 POWER_SUPPLY_PROP_VOLTAGE_NOW,
4191 POWER_SUPPLY_PROP_CURRENT_AVG,
4193 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
4194 POWER_SUPPLY_PROP_CAPACITY,
4195 POWER_SUPPLY_PROP_TEMP,
4196 //POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
4197 //POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
4199 POWER_SUPPLY_PROP_TECHNOLOGY,
4200 POWER_SUPPLY_PROP_HEALTH,
4203 static enum power_supply_property ricoh619_power_props[] = {
4204 POWER_SUPPLY_PROP_ONLINE,
4207 struct power_supply powerac = {
4209 .type = POWER_SUPPLY_TYPE_MAINS,
4210 .properties = ricoh619_power_props,
4211 .num_properties = ARRAY_SIZE(ricoh619_power_props),
4212 .get_property = ricoh619_batt_get_prop,
4215 struct power_supply powerusb = {
4217 .type = POWER_SUPPLY_TYPE_USB,
4218 .properties = ricoh619_power_props,
4219 .num_properties = ARRAY_SIZE(ricoh619_power_props),
4220 .get_property = ricoh619_batt_get_prop,
4224 static struct ricoh619_battery_platform_data *
4225 ricoh619_battery_dt_init(struct platform_device *pdev)
4227 struct device_node *nproot = pdev->dev.parent->of_node;
4228 struct device_node *np;
4229 struct ricoh619_battery_platform_data *pdata;
4232 return pdev->dev.platform_data;
4234 np = of_find_node_by_name(nproot, "battery");
4236 dev_err(&pdev->dev, "failed to find battery node\n");
4240 pdata = devm_kzalloc(&pdev->dev,
4241 sizeof(struct ricoh619_battery_platform_data),
4244 of_property_read_u32(np, "ricoh,monitor-time", &pdata->monitor_time);
4245 of_property_read_u32(np, "ricoh,alarm-vol-mv", &pdata->alarm_vol_mv);
4247 /* check rage of b,.attery type */
4248 type_n = Battery_Type();
4249 RICOH_FG_DBG("%s type_n=%d\n", __func__, type_n);
4253 of_property_read_u32(np, "ricoh,ch-vfchg", &pdata->type[0].ch_vfchg);
4254 of_property_read_u32(np, "ricoh,ch-vrchg", &pdata->type[0].ch_vrchg);
4255 of_property_read_u32(np, "ricoh,ch-vbatovset", &pdata->type[0].ch_vbatovset);
4256 of_property_read_u32(np, "ricoh,ch-ichg", &pdata->type[0].ch_ichg);
4257 of_property_read_u32(np, "ricoh,ch-ilim-adp", &pdata->type[0].ch_ilim_adp);
4258 of_property_read_u32(np, "ricoh,ch-ilim-usb", &pdata->type[0].ch_ilim_usb);
4259 of_property_read_u32(np, "ricoh,ch-icchg", &pdata->type[0].ch_icchg);
4260 of_property_read_u32(np, "ricoh,fg-target-vsys", &pdata->type[0].fg_target_vsys);
4261 of_property_read_u32(np, "ricoh,fg-target-ibat", &pdata->type[0].fg_target_ibat);
4262 of_property_read_u32(np, "ricoh,fg-poff-vbat", &pdata->type[0].fg_poff_vbat);
4263 of_property_read_u32(np, "ricoh,jt-en", &pdata->type[0].jt_en);
4264 of_property_read_u32(np, "ricoh,jt-hw-sw", &pdata->type[0].jt_hw_sw);
4265 of_property_read_u32(np, "ricoh,jt-temp-h", &pdata->type[0].jt_temp_h);
4266 of_property_read_u32(np, "ricoh,jt-temp-l", &pdata->type[0].jt_temp_l);
4267 of_property_read_u32(np, "ricoh,jt-vfchg-h", &pdata->type[0].jt_vfchg_h);
4268 of_property_read_u32(np, "ricoh,jt-vfchg-l", &pdata->type[0].jt_vfchg_l);
4269 of_property_read_u32(np, "ricoh,jt-ichg-h", &pdata->type[0].jt_ichg_h);
4270 of_property_read_u32(np, "ricoh,jt-ichg-l", &pdata->type[0].jt_ichg_l);
4274 of_property_read_u32(np, "ricoh,ch-vfchg-1", &pdata->type[1].ch_vfchg);
4275 of_property_read_u32(np, "ricoh,ch-vrchg-1", &pdata->type[1].ch_vrchg);
4276 of_property_read_u32(np, "ricoh,ch-vbatovset-1", &pdata->type[1].ch_vbatovset);
4277 of_property_read_u32(np, "ricoh,ch-ichg-1", &pdata->type[1].ch_ichg);
4278 of_property_read_u32(np, "ricoh,ch-ilim-adp-1", &pdata->type[1].ch_ilim_adp);
4279 of_property_read_u32(np, "ricoh,ch-ilim-usb-1", &pdata->type[1].ch_ilim_usb);
4280 of_property_read_u32(np, "ricoh,ch-icchg-1", &pdata->type[1].ch_icchg);
4281 of_property_read_u32(np, "ricoh,fg-target-vsys-1", &pdata->type[1].fg_target_vsys);
4282 of_property_read_u32(np, "ricoh,fg-target-ibat-1", &pdata->type[1].fg_target_ibat);
4283 of_property_read_u32(np, "ricoh,fg-poff-vbat-1", &pdata->type[1].fg_poff_vbat);
4284 of_property_read_u32(np, "ricoh,jt-en-1", &pdata->type[1].jt_en);
4285 of_property_read_u32(np, "ricoh,jt-hw-sw-1", &pdata->type[1].jt_hw_sw);
4286 of_property_read_u32(np, "ricoh,jt-temp-h-1", &pdata->type[1].jt_temp_h);
4287 of_property_read_u32(np, "ricoh,jt-temp-l-1", &pdata->type[1].jt_temp_l);
4288 of_property_read_u32(np, "ricoh,jt-vfchg-h-1", &pdata->type[1].jt_vfchg_h);
4289 of_property_read_u32(np, "ricoh,jt-vfchg-l-1", &pdata->type[1].jt_vfchg_l);
4290 of_property_read_u32(np, "ricoh,jt-ichg-h-1", &pdata->type[1].jt_ichg_h);
4291 of_property_read_u32(np, "ricoh,jt-ichg-l-1", &pdata->type[1].jt_ichg_l);
4304 static struct ricoh619_battery_platform_data *
4305 ricoh619_battery_dt_init(struct platform_device *pdev)
4307 return pdev->dev.platform_data;
4311 static int ricoh619_battery_probe(struct platform_device *pdev)
4313 struct ricoh619_battery_info *info;
4314 struct ricoh619_battery_platform_data *pdata;
4315 struct ricoh619 *ricoh619 = dev_get_drvdata(pdev->dev.parent);
4318 RICOH_FG_DBG(KERN_INFO "PMU: %s : version is %s\n", __func__,RICOH619_BATTERY_VERSION);
4320 pdata = ricoh619_battery_dt_init(pdev);
4322 dev_err(&pdev->dev, "platform data isn't assigned to "
4326 info = devm_kzalloc(ricoh619->dev,sizeof(struct ricoh619_battery_info), GFP_KERNEL);
4329 info->soca = devm_kzalloc(ricoh619->dev,sizeof(struct ricoh619_soca_info), GFP_KERNEL);
4333 info->dev = &pdev->dev;
4334 info->status = POWER_SUPPLY_STATUS_CHARGING;
4335 info->monitor_time = pdata->monitor_time * HZ;
4336 info->alarm_vol_mv = pdata->alarm_vol_mv;
4338 /* check rage of battery num */
4339 info->num = Battery_Table();
4340 temp = sizeof(battery_init_para)/(sizeof(uint8_t)*32);
4341 if(info->num >= (sizeof(battery_init_para)/(sizeof(uint8_t)*32)))
4343 RICOH_FG_DBG("%s : Battery num is out of range\n", __func__);
4346 RICOH_FG_DBG("%s info->num=%d,temp is %d\n", __func__, info->num,temp);
4348 /* these valuse are set in platform */
4349 info->ch_vfchg = pdata->type[type_n].ch_vfchg;
4350 info->ch_vrchg = pdata->type[type_n].ch_vrchg;
4351 info->ch_vbatovset = pdata->type[type_n].ch_vbatovset;
4352 info->ch_ichg = pdata->type[type_n].ch_ichg;
4353 info->ch_ilim_adp = pdata->type[type_n].ch_ilim_adp;
4354 info->ch_ilim_usb = pdata->type[type_n].ch_ilim_usb;
4355 info->ch_icchg = pdata->type[type_n].ch_icchg;
4356 info->fg_target_vsys = pdata->type[type_n].fg_target_vsys;
4357 info->fg_target_ibat = pdata->type[type_n].fg_target_ibat;
4358 info->fg_poff_vbat = pdata->type[type_n].fg_poff_vbat;
4359 info->jt_en = pdata->type[type_n].jt_en;
4360 info->jt_hw_sw = pdata->type[type_n].jt_hw_sw;
4361 info->jt_temp_h = pdata->type[type_n].jt_temp_h;
4362 info->jt_temp_l = pdata->type[type_n].jt_temp_l;
4363 info->jt_vfchg_h = pdata->type[type_n].jt_vfchg_h;
4364 info->jt_vfchg_l = pdata->type[type_n].jt_vfchg_l;
4365 info->jt_ichg_h = pdata->type[type_n].jt_ichg_h;
4366 info->jt_ichg_l = pdata->type[type_n].jt_ichg_l;
4368 info->adc_vdd_mv = ADC_VDD_MV; /* 2800; */
4369 info->min_voltage = MIN_VOLTAGE; /* 3100; */
4370 info->max_voltage = MAX_VOLTAGE; /* 4200; */
4372 info->entry_factory_mode = false;
4374 info->chg_complete_rd_flag = 0;
4375 info->chg_complete_rd_cnt = 0;
4376 info->chg_complete_tm_ov_flag = 0;
4377 info->chg_complete_sleep_flag = 0;
4379 mutex_init(&info->lock);
4380 platform_set_drvdata(pdev, info);
4382 info->battery.name = "battery";
4383 info->battery.type = POWER_SUPPLY_TYPE_BATTERY;
4384 info->battery.properties = ricoh619_batt_props;
4385 info->battery.num_properties = ARRAY_SIZE(ricoh619_batt_props);
4386 info->battery.get_property = ricoh619_batt_get_prop;
4387 info->battery.set_property = NULL;
4388 /* info->battery.external_power_changed
4389 = ricoh619_external_power_changed; */
4391 /* Disable Charger/ADC interrupt */
4392 ret = ricoh619_clr_bits(info->dev->parent, RICOH619_INTC_INTEN,
4397 ret = ricoh619_init_battery(info);
4401 #ifdef ENABLE_FACTORY_MODE
4402 info->factory_mode_wqueue
4403 = create_singlethread_workqueue("ricoh619_factory_mode");
4404 INIT_DEFERRABLE_WORK(&info->factory_mode_work,
4405 check_charging_state_work);
4407 ret = ricoh619_factory_mode(info);
4413 ret = power_supply_register(&pdev->dev, &info->battery);
4416 info->battery.dev->parent = &pdev->dev;
4418 ret = power_supply_register(&pdev->dev, &powerac);
4419 ret = power_supply_register(&pdev->dev, &powerusb);
4421 info->monitor_wqueue
4422 = create_singlethread_workqueue("ricoh619_battery_monitor");
4424 info->workqueue = create_singlethread_workqueue("rc5t619_charger_in");
4425 INIT_WORK(&info->irq_work, charger_irq_work);
4428 = create_singlethread_workqueue("rc5t619_usb_det");
4429 INIT_WORK(&info->usb_irq_work, usb_det_irq_work);
4431 INIT_DEFERRABLE_WORK(&info->monitor_work,
4432 ricoh619_battery_work);
4433 INIT_DEFERRABLE_WORK(&info->displayed_work,
4434 ricoh619_displayed_work);
4435 INIT_DEFERRABLE_WORK(&info->charge_stable_work,
4436 ricoh619_stable_charge_countdown_work);
4437 INIT_DEFERRABLE_WORK(&info->charge_monitor_work,
4438 ricoh619_charge_monitor_work);
4439 INIT_DEFERRABLE_WORK(&info->get_charge_work,
4440 ricoh619_get_charge_work);
4441 INIT_DEFERRABLE_WORK(&info->jeita_work, ricoh619_jeita_work);
4442 INIT_DELAYED_WORK(&info->changed_work, ricoh619_changed_work);
4444 INIT_DELAYED_WORK(&info->charge_complete_ready, ricoh619_charging_complete_work);
4446 /* Charger IRQ workqueue settings */
4448 ret = request_threaded_irq( irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FONCHGINT),NULL, charger_in_isr, IRQF_ONESHOT,
4449 "rc5t619_charger_in", info);
4451 dev_err(&pdev->dev, "Can't get CHG_INT IRQ for chrager: %d\n",ret);
4455 ret = request_threaded_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FCHGCMPINT),NULL, charger_complete_isr,
4456 IRQF_ONESHOT, "rc5t619_charger_comp",info);
4458 dev_err(&pdev->dev, "Can't get CHG_COMP IRQ for chrager: %d\n",ret);
4462 ret = request_threaded_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FVUSBDETSINT) ,NULL, charger_usb_isr, IRQF_ONESHOT,
4463 "rc5t619_usb_det", info);
4465 dev_err(&pdev->dev, "Can't get USB_DET IRQ for chrager: %d\n",ret);
4469 ret = request_threaded_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FVADPDETSINT),NULL, charger_adp_isr, IRQF_ONESHOT,
4470 "rc5t619_adp_det", info);
4473 "Can't get ADP_DET IRQ for chrager: %d\n", ret);
4477 #ifdef ENABLE_LOW_BATTERY_DETECTION
4478 ret = request_threaded_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_VSYSLIR) ,NULL, adc_vsysl_isr, IRQF_ONESHOT,
4479 "rc5t619_adc_vsysl", info);
4482 "Can't get ADC_VSYSL IRQ for chrager: %d\n", ret);
4485 INIT_DEFERRABLE_WORK(&info->low_battery_work,
4486 low_battery_irq_work);
4488 #ifdef RICOH619_VADP_DROP_WORK
4489 INIT_DEFERRABLE_WORK(&info->vadp_drop_work,vadp_drop_irq_work);
4490 queue_delayed_work(info->monitor_wqueue, &info->vadp_drop_work,0);
4492 /* Charger init and IRQ setting */
4493 ret = ricoh619_init_charger(info);
4497 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
4498 ret = ricoh619_init_fgsoca(info);
4500 queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
4501 RICOH619_MONITOR_START_TIME*HZ);
4503 /* Enable Charger/ADC interrupt */
4504 ricoh619_set_bits(info->dev->parent, RICOH619_INTC_INTEN, CHG_INT | ADC_INT);
4513 static int ricoh619_battery_remove(struct platform_device *pdev)
4515 struct ricoh619_battery_info *info = platform_get_drvdata(pdev);
4516 struct ricoh619 *ricoh619 = dev_get_drvdata(pdev->dev.parent);
4521 bool is_charging = true;
4522 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
4524 && (info->soca->status == RICOH619_SOCA_STABLE)) {
4525 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
4527 dev_err(info->dev, "Error in writing PSWR_REG\n");
4529 } else if (info->soca->status != RICOH619_SOCA_START
4530 && info->soca->status != RICOH619_SOCA_UNSTABLE) {
4531 if (info->soca->displayed_soc <= 0) {
4534 val = (info->soca->displayed_soc + 50)/100;
4537 ret = ricoh619_write(info->dev->parent, PSWR_REG, val);
4539 dev_err(info->dev, "Error in writing PSWR_REG\n");
4543 ret = calc_capacity_in_period(info, &cc_cap,
4544 &is_charging, true);
4546 dev_err(info->dev, "Read cc_sum Error !!-----\n");
4549 if (g_fg_on_mode == 0) {
4550 ret = ricoh619_clr_bits(info->dev->parent,
4553 dev_err(info->dev, "Error in clr FG EN\n");
4556 /* set rapid timer 300 min */
4557 err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x03);
4559 dev_err(info->dev, "Error in writing the control register\n");
4562 if(info->capacity == 100)
4564 ret = ricoh619_write(info->dev->parent, PSWR_REG, 100);
4566 dev_err(info->dev, "Error in writing PSWR_REG\n");
4569 free_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FONCHGINT), &info);
4570 free_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FCHGCMPINT), &info);
4571 free_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FVUSBDETSINT), &info);
4572 free_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FVADPDETSINT) , &info);
4573 #ifdef ENABLE_LOW_BATTERY_DETECTION
4574 free_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_VSYSLIR), &info);
4577 cancel_delayed_work(&info->monitor_work);
4578 cancel_delayed_work(&info->charge_stable_work);
4579 cancel_delayed_work(&info->charge_monitor_work);
4580 cancel_delayed_work(&info->get_charge_work);
4581 cancel_delayed_work(&info->displayed_work);
4583 cancel_delayed_work(&info->changed_work);
4584 #ifdef ENABLE_LOW_BATTERY_DETECTION
4585 cancel_delayed_work(&info->low_battery_work);
4587 #ifdef RICOH619_VADP_DROP_WORK
4588 cancel_delayed_work(&info->vadp_drop_work);
4590 #ifdef ENABLE_FACTORY_MODE
4591 cancel_delayed_work(&info->factory_mode_work);
4593 cancel_delayed_work(&info->jeita_work);
4594 cancel_delayed_work(&info->charge_complete_ready);
4596 cancel_work_sync(&info->irq_work);
4597 cancel_work_sync(&info->usb_irq_work);
4599 flush_workqueue(info->monitor_wqueue);
4600 flush_workqueue(info->workqueue);
4601 flush_workqueue(info->usb_workqueue);
4602 #ifdef ENABLE_FACTORY_MODE
4603 flush_workqueue(info->factory_mode_wqueue);
4605 destroy_workqueue(info->monitor_wqueue);
4606 destroy_workqueue(info->workqueue);
4607 destroy_workqueue(info->usb_workqueue);
4608 #ifdef ENABLE_FACTORY_MODE
4609 destroy_workqueue(info->factory_mode_wqueue);
4612 power_supply_unregister(&info->battery);
4614 platform_set_drvdata(pdev, NULL);
4619 static int ricoh619_battery_suspend(struct device *dev)
4621 struct ricoh619_battery_info *info = dev_get_drvdata(dev);
4626 bool is_charging = true;
4627 int displayed_soc_temp;
4630 && (info->soca->status == RICOH619_SOCA_STABLE)) {
4631 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
4633 dev_err(info->dev, "Error in writing PSWR_REG\n");
4635 info->soca->suspend_soc = info->soca->displayed_soc;
4636 ret = calc_capacity_in_period(info, &cc_cap,
4637 &is_charging, true);
4639 dev_err(info->dev, "Read cc_sum Error !!-----\n");
4641 } else if (info->soca->status != RICOH619_SOCA_START
4642 && info->soca->status != RICOH619_SOCA_UNSTABLE) {
4643 if (info->soca->displayed_soc <= 0) {
4646 val = (info->soca->displayed_soc + 50)/100;
4649 ret = ricoh619_write(info->dev->parent, PSWR_REG, val);
4651 dev_err(info->dev, "Error in writing PSWR_REG\n");
4655 ret = calc_capacity_in_period(info, &cc_cap,
4656 &is_charging, true);
4658 dev_err(info->dev, "Read cc_sum Error !!-----\n");
4660 if (info->soca->status != RICOH619_SOCA_STABLE) {
4661 info->soca->cc_delta
4662 = (is_charging == true) ? cc_cap : -cc_cap;
4665 = info->soca->displayed_soc + info->soca->cc_delta;
4666 displayed_soc_temp = min(10000, displayed_soc_temp);
4667 displayed_soc_temp = max(0, displayed_soc_temp);
4668 info->soca->displayed_soc = displayed_soc_temp;
4670 info->soca->suspend_soc = info->soca->displayed_soc;
4672 } else if (info->soca->status == RICOH619_SOCA_START
4673 || info->soca->status == RICOH619_SOCA_UNSTABLE) {
4675 ret = ricoh619_read(info->dev->parent, PSWR_REG, &val);
4677 dev_err(info->dev, "Error in reading the pswr register\n");
4680 info->soca->suspend_soc = val * 100;
4683 if (info->soca->status == RICOH619_SOCA_DISP
4684 || info->soca->status == RICOH619_SOCA_STABLE
4685 || info->soca->status == RICOH619_SOCA_FULL) {
4686 info->soca->soc = calc_capacity_2(info);
4687 info->soca->soc_delta =
4688 info->soca->soc_delta + (info->soca->soc - info->soca->last_soc);
4691 info->soca->soc_delta = 0;
4694 if (info->soca->status == RICOH619_SOCA_STABLE
4695 || info->soca->status == RICOH619_SOCA_FULL)
4696 info->soca->status = RICOH619_SOCA_DISP;
4697 /* set rapid timer 300 min */
4698 err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x03);
4700 dev_err(info->dev, "Error in writing the control register\n");
4703 // disable_irq(charger_irq + RICOH619_IRQ_FONCHGINT);
4704 // disable_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT);
4705 // disable_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT);
4706 // disable_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT);
4707 #ifdef ENABLE_LOW_BATTERY_DETECTION
4708 // disable_irq(charger_irq + RICOH619_IRQ_VSYSLIR);
4711 flush_delayed_work(&info->monitor_work);
4712 flush_delayed_work(&info->displayed_work);
4713 flush_delayed_work(&info->charge_stable_work);
4714 flush_delayed_work(&info->charge_monitor_work);
4715 flush_delayed_work(&info->get_charge_work);
4716 flush_delayed_work(&info->changed_work);
4717 #ifdef ENABLE_LOW_BATTERY_DETECTION
4718 flush_delayed_work(&info->low_battery_work);
4720 flush_delayed_work(&info->factory_mode_work);
4721 flush_delayed_work(&info->jeita_work);
4722 #ifdef RICOH619_VADP_DROP_WORK
4723 flush_delayed_work(&info->vadp_drop_work);
4726 // flush_work(&info->irq_work);
4727 // flush_work(&info->usb_irq_work);
4729 cancel_delayed_work(&info->monitor_work);
4730 cancel_delayed_work(&info->displayed_work);
4731 cancel_delayed_work(&info->charge_stable_work);
4732 cancel_delayed_work(&info->charge_monitor_work);
4733 cancel_delayed_work(&info->get_charge_work);
4734 cancel_delayed_work(&info->changed_work);
4735 #ifdef ENABLE_LOW_BATTERY_DETECTION
4736 cancel_delayed_work(&info->low_battery_work);
4738 cancel_delayed_work(&info->charge_complete_ready);
4739 #ifdef ENABLE_FACTORY_MODE
4740 cancel_delayed_work(&info->factory_mode_work);
4742 cancel_delayed_work(&info->jeita_work);
4743 #ifdef RICOH619_VADP_DROP_WORK
4744 cancel_delayed_work(&info->vadp_drop_work);
4746 info->chg_complete_rd_cnt = 0;
4747 info->chg_complete_rd_flag = 0;
4749 if(info->capacity == 100)
4751 ret = ricoh619_write(info->dev->parent, PSWR_REG, 100);
4753 dev_err(info->dev, "Error in writing PSWR_REG\n");
4754 if(info->chg_complete_tm_ov_flag != 1)
4756 info->chg_complete_tm_ov_flag = 0;
4757 info->chg_complete_sleep_flag = 1;
4760 // flush_work(&info->irq_work);
4761 // flush_work(&info->usb_irq_work);
4767 static int ricoh619_battery_resume(struct device *dev)
4769 struct ricoh619_battery_info *info = dev_get_drvdata(dev);
4772 int displayed_soc_temp;
4774 bool is_charging = true;
4775 bool is_jeita_updated;
4778 RICOH_FG_DBG(KERN_INFO "PMU: %s: \n", __func__);
4780 ret = check_jeita_status(info, &is_jeita_updated);
4782 dev_err(info->dev, "Error in updating JEITA %d\n", ret);
4785 if (info->entry_factory_mode) {
4786 info->soca->displayed_soc = -EINVAL;
4787 } else if (RICOH619_SOCA_ZERO == info->soca->status) {
4788 if (calc_ocv(info) > get_OCV_voltage(info, 0)) {
4789 ret = ricoh619_read(info->dev->parent, PSWR_REG, &val);
4791 info->soca->soc = val * 100;
4794 "Error in reading PSWR_REG %d\n", ret);
4796 = calc_capacity(info) * 100;
4799 ret = calc_capacity_in_period(info, &cc_cap,
4800 &is_charging, true);
4802 dev_err(info->dev, "Read cc_sum Error !!-----\n");
4804 info->soca->cc_delta
4805 = (is_charging == true) ? cc_cap : -cc_cap;
4808 = info->soca->soc + info->soca->cc_delta;
4809 if (displayed_soc_temp < 0)
4810 displayed_soc_temp = 0;
4811 displayed_soc_temp = min(10000, displayed_soc_temp);
4812 displayed_soc_temp = max(0, displayed_soc_temp);
4813 info->soca->displayed_soc = displayed_soc_temp;
4815 ret = reset_FG_process(info);
4818 dev_err(info->dev, "Error in writing the control register\n");
4819 info->soca->status = RICOH619_SOCA_FG_RESET;
4822 info->soca->displayed_soc = 0;
4824 info->soca->soc = info->soca->suspend_soc;
4826 if (RICOH619_SOCA_START == info->soca->status
4827 || RICOH619_SOCA_UNSTABLE == info->soca->status) {
4828 ret = calc_capacity_in_period(info, &cc_cap,
4829 &is_charging, false);
4831 ret = calc_capacity_in_period(info, &cc_cap,
4832 &is_charging, true);
4836 dev_err(info->dev, "Read cc_sum Error !!-----\n");
4838 info->soca->cc_delta = (is_charging == true) ? cc_cap : -cc_cap;
4840 displayed_soc_temp = info->soca->soc + info->soca->cc_delta;
4841 if (info->soca->zero_flg == 1) {
4842 if((info->soca->Ibat_ave >= 0)
4843 || (displayed_soc_temp >= 100)){
4844 info->soca->zero_flg = 0;
4846 displayed_soc_temp = 0;
4848 } else if (displayed_soc_temp < 100) {
4849 /* keep DSOC = 1 when Vbat is over 3.4V*/
4850 if( info->fg_poff_vbat != 0) {
4851 if (info->soca->Vbat_ave < 2000*1000) { /* error value */
4852 displayed_soc_temp = 100;
4853 } else if (info->soca->Vbat_ave < info->fg_poff_vbat*1000) {
4854 displayed_soc_temp = 0;
4855 info->soca->zero_flg = 1;
4857 displayed_soc_temp = 100;
4861 displayed_soc_temp = min(10000, displayed_soc_temp);
4862 displayed_soc_temp = max(0, displayed_soc_temp);
4864 if (0 == info->soca->jt_limit) {
4865 check_charge_status_2(info, displayed_soc_temp);
4867 info->soca->displayed_soc = displayed_soc_temp;
4870 if (RICOH619_SOCA_DISP == info->soca->status) {
4871 info->soca->last_soc = calc_capacity_2(info);
4875 ret = measure_vbatt_FG(info, &info->soca->Vbat_ave);
4876 ret = measure_vsys_ADC(info, &info->soca->Vsys_ave);
4877 ret = measure_Ibatt_FG(info, &info->soca->Ibat_ave);
4879 if(info->chg_complete_sleep_flag == 1)
4881 info->chg_complete_tm_ov_flag = 1;
4882 info->chg_complete_sleep_flag = 0;
4885 power_supply_changed(&info->battery);
4886 queue_delayed_work(info->monitor_wqueue, &info->displayed_work, HZ);
4888 if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
4889 info->soca->stable_count = 10;
4890 queue_delayed_work(info->monitor_wqueue,
4891 &info->charge_stable_work,
4892 RICOH619_FG_STABLE_TIME*HZ/10);
4893 } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
4894 info->soca->stable_count = 1;
4896 for (i = 0; i < 3; i = i+1)
4897 info->soca->reset_soc[i] = 0;
4898 info->soca->reset_count = 0;
4900 queue_delayed_work(info->monitor_wqueue,
4901 &info->charge_stable_work,
4902 RICOH619_FG_RESET_TIME*HZ);
4905 queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
4906 info->monitor_time);
4908 queue_delayed_work(info->monitor_wqueue, &info->charge_monitor_work,
4909 RICOH619_CHARGE_RESUME_TIME * HZ);
4911 info->soca->chg_count = 0;
4912 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
4913 RICOH619_CHARGE_RESUME_TIME * HZ);
4914 #ifdef RICOH619_VADP_DROP_WORK
4915 queue_delayed_work(info->monitor_wqueue, &info->vadp_drop_work,1 * HZ);
4918 if (!info->jt_hw_sw) {
4919 queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
4920 RICOH619_JEITA_UPDATE_TIME * HZ);
4923 // ricoh619_write(info->dev->parent, 0x9d, 0x00);
4924 // enable_irq(charger_irq + RICOH619_IRQ_FONCHGINT);
4925 // enable_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT);
4926 // enable_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT);
4927 // enable_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT);
4928 #ifdef ENABLE_LOW_BATTERY_DETECTION
4929 // enable_irq(charger_irq + RICOH619_IRQ_VSYSLIR);
4931 ricoh619_write(info->dev->parent, 0x9d, 0x4d);
4936 static const struct of_device_id ricoh619_battery_dt_match[] = {
4937 { .compatible = "ricoh,ricoh619-battery", },
4940 MODULE_DEVICE_TABLE(of, ricoh619_battery_dt_match);
4944 static const struct dev_pm_ops ricoh619_battery_pm_ops = {
4945 .suspend = ricoh619_battery_suspend,
4946 .resume = ricoh619_battery_resume,
4950 static struct platform_driver ricoh619_battery_driver = {
4952 .name = "ricoh619-battery",
4953 .owner = THIS_MODULE,
4954 .of_match_table = of_match_ptr(ricoh619_battery_dt_match),
4956 .pm = &ricoh619_battery_pm_ops,
4959 .probe = ricoh619_battery_probe,
4960 .remove = ricoh619_battery_remove,
4963 static int __init ricoh619_battery_init(void)
4965 RICOH_FG_DBG("PMU: %s\n", __func__);
4966 return platform_driver_register(&ricoh619_battery_driver);
4968 fs_initcall_sync(ricoh619_battery_init);
4970 static void __exit ricoh619_battery_exit(void)
4972 platform_driver_unregister(&ricoh619_battery_driver);
4974 module_exit(ricoh619_battery_exit);
4976 MODULE_DESCRIPTION("RICOH619 Battery driver");
4977 MODULE_ALIAS("platform:ricoh619-battery");
4978 MODULE_LICENSE("GPL");