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/rk_keys.h>
39 #include <linux/rtc.h>
41 #include <linux/interrupt.h>
42 #include <linux/irq.h>
43 #include <linux/irqdomain.h>
46 /* define for function */
47 #define ENABLE_FUEL_GAUGE_FUNCTION
48 #define ENABLE_LOW_BATTERY_DETECTION
49 //#define ENABLE_FACTORY_MODE
50 #define DISABLE_CHARGER_TIMER
51 /* #define ENABLE_FG_KEEP_ON_MODE */
52 /* #define ENABLE_OCV_TABLE_CALIB */
53 //#define SUPPORT_USB_CONNECT_TO_ADP
57 #define RICOH619_REL1_SEL_VALUE 64
58 #define RICOH619_REL2_SEL_VALUE 0
68 //for debug #ifdef ENABLE_FUEL_GAUGE_FUNCTION
69 /* define for FG delayed time */
70 #define RICOH619_MONITOR_START_TIME 15
71 #define RICOH619_FG_RESET_TIME 6
72 #define RICOH619_FG_STABLE_TIME 120
73 #define RICOH619_DISPLAY_UPDATE_TIME 15
74 #define RICOH619_LOW_VOL_DOWN_TIME 10
75 #define RICOH619_CHARGE_MONITOR_TIME 20
76 #define RICOH619_CHARGE_RESUME_TIME 1
77 #define RICOH619_CHARGE_CALC_TIME 1
78 #define RICOH619_JEITA_UPDATE_TIME 60
79 #define RICOH619_DELAY_TIME 60
80 /* define for FG parameter */
81 #define RICOH619_MAX_RESET_SOC_DIFF 5
82 #define RICOH619_GET_CHARGE_NUM 10
83 #define RICOH619_UPDATE_COUNT_DISP 4
84 #define RICOH619_UPDATE_COUNT_FULL 4
85 #define RICOH619_UPDATE_COUNT_FULL_RESET 7
86 #define RICOH619_CHARGE_UPDATE_TIME 3
87 #define RICOH619_FULL_WAIT_TIME 4
88 #define RE_CAP_GO_DOWN 10 /* 40 */
89 #define RICOH619_ENTER_LOW_VOL 70
90 #define RICOH619_TAH_SEL2 5
91 #define RICOH619_TAL_SEL2 6
93 #define RICOH619_OCV_OFFSET_BOUND 3
94 #define RICOH619_OCV_OFFSET_RATIO 2
96 #define RICOH619_VADP_DROP_WORK
97 #define RICOH619_TIME_CHG_STEP (1*HZ)// unit:secound
98 #define RICOH619_TIME_CHG_COUNT 15*60//only for test //15*60
100 /* define for FG status */
103 RICOH619_SOCA_UNSTABLE,
104 RICOH619_SOCA_FG_RESET,
106 RICOH619_SOCA_STABLE,
109 RICOH619_SOCA_LOW_VOL,
113 #ifdef ENABLE_LOW_BATTERY_DETECTION
114 #define LOW_BATTERY_DETECTION_TIME 10
117 struct ricoh619_soca_info {
120 int ocv_table_def[11];
122 int ocv_table_low[11];
123 int soc; /* Latest FG SOC value */
126 int status; /* SOCA status 0: Not initial; 5: Finished */
128 int chg_status; /* chg_status */
129 int soc_delta; /* soc delta for status3(DISP) */
133 int last_displayed_soc;
139 int Vbat[RICOH619_GET_CHARGE_NUM];
140 int Vsys[RICOH619_GET_CHARGE_NUM];
141 int Ibat[RICOH619_GET_CHARGE_NUM];
147 int full_reset_count;
150 /* for LOW VOL state */
168 struct ricoh619_battery_info {
170 struct power_supply battery;
171 struct delayed_work monitor_work;
172 struct delayed_work displayed_work;
173 struct delayed_work charge_stable_work;
174 struct delayed_work changed_work;
175 #ifdef ENABLE_LOW_BATTERY_DETECTION
176 struct delayed_work low_battery_work;
178 struct delayed_work charge_monitor_work;
179 struct delayed_work get_charge_work;
180 struct delayed_work jeita_work;
181 struct delayed_work charge_complete_ready;
183 struct work_struct irq_work; /* for Charging & VUSB/VADP */
184 struct work_struct usb_irq_work; /* for ADC_VUSB */
185 #ifdef RICOH619_VADP_DROP_WORK
186 struct delayed_work vadp_drop_work;
188 struct workqueue_struct *monitor_wqueue;
189 struct workqueue_struct *workqueue; /* for Charging & VUSB/VADP */
190 struct workqueue_struct *usb_workqueue; /* for ADC_VUSB */
192 #ifdef ENABLE_FACTORY_MODE
193 struct delayed_work factory_mode_work;
194 struct workqueue_struct *factory_mode_wqueue;
198 unsigned long monitor_time;
214 struct ricoh619_soca_info *soca;
216 bool entry_factory_mode;
236 int chg_complete_rd_flag;
237 int chg_complete_rd_cnt;
238 int chg_complete_tm_ov_flag;
239 int chg_complete_sleep_flag;
245 struct power_supply powerac;
246 struct power_supply powerusb;
250 /* this value is for mfd fucntion */
254 extern int dwc_otg_check_dpdm(bool wait);
255 /*This is for full state*/
256 static int BatteryTableFlagDef=0;
257 static int BatteryTypeDef=0;
258 static int Battery_Type(void)
260 return BatteryTypeDef;
263 static int Battery_Table(void)
265 return BatteryTableFlagDef;
268 static void ricoh619_battery_work(struct work_struct *work)
270 struct ricoh619_battery_info *info = container_of(work,
271 struct ricoh619_battery_info, monitor_work.work);
273 RICOH_FG_DBG("PMU: %s\n", __func__);
274 power_supply_changed(&info->battery);
275 queue_delayed_work(info->monitor_wqueue, &info->monitor_work,
279 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
280 static int measure_vbatt_FG(struct ricoh619_battery_info *info, int *data);
281 static int measure_Ibatt_FG(struct ricoh619_battery_info *info, int *data);
282 static int calc_capacity(struct ricoh619_battery_info *info);
283 static int calc_capacity_2(struct ricoh619_battery_info *info);
284 static int get_OCV_init_Data(struct ricoh619_battery_info *info, int index);
285 static int get_OCV_voltage(struct ricoh619_battery_info *info, int index);
286 static int get_check_fuel_gauge_reg(struct ricoh619_battery_info *info,
287 int Reg_h, int Reg_l, int enable_bit);
288 static int calc_capacity_in_period(struct ricoh619_battery_info *info,
289 int *cc_cap, bool *is_charging, bool cc_rst);
290 //static int get_charge_priority(struct ricoh619_battery_info *info, bool *data);
291 //static int set_charge_priority(struct ricoh619_battery_info *info, bool *data);
292 static int get_power_supply_status(struct ricoh619_battery_info *info);
293 static int get_power_supply_Android_status(struct ricoh619_battery_info *info);
294 static int measure_vsys_ADC(struct ricoh619_battery_info *info, int *data);
295 static int Calc_Linear_Interpolation(int x0, int y0, int x1, int y1, int y);
296 static int get_battery_temp(struct ricoh619_battery_info *info);
297 static int get_battery_temp_2(struct ricoh619_battery_info *info);
298 static int check_jeita_status(struct ricoh619_battery_info *info, bool *is_jeita_updated);
299 static void ricoh619_scaling_OCV_table(struct ricoh619_battery_info *info, int cutoff_vol, int full_vol, int *start_per, int *end_per);
300 //static int ricoh619_Check_OCV_Offset(struct ricoh619_battery_info *info);
302 static int calc_ocv(struct ricoh619_battery_info *info)
309 ret = measure_vbatt_FG(info, &Vbat);
310 ret = measure_Ibatt_FG(info, &Ibat);
312 ocv = Vbat - Ibat * info->soca->Rbat;
318 static int set_Rlow(struct ricoh619_battery_info *info)
326 if (info->soca->Rbat == 0)
327 info->soca->Rbat = get_OCV_init_Data(info, 12) * 1000 / 512
330 Vocv = calc_ocv(info);
331 Rbat_low_max = info->soca->Rbat * 1.5;
333 if (Vocv < get_OCV_voltage(info,3))
335 info->soca->R_low = Calc_Linear_Interpolation(info->soca->Rbat,get_OCV_voltage(info,3),
336 Rbat_low_max, get_OCV_voltage(info,0), Vocv);
337 RICOH_FG_DBG("PMU: Modify RBAT from %d to %d ", info->soca->Rbat, info->soca->R_low);
338 temp = info->soca->R_low *4095/5000*512/1000;
340 val = info->soca->R_low>>8;
341 err = ricoh619_write_bank1(info->dev->parent, 0xD4, val);
343 dev_err(info->dev, "batterry initialize error\n");
347 val = info->soca->R_low & 0xff;
348 err = ricoh619_write_bank1(info->dev->parent, 0xD5, val);
350 dev_err(info->dev, "batterry initialize error\n");
354 else info->soca->R_low = 0;
360 static int Set_back_ocv_table(struct ricoh619_battery_info *info)
366 uint8_t debug_disp[22];
368 /* Modify back ocv table */
370 if (0 != info->soca->ocv_table_low[0])
372 for (i = 0 ; i < 11; i++){
373 battery_init_para[info->num][i*2 + 1] = info->soca->ocv_table_low[i];
374 battery_init_para[info->num][i*2] = info->soca->ocv_table_low[i] >> 8;
376 err = ricoh619_clr_bits(info->dev->parent, FG_CTRL_REG, 0x01);
378 err = ricoh619_bulk_writes_bank1(info->dev->parent,
379 BAT_INIT_TOP_REG, 22, battery_init_para[info->num]);
381 err = ricoh619_set_bits(info->dev->parent, FG_CTRL_REG, 0x01);
383 /* debug comment start*/
384 err = ricoh619_bulk_reads_bank1(info->dev->parent,
385 BAT_INIT_TOP_REG, 22, debug_disp);
386 for (i = 0; i < 11; i++){
387 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]));
391 for(i = 0; i < 11; i++)
393 info->soca->ocv_table_low[i] = 0;
397 /* Modify back Rbat */
398 if (0!=info->soca->R_low)
400 RICOH_FG_DBG("PMU: Modify back RBAT from %d to %d ", info->soca->R_low,info->soca->Rbat);
401 temp = info->soca->Rbat*4095/5000*512/1000;
403 val = info->soca->R_low>>8;
404 err = ricoh619_write_bank1(info->dev->parent, 0xD4, val);
406 dev_err(info->dev, "batterry initialize error\n");
410 val = info->soca->R_low & 0xff;
411 err = ricoh619_write_bank1(info->dev->parent, 0xD5, val);
413 dev_err(info->dev, "batterry initialize error\n");
417 info->soca->R_low = 0;
425 static int ricoh619_Check_OCV_Offset(struct ricoh619_battery_info *info)
427 int ocv_table[11]; // HEX value
431 uint8_t debug_disp[22];
434 RICOH_FG_DBG("PMU : %s : calc ocv %d get OCV %d\n",__func__,calc_ocv(info),get_OCV_voltage(info, RICOH619_OCV_OFFSET_BOUND));
436 /* check adp/usb status */
437 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &val);
439 dev_err(info->dev, "Error in reading the control register\n");
443 val = (val & 0xC0) >> 6;
445 if (val != 0){ /* connect adp or usb */
446 if (calc_ocv(info) < get_OCV_voltage(info, RICOH619_OCV_OFFSET_BOUND) )
448 if(0 == info->soca->ocv_table_low[0]){
449 for (i = 0 ; i < 11; i++){
450 ocv_table[i] = (battery_init_para[info->num][i*2]<<8) | (battery_init_para[info->num][i*2+1]);
451 RICOH_FG_DBG("PMU : %s : OCV table %d 0x%x\n",__func__,i * 10, ocv_table[i]);
452 info->soca->ocv_table_low[i] = ocv_table[i];
455 for (i = 0 ; i < 11; i++){
456 temp = ocv_table[i] * (100 + RICOH619_OCV_OFFSET_RATIO) / 100;
458 battery_init_para[info->num][i*2 + 1] = temp;
459 battery_init_para[info->num][i*2] = temp >> 8;
461 ret = ricoh619_clr_bits(info->dev->parent, FG_CTRL_REG, 0x01);
463 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
464 BAT_INIT_TOP_REG, 22, battery_init_para[info->num]);
466 ret = ricoh619_set_bits(info->dev->parent, FG_CTRL_REG, 0x01);
468 /* debug comment start*/
469 ret = ricoh619_bulk_reads_bank1(info->dev->parent,
470 BAT_INIT_TOP_REG, 22, debug_disp);
471 for (i = 0; i < 11; i++){
472 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]));
482 static int reset_FG_process(struct ricoh619_battery_info *info)
486 //err = set_Rlow(info);
487 //err = ricoh619_Check_OCV_Offset(info);
488 err = ricoh619_write(info->dev->parent,
490 info->soca->ready_fg = 0;
495 static int check_charge_status_2(struct ricoh619_battery_info *info, int displayed_soc_temp)
497 if (displayed_soc_temp < 0)
498 displayed_soc_temp = 0;
500 get_power_supply_status(info);
501 info->soca->soc = calc_capacity(info) * 100;
503 if (POWER_SUPPLY_STATUS_FULL == info->soca->chg_status) {
504 if ((info->first_pwon == 1)
505 && (RICOH619_SOCA_START == info->soca->status)) {
507 info->soca->soc_full = info->soca->soc;
508 info->soca->displayed_soc = 100*100;
509 info->soca->full_reset_count = 0;
511 if ( (displayed_soc_temp > 97*100)
512 && (calc_ocv(info) > (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*7/10) )){
514 info->soca->soc_full = info->soca->soc;
515 info->soca->displayed_soc = 100*100;
516 info->soca->full_reset_count = 0;
519 info->soca->displayed_soc = displayed_soc_temp;
524 if (info->soca->Ibat_ave >= 0) {
525 if (g_full_flag == 1) {
526 info->soca->displayed_soc = 100*100;
528 if (info->soca->displayed_soc/100 < 99) {
529 info->soca->displayed_soc = displayed_soc_temp;
531 info->soca->displayed_soc = 99 * 100;
535 if (info->soca->Ibat_ave < 0) {
536 if (g_full_flag == 1) {
537 if (calc_ocv(info) < (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*7/10) ) {
539 //info->soca->displayed_soc = 100*100;
540 info->soca->displayed_soc = displayed_soc_temp;
542 info->soca->displayed_soc = 100*100;
546 info->soca->displayed_soc = displayed_soc_temp;
550 return info->soca->displayed_soc;
554 * Calculate Capacity in a period
555 * - read CC_SUM & FA_CAP from Coulom Counter
556 * - and calculate Capacity.
557 * @cc_cap: capacity in a period, unit 0.01%
558 * @is_charging: Flag of charging current direction
559 * TRUE : charging (plus)
560 * FALSE: discharging (minus)
561 * @cc_rst: reset CC_SUM or not
565 static int calc_capacity_in_period(struct ricoh619_battery_info *info,
566 int *cc_cap, bool *is_charging, bool cc_rst)
569 uint8_t cc_sum_reg[4];
570 uint8_t cc_clr[4] = {0, 0, 0, 0};
571 uint8_t fa_cap_reg[2];
576 uint8_t charge_state;
578 uint32_t cc_cap_temp;
582 *is_charging = true; /* currrent state initialize -> charging */
584 if (info->entry_factory_mode)
587 //check need charge stop or not
588 /* get power supply status */
589 err = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
592 charge_state = (status & 0x1F);
593 Ocv = calc_ocv(info);
594 if (charge_state == CHG_STATE_CHG_COMPLETE) {
595 /* Check CHG status is complete or not */
597 } else if (calc_capacity(info) == 100) {
598 /* Check HW soc is 100 or not */
600 } else if (Ocv/1000 < get_OCV_voltage(info, 9)) {
601 /* Check VBAT is high level or not */
607 if (cc_stop_flag == 1)
609 /* Disable Charging/Completion Interrupt */
610 err = ricoh619_set_bits(info->dev->parent,
611 RICOH619_INT_MSK_CHGSTS1, 0x01);
615 /* disable charging */
616 err = ricoh619_clr_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x03);
622 err = ricoh619_write(info->dev->parent, CC_CTRL_REG, 0x01);
627 err = ricoh619_bulk_reads(info->dev->parent,
628 CC_SUMREG3_REG, 4, cc_sum_reg);
632 if (cc_rst == true) {
634 err = ricoh619_bulk_writes(info->dev->parent,
635 CC_SUMREG3_REG, 4, cc_clr);
641 err = ricoh619_write(info->dev->parent, CC_CTRL_REG, 0);
644 if (cc_stop_flag == 1)
647 /* Enable charging */
648 err = ricoh619_set_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x03);
654 /* Clear Charging Interrupt status */
655 err = ricoh619_clr_bits(info->dev->parent,
656 RICOH619_INT_IR_CHGSTS1, 0x01);
660 /* ricoh619_read(info->dev->parent, RICOH619_INT_IR_CHGSTS1, &val);
661 RICOH_FG_DBG("INT_IR_CHGSTS1 = 0x%x\n",val); */
663 /* Enable Charging Interrupt */
664 err = ricoh619_clr_bits(info->dev->parent,
665 RICOH619_INT_MSK_CHGSTS1, 0x01);
670 err = ricoh619_bulk_reads(info->dev->parent,
671 FA_CAP_H_REG, 2, fa_cap_reg);
675 /* fa_cap = *(uint16_t*)fa_cap_reg & 0x7fff; */
676 fa_cap = (fa_cap_reg[0] << 8 | fa_cap_reg[1]) & 0x7fff;
678 /* cc_sum = *(uint32_t*)cc_sum_reg; */
679 cc_sum = cc_sum_reg[0] << 24 | cc_sum_reg[1] << 16 |
680 cc_sum_reg[2] << 8 | cc_sum_reg[3];
682 /* calculation two's complement of CC_SUM */
683 if (cc_sum & 0x80000000) {
684 cc_sum = (cc_sum^0xffffffff)+0x01;
685 *is_charging = false; /* discharge */
687 /* (CC_SUM x 10000)/3600/FA_CAP */
688 *cc_cap = cc_sum*25/9/fa_cap; /* unit is 0.01% */
690 //////////////////////////////////////////////////////////////////
691 cc_cap_min = fa_cap*3600/100/100/100; /* Unit is 0.0001% */
692 cc_cap_temp = cc_sum / cc_cap_min;
694 cc_cap_res = cc_cap_temp % 100;
696 RICOH_FG_DBG("PMU: cc_sum = %d: cc_cap_res= %d: \n", cc_sum, cc_cap_res);
700 info->soca->cc_cap_offset += cc_cap_res;
701 if (info->soca->cc_cap_offset >= 100) {
703 info->soca->cc_cap_offset %= 100;
706 info->soca->cc_cap_offset -= cc_cap_res;
707 if (info->soca->cc_cap_offset <= -100) {
709 info->soca->cc_cap_offset %= 100;
712 RICOH_FG_DBG("PMU: cc_cap_offset= %d: \n", info->soca->cc_cap_offset);
714 //////////////////////////////////////////////////////////////////
717 dev_err(info->dev, "Error !!-----\n");
721 * Calculate target using capacity
723 static int get_target_use_cap(struct ricoh619_battery_info *info)
728 // int Target_Cutoff_Vol = 0;
744 /* get const value */
745 Ibat_min = -1 * info->soca->target_ibat;
746 if (info->soca->Ibat_ave > Ibat_min) /* I bat is minus */
750 Ibat_now = info->soca->Ibat_ave;
752 fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
754 use_cap = fa_cap - info->soca->re_cap_old;
756 /* get OCV table % */
757 for (i = 0; i <= 10; i = i+1) {
758 temp = (battery_init_para[info->num][i*2]<<8)
759 | (battery_init_para[info->num][i*2+1]);
760 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
761 temp = ((temp * 50000 * 10 / 4095) + 5) / 10;
763 RICOH_FG_DBG("PMU : %s : ocv_table %d is %d v\n",__func__, i, ocv_table[i]);
766 /* Find out Current OCV */
767 i = info->soca->soc/1000;
768 j = info->soca->soc - info->soca->soc/1000*1000;
769 Ocv_now_table = ocv_table[i]*100+(ocv_table[i+1]-ocv_table[i])*j/10;
771 Rsys_now = (info->soca->Vsys_ave - Ocv_now_table) / info->soca->Ibat_ave;
772 if (((abs(info->soca->soc - info->soca->displayed_soc)) > 10)
773 && (info->soca->Ibat_ave > -250)) {
775 Rsys_now = max(-info->soca->Rbat, Rsys_now);
777 Rsys_now = min(info->soca->Rbat, Rsys_now);
780 Ocv_ZeroPer_now = info->soca->target_vsys * 1000 - Ibat_now * Rsys_now;
782 RICOH_FG_DBG("PMU: ------- Ocv_now_table= %d: Rsys_now= %d =======\n",
783 Ocv_now_table, Rsys_now);
785 RICOH_FG_DBG("PMU: ------- Rsys= %d: cutoff_ocv= %d: Ocv_ZeroPer_now= %d =======\n",
786 info->soca->Rsys, info->soca->cutoff_ocv, Ocv_ZeroPer_now);
791 for (i = 1; i < 11; i++) {
792 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));
793 if (ocv_table[i] >= Ocv_ZeroPer_now / 100) {
795 start_per = Calc_Linear_Interpolation(
796 (i-1)*1000, ocv_table[i-1], i*1000,
797 ocv_table[i], (Ocv_ZeroPer_now / 100));
802 start_per = max(0, start_per);
804 FA_CAP_now = fa_cap * ((10000 - start_per) / 100 ) / 100;
806 RICOH_FG_DBG("PMU: -------Ocv_ZeroPer_now= %d: start_per= %d =======\n",
807 Ocv_ZeroPer_now, start_per);
810 RE_CAP_now = FA_CAP_now - use_cap;
812 if (RE_CAP_now < RE_CAP_GO_DOWN) {
813 info->soca->hurry_up_flg = 1;
814 } else if (info->soca->Vsys_ave < info->soca->target_vsys*1000) {
815 info->soca->hurry_up_flg = 1;
816 } else if (info->fg_poff_vbat != 0) {
817 if (info->soca->Vbat_ave < info->fg_poff_vbat*1000) {
818 info->soca->hurry_up_flg = 1;
820 info->soca->hurry_up_flg = 0;
823 info->soca->hurry_up_flg = 0;
826 /* get CC_OnePer_step */
827 if (info->soca->displayed_soc > 0) { /* avoid divide-by-0 */
828 CC_OnePer_step = RE_CAP_now / (info->soca->displayed_soc / 100 + 1);
832 /* get info->soca->target_use_cap */
833 info->soca->target_use_cap = use_cap + CC_OnePer_step;
835 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",
836 FA_CAP_now, RE_CAP_now, CC_OnePer_step, info->soca->target_use_cap, info->soca->hurry_up_flg);
840 #ifdef ENABLE_OCV_TABLE_CALIB
842 * Calibration OCV Table
843 * - Update the value of VBAT on 100% in OCV table
844 * if battery is Full charged.
845 * - int vbat_ocv <- unit is uV
847 static int calib_ocvTable(struct ricoh619_battery_info *info, int vbat_ocv)
856 RICOH_FG_DBG("PMU: %s\n", __func__);
858 if (info->soca->Ibat_ave > RICOH619_REL1_SEL_VALUE) {
859 RICOH_FG_DBG("PMU: %s IBAT > 64mA -- Not Calibration --\n", __func__);
863 if (vbat_ocv < info->soca->OCV100_max) {
864 if (vbat_ocv < info->soca->OCV100_min)
865 ocv100_new = info->soca->OCV100_min;
867 ocv100_new = vbat_ocv;
869 ocv100_new = info->soca->OCV100_max;
871 RICOH_FG_DBG("PMU : %s :max %d min %d current %d\n",__func__,info->soca->OCV100_max,info->soca->OCV100_min,vbat_ocv);
872 RICOH_FG_DBG("PMU : %s : New OCV 100% = 0x%x\n",__func__,ocv100_new);
875 ret = ricoh619_clr_bits(info->dev->parent, FG_CTRL_REG, 0x01);
877 dev_err("PMU: %s Error in FG_En OFF\n", __func__);
882 //cutoff_ocv = (battery_init_para[info->num][0]<<8) | (battery_init_para[info->num][1]);
883 cutoff_ocv = get_OCV_voltage(info, 0);
885 info->soca->ocv_table_def[10] = info->soca->OCV100_max;
887 ricoh619_scaling_OCV_table(info, cutoff_ocv/1000, ocv100_new/1000, &start_per, &end_per);
889 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
890 BAT_INIT_TOP_REG, 22, battery_init_para[info->num]);
892 dev_err(info->dev, "batterry initialize error\n");
896 for (i = 0; i <= 10; i = i+1) {
897 info->soca->ocv_table[i] = get_OCV_voltage(info, i);
898 RICOH_FG_DBG("PMU: %s : * %d0%% voltage = %d uV\n",
899 __func__, i, info->soca->ocv_table[i]);
902 /* FG_En on & Reset*/
903 ret = reset_FG_process(info);
905 dev_err("PMU: %s Error in FG_En On & Reset\n", __func__);
909 RICOH_FG_DBG("PMU: %s Exit \n", __func__);
917 static void ricoh619_displayed_work(struct work_struct *work)
925 int displayed_soc_temp;
928 bool is_charging = true;
929 int re_cap,fa_cap,use_cap;
930 bool is_jeita_updated;
939 struct ricoh619_battery_info *info = container_of(work,
940 struct ricoh619_battery_info, displayed_work.work);
942 if (info->entry_factory_mode) {
943 info->soca->status = RICOH619_SOCA_STABLE;
944 info->soca->displayed_soc = -EINVAL;
945 info->soca->ready_fg = 0;
949 mutex_lock(&info->lock);
951 is_jeita_updated = false;
953 if ((RICOH619_SOCA_START == info->soca->status)
954 || (RICOH619_SOCA_STABLE == info->soca->status)
955 || (RICOH619_SOCA_FULL == info->soca->status))
956 info->soca->ready_fg = 1;
958 /* judge Full state or Moni Vsys state */
959 if ((RICOH619_SOCA_DISP == info->soca->status)
960 || (RICOH619_SOCA_STABLE == info->soca->status))
963 temp_ocv = get_OCV_voltage(info, 10) -
964 (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))/2;
966 if(g_full_flag == 1){ /* for issue 1 solution start*/
967 info->soca->status = RICOH619_SOCA_FULL;
968 }else if ((POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
969 && (calc_ocv(info) > temp_ocv)) {
970 info->soca->status = RICOH619_SOCA_FULL;
972 } else if (info->soca->Ibat_ave >= -20) {
973 /* for issue1 solution end */
974 /* check Full state or not*/
975 if ((calc_ocv(info) > (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*7/10))
976 || (POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
977 || (info->soca->displayed_soc > 9850))
979 info->soca->status = RICOH619_SOCA_FULL;
981 } else if ((calc_ocv(info) > (get_OCV_voltage(info, 9)))
982 && (info->soca->Ibat_ave < 300))
984 info->soca->status = RICOH619_SOCA_FULL;
987 } else { /* dis-charging */
988 if (info->soca->displayed_soc/100 < RICOH619_ENTER_LOW_VOL) {
989 info->soca->target_use_cap = 0;
990 info->soca->status = RICOH619_SOCA_LOW_VOL;
995 if (RICOH619_SOCA_STABLE == info->soca->status) {
996 info->soca->soc = calc_capacity_2(info);
997 info->soca->soc_delta = info->soca->soc - info->soca->last_soc;
999 if (info->soca->soc_delta >= -100 && info->soca->soc_delta <= 100) {
1000 info->soca->displayed_soc = info->soca->soc;
1002 info->soca->status = RICOH619_SOCA_DISP;
1004 info->soca->last_soc = info->soca->soc;
1005 info->soca->soc_delta = 0;
1006 } else if (RICOH619_SOCA_FULL == info->soca->status) {
1007 err = check_jeita_status(info, &is_jeita_updated);
1009 dev_err(info->dev, "Error in updating JEITA %d\n", err);
1012 info->soca->soc = calc_capacity(info) * 100;
1013 info->soca->last_soc = calc_capacity_2(info); /* for DISP */
1015 if (info->soca->Ibat_ave >= -20) { /* charging */
1016 if (0 == info->soca->jt_limit) {
1017 if (g_full_flag == 1) {
1019 if (POWER_SUPPLY_STATUS_FULL == info->soca->chg_status) {
1020 if(info->soca->full_reset_count < RICOH619_UPDATE_COUNT_FULL_RESET) {
1021 info->soca->full_reset_count++;
1022 } else if (info->soca->full_reset_count < (RICOH619_UPDATE_COUNT_FULL_RESET + 1)) {
1023 err = reset_FG_process(info);
1025 dev_err(info->dev, "Error in writing the control register\n");
1026 info->soca->full_reset_count++;
1028 } else if(info->soca->full_reset_count < (RICOH619_UPDATE_COUNT_FULL_RESET + 2)) {
1029 info->soca->full_reset_count++;
1030 info->soca->fc_cap = 0;
1031 info->soca->soc_full = info->soca->soc;
1034 if(info->soca->fc_cap < -1 * 200) {
1036 info->soca->displayed_soc = 99 * 100;
1038 info->soca->full_reset_count = 0;
1042 info->soca->chg_cmp_times = 0;
1043 err = calc_capacity_in_period(info, &cc_cap, &is_charging, true);
1045 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1047 fc_delta = (is_charging == true) ? cc_cap : -cc_cap;
1049 info->soca->fc_cap = info->soca->fc_cap + fc_delta;
1051 if (g_full_flag == 1){
1052 info->soca->displayed_soc = 100*100;
1055 if (calc_ocv(info) < (get_OCV_voltage(info, 8))) { /* fail safe*/
1057 info->soca->status = RICOH619_SOCA_DISP;
1058 info->soca->soc_delta = 0;
1059 } else if ((POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
1060 && (info->soca->displayed_soc >= 9890)){
1061 if(info->soca->chg_cmp_times > RICOH619_FULL_WAIT_TIME) {
1062 info->soca->displayed_soc = 100*100;
1064 info->soca->full_reset_count = 0;
1065 info->soca->soc_full = info->soca->soc;
1066 info->soca->fc_cap = 0;
1067 #ifdef ENABLE_OCV_TABLE_CALIB
1068 err = calib_ocvTable(info,calc_ocv(info));
1070 dev_err(info->dev, "Calibration OCV Error !!\n");
1073 info->soca->chg_cmp_times++;
1076 fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
1079 if (info->soca->displayed_soc >= 9950) {
1080 if((info->soca->soc_full - info->soca->soc) < 200) {
1084 info->soca->chg_cmp_times = 0;
1086 err = calc_capacity_in_period(info, &cc_cap, &is_charging, true);
1088 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1089 info->soca->cc_delta
1090 = (is_charging == true) ? cc_cap : -cc_cap;
1092 if((POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
1093 // || (info->soca->Ibat_ave > 200))
1094 || (info->soca->Ibat_ave < info->ch_icchg*50 + 100) || (info->soca->displayed_soc<9700))
1096 info->soca->displayed_soc += 13 * 3000 / fa_cap;
1099 info->soca->displayed_soc
1100 = info->soca->displayed_soc + info->soca->cc_delta*8/10;
1103 info->soca->displayed_soc
1104 = min(10000, info->soca->displayed_soc);
1105 info->soca->displayed_soc = max(0, info->soca->displayed_soc);
1107 if (info->soca->displayed_soc >= 9890) {
1108 info->soca->displayed_soc = 99 * 100;
1113 info->soca->full_reset_count = 0;
1115 } else { /* discharging */
1116 if (info->soca->displayed_soc >= 9950) {
1117 if (info->soca->Ibat_ave <= -1 * RICOH619_REL1_SEL_VALUE) {
1118 if ((calc_ocv(info) < (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*3/10))
1119 || ((info->soca->soc_full - info->soca->soc) > 200)) {
1122 info->soca->full_reset_count = 0;
1123 info->soca->displayed_soc = 100 * 100;
1124 info->soca->status = RICOH619_SOCA_DISP;
1125 info->soca->last_soc = info->soca->soc;
1126 info->soca->soc_delta = 0;
1128 info->soca->displayed_soc = 100 * 100;
1130 } else { /* into relaxation state */
1131 ricoh619_read(info->dev->parent, CHGSTATE_REG, ®_val);
1132 if (reg_val & 0xc0) {
1133 info->soca->displayed_soc = 100 * 100;
1136 info->soca->full_reset_count = 0;
1137 info->soca->displayed_soc = 100 * 100;
1138 info->soca->status = RICOH619_SOCA_DISP;
1139 info->soca->last_soc = info->soca->soc;
1140 info->soca->soc_delta = 0;
1145 info->soca->status = RICOH619_SOCA_DISP;
1146 info->soca->soc_delta = 0;
1147 info->soca->full_reset_count = 0;
1148 info->soca->last_soc = info->soca->soc;
1151 } else if (RICOH619_SOCA_LOW_VOL == info->soca->status) {
1152 if(info->soca->Ibat_ave >= 0) {
1153 info->soca->soc = calc_capacity(info) * 100;
1154 info->soca->status = RICOH619_SOCA_DISP;
1155 info->soca->last_soc = info->soca->soc;
1156 info->soca->soc_delta = 0;
1158 re_cap = get_check_fuel_gauge_reg(info, RE_CAP_H_REG, RE_CAP_L_REG,
1160 fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
1162 use_cap = fa_cap - re_cap;
1164 if (info->soca->target_use_cap == 0) {
1165 info->soca->re_cap_old = re_cap;
1166 get_target_use_cap(info);
1169 if(use_cap >= info->soca->target_use_cap) {
1170 info->soca->displayed_soc = info->soca->displayed_soc - 100;
1171 info->soca->displayed_soc = max(0, info->soca->displayed_soc);
1172 info->soca->re_cap_old = re_cap;
1173 } else if (info->soca->hurry_up_flg == 1) {
1174 info->soca->displayed_soc = info->soca->displayed_soc - 100;
1175 info->soca->displayed_soc = max(0, info->soca->displayed_soc);
1176 info->soca->re_cap_old = re_cap;
1178 get_target_use_cap(info);
1179 info->soca->soc = calc_capacity(info) * 100;
1182 else if (RICOH619_SOCA_DISP == info->soca->status) {
1184 info->soca->soc = calc_capacity_2(info);
1186 soc_round = (info->soca->soc + 50) / 100;
1187 last_soc_round = (info->soca->last_soc + 50) / 100;
1188 last_disp_round = (info->soca->displayed_soc + 50) / 100;
1190 info->soca->soc_delta =
1191 info->soca->soc_delta + (info->soca->soc - info->soca->last_soc);
1193 info->soca->last_soc = info->soca->soc;
1195 if (last_disp_round == soc_round) {
1196 /* if SOC == DISPLAY move to stable */
1197 info->soca->displayed_soc = info->soca->soc ;
1198 info->soca->status = RICOH619_SOCA_STABLE;
1200 } else if (info->soca->Ibat_ave > 0) {
1201 if ((0 == info->soca->jt_limit) ||
1202 (POWER_SUPPLY_STATUS_FULL != info->soca->chg_status)) {
1204 if (last_disp_round < soc_round) {
1205 /* Case 1 : Charge, Display < SOC */
1206 if (info->soca->soc_delta >= 100) {
1207 info->soca->displayed_soc
1208 = last_disp_round * 100 + 50;
1209 info->soca->soc_delta -= 100;
1210 if (info->soca->soc_delta >= 100)
1213 info->soca->displayed_soc += 25;
1214 disp_dec = info->soca->displayed_soc % 100;
1215 if ((50 <= disp_dec) && (disp_dec <= 74))
1216 info->soca->soc_delta = 0;
1218 if ((info->soca->displayed_soc + 50)/100
1220 info->soca->displayed_soc
1223 = RICOH619_SOCA_STABLE;
1226 } else if (last_disp_round > soc_round) {
1227 /* Case 2 : Charge, Display > SOC */
1228 if (info->soca->soc_delta >= 300) {
1229 info->soca->displayed_soc += 100;
1230 info->soca->soc_delta -= 300;
1232 if ((info->soca->displayed_soc + 50)/100
1234 info->soca->displayed_soc
1237 = RICOH619_SOCA_STABLE;
1242 info->soca->soc_delta = 0;
1246 if (last_disp_round > soc_round) {
1247 /* Case 3 : Dis-Charge, Display > SOC */
1248 if (info->soca->soc_delta <= -100) {
1249 info->soca->displayed_soc
1250 = last_disp_round * 100 - 75;
1251 info->soca->soc_delta += 100;
1252 if (info->soca->soc_delta <= -100)
1255 info->soca->displayed_soc -= 25;
1256 disp_dec = info->soca->displayed_soc % 100;
1257 if ((25 <= disp_dec) && (disp_dec <= 49))
1258 info->soca->soc_delta = 0;
1260 if ((info->soca->displayed_soc + 50)/100
1262 info->soca->displayed_soc
1265 = RICOH619_SOCA_STABLE;
1268 } else if (last_disp_round < soc_round) {
1269 /* Case 4 : Dis-Charge, Display < SOC */
1270 if (info->soca->soc_delta <= -300) {
1271 info->soca->displayed_soc -= 100;
1272 info->soca->soc_delta += 300;
1274 if ((info->soca->displayed_soc + 50)/100
1276 info->soca->displayed_soc
1279 = RICOH619_SOCA_STABLE;
1284 } else if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
1286 temp_ocv = get_OCV_voltage(info, 10) -
1287 (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))/2;
1289 if(g_full_flag == 1){ /* for issue 1 solution start*/
1290 info->soca->status = RICOH619_SOCA_FULL;
1291 err = reset_FG_process(info);
1293 dev_err(info->dev, "Error in writing the control register\n");
1296 }else if ((POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
1297 && (calc_ocv(info) > temp_ocv)) {
1298 info->soca->status = RICOH619_SOCA_FULL;
1300 err = reset_FG_process(info);
1302 dev_err(info->dev, "Error in writing the control register\n");
1304 } else if (info->soca->Ibat_ave >= -20) {
1305 /* for issue1 solution end */
1306 /* check Full state or not*/
1307 if ((calc_ocv(info) > (get_OCV_voltage(info, 9) + (get_OCV_voltage(info, 10) - get_OCV_voltage(info, 9))*7/10))
1308 || (POWER_SUPPLY_STATUS_FULL == info->soca->chg_status)
1309 || (info->soca->displayed_soc > 9850))
1311 info->soca->status = RICOH619_SOCA_FULL;
1313 err = reset_FG_process(info);
1315 dev_err(info->dev, "Error in writing the control register\n");
1317 } else if ((calc_ocv(info) > (get_OCV_voltage(info, 9)))
1318 && (info->soca->Ibat_ave < 300))
1320 info->soca->status = RICOH619_SOCA_FULL;
1322 err = reset_FG_process(info);
1324 dev_err(info->dev, "Error in writing the control register\n");
1329 err = ricoh619_read(info->dev->parent, PSWR_REG, &val);
1331 info->soca->soc = val * 100;
1334 "Error in reading PSWR_REG %d\n", err);
1336 = calc_capacity(info) * 100;
1339 err = calc_capacity_in_period(info, &cc_cap,
1340 &is_charging, false);
1342 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1344 info->soca->cc_delta
1345 = (is_charging == true) ? cc_cap : -cc_cap;
1348 = info->soca->soc + info->soca->cc_delta;
1349 if (displayed_soc_temp < 0)
1350 displayed_soc_temp = 0;
1352 = min(9850, displayed_soc_temp);
1353 displayed_soc_temp = max(0, displayed_soc_temp);
1355 info->soca->displayed_soc = displayed_soc_temp;
1357 } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
1359 } else if (RICOH619_SOCA_START == info->soca->status) {
1361 err = measure_Ibatt_FG(info, &Ibat);
1362 err = measure_vbatt_FG(info, &Vbat);
1363 err = measure_vsys_ADC(info, &Vsys);
1365 info->soca->Ibat_ave = Ibat;
1366 info->soca->Vbat_ave = Vbat;
1367 info->soca->Vsys_ave = Vsys;
1369 err = check_jeita_status(info, &is_jeita_updated);
1370 is_jeita_updated = false;
1372 dev_err(info->dev, "Error in updating JEITA %d\n", err);
1374 err = ricoh619_read(info->dev->parent, PSWR_REG, &val);
1376 if (info->first_pwon) {
1377 info->soca->soc = calc_capacity(info) * 100;
1378 val = (info->soca->soc + 50)/100;
1380 err = ricoh619_write(info->dev->parent, PSWR_REG, val);
1382 dev_err(info->dev, "Error in writing PSWR_REG\n");
1385 if ((info->soca->soc == 0) && (calc_ocv(info)
1386 < get_OCV_voltage(info, 0))) {
1387 info->soca->displayed_soc = 0;
1388 info->soca->status = RICOH619_SOCA_ZERO;
1390 if (0 == info->soca->jt_limit) {
1391 check_charge_status_2(info, info->soca->soc);
1393 info->soca->displayed_soc = info->soca->soc;
1396 if (info->soca->displayed_soc < 300) {
1397 info->soca->target_use_cap = 0;
1398 info->soca->status = RICOH619_SOCA_LOW_VOL;
1400 if ((info->fg_poff_vbat != 0)
1401 && (Vbat < info->fg_poff_vbat * 1000) ){
1402 info->soca->target_use_cap = 0;
1403 info->soca->status = RICOH619_SOCA_LOW_VOL;
1405 info->soca->status = RICOH619_SOCA_UNSTABLE;
1409 info->soca->status = RICOH619_SOCA_UNSTABLE;
1412 } else if (g_fg_on_mode && (val == 0x7f)) {
1413 info->soca->soc = calc_capacity(info) * 100;
1414 if ((info->soca->soc == 0) && (calc_ocv(info)
1415 < get_OCV_voltage(info, 0))) {
1416 info->soca->displayed_soc = 0;
1417 info->soca->status = RICOH619_SOCA_ZERO;
1419 if (0 == info->soca->jt_limit) {
1420 check_charge_status_2(info, info->soca->soc);
1422 info->soca->displayed_soc = info->soca->soc;
1424 info->soca->last_soc = info->soca->soc;
1425 info->soca->status = RICOH619_SOCA_STABLE;
1428 info->soca->soc = val * 100;
1431 "Error in reading PSWR_REG %d\n", err);
1433 = calc_capacity(info) * 100;
1436 err = calc_capacity_in_period(info, &cc_cap,
1437 &is_charging, false);
1439 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1441 info->soca->cc_delta
1442 = (is_charging == true) ? cc_cap : -cc_cap;
1443 if (calc_ocv(info) < get_OCV_voltage(info, 0)) {
1444 info->soca->displayed_soc = 0;
1445 info->soca->status = RICOH619_SOCA_ZERO;
1448 = info->soca->soc + info->soca->cc_delta;
1449 if (displayed_soc_temp < 0)
1450 displayed_soc_temp = 0;
1452 = min(10000, displayed_soc_temp);
1453 displayed_soc_temp = max(0, displayed_soc_temp);
1454 if (0 == info->soca->jt_limit) {
1455 check_charge_status_2(info, displayed_soc_temp);
1457 info->soca->displayed_soc = displayed_soc_temp;
1459 info->soca->last_soc = calc_capacity(info) * 100;
1461 if (info->soca->displayed_soc < 300) {
1462 info->soca->target_use_cap = 0;
1463 info->soca->status = RICOH619_SOCA_LOW_VOL;
1465 if ((info->fg_poff_vbat != 0)
1466 && (Vbat < info->fg_poff_vbat * 1000)){
1467 info->soca->target_use_cap = 0;
1468 info->soca->status = RICOH619_SOCA_LOW_VOL;
1470 info->soca->status = RICOH619_SOCA_UNSTABLE;
1474 if(info->soca->displayed_soc >= 9850)
1476 info->soca->displayed_soc = 10000;
1477 info->chg_complete_tm_ov_flag = 1;
1479 info->soca->status = RICOH619_SOCA_UNSTABLE;
1483 } else if (RICOH619_SOCA_ZERO == info->soca->status) {
1484 if (calc_ocv(info) > get_OCV_voltage(info, 0)) {
1485 err = reset_FG_process(info);
1487 dev_err(info->dev, "Error in writing the control register\n");
1488 info->soca->last_soc = calc_capacity_2(info);
1489 info->soca->status = RICOH619_SOCA_STABLE;
1491 info->soca->displayed_soc = 0;
1494 /* keep DSOC = 1 when Vbat is over 3.4V*/
1495 if( info->fg_poff_vbat != 0) {
1496 if (info->soca->zero_flg == 1) {
1497 if(info->soca->Ibat_ave >= 0) {
1498 info->soca->zero_flg = 0;
1500 info->soca->displayed_soc = 0;
1501 } else if (info->soca->displayed_soc < 50) {
1502 if (info->soca->Vbat_ave < 2000*1000) { /* error value */
1503 info->soca->displayed_soc = 100;
1504 } else if (info->soca->Vbat_ave < info->fg_poff_vbat*1000) {
1505 info->soca->displayed_soc = 0;
1506 info->soca->zero_flg = 1;
1508 info->soca->displayed_soc = 100;
1514 && (info->soca->status == RICOH619_SOCA_STABLE)) {
1515 err = ricoh619_write(info->dev->parent, PSWR_REG, 0x7f);
1517 dev_err(info->dev, "Error in writing PSWR_REG\n");
1519 err = calc_capacity_in_period(info, &cc_cap,
1520 &is_charging, true);
1522 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1524 } else if (RICOH619_SOCA_UNSTABLE != info->soca->status) {
1525 if ((info->soca->displayed_soc + 50) / 100 <= 1) {
1528 val = (info->soca->displayed_soc + 50)/100;
1531 err = ricoh619_write(info->dev->parent, PSWR_REG, val);
1533 dev_err(info->dev, "Error in writing PSWR_REG\n");
1537 err = calc_capacity_in_period(info, &cc_cap,
1538 &is_charging, true);
1540 dev_err(info->dev, "Read cc_sum Error !!-----\n");
1543 RICOH_FG_DBG("PMU: ------- STATUS= %d: IBAT= %d: VSYS= %d: VBAT= %d: DSOC= %d: RSOC= %d: -------\n",
1544 info->soca->status, info->soca->Ibat_ave, info->soca->Vsys_ave, info->soca->Vbat_ave,
1545 info->soca->displayed_soc, info->soca->soc);
1547 #ifdef DISABLE_CHARGER_TIMER
1548 /* clear charger timer */
1549 if ( info->soca->chg_status == POWER_SUPPLY_STATUS_CHARGING ) {
1550 err = ricoh619_read(info->dev->parent, TIMSET_REG, &val);
1553 "Error in read TIMSET_REG%d\n", err);
1554 /* to check bit 0-1 */
1558 /* set rapid timer 240 -> 300 */
1559 err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x03);
1561 dev_err(info->dev, "Error in writing the control register\n");
1564 /* set rapid timer 300 -> 240 */
1565 err = ricoh619_clr_bits(info->dev->parent, TIMSET_REG, 0x01);
1566 err = ricoh619_set_bits(info->dev->parent, TIMSET_REG, 0x02);
1568 dev_err(info->dev, "Error in writing the control register\n");
1574 if (0 == info->soca->ready_fg)
1575 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1576 RICOH619_FG_RESET_TIME * HZ);
1577 else if (delay_flag == 1)
1578 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1579 RICOH619_DELAY_TIME * HZ);
1580 else if (RICOH619_SOCA_DISP == info->soca->status)
1581 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1582 RICOH619_DISPLAY_UPDATE_TIME * HZ);
1583 else if (info->soca->hurry_up_flg == 1)
1584 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1585 RICOH619_LOW_VOL_DOWN_TIME * HZ);
1587 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1588 RICOH619_DISPLAY_UPDATE_TIME * HZ);
1590 mutex_unlock(&info->lock);
1592 if((true == is_jeita_updated)
1593 || (info->soca->last_displayed_soc/100 != (info->soca->displayed_soc+50)/100))
1594 power_supply_changed(&info->battery);
1596 info->soca->last_displayed_soc = info->soca->displayed_soc+50;
1598 if ((info->soca->displayed_soc >= 9850) && (info->soca->Ibat_ave > -20) && (info->capacity < 100)
1599 && (info->soca->chg_status == POWER_SUPPLY_STATUS_CHARGING))
1601 if(info->chg_complete_rd_flag == 0)
1603 info->chg_complete_rd_flag = 1;
1604 info->chg_complete_rd_cnt = 0;
1605 queue_delayed_work(info->monitor_wqueue, &info->charge_complete_ready, 0);
1610 info->chg_complete_rd_flag = 0;
1613 if(info->chg_complete_tm_ov_flag == 1)
1615 if(info->soca->displayed_soc < 9850 || info->soca->Ibat_ave < -20)
1617 info->chg_complete_tm_ov_flag = 0;
1618 power_supply_changed(&info->battery);
1624 static void ricoh619_stable_charge_countdown_work(struct work_struct *work)
1630 struct ricoh619_battery_info *info = container_of(work,
1631 struct ricoh619_battery_info, charge_stable_work.work);
1633 if (info->entry_factory_mode)
1636 mutex_lock(&info->lock);
1637 if (RICOH619_SOCA_FG_RESET == info->soca->status)
1638 info->soca->ready_fg = 1;
1640 if (2 <= info->soca->stable_count) {
1641 if (3 == info->soca->stable_count
1642 && RICOH619_SOCA_FG_RESET == info->soca->status) {
1643 ret = reset_FG_process(info);
1645 dev_err(info->dev, "Error in writing the control register\n");
1647 info->soca->stable_count = info->soca->stable_count - 1;
1648 queue_delayed_work(info->monitor_wqueue,
1649 &info->charge_stable_work,
1650 RICOH619_FG_STABLE_TIME * HZ / 10);
1651 } else if (0 >= info->soca->stable_count) {
1652 /* Finished queue, ignore */
1653 } else if (1 == info->soca->stable_count) {
1654 if (RICOH619_SOCA_UNSTABLE == info->soca->status) {
1655 /* Judge if FG need reset or Not */
1656 info->soca->soc = calc_capacity(info) * 100;
1657 if (info->chg_ctr != 0) {
1658 queue_delayed_work(info->monitor_wqueue,
1659 &info->charge_stable_work,
1660 RICOH619_FG_STABLE_TIME * HZ / 10);
1661 mutex_unlock(&info->lock);
1664 /* Do reset setting */
1665 ret = reset_FG_process(info);
1667 dev_err(info->dev, "Error in writing the control register\n");
1669 info->soca->status = RICOH619_SOCA_FG_RESET;
1671 /* Delay for addition Reset Time (6s) */
1672 queue_delayed_work(info->monitor_wqueue,
1673 &info->charge_stable_work,
1674 RICOH619_FG_RESET_TIME*HZ);
1675 } else if (RICOH619_SOCA_FG_RESET == info->soca->status) {
1676 info->soca->reset_soc[2] = info->soca->reset_soc[1];
1677 info->soca->reset_soc[1] = info->soca->reset_soc[0];
1678 info->soca->reset_soc[0] = calc_capacity(info) * 100;
1679 info->soca->reset_count++;
1681 if (info->soca->reset_count > 10) {
1682 /* Reset finished; */
1683 info->soca->soc = info->soca->reset_soc[0];
1684 info->soca->stable_count = 0;
1688 for (i = 0; i < 3; i++) {
1689 if (max < info->soca->reset_soc[i]/100)
1690 max = info->soca->reset_soc[i]/100;
1691 if (min > info->soca->reset_soc[i]/100)
1692 min = info->soca->reset_soc[i]/100;
1695 if ((info->soca->reset_count > 3) && ((max - min)
1696 < RICOH619_MAX_RESET_SOC_DIFF)) {
1697 /* Reset finished; */
1698 info->soca->soc = info->soca->reset_soc[0];
1699 info->soca->stable_count = 0;
1702 /* Do reset setting */
1703 ret = reset_FG_process(info);
1705 dev_err(info->dev, "Error in writing the control register\n");
1707 /* Delay for addition Reset Time (6s) */
1708 queue_delayed_work(info->monitor_wqueue,
1709 &info->charge_stable_work,
1710 RICOH619_FG_RESET_TIME*HZ);
1712 /* Finished queue From now, select FG as result; */
1713 } else if (RICOH619_SOCA_START == info->soca->status) {
1714 /* Normal condition */
1715 } else { /* other state ZERO/DISP/STABLE */
1716 info->soca->stable_count = 0;
1719 mutex_unlock(&info->lock);
1723 info->soca->last_soc = info->soca->soc;
1724 info->soca->status = RICOH619_SOCA_DISP;
1725 info->soca->soc_delta = 0;
1728 mutex_unlock(&info->lock);
1732 static void ricoh619_charge_monitor_work(struct work_struct *work)
1734 struct ricoh619_battery_info *info = container_of(work,
1735 struct ricoh619_battery_info, charge_monitor_work.work);
1737 get_power_supply_status(info);
1739 if (POWER_SUPPLY_STATUS_DISCHARGING == info->soca->chg_status
1740 || POWER_SUPPLY_STATUS_NOT_CHARGING == info->soca->chg_status) {
1741 switch (info->soca->dischg_state) {
1743 info->soca->dischg_state = 1;
1746 info->soca->dischg_state = 2;
1754 info->soca->dischg_state = 0;
1757 queue_delayed_work(info->monitor_wqueue, &info->charge_monitor_work,
1758 RICOH619_CHARGE_MONITOR_TIME * HZ);
1763 static void ricoh619_get_charge_work(struct work_struct *work)
1765 struct ricoh619_battery_info *info = container_of(work,
1766 struct ricoh619_battery_info, get_charge_work.work);
1768 int Vbat_temp, Vsys_temp, Ibat_temp;
1769 int Vbat_sort[RICOH619_GET_CHARGE_NUM];
1770 int Vsys_sort[RICOH619_GET_CHARGE_NUM];
1771 int Ibat_sort[RICOH619_GET_CHARGE_NUM];
1775 mutex_lock(&info->lock);
1777 for (i = RICOH619_GET_CHARGE_NUM-1; i > 0; i--) {
1778 if (0 == info->soca->chg_count) {
1779 info->soca->Vbat[i] = 0;
1780 info->soca->Vsys[i] = 0;
1781 info->soca->Ibat[i] = 0;
1783 info->soca->Vbat[i] = info->soca->Vbat[i-1];
1784 info->soca->Vsys[i] = info->soca->Vsys[i-1];
1785 info->soca->Ibat[i] = info->soca->Ibat[i-1];
1789 ret = measure_vbatt_FG(info, &info->soca->Vbat[0]);
1790 ret = measure_vsys_ADC(info, &info->soca->Vsys[0]);
1791 ret = measure_Ibatt_FG(info, &info->soca->Ibat[0]);
1793 info->soca->chg_count++;
1795 if (RICOH619_GET_CHARGE_NUM != info->soca->chg_count) {
1796 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1797 RICOH619_CHARGE_CALC_TIME * HZ);
1798 mutex_unlock(&info->lock);
1802 for (i = 0; i < RICOH619_GET_CHARGE_NUM; i++) {
1803 Vbat_sort[i] = info->soca->Vbat[i];
1804 Vsys_sort[i] = info->soca->Vsys[i];
1805 Ibat_sort[i] = info->soca->Ibat[i];
1811 for (i = 0; i < RICOH619_GET_CHARGE_NUM - 1; i++) {
1812 for (j = RICOH619_GET_CHARGE_NUM - 1; j > i; j--) {
1813 if (Vbat_sort[j - 1] > Vbat_sort[j]) {
1814 Vbat_temp = Vbat_sort[j];
1815 Vbat_sort[j] = Vbat_sort[j - 1];
1816 Vbat_sort[j - 1] = Vbat_temp;
1818 if (Vsys_sort[j - 1] > Vsys_sort[j]) {
1819 Vsys_temp = Vsys_sort[j];
1820 Vsys_sort[j] = Vsys_sort[j - 1];
1821 Vsys_sort[j - 1] = Vsys_temp;
1823 if (Ibat_sort[j - 1] > Ibat_sort[j]) {
1824 Ibat_temp = Ibat_sort[j];
1825 Ibat_sort[j] = Ibat_sort[j - 1];
1826 Ibat_sort[j - 1] = Ibat_temp;
1834 for (i = 3; i < RICOH619_GET_CHARGE_NUM-3; i++) {
1835 Vbat_temp = Vbat_temp + Vbat_sort[i];
1836 Vsys_temp = Vsys_temp + Vsys_sort[i];
1837 Ibat_temp = Ibat_temp + Ibat_sort[i];
1839 Vbat_temp = Vbat_temp / (RICOH619_GET_CHARGE_NUM - 6);
1840 Vsys_temp = Vsys_temp / (RICOH619_GET_CHARGE_NUM - 6);
1841 Ibat_temp = Ibat_temp / (RICOH619_GET_CHARGE_NUM - 6);
1843 if (0 == info->soca->chg_count) {
1844 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1845 RICOH619_CHARGE_UPDATE_TIME * HZ);
1846 mutex_unlock(&info->lock);
1849 info->soca->Vbat_ave = Vbat_temp;
1850 info->soca->Vsys_ave = Vsys_temp;
1851 info->soca->Ibat_ave = Ibat_temp;
1854 info->soca->chg_count = 0;
1855 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1856 RICOH619_CHARGE_UPDATE_TIME * HZ);
1857 mutex_unlock(&info->lock);
1861 /* Initial setting of FuelGauge SOCA function */
1862 static int ricoh619_init_fgsoca(struct ricoh619_battery_info *info)
1868 bool is_charging = true;
1870 for (i = 0; i <= 10; i = i+1) {
1871 info->soca->ocv_table[i] = get_OCV_voltage(info, i);
1872 RICOH_FG_DBG("PMU: %s : * %d0%% voltage = %d uV\n",
1873 __func__, i, info->soca->ocv_table[i]);
1876 for (i = 0; i < 3; i = i+1)
1877 info->soca->reset_soc[i] = 0;
1878 info->soca->reset_count = 0;
1880 if (info->first_pwon) {
1882 err = ricoh619_read(info->dev->parent, CHGISET_REG, &val);
1885 "Error in read CHGISET_REG%d\n", err);
1887 err = ricoh619_write(info->dev->parent, CHGISET_REG, 0);
1890 "Error in writing CHGISET_REG%d\n", err);
1893 if (!info->entry_factory_mode) {
1894 err = ricoh619_write(info->dev->parent,
1897 dev_err(info->dev, "Error in writing the control register\n");
1900 err = calc_capacity_in_period(info, &cc_cap, &is_charging, true);
1904 err = ricoh619_write(info->dev->parent, CHGISET_REG, val);
1907 "Error in writing CHGISET_REG%d\n", err);
1910 /* Rbat : Transfer */
1911 info->soca->Rbat = get_OCV_init_Data(info, 12) * 1000 / 512
1913 info->soca->n_cap = get_OCV_init_Data(info, 11);
1916 info->soca->displayed_soc = 0;
1917 info->soca->last_displayed_soc = 0;
1918 info->soca->suspend_soc = 0;
1919 info->soca->ready_fg = 0;
1920 info->soca->soc_delta = 0;
1921 info->soca->full_reset_count = 0;
1922 info->soca->soc_full = 0;
1923 info->soca->fc_cap = 0;
1924 info->soca->status = RICOH619_SOCA_START;
1925 /* stable count down 11->2, 1: reset; 0: Finished; */
1926 info->soca->stable_count = 11;
1927 info->soca->chg_cmp_times = 0;
1928 info->soca->dischg_state = 0;
1929 info->soca->Vbat_ave = 0;
1930 info->soca->Vbat_old = 0;
1931 info->soca->Vsys_ave = 0;
1932 info->soca->Ibat_ave = 0;
1933 info->soca->chg_count = 0;
1934 info->soca->target_use_cap = 0;
1935 info->soca->hurry_up_flg = 0;
1936 info->soca->re_cap_old = 0;
1937 info->soca->jt_limit = 0;
1938 info->soca->zero_flg = 0;
1939 info->soca->cc_cap_offset = 0;
1941 for (i = 0; i < 11; i++) {
1942 info->soca->ocv_table_low[i] = 0;
1945 for (i = 0; i < RICOH619_GET_CHARGE_NUM; i++) {
1946 info->soca->Vbat[i] = 0;
1947 info->soca->Vsys[i] = 0;
1948 info->soca->Ibat[i] = 0;
1953 #ifdef ENABLE_FG_KEEP_ON_MODE
1959 /* Start first Display job */
1960 queue_delayed_work(info->monitor_wqueue, &info->displayed_work,
1961 RICOH619_FG_RESET_TIME*HZ);
1963 /* Start first Waiting stable job */
1964 queue_delayed_work(info->monitor_wqueue, &info->charge_stable_work,
1965 RICOH619_FG_STABLE_TIME*HZ/10);
1967 queue_delayed_work(info->monitor_wqueue, &info->charge_monitor_work,
1968 RICOH619_CHARGE_MONITOR_TIME * HZ);
1970 queue_delayed_work(info->monitor_wqueue, &info->get_charge_work,
1971 RICOH619_CHARGE_MONITOR_TIME * HZ);
1973 if (info->jt_hw_sw) {
1974 /* Enable JEITA function supported by H/W */
1975 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x04);
1977 dev_err(info->dev, "Error in writing the control register\n");
1979 /* Disable JEITA function supported by H/W */
1980 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x04);
1982 dev_err(info->dev, "Error in writing the control register\n");
1983 queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
1984 RICOH619_FG_RESET_TIME * HZ);
1987 /* Disable JEITA function supported by H/W */
1988 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x04);
1990 dev_err(info->dev, "Error in writing the control register\n");
1993 RICOH_FG_DBG("PMU: %s : * Rbat = %d mOhm n_cap = %d mAH\n",
1994 __func__, info->soca->Rbat, info->soca->n_cap);
1999 static void ricoh619_charging_complete_work(struct work_struct *work)
2001 struct ricoh619_battery_info *info = container_of(work,
2002 struct ricoh619_battery_info, charge_complete_ready.work);
2004 uint8_t time_ov_flag;
2005 RICOH_FG_DBG("PMU: %s\n", __func__);
2006 RICOH_FG_DBG("info->chg_complete_rd_cnt = %d\n", info->chg_complete_rd_cnt);
2007 RICOH_FG_DBG("info->chg_complete_rd_flag = %d\n", info->chg_complete_rd_flag);
2008 RICOH_FG_DBG("info->chg_complete_tm_ov_flag = %d\n", info->chg_complete_tm_ov_flag);
2010 if(info->chg_complete_rd_flag == 1)
2012 // start chg 99per to 100per timer
2014 info->chg_complete_rd_flag = 2;
2015 info->chg_complete_tm_ov_flag = 0;
2019 if(info->capacity == 100)
2021 // battery arriver to 100% earlier than time ov
2023 info->chg_complete_rd_cnt = 0;
2024 info->chg_complete_tm_ov_flag = 1;
2026 else if(info->chg_complete_rd_cnt > RICOH619_TIME_CHG_COUNT)
2028 // chg timer ov before cap arrive to 100%
2030 info->chg_complete_tm_ov_flag = 1;
2031 info->chg_complete_rd_cnt = 0;
2032 info->soca->status = RICOH619_SOCA_FULL;
2033 power_supply_changed(&info->battery);
2038 info->chg_complete_tm_ov_flag = 0;
2042 if ((time_ov_flag == 0) && (info->soca->chg_status == POWER_SUPPLY_STATUS_CHARGING))
2044 info->chg_complete_rd_cnt++;
2045 queue_delayed_work(info->monitor_wqueue, &info->charge_complete_ready,
2046 RICOH619_TIME_CHG_STEP);
2050 info->chg_complete_rd_flag = 0;
2053 RICOH_FG_DBG("PMU2: %s return\n", __func__);
2054 RICOH_FG_DBG("info->chg_complete_rd_cnt = %d\n", info->chg_complete_rd_cnt);
2055 RICOH_FG_DBG("info->chg_complete_rd_flag = %d\n", info->chg_complete_rd_flag);
2056 RICOH_FG_DBG("info->chg_complete_tm_ov_flag = %d\n", info->chg_complete_tm_ov_flag);
2057 RICOH_FG_DBG("time_ov_flag = %d\n", time_ov_flag);
2060 static void ricoh619_changed_work(struct work_struct *work)
2062 struct ricoh619_battery_info *info = container_of(work,
2063 struct ricoh619_battery_info, changed_work.work);
2065 RICOH_FG_DBG("PMU: %s\n", __func__);
2066 power_supply_changed(&info->battery);
2071 static int check_jeita_status(struct ricoh619_battery_info *info, bool *is_jeita_updated)
2072 /* JEITA Parameter settings
2076 * jt_vfchg_h~+~~~~~~~~~~~~~~~~~~~+
2078 * jt_vfchg_l-| - - - - - - - - - +~~~~~~~~~~+
2080 * -------0--+-------------------+----------+--- Temp
2084 * jt_ichg_h-+ - -+~~~~~~~~~~~~~~+~~~~~~~~~~+
2086 * jt_ichg_l-+~~~~+ Charge area |
2088 * 0--+----+--------------+----------+--- Temp
2089 * 0 jt_temp_l jt_temp_h 55
2095 uint8_t chgiset_org;
2096 uint8_t batset2_org;
2097 uint8_t set_vchg_h, set_vchg_l;
2098 uint8_t set_ichg_h, set_ichg_l;
2100 *is_jeita_updated = false;
2102 /* No execute if JEITA disabled */
2103 if (!info->jt_en || info->jt_hw_sw)
2106 /* Check FG Reset */
2107 if (info->soca->ready_fg) {
2108 temp = get_battery_temp_2(info) / 10;
2110 RICOH_FG_DBG(KERN_INFO "JEITA: %s *** cannot update by resetting FG ******\n", __func__);
2115 err = ricoh619_read(info->dev->parent, BATSET2_REG, &batset2_org);
2117 dev_err(info->dev, "Error in readng the battery setting register\n");
2120 vfchg = (batset2_org & 0x70) >> 4;
2121 batset2_org &= 0x8F;
2124 err = ricoh619_read(info->dev->parent, CHGISET_REG, &chgiset_org);
2126 dev_err(info->dev, "Error in readng the chrage setting register\n");
2129 chgiset_org &= 0xC0;
2131 set_ichg_h = (uint8_t)(chgiset_org | info->jt_ichg_h);
2132 set_ichg_l = (uint8_t)(chgiset_org | info->jt_ichg_l);
2134 set_vchg_h = (uint8_t)((info->jt_vfchg_h << 4) | batset2_org);
2135 set_vchg_l = (uint8_t)((info->jt_vfchg_l << 4) | batset2_org);
2137 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Temperature: %d, vfchg: %d, SW status: %d, chg_status: %d ******\n",
2138 __func__, temp, vfchg, info->soca->status, info->soca->chg_status);
2140 if (temp <= 0 || 55 <= temp) {
2141 /* 1st and 5th temperature ranges (~0, 55~) */
2142 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Temp(%d) is out of 0-55 ******\n", __func__, temp);
2143 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2145 dev_err(info->dev, "Error in writing the control register\n");
2148 info->soca->jt_limit = 0;
2149 *is_jeita_updated = true;
2150 } else if (temp < info->jt_temp_l) {
2151 /* 2nd temperature range (0~12) */
2152 if (vfchg != info->jt_vfchg_h) {
2153 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 0<Temp<12, update to vfchg=%d ******\n",
2154 __func__, info->jt_vfchg_h);
2155 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2157 dev_err(info->dev, "Error in writing the control register\n");
2161 /* set VFCHG/VRCHG */
2162 err = ricoh619_write(info->dev->parent,
2163 BATSET2_REG, set_vchg_h);
2165 dev_err(info->dev, "Error in writing the battery setting register\n");
2168 info->soca->jt_limit = 0;
2169 *is_jeita_updated = true;
2171 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 0<Temp<50, already set vfchg=%d, so no need to update ******\n",
2172 __func__, info->jt_vfchg_h);
2175 err = ricoh619_write(info->dev->parent, CHGISET_REG, set_ichg_l);
2177 dev_err(info->dev, "Error in writing the battery setting register\n");
2180 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2182 dev_err(info->dev, "Error in writing the control register\n");
2185 } else if (temp < info->jt_temp_h) {
2186 /* 3rd temperature range (12~50) */
2187 if (vfchg != info->jt_vfchg_h) {
2188 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 12<Temp<50, update to vfchg==%d ******\n", __func__, info->jt_vfchg_h);
2190 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2192 dev_err(info->dev, "Error in writing the control register\n");
2195 /* set VFCHG/VRCHG */
2196 err = ricoh619_write(info->dev->parent,
2197 BATSET2_REG, set_vchg_h);
2199 dev_err(info->dev, "Error in writing the battery setting register\n");
2202 info->soca->jt_limit = 0;
2203 *is_jeita_updated = true;
2205 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 12<Temp<50, already set vfchg==%d, so no need to update ******\n",
2206 __func__, info->jt_vfchg_h);
2209 err = ricoh619_write(info->dev->parent, CHGISET_REG, set_ichg_h);
2211 dev_err(info->dev, "Error in writing the battery setting register\n");
2214 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2216 dev_err(info->dev, "Error in writing the control register\n");
2219 } else if (temp < 55) {
2220 /* 4th temperature range (50~55) */
2221 if (vfchg != info->jt_vfchg_l) {
2222 RICOH_FG_DBG(KERN_INFO "PMU: %s *** 50<Temp<55, update to vfchg==%d ******\n", __func__, info->jt_vfchg_l);
2224 err = ricoh619_clr_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2226 dev_err(info->dev, "Error in writing the control register\n");
2229 /* set VFCHG/VRCHG */
2230 err = ricoh619_write(info->dev->parent,
2231 BATSET2_REG, set_vchg_l);
2233 dev_err(info->dev, "Error in writing the battery setting register\n");
2236 info->soca->jt_limit = 1;
2237 *is_jeita_updated = true;
2239 RICOH_FG_DBG(KERN_INFO "JEITA: %s *** 50<Temp<55, already set vfchg==%d, so no need to update ******\n",
2240 __func__, info->jt_vfchg_l);
2243 err = ricoh619_write(info->dev->parent, CHGISET_REG, set_ichg_h);
2245 dev_err(info->dev, "Error in writing the battery setting register\n");
2248 err = ricoh619_set_bits(info->dev->parent, CHGCTL1_REG, 0x03);
2250 dev_err(info->dev, "Error in writing the control register\n");
2255 get_power_supply_status(info);
2256 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Hope updating value in this timing after checking jeita, chg_status: %d, is_jeita_updated: %d ******\n",
2257 __func__, info->soca->chg_status, *is_jeita_updated);
2262 RICOH_FG_DBG(KERN_INFO "PMU: %s ERROR ******\n", __func__);
2266 static void ricoh619_jeita_work(struct work_struct *work)
2269 bool is_jeita_updated = false;
2270 struct ricoh619_battery_info *info = container_of(work,
2271 struct ricoh619_battery_info, jeita_work.work);
2273 mutex_lock(&info->lock);
2275 ret = check_jeita_status(info, &is_jeita_updated);
2277 queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
2278 RICOH619_JEITA_UPDATE_TIME * HZ);
2280 RICOH_FG_DBG(KERN_INFO "PMU: %s *** Call check_jeita_status() in jeita_work, err:%d ******\n",
2282 queue_delayed_work(info->monitor_wqueue, &info->jeita_work,
2283 RICOH619_FG_RESET_TIME * HZ);
2286 mutex_unlock(&info->lock);
2288 if(true == is_jeita_updated)
2289 power_supply_changed(&info->battery);
2294 #ifdef ENABLE_FACTORY_MODE
2295 /*------------------------------------------------------*/
2297 /* Check Battery exist or not */
2298 /* If not, disabled Rapid to Complete State change */
2299 /*------------------------------------------------------*/
2300 static int ricoh619_factory_mode(struct ricoh619_battery_info *info)
2305 ret = ricoh619_read(info->dev->parent, RICOH619_INT_MON_CHGCTR, &val);
2307 dev_err(info->dev, "Error in reading the control register\n");
2310 if (!(val & 0x01)) /* No Adapter connected */
2313 /* Rapid to Complete State change disable */
2314 ret = ricoh619_set_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x40);
2316 dev_err(info->dev, "Error in writing the control register\n");
2320 /* Wait 1s for checking Charging State */
2321 queue_delayed_work(info->factory_mode_wqueue, &info->factory_mode_work,
2327 static void check_charging_state_work(struct work_struct *work)
2329 struct ricoh619_battery_info *info = container_of(work,
2330 struct ricoh619_battery_info, factory_mode_work.work);
2334 int chargeCurrent = 0;
2336 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &val);
2338 dev_err(info->dev, "Error in reading the control register\n");
2343 chargeCurrent = get_check_fuel_gauge_reg(info, CC_AVERAGE1_REG,
2344 CC_AVERAGE0_REG, 0x3fff);
2345 if (chargeCurrent < 0) {
2346 dev_err(info->dev, "Error in reading the FG register\n");
2350 /* Repid State && Charge Current about 0mA */
2351 if (((chargeCurrent >= 0x3ffc && chargeCurrent <= 0x3fff)
2352 || chargeCurrent < 0x05) && val == 0x43) {
2353 RICOH_FG_DBG("PMU:%s --- No battery !! Enter Factory mode ---\n"
2355 info->entry_factory_mode = true;
2356 /* clear FG_ACC bit */
2357 ret = ricoh619_clr_bits(info->dev->parent, RICOH619_FG_CTRL, 0x10);
2359 dev_err(info->dev, "Error in writing FG_CTRL\n");
2361 return; /* Factory Mode */
2364 /* Return Normal Mode --> Rapid to Complete State change enable */
2365 ret = ricoh619_clr_bits(info->dev->parent, RICOH619_CHG_CTL1, 0x40);
2367 dev_err(info->dev, "Error in writing the control register\n");
2370 RICOH_FG_DBG("PMU:%s --- Battery exist !! Return Normal mode ---0x%2x\n"
2375 #endif /* ENABLE_FACTORY_MODE */
2377 static int Calc_Linear_Interpolation(int x0, int y0, int x1, int y1, int y)
2382 alpha = (y - y0)*100 / (y1 - y0);
2384 x = ((100 - alpha) * x0 + alpha * x1) / 100;
2389 static void ricoh619_scaling_OCV_table(struct ricoh619_battery_info *info, int cutoff_vol, int full_vol, int *start_per, int *end_per)
2394 int OCV_percent_new[11];
2396 /* get ocv table. this table is calculated by Apprication */
2397 //RICOH_FG_DBG("PMU : %s : original table\n",__func__);
2398 for (i = 0; i <= 10; i = i+1) {
2399 RICOH_FG_DBG(KERN_INFO "PMU: %s : %d0%% voltage = %d uV\n",
2400 __func__, i, info->soca->ocv_table_def[i]);
2402 //RICOH_FG_DBG("PMU: %s : cutoff_vol %d full_vol %d\n",
2403 // __func__, cutoff_vol,full_vol);
2406 if (info->soca->ocv_table_def[0] > cutoff_vol * 1000) {
2408 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]);
2410 for (i = 1; i < 11; i++) {
2411 if (info->soca->ocv_table_def[i] >= cutoff_vol * 1000) {
2412 /* unit is 0.001% */
2413 *start_per = Calc_Linear_Interpolation(
2414 (i-1)*1000, info->soca->ocv_table_def[i-1], i*1000,
2415 info->soca->ocv_table_def[i], (cutoff_vol * 1000));
2422 for (i = 1; i < 11; i++) {
2423 if (info->soca->ocv_table_def[i] >= full_vol * 1000) {
2424 /* unit is 0.001% */
2425 *end_per = Calc_Linear_Interpolation(
2426 (i-1)*1000, info->soca->ocv_table_def[i-1], i*1000,
2427 info->soca->ocv_table_def[i], (full_vol * 1000));
2432 /* calc new ocv percent */
2433 percent_step = ( *end_per - *start_per) / 10;
2434 //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);
2436 for (i = 0; i < 11; i++) {
2438 = *start_per + percent_step*(i - 0);
2441 /* calc new ocv voltage */
2442 for (i = 0; i < 11; i++) {
2443 for (j = 1; j < 11; j++) {
2444 if (1000*j >= OCV_percent_new[i]) {
2445 temp = Calc_Linear_Interpolation(
2446 info->soca->ocv_table_def[j-1], (j-1)*1000,
2447 info->soca->ocv_table_def[j] , j*1000,
2448 OCV_percent_new[i]);
2450 temp = ( (temp/1000) * 4095 ) / 5000;
2452 battery_init_para[info->num][i*2 + 1] = temp;
2453 battery_init_para[info->num][i*2] = temp >> 8;
2459 RICOH_FG_DBG("PMU : %s : new table\n",__func__);
2460 for (i = 0; i <= 10; i = i+1) {
2461 temp = (battery_init_para[info->num][i*2]<<8)
2462 | (battery_init_para[info->num][i*2+1]);
2463 /* conversion unit 1 Unit is 1.22mv (5000/4095 mv) */
2464 temp = ((temp * 50000 * 10 / 4095) + 5) / 10;
2465 RICOH_FG_DBG("PMU : %s : ocv_table %d is %d v\n",__func__, i, temp);
2470 static int ricoh619_set_OCV_table(struct ricoh619_battery_info *info)
2476 int available_cap_ori;
2489 for (i = 0; i <= 10; i = i+1) {
2490 info->soca->ocv_table_def[i] = get_OCV_voltage(info, i);
2491 RICOH_FG_DBG(KERN_INFO "PMU: %s : %d0%% voltage = %d uV\n",
2492 __func__, i, info->soca->ocv_table_def[i]);
2495 temp = (battery_init_para[info->num][24]<<8) | (battery_init_para[info->num][25]);
2496 Rbat = temp * 1000 / 512 * 5000 / 4095;
2497 info->soca->Rsys = Rbat + 55;
2499 if ((info->fg_target_ibat == 0) || (info->fg_target_vsys == 0)) { /* normal version */
2501 temp = (battery_init_para[info->num][22]<<8) | (battery_init_para[info->num][23]);
2502 //fa_cap = get_check_fuel_gauge_reg(info, FA_CAP_H_REG, FA_CAP_L_REG,
2505 info->soca->target_ibat = temp*2/10; /* calc 0.2C*/
2506 temp1 = (battery_init_para[info->num][0]<<8) | (battery_init_para[info->num][1]);
2508 info->soca->target_vsys = temp1 + ( info->soca->target_ibat * info->soca->Rsys ) / 1000;
2512 info->soca->target_ibat = info->fg_target_ibat;
2513 /* calc min vsys value */
2514 temp1 = (battery_init_para[info->num][0]<<8) | (battery_init_para[info->num][1]);
2515 temp = temp1 + ( info->soca->target_ibat * info->soca->Rsys ) / 1000;
2516 if( temp < info->fg_target_vsys) {
2517 info->soca->target_vsys = info->fg_target_vsys;
2519 info->soca->target_vsys = temp;
2520 RICOH_FG_DBG("PMU : %s : setting value of target vsys(%d) is out of range(%d)\n",__func__, info->fg_target_vsys, temp);
2525 RICOH_FG_DBG("PMU : %s : target_vsys is %d target_ibat is %d\n",__func__,info->soca->target_vsys,info->soca->target_ibat);
2527 if ((info->soca->target_ibat == 0) || (info->soca->target_vsys == 0)) { /* normal version */
2528 } else { /*Slice cutoff voltage version. */
2530 Ibat_min = -1 * info->soca->target_ibat;
2531 info->soca->cutoff_ocv = info->soca->target_vsys - Ibat_min * info->soca->Rsys / 1000;
2533 full_ocv = (battery_init_para[info->num][20]<<8) | (battery_init_para[info->num][21]);
2534 full_ocv = full_ocv * 5000 / 4095;
2536 ricoh619_scaling_OCV_table(info, info->soca->cutoff_ocv, full_ocv, &start_per, &end_per);
2538 /* calc available capacity */
2539 /* get avilable capacity */
2540 /* battery_init_para23-24 is designe capacity */
2541 available_cap = (battery_init_para[info->num][22]<<8)
2542 | (battery_init_para[info->num][23]);
2544 available_cap = available_cap
2545 * ((10000 - start_per) / 100) / 100 ;
2548 battery_init_para[info->num][23] = available_cap;
2549 battery_init_para[info->num][22] = available_cap >> 8;
2552 ret = ricoh619_clr_bits(info->dev->parent, FG_CTRL_REG, 0x01);
2554 dev_err(info->dev, "error in FG_En off\n");
2557 /////////////////////////////////
2558 ret = ricoh619_read_bank1(info->dev->parent, 0xDC, &val);
2560 dev_err(info->dev, "batterry initialize error\n");
2565 val &= 0x0F; //clear bit 4-7
2566 val |= 0x10; //set bit 4
2568 ret = ricoh619_write_bank1(info->dev->parent, 0xDC, val);
2570 dev_err(info->dev, "batterry initialize error\n");
2574 ret = ricoh619_read_bank1(info->dev->parent, 0xDC, &val2);
2576 dev_err(info->dev, "batterry initialize error\n");
2580 ret = ricoh619_write_bank1(info->dev->parent, 0xDC, val_temp);
2582 dev_err(info->dev, "batterry initialize error\n");
2586 RICOH_FG_DBG("PMU : %s : original 0x%x, before 0x%x, after 0x%x\n",__func__, val_temp, val, val2);
2589 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
2590 BAT_INIT_TOP_REG, 30, battery_init_para[info->num]);
2592 dev_err(info->dev, "batterry initialize error\n");
2596 ret = ricoh619_read_bank1(info->dev->parent, 0xD2, &val);
2598 dev_err(info->dev, "batterry initialize error\n");
2602 ret = ricoh619_read_bank1(info->dev->parent, 0xD3, &val2);
2604 dev_err(info->dev, "batterry initialize error\n");
2608 available_cap_ori = val2 + (val << 8);
2609 available_cap = battery_init_para[info->num][23]
2610 + (battery_init_para[info->num][22] << 8);
2612 if (available_cap_ori == available_cap) {
2613 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
2614 BAT_INIT_TOP_REG, 22, battery_init_para[info->num]);
2616 dev_err(info->dev, "batterry initialize error\n");
2620 for (i = 0; i < 6; i++) {
2621 ret = ricoh619_write_bank1(info->dev->parent, 0xD4+i, battery_init_para[info->num][24+i]);
2623 dev_err(info->dev, "batterry initialize error\n");
2628 ret = ricoh619_bulk_writes_bank1(info->dev->parent,
2629 BAT_INIT_TOP_REG, 30, battery_init_para[info->num]);
2631 dev_err(info->dev, "batterry initialize error\n");
2637 ////////////////////////////////
2644 /* Initial setting of battery */
2645 static int ricoh619_init_battery(struct ricoh619_battery_info *info)
2650 /* Need to implement initial setting of batery and error */
2651 /* -------------------------- */
2652 #ifdef ENABLE_FUEL_GAUGE_FUNCTION
2654 /* set relaxation state */
2655 if (RICOH619_REL1_SEL_VALUE > 240)
2658 val = RICOH619_REL1_SEL_VALUE / 16 ;
2660 /* set relaxation state */
2661 if (RICOH619_REL2_SEL_VALUE > 120)
2664 val2 = RICOH619_REL2_SEL_VALUE / 8 ;
2666 val = val + (val2 << 4);
2668 ret = ricoh619_write_bank1(info->dev->parent, BAT_REL_SEL_REG, val);
2670 dev_err(info->dev, "Error in writing BAT_REL_SEL_REG\n");
2674 ret = ricoh619_read_bank1(info->dev->parent, BAT_REL_SEL_REG, &val);
2675 RICOH_FG_DBG("PMU: ------- BAT_REL_SEL= %xh: =======\n",
2678 ret = ricoh619_write_bank1(info->dev->parent, BAT_TA_SEL_REG, 0x00);
2680 dev_err(info->dev, "Error in writing BAT_TA_SEL_REG\n");
2684 // ret = ricoh619_read(info->dev->parent, FG_CTRL_REG, &val);
2686 // dev_err(info->dev, "Error in reading the control register\n");
2690 // val = (val & 0x10) >> 4;
2691 // info->first_pwon = (val == 0) ? 1 : 0;
2692 ret = ricoh619_read(info->dev->parent, PSWR_REG, &val);
2694 dev_err(info->dev,"Error in reading PSWR_REG %d\n", ret);
2697 info->first_pwon = (val == 0) ? 1 : 0;
2700 ret = ricoh619_set_OCV_table(info);
2702 dev_err(info->dev, "Error in writing the OCV Tabler\n");
2706 ret = ricoh619_write(info->dev->parent, FG_CTRL_REG, 0x11);
2708 dev_err(info->dev, "Error in writing the control register\n");
2714 ret = ricoh619_write(info->dev->parent, VINDAC_REG, 0x03);
2716 dev_err(info->dev, "Error in writing the control register\n");
2720 if (info->alarm_vol_mv < 2700 || info->alarm_vol_mv > 3400) {
2721 dev_err(info->dev, "alarm_vol_mv is out of range!\n");
2728 /* Initial setting of charger */
2729 static int ricoh619_init_charger(struct ricoh619_battery_info *info)
2742 info->chg_stat1 = 0;
2744 err = ricoh619_set_bits(info->dev->parent, RICOH619_PWR_FUNC, 0x20);
2746 dev_err(info->dev, "Error in writing the PWR FUNC register\n");
2750 charge_status = get_power_supply_status(info);
2752 if (charge_status != POWER_SUPPLY_STATUS_FULL)
2754 /* Disable charging */
2755 err = ricoh619_clr_bits(info->dev->parent,CHGCTL1_REG, 0x03);
2757 dev_err(info->dev, "Error in writing the control register\n");
2763 err = ricoh619_read(info->dev->parent, REGISET1_REG,&val);
2764 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);
2766 /* REGISET1:(0xB6) setting */
2767 if ((info->ch_ilim_adp != 0xFF) || (info->ch_ilim_adp <= 0x1D)) {
2768 val = info->ch_ilim_adp;
2770 err = ricoh619_write(info->dev->parent, REGISET1_REG,val);
2772 dev_err(info->dev, "Error in writing REGISET1_REG %d\n",
2779 err = ricoh619_read(info->dev->parent, REGISET1_REG,&val);
2780 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);
2783 err = ricoh619_read(info->dev->parent, REGISET2_REG,&val);
2784 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);
2786 /* REGISET2:(0xB7) setting */
2787 err = ricoh619_read(info->dev->parent, REGISET2_REG, &val);
2790 "Error in read REGISET2_REG %d\n", err);
2794 if ((info->ch_ilim_usb != 0xFF) || (info->ch_ilim_usb <= 0x1D)) {
2795 val2 = info->ch_ilim_usb;
2796 } else {/* Keep OTP value */
2797 val2 = (val & 0x1F);
2805 err = ricoh619_write(info->dev->parent, REGISET2_REG,val);
2807 dev_err(info->dev, "Error in writing REGISET2_REG %d\n",
2813 err = ricoh619_read(info->dev->parent, REGISET2_REG,&val);
2814 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);
2816 /* CHGISET_REG(0xB8) setting */
2818 err = ricoh619_read(info->dev->parent, CHGISET_REG,&val);
2819 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);
2821 err = ricoh619_read(info->dev->parent, CHGISET_REG, &val);
2824 "Error in read CHGISET_REG %d\n", err);
2828 /* Define Current settings value for charging (bit 4~0)*/
2829 if ((info->ch_ichg != 0xFF) || (info->ch_ichg <= 0x1D)) {
2830 val2 = info->ch_ichg;
2831 } else { /* Keep OTP value */
2832 val2 = (val & 0x1F);
2835 /* Define Current settings at the charge completion (bit 7~6)*/
2836 if ((info->ch_icchg != 0xFF) || (info->ch_icchg <= 0x03)) {
2837 val3 = info->ch_icchg << 6;
2838 } else { /* Keep OTP value */
2839 val3 = (val & 0xC0);
2844 err = ricoh619_write(info->dev->parent, CHGISET_REG, val);
2846 dev_err(info->dev, "Error in writing CHGISET_REG %d\n",
2852 err = ricoh619_read(info->dev->parent, CHGISET_REG,&val);
2853 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);
2856 err = ricoh619_read(info->dev->parent, BATSET1_REG,&val);
2857 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);
2859 /* BATSET1_REG(0xBA) setting */
2860 err = ricoh619_read(info->dev->parent, BATSET1_REG, &val);
2863 "Error in read BATSET1 register %d\n", err);
2867 /* Define Battery overvoltage (bit 4)*/
2868 if ((info->ch_vbatovset != 0xFF) || (info->ch_vbatovset <= 0x1)) {
2869 val2 = info->ch_vbatovset;
2871 } else { /* Keep OTP value */
2872 val2 = (val & 0x10);
2875 /* keep bit 0-3 and bit 5-7 */
2880 err = ricoh619_write(info->dev->parent, BATSET1_REG, val);
2882 dev_err(info->dev, "Error in writing BAT1_REG %d\n",
2887 err = ricoh619_read(info->dev->parent, BATSET1_REG,&val);
2888 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);
2891 err = ricoh619_read(info->dev->parent, BATSET2_REG,&val);
2892 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);
2895 /* BATSET2_REG(0xBB) setting */
2896 err = ricoh619_read(info->dev->parent, BATSET2_REG, &val);
2899 "Error in read BATSET2 register %d\n", err);
2903 /* Define Re-charging voltage (bit 2~0)*/
2904 if ((info->ch_vrchg != 0xFF) || (info->ch_vrchg <= 0x04)) {
2905 val2 = info->ch_vrchg;
2906 } else { /* Keep OTP value */
2907 val2 = (val & 0x07);
2910 /* Define FULL charging voltage (bit 6~4)*/
2911 if ((info->ch_vfchg != 0xFF) || (info->ch_vfchg <= 0x04)) {
2912 val3 = info->ch_vfchg;
2914 } else { /* Keep OTP value */
2915 val3 = (val & 0x70);
2918 /* keep bit 3 and bit 7 */
2921 val = val + val2 + val3;
2923 err = ricoh619_write(info->dev->parent, BATSET2_REG, val);
2925 dev_err(info->dev, "Error in writing RICOH619_RE_CHARGE_VOLTAGE %d\n",
2931 err = ricoh619_read(info->dev->parent, BATSET2_REG,&val);
2932 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);
2934 /* Set rising edge setting ([1:0]=01b)for INT in charging */
2935 /* and rising edge setting ([3:2]=01b)for charge completion */
2936 err = ricoh619_read(info->dev->parent, RICOH619_CHG_STAT_DETMOD1, &val);
2938 dev_err(info->dev, "Error in reading CHG_STAT_DETMOD1 %d\n",
2944 err = ricoh619_write(info->dev->parent, RICOH619_CHG_STAT_DETMOD1, val);
2946 dev_err(info->dev, "Error in writing CHG_STAT_DETMOD1 %d\n",
2951 /* Unmask In charging/charge completion */
2952 err = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGSTS1, 0xfc);
2954 dev_err(info->dev, "Error in writing INT_MSK_CHGSTS1 %d\n",
2959 /* Set both edge for VUSB([3:2]=11b)/VADP([1:0]=11b) detect */
2960 err = ricoh619_read(info->dev->parent, RICOH619_CHG_CTRL_DETMOD1, &val);
2962 dev_err(info->dev, "Error in reading CHG_CTRL_DETMOD1 %d\n",
2968 err = ricoh619_write(info->dev->parent, RICOH619_CHG_CTRL_DETMOD1, val);
2970 dev_err(info->dev, "Error in writing CHG_CTRL_DETMOD1 %d\n",
2975 /* Unmask In VUSB/VADP completion */
2976 err = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGCTR, 0xfc);
2978 dev_err(info->dev, "Error in writing INT_MSK_CHGSTS1 %d\n",
2983 if (charge_status != POWER_SUPPLY_STATUS_FULL)
2985 /* Enable charging */
2986 err = ricoh619_set_bits(info->dev->parent,CHGCTL1_REG, 0x03);
2988 dev_err(info->dev, "Error in writing the control register\n");
2992 /* get OCV100_min, OCV100_min*/
2993 temp = (battery_init_para[info->num][24]<<8) | (battery_init_para[info->num][25]);
2994 rbat = temp * 1000 / 512 * 5000 / 4095;
2996 /* get vfchg value */
2997 err = ricoh619_read(info->dev->parent, BATSET2_REG, &val);
2999 dev_err(info->dev, "Error in reading the batset2reg\n");
3005 vfchg_val = 4050 + val2 * 50;
3009 RICOH_FG_DBG("PMU : %s : test test val %d, val2 %d vfchg %d\n", __func__, val, val2, vfchg_val);
3012 err = ricoh619_read(info->dev->parent, CHGISET_REG, &val);
3014 dev_err(info->dev, "Error in reading the chgisetreg\n");
3019 icchg_val = 50 + val2 * 50;
3020 RICOH_FG_DBG("PMU : %s : test test val %d, val2 %d icchg %d\n", __func__, val, val2, icchg_val);
3022 info->soca->OCV100_min = ( vfchg_val * 99 / 100 - (icchg_val * (rbat +20))/1000 - 20 ) * 1000;
3023 info->soca->OCV100_max = ( vfchg_val * 101 / 100 - (icchg_val * (rbat +20))/1000 + 20 ) * 1000;
3025 RICOH_FG_DBG("PMU : %s : 100 min %d, 100 max %d vfchg %d icchg %d rbat %d\n",__func__,
3026 info->soca->OCV100_min,info->soca->OCV100_max,vfchg_val,icchg_val,rbat);
3028 #ifdef ENABLE_LOW_BATTERY_DETECTION
3029 /* Set ADRQ=00 to stop ADC */
3030 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT3, 0x0);
3031 /* Enable VSYS threshold Low interrupt */
3032 ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
3033 /* Set ADC auto conversion interval 250ms */
3034 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT2, 0x0);
3035 /* Enable VSYS pin conversion in auto-ADC */
3036 // ricoh619_write(info->dev->parent, RICOH619_ADC_CNT1, 0x10);
3037 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT1, 0x16);
3038 /* Set VSYS threshold low voltage value = (voltage(V)*255)/(3*2.5) */
3039 val = info->alarm_vol_mv * 255 / 7500;
3040 ricoh619_write(info->dev->parent, RICOH619_ADC_VSYS_THL, val);
3041 /* Start auto-mode & average 4-time conversion mode for ADC */
3042 ricoh619_write(info->dev->parent, RICOH619_ADC_CNT3, 0x28);
3043 /* Enable master ADC INT */
3044 ricoh619_set_bits(info->dev->parent, RICOH619_INTC_INTEN, ADC_INT);
3052 static int get_power_supply_status(struct ricoh619_battery_info *info)
3055 uint8_t supply_state;
3056 uint8_t charge_state;
3059 /* get power supply status */
3060 ret = ricoh619_read(info->dev->parent, CHGSTATE_REG, &status);
3062 dev_err(info->dev, "Error in reading the control register\n");
3066 charge_state = (status & 0x1F);
3067 supply_state = ((status & 0xC0) >> 6);
3069 if (info->entry_factory_mode)
3070 return POWER_SUPPLY_STATUS_NOT_CHARGING;
3072 if (supply_state == SUPPLY_STATE_BAT) {
3073 info->soca->chg_status = POWER_SUPPLY_STATUS_DISCHARGING;
3075 switch (charge_state) {
3076 case CHG_STATE_CHG_OFF:
3077 info->soca->chg_status
3078 = POWER_SUPPLY_STATUS_DISCHARGING;
3080 case CHG_STATE_CHG_READY_VADP:
3081 info->soca->chg_status
3082 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3084 case CHG_STATE_CHG_TRICKLE:
3085 info->soca->chg_status
3086 = POWER_SUPPLY_STATUS_CHARGING;
3088 case CHG_STATE_CHG_RAPID:
3089 info->soca->chg_status
3090 = POWER_SUPPLY_STATUS_CHARGING;
3092 case CHG_STATE_CHG_COMPLETE:
3093 info->soca->chg_status
3094 = POWER_SUPPLY_STATUS_FULL;
3096 case CHG_STATE_SUSPEND:
3097 info->soca->chg_status
3098 = POWER_SUPPLY_STATUS_DISCHARGING;
3100 case CHG_STATE_VCHG_OVER_VOL:
3101 info->soca->chg_status
3102 = POWER_SUPPLY_STATUS_DISCHARGING;
3104 case CHG_STATE_BAT_ERROR:
3105 info->soca->chg_status
3106 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3108 case CHG_STATE_NO_BAT:
3109 info->soca->chg_status
3110 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3112 case CHG_STATE_BAT_OVER_VOL:
3113 info->soca->chg_status
3114 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3116 case CHG_STATE_BAT_TEMP_ERR:
3117 info->soca->chg_status
3118 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3120 case CHG_STATE_DIE_ERR:
3121 info->soca->chg_status
3122 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3124 case CHG_STATE_DIE_SHUTDOWN:
3125 info->soca->chg_status
3126 = POWER_SUPPLY_STATUS_DISCHARGING;
3128 case CHG_STATE_NO_BAT2:
3129 info->soca->chg_status
3130 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3132 case CHG_STATE_CHG_READY_VUSB:
3133 info->soca->chg_status
3134 = POWER_SUPPLY_STATUS_NOT_CHARGING;
3137 info->soca->chg_status
3138 = POWER_SUPPLY_STATUS_UNKNOWN;
3143 return info->soca->chg_status;
3146 static int get_power_supply_Android_status(struct ricoh619_battery_info *info)
3149 get_power_supply_status(info);
3151 /* get power supply status */
3152 if (info->entry_factory_mode)
3153 return POWER_SUPPLY_STATUS_NOT_CHARGING;
3155 switch (info->soca->chg_status) {
3156 case POWER_SUPPLY_STATUS_UNKNOWN:
3157 return POWER_SUPPLY_STATUS_UNKNOWN;
3160 case POWER_SUPPLY_STATUS_NOT_CHARGING:
3161 return POWER_SUPPLY_STATUS_NOT_CHARGING;
3164 case POWER_SUPPLY_STATUS_DISCHARGING:
3165 return POWER_SUPPLY_STATUS_DISCHARGING;
3168 case POWER_SUPPLY_STATUS_CHARGING:
3169 return POWER_SUPPLY_STATUS_CHARGING;
3172 case POWER_SUPPLY_STATUS_FULL:
3173 if(info->soca->displayed_soc == 100 * 100) {
3174 return POWER_SUPPLY_STATUS_FULL;
3176 return POWER_SUPPLY_STATUS_CHARGING;
3180 return POWER_SUPPLY_STATUS_UNKNOWN;
3184 return POWER_SUPPLY_STATUS_UNKNOWN;
3186 extern struct ricoh619 *g_ricoh619;
3187 static void charger_irq_work(struct work_struct *work)
3189 struct ricoh619_battery_info *info
3190 = container_of(work, struct ricoh619_battery_info, irq_work);
3193 RICOH_FG_DBG("PMU:%s In\n", __func__);
3195 power_supply_changed(&info->battery);
3196 power_supply_changed(&powerac);
3197 power_supply_changed(&powerusb);
3199 // mutex_lock(&info->lock);
3201 if (info->chg_stat1 & 0x01) {
3202 ricoh619_read(info->dev->parent, CHGSTATE_REG, ®_val);
3203 if (reg_val & 0x40) { /* USE ADP */
3204 #ifdef SUPPORT_USB_CONNECT_TO_ADP
3207 RICOH_FG_DBG("PMU:%s usb det dwc_otg_check_dpdm =%d\n", __func__,dwc_otg_check_dpdm(0));
3208 if(2 == dwc_otg_check_dpdm(0)){
3209 /* set adp limit current 2A */
3210 ricoh619_write(info->dev->parent, REGISET1_REG, 0x16);
3211 /* set charge current 2A */
3212 ricoh619_write(info->dev->parent, CHGISET_REG, 0xD3);
3215 /* set adp limit current 500ma */
3216 ricoh619_write(info->dev->parent, REGISET1_REG, 0x04);
3217 /* set charge current 500ma */
3218 ricoh619_write(info->dev->parent, CHGISET_REG, 0xc4);
3221 power_supply_changed(&info->battery);
3222 power_supply_changed(&powerac);
3223 power_supply_changed(&powerusb);
3226 #else //support adp and usb chag
3227 if (gpio_is_valid(g_ricoh619->dc_det)){
3228 ret = gpio_request(g_ricoh619->dc_det, "ricoh619_dc_det");
3230 RICOH_FG_DBG("Failed to request gpio %d with ret:""%d\n",g_ricoh619->dc_det, ret);
3232 gpio_direction_input(g_ricoh619->dc_det);
3233 ret = gpio_get_value(g_ricoh619->dc_det);
3236 /* set adp limit current 2A */
3237 ricoh619_write(info->dev->parent, REGISET1_REG, 0x16);
3238 /* set charge current 2A */
3239 ricoh619_write(info->dev->parent, CHGISET_REG, 0xD3);
3242 /* set adp limit current 500ma */
3243 ricoh619_write(info->dev->parent, REGISET1_REG, 0x04);
3244 /* set charge current 500ma */
3245 ricoh619_write(info->dev->parent, CHGISET_REG, 0xc4);
3247 gpio_free(g_ricoh619->dc_det);
3250 /* set adp limit current 2A */
3251 ricoh619_write(info->dev->parent, REGISET1_REG, 0x16);
3252 /* set charge current 2A */
3253 ricoh619_write(info->dev->parent, CHGISET_REG, 0xD3);
3256 } else if (reg_val & 0x80) { /* USE ONLY USB */
3257 queue_work(info->usb_workqueue, &info->usb_irq_work);
3261 info->chg_stat1 = 0;
3263 /* Enable Interrupt for VADP/USB */
3264 ret = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGCTR, 0xfc);
3267 "%s(): Error in enable charger mask INT %d\n",
3270 /* Enable Interrupt for Charging & complete */
3271 ret = ricoh619_write(info->dev->parent, RICOH619_INT_MSK_CHGSTS1, 0xfc);
3274 "%s(): Error in enable charger mask INT %d\n",
3277 // mutex_unlock(&info->lock);
3278 RICOH_FG_DBG("PMU:%s Out\n", __func__);
3281 #ifdef ENABLE_LOW_BATTERY_DETECTION
3282 static void low_battery_irq_work(struct work_struct *work)
3284 struct ricoh619_battery_info *info = container_of(work,
3285 struct ricoh619_battery_info, low_battery_work.work);
3289 RICOH_FG_DBG("PMU:%s In\n", __func__);
3291 power_supply_changed(&info->battery);
3293 /* Enable VADP threshold Low interrupt */
3294 ricoh619_write(info->dev->parent, RICOH619_INT_EN_ADC1, 0x10);
3297 "%s(): Error in enable adc mask INT %d\n",
3303 static void ricoh619_usb_charge_det(void)
3305 struct ricoh619 *ricoh619 = g_ricoh619;
3306 ricoh619_set_bits(ricoh619->dev,REGISET2_REG,(1 << 7)); //set usb limit current when SDP or other mode
3307 RICOH_FG_DBG("PMU:%s usb det dwc_otg_check_dpdm =%d\n", __func__,dwc_otg_check_dpdm(0));
3308 if(2 == dwc_otg_check_dpdm(0)){
3309 ricoh619_write(ricoh619->dev,REGISET2_REG,0x16); //set usb limit current 2A
3310 ricoh619_write(ricoh619->dev,CHGISET_REG,0xD3); //set charge current 2A
3313 ricoh619_write(ricoh619->dev,REGISET2_REG,0x04); //set usb limit current 500ma
3314 ricoh619_write(ricoh619->dev,CHGISET_REG,0xC4); //set charge current 500ma
3316 power_supply_changed(&powerac);
3317 power_supply_changed(&powerusb);
3320 static void usb_det_irq_work(struct work_struct *work)
3322 struct ricoh619_battery_info *info = container_of(work,
3323 struct ricoh619_battery_info, usb_irq_work);
3327 RICOH_FG_DBG("PMU:%s In\n", __func__);
3329 power_supply_changed(&info->battery);
3330 power_supply_changed(&powerac);
3331 power_supply_changed(&powerusb);
3333 mutex_lock(&info->lock);
3335 /* Enable Interrupt for VUSB */
3336 ret = ricoh619_clr_bits(info->dev->parent,
3337 RICOH619_INT_MSK_CHGCTR, 0x02);
3340 "%s(): Error in enable charger mask INT %d\n",
3343 mutex_unlock(&info->lock);
3344 ret = ricoh619_read(info->dev->parent, RICOH619_INT_MON_CHGCTR, &sts);
3346 dev_err(info->dev, "Error in reading the control register\n");
3350 ricoh619_usb_charge_det();
3352 RICOH_FG_DBG("PMU:%s Out\n", __func__);
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");