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>
38 #include <linux/rtc.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
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;
771 if (((abs(info->soca->soc - info->soca->displayed_soc)) > 10)
772 && (info->soca->Ibat_ave > -250)) {
774 Rsys_now = max(-info->soca->Rbat, Rsys_now);
776 Rsys_now = min(info->soca->Rbat, Rsys_now);
779 Ocv_ZeroPer_now = info->soca->target_vsys * 1000 - Ibat_now * Rsys_now;
781 RICOH_FG_DBG("PMU: ------- Ocv_now_table= %d: Rsys_now= %d =======\n",
782 Ocv_now_table, Rsys_now);
784 RICOH_FG_DBG("PMU: ------- Rsys= %d: cutoff_ocv= %d: Ocv_ZeroPer_now= %d =======\n",
785 info->soca->Rsys, info->soca->cutoff_ocv, Ocv_ZeroPer_now);
790 for (i = 1; i < 11; i++) {
791 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));
792 if (ocv_table[i] >= Ocv_ZeroPer_now / 100) {
794 start_per = Calc_Linear_Interpolation(
795 (i-1)*1000, ocv_table[i-1], i*1000,
796 ocv_table[i], (Ocv_ZeroPer_now / 100));
801 start_per = max(0, start_per);
803 FA_CAP_now = fa_cap * ((10000 - start_per) / 100 ) / 100;
805 RICOH_FG_DBG("PMU: -------Ocv_ZeroPer_now= %d: start_per= %d =======\n",
806 Ocv_ZeroPer_now, start_per);
809 RE_CAP_now = FA_CAP_now - use_cap;
811 if (RE_CAP_now < RE_CAP_GO_DOWN) {
812 info->soca->hurry_up_flg = 1;
813 } else if (info->soca->Vsys_ave < info->soca->target_vsys*1000) {
814 info->soca->hurry_up_flg = 1;
815 } else if (info->fg_poff_vbat != 0) {
816 if (info->soca->Vbat_ave < info->fg_poff_vbat*1000) {
817 info->soca->hurry_up_flg = 1;
819 info->soca->hurry_up_flg = 0;
822 info->soca->hurry_up_flg = 0;
825 /* get CC_OnePer_step */
826 if (info->soca->displayed_soc > 0) { /* avoid divide-by-0 */
827 CC_OnePer_step = RE_CAP_now / (info->soca->displayed_soc / 100 + 1);
831 /* get info->soca->target_use_cap */
832 info->soca->target_use_cap = use_cap + CC_OnePer_step;
834 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",
835 FA_CAP_now, RE_CAP_now, CC_OnePer_step, info->soca->target_use_cap, info->soca->hurry_up_flg);
839 #ifdef ENABLE_OCV_TABLE_CALIB
841 * Calibration OCV Table
842 * - Update the value of VBAT on 100% in OCV table
843 * if battery is Full charged.
844 * - int vbat_ocv <- unit is uV
846 static int calib_ocvTable(struct ricoh619_battery_info *info, int vbat_ocv)
855 RICOH_FG_DBG("PMU: %s\n", __func__);
857 if (info->soca->Ibat_ave > RICOH619_REL1_SEL_VALUE) {
858 RICOH_FG_DBG("PMU: %s IBAT > 64mA -- Not Calibration --\n", __func__);
862 if (vbat_ocv < info->soca->OCV100_max) {
863 if (vbat_ocv < info->soca->OCV100_min)
864 ocv100_new = info->soca->OCV100_min;
866 ocv100_new = vbat_ocv;
868 ocv100_new = info->soca->OCV100_max;
870 RICOH_FG_DBG("PMU : %s :max %d min %d current %d\n",__func__,info->soca->OCV100_max,info->soca->OCV100_min,vbat_ocv);
871 RICOH_FG_DBG("PMU : %s : New OCV 100% = 0x%x\n",__func__,ocv100_new);
874 ret = ricoh619_clr_bits(info->dev->parent, FG_CTRL_REG, 0x01);
876 dev_err("PMU: %s Error in FG_En OFF\n", __func__);
881 //cutoff_ocv = (battery_init_para[info->num][0]<<8) | (battery_init_para[info->num][1]);
882 cutoff_ocv = get_OCV_voltage(info, 0);
884 info->soca->ocv_table_def[10] = info->soca->OCV100_max;
886 ricoh619_scaling_OCV_table(info, cutoff_ocv/1000, ocv100_new/1000, &start_per, &end_per);
888 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
889 BAT_INIT_TOP_REG, 22, battery_init_para[info->num]);
891 dev_err(info->dev, "batterry initialize error\n");
895 for (i = 0; i <= 10; i = i+1) {
896 info->soca->ocv_table[i] = get_OCV_voltage(info, i);
897 RICOH_FG_DBG("PMU: %s : * %d0%% voltage = %d uV\n",
898 __func__, i, info->soca->ocv_table[i]);
901 /* FG_En on & Reset*/
902 ret = reset_FG_process(info);
904 dev_err("PMU: %s Error in FG_En On & Reset\n", __func__);
908 RICOH_FG_DBG("PMU: %s Exit \n", __func__);
916 static void ricoh619_displayed_work(struct work_struct *work)
924 int displayed_soc_temp;
927 bool is_charging = true;
928 int re_cap,fa_cap,use_cap;
929 bool is_jeita_updated;
938 struct ricoh619_battery_info *info = container_of(work,
939 struct ricoh619_battery_info, displayed_work.work);
941 if (info->entry_factory_mode) {
942 info->soca->status = RICOH619_SOCA_STABLE;
943 info->soca->displayed_soc = -EINVAL;
944 info->soca->ready_fg = 0;
948 mutex_lock(&info->lock);
950 is_jeita_updated = false;
952 if ((RICOH619_SOCA_START == info->soca->status)
953 || (RICOH619_SOCA_STABLE == info->soca->status)
954 || (RICOH619_SOCA_FULL == info->soca->status))
955 info->soca->ready_fg = 1;
957 /* judge Full state or Moni Vsys state */
958 if ((RICOH619_SOCA_DISP == info->soca->status)
959 || (RICOH619_SOCA_STABLE == info->soca->status))
962 temp_ocv = get_OCV_voltage(info, 10) -
963 (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))/2;
965 if(g_full_flag == 1){ /* for issue 1 solution start*/
966 info->soca->status = RICOH619_SOCA_FULL;
967 }else if ((POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
968 && (calc_ocv(info) > temp_ocv)) {
969 info->soca->status = RICOH619_SOCA_FULL;
971 } else if (info->soca->Ibat_ave >= -20) {
972 /* for issue1 solution end */
973 /* check Full state or not*/
974 if ((calc_ocv(info) > (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*7/10))
975 || (POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
976 || (info->soca->displayed_soc > 9850))
978 info->soca->status = RICOH619_SOCA_FULL;
980 } else if ((calc_ocv(info) > (get_OCV_voltage(info, 9)))
981 && (info->soca->Ibat_ave < 300))
983 info->soca->status = RICOH619_SOCA_FULL;
986 } else { /* dis-charging */
987 if (info->soca->displayed_soc/100 < RICOH619_ENTER_LOW_VOL) {
988 info->soca->target_use_cap = 0;
989 info->soca->status = RICOH619_SOCA_LOW_VOL;
994 if (RICOH619_SOCA_STABLE == info->soca->status) {
995 info->soca->soc = calc_capacity_2(info);
996 info->soca->soc_delta = info->soca->soc - info->soca->last_soc;
998 if (info->soca->soc_delta >= -100 && info->soca->soc_delta <= 100) {
999 info->soca->displayed_soc = info->soca->soc;
1001 info->soca->status = RICOH619_SOCA_DISP;
1003 info->soca->last_soc = info->soca->soc;
1004 info->soca->soc_delta = 0;
1005 } else if (RICOH619_SOCA_FULL == info->soca->status) {
1006 err = check_jeita_status(info, &is_jeita_updated);
1008 dev_err(info->dev, "Error in updating JEITA %d\n", err);
1011 info->soca->soc = calc_capacity(info) * 100;
1012 info->soca->last_soc = calc_capacity_2(info); /* for DISP */
1014 if (info->soca->Ibat_ave >= -20) { /* charging */
1015 if (0 == info->soca->jt_limit) {
1016 if (g_full_flag == 1) {
1018 if (POWER_SUPPLY_STATUS_FULL == info->soca->chg_status) {
1019 if(info->soca->full_reset_count < RICOH619_UPDATE_COUNT_FULL_RESET) {
1020 info->soca->full_reset_count++;
1021 } else if (info->soca->full_reset_count < (RICOH619_UPDATE_COUNT_FULL_RESET + 1)) {
1022 err = reset_FG_process(info);
1024 dev_err(info->dev, "Error in writing the control register\n");
1025 info->soca->full_reset_count++;
1027 } else if(info->soca->full_reset_count < (RICOH619_UPDATE_COUNT_FULL_RESET + 2)) {
1028 info->soca->full_reset_count++;
1029 info->soca->fc_cap = 0;
1030 info->soca->soc_full = info->soca->soc;
1033 if(info->soca->fc_cap < -1 * 200) {
1035 info->soca->displayed_soc = 99 * 100;
1037 info->soca->full_reset_count = 0;
1041 info->soca->chg_cmp_times = 0;
1042 err = calc_capacity_in_period(info, &cc_cap, &is_charging, true);
1044 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1046 fc_delta = (is_charging == true) ? cc_cap : -cc_cap;
1048 info->soca->fc_cap = info->soca->fc_cap + fc_delta;
1050 if (g_full_flag == 1){
1051 info->soca->displayed_soc = 100*100;
1054 if (calc_ocv(info) < (get_OCV_voltage(info, 8))) { /* fail safe*/
1056 info->soca->status = RICOH619_SOCA_DISP;
1057 info->soca->soc_delta = 0;
1058 } else if ((POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
1059 && (info->soca->displayed_soc >= 9890)){
1060 if(info->soca->chg_cmp_times > RICOH619_FULL_WAIT_TIME) {
1061 info->soca->displayed_soc = 100*100;
1063 info->soca->full_reset_count = 0;
1064 info->soca->soc_full = info->soca->soc;
1065 info->soca->fc_cap = 0;
1066 #ifdef ENABLE_OCV_TABLE_CALIB
1067 err = calib_ocvTable(info,calc_ocv(info));
1069 dev_err(info->dev, "Calibration OCV Error !!\n");
1072 info->soca->chg_cmp_times++;
1075 fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
1078 if (info->soca->displayed_soc >= 9950) {
1079 if((info->soca->soc_full - info->soca->soc) < 200) {
1083 info->soca->chg_cmp_times = 0;
1085 err = calc_capacity_in_period(info, &cc_cap, &is_charging, true);
1087 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1088 info->soca->cc_delta
1089 = (is_charging == true) ? cc_cap : -cc_cap;
1091 if((POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
1092 // || (info->soca->Ibat_ave > 200))
1093 || (info->soca->Ibat_ave < info->ch_icchg*50 + 100) || (info->soca->displayed_soc<9700))
1095 info->soca->displayed_soc += 13 * 3000 / fa_cap;
1098 info->soca->displayed_soc
1099 = info->soca->displayed_soc + info->soca->cc_delta*8/10;
1102 info->soca->displayed_soc
1103 = min(10000, info->soca->displayed_soc);
1104 info->soca->displayed_soc = max(0, info->soca->displayed_soc);
1106 if (info->soca->displayed_soc >= 9890) {
1107 info->soca->displayed_soc = 99 * 100;
1112 info->soca->full_reset_count = 0;
1114 } else { /* discharging */
1115 if (info->soca->displayed_soc >= 9950) {
1116 if (info->soca->Ibat_ave <= -1 * RICOH619_REL1_SEL_VALUE) {
1117 if ((calc_ocv(info) < (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*3/10))
1118 || ((info->soca->soc_full - info->soca->soc) > 200)) {
1121 info->soca->full_reset_count = 0;
1122 info->soca->displayed_soc = 100 * 100;
1123 info->soca->status = RICOH619_SOCA_DISP;
1124 info->soca->last_soc = info->soca->soc;
1125 info->soca->soc_delta = 0;
1127 info->soca->displayed_soc = 100 * 100;
1129 } else { /* into relaxation state */
1130 ricoh619_read(info->dev->parent, CHGSTATE_REG, ®_val);
1131 if (reg_val & 0xc0) {
1132 info->soca->displayed_soc = 100 * 100;
1135 info->soca->full_reset_count = 0;
1136 info->soca->displayed_soc = 100 * 100;
1137 info->soca->status = RICOH619_SOCA_DISP;
1138 info->soca->last_soc = info->soca->soc;
1139 info->soca->soc_delta = 0;
1144 info->soca->status = RICOH619_SOCA_DISP;
1145 info->soca->soc_delta = 0;
1146 info->soca->full_reset_count = 0;
1147 info->soca->last_soc = info->soca->soc;
1150 } else if (RICOH619_SOCA_LOW_VOL == info->soca->status) {
1151 if(info->soca->Ibat_ave >= 0) {
1152 info->soca->soc = calc_capacity(info) * 100;
1153 info->soca->status = RICOH619_SOCA_DISP;
1154 info->soca->last_soc = info->soca->soc;
1155 info->soca->soc_delta = 0;
1157 re_cap = get_check_fuel_gauge_reg(info, RE_CAP_H_REG, RE_CAP_L_REG,
1159 fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
1161 use_cap = fa_cap - re_cap;
1163 if (info->soca->target_use_cap == 0) {
1164 info->soca->re_cap_old = re_cap;
1165 get_target_use_cap(info);
1168 if(use_cap >= info->soca->target_use_cap) {
1169 info->soca->displayed_soc = info->soca->displayed_soc - 100;
1170 info->soca->displayed_soc = max(0, info->soca->displayed_soc);
1171 info->soca->re_cap_old = re_cap;
1172 } else if (info->soca->hurry_up_flg == 1) {
1173 info->soca->displayed_soc = info->soca->displayed_soc - 100;
1174 info->soca->displayed_soc = max(0, info->soca->displayed_soc);
1175 info->soca->re_cap_old = re_cap;
1177 get_target_use_cap(info);
1178 info->soca->soc = calc_capacity(info) * 100;
1181 else if (RICOH619_SOCA_DISP == info->soca->status) {
1183 info->soca->soc = calc_capacity_2(info);
1185 soc_round = (info->soca->soc + 50) / 100;
1186 last_soc_round = (info->soca->last_soc + 50) / 100;
1187 last_disp_round = (info->soca->displayed_soc + 50) / 100;
1189 info->soca->soc_delta =
1190 info->soca->soc_delta + (info->soca->soc - info->soca->last_soc);
1192 info->soca->last_soc = info->soca->soc;
1194 if (last_disp_round == soc_round) {
1195 /* if SOC == DISPLAY move to stable */
1196 info->soca->displayed_soc = info->soca->soc ;
1197 info->soca->status = RICOH619_SOCA_STABLE;
1199 } else if (info->soca->Ibat_ave > 0) {
1200 if ((0 == info->soca->jt_limit) ||
1201 (POWER_SUPPLY_STATUS_FULL != info->soca->chg_status)) {
1203 if (last_disp_round < soc_round) {
1204 /* Case 1 : Charge, Display < SOC */
1205 if (info->soca->soc_delta >= 100) {
1206 info->soca->displayed_soc
1207 = last_disp_round * 100 + 50;
1208 info->soca->soc_delta -= 100;
1209 if (info->soca->soc_delta >= 100)
1212 info->soca->displayed_soc += 25;
1213 disp_dec = info->soca->displayed_soc % 100;
1214 if ((50 <= disp_dec) && (disp_dec <= 74))
1215 info->soca->soc_delta = 0;
1217 if ((info->soca->displayed_soc + 50)/100
1219 info->soca->displayed_soc
1222 = RICOH619_SOCA_STABLE;
1225 } else if (last_disp_round > soc_round) {
1226 /* Case 2 : Charge, Display > SOC */
1227 if (info->soca->soc_delta >= 300) {
1228 info->soca->displayed_soc += 100;
1229 info->soca->soc_delta -= 300;
1231 if ((info->soca->displayed_soc + 50)/100
1233 info->soca->displayed_soc
1236 = RICOH619_SOCA_STABLE;
1241 info->soca->soc_delta = 0;
1245 if (last_disp_round > soc_round) {
1246 /* Case 3 : Dis-Charge, Display > SOC */
1247 if (info->soca->soc_delta <= -100) {
1248 info->soca->displayed_soc
1249 = last_disp_round * 100 - 75;
1250 info->soca->soc_delta += 100;
1251 if (info->soca->soc_delta <= -100)
1254 info->soca->displayed_soc -= 25;
1255 disp_dec = info->soca->displayed_soc % 100;
1256 if ((25 <= disp_dec) && (disp_dec <= 49))
1257 info->soca->soc_delta = 0;
1259 if ((info->soca->displayed_soc + 50)/100
1261 info->soca->displayed_soc
1264 = RICOH619_SOCA_STABLE;
1267 } else if (last_disp_round < soc_round) {
1268 /* Case 4 : Dis-Charge, Display < SOC */
1269 if (info->soca->soc_delta <= -300) {
1270 info->soca->displayed_soc -= 100;
1271 info->soca->soc_delta += 300;
1273 if ((info->soca->displayed_soc + 50)/100
1275 info->soca->displayed_soc
1278 = RICOH619_SOCA_STABLE;
1283 } else if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
1285 temp_ocv = get_OCV_voltage(info, 10) -
1286 (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))/2;
1288 if(g_full_flag == 1){ /* for issue 1 solution start*/
1289 info->soca->status = RICOH619_SOCA_FULL;
1290 err = reset_FG_process(info);
1292 dev_err(info->dev, "Error in writing the control register\n");
1295 }else if ((POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
1296 && (calc_ocv(info) > temp_ocv)) {
1297 info->soca->status = RICOH619_SOCA_FULL;
1299 err = reset_FG_process(info);
1301 dev_err(info->dev, "Error in writing the control register\n");
1303 } else if (info->soca->Ibat_ave >= -20) {
1304 /* for issue1 solution end */
1305 /* check Full state or not*/
1306 if ((calc_ocv(info) > (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*7/10))
1307 || (POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
1308 || (info->soca->displayed_soc > 9850))
1310 info->soca->status = RICOH619_SOCA_FULL;
1312 err = reset_FG_process(info);
1314 dev_err(info->dev, "Error in writing the control register\n");
1316 } else if ((calc_ocv(info) > (get_OCV_voltage(info, 9)))
1317 && (info->soca->Ibat_ave < 300))
1319 info->soca->status = RICOH619_SOCA_FULL;
1321 err = reset_FG_process(info);
1323 dev_err(info->dev, "Error in writing the control register\n");
1328 err = ricoh619_read(info->dev->parent, PSWR_REG, &val);
1330 info->soca->soc = val * 100;
1333 "Error in reading PSWR_REG %d\n", err);
1335 = calc_capacity(info) * 100;
1338 err = calc_capacity_in_period(info, &cc_cap,
1339 &is_charging, false);
1341 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1343 info->soca->cc_delta
1344 = (is_charging == true) ? cc_cap : -cc_cap;
1347 = info->soca->soc + info->soca->cc_delta;
1348 if (displayed_soc_temp < 0)
1349 displayed_soc_temp = 0;
1351 = min(9850, displayed_soc_temp);
1352 displayed_soc_temp = max(0, displayed_soc_temp);
1354 info->soca->displayed_soc = displayed_soc_temp;
1356 } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
1358 } else if (RICOH619_SOCA_START == info->soca->status) {
1360 err = measure_Ibatt_FG(info, &Ibat);
1361 err = measure_vbatt_FG(info, &Vbat);
1362 err = measure_vsys_ADC(info, &Vsys);
1364 info->soca->Ibat_ave = Ibat;
1365 info->soca->Vbat_ave = Vbat;
1366 info->soca->Vsys_ave = Vsys;
1368 err = check_jeita_status(info, &is_jeita_updated);
1369 is_jeita_updated = false;
1371 dev_err(info->dev, "Error in updating JEITA %d\n", err);
1373 err = ricoh619_read(info->dev->parent, PSWR_REG, &val);
1375 if (info->first_pwon) {
1376 info->soca->soc = calc_capacity(info) * 100;
1377 val = (info->soca->soc + 50)/100;
1379 err = ricoh619_write(info->dev->parent, PSWR_REG, val);
1381 dev_err(info->dev, "Error in writing PSWR_REG\n");
1384 if ((info->soca->soc == 0) && (calc_ocv(info)
1385 < get_OCV_voltage(info, 0))) {
1386 info->soca->displayed_soc = 0;
1387 info->soca->status = RICOH619_SOCA_ZERO;
1389 if (0 == info->soca->jt_limit) {
1390 check_charge_status_2(info, info->soca->soc);
1392 info->soca->displayed_soc = info->soca->soc;
1395 if (info->soca->displayed_soc < 300) {
1396 info->soca->target_use_cap = 0;
1397 info->soca->status = RICOH619_SOCA_LOW_VOL;
1399 if ((info->fg_poff_vbat != 0)
1400 && (Vbat < info->fg_poff_vbat * 1000) ){
1401 info->soca->target_use_cap = 0;
1402 info->soca->status = RICOH619_SOCA_LOW_VOL;
1404 info->soca->status = RICOH619_SOCA_UNSTABLE;
1408 info->soca->status = RICOH619_SOCA_UNSTABLE;
1411 } else if (g_fg_on_mode && (val == 0x7f)) {
1412 info->soca->soc = calc_capacity(info) * 100;
1413 if ((info->soca->soc == 0) && (calc_ocv(info)
1414 < get_OCV_voltage(info, 0))) {
1415 info->soca->displayed_soc = 0;
1416 info->soca->status = RICOH619_SOCA_ZERO;
1418 if (0 == info->soca->jt_limit) {
1419 check_charge_status_2(info, info->soca->soc);
1421 info->soca->displayed_soc = info->soca->soc;
1423 info->soca->last_soc = info->soca->soc;
1424 info->soca->status = RICOH619_SOCA_STABLE;
1427 info->soca->soc = val * 100;
1430 "Error in reading PSWR_REG %d\n", err);
1432 = calc_capacity(info) * 100;
1435 err = calc_capacity_in_period(info, &cc_cap,
1436 &is_charging, false);
1438 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1440 info->soca->cc_delta
1441 = (is_charging == true) ? cc_cap : -cc_cap;
1442 if (calc_ocv(info) < get_OCV_voltage(info, 0)) {
1443 info->soca->displayed_soc = 0;
1444 info->soca->status = RICOH619_SOCA_ZERO;
1447 = info->soca->soc + info->soca->cc_delta;
1448 if (displayed_soc_temp < 0)
1449 displayed_soc_temp = 0;
1451 = min(10000, displayed_soc_temp);
1452 displayed_soc_temp = max(0, displayed_soc_temp);
1453 if (0 == info->soca->jt_limit) {
1454 check_charge_status_2(info, displayed_soc_temp);
1456 info->soca->displayed_soc = displayed_soc_temp;
1458 info->soca->last_soc = calc_capacity(info) * 100;
1460 if (info->soca->displayed_soc < 300) {
1461 info->soca->target_use_cap = 0;
1462 info->soca->status = RICOH619_SOCA_LOW_VOL;
1464 if ((info->fg_poff_vbat != 0)
1465 && (Vbat < info->fg_poff_vbat * 1000)){
1466 info->soca->target_use_cap = 0;
1467 info->soca->status = RICOH619_SOCA_LOW_VOL;
1469 info->soca->status = RICOH619_SOCA_UNSTABLE;
1473 if(info->soca->displayed_soc >= 9850)
1475 info->soca->displayed_soc = 10000;
1476 info->chg_complete_tm_ov_flag = 1;
1478 info->soca->status = RICOH619_SOCA_UNSTABLE;
1482 } else if (RICOH619_SOCA_ZERO == info->soca->status) {
1483 if (calc_ocv(info) > get_OCV_voltage(info, 0)) {
1484 err = reset_FG_process(info);
1486 dev_err(info->dev, "Error in writing the control register\n");
1487 info->soca->last_soc = calc_capacity_2(info);
1488 info->soca->status = RICOH619_SOCA_STABLE;
1490 info->soca->displayed_soc = 0;
1493 /* keep DSOC = 1 when Vbat is over 3.4V*/
1494 if( info->fg_poff_vbat != 0) {
1495 if (info->soca->zero_flg == 1) {
1496 if(info->soca->Ibat_ave >= 0) {
1497 info->soca->zero_flg = 0;
1499 info->soca->displayed_soc = 0;
1500 } else if (info->soca->displayed_soc < 50) {
1501 if (info->soca->Vbat_ave < 2000*1000) { /* error value */
1502 info->soca->displayed_soc = 100;
1503 } else if (info->soca->Vbat_ave < info->fg_poff_vbat*1000) {
1504 info->soca->displayed_soc = 0;
1505 info->soca->zero_flg = 1;
1507 info->soca->displayed_soc = 100;
1513 && (info->soca->status == RICOH619_SOCA_STABLE)) {
1514 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
1516 dev_err(info->dev, "Error in writing PSWR_REG\n");
1518 err = calc_capacity_in_period(info, &cc_cap,
1519 &is_charging, true);
1521 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1523 } else if (RICOH619_SOCA_UNSTABLE != info->soca->status) {
1524 if ((info->soca->displayed_soc + 50) / 100 <= 1) {
1527 val = (info->soca->displayed_soc + 50)/100;
1530 err = ricoh619_write(info->dev->parent, PSWR_REG, val);
1532 dev_err(info->dev, "Error in writing PSWR_REG\n");
1536 err = calc_capacity_in_period(info, &cc_cap,
1537 &is_charging, true);
1539 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1542 RICOH_FG_DBG("PMU: ------- STATUS= %d: IBAT= %d: VSYS= %d: VBAT= %d: DSOC= %d: RSOC= %d: -------\n",
1543 info->soca->status, info->soca->Ibat_ave, info->soca->Vsys_ave, info->soca->Vbat_ave,
1544 info->soca->displayed_soc, info->soca->soc);
1546 #ifdef DISABLE_CHARGER_TIMER
1547 /* clear charger timer */
1548 if ( info->soca->chg_status == POWER_SUPPLY_STATUS_CHARGING ) {
1549 err = ricoh619_read(info->dev->parent, TIMSET_REG, &val);
1552 "Error in read TIMSET_REG%d\n", err);
1553 /* to check bit 0-1 */
1557 /* set rapid timer 240 -> 300 */
1558 err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x03);
1560 dev_err(info->dev, "Error in writing the control register\n");
1563 /* set rapid timer 300 -> 240 */
1564 err = ricoh619_clr_bits(info->dev->parent, TIMSET_REG, 0x01);
1565 err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x02);
1567 dev_err(info->dev, "Error in writing the control register\n");
1573 if (0 == info->soca->ready_fg)
1574 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1575 RICOH619_FG_RESET_TIME * HZ);
1576 else if (delay_flag == 1)
1577 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1578 RICOH619_DELAY_TIME * HZ);
1579 else if (RICOH619_SOCA_DISP == info->soca->status)
1580 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1581 RICOH619_DISPLAY_UPDATE_TIME * HZ);
1582 else if (info->soca->hurry_up_flg == 1)
1583 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1584 RICOH619_LOW_VOL_DOWN_TIME * HZ);
1586 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1587 RICOH619_DISPLAY_UPDATE_TIME * HZ);
1589 mutex_unlock(&info->lock);
1591 if((true == is_jeita_updated)
1592 || (info->soca->last_displayed_soc/100 != (info->soca->displayed_soc+50)/100))
1593 power_supply_changed(&info->battery);
1595 info->soca->last_displayed_soc = info->soca->displayed_soc+50;
1597 if ((info->soca->displayed_soc >= 9850) && (info->soca->Ibat_ave > -20) && (info->capacity < 100)
1598 && (info->soca->chg_status == POWER_SUPPLY_STATUS_CHARGING))
1600 if(info->chg_complete_rd_flag == 0)
1602 info->chg_complete_rd_flag = 1;
1603 info->chg_complete_rd_cnt = 0;
1604 queue_delayed_work(info->monitor_wqueue, &info->charge_complete_ready, 0);
1609 info->chg_complete_rd_flag = 0;
1612 if(info->chg_complete_tm_ov_flag == 1)
1614 if(info->soca->displayed_soc < 9850 || info->soca->Ibat_ave < -20)
1616 info->chg_complete_tm_ov_flag = 0;
1617 power_supply_changed(&info->battery);
1623 static void ricoh619_stable_charge_countdown_work(struct work_struct *work)
1629 struct ricoh619_battery_info *info = container_of(work,
1630 struct ricoh619_battery_info, charge_stable_work.work);
1632 if (info->entry_factory_mode)
1635 mutex_lock(&info->lock);
1636 if (RICOH619_SOCA_FG_RESET == info->soca->status)
1637 info->soca->ready_fg = 1;
1639 if (2 <= info->soca->stable_count) {
1640 if (3 == info->soca->stable_count
1641 && RICOH619_SOCA_FG_RESET == info->soca->status) {
1642 ret = reset_FG_process(info);
1644 dev_err(info->dev, "Error in writing the control register\n");
1646 info->soca->stable_count = info->soca->stable_count - 1;
1647 queue_delayed_work(info->monitor_wqueue,
1648 &info->charge_stable_work,
1649 RICOH619_FG_STABLE_TIME * HZ / 10);
1650 } else if (0 >= info->soca->stable_count) {
1651 /* Finished queue, ignore */
1652 } else if (1 == info->soca->stable_count) {
1653 if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
1654 /* Judge if FG need reset or Not */
1655 info->soca->soc = calc_capacity(info) * 100;
1656 if (info->chg_ctr != 0) {
1657 queue_delayed_work(info->monitor_wqueue,
1658 &info->charge_stable_work,
1659 RICOH619_FG_STABLE_TIME * HZ / 10);
1660 mutex_unlock(&info->lock);
1663 /* Do reset setting */
1664 ret = reset_FG_process(info);
1666 dev_err(info->dev, "Error in writing the control register\n");
1668 info->soca->status = RICOH619_SOCA_FG_RESET;
1670 /* Delay for addition Reset Time (6s) */
1671 queue_delayed_work(info->monitor_wqueue,
1672 &info->charge_stable_work,
1673 RICOH619_FG_RESET_TIME*HZ);
1674 } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
1675 info->soca->reset_soc[2] = info->soca->reset_soc[1];
1676 info->soca->reset_soc[1] = info->soca->reset_soc[0];
1677 info->soca->reset_soc[0] = calc_capacity(info) * 100;
1678 info->soca->reset_count++;
1680 if (info->soca->reset_count > 10) {
1681 /* Reset finished; */
1682 info->soca->soc = info->soca->reset_soc[0];
1683 info->soca->stable_count = 0;
1687 for (i = 0; i < 3; i++) {
1688 if (max < info->soca->reset_soc[i]/100)
1689 max = info->soca->reset_soc[i]/100;
1690 if (min > info->soca->reset_soc[i]/100)
1691 min = info->soca->reset_soc[i]/100;
1694 if ((info->soca->reset_count > 3) && ((max - min)
1695 < RICOH619_MAX_RESET_SOC_DIFF)) {
1696 /* Reset finished; */
1697 info->soca->soc = info->soca->reset_soc[0];
1698 info->soca->stable_count = 0;
1701 /* Do reset setting */
1702 ret = reset_FG_process(info);
1704 dev_err(info->dev, "Error in writing the control register\n");
1706 /* Delay for addition Reset Time (6s) */
1707 queue_delayed_work(info->monitor_wqueue,
1708 &info->charge_stable_work,
1709 RICOH619_FG_RESET_TIME*HZ);
1711 /* Finished queue From now, select FG as result; */
1712 } else if (RICOH619_SOCA_START == info->soca->status) {
1713 /* Normal condition */
1714 } else { /* other state ZERO/DISP/STABLE */
1715 info->soca->stable_count = 0;
1718 mutex_unlock(&info->lock);
1722 info->soca->last_soc = info->soca->soc;
1723 info->soca->status = RICOH619_SOCA_DISP;
1724 info->soca->soc_delta = 0;
1727 mutex_unlock(&info->lock);
1731 static void ricoh619_charge_monitor_work(struct work_struct *work)
1733 struct ricoh619_battery_info *info = container_of(work,
1734 struct ricoh619_battery_info, charge_monitor_work.work);
1736 get_power_supply_status(info);
1738 if (POWER_SUPPLY_STATUS_DISCHARGING == info->soca->chg_status
1739 || POWER_SUPPLY_STATUS_NOT_CHARGING == info->soca->chg_status) {
1740 switch (info->soca->dischg_state) {
1742 info->soca->dischg_state = 1;
1745 info->soca->dischg_state = 2;
1753 info->soca->dischg_state = 0;
1756 queue_delayed_work(info->monitor_wqueue, &info->charge_monitor_work,
1757 RICOH619_CHARGE_MONITOR_TIME * HZ);
1762 static void ricoh619_get_charge_work(struct work_struct *work)
1764 struct ricoh619_battery_info *info = container_of(work,
1765 struct ricoh619_battery_info, get_charge_work.work);
1767 int Vbat_temp, Vsys_temp, Ibat_temp;
1768 int Vbat_sort[RICOH619_GET_CHARGE_NUM];
1769 int Vsys_sort[RICOH619_GET_CHARGE_NUM];
1770 int Ibat_sort[RICOH619_GET_CHARGE_NUM];
1774 mutex_lock(&info->lock);
1776 for (i = RICOH619_GET_CHARGE_NUM-1; i > 0; i--) {
1777 if (0 == info->soca->chg_count) {
1778 info->soca->Vbat[i] = 0;
1779 info->soca->Vsys[i] = 0;
1780 info->soca->Ibat[i] = 0;
1782 info->soca->Vbat[i] = info->soca->Vbat[i-1];
1783 info->soca->Vsys[i] = info->soca->Vsys[i-1];
1784 info->soca->Ibat[i] = info->soca->Ibat[i-1];
1788 ret = measure_vbatt_FG(info, &info->soca->Vbat[0]);
1789 ret = measure_vsys_ADC(info, &info->soca->Vsys[0]);
1790 ret = measure_Ibatt_FG(info, &info->soca->Ibat[0]);
1792 info->soca->chg_count++;
1794 if (RICOH619_GET_CHARGE_NUM != info->soca->chg_count) {
1795 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1796 RICOH619_CHARGE_CALC_TIME * HZ);
1797 mutex_unlock(&info->lock);
1801 for (i = 0; i < RICOH619_GET_CHARGE_NUM; i++) {
1802 Vbat_sort[i] = info->soca->Vbat[i];
1803 Vsys_sort[i] = info->soca->Vsys[i];
1804 Ibat_sort[i] = info->soca->Ibat[i];
1810 for (i = 0; i < RICOH619_GET_CHARGE_NUM - 1; i++) {
1811 for (j = RICOH619_GET_CHARGE_NUM - 1; j > i; j--) {
1812 if (Vbat_sort[j - 1] > Vbat_sort[j]) {
1813 Vbat_temp = Vbat_sort[j];
1814 Vbat_sort[j] = Vbat_sort[j - 1];
1815 Vbat_sort[j - 1] = Vbat_temp;
1817 if (Vsys_sort[j - 1] > Vsys_sort[j]) {
1818 Vsys_temp = Vsys_sort[j];
1819 Vsys_sort[j] = Vsys_sort[j - 1];
1820 Vsys_sort[j - 1] = Vsys_temp;
1822 if (Ibat_sort[j - 1] > Ibat_sort[j]) {
1823 Ibat_temp = Ibat_sort[j];
1824 Ibat_sort[j] = Ibat_sort[j - 1];
1825 Ibat_sort[j - 1] = Ibat_temp;
1833 for (i = 3; i < RICOH619_GET_CHARGE_NUM-3; i++) {
1834 Vbat_temp = Vbat_temp + Vbat_sort[i];
1835 Vsys_temp = Vsys_temp + Vsys_sort[i];
1836 Ibat_temp = Ibat_temp + Ibat_sort[i];
1838 Vbat_temp = Vbat_temp / (RICOH619_GET_CHARGE_NUM - 6);
1839 Vsys_temp = Vsys_temp / (RICOH619_GET_CHARGE_NUM - 6);
1840 Ibat_temp = Ibat_temp / (RICOH619_GET_CHARGE_NUM - 6);
1842 if (0 == info->soca->chg_count) {
1843 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1844 RICOH619_CHARGE_UPDATE_TIME * HZ);
1845 mutex_unlock(&info->lock);
1848 info->soca->Vbat_ave = Vbat_temp;
1849 info->soca->Vsys_ave = Vsys_temp;
1850 info->soca->Ibat_ave = Ibat_temp;
1853 info->soca->chg_count = 0;
1854 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1855 RICOH619_CHARGE_UPDATE_TIME * HZ);
1856 mutex_unlock(&info->lock);
1860 /* Initial setting of FuelGauge SOCA function */
1861 static int ricoh619_init_fgsoca(struct ricoh619_battery_info *info)
1867 bool is_charging = true;
1869 for (i = 0; i <= 10; i = i+1) {
1870 info->soca->ocv_table[i] = get_OCV_voltage(info, i);
1871 RICOH_FG_DBG("PMU: %s : * %d0%% voltage = %d uV\n",
1872 __func__, i, info->soca->ocv_table[i]);
1875 for (i = 0; i < 3; i = i+1)
1876 info->soca->reset_soc[i] = 0;
1877 info->soca->reset_count = 0;
1879 if (info->first_pwon) {
1881 err = ricoh619_read(info->dev->parent, CHGISET_REG, &val);
1884 "Error in read CHGISET_REG%d\n", err);
1886 err = ricoh619_write(info->dev->parent, CHGISET_REG, 0);
1889 "Error in writing CHGISET_REG%d\n", err);
1892 if (!info->entry_factory_mode) {
1893 err = ricoh619_write(info->dev->parent,
1896 dev_err(info->dev, "Error in writing the control register\n");
1899 err = calc_capacity_in_period(info, &cc_cap, &is_charging, true);
1903 err = ricoh619_write(info->dev->parent, CHGISET_REG, val);
1906 "Error in writing CHGISET_REG%d\n", err);
1909 /* Rbat : Transfer */
1910 info->soca->Rbat = get_OCV_init_Data(info, 12) * 1000 / 512
1912 info->soca->n_cap = get_OCV_init_Data(info, 11);
1915 info->soca->displayed_soc = 0;
1916 info->soca->last_displayed_soc = 0;
1917 info->soca->suspend_soc = 0;
1918 info->soca->ready_fg = 0;
1919 info->soca->soc_delta = 0;
1920 info->soca->full_reset_count = 0;
1921 info->soca->soc_full = 0;
1922 info->soca->fc_cap = 0;
1923 info->soca->status = RICOH619_SOCA_START;
1924 /* stable count down 11->2, 1: reset; 0: Finished; */
1925 info->soca->stable_count = 11;
1926 info->soca->chg_cmp_times = 0;
1927 info->soca->dischg_state = 0;
1928 info->soca->Vbat_ave = 0;
1929 info->soca->Vbat_old = 0;
1930 info->soca->Vsys_ave = 0;
1931 info->soca->Ibat_ave = 0;
1932 info->soca->chg_count = 0;
1933 info->soca->target_use_cap = 0;
1934 info->soca->hurry_up_flg = 0;
1935 info->soca->re_cap_old = 0;
1936 info->soca->jt_limit = 0;
1937 info->soca->zero_flg = 0;
1938 info->soca->cc_cap_offset = 0;
1940 for (i = 0; i < 11; i++) {
1941 info->soca->ocv_table_low[i] = 0;
1944 for (i = 0; i < RICOH619_GET_CHARGE_NUM; i++) {
1945 info->soca->Vbat[i] = 0;
1946 info->soca->Vsys[i] = 0;
1947 info->soca->Ibat[i] = 0;
1952 #ifdef ENABLE_FG_KEEP_ON_MODE
1958 /* Start first Display job */
1959 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1960 RICOH619_FG_RESET_TIME*HZ);
1962 /* Start first Waiting stable job */
1963 queue_delayed_work(info->monitor_wqueue, &info->charge_stable_work,
1964 RICOH619_FG_STABLE_TIME*HZ/10);
1966 queue_delayed_work(info->monitor_wqueue, &info->charge_monitor_work,
1967 RICOH619_CHARGE_MONITOR_TIME * HZ);
1969 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1970 RICOH619_CHARGE_MONITOR_TIME * HZ);
1972 if (info->jt_hw_sw) {
1973 /* Enable JEITA function supported by H/W */
1974 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x04);
1976 dev_err(info->dev, "Error in writing the control register\n");
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");
1982 queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
1983 RICOH619_FG_RESET_TIME * HZ);
1986 /* Disable JEITA function supported by H/W */
1987 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x04);
1989 dev_err(info->dev, "Error in writing the control register\n");
1992 RICOH_FG_DBG("PMU: %s : * Rbat = %d mOhm n_cap = %d mAH\n",
1993 __func__, info->soca->Rbat, info->soca->n_cap);
1998 static void ricoh619_charging_complete_work(struct work_struct *work)
2000 struct ricoh619_battery_info *info = container_of(work,
2001 struct ricoh619_battery_info, charge_complete_ready.work);
2003 uint8_t time_ov_flag;
2004 RICOH_FG_DBG("PMU: %s\n", __func__);
2005 RICOH_FG_DBG("info->chg_complete_rd_cnt = %d\n", info->chg_complete_rd_cnt);
2006 RICOH_FG_DBG("info->chg_complete_rd_flag = %d\n", info->chg_complete_rd_flag);
2007 RICOH_FG_DBG("info->chg_complete_tm_ov_flag = %d\n", info->chg_complete_tm_ov_flag);
2009 if(info->chg_complete_rd_flag == 1)
2011 // start chg 99per to 100per timer
2013 info->chg_complete_rd_flag = 2;
2014 info->chg_complete_tm_ov_flag = 0;
2018 if(info->capacity == 100)
2020 // battery arriver to 100% earlier than time ov
2022 info->chg_complete_rd_cnt = 0;
2023 info->chg_complete_tm_ov_flag = 1;
2025 else if(info->chg_complete_rd_cnt > RICOH619_TIME_CHG_COUNT)
2027 // chg timer ov before cap arrive to 100%
2029 info->chg_complete_tm_ov_flag = 1;
2030 info->chg_complete_rd_cnt = 0;
2031 info->soca->status = RICOH619_SOCA_FULL;
2032 power_supply_changed(&info->battery);
2037 info->chg_complete_tm_ov_flag = 0;
2041 if ((time_ov_flag == 0) && (info->soca->chg_status == POWER_SUPPLY_STATUS_CHARGING))
2043 info->chg_complete_rd_cnt++;
2044 queue_delayed_work(info->monitor_wqueue, &info->charge_complete_ready,
2045 RICOH619_TIME_CHG_STEP);
2049 info->chg_complete_rd_flag = 0;
2052 RICOH_FG_DBG("PMU2: %s return\n", __func__);
2053 RICOH_FG_DBG("info->chg_complete_rd_cnt = %d\n", info->chg_complete_rd_cnt);
2054 RICOH_FG_DBG("info->chg_complete_rd_flag = %d\n", info->chg_complete_rd_flag);
2055 RICOH_FG_DBG("info->chg_complete_tm_ov_flag = %d\n", info->chg_complete_tm_ov_flag);
2056 RICOH_FG_DBG("time_ov_flag = %d\n", time_ov_flag);
2059 static void ricoh619_changed_work(struct work_struct *work)
2061 struct ricoh619_battery_info *info = container_of(work,
2062 struct ricoh619_battery_info, changed_work.work);
2064 RICOH_FG_DBG("PMU: %s\n", __func__);
2065 power_supply_changed(&info->battery);
2070 static int check_jeita_status(struct ricoh619_battery_info *info, bool *is_jeita_updated)
2071 /* JEITA Parameter settings
2075 * jt_vfchg_h~+~~~~~~~~~~~~~~~~~~~+
2077 * jt_vfchg_l-| - - - - - - - - - +~~~~~~~~~~+
2079 * -------0--+-------------------+----------+--- Temp
2083 * jt_ichg_h-+ - -+~~~~~~~~~~~~~~+~~~~~~~~~~+
2085 * jt_ichg_l-+~~~~+ Charge area |
2087 * 0--+----+--------------+----------+--- Temp
2088 * 0 jt_temp_l jt_temp_h 55
2094 uint8_t chgiset_org;
2095 uint8_t batset2_org;
2096 uint8_t set_vchg_h, set_vchg_l;
2097 uint8_t set_ichg_h, set_ichg_l;
2099 *is_jeita_updated = false;
2101 /* No execute if JEITA disabled */
2102 if (!info->jt_en || info->jt_hw_sw)
2105 /* Check FG Reset */
2106 if (info->soca->ready_fg) {
2107 temp = get_battery_temp_2(info) / 10;
2109 RICOH_FG_DBG(KERN_INFO "JEITA: %s *** cannot update by resetting FG ******\n", __func__);
2114 err = ricoh619_read(info->dev->parent, BATSET2_REG, &batset2_org);
2116 dev_err(info->dev, "Error in readng the battery setting register\n");
2119 vfchg = (batset2_org & 0x70) >> 4;
2120 batset2_org &= 0x8F;
2123 err = ricoh619_read(info->dev->parent, CHGISET_REG, &chgiset_org);
2125 dev_err(info->dev, "Error in readng the chrage setting register\n");
2128 chgiset_org &= 0xC0;
2130 set_ichg_h = (uint8_t)(chgiset_org | info->jt_ichg_h);
2131 set_ichg_l = (uint8_t)(chgiset_org | info->jt_ichg_l);
2133 set_vchg_h = (uint8_t)((info->jt_vfchg_h << 4) | batset2_org);
2134 set_vchg_l = (uint8_t)((info->jt_vfchg_l << 4) | batset2_org);
2136 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Temperature: %d, vfchg: %d, SW status: %d, chg_status: %d ******\n",
2137 __func__, temp, vfchg, info->soca->status, info->soca->chg_status);
2139 if (temp <= 0 || 55 <= temp) {
2140 /* 1st and 5th temperature ranges (~0, 55~) */
2141 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Temp(%d) is out of 0-55 ******\n", __func__, temp);
2142 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2144 dev_err(info->dev, "Error in writing the control register\n");
2147 info->soca->jt_limit = 0;
2148 *is_jeita_updated = true;
2149 } else if (temp < info->jt_temp_l) {
2150 /* 2nd temperature range (0~12) */
2151 if (vfchg != info->jt_vfchg_h) {
2152 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 0<Temp<12, update to vfchg=%d ******\n",
2153 __func__, info->jt_vfchg_h);
2154 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2156 dev_err(info->dev, "Error in writing the control register\n");
2160 /* set VFCHG/VRCHG */
2161 err = ricoh619_write(info->dev->parent,
2162 BATSET2_REG, set_vchg_h);
2164 dev_err(info->dev, "Error in writing the battery setting register\n");
2167 info->soca->jt_limit = 0;
2168 *is_jeita_updated = true;
2170 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 0<Temp<50, already set vfchg=%d, so no need to update ******\n",
2171 __func__, info->jt_vfchg_h);
2174 err = ricoh619_write(info->dev->parent, CHGISET_REG, set_ichg_l);
2176 dev_err(info->dev, "Error in writing the battery setting register\n");
2179 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2181 dev_err(info->dev, "Error in writing the control register\n");
2184 } else if (temp < info->jt_temp_h) {
2185 /* 3rd temperature range (12~50) */
2186 if (vfchg != info->jt_vfchg_h) {
2187 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 12<Temp<50, update to vfchg==%d ******\n", __func__, info->jt_vfchg_h);
2189 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2191 dev_err(info->dev, "Error in writing the control register\n");
2194 /* set VFCHG/VRCHG */
2195 err = ricoh619_write(info->dev->parent,
2196 BATSET2_REG, set_vchg_h);
2198 dev_err(info->dev, "Error in writing the battery setting register\n");
2201 info->soca->jt_limit = 0;
2202 *is_jeita_updated = true;
2204 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 12<Temp<50, already set vfchg==%d, so no need to update ******\n",
2205 __func__, info->jt_vfchg_h);
2208 err = ricoh619_write(info->dev->parent, CHGISET_REG, set_ichg_h);
2210 dev_err(info->dev, "Error in writing the battery setting register\n");
2213 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2215 dev_err(info->dev, "Error in writing the control register\n");
2218 } else if (temp < 55) {
2219 /* 4th temperature range (50~55) */
2220 if (vfchg != info->jt_vfchg_l) {
2221 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 50<Temp<55, update to vfchg==%d ******\n", __func__, info->jt_vfchg_l);
2223 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2225 dev_err(info->dev, "Error in writing the control register\n");
2228 /* set VFCHG/VRCHG */
2229 err = ricoh619_write(info->dev->parent,
2230 BATSET2_REG, set_vchg_l);
2232 dev_err(info->dev, "Error in writing the battery setting register\n");
2235 info->soca->jt_limit = 1;
2236 *is_jeita_updated = true;
2238 RICOH_FG_DBG(KERN_INFO "JEITA: %s *** 50<Temp<55, already set vfchg==%d, so no need to update ******\n",
2239 __func__, info->jt_vfchg_l);
2242 err = ricoh619_write(info->dev->parent, CHGISET_REG, set_ichg_h);
2244 dev_err(info->dev, "Error in writing the battery setting register\n");
2247 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2249 dev_err(info->dev, "Error in writing the control register\n");
2254 get_power_supply_status(info);
2255 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Hope updating value in this timing after checking jeita, chg_status: %d, is_jeita_updated: %d ******\n",
2256 __func__, info->soca->chg_status, *is_jeita_updated);
2261 RICOH_FG_DBG(KERN_INFO "PMU: %s ERROR ******\n", __func__);
2265 static void ricoh619_jeita_work(struct work_struct *work)
2268 bool is_jeita_updated = false;
2269 struct ricoh619_battery_info *info = container_of(work,
2270 struct ricoh619_battery_info, jeita_work.work);
2272 mutex_lock(&info->lock);
2274 ret = check_jeita_status(info, &is_jeita_updated);
2276 queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
2277 RICOH619_JEITA_UPDATE_TIME * HZ);
2279 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Call check_jeita_status() in jeita_work, err:%d ******\n",
2281 queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
2282 RICOH619_FG_RESET_TIME * HZ);
2285 mutex_unlock(&info->lock);
2287 if(true == is_jeita_updated)
2288 power_supply_changed(&info->battery);
2293 #ifdef ENABLE_FACTORY_MODE
2294 /*------------------------------------------------------*/
2296 /* Check Battery exist or not */
2297 /* If not, disabled Rapid to Complete State change */
2298 /*------------------------------------------------------*/
2299 static int ricoh619_factory_mode(struct ricoh619_battery_info *info)
2304 ret = ricoh619_read(info->dev->parent, RICOH619_INT_MON_CHGCTR, &val);
2306 dev_err(info->dev, "Error in reading the control register\n");
2309 if (!(val & 0x01)) /* No Adapter connected */
2312 /* Rapid to Complete State change disable */
2313 ret = ricoh619_set_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x40);
2315 dev_err(info->dev, "Error in writing the control register\n");
2319 /* Wait 1s for checking Charging State */
2320 queue_delayed_work(info->factory_mode_wqueue, &info->factory_mode_work,
2326 static void check_charging_state_work(struct work_struct *work)
2328 struct ricoh619_battery_info *info = container_of(work,
2329 struct ricoh619_battery_info, factory_mode_work.work);
2333 int chargeCurrent = 0;
2335 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &val);
2337 dev_err(info->dev, "Error in reading the control register\n");
2342 chargeCurrent = get_check_fuel_gauge_reg(info, CC_AVERAGE1_REG,
2343 CC_AVERAGE0_REG, 0x3fff);
2344 if (chargeCurrent < 0) {
2345 dev_err(info->dev, "Error in reading the FG register\n");
2349 /* Repid State && Charge Current about 0mA */
2350 if (((chargeCurrent >= 0x3ffc && chargeCurrent <= 0x3fff)
2351 || chargeCurrent < 0x05) && val == 0x43) {
2352 RICOH_FG_DBG("PMU:%s --- No battery !! Enter Factory mode ---\n"
2354 info->entry_factory_mode = true;
2355 /* clear FG_ACC bit */
2356 ret = ricoh619_clr_bits(info->dev->parent, RICOH619_FG_CTRL, 0x10);
2358 dev_err(info->dev, "Error in writing FG_CTRL\n");
2360 return; /* Factory Mode */
2363 /* Return Normal Mode --> Rapid to Complete State change enable */
2364 ret = ricoh619_clr_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x40);
2366 dev_err(info->dev, "Error in writing the control register\n");
2369 RICOH_FG_DBG("PMU:%s --- Battery exist !! Return Normal mode ---0x%2x\n"
2374 #endif /* ENABLE_FACTORY_MODE */
2376 static int Calc_Linear_Interpolation(int x0, int y0, int x1, int y1, int y)
2381 alpha = (y - y0)*100 / (y1 - y0);
2383 x = ((100 - alpha) * x0 + alpha * x1) / 100;
2388 static void ricoh619_scaling_OCV_table(struct ricoh619_battery_info *info, int cutoff_vol, int full_vol, int *start_per, int *end_per)
2393 int OCV_percent_new[11];
2395 /* get ocv table. this table is calculated by Apprication */
2396 //RICOH_FG_DBG("PMU : %s : original table\n",__func__);
2397 for (i = 0; i <= 10; i = i+1) {
2398 RICOH_FG_DBG(KERN_INFO "PMU: %s : %d0%% voltage = %d uV\n",
2399 __func__, i, info->soca->ocv_table_def[i]);
2401 //RICOH_FG_DBG("PMU: %s : cutoff_vol %d full_vol %d\n",
2402 // __func__, cutoff_vol,full_vol);
2405 if (info->soca->ocv_table_def[0] > cutoff_vol * 1000) {
2407 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]);
2409 for (i = 1; i < 11; i++) {
2410 if (info->soca->ocv_table_def[i] >= cutoff_vol * 1000) {
2411 /* unit is 0.001% */
2412 *start_per = Calc_Linear_Interpolation(
2413 (i-1)*1000, info->soca->ocv_table_def[i-1], i*1000,
2414 info->soca->ocv_table_def[i], (cutoff_vol * 1000));
2421 for (i = 1; i < 11; i++) {
2422 if (info->soca->ocv_table_def[i] >= full_vol * 1000) {
2423 /* unit is 0.001% */
2424 *end_per = Calc_Linear_Interpolation(
2425 (i-1)*1000, info->soca->ocv_table_def[i-1], i*1000,
2426 info->soca->ocv_table_def[i], (full_vol * 1000));
2431 /* calc new ocv percent */
2432 percent_step = ( *end_per - *start_per) / 10;
2433 //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);
2435 for (i = 0; i < 11; i++) {
2437 = *start_per + percent_step*(i - 0);
2440 /* calc new ocv voltage */
2441 for (i = 0; i < 11; i++) {
2442 for (j = 1; j < 11; j++) {
2443 if (1000*j >= OCV_percent_new[i]) {
2444 temp = Calc_Linear_Interpolation(
2445 info->soca->ocv_table_def[j-1], (j-1)*1000,
2446 info->soca->ocv_table_def[j] , j*1000,
2447 OCV_percent_new[i]);
2449 temp = ( (temp/1000) * 4095 ) / 5000;
2451 battery_init_para[info->num][i*2 + 1] = temp;
2452 battery_init_para[info->num][i*2] = temp >> 8;
2458 RICOH_FG_DBG("PMU : %s : new table\n",__func__);
2459 for (i = 0; i <= 10; i = i+1) {
2460 temp = (battery_init_para[info->num][i*2]<<8)
2461 | (battery_init_para[info->num][i*2+1]);
2462 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
2463 temp = ((temp * 50000 * 10 / 4095) + 5) / 10;
2464 RICOH_FG_DBG("PMU : %s : ocv_table %d is %d v\n",__func__, i, temp);
2469 static int ricoh619_set_OCV_table(struct ricoh619_battery_info *info)
2475 int available_cap_ori;
2488 for (i = 0; i <= 10; i = i+1) {
2489 info->soca->ocv_table_def[i] = get_OCV_voltage(info, i);
2490 RICOH_FG_DBG(KERN_INFO "PMU: %s : %d0%% voltage = %d uV\n",
2491 __func__, i, info->soca->ocv_table_def[i]);
2494 temp = (battery_init_para[info->num][24]<<8) | (battery_init_para[info->num][25]);
2495 Rbat = temp * 1000 / 512 * 5000 / 4095;
2496 info->soca->Rsys = Rbat + 55;
2498 if ((info->fg_target_ibat == 0) || (info->fg_target_vsys == 0)) { /* normal version */
2500 temp = (battery_init_para[info->num][22]<<8) | (battery_init_para[info->num][23]);
2501 //fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
2504 info->soca->target_ibat = temp*2/10; /* calc 0.2C*/
2505 temp1 = (battery_init_para[info->num][0]<<8) | (battery_init_para[info->num][1]);
2507 info->soca->target_vsys = temp1 + ( info->soca->target_ibat * info->soca->Rsys ) / 1000;
2511 info->soca->target_ibat = info->fg_target_ibat;
2512 /* calc min vsys value */
2513 temp1 = (battery_init_para[info->num][0]<<8) | (battery_init_para[info->num][1]);
2514 temp = temp1 + ( info->soca->target_ibat * info->soca->Rsys ) / 1000;
2515 if( temp < info->fg_target_vsys) {
2516 info->soca->target_vsys = info->fg_target_vsys;
2518 info->soca->target_vsys = temp;
2519 RICOH_FG_DBG("PMU : %s : setting value of target vsys(%d) is out of range(%d)\n",__func__, info->fg_target_vsys, temp);
2524 RICOH_FG_DBG("PMU : %s : target_vsys is %d target_ibat is %d\n",__func__,info->soca->target_vsys,info->soca->target_ibat);
2526 if ((info->soca->target_ibat == 0) || (info->soca->target_vsys == 0)) { /* normal version */
2527 } else { /*Slice cutoff voltage version. */
2529 Ibat_min = -1 * info->soca->target_ibat;
2530 info->soca->cutoff_ocv = info->soca->target_vsys - Ibat_min * info->soca->Rsys / 1000;
2532 full_ocv = (battery_init_para[info->num][20]<<8) | (battery_init_para[info->num][21]);
2533 full_ocv = full_ocv * 5000 / 4095;
2535 ricoh619_scaling_OCV_table(info, info->soca->cutoff_ocv, full_ocv, &start_per, &end_per);
2537 /* calc available capacity */
2538 /* get avilable capacity */
2539 /* battery_init_para23-24 is designe capacity */
2540 available_cap = (battery_init_para[info->num][22]<<8)
2541 | (battery_init_para[info->num][23]);
2543 available_cap = available_cap
2544 * ((10000 - start_per) / 100) / 100 ;
2547 battery_init_para[info->num][23] = available_cap;
2548 battery_init_para[info->num][22] = available_cap >> 8;
2551 ret = ricoh619_clr_bits(info->dev->parent, FG_CTRL_REG, 0x01);
2553 dev_err(info->dev, "error in FG_En off\n");
2556 /////////////////////////////////
2557 ret = ricoh619_read_bank1(info->dev->parent, 0xDC, &val);
2559 dev_err(info->dev, "batterry initialize error\n");
2564 val &= 0x0F; //clear bit 4-7
2565 val |= 0x10; //set bit 4
2567 ret = ricoh619_write_bank1(info->dev->parent, 0xDC, val);
2569 dev_err(info->dev, "batterry initialize error\n");
2573 ret = ricoh619_read_bank1(info->dev->parent, 0xDC, &val2);
2575 dev_err(info->dev, "batterry initialize error\n");
2579 ret = ricoh619_write_bank1(info->dev->parent, 0xDC, val_temp);
2581 dev_err(info->dev, "batterry initialize error\n");
2585 RICOH_FG_DBG("PMU : %s : original 0x%x, before 0x%x, after 0x%x\n",__func__, val_temp, val, val2);
2588 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
2589 BAT_INIT_TOP_REG, 30, battery_init_para[info->num]);
2591 dev_err(info->dev, "batterry initialize error\n");
2595 ret = ricoh619_read_bank1(info->dev->parent, 0xD2, &val);
2597 dev_err(info->dev, "batterry initialize error\n");
2601 ret = ricoh619_read_bank1(info->dev->parent, 0xD3, &val2);
2603 dev_err(info->dev, "batterry initialize error\n");
2607 available_cap_ori = val2 + (val << 8);
2608 available_cap = battery_init_para[info->num][23]
2609 + (battery_init_para[info->num][22] << 8);
2611 if (available_cap_ori == available_cap) {
2612 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
2613 BAT_INIT_TOP_REG, 22, battery_init_para[info->num]);
2615 dev_err(info->dev, "batterry initialize error\n");
2619 for (i = 0; i < 6; i++) {
2620 ret = ricoh619_write_bank1(info->dev->parent, 0xD4+i, battery_init_para[info->num][24+i]);
2622 dev_err(info->dev, "batterry initialize error\n");
2627 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
2628 BAT_INIT_TOP_REG, 30, battery_init_para[info->num]);
2630 dev_err(info->dev, "batterry initialize error\n");
2636 ////////////////////////////////
2643 /* Initial setting of battery */
2644 static int ricoh619_init_battery(struct ricoh619_battery_info *info)
2649 /* Need to implement initial setting of batery and error */
2650 /* -------------------------- */
2651 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
2653 /* set relaxation state */
2654 if (RICOH619_REL1_SEL_VALUE > 240)
2657 val = RICOH619_REL1_SEL_VALUE / 16 ;
2659 /* set relaxation state */
2660 if (RICOH619_REL2_SEL_VALUE > 120)
2663 val2 = RICOH619_REL2_SEL_VALUE / 8 ;
2665 val = val + (val2 << 4);
2667 ret = ricoh619_write_bank1(info->dev->parent, BAT_REL_SEL_REG, val);
2669 dev_err(info->dev, "Error in writing BAT_REL_SEL_REG\n");
2673 ret = ricoh619_read_bank1(info->dev->parent, BAT_REL_SEL_REG, &val);
2674 RICOH_FG_DBG("PMU: ------- BAT_REL_SEL= %xh: =======\n",
2677 ret = ricoh619_write_bank1(info->dev->parent, BAT_TA_SEL_REG, 0x00);
2679 dev_err(info->dev, "Error in writing BAT_TA_SEL_REG\n");
2683 // ret = ricoh619_read(info->dev->parent, FG_CTRL_REG, &val);
2685 // dev_err(info->dev, "Error in reading the control register\n");
2689 // val = (val & 0x10) >> 4;
2690 // info->first_pwon = (val == 0) ? 1 : 0;
2691 ret = ricoh619_read(info->dev->parent, PSWR_REG, &val);
2693 dev_err(info->dev,"Error in reading PSWR_REG %d\n", ret);
2696 info->first_pwon = (val == 0) ? 1 : 0;
2699 ret = ricoh619_set_OCV_table(info);
2701 dev_err(info->dev, "Error in writing the OCV Tabler\n");
2705 ret = ricoh619_write(info->dev->parent, FG_CTRL_REG, 0x11);
2707 dev_err(info->dev, "Error in writing the control register\n");
2713 ret = ricoh619_write(info->dev->parent, VINDAC_REG, 0x03);
2715 dev_err(info->dev, "Error in writing the control register\n");
2719 if (info->alarm_vol_mv < 2700 || info->alarm_vol_mv > 3400) {
2720 dev_err(info->dev, "alarm_vol_mv is out of range!\n");
2727 /* Initial setting of charger */
2728 static int ricoh619_init_charger(struct ricoh619_battery_info *info)
2741 info->chg_stat1 = 0;
2743 err = ricoh619_set_bits(info->dev->parent, RICOH619_PWR_FUNC, 0x20);
2745 dev_err(info->dev, "Error in writing the PWR FUNC register\n");
2749 charge_status = get_power_supply_status(info);
2751 if (charge_status != POWER_SUPPLY_STATUS_FULL)
2753 /* Disable charging */
2754 err = ricoh619_clr_bits(info->dev->parent,CHGCTL1_REG, 0x03);
2756 dev_err(info->dev, "Error in writing the control register\n");
2762 err = ricoh619_read(info->dev->parent, REGISET1_REG,&val);
2763 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);
2765 /* REGISET1:(0xB6) setting */
2766 if ((info->ch_ilim_adp != 0xFF) || (info->ch_ilim_adp <= 0x1D)) {
2767 val = info->ch_ilim_adp;
2769 err = ricoh619_write(info->dev->parent, REGISET1_REG,val);
2771 dev_err(info->dev, "Error in writing REGISET1_REG %d\n",
2778 err = ricoh619_read(info->dev->parent, REGISET1_REG,&val);
2779 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);
2782 err = ricoh619_read(info->dev->parent, REGISET2_REG,&val);
2783 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);
2785 /* REGISET2:(0xB7) setting */
2786 err = ricoh619_read(info->dev->parent, REGISET2_REG, &val);
2789 "Error in read REGISET2_REG %d\n", err);
2793 if ((info->ch_ilim_usb != 0xFF) || (info->ch_ilim_usb <= 0x1D)) {
2794 val2 = info->ch_ilim_usb;
2795 } else {/* Keep OTP value */
2796 val2 = (val & 0x1F);
2804 err = ricoh619_write(info->dev->parent, REGISET2_REG,val);
2806 dev_err(info->dev, "Error in writing REGISET2_REG %d\n",
2812 err = ricoh619_read(info->dev->parent, REGISET2_REG,&val);
2813 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);
2815 /* CHGISET_REG(0xB8) setting */
2817 err = ricoh619_read(info->dev->parent, CHGISET_REG,&val);
2818 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);
2820 err = ricoh619_read(info->dev->parent, CHGISET_REG, &val);
2823 "Error in read CHGISET_REG %d\n", err);
2827 /* Define Current settings value for charging (bit 4~0)*/
2828 if ((info->ch_ichg != 0xFF) || (info->ch_ichg <= 0x1D)) {
2829 val2 = info->ch_ichg;
2830 } else { /* Keep OTP value */
2831 val2 = (val & 0x1F);
2834 /* Define Current settings at the charge completion (bit 7~6)*/
2835 if ((info->ch_icchg != 0xFF) || (info->ch_icchg <= 0x03)) {
2836 val3 = info->ch_icchg << 6;
2837 } else { /* Keep OTP value */
2838 val3 = (val & 0xC0);
2843 err = ricoh619_write(info->dev->parent, CHGISET_REG, val);
2845 dev_err(info->dev, "Error in writing CHGISET_REG %d\n",
2851 err = ricoh619_read(info->dev->parent, CHGISET_REG,&val);
2852 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);
2855 err = ricoh619_read(info->dev->parent, BATSET1_REG,&val);
2856 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);
2858 /* BATSET1_REG(0xBA) setting */
2859 err = ricoh619_read(info->dev->parent, BATSET1_REG, &val);
2862 "Error in read BATSET1 register %d\n", err);
2866 /* Define Battery overvoltage (bit 4)*/
2867 if ((info->ch_vbatovset != 0xFF) || (info->ch_vbatovset <= 0x1)) {
2868 val2 = info->ch_vbatovset;
2870 } else { /* Keep OTP value */
2871 val2 = (val & 0x10);
2874 /* keep bit 0-3 and bit 5-7 */
2879 err = ricoh619_write(info->dev->parent, BATSET1_REG, val);
2881 dev_err(info->dev, "Error in writing BAT1_REG %d\n",
2886 err = ricoh619_read(info->dev->parent, BATSET1_REG,&val);
2887 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);
2890 err = ricoh619_read(info->dev->parent, BATSET2_REG,&val);
2891 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);
2894 /* BATSET2_REG(0xBB) setting */
2895 err = ricoh619_read(info->dev->parent, BATSET2_REG, &val);
2898 "Error in read BATSET2 register %d\n", err);
2902 /* Define Re-charging voltage (bit 2~0)*/
2903 if ((info->ch_vrchg != 0xFF) || (info->ch_vrchg <= 0x04)) {
2904 val2 = info->ch_vrchg;
2905 } else { /* Keep OTP value */
2906 val2 = (val & 0x07);
2909 /* Define FULL charging voltage (bit 6~4)*/
2910 if ((info->ch_vfchg != 0xFF) || (info->ch_vfchg <= 0x04)) {
2911 val3 = info->ch_vfchg;
2913 } else { /* Keep OTP value */
2914 val3 = (val & 0x70);
2917 /* keep bit 3 and bit 7 */
2920 val = val + val2 + val3;
2922 err = ricoh619_write(info->dev->parent, BATSET2_REG, val);
2924 dev_err(info->dev, "Error in writing RICOH619_RE_CHARGE_VOLTAGE %d\n",
2930 err = ricoh619_read(info->dev->parent, BATSET2_REG,&val);
2931 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);
2933 /* Set rising edge setting ([1:0]=01b)for INT in charging */
2934 /* and rising edge setting ([3:2]=01b)for charge completion */
2935 err = ricoh619_read(info->dev->parent, RICOH619_CHG_STAT_DETMOD1, &val);
2937 dev_err(info->dev, "Error in reading CHG_STAT_DETMOD1 %d\n",
2943 err = ricoh619_write(info->dev->parent, RICOH619_CHG_STAT_DETMOD1, val);
2945 dev_err(info->dev, "Error in writing CHG_STAT_DETMOD1 %d\n",
2950 /* Unmask In charging/charge completion */
2951 err = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGSTS1, 0xfc);
2953 dev_err(info->dev, "Error in writing INT_MSK_CHGSTS1 %d\n",
2958 /* Set both edge for VUSB([3:2]=11b)/VADP([1:0]=11b) detect */
2959 err = ricoh619_read(info->dev->parent, RICOH619_CHG_CTRL_DETMOD1, &val);
2961 dev_err(info->dev, "Error in reading CHG_CTRL_DETMOD1 %d\n",
2967 err = ricoh619_write(info->dev->parent, RICOH619_CHG_CTRL_DETMOD1, val);
2969 dev_err(info->dev, "Error in writing CHG_CTRL_DETMOD1 %d\n",
2974 /* Unmask In VUSB/VADP completion */
2975 err = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGCTR, 0xfc);
2977 dev_err(info->dev, "Error in writing INT_MSK_CHGSTS1 %d\n",
2982 if (charge_status != POWER_SUPPLY_STATUS_FULL)
2984 /* Enable charging */
2985 err = ricoh619_set_bits(info->dev->parent,CHGCTL1_REG, 0x03);
2987 dev_err(info->dev, "Error in writing the control register\n");
2991 /* get OCV100_min, OCV100_min*/
2992 temp = (battery_init_para[info->num][24]<<8) | (battery_init_para[info->num][25]);
2993 rbat = temp * 1000 / 512 * 5000 / 4095;
2995 /* get vfchg value */
2996 err = ricoh619_read(info->dev->parent, BATSET2_REG, &val);
2998 dev_err(info->dev, "Error in reading the batset2reg\n");
3004 vfchg_val = 4050 + val2 * 50;
3008 RICOH_FG_DBG("PMU : %s : test test val %d, val2 %d vfchg %d\n", __func__, val, val2, vfchg_val);
3011 err = ricoh619_read(info->dev->parent, CHGISET_REG, &val);
3013 dev_err(info->dev, "Error in reading the chgisetreg\n");
3018 icchg_val = 50 + val2 * 50;
3019 RICOH_FG_DBG("PMU : %s : test test val %d, val2 %d icchg %d\n", __func__, val, val2, icchg_val);
3021 info->soca->OCV100_min = ( vfchg_val * 99 / 100 - (icchg_val * (rbat +20))/1000 - 20 ) * 1000;
3022 info->soca->OCV100_max = ( vfchg_val * 101 / 100 - (icchg_val * (rbat +20))/1000 + 20 ) * 1000;
3024 RICOH_FG_DBG("PMU : %s : 100 min %d, 100 max %d vfchg %d icchg %d rbat %d\n",__func__,
3025 info->soca->OCV100_min,info->soca->OCV100_max,vfchg_val,icchg_val,rbat);
3027 #ifdef ENABLE_LOW_BATTERY_DETECTION
3028 /* Set ADRQ=00 to stop ADC */
3029 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT3, 0x0);
3030 /* Enable VSYS threshold Low interrupt */
3031 ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
3032 /* Set ADC auto conversion interval 250ms */
3033 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT2, 0x0);
3034 /* Enable VSYS pin conversion in auto-ADC */
3035 // ricoh619_write(info->dev->parent, RICOH619_ADC_CNT1, 0x10);
3036 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT1, 0x16);
3037 /* Set VSYS threshold low voltage value = (voltage(V)*255)/(3*2.5) */
3038 val = info->alarm_vol_mv * 255 / 7500;
3039 ricoh619_write(info->dev->parent, RICOH619_ADC_VSYS_THL, val);
3040 /* Start auto-mode & average 4-time conversion mode for ADC */
3041 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT3, 0x28);
3042 /* Enable master ADC INT */
3043 ricoh619_set_bits(info->dev->parent, RICOH619_INTC_INTEN, ADC_INT);
3051 static int get_power_supply_status(struct ricoh619_battery_info *info)
3054 uint8_t supply_state;
3055 uint8_t charge_state;
3058 /* get power supply status */
3059 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
3061 dev_err(info->dev, "Error in reading the control register\n");
3065 charge_state = (status & 0x1F);
3066 supply_state = ((status & 0xC0) >> 6);
3068 if (info->entry_factory_mode)
3069 return POWER_SUPPLY_STATUS_NOT_CHARGING;
3071 if (supply_state == SUPPLY_STATE_BAT) {
3072 info->soca->chg_status = POWER_SUPPLY_STATUS_DISCHARGING;
3074 switch (charge_state) {
3075 case CHG_STATE_CHG_OFF:
3076 info->soca->chg_status
3077 = POWER_SUPPLY_STATUS_DISCHARGING;
3079 case CHG_STATE_CHG_READY_VADP:
3080 info->soca->chg_status
3081 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3083 case CHG_STATE_CHG_TRICKLE:
3084 info->soca->chg_status
3085 = POWER_SUPPLY_STATUS_CHARGING;
3087 case CHG_STATE_CHG_RAPID:
3088 info->soca->chg_status
3089 = POWER_SUPPLY_STATUS_CHARGING;
3091 case CHG_STATE_CHG_COMPLETE:
3092 info->soca->chg_status
3093 = POWER_SUPPLY_STATUS_FULL;
3095 case CHG_STATE_SUSPEND:
3096 info->soca->chg_status
3097 = POWER_SUPPLY_STATUS_DISCHARGING;
3099 case CHG_STATE_VCHG_OVER_VOL:
3100 info->soca->chg_status
3101 = POWER_SUPPLY_STATUS_DISCHARGING;
3103 case CHG_STATE_BAT_ERROR:
3104 info->soca->chg_status
3105 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3107 case CHG_STATE_NO_BAT:
3108 info->soca->chg_status
3109 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3111 case CHG_STATE_BAT_OVER_VOL:
3112 info->soca->chg_status
3113 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3115 case CHG_STATE_BAT_TEMP_ERR:
3116 info->soca->chg_status
3117 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3119 case CHG_STATE_DIE_ERR:
3120 info->soca->chg_status
3121 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3123 case CHG_STATE_DIE_SHUTDOWN:
3124 info->soca->chg_status
3125 = POWER_SUPPLY_STATUS_DISCHARGING;
3127 case CHG_STATE_NO_BAT2:
3128 info->soca->chg_status
3129 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3131 case CHG_STATE_CHG_READY_VUSB:
3132 info->soca->chg_status
3133 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3136 info->soca->chg_status
3137 = POWER_SUPPLY_STATUS_UNKNOWN;
3142 return info->soca->chg_status;
3145 static int get_power_supply_Android_status(struct ricoh619_battery_info *info)
3148 get_power_supply_status(info);
3150 /* get power supply status */
3151 if (info->entry_factory_mode)
3152 return POWER_SUPPLY_STATUS_NOT_CHARGING;
3154 switch (info->soca->chg_status) {
3155 case POWER_SUPPLY_STATUS_UNKNOWN:
3156 return POWER_SUPPLY_STATUS_UNKNOWN;
3159 case POWER_SUPPLY_STATUS_NOT_CHARGING:
3160 return POWER_SUPPLY_STATUS_NOT_CHARGING;
3163 case POWER_SUPPLY_STATUS_DISCHARGING:
3164 return POWER_SUPPLY_STATUS_DISCHARGING;
3167 case POWER_SUPPLY_STATUS_CHARGING:
3168 return POWER_SUPPLY_STATUS_CHARGING;
3171 case POWER_SUPPLY_STATUS_FULL:
3172 if(info->soca->displayed_soc == 100 * 100) {
3173 return POWER_SUPPLY_STATUS_FULL;
3175 return POWER_SUPPLY_STATUS_CHARGING;
3179 return POWER_SUPPLY_STATUS_UNKNOWN;
3183 return POWER_SUPPLY_STATUS_UNKNOWN;
3185 extern struct ricoh619 *g_ricoh619;
3186 static void charger_irq_work(struct work_struct *work)
3188 struct ricoh619_battery_info *info
3189 = container_of(work, struct ricoh619_battery_info, irq_work);
3192 RICOH_FG_DBG("PMU:%s In\n", __func__);
3194 power_supply_changed(&info->battery);
3195 power_supply_changed(&powerac);
3196 power_supply_changed(&powerusb);
3198 // mutex_lock(&info->lock);
3200 if (info->chg_stat1 & 0x01) {
3201 ricoh619_read(info->dev->parent, CHGSTATE_REG, ®_val);
3202 if (reg_val & 0x40) { /* USE ADP */
3203 #ifdef SUPPORT_USB_CONNECT_TO_ADP
3206 RICOH_FG_DBG("PMU:%s usb det dwc_otg_check_dpdm =%d\n", __func__,dwc_otg_check_dpdm(0));
3207 if(2 == dwc_otg_check_dpdm(0)){
3208 /* set adp limit current 2A */
3209 ricoh619_write(info->dev->parent, REGISET1_REG, 0x16);
3210 /* set charge current 2A */
3211 ricoh619_write(info->dev->parent, CHGISET_REG, 0xD3);
3214 /* set adp limit current 500ma */
3215 ricoh619_write(info->dev->parent, REGISET1_REG, 0x04);
3216 /* set charge current 500ma */
3217 ricoh619_write(info->dev->parent, CHGISET_REG, 0xc4);
3220 power_supply_changed(&info->battery);
3221 power_supply_changed(&powerac);
3222 power_supply_changed(&powerusb);
3225 #else //support adp and usb chag
3226 if (gpio_is_valid(g_ricoh619->dc_det)){
3227 ret = gpio_request(g_ricoh619->dc_det, "ricoh619_dc_det");
3229 RICOH_FG_DBG("Failed to request gpio %d with ret:""%d\n",g_ricoh619->dc_det, ret);
3231 gpio_direction_input(g_ricoh619->dc_det);
3232 ret = gpio_get_value(g_ricoh619->dc_det);
3235 /* set adp limit current 2A */
3236 ricoh619_write(info->dev->parent, REGISET1_REG, 0x16);
3237 /* set charge current 2A */
3238 ricoh619_write(info->dev->parent, CHGISET_REG, 0xD3);
3241 /* set adp limit current 500ma */
3242 ricoh619_write(info->dev->parent, REGISET1_REG, 0x04);
3243 /* set charge current 500ma */
3244 ricoh619_write(info->dev->parent, CHGISET_REG, 0xc4);
3246 gpio_free(g_ricoh619->dc_det);
3249 /* set adp limit current 2A */
3250 ricoh619_write(info->dev->parent, REGISET1_REG, 0x16);
3251 /* set charge current 2A */
3252 ricoh619_write(info->dev->parent, CHGISET_REG, 0xD3);
3255 } else if (reg_val & 0x80) { /* USE ONLY USB */
3256 queue_work(info->usb_workqueue, &info->usb_irq_work);
3260 info->chg_stat1 = 0;
3262 /* Enable Interrupt for VADP/USB */
3263 ret = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGCTR, 0xfc);
3266 "%s(): Error in enable charger mask INT %d\n",
3269 /* Enable Interrupt for Charging & complete */
3270 ret = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGSTS1, 0xfc);
3273 "%s(): Error in enable charger mask INT %d\n",
3276 // mutex_unlock(&info->lock);
3277 RICOH_FG_DBG("PMU:%s Out\n", __func__);
3280 #ifdef ENABLE_LOW_BATTERY_DETECTION
3281 static void low_battery_irq_work(struct work_struct *work)
3283 struct ricoh619_battery_info *info = container_of(work,
3284 struct ricoh619_battery_info, low_battery_work.work);
3288 RICOH_FG_DBG("PMU:%s In\n", __func__);
3290 power_supply_changed(&info->battery);
3292 /* Enable VADP threshold Low interrupt */
3293 ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
3296 "%s(): Error in enable adc mask INT %d\n",
3302 static void ricoh619_usb_charge_det(void)
3304 struct ricoh619 *ricoh619 = g_ricoh619;
3305 ricoh619_set_bits(ricoh619->dev,REGISET2_REG,(1 << 7)); //set usb limit current when SDP or other mode
3306 RICOH_FG_DBG("PMU:%s usb det dwc_otg_check_dpdm =%d\n", __func__,dwc_otg_check_dpdm(0));
3307 if(2 == dwc_otg_check_dpdm(0)){
3308 ricoh619_write(ricoh619->dev,REGISET2_REG,0x16); //set usb limit current 2A
3309 ricoh619_write(ricoh619->dev,CHGISET_REG,0xD3); //set charge current 2A
3312 ricoh619_write(ricoh619->dev,REGISET2_REG,0x04); //set usb limit current 500ma
3313 ricoh619_write(ricoh619->dev,CHGISET_REG,0xC4); //set charge current 500ma
3315 power_supply_changed(&powerac);
3316 power_supply_changed(&powerusb);
3319 static void usb_det_irq_work(struct work_struct *work)
3321 struct ricoh619_battery_info *info = container_of(work,
3322 struct ricoh619_battery_info, usb_irq_work);
3326 RICOH_FG_DBG("PMU:%s In\n", __func__);
3328 power_supply_changed(&info->battery);
3329 power_supply_changed(&powerac);
3330 power_supply_changed(&powerusb);
3332 mutex_lock(&info->lock);
3334 /* Enable Interrupt for VUSB */
3335 ret = ricoh619_clr_bits(info->dev->parent,
3336 RICOH619_INT_MSK_CHGCTR, 0x02);
3339 "%s(): Error in enable charger mask INT %d\n",
3342 mutex_unlock(&info->lock);
3343 ret = ricoh619_read(info->dev->parent, RICOH619_INT_MON_CHGCTR, &sts);
3345 dev_err(info->dev, "Error in reading the control register\n");
3349 ricoh619_usb_charge_det();
3351 RICOH_FG_DBG("PMU:%s Out\n", __func__);
3354 extern void rk_send_wakeup_key(void);
3355 static irqreturn_t charger_in_isr(int irq, void *battery_info)
3357 struct ricoh619_battery_info *info = battery_info;
3358 RICOH_FG_DBG("PMU:%s\n", __func__);
3360 info->chg_stat1 |= 0x01;
3362 queue_work(info->workqueue, &info->irq_work);
3363 // rk_send_wakeup_key();
3368 static irqreturn_t charger_complete_isr(int irq, void *battery_info)
3370 struct ricoh619_battery_info *info = battery_info;
3371 RICOH_FG_DBG("PMU:%s\n", __func__);
3373 info->chg_stat1 |= 0x02;
3374 queue_work(info->workqueue, &info->irq_work);
3375 // rk_send_wakeup_key();
3380 static irqreturn_t charger_usb_isr(int irq, void *battery_info)
3382 struct ricoh619_battery_info *info = battery_info;
3383 RICOH_FG_DBG("PMU:%s\n", __func__);
3385 info->chg_ctr |= 0x02;
3387 queue_work(info->workqueue, &info->irq_work);
3389 info->soca->dischg_state = 0;
3390 info->soca->chg_count = 0;
3392 // queue_work(info->usb_workqueue, &info->usb_irq_work);
3393 rk_send_wakeup_key();
3395 if (RICOH619_SOCA_UNSTABLE == info->soca->status
3396 || RICOH619_SOCA_FG_RESET == info->soca->status)
3397 info->soca->stable_count = 11;
3402 static irqreturn_t charger_adp_isr(int irq, void *battery_info)
3404 struct ricoh619_battery_info *info = battery_info;
3405 RICOH_FG_DBG("PMU:%s\n", __func__);
3407 info->chg_ctr |= 0x01;
3408 queue_work(info->workqueue, &info->irq_work);
3409 rk_send_wakeup_key();
3411 info->soca->dischg_state = 0;
3412 info->soca->chg_count = 0;
3413 if (RICOH619_SOCA_UNSTABLE == info->soca->status
3414 || RICOH619_SOCA_FG_RESET == info->soca->status)
3415 info->soca->stable_count = 11;
3421 #ifdef ENABLE_LOW_BATTERY_DETECTION
3422 /*************************************************************/
3423 /* for Detecting Low Battery */
3424 /*************************************************************/
3426 static irqreturn_t adc_vsysl_isr(int irq, void *battery_info)
3429 struct ricoh619_battery_info *info = battery_info;
3432 RICOH_FG_DBG("PMU:%s\n", __func__);
3434 queue_delayed_work(info->monitor_wqueue, &info->low_battery_work,
3435 LOW_BATTERY_DETECTION_TIME*HZ);
3439 RICOH_FG_DBG("PMU:%s\n", __func__);
3440 // ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
3441 rk_send_wakeup_key();
3446 #ifdef RICOH619_VADP_DROP_WORK
3447 static void vadp_drop_irq_work(struct work_struct *work)
3449 struct ricoh619_battery_info *info = container_of(work,
3450 struct ricoh619_battery_info, vadp_drop_work.work);
3456 RICOH_FG_DBG("PMU vadp_drop_work:%s In\n", __func__);
3457 mutex_lock(&info->lock);
3458 ret = ricoh619_read(info->dev->parent, 0x6a, &data[0]);
3459 ret = ricoh619_read(info->dev->parent, 0x6b, &data[1]);
3460 ret = ricoh619_read(info->dev->parent, 0x6c, &data[2]);
3461 ret = ricoh619_read(info->dev->parent, 0x6d, &data[3]);
3462 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG,&data[4]);
3463 reg[0]= (data[0]<<4) |data[1];
3464 reg[1]= (data[2]<<4) |data[3];
3466 // printk("PMU vadp_drop:%s In %08x %08x %08x %08x %08x %08x %d\n", __func__,data[0],data[1],data[2],data[3],reg[0],reg[1],ret);
3467 if ((2*(reg[0] +82)) > 3*reg[1]){
3468 ricoh619_write(info->dev->parent, 0xb3, 0x28);
3469 // printk("PMU vadp_drop charger disable:%s In %08x %08x\n", __func__,reg[0],reg[1]);
3471 else if(data[4] & 0xc0){
3472 ret = ricoh619_read(info->dev->parent, 0xb3, &data[5]);
3473 // printk("PMU charger is disabled:%s data[4]= %08x data[5]=%08x\n", __func__,data[4],data[5]);
3474 if(((data[5] & 0x03) ==0)|| ((data[5] & 0x08)==0)){
3475 ricoh619_write(info->dev->parent, 0xb3, 0x23);
3476 ret = ricoh619_read(info->dev->parent, 0xb3, &data[5]);
3477 // printk("PMU charger enable:%s data[4]= %08x data[5]=%08x\n", __func__,data[4],data[5]);
3480 power_supply_changed(&info->battery);
3481 power_supply_changed(&powerac);
3482 power_supply_changed(&powerusb);
3483 mutex_unlock(&info->lock);
3484 queue_delayed_work(info->monitor_wqueue, &info->vadp_drop_work,3*HZ);
3489 * Get Charger Priority
3490 * - get higher-priority between VADP and VUSB
3491 * @ data: higher-priority is stored
3496 static int get_charge_priority(struct ricoh619_battery_info *info, bool *data)
3501 ret = ricoh619_read(info->dev->parent, CHGCTL1_REG, &val);
3510 * Set Charger Priority
3511 * - set higher-priority between VADP and VUSB
3512 * - data: higher-priority is stored
3517 static int set_charge_priority(struct ricoh619_battery_info *info, bool *data)
3523 ret = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, val);
3525 ret = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, val);
3530 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
3531 static int get_check_fuel_gauge_reg(struct ricoh619_battery_info *info,
3532 int Reg_h, int Reg_l, int enable_bit)
3534 uint8_t get_data_h, get_data_l;
3535 int old_data, current_data;
3541 for (i = 0; i < 5 ; i++) {
3542 ret = ricoh619_read(info->dev->parent, Reg_h, &get_data_h);
3544 dev_err(info->dev, "Error in reading the control register\n");
3548 ret = ricoh619_read(info->dev->parent, Reg_l, &get_data_l);
3550 dev_err(info->dev, "Error in reading the control register\n");
3554 current_data = ((get_data_h & 0xff) << 8) | (get_data_l & 0xff);
3555 current_data = (current_data & enable_bit);
3557 if (current_data == old_data)
3558 return current_data;
3560 old_data = current_data;
3563 return current_data;
3566 static int calc_capacity(struct ricoh619_battery_info *info)
3574 temperature = get_battery_temp_2(info) / 10; /* unit 0.1 degree -> 1 degree */
3576 if (temperature >= 25) {
3578 } else if (temperature >= 5) {
3579 nt = (25 - temperature) * RICOH619_TAH_SEL2 * 625 / 100;
3581 nt = (625 + (5 - temperature) * RICOH619_TAL_SEL2 * 625 / 100);
3584 /* get remaining battery capacity from fuel gauge */
3585 ret = ricoh619_read(info->dev->parent, SOC_REG, &capacity);
3587 dev_err(info->dev, "Error in reading the control register\n");
3591 temp = capacity * 100 * 100 / (10000 - nt);
3593 temp = min(100, temp);
3594 temp = max(0, temp);
3596 return temp; /* Unit is 1% */
3599 static int calc_capacity_2(struct ricoh619_battery_info *info)
3609 temperature = get_battery_temp_2(info) / 10; /* unit 0.1 degree -> 1 degree */
3611 if (temperature >= 25) {
3613 } else if (temperature >= 5) {
3614 nt = (25 - temperature) * RICOH619_TAH_SEL2 * 625 / 100;
3616 nt = (625 + (5 - temperature) * RICOH619_TAL_SEL2 * 625 / 100);
3619 re_cap = get_check_fuel_gauge_reg(info, RE_CAP_H_REG, RE_CAP_L_REG,
3621 fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
3625 capacity = ((long)re_cap * 100 * 100 / fa_cap);
3626 capacity = (long)(min(10000, (int)capacity));
3627 capacity = (long)(max(0, (int)capacity));
3629 ret = ricoh619_read(info->dev->parent, SOC_REG, &val);
3631 dev_err(info->dev, "Error in reading the control register\n");
3634 capacity = (long)val * 100;
3638 temp = (int)(capacity * 100 * 100 / (10000 - nt));
3640 temp = min(10000, temp);
3641 temp = max(0, temp);
3643 return temp; /* Unit is 0.01% */
3646 static int get_battery_temp(struct ricoh619_battery_info *info)
3651 ret = get_check_fuel_gauge_reg(info, TEMP_1_REG, TEMP_2_REG, 0x0fff);
3653 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
3657 /* bit3 of 0xED(TEMP_1) is sign_bit */
3658 sign_bit = ((ret & 0x0800) >> 11);
3660 ret = (ret & 0x07ff);
3662 if (sign_bit == 0) /* positive value part */
3663 /* conversion unit */
3664 /* 1 unit is 0.0625 degree and retun unit
3665 * should be 0.1 degree,
3667 ret = ret * 625 / 1000;
3668 else { /*negative value part */
3669 ret = (~ret + 1) & 0x7ff;
3670 ret = -1 * ret * 625 / 1000;
3676 static int get_battery_temp_2(struct ricoh619_battery_info *info)
3678 uint8_t reg_buff[2];
3679 long temp, temp_off, temp_gain;
3680 bool temp_sign, temp_off_sign, temp_gain_sign;
3689 /* Calculate TEMP */
3690 ret = get_check_fuel_gauge_reg(info, TEMP_1_REG, TEMP_2_REG, 0x0fff);
3692 dev_err(info->dev, "Error in reading the fuel gauge register\n");
3697 temp_sign = (reg_val & 0x0800) >> 11;
3698 reg_val = (reg_val & 0x07ff);
3700 if (temp_sign == 0) /* positive value part */
3701 /* the unit is 0.0001 degree */
3702 temp = (long)reg_val * 625;
3703 else { /*negative value part */
3704 reg_val = (~reg_val + 1) & 0x7ff;
3705 temp = -1 * (long)reg_val * 625;
3708 /* Calculate TEMP_OFF */
3709 ret = ricoh619_bulk_reads_bank1(info->dev->parent,
3710 TEMP_OFF_H_REG, 2, reg_buff);
3712 dev_err(info->dev, "Error in reading the fuel gauge register\n");
3716 reg_val = reg_buff[0] << 8 | reg_buff[1];
3717 temp_off_sign = (reg_val & 0x0800) >> 11;
3718 reg_val = (reg_val & 0x07ff);
3720 if (temp_off_sign == 0) /* positive value part */
3721 /* the unit is 0.0001 degree */
3722 temp_off = (long)reg_val * 625;
3723 else { /*negative value part */
3724 reg_val = (~reg_val + 1) & 0x7ff;
3725 temp_off = -1 * (long)reg_val * 625;
3728 /* Calculate TEMP_GAIN */
3729 ret = ricoh619_bulk_reads_bank1(info->dev->parent,
3730 TEMP_GAIN_H_REG, 2, reg_buff);
3732 dev_err(info->dev, "Error in reading the fuel gauge register\n");
3736 reg_val = reg_buff[0] << 8 | reg_buff[1];
3737 temp_gain_sign = (reg_val & 0x0800) >> 11;
3738 reg_val = (reg_val & 0x07ff);
3740 if (temp_gain_sign == 0) /* positive value part */
3741 /* 1 unit is 0.000488281. the result is 0.01 */
3742 temp_gain = (long)reg_val * 488281 / 100000;
3743 else { /*negative value part */
3744 reg_val = (~reg_val + 1) & 0x7ff;
3745 temp_gain = -1 * (long)reg_val * 488281 / 100000;
3748 /* Calculate VTHM */
3750 Vthm = (int)((temp - temp_off) / 4095 * 2500 / temp_gain);
3752 RICOH_FG_DBG("PMU %s Skip to compensate temperature\n", __func__);
3756 ret = measure_Ibatt_FG(info, &Iout);
3757 Vsns = Iout * 2 / 100;
3759 if (temp < -120000) {
3760 /* Low Temperature */
3761 if (0 != (2500 - Vthm)) {
3762 Rthm = 10 * 10 * (Vthm - Vsns) / (2500 - Vthm);
3764 RICOH_FG_DBG("PMU %s Skip to compensate temperature\n", __func__);
3769 R_ln2 = (R_ln1 * R_ln1 * R_ln1 * R_ln1 * R_ln1 / 100000
3770 - R_ln1 * R_ln1 * R_ln1 * R_ln1 * 2 / 100
3771 + R_ln1 * R_ln1 * R_ln1 * 11
3772 - R_ln1 * R_ln1 * 2980
3776 /* the unit of new_temp is 0.1 degree */
3777 new_temp = (int)((100 * 1000 * B_VALUE / (R_ln2 + B_VALUE * 100 * 1000 / 29815) - 27315) / 10);
3778 RICOH_FG_DBG("PMU %s low temperature %d\n", __func__, new_temp/10);
3779 } else if (temp > 520000) {
3780 /* High Temperature */
3781 if (0 != (2500 - Vthm)) {
3782 Rthm = 100 * 10 * (Vthm - Vsns) / (2500 - Vthm);
3784 RICOH_FG_DBG("PMU %s Skip to compensate temperature\n", __func__);
3787 RICOH_FG_DBG("PMU %s [Rthm] Rthm %d[ohm]\n", __func__, Rthm);
3790 R_ln2 = (R_ln1 * R_ln1 * R_ln1 * R_ln1 * R_ln1 / 100000 * 15652 / 100
3791 - R_ln1 * R_ln1 * R_ln1 * R_ln1 / 1000 * 23103 / 100
3792 + R_ln1 * R_ln1 * R_ln1 * 1298 / 100
3793 - R_ln1 * R_ln1 * 35089 / 100
3794 + R_ln1 * 50334 / 10
3796 /* the unit of new_temp is 0.1 degree */
3797 new_temp = (int)((100 * 100 * B_VALUE / (R_ln2 + B_VALUE * 100 * 100 / 29815) - 27315) / 10);
3798 RICOH_FG_DBG("PMU %s high temperature %d\n", __func__, new_temp/10);
3800 /* the unit of new_temp is 0.1 degree */
3801 new_temp = temp / 1000;
3807 new_temp = get_battery_temp(info);
3811 static int get_time_to_empty(struct ricoh619_battery_info *info)
3815 ret = get_check_fuel_gauge_reg(info, TT_EMPTY_H_REG, TT_EMPTY_L_REG,
3818 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
3822 /* conversion unit */
3823 /* 1unit is 1miniute and return nnit should be 1 second */
3829 static int get_time_to_full(struct ricoh619_battery_info *info)
3833 ret = get_check_fuel_gauge_reg(info, TT_FULL_H_REG, TT_FULL_L_REG,
3836 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
3845 /* battery voltage is get from Fuel gauge */
3846 static int measure_vbatt_FG(struct ricoh619_battery_info *info, int *data)
3850 if(info->soca->ready_fg == 1) {
3851 ret = get_check_fuel_gauge_reg(info, VOLTAGE_1_REG, VOLTAGE_2_REG,
3854 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
3859 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
3860 *data = *data * 50000 / 4095;
3861 /* return unit should be 1uV */
3862 *data = *data * 100;
3863 info->soca->Vbat_old = *data;
3865 *data = info->soca->Vbat_old;
3871 static int measure_Ibatt_FG(struct ricoh619_battery_info *info, int *data)
3875 ret = get_check_fuel_gauge_reg(info, CC_AVERAGE1_REG,
3876 CC_AVERAGE0_REG, 0x3fff);
3878 dev_err(info->dev, "Error in reading the fuel gauge control register\n");
3882 *data = (ret > 0x1fff) ? (ret - 0x4000) : ret;
3886 static int get_OCV_init_Data(struct ricoh619_battery_info *info, int index)
3889 ret = (battery_init_para[info->num][index*2]<<8) | (battery_init_para[info->num][index*2+1]);
3893 static int get_OCV_voltage(struct ricoh619_battery_info *info, int index)
3896 ret = get_OCV_init_Data(info, index);
3897 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
3898 ret = ret * 50000 / 4095;
3899 /* return unit should be 1uV */
3905 /* battery voltage is get from ADC */
3906 static int measure_vbatt_ADC(struct ricoh619_battery_info *info, int *data)
3909 uint8_t data_l = 0, data_h = 0;
3912 /* ADC interrupt enable */
3913 ret = ricoh619_set_bits(info->dev->parent, INTEN_REG, 0x08);
3915 dev_err(info->dev, "Error in setting the control register bit\n");
3919 /* enable interrupt request of single mode */
3920 ret = ricoh619_set_bits(info->dev->parent, EN_ADCIR3_REG, 0x01);
3922 dev_err(info->dev, "Error in setting the control register bit\n");
3926 /* single request */
3927 ret = ricoh619_write(info->dev->parent, ADCCNT3_REG, 0x10);
3929 dev_err(info->dev, "Error in writing the control register\n");
3933 for (i = 0; i < 5; i++) {
3935 RICOH_FG_DBG("ADC conversion times: %d\n", i);
3936 /* read completed flag of ADC */
3937 ret = ricoh619_read(info->dev->parent, EN_ADCIR3_REG, &data_h);
3939 dev_err(info->dev, "Error in reading the control register\n");
3947 dev_err(info->dev, "ADC conversion too long!\n");
3951 ret = ricoh619_read(info->dev->parent, VBATDATAH_REG, &data_h);
3953 dev_err(info->dev, "Error in reading the control register\n");
3957 ret = ricoh619_read(info->dev->parent, VBATDATAL_REG, &data_l);
3959 dev_err(info->dev, "Error in reading the control register\n");
3963 *data = ((data_h & 0xff) << 4) | (data_l & 0x0f);
3964 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
3965 *data = *data * 5000 / 4095;
3966 /* return unit should be 1uV */
3967 *data = *data * 1000;
3976 static int measure_vsys_ADC(struct ricoh619_battery_info *info, int *data)
3978 uint8_t data_l = 0, data_h = 0;
3981 ret = ricoh619_read(info->dev->parent, VSYSDATAH_REG, &data_h);
3983 dev_err(info->dev, "Error in reading the control register\n");
3986 ret = ricoh619_read(info->dev->parent, VSYSDATAL_REG, &data_l);
3988 dev_err(info->dev, "Error in reading the control register\n");
3991 *data = ((data_h & 0xff) << 4) | (data_l & 0x0f);
3992 *data = *data * 1000 * 3 * 5 / 2 / 4095;
3993 /* return unit should be 1uV */
3994 *data = *data * 1000;
3999 static void ricoh619_external_power_changed(struct power_supply *psy)
4001 struct ricoh619_battery_info *info;
4003 info = container_of(psy, struct ricoh619_battery_info, battery);
4004 queue_delayed_work(info->monitor_wqueue,
4005 &info->changed_work, HZ / 2);
4010 static int ricoh619_batt_get_prop(struct power_supply *psy,
4011 enum power_supply_property psp,
4012 union power_supply_propval *val)
4014 struct ricoh619_battery_info *info = dev_get_drvdata(psy->dev->parent);
4019 mutex_lock(&info->lock);
4023 case POWER_SUPPLY_PROP_ONLINE:
4024 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
4026 dev_err(info->dev, "Error in reading the control register\n");
4027 mutex_unlock(&info->lock);
4030 #ifdef SUPPORT_USB_CONNECT_TO_ADP
4031 if (psy->type == POWER_SUPPLY_TYPE_MAINS){
4032 if((2 == dwc_otg_check_dpdm(0)) && (status & 0x40))
4037 else if (psy->type == POWER_SUPPLY_TYPE_USB){
4038 if((1 == dwc_otg_check_dpdm(0)) && (status & 0x40))
4044 if (psy->type == POWER_SUPPLY_TYPE_MAINS)
4045 val->intval = (status & 0x40 ? 1 : 0);
4046 else if (psy->type == POWER_SUPPLY_TYPE_USB)
4047 val->intval = (status & 0x80 ? 1 : 0);
4050 /* this setting is same as battery driver of 584 */
4051 case POWER_SUPPLY_PROP_STATUS:
4052 if(info->chg_complete_tm_ov_flag == 0)
4054 ret = get_power_supply_Android_status(info);
4057 /* RICOH_FG_DBG("Power Supply Status is %d\n",
4062 val->intval = POWER_SUPPLY_STATUS_FULL;
4066 /* this setting is same as battery driver of 584 */
4067 case POWER_SUPPLY_PROP_PRESENT:
4068 // val->intval = info->present;
4072 /* current voltage is get from fuel gauge */
4073 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
4074 /* return real vbatt Voltage */
4075 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
4076 if (info->soca->ready_fg)
4077 ret = measure_vbatt_FG(info, &data);
4079 //val->intval = -EINVAL;
4080 data = info->cur_voltage * 1000;
4081 RICOH_FG_DBG( "battery voltage is not ready\n");
4084 ret = measure_vbatt_ADC(info, &data);
4087 /* convert unit uV -> mV */
4088 info->cur_voltage = data / 1000;
4090 RICOH_FG_DBG( "battery voltage is %d mV\n",
4094 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
4095 /* current battery capacity is get from fuel gauge */
4096 case POWER_SUPPLY_PROP_CAPACITY:
4097 if (info->entry_factory_mode){
4099 info->capacity = 100;
4100 } else if ((info->soca->displayed_soc < 0) || (info->cur_voltage == 0)) {
4102 info->capacity = 50;
4104 if(info->chg_complete_tm_ov_flag == 1)
4106 info->capacity = 100;
4107 val->intval = info->capacity;
4111 info->capacity = (info->soca->displayed_soc + 50)/100;
4112 val->intval = info->capacity;
4115 RICOH_FG_DBG("battery capacity is %d%%\n", info->capacity);
4118 /* current temperature of battery */
4119 case POWER_SUPPLY_PROP_TEMP:
4120 if (info->soca->ready_fg) {
4122 val->intval = get_battery_temp_2(info);
4123 info->battery_temp = val->intval/10;
4124 RICOH_FG_DBG( "battery temperature is %d degree\n", info->battery_temp);
4126 val->intval = info->battery_temp * 10;
4127 /* RICOH_FG_DBG("battery temperature is not ready\n"); */
4132 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
4133 if (info->soca->ready_fg) {
4134 ret = get_time_to_empty(info);
4136 info->time_to_empty = ret/60;
4137 RICOH_FG_DBG("time of empty battery is %d minutes\n", info->time_to_empty);
4139 //val->intval = -EINVAL;
4140 val->intval = info->time_to_empty * 60;
4141 RICOH_FG_DBG("time of empty battery is %d minutes\n", info->time_to_empty);
4142 /* RICOH_FG_DBG( "time of empty battery is not ready\n"); */
4146 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
4147 if (info->soca->ready_fg) {
4148 ret = get_time_to_full(info);
4150 info->time_to_full = ret/60;
4151 RICOH_FG_DBG( "time of full battery is %d minutes\n", info->time_to_full);
4153 //val->intval = -EINVAL;
4154 val->intval = info->time_to_full * 60;
4155 RICOH_FG_DBG( "time of full battery is %d minutes\n", info->time_to_full);
4156 /* RICOH_FG_DBG("time of full battery is not ready\n"); */
4162 case POWER_SUPPLY_PROP_TECHNOLOGY:
4163 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
4167 case POWER_SUPPLY_PROP_HEALTH:
4168 val->intval = POWER_SUPPLY_HEALTH_GOOD;
4171 case POWER_SUPPLY_PROP_CURRENT_AVG:
4172 measure_Ibatt_FG(info, &data);
4173 //RICOH_FG_DBG("average current xxxxxxxxxxxxxx %d \n", data);
4176 mutex_unlock(&info->lock);
4180 mutex_unlock(&info->lock);
4185 static enum power_supply_property ricoh619_batt_props[] = {
4186 POWER_SUPPLY_PROP_STATUS,
4187 POWER_SUPPLY_PROP_PRESENT,
4188 POWER_SUPPLY_PROP_VOLTAGE_NOW,
4189 POWER_SUPPLY_PROP_CURRENT_AVG,
4191 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
4192 POWER_SUPPLY_PROP_CAPACITY,
4193 POWER_SUPPLY_PROP_TEMP,
4194 //POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
4195 //POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
4197 POWER_SUPPLY_PROP_TECHNOLOGY,
4198 POWER_SUPPLY_PROP_HEALTH,
4201 static enum power_supply_property ricoh619_power_props[] = {
4202 POWER_SUPPLY_PROP_ONLINE,
4205 struct power_supply powerac = {
4207 .type = POWER_SUPPLY_TYPE_MAINS,
4208 .properties = ricoh619_power_props,
4209 .num_properties = ARRAY_SIZE(ricoh619_power_props),
4210 .get_property = ricoh619_batt_get_prop,
4213 struct power_supply powerusb = {
4215 .type = POWER_SUPPLY_TYPE_USB,
4216 .properties = ricoh619_power_props,
4217 .num_properties = ARRAY_SIZE(ricoh619_power_props),
4218 .get_property = ricoh619_batt_get_prop,
4222 static struct ricoh619_battery_platform_data *
4223 ricoh619_battery_dt_init(struct platform_device *pdev)
4225 struct device_node *nproot = pdev->dev.parent->of_node;
4226 struct device_node *np;
4227 struct ricoh619_battery_platform_data *pdata;
4230 return pdev->dev.platform_data;
4232 np = of_find_node_by_name(nproot, "battery");
4234 dev_err(&pdev->dev, "failed to find battery node\n");
4238 pdata = devm_kzalloc(&pdev->dev,
4239 sizeof(struct ricoh619_battery_platform_data),
4242 of_property_read_u32(np, "ricoh,monitor-time", &pdata->monitor_time);
4243 of_property_read_u32(np, "ricoh,alarm-vol-mv", &pdata->alarm_vol_mv);
4245 /* check rage of b,.attery type */
4246 type_n = Battery_Type();
4247 RICOH_FG_DBG("%s type_n=%d\n", __func__, type_n);
4251 of_property_read_u32(np, "ricoh,ch-vfchg", &pdata->type[0].ch_vfchg);
4252 of_property_read_u32(np, "ricoh,ch-vrchg", &pdata->type[0].ch_vrchg);
4253 of_property_read_u32(np, "ricoh,ch-vbatovset", &pdata->type[0].ch_vbatovset);
4254 of_property_read_u32(np, "ricoh,ch-ichg", &pdata->type[0].ch_ichg);
4255 of_property_read_u32(np, "ricoh,ch-ilim-adp", &pdata->type[0].ch_ilim_adp);
4256 of_property_read_u32(np, "ricoh,ch-ilim-usb", &pdata->type[0].ch_ilim_usb);
4257 of_property_read_u32(np, "ricoh,ch-icchg", &pdata->type[0].ch_icchg);
4258 of_property_read_u32(np, "ricoh,fg-target-vsys", &pdata->type[0].fg_target_vsys);
4259 of_property_read_u32(np, "ricoh,fg-target-ibat", &pdata->type[0].fg_target_ibat);
4260 of_property_read_u32(np, "ricoh,fg-poff-vbat", &pdata->type[0].fg_poff_vbat);
4261 of_property_read_u32(np, "ricoh,jt-en", &pdata->type[0].jt_en);
4262 of_property_read_u32(np, "ricoh,jt-hw-sw", &pdata->type[0].jt_hw_sw);
4263 of_property_read_u32(np, "ricoh,jt-temp-h", &pdata->type[0].jt_temp_h);
4264 of_property_read_u32(np, "ricoh,jt-temp-l", &pdata->type[0].jt_temp_l);
4265 of_property_read_u32(np, "ricoh,jt-vfchg-h", &pdata->type[0].jt_vfchg_h);
4266 of_property_read_u32(np, "ricoh,jt-vfchg-l", &pdata->type[0].jt_vfchg_l);
4267 of_property_read_u32(np, "ricoh,jt-ichg-h", &pdata->type[0].jt_ichg_h);
4268 of_property_read_u32(np, "ricoh,jt-ichg-l", &pdata->type[0].jt_ichg_l);
4272 of_property_read_u32(np, "ricoh,ch-vfchg-1", &pdata->type[1].ch_vfchg);
4273 of_property_read_u32(np, "ricoh,ch-vrchg-1", &pdata->type[1].ch_vrchg);
4274 of_property_read_u32(np, "ricoh,ch-vbatovset-1", &pdata->type[1].ch_vbatovset);
4275 of_property_read_u32(np, "ricoh,ch-ichg-1", &pdata->type[1].ch_ichg);
4276 of_property_read_u32(np, "ricoh,ch-ilim-adp-1", &pdata->type[1].ch_ilim_adp);
4277 of_property_read_u32(np, "ricoh,ch-ilim-usb-1", &pdata->type[1].ch_ilim_usb);
4278 of_property_read_u32(np, "ricoh,ch-icchg-1", &pdata->type[1].ch_icchg);
4279 of_property_read_u32(np, "ricoh,fg-target-vsys-1", &pdata->type[1].fg_target_vsys);
4280 of_property_read_u32(np, "ricoh,fg-target-ibat-1", &pdata->type[1].fg_target_ibat);
4281 of_property_read_u32(np, "ricoh,fg-poff-vbat-1", &pdata->type[1].fg_poff_vbat);
4282 of_property_read_u32(np, "ricoh,jt-en-1", &pdata->type[1].jt_en);
4283 of_property_read_u32(np, "ricoh,jt-hw-sw-1", &pdata->type[1].jt_hw_sw);
4284 of_property_read_u32(np, "ricoh,jt-temp-h-1", &pdata->type[1].jt_temp_h);
4285 of_property_read_u32(np, "ricoh,jt-temp-l-1", &pdata->type[1].jt_temp_l);
4286 of_property_read_u32(np, "ricoh,jt-vfchg-h-1", &pdata->type[1].jt_vfchg_h);
4287 of_property_read_u32(np, "ricoh,jt-vfchg-l-1", &pdata->type[1].jt_vfchg_l);
4288 of_property_read_u32(np, "ricoh,jt-ichg-h-1", &pdata->type[1].jt_ichg_h);
4289 of_property_read_u32(np, "ricoh,jt-ichg-l-1", &pdata->type[1].jt_ichg_l);
4302 static struct ricoh619_battery_platform_data *
4303 ricoh619_battery_dt_init(struct platform_device *pdev)
4305 return pdev->dev.platform_data;
4309 static int ricoh619_battery_probe(struct platform_device *pdev)
4311 struct ricoh619_battery_info *info;
4312 struct ricoh619_battery_platform_data *pdata;
4313 struct ricoh619 *ricoh619 = dev_get_drvdata(pdev->dev.parent);
4316 RICOH_FG_DBG(KERN_INFO "PMU: %s : version is %s\n", __func__,RICOH619_BATTERY_VERSION);
4318 pdata = ricoh619_battery_dt_init(pdev);
4320 dev_err(&pdev->dev, "platform data isn't assigned to "
4324 info = devm_kzalloc(ricoh619->dev,sizeof(struct ricoh619_battery_info), GFP_KERNEL);
4327 info->soca = devm_kzalloc(ricoh619->dev,sizeof(struct ricoh619_soca_info), GFP_KERNEL);
4331 info->dev = &pdev->dev;
4332 info->status = POWER_SUPPLY_STATUS_CHARGING;
4333 info->monitor_time = pdata->monitor_time * HZ;
4334 info->alarm_vol_mv = pdata->alarm_vol_mv;
4336 /* check rage of battery num */
4337 info->num = Battery_Table();
4338 temp = sizeof(battery_init_para)/(sizeof(uint8_t)*32);
4339 if(info->num >= (sizeof(battery_init_para)/(sizeof(uint8_t)*32)))
4341 RICOH_FG_DBG("%s : Battery num is out of range\n", __func__);
4344 RICOH_FG_DBG("%s info->num=%d,temp is %d\n", __func__, info->num,temp);
4346 /* these valuse are set in platform */
4347 info->ch_vfchg = pdata->type[type_n].ch_vfchg;
4348 info->ch_vrchg = pdata->type[type_n].ch_vrchg;
4349 info->ch_vbatovset = pdata->type[type_n].ch_vbatovset;
4350 info->ch_ichg = pdata->type[type_n].ch_ichg;
4351 info->ch_ilim_adp = pdata->type[type_n].ch_ilim_adp;
4352 info->ch_ilim_usb = pdata->type[type_n].ch_ilim_usb;
4353 info->ch_icchg = pdata->type[type_n].ch_icchg;
4354 info->fg_target_vsys = pdata->type[type_n].fg_target_vsys;
4355 info->fg_target_ibat = pdata->type[type_n].fg_target_ibat;
4356 info->fg_poff_vbat = pdata->type[type_n].fg_poff_vbat;
4357 info->jt_en = pdata->type[type_n].jt_en;
4358 info->jt_hw_sw = pdata->type[type_n].jt_hw_sw;
4359 info->jt_temp_h = pdata->type[type_n].jt_temp_h;
4360 info->jt_temp_l = pdata->type[type_n].jt_temp_l;
4361 info->jt_vfchg_h = pdata->type[type_n].jt_vfchg_h;
4362 info->jt_vfchg_l = pdata->type[type_n].jt_vfchg_l;
4363 info->jt_ichg_h = pdata->type[type_n].jt_ichg_h;
4364 info->jt_ichg_l = pdata->type[type_n].jt_ichg_l;
4366 info->adc_vdd_mv = ADC_VDD_MV; /* 2800; */
4367 info->min_voltage = MIN_VOLTAGE; /* 3100; */
4368 info->max_voltage = MAX_VOLTAGE; /* 4200; */
4370 info->entry_factory_mode = false;
4372 info->chg_complete_rd_flag = 0;
4373 info->chg_complete_rd_cnt = 0;
4374 info->chg_complete_tm_ov_flag = 0;
4375 info->chg_complete_sleep_flag = 0;
4377 mutex_init(&info->lock);
4378 platform_set_drvdata(pdev, info);
4380 info->battery.name = "battery";
4381 info->battery.type = POWER_SUPPLY_TYPE_BATTERY;
4382 info->battery.properties = ricoh619_batt_props;
4383 info->battery.num_properties = ARRAY_SIZE(ricoh619_batt_props);
4384 info->battery.get_property = ricoh619_batt_get_prop;
4385 info->battery.set_property = NULL;
4386 /* info->battery.external_power_changed
4387 = ricoh619_external_power_changed; */
4389 /* Disable Charger/ADC interrupt */
4390 ret = ricoh619_clr_bits(info->dev->parent, RICOH619_INTC_INTEN,
4395 ret = ricoh619_init_battery(info);
4399 #ifdef ENABLE_FACTORY_MODE
4400 info->factory_mode_wqueue
4401 = create_singlethread_workqueue("ricoh619_factory_mode");
4402 INIT_DEFERRABLE_WORK(&info->factory_mode_work,
4403 check_charging_state_work);
4405 ret = ricoh619_factory_mode(info);
4411 ret = power_supply_register(&pdev->dev, &info->battery);
4414 info->battery.dev->parent = &pdev->dev;
4416 ret = power_supply_register(&pdev->dev, &powerac);
4417 ret = power_supply_register(&pdev->dev, &powerusb);
4419 info->monitor_wqueue
4420 = create_singlethread_workqueue("ricoh619_battery_monitor");
4422 info->workqueue = create_singlethread_workqueue("rc5t619_charger_in");
4423 INIT_WORK(&info->irq_work, charger_irq_work);
4426 = create_singlethread_workqueue("rc5t619_usb_det");
4427 INIT_WORK(&info->usb_irq_work, usb_det_irq_work);
4429 INIT_DEFERRABLE_WORK(&info->monitor_work,
4430 ricoh619_battery_work);
4431 INIT_DEFERRABLE_WORK(&info->displayed_work,
4432 ricoh619_displayed_work);
4433 INIT_DEFERRABLE_WORK(&info->charge_stable_work,
4434 ricoh619_stable_charge_countdown_work);
4435 INIT_DEFERRABLE_WORK(&info->charge_monitor_work,
4436 ricoh619_charge_monitor_work);
4437 INIT_DEFERRABLE_WORK(&info->get_charge_work,
4438 ricoh619_get_charge_work);
4439 INIT_DEFERRABLE_WORK(&info->jeita_work, ricoh619_jeita_work);
4440 INIT_DELAYED_WORK(&info->changed_work, ricoh619_changed_work);
4442 INIT_DELAYED_WORK(&info->charge_complete_ready, ricoh619_charging_complete_work);
4444 /* Charger IRQ workqueue settings */
4446 ret = request_threaded_irq( irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FONCHGINT),NULL, charger_in_isr, IRQF_ONESHOT,
4447 "rc5t619_charger_in", info);
4449 dev_err(&pdev->dev, "Can't get CHG_INT IRQ for chrager: %d\n",ret);
4453 ret = request_threaded_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FCHGCMPINT),NULL, charger_complete_isr,
4454 IRQF_ONESHOT, "rc5t619_charger_comp",info);
4456 dev_err(&pdev->dev, "Can't get CHG_COMP IRQ for chrager: %d\n",ret);
4460 ret = request_threaded_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FVUSBDETSINT) ,NULL, charger_usb_isr, IRQF_ONESHOT,
4461 "rc5t619_usb_det", info);
4463 dev_err(&pdev->dev, "Can't get USB_DET IRQ for chrager: %d\n",ret);
4467 ret = request_threaded_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FVADPDETSINT),NULL, charger_adp_isr, IRQF_ONESHOT,
4468 "rc5t619_adp_det", info);
4471 "Can't get ADP_DET IRQ for chrager: %d\n", ret);
4475 #ifdef ENABLE_LOW_BATTERY_DETECTION
4476 ret = request_threaded_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_VSYSLIR) ,NULL, adc_vsysl_isr, IRQF_ONESHOT,
4477 "rc5t619_adc_vsysl", info);
4480 "Can't get ADC_VSYSL IRQ for chrager: %d\n", ret);
4483 INIT_DEFERRABLE_WORK(&info->low_battery_work,
4484 low_battery_irq_work);
4486 #ifdef RICOH619_VADP_DROP_WORK
4487 INIT_DEFERRABLE_WORK(&info->vadp_drop_work,vadp_drop_irq_work);
4488 queue_delayed_work(info->monitor_wqueue, &info->vadp_drop_work,0);
4490 /* Charger init and IRQ setting */
4491 ret = ricoh619_init_charger(info);
4495 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
4496 ret = ricoh619_init_fgsoca(info);
4498 queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
4499 RICOH619_MONITOR_START_TIME*HZ);
4501 /* Enable Charger/ADC interrupt */
4502 ricoh619_set_bits(info->dev->parent, RICOH619_INTC_INTEN, CHG_INT | ADC_INT);
4510 static int ricoh619_battery_remove(struct platform_device *pdev)
4512 struct ricoh619_battery_info *info = platform_get_drvdata(pdev);
4513 struct ricoh619 *ricoh619 = dev_get_drvdata(pdev->dev.parent);
4518 bool is_charging = true;
4519 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
4521 && (info->soca->status == RICOH619_SOCA_STABLE)) {
4522 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
4524 dev_err(info->dev, "Error in writing PSWR_REG\n");
4526 } else if (info->soca->status != RICOH619_SOCA_START
4527 && info->soca->status != RICOH619_SOCA_UNSTABLE) {
4528 if (info->soca->displayed_soc <= 0) {
4531 val = (info->soca->displayed_soc + 50)/100;
4534 ret = ricoh619_write(info->dev->parent, PSWR_REG, val);
4536 dev_err(info->dev, "Error in writing PSWR_REG\n");
4540 ret = calc_capacity_in_period(info, &cc_cap,
4541 &is_charging, true);
4543 dev_err(info->dev, "Read cc_sum Error !!-----\n");
4546 if (g_fg_on_mode == 0) {
4547 ret = ricoh619_clr_bits(info->dev->parent,
4550 dev_err(info->dev, "Error in clr FG EN\n");
4553 /* set rapid timer 300 min */
4554 err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x03);
4556 dev_err(info->dev, "Error in writing the control register\n");
4559 if(info->capacity == 100)
4561 ret = ricoh619_write(info->dev->parent, PSWR_REG, 100);
4563 dev_err(info->dev, "Error in writing PSWR_REG\n");
4566 free_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FONCHGINT), &info);
4567 free_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FCHGCMPINT), &info);
4568 free_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FVUSBDETSINT), &info);
4569 free_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_FVADPDETSINT) , &info);
4570 #ifdef ENABLE_LOW_BATTERY_DETECTION
4571 free_irq(irq_create_mapping(ricoh619->irq_domain, RICOH619_IRQ_VSYSLIR), &info);
4574 cancel_delayed_work(&info->monitor_work);
4575 cancel_delayed_work(&info->charge_stable_work);
4576 cancel_delayed_work(&info->charge_monitor_work);
4577 cancel_delayed_work(&info->get_charge_work);
4578 cancel_delayed_work(&info->displayed_work);
4580 cancel_delayed_work(&info->changed_work);
4581 #ifdef ENABLE_LOW_BATTERY_DETECTION
4582 cancel_delayed_work(&info->low_battery_work);
4584 #ifdef RICOH619_VADP_DROP_WORK
4585 cancel_delayed_work(&info->vadp_drop_work);
4587 #ifdef ENABLE_FACTORY_MODE
4588 cancel_delayed_work(&info->factory_mode_work);
4590 cancel_delayed_work(&info->jeita_work);
4591 cancel_delayed_work(&info->charge_complete_ready);
4593 cancel_work_sync(&info->irq_work);
4594 cancel_work_sync(&info->usb_irq_work);
4596 flush_workqueue(info->monitor_wqueue);
4597 flush_workqueue(info->workqueue);
4598 flush_workqueue(info->usb_workqueue);
4599 #ifdef ENABLE_FACTORY_MODE
4600 flush_workqueue(info->factory_mode_wqueue);
4602 destroy_workqueue(info->monitor_wqueue);
4603 destroy_workqueue(info->workqueue);
4604 destroy_workqueue(info->usb_workqueue);
4605 #ifdef ENABLE_FACTORY_MODE
4606 destroy_workqueue(info->factory_mode_wqueue);
4609 power_supply_unregister(&info->battery);
4610 platform_set_drvdata(pdev, NULL);
4615 struct timeval ts_suspend;
4616 static int ricoh619_battery_suspend(struct device *dev)
4618 struct ricoh619_battery_info *info = dev_get_drvdata(dev);
4623 bool is_charging = true;
4624 int displayed_soc_temp;
4625 do_gettimeofday(&ts_suspend);
4628 && (info->soca->status == RICOH619_SOCA_STABLE)) {
4629 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
4631 dev_err(info->dev, "Error in writing PSWR_REG\n");
4633 info->soca->suspend_soc = info->soca->displayed_soc;
4634 ret = calc_capacity_in_period(info, &cc_cap,
4635 &is_charging, true);
4637 dev_err(info->dev, "Read cc_sum Error !!-----\n");
4639 } else if (info->soca->status != RICOH619_SOCA_START
4640 && info->soca->status != RICOH619_SOCA_UNSTABLE) {
4641 if (info->soca->displayed_soc <= 0) {
4644 val = (info->soca->displayed_soc + 50)/100;
4647 ret = ricoh619_write(info->dev->parent, PSWR_REG, val);
4649 dev_err(info->dev, "Error in writing PSWR_REG\n");
4653 ret = calc_capacity_in_period(info, &cc_cap,
4654 &is_charging, true);
4656 dev_err(info->dev, "Read cc_sum Error !!-----\n");
4658 if (info->soca->status != RICOH619_SOCA_STABLE) {
4659 info->soca->cc_delta
4660 = (is_charging == true) ? cc_cap : -cc_cap;
4663 = info->soca->displayed_soc + info->soca->cc_delta;
4664 displayed_soc_temp = min(10000, displayed_soc_temp);
4665 displayed_soc_temp = max(0, displayed_soc_temp);
4666 info->soca->displayed_soc = displayed_soc_temp;
4668 info->soca->suspend_soc = info->soca->displayed_soc;
4670 } else if (info->soca->status == RICOH619_SOCA_START
4671 || info->soca->status == RICOH619_SOCA_UNSTABLE) {
4673 ret = ricoh619_read(info->dev->parent, PSWR_REG, &val);
4675 dev_err(info->dev, "Error in reading the pswr register\n");
4678 info->soca->suspend_soc = val * 100;
4681 if (info->soca->status == RICOH619_SOCA_DISP
4682 || info->soca->status == RICOH619_SOCA_STABLE
4683 || info->soca->status == RICOH619_SOCA_FULL) {
4684 info->soca->soc = calc_capacity_2(info);
4685 info->soca->soc_delta =
4686 info->soca->soc_delta + (info->soca->soc - info->soca->last_soc);
4689 info->soca->soc_delta = 0;
4692 if (info->soca->status == RICOH619_SOCA_STABLE
4693 || info->soca->status == RICOH619_SOCA_FULL)
4694 info->soca->status = RICOH619_SOCA_DISP;
4695 /* set rapid timer 300 min */
4696 err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x03);
4698 dev_err(info->dev, "Error in writing the control register\n");
4701 // disable_irq(charger_irq + RICOH619_IRQ_FONCHGINT);
4702 // disable_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT);
4703 // disable_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT);
4704 // disable_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT);
4705 #ifdef ENABLE_LOW_BATTERY_DETECTION
4706 // disable_irq(charger_irq + RICOH619_IRQ_VSYSLIR);
4709 flush_delayed_work(&info->monitor_work);
4710 flush_delayed_work(&info->displayed_work);
4711 flush_delayed_work(&info->charge_stable_work);
4712 flush_delayed_work(&info->charge_monitor_work);
4713 flush_delayed_work(&info->get_charge_work);
4714 flush_delayed_work(&info->changed_work);
4715 #ifdef ENABLE_LOW_BATTERY_DETECTION
4716 flush_delayed_work(&info->low_battery_work);
4718 flush_delayed_work(&info->factory_mode_work);
4719 flush_delayed_work(&info->jeita_work);
4720 #ifdef RICOH619_VADP_DROP_WORK
4721 flush_delayed_work(&info->vadp_drop_work);
4724 // flush_work(&info->irq_work);
4725 // flush_work(&info->usb_irq_work);
4727 cancel_delayed_work(&info->monitor_work);
4728 cancel_delayed_work(&info->displayed_work);
4729 cancel_delayed_work(&info->charge_stable_work);
4730 cancel_delayed_work(&info->charge_monitor_work);
4731 cancel_delayed_work(&info->get_charge_work);
4732 cancel_delayed_work(&info->changed_work);
4733 #ifdef ENABLE_LOW_BATTERY_DETECTION
4734 cancel_delayed_work(&info->low_battery_work);
4736 /* cancel_delayed_work(&info->charge_complete_ready);*/
4737 #ifdef ENABLE_FACTORY_MODE
4738 cancel_delayed_work(&info->factory_mode_work);
4740 cancel_delayed_work(&info->jeita_work);
4741 #ifdef RICOH619_VADP_DROP_WORK
4742 cancel_delayed_work(&info->vadp_drop_work);
4744 /* info->chg_complete_rd_cnt = 0;*/
4745 /* info->chg_complete_rd_flag = 0;*/
4747 if(info->capacity == 100)
4749 ret = ricoh619_write(info->dev->parent, PSWR_REG, 100);
4751 dev_err(info->dev, "Error in writing PSWR_REG\n");
4752 if(info->chg_complete_tm_ov_flag != 1)
4754 info->chg_complete_tm_ov_flag = 0;
4755 info->chg_complete_sleep_flag = 1;
4758 // flush_work(&info->irq_work);
4759 // flush_work(&info->usb_irq_work);
4765 static int ricoh619_battery_resume(struct device *dev)
4767 struct ricoh619_battery_info *info = dev_get_drvdata(dev);
4770 int displayed_soc_temp;
4772 bool is_charging = true;
4773 bool is_jeita_updated;
4777 struct timespec tv = {
4778 .tv_nsec = NSEC_PER_SEC >> 1,
4780 struct rtc_device *rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
4782 err = rtc_read_time(rtc, &tm);
4784 dev_err(rtc->dev.parent,
4785 "hctosys: unable to read the hardware clock\n");
4788 err = rtc_valid_tm(&tm);
4790 dev_err(rtc->dev.parent,
4791 "hctosys: invalid date/time\n");
4794 rtc_tm_to_time(&tm, &tv.tv_sec);
4796 /*printk("suspend time: %d sec\n", ts_suspend.tv_sec);*/
4797 /*printk("resume time: %d sec\n", tv.tv_sec);*/
4799 if(info->chg_complete_rd_flag == 2){
4800 printk("chg_complete_rd_cnt suspend: %d\n", info->chg_complete_rd_cnt);
4801 info->chg_complete_rd_cnt += (tv.tv_sec - ts_suspend.tv_sec);
4802 printk("chg_complete_rd_cnt resume: %d\n", info->chg_complete_rd_cnt);
4803 flush_work(&info->charge_complete_ready.work);
4806 RICOH_FG_DBG(KERN_INFO "PMU: %s: \n", __func__);
4808 ret = check_jeita_status(info, &is_jeita_updated);
4810 dev_err(info->dev, "Error in updating JEITA %d\n", ret);
4813 if (info->entry_factory_mode) {
4814 info->soca->displayed_soc = -EINVAL;
4815 } else if (RICOH619_SOCA_ZERO == info->soca->status) {
4816 if (calc_ocv(info) > get_OCV_voltage(info, 0)) {
4817 RICOH_FG_DBG(KERN_INFO "PMU: %s: RICOH619_SOCA_ZERO if ()...\n", __func__);
4818 ret = ricoh619_read(info->dev->parent, PSWR_REG, &val);
4820 info->soca->soc = val * 100;
4823 "Error in reading PSWR_REG %d\n", ret);
4825 = calc_capacity(info) * 100;
4828 ret = calc_capacity_in_period(info, &cc_cap,
4829 &is_charging, true);
4831 dev_err(info->dev, "Read cc_sum Error !!-----\n");
4833 info->soca->cc_delta
4834 = (is_charging == true) ? cc_cap : -cc_cap;
4837 = info->soca->soc + info->soca->cc_delta;
4838 if (displayed_soc_temp < 0)
4839 displayed_soc_temp = 0;
4840 displayed_soc_temp = min(10000, displayed_soc_temp);
4841 displayed_soc_temp = max(0, displayed_soc_temp);
4842 info->soca->displayed_soc = displayed_soc_temp;
4844 ret = reset_FG_process(info);
4847 dev_err(info->dev, "Error in writing the control register\n");
4848 info->soca->status = RICOH619_SOCA_FG_RESET;
4851 RICOH_FG_DBG(KERN_INFO "PMU: %s: RICOH619_SOCA_ZERO else()...\n", __func__);
4852 /*info->soca->displayed_soc = 0;*/
4853 info->soca->displayed_soc = info->soca->suspend_soc;
4856 info->soca->soc = info->soca->suspend_soc;
4858 if (RICOH619_SOCA_START == info->soca->status
4859 || RICOH619_SOCA_UNSTABLE == info->soca->status) {
4860 ret = calc_capacity_in_period(info, &cc_cap,
4861 &is_charging, false);
4863 ret = calc_capacity_in_period(info, &cc_cap,
4864 &is_charging, true);
4868 dev_err(info->dev, "Read cc_sum Error !!-----\n");
4870 info->soca->cc_delta = (is_charging == true) ? cc_cap : -cc_cap;
4872 displayed_soc_temp = info->soca->soc + info->soca->cc_delta;
4873 if (info->soca->zero_flg == 1) {
4874 if((info->soca->Ibat_ave >= 0)
4875 || (displayed_soc_temp >= 100)){
4876 info->soca->zero_flg = 0;
4878 displayed_soc_temp = 0;
4880 } else if (displayed_soc_temp < 100) {
4881 /* keep DSOC = 1 when Vbat is over 3.4V*/
4882 if( info->fg_poff_vbat != 0) {
4883 if (info->soca->Vbat_ave < 2000*1000) { /* error value */
4884 displayed_soc_temp = 100;
4885 } else if (info->soca->Vbat_ave < info->fg_poff_vbat*1000) {
4886 displayed_soc_temp = 0;
4887 info->soca->zero_flg = 1;
4889 displayed_soc_temp = 100;
4893 displayed_soc_temp = min(10000, displayed_soc_temp);
4894 displayed_soc_temp = max(0, displayed_soc_temp);
4896 if (0 == info->soca->jt_limit) {
4897 check_charge_status_2(info, displayed_soc_temp);
4899 info->soca->displayed_soc = displayed_soc_temp;
4902 if (RICOH619_SOCA_DISP == info->soca->status) {
4903 info->soca->last_soc = calc_capacity_2(info);
4907 ret = measure_vbatt_FG(info, &info->soca->Vbat_ave);
4908 ret = measure_vsys_ADC(info, &info->soca->Vsys_ave);
4909 ret = measure_Ibatt_FG(info, &info->soca->Ibat_ave);
4911 if(info->chg_complete_sleep_flag == 1)
4913 info->chg_complete_tm_ov_flag = 1;
4914 info->chg_complete_sleep_flag = 0;
4917 power_supply_changed(&info->battery);
4918 queue_delayed_work(info->monitor_wqueue, &info->displayed_work, HZ);
4920 if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
4921 info->soca->stable_count = 10;
4922 queue_delayed_work(info->monitor_wqueue,
4923 &info->charge_stable_work,
4924 RICOH619_FG_STABLE_TIME*HZ/10);
4925 } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
4926 info->soca->stable_count = 1;
4928 for (i = 0; i < 3; i = i+1)
4929 info->soca->reset_soc[i] = 0;
4930 info->soca->reset_count = 0;
4932 queue_delayed_work(info->monitor_wqueue,
4933 &info->charge_stable_work,
4934 RICOH619_FG_RESET_TIME*HZ);
4937 queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
4938 info->monitor_time);
4940 queue_delayed_work(info->monitor_wqueue, &info->charge_monitor_work,
4941 RICOH619_CHARGE_RESUME_TIME * HZ);
4943 info->soca->chg_count = 0;
4944 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
4945 RICOH619_CHARGE_RESUME_TIME * HZ);
4946 #ifdef RICOH619_VADP_DROP_WORK
4947 queue_delayed_work(info->monitor_wqueue, &info->vadp_drop_work,1 * HZ);
4950 if (!info->jt_hw_sw) {
4951 queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
4952 RICOH619_JEITA_UPDATE_TIME * HZ);
4955 // ricoh619_write(info->dev->parent, 0x9d, 0x00);
4956 // enable_irq(charger_irq + RICOH619_IRQ_FONCHGINT);
4957 // enable_irq(charger_irq + RICOH619_IRQ_FCHGCMPINT);
4958 // enable_irq(charger_irq + RICOH619_IRQ_FVUSBDETSINT);
4959 // enable_irq(charger_irq + RICOH619_IRQ_FVADPDETSINT);
4960 #ifdef ENABLE_LOW_BATTERY_DETECTION
4961 // enable_irq(charger_irq + RICOH619_IRQ_VSYSLIR);
4963 ricoh619_write(info->dev->parent, 0x9d, 0x4d);
4968 static const struct of_device_id ricoh619_battery_dt_match[] = {
4969 { .compatible = "ricoh,ricoh619-battery", },
4972 MODULE_DEVICE_TABLE(of, ricoh619_battery_dt_match);
4976 static const struct dev_pm_ops ricoh619_battery_pm_ops = {
4977 .suspend = ricoh619_battery_suspend,
4978 .resume = ricoh619_battery_resume,
4982 static struct platform_driver ricoh619_battery_driver = {
4984 .name = "ricoh619-battery",
4985 .owner = THIS_MODULE,
4986 .of_match_table = of_match_ptr(ricoh619_battery_dt_match),
4988 .pm = &ricoh619_battery_pm_ops,
4991 .probe = ricoh619_battery_probe,
4992 .remove = ricoh619_battery_remove,
4995 static int __init ricoh619_battery_init(void)
4997 RICOH_FG_DBG("PMU: %s\n", __func__);
4998 return platform_driver_register(&ricoh619_battery_driver);
5000 fs_initcall_sync(ricoh619_battery_init);
5002 static void __exit ricoh619_battery_exit(void)
5004 platform_driver_unregister(&ricoh619_battery_driver);
5006 module_exit(ricoh619_battery_exit);
5008 MODULE_DESCRIPTION("RICOH619 Battery driver");
5009 MODULE_ALIAS("platform:ricoh619-battery");
5010 MODULE_LICENSE("GPL");